hci_bcm.c 24.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *
 *  Bluetooth HCI UART driver for Broadcom devices
 *
 *  Copyright (C) 2015  Intel Corporation
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
27
#include <linux/firmware.h>
28 29
#include <linux/module.h>
#include <linux/acpi.h>
30 31
#include <linux/of.h>
#include <linux/property.h>
32 33 34 35
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/gpio/consumer.h>
#include <linux/tty.h>
36
#include <linux/interrupt.h>
37
#include <linux/dmi.h>
38
#include <linux/pm_runtime.h>
39
#include <linux/serdev.h>
40 41 42 43

#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>

44
#include "btbcm.h"
45
#include "hci_uart.h"
46

47 48 49
#define BCM_NULL_PKT 0x00
#define BCM_NULL_SIZE 0

50 51 52
#define BCM_LM_DIAG_PKT 0x07
#define BCM_LM_DIAG_SIZE 63

53 54
#define BCM_AUTOSUSPEND_DELAY	5000 /* default autosleep delay */

55
/* device driver resources */
56
struct bcm_device {
57 58
	/* Must be the first member, hci_serdev.c expects this. */
	struct hci_uart		serdev_hu;
59 60
	struct list_head	list;

61
	struct device		*dev;
62 63 64 65 66 67 68

	const char		*name;
	struct gpio_desc	*device_wakeup;
	struct gpio_desc	*shutdown;

	struct clk		*clk;
	bool			clk_enabled;
69 70

	u32			init_speed;
71
	u32			oper_speed;
72
	int			irq;
73
	bool			irq_active_low;
74

75
#ifdef CONFIG_PM
76 77 78
	struct hci_uart		*hu;
	bool			is_suspended; /* suspend/resume flag */
#endif
79 80
};

81
/* generic bcm uart resources */
82
struct bcm_data {
83 84 85 86
	struct sk_buff		*rx_skb;
	struct sk_buff_head	txq;

	struct bcm_device	*dev;
87 88
};

89
/* List of BCM BT UART devices */
90
static DEFINE_MUTEX(bcm_device_lock);
91 92
static LIST_HEAD(bcm_device_list);

93 94 95 96 97 98 99 100
static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
{
	if (hu->serdev)
		serdev_device_set_baudrate(hu->serdev, speed);
	else
		hci_uart_set_baudrate(hu, speed);
}

101 102 103 104 105 106 107 108 109 110 111
static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
{
	struct hci_dev *hdev = hu->hdev;
	struct sk_buff *skb;
	struct bcm_update_uart_baud_rate param;

	if (speed > 3000000) {
		struct bcm_write_uart_clock_setting clock;

		clock.type = BCM_UART_CLOCK_48MHZ;

112
		bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
113 114 115 116 117 118 119

		/* This Broadcom specific command changes the UART's controller
		 * clock for baud rate > 3000000.
		 */
		skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
		if (IS_ERR(skb)) {
			int err = PTR_ERR(skb);
120 121
			bt_dev_err(hdev, "BCM: failed to write clock (%d)",
				   err);
122 123 124 125 126 127
			return err;
		}

		kfree_skb(skb);
	}

128
	bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
129 130 131 132 133 134 135 136 137 138 139

	param.zero = cpu_to_le16(0);
	param.baud_rate = cpu_to_le32(speed);

	/* This Broadcom specific command changes the UART's controller baud
	 * rate.
	 */
	skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
			     HCI_INIT_TIMEOUT);
	if (IS_ERR(skb)) {
		int err = PTR_ERR(skb);
140 141
		bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
			   err);
142 143 144 145 146 147 148 149
		return err;
	}

	kfree_skb(skb);

	return 0;
}

150
/* bcm_device_exists should be protected by bcm_device_lock */
151 152 153 154
static bool bcm_device_exists(struct bcm_device *device)
{
	struct list_head *p;

155
#ifdef CONFIG_PM
156 157 158
	/* Devices using serdev always exist */
	if (device && device->hu && device->hu->serdev)
		return true;
159
#endif
160

161 162 163 164 165 166 167 168 169 170 171 172 173
	list_for_each(p, &bcm_device_list) {
		struct bcm_device *dev = list_entry(p, struct bcm_device, list);

		if (device == dev)
			return true;
	}

	return false;
}

