Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[platform/kernel/linux-starfive.git] / drivers / bluetooth / btmtkuart.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 MediaTek Inc.
3
4 /*
5  * Bluetooth support for MediaTek serial devices
6  *
7  * Author: Sean Wang <sean.wang@mediatek.com>
8  *
9  */
10
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>
19 #include <linux/of.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>
26
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
29
30 #include "h4_recv.h"
31 #include "btmtk.h"
32
33 #define VERSION "0.2"
34
35 #define MTK_STP_TLR_SIZE        2
36
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
41
42 #define BTMTKUART_FLAG_STANDALONE_HW     BIT(0)
43
44 struct mtk_stp_hdr {
45         u8      prefix;
46         __be16  dlen;
47         u8      cs;
48 } __packed;
49
50 struct btmtkuart_data {
51         unsigned int flags;
52         const char *fwname;
53 };
54
55 struct btmtkuart_dev {
56         struct hci_dev *hdev;
57         struct serdev_device *serdev;
58
59         struct clk *clk;
60         struct clk *osc;
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;
68         speed_t curr_speed;
69
70         struct work_struct tx_work;
71         unsigned long tx_state;
72         struct sk_buff_head txq;
73
74         struct sk_buff *rx_skb;
75         struct sk_buff *evt_skb;
76
77         u8      stp_pad[6];
78         u8      stp_cursor;
79         u16     stp_dlen;
80
81         const struct btmtkuart_data *data;
82 };
83
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)
88
89 static int mtk_hci_wmt_sync(struct hci_dev *hdev,
90                             struct btmtk_hci_wmt_params *wmt_params)
91 {
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;
98         int err;
99
100         /* Send the WMT command and wait until the WMT event returns */
101         hlen = sizeof(*hdr) + wmt_params->dlen;
102         if (hlen > 255) {
103                 err = -EINVAL;
104                 goto err_free_skb;
105         }
106
107         wc = kzalloc(hlen, GFP_KERNEL);
108         if (!wc) {
109                 err = -ENOMEM;
110                 goto err_free_skb;
111         }
112
113         hdr = &wc->hdr;
114         hdr->dir = 1;
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);
119
120         set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
121
122         err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
123         if (err < 0) {
124                 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
125                 goto err_free_wc;
126         }
127
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.
131          *
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
135          * WMT command.
136          */
137         err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT,
138                                   TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
139         if (err == -EINTR) {
140                 bt_dev_err(hdev, "Execution of wmt command interrupted");
141                 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
142                 goto err_free_wc;
143         }
144
145         if (err) {
146                 bt_dev_err(hdev, "Execution of wmt command timed out");
147                 clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state);
148                 err = -ETIMEDOUT;
149                 goto err_free_wc;
150         }
151
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);
157                 err = -EIO;
158                 goto err_free_wc;
159         }
160
161         switch (wmt_evt->whdr.op) {
162         case BTMTK_WMT_SEMAPHORE:
163                 if (wmt_evt->whdr.flag == 2)
164                         status = BTMTK_WMT_PATCH_UNDONE;
165                 else
166                         status = BTMTK_WMT_PATCH_DONE;
167                 break;
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;
174                 else
175                         status = BTMTK_WMT_ON_UNDONE;
176                 break;
177         }
178
179         if (wmt_params->status)
180                 *wmt_params->status = status;
181
182 err_free_wc:
183         kfree(wc);
184 err_free_skb:
185         kfree_skb(bdev->evt_skb);
186         bdev->evt_skb = NULL;
187
188         return err;
189 }
190
191 static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
192 {
193         struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
194         struct hci_event_hdr *hdr = (void *)skb->data;
195         int err;
196
197         /* When someone waits for the WMT event, the skb is being cloned
198          * and being processed the events from there then.
199          */
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) {
203                         err = -ENOMEM;
204                         goto err_out;
205                 }
206         }
207
208         err = hci_recv_frame(hdev, skb);
209         if (err < 0)
210                 goto err_free_skb;
211
212         if (hdr->evt == HCI_EV_WMT) {
213                 if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT,
214                                        &bdev->tx_state)) {
215                         /* Barrier to sync with other CPUs */
216                         smp_mb__after_atomic();
217                         wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT);
218                 }
219         }
220
221         return 0;
222
223 err_free_skb:
224         kfree_skb(bdev->evt_skb);
225         bdev->evt_skb = NULL;
226
227 err_out:
228         return err;
229 }
230
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 },
235 };
236
237 static void btmtkuart_tx_work(struct work_struct *work)
238 {
239         struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev,
240                                                    tx_work);
241         struct serdev_device *serdev = bdev->serdev;
242         struct hci_dev *hdev = bdev->hdev;
243
244         while (1) {
245                 clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
246
247                 while (1) {
248                         struct sk_buff *skb = skb_dequeue(&bdev->txq);
249                         int len;
250
251                         if (!skb)
252                                 break;
253
254                         len = serdev_device_write_buf(serdev, skb->data,
255                                                       skb->len);
256                         hdev->stat.byte_tx += len;
257
258                         skb_pull(skb, len);
259                         if (skb->len > 0) {
260                                 skb_queue_head(&bdev->txq, skb);
261                                 break;
262                         }
263
264                         switch (hci_skb_pkt_type(skb)) {
265                         case HCI_COMMAND_PKT:
266                                 hdev->stat.cmd_tx++;
267                                 break;
268                         case HCI_ACLDATA_PKT:
269                                 hdev->stat.acl_tx++;
270                                 break;
271                         case HCI_SCODATA_PKT:
272                                 hdev->stat.sco_tx++;
273                                 break;
274                         }
275
276                         kfree_skb(skb);
277                 }
278
279                 if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state))
280                         break;
281         }
282
283         clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state);
284 }
285
286 static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev)
287 {
288         if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state))
289                 set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state);
290
291         schedule_work(&bdev->tx_work);
292 }
293
294 static const unsigned char *
295 mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count,
296               int *sz_h4)
297 {
298         struct mtk_stp_hdr *shdr;
299
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;
303
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;
307                 bdev->stp_cursor++;
308                 data++;
309                 count--;
310         }
311
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;
316
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;
322                         bdev->stp_dlen = 0;
323                 }
324         }
325
326         /* Directly quit when there's no data found for H4 can process */
327         if (count <= 0)
328                 return NULL;
329
330         /* Tranlate to how much the size of data H4 can handle so far */
331         *sz_h4 = min_t(int, count, bdev->stp_dlen);
332
333         /* Update the remaining size of STP packet */
334         bdev->stp_dlen -= *sz_h4;
335
336         /* Data points to STP payload which can be handled by H4 */
337         return data;
338 }
339
340 static int btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count)
341 {
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;
345         int err;
346
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.
350                  *
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.
361                  */
362
363                 p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4);
364                 if (!p_h4)
365                         break;
366
367                 adv = p_h4 - p_left;
368                 sz_left -= adv;
369                 p_left += adv;
370
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);
378                         bdev->rx_skb = NULL;
379                         return err;
380                 }
381
382                 sz_left -= sz_h4;
383                 p_left += sz_h4;
384         }
385
386         return 0;
387 }
388
389 static int btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data,
390                                  size_t count)
391 {
392         struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
393         int err;
394
395         err = btmtkuart_recv(bdev->hdev, data, count);
396         if (err < 0)
397                 return err;
398
399         bdev->hdev->stat.byte_rx += count;
400
401         return count;
402 }
403
404 static void btmtkuart_write_wakeup(struct serdev_device *serdev)
405 {
406         struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
407
408         btmtkuart_tx_wakeup(bdev);
409 }
410
411 static const struct serdev_device_ops btmtkuart_client_ops = {
412         .receive_buf = btmtkuart_receive_buf,
413         .write_wakeup = btmtkuart_write_wakeup,
414 };
415
416 static int btmtkuart_open(struct hci_dev *hdev)
417 {
418         struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
419         struct device *dev;
420         int err;
421
422         err = serdev_device_open(bdev->serdev);
423         if (err) {
424                 bt_dev_err(hdev, "Unable to open UART device %s",
425                            dev_name(&bdev->serdev->dev));
426                 goto err_open;
427         }
428
429         if (btmtkuart_is_standalone(bdev)) {
430                 if (bdev->curr_speed != bdev->desired_speed)
431                         err = serdev_device_set_baudrate(bdev->serdev,
432                                                          115200);
433                 else
434                         err = serdev_device_set_baudrate(bdev->serdev,
435                                                          bdev->desired_speed);
436
437                 if (err < 0) {
438                         bt_dev_err(hdev, "Unable to set baudrate UART device %s",
439                                    dev_name(&bdev->serdev->dev));
440                         goto  err_serdev_close;
441                 }
442
443                 serdev_device_set_flow_control(bdev->serdev, false);
444         }
445
446         bdev->stp_cursor = 2;
447         bdev->stp_dlen = 0;
448
449         dev = &bdev->serdev->dev;
450
451         /* Enable the power domain and clock the device requires */
452         pm_runtime_enable(dev);
453         err = pm_runtime_resume_and_get(dev);
454         if (err < 0)
455                 goto err_disable_rpm;
456
457         err = clk_prepare_enable(bdev->clk);
458         if (err < 0)
459                 goto err_put_rpm;
460
461         return 0;
462
463 err_put_rpm:
464         pm_runtime_put_sync(dev);
465 err_disable_rpm:
466         pm_runtime_disable(dev);
467 err_serdev_close:
468         serdev_device_close(bdev->serdev);
469 err_open:
470         return err;
471 }
472
473 static int btmtkuart_close(struct hci_dev *hdev)
474 {
475         struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
476         struct device *dev = &bdev->serdev->dev;
477
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);
482
483         serdev_device_close(bdev->serdev);
484
485         return 0;
486 }
487
488 static int btmtkuart_flush(struct hci_dev *hdev)
489 {
490         struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
491
492         /* Flush any pending characters */
493         serdev_device_write_flush(bdev->serdev);
494         skb_queue_purge(&bdev->txq);
495
496         cancel_work_sync(&bdev->tx_work);
497
498         kfree_skb(bdev->rx_skb);
499         bdev->rx_skb = NULL;
500
501         bdev->stp_cursor = 2;
502         bdev->stp_dlen = 0;
503
504         return 0;
505 }
506
507 static int btmtkuart_func_query(struct hci_dev *hdev)
508 {
509         struct btmtk_hci_wmt_params wmt_params;
510         int status, err;
511         u8 param = 0;
512
513         /* Query whether the function is enabled */
514         wmt_params.op = BTMTK_WMT_FUNC_CTRL;
515         wmt_params.flag = 4;
516         wmt_params.dlen = sizeof(param);
517         wmt_params.data = &param;
518         wmt_params.status = &status;
519
520         err = mtk_hci_wmt_sync(hdev, &wmt_params);
521         if (err < 0) {
522                 bt_dev_err(hdev, "Failed to query function status (%d)", err);
523                 return err;
524         }
525
526         return status;
527 }
528
529 static int btmtkuart_change_baudrate(struct hci_dev *hdev)
530 {
531         struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
532         struct btmtk_hci_wmt_params wmt_params;
533         __le32 baudrate;
534         u8 param;
535         int err;
536
537         /* Indicate the device to enter the probe state the host is
538          * ready to change a new baudrate.
539          */
540         baudrate = cpu_to_le32(bdev->desired_speed);
541         wmt_params.op = BTMTK_WMT_HIF;
542         wmt_params.flag = 1;
543         wmt_params.dlen = 4;
544         wmt_params.data = &baudrate;
545         wmt_params.status = NULL;
546
547         err = mtk_hci_wmt_sync(hdev, &wmt_params);
548         if (err < 0) {
549                 bt_dev_err(hdev, "Failed to device baudrate (%d)", err);
550                 return err;
551         }
552
553         err = serdev_device_set_baudrate(bdev->serdev,
554                                          bdev->desired_speed);
555         if (err < 0) {
556                 bt_dev_err(hdev, "Failed to set up host baudrate (%d)",
557                            err);
558                 return err;
559         }
560
561         serdev_device_set_flow_control(bdev->serdev, false);
562
563         /* Send a dummy byte 0xff to activate the new baudrate */
564         param = 0xff;
565         err = serdev_device_write_buf(bdev->serdev, &param, sizeof(param));
566         if (err < 0 || err < sizeof(param))
567                 return err;
568
569         serdev_device_wait_until_sent(bdev->serdev, 0);
570
571         /* Wait some time for the device changing baudrate done */
572         usleep_range(20000, 22000);
573
574         /* Test the new baudrate */
575         wmt_params.op = BTMTK_WMT_TEST;
576         wmt_params.flag = 7;
577         wmt_params.dlen = 0;
578         wmt_params.data = NULL;
579         wmt_params.status = NULL;
580
581         err = mtk_hci_wmt_sync(hdev, &wmt_params);
582         if (err < 0) {
583                 bt_dev_err(hdev, "Failed to test new baudrate (%d)",
584                            err);
585                 return err;
586         }
587
588         bdev->curr_speed = bdev->desired_speed;
589
590         return 0;
591 }
592
593 static int btmtkuart_setup(struct hci_dev *hdev)
594 {
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;
600         struct sk_buff *skb;
601         int err, status;
602         u8 param = 0x1;
603
604         calltime = ktime_get();
605
606         /* Wakeup MCUSYS is required for certain devices before we start to
607          * do any setups.
608          */
609         if (test_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state)) {
610                 wmt_params.op = BTMTK_WMT_WAKEUP;
611                 wmt_params.flag = 3;
612                 wmt_params.dlen = 0;
613                 wmt_params.data = NULL;
614                 wmt_params.status = NULL;
615
616                 err = mtk_hci_wmt_sync(hdev, &wmt_params);
617                 if (err < 0) {
618                         bt_dev_err(hdev, "Failed to wakeup the chip (%d)", err);
619                         return err;
620                 }
621
622                 clear_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
623         }
624
625         if (btmtkuart_is_standalone(bdev))
626                 btmtkuart_change_baudrate(hdev);
627
628         /* Query whether the firmware is already download */
629         wmt_params.op = BTMTK_WMT_SEMAPHORE;
630         wmt_params.flag = 1;
631         wmt_params.dlen = 0;
632         wmt_params.data = NULL;
633         wmt_params.status = &status;
634
635         err = mtk_hci_wmt_sync(hdev, &wmt_params);
636         if (err < 0) {
637                 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
638                 return err;
639         }
640
641         if (status == BTMTK_WMT_PATCH_DONE) {
642                 bt_dev_info(hdev, "Firmware already downloaded");
643                 goto ignore_setup_fw;
644         }
645
646         /* Setup a firmware which the device definitely requires */
647         err = btmtk_setup_firmware(hdev, bdev->data->fwname, mtk_hci_wmt_sync);
648         if (err < 0)
649                 return err;
650
651 ignore_setup_fw:
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,
655                                  2000, 5000000);
656         /* -ETIMEDOUT happens */
657         if (err < 0)
658                 return err;
659
660         /* The other errors happen in btusb_mtk_func_query */
661         if (status < 0)
662                 return status;
663
664         if (status == BTMTK_WMT_ON_DONE) {
665                 bt_dev_info(hdev, "function already on");
666                 goto ignore_func_on;
667         }
668
669         /* Enable Bluetooth protocol */
670         wmt_params.op = BTMTK_WMT_FUNC_CTRL;
671         wmt_params.flag = 0;
672         wmt_params.dlen = sizeof(param);
673         wmt_params.data = &param;
674         wmt_params.status = NULL;
675
676         err = mtk_hci_wmt_sync(hdev, &wmt_params);
677         if (err < 0) {
678                 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
679                 return err;
680         }
681
682 ignore_func_on:
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;
689
690         skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
691                              HCI_INIT_TIMEOUT);
692         if (IS_ERR(skb)) {
693                 err = PTR_ERR(skb);
694                 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
695                 return err;
696         }
697         kfree_skb(skb);
698
699         rettime = ktime_get();
700         delta = ktime_sub(rettime, calltime);
701         duration = (unsigned long long)ktime_to_ns(delta) >> 10;
702
703         bt_dev_info(hdev, "Device setup in %llu usecs", duration);
704
705         return 0;
706 }
707
708 static int btmtkuart_shutdown(struct hci_dev *hdev)
709 {
710         struct btmtk_hci_wmt_params wmt_params;
711         u8 param = 0x0;
712         int err;
713
714         /* Disable the device */
715         wmt_params.op = BTMTK_WMT_FUNC_CTRL;
716         wmt_params.flag = 0;
717         wmt_params.dlen = sizeof(param);
718         wmt_params.data = &param;
719         wmt_params.status = NULL;
720
721         err = mtk_hci_wmt_sync(hdev, &wmt_params);
722         if (err < 0) {
723                 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
724                 return err;
725         }
726
727         return 0;
728 }
729
730 static int btmtkuart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
731 {
732         struct btmtkuart_dev *bdev = hci_get_drvdata(hdev);
733         struct mtk_stp_hdr *shdr;
734         int err, dlen, type = 0;
735
736         /* Prepend skb with frame type */
737         memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
738
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,
743                                        GFP_ATOMIC);
744                 if (err < 0)
745                         return err;
746         }
747
748         /* Add the STP header */
749         dlen = skb->len;
750         shdr = skb_push(skb, sizeof(*shdr));
751         shdr->prefix = 0x80;
752         shdr->dlen = cpu_to_be16((dlen & 0x0fff) | (type << 12));
753         shdr->cs = 0;           /* MT7622 doesn't care about checksum value */
754
755         /* Add the STP trailer */
756         skb_put_zero(skb, MTK_STP_TLR_SIZE);
757
758         skb_queue_tail(&bdev->txq, skb);
759
760         btmtkuart_tx_wakeup(bdev);
761         return 0;
762 }
763
764 static int btmtkuart_parse_dt(struct serdev_device *serdev)
765 {
766         struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
767         struct device_node *node = serdev->dev.of_node;
768         u32 speed = 921600;
769         int err;
770
771         if (btmtkuart_is_standalone(bdev)) {
772                 of_property_read_u32(node, "current-speed", &speed);
773
774                 bdev->desired_speed = speed;
775
776                 bdev->vcc = devm_regulator_get(&serdev->dev, "vcc");
777                 if (IS_ERR(bdev->vcc)) {
778                         err = PTR_ERR(bdev->vcc);
779                         return err;
780                 }
781
782                 bdev->osc = devm_clk_get_optional(&serdev->dev, "osc");
783                 if (IS_ERR(bdev->osc)) {
784                         err = PTR_ERR(bdev->osc);
785                         return err;
786                 }
787
788                 bdev->boot = devm_gpiod_get_optional(&serdev->dev, "boot",
789                                                      GPIOD_OUT_LOW);
790                 if (IS_ERR(bdev->boot)) {
791                         err = PTR_ERR(bdev->boot);
792                         return err;
793                 }
794
795                 bdev->pinctrl = devm_pinctrl_get(&serdev->dev);
796                 if (IS_ERR(bdev->pinctrl)) {
797                         err = PTR_ERR(bdev->pinctrl);
798                         return err;
799                 }
800
801                 bdev->pins_boot = pinctrl_lookup_state(bdev->pinctrl,
802                                                        "default");
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");
807                         return err;
808                 }
809
810                 bdev->pins_runtime = pinctrl_lookup_state(bdev->pinctrl,
811                                                           "runtime");
812                 if (IS_ERR(bdev->pins_runtime)) {
813                         err = PTR_ERR(bdev->pins_runtime);
814                         return err;
815                 }
816
817                 bdev->reset = devm_gpiod_get_optional(&serdev->dev, "reset",
818                                                       GPIOD_OUT_LOW);
819                 if (IS_ERR(bdev->reset)) {
820                         err = PTR_ERR(bdev->reset);
821                         return err;
822                 }
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);
827         }
828
829         return 0;
830 }
831
832 static int btmtkuart_probe(struct serdev_device *serdev)
833 {
834         struct btmtkuart_dev *bdev;
835         struct hci_dev *hdev;
836         int err;
837
838         bdev = devm_kzalloc(&serdev->dev, sizeof(*bdev), GFP_KERNEL);
839         if (!bdev)
840                 return -ENOMEM;
841
842         bdev->data = of_device_get_match_data(&serdev->dev);
843         if (!bdev->data)
844                 return -ENODEV;
845
846         bdev->serdev = serdev;
847         serdev_device_set_drvdata(serdev, bdev);
848
849         serdev_device_set_client_ops(serdev, &btmtkuart_client_ops);
850
851         err = btmtkuart_parse_dt(serdev);
852         if (err < 0)
853                 return err;
854
855         INIT_WORK(&bdev->tx_work, btmtkuart_tx_work);
856         skb_queue_head_init(&bdev->txq);
857
858         /* Initialize and register HCI device */
859         hdev = hci_alloc_dev();
860         if (!hdev) {
861                 dev_err(&serdev->dev, "Can't allocate HCI device\n");
862                 return -ENOMEM;
863         }
864
865         bdev->hdev = hdev;
866
867         hdev->bus = HCI_UART;
868         hci_set_drvdata(hdev, bdev);
869
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);
878
879         hdev->manufacturer = 70;
880         set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
881
882         if (btmtkuart_is_standalone(bdev)) {
883                 err = clk_prepare_enable(bdev->osc);
884                 if (err < 0)
885                         goto err_hci_free_dev;
886
887                 if (bdev->boot) {
888                         gpiod_set_value_cansleep(bdev->boot, 1);
889                 } else {
890                         /* Switch to the specific pin state for the booting
891                          * requires.
892                          */
893                         pinctrl_select_state(bdev->pinctrl, bdev->pins_boot);
894                 }
895
896                 /* Power on */
897                 err = regulator_enable(bdev->vcc);
898                 if (err < 0)
899                         goto err_clk_disable_unprepare;
900
901                 /* Reset if the reset-gpios is available otherwise the board
902                  * -level design should be guaranteed.
903                  */
904                 if (bdev->reset) {
905                         gpiod_set_value_cansleep(bdev->reset, 1);
906                         usleep_range(1000, 2000);
907                         gpiod_set_value_cansleep(bdev->reset, 0);
908                 }
909
910                 /* Wait some time until device got ready and switch to the pin
911                  * mode the device requires for UART transfers.
912                  */
913                 msleep(50);
914
915                 if (bdev->boot)
916                         devm_gpiod_put(&serdev->dev, bdev->boot);
917
918                 pinctrl_select_state(bdev->pinctrl, bdev->pins_runtime);
919
920                 /* A standalone device doesn't depends on power domain on SoC,
921                  * so mark it as no callbacks.
922                  */
923                 pm_runtime_no_callbacks(&serdev->dev);
924
925                 set_bit(BTMTKUART_REQUIRED_WAKEUP, &bdev->tx_state);
926         }
927
928         err = hci_register_dev(hdev);
929         if (err < 0) {
930                 dev_err(&serdev->dev, "Can't register HCI device\n");
931                 goto err_regulator_disable;
932         }
933
934         return 0;
935
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);
942 err_hci_free_dev:
943         hci_free_dev(hdev);
944
945         return err;
946 }
947
948 static void btmtkuart_remove(struct serdev_device *serdev)
949 {
950         struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev);
951         struct hci_dev *hdev = bdev->hdev;
952
953         if (btmtkuart_is_standalone(bdev)) {
954                 regulator_disable(bdev->vcc);
955                 clk_disable_unprepare(bdev->osc);
956         }
957
958         hci_unregister_dev(hdev);
959         hci_free_dev(hdev);
960 }
961
962 static const struct btmtkuart_data mt7622_data = {
963         .fwname = FIRMWARE_MT7622,
964 };
965
966 static const struct btmtkuart_data mt7663_data = {
967         .flags = BTMTKUART_FLAG_STANDALONE_HW,
968         .fwname = FIRMWARE_MT7663,
969 };
970
971 static const struct btmtkuart_data mt7668_data = {
972         .flags = BTMTKUART_FLAG_STANDALONE_HW,
973         .fwname = FIRMWARE_MT7668,
974 };
975
976 #ifdef CONFIG_OF
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},
981         { }
982 };
983 MODULE_DEVICE_TABLE(of, mtk_of_match_table);
984 #endif
985
986 static struct serdev_device_driver btmtkuart_driver = {
987         .probe = btmtkuart_probe,
988         .remove = btmtkuart_remove,
989         .driver = {
990                 .name = "btmtkuart",
991                 .of_match_table = of_match_ptr(mtk_of_match_table),
992         },
993 };
994
995 module_serdev_device_driver(btmtkuart_driver);
996
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");