Lines Matching +full:serial +full:- +full:clk +full:- +full:low

1 // SPDX-License-Identifier: GPL-2.0
5 * Bluetooth support for MediaTek serial devices
13 #include <linux/clk.h>
59 struct clk *clk; member
60 struct clk *osc;
85 ((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
87 !((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)
101 hlen = sizeof(*hdr) + wmt_params->dlen; in mtk_hci_wmt_sync()
103 err = -EINVAL; in mtk_hci_wmt_sync()
109 err = -ENOMEM; in mtk_hci_wmt_sync()
113 hdr = &wc->hdr; in mtk_hci_wmt_sync()
114 hdr->dir = 1; in mtk_hci_wmt_sync()
115 hdr->op = wmt_params->op; in mtk_hci_wmt_sync()
116 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1); in mtk_hci_wmt_sync()
117 hdr->flag = wmt_params->flag; in mtk_hci_wmt_sync()
118 memcpy(wc->data, wmt_params->data, wmt_params->dlen); in mtk_hci_wmt_sync()
120 set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); in mtk_hci_wmt_sync()
124 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); in mtk_hci_wmt_sync()
137 err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT, in mtk_hci_wmt_sync()
139 if (err == -EINTR) { in mtk_hci_wmt_sync()
141 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); in mtk_hci_wmt_sync()
147 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); in mtk_hci_wmt_sync()
148 err = -ETIMEDOUT; in mtk_hci_wmt_sync()
153 wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data; in mtk_hci_wmt_sync()
154 if (wmt_evt->whdr.op != hdr->op) { in mtk_hci_wmt_sync()
156 wmt_evt->whdr.op, hdr->op); in mtk_hci_wmt_sync()
157 err = -EIO; in mtk_hci_wmt_sync()
161 switch (wmt_evt->whdr.op) { in mtk_hci_wmt_sync()
163 if (wmt_evt->whdr.flag == 2) in mtk_hci_wmt_sync()
170 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404) in mtk_hci_wmt_sync()
172 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420) in mtk_hci_wmt_sync()
179 if (wmt_params->status) in mtk_hci_wmt_sync()
180 *wmt_params->status = status; in mtk_hci_wmt_sync()
185 kfree_skb(bdev->evt_skb); in mtk_hci_wmt_sync()
186 bdev->evt_skb = NULL; in mtk_hci_wmt_sync()
194 struct hci_event_hdr *hdr = (void *)skb->data; in btmtkuart_recv_event()
200 if (test_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) { in btmtkuart_recv_event()
201 bdev->evt_skb = skb_clone(skb, GFP_KERNEL); in btmtkuart_recv_event()
202 if (!bdev->evt_skb) { in btmtkuart_recv_event()
203 err = -ENOMEM; in btmtkuart_recv_event()
212 if (hdr->evt == HCI_EV_WMT) { in btmtkuart_recv_event()
214 &bdev->tx_state)) { in btmtkuart_recv_event()
217 wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT); in btmtkuart_recv_event()
224 kfree_skb(bdev->evt_skb); in btmtkuart_recv_event()
225 bdev->evt_skb = NULL; in btmtkuart_recv_event()
241 struct serdev_device *serdev = bdev->serdev; in btmtkuart_tx_work()
242 struct hci_dev *hdev = bdev->hdev; in btmtkuart_tx_work()
245 clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state); in btmtkuart_tx_work()
248 struct sk_buff *skb = skb_dequeue(&bdev->txq); in btmtkuart_tx_work()
254 len = serdev_device_write_buf(serdev, skb->data, in btmtkuart_tx_work()
255 skb->len); in btmtkuart_tx_work()
256 hdev->stat.byte_tx += len; in btmtkuart_tx_work()
259 if (skb->len > 0) { in btmtkuart_tx_work()
260 skb_queue_head(&bdev->txq, skb); in btmtkuart_tx_work()
266 hdev->stat.cmd_tx++; in btmtkuart_tx_work()
269 hdev->stat.acl_tx++; in btmtkuart_tx_work()
272 hdev->stat.sco_tx++; in btmtkuart_tx_work()
279 if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state)) in btmtkuart_tx_work()
283 clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state); in btmtkuart_tx_work()
288 if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state)) in btmtkuart_tx_wakeup()
289 set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state); in btmtkuart_tx_wakeup()
291 schedule_work(&bdev->tx_work); in btmtkuart_tx_wakeup()
301 if (!bdev->stp_dlen && bdev->stp_cursor >= 6) in mtk_stp_split()
302 bdev->stp_cursor = 0; in mtk_stp_split()
305 while (bdev->stp_cursor < 6 && count > 0) { in mtk_stp_split()
306 bdev->stp_pad[bdev->stp_cursor] = *data; in mtk_stp_split()
307 bdev->stp_cursor++; in mtk_stp_split()
309 count--; in mtk_stp_split()
313 if (!bdev->stp_dlen && bdev->stp_cursor >= 6) { in mtk_stp_split()
314 shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2]; in mtk_stp_split()
315 bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff; in mtk_stp_split()
318 if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) { in mtk_stp_split()
319 bt_dev_err(bdev->hdev, "stp format unexpect (%d, %d)", in mtk_stp_split()
320 shdr->prefix, bdev->stp_dlen); in mtk_stp_split()
321 bdev->stp_cursor = 2; in mtk_stp_split()
322 bdev->stp_dlen = 0; in mtk_stp_split()
331 *sz_h4 = min_t(int, count, bdev->stp_dlen); in mtk_stp_split()
334 bdev->stp_dlen -= *sz_h4; in mtk_stp_split()
348 /* The serial data received from MT7622 BT controller is in btmtkuart_recv()
352 * ----------------------------------- in btmtkuart_recv()
354 * ----------------------------------- in btmtkuart_recv()
367 adv = p_h4 - p_left; in btmtkuart_recv()
368 sz_left -= adv; in btmtkuart_recv()
371 bdev->rx_skb = h4_recv_buf(bdev->hdev, bdev->rx_skb, p_h4, in btmtkuart_recv()
374 if (IS_ERR(bdev->rx_skb)) { in btmtkuart_recv()
375 err = PTR_ERR(bdev->rx_skb); in btmtkuart_recv()
376 bt_dev_err(bdev->hdev, in btmtkuart_recv()
378 bdev->rx_skb = NULL; in btmtkuart_recv()
382 sz_left -= sz_h4; in btmtkuart_recv()
392 btmtkuart_recv(bdev->hdev, data, count); in btmtkuart_receive_buf()
394 bdev->hdev->stat.byte_rx += count; in btmtkuart_receive_buf()
417 err = serdev_device_open(bdev->serdev); in btmtkuart_open()
420 dev_name(&bdev->serdev->dev)); in btmtkuart_open()
425 if (bdev->curr_speed != bdev->desired_speed) in btmtkuart_open()
426 err = serdev_device_set_baudrate(bdev->serdev, in btmtkuart_open()
429 err = serdev_device_set_baudrate(bdev->serdev, in btmtkuart_open()
430 bdev->desired_speed); in btmtkuart_open()
434 dev_name(&bdev->serdev->dev)); in btmtkuart_open()
438 serdev_device_set_flow_control(bdev->serdev, false); in btmtkuart_open()
441 bdev->stp_cursor = 2; in btmtkuart_open()
442 bdev->stp_dlen = 0; in btmtkuart_open()
444 dev = &bdev->serdev->dev; in btmtkuart_open()
452 err = clk_prepare_enable(bdev->clk); in btmtkuart_open()
463 serdev_device_close(bdev->serdev); in btmtkuart_open()
471 struct device *dev = &bdev->serdev->dev; in btmtkuart_close()
474 clk_disable_unprepare(bdev->clk); in btmtkuart_close()
478 serdev_device_close(bdev->serdev); in btmtkuart_close()
488 serdev_device_write_flush(bdev->serdev); in btmtkuart_flush()
489 skb_queue_purge(&bdev->txq); in btmtkuart_flush()
491 cancel_work_sync(&bdev->tx_work); in btmtkuart_flush()
493 kfree_skb(bdev->rx_skb); in btmtkuart_flush()
494 bdev->rx_skb = NULL; in btmtkuart_flush()
496 bdev->stp_cursor = 2; in btmtkuart_flush()
497 bdev->stp_dlen = 0; in btmtkuart_flush()
535 baudrate = cpu_to_le32(bdev->desired_speed); in btmtkuart_change_baudrate()
548 err = serdev_device_set_baudrate(bdev->serdev, in btmtkuart_change_baudrate()
549 bdev->desired_speed); in btmtkuart_change_baudrate()
556 serdev_device_set_flow_control(bdev->serdev, false); in btmtkuart_change_baudrate()
560 err = serdev_device_write_buf(bdev->serdev, &param, sizeof(param)); in btmtkuart_change_baudrate()
564 serdev_device_wait_until_sent(bdev->serdev, 0); in btmtkuart_change_baudrate()
583 bdev->curr_speed = bdev->desired_speed; in btmtkuart_change_baudrate()
604 if (test_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state)) { in btmtkuart_setup()
617 clear_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state); in btmtkuart_setup()
642 err = btmtk_setup_firmware(hdev, bdev->data->fwname, mtk_hci_wmt_sync); in btmtkuart_setup()
651 /* -ETIMEDOUT happens */ in btmtkuart_setup()
678 /* Apply the low power environment setup */ in btmtkuart_setup()
689 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err); in btmtkuart_setup()
744 dlen = skb->len; in btmtkuart_send_frame()
746 shdr->prefix = 0x80; in btmtkuart_send_frame()
747 shdr->dlen = cpu_to_be16((dlen & 0x0fff) | (type << 12)); in btmtkuart_send_frame()
748 shdr->cs = 0; /* MT7622 doesn't care about checksum value */ in btmtkuart_send_frame()
753 skb_queue_tail(&bdev->txq, skb); in btmtkuart_send_frame()
762 struct device_node *node = serdev->dev.of_node; in btmtkuart_parse_dt()
767 of_property_read_u32(node, "current-speed", &speed); in btmtkuart_parse_dt()
769 bdev->desired_speed = speed; in btmtkuart_parse_dt()
771 bdev->vcc = devm_regulator_get(&serdev->dev, "vcc"); in btmtkuart_parse_dt()
772 if (IS_ERR(bdev->vcc)) { in btmtkuart_parse_dt()
773 err = PTR_ERR(bdev->vcc); in btmtkuart_parse_dt()
777 bdev->osc = devm_clk_get_optional(&serdev->dev, "osc"); in btmtkuart_parse_dt()
778 if (IS_ERR(bdev->osc)) { in btmtkuart_parse_dt()
779 err = PTR_ERR(bdev->osc); in btmtkuart_parse_dt()
783 bdev->boot = devm_gpiod_get_optional(&serdev->dev, "boot", in btmtkuart_parse_dt()
785 if (IS_ERR(bdev->boot)) { in btmtkuart_parse_dt()
786 err = PTR_ERR(bdev->boot); in btmtkuart_parse_dt()
790 bdev->pinctrl = devm_pinctrl_get(&serdev->dev); in btmtkuart_parse_dt()
791 if (IS_ERR(bdev->pinctrl)) { in btmtkuart_parse_dt()
792 err = PTR_ERR(bdev->pinctrl); in btmtkuart_parse_dt()
796 bdev->pins_boot = pinctrl_lookup_state(bdev->pinctrl, in btmtkuart_parse_dt()
798 if (IS_ERR(bdev->pins_boot) && !bdev->boot) { in btmtkuart_parse_dt()
799 err = PTR_ERR(bdev->pins_boot); in btmtkuart_parse_dt()
800 dev_err(&serdev->dev, in btmtkuart_parse_dt()
801 "Should assign RXD to LOW at boot stage\n"); in btmtkuart_parse_dt()
805 bdev->pins_runtime = pinctrl_lookup_state(bdev->pinctrl, in btmtkuart_parse_dt()
807 if (IS_ERR(bdev->pins_runtime)) { in btmtkuart_parse_dt()
808 err = PTR_ERR(bdev->pins_runtime); in btmtkuart_parse_dt()
812 bdev->reset = devm_gpiod_get_optional(&serdev->dev, "reset", in btmtkuart_parse_dt()
814 if (IS_ERR(bdev->reset)) { in btmtkuart_parse_dt()
815 err = PTR_ERR(bdev->reset); in btmtkuart_parse_dt()
819 bdev->clk = devm_clk_get(&serdev->dev, "ref"); in btmtkuart_parse_dt()
820 if (IS_ERR(bdev->clk)) in btmtkuart_parse_dt()
821 return PTR_ERR(bdev->clk); in btmtkuart_parse_dt()
833 bdev = devm_kzalloc(&serdev->dev, sizeof(*bdev), GFP_KERNEL); in btmtkuart_probe()
835 return -ENOMEM; in btmtkuart_probe()
837 bdev->data = of_device_get_match_data(&serdev->dev); in btmtkuart_probe()
838 if (!bdev->data) in btmtkuart_probe()
839 return -ENODEV; in btmtkuart_probe()
841 bdev->serdev = serdev; in btmtkuart_probe()
850 INIT_WORK(&bdev->tx_work, btmtkuart_tx_work); in btmtkuart_probe()
851 skb_queue_head_init(&bdev->txq); in btmtkuart_probe()
856 dev_err(&serdev->dev, "Can't allocate HCI device\n"); in btmtkuart_probe()
857 return -ENOMEM; in btmtkuart_probe()
860 bdev->hdev = hdev; in btmtkuart_probe()
862 hdev->bus = HCI_UART; in btmtkuart_probe()
865 hdev->open = btmtkuart_open; in btmtkuart_probe()
866 hdev->close = btmtkuart_close; in btmtkuart_probe()
867 hdev->flush = btmtkuart_flush; in btmtkuart_probe()
868 hdev->setup = btmtkuart_setup; in btmtkuart_probe()
869 hdev->shutdown = btmtkuart_shutdown; in btmtkuart_probe()
870 hdev->send = btmtkuart_send_frame; in btmtkuart_probe()
871 hdev->set_bdaddr = btmtk_set_bdaddr; in btmtkuart_probe()
872 SET_HCIDEV_DEV(hdev, &serdev->dev); in btmtkuart_probe()
874 hdev->manufacturer = 70; in btmtkuart_probe()
875 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks); in btmtkuart_probe()
878 err = clk_prepare_enable(bdev->osc); in btmtkuart_probe()
882 if (bdev->boot) { in btmtkuart_probe()
883 gpiod_set_value_cansleep(bdev->boot, 1); in btmtkuart_probe()
888 pinctrl_select_state(bdev->pinctrl, bdev->pins_boot); in btmtkuart_probe()
892 err = regulator_enable(bdev->vcc); in btmtkuart_probe()
896 /* Reset if the reset-gpios is available otherwise the board in btmtkuart_probe()
897 * -level design should be guaranteed. in btmtkuart_probe()
899 if (bdev->reset) { in btmtkuart_probe()
900 gpiod_set_value_cansleep(bdev->reset, 1); in btmtkuart_probe()
902 gpiod_set_value_cansleep(bdev->reset, 0); in btmtkuart_probe()
910 if (bdev->boot) in btmtkuart_probe()
911 devm_gpiod_put(&serdev->dev, bdev->boot); in btmtkuart_probe()
913 pinctrl_select_state(bdev->pinctrl, bdev->pins_runtime); in btmtkuart_probe()
918 pm_runtime_no_callbacks(&serdev->dev); in btmtkuart_probe()
920 set_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state); in btmtkuart_probe()
925 dev_err(&serdev->dev, "Can't register HCI device\n"); in btmtkuart_probe()
933 regulator_disable(bdev->vcc); in btmtkuart_probe()
936 clk_disable_unprepare(bdev->osc); in btmtkuart_probe()
946 struct hci_dev *hdev = bdev->hdev; in btmtkuart_remove()
949 regulator_disable(bdev->vcc); in btmtkuart_remove()
950 clk_disable_unprepare(bdev->osc); in btmtkuart_remove()
973 { .compatible = "mediatek,mt7622-bluetooth", .data = &mt7622_data},
974 { .compatible = "mediatek,mt7663u-bluetooth", .data = &mt7663_data},
975 { .compatible = "mediatek,mt7668u-bluetooth", .data = &mt7668_data},
993 MODULE_DESCRIPTION("MediaTek Bluetooth Serial driver ver " VERSION);