static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
{
	if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
174
		clk_prepare_enable(dev->clk);
175

176 177
	gpiod_set_value(dev->shutdown, powered);
	gpiod_set_value(dev->device_wakeup, powered);
178 179

	if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
180
		clk_disable_unprepare(dev->clk);
181 182 183 184 185 186

	dev->clk_enabled = powered;

	return 0;
}

187
#ifdef CONFIG_PM
188 189 190 191 192 193
static irqreturn_t bcm_host_wake(int irq, void *data)
{
	struct bcm_device *bdev = data;

	bt_dev_dbg(bdev, "Host wake IRQ");

194
	pm_request_resume(bdev->dev);
195

196 197 198 199 200 201
	return IRQ_HANDLED;
}

static int bcm_request_irq(struct bcm_data *bcm)
{
	struct bcm_device *bdev = bcm->dev;
202
	int err;
203 204 205 206 207 208 209

	mutex_lock(&bcm_device_lock);
	if (!bcm_device_exists(bdev)) {
		err = -ENODEV;
		goto unlock;
	}

210 211 212 213
	if (bdev->irq <= 0) {
		err = -EOPNOTSUPP;
		goto unlock;
	}
214

215
	err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
216 217 218
			       bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
						      IRQF_TRIGGER_RISING,
			       "host_wake", bdev);
219 220
	if (err) {
		bdev->irq = err;
221
		goto unlock;
222
	}
223

224
	device_init_wakeup(bdev->dev, true);
225

226
	pm_runtime_set_autosuspend_delay(bdev->dev,
227
					 BCM_AUTOSUSPEND_DELAY);
228 229 230
	pm_runtime_use_autosuspend(bdev->dev);
	pm_runtime_set_active(bdev->dev);
	pm_runtime_enable(bdev->dev);
231 232 233 234 235 236 237 238 239 240 241 242 243 244

unlock:
	mutex_unlock(&bcm_device_lock);

	return err;
}

static const struct bcm_set_sleep_mode default_sleep_params = {
	.sleep_mode = 1,	/* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
	.idle_host = 2,		/* idle threshold HOST, in 300ms */
	.idle_dev = 2,		/* idle threshold device, in 300ms */
	.bt_wake_active = 1,	/* BT_WAKE active mode: 1 = high, 0 = low */
	.host_wake_active = 0,	/* HOST_WAKE active mode: 1 = high, 0 = low */
	.allow_host_sleep = 1,	/* Allow host sleep in SCO flag */
245
	.combine_modes = 1,	/* Combine sleep and LPM flag */
246 247 248 249 250 251 252 253 254 255 256 257 258 259
	.tristate_control = 0,	/* Allow tri-state control of UART tx flag */
	/* Irrelevant USB flags */
	.usb_auto_sleep = 0,
	.usb_resume_timeout = 0,
	.pulsed_host_wake = 0,
	.break_to_host = 0
};

static int bcm_setup_sleep(struct hci_uart *hu)
{
	struct bcm_data *bcm = hu->priv;
	struct sk_buff *skb;
	struct bcm_set_sleep_mode sleep_params = default_sleep_params;

260
	sleep_params.host_wake_active = !bcm->dev->irq_active_low;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

	skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
			     &sleep_params, HCI_INIT_TIMEOUT);
	if (IS_ERR(skb)) {
		int err = PTR_ERR(skb);
		bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
		return err;
	}
	kfree_skb(skb);

	bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");

	return 0;
}
#else
static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
#endif

