checkpatch: fix left brace warning
[deliverable/linux.git] / drivers / bluetooth / hci_bcm.c
CommitLineData
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
41struct 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;
ae056908
FD
52
53 u32 init_speed;
118612fb
FD
54
55#ifdef CONFIG_PM_SLEEP
56 struct hci_uart *hu;
57 bool is_suspended; /* suspend/resume flag */
58#endif
0395ffc1
FD
59};
60
bdd8818e 61struct bcm_data {
0395ffc1
FD
62 struct sk_buff *rx_skb;
63 struct sk_buff_head txq;
64
65 struct bcm_device *dev;
bdd8818e
MH
66};
67
0395ffc1 68/* List of BCM BT UART devices */
917522aa 69static DEFINE_SPINLOCK(bcm_device_lock);
0395ffc1
FD
70static LIST_HEAD(bcm_device_list);
71
61b2fc2b
FD
72static 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
917522aa 121/* bcm_device_exists should be protected by bcm_device_lock */
0395ffc1
FD
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
2af0a709
LP
141 gpiod_set_value(dev->shutdown, powered);
142 gpiod_set_value(dev->device_wakeup, powered);
0395ffc1
FD
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
bdd8818e
MH
152static int bcm_open(struct hci_uart *hu)
153{
154 struct bcm_data *bcm;
0395ffc1 155 struct list_head *p;
bdd8818e
MH
156
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;
0395ffc1 166
917522aa 167 spin_lock(&bcm_device_lock);
0395ffc1
FD
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;
ae056908 177 hu->init_speed = dev->init_speed;
118612fb
FD
178#ifdef CONFIG_PM_SLEEP
179 dev->hu = hu;
180#endif
0395ffc1
FD
181 break;
182 }
183 }
184
185 if (bcm->dev)
186 bcm_gpio_set_power(bcm->dev, true);
187
917522aa 188 spin_unlock(&bcm_device_lock);
0395ffc1 189
bdd8818e
MH
190 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
0395ffc1 199 /* Protect bcm->dev against removal of the device or driver */
917522aa 200 spin_lock(&bcm_device_lock);
118612fb 201 if (bcm_device_exists(bcm->dev)) {
0395ffc1 202 bcm_gpio_set_power(bcm->dev, false);
118612fb
FD
203#ifdef CONFIG_PM_SLEEP
204 bcm->dev->hu = NULL;
205#endif
206 }
917522aa 207 spin_unlock(&bcm_device_lock);
0395ffc1 208
bdd8818e
MH
209 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{
6be09b48
FD
230 char fw_name[64];
231 const struct firmware *fw;
960ef1d7 232 unsigned int speed;
6be09b48
FD
233 int err;
234
bdd8818e
MH
235 BT_DBG("hu %p", hu);
236
237 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
238
6be09b48
FD
239 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
960ef1d7
FD
255 /* 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;
262
263 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);
61b2fc2b 276 if (!err)
960ef1d7 277 hci_uart_set_baudrate(hu, speed);
61b2fc2b
FD
278 }
279
6be09b48
FD
280finalize:
281 release_firmware(fw);
282
283 err = btbcm_finalize(hu->hdev);
284
285 return err;
bdd8818e
MH
286}
287
79b8df93
MH
288static 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
bdd8818e
MH
294static 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
79b8df93
MH
301 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
302 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
bdd8818e
MH
303 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);
37134167 306 bcm->rx_skb = NULL;
bdd8818e
MH
307 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
118612fb
FD
333#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
917522aa
FD
341 spin_lock(&bcm_device_lock);
342
343 if (!bdev->hu)
344 goto unlock;
345
118612fb
FD
346 if (!bdev->is_suspended) {
347 hci_uart_set_flow_control(bdev->hu, true);
348
349 /* Once this callback returns, driver suspends BT via GPIO */
350 bdev->is_suspended = true;
351 }
352
353 /* Suspend the device */
354 if (bdev->device_wakeup) {
355 gpiod_set_value(bdev->device_wakeup, false);
356 BT_DBG("suspend, delaying 15 ms");
357 mdelay(15);
358 }
359
917522aa
FD
360unlock:
361 spin_unlock(&bcm_device_lock);
362
118612fb
FD
363 return 0;
364}
365
366/* Platform resume callback */
367static int bcm_resume(struct device *dev)
368{
369 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
370
371 BT_DBG("resume (%p): is_suspended %d", bdev, bdev->is_suspended);
372
917522aa
FD
373 spin_lock(&bcm_device_lock);
374
375 if (!bdev->hu)
376 goto unlock;
377
118612fb
FD
378 if (bdev->device_wakeup) {
379 gpiod_set_value(bdev->device_wakeup, true);
380 BT_DBG("resume, delaying 15 ms");
381 mdelay(15);
382 }
383
384 /* When this callback executes, the device has woken up already */
385 if (bdev->is_suspended) {
386 bdev->is_suspended = false;
387
388 hci_uart_set_flow_control(bdev->hu, false);
389 }
390
917522aa
FD
391unlock:
392 spin_unlock(&bcm_device_lock);
393
118612fb
FD
394 return 0;
395}
396#endif
397
0395ffc1
FD
398static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
399static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
400
401static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
402 { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
403 { "shutdown-gpios", &shutdown_gpios, 1 },
404 { },
405};
406
50d78bcf 407#ifdef CONFIG_ACPI
ae056908
FD
408static int bcm_resource(struct acpi_resource *ares, void *data)
409{
410 struct bcm_device *dev = data;
411
412 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
413 struct acpi_resource_uart_serialbus *sb;
414
415 sb = &ares->data.uart_serial_bus;
416 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
417 dev->init_speed = sb->default_baud_rate;
418 }
419
420 /* Always tell the ACPI core to skip this resource */
421 return 1;
422}
423
0395ffc1
FD
424static int bcm_acpi_probe(struct bcm_device *dev)
425{
426 struct platform_device *pdev = dev->pdev;
427 const struct acpi_device_id *id;
ae056908
FD
428 struct acpi_device *adev;
429 LIST_HEAD(resources);
0395ffc1
FD
430 int ret;
431
432 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
433 if (!id)
434 return -ENODEV;
435
436 /* Retrieve GPIO data */
437 dev->name = dev_name(&pdev->dev);
438 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
439 acpi_bcm_default_gpios);
440 if (ret)
441 return ret;
442
443 dev->clk = devm_clk_get(&pdev->dev, NULL);
444
62aaefa7
UKK
445 dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
446 "device-wakeup",
447 GPIOD_OUT_LOW);
448 if (IS_ERR(dev->device_wakeup))
449 return PTR_ERR(dev->device_wakeup);
0395ffc1 450
62aaefa7
UKK
451 dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
452 GPIOD_OUT_LOW);
453 if (IS_ERR(dev->shutdown))
454 return PTR_ERR(dev->shutdown);
0395ffc1
FD
455
456 /* Make sure at-least one of the GPIO is defined and that
457 * a name is specified for this instance
458 */
459 if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
460 dev_err(&pdev->dev, "invalid platform data\n");
461 return -EINVAL;
462 }
463
ae056908
FD
464 /* Retrieve UART ACPI info */
465 adev = ACPI_COMPANION(&dev->pdev->dev);
466 if (!adev)
467 return 0;
468
469 acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
470
0395ffc1
FD
471 return 0;
472}
50d78bcf
FD
473#else
474static int bcm_acpi_probe(struct bcm_device *dev)
475{
476 return -EINVAL;
477}
478#endif /* CONFIG_ACPI */
0395ffc1
FD
479
480static int bcm_probe(struct platform_device *pdev)
481{
482 struct bcm_device *dev;
483 struct acpi_device_id *pdata = pdev->dev.platform_data;
484 int ret;
485
486 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
487 if (!dev)
488 return -ENOMEM;
489
490 dev->pdev = pdev;
491
492 if (ACPI_HANDLE(&pdev->dev)) {
493 ret = bcm_acpi_probe(dev);
494 if (ret)
495 return ret;
496 } else if (pdata) {
497 dev->name = pdata->id;
498 } else {
499 return -ENODEV;
500 }
501
502 platform_set_drvdata(pdev, dev);
503
504 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
505
506 /* Place this instance on the device list */
917522aa 507 spin_lock(&bcm_device_lock);
0395ffc1 508 list_add_tail(&dev->list, &bcm_device_list);
917522aa 509 spin_unlock(&bcm_device_lock);
0395ffc1
FD
510
511 bcm_gpio_set_power(dev, false);
512
513 return 0;
514}
515
516static int bcm_remove(struct platform_device *pdev)
517{
518 struct bcm_device *dev = platform_get_drvdata(pdev);
519
917522aa 520 spin_lock(&bcm_device_lock);
0395ffc1 521 list_del(&dev->list);
917522aa 522 spin_unlock(&bcm_device_lock);
0395ffc1
FD
523
524 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
525
526 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
527
528 return 0;
529}
530
bdd8818e
MH
531static const struct hci_uart_proto bcm_proto = {
532 .id = HCI_UART_BCM,
533 .name = "BCM",
61b2fc2b
FD
534 .init_speed = 115200,
535 .oper_speed = 4000000,
bdd8818e
MH
536 .open = bcm_open,
537 .close = bcm_close,
538 .flush = bcm_flush,
539 .setup = bcm_setup,
61b2fc2b 540 .set_baudrate = bcm_set_baudrate,
bdd8818e
MH
541 .recv = bcm_recv,
542 .enqueue = bcm_enqueue,
543 .dequeue = bcm_dequeue,
544};
545
0395ffc1
FD
546#ifdef CONFIG_ACPI
547static const struct acpi_device_id bcm_acpi_match[] = {
548 { "BCM2E39", 0 },
549 { "BCM2E67", 0 },
550 { },
551};
552MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
553#endif
554
118612fb
FD
555/* Platform suspend and resume callbacks */
556static SIMPLE_DEV_PM_OPS(bcm_pm_ops, bcm_suspend, bcm_resume);
557
0395ffc1
FD
558static struct platform_driver bcm_driver = {
559 .probe = bcm_probe,
560 .remove = bcm_remove,
561 .driver = {
562 .name = "hci_bcm",
563 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
118612fb 564 .pm = &bcm_pm_ops,
0395ffc1
FD
565 },
566};
567
bdd8818e
MH
568int __init bcm_init(void)
569{
0395ffc1
FD
570 platform_driver_register(&bcm_driver);
571
bdd8818e
MH
572 return hci_uart_register_proto(&bcm_proto);
573}
574
575int __exit bcm_deinit(void)
576{
0395ffc1
FD
577 platform_driver_unregister(&bcm_driver);
578
bdd8818e
MH
579 return hci_uart_unregister_proto(&bcm_proto);
580}
This page took 0.14104 seconds and 5 git commands to generate.