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>
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>
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 */
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
94 /* Must be the first member, hci_serdev.c expects this. */
95 struct hci_uart serdev_hu
;
96 struct list_head list
;
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);
106 acpi_handle btlp
, btpu
, btpd
;
126 /* generic bcm uart resources */
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
)
145 serdev_device_set_baudrate(hu
->serdev
, speed
);
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
;
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
);
168 int err
= PTR_ERR(skb
);
169 bt_dev_err(hdev
, "BCM: failed to write clock (%d)",
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
185 skb
= __hci_cmd_sync(hdev
, 0xfc18, sizeof(param
), ¶m
,
188 int err
= PTR_ERR(skb
);
189 bt_dev_err(hdev
, "BCM: failed to write update baudrate (%d)",
199 /* bcm_device_exists should be protected by bcm_device_lock */
200 static bool bcm_device_exists(struct bcm_device
*device
)
205 /* Devices using serdev always exist */
206 if (device
&& device
->hu
&& device
->hu
->serdev
)
210 list_for_each(p
, &bcm_device_list
) {
211 struct bcm_device
*dev
= list_entry(p
, struct bcm_device
, list
);
220 static int bcm_gpio_set_power(struct bcm_device
*dev
, bool powered
)
224 if (powered
&& !IS_ERR(dev
->clk
) && !dev
->clk_enabled
) {
225 err
= clk_prepare_enable(dev
->clk
);
230 err
= dev
->set_shutdown(dev
, powered
);
232 goto err_clk_disable
;
234 err
= dev
->set_device_wakeup(dev
, powered
);
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
;
246 dev
->set_shutdown(dev
, !powered
);
248 if (powered
&& !IS_ERR(dev
->clk
) && !dev
->clk_enabled
)
249 clk_disable_unprepare(dev
->clk
);
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
);
267 static int bcm_request_irq(struct bcm_data
*bcm
)
269 struct bcm_device
*bdev
= bcm
->dev
;
272 mutex_lock(&bcm_device_lock
);
273 if (!bcm_device_exists(bdev
)) {
278 if (bdev
->irq
<= 0) {
283 err
= devm_request_irq(bdev
->dev
, bdev
->irq
, bcm_host_wake
,
284 bdev
->irq_active_low
? IRQF_TRIGGER_FALLING
:
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
);
303 mutex_unlock(&bcm_device_lock
);
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 */
319 .usb_resume_timeout
= 0,
321 .pulsed_host_wake
= 1,
324 static int bcm_setup_sleep(struct hci_uart
*hu
)
326 struct bcm_data
*bcm
= hu
->priv
;
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
);
335 int err
= PTR_ERR(skb
);
336 bt_dev_err(hu
->hdev
, "Sleep VSC failed (%d)", err
);
341 bt_dev_dbg(hu
->hdev
, "Set Sleep Parameters VSC succeeded");
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; }
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
;
356 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
359 skb
= bt_skb_alloc(3, GFP_KERNEL
);
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
);
373 static int bcm_open(struct hci_uart
*hu
)
375 struct bcm_data
*bcm
;
379 bt_dev_dbg(hu
->hdev
, "hu %p", hu
);
381 if (!hci_uart_has_flow_control(hu
))
384 bcm
= kzalloc(sizeof(*bcm
), GFP_KERNEL
);
388 skb_queue_head_init(&bcm
->txq
);
392 mutex_lock(&bcm_device_lock
);
395 bcm
->dev
= serdev_device_get_drvdata(hu
->serdev
);
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
) {
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);
427 mutex_unlock(&bcm_device_lock
);
435 mutex_unlock(&bcm_device_lock
);
441 static int bcm_close(struct hci_uart
*hu
)
443 struct bcm_data
*bcm
= hu
->priv
;
444 struct bcm_device
*bdev
= NULL
;
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
);
453 bdev
= serdev_device_get_drvdata(hu
->serdev
);
454 } else if (bcm_device_exists(bcm
->dev
)) {
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);
470 bt_dev_err(hu
->hdev
, "Failed to power down");
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
);
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
);
495 static int bcm_setup(struct hci_uart
*hu
)
497 struct bcm_data
*bcm
= hu
->priv
;
499 const struct firmware
*fw
;
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);
512 err
= request_firmware(&fw
, fw_name
, &hu
->hdev
->dev
);
514 bt_dev_info(hu
->hdev
, "BCM: Patch %s not found", fw_name
);
518 err
= btbcm_patchram(hu
->hdev
, fw
);
520 bt_dev_info(hu
->hdev
, "BCM: Patch failed (%d)", err
);
524 /* Init speed if any */
526 speed
= hu
->init_speed
;
527 else if (hu
->proto
->init_speed
)
528 speed
= hu
->proto
->init_speed
;
533 host_set_baudrate(hu
, speed
);
535 /* Operational speed if any */
537 speed
= hu
->oper_speed
;
538 else if (hu
->proto
->oper_speed
)
539 speed
= hu
->proto
->oper_speed
;
544 err
= bcm_set_baudrate(hu
, speed
);
546 host_set_baudrate(hu
, speed
);
550 release_firmware(fw
);
552 err
= btbcm_finalize(hu
->hdev
);
556 if (!bcm_request_irq(bcm
))
557 err
= bcm_setup_sleep(hu
);
562 #define BCM_RECV_LM_DIAG \
563 .type = BCM_LM_DIAG_PKT, \
564 .hlen = BCM_LM_DIAG_SIZE, \
567 .maxlen = BCM_LM_DIAG_SIZE
569 #define BCM_RECV_NULL \
570 .type = BCM_NULL_PKT, \
571 .hlen = BCM_NULL_SIZE, \
574 .maxlen = BCM_NULL_SIZE
576 #define BCM_RECV_TYPE49 \
577 .type = BCM_TYPE49_PKT, \
578 .hlen = BCM_TYPE49_SIZE, \
581 .maxlen = BCM_TYPE49_SIZE
583 #define BCM_RECV_TYPE52 \
584 .type = BCM_TYPE52_PKT, \
585 .hlen = BCM_TYPE52_SIZE, \
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
))
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
);
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
);
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
);
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
)) {
651 pm_runtime_get_sync(bdev
->dev
);
652 /* Shall be resumed here */
655 skb
= skb_dequeue(&bcm
->txq
);
658 pm_runtime_mark_last_busy(bdev
->dev
);
659 pm_runtime_put_autosuspend(bdev
->dev
);
662 mutex_unlock(&bcm_device_lock
);
668 static int bcm_suspend_device(struct device
*dev
)
670 struct bcm_device
*bdev
= dev_get_drvdata(dev
);
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);
685 if (bdev
->is_suspended
&& bdev
->hu
) {
686 bdev
->is_suspended
= false;
687 hci_uart_set_flow_control(bdev
->hu
, false);
692 bt_dev_dbg(bdev
, "suspend, delaying 15 ms");
698 static int bcm_resume_device(struct device
*dev
)
700 struct bcm_device
*bdev
= dev_get_drvdata(dev
);
703 bt_dev_dbg(bdev
, "");
705 err
= bdev
->set_device_wakeup(bdev
, true);
707 dev_err(dev
, "Failed to power up\n");
711 bt_dev_dbg(bdev
, "resume, delaying 15 ms");
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);
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
);
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
);
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
);
751 bt_dev_dbg(bdev
, "BCM irq: enabled");
755 mutex_unlock(&bcm_device_lock
);
760 /* resume callback */
761 static int bcm_resume(struct device
*dev
)
763 struct bcm_device
*bdev
= dev_get_drvdata(dev
);
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
);
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
);
787 mutex_unlock(&bcm_device_lock
);
790 pm_runtime_disable(dev
);
791 pm_runtime_set_active(dev
);
792 pm_runtime_enable(dev
);
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 },
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 },
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",
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"),
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;
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
;
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;
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
)))
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
,
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
;
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
)))
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
;
909 static inline int bcm_apple_get_resources(struct bcm_device
*dev
)
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
);
921 static int bcm_gpio_set_shutdown(struct bcm_device
*dev
, bool powered
)
923 gpiod_set_value_cansleep(dev
->shutdown
, powered
);
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
))
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",
944 if (IS_ERR(dev
->device_wakeup
))
945 return PTR_ERR(dev
->device_wakeup
);
947 dev
->shutdown
= devm_gpiod_get_optional(dev
->dev
, "shutdown",
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 */
957 struct gpio_desc
*gpio
;
959 gpio
= devm_gpiod_get_optional(dev
->dev
, "host-wakeup",
962 return PTR_ERR(gpio
);
964 dev
->irq
= gpiod_to_irq(gpio
);
967 dmi_id
= dmi_first_match(bcm_broken_irq_dmi_table
);
969 dev_info(dev
->dev
, "%s: Has a broken IRQ config, disabling IRQ support / runtime-pm\n",
974 dev_dbg(dev
->dev
, "BCM irq: %d\n", dev
->irq
);
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
;
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
);
993 resource_list_for_each_entry(entry
, &resources
) {
994 if (resource_type(entry
->res
) == IORESOURCE_IRQ
) {
995 dev
->irq
= entry
->res
->start
;
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.
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
;
1012 dev_warn(dev
->dev
, "Unexpected ACPI gpio_int_idx: %d\n",
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",
1020 ret
= devm_acpi_dev_add_driver_gpios(dev
->dev
, gpio_mapping
);
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");
1033 static int bcm_acpi_probe(struct bcm_device
*dev
)
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
);
1045 static int bcm_probe(struct platform_device
*pdev
)
1047 struct bcm_device
*dev
;
1050 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
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
);
1063 ret
= bcm_get_resources(dev
);
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);
1078 dev_err(&pdev
->dev
, "Failed to power down\n");
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
);
1096 static const struct hci_uart_proto bcm_proto
= {
1100 .init_speed
= 115200,
1105 .set_baudrate
= bcm_set_baudrate
,
1107 .enqueue
= bcm_enqueue
,
1108 .dequeue
= bcm_dequeue
,
1112 static const struct acpi_device_id bcm_acpi_match
[] = {
1281 MODULE_DEVICE_TABLE(acpi
, bcm_acpi_match
);
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
= {
1292 .remove
= bcm_remove
,
1295 .acpi_match_table
= ACPI_PTR(bcm_acpi_match
),
1300 static int bcm_serdev_probe(struct serdev_device
*serdev
)
1302 struct bcm_device
*bcmdev
;
1305 bcmdev
= devm_kzalloc(&serdev
->dev
, sizeof(*bcmdev
), GFP_KERNEL
);
1309 bcmdev
->dev
= &serdev
->dev
;
1311 bcmdev
->hu
= &bcmdev
->serdev_hu
;
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
);
1319 err
= bcm_of_probe(bcmdev
);
1323 err
= bcm_get_resources(bcmdev
);
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);
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
);
1348 static const struct of_device_id bcm_bluetooth_of_match
[] = {
1349 { .compatible
= "brcm,bcm43438-bt" },
1352 MODULE_DEVICE_TABLE(of
, bcm_bluetooth_of_match
);
1355 static struct serdev_device_driver bcm_serdev_driver
= {
1356 .probe
= bcm_serdev_probe
,
1357 .remove
= bcm_serdev_remove
,
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
),
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
);