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>
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>
36 #include <net/bluetooth/bluetooth.h>
37 #include <net/bluetooth/hci_core.h>
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
48 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
49 #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 200
50 #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000
51 #define CMD_TRANS_TIMEOUT_MS 100
52 #define MEMDUMP_TIMEOUT_MS 8000
53 #define IBS_DISABLE_SSR_TIMEOUT_MS \
54 (MEMDUMP_TIMEOUT_MS + FW_DOWNLOAD_TIMEOUT_MS)
55 #define FW_DOWNLOAD_TIMEOUT_MS 3000
58 #define SUSCLK_RATE_32KHZ 32768
60 /* Controller debug log header */
61 #define QCA_DEBUG_HANDLE 0x2EDC
63 /* max retry count when init fails */
64 #define MAX_INIT_RETRIES 3
66 /* Controller dump header */
67 #define QCA_SSR_DUMP_HANDLE 0x0108
68 #define QCA_DUMP_PACKET_SIZE 255
69 #define QCA_LAST_SEQUENCE_NUM 0xFFFF
70 #define QCA_CRASHBYTE_PACKET_LEN 1096
71 #define QCA_MEMDUMP_BYTE 0xFB
75 QCA_DROP_VENDOR_EVENT,
77 QCA_MEMDUMP_COLLECTION,
85 enum qca_capabilities {
86 QCA_CAP_WIDEBAND_SPEECH = BIT(0),
87 QCA_CAP_VALID_LE_STATES = BIT(1),
90 /* HCI_IBS transmit side sleep protocol states */
97 /* HCI_IBS receive side sleep protocol states */
103 /* HCI_IBS transmit and receive side clock state vote */
104 enum hci_ibs_clock_state_vote {
105 HCI_IBS_VOTE_STATS_UPDATE,
106 HCI_IBS_TX_VOTE_CLOCK_ON,
107 HCI_IBS_TX_VOTE_CLOCK_OFF,
108 HCI_IBS_RX_VOTE_CLOCK_ON,
109 HCI_IBS_RX_VOTE_CLOCK_OFF,
112 /* Controller memory dump states */
113 enum qca_memdump_states {
115 QCA_MEMDUMP_COLLECTING,
116 QCA_MEMDUMP_COLLECTED,
120 struct qca_memdump_info {
126 struct qca_memdump_event_hdr {
135 struct qca_dump_size {
141 struct sk_buff *rx_skb;
142 struct sk_buff_head txq;
143 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
144 struct sk_buff_head rx_memdump_q; /* Memdump wait queue */
145 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
146 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
147 u8 rx_ibs_state; /* HCI_IBS receive side power state */
148 bool tx_vote; /* Clock must be on for TX */
149 bool rx_vote; /* Clock must be on for RX */
150 struct timer_list tx_idle_timer;
152 struct timer_list wake_retrans_timer;
154 struct workqueue_struct *workqueue;
155 struct work_struct ws_awake_rx;
156 struct work_struct ws_awake_device;
157 struct work_struct ws_rx_vote_off;
158 struct work_struct ws_tx_vote_off;
159 struct work_struct ctrl_memdump_evt;
160 struct delayed_work ctrl_memdump_timeout;
161 struct qca_memdump_info *qca_memdump;
163 struct completion drop_ev_comp;
164 wait_queue_head_t suspend_wait_q;
165 enum qca_memdump_states memdump_state;
166 struct mutex hci_memdump_lock;
170 /* For debugging purpose */
188 enum qca_speed_type {
194 * Voltage regulator information required for configuring the
195 * QCA Bluetooth chipset
199 unsigned int load_uA;
202 struct qca_device_data {
203 enum qca_btsoc_type soc_type;
204 struct qca_vreg *vregs;
206 uint32_t capabilities;
210 * Platform data for the QCA Bluetooth power driver.
214 struct regulator_bulk_data *vreg_bulk;
220 struct hci_uart serdev_hu;
221 struct gpio_desc *bt_en;
222 struct gpio_desc *sw_ctrl;
224 enum qca_btsoc_type btsoc_type;
225 struct qca_power *bt_power;
228 const char *firmware_name;
231 static int qca_regulator_enable(struct qca_serdev *qcadev);
232 static void qca_regulator_disable(struct qca_serdev *qcadev);
233 static void qca_power_shutdown(struct hci_uart *hu);
234 static int qca_power_off(struct hci_dev *hdev);
235 static void qca_controller_memdump(struct work_struct *work);
236 static void qca_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb);
238 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
240 enum qca_btsoc_type soc_type;
243 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
245 soc_type = qsd->btsoc_type;
253 static const char *qca_get_firmware_name(struct hci_uart *hu)
256 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
258 return qsd->firmware_name;
264 static void __serial_clock_on(struct tty_struct *tty)
266 /* TODO: Some chipset requires to enable UART clock on client
267 * side to save power consumption or manual work is required.
268 * Please put your code to control UART clock here if needed
272 static void __serial_clock_off(struct tty_struct *tty)
274 /* TODO: Some chipset requires to disable UART clock on client
275 * side to save power consumption or manual work is required.
276 * Please put your code to control UART clock off here if needed
280 /* serial_clock_vote needs to be called with the ibs lock held */
281 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
283 struct qca_data *qca = hu->priv;
286 bool old_vote = (qca->tx_vote | qca->rx_vote);
290 case HCI_IBS_VOTE_STATS_UPDATE:
291 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
294 qca->vote_off_ms += diff;
296 qca->vote_on_ms += diff;
299 case HCI_IBS_TX_VOTE_CLOCK_ON:
304 case HCI_IBS_RX_VOTE_CLOCK_ON:
309 case HCI_IBS_TX_VOTE_CLOCK_OFF:
310 qca->tx_vote = false;
314 case HCI_IBS_RX_VOTE_CLOCK_OFF:
315 qca->rx_vote = false;
320 BT_ERR("Voting irregularity");
324 new_vote = qca->rx_vote | qca->tx_vote;
326 if (new_vote != old_vote) {
328 __serial_clock_on(hu->tty);
330 __serial_clock_off(hu->tty);
332 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
333 vote ? "true" : "false");
335 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
339 qca->vote_off_ms += diff;
342 qca->vote_on_ms += diff;
344 qca->vote_last_jif = jiffies;
348 /* Builds and sends an HCI_IBS command packet.
349 * These are very simple packets with only 1 cmd byte.
351 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
354 struct sk_buff *skb = NULL;
355 struct qca_data *qca = hu->priv;
357 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
359 skb = bt_skb_alloc(1, GFP_ATOMIC);
361 BT_ERR("Failed to allocate memory for HCI_IBS packet");
365 /* Assign HCI_IBS type */
366 skb_put_u8(skb, cmd);
368 skb_queue_tail(&qca->txq, skb);
373 static void qca_wq_awake_device(struct work_struct *work)
375 struct qca_data *qca = container_of(work, struct qca_data,
377 struct hci_uart *hu = qca->hu;
378 unsigned long retrans_delay;
381 BT_DBG("hu %p wq awake device", hu);
383 /* Vote for serial clock */
384 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
386 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
388 /* Send wake indication to device */
389 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
390 BT_ERR("Failed to send WAKE to device");
392 qca->ibs_sent_wakes++;
394 /* Start retransmit timer */
395 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
396 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
398 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
400 /* Actually send the packets */
401 hci_uart_tx_wakeup(hu);
404 static void qca_wq_awake_rx(struct work_struct *work)
406 struct qca_data *qca = container_of(work, struct qca_data,
408 struct hci_uart *hu = qca->hu;
411 BT_DBG("hu %p wq awake rx", hu);
413 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
415 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
416 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
418 /* Always acknowledge device wake up,
419 * sending IBS message doesn't count as TX ON.
421 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
422 BT_ERR("Failed to acknowledge device wake up");
424 qca->ibs_sent_wacks++;
426 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
428 /* Actually send the packets */
429 hci_uart_tx_wakeup(hu);
432 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
434 struct qca_data *qca = container_of(work, struct qca_data,
436 struct hci_uart *hu = qca->hu;
438 BT_DBG("hu %p rx clock vote off", hu);
440 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
443 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
445 struct qca_data *qca = container_of(work, struct qca_data,
447 struct hci_uart *hu = qca->hu;
449 BT_DBG("hu %p tx clock vote off", hu);
451 /* Run HCI tx handling unlocked */
452 hci_uart_tx_wakeup(hu);
454 /* Now that message queued to tty driver, vote for tty clocks off.
455 * It is up to the tty driver to pend the clocks off until tx done.
457 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
460 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
462 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
463 struct hci_uart *hu = qca->hu;
466 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
468 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
469 flags, SINGLE_DEPTH_NESTING);
471 switch (qca->tx_ibs_state) {
472 case HCI_IBS_TX_AWAKE:
473 /* TX_IDLE, go to SLEEP */
474 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
475 BT_ERR("Failed to send SLEEP to device");
478 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
479 qca->ibs_sent_slps++;
480 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
483 case HCI_IBS_TX_ASLEEP:
484 case HCI_IBS_TX_WAKING:
486 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
490 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
493 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
495 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
496 struct hci_uart *hu = qca->hu;
497 unsigned long flags, retrans_delay;
498 bool retransmit = false;
500 BT_DBG("hu %p wake retransmit timeout in %d state",
501 hu, qca->tx_ibs_state);
503 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
504 flags, SINGLE_DEPTH_NESTING);
506 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
507 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
508 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
512 switch (qca->tx_ibs_state) {
513 case HCI_IBS_TX_WAKING:
514 /* No WAKE_ACK, retransmit WAKE */
516 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
517 BT_ERR("Failed to acknowledge device wake up");
520 qca->ibs_sent_wakes++;
521 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
522 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
525 case HCI_IBS_TX_ASLEEP:
526 case HCI_IBS_TX_AWAKE:
528 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
532 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
535 hci_uart_tx_wakeup(hu);
539 static void qca_controller_memdump_timeout(struct work_struct *work)
541 struct qca_data *qca = container_of(work, struct qca_data,
542 ctrl_memdump_timeout.work);
543 struct hci_uart *hu = qca->hu;
545 mutex_lock(&qca->hci_memdump_lock);
546 if (test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) {
547 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
548 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
549 /* Inject hw error event to reset the device
552 hci_reset_dev(hu->hdev);
556 mutex_unlock(&qca->hci_memdump_lock);
560 /* Initialize protocol */
561 static int qca_open(struct hci_uart *hu)
563 struct qca_serdev *qcadev;
564 struct qca_data *qca;
566 BT_DBG("hu %p qca_open", hu);
568 if (!hci_uart_has_flow_control(hu))
571 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
575 skb_queue_head_init(&qca->txq);
576 skb_queue_head_init(&qca->tx_wait_q);
577 skb_queue_head_init(&qca->rx_memdump_q);
578 spin_lock_init(&qca->hci_ibs_lock);
579 mutex_init(&qca->hci_memdump_lock);
580 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
581 if (!qca->workqueue) {
582 BT_ERR("QCA Workqueue not initialized properly");
587 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
588 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
589 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
590 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
591 INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump);
592 INIT_DELAYED_WORK(&qca->ctrl_memdump_timeout,
593 qca_controller_memdump_timeout);
594 init_waitqueue_head(&qca->suspend_wait_q);
597 init_completion(&qca->drop_ev_comp);
599 /* Assume we start with both sides asleep -- extra wakes OK */
600 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
601 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
603 qca->vote_last_jif = jiffies;
608 qcadev = serdev_device_get_drvdata(hu->serdev);
610 switch (qcadev->btsoc_type) {
616 hu->init_speed = qcadev->init_speed;
623 if (qcadev->oper_speed)
624 hu->oper_speed = qcadev->oper_speed;
627 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
628 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
630 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
631 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
633 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
634 qca->tx_idle_delay, qca->wake_retrans);
639 static void qca_debugfs_init(struct hci_dev *hdev)
641 struct hci_uart *hu = hci_get_drvdata(hdev);
642 struct qca_data *qca = hu->priv;
643 struct dentry *ibs_dir;
649 if (test_and_set_bit(QCA_DEBUGFS_CREATED, &qca->flags))
652 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
656 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
657 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
658 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
659 &qca->ibs_sent_slps);
660 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
661 &qca->ibs_sent_wakes);
662 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
663 &qca->ibs_sent_wacks);
664 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
665 &qca->ibs_recv_slps);
666 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
667 &qca->ibs_recv_wakes);
668 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
669 &qca->ibs_recv_wacks);
670 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
671 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
672 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
673 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
674 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
675 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
676 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
677 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
678 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
679 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
683 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
684 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
685 &qca->tx_idle_delay);
688 /* Flush protocol data */
689 static int qca_flush(struct hci_uart *hu)
691 struct qca_data *qca = hu->priv;
693 BT_DBG("hu %p qca flush", hu);
695 skb_queue_purge(&qca->tx_wait_q);
696 skb_queue_purge(&qca->txq);
702 static int qca_close(struct hci_uart *hu)
704 struct qca_data *qca = hu->priv;
706 BT_DBG("hu %p qca close", hu);
708 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
710 skb_queue_purge(&qca->tx_wait_q);
711 skb_queue_purge(&qca->txq);
712 skb_queue_purge(&qca->rx_memdump_q);
714 * Shut the timers down so they can't be rearmed when
715 * destroy_workqueue() drains pending work which in turn might try
716 * to arm a timer. After shutdown rearm attempts are silently
717 * ignored by the timer core code.
719 timer_shutdown_sync(&qca->tx_idle_timer);
720 timer_shutdown_sync(&qca->wake_retrans_timer);
721 destroy_workqueue(qca->workqueue);
724 kfree_skb(qca->rx_skb);
733 /* Called upon a wake-up-indication from the device.
735 static void device_want_to_wakeup(struct hci_uart *hu)
738 struct qca_data *qca = hu->priv;
740 BT_DBG("hu %p want to wake up", hu);
742 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
744 qca->ibs_recv_wakes++;
746 /* Don't wake the rx up when suspending. */
747 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
748 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
752 switch (qca->rx_ibs_state) {
753 case HCI_IBS_RX_ASLEEP:
754 /* Make sure clock is on - we may have turned clock off since
755 * receiving the wake up indicator awake rx clock.
757 queue_work(qca->workqueue, &qca->ws_awake_rx);
758 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
761 case HCI_IBS_RX_AWAKE:
762 /* Always acknowledge device wake up,
763 * sending IBS message doesn't count as TX ON.
765 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
766 BT_ERR("Failed to acknowledge device wake up");
769 qca->ibs_sent_wacks++;
773 /* Any other state is illegal */
774 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
779 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
781 /* Actually send the packets */
782 hci_uart_tx_wakeup(hu);
785 /* Called upon a sleep-indication from the device.
787 static void device_want_to_sleep(struct hci_uart *hu)
790 struct qca_data *qca = hu->priv;
792 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
794 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
796 qca->ibs_recv_slps++;
798 switch (qca->rx_ibs_state) {
799 case HCI_IBS_RX_AWAKE:
801 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
802 /* Vote off rx clock under workqueue */
803 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
806 case HCI_IBS_RX_ASLEEP:
810 /* Any other state is illegal */
811 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
816 wake_up_interruptible(&qca->suspend_wait_q);
818 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
821 /* Called upon wake-up-acknowledgement from the device
823 static void device_woke_up(struct hci_uart *hu)
825 unsigned long flags, idle_delay;
826 struct qca_data *qca = hu->priv;
827 struct sk_buff *skb = NULL;
829 BT_DBG("hu %p woke up", hu);
831 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
833 qca->ibs_recv_wacks++;
835 /* Don't react to the wake-up-acknowledgment when suspending. */
836 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
837 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
841 switch (qca->tx_ibs_state) {
842 case HCI_IBS_TX_AWAKE:
843 /* Expect one if we send 2 WAKEs */
844 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
848 case HCI_IBS_TX_WAKING:
849 /* Send pending packets */
850 while ((skb = skb_dequeue(&qca->tx_wait_q)))
851 skb_queue_tail(&qca->txq, skb);
853 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
854 del_timer(&qca->wake_retrans_timer);
855 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
856 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
857 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
860 case HCI_IBS_TX_ASLEEP:
862 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
867 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
869 /* Actually send the packets */
870 hci_uart_tx_wakeup(hu);
873 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
874 * two simultaneous tasklets.
876 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
878 unsigned long flags = 0, idle_delay;
879 struct qca_data *qca = hu->priv;
881 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
884 if (test_bit(QCA_SSR_TRIGGERED, &qca->flags)) {
885 /* As SSR is in progress, ignore the packets */
886 bt_dev_dbg(hu->hdev, "SSR is in progress");
891 /* Prepend skb with frame type */
892 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
894 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
896 /* Don't go to sleep in middle of patch download or
897 * Out-Of-Band(GPIOs control) sleep is selected.
898 * Don't wake the device up when suspending.
900 if (test_bit(QCA_IBS_DISABLED, &qca->flags) ||
901 test_bit(QCA_SUSPENDING, &qca->flags)) {
902 skb_queue_tail(&qca->txq, skb);
903 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
907 /* Act according to current state */
908 switch (qca->tx_ibs_state) {
909 case HCI_IBS_TX_AWAKE:
910 BT_DBG("Device awake, sending normally");
911 skb_queue_tail(&qca->txq, skb);
912 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
913 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
916 case HCI_IBS_TX_ASLEEP:
917 BT_DBG("Device asleep, waking up and queueing packet");
918 /* Save packet for later */
919 skb_queue_tail(&qca->tx_wait_q, skb);
921 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
922 /* Schedule a work queue to wake up device */
923 queue_work(qca->workqueue, &qca->ws_awake_device);
926 case HCI_IBS_TX_WAKING:
927 BT_DBG("Device waking up, queueing packet");
928 /* Transient state; just keep packet for later */
929 skb_queue_tail(&qca->tx_wait_q, skb);
933 BT_ERR("Illegal tx state: %d (losing packet)",
935 dev_kfree_skb_irq(skb);
939 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
944 static int qca_ibs_sleep_ind(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_SLEEP_IND);
950 device_want_to_sleep(hu);
956 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
958 struct hci_uart *hu = hci_get_drvdata(hdev);
960 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
962 device_want_to_wakeup(hu);
968 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
970 struct hci_uart *hu = hci_get_drvdata(hdev);
972 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
980 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
982 /* We receive debug logs from chip as an ACL packets.
983 * Instead of sending the data to ACL to decode the
984 * received data, we are pushing them to the above layers
985 * as a diagnostic packet.
987 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
988 return hci_recv_diag(hdev, skb);
990 return hci_recv_frame(hdev, skb);
993 static void qca_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
995 struct hci_uart *hu = hci_get_drvdata(hdev);
996 struct qca_data *qca = hu->priv;
999 snprintf(buf, sizeof(buf), "Controller Name: 0x%x\n",
1000 qca->controller_id);
1001 skb_put_data(skb, buf, strlen(buf));
1003 snprintf(buf, sizeof(buf), "Firmware Version: 0x%x\n",
1005 skb_put_data(skb, buf, strlen(buf));
1007 snprintf(buf, sizeof(buf), "Vendor:Qualcomm\n");
1008 skb_put_data(skb, buf, strlen(buf));
1010 snprintf(buf, sizeof(buf), "Driver: %s\n",
1011 hu->serdev->dev.driver->name);
1012 skb_put_data(skb, buf, strlen(buf));
1015 static void qca_controller_memdump(struct work_struct *work)
1017 struct qca_data *qca = container_of(work, struct qca_data,
1019 struct hci_uart *hu = qca->hu;
1020 struct sk_buff *skb;
1021 struct qca_memdump_event_hdr *cmd_hdr;
1022 struct qca_memdump_info *qca_memdump = qca->qca_memdump;
1023 struct qca_dump_size *dump;
1027 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1029 while ((skb = skb_dequeue(&qca->rx_memdump_q))) {
1031 mutex_lock(&qca->hci_memdump_lock);
1032 /* Skip processing the received packets if timeout detected
1033 * or memdump collection completed.
1035 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT ||
1036 qca->memdump_state == QCA_MEMDUMP_COLLECTED) {
1037 mutex_unlock(&qca->hci_memdump_lock);
1042 qca_memdump = kzalloc(sizeof(struct qca_memdump_info),
1045 mutex_unlock(&qca->hci_memdump_lock);
1049 qca->qca_memdump = qca_memdump;
1052 qca->memdump_state = QCA_MEMDUMP_COLLECTING;
1053 cmd_hdr = (void *) skb->data;
1054 seq_no = __le16_to_cpu(cmd_hdr->seq_no);
1055 skb_pull(skb, sizeof(struct qca_memdump_event_hdr));
1059 /* This is the first frame of memdump packet from
1060 * the controller, Disable IBS to recevie dump
1061 * with out any interruption, ideally time required for
1062 * the controller to send the dump is 8 seconds. let us
1063 * start timer to handle this asynchronous activity.
1065 set_bit(QCA_IBS_DISABLED, &qca->flags);
1066 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1067 dump = (void *) skb->data;
1068 qca_memdump->ram_dump_size = __le32_to_cpu(dump->dump_size);
1069 if (!(qca_memdump->ram_dump_size)) {
1070 bt_dev_err(hu->hdev, "Rx invalid memdump size");
1073 mutex_unlock(&qca->hci_memdump_lock);
1077 queue_delayed_work(qca->workqueue,
1078 &qca->ctrl_memdump_timeout,
1079 msecs_to_jiffies(MEMDUMP_TIMEOUT_MS));
1080 skb_pull(skb, sizeof(qca_memdump->ram_dump_size));
1081 qca_memdump->current_seq_no = 0;
1082 qca_memdump->received_dump = 0;
1083 ret = hci_devcd_init(hu->hdev, qca_memdump->ram_dump_size);
1084 bt_dev_info(hu->hdev, "hci_devcd_init Return:%d",
1087 kfree(qca->qca_memdump);
1088 qca->qca_memdump = NULL;
1089 qca->memdump_state = QCA_MEMDUMP_COLLECTED;
1090 cancel_delayed_work(&qca->ctrl_memdump_timeout);
1091 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1092 mutex_unlock(&qca->hci_memdump_lock);
1096 bt_dev_info(hu->hdev, "QCA collecting dump of size:%u",
1097 qca_memdump->ram_dump_size);
1101 /* If sequence no 0 is missed then there is no point in
1102 * accepting the other sequences.
1104 if (!test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) {
1105 bt_dev_err(hu->hdev, "QCA: Discarding other packets");
1108 mutex_unlock(&qca->hci_memdump_lock);
1111 /* There could be chance of missing some packets from
1112 * the controller. In such cases let us store the dummy
1113 * packets in the buffer.
1115 /* For QCA6390, controller does not lost packets but
1116 * sequence number field of packet sometimes has error
1117 * bits, so skip this checking for missing packet.
1119 while ((seq_no > qca_memdump->current_seq_no + 1) &&
1120 (soc_type != QCA_QCA6390) &&
1121 seq_no != QCA_LAST_SEQUENCE_NUM) {
1122 bt_dev_err(hu->hdev, "QCA controller missed packet:%d",
1123 qca_memdump->current_seq_no);
1124 rx_size = qca_memdump->received_dump;
1125 rx_size += QCA_DUMP_PACKET_SIZE;
1126 if (rx_size > qca_memdump->ram_dump_size) {
1127 bt_dev_err(hu->hdev,
1128 "QCA memdump received %d, no space for missed packet",
1129 qca_memdump->received_dump);
1132 hci_devcd_append_pattern(hu->hdev, 0x00,
1133 QCA_DUMP_PACKET_SIZE);
1134 qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE;
1135 qca_memdump->current_seq_no++;
1138 rx_size = qca_memdump->received_dump + skb->len;
1139 if (rx_size <= qca_memdump->ram_dump_size) {
1140 if ((seq_no != QCA_LAST_SEQUENCE_NUM) &&
1141 (seq_no != qca_memdump->current_seq_no)) {
1142 bt_dev_err(hu->hdev,
1143 "QCA memdump unexpected packet %d",
1146 bt_dev_dbg(hu->hdev,
1147 "QCA memdump packet %d with length %d",
1149 hci_devcd_append(hu->hdev, skb);
1150 qca_memdump->current_seq_no += 1;
1151 qca_memdump->received_dump = rx_size;
1153 bt_dev_err(hu->hdev,
1154 "QCA memdump received no space for packet %d",
1155 qca_memdump->current_seq_no);
1158 if (seq_no == QCA_LAST_SEQUENCE_NUM) {
1159 bt_dev_info(hu->hdev,
1160 "QCA memdump Done, received %d, total %d",
1161 qca_memdump->received_dump,
1162 qca_memdump->ram_dump_size);
1163 hci_devcd_complete(hu->hdev);
1164 cancel_delayed_work(&qca->ctrl_memdump_timeout);
1165 kfree(qca->qca_memdump);
1166 qca->qca_memdump = NULL;
1167 qca->memdump_state = QCA_MEMDUMP_COLLECTED;
1168 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1171 mutex_unlock(&qca->hci_memdump_lock);
1176 static int qca_controller_memdump_event(struct hci_dev *hdev,
1177 struct sk_buff *skb)
1179 struct hci_uart *hu = hci_get_drvdata(hdev);
1180 struct qca_data *qca = hu->priv;
1182 set_bit(QCA_SSR_TRIGGERED, &qca->flags);
1183 skb_queue_tail(&qca->rx_memdump_q, skb);
1184 queue_work(qca->workqueue, &qca->ctrl_memdump_evt);
1189 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1191 struct hci_uart *hu = hci_get_drvdata(hdev);
1192 struct qca_data *qca = hu->priv;
1194 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
1195 struct hci_event_hdr *hdr = (void *)skb->data;
1197 /* For the WCN3990 the vendor command for a baudrate change
1198 * isn't sent as synchronous HCI command, because the
1199 * controller sends the corresponding vendor event with the
1200 * new baudrate. The event is received and properly decoded
1201 * after changing the baudrate of the host port. It needs to
1202 * be dropped, otherwise it can be misinterpreted as
1203 * response to a later firmware download command (also a
1207 if (hdr->evt == HCI_EV_VENDOR)
1208 complete(&qca->drop_ev_comp);
1214 /* We receive chip memory dump as an event packet, With a dedicated
1215 * handler followed by a hardware error event. When this event is
1216 * received we store dump into a file before closing hci. This
1217 * dump will help in triaging the issues.
1219 if ((skb->data[0] == HCI_VENDOR_PKT) &&
1220 (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE))
1221 return qca_controller_memdump_event(hdev, skb);
1223 return hci_recv_frame(hdev, skb);
1226 #define QCA_IBS_SLEEP_IND_EVENT \
1227 .type = HCI_IBS_SLEEP_IND, \
1231 .maxlen = HCI_MAX_IBS_SIZE
1233 #define QCA_IBS_WAKE_IND_EVENT \
1234 .type = HCI_IBS_WAKE_IND, \
1238 .maxlen = HCI_MAX_IBS_SIZE
1240 #define QCA_IBS_WAKE_ACK_EVENT \
1241 .type = HCI_IBS_WAKE_ACK, \
1245 .maxlen = HCI_MAX_IBS_SIZE
1247 static const struct h4_recv_pkt qca_recv_pkts[] = {
1248 { H4_RECV_ACL, .recv = qca_recv_acl_data },
1249 { H4_RECV_SCO, .recv = hci_recv_frame },
1250 { H4_RECV_EVENT, .recv = qca_recv_event },
1251 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
1252 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
1253 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
1256 static int qca_recv(struct hci_uart *hu, const void *data, int count)
1258 struct qca_data *qca = hu->priv;
1260 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1263 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
1264 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
1265 if (IS_ERR(qca->rx_skb)) {
1266 int err = PTR_ERR(qca->rx_skb);
1267 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
1275 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
1277 struct qca_data *qca = hu->priv;
1279 return skb_dequeue(&qca->txq);
1282 static uint8_t qca_get_baudrate_value(int speed)
1286 return QCA_BAUDRATE_9600;
1288 return QCA_BAUDRATE_19200;
1290 return QCA_BAUDRATE_38400;
1292 return QCA_BAUDRATE_57600;
1294 return QCA_BAUDRATE_115200;
1296 return QCA_BAUDRATE_230400;
1298 return QCA_BAUDRATE_460800;
1300 return QCA_BAUDRATE_500000;
1302 return QCA_BAUDRATE_921600;
1304 return QCA_BAUDRATE_1000000;
1306 return QCA_BAUDRATE_2000000;
1308 return QCA_BAUDRATE_3000000;
1310 return QCA_BAUDRATE_3200000;
1312 return QCA_BAUDRATE_3500000;
1314 return QCA_BAUDRATE_115200;
1318 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1320 struct hci_uart *hu = hci_get_drvdata(hdev);
1321 struct qca_data *qca = hu->priv;
1322 struct sk_buff *skb;
1323 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1325 if (baudrate > QCA_BAUDRATE_3200000)
1330 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1332 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1336 /* Assign commands to change baudrate and packet type. */
1337 skb_put_data(skb, cmd, sizeof(cmd));
1338 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1340 skb_queue_tail(&qca->txq, skb);
1341 hci_uart_tx_wakeup(hu);
1343 /* Wait for the baudrate change request to be sent */
1345 while (!skb_queue_empty(&qca->txq))
1346 usleep_range(100, 200);
1349 serdev_device_wait_until_sent(hu->serdev,
1350 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1352 /* Give the controller time to process the request */
1353 switch (qca_soc_type(hu)) {
1361 usleep_range(1000, 10000);
1371 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1374 serdev_device_set_baudrate(hu->serdev, speed);
1376 hci_uart_set_baudrate(hu, speed);
1379 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1382 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1383 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1385 /* These power pulses are single byte command which are sent
1386 * at required baudrate to wcn3990. On wcn3990, we have an external
1387 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1388 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1389 * and also we use the same power inputs to turn on and off for
1390 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1391 * we send a power on pulse at 115200 bps. This algorithm will help to
1392 * save power. Disabling hardware flow control is mandatory while
1393 * sending power pulses to SoC.
1395 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1397 serdev_device_write_flush(hu->serdev);
1398 hci_uart_set_flow_control(hu, true);
1399 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1401 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1405 serdev_device_wait_until_sent(hu->serdev, timeout);
1406 hci_uart_set_flow_control(hu, false);
1408 /* Give to controller time to boot/shutdown */
1412 usleep_range(1000, 10000);
1417 static unsigned int qca_get_speed(struct hci_uart *hu,
1418 enum qca_speed_type speed_type)
1420 unsigned int speed = 0;
1422 if (speed_type == QCA_INIT_SPEED) {
1424 speed = hu->init_speed;
1425 else if (hu->proto->init_speed)
1426 speed = hu->proto->init_speed;
1429 speed = hu->oper_speed;
1430 else if (hu->proto->oper_speed)
1431 speed = hu->proto->oper_speed;
1437 static int qca_check_speeds(struct hci_uart *hu)
1439 switch (qca_soc_type(hu)) {
1447 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1448 !qca_get_speed(hu, QCA_OPER_SPEED))
1453 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1454 !qca_get_speed(hu, QCA_OPER_SPEED))
1461 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1463 unsigned int speed, qca_baudrate;
1464 struct qca_data *qca = hu->priv;
1467 if (speed_type == QCA_INIT_SPEED) {
1468 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1470 host_set_baudrate(hu, speed);
1472 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1474 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1478 /* Disable flow control for wcn3990 to deassert RTS while
1479 * changing the baudrate of chip and host.
1489 hci_uart_set_flow_control(hu, true);
1498 reinit_completion(&qca->drop_ev_comp);
1499 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1506 qca_baudrate = qca_get_baudrate_value(speed);
1507 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1508 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1512 host_set_baudrate(hu, speed);
1523 hci_uart_set_flow_control(hu, false);
1532 /* Wait for the controller to send the vendor event
1533 * for the baudrate change command.
1535 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1536 msecs_to_jiffies(100))) {
1537 bt_dev_err(hu->hdev,
1538 "Failed to change controller baudrate\n");
1542 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1553 static int qca_send_crashbuffer(struct hci_uart *hu)
1555 struct qca_data *qca = hu->priv;
1556 struct sk_buff *skb;
1558 skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL);
1560 bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet");
1564 /* We forcefully crash the controller, by sending 0xfb byte for
1565 * 1024 times. We also might have chance of losing data, To be
1566 * on safer side we send 1096 bytes to the SoC.
1568 memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE,
1569 QCA_CRASHBYTE_PACKET_LEN);
1570 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1571 bt_dev_info(hu->hdev, "crash the soc to collect controller dump");
1572 skb_queue_tail(&qca->txq, skb);
1573 hci_uart_tx_wakeup(hu);
1578 static void qca_wait_for_dump_collection(struct hci_dev *hdev)
1580 struct hci_uart *hu = hci_get_drvdata(hdev);
1581 struct qca_data *qca = hu->priv;
1583 wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION,
1584 TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS);
1586 clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1589 static void qca_hw_error(struct hci_dev *hdev, u8 code)
1591 struct hci_uart *hu = hci_get_drvdata(hdev);
1592 struct qca_data *qca = hu->priv;
1594 set_bit(QCA_SSR_TRIGGERED, &qca->flags);
1595 set_bit(QCA_HW_ERROR_EVENT, &qca->flags);
1596 bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state);
1598 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1599 /* If hardware error event received for other than QCA
1600 * soc memory dump event, then we need to crash the SOC
1601 * and wait here for 8 seconds to get the dump packets.
1602 * This will block main thread to be on hold until we
1605 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1606 qca_send_crashbuffer(hu);
1607 qca_wait_for_dump_collection(hdev);
1608 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1609 /* Let us wait here until memory dump collected or
1610 * memory dump timer expired.
1612 bt_dev_info(hdev, "waiting for dump to complete");
1613 qca_wait_for_dump_collection(hdev);
1616 mutex_lock(&qca->hci_memdump_lock);
1617 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1618 bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout");
1619 hci_devcd_abort(hu->hdev);
1620 if (qca->qca_memdump) {
1621 kfree(qca->qca_memdump);
1622 qca->qca_memdump = NULL;
1624 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1625 cancel_delayed_work(&qca->ctrl_memdump_timeout);
1627 mutex_unlock(&qca->hci_memdump_lock);
1629 if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT ||
1630 qca->memdump_state == QCA_MEMDUMP_COLLECTED) {
1631 cancel_work_sync(&qca->ctrl_memdump_evt);
1632 skb_queue_purge(&qca->rx_memdump_q);
1635 clear_bit(QCA_HW_ERROR_EVENT, &qca->flags);
1638 static void qca_cmd_timeout(struct hci_dev *hdev)
1640 struct hci_uart *hu = hci_get_drvdata(hdev);
1641 struct qca_data *qca = hu->priv;
1643 set_bit(QCA_SSR_TRIGGERED, &qca->flags);
1644 if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1645 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1646 qca_send_crashbuffer(hu);
1647 qca_wait_for_dump_collection(hdev);
1648 } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1649 /* Let us wait here until memory dump collected or
1650 * memory dump timer expired.
1652 bt_dev_info(hdev, "waiting for dump to complete");
1653 qca_wait_for_dump_collection(hdev);
1656 mutex_lock(&qca->hci_memdump_lock);
1657 if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1658 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1659 if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
1660 /* Inject hw error event to reset the device
1663 hci_reset_dev(hu->hdev);
1666 mutex_unlock(&qca->hci_memdump_lock);
1669 static bool qca_wakeup(struct hci_dev *hdev)
1671 struct hci_uart *hu = hci_get_drvdata(hdev);
1674 /* BT SoC attached through the serial bus is handled by the serdev driver.
1675 * So we need to use the device handle of the serdev driver to get the
1676 * status of device may wakeup.
1678 wakeup = device_may_wakeup(&hu->serdev->ctrl->dev);
1679 bt_dev_dbg(hu->hdev, "wakeup status : %d", wakeup);
1684 static int qca_regulator_init(struct hci_uart *hu)
1686 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1687 struct qca_serdev *qcadev;
1691 /* Check for vregs status, may be hci down has turned
1692 * off the voltage regulator.
1694 qcadev = serdev_device_get_drvdata(hu->serdev);
1695 if (!qcadev->bt_power->vregs_on) {
1696 serdev_device_close(hu->serdev);
1697 ret = qca_regulator_enable(qcadev);
1701 ret = serdev_device_open(hu->serdev);
1703 bt_dev_err(hu->hdev, "failed to open port");
1713 /* Forcefully enable wcn399x to enter in to boot mode. */
1714 host_set_baudrate(hu, 2400);
1715 ret = qca_send_power_pulse(hu, false);
1724 /* For wcn6750 need to enable gpio bt_en */
1725 if (qcadev->bt_en) {
1726 gpiod_set_value_cansleep(qcadev->bt_en, 0);
1728 gpiod_set_value_cansleep(qcadev->bt_en, 1);
1730 if (qcadev->sw_ctrl) {
1731 sw_ctrl_state = gpiod_get_value_cansleep(qcadev->sw_ctrl);
1732 bt_dev_dbg(hu->hdev, "SW_CTRL is %d", sw_ctrl_state);
1736 qca_set_speed(hu, QCA_INIT_SPEED);
1743 ret = qca_send_power_pulse(hu, true);
1752 /* Now the device is in ready state to communicate with host.
1753 * To sync host with device we need to reopen port.
1754 * Without this, we will have RTS and CTS synchronization
1757 serdev_device_close(hu->serdev);
1758 ret = serdev_device_open(hu->serdev);
1760 bt_dev_err(hu->hdev, "failed to open port");
1764 hci_uart_set_flow_control(hu, false);
1769 static int qca_power_on(struct hci_dev *hdev)
1771 struct hci_uart *hu = hci_get_drvdata(hdev);
1772 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1773 struct qca_serdev *qcadev;
1774 struct qca_data *qca = hu->priv;
1777 /* Non-serdev device usually is powered by external power
1778 * and don't need additional action in driver for power on
1791 ret = qca_regulator_init(hu);
1795 qcadev = serdev_device_get_drvdata(hu->serdev);
1796 if (qcadev->bt_en) {
1797 gpiod_set_value_cansleep(qcadev->bt_en, 1);
1798 /* Controller needs time to bootup. */
1803 clear_bit(QCA_BT_OFF, &qca->flags);
1807 static void hci_coredump_qca(struct hci_dev *hdev)
1809 static const u8 param[] = { 0x26 };
1810 struct sk_buff *skb;
1812 skb = __hci_cmd_sync(hdev, 0xfc0c, 1, param, HCI_CMD_TIMEOUT);
1814 bt_dev_err(hdev, "%s: trigger crash failed (%ld)", __func__, PTR_ERR(skb));
1818 static int qca_setup(struct hci_uart *hu)
1820 struct hci_dev *hdev = hu->hdev;
1821 struct qca_data *qca = hu->priv;
1822 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1823 unsigned int retries = 0;
1824 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1825 const char *firmware_name = qca_get_firmware_name(hu);
1827 struct qca_btsoc_version ver;
1828 const char *soc_name;
1830 ret = qca_check_speeds(hu);
1834 clear_bit(QCA_ROM_FW, &qca->flags);
1835 /* Patch downloading has to be done without IBS mode */
1836 set_bit(QCA_IBS_DISABLED, &qca->flags);
1838 /* Enable controller to do both LE scan and BR/EDR inquiry
1841 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1848 soc_name = "wcn399x";
1852 soc_name = "wcn6750";
1856 soc_name = "wcn6855";
1860 soc_name = "wcn7850";
1864 soc_name = "ROME/QCA6390";
1866 bt_dev_info(hdev, "setting up %s", soc_name);
1868 qca->memdump_state = QCA_MEMDUMP_IDLE;
1871 ret = qca_power_on(hdev);
1875 clear_bit(QCA_SSR_TRIGGERED, &qca->flags);
1885 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1886 hci_set_aosp_capable(hdev);
1888 ret = qca_read_soc_version(hdev, &ver, soc_type);
1894 qca_set_speed(hu, QCA_INIT_SPEED);
1897 /* Setup user speed if needed */
1898 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1900 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1904 qca_baudrate = qca_get_baudrate_value(speed);
1918 /* Get QCA version information */
1919 ret = qca_read_soc_version(hdev, &ver, soc_type);
1924 /* Setup patch / NVM configurations */
1925 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, ver,
1928 clear_bit(QCA_IBS_DISABLED, &qca->flags);
1929 qca_debugfs_init(hdev);
1930 hu->hdev->hw_error = qca_hw_error;
1931 hu->hdev->cmd_timeout = qca_cmd_timeout;
1932 if (device_can_wakeup(hu->serdev->ctrl->dev.parent))
1933 hu->hdev->wakeup = qca_wakeup;
1934 } else if (ret == -ENOENT) {
1935 /* No patch/nvm-config found, run with original fw/config */
1936 set_bit(QCA_ROM_FW, &qca->flags);
1938 } else if (ret == -EAGAIN) {
1940 * Userspace firmware loader will return -EAGAIN in case no
1941 * patch/nvm-config is found, so run with original fw/config.
1943 set_bit(QCA_ROM_FW, &qca->flags);
1948 if (ret && retries < MAX_INIT_RETRIES) {
1949 bt_dev_warn(hdev, "Retry BT power ON:%d", retries);
1950 qca_power_shutdown(hu);
1952 serdev_device_close(hu->serdev);
1953 ret = serdev_device_open(hu->serdev);
1955 bt_dev_err(hdev, "failed to open port");
1964 if (soc_type == QCA_ROME)
1965 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1967 hu->hdev->set_bdaddr = qca_set_bdaddr;
1968 qca->fw_version = le16_to_cpu(ver.patch_ver);
1969 qca->controller_id = le16_to_cpu(ver.rom_ver);
1970 hci_devcd_register(hdev, hci_coredump_qca, qca_dmp_hdr, NULL);
1975 static const struct hci_uart_proto qca_proto = {
1979 .init_speed = 115200,
1980 .oper_speed = 3000000,
1986 .enqueue = qca_enqueue,
1987 .dequeue = qca_dequeue,
1990 static const struct qca_device_data qca_soc_data_wcn3988 __maybe_unused = {
1991 .soc_type = QCA_WCN3988,
1992 .vregs = (struct qca_vreg []) {
1995 { "vddrf", 300000 },
1996 { "vddch0", 450000 },
2001 static const struct qca_device_data qca_soc_data_wcn3990 __maybe_unused = {
2002 .soc_type = QCA_WCN3990,
2003 .vregs = (struct qca_vreg []) {
2006 { "vddrf", 300000 },
2007 { "vddch0", 450000 },
2012 static const struct qca_device_data qca_soc_data_wcn3991 __maybe_unused = {
2013 .soc_type = QCA_WCN3991,
2014 .vregs = (struct qca_vreg []) {
2017 { "vddrf", 300000 },
2018 { "vddch0", 450000 },
2021 .capabilities = QCA_CAP_WIDEBAND_SPEECH | QCA_CAP_VALID_LE_STATES,
2024 static const struct qca_device_data qca_soc_data_wcn3998 __maybe_unused = {
2025 .soc_type = QCA_WCN3998,
2026 .vregs = (struct qca_vreg []) {
2029 { "vddrf", 300000 },
2030 { "vddch0", 450000 },
2035 static const struct qca_device_data qca_soc_data_qca6390 __maybe_unused = {
2036 .soc_type = QCA_QCA6390,
2040 static const struct qca_device_data qca_soc_data_wcn6750 __maybe_unused = {
2041 .soc_type = QCA_WCN6750,
2042 .vregs = (struct qca_vreg []) {
2044 { "vddaon", 26000 },
2045 { "vddbtcxmx", 126000 },
2046 { "vddrfacmn", 12500 },
2047 { "vddrfa0p8", 102000 },
2048 { "vddrfa1p7", 302000 },
2049 { "vddrfa1p2", 257000 },
2050 { "vddrfa2p2", 1700000 },
2054 .capabilities = QCA_CAP_WIDEBAND_SPEECH | QCA_CAP_VALID_LE_STATES,
2057 static const struct qca_device_data qca_soc_data_wcn6855 __maybe_unused = {
2058 .soc_type = QCA_WCN6855,
2059 .vregs = (struct qca_vreg []) {
2061 { "vddbtcxmx", 126000 },
2062 { "vddrfacmn", 12500 },
2063 { "vddrfa0p8", 102000 },
2064 { "vddrfa1p7", 302000 },
2065 { "vddrfa1p2", 257000 },
2068 .capabilities = QCA_CAP_WIDEBAND_SPEECH | QCA_CAP_VALID_LE_STATES,
2071 static const struct qca_device_data qca_soc_data_wcn7850 __maybe_unused = {
2072 .soc_type = QCA_WCN7850,
2073 .vregs = (struct qca_vreg []) {
2075 { "vddaon", 26000 },
2076 { "vdddig", 126000 },
2077 { "vddrfa0p8", 102000 },
2078 { "vddrfa1p2", 257000 },
2079 { "vddrfa1p9", 302000 },
2082 .capabilities = QCA_CAP_WIDEBAND_SPEECH | QCA_CAP_VALID_LE_STATES,
2085 static void qca_power_shutdown(struct hci_uart *hu)
2087 struct qca_serdev *qcadev;
2088 struct qca_data *qca = hu->priv;
2089 unsigned long flags;
2090 enum qca_btsoc_type soc_type = qca_soc_type(hu);
2093 /* From this point we go into power off state. But serial port is
2094 * still open, stop queueing the IBS data and flush all the buffered
2097 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
2098 set_bit(QCA_IBS_DISABLED, &qca->flags);
2100 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
2102 /* Non-serdev device usually is powered by external power
2103 * and don't need additional action in driver for power down
2108 qcadev = serdev_device_get_drvdata(hu->serdev);
2115 host_set_baudrate(hu, 2400);
2116 qca_send_power_pulse(hu, false);
2117 qca_regulator_disable(qcadev);
2122 gpiod_set_value_cansleep(qcadev->bt_en, 0);
2124 qca_regulator_disable(qcadev);
2125 if (qcadev->sw_ctrl) {
2126 sw_ctrl_state = gpiod_get_value_cansleep(qcadev->sw_ctrl);
2127 bt_dev_dbg(hu->hdev, "SW_CTRL is %d", sw_ctrl_state);
2132 gpiod_set_value_cansleep(qcadev->bt_en, 0);
2135 set_bit(QCA_BT_OFF, &qca->flags);
2138 static int qca_power_off(struct hci_dev *hdev)
2140 struct hci_uart *hu = hci_get_drvdata(hdev);
2141 struct qca_data *qca = hu->priv;
2142 enum qca_btsoc_type soc_type = qca_soc_type(hu);
2144 hu->hdev->hw_error = NULL;
2145 hu->hdev->cmd_timeout = NULL;
2147 del_timer_sync(&qca->wake_retrans_timer);
2148 del_timer_sync(&qca->tx_idle_timer);
2150 /* Stop sending shutdown command if soc crashes. */
2151 if (soc_type != QCA_ROME
2152 && qca->memdump_state == QCA_MEMDUMP_IDLE) {
2153 qca_send_pre_shutdown_cmd(hdev);
2154 usleep_range(8000, 10000);
2157 qca_power_shutdown(hu);
2161 static int qca_regulator_enable(struct qca_serdev *qcadev)
2163 struct qca_power *power = qcadev->bt_power;
2166 /* Already enabled */
2167 if (power->vregs_on)
2170 BT_DBG("enabling %d regulators)", power->num_vregs);
2172 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
2176 power->vregs_on = true;
2178 ret = clk_prepare_enable(qcadev->susclk);
2180 qca_regulator_disable(qcadev);
2185 static void qca_regulator_disable(struct qca_serdev *qcadev)
2187 struct qca_power *power;
2192 power = qcadev->bt_power;
2194 /* Already disabled? */
2195 if (!power->vregs_on)
2198 regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
2199 power->vregs_on = false;
2201 clk_disable_unprepare(qcadev->susclk);
2204 static int qca_init_regulators(struct qca_power *qca,
2205 const struct qca_vreg *vregs, size_t num_vregs)
2207 struct regulator_bulk_data *bulk;
2211 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
2215 for (i = 0; i < num_vregs; i++)
2216 bulk[i].supply = vregs[i].name;
2218 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
2222 for (i = 0; i < num_vregs; i++) {
2223 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
2228 qca->vreg_bulk = bulk;
2229 qca->num_vregs = num_vregs;
2234 static int qca_serdev_probe(struct serdev_device *serdev)
2236 struct qca_serdev *qcadev;
2237 struct hci_dev *hdev;
2238 const struct qca_device_data *data;
2240 bool power_ctrl_enabled = true;
2242 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
2246 qcadev->serdev_hu.serdev = serdev;
2247 data = device_get_match_data(&serdev->dev);
2248 serdev_device_set_drvdata(serdev, qcadev);
2249 device_property_read_string(&serdev->dev, "firmware-name",
2250 &qcadev->firmware_name);
2251 device_property_read_u32(&serdev->dev, "max-speed",
2252 &qcadev->oper_speed);
2253 if (!qcadev->oper_speed)
2254 BT_DBG("UART will pick default operating speed");
2257 qcadev->btsoc_type = data->soc_type;
2259 qcadev->btsoc_type = QCA_ROME;
2261 switch (qcadev->btsoc_type) {
2269 qcadev->bt_power = devm_kzalloc(&serdev->dev,
2270 sizeof(struct qca_power),
2272 if (!qcadev->bt_power)
2275 qcadev->bt_power->dev = &serdev->dev;
2276 err = qca_init_regulators(qcadev->bt_power, data->vregs,
2279 BT_ERR("Failed to init regulators:%d", err);
2283 qcadev->bt_power->vregs_on = false;
2285 qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable",
2287 if (IS_ERR_OR_NULL(qcadev->bt_en) &&
2288 (data->soc_type == QCA_WCN6750 ||
2289 data->soc_type == QCA_WCN6855)) {
2290 dev_err(&serdev->dev, "failed to acquire BT_EN gpio\n");
2291 power_ctrl_enabled = false;
2294 qcadev->sw_ctrl = devm_gpiod_get_optional(&serdev->dev, "swctrl",
2296 if (IS_ERR_OR_NULL(qcadev->sw_ctrl) &&
2297 (data->soc_type == QCA_WCN6750 ||
2298 data->soc_type == QCA_WCN6855 ||
2299 data->soc_type == QCA_WCN7850))
2300 dev_warn(&serdev->dev, "failed to acquire SW_CTRL gpio\n");
2302 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
2303 if (IS_ERR(qcadev->susclk)) {
2304 dev_err(&serdev->dev, "failed to acquire clk\n");
2305 return PTR_ERR(qcadev->susclk);
2308 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
2310 BT_ERR("wcn3990 serdev registration failed");
2316 qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable",
2318 if (IS_ERR_OR_NULL(qcadev->bt_en)) {
2319 dev_warn(&serdev->dev, "failed to acquire enable gpio\n");
2320 power_ctrl_enabled = false;
2323 qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
2324 if (IS_ERR(qcadev->susclk)) {
2325 dev_warn(&serdev->dev, "failed to acquire clk\n");
2326 return PTR_ERR(qcadev->susclk);
2328 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
2332 err = clk_prepare_enable(qcadev->susclk);
2336 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
2338 BT_ERR("Rome serdev registration failed");
2339 clk_disable_unprepare(qcadev->susclk);
2344 hdev = qcadev->serdev_hu.hdev;
2346 if (power_ctrl_enabled) {
2347 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
2348 hdev->shutdown = qca_power_off;
2352 /* Wideband speech support must be set per driver since it can't
2353 * be queried via hci. Same with the valid le states quirk.
2355 if (data->capabilities & QCA_CAP_WIDEBAND_SPEECH)
2356 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2359 if (data->capabilities & QCA_CAP_VALID_LE_STATES)
2360 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2366 static void qca_serdev_remove(struct serdev_device *serdev)
2368 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2369 struct qca_power *power = qcadev->bt_power;
2371 switch (qcadev->btsoc_type) {
2379 if (power->vregs_on) {
2380 qca_power_shutdown(&qcadev->serdev_hu);
2387 clk_disable_unprepare(qcadev->susclk);
2390 hci_uart_unregister_device(&qcadev->serdev_hu);
2393 static void qca_serdev_shutdown(struct device *dev)
2396 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
2397 struct serdev_device *serdev = to_serdev_device(dev);
2398 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2399 struct hci_uart *hu = &qcadev->serdev_hu;
2400 struct hci_dev *hdev = hu->hdev;
2401 struct qca_data *qca = hu->priv;
2402 const u8 ibs_wake_cmd[] = { 0xFD };
2403 const u8 edl_reset_soc_cmd[] = { 0x01, 0x00, 0xFC, 0x01, 0x05 };
2405 if (qcadev->btsoc_type == QCA_QCA6390) {
2406 if (test_bit(QCA_BT_OFF, &qca->flags) ||
2407 !test_bit(HCI_RUNNING, &hdev->flags))
2410 serdev_device_write_flush(serdev);
2411 ret = serdev_device_write_buf(serdev, ibs_wake_cmd,
2412 sizeof(ibs_wake_cmd));
2414 BT_ERR("QCA send IBS_WAKE_IND error: %d", ret);
2417 serdev_device_wait_until_sent(serdev, timeout);
2418 usleep_range(8000, 10000);
2420 serdev_device_write_flush(serdev);
2421 ret = serdev_device_write_buf(serdev, edl_reset_soc_cmd,
2422 sizeof(edl_reset_soc_cmd));
2424 BT_ERR("QCA send EDL_RESET_REQ error: %d", ret);
2427 serdev_device_wait_until_sent(serdev, timeout);
2428 usleep_range(8000, 10000);
2432 static int __maybe_unused qca_suspend(struct device *dev)
2434 struct serdev_device *serdev = to_serdev_device(dev);
2435 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2436 struct hci_uart *hu = &qcadev->serdev_hu;
2437 struct qca_data *qca = hu->priv;
2438 unsigned long flags;
2439 bool tx_pending = false;
2442 u32 wait_timeout = 0;
2444 set_bit(QCA_SUSPENDING, &qca->flags);
2446 /* if BT SoC is running with default firmware then it does not
2447 * support in-band sleep
2449 if (test_bit(QCA_ROM_FW, &qca->flags))
2452 /* During SSR after memory dump collection, controller will be
2453 * powered off and then powered on.If controller is powered off
2454 * during SSR then we should wait until SSR is completed.
2456 if (test_bit(QCA_BT_OFF, &qca->flags) &&
2457 !test_bit(QCA_SSR_TRIGGERED, &qca->flags))
2460 if (test_bit(QCA_IBS_DISABLED, &qca->flags) ||
2461 test_bit(QCA_SSR_TRIGGERED, &qca->flags)) {
2462 wait_timeout = test_bit(QCA_SSR_TRIGGERED, &qca->flags) ?
2463 IBS_DISABLE_SSR_TIMEOUT_MS :
2464 FW_DOWNLOAD_TIMEOUT_MS;
2466 /* QCA_IBS_DISABLED flag is set to true, During FW download
2467 * and during memory dump collection. It is reset to false,
2468 * After FW download complete.
2470 wait_on_bit_timeout(&qca->flags, QCA_IBS_DISABLED,
2471 TASK_UNINTERRUPTIBLE, msecs_to_jiffies(wait_timeout));
2473 if (test_bit(QCA_IBS_DISABLED, &qca->flags)) {
2474 bt_dev_err(hu->hdev, "SSR or FW download time out");
2480 cancel_work_sync(&qca->ws_awake_device);
2481 cancel_work_sync(&qca->ws_awake_rx);
2483 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
2484 flags, SINGLE_DEPTH_NESTING);
2486 switch (qca->tx_ibs_state) {
2487 case HCI_IBS_TX_WAKING:
2488 del_timer(&qca->wake_retrans_timer);
2490 case HCI_IBS_TX_AWAKE:
2491 del_timer(&qca->tx_idle_timer);
2493 serdev_device_write_flush(hu->serdev);
2494 cmd = HCI_IBS_SLEEP_IND;
2495 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
2498 BT_ERR("Failed to send SLEEP to device");
2502 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
2503 qca->ibs_sent_slps++;
2507 case HCI_IBS_TX_ASLEEP:
2511 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
2516 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
2522 serdev_device_wait_until_sent(hu->serdev,
2523 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
2524 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
2527 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
2528 * to sleep, so that the packet does not wake the system later.
2530 ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
2531 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
2532 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
2541 clear_bit(QCA_SUSPENDING, &qca->flags);
2546 static int __maybe_unused qca_resume(struct device *dev)
2548 struct serdev_device *serdev = to_serdev_device(dev);
2549 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2550 struct hci_uart *hu = &qcadev->serdev_hu;
2551 struct qca_data *qca = hu->priv;
2553 clear_bit(QCA_SUSPENDING, &qca->flags);
2558 static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
2561 static const struct of_device_id qca_bluetooth_of_match[] = {
2562 { .compatible = "qcom,qca6174-bt" },
2563 { .compatible = "qcom,qca6390-bt", .data = &qca_soc_data_qca6390},
2564 { .compatible = "qcom,qca9377-bt" },
2565 { .compatible = "qcom,wcn3988-bt", .data = &qca_soc_data_wcn3988},
2566 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
2567 { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
2568 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
2569 { .compatible = "qcom,wcn6750-bt", .data = &qca_soc_data_wcn6750},
2570 { .compatible = "qcom,wcn6855-bt", .data = &qca_soc_data_wcn6855},
2571 { .compatible = "qcom,wcn7850-bt", .data = &qca_soc_data_wcn7850},
2574 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
2578 static const struct acpi_device_id qca_bluetooth_acpi_match[] = {
2579 { "QCOM6390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2580 { "DLA16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2581 { "DLB16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2582 { "DLB26390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2585 MODULE_DEVICE_TABLE(acpi, qca_bluetooth_acpi_match);
2588 #ifdef CONFIG_DEV_COREDUMP
2589 static void hciqca_coredump(struct device *dev)
2591 struct serdev_device *serdev = to_serdev_device(dev);
2592 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
2593 struct hci_uart *hu = &qcadev->serdev_hu;
2594 struct hci_dev *hdev = hu->hdev;
2596 if (hdev->dump.coredump)
2597 hdev->dump.coredump(hdev);
2601 static struct serdev_device_driver qca_serdev_driver = {
2602 .probe = qca_serdev_probe,
2603 .remove = qca_serdev_remove,
2605 .name = "hci_uart_qca",
2606 .of_match_table = of_match_ptr(qca_bluetooth_of_match),
2607 .acpi_match_table = ACPI_PTR(qca_bluetooth_acpi_match),
2608 .shutdown = qca_serdev_shutdown,
2610 #ifdef CONFIG_DEV_COREDUMP
2611 .coredump = hciqca_coredump,
2616 int __init qca_init(void)
2618 serdev_device_driver_register(&qca_serdev_driver);
2620 return hci_uart_register_proto(&qca_proto);
2623 int __exit qca_deinit(void)
2625 serdev_device_driver_unregister(&qca_serdev_driver);
2627 return hci_uart_unregister_proto(&qca_proto);