interconnect: qcom: Fix Kconfig indentation
[linux/fpc-iii.git] / drivers / bluetooth / hci_qca.c
blobf10bdf8e1fc5deee1336db469aeae27fee8341f5
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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.
11 * Acknowledgements:
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>
36 #include "hci_uart.h"
37 #include "btqca.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_BTSOC_TX_IDLE_TIMEOUT_MS 40
47 #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000
48 #define CMD_TRANS_TIMEOUT_MS 100
50 /* susclk rate */
51 #define SUSCLK_RATE_32KHZ 32768
53 /* Controller debug log header */
54 #define QCA_DEBUG_HANDLE 0x2EDC
56 enum qca_flags {
57 QCA_IBS_ENABLED,
58 QCA_DROP_VENDOR_EVENT,
59 QCA_SUSPENDING,
62 /* HCI_IBS transmit side sleep protocol states */
63 enum tx_ibs_states {
64 HCI_IBS_TX_ASLEEP,
65 HCI_IBS_TX_WAKING,
66 HCI_IBS_TX_AWAKE,
69 /* HCI_IBS receive side sleep protocol states */
70 enum rx_states {
71 HCI_IBS_RX_ASLEEP,
72 HCI_IBS_RX_AWAKE,
75 /* HCI_IBS transmit and receive side clock state vote */
76 enum hci_ibs_clock_state_vote {
77 HCI_IBS_VOTE_STATS_UPDATE,
78 HCI_IBS_TX_VOTE_CLOCK_ON,
79 HCI_IBS_TX_VOTE_CLOCK_OFF,
80 HCI_IBS_RX_VOTE_CLOCK_ON,
81 HCI_IBS_RX_VOTE_CLOCK_OFF,
84 struct qca_data {
85 struct hci_uart *hu;
86 struct sk_buff *rx_skb;
87 struct sk_buff_head txq;
88 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
89 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
90 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
91 u8 rx_ibs_state; /* HCI_IBS receive side power state */
92 bool tx_vote; /* Clock must be on for TX */
93 bool rx_vote; /* Clock must be on for RX */
94 struct timer_list tx_idle_timer;
95 u32 tx_idle_delay;
96 struct timer_list wake_retrans_timer;
97 u32 wake_retrans;
98 struct workqueue_struct *workqueue;
99 struct work_struct ws_awake_rx;
100 struct work_struct ws_awake_device;
101 struct work_struct ws_rx_vote_off;
102 struct work_struct ws_tx_vote_off;
103 unsigned long flags;
104 struct completion drop_ev_comp;
105 wait_queue_head_t suspend_wait_q;
107 /* For debugging purpose */
108 u64 ibs_sent_wacks;
109 u64 ibs_sent_slps;
110 u64 ibs_sent_wakes;
111 u64 ibs_recv_wacks;
112 u64 ibs_recv_slps;
113 u64 ibs_recv_wakes;
114 u64 vote_last_jif;
115 u32 vote_on_ms;
116 u32 vote_off_ms;
117 u64 tx_votes_on;
118 u64 rx_votes_on;
119 u64 tx_votes_off;
120 u64 rx_votes_off;
121 u64 votes_on;
122 u64 votes_off;
125 enum qca_speed_type {
126 QCA_INIT_SPEED = 1,
127 QCA_OPER_SPEED
131 * Voltage regulator information required for configuring the
132 * QCA Bluetooth chipset
134 struct qca_vreg {
135 const char *name;
136 unsigned int load_uA;
139 struct qca_vreg_data {
140 enum qca_btsoc_type soc_type;
141 struct qca_vreg *vregs;
142 size_t num_vregs;
146 * Platform data for the QCA Bluetooth power driver.
148 struct qca_power {
149 struct device *dev;
150 struct regulator_bulk_data *vreg_bulk;
151 int num_vregs;
152 bool vregs_on;
155 struct qca_serdev {
156 struct hci_uart serdev_hu;
157 struct gpio_desc *bt_en;
158 struct clk *susclk;
159 enum qca_btsoc_type btsoc_type;
160 struct qca_power *bt_power;
161 u32 init_speed;
162 u32 oper_speed;
163 const char *firmware_name;
166 static int qca_regulator_enable(struct qca_serdev *qcadev);
167 static void qca_regulator_disable(struct qca_serdev *qcadev);
168 static void qca_power_shutdown(struct hci_uart *hu);
169 static int qca_power_off(struct hci_dev *hdev);
171 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
173 enum qca_btsoc_type soc_type;
175 if (hu->serdev) {
176 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
178 soc_type = qsd->btsoc_type;
179 } else {
180 soc_type = QCA_ROME;
183 return soc_type;
186 static const char *qca_get_firmware_name(struct hci_uart *hu)
188 if (hu->serdev) {
189 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
191 return qsd->firmware_name;
192 } else {
193 return NULL;
197 static void __serial_clock_on(struct tty_struct *tty)
199 /* TODO: Some chipset requires to enable UART clock on client
200 * side to save power consumption or manual work is required.
201 * Please put your code to control UART clock here if needed
205 static void __serial_clock_off(struct tty_struct *tty)
207 /* TODO: Some chipset requires to disable UART clock on client
208 * side to save power consumption or manual work is required.
209 * Please put your code to control UART clock off here if needed
213 /* serial_clock_vote needs to be called with the ibs lock held */
214 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
216 struct qca_data *qca = hu->priv;
217 unsigned int diff;
219 bool old_vote = (qca->tx_vote | qca->rx_vote);
220 bool new_vote;
222 switch (vote) {
223 case HCI_IBS_VOTE_STATS_UPDATE:
224 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
226 if (old_vote)
227 qca->vote_off_ms += diff;
228 else
229 qca->vote_on_ms += diff;
230 return;
232 case HCI_IBS_TX_VOTE_CLOCK_ON:
233 qca->tx_vote = true;
234 qca->tx_votes_on++;
235 new_vote = true;
236 break;
238 case HCI_IBS_RX_VOTE_CLOCK_ON:
239 qca->rx_vote = true;
240 qca->rx_votes_on++;
241 new_vote = true;
242 break;
244 case HCI_IBS_TX_VOTE_CLOCK_OFF:
245 qca->tx_vote = false;
246 qca->tx_votes_off++;
247 new_vote = qca->rx_vote | qca->tx_vote;
248 break;
250 case HCI_IBS_RX_VOTE_CLOCK_OFF:
251 qca->rx_vote = false;
252 qca->rx_votes_off++;
253 new_vote = qca->rx_vote | qca->tx_vote;
254 break;
256 default:
257 BT_ERR("Voting irregularity");
258 return;
261 if (new_vote != old_vote) {
262 if (new_vote)
263 __serial_clock_on(hu->tty);
264 else
265 __serial_clock_off(hu->tty);
267 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
268 vote ? "true" : "false");
270 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
272 if (new_vote) {
273 qca->votes_on++;
274 qca->vote_off_ms += diff;
275 } else {
276 qca->votes_off++;
277 qca->vote_on_ms += diff;
279 qca->vote_last_jif = jiffies;
283 /* Builds and sends an HCI_IBS command packet.
284 * These are very simple packets with only 1 cmd byte.
286 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
288 int err = 0;
289 struct sk_buff *skb = NULL;
290 struct qca_data *qca = hu->priv;
292 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
294 skb = bt_skb_alloc(1, GFP_ATOMIC);
295 if (!skb) {
296 BT_ERR("Failed to allocate memory for HCI_IBS packet");
297 return -ENOMEM;
300 /* Assign HCI_IBS type */
301 skb_put_u8(skb, cmd);
303 skb_queue_tail(&qca->txq, skb);
305 return err;
308 static void qca_wq_awake_device(struct work_struct *work)
310 struct qca_data *qca = container_of(work, struct qca_data,
311 ws_awake_device);
312 struct hci_uart *hu = qca->hu;
313 unsigned long retrans_delay;
314 unsigned long flags;
316 BT_DBG("hu %p wq awake device", hu);
318 /* Vote for serial clock */
319 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
321 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
323 /* Send wake indication to device */
324 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
325 BT_ERR("Failed to send WAKE to device");
327 qca->ibs_sent_wakes++;
329 /* Start retransmit timer */
330 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
331 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
333 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
335 /* Actually send the packets */
336 hci_uart_tx_wakeup(hu);
339 static void qca_wq_awake_rx(struct work_struct *work)
341 struct qca_data *qca = container_of(work, struct qca_data,
342 ws_awake_rx);
343 struct hci_uart *hu = qca->hu;
344 unsigned long flags;
346 BT_DBG("hu %p wq awake rx", hu);
348 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
350 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
351 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
353 /* Always acknowledge device wake up,
354 * sending IBS message doesn't count as TX ON.
356 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
357 BT_ERR("Failed to acknowledge device wake up");
359 qca->ibs_sent_wacks++;
361 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
363 /* Actually send the packets */
364 hci_uart_tx_wakeup(hu);
367 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
369 struct qca_data *qca = container_of(work, struct qca_data,
370 ws_rx_vote_off);
371 struct hci_uart *hu = qca->hu;
373 BT_DBG("hu %p rx clock vote off", hu);
375 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
378 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
380 struct qca_data *qca = container_of(work, struct qca_data,
381 ws_tx_vote_off);
382 struct hci_uart *hu = qca->hu;
384 BT_DBG("hu %p tx clock vote off", hu);
386 /* Run HCI tx handling unlocked */
387 hci_uart_tx_wakeup(hu);
389 /* Now that message queued to tty driver, vote for tty clocks off.
390 * It is up to the tty driver to pend the clocks off until tx done.
392 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
395 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
397 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
398 struct hci_uart *hu = qca->hu;
399 unsigned long flags;
401 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
403 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
404 flags, SINGLE_DEPTH_NESTING);
406 switch (qca->tx_ibs_state) {
407 case HCI_IBS_TX_AWAKE:
408 /* TX_IDLE, go to SLEEP */
409 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
410 BT_ERR("Failed to send SLEEP to device");
411 break;
413 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
414 qca->ibs_sent_slps++;
415 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
416 break;
418 case HCI_IBS_TX_ASLEEP:
419 case HCI_IBS_TX_WAKING:
420 /* Fall through */
422 default:
423 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
424 break;
427 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
430 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
432 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
433 struct hci_uart *hu = qca->hu;
434 unsigned long flags, retrans_delay;
435 bool retransmit = false;
437 BT_DBG("hu %p wake retransmit timeout in %d state",
438 hu, qca->tx_ibs_state);
440 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
441 flags, SINGLE_DEPTH_NESTING);
443 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
444 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
445 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
446 return;
449 switch (qca->tx_ibs_state) {
450 case HCI_IBS_TX_WAKING:
451 /* No WAKE_ACK, retransmit WAKE */
452 retransmit = true;
453 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
454 BT_ERR("Failed to acknowledge device wake up");
455 break;
457 qca->ibs_sent_wakes++;
458 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
459 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
460 break;
462 case HCI_IBS_TX_ASLEEP:
463 case HCI_IBS_TX_AWAKE:
464 /* Fall through */
466 default:
467 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
468 break;
471 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
473 if (retransmit)
474 hci_uart_tx_wakeup(hu);
477 /* Initialize protocol */
478 static int qca_open(struct hci_uart *hu)
480 struct qca_serdev *qcadev;
481 struct qca_data *qca;
482 int ret;
484 BT_DBG("hu %p qca_open", hu);
486 if (!hci_uart_has_flow_control(hu))
487 return -EOPNOTSUPP;
489 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
490 if (!qca)
491 return -ENOMEM;
493 skb_queue_head_init(&qca->txq);
494 skb_queue_head_init(&qca->tx_wait_q);
495 spin_lock_init(&qca->hci_ibs_lock);
496 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
497 if (!qca->workqueue) {
498 BT_ERR("QCA Workqueue not initialized properly");
499 kfree(qca);
500 return -ENOMEM;
503 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
504 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
505 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
506 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
508 init_waitqueue_head(&qca->suspend_wait_q);
510 qca->hu = hu;
511 init_completion(&qca->drop_ev_comp);
513 /* Assume we start with both sides asleep -- extra wakes OK */
514 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
515 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
517 qca->vote_last_jif = jiffies;
519 hu->priv = qca;
521 if (hu->serdev) {
523 qcadev = serdev_device_get_drvdata(hu->serdev);
524 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
525 gpiod_set_value_cansleep(qcadev->bt_en, 1);
526 /* Controller needs time to bootup. */
527 msleep(150);
528 } else {
529 hu->init_speed = qcadev->init_speed;
530 hu->oper_speed = qcadev->oper_speed;
531 ret = qca_regulator_enable(qcadev);
532 if (ret) {
533 destroy_workqueue(qca->workqueue);
534 kfree_skb(qca->rx_skb);
535 hu->priv = NULL;
536 kfree(qca);
537 return ret;
542 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
543 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
545 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
546 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
548 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
549 qca->tx_idle_delay, qca->wake_retrans);
551 return 0;
554 static void qca_debugfs_init(struct hci_dev *hdev)
556 struct hci_uart *hu = hci_get_drvdata(hdev);
557 struct qca_data *qca = hu->priv;
558 struct dentry *ibs_dir;
559 umode_t mode;
561 if (!hdev->debugfs)
562 return;
564 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
566 /* read only */
567 mode = S_IRUGO;
568 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
569 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
570 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
571 &qca->ibs_sent_slps);
572 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
573 &qca->ibs_sent_wakes);
574 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
575 &qca->ibs_sent_wacks);
576 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
577 &qca->ibs_recv_slps);
578 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
579 &qca->ibs_recv_wakes);
580 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
581 &qca->ibs_recv_wacks);
582 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
583 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
584 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
585 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
586 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
587 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
588 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
589 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
590 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
591 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
593 /* read/write */
594 mode = S_IRUGO | S_IWUSR;
595 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
596 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
597 &qca->tx_idle_delay);
600 /* Flush protocol data */
601 static int qca_flush(struct hci_uart *hu)
603 struct qca_data *qca = hu->priv;
605 BT_DBG("hu %p qca flush", hu);
607 skb_queue_purge(&qca->tx_wait_q);
608 skb_queue_purge(&qca->txq);
610 return 0;
613 /* Close protocol */
614 static int qca_close(struct hci_uart *hu)
616 struct qca_serdev *qcadev;
617 struct qca_data *qca = hu->priv;
619 BT_DBG("hu %p qca close", hu);
621 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
623 skb_queue_purge(&qca->tx_wait_q);
624 skb_queue_purge(&qca->txq);
625 del_timer(&qca->tx_idle_timer);
626 del_timer(&qca->wake_retrans_timer);
627 destroy_workqueue(qca->workqueue);
628 qca->hu = NULL;
630 if (hu->serdev) {
631 qcadev = serdev_device_get_drvdata(hu->serdev);
632 if (qca_is_wcn399x(qcadev->btsoc_type))
633 qca_power_shutdown(hu);
634 else
635 gpiod_set_value_cansleep(qcadev->bt_en, 0);
639 kfree_skb(qca->rx_skb);
641 hu->priv = NULL;
643 kfree(qca);
645 return 0;
648 /* Called upon a wake-up-indication from the device.
650 static void device_want_to_wakeup(struct hci_uart *hu)
652 unsigned long flags;
653 struct qca_data *qca = hu->priv;
655 BT_DBG("hu %p want to wake up", hu);
657 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
659 qca->ibs_recv_wakes++;
661 /* Don't wake the rx up when suspending. */
662 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
663 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
664 return;
667 switch (qca->rx_ibs_state) {
668 case HCI_IBS_RX_ASLEEP:
669 /* Make sure clock is on - we may have turned clock off since
670 * receiving the wake up indicator awake rx clock.
672 queue_work(qca->workqueue, &qca->ws_awake_rx);
673 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
674 return;
676 case HCI_IBS_RX_AWAKE:
677 /* Always acknowledge device wake up,
678 * sending IBS message doesn't count as TX ON.
680 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
681 BT_ERR("Failed to acknowledge device wake up");
682 break;
684 qca->ibs_sent_wacks++;
685 break;
687 default:
688 /* Any other state is illegal */
689 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
690 qca->rx_ibs_state);
691 break;
694 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
696 /* Actually send the packets */
697 hci_uart_tx_wakeup(hu);
700 /* Called upon a sleep-indication from the device.
702 static void device_want_to_sleep(struct hci_uart *hu)
704 unsigned long flags;
705 struct qca_data *qca = hu->priv;
707 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
709 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
711 qca->ibs_recv_slps++;
713 switch (qca->rx_ibs_state) {
714 case HCI_IBS_RX_AWAKE:
715 /* Update state */
716 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
717 /* Vote off rx clock under workqueue */
718 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
719 break;
721 case HCI_IBS_RX_ASLEEP:
722 break;
724 default:
725 /* Any other state is illegal */
726 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
727 qca->rx_ibs_state);
728 break;
731 wake_up_interruptible(&qca->suspend_wait_q);
733 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
736 /* Called upon wake-up-acknowledgement from the device
738 static void device_woke_up(struct hci_uart *hu)
740 unsigned long flags, idle_delay;
741 struct qca_data *qca = hu->priv;
742 struct sk_buff *skb = NULL;
744 BT_DBG("hu %p woke up", hu);
746 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
748 qca->ibs_recv_wacks++;
750 /* Don't react to the wake-up-acknowledgment when suspending. */
751 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
752 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
753 return;
756 switch (qca->tx_ibs_state) {
757 case HCI_IBS_TX_AWAKE:
758 /* Expect one if we send 2 WAKEs */
759 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
760 qca->tx_ibs_state);
761 break;
763 case HCI_IBS_TX_WAKING:
764 /* Send pending packets */
765 while ((skb = skb_dequeue(&qca->tx_wait_q)))
766 skb_queue_tail(&qca->txq, skb);
768 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
769 del_timer(&qca->wake_retrans_timer);
770 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
771 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
772 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
773 break;
775 case HCI_IBS_TX_ASLEEP:
776 /* Fall through */
778 default:
779 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
780 qca->tx_ibs_state);
781 break;
784 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
786 /* Actually send the packets */
787 hci_uart_tx_wakeup(hu);
790 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
791 * two simultaneous tasklets.
793 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
795 unsigned long flags = 0, idle_delay;
796 struct qca_data *qca = hu->priv;
798 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
799 qca->tx_ibs_state);
801 /* Prepend skb with frame type */
802 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
804 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
806 /* Don't go to sleep in middle of patch download or
807 * Out-Of-Band(GPIOs control) sleep is selected.
808 * Don't wake the device up when suspending.
810 if (!test_bit(QCA_IBS_ENABLED, &qca->flags) ||
811 test_bit(QCA_SUSPENDING, &qca->flags)) {
812 skb_queue_tail(&qca->txq, skb);
813 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
814 return 0;
817 /* Act according to current state */
818 switch (qca->tx_ibs_state) {
819 case HCI_IBS_TX_AWAKE:
820 BT_DBG("Device awake, sending normally");
821 skb_queue_tail(&qca->txq, skb);
822 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
823 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
824 break;
826 case HCI_IBS_TX_ASLEEP:
827 BT_DBG("Device asleep, waking up and queueing packet");
828 /* Save packet for later */
829 skb_queue_tail(&qca->tx_wait_q, skb);
831 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
832 /* Schedule a work queue to wake up device */
833 queue_work(qca->workqueue, &qca->ws_awake_device);
834 break;
836 case HCI_IBS_TX_WAKING:
837 BT_DBG("Device waking up, queueing packet");
838 /* Transient state; just keep packet for later */
839 skb_queue_tail(&qca->tx_wait_q, skb);
840 break;
842 default:
843 BT_ERR("Illegal tx state: %d (losing packet)",
844 qca->tx_ibs_state);
845 kfree_skb(skb);
846 break;
849 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
851 return 0;
854 static int qca_ibs_sleep_ind(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_SLEEP_IND);
860 device_want_to_sleep(hu);
862 kfree_skb(skb);
863 return 0;
866 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
868 struct hci_uart *hu = hci_get_drvdata(hdev);
870 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
872 device_want_to_wakeup(hu);
874 kfree_skb(skb);
875 return 0;
878 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
880 struct hci_uart *hu = hci_get_drvdata(hdev);
882 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
884 device_woke_up(hu);
886 kfree_skb(skb);
887 return 0;
890 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
892 /* We receive debug logs from chip as an ACL packets.
893 * Instead of sending the data to ACL to decode the
894 * received data, we are pushing them to the above layers
895 * as a diagnostic packet.
897 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
898 return hci_recv_diag(hdev, skb);
900 return hci_recv_frame(hdev, skb);
903 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
905 struct hci_uart *hu = hci_get_drvdata(hdev);
906 struct qca_data *qca = hu->priv;
908 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
909 struct hci_event_hdr *hdr = (void *)skb->data;
911 /* For the WCN3990 the vendor command for a baudrate change
912 * isn't sent as synchronous HCI command, because the
913 * controller sends the corresponding vendor event with the
914 * new baudrate. The event is received and properly decoded
915 * after changing the baudrate of the host port. It needs to
916 * be dropped, otherwise it can be misinterpreted as
917 * response to a later firmware download command (also a
918 * vendor command).
921 if (hdr->evt == HCI_EV_VENDOR)
922 complete(&qca->drop_ev_comp);
924 kfree_skb(skb);
926 return 0;
929 return hci_recv_frame(hdev, skb);
932 #define QCA_IBS_SLEEP_IND_EVENT \
933 .type = HCI_IBS_SLEEP_IND, \
934 .hlen = 0, \
935 .loff = 0, \
936 .lsize = 0, \
937 .maxlen = HCI_MAX_IBS_SIZE
939 #define QCA_IBS_WAKE_IND_EVENT \
940 .type = HCI_IBS_WAKE_IND, \
941 .hlen = 0, \
942 .loff = 0, \
943 .lsize = 0, \
944 .maxlen = HCI_MAX_IBS_SIZE
946 #define QCA_IBS_WAKE_ACK_EVENT \
947 .type = HCI_IBS_WAKE_ACK, \
948 .hlen = 0, \
949 .loff = 0, \
950 .lsize = 0, \
951 .maxlen = HCI_MAX_IBS_SIZE
953 static const struct h4_recv_pkt qca_recv_pkts[] = {
954 { H4_RECV_ACL, .recv = qca_recv_acl_data },
955 { H4_RECV_SCO, .recv = hci_recv_frame },
956 { H4_RECV_EVENT, .recv = qca_recv_event },
957 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
958 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
959 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
962 static int qca_recv(struct hci_uart *hu, const void *data, int count)
964 struct qca_data *qca = hu->priv;
966 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
967 return -EUNATCH;
969 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
970 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
971 if (IS_ERR(qca->rx_skb)) {
972 int err = PTR_ERR(qca->rx_skb);
973 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
974 qca->rx_skb = NULL;
975 return err;
978 return count;
981 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
983 struct qca_data *qca = hu->priv;
985 return skb_dequeue(&qca->txq);
988 static uint8_t qca_get_baudrate_value(int speed)
990 switch (speed) {
991 case 9600:
992 return QCA_BAUDRATE_9600;
993 case 19200:
994 return QCA_BAUDRATE_19200;
995 case 38400:
996 return QCA_BAUDRATE_38400;
997 case 57600:
998 return QCA_BAUDRATE_57600;
999 case 115200:
1000 return QCA_BAUDRATE_115200;
1001 case 230400:
1002 return QCA_BAUDRATE_230400;
1003 case 460800:
1004 return QCA_BAUDRATE_460800;
1005 case 500000:
1006 return QCA_BAUDRATE_500000;
1007 case 921600:
1008 return QCA_BAUDRATE_921600;
1009 case 1000000:
1010 return QCA_BAUDRATE_1000000;
1011 case 2000000:
1012 return QCA_BAUDRATE_2000000;
1013 case 3000000:
1014 return QCA_BAUDRATE_3000000;
1015 case 3200000:
1016 return QCA_BAUDRATE_3200000;
1017 case 3500000:
1018 return QCA_BAUDRATE_3500000;
1019 default:
1020 return QCA_BAUDRATE_115200;
1024 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1026 struct hci_uart *hu = hci_get_drvdata(hdev);
1027 struct qca_data *qca = hu->priv;
1028 struct sk_buff *skb;
1029 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1031 if (baudrate > QCA_BAUDRATE_3200000)
1032 return -EINVAL;
1034 cmd[4] = baudrate;
1036 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1037 if (!skb) {
1038 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1039 return -ENOMEM;
1042 /* Assign commands to change baudrate and packet type. */
1043 skb_put_data(skb, cmd, sizeof(cmd));
1044 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1046 skb_queue_tail(&qca->txq, skb);
1047 hci_uart_tx_wakeup(hu);
1049 /* Wait for the baudrate change request to be sent */
1051 while (!skb_queue_empty(&qca->txq))
1052 usleep_range(100, 200);
1054 if (hu->serdev)
1055 serdev_device_wait_until_sent(hu->serdev,
1056 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1058 /* Give the controller time to process the request */
1059 if (qca_is_wcn399x(qca_soc_type(hu)))
1060 msleep(10);
1061 else
1062 msleep(300);
1064 return 0;
1067 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1069 if (hu->serdev)
1070 serdev_device_set_baudrate(hu->serdev, speed);
1071 else
1072 hci_uart_set_baudrate(hu, speed);
1075 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1077 int ret;
1078 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1079 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1081 /* These power pulses are single byte command which are sent
1082 * at required baudrate to wcn3990. On wcn3990, we have an external
1083 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1084 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1085 * and also we use the same power inputs to turn on and off for
1086 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1087 * we send a power on pulse at 115200 bps. This algorithm will help to
1088 * save power. Disabling hardware flow control is mandatory while
1089 * sending power pulses to SoC.
1091 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1093 serdev_device_write_flush(hu->serdev);
1094 hci_uart_set_flow_control(hu, true);
1095 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1096 if (ret < 0) {
1097 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1098 return ret;
1101 serdev_device_wait_until_sent(hu->serdev, timeout);
1102 hci_uart_set_flow_control(hu, false);
1104 /* Give to controller time to boot/shutdown */
1105 if (on)
1106 msleep(100);
1107 else
1108 msleep(10);
1110 return 0;
1113 static unsigned int qca_get_speed(struct hci_uart *hu,
1114 enum qca_speed_type speed_type)
1116 unsigned int speed = 0;
1118 if (speed_type == QCA_INIT_SPEED) {
1119 if (hu->init_speed)
1120 speed = hu->init_speed;
1121 else if (hu->proto->init_speed)
1122 speed = hu->proto->init_speed;
1123 } else {
1124 if (hu->oper_speed)
1125 speed = hu->oper_speed;
1126 else if (hu->proto->oper_speed)
1127 speed = hu->proto->oper_speed;
1130 return speed;
1133 static int qca_check_speeds(struct hci_uart *hu)
1135 if (qca_is_wcn399x(qca_soc_type(hu))) {
1136 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1137 !qca_get_speed(hu, QCA_OPER_SPEED))
1138 return -EINVAL;
1139 } else {
1140 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1141 !qca_get_speed(hu, QCA_OPER_SPEED))
1142 return -EINVAL;
1145 return 0;
1148 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1150 unsigned int speed, qca_baudrate;
1151 struct qca_data *qca = hu->priv;
1152 int ret = 0;
1154 if (speed_type == QCA_INIT_SPEED) {
1155 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1156 if (speed)
1157 host_set_baudrate(hu, speed);
1158 } else {
1159 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1161 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1162 if (!speed)
1163 return 0;
1165 /* Disable flow control for wcn3990 to deassert RTS while
1166 * changing the baudrate of chip and host.
1168 if (qca_is_wcn399x(soc_type))
1169 hci_uart_set_flow_control(hu, true);
1171 if (soc_type == QCA_WCN3990) {
1172 reinit_completion(&qca->drop_ev_comp);
1173 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1176 qca_baudrate = qca_get_baudrate_value(speed);
1177 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1178 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1179 if (ret)
1180 goto error;
1182 host_set_baudrate(hu, speed);
1184 error:
1185 if (qca_is_wcn399x(soc_type))
1186 hci_uart_set_flow_control(hu, false);
1188 if (soc_type == QCA_WCN3990) {
1189 /* Wait for the controller to send the vendor event
1190 * for the baudrate change command.
1192 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1193 msecs_to_jiffies(100))) {
1194 bt_dev_err(hu->hdev,
1195 "Failed to change controller baudrate\n");
1196 ret = -ETIMEDOUT;
1199 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1203 return ret;
1206 static int qca_wcn3990_init(struct hci_uart *hu)
1208 struct qca_serdev *qcadev;
1209 int ret;
1211 /* Check for vregs status, may be hci down has turned
1212 * off the voltage regulator.
1214 qcadev = serdev_device_get_drvdata(hu->serdev);
1215 if (!qcadev->bt_power->vregs_on) {
1216 serdev_device_close(hu->serdev);
1217 ret = qca_regulator_enable(qcadev);
1218 if (ret)
1219 return ret;
1221 ret = serdev_device_open(hu->serdev);
1222 if (ret) {
1223 bt_dev_err(hu->hdev, "failed to open port");
1224 return ret;
1228 /* Forcefully enable wcn3990 to enter in to boot mode. */
1229 host_set_baudrate(hu, 2400);
1230 ret = qca_send_power_pulse(hu, false);
1231 if (ret)
1232 return ret;
1234 qca_set_speed(hu, QCA_INIT_SPEED);
1235 ret = qca_send_power_pulse(hu, true);
1236 if (ret)
1237 return ret;
1239 /* Now the device is in ready state to communicate with host.
1240 * To sync host with device we need to reopen port.
1241 * Without this, we will have RTS and CTS synchronization
1242 * issues.
1244 serdev_device_close(hu->serdev);
1245 ret = serdev_device_open(hu->serdev);
1246 if (ret) {
1247 bt_dev_err(hu->hdev, "failed to open port");
1248 return ret;
1251 hci_uart_set_flow_control(hu, false);
1253 return 0;
1256 static int qca_setup(struct hci_uart *hu)
1258 struct hci_dev *hdev = hu->hdev;
1259 struct qca_data *qca = hu->priv;
1260 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1261 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1262 const char *firmware_name = qca_get_firmware_name(hu);
1263 int ret;
1264 int soc_ver = 0;
1266 ret = qca_check_speeds(hu);
1267 if (ret)
1268 return ret;
1270 /* Patch downloading has to be done without IBS mode */
1271 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1273 /* Enable controller to do both LE scan and BR/EDR inquiry
1274 * simultaneously.
1276 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1278 if (qca_is_wcn399x(soc_type)) {
1279 bt_dev_info(hdev, "setting up wcn3990");
1281 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1282 * setup for every hci up.
1284 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1285 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1286 hu->hdev->shutdown = qca_power_off;
1287 ret = qca_wcn3990_init(hu);
1288 if (ret)
1289 return ret;
1291 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
1292 if (ret)
1293 return ret;
1294 } else {
1295 bt_dev_info(hdev, "ROME setup");
1296 qca_set_speed(hu, QCA_INIT_SPEED);
1299 /* Setup user speed if needed */
1300 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1301 if (speed) {
1302 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1303 if (ret)
1304 return ret;
1306 qca_baudrate = qca_get_baudrate_value(speed);
1309 if (!qca_is_wcn399x(soc_type)) {
1310 /* Get QCA version information */
1311 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
1312 if (ret)
1313 return ret;
1316 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1317 /* Setup patch / NVM configurations */
1318 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1319 firmware_name);
1320 if (!ret) {
1321 set_bit(QCA_IBS_ENABLED, &qca->flags);
1322 qca_debugfs_init(hdev);
1323 } else if (ret == -ENOENT) {
1324 /* No patch/nvm-config found, run with original fw/config */
1325 ret = 0;
1326 } else if (ret == -EAGAIN) {
1328 * Userspace firmware loader will return -EAGAIN in case no
1329 * patch/nvm-config is found, so run with original fw/config.
1331 ret = 0;
1334 /* Setup bdaddr */
1335 if (qca_is_wcn399x(soc_type))
1336 hu->hdev->set_bdaddr = qca_set_bdaddr;
1337 else
1338 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1340 return ret;
1343 static const struct hci_uart_proto qca_proto = {
1344 .id = HCI_UART_QCA,
1345 .name = "QCA",
1346 .manufacturer = 29,
1347 .init_speed = 115200,
1348 .oper_speed = 3000000,
1349 .open = qca_open,
1350 .close = qca_close,
1351 .flush = qca_flush,
1352 .setup = qca_setup,
1353 .recv = qca_recv,
1354 .enqueue = qca_enqueue,
1355 .dequeue = qca_dequeue,
1358 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1359 .soc_type = QCA_WCN3990,
1360 .vregs = (struct qca_vreg []) {
1361 { "vddio", 15000 },
1362 { "vddxo", 80000 },
1363 { "vddrf", 300000 },
1364 { "vddch0", 450000 },
1366 .num_vregs = 4,
1369 static const struct qca_vreg_data qca_soc_data_wcn3991 = {
1370 .soc_type = QCA_WCN3991,
1371 .vregs = (struct qca_vreg []) {
1372 { "vddio", 15000 },
1373 { "vddxo", 80000 },
1374 { "vddrf", 300000 },
1375 { "vddch0", 450000 },
1377 .num_vregs = 4,
1380 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1381 .soc_type = QCA_WCN3998,
1382 .vregs = (struct qca_vreg []) {
1383 { "vddio", 10000 },
1384 { "vddxo", 80000 },
1385 { "vddrf", 300000 },
1386 { "vddch0", 450000 },
1388 .num_vregs = 4,
1391 static void qca_power_shutdown(struct hci_uart *hu)
1393 struct qca_serdev *qcadev;
1394 struct qca_data *qca = hu->priv;
1395 unsigned long flags;
1397 qcadev = serdev_device_get_drvdata(hu->serdev);
1399 /* From this point we go into power off state. But serial port is
1400 * still open, stop queueing the IBS data and flush all the buffered
1401 * data in skb's.
1403 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1404 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1405 qca_flush(hu);
1406 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1408 host_set_baudrate(hu, 2400);
1409 qca_send_power_pulse(hu, false);
1410 qca_regulator_disable(qcadev);
1413 static int qca_power_off(struct hci_dev *hdev)
1415 struct hci_uart *hu = hci_get_drvdata(hdev);
1417 /* Perform pre shutdown command */
1418 qca_send_pre_shutdown_cmd(hdev);
1420 usleep_range(8000, 10000);
1422 qca_power_shutdown(hu);
1423 return 0;
1426 static int qca_regulator_enable(struct qca_serdev *qcadev)
1428 struct qca_power *power = qcadev->bt_power;
1429 int ret;
1431 /* Already enabled */
1432 if (power->vregs_on)
1433 return 0;
1435 BT_DBG("enabling %d regulators)", power->num_vregs);
1437 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1438 if (ret)
1439 return ret;
1441 power->vregs_on = true;
1443 return 0;
1446 static void qca_regulator_disable(struct qca_serdev *qcadev)
1448 struct qca_power *power;
1450 if (!qcadev)
1451 return;
1453 power = qcadev->bt_power;
1455 /* Already disabled? */
1456 if (!power->vregs_on)
1457 return;
1459 regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1460 power->vregs_on = false;
1463 static int qca_init_regulators(struct qca_power *qca,
1464 const struct qca_vreg *vregs, size_t num_vregs)
1466 struct regulator_bulk_data *bulk;
1467 int ret;
1468 int i;
1470 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1471 if (!bulk)
1472 return -ENOMEM;
1474 for (i = 0; i < num_vregs; i++)
1475 bulk[i].supply = vregs[i].name;
1477 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1478 if (ret < 0)
1479 return ret;
1481 for (i = 0; i < num_vregs; i++) {
1482 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
1483 if (ret)
1484 return ret;
1487 qca->vreg_bulk = bulk;
1488 qca->num_vregs = num_vregs;
1490 return 0;
1493 static int qca_serdev_probe(struct serdev_device *serdev)
1495 struct qca_serdev *qcadev;
1496 const struct qca_vreg_data *data;
1497 int err;
1499 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1500 if (!qcadev)
1501 return -ENOMEM;
1503 qcadev->serdev_hu.serdev = serdev;
1504 data = of_device_get_match_data(&serdev->dev);
1505 serdev_device_set_drvdata(serdev, qcadev);
1506 device_property_read_string(&serdev->dev, "firmware-name",
1507 &qcadev->firmware_name);
1508 if (data && qca_is_wcn399x(data->soc_type)) {
1509 qcadev->btsoc_type = data->soc_type;
1510 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1511 sizeof(struct qca_power),
1512 GFP_KERNEL);
1513 if (!qcadev->bt_power)
1514 return -ENOMEM;
1516 qcadev->bt_power->dev = &serdev->dev;
1517 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1518 data->num_vregs);
1519 if (err) {
1520 BT_ERR("Failed to init regulators:%d", err);
1521 goto out;
1524 qcadev->bt_power->vregs_on = false;
1526 device_property_read_u32(&serdev->dev, "max-speed",
1527 &qcadev->oper_speed);
1528 if (!qcadev->oper_speed)
1529 BT_DBG("UART will pick default operating speed");
1531 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1532 if (err) {
1533 BT_ERR("wcn3990 serdev registration failed");
1534 goto out;
1536 } else {
1537 qcadev->btsoc_type = QCA_ROME;
1538 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1539 GPIOD_OUT_LOW);
1540 if (IS_ERR(qcadev->bt_en)) {
1541 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1542 return PTR_ERR(qcadev->bt_en);
1545 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1546 if (IS_ERR(qcadev->susclk)) {
1547 dev_err(&serdev->dev, "failed to acquire clk\n");
1548 return PTR_ERR(qcadev->susclk);
1551 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1552 if (err)
1553 return err;
1555 err = clk_prepare_enable(qcadev->susclk);
1556 if (err)
1557 return err;
1559 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1560 if (err)
1561 clk_disable_unprepare(qcadev->susclk);
1564 out: return err;
1568 static void qca_serdev_remove(struct serdev_device *serdev)
1570 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1572 if (qca_is_wcn399x(qcadev->btsoc_type))
1573 qca_power_shutdown(&qcadev->serdev_hu);
1574 else
1575 clk_disable_unprepare(qcadev->susclk);
1577 hci_uart_unregister_device(&qcadev->serdev_hu);
1580 static int __maybe_unused qca_suspend(struct device *dev)
1582 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1583 struct hci_uart *hu = hci_get_drvdata(hdev);
1584 struct qca_data *qca = hu->priv;
1585 unsigned long flags;
1586 int ret = 0;
1587 u8 cmd;
1589 set_bit(QCA_SUSPENDING, &qca->flags);
1591 /* Device is downloading patch or doesn't support in-band sleep. */
1592 if (!test_bit(QCA_IBS_ENABLED, &qca->flags))
1593 return 0;
1595 cancel_work_sync(&qca->ws_awake_device);
1596 cancel_work_sync(&qca->ws_awake_rx);
1598 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
1599 flags, SINGLE_DEPTH_NESTING);
1601 switch (qca->tx_ibs_state) {
1602 case HCI_IBS_TX_WAKING:
1603 del_timer(&qca->wake_retrans_timer);
1604 /* Fall through */
1605 case HCI_IBS_TX_AWAKE:
1606 del_timer(&qca->tx_idle_timer);
1608 serdev_device_write_flush(hu->serdev);
1609 cmd = HCI_IBS_SLEEP_IND;
1610 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1612 if (ret < 0) {
1613 BT_ERR("Failed to send SLEEP to device");
1614 break;
1617 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
1618 qca->ibs_sent_slps++;
1620 qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off);
1621 break;
1623 case HCI_IBS_TX_ASLEEP:
1624 break;
1626 default:
1627 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
1628 ret = -EINVAL;
1629 break;
1632 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1634 if (ret < 0)
1635 goto error;
1637 serdev_device_wait_until_sent(hu->serdev,
1638 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1640 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
1641 * to sleep, so that the packet does not wake the system later.
1644 ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
1645 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
1646 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
1648 if (ret > 0)
1649 return 0;
1651 if (ret == 0)
1652 ret = -ETIMEDOUT;
1654 error:
1655 clear_bit(QCA_SUSPENDING, &qca->flags);
1657 return ret;
1660 static int __maybe_unused qca_resume(struct device *dev)
1662 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1663 struct hci_uart *hu = hci_get_drvdata(hdev);
1664 struct qca_data *qca = hu->priv;
1666 clear_bit(QCA_SUSPENDING, &qca->flags);
1668 return 0;
1671 static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
1673 static const struct of_device_id qca_bluetooth_of_match[] = {
1674 { .compatible = "qcom,qca6174-bt" },
1675 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1676 { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
1677 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1678 { /* sentinel */ }
1680 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1682 static struct serdev_device_driver qca_serdev_driver = {
1683 .probe = qca_serdev_probe,
1684 .remove = qca_serdev_remove,
1685 .driver = {
1686 .name = "hci_uart_qca",
1687 .of_match_table = qca_bluetooth_of_match,
1688 .pm = &qca_pm_ops,
1692 int __init qca_init(void)
1694 serdev_device_driver_register(&qca_serdev_driver);
1696 return hci_uart_register_proto(&qca_proto);
1699 int __exit qca_deinit(void)
1701 serdev_device_driver_unregister(&qca_serdev_driver);
1703 return hci_uart_unregister_proto(&qca_proto);