]>
Commit | Line | Data |
---|---|---|
e9a2dd26 MH |
1 | /* |
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> | |
18aeb444 | 27 | #include <linux/firmware.h> |
0395ffc1 FD |
28 | #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> | |
6cc4396c | 34 | #include <linux/interrupt.h> |
5cebdfea | 35 | #include <linux/dmi.h> |
e88ab30d | 36 | #include <linux/pm_runtime.h> |
e9a2dd26 MH |
37 | |
38 | #include <net/bluetooth/bluetooth.h> | |
39 | #include <net/bluetooth/hci_core.h> | |
40 | ||
bdd8818e | 41 | #include "btbcm.h" |
e9a2dd26 | 42 | #include "hci_uart.h" |
bdd8818e | 43 | |
e88ab30d FD |
44 | #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */ |
45 | ||
0395ffc1 FD |
46 | struct 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; | |
ae056908 FD |
57 | |
58 | u32 init_speed; | |
6cc4396c FD |
59 | int irq; |
60 | u8 irq_polarity; | |
118612fb | 61 | |
b7a622a2 | 62 | #ifdef CONFIG_PM |
118612fb FD |
63 | struct hci_uart *hu; |
64 | bool is_suspended; /* suspend/resume flag */ | |
65 | #endif | |
0395ffc1 FD |
66 | }; |
67 | ||
bdd8818e | 68 | struct bcm_data { |
0395ffc1 FD |
69 | struct sk_buff *rx_skb; |
70 | struct sk_buff_head txq; | |
71 | ||
72 | struct bcm_device *dev; | |
bdd8818e MH |
73 | }; |
74 | ||
0395ffc1 | 75 | /* List of BCM BT UART devices */ |
bb3ea16a | 76 | static DEFINE_MUTEX(bcm_device_lock); |
0395ffc1 FD |
77 | static LIST_HEAD(bcm_device_list); |
78 | ||
61b2fc2b FD |
79 | static 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 | ||
65ad07c9 | 90 | bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type); |
61b2fc2b FD |
91 | |
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); | |
65ad07c9 FD |
98 | bt_dev_err(hdev, "BCM: failed to write clock (%d)", |
99 | err); | |
61b2fc2b FD |
100 | return err; |
101 | } | |
102 | ||
103 | kfree_skb(skb); | |
104 | } | |
105 | ||
65ad07c9 | 106 | bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed); |
61b2fc2b FD |
107 | |
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), ¶m, | |
115 | HCI_INIT_TIMEOUT); | |
116 | if (IS_ERR(skb)) { | |
117 | int err = PTR_ERR(skb); | |
65ad07c9 FD |
118 | bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)", |
119 | err); | |
61b2fc2b FD |
120 | return err; |
121 | } | |
122 | ||
123 | kfree_skb(skb); | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
917522aa | 128 | /* bcm_device_exists should be protected by bcm_device_lock */ |
0395ffc1 FD |
129 | static 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 | ||
143 | static 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 | ||
2af0a709 LP |
148 | gpiod_set_value(dev->shutdown, powered); |
149 | gpiod_set_value(dev->device_wakeup, powered); | |
0395ffc1 FD |
150 | |
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 | ||
b7a622a2 | 159 | #ifdef CONFIG_PM |
6cc4396c FD |
160 | static 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 | ||
e88ab30d FD |
166 | pm_runtime_get(&bdev->pdev->dev); |
167 | pm_runtime_mark_last_busy(&bdev->pdev->dev); | |
168 | pm_runtime_put_autosuspend(&bdev->pdev->dev); | |
169 | ||
6cc4396c FD |
170 | return IRQ_HANDLED; |
171 | } | |
172 | ||
173 | static 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); | |
e88ab30d FD |
193 | |
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); | |
6cc4396c FD |
199 | } |
200 | ||
201 | unlock: | |
202 | mutex_unlock(&bcm_device_lock); | |
203 | ||
204 | return err; | |
205 | } | |
206 | ||
207 | static 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 */ | |
e88ab30d | 214 | .combine_modes = 1, /* Combine sleep and LPM flag */ |
6cc4396c FD |
215 | .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 | ||
223 | static 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 | |
245 | static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; } | |
246 | static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; } | |
247 | #endif | |
248 | ||
bdd8818e MH |
249 | static int bcm_open(struct hci_uart *hu) |
250 | { | |
251 | struct bcm_data *bcm; | |
0395ffc1 | 252 | struct list_head *p; |
bdd8818e | 253 | |
65ad07c9 | 254 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
bdd8818e MH |
255 | |
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; | |
0395ffc1 | 263 | |
bb3ea16a | 264 | mutex_lock(&bcm_device_lock); |
0395ffc1 FD |
265 | 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; | |
ae056908 | 274 | hu->init_speed = dev->init_speed; |
b7a622a2 | 275 | #ifdef CONFIG_PM |
118612fb FD |
276 | dev->hu = hu; |
277 | #endif | |
6cc4396c | 278 | bcm_gpio_set_power(bcm->dev, true); |
0395ffc1 FD |
279 | break; |
280 | } | |
281 | } | |
282 | ||
bb3ea16a | 283 | mutex_unlock(&bcm_device_lock); |
0395ffc1 | 284 | |
bdd8818e MH |
285 | return 0; |
286 | } | |
287 | ||
288 | static int bcm_close(struct hci_uart *hu) | |
289 | { | |
290 | struct bcm_data *bcm = hu->priv; | |
6cc4396c | 291 | struct bcm_device *bdev = bcm->dev; |
bdd8818e | 292 | |
65ad07c9 | 293 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
bdd8818e | 294 | |
0395ffc1 | 295 | /* Protect bcm->dev against removal of the device or driver */ |
bb3ea16a | 296 | mutex_lock(&bcm_device_lock); |
6cc4396c FD |
297 | if (bcm_device_exists(bdev)) { |
298 | bcm_gpio_set_power(bdev, false); | |
b7a622a2 | 299 | #ifdef CONFIG_PM |
e88ab30d FD |
300 | pm_runtime_disable(&bdev->pdev->dev); |
301 | pm_runtime_set_suspended(&bdev->pdev->dev); | |
302 | ||
6cc4396c FD |
303 | 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; | |
118612fb FD |
309 | #endif |
310 | } | |
bb3ea16a | 311 | mutex_unlock(&bcm_device_lock); |
0395ffc1 | 312 | |
bdd8818e MH |
313 | skb_queue_purge(&bcm->txq); |
314 | kfree_skb(bcm->rx_skb); | |
315 | kfree(bcm); | |
316 | ||
317 | hu->priv = NULL; | |
318 | return 0; | |
319 | } | |
320 | ||
321 | static int bcm_flush(struct hci_uart *hu) | |
322 | { | |
323 | struct bcm_data *bcm = hu->priv; | |
324 | ||
65ad07c9 | 325 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
bdd8818e MH |
326 | |
327 | skb_queue_purge(&bcm->txq); | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
332 | static int bcm_setup(struct hci_uart *hu) | |
333 | { | |
6cc4396c | 334 | struct bcm_data *bcm = hu->priv; |
6be09b48 FD |
335 | char fw_name[64]; |
336 | const struct firmware *fw; | |
960ef1d7 | 337 | unsigned int speed; |
6be09b48 FD |
338 | int err; |
339 | ||
65ad07c9 | 340 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
bdd8818e MH |
341 | |
342 | hu->hdev->set_bdaddr = btbcm_set_bdaddr; | |
343 | ||
6be09b48 FD |
344 | 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) { | |
65ad07c9 | 350 | bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name); |
6be09b48 FD |
351 | return 0; |
352 | } | |
353 | ||
354 | err = btbcm_patchram(hu->hdev, fw); | |
355 | if (err) { | |
65ad07c9 | 356 | bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err); |
6be09b48 FD |
357 | goto finalize; |
358 | } | |
359 | ||
960ef1d7 FD |
360 | /* 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; | |
367 | ||
368 | 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); | |
61b2fc2b | 381 | if (!err) |
960ef1d7 | 382 | hci_uart_set_baudrate(hu, speed); |
61b2fc2b FD |
383 | } |
384 | ||
6be09b48 FD |
385 | finalize: |
386 | release_firmware(fw); | |
387 | ||
388 | err = btbcm_finalize(hu->hdev); | |
6cc4396c FD |
389 | if (err) |
390 | return err; | |
391 | ||
392 | err = bcm_request_irq(bcm); | |
393 | if (!err) | |
394 | err = bcm_setup_sleep(hu); | |
6be09b48 FD |
395 | |
396 | return err; | |
bdd8818e MH |
397 | } |
398 | ||
79b8df93 MH |
399 | static 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 | ||
bdd8818e MH |
405 | static 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 | ||
79b8df93 MH |
412 | bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count, |
413 | bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts)); | |
bdd8818e MH |
414 | if (IS_ERR(bcm->rx_skb)) { |
415 | int err = PTR_ERR(bcm->rx_skb); | |
65ad07c9 | 416 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
37134167 | 417 | bcm->rx_skb = NULL; |
bdd8818e | 418 | return err; |
e88ab30d FD |
419 | } 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); | |
bdd8818e MH |
428 | } |
429 | ||
430 | return count; | |
431 | } | |
432 | ||
433 | static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb) | |
434 | { | |
435 | struct bcm_data *bcm = hu->priv; | |
436 | ||
65ad07c9 | 437 | bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb); |
bdd8818e MH |
438 | |
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 | ||
446 | static struct sk_buff *bcm_dequeue(struct hci_uart *hu) | |
447 | { | |
448 | struct bcm_data *bcm = hu->priv; | |
e88ab30d FD |
449 | struct sk_buff *skb = NULL; |
450 | struct bcm_device *bdev = NULL; | |
451 | ||
452 | 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 | } | |
bdd8818e | 466 | |
e88ab30d FD |
467 | mutex_unlock(&bcm_device_lock); |
468 | ||
469 | return skb; | |
bdd8818e MH |
470 | } |
471 | ||
b7a622a2 FD |
472 | #ifdef CONFIG_PM |
473 | static int bcm_suspend_device(struct device *dev) | |
118612fb FD |
474 | { |
475 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); | |
476 | ||
b7a622a2 | 477 | bt_dev_dbg(bdev, ""); |
917522aa | 478 | |
b7a622a2 | 479 | if (!bdev->is_suspended && bdev->hu) { |
118612fb FD |
480 | hci_uart_set_flow_control(bdev->hu, true); |
481 | ||
b7a622a2 | 482 | /* Once this returns, driver suspends BT via GPIO */ |
118612fb FD |
483 | bdev->is_suspended = true; |
484 | } | |
485 | ||
486 | /* Suspend the device */ | |
487 | if (bdev->device_wakeup) { | |
488 | gpiod_set_value(bdev->device_wakeup, false); | |
65ad07c9 | 489 | bt_dev_dbg(bdev, "suspend, delaying 15 ms"); |
118612fb FD |
490 | mdelay(15); |
491 | } | |
492 | ||
b7a622a2 FD |
493 | return 0; |
494 | } | |
495 | ||
496 | static 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 */ | |
521 | static 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 | ||
e88ab30d FD |
537 | if (pm_runtime_active(dev)) |
538 | bcm_suspend_device(dev); | |
b7a622a2 | 539 | |
6cc4396c FD |
540 | 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 | ||
917522aa | 546 | unlock: |
bb3ea16a | 547 | mutex_unlock(&bcm_device_lock); |
917522aa | 548 | |
118612fb FD |
549 | return 0; |
550 | } | |
551 | ||
552 | /* Platform resume callback */ | |
553 | static int bcm_resume(struct device *dev) | |
554 | { | |
555 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); | |
556 | ||
65ad07c9 | 557 | bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended); |
118612fb | 558 | |
b7a622a2 FD |
559 | /* 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 | */ | |
bb3ea16a | 563 | mutex_lock(&bcm_device_lock); |
917522aa FD |
564 | |
565 | if (!bdev->hu) | |
566 | goto unlock; | |
567 | ||
6cc4396c FD |
568 | if (device_may_wakeup(&bdev->pdev->dev)) { |
569 | disable_irq_wake(bdev->irq); | |
570 | bt_dev_dbg(bdev, "BCM irq: disabled"); | |
571 | } | |
572 | ||
b7a622a2 | 573 | bcm_resume_device(dev); |
118612fb | 574 | |
917522aa | 575 | unlock: |
bb3ea16a | 576 | mutex_unlock(&bcm_device_lock); |
917522aa | 577 | |
e88ab30d FD |
578 | pm_runtime_disable(dev); |
579 | pm_runtime_set_active(dev); | |
580 | pm_runtime_enable(dev); | |
581 | ||
118612fb FD |
582 | return 0; |
583 | } | |
584 | #endif | |
585 | ||
0395ffc1 FD |
586 | static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false }; |
587 | static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false }; | |
6cc4396c | 588 | static const struct acpi_gpio_params host_wakeup_gpios = { 2, 0, false }; |
0395ffc1 FD |
589 | |
590 | static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = { | |
591 | { "device-wakeup-gpios", &device_wakeup_gpios, 1 }, | |
592 | { "shutdown-gpios", &shutdown_gpios, 1 }, | |
6cc4396c | 593 | { "host-wakeup-gpios", &host_wakeup_gpios, 1 }, |
0395ffc1 FD |
594 | { }, |
595 | }; | |
596 | ||
50d78bcf | 597 | #ifdef CONFIG_ACPI |
5cebdfea FD |
598 | static u8 acpi_active_low = ACPI_ACTIVE_LOW; |
599 | ||
600 | /* IRQ polarity of some chipsets are not defined correctly in ACPI table. */ | |
601 | static 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 | ||
ae056908 FD |
614 | static int bcm_resource(struct acpi_resource *ares, void *data) |
615 | { | |
616 | struct bcm_device *dev = data; | |
6cc4396c FD |
617 | struct acpi_resource_extended_irq *irq; |
618 | struct acpi_resource_gpio *gpio; | |
619 | struct acpi_resource_uart_serialbus *sb; | |
620 | ||
621 | switch (ares->type) { | |
622 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: | |
623 | irq = &ares->data.extended_irq; | |
624 | dev->irq_polarity = irq->polarity; | |
625 | break; | |
626 | ||
627 | 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: | |
ae056908 FD |
634 | sb = &ares->data.uart_serial_bus; |
635 | if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) | |
636 | dev->init_speed = sb->default_baud_rate; | |
6cc4396c FD |
637 | break; |
638 | ||
639 | default: | |
640 | break; | |
ae056908 FD |
641 | } |
642 | ||
643 | /* Always tell the ACPI core to skip this resource */ | |
644 | return 1; | |
645 | } | |
646 | ||
0395ffc1 FD |
647 | static int bcm_acpi_probe(struct bcm_device *dev) |
648 | { | |
649 | struct platform_device *pdev = dev->pdev; | |
650 | const struct acpi_device_id *id; | |
ae056908 FD |
651 | struct acpi_device *adev; |
652 | LIST_HEAD(resources); | |
5cebdfea | 653 | const struct dmi_system_id *dmi_id; |
0395ffc1 FD |
654 | 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 | ||
62aaefa7 UKK |
669 | 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); | |
0395ffc1 | 674 | |
62aaefa7 UKK |
675 | 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); | |
0395ffc1 | 679 | |
6cc4396c FD |
680 | /* 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 | ||
0395ffc1 FD |
695 | /* 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 | ||
ae056908 FD |
703 | /* Retrieve UART ACPI info */ |
704 | adev = ACPI_COMPANION(&dev->pdev->dev); | |
705 | if (!adev) | |
706 | return 0; | |
707 | ||
708 | acpi_dev_get_resources(adev, &resources, bcm_resource, dev); | |
09dbf1b7 | 709 | acpi_dev_free_resource_list(&resources); |
ae056908 | 710 | |
5cebdfea FD |
711 | dmi_id = dmi_first_match(bcm_wrong_irq_dmi_table); |
712 | if (dmi_id) { | |
713 | bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low", | |
714 | dmi_id->ident); | |
715 | dev->irq_polarity = *(u8 *)dmi_id->driver_data; | |
716 | } | |
717 | ||
0395ffc1 FD |
718 | return 0; |
719 | } | |
50d78bcf FD |
720 | #else |
721 | static int bcm_acpi_probe(struct bcm_device *dev) | |
722 | { | |
723 | return -EINVAL; | |
724 | } | |
725 | #endif /* CONFIG_ACPI */ | |
0395ffc1 FD |
726 | |
727 | static int bcm_probe(struct platform_device *pdev) | |
728 | { | |
729 | struct bcm_device *dev; | |
730 | struct acpi_device_id *pdata = pdev->dev.platform_data; | |
731 | int ret; | |
732 | ||
733 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); | |
734 | if (!dev) | |
735 | return -ENOMEM; | |
736 | ||
737 | dev->pdev = pdev; | |
738 | ||
739 | if (ACPI_HANDLE(&pdev->dev)) { | |
740 | ret = bcm_acpi_probe(dev); | |
741 | if (ret) | |
742 | return ret; | |
743 | } else if (pdata) { | |
744 | dev->name = pdata->id; | |
745 | } else { | |
746 | return -ENODEV; | |
747 | } | |
748 | ||
749 | platform_set_drvdata(pdev, dev); | |
750 | ||
751 | dev_info(&pdev->dev, "%s device registered.\n", dev->name); | |
752 | ||
753 | /* Place this instance on the device list */ | |
bb3ea16a | 754 | mutex_lock(&bcm_device_lock); |
0395ffc1 | 755 | list_add_tail(&dev->list, &bcm_device_list); |
bb3ea16a | 756 | mutex_unlock(&bcm_device_lock); |
0395ffc1 FD |
757 | |
758 | bcm_gpio_set_power(dev, false); | |
759 | ||
760 | return 0; | |
761 | } | |
762 | ||
763 | static int bcm_remove(struct platform_device *pdev) | |
764 | { | |
765 | struct bcm_device *dev = platform_get_drvdata(pdev); | |
766 | ||
bb3ea16a | 767 | mutex_lock(&bcm_device_lock); |
0395ffc1 | 768 | list_del(&dev->list); |
bb3ea16a | 769 | mutex_unlock(&bcm_device_lock); |
0395ffc1 FD |
770 | |
771 | acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev)); | |
772 | ||
773 | dev_info(&pdev->dev, "%s device unregistered.\n", dev->name); | |
774 | ||
775 | return 0; | |
776 | } | |
777 | ||
bdd8818e MH |
778 | static const struct hci_uart_proto bcm_proto = { |
779 | .id = HCI_UART_BCM, | |
780 | .name = "BCM", | |
61b2fc2b FD |
781 | .init_speed = 115200, |
782 | .oper_speed = 4000000, | |
bdd8818e MH |
783 | .open = bcm_open, |
784 | .close = bcm_close, | |
785 | .flush = bcm_flush, | |
786 | .setup = bcm_setup, | |
61b2fc2b | 787 | .set_baudrate = bcm_set_baudrate, |
bdd8818e MH |
788 | .recv = bcm_recv, |
789 | .enqueue = bcm_enqueue, | |
790 | .dequeue = bcm_dequeue, | |
791 | }; | |
792 | ||
0395ffc1 FD |
793 | #ifdef CONFIG_ACPI |
794 | static const struct acpi_device_id bcm_acpi_match[] = { | |
795 | { "BCM2E39", 0 }, | |
796 | { "BCM2E67", 0 }, | |
797 | { }, | |
798 | }; | |
799 | MODULE_DEVICE_TABLE(acpi, bcm_acpi_match); | |
800 | #endif | |
801 | ||
118612fb | 802 | /* Platform suspend and resume callbacks */ |
e88ab30d FD |
803 | static const struct dev_pm_ops bcm_pm_ops = { |
804 | SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume) | |
805 | SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL) | |
806 | }; | |
118612fb | 807 | |
0395ffc1 FD |
808 | static struct platform_driver bcm_driver = { |
809 | .probe = bcm_probe, | |
810 | .remove = bcm_remove, | |
811 | .driver = { | |
812 | .name = "hci_bcm", | |
813 | .acpi_match_table = ACPI_PTR(bcm_acpi_match), | |
118612fb | 814 | .pm = &bcm_pm_ops, |
0395ffc1 FD |
815 | }, |
816 | }; | |
817 | ||
bdd8818e MH |
818 | int __init bcm_init(void) |
819 | { | |
0395ffc1 FD |
820 | platform_driver_register(&bcm_driver); |
821 | ||
bdd8818e MH |
822 | return hci_uart_register_proto(&bcm_proto); |
823 | } | |
824 | ||
825 | int __exit bcm_deinit(void) | |
826 | { | |
0395ffc1 FD |
827 | platform_driver_unregister(&bcm_driver); |
828 | ||
bdd8818e MH |
829 | return hci_uart_unregister_proto(&bcm_proto); |
830 | } |