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