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
, 0x7663),
55 .driver_data
= (kernel_ulong_t
)&mt7663_data
},
56 {SDIO_DEVICE(SDIO_VENDOR_ID_MEDIATEK
, 0x7668),
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
, 0);
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
, 0);
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
);
699 /* The size of patch header is 30 bytes, should be skip */
709 wmt_params
.op
= MTK_WMT_PATCH_DWNLD
;
710 wmt_params
.status
= NULL
;
712 while (fw_size
> 0) {
713 dlen
= min_t(int, 250, fw_size
);
715 /* Tell device the position in sequence */
716 if (fw_size
- dlen
<= 0)
718 else if (fw_size
< fw
->size
- 30)
721 wmt_params
.flag
= flag
;
722 wmt_params
.dlen
= dlen
;
723 wmt_params
.data
= fw_ptr
;
725 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
727 bt_dev_err(hdev
, "Failed to send wmt patch dwnld (%d)",
736 wmt_params
.op
= MTK_WMT_RST
;
739 wmt_params
.data
= NULL
;
740 wmt_params
.status
= NULL
;
742 /* Activate funciton the firmware providing to */
743 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
745 bt_dev_err(hdev
, "Failed to send wmt rst (%d)", err
);
749 /* Wait a few moments for firmware activation done */
750 usleep_range(10000, 12000);
753 release_firmware(fw
);
757 static int btmtksdio_setup(struct hci_dev
*hdev
)
759 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
760 struct btmtk_hci_wmt_params wmt_params
;
761 ktime_t calltime
, delta
, rettime
;
762 struct btmtk_tci_sleep tci_sleep
;
763 unsigned long long duration
;
768 calltime
= ktime_get();
770 /* Query whether the firmware is already download */
771 wmt_params
.op
= MTK_WMT_SEMAPHORE
;
774 wmt_params
.data
= NULL
;
775 wmt_params
.status
= &status
;
777 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
779 bt_dev_err(hdev
, "Failed to query firmware status (%d)", err
);
783 if (status
== BTMTK_WMT_PATCH_DONE
) {
784 bt_dev_info(hdev
, "Firmware already downloaded");
785 goto ignore_setup_fw
;
788 /* Setup a firmware which the device definitely requires */
789 err
= mtk_setup_firmware(hdev
, bdev
->data
->fwname
);
794 /* Query whether the device is already enabled */
795 err
= readx_poll_timeout(btmtksdio_func_query
, hdev
, status
,
796 status
< 0 || status
!= BTMTK_WMT_ON_PROGRESS
,
798 /* -ETIMEDOUT happens */
802 /* The other errors happen in btusb_mtk_func_query */
806 if (status
== BTMTK_WMT_ON_DONE
) {
807 bt_dev_info(hdev
, "function already on");
811 /* Enable Bluetooth protocol */
812 wmt_params
.op
= MTK_WMT_FUNC_CTRL
;
814 wmt_params
.dlen
= sizeof(param
);
815 wmt_params
.data
= ¶m
;
816 wmt_params
.status
= NULL
;
818 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
820 bt_dev_err(hdev
, "Failed to send wmt func ctrl (%d)", err
);
825 /* Apply the low power environment setup */
826 tci_sleep
.mode
= 0x5;
827 tci_sleep
.duration
= cpu_to_le16(0x640);
828 tci_sleep
.host_duration
= cpu_to_le16(0x640);
829 tci_sleep
.host_wakeup_pin
= 0;
830 tci_sleep
.time_compensation
= 0;
832 skb
= __hci_cmd_sync(hdev
, 0xfc7a, sizeof(tci_sleep
), &tci_sleep
,
836 bt_dev_err(hdev
, "Failed to apply low power setting (%d)", err
);
841 rettime
= ktime_get();
842 delta
= ktime_sub(rettime
, calltime
);
843 duration
= (unsigned long long)ktime_to_ns(delta
) >> 10;
845 pm_runtime_set_autosuspend_delay(bdev
->dev
,
846 MTKBTSDIO_AUTOSUSPEND_DELAY
);
847 pm_runtime_use_autosuspend(bdev
->dev
);
849 err
= pm_runtime_set_active(bdev
->dev
);
853 /* Default forbid runtime auto suspend, that can be allowed by
854 * enable_autosuspend flag or the PM runtime entry under sysfs.
856 pm_runtime_forbid(bdev
->dev
);
857 pm_runtime_enable(bdev
->dev
);
859 if (enable_autosuspend
)
860 pm_runtime_allow(bdev
->dev
);
862 bt_dev_info(hdev
, "Device setup in %llu usecs", duration
);
867 static int btmtksdio_shutdown(struct hci_dev
*hdev
)
869 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
870 struct btmtk_hci_wmt_params wmt_params
;
874 /* Get back the state to be consistent with the state
875 * in btmtksdio_setup.
877 pm_runtime_get_sync(bdev
->dev
);
879 /* Disable the device */
880 wmt_params
.op
= MTK_WMT_FUNC_CTRL
;
882 wmt_params
.dlen
= sizeof(param
);
883 wmt_params
.data
= ¶m
;
884 wmt_params
.status
= NULL
;
886 err
= mtk_hci_wmt_sync(hdev
, &wmt_params
);
888 bt_dev_err(hdev
, "Failed to send wmt func ctrl (%d)", err
);
892 pm_runtime_put_noidle(bdev
->dev
);
893 pm_runtime_disable(bdev
->dev
);
898 static int btmtksdio_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
900 struct btmtksdio_dev
*bdev
= hci_get_drvdata(hdev
);
902 switch (hci_skb_pkt_type(skb
)) {
903 case HCI_COMMAND_PKT
:
907 case HCI_ACLDATA_PKT
:
911 case HCI_SCODATA_PKT
:
919 skb_queue_tail(&bdev
->txq
, skb
);
921 schedule_work(&bdev
->tx_work
);
926 static int btmtksdio_probe(struct sdio_func
*func
,
927 const struct sdio_device_id
*id
)
929 struct btmtksdio_dev
*bdev
;
930 struct hci_dev
*hdev
;
933 bdev
= devm_kzalloc(&func
->dev
, sizeof(*bdev
), GFP_KERNEL
);
937 bdev
->data
= (void *)id
->driver_data
;
941 bdev
->dev
= &func
->dev
;
944 INIT_WORK(&bdev
->tx_work
, btmtksdio_tx_work
);
945 skb_queue_head_init(&bdev
->txq
);
947 /* Initialize and register HCI device */
948 hdev
= hci_alloc_dev();
950 dev_err(&func
->dev
, "Can't allocate HCI device\n");
956 hdev
->bus
= HCI_SDIO
;
957 hci_set_drvdata(hdev
, bdev
);
959 hdev
->open
= btmtksdio_open
;
960 hdev
->close
= btmtksdio_close
;
961 hdev
->flush
= btmtksdio_flush
;
962 hdev
->setup
= btmtksdio_setup
;
963 hdev
->shutdown
= btmtksdio_shutdown
;
964 hdev
->send
= btmtksdio_send_frame
;
965 SET_HCIDEV_DEV(hdev
, &func
->dev
);
967 hdev
->manufacturer
= 70;
968 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP
, &hdev
->quirks
);
970 err
= hci_register_dev(hdev
);
972 dev_err(&func
->dev
, "Can't register HCI device\n");
977 sdio_set_drvdata(func
, bdev
);
979 /* pm_runtime_enable would be done after the firmware is being
980 * downloaded because the core layer probably already enables
981 * runtime PM for this func such as the case host->caps &
982 * MMC_CAP_POWER_OFF_CARD.
984 if (pm_runtime_enabled(bdev
->dev
))
985 pm_runtime_disable(bdev
->dev
);
987 /* As explaination in drivers/mmc/core/sdio_bus.c tells us:
988 * Unbound SDIO functions are always suspended.
989 * During probe, the function is set active and the usage count
990 * is incremented. If the driver supports runtime PM,
991 * it should call pm_runtime_put_noidle() in its probe routine and
992 * pm_runtime_get_noresume() in its remove routine.
994 * So, put a pm_runtime_put_noidle here !
996 pm_runtime_put_noidle(bdev
->dev
);
1001 static void btmtksdio_remove(struct sdio_func
*func
)
1003 struct btmtksdio_dev
*bdev
= sdio_get_drvdata(func
);
1004 struct hci_dev
*hdev
;
1009 /* Be consistent the state in btmtksdio_probe */
1010 pm_runtime_get_noresume(bdev
->dev
);
1014 sdio_set_drvdata(func
, NULL
);
1015 hci_unregister_dev(hdev
);
1020 static int btmtksdio_runtime_suspend(struct device
*dev
)
1022 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1023 struct btmtksdio_dev
*bdev
;
1027 bdev
= sdio_get_drvdata(func
);
1031 sdio_claim_host(bdev
->func
);
1033 sdio_writel(bdev
->func
, C_FW_OWN_REQ_SET
, MTK_REG_CHLPCR
, &err
);
1037 err
= readx_poll_timeout(btmtksdio_drv_own_query
, bdev
, status
,
1038 !(status
& C_COM_DRV_OWN
), 2000, 1000000);
1040 bt_dev_info(bdev
->hdev
, "status (%d) return ownership to device", err
);
1042 sdio_release_host(bdev
->func
);
1047 static int btmtksdio_runtime_resume(struct device
*dev
)
1049 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1050 struct btmtksdio_dev
*bdev
;
1054 bdev
= sdio_get_drvdata(func
);
1058 sdio_claim_host(bdev
->func
);
1060 sdio_writel(bdev
->func
, C_FW_OWN_REQ_CLR
, MTK_REG_CHLPCR
, &err
);
1064 err
= readx_poll_timeout(btmtksdio_drv_own_query
, bdev
, status
,
1065 status
& C_COM_DRV_OWN
, 2000, 1000000);
1067 bt_dev_info(bdev
->hdev
, "status (%d) get ownership from device", err
);
1069 sdio_release_host(bdev
->func
);
1074 static UNIVERSAL_DEV_PM_OPS(btmtksdio_pm_ops
, btmtksdio_runtime_suspend
,
1075 btmtksdio_runtime_resume
, NULL
);
1076 #define BTMTKSDIO_PM_OPS (&btmtksdio_pm_ops)
1077 #else /* CONFIG_PM */
1078 #define BTMTKSDIO_PM_OPS NULL
1079 #endif /* CONFIG_PM */
1081 static struct sdio_driver btmtksdio_driver
= {
1082 .name
= "btmtksdio",
1083 .probe
= btmtksdio_probe
,
1084 .remove
= btmtksdio_remove
,
1085 .id_table
= btmtksdio_table
,
1087 .owner
= THIS_MODULE
,
1088 .pm
= BTMTKSDIO_PM_OPS
,
1092 module_sdio_driver(btmtksdio_driver
);
1094 module_param(enable_autosuspend
, bool, 0644);
1095 MODULE_PARM_DESC(enable_autosuspend
, "Enable autosuspend by default");
1097 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1098 MODULE_DESCRIPTION("MediaTek Bluetooth SDIO driver ver " VERSION
);
1099 MODULE_VERSION(VERSION
);
1100 MODULE_LICENSE("GPL");
1101 MODULE_FIRMWARE(FIRMWARE_MT7663
);
1102 MODULE_FIRMWARE(FIRMWARE_MT7668
);