blob: 6ac66364684d07744bf99d149896b539f9df913f [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;
Marcel Holtmannbdd88182015-04-05 22:52:18 -070052};
53
Frederic Danis0395ffc2015-08-11 16:35:35 +020054struct bcm_data {
55 struct sk_buff *rx_skb;
56 struct sk_buff_head txq;
57
58 struct bcm_device *dev;
59};
60
61/* List of BCM BT UART devices */
62static DEFINE_SPINLOCK(bcm_device_list_lock);
63static LIST_HEAD(bcm_device_list);
64
Frederic Danis61b2fc22015-06-09 16:15:37 +020065static 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), &param,
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
Frederic Danis0395ffc2015-08-11 16:35:35 +0200114/* bcm_device_exists should be protected by bcm_device_list_lock */
115static 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
129static 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
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700145static int bcm_open(struct hci_uart *hu)
146{
147 struct bcm_data *bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200148 struct list_head *p;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700149
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;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200159
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
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700179 return 0;
180}
181
182static int bcm_close(struct hci_uart *hu)
183{
184 struct bcm_data *bcm = hu->priv;
185
186 BT_DBG("hu %p", hu);
187
Frederic Danis0395ffc2015-08-11 16:35:35 +0200188 /* 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
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700194 skb_queue_purge(&bcm->txq);
195 kfree_skb(bcm->rx_skb);
196 kfree(bcm);
197
198 hu->priv = NULL;
199 return 0;
200}
201
202static 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
213static int bcm_setup(struct hci_uart *hu)
214{
Frederic Danis6be09b42015-05-28 11:25:05 +0200215 char fw_name[64];
216 const struct firmware *fw;
Frederic Danis960ef1d2015-06-18 12:43:27 +0200217 unsigned int speed;
Frederic Danis6be09b42015-05-28 11:25:05 +0200218 int err;
219
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700220 BT_DBG("hu %p", hu);
221
222 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
223
Frederic Danis6be09b42015-05-28 11:25:05 +0200224 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
Frederic Danis960ef1d2015-06-18 12:43:27 +0200240 /* 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;
Frederic Danis6be09b42015-05-28 11:25:05 +0200247
Frederic Danis960ef1d2015-06-18 12:43:27 +0200248 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);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200261 if (!err)
Frederic Danis960ef1d2015-06-18 12:43:27 +0200262 hci_uart_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200263 }
264
Frederic Danis6be09b42015-05-28 11:25:05 +0200265finalize:
266 release_firmware(fw);
267
268 err = btbcm_finalize(hu->hdev);
269
270 return err;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700271}
272
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700273static 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
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700279static 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
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700286 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
287 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700288 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);
Chan-yeol Park37134162015-06-17 21:10:39 +0900291 bcm->rx_skb = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700292 return err;
293 }
294
295 return count;
296}
297
298static 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
311static 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
Frederic Danis0395ffc2015-08-11 16:35:35 +0200318static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
319static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
320
321static 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
327static 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
374static 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
410static 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
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700425static const struct hci_uart_proto bcm_proto = {
426 .id = HCI_UART_BCM,
427 .name = "BCM",
Frederic Danis61b2fc22015-06-09 16:15:37 +0200428 .init_speed = 115200,
429 .oper_speed = 4000000,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700430 .open = bcm_open,
431 .close = bcm_close,
432 .flush = bcm_flush,
433 .setup = bcm_setup,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200434 .set_baudrate = bcm_set_baudrate,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700435 .recv = bcm_recv,
436 .enqueue = bcm_enqueue,
437 .dequeue = bcm_dequeue,
438};
439
Frederic Danis0395ffc2015-08-11 16:35:35 +0200440#ifdef CONFIG_ACPI
441static const struct acpi_device_id bcm_acpi_match[] = {
442 { "BCM2E39", 0 },
443 { "BCM2E67", 0 },
444 { },
445};
446MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
447#endif
448
449static 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
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700458int __init bcm_init(void)
459{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200460 platform_driver_register(&bcm_driver);
461
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700462 return hci_uart_register_proto(&bcm_proto);
463}
464
465int __exit bcm_deinit(void)
466{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200467 platform_driver_unregister(&bcm_driver);
468
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700469 return hci_uart_unregister_proto(&bcm_proto);
470}