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