OSDN Git Service

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