280 281 282 283 284 285 286 287 288 289
static int bcm_set_diag(struct hci_dev *hdev, bool enable)
{
	struct hci_uart *hu = hci_get_drvdata(hdev);
	struct bcm_data *bcm = hu->priv;
	struct sk_buff *skb;

	if (!test_bit(HCI_RUNNING, &hdev->flags))
		return -ENETDOWN;

	skb = bt_skb_alloc(3, GFP_KERNEL);
290 291
	if (!skb)
		return -ENOMEM;
292

293 294 295
	skb_put_u8(skb, BCM_LM_DIAG_PKT);
	skb_put_u8(skb, 0xf0);
	skb_put_u8(skb, enable);
296 297 298 299 300 301 302

	skb_queue_tail(&bcm->txq, skb);
	hci_uart_tx_wakeup(hu);

	return 0;
}

303 304 305
static int bcm_open(struct hci_uart *hu)
{
	struct bcm_data *bcm;
306
	struct list_head *p;
307

308
	bt_dev_dbg(hu->hdev, "hu %p", hu);
309 310 311 312 313 314 315 316

	bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
	if (!bcm)
		return -ENOMEM;

	skb_queue_head_init(&bcm->txq);

	hu->priv = bcm;
317

318 319
	mutex_lock(&bcm_device_lock);

320 321
	if (hu->serdev) {
		serdev_device_open(hu->serdev);
322
		bcm->dev = serdev_device_get_drvdata(hu->serdev);
323 324 325
		goto out;
	}

326 327 328
	if (!hu->tty->dev)
		goto out;

329 330 331 332 333 334 335
	list_for_each(p, &bcm_device_list) {
		struct bcm_device *dev = list_entry(p, struct bcm_device, list);

		/* Retrieve saved bcm_device based on parent of the
		 * platform device (saved during device probe) and
		 * parent of tty device used by hci_uart
		 */
336
		if (hu->tty->dev->parent == dev->dev->parent) {
337
			bcm->dev = dev;
338
#ifdef CONFIG_PM
339 340
			dev->hu = hu;
#endif
341 342 343 344
			break;
		}
	}

345
out:
346 347 348 349 350 351 352
	if (bcm->dev) {
		hu->init_speed = bcm->dev->init_speed;
		hu->oper_speed = bcm->dev->oper_speed;
		bcm_gpio_set_power(bcm->dev, true);
	}

	mutex_unlock(&bcm_device_lock);
353 354 355 356 357 358
	return 0;
}

static int bcm_close(struct hci_uart *hu)
{
	struct bcm_data *bcm = hu->priv;
359
	struct bcm_device *bdev = NULL;
360

361
	bt_dev_dbg(hu->hdev, "hu %p", hu);
362

363
	/* Protect bcm->dev against removal of the device or driver */
364
	mutex_lock(&bcm_device_lock);
365 366 367 368 369 370 371 372 373 374 375 376

	if (hu->serdev) {
		serdev_device_close(hu->serdev);
		bdev = serdev_device_get_drvdata(hu->serdev);
	} else if (bcm_device_exists(bcm->dev)) {
		bdev = bcm->dev;
#ifdef CONFIG_PM
		bdev->hu = NULL;
#endif
	}

	if (bdev) {
377
		if (IS_ENABLED(CONFIG_PM) && bdev->irq > 0) {
378 379
			devm_free_irq(bdev->dev, bdev->irq, bdev);
			device_init_wakeup(bdev->dev, false);
380
			pm_runtime_disable(bdev->dev);
381
		}
382 383 384

		bcm_gpio_set_power(bdev, false);
		pm_runtime_set_suspended(bdev->dev);
385
	}
386
	mutex_unlock(&bcm_device_lock);
387

388 389 390 391 392 393 394 395 396 397 398 399
	skb_queue_purge(&bcm->txq);
	kfree_skb(bcm->rx_skb);
	kfree(bcm);

	hu->priv = NULL;
	return 0;
}

static int bcm_flush(struct hci_uart *hu)
{
	struct bcm_data *bcm = hu->priv;

400
	bt_dev_dbg(hu->hdev, "hu %p", hu);
401 402 403 404 405 406 407 408

	skb_queue_purge(&bcm->txq);

	return 0;
}

