]>
Commit | Line | Data |
---|---|---|
45051539 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
0ff252c1 BYTK |
2 | /* |
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. | |
fa9ad876 | 9 | * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved. |
0ff252c1 BYTK |
10 | * |
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. | |
0ff252c1 BYTK |
16 | */ |
17 | ||
18 | #include <linux/kernel.h> | |
05ba533c | 19 | #include <linux/clk.h> |
2faa3f15 | 20 | #include <linux/completion.h> |
0ff252c1 | 21 | #include <linux/debugfs.h> |
fa9ad876 BG |
22 | #include <linux/delay.h> |
23 | #include <linux/device.h> | |
05ba533c TE |
24 | #include <linux/gpio/consumer.h> |
25 | #include <linux/mod_devicetable.h> | |
26 | #include <linux/module.h> | |
fa9ad876 BG |
27 | #include <linux/of_device.h> |
28 | #include <linux/platform_device.h> | |
29 | #include <linux/regulator/consumer.h> | |
05ba533c | 30 | #include <linux/serdev.h> |
c614ca3f | 31 | #include <asm/unaligned.h> |
0ff252c1 BYTK |
32 | |
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 | |
f81b001a | 43 | #define HCI_MAX_IBS_SIZE 10 |
0ff252c1 | 44 | |
f81b001a | 45 | #define IBS_WAKE_RETRANS_TIMEOUT_MS 100 |
41d5b25f CC |
46 | #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS 40 |
47 | #define IBS_HOST_TX_IDLE_TIMEOUT_MS 2000 | |
94d66714 | 48 | #define CMD_TRANS_TIMEOUT_MS 100 |
0ff252c1 | 49 | |
05ba533c TE |
50 | /* susclk rate */ |
51 | #define SUSCLK_RATE_32KHZ 32768 | |
52 | ||
c614ca3f BG |
53 | /* Controller debug log header */ |
54 | #define QCA_DEBUG_HANDLE 0x2EDC | |
55 | ||
62a91990 MK |
56 | enum qca_flags { |
57 | QCA_IBS_ENABLED, | |
2faa3f15 | 58 | QCA_DROP_VENDOR_EVENT, |
41d5b25f | 59 | QCA_SUSPENDING, |
62a91990 MK |
60 | }; |
61 | ||
0ff252c1 BYTK |
62 | /* HCI_IBS transmit side sleep protocol states */ |
63 | enum 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 */ | |
70 | enum rx_states { | |
71 | HCI_IBS_RX_ASLEEP, | |
72 | HCI_IBS_RX_AWAKE, | |
73 | }; | |
74 | ||
75 | /* HCI_IBS transmit and receive side clock state vote */ | |
76 | enum 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 | ||
84 | struct 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 */ | |
621a5f7a VK |
92 | bool tx_vote; /* Clock must be on for TX */ |
93 | bool rx_vote; /* Clock must be on for RX */ | |
0ff252c1 BYTK |
94 | 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; | |
2faa3f15 | 104 | struct completion drop_ev_comp; |
41d5b25f | 105 | wait_queue_head_t suspend_wait_q; |
0ff252c1 BYTK |
106 | |
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 | ||
83d9c5e5 BG |
125 | enum qca_speed_type { |
126 | QCA_INIT_SPEED = 1, | |
127 | QCA_OPER_SPEED | |
128 | }; | |
129 | ||
fa9ad876 BG |
130 | /* |
131 | * Voltage regulator information required for configuring the | |
132 | * QCA Bluetooth chipset | |
133 | */ | |
134 | struct qca_vreg { | |
135 | const char *name; | |
fa9ad876 BG |
136 | unsigned int load_uA; |
137 | }; | |
138 | ||
139 | struct 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 | */ | |
148 | struct qca_power { | |
149 | struct device *dev; | |
fa9ad876 | 150 | struct regulator_bulk_data *vreg_bulk; |
163d42fa | 151 | int num_vregs; |
fa9ad876 BG |
152 | bool vregs_on; |
153 | }; | |
154 | ||
05ba533c TE |
155 | struct qca_serdev { |
156 | struct hci_uart serdev_hu; | |
157 | struct gpio_desc *bt_en; | |
158 | struct clk *susclk; | |
fa9ad876 BG |
159 | enum qca_btsoc_type btsoc_type; |
160 | struct qca_power *bt_power; | |
161 | u32 init_speed; | |
162 | u32 oper_speed; | |
99c905c6 | 163 | const char *firmware_name; |
05ba533c TE |
164 | }; |
165 | ||
a9314e76 BA |
166 | static int qca_regulator_enable(struct qca_serdev *qcadev); |
167 | static void qca_regulator_disable(struct qca_serdev *qcadev); | |
c2d78273 | 168 | static void qca_power_shutdown(struct hci_uart *hu); |
3e4be65e | 169 | static int qca_power_off(struct hci_dev *hdev); |
fa9ad876 | 170 | |
4fdd5a4f MK |
171 | static 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 | ||
99c905c6 RL |
186 | static 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 | ||
0ff252c1 BYTK |
197 | static 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 | ||
205 | static 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 */ | |
214 | static 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 | ||
ce26d813 PK |
267 | BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false", |
268 | vote ? "true" : "false"); | |
0ff252c1 BYTK |
269 | |
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 | */ | |
286 | static 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 */ | |
634fef61 | 301 | skb_put_u8(skb, cmd); |
0ff252c1 BYTK |
302 | |
303 | skb_queue_tail(&qca->txq, skb); | |
304 | ||
305 | return err; | |
306 | } | |
307 | ||
308 | static 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; | |
31fb1bbd | 314 | unsigned long flags; |
0ff252c1 BYTK |
315 | |
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 | ||
31fb1bbd | 321 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
0ff252c1 BYTK |
322 | |
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 | ||
31fb1bbd | 333 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
0ff252c1 BYTK |
334 | |
335 | /* Actually send the packets */ | |
336 | hci_uart_tx_wakeup(hu); | |
337 | } | |
338 | ||
339 | static 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; | |
31fb1bbd | 344 | unsigned long flags; |
0ff252c1 BYTK |
345 | |
346 | BT_DBG("hu %p wq awake rx", hu); | |
347 | ||
348 | serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu); | |
349 | ||
31fb1bbd | 350 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
0ff252c1 BYTK |
351 | 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 | ||
31fb1bbd | 361 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
0ff252c1 BYTK |
362 | |
363 | /* Actually send the packets */ | |
364 | hci_uart_tx_wakeup(hu); | |
365 | } | |
366 | ||
367 | static 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 | ||
378 | static 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 | ||
04356052 | 395 | static void hci_ibs_tx_idle_timeout(struct timer_list *t) |
0ff252c1 | 396 | { |
04356052 KC |
397 | struct qca_data *qca = from_timer(qca, t, tx_idle_timer); |
398 | struct hci_uart *hu = qca->hu; | |
0ff252c1 BYTK |
399 | 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: | |
e059a465 | 423 | BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); |
0ff252c1 BYTK |
424 | break; |
425 | } | |
426 | ||
427 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
428 | } | |
429 | ||
04356052 | 430 | static void hci_ibs_wake_retrans_timeout(struct timer_list *t) |
0ff252c1 | 431 | { |
04356052 KC |
432 | struct qca_data *qca = from_timer(qca, t, wake_retrans_timer); |
433 | struct hci_uart *hu = qca->hu; | |
0ff252c1 | 434 | unsigned long flags, retrans_delay; |
a9137188 | 435 | bool retransmit = false; |
0ff252c1 BYTK |
436 | |
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 | ||
41d5b25f CC |
443 | /* 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 | ||
0ff252c1 BYTK |
449 | switch (qca->tx_ibs_state) { |
450 | case HCI_IBS_TX_WAKING: | |
451 | /* No WAKE_ACK, retransmit WAKE */ | |
a9137188 | 452 | retransmit = true; |
0ff252c1 BYTK |
453 | 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: | |
e059a465 | 467 | BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state); |
0ff252c1 BYTK |
468 | 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 */ | |
478 | static int qca_open(struct hci_uart *hu) | |
479 | { | |
05ba533c | 480 | struct qca_serdev *qcadev; |
0ff252c1 | 481 | struct qca_data *qca; |
fa9ad876 | 482 | int ret; |
0ff252c1 BYTK |
483 | |
484 | BT_DBG("hu %p qca_open", hu); | |
485 | ||
b36a1552 VD |
486 | if (!hci_uart_has_flow_control(hu)) |
487 | return -EOPNOTSUPP; | |
488 | ||
25a13e38 | 489 | qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL); |
0ff252c1 BYTK |
490 | 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); | |
fac9a602 | 496 | qca->workqueue = alloc_ordered_workqueue("qca_wq", 0); |
0ff252c1 BYTK |
497 | 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 | ||
41d5b25f CC |
508 | init_waitqueue_head(&qca->suspend_wait_q); |
509 | ||
0ff252c1 | 510 | qca->hu = hu; |
2faa3f15 | 511 | init_completion(&qca->drop_ev_comp); |
0ff252c1 BYTK |
512 | |
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 | ||
0ff252c1 | 517 | qca->vote_last_jif = jiffies; |
0ff252c1 BYTK |
518 | |
519 | hu->priv = qca; | |
520 | ||
05ba533c | 521 | if (hu->serdev) { |
05ba533c TE |
522 | |
523 | qcadev = serdev_device_get_drvdata(hu->serdev); | |
523760b7 | 524 | if (!qca_is_wcn399x(qcadev->btsoc_type)) { |
fa9ad876 | 525 | gpiod_set_value_cansleep(qcadev->bt_en, 1); |
7f09d5a6 BG |
526 | /* Controller needs time to bootup. */ |
527 | msleep(150); | |
fa9ad876 BG |
528 | } else { |
529 | hu->init_speed = qcadev->init_speed; | |
530 | hu->oper_speed = qcadev->oper_speed; | |
a9314e76 | 531 | ret = qca_regulator_enable(qcadev); |
fa9ad876 BG |
532 | 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 | } | |
05ba533c TE |
540 | } |
541 | ||
fa9ad876 BG |
542 | timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0); |
543 | qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS; | |
544 | ||
545 | timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0); | |
41d5b25f | 546 | qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS; |
fa9ad876 | 547 | |
0ff252c1 BYTK |
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 | ||
554 | static 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); | |
10be6c0f | 582 | debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote); |
0ff252c1 BYTK |
583 | 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); | |
10be6c0f | 585 | debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote); |
0ff252c1 BYTK |
586 | 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 */ | |
601 | static 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 */ | |
614 | static int qca_close(struct hci_uart *hu) | |
615 | { | |
05ba533c | 616 | struct qca_serdev *qcadev; |
0ff252c1 BYTK |
617 | 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 | ||
05ba533c | 630 | if (hu->serdev) { |
05ba533c | 631 | qcadev = serdev_device_get_drvdata(hu->serdev); |
523760b7 | 632 | if (qca_is_wcn399x(qcadev->btsoc_type)) |
c2d78273 | 633 | qca_power_shutdown(hu); |
fa9ad876 BG |
634 | else |
635 | gpiod_set_value_cansleep(qcadev->bt_en, 0); | |
636 | ||
05ba533c TE |
637 | } |
638 | ||
0ff252c1 BYTK |
639 | 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 | */ | |
650 | static 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 | ||
41d5b25f CC |
661 | /* 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 | ||
0ff252c1 BYTK |
667 | 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 | */ | |
702 | static void device_want_to_sleep(struct hci_uart *hu) | |
703 | { | |
704 | unsigned long flags; | |
705 | struct qca_data *qca = hu->priv; | |
706 | ||
6600c080 | 707 | BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state); |
0ff252c1 BYTK |
708 | |
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: | |
6600c080 | 722 | break; |
0ff252c1 BYTK |
723 | |
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 | ||
41d5b25f CC |
731 | wake_up_interruptible(&qca->suspend_wait_q); |
732 | ||
0ff252c1 BYTK |
733 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
734 | } | |
735 | ||
736 | /* Called upon wake-up-acknowledgement from the device | |
737 | */ | |
738 | static 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 | ||
41d5b25f CC |
750 | /* 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 | ||
0ff252c1 BYTK |
756 | 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 | */ | |
793 | static 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 */ | |
618e8bc2 | 802 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
0ff252c1 | 803 | |
035a960e BG |
804 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); |
805 | ||
0ff252c1 BYTK |
806 | /* Don't go to sleep in middle of patch download or |
807 | * Out-Of-Band(GPIOs control) sleep is selected. | |
41d5b25f | 808 | * Don't wake the device up when suspending. |
0ff252c1 | 809 | */ |
41d5b25f CC |
810 | if (!test_bit(QCA_IBS_ENABLED, &qca->flags) || |
811 | test_bit(QCA_SUSPENDING, &qca->flags)) { | |
0ff252c1 | 812 | skb_queue_tail(&qca->txq, skb); |
035a960e | 813 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); |
0ff252c1 BYTK |
814 | return 0; |
815 | } | |
816 | ||
0ff252c1 BYTK |
817 | /* 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 | ||
854 | static 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 | ||
866 | static 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 | ||
878 | static 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 | ||
c614ca3f BG |
890 | static 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 | ||
2faa3f15 MK |
903 | static 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 | ||
4974c839 | 924 | kfree_skb(skb); |
2faa3f15 MK |
925 | |
926 | return 0; | |
927 | } | |
928 | ||
929 | return hci_recv_frame(hdev, skb); | |
930 | } | |
931 | ||
0ff252c1 BYTK |
932 | #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 | ||
953 | static const struct h4_recv_pkt qca_recv_pkts[] = { | |
c614ca3f | 954 | { H4_RECV_ACL, .recv = qca_recv_acl_data }, |
0ff252c1 | 955 | { H4_RECV_SCO, .recv = hci_recv_frame }, |
2faa3f15 | 956 | { H4_RECV_EVENT, .recv = qca_recv_event }, |
0ff252c1 BYTK |
957 | { 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 | ||
962 | static 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); | |
2064ee33 | 973 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
0ff252c1 BYTK |
974 | qca->rx_skb = NULL; |
975 | return err; | |
976 | } | |
977 | ||
978 | return count; | |
979 | } | |
980 | ||
981 | static 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 | ||
988 | static uint8_t qca_get_baudrate_value(int speed) | |
989 | { | |
ce26d813 | 990 | switch (speed) { |
0ff252c1 BYTK |
991 | 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; | |
be93a497 BG |
1015 | case 3200000: |
1016 | return QCA_BAUDRATE_3200000; | |
0ff252c1 BYTK |
1017 | case 3500000: |
1018 | return QCA_BAUDRATE_3500000; | |
1019 | default: | |
1020 | return QCA_BAUDRATE_115200; | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | static 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 | ||
be93a497 | 1031 | if (baudrate > QCA_BAUDRATE_3200000) |
0ff252c1 BYTK |
1032 | return -EINVAL; |
1033 | ||
1034 | cmd[4] = baudrate; | |
1035 | ||
25a13e38 | 1036 | skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); |
0ff252c1 | 1037 | if (!skb) { |
2064ee33 | 1038 | bt_dev_err(hdev, "Failed to allocate baudrate packet"); |
0ff252c1 BYTK |
1039 | return -ENOMEM; |
1040 | } | |
1041 | ||
1042 | /* Assign commands to change baudrate and packet type. */ | |
59ae1d12 | 1043 | skb_put_data(skb, cmd, sizeof(cmd)); |
618e8bc2 | 1044 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; |
0ff252c1 BYTK |
1045 | |
1046 | skb_queue_tail(&qca->txq, skb); | |
1047 | hci_uart_tx_wakeup(hu); | |
1048 | ||
94d66714 MK |
1049 | /* Wait for the baudrate change request to be sent */ |
1050 | ||
1051 | while (!skb_queue_empty(&qca->txq)) | |
1052 | usleep_range(100, 200); | |
1053 | ||
ecf2b768 MK |
1054 | if (hu->serdev) |
1055 | serdev_device_wait_until_sent(hu->serdev, | |
94d66714 MK |
1056 | msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS)); |
1057 | ||
1058 | /* Give the controller time to process the request */ | |
523760b7 | 1059 | if (qca_is_wcn399x(qca_soc_type(hu))) |
94d66714 MK |
1060 | msleep(10); |
1061 | else | |
1062 | msleep(300); | |
0ff252c1 BYTK |
1063 | |
1064 | return 0; | |
1065 | } | |
1066 | ||
05ba533c TE |
1067 | static 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 | ||
9836b802 | 1075 | static int qca_send_power_pulse(struct hci_uart *hu, bool on) |
fa9ad876 | 1076 | { |
f9558270 | 1077 | int ret; |
94d66714 | 1078 | int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS); |
9836b802 | 1079 | u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE; |
fa9ad876 BG |
1080 | |
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 | */ | |
f9558270 | 1091 | bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd); |
fa9ad876 | 1092 | |
f9558270 | 1093 | serdev_device_write_flush(hu->serdev); |
fa9ad876 | 1094 | hci_uart_set_flow_control(hu, true); |
f9558270 BG |
1095 | 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 | } | |
fa9ad876 | 1100 | |
f9558270 | 1101 | serdev_device_wait_until_sent(hu->serdev, timeout); |
fa9ad876 BG |
1102 | hci_uart_set_flow_control(hu, false); |
1103 | ||
0ebcddd8 | 1104 | /* Give to controller time to boot/shutdown */ |
ad571d72 MK |
1105 | if (on) |
1106 | msleep(100); | |
0ebcddd8 MK |
1107 | else |
1108 | msleep(10); | |
ad571d72 | 1109 | |
fa9ad876 BG |
1110 | return 0; |
1111 | } | |
1112 | ||
83d9c5e5 BG |
1113 | static 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 | ||
1133 | static int qca_check_speeds(struct hci_uart *hu) | |
1134 | { | |
523760b7 | 1135 | if (qca_is_wcn399x(qca_soc_type(hu))) { |
fa9ad876 BG |
1136 | 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 | } | |
83d9c5e5 BG |
1144 | |
1145 | return 0; | |
1146 | } | |
1147 | ||
1148 | static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type) | |
1149 | { | |
1150 | unsigned int speed, qca_baudrate; | |
2faa3f15 | 1151 | struct qca_data *qca = hu->priv; |
78e8fa29 | 1152 | int ret = 0; |
83d9c5e5 BG |
1153 | |
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 { | |
4fdd5a4f MK |
1159 | enum qca_btsoc_type soc_type = qca_soc_type(hu); |
1160 | ||
83d9c5e5 BG |
1161 | speed = qca_get_speed(hu, QCA_OPER_SPEED); |
1162 | if (!speed) | |
1163 | return 0; | |
1164 | ||
78e8fa29 BG |
1165 | /* Disable flow control for wcn3990 to deassert RTS while |
1166 | * changing the baudrate of chip and host. | |
1167 | */ | |
523760b7 | 1168 | if (qca_is_wcn399x(soc_type)) |
78e8fa29 BG |
1169 | hci_uart_set_flow_control(hu, true); |
1170 | ||
2faa3f15 MK |
1171 | if (soc_type == QCA_WCN3990) { |
1172 | reinit_completion(&qca->drop_ev_comp); | |
1173 | set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags); | |
1174 | } | |
1175 | ||
83d9c5e5 | 1176 | qca_baudrate = qca_get_baudrate_value(speed); |
fa9ad876 | 1177 | bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed); |
83d9c5e5 BG |
1178 | ret = qca_set_baudrate(hu->hdev, qca_baudrate); |
1179 | if (ret) | |
78e8fa29 | 1180 | goto error; |
83d9c5e5 BG |
1181 | |
1182 | host_set_baudrate(hu, speed); | |
78e8fa29 BG |
1183 | |
1184 | error: | |
bba79fee | 1185 | if (qca_is_wcn399x(soc_type)) |
78e8fa29 | 1186 | hci_uart_set_flow_control(hu, false); |
2faa3f15 MK |
1187 | |
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 | } | |
83d9c5e5 BG |
1201 | } |
1202 | ||
78e8fa29 | 1203 | return ret; |
83d9c5e5 BG |
1204 | } |
1205 | ||
fa9ad876 BG |
1206 | static int qca_wcn3990_init(struct hci_uart *hu) |
1207 | { | |
3e4be65e | 1208 | struct qca_serdev *qcadev; |
fa9ad876 BG |
1209 | int ret; |
1210 | ||
3e4be65e BG |
1211 | /* 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); | |
a9314e76 | 1217 | ret = qca_regulator_enable(qcadev); |
3e4be65e BG |
1218 | 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 | ||
fa9ad876 BG |
1228 | /* Forcefully enable wcn3990 to enter in to boot mode. */ |
1229 | host_set_baudrate(hu, 2400); | |
9836b802 | 1230 | ret = qca_send_power_pulse(hu, false); |
fa9ad876 BG |
1231 | if (ret) |
1232 | return ret; | |
1233 | ||
1234 | qca_set_speed(hu, QCA_INIT_SPEED); | |
9836b802 | 1235 | ret = qca_send_power_pulse(hu, true); |
fa9ad876 BG |
1236 | if (ret) |
1237 | return ret; | |
1238 | ||
fa9ad876 BG |
1239 | /* 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 | ||
0ff252c1 BYTK |
1256 | static 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; | |
4fdd5a4f | 1261 | enum qca_btsoc_type soc_type = qca_soc_type(hu); |
99c905c6 | 1262 | const char *firmware_name = qca_get_firmware_name(hu); |
0ff252c1 | 1263 | int ret; |
aadebac4 | 1264 | int soc_ver = 0; |
0ff252c1 | 1265 | |
83d9c5e5 BG |
1266 | ret = qca_check_speeds(hu); |
1267 | if (ret) | |
1268 | return ret; | |
1269 | ||
0ff252c1 | 1270 | /* Patch downloading has to be done without IBS mode */ |
62a91990 | 1271 | clear_bit(QCA_IBS_ENABLED, &qca->flags); |
0ff252c1 | 1272 | |
e14c167a RL |
1273 | /* Enable controller to do both LE scan and BR/EDR inquiry |
1274 | * simultaneously. | |
1275 | */ | |
1276 | set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); | |
1277 | ||
523760b7 | 1278 | if (qca_is_wcn399x(soc_type)) { |
fa9ad876 | 1279 | bt_dev_info(hdev, "setting up wcn3990"); |
3e4be65e BG |
1280 | |
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); | |
5971752d | 1285 | set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks); |
3e4be65e | 1286 | hu->hdev->shutdown = qca_power_off; |
fa9ad876 BG |
1287 | ret = qca_wcn3990_init(hu); |
1288 | if (ret) | |
1289 | return ret; | |
1290 | ||
7d250a06 | 1291 | ret = qca_read_soc_version(hdev, &soc_ver, soc_type); |
fa9ad876 BG |
1292 | if (ret) |
1293 | return ret; | |
1294 | } else { | |
1295 | bt_dev_info(hdev, "ROME setup"); | |
1296 | qca_set_speed(hu, QCA_INIT_SPEED); | |
1297 | } | |
0ff252c1 BYTK |
1298 | |
1299 | /* Setup user speed if needed */ | |
83d9c5e5 | 1300 | speed = qca_get_speed(hu, QCA_OPER_SPEED); |
0ff252c1 | 1301 | if (speed) { |
83d9c5e5 BG |
1302 | ret = qca_set_speed(hu, QCA_OPER_SPEED); |
1303 | if (ret) | |
0ff252c1 | 1304 | return ret; |
83d9c5e5 BG |
1305 | |
1306 | qca_baudrate = qca_get_baudrate_value(speed); | |
0ff252c1 BYTK |
1307 | } |
1308 | ||
523760b7 | 1309 | if (!qca_is_wcn399x(soc_type)) { |
fa9ad876 | 1310 | /* Get QCA version information */ |
7d250a06 | 1311 | ret = qca_read_soc_version(hdev, &soc_ver, soc_type); |
fa9ad876 BG |
1312 | if (ret) |
1313 | return ret; | |
1314 | } | |
aadebac4 BG |
1315 | |
1316 | bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver); | |
0ff252c1 | 1317 | /* Setup patch / NVM configurations */ |
99c905c6 RL |
1318 | ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver, |
1319 | firmware_name); | |
0ff252c1 | 1320 | if (!ret) { |
62a91990 | 1321 | set_bit(QCA_IBS_ENABLED, &qca->flags); |
0ff252c1 | 1322 | qca_debugfs_init(hdev); |
ba8f3597 LP |
1323 | } else if (ret == -ENOENT) { |
1324 | /* No patch/nvm-config found, run with original fw/config */ | |
1325 | ret = 0; | |
7dc5fe08 AP |
1326 | } 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; | |
0ff252c1 BYTK |
1332 | } |
1333 | ||
1334 | /* Setup bdaddr */ | |
523760b7 | 1335 | if (qca_is_wcn399x(soc_type)) |
5c0a1001 BG |
1336 | hu->hdev->set_bdaddr = qca_set_bdaddr; |
1337 | else | |
1338 | hu->hdev->set_bdaddr = qca_set_bdaddr_rome; | |
0ff252c1 BYTK |
1339 | |
1340 | return ret; | |
1341 | } | |
1342 | ||
2edc9c5c | 1343 | static const struct hci_uart_proto qca_proto = { |
0ff252c1 BYTK |
1344 | .id = HCI_UART_QCA, |
1345 | .name = "QCA", | |
aee61f7a | 1346 | .manufacturer = 29, |
0ff252c1 BYTK |
1347 | .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 | ||
523760b7 | 1358 | static const struct qca_vreg_data qca_soc_data_wcn3990 = { |
fa9ad876 BG |
1359 | .soc_type = QCA_WCN3990, |
1360 | .vregs = (struct qca_vreg []) { | |
f2edd66e BA |
1361 | { "vddio", 15000 }, |
1362 | { "vddxo", 80000 }, | |
1363 | { "vddrf", 300000 }, | |
1364 | { "vddch0", 450000 }, | |
fa9ad876 BG |
1365 | }, |
1366 | .num_vregs = 4, | |
1367 | }; | |
1368 | ||
7d250a06 BG |
1369 | static const struct qca_vreg_data qca_soc_data_wcn3991 = { |
1370 | .soc_type = QCA_WCN3991, | |
1371 | .vregs = (struct qca_vreg []) { | |
1372 | { "vddio", 15000 }, | |
1373 | { "vddxo", 80000 }, | |
1374 | { "vddrf", 300000 }, | |
1375 | { "vddch0", 450000 }, | |
1376 | }, | |
1377 | .num_vregs = 4, | |
1378 | }; | |
1379 | ||
523760b7 HB |
1380 | static const struct qca_vreg_data qca_soc_data_wcn3998 = { |
1381 | .soc_type = QCA_WCN3998, | |
1382 | .vregs = (struct qca_vreg []) { | |
f2edd66e BA |
1383 | { "vddio", 10000 }, |
1384 | { "vddxo", 80000 }, | |
1385 | { "vddrf", 300000 }, | |
1386 | { "vddch0", 450000 }, | |
523760b7 HB |
1387 | }, |
1388 | .num_vregs = 4, | |
1389 | }; | |
1390 | ||
c2d78273 | 1391 | static void qca_power_shutdown(struct hci_uart *hu) |
fa9ad876 | 1392 | { |
a9314e76 | 1393 | struct qca_serdev *qcadev; |
035a960e BG |
1394 | struct qca_data *qca = hu->priv; |
1395 | unsigned long flags; | |
1396 | ||
a9314e76 BA |
1397 | qcadev = serdev_device_get_drvdata(hu->serdev); |
1398 | ||
035a960e BG |
1399 | /* From this point we go into power off state. But serial port is |
1400 | * still open, stop queueing the IBS data and flush all the buffered | |
1401 | * data in skb's. | |
1402 | */ | |
1403 | spin_lock_irqsave(&qca->hci_ibs_lock, flags); | |
62a91990 | 1404 | clear_bit(QCA_IBS_ENABLED, &qca->flags); |
035a960e BG |
1405 | qca_flush(hu); |
1406 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
1407 | ||
fa9ad876 | 1408 | host_set_baudrate(hu, 2400); |
9836b802 | 1409 | qca_send_power_pulse(hu, false); |
a9314e76 | 1410 | qca_regulator_disable(qcadev); |
fa9ad876 BG |
1411 | } |
1412 | ||
3e4be65e BG |
1413 | static int qca_power_off(struct hci_dev *hdev) |
1414 | { | |
1415 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
1416 | ||
a2780889 HB |
1417 | /* Perform pre shutdown command */ |
1418 | qca_send_pre_shutdown_cmd(hdev); | |
1419 | ||
010376ab HB |
1420 | usleep_range(8000, 10000); |
1421 | ||
3e4be65e BG |
1422 | qca_power_shutdown(hu); |
1423 | return 0; | |
1424 | } | |
1425 | ||
a9314e76 | 1426 | static int qca_regulator_enable(struct qca_serdev *qcadev) |
fa9ad876 | 1427 | { |
a9314e76 BA |
1428 | struct qca_power *power = qcadev->bt_power; |
1429 | int ret; | |
fa9ad876 | 1430 | |
a9314e76 BA |
1431 | /* Already enabled */ |
1432 | if (power->vregs_on) | |
1433 | return 0; | |
fa9ad876 | 1434 | |
a9314e76 | 1435 | BT_DBG("enabling %d regulators)", power->num_vregs); |
fa9ad876 | 1436 | |
a9314e76 BA |
1437 | ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk); |
1438 | if (ret) | |
1439 | return ret; | |
fa9ad876 | 1440 | |
a9314e76 | 1441 | power->vregs_on = true; |
fa9ad876 | 1442 | |
163d42fa | 1443 | return 0; |
fa9ad876 BG |
1444 | } |
1445 | ||
a9314e76 BA |
1446 | static void qca_regulator_disable(struct qca_serdev *qcadev) |
1447 | { | |
1448 | struct qca_power *power; | |
1449 | ||
1450 | if (!qcadev) | |
1451 | return; | |
1452 | ||
1453 | power = qcadev->bt_power; | |
1454 | ||
1455 | /* Already disabled? */ | |
1456 | if (!power->vregs_on) | |
1457 | return; | |
1458 | ||
1459 | regulator_bulk_disable(power->num_vregs, power->vreg_bulk); | |
1460 | power->vregs_on = false; | |
1461 | } | |
1462 | ||
fa9ad876 BG |
1463 | static int qca_init_regulators(struct qca_power *qca, |
1464 | const struct qca_vreg *vregs, size_t num_vregs) | |
1465 | { | |
c29ff107 BA |
1466 | struct regulator_bulk_data *bulk; |
1467 | int ret; | |
fa9ad876 BG |
1468 | int i; |
1469 | ||
c29ff107 BA |
1470 | bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL); |
1471 | if (!bulk) | |
fa9ad876 BG |
1472 | return -ENOMEM; |
1473 | ||
1474 | for (i = 0; i < num_vregs; i++) | |
c29ff107 BA |
1475 | bulk[i].supply = vregs[i].name; |
1476 | ||
1477 | ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk); | |
1478 | if (ret < 0) | |
1479 | return ret; | |
fa9ad876 | 1480 | |
c29ff107 BA |
1481 | for (i = 0; i < num_vregs; i++) { |
1482 | ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA); | |
1483 | if (ret) | |
1484 | return ret; | |
1485 | } | |
1486 | ||
1487 | qca->vreg_bulk = bulk; | |
163d42fa | 1488 | qca->num_vregs = num_vregs; |
c29ff107 BA |
1489 | |
1490 | return 0; | |
fa9ad876 BG |
1491 | } |
1492 | ||
05ba533c TE |
1493 | static int qca_serdev_probe(struct serdev_device *serdev) |
1494 | { | |
1495 | struct qca_serdev *qcadev; | |
fa9ad876 | 1496 | const struct qca_vreg_data *data; |
05ba533c TE |
1497 | int err; |
1498 | ||
1499 | qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL); | |
1500 | if (!qcadev) | |
1501 | return -ENOMEM; | |
1502 | ||
1503 | qcadev->serdev_hu.serdev = serdev; | |
fa9ad876 | 1504 | data = of_device_get_match_data(&serdev->dev); |
05ba533c | 1505 | serdev_device_set_drvdata(serdev, qcadev); |
99c905c6 RL |
1506 | device_property_read_string(&serdev->dev, "firmware-name", |
1507 | &qcadev->firmware_name); | |
523760b7 HB |
1508 | if (data && qca_is_wcn399x(data->soc_type)) { |
1509 | qcadev->btsoc_type = data->soc_type; | |
fa9ad876 BG |
1510 | qcadev->bt_power = devm_kzalloc(&serdev->dev, |
1511 | sizeof(struct qca_power), | |
1512 | GFP_KERNEL); | |
1513 | if (!qcadev->bt_power) | |
1514 | return -ENOMEM; | |
1515 | ||
1516 | qcadev->bt_power->dev = &serdev->dev; | |
fa9ad876 BG |
1517 | err = qca_init_regulators(qcadev->bt_power, data->vregs, |
1518 | data->num_vregs); | |
1519 | if (err) { | |
1520 | BT_ERR("Failed to init regulators:%d", err); | |
1521 | goto out; | |
1522 | } | |
05ba533c | 1523 | |
fa9ad876 | 1524 | qcadev->bt_power->vregs_on = false; |
05ba533c | 1525 | |
fa9ad876 BG |
1526 | device_property_read_u32(&serdev->dev, "max-speed", |
1527 | &qcadev->oper_speed); | |
1528 | if (!qcadev->oper_speed) | |
1529 | BT_DBG("UART will pick default operating speed"); | |
05ba533c | 1530 | |
fa9ad876 BG |
1531 | err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); |
1532 | if (err) { | |
1533 | BT_ERR("wcn3990 serdev registration failed"); | |
1534 | goto out; | |
1535 | } | |
1536 | } else { | |
1537 | qcadev->btsoc_type = QCA_ROME; | |
1538 | qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable", | |
1539 | GPIOD_OUT_LOW); | |
1540 | if (IS_ERR(qcadev->bt_en)) { | |
1541 | dev_err(&serdev->dev, "failed to acquire enable gpio\n"); | |
1542 | return PTR_ERR(qcadev->bt_en); | |
1543 | } | |
05ba533c | 1544 | |
fa9ad876 BG |
1545 | qcadev->susclk = devm_clk_get(&serdev->dev, NULL); |
1546 | if (IS_ERR(qcadev->susclk)) { | |
1547 | dev_err(&serdev->dev, "failed to acquire clk\n"); | |
1548 | return PTR_ERR(qcadev->susclk); | |
1549 | } | |
05ba533c | 1550 | |
fa9ad876 BG |
1551 | err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ); |
1552 | if (err) | |
1553 | return err; | |
1554 | ||
1555 | err = clk_prepare_enable(qcadev->susclk); | |
1556 | if (err) | |
1557 | return err; | |
1558 | ||
1559 | err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto); | |
1560 | if (err) | |
1561 | clk_disable_unprepare(qcadev->susclk); | |
1562 | } | |
1563 | ||
1564 | out: return err; | |
05ba533c | 1565 | |
05ba533c TE |
1566 | } |
1567 | ||
1568 | static void qca_serdev_remove(struct serdev_device *serdev) | |
1569 | { | |
1570 | struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev); | |
1571 | ||
523760b7 | 1572 | if (qca_is_wcn399x(qcadev->btsoc_type)) |
c2d78273 | 1573 | qca_power_shutdown(&qcadev->serdev_hu); |
fa9ad876 BG |
1574 | else |
1575 | clk_disable_unprepare(qcadev->susclk); | |
05ba533c | 1576 | |
fa9ad876 | 1577 | hci_uart_unregister_device(&qcadev->serdev_hu); |
05ba533c TE |
1578 | } |
1579 | ||
41d5b25f CC |
1580 | static int __maybe_unused qca_suspend(struct device *dev) |
1581 | { | |
1582 | struct hci_dev *hdev = container_of(dev, struct hci_dev, dev); | |
1583 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
1584 | struct qca_data *qca = hu->priv; | |
1585 | unsigned long flags; | |
1586 | int ret = 0; | |
1587 | u8 cmd; | |
1588 | ||
1589 | set_bit(QCA_SUSPENDING, &qca->flags); | |
1590 | ||
1591 | /* Device is downloading patch or doesn't support in-band sleep. */ | |
1592 | if (!test_bit(QCA_IBS_ENABLED, &qca->flags)) | |
1593 | return 0; | |
1594 | ||
1595 | cancel_work_sync(&qca->ws_awake_device); | |
1596 | cancel_work_sync(&qca->ws_awake_rx); | |
1597 | ||
1598 | spin_lock_irqsave_nested(&qca->hci_ibs_lock, | |
1599 | flags, SINGLE_DEPTH_NESTING); | |
1600 | ||
1601 | switch (qca->tx_ibs_state) { | |
1602 | case HCI_IBS_TX_WAKING: | |
1603 | del_timer(&qca->wake_retrans_timer); | |
1604 | /* Fall through */ | |
1605 | case HCI_IBS_TX_AWAKE: | |
1606 | del_timer(&qca->tx_idle_timer); | |
1607 | ||
1608 | serdev_device_write_flush(hu->serdev); | |
1609 | cmd = HCI_IBS_SLEEP_IND; | |
1610 | ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd)); | |
1611 | ||
1612 | if (ret < 0) { | |
1613 | BT_ERR("Failed to send SLEEP to device"); | |
1614 | break; | |
1615 | } | |
1616 | ||
1617 | qca->tx_ibs_state = HCI_IBS_TX_ASLEEP; | |
1618 | qca->ibs_sent_slps++; | |
1619 | ||
1620 | qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off); | |
1621 | break; | |
1622 | ||
1623 | case HCI_IBS_TX_ASLEEP: | |
1624 | break; | |
1625 | ||
1626 | default: | |
1627 | BT_ERR("Spurious tx state %d", qca->tx_ibs_state); | |
1628 | ret = -EINVAL; | |
1629 | break; | |
1630 | } | |
1631 | ||
1632 | spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); | |
1633 | ||
1634 | if (ret < 0) | |
1635 | goto error; | |
1636 | ||
1637 | serdev_device_wait_until_sent(hu->serdev, | |
1638 | msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS)); | |
1639 | ||
1640 | /* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going | |
1641 | * to sleep, so that the packet does not wake the system later. | |
1642 | */ | |
1643 | ||
1644 | ret = wait_event_interruptible_timeout(qca->suspend_wait_q, | |
1645 | qca->rx_ibs_state == HCI_IBS_RX_ASLEEP, | |
1646 | msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS)); | |
1647 | ||
1648 | if (ret > 0) | |
1649 | return 0; | |
1650 | ||
1651 | if (ret == 0) | |
1652 | ret = -ETIMEDOUT; | |
1653 | ||
1654 | error: | |
1655 | clear_bit(QCA_SUSPENDING, &qca->flags); | |
1656 | ||
1657 | return ret; | |
1658 | } | |
1659 | ||
1660 | static int __maybe_unused qca_resume(struct device *dev) | |
1661 | { | |
1662 | struct hci_dev *hdev = container_of(dev, struct hci_dev, dev); | |
1663 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
1664 | struct qca_data *qca = hu->priv; | |
1665 | ||
1666 | clear_bit(QCA_SUSPENDING, &qca->flags); | |
1667 | ||
1668 | return 0; | |
1669 | } | |
1670 | ||
1671 | static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume); | |
1672 | ||
05ba533c TE |
1673 | static const struct of_device_id qca_bluetooth_of_match[] = { |
1674 | { .compatible = "qcom,qca6174-bt" }, | |
523760b7 | 1675 | { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990}, |
7d250a06 | 1676 | { .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991}, |
523760b7 | 1677 | { .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998}, |
05ba533c TE |
1678 | { /* sentinel */ } |
1679 | }; | |
1680 | MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match); | |
1681 | ||
1682 | static struct serdev_device_driver qca_serdev_driver = { | |
1683 | .probe = qca_serdev_probe, | |
1684 | .remove = qca_serdev_remove, | |
1685 | .driver = { | |
1686 | .name = "hci_uart_qca", | |
1687 | .of_match_table = qca_bluetooth_of_match, | |
41d5b25f | 1688 | .pm = &qca_pm_ops, |
05ba533c TE |
1689 | }, |
1690 | }; | |
1691 | ||
0ff252c1 BYTK |
1692 | int __init qca_init(void) |
1693 | { | |
05ba533c TE |
1694 | serdev_device_driver_register(&qca_serdev_driver); |
1695 | ||
0ff252c1 BYTK |
1696 | return hci_uart_register_proto(&qca_proto); |
1697 | } | |
1698 | ||
1699 | int __exit qca_deinit(void) | |
1700 | { | |
05ba533c TE |
1701 | serdev_device_driver_unregister(&qca_serdev_driver); |
1702 | ||
0ff252c1 BYTK |
1703 | return hci_uart_unregister_proto(&qca_proto); |
1704 | } |