rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 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> |
| 27 | #include <linux/firmware.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/acpi.h> |
| 30 | #include <linux/of.h> |
| 31 | #include <linux/property.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/clk.h> |
| 34 | #include <linux/gpio/consumer.h> |
| 35 | #include <linux/tty.h> |
| 36 | #include <linux/interrupt.h> |
| 37 | #include <linux/dmi.h> |
| 38 | #include <linux/pm_runtime.h> |
| 39 | #include <linux/serdev.h> |
| 40 | |
| 41 | #include <net/bluetooth/bluetooth.h> |
| 42 | #include <net/bluetooth/hci_core.h> |
| 43 | |
| 44 | #include "btbcm.h" |
| 45 | #include "hci_uart.h" |
| 46 | |
| 47 | #define BCM_NULL_PKT 0x00 |
| 48 | #define BCM_NULL_SIZE 0 |
| 49 | |
| 50 | #define BCM_LM_DIAG_PKT 0x07 |
| 51 | #define BCM_LM_DIAG_SIZE 63 |
| 52 | |
| 53 | #define BCM_TYPE49_PKT 0x31 |
| 54 | #define BCM_TYPE49_SIZE 0 |
| 55 | |
| 56 | #define BCM_TYPE52_PKT 0x34 |
| 57 | #define BCM_TYPE52_SIZE 0 |
| 58 | |
| 59 | #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */ |
| 60 | |
| 61 | /* platform device driver resources */ |
| 62 | struct bcm_device { |
| 63 | struct list_head list; |
| 64 | |
| 65 | struct platform_device *pdev; |
| 66 | |
| 67 | const char *name; |
| 68 | struct gpio_desc *device_wakeup; |
| 69 | struct gpio_desc *shutdown; |
| 70 | |
| 71 | struct clk *clk; |
| 72 | bool clk_enabled; |
| 73 | |
| 74 | u32 init_speed; |
| 75 | u32 oper_speed; |
| 76 | int irq; |
| 77 | bool irq_active_low; |
| 78 | |
| 79 | #ifdef CONFIG_PM |
| 80 | struct hci_uart *hu; |
| 81 | bool is_suspended; /* suspend/resume flag */ |
| 82 | #endif |
| 83 | }; |
| 84 | |
| 85 | /* serdev driver resources */ |
| 86 | struct bcm_serdev { |
| 87 | struct hci_uart hu; |
| 88 | }; |
| 89 | |
| 90 | /* generic bcm uart resources */ |
| 91 | struct bcm_data { |
| 92 | struct sk_buff *rx_skb; |
| 93 | struct sk_buff_head txq; |
| 94 | |
| 95 | struct bcm_device *dev; |
| 96 | }; |
| 97 | |
| 98 | /* List of BCM BT UART devices */ |
| 99 | static DEFINE_MUTEX(bcm_device_lock); |
| 100 | static LIST_HEAD(bcm_device_list); |
| 101 | |
| 102 | static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed) |
| 103 | { |
| 104 | if (hu->serdev) |
| 105 | serdev_device_set_baudrate(hu->serdev, speed); |
| 106 | else |
| 107 | hci_uart_set_baudrate(hu, speed); |
| 108 | } |
| 109 | |
| 110 | static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed) |
| 111 | { |
| 112 | struct hci_dev *hdev = hu->hdev; |
| 113 | struct sk_buff *skb; |
| 114 | struct bcm_update_uart_baud_rate param; |
| 115 | |
| 116 | if (speed > 3000000) { |
| 117 | struct bcm_write_uart_clock_setting clock; |
| 118 | |
| 119 | clock.type = BCM_UART_CLOCK_48MHZ; |
| 120 | |
| 121 | bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type); |
| 122 | |
| 123 | /* This Broadcom specific command changes the UART's controller |
| 124 | * clock for baud rate > 3000000. |
| 125 | */ |
| 126 | skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT); |
| 127 | if (IS_ERR(skb)) { |
| 128 | int err = PTR_ERR(skb); |
| 129 | bt_dev_err(hdev, "BCM: failed to write clock (%d)", |
| 130 | err); |
| 131 | return err; |
| 132 | } |
| 133 | |
| 134 | kfree_skb(skb); |
| 135 | } |
| 136 | |
| 137 | bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed); |
| 138 | |
| 139 | param.zero = cpu_to_le16(0); |
| 140 | param.baud_rate = cpu_to_le32(speed); |
| 141 | |
| 142 | /* This Broadcom specific command changes the UART's controller baud |
| 143 | * rate. |
| 144 | */ |
| 145 | skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), ¶m, |
| 146 | HCI_INIT_TIMEOUT); |
| 147 | if (IS_ERR(skb)) { |
| 148 | int err = PTR_ERR(skb); |
| 149 | bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)", |
| 150 | err); |
| 151 | return err; |
| 152 | } |
| 153 | |
| 154 | kfree_skb(skb); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* bcm_device_exists should be protected by bcm_device_lock */ |
| 160 | static bool bcm_device_exists(struct bcm_device *device) |
| 161 | { |
| 162 | struct list_head *p; |
| 163 | |
| 164 | list_for_each(p, &bcm_device_list) { |
| 165 | struct bcm_device *dev = list_entry(p, struct bcm_device, list); |
| 166 | |
| 167 | if (device == dev) |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | static int bcm_gpio_set_power(struct bcm_device *dev, bool powered) |
| 175 | { |
| 176 | if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled) |
| 177 | clk_prepare_enable(dev->clk); |
| 178 | |
| 179 | gpiod_set_value(dev->shutdown, powered); |
| 180 | gpiod_set_value(dev->device_wakeup, powered); |
| 181 | |
| 182 | if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled) |
| 183 | clk_disable_unprepare(dev->clk); |
| 184 | |
| 185 | dev->clk_enabled = powered; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | #ifdef CONFIG_PM |
| 191 | static irqreturn_t bcm_host_wake(int irq, void *data) |
| 192 | { |
| 193 | struct bcm_device *bdev = data; |
| 194 | |
| 195 | bt_dev_dbg(bdev, "Host wake IRQ"); |
| 196 | |
| 197 | pm_runtime_get(&bdev->pdev->dev); |
| 198 | pm_runtime_mark_last_busy(&bdev->pdev->dev); |
| 199 | pm_runtime_put_autosuspend(&bdev->pdev->dev); |
| 200 | |
| 201 | return IRQ_HANDLED; |
| 202 | } |
| 203 | |
| 204 | static int bcm_request_irq(struct bcm_data *bcm) |
| 205 | { |
| 206 | struct bcm_device *bdev = bcm->dev; |
| 207 | int err; |
| 208 | |
| 209 | /* If this is not a platform device, do not enable PM functionalities */ |
| 210 | mutex_lock(&bcm_device_lock); |
| 211 | if (!bcm_device_exists(bdev)) { |
| 212 | err = -ENODEV; |
| 213 | goto unlock; |
| 214 | } |
| 215 | |
| 216 | if (bdev->irq <= 0) { |
| 217 | err = -EOPNOTSUPP; |
| 218 | goto unlock; |
| 219 | } |
| 220 | |
| 221 | err = devm_request_irq(&bdev->pdev->dev, bdev->irq, bcm_host_wake, |
| 222 | bdev->irq_active_low ? IRQF_TRIGGER_FALLING : |
| 223 | IRQF_TRIGGER_RISING, |
| 224 | "host_wake", bdev); |
| 225 | if (err) |
| 226 | goto unlock; |
| 227 | |
| 228 | device_init_wakeup(&bdev->pdev->dev, true); |
| 229 | |
| 230 | pm_runtime_set_autosuspend_delay(&bdev->pdev->dev, |
| 231 | BCM_AUTOSUSPEND_DELAY); |
| 232 | pm_runtime_use_autosuspend(&bdev->pdev->dev); |
| 233 | pm_runtime_set_active(&bdev->pdev->dev); |
| 234 | pm_runtime_enable(&bdev->pdev->dev); |
| 235 | |
| 236 | unlock: |
| 237 | mutex_unlock(&bcm_device_lock); |
| 238 | |
| 239 | return err; |
| 240 | } |
| 241 | |
| 242 | static const struct bcm_set_sleep_mode default_sleep_params = { |
| 243 | .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */ |
| 244 | .idle_host = 2, /* idle threshold HOST, in 300ms */ |
| 245 | .idle_dev = 2, /* idle threshold device, in 300ms */ |
| 246 | .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */ |
| 247 | .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */ |
| 248 | .allow_host_sleep = 1, /* Allow host sleep in SCO flag */ |
| 249 | .combine_modes = 1, /* Combine sleep and LPM flag */ |
| 250 | .tristate_control = 0, /* Allow tri-state control of UART tx flag */ |
| 251 | /* Irrelevant USB flags */ |
| 252 | .usb_auto_sleep = 0, |
| 253 | .usb_resume_timeout = 0, |
| 254 | .pulsed_host_wake = 0, |
| 255 | .break_to_host = 0 |
| 256 | }; |
| 257 | |
| 258 | static int bcm_setup_sleep(struct hci_uart *hu) |
| 259 | { |
| 260 | struct bcm_data *bcm = hu->priv; |
| 261 | struct sk_buff *skb; |
| 262 | struct bcm_set_sleep_mode sleep_params = default_sleep_params; |
| 263 | |
| 264 | sleep_params.host_wake_active = !bcm->dev->irq_active_low; |
| 265 | |
| 266 | skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params), |
| 267 | &sleep_params, HCI_INIT_TIMEOUT); |
| 268 | if (IS_ERR(skb)) { |
| 269 | int err = PTR_ERR(skb); |
| 270 | bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err); |
| 271 | return err; |
| 272 | } |
| 273 | kfree_skb(skb); |
| 274 | |
| 275 | bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded"); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | #else |
| 280 | static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; } |
| 281 | static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; } |
| 282 | #endif |
| 283 | |
| 284 | static int bcm_set_diag(struct hci_dev *hdev, bool enable) |
| 285 | { |
| 286 | struct hci_uart *hu = hci_get_drvdata(hdev); |
| 287 | struct bcm_data *bcm = hu->priv; |
| 288 | struct sk_buff *skb; |
| 289 | |
| 290 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 291 | return -ENETDOWN; |
| 292 | |
| 293 | skb = bt_skb_alloc(3, GFP_KERNEL); |
| 294 | if (!skb) |
| 295 | return -ENOMEM; |
| 296 | |
| 297 | skb_put_u8(skb, BCM_LM_DIAG_PKT); |
| 298 | skb_put_u8(skb, 0xf0); |
| 299 | skb_put_u8(skb, enable); |
| 300 | |
| 301 | skb_queue_tail(&bcm->txq, skb); |
| 302 | hci_uart_tx_wakeup(hu); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int bcm_open(struct hci_uart *hu) |
| 308 | { |
| 309 | struct bcm_data *bcm; |
| 310 | struct list_head *p; |
| 311 | |
| 312 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
| 313 | |
| 314 | if (!hci_uart_has_flow_control(hu)) |
| 315 | return -EOPNOTSUPP; |
| 316 | |
| 317 | bcm = kzalloc(sizeof(*bcm), GFP_KERNEL); |
| 318 | if (!bcm) |
| 319 | return -ENOMEM; |
| 320 | |
| 321 | skb_queue_head_init(&bcm->txq); |
| 322 | |
| 323 | hu->priv = bcm; |
| 324 | |
| 325 | /* If this is a serdev defined device, then only use |
| 326 | * serdev open primitive and skip the rest. |
| 327 | */ |
| 328 | if (hu->serdev) { |
| 329 | serdev_device_open(hu->serdev); |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | if (!hu->tty->dev) |
| 334 | goto out; |
| 335 | |
| 336 | mutex_lock(&bcm_device_lock); |
| 337 | list_for_each(p, &bcm_device_list) { |
| 338 | struct bcm_device *dev = list_entry(p, struct bcm_device, list); |
| 339 | |
| 340 | /* Retrieve saved bcm_device based on parent of the |
| 341 | * platform device (saved during device probe) and |
| 342 | * parent of tty device used by hci_uart |
| 343 | */ |
| 344 | if (hu->tty->dev->parent == dev->pdev->dev.parent) { |
| 345 | bcm->dev = dev; |
| 346 | hu->init_speed = dev->init_speed; |
| 347 | hu->oper_speed = dev->oper_speed; |
| 348 | #ifdef CONFIG_PM |
| 349 | dev->hu = hu; |
| 350 | #endif |
| 351 | bcm_gpio_set_power(bcm->dev, true); |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | mutex_unlock(&bcm_device_lock); |
| 357 | out: |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int bcm_close(struct hci_uart *hu) |
| 362 | { |
| 363 | struct bcm_data *bcm = hu->priv; |
| 364 | struct bcm_device *bdev = bcm->dev; |
| 365 | |
| 366 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
| 367 | |
| 368 | /* If this is a serdev defined device, only use serdev |
| 369 | * close primitive and then continue as usual. |
| 370 | */ |
| 371 | if (hu->serdev) |
| 372 | serdev_device_close(hu->serdev); |
| 373 | |
| 374 | /* Protect bcm->dev against removal of the device or driver */ |
| 375 | mutex_lock(&bcm_device_lock); |
| 376 | if (bcm_device_exists(bdev)) { |
| 377 | bcm_gpio_set_power(bdev, false); |
| 378 | #ifdef CONFIG_PM |
| 379 | pm_runtime_disable(&bdev->pdev->dev); |
| 380 | pm_runtime_set_suspended(&bdev->pdev->dev); |
| 381 | |
| 382 | if (device_can_wakeup(&bdev->pdev->dev)) { |
| 383 | devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev); |
| 384 | device_init_wakeup(&bdev->pdev->dev, false); |
| 385 | } |
| 386 | |
| 387 | bdev->hu = NULL; |
| 388 | #endif |
| 389 | } |
| 390 | mutex_unlock(&bcm_device_lock); |
| 391 | |
| 392 | skb_queue_purge(&bcm->txq); |
| 393 | kfree_skb(bcm->rx_skb); |
| 394 | kfree(bcm); |
| 395 | |
| 396 | hu->priv = NULL; |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static int bcm_flush(struct hci_uart *hu) |
| 401 | { |
| 402 | struct bcm_data *bcm = hu->priv; |
| 403 | |
| 404 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
| 405 | |
| 406 | skb_queue_purge(&bcm->txq); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int bcm_setup(struct hci_uart *hu) |
| 412 | { |
| 413 | struct bcm_data *bcm = hu->priv; |
| 414 | char fw_name[64]; |
| 415 | const struct firmware *fw; |
| 416 | unsigned int speed; |
| 417 | int err; |
| 418 | |
| 419 | bt_dev_dbg(hu->hdev, "hu %p", hu); |
| 420 | |
| 421 | hu->hdev->set_diag = bcm_set_diag; |
| 422 | hu->hdev->set_bdaddr = btbcm_set_bdaddr; |
| 423 | |
| 424 | err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name)); |
| 425 | if (err) |
| 426 | return err; |
| 427 | |
| 428 | err = request_firmware(&fw, fw_name, &hu->hdev->dev); |
| 429 | if (err < 0) { |
| 430 | bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name); |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | err = btbcm_patchram(hu->hdev, fw); |
| 435 | if (err) { |
| 436 | bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err); |
| 437 | goto finalize; |
| 438 | } |
| 439 | |
| 440 | /* Init speed if any */ |
| 441 | if (hu->init_speed) |
| 442 | speed = hu->init_speed; |
| 443 | else if (hu->proto->init_speed) |
| 444 | speed = hu->proto->init_speed; |
| 445 | else |
| 446 | speed = 0; |
| 447 | |
| 448 | if (speed) |
| 449 | host_set_baudrate(hu, speed); |
| 450 | |
| 451 | /* Operational speed if any */ |
| 452 | if (hu->oper_speed) |
| 453 | speed = hu->oper_speed; |
| 454 | else if (hu->proto->oper_speed) |
| 455 | speed = hu->proto->oper_speed; |
| 456 | else |
| 457 | speed = 0; |
| 458 | |
| 459 | if (speed) { |
| 460 | err = bcm_set_baudrate(hu, speed); |
| 461 | if (!err) |
| 462 | host_set_baudrate(hu, speed); |
| 463 | } |
| 464 | |
| 465 | finalize: |
| 466 | release_firmware(fw); |
| 467 | |
| 468 | err = btbcm_finalize(hu->hdev); |
| 469 | if (err) |
| 470 | return err; |
| 471 | |
| 472 | if (!bcm_request_irq(bcm)) |
| 473 | err = bcm_setup_sleep(hu); |
| 474 | |
| 475 | return err; |
| 476 | } |
| 477 | |
| 478 | #define BCM_RECV_LM_DIAG \ |
| 479 | .type = BCM_LM_DIAG_PKT, \ |
| 480 | .hlen = BCM_LM_DIAG_SIZE, \ |
| 481 | .loff = 0, \ |
| 482 | .lsize = 0, \ |
| 483 | .maxlen = BCM_LM_DIAG_SIZE |
| 484 | |
| 485 | #define BCM_RECV_NULL \ |
| 486 | .type = BCM_NULL_PKT, \ |
| 487 | .hlen = BCM_NULL_SIZE, \ |
| 488 | .loff = 0, \ |
| 489 | .lsize = 0, \ |
| 490 | .maxlen = BCM_NULL_SIZE |
| 491 | |
| 492 | #define BCM_RECV_TYPE49 \ |
| 493 | .type = BCM_TYPE49_PKT, \ |
| 494 | .hlen = BCM_TYPE49_SIZE, \ |
| 495 | .loff = 0, \ |
| 496 | .lsize = 0, \ |
| 497 | .maxlen = BCM_TYPE49_SIZE |
| 498 | |
| 499 | #define BCM_RECV_TYPE52 \ |
| 500 | .type = BCM_TYPE52_PKT, \ |
| 501 | .hlen = BCM_TYPE52_SIZE, \ |
| 502 | .loff = 0, \ |
| 503 | .lsize = 0, \ |
| 504 | .maxlen = BCM_TYPE52_SIZE |
| 505 | |
| 506 | static const struct h4_recv_pkt bcm_recv_pkts[] = { |
| 507 | { H4_RECV_ACL, .recv = hci_recv_frame }, |
| 508 | { H4_RECV_SCO, .recv = hci_recv_frame }, |
| 509 | { H4_RECV_EVENT, .recv = hci_recv_frame }, |
| 510 | { BCM_RECV_LM_DIAG, .recv = hci_recv_diag }, |
| 511 | { BCM_RECV_NULL, .recv = hci_recv_diag }, |
| 512 | { BCM_RECV_TYPE49, .recv = hci_recv_diag }, |
| 513 | { BCM_RECV_TYPE52, .recv = hci_recv_diag }, |
| 514 | }; |
| 515 | |
| 516 | static int bcm_recv(struct hci_uart *hu, const void *data, int count) |
| 517 | { |
| 518 | struct bcm_data *bcm = hu->priv; |
| 519 | |
| 520 | if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) |
| 521 | return -EUNATCH; |
| 522 | |
| 523 | bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count, |
| 524 | bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts)); |
| 525 | if (IS_ERR(bcm->rx_skb)) { |
| 526 | int err = PTR_ERR(bcm->rx_skb); |
| 527 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
| 528 | bcm->rx_skb = NULL; |
| 529 | return err; |
| 530 | } else if (!bcm->rx_skb) { |
| 531 | /* Delay auto-suspend when receiving completed packet */ |
| 532 | mutex_lock(&bcm_device_lock); |
| 533 | if (bcm->dev && bcm_device_exists(bcm->dev)) { |
| 534 | pm_runtime_get(&bcm->dev->pdev->dev); |
| 535 | pm_runtime_mark_last_busy(&bcm->dev->pdev->dev); |
| 536 | pm_runtime_put_autosuspend(&bcm->dev->pdev->dev); |
| 537 | } |
| 538 | mutex_unlock(&bcm_device_lock); |
| 539 | } |
| 540 | |
| 541 | return count; |
| 542 | } |
| 543 | |
| 544 | static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 545 | { |
| 546 | struct bcm_data *bcm = hu->priv; |
| 547 | |
| 548 | bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb); |
| 549 | |
| 550 | /* Prepend skb with frame type */ |
| 551 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
| 552 | skb_queue_tail(&bcm->txq, skb); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static struct sk_buff *bcm_dequeue(struct hci_uart *hu) |
| 558 | { |
| 559 | struct bcm_data *bcm = hu->priv; |
| 560 | struct sk_buff *skb = NULL; |
| 561 | struct bcm_device *bdev = NULL; |
| 562 | |
| 563 | mutex_lock(&bcm_device_lock); |
| 564 | |
| 565 | if (bcm_device_exists(bcm->dev)) { |
| 566 | bdev = bcm->dev; |
| 567 | pm_runtime_get_sync(&bdev->pdev->dev); |
| 568 | /* Shall be resumed here */ |
| 569 | } |
| 570 | |
| 571 | skb = skb_dequeue(&bcm->txq); |
| 572 | |
| 573 | if (bdev) { |
| 574 | pm_runtime_mark_last_busy(&bdev->pdev->dev); |
| 575 | pm_runtime_put_autosuspend(&bdev->pdev->dev); |
| 576 | } |
| 577 | |
| 578 | mutex_unlock(&bcm_device_lock); |
| 579 | |
| 580 | return skb; |
| 581 | } |
| 582 | |
| 583 | #ifdef CONFIG_PM |
| 584 | static int bcm_suspend_device(struct device *dev) |
| 585 | { |
| 586 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); |
| 587 | |
| 588 | bt_dev_dbg(bdev, ""); |
| 589 | |
| 590 | if (!bdev->is_suspended && bdev->hu) { |
| 591 | hci_uart_set_flow_control(bdev->hu, true); |
| 592 | |
| 593 | /* Once this returns, driver suspends BT via GPIO */ |
| 594 | bdev->is_suspended = true; |
| 595 | } |
| 596 | |
| 597 | /* Suspend the device */ |
| 598 | if (bdev->device_wakeup) { |
| 599 | gpiod_set_value(bdev->device_wakeup, false); |
| 600 | bt_dev_dbg(bdev, "suspend, delaying 15 ms"); |
| 601 | mdelay(15); |
| 602 | } |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static int bcm_resume_device(struct device *dev) |
| 608 | { |
| 609 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); |
| 610 | |
| 611 | bt_dev_dbg(bdev, ""); |
| 612 | |
| 613 | if (bdev->device_wakeup) { |
| 614 | gpiod_set_value(bdev->device_wakeup, true); |
| 615 | bt_dev_dbg(bdev, "resume, delaying 15 ms"); |
| 616 | mdelay(15); |
| 617 | } |
| 618 | |
| 619 | /* When this executes, the device has woken up already */ |
| 620 | if (bdev->is_suspended && bdev->hu) { |
| 621 | bdev->is_suspended = false; |
| 622 | |
| 623 | hci_uart_set_flow_control(bdev->hu, false); |
| 624 | } |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | #endif |
| 629 | |
| 630 | #ifdef CONFIG_PM_SLEEP |
| 631 | /* Platform suspend callback */ |
| 632 | static int bcm_suspend(struct device *dev) |
| 633 | { |
| 634 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); |
| 635 | int error; |
| 636 | |
| 637 | bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended); |
| 638 | |
| 639 | /* bcm_suspend can be called at any time as long as platform device is |
| 640 | * bound, so it should use bcm_device_lock to protect access to hci_uart |
| 641 | * and device_wake-up GPIO. |
| 642 | */ |
| 643 | mutex_lock(&bcm_device_lock); |
| 644 | |
| 645 | if (!bdev->hu) |
| 646 | goto unlock; |
| 647 | |
| 648 | if (pm_runtime_active(dev)) |
| 649 | bcm_suspend_device(dev); |
| 650 | |
| 651 | if (device_may_wakeup(&bdev->pdev->dev)) { |
| 652 | error = enable_irq_wake(bdev->irq); |
| 653 | if (!error) |
| 654 | bt_dev_dbg(bdev, "BCM irq: enabled"); |
| 655 | } |
| 656 | |
| 657 | unlock: |
| 658 | mutex_unlock(&bcm_device_lock); |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | /* Platform resume callback */ |
| 664 | static int bcm_resume(struct device *dev) |
| 665 | { |
| 666 | struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev)); |
| 667 | |
| 668 | bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended); |
| 669 | |
| 670 | /* bcm_resume can be called at any time as long as platform device is |
| 671 | * bound, so it should use bcm_device_lock to protect access to hci_uart |
| 672 | * and device_wake-up GPIO. |
| 673 | */ |
| 674 | mutex_lock(&bcm_device_lock); |
| 675 | |
| 676 | if (!bdev->hu) |
| 677 | goto unlock; |
| 678 | |
| 679 | if (device_may_wakeup(&bdev->pdev->dev)) { |
| 680 | disable_irq_wake(bdev->irq); |
| 681 | bt_dev_dbg(bdev, "BCM irq: disabled"); |
| 682 | } |
| 683 | |
| 684 | bcm_resume_device(dev); |
| 685 | |
| 686 | unlock: |
| 687 | mutex_unlock(&bcm_device_lock); |
| 688 | |
| 689 | pm_runtime_disable(dev); |
| 690 | pm_runtime_set_active(dev); |
| 691 | pm_runtime_enable(dev); |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | #endif |
| 696 | |
| 697 | static const struct acpi_gpio_params int_last_device_wakeup_gpios = { 0, 0, false }; |
| 698 | static const struct acpi_gpio_params int_last_shutdown_gpios = { 1, 0, false }; |
| 699 | static const struct acpi_gpio_params int_last_host_wakeup_gpios = { 2, 0, false }; |
| 700 | |
| 701 | static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = { |
| 702 | { "device-wakeup-gpios", &int_last_device_wakeup_gpios, 1 }, |
| 703 | { "shutdown-gpios", &int_last_shutdown_gpios, 1 }, |
| 704 | { "host-wakeup-gpios", &int_last_host_wakeup_gpios, 1 }, |
| 705 | { }, |
| 706 | }; |
| 707 | |
| 708 | static const struct acpi_gpio_params int_first_host_wakeup_gpios = { 0, 0, false }; |
| 709 | static const struct acpi_gpio_params int_first_device_wakeup_gpios = { 1, 0, false }; |
| 710 | static const struct acpi_gpio_params int_first_shutdown_gpios = { 2, 0, false }; |
| 711 | |
| 712 | static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = { |
| 713 | { "device-wakeup-gpios", &int_first_device_wakeup_gpios, 1 }, |
| 714 | { "shutdown-gpios", &int_first_shutdown_gpios, 1 }, |
| 715 | { "host-wakeup-gpios", &int_first_host_wakeup_gpios, 1 }, |
| 716 | { }, |
| 717 | }; |
| 718 | |
| 719 | #ifdef CONFIG_ACPI |
| 720 | /* IRQ polarity of some chipsets are not defined correctly in ACPI table. */ |
| 721 | static const struct dmi_system_id bcm_active_low_irq_dmi_table[] = { |
| 722 | { /* Handle ThinkPad 8 tablets with BCM2E55 chipset ACPI ID */ |
| 723 | .ident = "Lenovo ThinkPad 8", |
| 724 | .matches = { |
| 725 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 726 | DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "ThinkPad 8"), |
| 727 | }, |
| 728 | }, |
| 729 | { } |
| 730 | }; |
| 731 | |
| 732 | static int bcm_resource(struct acpi_resource *ares, void *data) |
| 733 | { |
| 734 | struct bcm_device *dev = data; |
| 735 | struct acpi_resource_extended_irq *irq; |
| 736 | struct acpi_resource_gpio *gpio; |
| 737 | struct acpi_resource_uart_serialbus *sb; |
| 738 | |
| 739 | switch (ares->type) { |
| 740 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: |
| 741 | irq = &ares->data.extended_irq; |
| 742 | if (irq->polarity != ACPI_ACTIVE_LOW) |
| 743 | dev_info(&dev->pdev->dev, "ACPI Interrupt resource is active-high, this is usually wrong, treating the IRQ as active-low\n"); |
| 744 | dev->irq_active_low = true; |
| 745 | break; |
| 746 | |
| 747 | case ACPI_RESOURCE_TYPE_GPIO: |
| 748 | gpio = &ares->data.gpio; |
| 749 | if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) |
| 750 | dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW; |
| 751 | break; |
| 752 | |
| 753 | case ACPI_RESOURCE_TYPE_SERIAL_BUS: |
| 754 | sb = &ares->data.uart_serial_bus; |
| 755 | if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) { |
| 756 | dev->init_speed = sb->default_baud_rate; |
| 757 | dev->oper_speed = 4000000; |
| 758 | } |
| 759 | break; |
| 760 | |
| 761 | default: |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | /* Always tell the ACPI core to skip this resource */ |
| 766 | return 1; |
| 767 | } |
| 768 | #endif /* CONFIG_ACPI */ |
| 769 | |
| 770 | static int bcm_platform_probe(struct bcm_device *dev) |
| 771 | { |
| 772 | struct platform_device *pdev = dev->pdev; |
| 773 | |
| 774 | dev->name = dev_name(&pdev->dev); |
| 775 | |
| 776 | dev->clk = devm_clk_get(&pdev->dev, NULL); |
| 777 | |
| 778 | dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev, |
| 779 | "device-wakeup", |
| 780 | GPIOD_OUT_LOW); |
| 781 | if (IS_ERR(dev->device_wakeup)) |
| 782 | return PTR_ERR(dev->device_wakeup); |
| 783 | |
| 784 | dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown", |
| 785 | GPIOD_OUT_LOW); |
| 786 | if (IS_ERR(dev->shutdown)) |
| 787 | return PTR_ERR(dev->shutdown); |
| 788 | |
| 789 | /* IRQ can be declared in ACPI table as Interrupt or GpioInt */ |
| 790 | dev->irq = platform_get_irq(pdev, 0); |
| 791 | if (dev->irq <= 0) { |
| 792 | struct gpio_desc *gpio; |
| 793 | |
| 794 | gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup", |
| 795 | GPIOD_IN); |
| 796 | if (IS_ERR(gpio)) |
| 797 | return PTR_ERR(gpio); |
| 798 | |
| 799 | dev->irq = gpiod_to_irq(gpio); |
| 800 | } |
| 801 | |
| 802 | dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq); |
| 803 | |
| 804 | /* Make sure at-least one of the GPIO is defined and that |
| 805 | * a name is specified for this instance |
| 806 | */ |
| 807 | if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) { |
| 808 | dev_err(&pdev->dev, "invalid platform data\n"); |
| 809 | return -EINVAL; |
| 810 | } |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | #ifdef CONFIG_ACPI |
| 816 | static int bcm_acpi_probe(struct bcm_device *dev) |
| 817 | { |
| 818 | struct platform_device *pdev = dev->pdev; |
| 819 | LIST_HEAD(resources); |
| 820 | const struct dmi_system_id *dmi_id; |
| 821 | const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios; |
| 822 | const struct acpi_device_id *id; |
| 823 | int ret; |
| 824 | |
| 825 | /* Retrieve GPIO data */ |
| 826 | id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev); |
| 827 | if (id) |
| 828 | gpio_mapping = (const struct acpi_gpio_mapping *) id->driver_data; |
| 829 | |
| 830 | ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, gpio_mapping); |
| 831 | if (ret) |
| 832 | return ret; |
| 833 | |
| 834 | ret = bcm_platform_probe(dev); |
| 835 | if (ret) |
| 836 | return ret; |
| 837 | |
| 838 | /* Retrieve UART ACPI info */ |
| 839 | ret = acpi_dev_get_resources(ACPI_COMPANION(&dev->pdev->dev), |
| 840 | &resources, bcm_resource, dev); |
| 841 | if (ret < 0) |
| 842 | return ret; |
| 843 | acpi_dev_free_resource_list(&resources); |
| 844 | |
| 845 | dmi_id = dmi_first_match(bcm_active_low_irq_dmi_table); |
| 846 | if (dmi_id) { |
| 847 | bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low", |
| 848 | dmi_id->ident); |
| 849 | dev->irq_active_low = true; |
| 850 | } |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | #else |
| 855 | static int bcm_acpi_probe(struct bcm_device *dev) |
| 856 | { |
| 857 | return -EINVAL; |
| 858 | } |
| 859 | #endif /* CONFIG_ACPI */ |
| 860 | |
| 861 | static int bcm_probe(struct platform_device *pdev) |
| 862 | { |
| 863 | struct bcm_device *dev; |
| 864 | int ret; |
| 865 | |
| 866 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
| 867 | if (!dev) |
| 868 | return -ENOMEM; |
| 869 | |
| 870 | dev->pdev = pdev; |
| 871 | |
| 872 | if (has_acpi_companion(&pdev->dev)) |
| 873 | ret = bcm_acpi_probe(dev); |
| 874 | else |
| 875 | ret = bcm_platform_probe(dev); |
| 876 | if (ret) |
| 877 | return ret; |
| 878 | |
| 879 | platform_set_drvdata(pdev, dev); |
| 880 | |
| 881 | dev_info(&pdev->dev, "%s device registered.\n", dev->name); |
| 882 | |
| 883 | /* Place this instance on the device list */ |
| 884 | mutex_lock(&bcm_device_lock); |
| 885 | list_add_tail(&dev->list, &bcm_device_list); |
| 886 | mutex_unlock(&bcm_device_lock); |
| 887 | |
| 888 | bcm_gpio_set_power(dev, false); |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | static int bcm_remove(struct platform_device *pdev) |
| 894 | { |
| 895 | struct bcm_device *dev = platform_get_drvdata(pdev); |
| 896 | |
| 897 | mutex_lock(&bcm_device_lock); |
| 898 | list_del(&dev->list); |
| 899 | mutex_unlock(&bcm_device_lock); |
| 900 | |
| 901 | dev_info(&pdev->dev, "%s device unregistered.\n", dev->name); |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static const struct hci_uart_proto bcm_proto = { |
| 907 | .id = HCI_UART_BCM, |
| 908 | .name = "Broadcom", |
| 909 | .manufacturer = 15, |
| 910 | .init_speed = 115200, |
| 911 | .open = bcm_open, |
| 912 | .close = bcm_close, |
| 913 | .flush = bcm_flush, |
| 914 | .setup = bcm_setup, |
| 915 | .set_baudrate = bcm_set_baudrate, |
| 916 | .recv = bcm_recv, |
| 917 | .enqueue = bcm_enqueue, |
| 918 | .dequeue = bcm_dequeue, |
| 919 | }; |
| 920 | |
| 921 | #ifdef CONFIG_ACPI |
| 922 | static const struct acpi_device_id bcm_acpi_match[] = { |
| 923 | { "BCM2E1A", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 924 | { "BCM2E39", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 925 | { "BCM2E3A", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 926 | { "BCM2E3D", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 927 | { "BCM2E3F", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 928 | { "BCM2E40", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 929 | { "BCM2E54", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 930 | { "BCM2E55", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 931 | { "BCM2E64", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 932 | { "BCM2E65", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 933 | { "BCM2E67", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 934 | { "BCM2E71", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 935 | { "BCM2E7B", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 936 | { "BCM2E7C", (kernel_ulong_t)&acpi_bcm_int_last_gpios }, |
| 937 | { "BCM2E95", (kernel_ulong_t)&acpi_bcm_int_first_gpios }, |
| 938 | { "BCM2E96", (kernel_ulong_t)&acpi_bcm_int_first_gpios }, |
| 939 | { }, |
| 940 | }; |
| 941 | MODULE_DEVICE_TABLE(acpi, bcm_acpi_match); |
| 942 | #endif |
| 943 | |
| 944 | /* Platform suspend and resume callbacks */ |
| 945 | static const struct dev_pm_ops bcm_pm_ops = { |
| 946 | SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume) |
| 947 | SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL) |
| 948 | }; |
| 949 | |
| 950 | static struct platform_driver bcm_driver = { |
| 951 | .probe = bcm_probe, |
| 952 | .remove = bcm_remove, |
| 953 | .driver = { |
| 954 | .name = "hci_bcm", |
| 955 | .acpi_match_table = ACPI_PTR(bcm_acpi_match), |
| 956 | .pm = &bcm_pm_ops, |
| 957 | }, |
| 958 | }; |
| 959 | |
| 960 | static int bcm_serdev_probe(struct serdev_device *serdev) |
| 961 | { |
| 962 | struct bcm_serdev *bcmdev; |
| 963 | u32 speed; |
| 964 | int err; |
| 965 | |
| 966 | bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL); |
| 967 | if (!bcmdev) |
| 968 | return -ENOMEM; |
| 969 | |
| 970 | bcmdev->hu.serdev = serdev; |
| 971 | serdev_device_set_drvdata(serdev, bcmdev); |
| 972 | |
| 973 | err = device_property_read_u32(&serdev->dev, "max-speed", &speed); |
| 974 | if (!err) |
| 975 | bcmdev->hu.oper_speed = speed; |
| 976 | |
| 977 | return hci_uart_register_device(&bcmdev->hu, &bcm_proto); |
| 978 | } |
| 979 | |
| 980 | static void bcm_serdev_remove(struct serdev_device *serdev) |
| 981 | { |
| 982 | struct bcm_serdev *bcmdev = serdev_device_get_drvdata(serdev); |
| 983 | |
| 984 | hci_uart_unregister_device(&bcmdev->hu); |
| 985 | } |
| 986 | |
| 987 | #ifdef CONFIG_OF |
| 988 | static const struct of_device_id bcm_bluetooth_of_match[] = { |
| 989 | { .compatible = "brcm,bcm43438-bt" }, |
| 990 | { }, |
| 991 | }; |
| 992 | MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match); |
| 993 | #endif |
| 994 | |
| 995 | static struct serdev_device_driver bcm_serdev_driver = { |
| 996 | .probe = bcm_serdev_probe, |
| 997 | .remove = bcm_serdev_remove, |
| 998 | .driver = { |
| 999 | .name = "hci_uart_bcm", |
| 1000 | .of_match_table = of_match_ptr(bcm_bluetooth_of_match), |
| 1001 | }, |
| 1002 | }; |
| 1003 | |
| 1004 | int __init bcm_init(void) |
| 1005 | { |
| 1006 | /* For now, we need to keep both platform device |
| 1007 | * driver (ACPI generated) and serdev driver (DT). |
| 1008 | */ |
| 1009 | platform_driver_register(&bcm_driver); |
| 1010 | serdev_device_driver_register(&bcm_serdev_driver); |
| 1011 | |
| 1012 | return hci_uart_register_proto(&bcm_proto); |
| 1013 | } |
| 1014 | |
| 1015 | int __exit bcm_deinit(void) |
| 1016 | { |
| 1017 | platform_driver_unregister(&bcm_driver); |
| 1018 | serdev_device_driver_unregister(&bcm_serdev_driver); |
| 1019 | |
| 1020 | return hci_uart_unregister_proto(&bcm_proto); |
| 1021 | } |