xref: /linux/drivers/bluetooth/hci_qca.c (revision 40e79150c1686263e6a031d7702aec63aff31332)
1 // SPDX-License-Identifier: GPL-2.0-only
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.
9  *  Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
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.
16  */
17 
18 #include <linux/kernel.h>
19 #include <linux/clk.h>
20 #include <linux/completion.h>
21 #include <linux/debugfs.h>
22 #include <linux/delay.h>
23 #include <linux/devcoredump.h>
24 #include <linux/device.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/mod_devicetable.h>
27 #include <linux/module.h>
28 #include <linux/of_device.h>
29 #include <linux/acpi.h>
30 #include <linux/platform_device.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/serdev.h>
33 #include <linux/mutex.h>
34 #include <asm/unaligned.h>
35 
36 #include <net/bluetooth/bluetooth.h>
37 #include <net/bluetooth/hci_core.h>
38 
39 #include "hci_uart.h"
40 #include "btqca.h"
41 
42 /* HCI_IBS protocol messages */
43 #define HCI_IBS_SLEEP_IND	0xFE
44 #define HCI_IBS_WAKE_IND	0xFD
45 #define HCI_IBS_WAKE_ACK	0xFC
46 #define HCI_MAX_IBS_SIZE	10
47 
48 #define IBS_WAKE_RETRANS_TIMEOUT_MS	100
49 #define IBS_BTSOC_TX_IDLE_TIMEOUT_MS	40
50 #define IBS_HOST_TX_IDLE_TIMEOUT_MS	2000
51 #define CMD_TRANS_TIMEOUT_MS		100
52 #define MEMDUMP_TIMEOUT_MS		8000
53 
54 /* susclk rate */
55 #define SUSCLK_RATE_32KHZ	32768
56 
57 /* Controller debug log header */
58 #define QCA_DEBUG_HANDLE	0x2EDC
59 
60 /* max retry count when init fails */
61 #define MAX_INIT_RETRIES 3
62 
63 /* Controller dump header */
64 #define QCA_SSR_DUMP_HANDLE		0x0108
65 #define QCA_DUMP_PACKET_SIZE		255
66 #define QCA_LAST_SEQUENCE_NUM		0xFFFF
67 #define QCA_CRASHBYTE_PACKET_LEN	1096
68 #define QCA_MEMDUMP_BYTE		0xFB
69 
70 enum qca_flags {
71 	QCA_IBS_ENABLED,
72 	QCA_DROP_VENDOR_EVENT,
73 	QCA_SUSPENDING,
74 	QCA_MEMDUMP_COLLECTION,
75 	QCA_HW_ERROR_EVENT
76 };
77 
78 
79 /* HCI_IBS transmit side sleep protocol states */
80 enum tx_ibs_states {
81 	HCI_IBS_TX_ASLEEP,
82 	HCI_IBS_TX_WAKING,
83 	HCI_IBS_TX_AWAKE,
84 };
85 
86 /* HCI_IBS receive side sleep protocol states */
87 enum rx_states {
88 	HCI_IBS_RX_ASLEEP,
89 	HCI_IBS_RX_AWAKE,
90 };
91 
92 /* HCI_IBS transmit and receive side clock state vote */
93 enum hci_ibs_clock_state_vote {
94 	HCI_IBS_VOTE_STATS_UPDATE,
95 	HCI_IBS_TX_VOTE_CLOCK_ON,
96 	HCI_IBS_TX_VOTE_CLOCK_OFF,
97 	HCI_IBS_RX_VOTE_CLOCK_ON,
98 	HCI_IBS_RX_VOTE_CLOCK_OFF,
99 };
100 
101 /* Controller memory dump states */
102 enum qca_memdump_states {
103 	QCA_MEMDUMP_IDLE,
104 	QCA_MEMDUMP_COLLECTING,
105 	QCA_MEMDUMP_COLLECTED,
106 	QCA_MEMDUMP_TIMEOUT,
107 };
108 
109 struct qca_memdump_data {
110 	char *memdump_buf_head;
111 	char *memdump_buf_tail;
112 	u32 current_seq_no;
113 	u32 received_dump;
114 };
115 
116 struct qca_memdump_event_hdr {
117 	__u8    evt;
118 	__u8    plen;
119 	__u16   opcode;
120 	__u16   seq_no;
121 	__u8    reserved;
122 } __packed;
123 
124 
125 struct qca_dump_size {
126 	u32 dump_size;
127 } __packed;
128 
129 struct qca_data {
130 	struct hci_uart *hu;
131 	struct sk_buff *rx_skb;
132 	struct sk_buff_head txq;
133 	struct sk_buff_head tx_wait_q;	/* HCI_IBS wait queue	*/
134 	struct sk_buff_head rx_memdump_q;	/* Memdump wait queue	*/
135 	spinlock_t hci_ibs_lock;	/* HCI_IBS state lock	*/
136 	u8 tx_ibs_state;	/* HCI_IBS transmit side power state*/
137 	u8 rx_ibs_state;	/* HCI_IBS receive side power state */
138 	bool tx_vote;		/* Clock must be on for TX */
139 	bool rx_vote;		/* Clock must be on for RX */
140 	struct timer_list tx_idle_timer;
141 	u32 tx_idle_delay;
142 	struct timer_list wake_retrans_timer;
143 	u32 wake_retrans;
144 	struct workqueue_struct *workqueue;
145 	struct work_struct ws_awake_rx;
146 	struct work_struct ws_awake_device;
147 	struct work_struct ws_rx_vote_off;
148 	struct work_struct ws_tx_vote_off;
149 	struct work_struct ctrl_memdump_evt;
150 	struct delayed_work ctrl_memdump_timeout;
151 	struct qca_memdump_data *qca_memdump;
152 	unsigned long flags;
153 	struct completion drop_ev_comp;
154 	wait_queue_head_t suspend_wait_q;
155 	enum qca_memdump_states memdump_state;
156 	struct mutex hci_memdump_lock;
157 
158 	/* For debugging purpose */
159 	u64 ibs_sent_wacks;
160 	u64 ibs_sent_slps;
161 	u64 ibs_sent_wakes;
162 	u64 ibs_recv_wacks;
163 	u64 ibs_recv_slps;
164 	u64 ibs_recv_wakes;
165 	u64 vote_last_jif;
166 	u32 vote_on_ms;
167 	u32 vote_off_ms;
168 	u64 tx_votes_on;
169 	u64 rx_votes_on;
170 	u64 tx_votes_off;
171 	u64 rx_votes_off;
172 	u64 votes_on;
173 	u64 votes_off;
174 };
175 
176 enum qca_speed_type {
177 	QCA_INIT_SPEED = 1,
178 	QCA_OPER_SPEED
179 };
180 
181 /*
182  * Voltage regulator information required for configuring the
183  * QCA Bluetooth chipset
184  */
185 struct qca_vreg {
186 	const char *name;
187 	unsigned int load_uA;
188 };
189 
190 struct qca_vreg_data {
191 	enum qca_btsoc_type soc_type;
192 	struct qca_vreg *vregs;
193 	size_t num_vregs;
194 };
195 
196 /*
197  * Platform data for the QCA Bluetooth power driver.
198  */
199 struct qca_power {
200 	struct device *dev;
201 	struct regulator_bulk_data *vreg_bulk;
202 	int num_vregs;
203 	bool vregs_on;
204 };
205 
206 struct qca_serdev {
207 	struct hci_uart	 serdev_hu;
208 	struct gpio_desc *bt_en;
209 	struct clk	 *susclk;
210 	enum qca_btsoc_type btsoc_type;
211 	struct qca_power *bt_power;
212 	u32 init_speed;
213 	u32 oper_speed;
214 	const char *firmware_name;
215 };
216 
217 static int qca_regulator_enable(struct qca_serdev *qcadev);
218 static void qca_regulator_disable(struct qca_serdev *qcadev);
219 static void qca_power_shutdown(struct hci_uart *hu);
220 static int qca_power_off(struct hci_dev *hdev);
221 static void qca_controller_memdump(struct work_struct *work);
222 
223 static enum qca_btsoc_type qca_soc_type(struct hci_uart *hu)
224 {
225 	enum qca_btsoc_type soc_type;
226 
227 	if (hu->serdev) {
228 		struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
229 
230 		soc_type = qsd->btsoc_type;
231 	} else {
232 		soc_type = QCA_ROME;
233 	}
234 
235 	return soc_type;
236 }
237 
238 static const char *qca_get_firmware_name(struct hci_uart *hu)
239 {
240 	if (hu->serdev) {
241 		struct qca_serdev *qsd = serdev_device_get_drvdata(hu->serdev);
242 
243 		return qsd->firmware_name;
244 	} else {
245 		return NULL;
246 	}
247 }
248 
249 static void __serial_clock_on(struct tty_struct *tty)
250 {
251 	/* TODO: Some chipset requires to enable UART clock on client
252 	 * side to save power consumption or manual work is required.
253 	 * Please put your code to control UART clock here if needed
254 	 */
255 }
256 
257 static void __serial_clock_off(struct tty_struct *tty)
258 {
259 	/* TODO: Some chipset requires to disable UART clock on client
260 	 * side to save power consumption or manual work is required.
261 	 * Please put your code to control UART clock off here if needed
262 	 */
263 }
264 
265 /* serial_clock_vote needs to be called with the ibs lock held */
266 static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
267 {
268 	struct qca_data *qca = hu->priv;
269 	unsigned int diff;
270 
271 	bool old_vote = (qca->tx_vote | qca->rx_vote);
272 	bool new_vote;
273 
274 	switch (vote) {
275 	case HCI_IBS_VOTE_STATS_UPDATE:
276 		diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
277 
278 		if (old_vote)
279 			qca->vote_off_ms += diff;
280 		else
281 			qca->vote_on_ms += diff;
282 		return;
283 
284 	case HCI_IBS_TX_VOTE_CLOCK_ON:
285 		qca->tx_vote = true;
286 		qca->tx_votes_on++;
287 		new_vote = true;
288 		break;
289 
290 	case HCI_IBS_RX_VOTE_CLOCK_ON:
291 		qca->rx_vote = true;
292 		qca->rx_votes_on++;
293 		new_vote = true;
294 		break;
295 
296 	case HCI_IBS_TX_VOTE_CLOCK_OFF:
297 		qca->tx_vote = false;
298 		qca->tx_votes_off++;
299 		new_vote = qca->rx_vote | qca->tx_vote;
300 		break;
301 
302 	case HCI_IBS_RX_VOTE_CLOCK_OFF:
303 		qca->rx_vote = false;
304 		qca->rx_votes_off++;
305 		new_vote = qca->rx_vote | qca->tx_vote;
306 		break;
307 
308 	default:
309 		BT_ERR("Voting irregularity");
310 		return;
311 	}
312 
313 	if (new_vote != old_vote) {
314 		if (new_vote)
315 			__serial_clock_on(hu->tty);
316 		else
317 			__serial_clock_off(hu->tty);
318 
319 		BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
320 		       vote ? "true" : "false");
321 
322 		diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
323 
324 		if (new_vote) {
325 			qca->votes_on++;
326 			qca->vote_off_ms += diff;
327 		} else {
328 			qca->votes_off++;
329 			qca->vote_on_ms += diff;
330 		}
331 		qca->vote_last_jif = jiffies;
332 	}
333 }
334 
335 /* Builds and sends an HCI_IBS command packet.
336  * These are very simple packets with only 1 cmd byte.
337  */
338 static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
339 {
340 	int err = 0;
341 	struct sk_buff *skb = NULL;
342 	struct qca_data *qca = hu->priv;
343 
344 	BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
345 
346 	skb = bt_skb_alloc(1, GFP_ATOMIC);
347 	if (!skb) {
348 		BT_ERR("Failed to allocate memory for HCI_IBS packet");
349 		return -ENOMEM;
350 	}
351 
352 	/* Assign HCI_IBS type */
353 	skb_put_u8(skb, cmd);
354 
355 	skb_queue_tail(&qca->txq, skb);
356 
357 	return err;
358 }
359 
360 static void qca_wq_awake_device(struct work_struct *work)
361 {
362 	struct qca_data *qca = container_of(work, struct qca_data,
363 					    ws_awake_device);
364 	struct hci_uart *hu = qca->hu;
365 	unsigned long retrans_delay;
366 	unsigned long flags;
367 
368 	BT_DBG("hu %p wq awake device", hu);
369 
370 	/* Vote for serial clock */
371 	serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
372 
373 	spin_lock_irqsave(&qca->hci_ibs_lock, flags);
374 
375 	/* Send wake indication to device */
376 	if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
377 		BT_ERR("Failed to send WAKE to device");
378 
379 	qca->ibs_sent_wakes++;
380 
381 	/* Start retransmit timer */
382 	retrans_delay = msecs_to_jiffies(qca->wake_retrans);
383 	mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
384 
385 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
386 
387 	/* Actually send the packets */
388 	hci_uart_tx_wakeup(hu);
389 }
390 
391 static void qca_wq_awake_rx(struct work_struct *work)
392 {
393 	struct qca_data *qca = container_of(work, struct qca_data,
394 					    ws_awake_rx);
395 	struct hci_uart *hu = qca->hu;
396 	unsigned long flags;
397 
398 	BT_DBG("hu %p wq awake rx", hu);
399 
400 	serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
401 
402 	spin_lock_irqsave(&qca->hci_ibs_lock, flags);
403 	qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
404 
405 	/* Always acknowledge device wake up,
406 	 * sending IBS message doesn't count as TX ON.
407 	 */
408 	if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
409 		BT_ERR("Failed to acknowledge device wake up");
410 
411 	qca->ibs_sent_wacks++;
412 
413 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
414 
415 	/* Actually send the packets */
416 	hci_uart_tx_wakeup(hu);
417 }
418 
419 static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
420 {
421 	struct qca_data *qca = container_of(work, struct qca_data,
422 					    ws_rx_vote_off);
423 	struct hci_uart *hu = qca->hu;
424 
425 	BT_DBG("hu %p rx clock vote off", hu);
426 
427 	serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
428 }
429 
430 static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
431 {
432 	struct qca_data *qca = container_of(work, struct qca_data,
433 					    ws_tx_vote_off);
434 	struct hci_uart *hu = qca->hu;
435 
436 	BT_DBG("hu %p tx clock vote off", hu);
437 
438 	/* Run HCI tx handling unlocked */
439 	hci_uart_tx_wakeup(hu);
440 
441 	/* Now that message queued to tty driver, vote for tty clocks off.
442 	 * It is up to the tty driver to pend the clocks off until tx done.
443 	 */
444 	serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
445 }
446 
447 static void hci_ibs_tx_idle_timeout(struct timer_list *t)
448 {
449 	struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
450 	struct hci_uart *hu = qca->hu;
451 	unsigned long flags;
452 
453 	BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
454 
455 	spin_lock_irqsave_nested(&qca->hci_ibs_lock,
456 				 flags, SINGLE_DEPTH_NESTING);
457 
458 	switch (qca->tx_ibs_state) {
459 	case HCI_IBS_TX_AWAKE:
460 		/* TX_IDLE, go to SLEEP */
461 		if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
462 			BT_ERR("Failed to send SLEEP to device");
463 			break;
464 		}
465 		qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
466 		qca->ibs_sent_slps++;
467 		queue_work(qca->workqueue, &qca->ws_tx_vote_off);
468 		break;
469 
470 	case HCI_IBS_TX_ASLEEP:
471 	case HCI_IBS_TX_WAKING:
472 		/* Fall through */
473 
474 	default:
475 		BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
476 		break;
477 	}
478 
479 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
480 }
481 
482 static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
483 {
484 	struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
485 	struct hci_uart *hu = qca->hu;
486 	unsigned long flags, retrans_delay;
487 	bool retransmit = false;
488 
489 	BT_DBG("hu %p wake retransmit timeout in %d state",
490 		hu, qca->tx_ibs_state);
491 
492 	spin_lock_irqsave_nested(&qca->hci_ibs_lock,
493 				 flags, SINGLE_DEPTH_NESTING);
494 
495 	/* Don't retransmit the HCI_IBS_WAKE_IND when suspending. */
496 	if (test_bit(QCA_SUSPENDING, &qca->flags)) {
497 		spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
498 		return;
499 	}
500 
501 	switch (qca->tx_ibs_state) {
502 	case HCI_IBS_TX_WAKING:
503 		/* No WAKE_ACK, retransmit WAKE */
504 		retransmit = true;
505 		if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
506 			BT_ERR("Failed to acknowledge device wake up");
507 			break;
508 		}
509 		qca->ibs_sent_wakes++;
510 		retrans_delay = msecs_to_jiffies(qca->wake_retrans);
511 		mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
512 		break;
513 
514 	case HCI_IBS_TX_ASLEEP:
515 	case HCI_IBS_TX_AWAKE:
516 		/* Fall through */
517 
518 	default:
519 		BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
520 		break;
521 	}
522 
523 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
524 
525 	if (retransmit)
526 		hci_uart_tx_wakeup(hu);
527 }
528 
529 
530 static void qca_controller_memdump_timeout(struct work_struct *work)
531 {
532 	struct qca_data *qca = container_of(work, struct qca_data,
533 					ctrl_memdump_timeout.work);
534 	struct hci_uart *hu = qca->hu;
535 
536 	mutex_lock(&qca->hci_memdump_lock);
537 	if (test_bit(QCA_MEMDUMP_COLLECTION, &qca->flags)) {
538 		qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
539 		if (!test_bit(QCA_HW_ERROR_EVENT, &qca->flags)) {
540 			/* Inject hw error event to reset the device
541 			 * and driver.
542 			 */
543 			hci_reset_dev(hu->hdev);
544 		}
545 	}
546 
547 	mutex_unlock(&qca->hci_memdump_lock);
548 }
549 
550 
551 /* Initialize protocol */
552 static int qca_open(struct hci_uart *hu)
553 {
554 	struct qca_serdev *qcadev;
555 	struct qca_data *qca;
556 
557 	BT_DBG("hu %p qca_open", hu);
558 
559 	if (!hci_uart_has_flow_control(hu))
560 		return -EOPNOTSUPP;
561 
562 	qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
563 	if (!qca)
564 		return -ENOMEM;
565 
566 	skb_queue_head_init(&qca->txq);
567 	skb_queue_head_init(&qca->tx_wait_q);
568 	skb_queue_head_init(&qca->rx_memdump_q);
569 	spin_lock_init(&qca->hci_ibs_lock);
570 	mutex_init(&qca->hci_memdump_lock);
571 	qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
572 	if (!qca->workqueue) {
573 		BT_ERR("QCA Workqueue not initialized properly");
574 		kfree(qca);
575 		return -ENOMEM;
576 	}
577 
578 	INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
579 	INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
580 	INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
581 	INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
582 	INIT_WORK(&qca->ctrl_memdump_evt, qca_controller_memdump);
583 	INIT_DELAYED_WORK(&qca->ctrl_memdump_timeout,
584 			  qca_controller_memdump_timeout);
585 	init_waitqueue_head(&qca->suspend_wait_q);
586 
587 	qca->hu = hu;
588 	init_completion(&qca->drop_ev_comp);
589 
590 	/* Assume we start with both sides asleep -- extra wakes OK */
591 	qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
592 	qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
593 
594 	qca->vote_last_jif = jiffies;
595 
596 	hu->priv = qca;
597 
598 	if (hu->serdev) {
599 		qcadev = serdev_device_get_drvdata(hu->serdev);
600 		if (qca_is_wcn399x(qcadev->btsoc_type)) {
601 			hu->init_speed = qcadev->init_speed;
602 			hu->oper_speed = qcadev->oper_speed;
603 		}
604 	}
605 
606 	timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
607 	qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
608 
609 	timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
610 	qca->tx_idle_delay = IBS_HOST_TX_IDLE_TIMEOUT_MS;
611 
612 	BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
613 	       qca->tx_idle_delay, qca->wake_retrans);
614 
615 	return 0;
616 }
617 
618 static void qca_debugfs_init(struct hci_dev *hdev)
619 {
620 	struct hci_uart *hu = hci_get_drvdata(hdev);
621 	struct qca_data *qca = hu->priv;
622 	struct dentry *ibs_dir;
623 	umode_t mode;
624 
625 	if (!hdev->debugfs)
626 		return;
627 
628 	ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
629 
630 	/* read only */
631 	mode = S_IRUGO;
632 	debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
633 	debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
634 	debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
635 			   &qca->ibs_sent_slps);
636 	debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
637 			   &qca->ibs_sent_wakes);
638 	debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
639 			   &qca->ibs_sent_wacks);
640 	debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
641 			   &qca->ibs_recv_slps);
642 	debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
643 			   &qca->ibs_recv_wakes);
644 	debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
645 			   &qca->ibs_recv_wacks);
646 	debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
647 	debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
648 	debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
649 	debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
650 	debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
651 	debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
652 	debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
653 	debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
654 	debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
655 	debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
656 
657 	/* read/write */
658 	mode = S_IRUGO | S_IWUSR;
659 	debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
660 	debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
661 			   &qca->tx_idle_delay);
662 }
663 
664 /* Flush protocol data */
665 static int qca_flush(struct hci_uart *hu)
666 {
667 	struct qca_data *qca = hu->priv;
668 
669 	BT_DBG("hu %p qca flush", hu);
670 
671 	skb_queue_purge(&qca->tx_wait_q);
672 	skb_queue_purge(&qca->txq);
673 
674 	return 0;
675 }
676 
677 /* Close protocol */
678 static int qca_close(struct hci_uart *hu)
679 {
680 	struct qca_data *qca = hu->priv;
681 
682 	BT_DBG("hu %p qca close", hu);
683 
684 	serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
685 
686 	skb_queue_purge(&qca->tx_wait_q);
687 	skb_queue_purge(&qca->txq);
688 	skb_queue_purge(&qca->rx_memdump_q);
689 	del_timer(&qca->tx_idle_timer);
690 	del_timer(&qca->wake_retrans_timer);
691 	destroy_workqueue(qca->workqueue);
692 	qca->hu = NULL;
693 
694 	qca_power_shutdown(hu);
695 
696 	kfree_skb(qca->rx_skb);
697 
698 	hu->priv = NULL;
699 
700 	kfree(qca);
701 
702 	return 0;
703 }
704 
705 /* Called upon a wake-up-indication from the device.
706  */
707 static void device_want_to_wakeup(struct hci_uart *hu)
708 {
709 	unsigned long flags;
710 	struct qca_data *qca = hu->priv;
711 
712 	BT_DBG("hu %p want to wake up", hu);
713 
714 	spin_lock_irqsave(&qca->hci_ibs_lock, flags);
715 
716 	qca->ibs_recv_wakes++;
717 
718 	/* Don't wake the rx up when suspending. */
719 	if (test_bit(QCA_SUSPENDING, &qca->flags)) {
720 		spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
721 		return;
722 	}
723 
724 	switch (qca->rx_ibs_state) {
725 	case HCI_IBS_RX_ASLEEP:
726 		/* Make sure clock is on - we may have turned clock off since
727 		 * receiving the wake up indicator awake rx clock.
728 		 */
729 		queue_work(qca->workqueue, &qca->ws_awake_rx);
730 		spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
731 		return;
732 
733 	case HCI_IBS_RX_AWAKE:
734 		/* Always acknowledge device wake up,
735 		 * sending IBS message doesn't count as TX ON.
736 		 */
737 		if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
738 			BT_ERR("Failed to acknowledge device wake up");
739 			break;
740 		}
741 		qca->ibs_sent_wacks++;
742 		break;
743 
744 	default:
745 		/* Any other state is illegal */
746 		BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
747 		       qca->rx_ibs_state);
748 		break;
749 	}
750 
751 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
752 
753 	/* Actually send the packets */
754 	hci_uart_tx_wakeup(hu);
755 }
756 
757 /* Called upon a sleep-indication from the device.
758  */
759 static void device_want_to_sleep(struct hci_uart *hu)
760 {
761 	unsigned long flags;
762 	struct qca_data *qca = hu->priv;
763 
764 	BT_DBG("hu %p want to sleep in %d state", hu, qca->rx_ibs_state);
765 
766 	spin_lock_irqsave(&qca->hci_ibs_lock, flags);
767 
768 	qca->ibs_recv_slps++;
769 
770 	switch (qca->rx_ibs_state) {
771 	case HCI_IBS_RX_AWAKE:
772 		/* Update state */
773 		qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
774 		/* Vote off rx clock under workqueue */
775 		queue_work(qca->workqueue, &qca->ws_rx_vote_off);
776 		break;
777 
778 	case HCI_IBS_RX_ASLEEP:
779 		break;
780 
781 	default:
782 		/* Any other state is illegal */
783 		BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
784 		       qca->rx_ibs_state);
785 		break;
786 	}
787 
788 	wake_up_interruptible(&qca->suspend_wait_q);
789 
790 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
791 }
792 
793 /* Called upon wake-up-acknowledgement from the device
794  */
795 static void device_woke_up(struct hci_uart *hu)
796 {
797 	unsigned long flags, idle_delay;
798 	struct qca_data *qca = hu->priv;
799 	struct sk_buff *skb = NULL;
800 
801 	BT_DBG("hu %p woke up", hu);
802 
803 	spin_lock_irqsave(&qca->hci_ibs_lock, flags);
804 
805 	qca->ibs_recv_wacks++;
806 
807 	/* Don't react to the wake-up-acknowledgment when suspending. */
808 	if (test_bit(QCA_SUSPENDING, &qca->flags)) {
809 		spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
810 		return;
811 	}
812 
813 	switch (qca->tx_ibs_state) {
814 	case HCI_IBS_TX_AWAKE:
815 		/* Expect one if we send 2 WAKEs */
816 		BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
817 		       qca->tx_ibs_state);
818 		break;
819 
820 	case HCI_IBS_TX_WAKING:
821 		/* Send pending packets */
822 		while ((skb = skb_dequeue(&qca->tx_wait_q)))
823 			skb_queue_tail(&qca->txq, skb);
824 
825 		/* Switch timers and change state to HCI_IBS_TX_AWAKE */
826 		del_timer(&qca->wake_retrans_timer);
827 		idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
828 		mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
829 		qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
830 		break;
831 
832 	case HCI_IBS_TX_ASLEEP:
833 		/* Fall through */
834 
835 	default:
836 		BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
837 		       qca->tx_ibs_state);
838 		break;
839 	}
840 
841 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
842 
843 	/* Actually send the packets */
844 	hci_uart_tx_wakeup(hu);
845 }
846 
847 /* Enqueue frame for transmittion (padding, crc, etc) may be called from
848  * two simultaneous tasklets.
849  */
850 static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
851 {
852 	unsigned long flags = 0, idle_delay;
853 	struct qca_data *qca = hu->priv;
854 
855 	BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
856 	       qca->tx_ibs_state);
857 
858 	/* Prepend skb with frame type */
859 	memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
860 
861 	spin_lock_irqsave(&qca->hci_ibs_lock, flags);
862 
863 	/* Don't go to sleep in middle of patch download or
864 	 * Out-Of-Band(GPIOs control) sleep is selected.
865 	 * Don't wake the device up when suspending.
866 	 */
867 	if (!test_bit(QCA_IBS_ENABLED, &qca->flags) ||
868 	    test_bit(QCA_SUSPENDING, &qca->flags)) {
869 		skb_queue_tail(&qca->txq, skb);
870 		spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
871 		return 0;
872 	}
873 
874 	/* Act according to current state */
875 	switch (qca->tx_ibs_state) {
876 	case HCI_IBS_TX_AWAKE:
877 		BT_DBG("Device awake, sending normally");
878 		skb_queue_tail(&qca->txq, skb);
879 		idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
880 		mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
881 		break;
882 
883 	case HCI_IBS_TX_ASLEEP:
884 		BT_DBG("Device asleep, waking up and queueing packet");
885 		/* Save packet for later */
886 		skb_queue_tail(&qca->tx_wait_q, skb);
887 
888 		qca->tx_ibs_state = HCI_IBS_TX_WAKING;
889 		/* Schedule a work queue to wake up device */
890 		queue_work(qca->workqueue, &qca->ws_awake_device);
891 		break;
892 
893 	case HCI_IBS_TX_WAKING:
894 		BT_DBG("Device waking up, queueing packet");
895 		/* Transient state; just keep packet for later */
896 		skb_queue_tail(&qca->tx_wait_q, skb);
897 		break;
898 
899 	default:
900 		BT_ERR("Illegal tx state: %d (losing packet)",
901 		       qca->tx_ibs_state);
902 		kfree_skb(skb);
903 		break;
904 	}
905 
906 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
907 
908 	return 0;
909 }
910 
911 static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
912 {
913 	struct hci_uart *hu = hci_get_drvdata(hdev);
914 
915 	BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
916 
917 	device_want_to_sleep(hu);
918 
919 	kfree_skb(skb);
920 	return 0;
921 }
922 
923 static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
924 {
925 	struct hci_uart *hu = hci_get_drvdata(hdev);
926 
927 	BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
928 
929 	device_want_to_wakeup(hu);
930 
931 	kfree_skb(skb);
932 	return 0;
933 }
934 
935 static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
936 {
937 	struct hci_uart *hu = hci_get_drvdata(hdev);
938 
939 	BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
940 
941 	device_woke_up(hu);
942 
943 	kfree_skb(skb);
944 	return 0;
945 }
946 
947 static int qca_recv_acl_data(struct hci_dev *hdev, struct sk_buff *skb)
948 {
949 	/* We receive debug logs from chip as an ACL packets.
950 	 * Instead of sending the data to ACL to decode the
951 	 * received data, we are pushing them to the above layers
952 	 * as a diagnostic packet.
953 	 */
954 	if (get_unaligned_le16(skb->data) == QCA_DEBUG_HANDLE)
955 		return hci_recv_diag(hdev, skb);
956 
957 	return hci_recv_frame(hdev, skb);
958 }
959 
960 static void qca_controller_memdump(struct work_struct *work)
961 {
962 	struct qca_data *qca = container_of(work, struct qca_data,
963 					    ctrl_memdump_evt);
964 	struct hci_uart *hu = qca->hu;
965 	struct sk_buff *skb;
966 	struct qca_memdump_event_hdr *cmd_hdr;
967 	struct qca_memdump_data *qca_memdump = qca->qca_memdump;
968 	struct qca_dump_size *dump;
969 	char *memdump_buf;
970 	char nullBuff[QCA_DUMP_PACKET_SIZE] = { 0 };
971 	u16 seq_no;
972 	u32 dump_size;
973 
974 	while ((skb = skb_dequeue(&qca->rx_memdump_q))) {
975 
976 		mutex_lock(&qca->hci_memdump_lock);
977 		/* Skip processing the received packets if timeout detected. */
978 		if (qca->memdump_state == QCA_MEMDUMP_TIMEOUT) {
979 			mutex_unlock(&qca->hci_memdump_lock);
980 			return;
981 		}
982 
983 		if (!qca_memdump) {
984 			qca_memdump = kzalloc(sizeof(struct qca_memdump_data),
985 					      GFP_ATOMIC);
986 			if (!qca_memdump) {
987 				mutex_unlock(&qca->hci_memdump_lock);
988 				return;
989 			}
990 
991 			qca->qca_memdump = qca_memdump;
992 		}
993 
994 		qca->memdump_state = QCA_MEMDUMP_COLLECTING;
995 		cmd_hdr = (void *) skb->data;
996 		seq_no = __le16_to_cpu(cmd_hdr->seq_no);
997 		skb_pull(skb, sizeof(struct qca_memdump_event_hdr));
998 
999 		if (!seq_no) {
1000 
1001 			/* This is the first frame of memdump packet from
1002 			 * the controller, Disable IBS to recevie dump
1003 			 * with out any interruption, ideally time required for
1004 			 * the controller to send the dump is 8 seconds. let us
1005 			 * start timer to handle this asynchronous activity.
1006 			 */
1007 			clear_bit(QCA_IBS_ENABLED, &qca->flags);
1008 			set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1009 			dump = (void *) skb->data;
1010 			dump_size = __le32_to_cpu(dump->dump_size);
1011 			if (!(dump_size)) {
1012 				bt_dev_err(hu->hdev, "Rx invalid memdump size");
1013 				kfree_skb(skb);
1014 				mutex_unlock(&qca->hci_memdump_lock);
1015 				return;
1016 			}
1017 
1018 			bt_dev_info(hu->hdev, "QCA collecting dump of size:%u",
1019 				    dump_size);
1020 			queue_delayed_work(qca->workqueue,
1021 					   &qca->ctrl_memdump_timeout,
1022 					msecs_to_jiffies(MEMDUMP_TIMEOUT_MS));
1023 
1024 			skb_pull(skb, sizeof(dump_size));
1025 			memdump_buf = vmalloc(dump_size);
1026 			qca_memdump->memdump_buf_head = memdump_buf;
1027 			qca_memdump->memdump_buf_tail = memdump_buf;
1028 		}
1029 
1030 		memdump_buf = qca_memdump->memdump_buf_tail;
1031 
1032 		/* If sequence no 0 is missed then there is no point in
1033 		 * accepting the other sequences.
1034 		 */
1035 		if (!memdump_buf) {
1036 			bt_dev_err(hu->hdev, "QCA: Discarding other packets");
1037 			kfree(qca_memdump);
1038 			kfree_skb(skb);
1039 			qca->qca_memdump = NULL;
1040 			mutex_unlock(&qca->hci_memdump_lock);
1041 			return;
1042 		}
1043 
1044 		/* There could be chance of missing some packets from
1045 		 * the controller. In such cases let us store the dummy
1046 		 * packets in the buffer.
1047 		 */
1048 		while ((seq_no > qca_memdump->current_seq_no + 1) &&
1049 			seq_no != QCA_LAST_SEQUENCE_NUM) {
1050 			bt_dev_err(hu->hdev, "QCA controller missed packet:%d",
1051 				   qca_memdump->current_seq_no);
1052 			memcpy(memdump_buf, nullBuff, QCA_DUMP_PACKET_SIZE);
1053 			memdump_buf = memdump_buf + QCA_DUMP_PACKET_SIZE;
1054 			qca_memdump->received_dump += QCA_DUMP_PACKET_SIZE;
1055 			qca_memdump->current_seq_no++;
1056 		}
1057 
1058 		memcpy(memdump_buf, (unsigned char *) skb->data, skb->len);
1059 		memdump_buf = memdump_buf + skb->len;
1060 		qca_memdump->memdump_buf_tail = memdump_buf;
1061 		qca_memdump->current_seq_no = seq_no + 1;
1062 		qca_memdump->received_dump += skb->len;
1063 		qca->qca_memdump = qca_memdump;
1064 		kfree_skb(skb);
1065 		if (seq_no == QCA_LAST_SEQUENCE_NUM) {
1066 			bt_dev_info(hu->hdev, "QCA writing crash dump of size %d bytes",
1067 				   qca_memdump->received_dump);
1068 			memdump_buf = qca_memdump->memdump_buf_head;
1069 			dev_coredumpv(&hu->serdev->dev, memdump_buf,
1070 				      qca_memdump->received_dump, GFP_KERNEL);
1071 			cancel_delayed_work(&qca->ctrl_memdump_timeout);
1072 			kfree(qca->qca_memdump);
1073 			qca->qca_memdump = NULL;
1074 			qca->memdump_state = QCA_MEMDUMP_COLLECTED;
1075 			clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1076 		}
1077 
1078 		mutex_unlock(&qca->hci_memdump_lock);
1079 	}
1080 
1081 }
1082 
1083 static int qca_controller_memdump_event(struct hci_dev *hdev,
1084 					struct sk_buff *skb)
1085 {
1086 	struct hci_uart *hu = hci_get_drvdata(hdev);
1087 	struct qca_data *qca = hu->priv;
1088 
1089 	skb_queue_tail(&qca->rx_memdump_q, skb);
1090 	queue_work(qca->workqueue, &qca->ctrl_memdump_evt);
1091 
1092 	return 0;
1093 }
1094 
1095 static int qca_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1096 {
1097 	struct hci_uart *hu = hci_get_drvdata(hdev);
1098 	struct qca_data *qca = hu->priv;
1099 
1100 	if (test_bit(QCA_DROP_VENDOR_EVENT, &qca->flags)) {
1101 		struct hci_event_hdr *hdr = (void *)skb->data;
1102 
1103 		/* For the WCN3990 the vendor command for a baudrate change
1104 		 * isn't sent as synchronous HCI command, because the
1105 		 * controller sends the corresponding vendor event with the
1106 		 * new baudrate. The event is received and properly decoded
1107 		 * after changing the baudrate of the host port. It needs to
1108 		 * be dropped, otherwise it can be misinterpreted as
1109 		 * response to a later firmware download command (also a
1110 		 * vendor command).
1111 		 */
1112 
1113 		if (hdr->evt == HCI_EV_VENDOR)
1114 			complete(&qca->drop_ev_comp);
1115 
1116 		kfree_skb(skb);
1117 
1118 		return 0;
1119 	}
1120 	/* We receive chip memory dump as an event packet, With a dedicated
1121 	 * handler followed by a hardware error event. When this event is
1122 	 * received we store dump into a file before closing hci. This
1123 	 * dump will help in triaging the issues.
1124 	 */
1125 	if ((skb->data[0] == HCI_VENDOR_PKT) &&
1126 	    (get_unaligned_be16(skb->data + 2) == QCA_SSR_DUMP_HANDLE))
1127 		return qca_controller_memdump_event(hdev, skb);
1128 
1129 	return hci_recv_frame(hdev, skb);
1130 }
1131 
1132 #define QCA_IBS_SLEEP_IND_EVENT \
1133 	.type = HCI_IBS_SLEEP_IND, \
1134 	.hlen = 0, \
1135 	.loff = 0, \
1136 	.lsize = 0, \
1137 	.maxlen = HCI_MAX_IBS_SIZE
1138 
1139 #define QCA_IBS_WAKE_IND_EVENT \
1140 	.type = HCI_IBS_WAKE_IND, \
1141 	.hlen = 0, \
1142 	.loff = 0, \
1143 	.lsize = 0, \
1144 	.maxlen = HCI_MAX_IBS_SIZE
1145 
1146 #define QCA_IBS_WAKE_ACK_EVENT \
1147 	.type = HCI_IBS_WAKE_ACK, \
1148 	.hlen = 0, \
1149 	.loff = 0, \
1150 	.lsize = 0, \
1151 	.maxlen = HCI_MAX_IBS_SIZE
1152 
1153 static const struct h4_recv_pkt qca_recv_pkts[] = {
1154 	{ H4_RECV_ACL,             .recv = qca_recv_acl_data },
1155 	{ H4_RECV_SCO,             .recv = hci_recv_frame    },
1156 	{ H4_RECV_EVENT,           .recv = qca_recv_event    },
1157 	{ QCA_IBS_WAKE_IND_EVENT,  .recv = qca_ibs_wake_ind  },
1158 	{ QCA_IBS_WAKE_ACK_EVENT,  .recv = qca_ibs_wake_ack  },
1159 	{ QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
1160 };
1161 
1162 static int qca_recv(struct hci_uart *hu, const void *data, int count)
1163 {
1164 	struct qca_data *qca = hu->priv;
1165 
1166 	if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1167 		return -EUNATCH;
1168 
1169 	qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
1170 				  qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
1171 	if (IS_ERR(qca->rx_skb)) {
1172 		int err = PTR_ERR(qca->rx_skb);
1173 		bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
1174 		qca->rx_skb = NULL;
1175 		return err;
1176 	}
1177 
1178 	return count;
1179 }
1180 
1181 static struct sk_buff *qca_dequeue(struct hci_uart *hu)
1182 {
1183 	struct qca_data *qca = hu->priv;
1184 
1185 	return skb_dequeue(&qca->txq);
1186 }
1187 
1188 static uint8_t qca_get_baudrate_value(int speed)
1189 {
1190 	switch (speed) {
1191 	case 9600:
1192 		return QCA_BAUDRATE_9600;
1193 	case 19200:
1194 		return QCA_BAUDRATE_19200;
1195 	case 38400:
1196 		return QCA_BAUDRATE_38400;
1197 	case 57600:
1198 		return QCA_BAUDRATE_57600;
1199 	case 115200:
1200 		return QCA_BAUDRATE_115200;
1201 	case 230400:
1202 		return QCA_BAUDRATE_230400;
1203 	case 460800:
1204 		return QCA_BAUDRATE_460800;
1205 	case 500000:
1206 		return QCA_BAUDRATE_500000;
1207 	case 921600:
1208 		return QCA_BAUDRATE_921600;
1209 	case 1000000:
1210 		return QCA_BAUDRATE_1000000;
1211 	case 2000000:
1212 		return QCA_BAUDRATE_2000000;
1213 	case 3000000:
1214 		return QCA_BAUDRATE_3000000;
1215 	case 3200000:
1216 		return QCA_BAUDRATE_3200000;
1217 	case 3500000:
1218 		return QCA_BAUDRATE_3500000;
1219 	default:
1220 		return QCA_BAUDRATE_115200;
1221 	}
1222 }
1223 
1224 static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
1225 {
1226 	struct hci_uart *hu = hci_get_drvdata(hdev);
1227 	struct qca_data *qca = hu->priv;
1228 	struct sk_buff *skb;
1229 	u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
1230 
1231 	if (baudrate > QCA_BAUDRATE_3200000)
1232 		return -EINVAL;
1233 
1234 	cmd[4] = baudrate;
1235 
1236 	skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1237 	if (!skb) {
1238 		bt_dev_err(hdev, "Failed to allocate baudrate packet");
1239 		return -ENOMEM;
1240 	}
1241 
1242 	/* Assign commands to change baudrate and packet type. */
1243 	skb_put_data(skb, cmd, sizeof(cmd));
1244 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1245 
1246 	skb_queue_tail(&qca->txq, skb);
1247 	hci_uart_tx_wakeup(hu);
1248 
1249 	/* Wait for the baudrate change request to be sent */
1250 
1251 	while (!skb_queue_empty(&qca->txq))
1252 		usleep_range(100, 200);
1253 
1254 	if (hu->serdev)
1255 		serdev_device_wait_until_sent(hu->serdev,
1256 		      msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
1257 
1258 	/* Give the controller time to process the request */
1259 	if (qca_is_wcn399x(qca_soc_type(hu)))
1260 		msleep(10);
1261 	else
1262 		msleep(300);
1263 
1264 	return 0;
1265 }
1266 
1267 static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
1268 {
1269 	if (hu->serdev)
1270 		serdev_device_set_baudrate(hu->serdev, speed);
1271 	else
1272 		hci_uart_set_baudrate(hu, speed);
1273 }
1274 
1275 static int qca_send_power_pulse(struct hci_uart *hu, bool on)
1276 {
1277 	int ret;
1278 	int timeout = msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS);
1279 	u8 cmd = on ? QCA_WCN3990_POWERON_PULSE : QCA_WCN3990_POWEROFF_PULSE;
1280 
1281 	/* These power pulses are single byte command which are sent
1282 	 * at required baudrate to wcn3990. On wcn3990, we have an external
1283 	 * circuit at Tx pin which decodes the pulse sent at specific baudrate.
1284 	 * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
1285 	 * and also we use the same power inputs to turn on and off for
1286 	 * Wi-Fi/BT. Powering up the power sources will not enable BT, until
1287 	 * we send a power on pulse at 115200 bps. This algorithm will help to
1288 	 * save power. Disabling hardware flow control is mandatory while
1289 	 * sending power pulses to SoC.
1290 	 */
1291 	bt_dev_dbg(hu->hdev, "sending power pulse %02x to controller", cmd);
1292 
1293 	serdev_device_write_flush(hu->serdev);
1294 	hci_uart_set_flow_control(hu, true);
1295 	ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1296 	if (ret < 0) {
1297 		bt_dev_err(hu->hdev, "failed to send power pulse %02x", cmd);
1298 		return ret;
1299 	}
1300 
1301 	serdev_device_wait_until_sent(hu->serdev, timeout);
1302 	hci_uart_set_flow_control(hu, false);
1303 
1304 	/* Give to controller time to boot/shutdown */
1305 	if (on)
1306 		msleep(100);
1307 	else
1308 		msleep(10);
1309 
1310 	return 0;
1311 }
1312 
1313 static unsigned int qca_get_speed(struct hci_uart *hu,
1314 				  enum qca_speed_type speed_type)
1315 {
1316 	unsigned int speed = 0;
1317 
1318 	if (speed_type == QCA_INIT_SPEED) {
1319 		if (hu->init_speed)
1320 			speed = hu->init_speed;
1321 		else if (hu->proto->init_speed)
1322 			speed = hu->proto->init_speed;
1323 	} else {
1324 		if (hu->oper_speed)
1325 			speed = hu->oper_speed;
1326 		else if (hu->proto->oper_speed)
1327 			speed = hu->proto->oper_speed;
1328 	}
1329 
1330 	return speed;
1331 }
1332 
1333 static int qca_check_speeds(struct hci_uart *hu)
1334 {
1335 	if (qca_is_wcn399x(qca_soc_type(hu))) {
1336 		if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
1337 		    !qca_get_speed(hu, QCA_OPER_SPEED))
1338 			return -EINVAL;
1339 	} else {
1340 		if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
1341 		    !qca_get_speed(hu, QCA_OPER_SPEED))
1342 			return -EINVAL;
1343 	}
1344 
1345 	return 0;
1346 }
1347 
1348 static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
1349 {
1350 	unsigned int speed, qca_baudrate;
1351 	struct qca_data *qca = hu->priv;
1352 	int ret = 0;
1353 
1354 	if (speed_type == QCA_INIT_SPEED) {
1355 		speed = qca_get_speed(hu, QCA_INIT_SPEED);
1356 		if (speed)
1357 			host_set_baudrate(hu, speed);
1358 	} else {
1359 		enum qca_btsoc_type soc_type = qca_soc_type(hu);
1360 
1361 		speed = qca_get_speed(hu, QCA_OPER_SPEED);
1362 		if (!speed)
1363 			return 0;
1364 
1365 		/* Disable flow control for wcn3990 to deassert RTS while
1366 		 * changing the baudrate of chip and host.
1367 		 */
1368 		if (qca_is_wcn399x(soc_type))
1369 			hci_uart_set_flow_control(hu, true);
1370 
1371 		if (soc_type == QCA_WCN3990) {
1372 			reinit_completion(&qca->drop_ev_comp);
1373 			set_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1374 		}
1375 
1376 		qca_baudrate = qca_get_baudrate_value(speed);
1377 		bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
1378 		ret = qca_set_baudrate(hu->hdev, qca_baudrate);
1379 		if (ret)
1380 			goto error;
1381 
1382 		host_set_baudrate(hu, speed);
1383 
1384 error:
1385 		if (qca_is_wcn399x(soc_type))
1386 			hci_uart_set_flow_control(hu, false);
1387 
1388 		if (soc_type == QCA_WCN3990) {
1389 			/* Wait for the controller to send the vendor event
1390 			 * for the baudrate change command.
1391 			 */
1392 			if (!wait_for_completion_timeout(&qca->drop_ev_comp,
1393 						 msecs_to_jiffies(100))) {
1394 				bt_dev_err(hu->hdev,
1395 					   "Failed to change controller baudrate\n");
1396 				ret = -ETIMEDOUT;
1397 			}
1398 
1399 			clear_bit(QCA_DROP_VENDOR_EVENT, &qca->flags);
1400 		}
1401 	}
1402 
1403 	return ret;
1404 }
1405 
1406 static int qca_send_crashbuffer(struct hci_uart *hu)
1407 {
1408 	struct qca_data *qca = hu->priv;
1409 	struct sk_buff *skb;
1410 
1411 	skb = bt_skb_alloc(QCA_CRASHBYTE_PACKET_LEN, GFP_KERNEL);
1412 	if (!skb) {
1413 		bt_dev_err(hu->hdev, "Failed to allocate memory for skb packet");
1414 		return -ENOMEM;
1415 	}
1416 
1417 	/* We forcefully crash the controller, by sending 0xfb byte for
1418 	 * 1024 times. We also might have chance of losing data, To be
1419 	 * on safer side we send 1096 bytes to the SoC.
1420 	 */
1421 	memset(skb_put(skb, QCA_CRASHBYTE_PACKET_LEN), QCA_MEMDUMP_BYTE,
1422 	       QCA_CRASHBYTE_PACKET_LEN);
1423 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1424 	bt_dev_info(hu->hdev, "crash the soc to collect controller dump");
1425 	skb_queue_tail(&qca->txq, skb);
1426 	hci_uart_tx_wakeup(hu);
1427 
1428 	return 0;
1429 }
1430 
1431 static void qca_wait_for_dump_collection(struct hci_dev *hdev)
1432 {
1433 	struct hci_uart *hu = hci_get_drvdata(hdev);
1434 	struct qca_data *qca = hu->priv;
1435 
1436 	wait_on_bit_timeout(&qca->flags, QCA_MEMDUMP_COLLECTION,
1437 			    TASK_UNINTERRUPTIBLE, MEMDUMP_TIMEOUT_MS);
1438 
1439 	clear_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1440 }
1441 
1442 static void qca_hw_error(struct hci_dev *hdev, u8 code)
1443 {
1444 	struct hci_uart *hu = hci_get_drvdata(hdev);
1445 	struct qca_data *qca = hu->priv;
1446 	struct qca_memdump_data *qca_memdump = qca->qca_memdump;
1447 	char *memdump_buf = NULL;
1448 
1449 	set_bit(QCA_HW_ERROR_EVENT, &qca->flags);
1450 	bt_dev_info(hdev, "mem_dump_status: %d", qca->memdump_state);
1451 
1452 	if (qca->memdump_state == QCA_MEMDUMP_IDLE) {
1453 		/* If hardware error event received for other than QCA
1454 		 * soc memory dump event, then we need to crash the SOC
1455 		 * and wait here for 8 seconds to get the dump packets.
1456 		 * This will block main thread to be on hold until we
1457 		 * collect dump.
1458 		 */
1459 		set_bit(QCA_MEMDUMP_COLLECTION, &qca->flags);
1460 		qca_send_crashbuffer(hu);
1461 		qca_wait_for_dump_collection(hdev);
1462 	} else if (qca->memdump_state == QCA_MEMDUMP_COLLECTING) {
1463 		/* Let us wait here until memory dump collected or
1464 		 * memory dump timer expired.
1465 		 */
1466 		bt_dev_info(hdev, "waiting for dump to complete");
1467 		qca_wait_for_dump_collection(hdev);
1468 	}
1469 
1470 	if (qca->memdump_state != QCA_MEMDUMP_COLLECTED) {
1471 		bt_dev_err(hu->hdev, "clearing allocated memory due to memdump timeout");
1472 		mutex_lock(&qca->hci_memdump_lock);
1473 		if (qca_memdump)
1474 			memdump_buf = qca_memdump->memdump_buf_head;
1475 		vfree(memdump_buf);
1476 		kfree(qca_memdump);
1477 		qca->qca_memdump = NULL;
1478 		qca->memdump_state = QCA_MEMDUMP_TIMEOUT;
1479 		cancel_delayed_work(&qca->ctrl_memdump_timeout);
1480 		skb_queue_purge(&qca->rx_memdump_q);
1481 		mutex_unlock(&qca->hci_memdump_lock);
1482 		cancel_work_sync(&qca->ctrl_memdump_evt);
1483 	}
1484 
1485 	clear_bit(QCA_HW_ERROR_EVENT, &qca->flags);
1486 }
1487 
1488 static void qca_cmd_timeout(struct hci_dev *hdev)
1489 {
1490 	struct hci_uart *hu = hci_get_drvdata(hdev);
1491 	struct qca_data *qca = hu->priv;
1492 
1493 	if (qca->memdump_state == QCA_MEMDUMP_IDLE)
1494 		qca_send_crashbuffer(hu);
1495 	else
1496 		bt_dev_info(hdev, "Dump collection is in process");
1497 }
1498 
1499 static int qca_wcn3990_init(struct hci_uart *hu)
1500 {
1501 	struct qca_serdev *qcadev;
1502 	int ret;
1503 
1504 	/* Check for vregs status, may be hci down has turned
1505 	 * off the voltage regulator.
1506 	 */
1507 	qcadev = serdev_device_get_drvdata(hu->serdev);
1508 	if (!qcadev->bt_power->vregs_on) {
1509 		serdev_device_close(hu->serdev);
1510 		ret = qca_regulator_enable(qcadev);
1511 		if (ret)
1512 			return ret;
1513 
1514 		ret = serdev_device_open(hu->serdev);
1515 		if (ret) {
1516 			bt_dev_err(hu->hdev, "failed to open port");
1517 			return ret;
1518 		}
1519 	}
1520 
1521 	/* Forcefully enable wcn3990 to enter in to boot mode. */
1522 	host_set_baudrate(hu, 2400);
1523 	ret = qca_send_power_pulse(hu, false);
1524 	if (ret)
1525 		return ret;
1526 
1527 	qca_set_speed(hu, QCA_INIT_SPEED);
1528 	ret = qca_send_power_pulse(hu, true);
1529 	if (ret)
1530 		return ret;
1531 
1532 	/* Now the device is in ready state to communicate with host.
1533 	 * To sync host with device we need to reopen port.
1534 	 * Without this, we will have RTS and CTS synchronization
1535 	 * issues.
1536 	 */
1537 	serdev_device_close(hu->serdev);
1538 	ret = serdev_device_open(hu->serdev);
1539 	if (ret) {
1540 		bt_dev_err(hu->hdev, "failed to open port");
1541 		return ret;
1542 	}
1543 
1544 	hci_uart_set_flow_control(hu, false);
1545 
1546 	return 0;
1547 }
1548 
1549 static int qca_power_on(struct hci_dev *hdev)
1550 {
1551 	struct hci_uart *hu = hci_get_drvdata(hdev);
1552 	enum qca_btsoc_type soc_type = qca_soc_type(hu);
1553 	struct qca_serdev *qcadev;
1554 	int ret = 0;
1555 
1556 	/* Non-serdev device usually is powered by external power
1557 	 * and don't need additional action in driver for power on
1558 	 */
1559 	if (!hu->serdev)
1560 		return 0;
1561 
1562 	if (qca_is_wcn399x(soc_type)) {
1563 		ret = qca_wcn3990_init(hu);
1564 	} else {
1565 		qcadev = serdev_device_get_drvdata(hu->serdev);
1566 		if (qcadev->bt_en) {
1567 			gpiod_set_value_cansleep(qcadev->bt_en, 1);
1568 			/* Controller needs time to bootup. */
1569 			msleep(150);
1570 		}
1571 	}
1572 
1573 	return ret;
1574 }
1575 
1576 static int qca_setup(struct hci_uart *hu)
1577 {
1578 	struct hci_dev *hdev = hu->hdev;
1579 	struct qca_data *qca = hu->priv;
1580 	unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
1581 	unsigned int retries = 0;
1582 	enum qca_btsoc_type soc_type = qca_soc_type(hu);
1583 	const char *firmware_name = qca_get_firmware_name(hu);
1584 	int ret;
1585 	int soc_ver = 0;
1586 
1587 	ret = qca_check_speeds(hu);
1588 	if (ret)
1589 		return ret;
1590 
1591 	/* Patch downloading has to be done without IBS mode */
1592 	clear_bit(QCA_IBS_ENABLED, &qca->flags);
1593 
1594 	/* Enable controller to do both LE scan and BR/EDR inquiry
1595 	 * simultaneously.
1596 	 */
1597 	set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1598 
1599 	bt_dev_info(hdev, "setting up %s",
1600 		qca_is_wcn399x(soc_type) ? "wcn399x" : "ROME/QCA6390");
1601 
1602 retry:
1603 	ret = qca_power_on(hdev);
1604 	if (ret)
1605 		return ret;
1606 
1607 	if (qca_is_wcn399x(soc_type)) {
1608 		set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
1609 
1610 		ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
1611 		if (ret)
1612 			return ret;
1613 	} else {
1614 		qca_set_speed(hu, QCA_INIT_SPEED);
1615 	}
1616 
1617 	/* Setup user speed if needed */
1618 	speed = qca_get_speed(hu, QCA_OPER_SPEED);
1619 	if (speed) {
1620 		ret = qca_set_speed(hu, QCA_OPER_SPEED);
1621 		if (ret)
1622 			return ret;
1623 
1624 		qca_baudrate = qca_get_baudrate_value(speed);
1625 	}
1626 
1627 	if (!qca_is_wcn399x(soc_type)) {
1628 		/* Get QCA version information */
1629 		ret = qca_read_soc_version(hdev, &soc_ver, soc_type);
1630 		if (ret)
1631 			return ret;
1632 	}
1633 
1634 	bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
1635 	/* Setup patch / NVM configurations */
1636 	ret = qca_uart_setup(hdev, qca_baudrate, soc_type, soc_ver,
1637 			firmware_name);
1638 	if (!ret) {
1639 		set_bit(QCA_IBS_ENABLED, &qca->flags);
1640 		qca_debugfs_init(hdev);
1641 		hu->hdev->hw_error = qca_hw_error;
1642 		hu->hdev->cmd_timeout = qca_cmd_timeout;
1643 	} else if (ret == -ENOENT) {
1644 		/* No patch/nvm-config found, run with original fw/config */
1645 		ret = 0;
1646 	} else if (ret == -EAGAIN) {
1647 		/*
1648 		 * Userspace firmware loader will return -EAGAIN in case no
1649 		 * patch/nvm-config is found, so run with original fw/config.
1650 		 */
1651 		ret = 0;
1652 	} else {
1653 		if (retries < MAX_INIT_RETRIES) {
1654 			qca_power_shutdown(hu);
1655 			if (hu->serdev) {
1656 				serdev_device_close(hu->serdev);
1657 				ret = serdev_device_open(hu->serdev);
1658 				if (ret) {
1659 					bt_dev_err(hdev, "failed to open port");
1660 					return ret;
1661 				}
1662 			}
1663 			retries++;
1664 			goto retry;
1665 		}
1666 	}
1667 
1668 	/* Setup bdaddr */
1669 	if (soc_type == QCA_ROME)
1670 		hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
1671 	else
1672 		hu->hdev->set_bdaddr = qca_set_bdaddr;
1673 
1674 	return ret;
1675 }
1676 
1677 static const struct hci_uart_proto qca_proto = {
1678 	.id		= HCI_UART_QCA,
1679 	.name		= "QCA",
1680 	.manufacturer	= 29,
1681 	.init_speed	= 115200,
1682 	.oper_speed	= 3000000,
1683 	.open		= qca_open,
1684 	.close		= qca_close,
1685 	.flush		= qca_flush,
1686 	.setup		= qca_setup,
1687 	.recv		= qca_recv,
1688 	.enqueue	= qca_enqueue,
1689 	.dequeue	= qca_dequeue,
1690 };
1691 
1692 static const struct qca_vreg_data qca_soc_data_wcn3990 = {
1693 	.soc_type = QCA_WCN3990,
1694 	.vregs = (struct qca_vreg []) {
1695 		{ "vddio", 15000  },
1696 		{ "vddxo", 80000  },
1697 		{ "vddrf", 300000 },
1698 		{ "vddch0", 450000 },
1699 	},
1700 	.num_vregs = 4,
1701 };
1702 
1703 static const struct qca_vreg_data qca_soc_data_wcn3991 = {
1704 	.soc_type = QCA_WCN3991,
1705 	.vregs = (struct qca_vreg []) {
1706 		{ "vddio", 15000  },
1707 		{ "vddxo", 80000  },
1708 		{ "vddrf", 300000 },
1709 		{ "vddch0", 450000 },
1710 	},
1711 	.num_vregs = 4,
1712 };
1713 
1714 static const struct qca_vreg_data qca_soc_data_wcn3998 = {
1715 	.soc_type = QCA_WCN3998,
1716 	.vregs = (struct qca_vreg []) {
1717 		{ "vddio", 10000  },
1718 		{ "vddxo", 80000  },
1719 		{ "vddrf", 300000 },
1720 		{ "vddch0", 450000 },
1721 	},
1722 	.num_vregs = 4,
1723 };
1724 
1725 static const struct qca_vreg_data qca_soc_data_qca6390 = {
1726 	.soc_type = QCA_QCA6390,
1727 	.num_vregs = 0,
1728 };
1729 
1730 static void qca_power_shutdown(struct hci_uart *hu)
1731 {
1732 	struct qca_serdev *qcadev;
1733 	struct qca_data *qca = hu->priv;
1734 	unsigned long flags;
1735 	enum qca_btsoc_type soc_type = qca_soc_type(hu);
1736 
1737 	qcadev = serdev_device_get_drvdata(hu->serdev);
1738 
1739 	/* From this point we go into power off state. But serial port is
1740 	 * still open, stop queueing the IBS data and flush all the buffered
1741 	 * data in skb's.
1742 	 */
1743 	spin_lock_irqsave(&qca->hci_ibs_lock, flags);
1744 	clear_bit(QCA_IBS_ENABLED, &qca->flags);
1745 	qca_flush(hu);
1746 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
1747 
1748 	hu->hdev->hw_error = NULL;
1749 	hu->hdev->cmd_timeout = NULL;
1750 
1751 	/* Non-serdev device usually is powered by external power
1752 	 * and don't need additional action in driver for power down
1753 	 */
1754 	if (!hu->serdev)
1755 		return;
1756 
1757 	if (qca_is_wcn399x(soc_type)) {
1758 		host_set_baudrate(hu, 2400);
1759 		qca_send_power_pulse(hu, false);
1760 		qca_regulator_disable(qcadev);
1761 	} else if (qcadev->bt_en) {
1762 		gpiod_set_value_cansleep(qcadev->bt_en, 0);
1763 	}
1764 }
1765 
1766 static int qca_power_off(struct hci_dev *hdev)
1767 {
1768 	struct hci_uart *hu = hci_get_drvdata(hdev);
1769 	struct qca_data *qca = hu->priv;
1770 	enum qca_btsoc_type soc_type = qca_soc_type(hu);
1771 
1772 	/* Stop sending shutdown command if soc crashes. */
1773 	if (soc_type != QCA_ROME
1774 		&& qca->memdump_state == QCA_MEMDUMP_IDLE) {
1775 		qca_send_pre_shutdown_cmd(hdev);
1776 		usleep_range(8000, 10000);
1777 	}
1778 
1779 	qca->memdump_state = QCA_MEMDUMP_IDLE;
1780 	qca_power_shutdown(hu);
1781 	return 0;
1782 }
1783 
1784 static int qca_regulator_enable(struct qca_serdev *qcadev)
1785 {
1786 	struct qca_power *power = qcadev->bt_power;
1787 	int ret;
1788 
1789 	/* Already enabled */
1790 	if (power->vregs_on)
1791 		return 0;
1792 
1793 	BT_DBG("enabling %d regulators)", power->num_vregs);
1794 
1795 	ret = regulator_bulk_enable(power->num_vregs, power->vreg_bulk);
1796 	if (ret)
1797 		return ret;
1798 
1799 	power->vregs_on = true;
1800 
1801 	ret = clk_prepare_enable(qcadev->susclk);
1802 	if (ret)
1803 		qca_regulator_disable(qcadev);
1804 
1805 	return ret;
1806 }
1807 
1808 static void qca_regulator_disable(struct qca_serdev *qcadev)
1809 {
1810 	struct qca_power *power;
1811 
1812 	if (!qcadev)
1813 		return;
1814 
1815 	power = qcadev->bt_power;
1816 
1817 	/* Already disabled? */
1818 	if (!power->vregs_on)
1819 		return;
1820 
1821 	regulator_bulk_disable(power->num_vregs, power->vreg_bulk);
1822 	power->vregs_on = false;
1823 
1824 	clk_disable_unprepare(qcadev->susclk);
1825 }
1826 
1827 static int qca_init_regulators(struct qca_power *qca,
1828 				const struct qca_vreg *vregs, size_t num_vregs)
1829 {
1830 	struct regulator_bulk_data *bulk;
1831 	int ret;
1832 	int i;
1833 
1834 	bulk = devm_kcalloc(qca->dev, num_vregs, sizeof(*bulk), GFP_KERNEL);
1835 	if (!bulk)
1836 		return -ENOMEM;
1837 
1838 	for (i = 0; i < num_vregs; i++)
1839 		bulk[i].supply = vregs[i].name;
1840 
1841 	ret = devm_regulator_bulk_get(qca->dev, num_vregs, bulk);
1842 	if (ret < 0)
1843 		return ret;
1844 
1845 	for (i = 0; i < num_vregs; i++) {
1846 		ret = regulator_set_load(bulk[i].consumer, vregs[i].load_uA);
1847 		if (ret)
1848 			return ret;
1849 	}
1850 
1851 	qca->vreg_bulk = bulk;
1852 	qca->num_vregs = num_vregs;
1853 
1854 	return 0;
1855 }
1856 
1857 static int qca_serdev_probe(struct serdev_device *serdev)
1858 {
1859 	struct qca_serdev *qcadev;
1860 	struct hci_dev *hdev;
1861 	const struct qca_vreg_data *data;
1862 	int err;
1863 	bool power_ctrl_enabled = true;
1864 
1865 	qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
1866 	if (!qcadev)
1867 		return -ENOMEM;
1868 
1869 	qcadev->serdev_hu.serdev = serdev;
1870 	data = device_get_match_data(&serdev->dev);
1871 	serdev_device_set_drvdata(serdev, qcadev);
1872 	device_property_read_string(&serdev->dev, "firmware-name",
1873 					 &qcadev->firmware_name);
1874 	if (data && qca_is_wcn399x(data->soc_type)) {
1875 		qcadev->btsoc_type = data->soc_type;
1876 		qcadev->bt_power = devm_kzalloc(&serdev->dev,
1877 						sizeof(struct qca_power),
1878 						GFP_KERNEL);
1879 		if (!qcadev->bt_power)
1880 			return -ENOMEM;
1881 
1882 		qcadev->bt_power->dev = &serdev->dev;
1883 		err = qca_init_regulators(qcadev->bt_power, data->vregs,
1884 					  data->num_vregs);
1885 		if (err) {
1886 			BT_ERR("Failed to init regulators:%d", err);
1887 			return err;
1888 		}
1889 
1890 		qcadev->bt_power->vregs_on = false;
1891 
1892 		qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
1893 		if (IS_ERR(qcadev->susclk)) {
1894 			dev_err(&serdev->dev, "failed to acquire clk\n");
1895 			return PTR_ERR(qcadev->susclk);
1896 		}
1897 
1898 		device_property_read_u32(&serdev->dev, "max-speed",
1899 					 &qcadev->oper_speed);
1900 		if (!qcadev->oper_speed)
1901 			BT_DBG("UART will pick default operating speed");
1902 
1903 		err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1904 		if (err) {
1905 			BT_ERR("wcn3990 serdev registration failed");
1906 			return err;
1907 		}
1908 	} else {
1909 		if (data)
1910 			qcadev->btsoc_type = data->soc_type;
1911 		else
1912 			qcadev->btsoc_type = QCA_ROME;
1913 
1914 		qcadev->bt_en = devm_gpiod_get_optional(&serdev->dev, "enable",
1915 					       GPIOD_OUT_LOW);
1916 		if (!qcadev->bt_en) {
1917 			dev_warn(&serdev->dev, "failed to acquire enable gpio\n");
1918 			power_ctrl_enabled = false;
1919 		}
1920 
1921 		qcadev->susclk = devm_clk_get_optional(&serdev->dev, NULL);
1922 		if (!qcadev->susclk) {
1923 			dev_warn(&serdev->dev, "failed to acquire clk\n");
1924 		} else {
1925 			err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
1926 			if (err)
1927 				return err;
1928 
1929 			err = clk_prepare_enable(qcadev->susclk);
1930 			if (err)
1931 				return err;
1932 		}
1933 
1934 		err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
1935 		if (err) {
1936 			BT_ERR("Rome serdev registration failed");
1937 			if (qcadev->susclk)
1938 				clk_disable_unprepare(qcadev->susclk);
1939 			return err;
1940 		}
1941 	}
1942 
1943 	if (power_ctrl_enabled) {
1944 		hdev = qcadev->serdev_hu.hdev;
1945 		set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1946 		hdev->shutdown = qca_power_off;
1947 	}
1948 
1949 	return 0;
1950 }
1951 
1952 static void qca_serdev_remove(struct serdev_device *serdev)
1953 {
1954 	struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
1955 
1956 	if (qca_is_wcn399x(qcadev->btsoc_type))
1957 		qca_power_shutdown(&qcadev->serdev_hu);
1958 	else if (qcadev->susclk)
1959 		clk_disable_unprepare(qcadev->susclk);
1960 
1961 	hci_uart_unregister_device(&qcadev->serdev_hu);
1962 }
1963 
1964 static int __maybe_unused qca_suspend(struct device *dev)
1965 {
1966 	struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
1967 	struct hci_uart *hu = hci_get_drvdata(hdev);
1968 	struct qca_data *qca = hu->priv;
1969 	unsigned long flags;
1970 	int ret = 0;
1971 	u8 cmd;
1972 
1973 	set_bit(QCA_SUSPENDING, &qca->flags);
1974 
1975 	/* Device is downloading patch or doesn't support in-band sleep. */
1976 	if (!test_bit(QCA_IBS_ENABLED, &qca->flags))
1977 		return 0;
1978 
1979 	cancel_work_sync(&qca->ws_awake_device);
1980 	cancel_work_sync(&qca->ws_awake_rx);
1981 
1982 	spin_lock_irqsave_nested(&qca->hci_ibs_lock,
1983 				 flags, SINGLE_DEPTH_NESTING);
1984 
1985 	switch (qca->tx_ibs_state) {
1986 	case HCI_IBS_TX_WAKING:
1987 		del_timer(&qca->wake_retrans_timer);
1988 		/* Fall through */
1989 	case HCI_IBS_TX_AWAKE:
1990 		del_timer(&qca->tx_idle_timer);
1991 
1992 		serdev_device_write_flush(hu->serdev);
1993 		cmd = HCI_IBS_SLEEP_IND;
1994 		ret = serdev_device_write_buf(hu->serdev, &cmd, sizeof(cmd));
1995 
1996 		if (ret < 0) {
1997 			BT_ERR("Failed to send SLEEP to device");
1998 			break;
1999 		}
2000 
2001 		qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
2002 		qca->ibs_sent_slps++;
2003 
2004 		qca_wq_serial_tx_clock_vote_off(&qca->ws_tx_vote_off);
2005 		break;
2006 
2007 	case HCI_IBS_TX_ASLEEP:
2008 		break;
2009 
2010 	default:
2011 		BT_ERR("Spurious tx state %d", qca->tx_ibs_state);
2012 		ret = -EINVAL;
2013 		break;
2014 	}
2015 
2016 	spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
2017 
2018 	if (ret < 0)
2019 		goto error;
2020 
2021 	serdev_device_wait_until_sent(hu->serdev,
2022 				      msecs_to_jiffies(CMD_TRANS_TIMEOUT_MS));
2023 
2024 	/* Wait for HCI_IBS_SLEEP_IND sent by device to indicate its Tx is going
2025 	 * to sleep, so that the packet does not wake the system later.
2026 	 */
2027 
2028 	ret = wait_event_interruptible_timeout(qca->suspend_wait_q,
2029 			qca->rx_ibs_state == HCI_IBS_RX_ASLEEP,
2030 			msecs_to_jiffies(IBS_BTSOC_TX_IDLE_TIMEOUT_MS));
2031 
2032 	if (ret > 0)
2033 		return 0;
2034 
2035 	if (ret == 0)
2036 		ret = -ETIMEDOUT;
2037 
2038 error:
2039 	clear_bit(QCA_SUSPENDING, &qca->flags);
2040 
2041 	return ret;
2042 }
2043 
2044 static int __maybe_unused qca_resume(struct device *dev)
2045 {
2046 	struct hci_dev *hdev = container_of(dev, struct hci_dev, dev);
2047 	struct hci_uart *hu = hci_get_drvdata(hdev);
2048 	struct qca_data *qca = hu->priv;
2049 
2050 	clear_bit(QCA_SUSPENDING, &qca->flags);
2051 
2052 	return 0;
2053 }
2054 
2055 static SIMPLE_DEV_PM_OPS(qca_pm_ops, qca_suspend, qca_resume);
2056 
2057 #ifdef CONFIG_OF
2058 static const struct of_device_id qca_bluetooth_of_match[] = {
2059 	{ .compatible = "qcom,qca6174-bt" },
2060 	{ .compatible = "qcom,qca6390-bt", .data = &qca_soc_data_qca6390},
2061 	{ .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data_wcn3990},
2062 	{ .compatible = "qcom,wcn3991-bt", .data = &qca_soc_data_wcn3991},
2063 	{ .compatible = "qcom,wcn3998-bt", .data = &qca_soc_data_wcn3998},
2064 	{ /* sentinel */ }
2065 };
2066 MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
2067 #endif
2068 
2069 #ifdef CONFIG_ACPI
2070 static const struct acpi_device_id qca_bluetooth_acpi_match[] = {
2071 	{ "QCOM6390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2072 	{ "DLA16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2073 	{ "DLB16390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2074 	{ "DLB26390", (kernel_ulong_t)&qca_soc_data_qca6390 },
2075 	{ },
2076 };
2077 MODULE_DEVICE_TABLE(acpi, qca_bluetooth_acpi_match);
2078 #endif
2079 
2080 
2081 static struct serdev_device_driver qca_serdev_driver = {
2082 	.probe = qca_serdev_probe,
2083 	.remove = qca_serdev_remove,
2084 	.driver = {
2085 		.name = "hci_uart_qca",
2086 		.of_match_table = of_match_ptr(qca_bluetooth_of_match),
2087 		.acpi_match_table = ACPI_PTR(qca_bluetooth_acpi_match),
2088 		.pm = &qca_pm_ops,
2089 	},
2090 };
2091 
2092 int __init qca_init(void)
2093 {
2094 	serdev_device_driver_register(&qca_serdev_driver);
2095 
2096 	return hci_uart_register_proto(&qca_proto);
2097 }
2098 
2099 int __exit qca_deinit(void)
2100 {
2101 	serdev_device_driver_unregister(&qca_serdev_driver);
2102 
2103 	return hci_uart_unregister_proto(&qca_proto);
2104 }
2105