]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/bluetooth/hci_qca.c
Merge tag 'for-4.20-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[mirror_ubuntu-eoan-kernel.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 BAUDRATE_SETTLE_TIMEOUT_MS 300
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 /* Don't go to sleep in middle of patch download or
774 * Out-Of-Band(GPIOs control) sleep is selected.
775 */
776 if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags)) {
777 skb_queue_tail(&qca->txq, skb);
778 return 0;
779 }
780
781 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
782
783 /* Act according to current state */
784 switch (qca->tx_ibs_state) {
785 case HCI_IBS_TX_AWAKE:
786 BT_DBG("Device awake, sending normally");
787 skb_queue_tail(&qca->txq, skb);
788 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
789 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
790 break;
791
792 case HCI_IBS_TX_ASLEEP:
793 BT_DBG("Device asleep, waking up and queueing packet");
794 /* Save packet for later */
795 skb_queue_tail(&qca->tx_wait_q, skb);
796
797 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
798 /* Schedule a work queue to wake up device */
799 queue_work(qca->workqueue, &qca->ws_awake_device);
800 break;
801
802 case HCI_IBS_TX_WAKING:
803 BT_DBG("Device waking up, queueing packet");
804 /* Transient state; just keep packet for later */
805 skb_queue_tail(&qca->tx_wait_q, skb);
806 break;
807
808 default:
809 BT_ERR("Illegal tx state: %d (losing packet)",
810 qca->tx_ibs_state);
811 kfree_skb(skb);
812 break;
813 }
814
815 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
816
817 return 0;
818 }
819
820 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
821 {
822 struct hci_uart *hu = hci_get_drvdata(hdev);
823
824 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
825
826 device_want_to_sleep(hu);
827
828 kfree_skb(skb);
829 return 0;
830 }
831
832 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
833 {
834 struct hci_uart *hu = hci_get_drvdata(hdev);
835
836 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
837
838 device_want_to_wakeup(hu);
839
840 kfree_skb(skb);
841 return 0;
842 }
843
844 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
845 {
846 struct hci_uart *hu = hci_get_drvdata(hdev);
847
848 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
849
850 device_woke_up(hu);
851
852 kfree_skb(skb);
853 return 0;
854 }
855
856 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
857 {
858 /* We receive debug logs from chip as an ACL packets.
859 * Instead of sending the data to ACL to decode the
860 * received data, we are pushing them to the above layers
861 * as a diagnostic packet.
862 */
863 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
864 return hci_recv_diag(hdev, skb);
865
866 return hci_recv_frame(hdev, skb);
867 }
868
869 #define QCA_IBS_SLEEP_IND_EVENT \
870 .type = HCI_IBS_SLEEP_IND, \
871 .hlen = 0, \
872 .loff = 0, \
873 .lsize = 0, \
874 .maxlen = HCI_MAX_IBS_SIZE
875
876 #define QCA_IBS_WAKE_IND_EVENT \
877 .type = HCI_IBS_WAKE_IND, \
878 .hlen = 0, \
879 .loff = 0, \
880 .lsize = 0, \
881 .maxlen = HCI_MAX_IBS_SIZE
882
883 #define QCA_IBS_WAKE_ACK_EVENT \
884 .type = HCI_IBS_WAKE_ACK, \
885 .hlen = 0, \
886 .loff = 0, \
887 .lsize = 0, \
888 .maxlen = HCI_MAX_IBS_SIZE
889
890 static const struct h4_recv_pkt qca_recv_pkts[] = {
891 { H4_RECV_ACL, .recv = qca_recv_acl_data },
892 { H4_RECV_SCO, .recv = hci_recv_frame },
893 { H4_RECV_EVENT, .recv = hci_recv_frame },
894 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
895 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
896 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
897 };
898
899 static int qca_recv(struct hci_uart *hu, const void *data, int count)
900 {
901 struct qca_data *qca = hu->priv;
902
903 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
904 return -EUNATCH;
905
906 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
907 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
908 if (IS_ERR(qca->rx_skb)) {
909 int err = PTR_ERR(qca->rx_skb);
910 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
911 qca->rx_skb = NULL;
912 return err;
913 }
914
915 return count;
916 }
917
918 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
919 {
920 struct qca_data *qca = hu->priv;
921
922 return skb_dequeue(&qca->txq);
923 }
924
925 static uint8_t qca_get_baudrate_value(int speed)
926 {
927 switch (speed) {
928 case 9600:
929 return QCA_BAUDRATE_9600;
930 case 19200:
931 return QCA_BAUDRATE_19200;
932 case 38400:
933 return QCA_BAUDRATE_38400;
934 case 57600:
935 return QCA_BAUDRATE_57600;
936 case 115200:
937 return QCA_BAUDRATE_115200;
938 case 230400:
939 return QCA_BAUDRATE_230400;
940 case 460800:
941 return QCA_BAUDRATE_460800;
942 case 500000:
943 return QCA_BAUDRATE_500000;
944 case 921600:
945 return QCA_BAUDRATE_921600;
946 case 1000000:
947 return QCA_BAUDRATE_1000000;
948 case 2000000:
949 return QCA_BAUDRATE_2000000;
950 case 3000000:
951 return QCA_BAUDRATE_3000000;
952 case 3200000:
953 return QCA_BAUDRATE_3200000;
954 case 3500000:
955 return QCA_BAUDRATE_3500000;
956 default:
957 return QCA_BAUDRATE_115200;
958 }
959 }
960
961 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
962 {
963 struct hci_uart *hu = hci_get_drvdata(hdev);
964 struct qca_data *qca = hu->priv;
965 struct sk_buff *skb;
966 struct qca_serdev *qcadev;
967 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
968
969 if (baudrate > QCA_BAUDRATE_3200000)
970 return -EINVAL;
971
972 cmd[4] = baudrate;
973
974 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
975 if (!skb) {
976 bt_dev_err(hdev, "Failed to allocate baudrate packet");
977 return -ENOMEM;
978 }
979
980 /* Disabling hardware flow control is mandatory while
981 * sending change baudrate request to wcn3990 SoC.
982 */
983 qcadev = serdev_device_get_drvdata(hu->serdev);
984 if (qcadev->btsoc_type == QCA_WCN3990)
985 hci_uart_set_flow_control(hu, true);
986
987 /* Assign commands to change baudrate and packet type. */
988 skb_put_data(skb, cmd, sizeof(cmd));
989 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
990
991 skb_queue_tail(&qca->txq, skb);
992 hci_uart_tx_wakeup(hu);
993
994 /* wait 300ms to change new baudrate on controller side
995 * controller will come back after they receive this HCI command
996 * then host can communicate with new baudrate to controller
997 */
998 set_current_state(TASK_UNINTERRUPTIBLE);
999 schedule_timeout(msecs_to_jiffies(BAUDRATE_SETTLE_TIMEOUT_MS));
1000 set_current_state(TASK_RUNNING);
1001
1002 if (qcadev->btsoc_type == QCA_WCN3990)
1003 hci_uart_set_flow_control(hu, false);
1004
1005 return 0;
1006 }
1007
1008 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1009 {
1010 if (hu->serdev)
1011 serdev_device_set_baudrate(hu->serdev, speed);
1012 else
1013 hci_uart_set_baudrate(hu, speed);
1014 }
1015
1016 static int qca_send_power_pulse(struct hci_dev *hdev, u8 cmd)
1017 {
1018 struct hci_uart *hu = hci_get_drvdata(hdev);
1019 struct qca_data *qca = hu->priv;
1020 struct sk_buff *skb;
1021
1022 /* These power pulses are single byte command which are sent
1023 * at required baudrate to wcn3990. On wcn3990, we have an external
1024 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1025 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1026 * and also we use the same power inputs to turn on and off for
1027 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1028 * we send a power on pulse at 115200 bps. This algorithm will help to
1029 * save power. Disabling hardware flow control is mandatory while
1030 * sending power pulses to SoC.
1031 */
1032 bt_dev_dbg(hdev, "sending power pulse %02x to SoC", cmd);
1033
1034 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1035 if (!skb)
1036 return -ENOMEM;
1037
1038 hci_uart_set_flow_control(hu, true);
1039
1040 skb_put_u8(skb, cmd);
1041 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1042
1043 skb_queue_tail(&qca->txq, skb);
1044 hci_uart_tx_wakeup(hu);
1045
1046 /* Wait for 100 uS for SoC to settle down */
1047 usleep_range(100, 200);
1048 hci_uart_set_flow_control(hu, false);
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 int ret;
1095
1096 if (speed_type == QCA_INIT_SPEED) {
1097 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1098 if (speed)
1099 host_set_baudrate(hu, speed);
1100 } else {
1101 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1102 if (!speed)
1103 return 0;
1104
1105 qca_baudrate = qca_get_baudrate_value(speed);
1106 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1107 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1108 if (ret)
1109 return ret;
1110
1111 host_set_baudrate(hu, speed);
1112 }
1113
1114 return 0;
1115 }
1116
1117 static int qca_wcn3990_init(struct hci_uart *hu)
1118 {
1119 struct hci_dev *hdev = hu->hdev;
1120 struct qca_serdev *qcadev;
1121 int ret;
1122
1123 /* Check for vregs status, may be hci down has turned
1124 * off the voltage regulator.
1125 */
1126 qcadev = serdev_device_get_drvdata(hu->serdev);
1127 if (!qcadev->bt_power->vregs_on) {
1128 serdev_device_close(hu->serdev);
1129 ret = qca_power_setup(hu, true);
1130 if (ret)
1131 return ret;
1132
1133 ret = serdev_device_open(hu->serdev);
1134 if (ret) {
1135 bt_dev_err(hu->hdev, "failed to open port");
1136 return ret;
1137 }
1138 }
1139
1140 /* Forcefully enable wcn3990 to enter in to boot mode. */
1141 host_set_baudrate(hu, 2400);
1142 ret = qca_send_power_pulse(hdev, QCA_WCN3990_POWEROFF_PULSE);
1143 if (ret)
1144 return ret;
1145
1146 qca_set_speed(hu, QCA_INIT_SPEED);
1147 ret = qca_send_power_pulse(hdev, QCA_WCN3990_POWERON_PULSE);
1148 if (ret)
1149 return ret;
1150
1151 /* Wait for 100 ms for SoC to boot */
1152 msleep(100);
1153
1154 /* Now the device is in ready state to communicate with host.
1155 * To sync host with device we need to reopen port.
1156 * Without this, we will have RTS and CTS synchronization
1157 * issues.
1158 */
1159 serdev_device_close(hu->serdev);
1160 ret = serdev_device_open(hu->serdev);
1161 if (ret) {
1162 bt_dev_err(hu->hdev, "failed to open port");
1163 return ret;
1164 }
1165
1166 hci_uart_set_flow_control(hu, false);
1167
1168 return 0;
1169 }
1170
1171 static int qca_setup(struct hci_uart *hu)
1172 {
1173 struct hci_dev *hdev = hu->hdev;
1174 struct qca_data *qca = hu->priv;
1175 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1176 struct qca_serdev *qcadev;
1177 int ret;
1178 int soc_ver = 0;
1179
1180 qcadev = serdev_device_get_drvdata(hu->serdev);
1181
1182 ret = qca_check_speeds(hu);
1183 if (ret)
1184 return ret;
1185
1186 /* Patch downloading has to be done without IBS mode */
1187 clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
1188
1189 if (qcadev->btsoc_type == QCA_WCN3990) {
1190 bt_dev_info(hdev, "setting up wcn3990");
1191
1192 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1193 * setup for every hci up.
1194 */
1195 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1196 hu->hdev->shutdown = qca_power_off;
1197 ret = qca_wcn3990_init(hu);
1198 if (ret)
1199 return ret;
1200
1201 ret = qca_read_soc_version(hdev, &soc_ver);
1202 if (ret)
1203 return ret;
1204 } else {
1205 bt_dev_info(hdev, "ROME setup");
1206 qca_set_speed(hu, QCA_INIT_SPEED);
1207 }
1208
1209 /* Setup user speed if needed */
1210 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1211 if (speed) {
1212 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1213 if (ret)
1214 return ret;
1215
1216 qca_baudrate = qca_get_baudrate_value(speed);
1217 }
1218
1219 if (qcadev->btsoc_type != QCA_WCN3990) {
1220 /* Get QCA version information */
1221 ret = qca_read_soc_version(hdev, &soc_ver);
1222 if (ret)
1223 return ret;
1224 }
1225
1226 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1227 /* Setup patch / NVM configurations */
1228 ret = qca_uart_setup(hdev, qca_baudrate, qcadev->btsoc_type, soc_ver);
1229 if (!ret) {
1230 set_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
1231 qca_debugfs_init(hdev);
1232 } else if (ret == -ENOENT) {
1233 /* No patch/nvm-config found, run with original fw/config */
1234 ret = 0;
1235 } else if (ret == -EAGAIN) {
1236 /*
1237 * Userspace firmware loader will return -EAGAIN in case no
1238 * patch/nvm-config is found, so run with original fw/config.
1239 */
1240 ret = 0;
1241 }
1242
1243 /* Setup bdaddr */
1244 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1245
1246 return ret;
1247 }
1248
1249 static struct hci_uart_proto qca_proto = {
1250 .id = HCI_UART_QCA,
1251 .name = "QCA",
1252 .manufacturer = 29,
1253 .init_speed = 115200,
1254 .oper_speed = 3000000,
1255 .open = qca_open,
1256 .close = qca_close,
1257 .flush = qca_flush,
1258 .setup = qca_setup,
1259 .recv = qca_recv,
1260 .enqueue = qca_enqueue,
1261 .dequeue = qca_dequeue,
1262 };
1263
1264 static const struct qca_vreg_data qca_soc_data = {
1265 .soc_type = QCA_WCN3990,
1266 .vregs = (struct qca_vreg []) {
1267 { "vddio", 1800000, 1900000, 15000 },
1268 { "vddxo", 1800000, 1900000, 80000 },
1269 { "vddrf", 1300000, 1350000, 300000 },
1270 { "vddch0", 3300000, 3400000, 450000 },
1271 },
1272 .num_vregs = 4,
1273 };
1274
1275 static void qca_power_shutdown(struct hci_uart *hu)
1276 {
1277 struct serdev_device *serdev = hu->serdev;
1278 unsigned char cmd = QCA_WCN3990_POWEROFF_PULSE;
1279
1280 host_set_baudrate(hu, 2400);
1281 hci_uart_set_flow_control(hu, true);
1282 serdev_device_write_buf(serdev, &cmd, sizeof(cmd));
1283 hci_uart_set_flow_control(hu, false);
1284 qca_power_setup(hu, false);
1285 }
1286
1287 static int qca_power_off(struct hci_dev *hdev)
1288 {
1289 struct hci_uart *hu = hci_get_drvdata(hdev);
1290
1291 qca_power_shutdown(hu);
1292 return 0;
1293 }
1294
1295 static int qca_enable_regulator(struct qca_vreg vregs,
1296 struct regulator *regulator)
1297 {
1298 int ret;
1299
1300 ret = regulator_set_voltage(regulator, vregs.min_uV,
1301 vregs.max_uV);
1302 if (ret)
1303 return ret;
1304
1305 if (vregs.load_uA)
1306 ret = regulator_set_load(regulator,
1307 vregs.load_uA);
1308
1309 if (ret)
1310 return ret;
1311
1312 return regulator_enable(regulator);
1313
1314 }
1315
1316 static void qca_disable_regulator(struct qca_vreg vregs,
1317 struct regulator *regulator)
1318 {
1319 regulator_disable(regulator);
1320 regulator_set_voltage(regulator, 0, vregs.max_uV);
1321 if (vregs.load_uA)
1322 regulator_set_load(regulator, 0);
1323
1324 }
1325
1326 static int qca_power_setup(struct hci_uart *hu, bool on)
1327 {
1328 struct qca_vreg *vregs;
1329 struct regulator_bulk_data *vreg_bulk;
1330 struct qca_serdev *qcadev;
1331 int i, num_vregs, ret = 0;
1332
1333 qcadev = serdev_device_get_drvdata(hu->serdev);
1334 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1335 !qcadev->bt_power->vreg_bulk)
1336 return -EINVAL;
1337
1338 vregs = qcadev->bt_power->vreg_data->vregs;
1339 vreg_bulk = qcadev->bt_power->vreg_bulk;
1340 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1341 BT_DBG("on: %d", on);
1342 if (on && !qcadev->bt_power->vregs_on) {
1343 for (i = 0; i < num_vregs; i++) {
1344 ret = qca_enable_regulator(vregs[i],
1345 vreg_bulk[i].consumer);
1346 if (ret)
1347 break;
1348 }
1349
1350 if (ret) {
1351 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1352 /* turn off regulators which are enabled */
1353 for (i = i - 1; i >= 0; i--)
1354 qca_disable_regulator(vregs[i],
1355 vreg_bulk[i].consumer);
1356 } else {
1357 qcadev->bt_power->vregs_on = true;
1358 }
1359 } else if (!on && qcadev->bt_power->vregs_on) {
1360 /* turn off regulator in reverse order */
1361 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1362 for ( ; i >= 0; i--)
1363 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1364
1365 qcadev->bt_power->vregs_on = false;
1366 }
1367
1368 return ret;
1369 }
1370
1371 static int qca_init_regulators(struct qca_power *qca,
1372 const struct qca_vreg *vregs, size_t num_vregs)
1373 {
1374 int i;
1375
1376 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
1377 sizeof(struct regulator_bulk_data),
1378 GFP_KERNEL);
1379 if (!qca->vreg_bulk)
1380 return -ENOMEM;
1381
1382 for (i = 0; i < num_vregs; i++)
1383 qca->vreg_bulk[i].supply = vregs[i].name;
1384
1385 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1386 }
1387
1388 static int qca_serdev_probe(struct serdev_device *serdev)
1389 {
1390 struct qca_serdev *qcadev;
1391 const struct qca_vreg_data *data;
1392 int err;
1393
1394 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1395 if (!qcadev)
1396 return -ENOMEM;
1397
1398 qcadev->serdev_hu.serdev = serdev;
1399 data = of_device_get_match_data(&serdev->dev);
1400 serdev_device_set_drvdata(serdev, qcadev);
1401 if (data && data->soc_type == QCA_WCN3990) {
1402 qcadev->btsoc_type = QCA_WCN3990;
1403 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1404 sizeof(struct qca_power),
1405 GFP_KERNEL);
1406 if (!qcadev->bt_power)
1407 return -ENOMEM;
1408
1409 qcadev->bt_power->dev = &serdev->dev;
1410 qcadev->bt_power->vreg_data = data;
1411 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1412 data->num_vregs);
1413 if (err) {
1414 BT_ERR("Failed to init regulators:%d", err);
1415 goto out;
1416 }
1417
1418 qcadev->bt_power->vregs_on = false;
1419
1420 device_property_read_u32(&serdev->dev, "max-speed",
1421 &qcadev->oper_speed);
1422 if (!qcadev->oper_speed)
1423 BT_DBG("UART will pick default operating speed");
1424
1425 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1426 if (err) {
1427 BT_ERR("wcn3990 serdev registration failed");
1428 goto out;
1429 }
1430 } else {
1431 qcadev->btsoc_type = QCA_ROME;
1432 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1433 GPIOD_OUT_LOW);
1434 if (IS_ERR(qcadev->bt_en)) {
1435 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1436 return PTR_ERR(qcadev->bt_en);
1437 }
1438
1439 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1440 if (IS_ERR(qcadev->susclk)) {
1441 dev_err(&serdev->dev, "failed to acquire clk\n");
1442 return PTR_ERR(qcadev->susclk);
1443 }
1444
1445 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1446 if (err)
1447 return err;
1448
1449 err = clk_prepare_enable(qcadev->susclk);
1450 if (err)
1451 return err;
1452
1453 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1454 if (err)
1455 clk_disable_unprepare(qcadev->susclk);
1456 }
1457
1458 out: return err;
1459
1460 }
1461
1462 static void qca_serdev_remove(struct serdev_device *serdev)
1463 {
1464 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1465
1466 if (qcadev->btsoc_type == QCA_WCN3990)
1467 qca_power_shutdown(&qcadev->serdev_hu);
1468 else
1469 clk_disable_unprepare(qcadev->susclk);
1470
1471 hci_uart_unregister_device(&qcadev->serdev_hu);
1472 }
1473
1474 static const struct of_device_id qca_bluetooth_of_match[] = {
1475 { .compatible = "qcom,qca6174-bt" },
1476 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data},
1477 { /* sentinel */ }
1478 };
1479 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1480
1481 static struct serdev_device_driver qca_serdev_driver = {
1482 .probe = qca_serdev_probe,
1483 .remove = qca_serdev_remove,
1484 .driver = {
1485 .name = "hci_uart_qca",
1486 .of_match_table = qca_bluetooth_of_match,
1487 },
1488 };
1489
1490 int __init qca_init(void)
1491 {
1492 serdev_device_driver_register(&qca_serdev_driver);
1493
1494 return hci_uart_register_proto(&qca_proto);
1495 }
1496
1497 int __exit qca_deinit(void)
1498 {
1499 serdev_device_driver_unregister(&qca_serdev_driver);
1500
1501 return hci_uart_unregister_proto(&qca_proto);
1502 }