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