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