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/firmware.h>
16 #include <linux/init.h>
17 #include <linux/iopoll.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/skbuff.h>
23 #include <linux/mmc/host.h>
24 #include <linux/mmc/sdio_ids.h>
25 #include <linux/mmc/sdio_func.h>
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
34 #define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
35 #define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
37 #define MTKBTSDIO_AUTOSUSPEND_DELAY 8000
39 static bool enable_autosuspend
;
41 struct btmtksdio_data
{
45 static const struct btmtksdio_data mt7663_data
= {
46 .fwname
= FIRMWARE_MT7663
,
49 static const struct btmtksdio_data mt7668_data
= {
50 .fwname
= FIRMWARE_MT7668
,
53 static const struct sdio_device_id btmtksdio_table
[] = {
54 {SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK
, SDIO_DEVICE_ID_MEDIATEK_MT7663
),
55 .driver_data
= (kernel_ulong_t
)&mt7663_data
},
56 {SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK
, SDIO_DEVICE_ID_MEDIATEK_MT7668
),
57 .driver_data
= (kernel_ulong_t
)&mt7668_data
},
58 { } /* Terminating entry */
60 MODULE_DEVICE_TABLE(sdio
, btmtksdio_table
);
62 #define MTK_REG_CHLPCR 0x4 /* W1S */
63 #define C_INT_EN_SET BIT(0)
64 #define C_INT_EN_CLR BIT(1)
65 #define C_FW_OWN_REQ_SET BIT(8) /* For write */
66 #define C_COM_DRV_OWN BIT(8) /* For read */
67 #define C_FW_OWN_REQ_CLR BIT(9)
69 #define MTK_REG_CSDIOCSR 0x8
70 #define SDIO_RE_INIT_EN BIT(0)
71 #define SDIO_INT_CTL BIT(2)
73 #define MTK_REG_CHCR 0xc
74 #define C_INT_CLR_CTRL BIT(1)
76 /* CHISR have the same bits field definition with CHIER */
77 #define MTK_REG_CHISR 0x10
78 #define MTK_REG_CHIER 0x14
79 #define FW_OWN_BACK_INT BIT(0)
80 #define RX_DONE_INT BIT(1)
81 #define TX_EMPTY BIT(2)
82 #define TX_FIFO_OVERFLOW BIT(8)
83 #define RX_PKT_LEN GENMASK(31, 16)
85 #define MTK_REG_CTDR 0x18
87 #define MTK_REG_CRDR 0x1c
89 #define MTK_SDIO_BLOCK_SIZE 256
91 #define BTMTKSDIO_TX_WAIT_VND_EVT 1
94 MTK_WMT_PATCH_DWNLD
= 0x1,
98 MTK_WMT_FUNC_CTRL
= 0x6,
100 MTK_WMT_SEMAPHORE
= 0x17,
105 BTMTK_WMT_PATCH_UNDONE
,
106 BTMTK_WMT_PATCH_DONE
,
109 BTMTK_WMT_ON_PROGRESS
,
112 struct mtkbtsdio_hdr
{
125 struct mtk_hci_wmt_cmd
{
126 struct mtk_wmt_hdr hdr
;
130 struct btmtk_hci_wmt_evt
{
131 struct hci_event_hdr hhdr
;
132 struct mtk_wmt_hdr whdr
;
135 struct btmtk_hci_wmt_evt_funcc
{
136 struct btmtk_hci_wmt_evt hwhdr
;
140 struct btmtk_tci_sleep
{
143 __le16 host_duration
;
145 u8 time_compensation
;
148 struct btmtk_hci_wmt_params
{
156 struct btmtksdio_dev
{
157 struct hci_dev
*hdev
;
158 struct sdio_func
*func
;
161 struct work_struct tx_work
;
162 unsigned long tx_state
;
163 struct sk_buff_head txq
;
165 struct sk_buff
*evt_skb
;
167 const struct btmtksdio_data
*data
;
170 static int mtk_hci_wmt_sync(struct hci_dev
*hdev
,
171 struct btmtk_hci_wmt_params
*wmt_params
)
173 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
174 struct btmtk_hci_wmt_evt_funcc
*wmt_evt_funcc
;
175 u32 hlen
, status
= BTMTK_WMT_INVALID
;
176 struct btmtk_hci_wmt_evt
*wmt_evt
;
177 struct mtk_hci_wmt_cmd wc
;
178 struct mtk_wmt_hdr
*hdr
;
181 hlen
= sizeof(*hdr
) + wmt_params
->dlen
;
185 hdr
= (struct mtk_wmt_hdr
*)&wc
;
187 hdr
->op
= wmt_params
->op
;
188 hdr
->dlen
= cpu_to_le16(wmt_params
->dlen
+ 1);
189 hdr
->flag
= wmt_params
->flag
;
190 memcpy(wc
.data
, wmt_params
->data
, wmt_params
->dlen
);
192 set_bit(BTMTKSDIO_TX_WAIT_VND_EVT
, &bdev
->tx_state
);
194 err
= __hci_cmd_send(hdev
, 0xfc6f, hlen
, &wc
);
196 clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT
, &bdev
->tx_state
);
200 /* The vendor specific WMT commands are all answered by a vendor
201 * specific event and will not have the Command Status or Command
202 * Complete as with usual HCI command flow control.
204 * After sending the command, wait for BTMTKSDIO_TX_WAIT_VND_EVT
205 * state to be cleared. The driver specific event receive routine
206 * will clear that state and with that indicate completion of the
209 err
= wait_on_bit_timeout(&bdev
->tx_state
, BTMTKSDIO_TX_WAIT_VND_EVT
,
210 TASK_INTERRUPTIBLE
, HCI_INIT_TIMEOUT
);
212 bt_dev_err(hdev
, "Execution of wmt command interrupted");
213 clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT
, &bdev
->tx_state
);
218 bt_dev_err(hdev
, "Execution of wmt command timed out");
219 clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT
, &bdev
->tx_state
);
223 /* Parse and handle the return WMT event */
224 wmt_evt
= (struct btmtk_hci_wmt_evt
*)bdev
->evt_skb
->data
;
225 if (wmt_evt
->whdr
.op
!= hdr
->op
) {
226 bt_dev_err(hdev
, "Wrong op received %d expected %d",
227 wmt_evt
->whdr
.op
, hdr
->op
);
232 switch (wmt_evt
->whdr
.op
) {
233 case MTK_WMT_SEMAPHORE
:
234 if (wmt_evt
->whdr
.flag
== 2)
235 status
= BTMTK_WMT_PATCH_UNDONE
;
237 status
= BTMTK_WMT_PATCH_DONE
;
239 case MTK_WMT_FUNC_CTRL
:
240 wmt_evt_funcc
= (struct btmtk_hci_wmt_evt_funcc
*)wmt_evt
;
241 if (be16_to_cpu(wmt_evt_funcc
->status
) == 0x404)
242 status
= BTMTK_WMT_ON_DONE
;
243 else if (be16_to_cpu(wmt_evt_funcc
->status
) == 0x420)
244 status
= BTMTK_WMT_ON_PROGRESS
;
246 status
= BTMTK_WMT_ON_UNDONE
;
250 if (wmt_params
->status
)
251 *wmt_params
->status
= status
;
254 kfree_skb(bdev
->evt_skb
);
255 bdev
->evt_skb
= NULL
;
260 static int btmtksdio_tx_packet(struct btmtksdio_dev
*bdev
,
263 struct mtkbtsdio_hdr
*sdio_hdr
;
266 /* Make sure that there are enough rooms for SDIO header */
267 if (unlikely(skb_headroom(skb
) < sizeof(*sdio_hdr
))) {
268 err
= pskb_expand_head(skb
, sizeof(*sdio_hdr
), 0,
274 /* Prepend MediaTek SDIO Specific Header */
275 skb_push(skb
, sizeof(*sdio_hdr
));
277 sdio_hdr
= (void *)skb
->data
;
278 sdio_hdr
->len
= cpu_to_le16(skb
->len
);
279 sdio_hdr
->reserved
= cpu_to_le16(0);
280 sdio_hdr
->bt_type
= hci_skb_pkt_type(skb
);
282 err
= sdio_writesb(bdev
->func
, MTK_REG_CTDR
, skb
->data
,
283 round_up(skb
->len
, MTK_SDIO_BLOCK_SIZE
));
287 bdev
->hdev
->stat
.byte_tx
+= skb
->len
;
294 skb_pull(skb
, sizeof(*sdio_hdr
));
299 static u32
btmtksdio_drv_own_query(struct btmtksdio_dev
*bdev
)
301 return sdio_readl(bdev
->func
, MTK_REG_CHLPCR
, NULL
);
304 static void btmtksdio_tx_work(struct work_struct
*work
)
306 struct btmtksdio_dev
*bdev
= container_of(work
, struct btmtksdio_dev
,
311 pm_runtime_get_sync(bdev
->dev
);
313 sdio_claim_host(bdev
->func
);
315 while ((skb
= skb_dequeue(&bdev
->txq
))) {
316 err
= btmtksdio_tx_packet(bdev
, skb
);
318 bdev
->hdev
->stat
.err_tx
++;
319 skb_queue_head(&bdev
->txq
, skb
);
324 sdio_release_host(bdev
->func
);
326 pm_runtime_mark_last_busy(bdev
->dev
);
327 pm_runtime_put_autosuspend(bdev
->dev
);
330 static int btmtksdio_recv_event(struct hci_dev
*hdev
, struct sk_buff
*skb
)
332 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
333 struct hci_event_hdr
*hdr
= (void *)skb
->data
;
336 /* Fix up the vendor event id with 0xff for vendor specific instead
337 * of 0xe4 so that event send via monitoring socket can be parsed
340 if (hdr
->evt
== 0xe4)
341 hdr
->evt
= HCI_EV_VENDOR
;
343 /* When someone waits for the WMT event, the skb is being cloned
344 * and being processed the events from there then.
346 if (test_bit(BTMTKSDIO_TX_WAIT_VND_EVT
, &bdev
->tx_state
)) {
347 bdev
->evt_skb
= skb_clone(skb
, GFP_KERNEL
);
348 if (!bdev
->evt_skb
) {
354 err
= hci_recv_frame(hdev
, skb
);
358 if (hdr
->evt
== HCI_EV_VENDOR
) {
359 if (test_and_clear_bit(BTMTKSDIO_TX_WAIT_VND_EVT
,
361 /* Barrier to sync with other CPUs */
362 smp_mb__after_atomic();
363 wake_up_bit(&bdev
->tx_state
, BTMTKSDIO_TX_WAIT_VND_EVT
);
370 kfree_skb(bdev
->evt_skb
);
371 bdev
->evt_skb
= NULL
;
377 static const struct h4_recv_pkt mtk_recv_pkts
[] = {
378 { H4_RECV_ACL
, .recv
= hci_recv_frame
},
379 { H4_RECV_SCO
, .recv
= hci_recv_frame
},
380 { H4_RECV_EVENT
, .recv
= btmtksdio_recv_event
},
383 static int btmtksdio_rx_packet(struct btmtksdio_dev
*bdev
, u16 rx_size
)
385 const struct h4_recv_pkt
*pkts
= mtk_recv_pkts
;
386 int pkts_count
= ARRAY_SIZE(mtk_recv_pkts
);
387 struct mtkbtsdio_hdr
*sdio_hdr
;
388 int err
, i
, pad_size
;
392 if (rx_size
< sizeof(*sdio_hdr
))
395 /* A SDIO packet is exactly containing a Bluetooth packet */
396 skb
= bt_skb_alloc(rx_size
, GFP_KERNEL
);
400 skb_put(skb
, rx_size
);
402 err
= sdio_readsb(bdev
->func
, skb
->data
, MTK_REG_CRDR
, rx_size
);
406 sdio_hdr
= (void *)skb
->data
;
408 /* We assume the default error as -EILSEQ simply to make the error path
413 if (rx_size
!= le16_to_cpu(sdio_hdr
->len
)) {
414 bt_dev_err(bdev
->hdev
, "Rx size in sdio header is mismatched ");
418 hci_skb_pkt_type(skb
) = sdio_hdr
->bt_type
;
420 /* Remove MediaTek SDIO header */
421 skb_pull(skb
, sizeof(*sdio_hdr
));
423 /* We have to dig into the packet to get payload size and then know how
424 * many padding bytes at the tail, these padding bytes should be removed
425 * before the packet is indicated to the core layer.
427 for (i
= 0; i
< pkts_count
; i
++) {
428 if (sdio_hdr
->bt_type
== (&pkts
[i
])->type
)
432 if (i
>= pkts_count
) {
433 bt_dev_err(bdev
->hdev
, "Invalid bt type 0x%02x",
438 /* Remaining bytes cannot hold a header*/
439 if (skb
->len
< (&pkts
[i
])->hlen
) {
440 bt_dev_err(bdev
->hdev
, "The size of bt header is mismatched");
444 switch ((&pkts
[i
])->lsize
) {
446 dlen
= skb
->data
[(&pkts
[i
])->loff
];
449 dlen
= get_unaligned_le16(skb
->data
+
456 pad_size
= skb
->len
- (&pkts
[i
])->hlen
- dlen
;
458 /* Remaining bytes cannot hold a payload */
460 bt_dev_err(bdev
->hdev
, "The size of bt payload is mismatched");
464 /* Remove padding bytes */
465 skb_trim(skb
, skb
->len
- pad_size
);
468 (&pkts
[i
])->recv(bdev
->hdev
, skb
);
470 bdev
->hdev
->stat
.byte_rx
+= rx_size
;
480 static void btmtksdio_interrupt(struct sdio_func
*func
)
482 struct btmtksdio_dev
*bdev
= sdio_get_drvdata(func
);
486 /* It is required that the host gets ownership from the device before
487 * accessing any register, however, if SDIO host is not being released,
488 * a potential deadlock probably happens in a circular wait between SDIO
489 * IRQ work and PM runtime work. So, we have to explicitly release SDIO
490 * host here and claim again after the PM runtime work is all done.
492 sdio_release_host(bdev
->func
);
494 pm_runtime_get_sync(bdev
->dev
);
496 sdio_claim_host(bdev
->func
);
498 /* Disable interrupt */
499 sdio_writel(func
, C_INT_EN_CLR
, MTK_REG_CHLPCR
, NULL
);
501 int_status
= sdio_readl(func
, MTK_REG_CHISR
, NULL
);
503 /* Ack an interrupt as soon as possible before any operation on
506 * Note that we don't ack any status during operations to avoid race
507 * condition between the host and the device such as it's possible to
508 * mistakenly ack RX_DONE for the next packet and then cause interrupts
509 * not be raised again but there is still pending data in the hardware
512 sdio_writel(func
, int_status
, MTK_REG_CHISR
, NULL
);
514 if (unlikely(!int_status
))
515 bt_dev_err(bdev
->hdev
, "CHISR is 0");
517 if (int_status
& FW_OWN_BACK_INT
)
518 bt_dev_dbg(bdev
->hdev
, "Get fw own back");
520 if (int_status
& TX_EMPTY
)
521 schedule_work(&bdev
->tx_work
);
522 else if (unlikely(int_status
& TX_FIFO_OVERFLOW
))
523 bt_dev_warn(bdev
->hdev
, "Tx fifo overflow");
525 if (int_status
& RX_DONE_INT
) {
526 rx_size
= (int_status
& RX_PKT_LEN
) >> 16;
528 if (btmtksdio_rx_packet(bdev
, rx_size
) < 0)
529 bdev
->hdev
->stat
.err_rx
++;
532 /* Enable interrupt */
533 sdio_writel(func
, C_INT_EN_SET
, MTK_REG_CHLPCR
, NULL
);
535 pm_runtime_mark_last_busy(bdev
->dev
);
536 pm_runtime_put_autosuspend(bdev
->dev
);
539 static int btmtksdio_open(struct hci_dev
*hdev
)
541 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
545 sdio_claim_host(bdev
->func
);
547 err
= sdio_enable_func(bdev
->func
);
549 goto err_release_host
;
551 /* Get ownership from the device */
552 sdio_writel(bdev
->func
, C_FW_OWN_REQ_CLR
, MTK_REG_CHLPCR
, &err
);
554 goto err_disable_func
;
556 err
= readx_poll_timeout(btmtksdio_drv_own_query
, bdev
, status
,
557 status
& C_COM_DRV_OWN
, 2000, 1000000);
559 bt_dev_err(bdev
->hdev
, "Cannot get ownership from device");
560 goto err_disable_func
;
563 /* Disable interrupt & mask out all interrupt sources */
564 sdio_writel(bdev
->func
, C_INT_EN_CLR
, MTK_REG_CHLPCR
, &err
);
566 goto err_disable_func
;
568 sdio_writel(bdev
->func
, 0, MTK_REG_CHIER
, &err
);
570 goto err_disable_func
;
572 err
= sdio_claim_irq(bdev
->func
, btmtksdio_interrupt
);
574 goto err_disable_func
;
576 err
= sdio_set_block_size(bdev
->func
, MTK_SDIO_BLOCK_SIZE
);
578 goto err_release_irq
;
580 /* SDIO CMD 5 allows the SDIO device back to idle state an
581 * synchronous interrupt is supported in SDIO 4-bit mode
583 sdio_writel(bdev
->func
, SDIO_INT_CTL
| SDIO_RE_INIT_EN
,
584 MTK_REG_CSDIOCSR
, &err
);
586 goto err_release_irq
;
588 /* Setup write-1-clear for CHISR register */
589 sdio_writel(bdev
->func
, C_INT_CLR_CTRL
, MTK_REG_CHCR
, &err
);
591 goto err_release_irq
;
593 /* Setup interrupt sources */
594 sdio_writel(bdev
->func
, RX_DONE_INT
| TX_EMPTY
| TX_FIFO_OVERFLOW
,
595 MTK_REG_CHIER
, &err
);
597 goto err_release_irq
;
599 /* Enable interrupt */
600 sdio_writel(bdev
->func
, C_INT_EN_SET
, MTK_REG_CHLPCR
, &err
);
602 goto err_release_irq
;
604 sdio_release_host(bdev
->func
);
609 sdio_release_irq(bdev
->func
);
612 sdio_disable_func(bdev
->func
);
615 sdio_release_host(bdev
->func
);
620 static int btmtksdio_close(struct hci_dev
*hdev
)
622 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
626 sdio_claim_host(bdev
->func
);
628 /* Disable interrupt */
629 sdio_writel(bdev
->func
, C_INT_EN_CLR
, MTK_REG_CHLPCR
, NULL
);
631 sdio_release_irq(bdev
->func
);
633 /* Return ownership to the device */
634 sdio_writel(bdev
->func
, C_FW_OWN_REQ_SET
, MTK_REG_CHLPCR
, NULL
);
636 err
= readx_poll_timeout(btmtksdio_drv_own_query
, bdev
, status
,
637 !(status
& C_COM_DRV_OWN
), 2000, 1000000);
639 bt_dev_err(bdev
->hdev
, "Cannot return ownership to device");
641 sdio_disable_func(bdev
->func
);
643 sdio_release_host(bdev
->func
);
648 static int btmtksdio_flush(struct hci_dev
*hdev
)
650 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
652 skb_queue_purge(&bdev
->txq
);
654 cancel_work_sync(&bdev
->tx_work
);
659 static int btmtksdio_func_query(struct hci_dev
*hdev
)
661 struct btmtk_hci_wmt_params wmt_params
;
665 /* Query whether the function is enabled */
666 wmt_params
.op
= MTK_WMT_FUNC_CTRL
;
668 wmt_params
.dlen
= sizeof(param
);
669 wmt_params
.data
= ¶m
;
670 wmt_params
.status
= &status
;
672 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
674 bt_dev_err(hdev
, "Failed to query function status (%d)", err
);
681 static int mtk_setup_firmware(struct hci_dev
*hdev
, const char *fwname
)
683 struct btmtk_hci_wmt_params wmt_params
;
684 const struct firmware
*fw
;
690 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
692 bt_dev_err(hdev
, "Failed to load firmware file (%d)", err
);
696 /* Power on data RAM the firmware relies on. */
698 wmt_params
.op
= MTK_WMT_FUNC_CTRL
;
700 wmt_params
.dlen
= sizeof(param
);
701 wmt_params
.data
= ¶m
;
702 wmt_params
.status
= NULL
;
704 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
706 bt_dev_err(hdev
, "Failed to power on data RAM (%d)", err
);
713 /* The size of patch header is 30 bytes, should be skip */
723 wmt_params
.op
= MTK_WMT_PATCH_DWNLD
;
724 wmt_params
.status
= NULL
;
726 while (fw_size
> 0) {
727 dlen
= min_t(int, 250, fw_size
);
729 /* Tell device the position in sequence */
730 if (fw_size
- dlen
<= 0)
732 else if (fw_size
< fw
->size
- 30)
735 wmt_params
.flag
= flag
;
736 wmt_params
.dlen
= dlen
;
737 wmt_params
.data
= fw_ptr
;
739 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
741 bt_dev_err(hdev
, "Failed to send wmt patch dwnld (%d)",
750 wmt_params
.op
= MTK_WMT_RST
;
753 wmt_params
.data
= NULL
;
754 wmt_params
.status
= NULL
;
756 /* Activate funciton the firmware providing to */
757 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
759 bt_dev_err(hdev
, "Failed to send wmt rst (%d)", err
);
763 /* Wait a few moments for firmware activation done */
764 usleep_range(10000, 12000);
767 release_firmware(fw
);
771 static int btmtksdio_setup(struct hci_dev
*hdev
)
773 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
774 struct btmtk_hci_wmt_params wmt_params
;
775 ktime_t calltime
, delta
, rettime
;
776 struct btmtk_tci_sleep tci_sleep
;
777 unsigned long long duration
;
782 calltime
= ktime_get();
784 /* Query whether the firmware is already download */
785 wmt_params
.op
= MTK_WMT_SEMAPHORE
;
788 wmt_params
.data
= NULL
;
789 wmt_params
.status
= &status
;
791 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
793 bt_dev_err(hdev
, "Failed to query firmware status (%d)", err
);
797 if (status
== BTMTK_WMT_PATCH_DONE
) {
798 bt_dev_info(hdev
, "Firmware already downloaded");
799 goto ignore_setup_fw
;
802 /* Setup a firmware which the device definitely requires */
803 err
= mtk_setup_firmware(hdev
, bdev
->data
->fwname
);
808 /* Query whether the device is already enabled */
809 err
= readx_poll_timeout(btmtksdio_func_query
, hdev
, status
,
810 status
< 0 || status
!= BTMTK_WMT_ON_PROGRESS
,
812 /* -ETIMEDOUT happens */
816 /* The other errors happen in btusb_mtk_func_query */
820 if (status
== BTMTK_WMT_ON_DONE
) {
821 bt_dev_info(hdev
, "function already on");
825 /* Enable Bluetooth protocol */
826 wmt_params
.op
= MTK_WMT_FUNC_CTRL
;
828 wmt_params
.dlen
= sizeof(param
);
829 wmt_params
.data
= ¶m
;
830 wmt_params
.status
= NULL
;
832 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
834 bt_dev_err(hdev
, "Failed to send wmt func ctrl (%d)", err
);
839 /* Apply the low power environment setup */
840 tci_sleep
.mode
= 0x5;
841 tci_sleep
.duration
= cpu_to_le16(0x640);
842 tci_sleep
.host_duration
= cpu_to_le16(0x640);
843 tci_sleep
.host_wakeup_pin
= 0;
844 tci_sleep
.time_compensation
= 0;
846 skb
= __hci_cmd_sync(hdev
, 0xfc7a, sizeof(tci_sleep
), &tci_sleep
,
850 bt_dev_err(hdev
, "Failed to apply low power setting (%d)", err
);
855 rettime
= ktime_get();
856 delta
= ktime_sub(rettime
, calltime
);
857 duration
= (unsigned long long)ktime_to_ns(delta
) >> 10;
859 pm_runtime_set_autosuspend_delay(bdev
->dev
,
860 MTKBTSDIO_AUTOSUSPEND_DELAY
);
861 pm_runtime_use_autosuspend(bdev
->dev
);
863 err
= pm_runtime_set_active(bdev
->dev
);
867 /* Default forbid runtime auto suspend, that can be allowed by
868 * enable_autosuspend flag or the PM runtime entry under sysfs.
870 pm_runtime_forbid(bdev
->dev
);
871 pm_runtime_enable(bdev
->dev
);
873 if (enable_autosuspend
)
874 pm_runtime_allow(bdev
->dev
);
876 bt_dev_info(hdev
, "Device setup in %llu usecs", duration
);
881 static int btmtksdio_shutdown(struct hci_dev
*hdev
)
883 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
884 struct btmtk_hci_wmt_params wmt_params
;
888 /* Get back the state to be consistent with the state
889 * in btmtksdio_setup.
891 pm_runtime_get_sync(bdev
->dev
);
893 /* Disable the device */
894 wmt_params
.op
= MTK_WMT_FUNC_CTRL
;
896 wmt_params
.dlen
= sizeof(param
);
897 wmt_params
.data
= ¶m
;
898 wmt_params
.status
= NULL
;
900 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
902 bt_dev_err(hdev
, "Failed to send wmt func ctrl (%d)", err
);
906 pm_runtime_put_noidle(bdev
->dev
);
907 pm_runtime_disable(bdev
->dev
);
912 static int btmtksdio_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
914 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
916 switch (hci_skb_pkt_type(skb
)) {
917 case HCI_COMMAND_PKT
:
921 case HCI_ACLDATA_PKT
:
925 case HCI_SCODATA_PKT
:
933 skb_queue_tail(&bdev
->txq
, skb
);
935 schedule_work(&bdev
->tx_work
);
940 static int btmtksdio_probe(struct sdio_func
*func
,
941 const struct sdio_device_id
*id
)
943 struct btmtksdio_dev
*bdev
;
944 struct hci_dev
*hdev
;
947 bdev
= devm_kzalloc(&func
->dev
, sizeof(*bdev
), GFP_KERNEL
);
951 bdev
->data
= (void *)id
->driver_data
;
955 bdev
->dev
= &func
->dev
;
958 INIT_WORK(&bdev
->tx_work
, btmtksdio_tx_work
);
959 skb_queue_head_init(&bdev
->txq
);
961 /* Initialize and register HCI device */
962 hdev
= hci_alloc_dev();
964 dev_err(&func
->dev
, "Can't allocate HCI device\n");
970 hdev
->bus
= HCI_SDIO
;
971 hci_set_drvdata(hdev
, bdev
);
973 hdev
->open
= btmtksdio_open
;
974 hdev
->close
= btmtksdio_close
;
975 hdev
->flush
= btmtksdio_flush
;
976 hdev
->setup
= btmtksdio_setup
;
977 hdev
->shutdown
= btmtksdio_shutdown
;
978 hdev
->send
= btmtksdio_send_frame
;
979 SET_HCIDEV_DEV(hdev
, &func
->dev
);
981 hdev
->manufacturer
= 70;
982 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP
, &hdev
->quirks
);
984 err
= hci_register_dev(hdev
);
986 dev_err(&func
->dev
, "Can't register HCI device\n");
991 sdio_set_drvdata(func
, bdev
);
993 /* pm_runtime_enable would be done after the firmware is being
994 * downloaded because the core layer probably already enables
995 * runtime PM for this func such as the case host->caps &
996 * MMC_CAP_POWER_OFF_CARD.
998 if (pm_runtime_enabled(bdev
->dev
))
999 pm_runtime_disable(bdev
->dev
);
1001 /* As explaination in drivers/mmc/core/sdio_bus.c tells us:
1002 * Unbound SDIO functions are always suspended.
1003 * During probe, the function is set active and the usage count
1004 * is incremented. If the driver supports runtime PM,
1005 * it should call pm_runtime_put_noidle() in its probe routine and
1006 * pm_runtime_get_noresume() in its remove routine.
1008 * So, put a pm_runtime_put_noidle here !
1010 pm_runtime_put_noidle(bdev
->dev
);
1015 static void btmtksdio_remove(struct sdio_func
*func
)
1017 struct btmtksdio_dev
*bdev
= sdio_get_drvdata(func
);
1018 struct hci_dev
*hdev
;
1023 /* Be consistent the state in btmtksdio_probe */
1024 pm_runtime_get_noresume(bdev
->dev
);
1028 sdio_set_drvdata(func
, NULL
);
1029 hci_unregister_dev(hdev
);
1034 static int btmtksdio_runtime_suspend(struct device
*dev
)
1036 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1037 struct btmtksdio_dev
*bdev
;
1041 bdev
= sdio_get_drvdata(func
);
1045 sdio_claim_host(bdev
->func
);
1047 sdio_writel(bdev
->func
, C_FW_OWN_REQ_SET
, MTK_REG_CHLPCR
, &err
);
1051 err
= readx_poll_timeout(btmtksdio_drv_own_query
, bdev
, status
,
1052 !(status
& C_COM_DRV_OWN
), 2000, 1000000);
1054 bt_dev_info(bdev
->hdev
, "status (%d) return ownership to device", err
);
1056 sdio_release_host(bdev
->func
);
1061 static int btmtksdio_runtime_resume(struct device
*dev
)
1063 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1064 struct btmtksdio_dev
*bdev
;
1068 bdev
= sdio_get_drvdata(func
);
1072 sdio_claim_host(bdev
->func
);
1074 sdio_writel(bdev
->func
, C_FW_OWN_REQ_CLR
, MTK_REG_CHLPCR
, &err
);
1078 err
= readx_poll_timeout(btmtksdio_drv_own_query
, bdev
, status
,
1079 status
& C_COM_DRV_OWN
, 2000, 1000000);
1081 bt_dev_info(bdev
->hdev
, "status (%d) get ownership from device", err
);
1083 sdio_release_host(bdev
->func
);
1088 static UNIVERSAL_DEV_PM_OPS(btmtksdio_pm_ops
, btmtksdio_runtime_suspend
,
1089 btmtksdio_runtime_resume
, NULL
);
1090 #define BTMTKSDIO_PM_OPS (&btmtksdio_pm_ops)
1091 #else /* CONFIG_PM */
1092 #define BTMTKSDIO_PM_OPS NULL
1093 #endif /* CONFIG_PM */
1095 static struct sdio_driver btmtksdio_driver
= {
1096 .name
= "btmtksdio",
1097 .probe
= btmtksdio_probe
,
1098 .remove
= btmtksdio_remove
,
1099 .id_table
= btmtksdio_table
,
1101 .owner
= THIS_MODULE
,
1102 .pm
= BTMTKSDIO_PM_OPS
,
1106 module_sdio_driver(btmtksdio_driver
);
1108 module_param(enable_autosuspend
, bool, 0644);
1109 MODULE_PARM_DESC(enable_autosuspend
, "Enable autosuspend by default");
1111 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1112 MODULE_DESCRIPTION("MediaTek Bluetooth SDIO driver ver " VERSION
);
1113 MODULE_VERSION(VERSION
);
1114 MODULE_LICENSE("GPL");
1115 MODULE_FIRMWARE(FIRMWARE_MT7663
);
1116 MODULE_FIRMWARE(FIRMWARE_MT7668
);