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