static int bcm_setup(struct hci_uart *hu)
{
409
	struct bcm_data *bcm = hu->priv;
410 411
	char fw_name[64];
	const struct firmware *fw;
412
	unsigned int speed;
413 414
	int err;

415
	bt_dev_dbg(hu->hdev, "hu %p", hu);
416

417
	hu->hdev->set_diag = bcm_set_diag;
418 419
	hu->hdev->set_bdaddr = btbcm_set_bdaddr;

420 421 422 423 424 425
	err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
	if (err)
		return err;

	err = request_firmware(&fw, fw_name, &hu->hdev->dev);
	if (err < 0) {
426
		bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
427 428 429 430 431
		return 0;
	}

	err = btbcm_patchram(hu->hdev, fw);
	if (err) {
432
		bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
433 434 435
		goto finalize;
	}

436 437 438 439 440 441 442 443 444
	/* Init speed if any */
	if (hu->init_speed)
		speed = hu->init_speed;
	else if (hu->proto->init_speed)
		speed = hu->proto->init_speed;
	else
		speed = 0;

	if (speed)
445
		host_set_baudrate(hu, speed);
446 447 448 449 450 451 452 453 454 455 456

	/* Operational speed if any */
	if (hu->oper_speed)
		speed = hu->oper_speed;
	else if (hu->proto->oper_speed)
		speed = hu->proto->oper_speed;
	else
		speed = 0;

	if (speed) {
		err = bcm_set_baudrate(hu, speed);
457
		if (!err)
458
			host_set_baudrate(hu, speed);
459 460
	}

461 462 463 464
finalize:
	release_firmware(fw);

	err = btbcm_finalize(hu->hdev);
465 466 467
	if (err)
		return err;

468
	if (!bcm_request_irq(bcm))
469
		err = bcm_setup_sleep(hu);
470 471

	return err;
472 473
}

474 475 476 477 478 479 480
#define BCM_RECV_LM_DIAG \
	.type = BCM_LM_DIAG_PKT, \
	.hlen = BCM_LM_DIAG_SIZE, \
	.loff = 0, \
	.lsize = 0, \
	.maxlen = BCM_LM_DIAG_SIZE

481 482 483 484 485 486 487
#define BCM_RECV_NULL \
	.type = BCM_NULL_PKT, \
	.hlen = BCM_NULL_SIZE, \
	.loff = 0, \
	.lsize = 0, \
	.maxlen = BCM_NULL_SIZE

488
static const struct h4_recv_pkt bcm_recv_pkts[] = {
489 490 491 492
	{ H4_RECV_ACL,      .recv = hci_recv_frame },
	{ H4_RECV_SCO,      .recv = hci_recv_frame },
	{ H4_RECV_EVENT,    .recv = hci_recv_frame },
	{ BCM_RECV_LM_DIAG, .recv = hci_recv_diag  },
493
	{ BCM_RECV_NULL,    .recv = hci_recv_diag  },
494 495
};

496 497 498 499 500 501 502
static int bcm_recv(struct hci_uart *hu, const void *data, int count)
{
	struct bcm_data *bcm = hu->priv;

	if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
		return -EUNATCH;

503 504
	bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
				  bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
505 506
	if (IS_ERR(bcm->rx_skb)) {
		int err = PTR_ERR(bcm->rx_skb);
507
		bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
508
		bcm->rx_skb = NULL;
509
		return err;
510 511 512
	} else if (!bcm->rx_skb) {
		/* Delay auto-suspend when receiving completed packet */
		mutex_lock(&bcm_device_lock);
513 514
		if (bcm->dev && bcm_device_exists(bcm->dev))
			pm_request_resume(bcm->dev->dev);
515
		mutex_unlock(&bcm_device_lock);
516 517 518 519 520 521 522 523 524
	}

	return count;
}

static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
{
	struct bcm_data *bcm = hu->priv;

525
	bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
526 527

	/* Prepend skb with frame type */
528
	memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
529 530 531 532 533 534 535 536
	skb_queue_tail(&bcm->txq, skb);

	return 0;
}

