1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 MediaTek Inc.
5 * Bluetooth support for MediaTek serial devices
7 * Author: Sean Wang <sean.wang@mediatek.com>
11 #include <asm/unaligned.h>
12 #include <linux/atomic.h>
13 #include <linux/clk.h>
14 #include <linux/firmware.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/iopoll.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/serdev.h>
25 #include <linux/skbuff.h>
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
35 #define MTK_STP_TLR_SIZE 2
37 #define BTMTKUART_TX_STATE_ACTIVE 1
38 #define BTMTKUART_TX_STATE_WAKEUP 2
39 #define BTMTKUART_TX_WAIT_VND_EVT 3
40 #define BTMTKUART_REQUIRED_WAKEUP 4
42 #define BTMTKUART_FLAG_STANDALONE_HW BIT(0)
50 struct btmtkuart_data {
55 struct btmtkuart_dev {
57 struct serdev_device *serdev;
61 struct regulator *vcc;
62 struct gpio_desc *reset;
63 struct gpio_desc *boot;
64 struct pinctrl *pinctrl;
65 struct pinctrl_state *pins_runtime;
66 struct pinctrl_state *pins_boot;
67 speed_t desired_speed;
70 struct work_struct tx_work;
71 unsigned long tx_state;
72 struct sk_buff_head txq;
74 struct sk_buff *rx_skb;
75 struct sk_buff *evt_skb;
81 const struct btmtkuart_data *data;
84 #define btmtkuart_is_standalone(bdev) \
85 ((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
86 #define btmtkuart_is_builtin_soc(bdev) \
87 !((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
89 static int mtk_hci_wmt_sync(struct hci_dev *hdev,
90 struct btmtk_hci_wmt_params *wmt_params)
92 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
93 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
94 u32 hlen, status = BTMTK_WMT_INVALID;
95 struct btmtk_hci_wmt_evt *wmt_evt;
96 struct btmtk_hci_wmt_cmd *wc;
97 struct btmtk_wmt_hdr *hdr;
100 /* Send the WMT command and wait until the WMT event returns */
101 hlen = sizeof(*hdr) + wmt_params->dlen;
107 wc = kzalloc(hlen, GFP_KERNEL);
115 hdr->op = wmt_params->op;
116 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
117 hdr->flag = wmt_params->flag;
118 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
120 set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
122 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
124 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
128 /* The vendor specific WMT commands are all answered by a vendor
129 * specific event and will not have the Command Status or Command
130 * Complete as with usual HCI command flow control.
132 * After sending the command, wait for BTMTKUART_TX_WAIT_VND_EVT
133 * state to be cleared. The driver specific event receive routine
134 * will clear that state and with that indicate completion of the
137 err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT,
138 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
140 bt_dev_err(hdev, "Execution of wmt command interrupted");
141 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
146 bt_dev_err(hdev, "Execution of wmt command timed out");
147 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
152 /* Parse and handle the return WMT event */
153 wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data;
154 if (wmt_evt->whdr.op != hdr->op) {
155 bt_dev_err(hdev, "Wrong op received %d expected %d",
156 wmt_evt->whdr.op, hdr->op);
161 switch (wmt_evt->whdr.op) {
162 case BTMTK_WMT_SEMAPHORE:
163 if (wmt_evt->whdr.flag == 2)
164 status = BTMTK_WMT_PATCH_UNDONE;
166 status = BTMTK_WMT_PATCH_DONE;
168 case BTMTK_WMT_FUNC_CTRL:
169 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
170 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
171 status = BTMTK_WMT_ON_DONE;
172 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
173 status = BTMTK_WMT_ON_PROGRESS;
175 status = BTMTK_WMT_ON_UNDONE;
179 if (wmt_params->status)
180 *wmt_params->status = status;
185 kfree_skb(bdev->evt_skb);
186 bdev->evt_skb = NULL;
191 static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
193 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
194 struct hci_event_hdr *hdr = (void *)skb->data;
197 /* When someone waits for the WMT event, the skb is being cloned
198 * and being processed the events from there then.
200 if (test_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) {
201 bdev->evt_skb = skb_clone(skb, GFP_KERNEL);
202 if (!bdev->evt_skb) {
208 err = hci_recv_frame(hdev, skb);
212 if (hdr->evt == HCI_EV_WMT) {
213 if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT,
215 /* Barrier to sync with other CPUs */
216 smp_mb__after_atomic();
217 wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT);
224 kfree_skb(bdev->evt_skb);
225 bdev->evt_skb = NULL;
231 static const struct h4_recv_pkt mtk_recv_pkts[] = {
232 { H4_RECV_ACL, .recv = hci_recv_frame },
233 { H4_RECV_SCO, .recv = hci_recv_frame },
234 { H4_RECV_EVENT, .recv = btmtkuart_recv_event },
237 static void btmtkuart_tx_work(struct work_struct *work)
239 struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev,
241 struct serdev_device *serdev = bdev->serdev;
242 struct hci_dev *hdev = bdev->hdev;
245 clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
248 struct sk_buff *skb = skb_dequeue(&bdev->txq);
254 len = serdev_device_write_buf(serdev, skb->data,
256 hdev->stat.byte_tx += len;
260 skb_queue_head(&bdev->txq, skb);
264 switch (hci_skb_pkt_type(skb)) {
265 case HCI_COMMAND_PKT:
268 case HCI_ACLDATA_PKT:
271 case HCI_SCODATA_PKT:
279 if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state))
283 clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state);
286 static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev)
288 if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state))
289 set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
291 schedule_work(&bdev->tx_work);
294 static const unsigned char *
295 mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count,
298 struct mtk_stp_hdr *shdr;
300 /* The cursor is reset when all the data of STP is consumed out */
301 if (!bdev->stp_dlen && bdev->stp_cursor >= 6)
302 bdev->stp_cursor = 0;
304 /* Filling pad until all STP info is obtained */
305 while (bdev->stp_cursor < 6 && count > 0) {
306 bdev->stp_pad[bdev->stp_cursor] = *data;
312 /* Retrieve STP info and have a sanity check */
313 if (!bdev->stp_dlen && bdev->stp_cursor >= 6) {
314 shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2];
315 bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff;
317 /* Resync STP when unexpected data is being read */
318 if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) {
319 bt_dev_err(bdev->hdev, "stp format unexpect (%d, %d)",
320 shdr->prefix, bdev->stp_dlen);
321 bdev->stp_cursor = 2;
326 /* Directly quit when there's no data found for H4 can process */
330 /* Tranlate to how much the size of data H4 can handle so far */
331 *sz_h4 = min_t(int, count, bdev->stp_dlen);
333 /* Update the remaining size of STP packet */
334 bdev->stp_dlen -= *sz_h4;
336 /* Data points to STP payload which can be handled by H4 */
340 static int btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count)
342 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
343 const unsigned char *p_left = data, *p_h4;
344 int sz_left = count, sz_h4, adv;
347 while (sz_left > 0) {
348 /* The serial data received from MT7622 BT controller is
349 * at all time padded around with the STP header and tailer.
351 * A full STP packet is looking like
352 * -----------------------------------
353 * | STP header | H:4 | STP tailer |
354 * -----------------------------------
355 * but it doesn't guarantee to contain a full H:4 packet which
356 * means that it's possible for multiple STP packets forms a
357 * full H:4 packet that means extra STP header + length doesn't
358 * indicate a full H:4 frame, things can fragment. Whose length
359 * recorded in STP header just shows up the most length the
360 * H:4 engine can handle currently.
363 p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4);
371 bdev->rx_skb = h4_recv_buf(bdev->hdev, bdev->rx_skb, p_h4,
372 sz_h4, mtk_recv_pkts,
373 ARRAY_SIZE(mtk_recv_pkts));
374 if (IS_ERR(bdev->rx_skb)) {
375 err = PTR_ERR(bdev->rx_skb);
376 bt_dev_err(bdev->hdev,
377 "Frame reassembly failed (%d)", err);
389 static int btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data,
392 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
395 err = btmtkuart_recv(bdev->hdev, data, count);
399 bdev->hdev->stat.byte_rx += count;
404 static void btmtkuart_write_wakeup(struct serdev_device *serdev)
406 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
408 btmtkuart_tx_wakeup(bdev);
411 static const struct serdev_device_ops btmtkuart_client_ops = {
412 .receive_buf = btmtkuart_receive_buf,
413 .write_wakeup = btmtkuart_write_wakeup,
416 static int btmtkuart_open(struct hci_dev *hdev)
418 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
422 err = serdev_device_open(bdev->serdev);
424 bt_dev_err(hdev, "Unable to open UART device %s",
425 dev_name(&bdev->serdev->dev));
429 if (btmtkuart_is_standalone(bdev)) {
430 if (bdev->curr_speed != bdev->desired_speed)
431 err = serdev_device_set_baudrate(bdev->serdev,
434 err = serdev_device_set_baudrate(bdev->serdev,
435 bdev->desired_speed);
438 bt_dev_err(hdev, "Unable to set baudrate UART device %s",
439 dev_name(&bdev->serdev->dev));
440 goto err_serdev_close;
443 serdev_device_set_flow_control(bdev->serdev, false);
446 bdev->stp_cursor = 2;
449 dev = &bdev->serdev->dev;
451 /* Enable the power domain and clock the device requires */
452 pm_runtime_enable(dev);
453 err = pm_runtime_resume_and_get(dev);
455 goto err_disable_rpm;
457 err = clk_prepare_enable(bdev->clk);
464 pm_runtime_put_sync(dev);
466 pm_runtime_disable(dev);
468 serdev_device_close(bdev->serdev);
473 static int btmtkuart_close(struct hci_dev *hdev)
475 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
476 struct device *dev = &bdev->serdev->dev;
478 /* Shutdown the clock and power domain the device requires */
479 clk_disable_unprepare(bdev->clk);
480 pm_runtime_put_sync(dev);
481 pm_runtime_disable(dev);
483 serdev_device_close(bdev->serdev);
488 static int btmtkuart_flush(struct hci_dev *hdev)
490 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
492 /* Flush any pending characters */
493 serdev_device_write_flush(bdev->serdev);
494 skb_queue_purge(&bdev->txq);
496 cancel_work_sync(&bdev->tx_work);
498 kfree_skb(bdev->rx_skb);
501 bdev->stp_cursor = 2;
507 static int btmtkuart_func_query(struct hci_dev *hdev)
509 struct btmtk_hci_wmt_params wmt_params;
513 /* Query whether the function is enabled */
514 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
516 wmt_params.dlen = sizeof(param);
517 wmt_params.data = ¶m;
518 wmt_params.status = &status;
520 err = mtk_hci_wmt_sync(hdev, &wmt_params);
522 bt_dev_err(hdev, "Failed to query function status (%d)", err);
529 static int btmtkuart_change_baudrate(struct hci_dev *hdev)
531 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
532 struct btmtk_hci_wmt_params wmt_params;
537 /* Indicate the device to enter the probe state the host is
538 * ready to change a new baudrate.
540 baudrate = cpu_to_le32(bdev->desired_speed);
541 wmt_params.op = BTMTK_WMT_HIF;
544 wmt_params.data = &baudrate;
545 wmt_params.status = NULL;
547 err = mtk_hci_wmt_sync(hdev, &wmt_params);
549 bt_dev_err(hdev, "Failed to device baudrate (%d)", err);
553 err = serdev_device_set_baudrate(bdev->serdev,
554 bdev->desired_speed);
556 bt_dev_err(hdev, "Failed to set up host baudrate (%d)",
561 serdev_device_set_flow_control(bdev->serdev, false);
563 /* Send a dummy byte 0xff to activate the new baudrate */
565 err = serdev_device_write_buf(bdev->serdev, ¶m, sizeof(param));
566 if (err < 0 || err < sizeof(param))
569 serdev_device_wait_until_sent(bdev->serdev, 0);
571 /* Wait some time for the device changing baudrate done */
572 usleep_range(20000, 22000);
574 /* Test the new baudrate */
575 wmt_params.op = BTMTK_WMT_TEST;
578 wmt_params.data = NULL;
579 wmt_params.status = NULL;
581 err = mtk_hci_wmt_sync(hdev, &wmt_params);
583 bt_dev_err(hdev, "Failed to test new baudrate (%d)",
588 bdev->curr_speed = bdev->desired_speed;
593 static int btmtkuart_setup(struct hci_dev *hdev)
595 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
596 struct btmtk_hci_wmt_params wmt_params;
597 ktime_t calltime, delta, rettime;
598 struct btmtk_tci_sleep tci_sleep;
599 unsigned long long duration;
604 calltime = ktime_get();
606 /* Wakeup MCUSYS is required for certain devices before we start to
609 if (test_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state)) {
610 wmt_params.op = BTMTK_WMT_WAKEUP;
613 wmt_params.data = NULL;
614 wmt_params.status = NULL;
616 err = mtk_hci_wmt_sync(hdev, &wmt_params);
618 bt_dev_err(hdev, "Failed to wakeup the chip (%d)", err);
622 clear_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
625 if (btmtkuart_is_standalone(bdev))
626 btmtkuart_change_baudrate(hdev);
628 /* Query whether the firmware is already download */
629 wmt_params.op = BTMTK_WMT_SEMAPHORE;
632 wmt_params.data = NULL;
633 wmt_params.status = &status;
635 err = mtk_hci_wmt_sync(hdev, &wmt_params);
637 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
641 if (status == BTMTK_WMT_PATCH_DONE) {
642 bt_dev_info(hdev, "Firmware already downloaded");
643 goto ignore_setup_fw;
646 /* Setup a firmware which the device definitely requires */
647 err = btmtk_setup_firmware(hdev, bdev->data->fwname, mtk_hci_wmt_sync);
652 /* Query whether the device is already enabled */
653 err = readx_poll_timeout(btmtkuart_func_query, hdev, status,
654 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
656 /* -ETIMEDOUT happens */
660 /* The other errors happen in btusb_mtk_func_query */
664 if (status == BTMTK_WMT_ON_DONE) {
665 bt_dev_info(hdev, "function already on");
669 /* Enable Bluetooth protocol */
670 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
672 wmt_params.dlen = sizeof(param);
673 wmt_params.data = ¶m;
674 wmt_params.status = NULL;
676 err = mtk_hci_wmt_sync(hdev, &wmt_params);
678 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
683 /* Apply the low power environment setup */
684 tci_sleep.mode = 0x5;
685 tci_sleep.duration = cpu_to_le16(0x640);
686 tci_sleep.host_duration = cpu_to_le16(0x640);
687 tci_sleep.host_wakeup_pin = 0;
688 tci_sleep.time_compensation = 0;
690 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
694 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
699 rettime = ktime_get();
700 delta = ktime_sub(rettime, calltime);
701 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
703 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
708 static int btmtkuart_shutdown(struct hci_dev *hdev)
710 struct btmtk_hci_wmt_params wmt_params;
714 /* Disable the device */
715 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
717 wmt_params.dlen = sizeof(param);
718 wmt_params.data = ¶m;
719 wmt_params.status = NULL;
721 err = mtk_hci_wmt_sync(hdev, &wmt_params);
723 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
730 static int btmtkuart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
732 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
733 struct mtk_stp_hdr *shdr;
734 int err, dlen, type = 0;
736 /* Prepend skb with frame type */
737 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
739 /* Make sure that there is enough rooms for STP header and trailer */
740 if (unlikely(skb_headroom(skb) < sizeof(*shdr)) ||
741 (skb_tailroom(skb) < MTK_STP_TLR_SIZE)) {
742 err = pskb_expand_head(skb, sizeof(*shdr), MTK_STP_TLR_SIZE,
748 /* Add the STP header */
750 shdr = skb_push(skb, sizeof(*shdr));
752 shdr->dlen = cpu_to_be16((dlen & 0x0fff) | (type << 12));
753 shdr->cs = 0; /* MT7622 doesn't care about checksum value */
755 /* Add the STP trailer */
756 skb_put_zero(skb, MTK_STP_TLR_SIZE);
758 skb_queue_tail(&bdev->txq, skb);
760 btmtkuart_tx_wakeup(bdev);
764 static int btmtkuart_parse_dt(struct serdev_device *serdev)
766 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
767 struct device_node *node = serdev->dev.of_node;
771 if (btmtkuart_is_standalone(bdev)) {
772 of_property_read_u32(node, "current-speed", &speed);
774 bdev->desired_speed = speed;
776 bdev->vcc = devm_regulator_get(&serdev->dev, "vcc");
777 if (IS_ERR(bdev->vcc)) {
778 err = PTR_ERR(bdev->vcc);
782 bdev->osc = devm_clk_get_optional(&serdev->dev, "osc");
783 if (IS_ERR(bdev->osc)) {
784 err = PTR_ERR(bdev->osc);
788 bdev->boot = devm_gpiod_get_optional(&serdev->dev, "boot",
790 if (IS_ERR(bdev->boot)) {
791 err = PTR_ERR(bdev->boot);
795 bdev->pinctrl = devm_pinctrl_get(&serdev->dev);
796 if (IS_ERR(bdev->pinctrl)) {
797 err = PTR_ERR(bdev->pinctrl);
801 bdev->pins_boot = pinctrl_lookup_state(bdev->pinctrl,
803 if (IS_ERR(bdev->pins_boot) && !bdev->boot) {
804 err = PTR_ERR(bdev->pins_boot);
805 dev_err(&serdev->dev,
806 "Should assign RXD to LOW at boot stage\n");
810 bdev->pins_runtime = pinctrl_lookup_state(bdev->pinctrl,
812 if (IS_ERR(bdev->pins_runtime)) {
813 err = PTR_ERR(bdev->pins_runtime);
817 bdev->reset = devm_gpiod_get_optional(&serdev->dev, "reset",
819 if (IS_ERR(bdev->reset)) {
820 err = PTR_ERR(bdev->reset);
823 } else if (btmtkuart_is_builtin_soc(bdev)) {
824 bdev->clk = devm_clk_get(&serdev->dev, "ref");
825 if (IS_ERR(bdev->clk))
826 return PTR_ERR(bdev->clk);
832 static int btmtkuart_probe(struct serdev_device *serdev)
834 struct btmtkuart_dev *bdev;
835 struct hci_dev *hdev;
838 bdev = devm_kzalloc(&serdev->dev, sizeof(*bdev), GFP_KERNEL);
842 bdev->data = of_device_get_match_data(&serdev->dev);
846 bdev->serdev = serdev;
847 serdev_device_set_drvdata(serdev, bdev);
849 serdev_device_set_client_ops(serdev, &btmtkuart_client_ops);
851 err = btmtkuart_parse_dt(serdev);
855 INIT_WORK(&bdev->tx_work, btmtkuart_tx_work);
856 skb_queue_head_init(&bdev->txq);
858 /* Initialize and register HCI device */
859 hdev = hci_alloc_dev();
861 dev_err(&serdev->dev, "Can't allocate HCI device\n");
867 hdev->bus = HCI_UART;
868 hci_set_drvdata(hdev, bdev);
870 hdev->open = btmtkuart_open;
871 hdev->close = btmtkuart_close;
872 hdev->flush = btmtkuart_flush;
873 hdev->setup = btmtkuart_setup;
874 hdev->shutdown = btmtkuart_shutdown;
875 hdev->send = btmtkuart_send_frame;
876 hdev->set_bdaddr = btmtk_set_bdaddr;
877 SET_HCIDEV_DEV(hdev, &serdev->dev);
879 hdev->manufacturer = 70;
880 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
882 if (btmtkuart_is_standalone(bdev)) {
883 err = clk_prepare_enable(bdev->osc);
885 goto err_hci_free_dev;
888 gpiod_set_value_cansleep(bdev->boot, 1);
890 /* Switch to the specific pin state for the booting
893 pinctrl_select_state(bdev->pinctrl, bdev->pins_boot);
897 err = regulator_enable(bdev->vcc);
899 goto err_clk_disable_unprepare;
901 /* Reset if the reset-gpios is available otherwise the board
902 * -level design should be guaranteed.
905 gpiod_set_value_cansleep(bdev->reset, 1);
906 usleep_range(1000, 2000);
907 gpiod_set_value_cansleep(bdev->reset, 0);
910 /* Wait some time until device got ready and switch to the pin
911 * mode the device requires for UART transfers.
916 devm_gpiod_put(&serdev->dev, bdev->boot);
918 pinctrl_select_state(bdev->pinctrl, bdev->pins_runtime);
920 /* A standalone device doesn't depends on power domain on SoC,
921 * so mark it as no callbacks.
923 pm_runtime_no_callbacks(&serdev->dev);
925 set_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
928 err = hci_register_dev(hdev);
930 dev_err(&serdev->dev, "Can't register HCI device\n");
931 goto err_regulator_disable;
936 err_regulator_disable:
937 if (btmtkuart_is_standalone(bdev))
938 regulator_disable(bdev->vcc);
939 err_clk_disable_unprepare:
940 if (btmtkuart_is_standalone(bdev))
941 clk_disable_unprepare(bdev->osc);
948 static void btmtkuart_remove(struct serdev_device *serdev)
950 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
951 struct hci_dev *hdev = bdev->hdev;
953 if (btmtkuart_is_standalone(bdev)) {
954 regulator_disable(bdev->vcc);
955 clk_disable_unprepare(bdev->osc);
958 hci_unregister_dev(hdev);
962 static const struct btmtkuart_data mt7622_data = {
963 .fwname = FIRMWARE_MT7622,
966 static const struct btmtkuart_data mt7663_data = {
967 .flags = BTMTKUART_FLAG_STANDALONE_HW,
968 .fwname = FIRMWARE_MT7663,
971 static const struct btmtkuart_data mt7668_data = {
972 .flags = BTMTKUART_FLAG_STANDALONE_HW,
973 .fwname = FIRMWARE_MT7668,
977 static const struct of_device_id mtk_of_match_table[] = {
978 { .compatible = "mediatek,mt7622-bluetooth", .data = &mt7622_data},
979 { .compatible = "mediatek,mt7663u-bluetooth", .data = &mt7663_data},
980 { .compatible = "mediatek,mt7668u-bluetooth", .data = &mt7668_data},
983 MODULE_DEVICE_TABLE(of, mtk_of_match_table);
986 static struct serdev_device_driver btmtkuart_driver = {
987 .probe = btmtkuart_probe,
988 .remove = btmtkuart_remove,
991 .of_match_table = of_match_ptr(mtk_of_match_table),
995 module_serdev_device_driver(btmtkuart_driver);
997 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
998 MODULE_DESCRIPTION("MediaTek Bluetooth Serial driver ver " VERSION);
999 MODULE_VERSION(VERSION);
1000 MODULE_LICENSE("GPL");