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> | |
33cd149e LP |
30 | #include <linux/of.h> |
31 | #include <linux/property.h> | |
4c33162c | 32 | #include <linux/platform_data/x86/apple.h> |
0395ffc1 | 33 | #include <linux/platform_device.h> |
75d11676 | 34 | #include <linux/regulator/consumer.h> |
0395ffc1 FD |
35 | #include <linux/clk.h> |
36 | #include <linux/gpio/consumer.h> | |
37 | #include <linux/tty.h> | |
6cc4396c | 38 | #include <linux/interrupt.h> |
5cebdfea | 39 | #include <linux/dmi.h> |
e88ab30d | 40 | #include <linux/pm_runtime.h> |
33cd149e | 41 | #include <linux/serdev.h> |
e9a2dd26 MH |
42 | |
43 | #include <net/bluetooth/bluetooth.h> | |
44 | #include <net/bluetooth/hci_core.h> | |
45 | ||
bdd8818e | 46 | #include "btbcm.h" |
e9a2dd26 | 47 | #include "hci_uart.h" |
bdd8818e | 48 | |
01d5e44a MH |
49 | #define BCM_NULL_PKT 0x00 |
50 | #define BCM_NULL_SIZE 0 | |
51 | ||
94c58132 MH |
52 | #define BCM_LM_DIAG_PKT 0x07 |
53 | #define BCM_LM_DIAG_SIZE 63 | |
54 | ||
e88ab30d FD |
55 | #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */ |
56 | ||
75d11676 CYT |
57 | #define BCM_NUM_SUPPLIES 2 |
58 | ||
b7c2abac LW |
59 | /** |
60 | * struct bcm_device - device driver resources | |
61 | * @serdev_hu: HCI UART controller struct | |
62 | * @list: bcm_device_list node | |
63 | * @dev: physical UART slave | |
64 | * @name: device name logged by bt_dev_*() functions | |
65 | * @device_wakeup: BT_WAKE pin, | |
66 | * assert = Bluetooth device must wake up or remain awake, | |
67 | * deassert = Bluetooth device may sleep when sleep criteria are met | |
68 | * @shutdown: BT_REG_ON pin, | |
69 | * power up or power down Bluetooth device internal regulators | |
8353b4a6 | 70 | * @set_device_wakeup: callback to toggle BT_WAKE pin |
4c33162c | 71 | * either by accessing @device_wakeup or by calling @btlp |
8353b4a6 | 72 | * @set_shutdown: callback to toggle BT_REG_ON pin |
4c33162c LW |
73 | * either by accessing @shutdown or by calling @btpu/@btpd |
74 | * @btlp: Apple ACPI method to toggle BT_WAKE pin ("Bluetooth Low Power") | |
75 | * @btpu: Apple ACPI method to drive BT_REG_ON pin high ("Bluetooth Power Up") | |
76 | * @btpd: Apple ACPI method to drive BT_REG_ON pin low ("Bluetooth Power Down") | |
55dbfcd0 | 77 | * @txco_clk: external reference frequency clock used by Bluetooth device |
90bc07cc | 78 | * @lpo_clk: external LPO clock used by Bluetooth device |
75d11676 CYT |
79 | * @supplies: VBAT and VDDIO supplies used by Bluetooth device |
80 | * @res_enabled: whether clocks and supplies are prepared and enabled | |
b7c2abac LW |
81 | * @init_speed: default baudrate of Bluetooth device; |
82 | * the host UART is initially set to this baudrate so that | |
83 | * it can configure the Bluetooth device for @oper_speed | |
84 | * @oper_speed: preferred baudrate of Bluetooth device; | |
85 | * set to 0 if @init_speed is already the preferred baudrate | |
86 | * @irq: interrupt triggered by HOST_WAKE_BT pin | |
87 | * @irq_active_low: whether @irq is active low | |
88 | * @hu: pointer to HCI UART controller struct, | |
89 | * used to disable flow control during runtime suspend and system sleep | |
90 | * @is_suspended: whether flow control is currently disabled | |
91 | */ | |
0395ffc1 | 92 | struct bcm_device { |
8a920568 HG |
93 | /* Must be the first member, hci_serdev.c expects this. */ |
94 | struct hci_uart serdev_hu; | |
0395ffc1 FD |
95 | struct list_head list; |
96 | ||
c0d3ce58 | 97 | struct device *dev; |
0395ffc1 FD |
98 | |
99 | const char *name; | |
100 | struct gpio_desc *device_wakeup; | |
101 | struct gpio_desc *shutdown; | |
8353b4a6 LW |
102 | int (*set_device_wakeup)(struct bcm_device *, bool); |
103 | int (*set_shutdown)(struct bcm_device *, bool); | |
4c33162c LW |
104 | #ifdef CONFIG_ACPI |
105 | acpi_handle btlp, btpu, btpd; | |
a4de1567 HG |
106 | int gpio_count; |
107 | int gpio_int_idx; | |
4c33162c | 108 | #endif |
0395ffc1 | 109 | |
55dbfcd0 | 110 | struct clk *txco_clk; |
90bc07cc | 111 | struct clk *lpo_clk; |
75d11676 CYT |
112 | struct regulator_bulk_data supplies[BCM_NUM_SUPPLIES]; |
113 | bool res_enabled; | |
ae056908 FD |
114 | |
115 | u32 init_speed; | |
74183a1c | 116 | u32 oper_speed; |
6cc4396c | 117 | int irq; |
227630cc | 118 | bool irq_active_low; |
118612fb | 119 | |
b7a622a2 | 120 | #ifdef CONFIG_PM |
118612fb | 121 | struct hci_uart *hu; |
b7c2abac | 122 | bool is_suspended; |
118612fb | 123 | #endif |
0395ffc1 FD |
124 | }; |
125 | ||
33cd149e | 126 | /* generic bcm uart resources */ |
bdd8818e | 127 | struct bcm_data { |
0395ffc1 FD |
128 | struct sk_buff *rx_skb; |
129 | struct sk_buff_head txq; | |
130 | ||
131 | struct bcm_device *dev; | |
bdd8818e MH |
132 | }; |
133 | ||
0395ffc1 | 134 | /* List of BCM BT UART devices */ |
bb3ea16a | 135 | static DEFINE_MUTEX(bcm_device_lock); |
0395ffc1 FD |
136 | static LIST_HEAD(bcm_device_list); |
137 | ||
e09070c5 HG |
138 | static int irq_polarity = -1; |
139 | module_param(irq_polarity, int, 0444); | |
140 | MODULE_PARM_DESC(irq_polarity, "IRQ polarity 0: active-high 1: active-low"); | |
141 | ||
33cd149e LP |
142 | static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed) |
143 | { | |
144 | if (hu->serdev) | |
145 | serdev_device_set_baudrate(hu->serdev, speed); | |
146 | else | |
147 | hci_uart_set_baudrate(hu, speed); | |
148 | } | |
149 | ||
61b2fc2b FD |
150 | static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed) |
151 | { | |
152 | struct hci_dev *hdev = hu->hdev; | |
153 | struct sk_buff *skb; | |
154 | struct bcm_update_uart_baud_rate param; | |
155 | ||
156 | if (speed > 3000000) { | |
157 | struct bcm_write_uart_clock_setting clock; | |
158 | ||
159 | clock.type = BCM_UART_CLOCK_48MHZ; | |
160 | ||
65ad07c9 | 161 | bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type); |
61b2fc2b FD |
162 | |
163 | /* This Broadcom specific command changes the UART's controller | |
164 | * clock for baud rate > 3000000. | |
165 | */ | |
166 | skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT); | |
167 | if (IS_ERR(skb)) { | |
168 | int err = PTR_ERR(skb); | |
65ad07c9 FD |
169 | bt_dev_err(hdev, "BCM: failed to write clock (%d)", |
170 | err); | |
61b2fc2b FD |
171 | return err; |
172 | } | |
173 | ||
174 | kfree_skb(skb); | |
175 | } | |
176 | ||
65ad07c9 | 177 | bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed); |
61b2fc2b FD |
178 | |
179 | param.zero = cpu_to_le16(0); | |
180 | param.baud_rate = cpu_to_le32(speed); | |
181 | ||
182 | /* This Broadcom specific command changes the UART's controller baud | |
183 | * rate. | |
184 | */ | |
185 | skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), ¶m, | |
186 | HCI_INIT_TIMEOUT); | |
187 | if (IS_ERR(skb)) { | |
188 | int err = PTR_ERR(skb); | |
65ad07c9 FD |
189 | bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)", |
190 | err); | |
61b2fc2b FD |
191 | return err; |
192 | } | |
193 | ||
194 | kfree_skb(skb); | |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
917522aa | 199 | /* bcm_device_exists should be protected by bcm_device_lock */ |
0395ffc1 FD |
200 | static bool bcm_device_exists(struct bcm_device *device) |
201 | { | |
202 | struct list_head *p; | |
203 | ||
81a19053 | 204 | #ifdef CONFIG_PM |
8a920568 HG |
205 | /* Devices using serdev always exist */ |
206 | if (device && device->hu && device->hu->serdev) | |
207 | return true; | |
81a19053 | 208 | #endif |
8a920568 | 209 | |
0395ffc1 FD |
210 | list_for_each(p, &bcm_device_list) { |
211 | struct bcm_device *dev = list_entry(p, struct bcm_device, list); | |
212 | ||
213 | if (device == dev) | |
214 | return true; | |
215 | } | |
216 | ||
217 | return false; | |
218 | } | |
219 | ||
220 | static int bcm_gpio_set_power(struct bcm_device *dev, bool powered) | |
221 | { | |
8bfa7e1e | 222 | int err; |
0395ffc1 | 223 | |
75d11676 CYT |
224 | if (powered && !dev->res_enabled) { |
225 | err = regulator_bulk_enable(BCM_NUM_SUPPLIES, dev->supplies); | |
226 | if (err) | |
227 | return err; | |
228 | ||
90bc07cc CYT |
229 | /* LPO clock needs to be 32.768 kHz */ |
230 | err = clk_set_rate(dev->lpo_clk, 32768); | |
231 | if (err) { | |
232 | dev_err(dev->dev, "Could not set LPO clock rate\n"); | |
75d11676 | 233 | goto err_regulator_disable; |
90bc07cc CYT |
234 | } |
235 | ||
236 | err = clk_prepare_enable(dev->lpo_clk); | |
8bfa7e1e | 237 | if (err) |
75d11676 | 238 | goto err_regulator_disable; |
90bc07cc CYT |
239 | |
240 | err = clk_prepare_enable(dev->txco_clk); | |
241 | if (err) | |
242 | goto err_lpo_clk_disable; | |
8bfa7e1e LW |
243 | } |
244 | ||
245 | err = dev->set_shutdown(dev, powered); | |
246 | if (err) | |
90bc07cc | 247 | goto err_txco_clk_disable; |
8bfa7e1e LW |
248 | |
249 | err = dev->set_device_wakeup(dev, powered); | |
250 | if (err) | |
251 | goto err_revert_shutdown; | |
0395ffc1 | 252 | |
75d11676 | 253 | if (!powered && dev->res_enabled) { |
55dbfcd0 | 254 | clk_disable_unprepare(dev->txco_clk); |
90bc07cc | 255 | clk_disable_unprepare(dev->lpo_clk); |
75d11676 | 256 | regulator_bulk_disable(BCM_NUM_SUPPLIES, dev->supplies); |
90bc07cc | 257 | } |
0395ffc1 | 258 | |
91927a9b CYT |
259 | /* wait for device to power on and come out of reset */ |
260 | usleep_range(10000, 20000); | |
261 | ||
75d11676 | 262 | dev->res_enabled = powered; |
0395ffc1 FD |
263 | |
264 | return 0; | |
8bfa7e1e LW |
265 | |
266 | err_revert_shutdown: | |
267 | dev->set_shutdown(dev, !powered); | |
90bc07cc | 268 | err_txco_clk_disable: |
75d11676 | 269 | if (powered && !dev->res_enabled) |
55dbfcd0 | 270 | clk_disable_unprepare(dev->txco_clk); |
90bc07cc | 271 | err_lpo_clk_disable: |
75d11676 | 272 | if (powered && !dev->res_enabled) |
90bc07cc | 273 | clk_disable_unprepare(dev->lpo_clk); |
75d11676 CYT |
274 | err_regulator_disable: |
275 | if (powered && !dev->res_enabled) | |
276 | regulator_bulk_disable(BCM_NUM_SUPPLIES, dev->supplies); | |
8bfa7e1e | 277 | return err; |
0395ffc1 FD |
278 | } |
279 | ||
b7a622a2 | 280 | #ifdef CONFIG_PM |
6cc4396c FD |
281 | static irqreturn_t bcm_host_wake(int irq, void *data) |
282 | { | |
283 | struct bcm_device *bdev = data; | |
284 | ||
285 | bt_dev_dbg(bdev, "Host wake IRQ"); | |
286 | ||
b09c6152 HG |
287 | pm_runtime_get(bdev->dev); |
288 | pm_runtime_mark_last_busy(bdev->dev); | |
289 | pm_runtime_put_autosuspend(bdev->dev); | |
e88ab30d | 290 | |
6cc4396c FD |
291 | return IRQ_HANDLED; |
292 | } | |
293 | ||
294 | static int bcm_request_irq(struct bcm_data *bcm) | |
295 | { | |
296 | struct bcm_device *bdev = bcm->dev; | |
98dc77d5 | 297 | int err; |
6cc4396c | 298 | |
6cc4396c FD |
299 | mutex_lock(&bcm_device_lock); |
300 | if (!bcm_device_exists(bdev)) { | |
301 | err = -ENODEV; | |
302 | goto unlock; | |
303 | } | |
304 | ||
98dc77d5 LP |
305 | if (bdev->irq <= 0) { |
306 | err = -EOPNOTSUPP; | |
307 | goto unlock; | |
308 | } | |
6cc4396c | 309 | |
c0d3ce58 | 310 | err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake, |
227630cc HG |
311 | bdev->irq_active_low ? IRQF_TRIGGER_FALLING : |
312 | IRQF_TRIGGER_RISING, | |
313 | "host_wake", bdev); | |
4dc27330 LW |
314 | if (err) { |
315 | bdev->irq = err; | |
98dc77d5 | 316 | goto unlock; |
4dc27330 | 317 | } |
e88ab30d | 318 | |
c0d3ce58 | 319 | device_init_wakeup(bdev->dev, true); |
98dc77d5 | 320 | |
c0d3ce58 | 321 | pm_runtime_set_autosuspend_delay(bdev->dev, |
98dc77d5 | 322 | BCM_AUTOSUSPEND_DELAY); |
c0d3ce58 HG |
323 | pm_runtime_use_autosuspend(bdev->dev); |
324 | pm_runtime_set_active(bdev->dev); | |
325 | pm_runtime_enable(bdev->dev); | |
6cc4396c FD |
326 | |
327 | unlock: | |
328 | mutex_unlock(&bcm_device_lock); | |
329 | ||
330 | return err; | |
331 | } | |
332 | ||
333 | static const struct bcm_set_sleep_mode default_sleep_params = { | |
334 | .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */ | |
335 | .idle_host = 2, /* idle threshold HOST, in 300ms */ | |
336 | .idle_dev = 2, /* idle threshold device, in 300ms */ | |
337 | .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */ | |
338 | .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */ | |
339 | .allow_host_sleep = 1, /* Allow host sleep in SCO flag */ | |
e88ab30d | 340 | .combine_modes = 1, /* Combine sleep and LPM flag */ |
6cc4396c FD |
341 | .tristate_control = 0, /* Allow tri-state control of UART tx flag */ |
342 | /* Irrelevant USB flags */ | |
343 | .usb_auto_sleep = 0, | |
344 | .usb_resume_timeout = 0, | |
ff875960 | 345 | .break_to_host = 0, |
e07c99b0 | 346 | .pulsed_host_wake = 1, |
6cc4396c FD |
347 | }; |
348 | ||
349 | static int bcm_setup_sleep(struct hci_uart *hu) | |
350 | { | |
351 | struct bcm_data *bcm = hu->priv; | |
352 | struct sk_buff *skb; | |
353 | struct bcm_set_sleep_mode sleep_params = default_sleep_params; | |
354 | ||
227630cc | 355 | sleep_params.host_wake_active = !bcm->dev->irq_active_low; |
6cc4396c FD |
356 | |
357 | skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params), | |
358 | &sleep_params, HCI_INIT_TIMEOUT); | |
359 | if (IS_ERR(skb)) { | |
360 | int err = PTR_ERR(skb); | |
361 | bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err); | |
362 | return err; | |
363 | } | |
364 | kfree_skb(skb); | |
365 | ||
366 | bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded"); | |
367 | ||
368 | return 0; | |
369 | } | |
370 | #else | |
371 | static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; } | |
372 | static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; } | |
373 | #endif | |
374 | ||
075e1f5e MH |
375 | static int bcm_set_diag(struct hci_dev *hdev, bool enable) |
376 | { | |
377 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
378 | struct bcm_data *bcm = hu->priv; | |
379 | struct sk_buff *skb; | |
380 | ||
381 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
382 | return -ENETDOWN; | |
383 | ||
384 | skb = bt_skb_alloc(3, GFP_KERNEL); | |
a1857390 DC |
385 | if (!skb) |
386 | return -ENOMEM; | |
075e1f5e | 387 | |
634fef61 JB |
388 | skb_put_u8(skb, BCM_LM_DIAG_PKT); |
389 | skb_put_u8(skb, 0xf0); | |
390 | skb_put_u8(skb, enable); | |
075e1f5e MH |
391 | |
392 | skb_queue_tail(&bcm->txq, skb); | |
393 | hci_uart_tx_wakeup(hu); | |
394 | ||
395 | return 0; | |
396 | } | |
397 | ||
bdd8818e MH |
398 | static int bcm_open(struct hci_uart *hu) |
399 | { | |
400 | struct bcm_data *bcm; | |
0395ffc1 | 401 | struct list_head *p; |
8bfa7e1e | 402 | int err; |
bdd8818e | 403 | |
65ad07c9 | 404 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
bdd8818e MH |
405 | |
406 | bcm = kzalloc(sizeof(*bcm), GFP_KERNEL); | |
407 | if (!bcm) | |
408 | return -ENOMEM; | |
409 | ||
410 | skb_queue_head_init(&bcm->txq); | |
411 | ||
412 | hu->priv = bcm; | |
0395ffc1 | 413 | |
8a920568 HG |
414 | mutex_lock(&bcm_device_lock); |
415 | ||
33cd149e | 416 | if (hu->serdev) { |
8a920568 | 417 | bcm->dev = serdev_device_get_drvdata(hu->serdev); |
33cd149e LP |
418 | goto out; |
419 | } | |
420 | ||
95065a61 JH |
421 | if (!hu->tty->dev) |
422 | goto out; | |
423 | ||
0395ffc1 FD |
424 | list_for_each(p, &bcm_device_list) { |
425 | struct bcm_device *dev = list_entry(p, struct bcm_device, list); | |
426 | ||
427 | /* Retrieve saved bcm_device based on parent of the | |
428 | * platform device (saved during device probe) and | |
429 | * parent of tty device used by hci_uart | |
430 | */ | |
c0d3ce58 | 431 | if (hu->tty->dev->parent == dev->dev->parent) { |
0395ffc1 | 432 | bcm->dev = dev; |
b7a622a2 | 433 | #ifdef CONFIG_PM |
118612fb FD |
434 | dev->hu = hu; |
435 | #endif | |
0395ffc1 FD |
436 | break; |
437 | } | |
438 | } | |
439 | ||
95065a61 | 440 | out: |
8a920568 HG |
441 | if (bcm->dev) { |
442 | hu->init_speed = bcm->dev->init_speed; | |
443 | hu->oper_speed = bcm->dev->oper_speed; | |
8bfa7e1e LW |
444 | err = bcm_gpio_set_power(bcm->dev, true); |
445 | if (err) | |
446 | goto err_unset_hu; | |
8a920568 HG |
447 | } |
448 | ||
449 | mutex_unlock(&bcm_device_lock); | |
bdd8818e | 450 | return 0; |
8bfa7e1e LW |
451 | |
452 | err_unset_hu: | |
453 | #ifdef CONFIG_PM | |
e9ca0807 | 454 | if (!hu->serdev) |
8c6b8eda | 455 | bcm->dev->hu = NULL; |
8bfa7e1e | 456 | #endif |
8bfa7e1e LW |
457 | mutex_unlock(&bcm_device_lock); |
458 | hu->priv = NULL; | |
459 | kfree(bcm); | |
460 | return err; | |
bdd8818e MH |
461 | } |
462 | ||
463 | static int bcm_close(struct hci_uart *hu) | |
464 | { | |
465 | struct bcm_data *bcm = hu->priv; | |
8a920568 | 466 | struct bcm_device *bdev = NULL; |
8bfa7e1e | 467 | int err; |
bdd8818e | 468 | |
65ad07c9 | 469 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
bdd8818e | 470 | |
0395ffc1 | 471 | /* Protect bcm->dev against removal of the device or driver */ |
bb3ea16a | 472 | mutex_lock(&bcm_device_lock); |
8a920568 HG |
473 | |
474 | if (hu->serdev) { | |
8a920568 HG |
475 | bdev = serdev_device_get_drvdata(hu->serdev); |
476 | } else if (bcm_device_exists(bcm->dev)) { | |
477 | bdev = bcm->dev; | |
478 | #ifdef CONFIG_PM | |
479 | bdev->hu = NULL; | |
480 | #endif | |
481 | } | |
482 | ||
483 | if (bdev) { | |
6d83f1ee | 484 | if (IS_ENABLED(CONFIG_PM) && bdev->irq > 0) { |
c0d3ce58 HG |
485 | devm_free_irq(bdev->dev, bdev->irq, bdev); |
486 | device_init_wakeup(bdev->dev, false); | |
f4cf6b7e | 487 | pm_runtime_disable(bdev->dev); |
6cc4396c | 488 | } |
54ba69f9 | 489 | |
8bfa7e1e LW |
490 | err = bcm_gpio_set_power(bdev, false); |
491 | if (err) | |
492 | bt_dev_err(hu->hdev, "Failed to power down"); | |
493 | else | |
494 | pm_runtime_set_suspended(bdev->dev); | |
118612fb | 495 | } |
bb3ea16a | 496 | mutex_unlock(&bcm_device_lock); |
0395ffc1 | 497 | |
bdd8818e MH |
498 | skb_queue_purge(&bcm->txq); |
499 | kfree_skb(bcm->rx_skb); | |
500 | kfree(bcm); | |
501 | ||
502 | hu->priv = NULL; | |
503 | return 0; | |
504 | } | |
505 | ||
506 | static int bcm_flush(struct hci_uart *hu) | |
507 | { | |
508 | struct bcm_data *bcm = hu->priv; | |
509 | ||
65ad07c9 | 510 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
bdd8818e MH |
511 | |
512 | skb_queue_purge(&bcm->txq); | |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
517 | static int bcm_setup(struct hci_uart *hu) | |
518 | { | |
6cc4396c | 519 | struct bcm_data *bcm = hu->priv; |
6be09b48 FD |
520 | char fw_name[64]; |
521 | const struct firmware *fw; | |
960ef1d7 | 522 | unsigned int speed; |
6be09b48 FD |
523 | int err; |
524 | ||
65ad07c9 | 525 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
bdd8818e | 526 | |
075e1f5e | 527 | hu->hdev->set_diag = bcm_set_diag; |
bdd8818e MH |
528 | hu->hdev->set_bdaddr = btbcm_set_bdaddr; |
529 | ||
22ac1916 | 530 | err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name), false); |
6be09b48 FD |
531 | if (err) |
532 | return err; | |
533 | ||
534 | err = request_firmware(&fw, fw_name, &hu->hdev->dev); | |
535 | if (err < 0) { | |
65ad07c9 | 536 | bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name); |
6be09b48 FD |
537 | return 0; |
538 | } | |
539 | ||
540 | err = btbcm_patchram(hu->hdev, fw); | |
541 | if (err) { | |
65ad07c9 | 542 | bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err); |
6be09b48 FD |
543 | goto finalize; |
544 | } | |
545 | ||
960ef1d7 FD |
546 | /* Init speed if any */ |
547 | if (hu->init_speed) | |
548 | speed = hu->init_speed; | |
549 | else if (hu->proto->init_speed) | |
550 | speed = hu->proto->init_speed; | |
551 | else | |
552 | speed = 0; | |
553 | ||
554 | if (speed) | |
33cd149e | 555 | host_set_baudrate(hu, speed); |
960ef1d7 FD |
556 | |
557 | /* Operational speed if any */ | |
558 | if (hu->oper_speed) | |
559 | speed = hu->oper_speed; | |
560 | else if (hu->proto->oper_speed) | |
561 | speed = hu->proto->oper_speed; | |
562 | else | |
563 | speed = 0; | |
564 | ||
565 | if (speed) { | |
566 | err = bcm_set_baudrate(hu, speed); | |
61b2fc2b | 567 | if (!err) |
33cd149e | 568 | host_set_baudrate(hu, speed); |
61b2fc2b FD |
569 | } |
570 | ||
6be09b48 FD |
571 | finalize: |
572 | release_firmware(fw); | |
573 | ||
574 | err = btbcm_finalize(hu->hdev); | |
6cc4396c FD |
575 | if (err) |
576 | return err; | |
577 | ||
cdd24a20 | 578 | if (!bcm_request_irq(bcm)) |
6cc4396c | 579 | err = bcm_setup_sleep(hu); |
6be09b48 FD |
580 | |
581 | return err; | |
bdd8818e MH |
582 | } |
583 | ||
94c58132 MH |
584 | #define BCM_RECV_LM_DIAG \ |
585 | .type = BCM_LM_DIAG_PKT, \ | |
586 | .hlen = BCM_LM_DIAG_SIZE, \ | |
587 | .loff = 0, \ | |
588 | .lsize = 0, \ | |
589 | .maxlen = BCM_LM_DIAG_SIZE | |
590 | ||
01d5e44a MH |
591 | #define BCM_RECV_NULL \ |
592 | .type = BCM_NULL_PKT, \ | |
593 | .hlen = BCM_NULL_SIZE, \ | |
594 | .loff = 0, \ | |
595 | .lsize = 0, \ | |
596 | .maxlen = BCM_NULL_SIZE | |
597 | ||
79b8df93 | 598 | static const struct h4_recv_pkt bcm_recv_pkts[] = { |
94c58132 MH |
599 | { H4_RECV_ACL, .recv = hci_recv_frame }, |
600 | { H4_RECV_SCO, .recv = hci_recv_frame }, | |
601 | { H4_RECV_EVENT, .recv = hci_recv_frame }, | |
602 | { BCM_RECV_LM_DIAG, .recv = hci_recv_diag }, | |
01d5e44a | 603 | { BCM_RECV_NULL, .recv = hci_recv_diag }, |
79b8df93 MH |
604 | }; |
605 | ||
bdd8818e MH |
606 | static int bcm_recv(struct hci_uart *hu, const void *data, int count) |
607 | { | |
608 | struct bcm_data *bcm = hu->priv; | |
609 | ||
610 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) | |
611 | return -EUNATCH; | |
612 | ||
79b8df93 MH |
613 | bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count, |
614 | bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts)); | |
bdd8818e MH |
615 | if (IS_ERR(bcm->rx_skb)) { |
616 | int err = PTR_ERR(bcm->rx_skb); | |
65ad07c9 | 617 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
37134167 | 618 | bcm->rx_skb = NULL; |
bdd8818e | 619 | return err; |
e88ab30d FD |
620 | } else if (!bcm->rx_skb) { |
621 | /* Delay auto-suspend when receiving completed packet */ | |
622 | mutex_lock(&bcm_device_lock); | |
b09c6152 HG |
623 | if (bcm->dev && bcm_device_exists(bcm->dev)) { |
624 | pm_runtime_get(bcm->dev->dev); | |
625 | pm_runtime_mark_last_busy(bcm->dev->dev); | |
626 | pm_runtime_put_autosuspend(bcm->dev->dev); | |
627 | } | |
e88ab30d | 628 | mutex_unlock(&bcm_device_lock); |
bdd8818e MH |
629 | } |
630 | ||
631 | return count; | |
632 | } | |
633 | ||
634 | static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb) | |
635 | { | |
636 | struct bcm_data *bcm = hu->priv; | |
637 | ||
65ad07c9 | 638 | bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb); |
bdd8818e MH |
639 | |
640 | /* Prepend skb with frame type */ | |
618e8bc2 | 641 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
bdd8818e MH |
642 | skb_queue_tail(&bcm->txq, skb); |
643 | ||
644 | return 0; | |
645 | } | |
646 | ||
647 | static struct sk_buff *bcm_dequeue(struct hci_uart *hu) | |
648 | { | |
649 | struct bcm_data *bcm = hu->priv; | |
e88ab30d FD |
650 | struct sk_buff *skb = NULL; |
651 | struct bcm_device *bdev = NULL; | |
652 | ||
653 | mutex_lock(&bcm_device_lock); | |
654 | ||
655 | if (bcm_device_exists(bcm->dev)) { | |
656 | bdev = bcm->dev; | |
c0d3ce58 | 657 | pm_runtime_get_sync(bdev->dev); |
e88ab30d FD |
658 | /* Shall be resumed here */ |
659 | } | |
660 | ||
661 | skb = skb_dequeue(&bcm->txq); | |
662 | ||
663 | if (bdev) { | |
c0d3ce58 HG |
664 | pm_runtime_mark_last_busy(bdev->dev); |
665 | pm_runtime_put_autosuspend(bdev->dev); | |
e88ab30d | 666 | } |
bdd8818e | 667 | |
e88ab30d FD |
668 | mutex_unlock(&bcm_device_lock); |
669 | ||
670 | return skb; | |
bdd8818e MH |
671 | } |
672 | ||
b7a622a2 FD |
673 | #ifdef CONFIG_PM |
674 | static int bcm_suspend_device(struct device *dev) | |
118612fb | 675 | { |
78277d73 | 676 | struct bcm_device *bdev = dev_get_drvdata(dev); |
8bfa7e1e | 677 | int err; |
118612fb | 678 | |
b7a622a2 | 679 | bt_dev_dbg(bdev, ""); |
917522aa | 680 | |
b7a622a2 | 681 | if (!bdev->is_suspended && bdev->hu) { |
118612fb FD |
682 | hci_uart_set_flow_control(bdev->hu, true); |
683 | ||
b7a622a2 | 684 | /* Once this returns, driver suspends BT via GPIO */ |
118612fb FD |
685 | bdev->is_suspended = true; |
686 | } | |
687 | ||
688 | /* Suspend the device */ | |
8bfa7e1e LW |
689 | err = bdev->set_device_wakeup(bdev, false); |
690 | if (err) { | |
691 | if (bdev->is_suspended && bdev->hu) { | |
692 | bdev->is_suspended = false; | |
693 | hci_uart_set_flow_control(bdev->hu, false); | |
694 | } | |
695 | return -EBUSY; | |
696 | } | |
697 | ||
3e81a4ca | 698 | bt_dev_dbg(bdev, "suspend, delaying 15 ms"); |
e4b9e5b8 | 699 | msleep(15); |
118612fb | 700 | |
b7a622a2 FD |
701 | return 0; |
702 | } | |
703 | ||
704 | static int bcm_resume_device(struct device *dev) | |
705 | { | |
78277d73 | 706 | struct bcm_device *bdev = dev_get_drvdata(dev); |
8bfa7e1e | 707 | int err; |
b7a622a2 FD |
708 | |
709 | bt_dev_dbg(bdev, ""); | |
710 | ||
8bfa7e1e LW |
711 | err = bdev->set_device_wakeup(bdev, true); |
712 | if (err) { | |
713 | dev_err(dev, "Failed to power up\n"); | |
714 | return err; | |
715 | } | |
716 | ||
3e81a4ca | 717 | bt_dev_dbg(bdev, "resume, delaying 15 ms"); |
e4b9e5b8 | 718 | msleep(15); |
b7a622a2 FD |
719 | |
720 | /* When this executes, the device has woken up already */ | |
721 | if (bdev->is_suspended && bdev->hu) { | |
722 | bdev->is_suspended = false; | |
723 | ||
724 | hci_uart_set_flow_control(bdev->hu, false); | |
725 | } | |
726 | ||
727 | return 0; | |
728 | } | |
729 | #endif | |
730 | ||
731 | #ifdef CONFIG_PM_SLEEP | |
8a920568 | 732 | /* suspend callback */ |
b7a622a2 FD |
733 | static int bcm_suspend(struct device *dev) |
734 | { | |
78277d73 | 735 | struct bcm_device *bdev = dev_get_drvdata(dev); |
b7a622a2 FD |
736 | int error; |
737 | ||
738 | bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended); | |
739 | ||
8a920568 HG |
740 | /* |
741 | * When used with a device instantiated as platform_device, bcm_suspend | |
742 | * can be called at any time as long as the platform device is bound, | |
743 | * so it should use bcm_device_lock to protect access to hci_uart | |
b7a622a2 FD |
744 | * and device_wake-up GPIO. |
745 | */ | |
746 | mutex_lock(&bcm_device_lock); | |
747 | ||
748 | if (!bdev->hu) | |
749 | goto unlock; | |
750 | ||
e88ab30d FD |
751 | if (pm_runtime_active(dev)) |
752 | bcm_suspend_device(dev); | |
b7a622a2 | 753 | |
4a59f1fa | 754 | if (device_may_wakeup(dev) && bdev->irq > 0) { |
6cc4396c FD |
755 | error = enable_irq_wake(bdev->irq); |
756 | if (!error) | |
757 | bt_dev_dbg(bdev, "BCM irq: enabled"); | |
758 | } | |
759 | ||
917522aa | 760 | unlock: |
bb3ea16a | 761 | mutex_unlock(&bcm_device_lock); |
917522aa | 762 | |
118612fb FD |
763 | return 0; |
764 | } | |
765 | ||
8a920568 | 766 | /* resume callback */ |
118612fb FD |
767 | static int bcm_resume(struct device *dev) |
768 | { | |
78277d73 | 769 | struct bcm_device *bdev = dev_get_drvdata(dev); |
8bfa7e1e | 770 | int err = 0; |
118612fb | 771 | |
65ad07c9 | 772 | bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended); |
118612fb | 773 | |
8a920568 HG |
774 | /* |
775 | * When used with a device instantiated as platform_device, bcm_resume | |
776 | * can be called at any time as long as platform device is bound, | |
777 | * so it should use bcm_device_lock to protect access to hci_uart | |
b7a622a2 FD |
778 | * and device_wake-up GPIO. |
779 | */ | |
bb3ea16a | 780 | mutex_lock(&bcm_device_lock); |
917522aa FD |
781 | |
782 | if (!bdev->hu) | |
783 | goto unlock; | |
784 | ||
4a59f1fa | 785 | if (device_may_wakeup(dev) && bdev->irq > 0) { |
6cc4396c FD |
786 | disable_irq_wake(bdev->irq); |
787 | bt_dev_dbg(bdev, "BCM irq: disabled"); | |
788 | } | |
789 | ||
8bfa7e1e | 790 | err = bcm_resume_device(dev); |
118612fb | 791 | |
917522aa | 792 | unlock: |
bb3ea16a | 793 | mutex_unlock(&bcm_device_lock); |
917522aa | 794 | |
8bfa7e1e LW |
795 | if (!err) { |
796 | pm_runtime_disable(dev); | |
797 | pm_runtime_set_active(dev); | |
798 | pm_runtime_enable(dev); | |
799 | } | |
e88ab30d | 800 | |
118612fb FD |
801 | return 0; |
802 | } | |
803 | #endif | |
804 | ||
9644e6b9 HG |
805 | static const struct acpi_gpio_params first_gpio = { 0, 0, false }; |
806 | static const struct acpi_gpio_params second_gpio = { 1, 0, false }; | |
807 | static const struct acpi_gpio_params third_gpio = { 2, 0, false }; | |
89ab37b4 DD |
808 | |
809 | static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = { | |
9644e6b9 HG |
810 | { "device-wakeup-gpios", &first_gpio, 1 }, |
811 | { "shutdown-gpios", &second_gpio, 1 }, | |
812 | { "host-wakeup-gpios", &third_gpio, 1 }, | |
89ab37b4 DD |
813 | { }, |
814 | }; | |
815 | ||
89ab37b4 | 816 | static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = { |
9644e6b9 HG |
817 | { "host-wakeup-gpios", &first_gpio, 1 }, |
818 | { "device-wakeup-gpios", &second_gpio, 1 }, | |
819 | { "shutdown-gpios", &third_gpio, 1 }, | |
0395ffc1 FD |
820 | { }, |
821 | }; | |
822 | ||
2b05393b HG |
823 | /* Some firmware reports an IRQ which does not work (wrong pin in fw table?) */ |
824 | static const struct dmi_system_id bcm_broken_irq_dmi_table[] = { | |
825 | { | |
826 | .ident = "Meegopad T08", | |
827 | .matches = { | |
828 | DMI_EXACT_MATCH(DMI_BOARD_VENDOR, | |
829 | "To be filled by OEM."), | |
830 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "T3 MRD"), | |
831 | DMI_EXACT_MATCH(DMI_BOARD_VERSION, "V1.1"), | |
832 | }, | |
833 | }, | |
834 | { } | |
835 | }; | |
836 | ||
50d78bcf | 837 | #ifdef CONFIG_ACPI |
ae056908 FD |
838 | static int bcm_resource(struct acpi_resource *ares, void *data) |
839 | { | |
840 | struct bcm_device *dev = data; | |
6cc4396c FD |
841 | struct acpi_resource_extended_irq *irq; |
842 | struct acpi_resource_gpio *gpio; | |
843 | struct acpi_resource_uart_serialbus *sb; | |
844 | ||
845 | switch (ares->type) { | |
846 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: | |
847 | irq = &ares->data.extended_irq; | |
bb5208b3 HG |
848 | if (irq->polarity != ACPI_ACTIVE_LOW) |
849 | dev_info(dev->dev, "ACPI Interrupt resource is active-high, this is usually wrong, treating the IRQ as active-low\n"); | |
850 | dev->irq_active_low = true; | |
6cc4396c FD |
851 | break; |
852 | ||
853 | case ACPI_RESOURCE_TYPE_GPIO: | |
854 | gpio = &ares->data.gpio; | |
a4de1567 HG |
855 | if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) { |
856 | dev->gpio_int_idx = dev->gpio_count; | |
227630cc | 857 | dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW; |
a4de1567 HG |
858 | } |
859 | dev->gpio_count++; | |
6cc4396c FD |
860 | break; |
861 | ||
862 | case ACPI_RESOURCE_TYPE_SERIAL_BUS: | |
ae056908 | 863 | sb = &ares->data.uart_serial_bus; |
74183a1c | 864 | if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) { |
ae056908 | 865 | dev->init_speed = sb->default_baud_rate; |
74183a1c MH |
866 | dev->oper_speed = 4000000; |
867 | } | |
6cc4396c FD |
868 | break; |
869 | ||
870 | default: | |
871 | break; | |
ae056908 FD |
872 | } |
873 | ||
9d54fd6a | 874 | return 0; |
ae056908 | 875 | } |
4c33162c LW |
876 | |
877 | static int bcm_apple_set_device_wakeup(struct bcm_device *dev, bool awake) | |
878 | { | |
879 | if (ACPI_FAILURE(acpi_execute_simple_method(dev->btlp, NULL, !awake))) | |
880 | return -EIO; | |
881 | ||
882 | return 0; | |
883 | } | |
884 | ||
885 | static int bcm_apple_set_shutdown(struct bcm_device *dev, bool powered) | |
886 | { | |
887 | if (ACPI_FAILURE(acpi_evaluate_object(powered ? dev->btpu : dev->btpd, | |
888 | NULL, NULL, NULL))) | |
889 | return -EIO; | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
894 | static int bcm_apple_get_resources(struct bcm_device *dev) | |
895 | { | |
896 | struct acpi_device *adev = ACPI_COMPANION(dev->dev); | |
897 | const union acpi_object *obj; | |
898 | ||
899 | if (!adev || | |
900 | ACPI_FAILURE(acpi_get_handle(adev->handle, "BTLP", &dev->btlp)) || | |
901 | ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPU", &dev->btpu)) || | |
902 | ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPD", &dev->btpd))) | |
903 | return -ENODEV; | |
904 | ||
905 | if (!acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, &obj) && | |
906 | obj->buffer.length == 8) | |
907 | dev->init_speed = *(u64 *)obj->buffer.pointer; | |
908 | ||
909 | dev->set_device_wakeup = bcm_apple_set_device_wakeup; | |
910 | dev->set_shutdown = bcm_apple_set_shutdown; | |
911 | ||
912 | return 0; | |
913 | } | |
914 | #else | |
915 | static inline int bcm_apple_get_resources(struct bcm_device *dev) | |
916 | { | |
917 | return -EOPNOTSUPP; | |
918 | } | |
212d7183 | 919 | #endif /* CONFIG_ACPI */ |
ae056908 | 920 | |
8353b4a6 LW |
921 | static int bcm_gpio_set_device_wakeup(struct bcm_device *dev, bool awake) |
922 | { | |
fb2d466b | 923 | gpiod_set_value_cansleep(dev->device_wakeup, awake); |
8353b4a6 LW |
924 | return 0; |
925 | } | |
926 | ||
927 | static int bcm_gpio_set_shutdown(struct bcm_device *dev, bool powered) | |
928 | { | |
fb2d466b | 929 | gpiod_set_value_cansleep(dev->shutdown, powered); |
8353b4a6 LW |
930 | return 0; |
931 | } | |
932 | ||
55dbfcd0 CYT |
933 | /* Try a bunch of names for TXCO */ |
934 | static struct clk *bcm_get_txco(struct device *dev) | |
935 | { | |
936 | struct clk *clk; | |
937 | ||
938 | /* New explicit name */ | |
939 | clk = devm_clk_get(dev, "txco"); | |
940 | if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER) | |
941 | return clk; | |
942 | ||
943 | /* Deprecated name */ | |
944 | clk = devm_clk_get(dev, "extclk"); | |
945 | if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER) | |
946 | return clk; | |
947 | ||
948 | /* Original code used no name at all */ | |
949 | return devm_clk_get(dev, NULL); | |
950 | } | |
951 | ||
42ef18f0 | 952 | static int bcm_get_resources(struct bcm_device *dev) |
0395ffc1 | 953 | { |
2b05393b | 954 | const struct dmi_system_id *dmi_id; |
75d11676 | 955 | int err; |
2b05393b | 956 | |
c0d3ce58 | 957 | dev->name = dev_name(dev->dev); |
0395ffc1 | 958 | |
4c33162c LW |
959 | if (x86_apple_machine && !bcm_apple_get_resources(dev)) |
960 | return 0; | |
961 | ||
55dbfcd0 | 962 | dev->txco_clk = bcm_get_txco(dev->dev); |
89ab37b4 | 963 | |
28ac03b9 | 964 | /* Handle deferred probing */ |
55dbfcd0 CYT |
965 | if (dev->txco_clk == ERR_PTR(-EPROBE_DEFER)) |
966 | return PTR_ERR(dev->txco_clk); | |
28ac03b9 | 967 | |
8c08947b | 968 | /* Ignore all other errors as before */ |
55dbfcd0 CYT |
969 | if (IS_ERR(dev->txco_clk)) |
970 | dev->txco_clk = NULL; | |
8c08947b | 971 | |
90bc07cc CYT |
972 | dev->lpo_clk = devm_clk_get(dev->dev, "lpo"); |
973 | if (dev->lpo_clk == ERR_PTR(-EPROBE_DEFER)) | |
974 | return PTR_ERR(dev->lpo_clk); | |
975 | ||
976 | if (IS_ERR(dev->lpo_clk)) | |
977 | dev->lpo_clk = NULL; | |
978 | ||
979 | /* Check if we accidentally fetched the lpo clock twice */ | |
980 | if (dev->lpo_clk && clk_is_match(dev->lpo_clk, dev->txco_clk)) { | |
981 | devm_clk_put(dev->dev, dev->txco_clk); | |
982 | dev->txco_clk = NULL; | |
983 | } | |
984 | ||
ab2f336c SW |
985 | dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup", |
986 | GPIOD_OUT_LOW); | |
62aaefa7 UKK |
987 | if (IS_ERR(dev->device_wakeup)) |
988 | return PTR_ERR(dev->device_wakeup); | |
0395ffc1 | 989 | |
ab2f336c SW |
990 | dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown", |
991 | GPIOD_OUT_LOW); | |
62aaefa7 UKK |
992 | if (IS_ERR(dev->shutdown)) |
993 | return PTR_ERR(dev->shutdown); | |
0395ffc1 | 994 | |
8353b4a6 LW |
995 | dev->set_device_wakeup = bcm_gpio_set_device_wakeup; |
996 | dev->set_shutdown = bcm_gpio_set_shutdown; | |
997 | ||
75d11676 CYT |
998 | dev->supplies[0].supply = "vbat"; |
999 | dev->supplies[1].supply = "vddio"; | |
1000 | err = devm_regulator_bulk_get(dev->dev, BCM_NUM_SUPPLIES, | |
1001 | dev->supplies); | |
1002 | if (err) | |
1003 | return err; | |
1004 | ||
6cc4396c | 1005 | /* IRQ can be declared in ACPI table as Interrupt or GpioInt */ |
6cc4396c FD |
1006 | if (dev->irq <= 0) { |
1007 | struct gpio_desc *gpio; | |
1008 | ||
c0d3ce58 | 1009 | gpio = devm_gpiod_get_optional(dev->dev, "host-wakeup", |
6cc4396c FD |
1010 | GPIOD_IN); |
1011 | if (IS_ERR(gpio)) | |
1012 | return PTR_ERR(gpio); | |
1013 | ||
1014 | dev->irq = gpiod_to_irq(gpio); | |
1015 | } | |
1016 | ||
2b05393b HG |
1017 | dmi_id = dmi_first_match(bcm_broken_irq_dmi_table); |
1018 | if (dmi_id) { | |
1019 | dev_info(dev->dev, "%s: Has a broken IRQ config, disabling IRQ support / runtime-pm\n", | |
1020 | dmi_id->ident); | |
1021 | dev->irq = 0; | |
1022 | } | |
1023 | ||
5954cdf1 | 1024 | dev_dbg(dev->dev, "BCM irq: %d\n", dev->irq); |
212d7183 AS |
1025 | return 0; |
1026 | } | |
1027 | ||
1028 | #ifdef CONFIG_ACPI | |
1029 | static int bcm_acpi_probe(struct bcm_device *dev) | |
1030 | { | |
212d7183 | 1031 | LIST_HEAD(resources); |
212d7183 | 1032 | const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios; |
9d54fd6a | 1033 | struct resource_entry *entry; |
212d7183 AS |
1034 | int ret; |
1035 | ||
ae056908 | 1036 | /* Retrieve UART ACPI info */ |
a4de1567 | 1037 | dev->gpio_int_idx = -1; |
c0d3ce58 | 1038 | ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev), |
e98d6d62 | 1039 | &resources, bcm_resource, dev); |
5be00284 JN |
1040 | if (ret < 0) |
1041 | return ret; | |
9d54fd6a HG |
1042 | |
1043 | resource_list_for_each_entry(entry, &resources) { | |
1044 | if (resource_type(entry->res) == IORESOURCE_IRQ) { | |
1045 | dev->irq = entry->res->start; | |
1046 | break; | |
1047 | } | |
1048 | } | |
09dbf1b7 | 1049 | acpi_dev_free_resource_list(&resources); |
ae056908 | 1050 | |
a4de1567 HG |
1051 | /* If the DSDT uses an Interrupt resource for the IRQ, then there are |
1052 | * only 2 GPIO resources, we use the irq-last mapping for this, since | |
1053 | * we already have an irq the 3th / last mapping will not be used. | |
1054 | */ | |
1055 | if (dev->irq) | |
1056 | gpio_mapping = acpi_bcm_int_last_gpios; | |
1057 | else if (dev->gpio_int_idx == 0) | |
1058 | gpio_mapping = acpi_bcm_int_first_gpios; | |
1059 | else if (dev->gpio_int_idx == 2) | |
1060 | gpio_mapping = acpi_bcm_int_last_gpios; | |
1061 | else | |
1062 | dev_warn(dev->dev, "Unexpected ACPI gpio_int_idx: %d\n", | |
1063 | dev->gpio_int_idx); | |
1064 | ||
1065 | /* Warn if our expectations are not met. */ | |
1066 | if (dev->gpio_count != (dev->irq ? 2 : 3)) | |
1067 | dev_warn(dev->dev, "Unexpected number of ACPI GPIOs: %d\n", | |
1068 | dev->gpio_count); | |
1069 | ||
1070 | ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping); | |
1071 | if (ret) | |
1072 | return ret; | |
1073 | ||
e09070c5 HG |
1074 | if (irq_polarity != -1) { |
1075 | dev->irq_active_low = irq_polarity; | |
1076 | dev_warn(dev->dev, "Overwriting IRQ polarity to active %s by module-param\n", | |
1077 | dev->irq_active_low ? "low" : "high"); | |
5cebdfea FD |
1078 | } |
1079 | ||
0395ffc1 FD |
1080 | return 0; |
1081 | } | |
50d78bcf FD |
1082 | #else |
1083 | static int bcm_acpi_probe(struct bcm_device *dev) | |
1084 | { | |
1085 | return -EINVAL; | |
1086 | } | |
1087 | #endif /* CONFIG_ACPI */ | |
0395ffc1 | 1088 | |
8a920568 HG |
1089 | static int bcm_of_probe(struct bcm_device *bdev) |
1090 | { | |
1091 | device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed); | |
1092 | return 0; | |
1093 | } | |
1094 | ||
0395ffc1 FD |
1095 | static int bcm_probe(struct platform_device *pdev) |
1096 | { | |
1097 | struct bcm_device *dev; | |
0395ffc1 FD |
1098 | int ret; |
1099 | ||
1100 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); | |
1101 | if (!dev) | |
1102 | return -ENOMEM; | |
1103 | ||
c0d3ce58 | 1104 | dev->dev = &pdev->dev; |
4a56f891 | 1105 | dev->irq = platform_get_irq(pdev, 0); |
0395ffc1 | 1106 | |
201762e2 | 1107 | if (has_acpi_companion(&pdev->dev)) { |
212d7183 | 1108 | ret = bcm_acpi_probe(dev); |
201762e2 HG |
1109 | if (ret) |
1110 | return ret; | |
1111 | } | |
1112 | ||
42ef18f0 | 1113 | ret = bcm_get_resources(dev); |
4d1c4558 JN |
1114 | if (ret) |
1115 | return ret; | |
0395ffc1 FD |
1116 | |
1117 | platform_set_drvdata(pdev, dev); | |
1118 | ||
1119 | dev_info(&pdev->dev, "%s device registered.\n", dev->name); | |
1120 | ||
1121 | /* Place this instance on the device list */ | |
bb3ea16a | 1122 | mutex_lock(&bcm_device_lock); |
0395ffc1 | 1123 | list_add_tail(&dev->list, &bcm_device_list); |
bb3ea16a | 1124 | mutex_unlock(&bcm_device_lock); |
0395ffc1 | 1125 | |
8bfa7e1e LW |
1126 | ret = bcm_gpio_set_power(dev, false); |
1127 | if (ret) | |
1128 | dev_err(&pdev->dev, "Failed to power down\n"); | |
0395ffc1 FD |
1129 | |
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | static int bcm_remove(struct platform_device *pdev) | |
1134 | { | |
1135 | struct bcm_device *dev = platform_get_drvdata(pdev); | |
1136 | ||
bb3ea16a | 1137 | mutex_lock(&bcm_device_lock); |
0395ffc1 | 1138 | list_del(&dev->list); |
bb3ea16a | 1139 | mutex_unlock(&bcm_device_lock); |
0395ffc1 | 1140 | |
0395ffc1 FD |
1141 | dev_info(&pdev->dev, "%s device unregistered.\n", dev->name); |
1142 | ||
1143 | return 0; | |
1144 | } | |
1145 | ||
bdd8818e MH |
1146 | static const struct hci_uart_proto bcm_proto = { |
1147 | .id = HCI_UART_BCM, | |
143f0a28 | 1148 | .name = "Broadcom", |
aee61f7a | 1149 | .manufacturer = 15, |
61b2fc2b | 1150 | .init_speed = 115200, |
bdd8818e MH |
1151 | .open = bcm_open, |
1152 | .close = bcm_close, | |
1153 | .flush = bcm_flush, | |
1154 | .setup = bcm_setup, | |
61b2fc2b | 1155 | .set_baudrate = bcm_set_baudrate, |
bdd8818e MH |
1156 | .recv = bcm_recv, |
1157 | .enqueue = bcm_enqueue, | |
1158 | .dequeue = bcm_dequeue, | |
1159 | }; | |
1160 | ||
0395ffc1 FD |
1161 | #ifdef CONFIG_ACPI |
1162 | static const struct acpi_device_id bcm_acpi_match[] = { | |
61121502 HG |
1163 | { "BCM2E00" }, |
1164 | { "BCM2E01" }, | |
1165 | { "BCM2E02" }, | |
1166 | { "BCM2E03" }, | |
1167 | { "BCM2E04" }, | |
1168 | { "BCM2E05" }, | |
1169 | { "BCM2E06" }, | |
1170 | { "BCM2E07" }, | |
1171 | { "BCM2E08" }, | |
1172 | { "BCM2E09" }, | |
1173 | { "BCM2E0A" }, | |
1174 | { "BCM2E0B" }, | |
1175 | { "BCM2E0C" }, | |
1176 | { "BCM2E0D" }, | |
1177 | { "BCM2E0E" }, | |
1178 | { "BCM2E0F" }, | |
1179 | { "BCM2E10" }, | |
1180 | { "BCM2E11" }, | |
1181 | { "BCM2E12" }, | |
1182 | { "BCM2E13" }, | |
1183 | { "BCM2E14" }, | |
1184 | { "BCM2E15" }, | |
1185 | { "BCM2E16" }, | |
1186 | { "BCM2E17" }, | |
1187 | { "BCM2E18" }, | |
1188 | { "BCM2E19" }, | |
a4de1567 | 1189 | { "BCM2E1A" }, |
61121502 HG |
1190 | { "BCM2E1B" }, |
1191 | { "BCM2E1C" }, | |
1192 | { "BCM2E1D" }, | |
1193 | { "BCM2E1F" }, | |
1194 | { "BCM2E20" }, | |
1195 | { "BCM2E21" }, | |
1196 | { "BCM2E22" }, | |
1197 | { "BCM2E23" }, | |
1198 | { "BCM2E24" }, | |
1199 | { "BCM2E25" }, | |
1200 | { "BCM2E26" }, | |
1201 | { "BCM2E27" }, | |
1202 | { "BCM2E28" }, | |
1203 | { "BCM2E29" }, | |
1204 | { "BCM2E2A" }, | |
1205 | { "BCM2E2B" }, | |
1206 | { "BCM2E2C" }, | |
1207 | { "BCM2E2D" }, | |
1208 | { "BCM2E2E" }, | |
1209 | { "BCM2E2F" }, | |
1210 | { "BCM2E30" }, | |
1211 | { "BCM2E31" }, | |
1212 | { "BCM2E32" }, | |
1213 | { "BCM2E33" }, | |
1214 | { "BCM2E34" }, | |
1215 | { "BCM2E35" }, | |
1216 | { "BCM2E36" }, | |
1217 | { "BCM2E37" }, | |
a4de1567 HG |
1218 | { "BCM2E38" }, |
1219 | { "BCM2E39" }, | |
1220 | { "BCM2E3A" }, | |
61121502 HG |
1221 | { "BCM2E3B" }, |
1222 | { "BCM2E3C" }, | |
a4de1567 | 1223 | { "BCM2E3D" }, |
61121502 | 1224 | { "BCM2E3E" }, |
a4de1567 HG |
1225 | { "BCM2E3F" }, |
1226 | { "BCM2E40" }, | |
61121502 HG |
1227 | { "BCM2E41" }, |
1228 | { "BCM2E42" }, | |
1229 | { "BCM2E43" }, | |
1230 | { "BCM2E44" }, | |
1231 | { "BCM2E45" }, | |
1232 | { "BCM2E46" }, | |
1233 | { "BCM2E47" }, | |
1234 | { "BCM2E48" }, | |
1235 | { "BCM2E49" }, | |
1236 | { "BCM2E4A" }, | |
1237 | { "BCM2E4B" }, | |
1238 | { "BCM2E4C" }, | |
1239 | { "BCM2E4D" }, | |
1240 | { "BCM2E4E" }, | |
1241 | { "BCM2E4F" }, | |
1242 | { "BCM2E50" }, | |
1243 | { "BCM2E51" }, | |
1244 | { "BCM2E52" }, | |
1245 | { "BCM2E53" }, | |
a4de1567 HG |
1246 | { "BCM2E54" }, |
1247 | { "BCM2E55" }, | |
61121502 HG |
1248 | { "BCM2E56" }, |
1249 | { "BCM2E57" }, | |
1250 | { "BCM2E58" }, | |
1251 | { "BCM2E59" }, | |
1252 | { "BCM2E5A" }, | |
1253 | { "BCM2E5B" }, | |
1254 | { "BCM2E5C" }, | |
1255 | { "BCM2E5D" }, | |
1256 | { "BCM2E5E" }, | |
1257 | { "BCM2E5F" }, | |
1258 | { "BCM2E60" }, | |
1259 | { "BCM2E61" }, | |
1260 | { "BCM2E62" }, | |
1261 | { "BCM2E63" }, | |
a4de1567 HG |
1262 | { "BCM2E64" }, |
1263 | { "BCM2E65" }, | |
61121502 | 1264 | { "BCM2E66" }, |
a4de1567 | 1265 | { "BCM2E67" }, |
61121502 HG |
1266 | { "BCM2E68" }, |
1267 | { "BCM2E69" }, | |
1268 | { "BCM2E6B" }, | |
1269 | { "BCM2E6D" }, | |
1270 | { "BCM2E6E" }, | |
1271 | { "BCM2E6F" }, | |
1272 | { "BCM2E70" }, | |
a4de1567 HG |
1273 | { "BCM2E71" }, |
1274 | { "BCM2E72" }, | |
61121502 | 1275 | { "BCM2E73" }, |
a4de1567 | 1276 | { "BCM2E74" }, |
61121502 HG |
1277 | { "BCM2E75" }, |
1278 | { "BCM2E76" }, | |
1279 | { "BCM2E77" }, | |
1280 | { "BCM2E78" }, | |
1281 | { "BCM2E79" }, | |
1282 | { "BCM2E7A" }, | |
a4de1567 HG |
1283 | { "BCM2E7B" }, |
1284 | { "BCM2E7C" }, | |
61121502 | 1285 | { "BCM2E7D" }, |
a4de1567 | 1286 | { "BCM2E7E" }, |
61121502 HG |
1287 | { "BCM2E7F" }, |
1288 | { "BCM2E80" }, | |
1289 | { "BCM2E81" }, | |
1290 | { "BCM2E82" }, | |
a4de1567 HG |
1291 | { "BCM2E83" }, |
1292 | { "BCM2E84" }, | |
61121502 HG |
1293 | { "BCM2E85" }, |
1294 | { "BCM2E86" }, | |
1295 | { "BCM2E87" }, | |
1296 | { "BCM2E88" }, | |
1297 | { "BCM2E89" }, | |
1298 | { "BCM2E8A" }, | |
1299 | { "BCM2E8B" }, | |
1300 | { "BCM2E8C" }, | |
1301 | { "BCM2E8D" }, | |
1302 | { "BCM2E8E" }, | |
a4de1567 | 1303 | { "BCM2E90" }, |
61121502 HG |
1304 | { "BCM2E92" }, |
1305 | { "BCM2E93" }, | |
1306 | { "BCM2E94" }, | |
a4de1567 HG |
1307 | { "BCM2E95" }, |
1308 | { "BCM2E96" }, | |
61121502 HG |
1309 | { "BCM2E97" }, |
1310 | { "BCM2E98" }, | |
1311 | { "BCM2E99" }, | |
1312 | { "BCM2E9A" }, | |
1313 | { "BCM2E9B" }, | |
1314 | { "BCM2E9C" }, | |
1315 | { "BCM2E9D" }, | |
1316 | { "BCM2EA0" }, | |
1317 | { "BCM2EA1" }, | |
1318 | { "BCM2EA2" }, | |
1319 | { "BCM2EA3" }, | |
a4de1567 | 1320 | { "BCM2EA4" }, |
61121502 HG |
1321 | { "BCM2EA5" }, |
1322 | { "BCM2EA6" }, | |
1323 | { "BCM2EA7" }, | |
1324 | { "BCM2EA8" }, | |
1325 | { "BCM2EA9" }, | |
a4de1567 | 1326 | { "BCM2EAA" }, |
61121502 HG |
1327 | { "BCM2EAB" }, |
1328 | { "BCM2EAC" }, | |
0395ffc1 FD |
1329 | { }, |
1330 | }; | |
1331 | MODULE_DEVICE_TABLE(acpi, bcm_acpi_match); | |
1332 | #endif | |
1333 | ||
8a920568 | 1334 | /* suspend and resume callbacks */ |
e88ab30d FD |
1335 | static const struct dev_pm_ops bcm_pm_ops = { |
1336 | SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume) | |
1337 | SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL) | |
1338 | }; | |
118612fb | 1339 | |
0395ffc1 FD |
1340 | static struct platform_driver bcm_driver = { |
1341 | .probe = bcm_probe, | |
1342 | .remove = bcm_remove, | |
1343 | .driver = { | |
1344 | .name = "hci_bcm", | |
1345 | .acpi_match_table = ACPI_PTR(bcm_acpi_match), | |
118612fb | 1346 | .pm = &bcm_pm_ops, |
0395ffc1 FD |
1347 | }, |
1348 | }; | |
1349 | ||
33cd149e LP |
1350 | static int bcm_serdev_probe(struct serdev_device *serdev) |
1351 | { | |
8a920568 | 1352 | struct bcm_device *bcmdev; |
33cd149e LP |
1353 | int err; |
1354 | ||
1355 | bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL); | |
1356 | if (!bcmdev) | |
1357 | return -ENOMEM; | |
1358 | ||
8a920568 | 1359 | bcmdev->dev = &serdev->dev; |
81a19053 | 1360 | #ifdef CONFIG_PM |
8a920568 | 1361 | bcmdev->hu = &bcmdev->serdev_hu; |
81a19053 | 1362 | #endif |
8a920568 | 1363 | bcmdev->serdev_hu.serdev = serdev; |
33cd149e LP |
1364 | serdev_device_set_drvdata(serdev, bcmdev); |
1365 | ||
8a920568 HG |
1366 | if (has_acpi_companion(&serdev->dev)) |
1367 | err = bcm_acpi_probe(bcmdev); | |
1368 | else | |
1369 | err = bcm_of_probe(bcmdev); | |
1370 | if (err) | |
1371 | return err; | |
33cd149e | 1372 | |
8a920568 HG |
1373 | err = bcm_get_resources(bcmdev); |
1374 | if (err) | |
1375 | return err; | |
1376 | ||
f3863f1d MH |
1377 | if (!bcmdev->shutdown) { |
1378 | dev_warn(&serdev->dev, | |
1379 | "No reset resource, using default baud rate\n"); | |
1380 | bcmdev->oper_speed = bcmdev->init_speed; | |
1381 | } | |
1382 | ||
8bfa7e1e LW |
1383 | err = bcm_gpio_set_power(bcmdev, false); |
1384 | if (err) | |
1385 | dev_err(&serdev->dev, "Failed to power down\n"); | |
8a920568 HG |
1386 | |
1387 | return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto); | |
33cd149e LP |
1388 | } |
1389 | ||
1390 | static void bcm_serdev_remove(struct serdev_device *serdev) | |
1391 | { | |
8a920568 | 1392 | struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev); |
33cd149e | 1393 | |
8a920568 | 1394 | hci_uart_unregister_device(&bcmdev->serdev_hu); |
33cd149e LP |
1395 | } |
1396 | ||
1397 | #ifdef CONFIG_OF | |
1398 | static const struct of_device_id bcm_bluetooth_of_match[] = { | |
92ffe0db | 1399 | { .compatible = "brcm,bcm20702a1" }, |
66904555 | 1400 | { .compatible = "brcm,bcm4330-bt" }, |
33cd149e LP |
1401 | { .compatible = "brcm,bcm43438-bt" }, |
1402 | { }, | |
1403 | }; | |
1404 | MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match); | |
1405 | #endif | |
1406 | ||
1407 | static struct serdev_device_driver bcm_serdev_driver = { | |
1408 | .probe = bcm_serdev_probe, | |
1409 | .remove = bcm_serdev_remove, | |
1410 | .driver = { | |
1411 | .name = "hci_uart_bcm", | |
1412 | .of_match_table = of_match_ptr(bcm_bluetooth_of_match), | |
8a920568 HG |
1413 | .acpi_match_table = ACPI_PTR(bcm_acpi_match), |
1414 | .pm = &bcm_pm_ops, | |
33cd149e LP |
1415 | }, |
1416 | }; | |
1417 | ||
bdd8818e MH |
1418 | int __init bcm_init(void) |
1419 | { | |
33cd149e LP |
1420 | /* For now, we need to keep both platform device |
1421 | * driver (ACPI generated) and serdev driver (DT). | |
1422 | */ | |
0395ffc1 | 1423 | platform_driver_register(&bcm_driver); |
33cd149e | 1424 | serdev_device_driver_register(&bcm_serdev_driver); |
0395ffc1 | 1425 | |
bdd8818e MH |
1426 | return hci_uart_register_proto(&bcm_proto); |
1427 | } | |
1428 | ||
1429 | int __exit bcm_deinit(void) | |
1430 | { | |
0395ffc1 | 1431 | platform_driver_unregister(&bcm_driver); |
33cd149e | 1432 | serdev_device_driver_unregister(&bcm_serdev_driver); |
0395ffc1 | 1433 | |
bdd8818e MH |
1434 | return hci_uart_unregister_proto(&bcm_proto); |
1435 | } |