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