hci_bcm.c 40 KB

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