hci_bcm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. if (!hci_uart_has_flow_control(hu))
  223. return -EOPNOTSUPP;
  224. bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
  225. if (!bcm)
  226. return -ENOMEM;
  227. skb_queue_head_init(&bcm->txq);
  228. hu->priv = bcm;
  229. if (!hu->tty->dev)
  230. goto out;
  231. mutex_lock(&bcm_device_lock);
  232. list_for_each(p, &bcm_device_list) {
  233. struct bcm_device *dev = list_entry(p, struct bcm_device, list);
  234. /* Retrieve saved bcm_device based on parent of the
  235. * platform device (saved during device probe) and
  236. * parent of tty device used by hci_uart
  237. */
  238. if (hu->tty->dev->parent == dev->pdev->dev.parent) {
  239. bcm->dev = dev;
  240. hu->init_speed = dev->init_speed;
  241. #ifdef CONFIG_PM
  242. dev->hu = hu;
  243. #endif
  244. bcm_gpio_set_power(bcm->dev, true);
  245. break;
  246. }
  247. }
  248. mutex_unlock(&bcm_device_lock);
  249. out:
  250. return 0;
  251. }
  252. static int bcm_close(struct hci_uart *hu)
  253. {
  254. struct bcm_data *bcm = hu->priv;
  255. struct bcm_device *bdev = bcm->dev;
  256. bt_dev_dbg(hu->hdev, "hu %p", hu);
  257. /* Protect bcm->dev against removal of the device or driver */
  258. mutex_lock(&bcm_device_lock);
  259. if (bcm_device_exists(bdev)) {
  260. bcm_gpio_set_power(bdev, false);
  261. #ifdef CONFIG_PM
  262. pm_runtime_disable(&bdev->pdev->dev);
  263. pm_runtime_set_suspended(&bdev->pdev->dev);
  264. if (device_can_wakeup(&bdev->pdev->dev)) {
  265. devm_free_irq(&bdev->pdev->dev, bdev->irq, bdev);
  266. device_init_wakeup(&bdev->pdev->dev, false);
  267. }
  268. bdev->hu = NULL;
  269. #endif
  270. }
  271. mutex_unlock(&bcm_device_lock);
  272. skb_queue_purge(&bcm->txq);
  273. kfree_skb(bcm->rx_skb);
  274. kfree(bcm);
  275. hu->priv = NULL;
  276. return 0;
  277. }
  278. static int bcm_flush(struct hci_uart *hu)
  279. {
  280. struct bcm_data *bcm = hu->priv;
  281. bt_dev_dbg(hu->hdev, "hu %p", hu);
  282. skb_queue_purge(&bcm->txq);
  283. return 0;
  284. }
  285. static int bcm_setup(struct hci_uart *hu)
  286. {
  287. struct bcm_data *bcm = hu->priv;
  288. char fw_name[64];
  289. const struct firmware *fw;
  290. unsigned int speed;
  291. int err;
  292. bt_dev_dbg(hu->hdev, "hu %p", hu);
  293. hu->hdev->set_diag = bcm_set_diag;
  294. hu->hdev->set_bdaddr = btbcm_set_bdaddr;
  295. err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
  296. if (err)
  297. return err;
  298. err = request_firmware(&fw, fw_name, &hu->hdev->dev);
  299. if (err < 0) {
  300. bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
  301. return 0;
  302. }
  303. err = btbcm_patchram(hu->hdev, fw);
  304. if (err) {
  305. bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
  306. goto finalize;
  307. }
  308. /* Init speed if any */
  309. if (hu->init_speed)
  310. speed = hu->init_speed;
  311. else if (hu->proto->init_speed)
  312. speed = hu->proto->init_speed;
  313. else
  314. speed = 0;
  315. if (speed)
  316. hci_uart_set_baudrate(hu, speed);
  317. /* Operational speed if any */
  318. if (hu->oper_speed)
  319. speed = hu->oper_speed;
  320. else if (hu->proto->oper_speed)
  321. speed = hu->proto->oper_speed;
  322. else
  323. speed = 0;
  324. if (speed) {
  325. err = bcm_set_baudrate(hu, speed);
  326. if (!err)
  327. hci_uart_set_baudrate(hu, speed);
  328. }
  329. finalize:
  330. release_firmware(fw);
  331. err = btbcm_finalize(hu->hdev);
  332. if (err)
  333. return err;
  334. err = bcm_request_irq(bcm);
  335. if (!err)
  336. err = bcm_setup_sleep(hu);
  337. return err;
  338. }
  339. #define BCM_RECV_LM_DIAG \
  340. .type = BCM_LM_DIAG_PKT, \
  341. .hlen = BCM_LM_DIAG_SIZE, \
  342. .loff = 0, \
  343. .lsize = 0, \
  344. .maxlen = BCM_LM_DIAG_SIZE
  345. static const struct h4_recv_pkt bcm_recv_pkts[] = {
  346. { H4_RECV_ACL, .recv = hci_recv_frame },
  347. { H4_RECV_SCO, .recv = hci_recv_frame },
  348. { H4_RECV_EVENT, .recv = hci_recv_frame },
  349. { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
  350. };
  351. static int bcm_recv(struct hci_uart *hu, const void *data, int count)
  352. {
  353. struct bcm_data *bcm = hu->priv;
  354. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  355. return -EUNATCH;
  356. bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
  357. bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
  358. if (IS_ERR(bcm->rx_skb)) {
  359. int err = PTR_ERR(bcm->rx_skb);
  360. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  361. bcm->rx_skb = NULL;
  362. return err;
  363. } else if (!bcm->rx_skb) {
  364. /* Delay auto-suspend when receiving completed packet */
  365. mutex_lock(&bcm_device_lock);
  366. if (bcm->dev && bcm_device_exists(bcm->dev)) {
  367. pm_runtime_get(&bcm->dev->pdev->dev);
  368. pm_runtime_mark_last_busy(&bcm->dev->pdev->dev);
  369. pm_runtime_put_autosuspend(&bcm->dev->pdev->dev);
  370. }
  371. mutex_unlock(&bcm_device_lock);
  372. }
  373. return count;
  374. }
  375. static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  376. {
  377. struct bcm_data *bcm = hu->priv;
  378. bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
  379. /* Prepend skb with frame type */
  380. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  381. skb_queue_tail(&bcm->txq, skb);
  382. return 0;
  383. }
  384. static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
  385. {
  386. struct bcm_data *bcm = hu->priv;
  387. struct sk_buff *skb = NULL;
  388. struct bcm_device *bdev = NULL;
  389. mutex_lock(&bcm_device_lock);
  390. if (bcm_device_exists(bcm->dev)) {
  391. bdev = bcm->dev;
  392. pm_runtime_get_sync(&bdev->pdev->dev);
  393. /* Shall be resumed here */
  394. }
  395. skb = skb_dequeue(&bcm->txq);
  396. if (bdev) {
  397. pm_runtime_mark_last_busy(&bdev->pdev->dev);
  398. pm_runtime_put_autosuspend(&bdev->pdev->dev);
  399. }
  400. mutex_unlock(&bcm_device_lock);
  401. return skb;
  402. }
  403. #ifdef CONFIG_PM
  404. static int bcm_suspend_device(struct device *dev)
  405. {
  406. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  407. bt_dev_dbg(bdev, "");
  408. if (!bdev->is_suspended && bdev->hu) {
  409. hci_uart_set_flow_control(bdev->hu, true);
  410. /* Once this returns, driver suspends BT via GPIO */
  411. bdev->is_suspended = true;
  412. }
  413. /* Suspend the device */
  414. if (bdev->device_wakeup) {
  415. gpiod_set_value(bdev->device_wakeup, false);
  416. bt_dev_dbg(bdev, "suspend, delaying 15 ms");
  417. mdelay(15);
  418. }
  419. return 0;
  420. }
  421. static int bcm_resume_device(struct device *dev)
  422. {
  423. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  424. bt_dev_dbg(bdev, "");
  425. if (bdev->device_wakeup) {
  426. gpiod_set_value(bdev->device_wakeup, true);
  427. bt_dev_dbg(bdev, "resume, delaying 15 ms");
  428. mdelay(15);
  429. }
  430. /* When this executes, the device has woken up already */
  431. if (bdev->is_suspended && bdev->hu) {
  432. bdev->is_suspended = false;
  433. hci_uart_set_flow_control(bdev->hu, false);
  434. }
  435. return 0;
  436. }
  437. #endif
  438. #ifdef CONFIG_PM_SLEEP
  439. /* Platform suspend callback */
  440. static int bcm_suspend(struct device *dev)
  441. {
  442. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  443. int error;
  444. bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
  445. /* bcm_suspend can be called at any time as long as platform device is
  446. * bound, so it should use bcm_device_lock to protect access to hci_uart
  447. * and device_wake-up GPIO.
  448. */
  449. mutex_lock(&bcm_device_lock);
  450. if (!bdev->hu)
  451. goto unlock;
  452. if (pm_runtime_active(dev))
  453. bcm_suspend_device(dev);
  454. if (device_may_wakeup(&bdev->pdev->dev)) {
  455. error = enable_irq_wake(bdev->irq);
  456. if (!error)
  457. bt_dev_dbg(bdev, "BCM irq: enabled");
  458. }
  459. unlock:
  460. mutex_unlock(&bcm_device_lock);
  461. return 0;
  462. }
  463. /* Platform resume callback */
  464. static int bcm_resume(struct device *dev)
  465. {
  466. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  467. bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
  468. /* bcm_resume can be called at any time as long as platform device is
  469. * bound, so it should use bcm_device_lock to protect access to hci_uart
  470. * and device_wake-up GPIO.
  471. */
  472. mutex_lock(&bcm_device_lock);
  473. if (!bdev->hu)
  474. goto unlock;
  475. if (device_may_wakeup(&bdev->pdev->dev)) {
  476. disable_irq_wake(bdev->irq);
  477. bt_dev_dbg(bdev, "BCM irq: disabled");
  478. }
  479. bcm_resume_device(dev);
  480. unlock:
  481. mutex_unlock(&bcm_device_lock);
  482. pm_runtime_disable(dev);
  483. pm_runtime_set_active(dev);
  484. pm_runtime_enable(dev);
  485. return 0;
  486. }
  487. #endif
  488. static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
  489. static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
  490. static const struct acpi_gpio_params host_wakeup_gpios = { 2, 0, false };
  491. static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
  492. { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
  493. { "shutdown-gpios", &shutdown_gpios, 1 },
  494. { "host-wakeup-gpios", &host_wakeup_gpios, 1 },
  495. { },
  496. };
  497. #ifdef CONFIG_ACPI
  498. static u8 acpi_active_low = ACPI_ACTIVE_LOW;
  499. /* IRQ polarity of some chipsets are not defined correctly in ACPI table. */
  500. static const struct dmi_system_id bcm_wrong_irq_dmi_table[] = {
  501. {
  502. .ident = "Asus T100TA",
  503. .matches = {
  504. DMI_EXACT_MATCH(DMI_SYS_VENDOR,
  505. "ASUSTeK COMPUTER INC."),
  506. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
  507. },
  508. .driver_data = &acpi_active_low,
  509. },
  510. { /* Handle ThinkPad 8 tablets with BCM2E55 chipset ACPI ID */
  511. .ident = "Lenovo ThinkPad 8",
  512. .matches = {
  513. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  514. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "ThinkPad 8"),
  515. },
  516. .driver_data = &acpi_active_low,
  517. },
  518. { }
  519. };
  520. static int bcm_resource(struct acpi_resource *ares, void *data)
  521. {
  522. struct bcm_device *dev = data;
  523. struct acpi_resource_extended_irq *irq;
  524. struct acpi_resource_gpio *gpio;
  525. struct acpi_resource_uart_serialbus *sb;
  526. switch (ares->type) {
  527. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  528. irq = &ares->data.extended_irq;
  529. dev->irq_polarity = irq->polarity;
  530. break;
  531. case ACPI_RESOURCE_TYPE_GPIO:
  532. gpio = &ares->data.gpio;
  533. if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
  534. dev->irq_polarity = gpio->polarity;
  535. break;
  536. case ACPI_RESOURCE_TYPE_SERIAL_BUS:
  537. sb = &ares->data.uart_serial_bus;
  538. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
  539. dev->init_speed = sb->default_baud_rate;
  540. break;
  541. default:
  542. break;
  543. }
  544. /* Always tell the ACPI core to skip this resource */
  545. return 1;
  546. }
  547. static int bcm_acpi_probe(struct bcm_device *dev)
  548. {
  549. struct platform_device *pdev = dev->pdev;
  550. LIST_HEAD(resources);
  551. const struct dmi_system_id *dmi_id;
  552. int ret;
  553. /* Retrieve GPIO data */
  554. dev->name = dev_name(&pdev->dev);
  555. ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
  556. acpi_bcm_default_gpios);
  557. if (ret)
  558. return ret;
  559. dev->clk = devm_clk_get(&pdev->dev, NULL);
  560. dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
  561. "device-wakeup",
  562. GPIOD_OUT_LOW);
  563. if (IS_ERR(dev->device_wakeup))
  564. return PTR_ERR(dev->device_wakeup);
  565. dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
  566. GPIOD_OUT_LOW);
  567. if (IS_ERR(dev->shutdown))
  568. return PTR_ERR(dev->shutdown);
  569. /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
  570. dev->irq = platform_get_irq(pdev, 0);
  571. if (dev->irq <= 0) {
  572. struct gpio_desc *gpio;
  573. gpio = devm_gpiod_get_optional(&pdev->dev, "host-wakeup",
  574. GPIOD_IN);
  575. if (IS_ERR(gpio))
  576. return PTR_ERR(gpio);
  577. dev->irq = gpiod_to_irq(gpio);
  578. }
  579. dev_info(&pdev->dev, "BCM irq: %d\n", dev->irq);
  580. /* Make sure at-least one of the GPIO is defined and that
  581. * a name is specified for this instance
  582. */
  583. if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
  584. dev_err(&pdev->dev, "invalid platform data\n");
  585. return -EINVAL;
  586. }
  587. /* Retrieve UART ACPI info */
  588. ret = acpi_dev_get_resources(ACPI_COMPANION(&dev->pdev->dev),
  589. &resources, bcm_resource, dev);
  590. if (ret < 0)
  591. return ret;
  592. acpi_dev_free_resource_list(&resources);
  593. dmi_id = dmi_first_match(bcm_wrong_irq_dmi_table);
  594. if (dmi_id) {
  595. bt_dev_warn(dev, "%s: Overwriting IRQ polarity to active low",
  596. dmi_id->ident);
  597. dev->irq_polarity = *(u8 *)dmi_id->driver_data;
  598. }
  599. return 0;
  600. }
  601. #else
  602. static int bcm_acpi_probe(struct bcm_device *dev)
  603. {
  604. return -EINVAL;
  605. }
  606. #endif /* CONFIG_ACPI */
  607. static int bcm_probe(struct platform_device *pdev)
  608. {
  609. struct bcm_device *dev;
  610. int ret;
  611. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  612. if (!dev)
  613. return -ENOMEM;
  614. dev->pdev = pdev;
  615. ret = bcm_acpi_probe(dev);
  616. if (ret)
  617. return ret;
  618. platform_set_drvdata(pdev, dev);
  619. dev_info(&pdev->dev, "%s device registered.\n", dev->name);
  620. /* Place this instance on the device list */
  621. mutex_lock(&bcm_device_lock);
  622. list_add_tail(&dev->list, &bcm_device_list);
  623. mutex_unlock(&bcm_device_lock);
  624. bcm_gpio_set_power(dev, false);
  625. return 0;
  626. }
  627. static int bcm_remove(struct platform_device *pdev)
  628. {
  629. struct bcm_device *dev = platform_get_drvdata(pdev);
  630. mutex_lock(&bcm_device_lock);
  631. list_del(&dev->list);
  632. mutex_unlock(&bcm_device_lock);
  633. acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
  634. dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
  635. return 0;
  636. }
  637. static const struct hci_uart_proto bcm_proto = {
  638. .id = HCI_UART_BCM,
  639. .name = "Broadcom",
  640. .manufacturer = 15,
  641. .init_speed = 115200,
  642. .oper_speed = 4000000,
  643. .open = bcm_open,
  644. .close = bcm_close,
  645. .flush = bcm_flush,
  646. .setup = bcm_setup,
  647. .set_baudrate = bcm_set_baudrate,
  648. .recv = bcm_recv,
  649. .enqueue = bcm_enqueue,
  650. .dequeue = bcm_dequeue,
  651. };
  652. #ifdef CONFIG_ACPI
  653. static const struct acpi_device_id bcm_acpi_match[] = {
  654. { "BCM2E1A", 0 },
  655. { "BCM2E39", 0 },
  656. { "BCM2E3A", 0 },
  657. { "BCM2E3D", 0 },
  658. { "BCM2E3F", 0 },
  659. { "BCM2E40", 0 },
  660. { "BCM2E54", 0 },
  661. { "BCM2E55", 0 },
  662. { "BCM2E64", 0 },
  663. { "BCM2E65", 0 },
  664. { "BCM2E67", 0 },
  665. { "BCM2E71", 0 },
  666. { "BCM2E7B", 0 },
  667. { "BCM2E7C", 0 },
  668. { },
  669. };
  670. MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
  671. #endif
  672. /* Platform suspend and resume callbacks */
  673. static const struct dev_pm_ops bcm_pm_ops = {
  674. SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
  675. SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
  676. };
  677. static struct platform_driver bcm_driver = {
  678. .probe = bcm_probe,
  679. .remove = bcm_remove,
  680. .driver = {
  681. .name = "hci_bcm",
  682. .acpi_match_table = ACPI_PTR(bcm_acpi_match),
  683. .pm = &bcm_pm_ops,
  684. },
  685. };
  686. int __init bcm_init(void)
  687. {
  688. platform_driver_register(&bcm_driver);
  689. return hci_uart_register_proto(&bcm_proto);
  690. }
  691. int __exit bcm_deinit(void)
  692. {
  693. platform_driver_unregister(&bcm_driver);
  694. return hci_uart_unregister_proto(&bcm_proto);
  695. }