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/device.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/mod_devicetable.h>
26 #include <linux/module.h>
27 #include <linux/of_device.h>
28 #include <linux/platform_device.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/serdev.h>
31 #include <asm/unaligned.h>
33 #include <net/bluetooth/bluetooth.h>
34 #include <net/bluetooth/hci_core.h>
39 /* HCI_IBS protocol messages */
40 #define HCI_IBS_SLEEP_IND 0xFE
41 #define HCI_IBS_WAKE_IND 0xFD
42 #define HCI_IBS_WAKE_ACK 0xFC
43 #define HCI_MAX_IBS_SIZE 10
45 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
46 #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40
47 #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000
48 #define CMD_TRANS_TIMEOUT_MS 100
51 #define SUSCLK_RATE_32KHZ 32768
53 /* Controller debug log header */
54 #define QCA_DEBUG_HANDLE 0x2EDC
58 QCA_DROP_VENDOR_EVENT
,
62 /* HCI_IBS transmit side sleep protocol states */
69 /* HCI_IBS receive side sleep protocol states */
75 /* HCI_IBS transmit and receive side clock state vote */
76 enum hci_ibs_clock_state_vote
{
77 HCI_IBS_VOTE_STATS_UPDATE
,
78 HCI_IBS_TX_VOTE_CLOCK_ON
,
79 HCI_IBS_TX_VOTE_CLOCK_OFF
,
80 HCI_IBS_RX_VOTE_CLOCK_ON
,
81 HCI_IBS_RX_VOTE_CLOCK_OFF
,
86 struct sk_buff
*rx_skb
;
87 struct sk_buff_head txq
;
88 struct sk_buff_head tx_wait_q
; /* HCI_IBS wait queue */
89 spinlock_t hci_ibs_lock
; /* HCI_IBS state lock */
90 u8 tx_ibs_state
; /* HCI_IBS transmit side power state*/
91 u8 rx_ibs_state
; /* HCI_IBS receive side power state */
92 bool tx_vote
; /* Clock must be on for TX */
93 bool rx_vote
; /* Clock must be on for RX */
94 struct timer_list tx_idle_timer
;
96 struct timer_list wake_retrans_timer
;
98 struct workqueue_struct
*workqueue
;
99 struct work_struct ws_awake_rx
;
100 struct work_struct ws_awake_device
;
101 struct work_struct ws_rx_vote_off
;
102 struct work_struct ws_tx_vote_off
;
104 struct completion drop_ev_comp
;
105 wait_queue_head_t suspend_wait_q
;
107 /* For debugging purpose */
125 enum qca_speed_type
{
131 * Voltage regulator information required for configuring the
132 * QCA Bluetooth chipset
136 unsigned int load_uA
;
139 struct qca_vreg_data
{
140 enum qca_btsoc_type soc_type
;
141 struct qca_vreg
*vregs
;
146 * Platform data for the QCA Bluetooth power driver.
150 struct regulator_bulk_data
*vreg_bulk
;
156 struct hci_uart serdev_hu
;
157 struct gpio_desc
*bt_en
;
159 enum qca_btsoc_type btsoc_type
;
160 struct qca_power
*bt_power
;
163 const char *firmware_name
;
166 static int qca_regulator_enable(struct qca_serdev
*qcadev
);
167 static void qca_regulator_disable(struct qca_serdev
*qcadev
);
168 static void qca_power_shutdown(struct hci_uart
*hu
);
169 static int qca_power_off(struct hci_dev
*hdev
);
171 static enum qca_btsoc_type
qca_soc_type(struct hci_uart
*hu
)
173 enum qca_btsoc_type soc_type
;
176 struct qca_serdev
*qsd
= serdev_device_get_drvdata(hu
->serdev
);
178 soc_type
= qsd
->btsoc_type
;
186 static const char *qca_get_firmware_name(struct hci_uart
*hu
)
189 struct qca_serdev
*qsd
= serdev_device_get_drvdata(hu
->serdev
);
191 return qsd
->firmware_name
;
197 static void __serial_clock_on(struct tty_struct
*tty
)
199 /* TODO: Some chipset requires to enable UART clock on client
200 * side to save power consumption or manual work is required.
201 * Please put your code to control UART clock here if needed
205 static void __serial_clock_off(struct tty_struct
*tty
)
207 /* TODO: Some chipset requires to disable UART clock on client
208 * side to save power consumption or manual work is required.
209 * Please put your code to control UART clock off here if needed
213 /* serial_clock_vote needs to be called with the ibs lock held */
214 static void serial_clock_vote(unsigned long vote
, struct hci_uart
*hu
)
216 struct qca_data
*qca
= hu
->priv
;
219 bool old_vote
= (qca
->tx_vote
| qca
->rx_vote
);
223 case HCI_IBS_VOTE_STATS_UPDATE
:
224 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
227 qca
->vote_off_ms
+= diff
;
229 qca
->vote_on_ms
+= diff
;
232 case HCI_IBS_TX_VOTE_CLOCK_ON
:
238 case HCI_IBS_RX_VOTE_CLOCK_ON
:
244 case HCI_IBS_TX_VOTE_CLOCK_OFF
:
245 qca
->tx_vote
= false;
247 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
250 case HCI_IBS_RX_VOTE_CLOCK_OFF
:
251 qca
->rx_vote
= false;
253 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
257 BT_ERR("Voting irregularity");
261 if (new_vote
!= old_vote
) {
263 __serial_clock_on(hu
->tty
);
265 __serial_clock_off(hu
->tty
);
267 BT_DBG("Vote serial clock %s(%s)", new_vote
? "true" : "false",
268 vote
? "true" : "false");
270 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
274 qca
->vote_off_ms
+= diff
;
277 qca
->vote_on_ms
+= diff
;
279 qca
->vote_last_jif
= jiffies
;
283 /* Builds and sends an HCI_IBS command packet.
284 * These are very simple packets with only 1 cmd byte.
286 static int send_hci_ibs_cmd(u8 cmd
, struct hci_uart
*hu
)
289 struct sk_buff
*skb
= NULL
;
290 struct qca_data
*qca
= hu
->priv
;
292 BT_DBG("hu %p send hci ibs cmd 0x%x", hu
, cmd
);
294 skb
= bt_skb_alloc(1, GFP_ATOMIC
);
296 BT_ERR("Failed to allocate memory for HCI_IBS packet");
300 /* Assign HCI_IBS type */
301 skb_put_u8(skb
, cmd
);
303 skb_queue_tail(&qca
->txq
, skb
);
308 static void qca_wq_awake_device(struct work_struct
*work
)
310 struct qca_data
*qca
= container_of(work
, struct qca_data
,
312 struct hci_uart
*hu
= qca
->hu
;
313 unsigned long retrans_delay
;
316 BT_DBG("hu %p wq awake device", hu
);
318 /* Vote for serial clock */
319 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON
, hu
);
321 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
323 /* Send wake indication to device */
324 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0)
325 BT_ERR("Failed to send WAKE to device");
327 qca
->ibs_sent_wakes
++;
329 /* Start retransmit timer */
330 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
331 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
333 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
335 /* Actually send the packets */
336 hci_uart_tx_wakeup(hu
);
339 static void qca_wq_awake_rx(struct work_struct
*work
)
341 struct qca_data
*qca
= container_of(work
, struct qca_data
,
343 struct hci_uart
*hu
= qca
->hu
;
346 BT_DBG("hu %p wq awake rx", hu
);
348 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON
, hu
);
350 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
351 qca
->rx_ibs_state
= HCI_IBS_RX_AWAKE
;
353 /* Always acknowledge device wake up,
354 * sending IBS message doesn't count as TX ON.
356 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0)
357 BT_ERR("Failed to acknowledge device wake up");
359 qca
->ibs_sent_wacks
++;
361 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
363 /* Actually send the packets */
364 hci_uart_tx_wakeup(hu
);
367 static void qca_wq_serial_rx_clock_vote_off(struct work_struct
*work
)
369 struct qca_data
*qca
= container_of(work
, struct qca_data
,
371 struct hci_uart
*hu
= qca
->hu
;
373 BT_DBG("hu %p rx clock vote off", hu
);
375 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF
, hu
);
378 static void qca_wq_serial_tx_clock_vote_off(struct work_struct
*work
)
380 struct qca_data
*qca
= container_of(work
, struct qca_data
,
382 struct hci_uart
*hu
= qca
->hu
;
384 BT_DBG("hu %p tx clock vote off", hu
);
386 /* Run HCI tx handling unlocked */
387 hci_uart_tx_wakeup(hu
);
389 /* Now that message queued to tty driver, vote for tty clocks off.
390 * It is up to the tty driver to pend the clocks off until tx done.
392 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF
, hu
);
395 static void hci_ibs_tx_idle_timeout(struct timer_list
*t
)
397 struct qca_data
*qca
= from_timer(qca
, t
, tx_idle_timer
);
398 struct hci_uart
*hu
= qca
->hu
;
401 BT_DBG("hu %p idle timeout in %d state", hu
, qca
->tx_ibs_state
);
403 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
404 flags
, SINGLE_DEPTH_NESTING
);
406 switch (qca
->tx_ibs_state
) {
407 case HCI_IBS_TX_AWAKE
:
408 /* TX_IDLE, go to SLEEP */
409 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND
, hu
) < 0) {
410 BT_ERR("Failed to send SLEEP to device");
413 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
414 qca
->ibs_sent_slps
++;
415 queue_work(qca
->workqueue
, &qca
->ws_tx_vote_off
);
418 case HCI_IBS_TX_ASLEEP
:
419 case HCI_IBS_TX_WAKING
:
423 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
427 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
430 static void hci_ibs_wake_retrans_timeout(struct timer_list
*t
)
432 struct qca_data
*qca
= from_timer(qca
, t
, wake_retrans_timer
);
433 struct hci_uart
*hu
= qca
->hu
;
434 unsigned long flags
, retrans_delay
;
435 bool retransmit
= false;
437 BT_DBG("hu %p wake retransmit timeout in %d state",
438 hu
, qca
->tx_ibs_state
);
440 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
441 flags
, SINGLE_DEPTH_NESTING
);
443 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
444 if (test_bit(QCA_SUSPENDING
, &qca
->flags
)) {
445 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
449 switch (qca
->tx_ibs_state
) {
450 case HCI_IBS_TX_WAKING
:
451 /* No WAKE_ACK, retransmit WAKE */
453 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0) {
454 BT_ERR("Failed to acknowledge device wake up");
457 qca
->ibs_sent_wakes
++;
458 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
459 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
462 case HCI_IBS_TX_ASLEEP
:
463 case HCI_IBS_TX_AWAKE
:
467 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
471 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
474 hci_uart_tx_wakeup(hu
);
477 /* Initialize protocol */
478 static int qca_open(struct hci_uart
*hu
)
480 struct qca_serdev
*qcadev
;
481 struct qca_data
*qca
;
484 BT_DBG("hu %p qca_open", hu
);
486 if (!hci_uart_has_flow_control(hu
))
489 qca
= kzalloc(sizeof(struct qca_data
), GFP_KERNEL
);
493 skb_queue_head_init(&qca
->txq
);
494 skb_queue_head_init(&qca
->tx_wait_q
);
495 spin_lock_init(&qca
->hci_ibs_lock
);
496 qca
->workqueue
= alloc_ordered_workqueue("qca_wq", 0);
497 if (!qca
->workqueue
) {
498 BT_ERR("QCA Workqueue not initialized properly");
503 INIT_WORK(&qca
->ws_awake_rx
, qca_wq_awake_rx
);
504 INIT_WORK(&qca
->ws_awake_device
, qca_wq_awake_device
);
505 INIT_WORK(&qca
->ws_rx_vote_off
, qca_wq_serial_rx_clock_vote_off
);
506 INIT_WORK(&qca
->ws_tx_vote_off
, qca_wq_serial_tx_clock_vote_off
);
508 init_waitqueue_head(&qca
->suspend_wait_q
);
511 init_completion(&qca
->drop_ev_comp
);
513 /* Assume we start with both sides asleep -- extra wakes OK */
514 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
515 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
517 qca
->vote_last_jif
= jiffies
;
523 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
524 if (!qca_is_wcn399x(qcadev
->btsoc_type
)) {
525 gpiod_set_value_cansleep(qcadev
->bt_en
, 1);
526 /* Controller needs time to bootup. */
529 hu
->init_speed
= qcadev
->init_speed
;
530 hu
->oper_speed
= qcadev
->oper_speed
;
531 ret
= qca_regulator_enable(qcadev
);
533 destroy_workqueue(qca
->workqueue
);
534 kfree_skb(qca
->rx_skb
);
542 timer_setup(&qca
->wake_retrans_timer
, hci_ibs_wake_retrans_timeout
, 0);
543 qca
->wake_retrans
= IBS_WAKE_RETRANS_TIMEOUT_MS
;
545 timer_setup(&qca
->tx_idle_timer
, hci_ibs_tx_idle_timeout
, 0);
546 qca
->tx_idle_delay
= IBS_HOST_TX_IDLE_TIMEOUT_MS
;
548 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
549 qca
->tx_idle_delay
, qca
->wake_retrans
);
554 static void qca_debugfs_init(struct hci_dev
*hdev
)
556 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
557 struct qca_data
*qca
= hu
->priv
;
558 struct dentry
*ibs_dir
;
564 ibs_dir
= debugfs_create_dir("ibs", hdev
->debugfs
);
568 debugfs_create_u8("tx_ibs_state", mode
, ibs_dir
, &qca
->tx_ibs_state
);
569 debugfs_create_u8("rx_ibs_state", mode
, ibs_dir
, &qca
->rx_ibs_state
);
570 debugfs_create_u64("ibs_sent_sleeps", mode
, ibs_dir
,
571 &qca
->ibs_sent_slps
);
572 debugfs_create_u64("ibs_sent_wakes", mode
, ibs_dir
,
573 &qca
->ibs_sent_wakes
);
574 debugfs_create_u64("ibs_sent_wake_acks", mode
, ibs_dir
,
575 &qca
->ibs_sent_wacks
);
576 debugfs_create_u64("ibs_recv_sleeps", mode
, ibs_dir
,
577 &qca
->ibs_recv_slps
);
578 debugfs_create_u64("ibs_recv_wakes", mode
, ibs_dir
,
579 &qca
->ibs_recv_wakes
);
580 debugfs_create_u64("ibs_recv_wake_acks", mode
, ibs_dir
,
581 &qca
->ibs_recv_wacks
);
582 debugfs_create_bool("tx_vote", mode
, ibs_dir
, &qca
->tx_vote
);
583 debugfs_create_u64("tx_votes_on", mode
, ibs_dir
, &qca
->tx_votes_on
);
584 debugfs_create_u64("tx_votes_off", mode
, ibs_dir
, &qca
->tx_votes_off
);
585 debugfs_create_bool("rx_vote", mode
, ibs_dir
, &qca
->rx_vote
);
586 debugfs_create_u64("rx_votes_on", mode
, ibs_dir
, &qca
->rx_votes_on
);
587 debugfs_create_u64("rx_votes_off", mode
, ibs_dir
, &qca
->rx_votes_off
);
588 debugfs_create_u64("votes_on", mode
, ibs_dir
, &qca
->votes_on
);
589 debugfs_create_u64("votes_off", mode
, ibs_dir
, &qca
->votes_off
);
590 debugfs_create_u32("vote_on_ms", mode
, ibs_dir
, &qca
->vote_on_ms
);
591 debugfs_create_u32("vote_off_ms", mode
, ibs_dir
, &qca
->vote_off_ms
);
594 mode
= S_IRUGO
| S_IWUSR
;
595 debugfs_create_u32("wake_retrans", mode
, ibs_dir
, &qca
->wake_retrans
);
596 debugfs_create_u32("tx_idle_delay", mode
, ibs_dir
,
597 &qca
->tx_idle_delay
);
600 /* Flush protocol data */
601 static int qca_flush(struct hci_uart
*hu
)
603 struct qca_data
*qca
= hu
->priv
;
605 BT_DBG("hu %p qca flush", hu
);
607 skb_queue_purge(&qca
->tx_wait_q
);
608 skb_queue_purge(&qca
->txq
);
614 static int qca_close(struct hci_uart
*hu
)
616 struct qca_serdev
*qcadev
;
617 struct qca_data
*qca
= hu
->priv
;
619 BT_DBG("hu %p qca close", hu
);
621 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE
, hu
);
623 skb_queue_purge(&qca
->tx_wait_q
);
624 skb_queue_purge(&qca
->txq
);
625 del_timer(&qca
->tx_idle_timer
);
626 del_timer(&qca
->wake_retrans_timer
);
627 destroy_workqueue(qca
->workqueue
);
631 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
632 if (qca_is_wcn399x(qcadev
->btsoc_type
))
633 qca_power_shutdown(hu
);
635 gpiod_set_value_cansleep(qcadev
->bt_en
, 0);
639 kfree_skb(qca
->rx_skb
);
648 /* Called upon a wake-up-indication from the device.
650 static void device_want_to_wakeup(struct hci_uart
*hu
)
653 struct qca_data
*qca
= hu
->priv
;
655 BT_DBG("hu %p want to wake up", hu
);
657 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
659 qca
->ibs_recv_wakes
++;
661 /* Don't wake the rx up when suspending. */
662 if (test_bit(QCA_SUSPENDING
, &qca
->flags
)) {
663 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
667 switch (qca
->rx_ibs_state
) {
668 case HCI_IBS_RX_ASLEEP
:
669 /* Make sure clock is on - we may have turned clock off since
670 * receiving the wake up indicator awake rx clock.
672 queue_work(qca
->workqueue
, &qca
->ws_awake_rx
);
673 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
676 case HCI_IBS_RX_AWAKE
:
677 /* Always acknowledge device wake up,
678 * sending IBS message doesn't count as TX ON.
680 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0) {
681 BT_ERR("Failed to acknowledge device wake up");
684 qca
->ibs_sent_wacks
++;
688 /* Any other state is illegal */
689 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
694 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
696 /* Actually send the packets */
697 hci_uart_tx_wakeup(hu
);
700 /* Called upon a sleep-indication from the device.
702 static void device_want_to_sleep(struct hci_uart
*hu
)
705 struct qca_data
*qca
= hu
->priv
;
707 BT_DBG("hu %p want to sleep in %d state", hu
, qca
->rx_ibs_state
);
709 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
711 qca
->ibs_recv_slps
++;
713 switch (qca
->rx_ibs_state
) {
714 case HCI_IBS_RX_AWAKE
:
716 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
717 /* Vote off rx clock under workqueue */
718 queue_work(qca
->workqueue
, &qca
->ws_rx_vote_off
);
721 case HCI_IBS_RX_ASLEEP
:
725 /* Any other state is illegal */
726 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
731 wake_up_interruptible(&qca
->suspend_wait_q
);
733 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
736 /* Called upon wake-up-acknowledgement from the device
738 static void device_woke_up(struct hci_uart
*hu
)
740 unsigned long flags
, idle_delay
;
741 struct qca_data
*qca
= hu
->priv
;
742 struct sk_buff
*skb
= NULL
;
744 BT_DBG("hu %p woke up", hu
);
746 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
748 qca
->ibs_recv_wacks
++;
750 /* Don't react to the wake-up-acknowledgment when suspending. */
751 if (test_bit(QCA_SUSPENDING
, &qca
->flags
)) {
752 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
756 switch (qca
->tx_ibs_state
) {
757 case HCI_IBS_TX_AWAKE
:
758 /* Expect one if we send 2 WAKEs */
759 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
763 case HCI_IBS_TX_WAKING
:
764 /* Send pending packets */
765 while ((skb
= skb_dequeue(&qca
->tx_wait_q
)))
766 skb_queue_tail(&qca
->txq
, skb
);
768 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
769 del_timer(&qca
->wake_retrans_timer
);
770 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
771 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
772 qca
->tx_ibs_state
= HCI_IBS_TX_AWAKE
;
775 case HCI_IBS_TX_ASLEEP
:
779 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
784 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
786 /* Actually send the packets */
787 hci_uart_tx_wakeup(hu
);
790 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
791 * two simultaneous tasklets.
793 static int qca_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
795 unsigned long flags
= 0, idle_delay
;
796 struct qca_data
*qca
= hu
->priv
;
798 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu
, skb
,
801 /* Prepend skb with frame type */
802 memcpy(skb_push(skb
, 1), &hci_skb_pkt_type(skb
), 1);
804 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
806 /* Don't go to sleep in middle of patch download or
807 * Out-Of-Band(GPIOs control) sleep is selected.
808 * Don't wake the device up when suspending.
810 if (!test_bit(QCA_IBS_ENABLED
, &qca
->flags
) ||
811 test_bit(QCA_SUSPENDING
, &qca
->flags
)) {
812 skb_queue_tail(&qca
->txq
, skb
);
813 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
817 /* Act according to current state */
818 switch (qca
->tx_ibs_state
) {
819 case HCI_IBS_TX_AWAKE
:
820 BT_DBG("Device awake, sending normally");
821 skb_queue_tail(&qca
->txq
, skb
);
822 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
823 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
826 case HCI_IBS_TX_ASLEEP
:
827 BT_DBG("Device asleep, waking up and queueing packet");
828 /* Save packet for later */
829 skb_queue_tail(&qca
->tx_wait_q
, skb
);
831 qca
->tx_ibs_state
= HCI_IBS_TX_WAKING
;
832 /* Schedule a work queue to wake up device */
833 queue_work(qca
->workqueue
, &qca
->ws_awake_device
);
836 case HCI_IBS_TX_WAKING
:
837 BT_DBG("Device waking up, queueing packet");
838 /* Transient state; just keep packet for later */
839 skb_queue_tail(&qca
->tx_wait_q
, skb
);
843 BT_ERR("Illegal tx state: %d (losing packet)",
849 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
854 static int qca_ibs_sleep_ind(struct hci_dev
*hdev
, struct sk_buff
*skb
)
856 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
858 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_SLEEP_IND
);
860 device_want_to_sleep(hu
);
866 static int qca_ibs_wake_ind(struct hci_dev
*hdev
, struct sk_buff
*skb
)
868 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
870 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_WAKE_IND
);
872 device_want_to_wakeup(hu
);
878 static int qca_ibs_wake_ack(struct hci_dev
*hdev
, struct sk_buff
*skb
)
880 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
882 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_WAKE_ACK
);
890 static int qca_recv_acl_data(struct hci_dev
*hdev
, struct sk_buff
*skb
)
892 /* We receive debug logs from chip as an ACL packets.
893 * Instead of sending the data to ACL to decode the
894 * received data, we are pushing them to the above layers
895 * as a diagnostic packet.
897 if (get_unaligned_le16(skb
->data
) == QCA_DEBUG_HANDLE
)
898 return hci_recv_diag(hdev
, skb
);
900 return hci_recv_frame(hdev
, skb
);
903 static int qca_recv_event(struct hci_dev
*hdev
, struct sk_buff
*skb
)
905 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
906 struct qca_data
*qca
= hu
->priv
;
908 if (test_bit(QCA_DROP_VENDOR_EVENT
, &qca
->flags
)) {
909 struct hci_event_hdr
*hdr
= (void *)skb
->data
;
911 /* For the WCN3990 the vendor command for a baudrate change
912 * isn't sent as synchronous HCI command, because the
913 * controller sends the corresponding vendor event with the
914 * new baudrate. The event is received and properly decoded
915 * after changing the baudrate of the host port. It needs to
916 * be dropped, otherwise it can be misinterpreted as
917 * response to a later firmware download command (also a
921 if (hdr
->evt
== HCI_EV_VENDOR
)
922 complete(&qca
->drop_ev_comp
);
929 return hci_recv_frame(hdev
, skb
);
932 #define QCA_IBS_SLEEP_IND_EVENT \
933 .type = HCI_IBS_SLEEP_IND, \
937 .maxlen = HCI_MAX_IBS_SIZE
939 #define QCA_IBS_WAKE_IND_EVENT \
940 .type = HCI_IBS_WAKE_IND, \
944 .maxlen = HCI_MAX_IBS_SIZE
946 #define QCA_IBS_WAKE_ACK_EVENT \
947 .type = HCI_IBS_WAKE_ACK, \
951 .maxlen = HCI_MAX_IBS_SIZE
953 static const struct h4_recv_pkt qca_recv_pkts
[] = {
954 { H4_RECV_ACL
, .recv
= qca_recv_acl_data
},
955 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
956 { H4_RECV_EVENT
, .recv
= qca_recv_event
},
957 { QCA_IBS_WAKE_IND_EVENT
, .recv
= qca_ibs_wake_ind
},
958 { QCA_IBS_WAKE_ACK_EVENT
, .recv
= qca_ibs_wake_ack
},
959 { QCA_IBS_SLEEP_IND_EVENT
, .recv
= qca_ibs_sleep_ind
},
962 static int qca_recv(struct hci_uart
*hu
, const void *data
, int count
)
964 struct qca_data
*qca
= hu
->priv
;
966 if (!test_bit(HCI_UART_REGISTERED
, &hu
->flags
))
969 qca
->rx_skb
= h4_recv_buf(hu
->hdev
, qca
->rx_skb
, data
, count
,
970 qca_recv_pkts
, ARRAY_SIZE(qca_recv_pkts
));
971 if (IS_ERR(qca
->rx_skb
)) {
972 int err
= PTR_ERR(qca
->rx_skb
);
973 bt_dev_err(hu
->hdev
, "Frame reassembly failed (%d)", err
);
981 static struct sk_buff
*qca_dequeue(struct hci_uart
*hu
)
983 struct qca_data
*qca
= hu
->priv
;
985 return skb_dequeue(&qca
->txq
);
988 static uint8_t qca_get_baudrate_value(int speed
)
992 return QCA_BAUDRATE_9600
;
994 return QCA_BAUDRATE_19200
;
996 return QCA_BAUDRATE_38400
;
998 return QCA_BAUDRATE_57600
;
1000 return QCA_BAUDRATE_115200
;
1002 return QCA_BAUDRATE_230400
;
1004 return QCA_BAUDRATE_460800
;
1006 return QCA_BAUDRATE_500000
;
1008 return QCA_BAUDRATE_921600
;
1010 return QCA_BAUDRATE_1000000
;
1012 return QCA_BAUDRATE_2000000
;
1014 return QCA_BAUDRATE_3000000
;
1016 return QCA_BAUDRATE_3200000
;
1018 return QCA_BAUDRATE_3500000
;
1020 return QCA_BAUDRATE_115200
;
1024 static int qca_set_baudrate(struct hci_dev
*hdev
, uint8_t baudrate
)
1026 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1027 struct qca_data
*qca
= hu
->priv
;
1028 struct sk_buff
*skb
;
1029 u8 cmd
[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1031 if (baudrate
> QCA_BAUDRATE_3200000
)
1036 skb
= bt_skb_alloc(sizeof(cmd
), GFP_KERNEL
);
1038 bt_dev_err(hdev
, "Failed to allocate baudrate packet");
1042 /* Assign commands to change baudrate and packet type. */
1043 skb_put_data(skb
, cmd
, sizeof(cmd
));
1044 hci_skb_pkt_type(skb
) = HCI_COMMAND_PKT
;
1046 skb_queue_tail(&qca
->txq
, skb
);
1047 hci_uart_tx_wakeup(hu
);
1049 /* Wait for the baudrate change request to be sent */
1051 while (!skb_queue_empty(&qca
->txq
))
1052 usleep_range(100, 200);
1055 serdev_device_wait_until_sent(hu
->serdev
,
1056 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS
));
1058 /* Give the controller time to process the request */
1059 if (qca_is_wcn399x(qca_soc_type(hu
)))
1067 static inline void host_set_baudrate(struct hci_uart
*hu
, unsigned int speed
)
1070 serdev_device_set_baudrate(hu
->serdev
, speed
);
1072 hci_uart_set_baudrate(hu
, speed
);
1075 static int qca_send_power_pulse(struct hci_uart
*hu
, bool on
)
1078 int timeout
= msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS
);
1079 u8 cmd
= on
? QCA_WCN3990_POWERON_PULSE
: QCA_WCN3990_POWEROFF_PULSE
;
1081 /* These power pulses are single byte command which are sent
1082 * at required baudrate to wcn3990. On wcn3990, we have an external
1083 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1084 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1085 * and also we use the same power inputs to turn on and off for
1086 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1087 * we send a power on pulse at 115200 bps. This algorithm will help to
1088 * save power. Disabling hardware flow control is mandatory while
1089 * sending power pulses to SoC.
1091 bt_dev_dbg(hu
->hdev
, "sending power pulse %02x to controller", cmd
);
1093 serdev_device_write_flush(hu
->serdev
);
1094 hci_uart_set_flow_control(hu
, true);
1095 ret
= serdev_device_write_buf(hu
->serdev
, &cmd
, sizeof(cmd
));
1097 bt_dev_err(hu
->hdev
, "failed to send power pulse %02x", cmd
);
1101 serdev_device_wait_until_sent(hu
->serdev
, timeout
);
1102 hci_uart_set_flow_control(hu
, false);
1104 /* Give to controller time to boot/shutdown */
1113 static unsigned int qca_get_speed(struct hci_uart
*hu
,
1114 enum qca_speed_type speed_type
)
1116 unsigned int speed
= 0;
1118 if (speed_type
== QCA_INIT_SPEED
) {
1120 speed
= hu
->init_speed
;
1121 else if (hu
->proto
->init_speed
)
1122 speed
= hu
->proto
->init_speed
;
1125 speed
= hu
->oper_speed
;
1126 else if (hu
->proto
->oper_speed
)
1127 speed
= hu
->proto
->oper_speed
;
1133 static int qca_check_speeds(struct hci_uart
*hu
)
1135 if (qca_is_wcn399x(qca_soc_type(hu
))) {
1136 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) &&
1137 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1140 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) ||
1141 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1148 static int qca_set_speed(struct hci_uart
*hu
, enum qca_speed_type speed_type
)
1150 unsigned int speed
, qca_baudrate
;
1151 struct qca_data
*qca
= hu
->priv
;
1154 if (speed_type
== QCA_INIT_SPEED
) {
1155 speed
= qca_get_speed(hu
, QCA_INIT_SPEED
);
1157 host_set_baudrate(hu
, speed
);
1159 enum qca_btsoc_type soc_type
= qca_soc_type(hu
);
1161 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1165 /* Disable flow control for wcn3990 to deassert RTS while
1166 * changing the baudrate of chip and host.
1168 if (qca_is_wcn399x(soc_type
))
1169 hci_uart_set_flow_control(hu
, true);
1171 if (soc_type
== QCA_WCN3990
) {
1172 reinit_completion(&qca
->drop_ev_comp
);
1173 set_bit(QCA_DROP_VENDOR_EVENT
, &qca
->flags
);
1176 qca_baudrate
= qca_get_baudrate_value(speed
);
1177 bt_dev_dbg(hu
->hdev
, "Set UART speed to %d", speed
);
1178 ret
= qca_set_baudrate(hu
->hdev
, qca_baudrate
);
1182 host_set_baudrate(hu
, speed
);
1185 if (qca_is_wcn399x(soc_type
))
1186 hci_uart_set_flow_control(hu
, false);
1188 if (soc_type
== QCA_WCN3990
) {
1189 /* Wait for the controller to send the vendor event
1190 * for the baudrate change command.
1192 if (!wait_for_completion_timeout(&qca
->drop_ev_comp
,
1193 msecs_to_jiffies(100))) {
1194 bt_dev_err(hu
->hdev
,
1195 "Failed to change controller baudrate\n");
1199 clear_bit(QCA_DROP_VENDOR_EVENT
, &qca
->flags
);
1206 static int qca_wcn3990_init(struct hci_uart
*hu
)
1208 struct qca_serdev
*qcadev
;
1211 /* Check for vregs status, may be hci down has turned
1212 * off the voltage regulator.
1214 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1215 if (!qcadev
->bt_power
->vregs_on
) {
1216 serdev_device_close(hu
->serdev
);
1217 ret
= qca_regulator_enable(qcadev
);
1221 ret
= serdev_device_open(hu
->serdev
);
1223 bt_dev_err(hu
->hdev
, "failed to open port");
1228 /* Forcefully enable wcn3990 to enter in to boot mode. */
1229 host_set_baudrate(hu
, 2400);
1230 ret
= qca_send_power_pulse(hu
, false);
1234 qca_set_speed(hu
, QCA_INIT_SPEED
);
1235 ret
= qca_send_power_pulse(hu
, true);
1239 /* Now the device is in ready state to communicate with host.
1240 * To sync host with device we need to reopen port.
1241 * Without this, we will have RTS and CTS synchronization
1244 serdev_device_close(hu
->serdev
);
1245 ret
= serdev_device_open(hu
->serdev
);
1247 bt_dev_err(hu
->hdev
, "failed to open port");
1251 hci_uart_set_flow_control(hu
, false);
1256 static int qca_setup(struct hci_uart
*hu
)
1258 struct hci_dev
*hdev
= hu
->hdev
;
1259 struct qca_data
*qca
= hu
->priv
;
1260 unsigned int speed
, qca_baudrate
= QCA_BAUDRATE_115200
;
1261 enum qca_btsoc_type soc_type
= qca_soc_type(hu
);
1262 const char *firmware_name
= qca_get_firmware_name(hu
);
1266 ret
= qca_check_speeds(hu
);
1270 /* Patch downloading has to be done without IBS mode */
1271 clear_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1273 /* Enable controller to do both LE scan and BR/EDR inquiry
1276 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
1278 if (qca_is_wcn399x(soc_type
)) {
1279 bt_dev_info(hdev
, "setting up wcn3990");
1281 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1282 * setup for every hci up.
1284 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP
, &hdev
->quirks
);
1285 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY
, &hdev
->quirks
);
1286 hu
->hdev
->shutdown
= qca_power_off
;
1287 ret
= qca_wcn3990_init(hu
);
1291 ret
= qca_read_soc_version(hdev
, &soc_ver
, soc_type
);
1295 bt_dev_info(hdev
, "ROME setup");
1296 qca_set_speed(hu
, QCA_INIT_SPEED
);
1299 /* Setup user speed if needed */
1300 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1302 ret
= qca_set_speed(hu
, QCA_OPER_SPEED
);
1306 qca_baudrate
= qca_get_baudrate_value(speed
);
1309 if (!qca_is_wcn399x(soc_type
)) {
1310 /* Get QCA version information */
1311 ret
= qca_read_soc_version(hdev
, &soc_ver
, soc_type
);
1316 bt_dev_info(hdev
, "QCA controller version 0x%08x", soc_ver
);
1317 /* Setup patch / NVM configurations */
1318 ret
= qca_uart_setup(hdev
, qca_baudrate
, soc_type
, soc_ver
,
1321 set_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1322 qca_debugfs_init(hdev
);
1323 } else if (ret
== -ENOENT
) {
1324 /* No patch/nvm-config found, run with original fw/config */
1326 } else if (ret
== -EAGAIN
) {
1328 * Userspace firmware loader will return -EAGAIN in case no
1329 * patch/nvm-config is found, so run with original fw/config.
1335 if (qca_is_wcn399x(soc_type
))
1336 hu
->hdev
->set_bdaddr
= qca_set_bdaddr
;
1338 hu
->hdev
->set_bdaddr
= qca_set_bdaddr_rome
;
1343 static const struct hci_uart_proto qca_proto
= {
1347 .init_speed
= 115200,
1348 .oper_speed
= 3000000,
1354 .enqueue
= qca_enqueue
,
1355 .dequeue
= qca_dequeue
,
1358 static const struct qca_vreg_data qca_soc_data_wcn3990
= {
1359 .soc_type
= QCA_WCN3990
,
1360 .vregs
= (struct qca_vreg
[]) {
1363 { "vddrf", 300000 },
1364 { "vddch0", 450000 },
1369 static const struct qca_vreg_data qca_soc_data_wcn3991
= {
1370 .soc_type
= QCA_WCN3991
,
1371 .vregs
= (struct qca_vreg
[]) {
1374 { "vddrf", 300000 },
1375 { "vddch0", 450000 },
1380 static const struct qca_vreg_data qca_soc_data_wcn3998
= {
1381 .soc_type
= QCA_WCN3998
,
1382 .vregs
= (struct qca_vreg
[]) {
1385 { "vddrf", 300000 },
1386 { "vddch0", 450000 },
1391 static void qca_power_shutdown(struct hci_uart
*hu
)
1393 struct qca_serdev
*qcadev
;
1394 struct qca_data
*qca
= hu
->priv
;
1395 unsigned long flags
;
1397 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1399 /* From this point we go into power off state. But serial port is
1400 * still open, stop queueing the IBS data and flush all the buffered
1403 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
1404 clear_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1406 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
1408 host_set_baudrate(hu
, 2400);
1409 qca_send_power_pulse(hu
, false);
1410 qca_regulator_disable(qcadev
);
1413 static int qca_power_off(struct hci_dev
*hdev
)
1415 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1417 /* Perform pre shutdown command */
1418 qca_send_pre_shutdown_cmd(hdev
);
1420 usleep_range(8000, 10000);
1422 qca_power_shutdown(hu
);
1426 static int qca_regulator_enable(struct qca_serdev
*qcadev
)
1428 struct qca_power
*power
= qcadev
->bt_power
;
1431 /* Already enabled */
1432 if (power
->vregs_on
)
1435 BT_DBG("enabling %d regulators)", power
->num_vregs
);
1437 ret
= regulator_bulk_enable(power
->num_vregs
, power
->vreg_bulk
);
1441 power
->vregs_on
= true;
1446 static void qca_regulator_disable(struct qca_serdev
*qcadev
)
1448 struct qca_power
*power
;
1453 power
= qcadev
->bt_power
;
1455 /* Already disabled? */
1456 if (!power
->vregs_on
)
1459 regulator_bulk_disable(power
->num_vregs
, power
->vreg_bulk
);
1460 power
->vregs_on
= false;
1463 static int qca_init_regulators(struct qca_power
*qca
,
1464 const struct qca_vreg
*vregs
, size_t num_vregs
)
1466 struct regulator_bulk_data
*bulk
;
1470 bulk
= devm_kcalloc(qca
->dev
, num_vregs
, sizeof(*bulk
), GFP_KERNEL
);
1474 for (i
= 0; i
< num_vregs
; i
++)
1475 bulk
[i
].supply
= vregs
[i
].name
;
1477 ret
= devm_regulator_bulk_get(qca
->dev
, num_vregs
, bulk
);
1481 for (i
= 0; i
< num_vregs
; i
++) {
1482 ret
= regulator_set_load(bulk
[i
].consumer
, vregs
[i
].load_uA
);
1487 qca
->vreg_bulk
= bulk
;
1488 qca
->num_vregs
= num_vregs
;
1493 static int qca_serdev_probe(struct serdev_device
*serdev
)
1495 struct qca_serdev
*qcadev
;
1496 const struct qca_vreg_data
*data
;
1499 qcadev
= devm_kzalloc(&serdev
->dev
, sizeof(*qcadev
), GFP_KERNEL
);
1503 qcadev
->serdev_hu
.serdev
= serdev
;
1504 data
= of_device_get_match_data(&serdev
->dev
);
1505 serdev_device_set_drvdata(serdev
, qcadev
);
1506 device_property_read_string(&serdev
->dev
, "firmware-name",
1507 &qcadev
->firmware_name
);
1508 if (data
&& qca_is_wcn399x(data
->soc_type
)) {
1509 qcadev
->btsoc_type
= data
->soc_type
;
1510 qcadev
->bt_power
= devm_kzalloc(&serdev
->dev
,
1511 sizeof(struct qca_power
),
1513 if (!qcadev
->bt_power
)
1516 qcadev
->bt_power
->dev
= &serdev
->dev
;
1517 err
= qca_init_regulators(qcadev
->bt_power
, data
->vregs
,
1520 BT_ERR("Failed to init regulators:%d", err
);
1524 qcadev
->bt_power
->vregs_on
= false;
1526 device_property_read_u32(&serdev
->dev
, "max-speed",
1527 &qcadev
->oper_speed
);
1528 if (!qcadev
->oper_speed
)
1529 BT_DBG("UART will pick default operating speed");
1531 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1533 BT_ERR("wcn3990 serdev registration failed");
1537 qcadev
->btsoc_type
= QCA_ROME
;
1538 qcadev
->bt_en
= devm_gpiod_get(&serdev
->dev
, "enable",
1540 if (IS_ERR(qcadev
->bt_en
)) {
1541 dev_err(&serdev
->dev
, "failed to acquire enable gpio\n");
1542 return PTR_ERR(qcadev
->bt_en
);
1545 qcadev
->susclk
= devm_clk_get(&serdev
->dev
, NULL
);
1546 if (IS_ERR(qcadev
->susclk
)) {
1547 dev_err(&serdev
->dev
, "failed to acquire clk\n");
1548 return PTR_ERR(qcadev
->susclk
);
1551 err
= clk_set_rate(qcadev
->susclk
, SUSCLK_RATE_32KHZ
);
1555 err
= clk_prepare_enable(qcadev
->susclk
);
1559 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1561 clk_disable_unprepare(qcadev
->susclk
);
1568 static void qca_serdev_remove(struct serdev_device
*serdev
)
1570 struct qca_serdev
*qcadev
= serdev_device_get_drvdata(serdev
);
1572 if (qca_is_wcn399x(qcadev
->btsoc_type
))
1573 qca_power_shutdown(&qcadev
->serdev_hu
);
1575 clk_disable_unprepare(qcadev
->susclk
);
1577 hci_uart_unregister_device(&qcadev
->serdev_hu
);
1580 static int __maybe_unused
qca_suspend(struct device
*dev
)
1582 struct hci_dev
*hdev
= container_of(dev
, struct hci_dev
, dev
);
1583 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1584 struct qca_data
*qca
= hu
->priv
;
1585 unsigned long flags
;
1589 set_bit(QCA_SUSPENDING
, &qca
->flags
);
1591 /* Device is downloading patch or doesn't support in-band sleep. */
1592 if (!test_bit(QCA_IBS_ENABLED
, &qca
->flags
))
1595 cancel_work_sync(&qca
->ws_awake_device
);
1596 cancel_work_sync(&qca
->ws_awake_rx
);
1598 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
1599 flags
, SINGLE_DEPTH_NESTING
);
1601 switch (qca
->tx_ibs_state
) {
1602 case HCI_IBS_TX_WAKING
:
1603 del_timer(&qca
->wake_retrans_timer
);
1605 case HCI_IBS_TX_AWAKE
:
1606 del_timer(&qca
->tx_idle_timer
);
1608 serdev_device_write_flush(hu
->serdev
);
1609 cmd
= HCI_IBS_SLEEP_IND
;
1610 ret
= serdev_device_write_buf(hu
->serdev
, &cmd
, sizeof(cmd
));
1613 BT_ERR("Failed to send SLEEP to device");
1617 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
1618 qca
->ibs_sent_slps
++;
1620 qca_wq_serial_tx_clock_vote_off(&qca
->ws_tx_vote_off
);
1623 case HCI_IBS_TX_ASLEEP
:
1627 BT_ERR("Spurious tx state %d", qca
->tx_ibs_state
);
1632 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
1637 serdev_device_wait_until_sent(hu
->serdev
,
1638 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS
));
1640 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
1641 * to sleep, so that the packet does not wake the system later.
1644 ret
= wait_event_interruptible_timeout(qca
->suspend_wait_q
,
1645 qca
->rx_ibs_state
== HCI_IBS_RX_ASLEEP
,
1646 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS
));
1655 clear_bit(QCA_SUSPENDING
, &qca
->flags
);
1660 static int __maybe_unused
qca_resume(struct device
*dev
)
1662 struct hci_dev
*hdev
= container_of(dev
, struct hci_dev
, dev
);
1663 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1664 struct qca_data
*qca
= hu
->priv
;
1666 clear_bit(QCA_SUSPENDING
, &qca
->flags
);
1671 static SIMPLE_DEV_PM_OPS(qca_pm_ops
, qca_suspend
, qca_resume
);
1673 static const struct of_device_id qca_bluetooth_of_match
[] = {
1674 { .compatible
= "qcom,qca6174-bt" },
1675 { .compatible
= "qcom,wcn3990-bt", .data
= &qca_soc_data_wcn3990
},
1676 { .compatible
= "qcom,wcn3991-bt", .data
= &qca_soc_data_wcn3991
},
1677 { .compatible
= "qcom,wcn3998-bt", .data
= &qca_soc_data_wcn3998
},
1680 MODULE_DEVICE_TABLE(of
, qca_bluetooth_of_match
);
1682 static struct serdev_device_driver qca_serdev_driver
= {
1683 .probe
= qca_serdev_probe
,
1684 .remove
= qca_serdev_remove
,
1686 .name
= "hci_uart_qca",
1687 .of_match_table
= qca_bluetooth_of_match
,
1692 int __init
qca_init(void)
1694 serdev_device_driver_register(&qca_serdev_driver
);
1696 return hci_uart_register_proto(&qca_proto
);
1699 int __exit
qca_deinit(void)
1701 serdev_device_driver_unregister(&qca_serdev_driver
);
1703 return hci_uart_unregister_proto(&qca_proto
);