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;
314 BT_DBG("hu %p wq awake device", hu);
316 /* Vote for serial clock */
317 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
319 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
321 /* Send wake indication to device */
322 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
323 BT_ERR("Failed to send WAKE to device");
325 qca->ibs_sent_wakes++;
327 /* Start retransmit timer */
328 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
329 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
331 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
333 /* Actually send the packets */
334 hci_uart_tx_wakeup(hu);
337 static void qca_wq_awake_rx(struct work_struct *work)
339 struct qca_data *qca = container_of(work, struct qca_data,
341 struct hci_uart *hu = qca->hu;
344 BT_DBG("hu %p wq awake rx", hu);
346 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
348 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
349 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
351 /* Always acknowledge device wake up,
352 * sending IBS message doesn't count as TX ON.
354 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
355 BT_ERR("Failed to acknowledge device wake up");
357 qca->ibs_sent_wacks++;
359 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
361 /* Actually send the packets */
362 hci_uart_tx_wakeup(hu);
365 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
367 struct qca_data *qca = container_of(work, struct qca_data,
369 struct hci_uart *hu = qca->hu;
371 BT_DBG("hu %p rx clock vote off", hu);
373 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
376 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
378 struct qca_data *qca = container_of(work, struct qca_data,
380 struct hci_uart *hu = qca->hu;
382 BT_DBG("hu %p tx clock vote off", hu);
384 /* Run HCI tx handling unlocked */
385 hci_uart_tx_wakeup(hu);
387 /* Now that message queued to tty driver, vote for tty clocks off.
388 * It is up to the tty driver to pend the clocks off until tx done.
390 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
393 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
395 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
396 struct hci_uart *hu = qca->hu;
399 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
401 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
402 flags, SINGLE_DEPTH_NESTING);
404 switch (qca->tx_ibs_state) {
405 case HCI_IBS_TX_AWAKE:
406 /* TX_IDLE, go to SLEEP */
407 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
408 BT_ERR("Failed to send SLEEP to device");
411 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
412 qca->ibs_sent_slps++;
413 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
416 case HCI_IBS_TX_ASLEEP:
417 case HCI_IBS_TX_WAKING:
421 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
425 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
428 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
430 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
431 struct hci_uart *hu = qca->hu;
432 unsigned long flags, retrans_delay;
433 bool retransmit = false;
435 BT_DBG("hu %p wake retransmit timeout in %d state",
436 hu, qca->tx_ibs_state);
438 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
439 flags, SINGLE_DEPTH_NESTING);
441 switch (qca->tx_ibs_state) {
442 case HCI_IBS_TX_WAKING:
443 /* No WAKE_ACK, retransmit WAKE */
445 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
446 BT_ERR("Failed to acknowledge device wake up");
449 qca->ibs_sent_wakes++;
450 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
451 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
454 case HCI_IBS_TX_ASLEEP:
455 case HCI_IBS_TX_AWAKE:
459 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
463 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
466 hci_uart_tx_wakeup(hu);
469 /* Initialize protocol */
470 static int qca_open(struct hci_uart *hu)
472 struct qca_serdev *qcadev;
473 struct qca_data *qca;
476 BT_DBG("hu %p qca_open", hu);
478 if (!hci_uart_has_flow_control(hu))
481 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
485 skb_queue_head_init(&qca->txq);
486 skb_queue_head_init(&qca->tx_wait_q);
487 spin_lock_init(&qca->hci_ibs_lock);
488 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
489 if (!qca->workqueue) {
490 BT_ERR("QCA Workqueue not initialized properly");
495 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
496 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
497 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
498 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
501 init_completion(&qca->drop_ev_comp);
503 /* Assume we start with both sides asleep -- extra wakes OK */
504 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
505 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
507 qca->vote_last_jif = jiffies;
513 qcadev = serdev_device_get_drvdata(hu->serdev);
514 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
515 gpiod_set_value_cansleep(qcadev->bt_en, 1);
516 /* Controller needs time to bootup. */
519 hu->init_speed = qcadev->init_speed;
520 hu->oper_speed = qcadev->oper_speed;
521 ret = qca_power_setup(hu, true);
523 destroy_workqueue(qca->workqueue);
524 kfree_skb(qca->rx_skb);
532 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
533 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
535 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
536 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
538 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
539 qca->tx_idle_delay, qca->wake_retrans);
544 static void qca_debugfs_init(struct hci_dev *hdev)
546 struct hci_uart *hu = hci_get_drvdata(hdev);
547 struct qca_data *qca = hu->priv;
548 struct dentry *ibs_dir;
554 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
558 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
559 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
560 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
561 &qca->ibs_sent_slps);
562 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
563 &qca->ibs_sent_wakes);
564 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
565 &qca->ibs_sent_wacks);
566 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
567 &qca->ibs_recv_slps);
568 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
569 &qca->ibs_recv_wakes);
570 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
571 &qca->ibs_recv_wacks);
572 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
573 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
574 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
575 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
576 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
577 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
578 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
579 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
580 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
581 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
584 mode = S_IRUGO | S_IWUSR;
585 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
586 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
587 &qca->tx_idle_delay);
590 /* Flush protocol data */
591 static int qca_flush(struct hci_uart *hu)
593 struct qca_data *qca = hu->priv;
595 BT_DBG("hu %p qca flush", hu);
597 skb_queue_purge(&qca->tx_wait_q);
598 skb_queue_purge(&qca->txq);
604 static int qca_close(struct hci_uart *hu)
606 struct qca_serdev *qcadev;
607 struct qca_data *qca = hu->priv;
609 BT_DBG("hu %p qca close", hu);
611 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
613 skb_queue_purge(&qca->tx_wait_q);
614 skb_queue_purge(&qca->txq);
615 del_timer(&qca->tx_idle_timer);
616 del_timer(&qca->wake_retrans_timer);
617 destroy_workqueue(qca->workqueue);
621 qcadev = serdev_device_get_drvdata(hu->serdev);
622 if (qca_is_wcn399x(qcadev->btsoc_type))
623 qca_power_shutdown(hu);
625 gpiod_set_value_cansleep(qcadev->bt_en, 0);
629 kfree_skb(qca->rx_skb);
638 /* Called upon a wake-up-indication from the device.
640 static void device_want_to_wakeup(struct hci_uart *hu)
643 struct qca_data *qca = hu->priv;
645 BT_DBG("hu %p want to wake up", hu);
647 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
649 qca->ibs_recv_wakes++;
651 switch (qca->rx_ibs_state) {
652 case HCI_IBS_RX_ASLEEP:
653 /* Make sure clock is on - we may have turned clock off since
654 * receiving the wake up indicator awake rx clock.
656 queue_work(qca->workqueue, &qca->ws_awake_rx);
657 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
660 case HCI_IBS_RX_AWAKE:
661 /* Always acknowledge device wake up,
662 * sending IBS message doesn't count as TX ON.
664 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
665 BT_ERR("Failed to acknowledge device wake up");
668 qca->ibs_sent_wacks++;
672 /* Any other state is illegal */
673 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
678 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
680 /* Actually send the packets */
681 hci_uart_tx_wakeup(hu);
684 /* Called upon a sleep-indication from the device.
686 static void device_want_to_sleep(struct hci_uart *hu)
689 struct qca_data *qca = hu->priv;
691 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
693 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
695 qca->ibs_recv_slps++;
697 switch (qca->rx_ibs_state) {
698 case HCI_IBS_RX_AWAKE:
700 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
701 /* Vote off rx clock under workqueue */
702 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
705 case HCI_IBS_RX_ASLEEP:
709 /* Any other state is illegal */
710 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
715 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
718 /* Called upon wake-up-acknowledgement from the device
720 static void device_woke_up(struct hci_uart *hu)
722 unsigned long flags, idle_delay;
723 struct qca_data *qca = hu->priv;
724 struct sk_buff *skb = NULL;
726 BT_DBG("hu %p woke up", hu);
728 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
730 qca->ibs_recv_wacks++;
732 switch (qca->tx_ibs_state) {
733 case HCI_IBS_TX_AWAKE:
734 /* Expect one if we send 2 WAKEs */
735 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
739 case HCI_IBS_TX_WAKING:
740 /* Send pending packets */
741 while ((skb = skb_dequeue(&qca->tx_wait_q)))
742 skb_queue_tail(&qca->txq, skb);
744 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
745 del_timer(&qca->wake_retrans_timer);
746 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
747 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
748 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
751 case HCI_IBS_TX_ASLEEP:
755 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
760 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
762 /* Actually send the packets */
763 hci_uart_tx_wakeup(hu);
766 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
767 * two simultaneous tasklets.
769 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
771 unsigned long flags = 0, idle_delay;
772 struct qca_data *qca = hu->priv;
774 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
777 /* Prepend skb with frame type */
778 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
780 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
782 /* Don't go to sleep in middle of patch download or
783 * Out-Of-Band(GPIOs control) sleep is selected.
785 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) {
786 skb_queue_tail(&qca->txq, skb);
787 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
791 /* Act according to current state */
792 switch (qca->tx_ibs_state) {
793 case HCI_IBS_TX_AWAKE:
794 BT_DBG("Device awake, sending normally");
795 skb_queue_tail(&qca->txq, skb);
796 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
797 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
800 case HCI_IBS_TX_ASLEEP:
801 BT_DBG("Device asleep, waking up and queueing packet");
802 /* Save packet for later */
803 skb_queue_tail(&qca->tx_wait_q, skb);
805 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
806 /* Schedule a work queue to wake up device */
807 queue_work(qca->workqueue, &qca->ws_awake_device);
810 case HCI_IBS_TX_WAKING:
811 BT_DBG("Device waking up, queueing packet");
812 /* Transient state; just keep packet for later */
813 skb_queue_tail(&qca->tx_wait_q, skb);
817 BT_ERR("Illegal tx state: %d (losing packet)",
823 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
828 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
830 struct hci_uart *hu = hci_get_drvdata(hdev);
832 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
834 device_want_to_sleep(hu);
840 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
842 struct hci_uart *hu = hci_get_drvdata(hdev);
844 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
846 device_want_to_wakeup(hu);
852 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
854 struct hci_uart *hu = hci_get_drvdata(hdev);
856 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
864 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
866 /* We receive debug logs from chip as an ACL packets.
867 * Instead of sending the data to ACL to decode the
868 * received data, we are pushing them to the above layers
869 * as a diagnostic packet.
871 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
872 return hci_recv_diag(hdev, skb);
874 return hci_recv_frame(hdev, skb);
877 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
879 struct hci_uart *hu = hci_get_drvdata(hdev);
880 struct qca_data *qca = hu->priv;
882 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
883 struct hci_event_hdr *hdr = (void *)skb->data;
885 /* For the WCN3990 the vendor command for a baudrate change
886 * isn't sent as synchronous HCI command, because the
887 * controller sends the corresponding vendor event with the
888 * new baudrate. The event is received and properly decoded
889 * after changing the baudrate of the host port. It needs to
890 * be dropped, otherwise it can be misinterpreted as
891 * response to a later firmware download command (also a
895 if (hdr->evt == HCI_EV_VENDOR)
896 complete(&qca->drop_ev_comp);
903 return hci_recv_frame(hdev, skb);
906 #define QCA_IBS_SLEEP_IND_EVENT \
907 .type = HCI_IBS_SLEEP_IND, \
911 .maxlen = HCI_MAX_IBS_SIZE
913 #define QCA_IBS_WAKE_IND_EVENT \
914 .type = HCI_IBS_WAKE_IND, \
918 .maxlen = HCI_MAX_IBS_SIZE
920 #define QCA_IBS_WAKE_ACK_EVENT \
921 .type = HCI_IBS_WAKE_ACK, \
925 .maxlen = HCI_MAX_IBS_SIZE
927 static const struct h4_recv_pkt qca_recv_pkts[] = {
928 { H4_RECV_ACL, .recv = qca_recv_acl_data },
929 { H4_RECV_SCO, .recv = hci_recv_frame },
930 { H4_RECV_EVENT, .recv = qca_recv_event },
931 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
932 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
933 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
936 static int qca_recv(struct hci_uart *hu, const void *data, int count)
938 struct qca_data *qca = hu->priv;
940 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
943 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
944 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
945 if (IS_ERR(qca->rx_skb)) {
946 int err = PTR_ERR(qca->rx_skb);
947 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
955 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
957 struct qca_data *qca = hu->priv;
959 return skb_dequeue(&qca->txq);
962 static uint8_t qca_get_baudrate_value(int speed)
966 return QCA_BAUDRATE_9600;
968 return QCA_BAUDRATE_19200;
970 return QCA_BAUDRATE_38400;
972 return QCA_BAUDRATE_57600;
974 return QCA_BAUDRATE_115200;
976 return QCA_BAUDRATE_230400;
978 return QCA_BAUDRATE_460800;
980 return QCA_BAUDRATE_500000;
982 return QCA_BAUDRATE_921600;
984 return QCA_BAUDRATE_1000000;
986 return QCA_BAUDRATE_2000000;
988 return QCA_BAUDRATE_3000000;
990 return QCA_BAUDRATE_3200000;
992 return QCA_BAUDRATE_3500000;
994 return QCA_BAUDRATE_115200;
998 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1000 struct hci_uart *hu = hci_get_drvdata(hdev);
1001 struct qca_data *qca = hu->priv;
1002 struct sk_buff *skb;
1003 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1005 if (baudrate > QCA_BAUDRATE_3200000)
1010 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1012 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1016 /* Assign commands to change baudrate and packet type. */
1017 skb_put_data(skb, cmd, sizeof(cmd));
1018 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1020 skb_queue_tail(&qca->txq, skb);
1021 hci_uart_tx_wakeup(hu);
1023 /* Wait for the baudrate change request to be sent */
1025 while (!skb_queue_empty(&qca->txq))
1026 usleep_range(100, 200);
1029 serdev_device_wait_until_sent(hu->serdev,
1030 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1032 /* Give the controller time to process the request */
1033 if (qca_is_wcn399x(qca_soc_type(hu)))
1041 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1044 serdev_device_set_baudrate(hu->serdev, speed);
1046 hci_uart_set_baudrate(hu, speed);
1049 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1052 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1053 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1055 /* These power pulses are single byte command which are sent
1056 * at required baudrate to wcn3990. On wcn3990, we have an external
1057 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1058 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1059 * and also we use the same power inputs to turn on and off for
1060 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1061 * we send a power on pulse at 115200 bps. This algorithm will help to
1062 * save power. Disabling hardware flow control is mandatory while
1063 * sending power pulses to SoC.
1065 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1067 serdev_device_write_flush(hu->serdev);
1068 hci_uart_set_flow_control(hu, true);
1069 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1071 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1075 serdev_device_wait_until_sent(hu->serdev, timeout);
1076 hci_uart_set_flow_control(hu, false);
1078 /* Give to controller time to boot/shutdown */
1087 static unsigned int qca_get_speed(struct hci_uart *hu,
1088 enum qca_speed_type speed_type)
1090 unsigned int speed = 0;
1092 if (speed_type == QCA_INIT_SPEED) {
1094 speed = hu->init_speed;
1095 else if (hu->proto->init_speed)
1096 speed = hu->proto->init_speed;
1099 speed = hu->oper_speed;
1100 else if (hu->proto->oper_speed)
1101 speed = hu->proto->oper_speed;
1107 static int qca_check_speeds(struct hci_uart *hu)
1109 if (qca_is_wcn399x(qca_soc_type(hu))) {
1110 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1111 !qca_get_speed(hu, QCA_OPER_SPEED))
1114 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1115 !qca_get_speed(hu, QCA_OPER_SPEED))
1122 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1124 unsigned int speed, qca_baudrate;
1125 struct qca_data *qca = hu->priv;
1128 if (speed_type == QCA_INIT_SPEED) {
1129 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1131 host_set_baudrate(hu, speed);
1133 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1135 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1139 /* Disable flow control for wcn3990 to deassert RTS while
1140 * changing the baudrate of chip and host.
1142 if (qca_is_wcn399x(soc_type))
1143 hci_uart_set_flow_control(hu, true);
1145 if (soc_type == QCA_WCN3990) {
1146 reinit_completion(&qca->drop_ev_comp);
1147 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1150 qca_baudrate = qca_get_baudrate_value(speed);
1151 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1152 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1156 host_set_baudrate(hu, speed);
1159 if (qca_is_wcn399x(soc_type))
1160 hci_uart_set_flow_control(hu, false);
1162 if (soc_type == QCA_WCN3990) {
1163 /* Wait for the controller to send the vendor event
1164 * for the baudrate change command.
1166 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1167 msecs_to_jiffies(100))) {
1168 bt_dev_err(hu->hdev,
1169 "Failed to change controller baudrate\n");
1173 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1180 static int qca_wcn3990_init(struct hci_uart *hu)
1182 struct qca_serdev *qcadev;
1185 /* Check for vregs status, may be hci down has turned
1186 * off the voltage regulator.
1188 qcadev = serdev_device_get_drvdata(hu->serdev);
1189 if (!qcadev->bt_power->vregs_on) {
1190 serdev_device_close(hu->serdev);
1191 ret = qca_power_setup(hu, true);
1195 ret = serdev_device_open(hu->serdev);
1197 bt_dev_err(hu->hdev, "failed to open port");
1202 /* Forcefully enable wcn3990 to enter in to boot mode. */
1203 host_set_baudrate(hu, 2400);
1204 ret = qca_send_power_pulse(hu, false);
1208 qca_set_speed(hu, QCA_INIT_SPEED);
1209 ret = qca_send_power_pulse(hu, true);
1213 /* Now the device is in ready state to communicate with host.
1214 * To sync host with device we need to reopen port.
1215 * Without this, we will have RTS and CTS synchronization
1218 serdev_device_close(hu->serdev);
1219 ret = serdev_device_open(hu->serdev);
1221 bt_dev_err(hu->hdev, "failed to open port");
1225 hci_uart_set_flow_control(hu, false);
1230 static int qca_setup(struct hci_uart *hu)
1232 struct hci_dev *hdev = hu->hdev;
1233 struct qca_data *qca = hu->priv;
1234 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1235 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1236 const char *firmware_name = qca_get_firmware_name(hu);
1240 ret = qca_check_speeds(hu);
1244 /* Patch downloading has to be done without IBS mode */
1245 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1247 /* Enable controller to do both LE scan and BR/EDR inquiry
1250 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1252 if (qca_is_wcn399x(soc_type)) {
1253 bt_dev_info(hdev, "setting up wcn3990");
1255 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1256 * setup for every hci up.
1258 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1259 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1260 hu->hdev->shutdown = qca_power_off;
1261 ret = qca_wcn3990_init(hu);
1265 ret = qca_read_soc_version(hdev, &soc_ver);
1269 bt_dev_info(hdev, "ROME setup");
1270 qca_set_speed(hu, QCA_INIT_SPEED);
1273 /* Setup user speed if needed */
1274 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1276 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1280 qca_baudrate = qca_get_baudrate_value(speed);
1283 if (!qca_is_wcn399x(soc_type)) {
1284 /* Get QCA version information */
1285 ret = qca_read_soc_version(hdev, &soc_ver);
1290 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1291 /* Setup patch / NVM configurations */
1292 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1295 set_bit(QCA_IBS_ENABLED, &qca->flags);
1296 qca_debugfs_init(hdev);
1297 } else if (ret == -ENOENT) {
1298 /* No patch/nvm-config found, run with original fw/config */
1300 } else if (ret == -EAGAIN) {
1302 * Userspace firmware loader will return -EAGAIN in case no
1303 * patch/nvm-config is found, so run with original fw/config.
1309 if (qca_is_wcn399x(soc_type))
1310 hu->hdev->set_bdaddr = qca_set_bdaddr;
1312 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1317 static const struct hci_uart_proto qca_proto = {
1321 .init_speed = 115200,
1322 .oper_speed = 3000000,
1328 .enqueue = qca_enqueue,
1329 .dequeue = qca_dequeue,
1332 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1333 .soc_type = QCA_WCN3990,
1334 .vregs = (struct qca_vreg []) {
1335 { "vddio", 1800000, 1900000, 15000 },
1336 { "vddxo", 1800000, 1900000, 80000 },
1337 { "vddrf", 1300000, 1350000, 300000 },
1338 { "vddch0", 3300000, 3400000, 450000 },
1343 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1344 .soc_type = QCA_WCN3998,
1345 .vregs = (struct qca_vreg []) {
1346 { "vddio", 1800000, 1900000, 10000 },
1347 { "vddxo", 1800000, 1900000, 80000 },
1348 { "vddrf", 1300000, 1352000, 300000 },
1349 { "vddch0", 3300000, 3300000, 450000 },
1354 static void qca_power_shutdown(struct hci_uart *hu)
1356 struct qca_data *qca = hu->priv;
1357 unsigned long flags;
1359 /* From this point we go into power off state. But serial port is
1360 * still open, stop queueing the IBS data and flush all the buffered
1363 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1364 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1366 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1368 host_set_baudrate(hu, 2400);
1369 qca_send_power_pulse(hu, false);
1370 qca_power_setup(hu, false);
1373 static int qca_power_off(struct hci_dev *hdev)
1375 struct hci_uart *hu = hci_get_drvdata(hdev);
1377 /* Perform pre shutdown command */
1378 qca_send_pre_shutdown_cmd(hdev);
1380 usleep_range(8000, 10000);
1382 qca_power_shutdown(hu);
1386 static int qca_enable_regulator(struct qca_vreg vregs,
1387 struct regulator *regulator)
1391 ret = regulator_set_voltage(regulator, vregs.min_uV,
1397 ret = regulator_set_load(regulator,
1403 return regulator_enable(regulator);
1407 static void qca_disable_regulator(struct qca_vreg vregs,
1408 struct regulator *regulator)
1410 regulator_disable(regulator);
1411 regulator_set_voltage(regulator, 0, vregs.max_uV);
1413 regulator_set_load(regulator, 0);
1417 static int qca_power_setup(struct hci_uart *hu, bool on)
1419 struct qca_vreg *vregs;
1420 struct regulator_bulk_data *vreg_bulk;
1421 struct qca_serdev *qcadev;
1422 int i, num_vregs, ret = 0;
1424 qcadev = serdev_device_get_drvdata(hu->serdev);
1425 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1426 !qcadev->bt_power->vreg_bulk)
1429 vregs = qcadev->bt_power->vreg_data->vregs;
1430 vreg_bulk = qcadev->bt_power->vreg_bulk;
1431 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1432 BT_DBG("on: %d", on);
1433 if (on && !qcadev->bt_power->vregs_on) {
1434 for (i = 0; i < num_vregs; i++) {
1435 ret = qca_enable_regulator(vregs[i],
1436 vreg_bulk[i].consumer);
1442 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1443 /* turn off regulators which are enabled */
1444 for (i = i - 1; i >= 0; i--)
1445 qca_disable_regulator(vregs[i],
1446 vreg_bulk[i].consumer);
1448 qcadev->bt_power->vregs_on = true;
1450 } else if (!on && qcadev->bt_power->vregs_on) {
1451 /* turn off regulator in reverse order */
1452 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1453 for ( ; i >= 0; i--)
1454 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1456 qcadev->bt_power->vregs_on = false;
1462 static int qca_init_regulators(struct qca_power *qca,
1463 const struct qca_vreg *vregs, size_t num_vregs)
1467 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
1468 sizeof(struct regulator_bulk_data),
1470 if (!qca->vreg_bulk)
1473 for (i = 0; i < num_vregs; i++)
1474 qca->vreg_bulk[i].supply = vregs[i].name;
1476 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1479 static int qca_serdev_probe(struct serdev_device *serdev)
1481 struct qca_serdev *qcadev;
1482 const struct qca_vreg_data *data;
1485 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1489 qcadev->serdev_hu.serdev = serdev;
1490 data = of_device_get_match_data(&serdev->dev);
1491 serdev_device_set_drvdata(serdev, qcadev);
1492 device_property_read_string(&serdev->dev, "firmware-name",
1493 &qcadev->firmware_name);
1494 if (data && qca_is_wcn399x(data->soc_type)) {
1495 qcadev->btsoc_type = data->soc_type;
1496 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1497 sizeof(struct qca_power),
1499 if (!qcadev->bt_power)
1502 qcadev->bt_power->dev = &serdev->dev;
1503 qcadev->bt_power->vreg_data = data;
1504 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1507 BT_ERR("Failed to init regulators:%d", err);
1511 qcadev->bt_power->vregs_on = false;
1513 device_property_read_u32(&serdev->dev, "max-speed",
1514 &qcadev->oper_speed);
1515 if (!qcadev->oper_speed)
1516 BT_DBG("UART will pick default operating speed");
1518 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1520 BT_ERR("wcn3990 serdev registration failed");
1524 qcadev->btsoc_type = QCA_ROME;
1525 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1527 if (IS_ERR(qcadev->bt_en)) {
1528 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1529 return PTR_ERR(qcadev->bt_en);
1532 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1533 if (IS_ERR(qcadev->susclk)) {
1534 dev_err(&serdev->dev, "failed to acquire clk\n");
1535 return PTR_ERR(qcadev->susclk);
1538 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1542 err = clk_prepare_enable(qcadev->susclk);
1546 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1548 clk_disable_unprepare(qcadev->susclk);
1555 static void qca_serdev_remove(struct serdev_device *serdev)
1557 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1559 if (qca_is_wcn399x(qcadev->btsoc_type))
1560 qca_power_shutdown(&qcadev->serdev_hu);
1562 clk_disable_unprepare(qcadev->susclk);
1564 hci_uart_unregister_device(&qcadev->serdev_hu);
1567 static const struct of_device_id qca_bluetooth_of_match[] = {
1568 { .compatible = "qcom,qca6174-bt" },
1569 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1570 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1573 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1575 static struct serdev_device_driver qca_serdev_driver = {
1576 .probe = qca_serdev_probe,
1577 .remove = qca_serdev_remove,
1579 .name = "hci_uart_qca",
1580 .of_match_table = qca_bluetooth_of_match,
1584 int __init qca_init(void)
1586 serdev_device_driver_register(&qca_serdev_driver);
1588 return hci_uart_register_proto(&qca_proto);
1591 int __exit qca_deinit(void)
1593 serdev_device_driver_unregister(&qca_serdev_driver);
1595 return hci_uart_unregister_proto(&qca_proto);