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