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