static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
{
	struct bcm_data *bcm = hu->priv;
537 538 539 540 541 542 543
	struct sk_buff *skb = NULL;
	struct bcm_device *bdev = NULL;

	mutex_lock(&bcm_device_lock);

	if (bcm_device_exists(bcm->dev)) {
		bdev = bcm->dev;
544
		pm_runtime_get_sync(bdev->dev);
545 546 547 548 549 550
		/* Shall be resumed here */
	}

	skb = skb_dequeue(&bcm->txq);

	if (bdev) {
551 552
		pm_runtime_mark_last_busy(bdev->dev);
		pm_runtime_put_autosuspend(bdev->dev);
553
	}
554

555 556 557
	mutex_unlock(&bcm_device_lock);

	return skb;
558 559
}

560 561
#ifdef CONFIG_PM
static int bcm_suspend_device(struct device *dev)
562
{
563
	struct bcm_device *bdev = dev_get_drvdata(dev);
564

565
	bt_dev_dbg(bdev, "");
566

567
	if (!bdev->is_suspended && bdev->hu) {
568 569
		hci_uart_set_flow_control(bdev->hu, true);

570
		/* Once this returns, driver suspends BT via GPIO */
571 572 573 574
		bdev->is_suspended = true;
	}

	/* Suspend the device */
575 576 577
	gpiod_set_value(bdev->device_wakeup, false);
	bt_dev_dbg(bdev, "suspend, delaying 15 ms");
	mdelay(15);
578

579 580 581 582 583
	return 0;
}

static int bcm_resume_device(struct device *dev)
{
584
	struct bcm_device *bdev = dev_get_drvdata(dev);
585 586 587

	bt_dev_dbg(bdev, "");

588 589 590
	gpiod_set_value(bdev->device_wakeup, true);
	bt_dev_dbg(bdev, "resume, delaying 15 ms");
	mdelay(15);
591 592 593 594 595 596 597 598 599 600 601 602 603

	/* When this executes, the device has woken up already */
	if (bdev->is_suspended && bdev->hu) {
		bdev->is_suspended = false;

		hci_uart_set_flow_control(bdev->hu, false);
	}

	return 0;
}
#endif

#ifdef CONFIG_PM_SLEEP
604
/* suspend callback */
605 606
static int bcm_suspend(struct device *dev)
{
607
	struct bcm_device *bdev = dev_get_drvdata(dev);
608 609 610 611
	int error;

	bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);

612 613 614 615
	/*
	 * When used with a device instantiated as platform_device, bcm_suspend
	 * can be called at any time as long as the platform device is bound,
	 * so it should use bcm_device_lock to protect access to hci_uart
616 617 618 619 620 621 622
	 * and device_wake-up GPIO.
	 */
	mutex_lock(&bcm_device_lock);

	if (!bdev->hu)
		goto unlock;

623 624
	if (pm_runtime_active(dev))
		bcm_suspend_device(dev);
625

626
	if (device_may_wakeup(dev) && bdev->irq > 0) {
627 628 629 630 631
		error = enable_irq_wake(bdev->irq);
		if (!error)
			bt_dev_dbg(bdev, "BCM irq: enabled");
	}

632
unlock:
633
	mutex_unlock(&bcm_device_lock);
634

635 636 637
	return 0;
}

638
/* resume callback */
639 640
static int bcm_resume(struct device *dev)
{
641
	struct bcm_device *bdev = dev_get_drvdata(dev);
642

643
	bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
644

645 646 647 648
	/*
	 * When used with a device instantiated as platform_device, bcm_resume
	 * can be called at any time as long as platform device is bound,
	 * so it should use bcm_device_lock to protect access to hci_uart
649 650
	 * and device_wake-up GPIO.
	 */
651
	mutex_lock(&bcm_device_lock);
652 653 654 655

	if (!bdev->hu)
		goto unlock;

656
	if (device_may_wakeup(dev) && bdev->irq > 0) {
657 658 659 660
		disable_irq_wake(bdev->irq);
		bt_dev_dbg(bdev, "BCM irq: disabled");
	}

661
	bcm_resume_device(dev);
662

663
unlock:
664
	mutex_unlock(&bcm_device_lock);
665

666 667 668 669
	pm_runtime_disable(dev);
	pm_runtime_set_active(dev);
	pm_runtime_enable(dev);

670 671 672 673
	return 0;
}
#endif

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
static const struct acpi_gpio_params int_last_device_wakeup_gpios = { 0, 0, false };
static const struct acpi_gpio_params int_last_shutdown_gpios = { 1, 0, false };
static const struct acpi_gpio_params int_last_host_wakeup_gpios = { 2, 0, false };

