1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Bluetooth HCI UART driver for Broadcom devices
6 * Copyright (C) 2015 Intel Corporation
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/skbuff.h>
12 #include <linux/firmware.h>
13 #include <linux/module.h>
14 #include <linux/acpi.h>
16 #include <linux/of_irq.h>
17 #include <linux/property.h>
18 #include <linux/platform_data/x86/apple.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/clk.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/gpio/machine.h>
24 #include <linux/tty.h>
25 #include <linux/interrupt.h>
26 #include <linux/dmi.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/serdev.h>
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
36 #define BCM_NULL_PKT 0x00
37 #define BCM_NULL_SIZE 0
39 #define BCM_LM_DIAG_PKT 0x07
40 #define BCM_LM_DIAG_SIZE 63
42 #define BCM_TYPE49_PKT 0x31
43 #define BCM_TYPE49_SIZE 0
45 #define BCM_TYPE52_PKT 0x34
46 #define BCM_TYPE52_SIZE 0
48 #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
50 #define BCM_NUM_SUPPLIES 2
53 * struct bcm_device_data - device specific data
54 * @no_early_set_baudrate: Disallow set baudrate before driver setup()
55 * @drive_rts_on_open: drive RTS signal on ->open() when platform requires it
57 struct bcm_device_data {
58 bool no_early_set_baudrate;
59 bool drive_rts_on_open;
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 * @reset: BT_RST_N pin,
74 * active low resets the Bluetooth logic core
75 * @set_device_wakeup: callback to toggle BT_WAKE pin
76 * either by accessing @device_wakeup or by calling @btlp
77 * @set_shutdown: callback to toggle BT_REG_ON pin
78 * either by accessing @shutdown or by calling @btpu/@btpd
79 * @btlp: Apple ACPI method to toggle BT_WAKE pin ("Bluetooth Low Power")
80 * @btpu: Apple ACPI method to drive BT_REG_ON pin high ("Bluetooth Power Up")
81 * @btpd: Apple ACPI method to drive BT_REG_ON pin low ("Bluetooth Power Down")
82 * @gpio_count: internal counter for GPIO resources associated with ACPI device
83 * @gpio_int_idx: index in _CRS for GpioInt() resource
84 * @txco_clk: external reference frequency clock used by Bluetooth device
85 * @lpo_clk: external LPO clock used by Bluetooth device
86 * @supplies: VBAT and VDDIO supplies used by Bluetooth device
87 * @res_enabled: whether clocks and supplies are prepared and enabled
88 * @init_speed: default baudrate of Bluetooth device;
89 * the host UART is initially set to this baudrate so that
90 * it can configure the Bluetooth device for @oper_speed
91 * @oper_speed: preferred baudrate of Bluetooth device;
92 * set to 0 if @init_speed is already the preferred baudrate
93 * @irq: interrupt triggered by HOST_WAKE_BT pin
94 * @irq_active_low: whether @irq is active low
95 * @irq_acquired: flag to show if IRQ handler has been assigned
96 * @hu: pointer to HCI UART controller struct,
97 * used to disable flow control during runtime suspend and system sleep
98 * @is_suspended: whether flow control is currently disabled
99 * @no_early_set_baudrate: don't set_baudrate before setup()
100 * @drive_rts_on_open: drive RTS signal on ->open() when platform requires it
101 * @pcm_int_params: keep the initial PCM configuration
104 /* Must be the first member, hci_serdev.c expects this. */
105 struct hci_uart serdev_hu;
106 struct list_head list;
111 struct gpio_desc *device_wakeup;
112 struct gpio_desc *shutdown;
113 struct gpio_desc *reset;
114 int (*set_device_wakeup)(struct bcm_device *, bool);
115 int (*set_shutdown)(struct bcm_device *, bool);
117 acpi_handle btlp, btpu, btpd;
122 struct clk *txco_clk;
124 struct regulator_bulk_data supplies[BCM_NUM_SUPPLIES];
137 bool no_early_set_baudrate;
138 bool drive_rts_on_open;
139 u8 pcm_int_params[5];
142 /* generic bcm uart resources */
144 struct sk_buff *rx_skb;
145 struct sk_buff_head txq;
147 struct bcm_device *dev;
150 /* List of BCM BT UART devices */
151 static DEFINE_MUTEX(bcm_device_lock);
152 static LIST_HEAD(bcm_device_list);
154 static int irq_polarity = -1;
155 module_param(irq_polarity, int, 0444);
156 MODULE_PARM_DESC(irq_polarity, "IRQ polarity 0: active-high 1: active-low");
158 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
161 serdev_device_set_baudrate(hu->serdev, speed);
163 hci_uart_set_baudrate(hu, speed);
166 static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
168 struct hci_dev *hdev = hu->hdev;
170 struct bcm_update_uart_baud_rate param;
172 if (speed > 3000000) {
173 struct bcm_write_uart_clock_setting clock;
175 clock.type = BCM_UART_CLOCK_48MHZ;
177 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
179 /* This Broadcom specific command changes the UART's controller
180 * clock for baud rate > 3000000.
182 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
184 int err = PTR_ERR(skb);
185 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
193 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
195 param.zero = cpu_to_le16(0);
196 param.baud_rate = cpu_to_le32(speed);
198 /* This Broadcom specific command changes the UART's controller baud
201 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), ¶m,
204 int err = PTR_ERR(skb);
205 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
215 /* bcm_device_exists should be protected by bcm_device_lock */
216 static bool bcm_device_exists(struct bcm_device *device)
221 /* Devices using serdev always exist */
222 if (device && device->hu && device->hu->serdev)
226 list_for_each(p, &bcm_device_list) {
227 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
236 static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
240 if (powered && !dev->res_enabled) {
241 /* Intel Macs use bcm_apple_get_resources() and don't
242 * have regulator supplies configured.
244 if (dev->supplies[0].supply) {
245 err = regulator_bulk_enable(BCM_NUM_SUPPLIES,
251 /* LPO clock needs to be 32.768 kHz */
252 err = clk_set_rate(dev->lpo_clk, 32768);
254 dev_err(dev->dev, "Could not set LPO clock rate\n");
255 goto err_regulator_disable;
258 err = clk_prepare_enable(dev->lpo_clk);
260 goto err_regulator_disable;
262 err = clk_prepare_enable(dev->txco_clk);
264 goto err_lpo_clk_disable;
267 err = dev->set_shutdown(dev, powered);
269 goto err_txco_clk_disable;
271 err = dev->set_device_wakeup(dev, powered);
273 goto err_revert_shutdown;
275 if (!powered && dev->res_enabled) {
276 clk_disable_unprepare(dev->txco_clk);
277 clk_disable_unprepare(dev->lpo_clk);
279 /* Intel Macs use bcm_apple_get_resources() and don't
280 * have regulator supplies configured.
282 if (dev->supplies[0].supply)
283 regulator_bulk_disable(BCM_NUM_SUPPLIES,
287 /* wait for device to power on and come out of reset */
288 usleep_range(100000, 120000);
290 dev->res_enabled = powered;
295 dev->set_shutdown(dev, !powered);
296 err_txco_clk_disable:
297 if (powered && !dev->res_enabled)
298 clk_disable_unprepare(dev->txco_clk);
300 if (powered && !dev->res_enabled)
301 clk_disable_unprepare(dev->lpo_clk);
302 err_regulator_disable:
303 if (powered && !dev->res_enabled)
304 regulator_bulk_disable(BCM_NUM_SUPPLIES, dev->supplies);
309 static irqreturn_t bcm_host_wake(int irq, void *data)
311 struct bcm_device *bdev = data;
313 bt_dev_dbg(bdev, "Host wake IRQ");
315 pm_runtime_get(bdev->dev);
316 pm_runtime_mark_last_busy(bdev->dev);
317 pm_runtime_put_autosuspend(bdev->dev);
322 static int bcm_request_irq(struct bcm_data *bcm)
324 struct bcm_device *bdev = bcm->dev;
327 mutex_lock(&bcm_device_lock);
328 if (!bcm_device_exists(bdev)) {
333 if (bdev->irq <= 0) {
338 err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
339 bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
347 bdev->irq_acquired = true;
349 device_init_wakeup(bdev->dev, true);
351 pm_runtime_set_autosuspend_delay(bdev->dev,
352 BCM_AUTOSUSPEND_DELAY);
353 pm_runtime_use_autosuspend(bdev->dev);
354 pm_runtime_set_active(bdev->dev);
355 pm_runtime_enable(bdev->dev);
358 mutex_unlock(&bcm_device_lock);
363 static const struct bcm_set_sleep_mode default_sleep_params = {
364 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
365 .idle_host = 2, /* idle threshold HOST, in 300ms */
366 .idle_dev = 2, /* idle threshold device, in 300ms */
367 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
368 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
369 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
370 .combine_modes = 1, /* Combine sleep and LPM flag */
371 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
372 /* Irrelevant USB flags */
374 .usb_resume_timeout = 0,
376 .pulsed_host_wake = 1,
379 static int bcm_setup_sleep(struct hci_uart *hu)
381 struct bcm_data *bcm = hu->priv;
383 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
385 sleep_params.host_wake_active = !bcm->dev->irq_active_low;
387 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
388 &sleep_params, HCI_INIT_TIMEOUT);
390 int err = PTR_ERR(skb);
391 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
396 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
401 static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
402 static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
405 static int bcm_set_diag(struct hci_dev *hdev, bool enable)
407 struct hci_uart *hu = hci_get_drvdata(hdev);
408 struct bcm_data *bcm = hu->priv;
411 if (!test_bit(HCI_RUNNING, &hdev->flags))
414 skb = bt_skb_alloc(3, GFP_KERNEL);
418 skb_put_u8(skb, BCM_LM_DIAG_PKT);
419 skb_put_u8(skb, 0xf0);
420 skb_put_u8(skb, enable);
422 skb_queue_tail(&bcm->txq, skb);
423 hci_uart_tx_wakeup(hu);
428 static int bcm_open(struct hci_uart *hu)
430 struct bcm_data *bcm;
434 bt_dev_dbg(hu->hdev, "hu %p", hu);
436 if (!hci_uart_has_flow_control(hu))
439 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
443 skb_queue_head_init(&bcm->txq);
447 mutex_lock(&bcm_device_lock);
450 bcm->dev = serdev_device_get_drvdata(hu->serdev);
457 list_for_each(p, &bcm_device_list) {
458 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
460 /* Retrieve saved bcm_device based on parent of the
461 * platform device (saved during device probe) and
462 * parent of tty device used by hci_uart
464 if (hu->tty->dev->parent == dev->dev->parent) {
475 if (bcm->dev->drive_rts_on_open)
476 hci_uart_set_flow_control(hu, true);
478 hu->init_speed = bcm->dev->init_speed;
480 /* If oper_speed is set, ldisc/serdev will set the baudrate
481 * before calling setup()
483 if (!bcm->dev->no_early_set_baudrate)
484 hu->oper_speed = bcm->dev->oper_speed;
486 err = bcm_gpio_set_power(bcm->dev, true);
488 if (bcm->dev->drive_rts_on_open)
489 hci_uart_set_flow_control(hu, false);
495 mutex_unlock(&bcm_device_lock);
503 mutex_unlock(&bcm_device_lock);
509 static int bcm_close(struct hci_uart *hu)
511 struct bcm_data *bcm = hu->priv;
512 struct bcm_device *bdev = NULL;
515 bt_dev_dbg(hu->hdev, "hu %p", hu);
517 /* Protect bcm->dev against removal of the device or driver */
518 mutex_lock(&bcm_device_lock);
521 bdev = serdev_device_get_drvdata(hu->serdev);
522 } else if (bcm_device_exists(bcm->dev)) {
530 if (IS_ENABLED(CONFIG_PM) && bdev->irq_acquired) {
531 devm_free_irq(bdev->dev, bdev->irq, bdev);
532 device_init_wakeup(bdev->dev, false);
533 pm_runtime_disable(bdev->dev);
536 err = bcm_gpio_set_power(bdev, false);
538 bt_dev_err(hu->hdev, "Failed to power down");
540 pm_runtime_set_suspended(bdev->dev);
542 mutex_unlock(&bcm_device_lock);
544 skb_queue_purge(&bcm->txq);
545 kfree_skb(bcm->rx_skb);
552 static int bcm_flush(struct hci_uart *hu)
554 struct bcm_data *bcm = hu->priv;
556 bt_dev_dbg(hu->hdev, "hu %p", hu);
558 skb_queue_purge(&bcm->txq);
563 static int bcm_setup(struct hci_uart *hu)
565 struct bcm_data *bcm = hu->priv;
566 bool fw_load_done = false;
570 bt_dev_dbg(hu->hdev, "hu %p", hu);
572 hu->hdev->set_diag = bcm_set_diag;
573 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
575 err = btbcm_initialize(hu->hdev, &fw_load_done);
582 /* Init speed if any */
584 speed = hu->init_speed;
585 else if (hu->proto->init_speed)
586 speed = hu->proto->init_speed;
591 host_set_baudrate(hu, speed);
593 /* Operational speed if any */
595 speed = hu->oper_speed;
596 else if (bcm->dev && bcm->dev->oper_speed)
597 speed = bcm->dev->oper_speed;
598 else if (hu->proto->oper_speed)
599 speed = hu->proto->oper_speed;
604 err = bcm_set_baudrate(hu, speed);
606 host_set_baudrate(hu, speed);
609 /* PCM parameters if provided */
610 if (bcm->dev && bcm->dev->pcm_int_params[0] != 0xff) {
611 struct bcm_set_pcm_int_params params;
613 btbcm_read_pcm_int_params(hu->hdev, ¶ms);
615 memcpy(¶ms, bcm->dev->pcm_int_params, 5);
616 btbcm_write_pcm_int_params(hu->hdev, ¶ms);
619 err = btbcm_finalize(hu->hdev, &fw_load_done);
623 /* Some devices ship with the controller default address.
624 * Allow the bootloader to set a valid address through the
627 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hu->hdev->quirks);
629 if (!bcm_request_irq(bcm))
630 err = bcm_setup_sleep(hu);
635 #define BCM_RECV_LM_DIAG \
636 .type = BCM_LM_DIAG_PKT, \
637 .hlen = BCM_LM_DIAG_SIZE, \
640 .maxlen = BCM_LM_DIAG_SIZE
642 #define BCM_RECV_NULL \
643 .type = BCM_NULL_PKT, \
644 .hlen = BCM_NULL_SIZE, \
647 .maxlen = BCM_NULL_SIZE
649 #define BCM_RECV_TYPE49 \
650 .type = BCM_TYPE49_PKT, \
651 .hlen = BCM_TYPE49_SIZE, \
654 .maxlen = BCM_TYPE49_SIZE
656 #define BCM_RECV_TYPE52 \
657 .type = BCM_TYPE52_PKT, \
658 .hlen = BCM_TYPE52_SIZE, \
661 .maxlen = BCM_TYPE52_SIZE
663 static const struct h4_recv_pkt bcm_recv_pkts[] = {
664 { H4_RECV_ACL, .recv = hci_recv_frame },
665 { H4_RECV_SCO, .recv = hci_recv_frame },
666 { H4_RECV_EVENT, .recv = hci_recv_frame },
667 { H4_RECV_ISO, .recv = hci_recv_frame },
668 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
669 { BCM_RECV_NULL, .recv = hci_recv_diag },
670 { BCM_RECV_TYPE49, .recv = hci_recv_diag },
671 { BCM_RECV_TYPE52, .recv = hci_recv_diag },
674 static int bcm_recv(struct hci_uart *hu, const void *data, int count)
676 struct bcm_data *bcm = hu->priv;
678 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
681 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
682 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
683 if (IS_ERR(bcm->rx_skb)) {
684 int err = PTR_ERR(bcm->rx_skb);
685 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
688 } else if (!bcm->rx_skb) {
689 /* Delay auto-suspend when receiving completed packet */
690 mutex_lock(&bcm_device_lock);
691 if (bcm->dev && bcm_device_exists(bcm->dev)) {
692 pm_runtime_get(bcm->dev->dev);
693 pm_runtime_mark_last_busy(bcm->dev->dev);
694 pm_runtime_put_autosuspend(bcm->dev->dev);
696 mutex_unlock(&bcm_device_lock);
702 static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
704 struct bcm_data *bcm = hu->priv;
706 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
708 /* Prepend skb with frame type */
709 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
710 skb_queue_tail(&bcm->txq, skb);
715 static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
717 struct bcm_data *bcm = hu->priv;
718 struct sk_buff *skb = NULL;
719 struct bcm_device *bdev = NULL;
721 mutex_lock(&bcm_device_lock);
723 if (bcm_device_exists(bcm->dev)) {
725 pm_runtime_get_sync(bdev->dev);
726 /* Shall be resumed here */
729 skb = skb_dequeue(&bcm->txq);
732 pm_runtime_mark_last_busy(bdev->dev);
733 pm_runtime_put_autosuspend(bdev->dev);
736 mutex_unlock(&bcm_device_lock);
742 static int bcm_suspend_device(struct device *dev)
744 struct bcm_device *bdev = dev_get_drvdata(dev);
747 bt_dev_dbg(bdev, "");
749 if (!bdev->is_suspended && bdev->hu) {
750 hci_uart_set_flow_control(bdev->hu, true);
752 /* Once this returns, driver suspends BT via GPIO */
753 bdev->is_suspended = true;
756 /* Suspend the device */
757 err = bdev->set_device_wakeup(bdev, false);
759 if (bdev->is_suspended && bdev->hu) {
760 bdev->is_suspended = false;
761 hci_uart_set_flow_control(bdev->hu, false);
766 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
772 static int bcm_resume_device(struct device *dev)
774 struct bcm_device *bdev = dev_get_drvdata(dev);
777 bt_dev_dbg(bdev, "");
779 err = bdev->set_device_wakeup(bdev, true);
781 dev_err(dev, "Failed to power up\n");
785 bt_dev_dbg(bdev, "resume, delaying 15 ms");
788 /* When this executes, the device has woken up already */
789 if (bdev->is_suspended && bdev->hu) {
790 bdev->is_suspended = false;
792 hci_uart_set_flow_control(bdev->hu, false);
799 #ifdef CONFIG_PM_SLEEP
800 /* suspend callback */
801 static int bcm_suspend(struct device *dev)
803 struct bcm_device *bdev = dev_get_drvdata(dev);
806 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
809 * When used with a device instantiated as platform_device, bcm_suspend
810 * can be called at any time as long as the platform device is bound,
811 * so it should use bcm_device_lock to protect access to hci_uart
812 * and device_wake-up GPIO.
814 mutex_lock(&bcm_device_lock);
819 if (pm_runtime_active(dev))
820 bcm_suspend_device(dev);
822 if (device_may_wakeup(dev) && bdev->irq > 0) {
823 error = enable_irq_wake(bdev->irq);
825 bt_dev_dbg(bdev, "BCM irq: enabled");
829 mutex_unlock(&bcm_device_lock);
834 /* resume callback */
835 static int bcm_resume(struct device *dev)
837 struct bcm_device *bdev = dev_get_drvdata(dev);
840 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
843 * When used with a device instantiated as platform_device, bcm_resume
844 * can be called at any time as long as platform device is bound,
845 * so it should use bcm_device_lock to protect access to hci_uart
846 * and device_wake-up GPIO.
848 mutex_lock(&bcm_device_lock);
853 if (device_may_wakeup(dev) && bdev->irq > 0) {
854 disable_irq_wake(bdev->irq);
855 bt_dev_dbg(bdev, "BCM irq: disabled");
858 err = bcm_resume_device(dev);
861 mutex_unlock(&bcm_device_lock);
864 pm_runtime_disable(dev);
865 pm_runtime_set_active(dev);
866 pm_runtime_enable(dev);
873 /* Some firmware reports an IRQ which does not work (wrong pin in fw table?) */
874 static struct gpiod_lookup_table asus_tf103c_irq_gpios = {
875 .dev_id = "serial0-0",
877 GPIO_LOOKUP("INT33FC:02", 17, "host-wakeup-alt", GPIO_ACTIVE_HIGH),
882 static const struct dmi_system_id bcm_broken_irq_dmi_table[] = {
884 .ident = "Asus TF103C",
886 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
887 DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
889 .driver_data = &asus_tf103c_irq_gpios,
892 .ident = "Meegopad T08",
894 DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
895 "To be filled by OEM."),
896 DMI_EXACT_MATCH(DMI_BOARD_NAME, "T3 MRD"),
897 DMI_EXACT_MATCH(DMI_BOARD_VERSION, "V1.1"),
904 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
905 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
906 static const struct acpi_gpio_params third_gpio = { 2, 0, false };
908 static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
909 { "device-wakeup-gpios", &first_gpio, 1 },
910 { "shutdown-gpios", &second_gpio, 1 },
911 { "host-wakeup-gpios", &third_gpio, 1 },
915 static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
916 { "host-wakeup-gpios", &first_gpio, 1 },
917 { "device-wakeup-gpios", &second_gpio, 1 },
918 { "shutdown-gpios", &third_gpio, 1 },
922 static int bcm_resource(struct acpi_resource *ares, void *data)
924 struct bcm_device *dev = data;
925 struct acpi_resource_extended_irq *irq;
926 struct acpi_resource_gpio *gpio;
927 struct acpi_resource_uart_serialbus *sb;
929 switch (ares->type) {
930 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
931 irq = &ares->data.extended_irq;
932 if (irq->polarity != ACPI_ACTIVE_LOW)
933 dev_info(dev->dev, "ACPI Interrupt resource is active-high, this is usually wrong, treating the IRQ as active-low\n");
934 dev->irq_active_low = true;
937 case ACPI_RESOURCE_TYPE_GPIO:
938 gpio = &ares->data.gpio;
939 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
940 dev->gpio_int_idx = dev->gpio_count;
941 dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
946 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
947 sb = &ares->data.uart_serial_bus;
948 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
949 dev->init_speed = sb->default_baud_rate;
950 dev->oper_speed = 4000000;
961 static int bcm_apple_set_device_wakeup(struct bcm_device *dev, bool awake)
963 if (ACPI_FAILURE(acpi_execute_simple_method(dev->btlp, NULL, !awake)))
969 static int bcm_apple_set_shutdown(struct bcm_device *dev, bool powered)
971 if (ACPI_FAILURE(acpi_evaluate_object(powered ? dev->btpu : dev->btpd,
978 static int bcm_apple_get_resources(struct bcm_device *dev)
980 struct acpi_device *adev = ACPI_COMPANION(dev->dev);
981 const union acpi_object *obj;
984 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTLP", &dev->btlp)) ||
985 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPU", &dev->btpu)) ||
986 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPD", &dev->btpd)))
989 if (!acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, &obj) &&
990 obj->buffer.length == 8)
991 dev->init_speed = *(u64 *)obj->buffer.pointer;
993 dev->set_device_wakeup = bcm_apple_set_device_wakeup;
994 dev->set_shutdown = bcm_apple_set_shutdown;
999 static inline int bcm_apple_get_resources(struct bcm_device *dev)
1003 #endif /* CONFIG_ACPI */
1005 static int bcm_gpio_set_device_wakeup(struct bcm_device *dev, bool awake)
1007 gpiod_set_value_cansleep(dev->device_wakeup, awake);
1011 static int bcm_gpio_set_shutdown(struct bcm_device *dev, bool powered)
1013 gpiod_set_value_cansleep(dev->shutdown, powered);
1016 * The reset line is asserted on powerdown and deasserted
1017 * on poweron so the inverse of powered is used. Notice
1018 * that the GPIO line BT_RST_N needs to be specified as
1019 * active low in the device tree or similar system
1022 gpiod_set_value_cansleep(dev->reset, !powered);
1026 /* Try a bunch of names for TXCO */
1027 static struct clk *bcm_get_txco(struct device *dev)
1031 /* New explicit name */
1032 clk = devm_clk_get(dev, "txco");
1033 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
1036 /* Deprecated name */
1037 clk = devm_clk_get(dev, "extclk");
1038 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
1041 /* Original code used no name at all */
1042 return devm_clk_get(dev, NULL);
1045 static int bcm_get_resources(struct bcm_device *dev)
1047 const struct dmi_system_id *broken_irq_dmi_id;
1048 const char *irq_con_id = "host-wakeup";
1051 dev->name = dev_name(dev->dev);
1053 if (x86_apple_machine && !bcm_apple_get_resources(dev))
1056 dev->txco_clk = bcm_get_txco(dev->dev);
1058 /* Handle deferred probing */
1059 if (dev->txco_clk == ERR_PTR(-EPROBE_DEFER))
1060 return PTR_ERR(dev->txco_clk);
1062 /* Ignore all other errors as before */
1063 if (IS_ERR(dev->txco_clk))
1064 dev->txco_clk = NULL;
1066 dev->lpo_clk = devm_clk_get(dev->dev, "lpo");
1067 if (dev->lpo_clk == ERR_PTR(-EPROBE_DEFER))
1068 return PTR_ERR(dev->lpo_clk);
1070 if (IS_ERR(dev->lpo_clk))
1071 dev->lpo_clk = NULL;
1073 /* Check if we accidentally fetched the lpo clock twice */
1074 if (dev->lpo_clk && clk_is_match(dev->lpo_clk, dev->txco_clk)) {
1075 devm_clk_put(dev->dev, dev->txco_clk);
1076 dev->txco_clk = NULL;
1079 dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup",
1081 if (IS_ERR(dev->device_wakeup))
1082 return PTR_ERR(dev->device_wakeup);
1084 dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown",
1086 if (IS_ERR(dev->shutdown))
1087 return PTR_ERR(dev->shutdown);
1089 dev->reset = devm_gpiod_get_optional(dev->dev, "reset",
1091 if (IS_ERR(dev->reset))
1092 return PTR_ERR(dev->reset);
1094 dev->set_device_wakeup = bcm_gpio_set_device_wakeup;
1095 dev->set_shutdown = bcm_gpio_set_shutdown;
1097 dev->supplies[0].supply = "vbat";
1098 dev->supplies[1].supply = "vddio";
1099 err = devm_regulator_bulk_get(dev->dev, BCM_NUM_SUPPLIES,
1104 broken_irq_dmi_id = dmi_first_match(bcm_broken_irq_dmi_table);
1105 if (broken_irq_dmi_id && broken_irq_dmi_id->driver_data) {
1106 gpiod_add_lookup_table(broken_irq_dmi_id->driver_data);
1107 irq_con_id = "host-wakeup-alt";
1108 dev->irq_active_low = false;
1112 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
1113 if (dev->irq <= 0) {
1114 struct gpio_desc *gpio;
1116 gpio = devm_gpiod_get_optional(dev->dev, irq_con_id, GPIOD_IN);
1118 return PTR_ERR(gpio);
1120 dev->irq = gpiod_to_irq(gpio);
1123 if (broken_irq_dmi_id) {
1124 if (broken_irq_dmi_id->driver_data) {
1125 gpiod_remove_lookup_table(broken_irq_dmi_id->driver_data);
1127 dev_info(dev->dev, "%s: Has a broken IRQ config, disabling IRQ support / runtime-pm\n",
1128 broken_irq_dmi_id->ident);
1133 dev_dbg(dev->dev, "BCM irq: %d\n", dev->irq);
1138 static int bcm_acpi_probe(struct bcm_device *dev)
1140 LIST_HEAD(resources);
1141 const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
1142 struct resource_entry *entry;
1145 /* Retrieve UART ACPI info */
1146 dev->gpio_int_idx = -1;
1147 ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
1148 &resources, bcm_resource, dev);
1152 resource_list_for_each_entry(entry, &resources) {
1153 if (resource_type(entry->res) == IORESOURCE_IRQ) {
1154 dev->irq = entry->res->start;
1158 acpi_dev_free_resource_list(&resources);
1160 /* If the DSDT uses an Interrupt resource for the IRQ, then there are
1161 * only 2 GPIO resources, we use the irq-last mapping for this, since
1162 * we already have an irq the 3th / last mapping will not be used.
1165 gpio_mapping = acpi_bcm_int_last_gpios;
1166 else if (dev->gpio_int_idx == 0)
1167 gpio_mapping = acpi_bcm_int_first_gpios;
1168 else if (dev->gpio_int_idx == 2)
1169 gpio_mapping = acpi_bcm_int_last_gpios;
1171 dev_warn(dev->dev, "Unexpected ACPI gpio_int_idx: %d\n",
1174 /* Warn if our expectations are not met. */
1175 if (dev->gpio_count != (dev->irq ? 2 : 3))
1176 dev_warn(dev->dev, "Unexpected number of ACPI GPIOs: %d\n",
1179 ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
1183 if (irq_polarity != -1) {
1184 dev->irq_active_low = irq_polarity;
1185 dev_warn(dev->dev, "Overwriting IRQ polarity to active %s by module-param\n",
1186 dev->irq_active_low ? "low" : "high");
1192 static int bcm_acpi_probe(struct bcm_device *dev)
1196 #endif /* CONFIG_ACPI */
1198 static int bcm_of_probe(struct bcm_device *bdev)
1200 device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
1201 device_property_read_u8_array(bdev->dev, "brcm,bt-pcm-int-params",
1202 bdev->pcm_int_params, 5);
1203 bdev->irq = of_irq_get_byname(bdev->dev->of_node, "host-wakeup");
1204 bdev->irq_active_low = irq_get_trigger_type(bdev->irq)
1205 & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW);
1209 static int bcm_probe(struct platform_device *pdev)
1211 struct bcm_device *dev;
1214 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1218 dev->dev = &pdev->dev;
1220 ret = platform_get_irq(pdev, 0);
1226 /* Initialize routing field to an unused value */
1227 dev->pcm_int_params[0] = 0xff;
1229 if (has_acpi_companion(&pdev->dev)) {
1230 ret = bcm_acpi_probe(dev);
1235 ret = bcm_get_resources(dev);
1239 platform_set_drvdata(pdev, dev);
1241 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
1243 /* Place this instance on the device list */
1244 mutex_lock(&bcm_device_lock);
1245 list_add_tail(&dev->list, &bcm_device_list);
1246 mutex_unlock(&bcm_device_lock);
1248 ret = bcm_gpio_set_power(dev, false);
1250 dev_err(&pdev->dev, "Failed to power down\n");
1255 static int bcm_remove(struct platform_device *pdev)
1257 struct bcm_device *dev = platform_get_drvdata(pdev);
1259 mutex_lock(&bcm_device_lock);
1260 list_del(&dev->list);
1261 mutex_unlock(&bcm_device_lock);
1263 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
1268 static const struct hci_uart_proto bcm_proto = {
1272 .init_speed = 115200,
1277 .set_baudrate = bcm_set_baudrate,
1279 .enqueue = bcm_enqueue,
1280 .dequeue = bcm_dequeue,
1284 static const struct acpi_device_id bcm_acpi_match[] = {
1453 MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
1456 /* suspend and resume callbacks */
1457 static const struct dev_pm_ops bcm_pm_ops = {
1458 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
1459 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
1462 static struct platform_driver bcm_driver = {
1464 .remove = bcm_remove,
1467 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1472 static int bcm_serdev_probe(struct serdev_device *serdev)
1474 struct bcm_device *bcmdev;
1475 const struct bcm_device_data *data;
1478 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
1482 bcmdev->dev = &serdev->dev;
1484 bcmdev->hu = &bcmdev->serdev_hu;
1486 bcmdev->serdev_hu.serdev = serdev;
1487 serdev_device_set_drvdata(serdev, bcmdev);
1489 /* Initialize routing field to an unused value */
1490 bcmdev->pcm_int_params[0] = 0xff;
1492 if (has_acpi_companion(&serdev->dev))
1493 err = bcm_acpi_probe(bcmdev);
1495 err = bcm_of_probe(bcmdev);
1499 err = bcm_get_resources(bcmdev);
1503 if (!bcmdev->shutdown) {
1504 dev_warn(&serdev->dev,
1505 "No reset resource, using default baud rate\n");
1506 bcmdev->oper_speed = bcmdev->init_speed;
1509 err = bcm_gpio_set_power(bcmdev, false);
1511 dev_err(&serdev->dev, "Failed to power down\n");
1513 data = device_get_match_data(bcmdev->dev);
1515 bcmdev->no_early_set_baudrate = data->no_early_set_baudrate;
1516 bcmdev->drive_rts_on_open = data->drive_rts_on_open;
1519 return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
1522 static void bcm_serdev_remove(struct serdev_device *serdev)
1524 struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
1526 hci_uart_unregister_device(&bcmdev->serdev_hu);
1530 static struct bcm_device_data bcm4354_device_data = {
1531 .no_early_set_baudrate = true,
1534 static struct bcm_device_data bcm43438_device_data = {
1535 .drive_rts_on_open = true,
1538 static const struct of_device_id bcm_bluetooth_of_match[] = {
1539 { .compatible = "brcm,bcm20702a1" },
1540 { .compatible = "brcm,bcm4329-bt" },
1541 { .compatible = "brcm,bcm4330-bt" },
1542 { .compatible = "brcm,bcm4334-bt" },
1543 { .compatible = "brcm,bcm4345c5" },
1544 { .compatible = "brcm,bcm43430a0-bt" },
1545 { .compatible = "brcm,bcm43430a1-bt" },
1546 { .compatible = "brcm,bcm43438-bt", .data = &bcm43438_device_data },
1547 { .compatible = "brcm,bcm43540-bt", .data = &bcm4354_device_data },
1548 { .compatible = "brcm,bcm4335a0" },
1551 MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
1554 static struct serdev_device_driver bcm_serdev_driver = {
1555 .probe = bcm_serdev_probe,
1556 .remove = bcm_serdev_remove,
1558 .name = "hci_uart_bcm",
1559 .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
1560 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1565 int __init bcm_init(void)
1567 /* For now, we need to keep both platform device
1568 * driver (ACPI generated) and serdev driver (DT).
1570 platform_driver_register(&bcm_driver);
1571 serdev_device_driver_register(&bcm_serdev_driver);
1573 return hci_uart_register_proto(&bcm_proto);
1576 int __exit bcm_deinit(void)
1578 platform_driver_unregister(&bcm_driver);
1579 serdev_device_driver_unregister(&bcm_serdev_driver);
1581 return hci_uart_unregister_proto(&bcm_proto);