staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / drivers / bluetooth / hci_qca.c
blob9d273cdde5639aa39de4d8589814ed4c60a66b18
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/debugfs.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/mod_devicetable.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/serdev.h>
30 #include <asm/unaligned.h>
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
35 #include "hci_uart.h"
36 #include "btqca.h"
38 /* HCI_IBS protocol messages */
39 #define HCI_IBS_SLEEP_IND 0xFE
40 #define HCI_IBS_WAKE_IND 0xFD
41 #define HCI_IBS_WAKE_ACK 0xFC
42 #define HCI_MAX_IBS_SIZE 10
44 #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
45 #define IBS_TX_IDLE_TIMEOUT_MS 2000
46 #define CMD_TRANS_TIMEOUT_MS 100
48 /* susclk rate */
49 #define SUSCLK_RATE_32KHZ 32768
51 /* Controller debug log header */
52 #define QCA_DEBUG_HANDLE 0x2EDC
54 enum qca_flags {
55 QCA_IBS_ENABLED,
58 /* HCI_IBS transmit side sleep protocol states */
59 enum tx_ibs_states {
60 HCI_IBS_TX_ASLEEP,
61 HCI_IBS_TX_WAKING,
62 HCI_IBS_TX_AWAKE,
65 /* HCI_IBS receive side sleep protocol states */
66 enum rx_states {
67 HCI_IBS_RX_ASLEEP,
68 HCI_IBS_RX_AWAKE,
71 /* HCI_IBS transmit and receive side clock state vote */
72 enum hci_ibs_clock_state_vote {
73 HCI_IBS_VOTE_STATS_UPDATE,
74 HCI_IBS_TX_VOTE_CLOCK_ON,
75 HCI_IBS_TX_VOTE_CLOCK_OFF,
76 HCI_IBS_RX_VOTE_CLOCK_ON,
77 HCI_IBS_RX_VOTE_CLOCK_OFF,
80 struct qca_data {
81 struct hci_uart *hu;
82 struct sk_buff *rx_skb;
83 struct sk_buff_head txq;
84 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
85 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
86 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
87 u8 rx_ibs_state; /* HCI_IBS receive side power state */
88 bool tx_vote; /* Clock must be on for TX */
89 bool rx_vote; /* Clock must be on for RX */
90 struct timer_list tx_idle_timer;
91 u32 tx_idle_delay;
92 struct timer_list wake_retrans_timer;
93 u32 wake_retrans;
94 struct workqueue_struct *workqueue;
95 struct work_struct ws_awake_rx;
96 struct work_struct ws_awake_device;
97 struct work_struct ws_rx_vote_off;
98 struct work_struct ws_tx_vote_off;
99 unsigned long flags;
101 /* For debugging purpose */
102 u64 ibs_sent_wacks;
103 u64 ibs_sent_slps;
104 u64 ibs_sent_wakes;
105 u64 ibs_recv_wacks;
106 u64 ibs_recv_slps;
107 u64 ibs_recv_wakes;
108 u64 vote_last_jif;
109 u32 vote_on_ms;
110 u32 vote_off_ms;
111 u64 tx_votes_on;
112 u64 rx_votes_on;
113 u64 tx_votes_off;
114 u64 rx_votes_off;
115 u64 votes_on;
116 u64 votes_off;
119 enum qca_speed_type {
120 QCA_INIT_SPEED = 1,
121 QCA_OPER_SPEED
125 * Voltage regulator information required for configuring the
126 * QCA Bluetooth chipset
128 struct qca_vreg {
129 const char *name;
130 unsigned int min_uV;
131 unsigned int max_uV;
132 unsigned int load_uA;
135 struct qca_vreg_data {
136 enum qca_btsoc_type soc_type;
137 struct qca_vreg *vregs;
138 size_t num_vregs;
142 * Platform data for the QCA Bluetooth power driver.
144 struct qca_power {
145 struct device *dev;
146 const struct qca_vreg_data *vreg_data;
147 struct regulator_bulk_data *vreg_bulk;
148 bool vregs_on;
151 struct qca_serdev {
152 struct hci_uart serdev_hu;
153 struct gpio_desc *bt_en;
154 struct clk *susclk;
155 enum qca_btsoc_type btsoc_type;
156 struct qca_power *bt_power;
157 u32 init_speed;
158 u32 oper_speed;
161 static int qca_power_setup(struct hci_uart *hu, bool on);
162 static void qca_power_shutdown(struct hci_uart *hu);
163 static int qca_power_off(struct hci_dev *hdev);
165 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
167 enum qca_btsoc_type soc_type;
169 if (hu->serdev) {
170 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
172 soc_type = qsd->btsoc_type;
173 } else {
174 soc_type = QCA_ROME;
177 return soc_type;
180 static void __serial_clock_on(struct tty_struct *tty)
182 /* TODO: Some chipset requires to enable UART clock on client
183 * side to save power consumption or manual work is required.
184 * Please put your code to control UART clock here if needed
188 static void __serial_clock_off(struct tty_struct *tty)
190 /* TODO: Some chipset requires to disable UART clock on client
191 * side to save power consumption or manual work is required.
192 * Please put your code to control UART clock off here if needed
196 /* serial_clock_vote needs to be called with the ibs lock held */
197 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
199 struct qca_data *qca = hu->priv;
200 unsigned int diff;
202 bool old_vote = (qca->tx_vote | qca->rx_vote);
203 bool new_vote;
205 switch (vote) {
206 case HCI_IBS_VOTE_STATS_UPDATE:
207 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
209 if (old_vote)
210 qca->vote_off_ms += diff;
211 else
212 qca->vote_on_ms += diff;
213 return;
215 case HCI_IBS_TX_VOTE_CLOCK_ON:
216 qca->tx_vote = true;
217 qca->tx_votes_on++;
218 new_vote = true;
219 break;
221 case HCI_IBS_RX_VOTE_CLOCK_ON:
222 qca->rx_vote = true;
223 qca->rx_votes_on++;
224 new_vote = true;
225 break;
227 case HCI_IBS_TX_VOTE_CLOCK_OFF:
228 qca->tx_vote = false;
229 qca->tx_votes_off++;
230 new_vote = qca->rx_vote | qca->tx_vote;
231 break;
233 case HCI_IBS_RX_VOTE_CLOCK_OFF:
234 qca->rx_vote = false;
235 qca->rx_votes_off++;
236 new_vote = qca->rx_vote | qca->tx_vote;
237 break;
239 default:
240 BT_ERR("Voting irregularity");
241 return;
244 if (new_vote != old_vote) {
245 if (new_vote)
246 __serial_clock_on(hu->tty);
247 else
248 __serial_clock_off(hu->tty);
250 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
251 vote ? "true" : "false");
253 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
255 if (new_vote) {
256 qca->votes_on++;
257 qca->vote_off_ms += diff;
258 } else {
259 qca->votes_off++;
260 qca->vote_on_ms += diff;
262 qca->vote_last_jif = jiffies;
266 /* Builds and sends an HCI_IBS command packet.
267 * These are very simple packets with only 1 cmd byte.
269 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
271 int err = 0;
272 struct sk_buff *skb = NULL;
273 struct qca_data *qca = hu->priv;
275 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
277 skb = bt_skb_alloc(1, GFP_ATOMIC);
278 if (!skb) {
279 BT_ERR("Failed to allocate memory for HCI_IBS packet");
280 return -ENOMEM;
283 /* Assign HCI_IBS type */
284 skb_put_u8(skb, cmd);
286 skb_queue_tail(&qca->txq, skb);
288 return err;
291 static void qca_wq_awake_device(struct work_struct *work)
293 struct qca_data *qca = container_of(work, struct qca_data,
294 ws_awake_device);
295 struct hci_uart *hu = qca->hu;
296 unsigned long retrans_delay;
298 BT_DBG("hu %p wq awake device", hu);
300 /* Vote for serial clock */
301 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
303 spin_lock(&qca->hci_ibs_lock);
305 /* Send wake indication to device */
306 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
307 BT_ERR("Failed to send WAKE to device");
309 qca->ibs_sent_wakes++;
311 /* Start retransmit timer */
312 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
313 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
315 spin_unlock(&qca->hci_ibs_lock);
317 /* Actually send the packets */
318 hci_uart_tx_wakeup(hu);
321 static void qca_wq_awake_rx(struct work_struct *work)
323 struct qca_data *qca = container_of(work, struct qca_data,
324 ws_awake_rx);
325 struct hci_uart *hu = qca->hu;
327 BT_DBG("hu %p wq awake rx", hu);
329 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
331 spin_lock(&qca->hci_ibs_lock);
332 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
334 /* Always acknowledge device wake up,
335 * sending IBS message doesn't count as TX ON.
337 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
338 BT_ERR("Failed to acknowledge device wake up");
340 qca->ibs_sent_wacks++;
342 spin_unlock(&qca->hci_ibs_lock);
344 /* Actually send the packets */
345 hci_uart_tx_wakeup(hu);
348 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
350 struct qca_data *qca = container_of(work, struct qca_data,
351 ws_rx_vote_off);
352 struct hci_uart *hu = qca->hu;
354 BT_DBG("hu %p rx clock vote off", hu);
356 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
359 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
361 struct qca_data *qca = container_of(work, struct qca_data,
362 ws_tx_vote_off);
363 struct hci_uart *hu = qca->hu;
365 BT_DBG("hu %p tx clock vote off", hu);
367 /* Run HCI tx handling unlocked */
368 hci_uart_tx_wakeup(hu);
370 /* Now that message queued to tty driver, vote for tty clocks off.
371 * It is up to the tty driver to pend the clocks off until tx done.
373 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
376 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
378 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
379 struct hci_uart *hu = qca->hu;
380 unsigned long flags;
382 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
384 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
385 flags, SINGLE_DEPTH_NESTING);
387 switch (qca->tx_ibs_state) {
388 case HCI_IBS_TX_AWAKE:
389 /* TX_IDLE, go to SLEEP */
390 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
391 BT_ERR("Failed to send SLEEP to device");
392 break;
394 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
395 qca->ibs_sent_slps++;
396 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
397 break;
399 case HCI_IBS_TX_ASLEEP:
400 case HCI_IBS_TX_WAKING:
401 /* Fall through */
403 default:
404 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
405 break;
408 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
411 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
413 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
414 struct hci_uart *hu = qca->hu;
415 unsigned long flags, retrans_delay;
416 bool retransmit = false;
418 BT_DBG("hu %p wake retransmit timeout in %d state",
419 hu, qca->tx_ibs_state);
421 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
422 flags, SINGLE_DEPTH_NESTING);
424 switch (qca->tx_ibs_state) {
425 case HCI_IBS_TX_WAKING:
426 /* No WAKE_ACK, retransmit WAKE */
427 retransmit = true;
428 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
429 BT_ERR("Failed to acknowledge device wake up");
430 break;
432 qca->ibs_sent_wakes++;
433 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
434 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
435 break;
437 case HCI_IBS_TX_ASLEEP:
438 case HCI_IBS_TX_AWAKE:
439 /* Fall through */
441 default:
442 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
443 break;
446 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
448 if (retransmit)
449 hci_uart_tx_wakeup(hu);
452 /* Initialize protocol */
453 static int qca_open(struct hci_uart *hu)
455 struct qca_serdev *qcadev;
456 struct qca_data *qca;
457 int ret;
459 BT_DBG("hu %p qca_open", hu);
461 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
462 if (!qca)
463 return -ENOMEM;
465 skb_queue_head_init(&qca->txq);
466 skb_queue_head_init(&qca->tx_wait_q);
467 spin_lock_init(&qca->hci_ibs_lock);
468 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
469 if (!qca->workqueue) {
470 BT_ERR("QCA Workqueue not initialized properly");
471 kfree(qca);
472 return -ENOMEM;
475 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
476 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
477 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
478 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
480 qca->hu = hu;
482 /* Assume we start with both sides asleep -- extra wakes OK */
483 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
484 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
486 /* clocks actually on, but we start votes off */
487 qca->tx_vote = false;
488 qca->rx_vote = false;
489 qca->flags = 0;
491 qca->ibs_sent_wacks = 0;
492 qca->ibs_sent_slps = 0;
493 qca->ibs_sent_wakes = 0;
494 qca->ibs_recv_wacks = 0;
495 qca->ibs_recv_slps = 0;
496 qca->ibs_recv_wakes = 0;
497 qca->vote_last_jif = jiffies;
498 qca->vote_on_ms = 0;
499 qca->vote_off_ms = 0;
500 qca->votes_on = 0;
501 qca->votes_off = 0;
502 qca->tx_votes_on = 0;
503 qca->tx_votes_off = 0;
504 qca->rx_votes_on = 0;
505 qca->rx_votes_off = 0;
507 hu->priv = qca;
509 if (hu->serdev) {
511 qcadev = serdev_device_get_drvdata(hu->serdev);
512 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
513 gpiod_set_value_cansleep(qcadev->bt_en, 1);
514 /* Controller needs time to bootup. */
515 msleep(150);
516 } else {
517 hu->init_speed = qcadev->init_speed;
518 hu->oper_speed = qcadev->oper_speed;
519 ret = qca_power_setup(hu, true);
520 if (ret) {
521 destroy_workqueue(qca->workqueue);
522 kfree_skb(qca->rx_skb);
523 hu->priv = NULL;
524 kfree(qca);
525 return ret;
530 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
531 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
533 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
534 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
536 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
537 qca->tx_idle_delay, qca->wake_retrans);
539 return 0;
542 static void qca_debugfs_init(struct hci_dev *hdev)
544 struct hci_uart *hu = hci_get_drvdata(hdev);
545 struct qca_data *qca = hu->priv;
546 struct dentry *ibs_dir;
547 umode_t mode;
549 if (!hdev->debugfs)
550 return;
552 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
554 /* read only */
555 mode = S_IRUGO;
556 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
557 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
558 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
559 &qca->ibs_sent_slps);
560 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
561 &qca->ibs_sent_wakes);
562 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
563 &qca->ibs_sent_wacks);
564 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
565 &qca->ibs_recv_slps);
566 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
567 &qca->ibs_recv_wakes);
568 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
569 &qca->ibs_recv_wacks);
570 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
571 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
572 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
573 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
574 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
575 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
576 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
577 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
578 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
579 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
581 /* read/write */
582 mode = S_IRUGO | S_IWUSR;
583 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
584 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
585 &qca->tx_idle_delay);
588 /* Flush protocol data */
589 static int qca_flush(struct hci_uart *hu)
591 struct qca_data *qca = hu->priv;
593 BT_DBG("hu %p qca flush", hu);
595 skb_queue_purge(&qca->tx_wait_q);
596 skb_queue_purge(&qca->txq);
598 return 0;
601 /* Close protocol */
602 static int qca_close(struct hci_uart *hu)
604 struct qca_serdev *qcadev;
605 struct qca_data *qca = hu->priv;
607 BT_DBG("hu %p qca close", hu);
609 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
611 skb_queue_purge(&qca->tx_wait_q);
612 skb_queue_purge(&qca->txq);
613 del_timer(&qca->tx_idle_timer);
614 del_timer(&qca->wake_retrans_timer);
615 destroy_workqueue(qca->workqueue);
616 qca->hu = NULL;
618 if (hu->serdev) {
619 qcadev = serdev_device_get_drvdata(hu->serdev);
620 if (qca_is_wcn399x(qcadev->btsoc_type))
621 qca_power_shutdown(hu);
622 else
623 gpiod_set_value_cansleep(qcadev->bt_en, 0);
627 kfree_skb(qca->rx_skb);
629 hu->priv = NULL;
631 kfree(qca);
633 return 0;
636 /* Called upon a wake-up-indication from the device.
638 static void device_want_to_wakeup(struct hci_uart *hu)
640 unsigned long flags;
641 struct qca_data *qca = hu->priv;
643 BT_DBG("hu %p want to wake up", hu);
645 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
647 qca->ibs_recv_wakes++;
649 switch (qca->rx_ibs_state) {
650 case HCI_IBS_RX_ASLEEP:
651 /* Make sure clock is on - we may have turned clock off since
652 * receiving the wake up indicator awake rx clock.
654 queue_work(qca->workqueue, &qca->ws_awake_rx);
655 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
656 return;
658 case HCI_IBS_RX_AWAKE:
659 /* Always acknowledge device wake up,
660 * sending IBS message doesn't count as TX ON.
662 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
663 BT_ERR("Failed to acknowledge device wake up");
664 break;
666 qca->ibs_sent_wacks++;
667 break;
669 default:
670 /* Any other state is illegal */
671 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
672 qca->rx_ibs_state);
673 break;
676 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
678 /* Actually send the packets */
679 hci_uart_tx_wakeup(hu);
682 /* Called upon a sleep-indication from the device.
684 static void device_want_to_sleep(struct hci_uart *hu)
686 unsigned long flags;
687 struct qca_data *qca = hu->priv;
689 BT_DBG("hu %p want to sleep", hu);
691 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
693 qca->ibs_recv_slps++;
695 switch (qca->rx_ibs_state) {
696 case HCI_IBS_RX_AWAKE:
697 /* Update state */
698 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
699 /* Vote off rx clock under workqueue */
700 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
701 break;
703 case HCI_IBS_RX_ASLEEP:
704 /* Fall through */
706 default:
707 /* Any other state is illegal */
708 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
709 qca->rx_ibs_state);
710 break;
713 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
716 /* Called upon wake-up-acknowledgement from the device
718 static void device_woke_up(struct hci_uart *hu)
720 unsigned long flags, idle_delay;
721 struct qca_data *qca = hu->priv;
722 struct sk_buff *skb = NULL;
724 BT_DBG("hu %p woke up", hu);
726 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
728 qca->ibs_recv_wacks++;
730 switch (qca->tx_ibs_state) {
731 case HCI_IBS_TX_AWAKE:
732 /* Expect one if we send 2 WAKEs */
733 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
734 qca->tx_ibs_state);
735 break;
737 case HCI_IBS_TX_WAKING:
738 /* Send pending packets */
739 while ((skb = skb_dequeue(&qca->tx_wait_q)))
740 skb_queue_tail(&qca->txq, skb);
742 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
743 del_timer(&qca->wake_retrans_timer);
744 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
745 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
746 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
747 break;
749 case HCI_IBS_TX_ASLEEP:
750 /* Fall through */
752 default:
753 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
754 qca->tx_ibs_state);
755 break;
758 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
760 /* Actually send the packets */
761 hci_uart_tx_wakeup(hu);
764 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
765 * two simultaneous tasklets.
767 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
769 unsigned long flags = 0, idle_delay;
770 struct qca_data *qca = hu->priv;
772 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
773 qca->tx_ibs_state);
775 /* Prepend skb with frame type */
776 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
778 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
780 /* Don't go to sleep in middle of patch download or
781 * Out-Of-Band(GPIOs control) sleep is selected.
783 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) {
784 skb_queue_tail(&qca->txq, skb);
785 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
786 return 0;
789 /* Act according to current state */
790 switch (qca->tx_ibs_state) {
791 case HCI_IBS_TX_AWAKE:
792 BT_DBG("Device awake, sending normally");
793 skb_queue_tail(&qca->txq, skb);
794 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
795 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
796 break;
798 case HCI_IBS_TX_ASLEEP:
799 BT_DBG("Device asleep, waking up and queueing packet");
800 /* Save packet for later */
801 skb_queue_tail(&qca->tx_wait_q, skb);
803 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
804 /* Schedule a work queue to wake up device */
805 queue_work(qca->workqueue, &qca->ws_awake_device);
806 break;
808 case HCI_IBS_TX_WAKING:
809 BT_DBG("Device waking up, queueing packet");
810 /* Transient state; just keep packet for later */
811 skb_queue_tail(&qca->tx_wait_q, skb);
812 break;
814 default:
815 BT_ERR("Illegal tx state: %d (losing packet)",
816 qca->tx_ibs_state);
817 kfree_skb(skb);
818 break;
821 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
823 return 0;
826 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
828 struct hci_uart *hu = hci_get_drvdata(hdev);
830 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
832 device_want_to_sleep(hu);
834 kfree_skb(skb);
835 return 0;
838 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
840 struct hci_uart *hu = hci_get_drvdata(hdev);
842 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
844 device_want_to_wakeup(hu);
846 kfree_skb(skb);
847 return 0;
850 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
852 struct hci_uart *hu = hci_get_drvdata(hdev);
854 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
856 device_woke_up(hu);
858 kfree_skb(skb);
859 return 0;
862 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
864 /* We receive debug logs from chip as an ACL packets.
865 * Instead of sending the data to ACL to decode the
866 * received data, we are pushing them to the above layers
867 * as a diagnostic packet.
869 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
870 return hci_recv_diag(hdev, skb);
872 return hci_recv_frame(hdev, skb);
875 #define QCA_IBS_SLEEP_IND_EVENT \
876 .type = HCI_IBS_SLEEP_IND, \
877 .hlen = 0, \
878 .loff = 0, \
879 .lsize = 0, \
880 .maxlen = HCI_MAX_IBS_SIZE
882 #define QCA_IBS_WAKE_IND_EVENT \
883 .type = HCI_IBS_WAKE_IND, \
884 .hlen = 0, \
885 .loff = 0, \
886 .lsize = 0, \
887 .maxlen = HCI_MAX_IBS_SIZE
889 #define QCA_IBS_WAKE_ACK_EVENT \
890 .type = HCI_IBS_WAKE_ACK, \
891 .hlen = 0, \
892 .loff = 0, \
893 .lsize = 0, \
894 .maxlen = HCI_MAX_IBS_SIZE
896 static const struct h4_recv_pkt qca_recv_pkts[] = {
897 { H4_RECV_ACL, .recv = qca_recv_acl_data },
898 { H4_RECV_SCO, .recv = hci_recv_frame },
899 { H4_RECV_EVENT, .recv = hci_recv_frame },
900 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
901 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
902 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
905 static int qca_recv(struct hci_uart *hu, const void *data, int count)
907 struct qca_data *qca = hu->priv;
909 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
910 return -EUNATCH;
912 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
913 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
914 if (IS_ERR(qca->rx_skb)) {
915 int err = PTR_ERR(qca->rx_skb);
916 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
917 qca->rx_skb = NULL;
918 return err;
921 return count;
924 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
926 struct qca_data *qca = hu->priv;
928 return skb_dequeue(&qca->txq);
931 static uint8_t qca_get_baudrate_value(int speed)
933 switch (speed) {
934 case 9600:
935 return QCA_BAUDRATE_9600;
936 case 19200:
937 return QCA_BAUDRATE_19200;
938 case 38400:
939 return QCA_BAUDRATE_38400;
940 case 57600:
941 return QCA_BAUDRATE_57600;
942 case 115200:
943 return QCA_BAUDRATE_115200;
944 case 230400:
945 return QCA_BAUDRATE_230400;
946 case 460800:
947 return QCA_BAUDRATE_460800;
948 case 500000:
949 return QCA_BAUDRATE_500000;
950 case 921600:
951 return QCA_BAUDRATE_921600;
952 case 1000000:
953 return QCA_BAUDRATE_1000000;
954 case 2000000:
955 return QCA_BAUDRATE_2000000;
956 case 3000000:
957 return QCA_BAUDRATE_3000000;
958 case 3200000:
959 return QCA_BAUDRATE_3200000;
960 case 3500000:
961 return QCA_BAUDRATE_3500000;
962 default:
963 return QCA_BAUDRATE_115200;
967 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
969 struct hci_uart *hu = hci_get_drvdata(hdev);
970 struct qca_data *qca = hu->priv;
971 struct sk_buff *skb;
972 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
974 if (baudrate > QCA_BAUDRATE_3200000)
975 return -EINVAL;
977 cmd[4] = baudrate;
979 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
980 if (!skb) {
981 bt_dev_err(hdev, "Failed to allocate baudrate packet");
982 return -ENOMEM;
985 /* Assign commands to change baudrate and packet type. */
986 skb_put_data(skb, cmd, sizeof(cmd));
987 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
989 skb_queue_tail(&qca->txq, skb);
990 hci_uart_tx_wakeup(hu);
992 /* Wait for the baudrate change request to be sent */
994 while (!skb_queue_empty(&qca->txq))
995 usleep_range(100, 200);
997 if (hu->serdev)
998 serdev_device_wait_until_sent(hu->serdev,
999 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1001 /* Give the controller time to process the request */
1002 if (qca_is_wcn399x(qca_soc_type(hu)))
1003 msleep(10);
1004 else
1005 msleep(300);
1007 return 0;
1010 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1012 if (hu->serdev)
1013 serdev_device_set_baudrate(hu->serdev, speed);
1014 else
1015 hci_uart_set_baudrate(hu, speed);
1018 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1020 int ret;
1021 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1022 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1024 /* These power pulses are single byte command which are sent
1025 * at required baudrate to wcn3990. On wcn3990, we have an external
1026 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1027 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1028 * and also we use the same power inputs to turn on and off for
1029 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1030 * we send a power on pulse at 115200 bps. This algorithm will help to
1031 * save power. Disabling hardware flow control is mandatory while
1032 * sending power pulses to SoC.
1034 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1036 serdev_device_write_flush(hu->serdev);
1037 hci_uart_set_flow_control(hu, true);
1038 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1039 if (ret < 0) {
1040 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1041 return ret;
1044 serdev_device_wait_until_sent(hu->serdev, timeout);
1045 hci_uart_set_flow_control(hu, false);
1047 /* Give to controller time to boot/shutdown */
1048 if (on)
1049 msleep(100);
1050 else
1051 msleep(10);
1053 return 0;
1056 static unsigned int qca_get_speed(struct hci_uart *hu,
1057 enum qca_speed_type speed_type)
1059 unsigned int speed = 0;
1061 if (speed_type == QCA_INIT_SPEED) {
1062 if (hu->init_speed)
1063 speed = hu->init_speed;
1064 else if (hu->proto->init_speed)
1065 speed = hu->proto->init_speed;
1066 } else {
1067 if (hu->oper_speed)
1068 speed = hu->oper_speed;
1069 else if (hu->proto->oper_speed)
1070 speed = hu->proto->oper_speed;
1073 return speed;
1076 static int qca_check_speeds(struct hci_uart *hu)
1078 if (qca_is_wcn399x(qca_soc_type(hu))) {
1079 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1080 !qca_get_speed(hu, QCA_OPER_SPEED))
1081 return -EINVAL;
1082 } else {
1083 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1084 !qca_get_speed(hu, QCA_OPER_SPEED))
1085 return -EINVAL;
1088 return 0;
1091 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1093 unsigned int speed, qca_baudrate;
1094 int ret = 0;
1096 if (speed_type == QCA_INIT_SPEED) {
1097 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1098 if (speed)
1099 host_set_baudrate(hu, speed);
1100 } else {
1101 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1103 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1104 if (!speed)
1105 return 0;
1107 /* Disable flow control for wcn3990 to deassert RTS while
1108 * changing the baudrate of chip and host.
1110 if (qca_is_wcn399x(soc_type))
1111 hci_uart_set_flow_control(hu, true);
1113 qca_baudrate = qca_get_baudrate_value(speed);
1114 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1115 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1116 if (ret)
1117 goto error;
1119 host_set_baudrate(hu, speed);
1121 error:
1122 if (qca_is_wcn399x(soc_type))
1123 hci_uart_set_flow_control(hu, false);
1126 return ret;
1129 static int qca_wcn3990_init(struct hci_uart *hu)
1131 struct qca_serdev *qcadev;
1132 int ret;
1134 /* Check for vregs status, may be hci down has turned
1135 * off the voltage regulator.
1137 qcadev = serdev_device_get_drvdata(hu->serdev);
1138 if (!qcadev->bt_power->vregs_on) {
1139 serdev_device_close(hu->serdev);
1140 ret = qca_power_setup(hu, true);
1141 if (ret)
1142 return ret;
1144 ret = serdev_device_open(hu->serdev);
1145 if (ret) {
1146 bt_dev_err(hu->hdev, "failed to open port");
1147 return ret;
1151 /* Forcefully enable wcn3990 to enter in to boot mode. */
1152 host_set_baudrate(hu, 2400);
1153 ret = qca_send_power_pulse(hu, false);
1154 if (ret)
1155 return ret;
1157 qca_set_speed(hu, QCA_INIT_SPEED);
1158 ret = qca_send_power_pulse(hu, true);
1159 if (ret)
1160 return ret;
1162 /* Now the device is in ready state to communicate with host.
1163 * To sync host with device we need to reopen port.
1164 * Without this, we will have RTS and CTS synchronization
1165 * issues.
1167 serdev_device_close(hu->serdev);
1168 ret = serdev_device_open(hu->serdev);
1169 if (ret) {
1170 bt_dev_err(hu->hdev, "failed to open port");
1171 return ret;
1174 hci_uart_set_flow_control(hu, false);
1176 return 0;
1179 static int qca_setup(struct hci_uart *hu)
1181 struct hci_dev *hdev = hu->hdev;
1182 struct qca_data *qca = hu->priv;
1183 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1184 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1185 int ret;
1186 int soc_ver = 0;
1188 ret = qca_check_speeds(hu);
1189 if (ret)
1190 return ret;
1192 /* Patch downloading has to be done without IBS mode */
1193 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1195 if (qca_is_wcn399x(soc_type)) {
1196 bt_dev_info(hdev, "setting up wcn3990");
1198 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1199 * setup for every hci up.
1201 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1202 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1203 hu->hdev->shutdown = qca_power_off;
1204 ret = qca_wcn3990_init(hu);
1205 if (ret)
1206 return ret;
1208 ret = qca_read_soc_version(hdev, &soc_ver);
1209 if (ret)
1210 return ret;
1211 } else {
1212 bt_dev_info(hdev, "ROME setup");
1213 qca_set_speed(hu, QCA_INIT_SPEED);
1216 /* Setup user speed if needed */
1217 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1218 if (speed) {
1219 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1220 if (ret)
1221 return ret;
1223 qca_baudrate = qca_get_baudrate_value(speed);
1226 if (!qca_is_wcn399x(soc_type)) {
1227 /* Get QCA version information */
1228 ret = qca_read_soc_version(hdev, &soc_ver);
1229 if (ret)
1230 return ret;
1233 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1234 /* Setup patch / NVM configurations */
1235 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver);
1236 if (!ret) {
1237 set_bit(QCA_IBS_ENABLED, &qca->flags);
1238 qca_debugfs_init(hdev);
1239 } else if (ret == -ENOENT) {
1240 /* No patch/nvm-config found, run with original fw/config */
1241 ret = 0;
1242 } else if (ret == -EAGAIN) {
1244 * Userspace firmware loader will return -EAGAIN in case no
1245 * patch/nvm-config is found, so run with original fw/config.
1247 ret = 0;
1250 /* Setup bdaddr */
1251 if (qca_is_wcn399x(soc_type))
1252 hu->hdev->set_bdaddr = qca_set_bdaddr;
1253 else
1254 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1256 return ret;
1259 static struct hci_uart_proto qca_proto = {
1260 .id = HCI_UART_QCA,
1261 .name = "QCA",
1262 .manufacturer = 29,
1263 .init_speed = 115200,
1264 .oper_speed = 3000000,
1265 .open = qca_open,
1266 .close = qca_close,
1267 .flush = qca_flush,
1268 .setup = qca_setup,
1269 .recv = qca_recv,
1270 .enqueue = qca_enqueue,
1271 .dequeue = qca_dequeue,
1274 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1275 .soc_type = QCA_WCN3990,
1276 .vregs = (struct qca_vreg []) {
1277 { "vddio", 1800000, 1900000, 15000 },
1278 { "vddxo", 1800000, 1900000, 80000 },
1279 { "vddrf", 1300000, 1350000, 300000 },
1280 { "vddch0", 3300000, 3400000, 450000 },
1282 .num_vregs = 4,
1285 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1286 .soc_type = QCA_WCN3998,
1287 .vregs = (struct qca_vreg []) {
1288 { "vddio", 1800000, 1900000, 10000 },
1289 { "vddxo", 1800000, 1900000, 80000 },
1290 { "vddrf", 1300000, 1352000, 300000 },
1291 { "vddch0", 3300000, 3300000, 450000 },
1293 .num_vregs = 4,
1296 static void qca_power_shutdown(struct hci_uart *hu)
1298 struct qca_data *qca = hu->priv;
1299 unsigned long flags;
1301 /* From this point we go into power off state. But serial port is
1302 * still open, stop queueing the IBS data and flush all the buffered
1303 * data in skb's.
1305 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1306 clear_bit(QCA_IBS_ENABLED, &qca->flags);
1307 qca_flush(hu);
1308 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1310 host_set_baudrate(hu, 2400);
1311 qca_send_power_pulse(hu, false);
1312 qca_power_setup(hu, false);
1315 static int qca_power_off(struct hci_dev *hdev)
1317 struct hci_uart *hu = hci_get_drvdata(hdev);
1319 qca_power_shutdown(hu);
1320 return 0;
1323 static int qca_enable_regulator(struct qca_vreg vregs,
1324 struct regulator *regulator)
1326 int ret;
1328 ret = regulator_set_voltage(regulator, vregs.min_uV,
1329 vregs.max_uV);
1330 if (ret)
1331 return ret;
1333 if (vregs.load_uA)
1334 ret = regulator_set_load(regulator,
1335 vregs.load_uA);
1337 if (ret)
1338 return ret;
1340 return regulator_enable(regulator);
1344 static void qca_disable_regulator(struct qca_vreg vregs,
1345 struct regulator *regulator)
1347 regulator_disable(regulator);
1348 regulator_set_voltage(regulator, 0, vregs.max_uV);
1349 if (vregs.load_uA)
1350 regulator_set_load(regulator, 0);
1354 static int qca_power_setup(struct hci_uart *hu, bool on)
1356 struct qca_vreg *vregs;
1357 struct regulator_bulk_data *vreg_bulk;
1358 struct qca_serdev *qcadev;
1359 int i, num_vregs, ret = 0;
1361 qcadev = serdev_device_get_drvdata(hu->serdev);
1362 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1363 !qcadev->bt_power->vreg_bulk)
1364 return -EINVAL;
1366 vregs = qcadev->bt_power->vreg_data->vregs;
1367 vreg_bulk = qcadev->bt_power->vreg_bulk;
1368 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1369 BT_DBG("on: %d", on);
1370 if (on && !qcadev->bt_power->vregs_on) {
1371 for (i = 0; i < num_vregs; i++) {
1372 ret = qca_enable_regulator(vregs[i],
1373 vreg_bulk[i].consumer);
1374 if (ret)
1375 break;
1378 if (ret) {
1379 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1380 /* turn off regulators which are enabled */
1381 for (i = i - 1; i >= 0; i--)
1382 qca_disable_regulator(vregs[i],
1383 vreg_bulk[i].consumer);
1384 } else {
1385 qcadev->bt_power->vregs_on = true;
1387 } else if (!on && qcadev->bt_power->vregs_on) {
1388 /* turn off regulator in reverse order */
1389 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1390 for ( ; i >= 0; i--)
1391 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1393 qcadev->bt_power->vregs_on = false;
1396 return ret;
1399 static int qca_init_regulators(struct qca_power *qca,
1400 const struct qca_vreg *vregs, size_t num_vregs)
1402 int i;
1404 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
1405 sizeof(struct regulator_bulk_data),
1406 GFP_KERNEL);
1407 if (!qca->vreg_bulk)
1408 return -ENOMEM;
1410 for (i = 0; i < num_vregs; i++)
1411 qca->vreg_bulk[i].supply = vregs[i].name;
1413 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1416 static int qca_serdev_probe(struct serdev_device *serdev)
1418 struct qca_serdev *qcadev;
1419 const struct qca_vreg_data *data;
1420 int err;
1422 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1423 if (!qcadev)
1424 return -ENOMEM;
1426 qcadev->serdev_hu.serdev = serdev;
1427 data = of_device_get_match_data(&serdev->dev);
1428 serdev_device_set_drvdata(serdev, qcadev);
1429 if (data && qca_is_wcn399x(data->soc_type)) {
1430 qcadev->btsoc_type = data->soc_type;
1431 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1432 sizeof(struct qca_power),
1433 GFP_KERNEL);
1434 if (!qcadev->bt_power)
1435 return -ENOMEM;
1437 qcadev->bt_power->dev = &serdev->dev;
1438 qcadev->bt_power->vreg_data = data;
1439 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1440 data->num_vregs);
1441 if (err) {
1442 BT_ERR("Failed to init regulators:%d", err);
1443 goto out;
1446 qcadev->bt_power->vregs_on = false;
1448 device_property_read_u32(&serdev->dev, "max-speed",
1449 &qcadev->oper_speed);
1450 if (!qcadev->oper_speed)
1451 BT_DBG("UART will pick default operating speed");
1453 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1454 if (err) {
1455 BT_ERR("wcn3990 serdev registration failed");
1456 goto out;
1458 } else {
1459 qcadev->btsoc_type = QCA_ROME;
1460 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1461 GPIOD_OUT_LOW);
1462 if (IS_ERR(qcadev->bt_en)) {
1463 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1464 return PTR_ERR(qcadev->bt_en);
1467 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1468 if (IS_ERR(qcadev->susclk)) {
1469 dev_err(&serdev->dev, "failed to acquire clk\n");
1470 return PTR_ERR(qcadev->susclk);
1473 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1474 if (err)
1475 return err;
1477 err = clk_prepare_enable(qcadev->susclk);
1478 if (err)
1479 return err;
1481 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1482 if (err)
1483 clk_disable_unprepare(qcadev->susclk);
1486 out: return err;
1490 static void qca_serdev_remove(struct serdev_device *serdev)
1492 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1494 if (qca_is_wcn399x(qcadev->btsoc_type))
1495 qca_power_shutdown(&qcadev->serdev_hu);
1496 else
1497 clk_disable_unprepare(qcadev->susclk);
1499 hci_uart_unregister_device(&qcadev->serdev_hu);
1502 static const struct of_device_id qca_bluetooth_of_match[] = {
1503 { .compatible = "qcom,qca6174-bt" },
1504 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1505 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1506 { /* sentinel */ }
1508 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1510 static struct serdev_device_driver qca_serdev_driver = {
1511 .probe = qca_serdev_probe,
1512 .remove = qca_serdev_remove,
1513 .driver = {
1514 .name = "hci_uart_qca",
1515 .of_match_table = qca_bluetooth_of_match,
1519 int __init qca_init(void)
1521 serdev_device_driver_register(&qca_serdev_driver);
1523 return hci_uart_register_proto(&qca_proto);
1526 int __exit qca_deinit(void)
1528 serdev_device_driver_unregister(&qca_serdev_driver);
1530 return hci_uart_unregister_proto(&qca_proto);