hci_bcm.c 38 KB

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