1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Bluetooth Software UART Qualcomm protocol 4 * 5 * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management 6 * protocol extension to H4. 7 * 8 * Copyright (C) 2007 Texas Instruments, Inc. 9 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved. 10 * 11 * Acknowledgements: 12 * This file is based on hci_ll.c, which was... 13 * Written by Ohad Ben-Cohen <ohad@bencohen.org> 14 * which was in turn based on hci_h4.c, which was written 15 * by Maxim Krasnyansky and Marcel Holtmann. 16 */ 17 18 #include <linux/kernel.h> 19 #include <linux/clk.h> 20 #include <linux/completion.h> 21 #include <linux/debugfs.h> 22 #include <linux/delay.h> 23 #include <linux/device.h> 24 #include <linux/gpio/consumer.h> 25 #include <linux/mod_devicetable.h> 26 #include <linux/module.h> 27 #include <linux/of_device.h> 28 #include <linux/platform_device.h> 29 #include <linux/regulator/consumer.h> 30 #include <linux/serdev.h> 31 #include <asm/unaligned.h> 32 33 #include <net/bluetooth/bluetooth.h> 34 #include <net/bluetooth/hci_core.h> 35 36 #include "hci_uart.h" 37 #include "btqca.h" 38 39 /* HCI_IBS protocol messages */ 40 #define HCI_IBS_SLEEP_IND 0xFE 41 #define HCI_IBS_WAKE_IND 0xFD 42 #define HCI_IBS_WAKE_ACK 0xFC 43 #define HCI_MAX_IBS_SIZE 10 44 45 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100 46 #define IBS_TX_IDLE_TIMEOUT_MS 2000 47 #define CMD_TRANS_TIMEOUT_MS 100 48 49 /* susclk rate */ 50 #define SUSCLK_RATE_32KHZ 32768 51 52 /* Controller debug log header */ 53 #define QCA_DEBUG_HANDLE 0x2EDC 54 55 enum qca_flags { 56 QCA_IBS_ENABLED, 57 QCA_DROP_VENDOR_EVENT, 58 }; 59 60 /* HCI_IBS transmit side sleep protocol states */ 61 enum tx_ibs_states { 62 HCI_IBS_TX_ASLEEP, 63 HCI_IBS_TX_WAKING, 64 HCI_IBS_TX_AWAKE, 65 }; 66 67 /* HCI_IBS receive side sleep protocol states */ 68 enum rx_states { 69 HCI_IBS_RX_ASLEEP, 70 HCI_IBS_RX_AWAKE, 71 }; 72 73 /* HCI_IBS transmit and receive side clock state vote */ 74 enum hci_ibs_clock_state_vote { 75 HCI_IBS_VOTE_STATS_UPDATE, 76 HCI_IBS_TX_VOTE_CLOCK_ON, 77 HCI_IBS_TX_VOTE_CLOCK_OFF, 78 HCI_IBS_RX_VOTE_CLOCK_ON, 79 HCI_IBS_RX_VOTE_CLOCK_OFF, 80 }; 81 82 struct qca_data { 83 struct hci_uart *hu; 84 struct sk_buff *rx_skb; 85 struct sk_buff_head txq; 86 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */ 87 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */ 88 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/ 89 u8 rx_ibs_state; /* HCI_IBS receive side power state */ 90 bool tx_vote; /* Clock must be on for TX */ 91 bool rx_vote; /* Clock must be on for RX */ 92 struct timer_list tx_idle_timer; 93 u32 tx_idle_delay; 94 struct timer_list wake_retrans_timer; 95 u32 wake_retrans; 96 struct workqueue_struct *workqueue; 97 struct work_struct ws_awake_rx; 98 struct work_struct ws_awake_device; 99 struct work_struct ws_rx_vote_off; 100 struct work_struct ws_tx_vote_off; 101 unsigned long flags; 102 struct completion drop_ev_comp; 103 104 /* For debugging purpose */ 105 u64 ibs_sent_wacks; 106 u64 ibs_sent_slps; 107 u64 ibs_sent_wakes; 108 u64 ibs_recv_wacks; 109 u64 ibs_recv_slps; 110 u64 ibs_recv_wakes; 111 u64 vote_last_jif; 112 u32 vote_on_ms; 113 u32 vote_off_ms; 114 u64 tx_votes_on; 115 u64 rx_votes_on; 116 u64 tx_votes_off; 117 u64 rx_votes_off; 118 u64 votes_on; 119 u64 votes_off; 120 }; 121 122 enum qca_speed_type { 123 QCA_INIT_SPEED = 1, 124 QCA_OPER_SPEED 125 }; 126 127 /* 128 * Voltage regulator information required for configuring the 129 * QCA Bluetooth chipset 130 */ 131 struct qca_vreg { 132 const char *name; 133 unsigned int load_uA; 134 }; 135 136 struct qca_vreg_data { 137 enum qca_btsoc_type soc_type; 138 struct qca_vreg *vregs; 139 size_t num_vregs; 140 }; 141 142 /* 143 * Platform data for the QCA Bluetooth power driver. 144 */ 145 struct qca_power { 146 struct device *dev; 147 struct regulator_bulk_data *vreg_bulk; 148 int num_vregs; 149 bool vregs_on; 150 }; 151 152 struct qca_serdev { 153 struct hci_uart serdev_hu; 154 struct gpio_desc *bt_en; 155 struct clk *susclk; 156 enum qca_btsoc_type btsoc_type; 157 struct qca_power *bt_power; 158 u32 init_speed; 159 u32 oper_speed; 160 const char *firmware_name; 161 }; 162 163 static int qca_regulator_enable(struct qca_serdev *qcadev); 164 static void qca_regulator_disable(struct qca_serdev *qcadev); 165 static void qca_power_shutdown(struct hci_uart *hu); 166 static int qca_power_off(struct hci_dev *hdev); 167 168 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu) 169 { 170 enum qca_btsoc_type soc_type; 171 172 if (hu->serdev) { 173 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev); 174 175 soc_type = qsd->btsoc_type; 176 } else { 177 soc_type = QCA_ROME; 178 } 179 180 return soc_type; 181 } 182 183 static const char *qca_get_firmware_name(struct hci_uart *hu) 184 { 185 if (hu->serdev) { 186 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev); 187 188 return qsd->firmware_name; 189 } else { 190 return NULL; 191 } 192 } 193 194 static void __serial_clock_on(struct tty_struct *tty) 195 { 196 /* TODO: Some chipset requires to enable UART clock on client 197 * side to save power consumption or manual work is required. 198 * Please put your code to control UART clock here if needed 199 */ 200 } 201 202 static void __serial_clock_off(struct tty_struct *tty) 203 { 204 /* TODO: Some chipset requires to disable UART clock on client 205 * side to save power consumption or manual work is required. 206 * Please put your code to control UART clock off here if needed 207 */ 208 } 209 210 /* serial_clock_vote needs to be called with the ibs lock held */ 211 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu) 212 { 213 struct qca_data *qca = hu->priv; 214 unsigned int diff; 215 216 bool old_vote = (qca->tx_vote | qca->rx_vote); 217 bool new_vote; 218 219 switch (vote) { 220 case HCI_IBS_VOTE_STATS_UPDATE: 221 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); 222 223 if (old_vote) 224 qca->vote_off_ms += diff; 225 else 226 qca->vote_on_ms += diff; 227 return; 228 229 case HCI_IBS_TX_VOTE_CLOCK_ON: 230 qca->tx_vote = true; 231 qca->tx_votes_on++; 232 new_vote = true; 233 break; 234 235 case HCI_IBS_RX_VOTE_CLOCK_ON: 236 qca->rx_vote = true; 237 qca->rx_votes_on++; 238 new_vote = true; 239 break; 240 241 case HCI_IBS_TX_VOTE_CLOCK_OFF: 242 qca->tx_vote = false; 243 qca->tx_votes_off++; 244 new_vote = qca->rx_vote | qca->tx_vote; 245 break; 246 247 case HCI_IBS_RX_VOTE_CLOCK_OFF: 248 qca->rx_vote = false; 249 qca->rx_votes_off++; 250 new_vote = qca->rx_vote | qca->tx_vote; 251 break; 252 253 default: 254 BT_ERR("Voting irregularity"); 255 return; 256 } 257 258 if (new_vote != old_vote) { 259 if (new_vote) 260 __serial_clock_on(hu->tty); 261 else 262 __serial_clock_off(hu->tty); 263 264 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false", 265 vote ? "true" : "false"); 266 267 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); 268 269 if (new_vote) { 270 qca->votes_on++; 271 qca->vote_off_ms += diff; 272 } else { 273 qca->votes_off++; 274 qca->vote_on_ms += diff; 275 } 276 qca->vote_last_jif = jiffies; 277 } 278 } 279 280 /* Builds and sends an HCI_IBS command packet. 281 * These are very simple packets with only 1 cmd byte. 282 */ 283 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu) 284 { 285 int err = 0; 286 struct sk_buff *skb = NULL; 287 struct qca_data *qca = hu->priv; 288 289 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd); 290 291 skb = bt_skb_alloc(1, GFP_ATOMIC); 292 if (!skb) { 293 BT_ERR("Failed to allocate memory for HCI_IBS packet"); 294 return -ENOMEM; 295 } 296 297 /* Assign HCI_IBS type */ 298 skb_put_u8(skb, cmd); 299 300 skb_queue_tail(&qca->txq, skb); 301 302 return err; 303 } 304 305 static void qca_wq_awake_device(struct work_struct *work) 306 { 307 struct qca_data *qca = container_of(work, struct qca_data, 308 ws_awake_device); 309 struct hci_uart *hu = qca->hu; 310 unsigned long retrans_delay; 311 unsigned long flags; 312 313 BT_DBG("hu %p wq awake device", hu); 314 315 /* Vote for serial clock */ 316 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu); 317 318 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 319 320 /* Send wake indication to device */ 321 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) 322 BT_ERR("Failed to send WAKE to device"); 323 324 qca->ibs_sent_wakes++; 325 326 /* Start retransmit timer */ 327 retrans_delay = msecs_to_jiffies(qca->wake_retrans); 328 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); 329 330 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 331 332 /* Actually send the packets */ 333 hci_uart_tx_wakeup(hu); 334 } 335 336 static void qca_wq_awake_rx(struct work_struct *work) 337 { 338 struct qca_data *qca = container_of(work, struct qca_data, 339 ws_awake_rx); 340 struct hci_uart *hu = qca->hu; 341 unsigned long flags; 342 343 BT_DBG("hu %p wq awake rx", hu); 344 345 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu); 346 347 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 348 qca->rx_ibs_state = HCI_IBS_RX_AWAKE; 349 350 /* Always acknowledge device wake up, 351 * sending IBS message doesn't count as TX ON. 352 */ 353 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) 354 BT_ERR("Failed to acknowledge device wake up"); 355 356 qca->ibs_sent_wacks++; 357 358 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 359 360 /* Actually send the packets */ 361 hci_uart_tx_wakeup(hu); 362 } 363 364 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work) 365 { 366 struct qca_data *qca = container_of(work, struct qca_data, 367 ws_rx_vote_off); 368 struct hci_uart *hu = qca->hu; 369 370 BT_DBG("hu %p rx clock vote off", hu); 371 372 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu); 373 } 374 375 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work) 376 { 377 struct qca_data *qca = container_of(work, struct qca_data, 378 ws_tx_vote_off); 379 struct hci_uart *hu = qca->hu; 380 381 BT_DBG("hu %p tx clock vote off", hu); 382 383 /* Run HCI tx handling unlocked */ 384 hci_uart_tx_wakeup(hu); 385 386 /* Now that message queued to tty driver, vote for tty clocks off. 387 * It is up to the tty driver to pend the clocks off until tx done. 388 */ 389 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu); 390 } 391 392 static void hci_ibs_tx_idle_timeout(struct timer_list *t) 393 { 394 struct qca_data *qca = from_timer(qca, t, tx_idle_timer); 395 struct hci_uart *hu = qca->hu; 396 unsigned long flags; 397 398 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state); 399 400 spin_lock_irqsave_nested(&qca->hci_ibs_lock, 401 flags, SINGLE_DEPTH_NESTING); 402 403 switch (qca->tx_ibs_state) { 404 case HCI_IBS_TX_AWAKE: 405 /* TX_IDLE, go to SLEEP */ 406 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) { 407 BT_ERR("Failed to send SLEEP to device"); 408 break; 409 } 410 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; 411 qca->ibs_sent_slps++; 412 queue_work(qca->workqueue, &qca->ws_tx_vote_off); 413 break; 414 415 case HCI_IBS_TX_ASLEEP: 416 case HCI_IBS_TX_WAKING: 417 /* Fall through */ 418 419 default: 420 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); 421 break; 422 } 423 424 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 425 } 426 427 static void hci_ibs_wake_retrans_timeout(struct timer_list *t) 428 { 429 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer); 430 struct hci_uart *hu = qca->hu; 431 unsigned long flags, retrans_delay; 432 bool retransmit = false; 433 434 BT_DBG("hu %p wake retransmit timeout in %d state", 435 hu, qca->tx_ibs_state); 436 437 spin_lock_irqsave_nested(&qca->hci_ibs_lock, 438 flags, SINGLE_DEPTH_NESTING); 439 440 switch (qca->tx_ibs_state) { 441 case HCI_IBS_TX_WAKING: 442 /* No WAKE_ACK, retransmit WAKE */ 443 retransmit = true; 444 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) { 445 BT_ERR("Failed to acknowledge device wake up"); 446 break; 447 } 448 qca->ibs_sent_wakes++; 449 retrans_delay = msecs_to_jiffies(qca->wake_retrans); 450 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); 451 break; 452 453 case HCI_IBS_TX_ASLEEP: 454 case HCI_IBS_TX_AWAKE: 455 /* Fall through */ 456 457 default: 458 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); 459 break; 460 } 461 462 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 463 464 if (retransmit) 465 hci_uart_tx_wakeup(hu); 466 } 467 468 /* Initialize protocol */ 469 static int qca_open(struct hci_uart *hu) 470 { 471 struct qca_serdev *qcadev; 472 struct qca_data *qca; 473 int ret; 474 475 BT_DBG("hu %p qca_open", hu); 476 477 if (!hci_uart_has_flow_control(hu)) 478 return -EOPNOTSUPP; 479 480 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL); 481 if (!qca) 482 return -ENOMEM; 483 484 skb_queue_head_init(&qca->txq); 485 skb_queue_head_init(&qca->tx_wait_q); 486 spin_lock_init(&qca->hci_ibs_lock); 487 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0); 488 if (!qca->workqueue) { 489 BT_ERR("QCA Workqueue not initialized properly"); 490 kfree(qca); 491 return -ENOMEM; 492 } 493 494 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx); 495 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device); 496 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off); 497 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off); 498 499 qca->hu = hu; 500 init_completion(&qca->drop_ev_comp); 501 502 /* Assume we start with both sides asleep -- extra wakes OK */ 503 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; 504 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP; 505 506 qca->vote_last_jif = jiffies; 507 508 hu->priv = qca; 509 510 if (hu->serdev) { 511 512 qcadev = serdev_device_get_drvdata(hu->serdev); 513 if (!qca_is_wcn399x(qcadev->btsoc_type)) { 514 gpiod_set_value_cansleep(qcadev->bt_en, 1); 515 /* Controller needs time to bootup. */ 516 msleep(150); 517 } else { 518 hu->init_speed = qcadev->init_speed; 519 hu->oper_speed = qcadev->oper_speed; 520 ret = qca_regulator_enable(qcadev); 521 if (ret) { 522 destroy_workqueue(qca->workqueue); 523 kfree_skb(qca->rx_skb); 524 hu->priv = NULL; 525 kfree(qca); 526 return ret; 527 } 528 } 529 } 530 531 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0); 532 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS; 533 534 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0); 535 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS; 536 537 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u", 538 qca->tx_idle_delay, qca->wake_retrans); 539 540 return 0; 541 } 542 543 static void qca_debugfs_init(struct hci_dev *hdev) 544 { 545 struct hci_uart *hu = hci_get_drvdata(hdev); 546 struct qca_data *qca = hu->priv; 547 struct dentry *ibs_dir; 548 umode_t mode; 549 550 if (!hdev->debugfs) 551 return; 552 553 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs); 554 555 /* read only */ 556 mode = S_IRUGO; 557 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state); 558 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state); 559 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir, 560 &qca->ibs_sent_slps); 561 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir, 562 &qca->ibs_sent_wakes); 563 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir, 564 &qca->ibs_sent_wacks); 565 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir, 566 &qca->ibs_recv_slps); 567 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir, 568 &qca->ibs_recv_wakes); 569 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir, 570 &qca->ibs_recv_wacks); 571 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote); 572 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on); 573 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off); 574 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote); 575 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on); 576 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off); 577 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on); 578 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off); 579 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms); 580 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms); 581 582 /* read/write */ 583 mode = S_IRUGO | S_IWUSR; 584 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans); 585 debugfs_create_u32("tx_idle_delay", mode, ibs_dir, 586 &qca->tx_idle_delay); 587 } 588 589 /* Flush protocol data */ 590 static int qca_flush(struct hci_uart *hu) 591 { 592 struct qca_data *qca = hu->priv; 593 594 BT_DBG("hu %p qca flush", hu); 595 596 skb_queue_purge(&qca->tx_wait_q); 597 skb_queue_purge(&qca->txq); 598 599 return 0; 600 } 601 602 /* Close protocol */ 603 static int qca_close(struct hci_uart *hu) 604 { 605 struct qca_serdev *qcadev; 606 struct qca_data *qca = hu->priv; 607 608 BT_DBG("hu %p qca close", hu); 609 610 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu); 611 612 skb_queue_purge(&qca->tx_wait_q); 613 skb_queue_purge(&qca->txq); 614 del_timer(&qca->tx_idle_timer); 615 del_timer(&qca->wake_retrans_timer); 616 destroy_workqueue(qca->workqueue); 617 qca->hu = NULL; 618 619 if (hu->serdev) { 620 qcadev = serdev_device_get_drvdata(hu->serdev); 621 if (qca_is_wcn399x(qcadev->btsoc_type)) 622 qca_power_shutdown(hu); 623 else 624 gpiod_set_value_cansleep(qcadev->bt_en, 0); 625 626 } 627 628 kfree_skb(qca->rx_skb); 629 630 hu->priv = NULL; 631 632 kfree(qca); 633 634 return 0; 635 } 636 637 /* Called upon a wake-up-indication from the device. 638 */ 639 static void device_want_to_wakeup(struct hci_uart *hu) 640 { 641 unsigned long flags; 642 struct qca_data *qca = hu->priv; 643 644 BT_DBG("hu %p want to wake up", hu); 645 646 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 647 648 qca->ibs_recv_wakes++; 649 650 switch (qca->rx_ibs_state) { 651 case HCI_IBS_RX_ASLEEP: 652 /* Make sure clock is on - we may have turned clock off since 653 * receiving the wake up indicator awake rx clock. 654 */ 655 queue_work(qca->workqueue, &qca->ws_awake_rx); 656 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 657 return; 658 659 case HCI_IBS_RX_AWAKE: 660 /* Always acknowledge device wake up, 661 * sending IBS message doesn't count as TX ON. 662 */ 663 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) { 664 BT_ERR("Failed to acknowledge device wake up"); 665 break; 666 } 667 qca->ibs_sent_wacks++; 668 break; 669 670 default: 671 /* Any other state is illegal */ 672 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d", 673 qca->rx_ibs_state); 674 break; 675 } 676 677 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 678 679 /* Actually send the packets */ 680 hci_uart_tx_wakeup(hu); 681 } 682 683 /* Called upon a sleep-indication from the device. 684 */ 685 static void device_want_to_sleep(struct hci_uart *hu) 686 { 687 unsigned long flags; 688 struct qca_data *qca = hu->priv; 689 690 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state); 691 692 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 693 694 qca->ibs_recv_slps++; 695 696 switch (qca->rx_ibs_state) { 697 case HCI_IBS_RX_AWAKE: 698 /* Update state */ 699 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP; 700 /* Vote off rx clock under workqueue */ 701 queue_work(qca->workqueue, &qca->ws_rx_vote_off); 702 break; 703 704 case HCI_IBS_RX_ASLEEP: 705 break; 706 707 default: 708 /* Any other state is illegal */ 709 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d", 710 qca->rx_ibs_state); 711 break; 712 } 713 714 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 715 } 716 717 /* Called upon wake-up-acknowledgement from the device 718 */ 719 static void device_woke_up(struct hci_uart *hu) 720 { 721 unsigned long flags, idle_delay; 722 struct qca_data *qca = hu->priv; 723 struct sk_buff *skb = NULL; 724 725 BT_DBG("hu %p woke up", hu); 726 727 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 728 729 qca->ibs_recv_wacks++; 730 731 switch (qca->tx_ibs_state) { 732 case HCI_IBS_TX_AWAKE: 733 /* Expect one if we send 2 WAKEs */ 734 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d", 735 qca->tx_ibs_state); 736 break; 737 738 case HCI_IBS_TX_WAKING: 739 /* Send pending packets */ 740 while ((skb = skb_dequeue(&qca->tx_wait_q))) 741 skb_queue_tail(&qca->txq, skb); 742 743 /* Switch timers and change state to HCI_IBS_TX_AWAKE */ 744 del_timer(&qca->wake_retrans_timer); 745 idle_delay = msecs_to_jiffies(qca->tx_idle_delay); 746 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay); 747 qca->tx_ibs_state = HCI_IBS_TX_AWAKE; 748 break; 749 750 case HCI_IBS_TX_ASLEEP: 751 /* Fall through */ 752 753 default: 754 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d", 755 qca->tx_ibs_state); 756 break; 757 } 758 759 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 760 761 /* Actually send the packets */ 762 hci_uart_tx_wakeup(hu); 763 } 764 765 /* Enqueue frame for transmittion (padding, crc, etc) may be called from 766 * two simultaneous tasklets. 767 */ 768 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb) 769 { 770 unsigned long flags = 0, idle_delay; 771 struct qca_data *qca = hu->priv; 772 773 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb, 774 qca->tx_ibs_state); 775 776 /* Prepend skb with frame type */ 777 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); 778 779 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 780 781 /* Don't go to sleep in middle of patch download or 782 * Out-Of-Band(GPIOs control) sleep is selected. 783 */ 784 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) { 785 skb_queue_tail(&qca->txq, skb); 786 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 787 return 0; 788 } 789 790 /* Act according to current state */ 791 switch (qca->tx_ibs_state) { 792 case HCI_IBS_TX_AWAKE: 793 BT_DBG("Device awake, sending normally"); 794 skb_queue_tail(&qca->txq, skb); 795 idle_delay = msecs_to_jiffies(qca->tx_idle_delay); 796 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay); 797 break; 798 799 case HCI_IBS_TX_ASLEEP: 800 BT_DBG("Device asleep, waking up and queueing packet"); 801 /* Save packet for later */ 802 skb_queue_tail(&qca->tx_wait_q, skb); 803 804 qca->tx_ibs_state = HCI_IBS_TX_WAKING; 805 /* Schedule a work queue to wake up device */ 806 queue_work(qca->workqueue, &qca->ws_awake_device); 807 break; 808 809 case HCI_IBS_TX_WAKING: 810 BT_DBG("Device waking up, queueing packet"); 811 /* Transient state; just keep packet for later */ 812 skb_queue_tail(&qca->tx_wait_q, skb); 813 break; 814 815 default: 816 BT_ERR("Illegal tx state: %d (losing packet)", 817 qca->tx_ibs_state); 818 kfree_skb(skb); 819 break; 820 } 821 822 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 823 824 return 0; 825 } 826 827 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb) 828 { 829 struct hci_uart *hu = hci_get_drvdata(hdev); 830 831 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND); 832 833 device_want_to_sleep(hu); 834 835 kfree_skb(skb); 836 return 0; 837 } 838 839 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb) 840 { 841 struct hci_uart *hu = hci_get_drvdata(hdev); 842 843 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND); 844 845 device_want_to_wakeup(hu); 846 847 kfree_skb(skb); 848 return 0; 849 } 850 851 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb) 852 { 853 struct hci_uart *hu = hci_get_drvdata(hdev); 854 855 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK); 856 857 device_woke_up(hu); 858 859 kfree_skb(skb); 860 return 0; 861 } 862 863 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb) 864 { 865 /* We receive debug logs from chip as an ACL packets. 866 * Instead of sending the data to ACL to decode the 867 * received data, we are pushing them to the above layers 868 * as a diagnostic packet. 869 */ 870 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE) 871 return hci_recv_diag(hdev, skb); 872 873 return hci_recv_frame(hdev, skb); 874 } 875 876 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb) 877 { 878 struct hci_uart *hu = hci_get_drvdata(hdev); 879 struct qca_data *qca = hu->priv; 880 881 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) { 882 struct hci_event_hdr *hdr = (void *)skb->data; 883 884 /* For the WCN3990 the vendor command for a baudrate change 885 * isn't sent as synchronous HCI command, because the 886 * controller sends the corresponding vendor event with the 887 * new baudrate. The event is received and properly decoded 888 * after changing the baudrate of the host port. It needs to 889 * be dropped, otherwise it can be misinterpreted as 890 * response to a later firmware download command (also a 891 * vendor command). 892 */ 893 894 if (hdr->evt == HCI_EV_VENDOR) 895 complete(&qca->drop_ev_comp); 896 897 kfree_skb(skb); 898 899 return 0; 900 } 901 902 return hci_recv_frame(hdev, skb); 903 } 904 905 #define QCA_IBS_SLEEP_IND_EVENT \ 906 .type = HCI_IBS_SLEEP_IND, \ 907 .hlen = 0, \ 908 .loff = 0, \ 909 .lsize = 0, \ 910 .maxlen = HCI_MAX_IBS_SIZE 911 912 #define QCA_IBS_WAKE_IND_EVENT \ 913 .type = HCI_IBS_WAKE_IND, \ 914 .hlen = 0, \ 915 .loff = 0, \ 916 .lsize = 0, \ 917 .maxlen = HCI_MAX_IBS_SIZE 918 919 #define QCA_IBS_WAKE_ACK_EVENT \ 920 .type = HCI_IBS_WAKE_ACK, \ 921 .hlen = 0, \ 922 .loff = 0, \ 923 .lsize = 0, \ 924 .maxlen = HCI_MAX_IBS_SIZE 925 926 static const struct h4_recv_pkt qca_recv_pkts[] = { 927 { H4_RECV_ACL, .recv = qca_recv_acl_data }, 928 { H4_RECV_SCO, .recv = hci_recv_frame }, 929 { H4_RECV_EVENT, .recv = qca_recv_event }, 930 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind }, 931 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack }, 932 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind }, 933 }; 934 935 static int qca_recv(struct hci_uart *hu, const void *data, int count) 936 { 937 struct qca_data *qca = hu->priv; 938 939 if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 940 return -EUNATCH; 941 942 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count, 943 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts)); 944 if (IS_ERR(qca->rx_skb)) { 945 int err = PTR_ERR(qca->rx_skb); 946 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); 947 qca->rx_skb = NULL; 948 return err; 949 } 950 951 return count; 952 } 953 954 static struct sk_buff *qca_dequeue(struct hci_uart *hu) 955 { 956 struct qca_data *qca = hu->priv; 957 958 return skb_dequeue(&qca->txq); 959 } 960 961 static uint8_t qca_get_baudrate_value(int speed) 962 { 963 switch (speed) { 964 case 9600: 965 return QCA_BAUDRATE_9600; 966 case 19200: 967 return QCA_BAUDRATE_19200; 968 case 38400: 969 return QCA_BAUDRATE_38400; 970 case 57600: 971 return QCA_BAUDRATE_57600; 972 case 115200: 973 return QCA_BAUDRATE_115200; 974 case 230400: 975 return QCA_BAUDRATE_230400; 976 case 460800: 977 return QCA_BAUDRATE_460800; 978 case 500000: 979 return QCA_BAUDRATE_500000; 980 case 921600: 981 return QCA_BAUDRATE_921600; 982 case 1000000: 983 return QCA_BAUDRATE_1000000; 984 case 2000000: 985 return QCA_BAUDRATE_2000000; 986 case 3000000: 987 return QCA_BAUDRATE_3000000; 988 case 3200000: 989 return QCA_BAUDRATE_3200000; 990 case 3500000: 991 return QCA_BAUDRATE_3500000; 992 default: 993 return QCA_BAUDRATE_115200; 994 } 995 } 996 997 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate) 998 { 999 struct hci_uart *hu = hci_get_drvdata(hdev); 1000 struct qca_data *qca = hu->priv; 1001 struct sk_buff *skb; 1002 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 }; 1003 1004 if (baudrate > QCA_BAUDRATE_3200000) 1005 return -EINVAL; 1006 1007 cmd[4] = baudrate; 1008 1009 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); 1010 if (!skb) { 1011 bt_dev_err(hdev, "Failed to allocate baudrate packet"); 1012 return -ENOMEM; 1013 } 1014 1015 /* Assign commands to change baudrate and packet type. */ 1016 skb_put_data(skb, cmd, sizeof(cmd)); 1017 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; 1018 1019 skb_queue_tail(&qca->txq, skb); 1020 hci_uart_tx_wakeup(hu); 1021 1022 /* Wait for the baudrate change request to be sent */ 1023 1024 while (!skb_queue_empty(&qca->txq)) 1025 usleep_range(100, 200); 1026 1027 if (hu->serdev) 1028 serdev_device_wait_until_sent(hu->serdev, 1029 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS)); 1030 1031 /* Give the controller time to process the request */ 1032 if (qca_is_wcn399x(qca_soc_type(hu))) 1033 msleep(10); 1034 else 1035 msleep(300); 1036 1037 return 0; 1038 } 1039 1040 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed) 1041 { 1042 if (hu->serdev) 1043 serdev_device_set_baudrate(hu->serdev, speed); 1044 else 1045 hci_uart_set_baudrate(hu, speed); 1046 } 1047 1048 static int qca_send_power_pulse(struct hci_uart *hu, bool on) 1049 { 1050 int ret; 1051 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS); 1052 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE; 1053 1054 /* These power pulses are single byte command which are sent 1055 * at required baudrate to wcn3990. On wcn3990, we have an external 1056 * circuit at Tx pin which decodes the pulse sent at specific baudrate. 1057 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT 1058 * and also we use the same power inputs to turn on and off for 1059 * Wi-Fi/BT. Powering up the power sources will not enable BT, until 1060 * we send a power on pulse at 115200 bps. This algorithm will help to 1061 * save power. Disabling hardware flow control is mandatory while 1062 * sending power pulses to SoC. 1063 */ 1064 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd); 1065 1066 serdev_device_write_flush(hu->serdev); 1067 hci_uart_set_flow_control(hu, true); 1068 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd)); 1069 if (ret < 0) { 1070 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd); 1071 return ret; 1072 } 1073 1074 serdev_device_wait_until_sent(hu->serdev, timeout); 1075 hci_uart_set_flow_control(hu, false); 1076 1077 /* Give to controller time to boot/shutdown */ 1078 if (on) 1079 msleep(100); 1080 else 1081 msleep(10); 1082 1083 return 0; 1084 } 1085 1086 static unsigned int qca_get_speed(struct hci_uart *hu, 1087 enum qca_speed_type speed_type) 1088 { 1089 unsigned int speed = 0; 1090 1091 if (speed_type == QCA_INIT_SPEED) { 1092 if (hu->init_speed) 1093 speed = hu->init_speed; 1094 else if (hu->proto->init_speed) 1095 speed = hu->proto->init_speed; 1096 } else { 1097 if (hu->oper_speed) 1098 speed = hu->oper_speed; 1099 else if (hu->proto->oper_speed) 1100 speed = hu->proto->oper_speed; 1101 } 1102 1103 return speed; 1104 } 1105 1106 static int qca_check_speeds(struct hci_uart *hu) 1107 { 1108 if (qca_is_wcn399x(qca_soc_type(hu))) { 1109 if (!qca_get_speed(hu, QCA_INIT_SPEED) && 1110 !qca_get_speed(hu, QCA_OPER_SPEED)) 1111 return -EINVAL; 1112 } else { 1113 if (!qca_get_speed(hu, QCA_INIT_SPEED) || 1114 !qca_get_speed(hu, QCA_OPER_SPEED)) 1115 return -EINVAL; 1116 } 1117 1118 return 0; 1119 } 1120 1121 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type) 1122 { 1123 unsigned int speed, qca_baudrate; 1124 struct qca_data *qca = hu->priv; 1125 int ret = 0; 1126 1127 if (speed_type == QCA_INIT_SPEED) { 1128 speed = qca_get_speed(hu, QCA_INIT_SPEED); 1129 if (speed) 1130 host_set_baudrate(hu, speed); 1131 } else { 1132 enum qca_btsoc_type soc_type = qca_soc_type(hu); 1133 1134 speed = qca_get_speed(hu, QCA_OPER_SPEED); 1135 if (!speed) 1136 return 0; 1137 1138 /* Disable flow control for wcn3990 to deassert RTS while 1139 * changing the baudrate of chip and host. 1140 */ 1141 if (qca_is_wcn399x(soc_type)) 1142 hci_uart_set_flow_control(hu, true); 1143 1144 if (soc_type == QCA_WCN3990) { 1145 reinit_completion(&qca->drop_ev_comp); 1146 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags); 1147 } 1148 1149 qca_baudrate = qca_get_baudrate_value(speed); 1150 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed); 1151 ret = qca_set_baudrate(hu->hdev, qca_baudrate); 1152 if (ret) 1153 goto error; 1154 1155 host_set_baudrate(hu, speed); 1156 1157 error: 1158 if (qca_is_wcn399x(soc_type)) 1159 hci_uart_set_flow_control(hu, false); 1160 1161 if (soc_type == QCA_WCN3990) { 1162 /* Wait for the controller to send the vendor event 1163 * for the baudrate change command. 1164 */ 1165 if (!wait_for_completion_timeout(&qca->drop_ev_comp, 1166 msecs_to_jiffies(100))) { 1167 bt_dev_err(hu->hdev, 1168 "Failed to change controller baudrate\n"); 1169 ret = -ETIMEDOUT; 1170 } 1171 1172 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags); 1173 } 1174 } 1175 1176 return ret; 1177 } 1178 1179 static int qca_wcn3990_init(struct hci_uart *hu) 1180 { 1181 struct qca_serdev *qcadev; 1182 int ret; 1183 1184 /* Check for vregs status, may be hci down has turned 1185 * off the voltage regulator. 1186 */ 1187 qcadev = serdev_device_get_drvdata(hu->serdev); 1188 if (!qcadev->bt_power->vregs_on) { 1189 serdev_device_close(hu->serdev); 1190 ret = qca_regulator_enable(qcadev); 1191 if (ret) 1192 return ret; 1193 1194 ret = serdev_device_open(hu->serdev); 1195 if (ret) { 1196 bt_dev_err(hu->hdev, "failed to open port"); 1197 return ret; 1198 } 1199 } 1200 1201 /* Forcefully enable wcn3990 to enter in to boot mode. */ 1202 host_set_baudrate(hu, 2400); 1203 ret = qca_send_power_pulse(hu, false); 1204 if (ret) 1205 return ret; 1206 1207 qca_set_speed(hu, QCA_INIT_SPEED); 1208 ret = qca_send_power_pulse(hu, true); 1209 if (ret) 1210 return ret; 1211 1212 /* Now the device is in ready state to communicate with host. 1213 * To sync host with device we need to reopen port. 1214 * Without this, we will have RTS and CTS synchronization 1215 * issues. 1216 */ 1217 serdev_device_close(hu->serdev); 1218 ret = serdev_device_open(hu->serdev); 1219 if (ret) { 1220 bt_dev_err(hu->hdev, "failed to open port"); 1221 return ret; 1222 } 1223 1224 hci_uart_set_flow_control(hu, false); 1225 1226 return 0; 1227 } 1228 1229 static int qca_setup(struct hci_uart *hu) 1230 { 1231 struct hci_dev *hdev = hu->hdev; 1232 struct qca_data *qca = hu->priv; 1233 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200; 1234 enum qca_btsoc_type soc_type = qca_soc_type(hu); 1235 const char *firmware_name = qca_get_firmware_name(hu); 1236 int ret; 1237 int soc_ver = 0; 1238 1239 ret = qca_check_speeds(hu); 1240 if (ret) 1241 return ret; 1242 1243 /* Patch downloading has to be done without IBS mode */ 1244 clear_bit(QCA_IBS_ENABLED, &qca->flags); 1245 1246 /* Enable controller to do both LE scan and BR/EDR inquiry 1247 * simultaneously. 1248 */ 1249 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); 1250 1251 if (qca_is_wcn399x(soc_type)) { 1252 bt_dev_info(hdev, "setting up wcn3990"); 1253 1254 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute 1255 * setup for every hci up. 1256 */ 1257 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks); 1258 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks); 1259 hu->hdev->shutdown = qca_power_off; 1260 ret = qca_wcn3990_init(hu); 1261 if (ret) 1262 return ret; 1263 1264 ret = qca_read_soc_version(hdev, &soc_ver); 1265 if (ret) 1266 return ret; 1267 } else { 1268 bt_dev_info(hdev, "ROME setup"); 1269 qca_set_speed(hu, QCA_INIT_SPEED); 1270 } 1271 1272 /* Setup user speed if needed */ 1273 speed = qca_get_speed(hu, QCA_OPER_SPEED); 1274 if (speed) { 1275 ret = qca_set_speed(hu, QCA_OPER_SPEED); 1276 if (ret) 1277 return ret; 1278 1279 qca_baudrate = qca_get_baudrate_value(speed); 1280 } 1281 1282 if (!qca_is_wcn399x(soc_type)) { 1283 /* Get QCA version information */ 1284 ret = qca_read_soc_version(hdev, &soc_ver); 1285 if (ret) 1286 return ret; 1287 } 1288 1289 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver); 1290 /* Setup patch / NVM configurations */ 1291 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver, 1292 firmware_name); 1293 if (!ret) { 1294 set_bit(QCA_IBS_ENABLED, &qca->flags); 1295 qca_debugfs_init(hdev); 1296 } else if (ret == -ENOENT) { 1297 /* No patch/nvm-config found, run with original fw/config */ 1298 ret = 0; 1299 } else if (ret == -EAGAIN) { 1300 /* 1301 * Userspace firmware loader will return -EAGAIN in case no 1302 * patch/nvm-config is found, so run with original fw/config. 1303 */ 1304 ret = 0; 1305 } 1306 1307 /* Setup bdaddr */ 1308 if (qca_is_wcn399x(soc_type)) 1309 hu->hdev->set_bdaddr = qca_set_bdaddr; 1310 else 1311 hu->hdev->set_bdaddr = qca_set_bdaddr_rome; 1312 1313 return ret; 1314 } 1315 1316 static const struct hci_uart_proto qca_proto = { 1317 .id = HCI_UART_QCA, 1318 .name = "QCA", 1319 .manufacturer = 29, 1320 .init_speed = 115200, 1321 .oper_speed = 3000000, 1322 .open = qca_open, 1323 .close = qca_close, 1324 .flush = qca_flush, 1325 .setup = qca_setup, 1326 .recv = qca_recv, 1327 .enqueue = qca_enqueue, 1328 .dequeue = qca_dequeue, 1329 }; 1330 1331 static const struct qca_vreg_data qca_soc_data_wcn3990 = { 1332 .soc_type = QCA_WCN3990, 1333 .vregs = (struct qca_vreg []) { 1334 { "vddio", 15000 }, 1335 { "vddxo", 80000 }, 1336 { "vddrf", 300000 }, 1337 { "vddch0", 450000 }, 1338 }, 1339 .num_vregs = 4, 1340 }; 1341 1342 static const struct qca_vreg_data qca_soc_data_wcn3998 = { 1343 .soc_type = QCA_WCN3998, 1344 .vregs = (struct qca_vreg []) { 1345 { "vddio", 10000 }, 1346 { "vddxo", 80000 }, 1347 { "vddrf", 300000 }, 1348 { "vddch0", 450000 }, 1349 }, 1350 .num_vregs = 4, 1351 }; 1352 1353 static void qca_power_shutdown(struct hci_uart *hu) 1354 { 1355 struct qca_serdev *qcadev; 1356 struct qca_data *qca = hu->priv; 1357 unsigned long flags; 1358 1359 qcadev = serdev_device_get_drvdata(hu->serdev); 1360 1361 /* From this point we go into power off state. But serial port is 1362 * still open, stop queueing the IBS data and flush all the buffered 1363 * data in skb's. 1364 */ 1365 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 1366 clear_bit(QCA_IBS_ENABLED, &qca->flags); 1367 qca_flush(hu); 1368 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 1369 1370 host_set_baudrate(hu, 2400); 1371 qca_send_power_pulse(hu, false); 1372 qca_regulator_disable(qcadev); 1373 } 1374 1375 static int qca_power_off(struct hci_dev *hdev) 1376 { 1377 struct hci_uart *hu = hci_get_drvdata(hdev); 1378 1379 /* Perform pre shutdown command */ 1380 qca_send_pre_shutdown_cmd(hdev); 1381 1382 usleep_range(8000, 10000); 1383 1384 qca_power_shutdown(hu); 1385 return 0; 1386 } 1387 1388 static int qca_regulator_enable(struct qca_serdev *qcadev) 1389 { 1390 struct qca_power *power = qcadev->bt_power; 1391 int ret; 1392 1393 /* Already enabled */ 1394 if (power->vregs_on) 1395 return 0; 1396 1397 BT_DBG("enabling %d regulators)", power->num_vregs); 1398 1399 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk); 1400 if (ret) 1401 return ret; 1402 1403 power->vregs_on = true; 1404 1405 return 0; 1406 } 1407 1408 static void qca_regulator_disable(struct qca_serdev *qcadev) 1409 { 1410 struct qca_power *power; 1411 1412 if (!qcadev) 1413 return; 1414 1415 power = qcadev->bt_power; 1416 1417 /* Already disabled? */ 1418 if (!power->vregs_on) 1419 return; 1420 1421 regulator_bulk_disable(power->num_vregs, power->vreg_bulk); 1422 power->vregs_on = false; 1423 } 1424 1425 static int qca_init_regulators(struct qca_power *qca, 1426 const struct qca_vreg *vregs, size_t num_vregs) 1427 { 1428 struct regulator_bulk_data *bulk; 1429 int ret; 1430 int i; 1431 1432 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL); 1433 if (!bulk) 1434 return -ENOMEM; 1435 1436 for (i = 0; i < num_vregs; i++) 1437 bulk[i].supply = vregs[i].name; 1438 1439 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk); 1440 if (ret < 0) 1441 return ret; 1442 1443 for (i = 0; i < num_vregs; i++) { 1444 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA); 1445 if (ret) 1446 return ret; 1447 } 1448 1449 qca->vreg_bulk = bulk; 1450 qca->num_vregs = num_vregs; 1451 1452 return 0; 1453 } 1454 1455 static int qca_serdev_probe(struct serdev_device *serdev) 1456 { 1457 struct qca_serdev *qcadev; 1458 const struct qca_vreg_data *data; 1459 int err; 1460 1461 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL); 1462 if (!qcadev) 1463 return -ENOMEM; 1464 1465 qcadev->serdev_hu.serdev = serdev; 1466 data = of_device_get_match_data(&serdev->dev); 1467 serdev_device_set_drvdata(serdev, qcadev); 1468 device_property_read_string(&serdev->dev, "firmware-name", 1469 &qcadev->firmware_name); 1470 if (data && qca_is_wcn399x(data->soc_type)) { 1471 qcadev->btsoc_type = data->soc_type; 1472 qcadev->bt_power = devm_kzalloc(&serdev->dev, 1473 sizeof(struct qca_power), 1474 GFP_KERNEL); 1475 if (!qcadev->bt_power) 1476 return -ENOMEM; 1477 1478 qcadev->bt_power->dev = &serdev->dev; 1479 err = qca_init_regulators(qcadev->bt_power, data->vregs, 1480 data->num_vregs); 1481 if (err) { 1482 BT_ERR("Failed to init regulators:%d", err); 1483 goto out; 1484 } 1485 1486 qcadev->bt_power->vregs_on = false; 1487 1488 device_property_read_u32(&serdev->dev, "max-speed", 1489 &qcadev->oper_speed); 1490 if (!qcadev->oper_speed) 1491 BT_DBG("UART will pick default operating speed"); 1492 1493 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); 1494 if (err) { 1495 BT_ERR("wcn3990 serdev registration failed"); 1496 goto out; 1497 } 1498 } else { 1499 qcadev->btsoc_type = QCA_ROME; 1500 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable", 1501 GPIOD_OUT_LOW); 1502 if (IS_ERR(qcadev->bt_en)) { 1503 dev_err(&serdev->dev, "failed to acquire enable gpio\n"); 1504 return PTR_ERR(qcadev->bt_en); 1505 } 1506 1507 qcadev->susclk = devm_clk_get(&serdev->dev, NULL); 1508 if (IS_ERR(qcadev->susclk)) { 1509 dev_err(&serdev->dev, "failed to acquire clk\n"); 1510 return PTR_ERR(qcadev->susclk); 1511 } 1512 1513 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ); 1514 if (err) 1515 return err; 1516 1517 err = clk_prepare_enable(qcadev->susclk); 1518 if (err) 1519 return err; 1520 1521 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); 1522 if (err) 1523 clk_disable_unprepare(qcadev->susclk); 1524 } 1525 1526 out: return err; 1527 1528 } 1529 1530 static void qca_serdev_remove(struct serdev_device *serdev) 1531 { 1532 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev); 1533 1534 if (qca_is_wcn399x(qcadev->btsoc_type)) 1535 qca_power_shutdown(&qcadev->serdev_hu); 1536 else 1537 clk_disable_unprepare(qcadev->susclk); 1538 1539 hci_uart_unregister_device(&qcadev->serdev_hu); 1540 } 1541 1542 static const struct of_device_id qca_bluetooth_of_match[] = { 1543 { .compatible = "qcom,qca6174-bt" }, 1544 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990}, 1545 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998}, 1546 { /* sentinel */ } 1547 }; 1548 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match); 1549 1550 static struct serdev_device_driver qca_serdev_driver = { 1551 .probe = qca_serdev_probe, 1552 .remove = qca_serdev_remove, 1553 .driver = { 1554 .name = "hci_uart_qca", 1555 .of_match_table = qca_bluetooth_of_match, 1556 }, 1557 }; 1558 1559 int __init qca_init(void) 1560 { 1561 serdev_device_driver_register(&qca_serdev_driver); 1562 1563 return hci_uart_register_proto(&qca_proto); 1564 } 1565 1566 int __exit qca_deinit(void) 1567 { 1568 serdev_device_driver_unregister(&qca_serdev_driver); 1569 1570 return hci_uart_unregister_proto(&qca_proto); 1571 } 1572