hci_bcm.c 30 KB

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