hci_bcm.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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. int gpio_count;
  93. int gpio_int_idx;
  94. #endif
  95. struct clk *clk;
  96. bool clk_enabled;
  97. u32 init_speed;
  98. u32 oper_speed;
  99. int irq;
  100. bool irq_active_low;
  101. #ifdef CONFIG_PM
  102. struct hci_uart *hu;
  103. bool is_suspended;
  104. #endif
  105. };
  106. /* generic bcm uart resources */
  107. struct bcm_data {
  108. struct sk_buff *rx_skb;
  109. struct sk_buff_head txq;
  110. struct bcm_device *dev;
  111. };
  112. /* List of BCM BT UART devices */
  113. static DEFINE_MUTEX(bcm_device_lock);
  114. static LIST_HEAD(bcm_device_list);
  115. static int irq_polarity = -1;
  116. module_param(irq_polarity, int, 0444);
  117. MODULE_PARM_DESC(irq_polarity, "IRQ polarity 0: active-high 1: active-low");
  118. static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
  119. {
  120. if (hu->serdev)
  121. serdev_device_set_baudrate(hu->serdev, speed);
  122. else
  123. hci_uart_set_baudrate(hu, speed);
  124. }
  125. static int bcm_set_baudrate(struct hci_uart *hu, unsigned int speed)
  126. {
  127. struct hci_dev *hdev = hu->hdev;
  128. struct sk_buff *skb;
  129. struct bcm_update_uart_baud_rate param;
  130. if (speed > 3000000) {
  131. struct bcm_write_uart_clock_setting clock;
  132. clock.type = BCM_UART_CLOCK_48MHZ;
  133. bt_dev_dbg(hdev, "Set Controller clock (%d)", clock.type);
  134. /* This Broadcom specific command changes the UART's controller
  135. * clock for baud rate > 3000000.
  136. */
  137. skb = __hci_cmd_sync(hdev, 0xfc45, 1, &clock, HCI_INIT_TIMEOUT);
  138. if (IS_ERR(skb)) {
  139. int err = PTR_ERR(skb);
  140. bt_dev_err(hdev, "BCM: failed to write clock (%d)",
  141. err);
  142. return err;
  143. }
  144. kfree_skb(skb);
  145. }
  146. bt_dev_dbg(hdev, "Set Controller UART speed to %d bit/s", speed);
  147. param.zero = cpu_to_le16(0);
  148. param.baud_rate = cpu_to_le32(speed);
  149. /* This Broadcom specific command changes the UART's controller baud
  150. * rate.
  151. */
  152. skb = __hci_cmd_sync(hdev, 0xfc18, sizeof(param), &param,
  153. HCI_INIT_TIMEOUT);
  154. if (IS_ERR(skb)) {
  155. int err = PTR_ERR(skb);
  156. bt_dev_err(hdev, "BCM: failed to write update baudrate (%d)",
  157. err);
  158. return err;
  159. }
  160. kfree_skb(skb);
  161. return 0;
  162. }
  163. /* bcm_device_exists should be protected by bcm_device_lock */
  164. static bool bcm_device_exists(struct bcm_device *device)
  165. {
  166. struct list_head *p;
  167. #ifdef CONFIG_PM
  168. /* Devices using serdev always exist */
  169. if (device && device->hu && device->hu->serdev)
  170. return true;
  171. #endif
  172. list_for_each(p, &bcm_device_list) {
  173. struct bcm_device *dev = list_entry(p, struct bcm_device, list);
  174. if (device == dev)
  175. return true;
  176. }
  177. return false;
  178. }
  179. static int bcm_gpio_set_power(struct bcm_device *dev, bool powered)
  180. {
  181. int err;
  182. if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled) {
  183. err = clk_prepare_enable(dev->clk);
  184. if (err)
  185. return err;
  186. }
  187. err = dev->set_shutdown(dev, powered);
  188. if (err)
  189. goto err_clk_disable;
  190. err = dev->set_device_wakeup(dev, powered);
  191. if (err)
  192. goto err_revert_shutdown;
  193. if (!powered && !IS_ERR(dev->clk) && dev->clk_enabled)
  194. clk_disable_unprepare(dev->clk);
  195. dev->clk_enabled = powered;
  196. return 0;
  197. err_revert_shutdown:
  198. dev->set_shutdown(dev, !powered);
  199. err_clk_disable:
  200. if (powered && !IS_ERR(dev->clk) && !dev->clk_enabled)
  201. clk_disable_unprepare(dev->clk);
  202. return err;
  203. }
  204. #ifdef CONFIG_PM
  205. static irqreturn_t bcm_host_wake(int irq, void *data)
  206. {
  207. struct bcm_device *bdev = data;
  208. bt_dev_dbg(bdev, "Host wake IRQ");
  209. pm_runtime_get(bdev->dev);
  210. pm_runtime_mark_last_busy(bdev->dev);
  211. pm_runtime_put_autosuspend(bdev->dev);
  212. return IRQ_HANDLED;
  213. }
  214. static int bcm_request_irq(struct bcm_data *bcm)
  215. {
  216. struct bcm_device *bdev = bcm->dev;
  217. int err;
  218. mutex_lock(&bcm_device_lock);
  219. if (!bcm_device_exists(bdev)) {
  220. err = -ENODEV;
  221. goto unlock;
  222. }
  223. if (bdev->irq <= 0) {
  224. err = -EOPNOTSUPP;
  225. goto unlock;
  226. }
  227. err = devm_request_irq(bdev->dev, bdev->irq, bcm_host_wake,
  228. bdev->irq_active_low ? IRQF_TRIGGER_FALLING :
  229. IRQF_TRIGGER_RISING,
  230. "host_wake", bdev);
  231. if (err) {
  232. bdev->irq = err;
  233. goto unlock;
  234. }
  235. device_init_wakeup(bdev->dev, true);
  236. pm_runtime_set_autosuspend_delay(bdev->dev,
  237. BCM_AUTOSUSPEND_DELAY);
  238. pm_runtime_use_autosuspend(bdev->dev);
  239. pm_runtime_set_active(bdev->dev);
  240. pm_runtime_enable(bdev->dev);
  241. unlock:
  242. mutex_unlock(&bcm_device_lock);
  243. return err;
  244. }
  245. static const struct bcm_set_sleep_mode default_sleep_params = {
  246. .sleep_mode = 1, /* 0=Disabled, 1=UART, 2=Reserved, 3=USB */
  247. .idle_host = 2, /* idle threshold HOST, in 300ms */
  248. .idle_dev = 2, /* idle threshold device, in 300ms */
  249. .bt_wake_active = 1, /* BT_WAKE active mode: 1 = high, 0 = low */
  250. .host_wake_active = 0, /* HOST_WAKE active mode: 1 = high, 0 = low */
  251. .allow_host_sleep = 1, /* Allow host sleep in SCO flag */
  252. .combine_modes = 1, /* Combine sleep and LPM flag */
  253. .tristate_control = 0, /* Allow tri-state control of UART tx flag */
  254. /* Irrelevant USB flags */
  255. .usb_auto_sleep = 0,
  256. .usb_resume_timeout = 0,
  257. .break_to_host = 0,
  258. .pulsed_host_wake = 1,
  259. };
  260. static int bcm_setup_sleep(struct hci_uart *hu)
  261. {
  262. struct bcm_data *bcm = hu->priv;
  263. struct sk_buff *skb;
  264. struct bcm_set_sleep_mode sleep_params = default_sleep_params;
  265. sleep_params.host_wake_active = !bcm->dev->irq_active_low;
  266. skb = __hci_cmd_sync(hu->hdev, 0xfc27, sizeof(sleep_params),
  267. &sleep_params, HCI_INIT_TIMEOUT);
  268. if (IS_ERR(skb)) {
  269. int err = PTR_ERR(skb);
  270. bt_dev_err(hu->hdev, "Sleep VSC failed (%d)", err);
  271. return err;
  272. }
  273. kfree_skb(skb);
  274. bt_dev_dbg(hu->hdev, "Set Sleep Parameters VSC succeeded");
  275. return 0;
  276. }
  277. #else
  278. static inline int bcm_request_irq(struct bcm_data *bcm) { return 0; }
  279. static inline int bcm_setup_sleep(struct hci_uart *hu) { return 0; }
  280. #endif
  281. static int bcm_set_diag(struct hci_dev *hdev, bool enable)
  282. {
  283. struct hci_uart *hu = hci_get_drvdata(hdev);
  284. struct bcm_data *bcm = hu->priv;
  285. struct sk_buff *skb;
  286. if (!test_bit(HCI_RUNNING, &hdev->flags))
  287. return -ENETDOWN;
  288. skb = bt_skb_alloc(3, GFP_KERNEL);
  289. if (!skb)
  290. return -ENOMEM;
  291. skb_put_u8(skb, BCM_LM_DIAG_PKT);
  292. skb_put_u8(skb, 0xf0);
  293. skb_put_u8(skb, enable);
  294. skb_queue_tail(&bcm->txq, skb);
  295. hci_uart_tx_wakeup(hu);
  296. return 0;
  297. }
  298. static int bcm_open(struct hci_uart *hu)
  299. {
  300. struct bcm_data *bcm;
  301. struct list_head *p;
  302. int err;
  303. bt_dev_dbg(hu->hdev, "hu %p", hu);
  304. if (!hci_uart_has_flow_control(hu))
  305. return -EOPNOTSUPP;
  306. bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
  307. if (!bcm)
  308. return -ENOMEM;
  309. skb_queue_head_init(&bcm->txq);
  310. hu->priv = bcm;
  311. mutex_lock(&bcm_device_lock);
  312. if (hu->serdev) {
  313. bcm->dev = serdev_device_get_drvdata(hu->serdev);
  314. goto out;
  315. }
  316. if (!hu->tty->dev)
  317. goto out;
  318. list_for_each(p, &bcm_device_list) {
  319. struct bcm_device *dev = list_entry(p, struct bcm_device, list);
  320. /* Retrieve saved bcm_device based on parent of the
  321. * platform device (saved during device probe) and
  322. * parent of tty device used by hci_uart
  323. */
  324. if (hu->tty->dev->parent == dev->dev->parent) {
  325. bcm->dev = dev;
  326. #ifdef CONFIG_PM
  327. dev->hu = hu;
  328. #endif
  329. break;
  330. }
  331. }
  332. out:
  333. if (bcm->dev) {
  334. hu->init_speed = bcm->dev->init_speed;
  335. hu->oper_speed = bcm->dev->oper_speed;
  336. err = bcm_gpio_set_power(bcm->dev, true);
  337. if (err)
  338. goto err_unset_hu;
  339. }
  340. mutex_unlock(&bcm_device_lock);
  341. return 0;
  342. err_unset_hu:
  343. #ifdef CONFIG_PM
  344. if (!hu->serdev)
  345. bcm->dev->hu = NULL;
  346. #endif
  347. mutex_unlock(&bcm_device_lock);
  348. hu->priv = NULL;
  349. kfree(bcm);
  350. return err;
  351. }
  352. static int bcm_close(struct hci_uart *hu)
  353. {
  354. struct bcm_data *bcm = hu->priv;
  355. struct bcm_device *bdev = NULL;
  356. int err;
  357. bt_dev_dbg(hu->hdev, "hu %p", hu);
  358. /* Protect bcm->dev against removal of the device or driver */
  359. mutex_lock(&bcm_device_lock);
  360. if (hu->serdev) {
  361. bdev = serdev_device_get_drvdata(hu->serdev);
  362. } else if (bcm_device_exists(bcm->dev)) {
  363. bdev = bcm->dev;
  364. #ifdef CONFIG_PM
  365. bdev->hu = NULL;
  366. #endif
  367. }
  368. if (bdev) {
  369. if (IS_ENABLED(CONFIG_PM) && bdev->irq > 0) {
  370. devm_free_irq(bdev->dev, bdev->irq, bdev);
  371. device_init_wakeup(bdev->dev, false);
  372. pm_runtime_disable(bdev->dev);
  373. }
  374. err = bcm_gpio_set_power(bdev, false);
  375. if (err)
  376. bt_dev_err(hu->hdev, "Failed to power down");
  377. else
  378. pm_runtime_set_suspended(bdev->dev);
  379. }
  380. mutex_unlock(&bcm_device_lock);
  381. skb_queue_purge(&bcm->txq);
  382. kfree_skb(bcm->rx_skb);
  383. kfree(bcm);
  384. hu->priv = NULL;
  385. return 0;
  386. }
  387. static int bcm_flush(struct hci_uart *hu)
  388. {
  389. struct bcm_data *bcm = hu->priv;
  390. bt_dev_dbg(hu->hdev, "hu %p", hu);
  391. skb_queue_purge(&bcm->txq);
  392. return 0;
  393. }
  394. static int bcm_setup(struct hci_uart *hu)
  395. {
  396. struct bcm_data *bcm = hu->priv;
  397. char fw_name[64];
  398. const struct firmware *fw;
  399. unsigned int speed;
  400. int err;
  401. bt_dev_dbg(hu->hdev, "hu %p", hu);
  402. hu->hdev->set_diag = bcm_set_diag;
  403. hu->hdev->set_bdaddr = btbcm_set_bdaddr;
  404. err = btbcm_initialize(hu->hdev, fw_name, sizeof(fw_name), false);
  405. if (err)
  406. return err;
  407. err = request_firmware(&fw, fw_name, &hu->hdev->dev);
  408. if (err < 0) {
  409. bt_dev_info(hu->hdev, "BCM: Patch %s not found", fw_name);
  410. return 0;
  411. }
  412. err = btbcm_patchram(hu->hdev, fw);
  413. if (err) {
  414. bt_dev_info(hu->hdev, "BCM: Patch failed (%d)", err);
  415. goto finalize;
  416. }
  417. /* Init speed if any */
  418. if (hu->init_speed)
  419. speed = hu->init_speed;
  420. else if (hu->proto->init_speed)
  421. speed = hu->proto->init_speed;
  422. else
  423. speed = 0;
  424. if (speed)
  425. host_set_baudrate(hu, speed);
  426. /* Operational speed if any */
  427. if (hu->oper_speed)
  428. speed = hu->oper_speed;
  429. else if (hu->proto->oper_speed)
  430. speed = hu->proto->oper_speed;
  431. else
  432. speed = 0;
  433. if (speed) {
  434. err = bcm_set_baudrate(hu, speed);
  435. if (!err)
  436. host_set_baudrate(hu, speed);
  437. }
  438. finalize:
  439. release_firmware(fw);
  440. err = btbcm_finalize(hu->hdev);
  441. if (err)
  442. return err;
  443. if (!bcm_request_irq(bcm))
  444. err = bcm_setup_sleep(hu);
  445. return err;
  446. }
  447. #define BCM_RECV_LM_DIAG \
  448. .type = BCM_LM_DIAG_PKT, \
  449. .hlen = BCM_LM_DIAG_SIZE, \
  450. .loff = 0, \
  451. .lsize = 0, \
  452. .maxlen = BCM_LM_DIAG_SIZE
  453. #define BCM_RECV_NULL \
  454. .type = BCM_NULL_PKT, \
  455. .hlen = BCM_NULL_SIZE, \
  456. .loff = 0, \
  457. .lsize = 0, \
  458. .maxlen = BCM_NULL_SIZE
  459. static const struct h4_recv_pkt bcm_recv_pkts[] = {
  460. { H4_RECV_ACL, .recv = hci_recv_frame },
  461. { H4_RECV_SCO, .recv = hci_recv_frame },
  462. { H4_RECV_EVENT, .recv = hci_recv_frame },
  463. { BCM_RECV_LM_DIAG, .recv = hci_recv_diag },
  464. { BCM_RECV_NULL, .recv = hci_recv_diag },
  465. };
  466. static int bcm_recv(struct hci_uart *hu, const void *data, int count)
  467. {
  468. struct bcm_data *bcm = hu->priv;
  469. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  470. return -EUNATCH;
  471. bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
  472. bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
  473. if (IS_ERR(bcm->rx_skb)) {
  474. int err = PTR_ERR(bcm->rx_skb);
  475. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  476. bcm->rx_skb = NULL;
  477. return err;
  478. } else if (!bcm->rx_skb) {
  479. /* Delay auto-suspend when receiving completed packet */
  480. mutex_lock(&bcm_device_lock);
  481. if (bcm->dev && bcm_device_exists(bcm->dev)) {
  482. pm_runtime_get(bcm->dev->dev);
  483. pm_runtime_mark_last_busy(bcm->dev->dev);
  484. pm_runtime_put_autosuspend(bcm->dev->dev);
  485. }
  486. mutex_unlock(&bcm_device_lock);
  487. }
  488. return count;
  489. }
  490. static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  491. {
  492. struct bcm_data *bcm = hu->priv;
  493. bt_dev_dbg(hu->hdev, "hu %p skb %p", hu, skb);
  494. /* Prepend skb with frame type */
  495. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  496. skb_queue_tail(&bcm->txq, skb);
  497. return 0;
  498. }
  499. static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
  500. {
  501. struct bcm_data *bcm = hu->priv;
  502. struct sk_buff *skb = NULL;
  503. struct bcm_device *bdev = NULL;
  504. mutex_lock(&bcm_device_lock);
  505. if (bcm_device_exists(bcm->dev)) {
  506. bdev = bcm->dev;
  507. pm_runtime_get_sync(bdev->dev);
  508. /* Shall be resumed here */
  509. }
  510. skb = skb_dequeue(&bcm->txq);
  511. if (bdev) {
  512. pm_runtime_mark_last_busy(bdev->dev);
  513. pm_runtime_put_autosuspend(bdev->dev);
  514. }
  515. mutex_unlock(&bcm_device_lock);
  516. return skb;
  517. }
  518. #ifdef CONFIG_PM
  519. static int bcm_suspend_device(struct device *dev)
  520. {
  521. struct bcm_device *bdev = dev_get_drvdata(dev);
  522. int err;
  523. bt_dev_dbg(bdev, "");
  524. if (!bdev->is_suspended && bdev->hu) {
  525. hci_uart_set_flow_control(bdev->hu, true);
  526. /* Once this returns, driver suspends BT via GPIO */
  527. bdev->is_suspended = true;
  528. }
  529. /* Suspend the device */
  530. err = bdev->set_device_wakeup(bdev, false);
  531. if (err) {
  532. if (bdev->is_suspended && bdev->hu) {
  533. bdev->is_suspended = false;
  534. hci_uart_set_flow_control(bdev->hu, false);
  535. }
  536. return -EBUSY;
  537. }
  538. bt_dev_dbg(bdev, "suspend, delaying 15 ms");
  539. msleep(15);
  540. return 0;
  541. }
  542. static int bcm_resume_device(struct device *dev)
  543. {
  544. struct bcm_device *bdev = dev_get_drvdata(dev);
  545. int err;
  546. bt_dev_dbg(bdev, "");
  547. err = bdev->set_device_wakeup(bdev, true);
  548. if (err) {
  549. dev_err(dev, "Failed to power up\n");
  550. return err;
  551. }
  552. bt_dev_dbg(bdev, "resume, delaying 15 ms");
  553. msleep(15);
  554. /* When this executes, the device has woken up already */
  555. if (bdev->is_suspended && bdev->hu) {
  556. bdev->is_suspended = false;
  557. hci_uart_set_flow_control(bdev->hu, false);
  558. }
  559. return 0;
  560. }
  561. #endif
  562. #ifdef CONFIG_PM_SLEEP
  563. /* suspend callback */
  564. static int bcm_suspend(struct device *dev)
  565. {
  566. struct bcm_device *bdev = dev_get_drvdata(dev);
  567. int error;
  568. bt_dev_dbg(bdev, "suspend: is_suspended %d", bdev->is_suspended);
  569. /*
  570. * When used with a device instantiated as platform_device, bcm_suspend
  571. * can be called at any time as long as the platform device is bound,
  572. * so it should use bcm_device_lock to protect access to hci_uart
  573. * and device_wake-up GPIO.
  574. */
  575. mutex_lock(&bcm_device_lock);
  576. if (!bdev->hu)
  577. goto unlock;
  578. if (pm_runtime_active(dev))
  579. bcm_suspend_device(dev);
  580. if (device_may_wakeup(dev) && bdev->irq > 0) {
  581. error = enable_irq_wake(bdev->irq);
  582. if (!error)
  583. bt_dev_dbg(bdev, "BCM irq: enabled");
  584. }
  585. unlock:
  586. mutex_unlock(&bcm_device_lock);
  587. return 0;
  588. }
  589. /* resume callback */
  590. static int bcm_resume(struct device *dev)
  591. {
  592. struct bcm_device *bdev = dev_get_drvdata(dev);
  593. int err = 0;
  594. bt_dev_dbg(bdev, "resume: is_suspended %d", bdev->is_suspended);
  595. /*
  596. * When used with a device instantiated as platform_device, bcm_resume
  597. * can be called at any time as long as platform device is bound,
  598. * so it should use bcm_device_lock to protect access to hci_uart
  599. * and device_wake-up GPIO.
  600. */
  601. mutex_lock(&bcm_device_lock);
  602. if (!bdev->hu)
  603. goto unlock;
  604. if (device_may_wakeup(dev) && bdev->irq > 0) {
  605. disable_irq_wake(bdev->irq);
  606. bt_dev_dbg(bdev, "BCM irq: disabled");
  607. }
  608. err = bcm_resume_device(dev);
  609. unlock:
  610. mutex_unlock(&bcm_device_lock);
  611. if (!err) {
  612. pm_runtime_disable(dev);
  613. pm_runtime_set_active(dev);
  614. pm_runtime_enable(dev);
  615. }
  616. return 0;
  617. }
  618. #endif
  619. static const struct acpi_gpio_params first_gpio = { 0, 0, false };
  620. static const struct acpi_gpio_params second_gpio = { 1, 0, false };
  621. static const struct acpi_gpio_params third_gpio = { 2, 0, false };
  622. static const struct acpi_gpio_mapping acpi_bcm_int_last_gpios[] = {
  623. { "device-wakeup-gpios", &first_gpio, 1 },
  624. { "shutdown-gpios", &second_gpio, 1 },
  625. { "host-wakeup-gpios", &third_gpio, 1 },
  626. { },
  627. };
  628. static const struct acpi_gpio_mapping acpi_bcm_int_first_gpios[] = {
  629. { "host-wakeup-gpios", &first_gpio, 1 },
  630. { "device-wakeup-gpios", &second_gpio, 1 },
  631. { "shutdown-gpios", &third_gpio, 1 },
  632. { },
  633. };
  634. /* Some firmware reports an IRQ which does not work (wrong pin in fw table?) */
  635. static const struct dmi_system_id bcm_broken_irq_dmi_table[] = {
  636. {
  637. .ident = "Meegopad T08",
  638. .matches = {
  639. DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
  640. "To be filled by OEM."),
  641. DMI_EXACT_MATCH(DMI_BOARD_NAME, "T3 MRD"),
  642. DMI_EXACT_MATCH(DMI_BOARD_VERSION, "V1.1"),
  643. },
  644. },
  645. { }
  646. };
  647. #ifdef CONFIG_ACPI
  648. static int bcm_resource(struct acpi_resource *ares, void *data)
  649. {
  650. struct bcm_device *dev = data;
  651. struct acpi_resource_extended_irq *irq;
  652. struct acpi_resource_gpio *gpio;
  653. struct acpi_resource_uart_serialbus *sb;
  654. switch (ares->type) {
  655. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  656. irq = &ares->data.extended_irq;
  657. if (irq->polarity != ACPI_ACTIVE_LOW)
  658. dev_info(dev->dev, "ACPI Interrupt resource is active-high, this is usually wrong, treating the IRQ as active-low\n");
  659. dev->irq_active_low = true;
  660. break;
  661. case ACPI_RESOURCE_TYPE_GPIO:
  662. gpio = &ares->data.gpio;
  663. if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
  664. dev->gpio_int_idx = dev->gpio_count;
  665. dev->irq_active_low = gpio->polarity == ACPI_ACTIVE_LOW;
  666. }
  667. dev->gpio_count++;
  668. break;
  669. case ACPI_RESOURCE_TYPE_SERIAL_BUS:
  670. sb = &ares->data.uart_serial_bus;
  671. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
  672. dev->init_speed = sb->default_baud_rate;
  673. dev->oper_speed = 4000000;
  674. }
  675. break;
  676. default:
  677. break;
  678. }
  679. return 0;
  680. }
  681. static int bcm_apple_set_device_wakeup(struct bcm_device *dev, bool awake)
  682. {
  683. if (ACPI_FAILURE(acpi_execute_simple_method(dev->btlp, NULL, !awake)))
  684. return -EIO;
  685. return 0;
  686. }
  687. static int bcm_apple_set_shutdown(struct bcm_device *dev, bool powered)
  688. {
  689. if (ACPI_FAILURE(acpi_evaluate_object(powered ? dev->btpu : dev->btpd,
  690. NULL, NULL, NULL)))
  691. return -EIO;
  692. return 0;
  693. }
  694. static int bcm_apple_get_resources(struct bcm_device *dev)
  695. {
  696. struct acpi_device *adev = ACPI_COMPANION(dev->dev);
  697. const union acpi_object *obj;
  698. if (!adev ||
  699. ACPI_FAILURE(acpi_get_handle(adev->handle, "BTLP", &dev->btlp)) ||
  700. ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPU", &dev->btpu)) ||
  701. ACPI_FAILURE(acpi_get_handle(adev->handle, "BTPD", &dev->btpd)))
  702. return -ENODEV;
  703. if (!acpi_dev_get_property(adev, "baud", ACPI_TYPE_BUFFER, &obj) &&
  704. obj->buffer.length == 8)
  705. dev->init_speed = *(u64 *)obj->buffer.pointer;
  706. dev->set_device_wakeup = bcm_apple_set_device_wakeup;
  707. dev->set_shutdown = bcm_apple_set_shutdown;
  708. return 0;
  709. }
  710. #else
  711. static inline int bcm_apple_get_resources(struct bcm_device *dev)
  712. {
  713. return -EOPNOTSUPP;
  714. }
  715. #endif /* CONFIG_ACPI */
  716. static int bcm_gpio_set_device_wakeup(struct bcm_device *dev, bool awake)
  717. {
  718. gpiod_set_value_cansleep(dev->device_wakeup, awake);
  719. return 0;
  720. }
  721. static int bcm_gpio_set_shutdown(struct bcm_device *dev, bool powered)
  722. {
  723. gpiod_set_value_cansleep(dev->shutdown, powered);
  724. return 0;
  725. }
  726. static int bcm_get_resources(struct bcm_device *dev)
  727. {
  728. const struct dmi_system_id *dmi_id;
  729. dev->name = dev_name(dev->dev);
  730. if (x86_apple_machine && !bcm_apple_get_resources(dev))
  731. return 0;
  732. dev->clk = devm_clk_get(dev->dev, NULL);
  733. /* Handle deferred probing */
  734. if (dev->clk == ERR_PTR(-EPROBE_DEFER))
  735. return PTR_ERR(dev->clk);
  736. dev->device_wakeup = devm_gpiod_get_optional(dev->dev, "device-wakeup",
  737. GPIOD_OUT_LOW);
  738. if (IS_ERR(dev->device_wakeup))
  739. return PTR_ERR(dev->device_wakeup);
  740. dev->shutdown = devm_gpiod_get_optional(dev->dev, "shutdown",
  741. GPIOD_OUT_LOW);
  742. if (IS_ERR(dev->shutdown))
  743. return PTR_ERR(dev->shutdown);
  744. dev->set_device_wakeup = bcm_gpio_set_device_wakeup;
  745. dev->set_shutdown = bcm_gpio_set_shutdown;
  746. /* IRQ can be declared in ACPI table as Interrupt or GpioInt */
  747. if (dev->irq <= 0) {
  748. struct gpio_desc *gpio;
  749. gpio = devm_gpiod_get_optional(dev->dev, "host-wakeup",
  750. GPIOD_IN);
  751. if (IS_ERR(gpio))
  752. return PTR_ERR(gpio);
  753. dev->irq = gpiod_to_irq(gpio);
  754. }
  755. dmi_id = dmi_first_match(bcm_broken_irq_dmi_table);
  756. if (dmi_id) {
  757. dev_info(dev->dev, "%s: Has a broken IRQ config, disabling IRQ support / runtime-pm\n",
  758. dmi_id->ident);
  759. dev->irq = 0;
  760. }
  761. dev_dbg(dev->dev, "BCM irq: %d\n", dev->irq);
  762. return 0;
  763. }
  764. #ifdef CONFIG_ACPI
  765. static int bcm_acpi_probe(struct bcm_device *dev)
  766. {
  767. LIST_HEAD(resources);
  768. const struct acpi_gpio_mapping *gpio_mapping = acpi_bcm_int_last_gpios;
  769. struct resource_entry *entry;
  770. int ret;
  771. /* Retrieve UART ACPI info */
  772. dev->gpio_int_idx = -1;
  773. ret = acpi_dev_get_resources(ACPI_COMPANION(dev->dev),
  774. &resources, bcm_resource, dev);
  775. if (ret < 0)
  776. return ret;
  777. resource_list_for_each_entry(entry, &resources) {
  778. if (resource_type(entry->res) == IORESOURCE_IRQ) {
  779. dev->irq = entry->res->start;
  780. break;
  781. }
  782. }
  783. acpi_dev_free_resource_list(&resources);
  784. /* If the DSDT uses an Interrupt resource for the IRQ, then there are
  785. * only 2 GPIO resources, we use the irq-last mapping for this, since
  786. * we already have an irq the 3th / last mapping will not be used.
  787. */
  788. if (dev->irq)
  789. gpio_mapping = acpi_bcm_int_last_gpios;
  790. else if (dev->gpio_int_idx == 0)
  791. gpio_mapping = acpi_bcm_int_first_gpios;
  792. else if (dev->gpio_int_idx == 2)
  793. gpio_mapping = acpi_bcm_int_last_gpios;
  794. else
  795. dev_warn(dev->dev, "Unexpected ACPI gpio_int_idx: %d\n",
  796. dev->gpio_int_idx);
  797. /* Warn if our expectations are not met. */
  798. if (dev->gpio_count != (dev->irq ? 2 : 3))
  799. dev_warn(dev->dev, "Unexpected number of ACPI GPIOs: %d\n",
  800. dev->gpio_count);
  801. ret = devm_acpi_dev_add_driver_gpios(dev->dev, gpio_mapping);
  802. if (ret)
  803. return ret;
  804. if (irq_polarity != -1) {
  805. dev->irq_active_low = irq_polarity;
  806. dev_warn(dev->dev, "Overwriting IRQ polarity to active %s by module-param\n",
  807. dev->irq_active_low ? "low" : "high");
  808. }
  809. return 0;
  810. }
  811. #else
  812. static int bcm_acpi_probe(struct bcm_device *dev)
  813. {
  814. return -EINVAL;
  815. }
  816. #endif /* CONFIG_ACPI */
  817. static int bcm_of_probe(struct bcm_device *bdev)
  818. {
  819. device_property_read_u32(bdev->dev, "max-speed", &bdev->oper_speed);
  820. return 0;
  821. }
  822. static int bcm_probe(struct platform_device *pdev)
  823. {
  824. struct bcm_device *dev;
  825. int ret;
  826. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  827. if (!dev)
  828. return -ENOMEM;
  829. dev->dev = &pdev->dev;
  830. dev->irq = platform_get_irq(pdev, 0);
  831. if (has_acpi_companion(&pdev->dev)) {
  832. ret = bcm_acpi_probe(dev);
  833. if (ret)
  834. return ret;
  835. }
  836. ret = bcm_get_resources(dev);
  837. if (ret)
  838. return ret;
  839. platform_set_drvdata(pdev, dev);
  840. dev_info(&pdev->dev, "%s device registered.\n", dev->name);
  841. /* Place this instance on the device list */
  842. mutex_lock(&bcm_device_lock);
  843. list_add_tail(&dev->list, &bcm_device_list);
  844. mutex_unlock(&bcm_device_lock);
  845. ret = bcm_gpio_set_power(dev, false);
  846. if (ret)
  847. dev_err(&pdev->dev, "Failed to power down\n");
  848. return 0;
  849. }
  850. static int bcm_remove(struct platform_device *pdev)
  851. {
  852. struct bcm_device *dev = platform_get_drvdata(pdev);
  853. mutex_lock(&bcm_device_lock);
  854. list_del(&dev->list);
  855. mutex_unlock(&bcm_device_lock);
  856. dev_info(&pdev->dev, "%s device unregistered.\n", dev->name);
  857. return 0;
  858. }
  859. static const struct hci_uart_proto bcm_proto = {
  860. .id = HCI_UART_BCM,
  861. .name = "Broadcom",
  862. .manufacturer = 15,
  863. .init_speed = 115200,
  864. .open = bcm_open,
  865. .close = bcm_close,
  866. .flush = bcm_flush,
  867. .setup = bcm_setup,
  868. .set_baudrate = bcm_set_baudrate,
  869. .recv = bcm_recv,
  870. .enqueue = bcm_enqueue,
  871. .dequeue = bcm_dequeue,
  872. };
  873. #ifdef CONFIG_ACPI
  874. static const struct acpi_device_id bcm_acpi_match[] = {
  875. { "BCM2E00" },
  876. { "BCM2E01" },
  877. { "BCM2E02" },
  878. { "BCM2E03" },
  879. { "BCM2E04" },
  880. { "BCM2E05" },
  881. { "BCM2E06" },
  882. { "BCM2E07" },
  883. { "BCM2E08" },
  884. { "BCM2E09" },
  885. { "BCM2E0A" },
  886. { "BCM2E0B" },
  887. { "BCM2E0C" },
  888. { "BCM2E0D" },
  889. { "BCM2E0E" },
  890. { "BCM2E0F" },
  891. { "BCM2E10" },
  892. { "BCM2E11" },
  893. { "BCM2E12" },
  894. { "BCM2E13" },
  895. { "BCM2E14" },
  896. { "BCM2E15" },
  897. { "BCM2E16" },
  898. { "BCM2E17" },
  899. { "BCM2E18" },
  900. { "BCM2E19" },
  901. { "BCM2E1A" },
  902. { "BCM2E1B" },
  903. { "BCM2E1C" },
  904. { "BCM2E1D" },
  905. { "BCM2E1F" },
  906. { "BCM2E20" },
  907. { "BCM2E21" },
  908. { "BCM2E22" },
  909. { "BCM2E23" },
  910. { "BCM2E24" },
  911. { "BCM2E25" },
  912. { "BCM2E26" },
  913. { "BCM2E27" },
  914. { "BCM2E28" },
  915. { "BCM2E29" },
  916. { "BCM2E2A" },
  917. { "BCM2E2B" },
  918. { "BCM2E2C" },
  919. { "BCM2E2D" },
  920. { "BCM2E2E" },
  921. { "BCM2E2F" },
  922. { "BCM2E30" },
  923. { "BCM2E31" },
  924. { "BCM2E32" },
  925. { "BCM2E33" },
  926. { "BCM2E34" },
  927. { "BCM2E35" },
  928. { "BCM2E36" },
  929. { "BCM2E37" },
  930. { "BCM2E38" },
  931. { "BCM2E39" },
  932. { "BCM2E3A" },
  933. { "BCM2E3B" },
  934. { "BCM2E3C" },
  935. { "BCM2E3D" },
  936. { "BCM2E3E" },
  937. { "BCM2E3F" },
  938. { "BCM2E40" },
  939. { "BCM2E41" },
  940. { "BCM2E42" },
  941. { "BCM2E43" },
  942. { "BCM2E44" },
  943. { "BCM2E45" },
  944. { "BCM2E46" },
  945. { "BCM2E47" },
  946. { "BCM2E48" },
  947. { "BCM2E49" },
  948. { "BCM2E4A" },
  949. { "BCM2E4B" },
  950. { "BCM2E4C" },
  951. { "BCM2E4D" },
  952. { "BCM2E4E" },
  953. { "BCM2E4F" },
  954. { "BCM2E50" },
  955. { "BCM2E51" },
  956. { "BCM2E52" },
  957. { "BCM2E53" },
  958. { "BCM2E54" },
  959. { "BCM2E55" },
  960. { "BCM2E56" },
  961. { "BCM2E57" },
  962. { "BCM2E58" },
  963. { "BCM2E59" },
  964. { "BCM2E5A" },
  965. { "BCM2E5B" },
  966. { "BCM2E5C" },
  967. { "BCM2E5D" },
  968. { "BCM2E5E" },
  969. { "BCM2E5F" },
  970. { "BCM2E60" },
  971. { "BCM2E61" },
  972. { "BCM2E62" },
  973. { "BCM2E63" },
  974. { "BCM2E64" },
  975. { "BCM2E65" },
  976. { "BCM2E66" },
  977. { "BCM2E67" },
  978. { "BCM2E68" },
  979. { "BCM2E69" },
  980. { "BCM2E6B" },
  981. { "BCM2E6D" },
  982. { "BCM2E6E" },
  983. { "BCM2E6F" },
  984. { "BCM2E70" },
  985. { "BCM2E71" },
  986. { "BCM2E72" },
  987. { "BCM2E73" },
  988. { "BCM2E74" },
  989. { "BCM2E75" },
  990. { "BCM2E76" },
  991. { "BCM2E77" },
  992. { "BCM2E78" },
  993. { "BCM2E79" },
  994. { "BCM2E7A" },
  995. { "BCM2E7B" },
  996. { "BCM2E7C" },
  997. { "BCM2E7D" },
  998. { "BCM2E7E" },
  999. { "BCM2E7F" },
  1000. { "BCM2E80" },
  1001. { "BCM2E81" },
  1002. { "BCM2E82" },
  1003. { "BCM2E83" },
  1004. { "BCM2E84" },
  1005. { "BCM2E85" },
  1006. { "BCM2E86" },
  1007. { "BCM2E87" },
  1008. { "BCM2E88" },
  1009. { "BCM2E89" },
  1010. { "BCM2E8A" },
  1011. { "BCM2E8B" },
  1012. { "BCM2E8C" },
  1013. { "BCM2E8D" },
  1014. { "BCM2E8E" },
  1015. { "BCM2E90" },
  1016. { "BCM2E92" },
  1017. { "BCM2E93" },
  1018. { "BCM2E94" },
  1019. { "BCM2E95" },
  1020. { "BCM2E96" },
  1021. { "BCM2E97" },
  1022. { "BCM2E98" },
  1023. { "BCM2E99" },
  1024. { "BCM2E9A" },
  1025. { "BCM2E9B" },
  1026. { "BCM2E9C" },
  1027. { "BCM2E9D" },
  1028. { "BCM2EA0" },
  1029. { "BCM2EA1" },
  1030. { "BCM2EA2" },
  1031. { "BCM2EA3" },
  1032. { "BCM2EA4" },
  1033. { "BCM2EA5" },
  1034. { "BCM2EA6" },
  1035. { "BCM2EA7" },
  1036. { "BCM2EA8" },
  1037. { "BCM2EA9" },
  1038. { "BCM2EAA" },
  1039. { "BCM2EAB" },
  1040. { "BCM2EAC" },
  1041. { },
  1042. };
  1043. MODULE_DEVICE_TABLE(acpi, bcm_acpi_match);
  1044. #endif
  1045. /* suspend and resume callbacks */
  1046. static const struct dev_pm_ops bcm_pm_ops = {
  1047. SET_SYSTEM_SLEEP_PM_OPS(bcm_suspend, bcm_resume)
  1048. SET_RUNTIME_PM_OPS(bcm_suspend_device, bcm_resume_device, NULL)
  1049. };
  1050. static struct platform_driver bcm_driver = {
  1051. .probe = bcm_probe,
  1052. .remove = bcm_remove,
  1053. .driver = {
  1054. .name = "hci_bcm",
  1055. .acpi_match_table = ACPI_PTR(bcm_acpi_match),
  1056. .pm = &bcm_pm_ops,
  1057. },
  1058. };
  1059. static int bcm_serdev_probe(struct serdev_device *serdev)
  1060. {
  1061. struct bcm_device *bcmdev;
  1062. int err;
  1063. bcmdev = devm_kzalloc(&serdev->dev, sizeof(*bcmdev), GFP_KERNEL);
  1064. if (!bcmdev)
  1065. return -ENOMEM;
  1066. bcmdev->dev = &serdev->dev;
  1067. #ifdef CONFIG_PM
  1068. bcmdev->hu = &bcmdev->serdev_hu;
  1069. #endif
  1070. bcmdev->serdev_hu.serdev = serdev;
  1071. serdev_device_set_drvdata(serdev, bcmdev);
  1072. if (has_acpi_companion(&serdev->dev))
  1073. err = bcm_acpi_probe(bcmdev);
  1074. else
  1075. err = bcm_of_probe(bcmdev);
  1076. if (err)
  1077. return err;
  1078. err = bcm_get_resources(bcmdev);
  1079. if (err)
  1080. return err;
  1081. if (!bcmdev->shutdown) {
  1082. dev_warn(&serdev->dev,
  1083. "No reset resource, using default baud rate\n");
  1084. bcmdev->oper_speed = bcmdev->init_speed;
  1085. }
  1086. err = bcm_gpio_set_power(bcmdev, false);
  1087. if (err)
  1088. dev_err(&serdev->dev, "Failed to power down\n");
  1089. return hci_uart_register_device(&bcmdev->serdev_hu, &bcm_proto);
  1090. }
  1091. static void bcm_serdev_remove(struct serdev_device *serdev)
  1092. {
  1093. struct bcm_device *bcmdev = serdev_device_get_drvdata(serdev);
  1094. hci_uart_unregister_device(&bcmdev->serdev_hu);
  1095. }
  1096. #ifdef CONFIG_OF
  1097. static const struct of_device_id bcm_bluetooth_of_match[] = {
  1098. { .compatible = "brcm,bcm43438-bt" },
  1099. { },
  1100. };
  1101. MODULE_DEVICE_TABLE(of, bcm_bluetooth_of_match);
  1102. #endif
  1103. static struct serdev_device_driver bcm_serdev_driver = {
  1104. .probe = bcm_serdev_probe,
  1105. .remove = bcm_serdev_remove,
  1106. .driver = {
  1107. .name = "hci_uart_bcm",
  1108. .of_match_table = of_match_ptr(bcm_bluetooth_of_match),
  1109. .acpi_match_table = ACPI_PTR(bcm_acpi_match),
  1110. .pm = &bcm_pm_ops,
  1111. },
  1112. };
  1113. int __init bcm_init(void)
  1114. {
  1115. /* For now, we need to keep both platform device
  1116. * driver (ACPI generated) and serdev driver (DT).
  1117. */
  1118. platform_driver_register(&bcm_driver);
  1119. serdev_device_driver_register(&bcm_serdev_driver);
  1120. return hci_uart_register_proto(&bcm_proto);
  1121. }
  1122. int __exit bcm_deinit(void)
  1123. {
  1124. platform_driver_unregister(&bcm_driver);
  1125. serdev_device_driver_unregister(&bcm_serdev_driver);
  1126. return hci_uart_unregister_proto(&bcm_proto);
  1127. }