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