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