Commit | Line | Data |
---|---|---|
e9a2dd26 MH |
1 | /* |
2 | * | |
3 | * Bluetooth HCI UART driver for Broadcom devices | |
4 | * | |
5 | * Copyright (C) 2015 Intel Corporation | |
6 | * | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <linux/kernel.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/skbuff.h> | |
18aeb444 | 27 | #include <linux/firmware.h> |
0395ffc1 FD |
28 | #include <linux/module.h> |
29 | #include <linux/acpi.h> | |
30 | #include <linux/platform_device.h> | |
31 | #include <linux/clk.h> | |
32 | #include <linux/gpio/consumer.h> | |
33 | #include <linux/tty.h> | |
e9a2dd26 MH |
34 | |
35 | #include <net/bluetooth/bluetooth.h> | |
36 | #include <net/bluetooth/hci_core.h> | |
37 | ||
bdd8818e | 38 | #include "btbcm.h" |
e9a2dd26 | 39 | #include "hci_uart.h" |
bdd8818e | 40 | |
0395ffc1 FD |
41 | struct bcm_device { |
42 | struct list_head list; | |
43 | ||
44 | struct platform_device *pdev; | |
45 | ||
46 | const char *name; | |
47 | struct gpio_desc *device_wakeup; | |
48 | struct gpio_desc *shutdown; | |
49 | ||
50 | struct clk *clk; | |
51 | bool clk_enabled; | |
52 | }; | |
53 | ||
bdd8818e | 54 | struct bcm_data { |
0395ffc1 FD |
55 | struct sk_buff *rx_skb; |
56 | struct sk_buff_head txq; | |
57 | ||
58 | struct bcm_device *dev; | |
bdd8818e MH |
59 | }; |
60 | ||
0395ffc1 FD |
61 | /* List of BCM BT UART devices */ |
62 | static DEFINE_SPINLOCK(bcm_device_list_lock); | |
63 | static LIST_HEAD(bcm_device_list); | |
64 | ||
61b2fc2b FD |
65 | static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed) |
66 | { | |
67 | struct hci_dev *hdev = hu->hdev; | |
68 | struct sk_buff *skb; | |
69 | struct bcm_update_uart_baud_rate param; | |
70 | ||
71 | if (speed > 3000000) { | |
72 | struct bcm_write_uart_clock_setting clock; | |
73 | ||
74 | clock.type = BCM_UART_CLOCK_48MHZ; | |
75 | ||
76 | BT_DBG("%s: Set Controller clock (%d)", hdev->name, clock.type); | |
77 | ||
78 | /* This Broadcom specific command changes the UART's controller | |
79 | * clock for baud rate > 3000000. | |
80 | */ | |
81 | skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT); | |
82 | if (IS_ERR(skb)) { | |
83 | int err = PTR_ERR(skb); | |
84 | BT_ERR("%s: BCM: failed to write clock command (%d)", | |
85 | hdev->name, err); | |
86 | return err; | |
87 | } | |
88 | ||
89 | kfree_skb(skb); | |
90 | } | |
91 | ||
92 | BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev->name, speed); | |
93 | ||
94 | param.zero = cpu_to_le16(0); | |
95 | param.baud_rate = cpu_to_le32(speed); | |
96 | ||
97 | /* This Broadcom specific command changes the UART's controller baud | |
98 | * rate. | |
99 | */ | |
100 | skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), ¶m, | |
101 | HCI_INIT_TIMEOUT); | |
102 | if (IS_ERR(skb)) { | |
103 | int err = PTR_ERR(skb); | |
104 | BT_ERR("%s: BCM: failed to write update baudrate command (%d)", | |
105 | hdev->name, err); | |
106 | return err; | |
107 | } | |
108 | ||
109 | kfree_skb(skb); | |
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
0395ffc1 FD |
114 | /* bcm_device_exists should be protected by bcm_device_list_lock */ |
115 | static bool bcm_device_exists(struct bcm_device *device) | |
116 | { | |
117 | struct list_head *p; | |
118 | ||
119 | list_for_each(p, &bcm_device_list) { | |
120 | struct bcm_device *dev = list_entry(p, struct bcm_device, list); | |
121 | ||
122 | if (device == dev) | |
123 | return true; | |
124 | } | |
125 | ||
126 | return false; | |
127 | } | |
128 | ||
129 | static int bcm_gpio_set_power(struct bcm_device *dev, bool powered) | |
130 | { | |
131 | if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled) | |
132 | clk_enable(dev->clk); | |
133 | ||
134 | gpiod_set_value_cansleep(dev->shutdown, powered); | |
135 | gpiod_set_value_cansleep(dev->device_wakeup, powered); | |
136 | ||
137 | if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled) | |
138 | clk_disable(dev->clk); | |
139 | ||
140 | dev->clk_enabled = powered; | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
bdd8818e MH |
145 | static int bcm_open(struct hci_uart *hu) |
146 | { | |
147 | struct bcm_data *bcm; | |
0395ffc1 | 148 | struct list_head *p; |
bdd8818e MH |
149 | |
150 | BT_DBG("hu %p", hu); | |
151 | ||
152 | bcm = kzalloc(sizeof(*bcm), GFP_KERNEL); | |
153 | if (!bcm) | |
154 | return -ENOMEM; | |
155 | ||
156 | skb_queue_head_init(&bcm->txq); | |
157 | ||
158 | hu->priv = bcm; | |
0395ffc1 FD |
159 | |
160 | spin_lock(&bcm_device_list_lock); | |
161 | list_for_each(p, &bcm_device_list) { | |
162 | struct bcm_device *dev = list_entry(p, struct bcm_device, list); | |
163 | ||
164 | /* Retrieve saved bcm_device based on parent of the | |
165 | * platform device (saved during device probe) and | |
166 | * parent of tty device used by hci_uart | |
167 | */ | |
168 | if (hu->tty->dev->parent == dev->pdev->dev.parent) { | |
169 | bcm->dev = dev; | |
170 | break; | |
171 | } | |
172 | } | |
173 | ||
174 | if (bcm->dev) | |
175 | bcm_gpio_set_power(bcm->dev, true); | |
176 | ||
177 | spin_unlock(&bcm_device_list_lock); | |
178 | ||
bdd8818e MH |
179 | return 0; |
180 | } | |
181 | ||
182 | static int bcm_close(struct hci_uart *hu) | |
183 | { | |
184 | struct bcm_data *bcm = hu->priv; | |
185 | ||
186 | BT_DBG("hu %p", hu); | |
187 | ||
0395ffc1 FD |
188 | /* Protect bcm->dev against removal of the device or driver */ |
189 | spin_lock(&bcm_device_list_lock); | |
190 | if (bcm_device_exists(bcm->dev)) | |
191 | bcm_gpio_set_power(bcm->dev, false); | |
192 | spin_unlock(&bcm_device_list_lock); | |
193 | ||
bdd8818e MH |
194 | skb_queue_purge(&bcm->txq); |
195 | kfree_skb(bcm->rx_skb); | |
196 | kfree(bcm); | |
197 | ||
198 | hu->priv = NULL; | |
199 | return 0; | |
200 | } | |
201 | ||
202 | static int bcm_flush(struct hci_uart *hu) | |
203 | { | |
204 | struct bcm_data *bcm = hu->priv; | |
205 | ||
206 | BT_DBG("hu %p", hu); | |
207 | ||
208 | skb_queue_purge(&bcm->txq); | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
213 | static int bcm_setup(struct hci_uart *hu) | |
214 | { | |
6be09b48 FD |
215 | char fw_name[64]; |
216 | const struct firmware *fw; | |
960ef1d7 | 217 | unsigned int speed; |
6be09b48 FD |
218 | int err; |
219 | ||
bdd8818e MH |
220 | BT_DBG("hu %p", hu); |
221 | ||
222 | hu->hdev->set_bdaddr = btbcm_set_bdaddr; | |
223 | ||
6be09b48 FD |
224 | err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name)); |
225 | if (err) | |
226 | return err; | |
227 | ||
228 | err = request_firmware(&fw, fw_name, &hu->hdev->dev); | |
229 | if (err < 0) { | |
230 | BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name); | |
231 | return 0; | |
232 | } | |
233 | ||
234 | err = btbcm_patchram(hu->hdev, fw); | |
235 | if (err) { | |
236 | BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err); | |
237 | goto finalize; | |
238 | } | |
239 | ||
960ef1d7 FD |
240 | /* Init speed if any */ |
241 | if (hu->init_speed) | |
242 | speed = hu->init_speed; | |
243 | else if (hu->proto->init_speed) | |
244 | speed = hu->proto->init_speed; | |
245 | else | |
246 | speed = 0; | |
247 | ||
248 | if (speed) | |
249 | hci_uart_set_baudrate(hu, speed); | |
250 | ||
251 | /* Operational speed if any */ | |
252 | if (hu->oper_speed) | |
253 | speed = hu->oper_speed; | |
254 | else if (hu->proto->oper_speed) | |
255 | speed = hu->proto->oper_speed; | |
256 | else | |
257 | speed = 0; | |
258 | ||
259 | if (speed) { | |
260 | err = bcm_set_baudrate(hu, speed); | |
61b2fc2b | 261 | if (!err) |
960ef1d7 | 262 | hci_uart_set_baudrate(hu, speed); |
61b2fc2b FD |
263 | } |
264 | ||
6be09b48 FD |
265 | finalize: |
266 | release_firmware(fw); | |
267 | ||
268 | err = btbcm_finalize(hu->hdev); | |
269 | ||
270 | return err; | |
bdd8818e MH |
271 | } |
272 | ||
79b8df93 MH |
273 | static const struct h4_recv_pkt bcm_recv_pkts[] = { |
274 | { H4_RECV_ACL, .recv = hci_recv_frame }, | |
275 | { H4_RECV_SCO, .recv = hci_recv_frame }, | |
276 | { H4_RECV_EVENT, .recv = hci_recv_frame }, | |
277 | }; | |
278 | ||
bdd8818e MH |
279 | static int bcm_recv(struct hci_uart *hu, const void *data, int count) |
280 | { | |
281 | struct bcm_data *bcm = hu->priv; | |
282 | ||
283 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) | |
284 | return -EUNATCH; | |
285 | ||
79b8df93 MH |
286 | bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count, |
287 | bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts)); | |
bdd8818e MH |
288 | if (IS_ERR(bcm->rx_skb)) { |
289 | int err = PTR_ERR(bcm->rx_skb); | |
290 | BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err); | |
37134167 | 291 | bcm->rx_skb = NULL; |
bdd8818e MH |
292 | return err; |
293 | } | |
294 | ||
295 | return count; | |
296 | } | |
297 | ||
298 | static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb) | |
299 | { | |
300 | struct bcm_data *bcm = hu->priv; | |
301 | ||
302 | BT_DBG("hu %p skb %p", hu, skb); | |
303 | ||
304 | /* Prepend skb with frame type */ | |
305 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); | |
306 | skb_queue_tail(&bcm->txq, skb); | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | static struct sk_buff *bcm_dequeue(struct hci_uart *hu) | |
312 | { | |
313 | struct bcm_data *bcm = hu->priv; | |
314 | ||
315 | return skb_dequeue(&bcm->txq); | |
316 | } | |
317 | ||
0395ffc1 FD |
318 | static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false }; |
319 | static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false }; | |
320 | ||
321 | static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = { | |
322 | { "device-wakeup-gpios", &device_wakeup_gpios, 1 }, | |
323 | { "shutdown-gpios", &shutdown_gpios, 1 }, | |
324 | { }, | |
325 | }; | |
326 | ||
327 | static int bcm_acpi_probe(struct bcm_device *dev) | |
328 | { | |
329 | struct platform_device *pdev = dev->pdev; | |
330 | const struct acpi_device_id *id; | |
331 | struct gpio_desc *gpio; | |
332 | int ret; | |
333 | ||
334 | id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev); | |
335 | if (!id) | |
336 | return -ENODEV; | |
337 | ||
338 | /* Retrieve GPIO data */ | |
339 | dev->name = dev_name(&pdev->dev); | |
340 | ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev), | |
341 | acpi_bcm_default_gpios); | |
342 | if (ret) | |
343 | return ret; | |
344 | ||
345 | dev->clk = devm_clk_get(&pdev->dev, NULL); | |
346 | ||
347 | gpio = devm_gpiod_get(&pdev->dev, "device-wakeup"); | |
348 | if (!IS_ERR(gpio)) { | |
349 | ret = gpiod_direction_output(gpio, 0); | |
350 | if (ret) | |
351 | return ret; | |
352 | dev->device_wakeup = gpio; | |
353 | } | |
354 | ||
355 | gpio = devm_gpiod_get(&pdev->dev, "shutdown"); | |
356 | if (!IS_ERR(gpio)) { | |
357 | ret = gpiod_direction_output(gpio, 0); | |
358 | if (ret) | |
359 | return ret; | |
360 | dev->shutdown = gpio; | |
361 | } | |
362 | ||
363 | /* Make sure at-least one of the GPIO is defined and that | |
364 | * a name is specified for this instance | |
365 | */ | |
366 | if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) { | |
367 | dev_err(&pdev->dev, "invalid platform data\n"); | |
368 | return -EINVAL; | |
369 | } | |
370 | ||
371 | return 0; | |
372 | } | |
373 | ||
374 | static int bcm_probe(struct platform_device *pdev) | |
375 | { | |
376 | struct bcm_device *dev; | |
377 | struct acpi_device_id *pdata = pdev->dev.platform_data; | |
378 | int ret; | |
379 | ||
380 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); | |
381 | if (!dev) | |
382 | return -ENOMEM; | |
383 | ||
384 | dev->pdev = pdev; | |
385 | ||
386 | if (ACPI_HANDLE(&pdev->dev)) { | |
387 | ret = bcm_acpi_probe(dev); | |
388 | if (ret) | |
389 | return ret; | |
390 | } else if (pdata) { | |
391 | dev->name = pdata->id; | |
392 | } else { | |
393 | return -ENODEV; | |
394 | } | |
395 | ||
396 | platform_set_drvdata(pdev, dev); | |
397 | ||
398 | dev_info(&pdev->dev, "%s device registered.\n", dev->name); | |
399 | ||
400 | /* Place this instance on the device list */ | |
401 | spin_lock(&bcm_device_list_lock); | |
402 | list_add_tail(&dev->list, &bcm_device_list); | |
403 | spin_unlock(&bcm_device_list_lock); | |
404 | ||
405 | bcm_gpio_set_power(dev, false); | |
406 | ||
407 | return 0; | |
408 | } | |
409 | ||
410 | static int bcm_remove(struct platform_device *pdev) | |
411 | { | |
412 | struct bcm_device *dev = platform_get_drvdata(pdev); | |
413 | ||
414 | spin_lock(&bcm_device_list_lock); | |
415 | list_del(&dev->list); | |
416 | spin_unlock(&bcm_device_list_lock); | |
417 | ||
418 | acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev)); | |
419 | ||
420 | dev_info(&pdev->dev, "%s device unregistered.\n", dev->name); | |
421 | ||
422 | return 0; | |
423 | } | |
424 | ||
bdd8818e MH |
425 | static const struct hci_uart_proto bcm_proto = { |
426 | .id = HCI_UART_BCM, | |
427 | .name = "BCM", | |
61b2fc2b FD |
428 | .init_speed = 115200, |
429 | .oper_speed = 4000000, | |
bdd8818e MH |
430 | .open = bcm_open, |
431 | .close = bcm_close, | |
432 | .flush = bcm_flush, | |
433 | .setup = bcm_setup, | |
61b2fc2b | 434 | .set_baudrate = bcm_set_baudrate, |
bdd8818e MH |
435 | .recv = bcm_recv, |
436 | .enqueue = bcm_enqueue, | |
437 | .dequeue = bcm_dequeue, | |
438 | }; | |
439 | ||
0395ffc1 FD |
440 | #ifdef CONFIG_ACPI |
441 | static const struct acpi_device_id bcm_acpi_match[] = { | |
442 | { "BCM2E39", 0 }, | |
443 | { "BCM2E67", 0 }, | |
444 | { }, | |
445 | }; | |
446 | MODULE_DEVICE_TABLE(acpi, bcm_acpi_match); | |
447 | #endif | |
448 | ||
449 | static struct platform_driver bcm_driver = { | |
450 | .probe = bcm_probe, | |
451 | .remove = bcm_remove, | |
452 | .driver = { | |
453 | .name = "hci_bcm", | |
454 | .acpi_match_table = ACPI_PTR(bcm_acpi_match), | |
455 | }, | |
456 | }; | |
457 | ||
bdd8818e MH |
458 | int __init bcm_init(void) |
459 | { | |
0395ffc1 FD |
460 | platform_driver_register(&bcm_driver); |
461 | ||
bdd8818e MH |
462 | return hci_uart_register_proto(&bcm_proto); |
463 | } | |
464 | ||
465 | int __exit bcm_deinit(void) | |
466 | { | |
0395ffc1 FD |
467 | platform_driver_unregister(&bcm_driver); |
468 | ||
bdd8818e MH |
469 | return hci_uart_unregister_proto(&bcm_proto); |
470 | } |