blob: 8431be3a837f2c831f6b32bc3a56bcf4c21dd853 [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.
8 * Copyright (c) 2010, 2012 The Linux Foundation. All rights reserved.
9 *
10 * Acknowledgements:
11 * This file is based on hci_ll.c, which was...
12 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
13 * which was in turn based on hci_h4.c, which was written
14 * by Maxim Krasnyansky and Marcel Holtmann.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <linux/kernel.h>
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>
Thierry Escande05ba5332018-03-29 21:15:24 +020034#include <linux/gpio/consumer.h>
35#include <linux/mod_devicetable.h>
36#include <linux/module.h>
37#include <linux/serdev.h>
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070038
39#include <net/bluetooth/bluetooth.h>
40#include <net/bluetooth/hci_core.h>
41
42#include "hci_uart.h"
43#include "btqca.h"
44
45/* HCI_IBS protocol messages */
46#define HCI_IBS_SLEEP_IND 0xFE
47#define HCI_IBS_WAKE_IND 0xFD
48#define HCI_IBS_WAKE_ACK 0xFC
Marcel Holtmannf81b0012015-08-30 23:05:32 +020049#define HCI_MAX_IBS_SIZE 10
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070050
51/* Controller states */
52#define STATE_IN_BAND_SLEEP_ENABLED 1
53
Marcel Holtmannf81b0012015-08-30 23:05:32 +020054#define IBS_WAKE_RETRANS_TIMEOUT_MS 100
55#define IBS_TX_IDLE_TIMEOUT_MS 2000
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070056#define BAUDRATE_SETTLE_TIMEOUT_MS 300
57
Thierry Escande05ba5332018-03-29 21:15:24 +020058/* susclk rate */
59#define SUSCLK_RATE_32KHZ 32768
60
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070061/* HCI_IBS transmit side sleep protocol states */
62enum tx_ibs_states {
63 HCI_IBS_TX_ASLEEP,
64 HCI_IBS_TX_WAKING,
65 HCI_IBS_TX_AWAKE,
66};
67
68/* HCI_IBS receive side sleep protocol states */
69enum rx_states {
70 HCI_IBS_RX_ASLEEP,
71 HCI_IBS_RX_AWAKE,
72};
73
74/* HCI_IBS transmit and receive side clock state vote */
75enum hci_ibs_clock_state_vote {
76 HCI_IBS_VOTE_STATS_UPDATE,
77 HCI_IBS_TX_VOTE_CLOCK_ON,
78 HCI_IBS_TX_VOTE_CLOCK_OFF,
79 HCI_IBS_RX_VOTE_CLOCK_ON,
80 HCI_IBS_RX_VOTE_CLOCK_OFF,
81};
82
83struct qca_data {
84 struct hci_uart *hu;
85 struct sk_buff *rx_skb;
86 struct sk_buff_head txq;
87 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
88 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
89 u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
90 u8 rx_ibs_state; /* HCI_IBS receive side power state */
Viresh Kumar621a5f72015-09-26 15:04:07 -070091 bool tx_vote; /* Clock must be on for TX */
92 bool rx_vote; /* Clock must be on for RX */
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -070093 struct timer_list tx_idle_timer;
94 u32 tx_idle_delay;
95 struct timer_list wake_retrans_timer;
96 u32 wake_retrans;
97 struct workqueue_struct *workqueue;
98 struct work_struct ws_awake_rx;
99 struct work_struct ws_awake_device;
100 struct work_struct ws_rx_vote_off;
101 struct work_struct ws_tx_vote_off;
102 unsigned long flags;
103
104 /* For debugging purpose */
105 u64 ibs_sent_wacks;
106 u64 ibs_sent_slps;
107 u64 ibs_sent_wakes;
108 u64 ibs_recv_wacks;
109 u64 ibs_recv_slps;
110 u64 ibs_recv_wakes;
111 u64 vote_last_jif;
112 u32 vote_on_ms;
113 u32 vote_off_ms;
114 u64 tx_votes_on;
115 u64 rx_votes_on;
116 u64 tx_votes_off;
117 u64 rx_votes_off;
118 u64 votes_on;
119 u64 votes_off;
120};
121
Thierry Escande05ba5332018-03-29 21:15:24 +0200122struct qca_serdev {
123 struct hci_uart serdev_hu;
124 struct gpio_desc *bt_en;
125 struct clk *susclk;
126};
127
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700128static void __serial_clock_on(struct tty_struct *tty)
129{
130 /* TODO: Some chipset requires to enable UART clock on client
131 * side to save power consumption or manual work is required.
132 * Please put your code to control UART clock here if needed
133 */
134}
135
136static void __serial_clock_off(struct tty_struct *tty)
137{
138 /* TODO: Some chipset requires to disable UART clock on client
139 * side to save power consumption or manual work is required.
140 * Please put your code to control UART clock off here if needed
141 */
142}
143
144/* serial_clock_vote needs to be called with the ibs lock held */
145static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
146{
147 struct qca_data *qca = hu->priv;
148 unsigned int diff;
149
150 bool old_vote = (qca->tx_vote | qca->rx_vote);
151 bool new_vote;
152
153 switch (vote) {
154 case HCI_IBS_VOTE_STATS_UPDATE:
155 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
156
157 if (old_vote)
158 qca->vote_off_ms += diff;
159 else
160 qca->vote_on_ms += diff;
161 return;
162
163 case HCI_IBS_TX_VOTE_CLOCK_ON:
164 qca->tx_vote = true;
165 qca->tx_votes_on++;
166 new_vote = true;
167 break;
168
169 case HCI_IBS_RX_VOTE_CLOCK_ON:
170 qca->rx_vote = true;
171 qca->rx_votes_on++;
172 new_vote = true;
173 break;
174
175 case HCI_IBS_TX_VOTE_CLOCK_OFF:
176 qca->tx_vote = false;
177 qca->tx_votes_off++;
178 new_vote = qca->rx_vote | qca->tx_vote;
179 break;
180
181 case HCI_IBS_RX_VOTE_CLOCK_OFF:
182 qca->rx_vote = false;
183 qca->rx_votes_off++;
184 new_vote = qca->rx_vote | qca->tx_vote;
185 break;
186
187 default:
188 BT_ERR("Voting irregularity");
189 return;
190 }
191
192 if (new_vote != old_vote) {
193 if (new_vote)
194 __serial_clock_on(hu->tty);
195 else
196 __serial_clock_off(hu->tty);
197
Prasanna Karthikce26d812015-09-15 12:19:45 +0000198 BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
199 vote ? "true" : "false");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700200
201 diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
202
203 if (new_vote) {
204 qca->votes_on++;
205 qca->vote_off_ms += diff;
206 } else {
207 qca->votes_off++;
208 qca->vote_on_ms += diff;
209 }
210 qca->vote_last_jif = jiffies;
211 }
212}
213
214/* Builds and sends an HCI_IBS command packet.
215 * These are very simple packets with only 1 cmd byte.
216 */
217static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
218{
219 int err = 0;
220 struct sk_buff *skb = NULL;
221 struct qca_data *qca = hu->priv;
222
223 BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
224
225 skb = bt_skb_alloc(1, GFP_ATOMIC);
226 if (!skb) {
227 BT_ERR("Failed to allocate memory for HCI_IBS packet");
228 return -ENOMEM;
229 }
230
231 /* Assign HCI_IBS type */
Johannes Berg634fef62017-06-16 14:29:24 +0200232 skb_put_u8(skb, cmd);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700233
234 skb_queue_tail(&qca->txq, skb);
235
236 return err;
237}
238
239static void qca_wq_awake_device(struct work_struct *work)
240{
241 struct qca_data *qca = container_of(work, struct qca_data,
242 ws_awake_device);
243 struct hci_uart *hu = qca->hu;
244 unsigned long retrans_delay;
245
246 BT_DBG("hu %p wq awake device", hu);
247
248 /* Vote for serial clock */
249 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
250
251 spin_lock(&qca->hci_ibs_lock);
252
253 /* Send wake indication to device */
254 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
255 BT_ERR("Failed to send WAKE to device");
256
257 qca->ibs_sent_wakes++;
258
259 /* Start retransmit timer */
260 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
261 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
262
263 spin_unlock(&qca->hci_ibs_lock);
264
265 /* Actually send the packets */
266 hci_uart_tx_wakeup(hu);
267}
268
269static void qca_wq_awake_rx(struct work_struct *work)
270{
271 struct qca_data *qca = container_of(work, struct qca_data,
272 ws_awake_rx);
273 struct hci_uart *hu = qca->hu;
274
275 BT_DBG("hu %p wq awake rx", hu);
276
277 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
278
279 spin_lock(&qca->hci_ibs_lock);
280 qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
281
282 /* Always acknowledge device wake up,
283 * sending IBS message doesn't count as TX ON.
284 */
285 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
286 BT_ERR("Failed to acknowledge device wake up");
287
288 qca->ibs_sent_wacks++;
289
290 spin_unlock(&qca->hci_ibs_lock);
291
292 /* Actually send the packets */
293 hci_uart_tx_wakeup(hu);
294}
295
296static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
297{
298 struct qca_data *qca = container_of(work, struct qca_data,
299 ws_rx_vote_off);
300 struct hci_uart *hu = qca->hu;
301
302 BT_DBG("hu %p rx clock vote off", hu);
303
304 serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
305}
306
307static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
308{
309 struct qca_data *qca = container_of(work, struct qca_data,
310 ws_tx_vote_off);
311 struct hci_uart *hu = qca->hu;
312
313 BT_DBG("hu %p tx clock vote off", hu);
314
315 /* Run HCI tx handling unlocked */
316 hci_uart_tx_wakeup(hu);
317
318 /* Now that message queued to tty driver, vote for tty clocks off.
319 * It is up to the tty driver to pend the clocks off until tx done.
320 */
321 serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
322}
323
Kees Cook04356052017-10-04 17:54:29 -0700324static void hci_ibs_tx_idle_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700325{
Kees Cook04356052017-10-04 17:54:29 -0700326 struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
327 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700328 unsigned long flags;
329
330 BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
331
332 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
333 flags, SINGLE_DEPTH_NESTING);
334
335 switch (qca->tx_ibs_state) {
336 case HCI_IBS_TX_AWAKE:
337 /* TX_IDLE, go to SLEEP */
338 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
339 BT_ERR("Failed to send SLEEP to device");
340 break;
341 }
342 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
343 qca->ibs_sent_slps++;
344 queue_work(qca->workqueue, &qca->ws_tx_vote_off);
345 break;
346
347 case HCI_IBS_TX_ASLEEP:
348 case HCI_IBS_TX_WAKING:
349 /* Fall through */
350
351 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000352 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700353 break;
354 }
355
356 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
357}
358
Kees Cook04356052017-10-04 17:54:29 -0700359static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700360{
Kees Cook04356052017-10-04 17:54:29 -0700361 struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
362 struct hci_uart *hu = qca->hu;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700363 unsigned long flags, retrans_delay;
Prasanna Karthika9137182015-09-28 08:03:24 +0000364 bool retransmit = false;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700365
366 BT_DBG("hu %p wake retransmit timeout in %d state",
367 hu, qca->tx_ibs_state);
368
369 spin_lock_irqsave_nested(&qca->hci_ibs_lock,
370 flags, SINGLE_DEPTH_NESTING);
371
372 switch (qca->tx_ibs_state) {
373 case HCI_IBS_TX_WAKING:
374 /* No WAKE_ACK, retransmit WAKE */
Prasanna Karthika9137182015-09-28 08:03:24 +0000375 retransmit = true;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700376 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
377 BT_ERR("Failed to acknowledge device wake up");
378 break;
379 }
380 qca->ibs_sent_wakes++;
381 retrans_delay = msecs_to_jiffies(qca->wake_retrans);
382 mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
383 break;
384
385 case HCI_IBS_TX_ASLEEP:
386 case HCI_IBS_TX_AWAKE:
387 /* Fall through */
388
389 default:
Colin Ian Kinge059a462017-02-17 19:58:10 +0000390 BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700391 break;
392 }
393
394 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
395
396 if (retransmit)
397 hci_uart_tx_wakeup(hu);
398}
399
400/* Initialize protocol */
401static int qca_open(struct hci_uart *hu)
402{
Thierry Escande05ba5332018-03-29 21:15:24 +0200403 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700404 struct qca_data *qca;
405
406 BT_DBG("hu %p qca_open", hu);
407
Jia-Ju Bai25a13e32018-07-23 11:56:51 +0800408 qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700409 if (!qca)
410 return -ENOMEM;
411
412 skb_queue_head_init(&qca->txq);
413 skb_queue_head_init(&qca->tx_wait_q);
414 spin_lock_init(&qca->hci_ibs_lock);
Bhaktipriya Shridharfac9a602016-08-30 22:42:53 +0530415 qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700416 if (!qca->workqueue) {
417 BT_ERR("QCA Workqueue not initialized properly");
418 kfree(qca);
419 return -ENOMEM;
420 }
421
422 INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
423 INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
424 INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
425 INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
426
427 qca->hu = hu;
428
429 /* Assume we start with both sides asleep -- extra wakes OK */
430 qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
431 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
432
433 /* clocks actually on, but we start votes off */
434 qca->tx_vote = false;
435 qca->rx_vote = false;
436 qca->flags = 0;
437
438 qca->ibs_sent_wacks = 0;
439 qca->ibs_sent_slps = 0;
440 qca->ibs_sent_wakes = 0;
441 qca->ibs_recv_wacks = 0;
442 qca->ibs_recv_slps = 0;
443 qca->ibs_recv_wakes = 0;
444 qca->vote_last_jif = jiffies;
445 qca->vote_on_ms = 0;
446 qca->vote_off_ms = 0;
447 qca->votes_on = 0;
448 qca->votes_off = 0;
449 qca->tx_votes_on = 0;
450 qca->tx_votes_off = 0;
451 qca->rx_votes_on = 0;
452 qca->rx_votes_off = 0;
453
454 hu->priv = qca;
455
Kees Cook04356052017-10-04 17:54:29 -0700456 timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700457 qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
458
Kees Cook04356052017-10-04 17:54:29 -0700459 timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700460 qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
461
Thierry Escande05ba5332018-03-29 21:15:24 +0200462 if (hu->serdev) {
463 serdev_device_open(hu->serdev);
464
465 qcadev = serdev_device_get_drvdata(hu->serdev);
466 gpiod_set_value_cansleep(qcadev->bt_en, 1);
467 }
468
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700469 BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
470 qca->tx_idle_delay, qca->wake_retrans);
471
472 return 0;
473}
474
475static void qca_debugfs_init(struct hci_dev *hdev)
476{
477 struct hci_uart *hu = hci_get_drvdata(hdev);
478 struct qca_data *qca = hu->priv;
479 struct dentry *ibs_dir;
480 umode_t mode;
481
482 if (!hdev->debugfs)
483 return;
484
485 ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
486
487 /* read only */
488 mode = S_IRUGO;
489 debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
490 debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
491 debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
492 &qca->ibs_sent_slps);
493 debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
494 &qca->ibs_sent_wakes);
495 debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
496 &qca->ibs_sent_wacks);
497 debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
498 &qca->ibs_recv_slps);
499 debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
500 &qca->ibs_recv_wakes);
501 debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
502 &qca->ibs_recv_wacks);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700503 debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700504 debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
505 debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
Ben YoungTae Kim10be6c02015-08-13 22:09:42 -0700506 debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700507 debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
508 debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
509 debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
510 debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
511 debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
512 debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
513
514 /* read/write */
515 mode = S_IRUGO | S_IWUSR;
516 debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
517 debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
518 &qca->tx_idle_delay);
519}
520
521/* Flush protocol data */
522static int qca_flush(struct hci_uart *hu)
523{
524 struct qca_data *qca = hu->priv;
525
526 BT_DBG("hu %p qca flush", hu);
527
528 skb_queue_purge(&qca->tx_wait_q);
529 skb_queue_purge(&qca->txq);
530
531 return 0;
532}
533
534/* Close protocol */
535static int qca_close(struct hci_uart *hu)
536{
Thierry Escande05ba5332018-03-29 21:15:24 +0200537 struct qca_serdev *qcadev;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700538 struct qca_data *qca = hu->priv;
539
540 BT_DBG("hu %p qca close", hu);
541
542 serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
543
544 skb_queue_purge(&qca->tx_wait_q);
545 skb_queue_purge(&qca->txq);
546 del_timer(&qca->tx_idle_timer);
547 del_timer(&qca->wake_retrans_timer);
548 destroy_workqueue(qca->workqueue);
549 qca->hu = NULL;
550
Thierry Escande05ba5332018-03-29 21:15:24 +0200551 if (hu->serdev) {
552 serdev_device_close(hu->serdev);
553
554 qcadev = serdev_device_get_drvdata(hu->serdev);
555 gpiod_set_value_cansleep(qcadev->bt_en, 0);
556 }
557
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700558 kfree_skb(qca->rx_skb);
559
560 hu->priv = NULL;
561
562 kfree(qca);
563
564 return 0;
565}
566
567/* Called upon a wake-up-indication from the device.
568 */
569static void device_want_to_wakeup(struct hci_uart *hu)
570{
571 unsigned long flags;
572 struct qca_data *qca = hu->priv;
573
574 BT_DBG("hu %p want to wake up", hu);
575
576 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
577
578 qca->ibs_recv_wakes++;
579
580 switch (qca->rx_ibs_state) {
581 case HCI_IBS_RX_ASLEEP:
582 /* Make sure clock is on - we may have turned clock off since
583 * receiving the wake up indicator awake rx clock.
584 */
585 queue_work(qca->workqueue, &qca->ws_awake_rx);
586 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
587 return;
588
589 case HCI_IBS_RX_AWAKE:
590 /* Always acknowledge device wake up,
591 * sending IBS message doesn't count as TX ON.
592 */
593 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
594 BT_ERR("Failed to acknowledge device wake up");
595 break;
596 }
597 qca->ibs_sent_wacks++;
598 break;
599
600 default:
601 /* Any other state is illegal */
602 BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
603 qca->rx_ibs_state);
604 break;
605 }
606
607 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
608
609 /* Actually send the packets */
610 hci_uart_tx_wakeup(hu);
611}
612
613/* Called upon a sleep-indication from the device.
614 */
615static void device_want_to_sleep(struct hci_uart *hu)
616{
617 unsigned long flags;
618 struct qca_data *qca = hu->priv;
619
620 BT_DBG("hu %p want to sleep", hu);
621
622 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
623
624 qca->ibs_recv_slps++;
625
626 switch (qca->rx_ibs_state) {
627 case HCI_IBS_RX_AWAKE:
628 /* Update state */
629 qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
630 /* Vote off rx clock under workqueue */
631 queue_work(qca->workqueue, &qca->ws_rx_vote_off);
632 break;
633
634 case HCI_IBS_RX_ASLEEP:
635 /* Fall through */
636
637 default:
638 /* Any other state is illegal */
639 BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
640 qca->rx_ibs_state);
641 break;
642 }
643
644 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
645}
646
647/* Called upon wake-up-acknowledgement from the device
648 */
649static void device_woke_up(struct hci_uart *hu)
650{
651 unsigned long flags, idle_delay;
652 struct qca_data *qca = hu->priv;
653 struct sk_buff *skb = NULL;
654
655 BT_DBG("hu %p woke up", hu);
656
657 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
658
659 qca->ibs_recv_wacks++;
660
661 switch (qca->tx_ibs_state) {
662 case HCI_IBS_TX_AWAKE:
663 /* Expect one if we send 2 WAKEs */
664 BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
665 qca->tx_ibs_state);
666 break;
667
668 case HCI_IBS_TX_WAKING:
669 /* Send pending packets */
670 while ((skb = skb_dequeue(&qca->tx_wait_q)))
671 skb_queue_tail(&qca->txq, skb);
672
673 /* Switch timers and change state to HCI_IBS_TX_AWAKE */
674 del_timer(&qca->wake_retrans_timer);
675 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
676 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
677 qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
678 break;
679
680 case HCI_IBS_TX_ASLEEP:
681 /* Fall through */
682
683 default:
684 BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
685 qca->tx_ibs_state);
686 break;
687 }
688
689 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
690
691 /* Actually send the packets */
692 hci_uart_tx_wakeup(hu);
693}
694
695/* Enqueue frame for transmittion (padding, crc, etc) may be called from
696 * two simultaneous tasklets.
697 */
698static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
699{
700 unsigned long flags = 0, idle_delay;
701 struct qca_data *qca = hu->priv;
702
703 BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
704 qca->tx_ibs_state);
705
706 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100707 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700708
709 /* Don't go to sleep in middle of patch download or
710 * Out-Of-Band(GPIOs control) sleep is selected.
711 */
712 if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags)) {
713 skb_queue_tail(&qca->txq, skb);
714 return 0;
715 }
716
717 spin_lock_irqsave(&qca->hci_ibs_lock, flags);
718
719 /* Act according to current state */
720 switch (qca->tx_ibs_state) {
721 case HCI_IBS_TX_AWAKE:
722 BT_DBG("Device awake, sending normally");
723 skb_queue_tail(&qca->txq, skb);
724 idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
725 mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
726 break;
727
728 case HCI_IBS_TX_ASLEEP:
729 BT_DBG("Device asleep, waking up and queueing packet");
730 /* Save packet for later */
731 skb_queue_tail(&qca->tx_wait_q, skb);
732
733 qca->tx_ibs_state = HCI_IBS_TX_WAKING;
734 /* Schedule a work queue to wake up device */
735 queue_work(qca->workqueue, &qca->ws_awake_device);
736 break;
737
738 case HCI_IBS_TX_WAKING:
739 BT_DBG("Device waking up, queueing packet");
740 /* Transient state; just keep packet for later */
741 skb_queue_tail(&qca->tx_wait_q, skb);
742 break;
743
744 default:
745 BT_ERR("Illegal tx state: %d (losing packet)",
746 qca->tx_ibs_state);
747 kfree_skb(skb);
748 break;
749 }
750
751 spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
752
753 return 0;
754}
755
756static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
757{
758 struct hci_uart *hu = hci_get_drvdata(hdev);
759
760 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
761
762 device_want_to_sleep(hu);
763
764 kfree_skb(skb);
765 return 0;
766}
767
768static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
769{
770 struct hci_uart *hu = hci_get_drvdata(hdev);
771
772 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
773
774 device_want_to_wakeup(hu);
775
776 kfree_skb(skb);
777 return 0;
778}
779
780static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
781{
782 struct hci_uart *hu = hci_get_drvdata(hdev);
783
784 BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
785
786 device_woke_up(hu);
787
788 kfree_skb(skb);
789 return 0;
790}
791
792#define QCA_IBS_SLEEP_IND_EVENT \
793 .type = HCI_IBS_SLEEP_IND, \
794 .hlen = 0, \
795 .loff = 0, \
796 .lsize = 0, \
797 .maxlen = HCI_MAX_IBS_SIZE
798
799#define QCA_IBS_WAKE_IND_EVENT \
800 .type = HCI_IBS_WAKE_IND, \
801 .hlen = 0, \
802 .loff = 0, \
803 .lsize = 0, \
804 .maxlen = HCI_MAX_IBS_SIZE
805
806#define QCA_IBS_WAKE_ACK_EVENT \
807 .type = HCI_IBS_WAKE_ACK, \
808 .hlen = 0, \
809 .loff = 0, \
810 .lsize = 0, \
811 .maxlen = HCI_MAX_IBS_SIZE
812
813static const struct h4_recv_pkt qca_recv_pkts[] = {
814 { H4_RECV_ACL, .recv = hci_recv_frame },
815 { H4_RECV_SCO, .recv = hci_recv_frame },
816 { H4_RECV_EVENT, .recv = hci_recv_frame },
817 { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
818 { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
819 { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
820};
821
822static int qca_recv(struct hci_uart *hu, const void *data, int count)
823{
824 struct qca_data *qca = hu->priv;
825
826 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
827 return -EUNATCH;
828
829 qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
830 qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
831 if (IS_ERR(qca->rx_skb)) {
832 int err = PTR_ERR(qca->rx_skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100833 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700834 qca->rx_skb = NULL;
835 return err;
836 }
837
838 return count;
839}
840
841static struct sk_buff *qca_dequeue(struct hci_uart *hu)
842{
843 struct qca_data *qca = hu->priv;
844
845 return skb_dequeue(&qca->txq);
846}
847
848static uint8_t qca_get_baudrate_value(int speed)
849{
Prasanna Karthikce26d812015-09-15 12:19:45 +0000850 switch (speed) {
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700851 case 9600:
852 return QCA_BAUDRATE_9600;
853 case 19200:
854 return QCA_BAUDRATE_19200;
855 case 38400:
856 return QCA_BAUDRATE_38400;
857 case 57600:
858 return QCA_BAUDRATE_57600;
859 case 115200:
860 return QCA_BAUDRATE_115200;
861 case 230400:
862 return QCA_BAUDRATE_230400;
863 case 460800:
864 return QCA_BAUDRATE_460800;
865 case 500000:
866 return QCA_BAUDRATE_500000;
867 case 921600:
868 return QCA_BAUDRATE_921600;
869 case 1000000:
870 return QCA_BAUDRATE_1000000;
871 case 2000000:
872 return QCA_BAUDRATE_2000000;
873 case 3000000:
874 return QCA_BAUDRATE_3000000;
875 case 3500000:
876 return QCA_BAUDRATE_3500000;
877 default:
878 return QCA_BAUDRATE_115200;
879 }
880}
881
882static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
883{
884 struct hci_uart *hu = hci_get_drvdata(hdev);
885 struct qca_data *qca = hu->priv;
886 struct sk_buff *skb;
887 u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
888
889 if (baudrate > QCA_BAUDRATE_3000000)
890 return -EINVAL;
891
892 cmd[4] = baudrate;
893
Jia-Ju Bai25a13e32018-07-23 11:56:51 +0800894 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700895 if (!skb) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100896 bt_dev_err(hdev, "Failed to allocate baudrate packet");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700897 return -ENOMEM;
898 }
899
900 /* Assign commands to change baudrate and packet type. */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200901 skb_put_data(skb, cmd, sizeof(cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100902 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700903
904 skb_queue_tail(&qca->txq, skb);
905 hci_uart_tx_wakeup(hu);
906
907 /* wait 300ms to change new baudrate on controller side
908 * controller will come back after they receive this HCI command
909 * then host can communicate with new baudrate to controller
910 */
911 set_current_state(TASK_UNINTERRUPTIBLE);
912 schedule_timeout(msecs_to_jiffies(BAUDRATE_SETTLE_TIMEOUT_MS));
Thierry Escande99605212018-05-29 18:37:16 +0200913 set_current_state(TASK_RUNNING);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700914
915 return 0;
916}
917
Thierry Escande05ba5332018-03-29 21:15:24 +0200918static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
919{
920 if (hu->serdev)
921 serdev_device_set_baudrate(hu->serdev, speed);
922 else
923 hci_uart_set_baudrate(hu, speed);
924}
925
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700926static int qca_setup(struct hci_uart *hu)
927{
928 struct hci_dev *hdev = hu->hdev;
929 struct qca_data *qca = hu->priv;
930 unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
931 int ret;
932
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100933 bt_dev_info(hdev, "ROME setup");
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700934
935 /* Patch downloading has to be done without IBS mode */
936 clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
937
938 /* Setup initial baudrate */
939 speed = 0;
940 if (hu->init_speed)
941 speed = hu->init_speed;
942 else if (hu->proto->init_speed)
943 speed = hu->proto->init_speed;
944
945 if (speed)
Thierry Escande05ba5332018-03-29 21:15:24 +0200946 host_set_baudrate(hu, speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700947
948 /* Setup user speed if needed */
949 speed = 0;
950 if (hu->oper_speed)
951 speed = hu->oper_speed;
952 else if (hu->proto->oper_speed)
953 speed = hu->proto->oper_speed;
954
955 if (speed) {
956 qca_baudrate = qca_get_baudrate_value(speed);
957
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100958 bt_dev_info(hdev, "Set UART speed to %d", speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700959 ret = qca_set_baudrate(hdev, qca_baudrate);
960 if (ret) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100961 bt_dev_err(hdev, "Failed to change the baud rate (%d)",
962 ret);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700963 return ret;
964 }
Thierry Escande05ba5332018-03-29 21:15:24 +0200965 host_set_baudrate(hu, speed);
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700966 }
967
968 /* Setup patch / NVM configurations */
969 ret = qca_uart_setup_rome(hdev, qca_baudrate);
970 if (!ret) {
971 set_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
972 qca_debugfs_init(hdev);
Loic Poulainba8f3592017-11-06 12:16:56 +0100973 } else if (ret == -ENOENT) {
974 /* No patch/nvm-config found, run with original fw/config */
975 ret = 0;
Amit Pundir7dc5fe02018-04-16 12:10:24 +0530976 } else if (ret == -EAGAIN) {
977 /*
978 * Userspace firmware loader will return -EAGAIN in case no
979 * patch/nvm-config is found, so run with original fw/config.
980 */
981 ret = 0;
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700982 }
983
984 /* Setup bdaddr */
985 hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
986
987 return ret;
988}
989
990static struct hci_uart_proto qca_proto = {
991 .id = HCI_UART_QCA,
992 .name = "QCA",
Marcel Holtmannaee61f72015-10-20 21:30:45 +0200993 .manufacturer = 29,
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -0700994 .init_speed = 115200,
995 .oper_speed = 3000000,
996 .open = qca_open,
997 .close = qca_close,
998 .flush = qca_flush,
999 .setup = qca_setup,
1000 .recv = qca_recv,
1001 .enqueue = qca_enqueue,
1002 .dequeue = qca_dequeue,
1003};
1004
Thierry Escande05ba5332018-03-29 21:15:24 +02001005static int qca_serdev_probe(struct serdev_device *serdev)
1006{
1007 struct qca_serdev *qcadev;
1008 int err;
1009
1010 qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1011 if (!qcadev)
1012 return -ENOMEM;
1013
1014 qcadev->serdev_hu.serdev = serdev;
1015 serdev_device_set_drvdata(serdev, qcadev);
1016
1017 qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
1018 GPIOD_OUT_LOW);
1019 if (IS_ERR(qcadev->bt_en)) {
1020 dev_err(&serdev->dev, "failed to acquire enable gpio\n");
1021 return PTR_ERR(qcadev->bt_en);
1022 }
1023
1024 qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
1025 if (IS_ERR(qcadev->susclk)) {
1026 dev_err(&serdev->dev, "failed to acquire clk\n");
1027 return PTR_ERR(qcadev->susclk);
1028 }
1029
1030 err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1031 if (err)
1032 return err;
1033
1034 err = clk_prepare_enable(qcadev->susclk);
1035 if (err)
1036 return err;
1037
1038 err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1039 if (err)
1040 clk_disable_unprepare(qcadev->susclk);
1041
1042 return err;
1043}
1044
1045static void qca_serdev_remove(struct serdev_device *serdev)
1046{
1047 struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1048
1049 hci_uart_unregister_device(&qcadev->serdev_hu);
1050
1051 clk_disable_unprepare(qcadev->susclk);
1052}
1053
1054static const struct of_device_id qca_bluetooth_of_match[] = {
1055 { .compatible = "qcom,qca6174-bt" },
1056 { /* sentinel */ }
1057};
1058MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
1059
1060static struct serdev_device_driver qca_serdev_driver = {
1061 .probe = qca_serdev_probe,
1062 .remove = qca_serdev_remove,
1063 .driver = {
1064 .name = "hci_uart_qca",
1065 .of_match_table = qca_bluetooth_of_match,
1066 },
1067};
1068
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001069int __init qca_init(void)
1070{
Thierry Escande05ba5332018-03-29 21:15:24 +02001071 serdev_device_driver_register(&qca_serdev_driver);
1072
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001073 return hci_uart_register_proto(&qca_proto);
1074}
1075
1076int __exit qca_deinit(void)
1077{
Thierry Escande05ba5332018-03-29 21:15:24 +02001078 serdev_device_driver_unregister(&qca_serdev_driver);
1079
Ben Young Tae Kim0ff252c2015-08-10 14:24:17 -07001080 return hci_uart_unregister_proto(&qca_proto);
1081}