blob: 34882f18c563201ed77f9f899467f384dfdcb68d [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>
Loic Poulain33cd1492017-08-17 19:59:51 +020030#include <linux/of.h>
31#include <linux/property.h>
Frederic Danis0395ffc2015-08-11 16:35:35 +020032#include <linux/platform_device.h>
33#include <linux/clk.h>
34#include <linux/gpio/consumer.h>
35#include <linux/tty.h>
Frederic Danis6cc43962015-09-04 15:35:44 +020036#include <linux/interrupt.h>
Frederic Danis5cebdfe2015-09-23 18:18:08 +020037#include <linux/dmi.h>
Frederic Danise88ab302015-09-23 18:18:11 +020038#include <linux/pm_runtime.h>
Loic Poulain33cd1492017-08-17 19:59:51 +020039#include <linux/serdev.h>
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -070040
41#include <net/bluetooth/bluetooth.h>
42#include <net/bluetooth/hci_core.h>
43
Marcel Holtmannbdd88182015-04-05 22:52:18 -070044#include "btbcm.h"
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -070045#include "hci_uart.h"
Marcel Holtmannbdd88182015-04-05 22:52:18 -070046
Marcel Holtmann94c58132015-10-07 19:12:54 +020047#define BCM_LM_DIAG_PKT 0x07
48#define BCM_LM_DIAG_SIZE 63
49
Frederic Danise88ab302015-09-23 18:18:11 +020050#define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
51
Loic Poulain33cd1492017-08-17 19:59:51 +020052/* platform device driver resources */
Frederic Danis0395ffc2015-08-11 16:35:35 +020053struct bcm_device {
54 struct list_head list;
55
56 struct platform_device *pdev;
57
58 const char *name;
59 struct gpio_desc *device_wakeup;
60 struct gpio_desc *shutdown;
61
62 struct clk *clk;
63 bool clk_enabled;
Frederic Danisae056902015-08-11 16:35:37 +020064
65 u32 init_speed;
Marcel Holtmann74183a12017-08-16 09:53:30 +020066 u32 oper_speed;
Frederic Danis6cc43962015-09-04 15:35:44 +020067 int irq;
68 u8 irq_polarity;
Frederic Danis118612f2015-08-11 16:35:38 +020069
Frederic Danisb7a622a2015-09-23 18:18:09 +020070#ifdef CONFIG_PM
Frederic Danis118612f2015-08-11 16:35:38 +020071 struct hci_uart *hu;
72 bool is_suspended; /* suspend/resume flag */
73#endif
Marcel Holtmannbdd88182015-04-05 22:52:18 -070074};
75
Loic Poulain33cd1492017-08-17 19:59:51 +020076/* serdev driver resources */
77struct bcm_serdev {
78 struct hci_uart hu;
79};
80
81/* generic bcm uart resources */
Frederic Danis0395ffc2015-08-11 16:35:35 +020082struct bcm_data {
83 struct sk_buff *rx_skb;
84 struct sk_buff_head txq;
85
86 struct bcm_device *dev;
87};
88
89/* List of BCM BT UART devices */
Frederic Danisbb3ea162015-09-01 12:13:35 +020090static DEFINE_MUTEX(bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +020091static LIST_HEAD(bcm_device_list);
92
Loic Poulain33cd1492017-08-17 19:59:51 +020093static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
94{
95 if (hu->serdev)
96 serdev_device_set_baudrate(hu->serdev, speed);
97 else
98 hci_uart_set_baudrate(hu, speed);
99}
100
Frederic Danis61b2fc22015-06-09 16:15:37 +0200101static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
102{
103 struct hci_dev *hdev = hu->hdev;
104 struct sk_buff *skb;
105 struct bcm_update_uart_baud_rate param;
106
107 if (speed > 3000000) {
108 struct bcm_write_uart_clock_setting clock;
109
110 clock.type = BCM_UART_CLOCK_48MHZ;
111
Frederic Danis65ad07c2015-09-01 12:13:36 +0200112 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200113
114 /* This Broadcom specific command changes the UART's controller
115 * clock for baud rate > 3000000.
116 */
117 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
118 if (IS_ERR(skb)) {
119 int err = PTR_ERR(skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200120 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
121 err);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200122 return err;
123 }
124
125 kfree_skb(skb);
126 }
127
Frederic Danis65ad07c2015-09-01 12:13:36 +0200128 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200129
130 param.zero = cpu_to_le16(0);
131 param.baud_rate = cpu_to_le32(speed);
132
133 /* This Broadcom specific command changes the UART's controller baud
134 * rate.
135 */
136 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
137 HCI_INIT_TIMEOUT);
138 if (IS_ERR(skb)) {
139 int err = PTR_ERR(skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200140 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
141 err);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200142 return err;
143 }
144
145 kfree_skb(skb);
146
147 return 0;
148}
149
Frederic Danis917522a2015-08-28 15:44:00 +0200150/* bcm_device_exists should be protected by bcm_device_lock */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200151static bool bcm_device_exists(struct bcm_device *device)
152{
153 struct list_head *p;
154
155 list_for_each(p, &bcm_device_list) {
156 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
157
158 if (device == dev)
159 return true;
160 }
161
162 return false;
163}
164
165static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
166{
167 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
John Keeping730ce392017-03-15 12:20:05 +0000168 clk_prepare_enable(dev->clk);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200169
Loic Poulain2af0a702015-08-17 16:00:21 +0200170 gpiod_set_value(dev->shutdown, powered);
171 gpiod_set_value(dev->device_wakeup, powered);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200172
173 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
John Keeping730ce392017-03-15 12:20:05 +0000174 clk_disable_unprepare(dev->clk);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200175
176 dev->clk_enabled = powered;
177
178 return 0;
179}
180
Frederic Danisb7a622a2015-09-23 18:18:09 +0200181#ifdef CONFIG_PM
Frederic Danis6cc43962015-09-04 15:35:44 +0200182static irqreturn_t bcm_host_wake(int irq, void *data)
183{
184 struct bcm_device *bdev = data;
185
186 bt_dev_dbg(bdev, "Host wake IRQ");
187
Frederic Danise88ab302015-09-23 18:18:11 +0200188 pm_runtime_get(&bdev->pdev->dev);
189 pm_runtime_mark_last_busy(&bdev->pdev->dev);
190 pm_runtime_put_autosuspend(&bdev->pdev->dev);
191
Frederic Danis6cc43962015-09-04 15:35:44 +0200192 return IRQ_HANDLED;
193}
194
195static int bcm_request_irq(struct bcm_data *bcm)
196{
197 struct bcm_device *bdev = bcm->dev;
Loic Poulain98dc77d2017-07-04 12:57:56 +0200198 int err;
Frederic Danis6cc43962015-09-04 15:35:44 +0200199
200 /* If this is not a platform device, do not enable PM functionalities */
201 mutex_lock(&bcm_device_lock);
202 if (!bcm_device_exists(bdev)) {
203 err = -ENODEV;
204 goto unlock;
205 }
206
Loic Poulain98dc77d2017-07-04 12:57:56 +0200207 if (bdev->irq <= 0) {
208 err = -EOPNOTSUPP;
209 goto unlock;
Frederic Danis6cc43962015-09-04 15:35:44 +0200210 }
211
Loic Poulain98dc77d2017-07-04 12:57:56 +0200212 err = devm_request_irq(&bdev->pdev->dev, bdev->irq, bcm_host_wake,
213 IRQF_TRIGGER_RISING, "host_wake", bdev);
214 if (err)
215 goto unlock;
216
217 device_init_wakeup(&bdev->pdev->dev, true);
218
219 pm_runtime_set_autosuspend_delay(&bdev->pdev->dev,
220 BCM_AUTOSUSPEND_DELAY);
221 pm_runtime_use_autosuspend(&bdev->pdev->dev);
222 pm_runtime_set_active(&bdev->pdev->dev);
223 pm_runtime_enable(&bdev->pdev->dev);
224
Frederic Danis6cc43962015-09-04 15:35:44 +0200225unlock:
226 mutex_unlock(&bcm_device_lock);
227
228 return err;
229}
230
231static const struct bcm_set_sleep_mode default_sleep_params = {
232 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
233 .idle_host = 2, /* idle threshold HOST, in 300ms */
234 .idle_dev = 2, /* idle threshold device, in 300ms */
235 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
236 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
237 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
Frederic Danise88ab302015-09-23 18:18:11 +0200238 .combine_modes = 1, /* Combine sleep and LPM flag */
Frederic Danis6cc43962015-09-04 15:35:44 +0200239 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
240 /* Irrelevant USB flags */
241 .usb_auto_sleep = 0,
242 .usb_resume_timeout = 0,
243 .pulsed_host_wake = 0,
244 .break_to_host = 0
245};
246
247static int bcm_setup_sleep(struct hci_uart *hu)
248{
249 struct bcm_data *bcm = hu->priv;
250 struct sk_buff *skb;
251 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
252
253 sleep_params.host_wake_active = !bcm->dev->irq_polarity;
254
255 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
256 &sleep_params, HCI_INIT_TIMEOUT);
257 if (IS_ERR(skb)) {
258 int err = PTR_ERR(skb);
259 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
260 return err;
261 }
262 kfree_skb(skb);
263
264 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
265
266 return 0;
267}
268#else
269static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
270static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
271#endif
272
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200273static int bcm_set_diag(struct hci_dev *hdev, bool enable)
274{
275 struct hci_uart *hu = hci_get_drvdata(hdev);
276 struct bcm_data *bcm = hu->priv;
277 struct sk_buff *skb;
278
279 if (!test_bit(HCI_RUNNING, &hdev->flags))
280 return -ENETDOWN;
281
282 skb = bt_skb_alloc(3, GFP_KERNEL);
Dan Carpentera1857392015-10-22 12:06:09 +0300283 if (!skb)
284 return -ENOMEM;
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200285
Johannes Berg634fef62017-06-16 14:29:24 +0200286 skb_put_u8(skb, BCM_LM_DIAG_PKT);
287 skb_put_u8(skb, 0xf0);
288 skb_put_u8(skb, enable);
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200289
290 skb_queue_tail(&bcm->txq, skb);
291 hci_uart_tx_wakeup(hu);
292
293 return 0;
294}
295
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700296static int bcm_open(struct hci_uart *hu)
297{
298 struct bcm_data *bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200299 struct list_head *p;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700300
Frederic Danis65ad07c2015-09-01 12:13:36 +0200301 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700302
303 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
304 if (!bcm)
305 return -ENOMEM;
306
307 skb_queue_head_init(&bcm->txq);
308
309 hu->priv = bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200310
Loic Poulain33cd1492017-08-17 19:59:51 +0200311 /* If this is a serdev defined device, then only use
312 * serdev open primitive and skip the rest.
313 */
314 if (hu->serdev) {
315 serdev_device_open(hu->serdev);
316 goto out;
317 }
318
Johan Hovold95065a62017-03-29 18:15:27 +0200319 if (!hu->tty->dev)
320 goto out;
321
Frederic Danisbb3ea162015-09-01 12:13:35 +0200322 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200323 list_for_each(p, &bcm_device_list) {
324 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
325
326 /* Retrieve saved bcm_device based on parent of the
327 * platform device (saved during device probe) and
328 * parent of tty device used by hci_uart
329 */
330 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
331 bcm->dev = dev;
Frederic Danisae056902015-08-11 16:35:37 +0200332 hu->init_speed = dev->init_speed;
Marcel Holtmann74183a12017-08-16 09:53:30 +0200333 hu->oper_speed = dev->oper_speed;
Frederic Danisb7a622a2015-09-23 18:18:09 +0200334#ifdef CONFIG_PM
Frederic Danis118612f2015-08-11 16:35:38 +0200335 dev->hu = hu;
336#endif
Frederic Danis6cc43962015-09-04 15:35:44 +0200337 bcm_gpio_set_power(bcm->dev, true);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200338 break;
339 }
340 }
341
Frederic Danisbb3ea162015-09-01 12:13:35 +0200342 mutex_unlock(&bcm_device_lock);
Johan Hovold95065a62017-03-29 18:15:27 +0200343out:
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700344 return 0;
345}
346
347static int bcm_close(struct hci_uart *hu)
348{
349 struct bcm_data *bcm = hu->priv;
Frederic Danis6cc43962015-09-04 15:35:44 +0200350 struct bcm_device *bdev = bcm->dev;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700351
Frederic Danis65ad07c2015-09-01 12:13:36 +0200352 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700353
Loic Poulain33cd1492017-08-17 19:59:51 +0200354 /* If this is a serdev defined device, only use serdev
355 * close primitive and then continue as usual.
356 */
357 if (hu->serdev)
358 serdev_device_close(hu->serdev);
359
Frederic Danis0395ffc2015-08-11 16:35:35 +0200360 /* Protect bcm->dev against removal of the device or driver */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200361 mutex_lock(&bcm_device_lock);
Frederic Danis6cc43962015-09-04 15:35:44 +0200362 if (bcm_device_exists(bdev)) {
363 bcm_gpio_set_power(bdev, false);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200364#ifdef CONFIG_PM
Frederic Danise88ab302015-09-23 18:18:11 +0200365 pm_runtime_disable(&bdev->pdev->dev);
366 pm_runtime_set_suspended(&bdev->pdev->dev);
367
Frederic Danis6cc43962015-09-04 15:35:44 +0200368 if (device_can_wakeup(&bdev->pdev->dev)) {
369 devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev);
370 device_init_wakeup(&bdev->pdev->dev, false);
371 }
372
373 bdev->hu = NULL;
Frederic Danis118612f2015-08-11 16:35:38 +0200374#endif
375 }
Frederic Danisbb3ea162015-09-01 12:13:35 +0200376 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200377
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700378 skb_queue_purge(&bcm->txq);
379 kfree_skb(bcm->rx_skb);
380 kfree(bcm);
381
382 hu->priv = NULL;
383 return 0;
384}
385
386static int bcm_flush(struct hci_uart *hu)
387{
388 struct bcm_data *bcm = hu->priv;
389
Frederic Danis65ad07c2015-09-01 12:13:36 +0200390 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700391
392 skb_queue_purge(&bcm->txq);
393
394 return 0;
395}
396
397static int bcm_setup(struct hci_uart *hu)
398{
Frederic Danis6cc43962015-09-04 15:35:44 +0200399 struct bcm_data *bcm = hu->priv;
Frederic Danis6be09b42015-05-28 11:25:05 +0200400 char fw_name[64];
401 const struct firmware *fw;
Frederic Danis960ef1d2015-06-18 12:43:27 +0200402 unsigned int speed;
Frederic Danis6be09b42015-05-28 11:25:05 +0200403 int err;
404
Frederic Danis65ad07c2015-09-01 12:13:36 +0200405 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700406
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200407 hu->hdev->set_diag = bcm_set_diag;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700408 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
409
Frederic Danis6be09b42015-05-28 11:25:05 +0200410 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
411 if (err)
412 return err;
413
414 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
415 if (err < 0) {
Frederic Danis65ad07c2015-09-01 12:13:36 +0200416 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
Frederic Danis6be09b42015-05-28 11:25:05 +0200417 return 0;
418 }
419
420 err = btbcm_patchram(hu->hdev, fw);
421 if (err) {
Frederic Danis65ad07c2015-09-01 12:13:36 +0200422 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
Frederic Danis6be09b42015-05-28 11:25:05 +0200423 goto finalize;
424 }
425
Frederic Danis960ef1d2015-06-18 12:43:27 +0200426 /* Init speed if any */
427 if (hu->init_speed)
428 speed = hu->init_speed;
429 else if (hu->proto->init_speed)
430 speed = hu->proto->init_speed;
431 else
432 speed = 0;
Frederic Danis6be09b42015-05-28 11:25:05 +0200433
Frederic Danis960ef1d2015-06-18 12:43:27 +0200434 if (speed)
Loic Poulain33cd1492017-08-17 19:59:51 +0200435 host_set_baudrate(hu, speed);
Frederic Danis960ef1d2015-06-18 12:43:27 +0200436
437 /* Operational speed if any */
438 if (hu->oper_speed)
439 speed = hu->oper_speed;
440 else if (hu->proto->oper_speed)
441 speed = hu->proto->oper_speed;
442 else
443 speed = 0;
444
445 if (speed) {
446 err = bcm_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200447 if (!err)
Loic Poulain33cd1492017-08-17 19:59:51 +0200448 host_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200449 }
450
Frederic Danis6be09b42015-05-28 11:25:05 +0200451finalize:
452 release_firmware(fw);
453
454 err = btbcm_finalize(hu->hdev);
Frederic Danis6cc43962015-09-04 15:35:44 +0200455 if (err)
456 return err;
457
Loic Poulaincdd24a22017-06-27 19:15:07 +0200458 if (!bcm_request_irq(bcm))
Frederic Danis6cc43962015-09-04 15:35:44 +0200459 err = bcm_setup_sleep(hu);
Frederic Danis6be09b42015-05-28 11:25:05 +0200460
461 return err;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700462}
463
Marcel Holtmann94c58132015-10-07 19:12:54 +0200464#define BCM_RECV_LM_DIAG \
465 .type = BCM_LM_DIAG_PKT, \
466 .hlen = BCM_LM_DIAG_SIZE, \
467 .loff = 0, \
468 .lsize = 0, \
469 .maxlen = BCM_LM_DIAG_SIZE
470
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700471static const struct h4_recv_pkt bcm_recv_pkts[] = {
Marcel Holtmann94c58132015-10-07 19:12:54 +0200472 { H4_RECV_ACL, .recv = hci_recv_frame },
473 { H4_RECV_SCO, .recv = hci_recv_frame },
474 { H4_RECV_EVENT, .recv = hci_recv_frame },
475 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700476};
477
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700478static int bcm_recv(struct hci_uart *hu, const void *data, int count)
479{
480 struct bcm_data *bcm = hu->priv;
481
482 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
483 return -EUNATCH;
484
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700485 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
486 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700487 if (IS_ERR(bcm->rx_skb)) {
488 int err = PTR_ERR(bcm->rx_skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200489 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Chan-yeol Park37134162015-06-17 21:10:39 +0900490 bcm->rx_skb = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700491 return err;
Frederic Danise88ab302015-09-23 18:18:11 +0200492 } else if (!bcm->rx_skb) {
493 /* Delay auto-suspend when receiving completed packet */
494 mutex_lock(&bcm_device_lock);
495 if (bcm->dev && bcm_device_exists(bcm->dev)) {
496 pm_runtime_get(&bcm->dev->pdev->dev);
497 pm_runtime_mark_last_busy(&bcm->dev->pdev->dev);
498 pm_runtime_put_autosuspend(&bcm->dev->pdev->dev);
499 }
500 mutex_unlock(&bcm_device_lock);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700501 }
502
503 return count;
504}
505
506static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
507{
508 struct bcm_data *bcm = hu->priv;
509
Frederic Danis65ad07c2015-09-01 12:13:36 +0200510 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700511
512 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100513 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700514 skb_queue_tail(&bcm->txq, skb);
515
516 return 0;
517}
518
519static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
520{
521 struct bcm_data *bcm = hu->priv;
Frederic Danise88ab302015-09-23 18:18:11 +0200522 struct sk_buff *skb = NULL;
523 struct bcm_device *bdev = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700524
Frederic Danise88ab302015-09-23 18:18:11 +0200525 mutex_lock(&bcm_device_lock);
526
527 if (bcm_device_exists(bcm->dev)) {
528 bdev = bcm->dev;
529 pm_runtime_get_sync(&bdev->pdev->dev);
530 /* Shall be resumed here */
531 }
532
533 skb = skb_dequeue(&bcm->txq);
534
535 if (bdev) {
536 pm_runtime_mark_last_busy(&bdev->pdev->dev);
537 pm_runtime_put_autosuspend(&bdev->pdev->dev);
538 }
539
540 mutex_unlock(&bcm_device_lock);
541
542 return skb;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700543}
544
Frederic Danisb7a622a2015-09-23 18:18:09 +0200545#ifdef CONFIG_PM
546static int bcm_suspend_device(struct device *dev)
Frederic Danis118612f2015-08-11 16:35:38 +0200547{
548 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
549
Frederic Danisb7a622a2015-09-23 18:18:09 +0200550 bt_dev_dbg(bdev, "");
Frederic Danis118612f2015-08-11 16:35:38 +0200551
Frederic Danisb7a622a2015-09-23 18:18:09 +0200552 if (!bdev->is_suspended && bdev->hu) {
Frederic Danis118612f2015-08-11 16:35:38 +0200553 hci_uart_set_flow_control(bdev->hu, true);
554
Frederic Danisb7a622a2015-09-23 18:18:09 +0200555 /* Once this returns, driver suspends BT via GPIO */
Frederic Danis118612f2015-08-11 16:35:38 +0200556 bdev->is_suspended = true;
557 }
558
559 /* Suspend the device */
560 if (bdev->device_wakeup) {
561 gpiod_set_value(bdev->device_wakeup, false);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200562 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
Frederic Danis118612f2015-08-11 16:35:38 +0200563 mdelay(15);
564 }
565
Frederic Danisb7a622a2015-09-23 18:18:09 +0200566 return 0;
567}
568
569static int bcm_resume_device(struct device *dev)
570{
571 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
572
573 bt_dev_dbg(bdev, "");
574
575 if (bdev->device_wakeup) {
576 gpiod_set_value(bdev->device_wakeup, true);
577 bt_dev_dbg(bdev, "resume, delaying 15 ms");
578 mdelay(15);
579 }
580
581 /* When this executes, the device has woken up already */
582 if (bdev->is_suspended && bdev->hu) {
583 bdev->is_suspended = false;
584
585 hci_uart_set_flow_control(bdev->hu, false);
586 }
587
588 return 0;
589}
590#endif
591
592#ifdef CONFIG_PM_SLEEP
593/* Platform suspend callback */
594static int bcm_suspend(struct device *dev)
595{
596 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
597 int error;
598
599 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
600
601 /* bcm_suspend can be called at any time as long as platform device is
602 * bound, so it should use bcm_device_lock to protect access to hci_uart
603 * and device_wake-up GPIO.
604 */
605 mutex_lock(&bcm_device_lock);
606
607 if (!bdev->hu)
608 goto unlock;
609
Frederic Danise88ab302015-09-23 18:18:11 +0200610 if (pm_runtime_active(dev))
611 bcm_suspend_device(dev);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200612
Frederic Danis6cc43962015-09-04 15:35:44 +0200613 if (device_may_wakeup(&bdev->pdev->dev)) {
614 error = enable_irq_wake(bdev->irq);
615 if (!error)
616 bt_dev_dbg(bdev, "BCM irq: enabled");
617 }
618
Frederic Danis917522a2015-08-28 15:44:00 +0200619unlock:
Frederic Danisbb3ea162015-09-01 12:13:35 +0200620 mutex_unlock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200621
Frederic Danis118612f2015-08-11 16:35:38 +0200622 return 0;
623}
624
625/* Platform resume callback */
626static int bcm_resume(struct device *dev)
627{
628 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
629
Frederic Danis65ad07c2015-09-01 12:13:36 +0200630 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
Frederic Danis118612f2015-08-11 16:35:38 +0200631
Frederic Danisb7a622a2015-09-23 18:18:09 +0200632 /* bcm_resume can be called at any time as long as platform device is
633 * bound, so it should use bcm_device_lock to protect access to hci_uart
634 * and device_wake-up GPIO.
635 */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200636 mutex_lock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200637
638 if (!bdev->hu)
639 goto unlock;
640
Frederic Danis6cc43962015-09-04 15:35:44 +0200641 if (device_may_wakeup(&bdev->pdev->dev)) {
642 disable_irq_wake(bdev->irq);
643 bt_dev_dbg(bdev, "BCM irq: disabled");
644 }
645
Frederic Danisb7a622a2015-09-23 18:18:09 +0200646 bcm_resume_device(dev);
Frederic Danis118612f2015-08-11 16:35:38 +0200647
Frederic Danis917522a2015-08-28 15:44:00 +0200648unlock:
Frederic Danisbb3ea162015-09-01 12:13:35 +0200649 mutex_unlock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200650
Frederic Danise88ab302015-09-23 18:18:11 +0200651 pm_runtime_disable(dev);
652 pm_runtime_set_active(dev);
653 pm_runtime_enable(dev);
654
Frederic Danis118612f2015-08-11 16:35:38 +0200655 return 0;
656}
657#endif
658
Daniel Drake89ab37b2017-01-05 11:10:54 -0600659static const struct acpi_gpio_params int_last_device_wakeup_gpios = { 0, 0, false };
660static const struct acpi_gpio_params int_last_shutdown_gpios = { 1, 0, false };
661static const struct acpi_gpio_params int_last_host_wakeup_gpios = { 2, 0, false };
Frederic Danis0395ffc2015-08-11 16:35:35 +0200662
Daniel Drake89ab37b2017-01-05 11:10:54 -0600663static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
664 { "device-wakeup-gpios", &int_last_device_wakeup_gpios, 1 },
665 { "shutdown-gpios", &int_last_shutdown_gpios, 1 },
666 { "host-wakeup-gpios", &int_last_host_wakeup_gpios, 1 },
667 { },
668};
669
670static const struct acpi_gpio_params int_first_host_wakeup_gpios = { 0, 0, false };
671static const struct acpi_gpio_params int_first_device_wakeup_gpios = { 1, 0, false };
672static const struct acpi_gpio_params int_first_shutdown_gpios = { 2, 0, false };
673
674static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
675 { "device-wakeup-gpios", &int_first_device_wakeup_gpios, 1 },
676 { "shutdown-gpios", &int_first_shutdown_gpios, 1 },
677 { "host-wakeup-gpios", &int_first_host_wakeup_gpios, 1 },
Frederic Danis0395ffc2015-08-11 16:35:35 +0200678 { },
679};
680
Frederic Danis50d78bc2015-08-12 12:46:01 +0200681#ifdef CONFIG_ACPI
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200682static u8 acpi_active_low = ACPI_ACTIVE_LOW;
683
684/* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
685static const struct dmi_system_id bcm_wrong_irq_dmi_table[] = {
686 {
687 .ident = "Asus T100TA",
688 .matches = {
689 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
690 "ASUSTeK COMPUTER INC."),
691 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
692 },
693 .driver_data = &acpi_active_low,
694 },
Hans de Goedec4c285d2017-06-29 14:21:32 +0200695 {
696 .ident = "Asus T100CHI",
697 .matches = {
698 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
699 "ASUSTeK COMPUTER INC."),
700 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100CHI"),
701 },
702 .driver_data = &acpi_active_low,
703 },
Jérôme de Bretagne5e2bd932016-10-09 15:51:05 +0200704 { /* Handle ThinkPad 8 tablets with BCM2E55 chipset ACPI ID */
705 .ident = "Lenovo ThinkPad 8",
706 .matches = {
707 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
708 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "ThinkPad 8"),
709 },
710 .driver_data = &acpi_active_low,
711 },
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200712 { }
713};
714
Frederic Danisae056902015-08-11 16:35:37 +0200715static int bcm_resource(struct acpi_resource *ares, void *data)
716{
717 struct bcm_device *dev = data;
Frederic Danis6cc43962015-09-04 15:35:44 +0200718 struct acpi_resource_extended_irq *irq;
719 struct acpi_resource_gpio *gpio;
720 struct acpi_resource_uart_serialbus *sb;
Frederic Danisae056902015-08-11 16:35:37 +0200721
Frederic Danis6cc43962015-09-04 15:35:44 +0200722 switch (ares->type) {
723 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
724 irq = &ares->data.extended_irq;
725 dev->irq_polarity = irq->polarity;
726 break;
Frederic Danisae056902015-08-11 16:35:37 +0200727
Frederic Danis6cc43962015-09-04 15:35:44 +0200728 case ACPI_RESOURCE_TYPE_GPIO:
729 gpio = &ares->data.gpio;
730 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
731 dev->irq_polarity = gpio->polarity;
732 break;
733
734 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
Frederic Danisae056902015-08-11 16:35:37 +0200735 sb = &ares->data.uart_serial_bus;
Marcel Holtmann74183a12017-08-16 09:53:30 +0200736 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
Frederic Danisae056902015-08-11 16:35:37 +0200737 dev->init_speed = sb->default_baud_rate;
Marcel Holtmann74183a12017-08-16 09:53:30 +0200738 dev->oper_speed = 4000000;
739 }
Frederic Danis6cc43962015-09-04 15:35:44 +0200740 break;
741
742 default:
743 break;
Frederic Danisae056902015-08-11 16:35:37 +0200744 }
745
746 /* Always tell the ACPI core to skip this resource */
747 return 1;
748}
Andy Shevchenko212d7182017-03-10 14:28:20 +0200749#endif /* CONFIG_ACPI */
Frederic Danisae056902015-08-11 16:35:37 +0200750
Andy Shevchenko212d7182017-03-10 14:28:20 +0200751static int bcm_platform_probe(struct bcm_device *dev)
Frederic Danis0395ffc2015-08-11 16:35:35 +0200752{
753 struct platform_device *pdev = dev->pdev;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200754
Frederic Danis0395ffc2015-08-11 16:35:35 +0200755 dev->name = dev_name(&pdev->dev);
Daniel Drake89ab37b2017-01-05 11:10:54 -0600756
Frederic Danis0395ffc2015-08-11 16:35:35 +0200757 dev->clk = devm_clk_get(&pdev->dev, NULL);
758
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200759 dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
760 "device-wakeup",
761 GPIOD_OUT_LOW);
762 if (IS_ERR(dev->device_wakeup))
763 return PTR_ERR(dev->device_wakeup);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200764
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200765 dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
766 GPIOD_OUT_LOW);
767 if (IS_ERR(dev->shutdown))
768 return PTR_ERR(dev->shutdown);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200769
Frederic Danis6cc43962015-09-04 15:35:44 +0200770 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
771 dev->irq = platform_get_irq(pdev, 0);
772 if (dev->irq <= 0) {
773 struct gpio_desc *gpio;
774
775 gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup",
776 GPIOD_IN);
777 if (IS_ERR(gpio))
778 return PTR_ERR(gpio);
779
780 dev->irq = gpiod_to_irq(gpio);
781 }
782
783 dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq);
784
Frederic Danis0395ffc2015-08-11 16:35:35 +0200785 /* Make sure at-least one of the GPIO is defined and that
786 * a name is specified for this instance
787 */
788 if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
789 dev_err(&pdev->dev, "invalid platform data\n");
790 return -EINVAL;
791 }
792
Andy Shevchenko212d7182017-03-10 14:28:20 +0200793 return 0;
794}
795
796#ifdef CONFIG_ACPI
797static int bcm_acpi_probe(struct bcm_device *dev)
798{
799 struct platform_device *pdev = dev->pdev;
800 LIST_HEAD(resources);
801 const struct dmi_system_id *dmi_id;
802 const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
803 const struct acpi_device_id *id;
804 int ret;
805
806 /* Retrieve GPIO data */
807 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
808 if (id)
809 gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data;
810
Andy Shevchenkofda70572017-06-06 17:47:17 +0300811 ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, gpio_mapping);
Andy Shevchenko212d7182017-03-10 14:28:20 +0200812 if (ret)
813 return ret;
814
815 ret = bcm_platform_probe(dev);
816 if (ret)
817 return ret;
818
Frederic Danisae056902015-08-11 16:35:37 +0200819 /* Retrieve UART ACPI info */
Jarkko Nikulae98d6d62015-09-30 16:26:45 +0300820 ret = acpi_dev_get_resources(ACPI_COMPANION(&dev->pdev->dev),
821 &resources, bcm_resource, dev);
Jarkko Nikula5be00282015-09-30 16:26:42 +0300822 if (ret < 0)
823 return ret;
Jarkko Nikula09dbf1b2015-09-30 16:26:41 +0300824 acpi_dev_free_resource_list(&resources);
Frederic Danisae056902015-08-11 16:35:37 +0200825
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200826 dmi_id = dmi_first_match(bcm_wrong_irq_dmi_table);
827 if (dmi_id) {
828 bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low",
829 dmi_id->ident);
830 dev->irq_polarity = *(u8 *)dmi_id->driver_data;
831 }
832
Frederic Danis0395ffc2015-08-11 16:35:35 +0200833 return 0;
834}
Frederic Danis50d78bc2015-08-12 12:46:01 +0200835#else
836static int bcm_acpi_probe(struct bcm_device *dev)
837{
838 return -EINVAL;
839}
840#endif /* CONFIG_ACPI */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200841
842static int bcm_probe(struct platform_device *pdev)
843{
844 struct bcm_device *dev;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200845 int ret;
846
847 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
848 if (!dev)
849 return -ENOMEM;
850
851 dev->pdev = pdev;
852
Andy Shevchenko212d7182017-03-10 14:28:20 +0200853 if (has_acpi_companion(&pdev->dev))
854 ret = bcm_acpi_probe(dev);
855 else
856 ret = bcm_platform_probe(dev);
Jarkko Nikula4d1c4552015-09-30 16:26:44 +0300857 if (ret)
858 return ret;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200859
860 platform_set_drvdata(pdev, dev);
861
862 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
863
864 /* Place this instance on the device list */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200865 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200866 list_add_tail(&dev->list, &bcm_device_list);
Frederic Danisbb3ea162015-09-01 12:13:35 +0200867 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200868
869 bcm_gpio_set_power(dev, false);
870
871 return 0;
872}
873
874static int bcm_remove(struct platform_device *pdev)
875{
876 struct bcm_device *dev = platform_get_drvdata(pdev);
877
Frederic Danisbb3ea162015-09-01 12:13:35 +0200878 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200879 list_del(&dev->list);
Frederic Danisbb3ea162015-09-01 12:13:35 +0200880 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200881
Frederic Danis0395ffc2015-08-11 16:35:35 +0200882 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
883
884 return 0;
885}
886
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700887static const struct hci_uart_proto bcm_proto = {
888 .id = HCI_UART_BCM,
Loic Poulain143f0a22016-09-19 12:05:12 +0200889 .name = "Broadcom",
Marcel Holtmannaee61f72015-10-20 21:30:45 +0200890 .manufacturer = 15,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200891 .init_speed = 115200,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700892 .open = bcm_open,
893 .close = bcm_close,
894 .flush = bcm_flush,
895 .setup = bcm_setup,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200896 .set_baudrate = bcm_set_baudrate,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700897 .recv = bcm_recv,
898 .enqueue = bcm_enqueue,
899 .dequeue = bcm_dequeue,
900};
901
Frederic Danis0395ffc2015-08-11 16:35:35 +0200902#ifdef CONFIG_ACPI
903static const struct acpi_device_id bcm_acpi_match[] = {
Daniel Drake89ab37b2017-01-05 11:10:54 -0600904 { "BCM2E1A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
905 { "BCM2E39", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
906 { "BCM2E3A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
907 { "BCM2E3D", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
908 { "BCM2E3F", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
909 { "BCM2E40", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
910 { "BCM2E54", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
911 { "BCM2E55", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
912 { "BCM2E64", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
913 { "BCM2E65", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
914 { "BCM2E67", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
915 { "BCM2E71", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
916 { "BCM2E7B", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
917 { "BCM2E7C", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
918 { "BCM2E95", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
919 { "BCM2E96", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
Frederic Danis0395ffc2015-08-11 16:35:35 +0200920 { },
921};
922MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
923#endif
924
Frederic Danis118612f2015-08-11 16:35:38 +0200925/* Platform suspend and resume callbacks */
Frederic Danise88ab302015-09-23 18:18:11 +0200926static const struct dev_pm_ops bcm_pm_ops = {
927 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
928 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
929};
Frederic Danis118612f2015-08-11 16:35:38 +0200930
Frederic Danis0395ffc2015-08-11 16:35:35 +0200931static struct platform_driver bcm_driver = {
932 .probe = bcm_probe,
933 .remove = bcm_remove,
934 .driver = {
935 .name = "hci_bcm",
936 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
Frederic Danis118612f2015-08-11 16:35:38 +0200937 .pm = &bcm_pm_ops,
Frederic Danis0395ffc2015-08-11 16:35:35 +0200938 },
939};
940
Loic Poulain33cd1492017-08-17 19:59:51 +0200941static int bcm_serdev_probe(struct serdev_device *serdev)
942{
943 struct bcm_serdev *bcmdev;
944 u32 speed;
945 int err;
946
947 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
948 if (!bcmdev)
949 return -ENOMEM;
950
951 bcmdev->hu.serdev = serdev;
952 serdev_device_set_drvdata(serdev, bcmdev);
953
954 err = device_property_read_u32(&serdev->dev, "max-speed", &speed);
955 if (!err)
956 bcmdev->hu.oper_speed = speed;
957
958 return hci_uart_register_device(&bcmdev->hu, &bcm_proto);
959}
960
961static void bcm_serdev_remove(struct serdev_device *serdev)
962{
963 struct bcm_serdev *bcmdev = serdev_device_get_drvdata(serdev);
964
965 hci_uart_unregister_device(&bcmdev->hu);
966}
967
968#ifdef CONFIG_OF
969static const struct of_device_id bcm_bluetooth_of_match[] = {
970 { .compatible = "brcm,bcm43438-bt" },
971 { },
972};
973MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
974#endif
975
976static struct serdev_device_driver bcm_serdev_driver = {
977 .probe = bcm_serdev_probe,
978 .remove = bcm_serdev_remove,
979 .driver = {
980 .name = "hci_uart_bcm",
981 .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
982 },
983};
984
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700985int __init bcm_init(void)
986{
Loic Poulain33cd1492017-08-17 19:59:51 +0200987 /* For now, we need to keep both platform device
988 * driver (ACPI generated) and serdev driver (DT).
989 */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200990 platform_driver_register(&bcm_driver);
Loic Poulain33cd1492017-08-17 19:59:51 +0200991 serdev_device_driver_register(&bcm_serdev_driver);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200992
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700993 return hci_uart_register_proto(&bcm_proto);
994}
995
996int __exit bcm_deinit(void)
997{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200998 platform_driver_unregister(&bcm_driver);
Loic Poulain33cd1492017-08-17 19:59:51 +0200999 serdev_device_driver_unregister(&bcm_serdev_driver);
Frederic Danis0395ffc2015-08-11 16:35:35 +02001000
Marcel Holtmannbdd88182015-04-05 22:52:18 -07001001 return hci_uart_unregister_proto(&bcm_proto);
1002}