]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/bluetooth/hci_bcm.c
Bluetooth: hci_bcm: Retrieve UART speed from ACPI
[mirror_ubuntu-bionic-kernel.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;
0395ffc1
FD
54};
55
bdd8818e 56struct bcm_data {
0395ffc1
FD
57 struct sk_buff *rx_skb;
58 struct sk_buff_head txq;
59
60 struct bcm_device *dev;
bdd8818e
MH
61};
62
0395ffc1
FD
63/* List of BCM BT UART devices */
64static DEFINE_SPINLOCK(bcm_device_list_lock);
65static LIST_HEAD(bcm_device_list);
66
61b2fc2b
FD
67static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
68{
69 struct hci_dev *hdev = hu->hdev;
70 struct sk_buff *skb;
71 struct bcm_update_uart_baud_rate param;
72
73 if (speed > 3000000) {
74 struct bcm_write_uart_clock_setting clock;
75
76 clock.type = BCM_UART_CLOCK_48MHZ;
77
78 BT_DBG("%s: Set Controller clock (%d)", hdev->name, clock.type);
79
80 /* This Broadcom specific command changes the UART's controller
81 * clock for baud rate > 3000000.
82 */
83 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
84 if (IS_ERR(skb)) {
85 int err = PTR_ERR(skb);
86 BT_ERR("%s: BCM: failed to write clock command (%d)",
87 hdev->name, err);
88 return err;
89 }
90
91 kfree_skb(skb);
92 }
93
94 BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev->name, speed);
95
96 param.zero = cpu_to_le16(0);
97 param.baud_rate = cpu_to_le32(speed);
98
99 /* This Broadcom specific command changes the UART's controller baud
100 * rate.
101 */
102 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
103 HCI_INIT_TIMEOUT);
104 if (IS_ERR(skb)) {
105 int err = PTR_ERR(skb);
106 BT_ERR("%s: BCM: failed to write update baudrate command (%d)",
107 hdev->name, err);
108 return err;
109 }
110
111 kfree_skb(skb);
112
113 return 0;
114}
115
0395ffc1
FD
116/* bcm_device_exists should be protected by bcm_device_list_lock */
117static bool bcm_device_exists(struct bcm_device *device)
118{
119 struct list_head *p;
120
121 list_for_each(p, &bcm_device_list) {
122 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
123
124 if (device == dev)
125 return true;
126 }
127
128 return false;
129}
130
131static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
132{
133 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
134 clk_enable(dev->clk);
135
136 gpiod_set_value_cansleep(dev->shutdown, powered);
137 gpiod_set_value_cansleep(dev->device_wakeup, powered);
138
139 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
140 clk_disable(dev->clk);
141
142 dev->clk_enabled = powered;
143
144 return 0;
145}
146
bdd8818e
MH
147static int bcm_open(struct hci_uart *hu)
148{
149 struct bcm_data *bcm;
0395ffc1 150 struct list_head *p;
bdd8818e
MH
151
152 BT_DBG("hu %p", hu);
153
154 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
155 if (!bcm)
156 return -ENOMEM;
157
158 skb_queue_head_init(&bcm->txq);
159
160 hu->priv = bcm;
0395ffc1
FD
161
162 spin_lock(&bcm_device_list_lock);
163 list_for_each(p, &bcm_device_list) {
164 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
165
166 /* Retrieve saved bcm_device based on parent of the
167 * platform device (saved during device probe) and
168 * parent of tty device used by hci_uart
169 */
170 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
171 bcm->dev = dev;
ae056908 172 hu->init_speed = dev->init_speed;
0395ffc1
FD
173 break;
174 }
175 }
176
177 if (bcm->dev)
178 bcm_gpio_set_power(bcm->dev, true);
179
180 spin_unlock(&bcm_device_list_lock);
181
bdd8818e
MH
182 return 0;
183}
184
185static int bcm_close(struct hci_uart *hu)
186{
187 struct bcm_data *bcm = hu->priv;
188
189 BT_DBG("hu %p", hu);
190
0395ffc1
FD
191 /* Protect bcm->dev against removal of the device or driver */
192 spin_lock(&bcm_device_list_lock);
193 if (bcm_device_exists(bcm->dev))
194 bcm_gpio_set_power(bcm->dev, false);
195 spin_unlock(&bcm_device_list_lock);
196
bdd8818e
MH
197 skb_queue_purge(&bcm->txq);
198 kfree_skb(bcm->rx_skb);
199 kfree(bcm);
200
201 hu->priv = NULL;
202 return 0;
203}
204
205static int bcm_flush(struct hci_uart *hu)
206{
207 struct bcm_data *bcm = hu->priv;
208
209 BT_DBG("hu %p", hu);
210
211 skb_queue_purge(&bcm->txq);
212
213 return 0;
214}
215
216static int bcm_setup(struct hci_uart *hu)
217{
6be09b48
FD
218 char fw_name[64];
219 const struct firmware *fw;
960ef1d7 220 unsigned int speed;
6be09b48
FD
221 int err;
222
bdd8818e
MH
223 BT_DBG("hu %p", hu);
224
225 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
226
6be09b48
FD
227 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
228 if (err)
229 return err;
230
231 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
232 if (err < 0) {
233 BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name);
234 return 0;
235 }
236
237 err = btbcm_patchram(hu->hdev, fw);
238 if (err) {
239 BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err);
240 goto finalize;
241 }
242
960ef1d7
FD
243 /* Init speed if any */
244 if (hu->init_speed)
245 speed = hu->init_speed;
246 else if (hu->proto->init_speed)
247 speed = hu->proto->init_speed;
248 else
249 speed = 0;
250
251 if (speed)
252 hci_uart_set_baudrate(hu, speed);
253
254 /* Operational speed if any */
255 if (hu->oper_speed)
256 speed = hu->oper_speed;
257 else if (hu->proto->oper_speed)
258 speed = hu->proto->oper_speed;
259 else
260 speed = 0;
261
262 if (speed) {
263 err = bcm_set_baudrate(hu, speed);
61b2fc2b 264 if (!err)
960ef1d7 265 hci_uart_set_baudrate(hu, speed);
61b2fc2b
FD
266 }
267
6be09b48
FD
268finalize:
269 release_firmware(fw);
270
271 err = btbcm_finalize(hu->hdev);
272
273 return err;
bdd8818e
MH
274}
275
79b8df93
MH
276static const struct h4_recv_pkt bcm_recv_pkts[] = {
277 { H4_RECV_ACL, .recv = hci_recv_frame },
278 { H4_RECV_SCO, .recv = hci_recv_frame },
279 { H4_RECV_EVENT, .recv = hci_recv_frame },
280};
281
bdd8818e
MH
282static int bcm_recv(struct hci_uart *hu, const void *data, int count)
283{
284 struct bcm_data *bcm = hu->priv;
285
286 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
287 return -EUNATCH;
288
79b8df93
MH
289 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
290 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
bdd8818e
MH
291 if (IS_ERR(bcm->rx_skb)) {
292 int err = PTR_ERR(bcm->rx_skb);
293 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
37134167 294 bcm->rx_skb = NULL;
bdd8818e
MH
295 return err;
296 }
297
298 return count;
299}
300
301static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
302{
303 struct bcm_data *bcm = hu->priv;
304
305 BT_DBG("hu %p skb %p", hu, skb);
306
307 /* Prepend skb with frame type */
308 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
309 skb_queue_tail(&bcm->txq, skb);
310
311 return 0;
312}
313
314static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
315{
316 struct bcm_data *bcm = hu->priv;
317
318 return skb_dequeue(&bcm->txq);
319}
320
0395ffc1
FD
321static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
322static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
323
324static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
325 { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
326 { "shutdown-gpios", &shutdown_gpios, 1 },
327 { },
328};
329
ae056908
FD
330static int bcm_resource(struct acpi_resource *ares, void *data)
331{
332 struct bcm_device *dev = data;
333
334 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
335 struct acpi_resource_uart_serialbus *sb;
336
337 sb = &ares->data.uart_serial_bus;
338 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
339 dev->init_speed = sb->default_baud_rate;
340 }
341
342 /* Always tell the ACPI core to skip this resource */
343 return 1;
344}
345
0395ffc1
FD
346static int bcm_acpi_probe(struct bcm_device *dev)
347{
348 struct platform_device *pdev = dev->pdev;
349 const struct acpi_device_id *id;
350 struct gpio_desc *gpio;
ae056908
FD
351 struct acpi_device *adev;
352 LIST_HEAD(resources);
0395ffc1
FD
353 int ret;
354
355 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
356 if (!id)
357 return -ENODEV;
358
359 /* Retrieve GPIO data */
360 dev->name = dev_name(&pdev->dev);
361 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
362 acpi_bcm_default_gpios);
363 if (ret)
364 return ret;
365
366 dev->clk = devm_clk_get(&pdev->dev, NULL);
367
368 gpio = devm_gpiod_get(&pdev->dev, "device-wakeup");
369 if (!IS_ERR(gpio)) {
370 ret = gpiod_direction_output(gpio, 0);
371 if (ret)
372 return ret;
373 dev->device_wakeup = gpio;
374 }
375
376 gpio = devm_gpiod_get(&pdev->dev, "shutdown");
377 if (!IS_ERR(gpio)) {
378 ret = gpiod_direction_output(gpio, 0);
379 if (ret)
380 return ret;
381 dev->shutdown = gpio;
382 }
383
384 /* Make sure at-least one of the GPIO is defined and that
385 * a name is specified for this instance
386 */
387 if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
388 dev_err(&pdev->dev, "invalid platform data\n");
389 return -EINVAL;
390 }
391
ae056908
FD
392 /* Retrieve UART ACPI info */
393 adev = ACPI_COMPANION(&dev->pdev->dev);
394 if (!adev)
395 return 0;
396
397 acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
398
0395ffc1
FD
399 return 0;
400}
401
402static int bcm_probe(struct platform_device *pdev)
403{
404 struct bcm_device *dev;
405 struct acpi_device_id *pdata = pdev->dev.platform_data;
406 int ret;
407
408 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
409 if (!dev)
410 return -ENOMEM;
411
412 dev->pdev = pdev;
413
414 if (ACPI_HANDLE(&pdev->dev)) {
415 ret = bcm_acpi_probe(dev);
416 if (ret)
417 return ret;
418 } else if (pdata) {
419 dev->name = pdata->id;
420 } else {
421 return -ENODEV;
422 }
423
424 platform_set_drvdata(pdev, dev);
425
426 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
427
428 /* Place this instance on the device list */
429 spin_lock(&bcm_device_list_lock);
430 list_add_tail(&dev->list, &bcm_device_list);
431 spin_unlock(&bcm_device_list_lock);
432
433 bcm_gpio_set_power(dev, false);
434
435 return 0;
436}
437
438static int bcm_remove(struct platform_device *pdev)
439{
440 struct bcm_device *dev = platform_get_drvdata(pdev);
441
442 spin_lock(&bcm_device_list_lock);
443 list_del(&dev->list);
444 spin_unlock(&bcm_device_list_lock);
445
446 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
447
448 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
449
450 return 0;
451}
452
bdd8818e
MH
453static const struct hci_uart_proto bcm_proto = {
454 .id = HCI_UART_BCM,
455 .name = "BCM",
61b2fc2b
FD
456 .init_speed = 115200,
457 .oper_speed = 4000000,
bdd8818e
MH
458 .open = bcm_open,
459 .close = bcm_close,
460 .flush = bcm_flush,
461 .setup = bcm_setup,
61b2fc2b 462 .set_baudrate = bcm_set_baudrate,
bdd8818e
MH
463 .recv = bcm_recv,
464 .enqueue = bcm_enqueue,
465 .dequeue = bcm_dequeue,
466};
467
0395ffc1
FD
468#ifdef CONFIG_ACPI
469static const struct acpi_device_id bcm_acpi_match[] = {
470 { "BCM2E39", 0 },
471 { "BCM2E67", 0 },
472 { },
473};
474MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
475#endif
476
477static struct platform_driver bcm_driver = {
478 .probe = bcm_probe,
479 .remove = bcm_remove,
480 .driver = {
481 .name = "hci_bcm",
482 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
483 },
484};
485
bdd8818e
MH
486int __init bcm_init(void)
487{
0395ffc1
FD
488 platform_driver_register(&bcm_driver);
489
bdd8818e
MH
490 return hci_uart_register_proto(&bcm_proto);
491}
492
493int __exit bcm_deinit(void)
494{
0395ffc1
FD
495 platform_driver_unregister(&bcm_driver);
496
bdd8818e
MH
497 return hci_uart_unregister_proto(&bcm_proto);
498}