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>
34 #include <linux/interrupt.h>
35 #include <linux/dmi.h>
36 #include <linux/pm_runtime.h>
38 #include <net/bluetooth/bluetooth.h>
39 #include <net/bluetooth/hci_core.h>
44 #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
47 struct list_head list;
49 struct platform_device *pdev;
52 struct gpio_desc *device_wakeup;
53 struct gpio_desc *shutdown;
64 bool is_suspended; /* suspend/resume flag */
69 struct sk_buff *rx_skb;
70 struct sk_buff_head txq;
72 struct bcm_device *dev;
75 /* List of BCM BT UART devices */
76 static DEFINE_MUTEX(bcm_device_lock);
77 static LIST_HEAD(bcm_device_list);
79 static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
81 struct hci_dev *hdev = hu->hdev;
83 struct bcm_update_uart_baud_rate param;
85 if (speed > 3000000) {
86 struct bcm_write_uart_clock_setting clock;
88 clock.type = BCM_UART_CLOCK_48MHZ;
90 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
92 /* This Broadcom specific command changes the UART's controller
93 * clock for baud rate > 3000000.
95 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
97 int err = PTR_ERR(skb);
98 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
106 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
108 param.zero = cpu_to_le16(0);
109 param.baud_rate = cpu_to_le32(speed);
111 /* This Broadcom specific command changes the UART's controller baud
114 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), ¶m,
117 int err = PTR_ERR(skb);
118 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
128 /* bcm_device_exists should be protected by bcm_device_lock */
129 static bool bcm_device_exists(struct bcm_device *device)
133 list_for_each(p, &bcm_device_list) {
134 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
143 static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
145 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
146 clk_enable(dev->clk);
148 gpiod_set_value(dev->shutdown, powered);
149 gpiod_set_value(dev->device_wakeup, powered);
151 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
152 clk_disable(dev->clk);
154 dev->clk_enabled = powered;
160 static irqreturn_t bcm_host_wake(int irq, void *data)
162 struct bcm_device *bdev = data;
164 bt_dev_dbg(bdev, "Host wake IRQ");
166 pm_runtime_get(&bdev->pdev->dev);
167 pm_runtime_mark_last_busy(&bdev->pdev->dev);
168 pm_runtime_put_autosuspend(&bdev->pdev->dev);
173 static int bcm_request_irq(struct bcm_data *bcm)
175 struct bcm_device *bdev = bcm->dev;
178 /* If this is not a platform device, do not enable PM functionalities */
179 mutex_lock(&bcm_device_lock);
180 if (!bcm_device_exists(bdev)) {
186 err = devm_request_irq(&bdev->pdev->dev, bdev->irq,
187 bcm_host_wake, IRQF_TRIGGER_RISING,
192 device_init_wakeup(&bdev->pdev->dev, true);
194 pm_runtime_set_autosuspend_delay(&bdev->pdev->dev,
195 BCM_AUTOSUSPEND_DELAY);
196 pm_runtime_use_autosuspend(&bdev->pdev->dev);
197 pm_runtime_set_active(&bdev->pdev->dev);
198 pm_runtime_enable(&bdev->pdev->dev);
202 mutex_unlock(&bcm_device_lock);
207 static const struct bcm_set_sleep_mode default_sleep_params = {
208 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
209 .idle_host = 2, /* idle threshold HOST, in 300ms */
210 .idle_dev = 2, /* idle threshold device, in 300ms */
211 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
212 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
213 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
214 .combine_modes = 1, /* Combine sleep and LPM flag */
215 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
216 /* Irrelevant USB flags */
218 .usb_resume_timeout = 0,
219 .pulsed_host_wake = 0,
223 static int bcm_setup_sleep(struct hci_uart *hu)
225 struct bcm_data *bcm = hu->priv;
227 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
229 sleep_params.host_wake_active = !bcm->dev->irq_polarity;
231 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
232 &sleep_params, HCI_INIT_TIMEOUT);
234 int err = PTR_ERR(skb);
235 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
240 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
245 static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
246 static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
249 static int bcm_open(struct hci_uart *hu)
251 struct bcm_data *bcm;
254 bt_dev_dbg(hu->hdev, "hu %p", hu);
256 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
260 skb_queue_head_init(&bcm->txq);
264 mutex_lock(&bcm_device_lock);
265 list_for_each(p, &bcm_device_list) {
266 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
268 /* Retrieve saved bcm_device based on parent of the
269 * platform device (saved during device probe) and
270 * parent of tty device used by hci_uart
272 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
274 hu->init_speed = dev->init_speed;
278 bcm_gpio_set_power(bcm->dev, true);
283 mutex_unlock(&bcm_device_lock);
288 static int bcm_close(struct hci_uart *hu)
290 struct bcm_data *bcm = hu->priv;
291 struct bcm_device *bdev = bcm->dev;
293 bt_dev_dbg(hu->hdev, "hu %p", hu);
295 /* Protect bcm->dev against removal of the device or driver */
296 mutex_lock(&bcm_device_lock);
297 if (bcm_device_exists(bdev)) {
298 bcm_gpio_set_power(bdev, false);
300 pm_runtime_disable(&bdev->pdev->dev);
301 pm_runtime_set_suspended(&bdev->pdev->dev);
303 if (device_can_wakeup(&bdev->pdev->dev)) {
304 devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev);
305 device_init_wakeup(&bdev->pdev->dev, false);
311 mutex_unlock(&bcm_device_lock);
313 skb_queue_purge(&bcm->txq);
314 kfree_skb(bcm->rx_skb);
321 static int bcm_flush(struct hci_uart *hu)
323 struct bcm_data *bcm = hu->priv;
325 bt_dev_dbg(hu->hdev, "hu %p", hu);
327 skb_queue_purge(&bcm->txq);
332 static int bcm_setup(struct hci_uart *hu)
334 struct bcm_data *bcm = hu->priv;
336 const struct firmware *fw;
340 bt_dev_dbg(hu->hdev, "hu %p", hu);
342 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
344 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
348 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
350 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
354 err = btbcm_patchram(hu->hdev, fw);
356 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
360 /* Init speed if any */
362 speed = hu->init_speed;
363 else if (hu->proto->init_speed)
364 speed = hu->proto->init_speed;
369 hci_uart_set_baudrate(hu, speed);
371 /* Operational speed if any */
373 speed = hu->oper_speed;
374 else if (hu->proto->oper_speed)
375 speed = hu->proto->oper_speed;
380 err = bcm_set_baudrate(hu, speed);
382 hci_uart_set_baudrate(hu, speed);
386 release_firmware(fw);
388 err = btbcm_finalize(hu->hdev);
392 err = bcm_request_irq(bcm);
394 err = bcm_setup_sleep(hu);
399 static const struct h4_recv_pkt bcm_recv_pkts[] = {
400 { H4_RECV_ACL, .recv = hci_recv_frame },
401 { H4_RECV_SCO, .recv = hci_recv_frame },
402 { H4_RECV_EVENT, .recv = hci_recv_frame },
405 static int bcm_recv(struct hci_uart *hu, const void *data, int count)
407 struct bcm_data *bcm = hu->priv;
409 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
412 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
413 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
414 if (IS_ERR(bcm->rx_skb)) {
415 int err = PTR_ERR(bcm->rx_skb);
416 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
419 } else if (!bcm->rx_skb) {
420 /* Delay auto-suspend when receiving completed packet */
421 mutex_lock(&bcm_device_lock);
422 if (bcm->dev && bcm_device_exists(bcm->dev)) {
423 pm_runtime_get(&bcm->dev->pdev->dev);
424 pm_runtime_mark_last_busy(&bcm->dev->pdev->dev);
425 pm_runtime_put_autosuspend(&bcm->dev->pdev->dev);
427 mutex_unlock(&bcm_device_lock);
433 static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
435 struct bcm_data *bcm = hu->priv;
437 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
439 /* Prepend skb with frame type */
440 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
441 skb_queue_tail(&bcm->txq, skb);
446 static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
448 struct bcm_data *bcm = hu->priv;
449 struct sk_buff *skb = NULL;
450 struct bcm_device *bdev = NULL;
452 mutex_lock(&bcm_device_lock);
454 if (bcm_device_exists(bcm->dev)) {
456 pm_runtime_get_sync(&bdev->pdev->dev);
457 /* Shall be resumed here */
460 skb = skb_dequeue(&bcm->txq);
463 pm_runtime_mark_last_busy(&bdev->pdev->dev);
464 pm_runtime_put_autosuspend(&bdev->pdev->dev);
467 mutex_unlock(&bcm_device_lock);
473 static int bcm_suspend_device(struct device *dev)
475 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
477 bt_dev_dbg(bdev, "");
479 if (!bdev->is_suspended && bdev->hu) {
480 hci_uart_set_flow_control(bdev->hu, true);
482 /* Once this returns, driver suspends BT via GPIO */
483 bdev->is_suspended = true;
486 /* Suspend the device */
487 if (bdev->device_wakeup) {
488 gpiod_set_value(bdev->device_wakeup, false);
489 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
496 static int bcm_resume_device(struct device *dev)
498 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
500 bt_dev_dbg(bdev, "");
502 if (bdev->device_wakeup) {
503 gpiod_set_value(bdev->device_wakeup, true);
504 bt_dev_dbg(bdev, "resume, delaying 15 ms");
508 /* When this executes, the device has woken up already */
509 if (bdev->is_suspended && bdev->hu) {
510 bdev->is_suspended = false;
512 hci_uart_set_flow_control(bdev->hu, false);
519 #ifdef CONFIG_PM_SLEEP
520 /* Platform suspend callback */
521 static int bcm_suspend(struct device *dev)
523 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
526 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
528 /* bcm_suspend can be called at any time as long as platform device is
529 * bound, so it should use bcm_device_lock to protect access to hci_uart
530 * and device_wake-up GPIO.
532 mutex_lock(&bcm_device_lock);
537 if (pm_runtime_active(dev))
538 bcm_suspend_device(dev);
540 if (device_may_wakeup(&bdev->pdev->dev)) {
541 error = enable_irq_wake(bdev->irq);
543 bt_dev_dbg(bdev, "BCM irq: enabled");
547 mutex_unlock(&bcm_device_lock);
552 /* Platform resume callback */
553 static int bcm_resume(struct device *dev)
555 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
557 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
559 /* bcm_resume can be called at any time as long as platform device is
560 * bound, so it should use bcm_device_lock to protect access to hci_uart
561 * and device_wake-up GPIO.
563 mutex_lock(&bcm_device_lock);
568 if (device_may_wakeup(&bdev->pdev->dev)) {
569 disable_irq_wake(bdev->irq);
570 bt_dev_dbg(bdev, "BCM irq: disabled");
573 bcm_resume_device(dev);
576 mutex_unlock(&bcm_device_lock);
578 pm_runtime_disable(dev);
579 pm_runtime_set_active(dev);
580 pm_runtime_enable(dev);
586 static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
587 static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
588 static const struct acpi_gpio_params host_wakeup_gpios = { 2, 0, false };
590 static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
591 { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
592 { "shutdown-gpios", &shutdown_gpios, 1 },
593 { "host-wakeup-gpios", &host_wakeup_gpios, 1 },
598 static u8 acpi_active_low = ACPI_ACTIVE_LOW;
600 /* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
601 static const struct dmi_system_id bcm_wrong_irq_dmi_table[] = {
603 .ident = "Asus T100TA",
605 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
606 "ASUSTeK COMPUTER INC."),
607 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
609 .driver_data = &acpi_active_low,
614 static int bcm_resource(struct acpi_resource *ares, void *data)
616 struct bcm_device *dev = data;
617 struct acpi_resource_extended_irq *irq;
618 struct acpi_resource_gpio *gpio;
619 struct acpi_resource_uart_serialbus *sb;
621 switch (ares->type) {
622 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
623 irq = &ares->data.extended_irq;
624 dev->irq_polarity = irq->polarity;
627 case ACPI_RESOURCE_TYPE_GPIO:
628 gpio = &ares->data.gpio;
629 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
630 dev->irq_polarity = gpio->polarity;
633 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
634 sb = &ares->data.uart_serial_bus;
635 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
636 dev->init_speed = sb->default_baud_rate;
643 /* Always tell the ACPI core to skip this resource */
647 static int bcm_acpi_probe(struct bcm_device *dev)
649 struct platform_device *pdev = dev->pdev;
650 const struct acpi_device_id *id;
651 struct acpi_device *adev;
652 LIST_HEAD(resources);
653 const struct dmi_system_id *dmi_id;
656 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
660 /* Retrieve GPIO data */
661 dev->name = dev_name(&pdev->dev);
662 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
663 acpi_bcm_default_gpios);
667 dev->clk = devm_clk_get(&pdev->dev, NULL);
669 dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
672 if (IS_ERR(dev->device_wakeup))
673 return PTR_ERR(dev->device_wakeup);
675 dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
677 if (IS_ERR(dev->shutdown))
678 return PTR_ERR(dev->shutdown);
680 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
681 dev->irq = platform_get_irq(pdev, 0);
683 struct gpio_desc *gpio;
685 gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup",
688 return PTR_ERR(gpio);
690 dev->irq = gpiod_to_irq(gpio);
693 dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq);
695 /* Make sure at-least one of the GPIO is defined and that
696 * a name is specified for this instance
698 if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
699 dev_err(&pdev->dev, "invalid platform data\n");
703 /* Retrieve UART ACPI info */
704 adev = ACPI_COMPANION(&dev->pdev->dev);
708 acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
709 acpi_dev_free_resource_list(&resources);
711 dmi_id = dmi_first_match(bcm_wrong_irq_dmi_table);
713 bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low",
715 dev->irq_polarity = *(u8 *)dmi_id->driver_data;
721 static int bcm_acpi_probe(struct bcm_device *dev)
725 #endif /* CONFIG_ACPI */
727 static int bcm_probe(struct platform_device *pdev)
729 struct bcm_device *dev;
730 struct acpi_device_id *pdata = pdev->dev.platform_data;
733 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
739 if (ACPI_HANDLE(&pdev->dev)) {
740 ret = bcm_acpi_probe(dev);
744 dev->name = pdata->id;
749 platform_set_drvdata(pdev, dev);
751 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
753 /* Place this instance on the device list */
754 mutex_lock(&bcm_device_lock);
755 list_add_tail(&dev->list, &bcm_device_list);
756 mutex_unlock(&bcm_device_lock);
758 bcm_gpio_set_power(dev, false);
763 static int bcm_remove(struct platform_device *pdev)
765 struct bcm_device *dev = platform_get_drvdata(pdev);
767 mutex_lock(&bcm_device_lock);
768 list_del(&dev->list);
769 mutex_unlock(&bcm_device_lock);
771 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
773 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
778 static const struct hci_uart_proto bcm_proto = {
781 .init_speed = 115200,
782 .oper_speed = 4000000,
787 .set_baudrate = bcm_set_baudrate,
789 .enqueue = bcm_enqueue,
790 .dequeue = bcm_dequeue,
794 static const struct acpi_device_id bcm_acpi_match[] = {
799 MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
802 /* Platform suspend and resume callbacks */
803 static const struct dev_pm_ops bcm_pm_ops = {
804 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
805 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
808 static struct platform_driver bcm_driver = {
810 .remove = bcm_remove,
813 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
818 int __init bcm_init(void)
820 platform_driver_register(&bcm_driver);
822 return hci_uart_register_proto(&bcm_proto);
825 int __exit bcm_deinit(void)
827 platform_driver_unregister(&bcm_driver);
829 return hci_uart_unregister_proto(&bcm_proto);