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