]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/bluetooth/hci_bcm.c
Bluetooth: hci_bcm: Add support for MINIX Z83-4 based devices
[mirror_ubuntu-bionic-kernel.git] / drivers / bluetooth / hci_bcm.c
1 /*
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_device.h>
33 #include <linux/clk.h>
34 #include <linux/gpio/consumer.h>
35 #include <linux/tty.h>
36 #include <linux/interrupt.h>
37 #include <linux/dmi.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/serdev.h>
40
41 #include <net/bluetooth/bluetooth.h>
42 #include <net/bluetooth/hci_core.h>
43
44 #include "btbcm.h"
45 #include "hci_uart.h"
46
47 #define BCM_NULL_PKT 0x00
48 #define BCM_NULL_SIZE 0
49
50 #define BCM_LM_DIAG_PKT 0x07
51 #define BCM_LM_DIAG_SIZE 63
52
53 #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
54
55 /* device driver resources */
56 struct bcm_device {
57 /* Must be the first member, hci_serdev.c expects this. */
58 struct hci_uart serdev_hu;
59 struct list_head list;
60
61 struct device *dev;
62
63 const char *name;
64 struct gpio_desc *device_wakeup;
65 struct gpio_desc *shutdown;
66
67 struct clk *clk;
68 bool clk_enabled;
69
70 u32 init_speed;
71 u32 oper_speed;
72 int irq;
73 bool irq_active_low;
74
75 #ifdef CONFIG_PM
76 struct hci_uart *hu;
77 bool is_suspended; /* suspend/resume flag */
78 #endif
79 };
80
81 /* generic bcm uart resources */
82 struct bcm_data {
83 struct sk_buff *rx_skb;
84 struct sk_buff_head txq;
85
86 struct bcm_device *dev;
87 };
88
89 /* List of BCM BT UART devices */
90 static DEFINE_MUTEX(bcm_device_lock);
91 static LIST_HEAD(bcm_device_list);
92
93 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
94 {
95 if (hu->serdev)
96 serdev_device_set_baudrate(hu->serdev, speed);
97 else
98 hci_uart_set_baudrate(hu, speed);
99 }
100
101 static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
102 {
103 struct hci_dev *hdev = hu->hdev;
104 struct sk_buff *skb;
105 struct bcm_update_uart_baud_rate param;
106
107 if (speed > 3000000) {
108 struct bcm_write_uart_clock_setting clock;
109
110 clock.type = BCM_UART_CLOCK_48MHZ;
111
112 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
113
114 /* This Broadcom specific command changes the UART's controller
115 * clock for baud rate > 3000000.
116 */
117 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
118 if (IS_ERR(skb)) {
119 int err = PTR_ERR(skb);
120 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
121 err);
122 return err;
123 }
124
125 kfree_skb(skb);
126 }
127
128 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
129
130 param.zero = cpu_to_le16(0);
131 param.baud_rate = cpu_to_le32(speed);
132
133 /* This Broadcom specific command changes the UART's controller baud
134 * rate.
135 */
136 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
137 HCI_INIT_TIMEOUT);
138 if (IS_ERR(skb)) {
139 int err = PTR_ERR(skb);
140 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
141 err);
142 return err;
143 }
144
145 kfree_skb(skb);
146
147 return 0;
148 }
149
150 /* bcm_device_exists should be protected by bcm_device_lock */
151 static bool bcm_device_exists(struct bcm_device *device)
152 {
153 struct list_head *p;
154
155 /* Devices using serdev always exist */
156 if (device && device->hu && device->hu->serdev)
157 return true;
158
159 list_for_each(p, &bcm_device_list) {
160 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
161
162 if (device == dev)
163 return true;
164 }
165
166 return false;
167 }
168
169 static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
170 {
171 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
172 clk_prepare_enable(dev->clk);
173
174 gpiod_set_value(dev->shutdown, powered);
175 gpiod_set_value(dev->device_wakeup, powered);
176
177 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
178 clk_disable_unprepare(dev->clk);
179
180 dev->clk_enabled = powered;
181
182 return 0;
183 }
184
185 #ifdef CONFIG_PM
186 static irqreturn_t bcm_host_wake(int irq, void *data)
187 {
188 struct bcm_device *bdev = data;
189
190 bt_dev_dbg(bdev, "Host wake IRQ");
191
192 pm_runtime_get(bdev->dev);
193 pm_runtime_mark_last_busy(bdev->dev);
194 pm_runtime_put_autosuspend(bdev->dev);
195
196 return IRQ_HANDLED;
197 }
198
199 static int bcm_request_irq(struct bcm_data *bcm)
200 {
201 struct bcm_device *bdev = bcm->dev;
202 int err;
203
204 mutex_lock(&bcm_device_lock);
205 if (!bcm_device_exists(bdev)) {
206 err = -ENODEV;
207 goto unlock;
208 }
209
210 if (bdev->irq <= 0) {
211 err = -EOPNOTSUPP;
212 goto unlock;
213 }
214
215 err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
216 bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
217 IRQF_TRIGGER_RISING,
218 "host_wake", bdev);
219 if (err)
220 goto unlock;
221
222 device_init_wakeup(bdev->dev, true);
223
224 pm_runtime_set_autosuspend_delay(bdev->dev,
225 BCM_AUTOSUSPEND_DELAY);
226 pm_runtime_use_autosuspend(bdev->dev);
227 pm_runtime_set_active(bdev->dev);
228 pm_runtime_enable(bdev->dev);
229
230 unlock:
231 mutex_unlock(&bcm_device_lock);
232
233 return err;
234 }
235
236 static const struct bcm_set_sleep_mode default_sleep_params = {
237 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
238 .idle_host = 2, /* idle threshold HOST, in 300ms */
239 .idle_dev = 2, /* idle threshold device, in 300ms */
240 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
241 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
242 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
243 .combine_modes = 1, /* Combine sleep and LPM flag */
244 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
245 /* Irrelevant USB flags */
246 .usb_auto_sleep = 0,
247 .usb_resume_timeout = 0,
248 .pulsed_host_wake = 0,
249 .break_to_host = 0
250 };
251
252 static int bcm_setup_sleep(struct hci_uart *hu)
253 {
254 struct bcm_data *bcm = hu->priv;
255 struct sk_buff *skb;
256 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
257
258 sleep_params.host_wake_active = !bcm->dev->irq_active_low;
259
260 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
261 &sleep_params, HCI_INIT_TIMEOUT);
262 if (IS_ERR(skb)) {
263 int err = PTR_ERR(skb);
264 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
265 return err;
266 }
267 kfree_skb(skb);
268
269 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
270
271 return 0;
272 }
273 #else
274 static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
275 static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
276 #endif
277
278 static int bcm_set_diag(struct hci_dev *hdev, bool enable)
279 {
280 struct hci_uart *hu = hci_get_drvdata(hdev);
281 struct bcm_data *bcm = hu->priv;
282 struct sk_buff *skb;
283
284 if (!test_bit(HCI_RUNNING, &hdev->flags))
285 return -ENETDOWN;
286
287 skb = bt_skb_alloc(3, GFP_KERNEL);
288 if (!skb)
289 return -ENOMEM;
290
291 skb_put_u8(skb, BCM_LM_DIAG_PKT);
292 skb_put_u8(skb, 0xf0);
293 skb_put_u8(skb, enable);
294
295 skb_queue_tail(&bcm->txq, skb);
296 hci_uart_tx_wakeup(hu);
297
298 return 0;
299 }
300
301 static int bcm_open(struct hci_uart *hu)
302 {
303 struct bcm_data *bcm;
304 struct list_head *p;
305
306 bt_dev_dbg(hu->hdev, "hu %p", hu);
307
308 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
309 if (!bcm)
310 return -ENOMEM;
311
312 skb_queue_head_init(&bcm->txq);
313
314 hu->priv = bcm;
315
316 mutex_lock(&bcm_device_lock);
317
318 if (hu->serdev) {
319 serdev_device_open(hu->serdev);
320 bcm->dev = serdev_device_get_drvdata(hu->serdev);
321 goto out;
322 }
323
324 if (!hu->tty->dev)
325 goto out;
326
327 list_for_each(p, &bcm_device_list) {
328 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
329
330 /* Retrieve saved bcm_device based on parent of the
331 * platform device (saved during device probe) and
332 * parent of tty device used by hci_uart
333 */
334 if (hu->tty->dev->parent == dev->dev->parent) {
335 bcm->dev = dev;
336 #ifdef CONFIG_PM
337 dev->hu = hu;
338 #endif
339 break;
340 }
341 }
342
343 out:
344 if (bcm->dev) {
345 hu->init_speed = bcm->dev->init_speed;
346 hu->oper_speed = bcm->dev->oper_speed;
347 bcm_gpio_set_power(bcm->dev, true);
348 }
349
350 mutex_unlock(&bcm_device_lock);
351 return 0;
352 }
353
354 static int bcm_close(struct hci_uart *hu)
355 {
356 struct bcm_data *bcm = hu->priv;
357 struct bcm_device *bdev = NULL;
358
359 bt_dev_dbg(hu->hdev, "hu %p", hu);
360
361 /* Protect bcm->dev against removal of the device or driver */
362 mutex_lock(&bcm_device_lock);
363
364 if (hu->serdev) {
365 serdev_device_close(hu->serdev);
366 bdev = serdev_device_get_drvdata(hu->serdev);
367 } else if (bcm_device_exists(bcm->dev)) {
368 bdev = bcm->dev;
369 #ifdef CONFIG_PM
370 bdev->hu = NULL;
371 #endif
372 }
373
374 if (bdev) {
375 bcm_gpio_set_power(bdev, false);
376 #ifdef CONFIG_PM
377 pm_runtime_disable(bdev->dev);
378 pm_runtime_set_suspended(bdev->dev);
379
380 if (device_can_wakeup(bdev->dev)) {
381 devm_free_irq(bdev->dev, bdev->irq, bdev);
382 device_init_wakeup(bdev->dev, false);
383 }
384 #endif
385 }
386 mutex_unlock(&bcm_device_lock);
387
388 skb_queue_purge(&bcm->txq);
389 kfree_skb(bcm->rx_skb);
390 kfree(bcm);
391
392 hu->priv = NULL;
393 return 0;
394 }
395
396 static int bcm_flush(struct hci_uart *hu)
397 {
398 struct bcm_data *bcm = hu->priv;
399
400 bt_dev_dbg(hu->hdev, "hu %p", hu);
401
402 skb_queue_purge(&bcm->txq);
403
404 return 0;
405 }
406
407 static int bcm_setup(struct hci_uart *hu)
408 {
409 struct bcm_data *bcm = hu->priv;
410 char fw_name[64];
411 const struct firmware *fw;
412 unsigned int speed;
413 int err;
414
415 bt_dev_dbg(hu->hdev, "hu %p", hu);
416
417 hu->hdev->set_diag = bcm_set_diag;
418 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
419
420 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
421 if (err)
422 return err;
423
424 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
425 if (err < 0) {
426 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
427 return 0;
428 }
429
430 err = btbcm_patchram(hu->hdev, fw);
431 if (err) {
432 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
433 goto finalize;
434 }
435
436 /* Init speed if any */
437 if (hu->init_speed)
438 speed = hu->init_speed;
439 else if (hu->proto->init_speed)
440 speed = hu->proto->init_speed;
441 else
442 speed = 0;
443
444 if (speed)
445 host_set_baudrate(hu, speed);
446
447 /* Operational speed if any */
448 if (hu->oper_speed)
449 speed = hu->oper_speed;
450 else if (hu->proto->oper_speed)
451 speed = hu->proto->oper_speed;
452 else
453 speed = 0;
454
455 if (speed) {
456 err = bcm_set_baudrate(hu, speed);
457 if (!err)
458 host_set_baudrate(hu, speed);
459 }
460
461 finalize:
462 release_firmware(fw);
463
464 err = btbcm_finalize(hu->hdev);
465 if (err)
466 return err;
467
468 if (!bcm_request_irq(bcm))
469 err = bcm_setup_sleep(hu);
470
471 return err;
472 }
473
474 #define BCM_RECV_LM_DIAG \
475 .type = BCM_LM_DIAG_PKT, \
476 .hlen = BCM_LM_DIAG_SIZE, \
477 .loff = 0, \
478 .lsize = 0, \
479 .maxlen = BCM_LM_DIAG_SIZE
480
481 #define BCM_RECV_NULL \
482 .type = BCM_NULL_PKT, \
483 .hlen = BCM_NULL_SIZE, \
484 .loff = 0, \
485 .lsize = 0, \
486 .maxlen = BCM_NULL_SIZE
487
488 static const struct h4_recv_pkt bcm_recv_pkts[] = {
489 { H4_RECV_ACL, .recv = hci_recv_frame },
490 { H4_RECV_SCO, .recv = hci_recv_frame },
491 { H4_RECV_EVENT, .recv = hci_recv_frame },
492 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
493 { BCM_RECV_NULL, .recv = hci_recv_diag },
494 };
495
496 static int bcm_recv(struct hci_uart *hu, const void *data, int count)
497 {
498 struct bcm_data *bcm = hu->priv;
499
500 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
501 return -EUNATCH;
502
503 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
504 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
505 if (IS_ERR(bcm->rx_skb)) {
506 int err = PTR_ERR(bcm->rx_skb);
507 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
508 bcm->rx_skb = NULL;
509 return err;
510 } else if (!bcm->rx_skb) {
511 /* Delay auto-suspend when receiving completed packet */
512 mutex_lock(&bcm_device_lock);
513 if (bcm->dev && bcm_device_exists(bcm->dev)) {
514 pm_runtime_get(bcm->dev->dev);
515 pm_runtime_mark_last_busy(bcm->dev->dev);
516 pm_runtime_put_autosuspend(bcm->dev->dev);
517 }
518 mutex_unlock(&bcm_device_lock);
519 }
520
521 return count;
522 }
523
524 static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
525 {
526 struct bcm_data *bcm = hu->priv;
527
528 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
529
530 /* Prepend skb with frame type */
531 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
532 skb_queue_tail(&bcm->txq, skb);
533
534 return 0;
535 }
536
537 static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
538 {
539 struct bcm_data *bcm = hu->priv;
540 struct sk_buff *skb = NULL;
541 struct bcm_device *bdev = NULL;
542
543 mutex_lock(&bcm_device_lock);
544
545 if (bcm_device_exists(bcm->dev)) {
546 bdev = bcm->dev;
547 pm_runtime_get_sync(bdev->dev);
548 /* Shall be resumed here */
549 }
550
551 skb = skb_dequeue(&bcm->txq);
552
553 if (bdev) {
554 pm_runtime_mark_last_busy(bdev->dev);
555 pm_runtime_put_autosuspend(bdev->dev);
556 }
557
558 mutex_unlock(&bcm_device_lock);
559
560 return skb;
561 }
562
563 #ifdef CONFIG_PM
564 static int bcm_suspend_device(struct device *dev)
565 {
566 struct bcm_device *bdev = dev_get_drvdata(dev);
567
568 bt_dev_dbg(bdev, "");
569
570 if (!bdev->is_suspended && bdev->hu) {
571 hci_uart_set_flow_control(bdev->hu, true);
572
573 /* Once this returns, driver suspends BT via GPIO */
574 bdev->is_suspended = true;
575 }
576
577 /* Suspend the device */
578 if (bdev->device_wakeup) {
579 gpiod_set_value(bdev->device_wakeup, false);
580 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
581 mdelay(15);
582 }
583
584 return 0;
585 }
586
587 static int bcm_resume_device(struct device *dev)
588 {
589 struct bcm_device *bdev = dev_get_drvdata(dev);
590
591 bt_dev_dbg(bdev, "");
592
593 if (bdev->device_wakeup) {
594 gpiod_set_value(bdev->device_wakeup, true);
595 bt_dev_dbg(bdev, "resume, delaying 15 ms");
596 mdelay(15);
597 }
598
599 /* When this executes, the device has woken up already */
600 if (bdev->is_suspended && bdev->hu) {
601 bdev->is_suspended = false;
602
603 hci_uart_set_flow_control(bdev->hu, false);
604 }
605
606 return 0;
607 }
608 #endif
609
610 #ifdef CONFIG_PM_SLEEP
611 /* suspend callback */
612 static int bcm_suspend(struct device *dev)
613 {
614 struct bcm_device *bdev = dev_get_drvdata(dev);
615 int error;
616
617 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
618
619 /*
620 * When used with a device instantiated as platform_device, bcm_suspend
621 * can be called at any time as long as the platform device is bound,
622 * so it should use bcm_device_lock to protect access to hci_uart
623 * and device_wake-up GPIO.
624 */
625 mutex_lock(&bcm_device_lock);
626
627 if (!bdev->hu)
628 goto unlock;
629
630 if (pm_runtime_active(dev))
631 bcm_suspend_device(dev);
632
633 if (device_may_wakeup(dev)) {
634 error = enable_irq_wake(bdev->irq);
635 if (!error)
636 bt_dev_dbg(bdev, "BCM irq: enabled");
637 }
638
639 unlock:
640 mutex_unlock(&bcm_device_lock);
641
642 return 0;
643 }
644
645 /* resume callback */
646 static int bcm_resume(struct device *dev)
647 {
648 struct bcm_device *bdev = dev_get_drvdata(dev);
649
650 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
651
652 /*
653 * When used with a device instantiated as platform_device, bcm_resume
654 * can be called at any time as long as platform device is bound,
655 * so it should use bcm_device_lock to protect access to hci_uart
656 * and device_wake-up GPIO.
657 */
658 mutex_lock(&bcm_device_lock);
659
660 if (!bdev->hu)
661 goto unlock;
662
663 if (device_may_wakeup(dev)) {
664 disable_irq_wake(bdev->irq);
665 bt_dev_dbg(bdev, "BCM irq: disabled");
666 }
667
668 bcm_resume_device(dev);
669
670 unlock:
671 mutex_unlock(&bcm_device_lock);
672
673 pm_runtime_disable(dev);
674 pm_runtime_set_active(dev);
675 pm_runtime_enable(dev);
676
677 return 0;
678 }
679 #endif
680
681 static const struct acpi_gpio_params int_last_device_wakeup_gpios = { 0, 0, false };
682 static const struct acpi_gpio_params int_last_shutdown_gpios = { 1, 0, false };
683 static const struct acpi_gpio_params int_last_host_wakeup_gpios = { 2, 0, false };
684
685 static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
686 { "device-wakeup-gpios", &int_last_device_wakeup_gpios, 1 },
687 { "shutdown-gpios", &int_last_shutdown_gpios, 1 },
688 { "host-wakeup-gpios", &int_last_host_wakeup_gpios, 1 },
689 { },
690 };
691
692 static const struct acpi_gpio_params int_first_host_wakeup_gpios = { 0, 0, false };
693 static const struct acpi_gpio_params int_first_device_wakeup_gpios = { 1, 0, false };
694 static const struct acpi_gpio_params int_first_shutdown_gpios = { 2, 0, false };
695
696 static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
697 { "device-wakeup-gpios", &int_first_device_wakeup_gpios, 1 },
698 { "shutdown-gpios", &int_first_shutdown_gpios, 1 },
699 { "host-wakeup-gpios", &int_first_host_wakeup_gpios, 1 },
700 { },
701 };
702
703 #ifdef CONFIG_ACPI
704 /* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
705 static const struct dmi_system_id bcm_active_low_irq_dmi_table[] = {
706 {
707 .ident = "Asus T100TA",
708 .matches = {
709 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
710 "ASUSTeK COMPUTER INC."),
711 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
712 },
713 },
714 {
715 .ident = "Asus T100CHI",
716 .matches = {
717 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
718 "ASUSTeK COMPUTER INC."),
719 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100CHI"),
720 },
721 },
722 { /* Handle ThinkPad 8 tablets with BCM2E55 chipset ACPI ID */
723 .ident = "Lenovo ThinkPad 8",
724 .matches = {
725 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
726 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "ThinkPad 8"),
727 },
728 },
729 {
730 .ident = "MINIX Z83-4",
731 .matches = {
732 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MINIX"),
733 DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
734 },
735 },
736 { }
737 };
738
739 static int bcm_resource(struct acpi_resource *ares, void *data)
740 {
741 struct bcm_device *dev = data;
742 struct acpi_resource_extended_irq *irq;
743 struct acpi_resource_gpio *gpio;
744 struct acpi_resource_uart_serialbus *sb;
745
746 switch (ares->type) {
747 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
748 irq = &ares->data.extended_irq;
749 dev->irq_active_low = irq->polarity == ACPI_ACTIVE_LOW;
750 break;
751
752 case ACPI_RESOURCE_TYPE_GPIO:
753 gpio = &ares->data.gpio;
754 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
755 dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
756 break;
757
758 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
759 sb = &ares->data.uart_serial_bus;
760 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
761 dev->init_speed = sb->default_baud_rate;
762 dev->oper_speed = 4000000;
763 }
764 break;
765
766 default:
767 break;
768 }
769
770 return 0;
771 }
772 #endif /* CONFIG_ACPI */
773
774 static int bcm_get_resources(struct bcm_device *dev)
775 {
776 dev->name = dev_name(dev->dev);
777
778 dev->clk = devm_clk_get(dev->dev, NULL);
779
780 dev->device_wakeup = devm_gpiod_get_optional(dev->dev,
781 "device-wakeup",
782 GPIOD_OUT_LOW);
783 if (IS_ERR(dev->device_wakeup))
784 return PTR_ERR(dev->device_wakeup);
785
786 dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown",
787 GPIOD_OUT_LOW);
788 if (IS_ERR(dev->shutdown))
789 return PTR_ERR(dev->shutdown);
790
791 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
792 if (dev->irq <= 0) {
793 struct gpio_desc *gpio;
794
795 gpio = devm_gpiod_get_optional(dev->dev, "host-wakeup",
796 GPIOD_IN);
797 if (IS_ERR(gpio))
798 return PTR_ERR(gpio);
799
800 dev->irq = gpiod_to_irq(gpio);
801 }
802
803 dev_info(dev->dev, "BCM irq: %d\n", dev->irq);
804 return 0;
805 }
806
807 #ifdef CONFIG_ACPI
808 static int bcm_acpi_probe(struct bcm_device *dev)
809 {
810 LIST_HEAD(resources);
811 const struct dmi_system_id *dmi_id;
812 const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
813 const struct acpi_device_id *id;
814 struct resource_entry *entry;
815 int ret;
816
817 /* Retrieve GPIO data */
818 id = acpi_match_device(dev->dev->driver->acpi_match_table, dev->dev);
819 if (id)
820 gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data;
821
822 ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
823 if (ret)
824 return ret;
825
826 /* Retrieve UART ACPI info */
827 ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
828 &resources, bcm_resource, dev);
829 if (ret < 0)
830 return ret;
831
832 resource_list_for_each_entry(entry, &resources) {
833 if (resource_type(entry->res) == IORESOURCE_IRQ) {
834 dev->irq = entry->res->start;
835 break;
836 }
837 }
838 acpi_dev_free_resource_list(&resources);
839
840 dmi_id = dmi_first_match(bcm_active_low_irq_dmi_table);
841 if (dmi_id) {
842 dev_warn(dev->dev, "%s: Overwriting IRQ polarity to active low",
843 dmi_id->ident);
844 dev->irq_active_low = true;
845 }
846
847 return 0;
848 }
849 #else
850 static int bcm_acpi_probe(struct bcm_device *dev)
851 {
852 return -EINVAL;
853 }
854 #endif /* CONFIG_ACPI */
855
856 static int bcm_of_probe(struct bcm_device *bdev)
857 {
858 device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
859 return 0;
860 }
861
862 static int bcm_probe(struct platform_device *pdev)
863 {
864 struct bcm_device *dev;
865 int ret;
866
867 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
868 if (!dev)
869 return -ENOMEM;
870
871 dev->dev = &pdev->dev;
872 dev->irq = platform_get_irq(pdev, 0);
873
874 if (has_acpi_companion(&pdev->dev)) {
875 ret = bcm_acpi_probe(dev);
876 if (ret)
877 return ret;
878 }
879
880 ret = bcm_get_resources(dev);
881 if (ret)
882 return ret;
883
884 platform_set_drvdata(pdev, dev);
885
886 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
887
888 /* Place this instance on the device list */
889 mutex_lock(&bcm_device_lock);
890 list_add_tail(&dev->list, &bcm_device_list);
891 mutex_unlock(&bcm_device_lock);
892
893 bcm_gpio_set_power(dev, false);
894
895 return 0;
896 }
897
898 static int bcm_remove(struct platform_device *pdev)
899 {
900 struct bcm_device *dev = platform_get_drvdata(pdev);
901
902 mutex_lock(&bcm_device_lock);
903 list_del(&dev->list);
904 mutex_unlock(&bcm_device_lock);
905
906 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
907
908 return 0;
909 }
910
911 static const struct hci_uart_proto bcm_proto = {
912 .id = HCI_UART_BCM,
913 .name = "Broadcom",
914 .manufacturer = 15,
915 .init_speed = 115200,
916 .open = bcm_open,
917 .close = bcm_close,
918 .flush = bcm_flush,
919 .setup = bcm_setup,
920 .set_baudrate = bcm_set_baudrate,
921 .recv = bcm_recv,
922 .enqueue = bcm_enqueue,
923 .dequeue = bcm_dequeue,
924 };
925
926 #ifdef CONFIG_ACPI
927 static const struct acpi_device_id bcm_acpi_match[] = {
928 { "BCM2E1A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
929 { "BCM2E39", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
930 { "BCM2E3A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
931 { "BCM2E3D", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
932 { "BCM2E3F", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
933 { "BCM2E40", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
934 { "BCM2E54", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
935 { "BCM2E55", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
936 { "BCM2E64", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
937 { "BCM2E65", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
938 { "BCM2E67", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
939 { "BCM2E71", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
940 { "BCM2E7B", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
941 { "BCM2E7C", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
942 { "BCM2E95", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
943 { "BCM2E96", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
944 { "BCM2EA4", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
945 { },
946 };
947 MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
948 #endif
949
950 /* suspend and resume callbacks */
951 static const struct dev_pm_ops bcm_pm_ops = {
952 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
953 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
954 };
955
956 static struct platform_driver bcm_driver = {
957 .probe = bcm_probe,
958 .remove = bcm_remove,
959 .driver = {
960 .name = "hci_bcm",
961 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
962 .pm = &bcm_pm_ops,
963 },
964 };
965
966 static int bcm_serdev_probe(struct serdev_device *serdev)
967 {
968 struct bcm_device *bcmdev;
969 int err;
970
971 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
972 if (!bcmdev)
973 return -ENOMEM;
974
975 bcmdev->dev = &serdev->dev;
976 bcmdev->hu = &bcmdev->serdev_hu;
977 bcmdev->serdev_hu.serdev = serdev;
978 serdev_device_set_drvdata(serdev, bcmdev);
979
980 if (has_acpi_companion(&serdev->dev))
981 err = bcm_acpi_probe(bcmdev);
982 else
983 err = bcm_of_probe(bcmdev);
984 if (err)
985 return err;
986
987 err = bcm_get_resources(bcmdev);
988 if (err)
989 return err;
990
991 bcm_gpio_set_power(bcmdev, false);
992
993 return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
994 }
995
996 static void bcm_serdev_remove(struct serdev_device *serdev)
997 {
998 struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
999
1000 hci_uart_unregister_device(&bcmdev->serdev_hu);
1001 }
1002
1003 #ifdef CONFIG_OF
1004 static const struct of_device_id bcm_bluetooth_of_match[] = {
1005 { .compatible = "brcm,bcm43438-bt" },
1006 { },
1007 };
1008 MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
1009 #endif
1010
1011 static struct serdev_device_driver bcm_serdev_driver = {
1012 .probe = bcm_serdev_probe,
1013 .remove = bcm_serdev_remove,
1014 .driver = {
1015 .name = "hci_uart_bcm",
1016 .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
1017 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1018 .pm = &bcm_pm_ops,
1019 },
1020 };
1021
1022 int __init bcm_init(void)
1023 {
1024 /* For now, we need to keep both platform device
1025 * driver (ACPI generated) and serdev driver (DT).
1026 */
1027 platform_driver_register(&bcm_driver);
1028 serdev_device_driver_register(&bcm_serdev_driver);
1029
1030 return hci_uart_register_proto(&bcm_proto);
1031 }
1032
1033 int __exit bcm_deinit(void)
1034 {
1035 platform_driver_unregister(&bcm_driver);
1036 serdev_device_driver_unregister(&bcm_serdev_driver);
1037
1038 return hci_uart_unregister_proto(&bcm_proto);
1039 }