static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
	{ "device-wakeup-gpios", &int_last_device_wakeup_gpios, 1 },
	{ "shutdown-gpios", &int_last_shutdown_gpios, 1 },
	{ "host-wakeup-gpios", &int_last_host_wakeup_gpios, 1 },
	{ },
};

static const struct acpi_gpio_params int_first_host_wakeup_gpios = { 0, 0, false };
static const struct acpi_gpio_params int_first_device_wakeup_gpios = { 1, 0, false };
static const struct acpi_gpio_params int_first_shutdown_gpios = { 2, 0, false };

static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
	{ "device-wakeup-gpios", &int_first_device_wakeup_gpios, 1 },
	{ "shutdown-gpios", &int_first_shutdown_gpios, 1 },
	{ "host-wakeup-gpios", &int_first_host_wakeup_gpios, 1 },
693 694 695
	{ },
};

696
#ifdef CONFIG_ACPI
697
/* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
698
static const struct dmi_system_id bcm_active_low_irq_dmi_table[] = {
699 700 701 702 703 704 705 706
	{
		.ident = "Asus T100TA",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR,
					"ASUSTeK COMPUTER INC."),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
		},
	},
707 708 709 710 711 712 713 714
	{
		.ident = "Asus T100CHI",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR,
					"ASUSTeK COMPUTER INC."),
			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100CHI"),
		},
	},
715 716 717 718 719 720 721
	{	/* Handle ThinkPad 8 tablets with BCM2E55 chipset ACPI ID */
		.ident = "Lenovo ThinkPad 8",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
			DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "ThinkPad 8"),
		},
	},
722 723 724 725 726 727 728
	{
		.ident = "MINIX Z83-4",
		.matches = {
			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MINIX"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
		},
	},
729 730 731
	{ }
};

732 733 734
static int bcm_resource(struct acpi_resource *ares, void *data)
{
	struct bcm_device *dev = data;
735 736 737 738 739 740 741
	struct acpi_resource_extended_irq *irq;
	struct acpi_resource_gpio *gpio;
	struct acpi_resource_uart_serialbus *sb;

	switch (ares->type) {
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
		irq = &ares->data.extended_irq;
742
		dev->irq_active_low = irq->polarity == ACPI_ACTIVE_LOW;
743 744 745 746 747
		break;

	case ACPI_RESOURCE_TYPE_GPIO:
		gpio = &ares->data.gpio;
		if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
748
			dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
749 750 751
		break;

	case ACPI_RESOURCE_TYPE_SERIAL_BUS:
752
		sb = &ares->data.uart_serial_bus;
753
		if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
754
			dev->init_speed = sb->default_baud_rate;
755 756
			dev->oper_speed = 4000000;
		}
757 758 759 760
		break;

	default:
		break;
761 762
	}

763
	return 0;
764
}
765
#endif /* CONFIG_ACPI */
766

