hci_bcm.c 24 KB

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