hci_bcm.c 39 KB

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