1 // SPDX-License-Identifier: GPL-2.0-only
3 * Bluetooth Software UART Qualcomm protocol
5 * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management
6 * protocol extension to H4.
8 * Copyright (C) 2007 Texas Instruments, Inc.
9 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
12 * This file is based on hci_ll.c, which was...
13 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
14 * which was in turn based on hci_h4.c, which was written
15 * by Maxim Krasnyansky and Marcel Holtmann.
18 #include <linux/kernel.h>
19 #include <linux/clk.h>
20 #include <linux/completion.h>
21 #include <linux/debugfs.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/mod_devicetable.h>
26 #include <linux/module.h>
27 #include <linux/of_device.h>
28 #include <linux/platform_device.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/serdev.h>
31 #include <asm/unaligned.h>
33 #include <net/bluetooth/bluetooth.h>
34 #include <net/bluetooth/hci_core.h>
39 /* HCI_IBS protocol messages */
40 #define HCI_IBS_SLEEP_IND 0xFE
41 #define HCI_IBS_WAKE_IND 0xFD
42 #define HCI_IBS_WAKE_ACK 0xFC
43 #define HCI_MAX_IBS_SIZE 10
45 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
46 #define IBS_TX_IDLE_TIMEOUT_MS 2000
47 #define CMD_TRANS_TIMEOUT_MS 100
50 #define SUSCLK_RATE_32KHZ 32768
52 /* Controller debug log header */
53 #define QCA_DEBUG_HANDLE 0x2EDC
57 QCA_DROP_VENDOR_EVENT,
60 /* HCI_IBS transmit side sleep protocol states */
67 /* HCI_IBS receive side sleep protocol states */
73 /* HCI_IBS transmit and receive side clock state vote */
74 enum hci_ibs_clock_state_vote {
75 HCI_IBS_VOTE_STATS_UPDATE,
76 HCI_IBS_TX_VOTE_CLOCK_ON,
77 HCI_IBS_TX_VOTE_CLOCK_OFF,
78 HCI_IBS_RX_VOTE_CLOCK_ON,
79 HCI_IBS_RX_VOTE_CLOCK_OFF,
84 struct sk_buff *rx_skb;
85 struct sk_buff_head txq;
86 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
87 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
88 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
89 u8 rx_ibs_state; /* HCI_IBS receive side power state */
90 bool tx_vote; /* Clock must be on for TX */
91 bool rx_vote; /* Clock must be on for RX */
92 struct timer_list tx_idle_timer;
94 struct timer_list wake_retrans_timer;
96 struct workqueue_struct *workqueue;
97 struct work_struct ws_awake_rx;
98 struct work_struct ws_awake_device;
99 struct work_struct ws_rx_vote_off;
100 struct work_struct ws_tx_vote_off;
102 struct completion drop_ev_comp;
104 /* For debugging purpose */
122 enum qca_speed_type {
128 * Voltage regulator information required for configuring the
129 * QCA Bluetooth chipset
135 unsigned int load_uA;
138 struct qca_vreg_data {
139 enum qca_btsoc_type soc_type;
140 struct qca_vreg *vregs;
145 * Platform data for the QCA Bluetooth power driver.
149 const struct qca_vreg_data *vreg_data;
150 struct regulator_bulk_data *vreg_bulk;
155 struct hci_uart serdev_hu;
156 struct gpio_desc *bt_en;
158 enum qca_btsoc_type btsoc_type;
159 struct qca_power *bt_power;
162 const char *firmware_name;
165 static int qca_power_setup(struct hci_uart *hu, bool on);
166 static void qca_power_shutdown(struct hci_uart *hu);
167 static int qca_power_off(struct hci_dev *hdev);
169 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
171 enum qca_btsoc_type soc_type;
174 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
176 soc_type = qsd->btsoc_type;
184 static const char *qca_get_firmware_name(struct hci_uart *hu)
187 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
189 return qsd->firmware_name;
195 static void __serial_clock_on(struct tty_struct *tty)
197 /* TODO: Some chipset requires to enable UART clock on client
198 * side to save power consumption or manual work is required.
199 * Please put your code to control UART clock here if needed
203 static void __serial_clock_off(struct tty_struct *tty)
205 /* TODO: Some chipset requires to disable UART clock on client
206 * side to save power consumption or manual work is required.
207 * Please put your code to control UART clock off here if needed
211 /* serial_clock_vote needs to be called with the ibs lock held */
212 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
214 struct qca_data *qca = hu->priv;
217 bool old_vote = (qca->tx_vote | qca->rx_vote);
221 case HCI_IBS_VOTE_STATS_UPDATE:
222 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
225 qca->vote_off_ms += diff;
227 qca->vote_on_ms += diff;
230 case HCI_IBS_TX_VOTE_CLOCK_ON:
236 case HCI_IBS_RX_VOTE_CLOCK_ON:
242 case HCI_IBS_TX_VOTE_CLOCK_OFF:
243 qca->tx_vote = false;
245 new_vote = qca->rx_vote | qca->tx_vote;
248 case HCI_IBS_RX_VOTE_CLOCK_OFF:
249 qca->rx_vote = false;
251 new_vote = qca->rx_vote | qca->tx_vote;
255 BT_ERR("Voting irregularity");
259 if (new_vote != old_vote) {
261 __serial_clock_on(hu->tty);
263 __serial_clock_off(hu->tty);
265 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
266 vote ? "true" : "false");
268 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
272 qca->vote_off_ms += diff;
275 qca->vote_on_ms += diff;
277 qca->vote_last_jif = jiffies;
281 /* Builds and sends an HCI_IBS command packet.
282 * These are very simple packets with only 1 cmd byte.
284 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
287 struct sk_buff *skb = NULL;
288 struct qca_data *qca = hu->priv;
290 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
292 skb = bt_skb_alloc(1, GFP_ATOMIC);
294 BT_ERR("Failed to allocate memory for HCI_IBS packet");
298 /* Assign HCI_IBS type */
299 skb_put_u8(skb, cmd);
301 skb_queue_tail(&qca->txq, skb);
306 static void qca_wq_awake_device(struct work_struct *work)
308 struct qca_data *qca = container_of(work, struct qca_data,
310 struct hci_uart *hu = qca->hu;
311 unsigned long retrans_delay;
313 BT_DBG("hu %p wq awake device", hu);
315 /* Vote for serial clock */
316 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
318 spin_lock(&qca->hci_ibs_lock);
320 /* Send wake indication to device */
321 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
322 BT_ERR("Failed to send WAKE to device");
324 qca->ibs_sent_wakes++;
326 /* Start retransmit timer */
327 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
328 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
330 spin_unlock(&qca->hci_ibs_lock);
332 /* Actually send the packets */
333 hci_uart_tx_wakeup(hu);
336 static void qca_wq_awake_rx(struct work_struct *work)
338 struct qca_data *qca = container_of(work, struct qca_data,
340 struct hci_uart *hu = qca->hu;
342 BT_DBG("hu %p wq awake rx", hu);
344 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
346 spin_lock(&qca->hci_ibs_lock);
347 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
349 /* Always acknowledge device wake up,
350 * sending IBS message doesn't count as TX ON.
352 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
353 BT_ERR("Failed to acknowledge device wake up");
355 qca->ibs_sent_wacks++;
357 spin_unlock(&qca->hci_ibs_lock);
359 /* Actually send the packets */
360 hci_uart_tx_wakeup(hu);
363 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
365 struct qca_data *qca = container_of(work, struct qca_data,
367 struct hci_uart *hu = qca->hu;
369 BT_DBG("hu %p rx clock vote off", hu);
371 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
374 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
376 struct qca_data *qca = container_of(work, struct qca_data,
378 struct hci_uart *hu = qca->hu;
380 BT_DBG("hu %p tx clock vote off", hu);
382 /* Run HCI tx handling unlocked */
383 hci_uart_tx_wakeup(hu);
385 /* Now that message queued to tty driver, vote for tty clocks off.
386 * It is up to the tty driver to pend the clocks off until tx done.
388 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
391 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
393 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
394 struct hci_uart *hu = qca->hu;
397 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
399 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
400 flags, SINGLE_DEPTH_NESTING);
402 switch (qca->tx_ibs_state) {
403 case HCI_IBS_TX_AWAKE:
404 /* TX_IDLE, go to SLEEP */
405 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
406 BT_ERR("Failed to send SLEEP to device");
409 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
410 qca->ibs_sent_slps++;
411 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
414 case HCI_IBS_TX_ASLEEP:
415 case HCI_IBS_TX_WAKING:
419 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
423 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
426 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
428 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
429 struct hci_uart *hu = qca->hu;
430 unsigned long flags, retrans_delay;
431 bool retransmit = false;
433 BT_DBG("hu %p wake retransmit timeout in %d state",
434 hu, qca->tx_ibs_state);
436 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
437 flags, SINGLE_DEPTH_NESTING);
439 switch (qca->tx_ibs_state) {
440 case HCI_IBS_TX_WAKING:
441 /* No WAKE_ACK, retransmit WAKE */
443 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
444 BT_ERR("Failed to acknowledge device wake up");
447 qca->ibs_sent_wakes++;
448 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
449 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
452 case HCI_IBS_TX_ASLEEP:
453 case HCI_IBS_TX_AWAKE:
457 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
461 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
464 hci_uart_tx_wakeup(hu);
467 /* Initialize protocol */
468 static int qca_open(struct hci_uart *hu)
470 struct qca_serdev *qcadev;
471 struct qca_data *qca;
474 BT_DBG("hu %p qca_open", hu);
476 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
480 skb_queue_head_init(&qca->txq);
481 skb_queue_head_init(&qca->tx_wait_q);
482 spin_lock_init(&qca->hci_ibs_lock);
483 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
484 if (!qca->workqueue) {
485 BT_ERR("QCA Workqueue not initialized properly");
490 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
491 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
492 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
493 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
496 init_completion(&qca->drop_ev_comp);
498 /* Assume we start with both sides asleep -- extra wakes OK */
499 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
500 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
502 /* clocks actually on, but we start votes off */
503 qca->tx_vote = false;
504 qca->rx_vote = false;
507 qca->ibs_sent_wacks = 0;
508 qca->ibs_sent_slps = 0;
509 qca->ibs_sent_wakes = 0;
510 qca->ibs_recv_wacks = 0;
511 qca->ibs_recv_slps = 0;
512 qca->ibs_recv_wakes = 0;
513 qca->vote_last_jif = jiffies;
515 qca->vote_off_ms = 0;
518 qca->tx_votes_on = 0;
519 qca->tx_votes_off = 0;
520 qca->rx_votes_on = 0;
521 qca->rx_votes_off = 0;
527 qcadev = serdev_device_get_drvdata(hu->serdev);
528 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
529 gpiod_set_value_cansleep(qcadev->bt_en, 1);
530 /* Controller needs time to bootup. */
533 hu->init_speed = qcadev->init_speed;
534 hu->oper_speed = qcadev->oper_speed;
535 ret = qca_power_setup(hu, true);
537 destroy_workqueue(qca->workqueue);
538 kfree_skb(qca->rx_skb);
546 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
547 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
549 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
550 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
552 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
553 qca->tx_idle_delay, qca->wake_retrans);
558 static void qca_debugfs_init(struct hci_dev *hdev)
560 struct hci_uart *hu = hci_get_drvdata(hdev);
561 struct qca_data *qca = hu->priv;
562 struct dentry *ibs_dir;
568 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
572 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
573 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
574 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
575 &qca->ibs_sent_slps);
576 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
577 &qca->ibs_sent_wakes);
578 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
579 &qca->ibs_sent_wacks);
580 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
581 &qca->ibs_recv_slps);
582 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
583 &qca->ibs_recv_wakes);
584 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
585 &qca->ibs_recv_wacks);
586 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
587 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
588 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
589 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
590 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
591 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
592 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
593 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
594 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
595 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
598 mode = S_IRUGO | S_IWUSR;
599 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
600 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
601 &qca->tx_idle_delay);
604 /* Flush protocol data */
605 static int qca_flush(struct hci_uart *hu)
607 struct qca_data *qca = hu->priv;
609 BT_DBG("hu %p qca flush", hu);
611 skb_queue_purge(&qca->tx_wait_q);
612 skb_queue_purge(&qca->txq);
618 static int qca_close(struct hci_uart *hu)
620 struct qca_serdev *qcadev;
621 struct qca_data *qca = hu->priv;
623 BT_DBG("hu %p qca close", hu);
625 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
627 skb_queue_purge(&qca->tx_wait_q);
628 skb_queue_purge(&qca->txq);
629 del_timer(&qca->tx_idle_timer);
630 del_timer(&qca->wake_retrans_timer);
631 destroy_workqueue(qca->workqueue);
635 qcadev = serdev_device_get_drvdata(hu->serdev);
636 if (qca_is_wcn399x(qcadev->btsoc_type))
637 qca_power_shutdown(hu);
639 gpiod_set_value_cansleep(qcadev->bt_en, 0);
643 kfree_skb(qca->rx_skb);
652 /* Called upon a wake-up-indication from the device.
654 static void device_want_to_wakeup(struct hci_uart *hu)
657 struct qca_data *qca = hu->priv;
659 BT_DBG("hu %p want to wake up", hu);
661 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
663 qca->ibs_recv_wakes++;
665 switch (qca->rx_ibs_state) {
666 case HCI_IBS_RX_ASLEEP:
667 /* Make sure clock is on - we may have turned clock off since
668 * receiving the wake up indicator awake rx clock.
670 queue_work(qca->workqueue, &qca->ws_awake_rx);
671 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
674 case HCI_IBS_RX_AWAKE:
675 /* Always acknowledge device wake up,
676 * sending IBS message doesn't count as TX ON.
678 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
679 BT_ERR("Failed to acknowledge device wake up");
682 qca->ibs_sent_wacks++;
686 /* Any other state is illegal */
687 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
692 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
694 /* Actually send the packets */
695 hci_uart_tx_wakeup(hu);
698 /* Called upon a sleep-indication from the device.
700 static void device_want_to_sleep(struct hci_uart *hu)
703 struct qca_data *qca = hu->priv;
705 BT_DBG("hu %p want to sleep", hu);
707 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
709 qca->ibs_recv_slps++;
711 switch (qca->rx_ibs_state) {
712 case HCI_IBS_RX_AWAKE:
714 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
715 /* Vote off rx clock under workqueue */
716 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
719 case HCI_IBS_RX_ASLEEP:
723 /* Any other state is illegal */
724 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
729 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
732 /* Called upon wake-up-acknowledgement from the device
734 static void device_woke_up(struct hci_uart *hu)
736 unsigned long flags, idle_delay;
737 struct qca_data *qca = hu->priv;
738 struct sk_buff *skb = NULL;
740 BT_DBG("hu %p woke up", hu);
742 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
744 qca->ibs_recv_wacks++;
746 switch (qca->tx_ibs_state) {
747 case HCI_IBS_TX_AWAKE:
748 /* Expect one if we send 2 WAKEs */
749 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
753 case HCI_IBS_TX_WAKING:
754 /* Send pending packets */
755 while ((skb = skb_dequeue(&qca->tx_wait_q)))
756 skb_queue_tail(&qca->txq, skb);
758 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
759 del_timer(&qca->wake_retrans_timer);
760 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
761 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
762 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
765 case HCI_IBS_TX_ASLEEP:
769 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
774 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
776 /* Actually send the packets */
777 hci_uart_tx_wakeup(hu);
780 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
781 * two simultaneous tasklets.
783 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
785 unsigned long flags = 0, idle_delay;
786 struct qca_data *qca = hu->priv;
788 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
791 /* Prepend skb with frame type */
792 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
794 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
796 /* Don't go to sleep in middle of patch download or
797 * Out-Of-Band(GPIOs control) sleep is selected.
799 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) {
800 skb_queue_tail(&qca->txq, skb);
801 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
805 /* Act according to current state */
806 switch (qca->tx_ibs_state) {
807 case HCI_IBS_TX_AWAKE:
808 BT_DBG("Device awake, sending normally");
809 skb_queue_tail(&qca->txq, skb);
810 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
811 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
814 case HCI_IBS_TX_ASLEEP:
815 BT_DBG("Device asleep, waking up and queueing packet");
816 /* Save packet for later */
817 skb_queue_tail(&qca->tx_wait_q, skb);
819 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
820 /* Schedule a work queue to wake up device */
821 queue_work(qca->workqueue, &qca->ws_awake_device);
824 case HCI_IBS_TX_WAKING:
825 BT_DBG("Device waking up, queueing packet");
826 /* Transient state; just keep packet for later */
827 skb_queue_tail(&qca->tx_wait_q, skb);
831 BT_ERR("Illegal tx state: %d (losing packet)",
837 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
842 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
844 struct hci_uart *hu = hci_get_drvdata(hdev);
846 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
848 device_want_to_sleep(hu);
854 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
856 struct hci_uart *hu = hci_get_drvdata(hdev);
858 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
860 device_want_to_wakeup(hu);
866 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
868 struct hci_uart *hu = hci_get_drvdata(hdev);
870 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
878 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
880 /* We receive debug logs from chip as an ACL packets.
881 * Instead of sending the data to ACL to decode the
882 * received data, we are pushing them to the above layers
883 * as a diagnostic packet.
885 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
886 return hci_recv_diag(hdev, skb);
888 return hci_recv_frame(hdev, skb);
891 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
893 struct hci_uart *hu = hci_get_drvdata(hdev);
894 struct qca_data *qca = hu->priv;
896 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
897 struct hci_event_hdr *hdr = (void *)skb->data;
899 /* For the WCN3990 the vendor command for a baudrate change
900 * isn't sent as synchronous HCI command, because the
901 * controller sends the corresponding vendor event with the
902 * new baudrate. The event is received and properly decoded
903 * after changing the baudrate of the host port. It needs to
904 * be dropped, otherwise it can be misinterpreted as
905 * response to a later firmware download command (also a
909 if (hdr->evt == HCI_EV_VENDOR)
910 complete(&qca->drop_ev_comp);
917 return hci_recv_frame(hdev, skb);
920 #define QCA_IBS_SLEEP_IND_EVENT \
921 .type = HCI_IBS_SLEEP_IND, \
925 .maxlen = HCI_MAX_IBS_SIZE
927 #define QCA_IBS_WAKE_IND_EVENT \
928 .type = HCI_IBS_WAKE_IND, \
932 .maxlen = HCI_MAX_IBS_SIZE
934 #define QCA_IBS_WAKE_ACK_EVENT \
935 .type = HCI_IBS_WAKE_ACK, \
939 .maxlen = HCI_MAX_IBS_SIZE
941 static const struct h4_recv_pkt qca_recv_pkts[] = {
942 { H4_RECV_ACL, .recv = qca_recv_acl_data },
943 { H4_RECV_SCO, .recv = hci_recv_frame },
944 { H4_RECV_EVENT, .recv = qca_recv_event },
945 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
946 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
947 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
950 static int qca_recv(struct hci_uart *hu, const void *data, int count)
952 struct qca_data *qca = hu->priv;
954 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
957 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
958 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
959 if (IS_ERR(qca->rx_skb)) {
960 int err = PTR_ERR(qca->rx_skb);
961 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
969 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
971 struct qca_data *qca = hu->priv;
973 return skb_dequeue(&qca->txq);
976 static uint8_t qca_get_baudrate_value(int speed)
980 return QCA_BAUDRATE_9600;
982 return QCA_BAUDRATE_19200;
984 return QCA_BAUDRATE_38400;
986 return QCA_BAUDRATE_57600;
988 return QCA_BAUDRATE_115200;
990 return QCA_BAUDRATE_230400;
992 return QCA_BAUDRATE_460800;
994 return QCA_BAUDRATE_500000;
996 return QCA_BAUDRATE_921600;
998 return QCA_BAUDRATE_1000000;
1000 return QCA_BAUDRATE_2000000;
1002 return QCA_BAUDRATE_3000000;
1004 return QCA_BAUDRATE_3200000;
1006 return QCA_BAUDRATE_3500000;
1008 return QCA_BAUDRATE_115200;
1012 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1014 struct hci_uart *hu = hci_get_drvdata(hdev);
1015 struct qca_data *qca = hu->priv;
1016 struct sk_buff *skb;
1017 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1019 if (baudrate > QCA_BAUDRATE_3200000)
1024 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1026 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1030 /* Assign commands to change baudrate and packet type. */
1031 skb_put_data(skb, cmd, sizeof(cmd));
1032 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1034 skb_queue_tail(&qca->txq, skb);
1035 hci_uart_tx_wakeup(hu);
1037 /* Wait for the baudrate change request to be sent */
1039 while (!skb_queue_empty(&qca->txq))
1040 usleep_range(100, 200);
1043 serdev_device_wait_until_sent(hu->serdev,
1044 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1046 /* Give the controller time to process the request */
1047 if (qca_is_wcn399x(qca_soc_type(hu)))
1055 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1058 serdev_device_set_baudrate(hu->serdev, speed);
1060 hci_uart_set_baudrate(hu, speed);
1063 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1066 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1067 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1069 /* These power pulses are single byte command which are sent
1070 * at required baudrate to wcn3990. On wcn3990, we have an external
1071 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1072 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1073 * and also we use the same power inputs to turn on and off for
1074 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1075 * we send a power on pulse at 115200 bps. This algorithm will help to
1076 * save power. Disabling hardware flow control is mandatory while
1077 * sending power pulses to SoC.
1079 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1081 serdev_device_write_flush(hu->serdev);
1082 hci_uart_set_flow_control(hu, true);
1083 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1085 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1089 serdev_device_wait_until_sent(hu->serdev, timeout);
1090 hci_uart_set_flow_control(hu, false);
1092 /* Give to controller time to boot/shutdown */
1101 static unsigned int qca_get_speed(struct hci_uart *hu,
1102 enum qca_speed_type speed_type)
1104 unsigned int speed = 0;
1106 if (speed_type == QCA_INIT_SPEED) {
1108 speed = hu->init_speed;
1109 else if (hu->proto->init_speed)
1110 speed = hu->proto->init_speed;
1113 speed = hu->oper_speed;
1114 else if (hu->proto->oper_speed)
1115 speed = hu->proto->oper_speed;
1121 static int qca_check_speeds(struct hci_uart *hu)
1123 if (qca_is_wcn399x(qca_soc_type(hu))) {
1124 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1125 !qca_get_speed(hu, QCA_OPER_SPEED))
1128 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1129 !qca_get_speed(hu, QCA_OPER_SPEED))
1136 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1138 unsigned int speed, qca_baudrate;
1139 struct qca_data *qca = hu->priv;
1142 if (speed_type == QCA_INIT_SPEED) {
1143 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1145 host_set_baudrate(hu, speed);
1147 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1149 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1153 /* Disable flow control for wcn3990 to deassert RTS while
1154 * changing the baudrate of chip and host.
1156 if (qca_is_wcn399x(soc_type))
1157 hci_uart_set_flow_control(hu, true);
1159 if (soc_type == QCA_WCN3990) {
1160 reinit_completion(&qca->drop_ev_comp);
1161 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1164 qca_baudrate = qca_get_baudrate_value(speed);
1165 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1166 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1170 host_set_baudrate(hu, speed);
1173 if (qca_is_wcn399x(soc_type))
1174 hci_uart_set_flow_control(hu, false);
1176 if (soc_type == QCA_WCN3990) {
1177 /* Wait for the controller to send the vendor event
1178 * for the baudrate change command.
1180 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1181 msecs_to_jiffies(100))) {
1182 bt_dev_err(hu->hdev,
1183 "Failed to change controller baudrate\n");
1187 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1194 static int qca_wcn3990_init(struct hci_uart *hu)
1196 struct qca_serdev *qcadev;
1199 /* Check for vregs status, may be hci down has turned
1200 * off the voltage regulator.
1202 qcadev = serdev_device_get_drvdata(hu->serdev);
1203 if (!qcadev->bt_power->vregs_on) {
1204 serdev_device_close(hu->serdev);
1205 ret = qca_power_setup(hu, true);
1209 ret = serdev_device_open(hu->serdev);
1211 bt_dev_err(hu->hdev, "failed to open port");
1216 /* Forcefully enable wcn3990 to enter in to boot mode. */
1217 host_set_baudrate(hu, 2400);
1218 ret = qca_send_power_pulse(hu, false);
1222 qca_set_speed(hu, QCA_INIT_SPEED);
1223 ret = qca_send_power_pulse(hu, true);
1227 /* Now the device is in ready state to communicate with host.
1228 * To sync host with device we need to reopen port.
1229 * Without this, we will have RTS and CTS synchronization
1232 serdev_device_close(hu->serdev);
1233 ret = serdev_device_open(hu->serdev);
1235 bt_dev_err(hu->hdev, "failed to open port");
1239 hci_uart_set_flow_control(hu, false);
1244 static int qca_setup(struct hci_uart *hu)
1246 struct hci_dev *hdev = hu->hdev;
1247 struct qca_data *qca = hu->priv;
1248 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1249 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1250 const char *firmware_name = qca_get_firmware_name(hu);
1254 ret = qca_check_speeds(hu);
1258 /* Patch downloading has to be done without IBS mode */
1259 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1261 if (qca_is_wcn399x(soc_type)) {
1262 bt_dev_info(hdev, "setting up wcn3990");
1264 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1265 * setup for every hci up.
1267 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1268 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1269 hu->hdev->shutdown = qca_power_off;
1270 ret = qca_wcn3990_init(hu);
1274 ret = qca_read_soc_version(hdev, &soc_ver);
1278 bt_dev_info(hdev, "ROME setup");
1279 qca_set_speed(hu, QCA_INIT_SPEED);
1282 /* Setup user speed if needed */
1283 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1285 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1289 qca_baudrate = qca_get_baudrate_value(speed);
1292 if (!qca_is_wcn399x(soc_type)) {
1293 /* Get QCA version information */
1294 ret = qca_read_soc_version(hdev, &soc_ver);
1299 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1300 /* Setup patch / NVM configurations */
1301 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1304 set_bit(QCA_IBS_ENABLED, &qca->flags);
1305 qca_debugfs_init(hdev);
1306 } else if (ret == -ENOENT) {
1307 /* No patch/nvm-config found, run with original fw/config */
1309 } else if (ret == -EAGAIN) {
1311 * Userspace firmware loader will return -EAGAIN in case no
1312 * patch/nvm-config is found, so run with original fw/config.
1318 if (qca_is_wcn399x(soc_type))
1319 hu->hdev->set_bdaddr = qca_set_bdaddr;
1321 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1326 static struct hci_uart_proto qca_proto = {
1330 .init_speed = 115200,
1331 .oper_speed = 3000000,
1337 .enqueue = qca_enqueue,
1338 .dequeue = qca_dequeue,
1341 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1342 .soc_type = QCA_WCN3990,
1343 .vregs = (struct qca_vreg []) {
1344 { "vddio", 1800000, 1900000, 15000 },
1345 { "vddxo", 1800000, 1900000, 80000 },
1346 { "vddrf", 1300000, 1350000, 300000 },
1347 { "vddch0", 3300000, 3400000, 450000 },
1352 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1353 .soc_type = QCA_WCN3998,
1354 .vregs = (struct qca_vreg []) {
1355 { "vddio", 1800000, 1900000, 10000 },
1356 { "vddxo", 1800000, 1900000, 80000 },
1357 { "vddrf", 1300000, 1352000, 300000 },
1358 { "vddch0", 3300000, 3300000, 450000 },
1363 static void qca_power_shutdown(struct hci_uart *hu)
1365 struct qca_data *qca = hu->priv;
1366 unsigned long flags;
1368 /* From this point we go into power off state. But serial port is
1369 * still open, stop queueing the IBS data and flush all the buffered
1372 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1373 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1375 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1377 host_set_baudrate(hu, 2400);
1378 qca_send_power_pulse(hu, false);
1379 qca_power_setup(hu, false);
1382 static int qca_power_off(struct hci_dev *hdev)
1384 struct hci_uart *hu = hci_get_drvdata(hdev);
1386 qca_power_shutdown(hu);
1390 static int qca_enable_regulator(struct qca_vreg vregs,
1391 struct regulator *regulator)
1395 ret = regulator_set_voltage(regulator, vregs.min_uV,
1401 ret = regulator_set_load(regulator,
1407 return regulator_enable(regulator);
1411 static void qca_disable_regulator(struct qca_vreg vregs,
1412 struct regulator *regulator)
1414 regulator_disable(regulator);
1415 regulator_set_voltage(regulator, 0, vregs.max_uV);
1417 regulator_set_load(regulator, 0);
1421 static int qca_power_setup(struct hci_uart *hu, bool on)
1423 struct qca_vreg *vregs;
1424 struct regulator_bulk_data *vreg_bulk;
1425 struct qca_serdev *qcadev;
1426 int i, num_vregs, ret = 0;
1428 qcadev = serdev_device_get_drvdata(hu->serdev);
1429 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1430 !qcadev->bt_power->vreg_bulk)
1433 vregs = qcadev->bt_power->vreg_data->vregs;
1434 vreg_bulk = qcadev->bt_power->vreg_bulk;
1435 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1436 BT_DBG("on: %d", on);
1437 if (on && !qcadev->bt_power->vregs_on) {
1438 for (i = 0; i < num_vregs; i++) {
1439 ret = qca_enable_regulator(vregs[i],
1440 vreg_bulk[i].consumer);
1446 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1447 /* turn off regulators which are enabled */
1448 for (i = i - 1; i >= 0; i--)
1449 qca_disable_regulator(vregs[i],
1450 vreg_bulk[i].consumer);
1452 qcadev->bt_power->vregs_on = true;
1454 } else if (!on && qcadev->bt_power->vregs_on) {
1455 /* turn off regulator in reverse order */
1456 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1457 for ( ; i >= 0; i--)
1458 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1460 qcadev->bt_power->vregs_on = false;
1466 static int qca_init_regulators(struct qca_power *qca,
1467 const struct qca_vreg *vregs, size_t num_vregs)
1471 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
1472 sizeof(struct regulator_bulk_data),
1474 if (!qca->vreg_bulk)
1477 for (i = 0; i < num_vregs; i++)
1478 qca->vreg_bulk[i].supply = vregs[i].name;
1480 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1483 static int qca_serdev_probe(struct serdev_device *serdev)
1485 struct qca_serdev *qcadev;
1486 const struct qca_vreg_data *data;
1489 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1493 qcadev->serdev_hu.serdev = serdev;
1494 data = of_device_get_match_data(&serdev->dev);
1495 serdev_device_set_drvdata(serdev, qcadev);
1496 device_property_read_string(&serdev->dev, "firmware-name",
1497 &qcadev->firmware_name);
1498 if (data && qca_is_wcn399x(data->soc_type)) {
1499 qcadev->btsoc_type = data->soc_type;
1500 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1501 sizeof(struct qca_power),
1503 if (!qcadev->bt_power)
1506 qcadev->bt_power->dev = &serdev->dev;
1507 qcadev->bt_power->vreg_data = data;
1508 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1511 BT_ERR("Failed to init regulators:%d", err);
1515 qcadev->bt_power->vregs_on = false;
1517 device_property_read_u32(&serdev->dev, "max-speed",
1518 &qcadev->oper_speed);
1519 if (!qcadev->oper_speed)
1520 BT_DBG("UART will pick default operating speed");
1522 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1524 BT_ERR("wcn3990 serdev registration failed");
1528 qcadev->btsoc_type = QCA_ROME;
1529 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1531 if (IS_ERR(qcadev->bt_en)) {
1532 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1533 return PTR_ERR(qcadev->bt_en);
1536 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1537 if (IS_ERR(qcadev->susclk)) {
1538 dev_err(&serdev->dev, "failed to acquire clk\n");
1539 return PTR_ERR(qcadev->susclk);
1542 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1546 err = clk_prepare_enable(qcadev->susclk);
1550 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1552 clk_disable_unprepare(qcadev->susclk);
1559 static void qca_serdev_remove(struct serdev_device *serdev)
1561 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1563 if (qca_is_wcn399x(qcadev->btsoc_type))
1564 qca_power_shutdown(&qcadev->serdev_hu);
1566 clk_disable_unprepare(qcadev->susclk);
1568 hci_uart_unregister_device(&qcadev->serdev_hu);
1571 static const struct of_device_id qca_bluetooth_of_match[] = {
1572 { .compatible = "qcom,qca6174-bt" },
1573 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1574 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1577 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1579 static struct serdev_device_driver qca_serdev_driver = {
1580 .probe = qca_serdev_probe,
1581 .remove = qca_serdev_remove,
1583 .name = "hci_uart_qca",
1584 .of_match_table = qca_bluetooth_of_match,
1588 int __init qca_init(void)
1590 serdev_device_driver_register(&qca_serdev_driver);
1592 return hci_uart_register_proto(&qca_proto);
1595 int __exit qca_deinit(void)
1597 serdev_device_driver_unregister(&qca_serdev_driver);
1599 return hci_uart_unregister_proto(&qca_proto);