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