1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Bluetooth HCI UART H4 driver with Nokia Extensions AKA Nokia H4+ 4 * 5 * Copyright (C) 2015 Marcel Holtmann <marcel@holtmann.org> 6 * Copyright (C) 2015-2017 Sebastian Reichel <sre@kernel.org> 7 */ 8 9 #include <linux/clk.h> 10 #include <linux/errno.h> 11 #include <linux/firmware.h> 12 #include <linux/gpio/consumer.h> 13 #include <linux/interrupt.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/serdev.h> 19 #include <linux/skbuff.h> 20 #include <linux/slab.h> 21 #include <linux/string.h> 22 #include <linux/types.h> 23 #include <linux/unaligned.h> 24 #include <net/bluetooth/bluetooth.h> 25 #include <net/bluetooth/hci_core.h> 26 27 #include "hci_uart.h" 28 #include "btbcm.h" 29 30 #define VERSION "0.1" 31 32 #define NOKIA_ID_BCM2048 0x04 33 #define NOKIA_ID_TI1271 0x31 34 35 #define FIRMWARE_BCM2048 "nokia/bcmfw.bin" 36 #define FIRMWARE_TI1271 "nokia/ti1273.bin" 37 38 #define HCI_NOKIA_NEG_PKT 0x06 39 #define HCI_NOKIA_ALIVE_PKT 0x07 40 #define HCI_NOKIA_RADIO_PKT 0x08 41 42 #define HCI_NOKIA_NEG_HDR_SIZE 1 43 #define HCI_NOKIA_MAX_NEG_SIZE 255 44 #define HCI_NOKIA_ALIVE_HDR_SIZE 1 45 #define HCI_NOKIA_MAX_ALIVE_SIZE 255 46 #define HCI_NOKIA_RADIO_HDR_SIZE 2 47 #define HCI_NOKIA_MAX_RADIO_SIZE 255 48 49 #define NOKIA_PROTO_PKT 0x44 50 #define NOKIA_PROTO_BYTE 0x4c 51 52 #define NOKIA_NEG_REQ 0x00 53 #define NOKIA_NEG_ACK 0x20 54 #define NOKIA_NEG_NAK 0x40 55 56 #define H4_TYPE_SIZE 1 57 58 #define NOKIA_RECV_ALIVE \ 59 .type = HCI_NOKIA_ALIVE_PKT, \ 60 .hlen = HCI_NOKIA_ALIVE_HDR_SIZE, \ 61 .loff = 0, \ 62 .lsize = 1, \ 63 .maxlen = HCI_NOKIA_MAX_ALIVE_SIZE \ 64 65 #define NOKIA_RECV_NEG \ 66 .type = HCI_NOKIA_NEG_PKT, \ 67 .hlen = HCI_NOKIA_NEG_HDR_SIZE, \ 68 .loff = 0, \ 69 .lsize = 1, \ 70 .maxlen = HCI_NOKIA_MAX_NEG_SIZE \ 71 72 #define NOKIA_RECV_RADIO \ 73 .type = HCI_NOKIA_RADIO_PKT, \ 74 .hlen = HCI_NOKIA_RADIO_HDR_SIZE, \ 75 .loff = 1, \ 76 .lsize = 1, \ 77 .maxlen = HCI_NOKIA_MAX_RADIO_SIZE \ 78 79 struct hci_nokia_neg_hdr { 80 u8 dlen; 81 } __packed; 82 83 struct hci_nokia_neg_cmd { 84 u8 ack; 85 u16 baud; 86 u16 unused1; 87 u8 proto; 88 u16 sys_clk; 89 u16 unused2; 90 } __packed; 91 92 #define NOKIA_ALIVE_REQ 0x55 93 #define NOKIA_ALIVE_RESP 0xcc 94 95 struct hci_nokia_alive_hdr { 96 u8 dlen; 97 } __packed; 98 99 struct hci_nokia_alive_pkt { 100 u8 mid; 101 u8 unused; 102 } __packed; 103 104 struct hci_nokia_neg_evt { 105 u8 ack; 106 u16 baud; 107 u16 unused1; 108 u8 proto; 109 u16 sys_clk; 110 u16 unused2; 111 u8 man_id; 112 u8 ver_id; 113 } __packed; 114 115 #define MAX_BAUD_RATE 3692300 116 #define SETUP_BAUD_RATE 921600 117 #define INIT_BAUD_RATE 120000 118 119 struct nokia_bt_dev { 120 struct hci_uart hu; 121 struct serdev_device *serdev; 122 123 struct gpio_desc *reset; 124 struct gpio_desc *wakeup_host; 125 struct gpio_desc *wakeup_bt; 126 unsigned long sysclk_speed; 127 128 int wake_irq; 129 struct sk_buff *rx_skb; 130 struct sk_buff_head txq; 131 bdaddr_t bdaddr; 132 133 int init_error; 134 struct completion init_completion; 135 136 u8 man_id; 137 u8 ver_id; 138 139 bool initialized; 140 bool tx_enabled; 141 bool rx_enabled; 142 }; 143 144 static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb); 145 146 static void nokia_flow_control(struct serdev_device *serdev, bool enable) 147 { 148 if (enable) { 149 serdev_device_set_rts(serdev, true); 150 serdev_device_set_flow_control(serdev, true); 151 } else { 152 serdev_device_set_flow_control(serdev, false); 153 serdev_device_set_rts(serdev, false); 154 } 155 } 156 157 static irqreturn_t wakeup_handler(int irq, void *data) 158 { 159 struct nokia_bt_dev *btdev = data; 160 struct device *dev = &btdev->serdev->dev; 161 int wake_state = gpiod_get_value(btdev->wakeup_host); 162 163 if (btdev->rx_enabled == wake_state) 164 return IRQ_HANDLED; 165 166 if (wake_state) 167 pm_runtime_get(dev); 168 else 169 pm_runtime_put(dev); 170 171 btdev->rx_enabled = wake_state; 172 173 return IRQ_HANDLED; 174 } 175 176 static int nokia_reset(struct hci_uart *hu) 177 { 178 struct nokia_bt_dev *btdev = hu->priv; 179 struct device *dev = &btdev->serdev->dev; 180 int err; 181 182 /* reset routine */ 183 gpiod_set_value_cansleep(btdev->reset, 1); 184 gpiod_set_value_cansleep(btdev->wakeup_bt, 1); 185 186 msleep(100); 187 188 /* safety check */ 189 err = gpiod_get_value_cansleep(btdev->wakeup_host); 190 if (err == 1) { 191 dev_err(dev, "reset: host wakeup not low!"); 192 return -EPROTO; 193 } 194 195 /* flush queue */ 196 serdev_device_write_flush(btdev->serdev); 197 198 /* init uart */ 199 nokia_flow_control(btdev->serdev, false); 200 serdev_device_set_baudrate(btdev->serdev, INIT_BAUD_RATE); 201 202 gpiod_set_value_cansleep(btdev->reset, 0); 203 204 /* wait for cts */ 205 err = serdev_device_wait_for_cts(btdev->serdev, true, 200); 206 if (err < 0) { 207 dev_err(dev, "CTS not received: %d", err); 208 return err; 209 } 210 211 nokia_flow_control(btdev->serdev, true); 212 213 return 0; 214 } 215 216 static int nokia_send_alive_packet(struct hci_uart *hu) 217 { 218 struct nokia_bt_dev *btdev = hu->priv; 219 struct device *dev = &btdev->serdev->dev; 220 struct hci_nokia_alive_hdr *hdr; 221 struct hci_nokia_alive_pkt *pkt; 222 struct sk_buff *skb; 223 int len; 224 225 init_completion(&btdev->init_completion); 226 227 len = H4_TYPE_SIZE + sizeof(*hdr) + sizeof(*pkt); 228 skb = bt_skb_alloc(len, GFP_KERNEL); 229 if (!skb) 230 return -ENOMEM; 231 232 hci_skb_pkt_type(skb) = HCI_NOKIA_ALIVE_PKT; 233 memset(skb->data, 0x00, len); 234 235 hdr = skb_put(skb, sizeof(*hdr)); 236 hdr->dlen = sizeof(*pkt); 237 pkt = skb_put(skb, sizeof(*pkt)); 238 pkt->mid = NOKIA_ALIVE_REQ; 239 240 nokia_enqueue(hu, skb); 241 hci_uart_tx_wakeup(hu); 242 243 dev_dbg(dev, "Alive sent"); 244 245 if (!wait_for_completion_interruptible_timeout(&btdev->init_completion, 246 msecs_to_jiffies(1000))) { 247 return -ETIMEDOUT; 248 } 249 250 if (btdev->init_error < 0) 251 return btdev->init_error; 252 253 return 0; 254 } 255 256 static int nokia_send_negotiation(struct hci_uart *hu) 257 { 258 struct nokia_bt_dev *btdev = hu->priv; 259 struct device *dev = &btdev->serdev->dev; 260 struct hci_nokia_neg_cmd *neg_cmd; 261 struct hci_nokia_neg_hdr *neg_hdr; 262 struct sk_buff *skb; 263 int len, err; 264 u16 baud = DIV_ROUND_CLOSEST(btdev->sysclk_speed * 10, SETUP_BAUD_RATE); 265 int sysclk = btdev->sysclk_speed / 1000; 266 267 len = H4_TYPE_SIZE + sizeof(*neg_hdr) + sizeof(*neg_cmd); 268 skb = bt_skb_alloc(len, GFP_KERNEL); 269 if (!skb) 270 return -ENOMEM; 271 272 hci_skb_pkt_type(skb) = HCI_NOKIA_NEG_PKT; 273 274 neg_hdr = skb_put(skb, sizeof(*neg_hdr)); 275 neg_hdr->dlen = sizeof(*neg_cmd); 276 277 neg_cmd = skb_put(skb, sizeof(*neg_cmd)); 278 neg_cmd->ack = NOKIA_NEG_REQ; 279 neg_cmd->baud = cpu_to_le16(baud); 280 neg_cmd->unused1 = 0x0000; 281 neg_cmd->proto = NOKIA_PROTO_BYTE; 282 neg_cmd->sys_clk = cpu_to_le16(sysclk); 283 neg_cmd->unused2 = 0x0000; 284 285 btdev->init_error = 0; 286 init_completion(&btdev->init_completion); 287 288 nokia_enqueue(hu, skb); 289 hci_uart_tx_wakeup(hu); 290 291 dev_dbg(dev, "Negotiation sent"); 292 293 if (!wait_for_completion_interruptible_timeout(&btdev->init_completion, 294 msecs_to_jiffies(10000))) { 295 return -ETIMEDOUT; 296 } 297 298 if (btdev->init_error < 0) 299 return btdev->init_error; 300 301 /* Change to previously negotiated speed. Flow Control 302 * is disabled until bluetooth adapter is ready to avoid 303 * broken bytes being received. 304 */ 305 nokia_flow_control(btdev->serdev, false); 306 serdev_device_set_baudrate(btdev->serdev, SETUP_BAUD_RATE); 307 err = serdev_device_wait_for_cts(btdev->serdev, true, 200); 308 if (err < 0) { 309 dev_err(dev, "CTS not received: %d", err); 310 return err; 311 } 312 nokia_flow_control(btdev->serdev, true); 313 314 dev_dbg(dev, "Negotiation successful"); 315 316 return 0; 317 } 318 319 static int nokia_setup_fw(struct hci_uart *hu) 320 { 321 struct nokia_bt_dev *btdev = hu->priv; 322 struct device *dev = &btdev->serdev->dev; 323 const char *fwname; 324 const struct firmware *fw; 325 const u8 *fw_ptr; 326 size_t fw_size; 327 int err; 328 329 dev_dbg(dev, "setup firmware"); 330 331 if (btdev->man_id == NOKIA_ID_BCM2048) { 332 fwname = FIRMWARE_BCM2048; 333 } else if (btdev->man_id == NOKIA_ID_TI1271) { 334 fwname = FIRMWARE_TI1271; 335 } else { 336 dev_err(dev, "Unsupported bluetooth device!"); 337 return -ENODEV; 338 } 339 340 err = request_firmware(&fw, fwname, dev); 341 if (err < 0) { 342 dev_err(dev, "%s: Failed to load Nokia firmware file (%d)", 343 hu->hdev->name, err); 344 return err; 345 } 346 347 fw_ptr = fw->data; 348 fw_size = fw->size; 349 350 while (fw_size >= 4) { 351 u16 pkt_size = get_unaligned_le16(fw_ptr); 352 u8 pkt_type = fw_ptr[2]; 353 const struct hci_command_hdr *cmd; 354 u16 opcode; 355 struct sk_buff *skb; 356 357 if (pkt_size > fw_size - 2) { 358 err = -EINVAL; 359 dev_err(dev, "%s: Malformed firmware packet\n", 360 hu->hdev->name); 361 goto done; 362 } 363 364 switch (pkt_type) { 365 case HCI_COMMAND_PKT: 366 if (pkt_size < 1 + HCI_COMMAND_HDR_SIZE) { 367 err = -EINVAL; 368 dev_err(dev, "%s: Malformed firmware command\n", 369 hu->hdev->name); 370 goto done; 371 } 372 373 cmd = (struct hci_command_hdr *)(fw_ptr + 3); 374 if (cmd->plen > pkt_size - 1 - HCI_COMMAND_HDR_SIZE) { 375 err = -EINVAL; 376 dev_err(dev, "%s: Truncated firmware command\n", 377 hu->hdev->name); 378 goto done; 379 } 380 opcode = le16_to_cpu(cmd->opcode); 381 382 skb = __hci_cmd_sync(hu->hdev, opcode, cmd->plen, 383 fw_ptr + 3 + HCI_COMMAND_HDR_SIZE, 384 HCI_INIT_TIMEOUT); 385 if (IS_ERR(skb)) { 386 err = PTR_ERR(skb); 387 dev_err(dev, "%s: FW command %04x failed (%d)", 388 hu->hdev->name, opcode, err); 389 goto done; 390 } 391 kfree_skb(skb); 392 break; 393 case HCI_NOKIA_RADIO_PKT: 394 case HCI_NOKIA_NEG_PKT: 395 case HCI_NOKIA_ALIVE_PKT: 396 break; 397 } 398 399 fw_ptr += pkt_size + 2; 400 fw_size -= pkt_size + 2; 401 } 402 403 done: 404 release_firmware(fw); 405 return err; 406 } 407 408 static int nokia_setup(struct hci_uart *hu) 409 { 410 struct nokia_bt_dev *btdev = hu->priv; 411 struct device *dev = &btdev->serdev->dev; 412 int err; 413 414 btdev->initialized = false; 415 416 nokia_flow_control(btdev->serdev, false); 417 418 pm_runtime_get_sync(dev); 419 420 if (btdev->tx_enabled) { 421 gpiod_set_value_cansleep(btdev->wakeup_bt, 0); 422 pm_runtime_put(&btdev->serdev->dev); 423 btdev->tx_enabled = false; 424 } 425 426 dev_dbg(dev, "protocol setup"); 427 428 /* 0. reset connection */ 429 err = nokia_reset(hu); 430 if (err < 0) { 431 dev_err(dev, "Reset failed: %d", err); 432 goto out; 433 } 434 435 /* 1. negotiate speed etc */ 436 err = nokia_send_negotiation(hu); 437 if (err < 0) { 438 dev_err(dev, "Negotiation failed: %d", err); 439 goto out; 440 } 441 442 /* 2. verify correct setup using alive packet */ 443 err = nokia_send_alive_packet(hu); 444 if (err < 0) { 445 dev_err(dev, "Alive check failed: %d", err); 446 goto out; 447 } 448 449 /* 3. send firmware */ 450 err = nokia_setup_fw(hu); 451 if (err < 0) { 452 dev_err(dev, "Could not setup FW: %d", err); 453 goto out; 454 } 455 456 nokia_flow_control(btdev->serdev, false); 457 serdev_device_set_baudrate(btdev->serdev, MAX_BAUD_RATE); 458 nokia_flow_control(btdev->serdev, true); 459 460 if (btdev->man_id == NOKIA_ID_BCM2048) { 461 hu->hdev->set_bdaddr = btbcm_set_bdaddr; 462 hci_set_quirk(hu->hdev, HCI_QUIRK_INVALID_BDADDR); 463 dev_dbg(dev, "bcm2048 has invalid bluetooth address!"); 464 } 465 466 dev_dbg(dev, "protocol setup done!"); 467 468 gpiod_set_value_cansleep(btdev->wakeup_bt, 0); 469 pm_runtime_put(dev); 470 btdev->tx_enabled = false; 471 btdev->initialized = true; 472 473 return 0; 474 out: 475 pm_runtime_put(dev); 476 477 return err; 478 } 479 480 static int nokia_open(struct hci_uart *hu) 481 { 482 struct device *dev = &hu->serdev->dev; 483 484 dev_dbg(dev, "protocol open"); 485 486 pm_runtime_enable(dev); 487 488 return 0; 489 } 490 491 static int nokia_flush(struct hci_uart *hu) 492 { 493 struct nokia_bt_dev *btdev = hu->priv; 494 495 dev_dbg(&btdev->serdev->dev, "flush device"); 496 497 skb_queue_purge(&btdev->txq); 498 499 return 0; 500 } 501 502 static int nokia_close(struct hci_uart *hu) 503 { 504 struct nokia_bt_dev *btdev = hu->priv; 505 struct device *dev = &btdev->serdev->dev; 506 507 dev_dbg(dev, "close device"); 508 509 btdev->initialized = false; 510 511 skb_queue_purge(&btdev->txq); 512 513 kfree_skb(btdev->rx_skb); 514 515 /* disable module */ 516 gpiod_set_value(btdev->reset, 1); 517 gpiod_set_value(btdev->wakeup_bt, 0); 518 519 pm_runtime_disable(&btdev->serdev->dev); 520 521 return 0; 522 } 523 524 /* Enqueue frame for transmission (padding, crc, etc) */ 525 static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb) 526 { 527 struct nokia_bt_dev *btdev = hu->priv; 528 int err; 529 530 /* Prepend skb with frame type */ 531 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); 532 533 /* Packets must be word aligned */ 534 if (skb->len % 2) { 535 err = skb_pad(skb, 1); 536 if (err) 537 return err; 538 skb_put(skb, 1); 539 } 540 541 skb_queue_tail(&btdev->txq, skb); 542 543 return 0; 544 } 545 546 static int nokia_recv_negotiation_packet(struct hci_dev *hdev, 547 struct sk_buff *skb) 548 { 549 struct hci_uart *hu = hci_get_drvdata(hdev); 550 struct nokia_bt_dev *btdev = hu->priv; 551 struct device *dev = &btdev->serdev->dev; 552 struct hci_nokia_neg_hdr *hdr; 553 struct hci_nokia_neg_evt *evt; 554 int ret = 0; 555 556 hdr = (struct hci_nokia_neg_hdr *)skb->data; 557 if (hdr->dlen != sizeof(*evt)) { 558 btdev->init_error = -EIO; 559 ret = -EIO; 560 goto finish_neg; 561 } 562 563 evt = skb_pull(skb, sizeof(*hdr)); 564 565 if (evt->ack != NOKIA_NEG_ACK) { 566 dev_err(dev, "Negotiation received: wrong reply"); 567 btdev->init_error = -EINVAL; 568 ret = -EINVAL; 569 goto finish_neg; 570 } 571 572 btdev->man_id = evt->man_id; 573 btdev->ver_id = evt->ver_id; 574 575 dev_dbg(dev, "Negotiation received: baud=%u:clk=%u:manu=%u:vers=%u", 576 evt->baud, evt->sys_clk, evt->man_id, evt->ver_id); 577 578 finish_neg: 579 complete(&btdev->init_completion); 580 kfree_skb(skb); 581 return ret; 582 } 583 584 static int nokia_recv_alive_packet(struct hci_dev *hdev, struct sk_buff *skb) 585 { 586 struct hci_uart *hu = hci_get_drvdata(hdev); 587 struct nokia_bt_dev *btdev = hu->priv; 588 struct device *dev = &btdev->serdev->dev; 589 struct hci_nokia_alive_hdr *hdr; 590 struct hci_nokia_alive_pkt *pkt; 591 int ret = 0; 592 593 hdr = (struct hci_nokia_alive_hdr *)skb->data; 594 if (hdr->dlen != sizeof(*pkt)) { 595 dev_err(dev, "Corrupted alive message"); 596 btdev->init_error = -EIO; 597 ret = -EIO; 598 goto finish_alive; 599 } 600 601 pkt = skb_pull(skb, sizeof(*hdr)); 602 603 if (pkt->mid != NOKIA_ALIVE_RESP) { 604 dev_err(dev, "Alive received: invalid response: 0x%02x!", 605 pkt->mid); 606 btdev->init_error = -EINVAL; 607 ret = -EINVAL; 608 goto finish_alive; 609 } 610 611 dev_dbg(dev, "Alive received"); 612 613 finish_alive: 614 complete(&btdev->init_completion); 615 kfree_skb(skb); 616 return ret; 617 } 618 619 static int nokia_recv_radio(struct hci_dev *hdev, struct sk_buff *skb) 620 { 621 /* Packets received on the dedicated radio channel are 622 * HCI events and so feed them back into the core. 623 */ 624 hci_skb_pkt_type(skb) = HCI_EVENT_PKT; 625 return hci_recv_frame(hdev, skb); 626 } 627 628 /* Recv data */ 629 static const struct h4_recv_pkt nokia_recv_pkts[] = { 630 { H4_RECV_ACL, .recv = hci_recv_frame }, 631 { H4_RECV_SCO, .recv = hci_recv_frame }, 632 { H4_RECV_EVENT, .recv = hci_recv_frame }, 633 { NOKIA_RECV_ALIVE, .recv = nokia_recv_alive_packet }, 634 { NOKIA_RECV_NEG, .recv = nokia_recv_negotiation_packet }, 635 { NOKIA_RECV_RADIO, .recv = nokia_recv_radio }, 636 }; 637 638 static int nokia_recv(struct hci_uart *hu, const void *data, int count) 639 { 640 struct nokia_bt_dev *btdev = hu->priv; 641 struct device *dev = &btdev->serdev->dev; 642 int err; 643 644 if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 645 return -EUNATCH; 646 647 btdev->rx_skb = h4_recv_buf(hu, btdev->rx_skb, data, count, 648 nokia_recv_pkts, ARRAY_SIZE(nokia_recv_pkts)); 649 if (IS_ERR(btdev->rx_skb)) { 650 err = PTR_ERR(btdev->rx_skb); 651 dev_err(dev, "Frame reassembly failed (%d)", err); 652 btdev->rx_skb = NULL; 653 return err; 654 } 655 656 return count; 657 } 658 659 static struct sk_buff *nokia_dequeue(struct hci_uart *hu) 660 { 661 struct nokia_bt_dev *btdev = hu->priv; 662 struct device *dev = &btdev->serdev->dev; 663 struct sk_buff *result = skb_dequeue(&btdev->txq); 664 665 if (!btdev->initialized) 666 return result; 667 668 if (btdev->tx_enabled == !!result) 669 return result; 670 671 if (result) { 672 pm_runtime_get_sync(dev); 673 gpiod_set_value_cansleep(btdev->wakeup_bt, 1); 674 } else { 675 serdev_device_wait_until_sent(btdev->serdev, 0); 676 gpiod_set_value_cansleep(btdev->wakeup_bt, 0); 677 pm_runtime_put(dev); 678 } 679 680 btdev->tx_enabled = !!result; 681 682 return result; 683 } 684 685 static const struct hci_uart_proto nokia_proto = { 686 .id = HCI_UART_NOKIA, 687 .name = "Nokia", 688 .open = nokia_open, 689 .close = nokia_close, 690 .recv = nokia_recv, 691 .enqueue = nokia_enqueue, 692 .dequeue = nokia_dequeue, 693 .flush = nokia_flush, 694 .setup = nokia_setup, 695 .manufacturer = 1, 696 }; 697 698 static int nokia_bluetooth_serdev_probe(struct serdev_device *serdev) 699 { 700 struct device *dev = &serdev->dev; 701 struct nokia_bt_dev *btdev; 702 struct clk *sysclk; 703 int err = 0; 704 705 btdev = devm_kzalloc(dev, sizeof(*btdev), GFP_KERNEL); 706 if (!btdev) 707 return -ENOMEM; 708 709 btdev->hu.serdev = btdev->serdev = serdev; 710 serdev_device_set_drvdata(serdev, btdev); 711 712 btdev->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 713 if (IS_ERR(btdev->reset)) { 714 err = PTR_ERR(btdev->reset); 715 dev_err(dev, "could not get reset gpio: %d", err); 716 return err; 717 } 718 719 btdev->wakeup_host = devm_gpiod_get(dev, "host-wakeup", GPIOD_IN); 720 if (IS_ERR(btdev->wakeup_host)) { 721 err = PTR_ERR(btdev->wakeup_host); 722 dev_err(dev, "could not get host wakeup gpio: %d", err); 723 return err; 724 } 725 726 btdev->wake_irq = gpiod_to_irq(btdev->wakeup_host); 727 728 err = devm_request_threaded_irq(dev, btdev->wake_irq, NULL, 729 wakeup_handler, 730 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 731 "wakeup", btdev); 732 if (err) { 733 dev_err(dev, "could request wakeup irq: %d", err); 734 return err; 735 } 736 737 btdev->wakeup_bt = devm_gpiod_get(dev, "bluetooth-wakeup", 738 GPIOD_OUT_LOW); 739 if (IS_ERR(btdev->wakeup_bt)) { 740 err = PTR_ERR(btdev->wakeup_bt); 741 dev_err(dev, "could not get BT wakeup gpio: %d", err); 742 return err; 743 } 744 745 sysclk = devm_clk_get(dev, "sysclk"); 746 if (IS_ERR(sysclk)) { 747 err = PTR_ERR(sysclk); 748 dev_err(dev, "could not get sysclk: %d", err); 749 return err; 750 } 751 752 err = clk_prepare_enable(sysclk); 753 if (err) { 754 dev_err(dev, "could not enable sysclk: %d", err); 755 return err; 756 } 757 btdev->sysclk_speed = clk_get_rate(sysclk); 758 clk_disable_unprepare(sysclk); 759 760 skb_queue_head_init(&btdev->txq); 761 762 btdev->hu.priv = btdev; 763 btdev->hu.alignment = 2; /* Nokia H4+ is word aligned */ 764 765 err = hci_uart_register_device(&btdev->hu, &nokia_proto); 766 if (err) { 767 dev_err(dev, "could not register bluetooth uart: %d", err); 768 return err; 769 } 770 771 return 0; 772 } 773 774 static void nokia_bluetooth_serdev_remove(struct serdev_device *serdev) 775 { 776 struct nokia_bt_dev *btdev = serdev_device_get_drvdata(serdev); 777 778 hci_uart_unregister_device(&btdev->hu); 779 } 780 781 static int nokia_bluetooth_runtime_suspend(struct device *dev) 782 { 783 struct serdev_device *serdev = to_serdev_device(dev); 784 785 nokia_flow_control(serdev, false); 786 return 0; 787 } 788 789 static int nokia_bluetooth_runtime_resume(struct device *dev) 790 { 791 struct serdev_device *serdev = to_serdev_device(dev); 792 793 nokia_flow_control(serdev, true); 794 return 0; 795 } 796 797 static const struct dev_pm_ops nokia_bluetooth_pm_ops = { 798 SET_RUNTIME_PM_OPS(nokia_bluetooth_runtime_suspend, 799 nokia_bluetooth_runtime_resume, 800 NULL) 801 }; 802 803 #ifdef CONFIG_OF 804 static const struct of_device_id nokia_bluetooth_of_match[] = { 805 { .compatible = "nokia,h4p-bluetooth", }, 806 {}, 807 }; 808 MODULE_DEVICE_TABLE(of, nokia_bluetooth_of_match); 809 #endif 810 811 static struct serdev_device_driver nokia_bluetooth_serdev_driver = { 812 .probe = nokia_bluetooth_serdev_probe, 813 .remove = nokia_bluetooth_serdev_remove, 814 .driver = { 815 .name = "nokia-bluetooth", 816 .pm = &nokia_bluetooth_pm_ops, 817 .of_match_table = of_match_ptr(nokia_bluetooth_of_match), 818 }, 819 }; 820 821 module_serdev_device_driver(nokia_bluetooth_serdev_driver); 822 823 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>"); 824 MODULE_DESCRIPTION("Bluetooth HCI UART Nokia H4+ driver ver " VERSION); 825 MODULE_VERSION(VERSION); 826 MODULE_LICENSE("GPL"); 827