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/pinctrl/consumer.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/serdev.h>
24 #include <linux/skbuff.h>
26 #include <net/bluetooth/bluetooth.h>
27 #include <net/bluetooth/hci_core.h>
34 #define MTK_STP_TLR_SIZE 2
36 #define BTMTKUART_TX_STATE_ACTIVE 1
37 #define BTMTKUART_TX_STATE_WAKEUP 2
38 #define BTMTKUART_TX_WAIT_VND_EVT 3
39 #define BTMTKUART_REQUIRED_WAKEUP 4
41 #define BTMTKUART_FLAG_STANDALONE_HW BIT(0)
49 struct btmtkuart_data {
54 struct btmtkuart_dev {
56 struct serdev_device *serdev;
60 struct regulator *vcc;
61 struct gpio_desc *reset;
62 struct gpio_desc *boot;
63 struct pinctrl *pinctrl;
64 struct pinctrl_state *pins_runtime;
65 struct pinctrl_state *pins_boot;
66 speed_t desired_speed;
69 struct work_struct tx_work;
70 unsigned long tx_state;
71 struct sk_buff_head txq;
73 struct sk_buff *rx_skb;
74 struct sk_buff *evt_skb;
80 const struct btmtkuart_data *data;
83 #define btmtkuart_is_standalone(bdev) \
84 ((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
85 #define btmtkuart_is_builtin_soc(bdev) \
86 !((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
88 static int mtk_hci_wmt_sync(struct hci_dev *hdev,
89 struct btmtk_hci_wmt_params *wmt_params)
91 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
92 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
93 u32 hlen, status = BTMTK_WMT_INVALID;
94 struct btmtk_hci_wmt_evt *wmt_evt;
95 struct btmtk_hci_wmt_cmd *wc;
96 struct btmtk_wmt_hdr *hdr;
99 /* Send the WMT command and wait until the WMT event returns */
100 hlen = sizeof(*hdr) + wmt_params->dlen;
106 wc = kzalloc(hlen, GFP_KERNEL);
114 hdr->op = wmt_params->op;
115 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
116 hdr->flag = wmt_params->flag;
117 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
119 set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
121 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
123 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
127 /* The vendor specific WMT commands are all answered by a vendor
128 * specific event and will not have the Command Status or Command
129 * Complete as with usual HCI command flow control.
131 * After sending the command, wait for BTMTKUART_TX_WAIT_VND_EVT
132 * state to be cleared. The driver specific event receive routine
133 * will clear that state and with that indicate completion of the
136 err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT,
137 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
139 bt_dev_err(hdev, "Execution of wmt command interrupted");
140 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
145 bt_dev_err(hdev, "Execution of wmt command timed out");
146 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
151 /* Parse and handle the return WMT event */
152 wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data;
153 if (wmt_evt->whdr.op != hdr->op) {
154 bt_dev_err(hdev, "Wrong op received %d expected %d",
155 wmt_evt->whdr.op, hdr->op);
160 switch (wmt_evt->whdr.op) {
161 case BTMTK_WMT_SEMAPHORE:
162 if (wmt_evt->whdr.flag == 2)
163 status = BTMTK_WMT_PATCH_UNDONE;
165 status = BTMTK_WMT_PATCH_DONE;
167 case BTMTK_WMT_FUNC_CTRL:
168 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
169 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
170 status = BTMTK_WMT_ON_DONE;
171 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
172 status = BTMTK_WMT_ON_PROGRESS;
174 status = BTMTK_WMT_ON_UNDONE;
178 if (wmt_params->status)
179 *wmt_params->status = status;
184 kfree_skb(bdev->evt_skb);
185 bdev->evt_skb = NULL;
190 static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
192 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
193 struct hci_event_hdr *hdr = (void *)skb->data;
196 /* When someone waits for the WMT event, the skb is being cloned
197 * and being processed the events from there then.
199 if (test_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) {
200 bdev->evt_skb = skb_clone(skb, GFP_KERNEL);
201 if (!bdev->evt_skb) {
207 err = hci_recv_frame(hdev, skb);
211 if (hdr->evt == HCI_EV_WMT) {
212 if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT,
214 /* Barrier to sync with other CPUs */
215 smp_mb__after_atomic();
216 wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT);
223 kfree_skb(bdev->evt_skb);
224 bdev->evt_skb = NULL;
230 static const struct h4_recv_pkt mtk_recv_pkts[] = {
231 { H4_RECV_ACL, .recv = hci_recv_frame },
232 { H4_RECV_SCO, .recv = hci_recv_frame },
233 { H4_RECV_EVENT, .recv = btmtkuart_recv_event },
236 static void btmtkuart_tx_work(struct work_struct *work)
238 struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev,
240 struct serdev_device *serdev = bdev->serdev;
241 struct hci_dev *hdev = bdev->hdev;
244 clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
247 struct sk_buff *skb = skb_dequeue(&bdev->txq);
253 len = serdev_device_write_buf(serdev, skb->data,
255 hdev->stat.byte_tx += len;
259 skb_queue_head(&bdev->txq, skb);
263 switch (hci_skb_pkt_type(skb)) {
264 case HCI_COMMAND_PKT:
267 case HCI_ACLDATA_PKT:
270 case HCI_SCODATA_PKT:
278 if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state))
282 clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state);
285 static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev)
287 if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state))
288 set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
290 schedule_work(&bdev->tx_work);
293 static const unsigned char *
294 mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count,
297 struct mtk_stp_hdr *shdr;
299 /* The cursor is reset when all the data of STP is consumed out */
300 if (!bdev->stp_dlen && bdev->stp_cursor >= 6)
301 bdev->stp_cursor = 0;
303 /* Filling pad until all STP info is obtained */
304 while (bdev->stp_cursor < 6 && count > 0) {
305 bdev->stp_pad[bdev->stp_cursor] = *data;
311 /* Retrieve STP info and have a sanity check */
312 if (!bdev->stp_dlen && bdev->stp_cursor >= 6) {
313 shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2];
314 bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff;
316 /* Resync STP when unexpected data is being read */
317 if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) {
318 bt_dev_err(bdev->hdev, "stp format unexpect (%d, %d)",
319 shdr->prefix, bdev->stp_dlen);
320 bdev->stp_cursor = 2;
325 /* Directly quit when there's no data found for H4 can process */
329 /* Tranlate to how much the size of data H4 can handle so far */
330 *sz_h4 = min_t(int, count, bdev->stp_dlen);
332 /* Update the remaining size of STP packet */
333 bdev->stp_dlen -= *sz_h4;
335 /* Data points to STP payload which can be handled by H4 */
339 static int btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count)
341 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
342 const unsigned char *p_left = data, *p_h4;
343 int sz_left = count, sz_h4, adv;
346 while (sz_left > 0) {
347 /* The serial data received from MT7622 BT controller is
348 * at all time padded around with the STP header and tailer.
350 * A full STP packet is looking like
351 * -----------------------------------
352 * | STP header | H:4 | STP tailer |
353 * -----------------------------------
354 * but it doesn't guarantee to contain a full H:4 packet which
355 * means that it's possible for multiple STP packets forms a
356 * full H:4 packet that means extra STP header + length doesn't
357 * indicate a full H:4 frame, things can fragment. Whose length
358 * recorded in STP header just shows up the most length the
359 * H:4 engine can handle currently.
362 p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4);
370 bdev->rx_skb = h4_recv_buf(bdev->hdev, bdev->rx_skb, p_h4,
371 sz_h4, mtk_recv_pkts,
372 ARRAY_SIZE(mtk_recv_pkts));
373 if (IS_ERR(bdev->rx_skb)) {
374 err = PTR_ERR(bdev->rx_skb);
375 bt_dev_err(bdev->hdev,
376 "Frame reassembly failed (%d)", err);
388 static int btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data,
391 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
394 err = btmtkuart_recv(bdev->hdev, data, count);
398 bdev->hdev->stat.byte_rx += count;
403 static void btmtkuart_write_wakeup(struct serdev_device *serdev)
405 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
407 btmtkuart_tx_wakeup(bdev);
410 static const struct serdev_device_ops btmtkuart_client_ops = {
411 .receive_buf = btmtkuart_receive_buf,
412 .write_wakeup = btmtkuart_write_wakeup,
415 static int btmtkuart_open(struct hci_dev *hdev)
417 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
421 err = serdev_device_open(bdev->serdev);
423 bt_dev_err(hdev, "Unable to open UART device %s",
424 dev_name(&bdev->serdev->dev));
428 if (btmtkuart_is_standalone(bdev)) {
429 if (bdev->curr_speed != bdev->desired_speed)
430 err = serdev_device_set_baudrate(bdev->serdev,
433 err = serdev_device_set_baudrate(bdev->serdev,
434 bdev->desired_speed);
437 bt_dev_err(hdev, "Unable to set baudrate UART device %s",
438 dev_name(&bdev->serdev->dev));
439 goto err_serdev_close;
442 serdev_device_set_flow_control(bdev->serdev, false);
445 bdev->stp_cursor = 2;
448 dev = &bdev->serdev->dev;
450 /* Enable the power domain and clock the device requires */
451 pm_runtime_enable(dev);
452 err = pm_runtime_resume_and_get(dev);
454 goto err_disable_rpm;
456 err = clk_prepare_enable(bdev->clk);
463 pm_runtime_put_sync(dev);
465 pm_runtime_disable(dev);
467 serdev_device_close(bdev->serdev);
472 static int btmtkuart_close(struct hci_dev *hdev)
474 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
475 struct device *dev = &bdev->serdev->dev;
477 /* Shutdown the clock and power domain the device requires */
478 clk_disable_unprepare(bdev->clk);
479 pm_runtime_put_sync(dev);
480 pm_runtime_disable(dev);
482 serdev_device_close(bdev->serdev);
487 static int btmtkuart_flush(struct hci_dev *hdev)
489 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
491 /* Flush any pending characters */
492 serdev_device_write_flush(bdev->serdev);
493 skb_queue_purge(&bdev->txq);
495 cancel_work_sync(&bdev->tx_work);
497 kfree_skb(bdev->rx_skb);
500 bdev->stp_cursor = 2;
506 static int btmtkuart_func_query(struct hci_dev *hdev)
508 struct btmtk_hci_wmt_params wmt_params;
512 /* Query whether the function is enabled */
513 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
515 wmt_params.dlen = sizeof(param);
516 wmt_params.data = ¶m;
517 wmt_params.status = &status;
519 err = mtk_hci_wmt_sync(hdev, &wmt_params);
521 bt_dev_err(hdev, "Failed to query function status (%d)", err);
528 static int btmtkuart_change_baudrate(struct hci_dev *hdev)
530 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
531 struct btmtk_hci_wmt_params wmt_params;
536 /* Indicate the device to enter the probe state the host is
537 * ready to change a new baudrate.
539 baudrate = cpu_to_le32(bdev->desired_speed);
540 wmt_params.op = BTMTK_WMT_HIF;
543 wmt_params.data = &baudrate;
544 wmt_params.status = NULL;
546 err = mtk_hci_wmt_sync(hdev, &wmt_params);
548 bt_dev_err(hdev, "Failed to device baudrate (%d)", err);
552 err = serdev_device_set_baudrate(bdev->serdev,
553 bdev->desired_speed);
555 bt_dev_err(hdev, "Failed to set up host baudrate (%d)",
560 serdev_device_set_flow_control(bdev->serdev, false);
562 /* Send a dummy byte 0xff to activate the new baudrate */
564 err = serdev_device_write_buf(bdev->serdev, ¶m, sizeof(param));
565 if (err < 0 || err < sizeof(param))
568 serdev_device_wait_until_sent(bdev->serdev, 0);
570 /* Wait some time for the device changing baudrate done */
571 usleep_range(20000, 22000);
573 /* Test the new baudrate */
574 wmt_params.op = BTMTK_WMT_TEST;
577 wmt_params.data = NULL;
578 wmt_params.status = NULL;
580 err = mtk_hci_wmt_sync(hdev, &wmt_params);
582 bt_dev_err(hdev, "Failed to test new baudrate (%d)",
587 bdev->curr_speed = bdev->desired_speed;
592 static int btmtkuart_setup(struct hci_dev *hdev)
594 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
595 struct btmtk_hci_wmt_params wmt_params;
596 ktime_t calltime, delta, rettime;
597 struct btmtk_tci_sleep tci_sleep;
598 unsigned long long duration;
603 calltime = ktime_get();
605 /* Wakeup MCUSYS is required for certain devices before we start to
608 if (test_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state)) {
609 wmt_params.op = BTMTK_WMT_WAKEUP;
612 wmt_params.data = NULL;
613 wmt_params.status = NULL;
615 err = mtk_hci_wmt_sync(hdev, &wmt_params);
617 bt_dev_err(hdev, "Failed to wakeup the chip (%d)", err);
621 clear_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
624 if (btmtkuart_is_standalone(bdev))
625 btmtkuart_change_baudrate(hdev);
627 /* Query whether the firmware is already download */
628 wmt_params.op = BTMTK_WMT_SEMAPHORE;
631 wmt_params.data = NULL;
632 wmt_params.status = &status;
634 err = mtk_hci_wmt_sync(hdev, &wmt_params);
636 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
640 if (status == BTMTK_WMT_PATCH_DONE) {
641 bt_dev_info(hdev, "Firmware already downloaded");
642 goto ignore_setup_fw;
645 /* Setup a firmware which the device definitely requires */
646 err = btmtk_setup_firmware(hdev, bdev->data->fwname, mtk_hci_wmt_sync);
651 /* Query whether the device is already enabled */
652 err = readx_poll_timeout(btmtkuart_func_query, hdev, status,
653 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
655 /* -ETIMEDOUT happens */
659 /* The other errors happen in btusb_mtk_func_query */
663 if (status == BTMTK_WMT_ON_DONE) {
664 bt_dev_info(hdev, "function already on");
668 /* Enable Bluetooth protocol */
669 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
671 wmt_params.dlen = sizeof(param);
672 wmt_params.data = ¶m;
673 wmt_params.status = NULL;
675 err = mtk_hci_wmt_sync(hdev, &wmt_params);
677 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
682 /* Apply the low power environment setup */
683 tci_sleep.mode = 0x5;
684 tci_sleep.duration = cpu_to_le16(0x640);
685 tci_sleep.host_duration = cpu_to_le16(0x640);
686 tci_sleep.host_wakeup_pin = 0;
687 tci_sleep.time_compensation = 0;
689 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
693 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
698 rettime = ktime_get();
699 delta = ktime_sub(rettime, calltime);
700 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
702 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
707 static int btmtkuart_shutdown(struct hci_dev *hdev)
709 struct btmtk_hci_wmt_params wmt_params;
713 /* Disable the device */
714 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
716 wmt_params.dlen = sizeof(param);
717 wmt_params.data = ¶m;
718 wmt_params.status = NULL;
720 err = mtk_hci_wmt_sync(hdev, &wmt_params);
722 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
729 static int btmtkuart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
731 struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
732 struct mtk_stp_hdr *shdr;
733 int err, dlen, type = 0;
735 /* Prepend skb with frame type */
736 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
738 /* Make sure that there is enough rooms for STP header and trailer */
739 if (unlikely(skb_headroom(skb) < sizeof(*shdr)) ||
740 (skb_tailroom(skb) < MTK_STP_TLR_SIZE)) {
741 err = pskb_expand_head(skb, sizeof(*shdr), MTK_STP_TLR_SIZE,
747 /* Add the STP header */
749 shdr = skb_push(skb, sizeof(*shdr));
751 shdr->dlen = cpu_to_be16((dlen & 0x0fff) | (type << 12));
752 shdr->cs = 0; /* MT7622 doesn't care about checksum value */
754 /* Add the STP trailer */
755 skb_put_zero(skb, MTK_STP_TLR_SIZE);
757 skb_queue_tail(&bdev->txq, skb);
759 btmtkuart_tx_wakeup(bdev);
763 static int btmtkuart_parse_dt(struct serdev_device *serdev)
765 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
766 struct device_node *node = serdev->dev.of_node;
770 if (btmtkuart_is_standalone(bdev)) {
771 of_property_read_u32(node, "current-speed", &speed);
773 bdev->desired_speed = speed;
775 bdev->vcc = devm_regulator_get(&serdev->dev, "vcc");
776 if (IS_ERR(bdev->vcc)) {
777 err = PTR_ERR(bdev->vcc);
781 bdev->osc = devm_clk_get_optional(&serdev->dev, "osc");
782 if (IS_ERR(bdev->osc)) {
783 err = PTR_ERR(bdev->osc);
787 bdev->boot = devm_gpiod_get_optional(&serdev->dev, "boot",
789 if (IS_ERR(bdev->boot)) {
790 err = PTR_ERR(bdev->boot);
794 bdev->pinctrl = devm_pinctrl_get(&serdev->dev);
795 if (IS_ERR(bdev->pinctrl)) {
796 err = PTR_ERR(bdev->pinctrl);
800 bdev->pins_boot = pinctrl_lookup_state(bdev->pinctrl,
802 if (IS_ERR(bdev->pins_boot) && !bdev->boot) {
803 err = PTR_ERR(bdev->pins_boot);
804 dev_err(&serdev->dev,
805 "Should assign RXD to LOW at boot stage\n");
809 bdev->pins_runtime = pinctrl_lookup_state(bdev->pinctrl,
811 if (IS_ERR(bdev->pins_runtime)) {
812 err = PTR_ERR(bdev->pins_runtime);
816 bdev->reset = devm_gpiod_get_optional(&serdev->dev, "reset",
818 if (IS_ERR(bdev->reset)) {
819 err = PTR_ERR(bdev->reset);
822 } else if (btmtkuart_is_builtin_soc(bdev)) {
823 bdev->clk = devm_clk_get(&serdev->dev, "ref");
824 if (IS_ERR(bdev->clk))
825 return PTR_ERR(bdev->clk);
831 static int btmtkuart_probe(struct serdev_device *serdev)
833 struct btmtkuart_dev *bdev;
834 struct hci_dev *hdev;
837 bdev = devm_kzalloc(&serdev->dev, sizeof(*bdev), GFP_KERNEL);
841 bdev->data = of_device_get_match_data(&serdev->dev);
845 bdev->serdev = serdev;
846 serdev_device_set_drvdata(serdev, bdev);
848 serdev_device_set_client_ops(serdev, &btmtkuart_client_ops);
850 err = btmtkuart_parse_dt(serdev);
854 INIT_WORK(&bdev->tx_work, btmtkuart_tx_work);
855 skb_queue_head_init(&bdev->txq);
857 /* Initialize and register HCI device */
858 hdev = hci_alloc_dev();
860 dev_err(&serdev->dev, "Can't allocate HCI device\n");
866 hdev->bus = HCI_UART;
867 hci_set_drvdata(hdev, bdev);
869 hdev->open = btmtkuart_open;
870 hdev->close = btmtkuart_close;
871 hdev->flush = btmtkuart_flush;
872 hdev->setup = btmtkuart_setup;
873 hdev->shutdown = btmtkuart_shutdown;
874 hdev->send = btmtkuart_send_frame;
875 hdev->set_bdaddr = btmtk_set_bdaddr;
876 SET_HCIDEV_DEV(hdev, &serdev->dev);
878 hdev->manufacturer = 70;
879 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
881 if (btmtkuart_is_standalone(bdev)) {
882 err = clk_prepare_enable(bdev->osc);
884 goto err_hci_free_dev;
887 gpiod_set_value_cansleep(bdev->boot, 1);
889 /* Switch to the specific pin state for the booting
892 pinctrl_select_state(bdev->pinctrl, bdev->pins_boot);
896 err = regulator_enable(bdev->vcc);
898 goto err_clk_disable_unprepare;
900 /* Reset if the reset-gpios is available otherwise the board
901 * -level design should be guaranteed.
904 gpiod_set_value_cansleep(bdev->reset, 1);
905 usleep_range(1000, 2000);
906 gpiod_set_value_cansleep(bdev->reset, 0);
909 /* Wait some time until device got ready and switch to the pin
910 * mode the device requires for UART transfers.
915 devm_gpiod_put(&serdev->dev, bdev->boot);
917 pinctrl_select_state(bdev->pinctrl, bdev->pins_runtime);
919 /* A standalone device doesn't depends on power domain on SoC,
920 * so mark it as no callbacks.
922 pm_runtime_no_callbacks(&serdev->dev);
924 set_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
927 err = hci_register_dev(hdev);
929 dev_err(&serdev->dev, "Can't register HCI device\n");
930 goto err_regulator_disable;
935 err_regulator_disable:
936 if (btmtkuart_is_standalone(bdev))
937 regulator_disable(bdev->vcc);
938 err_clk_disable_unprepare:
939 if (btmtkuart_is_standalone(bdev))
940 clk_disable_unprepare(bdev->osc);
947 static void btmtkuart_remove(struct serdev_device *serdev)
949 struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
950 struct hci_dev *hdev = bdev->hdev;
952 if (btmtkuart_is_standalone(bdev)) {
953 regulator_disable(bdev->vcc);
954 clk_disable_unprepare(bdev->osc);
957 hci_unregister_dev(hdev);
961 static const struct btmtkuart_data mt7622_data __maybe_unused = {
962 .fwname = FIRMWARE_MT7622,
965 static const struct btmtkuart_data mt7663_data __maybe_unused = {
966 .flags = BTMTKUART_FLAG_STANDALONE_HW,
967 .fwname = FIRMWARE_MT7663,
970 static const struct btmtkuart_data mt7668_data __maybe_unused = {
971 .flags = BTMTKUART_FLAG_STANDALONE_HW,
972 .fwname = FIRMWARE_MT7668,
976 static const struct of_device_id mtk_of_match_table[] = {
977 { .compatible = "mediatek,mt7622-bluetooth", .data = &mt7622_data},
978 { .compatible = "mediatek,mt7663u-bluetooth", .data = &mt7663_data},
979 { .compatible = "mediatek,mt7668u-bluetooth", .data = &mt7668_data},
982 MODULE_DEVICE_TABLE(of, mtk_of_match_table);
985 static struct serdev_device_driver btmtkuart_driver = {
986 .probe = btmtkuart_probe,
987 .remove = btmtkuart_remove,
990 .of_match_table = of_match_ptr(mtk_of_match_table),
994 module_serdev_device_driver(btmtkuart_driver);
996 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
997 MODULE_DESCRIPTION("MediaTek Bluetooth Serial driver ver " VERSION);
998 MODULE_VERSION(VERSION);
999 MODULE_LICENSE("GPL");