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