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