]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/bluetooth/hci_qca.c
Bluetooth: hci_qca: Add qca_power_on() API to support both wcn399x and Rome power up
[linux.git] / drivers / bluetooth / hci_qca.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Bluetooth Software UART Qualcomm protocol
4  *
5  *  HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management
6  *  protocol extension to H4.
7  *
8  *  Copyright (C) 2007 Texas Instruments, Inc.
9  *  Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
10  *
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.
16  */
17
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/devcoredump.h>
24 #include <linux/device.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/mod_devicetable.h>
27 #include <linux/module.h>
28 #include <linux/of_device.h>
29 #include <linux/platform_device.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/serdev.h>
32 #include <asm/unaligned.h>
33
34 #include <net/bluetooth/bluetooth.h>
35 #include <net/bluetooth/hci_core.h>
36
37 #include "hci_uart.h"
38 #include "btqca.h"
39
40 /* HCI_IBS protocol messages */
41 #define HCI_IBS_SLEEP_IND       0xFE
42 #define HCI_IBS_WAKE_IND        0xFD
43 #define HCI_IBS_WAKE_ACK        0xFC
44 #define HCI_MAX_IBS_SIZE        10
45
46 #define IBS_WAKE_RETRANS_TIMEOUT_MS     100
47 #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS    40
48 #define IBS_HOST_TX_IDLE_TIMEOUT_MS     2000
49 #define CMD_TRANS_TIMEOUT_MS            100
50 #define MEMDUMP_TIMEOUT_MS              8000
51
52 /* susclk rate */
53 #define SUSCLK_RATE_32KHZ       32768
54
55 /* Controller debug log header */
56 #define QCA_DEBUG_HANDLE        0x2EDC
57
58 /* Controller dump header */
59 #define QCA_SSR_DUMP_HANDLE             0x0108
60 #define QCA_DUMP_PACKET_SIZE            255
61 #define QCA_LAST_SEQUENCE_NUM           0xFFFF
62 #define QCA_CRASHBYTE_PACKET_LEN        1096
63 #define QCA_MEMDUMP_BYTE                0xFB
64
65 enum qca_flags {
66         QCA_IBS_ENABLED,
67         QCA_DROP_VENDOR_EVENT,
68         QCA_SUSPENDING,
69         QCA_MEMDUMP_COLLECTION
70 };
71
72
73 /* HCI_IBS transmit side sleep protocol states */
74 enum tx_ibs_states {
75         HCI_IBS_TX_ASLEEP,
76         HCI_IBS_TX_WAKING,
77         HCI_IBS_TX_AWAKE,
78 };
79
80 /* HCI_IBS receive side sleep protocol states */
81 enum rx_states {
82         HCI_IBS_RX_ASLEEP,
83         HCI_IBS_RX_AWAKE,
84 };
85
86 /* HCI_IBS transmit and receive side clock state vote */
87 enum hci_ibs_clock_state_vote {
88         HCI_IBS_VOTE_STATS_UPDATE,
89         HCI_IBS_TX_VOTE_CLOCK_ON,
90         HCI_IBS_TX_VOTE_CLOCK_OFF,
91         HCI_IBS_RX_VOTE_CLOCK_ON,
92         HCI_IBS_RX_VOTE_CLOCK_OFF,
93 };
94
95 /* Controller memory dump states */
96 enum qca_memdump_states {
97         QCA_MEMDUMP_IDLE,
98         QCA_MEMDUMP_COLLECTING,
99         QCA_MEMDUMP_COLLECTED,
100         QCA_MEMDUMP_TIMEOUT,
101 };
102
103 struct qca_memdump_data {
104         char *memdump_buf_head;
105         char *memdump_buf_tail;
106         u32 current_seq_no;
107         u32 received_dump;
108 };
109
110 struct qca_memdump_event_hdr {
111         __u8    evt;
112         __u8    plen;
113         __u16   opcode;
114         __u16   seq_no;
115         __u8    reserved;
116 } __packed;
117
118
119 struct qca_dump_size {
120         u32 dump_size;
121 } __packed;
122
123 struct qca_data {
124         struct hci_uart *hu;
125         struct sk_buff *rx_skb;
126         struct sk_buff_head txq;
127         struct sk_buff_head tx_wait_q;  /* HCI_IBS wait queue   */
128         struct sk_buff_head rx_memdump_q;       /* Memdump wait queue   */
129         spinlock_t hci_ibs_lock;        /* HCI_IBS state lock   */
130         u8 tx_ibs_state;        /* HCI_IBS transmit side power state*/
131         u8 rx_ibs_state;        /* HCI_IBS receive side power state */
132         bool tx_vote;           /* Clock must be on for TX */
133         bool rx_vote;           /* Clock must be on for RX */
134         struct timer_list tx_idle_timer;
135         u32 tx_idle_delay;
136         struct timer_list wake_retrans_timer;
137         u32 wake_retrans;
138         struct timer_list memdump_timer;
139         struct workqueue_struct *workqueue;
140         struct work_struct ws_awake_rx;
141         struct work_struct ws_awake_device;
142         struct work_struct ws_rx_vote_off;
143         struct work_struct ws_tx_vote_off;
144         struct work_struct ctrl_memdump_evt;
145         struct qca_memdump_data *qca_memdump;
146         unsigned long flags;
147         struct completion drop_ev_comp;
148         wait_queue_head_t suspend_wait_q;
149         enum qca_memdump_states memdump_state;
150
151         /* For debugging purpose */
152         u64 ibs_sent_wacks;
153         u64 ibs_sent_slps;
154         u64 ibs_sent_wakes;
155         u64 ibs_recv_wacks;
156         u64 ibs_recv_slps;
157         u64 ibs_recv_wakes;
158         u64 vote_last_jif;
159         u32 vote_on_ms;
160         u32 vote_off_ms;
161         u64 tx_votes_on;
162         u64 rx_votes_on;
163         u64 tx_votes_off;
164         u64 rx_votes_off;
165         u64 votes_on;
166         u64 votes_off;
167 };
168
169 enum qca_speed_type {
170         QCA_INIT_SPEED = 1,
171         QCA_OPER_SPEED
172 };
173
174 /*
175  * Voltage regulator information required for configuring the
176  * QCA Bluetooth chipset
177  */
178 struct qca_vreg {
179         const char *name;
180         unsigned int load_uA;
181 };
182
183 struct qca_vreg_data {
184         enum qca_btsoc_type soc_type;
185         struct qca_vreg *vregs;
186         size_t num_vregs;
187 };
188
189 /*
190  * Platform data for the QCA Bluetooth power driver.
191  */
192 struct qca_power {
193         struct device *dev;
194         struct regulator_bulk_data *vreg_bulk;
195         int num_vregs;
196         bool vregs_on;
197 };
198
199 struct qca_serdev {
200         struct hci_uart  serdev_hu;
201         struct gpio_desc *bt_en;
202         struct clk       *susclk;
203         enum qca_btsoc_type btsoc_type;
204         struct qca_power *bt_power;
205         u32 init_speed;
206         u32 oper_speed;
207         const char *firmware_name;
208 };
209
210 static int qca_regulator_enable(struct qca_serdev *qcadev);
211 static void qca_regulator_disable(struct qca_serdev *qcadev);
212 static void qca_power_shutdown(struct hci_uart *hu);
213 static int qca_power_off(struct hci_dev *hdev);
214 static void qca_controller_memdump(struct work_struct *work);
215
216 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
217 {
218         enum qca_btsoc_type soc_type;
219
220         if (hu->serdev) {
221                 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
222
223                 soc_type = qsd->btsoc_type;
224         } else {
225                 soc_type = QCA_ROME;
226         }
227
228         return soc_type;
229 }
230
231 static const char *qca_get_firmware_name(struct hci_uart *hu)
232 {
233         if (hu->serdev) {
234                 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
235
236                 return qsd->firmware_name;
237         } else {
238                 return NULL;
239         }
240 }
241
242 static void __serial_clock_on(struct tty_struct *tty)
243 {
244         /* TODO: Some chipset requires to enable UART clock on client
245          * side to save power consumption or manual work is required.
246          * Please put your code to control UART clock here if needed
247          */
248 }
249
250 static void __serial_clock_off(struct tty_struct *tty)
251 {
252         /* TODO: Some chipset requires to disable UART clock on client
253          * side to save power consumption or manual work is required.
254          * Please put your code to control UART clock off here if needed
255          */
256 }
257
258 /* serial_clock_vote needs to be called with the ibs lock held */
259 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
260 {
261         struct qca_data *qca = hu->priv;
262         unsigned int diff;
263
264         bool old_vote = (qca->tx_vote | qca->rx_vote);
265         bool new_vote;
266
267         switch (vote) {
268         case HCI_IBS_VOTE_STATS_UPDATE:
269                 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
270
271                 if (old_vote)
272                         qca->vote_off_ms += diff;
273                 else
274                         qca->vote_on_ms += diff;
275                 return;
276
277         case HCI_IBS_TX_VOTE_CLOCK_ON:
278                 qca->tx_vote = true;
279                 qca->tx_votes_on++;
280                 new_vote = true;
281                 break;
282
283         case HCI_IBS_RX_VOTE_CLOCK_ON:
284                 qca->rx_vote = true;
285                 qca->rx_votes_on++;
286                 new_vote = true;
287                 break;
288
289         case HCI_IBS_TX_VOTE_CLOCK_OFF:
290                 qca->tx_vote = false;
291                 qca->tx_votes_off++;
292                 new_vote = qca->rx_vote | qca->tx_vote;
293                 break;
294
295         case HCI_IBS_RX_VOTE_CLOCK_OFF:
296                 qca->rx_vote = false;
297                 qca->rx_votes_off++;
298                 new_vote = qca->rx_vote | qca->tx_vote;
299                 break;
300
301         default:
302                 BT_ERR("Voting irregularity");
303                 return;
304         }
305
306         if (new_vote != old_vote) {
307                 if (new_vote)
308                         __serial_clock_on(hu->tty);
309                 else
310                         __serial_clock_off(hu->tty);
311
312                 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
313                        vote ? "true" : "false");
314
315                 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
316
317                 if (new_vote) {
318                         qca->votes_on++;
319                         qca->vote_off_ms += diff;
320                 } else {
321                         qca->votes_off++;
322                         qca->vote_on_ms += diff;
323                 }
324                 qca->vote_last_jif = jiffies;
325         }
326 }
327
328 /* Builds and sends an HCI_IBS command packet.
329  * These are very simple packets with only 1 cmd byte.
330  */
331 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
332 {
333         int err = 0;
334         struct sk_buff *skb = NULL;
335         struct qca_data *qca = hu->priv;
336
337         BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
338
339         skb = bt_skb_alloc(1, GFP_ATOMIC);
340         if (!skb) {
341                 BT_ERR("Failed to allocate memory for HCI_IBS packet");
342                 return -ENOMEM;
343         }
344
345         /* Assign HCI_IBS type */
346         skb_put_u8(skb, cmd);
347
348         skb_queue_tail(&qca->txq, skb);
349
350         return err;
351 }
352
353 static void qca_wq_awake_device(struct work_struct *work)
354 {
355         struct qca_data *qca = container_of(work, struct qca_data,
356                                             ws_awake_device);
357         struct hci_uart *hu = qca->hu;
358         unsigned long retrans_delay;
359         unsigned long flags;
360
361         BT_DBG("hu %p wq awake device", hu);
362
363         /* Vote for serial clock */
364         serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
365
366         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
367
368         /* Send wake indication to device */
369         if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
370                 BT_ERR("Failed to send WAKE to device");
371
372         qca->ibs_sent_wakes++;
373
374         /* Start retransmit timer */
375         retrans_delay = msecs_to_jiffies(qca->wake_retrans);
376         mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
377
378         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
379
380         /* Actually send the packets */
381         hci_uart_tx_wakeup(hu);
382 }
383
384 static void qca_wq_awake_rx(struct work_struct *work)
385 {
386         struct qca_data *qca = container_of(work, struct qca_data,
387                                             ws_awake_rx);
388         struct hci_uart *hu = qca->hu;
389         unsigned long flags;
390
391         BT_DBG("hu %p wq awake rx", hu);
392
393         serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
394
395         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
396         qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
397
398         /* Always acknowledge device wake up,
399          * sending IBS message doesn't count as TX ON.
400          */
401         if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
402                 BT_ERR("Failed to acknowledge device wake up");
403
404         qca->ibs_sent_wacks++;
405
406         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
407
408         /* Actually send the packets */
409         hci_uart_tx_wakeup(hu);
410 }
411
412 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
413 {
414         struct qca_data *qca = container_of(work, struct qca_data,
415                                             ws_rx_vote_off);
416         struct hci_uart *hu = qca->hu;
417
418         BT_DBG("hu %p rx clock vote off", hu);
419
420         serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
421 }
422
423 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
424 {
425         struct qca_data *qca = container_of(work, struct qca_data,
426                                             ws_tx_vote_off);
427         struct hci_uart *hu = qca->hu;
428
429         BT_DBG("hu %p tx clock vote off", hu);
430
431         /* Run HCI tx handling unlocked */
432         hci_uart_tx_wakeup(hu);
433
434         /* Now that message queued to tty driver, vote for tty clocks off.
435          * It is up to the tty driver to pend the clocks off until tx done.
436          */
437         serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
438 }
439
440 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
441 {
442         struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
443         struct hci_uart *hu = qca->hu;
444         unsigned long flags;
445
446         BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
447
448         spin_lock_irqsave_nested(&qca->hci_ibs_lock,
449                                  flags, SINGLE_DEPTH_NESTING);
450
451         switch (qca->tx_ibs_state) {
452         case HCI_IBS_TX_AWAKE:
453                 /* TX_IDLE, go to SLEEP */
454                 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
455                         BT_ERR("Failed to send SLEEP to device");
456                         break;
457                 }
458                 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
459                 qca->ibs_sent_slps++;
460                 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
461                 break;
462
463         case HCI_IBS_TX_ASLEEP:
464         case HCI_IBS_TX_WAKING:
465                 /* Fall through */
466
467         default:
468                 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
469                 break;
470         }
471
472         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
473 }
474
475 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
476 {
477         struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
478         struct hci_uart *hu = qca->hu;
479         unsigned long flags, retrans_delay;
480         bool retransmit = false;
481
482         BT_DBG("hu %p wake retransmit timeout in %d state",
483                 hu, qca->tx_ibs_state);
484
485         spin_lock_irqsave_nested(&qca->hci_ibs_lock,
486                                  flags, SINGLE_DEPTH_NESTING);
487
488         /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
489         if (test_bit(QCA_SUSPENDING, &qca->flags)) {
490                 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
491                 return;
492         }
493
494         switch (qca->tx_ibs_state) {
495         case HCI_IBS_TX_WAKING:
496                 /* No WAKE_ACK, retransmit WAKE */
497                 retransmit = true;
498                 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
499                         BT_ERR("Failed to acknowledge device wake up");
500                         break;
501                 }
502                 qca->ibs_sent_wakes++;
503                 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
504                 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
505                 break;
506
507         case HCI_IBS_TX_ASLEEP:
508         case HCI_IBS_TX_AWAKE:
509                 /* Fall through */
510
511         default:
512                 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
513                 break;
514         }
515
516         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
517
518         if (retransmit)
519                 hci_uart_tx_wakeup(hu);
520 }
521
522 static void hci_memdump_timeout(struct timer_list *t)
523 {
524         struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
525         struct hci_uart *hu = qca->hu;
526         struct qca_memdump_data *qca_memdump = qca->qca_memdump;
527         char *memdump_buf = qca_memdump->memdump_buf_tail;
528
529         bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout");
530         /* Inject hw error event to reset the device and driver. */
531         hci_reset_dev(hu->hdev);
532         vfree(memdump_buf);
533         kfree(qca_memdump);
534         qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
535         del_timer(&qca->memdump_timer);
536         cancel_work_sync(&qca->ctrl_memdump_evt);
537 }
538
539 /* Initialize protocol */
540 static int qca_open(struct hci_uart *hu)
541 {
542         struct qca_serdev *qcadev;
543         struct qca_data *qca;
544
545         BT_DBG("hu %p qca_open", hu);
546
547         if (!hci_uart_has_flow_control(hu))
548                 return -EOPNOTSUPP;
549
550         qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
551         if (!qca)
552                 return -ENOMEM;
553
554         skb_queue_head_init(&qca->txq);
555         skb_queue_head_init(&qca->tx_wait_q);
556         skb_queue_head_init(&qca->rx_memdump_q);
557         spin_lock_init(&qca->hci_ibs_lock);
558         qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
559         if (!qca->workqueue) {
560                 BT_ERR("QCA Workqueue not initialized properly");
561                 kfree(qca);
562                 return -ENOMEM;
563         }
564
565         INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
566         INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
567         INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
568         INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
569         INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump);
570         init_waitqueue_head(&qca->suspend_wait_q);
571
572         qca->hu = hu;
573         init_completion(&qca->drop_ev_comp);
574
575         /* Assume we start with both sides asleep -- extra wakes OK */
576         qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
577         qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
578
579         qca->vote_last_jif = jiffies;
580
581         hu->priv = qca;
582
583         if (hu->serdev) {
584                 qcadev = serdev_device_get_drvdata(hu->serdev);
585                 if (qca_is_wcn399x(qcadev->btsoc_type)) {
586                         hu->init_speed = qcadev->init_speed;
587                         hu->oper_speed = qcadev->oper_speed;
588                 }
589         }
590
591         timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
592         qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
593
594         timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
595         qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
596         timer_setup(&qca->memdump_timer, hci_memdump_timeout, 0);
597
598         BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
599                qca->tx_idle_delay, qca->wake_retrans);
600
601         return 0;
602 }
603
604 static void qca_debugfs_init(struct hci_dev *hdev)
605 {
606         struct hci_uart *hu = hci_get_drvdata(hdev);
607         struct qca_data *qca = hu->priv;
608         struct dentry *ibs_dir;
609         umode_t mode;
610
611         if (!hdev->debugfs)
612                 return;
613
614         ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
615
616         /* read only */
617         mode = S_IRUGO;
618         debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
619         debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
620         debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
621                            &qca->ibs_sent_slps);
622         debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
623                            &qca->ibs_sent_wakes);
624         debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
625                            &qca->ibs_sent_wacks);
626         debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
627                            &qca->ibs_recv_slps);
628         debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
629                            &qca->ibs_recv_wakes);
630         debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
631                            &qca->ibs_recv_wacks);
632         debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
633         debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
634         debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
635         debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
636         debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
637         debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
638         debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
639         debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
640         debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
641         debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
642
643         /* read/write */
644         mode = S_IRUGO | S_IWUSR;
645         debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
646         debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
647                            &qca->tx_idle_delay);
648 }
649
650 /* Flush protocol data */
651 static int qca_flush(struct hci_uart *hu)
652 {
653         struct qca_data *qca = hu->priv;
654
655         BT_DBG("hu %p qca flush", hu);
656
657         skb_queue_purge(&qca->tx_wait_q);
658         skb_queue_purge(&qca->txq);
659
660         return 0;
661 }
662
663 /* Close protocol */
664 static int qca_close(struct hci_uart *hu)
665 {
666         struct qca_serdev *qcadev;
667         struct qca_data *qca = hu->priv;
668
669         BT_DBG("hu %p qca close", hu);
670
671         serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
672
673         skb_queue_purge(&qca->tx_wait_q);
674         skb_queue_purge(&qca->txq);
675         skb_queue_purge(&qca->rx_memdump_q);
676         del_timer(&qca->tx_idle_timer);
677         del_timer(&qca->wake_retrans_timer);
678         del_timer(&qca->memdump_timer);
679         destroy_workqueue(qca->workqueue);
680         qca->hu = NULL;
681
682         if (hu->serdev) {
683                 qcadev = serdev_device_get_drvdata(hu->serdev);
684                 if (qca_is_wcn399x(qcadev->btsoc_type))
685                         qca_power_shutdown(hu);
686                 else
687                         gpiod_set_value_cansleep(qcadev->bt_en, 0);
688
689         }
690
691         kfree_skb(qca->rx_skb);
692
693         hu->priv = NULL;
694
695         kfree(qca);
696
697         return 0;
698 }
699
700 /* Called upon a wake-up-indication from the device.
701  */
702 static void device_want_to_wakeup(struct hci_uart *hu)
703 {
704         unsigned long flags;
705         struct qca_data *qca = hu->priv;
706
707         BT_DBG("hu %p want to wake up", hu);
708
709         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
710
711         qca->ibs_recv_wakes++;
712
713         /* Don't wake the rx up when suspending. */
714         if (test_bit(QCA_SUSPENDING, &qca->flags)) {
715                 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
716                 return;
717         }
718
719         switch (qca->rx_ibs_state) {
720         case HCI_IBS_RX_ASLEEP:
721                 /* Make sure clock is on - we may have turned clock off since
722                  * receiving the wake up indicator awake rx clock.
723                  */
724                 queue_work(qca->workqueue, &qca->ws_awake_rx);
725                 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
726                 return;
727
728         case HCI_IBS_RX_AWAKE:
729                 /* Always acknowledge device wake up,
730                  * sending IBS message doesn't count as TX ON.
731                  */
732                 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
733                         BT_ERR("Failed to acknowledge device wake up");
734                         break;
735                 }
736                 qca->ibs_sent_wacks++;
737                 break;
738
739         default:
740                 /* Any other state is illegal */
741                 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
742                        qca->rx_ibs_state);
743                 break;
744         }
745
746         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
747
748         /* Actually send the packets */
749         hci_uart_tx_wakeup(hu);
750 }
751
752 /* Called upon a sleep-indication from the device.
753  */
754 static void device_want_to_sleep(struct hci_uart *hu)
755 {
756         unsigned long flags;
757         struct qca_data *qca = hu->priv;
758
759         BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
760
761         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
762
763         qca->ibs_recv_slps++;
764
765         switch (qca->rx_ibs_state) {
766         case HCI_IBS_RX_AWAKE:
767                 /* Update state */
768                 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
769                 /* Vote off rx clock under workqueue */
770                 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
771                 break;
772
773         case HCI_IBS_RX_ASLEEP:
774                 break;
775
776         default:
777                 /* Any other state is illegal */
778                 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
779                        qca->rx_ibs_state);
780                 break;
781         }
782
783         wake_up_interruptible(&qca->suspend_wait_q);
784
785         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
786 }
787
788 /* Called upon wake-up-acknowledgement from the device
789  */
790 static void device_woke_up(struct hci_uart *hu)
791 {
792         unsigned long flags, idle_delay;
793         struct qca_data *qca = hu->priv;
794         struct sk_buff *skb = NULL;
795
796         BT_DBG("hu %p woke up", hu);
797
798         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
799
800         qca->ibs_recv_wacks++;
801
802         /* Don't react to the wake-up-acknowledgment when suspending. */
803         if (test_bit(QCA_SUSPENDING, &qca->flags)) {
804                 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
805                 return;
806         }
807
808         switch (qca->tx_ibs_state) {
809         case HCI_IBS_TX_AWAKE:
810                 /* Expect one if we send 2 WAKEs */
811                 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
812                        qca->tx_ibs_state);
813                 break;
814
815         case HCI_IBS_TX_WAKING:
816                 /* Send pending packets */
817                 while ((skb = skb_dequeue(&qca->tx_wait_q)))
818                         skb_queue_tail(&qca->txq, skb);
819
820                 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
821                 del_timer(&qca->wake_retrans_timer);
822                 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
823                 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
824                 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
825                 break;
826
827         case HCI_IBS_TX_ASLEEP:
828                 /* Fall through */
829
830         default:
831                 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
832                        qca->tx_ibs_state);
833                 break;
834         }
835
836         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
837
838         /* Actually send the packets */
839         hci_uart_tx_wakeup(hu);
840 }
841
842 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
843  * two simultaneous tasklets.
844  */
845 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
846 {
847         unsigned long flags = 0, idle_delay;
848         struct qca_data *qca = hu->priv;
849
850         BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
851                qca->tx_ibs_state);
852
853         /* Prepend skb with frame type */
854         memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
855
856         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
857
858         /* Don't go to sleep in middle of patch download or
859          * Out-Of-Band(GPIOs control) sleep is selected.
860          * Don't wake the device up when suspending.
861          */
862         if (!test_bit(QCA_IBS_ENABLED, &qca->flags) ||
863             test_bit(QCA_SUSPENDING, &qca->flags)) {
864                 skb_queue_tail(&qca->txq, skb);
865                 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
866                 return 0;
867         }
868
869         /* Act according to current state */
870         switch (qca->tx_ibs_state) {
871         case HCI_IBS_TX_AWAKE:
872                 BT_DBG("Device awake, sending normally");
873                 skb_queue_tail(&qca->txq, skb);
874                 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
875                 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
876                 break;
877
878         case HCI_IBS_TX_ASLEEP:
879                 BT_DBG("Device asleep, waking up and queueing packet");
880                 /* Save packet for later */
881                 skb_queue_tail(&qca->tx_wait_q, skb);
882
883                 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
884                 /* Schedule a work queue to wake up device */
885                 queue_work(qca->workqueue, &qca->ws_awake_device);
886                 break;
887
888         case HCI_IBS_TX_WAKING:
889                 BT_DBG("Device waking up, queueing packet");
890                 /* Transient state; just keep packet for later */
891                 skb_queue_tail(&qca->tx_wait_q, skb);
892                 break;
893
894         default:
895                 BT_ERR("Illegal tx state: %d (losing packet)",
896                        qca->tx_ibs_state);
897                 kfree_skb(skb);
898                 break;
899         }
900
901         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
902
903         return 0;
904 }
905
906 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
907 {
908         struct hci_uart *hu = hci_get_drvdata(hdev);
909
910         BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
911
912         device_want_to_sleep(hu);
913
914         kfree_skb(skb);
915         return 0;
916 }
917
918 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
919 {
920         struct hci_uart *hu = hci_get_drvdata(hdev);
921
922         BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
923
924         device_want_to_wakeup(hu);
925
926         kfree_skb(skb);
927         return 0;
928 }
929
930 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
931 {
932         struct hci_uart *hu = hci_get_drvdata(hdev);
933
934         BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
935
936         device_woke_up(hu);
937
938         kfree_skb(skb);
939         return 0;
940 }
941
942 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
943 {
944         /* We receive debug logs from chip as an ACL packets.
945          * Instead of sending the data to ACL to decode the
946          * received data, we are pushing them to the above layers
947          * as a diagnostic packet.
948          */
949         if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
950                 return hci_recv_diag(hdev, skb);
951
952         return hci_recv_frame(hdev, skb);
953 }
954
955 static void qca_controller_memdump(struct work_struct *work)
956 {
957         struct qca_data *qca = container_of(work, struct qca_data,
958                                             ctrl_memdump_evt);
959         struct hci_uart *hu = qca->hu;
960         struct sk_buff *skb;
961         struct qca_memdump_event_hdr *cmd_hdr;
962         struct qca_memdump_data *qca_memdump = qca->qca_memdump;
963         struct qca_dump_size *dump;
964         char *memdump_buf;
965         char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 };
966         u16 seq_no;
967         u32 dump_size;
968
969         while ((skb = skb_dequeue(&qca->rx_memdump_q))) {
970
971                 if (!qca_memdump) {
972                         qca_memdump = kzalloc(sizeof(struct qca_memdump_data),
973                                               GFP_ATOMIC);
974                         if (!qca_memdump)
975                                 return;
976
977                         qca->qca_memdump = qca_memdump;
978                 }
979
980                 qca->memdump_state = QCA_MEMDUMP_COLLECTING;
981                 cmd_hdr = (void *) skb->data;
982                 seq_no = __le16_to_cpu(cmd_hdr->seq_no);
983                 skb_pull(skb, sizeof(struct qca_memdump_event_hdr));
984
985                 if (!seq_no) {
986
987                         /* This is the first frame of memdump packet from
988                          * the controller, Disable IBS to recevie dump
989                          * with out any interruption, ideally time required for
990                          * the controller to send the dump is 8 seconds. let us
991                          * start timer to handle this asynchronous activity.
992                          */
993                         clear_bit(QCA_IBS_ENABLED, &qca->flags);
994                         set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
995                         dump = (void *) skb->data;
996                         dump_size = __le32_to_cpu(dump->dump_size);
997                         if (!(dump_size)) {
998                                 bt_dev_err(hu->hdev, "Rx invalid memdump size");
999                                 kfree_skb(skb);
1000                                 return;
1001                         }
1002
1003                         bt_dev_info(hu->hdev, "QCA collecting dump of size:%u",
1004                                     dump_size);
1005                         mod_timer(&qca->memdump_timer, (jiffies +
1006                                   msecs_to_jiffies(MEMDUMP_TIMEOUT_MS)));
1007
1008                         skb_pull(skb, sizeof(dump_size));
1009                         memdump_buf = vmalloc(dump_size);
1010                         qca_memdump->memdump_buf_head = memdump_buf;
1011                         qca_memdump->memdump_buf_tail = memdump_buf;
1012                 }
1013
1014                 memdump_buf = qca_memdump->memdump_buf_tail;
1015
1016                 /* If sequence no 0 is missed then there is no point in
1017                  * accepting the other sequences.
1018                  */
1019                 if (!memdump_buf) {
1020                         bt_dev_err(hu->hdev, "QCA: Discarding other packets");
1021                         kfree(qca_memdump);
1022                         kfree_skb(skb);
1023                         qca->qca_memdump = NULL;
1024                         return;
1025                 }
1026
1027                 /* There could be chance of missing some packets from
1028                  * the controller. In such cases let us store the dummy
1029                  * packets in the buffer.
1030                  */
1031                 while ((seq_no > qca_memdump->current_seq_no + 1) &&
1032                         seq_no != QCA_LAST_SEQUENCE_NUM) {
1033                         bt_dev_err(hu->hdev, "QCA controller missed packet:%d",
1034                                    qca_memdump->current_seq_no);
1035                         memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE);
1036                         memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE;
1037                         qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE;
1038                         qca_memdump->current_seq_no++;
1039                 }
1040
1041                 memcpy(memdump_buf, (unsigned char *) skb->data, skb->len);
1042                 memdump_buf = memdump_buf + skb->len;
1043                 qca_memdump->memdump_buf_tail = memdump_buf;
1044                 qca_memdump->current_seq_no = seq_no + 1;
1045                 qca_memdump->received_dump += skb->len;
1046                 qca->qca_memdump = qca_memdump;
1047                 kfree_skb(skb);
1048                 if (seq_no == QCA_LAST_SEQUENCE_NUM) {
1049                         bt_dev_info(hu->hdev, "QCA writing crash dump of size %d bytes",
1050                                    qca_memdump->received_dump);
1051                         memdump_buf = qca_memdump->memdump_buf_head;
1052                         dev_coredumpv(&hu->serdev->dev, memdump_buf,
1053                                       qca_memdump->received_dump, GFP_KERNEL);
1054                         del_timer(&qca->memdump_timer);
1055                         kfree(qca->qca_memdump);
1056                         qca->qca_memdump = NULL;
1057                         qca->memdump_state = QCA_MEMDUMP_COLLECTED;
1058                 }
1059         }
1060
1061 }
1062
1063 int qca_controller_memdump_event(struct hci_dev *hdev, struct sk_buff *skb)
1064 {
1065         struct hci_uart *hu = hci_get_drvdata(hdev);
1066         struct qca_data *qca = hu->priv;
1067
1068         skb_queue_tail(&qca->rx_memdump_q, skb);
1069         queue_work(qca->workqueue, &qca->ctrl_memdump_evt);
1070
1071         return 0;
1072 }
1073
1074 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1075 {
1076         struct hci_uart *hu = hci_get_drvdata(hdev);
1077         struct qca_data *qca = hu->priv;
1078
1079         if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
1080                 struct hci_event_hdr *hdr = (void *)skb->data;
1081
1082                 /* For the WCN3990 the vendor command for a baudrate change
1083                  * isn't sent as synchronous HCI command, because the
1084                  * controller sends the corresponding vendor event with the
1085                  * new baudrate. The event is received and properly decoded
1086                  * after changing the baudrate of the host port. It needs to
1087                  * be dropped, otherwise it can be misinterpreted as
1088                  * response to a later firmware download command (also a
1089                  * vendor command).
1090                  */
1091
1092                 if (hdr->evt == HCI_EV_VENDOR)
1093                         complete(&qca->drop_ev_comp);
1094
1095                 kfree_skb(skb);
1096
1097                 return 0;
1098         }
1099         /* We receive chip memory dump as an event packet, With a dedicated
1100          * handler followed by a hardware error event. When this event is
1101          * received we store dump into a file before closing hci. This
1102          * dump will help in triaging the issues.
1103          */
1104         if ((skb->data[0] == HCI_VENDOR_PKT) &&
1105             (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE))
1106                 return qca_controller_memdump_event(hdev, skb);
1107
1108         return hci_recv_frame(hdev, skb);
1109 }
1110
1111 #define QCA_IBS_SLEEP_IND_EVENT \
1112         .type = HCI_IBS_SLEEP_IND, \
1113         .hlen = 0, \
1114         .loff = 0, \
1115         .lsize = 0, \
1116         .maxlen = HCI_MAX_IBS_SIZE
1117
1118 #define QCA_IBS_WAKE_IND_EVENT \
1119         .type = HCI_IBS_WAKE_IND, \
1120         .hlen = 0, \
1121         .loff = 0, \
1122         .lsize = 0, \
1123         .maxlen = HCI_MAX_IBS_SIZE
1124
1125 #define QCA_IBS_WAKE_ACK_EVENT \
1126         .type = HCI_IBS_WAKE_ACK, \
1127         .hlen = 0, \
1128         .loff = 0, \
1129         .lsize = 0, \
1130         .maxlen = HCI_MAX_IBS_SIZE
1131
1132 static const struct h4_recv_pkt qca_recv_pkts[] = {
1133         { H4_RECV_ACL,             .recv = qca_recv_acl_data },
1134         { H4_RECV_SCO,             .recv = hci_recv_frame    },
1135         { H4_RECV_EVENT,           .recv = qca_recv_event    },
1136         { QCA_IBS_WAKE_IND_EVENT,  .recv = qca_ibs_wake_ind  },
1137         { QCA_IBS_WAKE_ACK_EVENT,  .recv = qca_ibs_wake_ack  },
1138         { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
1139 };
1140
1141 static int qca_recv(struct hci_uart *hu, const void *data, int count)
1142 {
1143         struct qca_data *qca = hu->priv;
1144
1145         if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1146                 return -EUNATCH;
1147
1148         qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
1149                                   qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
1150         if (IS_ERR(qca->rx_skb)) {
1151                 int err = PTR_ERR(qca->rx_skb);
1152                 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
1153                 qca->rx_skb = NULL;
1154                 return err;
1155         }
1156
1157         return count;
1158 }
1159
1160 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
1161 {
1162         struct qca_data *qca = hu->priv;
1163
1164         return skb_dequeue(&qca->txq);
1165 }
1166
1167 static uint8_t qca_get_baudrate_value(int speed)
1168 {
1169         switch (speed) {
1170         case 9600:
1171                 return QCA_BAUDRATE_9600;
1172         case 19200:
1173                 return QCA_BAUDRATE_19200;
1174         case 38400:
1175                 return QCA_BAUDRATE_38400;
1176         case 57600:
1177                 return QCA_BAUDRATE_57600;
1178         case 115200:
1179                 return QCA_BAUDRATE_115200;
1180         case 230400:
1181                 return QCA_BAUDRATE_230400;
1182         case 460800:
1183                 return QCA_BAUDRATE_460800;
1184         case 500000:
1185                 return QCA_BAUDRATE_500000;
1186         case 921600:
1187                 return QCA_BAUDRATE_921600;
1188         case 1000000:
1189                 return QCA_BAUDRATE_1000000;
1190         case 2000000:
1191                 return QCA_BAUDRATE_2000000;
1192         case 3000000:
1193                 return QCA_BAUDRATE_3000000;
1194         case 3200000:
1195                 return QCA_BAUDRATE_3200000;
1196         case 3500000:
1197                 return QCA_BAUDRATE_3500000;
1198         default:
1199                 return QCA_BAUDRATE_115200;
1200         }
1201 }
1202
1203 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1204 {
1205         struct hci_uart *hu = hci_get_drvdata(hdev);
1206         struct qca_data *qca = hu->priv;
1207         struct sk_buff *skb;
1208         u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1209
1210         if (baudrate > QCA_BAUDRATE_3200000)
1211                 return -EINVAL;
1212
1213         cmd[4] = baudrate;
1214
1215         skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1216         if (!skb) {
1217                 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1218                 return -ENOMEM;
1219         }
1220
1221         /* Assign commands to change baudrate and packet type. */
1222         skb_put_data(skb, cmd, sizeof(cmd));
1223         hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1224
1225         skb_queue_tail(&qca->txq, skb);
1226         hci_uart_tx_wakeup(hu);
1227
1228         /* Wait for the baudrate change request to be sent */
1229
1230         while (!skb_queue_empty(&qca->txq))
1231                 usleep_range(100, 200);
1232
1233         if (hu->serdev)
1234                 serdev_device_wait_until_sent(hu->serdev,
1235                       msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1236
1237         /* Give the controller time to process the request */
1238         if (qca_is_wcn399x(qca_soc_type(hu)))
1239                 msleep(10);
1240         else
1241                 msleep(300);
1242
1243         return 0;
1244 }
1245
1246 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1247 {
1248         if (hu->serdev)
1249                 serdev_device_set_baudrate(hu->serdev, speed);
1250         else
1251                 hci_uart_set_baudrate(hu, speed);
1252 }
1253
1254 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1255 {
1256         int ret;
1257         int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1258         u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1259
1260         /* These power pulses are single byte command which are sent
1261          * at required baudrate to wcn3990. On wcn3990, we have an external
1262          * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1263          * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1264          * and also we use the same power inputs to turn on and off for
1265          * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1266          * we send a power on pulse at 115200 bps. This algorithm will help to
1267          * save power. Disabling hardware flow control is mandatory while
1268          * sending power pulses to SoC.
1269          */
1270         bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1271
1272         serdev_device_write_flush(hu->serdev);
1273         hci_uart_set_flow_control(hu, true);
1274         ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1275         if (ret < 0) {
1276                 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1277                 return ret;
1278         }
1279
1280         serdev_device_wait_until_sent(hu->serdev, timeout);
1281         hci_uart_set_flow_control(hu, false);
1282
1283         /* Give to controller time to boot/shutdown */
1284         if (on)
1285                 msleep(100);
1286         else
1287                 msleep(10);
1288
1289         return 0;
1290 }
1291
1292 static unsigned int qca_get_speed(struct hci_uart *hu,
1293                                   enum qca_speed_type speed_type)
1294 {
1295         unsigned int speed = 0;
1296
1297         if (speed_type == QCA_INIT_SPEED) {
1298                 if (hu->init_speed)
1299                         speed = hu->init_speed;
1300                 else if (hu->proto->init_speed)
1301                         speed = hu->proto->init_speed;
1302         } else {
1303                 if (hu->oper_speed)
1304                         speed = hu->oper_speed;
1305                 else if (hu->proto->oper_speed)
1306                         speed = hu->proto->oper_speed;
1307         }
1308
1309         return speed;
1310 }
1311
1312 static int qca_check_speeds(struct hci_uart *hu)
1313 {
1314         if (qca_is_wcn399x(qca_soc_type(hu))) {
1315                 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1316                     !qca_get_speed(hu, QCA_OPER_SPEED))
1317                         return -EINVAL;
1318         } else {
1319                 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1320                     !qca_get_speed(hu, QCA_OPER_SPEED))
1321                         return -EINVAL;
1322         }
1323
1324         return 0;
1325 }
1326
1327 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1328 {
1329         unsigned int speed, qca_baudrate;
1330         struct qca_data *qca = hu->priv;
1331         int ret = 0;
1332
1333         if (speed_type == QCA_INIT_SPEED) {
1334                 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1335                 if (speed)
1336                         host_set_baudrate(hu, speed);
1337         } else {
1338                 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1339
1340                 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1341                 if (!speed)
1342                         return 0;
1343
1344                 /* Disable flow control for wcn3990 to deassert RTS while
1345                  * changing the baudrate of chip and host.
1346                  */
1347                 if (qca_is_wcn399x(soc_type))
1348                         hci_uart_set_flow_control(hu, true);
1349
1350                 if (soc_type == QCA_WCN3990) {
1351                         reinit_completion(&qca->drop_ev_comp);
1352                         set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1353                 }
1354
1355                 qca_baudrate = qca_get_baudrate_value(speed);
1356                 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1357                 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1358                 if (ret)
1359                         goto error;
1360
1361                 host_set_baudrate(hu, speed);
1362
1363 error:
1364                 if (qca_is_wcn399x(soc_type))
1365                         hci_uart_set_flow_control(hu, false);
1366
1367                 if (soc_type == QCA_WCN3990) {
1368                         /* Wait for the controller to send the vendor event
1369                          * for the baudrate change command.
1370                          */
1371                         if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1372                                                  msecs_to_jiffies(100))) {
1373                                 bt_dev_err(hu->hdev,
1374                                            "Failed to change controller baudrate\n");
1375                                 ret = -ETIMEDOUT;
1376                         }
1377
1378                         clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1379                 }
1380         }
1381
1382         return ret;
1383 }
1384
1385 static int qca_send_crashbuffer(struct hci_uart *hu)
1386 {
1387         struct qca_data *qca = hu->priv;
1388         struct sk_buff *skb;
1389
1390         skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL);
1391         if (!skb) {
1392                 bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet");
1393                 return -ENOMEM;
1394         }
1395
1396         /* We forcefully crash the controller, by sending 0xfb byte for
1397          * 1024 times. We also might have chance of losing data, To be
1398          * on safer side we send 1096 bytes to the SoC.
1399          */
1400         memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE,
1401                QCA_CRASHBYTE_PACKET_LEN);
1402         hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1403         bt_dev_info(hu->hdev, "crash the soc to collect controller dump");
1404         skb_queue_tail(&qca->txq, skb);
1405         hci_uart_tx_wakeup(hu);
1406
1407         return 0;
1408 }
1409
1410 static void qca_wait_for_dump_collection(struct hci_dev *hdev)
1411 {
1412         struct hci_uart *hu = hci_get_drvdata(hdev);
1413         struct qca_data *qca = hu->priv;
1414         struct qca_memdump_data *qca_memdump = qca->qca_memdump;
1415         char *memdump_buf = NULL;
1416
1417         wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION,
1418                             TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS);
1419
1420         clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1421         if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1422                 bt_dev_err(hu->hdev, "Clearing the buffers due to timeout");
1423                 if (qca_memdump)
1424                         memdump_buf = qca_memdump->memdump_buf_tail;
1425                 vfree(memdump_buf);
1426                 kfree(qca_memdump);
1427                 qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1428                 del_timer(&qca->memdump_timer);
1429                 cancel_work_sync(&qca->ctrl_memdump_evt);
1430         }
1431 }
1432
1433 static void qca_hw_error(struct hci_dev *hdev, u8 code)
1434 {
1435         struct hci_uart *hu = hci_get_drvdata(hdev);
1436         struct qca_data *qca = hu->priv;
1437
1438         bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state);
1439
1440         if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1441                 /* If hardware error event received for other than QCA
1442                  * soc memory dump event, then we need to crash the SOC
1443                  * and wait here for 8 seconds to get the dump packets.
1444                  * This will block main thread to be on hold until we
1445                  * collect dump.
1446                  */
1447                 set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1448                 qca_send_crashbuffer(hu);
1449                 qca_wait_for_dump_collection(hdev);
1450         } else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1451                 /* Let us wait here until memory dump collected or
1452                  * memory dump timer expired.
1453                  */
1454                 bt_dev_info(hdev, "waiting for dump to complete");
1455                 qca_wait_for_dump_collection(hdev);
1456         }
1457 }
1458
1459 static void qca_cmd_timeout(struct hci_dev *hdev)
1460 {
1461         struct hci_uart *hu = hci_get_drvdata(hdev);
1462         struct qca_data *qca = hu->priv;
1463
1464         if (qca->memdump_state == QCA_MEMDUMP_IDLE)
1465                 qca_send_crashbuffer(hu);
1466         else
1467                 bt_dev_info(hdev, "Dump collection is in process");
1468 }
1469
1470 static int qca_wcn3990_init(struct hci_uart *hu)
1471 {
1472         struct qca_serdev *qcadev;
1473         int ret;
1474
1475         /* Check for vregs status, may be hci down has turned
1476          * off the voltage regulator.
1477          */
1478         qcadev = serdev_device_get_drvdata(hu->serdev);
1479         if (!qcadev->bt_power->vregs_on) {
1480                 serdev_device_close(hu->serdev);
1481                 ret = qca_regulator_enable(qcadev);
1482                 if (ret)
1483                         return ret;
1484
1485                 ret = serdev_device_open(hu->serdev);
1486                 if (ret) {
1487                         bt_dev_err(hu->hdev, "failed to open port");
1488                         return ret;
1489                 }
1490         }
1491
1492         /* Forcefully enable wcn3990 to enter in to boot mode. */
1493         host_set_baudrate(hu, 2400);
1494         ret = qca_send_power_pulse(hu, false);
1495         if (ret)
1496                 return ret;
1497
1498         qca_set_speed(hu, QCA_INIT_SPEED);
1499         ret = qca_send_power_pulse(hu, true);
1500         if (ret)
1501                 return ret;
1502
1503         /* Now the device is in ready state to communicate with host.
1504          * To sync host with device we need to reopen port.
1505          * Without this, we will have RTS and CTS synchronization
1506          * issues.
1507          */
1508         serdev_device_close(hu->serdev);
1509         ret = serdev_device_open(hu->serdev);
1510         if (ret) {
1511                 bt_dev_err(hu->hdev, "failed to open port");
1512                 return ret;
1513         }
1514
1515         hci_uart_set_flow_control(hu, false);
1516
1517         return 0;
1518 }
1519
1520 static int qca_power_on(struct hci_dev *hdev)
1521 {
1522         struct hci_uart *hu = hci_get_drvdata(hdev);
1523         enum qca_btsoc_type soc_type = qca_soc_type(hu);
1524         struct qca_serdev *qcadev;
1525         int ret = 0;
1526
1527         /* Non-serdev device usually is powered by external power
1528          * and don't need additional action in driver for power on
1529          */
1530         if (!hu->serdev)
1531                 return 0;
1532
1533         if (qca_is_wcn399x(soc_type)) {
1534                 ret = qca_wcn3990_init(hu);
1535         } else {
1536                 qcadev = serdev_device_get_drvdata(hu->serdev);
1537                 gpiod_set_value_cansleep(qcadev->bt_en, 1);
1538                 /* Controller needs time to bootup. */
1539                 msleep(150);
1540         }
1541
1542         return ret;
1543 }
1544
1545 static int qca_setup(struct hci_uart *hu)
1546 {
1547         struct hci_dev *hdev = hu->hdev;
1548         struct qca_data *qca = hu->priv;
1549         unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1550         enum qca_btsoc_type soc_type = qca_soc_type(hu);
1551         const char *firmware_name = qca_get_firmware_name(hu);
1552         int ret;
1553         int soc_ver = 0;
1554
1555         ret = qca_check_speeds(hu);
1556         if (ret)
1557                 return ret;
1558
1559         /* Patch downloading has to be done without IBS mode */
1560         clear_bit(QCA_IBS_ENABLED, &qca->flags);
1561
1562         /* Enable controller to do both LE scan and BR/EDR inquiry
1563          * simultaneously.
1564          */
1565         set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1566
1567         bt_dev_info(hdev, "setting up %s",
1568                 qca_is_wcn399x(soc_type) ? "wcn399x" : "ROME");
1569
1570         ret = qca_power_on(hdev);
1571         if (ret)
1572                 return ret;
1573
1574         if (qca_is_wcn399x(soc_type)) {
1575                 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1576                  * setup for every hci up.
1577                  */
1578                 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1579                 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1580                 hu->hdev->shutdown = qca_power_off;
1581
1582                 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
1583                 if (ret)
1584                         return ret;
1585         } else {
1586                 qca_set_speed(hu, QCA_INIT_SPEED);
1587         }
1588
1589         /* Setup user speed if needed */
1590         speed = qca_get_speed(hu, QCA_OPER_SPEED);
1591         if (speed) {
1592                 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1593                 if (ret)
1594                         return ret;
1595
1596                 qca_baudrate = qca_get_baudrate_value(speed);
1597         }
1598
1599         if (!qca_is_wcn399x(soc_type)) {
1600                 /* Get QCA version information */
1601                 ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
1602                 if (ret)
1603                         return ret;
1604         }
1605
1606         bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1607         /* Setup patch / NVM configurations */
1608         ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1609                         firmware_name);
1610         if (!ret) {
1611                 set_bit(QCA_IBS_ENABLED, &qca->flags);
1612                 qca_debugfs_init(hdev);
1613                 hu->hdev->hw_error = qca_hw_error;
1614                 hu->hdev->cmd_timeout = qca_cmd_timeout;
1615         } else if (ret == -ENOENT) {
1616                 /* No patch/nvm-config found, run with original fw/config */
1617                 ret = 0;
1618         } else if (ret == -EAGAIN) {
1619                 /*
1620                  * Userspace firmware loader will return -EAGAIN in case no
1621                  * patch/nvm-config is found, so run with original fw/config.
1622                  */
1623                 ret = 0;
1624         }
1625
1626         /* Setup bdaddr */
1627         if (qca_is_wcn399x(soc_type))
1628                 hu->hdev->set_bdaddr = qca_set_bdaddr;
1629         else
1630                 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1631
1632         return ret;
1633 }
1634
1635 static const struct hci_uart_proto qca_proto = {
1636         .id             = HCI_UART_QCA,
1637         .name           = "QCA",
1638         .manufacturer   = 29,
1639         .init_speed     = 115200,
1640         .oper_speed     = 3000000,
1641         .open           = qca_open,
1642         .close          = qca_close,
1643         .flush          = qca_flush,
1644         .setup          = qca_setup,
1645         .recv           = qca_recv,
1646         .enqueue        = qca_enqueue,
1647         .dequeue        = qca_dequeue,
1648 };
1649
1650 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1651         .soc_type = QCA_WCN3990,
1652         .vregs = (struct qca_vreg []) {
1653                 { "vddio", 15000  },
1654                 { "vddxo", 80000  },
1655                 { "vddrf", 300000 },
1656                 { "vddch0", 450000 },
1657         },
1658         .num_vregs = 4,
1659 };
1660
1661 static const struct qca_vreg_data qca_soc_data_wcn3991 = {
1662         .soc_type = QCA_WCN3991,
1663         .vregs = (struct qca_vreg []) {
1664                 { "vddio", 15000  },
1665                 { "vddxo", 80000  },
1666                 { "vddrf", 300000 },
1667                 { "vddch0", 450000 },
1668         },
1669         .num_vregs = 4,
1670 };
1671
1672 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1673         .soc_type = QCA_WCN3998,
1674         .vregs = (struct qca_vreg []) {
1675                 { "vddio", 10000  },
1676                 { "vddxo", 80000  },
1677                 { "vddrf", 300000 },
1678                 { "vddch0", 450000 },
1679         },
1680         .num_vregs = 4,
1681 };
1682
1683 static void qca_power_shutdown(struct hci_uart *hu)
1684 {
1685         struct qca_serdev *qcadev;
1686         struct qca_data *qca = hu->priv;
1687         unsigned long flags;
1688
1689         qcadev = serdev_device_get_drvdata(hu->serdev);
1690
1691         /* From this point we go into power off state. But serial port is
1692          * still open, stop queueing the IBS data and flush all the buffered
1693          * data in skb's.
1694          */
1695         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1696         clear_bit(QCA_IBS_ENABLED, &qca->flags);
1697         qca_flush(hu);
1698         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1699
1700         host_set_baudrate(hu, 2400);
1701         qca_send_power_pulse(hu, false);
1702         qca_regulator_disable(qcadev);
1703         hu->hdev->hw_error = NULL;
1704         hu->hdev->cmd_timeout = NULL;
1705 }
1706
1707 static int qca_power_off(struct hci_dev *hdev)
1708 {
1709         struct hci_uart *hu = hci_get_drvdata(hdev);
1710         struct qca_data *qca = hu->priv;
1711
1712         /* Stop sending shutdown command if soc crashes. */
1713         if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1714                 qca_send_pre_shutdown_cmd(hdev);
1715                 usleep_range(8000, 10000);
1716         }
1717
1718         qca->memdump_state = QCA_MEMDUMP_IDLE;
1719         qca_power_shutdown(hu);
1720         return 0;
1721 }
1722
1723 static int qca_regulator_enable(struct qca_serdev *qcadev)
1724 {
1725         struct qca_power *power = qcadev->bt_power;
1726         int ret;
1727
1728         /* Already enabled */
1729         if (power->vregs_on)
1730                 return 0;
1731
1732         BT_DBG("enabling %d regulators)", power->num_vregs);
1733
1734         ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1735         if (ret)
1736                 return ret;
1737
1738         power->vregs_on = true;
1739
1740         return 0;
1741 }
1742
1743 static void qca_regulator_disable(struct qca_serdev *qcadev)
1744 {
1745         struct qca_power *power;
1746
1747         if (!qcadev)
1748                 return;
1749
1750         power = qcadev->bt_power;
1751
1752         /* Already disabled? */
1753         if (!power->vregs_on)
1754                 return;
1755
1756         regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1757         power->vregs_on = false;
1758 }
1759
1760 static int qca_init_regulators(struct qca_power *qca,
1761                                 const struct qca_vreg *vregs, size_t num_vregs)
1762 {
1763         struct regulator_bulk_data *bulk;
1764         int ret;
1765         int i;
1766
1767         bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1768         if (!bulk)
1769                 return -ENOMEM;
1770
1771         for (i = 0; i < num_vregs; i++)
1772                 bulk[i].supply = vregs[i].name;
1773
1774         ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1775         if (ret < 0)
1776                 return ret;
1777
1778         for (i = 0; i < num_vregs; i++) {
1779                 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
1780                 if (ret)
1781                         return ret;
1782         }
1783
1784         qca->vreg_bulk = bulk;
1785         qca->num_vregs = num_vregs;
1786
1787         return 0;
1788 }
1789
1790 static int qca_serdev_probe(struct serdev_device *serdev)
1791 {
1792         struct qca_serdev *qcadev;
1793         const struct qca_vreg_data *data;
1794         int err;
1795
1796         qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1797         if (!qcadev)
1798                 return -ENOMEM;
1799
1800         qcadev->serdev_hu.serdev = serdev;
1801         data = device_get_match_data(&serdev->dev);
1802         serdev_device_set_drvdata(serdev, qcadev);
1803         device_property_read_string(&serdev->dev, "firmware-name",
1804                                          &qcadev->firmware_name);
1805         if (data && qca_is_wcn399x(data->soc_type)) {
1806                 qcadev->btsoc_type = data->soc_type;
1807                 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1808                                                 sizeof(struct qca_power),
1809                                                 GFP_KERNEL);
1810                 if (!qcadev->bt_power)
1811                         return -ENOMEM;
1812
1813                 qcadev->bt_power->dev = &serdev->dev;
1814                 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1815                                           data->num_vregs);
1816                 if (err) {
1817                         BT_ERR("Failed to init regulators:%d", err);
1818                         goto out;
1819                 }
1820
1821                 qcadev->bt_power->vregs_on = false;
1822
1823                 device_property_read_u32(&serdev->dev, "max-speed",
1824                                          &qcadev->oper_speed);
1825                 if (!qcadev->oper_speed)
1826                         BT_DBG("UART will pick default operating speed");
1827
1828                 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1829                 if (err) {
1830                         BT_ERR("wcn3990 serdev registration failed");
1831                         goto out;
1832                 }
1833         } else {
1834                 qcadev->btsoc_type = QCA_ROME;
1835                 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1836                                                GPIOD_OUT_LOW);
1837                 if (IS_ERR(qcadev->bt_en)) {
1838                         dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1839                         return PTR_ERR(qcadev->bt_en);
1840                 }
1841
1842                 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1843                 if (IS_ERR(qcadev->susclk)) {
1844                         dev_err(&serdev->dev, "failed to acquire clk\n");
1845                         return PTR_ERR(qcadev->susclk);
1846                 }
1847
1848                 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1849                 if (err)
1850                         return err;
1851
1852                 err = clk_prepare_enable(qcadev->susclk);
1853                 if (err)
1854                         return err;
1855
1856                 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1857                 if (err)
1858                         clk_disable_unprepare(qcadev->susclk);
1859         }
1860
1861 out:    return err;
1862
1863 }
1864
1865 static void qca_serdev_remove(struct serdev_device *serdev)
1866 {
1867         struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1868
1869         if (qca_is_wcn399x(qcadev->btsoc_type))
1870                 qca_power_shutdown(&qcadev->serdev_hu);
1871         else
1872                 clk_disable_unprepare(qcadev->susclk);
1873
1874         hci_uart_unregister_device(&qcadev->serdev_hu);
1875 }
1876
1877 static int __maybe_unused qca_suspend(struct device *dev)
1878 {
1879         struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1880         struct hci_uart *hu = hci_get_drvdata(hdev);
1881         struct qca_data *qca = hu->priv;
1882         unsigned long flags;
1883         int ret = 0;
1884         u8 cmd;
1885
1886         set_bit(QCA_SUSPENDING, &qca->flags);
1887
1888         /* Device is downloading patch or doesn't support in-band sleep. */
1889         if (!test_bit(QCA_IBS_ENABLED, &qca->flags))
1890                 return 0;
1891
1892         cancel_work_sync(&qca->ws_awake_device);
1893         cancel_work_sync(&qca->ws_awake_rx);
1894
1895         spin_lock_irqsave_nested(&qca->hci_ibs_lock,
1896                                  flags, SINGLE_DEPTH_NESTING);
1897
1898         switch (qca->tx_ibs_state) {
1899         case HCI_IBS_TX_WAKING:
1900                 del_timer(&qca->wake_retrans_timer);
1901                 /* Fall through */
1902         case HCI_IBS_TX_AWAKE:
1903                 del_timer(&qca->tx_idle_timer);
1904
1905                 serdev_device_write_flush(hu->serdev);
1906                 cmd = HCI_IBS_SLEEP_IND;
1907                 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1908
1909                 if (ret < 0) {
1910                         BT_ERR("Failed to send SLEEP to device");
1911                         break;
1912                 }
1913
1914                 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
1915                 qca->ibs_sent_slps++;
1916
1917                 qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off);
1918                 break;
1919
1920         case HCI_IBS_TX_ASLEEP:
1921                 break;
1922
1923         default:
1924                 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
1925                 ret = -EINVAL;
1926                 break;
1927         }
1928
1929         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1930
1931         if (ret < 0)
1932                 goto error;
1933
1934         serdev_device_wait_until_sent(hu->serdev,
1935                                       msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1936
1937         /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
1938          * to sleep, so that the packet does not wake the system later.
1939          */
1940
1941         ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
1942                         qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
1943                         msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
1944
1945         if (ret > 0)
1946                 return 0;
1947
1948         if (ret == 0)
1949                 ret = -ETIMEDOUT;
1950
1951 error:
1952         clear_bit(QCA_SUSPENDING, &qca->flags);
1953
1954         return ret;
1955 }
1956
1957 static int __maybe_unused qca_resume(struct device *dev)
1958 {
1959         struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1960         struct hci_uart *hu = hci_get_drvdata(hdev);
1961         struct qca_data *qca = hu->priv;
1962
1963         clear_bit(QCA_SUSPENDING, &qca->flags);
1964
1965         return 0;
1966 }
1967
1968 static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
1969
1970 static const struct of_device_id qca_bluetooth_of_match[] = {
1971         { .compatible = "qcom,qca6174-bt" },
1972         { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1973         { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
1974         { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1975         { /* sentinel */ }
1976 };
1977 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1978
1979 static struct serdev_device_driver qca_serdev_driver = {
1980         .probe = qca_serdev_probe,
1981         .remove = qca_serdev_remove,
1982         .driver = {
1983                 .name = "hci_uart_qca",
1984                 .of_match_table = qca_bluetooth_of_match,
1985                 .pm = &qca_pm_ops,
1986         },
1987 };
1988
1989 int __init qca_init(void)
1990 {
1991         serdev_device_driver_register(&qca_serdev_driver);
1992
1993         return hci_uart_register_proto(&qca_proto);
1994 }
1995
1996 int __exit qca_deinit(void)
1997 {
1998         serdev_device_driver_unregister(&qca_serdev_driver);
1999
2000         return hci_uart_unregister_proto(&qca_proto);
2001 }