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;
164 static int qca_power_setup(struct hci_uart *hu, bool on);
165 static void qca_power_shutdown(struct hci_uart *hu);
166 static int qca_power_off(struct hci_dev *hdev);
168 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
170 enum qca_btsoc_type soc_type;
173 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
175 soc_type = qsd->btsoc_type;
183 static void __serial_clock_on(struct tty_struct *tty)
185 /* TODO: Some chipset requires to enable UART clock on client
186 * side to save power consumption or manual work is required.
187 * Please put your code to control UART clock here if needed
191 static void __serial_clock_off(struct tty_struct *tty)
193 /* TODO: Some chipset requires to disable UART clock on client
194 * side to save power consumption or manual work is required.
195 * Please put your code to control UART clock off here if needed
199 /* serial_clock_vote needs to be called with the ibs lock held */
200 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
202 struct qca_data *qca = hu->priv;
205 bool old_vote = (qca->tx_vote | qca->rx_vote);
209 case HCI_IBS_VOTE_STATS_UPDATE:
210 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
213 qca->vote_off_ms += diff;
215 qca->vote_on_ms += diff;
218 case HCI_IBS_TX_VOTE_CLOCK_ON:
224 case HCI_IBS_RX_VOTE_CLOCK_ON:
230 case HCI_IBS_TX_VOTE_CLOCK_OFF:
231 qca->tx_vote = false;
233 new_vote = qca->rx_vote | qca->tx_vote;
236 case HCI_IBS_RX_VOTE_CLOCK_OFF:
237 qca->rx_vote = false;
239 new_vote = qca->rx_vote | qca->tx_vote;
243 BT_ERR("Voting irregularity");
247 if (new_vote != old_vote) {
249 __serial_clock_on(hu->tty);
251 __serial_clock_off(hu->tty);
253 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
254 vote ? "true" : "false");
256 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
260 qca->vote_off_ms += diff;
263 qca->vote_on_ms += diff;
265 qca->vote_last_jif = jiffies;
269 /* Builds and sends an HCI_IBS command packet.
270 * These are very simple packets with only 1 cmd byte.
272 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
275 struct sk_buff *skb = NULL;
276 struct qca_data *qca = hu->priv;
278 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
280 skb = bt_skb_alloc(1, GFP_ATOMIC);
282 BT_ERR("Failed to allocate memory for HCI_IBS packet");
286 /* Assign HCI_IBS type */
287 skb_put_u8(skb, cmd);
289 skb_queue_tail(&qca->txq, skb);
294 static void qca_wq_awake_device(struct work_struct *work)
296 struct qca_data *qca = container_of(work, struct qca_data,
298 struct hci_uart *hu = qca->hu;
299 unsigned long retrans_delay;
301 BT_DBG("hu %p wq awake device", hu);
303 /* Vote for serial clock */
304 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
306 spin_lock(&qca->hci_ibs_lock);
308 /* Send wake indication to device */
309 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
310 BT_ERR("Failed to send WAKE to device");
312 qca->ibs_sent_wakes++;
314 /* Start retransmit timer */
315 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
316 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
318 spin_unlock(&qca->hci_ibs_lock);
320 /* Actually send the packets */
321 hci_uart_tx_wakeup(hu);
324 static void qca_wq_awake_rx(struct work_struct *work)
326 struct qca_data *qca = container_of(work, struct qca_data,
328 struct hci_uart *hu = qca->hu;
330 BT_DBG("hu %p wq awake rx", hu);
332 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
334 spin_lock(&qca->hci_ibs_lock);
335 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
337 /* Always acknowledge device wake up,
338 * sending IBS message doesn't count as TX ON.
340 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
341 BT_ERR("Failed to acknowledge device wake up");
343 qca->ibs_sent_wacks++;
345 spin_unlock(&qca->hci_ibs_lock);
347 /* Actually send the packets */
348 hci_uart_tx_wakeup(hu);
351 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
353 struct qca_data *qca = container_of(work, struct qca_data,
355 struct hci_uart *hu = qca->hu;
357 BT_DBG("hu %p rx clock vote off", hu);
359 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
362 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
364 struct qca_data *qca = container_of(work, struct qca_data,
366 struct hci_uart *hu = qca->hu;
368 BT_DBG("hu %p tx clock vote off", hu);
370 /* Run HCI tx handling unlocked */
371 hci_uart_tx_wakeup(hu);
373 /* Now that message queued to tty driver, vote for tty clocks off.
374 * It is up to the tty driver to pend the clocks off until tx done.
376 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
379 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
381 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
382 struct hci_uart *hu = qca->hu;
385 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
387 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
388 flags, SINGLE_DEPTH_NESTING);
390 switch (qca->tx_ibs_state) {
391 case HCI_IBS_TX_AWAKE:
392 /* TX_IDLE, go to SLEEP */
393 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
394 BT_ERR("Failed to send SLEEP to device");
397 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
398 qca->ibs_sent_slps++;
399 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
402 case HCI_IBS_TX_ASLEEP:
403 case HCI_IBS_TX_WAKING:
407 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
411 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
414 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
416 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
417 struct hci_uart *hu = qca->hu;
418 unsigned long flags, retrans_delay;
419 bool retransmit = false;
421 BT_DBG("hu %p wake retransmit timeout in %d state",
422 hu, qca->tx_ibs_state);
424 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
425 flags, SINGLE_DEPTH_NESTING);
427 switch (qca->tx_ibs_state) {
428 case HCI_IBS_TX_WAKING:
429 /* No WAKE_ACK, retransmit WAKE */
431 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
432 BT_ERR("Failed to acknowledge device wake up");
435 qca->ibs_sent_wakes++;
436 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
437 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
440 case HCI_IBS_TX_ASLEEP:
441 case HCI_IBS_TX_AWAKE:
445 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
449 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
452 hci_uart_tx_wakeup(hu);
455 /* Initialize protocol */
456 static int qca_open(struct hci_uart *hu)
458 struct qca_serdev *qcadev;
459 struct qca_data *qca;
462 BT_DBG("hu %p qca_open", hu);
464 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
468 skb_queue_head_init(&qca->txq);
469 skb_queue_head_init(&qca->tx_wait_q);
470 spin_lock_init(&qca->hci_ibs_lock);
471 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
472 if (!qca->workqueue) {
473 BT_ERR("QCA Workqueue not initialized properly");
478 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
479 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
480 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
481 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
484 init_completion(&qca->drop_ev_comp);
486 /* Assume we start with both sides asleep -- extra wakes OK */
487 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
488 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
490 /* clocks actually on, but we start votes off */
491 qca->tx_vote = false;
492 qca->rx_vote = false;
495 qca->ibs_sent_wacks = 0;
496 qca->ibs_sent_slps = 0;
497 qca->ibs_sent_wakes = 0;
498 qca->ibs_recv_wacks = 0;
499 qca->ibs_recv_slps = 0;
500 qca->ibs_recv_wakes = 0;
501 qca->vote_last_jif = jiffies;
503 qca->vote_off_ms = 0;
506 qca->tx_votes_on = 0;
507 qca->tx_votes_off = 0;
508 qca->rx_votes_on = 0;
509 qca->rx_votes_off = 0;
515 qcadev = serdev_device_get_drvdata(hu->serdev);
516 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
517 gpiod_set_value_cansleep(qcadev->bt_en, 1);
518 /* Controller needs time to bootup. */
521 hu->init_speed = qcadev->init_speed;
522 hu->oper_speed = qcadev->oper_speed;
523 ret = qca_power_setup(hu, true);
525 destroy_workqueue(qca->workqueue);
526 kfree_skb(qca->rx_skb);
534 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
535 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
537 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
538 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
540 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
541 qca->tx_idle_delay, qca->wake_retrans);
546 static void qca_debugfs_init(struct hci_dev *hdev)
548 struct hci_uart *hu = hci_get_drvdata(hdev);
549 struct qca_data *qca = hu->priv;
550 struct dentry *ibs_dir;
556 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
560 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
561 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
562 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
563 &qca->ibs_sent_slps);
564 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
565 &qca->ibs_sent_wakes);
566 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
567 &qca->ibs_sent_wacks);
568 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
569 &qca->ibs_recv_slps);
570 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
571 &qca->ibs_recv_wakes);
572 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
573 &qca->ibs_recv_wacks);
574 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
575 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
576 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
577 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
578 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
579 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
580 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
581 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
582 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
583 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
586 mode = S_IRUGO | S_IWUSR;
587 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
588 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
589 &qca->tx_idle_delay);
592 /* Flush protocol data */
593 static int qca_flush(struct hci_uart *hu)
595 struct qca_data *qca = hu->priv;
597 BT_DBG("hu %p qca flush", hu);
599 skb_queue_purge(&qca->tx_wait_q);
600 skb_queue_purge(&qca->txq);
606 static int qca_close(struct hci_uart *hu)
608 struct qca_serdev *qcadev;
609 struct qca_data *qca = hu->priv;
611 BT_DBG("hu %p qca close", hu);
613 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
615 skb_queue_purge(&qca->tx_wait_q);
616 skb_queue_purge(&qca->txq);
617 del_timer(&qca->tx_idle_timer);
618 del_timer(&qca->wake_retrans_timer);
619 destroy_workqueue(qca->workqueue);
623 qcadev = serdev_device_get_drvdata(hu->serdev);
624 if (qca_is_wcn399x(qcadev->btsoc_type))
625 qca_power_shutdown(hu);
627 gpiod_set_value_cansleep(qcadev->bt_en, 0);
631 kfree_skb(qca->rx_skb);
640 /* Called upon a wake-up-indication from the device.
642 static void device_want_to_wakeup(struct hci_uart *hu)
645 struct qca_data *qca = hu->priv;
647 BT_DBG("hu %p want to wake up", hu);
649 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
651 qca->ibs_recv_wakes++;
653 switch (qca->rx_ibs_state) {
654 case HCI_IBS_RX_ASLEEP:
655 /* Make sure clock is on - we may have turned clock off since
656 * receiving the wake up indicator awake rx clock.
658 queue_work(qca->workqueue, &qca->ws_awake_rx);
659 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
662 case HCI_IBS_RX_AWAKE:
663 /* Always acknowledge device wake up,
664 * sending IBS message doesn't count as TX ON.
666 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
667 BT_ERR("Failed to acknowledge device wake up");
670 qca->ibs_sent_wacks++;
674 /* Any other state is illegal */
675 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
680 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
682 /* Actually send the packets */
683 hci_uart_tx_wakeup(hu);
686 /* Called upon a sleep-indication from the device.
688 static void device_want_to_sleep(struct hci_uart *hu)
691 struct qca_data *qca = hu->priv;
693 BT_DBG("hu %p want to sleep", hu);
695 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
697 qca->ibs_recv_slps++;
699 switch (qca->rx_ibs_state) {
700 case HCI_IBS_RX_AWAKE:
702 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
703 /* Vote off rx clock under workqueue */
704 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
707 case HCI_IBS_RX_ASLEEP:
711 /* Any other state is illegal */
712 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
717 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
720 /* Called upon wake-up-acknowledgement from the device
722 static void device_woke_up(struct hci_uart *hu)
724 unsigned long flags, idle_delay;
725 struct qca_data *qca = hu->priv;
726 struct sk_buff *skb = NULL;
728 BT_DBG("hu %p woke up", hu);
730 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
732 qca->ibs_recv_wacks++;
734 switch (qca->tx_ibs_state) {
735 case HCI_IBS_TX_AWAKE:
736 /* Expect one if we send 2 WAKEs */
737 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
741 case HCI_IBS_TX_WAKING:
742 /* Send pending packets */
743 while ((skb = skb_dequeue(&qca->tx_wait_q)))
744 skb_queue_tail(&qca->txq, skb);
746 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
747 del_timer(&qca->wake_retrans_timer);
748 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
749 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
750 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
753 case HCI_IBS_TX_ASLEEP:
757 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
762 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
764 /* Actually send the packets */
765 hci_uart_tx_wakeup(hu);
768 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
769 * two simultaneous tasklets.
771 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
773 unsigned long flags = 0, idle_delay;
774 struct qca_data *qca = hu->priv;
776 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
779 /* Prepend skb with frame type */
780 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
782 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
784 /* Don't go to sleep in middle of patch download or
785 * Out-Of-Band(GPIOs control) sleep is selected.
787 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) {
788 skb_queue_tail(&qca->txq, skb);
789 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
793 /* Act according to current state */
794 switch (qca->tx_ibs_state) {
795 case HCI_IBS_TX_AWAKE:
796 BT_DBG("Device awake, sending normally");
797 skb_queue_tail(&qca->txq, skb);
798 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
799 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
802 case HCI_IBS_TX_ASLEEP:
803 BT_DBG("Device asleep, waking up and queueing packet");
804 /* Save packet for later */
805 skb_queue_tail(&qca->tx_wait_q, skb);
807 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
808 /* Schedule a work queue to wake up device */
809 queue_work(qca->workqueue, &qca->ws_awake_device);
812 case HCI_IBS_TX_WAKING:
813 BT_DBG("Device waking up, queueing packet");
814 /* Transient state; just keep packet for later */
815 skb_queue_tail(&qca->tx_wait_q, skb);
819 BT_ERR("Illegal tx state: %d (losing packet)",
825 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
830 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
832 struct hci_uart *hu = hci_get_drvdata(hdev);
834 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
836 device_want_to_sleep(hu);
842 static int qca_ibs_wake_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_WAKE_IND);
848 device_want_to_wakeup(hu);
854 static int qca_ibs_wake_ack(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_ACK);
866 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
868 /* We receive debug logs from chip as an ACL packets.
869 * Instead of sending the data to ACL to decode the
870 * received data, we are pushing them to the above layers
871 * as a diagnostic packet.
873 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
874 return hci_recv_diag(hdev, skb);
876 return hci_recv_frame(hdev, skb);
879 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
881 struct hci_uart *hu = hci_get_drvdata(hdev);
882 struct qca_data *qca = hu->priv;
884 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
885 struct hci_event_hdr *hdr = (void *)skb->data;
887 /* For the WCN3990 the vendor command for a baudrate change
888 * isn't sent as synchronous HCI command, because the
889 * controller sends the corresponding vendor event with the
890 * new baudrate. The event is received and properly decoded
891 * after changing the baudrate of the host port. It needs to
892 * be dropped, otherwise it can be misinterpreted as
893 * response to a later firmware download command (also a
897 if (hdr->evt == HCI_EV_VENDOR)
898 complete(&qca->drop_ev_comp);
905 return hci_recv_frame(hdev, skb);
908 #define QCA_IBS_SLEEP_IND_EVENT \
909 .type = HCI_IBS_SLEEP_IND, \
913 .maxlen = HCI_MAX_IBS_SIZE
915 #define QCA_IBS_WAKE_IND_EVENT \
916 .type = HCI_IBS_WAKE_IND, \
920 .maxlen = HCI_MAX_IBS_SIZE
922 #define QCA_IBS_WAKE_ACK_EVENT \
923 .type = HCI_IBS_WAKE_ACK, \
927 .maxlen = HCI_MAX_IBS_SIZE
929 static const struct h4_recv_pkt qca_recv_pkts[] = {
930 { H4_RECV_ACL, .recv = qca_recv_acl_data },
931 { H4_RECV_SCO, .recv = hci_recv_frame },
932 { H4_RECV_EVENT, .recv = qca_recv_event },
933 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
934 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
935 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
938 static int qca_recv(struct hci_uart *hu, const void *data, int count)
940 struct qca_data *qca = hu->priv;
942 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
945 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
946 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
947 if (IS_ERR(qca->rx_skb)) {
948 int err = PTR_ERR(qca->rx_skb);
949 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
957 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
959 struct qca_data *qca = hu->priv;
961 return skb_dequeue(&qca->txq);
964 static uint8_t qca_get_baudrate_value(int speed)
968 return QCA_BAUDRATE_9600;
970 return QCA_BAUDRATE_19200;
972 return QCA_BAUDRATE_38400;
974 return QCA_BAUDRATE_57600;
976 return QCA_BAUDRATE_115200;
978 return QCA_BAUDRATE_230400;
980 return QCA_BAUDRATE_460800;
982 return QCA_BAUDRATE_500000;
984 return QCA_BAUDRATE_921600;
986 return QCA_BAUDRATE_1000000;
988 return QCA_BAUDRATE_2000000;
990 return QCA_BAUDRATE_3000000;
992 return QCA_BAUDRATE_3200000;
994 return QCA_BAUDRATE_3500000;
996 return QCA_BAUDRATE_115200;
1000 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1002 struct hci_uart *hu = hci_get_drvdata(hdev);
1003 struct qca_data *qca = hu->priv;
1004 struct sk_buff *skb;
1005 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1007 if (baudrate > QCA_BAUDRATE_3200000)
1012 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1014 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1018 /* Assign commands to change baudrate and packet type. */
1019 skb_put_data(skb, cmd, sizeof(cmd));
1020 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1022 skb_queue_tail(&qca->txq, skb);
1023 hci_uart_tx_wakeup(hu);
1025 /* Wait for the baudrate change request to be sent */
1027 while (!skb_queue_empty(&qca->txq))
1028 usleep_range(100, 200);
1031 serdev_device_wait_until_sent(hu->serdev,
1032 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1034 /* Give the controller time to process the request */
1035 if (qca_is_wcn399x(qca_soc_type(hu)))
1043 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1046 serdev_device_set_baudrate(hu->serdev, speed);
1048 hci_uart_set_baudrate(hu, speed);
1051 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1054 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1055 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1057 /* These power pulses are single byte command which are sent
1058 * at required baudrate to wcn3990. On wcn3990, we have an external
1059 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1060 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1061 * and also we use the same power inputs to turn on and off for
1062 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1063 * we send a power on pulse at 115200 bps. This algorithm will help to
1064 * save power. Disabling hardware flow control is mandatory while
1065 * sending power pulses to SoC.
1067 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1069 serdev_device_write_flush(hu->serdev);
1070 hci_uart_set_flow_control(hu, true);
1071 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1073 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1077 serdev_device_wait_until_sent(hu->serdev, timeout);
1078 hci_uart_set_flow_control(hu, false);
1080 /* Give to controller time to boot/shutdown */
1089 static unsigned int qca_get_speed(struct hci_uart *hu,
1090 enum qca_speed_type speed_type)
1092 unsigned int speed = 0;
1094 if (speed_type == QCA_INIT_SPEED) {
1096 speed = hu->init_speed;
1097 else if (hu->proto->init_speed)
1098 speed = hu->proto->init_speed;
1101 speed = hu->oper_speed;
1102 else if (hu->proto->oper_speed)
1103 speed = hu->proto->oper_speed;
1109 static int qca_check_speeds(struct hci_uart *hu)
1111 if (qca_is_wcn399x(qca_soc_type(hu))) {
1112 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1113 !qca_get_speed(hu, QCA_OPER_SPEED))
1116 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1117 !qca_get_speed(hu, QCA_OPER_SPEED))
1124 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1126 unsigned int speed, qca_baudrate;
1127 struct qca_data *qca = hu->priv;
1130 if (speed_type == QCA_INIT_SPEED) {
1131 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1133 host_set_baudrate(hu, speed);
1135 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1137 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1141 /* Disable flow control for wcn3990 to deassert RTS while
1142 * changing the baudrate of chip and host.
1144 if (qca_is_wcn399x(soc_type))
1145 hci_uart_set_flow_control(hu, true);
1147 if (soc_type == QCA_WCN3990) {
1148 reinit_completion(&qca->drop_ev_comp);
1149 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1152 qca_baudrate = qca_get_baudrate_value(speed);
1153 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1154 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1158 host_set_baudrate(hu, speed);
1161 if (qca_is_wcn399x(soc_type))
1162 hci_uart_set_flow_control(hu, false);
1164 if (soc_type == QCA_WCN3990) {
1165 /* Wait for the controller to send the vendor event
1166 * for the baudrate change command.
1168 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1169 msecs_to_jiffies(100))) {
1170 bt_dev_err(hu->hdev,
1171 "Failed to change controller baudrate\n");
1175 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1182 static int qca_wcn3990_init(struct hci_uart *hu)
1184 struct qca_serdev *qcadev;
1187 /* Check for vregs status, may be hci down has turned
1188 * off the voltage regulator.
1190 qcadev = serdev_device_get_drvdata(hu->serdev);
1191 if (!qcadev->bt_power->vregs_on) {
1192 serdev_device_close(hu->serdev);
1193 ret = qca_power_setup(hu, true);
1197 ret = serdev_device_open(hu->serdev);
1199 bt_dev_err(hu->hdev, "failed to open port");
1204 /* Forcefully enable wcn3990 to enter in to boot mode. */
1205 host_set_baudrate(hu, 2400);
1206 ret = qca_send_power_pulse(hu, false);
1210 qca_set_speed(hu, QCA_INIT_SPEED);
1211 ret = qca_send_power_pulse(hu, true);
1215 /* Now the device is in ready state to communicate with host.
1216 * To sync host with device we need to reopen port.
1217 * Without this, we will have RTS and CTS synchronization
1220 serdev_device_close(hu->serdev);
1221 ret = serdev_device_open(hu->serdev);
1223 bt_dev_err(hu->hdev, "failed to open port");
1227 hci_uart_set_flow_control(hu, false);
1232 static int qca_setup(struct hci_uart *hu)
1234 struct hci_dev *hdev = hu->hdev;
1235 struct qca_data *qca = hu->priv;
1236 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1237 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1241 ret = qca_check_speeds(hu);
1245 /* Patch downloading has to be done without IBS mode */
1246 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1248 if (qca_is_wcn399x(soc_type)) {
1249 bt_dev_info(hdev, "setting up wcn3990");
1251 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1252 * setup for every hci up.
1254 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1255 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1256 hu->hdev->shutdown = qca_power_off;
1257 ret = qca_wcn3990_init(hu);
1261 ret = qca_read_soc_version(hdev, &soc_ver);
1265 bt_dev_info(hdev, "ROME setup");
1266 qca_set_speed(hu, QCA_INIT_SPEED);
1269 /* Setup user speed if needed */
1270 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1272 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1276 qca_baudrate = qca_get_baudrate_value(speed);
1279 if (!qca_is_wcn399x(soc_type)) {
1280 /* Get QCA version information */
1281 ret = qca_read_soc_version(hdev, &soc_ver);
1286 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1287 /* Setup patch / NVM configurations */
1288 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver);
1290 set_bit(QCA_IBS_ENABLED, &qca->flags);
1291 qca_debugfs_init(hdev);
1292 } else if (ret == -ENOENT) {
1293 /* No patch/nvm-config found, run with original fw/config */
1295 } else if (ret == -EAGAIN) {
1297 * Userspace firmware loader will return -EAGAIN in case no
1298 * patch/nvm-config is found, so run with original fw/config.
1304 if (qca_is_wcn399x(soc_type))
1305 hu->hdev->set_bdaddr = qca_set_bdaddr;
1307 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1312 static struct hci_uart_proto qca_proto = {
1316 .init_speed = 115200,
1317 .oper_speed = 3000000,
1323 .enqueue = qca_enqueue,
1324 .dequeue = qca_dequeue,
1327 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1328 .soc_type = QCA_WCN3990,
1329 .vregs = (struct qca_vreg []) {
1330 { "vddio", 1800000, 1900000, 15000 },
1331 { "vddxo", 1800000, 1900000, 80000 },
1332 { "vddrf", 1300000, 1350000, 300000 },
1333 { "vddch0", 3300000, 3400000, 450000 },
1338 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1339 .soc_type = QCA_WCN3998,
1340 .vregs = (struct qca_vreg []) {
1341 { "vddio", 1800000, 1900000, 10000 },
1342 { "vddxo", 1800000, 1900000, 80000 },
1343 { "vddrf", 1300000, 1352000, 300000 },
1344 { "vddch0", 3300000, 3300000, 450000 },
1349 static void qca_power_shutdown(struct hci_uart *hu)
1351 struct qca_data *qca = hu->priv;
1352 unsigned long flags;
1354 /* From this point we go into power off state. But serial port is
1355 * still open, stop queueing the IBS data and flush all the buffered
1358 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1359 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1361 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1363 host_set_baudrate(hu, 2400);
1364 qca_send_power_pulse(hu, false);
1365 qca_power_setup(hu, false);
1368 static int qca_power_off(struct hci_dev *hdev)
1370 struct hci_uart *hu = hci_get_drvdata(hdev);
1372 qca_power_shutdown(hu);
1376 static int qca_enable_regulator(struct qca_vreg vregs,
1377 struct regulator *regulator)
1381 ret = regulator_set_voltage(regulator, vregs.min_uV,
1387 ret = regulator_set_load(regulator,
1393 return regulator_enable(regulator);
1397 static void qca_disable_regulator(struct qca_vreg vregs,
1398 struct regulator *regulator)
1400 regulator_disable(regulator);
1401 regulator_set_voltage(regulator, 0, vregs.max_uV);
1403 regulator_set_load(regulator, 0);
1407 static int qca_power_setup(struct hci_uart *hu, bool on)
1409 struct qca_vreg *vregs;
1410 struct regulator_bulk_data *vreg_bulk;
1411 struct qca_serdev *qcadev;
1412 int i, num_vregs, ret = 0;
1414 qcadev = serdev_device_get_drvdata(hu->serdev);
1415 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1416 !qcadev->bt_power->vreg_bulk)
1419 vregs = qcadev->bt_power->vreg_data->vregs;
1420 vreg_bulk = qcadev->bt_power->vreg_bulk;
1421 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1422 BT_DBG("on: %d", on);
1423 if (on && !qcadev->bt_power->vregs_on) {
1424 for (i = 0; i < num_vregs; i++) {
1425 ret = qca_enable_regulator(vregs[i],
1426 vreg_bulk[i].consumer);
1432 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1433 /* turn off regulators which are enabled */
1434 for (i = i - 1; i >= 0; i--)
1435 qca_disable_regulator(vregs[i],
1436 vreg_bulk[i].consumer);
1438 qcadev->bt_power->vregs_on = true;
1440 } else if (!on && qcadev->bt_power->vregs_on) {
1441 /* turn off regulator in reverse order */
1442 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1443 for ( ; i >= 0; i--)
1444 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1446 qcadev->bt_power->vregs_on = false;
1452 static int qca_init_regulators(struct qca_power *qca,
1453 const struct qca_vreg *vregs, size_t num_vregs)
1457 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
1458 sizeof(struct regulator_bulk_data),
1460 if (!qca->vreg_bulk)
1463 for (i = 0; i < num_vregs; i++)
1464 qca->vreg_bulk[i].supply = vregs[i].name;
1466 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1469 static int qca_serdev_probe(struct serdev_device *serdev)
1471 struct qca_serdev *qcadev;
1472 const struct qca_vreg_data *data;
1475 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1479 qcadev->serdev_hu.serdev = serdev;
1480 data = of_device_get_match_data(&serdev->dev);
1481 serdev_device_set_drvdata(serdev, qcadev);
1482 if (data && qca_is_wcn399x(data->soc_type)) {
1483 qcadev->btsoc_type = data->soc_type;
1484 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1485 sizeof(struct qca_power),
1487 if (!qcadev->bt_power)
1490 qcadev->bt_power->dev = &serdev->dev;
1491 qcadev->bt_power->vreg_data = data;
1492 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1495 BT_ERR("Failed to init regulators:%d", err);
1499 qcadev->bt_power->vregs_on = false;
1501 device_property_read_u32(&serdev->dev, "max-speed",
1502 &qcadev->oper_speed);
1503 if (!qcadev->oper_speed)
1504 BT_DBG("UART will pick default operating speed");
1506 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1508 BT_ERR("wcn3990 serdev registration failed");
1512 qcadev->btsoc_type = QCA_ROME;
1513 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1515 if (IS_ERR(qcadev->bt_en)) {
1516 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1517 return PTR_ERR(qcadev->bt_en);
1520 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1521 if (IS_ERR(qcadev->susclk)) {
1522 dev_err(&serdev->dev, "failed to acquire clk\n");
1523 return PTR_ERR(qcadev->susclk);
1526 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1530 err = clk_prepare_enable(qcadev->susclk);
1534 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1536 clk_disable_unprepare(qcadev->susclk);
1543 static void qca_serdev_remove(struct serdev_device *serdev)
1545 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1547 if (qca_is_wcn399x(qcadev->btsoc_type))
1548 qca_power_shutdown(&qcadev->serdev_hu);
1550 clk_disable_unprepare(qcadev->susclk);
1552 hci_uart_unregister_device(&qcadev->serdev_hu);
1555 static const struct of_device_id qca_bluetooth_of_match[] = {
1556 { .compatible = "qcom,qca6174-bt" },
1557 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1558 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1561 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1563 static struct serdev_device_driver qca_serdev_driver = {
1564 .probe = qca_serdev_probe,
1565 .remove = qca_serdev_remove,
1567 .name = "hci_uart_qca",
1568 .of_match_table = qca_bluetooth_of_match,
1572 int __init qca_init(void)
1574 serdev_device_driver_register(&qca_serdev_driver);
1576 return hci_uart_register_proto(&qca_proto);
1579 int __exit qca_deinit(void)
1581 serdev_device_driver_unregister(&qca_serdev_driver);
1583 return hci_uart_unregister_proto(&qca_proto);