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