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