hci_bcm.c 31 KB

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