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/devcoredump.h> 24 #include <linux/device.h> 25 #include <linux/gpio/consumer.h> 26 #include <linux/mod_devicetable.h> 27 #include <linux/module.h> 28 #include <linux/of_device.h> 29 #include <linux/acpi.h> 30 #include <linux/platform_device.h> 31 #include <linux/regulator/consumer.h> 32 #include <linux/serdev.h> 33 #include <linux/mutex.h> 34 #include <asm/unaligned.h> 35 36 #include <net/bluetooth/bluetooth.h> 37 #include <net/bluetooth/hci_core.h> 38 39 #include "hci_uart.h" 40 #include "btqca.h" 41 42 /* HCI_IBS protocol messages */ 43 #define HCI_IBS_SLEEP_IND 0xFE 44 #define HCI_IBS_WAKE_IND 0xFD 45 #define HCI_IBS_WAKE_ACK 0xFC 46 #define HCI_MAX_IBS_SIZE 10 47 48 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100 49 #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40 50 #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000 51 #define CMD_TRANS_TIMEOUT_MS 100 52 #define MEMDUMP_TIMEOUT_MS 8000 53 54 /* susclk rate */ 55 #define SUSCLK_RATE_32KHZ 32768 56 57 /* Controller debug log header */ 58 #define QCA_DEBUG_HANDLE 0x2EDC 59 60 /* max retry count when init fails */ 61 #define MAX_INIT_RETRIES 3 62 63 /* Controller dump header */ 64 #define QCA_SSR_DUMP_HANDLE 0x0108 65 #define QCA_DUMP_PACKET_SIZE 255 66 #define QCA_LAST_SEQUENCE_NUM 0xFFFF 67 #define QCA_CRASHBYTE_PACKET_LEN 1096 68 #define QCA_MEMDUMP_BYTE 0xFB 69 70 enum qca_flags { 71 QCA_IBS_ENABLED, 72 QCA_DROP_VENDOR_EVENT, 73 QCA_SUSPENDING, 74 QCA_MEMDUMP_COLLECTION, 75 QCA_HW_ERROR_EVENT 76 }; 77 78 79 /* HCI_IBS transmit side sleep protocol states */ 80 enum tx_ibs_states { 81 HCI_IBS_TX_ASLEEP, 82 HCI_IBS_TX_WAKING, 83 HCI_IBS_TX_AWAKE, 84 }; 85 86 /* HCI_IBS receive side sleep protocol states */ 87 enum rx_states { 88 HCI_IBS_RX_ASLEEP, 89 HCI_IBS_RX_AWAKE, 90 }; 91 92 /* HCI_IBS transmit and receive side clock state vote */ 93 enum hci_ibs_clock_state_vote { 94 HCI_IBS_VOTE_STATS_UPDATE, 95 HCI_IBS_TX_VOTE_CLOCK_ON, 96 HCI_IBS_TX_VOTE_CLOCK_OFF, 97 HCI_IBS_RX_VOTE_CLOCK_ON, 98 HCI_IBS_RX_VOTE_CLOCK_OFF, 99 }; 100 101 /* Controller memory dump states */ 102 enum qca_memdump_states { 103 QCA_MEMDUMP_IDLE, 104 QCA_MEMDUMP_COLLECTING, 105 QCA_MEMDUMP_COLLECTED, 106 QCA_MEMDUMP_TIMEOUT, 107 }; 108 109 struct qca_memdump_data { 110 char *memdump_buf_head; 111 char *memdump_buf_tail; 112 u32 current_seq_no; 113 u32 received_dump; 114 }; 115 116 struct qca_memdump_event_hdr { 117 __u8 evt; 118 __u8 plen; 119 __u16 opcode; 120 __u16 seq_no; 121 __u8 reserved; 122 } __packed; 123 124 125 struct qca_dump_size { 126 u32 dump_size; 127 } __packed; 128 129 struct qca_data { 130 struct hci_uart *hu; 131 struct sk_buff *rx_skb; 132 struct sk_buff_head txq; 133 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */ 134 struct sk_buff_head rx_memdump_q; /* Memdump wait queue */ 135 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */ 136 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/ 137 u8 rx_ibs_state; /* HCI_IBS receive side power state */ 138 bool tx_vote; /* Clock must be on for TX */ 139 bool rx_vote; /* Clock must be on for RX */ 140 struct timer_list tx_idle_timer; 141 u32 tx_idle_delay; 142 struct timer_list wake_retrans_timer; 143 u32 wake_retrans; 144 struct workqueue_struct *workqueue; 145 struct work_struct ws_awake_rx; 146 struct work_struct ws_awake_device; 147 struct work_struct ws_rx_vote_off; 148 struct work_struct ws_tx_vote_off; 149 struct work_struct ctrl_memdump_evt; 150 struct delayed_work ctrl_memdump_timeout; 151 struct qca_memdump_data *qca_memdump; 152 unsigned long flags; 153 struct completion drop_ev_comp; 154 wait_queue_head_t suspend_wait_q; 155 enum qca_memdump_states memdump_state; 156 struct mutex hci_memdump_lock; 157 158 /* For debugging purpose */ 159 u64 ibs_sent_wacks; 160 u64 ibs_sent_slps; 161 u64 ibs_sent_wakes; 162 u64 ibs_recv_wacks; 163 u64 ibs_recv_slps; 164 u64 ibs_recv_wakes; 165 u64 vote_last_jif; 166 u32 vote_on_ms; 167 u32 vote_off_ms; 168 u64 tx_votes_on; 169 u64 rx_votes_on; 170 u64 tx_votes_off; 171 u64 rx_votes_off; 172 u64 votes_on; 173 u64 votes_off; 174 }; 175 176 enum qca_speed_type { 177 QCA_INIT_SPEED = 1, 178 QCA_OPER_SPEED 179 }; 180 181 /* 182 * Voltage regulator information required for configuring the 183 * QCA Bluetooth chipset 184 */ 185 struct qca_vreg { 186 const char *name; 187 unsigned int load_uA; 188 }; 189 190 struct qca_vreg_data { 191 enum qca_btsoc_type soc_type; 192 struct qca_vreg *vregs; 193 size_t num_vregs; 194 }; 195 196 /* 197 * Platform data for the QCA Bluetooth power driver. 198 */ 199 struct qca_power { 200 struct device *dev; 201 struct regulator_bulk_data *vreg_bulk; 202 int num_vregs; 203 bool vregs_on; 204 }; 205 206 struct qca_serdev { 207 struct hci_uart serdev_hu; 208 struct gpio_desc *bt_en; 209 struct clk *susclk; 210 enum qca_btsoc_type btsoc_type; 211 struct qca_power *bt_power; 212 u32 init_speed; 213 u32 oper_speed; 214 const char *firmware_name; 215 }; 216 217 static int qca_regulator_enable(struct qca_serdev *qcadev); 218 static void qca_regulator_disable(struct qca_serdev *qcadev); 219 static void qca_power_shutdown(struct hci_uart *hu); 220 static int qca_power_off(struct hci_dev *hdev); 221 static void qca_controller_memdump(struct work_struct *work); 222 223 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu) 224 { 225 enum qca_btsoc_type soc_type; 226 227 if (hu->serdev) { 228 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev); 229 230 soc_type = qsd->btsoc_type; 231 } else { 232 soc_type = QCA_ROME; 233 } 234 235 return soc_type; 236 } 237 238 static const char *qca_get_firmware_name(struct hci_uart *hu) 239 { 240 if (hu->serdev) { 241 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev); 242 243 return qsd->firmware_name; 244 } else { 245 return NULL; 246 } 247 } 248 249 static void __serial_clock_on(struct tty_struct *tty) 250 { 251 /* TODO: Some chipset requires to enable UART clock on client 252 * side to save power consumption or manual work is required. 253 * Please put your code to control UART clock here if needed 254 */ 255 } 256 257 static void __serial_clock_off(struct tty_struct *tty) 258 { 259 /* TODO: Some chipset requires to disable UART clock on client 260 * side to save power consumption or manual work is required. 261 * Please put your code to control UART clock off here if needed 262 */ 263 } 264 265 /* serial_clock_vote needs to be called with the ibs lock held */ 266 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu) 267 { 268 struct qca_data *qca = hu->priv; 269 unsigned int diff; 270 271 bool old_vote = (qca->tx_vote | qca->rx_vote); 272 bool new_vote; 273 274 switch (vote) { 275 case HCI_IBS_VOTE_STATS_UPDATE: 276 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); 277 278 if (old_vote) 279 qca->vote_off_ms += diff; 280 else 281 qca->vote_on_ms += diff; 282 return; 283 284 case HCI_IBS_TX_VOTE_CLOCK_ON: 285 qca->tx_vote = true; 286 qca->tx_votes_on++; 287 new_vote = true; 288 break; 289 290 case HCI_IBS_RX_VOTE_CLOCK_ON: 291 qca->rx_vote = true; 292 qca->rx_votes_on++; 293 new_vote = true; 294 break; 295 296 case HCI_IBS_TX_VOTE_CLOCK_OFF: 297 qca->tx_vote = false; 298 qca->tx_votes_off++; 299 new_vote = qca->rx_vote | qca->tx_vote; 300 break; 301 302 case HCI_IBS_RX_VOTE_CLOCK_OFF: 303 qca->rx_vote = false; 304 qca->rx_votes_off++; 305 new_vote = qca->rx_vote | qca->tx_vote; 306 break; 307 308 default: 309 BT_ERR("Voting irregularity"); 310 return; 311 } 312 313 if (new_vote != old_vote) { 314 if (new_vote) 315 __serial_clock_on(hu->tty); 316 else 317 __serial_clock_off(hu->tty); 318 319 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false", 320 vote ? "true" : "false"); 321 322 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); 323 324 if (new_vote) { 325 qca->votes_on++; 326 qca->vote_off_ms += diff; 327 } else { 328 qca->votes_off++; 329 qca->vote_on_ms += diff; 330 } 331 qca->vote_last_jif = jiffies; 332 } 333 } 334 335 /* Builds and sends an HCI_IBS command packet. 336 * These are very simple packets with only 1 cmd byte. 337 */ 338 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu) 339 { 340 int err = 0; 341 struct sk_buff *skb = NULL; 342 struct qca_data *qca = hu->priv; 343 344 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd); 345 346 skb = bt_skb_alloc(1, GFP_ATOMIC); 347 if (!skb) { 348 BT_ERR("Failed to allocate memory for HCI_IBS packet"); 349 return -ENOMEM; 350 } 351 352 /* Assign HCI_IBS type */ 353 skb_put_u8(skb, cmd); 354 355 skb_queue_tail(&qca->txq, skb); 356 357 return err; 358 } 359 360 static void qca_wq_awake_device(struct work_struct *work) 361 { 362 struct qca_data *qca = container_of(work, struct qca_data, 363 ws_awake_device); 364 struct hci_uart *hu = qca->hu; 365 unsigned long retrans_delay; 366 unsigned long flags; 367 368 BT_DBG("hu %p wq awake device", hu); 369 370 /* Vote for serial clock */ 371 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu); 372 373 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 374 375 /* Send wake indication to device */ 376 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) 377 BT_ERR("Failed to send WAKE to device"); 378 379 qca->ibs_sent_wakes++; 380 381 /* Start retransmit timer */ 382 retrans_delay = msecs_to_jiffies(qca->wake_retrans); 383 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); 384 385 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 386 387 /* Actually send the packets */ 388 hci_uart_tx_wakeup(hu); 389 } 390 391 static void qca_wq_awake_rx(struct work_struct *work) 392 { 393 struct qca_data *qca = container_of(work, struct qca_data, 394 ws_awake_rx); 395 struct hci_uart *hu = qca->hu; 396 unsigned long flags; 397 398 BT_DBG("hu %p wq awake rx", hu); 399 400 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu); 401 402 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 403 qca->rx_ibs_state = HCI_IBS_RX_AWAKE; 404 405 /* Always acknowledge device wake up, 406 * sending IBS message doesn't count as TX ON. 407 */ 408 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) 409 BT_ERR("Failed to acknowledge device wake up"); 410 411 qca->ibs_sent_wacks++; 412 413 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 414 415 /* Actually send the packets */ 416 hci_uart_tx_wakeup(hu); 417 } 418 419 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work) 420 { 421 struct qca_data *qca = container_of(work, struct qca_data, 422 ws_rx_vote_off); 423 struct hci_uart *hu = qca->hu; 424 425 BT_DBG("hu %p rx clock vote off", hu); 426 427 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu); 428 } 429 430 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work) 431 { 432 struct qca_data *qca = container_of(work, struct qca_data, 433 ws_tx_vote_off); 434 struct hci_uart *hu = qca->hu; 435 436 BT_DBG("hu %p tx clock vote off", hu); 437 438 /* Run HCI tx handling unlocked */ 439 hci_uart_tx_wakeup(hu); 440 441 /* Now that message queued to tty driver, vote for tty clocks off. 442 * It is up to the tty driver to pend the clocks off until tx done. 443 */ 444 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu); 445 } 446 447 static void hci_ibs_tx_idle_timeout(struct timer_list *t) 448 { 449 struct qca_data *qca = from_timer(qca, t, tx_idle_timer); 450 struct hci_uart *hu = qca->hu; 451 unsigned long flags; 452 453 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state); 454 455 spin_lock_irqsave_nested(&qca->hci_ibs_lock, 456 flags, SINGLE_DEPTH_NESTING); 457 458 switch (qca->tx_ibs_state) { 459 case HCI_IBS_TX_AWAKE: 460 /* TX_IDLE, go to SLEEP */ 461 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) { 462 BT_ERR("Failed to send SLEEP to device"); 463 break; 464 } 465 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; 466 qca->ibs_sent_slps++; 467 queue_work(qca->workqueue, &qca->ws_tx_vote_off); 468 break; 469 470 case HCI_IBS_TX_ASLEEP: 471 case HCI_IBS_TX_WAKING: 472 /* Fall through */ 473 474 default: 475 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); 476 break; 477 } 478 479 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 480 } 481 482 static void hci_ibs_wake_retrans_timeout(struct timer_list *t) 483 { 484 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer); 485 struct hci_uart *hu = qca->hu; 486 unsigned long flags, retrans_delay; 487 bool retransmit = false; 488 489 BT_DBG("hu %p wake retransmit timeout in %d state", 490 hu, qca->tx_ibs_state); 491 492 spin_lock_irqsave_nested(&qca->hci_ibs_lock, 493 flags, SINGLE_DEPTH_NESTING); 494 495 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */ 496 if (test_bit(QCA_SUSPENDING, &qca->flags)) { 497 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 498 return; 499 } 500 501 switch (qca->tx_ibs_state) { 502 case HCI_IBS_TX_WAKING: 503 /* No WAKE_ACK, retransmit WAKE */ 504 retransmit = true; 505 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) { 506 BT_ERR("Failed to acknowledge device wake up"); 507 break; 508 } 509 qca->ibs_sent_wakes++; 510 retrans_delay = msecs_to_jiffies(qca->wake_retrans); 511 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); 512 break; 513 514 case HCI_IBS_TX_ASLEEP: 515 case HCI_IBS_TX_AWAKE: 516 /* Fall through */ 517 518 default: 519 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); 520 break; 521 } 522 523 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 524 525 if (retransmit) 526 hci_uart_tx_wakeup(hu); 527 } 528 529 530 static void qca_controller_memdump_timeout(struct work_struct *work) 531 { 532 struct qca_data *qca = container_of(work, struct qca_data, 533 ctrl_memdump_timeout.work); 534 struct hci_uart *hu = qca->hu; 535 536 mutex_lock(&qca->hci_memdump_lock); 537 if (test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) { 538 qca->memdump_state = QCA_MEMDUMP_TIMEOUT; 539 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) { 540 /* Inject hw error event to reset the device 541 * and driver. 542 */ 543 hci_reset_dev(hu->hdev); 544 } 545 } 546 547 mutex_unlock(&qca->hci_memdump_lock); 548 } 549 550 551 /* Initialize protocol */ 552 static int qca_open(struct hci_uart *hu) 553 { 554 struct qca_serdev *qcadev; 555 struct qca_data *qca; 556 557 BT_DBG("hu %p qca_open", hu); 558 559 if (!hci_uart_has_flow_control(hu)) 560 return -EOPNOTSUPP; 561 562 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL); 563 if (!qca) 564 return -ENOMEM; 565 566 skb_queue_head_init(&qca->txq); 567 skb_queue_head_init(&qca->tx_wait_q); 568 skb_queue_head_init(&qca->rx_memdump_q); 569 spin_lock_init(&qca->hci_ibs_lock); 570 mutex_init(&qca->hci_memdump_lock); 571 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0); 572 if (!qca->workqueue) { 573 BT_ERR("QCA Workqueue not initialized properly"); 574 kfree(qca); 575 return -ENOMEM; 576 } 577 578 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx); 579 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device); 580 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off); 581 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off); 582 INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump); 583 INIT_DELAYED_WORK(&qca->ctrl_memdump_timeout, 584 qca_controller_memdump_timeout); 585 init_waitqueue_head(&qca->suspend_wait_q); 586 587 qca->hu = hu; 588 init_completion(&qca->drop_ev_comp); 589 590 /* Assume we start with both sides asleep -- extra wakes OK */ 591 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; 592 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP; 593 594 qca->vote_last_jif = jiffies; 595 596 hu->priv = qca; 597 598 if (hu->serdev) { 599 qcadev = serdev_device_get_drvdata(hu->serdev); 600 601 if (qca_is_wcn399x(qcadev->btsoc_type)) 602 hu->init_speed = qcadev->init_speed; 603 604 if (qcadev->oper_speed) 605 hu->oper_speed = qcadev->oper_speed; 606 } 607 608 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0); 609 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS; 610 611 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0); 612 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS; 613 614 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u", 615 qca->tx_idle_delay, qca->wake_retrans); 616 617 return 0; 618 } 619 620 static void qca_debugfs_init(struct hci_dev *hdev) 621 { 622 struct hci_uart *hu = hci_get_drvdata(hdev); 623 struct qca_data *qca = hu->priv; 624 struct dentry *ibs_dir; 625 umode_t mode; 626 627 if (!hdev->debugfs) 628 return; 629 630 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs); 631 632 /* read only */ 633 mode = S_IRUGO; 634 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state); 635 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state); 636 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir, 637 &qca->ibs_sent_slps); 638 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir, 639 &qca->ibs_sent_wakes); 640 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir, 641 &qca->ibs_sent_wacks); 642 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir, 643 &qca->ibs_recv_slps); 644 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir, 645 &qca->ibs_recv_wakes); 646 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir, 647 &qca->ibs_recv_wacks); 648 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote); 649 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on); 650 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off); 651 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote); 652 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on); 653 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off); 654 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on); 655 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off); 656 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms); 657 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms); 658 659 /* read/write */ 660 mode = S_IRUGO | S_IWUSR; 661 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans); 662 debugfs_create_u32("tx_idle_delay", mode, ibs_dir, 663 &qca->tx_idle_delay); 664 } 665 666 /* Flush protocol data */ 667 static int qca_flush(struct hci_uart *hu) 668 { 669 struct qca_data *qca = hu->priv; 670 671 BT_DBG("hu %p qca flush", hu); 672 673 skb_queue_purge(&qca->tx_wait_q); 674 skb_queue_purge(&qca->txq); 675 676 return 0; 677 } 678 679 /* Close protocol */ 680 static int qca_close(struct hci_uart *hu) 681 { 682 struct qca_data *qca = hu->priv; 683 684 BT_DBG("hu %p qca close", hu); 685 686 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu); 687 688 skb_queue_purge(&qca->tx_wait_q); 689 skb_queue_purge(&qca->txq); 690 skb_queue_purge(&qca->rx_memdump_q); 691 del_timer(&qca->tx_idle_timer); 692 del_timer(&qca->wake_retrans_timer); 693 destroy_workqueue(qca->workqueue); 694 qca->hu = NULL; 695 696 qca_power_shutdown(hu); 697 698 kfree_skb(qca->rx_skb); 699 700 hu->priv = NULL; 701 702 kfree(qca); 703 704 return 0; 705 } 706 707 /* Called upon a wake-up-indication from the device. 708 */ 709 static void device_want_to_wakeup(struct hci_uart *hu) 710 { 711 unsigned long flags; 712 struct qca_data *qca = hu->priv; 713 714 BT_DBG("hu %p want to wake up", hu); 715 716 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 717 718 qca->ibs_recv_wakes++; 719 720 /* Don't wake the rx up when suspending. */ 721 if (test_bit(QCA_SUSPENDING, &qca->flags)) { 722 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 723 return; 724 } 725 726 switch (qca->rx_ibs_state) { 727 case HCI_IBS_RX_ASLEEP: 728 /* Make sure clock is on - we may have turned clock off since 729 * receiving the wake up indicator awake rx clock. 730 */ 731 queue_work(qca->workqueue, &qca->ws_awake_rx); 732 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 733 return; 734 735 case HCI_IBS_RX_AWAKE: 736 /* Always acknowledge device wake up, 737 * sending IBS message doesn't count as TX ON. 738 */ 739 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) { 740 BT_ERR("Failed to acknowledge device wake up"); 741 break; 742 } 743 qca->ibs_sent_wacks++; 744 break; 745 746 default: 747 /* Any other state is illegal */ 748 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d", 749 qca->rx_ibs_state); 750 break; 751 } 752 753 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 754 755 /* Actually send the packets */ 756 hci_uart_tx_wakeup(hu); 757 } 758 759 /* Called upon a sleep-indication from the device. 760 */ 761 static void device_want_to_sleep(struct hci_uart *hu) 762 { 763 unsigned long flags; 764 struct qca_data *qca = hu->priv; 765 766 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state); 767 768 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 769 770 qca->ibs_recv_slps++; 771 772 switch (qca->rx_ibs_state) { 773 case HCI_IBS_RX_AWAKE: 774 /* Update state */ 775 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP; 776 /* Vote off rx clock under workqueue */ 777 queue_work(qca->workqueue, &qca->ws_rx_vote_off); 778 break; 779 780 case HCI_IBS_RX_ASLEEP: 781 break; 782 783 default: 784 /* Any other state is illegal */ 785 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d", 786 qca->rx_ibs_state); 787 break; 788 } 789 790 wake_up_interruptible(&qca->suspend_wait_q); 791 792 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 793 } 794 795 /* Called upon wake-up-acknowledgement from the device 796 */ 797 static void device_woke_up(struct hci_uart *hu) 798 { 799 unsigned long flags, idle_delay; 800 struct qca_data *qca = hu->priv; 801 struct sk_buff *skb = NULL; 802 803 BT_DBG("hu %p woke up", hu); 804 805 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 806 807 qca->ibs_recv_wacks++; 808 809 /* Don't react to the wake-up-acknowledgment when suspending. */ 810 if (test_bit(QCA_SUSPENDING, &qca->flags)) { 811 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 812 return; 813 } 814 815 switch (qca->tx_ibs_state) { 816 case HCI_IBS_TX_AWAKE: 817 /* Expect one if we send 2 WAKEs */ 818 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d", 819 qca->tx_ibs_state); 820 break; 821 822 case HCI_IBS_TX_WAKING: 823 /* Send pending packets */ 824 while ((skb = skb_dequeue(&qca->tx_wait_q))) 825 skb_queue_tail(&qca->txq, skb); 826 827 /* Switch timers and change state to HCI_IBS_TX_AWAKE */ 828 del_timer(&qca->wake_retrans_timer); 829 idle_delay = msecs_to_jiffies(qca->tx_idle_delay); 830 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay); 831 qca->tx_ibs_state = HCI_IBS_TX_AWAKE; 832 break; 833 834 case HCI_IBS_TX_ASLEEP: 835 /* Fall through */ 836 837 default: 838 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d", 839 qca->tx_ibs_state); 840 break; 841 } 842 843 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 844 845 /* Actually send the packets */ 846 hci_uart_tx_wakeup(hu); 847 } 848 849 /* Enqueue frame for transmittion (padding, crc, etc) may be called from 850 * two simultaneous tasklets. 851 */ 852 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb) 853 { 854 unsigned long flags = 0, idle_delay; 855 struct qca_data *qca = hu->priv; 856 857 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb, 858 qca->tx_ibs_state); 859 860 /* Prepend skb with frame type */ 861 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); 862 863 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 864 865 /* Don't go to sleep in middle of patch download or 866 * Out-Of-Band(GPIOs control) sleep is selected. 867 * Don't wake the device up when suspending. 868 */ 869 if (!test_bit(QCA_IBS_ENABLED, &qca->flags) || 870 test_bit(QCA_SUSPENDING, &qca->flags)) { 871 skb_queue_tail(&qca->txq, skb); 872 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 873 return 0; 874 } 875 876 /* Act according to current state */ 877 switch (qca->tx_ibs_state) { 878 case HCI_IBS_TX_AWAKE: 879 BT_DBG("Device awake, sending normally"); 880 skb_queue_tail(&qca->txq, skb); 881 idle_delay = msecs_to_jiffies(qca->tx_idle_delay); 882 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay); 883 break; 884 885 case HCI_IBS_TX_ASLEEP: 886 BT_DBG("Device asleep, waking up and queueing packet"); 887 /* Save packet for later */ 888 skb_queue_tail(&qca->tx_wait_q, skb); 889 890 qca->tx_ibs_state = HCI_IBS_TX_WAKING; 891 /* Schedule a work queue to wake up device */ 892 queue_work(qca->workqueue, &qca->ws_awake_device); 893 break; 894 895 case HCI_IBS_TX_WAKING: 896 BT_DBG("Device waking up, queueing packet"); 897 /* Transient state; just keep packet for later */ 898 skb_queue_tail(&qca->tx_wait_q, skb); 899 break; 900 901 default: 902 BT_ERR("Illegal tx state: %d (losing packet)", 903 qca->tx_ibs_state); 904 kfree_skb(skb); 905 break; 906 } 907 908 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 909 910 return 0; 911 } 912 913 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb) 914 { 915 struct hci_uart *hu = hci_get_drvdata(hdev); 916 917 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND); 918 919 device_want_to_sleep(hu); 920 921 kfree_skb(skb); 922 return 0; 923 } 924 925 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb) 926 { 927 struct hci_uart *hu = hci_get_drvdata(hdev); 928 929 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND); 930 931 device_want_to_wakeup(hu); 932 933 kfree_skb(skb); 934 return 0; 935 } 936 937 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb) 938 { 939 struct hci_uart *hu = hci_get_drvdata(hdev); 940 941 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK); 942 943 device_woke_up(hu); 944 945 kfree_skb(skb); 946 return 0; 947 } 948 949 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb) 950 { 951 /* We receive debug logs from chip as an ACL packets. 952 * Instead of sending the data to ACL to decode the 953 * received data, we are pushing them to the above layers 954 * as a diagnostic packet. 955 */ 956 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE) 957 return hci_recv_diag(hdev, skb); 958 959 return hci_recv_frame(hdev, skb); 960 } 961 962 static void qca_controller_memdump(struct work_struct *work) 963 { 964 struct qca_data *qca = container_of(work, struct qca_data, 965 ctrl_memdump_evt); 966 struct hci_uart *hu = qca->hu; 967 struct sk_buff *skb; 968 struct qca_memdump_event_hdr *cmd_hdr; 969 struct qca_memdump_data *qca_memdump = qca->qca_memdump; 970 struct qca_dump_size *dump; 971 char *memdump_buf; 972 char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 }; 973 u16 seq_no; 974 u32 dump_size; 975 976 while ((skb = skb_dequeue(&qca->rx_memdump_q))) { 977 978 mutex_lock(&qca->hci_memdump_lock); 979 /* Skip processing the received packets if timeout detected. */ 980 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT) { 981 mutex_unlock(&qca->hci_memdump_lock); 982 return; 983 } 984 985 if (!qca_memdump) { 986 qca_memdump = kzalloc(sizeof(struct qca_memdump_data), 987 GFP_ATOMIC); 988 if (!qca_memdump) { 989 mutex_unlock(&qca->hci_memdump_lock); 990 return; 991 } 992 993 qca->qca_memdump = qca_memdump; 994 } 995 996 qca->memdump_state = QCA_MEMDUMP_COLLECTING; 997 cmd_hdr = (void *) skb->data; 998 seq_no = __le16_to_cpu(cmd_hdr->seq_no); 999 skb_pull(skb, sizeof(struct qca_memdump_event_hdr)); 1000 1001 if (!seq_no) { 1002 1003 /* This is the first frame of memdump packet from 1004 * the controller, Disable IBS to recevie dump 1005 * with out any interruption, ideally time required for 1006 * the controller to send the dump is 8 seconds. let us 1007 * start timer to handle this asynchronous activity. 1008 */ 1009 clear_bit(QCA_IBS_ENABLED, &qca->flags); 1010 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags); 1011 dump = (void *) skb->data; 1012 dump_size = __le32_to_cpu(dump->dump_size); 1013 if (!(dump_size)) { 1014 bt_dev_err(hu->hdev, "Rx invalid memdump size"); 1015 kfree_skb(skb); 1016 mutex_unlock(&qca->hci_memdump_lock); 1017 return; 1018 } 1019 1020 bt_dev_info(hu->hdev, "QCA collecting dump of size:%u", 1021 dump_size); 1022 queue_delayed_work(qca->workqueue, 1023 &qca->ctrl_memdump_timeout, 1024 msecs_to_jiffies(MEMDUMP_TIMEOUT_MS)); 1025 1026 skb_pull(skb, sizeof(dump_size)); 1027 memdump_buf = vmalloc(dump_size); 1028 qca_memdump->memdump_buf_head = memdump_buf; 1029 qca_memdump->memdump_buf_tail = memdump_buf; 1030 } 1031 1032 memdump_buf = qca_memdump->memdump_buf_tail; 1033 1034 /* If sequence no 0 is missed then there is no point in 1035 * accepting the other sequences. 1036 */ 1037 if (!memdump_buf) { 1038 bt_dev_err(hu->hdev, "QCA: Discarding other packets"); 1039 kfree(qca_memdump); 1040 kfree_skb(skb); 1041 qca->qca_memdump = NULL; 1042 mutex_unlock(&qca->hci_memdump_lock); 1043 return; 1044 } 1045 1046 /* There could be chance of missing some packets from 1047 * the controller. In such cases let us store the dummy 1048 * packets in the buffer. 1049 */ 1050 while ((seq_no > qca_memdump->current_seq_no + 1) && 1051 seq_no != QCA_LAST_SEQUENCE_NUM) { 1052 bt_dev_err(hu->hdev, "QCA controller missed packet:%d", 1053 qca_memdump->current_seq_no); 1054 memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE); 1055 memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE; 1056 qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE; 1057 qca_memdump->current_seq_no++; 1058 } 1059 1060 memcpy(memdump_buf, (unsigned char *) skb->data, skb->len); 1061 memdump_buf = memdump_buf + skb->len; 1062 qca_memdump->memdump_buf_tail = memdump_buf; 1063 qca_memdump->current_seq_no = seq_no + 1; 1064 qca_memdump->received_dump += skb->len; 1065 qca->qca_memdump = qca_memdump; 1066 kfree_skb(skb); 1067 if (seq_no == QCA_LAST_SEQUENCE_NUM) { 1068 bt_dev_info(hu->hdev, "QCA writing crash dump of size %d bytes", 1069 qca_memdump->received_dump); 1070 memdump_buf = qca_memdump->memdump_buf_head; 1071 dev_coredumpv(&hu->serdev->dev, memdump_buf, 1072 qca_memdump->received_dump, GFP_KERNEL); 1073 cancel_delayed_work(&qca->ctrl_memdump_timeout); 1074 kfree(qca->qca_memdump); 1075 qca->qca_memdump = NULL; 1076 qca->memdump_state = QCA_MEMDUMP_COLLECTED; 1077 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags); 1078 } 1079 1080 mutex_unlock(&qca->hci_memdump_lock); 1081 } 1082 1083 } 1084 1085 static int qca_controller_memdump_event(struct hci_dev *hdev, 1086 struct sk_buff *skb) 1087 { 1088 struct hci_uart *hu = hci_get_drvdata(hdev); 1089 struct qca_data *qca = hu->priv; 1090 1091 skb_queue_tail(&qca->rx_memdump_q, skb); 1092 queue_work(qca->workqueue, &qca->ctrl_memdump_evt); 1093 1094 return 0; 1095 } 1096 1097 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb) 1098 { 1099 struct hci_uart *hu = hci_get_drvdata(hdev); 1100 struct qca_data *qca = hu->priv; 1101 1102 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) { 1103 struct hci_event_hdr *hdr = (void *)skb->data; 1104 1105 /* For the WCN3990 the vendor command for a baudrate change 1106 * isn't sent as synchronous HCI command, because the 1107 * controller sends the corresponding vendor event with the 1108 * new baudrate. The event is received and properly decoded 1109 * after changing the baudrate of the host port. It needs to 1110 * be dropped, otherwise it can be misinterpreted as 1111 * response to a later firmware download command (also a 1112 * vendor command). 1113 */ 1114 1115 if (hdr->evt == HCI_EV_VENDOR) 1116 complete(&qca->drop_ev_comp); 1117 1118 kfree_skb(skb); 1119 1120 return 0; 1121 } 1122 /* We receive chip memory dump as an event packet, With a dedicated 1123 * handler followed by a hardware error event. When this event is 1124 * received we store dump into a file before closing hci. This 1125 * dump will help in triaging the issues. 1126 */ 1127 if ((skb->data[0] == HCI_VENDOR_PKT) && 1128 (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE)) 1129 return qca_controller_memdump_event(hdev, skb); 1130 1131 return hci_recv_frame(hdev, skb); 1132 } 1133 1134 #define QCA_IBS_SLEEP_IND_EVENT \ 1135 .type = HCI_IBS_SLEEP_IND, \ 1136 .hlen = 0, \ 1137 .loff = 0, \ 1138 .lsize = 0, \ 1139 .maxlen = HCI_MAX_IBS_SIZE 1140 1141 #define QCA_IBS_WAKE_IND_EVENT \ 1142 .type = HCI_IBS_WAKE_IND, \ 1143 .hlen = 0, \ 1144 .loff = 0, \ 1145 .lsize = 0, \ 1146 .maxlen = HCI_MAX_IBS_SIZE 1147 1148 #define QCA_IBS_WAKE_ACK_EVENT \ 1149 .type = HCI_IBS_WAKE_ACK, \ 1150 .hlen = 0, \ 1151 .loff = 0, \ 1152 .lsize = 0, \ 1153 .maxlen = HCI_MAX_IBS_SIZE 1154 1155 static const struct h4_recv_pkt qca_recv_pkts[] = { 1156 { H4_RECV_ACL, .recv = qca_recv_acl_data }, 1157 { H4_RECV_SCO, .recv = hci_recv_frame }, 1158 { H4_RECV_EVENT, .recv = qca_recv_event }, 1159 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind }, 1160 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack }, 1161 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind }, 1162 }; 1163 1164 static int qca_recv(struct hci_uart *hu, const void *data, int count) 1165 { 1166 struct qca_data *qca = hu->priv; 1167 1168 if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 1169 return -EUNATCH; 1170 1171 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count, 1172 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts)); 1173 if (IS_ERR(qca->rx_skb)) { 1174 int err = PTR_ERR(qca->rx_skb); 1175 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); 1176 qca->rx_skb = NULL; 1177 return err; 1178 } 1179 1180 return count; 1181 } 1182 1183 static struct sk_buff *qca_dequeue(struct hci_uart *hu) 1184 { 1185 struct qca_data *qca = hu->priv; 1186 1187 return skb_dequeue(&qca->txq); 1188 } 1189 1190 static uint8_t qca_get_baudrate_value(int speed) 1191 { 1192 switch (speed) { 1193 case 9600: 1194 return QCA_BAUDRATE_9600; 1195 case 19200: 1196 return QCA_BAUDRATE_19200; 1197 case 38400: 1198 return QCA_BAUDRATE_38400; 1199 case 57600: 1200 return QCA_BAUDRATE_57600; 1201 case 115200: 1202 return QCA_BAUDRATE_115200; 1203 case 230400: 1204 return QCA_BAUDRATE_230400; 1205 case 460800: 1206 return QCA_BAUDRATE_460800; 1207 case 500000: 1208 return QCA_BAUDRATE_500000; 1209 case 921600: 1210 return QCA_BAUDRATE_921600; 1211 case 1000000: 1212 return QCA_BAUDRATE_1000000; 1213 case 2000000: 1214 return QCA_BAUDRATE_2000000; 1215 case 3000000: 1216 return QCA_BAUDRATE_3000000; 1217 case 3200000: 1218 return QCA_BAUDRATE_3200000; 1219 case 3500000: 1220 return QCA_BAUDRATE_3500000; 1221 default: 1222 return QCA_BAUDRATE_115200; 1223 } 1224 } 1225 1226 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate) 1227 { 1228 struct hci_uart *hu = hci_get_drvdata(hdev); 1229 struct qca_data *qca = hu->priv; 1230 struct sk_buff *skb; 1231 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 }; 1232 1233 if (baudrate > QCA_BAUDRATE_3200000) 1234 return -EINVAL; 1235 1236 cmd[4] = baudrate; 1237 1238 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); 1239 if (!skb) { 1240 bt_dev_err(hdev, "Failed to allocate baudrate packet"); 1241 return -ENOMEM; 1242 } 1243 1244 /* Assign commands to change baudrate and packet type. */ 1245 skb_put_data(skb, cmd, sizeof(cmd)); 1246 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; 1247 1248 skb_queue_tail(&qca->txq, skb); 1249 hci_uart_tx_wakeup(hu); 1250 1251 /* Wait for the baudrate change request to be sent */ 1252 1253 while (!skb_queue_empty(&qca->txq)) 1254 usleep_range(100, 200); 1255 1256 if (hu->serdev) 1257 serdev_device_wait_until_sent(hu->serdev, 1258 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS)); 1259 1260 /* Give the controller time to process the request */ 1261 if (qca_is_wcn399x(qca_soc_type(hu))) 1262 msleep(10); 1263 else 1264 msleep(300); 1265 1266 return 0; 1267 } 1268 1269 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed) 1270 { 1271 if (hu->serdev) 1272 serdev_device_set_baudrate(hu->serdev, speed); 1273 else 1274 hci_uart_set_baudrate(hu, speed); 1275 } 1276 1277 static int qca_send_power_pulse(struct hci_uart *hu, bool on) 1278 { 1279 int ret; 1280 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS); 1281 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE; 1282 1283 /* These power pulses are single byte command which are sent 1284 * at required baudrate to wcn3990. On wcn3990, we have an external 1285 * circuit at Tx pin which decodes the pulse sent at specific baudrate. 1286 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT 1287 * and also we use the same power inputs to turn on and off for 1288 * Wi-Fi/BT. Powering up the power sources will not enable BT, until 1289 * we send a power on pulse at 115200 bps. This algorithm will help to 1290 * save power. Disabling hardware flow control is mandatory while 1291 * sending power pulses to SoC. 1292 */ 1293 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd); 1294 1295 serdev_device_write_flush(hu->serdev); 1296 hci_uart_set_flow_control(hu, true); 1297 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd)); 1298 if (ret < 0) { 1299 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd); 1300 return ret; 1301 } 1302 1303 serdev_device_wait_until_sent(hu->serdev, timeout); 1304 hci_uart_set_flow_control(hu, false); 1305 1306 /* Give to controller time to boot/shutdown */ 1307 if (on) 1308 msleep(100); 1309 else 1310 msleep(10); 1311 1312 return 0; 1313 } 1314 1315 static unsigned int qca_get_speed(struct hci_uart *hu, 1316 enum qca_speed_type speed_type) 1317 { 1318 unsigned int speed = 0; 1319 1320 if (speed_type == QCA_INIT_SPEED) { 1321 if (hu->init_speed) 1322 speed = hu->init_speed; 1323 else if (hu->proto->init_speed) 1324 speed = hu->proto->init_speed; 1325 } else { 1326 if (hu->oper_speed) 1327 speed = hu->oper_speed; 1328 else if (hu->proto->oper_speed) 1329 speed = hu->proto->oper_speed; 1330 } 1331 1332 return speed; 1333 } 1334 1335 static int qca_check_speeds(struct hci_uart *hu) 1336 { 1337 if (qca_is_wcn399x(qca_soc_type(hu))) { 1338 if (!qca_get_speed(hu, QCA_INIT_SPEED) && 1339 !qca_get_speed(hu, QCA_OPER_SPEED)) 1340 return -EINVAL; 1341 } else { 1342 if (!qca_get_speed(hu, QCA_INIT_SPEED) || 1343 !qca_get_speed(hu, QCA_OPER_SPEED)) 1344 return -EINVAL; 1345 } 1346 1347 return 0; 1348 } 1349 1350 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type) 1351 { 1352 unsigned int speed, qca_baudrate; 1353 struct qca_data *qca = hu->priv; 1354 int ret = 0; 1355 1356 if (speed_type == QCA_INIT_SPEED) { 1357 speed = qca_get_speed(hu, QCA_INIT_SPEED); 1358 if (speed) 1359 host_set_baudrate(hu, speed); 1360 } else { 1361 enum qca_btsoc_type soc_type = qca_soc_type(hu); 1362 1363 speed = qca_get_speed(hu, QCA_OPER_SPEED); 1364 if (!speed) 1365 return 0; 1366 1367 /* Disable flow control for wcn3990 to deassert RTS while 1368 * changing the baudrate of chip and host. 1369 */ 1370 if (qca_is_wcn399x(soc_type)) 1371 hci_uart_set_flow_control(hu, true); 1372 1373 if (soc_type == QCA_WCN3990) { 1374 reinit_completion(&qca->drop_ev_comp); 1375 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags); 1376 } 1377 1378 qca_baudrate = qca_get_baudrate_value(speed); 1379 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed); 1380 ret = qca_set_baudrate(hu->hdev, qca_baudrate); 1381 if (ret) 1382 goto error; 1383 1384 host_set_baudrate(hu, speed); 1385 1386 error: 1387 if (qca_is_wcn399x(soc_type)) 1388 hci_uart_set_flow_control(hu, false); 1389 1390 if (soc_type == QCA_WCN3990) { 1391 /* Wait for the controller to send the vendor event 1392 * for the baudrate change command. 1393 */ 1394 if (!wait_for_completion_timeout(&qca->drop_ev_comp, 1395 msecs_to_jiffies(100))) { 1396 bt_dev_err(hu->hdev, 1397 "Failed to change controller baudrate\n"); 1398 ret = -ETIMEDOUT; 1399 } 1400 1401 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags); 1402 } 1403 } 1404 1405 return ret; 1406 } 1407 1408 static int qca_send_crashbuffer(struct hci_uart *hu) 1409 { 1410 struct qca_data *qca = hu->priv; 1411 struct sk_buff *skb; 1412 1413 skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL); 1414 if (!skb) { 1415 bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet"); 1416 return -ENOMEM; 1417 } 1418 1419 /* We forcefully crash the controller, by sending 0xfb byte for 1420 * 1024 times. We also might have chance of losing data, To be 1421 * on safer side we send 1096 bytes to the SoC. 1422 */ 1423 memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE, 1424 QCA_CRASHBYTE_PACKET_LEN); 1425 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; 1426 bt_dev_info(hu->hdev, "crash the soc to collect controller dump"); 1427 skb_queue_tail(&qca->txq, skb); 1428 hci_uart_tx_wakeup(hu); 1429 1430 return 0; 1431 } 1432 1433 static void qca_wait_for_dump_collection(struct hci_dev *hdev) 1434 { 1435 struct hci_uart *hu = hci_get_drvdata(hdev); 1436 struct qca_data *qca = hu->priv; 1437 1438 wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION, 1439 TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS); 1440 1441 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags); 1442 } 1443 1444 static void qca_hw_error(struct hci_dev *hdev, u8 code) 1445 { 1446 struct hci_uart *hu = hci_get_drvdata(hdev); 1447 struct qca_data *qca = hu->priv; 1448 struct qca_memdump_data *qca_memdump = qca->qca_memdump; 1449 char *memdump_buf = NULL; 1450 1451 set_bit(QCA_HW_ERROR_EVENT, &qca->flags); 1452 bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state); 1453 1454 if (qca->memdump_state == QCA_MEMDUMP_IDLE) { 1455 /* If hardware error event received for other than QCA 1456 * soc memory dump event, then we need to crash the SOC 1457 * and wait here for 8 seconds to get the dump packets. 1458 * This will block main thread to be on hold until we 1459 * collect dump. 1460 */ 1461 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags); 1462 qca_send_crashbuffer(hu); 1463 qca_wait_for_dump_collection(hdev); 1464 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) { 1465 /* Let us wait here until memory dump collected or 1466 * memory dump timer expired. 1467 */ 1468 bt_dev_info(hdev, "waiting for dump to complete"); 1469 qca_wait_for_dump_collection(hdev); 1470 } 1471 1472 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) { 1473 bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout"); 1474 mutex_lock(&qca->hci_memdump_lock); 1475 if (qca_memdump) 1476 memdump_buf = qca_memdump->memdump_buf_head; 1477 vfree(memdump_buf); 1478 kfree(qca_memdump); 1479 qca->qca_memdump = NULL; 1480 qca->memdump_state = QCA_MEMDUMP_TIMEOUT; 1481 cancel_delayed_work(&qca->ctrl_memdump_timeout); 1482 skb_queue_purge(&qca->rx_memdump_q); 1483 mutex_unlock(&qca->hci_memdump_lock); 1484 cancel_work_sync(&qca->ctrl_memdump_evt); 1485 } 1486 1487 clear_bit(QCA_HW_ERROR_EVENT, &qca->flags); 1488 } 1489 1490 static void qca_cmd_timeout(struct hci_dev *hdev) 1491 { 1492 struct hci_uart *hu = hci_get_drvdata(hdev); 1493 struct qca_data *qca = hu->priv; 1494 1495 if (qca->memdump_state == QCA_MEMDUMP_IDLE) 1496 qca_send_crashbuffer(hu); 1497 else 1498 bt_dev_info(hdev, "Dump collection is in process"); 1499 } 1500 1501 static int qca_wcn3990_init(struct hci_uart *hu) 1502 { 1503 struct qca_serdev *qcadev; 1504 int ret; 1505 1506 /* Check for vregs status, may be hci down has turned 1507 * off the voltage regulator. 1508 */ 1509 qcadev = serdev_device_get_drvdata(hu->serdev); 1510 if (!qcadev->bt_power->vregs_on) { 1511 serdev_device_close(hu->serdev); 1512 ret = qca_regulator_enable(qcadev); 1513 if (ret) 1514 return ret; 1515 1516 ret = serdev_device_open(hu->serdev); 1517 if (ret) { 1518 bt_dev_err(hu->hdev, "failed to open port"); 1519 return ret; 1520 } 1521 } 1522 1523 /* Forcefully enable wcn3990 to enter in to boot mode. */ 1524 host_set_baudrate(hu, 2400); 1525 ret = qca_send_power_pulse(hu, false); 1526 if (ret) 1527 return ret; 1528 1529 qca_set_speed(hu, QCA_INIT_SPEED); 1530 ret = qca_send_power_pulse(hu, true); 1531 if (ret) 1532 return ret; 1533 1534 /* Now the device is in ready state to communicate with host. 1535 * To sync host with device we need to reopen port. 1536 * Without this, we will have RTS and CTS synchronization 1537 * issues. 1538 */ 1539 serdev_device_close(hu->serdev); 1540 ret = serdev_device_open(hu->serdev); 1541 if (ret) { 1542 bt_dev_err(hu->hdev, "failed to open port"); 1543 return ret; 1544 } 1545 1546 hci_uart_set_flow_control(hu, false); 1547 1548 return 0; 1549 } 1550 1551 static int qca_power_on(struct hci_dev *hdev) 1552 { 1553 struct hci_uart *hu = hci_get_drvdata(hdev); 1554 enum qca_btsoc_type soc_type = qca_soc_type(hu); 1555 struct qca_serdev *qcadev; 1556 int ret = 0; 1557 1558 /* Non-serdev device usually is powered by external power 1559 * and don't need additional action in driver for power on 1560 */ 1561 if (!hu->serdev) 1562 return 0; 1563 1564 if (qca_is_wcn399x(soc_type)) { 1565 ret = qca_wcn3990_init(hu); 1566 } else { 1567 qcadev = serdev_device_get_drvdata(hu->serdev); 1568 if (qcadev->bt_en) { 1569 gpiod_set_value_cansleep(qcadev->bt_en, 1); 1570 /* Controller needs time to bootup. */ 1571 msleep(150); 1572 } 1573 } 1574 1575 return ret; 1576 } 1577 1578 static int qca_setup(struct hci_uart *hu) 1579 { 1580 struct hci_dev *hdev = hu->hdev; 1581 struct qca_data *qca = hu->priv; 1582 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200; 1583 unsigned int retries = 0; 1584 enum qca_btsoc_type soc_type = qca_soc_type(hu); 1585 const char *firmware_name = qca_get_firmware_name(hu); 1586 int ret; 1587 int soc_ver = 0; 1588 1589 ret = qca_check_speeds(hu); 1590 if (ret) 1591 return ret; 1592 1593 /* Patch downloading has to be done without IBS mode */ 1594 clear_bit(QCA_IBS_ENABLED, &qca->flags); 1595 1596 /* Enable controller to do both LE scan and BR/EDR inquiry 1597 * simultaneously. 1598 */ 1599 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); 1600 1601 bt_dev_info(hdev, "setting up %s", 1602 qca_is_wcn399x(soc_type) ? "wcn399x" : "ROME/QCA6390"); 1603 1604 retry: 1605 ret = qca_power_on(hdev); 1606 if (ret) 1607 return ret; 1608 1609 if (qca_is_wcn399x(soc_type)) { 1610 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks); 1611 1612 ret = qca_read_soc_version(hdev, &soc_ver, soc_type); 1613 if (ret) 1614 return ret; 1615 } else { 1616 qca_set_speed(hu, QCA_INIT_SPEED); 1617 } 1618 1619 /* Setup user speed if needed */ 1620 speed = qca_get_speed(hu, QCA_OPER_SPEED); 1621 if (speed) { 1622 ret = qca_set_speed(hu, QCA_OPER_SPEED); 1623 if (ret) 1624 return ret; 1625 1626 qca_baudrate = qca_get_baudrate_value(speed); 1627 } 1628 1629 if (!qca_is_wcn399x(soc_type)) { 1630 /* Get QCA version information */ 1631 ret = qca_read_soc_version(hdev, &soc_ver, soc_type); 1632 if (ret) 1633 return ret; 1634 } 1635 1636 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver); 1637 /* Setup patch / NVM configurations */ 1638 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver, 1639 firmware_name); 1640 if (!ret) { 1641 set_bit(QCA_IBS_ENABLED, &qca->flags); 1642 qca_debugfs_init(hdev); 1643 hu->hdev->hw_error = qca_hw_error; 1644 hu->hdev->cmd_timeout = qca_cmd_timeout; 1645 } else if (ret == -ENOENT) { 1646 /* No patch/nvm-config found, run with original fw/config */ 1647 ret = 0; 1648 } else if (ret == -EAGAIN) { 1649 /* 1650 * Userspace firmware loader will return -EAGAIN in case no 1651 * patch/nvm-config is found, so run with original fw/config. 1652 */ 1653 ret = 0; 1654 } else { 1655 if (retries < MAX_INIT_RETRIES) { 1656 qca_power_shutdown(hu); 1657 if (hu->serdev) { 1658 serdev_device_close(hu->serdev); 1659 ret = serdev_device_open(hu->serdev); 1660 if (ret) { 1661 bt_dev_err(hdev, "failed to open port"); 1662 return ret; 1663 } 1664 } 1665 retries++; 1666 goto retry; 1667 } 1668 } 1669 1670 /* Setup bdaddr */ 1671 if (soc_type == QCA_ROME) 1672 hu->hdev->set_bdaddr = qca_set_bdaddr_rome; 1673 else 1674 hu->hdev->set_bdaddr = qca_set_bdaddr; 1675 1676 return ret; 1677 } 1678 1679 static const struct hci_uart_proto qca_proto = { 1680 .id = HCI_UART_QCA, 1681 .name = "QCA", 1682 .manufacturer = 29, 1683 .init_speed = 115200, 1684 .oper_speed = 3000000, 1685 .open = qca_open, 1686 .close = qca_close, 1687 .flush = qca_flush, 1688 .setup = qca_setup, 1689 .recv = qca_recv, 1690 .enqueue = qca_enqueue, 1691 .dequeue = qca_dequeue, 1692 }; 1693 1694 static const struct qca_vreg_data qca_soc_data_wcn3990 = { 1695 .soc_type = QCA_WCN3990, 1696 .vregs = (struct qca_vreg []) { 1697 { "vddio", 15000 }, 1698 { "vddxo", 80000 }, 1699 { "vddrf", 300000 }, 1700 { "vddch0", 450000 }, 1701 }, 1702 .num_vregs = 4, 1703 }; 1704 1705 static const struct qca_vreg_data qca_soc_data_wcn3991 = { 1706 .soc_type = QCA_WCN3991, 1707 .vregs = (struct qca_vreg []) { 1708 { "vddio", 15000 }, 1709 { "vddxo", 80000 }, 1710 { "vddrf", 300000 }, 1711 { "vddch0", 450000 }, 1712 }, 1713 .num_vregs = 4, 1714 }; 1715 1716 static const struct qca_vreg_data qca_soc_data_wcn3998 = { 1717 .soc_type = QCA_WCN3998, 1718 .vregs = (struct qca_vreg []) { 1719 { "vddio", 10000 }, 1720 { "vddxo", 80000 }, 1721 { "vddrf", 300000 }, 1722 { "vddch0", 450000 }, 1723 }, 1724 .num_vregs = 4, 1725 }; 1726 1727 static const struct qca_vreg_data qca_soc_data_qca6390 = { 1728 .soc_type = QCA_QCA6390, 1729 .num_vregs = 0, 1730 }; 1731 1732 static void qca_power_shutdown(struct hci_uart *hu) 1733 { 1734 struct qca_serdev *qcadev; 1735 struct qca_data *qca = hu->priv; 1736 unsigned long flags; 1737 enum qca_btsoc_type soc_type = qca_soc_type(hu); 1738 1739 qcadev = serdev_device_get_drvdata(hu->serdev); 1740 1741 /* From this point we go into power off state. But serial port is 1742 * still open, stop queueing the IBS data and flush all the buffered 1743 * data in skb's. 1744 */ 1745 spin_lock_irqsave(&qca->hci_ibs_lock, flags); 1746 clear_bit(QCA_IBS_ENABLED, &qca->flags); 1747 qca_flush(hu); 1748 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 1749 1750 hu->hdev->hw_error = NULL; 1751 hu->hdev->cmd_timeout = NULL; 1752 1753 /* Non-serdev device usually is powered by external power 1754 * and don't need additional action in driver for power down 1755 */ 1756 if (!hu->serdev) 1757 return; 1758 1759 if (qca_is_wcn399x(soc_type)) { 1760 host_set_baudrate(hu, 2400); 1761 qca_send_power_pulse(hu, false); 1762 qca_regulator_disable(qcadev); 1763 } else if (qcadev->bt_en) { 1764 gpiod_set_value_cansleep(qcadev->bt_en, 0); 1765 } 1766 } 1767 1768 static int qca_power_off(struct hci_dev *hdev) 1769 { 1770 struct hci_uart *hu = hci_get_drvdata(hdev); 1771 struct qca_data *qca = hu->priv; 1772 enum qca_btsoc_type soc_type = qca_soc_type(hu); 1773 1774 /* Stop sending shutdown command if soc crashes. */ 1775 if (soc_type != QCA_ROME 1776 && qca->memdump_state == QCA_MEMDUMP_IDLE) { 1777 qca_send_pre_shutdown_cmd(hdev); 1778 usleep_range(8000, 10000); 1779 } 1780 1781 qca->memdump_state = QCA_MEMDUMP_IDLE; 1782 qca_power_shutdown(hu); 1783 return 0; 1784 } 1785 1786 static int qca_regulator_enable(struct qca_serdev *qcadev) 1787 { 1788 struct qca_power *power = qcadev->bt_power; 1789 int ret; 1790 1791 /* Already enabled */ 1792 if (power->vregs_on) 1793 return 0; 1794 1795 BT_DBG("enabling %d regulators)", power->num_vregs); 1796 1797 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk); 1798 if (ret) 1799 return ret; 1800 1801 power->vregs_on = true; 1802 1803 ret = clk_prepare_enable(qcadev->susclk); 1804 if (ret) 1805 qca_regulator_disable(qcadev); 1806 1807 return ret; 1808 } 1809 1810 static void qca_regulator_disable(struct qca_serdev *qcadev) 1811 { 1812 struct qca_power *power; 1813 1814 if (!qcadev) 1815 return; 1816 1817 power = qcadev->bt_power; 1818 1819 /* Already disabled? */ 1820 if (!power->vregs_on) 1821 return; 1822 1823 regulator_bulk_disable(power->num_vregs, power->vreg_bulk); 1824 power->vregs_on = false; 1825 1826 clk_disable_unprepare(qcadev->susclk); 1827 } 1828 1829 static int qca_init_regulators(struct qca_power *qca, 1830 const struct qca_vreg *vregs, size_t num_vregs) 1831 { 1832 struct regulator_bulk_data *bulk; 1833 int ret; 1834 int i; 1835 1836 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL); 1837 if (!bulk) 1838 return -ENOMEM; 1839 1840 for (i = 0; i < num_vregs; i++) 1841 bulk[i].supply = vregs[i].name; 1842 1843 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk); 1844 if (ret < 0) 1845 return ret; 1846 1847 for (i = 0; i < num_vregs; i++) { 1848 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA); 1849 if (ret) 1850 return ret; 1851 } 1852 1853 qca->vreg_bulk = bulk; 1854 qca->num_vregs = num_vregs; 1855 1856 return 0; 1857 } 1858 1859 static int qca_serdev_probe(struct serdev_device *serdev) 1860 { 1861 struct qca_serdev *qcadev; 1862 struct hci_dev *hdev; 1863 const struct qca_vreg_data *data; 1864 int err; 1865 bool power_ctrl_enabled = true; 1866 1867 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL); 1868 if (!qcadev) 1869 return -ENOMEM; 1870 1871 qcadev->serdev_hu.serdev = serdev; 1872 data = device_get_match_data(&serdev->dev); 1873 serdev_device_set_drvdata(serdev, qcadev); 1874 device_property_read_string(&serdev->dev, "firmware-name", 1875 &qcadev->firmware_name); 1876 device_property_read_u32(&serdev->dev, "max-speed", 1877 &qcadev->oper_speed); 1878 if (!qcadev->oper_speed) 1879 BT_DBG("UART will pick default operating speed"); 1880 1881 if (data && qca_is_wcn399x(data->soc_type)) { 1882 qcadev->btsoc_type = data->soc_type; 1883 qcadev->bt_power = devm_kzalloc(&serdev->dev, 1884 sizeof(struct qca_power), 1885 GFP_KERNEL); 1886 if (!qcadev->bt_power) 1887 return -ENOMEM; 1888 1889 qcadev->bt_power->dev = &serdev->dev; 1890 err = qca_init_regulators(qcadev->bt_power, data->vregs, 1891 data->num_vregs); 1892 if (err) { 1893 BT_ERR("Failed to init regulators:%d", err); 1894 return err; 1895 } 1896 1897 qcadev->bt_power->vregs_on = false; 1898 1899 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL); 1900 if (IS_ERR(qcadev->susclk)) { 1901 dev_err(&serdev->dev, "failed to acquire clk\n"); 1902 return PTR_ERR(qcadev->susclk); 1903 } 1904 1905 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); 1906 if (err) { 1907 BT_ERR("wcn3990 serdev registration failed"); 1908 return err; 1909 } 1910 } else { 1911 if (data) 1912 qcadev->btsoc_type = data->soc_type; 1913 else 1914 qcadev->btsoc_type = QCA_ROME; 1915 1916 qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable", 1917 GPIOD_OUT_LOW); 1918 if (!qcadev->bt_en) { 1919 dev_warn(&serdev->dev, "failed to acquire enable gpio\n"); 1920 power_ctrl_enabled = false; 1921 } 1922 1923 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL); 1924 if (!qcadev->susclk) { 1925 dev_warn(&serdev->dev, "failed to acquire clk\n"); 1926 } else { 1927 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ); 1928 if (err) 1929 return err; 1930 1931 err = clk_prepare_enable(qcadev->susclk); 1932 if (err) 1933 return err; 1934 } 1935 1936 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); 1937 if (err) { 1938 BT_ERR("Rome serdev registration failed"); 1939 if (qcadev->susclk) 1940 clk_disable_unprepare(qcadev->susclk); 1941 return err; 1942 } 1943 } 1944 1945 if (power_ctrl_enabled) { 1946 hdev = qcadev->serdev_hu.hdev; 1947 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks); 1948 hdev->shutdown = qca_power_off; 1949 } 1950 1951 return 0; 1952 } 1953 1954 static void qca_serdev_remove(struct serdev_device *serdev) 1955 { 1956 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev); 1957 1958 if (qca_is_wcn399x(qcadev->btsoc_type)) 1959 qca_power_shutdown(&qcadev->serdev_hu); 1960 else if (qcadev->susclk) 1961 clk_disable_unprepare(qcadev->susclk); 1962 1963 hci_uart_unregister_device(&qcadev->serdev_hu); 1964 } 1965 1966 static int __maybe_unused qca_suspend(struct device *dev) 1967 { 1968 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev); 1969 struct hci_uart *hu = hci_get_drvdata(hdev); 1970 struct qca_data *qca = hu->priv; 1971 unsigned long flags; 1972 int ret = 0; 1973 u8 cmd; 1974 1975 set_bit(QCA_SUSPENDING, &qca->flags); 1976 1977 /* Device is downloading patch or doesn't support in-band sleep. */ 1978 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) 1979 return 0; 1980 1981 cancel_work_sync(&qca->ws_awake_device); 1982 cancel_work_sync(&qca->ws_awake_rx); 1983 1984 spin_lock_irqsave_nested(&qca->hci_ibs_lock, 1985 flags, SINGLE_DEPTH_NESTING); 1986 1987 switch (qca->tx_ibs_state) { 1988 case HCI_IBS_TX_WAKING: 1989 del_timer(&qca->wake_retrans_timer); 1990 /* Fall through */ 1991 case HCI_IBS_TX_AWAKE: 1992 del_timer(&qca->tx_idle_timer); 1993 1994 serdev_device_write_flush(hu->serdev); 1995 cmd = HCI_IBS_SLEEP_IND; 1996 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd)); 1997 1998 if (ret < 0) { 1999 BT_ERR("Failed to send SLEEP to device"); 2000 break; 2001 } 2002 2003 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; 2004 qca->ibs_sent_slps++; 2005 2006 qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off); 2007 break; 2008 2009 case HCI_IBS_TX_ASLEEP: 2010 break; 2011 2012 default: 2013 BT_ERR("Spurious tx state %d", qca->tx_ibs_state); 2014 ret = -EINVAL; 2015 break; 2016 } 2017 2018 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); 2019 2020 if (ret < 0) 2021 goto error; 2022 2023 serdev_device_wait_until_sent(hu->serdev, 2024 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS)); 2025 2026 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going 2027 * to sleep, so that the packet does not wake the system later. 2028 */ 2029 2030 ret = wait_event_interruptible_timeout(qca->suspend_wait_q, 2031 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP, 2032 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS)); 2033 2034 if (ret > 0) 2035 return 0; 2036 2037 if (ret == 0) 2038 ret = -ETIMEDOUT; 2039 2040 error: 2041 clear_bit(QCA_SUSPENDING, &qca->flags); 2042 2043 return ret; 2044 } 2045 2046 static int __maybe_unused qca_resume(struct device *dev) 2047 { 2048 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev); 2049 struct hci_uart *hu = hci_get_drvdata(hdev); 2050 struct qca_data *qca = hu->priv; 2051 2052 clear_bit(QCA_SUSPENDING, &qca->flags); 2053 2054 return 0; 2055 } 2056 2057 static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume); 2058 2059 #ifdef CONFIG_OF 2060 static const struct of_device_id qca_bluetooth_of_match[] = { 2061 { .compatible = "qcom,qca6174-bt" }, 2062 { .compatible = "qcom,qca6390-bt", .data = &qca_soc_data_qca6390}, 2063 { .compatible = "qcom,qca9377-bt" }, 2064 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990}, 2065 { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991}, 2066 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998}, 2067 { /* sentinel */ } 2068 }; 2069 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match); 2070 #endif 2071 2072 #ifdef CONFIG_ACPI 2073 static const struct acpi_device_id qca_bluetooth_acpi_match[] = { 2074 { "QCOM6390", (kernel_ulong_t)&qca_soc_data_qca6390 }, 2075 { "DLA16390", (kernel_ulong_t)&qca_soc_data_qca6390 }, 2076 { "DLB16390", (kernel_ulong_t)&qca_soc_data_qca6390 }, 2077 { "DLB26390", (kernel_ulong_t)&qca_soc_data_qca6390 }, 2078 { }, 2079 }; 2080 MODULE_DEVICE_TABLE(acpi, qca_bluetooth_acpi_match); 2081 #endif 2082 2083 2084 static struct serdev_device_driver qca_serdev_driver = { 2085 .probe = qca_serdev_probe, 2086 .remove = qca_serdev_remove, 2087 .driver = { 2088 .name = "hci_uart_qca", 2089 .of_match_table = of_match_ptr(qca_bluetooth_of_match), 2090 .acpi_match_table = ACPI_PTR(qca_bluetooth_acpi_match), 2091 .pm = &qca_pm_ops, 2092 }, 2093 }; 2094 2095 int __init qca_init(void) 2096 { 2097 serdev_device_driver_register(&qca_serdev_driver); 2098 2099 return hci_uart_register_proto(&qca_proto); 2100 } 2101 2102 int __exit qca_deinit(void) 2103 { 2104 serdev_device_driver_unregister(&qca_serdev_driver); 2105 2106 return hci_uart_unregister_proto(&qca_proto); 2107 } 2108