hci_bcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 <net/bluetooth/bluetooth.h>
  34. #include <net/bluetooth/hci_core.h>
  35. #include "btbcm.h"
  36. #include "hci_uart.h"
  37. struct bcm_device {
  38. struct list_head list;
  39. struct platform_device *pdev;
  40. const char *name;
  41. struct gpio_desc *device_wakeup;
  42. struct gpio_desc *shutdown;
  43. struct clk *clk;
  44. bool clk_enabled;
  45. u32 init_speed;
  46. #ifdef CONFIG_PM_SLEEP
  47. struct hci_uart *hu;
  48. bool is_suspended; /* suspend/resume flag */
  49. #endif
  50. };
  51. struct bcm_data {
  52. struct sk_buff *rx_skb;
  53. struct sk_buff_head txq;
  54. struct bcm_device *dev;
  55. };
  56. /* List of BCM BT UART devices */
  57. static DEFINE_SPINLOCK(bcm_device_lock);
  58. static LIST_HEAD(bcm_device_list);
  59. static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
  60. {
  61. struct hci_dev *hdev = hu->hdev;
  62. struct sk_buff *skb;
  63. struct bcm_update_uart_baud_rate param;
  64. if (speed > 3000000) {
  65. struct bcm_write_uart_clock_setting clock;
  66. clock.type = BCM_UART_CLOCK_48MHZ;
  67. BT_DBG("%s: Set Controller clock (%d)", hdev->name, clock.type);
  68. /* This Broadcom specific command changes the UART's controller
  69. * clock for baud rate > 3000000.
  70. */
  71. skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
  72. if (IS_ERR(skb)) {
  73. int err = PTR_ERR(skb);
  74. BT_ERR("%s: BCM: failed to write clock command (%d)",
  75. hdev->name, err);
  76. return err;
  77. }
  78. kfree_skb(skb);
  79. }
  80. BT_DBG("%s: Set Controller UART speed to %d bit/s", hdev->name, speed);
  81. param.zero = cpu_to_le16(0);
  82. param.baud_rate = cpu_to_le32(speed);
  83. /* This Broadcom specific command changes the UART's controller baud
  84. * rate.
  85. */
  86. skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
  87. HCI_INIT_TIMEOUT);
  88. if (IS_ERR(skb)) {
  89. int err = PTR_ERR(skb);
  90. BT_ERR("%s: BCM: failed to write update baudrate command (%d)",
  91. hdev->name, err);
  92. return err;
  93. }
  94. kfree_skb(skb);
  95. return 0;
  96. }
  97. /* bcm_device_exists should be protected by bcm_device_lock */
  98. static bool bcm_device_exists(struct bcm_device *device)
  99. {
  100. struct list_head *p;
  101. list_for_each(p, &bcm_device_list) {
  102. struct bcm_device *dev = list_entry(p, struct bcm_device, list);
  103. if (device == dev)
  104. return true;
  105. }
  106. return false;
  107. }
  108. static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
  109. {
  110. if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
  111. clk_enable(dev->clk);
  112. gpiod_set_value(dev->shutdown, powered);
  113. gpiod_set_value(dev->device_wakeup, powered);
  114. if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
  115. clk_disable(dev->clk);
  116. dev->clk_enabled = powered;
  117. return 0;
  118. }
  119. static int bcm_open(struct hci_uart *hu)
  120. {
  121. struct bcm_data *bcm;
  122. struct list_head *p;
  123. BT_DBG("hu %p", hu);
  124. bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
  125. if (!bcm)
  126. return -ENOMEM;
  127. skb_queue_head_init(&bcm->txq);
  128. hu->priv = bcm;
  129. spin_lock(&bcm_device_lock);
  130. list_for_each(p, &bcm_device_list) {
  131. struct bcm_device *dev = list_entry(p, struct bcm_device, list);
  132. /* Retrieve saved bcm_device based on parent of the
  133. * platform device (saved during device probe) and
  134. * parent of tty device used by hci_uart
  135. */
  136. if (hu->tty->dev->parent == dev->pdev->dev.parent) {
  137. bcm->dev = dev;
  138. hu->init_speed = dev->init_speed;
  139. #ifdef CONFIG_PM_SLEEP
  140. dev->hu = hu;
  141. #endif
  142. break;
  143. }
  144. }
  145. if (bcm->dev)
  146. bcm_gpio_set_power(bcm->dev, true);
  147. spin_unlock(&bcm_device_lock);
  148. return 0;
  149. }
  150. static int bcm_close(struct hci_uart *hu)
  151. {
  152. struct bcm_data *bcm = hu->priv;
  153. BT_DBG("hu %p", hu);
  154. /* Protect bcm->dev against removal of the device or driver */
  155. spin_lock(&bcm_device_lock);
  156. if (bcm_device_exists(bcm->dev)) {
  157. bcm_gpio_set_power(bcm->dev, false);
  158. #ifdef CONFIG_PM_SLEEP
  159. bcm->dev->hu = NULL;
  160. #endif
  161. }
  162. spin_unlock(&bcm_device_lock);
  163. skb_queue_purge(&bcm->txq);
  164. kfree_skb(bcm->rx_skb);
  165. kfree(bcm);
  166. hu->priv = NULL;
  167. return 0;
  168. }
  169. static int bcm_flush(struct hci_uart *hu)
  170. {
  171. struct bcm_data *bcm = hu->priv;
  172. BT_DBG("hu %p", hu);
  173. skb_queue_purge(&bcm->txq);
  174. return 0;
  175. }
  176. static int bcm_setup(struct hci_uart *hu)
  177. {
  178. char fw_name[64];
  179. const struct firmware *fw;
  180. unsigned int speed;
  181. int err;
  182. BT_DBG("hu %p", hu);
  183. hu->hdev->set_bdaddr = btbcm_set_bdaddr;
  184. err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name));
  185. if (err)
  186. return err;
  187. err = request_firmware(&fw, fw_name, &hu->hdev->dev);
  188. if (err < 0) {
  189. BT_INFO("%s: BCM: Patch %s not found", hu->hdev->name, fw_name);
  190. return 0;
  191. }
  192. err = btbcm_patchram(hu->hdev, fw);
  193. if (err) {
  194. BT_INFO("%s: BCM: Patch failed (%d)", hu->hdev->name, err);
  195. goto finalize;
  196. }
  197. /* Init speed if any */
  198. if (hu->init_speed)
  199. speed = hu->init_speed;
  200. else if (hu->proto->init_speed)
  201. speed = hu->proto->init_speed;
  202. else
  203. speed = 0;
  204. if (speed)
  205. hci_uart_set_baudrate(hu, speed);
  206. /* Operational speed if any */
  207. if (hu->oper_speed)
  208. speed = hu->oper_speed;
  209. else if (hu->proto->oper_speed)
  210. speed = hu->proto->oper_speed;
  211. else
  212. speed = 0;
  213. if (speed) {
  214. err = bcm_set_baudrate(hu, speed);
  215. if (!err)
  216. hci_uart_set_baudrate(hu, speed);
  217. }
  218. finalize:
  219. release_firmware(fw);
  220. err = btbcm_finalize(hu->hdev);
  221. return err;
  222. }
  223. static const struct h4_recv_pkt bcm_recv_pkts[] = {
  224. { H4_RECV_ACL, .recv = hci_recv_frame },
  225. { H4_RECV_SCO, .recv = hci_recv_frame },
  226. { H4_RECV_EVENT, .recv = hci_recv_frame },
  227. };
  228. static int bcm_recv(struct hci_uart *hu, const void *data, int count)
  229. {
  230. struct bcm_data *bcm = hu->priv;
  231. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  232. return -EUNATCH;
  233. bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
  234. bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
  235. if (IS_ERR(bcm->rx_skb)) {
  236. int err = PTR_ERR(bcm->rx_skb);
  237. BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
  238. bcm->rx_skb = NULL;
  239. return err;
  240. }
  241. return count;
  242. }
  243. static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  244. {
  245. struct bcm_data *bcm = hu->priv;
  246. BT_DBG("hu %p skb %p", hu, skb);
  247. /* Prepend skb with frame type */
  248. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  249. skb_queue_tail(&bcm->txq, skb);
  250. return 0;
  251. }
  252. static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
  253. {
  254. struct bcm_data *bcm = hu->priv;
  255. return skb_dequeue(&bcm->txq);
  256. }
  257. #ifdef CONFIG_PM_SLEEP
  258. /* Platform suspend callback */
  259. static int bcm_suspend(struct device *dev)
  260. {
  261. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  262. BT_DBG("suspend (%p): is_suspended %d", bdev, bdev->is_suspended);
  263. spin_lock(&bcm_device_lock);
  264. if (!bdev->hu)
  265. goto unlock;
  266. if (!bdev->is_suspended) {
  267. hci_uart_set_flow_control(bdev->hu, true);
  268. /* Once this callback returns, driver suspends BT via GPIO */
  269. bdev->is_suspended = true;
  270. }
  271. /* Suspend the device */
  272. if (bdev->device_wakeup) {
  273. gpiod_set_value(bdev->device_wakeup, false);
  274. BT_DBG("suspend, delaying 15 ms");
  275. mdelay(15);
  276. }
  277. unlock:
  278. spin_unlock(&bcm_device_lock);
  279. return 0;
  280. }
  281. /* Platform resume callback */
  282. static int bcm_resume(struct device *dev)
  283. {
  284. struct bcm_device *bdev = platform_get_drvdata(to_platform_device(dev));
  285. BT_DBG("resume (%p): is_suspended %d", bdev, bdev->is_suspended);
  286. spin_lock(&bcm_device_lock);
  287. if (!bdev->hu)
  288. goto unlock;
  289. if (bdev->device_wakeup) {
  290. gpiod_set_value(bdev->device_wakeup, true);
  291. BT_DBG("resume, delaying 15 ms");
  292. mdelay(15);
  293. }
  294. /* When this callback executes, the device has woken up already */
  295. if (bdev->is_suspended) {
  296. bdev->is_suspended = false;
  297. hci_uart_set_flow_control(bdev->hu, false);
  298. }
  299. unlock:
  300. spin_unlock(&bcm_device_lock);
  301. return 0;
  302. }
  303. #endif
  304. static const struct acpi_gpio_params device_wakeup_gpios = { 0, 0, false };
  305. static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
  306. static const struct acpi_gpio_mapping acpi_bcm_default_gpios[] = {
  307. { "device-wakeup-gpios", &device_wakeup_gpios, 1 },
  308. { "shutdown-gpios", &shutdown_gpios, 1 },
  309. { },
  310. };
  311. #ifdef CONFIG_ACPI
  312. static int bcm_resource(struct acpi_resource *ares, void *data)
  313. {
  314. struct bcm_device *dev = data;
  315. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  316. struct acpi_resource_uart_serialbus *sb;
  317. sb = &ares->data.uart_serial_bus;
  318. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART)
  319. dev->init_speed = sb->default_baud_rate;
  320. }
  321. /* Always tell the ACPI core to skip this resource */
  322. return 1;
  323. }
  324. static int bcm_acpi_probe(struct bcm_device *dev)
  325. {
  326. struct platform_device *pdev = dev->pdev;
  327. const struct acpi_device_id *id;
  328. struct acpi_device *adev;
  329. LIST_HEAD(resources);
  330. int ret;
  331. id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
  332. if (!id)
  333. return -ENODEV;
  334. /* Retrieve GPIO data */
  335. dev->name = dev_name(&pdev->dev);
  336. ret = acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
  337. acpi_bcm_default_gpios);
  338. if (ret)
  339. return ret;
  340. dev->clk = devm_clk_get(&pdev->dev, NULL);
  341. dev->device_wakeup = devm_gpiod_get_optional(&pdev->dev,
  342. "device-wakeup",
  343. GPIOD_OUT_LOW);
  344. if (IS_ERR(dev->device_wakeup))
  345. return PTR_ERR(dev->device_wakeup);
  346. dev->shutdown = devm_gpiod_get_optional(&pdev->dev, "shutdown",
  347. GPIOD_OUT_LOW);
  348. if (IS_ERR(dev->shutdown))
  349. return PTR_ERR(dev->shutdown);
  350. /* Make sure at-least one of the GPIO is defined and that
  351. * a name is specified for this instance
  352. */
  353. if ((!dev->device_wakeup && !dev->shutdown) || !dev->name) {
  354. dev_err(&pdev->dev, "invalid platform data\n");
  355. return -EINVAL;
  356. }
  357. /* Retrieve UART ACPI info */
  358. adev = ACPI_COMPANION(&dev->pdev->dev);
  359. if (!adev)
  360. return 0;
  361. acpi_dev_get_resources(adev, &resources, bcm_resource, dev);
  362. return 0;
  363. }
  364. #else
  365. static int bcm_acpi_probe(struct bcm_device *dev)
  366. {
  367. return -EINVAL;
  368. }
  369. #endif /* CONFIG_ACPI */
  370. static int bcm_probe(struct platform_device *pdev)
  371. {
  372. struct bcm_device *dev;
  373. struct acpi_device_id *pdata = pdev->dev.platform_data;
  374. int ret;
  375. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  376. if (!dev)
  377. return -ENOMEM;
  378. dev->pdev = pdev;
  379. if (ACPI_HANDLE(&pdev->dev)) {
  380. ret = bcm_acpi_probe(dev);
  381. if (ret)
  382. return ret;
  383. } else if (pdata) {
  384. dev->name = pdata->id;
  385. } else {
  386. return -ENODEV;
  387. }
  388. platform_set_drvdata(pdev, dev);
  389. dev_info(&pdev->dev, "%s device registered.\n", dev->name);
  390. /* Place this instance on the device list */
  391. spin_lock(&bcm_device_lock);
  392. list_add_tail(&dev->list, &bcm_device_list);
  393. spin_unlock(&bcm_device_lock);
  394. bcm_gpio_set_power(dev, false);
  395. return 0;
  396. }
  397. static int bcm_remove(struct platform_device *pdev)
  398. {
  399. struct bcm_device *dev = platform_get_drvdata(pdev);
  400. spin_lock(&bcm_device_lock);
  401. list_del(&dev->list);
  402. spin_unlock(&bcm_device_lock);
  403. acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
  404. dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
  405. return 0;
  406. }
  407. static const struct hci_uart_proto bcm_proto = {
  408. .id = HCI_UART_BCM,
  409. .name = "BCM",
  410. .init_speed = 115200,
  411. .oper_speed = 4000000,
  412. .open = bcm_open,
  413. .close = bcm_close,
  414. .flush = bcm_flush,
  415. .setup = bcm_setup,
  416. .set_baudrate = bcm_set_baudrate,
  417. .recv = bcm_recv,
  418. .enqueue = bcm_enqueue,
  419. .dequeue = bcm_dequeue,
  420. };
  421. #ifdef CONFIG_ACPI
  422. static const struct acpi_device_id bcm_acpi_match[] = {
  423. { "BCM2E39", 0 },
  424. { "BCM2E67", 0 },
  425. { },
  426. };
  427. MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
  428. #endif
  429. /* Platform suspend and resume callbacks */
  430. static SIMPLE_DEV_PM_OPS(bcm_pm_ops, bcm_suspend, bcm_resume);
  431. static struct platform_driver bcm_driver = {
  432. .probe = bcm_probe,
  433. .remove = bcm_remove,
  434. .driver = {
  435. .name = "hci_bcm",
  436. .acpi_match_table = ACPI_PTR(bcm_acpi_match),
  437. .pm = &bcm_pm_ops,
  438. },
  439. };
  440. int __init bcm_init(void)
  441. {
  442. platform_driver_register(&bcm_driver);
  443. return hci_uart_register_proto(&bcm_proto);
  444. }
  445. int __exit bcm_deinit(void)
  446. {
  447. platform_driver_unregister(&bcm_driver);
  448. return hci_uart_unregister_proto(&bcm_proto);
  449. }