blob: 26efc2ef98d9a80a28686c3511978aabe10f547e [file] [log] [blame]
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001/*
2 * Bluetooth Software UART Qualcomm protocol
3 *
4 * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management
5 * protocol extension to H4.
6 *
7 * Copyright (C) 2007 Texas Instruments, Inc.
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05308 * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07009 *
10 * Acknowledgements:
11 * This file is based on hci_ll.c, which was...
12 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
13 * which was in turn based on hci_h4.c, which was written
14 * by Maxim Krasnyansky and Marcel Holtmann.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <linux/kernel.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020032#include <linux/clk.h>
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070033#include <linux/debugfs.h>
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +053034#include <linux/delay.h>
35#include <linux/device.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020036#include <linux/gpio/consumer.h>
37#include <linux/mod_devicetable.h>
38#include <linux/module.h>
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +053039#include <linux/of_device.h>
40#include <linux/platform_device.h>
41#include <linux/regulator/consumer.h>
Thierry Escande05ba5332018-03-29 21:15:24 +020042#include <linux/serdev.h>
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053043#include <asm/unaligned.h>
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070044
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
47
48#include "hci_uart.h"
49#include "btqca.h"
50
51/* HCI_IBS protocol messages */
52#define HCI_IBS_SLEEP_IND 0xFE
53#define HCI_IBS_WAKE_IND 0xFD
54#define HCI_IBS_WAKE_ACK 0xFC
Marcel Holtmannf81b0012015-08-30 23:05:32 +020055#define HCI_MAX_IBS_SIZE 10
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070056
57/* Controller states */
58#define STATE_IN_BAND_SLEEP_ENABLED 1
59
Marcel Holtmannf81b0012015-08-30 23:05:32 +020060#define IBS_WAKE_RETRANS_TIMEOUT_MS 100
61#define IBS_TX_IDLE_TIMEOUT_MS 2000
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070062#define BAUDRATE_SETTLE_TIMEOUT_MS 300
Balakrishna Godavarthif9558272019-02-04 20:36:41 +053063#define POWER_PULSE_TRANS_TIMEOUT_MS 100
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070064
Thierry Escande05ba5332018-03-29 21:15:24 +020065/* susclk rate */
66#define SUSCLK_RATE_32KHZ 32768
67
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053068/* Controller debug log header */
69#define QCA_DEBUG_HANDLE 0x2EDC
70
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070071/* HCI_IBS transmit side sleep protocol states */
72enum tx_ibs_states {
73 HCI_IBS_TX_ASLEEP,
74 HCI_IBS_TX_WAKING,
75 HCI_IBS_TX_AWAKE,
76};
77
78/* HCI_IBS receive side sleep protocol states */
79enum rx_states {
80 HCI_IBS_RX_ASLEEP,
81 HCI_IBS_RX_AWAKE,
82};
83
84/* HCI_IBS transmit and receive side clock state vote */
85enum hci_ibs_clock_state_vote {
86 HCI_IBS_VOTE_STATS_UPDATE,
87 HCI_IBS_TX_VOTE_CLOCK_ON,
88 HCI_IBS_TX_VOTE_CLOCK_OFF,
89 HCI_IBS_RX_VOTE_CLOCK_ON,
90 HCI_IBS_RX_VOTE_CLOCK_OFF,
91};
92
93struct qca_data {
94 struct hci_uart *hu;
95 struct sk_buff *rx_skb;
96 struct sk_buff_head txq;
97 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
98 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
99 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
100 u8 rx_ibs_state; /* HCI_IBS receive side power state */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700101 bool tx_vote; /* Clock must be on for TX */
102 bool rx_vote; /* Clock must be on for RX */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700103 struct timer_list tx_idle_timer;
104 u32 tx_idle_delay;
105 struct timer_list wake_retrans_timer;
106 u32 wake_retrans;
107 struct workqueue_struct *workqueue;
108 struct work_struct ws_awake_rx;
109 struct work_struct ws_awake_device;
110 struct work_struct ws_rx_vote_off;
111 struct work_struct ws_tx_vote_off;
112 unsigned long flags;
113
114 /* For debugging purpose */
115 u64 ibs_sent_wacks;
116 u64 ibs_sent_slps;
117 u64 ibs_sent_wakes;
118 u64 ibs_recv_wacks;
119 u64 ibs_recv_slps;
120 u64 ibs_recv_wakes;
121 u64 vote_last_jif;
122 u32 vote_on_ms;
123 u32 vote_off_ms;
124 u64 tx_votes_on;
125 u64 rx_votes_on;
126 u64 tx_votes_off;
127 u64 rx_votes_off;
128 u64 votes_on;
129 u64 votes_off;
130};
131
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +0530132enum qca_speed_type {
133 QCA_INIT_SPEED = 1,
134 QCA_OPER_SPEED
135};
136
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530137/*
138 * Voltage regulator information required for configuring the
139 * QCA Bluetooth chipset
140 */
141struct qca_vreg {
142 const char *name;
143 unsigned int min_uV;
144 unsigned int max_uV;
145 unsigned int load_uA;
146};
147
148struct qca_vreg_data {
149 enum qca_btsoc_type soc_type;
150 struct qca_vreg *vregs;
151 size_t num_vregs;
152};
153
154/*
155 * Platform data for the QCA Bluetooth power driver.
156 */
157struct qca_power {
158 struct device *dev;
159 const struct qca_vreg_data *vreg_data;
160 struct regulator_bulk_data *vreg_bulk;
161 bool vregs_on;
162};
163
Thierry Escande05ba5332018-03-29 21:15:24 +0200164struct qca_serdev {
165 struct hci_uart serdev_hu;
166 struct gpio_desc *bt_en;
167 struct clk *susclk;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530168 enum qca_btsoc_type btsoc_type;
169 struct qca_power *bt_power;
170 u32 init_speed;
171 u32 oper_speed;
Thierry Escande05ba5332018-03-29 21:15:24 +0200172};
173
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530174static int qca_power_setup(struct hci_uart *hu, bool on);
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530175static void qca_power_shutdown(struct hci_uart *hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +0530176static int qca_power_off(struct hci_dev *hdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530177
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700178static void __serial_clock_on(struct tty_struct *tty)
179{
180 /* TODO: Some chipset requires to enable UART clock on client
181 * side to save power consumption or manual work is required.
182 * Please put your code to control UART clock here if needed
183 */
184}
185
186static void __serial_clock_off(struct tty_struct *tty)
187{
188 /* TODO: Some chipset requires to disable UART clock on client
189 * side to save power consumption or manual work is required.
190 * Please put your code to control UART clock off here if needed
191 */
192}
193
194/* serial_clock_vote needs to be called with the ibs lock held */
195static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
196{
197 struct qca_data *qca = hu->priv;
198 unsigned int diff;
199
200 bool old_vote = (qca->tx_vote | qca->rx_vote);
201 bool new_vote;
202
203 switch (vote) {
204 case HCI_IBS_VOTE_STATS_UPDATE:
205 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
206
207 if (old_vote)
208 qca->vote_off_ms += diff;
209 else
210 qca->vote_on_ms += diff;
211 return;
212
213 case HCI_IBS_TX_VOTE_CLOCK_ON:
214 qca->tx_vote = true;
215 qca->tx_votes_on++;
216 new_vote = true;
217 break;
218
219 case HCI_IBS_RX_VOTE_CLOCK_ON:
220 qca->rx_vote = true;
221 qca->rx_votes_on++;
222 new_vote = true;
223 break;
224
225 case HCI_IBS_TX_VOTE_CLOCK_OFF:
226 qca->tx_vote = false;
227 qca->tx_votes_off++;
228 new_vote = qca->rx_vote | qca->tx_vote;
229 break;
230
231 case HCI_IBS_RX_VOTE_CLOCK_OFF:
232 qca->rx_vote = false;
233 qca->rx_votes_off++;
234 new_vote = qca->rx_vote | qca->tx_vote;
235 break;
236
237 default:
238 BT_ERR("Voting irregularity");
239 return;
240 }
241
242 if (new_vote != old_vote) {
243 if (new_vote)
244 __serial_clock_on(hu->tty);
245 else
246 __serial_clock_off(hu->tty);
247
Prasanna Karthikce26d812015-09-15 12:19:45 +0000248 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
249 vote ? "true" : "false");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700250
251 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
252
253 if (new_vote) {
254 qca->votes_on++;
255 qca->vote_off_ms += diff;
256 } else {
257 qca->votes_off++;
258 qca->vote_on_ms += diff;
259 }
260 qca->vote_last_jif = jiffies;
261 }
262}
263
264/* Builds and sends an HCI_IBS command packet.
265 * These are very simple packets with only 1 cmd byte.
266 */
267static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
268{
269 int err = 0;
270 struct sk_buff *skb = NULL;
271 struct qca_data *qca = hu->priv;
272
273 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
274
275 skb = bt_skb_alloc(1, GFP_ATOMIC);
276 if (!skb) {
277 BT_ERR("Failed to allocate memory for HCI_IBS packet");
278 return -ENOMEM;
279 }
280
281 /* Assign HCI_IBS type */
Johannes Berg634fef62017-06-16 14:29:24 +0200282 skb_put_u8(skb, cmd);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700283
284 skb_queue_tail(&qca->txq, skb);
285
286 return err;
287}
288
289static void qca_wq_awake_device(struct work_struct *work)
290{
291 struct qca_data *qca = container_of(work, struct qca_data,
292 ws_awake_device);
293 struct hci_uart *hu = qca->hu;
294 unsigned long retrans_delay;
295
296 BT_DBG("hu %p wq awake device", hu);
297
298 /* Vote for serial clock */
299 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
300
301 spin_lock(&qca->hci_ibs_lock);
302
303 /* Send wake indication to device */
304 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
305 BT_ERR("Failed to send WAKE to device");
306
307 qca->ibs_sent_wakes++;
308
309 /* Start retransmit timer */
310 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
311 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
312
313 spin_unlock(&qca->hci_ibs_lock);
314
315 /* Actually send the packets */
316 hci_uart_tx_wakeup(hu);
317}
318
319static void qca_wq_awake_rx(struct work_struct *work)
320{
321 struct qca_data *qca = container_of(work, struct qca_data,
322 ws_awake_rx);
323 struct hci_uart *hu = qca->hu;
324
325 BT_DBG("hu %p wq awake rx", hu);
326
327 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
328
329 spin_lock(&qca->hci_ibs_lock);
330 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
331
332 /* Always acknowledge device wake up,
333 * sending IBS message doesn't count as TX ON.
334 */
335 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
336 BT_ERR("Failed to acknowledge device wake up");
337
338 qca->ibs_sent_wacks++;
339
340 spin_unlock(&qca->hci_ibs_lock);
341
342 /* Actually send the packets */
343 hci_uart_tx_wakeup(hu);
344}
345
346static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
347{
348 struct qca_data *qca = container_of(work, struct qca_data,
349 ws_rx_vote_off);
350 struct hci_uart *hu = qca->hu;
351
352 BT_DBG("hu %p rx clock vote off", hu);
353
354 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
355}
356
357static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
358{
359 struct qca_data *qca = container_of(work, struct qca_data,
360 ws_tx_vote_off);
361 struct hci_uart *hu = qca->hu;
362
363 BT_DBG("hu %p tx clock vote off", hu);
364
365 /* Run HCI tx handling unlocked */
366 hci_uart_tx_wakeup(hu);
367
368 /* Now that message queued to tty driver, vote for tty clocks off.
369 * It is up to the tty driver to pend the clocks off until tx done.
370 */
371 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
372}
373
Kees Cook04356052017-10-04 17:54:29 -0700374static void hci_ibs_tx_idle_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700375{
Kees Cook04356052017-10-04 17:54:29 -0700376 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
377 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700378 unsigned long flags;
379
380 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
381
382 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
383 flags, SINGLE_DEPTH_NESTING);
384
385 switch (qca->tx_ibs_state) {
386 case HCI_IBS_TX_AWAKE:
387 /* TX_IDLE, go to SLEEP */
388 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
389 BT_ERR("Failed to send SLEEP to device");
390 break;
391 }
392 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
393 qca->ibs_sent_slps++;
394 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
395 break;
396
397 case HCI_IBS_TX_ASLEEP:
398 case HCI_IBS_TX_WAKING:
399 /* Fall through */
400
401 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000402 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700403 break;
404 }
405
406 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
407}
408
Kees Cook04356052017-10-04 17:54:29 -0700409static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700410{
Kees Cook04356052017-10-04 17:54:29 -0700411 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
412 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700413 unsigned long flags, retrans_delay;
Prasanna Karthika9137182015-09-28 08:03:24 +0000414 bool retransmit = false;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700415
416 BT_DBG("hu %p wake retransmit timeout in %d state",
417 hu, qca->tx_ibs_state);
418
419 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
420 flags, SINGLE_DEPTH_NESTING);
421
422 switch (qca->tx_ibs_state) {
423 case HCI_IBS_TX_WAKING:
424 /* No WAKE_ACK, retransmit WAKE */
Prasanna Karthika9137182015-09-28 08:03:24 +0000425 retransmit = true;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700426 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
427 BT_ERR("Failed to acknowledge device wake up");
428 break;
429 }
430 qca->ibs_sent_wakes++;
431 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
432 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
433 break;
434
435 case HCI_IBS_TX_ASLEEP:
436 case HCI_IBS_TX_AWAKE:
437 /* Fall through */
438
439 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000440 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700441 break;
442 }
443
444 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
445
446 if (retransmit)
447 hci_uart_tx_wakeup(hu);
448}
449
450/* Initialize protocol */
451static int qca_open(struct hci_uart *hu)
452{
Thierry Escande05ba5332018-03-29 21:15:24 +0200453 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700454 struct qca_data *qca;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530455 int ret;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700456
457 BT_DBG("hu %p qca_open", hu);
458
Jia-Ju Bai25a13e382018-07-23 11:56:51 +0800459 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700460 if (!qca)
461 return -ENOMEM;
462
463 skb_queue_head_init(&qca->txq);
464 skb_queue_head_init(&qca->tx_wait_q);
465 spin_lock_init(&qca->hci_ibs_lock);
Bhaktipriya Shridharfac9a602016-08-30 22:42:53 +0530466 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700467 if (!qca->workqueue) {
468 BT_ERR("QCA Workqueue not initialized properly");
469 kfree(qca);
470 return -ENOMEM;
471 }
472
473 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
474 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
475 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
476 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
477
478 qca->hu = hu;
479
480 /* Assume we start with both sides asleep -- extra wakes OK */
481 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
482 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
483
484 /* clocks actually on, but we start votes off */
485 qca->tx_vote = false;
486 qca->rx_vote = false;
487 qca->flags = 0;
488
489 qca->ibs_sent_wacks = 0;
490 qca->ibs_sent_slps = 0;
491 qca->ibs_sent_wakes = 0;
492 qca->ibs_recv_wacks = 0;
493 qca->ibs_recv_slps = 0;
494 qca->ibs_recv_wakes = 0;
495 qca->vote_last_jif = jiffies;
496 qca->vote_on_ms = 0;
497 qca->vote_off_ms = 0;
498 qca->votes_on = 0;
499 qca->votes_off = 0;
500 qca->tx_votes_on = 0;
501 qca->tx_votes_off = 0;
502 qca->rx_votes_on = 0;
503 qca->rx_votes_off = 0;
504
505 hu->priv = qca;
506
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530507 if (hu->serdev) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530508
509 qcadev = serdev_device_get_drvdata(hu->serdev);
510 if (qcadev->btsoc_type != QCA_WCN3990) {
511 gpiod_set_value_cansleep(qcadev->bt_en, 1);
512 } else {
513 hu->init_speed = qcadev->init_speed;
514 hu->oper_speed = qcadev->oper_speed;
515 ret = qca_power_setup(hu, true);
516 if (ret) {
517 destroy_workqueue(qca->workqueue);
518 kfree_skb(qca->rx_skb);
519 hu->priv = NULL;
520 kfree(qca);
521 return ret;
522 }
523 }
524 }
525
Kees Cook04356052017-10-04 17:54:29 -0700526 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700527 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
528
Kees Cook04356052017-10-04 17:54:29 -0700529 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700530 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
531
532 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
533 qca->tx_idle_delay, qca->wake_retrans);
534
535 return 0;
536}
537
538static void qca_debugfs_init(struct hci_dev *hdev)
539{
540 struct hci_uart *hu = hci_get_drvdata(hdev);
541 struct qca_data *qca = hu->priv;
542 struct dentry *ibs_dir;
543 umode_t mode;
544
545 if (!hdev->debugfs)
546 return;
547
548 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
549
550 /* read only */
551 mode = S_IRUGO;
552 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
553 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
554 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
555 &qca->ibs_sent_slps);
556 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
557 &qca->ibs_sent_wakes);
558 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
559 &qca->ibs_sent_wacks);
560 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
561 &qca->ibs_recv_slps);
562 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
563 &qca->ibs_recv_wakes);
564 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
565 &qca->ibs_recv_wacks);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700566 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700567 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
568 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700569 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700570 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
571 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
572 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
573 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
574 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
575 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
576
577 /* read/write */
578 mode = S_IRUGO | S_IWUSR;
579 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
580 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
581 &qca->tx_idle_delay);
582}
583
584/* Flush protocol data */
585static int qca_flush(struct hci_uart *hu)
586{
587 struct qca_data *qca = hu->priv;
588
589 BT_DBG("hu %p qca flush", hu);
590
591 skb_queue_purge(&qca->tx_wait_q);
592 skb_queue_purge(&qca->txq);
593
594 return 0;
595}
596
597/* Close protocol */
598static int qca_close(struct hci_uart *hu)
599{
Thierry Escande05ba5332018-03-29 21:15:24 +0200600 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700601 struct qca_data *qca = hu->priv;
602
603 BT_DBG("hu %p qca close", hu);
604
605 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
606
607 skb_queue_purge(&qca->tx_wait_q);
608 skb_queue_purge(&qca->txq);
609 del_timer(&qca->tx_idle_timer);
610 del_timer(&qca->wake_retrans_timer);
611 destroy_workqueue(qca->workqueue);
612 qca->hu = NULL;
613
Thierry Escande05ba5332018-03-29 21:15:24 +0200614 if (hu->serdev) {
Thierry Escande05ba5332018-03-29 21:15:24 +0200615 qcadev = serdev_device_get_drvdata(hu->serdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530616 if (qcadev->btsoc_type == QCA_WCN3990)
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530617 qca_power_shutdown(hu);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530618 else
619 gpiod_set_value_cansleep(qcadev->bt_en, 0);
620
Thierry Escande05ba5332018-03-29 21:15:24 +0200621 }
622
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700623 kfree_skb(qca->rx_skb);
624
625 hu->priv = NULL;
626
627 kfree(qca);
628
629 return 0;
630}
631
632/* Called upon a wake-up-indication from the device.
633 */
634static void device_want_to_wakeup(struct hci_uart *hu)
635{
636 unsigned long flags;
637 struct qca_data *qca = hu->priv;
638
639 BT_DBG("hu %p want to wake up", hu);
640
641 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
642
643 qca->ibs_recv_wakes++;
644
645 switch (qca->rx_ibs_state) {
646 case HCI_IBS_RX_ASLEEP:
647 /* Make sure clock is on - we may have turned clock off since
648 * receiving the wake up indicator awake rx clock.
649 */
650 queue_work(qca->workqueue, &qca->ws_awake_rx);
651 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
652 return;
653
654 case HCI_IBS_RX_AWAKE:
655 /* Always acknowledge device wake up,
656 * sending IBS message doesn't count as TX ON.
657 */
658 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
659 BT_ERR("Failed to acknowledge device wake up");
660 break;
661 }
662 qca->ibs_sent_wacks++;
663 break;
664
665 default:
666 /* Any other state is illegal */
667 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
668 qca->rx_ibs_state);
669 break;
670 }
671
672 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
673
674 /* Actually send the packets */
675 hci_uart_tx_wakeup(hu);
676}
677
678/* Called upon a sleep-indication from the device.
679 */
680static void device_want_to_sleep(struct hci_uart *hu)
681{
682 unsigned long flags;
683 struct qca_data *qca = hu->priv;
684
685 BT_DBG("hu %p want to sleep", hu);
686
687 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
688
689 qca->ibs_recv_slps++;
690
691 switch (qca->rx_ibs_state) {
692 case HCI_IBS_RX_AWAKE:
693 /* Update state */
694 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
695 /* Vote off rx clock under workqueue */
696 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
697 break;
698
699 case HCI_IBS_RX_ASLEEP:
700 /* Fall through */
701
702 default:
703 /* Any other state is illegal */
704 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
705 qca->rx_ibs_state);
706 break;
707 }
708
709 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
710}
711
712/* Called upon wake-up-acknowledgement from the device
713 */
714static void device_woke_up(struct hci_uart *hu)
715{
716 unsigned long flags, idle_delay;
717 struct qca_data *qca = hu->priv;
718 struct sk_buff *skb = NULL;
719
720 BT_DBG("hu %p woke up", hu);
721
722 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
723
724 qca->ibs_recv_wacks++;
725
726 switch (qca->tx_ibs_state) {
727 case HCI_IBS_TX_AWAKE:
728 /* Expect one if we send 2 WAKEs */
729 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
730 qca->tx_ibs_state);
731 break;
732
733 case HCI_IBS_TX_WAKING:
734 /* Send pending packets */
735 while ((skb = skb_dequeue(&qca->tx_wait_q)))
736 skb_queue_tail(&qca->txq, skb);
737
738 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
739 del_timer(&qca->wake_retrans_timer);
740 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
741 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
742 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
743 break;
744
745 case HCI_IBS_TX_ASLEEP:
746 /* Fall through */
747
748 default:
749 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
750 qca->tx_ibs_state);
751 break;
752 }
753
754 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
755
756 /* Actually send the packets */
757 hci_uart_tx_wakeup(hu);
758}
759
760/* Enqueue frame for transmittion (padding, crc, etc) may be called from
761 * two simultaneous tasklets.
762 */
763static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
764{
765 unsigned long flags = 0, idle_delay;
766 struct qca_data *qca = hu->priv;
767
768 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
769 qca->tx_ibs_state);
770
771 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100772 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700773
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530774 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
775
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700776 /* Don't go to sleep in middle of patch download or
777 * Out-Of-Band(GPIOs control) sleep is selected.
778 */
779 if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags)) {
780 skb_queue_tail(&qca->txq, skb);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530781 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700782 return 0;
783 }
784
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700785 /* Act according to current state */
786 switch (qca->tx_ibs_state) {
787 case HCI_IBS_TX_AWAKE:
788 BT_DBG("Device awake, sending normally");
789 skb_queue_tail(&qca->txq, skb);
790 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
791 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
792 break;
793
794 case HCI_IBS_TX_ASLEEP:
795 BT_DBG("Device asleep, waking up and queueing packet");
796 /* Save packet for later */
797 skb_queue_tail(&qca->tx_wait_q, skb);
798
799 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
800 /* Schedule a work queue to wake up device */
801 queue_work(qca->workqueue, &qca->ws_awake_device);
802 break;
803
804 case HCI_IBS_TX_WAKING:
805 BT_DBG("Device waking up, queueing packet");
806 /* Transient state; just keep packet for later */
807 skb_queue_tail(&qca->tx_wait_q, skb);
808 break;
809
810 default:
811 BT_ERR("Illegal tx state: %d (losing packet)",
812 qca->tx_ibs_state);
813 kfree_skb(skb);
814 break;
815 }
816
817 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
818
819 return 0;
820}
821
822static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
823{
824 struct hci_uart *hu = hci_get_drvdata(hdev);
825
826 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
827
828 device_want_to_sleep(hu);
829
830 kfree_skb(skb);
831 return 0;
832}
833
834static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
835{
836 struct hci_uart *hu = hci_get_drvdata(hdev);
837
838 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
839
840 device_want_to_wakeup(hu);
841
842 kfree_skb(skb);
843 return 0;
844}
845
846static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
847{
848 struct hci_uart *hu = hci_get_drvdata(hdev);
849
850 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
851
852 device_woke_up(hu);
853
854 kfree_skb(skb);
855 return 0;
856}
857
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530858static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
859{
860 /* We receive debug logs from chip as an ACL packets.
861 * Instead of sending the data to ACL to decode the
862 * received data, we are pushing them to the above layers
863 * as a diagnostic packet.
864 */
865 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
866 return hci_recv_diag(hdev, skb);
867
868 return hci_recv_frame(hdev, skb);
869}
870
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700871#define QCA_IBS_SLEEP_IND_EVENT \
872 .type = HCI_IBS_SLEEP_IND, \
873 .hlen = 0, \
874 .loff = 0, \
875 .lsize = 0, \
876 .maxlen = HCI_MAX_IBS_SIZE
877
878#define QCA_IBS_WAKE_IND_EVENT \
879 .type = HCI_IBS_WAKE_IND, \
880 .hlen = 0, \
881 .loff = 0, \
882 .lsize = 0, \
883 .maxlen = HCI_MAX_IBS_SIZE
884
885#define QCA_IBS_WAKE_ACK_EVENT \
886 .type = HCI_IBS_WAKE_ACK, \
887 .hlen = 0, \
888 .loff = 0, \
889 .lsize = 0, \
890 .maxlen = HCI_MAX_IBS_SIZE
891
892static const struct h4_recv_pkt qca_recv_pkts[] = {
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530893 { H4_RECV_ACL, .recv = qca_recv_acl_data },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700894 { H4_RECV_SCO, .recv = hci_recv_frame },
895 { H4_RECV_EVENT, .recv = hci_recv_frame },
896 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
897 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
898 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
899};
900
901static int qca_recv(struct hci_uart *hu, const void *data, int count)
902{
903 struct qca_data *qca = hu->priv;
904
905 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
906 return -EUNATCH;
907
908 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
909 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
910 if (IS_ERR(qca->rx_skb)) {
911 int err = PTR_ERR(qca->rx_skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100912 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700913 qca->rx_skb = NULL;
914 return err;
915 }
916
917 return count;
918}
919
920static struct sk_buff *qca_dequeue(struct hci_uart *hu)
921{
922 struct qca_data *qca = hu->priv;
923
924 return skb_dequeue(&qca->txq);
925}
926
927static uint8_t qca_get_baudrate_value(int speed)
928{
Prasanna Karthikce26d812015-09-15 12:19:45 +0000929 switch (speed) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700930 case 9600:
931 return QCA_BAUDRATE_9600;
932 case 19200:
933 return QCA_BAUDRATE_19200;
934 case 38400:
935 return QCA_BAUDRATE_38400;
936 case 57600:
937 return QCA_BAUDRATE_57600;
938 case 115200:
939 return QCA_BAUDRATE_115200;
940 case 230400:
941 return QCA_BAUDRATE_230400;
942 case 460800:
943 return QCA_BAUDRATE_460800;
944 case 500000:
945 return QCA_BAUDRATE_500000;
946 case 921600:
947 return QCA_BAUDRATE_921600;
948 case 1000000:
949 return QCA_BAUDRATE_1000000;
950 case 2000000:
951 return QCA_BAUDRATE_2000000;
952 case 3000000:
953 return QCA_BAUDRATE_3000000;
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +0530954 case 3200000:
955 return QCA_BAUDRATE_3200000;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700956 case 3500000:
957 return QCA_BAUDRATE_3500000;
958 default:
959 return QCA_BAUDRATE_115200;
960 }
961}
962
963static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
964{
965 struct hci_uart *hu = hci_get_drvdata(hdev);
966 struct qca_data *qca = hu->priv;
967 struct sk_buff *skb;
968 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
969
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +0530970 if (baudrate > QCA_BAUDRATE_3200000)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700971 return -EINVAL;
972
973 cmd[4] = baudrate;
974
Jia-Ju Bai25a13e382018-07-23 11:56:51 +0800975 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700976 if (!skb) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100977 bt_dev_err(hdev, "Failed to allocate baudrate packet");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700978 return -ENOMEM;
979 }
980
981 /* Assign commands to change baudrate and packet type. */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200982 skb_put_data(skb, cmd, sizeof(cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100983 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700984
985 skb_queue_tail(&qca->txq, skb);
986 hci_uart_tx_wakeup(hu);
987
988 /* wait 300ms to change new baudrate on controller side
989 * controller will come back after they receive this HCI command
990 * then host can communicate with new baudrate to controller
991 */
992 set_current_state(TASK_UNINTERRUPTIBLE);
993 schedule_timeout(msecs_to_jiffies(BAUDRATE_SETTLE_TIMEOUT_MS));
Thierry Escande99605212018-05-29 18:37:16 +0200994 set_current_state(TASK_RUNNING);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700995
996 return 0;
997}
998
Thierry Escande05ba5332018-03-29 21:15:24 +0200999static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1000{
1001 if (hu->serdev)
1002 serdev_device_set_baudrate(hu->serdev, speed);
1003 else
1004 hci_uart_set_baudrate(hu, speed);
1005}
1006
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301007static int qca_send_power_pulse(struct hci_uart *hu, u8 cmd)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301008{
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301009 int ret;
1010 int timeout = msecs_to_jiffies(POWER_PULSE_TRANS_TIMEOUT_MS);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301011
1012 /* These power pulses are single byte command which are sent
1013 * at required baudrate to wcn3990. On wcn3990, we have an external
1014 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1015 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1016 * and also we use the same power inputs to turn on and off for
1017 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1018 * we send a power on pulse at 115200 bps. This algorithm will help to
1019 * save power. Disabling hardware flow control is mandatory while
1020 * sending power pulses to SoC.
1021 */
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301022 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301023
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301024 serdev_device_write_flush(hu->serdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301025 hci_uart_set_flow_control(hu, true);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301026 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1027 if (ret < 0) {
1028 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1029 return ret;
1030 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301031
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301032 serdev_device_wait_until_sent(hu->serdev, timeout);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301033
1034 /* Wait for 100 uS for SoC to settle down */
1035 usleep_range(100, 200);
1036 hci_uart_set_flow_control(hu, false);
1037
1038 return 0;
1039}
1040
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301041static unsigned int qca_get_speed(struct hci_uart *hu,
1042 enum qca_speed_type speed_type)
1043{
1044 unsigned int speed = 0;
1045
1046 if (speed_type == QCA_INIT_SPEED) {
1047 if (hu->init_speed)
1048 speed = hu->init_speed;
1049 else if (hu->proto->init_speed)
1050 speed = hu->proto->init_speed;
1051 } else {
1052 if (hu->oper_speed)
1053 speed = hu->oper_speed;
1054 else if (hu->proto->oper_speed)
1055 speed = hu->proto->oper_speed;
1056 }
1057
1058 return speed;
1059}
1060
1061static int qca_check_speeds(struct hci_uart *hu)
1062{
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301063 struct qca_serdev *qcadev;
1064
1065 qcadev = serdev_device_get_drvdata(hu->serdev);
1066 if (qcadev->btsoc_type == QCA_WCN3990) {
1067 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1068 !qca_get_speed(hu, QCA_OPER_SPEED))
1069 return -EINVAL;
1070 } else {
1071 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1072 !qca_get_speed(hu, QCA_OPER_SPEED))
1073 return -EINVAL;
1074 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301075
1076 return 0;
1077}
1078
1079static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1080{
1081 unsigned int speed, qca_baudrate;
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301082 struct qca_serdev *qcadev;
1083 int ret = 0;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301084
1085 if (speed_type == QCA_INIT_SPEED) {
1086 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1087 if (speed)
1088 host_set_baudrate(hu, speed);
1089 } else {
1090 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1091 if (!speed)
1092 return 0;
1093
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301094 /* Disable flow control for wcn3990 to deassert RTS while
1095 * changing the baudrate of chip and host.
1096 */
1097 qcadev = serdev_device_get_drvdata(hu->serdev);
1098 if (qcadev->btsoc_type == QCA_WCN3990)
1099 hci_uart_set_flow_control(hu, true);
1100
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301101 qca_baudrate = qca_get_baudrate_value(speed);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301102 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301103 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1104 if (ret)
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301105 goto error;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301106
1107 host_set_baudrate(hu, speed);
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301108
1109error:
1110 if (qcadev->btsoc_type == QCA_WCN3990)
1111 hci_uart_set_flow_control(hu, false);
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301112 }
1113
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301114 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301115}
1116
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301117static int qca_wcn3990_init(struct hci_uart *hu)
1118{
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301119 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301120 int ret;
1121
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301122 /* Check for vregs status, may be hci down has turned
1123 * off the voltage regulator.
1124 */
1125 qcadev = serdev_device_get_drvdata(hu->serdev);
1126 if (!qcadev->bt_power->vregs_on) {
1127 serdev_device_close(hu->serdev);
1128 ret = qca_power_setup(hu, true);
1129 if (ret)
1130 return ret;
1131
1132 ret = serdev_device_open(hu->serdev);
1133 if (ret) {
1134 bt_dev_err(hu->hdev, "failed to open port");
1135 return ret;
1136 }
1137 }
1138
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301139 /* Forcefully enable wcn3990 to enter in to boot mode. */
1140 host_set_baudrate(hu, 2400);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301141 ret = qca_send_power_pulse(hu, QCA_WCN3990_POWEROFF_PULSE);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301142 if (ret)
1143 return ret;
1144
1145 qca_set_speed(hu, QCA_INIT_SPEED);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301146 ret = qca_send_power_pulse(hu, QCA_WCN3990_POWERON_PULSE);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301147 if (ret)
1148 return ret;
1149
1150 /* Wait for 100 ms for SoC to boot */
1151 msleep(100);
1152
1153 /* Now the device is in ready state to communicate with host.
1154 * To sync host with device we need to reopen port.
1155 * Without this, we will have RTS and CTS synchronization
1156 * issues.
1157 */
1158 serdev_device_close(hu->serdev);
1159 ret = serdev_device_open(hu->serdev);
1160 if (ret) {
1161 bt_dev_err(hu->hdev, "failed to open port");
1162 return ret;
1163 }
1164
1165 hci_uart_set_flow_control(hu, false);
1166
1167 return 0;
1168}
1169
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001170static int qca_setup(struct hci_uart *hu)
1171{
1172 struct hci_dev *hdev = hu->hdev;
1173 struct qca_data *qca = hu->priv;
1174 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301175 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001176 int ret;
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301177 int soc_ver = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001178
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301179 qcadev = serdev_device_get_drvdata(hu->serdev);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001180
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301181 ret = qca_check_speeds(hu);
1182 if (ret)
1183 return ret;
1184
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001185 /* Patch downloading has to be done without IBS mode */
1186 clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
1187
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301188 if (qcadev->btsoc_type == QCA_WCN3990) {
1189 bt_dev_info(hdev, "setting up wcn3990");
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301190
1191 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1192 * setup for every hci up.
1193 */
1194 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
Matthias Kaehlcke5971752d2019-02-19 12:05:59 -08001195 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301196 hu->hdev->shutdown = qca_power_off;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301197 ret = qca_wcn3990_init(hu);
1198 if (ret)
1199 return ret;
1200
1201 ret = qca_read_soc_version(hdev, &soc_ver);
1202 if (ret)
1203 return ret;
1204 } else {
1205 bt_dev_info(hdev, "ROME setup");
1206 qca_set_speed(hu, QCA_INIT_SPEED);
1207 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001208
1209 /* Setup user speed if needed */
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301210 speed = qca_get_speed(hu, QCA_OPER_SPEED);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001211 if (speed) {
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301212 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1213 if (ret)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001214 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301215
1216 qca_baudrate = qca_get_baudrate_value(speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001217 }
1218
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301219 if (qcadev->btsoc_type != QCA_WCN3990) {
1220 /* Get QCA version information */
1221 ret = qca_read_soc_version(hdev, &soc_ver);
1222 if (ret)
1223 return ret;
1224 }
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301225
1226 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001227 /* Setup patch / NVM configurations */
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301228 ret = qca_uart_setup(hdev, qca_baudrate, qcadev->btsoc_type, soc_ver);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001229 if (!ret) {
1230 set_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
1231 qca_debugfs_init(hdev);
Loic Poulainba8f3592017-11-06 12:16:56 +01001232 } else if (ret == -ENOENT) {
1233 /* No patch/nvm-config found, run with original fw/config */
1234 ret = 0;
Amit Pundir7dc5fe02018-04-16 12:10:24 +05301235 } else if (ret == -EAGAIN) {
1236 /*
1237 * Userspace firmware loader will return -EAGAIN in case no
1238 * patch/nvm-config is found, so run with original fw/config.
1239 */
1240 ret = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001241 }
1242
1243 /* Setup bdaddr */
Balakrishna Godavarthi5c0a10012019-01-16 18:01:15 +05301244 if (qcadev->btsoc_type == QCA_WCN3990)
1245 hu->hdev->set_bdaddr = qca_set_bdaddr;
1246 else
1247 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001248
1249 return ret;
1250}
1251
1252static struct hci_uart_proto qca_proto = {
1253 .id = HCI_UART_QCA,
1254 .name = "QCA",
Marcel Holtmannaee61f72015-10-20 21:30:45 +02001255 .manufacturer = 29,
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001256 .init_speed = 115200,
1257 .oper_speed = 3000000,
1258 .open = qca_open,
1259 .close = qca_close,
1260 .flush = qca_flush,
1261 .setup = qca_setup,
1262 .recv = qca_recv,
1263 .enqueue = qca_enqueue,
1264 .dequeue = qca_dequeue,
1265};
1266
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301267static const struct qca_vreg_data qca_soc_data = {
1268 .soc_type = QCA_WCN3990,
1269 .vregs = (struct qca_vreg []) {
1270 { "vddio", 1800000, 1900000, 15000 },
1271 { "vddxo", 1800000, 1900000, 80000 },
1272 { "vddrf", 1300000, 1350000, 300000 },
1273 { "vddch0", 3300000, 3400000, 450000 },
1274 },
1275 .num_vregs = 4,
1276};
1277
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301278static void qca_power_shutdown(struct hci_uart *hu)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301279{
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301280 struct qca_data *qca = hu->priv;
1281 unsigned long flags;
1282
1283 /* From this point we go into power off state. But serial port is
1284 * still open, stop queueing the IBS data and flush all the buffered
1285 * data in skb's.
1286 */
1287 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1288 clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
1289 qca_flush(hu);
1290 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1291
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301292 host_set_baudrate(hu, 2400);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301293 qca_send_power_pulse(hu, QCA_WCN3990_POWEROFF_PULSE);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301294 qca_power_setup(hu, false);
1295}
1296
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301297static int qca_power_off(struct hci_dev *hdev)
1298{
1299 struct hci_uart *hu = hci_get_drvdata(hdev);
1300
1301 qca_power_shutdown(hu);
1302 return 0;
1303}
1304
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301305static int qca_enable_regulator(struct qca_vreg vregs,
1306 struct regulator *regulator)
1307{
1308 int ret;
1309
1310 ret = regulator_set_voltage(regulator, vregs.min_uV,
1311 vregs.max_uV);
1312 if (ret)
1313 return ret;
1314
1315 if (vregs.load_uA)
1316 ret = regulator_set_load(regulator,
1317 vregs.load_uA);
1318
1319 if (ret)
1320 return ret;
1321
1322 return regulator_enable(regulator);
1323
1324}
1325
1326static void qca_disable_regulator(struct qca_vreg vregs,
1327 struct regulator *regulator)
1328{
1329 regulator_disable(regulator);
1330 regulator_set_voltage(regulator, 0, vregs.max_uV);
1331 if (vregs.load_uA)
1332 regulator_set_load(regulator, 0);
1333
1334}
1335
1336static int qca_power_setup(struct hci_uart *hu, bool on)
1337{
1338 struct qca_vreg *vregs;
1339 struct regulator_bulk_data *vreg_bulk;
1340 struct qca_serdev *qcadev;
1341 int i, num_vregs, ret = 0;
1342
1343 qcadev = serdev_device_get_drvdata(hu->serdev);
1344 if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
1345 !qcadev->bt_power->vreg_bulk)
1346 return -EINVAL;
1347
1348 vregs = qcadev->bt_power->vreg_data->vregs;
1349 vreg_bulk = qcadev->bt_power->vreg_bulk;
1350 num_vregs = qcadev->bt_power->vreg_data->num_vregs;
1351 BT_DBG("on: %d", on);
1352 if (on && !qcadev->bt_power->vregs_on) {
1353 for (i = 0; i < num_vregs; i++) {
1354 ret = qca_enable_regulator(vregs[i],
1355 vreg_bulk[i].consumer);
1356 if (ret)
1357 break;
1358 }
1359
1360 if (ret) {
1361 BT_ERR("failed to enable regulator:%s", vregs[i].name);
1362 /* turn off regulators which are enabled */
1363 for (i = i - 1; i >= 0; i--)
1364 qca_disable_regulator(vregs[i],
1365 vreg_bulk[i].consumer);
1366 } else {
1367 qcadev->bt_power->vregs_on = true;
1368 }
1369 } else if (!on && qcadev->bt_power->vregs_on) {
1370 /* turn off regulator in reverse order */
1371 i = qcadev->bt_power->vreg_data->num_vregs - 1;
1372 for ( ; i >= 0; i--)
1373 qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
1374
1375 qcadev->bt_power->vregs_on = false;
1376 }
1377
1378 return ret;
1379}
1380
1381static int qca_init_regulators(struct qca_power *qca,
1382 const struct qca_vreg *vregs, size_t num_vregs)
1383{
1384 int i;
1385
Kees Cook329e0982018-10-05 16:21:46 -07001386 qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301387 sizeof(struct regulator_bulk_data),
1388 GFP_KERNEL);
1389 if (!qca->vreg_bulk)
1390 return -ENOMEM;
1391
1392 for (i = 0; i < num_vregs; i++)
1393 qca->vreg_bulk[i].supply = vregs[i].name;
1394
1395 return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
1396}
1397
Thierry Escande05ba5332018-03-29 21:15:24 +02001398static int qca_serdev_probe(struct serdev_device *serdev)
1399{
1400 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301401 const struct qca_vreg_data *data;
Thierry Escande05ba5332018-03-29 21:15:24 +02001402 int err;
1403
1404 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1405 if (!qcadev)
1406 return -ENOMEM;
1407
1408 qcadev->serdev_hu.serdev = serdev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301409 data = of_device_get_match_data(&serdev->dev);
Thierry Escande05ba5332018-03-29 21:15:24 +02001410 serdev_device_set_drvdata(serdev, qcadev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301411 if (data && data->soc_type == QCA_WCN3990) {
1412 qcadev->btsoc_type = QCA_WCN3990;
1413 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1414 sizeof(struct qca_power),
1415 GFP_KERNEL);
1416 if (!qcadev->bt_power)
1417 return -ENOMEM;
Thierry Escande05ba5332018-03-29 21:15:24 +02001418
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301419 qcadev->bt_power->dev = &serdev->dev;
1420 qcadev->bt_power->vreg_data = data;
1421 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1422 data->num_vregs);
1423 if (err) {
1424 BT_ERR("Failed to init regulators:%d", err);
1425 goto out;
1426 }
1427
1428 qcadev->bt_power->vregs_on = false;
1429
1430 device_property_read_u32(&serdev->dev, "max-speed",
1431 &qcadev->oper_speed);
1432 if (!qcadev->oper_speed)
1433 BT_DBG("UART will pick default operating speed");
1434
1435 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1436 if (err) {
1437 BT_ERR("wcn3990 serdev registration failed");
1438 goto out;
1439 }
1440 } else {
1441 qcadev->btsoc_type = QCA_ROME;
1442 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1443 GPIOD_OUT_LOW);
1444 if (IS_ERR(qcadev->bt_en)) {
1445 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1446 return PTR_ERR(qcadev->bt_en);
1447 }
1448
1449 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1450 if (IS_ERR(qcadev->susclk)) {
1451 dev_err(&serdev->dev, "failed to acquire clk\n");
1452 return PTR_ERR(qcadev->susclk);
1453 }
1454
1455 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1456 if (err)
1457 return err;
1458
1459 err = clk_prepare_enable(qcadev->susclk);
1460 if (err)
1461 return err;
1462
1463 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1464 if (err)
1465 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02001466 }
1467
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301468out: return err;
Thierry Escande05ba5332018-03-29 21:15:24 +02001469
Thierry Escande05ba5332018-03-29 21:15:24 +02001470}
1471
1472static void qca_serdev_remove(struct serdev_device *serdev)
1473{
1474 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1475
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301476 if (qcadev->btsoc_type == QCA_WCN3990)
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301477 qca_power_shutdown(&qcadev->serdev_hu);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301478 else
1479 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02001480
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301481 hci_uart_unregister_device(&qcadev->serdev_hu);
Thierry Escande05ba5332018-03-29 21:15:24 +02001482}
1483
1484static const struct of_device_id qca_bluetooth_of_match[] = {
1485 { .compatible = "qcom,qca6174-bt" },
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301486 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data},
Thierry Escande05ba5332018-03-29 21:15:24 +02001487 { /* sentinel */ }
1488};
1489MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1490
1491static struct serdev_device_driver qca_serdev_driver = {
1492 .probe = qca_serdev_probe,
1493 .remove = qca_serdev_remove,
1494 .driver = {
1495 .name = "hci_uart_qca",
1496 .of_match_table = qca_bluetooth_of_match,
1497 },
1498};
1499
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001500int __init qca_init(void)
1501{
Thierry Escande05ba5332018-03-29 21:15:24 +02001502 serdev_device_driver_register(&qca_serdev_driver);
1503
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001504 return hci_uart_register_proto(&qca_proto);
1505}
1506
1507int __exit qca_deinit(void)
1508{
Thierry Escande05ba5332018-03-29 21:15:24 +02001509 serdev_device_driver_unregister(&qca_serdev_driver);
1510
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001511 return hci_uart_unregister_proto(&qca_proto);
1512}