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