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 if (!hci_uart_has_flow_control(hu))
479 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
483 skb_queue_head_init(&qca->txq);
484 skb_queue_head_init(&qca->tx_wait_q);
485 spin_lock_init(&qca->hci_ibs_lock);
486 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
487 if (!qca->workqueue) {
488 BT_ERR("QCA Workqueue not initialized properly");
493 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
494 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
495 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
496 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
499 init_completion(&qca->drop_ev_comp);
501 /* Assume we start with both sides asleep -- extra wakes OK */
502 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
503 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
505 /* clocks actually on, but we start votes off */
506 qca->tx_vote = false;
507 qca->rx_vote = false;
510 qca->ibs_sent_wacks = 0;
511 qca->ibs_sent_slps = 0;
512 qca->ibs_sent_wakes = 0;
513 qca->ibs_recv_wacks = 0;
514 qca->ibs_recv_slps = 0;
515 qca->ibs_recv_wakes = 0;
516 qca->vote_last_jif = jiffies;
518 qca->vote_off_ms = 0;
521 qca->tx_votes_on = 0;
522 qca->tx_votes_off = 0;
523 qca->rx_votes_on = 0;
524 qca->rx_votes_off = 0;
530 qcadev = serdev_device_get_drvdata(hu->serdev);
531 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
532 gpiod_set_value_cansleep(qcadev->bt_en, 1);
533 /* Controller needs time to bootup. */
536 hu->init_speed = qcadev->init_speed;
537 hu->oper_speed = qcadev->oper_speed;
538 ret = qca_power_setup(hu, true);
540 destroy_workqueue(qca->workqueue);
541 kfree_skb(qca->rx_skb);
549 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
550 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
552 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
553 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
555 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
556 qca->tx_idle_delay, qca->wake_retrans);
561 static void qca_debugfs_init(struct hci_dev *hdev)
563 struct hci_uart *hu = hci_get_drvdata(hdev);
564 struct qca_data *qca = hu->priv;
565 struct dentry *ibs_dir;
571 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
575 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
576 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
577 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
578 &qca->ibs_sent_slps);
579 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
580 &qca->ibs_sent_wakes);
581 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
582 &qca->ibs_sent_wacks);
583 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
584 &qca->ibs_recv_slps);
585 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
586 &qca->ibs_recv_wakes);
587 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
588 &qca->ibs_recv_wacks);
589 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
590 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
591 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
592 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
593 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
594 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
595 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
596 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
597 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
598 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
601 mode = S_IRUGO | S_IWUSR;
602 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
603 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
604 &qca->tx_idle_delay);
607 /* Flush protocol data */
608 static int qca_flush(struct hci_uart *hu)
610 struct qca_data *qca = hu->priv;
612 BT_DBG("hu %p qca flush", hu);
614 skb_queue_purge(&qca->tx_wait_q);
615 skb_queue_purge(&qca->txq);
621 static int qca_close(struct hci_uart *hu)
623 struct qca_serdev *qcadev;
624 struct qca_data *qca = hu->priv;
626 BT_DBG("hu %p qca close", hu);
628 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
630 skb_queue_purge(&qca->tx_wait_q);
631 skb_queue_purge(&qca->txq);
632 del_timer(&qca->tx_idle_timer);
633 del_timer(&qca->wake_retrans_timer);
634 destroy_workqueue(qca->workqueue);
638 qcadev = serdev_device_get_drvdata(hu->serdev);
639 if (qca_is_wcn399x(qcadev->btsoc_type))
640 qca_power_shutdown(hu);
642 gpiod_set_value_cansleep(qcadev->bt_en, 0);
646 kfree_skb(qca->rx_skb);
655 /* Called upon a wake-up-indication from the device.
657 static void device_want_to_wakeup(struct hci_uart *hu)
660 struct qca_data *qca = hu->priv;
662 BT_DBG("hu %p want to wake up", hu);
664 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
666 qca->ibs_recv_wakes++;
668 switch (qca->rx_ibs_state) {
669 case HCI_IBS_RX_ASLEEP:
670 /* Make sure clock is on - we may have turned clock off since
671 * receiving the wake up indicator awake rx clock.
673 queue_work(qca->workqueue, &qca->ws_awake_rx);
674 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
677 case HCI_IBS_RX_AWAKE:
678 /* Always acknowledge device wake up,
679 * sending IBS message doesn't count as TX ON.
681 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
682 BT_ERR("Failed to acknowledge device wake up");
685 qca->ibs_sent_wacks++;
689 /* Any other state is illegal */
690 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
695 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
697 /* Actually send the packets */
698 hci_uart_tx_wakeup(hu);
701 /* Called upon a sleep-indication from the device.
703 static void device_want_to_sleep(struct hci_uart *hu)
706 struct qca_data *qca = hu->priv;
708 BT_DBG("hu %p want to sleep", hu);
710 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
712 qca->ibs_recv_slps++;
714 switch (qca->rx_ibs_state) {
715 case HCI_IBS_RX_AWAKE:
717 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
718 /* Vote off rx clock under workqueue */
719 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
722 case HCI_IBS_RX_ASLEEP:
726 /* Any other state is illegal */
727 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
732 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
735 /* Called upon wake-up-acknowledgement from the device
737 static void device_woke_up(struct hci_uart *hu)
739 unsigned long flags, idle_delay;
740 struct qca_data *qca = hu->priv;
741 struct sk_buff *skb = NULL;
743 BT_DBG("hu %p woke up", hu);
745 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
747 qca->ibs_recv_wacks++;
749 switch (qca->tx_ibs_state) {
750 case HCI_IBS_TX_AWAKE:
751 /* Expect one if we send 2 WAKEs */
752 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
756 case HCI_IBS_TX_WAKING:
757 /* Send pending packets */
758 while ((skb = skb_dequeue(&qca->tx_wait_q)))
759 skb_queue_tail(&qca->txq, skb);
761 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
762 del_timer(&qca->wake_retrans_timer);
763 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
764 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
765 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
768 case HCI_IBS_TX_ASLEEP:
772 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
777 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
779 /* Actually send the packets */
780 hci_uart_tx_wakeup(hu);
783 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
784 * two simultaneous tasklets.
786 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
788 unsigned long flags = 0, idle_delay;
789 struct qca_data *qca = hu->priv;
791 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
794 /* Prepend skb with frame type */
795 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
797 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
799 /* Don't go to sleep in middle of patch download or
800 * Out-Of-Band(GPIOs control) sleep is selected.
802 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) {
803 skb_queue_tail(&qca->txq, skb);
804 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
808 /* Act according to current state */
809 switch (qca->tx_ibs_state) {
810 case HCI_IBS_TX_AWAKE:
811 BT_DBG("Device awake, sending normally");
812 skb_queue_tail(&qca->txq, skb);
813 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
814 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
817 case HCI_IBS_TX_ASLEEP:
818 BT_DBG("Device asleep, waking up and queueing packet");
819 /* Save packet for later */
820 skb_queue_tail(&qca->tx_wait_q, skb);
822 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
823 /* Schedule a work queue to wake up device */
824 queue_work(qca->workqueue, &qca->ws_awake_device);
827 case HCI_IBS_TX_WAKING:
828 BT_DBG("Device waking up, queueing packet");
829 /* Transient state; just keep packet for later */
830 skb_queue_tail(&qca->tx_wait_q, skb);
834 BT_ERR("Illegal tx state: %d (losing packet)",
840 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
845 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
847 struct hci_uart *hu = hci_get_drvdata(hdev);
849 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
851 device_want_to_sleep(hu);
857 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
859 struct hci_uart *hu = hci_get_drvdata(hdev);
861 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
863 device_want_to_wakeup(hu);
869 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
871 struct hci_uart *hu = hci_get_drvdata(hdev);
873 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
881 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
883 /* We receive debug logs from chip as an ACL packets.
884 * Instead of sending the data to ACL to decode the
885 * received data, we are pushing them to the above layers
886 * as a diagnostic packet.
888 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
889 return hci_recv_diag(hdev, skb);
891 return hci_recv_frame(hdev, skb);
894 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
896 struct hci_uart *hu = hci_get_drvdata(hdev);
897 struct qca_data *qca = hu->priv;
899 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
900 struct hci_event_hdr *hdr = (void *)skb->data;
902 /* For the WCN3990 the vendor command for a baudrate change
903 * isn't sent as synchronous HCI command, because the
904 * controller sends the corresponding vendor event with the
905 * new baudrate. The event is received and properly decoded
906 * after changing the baudrate of the host port. It needs to
907 * be dropped, otherwise it can be misinterpreted as
908 * response to a later firmware download command (also a
912 if (hdr->evt == HCI_EV_VENDOR)
913 complete(&qca->drop_ev_comp);
920 return hci_recv_frame(hdev, skb);
923 #define QCA_IBS_SLEEP_IND_EVENT \
924 .type = HCI_IBS_SLEEP_IND, \
928 .maxlen = HCI_MAX_IBS_SIZE
930 #define QCA_IBS_WAKE_IND_EVENT \
931 .type = HCI_IBS_WAKE_IND, \
935 .maxlen = HCI_MAX_IBS_SIZE
937 #define QCA_IBS_WAKE_ACK_EVENT \
938 .type = HCI_IBS_WAKE_ACK, \
942 .maxlen = HCI_MAX_IBS_SIZE
944 static const struct h4_recv_pkt qca_recv_pkts[] = {
945 { H4_RECV_ACL, .recv = qca_recv_acl_data },
946 { H4_RECV_SCO, .recv = hci_recv_frame },
947 { H4_RECV_EVENT, .recv = qca_recv_event },
948 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
949 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
950 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
953 static int qca_recv(struct hci_uart *hu, const void *data, int count)
955 struct qca_data *qca = hu->priv;
957 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
960 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
961 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
962 if (IS_ERR(qca->rx_skb)) {
963 int err = PTR_ERR(qca->rx_skb);
964 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
972 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
974 struct qca_data *qca = hu->priv;
976 return skb_dequeue(&qca->txq);
979 static uint8_t qca_get_baudrate_value(int speed)
983 return QCA_BAUDRATE_9600;
985 return QCA_BAUDRATE_19200;
987 return QCA_BAUDRATE_38400;
989 return QCA_BAUDRATE_57600;
991 return QCA_BAUDRATE_115200;
993 return QCA_BAUDRATE_230400;
995 return QCA_BAUDRATE_460800;
997 return QCA_BAUDRATE_500000;
999 return QCA_BAUDRATE_921600;
1001 return QCA_BAUDRATE_1000000;
1003 return QCA_BAUDRATE_2000000;
1005 return QCA_BAUDRATE_3000000;
1007 return QCA_BAUDRATE_3200000;
1009 return QCA_BAUDRATE_3500000;
1011 return QCA_BAUDRATE_115200;
1015 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1017 struct hci_uart *hu = hci_get_drvdata(hdev);
1018 struct qca_data *qca = hu->priv;
1019 struct sk_buff *skb;
1020 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1022 if (baudrate > QCA_BAUDRATE_3200000)
1027 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1029 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1033 /* Assign commands to change baudrate and packet type. */
1034 skb_put_data(skb, cmd, sizeof(cmd));
1035 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1037 skb_queue_tail(&qca->txq, skb);
1038 hci_uart_tx_wakeup(hu);
1040 /* Wait for the baudrate change request to be sent */
1042 while (!skb_queue_empty(&qca->txq))
1043 usleep_range(100, 200);
1046 serdev_device_wait_until_sent(hu->serdev,
1047 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1049 /* Give the controller time to process the request */
1050 if (qca_is_wcn399x(qca_soc_type(hu)))
1058 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1061 serdev_device_set_baudrate(hu->serdev, speed);
1063 hci_uart_set_baudrate(hu, speed);
1066 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1069 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1070 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1072 /* These power pulses are single byte command which are sent
1073 * at required baudrate to wcn3990. On wcn3990, we have an external
1074 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1075 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1076 * and also we use the same power inputs to turn on and off for
1077 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1078 * we send a power on pulse at 115200 bps. This algorithm will help to
1079 * save power. Disabling hardware flow control is mandatory while
1080 * sending power pulses to SoC.
1082 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1084 serdev_device_write_flush(hu->serdev);
1085 hci_uart_set_flow_control(hu, true);
1086 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1088 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1092 serdev_device_wait_until_sent(hu->serdev, timeout);
1093 hci_uart_set_flow_control(hu, false);
1095 /* Give to controller time to boot/shutdown */
1104 static unsigned int qca_get_speed(struct hci_uart *hu,
1105 enum qca_speed_type speed_type)
1107 unsigned int speed = 0;
1109 if (speed_type == QCA_INIT_SPEED) {
1111 speed = hu->init_speed;
1112 else if (hu->proto->init_speed)
1113 speed = hu->proto->init_speed;
1116 speed = hu->oper_speed;
1117 else if (hu->proto->oper_speed)
1118 speed = hu->proto->oper_speed;
1124 static int qca_check_speeds(struct hci_uart *hu)
1126 if (qca_is_wcn399x(qca_soc_type(hu))) {
1127 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1128 !qca_get_speed(hu, QCA_OPER_SPEED))
1131 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1132 !qca_get_speed(hu, QCA_OPER_SPEED))
1139 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1141 unsigned int speed, qca_baudrate;
1142 struct qca_data *qca = hu->priv;
1145 if (speed_type == QCA_INIT_SPEED) {
1146 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1148 host_set_baudrate(hu, speed);
1150 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1152 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1156 /* Disable flow control for wcn3990 to deassert RTS while
1157 * changing the baudrate of chip and host.
1159 if (qca_is_wcn399x(soc_type))
1160 hci_uart_set_flow_control(hu, true);
1162 if (soc_type == QCA_WCN3990) {
1163 reinit_completion(&qca->drop_ev_comp);
1164 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1167 qca_baudrate = qca_get_baudrate_value(speed);
1168 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1169 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1173 host_set_baudrate(hu, speed);
1176 if (qca_is_wcn399x(soc_type))
1177 hci_uart_set_flow_control(hu, false);
1179 if (soc_type == QCA_WCN3990) {
1180 /* Wait for the controller to send the vendor event
1181 * for the baudrate change command.
1183 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1184 msecs_to_jiffies(100))) {
1185 bt_dev_err(hu->hdev,
1186 "Failed to change controller baudrate\n");
1190 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1197 static int qca_wcn3990_init(struct hci_uart *hu)
1199 struct qca_serdev *qcadev;
1202 /* Check for vregs status, may be hci down has turned
1203 * off the voltage regulator.
1205 qcadev = serdev_device_get_drvdata(hu->serdev);
1206 if (!qcadev->bt_power->vregs_on) {
1207 serdev_device_close(hu->serdev);
1208 ret = qca_power_setup(hu, true);
1212 ret = serdev_device_open(hu->serdev);
1214 bt_dev_err(hu->hdev, "failed to open port");
1219 /* Forcefully enable wcn3990 to enter in to boot mode. */
1220 host_set_baudrate(hu, 2400);
1221 ret = qca_send_power_pulse(hu, false);
1225 qca_set_speed(hu, QCA_INIT_SPEED);
1226 ret = qca_send_power_pulse(hu, true);
1230 /* Now the device is in ready state to communicate with host.
1231 * To sync host with device we need to reopen port.
1232 * Without this, we will have RTS and CTS synchronization
1235 serdev_device_close(hu->serdev);
1236 ret = serdev_device_open(hu->serdev);
1238 bt_dev_err(hu->hdev, "failed to open port");
1242 hci_uart_set_flow_control(hu, false);
1247 static int qca_setup(struct hci_uart *hu)
1249 struct hci_dev *hdev = hu->hdev;
1250 struct qca_data *qca = hu->priv;
1251 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1252 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1253 const char *firmware_name = qca_get_firmware_name(hu);
1257 ret = qca_check_speeds(hu);
1261 /* Patch downloading has to be done without IBS mode */
1262 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1264 if (qca_is_wcn399x(soc_type)) {
1265 bt_dev_info(hdev, "setting up wcn3990");
1267 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1268 * setup for every hci up.
1270 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1271 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1272 hu->hdev->shutdown = qca_power_off;
1273 ret = qca_wcn3990_init(hu);
1277 ret = qca_read_soc_version(hdev, &soc_ver);
1281 bt_dev_info(hdev, "ROME setup");
1282 qca_set_speed(hu, QCA_INIT_SPEED);
1285 /* Setup user speed if needed */
1286 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1288 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1292 qca_baudrate = qca_get_baudrate_value(speed);
1295 if (!qca_is_wcn399x(soc_type)) {
1296 /* Get QCA version information */
1297 ret = qca_read_soc_version(hdev, &soc_ver);
1302 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1303 /* Setup patch / NVM configurations */
1304 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1307 set_bit(QCA_IBS_ENABLED, &qca->flags);
1308 qca_debugfs_init(hdev);
1309 } else if (ret == -ENOENT) {
1310 /* No patch/nvm-config found, run with original fw/config */
1312 } else if (ret == -EAGAIN) {
1314 * Userspace firmware loader will return -EAGAIN in case no
1315 * patch/nvm-config is found, so run with original fw/config.
1321 if (qca_is_wcn399x(soc_type))
1322 hu->hdev->set_bdaddr = qca_set_bdaddr;
1324 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1329 static struct hci_uart_proto qca_proto = {
1333 .init_speed = 115200,
1334 .oper_speed = 3000000,
1340 .enqueue = qca_enqueue,
1341 .dequeue = qca_dequeue,
1344 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1345 .soc_type = QCA_WCN3990,
1346 .vregs = (struct qca_vreg []) {
1347 { "vddio", 1800000, 1900000, 15000 },
1348 { "vddxo", 1800000, 1900000, 80000 },
1349 { "vddrf", 1300000, 1350000, 300000 },
1350 { "vddch0", 3300000, 3400000, 450000 },
1355 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1356 .soc_type = QCA_WCN3998,
1357 .vregs = (struct qca_vreg []) {
1358 { "vddio", 1800000, 1900000, 10000 },
1359 { "vddxo", 1800000, 1900000, 80000 },
1360 { "vddrf", 1300000, 1352000, 300000 },
1361 { "vddch0", 3300000, 3300000, 450000 },
1366 static void qca_power_shutdown(struct hci_uart *hu)
1368 struct qca_data *qca = hu->priv;
1369 unsigned long flags;
1371 /* From this point we go into power off state. But serial port is
1372 * still open, stop queueing the IBS data and flush all the buffered
1375 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1376 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1378 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1380 host_set_baudrate(hu, 2400);
1381 qca_send_power_pulse(hu, false);
1382 qca_power_setup(hu, false);
1385 static int qca_power_off(struct hci_dev *hdev)
1387 struct hci_uart *hu = hci_get_drvdata(hdev);
1389 /* Perform pre shutdown command */
1390 qca_send_pre_shutdown_cmd(hdev);
1392 qca_power_shutdown(hu);
1396 static int qca_enable_regulator(struct qca_vreg vregs,
1397 struct regulator *regulator)
1401 ret = regulator_set_voltage(regulator, vregs.min_uV,
1407 ret = regulator_set_load(regulator,
1413 return regulator_enable(regulator);
1417 static void qca_disable_regulator(struct qca_vreg vregs,
1418 struct regulator *regulator)
1420 regulator_disable(regulator);
1421 regulator_set_voltage(regulator, 0, vregs.max_uV);
1423 regulator_set_load(regulator, 0);
1427 static int qca_power_setup(struct hci_uart *hu, bool on)
1429 struct qca_vreg *vregs;
1430 struct regulator_bulk_data *vreg_bulk;
1431 struct qca_serdev *qcadev;
1432 int i, num_vregs, ret = 0;
1434 qcadev = serdev_device_get_drvdata(hu->serdev);
1435 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1436 !qcadev->bt_power->vreg_bulk)
1439 vregs = qcadev->bt_power->vreg_data->vregs;
1440 vreg_bulk = qcadev->bt_power->vreg_bulk;
1441 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1442 BT_DBG("on: %d", on);
1443 if (on && !qcadev->bt_power->vregs_on) {
1444 for (i = 0; i < num_vregs; i++) {
1445 ret = qca_enable_regulator(vregs[i],
1446 vreg_bulk[i].consumer);
1452 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1453 /* turn off regulators which are enabled */
1454 for (i = i - 1; i >= 0; i--)
1455 qca_disable_regulator(vregs[i],
1456 vreg_bulk[i].consumer);
1458 qcadev->bt_power->vregs_on = true;
1460 } else if (!on && qcadev->bt_power->vregs_on) {
1461 /* turn off regulator in reverse order */
1462 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1463 for ( ; i >= 0; i--)
1464 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1466 qcadev->bt_power->vregs_on = false;
1472 static int qca_init_regulators(struct qca_power *qca,
1473 const struct qca_vreg *vregs, size_t num_vregs)
1477 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
1478 sizeof(struct regulator_bulk_data),
1480 if (!qca->vreg_bulk)
1483 for (i = 0; i < num_vregs; i++)
1484 qca->vreg_bulk[i].supply = vregs[i].name;
1486 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1489 static int qca_serdev_probe(struct serdev_device *serdev)
1491 struct qca_serdev *qcadev;
1492 const struct qca_vreg_data *data;
1495 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1499 qcadev->serdev_hu.serdev = serdev;
1500 data = of_device_get_match_data(&serdev->dev);
1501 serdev_device_set_drvdata(serdev, qcadev);
1502 device_property_read_string(&serdev->dev, "firmware-name",
1503 &qcadev->firmware_name);
1504 if (data && qca_is_wcn399x(data->soc_type)) {
1505 qcadev->btsoc_type = data->soc_type;
1506 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1507 sizeof(struct qca_power),
1509 if (!qcadev->bt_power)
1512 qcadev->bt_power->dev = &serdev->dev;
1513 qcadev->bt_power->vreg_data = data;
1514 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1517 BT_ERR("Failed to init regulators:%d", err);
1521 qcadev->bt_power->vregs_on = false;
1523 device_property_read_u32(&serdev->dev, "max-speed",
1524 &qcadev->oper_speed);
1525 if (!qcadev->oper_speed)
1526 BT_DBG("UART will pick default operating speed");
1528 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1530 BT_ERR("wcn3990 serdev registration failed");
1534 qcadev->btsoc_type = QCA_ROME;
1535 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1537 if (IS_ERR(qcadev->bt_en)) {
1538 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1539 return PTR_ERR(qcadev->bt_en);
1542 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1543 if (IS_ERR(qcadev->susclk)) {
1544 dev_err(&serdev->dev, "failed to acquire clk\n");
1545 return PTR_ERR(qcadev->susclk);
1548 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1552 err = clk_prepare_enable(qcadev->susclk);
1556 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1558 clk_disable_unprepare(qcadev->susclk);
1565 static void qca_serdev_remove(struct serdev_device *serdev)
1567 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1569 if (qca_is_wcn399x(qcadev->btsoc_type))
1570 qca_power_shutdown(&qcadev->serdev_hu);
1572 clk_disable_unprepare(qcadev->susclk);
1574 hci_uart_unregister_device(&qcadev->serdev_hu);
1577 static const struct of_device_id qca_bluetooth_of_match[] = {
1578 { .compatible = "qcom,qca6174-bt" },
1579 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1580 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1583 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1585 static struct serdev_device_driver qca_serdev_driver = {
1586 .probe = qca_serdev_probe,
1587 .remove = qca_serdev_remove,
1589 .name = "hci_uart_qca",
1590 .of_match_table = qca_bluetooth_of_match,
1594 int __init qca_init(void)
1596 serdev_device_driver_register(&qca_serdev_driver);
1598 return hci_uart_register_proto(&qca_proto);
1601 int __exit qca_deinit(void)
1603 serdev_device_driver_unregister(&qca_serdev_driver);
1605 return hci_uart_unregister_proto(&qca_proto);