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 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/gpio/consumer.h>
35 #include <linux/mod_devicetable.h>
36 #include <linux/module.h>
37 #include <linux/serdev.h>
39 #include <net/bluetooth/bluetooth.h>
40 #include <net/bluetooth/hci_core.h>
45 /* HCI_IBS protocol messages */
46 #define HCI_IBS_SLEEP_IND 0xFE
47 #define HCI_IBS_WAKE_IND 0xFD
48 #define HCI_IBS_WAKE_ACK 0xFC
49 #define HCI_MAX_IBS_SIZE 10
51 /* Controller states */
52 #define STATE_IN_BAND_SLEEP_ENABLED 1
54 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
55 #define IBS_TX_IDLE_TIMEOUT_MS 2000
56 #define BAUDRATE_SETTLE_TIMEOUT_MS 300
59 #define SUSCLK_RATE_32KHZ 32768
61 /* HCI_IBS transmit side sleep protocol states */
68 /* HCI_IBS receive side sleep protocol states */
74 /* HCI_IBS transmit and receive side clock state vote */
75 enum hci_ibs_clock_state_vote {
76 HCI_IBS_VOTE_STATS_UPDATE,
77 HCI_IBS_TX_VOTE_CLOCK_ON,
78 HCI_IBS_TX_VOTE_CLOCK_OFF,
79 HCI_IBS_RX_VOTE_CLOCK_ON,
80 HCI_IBS_RX_VOTE_CLOCK_OFF,
85 struct sk_buff *rx_skb;
86 struct sk_buff_head txq;
87 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
88 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
89 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
90 u8 rx_ibs_state; /* HCI_IBS receive side power state */
91 bool tx_vote; /* Clock must be on for TX */
92 bool rx_vote; /* Clock must be on for RX */
93 struct timer_list tx_idle_timer;
95 struct timer_list wake_retrans_timer;
97 struct workqueue_struct *workqueue;
98 struct work_struct ws_awake_rx;
99 struct work_struct ws_awake_device;
100 struct work_struct ws_rx_vote_off;
101 struct work_struct ws_tx_vote_off;
104 /* For debugging purpose */
123 struct hci_uart serdev_hu;
124 struct gpio_desc *bt_en;
128 static void __serial_clock_on(struct tty_struct *tty)
130 /* TODO: Some chipset requires to enable UART clock on client
131 * side to save power consumption or manual work is required.
132 * Please put your code to control UART clock here if needed
136 static void __serial_clock_off(struct tty_struct *tty)
138 /* TODO: Some chipset requires to disable UART clock on client
139 * side to save power consumption or manual work is required.
140 * Please put your code to control UART clock off here if needed
144 /* serial_clock_vote needs to be called with the ibs lock held */
145 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
147 struct qca_data *qca = hu->priv;
150 bool old_vote = (qca->tx_vote | qca->rx_vote);
154 case HCI_IBS_VOTE_STATS_UPDATE:
155 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
158 qca->vote_off_ms += diff;
160 qca->vote_on_ms += diff;
163 case HCI_IBS_TX_VOTE_CLOCK_ON:
169 case HCI_IBS_RX_VOTE_CLOCK_ON:
175 case HCI_IBS_TX_VOTE_CLOCK_OFF:
176 qca->tx_vote = false;
178 new_vote = qca->rx_vote | qca->tx_vote;
181 case HCI_IBS_RX_VOTE_CLOCK_OFF:
182 qca->rx_vote = false;
184 new_vote = qca->rx_vote | qca->tx_vote;
188 BT_ERR("Voting irregularity");
192 if (new_vote != old_vote) {
194 __serial_clock_on(hu->tty);
196 __serial_clock_off(hu->tty);
198 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
199 vote ? "true" : "false");
201 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
205 qca->vote_off_ms += diff;
208 qca->vote_on_ms += diff;
210 qca->vote_last_jif = jiffies;
214 /* Builds and sends an HCI_IBS command packet.
215 * These are very simple packets with only 1 cmd byte.
217 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
220 struct sk_buff *skb = NULL;
221 struct qca_data *qca = hu->priv;
223 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
225 skb = bt_skb_alloc(1, GFP_ATOMIC);
227 BT_ERR("Failed to allocate memory for HCI_IBS packet");
231 /* Assign HCI_IBS type */
232 skb_put_u8(skb, cmd);
234 skb_queue_tail(&qca->txq, skb);
239 static void qca_wq_awake_device(struct work_struct *work)
241 struct qca_data *qca = container_of(work, struct qca_data,
243 struct hci_uart *hu = qca->hu;
244 unsigned long retrans_delay;
246 BT_DBG("hu %p wq awake device", hu);
248 /* Vote for serial clock */
249 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
251 spin_lock(&qca->hci_ibs_lock);
253 /* Send wake indication to device */
254 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
255 BT_ERR("Failed to send WAKE to device");
257 qca->ibs_sent_wakes++;
259 /* Start retransmit timer */
260 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
261 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
263 spin_unlock(&qca->hci_ibs_lock);
265 /* Actually send the packets */
266 hci_uart_tx_wakeup(hu);
269 static void qca_wq_awake_rx(struct work_struct *work)
271 struct qca_data *qca = container_of(work, struct qca_data,
273 struct hci_uart *hu = qca->hu;
275 BT_DBG("hu %p wq awake rx", hu);
277 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
279 spin_lock(&qca->hci_ibs_lock);
280 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
282 /* Always acknowledge device wake up,
283 * sending IBS message doesn't count as TX ON.
285 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
286 BT_ERR("Failed to acknowledge device wake up");
288 qca->ibs_sent_wacks++;
290 spin_unlock(&qca->hci_ibs_lock);
292 /* Actually send the packets */
293 hci_uart_tx_wakeup(hu);
296 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
298 struct qca_data *qca = container_of(work, struct qca_data,
300 struct hci_uart *hu = qca->hu;
302 BT_DBG("hu %p rx clock vote off", hu);
304 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
307 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
309 struct qca_data *qca = container_of(work, struct qca_data,
311 struct hci_uart *hu = qca->hu;
313 BT_DBG("hu %p tx clock vote off", hu);
315 /* Run HCI tx handling unlocked */
316 hci_uart_tx_wakeup(hu);
318 /* Now that message queued to tty driver, vote for tty clocks off.
319 * It is up to the tty driver to pend the clocks off until tx done.
321 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
324 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
326 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
327 struct hci_uart *hu = qca->hu;
330 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
332 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
333 flags, SINGLE_DEPTH_NESTING);
335 switch (qca->tx_ibs_state) {
336 case HCI_IBS_TX_AWAKE:
337 /* TX_IDLE, go to SLEEP */
338 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
339 BT_ERR("Failed to send SLEEP to device");
342 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
343 qca->ibs_sent_slps++;
344 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
347 case HCI_IBS_TX_ASLEEP:
348 case HCI_IBS_TX_WAKING:
352 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
356 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
359 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
361 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
362 struct hci_uart *hu = qca->hu;
363 unsigned long flags, retrans_delay;
364 bool retransmit = false;
366 BT_DBG("hu %p wake retransmit timeout in %d state",
367 hu, qca->tx_ibs_state);
369 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
370 flags, SINGLE_DEPTH_NESTING);
372 switch (qca->tx_ibs_state) {
373 case HCI_IBS_TX_WAKING:
374 /* No WAKE_ACK, retransmit WAKE */
376 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
377 BT_ERR("Failed to acknowledge device wake up");
380 qca->ibs_sent_wakes++;
381 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
382 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
385 case HCI_IBS_TX_ASLEEP:
386 case HCI_IBS_TX_AWAKE:
390 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
394 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
397 hci_uart_tx_wakeup(hu);
400 /* Initialize protocol */
401 static int qca_open(struct hci_uart *hu)
403 struct qca_serdev *qcadev;
404 struct qca_data *qca;
406 BT_DBG("hu %p qca_open", hu);
408 qca = kzalloc(sizeof(struct qca_data), GFP_ATOMIC);
412 skb_queue_head_init(&qca->txq);
413 skb_queue_head_init(&qca->tx_wait_q);
414 spin_lock_init(&qca->hci_ibs_lock);
415 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
416 if (!qca->workqueue) {
417 BT_ERR("QCA Workqueue not initialized properly");
422 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
423 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
424 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
425 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
429 /* Assume we start with both sides asleep -- extra wakes OK */
430 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
431 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
433 /* clocks actually on, but we start votes off */
434 qca->tx_vote = false;
435 qca->rx_vote = false;
438 qca->ibs_sent_wacks = 0;
439 qca->ibs_sent_slps = 0;
440 qca->ibs_sent_wakes = 0;
441 qca->ibs_recv_wacks = 0;
442 qca->ibs_recv_slps = 0;
443 qca->ibs_recv_wakes = 0;
444 qca->vote_last_jif = jiffies;
446 qca->vote_off_ms = 0;
449 qca->tx_votes_on = 0;
450 qca->tx_votes_off = 0;
451 qca->rx_votes_on = 0;
452 qca->rx_votes_off = 0;
456 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
457 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
459 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
460 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
463 serdev_device_open(hu->serdev);
465 qcadev = serdev_device_get_drvdata(hu->serdev);
466 gpiod_set_value_cansleep(qcadev->bt_en, 1);
469 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
470 qca->tx_idle_delay, qca->wake_retrans);
475 static void qca_debugfs_init(struct hci_dev *hdev)
477 struct hci_uart *hu = hci_get_drvdata(hdev);
478 struct qca_data *qca = hu->priv;
479 struct dentry *ibs_dir;
485 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
489 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
490 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
491 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
492 &qca->ibs_sent_slps);
493 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
494 &qca->ibs_sent_wakes);
495 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
496 &qca->ibs_sent_wacks);
497 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
498 &qca->ibs_recv_slps);
499 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
500 &qca->ibs_recv_wakes);
501 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
502 &qca->ibs_recv_wacks);
503 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
504 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
505 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
506 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
507 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
508 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
509 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
510 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
511 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
512 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
515 mode = S_IRUGO | S_IWUSR;
516 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
517 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
518 &qca->tx_idle_delay);
521 /* Flush protocol data */
522 static int qca_flush(struct hci_uart *hu)
524 struct qca_data *qca = hu->priv;
526 BT_DBG("hu %p qca flush", hu);
528 skb_queue_purge(&qca->tx_wait_q);
529 skb_queue_purge(&qca->txq);
535 static int qca_close(struct hci_uart *hu)
537 struct qca_serdev *qcadev;
538 struct qca_data *qca = hu->priv;
540 BT_DBG("hu %p qca close", hu);
542 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
544 skb_queue_purge(&qca->tx_wait_q);
545 skb_queue_purge(&qca->txq);
546 del_timer(&qca->tx_idle_timer);
547 del_timer(&qca->wake_retrans_timer);
548 destroy_workqueue(qca->workqueue);
552 serdev_device_close(hu->serdev);
554 qcadev = serdev_device_get_drvdata(hu->serdev);
555 gpiod_set_value_cansleep(qcadev->bt_en, 0);
558 kfree_skb(qca->rx_skb);
567 /* Called upon a wake-up-indication from the device.
569 static void device_want_to_wakeup(struct hci_uart *hu)
572 struct qca_data *qca = hu->priv;
574 BT_DBG("hu %p want to wake up", hu);
576 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
578 qca->ibs_recv_wakes++;
580 switch (qca->rx_ibs_state) {
581 case HCI_IBS_RX_ASLEEP:
582 /* Make sure clock is on - we may have turned clock off since
583 * receiving the wake up indicator awake rx clock.
585 queue_work(qca->workqueue, &qca->ws_awake_rx);
586 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
589 case HCI_IBS_RX_AWAKE:
590 /* Always acknowledge device wake up,
591 * sending IBS message doesn't count as TX ON.
593 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
594 BT_ERR("Failed to acknowledge device wake up");
597 qca->ibs_sent_wacks++;
601 /* Any other state is illegal */
602 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
607 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
609 /* Actually send the packets */
610 hci_uart_tx_wakeup(hu);
613 /* Called upon a sleep-indication from the device.
615 static void device_want_to_sleep(struct hci_uart *hu)
618 struct qca_data *qca = hu->priv;
620 BT_DBG("hu %p want to sleep", hu);
622 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
624 qca->ibs_recv_slps++;
626 switch (qca->rx_ibs_state) {
627 case HCI_IBS_RX_AWAKE:
629 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
630 /* Vote off rx clock under workqueue */
631 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
634 case HCI_IBS_RX_ASLEEP:
638 /* Any other state is illegal */
639 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
644 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
647 /* Called upon wake-up-acknowledgement from the device
649 static void device_woke_up(struct hci_uart *hu)
651 unsigned long flags, idle_delay;
652 struct qca_data *qca = hu->priv;
653 struct sk_buff *skb = NULL;
655 BT_DBG("hu %p woke up", hu);
657 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
659 qca->ibs_recv_wacks++;
661 switch (qca->tx_ibs_state) {
662 case HCI_IBS_TX_AWAKE:
663 /* Expect one if we send 2 WAKEs */
664 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
668 case HCI_IBS_TX_WAKING:
669 /* Send pending packets */
670 while ((skb = skb_dequeue(&qca->tx_wait_q)))
671 skb_queue_tail(&qca->txq, skb);
673 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
674 del_timer(&qca->wake_retrans_timer);
675 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
676 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
677 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
680 case HCI_IBS_TX_ASLEEP:
684 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
689 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
691 /* Actually send the packets */
692 hci_uart_tx_wakeup(hu);
695 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
696 * two simultaneous tasklets.
698 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
700 unsigned long flags = 0, idle_delay;
701 struct qca_data *qca = hu->priv;
703 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
706 /* Prepend skb with frame type */
707 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
709 /* Don't go to sleep in middle of patch download or
710 * Out-Of-Band(GPIOs control) sleep is selected.
712 if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags)) {
713 skb_queue_tail(&qca->txq, skb);
717 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
719 /* Act according to current state */
720 switch (qca->tx_ibs_state) {
721 case HCI_IBS_TX_AWAKE:
722 BT_DBG("Device awake, sending normally");
723 skb_queue_tail(&qca->txq, skb);
724 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
725 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
728 case HCI_IBS_TX_ASLEEP:
729 BT_DBG("Device asleep, waking up and queueing packet");
730 /* Save packet for later */
731 skb_queue_tail(&qca->tx_wait_q, skb);
733 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
734 /* Schedule a work queue to wake up device */
735 queue_work(qca->workqueue, &qca->ws_awake_device);
738 case HCI_IBS_TX_WAKING:
739 BT_DBG("Device waking up, queueing packet");
740 /* Transient state; just keep packet for later */
741 skb_queue_tail(&qca->tx_wait_q, skb);
745 BT_ERR("Illegal tx state: %d (losing packet)",
751 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
756 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
758 struct hci_uart *hu = hci_get_drvdata(hdev);
760 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
762 device_want_to_sleep(hu);
768 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
770 struct hci_uart *hu = hci_get_drvdata(hdev);
772 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
774 device_want_to_wakeup(hu);
780 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
782 struct hci_uart *hu = hci_get_drvdata(hdev);
784 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
792 #define QCA_IBS_SLEEP_IND_EVENT \
793 .type = HCI_IBS_SLEEP_IND, \
797 .maxlen = HCI_MAX_IBS_SIZE
799 #define QCA_IBS_WAKE_IND_EVENT \
800 .type = HCI_IBS_WAKE_IND, \
804 .maxlen = HCI_MAX_IBS_SIZE
806 #define QCA_IBS_WAKE_ACK_EVENT \
807 .type = HCI_IBS_WAKE_ACK, \
811 .maxlen = HCI_MAX_IBS_SIZE
813 static const struct h4_recv_pkt qca_recv_pkts[] = {
814 { H4_RECV_ACL, .recv = hci_recv_frame },
815 { H4_RECV_SCO, .recv = hci_recv_frame },
816 { H4_RECV_EVENT, .recv = hci_recv_frame },
817 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
818 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
819 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
822 static int qca_recv(struct hci_uart *hu, const void *data, int count)
824 struct qca_data *qca = hu->priv;
826 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
829 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
830 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
831 if (IS_ERR(qca->rx_skb)) {
832 int err = PTR_ERR(qca->rx_skb);
833 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
841 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
843 struct qca_data *qca = hu->priv;
845 return skb_dequeue(&qca->txq);
848 static uint8_t qca_get_baudrate_value(int speed)
852 return QCA_BAUDRATE_9600;
854 return QCA_BAUDRATE_19200;
856 return QCA_BAUDRATE_38400;
858 return QCA_BAUDRATE_57600;
860 return QCA_BAUDRATE_115200;
862 return QCA_BAUDRATE_230400;
864 return QCA_BAUDRATE_460800;
866 return QCA_BAUDRATE_500000;
868 return QCA_BAUDRATE_921600;
870 return QCA_BAUDRATE_1000000;
872 return QCA_BAUDRATE_2000000;
874 return QCA_BAUDRATE_3000000;
876 return QCA_BAUDRATE_3500000;
878 return QCA_BAUDRATE_115200;
882 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
884 struct hci_uart *hu = hci_get_drvdata(hdev);
885 struct qca_data *qca = hu->priv;
887 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
889 if (baudrate > QCA_BAUDRATE_3000000)
894 skb = bt_skb_alloc(sizeof(cmd), GFP_ATOMIC);
896 bt_dev_err(hdev, "Failed to allocate baudrate packet");
900 /* Assign commands to change baudrate and packet type. */
901 skb_put_data(skb, cmd, sizeof(cmd));
902 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
904 skb_queue_tail(&qca->txq, skb);
905 hci_uart_tx_wakeup(hu);
907 /* wait 300ms to change new baudrate on controller side
908 * controller will come back after they receive this HCI command
909 * then host can communicate with new baudrate to controller
911 set_current_state(TASK_UNINTERRUPTIBLE);
912 schedule_timeout(msecs_to_jiffies(BAUDRATE_SETTLE_TIMEOUT_MS));
913 set_current_state(TASK_RUNNING);
918 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
921 serdev_device_set_baudrate(hu->serdev, speed);
923 hci_uart_set_baudrate(hu, speed);
926 static int qca_setup(struct hci_uart *hu)
928 struct hci_dev *hdev = hu->hdev;
929 struct qca_data *qca = hu->priv;
930 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
933 bt_dev_info(hdev, "ROME setup");
935 /* Patch downloading has to be done without IBS mode */
936 clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
938 /* Setup initial baudrate */
941 speed = hu->init_speed;
942 else if (hu->proto->init_speed)
943 speed = hu->proto->init_speed;
946 host_set_baudrate(hu, speed);
948 /* Setup user speed if needed */
951 speed = hu->oper_speed;
952 else if (hu->proto->oper_speed)
953 speed = hu->proto->oper_speed;
956 qca_baudrate = qca_get_baudrate_value(speed);
958 bt_dev_info(hdev, "Set UART speed to %d", speed);
959 ret = qca_set_baudrate(hdev, qca_baudrate);
961 bt_dev_err(hdev, "Failed to change the baud rate (%d)",
965 host_set_baudrate(hu, speed);
968 /* Setup patch / NVM configurations */
969 ret = qca_uart_setup_rome(hdev, qca_baudrate);
971 set_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
972 qca_debugfs_init(hdev);
973 } else if (ret == -ENOENT) {
974 /* No patch/nvm-config found, run with original fw/config */
976 } else if (ret == -EAGAIN) {
978 * Userspace firmware loader will return -EAGAIN in case no
979 * patch/nvm-config is found, so run with original fw/config.
985 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
990 static struct hci_uart_proto qca_proto = {
994 .init_speed = 115200,
995 .oper_speed = 3000000,
1001 .enqueue = qca_enqueue,
1002 .dequeue = qca_dequeue,
1005 static int qca_serdev_probe(struct serdev_device *serdev)
1007 struct qca_serdev *qcadev;
1010 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1014 qcadev->serdev_hu.serdev = serdev;
1015 serdev_device_set_drvdata(serdev, qcadev);
1017 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1019 if (IS_ERR(qcadev->bt_en)) {
1020 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1021 return PTR_ERR(qcadev->bt_en);
1024 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1025 if (IS_ERR(qcadev->susclk)) {
1026 dev_err(&serdev->dev, "failed to acquire clk\n");
1027 return PTR_ERR(qcadev->susclk);
1030 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1034 err = clk_prepare_enable(qcadev->susclk);
1038 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1040 clk_disable_unprepare(qcadev->susclk);
1045 static void qca_serdev_remove(struct serdev_device *serdev)
1047 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1049 hci_uart_unregister_device(&qcadev->serdev_hu);
1051 clk_disable_unprepare(qcadev->susclk);
1054 static const struct of_device_id qca_bluetooth_of_match[] = {
1055 { .compatible = "qcom,qca6174-bt" },
1058 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1060 static struct serdev_device_driver qca_serdev_driver = {
1061 .probe = qca_serdev_probe,
1062 .remove = qca_serdev_remove,
1064 .name = "hci_uart_qca",
1065 .of_match_table = qca_bluetooth_of_match,
1069 int __init qca_init(void)
1071 serdev_device_driver_register(&qca_serdev_driver);
1073 return hci_uart_register_proto(&qca_proto);
1076 int __exit qca_deinit(void)
1078 serdev_device_driver_unregister(&qca_serdev_driver);
1080 return hci_uart_unregister_proto(&qca_proto);