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
56 * @max_autobaud_speed: max baudrate supported by device in autobaud mode
58 struct bcm_device_data {
59 bool no_early_set_baudrate;
60 bool drive_rts_on_open;
61 u32 max_autobaud_speed;
65 * struct bcm_device - device driver resources
66 * @serdev_hu: HCI UART controller struct
67 * @list: bcm_device_list node
68 * @dev: physical UART slave
69 * @name: device name logged by bt_dev_*() functions
70 * @device_wakeup: BT_WAKE pin,
71 * assert = Bluetooth device must wake up or remain awake,
72 * deassert = Bluetooth device may sleep when sleep criteria are met
73 * @shutdown: BT_REG_ON pin,
74 * power up or power down Bluetooth device internal regulators
75 * @reset: BT_RST_N pin,
76 * active low resets the Bluetooth logic core
77 * @set_device_wakeup: callback to toggle BT_WAKE pin
78 * either by accessing @device_wakeup or by calling @btlp
79 * @set_shutdown: callback to toggle BT_REG_ON pin
80 * either by accessing @shutdown or by calling @btpu/@btpd
81 * @btlp: Apple ACPI method to toggle BT_WAKE pin ("Bluetooth Low Power")
82 * @btpu: Apple ACPI method to drive BT_REG_ON pin high ("Bluetooth Power Up")
83 * @btpd: Apple ACPI method to drive BT_REG_ON pin low ("Bluetooth Power Down")
84 * @gpio_count: internal counter for GPIO resources associated with ACPI device
85 * @gpio_int_idx: index in _CRS for GpioInt() resource
86 * @txco_clk: external reference frequency clock used by Bluetooth device
87 * @lpo_clk: external LPO clock used by Bluetooth device
88 * @supplies: VBAT and VDDIO supplies used by Bluetooth device
89 * @res_enabled: whether clocks and supplies are prepared and enabled
90 * @init_speed: default baudrate of Bluetooth device;
91 * the host UART is initially set to this baudrate so that
92 * it can configure the Bluetooth device for @oper_speed
93 * @oper_speed: preferred baudrate of Bluetooth device;
94 * set to 0 if @init_speed is already the preferred baudrate
95 * @irq: interrupt triggered by HOST_WAKE_BT pin
96 * @irq_active_low: whether @irq is active low
97 * @irq_acquired: flag to show if IRQ handler has been assigned
98 * @hu: pointer to HCI UART controller struct,
99 * used to disable flow control during runtime suspend and system sleep
100 * @is_suspended: whether flow control is currently disabled
101 * @no_early_set_baudrate: don't set_baudrate before setup()
102 * @drive_rts_on_open: drive RTS signal on ->open() when platform requires it
103 * @pcm_int_params: keep the initial PCM configuration
104 * @use_autobaud_mode: start Bluetooth device in autobaud mode
105 * @max_autobaud_speed: max baudrate supported by device in autobaud mode
108 /* Must be the first member, hci_serdev.c expects this. */
109 struct hci_uart serdev_hu;
110 struct list_head list;
115 struct gpio_desc *device_wakeup;
116 struct gpio_desc *shutdown;
117 struct gpio_desc *reset;
118 int (*set_device_wakeup)(struct bcm_device *, bool);
119 int (*set_shutdown)(struct bcm_device *, bool);
121 acpi_handle btlp, btpu, btpd;
126 struct clk *txco_clk;
128 struct regulator_bulk_data supplies[BCM_NUM_SUPPLIES];
141 bool no_early_set_baudrate;
142 bool drive_rts_on_open;
143 bool use_autobaud_mode;
144 u8 pcm_int_params[5];
145 u32 max_autobaud_speed;
148 /* generic bcm uart resources */
150 struct sk_buff *rx_skb;
151 struct sk_buff_head txq;
153 struct bcm_device *dev;
156 /* List of BCM BT UART devices */
157 static DEFINE_MUTEX(bcm_device_lock);
158 static LIST_HEAD(bcm_device_list);
160 static int irq_polarity = -1;
161 module_param(irq_polarity, int, 0444);
162 MODULE_PARM_DESC(irq_polarity, "IRQ polarity 0: active-high 1: active-low");
164 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
167 serdev_device_set_baudrate(hu->serdev, speed);
169 hci_uart_set_baudrate(hu, speed);
172 static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
174 struct hci_dev *hdev = hu->hdev;
176 struct bcm_update_uart_baud_rate param;
178 if (speed > 3000000) {
179 struct bcm_write_uart_clock_setting clock;
181 clock.type = BCM_UART_CLOCK_48MHZ;
183 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
185 /* This Broadcom specific command changes the UART's controller
186 * clock for baud rate > 3000000.
188 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
190 int err = PTR_ERR(skb);
191 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
199 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
201 param.zero = cpu_to_le16(0);
202 param.baud_rate = cpu_to_le32(speed);
204 /* This Broadcom specific command changes the UART's controller baud
207 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), ¶m,
210 int err = PTR_ERR(skb);
211 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
221 /* bcm_device_exists should be protected by bcm_device_lock */
222 static bool bcm_device_exists(struct bcm_device *device)
227 /* Devices using serdev always exist */
228 if (device && device->hu && device->hu->serdev)
232 list_for_each(p, &bcm_device_list) {
233 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
242 static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
246 if (powered && !dev->res_enabled) {
247 /* Intel Macs use bcm_apple_get_resources() and don't
248 * have regulator supplies configured.
250 if (dev->supplies[0].supply) {
251 err = regulator_bulk_enable(BCM_NUM_SUPPLIES,
257 /* LPO clock needs to be 32.768 kHz */
258 err = clk_set_rate(dev->lpo_clk, 32768);
260 dev_err(dev->dev, "Could not set LPO clock rate\n");
261 goto err_regulator_disable;
264 err = clk_prepare_enable(dev->lpo_clk);
266 goto err_regulator_disable;
268 err = clk_prepare_enable(dev->txco_clk);
270 goto err_lpo_clk_disable;
273 err = dev->set_shutdown(dev, powered);
275 goto err_txco_clk_disable;
277 err = dev->set_device_wakeup(dev, powered);
279 goto err_revert_shutdown;
281 if (!powered && dev->res_enabled) {
282 clk_disable_unprepare(dev->txco_clk);
283 clk_disable_unprepare(dev->lpo_clk);
285 /* Intel Macs use bcm_apple_get_resources() and don't
286 * have regulator supplies configured.
288 if (dev->supplies[0].supply)
289 regulator_bulk_disable(BCM_NUM_SUPPLIES,
293 /* wait for device to power on and come out of reset */
294 usleep_range(100000, 120000);
296 dev->res_enabled = powered;
301 dev->set_shutdown(dev, !powered);
302 err_txco_clk_disable:
303 if (powered && !dev->res_enabled)
304 clk_disable_unprepare(dev->txco_clk);
306 if (powered && !dev->res_enabled)
307 clk_disable_unprepare(dev->lpo_clk);
308 err_regulator_disable:
309 if (powered && !dev->res_enabled)
310 regulator_bulk_disable(BCM_NUM_SUPPLIES, dev->supplies);
315 static irqreturn_t bcm_host_wake(int irq, void *data)
317 struct bcm_device *bdev = data;
319 bt_dev_dbg(bdev, "Host wake IRQ");
321 pm_runtime_get(bdev->dev);
322 pm_runtime_mark_last_busy(bdev->dev);
323 pm_runtime_put_autosuspend(bdev->dev);
328 static int bcm_request_irq(struct bcm_data *bcm)
330 struct bcm_device *bdev = bcm->dev;
333 mutex_lock(&bcm_device_lock);
334 if (!bcm_device_exists(bdev)) {
339 if (bdev->irq <= 0) {
344 err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
345 bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
353 bdev->irq_acquired = true;
355 device_init_wakeup(bdev->dev, true);
357 pm_runtime_set_autosuspend_delay(bdev->dev,
358 BCM_AUTOSUSPEND_DELAY);
359 pm_runtime_use_autosuspend(bdev->dev);
360 pm_runtime_set_active(bdev->dev);
361 pm_runtime_enable(bdev->dev);
364 mutex_unlock(&bcm_device_lock);
369 static const struct bcm_set_sleep_mode default_sleep_params = {
370 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
371 .idle_host = 2, /* idle threshold HOST, in 300ms */
372 .idle_dev = 2, /* idle threshold device, in 300ms */
373 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
374 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
375 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
376 .combine_modes = 1, /* Combine sleep and LPM flag */
377 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
378 /* Irrelevant USB flags */
380 .usb_resume_timeout = 0,
382 .pulsed_host_wake = 1,
385 static int bcm_setup_sleep(struct hci_uart *hu)
387 struct bcm_data *bcm = hu->priv;
389 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
391 sleep_params.host_wake_active = !bcm->dev->irq_active_low;
393 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
394 &sleep_params, HCI_INIT_TIMEOUT);
396 int err = PTR_ERR(skb);
397 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
402 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
407 static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
408 static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
411 static int bcm_set_diag(struct hci_dev *hdev, bool enable)
413 struct hci_uart *hu = hci_get_drvdata(hdev);
414 struct bcm_data *bcm = hu->priv;
417 if (!test_bit(HCI_RUNNING, &hdev->flags))
420 skb = bt_skb_alloc(3, GFP_KERNEL);
424 skb_put_u8(skb, BCM_LM_DIAG_PKT);
425 skb_put_u8(skb, 0xf0);
426 skb_put_u8(skb, enable);
428 skb_queue_tail(&bcm->txq, skb);
429 hci_uart_tx_wakeup(hu);
434 static int bcm_open(struct hci_uart *hu)
436 struct bcm_data *bcm;
440 bt_dev_dbg(hu->hdev, "hu %p", hu);
442 if (!hci_uart_has_flow_control(hu))
445 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
449 skb_queue_head_init(&bcm->txq);
453 mutex_lock(&bcm_device_lock);
456 bcm->dev = serdev_device_get_drvdata(hu->serdev);
463 list_for_each(p, &bcm_device_list) {
464 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
466 /* Retrieve saved bcm_device based on parent of the
467 * platform device (saved during device probe) and
468 * parent of tty device used by hci_uart
470 if (hu->tty->dev->parent == dev->dev->parent) {
481 if (bcm->dev->use_autobaud_mode)
482 hci_uart_set_flow_control(hu, false); /* Assert BT_UART_CTS_N */
483 else if (bcm->dev->drive_rts_on_open)
484 hci_uart_set_flow_control(hu, true);
486 if (bcm->dev->use_autobaud_mode && bcm->dev->max_autobaud_speed)
487 hu->init_speed = min(bcm->dev->oper_speed, bcm->dev->max_autobaud_speed);
489 hu->init_speed = bcm->dev->init_speed;
491 /* If oper_speed is set, ldisc/serdev will set the baudrate
492 * before calling setup()
494 if (!bcm->dev->no_early_set_baudrate && !bcm->dev->use_autobaud_mode)
495 hu->oper_speed = bcm->dev->oper_speed;
497 err = bcm_gpio_set_power(bcm->dev, true);
499 if (bcm->dev->drive_rts_on_open)
500 hci_uart_set_flow_control(hu, false);
506 mutex_unlock(&bcm_device_lock);
514 mutex_unlock(&bcm_device_lock);
520 static int bcm_close(struct hci_uart *hu)
522 struct bcm_data *bcm = hu->priv;
523 struct bcm_device *bdev = NULL;
526 bt_dev_dbg(hu->hdev, "hu %p", hu);
528 /* Protect bcm->dev against removal of the device or driver */
529 mutex_lock(&bcm_device_lock);
532 bdev = serdev_device_get_drvdata(hu->serdev);
533 } else if (bcm_device_exists(bcm->dev)) {
541 if (IS_ENABLED(CONFIG_PM) && bdev->irq_acquired) {
542 devm_free_irq(bdev->dev, bdev->irq, bdev);
543 device_init_wakeup(bdev->dev, false);
544 pm_runtime_disable(bdev->dev);
547 err = bcm_gpio_set_power(bdev, false);
549 bt_dev_err(hu->hdev, "Failed to power down");
551 pm_runtime_set_suspended(bdev->dev);
553 mutex_unlock(&bcm_device_lock);
555 skb_queue_purge(&bcm->txq);
556 kfree_skb(bcm->rx_skb);
563 static int bcm_flush(struct hci_uart *hu)
565 struct bcm_data *bcm = hu->priv;
567 bt_dev_dbg(hu->hdev, "hu %p", hu);
569 skb_queue_purge(&bcm->txq);
574 static int bcm_setup(struct hci_uart *hu)
576 struct bcm_data *bcm = hu->priv;
577 bool fw_load_done = false;
578 bool use_autobaud_mode = (bcm->dev ? bcm->dev->use_autobaud_mode : 0);
582 bt_dev_dbg(hu->hdev, "hu %p", hu);
584 hu->hdev->set_diag = bcm_set_diag;
585 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
587 err = btbcm_initialize(hu->hdev, &fw_load_done, use_autobaud_mode);
594 /* Init speed if any */
595 if (bcm->dev && bcm->dev->init_speed)
596 speed = bcm->dev->init_speed;
597 else if (hu->proto->init_speed)
598 speed = hu->proto->init_speed;
603 host_set_baudrate(hu, speed);
605 /* Operational speed if any */
607 speed = hu->oper_speed;
608 else if (bcm->dev && bcm->dev->oper_speed)
609 speed = bcm->dev->oper_speed;
610 else if (hu->proto->oper_speed)
611 speed = hu->proto->oper_speed;
616 err = bcm_set_baudrate(hu, speed);
618 host_set_baudrate(hu, speed);
621 /* PCM parameters if provided */
622 if (bcm->dev && bcm->dev->pcm_int_params[0] != 0xff) {
623 struct bcm_set_pcm_int_params params;
625 btbcm_read_pcm_int_params(hu->hdev, ¶ms);
627 memcpy(¶ms, bcm->dev->pcm_int_params, 5);
628 btbcm_write_pcm_int_params(hu->hdev, ¶ms);
631 err = btbcm_finalize(hu->hdev, &fw_load_done, use_autobaud_mode);
635 /* Some devices ship with the controller default address.
636 * Allow the bootloader to set a valid address through the
639 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hu->hdev->quirks);
641 if (!bcm_request_irq(bcm))
642 err = bcm_setup_sleep(hu);
647 #define BCM_RECV_LM_DIAG \
648 .type = BCM_LM_DIAG_PKT, \
649 .hlen = BCM_LM_DIAG_SIZE, \
652 .maxlen = BCM_LM_DIAG_SIZE
654 #define BCM_RECV_NULL \
655 .type = BCM_NULL_PKT, \
656 .hlen = BCM_NULL_SIZE, \
659 .maxlen = BCM_NULL_SIZE
661 #define BCM_RECV_TYPE49 \
662 .type = BCM_TYPE49_PKT, \
663 .hlen = BCM_TYPE49_SIZE, \
666 .maxlen = BCM_TYPE49_SIZE
668 #define BCM_RECV_TYPE52 \
669 .type = BCM_TYPE52_PKT, \
670 .hlen = BCM_TYPE52_SIZE, \
673 .maxlen = BCM_TYPE52_SIZE
675 static const struct h4_recv_pkt bcm_recv_pkts[] = {
676 { H4_RECV_ACL, .recv = hci_recv_frame },
677 { H4_RECV_SCO, .recv = hci_recv_frame },
678 { H4_RECV_EVENT, .recv = hci_recv_frame },
679 { H4_RECV_ISO, .recv = hci_recv_frame },
680 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
681 { BCM_RECV_NULL, .recv = hci_recv_diag },
682 { BCM_RECV_TYPE49, .recv = hci_recv_diag },
683 { BCM_RECV_TYPE52, .recv = hci_recv_diag },
686 static int bcm_recv(struct hci_uart *hu, const void *data, int count)
688 struct bcm_data *bcm = hu->priv;
690 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
693 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
694 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
695 if (IS_ERR(bcm->rx_skb)) {
696 int err = PTR_ERR(bcm->rx_skb);
697 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
700 } else if (!bcm->rx_skb) {
701 /* Delay auto-suspend when receiving completed packet */
702 mutex_lock(&bcm_device_lock);
703 if (bcm->dev && bcm_device_exists(bcm->dev)) {
704 pm_runtime_get(bcm->dev->dev);
705 pm_runtime_mark_last_busy(bcm->dev->dev);
706 pm_runtime_put_autosuspend(bcm->dev->dev);
708 mutex_unlock(&bcm_device_lock);
714 static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
716 struct bcm_data *bcm = hu->priv;
718 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
720 /* Prepend skb with frame type */
721 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
722 skb_queue_tail(&bcm->txq, skb);
727 static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
729 struct bcm_data *bcm = hu->priv;
730 struct sk_buff *skb = NULL;
731 struct bcm_device *bdev = NULL;
733 mutex_lock(&bcm_device_lock);
735 if (bcm_device_exists(bcm->dev)) {
737 pm_runtime_get_sync(bdev->dev);
738 /* Shall be resumed here */
741 skb = skb_dequeue(&bcm->txq);
744 pm_runtime_mark_last_busy(bdev->dev);
745 pm_runtime_put_autosuspend(bdev->dev);
748 mutex_unlock(&bcm_device_lock);
754 static int bcm_suspend_device(struct device *dev)
756 struct bcm_device *bdev = dev_get_drvdata(dev);
759 bt_dev_dbg(bdev, "");
761 if (!bdev->is_suspended && bdev->hu) {
762 hci_uart_set_flow_control(bdev->hu, true);
764 /* Once this returns, driver suspends BT via GPIO */
765 bdev->is_suspended = true;
768 /* Suspend the device */
769 err = bdev->set_device_wakeup(bdev, false);
771 if (bdev->is_suspended && bdev->hu) {
772 bdev->is_suspended = false;
773 hci_uart_set_flow_control(bdev->hu, false);
778 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
784 static int bcm_resume_device(struct device *dev)
786 struct bcm_device *bdev = dev_get_drvdata(dev);
789 bt_dev_dbg(bdev, "");
791 err = bdev->set_device_wakeup(bdev, true);
793 dev_err(dev, "Failed to power up\n");
797 bt_dev_dbg(bdev, "resume, delaying 15 ms");
800 /* When this executes, the device has woken up already */
801 if (bdev->is_suspended && bdev->hu) {
802 bdev->is_suspended = false;
804 hci_uart_set_flow_control(bdev->hu, false);
811 #ifdef CONFIG_PM_SLEEP
812 /* suspend callback */
813 static int bcm_suspend(struct device *dev)
815 struct bcm_device *bdev = dev_get_drvdata(dev);
818 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
821 * When used with a device instantiated as platform_device, bcm_suspend
822 * can be called at any time as long as the platform device is bound,
823 * so it should use bcm_device_lock to protect access to hci_uart
824 * and device_wake-up GPIO.
826 mutex_lock(&bcm_device_lock);
831 if (pm_runtime_active(dev))
832 bcm_suspend_device(dev);
834 if (device_may_wakeup(dev) && bdev->irq > 0) {
835 error = enable_irq_wake(bdev->irq);
837 bt_dev_dbg(bdev, "BCM irq: enabled");
841 mutex_unlock(&bcm_device_lock);
846 /* resume callback */
847 static int bcm_resume(struct device *dev)
849 struct bcm_device *bdev = dev_get_drvdata(dev);
852 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
855 * When used with a device instantiated as platform_device, bcm_resume
856 * can be called at any time as long as platform device is bound,
857 * so it should use bcm_device_lock to protect access to hci_uart
858 * and device_wake-up GPIO.
860 mutex_lock(&bcm_device_lock);
865 if (device_may_wakeup(dev) && bdev->irq > 0) {
866 disable_irq_wake(bdev->irq);
867 bt_dev_dbg(bdev, "BCM irq: disabled");
870 err = bcm_resume_device(dev);
873 mutex_unlock(&bcm_device_lock);
876 pm_runtime_disable(dev);
877 pm_runtime_set_active(dev);
878 pm_runtime_enable(dev);
885 /* Some firmware reports an IRQ which does not work (wrong pin in fw table?) */
886 static struct gpiod_lookup_table asus_tf103c_irq_gpios = {
887 .dev_id = "serial0-0",
889 GPIO_LOOKUP("INT33FC:02", 17, "host-wakeup-alt", GPIO_ACTIVE_HIGH),
894 static const struct dmi_system_id bcm_broken_irq_dmi_table[] = {
896 .ident = "Asus TF103C",
898 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
899 DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
901 .driver_data = &asus_tf103c_irq_gpios,
904 .ident = "Meegopad T08",
906 DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
907 "To be filled by OEM."),
908 DMI_EXACT_MATCH(DMI_BOARD_NAME, "T3 MRD"),
909 DMI_EXACT_MATCH(DMI_BOARD_VERSION, "V1.1"),
916 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
917 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
918 static const struct acpi_gpio_params third_gpio = { 2, 0, false };
920 static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
921 { "device-wakeup-gpios", &first_gpio, 1 },
922 { "shutdown-gpios", &second_gpio, 1 },
923 { "host-wakeup-gpios", &third_gpio, 1 },
927 static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
928 { "host-wakeup-gpios", &first_gpio, 1 },
929 { "device-wakeup-gpios", &second_gpio, 1 },
930 { "shutdown-gpios", &third_gpio, 1 },
934 static int bcm_resource(struct acpi_resource *ares, void *data)
936 struct bcm_device *dev = data;
937 struct acpi_resource_extended_irq *irq;
938 struct acpi_resource_gpio *gpio;
939 struct acpi_resource_uart_serialbus *sb;
941 switch (ares->type) {
942 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
943 irq = &ares->data.extended_irq;
944 if (irq->polarity != ACPI_ACTIVE_LOW)
945 dev_info(dev->dev, "ACPI Interrupt resource is active-high, this is usually wrong, treating the IRQ as active-low\n");
946 dev->irq_active_low = true;
949 case ACPI_RESOURCE_TYPE_GPIO:
950 gpio = &ares->data.gpio;
951 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
952 dev->gpio_int_idx = dev->gpio_count;
953 dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
958 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
959 sb = &ares->data.uart_serial_bus;
960 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
961 dev->init_speed = sb->default_baud_rate;
962 dev->oper_speed = 4000000;
973 static int bcm_apple_set_device_wakeup(struct bcm_device *dev, bool awake)
975 if (ACPI_FAILURE(acpi_execute_simple_method(dev->btlp, NULL, !awake)))
981 static int bcm_apple_set_shutdown(struct bcm_device *dev, bool powered)
983 if (ACPI_FAILURE(acpi_evaluate_object(powered ? dev->btpu : dev->btpd,
990 static int bcm_apple_get_resources(struct bcm_device *dev)
992 struct acpi_device *adev = ACPI_COMPANION(dev->dev);
993 const union acpi_object *obj;
996 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTLP", &dev->btlp)) ||
997 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPU", &dev->btpu)) ||
998 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPD", &dev->btpd)))
1001 if (!acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, &obj) &&
1002 obj->buffer.length == 8)
1003 dev->init_speed = *(u64 *)obj->buffer.pointer;
1005 dev->set_device_wakeup = bcm_apple_set_device_wakeup;
1006 dev->set_shutdown = bcm_apple_set_shutdown;
1011 static inline int bcm_apple_get_resources(struct bcm_device *dev)
1015 #endif /* CONFIG_ACPI */
1017 static int bcm_gpio_set_device_wakeup(struct bcm_device *dev, bool awake)
1019 gpiod_set_value_cansleep(dev->device_wakeup, awake);
1023 static int bcm_gpio_set_shutdown(struct bcm_device *dev, bool powered)
1025 gpiod_set_value_cansleep(dev->shutdown, powered);
1028 * The reset line is asserted on powerdown and deasserted
1029 * on poweron so the inverse of powered is used. Notice
1030 * that the GPIO line BT_RST_N needs to be specified as
1031 * active low in the device tree or similar system
1034 gpiod_set_value_cansleep(dev->reset, !powered);
1038 /* Try a bunch of names for TXCO */
1039 static struct clk *bcm_get_txco(struct device *dev)
1043 /* New explicit name */
1044 clk = devm_clk_get(dev, "txco");
1045 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
1048 /* Deprecated name */
1049 clk = devm_clk_get(dev, "extclk");
1050 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
1053 /* Original code used no name at all */
1054 return devm_clk_get(dev, NULL);
1057 static int bcm_get_resources(struct bcm_device *dev)
1059 const struct dmi_system_id *broken_irq_dmi_id;
1060 const char *irq_con_id = "host-wakeup";
1063 dev->name = dev_name(dev->dev);
1065 if (x86_apple_machine && !bcm_apple_get_resources(dev))
1068 dev->txco_clk = bcm_get_txco(dev->dev);
1070 /* Handle deferred probing */
1071 if (dev->txco_clk == ERR_PTR(-EPROBE_DEFER))
1072 return PTR_ERR(dev->txco_clk);
1074 /* Ignore all other errors as before */
1075 if (IS_ERR(dev->txco_clk))
1076 dev->txco_clk = NULL;
1078 dev->lpo_clk = devm_clk_get(dev->dev, "lpo");
1079 if (dev->lpo_clk == ERR_PTR(-EPROBE_DEFER))
1080 return PTR_ERR(dev->lpo_clk);
1082 if (IS_ERR(dev->lpo_clk))
1083 dev->lpo_clk = NULL;
1085 /* Check if we accidentally fetched the lpo clock twice */
1086 if (dev->lpo_clk && clk_is_match(dev->lpo_clk, dev->txco_clk)) {
1087 devm_clk_put(dev->dev, dev->txco_clk);
1088 dev->txco_clk = NULL;
1091 dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup",
1093 if (IS_ERR(dev->device_wakeup))
1094 return PTR_ERR(dev->device_wakeup);
1096 dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown",
1098 if (IS_ERR(dev->shutdown))
1099 return PTR_ERR(dev->shutdown);
1101 dev->reset = devm_gpiod_get_optional(dev->dev, "reset",
1103 if (IS_ERR(dev->reset))
1104 return PTR_ERR(dev->reset);
1106 dev->set_device_wakeup = bcm_gpio_set_device_wakeup;
1107 dev->set_shutdown = bcm_gpio_set_shutdown;
1109 dev->supplies[0].supply = "vbat";
1110 dev->supplies[1].supply = "vddio";
1111 err = devm_regulator_bulk_get(dev->dev, BCM_NUM_SUPPLIES,
1116 broken_irq_dmi_id = dmi_first_match(bcm_broken_irq_dmi_table);
1117 if (broken_irq_dmi_id && broken_irq_dmi_id->driver_data) {
1118 gpiod_add_lookup_table(broken_irq_dmi_id->driver_data);
1119 irq_con_id = "host-wakeup-alt";
1120 dev->irq_active_low = false;
1124 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
1125 if (dev->irq <= 0) {
1126 struct gpio_desc *gpio;
1128 gpio = devm_gpiod_get_optional(dev->dev, irq_con_id, GPIOD_IN);
1130 return PTR_ERR(gpio);
1132 dev->irq = gpiod_to_irq(gpio);
1135 if (broken_irq_dmi_id) {
1136 if (broken_irq_dmi_id->driver_data) {
1137 gpiod_remove_lookup_table(broken_irq_dmi_id->driver_data);
1139 dev_info(dev->dev, "%s: Has a broken IRQ config, disabling IRQ support / runtime-pm\n",
1140 broken_irq_dmi_id->ident);
1145 dev_dbg(dev->dev, "BCM irq: %d\n", dev->irq);
1150 static int bcm_acpi_probe(struct bcm_device *dev)
1152 LIST_HEAD(resources);
1153 const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
1154 struct resource_entry *entry;
1157 /* Retrieve UART ACPI info */
1158 dev->gpio_int_idx = -1;
1159 ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
1160 &resources, bcm_resource, dev);
1164 resource_list_for_each_entry(entry, &resources) {
1165 if (resource_type(entry->res) == IORESOURCE_IRQ) {
1166 dev->irq = entry->res->start;
1170 acpi_dev_free_resource_list(&resources);
1172 /* If the DSDT uses an Interrupt resource for the IRQ, then there are
1173 * only 2 GPIO resources, we use the irq-last mapping for this, since
1174 * we already have an irq the 3th / last mapping will not be used.
1177 gpio_mapping = acpi_bcm_int_last_gpios;
1178 else if (dev->gpio_int_idx == 0)
1179 gpio_mapping = acpi_bcm_int_first_gpios;
1180 else if (dev->gpio_int_idx == 2)
1181 gpio_mapping = acpi_bcm_int_last_gpios;
1183 dev_warn(dev->dev, "Unexpected ACPI gpio_int_idx: %d\n",
1186 /* Warn if our expectations are not met. */
1187 if (dev->gpio_count != (dev->irq ? 2 : 3))
1188 dev_warn(dev->dev, "Unexpected number of ACPI GPIOs: %d\n",
1191 ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
1195 if (irq_polarity != -1) {
1196 dev->irq_active_low = irq_polarity;
1197 dev_warn(dev->dev, "Overwriting IRQ polarity to active %s by module-param\n",
1198 dev->irq_active_low ? "low" : "high");
1204 static int bcm_acpi_probe(struct bcm_device *dev)
1208 #endif /* CONFIG_ACPI */
1210 static int bcm_of_probe(struct bcm_device *bdev)
1212 bdev->use_autobaud_mode = device_property_read_bool(bdev->dev,
1213 "brcm,requires-autobaud-mode");
1214 device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
1215 device_property_read_u8_array(bdev->dev, "brcm,bt-pcm-int-params",
1216 bdev->pcm_int_params, 5);
1217 bdev->irq = of_irq_get_byname(bdev->dev->of_node, "host-wakeup");
1218 bdev->irq_active_low = irq_get_trigger_type(bdev->irq)
1219 & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW);
1223 static int bcm_probe(struct platform_device *pdev)
1225 struct bcm_device *dev;
1228 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1232 dev->dev = &pdev->dev;
1234 ret = platform_get_irq(pdev, 0);
1240 /* Initialize routing field to an unused value */
1241 dev->pcm_int_params[0] = 0xff;
1243 if (has_acpi_companion(&pdev->dev)) {
1244 ret = bcm_acpi_probe(dev);
1249 ret = bcm_get_resources(dev);
1253 platform_set_drvdata(pdev, dev);
1255 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
1257 /* Place this instance on the device list */
1258 mutex_lock(&bcm_device_lock);
1259 list_add_tail(&dev->list, &bcm_device_list);
1260 mutex_unlock(&bcm_device_lock);
1262 ret = bcm_gpio_set_power(dev, false);
1264 dev_err(&pdev->dev, "Failed to power down\n");
1269 static int bcm_remove(struct platform_device *pdev)
1271 struct bcm_device *dev = platform_get_drvdata(pdev);
1273 mutex_lock(&bcm_device_lock);
1274 list_del(&dev->list);
1275 mutex_unlock(&bcm_device_lock);
1277 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
1282 static const struct hci_uart_proto bcm_proto = {
1286 .init_speed = 115200,
1291 .set_baudrate = bcm_set_baudrate,
1293 .enqueue = bcm_enqueue,
1294 .dequeue = bcm_dequeue,
1298 static const struct acpi_device_id bcm_acpi_match[] = {
1467 MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
1470 /* suspend and resume callbacks */
1471 static const struct dev_pm_ops bcm_pm_ops = {
1472 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
1473 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
1476 static struct platform_driver bcm_driver = {
1478 .remove = bcm_remove,
1481 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1486 static int bcm_serdev_probe(struct serdev_device *serdev)
1488 struct bcm_device *bcmdev;
1489 const struct bcm_device_data *data;
1492 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
1496 bcmdev->dev = &serdev->dev;
1498 bcmdev->hu = &bcmdev->serdev_hu;
1500 bcmdev->serdev_hu.serdev = serdev;
1501 serdev_device_set_drvdata(serdev, bcmdev);
1503 /* Initialize routing field to an unused value */
1504 bcmdev->pcm_int_params[0] = 0xff;
1506 if (has_acpi_companion(&serdev->dev))
1507 err = bcm_acpi_probe(bcmdev);
1509 err = bcm_of_probe(bcmdev);
1513 err = bcm_get_resources(bcmdev);
1517 if (!bcmdev->shutdown) {
1518 dev_warn(&serdev->dev,
1519 "No reset resource, using default baud rate\n");
1520 bcmdev->oper_speed = bcmdev->init_speed;
1523 err = bcm_gpio_set_power(bcmdev, false);
1525 dev_err(&serdev->dev, "Failed to power down\n");
1527 data = device_get_match_data(bcmdev->dev);
1529 bcmdev->max_autobaud_speed = data->max_autobaud_speed;
1530 bcmdev->no_early_set_baudrate = data->no_early_set_baudrate;
1531 bcmdev->drive_rts_on_open = data->drive_rts_on_open;
1534 return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
1537 static void bcm_serdev_remove(struct serdev_device *serdev)
1539 struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
1541 hci_uart_unregister_device(&bcmdev->serdev_hu);
1545 static struct bcm_device_data bcm4354_device_data = {
1546 .no_early_set_baudrate = true,
1549 static struct bcm_device_data bcm43438_device_data = {
1550 .drive_rts_on_open = true,
1553 static struct bcm_device_data cyw55572_device_data = {
1554 .max_autobaud_speed = 921600,
1557 static const struct of_device_id bcm_bluetooth_of_match[] = {
1558 { .compatible = "brcm,bcm20702a1" },
1559 { .compatible = "brcm,bcm4329-bt" },
1560 { .compatible = "brcm,bcm4330-bt" },
1561 { .compatible = "brcm,bcm4334-bt" },
1562 { .compatible = "brcm,bcm4345c5" },
1563 { .compatible = "brcm,bcm43430a0-bt" },
1564 { .compatible = "brcm,bcm43430a1-bt" },
1565 { .compatible = "brcm,bcm43438-bt", .data = &bcm43438_device_data },
1566 { .compatible = "brcm,bcm4349-bt", .data = &bcm43438_device_data },
1567 { .compatible = "brcm,bcm43540-bt", .data = &bcm4354_device_data },
1568 { .compatible = "brcm,bcm4335a0" },
1569 { .compatible = "infineon,cyw55572-bt", .data = &cyw55572_device_data },
1572 MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
1575 static struct serdev_device_driver bcm_serdev_driver = {
1576 .probe = bcm_serdev_probe,
1577 .remove = bcm_serdev_remove,
1579 .name = "hci_uart_bcm",
1580 .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
1581 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1586 int __init bcm_init(void)
1588 /* For now, we need to keep both platform device
1589 * driver (ACPI generated) and serdev driver (DT).
1591 platform_driver_register(&bcm_driver);
1592 serdev_device_driver_register(&bcm_serdev_driver);
1594 return hci_uart_register_proto(&bcm_proto);
1597 int __exit bcm_deinit(void)
1599 platform_driver_unregister(&bcm_driver);
1600 serdev_device_driver_unregister(&bcm_serdev_driver);
1602 return hci_uart_unregister_proto(&bcm_proto);