blob: cb7fe837f7d914e00c8b376b34114962f0b3f22a [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 Danisae056902015-08-11 16:35:37 +0200391static int bcm_resource(struct acpi_resource *ares, void *data)
392{
393 struct bcm_device *dev = data;
394
395 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
396 struct acpi_resource_uart_serialbus *sb;
397
398 sb = &ares->data.uart_serial_bus;
399 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
400 dev->init_speed = sb->default_baud_rate;
401 }
402
403 /* Always tell the ACPI core to skip this resource */
404 return 1;
405}
406
Frederic Danis0395ffc2015-08-11 16:35:35 +0200407static int bcm_acpi_probe(struct bcm_device *dev)
408{
409 struct platform_device *pdev = dev->pdev;
410 const struct acpi_device_id *id;
411 struct gpio_desc *gpio;
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
429 gpio = devm_gpiod_get(&pdev->dev, "device-wakeup");
430 if (!IS_ERR(gpio)) {
431 ret = gpiod_direction_output(gpio, 0);
432 if (ret)
433 return ret;
434 dev->device_wakeup = gpio;
435 }
436
437 gpio = devm_gpiod_get(&pdev->dev, "shutdown");
438 if (!IS_ERR(gpio)) {
439 ret = gpiod_direction_output(gpio, 0);
440 if (ret)
441 return ret;
442 dev->shutdown = gpio;
443 }
444
445 /* Make sure at-least one of the GPIO is defined and that
446 * a name is specified for this instance
447 */
448 if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
449 dev_err(&pdev->dev, "invalid platform data\n");
450 return -EINVAL;
451 }
452
Frederic Danisae056902015-08-11 16:35:37 +0200453 /* Retrieve UART ACPI info */
454 adev = ACPI_COMPANION(&dev->pdev->dev);
455 if (!adev)
456 return 0;
457
458 acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
459
Frederic Danis0395ffc2015-08-11 16:35:35 +0200460 return 0;
461}
462
463static int bcm_probe(struct platform_device *pdev)
464{
465 struct bcm_device *dev;
466 struct acpi_device_id *pdata = pdev->dev.platform_data;
467 int ret;
468
469 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
470 if (!dev)
471 return -ENOMEM;
472
473 dev->pdev = pdev;
474
475 if (ACPI_HANDLE(&pdev->dev)) {
476 ret = bcm_acpi_probe(dev);
477 if (ret)
478 return ret;
479 } else if (pdata) {
480 dev->name = pdata->id;
481 } else {
482 return -ENODEV;
483 }
484
485 platform_set_drvdata(pdev, dev);
486
487 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
488
489 /* Place this instance on the device list */
490 spin_lock(&bcm_device_list_lock);
491 list_add_tail(&dev->list, &bcm_device_list);
492 spin_unlock(&bcm_device_list_lock);
493
494 bcm_gpio_set_power(dev, false);
495
496 return 0;
497}
498
499static int bcm_remove(struct platform_device *pdev)
500{
501 struct bcm_device *dev = platform_get_drvdata(pdev);
502
503 spin_lock(&bcm_device_list_lock);
504 list_del(&dev->list);
505 spin_unlock(&bcm_device_list_lock);
506
507 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
508
509 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
510
511 return 0;
512}
513
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700514static const struct hci_uart_proto bcm_proto = {
515 .id = HCI_UART_BCM,
516 .name = "BCM",
Frederic Danis61b2fc22015-06-09 16:15:37 +0200517 .init_speed = 115200,
518 .oper_speed = 4000000,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700519 .open = bcm_open,
520 .close = bcm_close,
521 .flush = bcm_flush,
522 .setup = bcm_setup,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200523 .set_baudrate = bcm_set_baudrate,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700524 .recv = bcm_recv,
525 .enqueue = bcm_enqueue,
526 .dequeue = bcm_dequeue,
527};
528
Frederic Danis0395ffc2015-08-11 16:35:35 +0200529#ifdef CONFIG_ACPI
530static const struct acpi_device_id bcm_acpi_match[] = {
531 { "BCM2E39", 0 },
532 { "BCM2E67", 0 },
533 { },
534};
535MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
536#endif
537
Frederic Danis118612f2015-08-11 16:35:38 +0200538/* Platform suspend and resume callbacks */
539static SIMPLE_DEV_PM_OPS(bcm_pm_ops, bcm_suspend, bcm_resume);
540
Frederic Danis0395ffc2015-08-11 16:35:35 +0200541static struct platform_driver bcm_driver = {
542 .probe = bcm_probe,
543 .remove = bcm_remove,
544 .driver = {
545 .name = "hci_bcm",
546 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
Frederic Danis118612f2015-08-11 16:35:38 +0200547 .pm = &bcm_pm_ops,
Frederic Danis0395ffc2015-08-11 16:35:35 +0200548 },
549};
550
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700551int __init bcm_init(void)
552{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200553 platform_driver_register(&bcm_driver);
554
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700555 return hci_uart_register_proto(&bcm_proto);
556}
557
558int __exit bcm_deinit(void)
559{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200560 platform_driver_unregister(&bcm_driver);
561
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700562 return hci_uart_unregister_proto(&bcm_proto);
563}