xref: /linux/drivers/bluetooth/btmtksdio.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 MediaTek Inc.
3 
4 /*
5  * Bluetooth support for MediaTek SDIO devices
6  *
7  * This file is written based on btsdio.c and btmtkuart.c.
8  *
9  * Author: Sean Wang <sean.wang@mediatek.com>
10  *
11  */
12 
13 #include <asm/unaligned.h>
14 #include <linux/atomic.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/init.h>
17 #include <linux/iopoll.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/skbuff.h>
23 
24 #include <linux/mmc/host.h>
25 #include <linux/mmc/sdio_ids.h>
26 #include <linux/mmc/sdio_func.h>
27 
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
30 
31 #include "h4_recv.h"
32 #include "btmtk.h"
33 
34 #define VERSION "0.1"
35 
36 #define MTKBTSDIO_AUTOSUSPEND_DELAY	1000
37 
38 static bool enable_autosuspend = true;
39 
40 struct btmtksdio_data {
41 	const char *fwname;
42 	u16 chipid;
43 	bool lp_mbox_supported;
44 };
45 
46 static const struct btmtksdio_data mt7663_data = {
47 	.fwname = FIRMWARE_MT7663,
48 	.chipid = 0x7663,
49 	.lp_mbox_supported = false,
50 };
51 
52 static const struct btmtksdio_data mt7668_data = {
53 	.fwname = FIRMWARE_MT7668,
54 	.chipid = 0x7668,
55 	.lp_mbox_supported = false,
56 };
57 
58 static const struct btmtksdio_data mt7921_data = {
59 	.fwname = FIRMWARE_MT7961,
60 	.chipid = 0x7921,
61 	.lp_mbox_supported = true,
62 };
63 
64 static const struct sdio_device_id btmtksdio_table[] = {
65 	{SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, SDIO_DEVICE_ID_MEDIATEK_MT7663),
66 	 .driver_data = (kernel_ulong_t)&mt7663_data },
67 	{SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, SDIO_DEVICE_ID_MEDIATEK_MT7668),
68 	 .driver_data = (kernel_ulong_t)&mt7668_data },
69 	{SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK, SDIO_DEVICE_ID_MEDIATEK_MT7961),
70 	 .driver_data = (kernel_ulong_t)&mt7921_data },
71 	{ }	/* Terminating entry */
72 };
73 MODULE_DEVICE_TABLE(sdio, btmtksdio_table);
74 
75 #define MTK_REG_CHLPCR		0x4	/* W1S */
76 #define C_INT_EN_SET		BIT(0)
77 #define C_INT_EN_CLR		BIT(1)
78 #define C_FW_OWN_REQ_SET	BIT(8)  /* For write */
79 #define C_COM_DRV_OWN		BIT(8)  /* For read */
80 #define C_FW_OWN_REQ_CLR	BIT(9)
81 
82 #define MTK_REG_CSDIOCSR	0x8
83 #define SDIO_RE_INIT_EN		BIT(0)
84 #define SDIO_INT_CTL		BIT(2)
85 
86 #define MTK_REG_CHCR		0xc
87 #define C_INT_CLR_CTRL		BIT(1)
88 #define BT_RST_DONE		BIT(8)
89 
90 /* CHISR have the same bits field definition with CHIER */
91 #define MTK_REG_CHISR		0x10
92 #define MTK_REG_CHIER		0x14
93 #define FW_OWN_BACK_INT		BIT(0)
94 #define RX_DONE_INT		BIT(1)
95 #define TX_EMPTY		BIT(2)
96 #define TX_FIFO_OVERFLOW	BIT(8)
97 #define FW_MAILBOX_INT		BIT(15)
98 #define INT_MASK		GENMASK(15, 0)
99 #define RX_PKT_LEN		GENMASK(31, 16)
100 
101 #define MTK_REG_CSICR		0xc0
102 #define CSICR_CLR_MBOX_ACK BIT(0)
103 #define MTK_REG_PH2DSM0R	0xc4
104 #define PH2DSM0R_DRIVER_OWN	BIT(0)
105 #define MTK_REG_PD2HRM0R	0xdc
106 #define PD2HRM0R_DRV_OWN	BIT(0)
107 
108 #define MTK_REG_CTDR		0x18
109 
110 #define MTK_REG_CRDR		0x1c
111 
112 #define MTK_REG_CRPLR		0x24
113 
114 #define MTK_SDIO_BLOCK_SIZE	256
115 
116 #define BTMTKSDIO_TX_WAIT_VND_EVT	1
117 #define BTMTKSDIO_HW_TX_READY		2
118 #define BTMTKSDIO_FUNC_ENABLED		3
119 #define BTMTKSDIO_PATCH_ENABLED		4
120 #define BTMTKSDIO_HW_RESET_ACTIVE	5
121 
122 struct mtkbtsdio_hdr {
123 	__le16	len;
124 	__le16	reserved;
125 	u8	bt_type;
126 } __packed;
127 
128 struct btmtksdio_dev {
129 	struct hci_dev *hdev;
130 	struct sdio_func *func;
131 	struct device *dev;
132 
133 	struct work_struct txrx_work;
134 	unsigned long tx_state;
135 	struct sk_buff_head txq;
136 
137 	struct sk_buff *evt_skb;
138 
139 	const struct btmtksdio_data *data;
140 
141 	struct gpio_desc *reset;
142 };
143 
144 static int mtk_hci_wmt_sync(struct hci_dev *hdev,
145 			    struct btmtk_hci_wmt_params *wmt_params)
146 {
147 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
148 	struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
149 	struct btmtk_hci_wmt_evt_reg *wmt_evt_reg;
150 	u32 hlen, status = BTMTK_WMT_INVALID;
151 	struct btmtk_hci_wmt_evt *wmt_evt;
152 	struct btmtk_hci_wmt_cmd *wc;
153 	struct btmtk_wmt_hdr *hdr;
154 	int err;
155 
156 	/* Send the WMT command and wait until the WMT event returns */
157 	hlen = sizeof(*hdr) + wmt_params->dlen;
158 	if (hlen > 255)
159 		return -EINVAL;
160 
161 	wc = kzalloc(hlen, GFP_KERNEL);
162 	if (!wc)
163 		return -ENOMEM;
164 
165 	hdr = &wc->hdr;
166 	hdr->dir = 1;
167 	hdr->op = wmt_params->op;
168 	hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
169 	hdr->flag = wmt_params->flag;
170 	memcpy(wc->data, wmt_params->data, wmt_params->dlen);
171 
172 	set_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
173 
174 	err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
175 	if (err < 0) {
176 		clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
177 		goto err_free_wc;
178 	}
179 
180 	/* The vendor specific WMT commands are all answered by a vendor
181 	 * specific event and will not have the Command Status or Command
182 	 * Complete as with usual HCI command flow control.
183 	 *
184 	 * After sending the command, wait for BTMTKSDIO_TX_WAIT_VND_EVT
185 	 * state to be cleared. The driver specific event receive routine
186 	 * will clear that state and with that indicate completion of the
187 	 * WMT command.
188 	 */
189 	err = wait_on_bit_timeout(&bdev->tx_state, BTMTKSDIO_TX_WAIT_VND_EVT,
190 				  TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
191 	if (err == -EINTR) {
192 		bt_dev_err(hdev, "Execution of wmt command interrupted");
193 		clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
194 		goto err_free_wc;
195 	}
196 
197 	if (err) {
198 		bt_dev_err(hdev, "Execution of wmt command timed out");
199 		clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
200 		err = -ETIMEDOUT;
201 		goto err_free_wc;
202 	}
203 
204 	/* Parse and handle the return WMT event */
205 	wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data;
206 	if (wmt_evt->whdr.op != hdr->op) {
207 		bt_dev_err(hdev, "Wrong op received %d expected %d",
208 			   wmt_evt->whdr.op, hdr->op);
209 		err = -EIO;
210 		goto err_free_skb;
211 	}
212 
213 	switch (wmt_evt->whdr.op) {
214 	case BTMTK_WMT_SEMAPHORE:
215 		if (wmt_evt->whdr.flag == 2)
216 			status = BTMTK_WMT_PATCH_UNDONE;
217 		else
218 			status = BTMTK_WMT_PATCH_DONE;
219 		break;
220 	case BTMTK_WMT_FUNC_CTRL:
221 		wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
222 		if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
223 			status = BTMTK_WMT_ON_DONE;
224 		else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
225 			status = BTMTK_WMT_ON_PROGRESS;
226 		else
227 			status = BTMTK_WMT_ON_UNDONE;
228 		break;
229 	case BTMTK_WMT_PATCH_DWNLD:
230 		if (wmt_evt->whdr.flag == 2)
231 			status = BTMTK_WMT_PATCH_DONE;
232 		else if (wmt_evt->whdr.flag == 1)
233 			status = BTMTK_WMT_PATCH_PROGRESS;
234 		else
235 			status = BTMTK_WMT_PATCH_UNDONE;
236 		break;
237 	case BTMTK_WMT_REGISTER:
238 		wmt_evt_reg = (struct btmtk_hci_wmt_evt_reg *)wmt_evt;
239 		if (le16_to_cpu(wmt_evt->whdr.dlen) == 12)
240 			status = le32_to_cpu(wmt_evt_reg->val);
241 		break;
242 	}
243 
244 	if (wmt_params->status)
245 		*wmt_params->status = status;
246 
247 err_free_skb:
248 	kfree_skb(bdev->evt_skb);
249 	bdev->evt_skb = NULL;
250 err_free_wc:
251 	kfree(wc);
252 
253 	return err;
254 }
255 
256 static int btmtksdio_tx_packet(struct btmtksdio_dev *bdev,
257 			       struct sk_buff *skb)
258 {
259 	struct mtkbtsdio_hdr *sdio_hdr;
260 	int err;
261 
262 	/* Make sure that there are enough rooms for SDIO header */
263 	if (unlikely(skb_headroom(skb) < sizeof(*sdio_hdr))) {
264 		err = pskb_expand_head(skb, sizeof(*sdio_hdr), 0,
265 				       GFP_ATOMIC);
266 		if (err < 0)
267 			return err;
268 	}
269 
270 	/* Prepend MediaTek SDIO Specific Header */
271 	skb_push(skb, sizeof(*sdio_hdr));
272 
273 	sdio_hdr = (void *)skb->data;
274 	sdio_hdr->len = cpu_to_le16(skb->len);
275 	sdio_hdr->reserved = cpu_to_le16(0);
276 	sdio_hdr->bt_type = hci_skb_pkt_type(skb);
277 
278 	clear_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state);
279 	err = sdio_writesb(bdev->func, MTK_REG_CTDR, skb->data,
280 			   round_up(skb->len, MTK_SDIO_BLOCK_SIZE));
281 	if (err < 0)
282 		goto err_skb_pull;
283 
284 	bdev->hdev->stat.byte_tx += skb->len;
285 
286 	kfree_skb(skb);
287 
288 	return 0;
289 
290 err_skb_pull:
291 	skb_pull(skb, sizeof(*sdio_hdr));
292 
293 	return err;
294 }
295 
296 static u32 btmtksdio_drv_own_query(struct btmtksdio_dev *bdev)
297 {
298 	return sdio_readl(bdev->func, MTK_REG_CHLPCR, NULL);
299 }
300 
301 static u32 btmtksdio_drv_own_query_79xx(struct btmtksdio_dev *bdev)
302 {
303 	return sdio_readl(bdev->func, MTK_REG_PD2HRM0R, NULL);
304 }
305 
306 static u32 btmtksdio_chcr_query(struct btmtksdio_dev *bdev)
307 {
308 	return sdio_readl(bdev->func, MTK_REG_CHCR, NULL);
309 }
310 
311 static int btmtksdio_fw_pmctrl(struct btmtksdio_dev *bdev)
312 {
313 	u32 status;
314 	int err;
315 
316 	sdio_claim_host(bdev->func);
317 
318 	if (bdev->data->lp_mbox_supported &&
319 	    test_bit(BTMTKSDIO_PATCH_ENABLED, &bdev->tx_state)) {
320 		sdio_writel(bdev->func, CSICR_CLR_MBOX_ACK, MTK_REG_CSICR,
321 			    &err);
322 		err = readx_poll_timeout(btmtksdio_drv_own_query_79xx, bdev,
323 					 status, !(status & PD2HRM0R_DRV_OWN),
324 					 2000, 1000000);
325 		if (err < 0) {
326 			bt_dev_err(bdev->hdev, "mailbox ACK not cleared");
327 			goto out;
328 		}
329 	}
330 
331 	/* Return ownership to the device */
332 	sdio_writel(bdev->func, C_FW_OWN_REQ_SET, MTK_REG_CHLPCR, &err);
333 	if (err < 0)
334 		goto out;
335 
336 	err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status,
337 				 !(status & C_COM_DRV_OWN), 2000, 1000000);
338 
339 out:
340 	sdio_release_host(bdev->func);
341 
342 	if (err < 0)
343 		bt_dev_err(bdev->hdev, "Cannot return ownership to device");
344 
345 	return err;
346 }
347 
348 static int btmtksdio_drv_pmctrl(struct btmtksdio_dev *bdev)
349 {
350 	u32 status;
351 	int err;
352 
353 	sdio_claim_host(bdev->func);
354 
355 	/* Get ownership from the device */
356 	sdio_writel(bdev->func, C_FW_OWN_REQ_CLR, MTK_REG_CHLPCR, &err);
357 	if (err < 0)
358 		goto out;
359 
360 	err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status,
361 				 status & C_COM_DRV_OWN, 2000, 1000000);
362 
363 	if (!err && bdev->data->lp_mbox_supported &&
364 	    test_bit(BTMTKSDIO_PATCH_ENABLED, &bdev->tx_state))
365 		err = readx_poll_timeout(btmtksdio_drv_own_query_79xx, bdev,
366 					 status, status & PD2HRM0R_DRV_OWN,
367 					 2000, 1000000);
368 
369 out:
370 	sdio_release_host(bdev->func);
371 
372 	if (err < 0)
373 		bt_dev_err(bdev->hdev, "Cannot get ownership from device");
374 
375 	return err;
376 }
377 
378 static int btmtksdio_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
379 {
380 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
381 	struct hci_event_hdr *hdr = (void *)skb->data;
382 	int err;
383 
384 	/* When someone waits for the WMT event, the skb is being cloned
385 	 * and being processed the events from there then.
386 	 */
387 	if (test_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state)) {
388 		bdev->evt_skb = skb_clone(skb, GFP_KERNEL);
389 		if (!bdev->evt_skb) {
390 			err = -ENOMEM;
391 			goto err_out;
392 		}
393 	}
394 
395 	err = hci_recv_frame(hdev, skb);
396 	if (err < 0)
397 		goto err_free_skb;
398 
399 	if (hdr->evt == HCI_EV_WMT) {
400 		if (test_and_clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT,
401 				       &bdev->tx_state)) {
402 			/* Barrier to sync with other CPUs */
403 			smp_mb__after_atomic();
404 			wake_up_bit(&bdev->tx_state, BTMTKSDIO_TX_WAIT_VND_EVT);
405 		}
406 	}
407 
408 	return 0;
409 
410 err_free_skb:
411 	kfree_skb(bdev->evt_skb);
412 	bdev->evt_skb = NULL;
413 
414 err_out:
415 	return err;
416 }
417 
418 static int btmtksdio_recv_acl(struct hci_dev *hdev, struct sk_buff *skb)
419 {
420 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
421 	u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
422 
423 	switch (handle) {
424 	case 0xfc6f:
425 		/* Firmware dump from device: when the firmware hangs, the
426 		 * device can no longer suspend and thus disable auto-suspend.
427 		 */
428 		pm_runtime_forbid(bdev->dev);
429 		fallthrough;
430 	case 0x05ff:
431 	case 0x05fe:
432 		/* Firmware debug logging */
433 		return hci_recv_diag(hdev, skb);
434 	}
435 
436 	return hci_recv_frame(hdev, skb);
437 }
438 
439 static const struct h4_recv_pkt mtk_recv_pkts[] = {
440 	{ H4_RECV_ACL,      .recv = btmtksdio_recv_acl },
441 	{ H4_RECV_SCO,      .recv = hci_recv_frame },
442 	{ H4_RECV_EVENT,    .recv = btmtksdio_recv_event },
443 };
444 
445 static int btmtksdio_rx_packet(struct btmtksdio_dev *bdev, u16 rx_size)
446 {
447 	const struct h4_recv_pkt *pkts = mtk_recv_pkts;
448 	int pkts_count = ARRAY_SIZE(mtk_recv_pkts);
449 	struct mtkbtsdio_hdr *sdio_hdr;
450 	int err, i, pad_size;
451 	struct sk_buff *skb;
452 	u16 dlen;
453 
454 	if (rx_size < sizeof(*sdio_hdr))
455 		return -EILSEQ;
456 
457 	/* A SDIO packet is exactly containing a Bluetooth packet */
458 	skb = bt_skb_alloc(rx_size, GFP_KERNEL);
459 	if (!skb)
460 		return -ENOMEM;
461 
462 	skb_put(skb, rx_size);
463 
464 	err = sdio_readsb(bdev->func, skb->data, MTK_REG_CRDR, rx_size);
465 	if (err < 0)
466 		goto err_kfree_skb;
467 
468 	sdio_hdr = (void *)skb->data;
469 
470 	/* We assume the default error as -EILSEQ simply to make the error path
471 	 * be cleaner.
472 	 */
473 	err = -EILSEQ;
474 
475 	if (rx_size != le16_to_cpu(sdio_hdr->len)) {
476 		bt_dev_err(bdev->hdev, "Rx size in sdio header is mismatched ");
477 		goto err_kfree_skb;
478 	}
479 
480 	hci_skb_pkt_type(skb) = sdio_hdr->bt_type;
481 
482 	/* Remove MediaTek SDIO header */
483 	skb_pull(skb, sizeof(*sdio_hdr));
484 
485 	/* We have to dig into the packet to get payload size and then know how
486 	 * many padding bytes at the tail, these padding bytes should be removed
487 	 * before the packet is indicated to the core layer.
488 	 */
489 	for (i = 0; i < pkts_count; i++) {
490 		if (sdio_hdr->bt_type == (&pkts[i])->type)
491 			break;
492 	}
493 
494 	if (i >= pkts_count) {
495 		bt_dev_err(bdev->hdev, "Invalid bt type 0x%02x",
496 			   sdio_hdr->bt_type);
497 		goto err_kfree_skb;
498 	}
499 
500 	/* Remaining bytes cannot hold a header*/
501 	if (skb->len < (&pkts[i])->hlen) {
502 		bt_dev_err(bdev->hdev, "The size of bt header is mismatched");
503 		goto err_kfree_skb;
504 	}
505 
506 	switch ((&pkts[i])->lsize) {
507 	case 1:
508 		dlen = skb->data[(&pkts[i])->loff];
509 		break;
510 	case 2:
511 		dlen = get_unaligned_le16(skb->data +
512 						  (&pkts[i])->loff);
513 		break;
514 	default:
515 		goto err_kfree_skb;
516 	}
517 
518 	pad_size = skb->len - (&pkts[i])->hlen -  dlen;
519 
520 	/* Remaining bytes cannot hold a payload */
521 	if (pad_size < 0) {
522 		bt_dev_err(bdev->hdev, "The size of bt payload is mismatched");
523 		goto err_kfree_skb;
524 	}
525 
526 	/* Remove padding bytes */
527 	skb_trim(skb, skb->len - pad_size);
528 
529 	/* Complete frame */
530 	(&pkts[i])->recv(bdev->hdev, skb);
531 
532 	bdev->hdev->stat.byte_rx += rx_size;
533 
534 	return 0;
535 
536 err_kfree_skb:
537 	kfree_skb(skb);
538 
539 	return err;
540 }
541 
542 static void btmtksdio_txrx_work(struct work_struct *work)
543 {
544 	struct btmtksdio_dev *bdev = container_of(work, struct btmtksdio_dev,
545 						  txrx_work);
546 	unsigned long txrx_timeout;
547 	u32 int_status, rx_size;
548 	struct sk_buff *skb;
549 	int err;
550 
551 	pm_runtime_get_sync(bdev->dev);
552 
553 	sdio_claim_host(bdev->func);
554 
555 	/* Disable interrupt */
556 	sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, 0);
557 
558 	txrx_timeout = jiffies + 5 * HZ;
559 
560 	do {
561 		int_status = sdio_readl(bdev->func, MTK_REG_CHISR, NULL);
562 
563 		/* Ack an interrupt as soon as possible before any operation on
564 		 * hardware.
565 		 *
566 		 * Note that we don't ack any status during operations to avoid race
567 		 * condition between the host and the device such as it's possible to
568 		 * mistakenly ack RX_DONE for the next packet and then cause interrupts
569 		 * not be raised again but there is still pending data in the hardware
570 		 * FIFO.
571 		 */
572 		sdio_writel(bdev->func, int_status, MTK_REG_CHISR, NULL);
573 		int_status &= INT_MASK;
574 
575 		if ((int_status & FW_MAILBOX_INT) &&
576 		    bdev->data->chipid == 0x7921) {
577 			sdio_writel(bdev->func, PH2DSM0R_DRIVER_OWN,
578 				    MTK_REG_PH2DSM0R, 0);
579 		}
580 
581 		if (int_status & FW_OWN_BACK_INT)
582 			bt_dev_dbg(bdev->hdev, "Get fw own back");
583 
584 		if (int_status & TX_EMPTY)
585 			set_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state);
586 
587 		else if (unlikely(int_status & TX_FIFO_OVERFLOW))
588 			bt_dev_warn(bdev->hdev, "Tx fifo overflow");
589 
590 		if (test_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state)) {
591 			skb = skb_dequeue(&bdev->txq);
592 			if (skb) {
593 				err = btmtksdio_tx_packet(bdev, skb);
594 				if (err < 0) {
595 					bdev->hdev->stat.err_tx++;
596 					skb_queue_head(&bdev->txq, skb);
597 				}
598 			}
599 		}
600 
601 		if (int_status & RX_DONE_INT) {
602 			rx_size = sdio_readl(bdev->func, MTK_REG_CRPLR, NULL);
603 			rx_size = (rx_size & RX_PKT_LEN) >> 16;
604 			if (btmtksdio_rx_packet(bdev, rx_size) < 0)
605 				bdev->hdev->stat.err_rx++;
606 		}
607 	} while (int_status || time_is_before_jiffies(txrx_timeout));
608 
609 	/* Enable interrupt */
610 	sdio_writel(bdev->func, C_INT_EN_SET, MTK_REG_CHLPCR, 0);
611 
612 	sdio_release_host(bdev->func);
613 
614 	pm_runtime_mark_last_busy(bdev->dev);
615 	pm_runtime_put_autosuspend(bdev->dev);
616 }
617 
618 static void btmtksdio_interrupt(struct sdio_func *func)
619 {
620 	struct btmtksdio_dev *bdev = sdio_get_drvdata(func);
621 
622 	/* Disable interrupt */
623 	sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, 0);
624 
625 	schedule_work(&bdev->txrx_work);
626 }
627 
628 static int btmtksdio_open(struct hci_dev *hdev)
629 {
630 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
631 	u32 val;
632 	int err;
633 
634 	sdio_claim_host(bdev->func);
635 
636 	err = sdio_enable_func(bdev->func);
637 	if (err < 0)
638 		goto err_release_host;
639 
640 	set_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state);
641 
642 	err = btmtksdio_drv_pmctrl(bdev);
643 	if (err < 0)
644 		goto err_disable_func;
645 
646 	/* Disable interrupt & mask out all interrupt sources */
647 	sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, &err);
648 	if (err < 0)
649 		goto err_disable_func;
650 
651 	sdio_writel(bdev->func, 0, MTK_REG_CHIER, &err);
652 	if (err < 0)
653 		goto err_disable_func;
654 
655 	err = sdio_claim_irq(bdev->func, btmtksdio_interrupt);
656 	if (err < 0)
657 		goto err_disable_func;
658 
659 	err = sdio_set_block_size(bdev->func, MTK_SDIO_BLOCK_SIZE);
660 	if (err < 0)
661 		goto err_release_irq;
662 
663 	/* SDIO CMD 5 allows the SDIO device back to idle state an
664 	 * synchronous interrupt is supported in SDIO 4-bit mode
665 	 */
666 	val = sdio_readl(bdev->func, MTK_REG_CSDIOCSR, &err);
667 	if (err < 0)
668 		goto err_release_irq;
669 
670 	val |= SDIO_INT_CTL;
671 	sdio_writel(bdev->func, val, MTK_REG_CSDIOCSR, &err);
672 	if (err < 0)
673 		goto err_release_irq;
674 
675 	/* Explitly set write-1-clear method */
676 	val = sdio_readl(bdev->func, MTK_REG_CHCR, &err);
677 	if (err < 0)
678 		goto err_release_irq;
679 
680 	val |= C_INT_CLR_CTRL;
681 	sdio_writel(bdev->func, val, MTK_REG_CHCR, &err);
682 	if (err < 0)
683 		goto err_release_irq;
684 
685 	/* Setup interrupt sources */
686 	sdio_writel(bdev->func, RX_DONE_INT | TX_EMPTY | TX_FIFO_OVERFLOW,
687 		    MTK_REG_CHIER, &err);
688 	if (err < 0)
689 		goto err_release_irq;
690 
691 	/* Enable interrupt */
692 	sdio_writel(bdev->func, C_INT_EN_SET, MTK_REG_CHLPCR, &err);
693 	if (err < 0)
694 		goto err_release_irq;
695 
696 	sdio_release_host(bdev->func);
697 
698 	return 0;
699 
700 err_release_irq:
701 	sdio_release_irq(bdev->func);
702 
703 err_disable_func:
704 	sdio_disable_func(bdev->func);
705 
706 err_release_host:
707 	sdio_release_host(bdev->func);
708 
709 	return err;
710 }
711 
712 static int btmtksdio_close(struct hci_dev *hdev)
713 {
714 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
715 
716 	sdio_claim_host(bdev->func);
717 
718 	/* Disable interrupt */
719 	sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, NULL);
720 
721 	sdio_release_irq(bdev->func);
722 
723 	cancel_work_sync(&bdev->txrx_work);
724 
725 	btmtksdio_fw_pmctrl(bdev);
726 
727 	clear_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state);
728 	sdio_disable_func(bdev->func);
729 
730 	sdio_release_host(bdev->func);
731 
732 	return 0;
733 }
734 
735 static int btmtksdio_flush(struct hci_dev *hdev)
736 {
737 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
738 
739 	skb_queue_purge(&bdev->txq);
740 
741 	cancel_work_sync(&bdev->txrx_work);
742 
743 	return 0;
744 }
745 
746 static int btmtksdio_func_query(struct hci_dev *hdev)
747 {
748 	struct btmtk_hci_wmt_params wmt_params;
749 	int status, err;
750 	u8 param = 0;
751 
752 	/* Query whether the function is enabled */
753 	wmt_params.op = BTMTK_WMT_FUNC_CTRL;
754 	wmt_params.flag = 4;
755 	wmt_params.dlen = sizeof(param);
756 	wmt_params.data = &param;
757 	wmt_params.status = &status;
758 
759 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
760 	if (err < 0) {
761 		bt_dev_err(hdev, "Failed to query function status (%d)", err);
762 		return err;
763 	}
764 
765 	return status;
766 }
767 
768 static int mt76xx_setup(struct hci_dev *hdev, const char *fwname)
769 {
770 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
771 	struct btmtk_hci_wmt_params wmt_params;
772 	struct btmtk_tci_sleep tci_sleep;
773 	struct sk_buff *skb;
774 	int err, status;
775 	u8 param = 0x1;
776 
777 	/* Query whether the firmware is already download */
778 	wmt_params.op = BTMTK_WMT_SEMAPHORE;
779 	wmt_params.flag = 1;
780 	wmt_params.dlen = 0;
781 	wmt_params.data = NULL;
782 	wmt_params.status = &status;
783 
784 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
785 	if (err < 0) {
786 		bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
787 		return err;
788 	}
789 
790 	if (status == BTMTK_WMT_PATCH_DONE) {
791 		bt_dev_info(hdev, "Firmware already downloaded");
792 		goto ignore_setup_fw;
793 	}
794 
795 	/* Setup a firmware which the device definitely requires */
796 	err = btmtk_setup_firmware(hdev, fwname, mtk_hci_wmt_sync);
797 	if (err < 0)
798 		return err;
799 
800 ignore_setup_fw:
801 	/* Query whether the device is already enabled */
802 	err = readx_poll_timeout(btmtksdio_func_query, hdev, status,
803 				 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
804 				 2000, 5000000);
805 	/* -ETIMEDOUT happens */
806 	if (err < 0)
807 		return err;
808 
809 	/* The other errors happen in btusb_mtk_func_query */
810 	if (status < 0)
811 		return status;
812 
813 	if (status == BTMTK_WMT_ON_DONE) {
814 		bt_dev_info(hdev, "function already on");
815 		goto ignore_func_on;
816 	}
817 
818 	/* Enable Bluetooth protocol */
819 	wmt_params.op = BTMTK_WMT_FUNC_CTRL;
820 	wmt_params.flag = 0;
821 	wmt_params.dlen = sizeof(param);
822 	wmt_params.data = &param;
823 	wmt_params.status = NULL;
824 
825 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
826 	if (err < 0) {
827 		bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
828 		return err;
829 	}
830 
831 	set_bit(BTMTKSDIO_PATCH_ENABLED, &bdev->tx_state);
832 
833 ignore_func_on:
834 	/* Apply the low power environment setup */
835 	tci_sleep.mode = 0x5;
836 	tci_sleep.duration = cpu_to_le16(0x640);
837 	tci_sleep.host_duration = cpu_to_le16(0x640);
838 	tci_sleep.host_wakeup_pin = 0;
839 	tci_sleep.time_compensation = 0;
840 
841 	skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
842 			     HCI_INIT_TIMEOUT);
843 	if (IS_ERR(skb)) {
844 		err = PTR_ERR(skb);
845 		bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
846 		return err;
847 	}
848 	kfree_skb(skb);
849 
850 	return 0;
851 }
852 
853 static int mt79xx_setup(struct hci_dev *hdev, const char *fwname)
854 {
855 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
856 	struct btmtk_hci_wmt_params wmt_params;
857 	u8 param = 0x1;
858 	int err;
859 
860 	err = btmtk_setup_firmware_79xx(hdev, fwname, mtk_hci_wmt_sync);
861 	if (err < 0) {
862 		bt_dev_err(hdev, "Failed to setup 79xx firmware (%d)", err);
863 		return err;
864 	}
865 
866 	/* Enable Bluetooth protocol */
867 	wmt_params.op = BTMTK_WMT_FUNC_CTRL;
868 	wmt_params.flag = 0;
869 	wmt_params.dlen = sizeof(param);
870 	wmt_params.data = &param;
871 	wmt_params.status = NULL;
872 
873 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
874 	if (err < 0) {
875 		bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
876 		return err;
877 	}
878 
879 	hci_set_msft_opcode(hdev, 0xFD30);
880 	hci_set_aosp_capable(hdev);
881 	set_bit(BTMTKSDIO_PATCH_ENABLED, &bdev->tx_state);
882 
883 	return err;
884 }
885 
886 static int btmtksdio_mtk_reg_read(struct hci_dev *hdev, u32 reg, u32 *val)
887 {
888 	struct btmtk_hci_wmt_params wmt_params;
889 	struct reg_read_cmd reg_read = {
890 		.type = 1,
891 		.num = 1,
892 	};
893 	u32 status;
894 	int err;
895 
896 	reg_read.addr = cpu_to_le32(reg);
897 	wmt_params.op = BTMTK_WMT_REGISTER;
898 	wmt_params.flag = BTMTK_WMT_REG_READ;
899 	wmt_params.dlen = sizeof(reg_read);
900 	wmt_params.data = &reg_read;
901 	wmt_params.status = &status;
902 
903 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
904 	if (err < 0) {
905 		bt_dev_err(hdev, "Failed to read reg (%d)", err);
906 		return err;
907 	}
908 
909 	*val = status;
910 
911 	return err;
912 }
913 
914 static int btmtksdio_mtk_reg_write(struct hci_dev *hdev, u32 reg, u32 val, u32 mask)
915 {
916 	struct btmtk_hci_wmt_params wmt_params;
917 	const struct reg_write_cmd reg_write = {
918 		.type = 1,
919 		.num = 1,
920 		.addr = cpu_to_le32(reg),
921 		.data = cpu_to_le32(val),
922 		.mask = cpu_to_le32(mask),
923 	};
924 	int err, status;
925 
926 	wmt_params.op = BTMTK_WMT_REGISTER;
927 	wmt_params.flag = BTMTK_WMT_REG_WRITE;
928 	wmt_params.dlen = sizeof(reg_write);
929 	wmt_params.data = &reg_write;
930 	wmt_params.status = &status;
931 
932 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
933 	if (err < 0)
934 		bt_dev_err(hdev, "Failed to write reg (%d)", err);
935 
936 	return err;
937 }
938 
939 static int btmtksdio_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
940 {
941 	/* uses 1 as data path id for all the usecases */
942 	*data_path_id = 1;
943 	return 0;
944 }
945 
946 static int btmtksdio_get_codec_config_data(struct hci_dev *hdev,
947 					   __u8 link, struct bt_codec *codec,
948 					   __u8 *ven_len, __u8 **ven_data)
949 {
950 	int err = 0;
951 
952 	if (!ven_data || !ven_len)
953 		return -EINVAL;
954 
955 	*ven_len = 0;
956 	*ven_data = NULL;
957 
958 	if (link != ESCO_LINK) {
959 		bt_dev_err(hdev, "Invalid link type(%u)", link);
960 		return -EINVAL;
961 	}
962 
963 	*ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
964 	if (!ven_data) {
965 		err = -ENOMEM;
966 		goto error;
967 	}
968 
969 	/* supports only CVSD and mSBC offload codecs */
970 	switch (codec->id) {
971 	case 0x02:
972 		**ven_data = 0x00;
973 		break;
974 	case 0x05:
975 		**ven_data = 0x01;
976 		break;
977 	default:
978 		err = -EINVAL;
979 		bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
980 		goto error;
981 	}
982 	/* codec and its capabilities are pre-defined to ids
983 	 * preset id = 0x00 represents CVSD codec with sampling rate 8K
984 	 * preset id = 0x01 represents mSBC codec with sampling rate 16K
985 	 */
986 	*ven_len = sizeof(__u8);
987 	return err;
988 
989 error:
990 	kfree(*ven_data);
991 	*ven_data = NULL;
992 	return err;
993 }
994 
995 static int btmtksdio_sco_setting(struct hci_dev *hdev)
996 {
997 	const struct btmtk_sco sco_setting = {
998 		.clock_config = 0x49,
999 		.channel_format_config = 0x80,
1000 	};
1001 	struct sk_buff *skb;
1002 	u32 val;
1003 	int err;
1004 
1005 	/* Enable SCO over I2S/PCM for MediaTek chipset */
1006 	skb =  __hci_cmd_sync(hdev, 0xfc72, sizeof(sco_setting),
1007 			      &sco_setting, HCI_CMD_TIMEOUT);
1008 	if (IS_ERR(skb))
1009 		return PTR_ERR(skb);
1010 
1011 	kfree_skb(skb);
1012 
1013 	err = btmtksdio_mtk_reg_read(hdev, MT7921_PINMUX_0, &val);
1014 	if (err < 0)
1015 		return err;
1016 
1017 	val |= 0x11000000;
1018 	err = btmtksdio_mtk_reg_write(hdev, MT7921_PINMUX_0, val, ~0);
1019 	if (err < 0)
1020 		return err;
1021 
1022 	err = btmtksdio_mtk_reg_read(hdev, MT7921_PINMUX_1, &val);
1023 	if (err < 0)
1024 		return err;
1025 
1026 	val |= 0x00000101;
1027 	err =  btmtksdio_mtk_reg_write(hdev, MT7921_PINMUX_1, val, ~0);
1028 	if (err < 0)
1029 		return err;
1030 
1031 	hdev->get_data_path_id = btmtksdio_get_data_path_id;
1032 	hdev->get_codec_config_data = btmtksdio_get_codec_config_data;
1033 
1034 	return err;
1035 }
1036 
1037 static int btmtksdio_reset_setting(struct hci_dev *hdev)
1038 {
1039 	int err;
1040 	u32 val;
1041 
1042 	err = btmtksdio_mtk_reg_read(hdev, MT7921_PINMUX_1, &val);
1043 	if (err < 0)
1044 		return err;
1045 
1046 	val |= 0x20; /* set the pin (bit field 11:8) work as GPIO mode */
1047 	err = btmtksdio_mtk_reg_write(hdev, MT7921_PINMUX_1, val, ~0);
1048 	if (err < 0)
1049 		return err;
1050 
1051 	err = btmtksdio_mtk_reg_read(hdev, MT7921_BTSYS_RST, &val);
1052 	if (err < 0)
1053 		return err;
1054 
1055 	val |= MT7921_BTSYS_RST_WITH_GPIO;
1056 	return btmtksdio_mtk_reg_write(hdev, MT7921_BTSYS_RST, val, ~0);
1057 }
1058 
1059 static int btmtksdio_setup(struct hci_dev *hdev)
1060 {
1061 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1062 	ktime_t calltime, delta, rettime;
1063 	unsigned long long duration;
1064 	char fwname[64];
1065 	int err, dev_id;
1066 	u32 fw_version = 0, val;
1067 
1068 	calltime = ktime_get();
1069 	set_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state);
1070 
1071 	switch (bdev->data->chipid) {
1072 	case 0x7921:
1073 		if (test_bit(BTMTKSDIO_HW_RESET_ACTIVE, &bdev->tx_state)) {
1074 			err = btmtksdio_mtk_reg_read(hdev, MT7921_DLSTATUS,
1075 						     &val);
1076 			if (err < 0)
1077 				return err;
1078 
1079 			val &= ~BT_DL_STATE;
1080 			err = btmtksdio_mtk_reg_write(hdev, MT7921_DLSTATUS,
1081 						      val, ~0);
1082 			if (err < 0)
1083 				return err;
1084 
1085 			btmtksdio_fw_pmctrl(bdev);
1086 			msleep(20);
1087 			btmtksdio_drv_pmctrl(bdev);
1088 
1089 			clear_bit(BTMTKSDIO_HW_RESET_ACTIVE, &bdev->tx_state);
1090 		}
1091 
1092 		err = btmtksdio_mtk_reg_read(hdev, 0x70010200, &dev_id);
1093 		if (err < 0) {
1094 			bt_dev_err(hdev, "Failed to get device id (%d)", err);
1095 			return err;
1096 		}
1097 
1098 		err = btmtksdio_mtk_reg_read(hdev, 0x80021004, &fw_version);
1099 		if (err < 0) {
1100 			bt_dev_err(hdev, "Failed to get fw version (%d)", err);
1101 			return err;
1102 		}
1103 
1104 		snprintf(fwname, sizeof(fwname),
1105 			 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
1106 			 dev_id & 0xffff, (fw_version & 0xff) + 1);
1107 		err = mt79xx_setup(hdev, fwname);
1108 		if (err < 0)
1109 			return err;
1110 
1111 		err = btmtksdio_fw_pmctrl(bdev);
1112 		if (err < 0)
1113 			return err;
1114 
1115 		err = btmtksdio_drv_pmctrl(bdev);
1116 		if (err < 0)
1117 			return err;
1118 
1119 		/* Enable SCO over I2S/PCM */
1120 		err = btmtksdio_sco_setting(hdev);
1121 		if (err < 0) {
1122 			bt_dev_err(hdev, "Failed to enable SCO setting (%d)", err);
1123 			return err;
1124 		}
1125 
1126 		/* Enable WBS with mSBC codec */
1127 		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
1128 
1129 		/* Enable GPIO reset mechanism */
1130 		if (bdev->reset) {
1131 			err = btmtksdio_reset_setting(hdev);
1132 			if (err < 0) {
1133 				bt_dev_err(hdev, "Failed to enable Reset setting (%d)", err);
1134 				devm_gpiod_put(bdev->dev, bdev->reset);
1135 				bdev->reset = NULL;
1136 			}
1137 		}
1138 
1139 		/* Valid LE States quirk for MediaTek 7921 */
1140 		set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
1141 
1142 		break;
1143 	case 0x7663:
1144 	case 0x7668:
1145 		err = mt76xx_setup(hdev, bdev->data->fwname);
1146 		if (err < 0)
1147 			return err;
1148 		break;
1149 	default:
1150 		return -ENODEV;
1151 	}
1152 
1153 	rettime = ktime_get();
1154 	delta = ktime_sub(rettime, calltime);
1155 	duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1156 
1157 	pm_runtime_set_autosuspend_delay(bdev->dev,
1158 					 MTKBTSDIO_AUTOSUSPEND_DELAY);
1159 	pm_runtime_use_autosuspend(bdev->dev);
1160 
1161 	err = pm_runtime_set_active(bdev->dev);
1162 	if (err < 0)
1163 		return err;
1164 
1165 	/* Default forbid runtime auto suspend, that can be allowed by
1166 	 * enable_autosuspend flag or the PM runtime entry under sysfs.
1167 	 */
1168 	pm_runtime_forbid(bdev->dev);
1169 	pm_runtime_enable(bdev->dev);
1170 
1171 	if (enable_autosuspend)
1172 		pm_runtime_allow(bdev->dev);
1173 
1174 	bt_dev_info(hdev, "Device setup in %llu usecs", duration);
1175 
1176 	return 0;
1177 }
1178 
1179 static int btmtksdio_shutdown(struct hci_dev *hdev)
1180 {
1181 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1182 	struct btmtk_hci_wmt_params wmt_params;
1183 	u8 param = 0x0;
1184 	int err;
1185 
1186 	/* Get back the state to be consistent with the state
1187 	 * in btmtksdio_setup.
1188 	 */
1189 	pm_runtime_get_sync(bdev->dev);
1190 
1191 	/* Disable the device */
1192 	wmt_params.op = BTMTK_WMT_FUNC_CTRL;
1193 	wmt_params.flag = 0;
1194 	wmt_params.dlen = sizeof(param);
1195 	wmt_params.data = &param;
1196 	wmt_params.status = NULL;
1197 
1198 	err = mtk_hci_wmt_sync(hdev, &wmt_params);
1199 	if (err < 0) {
1200 		bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
1201 		return err;
1202 	}
1203 
1204 	pm_runtime_put_noidle(bdev->dev);
1205 	pm_runtime_disable(bdev->dev);
1206 
1207 	return 0;
1208 }
1209 
1210 static int btmtksdio_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1211 {
1212 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1213 
1214 	switch (hci_skb_pkt_type(skb)) {
1215 	case HCI_COMMAND_PKT:
1216 		hdev->stat.cmd_tx++;
1217 		break;
1218 
1219 	case HCI_ACLDATA_PKT:
1220 		hdev->stat.acl_tx++;
1221 		break;
1222 
1223 	case HCI_SCODATA_PKT:
1224 		hdev->stat.sco_tx++;
1225 		break;
1226 
1227 	default:
1228 		return -EILSEQ;
1229 	}
1230 
1231 	skb_queue_tail(&bdev->txq, skb);
1232 
1233 	schedule_work(&bdev->txrx_work);
1234 
1235 	return 0;
1236 }
1237 
1238 static void btmtksdio_cmd_timeout(struct hci_dev *hdev)
1239 {
1240 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1241 	u32 status;
1242 	int err;
1243 
1244 	if (!bdev->reset || bdev->data->chipid != 0x7921)
1245 		return;
1246 
1247 	pm_runtime_get_sync(bdev->dev);
1248 
1249 	if (test_and_set_bit(BTMTKSDIO_HW_RESET_ACTIVE, &bdev->tx_state))
1250 		return;
1251 
1252 	sdio_claim_host(bdev->func);
1253 
1254 	sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, NULL);
1255 	skb_queue_purge(&bdev->txq);
1256 	cancel_work_sync(&bdev->txrx_work);
1257 
1258 	gpiod_set_value_cansleep(bdev->reset, 1);
1259 	msleep(100);
1260 	gpiod_set_value_cansleep(bdev->reset, 0);
1261 
1262 	err = readx_poll_timeout(btmtksdio_chcr_query, bdev, status,
1263 				 status & BT_RST_DONE, 100000, 2000000);
1264 	if (err < 0) {
1265 		bt_dev_err(hdev, "Failed to reset (%d)", err);
1266 		goto err;
1267 	}
1268 
1269 	clear_bit(BTMTKSDIO_PATCH_ENABLED, &bdev->tx_state);
1270 err:
1271 	sdio_release_host(bdev->func);
1272 
1273 	pm_runtime_put_noidle(bdev->dev);
1274 	pm_runtime_disable(bdev->dev);
1275 
1276 	hci_reset_dev(hdev);
1277 }
1278 
1279 static bool btmtksdio_sdio_wakeup(struct hci_dev *hdev)
1280 {
1281 	struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1282 	bool may_wakeup = device_may_wakeup(bdev->dev);
1283 	const struct btmtk_wakeon bt_awake = {
1284 		.mode = 0x1,
1285 		.gpo = 0,
1286 		.active_high = 0x1,
1287 		.enable_delay = cpu_to_le16(0xc80),
1288 		.wakeup_delay = cpu_to_le16(0x20),
1289 	};
1290 
1291 	if (may_wakeup && bdev->data->chipid == 0x7921) {
1292 		struct sk_buff *skb;
1293 
1294 		skb =  __hci_cmd_sync(hdev, 0xfc27, sizeof(bt_awake),
1295 				      &bt_awake, HCI_CMD_TIMEOUT);
1296 		if (IS_ERR(skb))
1297 			may_wakeup = false;
1298 		else
1299 			kfree_skb(skb);
1300 	}
1301 
1302 	return may_wakeup;
1303 }
1304 
1305 static int btmtksdio_probe(struct sdio_func *func,
1306 			   const struct sdio_device_id *id)
1307 {
1308 	struct btmtksdio_dev *bdev;
1309 	struct hci_dev *hdev;
1310 	int err;
1311 
1312 	bdev = devm_kzalloc(&func->dev, sizeof(*bdev), GFP_KERNEL);
1313 	if (!bdev)
1314 		return -ENOMEM;
1315 
1316 	bdev->data = (void *)id->driver_data;
1317 	if (!bdev->data)
1318 		return -ENODEV;
1319 
1320 	bdev->dev = &func->dev;
1321 	bdev->func = func;
1322 
1323 	INIT_WORK(&bdev->txrx_work, btmtksdio_txrx_work);
1324 	skb_queue_head_init(&bdev->txq);
1325 
1326 	/* Initialize and register HCI device */
1327 	hdev = hci_alloc_dev();
1328 	if (!hdev) {
1329 		dev_err(&func->dev, "Can't allocate HCI device\n");
1330 		return -ENOMEM;
1331 	}
1332 
1333 	bdev->hdev = hdev;
1334 
1335 	hdev->bus = HCI_SDIO;
1336 	hci_set_drvdata(hdev, bdev);
1337 
1338 	hdev->open     = btmtksdio_open;
1339 	hdev->close    = btmtksdio_close;
1340 	hdev->cmd_timeout = btmtksdio_cmd_timeout;
1341 	hdev->flush    = btmtksdio_flush;
1342 	hdev->setup    = btmtksdio_setup;
1343 	hdev->shutdown = btmtksdio_shutdown;
1344 	hdev->send     = btmtksdio_send_frame;
1345 	hdev->wakeup   = btmtksdio_sdio_wakeup;
1346 	hdev->set_bdaddr = btmtk_set_bdaddr;
1347 
1348 	SET_HCIDEV_DEV(hdev, &func->dev);
1349 
1350 	hdev->manufacturer = 70;
1351 	set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1352 
1353 	sdio_set_drvdata(func, bdev);
1354 
1355 	err = hci_register_dev(hdev);
1356 	if (err < 0) {
1357 		dev_err(&func->dev, "Can't register HCI device\n");
1358 		hci_free_dev(hdev);
1359 		return err;
1360 	}
1361 
1362 	/* pm_runtime_enable would be done after the firmware is being
1363 	 * downloaded because the core layer probably already enables
1364 	 * runtime PM for this func such as the case host->caps &
1365 	 * MMC_CAP_POWER_OFF_CARD.
1366 	 */
1367 	if (pm_runtime_enabled(bdev->dev))
1368 		pm_runtime_disable(bdev->dev);
1369 
1370 	/* As explaination in drivers/mmc/core/sdio_bus.c tells us:
1371 	 * Unbound SDIO functions are always suspended.
1372 	 * During probe, the function is set active and the usage count
1373 	 * is incremented.  If the driver supports runtime PM,
1374 	 * it should call pm_runtime_put_noidle() in its probe routine and
1375 	 * pm_runtime_get_noresume() in its remove routine.
1376 	 *
1377 	 * So, put a pm_runtime_put_noidle here !
1378 	 */
1379 	pm_runtime_put_noidle(bdev->dev);
1380 
1381 	err = device_init_wakeup(bdev->dev, true);
1382 	if (err)
1383 		bt_dev_err(hdev, "failed to initialize device wakeup");
1384 
1385 	bdev->dev->of_node = of_find_compatible_node(NULL, NULL,
1386 						     "mediatek,mt7921s-bluetooth");
1387 	bdev->reset = devm_gpiod_get_optional(bdev->dev, "reset",
1388 					      GPIOD_OUT_LOW);
1389 	if (IS_ERR(bdev->reset))
1390 		err = PTR_ERR(bdev->reset);
1391 
1392 	return err;
1393 }
1394 
1395 static void btmtksdio_remove(struct sdio_func *func)
1396 {
1397 	struct btmtksdio_dev *bdev = sdio_get_drvdata(func);
1398 	struct hci_dev *hdev;
1399 
1400 	if (!bdev)
1401 		return;
1402 
1403 	/* Be consistent the state in btmtksdio_probe */
1404 	pm_runtime_get_noresume(bdev->dev);
1405 
1406 	hdev = bdev->hdev;
1407 
1408 	sdio_set_drvdata(func, NULL);
1409 	hci_unregister_dev(hdev);
1410 	hci_free_dev(hdev);
1411 }
1412 
1413 #ifdef CONFIG_PM
1414 static int btmtksdio_runtime_suspend(struct device *dev)
1415 {
1416 	struct sdio_func *func = dev_to_sdio_func(dev);
1417 	struct btmtksdio_dev *bdev;
1418 	int err;
1419 
1420 	bdev = sdio_get_drvdata(func);
1421 	if (!bdev)
1422 		return 0;
1423 
1424 	if (!test_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state))
1425 		return 0;
1426 
1427 	sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1428 
1429 	err = btmtksdio_fw_pmctrl(bdev);
1430 
1431 	bt_dev_dbg(bdev->hdev, "status (%d) return ownership to device", err);
1432 
1433 	return err;
1434 }
1435 
1436 static int btmtksdio_runtime_resume(struct device *dev)
1437 {
1438 	struct sdio_func *func = dev_to_sdio_func(dev);
1439 	struct btmtksdio_dev *bdev;
1440 	int err;
1441 
1442 	bdev = sdio_get_drvdata(func);
1443 	if (!bdev)
1444 		return 0;
1445 
1446 	if (!test_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state))
1447 		return 0;
1448 
1449 	err = btmtksdio_drv_pmctrl(bdev);
1450 
1451 	bt_dev_dbg(bdev->hdev, "status (%d) get ownership from device", err);
1452 
1453 	return err;
1454 }
1455 
1456 static UNIVERSAL_DEV_PM_OPS(btmtksdio_pm_ops, btmtksdio_runtime_suspend,
1457 			    btmtksdio_runtime_resume, NULL);
1458 #define BTMTKSDIO_PM_OPS (&btmtksdio_pm_ops)
1459 #else	/* CONFIG_PM */
1460 #define BTMTKSDIO_PM_OPS NULL
1461 #endif	/* CONFIG_PM */
1462 
1463 static struct sdio_driver btmtksdio_driver = {
1464 	.name		= "btmtksdio",
1465 	.probe		= btmtksdio_probe,
1466 	.remove		= btmtksdio_remove,
1467 	.id_table	= btmtksdio_table,
1468 	.drv = {
1469 		.owner = THIS_MODULE,
1470 		.pm = BTMTKSDIO_PM_OPS,
1471 	}
1472 };
1473 
1474 module_sdio_driver(btmtksdio_driver);
1475 
1476 module_param(enable_autosuspend, bool, 0644);
1477 MODULE_PARM_DESC(enable_autosuspend, "Enable autosuspend by default");
1478 
1479 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1480 MODULE_DESCRIPTION("MediaTek Bluetooth SDIO driver ver " VERSION);
1481 MODULE_VERSION(VERSION);
1482 MODULE_LICENSE("GPL");
1483