blob: 0cfa5b831d393c9ce4a249564e59b717f9c5a1a5 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07002/*
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.
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05309 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070010 *
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.
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070016 */
17
18#include <linux/kernel.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020019#include <linux/clk.h>
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -070020#include <linux/completion.h>
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070021#include <linux/debugfs.h>
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +053022#include <linux/delay.h>
23#include <linux/device.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020024#include <linux/gpio/consumer.h>
25#include <linux/mod_devicetable.h>
26#include <linux/module.h>
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +053027#include <linux/of_device.h>
28#include <linux/platform_device.h>
29#include <linux/regulator/consumer.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020030#include <linux/serdev.h>
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053031#include <asm/unaligned.h>
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070032
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
Marcel Holtmannf81b0012015-08-30 23:05:32 +020043#define HCI_MAX_IBS_SIZE 10
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070044
Marcel Holtmannf81b0012015-08-30 23:05:32 +020045#define IBS_WAKE_RETRANS_TIMEOUT_MS 100
46#define IBS_TX_IDLE_TIMEOUT_MS 2000
Matthias Kaehlcke94d66712019-02-27 15:52:23 -080047#define CMD_TRANS_TIMEOUT_MS 100
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070048
Thierry Escande05ba5332018-03-29 21:15:24 +020049/* susclk rate */
50#define SUSCLK_RATE_32KHZ 32768
51
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053052/* Controller debug log header */
53#define QCA_DEBUG_HANDLE 0x2EDC
54
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070055enum qca_flags {
56 QCA_IBS_ENABLED,
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -070057 QCA_DROP_VENDOR_EVENT,
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070058};
59
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070060/* 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 */
Viresh Kumar621a5f72015-09-26 15:04:07 -070090 bool tx_vote; /* Clock must be on for TX */
91 bool rx_vote; /* Clock must be on for RX */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070092 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;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700102 struct completion drop_ev_comp;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700103
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
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +0530122enum qca_speed_type {
123 QCA_INIT_SPEED = 1,
124 QCA_OPER_SPEED
125};
126
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530127/*
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
Thierry Escande05ba5332018-03-29 21:15:24 +0200154struct qca_serdev {
155 struct hci_uart serdev_hu;
156 struct gpio_desc *bt_en;
157 struct clk *susclk;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530158 enum qca_btsoc_type btsoc_type;
159 struct qca_power *bt_power;
160 u32 init_speed;
161 u32 oper_speed;
Rocky Liao99c905c2019-06-06 17:40:30 +0800162 const char *firmware_name;
Thierry Escande05ba5332018-03-29 21:15:24 +0200163};
164
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530165static int qca_power_setup(struct hci_uart *hu, bool on);
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530166static void qca_power_shutdown(struct hci_uart *hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +0530167static int qca_power_off(struct hci_dev *hdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530168
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -0700169static 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
Rocky Liao99c905c2019-06-06 17:40:30 +0800184static 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
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700195static 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
Prasanna Karthikce26d812015-09-15 12:19:45 +0000265 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
266 vote ? "true" : "false");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700267
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 */
Johannes Berg634fef62017-06-16 14:29:24 +0200299 skb_put_u8(skb, cmd);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700300
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
Kees Cook04356052017-10-04 17:54:29 -0700391static void hci_ibs_tx_idle_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700392{
Kees Cook04356052017-10-04 17:54:29 -0700393 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
394 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700395 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:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000419 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700420 break;
421 }
422
423 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
424}
425
Kees Cook04356052017-10-04 17:54:29 -0700426static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700427{
Kees Cook04356052017-10-04 17:54:29 -0700428 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
429 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700430 unsigned long flags, retrans_delay;
Prasanna Karthika9137182015-09-28 08:03:24 +0000431 bool retransmit = false;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700432
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 */
Prasanna Karthika9137182015-09-28 08:03:24 +0000442 retransmit = true;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700443 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:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000457 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700458 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{
Thierry Escande05ba5332018-03-29 21:15:24 +0200470 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700471 struct qca_data *qca;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530472 int ret;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700473
474 BT_DBG("hu %p qca_open", hu);
475
Vladis Dronovb36a1552019-07-30 11:33:45 +0200476 if (!hci_uart_has_flow_control(hu))
477 return -EOPNOTSUPP;
478
Jia-Ju Bai25a13e382018-07-23 11:56:51 +0800479 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700480 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);
Bhaktipriya Shridharfac9a602016-08-30 22:42:53 +0530486 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700487 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;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700499 init_completion(&qca->drop_ev_comp);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700500
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
505 /* clocks actually on, but we start votes off */
506 qca->tx_vote = false;
507 qca->rx_vote = false;
508 qca->flags = 0;
509
510 qca->ibs_sent_wacks = 0;
511 qca->ibs_sent_slps = 0;
512 qca->ibs_sent_wakes = 0;
513 qca->ibs_recv_wacks = 0;
514 qca->ibs_recv_slps = 0;
515 qca->ibs_recv_wakes = 0;
516 qca->vote_last_jif = jiffies;
517 qca->vote_on_ms = 0;
518 qca->vote_off_ms = 0;
519 qca->votes_on = 0;
520 qca->votes_off = 0;
521 qca->tx_votes_on = 0;
522 qca->tx_votes_off = 0;
523 qca->rx_votes_on = 0;
524 qca->rx_votes_off = 0;
525
526 hu->priv = qca;
527
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530528 if (hu->serdev) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530529
530 qcadev = serdev_device_get_drvdata(hu->serdev);
Harish Bandi523760b2019-04-26 19:26:01 +0530531 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530532 gpiod_set_value_cansleep(qcadev->bt_en, 1);
Balakrishna Godavarthi7f09d5a2019-04-01 15:19:08 +0530533 /* Controller needs time to bootup. */
534 msleep(150);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530535 } else {
536 hu->init_speed = qcadev->init_speed;
537 hu->oper_speed = qcadev->oper_speed;
538 ret = qca_power_setup(hu, true);
539 if (ret) {
540 destroy_workqueue(qca->workqueue);
541 kfree_skb(qca->rx_skb);
542 hu->priv = NULL;
543 kfree(qca);
544 return ret;
545 }
546 }
547 }
548
Kees Cook04356052017-10-04 17:54:29 -0700549 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700550 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
551
Kees Cook04356052017-10-04 17:54:29 -0700552 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700553 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
554
555 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
556 qca->tx_idle_delay, qca->wake_retrans);
557
558 return 0;
559}
560
561static void qca_debugfs_init(struct hci_dev *hdev)
562{
563 struct hci_uart *hu = hci_get_drvdata(hdev);
564 struct qca_data *qca = hu->priv;
565 struct dentry *ibs_dir;
566 umode_t mode;
567
568 if (!hdev->debugfs)
569 return;
570
571 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
572
573 /* read only */
574 mode = S_IRUGO;
575 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
576 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
577 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
578 &qca->ibs_sent_slps);
579 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
580 &qca->ibs_sent_wakes);
581 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
582 &qca->ibs_sent_wacks);
583 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
584 &qca->ibs_recv_slps);
585 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
586 &qca->ibs_recv_wakes);
587 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
588 &qca->ibs_recv_wacks);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700589 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700590 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
591 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700592 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700593 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
594 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
595 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
596 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
597 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
598 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
599
600 /* read/write */
601 mode = S_IRUGO | S_IWUSR;
602 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
603 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
604 &qca->tx_idle_delay);
605}
606
607/* Flush protocol data */
608static int qca_flush(struct hci_uart *hu)
609{
610 struct qca_data *qca = hu->priv;
611
612 BT_DBG("hu %p qca flush", hu);
613
614 skb_queue_purge(&qca->tx_wait_q);
615 skb_queue_purge(&qca->txq);
616
617 return 0;
618}
619
620/* Close protocol */
621static int qca_close(struct hci_uart *hu)
622{
Thierry Escande05ba5332018-03-29 21:15:24 +0200623 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700624 struct qca_data *qca = hu->priv;
625
626 BT_DBG("hu %p qca close", hu);
627
628 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
629
630 skb_queue_purge(&qca->tx_wait_q);
631 skb_queue_purge(&qca->txq);
632 del_timer(&qca->tx_idle_timer);
633 del_timer(&qca->wake_retrans_timer);
634 destroy_workqueue(qca->workqueue);
635 qca->hu = NULL;
636
Thierry Escande05ba5332018-03-29 21:15:24 +0200637 if (hu->serdev) {
Thierry Escande05ba5332018-03-29 21:15:24 +0200638 qcadev = serdev_device_get_drvdata(hu->serdev);
Harish Bandi523760b2019-04-26 19:26:01 +0530639 if (qca_is_wcn399x(qcadev->btsoc_type))
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530640 qca_power_shutdown(hu);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530641 else
642 gpiod_set_value_cansleep(qcadev->bt_en, 0);
643
Thierry Escande05ba5332018-03-29 21:15:24 +0200644 }
645
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700646 kfree_skb(qca->rx_skb);
647
648 hu->priv = NULL;
649
650 kfree(qca);
651
652 return 0;
653}
654
655/* Called upon a wake-up-indication from the device.
656 */
657static void device_want_to_wakeup(struct hci_uart *hu)
658{
659 unsigned long flags;
660 struct qca_data *qca = hu->priv;
661
662 BT_DBG("hu %p want to wake up", hu);
663
664 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
665
666 qca->ibs_recv_wakes++;
667
668 switch (qca->rx_ibs_state) {
669 case HCI_IBS_RX_ASLEEP:
670 /* Make sure clock is on - we may have turned clock off since
671 * receiving the wake up indicator awake rx clock.
672 */
673 queue_work(qca->workqueue, &qca->ws_awake_rx);
674 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
675 return;
676
677 case HCI_IBS_RX_AWAKE:
678 /* Always acknowledge device wake up,
679 * sending IBS message doesn't count as TX ON.
680 */
681 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
682 BT_ERR("Failed to acknowledge device wake up");
683 break;
684 }
685 qca->ibs_sent_wacks++;
686 break;
687
688 default:
689 /* Any other state is illegal */
690 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
691 qca->rx_ibs_state);
692 break;
693 }
694
695 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
696
697 /* Actually send the packets */
698 hci_uart_tx_wakeup(hu);
699}
700
701/* Called upon a sleep-indication from the device.
702 */
703static void device_want_to_sleep(struct hci_uart *hu)
704{
705 unsigned long flags;
706 struct qca_data *qca = hu->priv;
707
708 BT_DBG("hu %p want to sleep", hu);
709
710 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
711
712 qca->ibs_recv_slps++;
713
714 switch (qca->rx_ibs_state) {
715 case HCI_IBS_RX_AWAKE:
716 /* Update state */
717 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
718 /* Vote off rx clock under workqueue */
719 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
720 break;
721
722 case HCI_IBS_RX_ASLEEP:
723 /* Fall through */
724
725 default:
726 /* Any other state is illegal */
727 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
728 qca->rx_ibs_state);
729 break;
730 }
731
732 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
733}
734
735/* Called upon wake-up-acknowledgement from the device
736 */
737static void device_woke_up(struct hci_uart *hu)
738{
739 unsigned long flags, idle_delay;
740 struct qca_data *qca = hu->priv;
741 struct sk_buff *skb = NULL;
742
743 BT_DBG("hu %p woke up", hu);
744
745 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
746
747 qca->ibs_recv_wacks++;
748
749 switch (qca->tx_ibs_state) {
750 case HCI_IBS_TX_AWAKE:
751 /* Expect one if we send 2 WAKEs */
752 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
753 qca->tx_ibs_state);
754 break;
755
756 case HCI_IBS_TX_WAKING:
757 /* Send pending packets */
758 while ((skb = skb_dequeue(&qca->tx_wait_q)))
759 skb_queue_tail(&qca->txq, skb);
760
761 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
762 del_timer(&qca->wake_retrans_timer);
763 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
764 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
765 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
766 break;
767
768 case HCI_IBS_TX_ASLEEP:
769 /* Fall through */
770
771 default:
772 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
773 qca->tx_ibs_state);
774 break;
775 }
776
777 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
778
779 /* Actually send the packets */
780 hci_uart_tx_wakeup(hu);
781}
782
783/* Enqueue frame for transmittion (padding, crc, etc) may be called from
784 * two simultaneous tasklets.
785 */
786static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
787{
788 unsigned long flags = 0, idle_delay;
789 struct qca_data *qca = hu->priv;
790
791 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
792 qca->tx_ibs_state);
793
794 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100795 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700796
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530797 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
798
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700799 /* Don't go to sleep in middle of patch download or
800 * Out-Of-Band(GPIOs control) sleep is selected.
801 */
Matthias Kaehlcke62a91992019-04-29 16:21:30 -0700802 if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700803 skb_queue_tail(&qca->txq, skb);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530804 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700805 return 0;
806 }
807
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700808 /* Act according to current state */
809 switch (qca->tx_ibs_state) {
810 case HCI_IBS_TX_AWAKE:
811 BT_DBG("Device awake, sending normally");
812 skb_queue_tail(&qca->txq, skb);
813 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
814 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
815 break;
816
817 case HCI_IBS_TX_ASLEEP:
818 BT_DBG("Device asleep, waking up and queueing packet");
819 /* Save packet for later */
820 skb_queue_tail(&qca->tx_wait_q, skb);
821
822 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
823 /* Schedule a work queue to wake up device */
824 queue_work(qca->workqueue, &qca->ws_awake_device);
825 break;
826
827 case HCI_IBS_TX_WAKING:
828 BT_DBG("Device waking up, queueing packet");
829 /* Transient state; just keep packet for later */
830 skb_queue_tail(&qca->tx_wait_q, skb);
831 break;
832
833 default:
834 BT_ERR("Illegal tx state: %d (losing packet)",
835 qca->tx_ibs_state);
836 kfree_skb(skb);
837 break;
838 }
839
840 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
841
842 return 0;
843}
844
845static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
846{
847 struct hci_uart *hu = hci_get_drvdata(hdev);
848
849 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
850
851 device_want_to_sleep(hu);
852
853 kfree_skb(skb);
854 return 0;
855}
856
857static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
858{
859 struct hci_uart *hu = hci_get_drvdata(hdev);
860
861 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
862
863 device_want_to_wakeup(hu);
864
865 kfree_skb(skb);
866 return 0;
867}
868
869static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
870{
871 struct hci_uart *hu = hci_get_drvdata(hdev);
872
873 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
874
875 device_woke_up(hu);
876
877 kfree_skb(skb);
878 return 0;
879}
880
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530881static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
882{
883 /* We receive debug logs from chip as an ACL packets.
884 * Instead of sending the data to ACL to decode the
885 * received data, we are pushing them to the above layers
886 * as a diagnostic packet.
887 */
888 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
889 return hci_recv_diag(hdev, skb);
890
891 return hci_recv_frame(hdev, skb);
892}
893
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700894static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
895{
896 struct hci_uart *hu = hci_get_drvdata(hdev);
897 struct qca_data *qca = hu->priv;
898
899 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
900 struct hci_event_hdr *hdr = (void *)skb->data;
901
902 /* For the WCN3990 the vendor command for a baudrate change
903 * isn't sent as synchronous HCI command, because the
904 * controller sends the corresponding vendor event with the
905 * new baudrate. The event is received and properly decoded
906 * after changing the baudrate of the host port. It needs to
907 * be dropped, otherwise it can be misinterpreted as
908 * response to a later firmware download command (also a
909 * vendor command).
910 */
911
912 if (hdr->evt == HCI_EV_VENDOR)
913 complete(&qca->drop_ev_comp);
914
Wei Yongjun4974c832019-07-09 01:35:30 +0000915 kfree_skb(skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700916
917 return 0;
918 }
919
920 return hci_recv_frame(hdev, skb);
921}
922
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700923#define QCA_IBS_SLEEP_IND_EVENT \
924 .type = HCI_IBS_SLEEP_IND, \
925 .hlen = 0, \
926 .loff = 0, \
927 .lsize = 0, \
928 .maxlen = HCI_MAX_IBS_SIZE
929
930#define QCA_IBS_WAKE_IND_EVENT \
931 .type = HCI_IBS_WAKE_IND, \
932 .hlen = 0, \
933 .loff = 0, \
934 .lsize = 0, \
935 .maxlen = HCI_MAX_IBS_SIZE
936
937#define QCA_IBS_WAKE_ACK_EVENT \
938 .type = HCI_IBS_WAKE_ACK, \
939 .hlen = 0, \
940 .loff = 0, \
941 .lsize = 0, \
942 .maxlen = HCI_MAX_IBS_SIZE
943
944static const struct h4_recv_pkt qca_recv_pkts[] = {
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530945 { H4_RECV_ACL, .recv = qca_recv_acl_data },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700946 { H4_RECV_SCO, .recv = hci_recv_frame },
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700947 { H4_RECV_EVENT, .recv = qca_recv_event },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700948 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
949 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
950 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
951};
952
953static int qca_recv(struct hci_uart *hu, const void *data, int count)
954{
955 struct qca_data *qca = hu->priv;
956
957 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
958 return -EUNATCH;
959
960 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
961 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
962 if (IS_ERR(qca->rx_skb)) {
963 int err = PTR_ERR(qca->rx_skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100964 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700965 qca->rx_skb = NULL;
966 return err;
967 }
968
969 return count;
970}
971
972static struct sk_buff *qca_dequeue(struct hci_uart *hu)
973{
974 struct qca_data *qca = hu->priv;
975
976 return skb_dequeue(&qca->txq);
977}
978
979static uint8_t qca_get_baudrate_value(int speed)
980{
Prasanna Karthikce26d812015-09-15 12:19:45 +0000981 switch (speed) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700982 case 9600:
983 return QCA_BAUDRATE_9600;
984 case 19200:
985 return QCA_BAUDRATE_19200;
986 case 38400:
987 return QCA_BAUDRATE_38400;
988 case 57600:
989 return QCA_BAUDRATE_57600;
990 case 115200:
991 return QCA_BAUDRATE_115200;
992 case 230400:
993 return QCA_BAUDRATE_230400;
994 case 460800:
995 return QCA_BAUDRATE_460800;
996 case 500000:
997 return QCA_BAUDRATE_500000;
998 case 921600:
999 return QCA_BAUDRATE_921600;
1000 case 1000000:
1001 return QCA_BAUDRATE_1000000;
1002 case 2000000:
1003 return QCA_BAUDRATE_2000000;
1004 case 3000000:
1005 return QCA_BAUDRATE_3000000;
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301006 case 3200000:
1007 return QCA_BAUDRATE_3200000;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001008 case 3500000:
1009 return QCA_BAUDRATE_3500000;
1010 default:
1011 return QCA_BAUDRATE_115200;
1012 }
1013}
1014
1015static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1016{
1017 struct hci_uart *hu = hci_get_drvdata(hdev);
1018 struct qca_data *qca = hu->priv;
1019 struct sk_buff *skb;
1020 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1021
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301022 if (baudrate > QCA_BAUDRATE_3200000)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001023 return -EINVAL;
1024
1025 cmd[4] = baudrate;
1026
Jia-Ju Bai25a13e382018-07-23 11:56:51 +08001027 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001028 if (!skb) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001029 bt_dev_err(hdev, "Failed to allocate baudrate packet");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001030 return -ENOMEM;
1031 }
1032
1033 /* Assign commands to change baudrate and packet type. */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001034 skb_put_data(skb, cmd, sizeof(cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001035 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001036
1037 skb_queue_tail(&qca->txq, skb);
1038 hci_uart_tx_wakeup(hu);
1039
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001040 /* Wait for the baudrate change request to be sent */
1041
1042 while (!skb_queue_empty(&qca->txq))
1043 usleep_range(100, 200);
1044
Matthias Kaehlckeecf2b762019-04-23 11:16:52 -07001045 if (hu->serdev)
1046 serdev_device_wait_until_sent(hu->serdev,
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001047 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1048
1049 /* Give the controller time to process the request */
Harish Bandi523760b2019-04-26 19:26:01 +05301050 if (qca_is_wcn399x(qca_soc_type(hu)))
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001051 msleep(10);
1052 else
1053 msleep(300);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001054
1055 return 0;
1056}
1057
Thierry Escande05ba5332018-03-29 21:15:24 +02001058static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1059{
1060 if (hu->serdev)
1061 serdev_device_set_baudrate(hu->serdev, speed);
1062 else
1063 hci_uart_set_baudrate(hu, speed);
1064}
1065
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001066static int qca_send_power_pulse(struct hci_uart *hu, bool on)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301067{
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301068 int ret;
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001069 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001070 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301071
1072 /* These power pulses are single byte command which are sent
1073 * at required baudrate to wcn3990. On wcn3990, we have an external
1074 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1075 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1076 * and also we use the same power inputs to turn on and off for
1077 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1078 * we send a power on pulse at 115200 bps. This algorithm will help to
1079 * save power. Disabling hardware flow control is mandatory while
1080 * sending power pulses to SoC.
1081 */
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301082 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301083
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301084 serdev_device_write_flush(hu->serdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301085 hci_uart_set_flow_control(hu, true);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301086 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1087 if (ret < 0) {
1088 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1089 return ret;
1090 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301091
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301092 serdev_device_wait_until_sent(hu->serdev, timeout);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301093 hci_uart_set_flow_control(hu, false);
1094
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001095 /* Give to controller time to boot/shutdown */
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001096 if (on)
1097 msleep(100);
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001098 else
1099 msleep(10);
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001100
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301101 return 0;
1102}
1103
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301104static unsigned int qca_get_speed(struct hci_uart *hu,
1105 enum qca_speed_type speed_type)
1106{
1107 unsigned int speed = 0;
1108
1109 if (speed_type == QCA_INIT_SPEED) {
1110 if (hu->init_speed)
1111 speed = hu->init_speed;
1112 else if (hu->proto->init_speed)
1113 speed = hu->proto->init_speed;
1114 } else {
1115 if (hu->oper_speed)
1116 speed = hu->oper_speed;
1117 else if (hu->proto->oper_speed)
1118 speed = hu->proto->oper_speed;
1119 }
1120
1121 return speed;
1122}
1123
1124static int qca_check_speeds(struct hci_uart *hu)
1125{
Harish Bandi523760b2019-04-26 19:26:01 +05301126 if (qca_is_wcn399x(qca_soc_type(hu))) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301127 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1128 !qca_get_speed(hu, QCA_OPER_SPEED))
1129 return -EINVAL;
1130 } else {
1131 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1132 !qca_get_speed(hu, QCA_OPER_SPEED))
1133 return -EINVAL;
1134 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301135
1136 return 0;
1137}
1138
1139static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1140{
1141 unsigned int speed, qca_baudrate;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001142 struct qca_data *qca = hu->priv;
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301143 int ret = 0;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301144
1145 if (speed_type == QCA_INIT_SPEED) {
1146 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1147 if (speed)
1148 host_set_baudrate(hu, speed);
1149 } else {
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001150 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1151
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301152 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1153 if (!speed)
1154 return 0;
1155
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301156 /* Disable flow control for wcn3990 to deassert RTS while
1157 * changing the baudrate of chip and host.
1158 */
Harish Bandi523760b2019-04-26 19:26:01 +05301159 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301160 hci_uart_set_flow_control(hu, true);
1161
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001162 if (soc_type == QCA_WCN3990) {
1163 reinit_completion(&qca->drop_ev_comp);
1164 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1165 }
1166
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301167 qca_baudrate = qca_get_baudrate_value(speed);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301168 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301169 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1170 if (ret)
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301171 goto error;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301172
1173 host_set_baudrate(hu, speed);
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301174
1175error:
Harish Bandi523760b2019-04-26 19:26:01 +05301176 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301177 hci_uart_set_flow_control(hu, false);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001178
1179 if (soc_type == QCA_WCN3990) {
1180 /* Wait for the controller to send the vendor event
1181 * for the baudrate change command.
1182 */
1183 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1184 msecs_to_jiffies(100))) {
1185 bt_dev_err(hu->hdev,
1186 "Failed to change controller baudrate\n");
1187 ret = -ETIMEDOUT;
1188 }
1189
1190 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1191 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301192 }
1193
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301194 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301195}
1196
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301197static int qca_wcn3990_init(struct hci_uart *hu)
1198{
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301199 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301200 int ret;
1201
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301202 /* Check for vregs status, may be hci down has turned
1203 * off the voltage regulator.
1204 */
1205 qcadev = serdev_device_get_drvdata(hu->serdev);
1206 if (!qcadev->bt_power->vregs_on) {
1207 serdev_device_close(hu->serdev);
1208 ret = qca_power_setup(hu, true);
1209 if (ret)
1210 return ret;
1211
1212 ret = serdev_device_open(hu->serdev);
1213 if (ret) {
1214 bt_dev_err(hu->hdev, "failed to open port");
1215 return ret;
1216 }
1217 }
1218
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301219 /* Forcefully enable wcn3990 to enter in to boot mode. */
1220 host_set_baudrate(hu, 2400);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001221 ret = qca_send_power_pulse(hu, false);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301222 if (ret)
1223 return ret;
1224
1225 qca_set_speed(hu, QCA_INIT_SPEED);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001226 ret = qca_send_power_pulse(hu, true);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301227 if (ret)
1228 return ret;
1229
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301230 /* Now the device is in ready state to communicate with host.
1231 * To sync host with device we need to reopen port.
1232 * Without this, we will have RTS and CTS synchronization
1233 * issues.
1234 */
1235 serdev_device_close(hu->serdev);
1236 ret = serdev_device_open(hu->serdev);
1237 if (ret) {
1238 bt_dev_err(hu->hdev, "failed to open port");
1239 return ret;
1240 }
1241
1242 hci_uart_set_flow_control(hu, false);
1243
1244 return 0;
1245}
1246
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001247static int qca_setup(struct hci_uart *hu)
1248{
1249 struct hci_dev *hdev = hu->hdev;
1250 struct qca_data *qca = hu->priv;
1251 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001252 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Rocky Liao99c905c2019-06-06 17:40:30 +08001253 const char *firmware_name = qca_get_firmware_name(hu);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001254 int ret;
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301255 int soc_ver = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001256
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301257 ret = qca_check_speeds(hu);
1258 if (ret)
1259 return ret;
1260
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001261 /* Patch downloading has to be done without IBS mode */
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001262 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001263
Harish Bandi523760b2019-04-26 19:26:01 +05301264 if (qca_is_wcn399x(soc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301265 bt_dev_info(hdev, "setting up wcn3990");
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301266
1267 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1268 * setup for every hci up.
1269 */
1270 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
Matthias Kaehlcke5971752d2019-02-19 12:05:59 -08001271 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301272 hu->hdev->shutdown = qca_power_off;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301273 ret = qca_wcn3990_init(hu);
1274 if (ret)
1275 return ret;
1276
1277 ret = qca_read_soc_version(hdev, &soc_ver);
1278 if (ret)
1279 return ret;
1280 } else {
1281 bt_dev_info(hdev, "ROME setup");
1282 qca_set_speed(hu, QCA_INIT_SPEED);
1283 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001284
1285 /* Setup user speed if needed */
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301286 speed = qca_get_speed(hu, QCA_OPER_SPEED);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001287 if (speed) {
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301288 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1289 if (ret)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001290 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301291
1292 qca_baudrate = qca_get_baudrate_value(speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001293 }
1294
Harish Bandi523760b2019-04-26 19:26:01 +05301295 if (!qca_is_wcn399x(soc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301296 /* Get QCA version information */
1297 ret = qca_read_soc_version(hdev, &soc_ver);
1298 if (ret)
1299 return ret;
1300 }
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301301
1302 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001303 /* Setup patch / NVM configurations */
Rocky Liao99c905c2019-06-06 17:40:30 +08001304 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1305 firmware_name);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001306 if (!ret) {
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001307 set_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001308 qca_debugfs_init(hdev);
Loic Poulainba8f3592017-11-06 12:16:56 +01001309 } else if (ret == -ENOENT) {
1310 /* No patch/nvm-config found, run with original fw/config */
1311 ret = 0;
Amit Pundir7dc5fe02018-04-16 12:10:24 +05301312 } else if (ret == -EAGAIN) {
1313 /*
1314 * Userspace firmware loader will return -EAGAIN in case no
1315 * patch/nvm-config is found, so run with original fw/config.
1316 */
1317 ret = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001318 }
1319
1320 /* Setup bdaddr */
Harish Bandi523760b2019-04-26 19:26:01 +05301321 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi5c0a10012019-01-16 18:01:15 +05301322 hu->hdev->set_bdaddr = qca_set_bdaddr;
1323 else
1324 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001325
1326 return ret;
1327}
1328
1329static struct hci_uart_proto qca_proto = {
1330 .id = HCI_UART_QCA,
1331 .name = "QCA",
Marcel Holtmannaee61f72015-10-20 21:30:45 +02001332 .manufacturer = 29,
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001333 .init_speed = 115200,
1334 .oper_speed = 3000000,
1335 .open = qca_open,
1336 .close = qca_close,
1337 .flush = qca_flush,
1338 .setup = qca_setup,
1339 .recv = qca_recv,
1340 .enqueue = qca_enqueue,
1341 .dequeue = qca_dequeue,
1342};
1343
Harish Bandi523760b2019-04-26 19:26:01 +05301344static const struct qca_vreg_data qca_soc_data_wcn3990 = {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301345 .soc_type = QCA_WCN3990,
1346 .vregs = (struct qca_vreg []) {
1347 { "vddio", 1800000, 1900000, 15000 },
1348 { "vddxo", 1800000, 1900000, 80000 },
1349 { "vddrf", 1300000, 1350000, 300000 },
1350 { "vddch0", 3300000, 3400000, 450000 },
1351 },
1352 .num_vregs = 4,
1353};
1354
Harish Bandi523760b2019-04-26 19:26:01 +05301355static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1356 .soc_type = QCA_WCN3998,
1357 .vregs = (struct qca_vreg []) {
1358 { "vddio", 1800000, 1900000, 10000 },
1359 { "vddxo", 1800000, 1900000, 80000 },
1360 { "vddrf", 1300000, 1352000, 300000 },
1361 { "vddch0", 3300000, 3300000, 450000 },
1362 },
1363 .num_vregs = 4,
1364};
1365
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301366static void qca_power_shutdown(struct hci_uart *hu)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301367{
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301368 struct qca_data *qca = hu->priv;
1369 unsigned long flags;
1370
1371 /* From this point we go into power off state. But serial port is
1372 * still open, stop queueing the IBS data and flush all the buffered
1373 * data in skb's.
1374 */
1375 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001376 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301377 qca_flush(hu);
1378 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1379
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301380 host_set_baudrate(hu, 2400);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001381 qca_send_power_pulse(hu, false);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301382 qca_power_setup(hu, false);
1383}
1384
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301385static int qca_power_off(struct hci_dev *hdev)
1386{
1387 struct hci_uart *hu = hci_get_drvdata(hdev);
1388
Harish Bandia2780882019-07-12 10:39:40 +05301389 /* Perform pre shutdown command */
1390 qca_send_pre_shutdown_cmd(hdev);
1391
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301392 qca_power_shutdown(hu);
1393 return 0;
1394}
1395
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301396static int qca_enable_regulator(struct qca_vreg vregs,
1397 struct regulator *regulator)
1398{
1399 int ret;
1400
1401 ret = regulator_set_voltage(regulator, vregs.min_uV,
1402 vregs.max_uV);
1403 if (ret)
1404 return ret;
1405
1406 if (vregs.load_uA)
1407 ret = regulator_set_load(regulator,
1408 vregs.load_uA);
1409
1410 if (ret)
1411 return ret;
1412
1413 return regulator_enable(regulator);
1414
1415}
1416
1417static void qca_disable_regulator(struct qca_vreg vregs,
1418 struct regulator *regulator)
1419{
1420 regulator_disable(regulator);
1421 regulator_set_voltage(regulator, 0, vregs.max_uV);
1422 if (vregs.load_uA)
1423 regulator_set_load(regulator, 0);
1424
1425}
1426
1427static int qca_power_setup(struct hci_uart *hu, bool on)
1428{
1429 struct qca_vreg *vregs;
1430 struct regulator_bulk_data *vreg_bulk;
1431 struct qca_serdev *qcadev;
1432 int i, num_vregs, ret = 0;
1433
1434 qcadev = serdev_device_get_drvdata(hu->serdev);
1435 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1436 !qcadev->bt_power->vreg_bulk)
1437 return -EINVAL;
1438
1439 vregs = qcadev->bt_power->vreg_data->vregs;
1440 vreg_bulk = qcadev->bt_power->vreg_bulk;
1441 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1442 BT_DBG("on: %d", on);
1443 if (on && !qcadev->bt_power->vregs_on) {
1444 for (i = 0; i < num_vregs; i++) {
1445 ret = qca_enable_regulator(vregs[i],
1446 vreg_bulk[i].consumer);
1447 if (ret)
1448 break;
1449 }
1450
1451 if (ret) {
1452 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1453 /* turn off regulators which are enabled */
1454 for (i = i - 1; i >= 0; i--)
1455 qca_disable_regulator(vregs[i],
1456 vreg_bulk[i].consumer);
1457 } else {
1458 qcadev->bt_power->vregs_on = true;
1459 }
1460 } else if (!on && qcadev->bt_power->vregs_on) {
1461 /* turn off regulator in reverse order */
1462 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1463 for ( ; i >= 0; i--)
1464 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1465
1466 qcadev->bt_power->vregs_on = false;
1467 }
1468
1469 return ret;
1470}
1471
1472static int qca_init_regulators(struct qca_power *qca,
1473 const struct qca_vreg *vregs, size_t num_vregs)
1474{
1475 int i;
1476
Kees Cook329e0982018-10-05 16:21:46 -07001477 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301478 sizeof(struct regulator_bulk_data),
1479 GFP_KERNEL);
1480 if (!qca->vreg_bulk)
1481 return -ENOMEM;
1482
1483 for (i = 0; i < num_vregs; i++)
1484 qca->vreg_bulk[i].supply = vregs[i].name;
1485
1486 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1487}
1488
Thierry Escande05ba5332018-03-29 21:15:24 +02001489static int qca_serdev_probe(struct serdev_device *serdev)
1490{
1491 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301492 const struct qca_vreg_data *data;
Thierry Escande05ba5332018-03-29 21:15:24 +02001493 int err;
1494
1495 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1496 if (!qcadev)
1497 return -ENOMEM;
1498
1499 qcadev->serdev_hu.serdev = serdev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301500 data = of_device_get_match_data(&serdev->dev);
Thierry Escande05ba5332018-03-29 21:15:24 +02001501 serdev_device_set_drvdata(serdev, qcadev);
Rocky Liao99c905c2019-06-06 17:40:30 +08001502 device_property_read_string(&serdev->dev, "firmware-name",
1503 &qcadev->firmware_name);
Harish Bandi523760b2019-04-26 19:26:01 +05301504 if (data && qca_is_wcn399x(data->soc_type)) {
1505 qcadev->btsoc_type = data->soc_type;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301506 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1507 sizeof(struct qca_power),
1508 GFP_KERNEL);
1509 if (!qcadev->bt_power)
1510 return -ENOMEM;
Thierry Escande05ba5332018-03-29 21:15:24 +02001511
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301512 qcadev->bt_power->dev = &serdev->dev;
1513 qcadev->bt_power->vreg_data = data;
1514 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1515 data->num_vregs);
1516 if (err) {
1517 BT_ERR("Failed to init regulators:%d", err);
1518 goto out;
1519 }
1520
1521 qcadev->bt_power->vregs_on = false;
1522
1523 device_property_read_u32(&serdev->dev, "max-speed",
1524 &qcadev->oper_speed);
1525 if (!qcadev->oper_speed)
1526 BT_DBG("UART will pick default operating speed");
1527
1528 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1529 if (err) {
1530 BT_ERR("wcn3990 serdev registration failed");
1531 goto out;
1532 }
1533 } else {
1534 qcadev->btsoc_type = QCA_ROME;
1535 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1536 GPIOD_OUT_LOW);
1537 if (IS_ERR(qcadev->bt_en)) {
1538 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1539 return PTR_ERR(qcadev->bt_en);
1540 }
1541
1542 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1543 if (IS_ERR(qcadev->susclk)) {
1544 dev_err(&serdev->dev, "failed to acquire clk\n");
1545 return PTR_ERR(qcadev->susclk);
1546 }
1547
1548 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1549 if (err)
1550 return err;
1551
1552 err = clk_prepare_enable(qcadev->susclk);
1553 if (err)
1554 return err;
1555
1556 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1557 if (err)
1558 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02001559 }
1560
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301561out: return err;
Thierry Escande05ba5332018-03-29 21:15:24 +02001562
Thierry Escande05ba5332018-03-29 21:15:24 +02001563}
1564
1565static void qca_serdev_remove(struct serdev_device *serdev)
1566{
1567 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1568
Harish Bandi523760b2019-04-26 19:26:01 +05301569 if (qca_is_wcn399x(qcadev->btsoc_type))
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301570 qca_power_shutdown(&qcadev->serdev_hu);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301571 else
1572 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02001573
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301574 hci_uart_unregister_device(&qcadev->serdev_hu);
Thierry Escande05ba5332018-03-29 21:15:24 +02001575}
1576
1577static const struct of_device_id qca_bluetooth_of_match[] = {
1578 { .compatible = "qcom,qca6174-bt" },
Harish Bandi523760b2019-04-26 19:26:01 +05301579 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1580 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
Thierry Escande05ba5332018-03-29 21:15:24 +02001581 { /* sentinel */ }
1582};
1583MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1584
1585static struct serdev_device_driver qca_serdev_driver = {
1586 .probe = qca_serdev_probe,
1587 .remove = qca_serdev_remove,
1588 .driver = {
1589 .name = "hci_uart_qca",
1590 .of_match_table = qca_bluetooth_of_match,
1591 },
1592};
1593
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001594int __init qca_init(void)
1595{
Thierry Escande05ba5332018-03-29 21:15:24 +02001596 serdev_device_driver_register(&qca_serdev_driver);
1597
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001598 return hci_uart_register_proto(&qca_proto);
1599}
1600
1601int __exit qca_deinit(void)
1602{
Thierry Escande05ba5332018-03-29 21:15:24 +02001603 serdev_device_driver_unregister(&qca_serdev_driver);
1604
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001605 return hci_uart_unregister_proto(&qca_proto);
1606}