xref: /linux/drivers/bluetooth/btmtkuart.c (revision 2634682fdffd9ba6e74b76be8aa91cf8b2e05c41)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 MediaTek Inc.
3 
4 /*
5  * Bluetooth support for MediaTek serial devices
6  *
7  * Author: Sean Wang <sean.wang@mediatek.com>
8  *
9  */
10 
11 #include <asm/unaligned.h>
12 #include <linux/atomic.h>
13 #include <linux/clk.h>
14 #include <linux/firmware.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/iopoll.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/serdev.h>
25 #include <linux/skbuff.h>
26 
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
29 
30 #include "h4_recv.h"
31 
32 #define VERSION "0.2"
33 
34 #define FIRMWARE_MT7622		"mediatek/mt7622pr2h.bin"
35 #define FIRMWARE_MT7663		"mediatek/mt7663pr2h.bin"
36 #define FIRMWARE_MT7668		"mediatek/mt7668pr2h.bin"
37 
38 #define MTK_STP_TLR_SIZE	2
39 
40 #define BTMTKUART_TX_STATE_ACTIVE	1
41 #define BTMTKUART_TX_STATE_WAKEUP	2
42 #define BTMTKUART_TX_WAIT_VND_EVT	3
43 #define BTMTKUART_REQUIRED_WAKEUP	4
44 
45 #define BTMTKUART_FLAG_STANDALONE_HW	 BIT(0)
46 
47 enum {
48 	MTK_WMT_PATCH_DWNLD = 0x1,
49 	MTK_WMT_TEST = 0x2,
50 	MTK_WMT_WAKEUP = 0x3,
51 	MTK_WMT_HIF = 0x4,
52 	MTK_WMT_FUNC_CTRL = 0x6,
53 	MTK_WMT_RST = 0x7,
54 	MTK_WMT_SEMAPHORE = 0x17,
55 };
56 
57 enum {
58 	BTMTK_WMT_INVALID,
59 	BTMTK_WMT_PATCH_UNDONE,
60 	BTMTK_WMT_PATCH_DONE,
61 	BTMTK_WMT_ON_UNDONE,
62 	BTMTK_WMT_ON_DONE,
63 	BTMTK_WMT_ON_PROGRESS,
64 };
65 
66 struct mtk_stp_hdr {
67 	u8	prefix;
68 	__be16	dlen;
69 	u8	cs;
70 } __packed;
71 
72 struct btmtkuart_data {
73 	unsigned int flags;
74 	const char *fwname;
75 };
76 
77 struct mtk_wmt_hdr {
78 	u8	dir;
79 	u8	op;
80 	__le16	dlen;
81 	u8	flag;
82 } __packed;
83 
84 struct mtk_hci_wmt_cmd {
85 	struct mtk_wmt_hdr hdr;
86 	u8 data[256];
87 } __packed;
88 
89 struct btmtk_hci_wmt_evt {
90 	struct hci_event_hdr hhdr;
91 	struct mtk_wmt_hdr whdr;
92 } __packed;
93 
94 struct btmtk_hci_wmt_evt_funcc {
95 	struct btmtk_hci_wmt_evt hwhdr;
96 	__be16 status;
97 } __packed;
98 
99 struct btmtk_tci_sleep {
100 	u8 mode;
101 	__le16 duration;
102 	__le16 host_duration;
103 	u8 host_wakeup_pin;
104 	u8 time_compensation;
105 } __packed;
106 
107 struct btmtk_hci_wmt_params {
108 	u8 op;
109 	u8 flag;
110 	u16 dlen;
111 	const void *data;
112 	u32 *status;
113 };
114 
115 struct btmtkuart_dev {
116 	struct hci_dev *hdev;
117 	struct serdev_device *serdev;
118 
119 	struct clk *clk;
120 	struct clk *osc;
121 	struct regulator *vcc;
122 	struct gpio_desc *reset;
123 	struct gpio_desc *boot;
124 	struct pinctrl *pinctrl;
125 	struct pinctrl_state *pins_runtime;
126 	struct pinctrl_state *pins_boot;
127 	speed_t	desired_speed;
128 	speed_t	curr_speed;
129 
130 	struct work_struct tx_work;
131 	unsigned long tx_state;
132 	struct sk_buff_head txq;
133 
134 	struct sk_buff *rx_skb;
135 	struct sk_buff *evt_skb;
136 
137 	u8	stp_pad[6];
138 	u8	stp_cursor;
139 	u16	stp_dlen;
140 
141 	const struct btmtkuart_data *data;
142 };
143 
144 #define btmtkuart_is_standalone(bdev)	\
145 	((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
146 #define btmtkuart_is_builtin_soc(bdev)	\
147 	!((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
148 
149 static int mtk_hci_wmt_sync(struct hci_dev *hdev,
150 			    struct btmtk_hci_wmt_params *wmt_params)
151 {
152 	struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
153 	struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
154 	u32 hlen, status = BTMTK_WMT_INVALID;
155 	struct btmtk_hci_wmt_evt *wmt_evt;
156 	struct mtk_hci_wmt_cmd wc;
157 	struct mtk_wmt_hdr *hdr;
158 	int err;
159 
160 	hlen = sizeof(*hdr) + wmt_params->dlen;
161 	if (hlen > 255)
162 		return -EINVAL;
163 
164 	hdr = (struct mtk_wmt_hdr *)&wc;
165 	hdr->dir = 1;
166 	hdr->op = wmt_params->op;
167 	hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
168 	hdr->flag = wmt_params->flag;
169 	memcpy(wc.data, wmt_params->data, wmt_params->dlen);
170 
171 	set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
172 
173 	err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
174 	if (err < 0) {
175 		clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
176 		return err;
177 	}
178 
179 	/* The vendor specific WMT commands are all answered by a vendor
180 	 * specific event and will not have the Command Status or Command
181 	 * Complete as with usual HCI command flow control.
182 	 *
183 	 * After sending the command, wait for BTMTKUART_TX_WAIT_VND_EVT
184 	 * state to be cleared. The driver specific event receive routine
185 	 * will clear that state and with that indicate completion of the
186 	 * WMT command.
187 	 */
188 	err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT,
189 				  TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
190 	if (err == -EINTR) {
191 		bt_dev_err(hdev, "Execution of wmt command interrupted");
192 		clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
193 		return err;
194 	}
195 
196 	if (err) {
197 		bt_dev_err(hdev, "Execution of wmt command timed out");
198 		clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
199 		return -ETIMEDOUT;
200 	}
201 
202 	/* Parse and handle the return WMT event */
203 	wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data;
204 	if (wmt_evt->whdr.op != hdr->op) {
205 		bt_dev_err(hdev, "Wrong op received %d expected %d",
206 			   wmt_evt->whdr.op, hdr->op);
207 		err = -EIO;
208 		goto err_free_skb;
209 	}
210 
211 	switch (wmt_evt->whdr.op) {
212 	case MTK_WMT_SEMAPHORE:
213 		if (wmt_evt->whdr.flag == 2)
214 			status = BTMTK_WMT_PATCH_UNDONE;
215 		else
216 			status = BTMTK_WMT_PATCH_DONE;
217 		break;
218 	case MTK_WMT_FUNC_CTRL:
219 		wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
220 		if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
221 			status = BTMTK_WMT_ON_DONE;
222 		else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
223 			status = BTMTK_WMT_ON_PROGRESS;
224 		else
225 			status = BTMTK_WMT_ON_UNDONE;
226 		break;
227 	}
228 
229 	if (wmt_params->status)
230 		*wmt_params->status = status;
231 
232 err_free_skb:
233 	kfree_skb(bdev->evt_skb);
234 	bdev->evt_skb = NULL;
235 
236 	return err;
237 }
238 
239 static int mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
240 {
241 	struct btmtk_hci_wmt_params wmt_params;
242 	const struct firmware *fw;
243 	const u8 *fw_ptr;
244 	size_t fw_size;
245 	int err, dlen;
246 	u8 flag;
247 
248 	err = request_firmware(&fw, fwname, &hdev->dev);
249 	if (err < 0) {
250 		bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
251 		return err;
252 	}
253 
254 	fw_ptr = fw->data;
255 	fw_size = fw->size;
256 
257 	/* The size of patch header is 30 bytes, should be skip */
258 	if (fw_size < 30) {
259 		err = -EINVAL;
260 		goto free_fw;
261 	}
262 
263 	fw_size -= 30;
264 	fw_ptr += 30;
265 	flag = 1;
266 
267 	wmt_params.op = MTK_WMT_PATCH_DWNLD;
268 	wmt_params.status = NULL;
269 
270 	while (fw_size > 0) {
271 		dlen = min_t(int, 250, fw_size);
272 
273 		/* Tell device the position in sequence */
274 		if (fw_size - dlen <= 0)
275 			flag = 3;
276 		else if (fw_size < fw->size - 30)
277 			flag = 2;
278 
279 		wmt_params.flag = flag;
280 		wmt_params.dlen = dlen;
281 		wmt_params.data = fw_ptr;
282 
283 		err = mtk_hci_wmt_sync(hdev, &wmt_params);
284 		if (err < 0) {
285 			bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
286 				   err);
287 			goto free_fw;
288 		}
289 
290 		fw_size -= dlen;
291 		fw_ptr += dlen;
292 	}
293 
294 	wmt_params.op = MTK_WMT_RST;
295 	wmt_params.flag = 4;
296 	wmt_params.dlen = 0;
297 	wmt_params.data = NULL;
298 	wmt_params.status = NULL;
299 
300 	/* Activate funciton the firmware providing to */
301 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
302 	if (err < 0) {
303 		bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
304 		goto free_fw;
305 	}
306 
307 	/* Wait a few moments for firmware activation done */
308 	usleep_range(10000, 12000);
309 
310 free_fw:
311 	release_firmware(fw);
312 	return err;
313 }
314 
315 static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
316 {
317 	struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
318 	struct hci_event_hdr *hdr = (void *)skb->data;
319 	int err;
320 
321 	/* Fix up the vendor event id with 0xff for vendor specific instead
322 	 * of 0xe4 so that event send via monitoring socket can be parsed
323 	 * properly.
324 	 */
325 	if (hdr->evt == 0xe4)
326 		hdr->evt = HCI_EV_VENDOR;
327 
328 	/* When someone waits for the WMT event, the skb is being cloned
329 	 * and being processed the events from there then.
330 	 */
331 	if (test_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) {
332 		bdev->evt_skb = skb_clone(skb, GFP_KERNEL);
333 		if (!bdev->evt_skb) {
334 			err = -ENOMEM;
335 			goto err_out;
336 		}
337 	}
338 
339 	err = hci_recv_frame(hdev, skb);
340 	if (err < 0)
341 		goto err_free_skb;
342 
343 	if (hdr->evt == HCI_EV_VENDOR) {
344 		if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT,
345 				       &bdev->tx_state)) {
346 			/* Barrier to sync with other CPUs */
347 			smp_mb__after_atomic();
348 			wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT);
349 		}
350 	}
351 
352 	return 0;
353 
354 err_free_skb:
355 	kfree_skb(bdev->evt_skb);
356 	bdev->evt_skb = NULL;
357 
358 err_out:
359 	return err;
360 }
361 
362 static const struct h4_recv_pkt mtk_recv_pkts[] = {
363 	{ H4_RECV_ACL,      .recv = hci_recv_frame },
364 	{ H4_RECV_SCO,      .recv = hci_recv_frame },
365 	{ H4_RECV_EVENT,    .recv = btmtkuart_recv_event },
366 };
367 
368 static void btmtkuart_tx_work(struct work_struct *work)
369 {
370 	struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev,
371 						   tx_work);
372 	struct serdev_device *serdev = bdev->serdev;
373 	struct hci_dev *hdev = bdev->hdev;
374 
375 	while (1) {
376 		clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
377 
378 		while (1) {
379 			struct sk_buff *skb = skb_dequeue(&bdev->txq);
380 			int len;
381 
382 			if (!skb)
383 				break;
384 
385 			len = serdev_device_write_buf(serdev, skb->data,
386 						      skb->len);
387 			hdev->stat.byte_tx += len;
388 
389 			skb_pull(skb, len);
390 			if (skb->len > 0) {
391 				skb_queue_head(&bdev->txq, skb);
392 				break;
393 			}
394 
395 			switch (hci_skb_pkt_type(skb)) {
396 			case HCI_COMMAND_PKT:
397 				hdev->stat.cmd_tx++;
398 				break;
399 			case HCI_ACLDATA_PKT:
400 				hdev->stat.acl_tx++;
401 				break;
402 			case HCI_SCODATA_PKT:
403 				hdev->stat.sco_tx++;
404 				break;
405 			}
406 
407 			kfree_skb(skb);
408 		}
409 
410 		if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state))
411 			break;
412 	}
413 
414 	clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state);
415 }
416 
417 static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev)
418 {
419 	if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state))
420 		set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
421 
422 	schedule_work(&bdev->tx_work);
423 }
424 
425 static const unsigned char *
426 mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count,
427 	      int *sz_h4)
428 {
429 	struct mtk_stp_hdr *shdr;
430 
431 	/* The cursor is reset when all the data of STP is consumed out */
432 	if (!bdev->stp_dlen && bdev->stp_cursor >= 6)
433 		bdev->stp_cursor = 0;
434 
435 	/* Filling pad until all STP info is obtained */
436 	while (bdev->stp_cursor < 6 && count > 0) {
437 		bdev->stp_pad[bdev->stp_cursor] = *data;
438 		bdev->stp_cursor++;
439 		data++;
440 		count--;
441 	}
442 
443 	/* Retrieve STP info and have a sanity check */
444 	if (!bdev->stp_dlen && bdev->stp_cursor >= 6) {
445 		shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2];
446 		bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff;
447 
448 		/* Resync STP when unexpected data is being read */
449 		if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) {
450 			bt_dev_err(bdev->hdev, "stp format unexpect (%d, %d)",
451 				   shdr->prefix, bdev->stp_dlen);
452 			bdev->stp_cursor = 2;
453 			bdev->stp_dlen = 0;
454 		}
455 	}
456 
457 	/* Directly quit when there's no data found for H4 can process */
458 	if (count <= 0)
459 		return NULL;
460 
461 	/* Tranlate to how much the size of data H4 can handle so far */
462 	*sz_h4 = min_t(int, count, bdev->stp_dlen);
463 
464 	/* Update the remaining size of STP packet */
465 	bdev->stp_dlen -= *sz_h4;
466 
467 	/* Data points to STP payload which can be handled by H4 */
468 	return data;
469 }
470 
471 static int btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count)
472 {
473 	struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
474 	const unsigned char *p_left = data, *p_h4;
475 	int sz_left = count, sz_h4, adv;
476 	int err;
477 
478 	while (sz_left > 0) {
479 		/*  The serial data received from MT7622 BT controller is
480 		 *  at all time padded around with the STP header and tailer.
481 		 *
482 		 *  A full STP packet is looking like
483 		 *   -----------------------------------
484 		 *  | STP header  |  H:4   | STP tailer |
485 		 *   -----------------------------------
486 		 *  but it doesn't guarantee to contain a full H:4 packet which
487 		 *  means that it's possible for multiple STP packets forms a
488 		 *  full H:4 packet that means extra STP header + length doesn't
489 		 *  indicate a full H:4 frame, things can fragment. Whose length
490 		 *  recorded in STP header just shows up the most length the
491 		 *  H:4 engine can handle currently.
492 		 */
493 
494 		p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4);
495 		if (!p_h4)
496 			break;
497 
498 		adv = p_h4 - p_left;
499 		sz_left -= adv;
500 		p_left += adv;
501 
502 		bdev->rx_skb = h4_recv_buf(bdev->hdev, bdev->rx_skb, p_h4,
503 					   sz_h4, mtk_recv_pkts,
504 					   ARRAY_SIZE(mtk_recv_pkts));
505 		if (IS_ERR(bdev->rx_skb)) {
506 			err = PTR_ERR(bdev->rx_skb);
507 			bt_dev_err(bdev->hdev,
508 				   "Frame reassembly failed (%d)", err);
509 			bdev->rx_skb = NULL;
510 			return err;
511 		}
512 
513 		sz_left -= sz_h4;
514 		p_left += sz_h4;
515 	}
516 
517 	return 0;
518 }
519 
520 static int btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data,
521 				 size_t count)
522 {
523 	struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
524 	int err;
525 
526 	err = btmtkuart_recv(bdev->hdev, data, count);
527 	if (err < 0)
528 		return err;
529 
530 	bdev->hdev->stat.byte_rx += count;
531 
532 	return count;
533 }
534 
535 static void btmtkuart_write_wakeup(struct serdev_device *serdev)
536 {
537 	struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
538 
539 	btmtkuart_tx_wakeup(bdev);
540 }
541 
542 static const struct serdev_device_ops btmtkuart_client_ops = {
543 	.receive_buf = btmtkuart_receive_buf,
544 	.write_wakeup = btmtkuart_write_wakeup,
545 };
546 
547 static int btmtkuart_open(struct hci_dev *hdev)
548 {
549 	struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
550 	struct device *dev;
551 	int err;
552 
553 	err = serdev_device_open(bdev->serdev);
554 	if (err) {
555 		bt_dev_err(hdev, "Unable to open UART device %s",
556 			   dev_name(&bdev->serdev->dev));
557 		goto err_open;
558 	}
559 
560 	if (btmtkuart_is_standalone(bdev)) {
561 		if (bdev->curr_speed != bdev->desired_speed)
562 			err = serdev_device_set_baudrate(bdev->serdev,
563 							 115200);
564 		else
565 			err = serdev_device_set_baudrate(bdev->serdev,
566 							 bdev->desired_speed);
567 
568 		if (err < 0) {
569 			bt_dev_err(hdev, "Unable to set baudrate UART device %s",
570 				   dev_name(&bdev->serdev->dev));
571 			goto  err_serdev_close;
572 		}
573 
574 		serdev_device_set_flow_control(bdev->serdev, false);
575 	}
576 
577 	bdev->stp_cursor = 2;
578 	bdev->stp_dlen = 0;
579 
580 	dev = &bdev->serdev->dev;
581 
582 	/* Enable the power domain and clock the device requires */
583 	pm_runtime_enable(dev);
584 	err = pm_runtime_get_sync(dev);
585 	if (err < 0) {
586 		pm_runtime_put_noidle(dev);
587 		goto err_disable_rpm;
588 	}
589 
590 	err = clk_prepare_enable(bdev->clk);
591 	if (err < 0)
592 		goto err_put_rpm;
593 
594 	return 0;
595 
596 err_put_rpm:
597 	pm_runtime_put_sync(dev);
598 err_disable_rpm:
599 	pm_runtime_disable(dev);
600 err_serdev_close:
601 	serdev_device_close(bdev->serdev);
602 err_open:
603 	return err;
604 }
605 
606 static int btmtkuart_close(struct hci_dev *hdev)
607 {
608 	struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
609 	struct device *dev = &bdev->serdev->dev;
610 
611 	/* Shutdown the clock and power domain the device requires */
612 	clk_disable_unprepare(bdev->clk);
613 	pm_runtime_put_sync(dev);
614 	pm_runtime_disable(dev);
615 
616 	serdev_device_close(bdev->serdev);
617 
618 	return 0;
619 }
620 
621 static int btmtkuart_flush(struct hci_dev *hdev)
622 {
623 	struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
624 
625 	/* Flush any pending characters */
626 	serdev_device_write_flush(bdev->serdev);
627 	skb_queue_purge(&bdev->txq);
628 
629 	cancel_work_sync(&bdev->tx_work);
630 
631 	kfree_skb(bdev->rx_skb);
632 	bdev->rx_skb = NULL;
633 
634 	bdev->stp_cursor = 2;
635 	bdev->stp_dlen = 0;
636 
637 	return 0;
638 }
639 
640 static int btmtkuart_func_query(struct hci_dev *hdev)
641 {
642 	struct btmtk_hci_wmt_params wmt_params;
643 	int status, err;
644 	u8 param = 0;
645 
646 	/* Query whether the function is enabled */
647 	wmt_params.op = MTK_WMT_FUNC_CTRL;
648 	wmt_params.flag = 4;
649 	wmt_params.dlen = sizeof(param);
650 	wmt_params.data = &param;
651 	wmt_params.status = &status;
652 
653 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
654 	if (err < 0) {
655 		bt_dev_err(hdev, "Failed to query function status (%d)", err);
656 		return err;
657 	}
658 
659 	return status;
660 }
661 
662 static int btmtkuart_change_baudrate(struct hci_dev *hdev)
663 {
664 	struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
665 	struct btmtk_hci_wmt_params wmt_params;
666 	__le32 baudrate;
667 	u8 param;
668 	int err;
669 
670 	/* Indicate the device to enter the probe state the host is
671 	 * ready to change a new baudrate.
672 	 */
673 	baudrate = cpu_to_le32(bdev->desired_speed);
674 	wmt_params.op = MTK_WMT_HIF;
675 	wmt_params.flag = 1;
676 	wmt_params.dlen = 4;
677 	wmt_params.data = &baudrate;
678 	wmt_params.status = NULL;
679 
680 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
681 	if (err < 0) {
682 		bt_dev_err(hdev, "Failed to device baudrate (%d)", err);
683 		return err;
684 	}
685 
686 	err = serdev_device_set_baudrate(bdev->serdev,
687 					 bdev->desired_speed);
688 	if (err < 0) {
689 		bt_dev_err(hdev, "Failed to set up host baudrate (%d)",
690 			   err);
691 		return err;
692 	}
693 
694 	serdev_device_set_flow_control(bdev->serdev, false);
695 
696 	/* Send a dummy byte 0xff to activate the new baudrate */
697 	param = 0xff;
698 	err = serdev_device_write_buf(bdev->serdev, &param, sizeof(param));
699 	if (err < 0 || err < sizeof(param))
700 		return err;
701 
702 	serdev_device_wait_until_sent(bdev->serdev, 0);
703 
704 	/* Wait some time for the device changing baudrate done */
705 	usleep_range(20000, 22000);
706 
707 	/* Test the new baudrate */
708 	wmt_params.op = MTK_WMT_TEST;
709 	wmt_params.flag = 7;
710 	wmt_params.dlen = 0;
711 	wmt_params.data = NULL;
712 	wmt_params.status = NULL;
713 
714 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
715 	if (err < 0) {
716 		bt_dev_err(hdev, "Failed to test new baudrate (%d)",
717 			   err);
718 		return err;
719 	}
720 
721 	bdev->curr_speed = bdev->desired_speed;
722 
723 	return 0;
724 }
725 
726 static int btmtkuart_setup(struct hci_dev *hdev)
727 {
728 	struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
729 	struct btmtk_hci_wmt_params wmt_params;
730 	ktime_t calltime, delta, rettime;
731 	struct btmtk_tci_sleep tci_sleep;
732 	unsigned long long duration;
733 	struct sk_buff *skb;
734 	int err, status;
735 	u8 param = 0x1;
736 
737 	calltime = ktime_get();
738 
739 	/* Wakeup MCUSYS is required for certain devices before we start to
740 	 * do any setups.
741 	 */
742 	if (test_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state)) {
743 		wmt_params.op = MTK_WMT_WAKEUP;
744 		wmt_params.flag = 3;
745 		wmt_params.dlen = 0;
746 		wmt_params.data = NULL;
747 		wmt_params.status = NULL;
748 
749 		err = mtk_hci_wmt_sync(hdev, &wmt_params);
750 		if (err < 0) {
751 			bt_dev_err(hdev, "Failed to wakeup the chip (%d)", err);
752 			return err;
753 		}
754 
755 		clear_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
756 	}
757 
758 	if (btmtkuart_is_standalone(bdev))
759 		btmtkuart_change_baudrate(hdev);
760 
761 	/* Query whether the firmware is already download */
762 	wmt_params.op = MTK_WMT_SEMAPHORE;
763 	wmt_params.flag = 1;
764 	wmt_params.dlen = 0;
765 	wmt_params.data = NULL;
766 	wmt_params.status = &status;
767 
768 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
769 	if (err < 0) {
770 		bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
771 		return err;
772 	}
773 
774 	if (status == BTMTK_WMT_PATCH_DONE) {
775 		bt_dev_info(hdev, "Firmware already downloaded");
776 		goto ignore_setup_fw;
777 	}
778 
779 	/* Setup a firmware which the device definitely requires */
780 	err = mtk_setup_firmware(hdev, bdev->data->fwname);
781 	if (err < 0)
782 		return err;
783 
784 ignore_setup_fw:
785 	/* Query whether the device is already enabled */
786 	err = readx_poll_timeout(btmtkuart_func_query, hdev, status,
787 				 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
788 				 2000, 5000000);
789 	/* -ETIMEDOUT happens */
790 	if (err < 0)
791 		return err;
792 
793 	/* The other errors happen in btusb_mtk_func_query */
794 	if (status < 0)
795 		return status;
796 
797 	if (status == BTMTK_WMT_ON_DONE) {
798 		bt_dev_info(hdev, "function already on");
799 		goto ignore_func_on;
800 	}
801 
802 	/* Enable Bluetooth protocol */
803 	wmt_params.op = MTK_WMT_FUNC_CTRL;
804 	wmt_params.flag = 0;
805 	wmt_params.dlen = sizeof(param);
806 	wmt_params.data = &param;
807 	wmt_params.status = NULL;
808 
809 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
810 	if (err < 0) {
811 		bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
812 		return err;
813 	}
814 
815 ignore_func_on:
816 	/* Apply the low power environment setup */
817 	tci_sleep.mode = 0x5;
818 	tci_sleep.duration = cpu_to_le16(0x640);
819 	tci_sleep.host_duration = cpu_to_le16(0x640);
820 	tci_sleep.host_wakeup_pin = 0;
821 	tci_sleep.time_compensation = 0;
822 
823 	skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
824 			     HCI_INIT_TIMEOUT);
825 	if (IS_ERR(skb)) {
826 		err = PTR_ERR(skb);
827 		bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
828 		return err;
829 	}
830 	kfree_skb(skb);
831 
832 	rettime = ktime_get();
833 	delta = ktime_sub(rettime, calltime);
834 	duration = (unsigned long long)ktime_to_ns(delta) >> 10;
835 
836 	bt_dev_info(hdev, "Device setup in %llu usecs", duration);
837 
838 	return 0;
839 }
840 
841 static int btmtkuart_shutdown(struct hci_dev *hdev)
842 {
843 	struct btmtk_hci_wmt_params wmt_params;
844 	u8 param = 0x0;
845 	int err;
846 
847 	/* Disable the device */
848 	wmt_params.op = MTK_WMT_FUNC_CTRL;
849 	wmt_params.flag = 0;
850 	wmt_params.dlen = sizeof(param);
851 	wmt_params.data = &param;
852 	wmt_params.status = NULL;
853 
854 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
855 	if (err < 0) {
856 		bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
857 		return err;
858 	}
859 
860 	return 0;
861 }
862 
863 static int btmtkuart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
864 {
865 	struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
866 	struct mtk_stp_hdr *shdr;
867 	int err, dlen, type = 0;
868 
869 	/* Prepend skb with frame type */
870 	memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
871 
872 	/* Make sure that there is enough rooms for STP header and trailer */
873 	if (unlikely(skb_headroom(skb) < sizeof(*shdr)) ||
874 	    (skb_tailroom(skb) < MTK_STP_TLR_SIZE)) {
875 		err = pskb_expand_head(skb, sizeof(*shdr), MTK_STP_TLR_SIZE,
876 				       GFP_ATOMIC);
877 		if (err < 0)
878 			return err;
879 	}
880 
881 	/* Add the STP header */
882 	dlen = skb->len;
883 	shdr = skb_push(skb, sizeof(*shdr));
884 	shdr->prefix = 0x80;
885 	shdr->dlen = cpu_to_be16((dlen & 0x0fff) | (type << 12));
886 	shdr->cs = 0;		/* MT7622 doesn't care about checksum value */
887 
888 	/* Add the STP trailer */
889 	skb_put_zero(skb, MTK_STP_TLR_SIZE);
890 
891 	skb_queue_tail(&bdev->txq, skb);
892 
893 	btmtkuart_tx_wakeup(bdev);
894 	return 0;
895 }
896 
897 static int btmtkuart_parse_dt(struct serdev_device *serdev)
898 {
899 	struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
900 	struct device_node *node = serdev->dev.of_node;
901 	u32 speed = 921600;
902 	int err;
903 
904 	if (btmtkuart_is_standalone(bdev)) {
905 		of_property_read_u32(node, "current-speed", &speed);
906 
907 		bdev->desired_speed = speed;
908 
909 		bdev->vcc = devm_regulator_get(&serdev->dev, "vcc");
910 		if (IS_ERR(bdev->vcc)) {
911 			err = PTR_ERR(bdev->vcc);
912 			return err;
913 		}
914 
915 		bdev->osc = devm_clk_get_optional(&serdev->dev, "osc");
916 		if (IS_ERR(bdev->osc)) {
917 			err = PTR_ERR(bdev->osc);
918 			return err;
919 		}
920 
921 		bdev->boot = devm_gpiod_get_optional(&serdev->dev, "boot",
922 						     GPIOD_OUT_LOW);
923 		if (IS_ERR(bdev->boot)) {
924 			err = PTR_ERR(bdev->boot);
925 			return err;
926 		}
927 
928 		bdev->pinctrl = devm_pinctrl_get(&serdev->dev);
929 		if (IS_ERR(bdev->pinctrl)) {
930 			err = PTR_ERR(bdev->pinctrl);
931 			return err;
932 		}
933 
934 		bdev->pins_boot = pinctrl_lookup_state(bdev->pinctrl,
935 						       "default");
936 		if (IS_ERR(bdev->pins_boot) && !bdev->boot) {
937 			err = PTR_ERR(bdev->pins_boot);
938 			dev_err(&serdev->dev,
939 				"Should assign RXD to LOW at boot stage\n");
940 			return err;
941 		}
942 
943 		bdev->pins_runtime = pinctrl_lookup_state(bdev->pinctrl,
944 							  "runtime");
945 		if (IS_ERR(bdev->pins_runtime)) {
946 			err = PTR_ERR(bdev->pins_runtime);
947 			return err;
948 		}
949 
950 		bdev->reset = devm_gpiod_get_optional(&serdev->dev, "reset",
951 						      GPIOD_OUT_LOW);
952 		if (IS_ERR(bdev->reset)) {
953 			err = PTR_ERR(bdev->reset);
954 			return err;
955 		}
956 	} else if (btmtkuart_is_builtin_soc(bdev)) {
957 		bdev->clk = devm_clk_get(&serdev->dev, "ref");
958 		if (IS_ERR(bdev->clk))
959 			return PTR_ERR(bdev->clk);
960 	}
961 
962 	return 0;
963 }
964 
965 static int btmtkuart_probe(struct serdev_device *serdev)
966 {
967 	struct btmtkuart_dev *bdev;
968 	struct hci_dev *hdev;
969 	int err;
970 
971 	bdev = devm_kzalloc(&serdev->dev, sizeof(*bdev), GFP_KERNEL);
972 	if (!bdev)
973 		return -ENOMEM;
974 
975 	bdev->data = of_device_get_match_data(&serdev->dev);
976 	if (!bdev->data)
977 		return -ENODEV;
978 
979 	bdev->serdev = serdev;
980 	serdev_device_set_drvdata(serdev, bdev);
981 
982 	serdev_device_set_client_ops(serdev, &btmtkuart_client_ops);
983 
984 	err = btmtkuart_parse_dt(serdev);
985 	if (err < 0)
986 		return err;
987 
988 	INIT_WORK(&bdev->tx_work, btmtkuart_tx_work);
989 	skb_queue_head_init(&bdev->txq);
990 
991 	/* Initialize and register HCI device */
992 	hdev = hci_alloc_dev();
993 	if (!hdev) {
994 		dev_err(&serdev->dev, "Can't allocate HCI device\n");
995 		return -ENOMEM;
996 	}
997 
998 	bdev->hdev = hdev;
999 
1000 	hdev->bus = HCI_UART;
1001 	hci_set_drvdata(hdev, bdev);
1002 
1003 	hdev->open     = btmtkuart_open;
1004 	hdev->close    = btmtkuart_close;
1005 	hdev->flush    = btmtkuart_flush;
1006 	hdev->setup    = btmtkuart_setup;
1007 	hdev->shutdown = btmtkuart_shutdown;
1008 	hdev->send     = btmtkuart_send_frame;
1009 	SET_HCIDEV_DEV(hdev, &serdev->dev);
1010 
1011 	hdev->manufacturer = 70;
1012 	set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1013 
1014 	if (btmtkuart_is_standalone(bdev)) {
1015 		err = clk_prepare_enable(bdev->osc);
1016 		if (err < 0)
1017 			goto err_hci_free_dev;
1018 
1019 		if (bdev->boot) {
1020 			gpiod_set_value_cansleep(bdev->boot, 1);
1021 		} else {
1022 			/* Switch to the specific pin state for the booting
1023 			 * requires.
1024 			 */
1025 			pinctrl_select_state(bdev->pinctrl, bdev->pins_boot);
1026 		}
1027 
1028 		/* Power on */
1029 		err = regulator_enable(bdev->vcc);
1030 		if (err < 0)
1031 			goto err_clk_disable_unprepare;
1032 
1033 		/* Reset if the reset-gpios is available otherwise the board
1034 		 * -level design should be guaranteed.
1035 		 */
1036 		if (bdev->reset) {
1037 			gpiod_set_value_cansleep(bdev->reset, 1);
1038 			usleep_range(1000, 2000);
1039 			gpiod_set_value_cansleep(bdev->reset, 0);
1040 		}
1041 
1042 		/* Wait some time until device got ready and switch to the pin
1043 		 * mode the device requires for UART transfers.
1044 		 */
1045 		msleep(50);
1046 
1047 		if (bdev->boot)
1048 			devm_gpiod_put(&serdev->dev, bdev->boot);
1049 
1050 		pinctrl_select_state(bdev->pinctrl, bdev->pins_runtime);
1051 
1052 		/* A standalone device doesn't depends on power domain on SoC,
1053 		 * so mark it as no callbacks.
1054 		 */
1055 		pm_runtime_no_callbacks(&serdev->dev);
1056 
1057 		set_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
1058 	}
1059 
1060 	err = hci_register_dev(hdev);
1061 	if (err < 0) {
1062 		dev_err(&serdev->dev, "Can't register HCI device\n");
1063 		goto err_regulator_disable;
1064 	}
1065 
1066 	return 0;
1067 
1068 err_regulator_disable:
1069 	if (btmtkuart_is_standalone(bdev))
1070 		regulator_disable(bdev->vcc);
1071 err_clk_disable_unprepare:
1072 	if (btmtkuart_is_standalone(bdev))
1073 		clk_disable_unprepare(bdev->osc);
1074 err_hci_free_dev:
1075 	hci_free_dev(hdev);
1076 
1077 	return err;
1078 }
1079 
1080 static void btmtkuart_remove(struct serdev_device *serdev)
1081 {
1082 	struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
1083 	struct hci_dev *hdev = bdev->hdev;
1084 
1085 	if (btmtkuart_is_standalone(bdev)) {
1086 		regulator_disable(bdev->vcc);
1087 		clk_disable_unprepare(bdev->osc);
1088 	}
1089 
1090 	hci_unregister_dev(hdev);
1091 	hci_free_dev(hdev);
1092 }
1093 
1094 static const struct btmtkuart_data mt7622_data = {
1095 	.fwname = FIRMWARE_MT7622,
1096 };
1097 
1098 static const struct btmtkuart_data mt7663_data = {
1099 	.flags = BTMTKUART_FLAG_STANDALONE_HW,
1100 	.fwname = FIRMWARE_MT7663,
1101 };
1102 
1103 static const struct btmtkuart_data mt7668_data = {
1104 	.flags = BTMTKUART_FLAG_STANDALONE_HW,
1105 	.fwname = FIRMWARE_MT7668,
1106 };
1107 
1108 #ifdef CONFIG_OF
1109 static const struct of_device_id mtk_of_match_table[] = {
1110 	{ .compatible = "mediatek,mt7622-bluetooth", .data = &mt7622_data},
1111 	{ .compatible = "mediatek,mt7663u-bluetooth", .data = &mt7663_data},
1112 	{ .compatible = "mediatek,mt7668u-bluetooth", .data = &mt7668_data},
1113 	{ }
1114 };
1115 MODULE_DEVICE_TABLE(of, mtk_of_match_table);
1116 #endif
1117 
1118 static struct serdev_device_driver btmtkuart_driver = {
1119 	.probe = btmtkuart_probe,
1120 	.remove = btmtkuart_remove,
1121 	.driver = {
1122 		.name = "btmtkuart",
1123 		.of_match_table = of_match_ptr(mtk_of_match_table),
1124 	},
1125 };
1126 
1127 module_serdev_device_driver(btmtkuart_driver);
1128 
1129 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1130 MODULE_DESCRIPTION("MediaTek Bluetooth Serial driver ver " VERSION);
1131 MODULE_VERSION(VERSION);
1132 MODULE_LICENSE("GPL");
1133 MODULE_FIRMWARE(FIRMWARE_MT7622);
1134 MODULE_FIRMWARE(FIRMWARE_MT7663);
1135 MODULE_FIRMWARE(FIRMWARE_MT7668);
1136