]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/bluetooth/hci_bcm.c
drivers/regulator: fix a missing check of return value
[mirror_ubuntu-bionic-kernel.git] / drivers / bluetooth / hci_bcm.c
CommitLineData
e9a2dd26
MH
1/*
2 *
3 * Bluetooth HCI UART driver for Broadcom devices
4 *
5 * Copyright (C) 2015 Intel Corporation
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/errno.h>
26#include <linux/skbuff.h>
18aeb444 27#include <linux/firmware.h>
0395ffc1
FD
28#include <linux/module.h>
29#include <linux/acpi.h>
33cd149e
LP
30#include <linux/of.h>
31#include <linux/property.h>
0395ffc1
FD
32#include <linux/platform_device.h>
33#include <linux/clk.h>
34#include <linux/gpio/consumer.h>
35#include <linux/tty.h>
6cc4396c 36#include <linux/interrupt.h>
5cebdfea 37#include <linux/dmi.h>
e88ab30d 38#include <linux/pm_runtime.h>
33cd149e 39#include <linux/serdev.h>
e9a2dd26
MH
40
41#include <net/bluetooth/bluetooth.h>
42#include <net/bluetooth/hci_core.h>
43
bdd8818e 44#include "btbcm.h"
e9a2dd26 45#include "hci_uart.h"
bdd8818e 46
01d5e44a
MH
47#define BCM_NULL_PKT 0x00
48#define BCM_NULL_SIZE 0
49
94c58132
MH
50#define BCM_LM_DIAG_PKT 0x07
51#define BCM_LM_DIAG_SIZE 63
52
e88ab30d
FD
53#define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
54
8a920568 55/* device driver resources */
0395ffc1 56struct bcm_device {
8a920568
HG
57 /* Must be the first member, hci_serdev.c expects this. */
58 struct hci_uart serdev_hu;
0395ffc1
FD
59 struct list_head list;
60
c0d3ce58 61 struct device *dev;
0395ffc1
FD
62
63 const char *name;
64 struct gpio_desc *device_wakeup;
65 struct gpio_desc *shutdown;
66
67 struct clk *clk;
68 bool clk_enabled;
ae056908
FD
69
70 u32 init_speed;
74183a1c 71 u32 oper_speed;
6cc4396c 72 int irq;
227630cc 73 bool irq_active_low;
118612fb 74
b7a622a2 75#ifdef CONFIG_PM
118612fb
FD
76 struct hci_uart *hu;
77 bool is_suspended; /* suspend/resume flag */
78#endif
0395ffc1
FD
79};
80
33cd149e 81/* generic bcm uart resources */
bdd8818e 82struct bcm_data {
0395ffc1
FD
83 struct sk_buff *rx_skb;
84 struct sk_buff_head txq;
85
86 struct bcm_device *dev;
bdd8818e
MH
87};
88
0395ffc1 89/* List of BCM BT UART devices */
bb3ea16a 90static DEFINE_MUTEX(bcm_device_lock);
0395ffc1
FD
91static LIST_HEAD(bcm_device_list);
92
5bffac2b
HG
93static int irq_polarity = -1;
94module_param(irq_polarity, int, 0444);
95MODULE_PARM_DESC(irq_polarity, "IRQ polarity 0: active-high 1: active-low");
96
33cd149e
LP
97static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
98{
99 if (hu->serdev)
100 serdev_device_set_baudrate(hu->serdev, speed);
101 else
102 hci_uart_set_baudrate(hu, speed);
103}
104
61b2fc2b
FD
105static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
106{
107 struct hci_dev *hdev = hu->hdev;
108 struct sk_buff *skb;
109 struct bcm_update_uart_baud_rate param;
110
111 if (speed > 3000000) {
112 struct bcm_write_uart_clock_setting clock;
113
114 clock.type = BCM_UART_CLOCK_48MHZ;
115
65ad07c9 116 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
61b2fc2b
FD
117
118 /* This Broadcom specific command changes the UART's controller
119 * clock for baud rate > 3000000.
120 */
121 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
122 if (IS_ERR(skb)) {
123 int err = PTR_ERR(skb);
65ad07c9
FD
124 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
125 err);
61b2fc2b
FD
126 return err;
127 }
128
129 kfree_skb(skb);
130 }
131
65ad07c9 132 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
61b2fc2b
FD
133
134 param.zero = cpu_to_le16(0);
135 param.baud_rate = cpu_to_le32(speed);
136
137 /* This Broadcom specific command changes the UART's controller baud
138 * rate.
139 */
140 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
141 HCI_INIT_TIMEOUT);
142 if (IS_ERR(skb)) {
143 int err = PTR_ERR(skb);
65ad07c9
FD
144 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
145 err);
61b2fc2b
FD
146 return err;
147 }
148
149 kfree_skb(skb);
150
151 return 0;
152}
153
917522aa 154/* bcm_device_exists should be protected by bcm_device_lock */
0395ffc1
FD
155static bool bcm_device_exists(struct bcm_device *device)
156{
157 struct list_head *p;
158
81a19053 159#ifdef CONFIG_PM
8a920568
HG
160 /* Devices using serdev always exist */
161 if (device && device->hu && device->hu->serdev)
162 return true;
81a19053 163#endif
8a920568 164
0395ffc1
FD
165 list_for_each(p, &bcm_device_list) {
166 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
167
168 if (device == dev)
169 return true;
170 }
171
172 return false;
173}
174
175static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
176{
177 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
730ce397 178 clk_prepare_enable(dev->clk);
0395ffc1 179
2af0a709
LP
180 gpiod_set_value(dev->shutdown, powered);
181 gpiod_set_value(dev->device_wakeup, powered);
0395ffc1
FD
182
183 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
730ce397 184 clk_disable_unprepare(dev->clk);
0395ffc1
FD
185
186 dev->clk_enabled = powered;
187
188 return 0;
189}
190
b7a622a2 191#ifdef CONFIG_PM
6cc4396c
FD
192static irqreturn_t bcm_host_wake(int irq, void *data)
193{
194 struct bcm_device *bdev = data;
195
196 bt_dev_dbg(bdev, "Host wake IRQ");
197
c0d3ce58
HG
198 pm_runtime_get(bdev->dev);
199 pm_runtime_mark_last_busy(bdev->dev);
200 pm_runtime_put_autosuspend(bdev->dev);
e88ab30d 201
6cc4396c
FD
202 return IRQ_HANDLED;
203}
204
205static int bcm_request_irq(struct bcm_data *bcm)
206{
207 struct bcm_device *bdev = bcm->dev;
98dc77d5 208 int err;
6cc4396c 209
6cc4396c
FD
210 mutex_lock(&bcm_device_lock);
211 if (!bcm_device_exists(bdev)) {
212 err = -ENODEV;
213 goto unlock;
214 }
215
98dc77d5
LP
216 if (bdev->irq <= 0) {
217 err = -EOPNOTSUPP;
218 goto unlock;
219 }
6cc4396c 220
c0d3ce58 221 err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
227630cc
HG
222 bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
223 IRQF_TRIGGER_RISING,
224 "host_wake", bdev);
98dc77d5
LP
225 if (err)
226 goto unlock;
e88ab30d 227
c0d3ce58 228 device_init_wakeup(bdev->dev, true);
98dc77d5 229
c0d3ce58 230 pm_runtime_set_autosuspend_delay(bdev->dev,
98dc77d5 231 BCM_AUTOSUSPEND_DELAY);
c0d3ce58
HG
232 pm_runtime_use_autosuspend(bdev->dev);
233 pm_runtime_set_active(bdev->dev);
234 pm_runtime_enable(bdev->dev);
6cc4396c
FD
235
236unlock:
237 mutex_unlock(&bcm_device_lock);
238
239 return err;
240}
241
242static const struct bcm_set_sleep_mode default_sleep_params = {
243 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
244 .idle_host = 2, /* idle threshold HOST, in 300ms */
245 .idle_dev = 2, /* idle threshold device, in 300ms */
246 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
247 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
248 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
e88ab30d 249 .combine_modes = 1, /* Combine sleep and LPM flag */
6cc4396c
FD
250 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
251 /* Irrelevant USB flags */
252 .usb_auto_sleep = 0,
253 .usb_resume_timeout = 0,
254 .pulsed_host_wake = 0,
255 .break_to_host = 0
256};
257
258static int bcm_setup_sleep(struct hci_uart *hu)
259{
260 struct bcm_data *bcm = hu->priv;
261 struct sk_buff *skb;
262 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
263
227630cc 264 sleep_params.host_wake_active = !bcm->dev->irq_active_low;
6cc4396c
FD
265
266 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
267 &sleep_params, HCI_INIT_TIMEOUT);
268 if (IS_ERR(skb)) {
269 int err = PTR_ERR(skb);
270 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
271 return err;
272 }
273 kfree_skb(skb);
274
275 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
276
277 return 0;
278}
279#else
280static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
281static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
282#endif
283
075e1f5e
MH
284static int bcm_set_diag(struct hci_dev *hdev, bool enable)
285{
286 struct hci_uart *hu = hci_get_drvdata(hdev);
287 struct bcm_data *bcm = hu->priv;
288 struct sk_buff *skb;
289
290 if (!test_bit(HCI_RUNNING, &hdev->flags))
291 return -ENETDOWN;
292
293 skb = bt_skb_alloc(3, GFP_KERNEL);
a1857390
DC
294 if (!skb)
295 return -ENOMEM;
075e1f5e 296
634fef61
JB
297 skb_put_u8(skb, BCM_LM_DIAG_PKT);
298 skb_put_u8(skb, 0xf0);
299 skb_put_u8(skb, enable);
075e1f5e
MH
300
301 skb_queue_tail(&bcm->txq, skb);
302 hci_uart_tx_wakeup(hu);
303
304 return 0;
305}
306
bdd8818e
MH
307static int bcm_open(struct hci_uart *hu)
308{
309 struct bcm_data *bcm;
0395ffc1 310 struct list_head *p;
bdd8818e 311
65ad07c9 312 bt_dev_dbg(hu->hdev, "hu %p", hu);
bdd8818e 313
94d81df9
VD
314 if (!hci_uart_has_flow_control(hu))
315 return -EOPNOTSUPP;
316
bdd8818e
MH
317 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
318 if (!bcm)
319 return -ENOMEM;
320
321 skb_queue_head_init(&bcm->txq);
322
323 hu->priv = bcm;
0395ffc1 324
8a920568
HG
325 mutex_lock(&bcm_device_lock);
326
33cd149e
LP
327 if (hu->serdev) {
328 serdev_device_open(hu->serdev);
8a920568 329 bcm->dev = serdev_device_get_drvdata(hu->serdev);
33cd149e
LP
330 goto out;
331 }
332
95065a61
JH
333 if (!hu->tty->dev)
334 goto out;
335
0395ffc1
FD
336 list_for_each(p, &bcm_device_list) {
337 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
338
339 /* Retrieve saved bcm_device based on parent of the
340 * platform device (saved during device probe) and
341 * parent of tty device used by hci_uart
342 */
c0d3ce58 343 if (hu->tty->dev->parent == dev->dev->parent) {
0395ffc1 344 bcm->dev = dev;
b7a622a2 345#ifdef CONFIG_PM
118612fb
FD
346 dev->hu = hu;
347#endif
0395ffc1
FD
348 break;
349 }
350 }
351
95065a61 352out:
8a920568
HG
353 if (bcm->dev) {
354 hu->init_speed = bcm->dev->init_speed;
355 hu->oper_speed = bcm->dev->oper_speed;
356 bcm_gpio_set_power(bcm->dev, true);
357 }
358
359 mutex_unlock(&bcm_device_lock);
bdd8818e
MH
360 return 0;
361}
362
363static int bcm_close(struct hci_uart *hu)
364{
365 struct bcm_data *bcm = hu->priv;
8a920568 366 struct bcm_device *bdev = NULL;
bdd8818e 367
65ad07c9 368 bt_dev_dbg(hu->hdev, "hu %p", hu);
bdd8818e 369
0395ffc1 370 /* Protect bcm->dev against removal of the device or driver */
bb3ea16a 371 mutex_lock(&bcm_device_lock);
8a920568
HG
372
373 if (hu->serdev) {
374 serdev_device_close(hu->serdev);
375 bdev = serdev_device_get_drvdata(hu->serdev);
376 } else if (bcm_device_exists(bcm->dev)) {
377 bdev = bcm->dev;
378#ifdef CONFIG_PM
379 bdev->hu = NULL;
380#endif
381 }
382
383 if (bdev) {
6cc4396c 384 bcm_gpio_set_power(bdev, false);
b7a622a2 385#ifdef CONFIG_PM
c0d3ce58
HG
386 pm_runtime_disable(bdev->dev);
387 pm_runtime_set_suspended(bdev->dev);
e88ab30d 388
514a00bf 389 if (bdev->irq > 0) {
c0d3ce58
HG
390 devm_free_irq(bdev->dev, bdev->irq, bdev);
391 device_init_wakeup(bdev->dev, false);
6cc4396c 392 }
118612fb
FD
393#endif
394 }
bb3ea16a 395 mutex_unlock(&bcm_device_lock);
0395ffc1 396
bdd8818e
MH
397 skb_queue_purge(&bcm->txq);
398 kfree_skb(bcm->rx_skb);
399 kfree(bcm);
400
401 hu->priv = NULL;
402 return 0;
403}
404
405static int bcm_flush(struct hci_uart *hu)
406{
407 struct bcm_data *bcm = hu->priv;
408
65ad07c9 409 bt_dev_dbg(hu->hdev, "hu %p", hu);
bdd8818e
MH
410
411 skb_queue_purge(&bcm->txq);
412
413 return 0;
414}
415
416static int bcm_setup(struct hci_uart *hu)
417{
6cc4396c 418 struct bcm_data *bcm = hu->priv;
6be09b48
FD
419 char fw_name[64];
420 const struct firmware *fw;
960ef1d7 421 unsigned int speed;
6be09b48
FD
422 int err;
423
65ad07c9 424 bt_dev_dbg(hu->hdev, "hu %p", hu);
bdd8818e 425
075e1f5e 426 hu->hdev->set_diag = bcm_set_diag;
bdd8818e
MH
427 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
428
6be09b48
FD
429 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
430 if (err)
431 return err;
432
433 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
434 if (err < 0) {
65ad07c9 435 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
6be09b48
FD
436 return 0;
437 }
438
439 err = btbcm_patchram(hu->hdev, fw);
440 if (err) {
65ad07c9 441 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
6be09b48
FD
442 goto finalize;
443 }
444
960ef1d7
FD
445 /* Init speed if any */
446 if (hu->init_speed)
447 speed = hu->init_speed;
448 else if (hu->proto->init_speed)
449 speed = hu->proto->init_speed;
450 else
451 speed = 0;
452
453 if (speed)
33cd149e 454 host_set_baudrate(hu, speed);
960ef1d7
FD
455
456 /* Operational speed if any */
457 if (hu->oper_speed)
458 speed = hu->oper_speed;
459 else if (hu->proto->oper_speed)
460 speed = hu->proto->oper_speed;
461 else
462 speed = 0;
463
464 if (speed) {
465 err = bcm_set_baudrate(hu, speed);
61b2fc2b 466 if (!err)
33cd149e 467 host_set_baudrate(hu, speed);
61b2fc2b
FD
468 }
469
6be09b48
FD
470finalize:
471 release_firmware(fw);
472
473 err = btbcm_finalize(hu->hdev);
6cc4396c
FD
474 if (err)
475 return err;
476
cdd24a20 477 if (!bcm_request_irq(bcm))
6cc4396c 478 err = bcm_setup_sleep(hu);
6be09b48
FD
479
480 return err;
bdd8818e
MH
481}
482
94c58132
MH
483#define BCM_RECV_LM_DIAG \
484 .type = BCM_LM_DIAG_PKT, \
485 .hlen = BCM_LM_DIAG_SIZE, \
486 .loff = 0, \
487 .lsize = 0, \
488 .maxlen = BCM_LM_DIAG_SIZE
489
01d5e44a
MH
490#define BCM_RECV_NULL \
491 .type = BCM_NULL_PKT, \
492 .hlen = BCM_NULL_SIZE, \
493 .loff = 0, \
494 .lsize = 0, \
495 .maxlen = BCM_NULL_SIZE
496
79b8df93 497static const struct h4_recv_pkt bcm_recv_pkts[] = {
94c58132
MH
498 { H4_RECV_ACL, .recv = hci_recv_frame },
499 { H4_RECV_SCO, .recv = hci_recv_frame },
500 { H4_RECV_EVENT, .recv = hci_recv_frame },
501 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
01d5e44a 502 { BCM_RECV_NULL, .recv = hci_recv_diag },
79b8df93
MH
503};
504
bdd8818e
MH
505static int bcm_recv(struct hci_uart *hu, const void *data, int count)
506{
507 struct bcm_data *bcm = hu->priv;
508
509 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
510 return -EUNATCH;
511
79b8df93
MH
512 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
513 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
bdd8818e
MH
514 if (IS_ERR(bcm->rx_skb)) {
515 int err = PTR_ERR(bcm->rx_skb);
65ad07c9 516 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
37134167 517 bcm->rx_skb = NULL;
bdd8818e 518 return err;
e88ab30d
FD
519 } else if (!bcm->rx_skb) {
520 /* Delay auto-suspend when receiving completed packet */
521 mutex_lock(&bcm_device_lock);
522 if (bcm->dev && bcm_device_exists(bcm->dev)) {
c0d3ce58
HG
523 pm_runtime_get(bcm->dev->dev);
524 pm_runtime_mark_last_busy(bcm->dev->dev);
525 pm_runtime_put_autosuspend(bcm->dev->dev);
e88ab30d
FD
526 }
527 mutex_unlock(&bcm_device_lock);
bdd8818e
MH
528 }
529
530 return count;
531}
532
533static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
534{
535 struct bcm_data *bcm = hu->priv;
536
65ad07c9 537 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
bdd8818e
MH
538
539 /* Prepend skb with frame type */
618e8bc2 540 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
bdd8818e
MH
541 skb_queue_tail(&bcm->txq, skb);
542
543 return 0;
544}
545
546static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
547{
548 struct bcm_data *bcm = hu->priv;
e88ab30d
FD
549 struct sk_buff *skb = NULL;
550 struct bcm_device *bdev = NULL;
551
552 mutex_lock(&bcm_device_lock);
553
554 if (bcm_device_exists(bcm->dev)) {
555 bdev = bcm->dev;
c0d3ce58 556 pm_runtime_get_sync(bdev->dev);
e88ab30d
FD
557 /* Shall be resumed here */
558 }
559
560 skb = skb_dequeue(&bcm->txq);
561
562 if (bdev) {
c0d3ce58
HG
563 pm_runtime_mark_last_busy(bdev->dev);
564 pm_runtime_put_autosuspend(bdev->dev);
e88ab30d 565 }
bdd8818e 566
e88ab30d
FD
567 mutex_unlock(&bcm_device_lock);
568
569 return skb;
bdd8818e
MH
570}
571
b7a622a2
FD
572#ifdef CONFIG_PM
573static int bcm_suspend_device(struct device *dev)
118612fb 574{
78277d73 575 struct bcm_device *bdev = dev_get_drvdata(dev);
118612fb 576
b7a622a2 577 bt_dev_dbg(bdev, "");
917522aa 578
b7a622a2 579 if (!bdev->is_suspended && bdev->hu) {
118612fb
FD
580 hci_uart_set_flow_control(bdev->hu, true);
581
b7a622a2 582 /* Once this returns, driver suspends BT via GPIO */
118612fb
FD
583 bdev->is_suspended = true;
584 }
585
586 /* Suspend the device */
c926558a
LW
587 gpiod_set_value(bdev->device_wakeup, false);
588 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
589 mdelay(15);
118612fb 590
b7a622a2
FD
591 return 0;
592}
593
594static int bcm_resume_device(struct device *dev)
595{
78277d73 596 struct bcm_device *bdev = dev_get_drvdata(dev);
b7a622a2
FD
597
598 bt_dev_dbg(bdev, "");
599
c926558a
LW
600 gpiod_set_value(bdev->device_wakeup, true);
601 bt_dev_dbg(bdev, "resume, delaying 15 ms");
602 mdelay(15);
b7a622a2
FD
603
604 /* When this executes, the device has woken up already */
605 if (bdev->is_suspended && bdev->hu) {
606 bdev->is_suspended = false;
607
608 hci_uart_set_flow_control(bdev->hu, false);
609 }
610
611 return 0;
612}
613#endif
614
615#ifdef CONFIG_PM_SLEEP
8a920568 616/* suspend callback */
b7a622a2
FD
617static int bcm_suspend(struct device *dev)
618{
78277d73 619 struct bcm_device *bdev = dev_get_drvdata(dev);
b7a622a2
FD
620 int error;
621
622 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
623
8a920568
HG
624 /*
625 * When used with a device instantiated as platform_device, bcm_suspend
626 * can be called at any time as long as the platform device is bound,
627 * so it should use bcm_device_lock to protect access to hci_uart
b7a622a2
FD
628 * and device_wake-up GPIO.
629 */
630 mutex_lock(&bcm_device_lock);
631
632 if (!bdev->hu)
633 goto unlock;
634
e88ab30d
FD
635 if (pm_runtime_active(dev))
636 bcm_suspend_device(dev);
b7a622a2 637
514a00bf 638 if (device_may_wakeup(dev) && bdev->irq > 0) {
6cc4396c
FD
639 error = enable_irq_wake(bdev->irq);
640 if (!error)
641 bt_dev_dbg(bdev, "BCM irq: enabled");
642 }
643
917522aa 644unlock:
bb3ea16a 645 mutex_unlock(&bcm_device_lock);
917522aa 646
118612fb
FD
647 return 0;
648}
649
8a920568 650/* resume callback */
118612fb
FD
651static int bcm_resume(struct device *dev)
652{
78277d73 653 struct bcm_device *bdev = dev_get_drvdata(dev);
118612fb 654
65ad07c9 655 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
118612fb 656
8a920568
HG
657 /*
658 * When used with a device instantiated as platform_device, bcm_resume
659 * can be called at any time as long as platform device is bound,
660 * so it should use bcm_device_lock to protect access to hci_uart
b7a622a2
FD
661 * and device_wake-up GPIO.
662 */
bb3ea16a 663 mutex_lock(&bcm_device_lock);
917522aa
FD
664
665 if (!bdev->hu)
666 goto unlock;
667
514a00bf 668 if (device_may_wakeup(dev) && bdev->irq > 0) {
6cc4396c
FD
669 disable_irq_wake(bdev->irq);
670 bt_dev_dbg(bdev, "BCM irq: disabled");
671 }
672
b7a622a2 673 bcm_resume_device(dev);
118612fb 674
917522aa 675unlock:
bb3ea16a 676 mutex_unlock(&bcm_device_lock);
917522aa 677
e88ab30d
FD
678 pm_runtime_disable(dev);
679 pm_runtime_set_active(dev);
680 pm_runtime_enable(dev);
681
118612fb
FD
682 return 0;
683}
684#endif
685
89ab37b4
DD
686static const struct acpi_gpio_params int_last_device_wakeup_gpios = { 0, 0, false };
687static const struct acpi_gpio_params int_last_shutdown_gpios = { 1, 0, false };
688static const struct acpi_gpio_params int_last_host_wakeup_gpios = { 2, 0, false };
689
690static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
691 { "device-wakeup-gpios", &int_last_device_wakeup_gpios, 1 },
692 { "shutdown-gpios", &int_last_shutdown_gpios, 1 },
693 { "host-wakeup-gpios", &int_last_host_wakeup_gpios, 1 },
694 { },
695};
696
697static const struct acpi_gpio_params int_first_host_wakeup_gpios = { 0, 0, false };
698static const struct acpi_gpio_params int_first_device_wakeup_gpios = { 1, 0, false };
699static const struct acpi_gpio_params int_first_shutdown_gpios = { 2, 0, false };
700
701static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
702 { "device-wakeup-gpios", &int_first_device_wakeup_gpios, 1 },
703 { "shutdown-gpios", &int_first_shutdown_gpios, 1 },
704 { "host-wakeup-gpios", &int_first_host_wakeup_gpios, 1 },
0395ffc1
FD
705 { },
706};
707
50d78bcf 708#ifdef CONFIG_ACPI
5cebdfea 709/* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
227630cc 710static const struct dmi_system_id bcm_active_low_irq_dmi_table[] = {
5e2bd93b
JB
711 { /* Handle ThinkPad 8 tablets with BCM2E55 chipset ACPI ID */
712 .ident = "Lenovo ThinkPad 8",
713 .matches = {
714 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
715 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "ThinkPad 8"),
716 },
5e2bd93b 717 },
1bdb68b2
IM
718 {
719 .ident = "MINIX Z83-4",
720 .matches = {
721 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MINIX"),
722 DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
723 },
724 },
5cebdfea
FD
725 { }
726};
727
ae056908
FD
728static int bcm_resource(struct acpi_resource *ares, void *data)
729{
730 struct bcm_device *dev = data;
6cc4396c
FD
731 struct acpi_resource_extended_irq *irq;
732 struct acpi_resource_gpio *gpio;
733 struct acpi_resource_uart_serialbus *sb;
734
735 switch (ares->type) {
736 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
737 irq = &ares->data.extended_irq;
09d86354
HG
738 if (irq->polarity != ACPI_ACTIVE_LOW)
739 dev_info(dev->dev, "ACPI Interrupt resource is active-high, this is usually wrong, treating the IRQ as active-low\n");
740 dev->irq_active_low = true;
6cc4396c
FD
741 break;
742
743 case ACPI_RESOURCE_TYPE_GPIO:
744 gpio = &ares->data.gpio;
745 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
227630cc 746 dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
6cc4396c
FD
747 break;
748
749 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
ae056908 750 sb = &ares->data.uart_serial_bus;
74183a1c 751 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
ae056908 752 dev->init_speed = sb->default_baud_rate;
74183a1c
MH
753 dev->oper_speed = 4000000;
754 }
6cc4396c
FD
755 break;
756
757 default:
758 break;
ae056908
FD
759 }
760
9d54fd6a 761 return 0;
ae056908 762}
212d7183 763#endif /* CONFIG_ACPI */
ae056908 764
42ef18f0 765static int bcm_get_resources(struct bcm_device *dev)
0395ffc1 766{
c0d3ce58 767 dev->name = dev_name(dev->dev);
0395ffc1 768
c0d3ce58 769 dev->clk = devm_clk_get(dev->dev, NULL);
89ab37b4 770
d76a1513
CYT
771 /* Handle deferred probing */
772 if (dev->clk == ERR_PTR(-EPROBE_DEFER))
773 return PTR_ERR(dev->clk);
774
aee67da6
SW
775 dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup",
776 GPIOD_OUT_LOW);
62aaefa7
UKK
777 if (IS_ERR(dev->device_wakeup))
778 return PTR_ERR(dev->device_wakeup);
0395ffc1 779
aee67da6
SW
780 dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown",
781 GPIOD_OUT_LOW);
62aaefa7
UKK
782 if (IS_ERR(dev->shutdown))
783 return PTR_ERR(dev->shutdown);
0395ffc1 784
6cc4396c 785 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
6cc4396c
FD
786 if (dev->irq <= 0) {
787 struct gpio_desc *gpio;
788
c0d3ce58 789 gpio = devm_gpiod_get_optional(dev->dev, "host-wakeup",
6cc4396c
FD
790 GPIOD_IN);
791 if (IS_ERR(gpio))
792 return PTR_ERR(gpio);
793
794 dev->irq = gpiod_to_irq(gpio);
795 }
796
c0d3ce58 797 dev_info(dev->dev, "BCM irq: %d\n", dev->irq);
212d7183
AS
798 return 0;
799}
800
801#ifdef CONFIG_ACPI
802static int bcm_acpi_probe(struct bcm_device *dev)
803{
212d7183
AS
804 LIST_HEAD(resources);
805 const struct dmi_system_id *dmi_id;
806 const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
807 const struct acpi_device_id *id;
9d54fd6a 808 struct resource_entry *entry;
212d7183
AS
809 int ret;
810
811 /* Retrieve GPIO data */
c0d3ce58 812 id = acpi_match_device(dev->dev->driver->acpi_match_table, dev->dev);
212d7183
AS
813 if (id)
814 gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data;
815
c0d3ce58 816 ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
212d7183
AS
817 if (ret)
818 return ret;
819
ae056908 820 /* Retrieve UART ACPI info */
c0d3ce58 821 ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
e98d6d62 822 &resources, bcm_resource, dev);
5be00284
JN
823 if (ret < 0)
824 return ret;
9d54fd6a
HG
825
826 resource_list_for_each_entry(entry, &resources) {
827 if (resource_type(entry->res) == IORESOURCE_IRQ) {
828 dev->irq = entry->res->start;
829 break;
830 }
831 }
09dbf1b7 832 acpi_dev_free_resource_list(&resources);
ae056908 833
5bffac2b
HG
834 if (irq_polarity != -1) {
835 dev->irq_active_low = irq_polarity;
836 dev_warn(dev->dev, "Overwriting IRQ polarity to active %s by module-param\n",
837 dev->irq_active_low ? "low" : "high");
838 } else {
839 dmi_id = dmi_first_match(bcm_active_low_irq_dmi_table);
840 if (dmi_id) {
841 dev_warn(dev->dev, "%s: Overwriting IRQ polarity to active low",
842 dmi_id->ident);
843 dev->irq_active_low = true;
844 }
5cebdfea
FD
845 }
846
0395ffc1
FD
847 return 0;
848}
50d78bcf
FD
849#else
850static int bcm_acpi_probe(struct bcm_device *dev)
851{
852 return -EINVAL;
853}
854#endif /* CONFIG_ACPI */
0395ffc1 855
8a920568
HG
856static int bcm_of_probe(struct bcm_device *bdev)
857{
858 device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
859 return 0;
860}
861
0395ffc1
FD
862static int bcm_probe(struct platform_device *pdev)
863{
864 struct bcm_device *dev;
0395ffc1
FD
865 int ret;
866
867 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
868 if (!dev)
869 return -ENOMEM;
870
c0d3ce58 871 dev->dev = &pdev->dev;
4a56f891 872 dev->irq = platform_get_irq(pdev, 0);
0395ffc1 873
201762e2 874 if (has_acpi_companion(&pdev->dev)) {
212d7183 875 ret = bcm_acpi_probe(dev);
201762e2
HG
876 if (ret)
877 return ret;
878 }
879
42ef18f0 880 ret = bcm_get_resources(dev);
4d1c4558
JN
881 if (ret)
882 return ret;
0395ffc1
FD
883
884 platform_set_drvdata(pdev, dev);
885
886 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
887
888 /* Place this instance on the device list */
bb3ea16a 889 mutex_lock(&bcm_device_lock);
0395ffc1 890 list_add_tail(&dev->list, &bcm_device_list);
bb3ea16a 891 mutex_unlock(&bcm_device_lock);
0395ffc1
FD
892
893 bcm_gpio_set_power(dev, false);
894
895 return 0;
896}
897
898static int bcm_remove(struct platform_device *pdev)
899{
900 struct bcm_device *dev = platform_get_drvdata(pdev);
901
bb3ea16a 902 mutex_lock(&bcm_device_lock);
0395ffc1 903 list_del(&dev->list);
bb3ea16a 904 mutex_unlock(&bcm_device_lock);
0395ffc1 905
0395ffc1
FD
906 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
907
908 return 0;
909}
910
bdd8818e
MH
911static const struct hci_uart_proto bcm_proto = {
912 .id = HCI_UART_BCM,
143f0a28 913 .name = "Broadcom",
aee61f7a 914 .manufacturer = 15,
61b2fc2b 915 .init_speed = 115200,
bdd8818e
MH
916 .open = bcm_open,
917 .close = bcm_close,
918 .flush = bcm_flush,
919 .setup = bcm_setup,
61b2fc2b 920 .set_baudrate = bcm_set_baudrate,
bdd8818e
MH
921 .recv = bcm_recv,
922 .enqueue = bcm_enqueue,
923 .dequeue = bcm_dequeue,
924};
925
0395ffc1
FD
926#ifdef CONFIG_ACPI
927static const struct acpi_device_id bcm_acpi_match[] = {
89ab37b4
DD
928 { "BCM2E1A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
929 { "BCM2E39", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
930 { "BCM2E3A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
931 { "BCM2E3D", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
932 { "BCM2E3F", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
933 { "BCM2E40", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
934 { "BCM2E54", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
935 { "BCM2E55", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
936 { "BCM2E64", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
937 { "BCM2E65", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
938 { "BCM2E67", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
939 { "BCM2E71", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
940 { "BCM2E7B", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
941 { "BCM2E7C", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
61d220a6 942 { "BCM2E7E", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
89ab37b4
DD
943 { "BCM2E95", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
944 { "BCM2E96", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
1bdb68b2 945 { "BCM2EA4", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
0395ffc1
FD
946 { },
947};
948MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
949#endif
950
8a920568 951/* suspend and resume callbacks */
e88ab30d
FD
952static const struct dev_pm_ops bcm_pm_ops = {
953 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
954 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
955};
118612fb 956
0395ffc1
FD
957static struct platform_driver bcm_driver = {
958 .probe = bcm_probe,
959 .remove = bcm_remove,
960 .driver = {
961 .name = "hci_bcm",
962 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
118612fb 963 .pm = &bcm_pm_ops,
0395ffc1
FD
964 },
965};
966
33cd149e
LP
967static int bcm_serdev_probe(struct serdev_device *serdev)
968{
8a920568 969 struct bcm_device *bcmdev;
33cd149e
LP
970 int err;
971
972 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
973 if (!bcmdev)
974 return -ENOMEM;
975
8a920568 976 bcmdev->dev = &serdev->dev;
81a19053 977#ifdef CONFIG_PM
8a920568 978 bcmdev->hu = &bcmdev->serdev_hu;
81a19053 979#endif
8a920568 980 bcmdev->serdev_hu.serdev = serdev;
33cd149e
LP
981 serdev_device_set_drvdata(serdev, bcmdev);
982
8a920568
HG
983 if (has_acpi_companion(&serdev->dev))
984 err = bcm_acpi_probe(bcmdev);
985 else
986 err = bcm_of_probe(bcmdev);
987 if (err)
988 return err;
33cd149e 989
8a920568
HG
990 err = bcm_get_resources(bcmdev);
991 if (err)
992 return err;
993
994 bcm_gpio_set_power(bcmdev, false);
995
996 return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
33cd149e
LP
997}
998
999static void bcm_serdev_remove(struct serdev_device *serdev)
1000{
8a920568 1001 struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
33cd149e 1002
8a920568 1003 hci_uart_unregister_device(&bcmdev->serdev_hu);
33cd149e
LP
1004}
1005
1006#ifdef CONFIG_OF
1007static const struct of_device_id bcm_bluetooth_of_match[] = {
1008 { .compatible = "brcm,bcm43438-bt" },
1009 { },
1010};
1011MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
1012#endif
1013
1014static struct serdev_device_driver bcm_serdev_driver = {
1015 .probe = bcm_serdev_probe,
1016 .remove = bcm_serdev_remove,
1017 .driver = {
1018 .name = "hci_uart_bcm",
1019 .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
8a920568
HG
1020 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1021 .pm = &bcm_pm_ops,
33cd149e
LP
1022 },
1023};
1024
bdd8818e
MH
1025int __init bcm_init(void)
1026{
33cd149e
LP
1027 /* For now, we need to keep both platform device
1028 * driver (ACPI generated) and serdev driver (DT).
1029 */
0395ffc1 1030 platform_driver_register(&bcm_driver);
33cd149e 1031 serdev_device_driver_register(&bcm_serdev_driver);
0395ffc1 1032
bdd8818e
MH
1033 return hci_uart_register_proto(&bcm_proto);
1034}
1035
1036int __exit bcm_deinit(void)
1037{
0395ffc1 1038 platform_driver_unregister(&bcm_driver);
33cd149e 1039 serdev_device_driver_unregister(&bcm_serdev_driver);
0395ffc1 1040
bdd8818e
MH
1041 return hci_uart_unregister_proto(&bcm_proto);
1042}