Bluetooth: hci_qca: wcn3990: Drop baudrate change vendor event
[linux-2.6-microblaze.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/device.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/mod_devicetable.h>
26 #include <linux/module.h>
27 #include <linux/of_device.h>
28 #include <linux/platform_device.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/serdev.h>
31 #include <asm/unaligned.h>
32
33 #include <net/bluetooth/bluetooth.h>
34 #include <net/bluetooth/hci_core.h>
35
36 #include "hci_uart.h"
37 #include "btqca.h"
38
39 /* HCI_IBS protocol messages */
40 #define HCI_IBS_SLEEP_IND       0xFE
41 #define HCI_IBS_WAKE_IND        0xFD
42 #define HCI_IBS_WAKE_ACK        0xFC
43 #define HCI_MAX_IBS_SIZE        10
44
45 #define IBS_WAKE_RETRANS_TIMEOUT_MS     100
46 #define IBS_TX_IDLE_TIMEOUT_MS          2000
47 #define CMD_TRANS_TIMEOUT_MS            100
48
49 /* susclk rate */
50 #define SUSCLK_RATE_32KHZ       32768
51
52 /* Controller debug log header */
53 #define QCA_DEBUG_HANDLE        0x2EDC
54
55 enum qca_flags {
56         QCA_IBS_ENABLED,
57         QCA_DROP_VENDOR_EVENT,
58 };
59
60 /* HCI_IBS transmit side sleep protocol states */
61 enum tx_ibs_states {
62         HCI_IBS_TX_ASLEEP,
63         HCI_IBS_TX_WAKING,
64         HCI_IBS_TX_AWAKE,
65 };
66
67 /* HCI_IBS receive side sleep protocol states */
68 enum rx_states {
69         HCI_IBS_RX_ASLEEP,
70         HCI_IBS_RX_AWAKE,
71 };
72
73 /* HCI_IBS transmit and receive side clock state vote */
74 enum hci_ibs_clock_state_vote {
75         HCI_IBS_VOTE_STATS_UPDATE,
76         HCI_IBS_TX_VOTE_CLOCK_ON,
77         HCI_IBS_TX_VOTE_CLOCK_OFF,
78         HCI_IBS_RX_VOTE_CLOCK_ON,
79         HCI_IBS_RX_VOTE_CLOCK_OFF,
80 };
81
82 struct qca_data {
83         struct hci_uart *hu;
84         struct sk_buff *rx_skb;
85         struct sk_buff_head txq;
86         struct sk_buff_head tx_wait_q;  /* HCI_IBS wait queue   */
87         spinlock_t hci_ibs_lock;        /* HCI_IBS state lock   */
88         u8 tx_ibs_state;        /* HCI_IBS transmit side power state*/
89         u8 rx_ibs_state;        /* HCI_IBS receive side power state */
90         bool tx_vote;           /* Clock must be on for TX */
91         bool rx_vote;           /* Clock must be on for RX */
92         struct timer_list tx_idle_timer;
93         u32 tx_idle_delay;
94         struct timer_list wake_retrans_timer;
95         u32 wake_retrans;
96         struct workqueue_struct *workqueue;
97         struct work_struct ws_awake_rx;
98         struct work_struct ws_awake_device;
99         struct work_struct ws_rx_vote_off;
100         struct work_struct ws_tx_vote_off;
101         unsigned long flags;
102         struct completion drop_ev_comp;
103
104         /* For debugging purpose */
105         u64 ibs_sent_wacks;
106         u64 ibs_sent_slps;
107         u64 ibs_sent_wakes;
108         u64 ibs_recv_wacks;
109         u64 ibs_recv_slps;
110         u64 ibs_recv_wakes;
111         u64 vote_last_jif;
112         u32 vote_on_ms;
113         u32 vote_off_ms;
114         u64 tx_votes_on;
115         u64 rx_votes_on;
116         u64 tx_votes_off;
117         u64 rx_votes_off;
118         u64 votes_on;
119         u64 votes_off;
120 };
121
122 enum qca_speed_type {
123         QCA_INIT_SPEED = 1,
124         QCA_OPER_SPEED
125 };
126
127 /*
128  * Voltage regulator information required for configuring the
129  * QCA Bluetooth chipset
130  */
131 struct qca_vreg {
132         const char *name;
133         unsigned int min_uV;
134         unsigned int max_uV;
135         unsigned int load_uA;
136 };
137
138 struct qca_vreg_data {
139         enum qca_btsoc_type soc_type;
140         struct qca_vreg *vregs;
141         size_t num_vregs;
142 };
143
144 /*
145  * Platform data for the QCA Bluetooth power driver.
146  */
147 struct qca_power {
148         struct device *dev;
149         const struct qca_vreg_data *vreg_data;
150         struct regulator_bulk_data *vreg_bulk;
151         bool vregs_on;
152 };
153
154 struct qca_serdev {
155         struct hci_uart  serdev_hu;
156         struct gpio_desc *bt_en;
157         struct clk       *susclk;
158         enum qca_btsoc_type btsoc_type;
159         struct qca_power *bt_power;
160         u32 init_speed;
161         u32 oper_speed;
162 };
163
164 static int qca_power_setup(struct hci_uart *hu, bool on);
165 static void qca_power_shutdown(struct hci_uart *hu);
166 static int qca_power_off(struct hci_dev *hdev);
167
168 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
169 {
170         enum qca_btsoc_type soc_type;
171
172         if (hu->serdev) {
173                 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
174
175                 soc_type = qsd->btsoc_type;
176         } else {
177                 soc_type = QCA_ROME;
178         }
179
180         return soc_type;
181 }
182
183 static void __serial_clock_on(struct tty_struct *tty)
184 {
185         /* TODO: Some chipset requires to enable UART clock on client
186          * side to save power consumption or manual work is required.
187          * Please put your code to control UART clock here if needed
188          */
189 }
190
191 static void __serial_clock_off(struct tty_struct *tty)
192 {
193         /* TODO: Some chipset requires to disable UART clock on client
194          * side to save power consumption or manual work is required.
195          * Please put your code to control UART clock off here if needed
196          */
197 }
198
199 /* serial_clock_vote needs to be called with the ibs lock held */
200 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
201 {
202         struct qca_data *qca = hu->priv;
203         unsigned int diff;
204
205         bool old_vote = (qca->tx_vote | qca->rx_vote);
206         bool new_vote;
207
208         switch (vote) {
209         case HCI_IBS_VOTE_STATS_UPDATE:
210                 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
211
212                 if (old_vote)
213                         qca->vote_off_ms += diff;
214                 else
215                         qca->vote_on_ms += diff;
216                 return;
217
218         case HCI_IBS_TX_VOTE_CLOCK_ON:
219                 qca->tx_vote = true;
220                 qca->tx_votes_on++;
221                 new_vote = true;
222                 break;
223
224         case HCI_IBS_RX_VOTE_CLOCK_ON:
225                 qca->rx_vote = true;
226                 qca->rx_votes_on++;
227                 new_vote = true;
228                 break;
229
230         case HCI_IBS_TX_VOTE_CLOCK_OFF:
231                 qca->tx_vote = false;
232                 qca->tx_votes_off++;
233                 new_vote = qca->rx_vote | qca->tx_vote;
234                 break;
235
236         case HCI_IBS_RX_VOTE_CLOCK_OFF:
237                 qca->rx_vote = false;
238                 qca->rx_votes_off++;
239                 new_vote = qca->rx_vote | qca->tx_vote;
240                 break;
241
242         default:
243                 BT_ERR("Voting irregularity");
244                 return;
245         }
246
247         if (new_vote != old_vote) {
248                 if (new_vote)
249                         __serial_clock_on(hu->tty);
250                 else
251                         __serial_clock_off(hu->tty);
252
253                 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
254                        vote ? "true" : "false");
255
256                 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
257
258                 if (new_vote) {
259                         qca->votes_on++;
260                         qca->vote_off_ms += diff;
261                 } else {
262                         qca->votes_off++;
263                         qca->vote_on_ms += diff;
264                 }
265                 qca->vote_last_jif = jiffies;
266         }
267 }
268
269 /* Builds and sends an HCI_IBS command packet.
270  * These are very simple packets with only 1 cmd byte.
271  */
272 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
273 {
274         int err = 0;
275         struct sk_buff *skb = NULL;
276         struct qca_data *qca = hu->priv;
277
278         BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
279
280         skb = bt_skb_alloc(1, GFP_ATOMIC);
281         if (!skb) {
282                 BT_ERR("Failed to allocate memory for HCI_IBS packet");
283                 return -ENOMEM;
284         }
285
286         /* Assign HCI_IBS type */
287         skb_put_u8(skb, cmd);
288
289         skb_queue_tail(&qca->txq, skb);
290
291         return err;
292 }
293
294 static void qca_wq_awake_device(struct work_struct *work)
295 {
296         struct qca_data *qca = container_of(work, struct qca_data,
297                                             ws_awake_device);
298         struct hci_uart *hu = qca->hu;
299         unsigned long retrans_delay;
300
301         BT_DBG("hu %p wq awake device", hu);
302
303         /* Vote for serial clock */
304         serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
305
306         spin_lock(&qca->hci_ibs_lock);
307
308         /* Send wake indication to device */
309         if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
310                 BT_ERR("Failed to send WAKE to device");
311
312         qca->ibs_sent_wakes++;
313
314         /* Start retransmit timer */
315         retrans_delay = msecs_to_jiffies(qca->wake_retrans);
316         mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
317
318         spin_unlock(&qca->hci_ibs_lock);
319
320         /* Actually send the packets */
321         hci_uart_tx_wakeup(hu);
322 }
323
324 static void qca_wq_awake_rx(struct work_struct *work)
325 {
326         struct qca_data *qca = container_of(work, struct qca_data,
327                                             ws_awake_rx);
328         struct hci_uart *hu = qca->hu;
329
330         BT_DBG("hu %p wq awake rx", hu);
331
332         serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
333
334         spin_lock(&qca->hci_ibs_lock);
335         qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
336
337         /* Always acknowledge device wake up,
338          * sending IBS message doesn't count as TX ON.
339          */
340         if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
341                 BT_ERR("Failed to acknowledge device wake up");
342
343         qca->ibs_sent_wacks++;
344
345         spin_unlock(&qca->hci_ibs_lock);
346
347         /* Actually send the packets */
348         hci_uart_tx_wakeup(hu);
349 }
350
351 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
352 {
353         struct qca_data *qca = container_of(work, struct qca_data,
354                                             ws_rx_vote_off);
355         struct hci_uart *hu = qca->hu;
356
357         BT_DBG("hu %p rx clock vote off", hu);
358
359         serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
360 }
361
362 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
363 {
364         struct qca_data *qca = container_of(work, struct qca_data,
365                                             ws_tx_vote_off);
366         struct hci_uart *hu = qca->hu;
367
368         BT_DBG("hu %p tx clock vote off", hu);
369
370         /* Run HCI tx handling unlocked */
371         hci_uart_tx_wakeup(hu);
372
373         /* Now that message queued to tty driver, vote for tty clocks off.
374          * It is up to the tty driver to pend the clocks off until tx done.
375          */
376         serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
377 }
378
379 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
380 {
381         struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
382         struct hci_uart *hu = qca->hu;
383         unsigned long flags;
384
385         BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
386
387         spin_lock_irqsave_nested(&qca->hci_ibs_lock,
388                                  flags, SINGLE_DEPTH_NESTING);
389
390         switch (qca->tx_ibs_state) {
391         case HCI_IBS_TX_AWAKE:
392                 /* TX_IDLE, go to SLEEP */
393                 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
394                         BT_ERR("Failed to send SLEEP to device");
395                         break;
396                 }
397                 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
398                 qca->ibs_sent_slps++;
399                 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
400                 break;
401
402         case HCI_IBS_TX_ASLEEP:
403         case HCI_IBS_TX_WAKING:
404                 /* Fall through */
405
406         default:
407                 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
408                 break;
409         }
410
411         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
412 }
413
414 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
415 {
416         struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
417         struct hci_uart *hu = qca->hu;
418         unsigned long flags, retrans_delay;
419         bool retransmit = false;
420
421         BT_DBG("hu %p wake retransmit timeout in %d state",
422                 hu, qca->tx_ibs_state);
423
424         spin_lock_irqsave_nested(&qca->hci_ibs_lock,
425                                  flags, SINGLE_DEPTH_NESTING);
426
427         switch (qca->tx_ibs_state) {
428         case HCI_IBS_TX_WAKING:
429                 /* No WAKE_ACK, retransmit WAKE */
430                 retransmit = true;
431                 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
432                         BT_ERR("Failed to acknowledge device wake up");
433                         break;
434                 }
435                 qca->ibs_sent_wakes++;
436                 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
437                 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
438                 break;
439
440         case HCI_IBS_TX_ASLEEP:
441         case HCI_IBS_TX_AWAKE:
442                 /* Fall through */
443
444         default:
445                 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
446                 break;
447         }
448
449         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
450
451         if (retransmit)
452                 hci_uart_tx_wakeup(hu);
453 }
454
455 /* Initialize protocol */
456 static int qca_open(struct hci_uart *hu)
457 {
458         struct qca_serdev *qcadev;
459         struct qca_data *qca;
460         int ret;
461
462         BT_DBG("hu %p qca_open", hu);
463
464         qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
465         if (!qca)
466                 return -ENOMEM;
467
468         skb_queue_head_init(&qca->txq);
469         skb_queue_head_init(&qca->tx_wait_q);
470         spin_lock_init(&qca->hci_ibs_lock);
471         qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
472         if (!qca->workqueue) {
473                 BT_ERR("QCA Workqueue not initialized properly");
474                 kfree(qca);
475                 return -ENOMEM;
476         }
477
478         INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
479         INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
480         INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
481         INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
482
483         qca->hu = hu;
484         init_completion(&qca->drop_ev_comp);
485
486         /* Assume we start with both sides asleep -- extra wakes OK */
487         qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
488         qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
489
490         /* clocks actually on, but we start votes off */
491         qca->tx_vote = false;
492         qca->rx_vote = false;
493         qca->flags = 0;
494
495         qca->ibs_sent_wacks = 0;
496         qca->ibs_sent_slps = 0;
497         qca->ibs_sent_wakes = 0;
498         qca->ibs_recv_wacks = 0;
499         qca->ibs_recv_slps = 0;
500         qca->ibs_recv_wakes = 0;
501         qca->vote_last_jif = jiffies;
502         qca->vote_on_ms = 0;
503         qca->vote_off_ms = 0;
504         qca->votes_on = 0;
505         qca->votes_off = 0;
506         qca->tx_votes_on = 0;
507         qca->tx_votes_off = 0;
508         qca->rx_votes_on = 0;
509         qca->rx_votes_off = 0;
510
511         hu->priv = qca;
512
513         if (hu->serdev) {
514
515                 qcadev = serdev_device_get_drvdata(hu->serdev);
516                 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
517                         gpiod_set_value_cansleep(qcadev->bt_en, 1);
518                         /* Controller needs time to bootup. */
519                         msleep(150);
520                 } else {
521                         hu->init_speed = qcadev->init_speed;
522                         hu->oper_speed = qcadev->oper_speed;
523                         ret = qca_power_setup(hu, true);
524                         if (ret) {
525                                 destroy_workqueue(qca->workqueue);
526                                 kfree_skb(qca->rx_skb);
527                                 hu->priv = NULL;
528                                 kfree(qca);
529                                 return ret;
530                         }
531                 }
532         }
533
534         timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
535         qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
536
537         timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
538         qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
539
540         BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
541                qca->tx_idle_delay, qca->wake_retrans);
542
543         return 0;
544 }
545
546 static void qca_debugfs_init(struct hci_dev *hdev)
547 {
548         struct hci_uart *hu = hci_get_drvdata(hdev);
549         struct qca_data *qca = hu->priv;
550         struct dentry *ibs_dir;
551         umode_t mode;
552
553         if (!hdev->debugfs)
554                 return;
555
556         ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
557
558         /* read only */
559         mode = S_IRUGO;
560         debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
561         debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
562         debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
563                            &qca->ibs_sent_slps);
564         debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
565                            &qca->ibs_sent_wakes);
566         debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
567                            &qca->ibs_sent_wacks);
568         debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
569                            &qca->ibs_recv_slps);
570         debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
571                            &qca->ibs_recv_wakes);
572         debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
573                            &qca->ibs_recv_wacks);
574         debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
575         debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
576         debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
577         debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
578         debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
579         debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
580         debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
581         debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
582         debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
583         debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
584
585         /* read/write */
586         mode = S_IRUGO | S_IWUSR;
587         debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
588         debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
589                            &qca->tx_idle_delay);
590 }
591
592 /* Flush protocol data */
593 static int qca_flush(struct hci_uart *hu)
594 {
595         struct qca_data *qca = hu->priv;
596
597         BT_DBG("hu %p qca flush", hu);
598
599         skb_queue_purge(&qca->tx_wait_q);
600         skb_queue_purge(&qca->txq);
601
602         return 0;
603 }
604
605 /* Close protocol */
606 static int qca_close(struct hci_uart *hu)
607 {
608         struct qca_serdev *qcadev;
609         struct qca_data *qca = hu->priv;
610
611         BT_DBG("hu %p qca close", hu);
612
613         serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
614
615         skb_queue_purge(&qca->tx_wait_q);
616         skb_queue_purge(&qca->txq);
617         del_timer(&qca->tx_idle_timer);
618         del_timer(&qca->wake_retrans_timer);
619         destroy_workqueue(qca->workqueue);
620         qca->hu = NULL;
621
622         if (hu->serdev) {
623                 qcadev = serdev_device_get_drvdata(hu->serdev);
624                 if (qca_is_wcn399x(qcadev->btsoc_type))
625                         qca_power_shutdown(hu);
626                 else
627                         gpiod_set_value_cansleep(qcadev->bt_en, 0);
628
629         }
630
631         kfree_skb(qca->rx_skb);
632
633         hu->priv = NULL;
634
635         kfree(qca);
636
637         return 0;
638 }
639
640 /* Called upon a wake-up-indication from the device.
641  */
642 static void device_want_to_wakeup(struct hci_uart *hu)
643 {
644         unsigned long flags;
645         struct qca_data *qca = hu->priv;
646
647         BT_DBG("hu %p want to wake up", hu);
648
649         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
650
651         qca->ibs_recv_wakes++;
652
653         switch (qca->rx_ibs_state) {
654         case HCI_IBS_RX_ASLEEP:
655                 /* Make sure clock is on - we may have turned clock off since
656                  * receiving the wake up indicator awake rx clock.
657                  */
658                 queue_work(qca->workqueue, &qca->ws_awake_rx);
659                 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
660                 return;
661
662         case HCI_IBS_RX_AWAKE:
663                 /* Always acknowledge device wake up,
664                  * sending IBS message doesn't count as TX ON.
665                  */
666                 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
667                         BT_ERR("Failed to acknowledge device wake up");
668                         break;
669                 }
670                 qca->ibs_sent_wacks++;
671                 break;
672
673         default:
674                 /* Any other state is illegal */
675                 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
676                        qca->rx_ibs_state);
677                 break;
678         }
679
680         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
681
682         /* Actually send the packets */
683         hci_uart_tx_wakeup(hu);
684 }
685
686 /* Called upon a sleep-indication from the device.
687  */
688 static void device_want_to_sleep(struct hci_uart *hu)
689 {
690         unsigned long flags;
691         struct qca_data *qca = hu->priv;
692
693         BT_DBG("hu %p want to sleep", hu);
694
695         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
696
697         qca->ibs_recv_slps++;
698
699         switch (qca->rx_ibs_state) {
700         case HCI_IBS_RX_AWAKE:
701                 /* Update state */
702                 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
703                 /* Vote off rx clock under workqueue */
704                 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
705                 break;
706
707         case HCI_IBS_RX_ASLEEP:
708                 /* Fall through */
709
710         default:
711                 /* Any other state is illegal */
712                 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
713                        qca->rx_ibs_state);
714                 break;
715         }
716
717         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
718 }
719
720 /* Called upon wake-up-acknowledgement from the device
721  */
722 static void device_woke_up(struct hci_uart *hu)
723 {
724         unsigned long flags, idle_delay;
725         struct qca_data *qca = hu->priv;
726         struct sk_buff *skb = NULL;
727
728         BT_DBG("hu %p woke up", hu);
729
730         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
731
732         qca->ibs_recv_wacks++;
733
734         switch (qca->tx_ibs_state) {
735         case HCI_IBS_TX_AWAKE:
736                 /* Expect one if we send 2 WAKEs */
737                 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
738                        qca->tx_ibs_state);
739                 break;
740
741         case HCI_IBS_TX_WAKING:
742                 /* Send pending packets */
743                 while ((skb = skb_dequeue(&qca->tx_wait_q)))
744                         skb_queue_tail(&qca->txq, skb);
745
746                 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
747                 del_timer(&qca->wake_retrans_timer);
748                 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
749                 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
750                 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
751                 break;
752
753         case HCI_IBS_TX_ASLEEP:
754                 /* Fall through */
755
756         default:
757                 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
758                        qca->tx_ibs_state);
759                 break;
760         }
761
762         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
763
764         /* Actually send the packets */
765         hci_uart_tx_wakeup(hu);
766 }
767
768 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
769  * two simultaneous tasklets.
770  */
771 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
772 {
773         unsigned long flags = 0, idle_delay;
774         struct qca_data *qca = hu->priv;
775
776         BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
777                qca->tx_ibs_state);
778
779         /* Prepend skb with frame type */
780         memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
781
782         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
783
784         /* Don't go to sleep in middle of patch download or
785          * Out-Of-Band(GPIOs control) sleep is selected.
786          */
787         if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) {
788                 skb_queue_tail(&qca->txq, skb);
789                 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
790                 return 0;
791         }
792
793         /* Act according to current state */
794         switch (qca->tx_ibs_state) {
795         case HCI_IBS_TX_AWAKE:
796                 BT_DBG("Device awake, sending normally");
797                 skb_queue_tail(&qca->txq, skb);
798                 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
799                 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
800                 break;
801
802         case HCI_IBS_TX_ASLEEP:
803                 BT_DBG("Device asleep, waking up and queueing packet");
804                 /* Save packet for later */
805                 skb_queue_tail(&qca->tx_wait_q, skb);
806
807                 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
808                 /* Schedule a work queue to wake up device */
809                 queue_work(qca->workqueue, &qca->ws_awake_device);
810                 break;
811
812         case HCI_IBS_TX_WAKING:
813                 BT_DBG("Device waking up, queueing packet");
814                 /* Transient state; just keep packet for later */
815                 skb_queue_tail(&qca->tx_wait_q, skb);
816                 break;
817
818         default:
819                 BT_ERR("Illegal tx state: %d (losing packet)",
820                        qca->tx_ibs_state);
821                 kfree_skb(skb);
822                 break;
823         }
824
825         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
826
827         return 0;
828 }
829
830 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
831 {
832         struct hci_uart *hu = hci_get_drvdata(hdev);
833
834         BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
835
836         device_want_to_sleep(hu);
837
838         kfree_skb(skb);
839         return 0;
840 }
841
842 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
843 {
844         struct hci_uart *hu = hci_get_drvdata(hdev);
845
846         BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
847
848         device_want_to_wakeup(hu);
849
850         kfree_skb(skb);
851         return 0;
852 }
853
854 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
855 {
856         struct hci_uart *hu = hci_get_drvdata(hdev);
857
858         BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
859
860         device_woke_up(hu);
861
862         kfree_skb(skb);
863         return 0;
864 }
865
866 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
867 {
868         /* We receive debug logs from chip as an ACL packets.
869          * Instead of sending the data to ACL to decode the
870          * received data, we are pushing them to the above layers
871          * as a diagnostic packet.
872          */
873         if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
874                 return hci_recv_diag(hdev, skb);
875
876         return hci_recv_frame(hdev, skb);
877 }
878
879 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
880 {
881         struct hci_uart *hu = hci_get_drvdata(hdev);
882         struct qca_data *qca = hu->priv;
883
884         if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
885                 struct hci_event_hdr *hdr = (void *)skb->data;
886
887                 /* For the WCN3990 the vendor command for a baudrate change
888                  * isn't sent as synchronous HCI command, because the
889                  * controller sends the corresponding vendor event with the
890                  * new baudrate. The event is received and properly decoded
891                  * after changing the baudrate of the host port. It needs to
892                  * be dropped, otherwise it can be misinterpreted as
893                  * response to a later firmware download command (also a
894                  * vendor command).
895                  */
896
897                 if (hdr->evt == HCI_EV_VENDOR)
898                         complete(&qca->drop_ev_comp);
899
900                 kfree(skb);
901
902                 return 0;
903         }
904
905         return hci_recv_frame(hdev, skb);
906 }
907
908 #define QCA_IBS_SLEEP_IND_EVENT \
909         .type = HCI_IBS_SLEEP_IND, \
910         .hlen = 0, \
911         .loff = 0, \
912         .lsize = 0, \
913         .maxlen = HCI_MAX_IBS_SIZE
914
915 #define QCA_IBS_WAKE_IND_EVENT \
916         .type = HCI_IBS_WAKE_IND, \
917         .hlen = 0, \
918         .loff = 0, \
919         .lsize = 0, \
920         .maxlen = HCI_MAX_IBS_SIZE
921
922 #define QCA_IBS_WAKE_ACK_EVENT \
923         .type = HCI_IBS_WAKE_ACK, \
924         .hlen = 0, \
925         .loff = 0, \
926         .lsize = 0, \
927         .maxlen = HCI_MAX_IBS_SIZE
928
929 static const struct h4_recv_pkt qca_recv_pkts[] = {
930         { H4_RECV_ACL,             .recv = qca_recv_acl_data },
931         { H4_RECV_SCO,             .recv = hci_recv_frame    },
932         { H4_RECV_EVENT,           .recv = qca_recv_event    },
933         { QCA_IBS_WAKE_IND_EVENT,  .recv = qca_ibs_wake_ind  },
934         { QCA_IBS_WAKE_ACK_EVENT,  .recv = qca_ibs_wake_ack  },
935         { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
936 };
937
938 static int qca_recv(struct hci_uart *hu, const void *data, int count)
939 {
940         struct qca_data *qca = hu->priv;
941
942         if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
943                 return -EUNATCH;
944
945         qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
946                                   qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
947         if (IS_ERR(qca->rx_skb)) {
948                 int err = PTR_ERR(qca->rx_skb);
949                 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
950                 qca->rx_skb = NULL;
951                 return err;
952         }
953
954         return count;
955 }
956
957 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
958 {
959         struct qca_data *qca = hu->priv;
960
961         return skb_dequeue(&qca->txq);
962 }
963
964 static uint8_t qca_get_baudrate_value(int speed)
965 {
966         switch (speed) {
967         case 9600:
968                 return QCA_BAUDRATE_9600;
969         case 19200:
970                 return QCA_BAUDRATE_19200;
971         case 38400:
972                 return QCA_BAUDRATE_38400;
973         case 57600:
974                 return QCA_BAUDRATE_57600;
975         case 115200:
976                 return QCA_BAUDRATE_115200;
977         case 230400:
978                 return QCA_BAUDRATE_230400;
979         case 460800:
980                 return QCA_BAUDRATE_460800;
981         case 500000:
982                 return QCA_BAUDRATE_500000;
983         case 921600:
984                 return QCA_BAUDRATE_921600;
985         case 1000000:
986                 return QCA_BAUDRATE_1000000;
987         case 2000000:
988                 return QCA_BAUDRATE_2000000;
989         case 3000000:
990                 return QCA_BAUDRATE_3000000;
991         case 3200000:
992                 return QCA_BAUDRATE_3200000;
993         case 3500000:
994                 return QCA_BAUDRATE_3500000;
995         default:
996                 return QCA_BAUDRATE_115200;
997         }
998 }
999
1000 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1001 {
1002         struct hci_uart *hu = hci_get_drvdata(hdev);
1003         struct qca_data *qca = hu->priv;
1004         struct sk_buff *skb;
1005         u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1006
1007         if (baudrate > QCA_BAUDRATE_3200000)
1008                 return -EINVAL;
1009
1010         cmd[4] = baudrate;
1011
1012         skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1013         if (!skb) {
1014                 bt_dev_err(hdev, "Failed to allocate baudrate packet");
1015                 return -ENOMEM;
1016         }
1017
1018         /* Assign commands to change baudrate and packet type. */
1019         skb_put_data(skb, cmd, sizeof(cmd));
1020         hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1021
1022         skb_queue_tail(&qca->txq, skb);
1023         hci_uart_tx_wakeup(hu);
1024
1025         /* Wait for the baudrate change request to be sent */
1026
1027         while (!skb_queue_empty(&qca->txq))
1028                 usleep_range(100, 200);
1029
1030         if (hu->serdev)
1031                 serdev_device_wait_until_sent(hu->serdev,
1032                       msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1033
1034         /* Give the controller time to process the request */
1035         if (qca_is_wcn399x(qca_soc_type(hu)))
1036                 msleep(10);
1037         else
1038                 msleep(300);
1039
1040         return 0;
1041 }
1042
1043 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1044 {
1045         if (hu->serdev)
1046                 serdev_device_set_baudrate(hu->serdev, speed);
1047         else
1048                 hci_uart_set_baudrate(hu, speed);
1049 }
1050
1051 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1052 {
1053         int ret;
1054         int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1055         u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1056
1057         /* These power pulses are single byte command which are sent
1058          * at required baudrate to wcn3990. On wcn3990, we have an external
1059          * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1060          * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1061          * and also we use the same power inputs to turn on and off for
1062          * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1063          * we send a power on pulse at 115200 bps. This algorithm will help to
1064          * save power. Disabling hardware flow control is mandatory while
1065          * sending power pulses to SoC.
1066          */
1067         bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1068
1069         serdev_device_write_flush(hu->serdev);
1070         hci_uart_set_flow_control(hu, true);
1071         ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1072         if (ret < 0) {
1073                 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1074                 return ret;
1075         }
1076
1077         serdev_device_wait_until_sent(hu->serdev, timeout);
1078         hci_uart_set_flow_control(hu, false);
1079
1080         /* Give to controller time to boot/shutdown */
1081         if (on)
1082                 msleep(100);
1083         else
1084                 msleep(10);
1085
1086         return 0;
1087 }
1088
1089 static unsigned int qca_get_speed(struct hci_uart *hu,
1090                                   enum qca_speed_type speed_type)
1091 {
1092         unsigned int speed = 0;
1093
1094         if (speed_type == QCA_INIT_SPEED) {
1095                 if (hu->init_speed)
1096                         speed = hu->init_speed;
1097                 else if (hu->proto->init_speed)
1098                         speed = hu->proto->init_speed;
1099         } else {
1100                 if (hu->oper_speed)
1101                         speed = hu->oper_speed;
1102                 else if (hu->proto->oper_speed)
1103                         speed = hu->proto->oper_speed;
1104         }
1105
1106         return speed;
1107 }
1108
1109 static int qca_check_speeds(struct hci_uart *hu)
1110 {
1111         if (qca_is_wcn399x(qca_soc_type(hu))) {
1112                 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1113                     !qca_get_speed(hu, QCA_OPER_SPEED))
1114                         return -EINVAL;
1115         } else {
1116                 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1117                     !qca_get_speed(hu, QCA_OPER_SPEED))
1118                         return -EINVAL;
1119         }
1120
1121         return 0;
1122 }
1123
1124 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1125 {
1126         unsigned int speed, qca_baudrate;
1127         struct qca_data *qca = hu->priv;
1128         int ret = 0;
1129
1130         if (speed_type == QCA_INIT_SPEED) {
1131                 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1132                 if (speed)
1133                         host_set_baudrate(hu, speed);
1134         } else {
1135                 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1136
1137                 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1138                 if (!speed)
1139                         return 0;
1140
1141                 /* Disable flow control for wcn3990 to deassert RTS while
1142                  * changing the baudrate of chip and host.
1143                  */
1144                 if (qca_is_wcn399x(soc_type))
1145                         hci_uart_set_flow_control(hu, true);
1146
1147                 if (soc_type == QCA_WCN3990) {
1148                         reinit_completion(&qca->drop_ev_comp);
1149                         set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1150                 }
1151
1152                 qca_baudrate = qca_get_baudrate_value(speed);
1153                 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1154                 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1155                 if (ret)
1156                         goto error;
1157
1158                 host_set_baudrate(hu, speed);
1159
1160 error:
1161                 if (qca_is_wcn399x(soc_type))
1162                         hci_uart_set_flow_control(hu, false);
1163
1164                 if (soc_type == QCA_WCN3990) {
1165                         /* Wait for the controller to send the vendor event
1166                          * for the baudrate change command.
1167                          */
1168                         if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1169                                                  msecs_to_jiffies(100))) {
1170                                 bt_dev_err(hu->hdev,
1171                                            "Failed to change controller baudrate\n");
1172                                 ret = -ETIMEDOUT;
1173                         }
1174
1175                         clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1176                 }
1177         }
1178
1179         return ret;
1180 }
1181
1182 static int qca_wcn3990_init(struct hci_uart *hu)
1183 {
1184         struct qca_serdev *qcadev;
1185         int ret;
1186
1187         /* Check for vregs status, may be hci down has turned
1188          * off the voltage regulator.
1189          */
1190         qcadev = serdev_device_get_drvdata(hu->serdev);
1191         if (!qcadev->bt_power->vregs_on) {
1192                 serdev_device_close(hu->serdev);
1193                 ret = qca_power_setup(hu, true);
1194                 if (ret)
1195                         return ret;
1196
1197                 ret = serdev_device_open(hu->serdev);
1198                 if (ret) {
1199                         bt_dev_err(hu->hdev, "failed to open port");
1200                         return ret;
1201                 }
1202         }
1203
1204         /* Forcefully enable wcn3990 to enter in to boot mode. */
1205         host_set_baudrate(hu, 2400);
1206         ret = qca_send_power_pulse(hu, false);
1207         if (ret)
1208                 return ret;
1209
1210         qca_set_speed(hu, QCA_INIT_SPEED);
1211         ret = qca_send_power_pulse(hu, true);
1212         if (ret)
1213                 return ret;
1214
1215         /* Now the device is in ready state to communicate with host.
1216          * To sync host with device we need to reopen port.
1217          * Without this, we will have RTS and CTS synchronization
1218          * issues.
1219          */
1220         serdev_device_close(hu->serdev);
1221         ret = serdev_device_open(hu->serdev);
1222         if (ret) {
1223                 bt_dev_err(hu->hdev, "failed to open port");
1224                 return ret;
1225         }
1226
1227         hci_uart_set_flow_control(hu, false);
1228
1229         return 0;
1230 }
1231
1232 static int qca_setup(struct hci_uart *hu)
1233 {
1234         struct hci_dev *hdev = hu->hdev;
1235         struct qca_data *qca = hu->priv;
1236         unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1237         enum qca_btsoc_type soc_type = qca_soc_type(hu);
1238         int ret;
1239         int soc_ver = 0;
1240
1241         ret = qca_check_speeds(hu);
1242         if (ret)
1243                 return ret;
1244
1245         /* Patch downloading has to be done without IBS mode */
1246         clear_bit(QCA_IBS_ENABLED, &qca->flags);
1247
1248         if (qca_is_wcn399x(soc_type)) {
1249                 bt_dev_info(hdev, "setting up wcn3990");
1250
1251                 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1252                  * setup for every hci up.
1253                  */
1254                 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1255                 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1256                 hu->hdev->shutdown = qca_power_off;
1257                 ret = qca_wcn3990_init(hu);
1258                 if (ret)
1259                         return ret;
1260
1261                 ret = qca_read_soc_version(hdev, &soc_ver);
1262                 if (ret)
1263                         return ret;
1264         } else {
1265                 bt_dev_info(hdev, "ROME setup");
1266                 qca_set_speed(hu, QCA_INIT_SPEED);
1267         }
1268
1269         /* Setup user speed if needed */
1270         speed = qca_get_speed(hu, QCA_OPER_SPEED);
1271         if (speed) {
1272                 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1273                 if (ret)
1274                         return ret;
1275
1276                 qca_baudrate = qca_get_baudrate_value(speed);
1277         }
1278
1279         if (!qca_is_wcn399x(soc_type)) {
1280                 /* Get QCA version information */
1281                 ret = qca_read_soc_version(hdev, &soc_ver);
1282                 if (ret)
1283                         return ret;
1284         }
1285
1286         bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1287         /* Setup patch / NVM configurations */
1288         ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver);
1289         if (!ret) {
1290                 set_bit(QCA_IBS_ENABLED, &qca->flags);
1291                 qca_debugfs_init(hdev);
1292         } else if (ret == -ENOENT) {
1293                 /* No patch/nvm-config found, run with original fw/config */
1294                 ret = 0;
1295         } else if (ret == -EAGAIN) {
1296                 /*
1297                  * Userspace firmware loader will return -EAGAIN in case no
1298                  * patch/nvm-config is found, so run with original fw/config.
1299                  */
1300                 ret = 0;
1301         }
1302
1303         /* Setup bdaddr */
1304         if (qca_is_wcn399x(soc_type))
1305                 hu->hdev->set_bdaddr = qca_set_bdaddr;
1306         else
1307                 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1308
1309         return ret;
1310 }
1311
1312 static struct hci_uart_proto qca_proto = {
1313         .id             = HCI_UART_QCA,
1314         .name           = "QCA",
1315         .manufacturer   = 29,
1316         .init_speed     = 115200,
1317         .oper_speed     = 3000000,
1318         .open           = qca_open,
1319         .close          = qca_close,
1320         .flush          = qca_flush,
1321         .setup          = qca_setup,
1322         .recv           = qca_recv,
1323         .enqueue        = qca_enqueue,
1324         .dequeue        = qca_dequeue,
1325 };
1326
1327 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1328         .soc_type = QCA_WCN3990,
1329         .vregs = (struct qca_vreg []) {
1330                 { "vddio",   1800000, 1900000,  15000  },
1331                 { "vddxo",   1800000, 1900000,  80000  },
1332                 { "vddrf",   1300000, 1350000,  300000 },
1333                 { "vddch0",  3300000, 3400000,  450000 },
1334         },
1335         .num_vregs = 4,
1336 };
1337
1338 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1339         .soc_type = QCA_WCN3998,
1340         .vregs = (struct qca_vreg []) {
1341                 { "vddio",   1800000, 1900000,  10000  },
1342                 { "vddxo",   1800000, 1900000,  80000  },
1343                 { "vddrf",   1300000, 1352000,  300000 },
1344                 { "vddch0",  3300000, 3300000,  450000 },
1345         },
1346         .num_vregs = 4,
1347 };
1348
1349 static void qca_power_shutdown(struct hci_uart *hu)
1350 {
1351         struct qca_data *qca = hu->priv;
1352         unsigned long flags;
1353
1354         /* From this point we go into power off state. But serial port is
1355          * still open, stop queueing the IBS data and flush all the buffered
1356          * data in skb's.
1357          */
1358         spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1359         clear_bit(QCA_IBS_ENABLED, &qca->flags);
1360         qca_flush(hu);
1361         spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1362
1363         host_set_baudrate(hu, 2400);
1364         qca_send_power_pulse(hu, false);
1365         qca_power_setup(hu, false);
1366 }
1367
1368 static int qca_power_off(struct hci_dev *hdev)
1369 {
1370         struct hci_uart *hu = hci_get_drvdata(hdev);
1371
1372         qca_power_shutdown(hu);
1373         return 0;
1374 }
1375
1376 static int qca_enable_regulator(struct qca_vreg vregs,
1377                                 struct regulator *regulator)
1378 {
1379         int ret;
1380
1381         ret = regulator_set_voltage(regulator, vregs.min_uV,
1382                                     vregs.max_uV);
1383         if (ret)
1384                 return ret;
1385
1386         if (vregs.load_uA)
1387                 ret = regulator_set_load(regulator,
1388                                          vregs.load_uA);
1389
1390         if (ret)
1391                 return ret;
1392
1393         return regulator_enable(regulator);
1394
1395 }
1396
1397 static void qca_disable_regulator(struct qca_vreg vregs,
1398                                   struct regulator *regulator)
1399 {
1400         regulator_disable(regulator);
1401         regulator_set_voltage(regulator, 0, vregs.max_uV);
1402         if (vregs.load_uA)
1403                 regulator_set_load(regulator, 0);
1404
1405 }
1406
1407 static int qca_power_setup(struct hci_uart *hu, bool on)
1408 {
1409         struct qca_vreg *vregs;
1410         struct regulator_bulk_data *vreg_bulk;
1411         struct qca_serdev *qcadev;
1412         int i, num_vregs, ret = 0;
1413
1414         qcadev = serdev_device_get_drvdata(hu->serdev);
1415         if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1416             !qcadev->bt_power->vreg_bulk)
1417                 return -EINVAL;
1418
1419         vregs = qcadev->bt_power->vreg_data->vregs;
1420         vreg_bulk = qcadev->bt_power->vreg_bulk;
1421         num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1422         BT_DBG("on: %d", on);
1423         if (on && !qcadev->bt_power->vregs_on) {
1424                 for (i = 0; i < num_vregs; i++) {
1425                         ret = qca_enable_regulator(vregs[i],
1426                                                    vreg_bulk[i].consumer);
1427                         if (ret)
1428                                 break;
1429                 }
1430
1431                 if (ret) {
1432                         BT_ERR("failed to enable regulator:%s", vregs[i].name);
1433                         /* turn off regulators which are enabled */
1434                         for (i = i - 1; i >= 0; i--)
1435                                 qca_disable_regulator(vregs[i],
1436                                                       vreg_bulk[i].consumer);
1437                 } else {
1438                         qcadev->bt_power->vregs_on = true;
1439                 }
1440         } else if (!on && qcadev->bt_power->vregs_on) {
1441                 /* turn off regulator in reverse order */
1442                 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1443                 for ( ; i >= 0; i--)
1444                         qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1445
1446                 qcadev->bt_power->vregs_on = false;
1447         }
1448
1449         return ret;
1450 }
1451
1452 static int qca_init_regulators(struct qca_power *qca,
1453                                 const struct qca_vreg *vregs, size_t num_vregs)
1454 {
1455         int i;
1456
1457         qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
1458                                       sizeof(struct regulator_bulk_data),
1459                                       GFP_KERNEL);
1460         if (!qca->vreg_bulk)
1461                 return -ENOMEM;
1462
1463         for (i = 0; i < num_vregs; i++)
1464                 qca->vreg_bulk[i].supply = vregs[i].name;
1465
1466         return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1467 }
1468
1469 static int qca_serdev_probe(struct serdev_device *serdev)
1470 {
1471         struct qca_serdev *qcadev;
1472         const struct qca_vreg_data *data;
1473         int err;
1474
1475         qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1476         if (!qcadev)
1477                 return -ENOMEM;
1478
1479         qcadev->serdev_hu.serdev = serdev;
1480         data = of_device_get_match_data(&serdev->dev);
1481         serdev_device_set_drvdata(serdev, qcadev);
1482         if (data && qca_is_wcn399x(data->soc_type)) {
1483                 qcadev->btsoc_type = data->soc_type;
1484                 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1485                                                 sizeof(struct qca_power),
1486                                                 GFP_KERNEL);
1487                 if (!qcadev->bt_power)
1488                         return -ENOMEM;
1489
1490                 qcadev->bt_power->dev = &serdev->dev;
1491                 qcadev->bt_power->vreg_data = data;
1492                 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1493                                           data->num_vregs);
1494                 if (err) {
1495                         BT_ERR("Failed to init regulators:%d", err);
1496                         goto out;
1497                 }
1498
1499                 qcadev->bt_power->vregs_on = false;
1500
1501                 device_property_read_u32(&serdev->dev, "max-speed",
1502                                          &qcadev->oper_speed);
1503                 if (!qcadev->oper_speed)
1504                         BT_DBG("UART will pick default operating speed");
1505
1506                 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1507                 if (err) {
1508                         BT_ERR("wcn3990 serdev registration failed");
1509                         goto out;
1510                 }
1511         } else {
1512                 qcadev->btsoc_type = QCA_ROME;
1513                 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1514                                                GPIOD_OUT_LOW);
1515                 if (IS_ERR(qcadev->bt_en)) {
1516                         dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1517                         return PTR_ERR(qcadev->bt_en);
1518                 }
1519
1520                 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1521                 if (IS_ERR(qcadev->susclk)) {
1522                         dev_err(&serdev->dev, "failed to acquire clk\n");
1523                         return PTR_ERR(qcadev->susclk);
1524                 }
1525
1526                 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1527                 if (err)
1528                         return err;
1529
1530                 err = clk_prepare_enable(qcadev->susclk);
1531                 if (err)
1532                         return err;
1533
1534                 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1535                 if (err)
1536                         clk_disable_unprepare(qcadev->susclk);
1537         }
1538
1539 out:    return err;
1540
1541 }
1542
1543 static void qca_serdev_remove(struct serdev_device *serdev)
1544 {
1545         struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1546
1547         if (qca_is_wcn399x(qcadev->btsoc_type))
1548                 qca_power_shutdown(&qcadev->serdev_hu);
1549         else
1550                 clk_disable_unprepare(qcadev->susclk);
1551
1552         hci_uart_unregister_device(&qcadev->serdev_hu);
1553 }
1554
1555 static const struct of_device_id qca_bluetooth_of_match[] = {
1556         { .compatible = "qcom,qca6174-bt" },
1557         { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1558         { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
1559         { /* sentinel */ }
1560 };
1561 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1562
1563 static struct serdev_device_driver qca_serdev_driver = {
1564         .probe = qca_serdev_probe,
1565         .remove = qca_serdev_remove,
1566         .driver = {
1567                 .name = "hci_uart_qca",
1568                 .of_match_table = qca_bluetooth_of_match,
1569         },
1570 };
1571
1572 int __init qca_init(void)
1573 {
1574         serdev_device_driver_register(&qca_serdev_driver);
1575
1576         return hci_uart_register_proto(&qca_proto);
1577 }
1578
1579 int __exit qca_deinit(void)
1580 {
1581         serdev_device_driver_unregister(&qca_serdev_driver);
1582
1583         return hci_uart_unregister_proto(&qca_proto);
1584 }