1 // SPDX-License-Identifier: GPL-2.0-only
3 * Bluetooth Software UART Qualcomm protocol
5 * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management
6 * protocol extension to H4.
8 * Copyright (C) 2007 Texas Instruments, Inc.
9 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
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.
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/platform_device.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/serdev.h>
32 #include <asm/unaligned.h>
34 #include <net/bluetooth/bluetooth.h>
35 #include <net/bluetooth/hci_core.h>
40 /* HCI_IBS protocol messages */
41 #define HCI_IBS_SLEEP_IND 0xFE
42 #define HCI_IBS_WAKE_IND 0xFD
43 #define HCI_IBS_WAKE_ACK 0xFC
44 #define HCI_MAX_IBS_SIZE 10
46 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
47 #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40
48 #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000
49 #define CMD_TRANS_TIMEOUT_MS 100
50 #define MEMDUMP_TIMEOUT_MS 8000
53 #define SUSCLK_RATE_32KHZ 32768
55 /* Controller debug log header */
56 #define QCA_DEBUG_HANDLE 0x2EDC
58 /* Controller dump header */
59 #define QCA_SSR_DUMP_HANDLE 0x0108
60 #define QCA_DUMP_PACKET_SIZE 255
61 #define QCA_LAST_SEQUENCE_NUM 0xFFFF
62 #define QCA_CRASHBYTE_PACKET_LEN 1096
63 #define QCA_MEMDUMP_BYTE 0xFB
67 QCA_DROP_VENDOR_EVENT,
69 QCA_MEMDUMP_COLLECTION
73 /* HCI_IBS transmit side sleep protocol states */
80 /* HCI_IBS receive side sleep protocol states */
86 /* HCI_IBS transmit and receive side clock state vote */
87 enum hci_ibs_clock_state_vote {
88 HCI_IBS_VOTE_STATS_UPDATE,
89 HCI_IBS_TX_VOTE_CLOCK_ON,
90 HCI_IBS_TX_VOTE_CLOCK_OFF,
91 HCI_IBS_RX_VOTE_CLOCK_ON,
92 HCI_IBS_RX_VOTE_CLOCK_OFF,
95 /* Controller memory dump states */
96 enum qca_memdump_states {
98 QCA_MEMDUMP_COLLECTING,
99 QCA_MEMDUMP_COLLECTED,
103 struct qca_memdump_data {
104 char *memdump_buf_head;
105 char *memdump_buf_tail;
110 struct qca_memdump_event_hdr {
119 struct qca_dump_size {
125 struct sk_buff *rx_skb;
126 struct sk_buff_head txq;
127 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
128 struct sk_buff_head rx_memdump_q; /* Memdump wait queue */
129 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
130 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
131 u8 rx_ibs_state; /* HCI_IBS receive side power state */
132 bool tx_vote; /* Clock must be on for TX */
133 bool rx_vote; /* Clock must be on for RX */
134 struct timer_list tx_idle_timer;
136 struct timer_list wake_retrans_timer;
138 struct timer_list memdump_timer;
139 struct workqueue_struct *workqueue;
140 struct work_struct ws_awake_rx;
141 struct work_struct ws_awake_device;
142 struct work_struct ws_rx_vote_off;
143 struct work_struct ws_tx_vote_off;
144 struct work_struct ctrl_memdump_evt;
145 struct qca_memdump_data *qca_memdump;
147 struct completion drop_ev_comp;
148 wait_queue_head_t suspend_wait_q;
149 enum qca_memdump_states memdump_state;
151 /* For debugging purpose */
169 enum qca_speed_type {
175 * Voltage regulator information required for configuring the
176 * QCA Bluetooth chipset
180 unsigned int load_uA;
183 struct qca_vreg_data {
184 enum qca_btsoc_type soc_type;
185 struct qca_vreg *vregs;
190 * Platform data for the QCA Bluetooth power driver.
194 struct regulator_bulk_data *vreg_bulk;
200 struct hci_uart serdev_hu;
201 struct gpio_desc *bt_en;
203 enum qca_btsoc_type btsoc_type;
204 struct qca_power *bt_power;
207 const char *firmware_name;
210 static int qca_regulator_enable(struct qca_serdev *qcadev);
211 static void qca_regulator_disable(struct qca_serdev *qcadev);
212 static void qca_power_shutdown(struct hci_uart *hu);
213 static int qca_power_off(struct hci_dev *hdev);
214 static void qca_controller_memdump(struct work_struct *work);
216 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
218 enum qca_btsoc_type soc_type;
221 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
223 soc_type = qsd->btsoc_type;
231 static const char *qca_get_firmware_name(struct hci_uart *hu)
234 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
236 return qsd->firmware_name;
242 static void __serial_clock_on(struct tty_struct *tty)
244 /* TODO: Some chipset requires to enable UART clock on client
245 * side to save power consumption or manual work is required.
246 * Please put your code to control UART clock here if needed
250 static void __serial_clock_off(struct tty_struct *tty)
252 /* TODO: Some chipset requires to disable UART clock on client
253 * side to save power consumption or manual work is required.
254 * Please put your code to control UART clock off here if needed
258 /* serial_clock_vote needs to be called with the ibs lock held */
259 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
261 struct qca_data *qca = hu->priv;
264 bool old_vote = (qca->tx_vote | qca->rx_vote);
268 case HCI_IBS_VOTE_STATS_UPDATE:
269 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
272 qca->vote_off_ms += diff;
274 qca->vote_on_ms += diff;
277 case HCI_IBS_TX_VOTE_CLOCK_ON:
283 case HCI_IBS_RX_VOTE_CLOCK_ON:
289 case HCI_IBS_TX_VOTE_CLOCK_OFF:
290 qca->tx_vote = false;
292 new_vote = qca->rx_vote | qca->tx_vote;
295 case HCI_IBS_RX_VOTE_CLOCK_OFF:
296 qca->rx_vote = false;
298 new_vote = qca->rx_vote | qca->tx_vote;
302 BT_ERR("Voting irregularity");
306 if (new_vote != old_vote) {
308 __serial_clock_on(hu->tty);
310 __serial_clock_off(hu->tty);
312 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
313 vote ? "true" : "false");
315 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
319 qca->vote_off_ms += diff;
322 qca->vote_on_ms += diff;
324 qca->vote_last_jif = jiffies;
328 /* Builds and sends an HCI_IBS command packet.
329 * These are very simple packets with only 1 cmd byte.
331 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
334 struct sk_buff *skb = NULL;
335 struct qca_data *qca = hu->priv;
337 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
339 skb = bt_skb_alloc(1, GFP_ATOMIC);
341 BT_ERR("Failed to allocate memory for HCI_IBS packet");
345 /* Assign HCI_IBS type */
346 skb_put_u8(skb, cmd);
348 skb_queue_tail(&qca->txq, skb);
353 static void qca_wq_awake_device(struct work_struct *work)
355 struct qca_data *qca = container_of(work, struct qca_data,
357 struct hci_uart *hu = qca->hu;
358 unsigned long retrans_delay;
361 BT_DBG("hu %p wq awake device", hu);
363 /* Vote for serial clock */
364 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
366 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
368 /* Send wake indication to device */
369 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
370 BT_ERR("Failed to send WAKE to device");
372 qca->ibs_sent_wakes++;
374 /* Start retransmit timer */
375 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
376 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
378 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
380 /* Actually send the packets */
381 hci_uart_tx_wakeup(hu);
384 static void qca_wq_awake_rx(struct work_struct *work)
386 struct qca_data *qca = container_of(work, struct qca_data,
388 struct hci_uart *hu = qca->hu;
391 BT_DBG("hu %p wq awake rx", hu);
393 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
395 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
396 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
398 /* Always acknowledge device wake up,
399 * sending IBS message doesn't count as TX ON.
401 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
402 BT_ERR("Failed to acknowledge device wake up");
404 qca->ibs_sent_wacks++;
406 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
408 /* Actually send the packets */
409 hci_uart_tx_wakeup(hu);
412 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
414 struct qca_data *qca = container_of(work, struct qca_data,
416 struct hci_uart *hu = qca->hu;
418 BT_DBG("hu %p rx clock vote off", hu);
420 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
423 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
425 struct qca_data *qca = container_of(work, struct qca_data,
427 struct hci_uart *hu = qca->hu;
429 BT_DBG("hu %p tx clock vote off", hu);
431 /* Run HCI tx handling unlocked */
432 hci_uart_tx_wakeup(hu);
434 /* Now that message queued to tty driver, vote for tty clocks off.
435 * It is up to the tty driver to pend the clocks off until tx done.
437 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
440 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
442 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
443 struct hci_uart *hu = qca->hu;
446 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
448 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
449 flags, SINGLE_DEPTH_NESTING);
451 switch (qca->tx_ibs_state) {
452 case HCI_IBS_TX_AWAKE:
453 /* TX_IDLE, go to SLEEP */
454 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
455 BT_ERR("Failed to send SLEEP to device");
458 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
459 qca->ibs_sent_slps++;
460 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
463 case HCI_IBS_TX_ASLEEP:
464 case HCI_IBS_TX_WAKING:
468 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
472 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
475 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
477 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
478 struct hci_uart *hu = qca->hu;
479 unsigned long flags, retrans_delay;
480 bool retransmit = false;
482 BT_DBG("hu %p wake retransmit timeout in %d state",
483 hu, qca->tx_ibs_state);
485 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
486 flags, SINGLE_DEPTH_NESTING);
488 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
489 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
490 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
494 switch (qca->tx_ibs_state) {
495 case HCI_IBS_TX_WAKING:
496 /* No WAKE_ACK, retransmit WAKE */
498 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
499 BT_ERR("Failed to acknowledge device wake up");
502 qca->ibs_sent_wakes++;
503 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
504 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
507 case HCI_IBS_TX_ASLEEP:
508 case HCI_IBS_TX_AWAKE:
512 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
516 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
519 hci_uart_tx_wakeup(hu);
522 static void hci_memdump_timeout(struct timer_list *t)
524 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
525 struct hci_uart *hu = qca->hu;
526 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
527 char *memdump_buf = qca_memdump->memdump_buf_tail;
529 bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout");
530 /* Inject hw error event to reset the device and driver. */
531 hci_reset_dev(hu->hdev);
534 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
535 del_timer(&qca->memdump_timer);
536 cancel_work_sync(&qca->ctrl_memdump_evt);
539 /* Initialize protocol */
540 static int qca_open(struct hci_uart *hu)
542 struct qca_serdev *qcadev;
543 struct qca_data *qca;
546 BT_DBG("hu %p qca_open", hu);
548 if (!hci_uart_has_flow_control(hu))
551 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
555 skb_queue_head_init(&qca->txq);
556 skb_queue_head_init(&qca->tx_wait_q);
557 skb_queue_head_init(&qca->rx_memdump_q);
558 spin_lock_init(&qca->hci_ibs_lock);
559 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
560 if (!qca->workqueue) {
561 BT_ERR("QCA Workqueue not initialized properly");
566 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
567 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
568 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
569 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
570 INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump);
571 init_waitqueue_head(&qca->suspend_wait_q);
574 init_completion(&qca->drop_ev_comp);
576 /* Assume we start with both sides asleep -- extra wakes OK */
577 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
578 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
580 qca->vote_last_jif = jiffies;
586 qcadev = serdev_device_get_drvdata(hu->serdev);
587 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
588 gpiod_set_value_cansleep(qcadev->bt_en, 1);
589 /* Controller needs time to bootup. */
592 hu->init_speed = qcadev->init_speed;
593 hu->oper_speed = qcadev->oper_speed;
594 ret = qca_regulator_enable(qcadev);
596 destroy_workqueue(qca->workqueue);
597 kfree_skb(qca->rx_skb);
605 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
606 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
608 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
609 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
610 timer_setup(&qca->memdump_timer, hci_memdump_timeout, 0);
612 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
613 qca->tx_idle_delay, qca->wake_retrans);
618 static void qca_debugfs_init(struct hci_dev *hdev)
620 struct hci_uart *hu = hci_get_drvdata(hdev);
621 struct qca_data *qca = hu->priv;
622 struct dentry *ibs_dir;
628 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
632 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
633 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
634 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
635 &qca->ibs_sent_slps);
636 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
637 &qca->ibs_sent_wakes);
638 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
639 &qca->ibs_sent_wacks);
640 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
641 &qca->ibs_recv_slps);
642 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
643 &qca->ibs_recv_wakes);
644 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
645 &qca->ibs_recv_wacks);
646 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
647 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
648 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
649 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
650 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
651 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
652 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
653 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
654 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
655 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
658 mode = S_IRUGO | S_IWUSR;
659 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
660 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
661 &qca->tx_idle_delay);
664 /* Flush protocol data */
665 static int qca_flush(struct hci_uart *hu)
667 struct qca_data *qca = hu->priv;
669 BT_DBG("hu %p qca flush", hu);
671 skb_queue_purge(&qca->tx_wait_q);
672 skb_queue_purge(&qca->txq);
678 static int qca_close(struct hci_uart *hu)
680 struct qca_serdev *qcadev;
681 struct qca_data *qca = hu->priv;
683 BT_DBG("hu %p qca close", hu);
685 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
687 skb_queue_purge(&qca->tx_wait_q);
688 skb_queue_purge(&qca->txq);
689 skb_queue_purge(&qca->rx_memdump_q);
690 del_timer(&qca->tx_idle_timer);
691 del_timer(&qca->wake_retrans_timer);
692 del_timer(&qca->memdump_timer);
693 destroy_workqueue(qca->workqueue);
697 qcadev = serdev_device_get_drvdata(hu->serdev);
698 if (qca_is_wcn399x(qcadev->btsoc_type))
699 qca_power_shutdown(hu);
701 gpiod_set_value_cansleep(qcadev->bt_en, 0);
705 kfree_skb(qca->rx_skb);
714 /* Called upon a wake-up-indication from the device.
716 static void device_want_to_wakeup(struct hci_uart *hu)
719 struct qca_data *qca = hu->priv;
721 BT_DBG("hu %p want to wake up", hu);
723 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
725 qca->ibs_recv_wakes++;
727 /* Don't wake the rx up when suspending. */
728 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
729 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
733 switch (qca->rx_ibs_state) {
734 case HCI_IBS_RX_ASLEEP:
735 /* Make sure clock is on - we may have turned clock off since
736 * receiving the wake up indicator awake rx clock.
738 queue_work(qca->workqueue, &qca->ws_awake_rx);
739 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
742 case HCI_IBS_RX_AWAKE:
743 /* Always acknowledge device wake up,
744 * sending IBS message doesn't count as TX ON.
746 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
747 BT_ERR("Failed to acknowledge device wake up");
750 qca->ibs_sent_wacks++;
754 /* Any other state is illegal */
755 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
760 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
762 /* Actually send the packets */
763 hci_uart_tx_wakeup(hu);
766 /* Called upon a sleep-indication from the device.
768 static void device_want_to_sleep(struct hci_uart *hu)
771 struct qca_data *qca = hu->priv;
773 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
775 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
777 qca->ibs_recv_slps++;
779 switch (qca->rx_ibs_state) {
780 case HCI_IBS_RX_AWAKE:
782 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
783 /* Vote off rx clock under workqueue */
784 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
787 case HCI_IBS_RX_ASLEEP:
791 /* Any other state is illegal */
792 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
797 wake_up_interruptible(&qca->suspend_wait_q);
799 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
802 /* Called upon wake-up-acknowledgement from the device
804 static void device_woke_up(struct hci_uart *hu)
806 unsigned long flags, idle_delay;
807 struct qca_data *qca = hu->priv;
808 struct sk_buff *skb = NULL;
810 BT_DBG("hu %p woke up", hu);
812 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
814 qca->ibs_recv_wacks++;
816 /* Don't react to the wake-up-acknowledgment when suspending. */
817 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
818 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
822 switch (qca->tx_ibs_state) {
823 case HCI_IBS_TX_AWAKE:
824 /* Expect one if we send 2 WAKEs */
825 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
829 case HCI_IBS_TX_WAKING:
830 /* Send pending packets */
831 while ((skb = skb_dequeue(&qca->tx_wait_q)))
832 skb_queue_tail(&qca->txq, skb);
834 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
835 del_timer(&qca->wake_retrans_timer);
836 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
837 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
838 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
841 case HCI_IBS_TX_ASLEEP:
845 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
850 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
852 /* Actually send the packets */
853 hci_uart_tx_wakeup(hu);
856 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
857 * two simultaneous tasklets.
859 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
861 unsigned long flags = 0, idle_delay;
862 struct qca_data *qca = hu->priv;
864 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
867 /* Prepend skb with frame type */
868 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
870 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
872 /* Don't go to sleep in middle of patch download or
873 * Out-Of-Band(GPIOs control) sleep is selected.
874 * Don't wake the device up when suspending.
876 if (!test_bit(QCA_IBS_ENABLED, &qca->flags) ||
877 test_bit(QCA_SUSPENDING, &qca->flags)) {
878 skb_queue_tail(&qca->txq, skb);
879 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
883 /* Act according to current state */
884 switch (qca->tx_ibs_state) {
885 case HCI_IBS_TX_AWAKE:
886 BT_DBG("Device awake, sending normally");
887 skb_queue_tail(&qca->txq, skb);
888 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
889 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
892 case HCI_IBS_TX_ASLEEP:
893 BT_DBG("Device asleep, waking up and queueing packet");
894 /* Save packet for later */
895 skb_queue_tail(&qca->tx_wait_q, skb);
897 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
898 /* Schedule a work queue to wake up device */
899 queue_work(qca->workqueue, &qca->ws_awake_device);
902 case HCI_IBS_TX_WAKING:
903 BT_DBG("Device waking up, queueing packet");
904 /* Transient state; just keep packet for later */
905 skb_queue_tail(&qca->tx_wait_q, skb);
909 BT_ERR("Illegal tx state: %d (losing packet)",
915 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
920 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
922 struct hci_uart *hu = hci_get_drvdata(hdev);
924 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
926 device_want_to_sleep(hu);
932 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
934 struct hci_uart *hu = hci_get_drvdata(hdev);
936 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
938 device_want_to_wakeup(hu);
944 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
946 struct hci_uart *hu = hci_get_drvdata(hdev);
948 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
956 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
958 /* We receive debug logs from chip as an ACL packets.
959 * Instead of sending the data to ACL to decode the
960 * received data, we are pushing them to the above layers
961 * as a diagnostic packet.
963 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
964 return hci_recv_diag(hdev, skb);
966 return hci_recv_frame(hdev, skb);
969 static void qca_controller_memdump(struct work_struct *work)
971 struct qca_data *qca = container_of(work, struct qca_data,
973 struct hci_uart *hu = qca->hu;
975 struct qca_memdump_event_hdr *cmd_hdr;
976 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
977 struct qca_dump_size *dump;
979 char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 };
983 while ((skb = skb_dequeue(&qca->rx_memdump_q))) {
986 qca_memdump = kzalloc(sizeof(struct qca_memdump_data),
991 qca->qca_memdump = qca_memdump;
994 qca->memdump_state = QCA_MEMDUMP_COLLECTING;
995 cmd_hdr = (void *) skb->data;
996 seq_no = __le16_to_cpu(cmd_hdr->seq_no);
997 skb_pull(skb, sizeof(struct qca_memdump_event_hdr));
1001 /* This is the first frame of memdump packet from
1002 * the controller, Disable IBS to recevie dump
1003 * with out any interruption, ideally time required for
1004 * the controller to send the dump is 8 seconds. let us
1005 * start timer to handle this asynchronous activity.
1007 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1008 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1009 dump = (void *) skb->data;
1010 dump_size = __le32_to_cpu(dump->dump_size);
1012 bt_dev_err(hu->hdev, "Rx invalid memdump size");
1017 bt_dev_info(hu->hdev, "QCA collecting dump of size:%u",
1019 mod_timer(&qca->memdump_timer, (jiffies +
1020 msecs_to_jiffies(MEMDUMP_TIMEOUT_MS)));
1022 skb_pull(skb, sizeof(dump_size));
1023 memdump_buf = vmalloc(dump_size);
1024 qca_memdump->memdump_buf_head = memdump_buf;
1025 qca_memdump->memdump_buf_tail = memdump_buf;
1028 memdump_buf = qca_memdump->memdump_buf_tail;
1030 /* If sequence no 0 is missed then there is no point in
1031 * accepting the other sequences.
1034 bt_dev_err(hu->hdev, "QCA: Discarding other packets");
1037 qca->qca_memdump = NULL;
1041 /* There could be chance of missing some packets from
1042 * the controller. In such cases let us store the dummy
1043 * packets in the buffer.
1045 while ((seq_no > qca_memdump->current_seq_no + 1) &&
1046 seq_no != QCA_LAST_SEQUENCE_NUM) {
1047 bt_dev_err(hu->hdev, "QCA controller missed packet:%d",
1048 qca_memdump->current_seq_no);
1049 memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE);
1050 memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE;
1051 qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE;
1052 qca_memdump->current_seq_no++;
1055 memcpy(memdump_buf, (unsigned char *) skb->data, skb->len);
1056 memdump_buf = memdump_buf + skb->len;
1057 qca_memdump->memdump_buf_tail = memdump_buf;
1058 qca_memdump->current_seq_no = seq_no + 1;
1059 qca_memdump->received_dump += skb->len;
1060 qca->qca_memdump = qca_memdump;
1062 if (seq_no == QCA_LAST_SEQUENCE_NUM) {
1063 bt_dev_info(hu->hdev, "QCA writing crash dump of size %d bytes",
1064 qca_memdump->received_dump);
1065 memdump_buf = qca_memdump->memdump_buf_head;
1066 dev_coredumpv(&hu->serdev->dev, memdump_buf,
1067 qca_memdump->received_dump, GFP_KERNEL);
1068 del_timer(&qca->memdump_timer);
1069 kfree(qca->qca_memdump);
1070 qca->qca_memdump = NULL;
1071 qca->memdump_state = QCA_MEMDUMP_COLLECTED;
1077 int qca_controller_memdump_event(struct hci_dev *hdev, struct sk_buff *skb)
1079 struct hci_uart *hu = hci_get_drvdata(hdev);
1080 struct qca_data *qca = hu->priv;
1082 skb_queue_tail(&qca->rx_memdump_q, skb);
1083 queue_work(qca->workqueue, &qca->ctrl_memdump_evt);
1088 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1090 struct hci_uart *hu = hci_get_drvdata(hdev);
1091 struct qca_data *qca = hu->priv;
1093 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
1094 struct hci_event_hdr *hdr = (void *)skb->data;
1096 /* For the WCN3990 the vendor command for a baudrate change
1097 * isn't sent as synchronous HCI command, because the
1098 * controller sends the corresponding vendor event with the
1099 * new baudrate. The event is received and properly decoded
1100 * after changing the baudrate of the host port. It needs to
1101 * be dropped, otherwise it can be misinterpreted as
1102 * response to a later firmware download command (also a
1106 if (hdr->evt == HCI_EV_VENDOR)
1107 complete(&qca->drop_ev_comp);
1113 /* We receive chip memory dump as an event packet, With a dedicated
1114 * handler followed by a hardware error event. When this event is
1115 * received we store dump into a file before closing hci. This
1116 * dump will help in triaging the issues.
1118 if ((skb->data[0] == HCI_VENDOR_PKT) &&
1119 (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE))
1120 return qca_controller_memdump_event(hdev, skb);
1122 return hci_recv_frame(hdev, skb);
1125 #define QCA_IBS_SLEEP_IND_EVENT \
1126 .type = HCI_IBS_SLEEP_IND, \
1130 .maxlen = HCI_MAX_IBS_SIZE
1132 #define QCA_IBS_WAKE_IND_EVENT \
1133 .type = HCI_IBS_WAKE_IND, \
1137 .maxlen = HCI_MAX_IBS_SIZE
1139 #define QCA_IBS_WAKE_ACK_EVENT \
1140 .type = HCI_IBS_WAKE_ACK, \
1144 .maxlen = HCI_MAX_IBS_SIZE
1146 static const struct h4_recv_pkt qca_recv_pkts[] = {
1147 { H4_RECV_ACL, .recv = qca_recv_acl_data },
1148 { H4_RECV_SCO, .recv = hci_recv_frame },
1149 { H4_RECV_EVENT, .recv = qca_recv_event },
1150 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
1151 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
1152 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
1155 static int qca_recv(struct hci_uart *hu, const void *data, int count)
1157 struct qca_data *qca = hu->priv;
1159 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1162 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
1163 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
1164 if (IS_ERR(qca->rx_skb)) {
1165 int err = PTR_ERR(qca->rx_skb);
1166 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
1174 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
1176 struct qca_data *qca = hu->priv;
1178 return skb_dequeue(&qca->txq);
1181 static uint8_t qca_get_baudrate_value(int speed)
1185 return QCA_BAUDRATE_9600;
1187 return QCA_BAUDRATE_19200;
1189 return QCA_BAUDRATE_38400;
1191 return QCA_BAUDRATE_57600;
1193 return QCA_BAUDRATE_115200;
1195 return QCA_BAUDRATE_230400;
1197 return QCA_BAUDRATE_460800;
1199 return QCA_BAUDRATE_500000;
1201 return QCA_BAUDRATE_921600;
1203 return QCA_BAUDRATE_1000000;
1205 return QCA_BAUDRATE_2000000;
1207 return QCA_BAUDRATE_3000000;
1209 return QCA_BAUDRATE_3200000;
1211 return QCA_BAUDRATE_3500000;
1213 return QCA_BAUDRATE_115200;
1217 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1219 struct hci_uart *hu = hci_get_drvdata(hdev);
1220 struct qca_data *qca = hu->priv;
1221 struct sk_buff *skb;
1222 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1224 if (baudrate > QCA_BAUDRATE_3200000)
1229 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1231 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1235 /* Assign commands to change baudrate and packet type. */
1236 skb_put_data(skb, cmd, sizeof(cmd));
1237 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1239 skb_queue_tail(&qca->txq, skb);
1240 hci_uart_tx_wakeup(hu);
1242 /* Wait for the baudrate change request to be sent */
1244 while (!skb_queue_empty(&qca->txq))
1245 usleep_range(100, 200);
1248 serdev_device_wait_until_sent(hu->serdev,
1249 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1251 /* Give the controller time to process the request */
1252 if (qca_is_wcn399x(qca_soc_type(hu)))
1260 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1263 serdev_device_set_baudrate(hu->serdev, speed);
1265 hci_uart_set_baudrate(hu, speed);
1268 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1271 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1272 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1274 /* These power pulses are single byte command which are sent
1275 * at required baudrate to wcn3990. On wcn3990, we have an external
1276 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1277 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1278 * and also we use the same power inputs to turn on and off for
1279 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1280 * we send a power on pulse at 115200 bps. This algorithm will help to
1281 * save power. Disabling hardware flow control is mandatory while
1282 * sending power pulses to SoC.
1284 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1286 serdev_device_write_flush(hu->serdev);
1287 hci_uart_set_flow_control(hu, true);
1288 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1290 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1294 serdev_device_wait_until_sent(hu->serdev, timeout);
1295 hci_uart_set_flow_control(hu, false);
1297 /* Give to controller time to boot/shutdown */
1306 static unsigned int qca_get_speed(struct hci_uart *hu,
1307 enum qca_speed_type speed_type)
1309 unsigned int speed = 0;
1311 if (speed_type == QCA_INIT_SPEED) {
1313 speed = hu->init_speed;
1314 else if (hu->proto->init_speed)
1315 speed = hu->proto->init_speed;
1318 speed = hu->oper_speed;
1319 else if (hu->proto->oper_speed)
1320 speed = hu->proto->oper_speed;
1326 static int qca_check_speeds(struct hci_uart *hu)
1328 if (qca_is_wcn399x(qca_soc_type(hu))) {
1329 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1330 !qca_get_speed(hu, QCA_OPER_SPEED))
1333 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1334 !qca_get_speed(hu, QCA_OPER_SPEED))
1341 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1343 unsigned int speed, qca_baudrate;
1344 struct qca_data *qca = hu->priv;
1347 if (speed_type == QCA_INIT_SPEED) {
1348 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1350 host_set_baudrate(hu, speed);
1352 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1354 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1358 /* Disable flow control for wcn3990 to deassert RTS while
1359 * changing the baudrate of chip and host.
1361 if (qca_is_wcn399x(soc_type))
1362 hci_uart_set_flow_control(hu, true);
1364 if (soc_type == QCA_WCN3990) {
1365 reinit_completion(&qca->drop_ev_comp);
1366 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1369 qca_baudrate = qca_get_baudrate_value(speed);
1370 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1371 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1375 host_set_baudrate(hu, speed);
1378 if (qca_is_wcn399x(soc_type))
1379 hci_uart_set_flow_control(hu, false);
1381 if (soc_type == QCA_WCN3990) {
1382 /* Wait for the controller to send the vendor event
1383 * for the baudrate change command.
1385 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1386 msecs_to_jiffies(100))) {
1387 bt_dev_err(hu->hdev,
1388 "Failed to change controller baudrate\n");
1392 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1399 static int qca_send_crashbuffer(struct hci_uart *hu)
1401 struct qca_data *qca = hu->priv;
1402 struct sk_buff *skb;
1404 skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL);
1406 bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet");
1410 /* We forcefully crash the controller, by sending 0xfb byte for
1411 * 1024 times. We also might have chance of losing data, To be
1412 * on safer side we send 1096 bytes to the SoC.
1414 memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE,
1415 QCA_CRASHBYTE_PACKET_LEN);
1416 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1417 bt_dev_info(hu->hdev, "crash the soc to collect controller dump");
1418 skb_queue_tail(&qca->txq, skb);
1419 hci_uart_tx_wakeup(hu);
1424 static void qca_wait_for_dump_collection(struct hci_dev *hdev)
1426 struct hci_uart *hu = hci_get_drvdata(hdev);
1427 struct qca_data *qca = hu->priv;
1428 struct qca_memdump_data *qca_memdump = qca->qca_memdump;
1429 char *memdump_buf = NULL;
1431 wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION,
1432 TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS);
1434 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1435 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1436 bt_dev_err(hu->hdev, "Clearing the buffers due to timeout");
1438 memdump_buf = qca_memdump->memdump_buf_tail;
1441 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1442 del_timer(&qca->memdump_timer);
1443 cancel_work_sync(&qca->ctrl_memdump_evt);
1447 static void qca_hw_error(struct hci_dev *hdev, u8 code)
1449 struct hci_uart *hu = hci_get_drvdata(hdev);
1450 struct qca_data *qca = hu->priv;
1452 bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state);
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
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.
1468 bt_dev_info(hdev, "waiting for dump to complete");
1469 qca_wait_for_dump_collection(hdev);
1473 static void qca_cmd_timeout(struct hci_dev *hdev)
1475 struct hci_uart *hu = hci_get_drvdata(hdev);
1476 struct qca_data *qca = hu->priv;
1478 if (qca->memdump_state == QCA_MEMDUMP_IDLE)
1479 qca_send_crashbuffer(hu);
1481 bt_dev_info(hdev, "Dump collection is in process");
1484 static int qca_wcn3990_init(struct hci_uart *hu)
1486 struct qca_serdev *qcadev;
1489 /* Check for vregs status, may be hci down has turned
1490 * off the voltage regulator.
1492 qcadev = serdev_device_get_drvdata(hu->serdev);
1493 if (!qcadev->bt_power->vregs_on) {
1494 serdev_device_close(hu->serdev);
1495 ret = qca_regulator_enable(qcadev);
1499 ret = serdev_device_open(hu->serdev);
1501 bt_dev_err(hu->hdev, "failed to open port");
1506 /* Forcefully enable wcn3990 to enter in to boot mode. */
1507 host_set_baudrate(hu, 2400);
1508 ret = qca_send_power_pulse(hu, false);
1512 qca_set_speed(hu, QCA_INIT_SPEED);
1513 ret = qca_send_power_pulse(hu, true);
1517 /* Now the device is in ready state to communicate with host.
1518 * To sync host with device we need to reopen port.
1519 * Without this, we will have RTS and CTS synchronization
1522 serdev_device_close(hu->serdev);
1523 ret = serdev_device_open(hu->serdev);
1525 bt_dev_err(hu->hdev, "failed to open port");
1529 hci_uart_set_flow_control(hu, false);
1534 static int qca_setup(struct hci_uart *hu)
1536 struct hci_dev *hdev = hu->hdev;
1537 struct qca_data *qca = hu->priv;
1538 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1539 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1540 const char *firmware_name = qca_get_firmware_name(hu);
1544 ret = qca_check_speeds(hu);
1548 /* Patch downloading has to be done without IBS mode */
1549 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1551 /* Enable controller to do both LE scan and BR/EDR inquiry
1554 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1556 if (qca_is_wcn399x(soc_type)) {
1557 bt_dev_info(hdev, "setting up wcn3990");
1559 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1560 * setup for every hci up.
1562 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1563 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1564 hu->hdev->shutdown = qca_power_off;
1565 ret = qca_wcn3990_init(hu);
1569 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
1573 bt_dev_info(hdev, "ROME setup");
1574 qca_set_speed(hu, QCA_INIT_SPEED);
1577 /* Setup user speed if needed */
1578 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1580 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1584 qca_baudrate = qca_get_baudrate_value(speed);
1587 if (!qca_is_wcn399x(soc_type)) {
1588 /* Get QCA version information */
1589 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
1594 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1595 /* Setup patch / NVM configurations */
1596 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1599 set_bit(QCA_IBS_ENABLED, &qca->flags);
1600 qca_debugfs_init(hdev);
1601 hu->hdev->hw_error = qca_hw_error;
1602 hu->hdev->cmd_timeout = qca_cmd_timeout;
1603 } else if (ret == -ENOENT) {
1604 /* No patch/nvm-config found, run with original fw/config */
1606 } else if (ret == -EAGAIN) {
1608 * Userspace firmware loader will return -EAGAIN in case no
1609 * patch/nvm-config is found, so run with original fw/config.
1615 if (qca_is_wcn399x(soc_type))
1616 hu->hdev->set_bdaddr = qca_set_bdaddr;
1618 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1623 static const struct hci_uart_proto qca_proto = {
1627 .init_speed = 115200,
1628 .oper_speed = 3000000,
1634 .enqueue = qca_enqueue,
1635 .dequeue = qca_dequeue,
1638 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1639 .soc_type = QCA_WCN3990,
1640 .vregs = (struct qca_vreg []) {
1643 { "vddrf", 300000 },
1644 { "vddch0", 450000 },
1649 static const struct qca_vreg_data qca_soc_data_wcn3991 = {
1650 .soc_type = QCA_WCN3991,
1651 .vregs = (struct qca_vreg []) {
1654 { "vddrf", 300000 },
1655 { "vddch0", 450000 },
1660 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1661 .soc_type = QCA_WCN3998,
1662 .vregs = (struct qca_vreg []) {
1665 { "vddrf", 300000 },
1666 { "vddch0", 450000 },
1671 static void qca_power_shutdown(struct hci_uart *hu)
1673 struct qca_serdev *qcadev;
1674 struct qca_data *qca = hu->priv;
1675 unsigned long flags;
1677 qcadev = serdev_device_get_drvdata(hu->serdev);
1679 /* From this point we go into power off state. But serial port is
1680 * still open, stop queueing the IBS data and flush all the buffered
1683 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1684 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1686 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1688 host_set_baudrate(hu, 2400);
1689 qca_send_power_pulse(hu, false);
1690 qca_regulator_disable(qcadev);
1691 hu->hdev->hw_error = NULL;
1692 hu->hdev->cmd_timeout = NULL;
1695 static int qca_power_off(struct hci_dev *hdev)
1697 struct hci_uart *hu = hci_get_drvdata(hdev);
1698 struct qca_data *qca = hu->priv;
1700 /* Stop sending shutdown command if soc crashes. */
1701 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1702 qca_send_pre_shutdown_cmd(hdev);
1703 usleep_range(8000, 10000);
1706 qca->memdump_state = QCA_MEMDUMP_IDLE;
1707 qca_power_shutdown(hu);
1711 static int qca_regulator_enable(struct qca_serdev *qcadev)
1713 struct qca_power *power = qcadev->bt_power;
1716 /* Already enabled */
1717 if (power->vregs_on)
1720 BT_DBG("enabling %d regulators)", power->num_vregs);
1722 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1726 power->vregs_on = true;
1731 static void qca_regulator_disable(struct qca_serdev *qcadev)
1733 struct qca_power *power;
1738 power = qcadev->bt_power;
1740 /* Already disabled? */
1741 if (!power->vregs_on)
1744 regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1745 power->vregs_on = false;
1748 static int qca_init_regulators(struct qca_power *qca,
1749 const struct qca_vreg *vregs, size_t num_vregs)
1751 struct regulator_bulk_data *bulk;
1755 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1759 for (i = 0; i < num_vregs; i++)
1760 bulk[i].supply = vregs[i].name;
1762 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1766 for (i = 0; i < num_vregs; i++) {
1767 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
1772 qca->vreg_bulk = bulk;
1773 qca->num_vregs = num_vregs;
1778 static int qca_serdev_probe(struct serdev_device *serdev)
1780 struct qca_serdev *qcadev;
1781 const struct qca_vreg_data *data;
1784 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1788 qcadev->serdev_hu.serdev = serdev;
1789 data = device_get_match_data(&serdev->dev);
1790 serdev_device_set_drvdata(serdev, qcadev);
1791 device_property_read_string(&serdev->dev, "firmware-name",
1792 &qcadev->firmware_name);
1793 if (data && qca_is_wcn399x(data->soc_type)) {
1794 qcadev->btsoc_type = data->soc_type;
1795 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1796 sizeof(struct qca_power),
1798 if (!qcadev->bt_power)
1801 qcadev->bt_power->dev = &serdev->dev;
1802 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1805 BT_ERR("Failed to init regulators:%d", err);
1809 qcadev->bt_power->vregs_on = false;
1811 device_property_read_u32(&serdev->dev, "max-speed",
1812 &qcadev->oper_speed);
1813 if (!qcadev->oper_speed)
1814 BT_DBG("UART will pick default operating speed");
1816 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1818 BT_ERR("wcn3990 serdev registration failed");
1822 qcadev->btsoc_type = QCA_ROME;
1823 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1825 if (IS_ERR(qcadev->bt_en)) {
1826 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1827 return PTR_ERR(qcadev->bt_en);
1830 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1831 if (IS_ERR(qcadev->susclk)) {
1832 dev_err(&serdev->dev, "failed to acquire clk\n");
1833 return PTR_ERR(qcadev->susclk);
1836 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1840 err = clk_prepare_enable(qcadev->susclk);
1844 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1846 clk_disable_unprepare(qcadev->susclk);
1853 static void qca_serdev_remove(struct serdev_device *serdev)
1855 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1857 if (qca_is_wcn399x(qcadev->btsoc_type))
1858 qca_power_shutdown(&qcadev->serdev_hu);
1860 clk_disable_unprepare(qcadev->susclk);
1862 hci_uart_unregister_device(&qcadev->serdev_hu);
1865 static int __maybe_unused qca_suspend(struct device *dev)
1867 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1868 struct hci_uart *hu = hci_get_drvdata(hdev);
1869 struct qca_data *qca = hu->priv;
1870 unsigned long flags;
1874 set_bit(QCA_SUSPENDING, &qca->flags);
1876 /* Device is downloading patch or doesn't support in-band sleep. */
1877 if (!test_bit(QCA_IBS_ENABLED, &qca->flags))
1880 cancel_work_sync(&qca->ws_awake_device);
1881 cancel_work_sync(&qca->ws_awake_rx);
1883 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
1884 flags, SINGLE_DEPTH_NESTING);
1886 switch (qca->tx_ibs_state) {
1887 case HCI_IBS_TX_WAKING:
1888 del_timer(&qca->wake_retrans_timer);
1890 case HCI_IBS_TX_AWAKE:
1891 del_timer(&qca->tx_idle_timer);
1893 serdev_device_write_flush(hu->serdev);
1894 cmd = HCI_IBS_SLEEP_IND;
1895 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1898 BT_ERR("Failed to send SLEEP to device");
1902 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
1903 qca->ibs_sent_slps++;
1905 qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off);
1908 case HCI_IBS_TX_ASLEEP:
1912 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
1917 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1922 serdev_device_wait_until_sent(hu->serdev,
1923 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1925 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
1926 * to sleep, so that the packet does not wake the system later.
1929 ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
1930 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
1931 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
1940 clear_bit(QCA_SUSPENDING, &qca->flags);
1945 static int __maybe_unused qca_resume(struct device *dev)
1947 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1948 struct hci_uart *hu = hci_get_drvdata(hdev);
1949 struct qca_data *qca = hu->priv;
1951 clear_bit(QCA_SUSPENDING, &qca->flags);
1956 static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
1958 static const struct of_device_id qca_bluetooth_of_match[] = {
1959 { .compatible = "qcom,qca6174-bt" },
1960 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1961 { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
1962 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1965 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1967 static struct serdev_device_driver qca_serdev_driver = {
1968 .probe = qca_serdev_probe,
1969 .remove = qca_serdev_remove,
1971 .name = "hci_uart_qca",
1972 .of_match_table = qca_bluetooth_of_match,
1977 int __init qca_init(void)
1979 serdev_device_driver_register(&qca_serdev_driver);
1981 return hci_uart_register_proto(&qca_proto);
1984 int __exit qca_deinit(void)
1986 serdev_device_driver_unregister(&qca_serdev_driver);
1988 return hci_uart_unregister_proto(&qca_proto);