blob: 5375c9c04fdae9db9d71801a772efd8449d902a4 [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
Frederic Danise88ab302015-09-23 18:18:11 +020044#define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
45
Frederic Danis0395ffc2015-08-11 16:35:35 +020046struct bcm_device {
47 struct list_head list;
48
49 struct platform_device *pdev;
50
51 const char *name;
52 struct gpio_desc *device_wakeup;
53 struct gpio_desc *shutdown;
54
55 struct clk *clk;
56 bool clk_enabled;
Frederic Danisae056902015-08-11 16:35:37 +020057
58 u32 init_speed;
Frederic Danis6cc43962015-09-04 15:35:44 +020059 int irq;
60 u8 irq_polarity;
Frederic Danis118612f2015-08-11 16:35:38 +020061
Frederic Danisb7a622a2015-09-23 18:18:09 +020062#ifdef CONFIG_PM
Frederic Danis118612f2015-08-11 16:35:38 +020063 struct hci_uart *hu;
64 bool is_suspended; /* suspend/resume flag */
65#endif
Marcel Holtmannbdd88182015-04-05 22:52:18 -070066};
67
Frederic Danis0395ffc2015-08-11 16:35:35 +020068struct bcm_data {
69 struct sk_buff *rx_skb;
70 struct sk_buff_head txq;
71
72 struct bcm_device *dev;
73};
74
75/* List of BCM BT UART devices */
Frederic Danisbb3ea162015-09-01 12:13:35 +020076static DEFINE_MUTEX(bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +020077static LIST_HEAD(bcm_device_list);
78
Frederic Danis61b2fc22015-06-09 16:15:37 +020079static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
80{
81 struct hci_dev *hdev = hu->hdev;
82 struct sk_buff *skb;
83 struct bcm_update_uart_baud_rate param;
84
85 if (speed > 3000000) {
86 struct bcm_write_uart_clock_setting clock;
87
88 clock.type = BCM_UART_CLOCK_48MHZ;
89
Frederic Danis65ad07c2015-09-01 12:13:36 +020090 bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
Frederic Danis61b2fc22015-06-09 16:15:37 +020091
92 /* This Broadcom specific command changes the UART's controller
93 * clock for baud rate > 3000000.
94 */
95 skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
96 if (IS_ERR(skb)) {
97 int err = PTR_ERR(skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +020098 bt_dev_err(hdev, "BCM: failed to write clock (%d)",
99 err);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200100 return err;
101 }
102
103 kfree_skb(skb);
104 }
105
Frederic Danis65ad07c2015-09-01 12:13:36 +0200106 bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200107
108 param.zero = cpu_to_le16(0);
109 param.baud_rate = cpu_to_le32(speed);
110
111 /* This Broadcom specific command changes the UART's controller baud
112 * rate.
113 */
114 skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
115 HCI_INIT_TIMEOUT);
116 if (IS_ERR(skb)) {
117 int err = PTR_ERR(skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200118 bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
119 err);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200120 return err;
121 }
122
123 kfree_skb(skb);
124
125 return 0;
126}
127
Frederic Danis917522a2015-08-28 15:44:00 +0200128/* bcm_device_exists should be protected by bcm_device_lock */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200129static bool bcm_device_exists(struct bcm_device *device)
130{
131 struct list_head *p;
132
133 list_for_each(p, &bcm_device_list) {
134 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
135
136 if (device == dev)
137 return true;
138 }
139
140 return false;
141}
142
143static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
144{
145 if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
146 clk_enable(dev->clk);
147
Loic Poulain2af0a702015-08-17 16:00:21 +0200148 gpiod_set_value(dev->shutdown, powered);
149 gpiod_set_value(dev->device_wakeup, powered);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200150
151 if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
152 clk_disable(dev->clk);
153
154 dev->clk_enabled = powered;
155
156 return 0;
157}
158
Frederic Danisb7a622a2015-09-23 18:18:09 +0200159#ifdef CONFIG_PM
Frederic Danis6cc43962015-09-04 15:35:44 +0200160static irqreturn_t bcm_host_wake(int irq, void *data)
161{
162 struct bcm_device *bdev = data;
163
164 bt_dev_dbg(bdev, "Host wake IRQ");
165
Frederic Danise88ab302015-09-23 18:18:11 +0200166 pm_runtime_get(&bdev->pdev->dev);
167 pm_runtime_mark_last_busy(&bdev->pdev->dev);
168 pm_runtime_put_autosuspend(&bdev->pdev->dev);
169
Frederic Danis6cc43962015-09-04 15:35:44 +0200170 return IRQ_HANDLED;
171}
172
173static int bcm_request_irq(struct bcm_data *bcm)
174{
175 struct bcm_device *bdev = bcm->dev;
176 int err = 0;
177
178 /* If this is not a platform device, do not enable PM functionalities */
179 mutex_lock(&bcm_device_lock);
180 if (!bcm_device_exists(bdev)) {
181 err = -ENODEV;
182 goto unlock;
183 }
184
185 if (bdev->irq > 0) {
186 err = devm_request_irq(&bdev->pdev->dev, bdev->irq,
187 bcm_host_wake, IRQF_TRIGGER_RISING,
188 "host_wake", bdev);
189 if (err)
190 goto unlock;
191
192 device_init_wakeup(&bdev->pdev->dev, true);
Frederic Danise88ab302015-09-23 18:18:11 +0200193
194 pm_runtime_set_autosuspend_delay(&bdev->pdev->dev,
195 BCM_AUTOSUSPEND_DELAY);
196 pm_runtime_use_autosuspend(&bdev->pdev->dev);
197 pm_runtime_set_active(&bdev->pdev->dev);
198 pm_runtime_enable(&bdev->pdev->dev);
Frederic Danis6cc43962015-09-04 15:35:44 +0200199 }
200
201unlock:
202 mutex_unlock(&bcm_device_lock);
203
204 return err;
205}
206
207static const struct bcm_set_sleep_mode default_sleep_params = {
208 .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
209 .idle_host = 2, /* idle threshold HOST, in 300ms */
210 .idle_dev = 2, /* idle threshold device, in 300ms */
211 .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
212 .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
213 .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
Frederic Danise88ab302015-09-23 18:18:11 +0200214 .combine_modes = 1, /* Combine sleep and LPM flag */
Frederic Danis6cc43962015-09-04 15:35:44 +0200215 .tristate_control = 0, /* Allow tri-state control of UART tx flag */
216 /* Irrelevant USB flags */
217 .usb_auto_sleep = 0,
218 .usb_resume_timeout = 0,
219 .pulsed_host_wake = 0,
220 .break_to_host = 0
221};
222
223static int bcm_setup_sleep(struct hci_uart *hu)
224{
225 struct bcm_data *bcm = hu->priv;
226 struct sk_buff *skb;
227 struct bcm_set_sleep_mode sleep_params = default_sleep_params;
228
229 sleep_params.host_wake_active = !bcm->dev->irq_polarity;
230
231 skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
232 &sleep_params, HCI_INIT_TIMEOUT);
233 if (IS_ERR(skb)) {
234 int err = PTR_ERR(skb);
235 bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
236 return err;
237 }
238 kfree_skb(skb);
239
240 bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
241
242 return 0;
243}
244#else
245static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
246static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
247#endif
248
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700249static int bcm_open(struct hci_uart *hu)
250{
251 struct bcm_data *bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200252 struct list_head *p;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700253
Frederic Danis65ad07c2015-09-01 12:13:36 +0200254 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700255
256 bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
257 if (!bcm)
258 return -ENOMEM;
259
260 skb_queue_head_init(&bcm->txq);
261
262 hu->priv = bcm;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200263
Frederic Danisbb3ea162015-09-01 12:13:35 +0200264 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200265 list_for_each(p, &bcm_device_list) {
266 struct bcm_device *dev = list_entry(p, struct bcm_device, list);
267
268 /* Retrieve saved bcm_device based on parent of the
269 * platform device (saved during device probe) and
270 * parent of tty device used by hci_uart
271 */
272 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
273 bcm->dev = dev;
Frederic Danisae056902015-08-11 16:35:37 +0200274 hu->init_speed = dev->init_speed;
Frederic Danisb7a622a2015-09-23 18:18:09 +0200275#ifdef CONFIG_PM
Frederic Danis118612f2015-08-11 16:35:38 +0200276 dev->hu = hu;
277#endif
Frederic Danis6cc43962015-09-04 15:35:44 +0200278 bcm_gpio_set_power(bcm->dev, true);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200279 break;
280 }
281 }
282
Frederic Danisbb3ea162015-09-01 12:13:35 +0200283 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200284
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700285 return 0;
286}
287
288static int bcm_close(struct hci_uart *hu)
289{
290 struct bcm_data *bcm = hu->priv;
Frederic Danis6cc43962015-09-04 15:35:44 +0200291 struct bcm_device *bdev = bcm->dev;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700292
Frederic Danis65ad07c2015-09-01 12:13:36 +0200293 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700294
Frederic Danis0395ffc2015-08-11 16:35:35 +0200295 /* Protect bcm->dev against removal of the device or driver */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200296 mutex_lock(&bcm_device_lock);
Frederic Danis6cc43962015-09-04 15:35:44 +0200297 if (bcm_device_exists(bdev)) {
298 bcm_gpio_set_power(bdev, false);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200299#ifdef CONFIG_PM
Frederic Danise88ab302015-09-23 18:18:11 +0200300 pm_runtime_disable(&bdev->pdev->dev);
301 pm_runtime_set_suspended(&bdev->pdev->dev);
302
Frederic Danis6cc43962015-09-04 15:35:44 +0200303 if (device_can_wakeup(&bdev->pdev->dev)) {
304 devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev);
305 device_init_wakeup(&bdev->pdev->dev, false);
306 }
307
308 bdev->hu = NULL;
Frederic Danis118612f2015-08-11 16:35:38 +0200309#endif
310 }
Frederic Danisbb3ea162015-09-01 12:13:35 +0200311 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200312
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700313 skb_queue_purge(&bcm->txq);
314 kfree_skb(bcm->rx_skb);
315 kfree(bcm);
316
317 hu->priv = NULL;
318 return 0;
319}
320
321static int bcm_flush(struct hci_uart *hu)
322{
323 struct bcm_data *bcm = hu->priv;
324
Frederic Danis65ad07c2015-09-01 12:13:36 +0200325 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700326
327 skb_queue_purge(&bcm->txq);
328
329 return 0;
330}
331
332static int bcm_setup(struct hci_uart *hu)
333{
Frederic Danis6cc43962015-09-04 15:35:44 +0200334 struct bcm_data *bcm = hu->priv;
Frederic Danis6be09b42015-05-28 11:25:05 +0200335 char fw_name[64];
336 const struct firmware *fw;
Frederic Danis960ef1d2015-06-18 12:43:27 +0200337 unsigned int speed;
Frederic Danis6be09b42015-05-28 11:25:05 +0200338 int err;
339
Frederic Danis65ad07c2015-09-01 12:13:36 +0200340 bt_dev_dbg(hu->hdev, "hu %p", hu);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700341
342 hu->hdev->set_bdaddr = btbcm_set_bdaddr;
343
Frederic Danis6be09b42015-05-28 11:25:05 +0200344 err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
345 if (err)
346 return err;
347
348 err = request_firmware(&fw, fw_name, &hu->hdev->dev);
349 if (err < 0) {
Frederic Danis65ad07c2015-09-01 12:13:36 +0200350 bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
Frederic Danis6be09b42015-05-28 11:25:05 +0200351 return 0;
352 }
353
354 err = btbcm_patchram(hu->hdev, fw);
355 if (err) {
Frederic Danis65ad07c2015-09-01 12:13:36 +0200356 bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
Frederic Danis6be09b42015-05-28 11:25:05 +0200357 goto finalize;
358 }
359
Frederic Danis960ef1d2015-06-18 12:43:27 +0200360 /* Init speed if any */
361 if (hu->init_speed)
362 speed = hu->init_speed;
363 else if (hu->proto->init_speed)
364 speed = hu->proto->init_speed;
365 else
366 speed = 0;
Frederic Danis6be09b42015-05-28 11:25:05 +0200367
Frederic Danis960ef1d2015-06-18 12:43:27 +0200368 if (speed)
369 hci_uart_set_baudrate(hu, speed);
370
371 /* Operational speed if any */
372 if (hu->oper_speed)
373 speed = hu->oper_speed;
374 else if (hu->proto->oper_speed)
375 speed = hu->proto->oper_speed;
376 else
377 speed = 0;
378
379 if (speed) {
380 err = bcm_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200381 if (!err)
Frederic Danis960ef1d2015-06-18 12:43:27 +0200382 hci_uart_set_baudrate(hu, speed);
Frederic Danis61b2fc22015-06-09 16:15:37 +0200383 }
384
Frederic Danis6be09b42015-05-28 11:25:05 +0200385finalize:
386 release_firmware(fw);
387
388 err = btbcm_finalize(hu->hdev);
Frederic Danis6cc43962015-09-04 15:35:44 +0200389 if (err)
390 return err;
391
392 err = bcm_request_irq(bcm);
393 if (!err)
394 err = bcm_setup_sleep(hu);
Frederic Danis6be09b42015-05-28 11:25:05 +0200395
396 return err;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700397}
398
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700399static const struct h4_recv_pkt bcm_recv_pkts[] = {
400 { H4_RECV_ACL, .recv = hci_recv_frame },
401 { H4_RECV_SCO, .recv = hci_recv_frame },
402 { H4_RECV_EVENT, .recv = hci_recv_frame },
403};
404
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700405static int bcm_recv(struct hci_uart *hu, const void *data, int count)
406{
407 struct bcm_data *bcm = hu->priv;
408
409 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
410 return -EUNATCH;
411
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700412 bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
413 bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700414 if (IS_ERR(bcm->rx_skb)) {
415 int err = PTR_ERR(bcm->rx_skb);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200416 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Chan-yeol Park37134162015-06-17 21:10:39 +0900417 bcm->rx_skb = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700418 return err;
Frederic Danise88ab302015-09-23 18:18:11 +0200419 } else if (!bcm->rx_skb) {
420 /* Delay auto-suspend when receiving completed packet */
421 mutex_lock(&bcm_device_lock);
422 if (bcm->dev && bcm_device_exists(bcm->dev)) {
423 pm_runtime_get(&bcm->dev->pdev->dev);
424 pm_runtime_mark_last_busy(&bcm->dev->pdev->dev);
425 pm_runtime_put_autosuspend(&bcm->dev->pdev->dev);
426 }
427 mutex_unlock(&bcm_device_lock);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700428 }
429
430 return count;
431}
432
433static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
434{
435 struct bcm_data *bcm = hu->priv;
436
Frederic Danis65ad07c2015-09-01 12:13:36 +0200437 bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700438
439 /* Prepend skb with frame type */
440 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
441 skb_queue_tail(&bcm->txq, skb);
442
443 return 0;
444}
445
446static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
447{
448 struct bcm_data *bcm = hu->priv;
Frederic Danise88ab302015-09-23 18:18:11 +0200449 struct sk_buff *skb = NULL;
450 struct bcm_device *bdev = NULL;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700451
Frederic Danise88ab302015-09-23 18:18:11 +0200452 mutex_lock(&bcm_device_lock);
453
454 if (bcm_device_exists(bcm->dev)) {
455 bdev = bcm->dev;
456 pm_runtime_get_sync(&bdev->pdev->dev);
457 /* Shall be resumed here */
458 }
459
460 skb = skb_dequeue(&bcm->txq);
461
462 if (bdev) {
463 pm_runtime_mark_last_busy(&bdev->pdev->dev);
464 pm_runtime_put_autosuspend(&bdev->pdev->dev);
465 }
466
467 mutex_unlock(&bcm_device_lock);
468
469 return skb;
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700470}
471
Frederic Danisb7a622a2015-09-23 18:18:09 +0200472#ifdef CONFIG_PM
473static int bcm_suspend_device(struct device *dev)
Frederic Danis118612f2015-08-11 16:35:38 +0200474{
475 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
476
Frederic Danisb7a622a2015-09-23 18:18:09 +0200477 bt_dev_dbg(bdev, "");
Frederic Danis118612f2015-08-11 16:35:38 +0200478
Frederic Danisb7a622a2015-09-23 18:18:09 +0200479 if (!bdev->is_suspended && bdev->hu) {
Frederic Danis118612f2015-08-11 16:35:38 +0200480 hci_uart_set_flow_control(bdev->hu, true);
481
Frederic Danisb7a622a2015-09-23 18:18:09 +0200482 /* Once this returns, driver suspends BT via GPIO */
Frederic Danis118612f2015-08-11 16:35:38 +0200483 bdev->is_suspended = true;
484 }
485
486 /* Suspend the device */
487 if (bdev->device_wakeup) {
488 gpiod_set_value(bdev->device_wakeup, false);
Frederic Danis65ad07c2015-09-01 12:13:36 +0200489 bt_dev_dbg(bdev, "suspend, delaying 15 ms");
Frederic Danis118612f2015-08-11 16:35:38 +0200490 mdelay(15);
491 }
492
Frederic Danisb7a622a2015-09-23 18:18:09 +0200493 return 0;
494}
495
496static int bcm_resume_device(struct device *dev)
497{
498 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
499
500 bt_dev_dbg(bdev, "");
501
502 if (bdev->device_wakeup) {
503 gpiod_set_value(bdev->device_wakeup, true);
504 bt_dev_dbg(bdev, "resume, delaying 15 ms");
505 mdelay(15);
506 }
507
508 /* When this executes, the device has woken up already */
509 if (bdev->is_suspended && bdev->hu) {
510 bdev->is_suspended = false;
511
512 hci_uart_set_flow_control(bdev->hu, false);
513 }
514
515 return 0;
516}
517#endif
518
519#ifdef CONFIG_PM_SLEEP
520/* Platform suspend callback */
521static int bcm_suspend(struct device *dev)
522{
523 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
524 int error;
525
526 bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
527
528 /* bcm_suspend can be called at any time as long as platform device is
529 * bound, so it should use bcm_device_lock to protect access to hci_uart
530 * and device_wake-up GPIO.
531 */
532 mutex_lock(&bcm_device_lock);
533
534 if (!bdev->hu)
535 goto unlock;
536
Frederic Danise88ab302015-09-23 18:18:11 +0200537 if (pm_runtime_active(dev))
538 bcm_suspend_device(dev);
Frederic Danisb7a622a2015-09-23 18:18:09 +0200539
Frederic Danis6cc43962015-09-04 15:35:44 +0200540 if (device_may_wakeup(&bdev->pdev->dev)) {
541 error = enable_irq_wake(bdev->irq);
542 if (!error)
543 bt_dev_dbg(bdev, "BCM irq: enabled");
544 }
545
Frederic Danis917522a2015-08-28 15:44:00 +0200546unlock:
Frederic Danisbb3ea162015-09-01 12:13:35 +0200547 mutex_unlock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200548
Frederic Danis118612f2015-08-11 16:35:38 +0200549 return 0;
550}
551
552/* Platform resume callback */
553static int bcm_resume(struct device *dev)
554{
555 struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
556
Frederic Danis65ad07c2015-09-01 12:13:36 +0200557 bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
Frederic Danis118612f2015-08-11 16:35:38 +0200558
Frederic Danisb7a622a2015-09-23 18:18:09 +0200559 /* bcm_resume can be called at any time as long as platform device is
560 * bound, so it should use bcm_device_lock to protect access to hci_uart
561 * and device_wake-up GPIO.
562 */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200563 mutex_lock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200564
565 if (!bdev->hu)
566 goto unlock;
567
Frederic Danis6cc43962015-09-04 15:35:44 +0200568 if (device_may_wakeup(&bdev->pdev->dev)) {
569 disable_irq_wake(bdev->irq);
570 bt_dev_dbg(bdev, "BCM irq: disabled");
571 }
572
Frederic Danisb7a622a2015-09-23 18:18:09 +0200573 bcm_resume_device(dev);
Frederic Danis118612f2015-08-11 16:35:38 +0200574
Frederic Danis917522a2015-08-28 15:44:00 +0200575unlock:
Frederic Danisbb3ea162015-09-01 12:13:35 +0200576 mutex_unlock(&bcm_device_lock);
Frederic Danis917522a2015-08-28 15:44:00 +0200577
Frederic Danise88ab302015-09-23 18:18:11 +0200578 pm_runtime_disable(dev);
579 pm_runtime_set_active(dev);
580 pm_runtime_enable(dev);
581
Frederic Danis118612f2015-08-11 16:35:38 +0200582 return 0;
583}
584#endif
585
Frederic Danis0395ffc2015-08-11 16:35:35 +0200586static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
587static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
Frederic Danis6cc43962015-09-04 15:35:44 +0200588static const struct acpi_gpio_params host_wakeup_gpios = { 2, 0, false };
Frederic Danis0395ffc2015-08-11 16:35:35 +0200589
590static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
591 { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
592 { "shutdown-gpios", &shutdown_gpios, 1 },
Frederic Danis6cc43962015-09-04 15:35:44 +0200593 { "host-wakeup-gpios", &host_wakeup_gpios, 1 },
Frederic Danis0395ffc2015-08-11 16:35:35 +0200594 { },
595};
596
Frederic Danis50d78bc2015-08-12 12:46:01 +0200597#ifdef CONFIG_ACPI
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200598static u8 acpi_active_low = ACPI_ACTIVE_LOW;
599
600/* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
601static const struct dmi_system_id bcm_wrong_irq_dmi_table[] = {
602 {
603 .ident = "Asus T100TA",
604 .matches = {
605 DMI_EXACT_MATCH(DMI_SYS_VENDOR,
606 "ASUSTeK COMPUTER INC."),
607 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
608 },
609 .driver_data = &acpi_active_low,
610 },
611 { }
612};
613
Frederic Danisae056902015-08-11 16:35:37 +0200614static int bcm_resource(struct acpi_resource *ares, void *data)
615{
616 struct bcm_device *dev = data;
Frederic Danis6cc43962015-09-04 15:35:44 +0200617 struct acpi_resource_extended_irq *irq;
618 struct acpi_resource_gpio *gpio;
619 struct acpi_resource_uart_serialbus *sb;
Frederic Danisae056902015-08-11 16:35:37 +0200620
Frederic Danis6cc43962015-09-04 15:35:44 +0200621 switch (ares->type) {
622 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
623 irq = &ares->data.extended_irq;
624 dev->irq_polarity = irq->polarity;
625 break;
Frederic Danisae056902015-08-11 16:35:37 +0200626
Frederic Danis6cc43962015-09-04 15:35:44 +0200627 case ACPI_RESOURCE_TYPE_GPIO:
628 gpio = &ares->data.gpio;
629 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
630 dev->irq_polarity = gpio->polarity;
631 break;
632
633 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
Frederic Danisae056902015-08-11 16:35:37 +0200634 sb = &ares->data.uart_serial_bus;
635 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
636 dev->init_speed = sb->default_baud_rate;
Frederic Danis6cc43962015-09-04 15:35:44 +0200637 break;
638
639 default:
640 break;
Frederic Danisae056902015-08-11 16:35:37 +0200641 }
642
643 /* Always tell the ACPI core to skip this resource */
644 return 1;
645}
646
Frederic Danis0395ffc2015-08-11 16:35:35 +0200647static int bcm_acpi_probe(struct bcm_device *dev)
648{
649 struct platform_device *pdev = dev->pdev;
650 const struct acpi_device_id *id;
Frederic Danisae056902015-08-11 16:35:37 +0200651 struct acpi_device *adev;
652 LIST_HEAD(resources);
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200653 const struct dmi_system_id *dmi_id;
Frederic Danis0395ffc2015-08-11 16:35:35 +0200654 int ret;
655
656 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
657 if (!id)
658 return -ENODEV;
659
660 /* Retrieve GPIO data */
661 dev->name = dev_name(&pdev->dev);
662 ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
663 acpi_bcm_default_gpios);
664 if (ret)
665 return ret;
666
667 dev->clk = devm_clk_get(&pdev->dev, NULL);
668
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200669 dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
670 "device-wakeup",
671 GPIOD_OUT_LOW);
672 if (IS_ERR(dev->device_wakeup))
673 return PTR_ERR(dev->device_wakeup);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200674
Uwe Kleine-König62aaefa2015-08-12 09:20:56 +0200675 dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
676 GPIOD_OUT_LOW);
677 if (IS_ERR(dev->shutdown))
678 return PTR_ERR(dev->shutdown);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200679
Frederic Danis6cc43962015-09-04 15:35:44 +0200680 /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
681 dev->irq = platform_get_irq(pdev, 0);
682 if (dev->irq <= 0) {
683 struct gpio_desc *gpio;
684
685 gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup",
686 GPIOD_IN);
687 if (IS_ERR(gpio))
688 return PTR_ERR(gpio);
689
690 dev->irq = gpiod_to_irq(gpio);
691 }
692
693 dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq);
694
Frederic Danis0395ffc2015-08-11 16:35:35 +0200695 /* Make sure at-least one of the GPIO is defined and that
696 * a name is specified for this instance
697 */
698 if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
699 dev_err(&pdev->dev, "invalid platform data\n");
700 return -EINVAL;
701 }
702
Frederic Danisae056902015-08-11 16:35:37 +0200703 /* Retrieve UART ACPI info */
704 adev = ACPI_COMPANION(&dev->pdev->dev);
705 if (!adev)
706 return 0;
707
Jarkko Nikula5be00282015-09-30 16:26:42 +0300708 ret = acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
709 if (ret < 0)
710 return ret;
Jarkko Nikula09dbf1b2015-09-30 16:26:41 +0300711 acpi_dev_free_resource_list(&resources);
Frederic Danisae056902015-08-11 16:35:37 +0200712
Frederic Danis5cebdfe2015-09-23 18:18:08 +0200713 dmi_id = dmi_first_match(bcm_wrong_irq_dmi_table);
714 if (dmi_id) {
715 bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low",
716 dmi_id->ident);
717 dev->irq_polarity = *(u8 *)dmi_id->driver_data;
718 }
719
Frederic Danis0395ffc2015-08-11 16:35:35 +0200720 return 0;
721}
Frederic Danis50d78bc2015-08-12 12:46:01 +0200722#else
723static int bcm_acpi_probe(struct bcm_device *dev)
724{
725 return -EINVAL;
726}
727#endif /* CONFIG_ACPI */
Frederic Danis0395ffc2015-08-11 16:35:35 +0200728
729static int bcm_probe(struct platform_device *pdev)
730{
731 struct bcm_device *dev;
732 struct acpi_device_id *pdata = pdev->dev.platform_data;
733 int ret;
734
735 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
736 if (!dev)
737 return -ENOMEM;
738
739 dev->pdev = pdev;
740
741 if (ACPI_HANDLE(&pdev->dev)) {
742 ret = bcm_acpi_probe(dev);
743 if (ret)
744 return ret;
745 } else if (pdata) {
746 dev->name = pdata->id;
747 } else {
748 return -ENODEV;
749 }
750
751 platform_set_drvdata(pdev, dev);
752
753 dev_info(&pdev->dev, "%s device registered.\n", dev->name);
754
755 /* Place this instance on the device list */
Frederic Danisbb3ea162015-09-01 12:13:35 +0200756 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200757 list_add_tail(&dev->list, &bcm_device_list);
Frederic Danisbb3ea162015-09-01 12:13:35 +0200758 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200759
760 bcm_gpio_set_power(dev, false);
761
762 return 0;
763}
764
765static int bcm_remove(struct platform_device *pdev)
766{
767 struct bcm_device *dev = platform_get_drvdata(pdev);
768
Frederic Danisbb3ea162015-09-01 12:13:35 +0200769 mutex_lock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200770 list_del(&dev->list);
Frederic Danisbb3ea162015-09-01 12:13:35 +0200771 mutex_unlock(&bcm_device_lock);
Frederic Danis0395ffc2015-08-11 16:35:35 +0200772
773 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
774
775 dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
776
777 return 0;
778}
779
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700780static const struct hci_uart_proto bcm_proto = {
781 .id = HCI_UART_BCM,
782 .name = "BCM",
Frederic Danis61b2fc22015-06-09 16:15:37 +0200783 .init_speed = 115200,
784 .oper_speed = 4000000,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700785 .open = bcm_open,
786 .close = bcm_close,
787 .flush = bcm_flush,
788 .setup = bcm_setup,
Frederic Danis61b2fc22015-06-09 16:15:37 +0200789 .set_baudrate = bcm_set_baudrate,
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700790 .recv = bcm_recv,
791 .enqueue = bcm_enqueue,
792 .dequeue = bcm_dequeue,
793};
794
Frederic Danis0395ffc2015-08-11 16:35:35 +0200795#ifdef CONFIG_ACPI
796static const struct acpi_device_id bcm_acpi_match[] = {
797 { "BCM2E39", 0 },
798 { "BCM2E67", 0 },
799 { },
800};
801MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
802#endif
803
Frederic Danis118612f2015-08-11 16:35:38 +0200804/* Platform suspend and resume callbacks */
Frederic Danise88ab302015-09-23 18:18:11 +0200805static const struct dev_pm_ops bcm_pm_ops = {
806 SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
807 SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
808};
Frederic Danis118612f2015-08-11 16:35:38 +0200809
Frederic Danis0395ffc2015-08-11 16:35:35 +0200810static struct platform_driver bcm_driver = {
811 .probe = bcm_probe,
812 .remove = bcm_remove,
813 .driver = {
814 .name = "hci_bcm",
815 .acpi_match_table = ACPI_PTR(bcm_acpi_match),
Frederic Danis118612f2015-08-11 16:35:38 +0200816 .pm = &bcm_pm_ops,
Frederic Danis0395ffc2015-08-11 16:35:35 +0200817 },
818};
819
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700820int __init bcm_init(void)
821{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200822 platform_driver_register(&bcm_driver);
823
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700824 return hci_uart_register_proto(&bcm_proto);
825}
826
827int __exit bcm_deinit(void)
828{
Frederic Danis0395ffc2015-08-11 16:35:35 +0200829 platform_driver_unregister(&bcm_driver);
830
Marcel Holtmannbdd88182015-04-05 22:52:18 -0700831 return hci_uart_unregister_proto(&bcm_proto);
832}