blob: 1eb286ade48aa043aa35a496a903a8c74b99d4fe [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>
Frederic Danis6cc43962015-09-04 15:35:44 +020034#include <linux/interrupt.h>
Frederic Danis5cebdfe2015-09-23 18:18:08 +020035#include <linux/dmi.h>
Frederic Danise88ab302015-09-23 18:18:11 +020036#include <linux/pm_runtime.h>
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -070037
38#include <net/bluetooth/bluetooth.h>
39#include <net/bluetooth/hci_core.h>
40
Marcel Holtmannbdd88182015-04-05 22:52:18 -070041#include "btbcm.h"
Marcel Holtmanne9a2dd22015-04-04 16:13:03 -070042#include "hci_uart.h"
Marcel Holtmannbdd88182015-04-05 22:52:18 -070043
Marcel Holtmann94c58132015-10-07 19:12:54 +020044#define BCM_LM_DIAG_PKT 0x07
45#define BCM_LM_DIAG_SIZE 63
46
Frederic Danise88ab302015-09-23 18:18:11 +020047#define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
48
Frederic Danis0395ffc2015-08-11 16:35:35 +020049struct bcm_device {
50 struct list_head list;
51
52 struct platform_device *pdev;
53
54 const char *name;
55 struct gpio_desc *device_wakeup;
56 struct gpio_desc *shutdown;
57
58 struct clk *clk;
59 bool clk_enabled;
Frederic Danisae056902015-08-11 16:35:37 +020060
61 u32 init_speed;
Marcel Holtmann74183a12017-08-16 09:53:30 +020062 u32 oper_speed;
Frederic Danis6cc43962015-09-04 15:35:44 +020063 int irq;
64 u8 irq_polarity;
Frederic Danis118612f2015-08-11 16:35:38 +020065
Frederic Danisb7a622a2015-09-23 18:18:09 +020066#ifdef CONFIG_PM
Frederic Danis118612f2015-08-11 16:35:38 +020067 struct hci_uart *hu;
68 bool is_suspended; /* suspend/resume flag */
69#endif
Marcel Holtmannbdd88182015-04-05 22:52:18 -070070};
71
Frederic Danis0395ffc2015-08-11 16:35:35 +020072struct bcm_data {
73 struct sk_buff *rx_skb;
74 struct sk_buff_head txq;
75
76 struct bcm_device *dev;
77};
78
79/* List of BCM BT UART devices */
Frederic Danisbb3ea162015-09-01 12:13:35 +020080static DEFINE_MUTEX(bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +020081static LIST_HEAD(bcm_device_list);
82
Frederic Danis61b2fc22015-06-09 16:15:37 +020083static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
84{
85 struct hci_dev *hdev = hu->hdev;
86 struct sk_buff *skb;
87 struct bcm_update_uart_baud_rate param;
88
89 if (speed > 3000000) {
90 struct bcm_write_uart_clock_setting clock;
91
92 clock.type = BCM_UART_CLOCK_48MHZ;
93
Frederic Danis65ad07c2015-09-01 12:13:36 +020094 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
Frederic Danis61b2fc22015-06-09 16:15:37 +020095
96 /* This Broadcom specific command changes the UART's controller
97 * clock for baud rate > 3000000.
98 */
99 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
100 if (IS_ERR(skb)) {
101 int err = PTR_ERR(skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200102 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
103 err);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200104 return err;
105 }
106
107 kfree_skb(skb);
108 }
109
Frederic Danis65ad07c2015-09-01 12:13:36 +0200110 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200111
112 param.zero = cpu_to_le16(0);
113 param.baud_rate = cpu_to_le32(speed);
114
115 /* This Broadcom specific command changes the UART's controller baud
116 * rate.
117 */
118 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
119 HCI_INIT_TIMEOUT);
120 if (IS_ERR(skb)) {
121 int err = PTR_ERR(skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200122 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
123 err);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200124 return err;
125 }
126
127 kfree_skb(skb);
128
129 return 0;
130}
131
Frederic Danis917522a2015-08-28 15:44:00 +0200132/* bcm_device_exists should be protected by bcm_device_lock */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200133static bool bcm_device_exists(struct bcm_device *device)
134{
135 struct list_head *p;
136
137 list_for_each(p, &bcm_device_list) {
138 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
139
140 if (device == dev)
141 return true;
142 }
143
144 return false;
145}
146
147static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
148{
149 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
John Keeping730ce392017-03-15 12:20:05 +0000150 clk_prepare_enable(dev->clk);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200151
Loic Poulain2af0a702015-08-17 16:00:21 +0200152 gpiod_set_value(dev->shutdown, powered);
153 gpiod_set_value(dev->device_wakeup, powered);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200154
155 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
John Keeping730ce392017-03-15 12:20:05 +0000156 clk_disable_unprepare(dev->clk);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200157
158 dev->clk_enabled = powered;
159
160 return 0;
161}
162
Frederic Danisb7a622a2015-09-23 18:18:09 +0200163#ifdef CONFIG_PM
Frederic Danis6cc43962015-09-04 15:35:44 +0200164static irqreturn_t bcm_host_wake(int irq, void *data)
165{
166 struct bcm_device *bdev = data;
167
168 bt_dev_dbg(bdev, "Host wake IRQ");
169
Frederic Danise88ab302015-09-23 18:18:11 +0200170 pm_runtime_get(&bdev->pdev->dev);
171 pm_runtime_mark_last_busy(&bdev->pdev->dev);
172 pm_runtime_put_autosuspend(&bdev->pdev->dev);
173
Frederic Danis6cc43962015-09-04 15:35:44 +0200174 return IRQ_HANDLED;
175}
176
177static int bcm_request_irq(struct bcm_data *bcm)
178{
179 struct bcm_device *bdev = bcm->dev;
Loic Poulain98dc77d2017-07-04 12:57:56 +0200180 int err;
Frederic Danis6cc43962015-09-04 15:35:44 +0200181
182 /* If this is not a platform device, do not enable PM functionalities */
183 mutex_lock(&bcm_device_lock);
184 if (!bcm_device_exists(bdev)) {
185 err = -ENODEV;
186 goto unlock;
187 }
188
Loic Poulain98dc77d2017-07-04 12:57:56 +0200189 if (bdev->irq <= 0) {
190 err = -EOPNOTSUPP;
191 goto unlock;
Frederic Danis6cc43962015-09-04 15:35:44 +0200192 }
193
Loic Poulain98dc77d2017-07-04 12:57:56 +0200194 err = devm_request_irq(&bdev->pdev->dev, bdev->irq, bcm_host_wake,
195 IRQF_TRIGGER_RISING, "host_wake", bdev);
196 if (err)
197 goto unlock;
198
199 device_init_wakeup(&bdev->pdev->dev, true);
200
201 pm_runtime_set_autosuspend_delay(&bdev->pdev->dev,
202 BCM_AUTOSUSPEND_DELAY);
203 pm_runtime_use_autosuspend(&bdev->pdev->dev);
204 pm_runtime_set_active(&bdev->pdev->dev);
205 pm_runtime_enable(&bdev->pdev->dev);
206
Frederic Danis6cc43962015-09-04 15:35:44 +0200207unlock:
208 mutex_unlock(&bcm_device_lock);
209
210 return err;
211}
212
213static const struct bcm_set_sleep_mode default_sleep_params = {
214 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
215 .idle_host = 2, /* idle threshold HOST, in 300ms */
216 .idle_dev = 2, /* idle threshold device, in 300ms */
217 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
218 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
219 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
Frederic Danise88ab302015-09-23 18:18:11 +0200220 .combine_modes = 1, /* Combine sleep and LPM flag */
Frederic Danis6cc43962015-09-04 15:35:44 +0200221 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
222 /* Irrelevant USB flags */
223 .usb_auto_sleep = 0,
224 .usb_resume_timeout = 0,
225 .pulsed_host_wake = 0,
226 .break_to_host = 0
227};
228
229static int bcm_setup_sleep(struct hci_uart *hu)
230{
231 struct bcm_data *bcm = hu->priv;
232 struct sk_buff *skb;
233 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
234
235 sleep_params.host_wake_active = !bcm->dev->irq_polarity;
236
237 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
238 &sleep_params, HCI_INIT_TIMEOUT);
239 if (IS_ERR(skb)) {
240 int err = PTR_ERR(skb);
241 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
242 return err;
243 }
244 kfree_skb(skb);
245
246 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
247
248 return 0;
249}
250#else
251static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
252static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
253#endif
254
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200255static int bcm_set_diag(struct hci_dev *hdev, bool enable)
256{
257 struct hci_uart *hu = hci_get_drvdata(hdev);
258 struct bcm_data *bcm = hu->priv;
259 struct sk_buff *skb;
260
261 if (!test_bit(HCI_RUNNING, &hdev->flags))
262 return -ENETDOWN;
263
264 skb = bt_skb_alloc(3, GFP_KERNEL);
Dan Carpentera1857392015-10-22 12:06:09 +0300265 if (!skb)
266 return -ENOMEM;
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200267
Johannes Berg634fef62017-06-16 14:29:24 +0200268 skb_put_u8(skb, BCM_LM_DIAG_PKT);
269 skb_put_u8(skb, 0xf0);
270 skb_put_u8(skb, enable);
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200271
272 skb_queue_tail(&bcm->txq, skb);
273 hci_uart_tx_wakeup(hu);
274
275 return 0;
276}
277
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700278static int bcm_open(struct hci_uart *hu)
279{
280 struct bcm_data *bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200281 struct list_head *p;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700282
Frederic Danis65ad07c2015-09-01 12:13:36 +0200283 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700284
285 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
286 if (!bcm)
287 return -ENOMEM;
288
289 skb_queue_head_init(&bcm->txq);
290
291 hu->priv = bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200292
Johan Hovold95065a62017-03-29 18:15:27 +0200293 if (!hu->tty->dev)
294 goto out;
295
Frederic Danisbb3ea162015-09-01 12:13:35 +0200296 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200297 list_for_each(p, &bcm_device_list) {
298 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
299
300 /* Retrieve saved bcm_device based on parent of the
301 * platform device (saved during device probe) and
302 * parent of tty device used by hci_uart
303 */
304 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
305 bcm->dev = dev;
Frederic Danisae056902015-08-11 16:35:37 +0200306 hu->init_speed = dev->init_speed;
Marcel Holtmann74183a12017-08-16 09:53:30 +0200307 hu->oper_speed = dev->oper_speed;
Frederic Danisb7a622a2015-09-23 18:18:09 +0200308#ifdef CONFIG_PM
Frederic Danis118612f2015-08-11 16:35:38 +0200309 dev->hu = hu;
310#endif
Frederic Danis6cc43962015-09-04 15:35:44 +0200311 bcm_gpio_set_power(bcm->dev, true);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200312 break;
313 }
314 }
315
Frederic Danisbb3ea162015-09-01 12:13:35 +0200316 mutex_unlock(&bcm_device_lock);
Johan Hovold95065a62017-03-29 18:15:27 +0200317out:
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700318 return 0;
319}
320
321static int bcm_close(struct hci_uart *hu)
322{
323 struct bcm_data *bcm = hu->priv;
Frederic Danis6cc43962015-09-04 15:35:44 +0200324 struct bcm_device *bdev = bcm->dev;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700325
Frederic Danis65ad07c2015-09-01 12:13:36 +0200326 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700327
Frederic Danis0395ffc2015-08-11 16:35:35 +0200328 /* Protect bcm->dev against removal of the device or driver */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200329 mutex_lock(&bcm_device_lock);
Frederic Danis6cc43962015-09-04 15:35:44 +0200330 if (bcm_device_exists(bdev)) {
331 bcm_gpio_set_power(bdev, false);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200332#ifdef CONFIG_PM
Frederic Danise88ab302015-09-23 18:18:11 +0200333 pm_runtime_disable(&bdev->pdev->dev);
334 pm_runtime_set_suspended(&bdev->pdev->dev);
335
Frederic Danis6cc43962015-09-04 15:35:44 +0200336 if (device_can_wakeup(&bdev->pdev->dev)) {
337 devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev);
338 device_init_wakeup(&bdev->pdev->dev, false);
339 }
340
341 bdev->hu = NULL;
Frederic Danis118612f2015-08-11 16:35:38 +0200342#endif
343 }
Frederic Danisbb3ea162015-09-01 12:13:35 +0200344 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200345
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700346 skb_queue_purge(&bcm->txq);
347 kfree_skb(bcm->rx_skb);
348 kfree(bcm);
349
350 hu->priv = NULL;
351 return 0;
352}
353
354static int bcm_flush(struct hci_uart *hu)
355{
356 struct bcm_data *bcm = hu->priv;
357
Frederic Danis65ad07c2015-09-01 12:13:36 +0200358 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700359
360 skb_queue_purge(&bcm->txq);
361
362 return 0;
363}
364
365static int bcm_setup(struct hci_uart *hu)
366{
Frederic Danis6cc43962015-09-04 15:35:44 +0200367 struct bcm_data *bcm = hu->priv;
Frederic Danis6be09b42015-05-28 11:25:05 +0200368 char fw_name[64];
369 const struct firmware *fw;
Frederic Danis960ef1d2015-06-18 12:43:27 +0200370 unsigned int speed;
Frederic Danis6be09b42015-05-28 11:25:05 +0200371 int err;
372
Frederic Danis65ad07c2015-09-01 12:13:36 +0200373 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700374
Marcel Holtmann075e1f52015-10-07 20:08:26 +0200375 hu->hdev->set_diag = bcm_set_diag;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700376 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
377
Frederic Danis6be09b42015-05-28 11:25:05 +0200378 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
379 if (err)
380 return err;
381
382 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
383 if (err < 0) {
Frederic Danis65ad07c2015-09-01 12:13:36 +0200384 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
Frederic Danis6be09b42015-05-28 11:25:05 +0200385 return 0;
386 }
387
388 err = btbcm_patchram(hu->hdev, fw);
389 if (err) {
Frederic Danis65ad07c2015-09-01 12:13:36 +0200390 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
Frederic Danis6be09b42015-05-28 11:25:05 +0200391 goto finalize;
392 }
393
Frederic Danis960ef1d2015-06-18 12:43:27 +0200394 /* Init speed if any */
395 if (hu->init_speed)
396 speed = hu->init_speed;
397 else if (hu->proto->init_speed)
398 speed = hu->proto->init_speed;
399 else
400 speed = 0;
Frederic Danis6be09b42015-05-28 11:25:05 +0200401
Frederic Danis960ef1d2015-06-18 12:43:27 +0200402 if (speed)
403 hci_uart_set_baudrate(hu, speed);
404
405 /* Operational speed if any */
406 if (hu->oper_speed)
407 speed = hu->oper_speed;
408 else if (hu->proto->oper_speed)
409 speed = hu->proto->oper_speed;
410 else
411 speed = 0;
412
413 if (speed) {
414 err = bcm_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200415 if (!err)
Frederic Danis960ef1d2015-06-18 12:43:27 +0200416 hci_uart_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200417 }
418
Frederic Danis6be09b42015-05-28 11:25:05 +0200419finalize:
420 release_firmware(fw);
421
422 err = btbcm_finalize(hu->hdev);
Frederic Danis6cc43962015-09-04 15:35:44 +0200423 if (err)
424 return err;
425
Loic Poulaincdd24a22017-06-27 19:15:07 +0200426 if (!bcm_request_irq(bcm))
Frederic Danis6cc43962015-09-04 15:35:44 +0200427 err = bcm_setup_sleep(hu);
Frederic Danis6be09b42015-05-28 11:25:05 +0200428
429 return err;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700430}
431
Marcel Holtmann94c58132015-10-07 19:12:54 +0200432#define BCM_RECV_LM_DIAG \
433 .type = BCM_LM_DIAG_PKT, \
434 .hlen = BCM_LM_DIAG_SIZE, \
435 .loff = 0, \
436 .lsize = 0, \
437 .maxlen = BCM_LM_DIAG_SIZE
438
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700439static const struct h4_recv_pkt bcm_recv_pkts[] = {
Marcel Holtmann94c58132015-10-07 19:12:54 +0200440 { H4_RECV_ACL, .recv = hci_recv_frame },
441 { H4_RECV_SCO, .recv = hci_recv_frame },
442 { H4_RECV_EVENT, .recv = hci_recv_frame },
443 { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700444};
445
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700446static int bcm_recv(struct hci_uart *hu, const void *data, int count)
447{
448 struct bcm_data *bcm = hu->priv;
449
450 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
451 return -EUNATCH;
452
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700453 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
454 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700455 if (IS_ERR(bcm->rx_skb)) {
456 int err = PTR_ERR(bcm->rx_skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200457 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Chan-yeol Park37134162015-06-17 21:10:39 +0900458 bcm->rx_skb = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700459 return err;
Frederic Danise88ab302015-09-23 18:18:11 +0200460 } else if (!bcm->rx_skb) {
461 /* Delay auto-suspend when receiving completed packet */
462 mutex_lock(&bcm_device_lock);
463 if (bcm->dev && bcm_device_exists(bcm->dev)) {
464 pm_runtime_get(&bcm->dev->pdev->dev);
465 pm_runtime_mark_last_busy(&bcm->dev->pdev->dev);
466 pm_runtime_put_autosuspend(&bcm->dev->pdev->dev);
467 }
468 mutex_unlock(&bcm_device_lock);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700469 }
470
471 return count;
472}
473
474static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
475{
476 struct bcm_data *bcm = hu->priv;
477
Frederic Danis65ad07c2015-09-01 12:13:36 +0200478 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700479
480 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100481 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700482 skb_queue_tail(&bcm->txq, skb);
483
484 return 0;
485}
486
487static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
488{
489 struct bcm_data *bcm = hu->priv;
Frederic Danise88ab302015-09-23 18:18:11 +0200490 struct sk_buff *skb = NULL;
491 struct bcm_device *bdev = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700492
Frederic Danise88ab302015-09-23 18:18:11 +0200493 mutex_lock(&bcm_device_lock);
494
495 if (bcm_device_exists(bcm->dev)) {
496 bdev = bcm->dev;
497 pm_runtime_get_sync(&bdev->pdev->dev);
498 /* Shall be resumed here */
499 }
500
501 skb = skb_dequeue(&bcm->txq);
502
503 if (bdev) {
504 pm_runtime_mark_last_busy(&bdev->pdev->dev);
505 pm_runtime_put_autosuspend(&bdev->pdev->dev);
506 }
507
508 mutex_unlock(&bcm_device_lock);
509
510 return skb;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700511}
512
Frederic Danisb7a622a2015-09-23 18:18:09 +0200513#ifdef CONFIG_PM
514static int bcm_suspend_device(struct device *dev)
Frederic Danis118612f2015-08-11 16:35:38 +0200515{
516 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
517
Frederic Danisb7a622a2015-09-23 18:18:09 +0200518 bt_dev_dbg(bdev, "");
Frederic Danis118612f2015-08-11 16:35:38 +0200519
Frederic Danisb7a622a2015-09-23 18:18:09 +0200520 if (!bdev->is_suspended && bdev->hu) {
Frederic Danis118612f2015-08-11 16:35:38 +0200521 hci_uart_set_flow_control(bdev->hu, true);
522
Frederic Danisb7a622a2015-09-23 18:18:09 +0200523 /* Once this returns, driver suspends BT via GPIO */
Frederic Danis118612f2015-08-11 16:35:38 +0200524 bdev->is_suspended = true;
525 }
526
527 /* Suspend the device */
528 if (bdev->device_wakeup) {
529 gpiod_set_value(bdev->device_wakeup, false);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200530 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
Frederic Danis118612f2015-08-11 16:35:38 +0200531 mdelay(15);
532 }
533
Frederic Danisb7a622a2015-09-23 18:18:09 +0200534 return 0;
535}
536
537static int bcm_resume_device(struct device *dev)
538{
539 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
540
541 bt_dev_dbg(bdev, "");
542
543 if (bdev->device_wakeup) {
544 gpiod_set_value(bdev->device_wakeup, true);
545 bt_dev_dbg(bdev, "resume, delaying 15 ms");
546 mdelay(15);
547 }
548
549 /* When this executes, the device has woken up already */
550 if (bdev->is_suspended && bdev->hu) {
551 bdev->is_suspended = false;
552
553 hci_uart_set_flow_control(bdev->hu, false);
554 }
555
556 return 0;
557}
558#endif
559
560#ifdef CONFIG_PM_SLEEP
561/* Platform suspend callback */
562static int bcm_suspend(struct device *dev)
563{
564 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
565 int error;
566
567 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
568
569 /* bcm_suspend can be called at any time as long as platform device is
570 * bound, so it should use bcm_device_lock to protect access to hci_uart
571 * and device_wake-up GPIO.
572 */
573 mutex_lock(&bcm_device_lock);
574
575 if (!bdev->hu)
576 goto unlock;
577
Frederic Danise88ab302015-09-23 18:18:11 +0200578 if (pm_runtime_active(dev))
579 bcm_suspend_device(dev);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200580
Frederic Danis6cc43962015-09-04 15:35:44 +0200581 if (device_may_wakeup(&bdev->pdev->dev)) {
582 error = enable_irq_wake(bdev->irq);
583 if (!error)
584 bt_dev_dbg(bdev, "BCM irq: enabled");
585 }
586
Frederic Danis917522a2015-08-28 15:44:00 +0200587unlock:
Frederic Danisbb3ea162015-09-01 12:13:35 +0200588 mutex_unlock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200589
Frederic Danis118612f2015-08-11 16:35:38 +0200590 return 0;
591}
592
593/* Platform resume callback */
594static int bcm_resume(struct device *dev)
595{
596 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
597
Frederic Danis65ad07c2015-09-01 12:13:36 +0200598 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
Frederic Danis118612f2015-08-11 16:35:38 +0200599
Frederic Danisb7a622a2015-09-23 18:18:09 +0200600 /* bcm_resume can be called at any time as long as platform device is
601 * bound, so it should use bcm_device_lock to protect access to hci_uart
602 * and device_wake-up GPIO.
603 */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200604 mutex_lock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200605
606 if (!bdev->hu)
607 goto unlock;
608
Frederic Danis6cc43962015-09-04 15:35:44 +0200609 if (device_may_wakeup(&bdev->pdev->dev)) {
610 disable_irq_wake(bdev->irq);
611 bt_dev_dbg(bdev, "BCM irq: disabled");
612 }
613
Frederic Danisb7a622a2015-09-23 18:18:09 +0200614 bcm_resume_device(dev);
Frederic Danis118612f2015-08-11 16:35:38 +0200615
Frederic Danis917522a2015-08-28 15:44:00 +0200616unlock:
Frederic Danisbb3ea162015-09-01 12:13:35 +0200617 mutex_unlock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200618
Frederic Danise88ab302015-09-23 18:18:11 +0200619 pm_runtime_disable(dev);
620 pm_runtime_set_active(dev);
621 pm_runtime_enable(dev);
622
Frederic Danis118612f2015-08-11 16:35:38 +0200623 return 0;
624}
625#endif
626
Daniel Drake89ab37b2017-01-05 11:10:54 -0600627static const struct acpi_gpio_params int_last_device_wakeup_gpios = { 0, 0, false };
628static const struct acpi_gpio_params int_last_shutdown_gpios = { 1, 0, false };
629static const struct acpi_gpio_params int_last_host_wakeup_gpios = { 2, 0, false };
Frederic Danis0395ffc2015-08-11 16:35:35 +0200630
Daniel Drake89ab37b2017-01-05 11:10:54 -0600631static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
632 { "device-wakeup-gpios", &int_last_device_wakeup_gpios, 1 },
633 { "shutdown-gpios", &int_last_shutdown_gpios, 1 },
634 { "host-wakeup-gpios", &int_last_host_wakeup_gpios, 1 },
635 { },
636};
637
638static const struct acpi_gpio_params int_first_host_wakeup_gpios = { 0, 0, false };
639static const struct acpi_gpio_params int_first_device_wakeup_gpios = { 1, 0, false };
640static const struct acpi_gpio_params int_first_shutdown_gpios = { 2, 0, false };
641
642static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
643 { "device-wakeup-gpios", &int_first_device_wakeup_gpios, 1 },
644 { "shutdown-gpios", &int_first_shutdown_gpios, 1 },
645 { "host-wakeup-gpios", &int_first_host_wakeup_gpios, 1 },
Frederic Danis0395ffc2015-08-11 16:35:35 +0200646 { },
647};
648
Frederic Danis50d78bc2015-08-12 12:46:01 +0200649#ifdef CONFIG_ACPI
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200650static u8 acpi_active_low = ACPI_ACTIVE_LOW;
651
652/* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
653static const struct dmi_system_id bcm_wrong_irq_dmi_table[] = {
654 {
655 .ident = "Asus T100TA",
656 .matches = {
657 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
658 "ASUSTeK COMPUTER INC."),
659 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
660 },
661 .driver_data = &acpi_active_low,
662 },
Hans de Goedec4c285d2017-06-29 14:21:32 +0200663 {
664 .ident = "Asus T100CHI",
665 .matches = {
666 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
667 "ASUSTeK COMPUTER INC."),
668 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100CHI"),
669 },
670 .driver_data = &acpi_active_low,
671 },
Jérôme de Bretagne5e2bd932016-10-09 15:51:05 +0200672 { /* Handle ThinkPad 8 tablets with BCM2E55 chipset ACPI ID */
673 .ident = "Lenovo ThinkPad 8",
674 .matches = {
675 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
676 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "ThinkPad 8"),
677 },
678 .driver_data = &acpi_active_low,
679 },
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200680 { }
681};
682
Frederic Danisae056902015-08-11 16:35:37 +0200683static int bcm_resource(struct acpi_resource *ares, void *data)
684{
685 struct bcm_device *dev = data;
Frederic Danis6cc43962015-09-04 15:35:44 +0200686 struct acpi_resource_extended_irq *irq;
687 struct acpi_resource_gpio *gpio;
688 struct acpi_resource_uart_serialbus *sb;
Frederic Danisae056902015-08-11 16:35:37 +0200689
Frederic Danis6cc43962015-09-04 15:35:44 +0200690 switch (ares->type) {
691 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
692 irq = &ares->data.extended_irq;
693 dev->irq_polarity = irq->polarity;
694 break;
Frederic Danisae056902015-08-11 16:35:37 +0200695
Frederic Danis6cc43962015-09-04 15:35:44 +0200696 case ACPI_RESOURCE_TYPE_GPIO:
697 gpio = &ares->data.gpio;
698 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
699 dev->irq_polarity = gpio->polarity;
700 break;
701
702 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
Frederic Danisae056902015-08-11 16:35:37 +0200703 sb = &ares->data.uart_serial_bus;
Marcel Holtmann74183a12017-08-16 09:53:30 +0200704 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
Frederic Danisae056902015-08-11 16:35:37 +0200705 dev->init_speed = sb->default_baud_rate;
Marcel Holtmann74183a12017-08-16 09:53:30 +0200706 dev->oper_speed = 4000000;
707 }
Frederic Danis6cc43962015-09-04 15:35:44 +0200708 break;
709
710 default:
711 break;
Frederic Danisae056902015-08-11 16:35:37 +0200712 }
713
714 /* Always tell the ACPI core to skip this resource */
715 return 1;
716}
Andy Shevchenko212d71832017-03-10 14:28:20 +0200717#endif /* CONFIG_ACPI */
Frederic Danisae056902015-08-11 16:35:37 +0200718
Andy Shevchenko212d71832017-03-10 14:28:20 +0200719static int bcm_platform_probe(struct bcm_device *dev)
Frederic Danis0395ffc2015-08-11 16:35:35 +0200720{
721 struct platform_device *pdev = dev->pdev;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200722
Frederic Danis0395ffc2015-08-11 16:35:35 +0200723 dev->name = dev_name(&pdev->dev);
Daniel Drake89ab37b2017-01-05 11:10:54 -0600724
Frederic Danis0395ffc2015-08-11 16:35:35 +0200725 dev->clk = devm_clk_get(&pdev->dev, NULL);
726
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200727 dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
728 "device-wakeup",
729 GPIOD_OUT_LOW);
730 if (IS_ERR(dev->device_wakeup))
731 return PTR_ERR(dev->device_wakeup);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200732
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200733 dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
734 GPIOD_OUT_LOW);
735 if (IS_ERR(dev->shutdown))
736 return PTR_ERR(dev->shutdown);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200737
Frederic Danis6cc43962015-09-04 15:35:44 +0200738 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
739 dev->irq = platform_get_irq(pdev, 0);
740 if (dev->irq <= 0) {
741 struct gpio_desc *gpio;
742
743 gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup",
744 GPIOD_IN);
745 if (IS_ERR(gpio))
746 return PTR_ERR(gpio);
747
748 dev->irq = gpiod_to_irq(gpio);
749 }
750
751 dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq);
752
Frederic Danis0395ffc2015-08-11 16:35:35 +0200753 /* Make sure at-least one of the GPIO is defined and that
754 * a name is specified for this instance
755 */
756 if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
757 dev_err(&pdev->dev, "invalid platform data\n");
758 return -EINVAL;
759 }
760
Andy Shevchenko212d71832017-03-10 14:28:20 +0200761 return 0;
762}
763
764#ifdef CONFIG_ACPI
765static int bcm_acpi_probe(struct bcm_device *dev)
766{
767 struct platform_device *pdev = dev->pdev;
768 LIST_HEAD(resources);
769 const struct dmi_system_id *dmi_id;
770 const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
771 const struct acpi_device_id *id;
772 int ret;
773
774 /* Retrieve GPIO data */
775 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
776 if (id)
777 gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data;
778
Andy Shevchenkofda70572017-06-06 17:47:17 +0300779 ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, gpio_mapping);
Andy Shevchenko212d71832017-03-10 14:28:20 +0200780 if (ret)
781 return ret;
782
783 ret = bcm_platform_probe(dev);
784 if (ret)
785 return ret;
786
Frederic Danisae056902015-08-11 16:35:37 +0200787 /* Retrieve UART ACPI info */
Jarkko Nikulae98d6d62015-09-30 16:26:45 +0300788 ret = acpi_dev_get_resources(ACPI_COMPANION(&dev->pdev->dev),
789 &resources, bcm_resource, dev);
Jarkko Nikula5be00282015-09-30 16:26:42 +0300790 if (ret < 0)
791 return ret;
Jarkko Nikula09dbf1b2015-09-30 16:26:41 +0300792 acpi_dev_free_resource_list(&resources);
Frederic Danisae056902015-08-11 16:35:37 +0200793
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200794 dmi_id = dmi_first_match(bcm_wrong_irq_dmi_table);
795 if (dmi_id) {
796 bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low",
797 dmi_id->ident);
798 dev->irq_polarity = *(u8 *)dmi_id->driver_data;
799 }
800
Frederic Danis0395ffc2015-08-11 16:35:35 +0200801 return 0;
802}
Frederic Danis50d78bc2015-08-12 12:46:01 +0200803#else
804static int bcm_acpi_probe(struct bcm_device *dev)
805{
806 return -EINVAL;
807}
808#endif /* CONFIG_ACPI */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200809
810static int bcm_probe(struct platform_device *pdev)
811{
812 struct bcm_device *dev;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200813 int ret;
814
815 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
816 if (!dev)
817 return -ENOMEM;
818
819 dev->pdev = pdev;
820
Andy Shevchenko212d71832017-03-10 14:28:20 +0200821 if (has_acpi_companion(&pdev->dev))
822 ret = bcm_acpi_probe(dev);
823 else
824 ret = bcm_platform_probe(dev);
Jarkko Nikula4d1c4552015-09-30 16:26:44 +0300825 if (ret)
826 return ret;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200827
828 platform_set_drvdata(pdev, dev);
829
830 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
831
832 /* Place this instance on the device list */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200833 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200834 list_add_tail(&dev->list, &bcm_device_list);
Frederic Danisbb3ea162015-09-01 12:13:35 +0200835 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200836
837 bcm_gpio_set_power(dev, false);
838
839 return 0;
840}
841
842static int bcm_remove(struct platform_device *pdev)
843{
844 struct bcm_device *dev = platform_get_drvdata(pdev);
845
Frederic Danisbb3ea162015-09-01 12:13:35 +0200846 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200847 list_del(&dev->list);
Frederic Danisbb3ea162015-09-01 12:13:35 +0200848 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200849
Frederic Danis0395ffc2015-08-11 16:35:35 +0200850 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
851
852 return 0;
853}
854
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700855static const struct hci_uart_proto bcm_proto = {
856 .id = HCI_UART_BCM,
Loic Poulain143f0a22016-09-19 12:05:12 +0200857 .name = "Broadcom",
Marcel Holtmannaee61f72015-10-20 21:30:45 +0200858 .manufacturer = 15,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200859 .init_speed = 115200,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700860 .open = bcm_open,
861 .close = bcm_close,
862 .flush = bcm_flush,
863 .setup = bcm_setup,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200864 .set_baudrate = bcm_set_baudrate,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700865 .recv = bcm_recv,
866 .enqueue = bcm_enqueue,
867 .dequeue = bcm_dequeue,
868};
869
Frederic Danis0395ffc2015-08-11 16:35:35 +0200870#ifdef CONFIG_ACPI
871static const struct acpi_device_id bcm_acpi_match[] = {
Daniel Drake89ab37b2017-01-05 11:10:54 -0600872 { "BCM2E1A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
873 { "BCM2E39", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
874 { "BCM2E3A", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
875 { "BCM2E3D", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
876 { "BCM2E3F", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
877 { "BCM2E40", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
878 { "BCM2E54", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
879 { "BCM2E55", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
880 { "BCM2E64", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
881 { "BCM2E65", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
882 { "BCM2E67", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
883 { "BCM2E71", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
884 { "BCM2E7B", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
885 { "BCM2E7C", (kernel_ulong_t)&acpi_bcm_int_last_gpios },
886 { "BCM2E95", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
887 { "BCM2E96", (kernel_ulong_t)&acpi_bcm_int_first_gpios },
Frederic Danis0395ffc2015-08-11 16:35:35 +0200888 { },
889};
890MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
891#endif
892
Frederic Danis118612f2015-08-11 16:35:38 +0200893/* Platform suspend and resume callbacks */
Frederic Danise88ab302015-09-23 18:18:11 +0200894static const struct dev_pm_ops bcm_pm_ops = {
895 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
896 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
897};
Frederic Danis118612f2015-08-11 16:35:38 +0200898
Frederic Danis0395ffc2015-08-11 16:35:35 +0200899static struct platform_driver bcm_driver = {
900 .probe = bcm_probe,
901 .remove = bcm_remove,
902 .driver = {
903 .name = "hci_bcm",
904 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
Frederic Danis118612f2015-08-11 16:35:38 +0200905 .pm = &bcm_pm_ops,
Frederic Danis0395ffc2015-08-11 16:35:35 +0200906 },
907};
908
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700909int __init bcm_init(void)
910{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200911 platform_driver_register(&bcm_driver);
912
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700913 return hci_uart_register_proto(&bcm_proto);
914}
915
916int __exit bcm_deinit(void)
917{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200918 platform_driver_unregister(&bcm_driver);
919
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700920 return hci_uart_unregister_proto(&bcm_proto);
921}