blob: 3c1282a9fcd48faa511b415fe23e661972de50ae [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 Holtmann01d5e442017-08-17 21:41:09 +020047#define BCM_NULL_PKT 0x00
48#define BCM_NULL_SIZE 0
49
Marcel Holtmann94c58132015-10-07 19:12:54 +020050#define BCM_LM_DIAG_PKT 0x07
51#define BCM_LM_DIAG_SIZE 63
52
Frederic Danise88ab302015-09-23 18:18:11 +020053#define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
54
Lukas Wunnerb7c2aba2018-01-10 16:32:10 +010055/**
56 * struct bcm_device - device driver resources
57 * @serdev_hu: HCI UART controller struct
58 * @list: bcm_device_list node
59 * @dev: physical UART slave
60 * @name: device name logged by bt_dev_*() functions
61 * @device_wakeup: BT_WAKE pin,
62 * assert = Bluetooth device must wake up or remain awake,
63 * deassert = Bluetooth device may sleep when sleep criteria are met
64 * @shutdown: BT_REG_ON pin,
65 * power up or power down Bluetooth device internal regulators
66 * @clk: clock used by Bluetooth device
67 * @clk_enabled: whether @clk is prepared and enabled
68 * @init_speed: default baudrate of Bluetooth device;
69 * the host UART is initially set to this baudrate so that
70 * it can configure the Bluetooth device for @oper_speed
71 * @oper_speed: preferred baudrate of Bluetooth device;
72 * set to 0 if @init_speed is already the preferred baudrate
73 * @irq: interrupt triggered by HOST_WAKE_BT pin
74 * @irq_active_low: whether @irq is active low
75 * @hu: pointer to HCI UART controller struct,
76 * used to disable flow control during runtime suspend and system sleep
77 * @is_suspended: whether flow control is currently disabled
78 */
Frederic Danis0395ffc2015-08-11 16:35:35 +020079struct bcm_device {
Hans de Goede8a920562017-10-04 20:43:43 +020080 /* Must be the first member, hci_serdev.c expects this. */
81 struct hci_uart serdev_hu;
Frederic Danis0395ffc2015-08-11 16:35:35 +020082 struct list_head list;
83
Hans de Goedec0d3ce52017-10-04 20:43:39 +020084 struct device *dev;
Frederic Danis0395ffc2015-08-11 16:35:35 +020085
86 const char *name;
87 struct gpio_desc *device_wakeup;
88 struct gpio_desc *shutdown;
89
90 struct clk *clk;
91 bool clk_enabled;
Frederic Danisae056902015-08-11 16:35:37 +020092
93 u32 init_speed;
Marcel Holtmann74183a12017-08-16 09:53:30 +020094 u32 oper_speed;
Frederic Danis6cc43962015-09-04 15:35:44 +020095 int irq;
Hans de Goede227630c2017-10-04 20:43:36 +020096 bool irq_active_low;
Frederic Danis118612f2015-08-11 16:35:38 +020097
Frederic Danisb7a622a2015-09-23 18:18:09 +020098#ifdef CONFIG_PM
Frederic Danis118612f2015-08-11 16:35:38 +020099 struct hci_uart *hu;
Lukas Wunnerb7c2aba2018-01-10 16:32:10 +0100100 bool is_suspended;
Frederic Danis118612f2015-08-11 16:35:38 +0200101#endif
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700102};
103
Loic Poulain33cd1492017-08-17 19:59:51 +0200104/* generic bcm uart resources */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200105struct bcm_data {
106 struct sk_buff *rx_skb;
107 struct sk_buff_head txq;
108
109 struct bcm_device *dev;
110};
111
112/* List of BCM BT UART devices */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200113static DEFINE_MUTEX(bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200114static LIST_HEAD(bcm_device_list);
115
Loic Poulain33cd1492017-08-17 19:59:51 +0200116static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
117{
118 if (hu->serdev)
119 serdev_device_set_baudrate(hu->serdev, speed);
120 else
121 hci_uart_set_baudrate(hu, speed);
122}
123
Frederic Danis61b2fc22015-06-09 16:15:37 +0200124static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
125{
126 struct hci_dev *hdev = hu->hdev;
127 struct sk_buff *skb;
128 struct bcm_update_uart_baud_rate param;
129
130 if (speed > 3000000) {
131 struct bcm_write_uart_clock_setting clock;
132
133 clock.type = BCM_UART_CLOCK_48MHZ;
134
Frederic Danis65ad07c2015-09-01 12:13:36 +0200135 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200136
137 /* This Broadcom specific command changes the UART's controller
138 * clock for baud rate > 3000000.
139 */
140 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
141 if (IS_ERR(skb)) {
142 int err = PTR_ERR(skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200143 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
144 err);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200145 return err;
146 }
147
148 kfree_skb(skb);
149 }
150
Frederic Danis65ad07c2015-09-01 12:13:36 +0200151 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200152
153 param.zero = cpu_to_le16(0);
154 param.baud_rate = cpu_to_le32(speed);
155
156 /* This Broadcom specific command changes the UART's controller baud
157 * rate.
158 */
159 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
160 HCI_INIT_TIMEOUT);
161 if (IS_ERR(skb)) {
162 int err = PTR_ERR(skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200163 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
164 err);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200165 return err;
166 }
167
168 kfree_skb(skb);
169
170 return 0;
171}
172
Frederic Danis917522a2015-08-28 15:44:00 +0200173/* bcm_device_exists should be protected by bcm_device_lock */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200174static bool bcm_device_exists(struct bcm_device *device)
175{
176 struct list_head *p;
177
Arnd Bergmann81a19052017-10-11 15:46:21 +0200178#ifdef CONFIG_PM
Hans de Goede8a920562017-10-04 20:43:43 +0200179 /* Devices using serdev always exist */
180 if (device && device->hu && device->hu->serdev)
181 return true;
Arnd Bergmann81a19052017-10-11 15:46:21 +0200182#endif
Hans de Goede8a920562017-10-04 20:43:43 +0200183
Frederic Danis0395ffc2015-08-11 16:35:35 +0200184 list_for_each(p, &bcm_device_list) {
185 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
186
187 if (device == dev)
188 return true;
189 }
190
191 return false;
192}
193
194static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
195{
196 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
John Keeping730ce392017-03-15 12:20:05 +0000197 clk_prepare_enable(dev->clk);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200198
Loic Poulain2af0a702015-08-17 16:00:21 +0200199 gpiod_set_value(dev->shutdown, powered);
200 gpiod_set_value(dev->device_wakeup, powered);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200201
202 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
John Keeping730ce392017-03-15 12:20:05 +0000203 clk_disable_unprepare(dev->clk);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200204
205 dev->clk_enabled = powered;
206
207 return 0;
208}
209
Frederic Danisb7a622a2015-09-23 18:18:09 +0200210#ifdef CONFIG_PM
Frederic Danis6cc43962015-09-04 15:35:44 +0200211static irqreturn_t bcm_host_wake(int irq, void *data)
212{
213 struct bcm_device *bdev = data;
214
215 bt_dev_dbg(bdev, "Host wake IRQ");
216
Lukas Wunner43fff7682017-12-26 17:07:34 +0200217 pm_request_resume(bdev->dev);
Frederic Danise88ab302015-09-23 18:18:11 +0200218
Frederic Danis6cc43962015-09-04 15:35:44 +0200219 return IRQ_HANDLED;
220}
221
222static int bcm_request_irq(struct bcm_data *bcm)
223{
224 struct bcm_device *bdev = bcm->dev;
Loic Poulain98dc77d2017-07-04 12:57:56 +0200225 int err;
Frederic Danis6cc43962015-09-04 15:35:44 +0200226
Frederic Danis6cc43962015-09-04 15:35:44 +0200227 mutex_lock(&bcm_device_lock);
228 if (!bcm_device_exists(bdev)) {
229 err = -ENODEV;
230 goto unlock;
231 }
232
Loic Poulain98dc77d2017-07-04 12:57:56 +0200233 if (bdev->irq <= 0) {
234 err = -EOPNOTSUPP;
235 goto unlock;
Frederic Danis6cc43962015-09-04 15:35:44 +0200236 }
237
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200238 err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
Hans de Goede227630c2017-10-04 20:43:36 +0200239 bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
240 IRQF_TRIGGER_RISING,
241 "host_wake", bdev);
Lukas Wunner4dc27332018-01-10 16:32:10 +0100242 if (err) {
243 bdev->irq = err;
Loic Poulain98dc77d2017-07-04 12:57:56 +0200244 goto unlock;
Lukas Wunner4dc27332018-01-10 16:32:10 +0100245 }
Loic Poulain98dc77d2017-07-04 12:57:56 +0200246
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200247 device_init_wakeup(bdev->dev, true);
Loic Poulain98dc77d2017-07-04 12:57:56 +0200248
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200249 pm_runtime_set_autosuspend_delay(bdev->dev,
Loic Poulain98dc77d2017-07-04 12:57:56 +0200250 BCM_AUTOSUSPEND_DELAY);
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200251 pm_runtime_use_autosuspend(bdev->dev);
252 pm_runtime_set_active(bdev->dev);
253 pm_runtime_enable(bdev->dev);
Loic Poulain98dc77d2017-07-04 12:57:56 +0200254
Frederic Danis6cc43962015-09-04 15:35:44 +0200255unlock:
256 mutex_unlock(&bcm_device_lock);
257
258 return err;
259}
260
261static const struct bcm_set_sleep_mode default_sleep_params = {
262 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
263 .idle_host = 2, /* idle threshold HOST, in 300ms */
264 .idle_dev = 2, /* idle threshold device, in 300ms */
265 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
266 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
267 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
Frederic Danise88ab302015-09-23 18:18:11 +0200268 .combine_modes = 1, /* Combine sleep and LPM flag */
Frederic Danis6cc43962015-09-04 15:35:44 +0200269 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
270 /* Irrelevant USB flags */
271 .usb_auto_sleep = 0,
272 .usb_resume_timeout = 0,
273 .pulsed_host_wake = 0,
274 .break_to_host = 0
275};
276
277static int bcm_setup_sleep(struct hci_uart *hu)
278{
279 struct bcm_data *bcm = hu->priv;
280 struct sk_buff *skb;
281 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
282
Hans de Goede227630c2017-10-04 20:43:36 +0200283 sleep_params.host_wake_active = !bcm->dev->irq_active_low;
Frederic Danis6cc43962015-09-04 15:35:44 +0200284
285 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
286 &sleep_params, HCI_INIT_TIMEOUT);
287 if (IS_ERR(skb)) {
288 int err = PTR_ERR(skb);
289 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
290 return err;
291 }
292 kfree_skb(skb);
293
294 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
295
296 return 0;
297}
298#else
299static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
300static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
301#endif
302
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200303static int bcm_set_diag(struct hci_dev *hdev, bool enable)
304{
305 struct hci_uart *hu = hci_get_drvdata(hdev);
306 struct bcm_data *bcm = hu->priv;
307 struct sk_buff *skb;
308
309 if (!test_bit(HCI_RUNNING, &hdev->flags))
310 return -ENETDOWN;
311
312 skb = bt_skb_alloc(3, GFP_KERNEL);
Dan Carpentera1857392015-10-22 12:06:09 +0300313 if (!skb)
314 return -ENOMEM;
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200315
Johannes Berg634fef62017-06-16 14:29:24 +0200316 skb_put_u8(skb, BCM_LM_DIAG_PKT);
317 skb_put_u8(skb, 0xf0);
318 skb_put_u8(skb, enable);
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200319
320 skb_queue_tail(&bcm->txq, skb);
321 hci_uart_tx_wakeup(hu);
322
323 return 0;
324}
325
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700326static int bcm_open(struct hci_uart *hu)
327{
328 struct bcm_data *bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200329 struct list_head *p;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700330
Frederic Danis65ad07c2015-09-01 12:13:36 +0200331 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700332
333 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
334 if (!bcm)
335 return -ENOMEM;
336
337 skb_queue_head_init(&bcm->txq);
338
339 hu->priv = bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200340
Hans de Goede8a920562017-10-04 20:43:43 +0200341 mutex_lock(&bcm_device_lock);
342
Loic Poulain33cd1492017-08-17 19:59:51 +0200343 if (hu->serdev) {
344 serdev_device_open(hu->serdev);
Hans de Goede8a920562017-10-04 20:43:43 +0200345 bcm->dev = serdev_device_get_drvdata(hu->serdev);
Loic Poulain33cd1492017-08-17 19:59:51 +0200346 goto out;
347 }
348
Johan Hovold95065a62017-03-29 18:15:27 +0200349 if (!hu->tty->dev)
350 goto out;
351
Frederic Danis0395ffc2015-08-11 16:35:35 +0200352 list_for_each(p, &bcm_device_list) {
353 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
354
355 /* Retrieve saved bcm_device based on parent of the
356 * platform device (saved during device probe) and
357 * parent of tty device used by hci_uart
358 */
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200359 if (hu->tty->dev->parent == dev->dev->parent) {
Frederic Danis0395ffc2015-08-11 16:35:35 +0200360 bcm->dev = dev;
Frederic Danisb7a622a2015-09-23 18:18:09 +0200361#ifdef CONFIG_PM
Frederic Danis118612f2015-08-11 16:35:38 +0200362 dev->hu = hu;
363#endif
Frederic Danis0395ffc2015-08-11 16:35:35 +0200364 break;
365 }
366 }
367
Johan Hovold95065a62017-03-29 18:15:27 +0200368out:
Hans de Goede8a920562017-10-04 20:43:43 +0200369 if (bcm->dev) {
370 hu->init_speed = bcm->dev->init_speed;
371 hu->oper_speed = bcm->dev->oper_speed;
372 bcm_gpio_set_power(bcm->dev, true);
373 }
374
375 mutex_unlock(&bcm_device_lock);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700376 return 0;
377}
378
379static int bcm_close(struct hci_uart *hu)
380{
381 struct bcm_data *bcm = hu->priv;
Hans de Goede8a920562017-10-04 20:43:43 +0200382 struct bcm_device *bdev = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700383
Frederic Danis65ad07c2015-09-01 12:13:36 +0200384 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700385
Frederic Danis0395ffc2015-08-11 16:35:35 +0200386 /* Protect bcm->dev against removal of the device or driver */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200387 mutex_lock(&bcm_device_lock);
Hans de Goede8a920562017-10-04 20:43:43 +0200388
389 if (hu->serdev) {
390 serdev_device_close(hu->serdev);
391 bdev = serdev_device_get_drvdata(hu->serdev);
392 } else if (bcm_device_exists(bcm->dev)) {
393 bdev = bcm->dev;
394#ifdef CONFIG_PM
395 bdev->hu = NULL;
396#endif
397 }
398
399 if (bdev) {
Lukas Wunner6d83f1e2018-01-10 16:32:10 +0100400 if (IS_ENABLED(CONFIG_PM) && bdev->irq > 0) {
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200401 devm_free_irq(bdev->dev, bdev->irq, bdev);
402 device_init_wakeup(bdev->dev, false);
Lukas Wunnerf4cf6b72018-01-10 16:32:10 +0100403 pm_runtime_disable(bdev->dev);
Frederic Danis6cc43962015-09-04 15:35:44 +0200404 }
Lukas Wunner54ba69f2018-01-10 16:32:10 +0100405
406 bcm_gpio_set_power(bdev, false);
Lukas Wunner54ba69f2018-01-10 16:32:10 +0100407 pm_runtime_set_suspended(bdev->dev);
Frederic Danis118612f2015-08-11 16:35:38 +0200408 }
Frederic Danisbb3ea162015-09-01 12:13:35 +0200409 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200410
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700411 skb_queue_purge(&bcm->txq);
412 kfree_skb(bcm->rx_skb);
413 kfree(bcm);
414
415 hu->priv = NULL;
416 return 0;
417}
418
419static int bcm_flush(struct hci_uart *hu)
420{
421 struct bcm_data *bcm = hu->priv;
422
Frederic Danis65ad07c2015-09-01 12:13:36 +0200423 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700424
425 skb_queue_purge(&bcm->txq);
426
427 return 0;
428}
429
430static int bcm_setup(struct hci_uart *hu)
431{
Frederic Danis6cc43962015-09-04 15:35:44 +0200432 struct bcm_data *bcm = hu->priv;
Frederic Danis6be09b42015-05-28 11:25:05 +0200433 char fw_name[64];
434 const struct firmware *fw;
Frederic Danis960ef1d2015-06-18 12:43:27 +0200435 unsigned int speed;
Frederic Danis6be09b42015-05-28 11:25:05 +0200436 int err;
437
Frederic Danis65ad07c2015-09-01 12:13:36 +0200438 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700439
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200440 hu->hdev->set_diag = bcm_set_diag;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700441 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
442
Frederic Danis6be09b42015-05-28 11:25:05 +0200443 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
444 if (err)
445 return err;
446
447 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
448 if (err < 0) {
Frederic Danis65ad07c2015-09-01 12:13:36 +0200449 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
Frederic Danis6be09b42015-05-28 11:25:05 +0200450 return 0;
451 }
452
453 err = btbcm_patchram(hu->hdev, fw);
454 if (err) {
Frederic Danis65ad07c2015-09-01 12:13:36 +0200455 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
Frederic Danis6be09b42015-05-28 11:25:05 +0200456 goto finalize;
457 }
458
Frederic Danis960ef1d2015-06-18 12:43:27 +0200459 /* Init speed if any */
460 if (hu->init_speed)
461 speed = hu->init_speed;
462 else if (hu->proto->init_speed)
463 speed = hu->proto->init_speed;
464 else
465 speed = 0;
Frederic Danis6be09b42015-05-28 11:25:05 +0200466
Frederic Danis960ef1d2015-06-18 12:43:27 +0200467 if (speed)
Loic Poulain33cd1492017-08-17 19:59:51 +0200468 host_set_baudrate(hu, speed);
Frederic Danis960ef1d2015-06-18 12:43:27 +0200469
470 /* Operational speed if any */
471 if (hu->oper_speed)
472 speed = hu->oper_speed;
473 else if (hu->proto->oper_speed)
474 speed = hu->proto->oper_speed;
475 else
476 speed = 0;
477
478 if (speed) {
479 err = bcm_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200480 if (!err)
Loic Poulain33cd1492017-08-17 19:59:51 +0200481 host_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200482 }
483
Frederic Danis6be09b42015-05-28 11:25:05 +0200484finalize:
485 release_firmware(fw);
486
487 err = btbcm_finalize(hu->hdev);
Frederic Danis6cc43962015-09-04 15:35:44 +0200488 if (err)
489 return err;
490
Loic Poulaincdd24a22017-06-27 19:15:07 +0200491 if (!bcm_request_irq(bcm))
Frederic Danis6cc43962015-09-04 15:35:44 +0200492 err = bcm_setup_sleep(hu);
Frederic Danis6be09b42015-05-28 11:25:05 +0200493
494 return err;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700495}
496
Marcel Holtmann94c58132015-10-07 19:12:54 +0200497#define BCM_RECV_LM_DIAG \
498 .type = BCM_LM_DIAG_PKT, \
499 .hlen = BCM_LM_DIAG_SIZE, \
500 .loff = 0, \
501 .lsize = 0, \
502 .maxlen = BCM_LM_DIAG_SIZE
503
Marcel Holtmann01d5e442017-08-17 21:41:09 +0200504#define BCM_RECV_NULL \
505 .type = BCM_NULL_PKT, \
506 .hlen = BCM_NULL_SIZE, \
507 .loff = 0, \
508 .lsize = 0, \
509 .maxlen = BCM_NULL_SIZE
510
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700511static const struct h4_recv_pkt bcm_recv_pkts[] = {
Marcel Holtmann94c58132015-10-07 19:12:54 +0200512 { H4_RECV_ACL, .recv = hci_recv_frame },
513 { H4_RECV_SCO, .recv = hci_recv_frame },
514 { H4_RECV_EVENT, .recv = hci_recv_frame },
515 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
Marcel Holtmann01d5e442017-08-17 21:41:09 +0200516 { BCM_RECV_NULL, .recv = hci_recv_diag },
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700517};
518
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700519static int bcm_recv(struct hci_uart *hu, const void *data, int count)
520{
521 struct bcm_data *bcm = hu->priv;
522
523 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
524 return -EUNATCH;
525
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700526 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
527 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700528 if (IS_ERR(bcm->rx_skb)) {
529 int err = PTR_ERR(bcm->rx_skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200530 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Chan-yeol Park37134162015-06-17 21:10:39 +0900531 bcm->rx_skb = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700532 return err;
Frederic Danise88ab302015-09-23 18:18:11 +0200533 } else if (!bcm->rx_skb) {
534 /* Delay auto-suspend when receiving completed packet */
535 mutex_lock(&bcm_device_lock);
Lukas Wunner43fff7682017-12-26 17:07:34 +0200536 if (bcm->dev && bcm_device_exists(bcm->dev))
537 pm_request_resume(bcm->dev->dev);
Frederic Danise88ab302015-09-23 18:18:11 +0200538 mutex_unlock(&bcm_device_lock);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700539 }
540
541 return count;
542}
543
544static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
545{
546 struct bcm_data *bcm = hu->priv;
547
Frederic Danis65ad07c2015-09-01 12:13:36 +0200548 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700549
550 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100551 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700552 skb_queue_tail(&bcm->txq, skb);
553
554 return 0;
555}
556
557static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
558{
559 struct bcm_data *bcm = hu->priv;
Frederic Danise88ab302015-09-23 18:18:11 +0200560 struct sk_buff *skb = NULL;
561 struct bcm_device *bdev = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700562
Frederic Danise88ab302015-09-23 18:18:11 +0200563 mutex_lock(&bcm_device_lock);
564
565 if (bcm_device_exists(bcm->dev)) {
566 bdev = bcm->dev;
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200567 pm_runtime_get_sync(bdev->dev);
Frederic Danise88ab302015-09-23 18:18:11 +0200568 /* Shall be resumed here */
569 }
570
571 skb = skb_dequeue(&bcm->txq);
572
573 if (bdev) {
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200574 pm_runtime_mark_last_busy(bdev->dev);
575 pm_runtime_put_autosuspend(bdev->dev);
Frederic Danise88ab302015-09-23 18:18:11 +0200576 }
577
578 mutex_unlock(&bcm_device_lock);
579
580 return skb;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700581}
582
Frederic Danisb7a622a2015-09-23 18:18:09 +0200583#ifdef CONFIG_PM
584static int bcm_suspend_device(struct device *dev)
Frederic Danis118612f2015-08-11 16:35:38 +0200585{
Hans de Goede78277d72017-10-04 20:43:42 +0200586 struct bcm_device *bdev = dev_get_drvdata(dev);
Frederic Danis118612f2015-08-11 16:35:38 +0200587
Frederic Danisb7a622a2015-09-23 18:18:09 +0200588 bt_dev_dbg(bdev, "");
Frederic Danis118612f2015-08-11 16:35:38 +0200589
Frederic Danisb7a622a2015-09-23 18:18:09 +0200590 if (!bdev->is_suspended && bdev->hu) {
Frederic Danis118612f2015-08-11 16:35:38 +0200591 hci_uart_set_flow_control(bdev->hu, true);
592
Frederic Danisb7a622a2015-09-23 18:18:09 +0200593 /* Once this returns, driver suspends BT via GPIO */
Frederic Danis118612f2015-08-11 16:35:38 +0200594 bdev->is_suspended = true;
595 }
596
597 /* Suspend the device */
Lukas Wunner3e81a4c2018-01-10 16:32:10 +0100598 gpiod_set_value(bdev->device_wakeup, false);
599 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
600 mdelay(15);
Frederic Danis118612f2015-08-11 16:35:38 +0200601
Frederic Danisb7a622a2015-09-23 18:18:09 +0200602 return 0;
603}
604
605static int bcm_resume_device(struct device *dev)
606{
Hans de Goede78277d72017-10-04 20:43:42 +0200607 struct bcm_device *bdev = dev_get_drvdata(dev);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200608
609 bt_dev_dbg(bdev, "");
610
Lukas Wunner3e81a4c2018-01-10 16:32:10 +0100611 gpiod_set_value(bdev->device_wakeup, true);
612 bt_dev_dbg(bdev, "resume, delaying 15 ms");
613 mdelay(15);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200614
615 /* When this executes, the device has woken up already */
616 if (bdev->is_suspended && bdev->hu) {
617 bdev->is_suspended = false;
618
619 hci_uart_set_flow_control(bdev->hu, false);
620 }
621
622 return 0;
623}
624#endif
625
626#ifdef CONFIG_PM_SLEEP
Hans de Goede8a920562017-10-04 20:43:43 +0200627/* suspend callback */
Frederic Danisb7a622a2015-09-23 18:18:09 +0200628static int bcm_suspend(struct device *dev)
629{
Hans de Goede78277d72017-10-04 20:43:42 +0200630 struct bcm_device *bdev = dev_get_drvdata(dev);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200631 int error;
632
633 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
634
Hans de Goede8a920562017-10-04 20:43:43 +0200635 /*
636 * When used with a device instantiated as platform_device, bcm_suspend
637 * can be called at any time as long as the platform device is bound,
638 * so it should use bcm_device_lock to protect access to hci_uart
Frederic Danisb7a622a2015-09-23 18:18:09 +0200639 * and device_wake-up GPIO.
640 */
641 mutex_lock(&bcm_device_lock);
642
643 if (!bdev->hu)
644 goto unlock;
645
Frederic Danise88ab302015-09-23 18:18:11 +0200646 if (pm_runtime_active(dev))
647 bcm_suspend_device(dev);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200648
Ronald Tschalär4a59f1f2018-01-10 16:32:10 +0100649 if (device_may_wakeup(dev) && bdev->irq > 0) {
Frederic Danis6cc43962015-09-04 15:35:44 +0200650 error = enable_irq_wake(bdev->irq);
651 if (!error)
652 bt_dev_dbg(bdev, "BCM irq: enabled");
653 }
654
Frederic Danis917522a2015-08-28 15:44:00 +0200655unlock:
Frederic Danisbb3ea162015-09-01 12:13:35 +0200656 mutex_unlock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200657
Frederic Danis118612f2015-08-11 16:35:38 +0200658 return 0;
659}
660
Hans de Goede8a920562017-10-04 20:43:43 +0200661/* resume callback */
Frederic Danis118612f2015-08-11 16:35:38 +0200662static int bcm_resume(struct device *dev)
663{
Hans de Goede78277d72017-10-04 20:43:42 +0200664 struct bcm_device *bdev = dev_get_drvdata(dev);
Frederic Danis118612f2015-08-11 16:35:38 +0200665
Frederic Danis65ad07c2015-09-01 12:13:36 +0200666 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
Frederic Danis118612f2015-08-11 16:35:38 +0200667
Hans de Goede8a920562017-10-04 20:43:43 +0200668 /*
669 * When used with a device instantiated as platform_device, bcm_resume
670 * can be called at any time as long as platform device is bound,
671 * so it should use bcm_device_lock to protect access to hci_uart
Frederic Danisb7a622a2015-09-23 18:18:09 +0200672 * and device_wake-up GPIO.
673 */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200674 mutex_lock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200675
676 if (!bdev->hu)
677 goto unlock;
678
Ronald Tschalär4a59f1f2018-01-10 16:32:10 +0100679 if (device_may_wakeup(dev) && bdev->irq > 0) {
Frederic Danis6cc43962015-09-04 15:35:44 +0200680 disable_irq_wake(bdev->irq);
681 bt_dev_dbg(bdev, "BCM irq: disabled");
682 }
683
Frederic Danisb7a622a2015-09-23 18:18:09 +0200684 bcm_resume_device(dev);
Frederic Danis118612f2015-08-11 16:35:38 +0200685
Frederic Danis917522a2015-08-28 15:44:00 +0200686unlock:
Frederic Danisbb3ea162015-09-01 12:13:35 +0200687 mutex_unlock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200688
Frederic Danise88ab302015-09-23 18:18:11 +0200689 pm_runtime_disable(dev);
690 pm_runtime_set_active(dev);
691 pm_runtime_enable(dev);
692
Frederic Danis118612f2015-08-11 16:35:38 +0200693 return 0;
694}
695#endif
696
Daniel Drake89ab37b2017-01-05 11:10:54 -0600697static const struct acpi_gpio_params int_last_device_wakeup_gpios = { 0, 0, false };
698static const struct acpi_gpio_params int_last_shutdown_gpios = { 1, 0, false };
699static const struct acpi_gpio_params int_last_host_wakeup_gpios = { 2, 0, false };
Frederic Danis0395ffc2015-08-11 16:35:35 +0200700
Daniel Drake89ab37b2017-01-05 11:10:54 -0600701static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
702 { "device-wakeup-gpios", &int_last_device_wakeup_gpios, 1 },
703 { "shutdown-gpios", &int_last_shutdown_gpios, 1 },
704 { "host-wakeup-gpios", &int_last_host_wakeup_gpios, 1 },
705 { },
706};
707
708static const struct acpi_gpio_params int_first_host_wakeup_gpios = { 0, 0, false };
709static const struct acpi_gpio_params int_first_device_wakeup_gpios = { 1, 0, false };
710static const struct acpi_gpio_params int_first_shutdown_gpios = { 2, 0, false };
711
712static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
713 { "device-wakeup-gpios", &int_first_device_wakeup_gpios, 1 },
714 { "shutdown-gpios", &int_first_shutdown_gpios, 1 },
715 { "host-wakeup-gpios", &int_first_host_wakeup_gpios, 1 },
Frederic Danis0395ffc2015-08-11 16:35:35 +0200716 { },
717};
718
Frederic Danis50d78bc2015-08-12 12:46:01 +0200719#ifdef CONFIG_ACPI
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200720/* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
Hans de Goede227630c2017-10-04 20:43:36 +0200721static const struct dmi_system_id bcm_active_low_irq_dmi_table[] = {
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200722 {
723 .ident = "Asus T100TA",
724 .matches = {
725 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
726 "ASUSTeK COMPUTER INC."),
727 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
728 },
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200729 },
Hans de Goedec4c285d2017-06-29 14:21:32 +0200730 {
731 .ident = "Asus T100CHI",
732 .matches = {
733 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
734 "ASUSTeK COMPUTER INC."),
735 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100CHI"),
736 },
Hans de Goedec4c285d2017-06-29 14:21:32 +0200737 },
Jérôme de Bretagne5e2bd932016-10-09 15:51:05 +0200738 { /* Handle ThinkPad 8 tablets with BCM2E55 chipset ACPI ID */
739 .ident = "Lenovo ThinkPad 8",
740 .matches = {
741 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
742 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "ThinkPad 8"),
743 },
Jérôme de Bretagne5e2bd932016-10-09 15:51:05 +0200744 },
Ian W MORRISON1bdb68b2017-10-07 17:15:25 +1100745 {
746 .ident = "MINIX Z83-4",
747 .matches = {
748 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MINIX"),
749 DMI_MATCH(DMI_PRODUCT_NAME, "Z83-4"),
750 },
751 },
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200752 { }
753};
754
Frederic Danisae056902015-08-11 16:35:37 +0200755static int bcm_resource(struct acpi_resource *ares, void *data)
756{
757 struct bcm_device *dev = data;
Frederic Danis6cc43962015-09-04 15:35:44 +0200758 struct acpi_resource_extended_irq *irq;
759 struct acpi_resource_gpio *gpio;
760 struct acpi_resource_uart_serialbus *sb;
Frederic Danisae056902015-08-11 16:35:37 +0200761
Frederic Danis6cc43962015-09-04 15:35:44 +0200762 switch (ares->type) {
763 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
764 irq = &ares->data.extended_irq;
Hans de Goede227630c2017-10-04 20:43:36 +0200765 dev->irq_active_low = irq->polarity == ACPI_ACTIVE_LOW;
Frederic Danis6cc43962015-09-04 15:35:44 +0200766 break;
Frederic Danisae056902015-08-11 16:35:37 +0200767
Frederic Danis6cc43962015-09-04 15:35:44 +0200768 case ACPI_RESOURCE_TYPE_GPIO:
769 gpio = &ares->data.gpio;
770 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
Hans de Goede227630c2017-10-04 20:43:36 +0200771 dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
Frederic Danis6cc43962015-09-04 15:35:44 +0200772 break;
773
774 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
Frederic Danisae056902015-08-11 16:35:37 +0200775 sb = &ares->data.uart_serial_bus;
Marcel Holtmann74183a12017-08-16 09:53:30 +0200776 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
Frederic Danisae056902015-08-11 16:35:37 +0200777 dev->init_speed = sb->default_baud_rate;
Marcel Holtmann74183a12017-08-16 09:53:30 +0200778 dev->oper_speed = 4000000;
779 }
Frederic Danis6cc43962015-09-04 15:35:44 +0200780 break;
781
782 default:
783 break;
Frederic Danisae056902015-08-11 16:35:37 +0200784 }
785
Hans de Goede9d54fd62017-10-04 20:43:41 +0200786 return 0;
Frederic Danisae056902015-08-11 16:35:37 +0200787}
Andy Shevchenko212d7182017-03-10 14:28:20 +0200788#endif /* CONFIG_ACPI */
Frederic Danisae056902015-08-11 16:35:37 +0200789
Hans de Goede42ef18f2017-10-04 20:43:40 +0200790static int bcm_get_resources(struct bcm_device *dev)
Frederic Danis0395ffc2015-08-11 16:35:35 +0200791{
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200792 dev->name = dev_name(dev->dev);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200793
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200794 dev->clk = devm_clk_get(dev->dev, NULL);
Daniel Drake89ab37b2017-01-05 11:10:54 -0600795
Lukas Wunner3e81a4c2018-01-10 16:32:10 +0100796 dev->device_wakeup = devm_gpiod_get(dev->dev, "device-wakeup",
797 GPIOD_OUT_LOW);
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200798 if (IS_ERR(dev->device_wakeup))
799 return PTR_ERR(dev->device_wakeup);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200800
Lukas Wunner3e81a4c2018-01-10 16:32:10 +0100801 dev->shutdown = devm_gpiod_get(dev->dev, "shutdown", GPIOD_OUT_LOW);
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200802 if (IS_ERR(dev->shutdown))
803 return PTR_ERR(dev->shutdown);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200804
Frederic Danis6cc43962015-09-04 15:35:44 +0200805 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
Frederic Danis6cc43962015-09-04 15:35:44 +0200806 if (dev->irq <= 0) {
807 struct gpio_desc *gpio;
808
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200809 gpio = devm_gpiod_get_optional(dev->dev, "host-wakeup",
Frederic Danis6cc43962015-09-04 15:35:44 +0200810 GPIOD_IN);
811 if (IS_ERR(gpio))
812 return PTR_ERR(gpio);
813
814 dev->irq = gpiod_to_irq(gpio);
815 }
816
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200817 dev_info(dev->dev, "BCM irq: %d\n", dev->irq);
Andy Shevchenko212d7182017-03-10 14:28:20 +0200818 return 0;
819}
820
821#ifdef CONFIG_ACPI
822static int bcm_acpi_probe(struct bcm_device *dev)
823{
Andy Shevchenko212d7182017-03-10 14:28:20 +0200824 LIST_HEAD(resources);
825 const struct dmi_system_id *dmi_id;
826 const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
827 const struct acpi_device_id *id;
Hans de Goede9d54fd62017-10-04 20:43:41 +0200828 struct resource_entry *entry;
Andy Shevchenko212d7182017-03-10 14:28:20 +0200829 int ret;
830
831 /* Retrieve GPIO data */
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200832 id = acpi_match_device(dev->dev->driver->acpi_match_table, dev->dev);
Andy Shevchenko212d7182017-03-10 14:28:20 +0200833 if (id)
834 gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data;
835
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200836 ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
Andy Shevchenko212d7182017-03-10 14:28:20 +0200837 if (ret)
838 return ret;
839
Frederic Danisae056902015-08-11 16:35:37 +0200840 /* Retrieve UART ACPI info */
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200841 ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
Jarkko Nikulae98d6d62015-09-30 16:26:45 +0300842 &resources, bcm_resource, dev);
Jarkko Nikula5be00282015-09-30 16:26:42 +0300843 if (ret < 0)
844 return ret;
Hans de Goede9d54fd62017-10-04 20:43:41 +0200845
846 resource_list_for_each_entry(entry, &resources) {
847 if (resource_type(entry->res) == IORESOURCE_IRQ) {
848 dev->irq = entry->res->start;
849 break;
850 }
851 }
Jarkko Nikula09dbf1b2015-09-30 16:26:41 +0300852 acpi_dev_free_resource_list(&resources);
Frederic Danisae056902015-08-11 16:35:37 +0200853
Hans de Goede227630c2017-10-04 20:43:36 +0200854 dmi_id = dmi_first_match(bcm_active_low_irq_dmi_table);
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200855 if (dmi_id) {
Ian W MORRISONe8bfe862017-10-07 17:16:08 +1100856 dev_warn(dev->dev, "%s: Overwriting IRQ polarity to active low",
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200857 dmi_id->ident);
Hans de Goede227630c2017-10-04 20:43:36 +0200858 dev->irq_active_low = true;
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200859 }
860
Frederic Danis0395ffc2015-08-11 16:35:35 +0200861 return 0;
862}
Frederic Danis50d78bc2015-08-12 12:46:01 +0200863#else
864static int bcm_acpi_probe(struct bcm_device *dev)
865{
866 return -EINVAL;
867}
868#endif /* CONFIG_ACPI */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200869
Hans de Goede8a920562017-10-04 20:43:43 +0200870static int bcm_of_probe(struct bcm_device *bdev)
871{
872 device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
873 return 0;
874}
875
Frederic Danis0395ffc2015-08-11 16:35:35 +0200876static int bcm_probe(struct platform_device *pdev)
877{
878 struct bcm_device *dev;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200879 int ret;
880
881 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
882 if (!dev)
883 return -ENOMEM;
884
Hans de Goedec0d3ce52017-10-04 20:43:39 +0200885 dev->dev = &pdev->dev;
Hans de Goede4a56f892017-10-04 20:43:38 +0200886 dev->irq = platform_get_irq(pdev, 0);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200887
Hans de Goede201762e2017-10-04 20:43:37 +0200888 if (has_acpi_companion(&pdev->dev)) {
Andy Shevchenko212d7182017-03-10 14:28:20 +0200889 ret = bcm_acpi_probe(dev);
Hans de Goede201762e2017-10-04 20:43:37 +0200890 if (ret)
891 return ret;
892 }
893
Hans de Goede42ef18f2017-10-04 20:43:40 +0200894 ret = bcm_get_resources(dev);
Jarkko Nikula4d1c4552015-09-30 16:26:44 +0300895 if (ret)
896 return ret;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200897
898 platform_set_drvdata(pdev, dev);
899
900 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
901
902 /* Place this instance on the device list */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200903 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200904 list_add_tail(&dev->list, &bcm_device_list);
Frederic Danisbb3ea162015-09-01 12:13:35 +0200905 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200906
907 bcm_gpio_set_power(dev, false);
908
909 return 0;
910}
911
912static int bcm_remove(struct platform_device *pdev)
913{
914 struct bcm_device *dev = platform_get_drvdata(pdev);
915
Frederic Danisbb3ea162015-09-01 12:13:35 +0200916 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200917 list_del(&dev->list);
Frederic Danisbb3ea162015-09-01 12:13:35 +0200918 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200919
Frederic Danis0395ffc2015-08-11 16:35:35 +0200920 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
921
922 return 0;
923}
924
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700925static const struct hci_uart_proto bcm_proto = {
926 .id = HCI_UART_BCM,
Loic Poulain143f0a22016-09-19 12:05:12 +0200927 .name = "Broadcom",
Marcel Holtmannaee61f72015-10-20 21:30:45 +0200928 .manufacturer = 15,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200929 .init_speed = 115200,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700930 .open = bcm_open,
931 .close = bcm_close,
932 .flush = bcm_flush,
933 .setup = bcm_setup,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200934 .set_baudrate = bcm_set_baudrate,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700935 .recv = bcm_recv,
936 .enqueue = bcm_enqueue,
937 .dequeue = bcm_dequeue,
938};
939
Frederic Danis0395ffc2015-08-11 16:35:35 +0200940#ifdef CONFIG_ACPI
941static const struct acpi_device_id bcm_acpi_match[] = {
Daniel Drake89ab37b2017-01-05 11:10:54 -0600942 { "BCM2E1A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
943 { "BCM2E39", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
944 { "BCM2E3A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
945 { "BCM2E3D", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
946 { "BCM2E3F", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
947 { "BCM2E40", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
948 { "BCM2E54", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
949 { "BCM2E55", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
950 { "BCM2E64", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
951 { "BCM2E65", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
952 { "BCM2E67", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
953 { "BCM2E71", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
Hans de Goedec23fae12017-11-22 14:37:28 +0100954 { "BCM2E72", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
Daniel Drake89ab37b2017-01-05 11:10:54 -0600955 { "BCM2E7B", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
956 { "BCM2E7C", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
Hans de Goede61d220a2017-10-13 17:54:02 +0200957 { "BCM2E7E", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
Daniel Drake89ab37b2017-01-05 11:10:54 -0600958 { "BCM2E95", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
959 { "BCM2E96", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
Ian W MORRISON1bdb68b2017-10-07 17:15:25 +1100960 { "BCM2EA4", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
Frederic Danis0395ffc2015-08-11 16:35:35 +0200961 { },
962};
963MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
964#endif
965
Hans de Goede8a920562017-10-04 20:43:43 +0200966/* suspend and resume callbacks */
Frederic Danise88ab302015-09-23 18:18:11 +0200967static const struct dev_pm_ops bcm_pm_ops = {
968 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
969 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
970};
Frederic Danis118612f2015-08-11 16:35:38 +0200971
Frederic Danis0395ffc2015-08-11 16:35:35 +0200972static struct platform_driver bcm_driver = {
973 .probe = bcm_probe,
974 .remove = bcm_remove,
975 .driver = {
976 .name = "hci_bcm",
977 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
Frederic Danis118612f2015-08-11 16:35:38 +0200978 .pm = &bcm_pm_ops,
Frederic Danis0395ffc2015-08-11 16:35:35 +0200979 },
980};
981
Loic Poulain33cd1492017-08-17 19:59:51 +0200982static int bcm_serdev_probe(struct serdev_device *serdev)
983{
Hans de Goede8a920562017-10-04 20:43:43 +0200984 struct bcm_device *bcmdev;
Loic Poulain33cd1492017-08-17 19:59:51 +0200985 int err;
986
987 bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
988 if (!bcmdev)
989 return -ENOMEM;
990
Hans de Goede8a920562017-10-04 20:43:43 +0200991 bcmdev->dev = &serdev->dev;
Arnd Bergmann81a19052017-10-11 15:46:21 +0200992#ifdef CONFIG_PM
Hans de Goede8a920562017-10-04 20:43:43 +0200993 bcmdev->hu = &bcmdev->serdev_hu;
Arnd Bergmann81a19052017-10-11 15:46:21 +0200994#endif
Hans de Goede8a920562017-10-04 20:43:43 +0200995 bcmdev->serdev_hu.serdev = serdev;
Loic Poulain33cd1492017-08-17 19:59:51 +0200996 serdev_device_set_drvdata(serdev, bcmdev);
997
Hans de Goede8a920562017-10-04 20:43:43 +0200998 if (has_acpi_companion(&serdev->dev))
999 err = bcm_acpi_probe(bcmdev);
1000 else
1001 err = bcm_of_probe(bcmdev);
1002 if (err)
1003 return err;
Loic Poulain33cd1492017-08-17 19:59:51 +02001004
Hans de Goede8a920562017-10-04 20:43:43 +02001005 err = bcm_get_resources(bcmdev);
1006 if (err)
1007 return err;
1008
1009 bcm_gpio_set_power(bcmdev, false);
1010
1011 return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
Loic Poulain33cd1492017-08-17 19:59:51 +02001012}
1013
1014static void bcm_serdev_remove(struct serdev_device *serdev)
1015{
Hans de Goede8a920562017-10-04 20:43:43 +02001016 struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
Loic Poulain33cd1492017-08-17 19:59:51 +02001017
Hans de Goede8a920562017-10-04 20:43:43 +02001018 hci_uart_unregister_device(&bcmdev->serdev_hu);
Loic Poulain33cd1492017-08-17 19:59:51 +02001019}
1020
1021#ifdef CONFIG_OF
1022static const struct of_device_id bcm_bluetooth_of_match[] = {
1023 { .compatible = "brcm,bcm43438-bt" },
1024 { },
1025};
1026MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
1027#endif
1028
1029static struct serdev_device_driver bcm_serdev_driver = {
1030 .probe = bcm_serdev_probe,
1031 .remove = bcm_serdev_remove,
1032 .driver = {
1033 .name = "hci_uart_bcm",
1034 .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
Hans de Goede8a920562017-10-04 20:43:43 +02001035 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
1036 .pm = &bcm_pm_ops,
Loic Poulain33cd1492017-08-17 19:59:51 +02001037 },
1038};
1039
Marcel Holtmannbdd88182015-04-05 22:52:18 -07001040int __init bcm_init(void)
1041{
Loic Poulain33cd1492017-08-17 19:59:51 +02001042 /* For now, we need to keep both platform device
1043 * driver (ACPI generated) and serdev driver (DT).
1044 */
Frederic Danis0395ffc2015-08-11 16:35:35 +02001045 platform_driver_register(&bcm_driver);
Loic Poulain33cd1492017-08-17 19:59:51 +02001046 serdev_device_driver_register(&bcm_serdev_driver);
Frederic Danis0395ffc2015-08-11 16:35:35 +02001047
Marcel Holtmannbdd88182015-04-05 22:52:18 -07001048 return hci_uart_register_proto(&bcm_proto);
1049}
1050
1051int __exit bcm_deinit(void)
1052{
Frederic Danis0395ffc2015-08-11 16:35:35 +02001053 platform_driver_unregister(&bcm_driver);
Loic Poulain33cd1492017-08-17 19:59:51 +02001054 serdev_device_driver_unregister(&bcm_serdev_driver);
Frederic Danis0395ffc2015-08-11 16:35:35 +02001055
Marcel Holtmannbdd88182015-04-05 22:52:18 -07001056 return hci_uart_unregister_proto(&bcm_proto);
1057}