Linux 4.19.133
[linux/fpc-iii.git] / drivers / bluetooth / hci_bcm.c
blob3e386f68faa0232f000b104c4492059a26434634
1 /*
3 * Bluetooth HCI UART driver for Broadcom devices
5 * Copyright (C) 2015 Intel Corporation
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.
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.
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
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_data/x86/apple.h>
33 #include <linux/platform_device.h>
34 #include <linux/clk.h>
35 #include <linux/gpio/consumer.h>
36 #include <linux/tty.h>
37 #include <linux/interrupt.h>
38 #include <linux/dmi.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/serdev.h>
42 #include <net/bluetooth/bluetooth.h>
43 #include <net/bluetooth/hci_core.h>
45 #include "btbcm.h"
46 #include "hci_uart.h"
48 #define BCM_NULL_PKT 0x00
49 #define BCM_NULL_SIZE 0
51 #define BCM_LM_DIAG_PKT 0x07
52 #define BCM_LM_DIAG_SIZE 63
54 #define BCM_TYPE49_PKT 0x31
55 #define BCM_TYPE49_SIZE 0
57 #define BCM_TYPE52_PKT 0x34
58 #define BCM_TYPE52_SIZE 0
60 #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
62 /**
63 * struct bcm_device - device driver resources
64 * @serdev_hu: HCI UART controller struct
65 * @list: bcm_device_list node
66 * @dev: physical UART slave
67 * @name: device name logged by bt_dev_*() functions
68 * @device_wakeup: BT_WAKE pin,
69 * assert = Bluetooth device must wake up or remain awake,
70 * deassert = Bluetooth device may sleep when sleep criteria are met
71 * @shutdown: BT_REG_ON pin,
72 * power up or power down Bluetooth device internal regulators
73 * @set_device_wakeup: callback to toggle BT_WAKE pin
74 * either by accessing @device_wakeup or by calling @btlp
75 * @set_shutdown: callback to toggle BT_REG_ON pin
76 * either by accessing @shutdown or by calling @btpu/@btpd
77 * @btlp: Apple ACPI method to toggle BT_WAKE pin ("Bluetooth Low Power")
78 * @btpu: Apple ACPI method to drive BT_REG_ON pin high ("Bluetooth Power Up")
79 * @btpd: Apple ACPI method to drive BT_REG_ON pin low ("Bluetooth Power Down")
80 * @clk: clock used by Bluetooth device
81 * @clk_enabled: whether @clk is prepared and enabled
82 * @init_speed: default baudrate of Bluetooth device;
83 * the host UART is initially set to this baudrate so that
84 * it can configure the Bluetooth device for @oper_speed
85 * @oper_speed: preferred baudrate of Bluetooth device;
86 * set to 0 if @init_speed is already the preferred baudrate
87 * @irq: interrupt triggered by HOST_WAKE_BT pin
88 * @irq_active_low: whether @irq is active low
89 * @hu: pointer to HCI UART controller struct,
90 * used to disable flow control during runtime suspend and system sleep
91 * @is_suspended: whether flow control is currently disabled
93 struct bcm_device {
94 /* Must be the first member, hci_serdev.c expects this. */
95 struct hci_uart serdev_hu;
96 struct list_head list;
98 struct device *dev;
100 const char *name;
101 struct gpio_desc *device_wakeup;
102 struct gpio_desc *shutdown;
103 int (*set_device_wakeup)(struct bcm_device *, bool);
104 int (*set_shutdown)(struct bcm_device *, bool);
105 #ifdef CONFIG_ACPI
106 acpi_handle btlp, btpu, btpd;
107 int gpio_count;
108 int gpio_int_idx;
109 #endif
111 struct clk *clk;
112 bool clk_enabled;
114 u32 init_speed;
115 u32 oper_speed;
116 int irq;
117 bool irq_active_low;
118 bool irq_acquired;
120 #ifdef CONFIG_PM
121 struct hci_uart *hu;
122 bool is_suspended;
123 #endif
126 /* generic bcm uart resources */
127 struct bcm_data {
128 struct sk_buff *rx_skb;
129 struct sk_buff_head txq;
131 struct bcm_device *dev;
134 /* List of BCM BT UART devices */
135 static DEFINE_MUTEX(bcm_device_lock);
136 static LIST_HEAD(bcm_device_list);
138 static int irq_polarity = -1;
139 module_param(irq_polarity, int, 0444);
140 MODULE_PARM_DESC(irq_polarity, "IRQ polarity 0: active-high 1: active-low");
142 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
144 if (hu->serdev)
145 serdev_device_set_baudrate(hu->serdev, speed);
146 else
147 hci_uart_set_baudrate(hu, speed);
150 static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
152 struct hci_dev *hdev = hu->hdev;
153 struct sk_buff *skb;
154 struct bcm_update_uart_baud_rate param;
156 if (speed > 3000000) {
157 struct bcm_write_uart_clock_setting clock;
159 clock.type = BCM_UART_CLOCK_48MHZ;
161 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
163 /* This Broadcom specific command changes the UART's controller
164 * clock for baud rate > 3000000.
166 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
167 if (IS_ERR(skb)) {
168 int err = PTR_ERR(skb);
169 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
170 err);
171 return err;
174 kfree_skb(skb);
177 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
179 param.zero = cpu_to_le16(0);
180 param.baud_rate = cpu_to_le32(speed);
182 /* This Broadcom specific command changes the UART's controller baud
183 * rate.
185 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
186 HCI_INIT_TIMEOUT);
187 if (IS_ERR(skb)) {
188 int err = PTR_ERR(skb);
189 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
190 err);
191 return err;
194 kfree_skb(skb);
196 return 0;
199 /* bcm_device_exists should be protected by bcm_device_lock */
200 static bool bcm_device_exists(struct bcm_device *device)
202 struct list_head *p;
204 #ifdef CONFIG_PM
205 /* Devices using serdev always exist */
206 if (device && device->hu && device->hu->serdev)
207 return true;
208 #endif
210 list_for_each(p, &bcm_device_list) {
211 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
213 if (device == dev)
214 return true;
217 return false;
220 static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
222 int err;
224 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled) {
225 err = clk_prepare_enable(dev->clk);
226 if (err)
227 return err;
230 err = dev->set_shutdown(dev, powered);
231 if (err)
232 goto err_clk_disable;
234 err = dev->set_device_wakeup(dev, powered);
235 if (err)
236 goto err_revert_shutdown;
238 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
239 clk_disable_unprepare(dev->clk);
241 dev->clk_enabled = powered;
243 return 0;
245 err_revert_shutdown:
246 dev->set_shutdown(dev, !powered);
247 err_clk_disable:
248 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
249 clk_disable_unprepare(dev->clk);
250 return err;
253 #ifdef CONFIG_PM
254 static irqreturn_t bcm_host_wake(int irq, void *data)
256 struct bcm_device *bdev = data;
258 bt_dev_dbg(bdev, "Host wake IRQ");
260 pm_runtime_get(bdev->dev);
261 pm_runtime_mark_last_busy(bdev->dev);
262 pm_runtime_put_autosuspend(bdev->dev);
264 return IRQ_HANDLED;
267 static int bcm_request_irq(struct bcm_data *bcm)
269 struct bcm_device *bdev = bcm->dev;
270 int err;
272 mutex_lock(&bcm_device_lock);
273 if (!bcm_device_exists(bdev)) {
274 err = -ENODEV;
275 goto unlock;
278 if (bdev->irq <= 0) {
279 err = -EOPNOTSUPP;
280 goto unlock;
283 err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
284 bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
285 IRQF_TRIGGER_RISING,
286 "host_wake", bdev);
287 if (err) {
288 bdev->irq = err;
289 goto unlock;
292 bdev->irq_acquired = true;
294 device_init_wakeup(bdev->dev, true);
296 pm_runtime_set_autosuspend_delay(bdev->dev,
297 BCM_AUTOSUSPEND_DELAY);
298 pm_runtime_use_autosuspend(bdev->dev);
299 pm_runtime_set_active(bdev->dev);
300 pm_runtime_enable(bdev->dev);
302 unlock:
303 mutex_unlock(&bcm_device_lock);
305 return err;
308 static const struct bcm_set_sleep_mode default_sleep_params = {
309 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
310 .idle_host = 2, /* idle threshold HOST, in 300ms */
311 .idle_dev = 2, /* idle threshold device, in 300ms */
312 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
313 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
314 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
315 .combine_modes = 1, /* Combine sleep and LPM flag */
316 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
317 /* Irrelevant USB flags */
318 .usb_auto_sleep = 0,
319 .usb_resume_timeout = 0,
320 .break_to_host = 0,
321 .pulsed_host_wake = 1,
324 static int bcm_setup_sleep(struct hci_uart *hu)
326 struct bcm_data *bcm = hu->priv;
327 struct sk_buff *skb;
328 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
330 sleep_params.host_wake_active = !bcm->dev->irq_active_low;
332 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
333 &sleep_params, HCI_INIT_TIMEOUT);
334 if (IS_ERR(skb)) {
335 int err = PTR_ERR(skb);
336 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
337 return err;
339 kfree_skb(skb);
341 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
343 return 0;
345 #else
346 static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
347 static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
348 #endif
350 static int bcm_set_diag(struct hci_dev *hdev, bool enable)
352 struct hci_uart *hu = hci_get_drvdata(hdev);
353 struct bcm_data *bcm = hu->priv;
354 struct sk_buff *skb;
356 if (!test_bit(HCI_RUNNING, &hdev->flags))
357 return -ENETDOWN;
359 skb = bt_skb_alloc(3, GFP_KERNEL);
360 if (!skb)
361 return -ENOMEM;
363 skb_put_u8(skb, BCM_LM_DIAG_PKT);
364 skb_put_u8(skb, 0xf0);
365 skb_put_u8(skb, enable);
367 skb_queue_tail(&bcm->txq, skb);
368 hci_uart_tx_wakeup(hu);
370 return 0;
373 static int bcm_open(struct hci_uart *hu)
375 struct bcm_data *bcm;
376 struct list_head *p;
377 int err;
379 bt_dev_dbg(hu->hdev, "hu %p", hu);
381 if (!hci_uart_has_flow_control(hu))
382 return -EOPNOTSUPP;
384 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
385 if (!bcm)
386 return -ENOMEM;
388 skb_queue_head_init(&bcm->txq);
390 hu->priv = bcm;
392 mutex_lock(&bcm_device_lock);
394 if (hu->serdev) {
395 bcm->dev = serdev_device_get_drvdata(hu->serdev);
396 goto out;
399 if (!hu->tty->dev)
400 goto out;
402 list_for_each(p, &bcm_device_list) {
403 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
405 /* Retrieve saved bcm_device based on parent of the
406 * platform device (saved during device probe) and
407 * parent of tty device used by hci_uart
409 if (hu->tty->dev->parent == dev->dev->parent) {
410 bcm->dev = dev;
411 #ifdef CONFIG_PM
412 dev->hu = hu;
413 #endif
414 break;
418 out:
419 if (bcm->dev) {
420 hu->init_speed = bcm->dev->init_speed;
421 hu->oper_speed = bcm->dev->oper_speed;
422 err = bcm_gpio_set_power(bcm->dev, true);
423 if (err)
424 goto err_unset_hu;
427 mutex_unlock(&bcm_device_lock);
428 return 0;
430 err_unset_hu:
431 #ifdef CONFIG_PM
432 if (!hu->serdev)
433 bcm->dev->hu = NULL;
434 #endif
435 mutex_unlock(&bcm_device_lock);
436 hu->priv = NULL;
437 kfree(bcm);
438 return err;
441 static int bcm_close(struct hci_uart *hu)
443 struct bcm_data *bcm = hu->priv;
444 struct bcm_device *bdev = NULL;
445 int err;
447 bt_dev_dbg(hu->hdev, "hu %p", hu);
449 /* Protect bcm->dev against removal of the device or driver */
450 mutex_lock(&bcm_device_lock);
452 if (hu->serdev) {
453 bdev = serdev_device_get_drvdata(hu->serdev);
454 } else if (bcm_device_exists(bcm->dev)) {
455 bdev = bcm->dev;
456 #ifdef CONFIG_PM
457 bdev->hu = NULL;
458 #endif
461 if (bdev) {
462 if (IS_ENABLED(CONFIG_PM) && bdev->irq_acquired) {
463 devm_free_irq(bdev->dev, bdev->irq, bdev);
464 device_init_wakeup(bdev->dev, false);
465 pm_runtime_disable(bdev->dev);
468 err = bcm_gpio_set_power(bdev, false);
469 if (err)
470 bt_dev_err(hu->hdev, "Failed to power down");
471 else
472 pm_runtime_set_suspended(bdev->dev);
474 mutex_unlock(&bcm_device_lock);
476 skb_queue_purge(&bcm->txq);
477 kfree_skb(bcm->rx_skb);
478 kfree(bcm);
480 hu->priv = NULL;
481 return 0;
484 static int bcm_flush(struct hci_uart *hu)
486 struct bcm_data *bcm = hu->priv;
488 bt_dev_dbg(hu->hdev, "hu %p", hu);
490 skb_queue_purge(&bcm->txq);
492 return 0;
495 static int bcm_setup(struct hci_uart *hu)
497 struct bcm_data *bcm = hu->priv;
498 char fw_name[64];
499 const struct firmware *fw;
500 unsigned int speed;
501 int err;
503 bt_dev_dbg(hu->hdev, "hu %p", hu);
505 hu->hdev->set_diag = bcm_set_diag;
506 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
508 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name), false);
509 if (err)
510 return err;
512 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
513 if (err < 0) {
514 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
515 return 0;
518 err = btbcm_patchram(hu->hdev, fw);
519 if (err) {
520 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
521 goto finalize;
524 /* Init speed if any */
525 if (hu->init_speed)
526 speed = hu->init_speed;
527 else if (hu->proto->init_speed)
528 speed = hu->proto->init_speed;
529 else
530 speed = 0;
532 if (speed)
533 host_set_baudrate(hu, speed);
535 /* Operational speed if any */
536 if (hu->oper_speed)
537 speed = hu->oper_speed;
538 else if (hu->proto->oper_speed)
539 speed = hu->proto->oper_speed;
540 else
541 speed = 0;
543 if (speed) {
544 err = bcm_set_baudrate(hu, speed);
545 if (!err)
546 host_set_baudrate(hu, speed);
549 finalize:
550 release_firmware(fw);
552 err = btbcm_finalize(hu->hdev);
553 if (err)
554 return err;
556 if (!bcm_request_irq(bcm))
557 err = bcm_setup_sleep(hu);
559 return err;
562 #define BCM_RECV_LM_DIAG \
563 .type = BCM_LM_DIAG_PKT, \
564 .hlen = BCM_LM_DIAG_SIZE, \
565 .loff = 0, \
566 .lsize = 0, \
567 .maxlen = BCM_LM_DIAG_SIZE
569 #define BCM_RECV_NULL \
570 .type = BCM_NULL_PKT, \
571 .hlen = BCM_NULL_SIZE, \
572 .loff = 0, \
573 .lsize = 0, \
574 .maxlen = BCM_NULL_SIZE
576 #define BCM_RECV_TYPE49 \
577 .type = BCM_TYPE49_PKT, \
578 .hlen = BCM_TYPE49_SIZE, \
579 .loff = 0, \
580 .lsize = 0, \
581 .maxlen = BCM_TYPE49_SIZE
583 #define BCM_RECV_TYPE52 \
584 .type = BCM_TYPE52_PKT, \
585 .hlen = BCM_TYPE52_SIZE, \
586 .loff = 0, \
587 .lsize = 0, \
588 .maxlen = BCM_TYPE52_SIZE
590 static const struct h4_recv_pkt bcm_recv_pkts[] = {
591 { H4_RECV_ACL, .recv = hci_recv_frame },
592 { H4_RECV_SCO, .recv = hci_recv_frame },
593 { H4_RECV_EVENT, .recv = hci_recv_frame },
594 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
595 { BCM_RECV_NULL, .recv = hci_recv_diag },
596 { BCM_RECV_TYPE49, .recv = hci_recv_diag },
597 { BCM_RECV_TYPE52, .recv = hci_recv_diag },
600 static int bcm_recv(struct hci_uart *hu, const void *data, int count)
602 struct bcm_data *bcm = hu->priv;
604 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
605 return -EUNATCH;
607 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
608 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
609 if (IS_ERR(bcm->rx_skb)) {
610 int err = PTR_ERR(bcm->rx_skb);
611 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
612 bcm->rx_skb = NULL;
613 return err;
614 } else if (!bcm->rx_skb) {
615 /* Delay auto-suspend when receiving completed packet */
616 mutex_lock(&bcm_device_lock);
617 if (bcm->dev && bcm_device_exists(bcm->dev)) {
618 pm_runtime_get(bcm->dev->dev);
619 pm_runtime_mark_last_busy(bcm->dev->dev);
620 pm_runtime_put_autosuspend(bcm->dev->dev);
622 mutex_unlock(&bcm_device_lock);
625 return count;
628 static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
630 struct bcm_data *bcm = hu->priv;
632 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
634 /* Prepend skb with frame type */
635 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
636 skb_queue_tail(&bcm->txq, skb);
638 return 0;
641 static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
643 struct bcm_data *bcm = hu->priv;
644 struct sk_buff *skb = NULL;
645 struct bcm_device *bdev = NULL;
647 mutex_lock(&bcm_device_lock);
649 if (bcm_device_exists(bcm->dev)) {
650 bdev = bcm->dev;
651 pm_runtime_get_sync(bdev->dev);
652 /* Shall be resumed here */
655 skb = skb_dequeue(&bcm->txq);
657 if (bdev) {
658 pm_runtime_mark_last_busy(bdev->dev);
659 pm_runtime_put_autosuspend(bdev->dev);
662 mutex_unlock(&bcm_device_lock);
664 return skb;
667 #ifdef CONFIG_PM
668 static int bcm_suspend_device(struct device *dev)
670 struct bcm_device *bdev = dev_get_drvdata(dev);
671 int err;
673 bt_dev_dbg(bdev, "");
675 if (!bdev->is_suspended && bdev->hu) {
676 hci_uart_set_flow_control(bdev->hu, true);
678 /* Once this returns, driver suspends BT via GPIO */
679 bdev->is_suspended = true;
682 /* Suspend the device */
683 err = bdev->set_device_wakeup(bdev, false);
684 if (err) {
685 if (bdev->is_suspended && bdev->hu) {
686 bdev->is_suspended = false;
687 hci_uart_set_flow_control(bdev->hu, false);
689 return -EBUSY;
692 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
693 msleep(15);
695 return 0;
698 static int bcm_resume_device(struct device *dev)
700 struct bcm_device *bdev = dev_get_drvdata(dev);
701 int err;
703 bt_dev_dbg(bdev, "");
705 err = bdev->set_device_wakeup(bdev, true);
706 if (err) {
707 dev_err(dev, "Failed to power up\n");
708 return err;
711 bt_dev_dbg(bdev, "resume, delaying 15 ms");
712 msleep(15);
714 /* When this executes, the device has woken up already */
715 if (bdev->is_suspended && bdev->hu) {
716 bdev->is_suspended = false;
718 hci_uart_set_flow_control(bdev->hu, false);
721 return 0;
723 #endif
725 #ifdef CONFIG_PM_SLEEP
726 /* suspend callback */
727 static int bcm_suspend(struct device *dev)
729 struct bcm_device *bdev = dev_get_drvdata(dev);
730 int error;
732 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
735 * When used with a device instantiated as platform_device, bcm_suspend
736 * can be called at any time as long as the platform device is bound,
737 * so it should use bcm_device_lock to protect access to hci_uart
738 * and device_wake-up GPIO.
740 mutex_lock(&bcm_device_lock);
742 if (!bdev->hu)
743 goto unlock;
745 if (pm_runtime_active(dev))
746 bcm_suspend_device(dev);
748 if (device_may_wakeup(dev) && bdev->irq > 0) {
749 error = enable_irq_wake(bdev->irq);
750 if (!error)
751 bt_dev_dbg(bdev, "BCM irq: enabled");
754 unlock:
755 mutex_unlock(&bcm_device_lock);
757 return 0;
760 /* resume callback */
761 static int bcm_resume(struct device *dev)
763 struct bcm_device *bdev = dev_get_drvdata(dev);
764 int err = 0;
766 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
769 * When used with a device instantiated as platform_device, bcm_resume
770 * can be called at any time as long as platform device is bound,
771 * so it should use bcm_device_lock to protect access to hci_uart
772 * and device_wake-up GPIO.
774 mutex_lock(&bcm_device_lock);
776 if (!bdev->hu)
777 goto unlock;
779 if (device_may_wakeup(dev) && bdev->irq > 0) {
780 disable_irq_wake(bdev->irq);
781 bt_dev_dbg(bdev, "BCM irq: disabled");
784 err = bcm_resume_device(dev);
786 unlock:
787 mutex_unlock(&bcm_device_lock);
789 if (!err) {
790 pm_runtime_disable(dev);
791 pm_runtime_set_active(dev);
792 pm_runtime_enable(dev);
795 return 0;
797 #endif
799 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
800 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
801 static const struct acpi_gpio_params third_gpio = { 2, 0, false };
803 static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
804 { "device-wakeup-gpios", &first_gpio, 1 },
805 { "shutdown-gpios", &second_gpio, 1 },
806 { "host-wakeup-gpios", &third_gpio, 1 },
807 { },
810 static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
811 { "host-wakeup-gpios", &first_gpio, 1 },
812 { "device-wakeup-gpios", &second_gpio, 1 },
813 { "shutdown-gpios", &third_gpio, 1 },
814 { },
817 /* Some firmware reports an IRQ which does not work (wrong pin in fw table?) */
818 static const struct dmi_system_id bcm_broken_irq_dmi_table[] = {
820 .ident = "Meegopad T08",
821 .matches = {
822 DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
823 "To be filled by OEM."),
824 DMI_EXACT_MATCH(DMI_BOARD_NAME, "T3 MRD"),
825 DMI_EXACT_MATCH(DMI_BOARD_VERSION, "V1.1"),
831 #ifdef CONFIG_ACPI
832 static int bcm_resource(struct acpi_resource *ares, void *data)
834 struct bcm_device *dev = data;
835 struct acpi_resource_extended_irq *irq;
836 struct acpi_resource_gpio *gpio;
837 struct acpi_resource_uart_serialbus *sb;
839 switch (ares->type) {
840 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
841 irq = &ares->data.extended_irq;
842 if (irq->polarity != ACPI_ACTIVE_LOW)
843 dev_info(dev->dev, "ACPI Interrupt resource is active-high, this is usually wrong, treating the IRQ as active-low\n");
844 dev->irq_active_low = true;
845 break;
847 case ACPI_RESOURCE_TYPE_GPIO:
848 gpio = &ares->data.gpio;
849 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
850 dev->gpio_int_idx = dev->gpio_count;
851 dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
853 dev->gpio_count++;
854 break;
856 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
857 sb = &ares->data.uart_serial_bus;
858 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
859 dev->init_speed = sb->default_baud_rate;
860 dev->oper_speed = 4000000;
862 break;
864 default:
865 break;
868 return 0;
871 static int bcm_apple_set_device_wakeup(struct bcm_device *dev, bool awake)
873 if (ACPI_FAILURE(acpi_execute_simple_method(dev->btlp, NULL, !awake)))
874 return -EIO;
876 return 0;
879 static int bcm_apple_set_shutdown(struct bcm_device *dev, bool powered)
881 if (ACPI_FAILURE(acpi_evaluate_object(powered ? dev->btpu : dev->btpd,
882 NULL, NULL, NULL)))
883 return -EIO;
885 return 0;
888 static int bcm_apple_get_resources(struct bcm_device *dev)
890 struct acpi_device *adev = ACPI_COMPANION(dev->dev);
891 const union acpi_object *obj;
893 if (!adev ||
894 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTLP", &dev->btlp)) ||
895 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPU", &dev->btpu)) ||
896 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPD", &dev->btpd)))
897 return -ENODEV;
899 if (!acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, &obj) &&
900 obj->buffer.length == 8)
901 dev->init_speed = *(u64 *)obj->buffer.pointer;
903 dev->set_device_wakeup = bcm_apple_set_device_wakeup;
904 dev->set_shutdown = bcm_apple_set_shutdown;
906 return 0;
908 #else
909 static inline int bcm_apple_get_resources(struct bcm_device *dev)
911 return -EOPNOTSUPP;
913 #endif /* CONFIG_ACPI */
915 static int bcm_gpio_set_device_wakeup(struct bcm_device *dev, bool awake)
917 gpiod_set_value_cansleep(dev->device_wakeup, awake);
918 return 0;
921 static int bcm_gpio_set_shutdown(struct bcm_device *dev, bool powered)
923 gpiod_set_value_cansleep(dev->shutdown, powered);
924 return 0;
927 static int bcm_get_resources(struct bcm_device *dev)
929 const struct dmi_system_id *dmi_id;
931 dev->name = dev_name(dev->dev);
933 if (x86_apple_machine && !bcm_apple_get_resources(dev))
934 return 0;
936 dev->clk = devm_clk_get(dev->dev, NULL);
938 /* Handle deferred probing */
939 if (dev->clk == ERR_PTR(-EPROBE_DEFER))
940 return PTR_ERR(dev->clk);
942 dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup",
943 GPIOD_OUT_LOW);
944 if (IS_ERR(dev->device_wakeup))
945 return PTR_ERR(dev->device_wakeup);
947 dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown",
948 GPIOD_OUT_LOW);
949 if (IS_ERR(dev->shutdown))
950 return PTR_ERR(dev->shutdown);
952 dev->set_device_wakeup = bcm_gpio_set_device_wakeup;
953 dev->set_shutdown = bcm_gpio_set_shutdown;
955 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
956 if (dev->irq <= 0) {
957 struct gpio_desc *gpio;
959 gpio = devm_gpiod_get_optional(dev->dev, "host-wakeup",
960 GPIOD_IN);
961 if (IS_ERR(gpio))
962 return PTR_ERR(gpio);
964 dev->irq = gpiod_to_irq(gpio);
967 dmi_id = dmi_first_match(bcm_broken_irq_dmi_table);
968 if (dmi_id) {
969 dev_info(dev->dev, "%s: Has a broken IRQ config, disabling IRQ support / runtime-pm\n",
970 dmi_id->ident);
971 dev->irq = 0;
974 dev_dbg(dev->dev, "BCM irq: %d\n", dev->irq);
975 return 0;
978 #ifdef CONFIG_ACPI
979 static int bcm_acpi_probe(struct bcm_device *dev)
981 LIST_HEAD(resources);
982 const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
983 struct resource_entry *entry;
984 int ret;
986 /* Retrieve UART ACPI info */
987 dev->gpio_int_idx = -1;
988 ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
989 &resources, bcm_resource, dev);
990 if (ret < 0)
991 return ret;
993 resource_list_for_each_entry(entry, &resources) {
994 if (resource_type(entry->res) == IORESOURCE_IRQ) {
995 dev->irq = entry->res->start;
996 break;
999 acpi_dev_free_resource_list(&resources);
1001 /* If the DSDT uses an Interrupt resource for the IRQ, then there are
1002 * only 2 GPIO resources, we use the irq-last mapping for this, since
1003 * we already have an irq the 3th / last mapping will not be used.
1005 if (dev->irq)
1006 gpio_mapping = acpi_bcm_int_last_gpios;
1007 else if (dev->gpio_int_idx == 0)
1008 gpio_mapping = acpi_bcm_int_first_gpios;
1009 else if (dev->gpio_int_idx == 2)
1010 gpio_mapping = acpi_bcm_int_last_gpios;
1011 else
1012 dev_warn(dev->dev, "Unexpected ACPI gpio_int_idx: %d\n",
1013 dev->gpio_int_idx);
1015 /* Warn if our expectations are not met. */
1016 if (dev->gpio_count != (dev->irq ? 2 : 3))
1017 dev_warn(dev->dev, "Unexpected number of ACPI GPIOs: %d\n",
1018 dev->gpio_count);
1020 ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
1021 if (ret)
1022 return ret;
1024 if (irq_polarity != -1) {
1025 dev->irq_active_low = irq_polarity;
1026 dev_warn(dev->dev, "Overwriting IRQ polarity to active %s by module-param\n",
1027 dev->irq_active_low ? "low" : "high");
1030 return 0;
1032 #else
1033 static int bcm_acpi_probe(struct bcm_device *dev)
1035 return -EINVAL;
1037 #endif /* CONFIG_ACPI */
1039 static int bcm_of_probe(struct bcm_device *bdev)
1041 device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
1042 return 0;
1045 static int bcm_probe(struct platform_device *pdev)
1047 struct bcm_device *dev;
1048 int ret;
1050 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1051 if (!dev)
1052 return -ENOMEM;
1054 dev->dev = &pdev->dev;
1055 dev->irq = platform_get_irq(pdev, 0);
1057 if (has_acpi_companion(&pdev->dev)) {
1058 ret = bcm_acpi_probe(dev);
1059 if (ret)
1060 return ret;
1063 ret = bcm_get_resources(dev);
1064 if (ret)
1065 return ret;
1067 platform_set_drvdata(pdev, dev);
1069 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
1071 /* Place this instance on the device list */
1072 mutex_lock(&bcm_device_lock);
1073 list_add_tail(&dev->list, &bcm_device_list);
1074 mutex_unlock(&bcm_device_lock);
1076 ret = bcm_gpio_set_power(dev, false);
1077 if (ret)
1078 dev_err(&pdev->dev, "Failed to power down\n");
1080 return 0;
1083 static int bcm_remove(struct platform_device *pdev)
1085 struct bcm_device *dev = platform_get_drvdata(pdev);
1087 mutex_lock(&bcm_device_lock);
1088 list_del(&dev->list);
1089 mutex_unlock(&bcm_device_lock);
1091 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
1093 return 0;
1096 static const struct hci_uart_proto bcm_proto = {
1097 .id = HCI_UART_BCM,
1098 .name = "Broadcom",
1099 .manufacturer = 15,
1100 .init_speed = 115200,
1101 .open = bcm_open,
1102 .close = bcm_close,
1103 .flush = bcm_flush,
1104 .setup = bcm_setup,
1105 .set_baudrate = bcm_set_baudrate,
1106 .recv = bcm_recv,
1107 .enqueue = bcm_enqueue,
1108 .dequeue = bcm_dequeue,
1111 #ifdef CONFIG_ACPI
1112 static const struct acpi_device_id bcm_acpi_match[] = {
1113 { "BCM2E00" },
1114 { "BCM2E01" },
1115 { "BCM2E02" },
1116 { "BCM2E03" },
1117 { "BCM2E04" },
1118 { "BCM2E05" },
1119 { "BCM2E06" },
1120 { "BCM2E07" },
1121 { "BCM2E08" },
1122 { "BCM2E09" },
1123 { "BCM2E0A" },
1124 { "BCM2E0B" },
1125 { "BCM2E0C" },
1126 { "BCM2E0D" },
1127 { "BCM2E0E" },
1128 { "BCM2E0F" },
1129 { "BCM2E10" },
1130 { "BCM2E11" },
1131 { "BCM2E12" },
1132 { "BCM2E13" },
1133 { "BCM2E14" },
1134 { "BCM2E15" },
1135 { "BCM2E16" },
1136 { "BCM2E17" },
1137 { "BCM2E18" },
1138 { "BCM2E19" },
1139 { "BCM2E1A" },
1140 { "BCM2E1B" },
1141 { "BCM2E1C" },
1142 { "BCM2E1D" },
1143 { "BCM2E1F" },
1144 { "BCM2E20" },
1145 { "BCM2E21" },
1146 { "BCM2E22" },
1147 { "BCM2E23" },
1148 { "BCM2E24" },
1149 { "BCM2E25" },
1150 { "BCM2E26" },
1151 { "BCM2E27" },
1152 { "BCM2E28" },
1153 { "BCM2E29" },
1154 { "BCM2E2A" },
1155 { "BCM2E2B" },
1156 { "BCM2E2C" },
1157 { "BCM2E2D" },
1158 { "BCM2E2E" },
1159 { "BCM2E2F" },
1160 { "BCM2E30" },
1161 { "BCM2E31" },
1162 { "BCM2E32" },
1163 { "BCM2E33" },
1164 { "BCM2E34" },
1165 { "BCM2E35" },
1166 { "BCM2E36" },
1167 { "BCM2E37" },
1168 { "BCM2E38" },
1169 { "BCM2E39" },
1170 { "BCM2E3A" },
1171 { "BCM2E3B" },
1172 { "BCM2E3C" },
1173 { "BCM2E3D" },
1174 { "BCM2E3E" },
1175 { "BCM2E3F" },
1176 { "BCM2E40" },
1177 { "BCM2E41" },
1178 { "BCM2E42" },
1179 { "BCM2E43" },
1180 { "BCM2E44" },
1181 { "BCM2E45" },
1182 { "BCM2E46" },
1183 { "BCM2E47" },
1184 { "BCM2E48" },
1185 { "BCM2E49" },
1186 { "BCM2E4A" },
1187 { "BCM2E4B" },
1188 { "BCM2E4C" },
1189 { "BCM2E4D" },
1190 { "BCM2E4E" },
1191 { "BCM2E4F" },
1192 { "BCM2E50" },
1193 { "BCM2E51" },
1194 { "BCM2E52" },
1195 { "BCM2E53" },
1196 { "BCM2E54" },
1197 { "BCM2E55" },
1198 { "BCM2E56" },
1199 { "BCM2E57" },
1200 { "BCM2E58" },
1201 { "BCM2E59" },
1202 { "BCM2E5A" },
1203 { "BCM2E5B" },
1204 { "BCM2E5C" },
1205 { "BCM2E5D" },
1206 { "BCM2E5E" },
1207 { "BCM2E5F" },
1208 { "BCM2E60" },
1209 { "BCM2E61" },
1210 { "BCM2E62" },
1211 { "BCM2E63" },
1212 { "BCM2E64" },
1213 { "BCM2E65" },
1214 { "BCM2E66" },
1215 { "BCM2E67" },
1216 { "BCM2E68" },
1217 { "BCM2E69" },
1218 { "BCM2E6B" },
1219 { "BCM2E6D" },
1220 { "BCM2E6E" },
1221 { "BCM2E6F" },
1222 { "BCM2E70" },
1223 { "BCM2E71" },
1224 { "BCM2E72" },
1225 { "BCM2E73" },
1226 { "BCM2E74" },
1227 { "BCM2E75" },
1228 { "BCM2E76" },
1229 { "BCM2E77" },
1230 { "BCM2E78" },
1231 { "BCM2E79" },
1232 { "BCM2E7A" },
1233 { "BCM2E7B" },
1234 { "BCM2E7C" },
1235 { "BCM2E7D" },
1236 { "BCM2E7E" },
1237 { "BCM2E7F" },
1238 { "BCM2E80" },
1239 { "BCM2E81" },
1240 { "BCM2E82" },
1241 { "BCM2E83" },
1242 { "BCM2E84" },
1243 { "BCM2E85" },
1244 { "BCM2E86" },
1245 { "BCM2E87" },
1246 { "BCM2E88" },
1247 { "BCM2E89" },
1248 { "BCM2E8A" },
1249 { "BCM2E8B" },
1250 { "BCM2E8C" },
1251 { "BCM2E8D" },
1252 { "BCM2E8E" },
1253 { "BCM2E90" },
1254 { "BCM2E92" },
1255 { "BCM2E93" },
1256 { "BCM2E94" },
1257 { "BCM2E95" },
1258 { "BCM2E96" },
1259 { "BCM2E97" },
1260 { "BCM2E98" },
1261 { "BCM2E99" },
1262 { "BCM2E9A" },
1263 { "BCM2E9B" },
1264 { "BCM2E9C" },
1265 { "BCM2E9D" },
1266 { "BCM2EA0" },
1267 { "BCM2EA1" },
1268 { "BCM2EA2" },
1269 { "BCM2EA3" },
1270 { "BCM2EA4" },
1271 { "BCM2EA5" },
1272 { "BCM2EA6" },
1273 { "BCM2EA7" },
1274 { "BCM2EA8" },
1275 { "BCM2EA9" },
1276 { "BCM2EAA" },
1277 { "BCM2EAB" },
1278 { "BCM2EAC" },
1279 { },
1281 MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
1282 #endif
1284 /* suspend and resume callbacks */
1285 static const struct dev_pm_ops bcm_pm_ops = {
1286 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
1287 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
1290 static struct platform_driver bcm_driver = {
1291 .probe = bcm_probe,
1292 .remove = bcm_remove,
1293 .driver = {
1294 .name = "hci_bcm",
1295 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1296 .pm = &bcm_pm_ops,
1300 static int bcm_serdev_probe(struct serdev_device *serdev)
1302 struct bcm_device *bcmdev;
1303 int err;
1305 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
1306 if (!bcmdev)
1307 return -ENOMEM;
1309 bcmdev->dev = &serdev->dev;
1310 #ifdef CONFIG_PM
1311 bcmdev->hu = &bcmdev->serdev_hu;
1312 #endif
1313 bcmdev->serdev_hu.serdev = serdev;
1314 serdev_device_set_drvdata(serdev, bcmdev);
1316 if (has_acpi_companion(&serdev->dev))
1317 err = bcm_acpi_probe(bcmdev);
1318 else
1319 err = bcm_of_probe(bcmdev);
1320 if (err)
1321 return err;
1323 err = bcm_get_resources(bcmdev);
1324 if (err)
1325 return err;
1327 if (!bcmdev->shutdown) {
1328 dev_warn(&serdev->dev,
1329 "No reset resource, using default baud rate\n");
1330 bcmdev->oper_speed = bcmdev->init_speed;
1333 err = bcm_gpio_set_power(bcmdev, false);
1334 if (err)
1335 dev_err(&serdev->dev, "Failed to power down\n");
1337 return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
1340 static void bcm_serdev_remove(struct serdev_device *serdev)
1342 struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
1344 hci_uart_unregister_device(&bcmdev->serdev_hu);
1347 #ifdef CONFIG_OF
1348 static const struct of_device_id bcm_bluetooth_of_match[] = {
1349 { .compatible = "brcm,bcm43438-bt" },
1350 { },
1352 MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
1353 #endif
1355 static struct serdev_device_driver bcm_serdev_driver = {
1356 .probe = bcm_serdev_probe,
1357 .remove = bcm_serdev_remove,
1358 .driver = {
1359 .name = "hci_uart_bcm",
1360 .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
1361 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1362 .pm = &bcm_pm_ops,
1366 int __init bcm_init(void)
1368 /* For now, we need to keep both platform device
1369 * driver (ACPI generated) and serdev driver (DT).
1371 platform_driver_register(&bcm_driver);
1372 serdev_device_driver_register(&bcm_serdev_driver);
1374 return hci_uart_register_proto(&bcm_proto);
1377 int __exit bcm_deinit(void)
1379 platform_driver_unregister(&bcm_driver);
1380 serdev_device_driver_unregister(&bcm_serdev_driver);
1382 return hci_uart_unregister_proto(&bcm_proto);