Bluetooth: hci_qca: Make structure qca_proto constant
[linux-2.6-block.git] / drivers / bluetooth / hci_qca.c
CommitLineData
45051539 1// SPDX-License-Identifier: GPL-2.0-only
0ff252c1
BYTK
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.
fa9ad876 9 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
0ff252c1
BYTK
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.
0ff252c1
BYTK
16 */
17
18#include <linux/kernel.h>
05ba533c 19#include <linux/clk.h>
2faa3f15 20#include <linux/completion.h>
0ff252c1 21#include <linux/debugfs.h>
fa9ad876
BG
22#include <linux/delay.h>
23#include <linux/device.h>
05ba533c
TE
24#include <linux/gpio/consumer.h>
25#include <linux/mod_devicetable.h>
26#include <linux/module.h>
fa9ad876
BG
27#include <linux/of_device.h>
28#include <linux/platform_device.h>
29#include <linux/regulator/consumer.h>
05ba533c 30#include <linux/serdev.h>
c614ca3f 31#include <asm/unaligned.h>
0ff252c1
BYTK
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
f81b001a 43#define HCI_MAX_IBS_SIZE 10
0ff252c1 44
f81b001a
MH
45#define IBS_WAKE_RETRANS_TIMEOUT_MS 100
46#define IBS_TX_IDLE_TIMEOUT_MS 2000
94d66714 47#define CMD_TRANS_TIMEOUT_MS 100
0ff252c1 48
05ba533c
TE
49/* susclk rate */
50#define SUSCLK_RATE_32KHZ 32768
51
c614ca3f
BG
52/* Controller debug log header */
53#define QCA_DEBUG_HANDLE 0x2EDC
54
62a91990
MK
55enum qca_flags {
56 QCA_IBS_ENABLED,
2faa3f15 57 QCA_DROP_VENDOR_EVENT,
62a91990
MK
58};
59
0ff252c1
BYTK
60/* HCI_IBS transmit side sleep protocol states */
61enum 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 */
68enum rx_states {
69 HCI_IBS_RX_ASLEEP,
70 HCI_IBS_RX_AWAKE,
71};
72
73/* HCI_IBS transmit and receive side clock state vote */
74enum 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
82struct 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 */
621a5f7a
VK
90 bool tx_vote; /* Clock must be on for TX */
91 bool rx_vote; /* Clock must be on for RX */
0ff252c1
BYTK
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;
2faa3f15 102 struct completion drop_ev_comp;
0ff252c1
BYTK
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
83d9c5e5
BG
122enum qca_speed_type {
123 QCA_INIT_SPEED = 1,
124 QCA_OPER_SPEED
125};
126
fa9ad876
BG
127/*
128 * Voltage regulator information required for configuring the
129 * QCA Bluetooth chipset
130 */
131struct qca_vreg {
132 const char *name;
133 unsigned int min_uV;
134 unsigned int max_uV;
135 unsigned int load_uA;
136};
137
138struct 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 */
147struct 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
05ba533c
TE
154struct qca_serdev {
155 struct hci_uart serdev_hu;
156 struct gpio_desc *bt_en;
157 struct clk *susclk;
fa9ad876
BG
158 enum qca_btsoc_type btsoc_type;
159 struct qca_power *bt_power;
160 u32 init_speed;
161 u32 oper_speed;
99c905c6 162 const char *firmware_name;
05ba533c
TE
163};
164
fa9ad876 165static int qca_power_setup(struct hci_uart *hu, bool on);
c2d78273 166static void qca_power_shutdown(struct hci_uart *hu);
3e4be65e 167static int qca_power_off(struct hci_dev *hdev);
fa9ad876 168
4fdd5a4f
MK
169static 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
99c905c6
RL
184static 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
0ff252c1
BYTK
195static 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
203static 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 */
212static 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
ce26d813
PK
265 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
266 vote ? "true" : "false");
0ff252c1
BYTK
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 */
284static 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 */
634fef61 299 skb_put_u8(skb, cmd);
0ff252c1
BYTK
300
301 skb_queue_tail(&qca->txq, skb);
302
303 return err;
304}
305
306static 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
336static 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
363static 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
374static 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
04356052 391static void hci_ibs_tx_idle_timeout(struct timer_list *t)
0ff252c1 392{
04356052
KC
393 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
394 struct hci_uart *hu = qca->hu;
0ff252c1
BYTK
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:
e059a465 419 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
0ff252c1
BYTK
420 break;
421 }
422
423 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
424}
425
04356052 426static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
0ff252c1 427{
04356052
KC
428 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
429 struct hci_uart *hu = qca->hu;
0ff252c1 430 unsigned long flags, retrans_delay;
a9137188 431 bool retransmit = false;
0ff252c1
BYTK
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 */
a9137188 442 retransmit = true;
0ff252c1
BYTK
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:
e059a465 457 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
0ff252c1
BYTK
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 */
468static int qca_open(struct hci_uart *hu)
469{
05ba533c 470 struct qca_serdev *qcadev;
0ff252c1 471 struct qca_data *qca;
fa9ad876 472 int ret;
0ff252c1
BYTK
473
474 BT_DBG("hu %p qca_open", hu);
475
b36a1552
VD
476 if (!hci_uart_has_flow_control(hu))
477 return -EOPNOTSUPP;
478
25a13e38 479 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
0ff252c1
BYTK
480 if (!qca)
481 return -ENOMEM;
482
483 skb_queue_head_init(&qca->txq);
484 skb_queue_head_init(&qca->tx_wait_q);
485 spin_lock_init(&qca->hci_ibs_lock);
fac9a602 486 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
0ff252c1
BYTK
487 if (!qca->workqueue) {
488 BT_ERR("QCA Workqueue not initialized properly");
489 kfree(qca);
490 return -ENOMEM;
491 }
492
493 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
494 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
495 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
496 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
497
498 qca->hu = hu;
2faa3f15 499 init_completion(&qca->drop_ev_comp);
0ff252c1
BYTK
500
501 /* Assume we start with both sides asleep -- extra wakes OK */
502 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
503 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
504
0ff252c1 505 qca->vote_last_jif = jiffies;
0ff252c1
BYTK
506
507 hu->priv = qca;
508
05ba533c 509 if (hu->serdev) {
05ba533c
TE
510
511 qcadev = serdev_device_get_drvdata(hu->serdev);
523760b7 512 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
fa9ad876 513 gpiod_set_value_cansleep(qcadev->bt_en, 1);
7f09d5a6
BG
514 /* Controller needs time to bootup. */
515 msleep(150);
fa9ad876
BG
516 } else {
517 hu->init_speed = qcadev->init_speed;
518 hu->oper_speed = qcadev->oper_speed;
519 ret = qca_power_setup(hu, true);
520 if (ret) {
521 destroy_workqueue(qca->workqueue);
522 kfree_skb(qca->rx_skb);
523 hu->priv = NULL;
524 kfree(qca);
525 return ret;
526 }
527 }
05ba533c
TE
528 }
529
fa9ad876
BG
530 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
531 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
532
533 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
534 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
535
0ff252c1
BYTK
536 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
537 qca->tx_idle_delay, qca->wake_retrans);
538
539 return 0;
540}
541
542static void qca_debugfs_init(struct hci_dev *hdev)
543{
544 struct hci_uart *hu = hci_get_drvdata(hdev);
545 struct qca_data *qca = hu->priv;
546 struct dentry *ibs_dir;
547 umode_t mode;
548
549 if (!hdev->debugfs)
550 return;
551
552 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
553
554 /* read only */
555 mode = S_IRUGO;
556 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
557 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
558 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
559 &qca->ibs_sent_slps);
560 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
561 &qca->ibs_sent_wakes);
562 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
563 &qca->ibs_sent_wacks);
564 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
565 &qca->ibs_recv_slps);
566 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
567 &qca->ibs_recv_wakes);
568 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
569 &qca->ibs_recv_wacks);
10be6c0f 570 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
0ff252c1
BYTK
571 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
572 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
10be6c0f 573 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
0ff252c1
BYTK
574 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
575 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
576 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
577 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
578 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
579 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
580
581 /* read/write */
582 mode = S_IRUGO | S_IWUSR;
583 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
584 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
585 &qca->tx_idle_delay);
586}
587
588/* Flush protocol data */
589static int qca_flush(struct hci_uart *hu)
590{
591 struct qca_data *qca = hu->priv;
592
593 BT_DBG("hu %p qca flush", hu);
594
595 skb_queue_purge(&qca->tx_wait_q);
596 skb_queue_purge(&qca->txq);
597
598 return 0;
599}
600
601/* Close protocol */
602static int qca_close(struct hci_uart *hu)
603{
05ba533c 604 struct qca_serdev *qcadev;
0ff252c1
BYTK
605 struct qca_data *qca = hu->priv;
606
607 BT_DBG("hu %p qca close", hu);
608
609 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
610
611 skb_queue_purge(&qca->tx_wait_q);
612 skb_queue_purge(&qca->txq);
613 del_timer(&qca->tx_idle_timer);
614 del_timer(&qca->wake_retrans_timer);
615 destroy_workqueue(qca->workqueue);
616 qca->hu = NULL;
617
05ba533c 618 if (hu->serdev) {
05ba533c 619 qcadev = serdev_device_get_drvdata(hu->serdev);
523760b7 620 if (qca_is_wcn399x(qcadev->btsoc_type))
c2d78273 621 qca_power_shutdown(hu);
fa9ad876
BG
622 else
623 gpiod_set_value_cansleep(qcadev->bt_en, 0);
624
05ba533c
TE
625 }
626
0ff252c1
BYTK
627 kfree_skb(qca->rx_skb);
628
629 hu->priv = NULL;
630
631 kfree(qca);
632
633 return 0;
634}
635
636/* Called upon a wake-up-indication from the device.
637 */
638static void device_want_to_wakeup(struct hci_uart *hu)
639{
640 unsigned long flags;
641 struct qca_data *qca = hu->priv;
642
643 BT_DBG("hu %p want to wake up", hu);
644
645 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
646
647 qca->ibs_recv_wakes++;
648
649 switch (qca->rx_ibs_state) {
650 case HCI_IBS_RX_ASLEEP:
651 /* Make sure clock is on - we may have turned clock off since
652 * receiving the wake up indicator awake rx clock.
653 */
654 queue_work(qca->workqueue, &qca->ws_awake_rx);
655 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
656 return;
657
658 case HCI_IBS_RX_AWAKE:
659 /* Always acknowledge device wake up,
660 * sending IBS message doesn't count as TX ON.
661 */
662 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
663 BT_ERR("Failed to acknowledge device wake up");
664 break;
665 }
666 qca->ibs_sent_wacks++;
667 break;
668
669 default:
670 /* Any other state is illegal */
671 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
672 qca->rx_ibs_state);
673 break;
674 }
675
676 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
677
678 /* Actually send the packets */
679 hci_uart_tx_wakeup(hu);
680}
681
682/* Called upon a sleep-indication from the device.
683 */
684static void device_want_to_sleep(struct hci_uart *hu)
685{
686 unsigned long flags;
687 struct qca_data *qca = hu->priv;
688
6600c080 689 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
0ff252c1
BYTK
690
691 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
692
693 qca->ibs_recv_slps++;
694
695 switch (qca->rx_ibs_state) {
696 case HCI_IBS_RX_AWAKE:
697 /* Update state */
698 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
699 /* Vote off rx clock under workqueue */
700 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
701 break;
702
703 case HCI_IBS_RX_ASLEEP:
6600c080 704 break;
0ff252c1
BYTK
705
706 default:
707 /* Any other state is illegal */
708 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
709 qca->rx_ibs_state);
710 break;
711 }
712
713 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
714}
715
716/* Called upon wake-up-acknowledgement from the device
717 */
718static void device_woke_up(struct hci_uart *hu)
719{
720 unsigned long flags, idle_delay;
721 struct qca_data *qca = hu->priv;
722 struct sk_buff *skb = NULL;
723
724 BT_DBG("hu %p woke up", hu);
725
726 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
727
728 qca->ibs_recv_wacks++;
729
730 switch (qca->tx_ibs_state) {
731 case HCI_IBS_TX_AWAKE:
732 /* Expect one if we send 2 WAKEs */
733 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
734 qca->tx_ibs_state);
735 break;
736
737 case HCI_IBS_TX_WAKING:
738 /* Send pending packets */
739 while ((skb = skb_dequeue(&qca->tx_wait_q)))
740 skb_queue_tail(&qca->txq, skb);
741
742 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
743 del_timer(&qca->wake_retrans_timer);
744 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
745 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
746 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
747 break;
748
749 case HCI_IBS_TX_ASLEEP:
750 /* Fall through */
751
752 default:
753 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
754 qca->tx_ibs_state);
755 break;
756 }
757
758 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
759
760 /* Actually send the packets */
761 hci_uart_tx_wakeup(hu);
762}
763
764/* Enqueue frame for transmittion (padding, crc, etc) may be called from
765 * two simultaneous tasklets.
766 */
767static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
768{
769 unsigned long flags = 0, idle_delay;
770 struct qca_data *qca = hu->priv;
771
772 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
773 qca->tx_ibs_state);
774
775 /* Prepend skb with frame type */
618e8bc2 776 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
0ff252c1 777
035a960e
BG
778 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
779
0ff252c1
BYTK
780 /* Don't go to sleep in middle of patch download or
781 * Out-Of-Band(GPIOs control) sleep is selected.
782 */
62a91990 783 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) {
0ff252c1 784 skb_queue_tail(&qca->txq, skb);
035a960e 785 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
0ff252c1
BYTK
786 return 0;
787 }
788
0ff252c1
BYTK
789 /* Act according to current state */
790 switch (qca->tx_ibs_state) {
791 case HCI_IBS_TX_AWAKE:
792 BT_DBG("Device awake, sending normally");
793 skb_queue_tail(&qca->txq, skb);
794 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
795 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
796 break;
797
798 case HCI_IBS_TX_ASLEEP:
799 BT_DBG("Device asleep, waking up and queueing packet");
800 /* Save packet for later */
801 skb_queue_tail(&qca->tx_wait_q, skb);
802
803 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
804 /* Schedule a work queue to wake up device */
805 queue_work(qca->workqueue, &qca->ws_awake_device);
806 break;
807
808 case HCI_IBS_TX_WAKING:
809 BT_DBG("Device waking up, queueing packet");
810 /* Transient state; just keep packet for later */
811 skb_queue_tail(&qca->tx_wait_q, skb);
812 break;
813
814 default:
815 BT_ERR("Illegal tx state: %d (losing packet)",
816 qca->tx_ibs_state);
817 kfree_skb(skb);
818 break;
819 }
820
821 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
822
823 return 0;
824}
825
826static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
827{
828 struct hci_uart *hu = hci_get_drvdata(hdev);
829
830 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
831
832 device_want_to_sleep(hu);
833
834 kfree_skb(skb);
835 return 0;
836}
837
838static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
839{
840 struct hci_uart *hu = hci_get_drvdata(hdev);
841
842 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
843
844 device_want_to_wakeup(hu);
845
846 kfree_skb(skb);
847 return 0;
848}
849
850static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
851{
852 struct hci_uart *hu = hci_get_drvdata(hdev);
853
854 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
855
856 device_woke_up(hu);
857
858 kfree_skb(skb);
859 return 0;
860}
861
c614ca3f
BG
862static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
863{
864 /* We receive debug logs from chip as an ACL packets.
865 * Instead of sending the data to ACL to decode the
866 * received data, we are pushing them to the above layers
867 * as a diagnostic packet.
868 */
869 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
870 return hci_recv_diag(hdev, skb);
871
872 return hci_recv_frame(hdev, skb);
873}
874
2faa3f15
MK
875static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
876{
877 struct hci_uart *hu = hci_get_drvdata(hdev);
878 struct qca_data *qca = hu->priv;
879
880 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
881 struct hci_event_hdr *hdr = (void *)skb->data;
882
883 /* For the WCN3990 the vendor command for a baudrate change
884 * isn't sent as synchronous HCI command, because the
885 * controller sends the corresponding vendor event with the
886 * new baudrate. The event is received and properly decoded
887 * after changing the baudrate of the host port. It needs to
888 * be dropped, otherwise it can be misinterpreted as
889 * response to a later firmware download command (also a
890 * vendor command).
891 */
892
893 if (hdr->evt == HCI_EV_VENDOR)
894 complete(&qca->drop_ev_comp);
895
4974c839 896 kfree_skb(skb);
2faa3f15
MK
897
898 return 0;
899 }
900
901 return hci_recv_frame(hdev, skb);
902}
903
0ff252c1
BYTK
904#define QCA_IBS_SLEEP_IND_EVENT \
905 .type = HCI_IBS_SLEEP_IND, \
906 .hlen = 0, \
907 .loff = 0, \
908 .lsize = 0, \
909 .maxlen = HCI_MAX_IBS_SIZE
910
911#define QCA_IBS_WAKE_IND_EVENT \
912 .type = HCI_IBS_WAKE_IND, \
913 .hlen = 0, \
914 .loff = 0, \
915 .lsize = 0, \
916 .maxlen = HCI_MAX_IBS_SIZE
917
918#define QCA_IBS_WAKE_ACK_EVENT \
919 .type = HCI_IBS_WAKE_ACK, \
920 .hlen = 0, \
921 .loff = 0, \
922 .lsize = 0, \
923 .maxlen = HCI_MAX_IBS_SIZE
924
925static const struct h4_recv_pkt qca_recv_pkts[] = {
c614ca3f 926 { H4_RECV_ACL, .recv = qca_recv_acl_data },
0ff252c1 927 { H4_RECV_SCO, .recv = hci_recv_frame },
2faa3f15 928 { H4_RECV_EVENT, .recv = qca_recv_event },
0ff252c1
BYTK
929 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
930 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
931 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
932};
933
934static int qca_recv(struct hci_uart *hu, const void *data, int count)
935{
936 struct qca_data *qca = hu->priv;
937
938 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
939 return -EUNATCH;
940
941 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
942 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
943 if (IS_ERR(qca->rx_skb)) {
944 int err = PTR_ERR(qca->rx_skb);
2064ee33 945 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
0ff252c1
BYTK
946 qca->rx_skb = NULL;
947 return err;
948 }
949
950 return count;
951}
952
953static struct sk_buff *qca_dequeue(struct hci_uart *hu)
954{
955 struct qca_data *qca = hu->priv;
956
957 return skb_dequeue(&qca->txq);
958}
959
960static uint8_t qca_get_baudrate_value(int speed)
961{
ce26d813 962 switch (speed) {
0ff252c1
BYTK
963 case 9600:
964 return QCA_BAUDRATE_9600;
965 case 19200:
966 return QCA_BAUDRATE_19200;
967 case 38400:
968 return QCA_BAUDRATE_38400;
969 case 57600:
970 return QCA_BAUDRATE_57600;
971 case 115200:
972 return QCA_BAUDRATE_115200;
973 case 230400:
974 return QCA_BAUDRATE_230400;
975 case 460800:
976 return QCA_BAUDRATE_460800;
977 case 500000:
978 return QCA_BAUDRATE_500000;
979 case 921600:
980 return QCA_BAUDRATE_921600;
981 case 1000000:
982 return QCA_BAUDRATE_1000000;
983 case 2000000:
984 return QCA_BAUDRATE_2000000;
985 case 3000000:
986 return QCA_BAUDRATE_3000000;
be93a497
BG
987 case 3200000:
988 return QCA_BAUDRATE_3200000;
0ff252c1
BYTK
989 case 3500000:
990 return QCA_BAUDRATE_3500000;
991 default:
992 return QCA_BAUDRATE_115200;
993 }
994}
995
996static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
997{
998 struct hci_uart *hu = hci_get_drvdata(hdev);
999 struct qca_data *qca = hu->priv;
1000 struct sk_buff *skb;
1001 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1002
be93a497 1003 if (baudrate > QCA_BAUDRATE_3200000)
0ff252c1
BYTK
1004 return -EINVAL;
1005
1006 cmd[4] = baudrate;
1007
25a13e38 1008 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
0ff252c1 1009 if (!skb) {
2064ee33 1010 bt_dev_err(hdev, "Failed to allocate baudrate packet");
0ff252c1
BYTK
1011 return -ENOMEM;
1012 }
1013
1014 /* Assign commands to change baudrate and packet type. */
59ae1d12 1015 skb_put_data(skb, cmd, sizeof(cmd));
618e8bc2 1016 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
0ff252c1
BYTK
1017
1018 skb_queue_tail(&qca->txq, skb);
1019 hci_uart_tx_wakeup(hu);
1020
94d66714
MK
1021 /* Wait for the baudrate change request to be sent */
1022
1023 while (!skb_queue_empty(&qca->txq))
1024 usleep_range(100, 200);
1025
ecf2b768
MK
1026 if (hu->serdev)
1027 serdev_device_wait_until_sent(hu->serdev,
94d66714
MK
1028 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1029
1030 /* Give the controller time to process the request */
523760b7 1031 if (qca_is_wcn399x(qca_soc_type(hu)))
94d66714
MK
1032 msleep(10);
1033 else
1034 msleep(300);
0ff252c1
BYTK
1035
1036 return 0;
1037}
1038
05ba533c
TE
1039static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1040{
1041 if (hu->serdev)
1042 serdev_device_set_baudrate(hu->serdev, speed);
1043 else
1044 hci_uart_set_baudrate(hu, speed);
1045}
1046
9836b802 1047static int qca_send_power_pulse(struct hci_uart *hu, bool on)
fa9ad876 1048{
f9558270 1049 int ret;
94d66714 1050 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
9836b802 1051 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
fa9ad876
BG
1052
1053 /* These power pulses are single byte command which are sent
1054 * at required baudrate to wcn3990. On wcn3990, we have an external
1055 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1056 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1057 * and also we use the same power inputs to turn on and off for
1058 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1059 * we send a power on pulse at 115200 bps. This algorithm will help to
1060 * save power. Disabling hardware flow control is mandatory while
1061 * sending power pulses to SoC.
1062 */
f9558270 1063 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
fa9ad876 1064
f9558270 1065 serdev_device_write_flush(hu->serdev);
fa9ad876 1066 hci_uart_set_flow_control(hu, true);
f9558270
BG
1067 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1068 if (ret < 0) {
1069 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1070 return ret;
1071 }
fa9ad876 1072
f9558270 1073 serdev_device_wait_until_sent(hu->serdev, timeout);
fa9ad876
BG
1074 hci_uart_set_flow_control(hu, false);
1075
0ebcddd8 1076 /* Give to controller time to boot/shutdown */
ad571d72
MK
1077 if (on)
1078 msleep(100);
0ebcddd8
MK
1079 else
1080 msleep(10);
ad571d72 1081
fa9ad876
BG
1082 return 0;
1083}
1084
83d9c5e5
BG
1085static unsigned int qca_get_speed(struct hci_uart *hu,
1086 enum qca_speed_type speed_type)
1087{
1088 unsigned int speed = 0;
1089
1090 if (speed_type == QCA_INIT_SPEED) {
1091 if (hu->init_speed)
1092 speed = hu->init_speed;
1093 else if (hu->proto->init_speed)
1094 speed = hu->proto->init_speed;
1095 } else {
1096 if (hu->oper_speed)
1097 speed = hu->oper_speed;
1098 else if (hu->proto->oper_speed)
1099 speed = hu->proto->oper_speed;
1100 }
1101
1102 return speed;
1103}
1104
1105static int qca_check_speeds(struct hci_uart *hu)
1106{
523760b7 1107 if (qca_is_wcn399x(qca_soc_type(hu))) {
fa9ad876
BG
1108 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1109 !qca_get_speed(hu, QCA_OPER_SPEED))
1110 return -EINVAL;
1111 } else {
1112 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1113 !qca_get_speed(hu, QCA_OPER_SPEED))
1114 return -EINVAL;
1115 }
83d9c5e5
BG
1116
1117 return 0;
1118}
1119
1120static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1121{
1122 unsigned int speed, qca_baudrate;
2faa3f15 1123 struct qca_data *qca = hu->priv;
78e8fa29 1124 int ret = 0;
83d9c5e5
BG
1125
1126 if (speed_type == QCA_INIT_SPEED) {
1127 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1128 if (speed)
1129 host_set_baudrate(hu, speed);
1130 } else {
4fdd5a4f
MK
1131 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1132
83d9c5e5
BG
1133 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1134 if (!speed)
1135 return 0;
1136
78e8fa29
BG
1137 /* Disable flow control for wcn3990 to deassert RTS while
1138 * changing the baudrate of chip and host.
1139 */
523760b7 1140 if (qca_is_wcn399x(soc_type))
78e8fa29
BG
1141 hci_uart_set_flow_control(hu, true);
1142
2faa3f15
MK
1143 if (soc_type == QCA_WCN3990) {
1144 reinit_completion(&qca->drop_ev_comp);
1145 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1146 }
1147
83d9c5e5 1148 qca_baudrate = qca_get_baudrate_value(speed);
fa9ad876 1149 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
83d9c5e5
BG
1150 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1151 if (ret)
78e8fa29 1152 goto error;
83d9c5e5
BG
1153
1154 host_set_baudrate(hu, speed);
78e8fa29
BG
1155
1156error:
523760b7 1157 if (qca_is_wcn399x(soc_type))
78e8fa29 1158 hci_uart_set_flow_control(hu, false);
2faa3f15
MK
1159
1160 if (soc_type == QCA_WCN3990) {
1161 /* Wait for the controller to send the vendor event
1162 * for the baudrate change command.
1163 */
1164 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1165 msecs_to_jiffies(100))) {
1166 bt_dev_err(hu->hdev,
1167 "Failed to change controller baudrate\n");
1168 ret = -ETIMEDOUT;
1169 }
1170
1171 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1172 }
83d9c5e5
BG
1173 }
1174
78e8fa29 1175 return ret;
83d9c5e5
BG
1176}
1177
fa9ad876
BG
1178static int qca_wcn3990_init(struct hci_uart *hu)
1179{
3e4be65e 1180 struct qca_serdev *qcadev;
fa9ad876
BG
1181 int ret;
1182
3e4be65e
BG
1183 /* Check for vregs status, may be hci down has turned
1184 * off the voltage regulator.
1185 */
1186 qcadev = serdev_device_get_drvdata(hu->serdev);
1187 if (!qcadev->bt_power->vregs_on) {
1188 serdev_device_close(hu->serdev);
1189 ret = qca_power_setup(hu, true);
1190 if (ret)
1191 return ret;
1192
1193 ret = serdev_device_open(hu->serdev);
1194 if (ret) {
1195 bt_dev_err(hu->hdev, "failed to open port");
1196 return ret;
1197 }
1198 }
1199
fa9ad876
BG
1200 /* Forcefully enable wcn3990 to enter in to boot mode. */
1201 host_set_baudrate(hu, 2400);
9836b802 1202 ret = qca_send_power_pulse(hu, false);
fa9ad876
BG
1203 if (ret)
1204 return ret;
1205
1206 qca_set_speed(hu, QCA_INIT_SPEED);
9836b802 1207 ret = qca_send_power_pulse(hu, true);
fa9ad876
BG
1208 if (ret)
1209 return ret;
1210
fa9ad876
BG
1211 /* Now the device is in ready state to communicate with host.
1212 * To sync host with device we need to reopen port.
1213 * Without this, we will have RTS and CTS synchronization
1214 * issues.
1215 */
1216 serdev_device_close(hu->serdev);
1217 ret = serdev_device_open(hu->serdev);
1218 if (ret) {
1219 bt_dev_err(hu->hdev, "failed to open port");
1220 return ret;
1221 }
1222
1223 hci_uart_set_flow_control(hu, false);
1224
1225 return 0;
1226}
1227
0ff252c1
BYTK
1228static int qca_setup(struct hci_uart *hu)
1229{
1230 struct hci_dev *hdev = hu->hdev;
1231 struct qca_data *qca = hu->priv;
1232 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
4fdd5a4f 1233 enum qca_btsoc_type soc_type = qca_soc_type(hu);
99c905c6 1234 const char *firmware_name = qca_get_firmware_name(hu);
0ff252c1 1235 int ret;
aadebac4 1236 int soc_ver = 0;
0ff252c1 1237
83d9c5e5
BG
1238 ret = qca_check_speeds(hu);
1239 if (ret)
1240 return ret;
1241
0ff252c1 1242 /* Patch downloading has to be done without IBS mode */
62a91990 1243 clear_bit(QCA_IBS_ENABLED, &qca->flags);
0ff252c1 1244
523760b7 1245 if (qca_is_wcn399x(soc_type)) {
fa9ad876 1246 bt_dev_info(hdev, "setting up wcn3990");
3e4be65e
BG
1247
1248 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1249 * setup for every hci up.
1250 */
1251 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
5971752d 1252 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
3e4be65e 1253 hu->hdev->shutdown = qca_power_off;
fa9ad876
BG
1254 ret = qca_wcn3990_init(hu);
1255 if (ret)
1256 return ret;
1257
1258 ret = qca_read_soc_version(hdev, &soc_ver);
1259 if (ret)
1260 return ret;
1261 } else {
1262 bt_dev_info(hdev, "ROME setup");
1263 qca_set_speed(hu, QCA_INIT_SPEED);
1264 }
0ff252c1
BYTK
1265
1266 /* Setup user speed if needed */
83d9c5e5 1267 speed = qca_get_speed(hu, QCA_OPER_SPEED);
0ff252c1 1268 if (speed) {
83d9c5e5
BG
1269 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1270 if (ret)
0ff252c1 1271 return ret;
83d9c5e5
BG
1272
1273 qca_baudrate = qca_get_baudrate_value(speed);
0ff252c1
BYTK
1274 }
1275
523760b7 1276 if (!qca_is_wcn399x(soc_type)) {
fa9ad876
BG
1277 /* Get QCA version information */
1278 ret = qca_read_soc_version(hdev, &soc_ver);
1279 if (ret)
1280 return ret;
1281 }
aadebac4
BG
1282
1283 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
0ff252c1 1284 /* Setup patch / NVM configurations */
99c905c6
RL
1285 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1286 firmware_name);
0ff252c1 1287 if (!ret) {
62a91990 1288 set_bit(QCA_IBS_ENABLED, &qca->flags);
0ff252c1 1289 qca_debugfs_init(hdev);
ba8f3597
LP
1290 } else if (ret == -ENOENT) {
1291 /* No patch/nvm-config found, run with original fw/config */
1292 ret = 0;
7dc5fe08
AP
1293 } else if (ret == -EAGAIN) {
1294 /*
1295 * Userspace firmware loader will return -EAGAIN in case no
1296 * patch/nvm-config is found, so run with original fw/config.
1297 */
1298 ret = 0;
0ff252c1
BYTK
1299 }
1300
1301 /* Setup bdaddr */
523760b7 1302 if (qca_is_wcn399x(soc_type))
5c0a1001
BG
1303 hu->hdev->set_bdaddr = qca_set_bdaddr;
1304 else
1305 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
0ff252c1
BYTK
1306
1307 return ret;
1308}
1309
2edc9c5c 1310static const struct hci_uart_proto qca_proto = {
0ff252c1
BYTK
1311 .id = HCI_UART_QCA,
1312 .name = "QCA",
aee61f7a 1313 .manufacturer = 29,
0ff252c1
BYTK
1314 .init_speed = 115200,
1315 .oper_speed = 3000000,
1316 .open = qca_open,
1317 .close = qca_close,
1318 .flush = qca_flush,
1319 .setup = qca_setup,
1320 .recv = qca_recv,
1321 .enqueue = qca_enqueue,
1322 .dequeue = qca_dequeue,
1323};
1324
523760b7 1325static const struct qca_vreg_data qca_soc_data_wcn3990 = {
fa9ad876
BG
1326 .soc_type = QCA_WCN3990,
1327 .vregs = (struct qca_vreg []) {
1328 { "vddio", 1800000, 1900000, 15000 },
1329 { "vddxo", 1800000, 1900000, 80000 },
1330 { "vddrf", 1300000, 1350000, 300000 },
1331 { "vddch0", 3300000, 3400000, 450000 },
1332 },
1333 .num_vregs = 4,
1334};
1335
523760b7
HB
1336static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1337 .soc_type = QCA_WCN3998,
1338 .vregs = (struct qca_vreg []) {
1339 { "vddio", 1800000, 1900000, 10000 },
1340 { "vddxo", 1800000, 1900000, 80000 },
1341 { "vddrf", 1300000, 1352000, 300000 },
1342 { "vddch0", 3300000, 3300000, 450000 },
1343 },
1344 .num_vregs = 4,
1345};
1346
c2d78273 1347static void qca_power_shutdown(struct hci_uart *hu)
fa9ad876 1348{
035a960e
BG
1349 struct qca_data *qca = hu->priv;
1350 unsigned long flags;
1351
1352 /* From this point we go into power off state. But serial port is
1353 * still open, stop queueing the IBS data and flush all the buffered
1354 * data in skb's.
1355 */
1356 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
62a91990 1357 clear_bit(QCA_IBS_ENABLED, &qca->flags);
035a960e
BG
1358 qca_flush(hu);
1359 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1360
fa9ad876 1361 host_set_baudrate(hu, 2400);
9836b802 1362 qca_send_power_pulse(hu, false);
fa9ad876
BG
1363 qca_power_setup(hu, false);
1364}
1365
3e4be65e
BG
1366static int qca_power_off(struct hci_dev *hdev)
1367{
1368 struct hci_uart *hu = hci_get_drvdata(hdev);
1369
a2780889
HB
1370 /* Perform pre shutdown command */
1371 qca_send_pre_shutdown_cmd(hdev);
1372
3e4be65e
BG
1373 qca_power_shutdown(hu);
1374 return 0;
1375}
1376
fa9ad876
BG
1377static int qca_enable_regulator(struct qca_vreg vregs,
1378 struct regulator *regulator)
1379{
1380 int ret;
1381
1382 ret = regulator_set_voltage(regulator, vregs.min_uV,
1383 vregs.max_uV);
1384 if (ret)
1385 return ret;
1386
1387 if (vregs.load_uA)
1388 ret = regulator_set_load(regulator,
1389 vregs.load_uA);
1390
1391 if (ret)
1392 return ret;
1393
1394 return regulator_enable(regulator);
1395
1396}
1397
1398static void qca_disable_regulator(struct qca_vreg vregs,
1399 struct regulator *regulator)
1400{
1401 regulator_disable(regulator);
1402 regulator_set_voltage(regulator, 0, vregs.max_uV);
1403 if (vregs.load_uA)
1404 regulator_set_load(regulator, 0);
1405
1406}
1407
1408static int qca_power_setup(struct hci_uart *hu, bool on)
1409{
1410 struct qca_vreg *vregs;
1411 struct regulator_bulk_data *vreg_bulk;
1412 struct qca_serdev *qcadev;
1413 int i, num_vregs, ret = 0;
1414
1415 qcadev = serdev_device_get_drvdata(hu->serdev);
1416 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1417 !qcadev->bt_power->vreg_bulk)
1418 return -EINVAL;
1419
1420 vregs = qcadev->bt_power->vreg_data->vregs;
1421 vreg_bulk = qcadev->bt_power->vreg_bulk;
1422 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1423 BT_DBG("on: %d", on);
1424 if (on && !qcadev->bt_power->vregs_on) {
1425 for (i = 0; i < num_vregs; i++) {
1426 ret = qca_enable_regulator(vregs[i],
1427 vreg_bulk[i].consumer);
1428 if (ret)
1429 break;
1430 }
1431
1432 if (ret) {
1433 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1434 /* turn off regulators which are enabled */
1435 for (i = i - 1; i >= 0; i--)
1436 qca_disable_regulator(vregs[i],
1437 vreg_bulk[i].consumer);
1438 } else {
1439 qcadev->bt_power->vregs_on = true;
1440 }
1441 } else if (!on && qcadev->bt_power->vregs_on) {
1442 /* turn off regulator in reverse order */
1443 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1444 for ( ; i >= 0; i--)
1445 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1446
1447 qcadev->bt_power->vregs_on = false;
1448 }
1449
1450 return ret;
1451}
1452
1453static int qca_init_regulators(struct qca_power *qca,
1454 const struct qca_vreg *vregs, size_t num_vregs)
1455{
1456 int i;
1457
329e0989 1458 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
fa9ad876
BG
1459 sizeof(struct regulator_bulk_data),
1460 GFP_KERNEL);
1461 if (!qca->vreg_bulk)
1462 return -ENOMEM;
1463
1464 for (i = 0; i < num_vregs; i++)
1465 qca->vreg_bulk[i].supply = vregs[i].name;
1466
1467 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1468}
1469
05ba533c
TE
1470static int qca_serdev_probe(struct serdev_device *serdev)
1471{
1472 struct qca_serdev *qcadev;
fa9ad876 1473 const struct qca_vreg_data *data;
05ba533c
TE
1474 int err;
1475
1476 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1477 if (!qcadev)
1478 return -ENOMEM;
1479
1480 qcadev->serdev_hu.serdev = serdev;
fa9ad876 1481 data = of_device_get_match_data(&serdev->dev);
05ba533c 1482 serdev_device_set_drvdata(serdev, qcadev);
99c905c6
RL
1483 device_property_read_string(&serdev->dev, "firmware-name",
1484 &qcadev->firmware_name);
523760b7
HB
1485 if (data && qca_is_wcn399x(data->soc_type)) {
1486 qcadev->btsoc_type = data->soc_type;
fa9ad876
BG
1487 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1488 sizeof(struct qca_power),
1489 GFP_KERNEL);
1490 if (!qcadev->bt_power)
1491 return -ENOMEM;
1492
1493 qcadev->bt_power->dev = &serdev->dev;
1494 qcadev->bt_power->vreg_data = data;
1495 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1496 data->num_vregs);
1497 if (err) {
1498 BT_ERR("Failed to init regulators:%d", err);
1499 goto out;
1500 }
05ba533c 1501
fa9ad876 1502 qcadev->bt_power->vregs_on = false;
05ba533c 1503
fa9ad876
BG
1504 device_property_read_u32(&serdev->dev, "max-speed",
1505 &qcadev->oper_speed);
1506 if (!qcadev->oper_speed)
1507 BT_DBG("UART will pick default operating speed");
05ba533c 1508
fa9ad876
BG
1509 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1510 if (err) {
1511 BT_ERR("wcn3990 serdev registration failed");
1512 goto out;
1513 }
1514 } else {
1515 qcadev->btsoc_type = QCA_ROME;
1516 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1517 GPIOD_OUT_LOW);
1518 if (IS_ERR(qcadev->bt_en)) {
1519 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1520 return PTR_ERR(qcadev->bt_en);
1521 }
05ba533c 1522
fa9ad876
BG
1523 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1524 if (IS_ERR(qcadev->susclk)) {
1525 dev_err(&serdev->dev, "failed to acquire clk\n");
1526 return PTR_ERR(qcadev->susclk);
1527 }
05ba533c 1528
fa9ad876
BG
1529 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1530 if (err)
1531 return err;
1532
1533 err = clk_prepare_enable(qcadev->susclk);
1534 if (err)
1535 return err;
1536
1537 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1538 if (err)
1539 clk_disable_unprepare(qcadev->susclk);
1540 }
1541
1542out: return err;
05ba533c 1543
05ba533c
TE
1544}
1545
1546static void qca_serdev_remove(struct serdev_device *serdev)
1547{
1548 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1549
523760b7 1550 if (qca_is_wcn399x(qcadev->btsoc_type))
c2d78273 1551 qca_power_shutdown(&qcadev->serdev_hu);
fa9ad876
BG
1552 else
1553 clk_disable_unprepare(qcadev->susclk);
05ba533c 1554
fa9ad876 1555 hci_uart_unregister_device(&qcadev->serdev_hu);
05ba533c
TE
1556}
1557
1558static const struct of_device_id qca_bluetooth_of_match[] = {
1559 { .compatible = "qcom,qca6174-bt" },
523760b7
HB
1560 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1561 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
05ba533c
TE
1562 { /* sentinel */ }
1563};
1564MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1565
1566static struct serdev_device_driver qca_serdev_driver = {
1567 .probe = qca_serdev_probe,
1568 .remove = qca_serdev_remove,
1569 .driver = {
1570 .name = "hci_uart_qca",
1571 .of_match_table = qca_bluetooth_of_match,
1572 },
1573};
1574
0ff252c1
BYTK
1575int __init qca_init(void)
1576{
05ba533c
TE
1577 serdev_device_driver_register(&qca_serdev_driver);
1578
0ff252c1
BYTK
1579 return hci_uart_register_proto(&qca_proto);
1580}
1581
1582int __exit qca_deinit(void)
1583{
05ba533c
TE
1584 serdev_device_driver_unregister(&qca_serdev_driver);
1585
0ff252c1
BYTK
1586 return hci_uart_unregister_proto(&qca_proto);
1587}