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/platform_device.h>
31 #include <linux/clk.h>
32 #include <linux/gpio/consumer.h>
33 #include <linux/tty.h>
35 #include <net/bluetooth/bluetooth.h>
36 #include <net/bluetooth/hci_core.h>
42 struct list_head list
;
44 struct platform_device
*pdev
;
47 struct gpio_desc
*device_wakeup
;
48 struct gpio_desc
*shutdown
;
55 #ifdef CONFIG_PM_SLEEP
57 bool is_suspended
; /* suspend/resume flag */
62 struct sk_buff
*rx_skb
;
63 struct sk_buff_head txq
;
65 struct bcm_device
*dev
;
68 /* List of BCM BT UART devices */
69 static DEFINE_SPINLOCK(bcm_device_lock
);
70 static LIST_HEAD(bcm_device_list
);
72 static int bcm_set_baudrate(struct hci_uart
*hu
, unsigned int speed
)
74 struct hci_dev
*hdev
= hu
->hdev
;
76 struct bcm_update_uart_baud_rate param
;
78 if (speed
> 3000000) {
79 struct bcm_write_uart_clock_setting clock
;
81 clock
.type
= BCM_UART_CLOCK_48MHZ
;
83 BT_DBG("%s: Set Controller clock (%d)", hdev
->name
, clock
.type
);
85 /* This Broadcom specific command changes the UART's controller
86 * clock for baud rate > 3000000.
88 skb
= __hci_cmd_sync(hdev
, 0xfc45, 1, &clock
, HCI_INIT_TIMEOUT
);
90 int err
= PTR_ERR(skb
);
91 BT_ERR("%s: BCM: failed to write clock command (%d)",
99 BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev
->name
, speed
);
101 param
.zero
= cpu_to_le16(0);
102 param
.baud_rate
= cpu_to_le32(speed
);
104 /* This Broadcom specific command changes the UART's controller baud
107 skb
= __hci_cmd_sync(hdev
, 0xfc18, sizeof(param
), ¶m
,
110 int err
= PTR_ERR(skb
);
111 BT_ERR("%s: BCM: failed to write update baudrate command (%d)",
121 /* bcm_device_exists should be protected by bcm_device_lock */
122 static bool bcm_device_exists(struct bcm_device
*device
)
126 list_for_each(p
, &bcm_device_list
) {
127 struct bcm_device
*dev
= list_entry(p
, struct bcm_device
, list
);
136 static int bcm_gpio_set_power(struct bcm_device
*dev
, bool powered
)
138 if (powered
&& !IS_ERR(dev
->clk
) && !dev
->clk_enabled
)
139 clk_enable(dev
->clk
);
141 gpiod_set_value(dev
->shutdown
, powered
);
142 gpiod_set_value(dev
->device_wakeup
, powered
);
144 if (!powered
&& !IS_ERR(dev
->clk
) && dev
->clk_enabled
)
145 clk_disable(dev
->clk
);
147 dev
->clk_enabled
= powered
;
152 static int bcm_open(struct hci_uart
*hu
)
154 struct bcm_data
*bcm
;
159 bcm
= kzalloc(sizeof(*bcm
), GFP_KERNEL
);
163 skb_queue_head_init(&bcm
->txq
);
167 spin_lock(&bcm_device_lock
);
168 list_for_each(p
, &bcm_device_list
) {
169 struct bcm_device
*dev
= list_entry(p
, struct bcm_device
, list
);
171 /* Retrieve saved bcm_device based on parent of the
172 * platform device (saved during device probe) and
173 * parent of tty device used by hci_uart
175 if (hu
->tty
->dev
->parent
== dev
->pdev
->dev
.parent
) {
177 hu
->init_speed
= dev
->init_speed
;
178 #ifdef CONFIG_PM_SLEEP
186 bcm_gpio_set_power(bcm
->dev
, true);
188 spin_unlock(&bcm_device_lock
);
193 static int bcm_close(struct hci_uart
*hu
)
195 struct bcm_data
*bcm
= hu
->priv
;
199 /* Protect bcm->dev against removal of the device or driver */
200 spin_lock(&bcm_device_lock
);
201 if (bcm_device_exists(bcm
->dev
)) {
202 bcm_gpio_set_power(bcm
->dev
, false);
203 #ifdef CONFIG_PM_SLEEP
207 spin_unlock(&bcm_device_lock
);
209 skb_queue_purge(&bcm
->txq
);
210 kfree_skb(bcm
->rx_skb
);
217 static int bcm_flush(struct hci_uart
*hu
)
219 struct bcm_data
*bcm
= hu
->priv
;
223 skb_queue_purge(&bcm
->txq
);
228 static int bcm_setup(struct hci_uart
*hu
)
231 const struct firmware
*fw
;
237 hu
->hdev
->set_bdaddr
= btbcm_set_bdaddr
;
239 err
= btbcm_initialize(hu
->hdev
, fw_name
, sizeof(fw_name
));
243 err
= request_firmware(&fw
, fw_name
, &hu
->hdev
->dev
);
245 BT_INFO("%s: BCM: Patch %s not found", hu
->hdev
->name
, fw_name
);
249 err
= btbcm_patchram(hu
->hdev
, fw
);
251 BT_INFO("%s: BCM: Patch failed (%d)", hu
->hdev
->name
, err
);
255 /* Init speed if any */
257 speed
= hu
->init_speed
;
258 else if (hu
->proto
->init_speed
)
259 speed
= hu
->proto
->init_speed
;
264 hci_uart_set_baudrate(hu
, speed
);
266 /* Operational speed if any */
268 speed
= hu
->oper_speed
;
269 else if (hu
->proto
->oper_speed
)
270 speed
= hu
->proto
->oper_speed
;
275 err
= bcm_set_baudrate(hu
, speed
);
277 hci_uart_set_baudrate(hu
, speed
);
281 release_firmware(fw
);
283 err
= btbcm_finalize(hu
->hdev
);
288 static const struct h4_recv_pkt bcm_recv_pkts
[] = {
289 { H4_RECV_ACL
, .recv
= hci_recv_frame
},
290 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
291 { H4_RECV_EVENT
, .recv
= hci_recv_frame
},
294 static int bcm_recv(struct hci_uart
*hu
, const void *data
, int count
)
296 struct bcm_data
*bcm
= hu
->priv
;
298 if (!test_bit(HCI_UART_REGISTERED
, &hu
->flags
))
301 bcm
->rx_skb
= h4_recv_buf(hu
->hdev
, bcm
->rx_skb
, data
, count
,
302 bcm_recv_pkts
, ARRAY_SIZE(bcm_recv_pkts
));
303 if (IS_ERR(bcm
->rx_skb
)) {
304 int err
= PTR_ERR(bcm
->rx_skb
);
305 BT_ERR("%s: Frame reassembly failed (%d)", hu
->hdev
->name
, err
);
313 static int bcm_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
315 struct bcm_data
*bcm
= hu
->priv
;
317 BT_DBG("hu %p skb %p", hu
, skb
);
319 /* Prepend skb with frame type */
320 memcpy(skb_push(skb
, 1), &bt_cb(skb
)->pkt_type
, 1);
321 skb_queue_tail(&bcm
->txq
, skb
);
326 static struct sk_buff
*bcm_dequeue(struct hci_uart
*hu
)
328 struct bcm_data
*bcm
= hu
->priv
;
330 return skb_dequeue(&bcm
->txq
);
333 #ifdef CONFIG_PM_SLEEP
334 /* Platform suspend callback */
335 static int bcm_suspend(struct device
*dev
)
337 struct bcm_device
*bdev
= platform_get_drvdata(to_platform_device(dev
));
339 BT_DBG("suspend (%p): is_suspended %d", bdev
, bdev
->is_suspended
);
341 spin_lock(&bcm_device_lock
);
346 if (!bdev
->is_suspended
) {
347 hci_uart_set_flow_control(bdev
->hu
, true);
349 /* Once this callback returns, driver suspends BT via GPIO */
350 bdev
->is_suspended
= true;
353 /* Suspend the device */
354 if (bdev
->device_wakeup
) {
355 gpiod_set_value(bdev
->device_wakeup
, false);
356 BT_DBG("suspend, delaying 15 ms");
361 spin_unlock(&bcm_device_lock
);
366 /* Platform resume callback */
367 static int bcm_resume(struct device
*dev
)
369 struct bcm_device
*bdev
= platform_get_drvdata(to_platform_device(dev
));
371 BT_DBG("resume (%p): is_suspended %d", bdev
, bdev
->is_suspended
);
373 spin_lock(&bcm_device_lock
);
378 if (bdev
->device_wakeup
) {
379 gpiod_set_value(bdev
->device_wakeup
, true);
380 BT_DBG("resume, delaying 15 ms");
384 /* When this callback executes, the device has woken up already */
385 if (bdev
->is_suspended
) {
386 bdev
->is_suspended
= false;
388 hci_uart_set_flow_control(bdev
->hu
, false);
392 spin_unlock(&bcm_device_lock
);
398 static const struct acpi_gpio_params device_wakeup_gpios
= { 0, 0, false };
399 static const struct acpi_gpio_params shutdown_gpios
= { 1, 0, false };
401 static const struct acpi_gpio_mapping acpi_bcm_default_gpios
[] = {
402 { "device-wakeup-gpios", &device_wakeup_gpios
, 1 },
403 { "shutdown-gpios", &shutdown_gpios
, 1 },
408 static int bcm_resource(struct acpi_resource
*ares
, void *data
)
410 struct bcm_device
*dev
= data
;
412 if (ares
->type
== ACPI_RESOURCE_TYPE_SERIAL_BUS
) {
413 struct acpi_resource_uart_serialbus
*sb
;
415 sb
= &ares
->data
.uart_serial_bus
;
416 if (sb
->type
== ACPI_RESOURCE_SERIAL_TYPE_UART
)
417 dev
->init_speed
= sb
->default_baud_rate
;
420 /* Always tell the ACPI core to skip this resource */
424 static int bcm_acpi_probe(struct bcm_device
*dev
)
426 struct platform_device
*pdev
= dev
->pdev
;
427 const struct acpi_device_id
*id
;
428 struct acpi_device
*adev
;
429 LIST_HEAD(resources
);
432 id
= acpi_match_device(pdev
->dev
.driver
->acpi_match_table
, &pdev
->dev
);
436 /* Retrieve GPIO data */
437 dev
->name
= dev_name(&pdev
->dev
);
438 ret
= acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev
->dev
),
439 acpi_bcm_default_gpios
);
443 dev
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
445 dev
->device_wakeup
= devm_gpiod_get_optional(&pdev
->dev
,
448 if (IS_ERR(dev
->device_wakeup
))
449 return PTR_ERR(dev
->device_wakeup
);
451 dev
->shutdown
= devm_gpiod_get_optional(&pdev
->dev
, "shutdown",
453 if (IS_ERR(dev
->shutdown
))
454 return PTR_ERR(dev
->shutdown
);
456 /* Make sure at-least one of the GPIO is defined and that
457 * a name is specified for this instance
459 if ((!dev
->device_wakeup
&& !dev
->shutdown
) || !dev
->name
) {
460 dev_err(&pdev
->dev
, "invalid platform data\n");
464 /* Retrieve UART ACPI info */
465 adev
= ACPI_COMPANION(&dev
->pdev
->dev
);
469 acpi_dev_get_resources(adev
, &resources
, bcm_resource
, dev
);
474 static int bcm_acpi_probe(struct bcm_device
*dev
)
478 #endif /* CONFIG_ACPI */
480 static int bcm_probe(struct platform_device
*pdev
)
482 struct bcm_device
*dev
;
483 struct acpi_device_id
*pdata
= pdev
->dev
.platform_data
;
486 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
492 if (ACPI_HANDLE(&pdev
->dev
)) {
493 ret
= bcm_acpi_probe(dev
);
497 dev
->name
= pdata
->id
;
502 platform_set_drvdata(pdev
, dev
);
504 dev_info(&pdev
->dev
, "%s device registered.\n", dev
->name
);
506 /* Place this instance on the device list */
507 spin_lock(&bcm_device_lock
);
508 list_add_tail(&dev
->list
, &bcm_device_list
);
509 spin_unlock(&bcm_device_lock
);
511 bcm_gpio_set_power(dev
, false);
516 static int bcm_remove(struct platform_device
*pdev
)
518 struct bcm_device
*dev
= platform_get_drvdata(pdev
);
520 spin_lock(&bcm_device_lock
);
521 list_del(&dev
->list
);
522 spin_unlock(&bcm_device_lock
);
524 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev
->dev
));
526 dev_info(&pdev
->dev
, "%s device unregistered.\n", dev
->name
);
531 static const struct hci_uart_proto bcm_proto
= {
534 .init_speed
= 115200,
535 .oper_speed
= 4000000,
540 .set_baudrate
= bcm_set_baudrate
,
542 .enqueue
= bcm_enqueue
,
543 .dequeue
= bcm_dequeue
,
547 static const struct acpi_device_id bcm_acpi_match
[] = {
552 MODULE_DEVICE_TABLE(acpi
, bcm_acpi_match
);
555 /* Platform suspend and resume callbacks */
556 static SIMPLE_DEV_PM_OPS(bcm_pm_ops
, bcm_suspend
, bcm_resume
);
558 static struct platform_driver bcm_driver
= {
560 .remove
= bcm_remove
,
563 .acpi_match_table
= ACPI_PTR(bcm_acpi_match
),
568 int __init
bcm_init(void)
570 platform_driver_register(&bcm_driver
);
572 return hci_uart_register_proto(&bcm_proto
);
575 int __exit
bcm_deinit(void)
577 platform_driver_unregister(&bcm_driver
);
579 return hci_uart_unregister_proto(&bcm_proto
);