1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 MediaTek Inc.
5 * Bluetooth support for MediaTek SDIO devices
7 * This file is written based on btsdio.c and btmtkuart.c.
9 * Author: Sean Wang <sean.wang@mediatek.com>
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>
21 #include <linux/pm_runtime.h>
22 #include <linux/skbuff.h>
24 #include <linux/mmc/host.h>
25 #include <linux/mmc/sdio_ids.h>
26 #include <linux/mmc/sdio_func.h>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
36 #define MTKBTSDIO_AUTOSUSPEND_DELAY 1000
38 static bool enable_autosuspend = true;
40 struct btmtksdio_data {
43 bool lp_mbox_supported;
46 static const struct btmtksdio_data mt7663_data = {
47 .fwname = FIRMWARE_MT7663,
49 .lp_mbox_supported = false,
52 static const struct btmtksdio_data mt7668_data = {
53 .fwname = FIRMWARE_MT7668,
55 .lp_mbox_supported = false,
58 static const struct btmtksdio_data mt7921_data = {
59 .fwname = FIRMWARE_MT7961,
61 .lp_mbox_supported = true,
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 */
73 MODULE_DEVICE_TABLE(sdio, btmtksdio_table);
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)
82 #define MTK_REG_CSDIOCSR 0x8
83 #define SDIO_RE_INIT_EN BIT(0)
84 #define SDIO_INT_CTL BIT(2)
86 #define MTK_REG_CHCR 0xc
87 #define C_INT_CLR_CTRL BIT(1)
88 #define BT_RST_DONE BIT(8)
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)
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)
108 #define MTK_REG_CTDR 0x18
110 #define MTK_REG_CRDR 0x1c
112 #define MTK_REG_CRPLR 0x24
114 #define MTK_SDIO_BLOCK_SIZE 256
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
122 struct mtkbtsdio_hdr {
128 struct btmtksdio_dev {
129 struct hci_dev *hdev;
130 struct sdio_func *func;
133 struct work_struct txrx_work;
134 unsigned long tx_state;
135 struct sk_buff_head txq;
137 struct sk_buff *evt_skb;
139 const struct btmtksdio_data *data;
141 struct gpio_desc *reset;
144 static int mtk_hci_wmt_sync(struct hci_dev *hdev,
145 struct btmtk_hci_wmt_params *wmt_params)
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;
156 /* Send the WMT command and wait until the WMT event returns */
157 hlen = sizeof(*hdr) + wmt_params->dlen;
161 wc = kzalloc(hlen, GFP_KERNEL);
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);
172 set_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
174 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
176 clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
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.
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
189 err = wait_on_bit_timeout(&bdev->tx_state, BTMTKSDIO_TX_WAIT_VND_EVT,
190 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
192 bt_dev_err(hdev, "Execution of wmt command interrupted");
193 clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
198 bt_dev_err(hdev, "Execution of wmt command timed out");
199 clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state);
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);
213 switch (wmt_evt->whdr.op) {
214 case BTMTK_WMT_SEMAPHORE:
215 if (wmt_evt->whdr.flag == 2)
216 status = BTMTK_WMT_PATCH_UNDONE;
218 status = BTMTK_WMT_PATCH_DONE;
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;
227 status = BTMTK_WMT_ON_UNDONE;
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;
235 status = BTMTK_WMT_PATCH_UNDONE;
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);
244 if (wmt_params->status)
245 *wmt_params->status = status;
248 kfree_skb(bdev->evt_skb);
249 bdev->evt_skb = NULL;
256 static int btmtksdio_tx_packet(struct btmtksdio_dev *bdev,
259 struct mtkbtsdio_hdr *sdio_hdr;
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,
270 /* Prepend MediaTek SDIO Specific Header */
271 skb_push(skb, sizeof(*sdio_hdr));
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);
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));
284 bdev->hdev->stat.byte_tx += skb->len;
291 skb_pull(skb, sizeof(*sdio_hdr));
296 static u32 btmtksdio_drv_own_query(struct btmtksdio_dev *bdev)
298 return sdio_readl(bdev->func, MTK_REG_CHLPCR, NULL);
301 static u32 btmtksdio_drv_own_query_79xx(struct btmtksdio_dev *bdev)
303 return sdio_readl(bdev->func, MTK_REG_PD2HRM0R, NULL);
306 static u32 btmtksdio_chcr_query(struct btmtksdio_dev *bdev)
308 return sdio_readl(bdev->func, MTK_REG_CHCR, NULL);
311 static int btmtksdio_fw_pmctrl(struct btmtksdio_dev *bdev)
316 sdio_claim_host(bdev->func);
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,
322 err = readx_poll_timeout(btmtksdio_drv_own_query_79xx, bdev,
323 status, !(status & PD2HRM0R_DRV_OWN),
326 bt_dev_err(bdev->hdev, "mailbox ACK not cleared");
331 /* Return ownership to the device */
332 sdio_writel(bdev->func, C_FW_OWN_REQ_SET, MTK_REG_CHLPCR, &err);
336 err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status,
337 !(status & C_COM_DRV_OWN), 2000, 1000000);
340 sdio_release_host(bdev->func);
343 bt_dev_err(bdev->hdev, "Cannot return ownership to device");
348 static int btmtksdio_drv_pmctrl(struct btmtksdio_dev *bdev)
353 sdio_claim_host(bdev->func);
355 /* Get ownership from the device */
356 sdio_writel(bdev->func, C_FW_OWN_REQ_CLR, MTK_REG_CHLPCR, &err);
360 err = readx_poll_timeout(btmtksdio_drv_own_query, bdev, status,
361 status & C_COM_DRV_OWN, 2000, 1000000);
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,
370 sdio_release_host(bdev->func);
373 bt_dev_err(bdev->hdev, "Cannot get ownership from device");
378 static int btmtksdio_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
380 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
381 struct hci_event_hdr *hdr = (void *)skb->data;
385 /* When someone waits for the WMT event, the skb is being cloned
386 * and being processed the events from there then.
388 if (test_bit(BTMTKSDIO_TX_WAIT_VND_EVT, &bdev->tx_state)) {
389 bdev->evt_skb = skb_clone(skb, GFP_KERNEL);
390 if (!bdev->evt_skb) {
396 err = hci_recv_frame(hdev, skb);
400 if (evt == HCI_EV_WMT) {
401 if (test_and_clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT,
403 /* Barrier to sync with other CPUs */
404 smp_mb__after_atomic();
405 wake_up_bit(&bdev->tx_state, BTMTKSDIO_TX_WAIT_VND_EVT);
412 kfree_skb(bdev->evt_skb);
413 bdev->evt_skb = NULL;
419 static int btmtksdio_recv_acl(struct hci_dev *hdev, struct sk_buff *skb)
421 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
422 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
426 /* Firmware dump from device: when the firmware hangs, the
427 * device can no longer suspend and thus disable auto-suspend.
429 pm_runtime_forbid(bdev->dev);
433 /* Firmware debug logging */
434 return hci_recv_diag(hdev, skb);
437 return hci_recv_frame(hdev, skb);
440 static const struct h4_recv_pkt mtk_recv_pkts[] = {
441 { H4_RECV_ACL, .recv = btmtksdio_recv_acl },
442 { H4_RECV_SCO, .recv = hci_recv_frame },
443 { H4_RECV_EVENT, .recv = btmtksdio_recv_event },
446 static int btmtksdio_rx_packet(struct btmtksdio_dev *bdev, u16 rx_size)
448 const struct h4_recv_pkt *pkts = mtk_recv_pkts;
449 int pkts_count = ARRAY_SIZE(mtk_recv_pkts);
450 struct mtkbtsdio_hdr *sdio_hdr;
451 int err, i, pad_size;
455 if (rx_size < sizeof(*sdio_hdr))
458 /* A SDIO packet is exactly containing a Bluetooth packet */
459 skb = bt_skb_alloc(rx_size, GFP_KERNEL);
463 skb_put(skb, rx_size);
465 err = sdio_readsb(bdev->func, skb->data, MTK_REG_CRDR, rx_size);
469 sdio_hdr = (void *)skb->data;
471 /* We assume the default error as -EILSEQ simply to make the error path
476 if (rx_size != le16_to_cpu(sdio_hdr->len)) {
477 bt_dev_err(bdev->hdev, "Rx size in sdio header is mismatched ");
481 hci_skb_pkt_type(skb) = sdio_hdr->bt_type;
483 /* Remove MediaTek SDIO header */
484 skb_pull(skb, sizeof(*sdio_hdr));
486 /* We have to dig into the packet to get payload size and then know how
487 * many padding bytes at the tail, these padding bytes should be removed
488 * before the packet is indicated to the core layer.
490 for (i = 0; i < pkts_count; i++) {
491 if (sdio_hdr->bt_type == (&pkts[i])->type)
495 if (i >= pkts_count) {
496 bt_dev_err(bdev->hdev, "Invalid bt type 0x%02x",
501 /* Remaining bytes cannot hold a header*/
502 if (skb->len < (&pkts[i])->hlen) {
503 bt_dev_err(bdev->hdev, "The size of bt header is mismatched");
507 switch ((&pkts[i])->lsize) {
509 dlen = skb->data[(&pkts[i])->loff];
512 dlen = get_unaligned_le16(skb->data +
519 pad_size = skb->len - (&pkts[i])->hlen - dlen;
521 /* Remaining bytes cannot hold a payload */
523 bt_dev_err(bdev->hdev, "The size of bt payload is mismatched");
527 /* Remove padding bytes */
528 skb_trim(skb, skb->len - pad_size);
531 (&pkts[i])->recv(bdev->hdev, skb);
533 bdev->hdev->stat.byte_rx += rx_size;
543 static void btmtksdio_txrx_work(struct work_struct *work)
545 struct btmtksdio_dev *bdev = container_of(work, struct btmtksdio_dev,
547 unsigned long txrx_timeout;
548 u32 int_status, rx_size;
552 pm_runtime_get_sync(bdev->dev);
554 sdio_claim_host(bdev->func);
556 /* Disable interrupt */
557 sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, 0);
559 txrx_timeout = jiffies + 5 * HZ;
562 int_status = sdio_readl(bdev->func, MTK_REG_CHISR, NULL);
564 /* Ack an interrupt as soon as possible before any operation on
567 * Note that we don't ack any status during operations to avoid race
568 * condition between the host and the device such as it's possible to
569 * mistakenly ack RX_DONE for the next packet and then cause interrupts
570 * not be raised again but there is still pending data in the hardware
573 sdio_writel(bdev->func, int_status, MTK_REG_CHISR, NULL);
574 int_status &= INT_MASK;
576 if ((int_status & FW_MAILBOX_INT) &&
577 bdev->data->chipid == 0x7921) {
578 sdio_writel(bdev->func, PH2DSM0R_DRIVER_OWN,
579 MTK_REG_PH2DSM0R, 0);
582 if (int_status & FW_OWN_BACK_INT)
583 bt_dev_dbg(bdev->hdev, "Get fw own back");
585 if (int_status & TX_EMPTY)
586 set_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state);
588 else if (unlikely(int_status & TX_FIFO_OVERFLOW))
589 bt_dev_warn(bdev->hdev, "Tx fifo overflow");
591 if (test_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state)) {
592 skb = skb_dequeue(&bdev->txq);
594 err = btmtksdio_tx_packet(bdev, skb);
596 bdev->hdev->stat.err_tx++;
597 skb_queue_head(&bdev->txq, skb);
602 if (int_status & RX_DONE_INT) {
603 rx_size = sdio_readl(bdev->func, MTK_REG_CRPLR, NULL);
604 rx_size = (rx_size & RX_PKT_LEN) >> 16;
605 if (btmtksdio_rx_packet(bdev, rx_size) < 0)
606 bdev->hdev->stat.err_rx++;
608 } while (int_status || time_is_before_jiffies(txrx_timeout));
610 /* Enable interrupt */
611 sdio_writel(bdev->func, C_INT_EN_SET, MTK_REG_CHLPCR, 0);
613 sdio_release_host(bdev->func);
615 pm_runtime_mark_last_busy(bdev->dev);
616 pm_runtime_put_autosuspend(bdev->dev);
619 static void btmtksdio_interrupt(struct sdio_func *func)
621 struct btmtksdio_dev *bdev = sdio_get_drvdata(func);
623 /* Disable interrupt */
624 sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, 0);
626 schedule_work(&bdev->txrx_work);
629 static int btmtksdio_open(struct hci_dev *hdev)
631 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
635 sdio_claim_host(bdev->func);
637 err = sdio_enable_func(bdev->func);
639 goto err_release_host;
641 set_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state);
643 err = btmtksdio_drv_pmctrl(bdev);
645 goto err_disable_func;
647 /* Disable interrupt & mask out all interrupt sources */
648 sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, &err);
650 goto err_disable_func;
652 sdio_writel(bdev->func, 0, MTK_REG_CHIER, &err);
654 goto err_disable_func;
656 err = sdio_claim_irq(bdev->func, btmtksdio_interrupt);
658 goto err_disable_func;
660 err = sdio_set_block_size(bdev->func, MTK_SDIO_BLOCK_SIZE);
662 goto err_release_irq;
664 /* SDIO CMD 5 allows the SDIO device back to idle state an
665 * synchronous interrupt is supported in SDIO 4-bit mode
667 val = sdio_readl(bdev->func, MTK_REG_CSDIOCSR, &err);
669 goto err_release_irq;
672 sdio_writel(bdev->func, val, MTK_REG_CSDIOCSR, &err);
674 goto err_release_irq;
676 /* Explitly set write-1-clear method */
677 val = sdio_readl(bdev->func, MTK_REG_CHCR, &err);
679 goto err_release_irq;
681 val |= C_INT_CLR_CTRL;
682 sdio_writel(bdev->func, val, MTK_REG_CHCR, &err);
684 goto err_release_irq;
686 /* Setup interrupt sources */
687 sdio_writel(bdev->func, RX_DONE_INT | TX_EMPTY | TX_FIFO_OVERFLOW,
688 MTK_REG_CHIER, &err);
690 goto err_release_irq;
692 /* Enable interrupt */
693 sdio_writel(bdev->func, C_INT_EN_SET, MTK_REG_CHLPCR, &err);
695 goto err_release_irq;
697 sdio_release_host(bdev->func);
702 sdio_release_irq(bdev->func);
705 sdio_disable_func(bdev->func);
708 sdio_release_host(bdev->func);
713 static int btmtksdio_close(struct hci_dev *hdev)
715 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
717 sdio_claim_host(bdev->func);
719 /* Disable interrupt */
720 sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, NULL);
722 sdio_release_irq(bdev->func);
724 cancel_work_sync(&bdev->txrx_work);
726 btmtksdio_fw_pmctrl(bdev);
728 clear_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state);
729 sdio_disable_func(bdev->func);
731 sdio_release_host(bdev->func);
736 static int btmtksdio_flush(struct hci_dev *hdev)
738 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
740 skb_queue_purge(&bdev->txq);
742 cancel_work_sync(&bdev->txrx_work);
747 static int btmtksdio_func_query(struct hci_dev *hdev)
749 struct btmtk_hci_wmt_params wmt_params;
753 /* Query whether the function is enabled */
754 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
756 wmt_params.dlen = sizeof(param);
757 wmt_params.data = ¶m;
758 wmt_params.status = &status;
760 err = mtk_hci_wmt_sync(hdev, &wmt_params);
762 bt_dev_err(hdev, "Failed to query function status (%d)", err);
769 static int mt76xx_setup(struct hci_dev *hdev, const char *fwname)
771 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
772 struct btmtk_hci_wmt_params wmt_params;
773 struct btmtk_tci_sleep tci_sleep;
778 /* Query whether the firmware is already download */
779 wmt_params.op = BTMTK_WMT_SEMAPHORE;
782 wmt_params.data = NULL;
783 wmt_params.status = &status;
785 err = mtk_hci_wmt_sync(hdev, &wmt_params);
787 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
791 if (status == BTMTK_WMT_PATCH_DONE) {
792 bt_dev_info(hdev, "Firmware already downloaded");
793 goto ignore_setup_fw;
796 /* Setup a firmware which the device definitely requires */
797 err = btmtk_setup_firmware(hdev, fwname, mtk_hci_wmt_sync);
802 /* Query whether the device is already enabled */
803 err = readx_poll_timeout(btmtksdio_func_query, hdev, status,
804 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
806 /* -ETIMEDOUT happens */
810 /* The other errors happen in btusb_mtk_func_query */
814 if (status == BTMTK_WMT_ON_DONE) {
815 bt_dev_info(hdev, "function already on");
819 /* Enable Bluetooth protocol */
820 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
822 wmt_params.dlen = sizeof(param);
823 wmt_params.data = ¶m;
824 wmt_params.status = NULL;
826 err = mtk_hci_wmt_sync(hdev, &wmt_params);
828 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
832 set_bit(BTMTKSDIO_PATCH_ENABLED, &bdev->tx_state);
835 /* Apply the low power environment setup */
836 tci_sleep.mode = 0x5;
837 tci_sleep.duration = cpu_to_le16(0x640);
838 tci_sleep.host_duration = cpu_to_le16(0x640);
839 tci_sleep.host_wakeup_pin = 0;
840 tci_sleep.time_compensation = 0;
842 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
846 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
854 static int mt79xx_setup(struct hci_dev *hdev, const char *fwname)
856 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
857 struct btmtk_hci_wmt_params wmt_params;
861 err = btmtk_setup_firmware_79xx(hdev, fwname, mtk_hci_wmt_sync);
863 bt_dev_err(hdev, "Failed to setup 79xx firmware (%d)", err);
867 err = btmtksdio_fw_pmctrl(bdev);
871 err = btmtksdio_drv_pmctrl(bdev);
875 /* Enable Bluetooth protocol */
876 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
878 wmt_params.dlen = sizeof(param);
879 wmt_params.data = ¶m;
880 wmt_params.status = NULL;
882 err = mtk_hci_wmt_sync(hdev, &wmt_params);
884 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
888 hci_set_msft_opcode(hdev, 0xFD30);
889 hci_set_aosp_capable(hdev);
890 set_bit(BTMTKSDIO_PATCH_ENABLED, &bdev->tx_state);
895 static int btmtksdio_mtk_reg_read(struct hci_dev *hdev, u32 reg, u32 *val)
897 struct btmtk_hci_wmt_params wmt_params;
898 struct reg_read_cmd reg_read = {
905 reg_read.addr = cpu_to_le32(reg);
906 wmt_params.op = BTMTK_WMT_REGISTER;
907 wmt_params.flag = BTMTK_WMT_REG_READ;
908 wmt_params.dlen = sizeof(reg_read);
909 wmt_params.data = ®_read;
910 wmt_params.status = &status;
912 err = mtk_hci_wmt_sync(hdev, &wmt_params);
914 bt_dev_err(hdev, "Failed to read reg (%d)", err);
923 static int btmtksdio_mtk_reg_write(struct hci_dev *hdev, u32 reg, u32 val, u32 mask)
925 struct btmtk_hci_wmt_params wmt_params;
926 const struct reg_write_cmd reg_write = {
929 .addr = cpu_to_le32(reg),
930 .data = cpu_to_le32(val),
931 .mask = cpu_to_le32(mask),
935 wmt_params.op = BTMTK_WMT_REGISTER;
936 wmt_params.flag = BTMTK_WMT_REG_WRITE;
937 wmt_params.dlen = sizeof(reg_write);
938 wmt_params.data = ®_write;
939 wmt_params.status = &status;
941 err = mtk_hci_wmt_sync(hdev, &wmt_params);
943 bt_dev_err(hdev, "Failed to write reg (%d)", err);
948 static int btmtksdio_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
950 /* uses 1 as data path id for all the usecases */
955 static int btmtksdio_get_codec_config_data(struct hci_dev *hdev,
956 __u8 link, struct bt_codec *codec,
957 __u8 *ven_len, __u8 **ven_data)
961 if (!ven_data || !ven_len)
967 if (link != ESCO_LINK) {
968 bt_dev_err(hdev, "Invalid link type(%u)", link);
972 *ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
978 /* supports only CVSD and mSBC offload codecs */
988 bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
991 /* codec and its capabilities are pre-defined to ids
992 * preset id = 0x00 represents CVSD codec with sampling rate 8K
993 * preset id = 0x01 represents mSBC codec with sampling rate 16K
995 *ven_len = sizeof(__u8);
1004 static int btmtksdio_sco_setting(struct hci_dev *hdev)
1006 const struct btmtk_sco sco_setting = {
1007 .clock_config = 0x49,
1008 .channel_format_config = 0x80,
1010 struct sk_buff *skb;
1014 /* Enable SCO over I2S/PCM for MediaTek chipset */
1015 skb = __hci_cmd_sync(hdev, 0xfc72, sizeof(sco_setting),
1016 &sco_setting, HCI_CMD_TIMEOUT);
1018 return PTR_ERR(skb);
1022 err = btmtksdio_mtk_reg_read(hdev, MT7921_PINMUX_0, &val);
1027 err = btmtksdio_mtk_reg_write(hdev, MT7921_PINMUX_0, val, ~0);
1031 err = btmtksdio_mtk_reg_read(hdev, MT7921_PINMUX_1, &val);
1036 err = btmtksdio_mtk_reg_write(hdev, MT7921_PINMUX_1, val, ~0);
1040 hdev->get_data_path_id = btmtksdio_get_data_path_id;
1041 hdev->get_codec_config_data = btmtksdio_get_codec_config_data;
1046 static int btmtksdio_reset_setting(struct hci_dev *hdev)
1051 err = btmtksdio_mtk_reg_read(hdev, MT7921_PINMUX_1, &val);
1055 val |= 0x20; /* set the pin (bit field 11:8) work as GPIO mode */
1056 err = btmtksdio_mtk_reg_write(hdev, MT7921_PINMUX_1, val, ~0);
1060 err = btmtksdio_mtk_reg_read(hdev, MT7921_BTSYS_RST, &val);
1064 val |= MT7921_BTSYS_RST_WITH_GPIO;
1065 return btmtksdio_mtk_reg_write(hdev, MT7921_BTSYS_RST, val, ~0);
1068 static int btmtksdio_setup(struct hci_dev *hdev)
1070 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1071 ktime_t calltime, delta, rettime;
1072 unsigned long long duration;
1075 u32 fw_version = 0, val;
1077 calltime = ktime_get();
1078 set_bit(BTMTKSDIO_HW_TX_READY, &bdev->tx_state);
1080 switch (bdev->data->chipid) {
1082 if (test_bit(BTMTKSDIO_HW_RESET_ACTIVE, &bdev->tx_state)) {
1083 err = btmtksdio_mtk_reg_read(hdev, MT7921_DLSTATUS,
1088 val &= ~BT_DL_STATE;
1089 err = btmtksdio_mtk_reg_write(hdev, MT7921_DLSTATUS,
1094 btmtksdio_fw_pmctrl(bdev);
1096 btmtksdio_drv_pmctrl(bdev);
1098 clear_bit(BTMTKSDIO_HW_RESET_ACTIVE, &bdev->tx_state);
1101 err = btmtksdio_mtk_reg_read(hdev, 0x70010200, &dev_id);
1103 bt_dev_err(hdev, "Failed to get device id (%d)", err);
1107 err = btmtksdio_mtk_reg_read(hdev, 0x80021004, &fw_version);
1109 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
1113 snprintf(fwname, sizeof(fwname),
1114 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
1115 dev_id & 0xffff, (fw_version & 0xff) + 1);
1116 err = mt79xx_setup(hdev, fwname);
1120 /* Enable SCO over I2S/PCM */
1121 err = btmtksdio_sco_setting(hdev);
1123 bt_dev_err(hdev, "Failed to enable SCO setting (%d)", err);
1127 /* Enable WBS with mSBC codec */
1128 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
1130 /* Enable GPIO reset mechanism */
1132 err = btmtksdio_reset_setting(hdev);
1134 bt_dev_err(hdev, "Failed to enable Reset setting (%d)", err);
1135 devm_gpiod_put(bdev->dev, bdev->reset);
1140 /* Valid LE States quirk for MediaTek 7921 */
1141 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
1146 err = mt76xx_setup(hdev, bdev->data->fwname);
1154 rettime = ktime_get();
1155 delta = ktime_sub(rettime, calltime);
1156 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1158 pm_runtime_set_autosuspend_delay(bdev->dev,
1159 MTKBTSDIO_AUTOSUSPEND_DELAY);
1160 pm_runtime_use_autosuspend(bdev->dev);
1162 err = pm_runtime_set_active(bdev->dev);
1166 /* Default forbid runtime auto suspend, that can be allowed by
1167 * enable_autosuspend flag or the PM runtime entry under sysfs.
1169 pm_runtime_forbid(bdev->dev);
1170 pm_runtime_enable(bdev->dev);
1172 if (enable_autosuspend)
1173 pm_runtime_allow(bdev->dev);
1175 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
1180 static int btmtksdio_shutdown(struct hci_dev *hdev)
1182 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1183 struct btmtk_hci_wmt_params wmt_params;
1187 /* Get back the state to be consistent with the state
1188 * in btmtksdio_setup.
1190 pm_runtime_get_sync(bdev->dev);
1192 /* wmt command only works until the reset is complete */
1193 if (test_bit(BTMTKSDIO_HW_RESET_ACTIVE, &bdev->tx_state))
1194 goto ignore_wmt_cmd;
1196 /* Disable the device */
1197 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
1198 wmt_params.flag = 0;
1199 wmt_params.dlen = sizeof(param);
1200 wmt_params.data = ¶m;
1201 wmt_params.status = NULL;
1203 err = mtk_hci_wmt_sync(hdev, &wmt_params);
1205 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
1210 pm_runtime_put_noidle(bdev->dev);
1211 pm_runtime_disable(bdev->dev);
1216 static int btmtksdio_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1218 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1220 switch (hci_skb_pkt_type(skb)) {
1221 case HCI_COMMAND_PKT:
1222 hdev->stat.cmd_tx++;
1225 case HCI_ACLDATA_PKT:
1226 hdev->stat.acl_tx++;
1229 case HCI_SCODATA_PKT:
1230 hdev->stat.sco_tx++;
1237 skb_queue_tail(&bdev->txq, skb);
1239 schedule_work(&bdev->txrx_work);
1244 static void btmtksdio_cmd_timeout(struct hci_dev *hdev)
1246 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1250 if (!bdev->reset || bdev->data->chipid != 0x7921)
1253 pm_runtime_get_sync(bdev->dev);
1255 if (test_and_set_bit(BTMTKSDIO_HW_RESET_ACTIVE, &bdev->tx_state))
1258 sdio_claim_host(bdev->func);
1260 sdio_writel(bdev->func, C_INT_EN_CLR, MTK_REG_CHLPCR, NULL);
1261 skb_queue_purge(&bdev->txq);
1262 cancel_work_sync(&bdev->txrx_work);
1264 gpiod_set_value_cansleep(bdev->reset, 1);
1266 gpiod_set_value_cansleep(bdev->reset, 0);
1268 err = readx_poll_timeout(btmtksdio_chcr_query, bdev, status,
1269 status & BT_RST_DONE, 100000, 2000000);
1271 bt_dev_err(hdev, "Failed to reset (%d)", err);
1275 clear_bit(BTMTKSDIO_PATCH_ENABLED, &bdev->tx_state);
1277 sdio_release_host(bdev->func);
1279 pm_runtime_put_noidle(bdev->dev);
1280 pm_runtime_disable(bdev->dev);
1282 hci_reset_dev(hdev);
1285 static bool btmtksdio_sdio_inband_wakeup(struct hci_dev *hdev)
1287 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1289 return device_may_wakeup(bdev->dev);
1292 static bool btmtksdio_sdio_wakeup(struct hci_dev *hdev)
1294 struct btmtksdio_dev *bdev = hci_get_drvdata(hdev);
1295 bool may_wakeup = device_may_wakeup(bdev->dev);
1296 const struct btmtk_wakeon bt_awake = {
1300 .enable_delay = cpu_to_le16(0xc80),
1301 .wakeup_delay = cpu_to_le16(0x20),
1304 if (may_wakeup && bdev->data->chipid == 0x7921) {
1305 struct sk_buff *skb;
1307 skb = __hci_cmd_sync(hdev, 0xfc27, sizeof(bt_awake),
1308 &bt_awake, HCI_CMD_TIMEOUT);
1318 static int btmtksdio_probe(struct sdio_func *func,
1319 const struct sdio_device_id *id)
1321 struct btmtksdio_dev *bdev;
1322 struct hci_dev *hdev;
1325 bdev = devm_kzalloc(&func->dev, sizeof(*bdev), GFP_KERNEL);
1329 bdev->data = (void *)id->driver_data;
1333 bdev->dev = &func->dev;
1336 INIT_WORK(&bdev->txrx_work, btmtksdio_txrx_work);
1337 skb_queue_head_init(&bdev->txq);
1339 /* Initialize and register HCI device */
1340 hdev = hci_alloc_dev();
1342 dev_err(&func->dev, "Can't allocate HCI device\n");
1348 hdev->bus = HCI_SDIO;
1349 hci_set_drvdata(hdev, bdev);
1351 hdev->open = btmtksdio_open;
1352 hdev->close = btmtksdio_close;
1353 hdev->cmd_timeout = btmtksdio_cmd_timeout;
1354 hdev->flush = btmtksdio_flush;
1355 hdev->setup = btmtksdio_setup;
1356 hdev->shutdown = btmtksdio_shutdown;
1357 hdev->send = btmtksdio_send_frame;
1358 hdev->wakeup = btmtksdio_sdio_wakeup;
1360 * If SDIO controller supports wake on Bluetooth, sending a wakeon
1361 * command is not necessary.
1363 if (device_can_wakeup(func->card->host->parent))
1364 hdev->wakeup = btmtksdio_sdio_inband_wakeup;
1366 hdev->wakeup = btmtksdio_sdio_wakeup;
1367 hdev->set_bdaddr = btmtk_set_bdaddr;
1369 SET_HCIDEV_DEV(hdev, &func->dev);
1371 hdev->manufacturer = 70;
1372 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
1374 sdio_set_drvdata(func, bdev);
1376 err = hci_register_dev(hdev);
1378 dev_err(&func->dev, "Can't register HCI device\n");
1383 /* pm_runtime_enable would be done after the firmware is being
1384 * downloaded because the core layer probably already enables
1385 * runtime PM for this func such as the case host->caps &
1386 * MMC_CAP_POWER_OFF_CARD.
1388 if (pm_runtime_enabled(bdev->dev))
1389 pm_runtime_disable(bdev->dev);
1391 /* As explaination in drivers/mmc/core/sdio_bus.c tells us:
1392 * Unbound SDIO functions are always suspended.
1393 * During probe, the function is set active and the usage count
1394 * is incremented. If the driver supports runtime PM,
1395 * it should call pm_runtime_put_noidle() in its probe routine and
1396 * pm_runtime_get_noresume() in its remove routine.
1398 * So, put a pm_runtime_put_noidle here !
1400 pm_runtime_put_noidle(bdev->dev);
1402 err = device_init_wakeup(bdev->dev, true);
1404 bt_dev_err(hdev, "failed to initialize device wakeup");
1406 bdev->dev->of_node = of_find_compatible_node(NULL, NULL,
1407 "mediatek,mt7921s-bluetooth");
1408 bdev->reset = devm_gpiod_get_optional(bdev->dev, "reset",
1410 if (IS_ERR(bdev->reset))
1411 err = PTR_ERR(bdev->reset);
1416 static void btmtksdio_remove(struct sdio_func *func)
1418 struct btmtksdio_dev *bdev = sdio_get_drvdata(func);
1419 struct hci_dev *hdev;
1424 /* Be consistent the state in btmtksdio_probe */
1425 pm_runtime_get_noresume(bdev->dev);
1429 sdio_set_drvdata(func, NULL);
1430 hci_unregister_dev(hdev);
1435 static int btmtksdio_runtime_suspend(struct device *dev)
1437 struct sdio_func *func = dev_to_sdio_func(dev);
1438 struct btmtksdio_dev *bdev;
1441 bdev = sdio_get_drvdata(func);
1445 if (!test_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state))
1448 sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1450 err = btmtksdio_fw_pmctrl(bdev);
1452 bt_dev_dbg(bdev->hdev, "status (%d) return ownership to device", err);
1457 static int btmtksdio_runtime_resume(struct device *dev)
1459 struct sdio_func *func = dev_to_sdio_func(dev);
1460 struct btmtksdio_dev *bdev;
1463 bdev = sdio_get_drvdata(func);
1467 if (!test_bit(BTMTKSDIO_FUNC_ENABLED, &bdev->tx_state))
1470 err = btmtksdio_drv_pmctrl(bdev);
1472 bt_dev_dbg(bdev->hdev, "status (%d) get ownership from device", err);
1477 static UNIVERSAL_DEV_PM_OPS(btmtksdio_pm_ops, btmtksdio_runtime_suspend,
1478 btmtksdio_runtime_resume, NULL);
1479 #define BTMTKSDIO_PM_OPS (&btmtksdio_pm_ops)
1480 #else /* CONFIG_PM */
1481 #define BTMTKSDIO_PM_OPS NULL
1482 #endif /* CONFIG_PM */
1484 static struct sdio_driver btmtksdio_driver = {
1485 .name = "btmtksdio",
1486 .probe = btmtksdio_probe,
1487 .remove = btmtksdio_remove,
1488 .id_table = btmtksdio_table,
1490 .owner = THIS_MODULE,
1491 .pm = BTMTKSDIO_PM_OPS,
1495 module_sdio_driver(btmtksdio_driver);
1497 module_param(enable_autosuspend, bool, 0644);
1498 MODULE_PARM_DESC(enable_autosuspend, "Enable autosuspend by default");
1500 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1501 MODULE_DESCRIPTION("MediaTek Bluetooth SDIO driver ver " VERSION);
1502 MODULE_VERSION(VERSION);
1503 MODULE_LICENSE("GPL");