2 * Bluetooth Software UART Qualcomm protocol
4 * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management
5 * protocol extension to H4.
7 * Copyright (C) 2007 Texas Instruments, Inc.
8 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
11 * This file is based on hci_ll.c, which was...
12 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
13 * which was in turn based on hci_h4.c, which was written
14 * by Maxim Krasnyansky and Marcel Holtmann.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <linux/kernel.h>
32 #include <linux/clk.h>
33 #include <linux/debugfs.h>
34 #include <linux/delay.h>
35 #include <linux/device.h>
36 #include <linux/gpio/consumer.h>
37 #include <linux/mod_devicetable.h>
38 #include <linux/module.h>
39 #include <linux/of_device.h>
40 #include <linux/platform_device.h>
41 #include <linux/regulator/consumer.h>
42 #include <linux/serdev.h>
43 #include <asm/unaligned.h>
45 #include <net/bluetooth/bluetooth.h>
46 #include <net/bluetooth/hci_core.h>
51 /* HCI_IBS protocol messages */
52 #define HCI_IBS_SLEEP_IND 0xFE
53 #define HCI_IBS_WAKE_IND 0xFD
54 #define HCI_IBS_WAKE_ACK 0xFC
55 #define HCI_MAX_IBS_SIZE 10
57 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
58 #define IBS_TX_IDLE_TIMEOUT_MS 2000
59 #define CMD_TRANS_TIMEOUT_MS 100
62 #define SUSCLK_RATE_32KHZ 32768
64 /* Controller debug log header */
65 #define QCA_DEBUG_HANDLE 0x2EDC
71 /* HCI_IBS transmit side sleep protocol states */
78 /* HCI_IBS receive side sleep protocol states */
84 /* HCI_IBS transmit and receive side clock state vote */
85 enum hci_ibs_clock_state_vote {
86 HCI_IBS_VOTE_STATS_UPDATE,
87 HCI_IBS_TX_VOTE_CLOCK_ON,
88 HCI_IBS_TX_VOTE_CLOCK_OFF,
89 HCI_IBS_RX_VOTE_CLOCK_ON,
90 HCI_IBS_RX_VOTE_CLOCK_OFF,
95 struct sk_buff *rx_skb;
96 struct sk_buff_head txq;
97 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
98 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
99 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
100 u8 rx_ibs_state; /* HCI_IBS receive side power state */
101 bool tx_vote; /* Clock must be on for TX */
102 bool rx_vote; /* Clock must be on for RX */
103 struct timer_list tx_idle_timer;
105 struct timer_list wake_retrans_timer;
107 struct workqueue_struct *workqueue;
108 struct work_struct ws_awake_rx;
109 struct work_struct ws_awake_device;
110 struct work_struct ws_rx_vote_off;
111 struct work_struct ws_tx_vote_off;
114 /* For debugging purpose */
132 enum qca_speed_type {
138 * Voltage regulator information required for configuring the
139 * QCA Bluetooth chipset
145 unsigned int load_uA;
148 struct qca_vreg_data {
149 enum qca_btsoc_type soc_type;
150 struct qca_vreg *vregs;
155 * Platform data for the QCA Bluetooth power driver.
159 const struct qca_vreg_data *vreg_data;
160 struct regulator_bulk_data *vreg_bulk;
165 struct hci_uart serdev_hu;
166 struct gpio_desc *bt_en;
168 enum qca_btsoc_type btsoc_type;
169 struct qca_power *bt_power;
174 static int qca_power_setup(struct hci_uart *hu, bool on);
175 static void qca_power_shutdown(struct hci_uart *hu);
176 static int qca_power_off(struct hci_dev *hdev);
178 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
180 enum qca_btsoc_type soc_type;
183 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
185 soc_type = qsd->btsoc_type;
193 static void __serial_clock_on(struct tty_struct *tty)
195 /* TODO: Some chipset requires to enable UART clock on client
196 * side to save power consumption or manual work is required.
197 * Please put your code to control UART clock here if needed
201 static void __serial_clock_off(struct tty_struct *tty)
203 /* TODO: Some chipset requires to disable UART clock on client
204 * side to save power consumption or manual work is required.
205 * Please put your code to control UART clock off here if needed
209 /* serial_clock_vote needs to be called with the ibs lock held */
210 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
212 struct qca_data *qca = hu->priv;
215 bool old_vote = (qca->tx_vote | qca->rx_vote);
219 case HCI_IBS_VOTE_STATS_UPDATE:
220 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
223 qca->vote_off_ms += diff;
225 qca->vote_on_ms += diff;
228 case HCI_IBS_TX_VOTE_CLOCK_ON:
234 case HCI_IBS_RX_VOTE_CLOCK_ON:
240 case HCI_IBS_TX_VOTE_CLOCK_OFF:
241 qca->tx_vote = false;
243 new_vote = qca->rx_vote | qca->tx_vote;
246 case HCI_IBS_RX_VOTE_CLOCK_OFF:
247 qca->rx_vote = false;
249 new_vote = qca->rx_vote | qca->tx_vote;
253 BT_ERR("Voting irregularity");
257 if (new_vote != old_vote) {
259 __serial_clock_on(hu->tty);
261 __serial_clock_off(hu->tty);
263 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
264 vote ? "true" : "false");
266 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
270 qca->vote_off_ms += diff;
273 qca->vote_on_ms += diff;
275 qca->vote_last_jif = jiffies;
279 /* Builds and sends an HCI_IBS command packet.
280 * These are very simple packets with only 1 cmd byte.
282 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
285 struct sk_buff *skb = NULL;
286 struct qca_data *qca = hu->priv;
288 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
290 skb = bt_skb_alloc(1, GFP_ATOMIC);
292 BT_ERR("Failed to allocate memory for HCI_IBS packet");
296 /* Assign HCI_IBS type */
297 skb_put_u8(skb, cmd);
299 skb_queue_tail(&qca->txq, skb);
304 static void qca_wq_awake_device(struct work_struct *work)
306 struct qca_data *qca = container_of(work, struct qca_data,
308 struct hci_uart *hu = qca->hu;
309 unsigned long retrans_delay;
311 BT_DBG("hu %p wq awake device", hu);
313 /* Vote for serial clock */
314 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
316 spin_lock(&qca->hci_ibs_lock);
318 /* Send wake indication to device */
319 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
320 BT_ERR("Failed to send WAKE to device");
322 qca->ibs_sent_wakes++;
324 /* Start retransmit timer */
325 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
326 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
328 spin_unlock(&qca->hci_ibs_lock);
330 /* Actually send the packets */
331 hci_uart_tx_wakeup(hu);
334 static void qca_wq_awake_rx(struct work_struct *work)
336 struct qca_data *qca = container_of(work, struct qca_data,
338 struct hci_uart *hu = qca->hu;
340 BT_DBG("hu %p wq awake rx", hu);
342 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
344 spin_lock(&qca->hci_ibs_lock);
345 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
347 /* Always acknowledge device wake up,
348 * sending IBS message doesn't count as TX ON.
350 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
351 BT_ERR("Failed to acknowledge device wake up");
353 qca->ibs_sent_wacks++;
355 spin_unlock(&qca->hci_ibs_lock);
357 /* Actually send the packets */
358 hci_uart_tx_wakeup(hu);
361 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
363 struct qca_data *qca = container_of(work, struct qca_data,
365 struct hci_uart *hu = qca->hu;
367 BT_DBG("hu %p rx clock vote off", hu);
369 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
372 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
374 struct qca_data *qca = container_of(work, struct qca_data,
376 struct hci_uart *hu = qca->hu;
378 BT_DBG("hu %p tx clock vote off", hu);
380 /* Run HCI tx handling unlocked */
381 hci_uart_tx_wakeup(hu);
383 /* Now that message queued to tty driver, vote for tty clocks off.
384 * It is up to the tty driver to pend the clocks off until tx done.
386 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
389 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
391 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
392 struct hci_uart *hu = qca->hu;
395 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
397 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
398 flags, SINGLE_DEPTH_NESTING);
400 switch (qca->tx_ibs_state) {
401 case HCI_IBS_TX_AWAKE:
402 /* TX_IDLE, go to SLEEP */
403 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
404 BT_ERR("Failed to send SLEEP to device");
407 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
408 qca->ibs_sent_slps++;
409 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
412 case HCI_IBS_TX_ASLEEP:
413 case HCI_IBS_TX_WAKING:
417 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
421 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
424 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
426 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
427 struct hci_uart *hu = qca->hu;
428 unsigned long flags, retrans_delay;
429 bool retransmit = false;
431 BT_DBG("hu %p wake retransmit timeout in %d state",
432 hu, qca->tx_ibs_state);
434 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
435 flags, SINGLE_DEPTH_NESTING);
437 switch (qca->tx_ibs_state) {
438 case HCI_IBS_TX_WAKING:
439 /* No WAKE_ACK, retransmit WAKE */
441 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
442 BT_ERR("Failed to acknowledge device wake up");
445 qca->ibs_sent_wakes++;
446 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
447 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
450 case HCI_IBS_TX_ASLEEP:
451 case HCI_IBS_TX_AWAKE:
455 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
459 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
462 hci_uart_tx_wakeup(hu);
465 /* Initialize protocol */
466 static int qca_open(struct hci_uart *hu)
468 struct qca_serdev *qcadev;
469 struct qca_data *qca;
472 BT_DBG("hu %p qca_open", hu);
474 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
478 skb_queue_head_init(&qca->txq);
479 skb_queue_head_init(&qca->tx_wait_q);
480 spin_lock_init(&qca->hci_ibs_lock);
481 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
482 if (!qca->workqueue) {
483 BT_ERR("QCA Workqueue not initialized properly");
488 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
489 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
490 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
491 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
495 /* Assume we start with both sides asleep -- extra wakes OK */
496 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
497 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
499 /* clocks actually on, but we start votes off */
500 qca->tx_vote = false;
501 qca->rx_vote = false;
504 qca->ibs_sent_wacks = 0;
505 qca->ibs_sent_slps = 0;
506 qca->ibs_sent_wakes = 0;
507 qca->ibs_recv_wacks = 0;
508 qca->ibs_recv_slps = 0;
509 qca->ibs_recv_wakes = 0;
510 qca->vote_last_jif = jiffies;
512 qca->vote_off_ms = 0;
515 qca->tx_votes_on = 0;
516 qca->tx_votes_off = 0;
517 qca->rx_votes_on = 0;
518 qca->rx_votes_off = 0;
524 qcadev = serdev_device_get_drvdata(hu->serdev);
525 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
526 gpiod_set_value_cansleep(qcadev->bt_en, 1);
527 /* Controller needs time to bootup. */
530 hu->init_speed = qcadev->init_speed;
531 hu->oper_speed = qcadev->oper_speed;
532 ret = qca_power_setup(hu, true);
534 destroy_workqueue(qca->workqueue);
535 kfree_skb(qca->rx_skb);
543 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
544 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
546 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
547 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
549 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
550 qca->tx_idle_delay, qca->wake_retrans);
555 static void qca_debugfs_init(struct hci_dev *hdev)
557 struct hci_uart *hu = hci_get_drvdata(hdev);
558 struct qca_data *qca = hu->priv;
559 struct dentry *ibs_dir;
565 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
569 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
570 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
571 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
572 &qca->ibs_sent_slps);
573 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
574 &qca->ibs_sent_wakes);
575 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
576 &qca->ibs_sent_wacks);
577 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
578 &qca->ibs_recv_slps);
579 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
580 &qca->ibs_recv_wakes);
581 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
582 &qca->ibs_recv_wacks);
583 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
584 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
585 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
586 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
587 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
588 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
589 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
590 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
591 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
592 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
595 mode = S_IRUGO | S_IWUSR;
596 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
597 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
598 &qca->tx_idle_delay);
601 /* Flush protocol data */
602 static int qca_flush(struct hci_uart *hu)
604 struct qca_data *qca = hu->priv;
606 BT_DBG("hu %p qca flush", hu);
608 skb_queue_purge(&qca->tx_wait_q);
609 skb_queue_purge(&qca->txq);
615 static int qca_close(struct hci_uart *hu)
617 struct qca_serdev *qcadev;
618 struct qca_data *qca = hu->priv;
620 BT_DBG("hu %p qca close", hu);
622 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
624 skb_queue_purge(&qca->tx_wait_q);
625 skb_queue_purge(&qca->txq);
626 del_timer(&qca->tx_idle_timer);
627 del_timer(&qca->wake_retrans_timer);
628 destroy_workqueue(qca->workqueue);
632 qcadev = serdev_device_get_drvdata(hu->serdev);
633 if (qca_is_wcn399x(qcadev->btsoc_type))
634 qca_power_shutdown(hu);
636 gpiod_set_value_cansleep(qcadev->bt_en, 0);
640 kfree_skb(qca->rx_skb);
649 /* Called upon a wake-up-indication from the device.
651 static void device_want_to_wakeup(struct hci_uart *hu)
654 struct qca_data *qca = hu->priv;
656 BT_DBG("hu %p want to wake up", hu);
658 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
660 qca->ibs_recv_wakes++;
662 switch (qca->rx_ibs_state) {
663 case HCI_IBS_RX_ASLEEP:
664 /* Make sure clock is on - we may have turned clock off since
665 * receiving the wake up indicator awake rx clock.
667 queue_work(qca->workqueue, &qca->ws_awake_rx);
668 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
671 case HCI_IBS_RX_AWAKE:
672 /* Always acknowledge device wake up,
673 * sending IBS message doesn't count as TX ON.
675 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
676 BT_ERR("Failed to acknowledge device wake up");
679 qca->ibs_sent_wacks++;
683 /* Any other state is illegal */
684 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
689 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
691 /* Actually send the packets */
692 hci_uart_tx_wakeup(hu);
695 /* Called upon a sleep-indication from the device.
697 static void device_want_to_sleep(struct hci_uart *hu)
700 struct qca_data *qca = hu->priv;
702 BT_DBG("hu %p want to sleep", hu);
704 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
706 qca->ibs_recv_slps++;
708 switch (qca->rx_ibs_state) {
709 case HCI_IBS_RX_AWAKE:
711 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
712 /* Vote off rx clock under workqueue */
713 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
716 case HCI_IBS_RX_ASLEEP:
720 /* Any other state is illegal */
721 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
726 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
729 /* Called upon wake-up-acknowledgement from the device
731 static void device_woke_up(struct hci_uart *hu)
733 unsigned long flags, idle_delay;
734 struct qca_data *qca = hu->priv;
735 struct sk_buff *skb = NULL;
737 BT_DBG("hu %p woke up", hu);
739 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
741 qca->ibs_recv_wacks++;
743 switch (qca->tx_ibs_state) {
744 case HCI_IBS_TX_AWAKE:
745 /* Expect one if we send 2 WAKEs */
746 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
750 case HCI_IBS_TX_WAKING:
751 /* Send pending packets */
752 while ((skb = skb_dequeue(&qca->tx_wait_q)))
753 skb_queue_tail(&qca->txq, skb);
755 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
756 del_timer(&qca->wake_retrans_timer);
757 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
758 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
759 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
762 case HCI_IBS_TX_ASLEEP:
766 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
771 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
773 /* Actually send the packets */
774 hci_uart_tx_wakeup(hu);
777 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
778 * two simultaneous tasklets.
780 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
782 unsigned long flags = 0, idle_delay;
783 struct qca_data *qca = hu->priv;
785 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
788 /* Prepend skb with frame type */
789 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
791 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
793 /* Don't go to sleep in middle of patch download or
794 * Out-Of-Band(GPIOs control) sleep is selected.
796 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) {
797 skb_queue_tail(&qca->txq, skb);
798 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
802 /* Act according to current state */
803 switch (qca->tx_ibs_state) {
804 case HCI_IBS_TX_AWAKE:
805 BT_DBG("Device awake, sending normally");
806 skb_queue_tail(&qca->txq, skb);
807 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
808 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
811 case HCI_IBS_TX_ASLEEP:
812 BT_DBG("Device asleep, waking up and queueing packet");
813 /* Save packet for later */
814 skb_queue_tail(&qca->tx_wait_q, skb);
816 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
817 /* Schedule a work queue to wake up device */
818 queue_work(qca->workqueue, &qca->ws_awake_device);
821 case HCI_IBS_TX_WAKING:
822 BT_DBG("Device waking up, queueing packet");
823 /* Transient state; just keep packet for later */
824 skb_queue_tail(&qca->tx_wait_q, skb);
828 BT_ERR("Illegal tx state: %d (losing packet)",
834 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
839 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
841 struct hci_uart *hu = hci_get_drvdata(hdev);
843 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
845 device_want_to_sleep(hu);
851 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
853 struct hci_uart *hu = hci_get_drvdata(hdev);
855 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
857 device_want_to_wakeup(hu);
863 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
865 struct hci_uart *hu = hci_get_drvdata(hdev);
867 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
875 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
877 /* We receive debug logs from chip as an ACL packets.
878 * Instead of sending the data to ACL to decode the
879 * received data, we are pushing them to the above layers
880 * as a diagnostic packet.
882 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
883 return hci_recv_diag(hdev, skb);
885 return hci_recv_frame(hdev, skb);
888 #define QCA_IBS_SLEEP_IND_EVENT \
889 .type = HCI_IBS_SLEEP_IND, \
893 .maxlen = HCI_MAX_IBS_SIZE
895 #define QCA_IBS_WAKE_IND_EVENT \
896 .type = HCI_IBS_WAKE_IND, \
900 .maxlen = HCI_MAX_IBS_SIZE
902 #define QCA_IBS_WAKE_ACK_EVENT \
903 .type = HCI_IBS_WAKE_ACK, \
907 .maxlen = HCI_MAX_IBS_SIZE
909 static const struct h4_recv_pkt qca_recv_pkts[] = {
910 { H4_RECV_ACL, .recv = qca_recv_acl_data },
911 { H4_RECV_SCO, .recv = hci_recv_frame },
912 { H4_RECV_EVENT, .recv = hci_recv_frame },
913 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
914 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
915 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
918 static int qca_recv(struct hci_uart *hu, const void *data, int count)
920 struct qca_data *qca = hu->priv;
922 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
925 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
926 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
927 if (IS_ERR(qca->rx_skb)) {
928 int err = PTR_ERR(qca->rx_skb);
929 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
937 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
939 struct qca_data *qca = hu->priv;
941 return skb_dequeue(&qca->txq);
944 static uint8_t qca_get_baudrate_value(int speed)
948 return QCA_BAUDRATE_9600;
950 return QCA_BAUDRATE_19200;
952 return QCA_BAUDRATE_38400;
954 return QCA_BAUDRATE_57600;
956 return QCA_BAUDRATE_115200;
958 return QCA_BAUDRATE_230400;
960 return QCA_BAUDRATE_460800;
962 return QCA_BAUDRATE_500000;
964 return QCA_BAUDRATE_921600;
966 return QCA_BAUDRATE_1000000;
968 return QCA_BAUDRATE_2000000;
970 return QCA_BAUDRATE_3000000;
972 return QCA_BAUDRATE_3200000;
974 return QCA_BAUDRATE_3500000;
976 return QCA_BAUDRATE_115200;
980 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
982 struct hci_uart *hu = hci_get_drvdata(hdev);
983 struct qca_data *qca = hu->priv;
985 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
987 if (baudrate > QCA_BAUDRATE_3200000)
992 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
994 bt_dev_err(hdev, "Failed to allocate baudrate packet");
998 /* Assign commands to change baudrate and packet type. */
999 skb_put_data(skb, cmd, sizeof(cmd));
1000 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1002 skb_queue_tail(&qca->txq, skb);
1003 hci_uart_tx_wakeup(hu);
1005 /* Wait for the baudrate change request to be sent */
1007 while (!skb_queue_empty(&qca->txq))
1008 usleep_range(100, 200);
1011 serdev_device_wait_until_sent(hu->serdev,
1012 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1014 /* Give the controller time to process the request */
1015 if (qca_is_wcn399x(qca_soc_type(hu)))
1023 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1026 serdev_device_set_baudrate(hu->serdev, speed);
1028 hci_uart_set_baudrate(hu, speed);
1031 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1034 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1035 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1037 /* These power pulses are single byte command which are sent
1038 * at required baudrate to wcn3990. On wcn3990, we have an external
1039 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1040 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1041 * and also we use the same power inputs to turn on and off for
1042 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1043 * we send a power on pulse at 115200 bps. This algorithm will help to
1044 * save power. Disabling hardware flow control is mandatory while
1045 * sending power pulses to SoC.
1047 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1049 serdev_device_write_flush(hu->serdev);
1050 hci_uart_set_flow_control(hu, true);
1051 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1053 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1057 serdev_device_wait_until_sent(hu->serdev, timeout);
1058 hci_uart_set_flow_control(hu, false);
1060 /* Give to controller time to boot/shutdown */
1069 static unsigned int qca_get_speed(struct hci_uart *hu,
1070 enum qca_speed_type speed_type)
1072 unsigned int speed = 0;
1074 if (speed_type == QCA_INIT_SPEED) {
1076 speed = hu->init_speed;
1077 else if (hu->proto->init_speed)
1078 speed = hu->proto->init_speed;
1081 speed = hu->oper_speed;
1082 else if (hu->proto->oper_speed)
1083 speed = hu->proto->oper_speed;
1089 static int qca_check_speeds(struct hci_uart *hu)
1091 if (qca_is_wcn399x(qca_soc_type(hu))) {
1092 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1093 !qca_get_speed(hu, QCA_OPER_SPEED))
1096 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1097 !qca_get_speed(hu, QCA_OPER_SPEED))
1104 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1106 unsigned int speed, qca_baudrate;
1109 if (speed_type == QCA_INIT_SPEED) {
1110 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1112 host_set_baudrate(hu, speed);
1114 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1116 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1120 /* Disable flow control for wcn3990 to deassert RTS while
1121 * changing the baudrate of chip and host.
1123 if (qca_is_wcn399x(soc_type))
1124 hci_uart_set_flow_control(hu, true);
1126 qca_baudrate = qca_get_baudrate_value(speed);
1127 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1128 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1132 host_set_baudrate(hu, speed);
1135 if (qca_is_wcn399x(soc_type))
1136 hci_uart_set_flow_control(hu, false);
1142 static int qca_wcn3990_init(struct hci_uart *hu)
1144 struct qca_serdev *qcadev;
1147 /* Check for vregs status, may be hci down has turned
1148 * off the voltage regulator.
1150 qcadev = serdev_device_get_drvdata(hu->serdev);
1151 if (!qcadev->bt_power->vregs_on) {
1152 serdev_device_close(hu->serdev);
1153 ret = qca_power_setup(hu, true);
1157 ret = serdev_device_open(hu->serdev);
1159 bt_dev_err(hu->hdev, "failed to open port");
1164 /* Forcefully enable wcn3990 to enter in to boot mode. */
1165 host_set_baudrate(hu, 2400);
1166 ret = qca_send_power_pulse(hu, false);
1170 qca_set_speed(hu, QCA_INIT_SPEED);
1171 ret = qca_send_power_pulse(hu, true);
1175 /* Now the device is in ready state to communicate with host.
1176 * To sync host with device we need to reopen port.
1177 * Without this, we will have RTS and CTS synchronization
1180 serdev_device_close(hu->serdev);
1181 ret = serdev_device_open(hu->serdev);
1183 bt_dev_err(hu->hdev, "failed to open port");
1187 hci_uart_set_flow_control(hu, false);
1192 static int qca_setup(struct hci_uart *hu)
1194 struct hci_dev *hdev = hu->hdev;
1195 struct qca_data *qca = hu->priv;
1196 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1197 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1201 ret = qca_check_speeds(hu);
1205 /* Patch downloading has to be done without IBS mode */
1206 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1208 if (qca_is_wcn399x(soc_type)) {
1209 bt_dev_info(hdev, "setting up wcn3990");
1211 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1212 * setup for every hci up.
1214 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1215 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1216 hu->hdev->shutdown = qca_power_off;
1217 ret = qca_wcn3990_init(hu);
1221 ret = qca_read_soc_version(hdev, &soc_ver);
1225 bt_dev_info(hdev, "ROME setup");
1226 qca_set_speed(hu, QCA_INIT_SPEED);
1229 /* Setup user speed if needed */
1230 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1232 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1236 qca_baudrate = qca_get_baudrate_value(speed);
1239 if (!qca_is_wcn399x(soc_type)) {
1240 /* Get QCA version information */
1241 ret = qca_read_soc_version(hdev, &soc_ver);
1246 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1247 /* Setup patch / NVM configurations */
1248 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver);
1250 set_bit(QCA_IBS_ENABLED, &qca->flags);
1251 qca_debugfs_init(hdev);
1252 } else if (ret == -ENOENT) {
1253 /* No patch/nvm-config found, run with original fw/config */
1255 } else if (ret == -EAGAIN) {
1257 * Userspace firmware loader will return -EAGAIN in case no
1258 * patch/nvm-config is found, so run with original fw/config.
1264 if (qca_is_wcn399x(soc_type))
1265 hu->hdev->set_bdaddr = qca_set_bdaddr;
1267 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1272 static struct hci_uart_proto qca_proto = {
1276 .init_speed = 115200,
1277 .oper_speed = 3000000,
1283 .enqueue = qca_enqueue,
1284 .dequeue = qca_dequeue,
1287 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1288 .soc_type = QCA_WCN3990,
1289 .vregs = (struct qca_vreg []) {
1290 { "vddio", 1800000, 1900000, 15000 },
1291 { "vddxo", 1800000, 1900000, 80000 },
1292 { "vddrf", 1300000, 1350000, 300000 },
1293 { "vddch0", 3300000, 3400000, 450000 },
1298 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1299 .soc_type = QCA_WCN3998,
1300 .vregs = (struct qca_vreg []) {
1301 { "vddio", 1800000, 1900000, 10000 },
1302 { "vddxo", 1800000, 1900000, 80000 },
1303 { "vddrf", 1300000, 1352000, 300000 },
1304 { "vddch0", 3300000, 3300000, 450000 },
1309 static void qca_power_shutdown(struct hci_uart *hu)
1311 struct qca_data *qca = hu->priv;
1312 unsigned long flags;
1314 /* From this point we go into power off state. But serial port is
1315 * still open, stop queueing the IBS data and flush all the buffered
1318 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1319 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1321 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1323 host_set_baudrate(hu, 2400);
1324 qca_send_power_pulse(hu, false);
1325 qca_power_setup(hu, false);
1328 static int qca_power_off(struct hci_dev *hdev)
1330 struct hci_uart *hu = hci_get_drvdata(hdev);
1332 qca_power_shutdown(hu);
1336 static int qca_enable_regulator(struct qca_vreg vregs,
1337 struct regulator *regulator)
1341 ret = regulator_set_voltage(regulator, vregs.min_uV,
1347 ret = regulator_set_load(regulator,
1353 return regulator_enable(regulator);
1357 static void qca_disable_regulator(struct qca_vreg vregs,
1358 struct regulator *regulator)
1360 regulator_disable(regulator);
1361 regulator_set_voltage(regulator, 0, vregs.max_uV);
1363 regulator_set_load(regulator, 0);
1367 static int qca_power_setup(struct hci_uart *hu, bool on)
1369 struct qca_vreg *vregs;
1370 struct regulator_bulk_data *vreg_bulk;
1371 struct qca_serdev *qcadev;
1372 int i, num_vregs, ret = 0;
1374 qcadev = serdev_device_get_drvdata(hu->serdev);
1375 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1376 !qcadev->bt_power->vreg_bulk)
1379 vregs = qcadev->bt_power->vreg_data->vregs;
1380 vreg_bulk = qcadev->bt_power->vreg_bulk;
1381 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1382 BT_DBG("on: %d", on);
1383 if (on && !qcadev->bt_power->vregs_on) {
1384 for (i = 0; i < num_vregs; i++) {
1385 ret = qca_enable_regulator(vregs[i],
1386 vreg_bulk[i].consumer);
1392 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1393 /* turn off regulators which are enabled */
1394 for (i = i - 1; i >= 0; i--)
1395 qca_disable_regulator(vregs[i],
1396 vreg_bulk[i].consumer);
1398 qcadev->bt_power->vregs_on = true;
1400 } else if (!on && qcadev->bt_power->vregs_on) {
1401 /* turn off regulator in reverse order */
1402 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1403 for ( ; i >= 0; i--)
1404 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1406 qcadev->bt_power->vregs_on = false;
1412 static int qca_init_regulators(struct qca_power *qca,
1413 const struct qca_vreg *vregs, size_t num_vregs)
1417 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
1418 sizeof(struct regulator_bulk_data),
1420 if (!qca->vreg_bulk)
1423 for (i = 0; i < num_vregs; i++)
1424 qca->vreg_bulk[i].supply = vregs[i].name;
1426 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1429 static int qca_serdev_probe(struct serdev_device *serdev)
1431 struct qca_serdev *qcadev;
1432 const struct qca_vreg_data *data;
1435 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1439 qcadev->serdev_hu.serdev = serdev;
1440 data = of_device_get_match_data(&serdev->dev);
1441 serdev_device_set_drvdata(serdev, qcadev);
1442 if (data && qca_is_wcn399x(data->soc_type)) {
1443 qcadev->btsoc_type = data->soc_type;
1444 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1445 sizeof(struct qca_power),
1447 if (!qcadev->bt_power)
1450 qcadev->bt_power->dev = &serdev->dev;
1451 qcadev->bt_power->vreg_data = data;
1452 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1455 BT_ERR("Failed to init regulators:%d", err);
1459 qcadev->bt_power->vregs_on = false;
1461 device_property_read_u32(&serdev->dev, "max-speed",
1462 &qcadev->oper_speed);
1463 if (!qcadev->oper_speed)
1464 BT_DBG("UART will pick default operating speed");
1466 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1468 BT_ERR("wcn3990 serdev registration failed");
1472 qcadev->btsoc_type = QCA_ROME;
1473 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1475 if (IS_ERR(qcadev->bt_en)) {
1476 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1477 return PTR_ERR(qcadev->bt_en);
1480 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1481 if (IS_ERR(qcadev->susclk)) {
1482 dev_err(&serdev->dev, "failed to acquire clk\n");
1483 return PTR_ERR(qcadev->susclk);
1486 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1490 err = clk_prepare_enable(qcadev->susclk);
1494 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1496 clk_disable_unprepare(qcadev->susclk);
1503 static void qca_serdev_remove(struct serdev_device *serdev)
1505 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1507 if (qca_is_wcn399x(qcadev->btsoc_type))
1508 qca_power_shutdown(&qcadev->serdev_hu);
1510 clk_disable_unprepare(qcadev->susclk);
1512 hci_uart_unregister_device(&qcadev->serdev_hu);
1515 static const struct of_device_id qca_bluetooth_of_match[] = {
1516 { .compatible = "qcom,qca6174-bt" },
1517 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1518 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1521 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1523 static struct serdev_device_driver qca_serdev_driver = {
1524 .probe = qca_serdev_probe,
1525 .remove = qca_serdev_remove,
1527 .name = "hci_uart_qca",
1528 .of_match_table = qca_bluetooth_of_match,
1532 int __init qca_init(void)
1534 serdev_device_driver_register(&qca_serdev_driver);
1536 return hci_uart_register_proto(&qca_proto);
1539 int __exit qca_deinit(void)
1541 serdev_device_driver_unregister(&qca_serdev_driver);
1543 return hci_uart_unregister_proto(&qca_proto);