hci_bcm.c 24 KB

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