hci_bcm.c 28 KB

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