hci_bcm.c 31 KB

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