blob: 322302b04710f1764fd339247f0ee32aed924743 [file] [log] [blame]
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -07001/*
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>
Frederic Danis18aeb442015-05-28 11:25:01 +020027#include <linux/firmware.h>
Frederic Danis0395ffc2015-08-11 16:35:35 +020028#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>
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -070034
35#include <net/bluetooth/bluetooth.h>
36#include <net/bluetooth/hci_core.h>
37
Marcel Holtmannbdd88182015-04-05 22:52:18 -070038#include "btbcm.h"
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -070039#include "hci_uart.h"
Marcel Holtmannbdd88182015-04-05 22:52:18 -070040
Frederic Danis0395ffc2015-08-11 16:35:35 +020041struct 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;
Frederic Danisae056902015-08-11 16:35:37 +020052
53 u32 init_speed;
Frederic Danis118612f2015-08-11 16:35:38 +020054
55#ifdef CONFIG_PM_SLEEP
56 struct hci_uart *hu;
57 bool is_suspended; /* suspend/resume flag */
58#endif
Marcel Holtmannbdd88182015-04-05 22:52:18 -070059};
60
Frederic Danis0395ffc2015-08-11 16:35:35 +020061struct bcm_data {
62 struct sk_buff *rx_skb;
63 struct sk_buff_head txq;
64
65 struct bcm_device *dev;
66};
67
68/* List of BCM BT UART devices */
69static DEFINE_SPINLOCK(bcm_device_list_lock);
70static LIST_HEAD(bcm_device_list);
71
Frederic Danis61b2fc22015-06-09 16:15:37 +020072static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
73{
74 struct hci_dev *hdev = hu->hdev;
75 struct sk_buff *skb;
76 struct bcm_update_uart_baud_rate param;
77
78 if (speed > 3000000) {
79 struct bcm_write_uart_clock_setting clock;
80
81 clock.type = BCM_UART_CLOCK_48MHZ;
82
83 BT_DBG("%s: Set Controller clock (%d)", hdev->name, clock.type);
84
85 /* This Broadcom specific command changes the UART's controller
86 * clock for baud rate > 3000000.
87 */
88 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
89 if (IS_ERR(skb)) {
90 int err = PTR_ERR(skb);
91 BT_ERR("%s: BCM: failed to write clock command (%d)",
92 hdev->name, err);
93 return err;
94 }
95
96 kfree_skb(skb);
97 }
98
99 BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev->name, speed);
100
101 param.zero = cpu_to_le16(0);
102 param.baud_rate = cpu_to_le32(speed);
103
104 /* This Broadcom specific command changes the UART's controller baud
105 * rate.
106 */
107 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
108 HCI_INIT_TIMEOUT);
109 if (IS_ERR(skb)) {
110 int err = PTR_ERR(skb);
111 BT_ERR("%s: BCM: failed to write update baudrate command (%d)",
112 hdev->name, err);
113 return err;
114 }
115
116 kfree_skb(skb);
117
118 return 0;
119}
120
Frederic Danis0395ffc2015-08-11 16:35:35 +0200121/* bcm_device_exists should be protected by bcm_device_list_lock */
122static bool bcm_device_exists(struct bcm_device *device)
123{
124 struct list_head *p;
125
126 list_for_each(p, &bcm_device_list) {
127 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
128
129 if (device == dev)
130 return true;
131 }
132
133 return false;
134}
135
136static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
137{
138 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
139 clk_enable(dev->clk);
140
141 gpiod_set_value_cansleep(dev->shutdown, powered);
142 gpiod_set_value_cansleep(dev->device_wakeup, powered);
143
144 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
145 clk_disable(dev->clk);
146
147 dev->clk_enabled = powered;
148
149 return 0;
150}
151
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700152static int bcm_open(struct hci_uart *hu)
153{
154 struct bcm_data *bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200155 struct list_head *p;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700156
157 BT_DBG("hu %p", hu);
158
159 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
160 if (!bcm)
161 return -ENOMEM;
162
163 skb_queue_head_init(&bcm->txq);
164
165 hu->priv = bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200166
167 spin_lock(&bcm_device_list_lock);
168 list_for_each(p, &bcm_device_list) {
169 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
170
171 /* Retrieve saved bcm_device based on parent of the
172 * platform device (saved during device probe) and
173 * parent of tty device used by hci_uart
174 */
175 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
176 bcm->dev = dev;
Frederic Danisae056902015-08-11 16:35:37 +0200177 hu->init_speed = dev->init_speed;
Frederic Danis118612f2015-08-11 16:35:38 +0200178#ifdef CONFIG_PM_SLEEP
179 dev->hu = hu;
180#endif
Frederic Danis0395ffc2015-08-11 16:35:35 +0200181 break;
182 }
183 }
184
185 if (bcm->dev)
186 bcm_gpio_set_power(bcm->dev, true);
187
188 spin_unlock(&bcm_device_list_lock);
189
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700190 return 0;
191}
192
193static int bcm_close(struct hci_uart *hu)
194{
195 struct bcm_data *bcm = hu->priv;
196
197 BT_DBG("hu %p", hu);
198
Frederic Danis0395ffc2015-08-11 16:35:35 +0200199 /* Protect bcm->dev against removal of the device or driver */
200 spin_lock(&bcm_device_list_lock);
Frederic Danis118612f2015-08-11 16:35:38 +0200201 if (bcm_device_exists(bcm->dev)) {
Frederic Danis0395ffc2015-08-11 16:35:35 +0200202 bcm_gpio_set_power(bcm->dev, false);
Frederic Danis118612f2015-08-11 16:35:38 +0200203#ifdef CONFIG_PM_SLEEP
204 bcm->dev->hu = NULL;
205#endif
206 }
Frederic Danis0395ffc2015-08-11 16:35:35 +0200207 spin_unlock(&bcm_device_list_lock);
208
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700209 skb_queue_purge(&bcm->txq);
210 kfree_skb(bcm->rx_skb);
211 kfree(bcm);
212
213 hu->priv = NULL;
214 return 0;
215}
216
217static int bcm_flush(struct hci_uart *hu)
218{
219 struct bcm_data *bcm = hu->priv;
220
221 BT_DBG("hu %p", hu);
222
223 skb_queue_purge(&bcm->txq);
224
225 return 0;
226}
227
228static int bcm_setup(struct hci_uart *hu)
229{
Frederic Danis6be09b42015-05-28 11:25:05 +0200230 char fw_name[64];
231 const struct firmware *fw;
Frederic Danis960ef1d2015-06-18 12:43:27 +0200232 unsigned int speed;
Frederic Danis6be09b42015-05-28 11:25:05 +0200233 int err;
234
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700235 BT_DBG("hu %p", hu);
236
237 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
238
Frederic Danis6be09b42015-05-28 11:25:05 +0200239 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
240 if (err)
241 return err;
242
243 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
244 if (err < 0) {
245 BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name);
246 return 0;
247 }
248
249 err = btbcm_patchram(hu->hdev, fw);
250 if (err) {
251 BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err);
252 goto finalize;
253 }
254
Frederic Danis960ef1d2015-06-18 12:43:27 +0200255 /* Init speed if any */
256 if (hu->init_speed)
257 speed = hu->init_speed;
258 else if (hu->proto->init_speed)
259 speed = hu->proto->init_speed;
260 else
261 speed = 0;
Frederic Danis6be09b42015-05-28 11:25:05 +0200262
Frederic Danis960ef1d2015-06-18 12:43:27 +0200263 if (speed)
264 hci_uart_set_baudrate(hu, speed);
265
266 /* Operational speed if any */
267 if (hu->oper_speed)
268 speed = hu->oper_speed;
269 else if (hu->proto->oper_speed)
270 speed = hu->proto->oper_speed;
271 else
272 speed = 0;
273
274 if (speed) {
275 err = bcm_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200276 if (!err)
Frederic Danis960ef1d2015-06-18 12:43:27 +0200277 hci_uart_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200278 }
279
Frederic Danis6be09b42015-05-28 11:25:05 +0200280finalize:
281 release_firmware(fw);
282
283 err = btbcm_finalize(hu->hdev);
284
285 return err;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700286}
287
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700288static const struct h4_recv_pkt bcm_recv_pkts[] = {
289 { H4_RECV_ACL, .recv = hci_recv_frame },
290 { H4_RECV_SCO, .recv = hci_recv_frame },
291 { H4_RECV_EVENT, .recv = hci_recv_frame },
292};
293
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700294static int bcm_recv(struct hci_uart *hu, const void *data, int count)
295{
296 struct bcm_data *bcm = hu->priv;
297
298 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
299 return -EUNATCH;
300
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700301 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
302 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700303 if (IS_ERR(bcm->rx_skb)) {
304 int err = PTR_ERR(bcm->rx_skb);
305 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
Chan-yeol Park37134162015-06-17 21:10:39 +0900306 bcm->rx_skb = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700307 return err;
308 }
309
310 return count;
311}
312
313static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
314{
315 struct bcm_data *bcm = hu->priv;
316
317 BT_DBG("hu %p skb %p", hu, skb);
318
319 /* Prepend skb with frame type */
320 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
321 skb_queue_tail(&bcm->txq, skb);
322
323 return 0;
324}
325
326static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
327{
328 struct bcm_data *bcm = hu->priv;
329
330 return skb_dequeue(&bcm->txq);
331}
332
Frederic Danis118612f2015-08-11 16:35:38 +0200333#ifdef CONFIG_PM_SLEEP
334/* Platform suspend callback */
335static int bcm_suspend(struct device *dev)
336{
337 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
338
339 BT_DBG("suspend (%p): is_suspended %d", bdev, bdev->is_suspended);
340
341 if (!bdev->is_suspended) {
342 hci_uart_set_flow_control(bdev->hu, true);
343
344 /* Once this callback returns, driver suspends BT via GPIO */
345 bdev->is_suspended = true;
346 }
347
348 /* Suspend the device */
349 if (bdev->device_wakeup) {
350 gpiod_set_value(bdev->device_wakeup, false);
351 BT_DBG("suspend, delaying 15 ms");
352 mdelay(15);
353 }
354
355 return 0;
356}
357
358/* Platform resume callback */
359static int bcm_resume(struct device *dev)
360{
361 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
362
363 BT_DBG("resume (%p): is_suspended %d", bdev, bdev->is_suspended);
364
365 if (bdev->device_wakeup) {
366 gpiod_set_value(bdev->device_wakeup, true);
367 BT_DBG("resume, delaying 15 ms");
368 mdelay(15);
369 }
370
371 /* When this callback executes, the device has woken up already */
372 if (bdev->is_suspended) {
373 bdev->is_suspended = false;
374
375 hci_uart_set_flow_control(bdev->hu, false);
376 }
377
378 return 0;
379}
380#endif
381
Frederic Danis0395ffc2015-08-11 16:35:35 +0200382static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
383static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
384
385static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
386 { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
387 { "shutdown-gpios", &shutdown_gpios, 1 },
388 { },
389};
390
Frederic Danis50d78bc2015-08-12 12:46:01 +0200391#ifdef CONFIG_ACPI
Frederic Danisae056902015-08-11 16:35:37 +0200392static int bcm_resource(struct acpi_resource *ares, void *data)
393{
394 struct bcm_device *dev = data;
395
396 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
397 struct acpi_resource_uart_serialbus *sb;
398
399 sb = &ares->data.uart_serial_bus;
400 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
401 dev->init_speed = sb->default_baud_rate;
402 }
403
404 /* Always tell the ACPI core to skip this resource */
405 return 1;
406}
407
Frederic Danis0395ffc2015-08-11 16:35:35 +0200408static int bcm_acpi_probe(struct bcm_device *dev)
409{
410 struct platform_device *pdev = dev->pdev;
411 const struct acpi_device_id *id;
Frederic Danisae056902015-08-11 16:35:37 +0200412 struct acpi_device *adev;
413 LIST_HEAD(resources);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200414 int ret;
415
416 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
417 if (!id)
418 return -ENODEV;
419
420 /* Retrieve GPIO data */
421 dev->name = dev_name(&pdev->dev);
422 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
423 acpi_bcm_default_gpios);
424 if (ret)
425 return ret;
426
427 dev->clk = devm_clk_get(&pdev->dev, NULL);
428
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200429 dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
430 "device-wakeup",
431 GPIOD_OUT_LOW);
432 if (IS_ERR(dev->device_wakeup))
433 return PTR_ERR(dev->device_wakeup);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200434
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200435 dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
436 GPIOD_OUT_LOW);
437 if (IS_ERR(dev->shutdown))
438 return PTR_ERR(dev->shutdown);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200439
440 /* Make sure at-least one of the GPIO is defined and that
441 * a name is specified for this instance
442 */
443 if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
444 dev_err(&pdev->dev, "invalid platform data\n");
445 return -EINVAL;
446 }
447
Frederic Danisae056902015-08-11 16:35:37 +0200448 /* Retrieve UART ACPI info */
449 adev = ACPI_COMPANION(&dev->pdev->dev);
450 if (!adev)
451 return 0;
452
453 acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
454
Frederic Danis0395ffc2015-08-11 16:35:35 +0200455 return 0;
456}
Frederic Danis50d78bc2015-08-12 12:46:01 +0200457#else
458static int bcm_acpi_probe(struct bcm_device *dev)
459{
460 return -EINVAL;
461}
462#endif /* CONFIG_ACPI */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200463
464static int bcm_probe(struct platform_device *pdev)
465{
466 struct bcm_device *dev;
467 struct acpi_device_id *pdata = pdev->dev.platform_data;
468 int ret;
469
470 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
471 if (!dev)
472 return -ENOMEM;
473
474 dev->pdev = pdev;
475
476 if (ACPI_HANDLE(&pdev->dev)) {
477 ret = bcm_acpi_probe(dev);
478 if (ret)
479 return ret;
480 } else if (pdata) {
481 dev->name = pdata->id;
482 } else {
483 return -ENODEV;
484 }
485
486 platform_set_drvdata(pdev, dev);
487
488 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
489
490 /* Place this instance on the device list */
491 spin_lock(&bcm_device_list_lock);
492 list_add_tail(&dev->list, &bcm_device_list);
493 spin_unlock(&bcm_device_list_lock);
494
495 bcm_gpio_set_power(dev, false);
496
497 return 0;
498}
499
500static int bcm_remove(struct platform_device *pdev)
501{
502 struct bcm_device *dev = platform_get_drvdata(pdev);
503
504 spin_lock(&bcm_device_list_lock);
505 list_del(&dev->list);
506 spin_unlock(&bcm_device_list_lock);
507
508 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
509
510 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
511
512 return 0;
513}
514
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700515static const struct hci_uart_proto bcm_proto = {
516 .id = HCI_UART_BCM,
517 .name = "BCM",
Frederic Danis61b2fc22015-06-09 16:15:37 +0200518 .init_speed = 115200,
519 .oper_speed = 4000000,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700520 .open = bcm_open,
521 .close = bcm_close,
522 .flush = bcm_flush,
523 .setup = bcm_setup,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200524 .set_baudrate = bcm_set_baudrate,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700525 .recv = bcm_recv,
526 .enqueue = bcm_enqueue,
527 .dequeue = bcm_dequeue,
528};
529
Frederic Danis0395ffc2015-08-11 16:35:35 +0200530#ifdef CONFIG_ACPI
531static const struct acpi_device_id bcm_acpi_match[] = {
532 { "BCM2E39", 0 },
533 { "BCM2E67", 0 },
534 { },
535};
536MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
537#endif
538
Frederic Danis118612f2015-08-11 16:35:38 +0200539/* Platform suspend and resume callbacks */
540static SIMPLE_DEV_PM_OPS(bcm_pm_ops, bcm_suspend, bcm_resume);
541
Frederic Danis0395ffc2015-08-11 16:35:35 +0200542static struct platform_driver bcm_driver = {
543 .probe = bcm_probe,
544 .remove = bcm_remove,
545 .driver = {
546 .name = "hci_bcm",
547 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
Frederic Danis118612f2015-08-11 16:35:38 +0200548 .pm = &bcm_pm_ops,
Frederic Danis0395ffc2015-08-11 16:35:35 +0200549 },
550};
551
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700552int __init bcm_init(void)
553{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200554 platform_driver_register(&bcm_driver);
555
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700556 return hci_uart_register_proto(&bcm_proto);
557}
558
559int __exit bcm_deinit(void)
560{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200561 platform_driver_unregister(&bcm_driver);
562
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700563 return hci_uart_unregister_proto(&bcm_proto);
564}