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