767
static int bcm_get_resources(struct bcm_device *dev)
768
{
769
	dev->name = dev_name(dev->dev);
770

771
	dev->clk = devm_clk_get(dev->dev, NULL);
772

773 774
	dev->device_wakeup = devm_gpiod_get(dev->dev, "device-wakeup",
					    GPIOD_OUT_LOW);
775 776
	if (IS_ERR(dev->device_wakeup))
		return PTR_ERR(dev->device_wakeup);
777

778
	dev->shutdown = devm_gpiod_get(dev->dev, "shutdown", GPIOD_OUT_LOW);
779 780
	if (IS_ERR(dev->shutdown))
		return PTR_ERR(dev->shutdown);
781

782 783 784 785
	/* IRQ can be declared in ACPI table as Interrupt or GpioInt */
	if (dev->irq <= 0) {
		struct gpio_desc *gpio;

786
		gpio = devm_gpiod_get_optional(dev->dev, "host-wakeup",
787 788 789 790 791 792 793
					       GPIOD_IN);
		if (IS_ERR(gpio))
			return PTR_ERR(gpio);

		dev->irq = gpiod_to_irq(gpio);
	}

794
	dev_info(dev->dev, "BCM irq: %d\n", dev->irq);
795 796 797 798 799 800 801 802 803 804
	return 0;
}

#ifdef CONFIG_ACPI
static int bcm_acpi_probe(struct bcm_device *dev)
{
	LIST_HEAD(resources);
	const struct dmi_system_id *dmi_id;
	const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
	const struct acpi_device_id *id;
805
	struct resource_entry *entry;
806 807 808
	int ret;

	/* Retrieve GPIO data */
809
	id = acpi_match_device(dev->dev->driver->acpi_match_table, dev->dev);
810 811 812
	if (id)
		gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data;

813
	ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
814 815 816
	if (ret)
		return ret;

817
	/* Retrieve UART ACPI info */
818
	ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
819
				     &resources, bcm_resource, dev);
820 821
	if (ret < 0)
		return ret;
822 823 824 825 826 827 828

	resource_list_for_each_entry(entry, &resources) {
		if (resource_type(entry->res) == IORESOURCE_IRQ) {
			dev->irq = entry->res->start;
			break;
		}
	}
829
	acpi_dev_free_resource_list(&resources);
830

831
	dmi_id = dmi_first_match(bcm_active_low_irq_dmi_table);
832
	if (dmi_id) {
833
		dev_warn(dev->dev, "%s: Overwriting IRQ polarity to active low",
834
			    dmi_id->ident);
835
		dev->irq_active_low = true;
836 837
	}

838 839
	return 0;
}
840 841 842 843 844 845
#else
static int bcm_acpi_probe(struct bcm_device *dev)
{
	return -EINVAL;
}
#endif /* CONFIG_ACPI */
846

847 848 849 850 851 852
static int bcm_of_probe(struct bcm_device *bdev)
{
	device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
	return 0;
}

853 854 855 856 857 858 859 860 861
static int bcm_probe(struct platform_device *pdev)
{
	struct bcm_device *dev;
	int ret;

	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return -ENOMEM;

862
	dev->dev = &pdev->dev;
863
	dev->irq = platform_get_irq(pdev, 0);
864

865
	if (has_acpi_companion(&pdev->dev)) {
866
		ret = bcm_acpi_probe(dev);
867 868 869 870
		if (ret)
			return ret;
	}

871
	ret = bcm_get_resources(dev);
872 873
	if (ret)
		return ret;
874 875 876 877 878 879

	platform_set_drvdata(pdev, dev);

	dev_info(&pdev->dev, "%s device registered.\n", dev->name);

	/* Place this instance on the device list */
880
	mutex_lock(&bcm_device_lock);
881
	list_add_tail(&dev->list, &bcm_device_list);
882
	mutex_unlock(&bcm_device_lock);
883 884 885 886 887 888 889 890 891 892

	bcm_gpio_set_power(dev, false);

	return 0;
}

static int bcm_remove(struct platform_device *pdev)
{
	struct bcm_device *dev = platform_get_drvdata(pdev);

893
	mutex_lock(&bcm_device_lock);
894
	list_del(&dev->list);
895
	mutex_unlock(&bcm_device_lock);
896 897 898 899 900 901

	dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);

	return 0;
}

