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/debugfs.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/mod_devicetable.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/serdev.h>
30 #include <asm/unaligned.h>
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
38 /* HCI_IBS protocol messages */
39 #define HCI_IBS_SLEEP_IND 0xFE
40 #define HCI_IBS_WAKE_IND 0xFD
41 #define HCI_IBS_WAKE_ACK 0xFC
42 #define HCI_MAX_IBS_SIZE 10
44 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
45 #define IBS_TX_IDLE_TIMEOUT_MS 2000
46 #define CMD_TRANS_TIMEOUT_MS 100
49 #define SUSCLK_RATE_32KHZ 32768
51 /* Controller debug log header */
52 #define QCA_DEBUG_HANDLE 0x2EDC
58 /* HCI_IBS transmit side sleep protocol states */
65 /* HCI_IBS receive side sleep protocol states */
71 /* HCI_IBS transmit and receive side clock state vote */
72 enum hci_ibs_clock_state_vote
{
73 HCI_IBS_VOTE_STATS_UPDATE
,
74 HCI_IBS_TX_VOTE_CLOCK_ON
,
75 HCI_IBS_TX_VOTE_CLOCK_OFF
,
76 HCI_IBS_RX_VOTE_CLOCK_ON
,
77 HCI_IBS_RX_VOTE_CLOCK_OFF
,
82 struct sk_buff
*rx_skb
;
83 struct sk_buff_head txq
;
84 struct sk_buff_head tx_wait_q
; /* HCI_IBS wait queue */
85 spinlock_t hci_ibs_lock
; /* HCI_IBS state lock */
86 u8 tx_ibs_state
; /* HCI_IBS transmit side power state*/
87 u8 rx_ibs_state
; /* HCI_IBS receive side power state */
88 bool tx_vote
; /* Clock must be on for TX */
89 bool rx_vote
; /* Clock must be on for RX */
90 struct timer_list tx_idle_timer
;
92 struct timer_list wake_retrans_timer
;
94 struct workqueue_struct
*workqueue
;
95 struct work_struct ws_awake_rx
;
96 struct work_struct ws_awake_device
;
97 struct work_struct ws_rx_vote_off
;
98 struct work_struct ws_tx_vote_off
;
101 /* For debugging purpose */
119 enum qca_speed_type
{
125 * Voltage regulator information required for configuring the
126 * QCA Bluetooth chipset
132 unsigned int load_uA
;
135 struct qca_vreg_data
{
136 enum qca_btsoc_type soc_type
;
137 struct qca_vreg
*vregs
;
142 * Platform data for the QCA Bluetooth power driver.
146 const struct qca_vreg_data
*vreg_data
;
147 struct regulator_bulk_data
*vreg_bulk
;
152 struct hci_uart serdev_hu
;
153 struct gpio_desc
*bt_en
;
155 enum qca_btsoc_type btsoc_type
;
156 struct qca_power
*bt_power
;
161 static int qca_power_setup(struct hci_uart
*hu
, bool on
);
162 static void qca_power_shutdown(struct hci_uart
*hu
);
163 static int qca_power_off(struct hci_dev
*hdev
);
165 static enum qca_btsoc_type
qca_soc_type(struct hci_uart
*hu
)
167 enum qca_btsoc_type soc_type
;
170 struct qca_serdev
*qsd
= serdev_device_get_drvdata(hu
->serdev
);
172 soc_type
= qsd
->btsoc_type
;
180 static void __serial_clock_on(struct tty_struct
*tty
)
182 /* TODO: Some chipset requires to enable UART clock on client
183 * side to save power consumption or manual work is required.
184 * Please put your code to control UART clock here if needed
188 static void __serial_clock_off(struct tty_struct
*tty
)
190 /* TODO: Some chipset requires to disable UART clock on client
191 * side to save power consumption or manual work is required.
192 * Please put your code to control UART clock off here if needed
196 /* serial_clock_vote needs to be called with the ibs lock held */
197 static void serial_clock_vote(unsigned long vote
, struct hci_uart
*hu
)
199 struct qca_data
*qca
= hu
->priv
;
202 bool old_vote
= (qca
->tx_vote
| qca
->rx_vote
);
206 case HCI_IBS_VOTE_STATS_UPDATE
:
207 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
210 qca
->vote_off_ms
+= diff
;
212 qca
->vote_on_ms
+= diff
;
215 case HCI_IBS_TX_VOTE_CLOCK_ON
:
221 case HCI_IBS_RX_VOTE_CLOCK_ON
:
227 case HCI_IBS_TX_VOTE_CLOCK_OFF
:
228 qca
->tx_vote
= false;
230 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
233 case HCI_IBS_RX_VOTE_CLOCK_OFF
:
234 qca
->rx_vote
= false;
236 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
240 BT_ERR("Voting irregularity");
244 if (new_vote
!= old_vote
) {
246 __serial_clock_on(hu
->tty
);
248 __serial_clock_off(hu
->tty
);
250 BT_DBG("Vote serial clock %s(%s)", new_vote
? "true" : "false",
251 vote
? "true" : "false");
253 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
257 qca
->vote_off_ms
+= diff
;
260 qca
->vote_on_ms
+= diff
;
262 qca
->vote_last_jif
= jiffies
;
266 /* Builds and sends an HCI_IBS command packet.
267 * These are very simple packets with only 1 cmd byte.
269 static int send_hci_ibs_cmd(u8 cmd
, struct hci_uart
*hu
)
272 struct sk_buff
*skb
= NULL
;
273 struct qca_data
*qca
= hu
->priv
;
275 BT_DBG("hu %p send hci ibs cmd 0x%x", hu
, cmd
);
277 skb
= bt_skb_alloc(1, GFP_ATOMIC
);
279 BT_ERR("Failed to allocate memory for HCI_IBS packet");
283 /* Assign HCI_IBS type */
284 skb_put_u8(skb
, cmd
);
286 skb_queue_tail(&qca
->txq
, skb
);
291 static void qca_wq_awake_device(struct work_struct
*work
)
293 struct qca_data
*qca
= container_of(work
, struct qca_data
,
295 struct hci_uart
*hu
= qca
->hu
;
296 unsigned long retrans_delay
;
298 BT_DBG("hu %p wq awake device", hu
);
300 /* Vote for serial clock */
301 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON
, hu
);
303 spin_lock(&qca
->hci_ibs_lock
);
305 /* Send wake indication to device */
306 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0)
307 BT_ERR("Failed to send WAKE to device");
309 qca
->ibs_sent_wakes
++;
311 /* Start retransmit timer */
312 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
313 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
315 spin_unlock(&qca
->hci_ibs_lock
);
317 /* Actually send the packets */
318 hci_uart_tx_wakeup(hu
);
321 static void qca_wq_awake_rx(struct work_struct
*work
)
323 struct qca_data
*qca
= container_of(work
, struct qca_data
,
325 struct hci_uart
*hu
= qca
->hu
;
327 BT_DBG("hu %p wq awake rx", hu
);
329 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON
, hu
);
331 spin_lock(&qca
->hci_ibs_lock
);
332 qca
->rx_ibs_state
= HCI_IBS_RX_AWAKE
;
334 /* Always acknowledge device wake up,
335 * sending IBS message doesn't count as TX ON.
337 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0)
338 BT_ERR("Failed to acknowledge device wake up");
340 qca
->ibs_sent_wacks
++;
342 spin_unlock(&qca
->hci_ibs_lock
);
344 /* Actually send the packets */
345 hci_uart_tx_wakeup(hu
);
348 static void qca_wq_serial_rx_clock_vote_off(struct work_struct
*work
)
350 struct qca_data
*qca
= container_of(work
, struct qca_data
,
352 struct hci_uart
*hu
= qca
->hu
;
354 BT_DBG("hu %p rx clock vote off", hu
);
356 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF
, hu
);
359 static void qca_wq_serial_tx_clock_vote_off(struct work_struct
*work
)
361 struct qca_data
*qca
= container_of(work
, struct qca_data
,
363 struct hci_uart
*hu
= qca
->hu
;
365 BT_DBG("hu %p tx clock vote off", hu
);
367 /* Run HCI tx handling unlocked */
368 hci_uart_tx_wakeup(hu
);
370 /* Now that message queued to tty driver, vote for tty clocks off.
371 * It is up to the tty driver to pend the clocks off until tx done.
373 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF
, hu
);
376 static void hci_ibs_tx_idle_timeout(struct timer_list
*t
)
378 struct qca_data
*qca
= from_timer(qca
, t
, tx_idle_timer
);
379 struct hci_uart
*hu
= qca
->hu
;
382 BT_DBG("hu %p idle timeout in %d state", hu
, qca
->tx_ibs_state
);
384 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
385 flags
, SINGLE_DEPTH_NESTING
);
387 switch (qca
->tx_ibs_state
) {
388 case HCI_IBS_TX_AWAKE
:
389 /* TX_IDLE, go to SLEEP */
390 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND
, hu
) < 0) {
391 BT_ERR("Failed to send SLEEP to device");
394 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
395 qca
->ibs_sent_slps
++;
396 queue_work(qca
->workqueue
, &qca
->ws_tx_vote_off
);
399 case HCI_IBS_TX_ASLEEP
:
400 case HCI_IBS_TX_WAKING
:
404 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
408 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
411 static void hci_ibs_wake_retrans_timeout(struct timer_list
*t
)
413 struct qca_data
*qca
= from_timer(qca
, t
, wake_retrans_timer
);
414 struct hci_uart
*hu
= qca
->hu
;
415 unsigned long flags
, retrans_delay
;
416 bool retransmit
= false;
418 BT_DBG("hu %p wake retransmit timeout in %d state",
419 hu
, qca
->tx_ibs_state
);
421 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
422 flags
, SINGLE_DEPTH_NESTING
);
424 switch (qca
->tx_ibs_state
) {
425 case HCI_IBS_TX_WAKING
:
426 /* No WAKE_ACK, retransmit WAKE */
428 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0) {
429 BT_ERR("Failed to acknowledge device wake up");
432 qca
->ibs_sent_wakes
++;
433 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
434 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
437 case HCI_IBS_TX_ASLEEP
:
438 case HCI_IBS_TX_AWAKE
:
442 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
446 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
449 hci_uart_tx_wakeup(hu
);
452 /* Initialize protocol */
453 static int qca_open(struct hci_uart
*hu
)
455 struct qca_serdev
*qcadev
;
456 struct qca_data
*qca
;
459 BT_DBG("hu %p qca_open", hu
);
461 qca
= kzalloc(sizeof(struct qca_data
), GFP_KERNEL
);
465 skb_queue_head_init(&qca
->txq
);
466 skb_queue_head_init(&qca
->tx_wait_q
);
467 spin_lock_init(&qca
->hci_ibs_lock
);
468 qca
->workqueue
= alloc_ordered_workqueue("qca_wq", 0);
469 if (!qca
->workqueue
) {
470 BT_ERR("QCA Workqueue not initialized properly");
475 INIT_WORK(&qca
->ws_awake_rx
, qca_wq_awake_rx
);
476 INIT_WORK(&qca
->ws_awake_device
, qca_wq_awake_device
);
477 INIT_WORK(&qca
->ws_rx_vote_off
, qca_wq_serial_rx_clock_vote_off
);
478 INIT_WORK(&qca
->ws_tx_vote_off
, qca_wq_serial_tx_clock_vote_off
);
482 /* Assume we start with both sides asleep -- extra wakes OK */
483 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
484 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
486 /* clocks actually on, but we start votes off */
487 qca
->tx_vote
= false;
488 qca
->rx_vote
= false;
491 qca
->ibs_sent_wacks
= 0;
492 qca
->ibs_sent_slps
= 0;
493 qca
->ibs_sent_wakes
= 0;
494 qca
->ibs_recv_wacks
= 0;
495 qca
->ibs_recv_slps
= 0;
496 qca
->ibs_recv_wakes
= 0;
497 qca
->vote_last_jif
= jiffies
;
499 qca
->vote_off_ms
= 0;
502 qca
->tx_votes_on
= 0;
503 qca
->tx_votes_off
= 0;
504 qca
->rx_votes_on
= 0;
505 qca
->rx_votes_off
= 0;
511 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
512 if (!qca_is_wcn399x(qcadev
->btsoc_type
)) {
513 gpiod_set_value_cansleep(qcadev
->bt_en
, 1);
514 /* Controller needs time to bootup. */
517 hu
->init_speed
= qcadev
->init_speed
;
518 hu
->oper_speed
= qcadev
->oper_speed
;
519 ret
= qca_power_setup(hu
, true);
521 destroy_workqueue(qca
->workqueue
);
522 kfree_skb(qca
->rx_skb
);
530 timer_setup(&qca
->wake_retrans_timer
, hci_ibs_wake_retrans_timeout
, 0);
531 qca
->wake_retrans
= IBS_WAKE_RETRANS_TIMEOUT_MS
;
533 timer_setup(&qca
->tx_idle_timer
, hci_ibs_tx_idle_timeout
, 0);
534 qca
->tx_idle_delay
= IBS_TX_IDLE_TIMEOUT_MS
;
536 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
537 qca
->tx_idle_delay
, qca
->wake_retrans
);
542 static void qca_debugfs_init(struct hci_dev
*hdev
)
544 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
545 struct qca_data
*qca
= hu
->priv
;
546 struct dentry
*ibs_dir
;
552 ibs_dir
= debugfs_create_dir("ibs", hdev
->debugfs
);
556 debugfs_create_u8("tx_ibs_state", mode
, ibs_dir
, &qca
->tx_ibs_state
);
557 debugfs_create_u8("rx_ibs_state", mode
, ibs_dir
, &qca
->rx_ibs_state
);
558 debugfs_create_u64("ibs_sent_sleeps", mode
, ibs_dir
,
559 &qca
->ibs_sent_slps
);
560 debugfs_create_u64("ibs_sent_wakes", mode
, ibs_dir
,
561 &qca
->ibs_sent_wakes
);
562 debugfs_create_u64("ibs_sent_wake_acks", mode
, ibs_dir
,
563 &qca
->ibs_sent_wacks
);
564 debugfs_create_u64("ibs_recv_sleeps", mode
, ibs_dir
,
565 &qca
->ibs_recv_slps
);
566 debugfs_create_u64("ibs_recv_wakes", mode
, ibs_dir
,
567 &qca
->ibs_recv_wakes
);
568 debugfs_create_u64("ibs_recv_wake_acks", mode
, ibs_dir
,
569 &qca
->ibs_recv_wacks
);
570 debugfs_create_bool("tx_vote", mode
, ibs_dir
, &qca
->tx_vote
);
571 debugfs_create_u64("tx_votes_on", mode
, ibs_dir
, &qca
->tx_votes_on
);
572 debugfs_create_u64("tx_votes_off", mode
, ibs_dir
, &qca
->tx_votes_off
);
573 debugfs_create_bool("rx_vote", mode
, ibs_dir
, &qca
->rx_vote
);
574 debugfs_create_u64("rx_votes_on", mode
, ibs_dir
, &qca
->rx_votes_on
);
575 debugfs_create_u64("rx_votes_off", mode
, ibs_dir
, &qca
->rx_votes_off
);
576 debugfs_create_u64("votes_on", mode
, ibs_dir
, &qca
->votes_on
);
577 debugfs_create_u64("votes_off", mode
, ibs_dir
, &qca
->votes_off
);
578 debugfs_create_u32("vote_on_ms", mode
, ibs_dir
, &qca
->vote_on_ms
);
579 debugfs_create_u32("vote_off_ms", mode
, ibs_dir
, &qca
->vote_off_ms
);
582 mode
= S_IRUGO
| S_IWUSR
;
583 debugfs_create_u32("wake_retrans", mode
, ibs_dir
, &qca
->wake_retrans
);
584 debugfs_create_u32("tx_idle_delay", mode
, ibs_dir
,
585 &qca
->tx_idle_delay
);
588 /* Flush protocol data */
589 static int qca_flush(struct hci_uart
*hu
)
591 struct qca_data
*qca
= hu
->priv
;
593 BT_DBG("hu %p qca flush", hu
);
595 skb_queue_purge(&qca
->tx_wait_q
);
596 skb_queue_purge(&qca
->txq
);
602 static int qca_close(struct hci_uart
*hu
)
604 struct qca_serdev
*qcadev
;
605 struct qca_data
*qca
= hu
->priv
;
607 BT_DBG("hu %p qca close", hu
);
609 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE
, hu
);
611 skb_queue_purge(&qca
->tx_wait_q
);
612 skb_queue_purge(&qca
->txq
);
613 del_timer(&qca
->tx_idle_timer
);
614 del_timer(&qca
->wake_retrans_timer
);
615 destroy_workqueue(qca
->workqueue
);
619 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
620 if (qca_is_wcn399x(qcadev
->btsoc_type
))
621 qca_power_shutdown(hu
);
623 gpiod_set_value_cansleep(qcadev
->bt_en
, 0);
627 kfree_skb(qca
->rx_skb
);
636 /* Called upon a wake-up-indication from the device.
638 static void device_want_to_wakeup(struct hci_uart
*hu
)
641 struct qca_data
*qca
= hu
->priv
;
643 BT_DBG("hu %p want to wake up", hu
);
645 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
647 qca
->ibs_recv_wakes
++;
649 switch (qca
->rx_ibs_state
) {
650 case HCI_IBS_RX_ASLEEP
:
651 /* Make sure clock is on - we may have turned clock off since
652 * receiving the wake up indicator awake rx clock.
654 queue_work(qca
->workqueue
, &qca
->ws_awake_rx
);
655 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
658 case HCI_IBS_RX_AWAKE
:
659 /* Always acknowledge device wake up,
660 * sending IBS message doesn't count as TX ON.
662 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0) {
663 BT_ERR("Failed to acknowledge device wake up");
666 qca
->ibs_sent_wacks
++;
670 /* Any other state is illegal */
671 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
676 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
678 /* Actually send the packets */
679 hci_uart_tx_wakeup(hu
);
682 /* Called upon a sleep-indication from the device.
684 static void device_want_to_sleep(struct hci_uart
*hu
)
687 struct qca_data
*qca
= hu
->priv
;
689 BT_DBG("hu %p want to sleep", hu
);
691 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
693 qca
->ibs_recv_slps
++;
695 switch (qca
->rx_ibs_state
) {
696 case HCI_IBS_RX_AWAKE
:
698 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
699 /* Vote off rx clock under workqueue */
700 queue_work(qca
->workqueue
, &qca
->ws_rx_vote_off
);
703 case HCI_IBS_RX_ASLEEP
:
707 /* Any other state is illegal */
708 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
713 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
716 /* Called upon wake-up-acknowledgement from the device
718 static void device_woke_up(struct hci_uart
*hu
)
720 unsigned long flags
, idle_delay
;
721 struct qca_data
*qca
= hu
->priv
;
722 struct sk_buff
*skb
= NULL
;
724 BT_DBG("hu %p woke up", hu
);
726 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
728 qca
->ibs_recv_wacks
++;
730 switch (qca
->tx_ibs_state
) {
731 case HCI_IBS_TX_AWAKE
:
732 /* Expect one if we send 2 WAKEs */
733 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
737 case HCI_IBS_TX_WAKING
:
738 /* Send pending packets */
739 while ((skb
= skb_dequeue(&qca
->tx_wait_q
)))
740 skb_queue_tail(&qca
->txq
, skb
);
742 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
743 del_timer(&qca
->wake_retrans_timer
);
744 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
745 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
746 qca
->tx_ibs_state
= HCI_IBS_TX_AWAKE
;
749 case HCI_IBS_TX_ASLEEP
:
753 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
758 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
760 /* Actually send the packets */
761 hci_uart_tx_wakeup(hu
);
764 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
765 * two simultaneous tasklets.
767 static int qca_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
769 unsigned long flags
= 0, idle_delay
;
770 struct qca_data
*qca
= hu
->priv
;
772 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu
, skb
,
775 /* Prepend skb with frame type */
776 memcpy(skb_push(skb
, 1), &hci_skb_pkt_type(skb
), 1);
778 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
780 /* Don't go to sleep in middle of patch download or
781 * Out-Of-Band(GPIOs control) sleep is selected.
783 if (!test_bit(QCA_IBS_ENABLED
, &qca
->flags
)) {
784 skb_queue_tail(&qca
->txq
, skb
);
785 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
789 /* Act according to current state */
790 switch (qca
->tx_ibs_state
) {
791 case HCI_IBS_TX_AWAKE
:
792 BT_DBG("Device awake, sending normally");
793 skb_queue_tail(&qca
->txq
, skb
);
794 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
795 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
798 case HCI_IBS_TX_ASLEEP
:
799 BT_DBG("Device asleep, waking up and queueing packet");
800 /* Save packet for later */
801 skb_queue_tail(&qca
->tx_wait_q
, skb
);
803 qca
->tx_ibs_state
= HCI_IBS_TX_WAKING
;
804 /* Schedule a work queue to wake up device */
805 queue_work(qca
->workqueue
, &qca
->ws_awake_device
);
808 case HCI_IBS_TX_WAKING
:
809 BT_DBG("Device waking up, queueing packet");
810 /* Transient state; just keep packet for later */
811 skb_queue_tail(&qca
->tx_wait_q
, skb
);
815 BT_ERR("Illegal tx state: %d (losing packet)",
821 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
826 static int qca_ibs_sleep_ind(struct hci_dev
*hdev
, struct sk_buff
*skb
)
828 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
830 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_SLEEP_IND
);
832 device_want_to_sleep(hu
);
838 static int qca_ibs_wake_ind(struct hci_dev
*hdev
, struct sk_buff
*skb
)
840 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
842 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_WAKE_IND
);
844 device_want_to_wakeup(hu
);
850 static int qca_ibs_wake_ack(struct hci_dev
*hdev
, struct sk_buff
*skb
)
852 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
854 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_WAKE_ACK
);
862 static int qca_recv_acl_data(struct hci_dev
*hdev
, struct sk_buff
*skb
)
864 /* We receive debug logs from chip as an ACL packets.
865 * Instead of sending the data to ACL to decode the
866 * received data, we are pushing them to the above layers
867 * as a diagnostic packet.
869 if (get_unaligned_le16(skb
->data
) == QCA_DEBUG_HANDLE
)
870 return hci_recv_diag(hdev
, skb
);
872 return hci_recv_frame(hdev
, skb
);
875 #define QCA_IBS_SLEEP_IND_EVENT \
876 .type = HCI_IBS_SLEEP_IND, \
880 .maxlen = HCI_MAX_IBS_SIZE
882 #define QCA_IBS_WAKE_IND_EVENT \
883 .type = HCI_IBS_WAKE_IND, \
887 .maxlen = HCI_MAX_IBS_SIZE
889 #define QCA_IBS_WAKE_ACK_EVENT \
890 .type = HCI_IBS_WAKE_ACK, \
894 .maxlen = HCI_MAX_IBS_SIZE
896 static const struct h4_recv_pkt qca_recv_pkts
[] = {
897 { H4_RECV_ACL
, .recv
= qca_recv_acl_data
},
898 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
899 { H4_RECV_EVENT
, .recv
= hci_recv_frame
},
900 { QCA_IBS_WAKE_IND_EVENT
, .recv
= qca_ibs_wake_ind
},
901 { QCA_IBS_WAKE_ACK_EVENT
, .recv
= qca_ibs_wake_ack
},
902 { QCA_IBS_SLEEP_IND_EVENT
, .recv
= qca_ibs_sleep_ind
},
905 static int qca_recv(struct hci_uart
*hu
, const void *data
, int count
)
907 struct qca_data
*qca
= hu
->priv
;
909 if (!test_bit(HCI_UART_REGISTERED
, &hu
->flags
))
912 qca
->rx_skb
= h4_recv_buf(hu
->hdev
, qca
->rx_skb
, data
, count
,
913 qca_recv_pkts
, ARRAY_SIZE(qca_recv_pkts
));
914 if (IS_ERR(qca
->rx_skb
)) {
915 int err
= PTR_ERR(qca
->rx_skb
);
916 bt_dev_err(hu
->hdev
, "Frame reassembly failed (%d)", err
);
924 static struct sk_buff
*qca_dequeue(struct hci_uart
*hu
)
926 struct qca_data
*qca
= hu
->priv
;
928 return skb_dequeue(&qca
->txq
);
931 static uint8_t qca_get_baudrate_value(int speed
)
935 return QCA_BAUDRATE_9600
;
937 return QCA_BAUDRATE_19200
;
939 return QCA_BAUDRATE_38400
;
941 return QCA_BAUDRATE_57600
;
943 return QCA_BAUDRATE_115200
;
945 return QCA_BAUDRATE_230400
;
947 return QCA_BAUDRATE_460800
;
949 return QCA_BAUDRATE_500000
;
951 return QCA_BAUDRATE_921600
;
953 return QCA_BAUDRATE_1000000
;
955 return QCA_BAUDRATE_2000000
;
957 return QCA_BAUDRATE_3000000
;
959 return QCA_BAUDRATE_3200000
;
961 return QCA_BAUDRATE_3500000
;
963 return QCA_BAUDRATE_115200
;
967 static int qca_set_baudrate(struct hci_dev
*hdev
, uint8_t baudrate
)
969 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
970 struct qca_data
*qca
= hu
->priv
;
972 u8 cmd
[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
974 if (baudrate
> QCA_BAUDRATE_3200000
)
979 skb
= bt_skb_alloc(sizeof(cmd
), GFP_KERNEL
);
981 bt_dev_err(hdev
, "Failed to allocate baudrate packet");
985 /* Assign commands to change baudrate and packet type. */
986 skb_put_data(skb
, cmd
, sizeof(cmd
));
987 hci_skb_pkt_type(skb
) = HCI_COMMAND_PKT
;
989 skb_queue_tail(&qca
->txq
, skb
);
990 hci_uart_tx_wakeup(hu
);
992 /* Wait for the baudrate change request to be sent */
994 while (!skb_queue_empty(&qca
->txq
))
995 usleep_range(100, 200);
998 serdev_device_wait_until_sent(hu
->serdev
,
999 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS
));
1001 /* Give the controller time to process the request */
1002 if (qca_is_wcn399x(qca_soc_type(hu
)))
1010 static inline void host_set_baudrate(struct hci_uart
*hu
, unsigned int speed
)
1013 serdev_device_set_baudrate(hu
->serdev
, speed
);
1015 hci_uart_set_baudrate(hu
, speed
);
1018 static int qca_send_power_pulse(struct hci_uart
*hu
, bool on
)
1021 int timeout
= msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS
);
1022 u8 cmd
= on
? QCA_WCN3990_POWERON_PULSE
: QCA_WCN3990_POWEROFF_PULSE
;
1024 /* These power pulses are single byte command which are sent
1025 * at required baudrate to wcn3990. On wcn3990, we have an external
1026 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1027 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1028 * and also we use the same power inputs to turn on and off for
1029 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1030 * we send a power on pulse at 115200 bps. This algorithm will help to
1031 * save power. Disabling hardware flow control is mandatory while
1032 * sending power pulses to SoC.
1034 bt_dev_dbg(hu
->hdev
, "sending power pulse %02x to controller", cmd
);
1036 serdev_device_write_flush(hu
->serdev
);
1037 hci_uart_set_flow_control(hu
, true);
1038 ret
= serdev_device_write_buf(hu
->serdev
, &cmd
, sizeof(cmd
));
1040 bt_dev_err(hu
->hdev
, "failed to send power pulse %02x", cmd
);
1044 serdev_device_wait_until_sent(hu
->serdev
, timeout
);
1045 hci_uart_set_flow_control(hu
, false);
1047 /* Give to controller time to boot/shutdown */
1056 static unsigned int qca_get_speed(struct hci_uart
*hu
,
1057 enum qca_speed_type speed_type
)
1059 unsigned int speed
= 0;
1061 if (speed_type
== QCA_INIT_SPEED
) {
1063 speed
= hu
->init_speed
;
1064 else if (hu
->proto
->init_speed
)
1065 speed
= hu
->proto
->init_speed
;
1068 speed
= hu
->oper_speed
;
1069 else if (hu
->proto
->oper_speed
)
1070 speed
= hu
->proto
->oper_speed
;
1076 static int qca_check_speeds(struct hci_uart
*hu
)
1078 if (qca_is_wcn399x(qca_soc_type(hu
))) {
1079 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) &&
1080 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1083 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) ||
1084 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1091 static int qca_set_speed(struct hci_uart
*hu
, enum qca_speed_type speed_type
)
1093 unsigned int speed
, qca_baudrate
;
1096 if (speed_type
== QCA_INIT_SPEED
) {
1097 speed
= qca_get_speed(hu
, QCA_INIT_SPEED
);
1099 host_set_baudrate(hu
, speed
);
1101 enum qca_btsoc_type soc_type
= qca_soc_type(hu
);
1103 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1107 /* Disable flow control for wcn3990 to deassert RTS while
1108 * changing the baudrate of chip and host.
1110 if (qca_is_wcn399x(soc_type
))
1111 hci_uart_set_flow_control(hu
, true);
1113 qca_baudrate
= qca_get_baudrate_value(speed
);
1114 bt_dev_dbg(hu
->hdev
, "Set UART speed to %d", speed
);
1115 ret
= qca_set_baudrate(hu
->hdev
, qca_baudrate
);
1119 host_set_baudrate(hu
, speed
);
1122 if (qca_is_wcn399x(soc_type
))
1123 hci_uart_set_flow_control(hu
, false);
1129 static int qca_wcn3990_init(struct hci_uart
*hu
)
1131 struct qca_serdev
*qcadev
;
1134 /* Check for vregs status, may be hci down has turned
1135 * off the voltage regulator.
1137 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1138 if (!qcadev
->bt_power
->vregs_on
) {
1139 serdev_device_close(hu
->serdev
);
1140 ret
= qca_power_setup(hu
, true);
1144 ret
= serdev_device_open(hu
->serdev
);
1146 bt_dev_err(hu
->hdev
, "failed to open port");
1151 /* Forcefully enable wcn3990 to enter in to boot mode. */
1152 host_set_baudrate(hu
, 2400);
1153 ret
= qca_send_power_pulse(hu
, false);
1157 qca_set_speed(hu
, QCA_INIT_SPEED
);
1158 ret
= qca_send_power_pulse(hu
, true);
1162 /* Now the device is in ready state to communicate with host.
1163 * To sync host with device we need to reopen port.
1164 * Without this, we will have RTS and CTS synchronization
1167 serdev_device_close(hu
->serdev
);
1168 ret
= serdev_device_open(hu
->serdev
);
1170 bt_dev_err(hu
->hdev
, "failed to open port");
1174 hci_uart_set_flow_control(hu
, false);
1179 static int qca_setup(struct hci_uart
*hu
)
1181 struct hci_dev
*hdev
= hu
->hdev
;
1182 struct qca_data
*qca
= hu
->priv
;
1183 unsigned int speed
, qca_baudrate
= QCA_BAUDRATE_115200
;
1184 enum qca_btsoc_type soc_type
= qca_soc_type(hu
);
1188 ret
= qca_check_speeds(hu
);
1192 /* Patch downloading has to be done without IBS mode */
1193 clear_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1195 if (qca_is_wcn399x(soc_type
)) {
1196 bt_dev_info(hdev
, "setting up wcn3990");
1198 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1199 * setup for every hci up.
1201 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP
, &hdev
->quirks
);
1202 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY
, &hdev
->quirks
);
1203 hu
->hdev
->shutdown
= qca_power_off
;
1204 ret
= qca_wcn3990_init(hu
);
1208 ret
= qca_read_soc_version(hdev
, &soc_ver
);
1212 bt_dev_info(hdev
, "ROME setup");
1213 qca_set_speed(hu
, QCA_INIT_SPEED
);
1216 /* Setup user speed if needed */
1217 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1219 ret
= qca_set_speed(hu
, QCA_OPER_SPEED
);
1223 qca_baudrate
= qca_get_baudrate_value(speed
);
1226 if (!qca_is_wcn399x(soc_type
)) {
1227 /* Get QCA version information */
1228 ret
= qca_read_soc_version(hdev
, &soc_ver
);
1233 bt_dev_info(hdev
, "QCA controller version 0x%08x", soc_ver
);
1234 /* Setup patch / NVM configurations */
1235 ret
= qca_uart_setup(hdev
, qca_baudrate
, soc_type
, soc_ver
);
1237 set_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1238 qca_debugfs_init(hdev
);
1239 } else if (ret
== -ENOENT
) {
1240 /* No patch/nvm-config found, run with original fw/config */
1242 } else if (ret
== -EAGAIN
) {
1244 * Userspace firmware loader will return -EAGAIN in case no
1245 * patch/nvm-config is found, so run with original fw/config.
1251 if (qca_is_wcn399x(soc_type
))
1252 hu
->hdev
->set_bdaddr
= qca_set_bdaddr
;
1254 hu
->hdev
->set_bdaddr
= qca_set_bdaddr_rome
;
1259 static struct hci_uart_proto qca_proto
= {
1263 .init_speed
= 115200,
1264 .oper_speed
= 3000000,
1270 .enqueue
= qca_enqueue
,
1271 .dequeue
= qca_dequeue
,
1274 static const struct qca_vreg_data qca_soc_data_wcn3990
= {
1275 .soc_type
= QCA_WCN3990
,
1276 .vregs
= (struct qca_vreg
[]) {
1277 { "vddio", 1800000, 1900000, 15000 },
1278 { "vddxo", 1800000, 1900000, 80000 },
1279 { "vddrf", 1300000, 1350000, 300000 },
1280 { "vddch0", 3300000, 3400000, 450000 },
1285 static const struct qca_vreg_data qca_soc_data_wcn3998
= {
1286 .soc_type
= QCA_WCN3998
,
1287 .vregs
= (struct qca_vreg
[]) {
1288 { "vddio", 1800000, 1900000, 10000 },
1289 { "vddxo", 1800000, 1900000, 80000 },
1290 { "vddrf", 1300000, 1352000, 300000 },
1291 { "vddch0", 3300000, 3300000, 450000 },
1296 static void qca_power_shutdown(struct hci_uart
*hu
)
1298 struct qca_data
*qca
= hu
->priv
;
1299 unsigned long flags
;
1301 /* From this point we go into power off state. But serial port is
1302 * still open, stop queueing the IBS data and flush all the buffered
1305 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
1306 clear_bit(QCA_IBS_ENABLED
, &qca
->flags
);
1308 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
1310 host_set_baudrate(hu
, 2400);
1311 qca_send_power_pulse(hu
, false);
1312 qca_power_setup(hu
, false);
1315 static int qca_power_off(struct hci_dev
*hdev
)
1317 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1319 qca_power_shutdown(hu
);
1323 static int qca_enable_regulator(struct qca_vreg vregs
,
1324 struct regulator
*regulator
)
1328 ret
= regulator_set_voltage(regulator
, vregs
.min_uV
,
1334 ret
= regulator_set_load(regulator
,
1340 return regulator_enable(regulator
);
1344 static void qca_disable_regulator(struct qca_vreg vregs
,
1345 struct regulator
*regulator
)
1347 regulator_disable(regulator
);
1348 regulator_set_voltage(regulator
, 0, vregs
.max_uV
);
1350 regulator_set_load(regulator
, 0);
1354 static int qca_power_setup(struct hci_uart
*hu
, bool on
)
1356 struct qca_vreg
*vregs
;
1357 struct regulator_bulk_data
*vreg_bulk
;
1358 struct qca_serdev
*qcadev
;
1359 int i
, num_vregs
, ret
= 0;
1361 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1362 if (!qcadev
|| !qcadev
->bt_power
|| !qcadev
->bt_power
->vreg_data
||
1363 !qcadev
->bt_power
->vreg_bulk
)
1366 vregs
= qcadev
->bt_power
->vreg_data
->vregs
;
1367 vreg_bulk
= qcadev
->bt_power
->vreg_bulk
;
1368 num_vregs
= qcadev
->bt_power
->vreg_data
->num_vregs
;
1369 BT_DBG("on: %d", on
);
1370 if (on
&& !qcadev
->bt_power
->vregs_on
) {
1371 for (i
= 0; i
< num_vregs
; i
++) {
1372 ret
= qca_enable_regulator(vregs
[i
],
1373 vreg_bulk
[i
].consumer
);
1379 BT_ERR("failed to enable regulator:%s", vregs
[i
].name
);
1380 /* turn off regulators which are enabled */
1381 for (i
= i
- 1; i
>= 0; i
--)
1382 qca_disable_regulator(vregs
[i
],
1383 vreg_bulk
[i
].consumer
);
1385 qcadev
->bt_power
->vregs_on
= true;
1387 } else if (!on
&& qcadev
->bt_power
->vregs_on
) {
1388 /* turn off regulator in reverse order */
1389 i
= qcadev
->bt_power
->vreg_data
->num_vregs
- 1;
1390 for ( ; i
>= 0; i
--)
1391 qca_disable_regulator(vregs
[i
], vreg_bulk
[i
].consumer
);
1393 qcadev
->bt_power
->vregs_on
= false;
1399 static int qca_init_regulators(struct qca_power
*qca
,
1400 const struct qca_vreg
*vregs
, size_t num_vregs
)
1404 qca
->vreg_bulk
= devm_kcalloc(qca
->dev
, num_vregs
,
1405 sizeof(struct regulator_bulk_data
),
1407 if (!qca
->vreg_bulk
)
1410 for (i
= 0; i
< num_vregs
; i
++)
1411 qca
->vreg_bulk
[i
].supply
= vregs
[i
].name
;
1413 return devm_regulator_bulk_get(qca
->dev
, num_vregs
, qca
->vreg_bulk
);
1416 static int qca_serdev_probe(struct serdev_device
*serdev
)
1418 struct qca_serdev
*qcadev
;
1419 const struct qca_vreg_data
*data
;
1422 qcadev
= devm_kzalloc(&serdev
->dev
, sizeof(*qcadev
), GFP_KERNEL
);
1426 qcadev
->serdev_hu
.serdev
= serdev
;
1427 data
= of_device_get_match_data(&serdev
->dev
);
1428 serdev_device_set_drvdata(serdev
, qcadev
);
1429 if (data
&& qca_is_wcn399x(data
->soc_type
)) {
1430 qcadev
->btsoc_type
= data
->soc_type
;
1431 qcadev
->bt_power
= devm_kzalloc(&serdev
->dev
,
1432 sizeof(struct qca_power
),
1434 if (!qcadev
->bt_power
)
1437 qcadev
->bt_power
->dev
= &serdev
->dev
;
1438 qcadev
->bt_power
->vreg_data
= data
;
1439 err
= qca_init_regulators(qcadev
->bt_power
, data
->vregs
,
1442 BT_ERR("Failed to init regulators:%d", err
);
1446 qcadev
->bt_power
->vregs_on
= false;
1448 device_property_read_u32(&serdev
->dev
, "max-speed",
1449 &qcadev
->oper_speed
);
1450 if (!qcadev
->oper_speed
)
1451 BT_DBG("UART will pick default operating speed");
1453 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1455 BT_ERR("wcn3990 serdev registration failed");
1459 qcadev
->btsoc_type
= QCA_ROME
;
1460 qcadev
->bt_en
= devm_gpiod_get(&serdev
->dev
, "enable",
1462 if (IS_ERR(qcadev
->bt_en
)) {
1463 dev_err(&serdev
->dev
, "failed to acquire enable gpio\n");
1464 return PTR_ERR(qcadev
->bt_en
);
1467 qcadev
->susclk
= devm_clk_get(&serdev
->dev
, NULL
);
1468 if (IS_ERR(qcadev
->susclk
)) {
1469 dev_err(&serdev
->dev
, "failed to acquire clk\n");
1470 return PTR_ERR(qcadev
->susclk
);
1473 err
= clk_set_rate(qcadev
->susclk
, SUSCLK_RATE_32KHZ
);
1477 err
= clk_prepare_enable(qcadev
->susclk
);
1481 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1483 clk_disable_unprepare(qcadev
->susclk
);
1490 static void qca_serdev_remove(struct serdev_device
*serdev
)
1492 struct qca_serdev
*qcadev
= serdev_device_get_drvdata(serdev
);
1494 if (qca_is_wcn399x(qcadev
->btsoc_type
))
1495 qca_power_shutdown(&qcadev
->serdev_hu
);
1497 clk_disable_unprepare(qcadev
->susclk
);
1499 hci_uart_unregister_device(&qcadev
->serdev_hu
);
1502 static const struct of_device_id qca_bluetooth_of_match
[] = {
1503 { .compatible
= "qcom,qca6174-bt" },
1504 { .compatible
= "qcom,wcn3990-bt", .data
= &qca_soc_data_wcn3990
},
1505 { .compatible
= "qcom,wcn3998-bt", .data
= &qca_soc_data_wcn3998
},
1508 MODULE_DEVICE_TABLE(of
, qca_bluetooth_of_match
);
1510 static struct serdev_device_driver qca_serdev_driver
= {
1511 .probe
= qca_serdev_probe
,
1512 .remove
= qca_serdev_remove
,
1514 .name
= "hci_uart_qca",
1515 .of_match_table
= qca_bluetooth_of_match
,
1519 int __init
qca_init(void)
1521 serdev_device_driver_register(&qca_serdev_driver
);
1523 return hci_uart_register_proto(&qca_proto
);
1526 int __exit
qca_deinit(void)
1528 serdev_device_driver_unregister(&qca_serdev_driver
);
1530 return hci_uart_unregister_proto(&qca_proto
);