blob: 59e5fc5eec8f878f5f02f5bcd7b41776b2ce039d [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 *
3 * Bluetooth HCI UART driver for Broadcom devices
4 *
5 * Copyright (C) 2015 Intel Corporation
6 *
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
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/of.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>
41
42#include <net/bluetooth/bluetooth.h>
43#include <net/bluetooth/hci_core.h>
44
45#include "btbcm.h"
46#include "hci_uart.h"
47
48#define BCM_NULL_PKT 0x00
49#define BCM_NULL_SIZE 0
50
51#define BCM_LM_DIAG_PKT 0x07
52#define BCM_LM_DIAG_SIZE 63
53
54#define BCM_TYPE49_PKT 0x31
55#define BCM_TYPE49_SIZE 0
56
57#define BCM_TYPE52_PKT 0x34
58#define BCM_TYPE52_SIZE 0
59
60#define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
61
62/**
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
92 */
93struct bcm_device {
94 /* Must be the first member, hci_serdev.c expects this. */
95 struct hci_uart serdev_hu;
96 struct list_head list;
97
98 struct device *dev;
99
100 const char *name;
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);
105#ifdef CONFIG_ACPI
106 acpi_handle btlp, btpu, btpd;
107 int gpio_count;
108 int gpio_int_idx;
109#endif
110
111 struct clk *clk;
112 bool clk_enabled;
113
114 u32 init_speed;
115 u32 oper_speed;
116 int irq;
117 bool irq_active_low;
118
119#ifdef CONFIG_PM
120 struct hci_uart *hu;
121 bool is_suspended;
122#endif
123};
124
125/* generic bcm uart resources */
126struct bcm_data {
127 struct sk_buff *rx_skb;
128 struct sk_buff_head txq;
129
130 struct bcm_device *dev;
131};
132
133/* List of BCM BT UART devices */
134static DEFINE_MUTEX(bcm_device_lock);
135static LIST_HEAD(bcm_device_list);
136
137static int irq_polarity = -1;
138module_param(irq_polarity, int, 0444);
139MODULE_PARM_DESC(irq_polarity, "IRQ polarity 0: active-high 1: active-low");
140
141static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
142{
143 if (hu->serdev)
144 serdev_device_set_baudrate(hu->serdev, speed);
145 else
146 hci_uart_set_baudrate(hu, speed);
147}
148
149static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
150{
151 struct hci_dev *hdev = hu->hdev;
152 struct sk_buff *skb;
153 struct bcm_update_uart_baud_rate param;
154
155 if (speed > 3000000) {
156 struct bcm_write_uart_clock_setting clock;
157
158 clock.type = BCM_UART_CLOCK_48MHZ;
159
160 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
161
162 /* This Broadcom specific command changes the UART's controller
163 * clock for baud rate > 3000000.
164 */
165 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
166 if (IS_ERR(skb)) {
167 int err = PTR_ERR(skb);
168 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
169 err);
170 return err;
171 }
172
173 kfree_skb(skb);
174 }
175
176 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
177
178 param.zero = cpu_to_le16(0);
179 param.baud_rate = cpu_to_le32(speed);
180
181 /* This Broadcom specific command changes the UART's controller baud
182 * rate.
183 */
184 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
185 HCI_INIT_TIMEOUT);
186 if (IS_ERR(skb)) {
187 int err = PTR_ERR(skb);
188 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
189 err);
190 return err;
191 }
192
193 kfree_skb(skb);
194
195 return 0;
196}
197
198/* bcm_device_exists should be protected by bcm_device_lock */
199static bool bcm_device_exists(struct bcm_device *device)
200{
201 struct list_head *p;
202
203#ifdef CONFIG_PM
204 /* Devices using serdev always exist */
205 if (device && device->hu && device->hu->serdev)
206 return true;
207#endif
208
209 list_for_each(p, &bcm_device_list) {
210 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
211
212 if (device == dev)
213 return true;
214 }
215
216 return false;
217}
218
219static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
220{
221 int err;
222
223 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled) {
224 err = clk_prepare_enable(dev->clk);
225 if (err)
226 return err;
227 }
228
229 err = dev->set_shutdown(dev, powered);
230 if (err)
231 goto err_clk_disable;
232
233 err = dev->set_device_wakeup(dev, powered);
234 if (err)
235 goto err_revert_shutdown;
236
237 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
238 clk_disable_unprepare(dev->clk);
239
240 dev->clk_enabled = powered;
241
242 return 0;
243
244err_revert_shutdown:
245 dev->set_shutdown(dev, !powered);
246err_clk_disable:
247 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
248 clk_disable_unprepare(dev->clk);
249 return err;
250}
251
252#ifdef CONFIG_PM
253static irqreturn_t bcm_host_wake(int irq, void *data)
254{
255 struct bcm_device *bdev = data;
256
257 bt_dev_dbg(bdev, "Host wake IRQ");
258
259 pm_runtime_get(bdev->dev);
260 pm_runtime_mark_last_busy(bdev->dev);
261 pm_runtime_put_autosuspend(bdev->dev);
262
263 return IRQ_HANDLED;
264}
265
266static int bcm_request_irq(struct bcm_data *bcm)
267{
268 struct bcm_device *bdev = bcm->dev;
269 int err;
270
271 mutex_lock(&bcm_device_lock);
272 if (!bcm_device_exists(bdev)) {
273 err = -ENODEV;
274 goto unlock;
275 }
276
277 if (bdev->irq <= 0) {
278 err = -EOPNOTSUPP;
279 goto unlock;
280 }
281
282 err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
283 bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
284 IRQF_TRIGGER_RISING,
285 "host_wake", bdev);
286 if (err) {
287 bdev->irq = err;
288 goto unlock;
289 }
290
291 device_init_wakeup(bdev->dev, true);
292
293 pm_runtime_set_autosuspend_delay(bdev->dev,
294 BCM_AUTOSUSPEND_DELAY);
295 pm_runtime_use_autosuspend(bdev->dev);
296 pm_runtime_set_active(bdev->dev);
297 pm_runtime_enable(bdev->dev);
298
299unlock:
300 mutex_unlock(&bcm_device_lock);
301
302 return err;
303}
304
305static const struct bcm_set_sleep_mode default_sleep_params = {
306 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
307 .idle_host = 2, /* idle threshold HOST, in 300ms */
308 .idle_dev = 2, /* idle threshold device, in 300ms */
309 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
310 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
311 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
312 .combine_modes = 1, /* Combine sleep and LPM flag */
313 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
314 /* Irrelevant USB flags */
315 .usb_auto_sleep = 0,
316 .usb_resume_timeout = 0,
317 .break_to_host = 0,
318 .pulsed_host_wake = 1,
319};
320
321static int bcm_setup_sleep(struct hci_uart *hu)
322{
323 struct bcm_data *bcm = hu->priv;
324 struct sk_buff *skb;
325 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
326
327 sleep_params.host_wake_active = !bcm->dev->irq_active_low;
328
329 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
330 &sleep_params, HCI_INIT_TIMEOUT);
331 if (IS_ERR(skb)) {
332 int err = PTR_ERR(skb);
333 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
334 return err;
335 }
336 kfree_skb(skb);
337
338 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
339
340 return 0;
341}
342#else
343static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
344static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
345#endif
346
347static int bcm_set_diag(struct hci_dev *hdev, bool enable)
348{
349 struct hci_uart *hu = hci_get_drvdata(hdev);
350 struct bcm_data *bcm = hu->priv;
351 struct sk_buff *skb;
352
353 if (!test_bit(HCI_RUNNING, &hdev->flags))
354 return -ENETDOWN;
355
356 skb = bt_skb_alloc(3, GFP_KERNEL);
357 if (!skb)
358 return -ENOMEM;
359
360 skb_put_u8(skb, BCM_LM_DIAG_PKT);
361 skb_put_u8(skb, 0xf0);
362 skb_put_u8(skb, enable);
363
364 skb_queue_tail(&bcm->txq, skb);
365 hci_uart_tx_wakeup(hu);
366
367 return 0;
368}
369
370static int bcm_open(struct hci_uart *hu)
371{
372 struct bcm_data *bcm;
373 struct list_head *p;
374 int err;
375
376 bt_dev_dbg(hu->hdev, "hu %p", hu);
377
378 if (!hci_uart_has_flow_control(hu))
379 return -EOPNOTSUPP;
380
381 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
382 if (!bcm)
383 return -ENOMEM;
384
385 skb_queue_head_init(&bcm->txq);
386
387 hu->priv = bcm;
388
389 mutex_lock(&bcm_device_lock);
390
391 if (hu->serdev) {
392 bcm->dev = serdev_device_get_drvdata(hu->serdev);
393 goto out;
394 }
395
396 if (!hu->tty->dev)
397 goto out;
398
399 list_for_each(p, &bcm_device_list) {
400 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
401
402 /* Retrieve saved bcm_device based on parent of the
403 * platform device (saved during device probe) and
404 * parent of tty device used by hci_uart
405 */
406 if (hu->tty->dev->parent == dev->dev->parent) {
407 bcm->dev = dev;
408#ifdef CONFIG_PM
409 dev->hu = hu;
410#endif
411 break;
412 }
413 }
414
415out:
416 if (bcm->dev) {
417 hu->init_speed = bcm->dev->init_speed;
418 hu->oper_speed = bcm->dev->oper_speed;
419 err = bcm_gpio_set_power(bcm->dev, true);
420 if (err)
421 goto err_unset_hu;
422 }
423
424 mutex_unlock(&bcm_device_lock);
425 return 0;
426
427err_unset_hu:
428#ifdef CONFIG_PM
429 if (!hu->serdev)
430 bcm->dev->hu = NULL;
431#endif
432 mutex_unlock(&bcm_device_lock);
433 hu->priv = NULL;
434 kfree(bcm);
435 return err;
436}
437
438static int bcm_close(struct hci_uart *hu)
439{
440 struct bcm_data *bcm = hu->priv;
441 struct bcm_device *bdev = NULL;
442 int err;
443
444 bt_dev_dbg(hu->hdev, "hu %p", hu);
445
446 /* Protect bcm->dev against removal of the device or driver */
447 mutex_lock(&bcm_device_lock);
448
449 if (hu->serdev) {
450 bdev = serdev_device_get_drvdata(hu->serdev);
451 } else if (bcm_device_exists(bcm->dev)) {
452 bdev = bcm->dev;
453#ifdef CONFIG_PM
454 bdev->hu = NULL;
455#endif
456 }
457
458 if (bdev) {
459 if (IS_ENABLED(CONFIG_PM) && bdev->irq > 0) {
460 devm_free_irq(bdev->dev, bdev->irq, bdev);
461 device_init_wakeup(bdev->dev, false);
462 pm_runtime_disable(bdev->dev);
463 }
464
465 err = bcm_gpio_set_power(bdev, false);
466 if (err)
467 bt_dev_err(hu->hdev, "Failed to power down");
468 else
469 pm_runtime_set_suspended(bdev->dev);
470 }
471 mutex_unlock(&bcm_device_lock);
472
473 skb_queue_purge(&bcm->txq);
474 kfree_skb(bcm->rx_skb);
475 kfree(bcm);
476
477 hu->priv = NULL;
478 return 0;
479}
480
481static int bcm_flush(struct hci_uart *hu)
482{
483 struct bcm_data *bcm = hu->priv;
484
485 bt_dev_dbg(hu->hdev, "hu %p", hu);
486
487 skb_queue_purge(&bcm->txq);
488
489 return 0;
490}
491
492static int bcm_setup(struct hci_uart *hu)
493{
494 struct bcm_data *bcm = hu->priv;
495 char fw_name[64];
496 const struct firmware *fw;
497 unsigned int speed;
498 int err;
499
500 bt_dev_dbg(hu->hdev, "hu %p", hu);
501
502 hu->hdev->set_diag = bcm_set_diag;
503 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
504
505 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name), false);
506 if (err)
507 return err;
508
509 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
510 if (err < 0) {
511 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
512 return 0;
513 }
514
515 err = btbcm_patchram(hu->hdev, fw);
516 if (err) {
517 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
518 goto finalize;
519 }
520
521 /* Init speed if any */
522 if (hu->init_speed)
523 speed = hu->init_speed;
524 else if (hu->proto->init_speed)
525 speed = hu->proto->init_speed;
526 else
527 speed = 0;
528
529 if (speed)
530 host_set_baudrate(hu, speed);
531
532 /* Operational speed if any */
533 if (hu->oper_speed)
534 speed = hu->oper_speed;
535 else if (hu->proto->oper_speed)
536 speed = hu->proto->oper_speed;
537 else
538 speed = 0;
539
540 if (speed) {
541 err = bcm_set_baudrate(hu, speed);
542 if (!err)
543 host_set_baudrate(hu, speed);
544 }
545
546finalize:
547 release_firmware(fw);
548
549 err = btbcm_finalize(hu->hdev);
550 if (err)
551 return err;
552
553 if (!bcm_request_irq(bcm))
554 err = bcm_setup_sleep(hu);
555
556 return err;
557}
558
559#define BCM_RECV_LM_DIAG \
560 .type = BCM_LM_DIAG_PKT, \
561 .hlen = BCM_LM_DIAG_SIZE, \
562 .loff = 0, \
563 .lsize = 0, \
564 .maxlen = BCM_LM_DIAG_SIZE
565
566#define BCM_RECV_NULL \
567 .type = BCM_NULL_PKT, \
568 .hlen = BCM_NULL_SIZE, \
569 .loff = 0, \
570 .lsize = 0, \
571 .maxlen = BCM_NULL_SIZE
572
573#define BCM_RECV_TYPE49 \
574 .type = BCM_TYPE49_PKT, \
575 .hlen = BCM_TYPE49_SIZE, \
576 .loff = 0, \
577 .lsize = 0, \
578 .maxlen = BCM_TYPE49_SIZE
579
580#define BCM_RECV_TYPE52 \
581 .type = BCM_TYPE52_PKT, \
582 .hlen = BCM_TYPE52_SIZE, \
583 .loff = 0, \
584 .lsize = 0, \
585 .maxlen = BCM_TYPE52_SIZE
586
587static const struct h4_recv_pkt bcm_recv_pkts[] = {
588 { H4_RECV_ACL, .recv = hci_recv_frame },
589 { H4_RECV_SCO, .recv = hci_recv_frame },
590 { H4_RECV_EVENT, .recv = hci_recv_frame },
591 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
592 { BCM_RECV_NULL, .recv = hci_recv_diag },
593 { BCM_RECV_TYPE49, .recv = hci_recv_diag },
594 { BCM_RECV_TYPE52, .recv = hci_recv_diag },
595};
596
597static int bcm_recv(struct hci_uart *hu, const void *data, int count)
598{
599 struct bcm_data *bcm = hu->priv;
600
601 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
602 return -EUNATCH;
603
604 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
605 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
606 if (IS_ERR(bcm->rx_skb)) {
607 int err = PTR_ERR(bcm->rx_skb);
608 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
609 bcm->rx_skb = NULL;
610 return err;
611 } else if (!bcm->rx_skb) {
612 /* Delay auto-suspend when receiving completed packet */
613 mutex_lock(&bcm_device_lock);
614 if (bcm->dev && bcm_device_exists(bcm->dev)) {
615 pm_runtime_get(bcm->dev->dev);
616 pm_runtime_mark_last_busy(bcm->dev->dev);
617 pm_runtime_put_autosuspend(bcm->dev->dev);
618 }
619 mutex_unlock(&bcm_device_lock);
620 }
621
622 return count;
623}
624
625static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
626{
627 struct bcm_data *bcm = hu->priv;
628
629 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
630
631 /* Prepend skb with frame type */
632 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
633 skb_queue_tail(&bcm->txq, skb);
634
635 return 0;
636}
637
638static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
639{
640 struct bcm_data *bcm = hu->priv;
641 struct sk_buff *skb = NULL;
642 struct bcm_device *bdev = NULL;
643
644 mutex_lock(&bcm_device_lock);
645
646 if (bcm_device_exists(bcm->dev)) {
647 bdev = bcm->dev;
648 pm_runtime_get_sync(bdev->dev);
649 /* Shall be resumed here */
650 }
651
652 skb = skb_dequeue(&bcm->txq);
653
654 if (bdev) {
655 pm_runtime_mark_last_busy(bdev->dev);
656 pm_runtime_put_autosuspend(bdev->dev);
657 }
658
659 mutex_unlock(&bcm_device_lock);
660
661 return skb;
662}
663
664#ifdef CONFIG_PM
665static int bcm_suspend_device(struct device *dev)
666{
667 struct bcm_device *bdev = dev_get_drvdata(dev);
668 int err;
669
670 bt_dev_dbg(bdev, "");
671
672 if (!bdev->is_suspended && bdev->hu) {
673 hci_uart_set_flow_control(bdev->hu, true);
674
675 /* Once this returns, driver suspends BT via GPIO */
676 bdev->is_suspended = true;
677 }
678
679 /* Suspend the device */
680 err = bdev->set_device_wakeup(bdev, false);
681 if (err) {
682 if (bdev->is_suspended && bdev->hu) {
683 bdev->is_suspended = false;
684 hci_uart_set_flow_control(bdev->hu, false);
685 }
686 return -EBUSY;
687 }
688
689 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
690 msleep(15);
691
692 return 0;
693}
694
695static int bcm_resume_device(struct device *dev)
696{
697 struct bcm_device *bdev = dev_get_drvdata(dev);
698 int err;
699
700 bt_dev_dbg(bdev, "");
701
702 err = bdev->set_device_wakeup(bdev, true);
703 if (err) {
704 dev_err(dev, "Failed to power up\n");
705 return err;
706 }
707
708 bt_dev_dbg(bdev, "resume, delaying 15 ms");
709 msleep(15);
710
711 /* When this executes, the device has woken up already */
712 if (bdev->is_suspended && bdev->hu) {
713 bdev->is_suspended = false;
714
715 hci_uart_set_flow_control(bdev->hu, false);
716 }
717
718 return 0;
719}
720#endif
721
722#ifdef CONFIG_PM_SLEEP
723/* suspend callback */
724static int bcm_suspend(struct device *dev)
725{
726 struct bcm_device *bdev = dev_get_drvdata(dev);
727 int error;
728
729 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
730
731 /*
732 * When used with a device instantiated as platform_device, bcm_suspend
733 * can be called at any time as long as the platform device is bound,
734 * so it should use bcm_device_lock to protect access to hci_uart
735 * and device_wake-up GPIO.
736 */
737 mutex_lock(&bcm_device_lock);
738
739 if (!bdev->hu)
740 goto unlock;
741
742 if (pm_runtime_active(dev))
743 bcm_suspend_device(dev);
744
745 if (device_may_wakeup(dev) && bdev->irq > 0) {
746 error = enable_irq_wake(bdev->irq);
747 if (!error)
748 bt_dev_dbg(bdev, "BCM irq: enabled");
749 }
750
751unlock:
752 mutex_unlock(&bcm_device_lock);
753
754 return 0;
755}
756
757/* resume callback */
758static int bcm_resume(struct device *dev)
759{
760 struct bcm_device *bdev = dev_get_drvdata(dev);
761 int err = 0;
762
763 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
764
765 /*
766 * When used with a device instantiated as platform_device, bcm_resume
767 * can be called at any time as long as platform device is bound,
768 * so it should use bcm_device_lock to protect access to hci_uart
769 * and device_wake-up GPIO.
770 */
771 mutex_lock(&bcm_device_lock);
772
773 if (!bdev->hu)
774 goto unlock;
775
776 if (device_may_wakeup(dev) && bdev->irq > 0) {
777 disable_irq_wake(bdev->irq);
778 bt_dev_dbg(bdev, "BCM irq: disabled");
779 }
780
781 err = bcm_resume_device(dev);
782
783unlock:
784 mutex_unlock(&bcm_device_lock);
785
786 if (!err) {
787 pm_runtime_disable(dev);
788 pm_runtime_set_active(dev);
789 pm_runtime_enable(dev);
790 }
791
792 return 0;
793}
794#endif
795
796static const struct acpi_gpio_params first_gpio = { 0, 0, false };
797static const struct acpi_gpio_params second_gpio = { 1, 0, false };
798static const struct acpi_gpio_params third_gpio = { 2, 0, false };
799
800static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
801 { "device-wakeup-gpios", &first_gpio, 1 },
802 { "shutdown-gpios", &second_gpio, 1 },
803 { "host-wakeup-gpios", &third_gpio, 1 },
804 { },
805};
806
807static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
808 { "host-wakeup-gpios", &first_gpio, 1 },
809 { "device-wakeup-gpios", &second_gpio, 1 },
810 { "shutdown-gpios", &third_gpio, 1 },
811 { },
812};
813
814/* Some firmware reports an IRQ which does not work (wrong pin in fw table?) */
815static const struct dmi_system_id bcm_broken_irq_dmi_table[] = {
816 {
817 .ident = "Meegopad T08",
818 .matches = {
819 DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
820 "To be filled by OEM."),
821 DMI_EXACT_MATCH(DMI_BOARD_NAME, "T3 MRD"),
822 DMI_EXACT_MATCH(DMI_BOARD_VERSION, "V1.1"),
823 },
824 },
825 { }
826};
827
828#ifdef CONFIG_ACPI
829static int bcm_resource(struct acpi_resource *ares, void *data)
830{
831 struct bcm_device *dev = data;
832 struct acpi_resource_extended_irq *irq;
833 struct acpi_resource_gpio *gpio;
834 struct acpi_resource_uart_serialbus *sb;
835
836 switch (ares->type) {
837 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
838 irq = &ares->data.extended_irq;
839 if (irq->polarity != ACPI_ACTIVE_LOW)
840 dev_info(dev->dev, "ACPI Interrupt resource is active-high, this is usually wrong, treating the IRQ as active-low\n");
841 dev->irq_active_low = true;
842 break;
843
844 case ACPI_RESOURCE_TYPE_GPIO:
845 gpio = &ares->data.gpio;
846 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
847 dev->gpio_int_idx = dev->gpio_count;
848 dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
849 }
850 dev->gpio_count++;
851 break;
852
853 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
854 sb = &ares->data.uart_serial_bus;
855 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
856 dev->init_speed = sb->default_baud_rate;
857 dev->oper_speed = 4000000;
858 }
859 break;
860
861 default:
862 break;
863 }
864
865 return 0;
866}
867
868static int bcm_apple_set_device_wakeup(struct bcm_device *dev, bool awake)
869{
870 if (ACPI_FAILURE(acpi_execute_simple_method(dev->btlp, NULL, !awake)))
871 return -EIO;
872
873 return 0;
874}
875
876static int bcm_apple_set_shutdown(struct bcm_device *dev, bool powered)
877{
878 if (ACPI_FAILURE(acpi_evaluate_object(powered ? dev->btpu : dev->btpd,
879 NULL, NULL, NULL)))
880 return -EIO;
881
882 return 0;
883}
884
885static int bcm_apple_get_resources(struct bcm_device *dev)
886{
887 struct acpi_device *adev = ACPI_COMPANION(dev->dev);
888 const union acpi_object *obj;
889
890 if (!adev ||
891 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTLP", &dev->btlp)) ||
892 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPU", &dev->btpu)) ||
893 ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPD", &dev->btpd)))
894 return -ENODEV;
895
896 if (!acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, &obj) &&
897 obj->buffer.length == 8)
898 dev->init_speed = *(u64 *)obj->buffer.pointer;
899
900 dev->set_device_wakeup = bcm_apple_set_device_wakeup;
901 dev->set_shutdown = bcm_apple_set_shutdown;
902
903 return 0;
904}
905#else
906static inline int bcm_apple_get_resources(struct bcm_device *dev)
907{
908 return -EOPNOTSUPP;
909}
910#endif /* CONFIG_ACPI */
911
912static int bcm_gpio_set_device_wakeup(struct bcm_device *dev, bool awake)
913{
914 gpiod_set_value_cansleep(dev->device_wakeup, awake);
915 return 0;
916}
917
918static int bcm_gpio_set_shutdown(struct bcm_device *dev, bool powered)
919{
920 gpiod_set_value_cansleep(dev->shutdown, powered);
921 return 0;
922}
923
924static int bcm_get_resources(struct bcm_device *dev)
925{
926 const struct dmi_system_id *dmi_id;
927
928 dev->name = dev_name(dev->dev);
929
930 if (x86_apple_machine && !bcm_apple_get_resources(dev))
931 return 0;
932
933 dev->clk = devm_clk_get(dev->dev, NULL);
934
935 /* Handle deferred probing */
936 if (dev->clk == ERR_PTR(-EPROBE_DEFER))
937 return PTR_ERR(dev->clk);
938
939 dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup",
940 GPIOD_OUT_LOW);
941 if (IS_ERR(dev->device_wakeup))
942 return PTR_ERR(dev->device_wakeup);
943
944 dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown",
945 GPIOD_OUT_LOW);
946 if (IS_ERR(dev->shutdown))
947 return PTR_ERR(dev->shutdown);
948
949 dev->set_device_wakeup = bcm_gpio_set_device_wakeup;
950 dev->set_shutdown = bcm_gpio_set_shutdown;
951
952 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
953 if (dev->irq <= 0) {
954 struct gpio_desc *gpio;
955
956 gpio = devm_gpiod_get_optional(dev->dev, "host-wakeup",
957 GPIOD_IN);
958 if (IS_ERR(gpio))
959 return PTR_ERR(gpio);
960
961 dev->irq = gpiod_to_irq(gpio);
962 }
963
964 dmi_id = dmi_first_match(bcm_broken_irq_dmi_table);
965 if (dmi_id) {
966 dev_info(dev->dev, "%s: Has a broken IRQ config, disabling IRQ support / runtime-pm\n",
967 dmi_id->ident);
968 dev->irq = 0;
969 }
970
971 dev_dbg(dev->dev, "BCM irq: %d\n", dev->irq);
972 return 0;
973}
974
975#ifdef CONFIG_ACPI
976static int bcm_acpi_probe(struct bcm_device *dev)
977{
978 LIST_HEAD(resources);
979 const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
980 struct resource_entry *entry;
981 int ret;
982
983 /* Retrieve UART ACPI info */
984 dev->gpio_int_idx = -1;
985 ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
986 &resources, bcm_resource, dev);
987 if (ret < 0)
988 return ret;
989
990 resource_list_for_each_entry(entry, &resources) {
991 if (resource_type(entry->res) == IORESOURCE_IRQ) {
992 dev->irq = entry->res->start;
993 break;
994 }
995 }
996 acpi_dev_free_resource_list(&resources);
997
998 /* If the DSDT uses an Interrupt resource for the IRQ, then there are
999 * only 2 GPIO resources, we use the irq-last mapping for this, since
1000 * we already have an irq the 3th / last mapping will not be used.
1001 */
1002 if (dev->irq)
1003 gpio_mapping = acpi_bcm_int_last_gpios;
1004 else if (dev->gpio_int_idx == 0)
1005 gpio_mapping = acpi_bcm_int_first_gpios;
1006 else if (dev->gpio_int_idx == 2)
1007 gpio_mapping = acpi_bcm_int_last_gpios;
1008 else
1009 dev_warn(dev->dev, "Unexpected ACPI gpio_int_idx: %d\n",
1010 dev->gpio_int_idx);
1011
1012 /* Warn if our expectations are not met. */
1013 if (dev->gpio_count != (dev->irq ? 2 : 3))
1014 dev_warn(dev->dev, "Unexpected number of ACPI GPIOs: %d\n",
1015 dev->gpio_count);
1016
1017 ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
1018 if (ret)
1019 return ret;
1020
1021 if (irq_polarity != -1) {
1022 dev->irq_active_low = irq_polarity;
1023 dev_warn(dev->dev, "Overwriting IRQ polarity to active %s by module-param\n",
1024 dev->irq_active_low ? "low" : "high");
1025 }
1026
1027 return 0;
1028}
1029#else
1030static int bcm_acpi_probe(struct bcm_device *dev)
1031{
1032 return -EINVAL;
1033}
1034#endif /* CONFIG_ACPI */
1035
1036static int bcm_of_probe(struct bcm_device *bdev)
1037{
1038 device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
1039 return 0;
1040}
1041
1042static int bcm_probe(struct platform_device *pdev)
1043{
1044 struct bcm_device *dev;
1045 int ret;
1046
1047 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1048 if (!dev)
1049 return -ENOMEM;
1050
1051 dev->dev = &pdev->dev;
1052 dev->irq = platform_get_irq(pdev, 0);
1053
1054 if (has_acpi_companion(&pdev->dev)) {
1055 ret = bcm_acpi_probe(dev);
1056 if (ret)
1057 return ret;
1058 }
1059
1060 ret = bcm_get_resources(dev);
1061 if (ret)
1062 return ret;
1063
1064 platform_set_drvdata(pdev, dev);
1065
1066 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
1067
1068 /* Place this instance on the device list */
1069 mutex_lock(&bcm_device_lock);
1070 list_add_tail(&dev->list, &bcm_device_list);
1071 mutex_unlock(&bcm_device_lock);
1072
1073 ret = bcm_gpio_set_power(dev, false);
1074 if (ret)
1075 dev_err(&pdev->dev, "Failed to power down\n");
1076
1077 return 0;
1078}
1079
1080static int bcm_remove(struct platform_device *pdev)
1081{
1082 struct bcm_device *dev = platform_get_drvdata(pdev);
1083
1084 mutex_lock(&bcm_device_lock);
1085 list_del(&dev->list);
1086 mutex_unlock(&bcm_device_lock);
1087
1088 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
1089
1090 return 0;
1091}
1092
1093static const struct hci_uart_proto bcm_proto = {
1094 .id = HCI_UART_BCM,
1095 .name = "Broadcom",
1096 .manufacturer = 15,
1097 .init_speed = 115200,
1098 .open = bcm_open,
1099 .close = bcm_close,
1100 .flush = bcm_flush,
1101 .setup = bcm_setup,
1102 .set_baudrate = bcm_set_baudrate,
1103 .recv = bcm_recv,
1104 .enqueue = bcm_enqueue,
1105 .dequeue = bcm_dequeue,
1106};
1107
1108#ifdef CONFIG_ACPI
1109static const struct acpi_device_id bcm_acpi_match[] = {
1110 { "BCM2E00" },
1111 { "BCM2E01" },
1112 { "BCM2E02" },
1113 { "BCM2E03" },
1114 { "BCM2E04" },
1115 { "BCM2E05" },
1116 { "BCM2E06" },
1117 { "BCM2E07" },
1118 { "BCM2E08" },
1119 { "BCM2E09" },
1120 { "BCM2E0A" },
1121 { "BCM2E0B" },
1122 { "BCM2E0C" },
1123 { "BCM2E0D" },
1124 { "BCM2E0E" },
1125 { "BCM2E0F" },
1126 { "BCM2E10" },
1127 { "BCM2E11" },
1128 { "BCM2E12" },
1129 { "BCM2E13" },
1130 { "BCM2E14" },
1131 { "BCM2E15" },
1132 { "BCM2E16" },
1133 { "BCM2E17" },
1134 { "BCM2E18" },
1135 { "BCM2E19" },
1136 { "BCM2E1A" },
1137 { "BCM2E1B" },
1138 { "BCM2E1C" },
1139 { "BCM2E1D" },
1140 { "BCM2E1F" },
1141 { "BCM2E20" },
1142 { "BCM2E21" },
1143 { "BCM2E22" },
1144 { "BCM2E23" },
1145 { "BCM2E24" },
1146 { "BCM2E25" },
1147 { "BCM2E26" },
1148 { "BCM2E27" },
1149 { "BCM2E28" },
1150 { "BCM2E29" },
1151 { "BCM2E2A" },
1152 { "BCM2E2B" },
1153 { "BCM2E2C" },
1154 { "BCM2E2D" },
1155 { "BCM2E2E" },
1156 { "BCM2E2F" },
1157 { "BCM2E30" },
1158 { "BCM2E31" },
1159 { "BCM2E32" },
1160 { "BCM2E33" },
1161 { "BCM2E34" },
1162 { "BCM2E35" },
1163 { "BCM2E36" },
1164 { "BCM2E37" },
1165 { "BCM2E38" },
1166 { "BCM2E39" },
1167 { "BCM2E3A" },
1168 { "BCM2E3B" },
1169 { "BCM2E3C" },
1170 { "BCM2E3D" },
1171 { "BCM2E3E" },
1172 { "BCM2E3F" },
1173 { "BCM2E40" },
1174 { "BCM2E41" },
1175 { "BCM2E42" },
1176 { "BCM2E43" },
1177 { "BCM2E44" },
1178 { "BCM2E45" },
1179 { "BCM2E46" },
1180 { "BCM2E47" },
1181 { "BCM2E48" },
1182 { "BCM2E49" },
1183 { "BCM2E4A" },
1184 { "BCM2E4B" },
1185 { "BCM2E4C" },
1186 { "BCM2E4D" },
1187 { "BCM2E4E" },
1188 { "BCM2E4F" },
1189 { "BCM2E50" },
1190 { "BCM2E51" },
1191 { "BCM2E52" },
1192 { "BCM2E53" },
1193 { "BCM2E54" },
1194 { "BCM2E55" },
1195 { "BCM2E56" },
1196 { "BCM2E57" },
1197 { "BCM2E58" },
1198 { "BCM2E59" },
1199 { "BCM2E5A" },
1200 { "BCM2E5B" },
1201 { "BCM2E5C" },
1202 { "BCM2E5D" },
1203 { "BCM2E5E" },
1204 { "BCM2E5F" },
1205 { "BCM2E60" },
1206 { "BCM2E61" },
1207 { "BCM2E62" },
1208 { "BCM2E63" },
1209 { "BCM2E64" },
1210 { "BCM2E65" },
1211 { "BCM2E66" },
1212 { "BCM2E67" },
1213 { "BCM2E68" },
1214 { "BCM2E69" },
1215 { "BCM2E6B" },
1216 { "BCM2E6D" },
1217 { "BCM2E6E" },
1218 { "BCM2E6F" },
1219 { "BCM2E70" },
1220 { "BCM2E71" },
1221 { "BCM2E72" },
1222 { "BCM2E73" },
1223 { "BCM2E74" },
1224 { "BCM2E75" },
1225 { "BCM2E76" },
1226 { "BCM2E77" },
1227 { "BCM2E78" },
1228 { "BCM2E79" },
1229 { "BCM2E7A" },
1230 { "BCM2E7B" },
1231 { "BCM2E7C" },
1232 { "BCM2E7D" },
1233 { "BCM2E7E" },
1234 { "BCM2E7F" },
1235 { "BCM2E80" },
1236 { "BCM2E81" },
1237 { "BCM2E82" },
1238 { "BCM2E83" },
1239 { "BCM2E84" },
1240 { "BCM2E85" },
1241 { "BCM2E86" },
1242 { "BCM2E87" },
1243 { "BCM2E88" },
1244 { "BCM2E89" },
1245 { "BCM2E8A" },
1246 { "BCM2E8B" },
1247 { "BCM2E8C" },
1248 { "BCM2E8D" },
1249 { "BCM2E8E" },
1250 { "BCM2E90" },
1251 { "BCM2E92" },
1252 { "BCM2E93" },
1253 { "BCM2E94" },
1254 { "BCM2E95" },
1255 { "BCM2E96" },
1256 { "BCM2E97" },
1257 { "BCM2E98" },
1258 { "BCM2E99" },
1259 { "BCM2E9A" },
1260 { "BCM2E9B" },
1261 { "BCM2E9C" },
1262 { "BCM2E9D" },
1263 { "BCM2EA0" },
1264 { "BCM2EA1" },
1265 { "BCM2EA2" },
1266 { "BCM2EA3" },
1267 { "BCM2EA4" },
1268 { "BCM2EA5" },
1269 { "BCM2EA6" },
1270 { "BCM2EA7" },
1271 { "BCM2EA8" },
1272 { "BCM2EA9" },
1273 { "BCM2EAA" },
1274 { "BCM2EAB" },
1275 { "BCM2EAC" },
1276 { },
1277};
1278MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
1279#endif
1280
1281/* suspend and resume callbacks */
1282static const struct dev_pm_ops bcm_pm_ops = {
1283 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
1284 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
1285};
1286
1287static struct platform_driver bcm_driver = {
1288 .probe = bcm_probe,
1289 .remove = bcm_remove,
1290 .driver = {
1291 .name = "hci_bcm",
1292 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1293 .pm = &bcm_pm_ops,
1294 },
1295};
1296
1297static int bcm_serdev_probe(struct serdev_device *serdev)
1298{
1299 struct bcm_device *bcmdev;
1300 int err;
1301
1302 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
1303 if (!bcmdev)
1304 return -ENOMEM;
1305
1306 bcmdev->dev = &serdev->dev;
1307#ifdef CONFIG_PM
1308 bcmdev->hu = &bcmdev->serdev_hu;
1309#endif
1310 bcmdev->serdev_hu.serdev = serdev;
1311 serdev_device_set_drvdata(serdev, bcmdev);
1312
1313 if (has_acpi_companion(&serdev->dev))
1314 err = bcm_acpi_probe(bcmdev);
1315 else
1316 err = bcm_of_probe(bcmdev);
1317 if (err)
1318 return err;
1319
1320 err = bcm_get_resources(bcmdev);
1321 if (err)
1322 return err;
1323
1324 if (!bcmdev->shutdown) {
1325 dev_warn(&serdev->dev,
1326 "No reset resource, using default baud rate\n");
1327 bcmdev->oper_speed = bcmdev->init_speed;
1328 }
1329
1330 err = bcm_gpio_set_power(bcmdev, false);
1331 if (err)
1332 dev_err(&serdev->dev, "Failed to power down\n");
1333
1334 return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
1335}
1336
1337static void bcm_serdev_remove(struct serdev_device *serdev)
1338{
1339 struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
1340
1341 hci_uart_unregister_device(&bcmdev->serdev_hu);
1342}
1343
1344#ifdef CONFIG_OF
1345static const struct of_device_id bcm_bluetooth_of_match[] = {
1346 { .compatible = "brcm,bcm43438-bt" },
1347 { },
1348};
1349MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
1350#endif
1351
1352static struct serdev_device_driver bcm_serdev_driver = {
1353 .probe = bcm_serdev_probe,
1354 .remove = bcm_serdev_remove,
1355 .driver = {
1356 .name = "hci_uart_bcm",
1357 .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
1358 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1359 .pm = &bcm_pm_ops,
1360 },
1361};
1362
1363int __init bcm_init(void)
1364{
1365 /* For now, we need to keep both platform device
1366 * driver (ACPI generated) and serdev driver (DT).
1367 */
1368 platform_driver_register(&bcm_driver);
1369 serdev_device_driver_register(&bcm_serdev_driver);
1370
1371 return hci_uart_register_proto(&bcm_proto);
1372}
1373
1374int __exit bcm_deinit(void)
1375{
1376 platform_driver_unregister(&bcm_driver);
1377 serdev_device_driver_unregister(&bcm_serdev_driver);
1378
1379 return hci_uart_unregister_proto(&bcm_proto);
1380}