902 903
static const struct hci_uart_proto bcm_proto = {
	.id		= HCI_UART_BCM,
904
	.name		= "Broadcom",
905
	.manufacturer	= 15,
906
	.init_speed	= 115200,
907 908 909 910
	.open		= bcm_open,
	.close		= bcm_close,
	.flush		= bcm_flush,
	.setup		= bcm_setup,
911
	.set_baudrate	= bcm_set_baudrate,
912 913 914 915 916
	.recv		= bcm_recv,
	.enqueue	= bcm_enqueue,
	.dequeue	= bcm_dequeue,
};

917 918
#ifdef CONFIG_ACPI
static const struct acpi_device_id bcm_acpi_match[] = {
919 920 921 922 923 924 925 926 927 928 929 930
	{ "BCM2E1A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E39", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E3A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E3D", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E3F", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E40", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E54", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E55", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E64", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E65", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E67", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E71", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
931
	{ "BCM2E72", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
932 933
	{ "BCM2E7B", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
	{ "BCM2E7C", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
934
	{ "BCM2E7E", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
935 936
	{ "BCM2E95", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
	{ "BCM2E96", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
937
	{ "BCM2EA4", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
938 939 940 941 942
	{ },
};
MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
#endif

943
/* suspend and resume callbacks */
944 945 946 947
static const struct dev_pm_ops bcm_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
	SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
};
948

949 950 951 952 953 954
static struct platform_driver bcm_driver = {
	.probe = bcm_probe,
	.remove = bcm_remove,
	.driver = {
		.name = "hci_bcm",
		.acpi_match_table = ACPI_PTR(bcm_acpi_match),
955
		.pm = &bcm_pm_ops,
956 957 958
	},
};

959 960
static int bcm_serdev_probe(struct serdev_device *serdev)
{
961
	struct bcm_device *bcmdev;
962 963 964 965 966 967
	int err;

	bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
	if (!bcmdev)
		return -ENOMEM;

968
	bcmdev->dev = &serdev->dev;
969
#ifdef CONFIG_PM
970
	bcmdev->hu = &bcmdev->serdev_hu;
971
#endif
972
	bcmdev->serdev_hu.serdev = serdev;
973 974
	serdev_device_set_drvdata(serdev, bcmdev);

975 976 977 978 979 980
	if (has_acpi_companion(&serdev->dev))
		err = bcm_acpi_probe(bcmdev);
	else
		err = bcm_of_probe(bcmdev);
	if (err)
		return err;
981

982 983 984 985 986 987 988
	err = bcm_get_resources(bcmdev);
	if (err)
		return err;

	bcm_gpio_set_power(bcmdev, false);

	return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
989 990 991 992
}

static void bcm_serdev_remove(struct serdev_device *serdev)
{
993
	struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
994

995
	hci_uart_unregister_device(&bcmdev->serdev_hu);
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
}

#ifdef CONFIG_OF
static const struct of_device_id bcm_bluetooth_of_match[] = {
	{ .compatible = "brcm,bcm43438-bt" },
	{ },
};
MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
#endif

static struct serdev_device_driver bcm_serdev_driver = {
	.probe = bcm_serdev_probe,
	.remove = bcm_serdev_remove,
	.driver = {
		.name = "hci_uart_bcm",
		.of_match_table = of_match_ptr(bcm_bluetooth_of_match),
1012 1013
		.acpi_match_table = ACPI_PTR(bcm_acpi_match),
		.pm = &bcm_pm_ops,
1014 1015 1016
	},
};

1017 1018
int __init bcm_init(void)
{
1019 1020 1021
	/* For now, we need to keep both platform device
	 * driver (ACPI generated) and serdev driver (DT).
	 */
1022
	platform_driver_register(&bcm_driver);
1023
	serdev_device_driver_register(&bcm_serdev_driver);
1024

1025 1026 1027 1028 1029
	return hci_uart_register_proto(&bcm_proto);
}

int __exit bcm_deinit(void)
{
1030
	platform_driver_unregister(&bcm_driver);
1031
	serdev_device_driver_unregister(&bcm_serdev_driver);
1032

1033 1034
	return hci_uart_unregister_proto(&bcm_proto);
}