hci_bcm.c 35 KB

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