2 * Bluetooth Software UART Qualcomm protocol
4 * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management
5 * protocol extension to H4.
7 * Copyright (C) 2007 Texas Instruments, Inc.
8 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
11 * This file is based on hci_ll.c, which was...
12 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
13 * which was in turn based on hci_h4.c, which was written
14 * by Maxim Krasnyansky and Marcel Holtmann.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <linux/kernel.h>
32 #include <linux/clk.h>
33 #include <linux/debugfs.h>
34 #include <linux/delay.h>
35 #include <linux/device.h>
36 #include <linux/gpio/consumer.h>
37 #include <linux/mod_devicetable.h>
38 #include <linux/module.h>
39 #include <linux/of_device.h>
40 #include <linux/platform_device.h>
41 #include <linux/regulator/consumer.h>
42 #include <linux/serdev.h>
43 #include <asm/unaligned.h>
45 #include <net/bluetooth/bluetooth.h>
46 #include <net/bluetooth/hci_core.h>
51 /* HCI_IBS protocol messages */
52 #define HCI_IBS_SLEEP_IND 0xFE
53 #define HCI_IBS_WAKE_IND 0xFD
54 #define HCI_IBS_WAKE_ACK 0xFC
55 #define HCI_MAX_IBS_SIZE 10
57 /* Controller states */
58 #define STATE_IN_BAND_SLEEP_ENABLED 1
60 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
61 #define IBS_TX_IDLE_TIMEOUT_MS 2000
62 #define CMD_TRANS_TIMEOUT_MS 100
65 #define SUSCLK_RATE_32KHZ 32768
67 /* Controller debug log header */
68 #define QCA_DEBUG_HANDLE 0x2EDC
70 /* HCI_IBS transmit side sleep protocol states */
77 /* HCI_IBS receive side sleep protocol states */
83 /* HCI_IBS transmit and receive side clock state vote */
84 enum hci_ibs_clock_state_vote
{
85 HCI_IBS_VOTE_STATS_UPDATE
,
86 HCI_IBS_TX_VOTE_CLOCK_ON
,
87 HCI_IBS_TX_VOTE_CLOCK_OFF
,
88 HCI_IBS_RX_VOTE_CLOCK_ON
,
89 HCI_IBS_RX_VOTE_CLOCK_OFF
,
94 struct sk_buff
*rx_skb
;
95 struct sk_buff_head txq
;
96 struct sk_buff_head tx_wait_q
; /* HCI_IBS wait queue */
97 spinlock_t hci_ibs_lock
; /* HCI_IBS state lock */
98 u8 tx_ibs_state
; /* HCI_IBS transmit side power state*/
99 u8 rx_ibs_state
; /* HCI_IBS receive side power state */
100 bool tx_vote
; /* Clock must be on for TX */
101 bool rx_vote
; /* Clock must be on for RX */
102 struct timer_list tx_idle_timer
;
104 struct timer_list wake_retrans_timer
;
106 struct workqueue_struct
*workqueue
;
107 struct work_struct ws_awake_rx
;
108 struct work_struct ws_awake_device
;
109 struct work_struct ws_rx_vote_off
;
110 struct work_struct ws_tx_vote_off
;
113 /* For debugging purpose */
131 enum qca_speed_type
{
137 * Voltage regulator information required for configuring the
138 * QCA Bluetooth chipset
144 unsigned int load_uA
;
147 struct qca_vreg_data
{
148 enum qca_btsoc_type soc_type
;
149 struct qca_vreg
*vregs
;
154 * Platform data for the QCA Bluetooth power driver.
158 const struct qca_vreg_data
*vreg_data
;
159 struct regulator_bulk_data
*vreg_bulk
;
164 struct hci_uart serdev_hu
;
165 struct gpio_desc
*bt_en
;
167 enum qca_btsoc_type btsoc_type
;
168 struct qca_power
*bt_power
;
173 static int qca_power_setup(struct hci_uart
*hu
, bool on
);
174 static void qca_power_shutdown(struct hci_uart
*hu
);
175 static int qca_power_off(struct hci_dev
*hdev
);
177 static void __serial_clock_on(struct tty_struct
*tty
)
179 /* TODO: Some chipset requires to enable UART clock on client
180 * side to save power consumption or manual work is required.
181 * Please put your code to control UART clock here if needed
185 static void __serial_clock_off(struct tty_struct
*tty
)
187 /* TODO: Some chipset requires to disable UART clock on client
188 * side to save power consumption or manual work is required.
189 * Please put your code to control UART clock off here if needed
193 /* serial_clock_vote needs to be called with the ibs lock held */
194 static void serial_clock_vote(unsigned long vote
, struct hci_uart
*hu
)
196 struct qca_data
*qca
= hu
->priv
;
199 bool old_vote
= (qca
->tx_vote
| qca
->rx_vote
);
203 case HCI_IBS_VOTE_STATS_UPDATE
:
204 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
207 qca
->vote_off_ms
+= diff
;
209 qca
->vote_on_ms
+= diff
;
212 case HCI_IBS_TX_VOTE_CLOCK_ON
:
218 case HCI_IBS_RX_VOTE_CLOCK_ON
:
224 case HCI_IBS_TX_VOTE_CLOCK_OFF
:
225 qca
->tx_vote
= false;
227 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
230 case HCI_IBS_RX_VOTE_CLOCK_OFF
:
231 qca
->rx_vote
= false;
233 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
237 BT_ERR("Voting irregularity");
241 if (new_vote
!= old_vote
) {
243 __serial_clock_on(hu
->tty
);
245 __serial_clock_off(hu
->tty
);
247 BT_DBG("Vote serial clock %s(%s)", new_vote
? "true" : "false",
248 vote
? "true" : "false");
250 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
254 qca
->vote_off_ms
+= diff
;
257 qca
->vote_on_ms
+= diff
;
259 qca
->vote_last_jif
= jiffies
;
263 /* Builds and sends an HCI_IBS command packet.
264 * These are very simple packets with only 1 cmd byte.
266 static int send_hci_ibs_cmd(u8 cmd
, struct hci_uart
*hu
)
269 struct sk_buff
*skb
= NULL
;
270 struct qca_data
*qca
= hu
->priv
;
272 BT_DBG("hu %p send hci ibs cmd 0x%x", hu
, cmd
);
274 skb
= bt_skb_alloc(1, GFP_ATOMIC
);
276 BT_ERR("Failed to allocate memory for HCI_IBS packet");
280 /* Assign HCI_IBS type */
281 skb_put_u8(skb
, cmd
);
283 skb_queue_tail(&qca
->txq
, skb
);
288 static void qca_wq_awake_device(struct work_struct
*work
)
290 struct qca_data
*qca
= container_of(work
, struct qca_data
,
292 struct hci_uart
*hu
= qca
->hu
;
293 unsigned long retrans_delay
;
295 BT_DBG("hu %p wq awake device", hu
);
297 /* Vote for serial clock */
298 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON
, hu
);
300 spin_lock(&qca
->hci_ibs_lock
);
302 /* Send wake indication to device */
303 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0)
304 BT_ERR("Failed to send WAKE to device");
306 qca
->ibs_sent_wakes
++;
308 /* Start retransmit timer */
309 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
310 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
312 spin_unlock(&qca
->hci_ibs_lock
);
314 /* Actually send the packets */
315 hci_uart_tx_wakeup(hu
);
318 static void qca_wq_awake_rx(struct work_struct
*work
)
320 struct qca_data
*qca
= container_of(work
, struct qca_data
,
322 struct hci_uart
*hu
= qca
->hu
;
324 BT_DBG("hu %p wq awake rx", hu
);
326 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON
, hu
);
328 spin_lock(&qca
->hci_ibs_lock
);
329 qca
->rx_ibs_state
= HCI_IBS_RX_AWAKE
;
331 /* Always acknowledge device wake up,
332 * sending IBS message doesn't count as TX ON.
334 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0)
335 BT_ERR("Failed to acknowledge device wake up");
337 qca
->ibs_sent_wacks
++;
339 spin_unlock(&qca
->hci_ibs_lock
);
341 /* Actually send the packets */
342 hci_uart_tx_wakeup(hu
);
345 static void qca_wq_serial_rx_clock_vote_off(struct work_struct
*work
)
347 struct qca_data
*qca
= container_of(work
, struct qca_data
,
349 struct hci_uart
*hu
= qca
->hu
;
351 BT_DBG("hu %p rx clock vote off", hu
);
353 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF
, hu
);
356 static void qca_wq_serial_tx_clock_vote_off(struct work_struct
*work
)
358 struct qca_data
*qca
= container_of(work
, struct qca_data
,
360 struct hci_uart
*hu
= qca
->hu
;
362 BT_DBG("hu %p tx clock vote off", hu
);
364 /* Run HCI tx handling unlocked */
365 hci_uart_tx_wakeup(hu
);
367 /* Now that message queued to tty driver, vote for tty clocks off.
368 * It is up to the tty driver to pend the clocks off until tx done.
370 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF
, hu
);
373 static void hci_ibs_tx_idle_timeout(struct timer_list
*t
)
375 struct qca_data
*qca
= from_timer(qca
, t
, tx_idle_timer
);
376 struct hci_uart
*hu
= qca
->hu
;
379 BT_DBG("hu %p idle timeout in %d state", hu
, qca
->tx_ibs_state
);
381 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
382 flags
, SINGLE_DEPTH_NESTING
);
384 switch (qca
->tx_ibs_state
) {
385 case HCI_IBS_TX_AWAKE
:
386 /* TX_IDLE, go to SLEEP */
387 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND
, hu
) < 0) {
388 BT_ERR("Failed to send SLEEP to device");
391 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
392 qca
->ibs_sent_slps
++;
393 queue_work(qca
->workqueue
, &qca
->ws_tx_vote_off
);
396 case HCI_IBS_TX_ASLEEP
:
397 case HCI_IBS_TX_WAKING
:
401 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
405 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
408 static void hci_ibs_wake_retrans_timeout(struct timer_list
*t
)
410 struct qca_data
*qca
= from_timer(qca
, t
, wake_retrans_timer
);
411 struct hci_uart
*hu
= qca
->hu
;
412 unsigned long flags
, retrans_delay
;
413 bool retransmit
= false;
415 BT_DBG("hu %p wake retransmit timeout in %d state",
416 hu
, qca
->tx_ibs_state
);
418 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
419 flags
, SINGLE_DEPTH_NESTING
);
421 switch (qca
->tx_ibs_state
) {
422 case HCI_IBS_TX_WAKING
:
423 /* No WAKE_ACK, retransmit WAKE */
425 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0) {
426 BT_ERR("Failed to acknowledge device wake up");
429 qca
->ibs_sent_wakes
++;
430 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
431 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
434 case HCI_IBS_TX_ASLEEP
:
435 case HCI_IBS_TX_AWAKE
:
439 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
443 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
446 hci_uart_tx_wakeup(hu
);
449 /* Initialize protocol */
450 static int qca_open(struct hci_uart
*hu
)
452 struct qca_serdev
*qcadev
;
453 struct qca_data
*qca
;
456 BT_DBG("hu %p qca_open", hu
);
458 qca
= kzalloc(sizeof(struct qca_data
), GFP_KERNEL
);
462 skb_queue_head_init(&qca
->txq
);
463 skb_queue_head_init(&qca
->tx_wait_q
);
464 spin_lock_init(&qca
->hci_ibs_lock
);
465 qca
->workqueue
= alloc_ordered_workqueue("qca_wq", 0);
466 if (!qca
->workqueue
) {
467 BT_ERR("QCA Workqueue not initialized properly");
472 INIT_WORK(&qca
->ws_awake_rx
, qca_wq_awake_rx
);
473 INIT_WORK(&qca
->ws_awake_device
, qca_wq_awake_device
);
474 INIT_WORK(&qca
->ws_rx_vote_off
, qca_wq_serial_rx_clock_vote_off
);
475 INIT_WORK(&qca
->ws_tx_vote_off
, qca_wq_serial_tx_clock_vote_off
);
479 /* Assume we start with both sides asleep -- extra wakes OK */
480 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
481 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
483 /* clocks actually on, but we start votes off */
484 qca
->tx_vote
= false;
485 qca
->rx_vote
= false;
488 qca
->ibs_sent_wacks
= 0;
489 qca
->ibs_sent_slps
= 0;
490 qca
->ibs_sent_wakes
= 0;
491 qca
->ibs_recv_wacks
= 0;
492 qca
->ibs_recv_slps
= 0;
493 qca
->ibs_recv_wakes
= 0;
494 qca
->vote_last_jif
= jiffies
;
496 qca
->vote_off_ms
= 0;
499 qca
->tx_votes_on
= 0;
500 qca
->tx_votes_off
= 0;
501 qca
->rx_votes_on
= 0;
502 qca
->rx_votes_off
= 0;
508 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
509 if (qcadev
->btsoc_type
!= QCA_WCN3990
) {
510 gpiod_set_value_cansleep(qcadev
->bt_en
, 1);
511 /* Controller needs time to bootup. */
514 hu
->init_speed
= qcadev
->init_speed
;
515 hu
->oper_speed
= qcadev
->oper_speed
;
516 ret
= qca_power_setup(hu
, true);
518 destroy_workqueue(qca
->workqueue
);
519 kfree_skb(qca
->rx_skb
);
527 timer_setup(&qca
->wake_retrans_timer
, hci_ibs_wake_retrans_timeout
, 0);
528 qca
->wake_retrans
= IBS_WAKE_RETRANS_TIMEOUT_MS
;
530 timer_setup(&qca
->tx_idle_timer
, hci_ibs_tx_idle_timeout
, 0);
531 qca
->tx_idle_delay
= IBS_TX_IDLE_TIMEOUT_MS
;
533 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
534 qca
->tx_idle_delay
, qca
->wake_retrans
);
539 static void qca_debugfs_init(struct hci_dev
*hdev
)
541 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
542 struct qca_data
*qca
= hu
->priv
;
543 struct dentry
*ibs_dir
;
549 ibs_dir
= debugfs_create_dir("ibs", hdev
->debugfs
);
553 debugfs_create_u8("tx_ibs_state", mode
, ibs_dir
, &qca
->tx_ibs_state
);
554 debugfs_create_u8("rx_ibs_state", mode
, ibs_dir
, &qca
->rx_ibs_state
);
555 debugfs_create_u64("ibs_sent_sleeps", mode
, ibs_dir
,
556 &qca
->ibs_sent_slps
);
557 debugfs_create_u64("ibs_sent_wakes", mode
, ibs_dir
,
558 &qca
->ibs_sent_wakes
);
559 debugfs_create_u64("ibs_sent_wake_acks", mode
, ibs_dir
,
560 &qca
->ibs_sent_wacks
);
561 debugfs_create_u64("ibs_recv_sleeps", mode
, ibs_dir
,
562 &qca
->ibs_recv_slps
);
563 debugfs_create_u64("ibs_recv_wakes", mode
, ibs_dir
,
564 &qca
->ibs_recv_wakes
);
565 debugfs_create_u64("ibs_recv_wake_acks", mode
, ibs_dir
,
566 &qca
->ibs_recv_wacks
);
567 debugfs_create_bool("tx_vote", mode
, ibs_dir
, &qca
->tx_vote
);
568 debugfs_create_u64("tx_votes_on", mode
, ibs_dir
, &qca
->tx_votes_on
);
569 debugfs_create_u64("tx_votes_off", mode
, ibs_dir
, &qca
->tx_votes_off
);
570 debugfs_create_bool("rx_vote", mode
, ibs_dir
, &qca
->rx_vote
);
571 debugfs_create_u64("rx_votes_on", mode
, ibs_dir
, &qca
->rx_votes_on
);
572 debugfs_create_u64("rx_votes_off", mode
, ibs_dir
, &qca
->rx_votes_off
);
573 debugfs_create_u64("votes_on", mode
, ibs_dir
, &qca
->votes_on
);
574 debugfs_create_u64("votes_off", mode
, ibs_dir
, &qca
->votes_off
);
575 debugfs_create_u32("vote_on_ms", mode
, ibs_dir
, &qca
->vote_on_ms
);
576 debugfs_create_u32("vote_off_ms", mode
, ibs_dir
, &qca
->vote_off_ms
);
579 mode
= S_IRUGO
| S_IWUSR
;
580 debugfs_create_u32("wake_retrans", mode
, ibs_dir
, &qca
->wake_retrans
);
581 debugfs_create_u32("tx_idle_delay", mode
, ibs_dir
,
582 &qca
->tx_idle_delay
);
585 /* Flush protocol data */
586 static int qca_flush(struct hci_uart
*hu
)
588 struct qca_data
*qca
= hu
->priv
;
590 BT_DBG("hu %p qca flush", hu
);
592 skb_queue_purge(&qca
->tx_wait_q
);
593 skb_queue_purge(&qca
->txq
);
599 static int qca_close(struct hci_uart
*hu
)
601 struct qca_serdev
*qcadev
;
602 struct qca_data
*qca
= hu
->priv
;
604 BT_DBG("hu %p qca close", hu
);
606 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE
, hu
);
608 skb_queue_purge(&qca
->tx_wait_q
);
609 skb_queue_purge(&qca
->txq
);
610 del_timer(&qca
->tx_idle_timer
);
611 del_timer(&qca
->wake_retrans_timer
);
612 destroy_workqueue(qca
->workqueue
);
616 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
617 if (qcadev
->btsoc_type
== QCA_WCN3990
)
618 qca_power_shutdown(hu
);
620 gpiod_set_value_cansleep(qcadev
->bt_en
, 0);
624 kfree_skb(qca
->rx_skb
);
633 /* Called upon a wake-up-indication from the device.
635 static void device_want_to_wakeup(struct hci_uart
*hu
)
638 struct qca_data
*qca
= hu
->priv
;
640 BT_DBG("hu %p want to wake up", hu
);
642 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
644 qca
->ibs_recv_wakes
++;
646 switch (qca
->rx_ibs_state
) {
647 case HCI_IBS_RX_ASLEEP
:
648 /* Make sure clock is on - we may have turned clock off since
649 * receiving the wake up indicator awake rx clock.
651 queue_work(qca
->workqueue
, &qca
->ws_awake_rx
);
652 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
655 case HCI_IBS_RX_AWAKE
:
656 /* Always acknowledge device wake up,
657 * sending IBS message doesn't count as TX ON.
659 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0) {
660 BT_ERR("Failed to acknowledge device wake up");
663 qca
->ibs_sent_wacks
++;
667 /* Any other state is illegal */
668 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
673 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
675 /* Actually send the packets */
676 hci_uart_tx_wakeup(hu
);
679 /* Called upon a sleep-indication from the device.
681 static void device_want_to_sleep(struct hci_uart
*hu
)
684 struct qca_data
*qca
= hu
->priv
;
686 BT_DBG("hu %p want to sleep", hu
);
688 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
690 qca
->ibs_recv_slps
++;
692 switch (qca
->rx_ibs_state
) {
693 case HCI_IBS_RX_AWAKE
:
695 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
696 /* Vote off rx clock under workqueue */
697 queue_work(qca
->workqueue
, &qca
->ws_rx_vote_off
);
700 case HCI_IBS_RX_ASLEEP
:
704 /* Any other state is illegal */
705 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
710 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
713 /* Called upon wake-up-acknowledgement from the device
715 static void device_woke_up(struct hci_uart
*hu
)
717 unsigned long flags
, idle_delay
;
718 struct qca_data
*qca
= hu
->priv
;
719 struct sk_buff
*skb
= NULL
;
721 BT_DBG("hu %p woke up", hu
);
723 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
725 qca
->ibs_recv_wacks
++;
727 switch (qca
->tx_ibs_state
) {
728 case HCI_IBS_TX_AWAKE
:
729 /* Expect one if we send 2 WAKEs */
730 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
734 case HCI_IBS_TX_WAKING
:
735 /* Send pending packets */
736 while ((skb
= skb_dequeue(&qca
->tx_wait_q
)))
737 skb_queue_tail(&qca
->txq
, skb
);
739 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
740 del_timer(&qca
->wake_retrans_timer
);
741 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
742 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
743 qca
->tx_ibs_state
= HCI_IBS_TX_AWAKE
;
746 case HCI_IBS_TX_ASLEEP
:
750 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
755 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
757 /* Actually send the packets */
758 hci_uart_tx_wakeup(hu
);
761 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
762 * two simultaneous tasklets.
764 static int qca_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
766 unsigned long flags
= 0, idle_delay
;
767 struct qca_data
*qca
= hu
->priv
;
769 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu
, skb
,
772 /* Prepend skb with frame type */
773 memcpy(skb_push(skb
, 1), &hci_skb_pkt_type(skb
), 1);
775 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
777 /* Don't go to sleep in middle of patch download or
778 * Out-Of-Band(GPIOs control) sleep is selected.
780 if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED
, &qca
->flags
)) {
781 skb_queue_tail(&qca
->txq
, skb
);
782 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
786 /* Act according to current state */
787 switch (qca
->tx_ibs_state
) {
788 case HCI_IBS_TX_AWAKE
:
789 BT_DBG("Device awake, sending normally");
790 skb_queue_tail(&qca
->txq
, skb
);
791 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
792 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
795 case HCI_IBS_TX_ASLEEP
:
796 BT_DBG("Device asleep, waking up and queueing packet");
797 /* Save packet for later */
798 skb_queue_tail(&qca
->tx_wait_q
, skb
);
800 qca
->tx_ibs_state
= HCI_IBS_TX_WAKING
;
801 /* Schedule a work queue to wake up device */
802 queue_work(qca
->workqueue
, &qca
->ws_awake_device
);
805 case HCI_IBS_TX_WAKING
:
806 BT_DBG("Device waking up, queueing packet");
807 /* Transient state; just keep packet for later */
808 skb_queue_tail(&qca
->tx_wait_q
, skb
);
812 BT_ERR("Illegal tx state: %d (losing packet)",
818 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
823 static int qca_ibs_sleep_ind(struct hci_dev
*hdev
, struct sk_buff
*skb
)
825 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
827 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_SLEEP_IND
);
829 device_want_to_sleep(hu
);
835 static int qca_ibs_wake_ind(struct hci_dev
*hdev
, struct sk_buff
*skb
)
837 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
839 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_WAKE_IND
);
841 device_want_to_wakeup(hu
);
847 static int qca_ibs_wake_ack(struct hci_dev
*hdev
, struct sk_buff
*skb
)
849 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
851 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_WAKE_ACK
);
859 static int qca_recv_acl_data(struct hci_dev
*hdev
, struct sk_buff
*skb
)
861 /* We receive debug logs from chip as an ACL packets.
862 * Instead of sending the data to ACL to decode the
863 * received data, we are pushing them to the above layers
864 * as a diagnostic packet.
866 if (get_unaligned_le16(skb
->data
) == QCA_DEBUG_HANDLE
)
867 return hci_recv_diag(hdev
, skb
);
869 return hci_recv_frame(hdev
, skb
);
872 #define QCA_IBS_SLEEP_IND_EVENT \
873 .type = HCI_IBS_SLEEP_IND, \
877 .maxlen = HCI_MAX_IBS_SIZE
879 #define QCA_IBS_WAKE_IND_EVENT \
880 .type = HCI_IBS_WAKE_IND, \
884 .maxlen = HCI_MAX_IBS_SIZE
886 #define QCA_IBS_WAKE_ACK_EVENT \
887 .type = HCI_IBS_WAKE_ACK, \
891 .maxlen = HCI_MAX_IBS_SIZE
893 static const struct h4_recv_pkt qca_recv_pkts
[] = {
894 { H4_RECV_ACL
, .recv
= qca_recv_acl_data
},
895 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
896 { H4_RECV_EVENT
, .recv
= hci_recv_frame
},
897 { QCA_IBS_WAKE_IND_EVENT
, .recv
= qca_ibs_wake_ind
},
898 { QCA_IBS_WAKE_ACK_EVENT
, .recv
= qca_ibs_wake_ack
},
899 { QCA_IBS_SLEEP_IND_EVENT
, .recv
= qca_ibs_sleep_ind
},
902 static int qca_recv(struct hci_uart
*hu
, const void *data
, int count
)
904 struct qca_data
*qca
= hu
->priv
;
906 if (!test_bit(HCI_UART_REGISTERED
, &hu
->flags
))
909 qca
->rx_skb
= h4_recv_buf(hu
->hdev
, qca
->rx_skb
, data
, count
,
910 qca_recv_pkts
, ARRAY_SIZE(qca_recv_pkts
));
911 if (IS_ERR(qca
->rx_skb
)) {
912 int err
= PTR_ERR(qca
->rx_skb
);
913 bt_dev_err(hu
->hdev
, "Frame reassembly failed (%d)", err
);
921 static struct sk_buff
*qca_dequeue(struct hci_uart
*hu
)
923 struct qca_data
*qca
= hu
->priv
;
925 return skb_dequeue(&qca
->txq
);
928 static uint8_t qca_get_baudrate_value(int speed
)
932 return QCA_BAUDRATE_9600
;
934 return QCA_BAUDRATE_19200
;
936 return QCA_BAUDRATE_38400
;
938 return QCA_BAUDRATE_57600
;
940 return QCA_BAUDRATE_115200
;
942 return QCA_BAUDRATE_230400
;
944 return QCA_BAUDRATE_460800
;
946 return QCA_BAUDRATE_500000
;
948 return QCA_BAUDRATE_921600
;
950 return QCA_BAUDRATE_1000000
;
952 return QCA_BAUDRATE_2000000
;
954 return QCA_BAUDRATE_3000000
;
956 return QCA_BAUDRATE_3200000
;
958 return QCA_BAUDRATE_3500000
;
960 return QCA_BAUDRATE_115200
;
964 static int qca_set_baudrate(struct hci_dev
*hdev
, uint8_t baudrate
)
966 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
967 struct qca_data
*qca
= hu
->priv
;
968 struct qca_serdev
*qcadev
;
970 u8 cmd
[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
972 if (baudrate
> QCA_BAUDRATE_3200000
)
977 skb
= bt_skb_alloc(sizeof(cmd
), GFP_KERNEL
);
979 bt_dev_err(hdev
, "Failed to allocate baudrate packet");
983 /* Assign commands to change baudrate and packet type. */
984 skb_put_data(skb
, cmd
, sizeof(cmd
));
985 hci_skb_pkt_type(skb
) = HCI_COMMAND_PKT
;
987 skb_queue_tail(&qca
->txq
, skb
);
988 hci_uart_tx_wakeup(hu
);
990 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
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 (qcadev
->btsoc_type
== QCA_WCN3990
)
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 struct qca_serdev
*qcadev
;
1080 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1081 if (qcadev
->btsoc_type
== QCA_WCN3990
) {
1082 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) &&
1083 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1086 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) ||
1087 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1094 static int qca_set_speed(struct hci_uart
*hu
, enum qca_speed_type speed_type
)
1096 unsigned int speed
, qca_baudrate
;
1097 struct qca_serdev
*qcadev
;
1100 if (speed_type
== QCA_INIT_SPEED
) {
1101 speed
= qca_get_speed(hu
, QCA_INIT_SPEED
);
1103 host_set_baudrate(hu
, speed
);
1105 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1109 /* Disable flow control for wcn3990 to deassert RTS while
1110 * changing the baudrate of chip and host.
1112 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1113 if (qcadev
->btsoc_type
== QCA_WCN3990
)
1114 hci_uart_set_flow_control(hu
, true);
1116 qca_baudrate
= qca_get_baudrate_value(speed
);
1117 bt_dev_dbg(hu
->hdev
, "Set UART speed to %d", speed
);
1118 ret
= qca_set_baudrate(hu
->hdev
, qca_baudrate
);
1122 host_set_baudrate(hu
, speed
);
1125 if (qcadev
->btsoc_type
== QCA_WCN3990
)
1126 hci_uart_set_flow_control(hu
, false);
1132 static int qca_wcn3990_init(struct hci_uart
*hu
)
1134 struct qca_serdev
*qcadev
;
1137 /* Check for vregs status, may be hci down has turned
1138 * off the voltage regulator.
1140 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1141 if (!qcadev
->bt_power
->vregs_on
) {
1142 serdev_device_close(hu
->serdev
);
1143 ret
= qca_power_setup(hu
, true);
1147 ret
= serdev_device_open(hu
->serdev
);
1149 bt_dev_err(hu
->hdev
, "failed to open port");
1154 /* Forcefully enable wcn3990 to enter in to boot mode. */
1155 host_set_baudrate(hu
, 2400);
1156 ret
= qca_send_power_pulse(hu
, false);
1160 qca_set_speed(hu
, QCA_INIT_SPEED
);
1161 ret
= qca_send_power_pulse(hu
, true);
1165 /* Now the device is in ready state to communicate with host.
1166 * To sync host with device we need to reopen port.
1167 * Without this, we will have RTS and CTS synchronization
1170 serdev_device_close(hu
->serdev
);
1171 ret
= serdev_device_open(hu
->serdev
);
1173 bt_dev_err(hu
->hdev
, "failed to open port");
1177 hci_uart_set_flow_control(hu
, false);
1182 static int qca_setup(struct hci_uart
*hu
)
1184 struct hci_dev
*hdev
= hu
->hdev
;
1185 struct qca_data
*qca
= hu
->priv
;
1186 unsigned int speed
, qca_baudrate
= QCA_BAUDRATE_115200
;
1187 struct qca_serdev
*qcadev
;
1191 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1193 ret
= qca_check_speeds(hu
);
1197 /* Patch downloading has to be done without IBS mode */
1198 clear_bit(STATE_IN_BAND_SLEEP_ENABLED
, &qca
->flags
);
1200 if (qcadev
->btsoc_type
== QCA_WCN3990
) {
1201 bt_dev_info(hdev
, "setting up wcn3990");
1203 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1204 * setup for every hci up.
1206 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP
, &hdev
->quirks
);
1207 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY
, &hdev
->quirks
);
1208 hu
->hdev
->shutdown
= qca_power_off
;
1209 ret
= qca_wcn3990_init(hu
);
1213 ret
= qca_read_soc_version(hdev
, &soc_ver
);
1217 bt_dev_info(hdev
, "ROME setup");
1218 qca_set_speed(hu
, QCA_INIT_SPEED
);
1221 /* Setup user speed if needed */
1222 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1224 ret
= qca_set_speed(hu
, QCA_OPER_SPEED
);
1228 qca_baudrate
= qca_get_baudrate_value(speed
);
1231 if (qcadev
->btsoc_type
!= QCA_WCN3990
) {
1232 /* Get QCA version information */
1233 ret
= qca_read_soc_version(hdev
, &soc_ver
);
1238 bt_dev_info(hdev
, "QCA controller version 0x%08x", soc_ver
);
1239 /* Setup patch / NVM configurations */
1240 ret
= qca_uart_setup(hdev
, qca_baudrate
, qcadev
->btsoc_type
, soc_ver
);
1242 set_bit(STATE_IN_BAND_SLEEP_ENABLED
, &qca
->flags
);
1243 qca_debugfs_init(hdev
);
1244 } else if (ret
== -ENOENT
) {
1245 /* No patch/nvm-config found, run with original fw/config */
1247 } else if (ret
== -EAGAIN
) {
1249 * Userspace firmware loader will return -EAGAIN in case no
1250 * patch/nvm-config is found, so run with original fw/config.
1256 if (qcadev
->btsoc_type
== QCA_WCN3990
)
1257 hu
->hdev
->set_bdaddr
= qca_set_bdaddr
;
1259 hu
->hdev
->set_bdaddr
= qca_set_bdaddr_rome
;
1264 static struct hci_uart_proto qca_proto
= {
1268 .init_speed
= 115200,
1269 .oper_speed
= 3000000,
1275 .enqueue
= qca_enqueue
,
1276 .dequeue
= qca_dequeue
,
1279 static const struct qca_vreg_data qca_soc_data
= {
1280 .soc_type
= QCA_WCN3990
,
1281 .vregs
= (struct qca_vreg
[]) {
1282 { "vddio", 1800000, 1900000, 15000 },
1283 { "vddxo", 1800000, 1900000, 80000 },
1284 { "vddrf", 1300000, 1350000, 300000 },
1285 { "vddch0", 3300000, 3400000, 450000 },
1290 static void qca_power_shutdown(struct hci_uart
*hu
)
1292 struct qca_data
*qca
= hu
->priv
;
1293 unsigned long flags
;
1295 /* From this point we go into power off state. But serial port is
1296 * still open, stop queueing the IBS data and flush all the buffered
1299 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
1300 clear_bit(STATE_IN_BAND_SLEEP_ENABLED
, &qca
->flags
);
1302 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
1304 host_set_baudrate(hu
, 2400);
1305 qca_send_power_pulse(hu
, false);
1306 qca_power_setup(hu
, false);
1309 static int qca_power_off(struct hci_dev
*hdev
)
1311 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1313 qca_power_shutdown(hu
);
1317 static int qca_enable_regulator(struct qca_vreg vregs
,
1318 struct regulator
*regulator
)
1322 ret
= regulator_set_voltage(regulator
, vregs
.min_uV
,
1328 ret
= regulator_set_load(regulator
,
1334 return regulator_enable(regulator
);
1338 static void qca_disable_regulator(struct qca_vreg vregs
,
1339 struct regulator
*regulator
)
1341 regulator_disable(regulator
);
1342 regulator_set_voltage(regulator
, 0, vregs
.max_uV
);
1344 regulator_set_load(regulator
, 0);
1348 static int qca_power_setup(struct hci_uart
*hu
, bool on
)
1350 struct qca_vreg
*vregs
;
1351 struct regulator_bulk_data
*vreg_bulk
;
1352 struct qca_serdev
*qcadev
;
1353 int i
, num_vregs
, ret
= 0;
1355 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1356 if (!qcadev
|| !qcadev
->bt_power
|| !qcadev
->bt_power
->vreg_data
||
1357 !qcadev
->bt_power
->vreg_bulk
)
1360 vregs
= qcadev
->bt_power
->vreg_data
->vregs
;
1361 vreg_bulk
= qcadev
->bt_power
->vreg_bulk
;
1362 num_vregs
= qcadev
->bt_power
->vreg_data
->num_vregs
;
1363 BT_DBG("on: %d", on
);
1364 if (on
&& !qcadev
->bt_power
->vregs_on
) {
1365 for (i
= 0; i
< num_vregs
; i
++) {
1366 ret
= qca_enable_regulator(vregs
[i
],
1367 vreg_bulk
[i
].consumer
);
1373 BT_ERR("failed to enable regulator:%s", vregs
[i
].name
);
1374 /* turn off regulators which are enabled */
1375 for (i
= i
- 1; i
>= 0; i
--)
1376 qca_disable_regulator(vregs
[i
],
1377 vreg_bulk
[i
].consumer
);
1379 qcadev
->bt_power
->vregs_on
= true;
1381 } else if (!on
&& qcadev
->bt_power
->vregs_on
) {
1382 /* turn off regulator in reverse order */
1383 i
= qcadev
->bt_power
->vreg_data
->num_vregs
- 1;
1384 for ( ; i
>= 0; i
--)
1385 qca_disable_regulator(vregs
[i
], vreg_bulk
[i
].consumer
);
1387 qcadev
->bt_power
->vregs_on
= false;
1393 static int qca_init_regulators(struct qca_power
*qca
,
1394 const struct qca_vreg
*vregs
, size_t num_vregs
)
1398 qca
->vreg_bulk
= devm_kcalloc(qca
->dev
, num_vregs
,
1399 sizeof(struct regulator_bulk_data
),
1401 if (!qca
->vreg_bulk
)
1404 for (i
= 0; i
< num_vregs
; i
++)
1405 qca
->vreg_bulk
[i
].supply
= vregs
[i
].name
;
1407 return devm_regulator_bulk_get(qca
->dev
, num_vregs
, qca
->vreg_bulk
);
1410 static int qca_serdev_probe(struct serdev_device
*serdev
)
1412 struct qca_serdev
*qcadev
;
1413 const struct qca_vreg_data
*data
;
1416 qcadev
= devm_kzalloc(&serdev
->dev
, sizeof(*qcadev
), GFP_KERNEL
);
1420 qcadev
->serdev_hu
.serdev
= serdev
;
1421 data
= of_device_get_match_data(&serdev
->dev
);
1422 serdev_device_set_drvdata(serdev
, qcadev
);
1423 if (data
&& data
->soc_type
== QCA_WCN3990
) {
1424 qcadev
->btsoc_type
= QCA_WCN3990
;
1425 qcadev
->bt_power
= devm_kzalloc(&serdev
->dev
,
1426 sizeof(struct qca_power
),
1428 if (!qcadev
->bt_power
)
1431 qcadev
->bt_power
->dev
= &serdev
->dev
;
1432 qcadev
->bt_power
->vreg_data
= data
;
1433 err
= qca_init_regulators(qcadev
->bt_power
, data
->vregs
,
1436 BT_ERR("Failed to init regulators:%d", err
);
1440 qcadev
->bt_power
->vregs_on
= false;
1442 device_property_read_u32(&serdev
->dev
, "max-speed",
1443 &qcadev
->oper_speed
);
1444 if (!qcadev
->oper_speed
)
1445 BT_DBG("UART will pick default operating speed");
1447 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1449 BT_ERR("wcn3990 serdev registration failed");
1453 qcadev
->btsoc_type
= QCA_ROME
;
1454 qcadev
->bt_en
= devm_gpiod_get(&serdev
->dev
, "enable",
1456 if (IS_ERR(qcadev
->bt_en
)) {
1457 dev_err(&serdev
->dev
, "failed to acquire enable gpio\n");
1458 return PTR_ERR(qcadev
->bt_en
);
1461 qcadev
->susclk
= devm_clk_get(&serdev
->dev
, NULL
);
1462 if (IS_ERR(qcadev
->susclk
)) {
1463 dev_err(&serdev
->dev
, "failed to acquire clk\n");
1464 return PTR_ERR(qcadev
->susclk
);
1467 err
= clk_set_rate(qcadev
->susclk
, SUSCLK_RATE_32KHZ
);
1471 err
= clk_prepare_enable(qcadev
->susclk
);
1475 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1477 clk_disable_unprepare(qcadev
->susclk
);
1484 static void qca_serdev_remove(struct serdev_device
*serdev
)
1486 struct qca_serdev
*qcadev
= serdev_device_get_drvdata(serdev
);
1488 if (qcadev
->btsoc_type
== QCA_WCN3990
)
1489 qca_power_shutdown(&qcadev
->serdev_hu
);
1491 clk_disable_unprepare(qcadev
->susclk
);
1493 hci_uart_unregister_device(&qcadev
->serdev_hu
);
1496 static const struct of_device_id qca_bluetooth_of_match
[] = {
1497 { .compatible
= "qcom,qca6174-bt" },
1498 { .compatible
= "qcom,wcn3990-bt", .data
= &qca_soc_data
},
1501 MODULE_DEVICE_TABLE(of
, qca_bluetooth_of_match
);
1503 static struct serdev_device_driver qca_serdev_driver
= {
1504 .probe
= qca_serdev_probe
,
1505 .remove
= qca_serdev_remove
,
1507 .name
= "hci_uart_qca",
1508 .of_match_table
= qca_bluetooth_of_match
,
1512 int __init
qca_init(void)
1514 serdev_device_driver_register(&qca_serdev_driver
);
1516 return hci_uart_register_proto(&qca_proto
);
1519 int __exit
qca_deinit(void)
1521 serdev_device_driver_unregister(&qca_serdev_driver
);
1523 return hci_uart_unregister_proto(&qca_proto
);