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