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