]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - 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
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>
27 #include <linux/firmware.h>
28 #include <linux/module.h>
29 #include <linux/acpi.h>
30 #include <linux/of.h>
31 #include <linux/property.h>
32 #include <linux/platform_device.h>
33 #include <linux/clk.h>
34 #include <linux/gpio/consumer.h>
35 #include <linux/tty.h>
36 #include <linux/interrupt.h>
37 #include <linux/dmi.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/serdev.h>
40
41 #include <net/bluetooth/bluetooth.h>
42 #include <net/bluetooth/hci_core.h>
43
44 #include "btbcm.h"
45 #include "hci_uart.h"
46
47 #define BCM_NULL_PKT 0x00
48 #define BCM_NULL_SIZE 0
49
50 #define BCM_LM_DIAG_PKT 0x07
51 #define BCM_LM_DIAG_SIZE 63
52
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
59 #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
60
61 /* device driver resources */
62 struct bcm_device {
63 /* Must be the first member, hci_serdev.c expects this. */
64 struct hci_uart serdev_hu;
65 struct list_head list;
66
67 struct device *dev;
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;
75
76 u32 init_speed;
77 u32 oper_speed;
78 int irq;
79 bool irq_active_low;
80
81 #ifdef CONFIG_PM
82 struct hci_uart *hu;
83 bool is_suspended; /* suspend/resume flag */
84 #endif
85 };
86
87 /* generic bcm uart resources */
88 struct bcm_data {
89 struct sk_buff *rx_skb;
90 struct sk_buff_head txq;
91
92 struct bcm_device *dev;
93 };
94
95 /* List of BCM BT UART devices */
96 static DEFINE_MUTEX(bcm_device_lock);
97 static LIST_HEAD(bcm_device_list);
98
99 static int irq_polarity = -1;
100 module_param(irq_polarity, int, 0444);
101 MODULE_PARM_DESC(irq_polarity, "IRQ polarity 0: active-high 1: active-low");
102
103 static 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
111 static 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
122 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
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);
130 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
131 err);
132 return err;
133 }
134
135 kfree_skb(skb);
136 }
137
138 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
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);
150 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
151 err);
152 return err;
153 }
154
155 kfree_skb(skb);
156
157 return 0;
158 }
159
160 /* bcm_device_exists should be protected by bcm_device_lock */
161 static bool bcm_device_exists(struct bcm_device *device)
162 {
163 struct list_head *p;
164
165 #ifdef CONFIG_PM
166 /* Devices using serdev always exist */
167 if (device && device->hu && device->hu->serdev)
168 return true;
169 #endif
170
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
181 static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
182 {
183 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
184 clk_prepare_enable(dev->clk);
185
186 gpiod_set_value(dev->shutdown, powered);
187 gpiod_set_value(dev->device_wakeup, powered);
188
189 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
190 clk_disable_unprepare(dev->clk);
191
192 dev->clk_enabled = powered;
193
194 return 0;
195 }
196
197 #ifdef CONFIG_PM
198 static 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
204 pm_runtime_get(bdev->dev);
205 pm_runtime_mark_last_busy(bdev->dev);
206 pm_runtime_put_autosuspend(bdev->dev);
207
208 return IRQ_HANDLED;
209 }
210
211 static int bcm_request_irq(struct bcm_data *bcm)
212 {
213 struct bcm_device *bdev = bcm->dev;
214 int err;
215
216 mutex_lock(&bcm_device_lock);
217 if (!bcm_device_exists(bdev)) {
218 err = -ENODEV;
219 goto unlock;
220 }
221
222 if (bdev->irq <= 0) {
223 err = -EOPNOTSUPP;
224 goto unlock;
225 }
226
227 err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
228 bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
229 IRQF_TRIGGER_RISING,
230 "host_wake", bdev);
231 if (err)
232 goto unlock;
233
234 device_init_wakeup(bdev->dev, true);
235
236 pm_runtime_set_autosuspend_delay(bdev->dev,
237 BCM_AUTOSUSPEND_DELAY);
238 pm_runtime_use_autosuspend(bdev->dev);
239 pm_runtime_set_active(bdev->dev);
240 pm_runtime_enable(bdev->dev);
241
242 unlock:
243 mutex_unlock(&bcm_device_lock);
244
245 return err;
246 }
247
248 static 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 */
255 .combine_modes = 1, /* Combine sleep and LPM flag */
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
264 static 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
270 sleep_params.host_wake_active = !bcm->dev->irq_active_low;
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
286 static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
287 static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
288 #endif
289
290 static 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);
300 if (!skb)
301 return -ENOMEM;
302
303 skb_put_u8(skb, BCM_LM_DIAG_PKT);
304 skb_put_u8(skb, 0xf0);
305 skb_put_u8(skb, enable);
306
307 skb_queue_tail(&bcm->txq, skb);
308 hci_uart_tx_wakeup(hu);
309
310 return 0;
311 }
312
313 static int bcm_open(struct hci_uart *hu)
314 {
315 struct bcm_data *bcm;
316 struct list_head *p;
317
318 bt_dev_dbg(hu->hdev, "hu %p", hu);
319
320 if (!hci_uart_has_flow_control(hu))
321 return -EOPNOTSUPP;
322
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;
330
331 mutex_lock(&bcm_device_lock);
332
333 if (hu->serdev) {
334 serdev_device_open(hu->serdev);
335 bcm->dev = serdev_device_get_drvdata(hu->serdev);
336 goto out;
337 }
338
339 if (!hu->tty->dev)
340 goto out;
341
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 */
349 if (hu->tty->dev->parent == dev->dev->parent) {
350 bcm->dev = dev;
351 #ifdef CONFIG_PM
352 dev->hu = hu;
353 #endif
354 break;
355 }
356 }
357
358 out:
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);
366 return 0;
367 }
368
369 static int bcm_close(struct hci_uart *hu)
370 {
371 struct bcm_data *bcm = hu->priv;
372 struct bcm_device *bdev = NULL;
373
374 bt_dev_dbg(hu->hdev, "hu %p", hu);
375
376 /* Protect bcm->dev against removal of the device or driver */
377 mutex_lock(&bcm_device_lock);
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) {
390 bcm_gpio_set_power(bdev, false);
391 #ifdef CONFIG_PM
392 pm_runtime_disable(bdev->dev);
393 pm_runtime_set_suspended(bdev->dev);
394
395 if (bdev->irq > 0) {
396 devm_free_irq(bdev->dev, bdev->irq, bdev);
397 device_init_wakeup(bdev->dev, false);
398 }
399 #endif
400 }
401 mutex_unlock(&bcm_device_lock);
402
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
411 static int bcm_flush(struct hci_uart *hu)
412 {
413 struct bcm_data *bcm = hu->priv;
414
415 bt_dev_dbg(hu->hdev, "hu %p", hu);
416
417 skb_queue_purge(&bcm->txq);
418
419 return 0;
420 }
421
422 static int bcm_setup(struct hci_uart *hu)
423 {
424 struct bcm_data *bcm = hu->priv;
425 char fw_name[64];
426 const struct firmware *fw;
427 unsigned int speed;
428 int err;
429
430 bt_dev_dbg(hu->hdev, "hu %p", hu);
431
432 hu->hdev->set_diag = bcm_set_diag;
433 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
434
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) {
441 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
442 return 0;
443 }
444
445 err = btbcm_patchram(hu->hdev, fw);
446 if (err) {
447 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
448 goto finalize;
449 }
450
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)
460 host_set_baudrate(hu, speed);
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);
472 if (!err)
473 host_set_baudrate(hu, speed);
474 }
475
476 finalize:
477 release_firmware(fw);
478
479 err = btbcm_finalize(hu->hdev);
480 if (err)
481 return err;
482
483 if (!bcm_request_irq(bcm))
484 err = bcm_setup_sleep(hu);
485
486 return err;
487 }
488
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
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
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
517 static const struct h4_recv_pkt bcm_recv_pkts[] = {
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 },
522 { BCM_RECV_NULL, .recv = hci_recv_diag },
523 { BCM_RECV_TYPE49, .recv = hci_recv_diag },
524 { BCM_RECV_TYPE52, .recv = hci_recv_diag },
525 };
526
527 static 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
534 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
535 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
536 if (IS_ERR(bcm->rx_skb)) {
537 int err = PTR_ERR(bcm->rx_skb);
538 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
539 bcm->rx_skb = NULL;
540 return err;
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)) {
545 pm_runtime_get(bcm->dev->dev);
546 pm_runtime_mark_last_busy(bcm->dev->dev);
547 pm_runtime_put_autosuspend(bcm->dev->dev);
548 }
549 mutex_unlock(&bcm_device_lock);
550 }
551
552 return count;
553 }
554
555 static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
556 {
557 struct bcm_data *bcm = hu->priv;
558
559 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
560
561 /* Prepend skb with frame type */
562 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
563 skb_queue_tail(&bcm->txq, skb);
564
565 return 0;
566 }
567
568 static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
569 {
570 struct bcm_data *bcm = hu->priv;
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;
578 pm_runtime_get_sync(bdev->dev);
579 /* Shall be resumed here */
580 }
581
582 skb = skb_dequeue(&bcm->txq);
583
584 if (bdev) {
585 pm_runtime_mark_last_busy(bdev->dev);
586 pm_runtime_put_autosuspend(bdev->dev);
587 }
588
589 mutex_unlock(&bcm_device_lock);
590
591 return skb;
592 }
593
594 #ifdef CONFIG_PM
595 static int bcm_suspend_device(struct device *dev)
596 {
597 struct bcm_device *bdev = dev_get_drvdata(dev);
598
599 bt_dev_dbg(bdev, "");
600
601 if (!bdev->is_suspended && bdev->hu) {
602 hci_uart_set_flow_control(bdev->hu, true);
603
604 /* Once this returns, driver suspends BT via GPIO */
605 bdev->is_suspended = true;
606 }
607
608 /* Suspend the device */
609 gpiod_set_value(bdev->device_wakeup, false);
610 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
611 mdelay(15);
612
613 return 0;
614 }
615
616 static int bcm_resume_device(struct device *dev)
617 {
618 struct bcm_device *bdev = dev_get_drvdata(dev);
619
620 bt_dev_dbg(bdev, "");
621
622 gpiod_set_value(bdev->device_wakeup, true);
623 bt_dev_dbg(bdev, "resume, delaying 15 ms");
624 mdelay(15);
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
638 /* suspend callback */
639 static int bcm_suspend(struct device *dev)
640 {
641 struct bcm_device *bdev = dev_get_drvdata(dev);
642 int error;
643
644 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
645
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
650 * and device_wake-up GPIO.
651 */
652 mutex_lock(&bcm_device_lock);
653
654 if (!bdev->hu)
655 goto unlock;
656
657 if (pm_runtime_active(dev))
658 bcm_suspend_device(dev);
659
660 if (device_may_wakeup(dev) && bdev->irq > 0) {
661 error = enable_irq_wake(bdev->irq);
662 if (!error)
663 bt_dev_dbg(bdev, "BCM irq: enabled");
664 }
665
666 unlock:
667 mutex_unlock(&bcm_device_lock);
668
669 return 0;
670 }
671
672 /* resume callback */
673 static int bcm_resume(struct device *dev)
674 {
675 struct bcm_device *bdev = dev_get_drvdata(dev);
676
677 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
678
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
683 * and device_wake-up GPIO.
684 */
685 mutex_lock(&bcm_device_lock);
686
687 if (!bdev->hu)
688 goto unlock;
689
690 if (device_may_wakeup(dev) && bdev->irq > 0) {
691 disable_irq_wake(bdev->irq);
692 bt_dev_dbg(bdev, "BCM irq: disabled");
693 }
694
695 bcm_resume_device(dev);
696
697 unlock:
698 mutex_unlock(&bcm_device_lock);
699
700 pm_runtime_disable(dev);
701 pm_runtime_set_active(dev);
702 pm_runtime_enable(dev);
703
704 return 0;
705 }
706 #endif
707
708 static const struct acpi_gpio_params int_last_device_wakeup_gpios = { 0, 0, false };
709 static const struct acpi_gpio_params int_last_shutdown_gpios = { 1, 0, false };
710 static const struct acpi_gpio_params int_last_host_wakeup_gpios = { 2, 0, false };
711
712 static 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
719 static const struct acpi_gpio_params int_first_host_wakeup_gpios = { 0, 0, false };
720 static const struct acpi_gpio_params int_first_device_wakeup_gpios = { 1, 0, false };
721 static const struct acpi_gpio_params int_first_shutdown_gpios = { 2, 0, false };
722
723 static 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 },
727 { },
728 };
729
730 #ifdef CONFIG_ACPI
731 /* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
732 static const struct dmi_system_id bcm_active_low_irq_dmi_table[] = {
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 },
739 },
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 },
747 { }
748 };
749
750 static int bcm_resource(struct acpi_resource *ares, void *data)
751 {
752 struct bcm_device *dev = data;
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;
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;
763 break;
764
765 case ACPI_RESOURCE_TYPE_GPIO:
766 gpio = &ares->data.gpio;
767 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
768 dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
769 break;
770
771 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
772 sb = &ares->data.uart_serial_bus;
773 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
774 dev->init_speed = sb->default_baud_rate;
775 dev->oper_speed = 4000000;
776 }
777 break;
778
779 default:
780 break;
781 }
782
783 return 0;
784 }
785 #endif /* CONFIG_ACPI */
786
787 static int bcm_get_resources(struct bcm_device *dev)
788 {
789 dev->name = dev_name(dev->dev);
790
791 dev->clk = devm_clk_get(dev->dev, NULL);
792
793 /* Handle deferred probing */
794 if (dev->clk == ERR_PTR(-EPROBE_DEFER))
795 return PTR_ERR(dev->clk);
796
797 dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup",
798 GPIOD_OUT_LOW);
799 if (IS_ERR(dev->device_wakeup))
800 return PTR_ERR(dev->device_wakeup);
801
802 dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown",
803 GPIOD_OUT_LOW);
804 if (IS_ERR(dev->shutdown))
805 return PTR_ERR(dev->shutdown);
806
807 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
808 if (dev->irq <= 0) {
809 struct gpio_desc *gpio;
810
811 gpio = devm_gpiod_get_optional(dev->dev, "host-wakeup",
812 GPIOD_IN);
813 if (IS_ERR(gpio))
814 return PTR_ERR(gpio);
815
816 dev->irq = gpiod_to_irq(gpio);
817 }
818
819 dev_info(dev->dev, "BCM irq: %d\n", dev->irq);
820 return 0;
821 }
822
823 #ifdef CONFIG_ACPI
824 static int bcm_acpi_probe(struct bcm_device *dev)
825 {
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;
830 struct resource_entry *entry;
831 int ret;
832
833 /* Retrieve GPIO data */
834 id = acpi_match_device(dev->dev->driver->acpi_match_table, dev->dev);
835 if (id)
836 gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data;
837
838 ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
839 if (ret)
840 return ret;
841
842 /* Retrieve UART ACPI info */
843 ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
844 &resources, bcm_resource, dev);
845 if (ret < 0)
846 return ret;
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 }
854 acpi_dev_free_resource_list(&resources);
855
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 }
867 }
868
869 return 0;
870 }
871 #else
872 static int bcm_acpi_probe(struct bcm_device *dev)
873 {
874 return -EINVAL;
875 }
876 #endif /* CONFIG_ACPI */
877
878 static 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
884 static int bcm_probe(struct platform_device *pdev)
885 {
886 struct bcm_device *dev;
887 int ret;
888
889 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
890 if (!dev)
891 return -ENOMEM;
892
893 dev->dev = &pdev->dev;
894 dev->irq = platform_get_irq(pdev, 0);
895
896 if (has_acpi_companion(&pdev->dev)) {
897 ret = bcm_acpi_probe(dev);
898 if (ret)
899 return ret;
900 }
901
902 ret = bcm_get_resources(dev);
903 if (ret)
904 return ret;
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 */
911 mutex_lock(&bcm_device_lock);
912 list_add_tail(&dev->list, &bcm_device_list);
913 mutex_unlock(&bcm_device_lock);
914
915 bcm_gpio_set_power(dev, false);
916
917 return 0;
918 }
919
920 static int bcm_remove(struct platform_device *pdev)
921 {
922 struct bcm_device *dev = platform_get_drvdata(pdev);
923
924 mutex_lock(&bcm_device_lock);
925 list_del(&dev->list);
926 mutex_unlock(&bcm_device_lock);
927
928 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
929
930 return 0;
931 }
932
933 static const struct hci_uart_proto bcm_proto = {
934 .id = HCI_UART_BCM,
935 .name = "Broadcom",
936 .manufacturer = 15,
937 .init_speed = 115200,
938 .open = bcm_open,
939 .close = bcm_close,
940 .flush = bcm_flush,
941 .setup = bcm_setup,
942 .set_baudrate = bcm_set_baudrate,
943 .recv = bcm_recv,
944 .enqueue = bcm_enqueue,
945 .dequeue = bcm_dequeue,
946 };
947
948 #ifdef CONFIG_ACPI
949 static const struct acpi_device_id bcm_acpi_match[] = {
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 },
964 { "BCM2E7E", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
965 { "BCM2E95", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
966 { "BCM2E96", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
967 { "BCM2EA4", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
968 { },
969 };
970 MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
971 #endif
972
973 /* suspend and resume callbacks */
974 static 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 };
978
979 static 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),
985 .pm = &bcm_pm_ops,
986 },
987 };
988
989 static int bcm_serdev_probe(struct serdev_device *serdev)
990 {
991 struct bcm_device *bcmdev;
992 int err;
993
994 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
995 if (!bcmdev)
996 return -ENOMEM;
997
998 bcmdev->dev = &serdev->dev;
999 #ifdef CONFIG_PM
1000 bcmdev->hu = &bcmdev->serdev_hu;
1001 #endif
1002 bcmdev->serdev_hu.serdev = serdev;
1003 serdev_device_set_drvdata(serdev, bcmdev);
1004
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;
1011
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);
1019 }
1020
1021 static void bcm_serdev_remove(struct serdev_device *serdev)
1022 {
1023 struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
1024
1025 hci_uart_unregister_device(&bcmdev->serdev_hu);
1026 }
1027
1028 #ifdef CONFIG_OF
1029 static const struct of_device_id bcm_bluetooth_of_match[] = {
1030 { .compatible = "brcm,bcm43438-bt" },
1031 { },
1032 };
1033 MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
1034 #endif
1035
1036 static 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),
1042 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1043 .pm = &bcm_pm_ops,
1044 },
1045 };
1046
1047 int __init bcm_init(void)
1048 {
1049 /* For now, we need to keep both platform device
1050 * driver (ACPI generated) and serdev driver (DT).
1051 */
1052 platform_driver_register(&bcm_driver);
1053 serdev_device_driver_register(&bcm_serdev_driver);
1054
1055 return hci_uart_register_proto(&bcm_proto);
1056 }
1057
1058 int __exit bcm_deinit(void)
1059 {
1060 platform_driver_unregister(&bcm_driver);
1061 serdev_device_driver_unregister(&bcm_serdev_driver);
1062
1063 return hci_uart_unregister_proto(&bcm_proto);
1064 }