hci_bcm.c 19 KB

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