blob: c2062087b46bfb2c7d4debfc98d42ef0b6d48980 [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
Claire Chang41d5b252019-10-31 18:46:14 +080046#define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40
47#define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000
Matthias Kaehlcke94d66712019-02-27 15:52:23 -080048#define CMD_TRANS_TIMEOUT_MS 100
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070049
Thierry Escande05ba5332018-03-29 21:15:24 +020050/* susclk rate */
51#define SUSCLK_RATE_32KHZ 32768
52
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +053053/* Controller debug log header */
54#define QCA_DEBUG_HANDLE 0x2EDC
55
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070056enum qca_flags {
57 QCA_IBS_ENABLED,
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -070058 QCA_DROP_VENDOR_EVENT,
Claire Chang41d5b252019-10-31 18:46:14 +080059 QCA_SUSPENDING,
Matthias Kaehlcke62a91992019-04-29 16:21:30 -070060};
61
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070062/* HCI_IBS transmit side sleep protocol states */
63enum tx_ibs_states {
64 HCI_IBS_TX_ASLEEP,
65 HCI_IBS_TX_WAKING,
66 HCI_IBS_TX_AWAKE,
67};
68
69/* HCI_IBS receive side sleep protocol states */
70enum rx_states {
71 HCI_IBS_RX_ASLEEP,
72 HCI_IBS_RX_AWAKE,
73};
74
75/* HCI_IBS transmit and receive side clock state vote */
76enum hci_ibs_clock_state_vote {
77 HCI_IBS_VOTE_STATS_UPDATE,
78 HCI_IBS_TX_VOTE_CLOCK_ON,
79 HCI_IBS_TX_VOTE_CLOCK_OFF,
80 HCI_IBS_RX_VOTE_CLOCK_ON,
81 HCI_IBS_RX_VOTE_CLOCK_OFF,
82};
83
84struct qca_data {
85 struct hci_uart *hu;
86 struct sk_buff *rx_skb;
87 struct sk_buff_head txq;
88 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
89 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
90 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
91 u8 rx_ibs_state; /* HCI_IBS receive side power state */
Viresh Kumar621a5f72015-09-26 15:04:07 -070092 bool tx_vote; /* Clock must be on for TX */
93 bool rx_vote; /* Clock must be on for RX */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070094 struct timer_list tx_idle_timer;
95 u32 tx_idle_delay;
96 struct timer_list wake_retrans_timer;
97 u32 wake_retrans;
98 struct workqueue_struct *workqueue;
99 struct work_struct ws_awake_rx;
100 struct work_struct ws_awake_device;
101 struct work_struct ws_rx_vote_off;
102 struct work_struct ws_tx_vote_off;
103 unsigned long flags;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700104 struct completion drop_ev_comp;
Claire Chang41d5b252019-10-31 18:46:14 +0800105 wait_queue_head_t suspend_wait_q;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700106
107 /* For debugging purpose */
108 u64 ibs_sent_wacks;
109 u64 ibs_sent_slps;
110 u64 ibs_sent_wakes;
111 u64 ibs_recv_wacks;
112 u64 ibs_recv_slps;
113 u64 ibs_recv_wakes;
114 u64 vote_last_jif;
115 u32 vote_on_ms;
116 u32 vote_off_ms;
117 u64 tx_votes_on;
118 u64 rx_votes_on;
119 u64 tx_votes_off;
120 u64 rx_votes_off;
121 u64 votes_on;
122 u64 votes_off;
123};
124
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +0530125enum qca_speed_type {
126 QCA_INIT_SPEED = 1,
127 QCA_OPER_SPEED
128};
129
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530130/*
131 * Voltage regulator information required for configuring the
132 * QCA Bluetooth chipset
133 */
134struct qca_vreg {
135 const char *name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530136 unsigned int load_uA;
137};
138
139struct qca_vreg_data {
140 enum qca_btsoc_type soc_type;
141 struct qca_vreg *vregs;
142 size_t num_vregs;
143};
144
145/*
146 * Platform data for the QCA Bluetooth power driver.
147 */
148struct qca_power {
149 struct device *dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530150 struct regulator_bulk_data *vreg_bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -0700151 int num_vregs;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530152 bool vregs_on;
153};
154
Thierry Escande05ba5332018-03-29 21:15:24 +0200155struct qca_serdev {
156 struct hci_uart serdev_hu;
157 struct gpio_desc *bt_en;
158 struct clk *susclk;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530159 enum qca_btsoc_type btsoc_type;
160 struct qca_power *bt_power;
161 u32 init_speed;
162 u32 oper_speed;
Rocky Liao99c905c2019-06-06 17:40:30 +0800163 const char *firmware_name;
Thierry Escande05ba5332018-03-29 21:15:24 +0200164};
165
Bjorn Anderssona9314e72019-10-17 22:24:04 -0700166static int qca_regulator_enable(struct qca_serdev *qcadev);
167static void qca_regulator_disable(struct qca_serdev *qcadev);
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530168static void qca_power_shutdown(struct hci_uart *hu);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +0530169static int qca_power_off(struct hci_dev *hdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530170
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -0700171static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
172{
173 enum qca_btsoc_type soc_type;
174
175 if (hu->serdev) {
176 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
177
178 soc_type = qsd->btsoc_type;
179 } else {
180 soc_type = QCA_ROME;
181 }
182
183 return soc_type;
184}
185
Rocky Liao99c905c2019-06-06 17:40:30 +0800186static const char *qca_get_firmware_name(struct hci_uart *hu)
187{
188 if (hu->serdev) {
189 struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
190
191 return qsd->firmware_name;
192 } else {
193 return NULL;
194 }
195}
196
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700197static void __serial_clock_on(struct tty_struct *tty)
198{
199 /* TODO: Some chipset requires to enable UART clock on client
200 * side to save power consumption or manual work is required.
201 * Please put your code to control UART clock here if needed
202 */
203}
204
205static void __serial_clock_off(struct tty_struct *tty)
206{
207 /* TODO: Some chipset requires to disable UART clock on client
208 * side to save power consumption or manual work is required.
209 * Please put your code to control UART clock off here if needed
210 */
211}
212
213/* serial_clock_vote needs to be called with the ibs lock held */
214static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
215{
216 struct qca_data *qca = hu->priv;
217 unsigned int diff;
218
219 bool old_vote = (qca->tx_vote | qca->rx_vote);
220 bool new_vote;
221
222 switch (vote) {
223 case HCI_IBS_VOTE_STATS_UPDATE:
224 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
225
226 if (old_vote)
227 qca->vote_off_ms += diff;
228 else
229 qca->vote_on_ms += diff;
230 return;
231
232 case HCI_IBS_TX_VOTE_CLOCK_ON:
233 qca->tx_vote = true;
234 qca->tx_votes_on++;
235 new_vote = true;
236 break;
237
238 case HCI_IBS_RX_VOTE_CLOCK_ON:
239 qca->rx_vote = true;
240 qca->rx_votes_on++;
241 new_vote = true;
242 break;
243
244 case HCI_IBS_TX_VOTE_CLOCK_OFF:
245 qca->tx_vote = false;
246 qca->tx_votes_off++;
247 new_vote = qca->rx_vote | qca->tx_vote;
248 break;
249
250 case HCI_IBS_RX_VOTE_CLOCK_OFF:
251 qca->rx_vote = false;
252 qca->rx_votes_off++;
253 new_vote = qca->rx_vote | qca->tx_vote;
254 break;
255
256 default:
257 BT_ERR("Voting irregularity");
258 return;
259 }
260
261 if (new_vote != old_vote) {
262 if (new_vote)
263 __serial_clock_on(hu->tty);
264 else
265 __serial_clock_off(hu->tty);
266
Prasanna Karthikce26d812015-09-15 12:19:45 +0000267 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
268 vote ? "true" : "false");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700269
270 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
271
272 if (new_vote) {
273 qca->votes_on++;
274 qca->vote_off_ms += diff;
275 } else {
276 qca->votes_off++;
277 qca->vote_on_ms += diff;
278 }
279 qca->vote_last_jif = jiffies;
280 }
281}
282
283/* Builds and sends an HCI_IBS command packet.
284 * These are very simple packets with only 1 cmd byte.
285 */
286static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
287{
288 int err = 0;
289 struct sk_buff *skb = NULL;
290 struct qca_data *qca = hu->priv;
291
292 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
293
294 skb = bt_skb_alloc(1, GFP_ATOMIC);
295 if (!skb) {
296 BT_ERR("Failed to allocate memory for HCI_IBS packet");
297 return -ENOMEM;
298 }
299
300 /* Assign HCI_IBS type */
Johannes Berg634fef62017-06-16 14:29:24 +0200301 skb_put_u8(skb, cmd);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700302
303 skb_queue_tail(&qca->txq, skb);
304
305 return err;
306}
307
308static void qca_wq_awake_device(struct work_struct *work)
309{
310 struct qca_data *qca = container_of(work, struct qca_data,
311 ws_awake_device);
312 struct hci_uart *hu = qca->hu;
313 unsigned long retrans_delay;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530314 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700315
316 BT_DBG("hu %p wq awake device", hu);
317
318 /* Vote for serial clock */
319 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
320
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530321 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700322
323 /* Send wake indication to device */
324 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
325 BT_ERR("Failed to send WAKE to device");
326
327 qca->ibs_sent_wakes++;
328
329 /* Start retransmit timer */
330 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
331 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
332
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530333 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700334
335 /* Actually send the packets */
336 hci_uart_tx_wakeup(hu);
337}
338
339static void qca_wq_awake_rx(struct work_struct *work)
340{
341 struct qca_data *qca = container_of(work, struct qca_data,
342 ws_awake_rx);
343 struct hci_uart *hu = qca->hu;
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530344 unsigned long flags;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700345
346 BT_DBG("hu %p wq awake rx", hu);
347
348 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
349
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530350 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700351 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
352
353 /* Always acknowledge device wake up,
354 * sending IBS message doesn't count as TX ON.
355 */
356 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
357 BT_ERR("Failed to acknowledge device wake up");
358
359 qca->ibs_sent_wacks++;
360
Harish Bandi31fb1bb2019-09-04 10:04:16 +0530361 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700362
363 /* Actually send the packets */
364 hci_uart_tx_wakeup(hu);
365}
366
367static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
368{
369 struct qca_data *qca = container_of(work, struct qca_data,
370 ws_rx_vote_off);
371 struct hci_uart *hu = qca->hu;
372
373 BT_DBG("hu %p rx clock vote off", hu);
374
375 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
376}
377
378static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
379{
380 struct qca_data *qca = container_of(work, struct qca_data,
381 ws_tx_vote_off);
382 struct hci_uart *hu = qca->hu;
383
384 BT_DBG("hu %p tx clock vote off", hu);
385
386 /* Run HCI tx handling unlocked */
387 hci_uart_tx_wakeup(hu);
388
389 /* Now that message queued to tty driver, vote for tty clocks off.
390 * It is up to the tty driver to pend the clocks off until tx done.
391 */
392 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
393}
394
Kees Cook04356052017-10-04 17:54:29 -0700395static void hci_ibs_tx_idle_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700396{
Kees Cook04356052017-10-04 17:54:29 -0700397 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
398 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700399 unsigned long flags;
400
401 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
402
403 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
404 flags, SINGLE_DEPTH_NESTING);
405
406 switch (qca->tx_ibs_state) {
407 case HCI_IBS_TX_AWAKE:
408 /* TX_IDLE, go to SLEEP */
409 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
410 BT_ERR("Failed to send SLEEP to device");
411 break;
412 }
413 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
414 qca->ibs_sent_slps++;
415 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
416 break;
417
418 case HCI_IBS_TX_ASLEEP:
419 case HCI_IBS_TX_WAKING:
420 /* Fall through */
421
422 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000423 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700424 break;
425 }
426
427 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
428}
429
Kees Cook04356052017-10-04 17:54:29 -0700430static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700431{
Kees Cook04356052017-10-04 17:54:29 -0700432 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
433 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700434 unsigned long flags, retrans_delay;
Prasanna Karthika9137182015-09-28 08:03:24 +0000435 bool retransmit = false;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700436
437 BT_DBG("hu %p wake retransmit timeout in %d state",
438 hu, qca->tx_ibs_state);
439
440 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
441 flags, SINGLE_DEPTH_NESTING);
442
Claire Chang41d5b252019-10-31 18:46:14 +0800443 /* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
444 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
445 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
446 return;
447 }
448
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700449 switch (qca->tx_ibs_state) {
450 case HCI_IBS_TX_WAKING:
451 /* No WAKE_ACK, retransmit WAKE */
Prasanna Karthika9137182015-09-28 08:03:24 +0000452 retransmit = true;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700453 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
454 BT_ERR("Failed to acknowledge device wake up");
455 break;
456 }
457 qca->ibs_sent_wakes++;
458 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
459 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
460 break;
461
462 case HCI_IBS_TX_ASLEEP:
463 case HCI_IBS_TX_AWAKE:
464 /* Fall through */
465
466 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000467 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700468 break;
469 }
470
471 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
472
473 if (retransmit)
474 hci_uart_tx_wakeup(hu);
475}
476
477/* Initialize protocol */
478static int qca_open(struct hci_uart *hu)
479{
Thierry Escande05ba5332018-03-29 21:15:24 +0200480 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700481 struct qca_data *qca;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530482 int ret;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700483
484 BT_DBG("hu %p qca_open", hu);
485
Vladis Dronovb36a1552019-07-30 11:33:45 +0200486 if (!hci_uart_has_flow_control(hu))
487 return -EOPNOTSUPP;
488
Jia-Ju Bai25a13e382018-07-23 11:56:51 +0800489 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700490 if (!qca)
491 return -ENOMEM;
492
493 skb_queue_head_init(&qca->txq);
494 skb_queue_head_init(&qca->tx_wait_q);
495 spin_lock_init(&qca->hci_ibs_lock);
Bhaktipriya Shridharfac9a602016-08-30 22:42:53 +0530496 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700497 if (!qca->workqueue) {
498 BT_ERR("QCA Workqueue not initialized properly");
499 kfree(qca);
500 return -ENOMEM;
501 }
502
503 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
504 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
505 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
506 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
507
Claire Chang41d5b252019-10-31 18:46:14 +0800508 init_waitqueue_head(&qca->suspend_wait_q);
509
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700510 qca->hu = hu;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700511 init_completion(&qca->drop_ev_comp);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700512
513 /* Assume we start with both sides asleep -- extra wakes OK */
514 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
515 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
516
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700517 qca->vote_last_jif = jiffies;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700518
519 hu->priv = qca;
520
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530521 if (hu->serdev) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530522
523 qcadev = serdev_device_get_drvdata(hu->serdev);
Harish Bandi523760b2019-04-26 19:26:01 +0530524 if (!qca_is_wcn399x(qcadev->btsoc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530525 gpiod_set_value_cansleep(qcadev->bt_en, 1);
Balakrishna Godavarthi7f09d5a2019-04-01 15:19:08 +0530526 /* Controller needs time to bootup. */
527 msleep(150);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530528 } else {
529 hu->init_speed = qcadev->init_speed;
530 hu->oper_speed = qcadev->oper_speed;
Bjorn Anderssona9314e72019-10-17 22:24:04 -0700531 ret = qca_regulator_enable(qcadev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530532 if (ret) {
533 destroy_workqueue(qca->workqueue);
534 kfree_skb(qca->rx_skb);
535 hu->priv = NULL;
536 kfree(qca);
537 return ret;
538 }
539 }
540 }
541
Kees Cook04356052017-10-04 17:54:29 -0700542 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700543 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
544
Kees Cook04356052017-10-04 17:54:29 -0700545 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
Claire Chang41d5b252019-10-31 18:46:14 +0800546 qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700547
548 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
549 qca->tx_idle_delay, qca->wake_retrans);
550
551 return 0;
552}
553
554static void qca_debugfs_init(struct hci_dev *hdev)
555{
556 struct hci_uart *hu = hci_get_drvdata(hdev);
557 struct qca_data *qca = hu->priv;
558 struct dentry *ibs_dir;
559 umode_t mode;
560
561 if (!hdev->debugfs)
562 return;
563
564 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
565
566 /* read only */
567 mode = S_IRUGO;
568 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
569 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
570 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
571 &qca->ibs_sent_slps);
572 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
573 &qca->ibs_sent_wakes);
574 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
575 &qca->ibs_sent_wacks);
576 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
577 &qca->ibs_recv_slps);
578 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
579 &qca->ibs_recv_wakes);
580 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
581 &qca->ibs_recv_wacks);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700582 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700583 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
584 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700585 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700586 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
587 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
588 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
589 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
590 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
591 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
592
593 /* read/write */
594 mode = S_IRUGO | S_IWUSR;
595 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
596 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
597 &qca->tx_idle_delay);
598}
599
600/* Flush protocol data */
601static int qca_flush(struct hci_uart *hu)
602{
603 struct qca_data *qca = hu->priv;
604
605 BT_DBG("hu %p qca flush", hu);
606
607 skb_queue_purge(&qca->tx_wait_q);
608 skb_queue_purge(&qca->txq);
609
610 return 0;
611}
612
613/* Close protocol */
614static int qca_close(struct hci_uart *hu)
615{
Thierry Escande05ba5332018-03-29 21:15:24 +0200616 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700617 struct qca_data *qca = hu->priv;
618
619 BT_DBG("hu %p qca close", hu);
620
621 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
622
623 skb_queue_purge(&qca->tx_wait_q);
624 skb_queue_purge(&qca->txq);
625 del_timer(&qca->tx_idle_timer);
626 del_timer(&qca->wake_retrans_timer);
627 destroy_workqueue(qca->workqueue);
628 qca->hu = NULL;
629
Thierry Escande05ba5332018-03-29 21:15:24 +0200630 if (hu->serdev) {
Thierry Escande05ba5332018-03-29 21:15:24 +0200631 qcadev = serdev_device_get_drvdata(hu->serdev);
Harish Bandi523760b2019-04-26 19:26:01 +0530632 if (qca_is_wcn399x(qcadev->btsoc_type))
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +0530633 qca_power_shutdown(hu);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +0530634 else
635 gpiod_set_value_cansleep(qcadev->bt_en, 0);
636
Thierry Escande05ba5332018-03-29 21:15:24 +0200637 }
638
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700639 kfree_skb(qca->rx_skb);
640
641 hu->priv = NULL;
642
643 kfree(qca);
644
645 return 0;
646}
647
648/* Called upon a wake-up-indication from the device.
649 */
650static void device_want_to_wakeup(struct hci_uart *hu)
651{
652 unsigned long flags;
653 struct qca_data *qca = hu->priv;
654
655 BT_DBG("hu %p want to wake up", hu);
656
657 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
658
659 qca->ibs_recv_wakes++;
660
Claire Chang41d5b252019-10-31 18:46:14 +0800661 /* Don't wake the rx up when suspending. */
662 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
663 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
664 return;
665 }
666
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700667 switch (qca->rx_ibs_state) {
668 case HCI_IBS_RX_ASLEEP:
669 /* Make sure clock is on - we may have turned clock off since
670 * receiving the wake up indicator awake rx clock.
671 */
672 queue_work(qca->workqueue, &qca->ws_awake_rx);
673 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
674 return;
675
676 case HCI_IBS_RX_AWAKE:
677 /* Always acknowledge device wake up,
678 * sending IBS message doesn't count as TX ON.
679 */
680 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
681 BT_ERR("Failed to acknowledge device wake up");
682 break;
683 }
684 qca->ibs_sent_wacks++;
685 break;
686
687 default:
688 /* Any other state is illegal */
689 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
690 qca->rx_ibs_state);
691 break;
692 }
693
694 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
695
696 /* Actually send the packets */
697 hci_uart_tx_wakeup(hu);
698}
699
700/* Called upon a sleep-indication from the device.
701 */
702static void device_want_to_sleep(struct hci_uart *hu)
703{
704 unsigned long flags;
705 struct qca_data *qca = hu->priv;
706
Rocky Liao6600c082019-08-14 15:42:39 +0800707 BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700708
709 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
710
711 qca->ibs_recv_slps++;
712
713 switch (qca->rx_ibs_state) {
714 case HCI_IBS_RX_AWAKE:
715 /* Update state */
716 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
717 /* Vote off rx clock under workqueue */
718 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
719 break;
720
721 case HCI_IBS_RX_ASLEEP:
Rocky Liao6600c082019-08-14 15:42:39 +0800722 break;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700723
724 default:
725 /* Any other state is illegal */
726 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
727 qca->rx_ibs_state);
728 break;
729 }
730
Claire Chang41d5b252019-10-31 18:46:14 +0800731 wake_up_interruptible(&qca->suspend_wait_q);
732
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700733 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
734}
735
736/* Called upon wake-up-acknowledgement from the device
737 */
738static void device_woke_up(struct hci_uart *hu)
739{
740 unsigned long flags, idle_delay;
741 struct qca_data *qca = hu->priv;
742 struct sk_buff *skb = NULL;
743
744 BT_DBG("hu %p woke up", hu);
745
746 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
747
748 qca->ibs_recv_wacks++;
749
Claire Chang41d5b252019-10-31 18:46:14 +0800750 /* Don't react to the wake-up-acknowledgment when suspending. */
751 if (test_bit(QCA_SUSPENDING, &qca->flags)) {
752 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
753 return;
754 }
755
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700756 switch (qca->tx_ibs_state) {
757 case HCI_IBS_TX_AWAKE:
758 /* Expect one if we send 2 WAKEs */
759 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
760 qca->tx_ibs_state);
761 break;
762
763 case HCI_IBS_TX_WAKING:
764 /* Send pending packets */
765 while ((skb = skb_dequeue(&qca->tx_wait_q)))
766 skb_queue_tail(&qca->txq, skb);
767
768 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
769 del_timer(&qca->wake_retrans_timer);
770 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
771 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
772 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
773 break;
774
775 case HCI_IBS_TX_ASLEEP:
776 /* Fall through */
777
778 default:
779 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
780 qca->tx_ibs_state);
781 break;
782 }
783
784 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
785
786 /* Actually send the packets */
787 hci_uart_tx_wakeup(hu);
788}
789
790/* Enqueue frame for transmittion (padding, crc, etc) may be called from
791 * two simultaneous tasklets.
792 */
793static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
794{
795 unsigned long flags = 0, idle_delay;
796 struct qca_data *qca = hu->priv;
797
798 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
799 qca->tx_ibs_state);
800
801 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100802 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700803
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530804 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
805
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700806 /* Don't go to sleep in middle of patch download or
807 * Out-Of-Band(GPIOs control) sleep is selected.
Claire Chang41d5b252019-10-31 18:46:14 +0800808 * Don't wake the device up when suspending.
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700809 */
Claire Chang41d5b252019-10-31 18:46:14 +0800810 if (!test_bit(QCA_IBS_ENABLED, &qca->flags) ||
811 test_bit(QCA_SUSPENDING, &qca->flags)) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700812 skb_queue_tail(&qca->txq, skb);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +0530813 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700814 return 0;
815 }
816
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700817 /* Act according to current state */
818 switch (qca->tx_ibs_state) {
819 case HCI_IBS_TX_AWAKE:
820 BT_DBG("Device awake, sending normally");
821 skb_queue_tail(&qca->txq, skb);
822 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
823 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
824 break;
825
826 case HCI_IBS_TX_ASLEEP:
827 BT_DBG("Device asleep, waking up and queueing packet");
828 /* Save packet for later */
829 skb_queue_tail(&qca->tx_wait_q, skb);
830
831 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
832 /* Schedule a work queue to wake up device */
833 queue_work(qca->workqueue, &qca->ws_awake_device);
834 break;
835
836 case HCI_IBS_TX_WAKING:
837 BT_DBG("Device waking up, queueing packet");
838 /* Transient state; just keep packet for later */
839 skb_queue_tail(&qca->tx_wait_q, skb);
840 break;
841
842 default:
843 BT_ERR("Illegal tx state: %d (losing packet)",
844 qca->tx_ibs_state);
845 kfree_skb(skb);
846 break;
847 }
848
849 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
850
851 return 0;
852}
853
854static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
855{
856 struct hci_uart *hu = hci_get_drvdata(hdev);
857
858 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
859
860 device_want_to_sleep(hu);
861
862 kfree_skb(skb);
863 return 0;
864}
865
866static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
867{
868 struct hci_uart *hu = hci_get_drvdata(hdev);
869
870 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
871
872 device_want_to_wakeup(hu);
873
874 kfree_skb(skb);
875 return 0;
876}
877
878static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
879{
880 struct hci_uart *hu = hci_get_drvdata(hdev);
881
882 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
883
884 device_woke_up(hu);
885
886 kfree_skb(skb);
887 return 0;
888}
889
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530890static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
891{
892 /* We receive debug logs from chip as an ACL packets.
893 * Instead of sending the data to ACL to decode the
894 * received data, we are pushing them to the above layers
895 * as a diagnostic packet.
896 */
897 if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
898 return hci_recv_diag(hdev, skb);
899
900 return hci_recv_frame(hdev, skb);
901}
902
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700903static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
904{
905 struct hci_uart *hu = hci_get_drvdata(hdev);
906 struct qca_data *qca = hu->priv;
907
908 if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
909 struct hci_event_hdr *hdr = (void *)skb->data;
910
911 /* For the WCN3990 the vendor command for a baudrate change
912 * isn't sent as synchronous HCI command, because the
913 * controller sends the corresponding vendor event with the
914 * new baudrate. The event is received and properly decoded
915 * after changing the baudrate of the host port. It needs to
916 * be dropped, otherwise it can be misinterpreted as
917 * response to a later firmware download command (also a
918 * vendor command).
919 */
920
921 if (hdr->evt == HCI_EV_VENDOR)
922 complete(&qca->drop_ev_comp);
923
Wei Yongjun4974c832019-07-09 01:35:30 +0000924 kfree_skb(skb);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700925
926 return 0;
927 }
928
929 return hci_recv_frame(hdev, skb);
930}
931
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700932#define QCA_IBS_SLEEP_IND_EVENT \
933 .type = HCI_IBS_SLEEP_IND, \
934 .hlen = 0, \
935 .loff = 0, \
936 .lsize = 0, \
937 .maxlen = HCI_MAX_IBS_SIZE
938
939#define QCA_IBS_WAKE_IND_EVENT \
940 .type = HCI_IBS_WAKE_IND, \
941 .hlen = 0, \
942 .loff = 0, \
943 .lsize = 0, \
944 .maxlen = HCI_MAX_IBS_SIZE
945
946#define QCA_IBS_WAKE_ACK_EVENT \
947 .type = HCI_IBS_WAKE_ACK, \
948 .hlen = 0, \
949 .loff = 0, \
950 .lsize = 0, \
951 .maxlen = HCI_MAX_IBS_SIZE
952
953static const struct h4_recv_pkt qca_recv_pkts[] = {
Balakrishna Godavarthic614ca32018-10-16 19:51:35 +0530954 { H4_RECV_ACL, .recv = qca_recv_acl_data },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700955 { H4_RECV_SCO, .recv = hci_recv_frame },
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -0700956 { H4_RECV_EVENT, .recv = qca_recv_event },
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700957 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
958 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
959 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
960};
961
962static int qca_recv(struct hci_uart *hu, const void *data, int count)
963{
964 struct qca_data *qca = hu->priv;
965
966 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
967 return -EUNATCH;
968
969 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
970 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
971 if (IS_ERR(qca->rx_skb)) {
972 int err = PTR_ERR(qca->rx_skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100973 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700974 qca->rx_skb = NULL;
975 return err;
976 }
977
978 return count;
979}
980
981static struct sk_buff *qca_dequeue(struct hci_uart *hu)
982{
983 struct qca_data *qca = hu->priv;
984
985 return skb_dequeue(&qca->txq);
986}
987
988static uint8_t qca_get_baudrate_value(int speed)
989{
Prasanna Karthikce26d812015-09-15 12:19:45 +0000990 switch (speed) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700991 case 9600:
992 return QCA_BAUDRATE_9600;
993 case 19200:
994 return QCA_BAUDRATE_19200;
995 case 38400:
996 return QCA_BAUDRATE_38400;
997 case 57600:
998 return QCA_BAUDRATE_57600;
999 case 115200:
1000 return QCA_BAUDRATE_115200;
1001 case 230400:
1002 return QCA_BAUDRATE_230400;
1003 case 460800:
1004 return QCA_BAUDRATE_460800;
1005 case 500000:
1006 return QCA_BAUDRATE_500000;
1007 case 921600:
1008 return QCA_BAUDRATE_921600;
1009 case 1000000:
1010 return QCA_BAUDRATE_1000000;
1011 case 2000000:
1012 return QCA_BAUDRATE_2000000;
1013 case 3000000:
1014 return QCA_BAUDRATE_3000000;
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301015 case 3200000:
1016 return QCA_BAUDRATE_3200000;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001017 case 3500000:
1018 return QCA_BAUDRATE_3500000;
1019 default:
1020 return QCA_BAUDRATE_115200;
1021 }
1022}
1023
1024static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1025{
1026 struct hci_uart *hu = hci_get_drvdata(hdev);
1027 struct qca_data *qca = hu->priv;
1028 struct sk_buff *skb;
1029 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1030
Balakrishna Godavarthibe93a492018-08-03 17:46:30 +05301031 if (baudrate > QCA_BAUDRATE_3200000)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001032 return -EINVAL;
1033
1034 cmd[4] = baudrate;
1035
Jia-Ju Bai25a13e382018-07-23 11:56:51 +08001036 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001037 if (!skb) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001038 bt_dev_err(hdev, "Failed to allocate baudrate packet");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001039 return -ENOMEM;
1040 }
1041
1042 /* Assign commands to change baudrate and packet type. */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001043 skb_put_data(skb, cmd, sizeof(cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001044 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001045
1046 skb_queue_tail(&qca->txq, skb);
1047 hci_uart_tx_wakeup(hu);
1048
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001049 /* Wait for the baudrate change request to be sent */
1050
1051 while (!skb_queue_empty(&qca->txq))
1052 usleep_range(100, 200);
1053
Matthias Kaehlckeecf2b762019-04-23 11:16:52 -07001054 if (hu->serdev)
1055 serdev_device_wait_until_sent(hu->serdev,
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001056 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1057
1058 /* Give the controller time to process the request */
Harish Bandi523760b2019-04-26 19:26:01 +05301059 if (qca_is_wcn399x(qca_soc_type(hu)))
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001060 msleep(10);
1061 else
1062 msleep(300);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001063
1064 return 0;
1065}
1066
Thierry Escande05ba5332018-03-29 21:15:24 +02001067static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1068{
1069 if (hu->serdev)
1070 serdev_device_set_baudrate(hu->serdev, speed);
1071 else
1072 hci_uart_set_baudrate(hu, speed);
1073}
1074
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001075static int qca_send_power_pulse(struct hci_uart *hu, bool on)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301076{
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301077 int ret;
Matthias Kaehlcke94d66712019-02-27 15:52:23 -08001078 int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001079 u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301080
1081 /* These power pulses are single byte command which are sent
1082 * at required baudrate to wcn3990. On wcn3990, we have an external
1083 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1084 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1085 * and also we use the same power inputs to turn on and off for
1086 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1087 * we send a power on pulse at 115200 bps. This algorithm will help to
1088 * save power. Disabling hardware flow control is mandatory while
1089 * sending power pulses to SoC.
1090 */
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301091 bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301092
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301093 serdev_device_write_flush(hu->serdev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301094 hci_uart_set_flow_control(hu, true);
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301095 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1096 if (ret < 0) {
1097 bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1098 return ret;
1099 }
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301100
Balakrishna Godavarthif9558272019-02-04 20:36:41 +05301101 serdev_device_wait_until_sent(hu->serdev, timeout);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301102 hci_uart_set_flow_control(hu, false);
1103
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001104 /* Give to controller time to boot/shutdown */
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001105 if (on)
1106 msleep(100);
Matthias Kaehlcke0ebcddd2019-02-26 11:46:47 -08001107 else
1108 msleep(10);
Matthias Kaehlckead571d72019-02-26 11:46:46 -08001109
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301110 return 0;
1111}
1112
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301113static unsigned int qca_get_speed(struct hci_uart *hu,
1114 enum qca_speed_type speed_type)
1115{
1116 unsigned int speed = 0;
1117
1118 if (speed_type == QCA_INIT_SPEED) {
1119 if (hu->init_speed)
1120 speed = hu->init_speed;
1121 else if (hu->proto->init_speed)
1122 speed = hu->proto->init_speed;
1123 } else {
1124 if (hu->oper_speed)
1125 speed = hu->oper_speed;
1126 else if (hu->proto->oper_speed)
1127 speed = hu->proto->oper_speed;
1128 }
1129
1130 return speed;
1131}
1132
1133static int qca_check_speeds(struct hci_uart *hu)
1134{
Harish Bandi523760b2019-04-26 19:26:01 +05301135 if (qca_is_wcn399x(qca_soc_type(hu))) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301136 if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1137 !qca_get_speed(hu, QCA_OPER_SPEED))
1138 return -EINVAL;
1139 } else {
1140 if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1141 !qca_get_speed(hu, QCA_OPER_SPEED))
1142 return -EINVAL;
1143 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301144
1145 return 0;
1146}
1147
1148static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1149{
1150 unsigned int speed, qca_baudrate;
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001151 struct qca_data *qca = hu->priv;
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301152 int ret = 0;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301153
1154 if (speed_type == QCA_INIT_SPEED) {
1155 speed = qca_get_speed(hu, QCA_INIT_SPEED);
1156 if (speed)
1157 host_set_baudrate(hu, speed);
1158 } else {
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001159 enum qca_btsoc_type soc_type = qca_soc_type(hu);
1160
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301161 speed = qca_get_speed(hu, QCA_OPER_SPEED);
1162 if (!speed)
1163 return 0;
1164
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301165 /* Disable flow control for wcn3990 to deassert RTS while
1166 * changing the baudrate of chip and host.
1167 */
Harish Bandi523760b2019-04-26 19:26:01 +05301168 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301169 hci_uart_set_flow_control(hu, true);
1170
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001171 if (soc_type == QCA_WCN3990) {
1172 reinit_completion(&qca->drop_ev_comp);
1173 set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1174 }
1175
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301176 qca_baudrate = qca_get_baudrate_value(speed);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301177 bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301178 ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1179 if (ret)
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301180 goto error;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301181
1182 host_set_baudrate(hu, speed);
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301183
1184error:
Jeffrey Hugobba79fe2019-10-21 07:18:27 -07001185 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301186 hci_uart_set_flow_control(hu, false);
Matthias Kaehlcke2faa3f12019-05-21 12:53:07 -07001187
1188 if (soc_type == QCA_WCN3990) {
1189 /* Wait for the controller to send the vendor event
1190 * for the baudrate change command.
1191 */
1192 if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1193 msecs_to_jiffies(100))) {
1194 bt_dev_err(hu->hdev,
1195 "Failed to change controller baudrate\n");
1196 ret = -ETIMEDOUT;
1197 }
1198
1199 clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1200 }
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301201 }
1202
Balakrishna Godavarthi78e8fa22019-02-04 20:36:42 +05301203 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301204}
1205
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301206static int qca_wcn3990_init(struct hci_uart *hu)
1207{
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301208 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301209 int ret;
1210
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301211 /* Check for vregs status, may be hci down has turned
1212 * off the voltage regulator.
1213 */
1214 qcadev = serdev_device_get_drvdata(hu->serdev);
1215 if (!qcadev->bt_power->vregs_on) {
1216 serdev_device_close(hu->serdev);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001217 ret = qca_regulator_enable(qcadev);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301218 if (ret)
1219 return ret;
1220
1221 ret = serdev_device_open(hu->serdev);
1222 if (ret) {
1223 bt_dev_err(hu->hdev, "failed to open port");
1224 return ret;
1225 }
1226 }
1227
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301228 /* Forcefully enable wcn3990 to enter in to boot mode. */
1229 host_set_baudrate(hu, 2400);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001230 ret = qca_send_power_pulse(hu, false);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301231 if (ret)
1232 return ret;
1233
1234 qca_set_speed(hu, QCA_INIT_SPEED);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001235 ret = qca_send_power_pulse(hu, true);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301236 if (ret)
1237 return ret;
1238
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301239 /* Now the device is in ready state to communicate with host.
1240 * To sync host with device we need to reopen port.
1241 * Without this, we will have RTS and CTS synchronization
1242 * issues.
1243 */
1244 serdev_device_close(hu->serdev);
1245 ret = serdev_device_open(hu->serdev);
1246 if (ret) {
1247 bt_dev_err(hu->hdev, "failed to open port");
1248 return ret;
1249 }
1250
1251 hci_uart_set_flow_control(hu, false);
1252
1253 return 0;
1254}
1255
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001256static int qca_setup(struct hci_uart *hu)
1257{
1258 struct hci_dev *hdev = hu->hdev;
1259 struct qca_data *qca = hu->priv;
1260 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
Matthias Kaehlcke4fdd5a42019-03-11 11:38:31 -07001261 enum qca_btsoc_type soc_type = qca_soc_type(hu);
Rocky Liao99c905c2019-06-06 17:40:30 +08001262 const char *firmware_name = qca_get_firmware_name(hu);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001263 int ret;
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301264 int soc_ver = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001265
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301266 ret = qca_check_speeds(hu);
1267 if (ret)
1268 return ret;
1269
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001270 /* Patch downloading has to be done without IBS mode */
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001271 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001272
Rocky Liaoe14c1672019-08-21 14:23:39 +08001273 /* Enable controller to do both LE scan and BR/EDR inquiry
1274 * simultaneously.
1275 */
1276 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1277
Harish Bandi523760b2019-04-26 19:26:01 +05301278 if (qca_is_wcn399x(soc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301279 bt_dev_info(hdev, "setting up wcn3990");
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301280
1281 /* Enable NON_PERSISTENT_SETUP QUIRK to ensure to execute
1282 * setup for every hci up.
1283 */
1284 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
Matthias Kaehlcke5971752d2019-02-19 12:05:59 -08001285 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301286 hu->hdev->shutdown = qca_power_off;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301287 ret = qca_wcn3990_init(hu);
1288 if (ret)
1289 return ret;
1290
1291 ret = qca_read_soc_version(hdev, &soc_ver);
1292 if (ret)
1293 return ret;
1294 } else {
1295 bt_dev_info(hdev, "ROME setup");
1296 qca_set_speed(hu, QCA_INIT_SPEED);
1297 }
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001298
1299 /* Setup user speed if needed */
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301300 speed = qca_get_speed(hu, QCA_OPER_SPEED);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001301 if (speed) {
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301302 ret = qca_set_speed(hu, QCA_OPER_SPEED);
1303 if (ret)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001304 return ret;
Balakrishna Godavarthi83d9c5e2018-08-03 17:46:29 +05301305
1306 qca_baudrate = qca_get_baudrate_value(speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001307 }
1308
Harish Bandi523760b2019-04-26 19:26:01 +05301309 if (!qca_is_wcn399x(soc_type)) {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301310 /* Get QCA version information */
1311 ret = qca_read_soc_version(hdev, &soc_ver);
1312 if (ret)
1313 return ret;
1314 }
Balakrishna Godavarthiaadebac2018-08-03 17:46:28 +05301315
1316 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001317 /* Setup patch / NVM configurations */
Rocky Liao99c905c2019-06-06 17:40:30 +08001318 ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1319 firmware_name);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001320 if (!ret) {
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001321 set_bit(QCA_IBS_ENABLED, &qca->flags);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001322 qca_debugfs_init(hdev);
Loic Poulainba8f3592017-11-06 12:16:56 +01001323 } else if (ret == -ENOENT) {
1324 /* No patch/nvm-config found, run with original fw/config */
1325 ret = 0;
Amit Pundir7dc5fe02018-04-16 12:10:24 +05301326 } else if (ret == -EAGAIN) {
1327 /*
1328 * Userspace firmware loader will return -EAGAIN in case no
1329 * patch/nvm-config is found, so run with original fw/config.
1330 */
1331 ret = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001332 }
1333
1334 /* Setup bdaddr */
Harish Bandi523760b2019-04-26 19:26:01 +05301335 if (qca_is_wcn399x(soc_type))
Balakrishna Godavarthi5c0a10012019-01-16 18:01:15 +05301336 hu->hdev->set_bdaddr = qca_set_bdaddr;
1337 else
1338 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001339
1340 return ret;
1341}
1342
Nishka Dasgupta2edc9c52019-08-15 11:21:49 +05301343static const struct hci_uart_proto qca_proto = {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001344 .id = HCI_UART_QCA,
1345 .name = "QCA",
Marcel Holtmannaee61f72015-10-20 21:30:45 +02001346 .manufacturer = 29,
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001347 .init_speed = 115200,
1348 .oper_speed = 3000000,
1349 .open = qca_open,
1350 .close = qca_close,
1351 .flush = qca_flush,
1352 .setup = qca_setup,
1353 .recv = qca_recv,
1354 .enqueue = qca_enqueue,
1355 .dequeue = qca_dequeue,
1356};
1357
Harish Bandi523760b2019-04-26 19:26:01 +05301358static const struct qca_vreg_data qca_soc_data_wcn3990 = {
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301359 .soc_type = QCA_WCN3990,
1360 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001361 { "vddio", 15000 },
1362 { "vddxo", 80000 },
1363 { "vddrf", 300000 },
1364 { "vddch0", 450000 },
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301365 },
1366 .num_vregs = 4,
1367};
1368
Harish Bandi523760b2019-04-26 19:26:01 +05301369static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1370 .soc_type = QCA_WCN3998,
1371 .vregs = (struct qca_vreg []) {
Bjorn Anderssonf2edd662019-10-17 22:24:02 -07001372 { "vddio", 10000 },
1373 { "vddxo", 80000 },
1374 { "vddrf", 300000 },
1375 { "vddch0", 450000 },
Harish Bandi523760b2019-04-26 19:26:01 +05301376 },
1377 .num_vregs = 4,
1378};
1379
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301380static void qca_power_shutdown(struct hci_uart *hu)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301381{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001382 struct qca_serdev *qcadev;
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301383 struct qca_data *qca = hu->priv;
1384 unsigned long flags;
1385
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001386 qcadev = serdev_device_get_drvdata(hu->serdev);
1387
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301388 /* From this point we go into power off state. But serial port is
1389 * still open, stop queueing the IBS data and flush all the buffered
1390 * data in skb's.
1391 */
1392 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
Matthias Kaehlcke62a91992019-04-29 16:21:30 -07001393 clear_bit(QCA_IBS_ENABLED, &qca->flags);
Balakrishna Godavarthi035a9602019-02-04 20:36:43 +05301394 qca_flush(hu);
1395 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1396
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301397 host_set_baudrate(hu, 2400);
Matthias Kaehlcke9836b802019-02-26 11:46:45 -08001398 qca_send_power_pulse(hu, false);
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001399 qca_regulator_disable(qcadev);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301400}
1401
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301402static int qca_power_off(struct hci_dev *hdev)
1403{
1404 struct hci_uart *hu = hci_get_drvdata(hdev);
1405
Harish Bandia2780882019-07-12 10:39:40 +05301406 /* Perform pre shutdown command */
1407 qca_send_pre_shutdown_cmd(hdev);
1408
Harish Bandi010376a2019-08-30 17:58:36 +05301409 usleep_range(8000, 10000);
1410
Balakrishna Godavarthi3e4be652018-09-24 20:14:45 +05301411 qca_power_shutdown(hu);
1412 return 0;
1413}
1414
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001415static int qca_regulator_enable(struct qca_serdev *qcadev)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301416{
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001417 struct qca_power *power = qcadev->bt_power;
1418 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301419
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001420 /* Already enabled */
1421 if (power->vregs_on)
1422 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301423
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001424 BT_DBG("enabling %d regulators)", power->num_vregs);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301425
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001426 ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1427 if (ret)
1428 return ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301429
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001430 power->vregs_on = true;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301431
Bjorn Andersson163d42f2019-10-17 22:24:03 -07001432 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301433}
1434
Bjorn Anderssona9314e72019-10-17 22:24:04 -07001435static void qca_regulator_disable(struct qca_serdev *qcadev)
1436{
1437 struct qca_power *power;
1438
1439 if (!qcadev)
1440 return;
1441
1442 power = qcadev->bt_power;
1443
1444 /* Already disabled? */
1445 if (!power->vregs_on)
1446 return;
1447
1448 regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1449 power->vregs_on = false;
1450}
1451
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301452static int qca_init_regulators(struct qca_power *qca,
1453 const struct qca_vreg *vregs, size_t num_vregs)
1454{
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001455 struct regulator_bulk_data *bulk;
1456 int ret;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301457 int i;
1458
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001459 bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1460 if (!bulk)
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301461 return -ENOMEM;
1462
1463 for (i = 0; i < num_vregs; i++)
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001464 bulk[i].supply = vregs[i].name;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301465
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001466 ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1467 if (ret < 0)
1468 return ret;
1469
1470 for (i = 0; i < num_vregs; i++) {
1471 ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
1472 if (ret)
1473 return ret;
1474 }
1475
1476 qca->vreg_bulk = bulk;
Bjorn Andersson163d42f2019-10-17 22:24:03 -07001477 qca->num_vregs = num_vregs;
Bjorn Anderssonc29ff102019-10-17 22:24:01 -07001478
1479 return 0;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301480}
1481
Thierry Escande05ba5332018-03-29 21:15:24 +02001482static int qca_serdev_probe(struct serdev_device *serdev)
1483{
1484 struct qca_serdev *qcadev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301485 const struct qca_vreg_data *data;
Thierry Escande05ba5332018-03-29 21:15:24 +02001486 int err;
1487
1488 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1489 if (!qcadev)
1490 return -ENOMEM;
1491
1492 qcadev->serdev_hu.serdev = serdev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301493 data = of_device_get_match_data(&serdev->dev);
Thierry Escande05ba5332018-03-29 21:15:24 +02001494 serdev_device_set_drvdata(serdev, qcadev);
Rocky Liao99c905c2019-06-06 17:40:30 +08001495 device_property_read_string(&serdev->dev, "firmware-name",
1496 &qcadev->firmware_name);
Harish Bandi523760b2019-04-26 19:26:01 +05301497 if (data && qca_is_wcn399x(data->soc_type)) {
1498 qcadev->btsoc_type = data->soc_type;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301499 qcadev->bt_power = devm_kzalloc(&serdev->dev,
1500 sizeof(struct qca_power),
1501 GFP_KERNEL);
1502 if (!qcadev->bt_power)
1503 return -ENOMEM;
Thierry Escande05ba5332018-03-29 21:15:24 +02001504
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301505 qcadev->bt_power->dev = &serdev->dev;
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301506 err = qca_init_regulators(qcadev->bt_power, data->vregs,
1507 data->num_vregs);
1508 if (err) {
1509 BT_ERR("Failed to init regulators:%d", err);
1510 goto out;
1511 }
1512
1513 qcadev->bt_power->vregs_on = false;
1514
1515 device_property_read_u32(&serdev->dev, "max-speed",
1516 &qcadev->oper_speed);
1517 if (!qcadev->oper_speed)
1518 BT_DBG("UART will pick default operating speed");
1519
1520 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1521 if (err) {
1522 BT_ERR("wcn3990 serdev registration failed");
1523 goto out;
1524 }
1525 } else {
1526 qcadev->btsoc_type = QCA_ROME;
1527 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1528 GPIOD_OUT_LOW);
1529 if (IS_ERR(qcadev->bt_en)) {
1530 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1531 return PTR_ERR(qcadev->bt_en);
1532 }
1533
1534 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1535 if (IS_ERR(qcadev->susclk)) {
1536 dev_err(&serdev->dev, "failed to acquire clk\n");
1537 return PTR_ERR(qcadev->susclk);
1538 }
1539
1540 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1541 if (err)
1542 return err;
1543
1544 err = clk_prepare_enable(qcadev->susclk);
1545 if (err)
1546 return err;
1547
1548 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1549 if (err)
1550 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02001551 }
1552
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301553out: return err;
Thierry Escande05ba5332018-03-29 21:15:24 +02001554
Thierry Escande05ba5332018-03-29 21:15:24 +02001555}
1556
1557static void qca_serdev_remove(struct serdev_device *serdev)
1558{
1559 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1560
Harish Bandi523760b2019-04-26 19:26:01 +05301561 if (qca_is_wcn399x(qcadev->btsoc_type))
Balakrishna Godavarthic2d78272018-08-22 17:50:05 +05301562 qca_power_shutdown(&qcadev->serdev_hu);
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301563 else
1564 clk_disable_unprepare(qcadev->susclk);
Thierry Escande05ba5332018-03-29 21:15:24 +02001565
Balakrishna Godavarthifa9ad872018-08-03 17:46:32 +05301566 hci_uart_unregister_device(&qcadev->serdev_hu);
Thierry Escande05ba5332018-03-29 21:15:24 +02001567}
1568
Claire Chang41d5b252019-10-31 18:46:14 +08001569static int __maybe_unused qca_suspend(struct device *dev)
1570{
1571 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1572 struct hci_uart *hu = hci_get_drvdata(hdev);
1573 struct qca_data *qca = hu->priv;
1574 unsigned long flags;
1575 int ret = 0;
1576 u8 cmd;
1577
1578 set_bit(QCA_SUSPENDING, &qca->flags);
1579
1580 /* Device is downloading patch or doesn't support in-band sleep. */
1581 if (!test_bit(QCA_IBS_ENABLED, &qca->flags))
1582 return 0;
1583
1584 cancel_work_sync(&qca->ws_awake_device);
1585 cancel_work_sync(&qca->ws_awake_rx);
1586
1587 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
1588 flags, SINGLE_DEPTH_NESTING);
1589
1590 switch (qca->tx_ibs_state) {
1591 case HCI_IBS_TX_WAKING:
1592 del_timer(&qca->wake_retrans_timer);
1593 /* Fall through */
1594 case HCI_IBS_TX_AWAKE:
1595 del_timer(&qca->tx_idle_timer);
1596
1597 serdev_device_write_flush(hu->serdev);
1598 cmd = HCI_IBS_SLEEP_IND;
1599 ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1600
1601 if (ret < 0) {
1602 BT_ERR("Failed to send SLEEP to device");
1603 break;
1604 }
1605
1606 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
1607 qca->ibs_sent_slps++;
1608
1609 qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off);
1610 break;
1611
1612 case HCI_IBS_TX_ASLEEP:
1613 break;
1614
1615 default:
1616 BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
1617 ret = -EINVAL;
1618 break;
1619 }
1620
1621 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1622
1623 if (ret < 0)
1624 goto error;
1625
1626 serdev_device_wait_until_sent(hu->serdev,
1627 msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1628
1629 /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
1630 * to sleep, so that the packet does not wake the system later.
1631 */
1632
1633 ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
1634 qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
1635 msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
1636
1637 if (ret > 0)
1638 return 0;
1639
1640 if (ret == 0)
1641 ret = -ETIMEDOUT;
1642
1643error:
1644 clear_bit(QCA_SUSPENDING, &qca->flags);
1645
1646 return ret;
1647}
1648
1649static int __maybe_unused qca_resume(struct device *dev)
1650{
1651 struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1652 struct hci_uart *hu = hci_get_drvdata(hdev);
1653 struct qca_data *qca = hu->priv;
1654
1655 clear_bit(QCA_SUSPENDING, &qca->flags);
1656
1657 return 0;
1658}
1659
1660static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
1661
Thierry Escande05ba5332018-03-29 21:15:24 +02001662static const struct of_device_id qca_bluetooth_of_match[] = {
1663 { .compatible = "qcom,qca6174-bt" },
Harish Bandi523760b2019-04-26 19:26:01 +05301664 { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
1665 { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
Thierry Escande05ba5332018-03-29 21:15:24 +02001666 { /* sentinel */ }
1667};
1668MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1669
1670static struct serdev_device_driver qca_serdev_driver = {
1671 .probe = qca_serdev_probe,
1672 .remove = qca_serdev_remove,
1673 .driver = {
1674 .name = "hci_uart_qca",
1675 .of_match_table = qca_bluetooth_of_match,
Claire Chang41d5b252019-10-31 18:46:14 +08001676 .pm = &qca_pm_ops,
Thierry Escande05ba5332018-03-29 21:15:24 +02001677 },
1678};
1679
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001680int __init qca_init(void)
1681{
Thierry Escande05ba5332018-03-29 21:15:24 +02001682 serdev_device_driver_register(&qca_serdev_driver);
1683
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001684 return hci_uart_register_proto(&qca_proto);
1685}
1686
1687int __exit qca_deinit(void)
1688{
Thierry Escande05ba5332018-03-29 21:15:24 +02001689 serdev_device_driver_unregister(&qca_serdev_driver);
1690
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001691 return hci_uart_unregister_proto(&qca_proto);
1692}