hci_bcm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/firmware.h>
  27. #include <linux/module.h>
  28. #include <linux/acpi.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/clk.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/tty.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/dmi.h>
  35. #include <linux/pm_runtime.h>
  36. #include <net/bluetooth/bluetooth.h>
  37. #include <net/bluetooth/hci_core.h>
  38. #include "btbcm.h"
  39. #include "hci_uart.h"
  40. #define BCM_AUTOSUSPEND_DELAY 5000 /* default autosleep delay */
  41. struct bcm_device {
  42. struct list_head list;
  43. struct platform_device *pdev;
  44. const char *name;
  45. struct gpio_desc *device_wakeup;
  46. struct gpio_desc *shutdown;
  47. struct clk *clk;
  48. bool clk_enabled;
  49. u32 init_speed;
  50. int irq;
  51. u8 irq_polarity;
  52. #ifdef CONFIG_PM
  53. struct hci_uart *hu;
  54. bool is_suspended; /* suspend/resume flag */
  55. #endif
  56. };
  57. struct bcm_data {
  58. struct sk_buff *rx_skb;
  59. struct sk_buff_head txq;
  60. struct bcm_device *dev;
  61. };
  62. /* List of BCM BT UART devices */
  63. static DEFINE_MUTEX(bcm_device_lock);
  64. static LIST_HEAD(bcm_device_list);
  65. static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
  66. {
  67. struct hci_dev *hdev = hu->hdev;
  68. struct sk_buff *skb;
  69. struct bcm_update_uart_baud_rate param;
  70. if (speed > 3000000) {
  71. struct bcm_write_uart_clock_setting clock;
  72. clock.type = BCM_UART_CLOCK_48MHZ;
  73. bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
  74. /* This Broadcom specific command changes the UART's controller
  75. * clock for baud rate > 3000000.
  76. */
  77. skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
  78. if (IS_ERR(skb)) {
  79. int err = PTR_ERR(skb);
  80. bt_dev_err(hdev, "BCM: failed to write clock (%d)",
  81. err);
  82. return err;
  83. }
  84. kfree_skb(skb);
  85. }
  86. bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
  87. param.zero = cpu_to_le16(0);
  88. param.baud_rate = cpu_to_le32(speed);
  89. /* This Broadcom specific command changes the UART's controller baud
  90. * rate.
  91. */
  92. skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
  93. HCI_INIT_TIMEOUT);
  94. if (IS_ERR(skb)) {
  95. int err = PTR_ERR(skb);
  96. bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
  97. err);
  98. return err;
  99. }
  100. kfree_skb(skb);
  101. return 0;
  102. }
  103. /* bcm_device_exists should be protected by bcm_device_lock */
  104. static bool bcm_device_exists(struct bcm_device *device)
  105. {
  106. struct list_head *p;
  107. list_for_each(p, &bcm_device_list) {
  108. struct bcm_device *dev = list_entry(p, struct bcm_device, list);
  109. if (device == dev)
  110. return true;
  111. }
  112. return false;
  113. }
  114. static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
  115. {
  116. if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
  117. clk_enable(dev->clk);
  118. gpiod_set_value(dev->shutdown, powered);
  119. gpiod_set_value(dev->device_wakeup, powered);
  120. if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
  121. clk_disable(dev->clk);
  122. dev->clk_enabled = powered;
  123. return 0;
  124. }
  125. #ifdef CONFIG_PM
  126. static irqreturn_t bcm_host_wake(int irq, void *data)
  127. {
  128. struct bcm_device *bdev = data;
  129. bt_dev_dbg(bdev, "Host wake IRQ");
  130. pm_runtime_get(&bdev->pdev->dev);
  131. pm_runtime_mark_last_busy(&bdev->pdev->dev);
  132. pm_runtime_put_autosuspend(&bdev->pdev->dev);
  133. return IRQ_HANDLED;
  134. }
  135. static int bcm_request_irq(struct bcm_data *bcm)
  136. {
  137. struct bcm_device *bdev = bcm->dev;
  138. int err = 0;
  139. /* If this is not a platform device, do not enable PM functionalities */
  140. mutex_lock(&bcm_device_lock);
  141. if (!bcm_device_exists(bdev)) {
  142. err = -ENODEV;
  143. goto unlock;
  144. }
  145. if (bdev->irq > 0) {
  146. err = devm_request_irq(&bdev->pdev->dev, bdev->irq,
  147. bcm_host_wake, IRQF_TRIGGER_RISING,
  148. "host_wake", bdev);
  149. if (err)
  150. goto unlock;
  151. device_init_wakeup(&bdev->pdev->dev, true);
  152. pm_runtime_set_autosuspend_delay(&bdev->pdev->dev,
  153. BCM_AUTOSUSPEND_DELAY);
  154. pm_runtime_use_autosuspend(&bdev->pdev->dev);
  155. pm_runtime_set_active(&bdev->pdev->dev);
  156. pm_runtime_enable(&bdev->pdev->dev);
  157. }
  158. unlock:
  159. mutex_unlock(&bcm_device_lock);
  160. return err;
  161. }
  162. static const struct bcm_set_sleep_mode default_sleep_params = {
  163. .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
  164. .idle_host = 2, /* idle threshold HOST, in 300ms */
  165. .idle_dev = 2, /* idle threshold device, in 300ms */
  166. .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
  167. .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
  168. .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
  169. .combine_modes = 1, /* Combine sleep and LPM flag */
  170. .tristate_control = 0, /* Allow tri-state control of UART tx flag */
  171. /* Irrelevant USB flags */
  172. .usb_auto_sleep = 0,
  173. .usb_resume_timeout = 0,
  174. .pulsed_host_wake = 0,
  175. .break_to_host = 0
  176. };
  177. static int bcm_setup_sleep(struct hci_uart *hu)
  178. {
  179. struct bcm_data *bcm = hu->priv;
  180. struct sk_buff *skb;
  181. struct bcm_set_sleep_mode sleep_params = default_sleep_params;
  182. sleep_params.host_wake_active = !bcm->dev->irq_polarity;
  183. skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
  184. &sleep_params, HCI_INIT_TIMEOUT);
  185. if (IS_ERR(skb)) {
  186. int err = PTR_ERR(skb);
  187. bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
  188. return err;
  189. }
  190. kfree_skb(skb);
  191. bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
  192. return 0;
  193. }
  194. #else
  195. static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
  196. static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
  197. #endif
  198. static int bcm_open(struct hci_uart *hu)
  199. {
  200. struct bcm_data *bcm;
  201. struct list_head *p;
  202. bt_dev_dbg(hu->hdev, "hu %p", hu);
  203. bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
  204. if (!bcm)
  205. return -ENOMEM;
  206. skb_queue_head_init(&bcm->txq);
  207. hu->priv = bcm;
  208. mutex_lock(&bcm_device_lock);
  209. list_for_each(p, &bcm_device_list) {
  210. struct bcm_device *dev = list_entry(p, struct bcm_device, list);
  211. /* Retrieve saved bcm_device based on parent of the
  212. * platform device (saved during device probe) and
  213. * parent of tty device used by hci_uart
  214. */
  215. if (hu->tty->dev->parent == dev->pdev->dev.parent) {
  216. bcm->dev = dev;
  217. hu->init_speed = dev->init_speed;
  218. #ifdef CONFIG_PM
  219. dev->hu = hu;
  220. #endif
  221. bcm_gpio_set_power(bcm->dev, true);
  222. break;
  223. }
  224. }
  225. mutex_unlock(&bcm_device_lock);
  226. return 0;
  227. }
  228. static int bcm_close(struct hci_uart *hu)
  229. {
  230. struct bcm_data *bcm = hu->priv;
  231. struct bcm_device *bdev = bcm->dev;
  232. bt_dev_dbg(hu->hdev, "hu %p", hu);
  233. /* Protect bcm->dev against removal of the device or driver */
  234. mutex_lock(&bcm_device_lock);
  235. if (bcm_device_exists(bdev)) {
  236. bcm_gpio_set_power(bdev, false);
  237. #ifdef CONFIG_PM
  238. pm_runtime_disable(&bdev->pdev->dev);
  239. pm_runtime_set_suspended(&bdev->pdev->dev);
  240. if (device_can_wakeup(&bdev->pdev->dev)) {
  241. devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev);
  242. device_init_wakeup(&bdev->pdev->dev, false);
  243. }
  244. bdev->hu = NULL;
  245. #endif
  246. }
  247. mutex_unlock(&bcm_device_lock);
  248. skb_queue_purge(&bcm->txq);
  249. kfree_skb(bcm->rx_skb);
  250. kfree(bcm);
  251. hu->priv = NULL;
  252. return 0;
  253. }
  254. static int bcm_flush(struct hci_uart *hu)
  255. {
  256. struct bcm_data *bcm = hu->priv;
  257. bt_dev_dbg(hu->hdev, "hu %p", hu);
  258. skb_queue_purge(&bcm->txq);
  259. return 0;
  260. }
  261. static int bcm_setup(struct hci_uart *hu)
  262. {
  263. struct bcm_data *bcm = hu->priv;
  264. char fw_name[64];
  265. const struct firmware *fw;
  266. unsigned int speed;
  267. int err;
  268. bt_dev_dbg(hu->hdev, "hu %p", hu);
  269. hu->hdev->set_bdaddr = btbcm_set_bdaddr;
  270. err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
  271. if (err)
  272. return err;
  273. err = request_firmware(&fw, fw_name, &hu->hdev->dev);
  274. if (err < 0) {
  275. bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
  276. return 0;
  277. }
  278. err = btbcm_patchram(hu->hdev, fw);
  279. if (err) {
  280. bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
  281. goto finalize;
  282. }
  283. /* Init speed if any */
  284. if (hu->init_speed)
  285. speed = hu->init_speed;
  286. else if (hu->proto->init_speed)
  287. speed = hu->proto->init_speed;
  288. else
  289. speed = 0;
  290. if (speed)
  291. hci_uart_set_baudrate(hu, speed);
  292. /* Operational speed if any */
  293. if (hu->oper_speed)
  294. speed = hu->oper_speed;
  295. else if (hu->proto->oper_speed)
  296. speed = hu->proto->oper_speed;
  297. else
  298. speed = 0;
  299. if (speed) {
  300. err = bcm_set_baudrate(hu, speed);
  301. if (!err)
  302. hci_uart_set_baudrate(hu, speed);
  303. }
  304. finalize:
  305. release_firmware(fw);
  306. err = btbcm_finalize(hu->hdev);
  307. if (err)
  308. return err;
  309. err = bcm_request_irq(bcm);
  310. if (!err)
  311. err = bcm_setup_sleep(hu);
  312. return err;
  313. }
  314. static const struct h4_recv_pkt bcm_recv_pkts[] = {
  315. { H4_RECV_ACL, .recv = hci_recv_frame },
  316. { H4_RECV_SCO, .recv = hci_recv_frame },
  317. { H4_RECV_EVENT, .recv = hci_recv_frame },
  318. };
  319. static int bcm_recv(struct hci_uart *hu, const void *data, int count)
  320. {
  321. struct bcm_data *bcm = hu->priv;
  322. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  323. return -EUNATCH;
  324. bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
  325. bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
  326. if (IS_ERR(bcm->rx_skb)) {
  327. int err = PTR_ERR(bcm->rx_skb);
  328. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  329. bcm->rx_skb = NULL;
  330. return err;
  331. } else if (!bcm->rx_skb) {
  332. /* Delay auto-suspend when receiving completed packet */
  333. mutex_lock(&bcm_device_lock);
  334. if (bcm->dev && bcm_device_exists(bcm->dev)) {
  335. pm_runtime_get(&bcm->dev->pdev->dev);
  336. pm_runtime_mark_last_busy(&bcm->dev->pdev->dev);
  337. pm_runtime_put_autosuspend(&bcm->dev->pdev->dev);
  338. }
  339. mutex_unlock(&bcm_device_lock);
  340. }
  341. return count;
  342. }
  343. static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  344. {
  345. struct bcm_data *bcm = hu->priv;
  346. bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
  347. /* Prepend skb with frame type */
  348. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  349. skb_queue_tail(&bcm->txq, skb);
  350. return 0;
  351. }
  352. static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
  353. {
  354. struct bcm_data *bcm = hu->priv;
  355. struct sk_buff *skb = NULL;
  356. struct bcm_device *bdev = NULL;
  357. mutex_lock(&bcm_device_lock);
  358. if (bcm_device_exists(bcm->dev)) {
  359. bdev = bcm->dev;
  360. pm_runtime_get_sync(&bdev->pdev->dev);
  361. /* Shall be resumed here */
  362. }
  363. skb = skb_dequeue(&bcm->txq);
  364. if (bdev) {
  365. pm_runtime_mark_last_busy(&bdev->pdev->dev);
  366. pm_runtime_put_autosuspend(&bdev->pdev->dev);
  367. }
  368. mutex_unlock(&bcm_device_lock);
  369. return skb;
  370. }
  371. #ifdef CONFIG_PM
  372. static int bcm_suspend_device(struct device *dev)
  373. {
  374. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  375. bt_dev_dbg(bdev, "");
  376. if (!bdev->is_suspended && bdev->hu) {
  377. hci_uart_set_flow_control(bdev->hu, true);
  378. /* Once this returns, driver suspends BT via GPIO */
  379. bdev->is_suspended = true;
  380. }
  381. /* Suspend the device */
  382. if (bdev->device_wakeup) {
  383. gpiod_set_value(bdev->device_wakeup, false);
  384. bt_dev_dbg(bdev, "suspend, delaying 15 ms");
  385. mdelay(15);
  386. }
  387. return 0;
  388. }
  389. static int bcm_resume_device(struct device *dev)
  390. {
  391. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  392. bt_dev_dbg(bdev, "");
  393. if (bdev->device_wakeup) {
  394. gpiod_set_value(bdev->device_wakeup, true);
  395. bt_dev_dbg(bdev, "resume, delaying 15 ms");
  396. mdelay(15);
  397. }
  398. /* When this executes, the device has woken up already */
  399. if (bdev->is_suspended && bdev->hu) {
  400. bdev->is_suspended = false;
  401. hci_uart_set_flow_control(bdev->hu, false);
  402. }
  403. return 0;
  404. }
  405. #endif
  406. #ifdef CONFIG_PM_SLEEP
  407. /* Platform suspend callback */
  408. static int bcm_suspend(struct device *dev)
  409. {
  410. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  411. int error;
  412. bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
  413. /* bcm_suspend can be called at any time as long as platform device is
  414. * bound, so it should use bcm_device_lock to protect access to hci_uart
  415. * and device_wake-up GPIO.
  416. */
  417. mutex_lock(&bcm_device_lock);
  418. if (!bdev->hu)
  419. goto unlock;
  420. if (pm_runtime_active(dev))
  421. bcm_suspend_device(dev);
  422. if (device_may_wakeup(&bdev->pdev->dev)) {
  423. error = enable_irq_wake(bdev->irq);
  424. if (!error)
  425. bt_dev_dbg(bdev, "BCM irq: enabled");
  426. }
  427. unlock:
  428. mutex_unlock(&bcm_device_lock);
  429. return 0;
  430. }
  431. /* Platform resume callback */
  432. static int bcm_resume(struct device *dev)
  433. {
  434. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  435. bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
  436. /* bcm_resume can be called at any time as long as platform device is
  437. * bound, so it should use bcm_device_lock to protect access to hci_uart
  438. * and device_wake-up GPIO.
  439. */
  440. mutex_lock(&bcm_device_lock);
  441. if (!bdev->hu)
  442. goto unlock;
  443. if (device_may_wakeup(&bdev->pdev->dev)) {
  444. disable_irq_wake(bdev->irq);
  445. bt_dev_dbg(bdev, "BCM irq: disabled");
  446. }
  447. bcm_resume_device(dev);
  448. unlock:
  449. mutex_unlock(&bcm_device_lock);
  450. pm_runtime_disable(dev);
  451. pm_runtime_set_active(dev);
  452. pm_runtime_enable(dev);
  453. return 0;
  454. }
  455. #endif
  456. static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
  457. static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
  458. static const struct acpi_gpio_params host_wakeup_gpios = { 2, 0, false };
  459. static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
  460. { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
  461. { "shutdown-gpios", &shutdown_gpios, 1 },
  462. { "host-wakeup-gpios", &host_wakeup_gpios, 1 },
  463. { },
  464. };
  465. #ifdef CONFIG_ACPI
  466. static u8 acpi_active_low = ACPI_ACTIVE_LOW;
  467. /* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
  468. static const struct dmi_system_id bcm_wrong_irq_dmi_table[] = {
  469. {
  470. .ident = "Asus T100TA",
  471. .matches = {
  472. DMI_EXACT_MATCH(DMI_SYS_VENDOR,
  473. "ASUSTeK COMPUTER INC."),
  474. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
  475. },
  476. .driver_data = &acpi_active_low,
  477. },
  478. { }
  479. };
  480. static int bcm_resource(struct acpi_resource *ares, void *data)
  481. {
  482. struct bcm_device *dev = data;
  483. struct acpi_resource_extended_irq *irq;
  484. struct acpi_resource_gpio *gpio;
  485. struct acpi_resource_uart_serialbus *sb;
  486. switch (ares->type) {
  487. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  488. irq = &ares->data.extended_irq;
  489. dev->irq_polarity = irq->polarity;
  490. break;
  491. case ACPI_RESOURCE_TYPE_GPIO:
  492. gpio = &ares->data.gpio;
  493. if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
  494. dev->irq_polarity = gpio->polarity;
  495. break;
  496. case ACPI_RESOURCE_TYPE_SERIAL_BUS:
  497. sb = &ares->data.uart_serial_bus;
  498. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
  499. dev->init_speed = sb->default_baud_rate;
  500. break;
  501. default:
  502. break;
  503. }
  504. /* Always tell the ACPI core to skip this resource */
  505. return 1;
  506. }
  507. static int bcm_acpi_probe(struct bcm_device *dev)
  508. {
  509. struct platform_device *pdev = dev->pdev;
  510. const struct acpi_device_id *id;
  511. struct acpi_device *adev;
  512. LIST_HEAD(resources);
  513. const struct dmi_system_id *dmi_id;
  514. int ret;
  515. id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
  516. if (!id)
  517. return -ENODEV;
  518. /* Retrieve GPIO data */
  519. dev->name = dev_name(&pdev->dev);
  520. ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
  521. acpi_bcm_default_gpios);
  522. if (ret)
  523. return ret;
  524. dev->clk = devm_clk_get(&pdev->dev, NULL);
  525. dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
  526. "device-wakeup",
  527. GPIOD_OUT_LOW);
  528. if (IS_ERR(dev->device_wakeup))
  529. return PTR_ERR(dev->device_wakeup);
  530. dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
  531. GPIOD_OUT_LOW);
  532. if (IS_ERR(dev->shutdown))
  533. return PTR_ERR(dev->shutdown);
  534. /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
  535. dev->irq = platform_get_irq(pdev, 0);
  536. if (dev->irq <= 0) {
  537. struct gpio_desc *gpio;
  538. gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup",
  539. GPIOD_IN);
  540. if (IS_ERR(gpio))
  541. return PTR_ERR(gpio);
  542. dev->irq = gpiod_to_irq(gpio);
  543. }
  544. dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq);
  545. /* Make sure at-least one of the GPIO is defined and that
  546. * a name is specified for this instance
  547. */
  548. if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
  549. dev_err(&pdev->dev, "invalid platform data\n");
  550. return -EINVAL;
  551. }
  552. /* Retrieve UART ACPI info */
  553. adev = ACPI_COMPANION(&dev->pdev->dev);
  554. if (!adev)
  555. return 0;
  556. acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
  557. dmi_id = dmi_first_match(bcm_wrong_irq_dmi_table);
  558. if (dmi_id) {
  559. bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low",
  560. dmi_id->ident);
  561. dev->irq_polarity = *(u8 *)dmi_id->driver_data;
  562. }
  563. return 0;
  564. }
  565. #else
  566. static int bcm_acpi_probe(struct bcm_device *dev)
  567. {
  568. return -EINVAL;
  569. }
  570. #endif /* CONFIG_ACPI */
  571. static int bcm_probe(struct platform_device *pdev)
  572. {
  573. struct bcm_device *dev;
  574. struct acpi_device_id *pdata = pdev->dev.platform_data;
  575. int ret;
  576. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  577. if (!dev)
  578. return -ENOMEM;
  579. dev->pdev = pdev;
  580. if (ACPI_HANDLE(&pdev->dev)) {
  581. ret = bcm_acpi_probe(dev);
  582. if (ret)
  583. return ret;
  584. } else if (pdata) {
  585. dev->name = pdata->id;
  586. } else {
  587. return -ENODEV;
  588. }
  589. platform_set_drvdata(pdev, dev);
  590. dev_info(&pdev->dev, "%s device registered.\n", dev->name);
  591. /* Place this instance on the device list */
  592. mutex_lock(&bcm_device_lock);
  593. list_add_tail(&dev->list, &bcm_device_list);
  594. mutex_unlock(&bcm_device_lock);
  595. bcm_gpio_set_power(dev, false);
  596. return 0;
  597. }
  598. static int bcm_remove(struct platform_device *pdev)
  599. {
  600. struct bcm_device *dev = platform_get_drvdata(pdev);
  601. mutex_lock(&bcm_device_lock);
  602. list_del(&dev->list);
  603. mutex_unlock(&bcm_device_lock);
  604. acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
  605. dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
  606. return 0;
  607. }
  608. static const struct hci_uart_proto bcm_proto = {
  609. .id = HCI_UART_BCM,
  610. .name = "BCM",
  611. .init_speed = 115200,
  612. .oper_speed = 4000000,
  613. .open = bcm_open,
  614. .close = bcm_close,
  615. .flush = bcm_flush,
  616. .setup = bcm_setup,
  617. .set_baudrate = bcm_set_baudrate,
  618. .recv = bcm_recv,
  619. .enqueue = bcm_enqueue,
  620. .dequeue = bcm_dequeue,
  621. };
  622. #ifdef CONFIG_ACPI
  623. static const struct acpi_device_id bcm_acpi_match[] = {
  624. { "BCM2E39", 0 },
  625. { "BCM2E67", 0 },
  626. { },
  627. };
  628. MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
  629. #endif
  630. /* Platform suspend and resume callbacks */
  631. static const struct dev_pm_ops bcm_pm_ops = {
  632. SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
  633. SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
  634. };
  635. static struct platform_driver bcm_driver = {
  636. .probe = bcm_probe,
  637. .remove = bcm_remove,
  638. .driver = {
  639. .name = "hci_bcm",
  640. .acpi_match_table = ACPI_PTR(bcm_acpi_match),
  641. .pm = &bcm_pm_ops,
  642. },
  643. };
  644. int __init bcm_init(void)
  645. {
  646. platform_driver_register(&bcm_driver);
  647. return hci_uart_register_proto(&bcm_proto);
  648. }
  649. int __exit bcm_deinit(void)
  650. {
  651. platform_driver_unregister(&bcm_driver);
  652. return hci_uart_unregister_proto(&bcm_proto);
  653. }