Commit | Line | Data |
---|---|---|
0ff252c1 BYTK |
1 | /* |
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. | |
fa9ad876 | 8 | * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved. |
0ff252c1 BYTK |
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> | |
05ba533c | 32 | #include <linux/clk.h> |
0ff252c1 | 33 | #include <linux/debugfs.h> |
fa9ad876 BG |
34 | #include <linux/delay.h> |
35 | #include <linux/device.h> | |
05ba533c TE |
36 | #include <linux/gpio/consumer.h> |
37 | #include <linux/mod_devicetable.h> | |
38 | #include <linux/module.h> | |
fa9ad876 BG |
39 | #include <linux/of_device.h> |
40 | #include <linux/platform_device.h> | |
41 | #include <linux/regulator/consumer.h> | |
05ba533c | 42 | #include <linux/serdev.h> |
0ff252c1 BYTK |
43 | |
44 | #include <net/bluetooth/bluetooth.h> | |
45 | #include <net/bluetooth/hci_core.h> | |
46 | ||
47 | #include "hci_uart.h" | |
48 | #include "btqca.h" | |
49 | ||
50 | /* HCI_IBS protocol messages */ | |
51 | #define HCI_IBS_SLEEP_IND 0xFE | |
52 | #define HCI_IBS_WAKE_IND 0xFD | |
53 | #define HCI_IBS_WAKE_ACK 0xFC | |
f81b001a | 54 | #define HCI_MAX_IBS_SIZE 10 |
0ff252c1 BYTK |
55 | |
56 | /* Controller states */ | |
57 | #define STATE_IN_BAND_SLEEP_ENABLED 1 | |
58 | ||
f81b001a MH |
59 | #define IBS_WAKE_RETRANS_TIMEOUT_MS 100 |
60 | #define IBS_TX_IDLE_TIMEOUT_MS 2000 | |
0ff252c1 BYTK |
61 | #define BAUDRATE_SETTLE_TIMEOUT_MS 300 |
62 | ||
05ba533c TE |
63 | /* susclk rate */ |
64 | #define SUSCLK_RATE_32KHZ 32768 | |
65 | ||
0ff252c1 BYTK |
66 | /* HCI_IBS transmit side sleep protocol states */ |
67 | enum tx_ibs_states { | |
68 | HCI_IBS_TX_ASLEEP, | |
69 | HCI_IBS_TX_WAKING, | |
70 | HCI_IBS_TX_AWAKE, | |
71 | }; | |
72 | ||
73 | /* HCI_IBS receive side sleep protocol states */ | |
74 | enum rx_states { | |
75 | HCI_IBS_RX_ASLEEP, | |
76 | HCI_IBS_RX_AWAKE, | |
77 | }; | |
78 | ||
79 | /* HCI_IBS transmit and receive side clock state vote */ | |
80 | enum hci_ibs_clock_state_vote { | |
81 | HCI_IBS_VOTE_STATS_UPDATE, | |
82 | HCI_IBS_TX_VOTE_CLOCK_ON, | |
83 | HCI_IBS_TX_VOTE_CLOCK_OFF, | |
84 | HCI_IBS_RX_VOTE_CLOCK_ON, | |
85 | HCI_IBS_RX_VOTE_CLOCK_OFF, | |
86 | }; | |
87 | ||
88 | struct qca_data { | |
89 | struct hci_uart *hu; | |
90 | struct sk_buff *rx_skb; | |
91 | struct sk_buff_head txq; | |
92 | struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */ | |
93 | spinlock_t hci_ibs_lock; /* HCI_IBS state lock */ | |
94 | u8 tx_ibs_state; /* HCI_IBS transmit side power state*/ | |
95 | u8 rx_ibs_state; /* HCI_IBS receive side power state */ | |
621a5f7a VK |
96 | bool tx_vote; /* Clock must be on for TX */ |
97 | bool rx_vote; /* Clock must be on for RX */ | |
0ff252c1 BYTK |
98 | struct timer_list tx_idle_timer; |
99 | u32 tx_idle_delay; | |
100 | struct timer_list wake_retrans_timer; | |
101 | u32 wake_retrans; | |
102 | struct workqueue_struct *workqueue; | |
103 | struct work_struct ws_awake_rx; | |
104 | struct work_struct ws_awake_device; | |
105 | struct work_struct ws_rx_vote_off; | |
106 | struct work_struct ws_tx_vote_off; | |
107 | unsigned long flags; | |
108 | ||
109 | /* For debugging purpose */ | |
110 | u64 ibs_sent_wacks; | |
111 | u64 ibs_sent_slps; | |
112 | u64 ibs_sent_wakes; | |
113 | u64 ibs_recv_wacks; | |
114 | u64 ibs_recv_slps; | |
115 | u64 ibs_recv_wakes; | |
116 | u64 vote_last_jif; | |
117 | u32 vote_on_ms; | |
118 | u32 vote_off_ms; | |
119 | u64 tx_votes_on; | |
120 | u64 rx_votes_on; | |
121 | u64 tx_votes_off; | |
122 | u64 rx_votes_off; | |
123 | u64 votes_on; | |
124 | u64 votes_off; | |
125 | }; | |
126 | ||
83d9c5e5 BG |
127 | enum qca_speed_type { |
128 | QCA_INIT_SPEED = 1, | |
129 | QCA_OPER_SPEED | |
130 | }; | |
131 | ||
fa9ad876 BG |
132 | /* |
133 | * Voltage regulator information required for configuring the | |
134 | * QCA Bluetooth chipset | |
135 | */ | |
136 | struct qca_vreg { | |
137 | const char *name; | |
138 | unsigned int min_uV; | |
139 | unsigned int max_uV; | |
140 | unsigned int load_uA; | |
141 | }; | |
142 | ||
143 | struct qca_vreg_data { | |
144 | enum qca_btsoc_type soc_type; | |
145 | struct qca_vreg *vregs; | |
146 | size_t num_vregs; | |
147 | }; | |
148 | ||
149 | /* | |
150 | * Platform data for the QCA Bluetooth power driver. | |
151 | */ | |
152 | struct qca_power { | |
153 | struct device *dev; | |
154 | const struct qca_vreg_data *vreg_data; | |
155 | struct regulator_bulk_data *vreg_bulk; | |
156 | bool vregs_on; | |
157 | }; | |
158 | ||
05ba533c TE |
159 | struct qca_serdev { |
160 | struct hci_uart serdev_hu; | |
161 | struct gpio_desc *bt_en; | |
162 | struct clk *susclk; | |
fa9ad876 BG |
163 | enum qca_btsoc_type btsoc_type; |
164 | struct qca_power *bt_power; | |
165 | u32 init_speed; | |
166 | u32 oper_speed; | |
05ba533c TE |
167 | }; |
168 | ||
fa9ad876 | 169 | static int qca_power_setup(struct hci_uart *hu, bool on); |
c2d78273 | 170 | static void qca_power_shutdown(struct hci_uart *hu); |
fa9ad876 | 171 | |
0ff252c1 BYTK |
172 | static void __serial_clock_on(struct tty_struct *tty) |
173 | { | |
174 | /* TODO: Some chipset requires to enable UART clock on client | |
175 | * side to save power consumption or manual work is required. | |
176 | * Please put your code to control UART clock here if needed | |
177 | */ | |
178 | } | |
179 | ||
180 | static void __serial_clock_off(struct tty_struct *tty) | |
181 | { | |
182 | /* TODO: Some chipset requires to disable UART clock on client | |
183 | * side to save power consumption or manual work is required. | |
184 | * Please put your code to control UART clock off here if needed | |
185 | */ | |
186 | } | |
187 | ||
188 | /* serial_clock_vote needs to be called with the ibs lock held */ | |
189 | static void serial_clock_vote(unsigned long vote, struct hci_uart *hu) | |
190 | { | |
191 | struct qca_data *qca = hu->priv; | |
192 | unsigned int diff; | |
193 | ||
194 | bool old_vote = (qca->tx_vote | qca->rx_vote); | |
195 | bool new_vote; | |
196 | ||
197 | switch (vote) { | |
198 | case HCI_IBS_VOTE_STATS_UPDATE: | |
199 | diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); | |
200 | ||
201 | if (old_vote) | |
202 | qca->vote_off_ms += diff; | |
203 | else | |
204 | qca->vote_on_ms += diff; | |
205 | return; | |
206 | ||
207 | case HCI_IBS_TX_VOTE_CLOCK_ON: | |
208 | qca->tx_vote = true; | |
209 | qca->tx_votes_on++; | |
210 | new_vote = true; | |
211 | break; | |
212 | ||
213 | case HCI_IBS_RX_VOTE_CLOCK_ON: | |
214 | qca->rx_vote = true; | |
215 | qca->rx_votes_on++; | |
216 | new_vote = true; | |
217 | break; | |
218 | ||
219 | case HCI_IBS_TX_VOTE_CLOCK_OFF: | |
220 | qca->tx_vote = false; | |
221 | qca->tx_votes_off++; | |
222 | new_vote = qca->rx_vote | qca->tx_vote; | |
223 | break; | |
224 | ||
225 | case HCI_IBS_RX_VOTE_CLOCK_OFF: | |
226 | qca->rx_vote = false; | |
227 | qca->rx_votes_off++; | |
228 | new_vote = qca->rx_vote | qca->tx_vote; | |
229 | break; | |
230 | ||
231 | default: | |
232 | BT_ERR("Voting irregularity"); | |
233 | return; | |
234 | } | |
235 | ||
236 | if (new_vote != old_vote) { | |
237 | if (new_vote) | |
238 | __serial_clock_on(hu->tty); | |
239 | else | |
240 | __serial_clock_off(hu->tty); | |
241 | ||
ce26d813 PK |
242 | BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false", |
243 | vote ? "true" : "false"); | |
0ff252c1 BYTK |
244 | |
245 | diff = jiffies_to_msecs(jiffies - qca->vote_last_jif); | |
246 | ||
247 | if (new_vote) { | |
248 | qca->votes_on++; | |
249 | qca->vote_off_ms += diff; | |
250 | } else { | |
251 | qca->votes_off++; | |
252 | qca->vote_on_ms += diff; | |
253 | } | |
254 | qca->vote_last_jif = jiffies; | |
255 | } | |
256 | } | |
257 | ||
258 | /* Builds and sends an HCI_IBS command packet. | |
259 | * These are very simple packets with only 1 cmd byte. | |
260 | */ | |
261 | static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu) | |
262 | { | |
263 | int err = 0; | |
264 | struct sk_buff *skb = NULL; | |
265 | struct qca_data *qca = hu->priv; | |
266 | ||
267 | BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd); | |
268 | ||
269 | skb = bt_skb_alloc(1, GFP_ATOMIC); | |
270 | if (!skb) { | |
271 | BT_ERR("Failed to allocate memory for HCI_IBS packet"); | |
272 | return -ENOMEM; | |
273 | } | |
274 | ||
275 | /* Assign HCI_IBS type */ | |
634fef61 | 276 | skb_put_u8(skb, cmd); |
0ff252c1 BYTK |
277 | |
278 | skb_queue_tail(&qca->txq, skb); | |
279 | ||
280 | return err; | |
281 | } | |
282 | ||
283 | static void qca_wq_awake_device(struct work_struct *work) | |
284 | { | |
285 | struct qca_data *qca = container_of(work, struct qca_data, | |
286 | ws_awake_device); | |
287 | struct hci_uart *hu = qca->hu; | |
288 | unsigned long retrans_delay; | |
289 | ||
290 | BT_DBG("hu %p wq awake device", hu); | |
291 | ||
292 | /* Vote for serial clock */ | |
293 | serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu); | |
294 | ||
295 | spin_lock(&qca->hci_ibs_lock); | |
296 | ||
297 | /* Send wake indication to device */ | |
298 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) | |
299 | BT_ERR("Failed to send WAKE to device"); | |
300 | ||
301 | qca->ibs_sent_wakes++; | |
302 | ||
303 | /* Start retransmit timer */ | |
304 | retrans_delay = msecs_to_jiffies(qca->wake_retrans); | |
305 | mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); | |
306 | ||
307 | spin_unlock(&qca->hci_ibs_lock); | |
308 | ||
309 | /* Actually send the packets */ | |
310 | hci_uart_tx_wakeup(hu); | |
311 | } | |
312 | ||
313 | static void qca_wq_awake_rx(struct work_struct *work) | |
314 | { | |
315 | struct qca_data *qca = container_of(work, struct qca_data, | |
316 | ws_awake_rx); | |
317 | struct hci_uart *hu = qca->hu; | |
318 | ||
319 | BT_DBG("hu %p wq awake rx", hu); | |
320 | ||
321 | serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu); | |
322 | ||
323 | spin_lock(&qca->hci_ibs_lock); | |
324 | qca->rx_ibs_state = HCI_IBS_RX_AWAKE; | |
325 | ||
326 | /* Always acknowledge device wake up, | |
327 | * sending IBS message doesn't count as TX ON. | |
328 | */ | |
329 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) | |
330 | BT_ERR("Failed to acknowledge device wake up"); | |
331 | ||
332 | qca->ibs_sent_wacks++; | |
333 | ||
334 | spin_unlock(&qca->hci_ibs_lock); | |
335 | ||
336 | /* Actually send the packets */ | |
337 | hci_uart_tx_wakeup(hu); | |
338 | } | |
339 | ||
340 | static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work) | |
341 | { | |
342 | struct qca_data *qca = container_of(work, struct qca_data, | |
343 | ws_rx_vote_off); | |
344 | struct hci_uart *hu = qca->hu; | |
345 | ||
346 | BT_DBG("hu %p rx clock vote off", hu); | |
347 | ||
348 | serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu); | |
349 | } | |
350 | ||
351 | static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work) | |
352 | { | |
353 | struct qca_data *qca = container_of(work, struct qca_data, | |
354 | ws_tx_vote_off); | |
355 | struct hci_uart *hu = qca->hu; | |
356 | ||
357 | BT_DBG("hu %p tx clock vote off", hu); | |
358 | ||
359 | /* Run HCI tx handling unlocked */ | |
360 | hci_uart_tx_wakeup(hu); | |
361 | ||
362 | /* Now that message queued to tty driver, vote for tty clocks off. | |
363 | * It is up to the tty driver to pend the clocks off until tx done. | |
364 | */ | |
365 | serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu); | |
366 | } | |
367 | ||
04356052 | 368 | static void hci_ibs_tx_idle_timeout(struct timer_list *t) |
0ff252c1 | 369 | { |
04356052 KC |
370 | struct qca_data *qca = from_timer(qca, t, tx_idle_timer); |
371 | struct hci_uart *hu = qca->hu; | |
0ff252c1 BYTK |
372 | unsigned long flags; |
373 | ||
374 | BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state); | |
375 | ||
376 | spin_lock_irqsave_nested(&qca->hci_ibs_lock, | |
377 | flags, SINGLE_DEPTH_NESTING); | |
378 | ||
379 | switch (qca->tx_ibs_state) { | |
380 | case HCI_IBS_TX_AWAKE: | |
381 | /* TX_IDLE, go to SLEEP */ | |
382 | if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) { | |
383 | BT_ERR("Failed to send SLEEP to device"); | |
384 | break; | |
385 | } | |
386 | qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; | |
387 | qca->ibs_sent_slps++; | |
388 | queue_work(qca->workqueue, &qca->ws_tx_vote_off); | |
389 | break; | |
390 | ||
391 | case HCI_IBS_TX_ASLEEP: | |
392 | case HCI_IBS_TX_WAKING: | |
393 | /* Fall through */ | |
394 | ||
395 | default: | |
e059a465 | 396 | BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); |
0ff252c1 BYTK |
397 | break; |
398 | } | |
399 | ||
400 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
401 | } | |
402 | ||
04356052 | 403 | static void hci_ibs_wake_retrans_timeout(struct timer_list *t) |
0ff252c1 | 404 | { |
04356052 KC |
405 | struct qca_data *qca = from_timer(qca, t, wake_retrans_timer); |
406 | struct hci_uart *hu = qca->hu; | |
0ff252c1 | 407 | unsigned long flags, retrans_delay; |
a9137188 | 408 | bool retransmit = false; |
0ff252c1 BYTK |
409 | |
410 | BT_DBG("hu %p wake retransmit timeout in %d state", | |
411 | hu, qca->tx_ibs_state); | |
412 | ||
413 | spin_lock_irqsave_nested(&qca->hci_ibs_lock, | |
414 | flags, SINGLE_DEPTH_NESTING); | |
415 | ||
416 | switch (qca->tx_ibs_state) { | |
417 | case HCI_IBS_TX_WAKING: | |
418 | /* No WAKE_ACK, retransmit WAKE */ | |
a9137188 | 419 | retransmit = true; |
0ff252c1 BYTK |
420 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) { |
421 | BT_ERR("Failed to acknowledge device wake up"); | |
422 | break; | |
423 | } | |
424 | qca->ibs_sent_wakes++; | |
425 | retrans_delay = msecs_to_jiffies(qca->wake_retrans); | |
426 | mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay); | |
427 | break; | |
428 | ||
429 | case HCI_IBS_TX_ASLEEP: | |
430 | case HCI_IBS_TX_AWAKE: | |
431 | /* Fall through */ | |
432 | ||
433 | default: | |
e059a465 | 434 | BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); |
0ff252c1 BYTK |
435 | break; |
436 | } | |
437 | ||
438 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
439 | ||
440 | if (retransmit) | |
441 | hci_uart_tx_wakeup(hu); | |
442 | } | |
443 | ||
444 | /* Initialize protocol */ | |
445 | static int qca_open(struct hci_uart *hu) | |
446 | { | |
05ba533c | 447 | struct qca_serdev *qcadev; |
0ff252c1 | 448 | struct qca_data *qca; |
fa9ad876 | 449 | int ret; |
0ff252c1 BYTK |
450 | |
451 | BT_DBG("hu %p qca_open", hu); | |
452 | ||
25a13e38 | 453 | qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL); |
0ff252c1 BYTK |
454 | if (!qca) |
455 | return -ENOMEM; | |
456 | ||
457 | skb_queue_head_init(&qca->txq); | |
458 | skb_queue_head_init(&qca->tx_wait_q); | |
459 | spin_lock_init(&qca->hci_ibs_lock); | |
fac9a602 | 460 | qca->workqueue = alloc_ordered_workqueue("qca_wq", 0); |
0ff252c1 BYTK |
461 | if (!qca->workqueue) { |
462 | BT_ERR("QCA Workqueue not initialized properly"); | |
463 | kfree(qca); | |
464 | return -ENOMEM; | |
465 | } | |
466 | ||
467 | INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx); | |
468 | INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device); | |
469 | INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off); | |
470 | INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off); | |
471 | ||
472 | qca->hu = hu; | |
473 | ||
474 | /* Assume we start with both sides asleep -- extra wakes OK */ | |
475 | qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; | |
476 | qca->rx_ibs_state = HCI_IBS_RX_ASLEEP; | |
477 | ||
478 | /* clocks actually on, but we start votes off */ | |
479 | qca->tx_vote = false; | |
480 | qca->rx_vote = false; | |
481 | qca->flags = 0; | |
482 | ||
483 | qca->ibs_sent_wacks = 0; | |
484 | qca->ibs_sent_slps = 0; | |
485 | qca->ibs_sent_wakes = 0; | |
486 | qca->ibs_recv_wacks = 0; | |
487 | qca->ibs_recv_slps = 0; | |
488 | qca->ibs_recv_wakes = 0; | |
489 | qca->vote_last_jif = jiffies; | |
490 | qca->vote_on_ms = 0; | |
491 | qca->vote_off_ms = 0; | |
492 | qca->votes_on = 0; | |
493 | qca->votes_off = 0; | |
494 | qca->tx_votes_on = 0; | |
495 | qca->tx_votes_off = 0; | |
496 | qca->rx_votes_on = 0; | |
497 | qca->rx_votes_off = 0; | |
498 | ||
499 | hu->priv = qca; | |
500 | ||
05ba533c | 501 | if (hu->serdev) { |
05ba533c TE |
502 | |
503 | qcadev = serdev_device_get_drvdata(hu->serdev); | |
fa9ad876 BG |
504 | if (qcadev->btsoc_type != QCA_WCN3990) { |
505 | gpiod_set_value_cansleep(qcadev->bt_en, 1); | |
506 | } else { | |
507 | hu->init_speed = qcadev->init_speed; | |
508 | hu->oper_speed = qcadev->oper_speed; | |
509 | ret = qca_power_setup(hu, true); | |
510 | if (ret) { | |
511 | destroy_workqueue(qca->workqueue); | |
512 | kfree_skb(qca->rx_skb); | |
513 | hu->priv = NULL; | |
514 | kfree(qca); | |
515 | return ret; | |
516 | } | |
517 | } | |
05ba533c TE |
518 | } |
519 | ||
fa9ad876 BG |
520 | timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0); |
521 | qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS; | |
522 | ||
523 | timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0); | |
524 | qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS; | |
525 | ||
0ff252c1 BYTK |
526 | BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u", |
527 | qca->tx_idle_delay, qca->wake_retrans); | |
528 | ||
529 | return 0; | |
530 | } | |
531 | ||
532 | static void qca_debugfs_init(struct hci_dev *hdev) | |
533 | { | |
534 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
535 | struct qca_data *qca = hu->priv; | |
536 | struct dentry *ibs_dir; | |
537 | umode_t mode; | |
538 | ||
539 | if (!hdev->debugfs) | |
540 | return; | |
541 | ||
542 | ibs_dir = debugfs_create_dir("ibs", hdev->debugfs); | |
543 | ||
544 | /* read only */ | |
545 | mode = S_IRUGO; | |
546 | debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state); | |
547 | debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state); | |
548 | debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir, | |
549 | &qca->ibs_sent_slps); | |
550 | debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir, | |
551 | &qca->ibs_sent_wakes); | |
552 | debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir, | |
553 | &qca->ibs_sent_wacks); | |
554 | debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir, | |
555 | &qca->ibs_recv_slps); | |
556 | debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir, | |
557 | &qca->ibs_recv_wakes); | |
558 | debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir, | |
559 | &qca->ibs_recv_wacks); | |
10be6c0f | 560 | debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote); |
0ff252c1 BYTK |
561 | debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on); |
562 | debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off); | |
10be6c0f | 563 | debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote); |
0ff252c1 BYTK |
564 | debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on); |
565 | debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off); | |
566 | debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on); | |
567 | debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off); | |
568 | debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms); | |
569 | debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms); | |
570 | ||
571 | /* read/write */ | |
572 | mode = S_IRUGO | S_IWUSR; | |
573 | debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans); | |
574 | debugfs_create_u32("tx_idle_delay", mode, ibs_dir, | |
575 | &qca->tx_idle_delay); | |
576 | } | |
577 | ||
578 | /* Flush protocol data */ | |
579 | static int qca_flush(struct hci_uart *hu) | |
580 | { | |
581 | struct qca_data *qca = hu->priv; | |
582 | ||
583 | BT_DBG("hu %p qca flush", hu); | |
584 | ||
585 | skb_queue_purge(&qca->tx_wait_q); | |
586 | skb_queue_purge(&qca->txq); | |
587 | ||
588 | return 0; | |
589 | } | |
590 | ||
591 | /* Close protocol */ | |
592 | static int qca_close(struct hci_uart *hu) | |
593 | { | |
05ba533c | 594 | struct qca_serdev *qcadev; |
0ff252c1 BYTK |
595 | struct qca_data *qca = hu->priv; |
596 | ||
597 | BT_DBG("hu %p qca close", hu); | |
598 | ||
599 | serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu); | |
600 | ||
601 | skb_queue_purge(&qca->tx_wait_q); | |
602 | skb_queue_purge(&qca->txq); | |
603 | del_timer(&qca->tx_idle_timer); | |
604 | del_timer(&qca->wake_retrans_timer); | |
605 | destroy_workqueue(qca->workqueue); | |
606 | qca->hu = NULL; | |
607 | ||
05ba533c | 608 | if (hu->serdev) { |
05ba533c | 609 | qcadev = serdev_device_get_drvdata(hu->serdev); |
fa9ad876 | 610 | if (qcadev->btsoc_type == QCA_WCN3990) |
c2d78273 | 611 | qca_power_shutdown(hu); |
fa9ad876 BG |
612 | else |
613 | gpiod_set_value_cansleep(qcadev->bt_en, 0); | |
614 | ||
05ba533c TE |
615 | } |
616 | ||
0ff252c1 BYTK |
617 | kfree_skb(qca->rx_skb); |
618 | ||
619 | hu->priv = NULL; | |
620 | ||
621 | kfree(qca); | |
622 | ||
623 | return 0; | |
624 | } | |
625 | ||
626 | /* Called upon a wake-up-indication from the device. | |
627 | */ | |
628 | static void device_want_to_wakeup(struct hci_uart *hu) | |
629 | { | |
630 | unsigned long flags; | |
631 | struct qca_data *qca = hu->priv; | |
632 | ||
633 | BT_DBG("hu %p want to wake up", hu); | |
634 | ||
635 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); | |
636 | ||
637 | qca->ibs_recv_wakes++; | |
638 | ||
639 | switch (qca->rx_ibs_state) { | |
640 | case HCI_IBS_RX_ASLEEP: | |
641 | /* Make sure clock is on - we may have turned clock off since | |
642 | * receiving the wake up indicator awake rx clock. | |
643 | */ | |
644 | queue_work(qca->workqueue, &qca->ws_awake_rx); | |
645 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
646 | return; | |
647 | ||
648 | case HCI_IBS_RX_AWAKE: | |
649 | /* Always acknowledge device wake up, | |
650 | * sending IBS message doesn't count as TX ON. | |
651 | */ | |
652 | if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) { | |
653 | BT_ERR("Failed to acknowledge device wake up"); | |
654 | break; | |
655 | } | |
656 | qca->ibs_sent_wacks++; | |
657 | break; | |
658 | ||
659 | default: | |
660 | /* Any other state is illegal */ | |
661 | BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d", | |
662 | qca->rx_ibs_state); | |
663 | break; | |
664 | } | |
665 | ||
666 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
667 | ||
668 | /* Actually send the packets */ | |
669 | hci_uart_tx_wakeup(hu); | |
670 | } | |
671 | ||
672 | /* Called upon a sleep-indication from the device. | |
673 | */ | |
674 | static void device_want_to_sleep(struct hci_uart *hu) | |
675 | { | |
676 | unsigned long flags; | |
677 | struct qca_data *qca = hu->priv; | |
678 | ||
679 | BT_DBG("hu %p want to sleep", hu); | |
680 | ||
681 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); | |
682 | ||
683 | qca->ibs_recv_slps++; | |
684 | ||
685 | switch (qca->rx_ibs_state) { | |
686 | case HCI_IBS_RX_AWAKE: | |
687 | /* Update state */ | |
688 | qca->rx_ibs_state = HCI_IBS_RX_ASLEEP; | |
689 | /* Vote off rx clock under workqueue */ | |
690 | queue_work(qca->workqueue, &qca->ws_rx_vote_off); | |
691 | break; | |
692 | ||
693 | case HCI_IBS_RX_ASLEEP: | |
694 | /* Fall through */ | |
695 | ||
696 | default: | |
697 | /* Any other state is illegal */ | |
698 | BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d", | |
699 | qca->rx_ibs_state); | |
700 | break; | |
701 | } | |
702 | ||
703 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
704 | } | |
705 | ||
706 | /* Called upon wake-up-acknowledgement from the device | |
707 | */ | |
708 | static void device_woke_up(struct hci_uart *hu) | |
709 | { | |
710 | unsigned long flags, idle_delay; | |
711 | struct qca_data *qca = hu->priv; | |
712 | struct sk_buff *skb = NULL; | |
713 | ||
714 | BT_DBG("hu %p woke up", hu); | |
715 | ||
716 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); | |
717 | ||
718 | qca->ibs_recv_wacks++; | |
719 | ||
720 | switch (qca->tx_ibs_state) { | |
721 | case HCI_IBS_TX_AWAKE: | |
722 | /* Expect one if we send 2 WAKEs */ | |
723 | BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d", | |
724 | qca->tx_ibs_state); | |
725 | break; | |
726 | ||
727 | case HCI_IBS_TX_WAKING: | |
728 | /* Send pending packets */ | |
729 | while ((skb = skb_dequeue(&qca->tx_wait_q))) | |
730 | skb_queue_tail(&qca->txq, skb); | |
731 | ||
732 | /* Switch timers and change state to HCI_IBS_TX_AWAKE */ | |
733 | del_timer(&qca->wake_retrans_timer); | |
734 | idle_delay = msecs_to_jiffies(qca->tx_idle_delay); | |
735 | mod_timer(&qca->tx_idle_timer, jiffies + idle_delay); | |
736 | qca->tx_ibs_state = HCI_IBS_TX_AWAKE; | |
737 | break; | |
738 | ||
739 | case HCI_IBS_TX_ASLEEP: | |
740 | /* Fall through */ | |
741 | ||
742 | default: | |
743 | BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d", | |
744 | qca->tx_ibs_state); | |
745 | break; | |
746 | } | |
747 | ||
748 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
749 | ||
750 | /* Actually send the packets */ | |
751 | hci_uart_tx_wakeup(hu); | |
752 | } | |
753 | ||
754 | /* Enqueue frame for transmittion (padding, crc, etc) may be called from | |
755 | * two simultaneous tasklets. | |
756 | */ | |
757 | static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb) | |
758 | { | |
759 | unsigned long flags = 0, idle_delay; | |
760 | struct qca_data *qca = hu->priv; | |
761 | ||
762 | BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb, | |
763 | qca->tx_ibs_state); | |
764 | ||
765 | /* Prepend skb with frame type */ | |
618e8bc2 | 766 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
0ff252c1 BYTK |
767 | |
768 | /* Don't go to sleep in middle of patch download or | |
769 | * Out-Of-Band(GPIOs control) sleep is selected. | |
770 | */ | |
771 | if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags)) { | |
772 | skb_queue_tail(&qca->txq, skb); | |
773 | return 0; | |
774 | } | |
775 | ||
776 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); | |
777 | ||
778 | /* Act according to current state */ | |
779 | switch (qca->tx_ibs_state) { | |
780 | case HCI_IBS_TX_AWAKE: | |
781 | BT_DBG("Device awake, sending normally"); | |
782 | skb_queue_tail(&qca->txq, skb); | |
783 | idle_delay = msecs_to_jiffies(qca->tx_idle_delay); | |
784 | mod_timer(&qca->tx_idle_timer, jiffies + idle_delay); | |
785 | break; | |
786 | ||
787 | case HCI_IBS_TX_ASLEEP: | |
788 | BT_DBG("Device asleep, waking up and queueing packet"); | |
789 | /* Save packet for later */ | |
790 | skb_queue_tail(&qca->tx_wait_q, skb); | |
791 | ||
792 | qca->tx_ibs_state = HCI_IBS_TX_WAKING; | |
793 | /* Schedule a work queue to wake up device */ | |
794 | queue_work(qca->workqueue, &qca->ws_awake_device); | |
795 | break; | |
796 | ||
797 | case HCI_IBS_TX_WAKING: | |
798 | BT_DBG("Device waking up, queueing packet"); | |
799 | /* Transient state; just keep packet for later */ | |
800 | skb_queue_tail(&qca->tx_wait_q, skb); | |
801 | break; | |
802 | ||
803 | default: | |
804 | BT_ERR("Illegal tx state: %d (losing packet)", | |
805 | qca->tx_ibs_state); | |
806 | kfree_skb(skb); | |
807 | break; | |
808 | } | |
809 | ||
810 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
811 | ||
812 | return 0; | |
813 | } | |
814 | ||
815 | static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb) | |
816 | { | |
817 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
818 | ||
819 | BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND); | |
820 | ||
821 | device_want_to_sleep(hu); | |
822 | ||
823 | kfree_skb(skb); | |
824 | return 0; | |
825 | } | |
826 | ||
827 | static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb) | |
828 | { | |
829 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
830 | ||
831 | BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND); | |
832 | ||
833 | device_want_to_wakeup(hu); | |
834 | ||
835 | kfree_skb(skb); | |
836 | return 0; | |
837 | } | |
838 | ||
839 | static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb) | |
840 | { | |
841 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
842 | ||
843 | BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK); | |
844 | ||
845 | device_woke_up(hu); | |
846 | ||
847 | kfree_skb(skb); | |
848 | return 0; | |
849 | } | |
850 | ||
851 | #define QCA_IBS_SLEEP_IND_EVENT \ | |
852 | .type = HCI_IBS_SLEEP_IND, \ | |
853 | .hlen = 0, \ | |
854 | .loff = 0, \ | |
855 | .lsize = 0, \ | |
856 | .maxlen = HCI_MAX_IBS_SIZE | |
857 | ||
858 | #define QCA_IBS_WAKE_IND_EVENT \ | |
859 | .type = HCI_IBS_WAKE_IND, \ | |
860 | .hlen = 0, \ | |
861 | .loff = 0, \ | |
862 | .lsize = 0, \ | |
863 | .maxlen = HCI_MAX_IBS_SIZE | |
864 | ||
865 | #define QCA_IBS_WAKE_ACK_EVENT \ | |
866 | .type = HCI_IBS_WAKE_ACK, \ | |
867 | .hlen = 0, \ | |
868 | .loff = 0, \ | |
869 | .lsize = 0, \ | |
870 | .maxlen = HCI_MAX_IBS_SIZE | |
871 | ||
872 | static const struct h4_recv_pkt qca_recv_pkts[] = { | |
873 | { H4_RECV_ACL, .recv = hci_recv_frame }, | |
874 | { H4_RECV_SCO, .recv = hci_recv_frame }, | |
875 | { H4_RECV_EVENT, .recv = hci_recv_frame }, | |
876 | { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind }, | |
877 | { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack }, | |
878 | { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind }, | |
879 | }; | |
880 | ||
881 | static int qca_recv(struct hci_uart *hu, const void *data, int count) | |
882 | { | |
883 | struct qca_data *qca = hu->priv; | |
884 | ||
885 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) | |
886 | return -EUNATCH; | |
887 | ||
888 | qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count, | |
889 | qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts)); | |
890 | if (IS_ERR(qca->rx_skb)) { | |
891 | int err = PTR_ERR(qca->rx_skb); | |
2064ee33 | 892 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
0ff252c1 BYTK |
893 | qca->rx_skb = NULL; |
894 | return err; | |
895 | } | |
896 | ||
897 | return count; | |
898 | } | |
899 | ||
900 | static struct sk_buff *qca_dequeue(struct hci_uart *hu) | |
901 | { | |
902 | struct qca_data *qca = hu->priv; | |
903 | ||
904 | return skb_dequeue(&qca->txq); | |
905 | } | |
906 | ||
907 | static uint8_t qca_get_baudrate_value(int speed) | |
908 | { | |
ce26d813 | 909 | switch (speed) { |
0ff252c1 BYTK |
910 | case 9600: |
911 | return QCA_BAUDRATE_9600; | |
912 | case 19200: | |
913 | return QCA_BAUDRATE_19200; | |
914 | case 38400: | |
915 | return QCA_BAUDRATE_38400; | |
916 | case 57600: | |
917 | return QCA_BAUDRATE_57600; | |
918 | case 115200: | |
919 | return QCA_BAUDRATE_115200; | |
920 | case 230400: | |
921 | return QCA_BAUDRATE_230400; | |
922 | case 460800: | |
923 | return QCA_BAUDRATE_460800; | |
924 | case 500000: | |
925 | return QCA_BAUDRATE_500000; | |
926 | case 921600: | |
927 | return QCA_BAUDRATE_921600; | |
928 | case 1000000: | |
929 | return QCA_BAUDRATE_1000000; | |
930 | case 2000000: | |
931 | return QCA_BAUDRATE_2000000; | |
932 | case 3000000: | |
933 | return QCA_BAUDRATE_3000000; | |
be93a497 BG |
934 | case 3200000: |
935 | return QCA_BAUDRATE_3200000; | |
0ff252c1 BYTK |
936 | case 3500000: |
937 | return QCA_BAUDRATE_3500000; | |
938 | default: | |
939 | return QCA_BAUDRATE_115200; | |
940 | } | |
941 | } | |
942 | ||
943 | static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate) | |
944 | { | |
945 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
946 | struct qca_data *qca = hu->priv; | |
947 | struct sk_buff *skb; | |
fa9ad876 | 948 | struct qca_serdev *qcadev; |
0ff252c1 BYTK |
949 | u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 }; |
950 | ||
be93a497 | 951 | if (baudrate > QCA_BAUDRATE_3200000) |
0ff252c1 BYTK |
952 | return -EINVAL; |
953 | ||
954 | cmd[4] = baudrate; | |
955 | ||
25a13e38 | 956 | skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); |
0ff252c1 | 957 | if (!skb) { |
2064ee33 | 958 | bt_dev_err(hdev, "Failed to allocate baudrate packet"); |
0ff252c1 BYTK |
959 | return -ENOMEM; |
960 | } | |
961 | ||
fa9ad876 BG |
962 | /* Disabling hardware flow control is mandatory while |
963 | * sending change baudrate request to wcn3990 SoC. | |
964 | */ | |
965 | qcadev = serdev_device_get_drvdata(hu->serdev); | |
966 | if (qcadev->btsoc_type == QCA_WCN3990) | |
967 | hci_uart_set_flow_control(hu, true); | |
968 | ||
0ff252c1 | 969 | /* Assign commands to change baudrate and packet type. */ |
59ae1d12 | 970 | skb_put_data(skb, cmd, sizeof(cmd)); |
618e8bc2 | 971 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; |
0ff252c1 BYTK |
972 | |
973 | skb_queue_tail(&qca->txq, skb); | |
974 | hci_uart_tx_wakeup(hu); | |
975 | ||
976 | /* wait 300ms to change new baudrate on controller side | |
977 | * controller will come back after they receive this HCI command | |
978 | * then host can communicate with new baudrate to controller | |
979 | */ | |
980 | set_current_state(TASK_UNINTERRUPTIBLE); | |
981 | schedule_timeout(msecs_to_jiffies(BAUDRATE_SETTLE_TIMEOUT_MS)); | |
9960521c | 982 | set_current_state(TASK_RUNNING); |
0ff252c1 | 983 | |
fa9ad876 BG |
984 | if (qcadev->btsoc_type == QCA_WCN3990) |
985 | hci_uart_set_flow_control(hu, false); | |
986 | ||
0ff252c1 BYTK |
987 | return 0; |
988 | } | |
989 | ||
05ba533c TE |
990 | static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed) |
991 | { | |
992 | if (hu->serdev) | |
993 | serdev_device_set_baudrate(hu->serdev, speed); | |
994 | else | |
995 | hci_uart_set_baudrate(hu, speed); | |
996 | } | |
997 | ||
fa9ad876 BG |
998 | static int qca_send_power_pulse(struct hci_dev *hdev, u8 cmd) |
999 | { | |
1000 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
1001 | struct qca_data *qca = hu->priv; | |
1002 | struct sk_buff *skb; | |
1003 | ||
1004 | /* These power pulses are single byte command which are sent | |
1005 | * at required baudrate to wcn3990. On wcn3990, we have an external | |
1006 | * circuit at Tx pin which decodes the pulse sent at specific baudrate. | |
1007 | * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT | |
1008 | * and also we use the same power inputs to turn on and off for | |
1009 | * Wi-Fi/BT. Powering up the power sources will not enable BT, until | |
1010 | * we send a power on pulse at 115200 bps. This algorithm will help to | |
1011 | * save power. Disabling hardware flow control is mandatory while | |
1012 | * sending power pulses to SoC. | |
1013 | */ | |
1014 | bt_dev_dbg(hdev, "sending power pulse %02x to SoC", cmd); | |
1015 | ||
1016 | skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); | |
1017 | if (!skb) | |
1018 | return -ENOMEM; | |
1019 | ||
1020 | hci_uart_set_flow_control(hu, true); | |
1021 | ||
1022 | skb_put_u8(skb, cmd); | |
1023 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; | |
1024 | ||
1025 | skb_queue_tail(&qca->txq, skb); | |
1026 | hci_uart_tx_wakeup(hu); | |
1027 | ||
1028 | /* Wait for 100 uS for SoC to settle down */ | |
1029 | usleep_range(100, 200); | |
1030 | hci_uart_set_flow_control(hu, false); | |
1031 | ||
1032 | return 0; | |
1033 | } | |
1034 | ||
83d9c5e5 BG |
1035 | static unsigned int qca_get_speed(struct hci_uart *hu, |
1036 | enum qca_speed_type speed_type) | |
1037 | { | |
1038 | unsigned int speed = 0; | |
1039 | ||
1040 | if (speed_type == QCA_INIT_SPEED) { | |
1041 | if (hu->init_speed) | |
1042 | speed = hu->init_speed; | |
1043 | else if (hu->proto->init_speed) | |
1044 | speed = hu->proto->init_speed; | |
1045 | } else { | |
1046 | if (hu->oper_speed) | |
1047 | speed = hu->oper_speed; | |
1048 | else if (hu->proto->oper_speed) | |
1049 | speed = hu->proto->oper_speed; | |
1050 | } | |
1051 | ||
1052 | return speed; | |
1053 | } | |
1054 | ||
1055 | static int qca_check_speeds(struct hci_uart *hu) | |
1056 | { | |
fa9ad876 BG |
1057 | struct qca_serdev *qcadev; |
1058 | ||
1059 | qcadev = serdev_device_get_drvdata(hu->serdev); | |
1060 | if (qcadev->btsoc_type == QCA_WCN3990) { | |
1061 | if (!qca_get_speed(hu, QCA_INIT_SPEED) && | |
1062 | !qca_get_speed(hu, QCA_OPER_SPEED)) | |
1063 | return -EINVAL; | |
1064 | } else { | |
1065 | if (!qca_get_speed(hu, QCA_INIT_SPEED) || | |
1066 | !qca_get_speed(hu, QCA_OPER_SPEED)) | |
1067 | return -EINVAL; | |
1068 | } | |
83d9c5e5 BG |
1069 | |
1070 | return 0; | |
1071 | } | |
1072 | ||
1073 | static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type) | |
1074 | { | |
1075 | unsigned int speed, qca_baudrate; | |
1076 | int ret; | |
1077 | ||
1078 | if (speed_type == QCA_INIT_SPEED) { | |
1079 | speed = qca_get_speed(hu, QCA_INIT_SPEED); | |
1080 | if (speed) | |
1081 | host_set_baudrate(hu, speed); | |
1082 | } else { | |
1083 | speed = qca_get_speed(hu, QCA_OPER_SPEED); | |
1084 | if (!speed) | |
1085 | return 0; | |
1086 | ||
1087 | qca_baudrate = qca_get_baudrate_value(speed); | |
fa9ad876 | 1088 | bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed); |
83d9c5e5 BG |
1089 | ret = qca_set_baudrate(hu->hdev, qca_baudrate); |
1090 | if (ret) | |
1091 | return ret; | |
1092 | ||
1093 | host_set_baudrate(hu, speed); | |
1094 | } | |
1095 | ||
1096 | return 0; | |
1097 | } | |
1098 | ||
fa9ad876 BG |
1099 | static int qca_wcn3990_init(struct hci_uart *hu) |
1100 | { | |
1101 | struct hci_dev *hdev = hu->hdev; | |
1102 | int ret; | |
1103 | ||
1104 | /* Forcefully enable wcn3990 to enter in to boot mode. */ | |
1105 | host_set_baudrate(hu, 2400); | |
1106 | ret = qca_send_power_pulse(hdev, QCA_WCN3990_POWEROFF_PULSE); | |
1107 | if (ret) | |
1108 | return ret; | |
1109 | ||
1110 | qca_set_speed(hu, QCA_INIT_SPEED); | |
1111 | ret = qca_send_power_pulse(hdev, QCA_WCN3990_POWERON_PULSE); | |
1112 | if (ret) | |
1113 | return ret; | |
1114 | ||
1115 | /* Wait for 100 ms for SoC to boot */ | |
1116 | msleep(100); | |
1117 | ||
1118 | /* Now the device is in ready state to communicate with host. | |
1119 | * To sync host with device we need to reopen port. | |
1120 | * Without this, we will have RTS and CTS synchronization | |
1121 | * issues. | |
1122 | */ | |
1123 | serdev_device_close(hu->serdev); | |
1124 | ret = serdev_device_open(hu->serdev); | |
1125 | if (ret) { | |
1126 | bt_dev_err(hu->hdev, "failed to open port"); | |
1127 | return ret; | |
1128 | } | |
1129 | ||
1130 | hci_uart_set_flow_control(hu, false); | |
1131 | ||
1132 | return 0; | |
1133 | } | |
1134 | ||
0ff252c1 BYTK |
1135 | static int qca_setup(struct hci_uart *hu) |
1136 | { | |
1137 | struct hci_dev *hdev = hu->hdev; | |
1138 | struct qca_data *qca = hu->priv; | |
1139 | unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200; | |
fa9ad876 | 1140 | struct qca_serdev *qcadev; |
0ff252c1 | 1141 | int ret; |
aadebac4 | 1142 | int soc_ver = 0; |
0ff252c1 | 1143 | |
fa9ad876 | 1144 | qcadev = serdev_device_get_drvdata(hu->serdev); |
0ff252c1 | 1145 | |
83d9c5e5 BG |
1146 | ret = qca_check_speeds(hu); |
1147 | if (ret) | |
1148 | return ret; | |
1149 | ||
0ff252c1 BYTK |
1150 | /* Patch downloading has to be done without IBS mode */ |
1151 | clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags); | |
1152 | ||
fa9ad876 BG |
1153 | if (qcadev->btsoc_type == QCA_WCN3990) { |
1154 | bt_dev_info(hdev, "setting up wcn3990"); | |
1155 | ret = qca_wcn3990_init(hu); | |
1156 | if (ret) | |
1157 | return ret; | |
1158 | ||
1159 | ret = qca_read_soc_version(hdev, &soc_ver); | |
1160 | if (ret) | |
1161 | return ret; | |
1162 | } else { | |
1163 | bt_dev_info(hdev, "ROME setup"); | |
1164 | qca_set_speed(hu, QCA_INIT_SPEED); | |
1165 | } | |
0ff252c1 BYTK |
1166 | |
1167 | /* Setup user speed if needed */ | |
83d9c5e5 | 1168 | speed = qca_get_speed(hu, QCA_OPER_SPEED); |
0ff252c1 | 1169 | if (speed) { |
83d9c5e5 BG |
1170 | ret = qca_set_speed(hu, QCA_OPER_SPEED); |
1171 | if (ret) | |
0ff252c1 | 1172 | return ret; |
83d9c5e5 BG |
1173 | |
1174 | qca_baudrate = qca_get_baudrate_value(speed); | |
0ff252c1 BYTK |
1175 | } |
1176 | ||
fa9ad876 BG |
1177 | if (qcadev->btsoc_type != QCA_WCN3990) { |
1178 | /* Get QCA version information */ | |
1179 | ret = qca_read_soc_version(hdev, &soc_ver); | |
1180 | if (ret) | |
1181 | return ret; | |
1182 | } | |
aadebac4 BG |
1183 | |
1184 | bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver); | |
0ff252c1 | 1185 | /* Setup patch / NVM configurations */ |
fa9ad876 | 1186 | ret = qca_uart_setup(hdev, qca_baudrate, qcadev->btsoc_type, soc_ver); |
0ff252c1 BYTK |
1187 | if (!ret) { |
1188 | set_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags); | |
1189 | qca_debugfs_init(hdev); | |
ba8f3597 LP |
1190 | } else if (ret == -ENOENT) { |
1191 | /* No patch/nvm-config found, run with original fw/config */ | |
1192 | ret = 0; | |
7dc5fe08 AP |
1193 | } else if (ret == -EAGAIN) { |
1194 | /* | |
1195 | * Userspace firmware loader will return -EAGAIN in case no | |
1196 | * patch/nvm-config is found, so run with original fw/config. | |
1197 | */ | |
1198 | ret = 0; | |
0ff252c1 BYTK |
1199 | } |
1200 | ||
1201 | /* Setup bdaddr */ | |
1202 | hu->hdev->set_bdaddr = qca_set_bdaddr_rome; | |
1203 | ||
1204 | return ret; | |
1205 | } | |
1206 | ||
1207 | static struct hci_uart_proto qca_proto = { | |
1208 | .id = HCI_UART_QCA, | |
1209 | .name = "QCA", | |
aee61f7a | 1210 | .manufacturer = 29, |
0ff252c1 BYTK |
1211 | .init_speed = 115200, |
1212 | .oper_speed = 3000000, | |
1213 | .open = qca_open, | |
1214 | .close = qca_close, | |
1215 | .flush = qca_flush, | |
1216 | .setup = qca_setup, | |
1217 | .recv = qca_recv, | |
1218 | .enqueue = qca_enqueue, | |
1219 | .dequeue = qca_dequeue, | |
1220 | }; | |
1221 | ||
fa9ad876 BG |
1222 | static const struct qca_vreg_data qca_soc_data = { |
1223 | .soc_type = QCA_WCN3990, | |
1224 | .vregs = (struct qca_vreg []) { | |
1225 | { "vddio", 1800000, 1900000, 15000 }, | |
1226 | { "vddxo", 1800000, 1900000, 80000 }, | |
1227 | { "vddrf", 1300000, 1350000, 300000 }, | |
1228 | { "vddch0", 3300000, 3400000, 450000 }, | |
1229 | }, | |
1230 | .num_vregs = 4, | |
1231 | }; | |
1232 | ||
c2d78273 | 1233 | static void qca_power_shutdown(struct hci_uart *hu) |
fa9ad876 | 1234 | { |
c2d78273 BG |
1235 | struct serdev_device *serdev = hu->serdev; |
1236 | unsigned char cmd = QCA_WCN3990_POWEROFF_PULSE; | |
fa9ad876 BG |
1237 | |
1238 | host_set_baudrate(hu, 2400); | |
c2d78273 BG |
1239 | hci_uart_set_flow_control(hu, true); |
1240 | serdev_device_write_buf(serdev, &cmd, sizeof(cmd)); | |
1241 | hci_uart_set_flow_control(hu, false); | |
fa9ad876 BG |
1242 | qca_power_setup(hu, false); |
1243 | } | |
1244 | ||
1245 | static int qca_enable_regulator(struct qca_vreg vregs, | |
1246 | struct regulator *regulator) | |
1247 | { | |
1248 | int ret; | |
1249 | ||
1250 | ret = regulator_set_voltage(regulator, vregs.min_uV, | |
1251 | vregs.max_uV); | |
1252 | if (ret) | |
1253 | return ret; | |
1254 | ||
1255 | if (vregs.load_uA) | |
1256 | ret = regulator_set_load(regulator, | |
1257 | vregs.load_uA); | |
1258 | ||
1259 | if (ret) | |
1260 | return ret; | |
1261 | ||
1262 | return regulator_enable(regulator); | |
1263 | ||
1264 | } | |
1265 | ||
1266 | static void qca_disable_regulator(struct qca_vreg vregs, | |
1267 | struct regulator *regulator) | |
1268 | { | |
1269 | regulator_disable(regulator); | |
1270 | regulator_set_voltage(regulator, 0, vregs.max_uV); | |
1271 | if (vregs.load_uA) | |
1272 | regulator_set_load(regulator, 0); | |
1273 | ||
1274 | } | |
1275 | ||
1276 | static int qca_power_setup(struct hci_uart *hu, bool on) | |
1277 | { | |
1278 | struct qca_vreg *vregs; | |
1279 | struct regulator_bulk_data *vreg_bulk; | |
1280 | struct qca_serdev *qcadev; | |
1281 | int i, num_vregs, ret = 0; | |
1282 | ||
1283 | qcadev = serdev_device_get_drvdata(hu->serdev); | |
1284 | if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data || | |
1285 | !qcadev->bt_power->vreg_bulk) | |
1286 | return -EINVAL; | |
1287 | ||
1288 | vregs = qcadev->bt_power->vreg_data->vregs; | |
1289 | vreg_bulk = qcadev->bt_power->vreg_bulk; | |
1290 | num_vregs = qcadev->bt_power->vreg_data->num_vregs; | |
1291 | BT_DBG("on: %d", on); | |
1292 | if (on && !qcadev->bt_power->vregs_on) { | |
1293 | for (i = 0; i < num_vregs; i++) { | |
1294 | ret = qca_enable_regulator(vregs[i], | |
1295 | vreg_bulk[i].consumer); | |
1296 | if (ret) | |
1297 | break; | |
1298 | } | |
1299 | ||
1300 | if (ret) { | |
1301 | BT_ERR("failed to enable regulator:%s", vregs[i].name); | |
1302 | /* turn off regulators which are enabled */ | |
1303 | for (i = i - 1; i >= 0; i--) | |
1304 | qca_disable_regulator(vregs[i], | |
1305 | vreg_bulk[i].consumer); | |
1306 | } else { | |
1307 | qcadev->bt_power->vregs_on = true; | |
1308 | } | |
1309 | } else if (!on && qcadev->bt_power->vregs_on) { | |
1310 | /* turn off regulator in reverse order */ | |
1311 | i = qcadev->bt_power->vreg_data->num_vregs - 1; | |
1312 | for ( ; i >= 0; i--) | |
1313 | qca_disable_regulator(vregs[i], vreg_bulk[i].consumer); | |
1314 | ||
1315 | qcadev->bt_power->vregs_on = false; | |
1316 | } | |
1317 | ||
1318 | return ret; | |
1319 | } | |
1320 | ||
1321 | static int qca_init_regulators(struct qca_power *qca, | |
1322 | const struct qca_vreg *vregs, size_t num_vregs) | |
1323 | { | |
1324 | int i; | |
1325 | ||
1326 | qca->vreg_bulk = devm_kzalloc(qca->dev, num_vregs * | |
1327 | sizeof(struct regulator_bulk_data), | |
1328 | GFP_KERNEL); | |
1329 | if (!qca->vreg_bulk) | |
1330 | return -ENOMEM; | |
1331 | ||
1332 | for (i = 0; i < num_vregs; i++) | |
1333 | qca->vreg_bulk[i].supply = vregs[i].name; | |
1334 | ||
1335 | return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk); | |
1336 | } | |
1337 | ||
05ba533c TE |
1338 | static int qca_serdev_probe(struct serdev_device *serdev) |
1339 | { | |
1340 | struct qca_serdev *qcadev; | |
fa9ad876 | 1341 | const struct qca_vreg_data *data; |
05ba533c TE |
1342 | int err; |
1343 | ||
1344 | qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL); | |
1345 | if (!qcadev) | |
1346 | return -ENOMEM; | |
1347 | ||
1348 | qcadev->serdev_hu.serdev = serdev; | |
fa9ad876 | 1349 | data = of_device_get_match_data(&serdev->dev); |
05ba533c | 1350 | serdev_device_set_drvdata(serdev, qcadev); |
fa9ad876 BG |
1351 | if (data && data->soc_type == QCA_WCN3990) { |
1352 | qcadev->btsoc_type = QCA_WCN3990; | |
1353 | qcadev->bt_power = devm_kzalloc(&serdev->dev, | |
1354 | sizeof(struct qca_power), | |
1355 | GFP_KERNEL); | |
1356 | if (!qcadev->bt_power) | |
1357 | return -ENOMEM; | |
1358 | ||
1359 | qcadev->bt_power->dev = &serdev->dev; | |
1360 | qcadev->bt_power->vreg_data = data; | |
1361 | err = qca_init_regulators(qcadev->bt_power, data->vregs, | |
1362 | data->num_vregs); | |
1363 | if (err) { | |
1364 | BT_ERR("Failed to init regulators:%d", err); | |
1365 | goto out; | |
1366 | } | |
05ba533c | 1367 | |
fa9ad876 | 1368 | qcadev->bt_power->vregs_on = false; |
05ba533c | 1369 | |
fa9ad876 BG |
1370 | device_property_read_u32(&serdev->dev, "max-speed", |
1371 | &qcadev->oper_speed); | |
1372 | if (!qcadev->oper_speed) | |
1373 | BT_DBG("UART will pick default operating speed"); | |
05ba533c | 1374 | |
fa9ad876 BG |
1375 | err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); |
1376 | if (err) { | |
1377 | BT_ERR("wcn3990 serdev registration failed"); | |
1378 | goto out; | |
1379 | } | |
1380 | } else { | |
1381 | qcadev->btsoc_type = QCA_ROME; | |
1382 | qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable", | |
1383 | GPIOD_OUT_LOW); | |
1384 | if (IS_ERR(qcadev->bt_en)) { | |
1385 | dev_err(&serdev->dev, "failed to acquire enable gpio\n"); | |
1386 | return PTR_ERR(qcadev->bt_en); | |
1387 | } | |
05ba533c | 1388 | |
fa9ad876 BG |
1389 | qcadev->susclk = devm_clk_get(&serdev->dev, NULL); |
1390 | if (IS_ERR(qcadev->susclk)) { | |
1391 | dev_err(&serdev->dev, "failed to acquire clk\n"); | |
1392 | return PTR_ERR(qcadev->susclk); | |
1393 | } | |
05ba533c | 1394 | |
fa9ad876 BG |
1395 | err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ); |
1396 | if (err) | |
1397 | return err; | |
1398 | ||
1399 | err = clk_prepare_enable(qcadev->susclk); | |
1400 | if (err) | |
1401 | return err; | |
1402 | ||
1403 | err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); | |
1404 | if (err) | |
1405 | clk_disable_unprepare(qcadev->susclk); | |
1406 | } | |
1407 | ||
1408 | out: return err; | |
05ba533c | 1409 | |
05ba533c TE |
1410 | } |
1411 | ||
1412 | static void qca_serdev_remove(struct serdev_device *serdev) | |
1413 | { | |
1414 | struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev); | |
1415 | ||
fa9ad876 | 1416 | if (qcadev->btsoc_type == QCA_WCN3990) |
c2d78273 | 1417 | qca_power_shutdown(&qcadev->serdev_hu); |
fa9ad876 BG |
1418 | else |
1419 | clk_disable_unprepare(qcadev->susclk); | |
05ba533c | 1420 | |
fa9ad876 | 1421 | hci_uart_unregister_device(&qcadev->serdev_hu); |
05ba533c TE |
1422 | } |
1423 | ||
1424 | static const struct of_device_id qca_bluetooth_of_match[] = { | |
1425 | { .compatible = "qcom,qca6174-bt" }, | |
fa9ad876 | 1426 | { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data}, |
05ba533c TE |
1427 | { /* sentinel */ } |
1428 | }; | |
1429 | MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match); | |
1430 | ||
1431 | static struct serdev_device_driver qca_serdev_driver = { | |
1432 | .probe = qca_serdev_probe, | |
1433 | .remove = qca_serdev_remove, | |
1434 | .driver = { | |
1435 | .name = "hci_uart_qca", | |
1436 | .of_match_table = qca_bluetooth_of_match, | |
1437 | }, | |
1438 | }; | |
1439 | ||
0ff252c1 BYTK |
1440 | int __init qca_init(void) |
1441 | { | |
05ba533c TE |
1442 | serdev_device_driver_register(&qca_serdev_driver); |
1443 | ||
0ff252c1 BYTK |
1444 | return hci_uart_register_proto(&qca_proto); |
1445 | } | |
1446 | ||
1447 | int __exit qca_deinit(void) | |
1448 | { | |
05ba533c TE |
1449 | serdev_device_driver_unregister(&qca_serdev_driver); |
1450 | ||
0ff252c1 BYTK |
1451 | return hci_uart_unregister_proto(&qca_proto); |
1452 | } |