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>
44 #include <net/bluetooth/bluetooth.h>
45 #include <net/bluetooth/hci_core.h>
50 /* HCI_IBS protocol messages */
51 #define HCI_IBS_SLEEP_IND 0xFE
52 #define HCI_IBS_WAKE_IND 0xFD
53 #define HCI_IBS_WAKE_ACK 0xFC
54 #define HCI_MAX_IBS_SIZE 10
56 /* Controller states */
57 #define STATE_IN_BAND_SLEEP_ENABLED 1
59 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
60 #define IBS_TX_IDLE_TIMEOUT_MS 2000
61 #define BAUDRATE_SETTLE_TIMEOUT_MS 300
64 #define SUSCLK_RATE_32KHZ 32768
66 /* HCI_IBS transmit side sleep protocol states */
73 /* HCI_IBS receive side sleep protocol states */
79 /* HCI_IBS transmit and receive side clock state vote */
80 enum hci_ibs_clock_state_vote
{
81 HCI_IBS_VOTE_STATS_UPDATE
,
82 HCI_IBS_TX_VOTE_CLOCK_ON
,
83 HCI_IBS_TX_VOTE_CLOCK_OFF
,
84 HCI_IBS_RX_VOTE_CLOCK_ON
,
85 HCI_IBS_RX_VOTE_CLOCK_OFF
,
90 struct sk_buff
*rx_skb
;
91 struct sk_buff_head txq
;
92 struct sk_buff_head tx_wait_q
; /* HCI_IBS wait queue */
93 spinlock_t hci_ibs_lock
; /* HCI_IBS state lock */
94 u8 tx_ibs_state
; /* HCI_IBS transmit side power state*/
95 u8 rx_ibs_state
; /* HCI_IBS receive side power state */
96 bool tx_vote
; /* Clock must be on for TX */
97 bool rx_vote
; /* Clock must be on for RX */
98 struct timer_list tx_idle_timer
;
100 struct timer_list wake_retrans_timer
;
102 struct workqueue_struct
*workqueue
;
103 struct work_struct ws_awake_rx
;
104 struct work_struct ws_awake_device
;
105 struct work_struct ws_rx_vote_off
;
106 struct work_struct ws_tx_vote_off
;
109 /* For debugging purpose */
127 enum qca_speed_type
{
133 * Voltage regulator information required for configuring the
134 * QCA Bluetooth chipset
140 unsigned int load_uA
;
143 struct qca_vreg_data
{
144 enum qca_btsoc_type soc_type
;
145 struct qca_vreg
*vregs
;
150 * Platform data for the QCA Bluetooth power driver.
154 const struct qca_vreg_data
*vreg_data
;
155 struct regulator_bulk_data
*vreg_bulk
;
160 struct hci_uart serdev_hu
;
161 struct gpio_desc
*bt_en
;
163 enum qca_btsoc_type btsoc_type
;
164 struct qca_power
*bt_power
;
169 static int qca_power_setup(struct hci_uart
*hu
, bool on
);
170 static void qca_power_shutdown(struct hci_dev
*hdev
);
172 static void __serial_clock_on(struct tty_struct
*tty
)
174 /* TODO: Some chipset requires to enable UART clock on client
175 * side to save power consumption or manual work is required.
176 * Please put your code to control UART clock here if needed
180 static void __serial_clock_off(struct tty_struct
*tty
)
182 /* TODO: Some chipset requires to disable UART clock on client
183 * side to save power consumption or manual work is required.
184 * Please put your code to control UART clock off here if needed
188 /* serial_clock_vote needs to be called with the ibs lock held */
189 static void serial_clock_vote(unsigned long vote
, struct hci_uart
*hu
)
191 struct qca_data
*qca
= hu
->priv
;
194 bool old_vote
= (qca
->tx_vote
| qca
->rx_vote
);
198 case HCI_IBS_VOTE_STATS_UPDATE
:
199 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
202 qca
->vote_off_ms
+= diff
;
204 qca
->vote_on_ms
+= diff
;
207 case HCI_IBS_TX_VOTE_CLOCK_ON
:
213 case HCI_IBS_RX_VOTE_CLOCK_ON
:
219 case HCI_IBS_TX_VOTE_CLOCK_OFF
:
220 qca
->tx_vote
= false;
222 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
225 case HCI_IBS_RX_VOTE_CLOCK_OFF
:
226 qca
->rx_vote
= false;
228 new_vote
= qca
->rx_vote
| qca
->tx_vote
;
232 BT_ERR("Voting irregularity");
236 if (new_vote
!= old_vote
) {
238 __serial_clock_on(hu
->tty
);
240 __serial_clock_off(hu
->tty
);
242 BT_DBG("Vote serial clock %s(%s)", new_vote
? "true" : "false",
243 vote
? "true" : "false");
245 diff
= jiffies_to_msecs(jiffies
- qca
->vote_last_jif
);
249 qca
->vote_off_ms
+= diff
;
252 qca
->vote_on_ms
+= diff
;
254 qca
->vote_last_jif
= jiffies
;
258 /* Builds and sends an HCI_IBS command packet.
259 * These are very simple packets with only 1 cmd byte.
261 static int send_hci_ibs_cmd(u8 cmd
, struct hci_uart
*hu
)
264 struct sk_buff
*skb
= NULL
;
265 struct qca_data
*qca
= hu
->priv
;
267 BT_DBG("hu %p send hci ibs cmd 0x%x", hu
, cmd
);
269 skb
= bt_skb_alloc(1, GFP_ATOMIC
);
271 BT_ERR("Failed to allocate memory for HCI_IBS packet");
275 /* Assign HCI_IBS type */
276 skb_put_u8(skb
, cmd
);
278 skb_queue_tail(&qca
->txq
, skb
);
283 static void qca_wq_awake_device(struct work_struct
*work
)
285 struct qca_data
*qca
= container_of(work
, struct qca_data
,
287 struct hci_uart
*hu
= qca
->hu
;
288 unsigned long retrans_delay
;
290 BT_DBG("hu %p wq awake device", hu
);
292 /* Vote for serial clock */
293 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON
, hu
);
295 spin_lock(&qca
->hci_ibs_lock
);
297 /* Send wake indication to device */
298 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0)
299 BT_ERR("Failed to send WAKE to device");
301 qca
->ibs_sent_wakes
++;
303 /* Start retransmit timer */
304 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
305 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
307 spin_unlock(&qca
->hci_ibs_lock
);
309 /* Actually send the packets */
310 hci_uart_tx_wakeup(hu
);
313 static void qca_wq_awake_rx(struct work_struct
*work
)
315 struct qca_data
*qca
= container_of(work
, struct qca_data
,
317 struct hci_uart
*hu
= qca
->hu
;
319 BT_DBG("hu %p wq awake rx", hu
);
321 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON
, hu
);
323 spin_lock(&qca
->hci_ibs_lock
);
324 qca
->rx_ibs_state
= HCI_IBS_RX_AWAKE
;
326 /* Always acknowledge device wake up,
327 * sending IBS message doesn't count as TX ON.
329 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0)
330 BT_ERR("Failed to acknowledge device wake up");
332 qca
->ibs_sent_wacks
++;
334 spin_unlock(&qca
->hci_ibs_lock
);
336 /* Actually send the packets */
337 hci_uart_tx_wakeup(hu
);
340 static void qca_wq_serial_rx_clock_vote_off(struct work_struct
*work
)
342 struct qca_data
*qca
= container_of(work
, struct qca_data
,
344 struct hci_uart
*hu
= qca
->hu
;
346 BT_DBG("hu %p rx clock vote off", hu
);
348 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF
, hu
);
351 static void qca_wq_serial_tx_clock_vote_off(struct work_struct
*work
)
353 struct qca_data
*qca
= container_of(work
, struct qca_data
,
355 struct hci_uart
*hu
= qca
->hu
;
357 BT_DBG("hu %p tx clock vote off", hu
);
359 /* Run HCI tx handling unlocked */
360 hci_uart_tx_wakeup(hu
);
362 /* Now that message queued to tty driver, vote for tty clocks off.
363 * It is up to the tty driver to pend the clocks off until tx done.
365 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF
, hu
);
368 static void hci_ibs_tx_idle_timeout(struct timer_list
*t
)
370 struct qca_data
*qca
= from_timer(qca
, t
, tx_idle_timer
);
371 struct hci_uart
*hu
= qca
->hu
;
374 BT_DBG("hu %p idle timeout in %d state", hu
, qca
->tx_ibs_state
);
376 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
377 flags
, SINGLE_DEPTH_NESTING
);
379 switch (qca
->tx_ibs_state
) {
380 case HCI_IBS_TX_AWAKE
:
381 /* TX_IDLE, go to SLEEP */
382 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND
, hu
) < 0) {
383 BT_ERR("Failed to send SLEEP to device");
386 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
387 qca
->ibs_sent_slps
++;
388 queue_work(qca
->workqueue
, &qca
->ws_tx_vote_off
);
391 case HCI_IBS_TX_ASLEEP
:
392 case HCI_IBS_TX_WAKING
:
396 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
400 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
403 static void hci_ibs_wake_retrans_timeout(struct timer_list
*t
)
405 struct qca_data
*qca
= from_timer(qca
, t
, wake_retrans_timer
);
406 struct hci_uart
*hu
= qca
->hu
;
407 unsigned long flags
, retrans_delay
;
408 bool retransmit
= false;
410 BT_DBG("hu %p wake retransmit timeout in %d state",
411 hu
, qca
->tx_ibs_state
);
413 spin_lock_irqsave_nested(&qca
->hci_ibs_lock
,
414 flags
, SINGLE_DEPTH_NESTING
);
416 switch (qca
->tx_ibs_state
) {
417 case HCI_IBS_TX_WAKING
:
418 /* No WAKE_ACK, retransmit WAKE */
420 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND
, hu
) < 0) {
421 BT_ERR("Failed to acknowledge device wake up");
424 qca
->ibs_sent_wakes
++;
425 retrans_delay
= msecs_to_jiffies(qca
->wake_retrans
);
426 mod_timer(&qca
->wake_retrans_timer
, jiffies
+ retrans_delay
);
429 case HCI_IBS_TX_ASLEEP
:
430 case HCI_IBS_TX_AWAKE
:
434 BT_ERR("Spurious timeout tx state %d", qca
->tx_ibs_state
);
438 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
441 hci_uart_tx_wakeup(hu
);
444 /* Initialize protocol */
445 static int qca_open(struct hci_uart
*hu
)
447 struct qca_serdev
*qcadev
;
448 struct qca_data
*qca
;
451 BT_DBG("hu %p qca_open", hu
);
453 qca
= kzalloc(sizeof(struct qca_data
), GFP_KERNEL
);
457 skb_queue_head_init(&qca
->txq
);
458 skb_queue_head_init(&qca
->tx_wait_q
);
459 spin_lock_init(&qca
->hci_ibs_lock
);
460 qca
->workqueue
= alloc_ordered_workqueue("qca_wq", 0);
461 if (!qca
->workqueue
) {
462 BT_ERR("QCA Workqueue not initialized properly");
467 INIT_WORK(&qca
->ws_awake_rx
, qca_wq_awake_rx
);
468 INIT_WORK(&qca
->ws_awake_device
, qca_wq_awake_device
);
469 INIT_WORK(&qca
->ws_rx_vote_off
, qca_wq_serial_rx_clock_vote_off
);
470 INIT_WORK(&qca
->ws_tx_vote_off
, qca_wq_serial_tx_clock_vote_off
);
474 /* Assume we start with both sides asleep -- extra wakes OK */
475 qca
->tx_ibs_state
= HCI_IBS_TX_ASLEEP
;
476 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
478 /* clocks actually on, but we start votes off */
479 qca
->tx_vote
= false;
480 qca
->rx_vote
= false;
483 qca
->ibs_sent_wacks
= 0;
484 qca
->ibs_sent_slps
= 0;
485 qca
->ibs_sent_wakes
= 0;
486 qca
->ibs_recv_wacks
= 0;
487 qca
->ibs_recv_slps
= 0;
488 qca
->ibs_recv_wakes
= 0;
489 qca
->vote_last_jif
= jiffies
;
491 qca
->vote_off_ms
= 0;
494 qca
->tx_votes_on
= 0;
495 qca
->tx_votes_off
= 0;
496 qca
->rx_votes_on
= 0;
497 qca
->rx_votes_off
= 0;
502 serdev_device_open(hu
->serdev
);
504 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
505 if (qcadev
->btsoc_type
!= QCA_WCN3990
) {
506 gpiod_set_value_cansleep(qcadev
->bt_en
, 1);
508 hu
->init_speed
= qcadev
->init_speed
;
509 hu
->oper_speed
= qcadev
->oper_speed
;
510 ret
= qca_power_setup(hu
, true);
512 destroy_workqueue(qca
->workqueue
);
513 kfree_skb(qca
->rx_skb
);
521 timer_setup(&qca
->wake_retrans_timer
, hci_ibs_wake_retrans_timeout
, 0);
522 qca
->wake_retrans
= IBS_WAKE_RETRANS_TIMEOUT_MS
;
524 timer_setup(&qca
->tx_idle_timer
, hci_ibs_tx_idle_timeout
, 0);
525 qca
->tx_idle_delay
= IBS_TX_IDLE_TIMEOUT_MS
;
527 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
528 qca
->tx_idle_delay
, qca
->wake_retrans
);
533 static void qca_debugfs_init(struct hci_dev
*hdev
)
535 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
536 struct qca_data
*qca
= hu
->priv
;
537 struct dentry
*ibs_dir
;
543 ibs_dir
= debugfs_create_dir("ibs", hdev
->debugfs
);
547 debugfs_create_u8("tx_ibs_state", mode
, ibs_dir
, &qca
->tx_ibs_state
);
548 debugfs_create_u8("rx_ibs_state", mode
, ibs_dir
, &qca
->rx_ibs_state
);
549 debugfs_create_u64("ibs_sent_sleeps", mode
, ibs_dir
,
550 &qca
->ibs_sent_slps
);
551 debugfs_create_u64("ibs_sent_wakes", mode
, ibs_dir
,
552 &qca
->ibs_sent_wakes
);
553 debugfs_create_u64("ibs_sent_wake_acks", mode
, ibs_dir
,
554 &qca
->ibs_sent_wacks
);
555 debugfs_create_u64("ibs_recv_sleeps", mode
, ibs_dir
,
556 &qca
->ibs_recv_slps
);
557 debugfs_create_u64("ibs_recv_wakes", mode
, ibs_dir
,
558 &qca
->ibs_recv_wakes
);
559 debugfs_create_u64("ibs_recv_wake_acks", mode
, ibs_dir
,
560 &qca
->ibs_recv_wacks
);
561 debugfs_create_bool("tx_vote", mode
, ibs_dir
, &qca
->tx_vote
);
562 debugfs_create_u64("tx_votes_on", mode
, ibs_dir
, &qca
->tx_votes_on
);
563 debugfs_create_u64("tx_votes_off", mode
, ibs_dir
, &qca
->tx_votes_off
);
564 debugfs_create_bool("rx_vote", mode
, ibs_dir
, &qca
->rx_vote
);
565 debugfs_create_u64("rx_votes_on", mode
, ibs_dir
, &qca
->rx_votes_on
);
566 debugfs_create_u64("rx_votes_off", mode
, ibs_dir
, &qca
->rx_votes_off
);
567 debugfs_create_u64("votes_on", mode
, ibs_dir
, &qca
->votes_on
);
568 debugfs_create_u64("votes_off", mode
, ibs_dir
, &qca
->votes_off
);
569 debugfs_create_u32("vote_on_ms", mode
, ibs_dir
, &qca
->vote_on_ms
);
570 debugfs_create_u32("vote_off_ms", mode
, ibs_dir
, &qca
->vote_off_ms
);
573 mode
= S_IRUGO
| S_IWUSR
;
574 debugfs_create_u32("wake_retrans", mode
, ibs_dir
, &qca
->wake_retrans
);
575 debugfs_create_u32("tx_idle_delay", mode
, ibs_dir
,
576 &qca
->tx_idle_delay
);
579 /* Flush protocol data */
580 static int qca_flush(struct hci_uart
*hu
)
582 struct qca_data
*qca
= hu
->priv
;
584 BT_DBG("hu %p qca flush", hu
);
586 skb_queue_purge(&qca
->tx_wait_q
);
587 skb_queue_purge(&qca
->txq
);
593 static int qca_close(struct hci_uart
*hu
)
595 struct qca_serdev
*qcadev
;
596 struct qca_data
*qca
= hu
->priv
;
598 BT_DBG("hu %p qca close", hu
);
600 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE
, hu
);
602 skb_queue_purge(&qca
->tx_wait_q
);
603 skb_queue_purge(&qca
->txq
);
604 del_timer(&qca
->tx_idle_timer
);
605 del_timer(&qca
->wake_retrans_timer
);
606 destroy_workqueue(qca
->workqueue
);
610 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
611 if (qcadev
->btsoc_type
== QCA_WCN3990
)
612 qca_power_shutdown(hu
->hdev
);
614 gpiod_set_value_cansleep(qcadev
->bt_en
, 0);
616 serdev_device_close(hu
->serdev
);
619 kfree_skb(qca
->rx_skb
);
628 /* Called upon a wake-up-indication from the device.
630 static void device_want_to_wakeup(struct hci_uart
*hu
)
633 struct qca_data
*qca
= hu
->priv
;
635 BT_DBG("hu %p want to wake up", hu
);
637 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
639 qca
->ibs_recv_wakes
++;
641 switch (qca
->rx_ibs_state
) {
642 case HCI_IBS_RX_ASLEEP
:
643 /* Make sure clock is on - we may have turned clock off since
644 * receiving the wake up indicator awake rx clock.
646 queue_work(qca
->workqueue
, &qca
->ws_awake_rx
);
647 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
650 case HCI_IBS_RX_AWAKE
:
651 /* Always acknowledge device wake up,
652 * sending IBS message doesn't count as TX ON.
654 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK
, hu
) < 0) {
655 BT_ERR("Failed to acknowledge device wake up");
658 qca
->ibs_sent_wacks
++;
662 /* Any other state is illegal */
663 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
668 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
670 /* Actually send the packets */
671 hci_uart_tx_wakeup(hu
);
674 /* Called upon a sleep-indication from the device.
676 static void device_want_to_sleep(struct hci_uart
*hu
)
679 struct qca_data
*qca
= hu
->priv
;
681 BT_DBG("hu %p want to sleep", hu
);
683 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
685 qca
->ibs_recv_slps
++;
687 switch (qca
->rx_ibs_state
) {
688 case HCI_IBS_RX_AWAKE
:
690 qca
->rx_ibs_state
= HCI_IBS_RX_ASLEEP
;
691 /* Vote off rx clock under workqueue */
692 queue_work(qca
->workqueue
, &qca
->ws_rx_vote_off
);
695 case HCI_IBS_RX_ASLEEP
:
699 /* Any other state is illegal */
700 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
705 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
708 /* Called upon wake-up-acknowledgement from the device
710 static void device_woke_up(struct hci_uart
*hu
)
712 unsigned long flags
, idle_delay
;
713 struct qca_data
*qca
= hu
->priv
;
714 struct sk_buff
*skb
= NULL
;
716 BT_DBG("hu %p woke up", hu
);
718 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
720 qca
->ibs_recv_wacks
++;
722 switch (qca
->tx_ibs_state
) {
723 case HCI_IBS_TX_AWAKE
:
724 /* Expect one if we send 2 WAKEs */
725 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
729 case HCI_IBS_TX_WAKING
:
730 /* Send pending packets */
731 while ((skb
= skb_dequeue(&qca
->tx_wait_q
)))
732 skb_queue_tail(&qca
->txq
, skb
);
734 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
735 del_timer(&qca
->wake_retrans_timer
);
736 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
737 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
738 qca
->tx_ibs_state
= HCI_IBS_TX_AWAKE
;
741 case HCI_IBS_TX_ASLEEP
:
745 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
750 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
752 /* Actually send the packets */
753 hci_uart_tx_wakeup(hu
);
756 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
757 * two simultaneous tasklets.
759 static int qca_enqueue(struct hci_uart
*hu
, struct sk_buff
*skb
)
761 unsigned long flags
= 0, idle_delay
;
762 struct qca_data
*qca
= hu
->priv
;
764 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu
, skb
,
767 /* Prepend skb with frame type */
768 memcpy(skb_push(skb
, 1), &hci_skb_pkt_type(skb
), 1);
770 /* Don't go to sleep in middle of patch download or
771 * Out-Of-Band(GPIOs control) sleep is selected.
773 if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED
, &qca
->flags
)) {
774 skb_queue_tail(&qca
->txq
, skb
);
778 spin_lock_irqsave(&qca
->hci_ibs_lock
, flags
);
780 /* Act according to current state */
781 switch (qca
->tx_ibs_state
) {
782 case HCI_IBS_TX_AWAKE
:
783 BT_DBG("Device awake, sending normally");
784 skb_queue_tail(&qca
->txq
, skb
);
785 idle_delay
= msecs_to_jiffies(qca
->tx_idle_delay
);
786 mod_timer(&qca
->tx_idle_timer
, jiffies
+ idle_delay
);
789 case HCI_IBS_TX_ASLEEP
:
790 BT_DBG("Device asleep, waking up and queueing packet");
791 /* Save packet for later */
792 skb_queue_tail(&qca
->tx_wait_q
, skb
);
794 qca
->tx_ibs_state
= HCI_IBS_TX_WAKING
;
795 /* Schedule a work queue to wake up device */
796 queue_work(qca
->workqueue
, &qca
->ws_awake_device
);
799 case HCI_IBS_TX_WAKING
:
800 BT_DBG("Device waking up, queueing packet");
801 /* Transient state; just keep packet for later */
802 skb_queue_tail(&qca
->tx_wait_q
, skb
);
806 BT_ERR("Illegal tx state: %d (losing packet)",
812 spin_unlock_irqrestore(&qca
->hci_ibs_lock
, flags
);
817 static int qca_ibs_sleep_ind(struct hci_dev
*hdev
, struct sk_buff
*skb
)
819 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
821 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_SLEEP_IND
);
823 device_want_to_sleep(hu
);
829 static int qca_ibs_wake_ind(struct hci_dev
*hdev
, struct sk_buff
*skb
)
831 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
833 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_WAKE_IND
);
835 device_want_to_wakeup(hu
);
841 static int qca_ibs_wake_ack(struct hci_dev
*hdev
, struct sk_buff
*skb
)
843 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
845 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu
, HCI_IBS_WAKE_ACK
);
853 #define QCA_IBS_SLEEP_IND_EVENT \
854 .type = HCI_IBS_SLEEP_IND, \
858 .maxlen = HCI_MAX_IBS_SIZE
860 #define QCA_IBS_WAKE_IND_EVENT \
861 .type = HCI_IBS_WAKE_IND, \
865 .maxlen = HCI_MAX_IBS_SIZE
867 #define QCA_IBS_WAKE_ACK_EVENT \
868 .type = HCI_IBS_WAKE_ACK, \
872 .maxlen = HCI_MAX_IBS_SIZE
874 static const struct h4_recv_pkt qca_recv_pkts
[] = {
875 { H4_RECV_ACL
, .recv
= hci_recv_frame
},
876 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
877 { H4_RECV_EVENT
, .recv
= hci_recv_frame
},
878 { QCA_IBS_WAKE_IND_EVENT
, .recv
= qca_ibs_wake_ind
},
879 { QCA_IBS_WAKE_ACK_EVENT
, .recv
= qca_ibs_wake_ack
},
880 { QCA_IBS_SLEEP_IND_EVENT
, .recv
= qca_ibs_sleep_ind
},
883 static int qca_recv(struct hci_uart
*hu
, const void *data
, int count
)
885 struct qca_data
*qca
= hu
->priv
;
887 if (!test_bit(HCI_UART_REGISTERED
, &hu
->flags
))
890 qca
->rx_skb
= h4_recv_buf(hu
->hdev
, qca
->rx_skb
, data
, count
,
891 qca_recv_pkts
, ARRAY_SIZE(qca_recv_pkts
));
892 if (IS_ERR(qca
->rx_skb
)) {
893 int err
= PTR_ERR(qca
->rx_skb
);
894 bt_dev_err(hu
->hdev
, "Frame reassembly failed (%d)", err
);
902 static struct sk_buff
*qca_dequeue(struct hci_uart
*hu
)
904 struct qca_data
*qca
= hu
->priv
;
906 return skb_dequeue(&qca
->txq
);
909 static uint8_t qca_get_baudrate_value(int speed
)
913 return QCA_BAUDRATE_9600
;
915 return QCA_BAUDRATE_19200
;
917 return QCA_BAUDRATE_38400
;
919 return QCA_BAUDRATE_57600
;
921 return QCA_BAUDRATE_115200
;
923 return QCA_BAUDRATE_230400
;
925 return QCA_BAUDRATE_460800
;
927 return QCA_BAUDRATE_500000
;
929 return QCA_BAUDRATE_921600
;
931 return QCA_BAUDRATE_1000000
;
933 return QCA_BAUDRATE_2000000
;
935 return QCA_BAUDRATE_3000000
;
937 return QCA_BAUDRATE_3200000
;
939 return QCA_BAUDRATE_3500000
;
941 return QCA_BAUDRATE_115200
;
945 static int qca_set_baudrate(struct hci_dev
*hdev
, uint8_t baudrate
)
947 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
948 struct qca_data
*qca
= hu
->priv
;
950 struct qca_serdev
*qcadev
;
951 u8 cmd
[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
953 if (baudrate
> QCA_BAUDRATE_3200000
)
958 skb
= bt_skb_alloc(sizeof(cmd
), GFP_KERNEL
);
960 bt_dev_err(hdev
, "Failed to allocate baudrate packet");
964 /* Disabling hardware flow control is mandatory while
965 * sending change baudrate request to wcn3990 SoC.
967 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
968 if (qcadev
->btsoc_type
== QCA_WCN3990
)
969 hci_uart_set_flow_control(hu
, true);
971 /* Assign commands to change baudrate and packet type. */
972 skb_put_data(skb
, cmd
, sizeof(cmd
));
973 hci_skb_pkt_type(skb
) = HCI_COMMAND_PKT
;
975 skb_queue_tail(&qca
->txq
, skb
);
976 hci_uart_tx_wakeup(hu
);
978 /* wait 300ms to change new baudrate on controller side
979 * controller will come back after they receive this HCI command
980 * then host can communicate with new baudrate to controller
982 set_current_state(TASK_UNINTERRUPTIBLE
);
983 schedule_timeout(msecs_to_jiffies(BAUDRATE_SETTLE_TIMEOUT_MS
));
984 set_current_state(TASK_RUNNING
);
986 if (qcadev
->btsoc_type
== QCA_WCN3990
)
987 hci_uart_set_flow_control(hu
, false);
992 static inline void host_set_baudrate(struct hci_uart
*hu
, unsigned int speed
)
995 serdev_device_set_baudrate(hu
->serdev
, speed
);
997 hci_uart_set_baudrate(hu
, speed
);
1000 static int qca_send_power_pulse(struct hci_dev
*hdev
, u8 cmd
)
1002 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1003 struct qca_data
*qca
= hu
->priv
;
1004 struct sk_buff
*skb
;
1006 /* These power pulses are single byte command which are sent
1007 * at required baudrate to wcn3990. On wcn3990, we have an external
1008 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1009 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1010 * and also we use the same power inputs to turn on and off for
1011 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1012 * we send a power on pulse at 115200 bps. This algorithm will help to
1013 * save power. Disabling hardware flow control is mandatory while
1014 * sending power pulses to SoC.
1016 bt_dev_dbg(hdev
, "sending power pulse %02x to SoC", cmd
);
1018 skb
= bt_skb_alloc(sizeof(cmd
), GFP_KERNEL
);
1022 hci_uart_set_flow_control(hu
, true);
1024 skb_put_u8(skb
, cmd
);
1025 hci_skb_pkt_type(skb
) = HCI_COMMAND_PKT
;
1027 skb_queue_tail(&qca
->txq
, skb
);
1028 hci_uart_tx_wakeup(hu
);
1030 /* Wait for 100 uS for SoC to settle down */
1031 usleep_range(100, 200);
1032 hci_uart_set_flow_control(hu
, false);
1037 static unsigned int qca_get_speed(struct hci_uart
*hu
,
1038 enum qca_speed_type speed_type
)
1040 unsigned int speed
= 0;
1042 if (speed_type
== QCA_INIT_SPEED
) {
1044 speed
= hu
->init_speed
;
1045 else if (hu
->proto
->init_speed
)
1046 speed
= hu
->proto
->init_speed
;
1049 speed
= hu
->oper_speed
;
1050 else if (hu
->proto
->oper_speed
)
1051 speed
= hu
->proto
->oper_speed
;
1057 static int qca_check_speeds(struct hci_uart
*hu
)
1059 struct qca_serdev
*qcadev
;
1061 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1062 if (qcadev
->btsoc_type
== QCA_WCN3990
) {
1063 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) &&
1064 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1067 if (!qca_get_speed(hu
, QCA_INIT_SPEED
) ||
1068 !qca_get_speed(hu
, QCA_OPER_SPEED
))
1075 static int qca_set_speed(struct hci_uart
*hu
, enum qca_speed_type speed_type
)
1077 unsigned int speed
, qca_baudrate
;
1080 if (speed_type
== QCA_INIT_SPEED
) {
1081 speed
= qca_get_speed(hu
, QCA_INIT_SPEED
);
1083 host_set_baudrate(hu
, speed
);
1085 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1089 qca_baudrate
= qca_get_baudrate_value(speed
);
1090 bt_dev_dbg(hu
->hdev
, "Set UART speed to %d", speed
);
1091 ret
= qca_set_baudrate(hu
->hdev
, qca_baudrate
);
1095 host_set_baudrate(hu
, speed
);
1101 static int qca_wcn3990_init(struct hci_uart
*hu
)
1103 struct hci_dev
*hdev
= hu
->hdev
;
1106 /* Forcefully enable wcn3990 to enter in to boot mode. */
1107 host_set_baudrate(hu
, 2400);
1108 ret
= qca_send_power_pulse(hdev
, QCA_WCN3990_POWEROFF_PULSE
);
1112 qca_set_speed(hu
, QCA_INIT_SPEED
);
1113 ret
= qca_send_power_pulse(hdev
, QCA_WCN3990_POWERON_PULSE
);
1117 /* Wait for 100 ms for SoC to boot */
1120 /* Now the device is in ready state to communicate with host.
1121 * To sync host with device we need to reopen port.
1122 * Without this, we will have RTS and CTS synchronization
1125 serdev_device_close(hu
->serdev
);
1126 ret
= serdev_device_open(hu
->serdev
);
1128 bt_dev_err(hu
->hdev
, "failed to open port");
1132 hci_uart_set_flow_control(hu
, false);
1137 static int qca_setup(struct hci_uart
*hu
)
1139 struct hci_dev
*hdev
= hu
->hdev
;
1140 struct qca_data
*qca
= hu
->priv
;
1141 unsigned int speed
, qca_baudrate
= QCA_BAUDRATE_115200
;
1142 struct qca_serdev
*qcadev
;
1146 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1148 ret
= qca_check_speeds(hu
);
1152 /* Patch downloading has to be done without IBS mode */
1153 clear_bit(STATE_IN_BAND_SLEEP_ENABLED
, &qca
->flags
);
1155 if (qcadev
->btsoc_type
== QCA_WCN3990
) {
1156 bt_dev_info(hdev
, "setting up wcn3990");
1157 ret
= qca_wcn3990_init(hu
);
1161 ret
= qca_read_soc_version(hdev
, &soc_ver
);
1165 bt_dev_info(hdev
, "ROME setup");
1166 qca_set_speed(hu
, QCA_INIT_SPEED
);
1169 /* Setup user speed if needed */
1170 speed
= qca_get_speed(hu
, QCA_OPER_SPEED
);
1172 ret
= qca_set_speed(hu
, QCA_OPER_SPEED
);
1176 qca_baudrate
= qca_get_baudrate_value(speed
);
1179 if (qcadev
->btsoc_type
!= QCA_WCN3990
) {
1180 /* Get QCA version information */
1181 ret
= qca_read_soc_version(hdev
, &soc_ver
);
1186 bt_dev_info(hdev
, "QCA controller version 0x%08x", soc_ver
);
1187 /* Setup patch / NVM configurations */
1188 ret
= qca_uart_setup(hdev
, qca_baudrate
, qcadev
->btsoc_type
, soc_ver
);
1190 set_bit(STATE_IN_BAND_SLEEP_ENABLED
, &qca
->flags
);
1191 qca_debugfs_init(hdev
);
1192 } else if (ret
== -ENOENT
) {
1193 /* No patch/nvm-config found, run with original fw/config */
1195 } else if (ret
== -EAGAIN
) {
1197 * Userspace firmware loader will return -EAGAIN in case no
1198 * patch/nvm-config is found, so run with original fw/config.
1204 hu
->hdev
->set_bdaddr
= qca_set_bdaddr_rome
;
1209 static struct hci_uart_proto qca_proto
= {
1213 .init_speed
= 115200,
1214 .oper_speed
= 3000000,
1220 .enqueue
= qca_enqueue
,
1221 .dequeue
= qca_dequeue
,
1224 static const struct qca_vreg_data qca_soc_data
= {
1225 .soc_type
= QCA_WCN3990
,
1226 .vregs
= (struct qca_vreg
[]) {
1227 { "vddio", 1800000, 1900000, 15000 },
1228 { "vddxo", 1800000, 1900000, 80000 },
1229 { "vddrf", 1300000, 1350000, 300000 },
1230 { "vddch0", 3300000, 3400000, 450000 },
1235 static void qca_power_shutdown(struct hci_dev
*hdev
)
1237 struct hci_uart
*hu
= hci_get_drvdata(hdev
);
1239 host_set_baudrate(hu
, 2400);
1240 qca_send_power_pulse(hdev
, QCA_WCN3990_POWEROFF_PULSE
);
1241 qca_power_setup(hu
, false);
1244 static int qca_enable_regulator(struct qca_vreg vregs
,
1245 struct regulator
*regulator
)
1249 ret
= regulator_set_voltage(regulator
, vregs
.min_uV
,
1255 ret
= regulator_set_load(regulator
,
1261 return regulator_enable(regulator
);
1265 static void qca_disable_regulator(struct qca_vreg vregs
,
1266 struct regulator
*regulator
)
1268 regulator_disable(regulator
);
1269 regulator_set_voltage(regulator
, 0, vregs
.max_uV
);
1271 regulator_set_load(regulator
, 0);
1275 static int qca_power_setup(struct hci_uart
*hu
, bool on
)
1277 struct qca_vreg
*vregs
;
1278 struct regulator_bulk_data
*vreg_bulk
;
1279 struct qca_serdev
*qcadev
;
1280 int i
, num_vregs
, ret
= 0;
1282 qcadev
= serdev_device_get_drvdata(hu
->serdev
);
1283 if (!qcadev
|| !qcadev
->bt_power
|| !qcadev
->bt_power
->vreg_data
||
1284 !qcadev
->bt_power
->vreg_bulk
)
1287 vregs
= qcadev
->bt_power
->vreg_data
->vregs
;
1288 vreg_bulk
= qcadev
->bt_power
->vreg_bulk
;
1289 num_vregs
= qcadev
->bt_power
->vreg_data
->num_vregs
;
1290 BT_DBG("on: %d", on
);
1291 if (on
&& !qcadev
->bt_power
->vregs_on
) {
1292 for (i
= 0; i
< num_vregs
; i
++) {
1293 ret
= qca_enable_regulator(vregs
[i
],
1294 vreg_bulk
[i
].consumer
);
1300 BT_ERR("failed to enable regulator:%s", vregs
[i
].name
);
1301 /* turn off regulators which are enabled */
1302 for (i
= i
- 1; i
>= 0; i
--)
1303 qca_disable_regulator(vregs
[i
],
1304 vreg_bulk
[i
].consumer
);
1306 qcadev
->bt_power
->vregs_on
= true;
1308 } else if (!on
&& qcadev
->bt_power
->vregs_on
) {
1309 /* turn off regulator in reverse order */
1310 i
= qcadev
->bt_power
->vreg_data
->num_vregs
- 1;
1311 for ( ; i
>= 0; i
--)
1312 qca_disable_regulator(vregs
[i
], vreg_bulk
[i
].consumer
);
1314 qcadev
->bt_power
->vregs_on
= false;
1320 static int qca_init_regulators(struct qca_power
*qca
,
1321 const struct qca_vreg
*vregs
, size_t num_vregs
)
1325 qca
->vreg_bulk
= devm_kzalloc(qca
->dev
, num_vregs
*
1326 sizeof(struct regulator_bulk_data
),
1328 if (!qca
->vreg_bulk
)
1331 for (i
= 0; i
< num_vregs
; i
++)
1332 qca
->vreg_bulk
[i
].supply
= vregs
[i
].name
;
1334 return devm_regulator_bulk_get(qca
->dev
, num_vregs
, qca
->vreg_bulk
);
1337 static int qca_serdev_probe(struct serdev_device
*serdev
)
1339 struct qca_serdev
*qcadev
;
1340 const struct qca_vreg_data
*data
;
1343 qcadev
= devm_kzalloc(&serdev
->dev
, sizeof(*qcadev
), GFP_KERNEL
);
1347 qcadev
->serdev_hu
.serdev
= serdev
;
1348 data
= of_device_get_match_data(&serdev
->dev
);
1349 serdev_device_set_drvdata(serdev
, qcadev
);
1350 if (data
&& data
->soc_type
== QCA_WCN3990
) {
1351 qcadev
->btsoc_type
= QCA_WCN3990
;
1352 qcadev
->bt_power
= devm_kzalloc(&serdev
->dev
,
1353 sizeof(struct qca_power
),
1355 if (!qcadev
->bt_power
)
1358 qcadev
->bt_power
->dev
= &serdev
->dev
;
1359 qcadev
->bt_power
->vreg_data
= data
;
1360 err
= qca_init_regulators(qcadev
->bt_power
, data
->vregs
,
1363 BT_ERR("Failed to init regulators:%d", err
);
1367 qcadev
->bt_power
->vregs_on
= false;
1369 device_property_read_u32(&serdev
->dev
, "max-speed",
1370 &qcadev
->oper_speed
);
1371 if (!qcadev
->oper_speed
)
1372 BT_DBG("UART will pick default operating speed");
1374 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1376 BT_ERR("wcn3990 serdev registration failed");
1380 qcadev
->btsoc_type
= QCA_ROME
;
1381 qcadev
->bt_en
= devm_gpiod_get(&serdev
->dev
, "enable",
1383 if (IS_ERR(qcadev
->bt_en
)) {
1384 dev_err(&serdev
->dev
, "failed to acquire enable gpio\n");
1385 return PTR_ERR(qcadev
->bt_en
);
1388 qcadev
->susclk
= devm_clk_get(&serdev
->dev
, NULL
);
1389 if (IS_ERR(qcadev
->susclk
)) {
1390 dev_err(&serdev
->dev
, "failed to acquire clk\n");
1391 return PTR_ERR(qcadev
->susclk
);
1394 err
= clk_set_rate(qcadev
->susclk
, SUSCLK_RATE_32KHZ
);
1398 err
= clk_prepare_enable(qcadev
->susclk
);
1402 err
= hci_uart_register_device(&qcadev
->serdev_hu
, &qca_proto
);
1404 clk_disable_unprepare(qcadev
->susclk
);
1411 static void qca_serdev_remove(struct serdev_device
*serdev
)
1413 struct qca_serdev
*qcadev
= serdev_device_get_drvdata(serdev
);
1415 if (qcadev
->btsoc_type
== QCA_WCN3990
)
1416 qca_power_shutdown(qcadev
->serdev_hu
.hdev
);
1418 clk_disable_unprepare(qcadev
->susclk
);
1420 hci_uart_unregister_device(&qcadev
->serdev_hu
);
1423 static const struct of_device_id qca_bluetooth_of_match
[] = {
1424 { .compatible
= "qcom,qca6174-bt" },
1425 { .compatible
= "qcom,wcn3990-bt", .data
= &qca_soc_data
},
1428 MODULE_DEVICE_TABLE(of
, qca_bluetooth_of_match
);
1430 static struct serdev_device_driver qca_serdev_driver
= {
1431 .probe
= qca_serdev_probe
,
1432 .remove
= qca_serdev_remove
,
1434 .name
= "hci_uart_qca",
1435 .of_match_table
= qca_bluetooth_of_match
,
1439 int __init
qca_init(void)
1441 serdev_device_driver_register(&qca_serdev_driver
);
1443 return hci_uart_register_proto(&qca_proto
);
1446 int __exit
qca_deinit(void)
1448 serdev_device_driver_unregister(&qca_serdev_driver
);
1450 return hci_uart_unregister_proto(&qca_proto
);