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