hci_qca.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /*
  2. * Bluetooth Software UART Qualcomm protocol
  3. *
  4. * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management
  5. * protocol extension to H4.
  6. *
  7. * Copyright (C) 2007 Texas Instruments, Inc.
  8. * Copyright (c) 2010, 2012, 2018 The Linux Foundation. All rights reserved.
  9. *
  10. * Acknowledgements:
  11. * This file is based on hci_ll.c, which was...
  12. * Written by Ohad Ben-Cohen <ohad@bencohen.org>
  13. * which was in turn based on hci_h4.c, which was written
  14. * by Maxim Krasnyansky and Marcel Holtmann.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2
  18. * as published by the Free Software Foundation
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/clk.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/delay.h>
  34. #include <linux/device.h>
  35. #include <linux/gpio/consumer.h>
  36. #include <linux/mod_devicetable.h>
  37. #include <linux/module.h>
  38. #include <linux/of_device.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/regulator/consumer.h>
  41. #include <linux/serdev.h>
  42. #include <net/bluetooth/bluetooth.h>
  43. #include <net/bluetooth/hci_core.h>
  44. #include "hci_uart.h"
  45. #include "btqca.h"
  46. /* HCI_IBS protocol messages */
  47. #define HCI_IBS_SLEEP_IND 0xFE
  48. #define HCI_IBS_WAKE_IND 0xFD
  49. #define HCI_IBS_WAKE_ACK 0xFC
  50. #define HCI_MAX_IBS_SIZE 10
  51. /* Controller states */
  52. #define STATE_IN_BAND_SLEEP_ENABLED 1
  53. #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
  54. #define IBS_TX_IDLE_TIMEOUT_MS 2000
  55. #define BAUDRATE_SETTLE_TIMEOUT_MS 300
  56. /* susclk rate */
  57. #define SUSCLK_RATE_32KHZ 32768
  58. /* HCI_IBS transmit side sleep protocol states */
  59. enum tx_ibs_states {
  60. HCI_IBS_TX_ASLEEP,
  61. HCI_IBS_TX_WAKING,
  62. HCI_IBS_TX_AWAKE,
  63. };
  64. /* HCI_IBS receive side sleep protocol states */
  65. enum rx_states {
  66. HCI_IBS_RX_ASLEEP,
  67. HCI_IBS_RX_AWAKE,
  68. };
  69. /* HCI_IBS transmit and receive side clock state vote */
  70. enum hci_ibs_clock_state_vote {
  71. HCI_IBS_VOTE_STATS_UPDATE,
  72. HCI_IBS_TX_VOTE_CLOCK_ON,
  73. HCI_IBS_TX_VOTE_CLOCK_OFF,
  74. HCI_IBS_RX_VOTE_CLOCK_ON,
  75. HCI_IBS_RX_VOTE_CLOCK_OFF,
  76. };
  77. struct qca_data {
  78. struct hci_uart *hu;
  79. struct sk_buff *rx_skb;
  80. struct sk_buff_head txq;
  81. struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
  82. spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
  83. u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
  84. u8 rx_ibs_state; /* HCI_IBS receive side power state */
  85. bool tx_vote; /* Clock must be on for TX */
  86. bool rx_vote; /* Clock must be on for RX */
  87. struct timer_list tx_idle_timer;
  88. u32 tx_idle_delay;
  89. struct timer_list wake_retrans_timer;
  90. u32 wake_retrans;
  91. struct workqueue_struct *workqueue;
  92. struct work_struct ws_awake_rx;
  93. struct work_struct ws_awake_device;
  94. struct work_struct ws_rx_vote_off;
  95. struct work_struct ws_tx_vote_off;
  96. unsigned long flags;
  97. /* For debugging purpose */
  98. u64 ibs_sent_wacks;
  99. u64 ibs_sent_slps;
  100. u64 ibs_sent_wakes;
  101. u64 ibs_recv_wacks;
  102. u64 ibs_recv_slps;
  103. u64 ibs_recv_wakes;
  104. u64 vote_last_jif;
  105. u32 vote_on_ms;
  106. u32 vote_off_ms;
  107. u64 tx_votes_on;
  108. u64 rx_votes_on;
  109. u64 tx_votes_off;
  110. u64 rx_votes_off;
  111. u64 votes_on;
  112. u64 votes_off;
  113. };
  114. enum qca_speed_type {
  115. QCA_INIT_SPEED = 1,
  116. QCA_OPER_SPEED
  117. };
  118. /*
  119. * Voltage regulator information required for configuring the
  120. * QCA Bluetooth chipset
  121. */
  122. struct qca_vreg {
  123. const char *name;
  124. unsigned int min_uV;
  125. unsigned int max_uV;
  126. unsigned int load_uA;
  127. };
  128. struct qca_vreg_data {
  129. enum qca_btsoc_type soc_type;
  130. struct qca_vreg *vregs;
  131. size_t num_vregs;
  132. };
  133. /*
  134. * Platform data for the QCA Bluetooth power driver.
  135. */
  136. struct qca_power {
  137. struct device *dev;
  138. const struct qca_vreg_data *vreg_data;
  139. struct regulator_bulk_data *vreg_bulk;
  140. bool vregs_on;
  141. };
  142. struct qca_serdev {
  143. struct hci_uart serdev_hu;
  144. struct gpio_desc *bt_en;
  145. struct clk *susclk;
  146. enum qca_btsoc_type btsoc_type;
  147. struct qca_power *bt_power;
  148. u32 init_speed;
  149. u32 oper_speed;
  150. };
  151. static int qca_power_setup(struct hci_uart *hu, bool on);
  152. static void qca_power_shutdown(struct hci_uart *hu);
  153. static void __serial_clock_on(struct tty_struct *tty)
  154. {
  155. /* TODO: Some chipset requires to enable UART clock on client
  156. * side to save power consumption or manual work is required.
  157. * Please put your code to control UART clock here if needed
  158. */
  159. }
  160. static void __serial_clock_off(struct tty_struct *tty)
  161. {
  162. /* TODO: Some chipset requires to disable UART clock on client
  163. * side to save power consumption or manual work is required.
  164. * Please put your code to control UART clock off here if needed
  165. */
  166. }
  167. /* serial_clock_vote needs to be called with the ibs lock held */
  168. static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
  169. {
  170. struct qca_data *qca = hu->priv;
  171. unsigned int diff;
  172. bool old_vote = (qca->tx_vote | qca->rx_vote);
  173. bool new_vote;
  174. switch (vote) {
  175. case HCI_IBS_VOTE_STATS_UPDATE:
  176. diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
  177. if (old_vote)
  178. qca->vote_off_ms += diff;
  179. else
  180. qca->vote_on_ms += diff;
  181. return;
  182. case HCI_IBS_TX_VOTE_CLOCK_ON:
  183. qca->tx_vote = true;
  184. qca->tx_votes_on++;
  185. new_vote = true;
  186. break;
  187. case HCI_IBS_RX_VOTE_CLOCK_ON:
  188. qca->rx_vote = true;
  189. qca->rx_votes_on++;
  190. new_vote = true;
  191. break;
  192. case HCI_IBS_TX_VOTE_CLOCK_OFF:
  193. qca->tx_vote = false;
  194. qca->tx_votes_off++;
  195. new_vote = qca->rx_vote | qca->tx_vote;
  196. break;
  197. case HCI_IBS_RX_VOTE_CLOCK_OFF:
  198. qca->rx_vote = false;
  199. qca->rx_votes_off++;
  200. new_vote = qca->rx_vote | qca->tx_vote;
  201. break;
  202. default:
  203. BT_ERR("Voting irregularity");
  204. return;
  205. }
  206. if (new_vote != old_vote) {
  207. if (new_vote)
  208. __serial_clock_on(hu->tty);
  209. else
  210. __serial_clock_off(hu->tty);
  211. BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
  212. vote ? "true" : "false");
  213. diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
  214. if (new_vote) {
  215. qca->votes_on++;
  216. qca->vote_off_ms += diff;
  217. } else {
  218. qca->votes_off++;
  219. qca->vote_on_ms += diff;
  220. }
  221. qca->vote_last_jif = jiffies;
  222. }
  223. }
  224. /* Builds and sends an HCI_IBS command packet.
  225. * These are very simple packets with only 1 cmd byte.
  226. */
  227. static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
  228. {
  229. int err = 0;
  230. struct sk_buff *skb = NULL;
  231. struct qca_data *qca = hu->priv;
  232. BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
  233. skb = bt_skb_alloc(1, GFP_ATOMIC);
  234. if (!skb) {
  235. BT_ERR("Failed to allocate memory for HCI_IBS packet");
  236. return -ENOMEM;
  237. }
  238. /* Assign HCI_IBS type */
  239. skb_put_u8(skb, cmd);
  240. skb_queue_tail(&qca->txq, skb);
  241. return err;
  242. }
  243. static void qca_wq_awake_device(struct work_struct *work)
  244. {
  245. struct qca_data *qca = container_of(work, struct qca_data,
  246. ws_awake_device);
  247. struct hci_uart *hu = qca->hu;
  248. unsigned long retrans_delay;
  249. BT_DBG("hu %p wq awake device", hu);
  250. /* Vote for serial clock */
  251. serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
  252. spin_lock(&qca->hci_ibs_lock);
  253. /* Send wake indication to device */
  254. if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
  255. BT_ERR("Failed to send WAKE to device");
  256. qca->ibs_sent_wakes++;
  257. /* Start retransmit timer */
  258. retrans_delay = msecs_to_jiffies(qca->wake_retrans);
  259. mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
  260. spin_unlock(&qca->hci_ibs_lock);
  261. /* Actually send the packets */
  262. hci_uart_tx_wakeup(hu);
  263. }
  264. static void qca_wq_awake_rx(struct work_struct *work)
  265. {
  266. struct qca_data *qca = container_of(work, struct qca_data,
  267. ws_awake_rx);
  268. struct hci_uart *hu = qca->hu;
  269. BT_DBG("hu %p wq awake rx", hu);
  270. serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
  271. spin_lock(&qca->hci_ibs_lock);
  272. qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
  273. /* Always acknowledge device wake up,
  274. * sending IBS message doesn't count as TX ON.
  275. */
  276. if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
  277. BT_ERR("Failed to acknowledge device wake up");
  278. qca->ibs_sent_wacks++;
  279. spin_unlock(&qca->hci_ibs_lock);
  280. /* Actually send the packets */
  281. hci_uart_tx_wakeup(hu);
  282. }
  283. static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
  284. {
  285. struct qca_data *qca = container_of(work, struct qca_data,
  286. ws_rx_vote_off);
  287. struct hci_uart *hu = qca->hu;
  288. BT_DBG("hu %p rx clock vote off", hu);
  289. serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
  290. }
  291. static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
  292. {
  293. struct qca_data *qca = container_of(work, struct qca_data,
  294. ws_tx_vote_off);
  295. struct hci_uart *hu = qca->hu;
  296. BT_DBG("hu %p tx clock vote off", hu);
  297. /* Run HCI tx handling unlocked */
  298. hci_uart_tx_wakeup(hu);
  299. /* Now that message queued to tty driver, vote for tty clocks off.
  300. * It is up to the tty driver to pend the clocks off until tx done.
  301. */
  302. serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
  303. }
  304. static void hci_ibs_tx_idle_timeout(struct timer_list *t)
  305. {
  306. struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
  307. struct hci_uart *hu = qca->hu;
  308. unsigned long flags;
  309. BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
  310. spin_lock_irqsave_nested(&qca->hci_ibs_lock,
  311. flags, SINGLE_DEPTH_NESTING);
  312. switch (qca->tx_ibs_state) {
  313. case HCI_IBS_TX_AWAKE:
  314. /* TX_IDLE, go to SLEEP */
  315. if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
  316. BT_ERR("Failed to send SLEEP to device");
  317. break;
  318. }
  319. qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
  320. qca->ibs_sent_slps++;
  321. queue_work(qca->workqueue, &qca->ws_tx_vote_off);
  322. break;
  323. case HCI_IBS_TX_ASLEEP:
  324. case HCI_IBS_TX_WAKING:
  325. /* Fall through */
  326. default:
  327. BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
  328. break;
  329. }
  330. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  331. }
  332. static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
  333. {
  334. struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
  335. struct hci_uart *hu = qca->hu;
  336. unsigned long flags, retrans_delay;
  337. bool retransmit = false;
  338. BT_DBG("hu %p wake retransmit timeout in %d state",
  339. hu, qca->tx_ibs_state);
  340. spin_lock_irqsave_nested(&qca->hci_ibs_lock,
  341. flags, SINGLE_DEPTH_NESTING);
  342. switch (qca->tx_ibs_state) {
  343. case HCI_IBS_TX_WAKING:
  344. /* No WAKE_ACK, retransmit WAKE */
  345. retransmit = true;
  346. if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
  347. BT_ERR("Failed to acknowledge device wake up");
  348. break;
  349. }
  350. qca->ibs_sent_wakes++;
  351. retrans_delay = msecs_to_jiffies(qca->wake_retrans);
  352. mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
  353. break;
  354. case HCI_IBS_TX_ASLEEP:
  355. case HCI_IBS_TX_AWAKE:
  356. /* Fall through */
  357. default:
  358. BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
  359. break;
  360. }
  361. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  362. if (retransmit)
  363. hci_uart_tx_wakeup(hu);
  364. }
  365. /* Initialize protocol */
  366. static int qca_open(struct hci_uart *hu)
  367. {
  368. struct qca_serdev *qcadev;
  369. struct qca_data *qca;
  370. int ret;
  371. BT_DBG("hu %p qca_open", hu);
  372. if (!hci_uart_has_flow_control(hu))
  373. return -EOPNOTSUPP;
  374. qca = kzalloc(sizeof(struct qca_data), GFP_KERNEL);
  375. if (!qca)
  376. return -ENOMEM;
  377. skb_queue_head_init(&qca->txq);
  378. skb_queue_head_init(&qca->tx_wait_q);
  379. spin_lock_init(&qca->hci_ibs_lock);
  380. qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
  381. if (!qca->workqueue) {
  382. BT_ERR("QCA Workqueue not initialized properly");
  383. kfree(qca);
  384. return -ENOMEM;
  385. }
  386. INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
  387. INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
  388. INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
  389. INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
  390. qca->hu = hu;
  391. /* Assume we start with both sides asleep -- extra wakes OK */
  392. qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
  393. qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
  394. /* clocks actually on, but we start votes off */
  395. qca->tx_vote = false;
  396. qca->rx_vote = false;
  397. qca->flags = 0;
  398. qca->ibs_sent_wacks = 0;
  399. qca->ibs_sent_slps = 0;
  400. qca->ibs_sent_wakes = 0;
  401. qca->ibs_recv_wacks = 0;
  402. qca->ibs_recv_slps = 0;
  403. qca->ibs_recv_wakes = 0;
  404. qca->vote_last_jif = jiffies;
  405. qca->vote_on_ms = 0;
  406. qca->vote_off_ms = 0;
  407. qca->votes_on = 0;
  408. qca->votes_off = 0;
  409. qca->tx_votes_on = 0;
  410. qca->tx_votes_off = 0;
  411. qca->rx_votes_on = 0;
  412. qca->rx_votes_off = 0;
  413. hu->priv = qca;
  414. if (hu->serdev) {
  415. serdev_device_open(hu->serdev);
  416. qcadev = serdev_device_get_drvdata(hu->serdev);
  417. if (qcadev->btsoc_type != QCA_WCN3990) {
  418. gpiod_set_value_cansleep(qcadev->bt_en, 1);
  419. /* Controller needs time to bootup. */
  420. msleep(150);
  421. } else {
  422. hu->init_speed = qcadev->init_speed;
  423. hu->oper_speed = qcadev->oper_speed;
  424. ret = qca_power_setup(hu, true);
  425. if (ret) {
  426. destroy_workqueue(qca->workqueue);
  427. kfree_skb(qca->rx_skb);
  428. hu->priv = NULL;
  429. kfree(qca);
  430. return ret;
  431. }
  432. }
  433. }
  434. timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
  435. qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
  436. timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
  437. qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
  438. BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
  439. qca->tx_idle_delay, qca->wake_retrans);
  440. return 0;
  441. }
  442. static void qca_debugfs_init(struct hci_dev *hdev)
  443. {
  444. struct hci_uart *hu = hci_get_drvdata(hdev);
  445. struct qca_data *qca = hu->priv;
  446. struct dentry *ibs_dir;
  447. umode_t mode;
  448. if (!hdev->debugfs)
  449. return;
  450. ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
  451. /* read only */
  452. mode = S_IRUGO;
  453. debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
  454. debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
  455. debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
  456. &qca->ibs_sent_slps);
  457. debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
  458. &qca->ibs_sent_wakes);
  459. debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
  460. &qca->ibs_sent_wacks);
  461. debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
  462. &qca->ibs_recv_slps);
  463. debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
  464. &qca->ibs_recv_wakes);
  465. debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
  466. &qca->ibs_recv_wacks);
  467. debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
  468. debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
  469. debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
  470. debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
  471. debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
  472. debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
  473. debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
  474. debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
  475. debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
  476. debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
  477. /* read/write */
  478. mode = S_IRUGO | S_IWUSR;
  479. debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
  480. debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
  481. &qca->tx_idle_delay);
  482. }
  483. /* Flush protocol data */
  484. static int qca_flush(struct hci_uart *hu)
  485. {
  486. struct qca_data *qca = hu->priv;
  487. BT_DBG("hu %p qca flush", hu);
  488. skb_queue_purge(&qca->tx_wait_q);
  489. skb_queue_purge(&qca->txq);
  490. return 0;
  491. }
  492. /* Close protocol */
  493. static int qca_close(struct hci_uart *hu)
  494. {
  495. struct qca_serdev *qcadev;
  496. struct qca_data *qca = hu->priv;
  497. BT_DBG("hu %p qca close", hu);
  498. serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
  499. skb_queue_purge(&qca->tx_wait_q);
  500. skb_queue_purge(&qca->txq);
  501. del_timer(&qca->tx_idle_timer);
  502. del_timer(&qca->wake_retrans_timer);
  503. destroy_workqueue(qca->workqueue);
  504. qca->hu = NULL;
  505. if (hu->serdev) {
  506. qcadev = serdev_device_get_drvdata(hu->serdev);
  507. if (qcadev->btsoc_type == QCA_WCN3990)
  508. qca_power_shutdown(hu);
  509. else
  510. gpiod_set_value_cansleep(qcadev->bt_en, 0);
  511. serdev_device_close(hu->serdev);
  512. }
  513. kfree_skb(qca->rx_skb);
  514. hu->priv = NULL;
  515. kfree(qca);
  516. return 0;
  517. }
  518. /* Called upon a wake-up-indication from the device.
  519. */
  520. static void device_want_to_wakeup(struct hci_uart *hu)
  521. {
  522. unsigned long flags;
  523. struct qca_data *qca = hu->priv;
  524. BT_DBG("hu %p want to wake up", hu);
  525. spin_lock_irqsave(&qca->hci_ibs_lock, flags);
  526. qca->ibs_recv_wakes++;
  527. switch (qca->rx_ibs_state) {
  528. case HCI_IBS_RX_ASLEEP:
  529. /* Make sure clock is on - we may have turned clock off since
  530. * receiving the wake up indicator awake rx clock.
  531. */
  532. queue_work(qca->workqueue, &qca->ws_awake_rx);
  533. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  534. return;
  535. case HCI_IBS_RX_AWAKE:
  536. /* Always acknowledge device wake up,
  537. * sending IBS message doesn't count as TX ON.
  538. */
  539. if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
  540. BT_ERR("Failed to acknowledge device wake up");
  541. break;
  542. }
  543. qca->ibs_sent_wacks++;
  544. break;
  545. default:
  546. /* Any other state is illegal */
  547. BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
  548. qca->rx_ibs_state);
  549. break;
  550. }
  551. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  552. /* Actually send the packets */
  553. hci_uart_tx_wakeup(hu);
  554. }
  555. /* Called upon a sleep-indication from the device.
  556. */
  557. static void device_want_to_sleep(struct hci_uart *hu)
  558. {
  559. unsigned long flags;
  560. struct qca_data *qca = hu->priv;
  561. BT_DBG("hu %p want to sleep", hu);
  562. spin_lock_irqsave(&qca->hci_ibs_lock, flags);
  563. qca->ibs_recv_slps++;
  564. switch (qca->rx_ibs_state) {
  565. case HCI_IBS_RX_AWAKE:
  566. /* Update state */
  567. qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
  568. /* Vote off rx clock under workqueue */
  569. queue_work(qca->workqueue, &qca->ws_rx_vote_off);
  570. break;
  571. case HCI_IBS_RX_ASLEEP:
  572. /* Fall through */
  573. default:
  574. /* Any other state is illegal */
  575. BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
  576. qca->rx_ibs_state);
  577. break;
  578. }
  579. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  580. }
  581. /* Called upon wake-up-acknowledgement from the device
  582. */
  583. static void device_woke_up(struct hci_uart *hu)
  584. {
  585. unsigned long flags, idle_delay;
  586. struct qca_data *qca = hu->priv;
  587. struct sk_buff *skb = NULL;
  588. BT_DBG("hu %p woke up", hu);
  589. spin_lock_irqsave(&qca->hci_ibs_lock, flags);
  590. qca->ibs_recv_wacks++;
  591. switch (qca->tx_ibs_state) {
  592. case HCI_IBS_TX_AWAKE:
  593. /* Expect one if we send 2 WAKEs */
  594. BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
  595. qca->tx_ibs_state);
  596. break;
  597. case HCI_IBS_TX_WAKING:
  598. /* Send pending packets */
  599. while ((skb = skb_dequeue(&qca->tx_wait_q)))
  600. skb_queue_tail(&qca->txq, skb);
  601. /* Switch timers and change state to HCI_IBS_TX_AWAKE */
  602. del_timer(&qca->wake_retrans_timer);
  603. idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
  604. mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
  605. qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
  606. break;
  607. case HCI_IBS_TX_ASLEEP:
  608. /* Fall through */
  609. default:
  610. BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
  611. qca->tx_ibs_state);
  612. break;
  613. }
  614. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  615. /* Actually send the packets */
  616. hci_uart_tx_wakeup(hu);
  617. }
  618. /* Enqueue frame for transmittion (padding, crc, etc) may be called from
  619. * two simultaneous tasklets.
  620. */
  621. static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  622. {
  623. unsigned long flags = 0, idle_delay;
  624. struct qca_data *qca = hu->priv;
  625. BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
  626. qca->tx_ibs_state);
  627. /* Prepend skb with frame type */
  628. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  629. /* Don't go to sleep in middle of patch download or
  630. * Out-Of-Band(GPIOs control) sleep is selected.
  631. */
  632. if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags)) {
  633. skb_queue_tail(&qca->txq, skb);
  634. return 0;
  635. }
  636. spin_lock_irqsave(&qca->hci_ibs_lock, flags);
  637. /* Act according to current state */
  638. switch (qca->tx_ibs_state) {
  639. case HCI_IBS_TX_AWAKE:
  640. BT_DBG("Device awake, sending normally");
  641. skb_queue_tail(&qca->txq, skb);
  642. idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
  643. mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
  644. break;
  645. case HCI_IBS_TX_ASLEEP:
  646. BT_DBG("Device asleep, waking up and queueing packet");
  647. /* Save packet for later */
  648. skb_queue_tail(&qca->tx_wait_q, skb);
  649. qca->tx_ibs_state = HCI_IBS_TX_WAKING;
  650. /* Schedule a work queue to wake up device */
  651. queue_work(qca->workqueue, &qca->ws_awake_device);
  652. break;
  653. case HCI_IBS_TX_WAKING:
  654. BT_DBG("Device waking up, queueing packet");
  655. /* Transient state; just keep packet for later */
  656. skb_queue_tail(&qca->tx_wait_q, skb);
  657. break;
  658. default:
  659. BT_ERR("Illegal tx state: %d (losing packet)",
  660. qca->tx_ibs_state);
  661. kfree_skb(skb);
  662. break;
  663. }
  664. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  665. return 0;
  666. }
  667. static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
  668. {
  669. struct hci_uart *hu = hci_get_drvdata(hdev);
  670. BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
  671. device_want_to_sleep(hu);
  672. kfree_skb(skb);
  673. return 0;
  674. }
  675. static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
  676. {
  677. struct hci_uart *hu = hci_get_drvdata(hdev);
  678. BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
  679. device_want_to_wakeup(hu);
  680. kfree_skb(skb);
  681. return 0;
  682. }
  683. static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
  684. {
  685. struct hci_uart *hu = hci_get_drvdata(hdev);
  686. BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
  687. device_woke_up(hu);
  688. kfree_skb(skb);
  689. return 0;
  690. }
  691. #define QCA_IBS_SLEEP_IND_EVENT \
  692. .type = HCI_IBS_SLEEP_IND, \
  693. .hlen = 0, \
  694. .loff = 0, \
  695. .lsize = 0, \
  696. .maxlen = HCI_MAX_IBS_SIZE
  697. #define QCA_IBS_WAKE_IND_EVENT \
  698. .type = HCI_IBS_WAKE_IND, \
  699. .hlen = 0, \
  700. .loff = 0, \
  701. .lsize = 0, \
  702. .maxlen = HCI_MAX_IBS_SIZE
  703. #define QCA_IBS_WAKE_ACK_EVENT \
  704. .type = HCI_IBS_WAKE_ACK, \
  705. .hlen = 0, \
  706. .loff = 0, \
  707. .lsize = 0, \
  708. .maxlen = HCI_MAX_IBS_SIZE
  709. static const struct h4_recv_pkt qca_recv_pkts[] = {
  710. { H4_RECV_ACL, .recv = hci_recv_frame },
  711. { H4_RECV_SCO, .recv = hci_recv_frame },
  712. { H4_RECV_EVENT, .recv = hci_recv_frame },
  713. { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
  714. { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
  715. { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
  716. };
  717. static int qca_recv(struct hci_uart *hu, const void *data, int count)
  718. {
  719. struct qca_data *qca = hu->priv;
  720. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  721. return -EUNATCH;
  722. qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
  723. qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
  724. if (IS_ERR(qca->rx_skb)) {
  725. int err = PTR_ERR(qca->rx_skb);
  726. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  727. qca->rx_skb = NULL;
  728. return err;
  729. }
  730. return count;
  731. }
  732. static struct sk_buff *qca_dequeue(struct hci_uart *hu)
  733. {
  734. struct qca_data *qca = hu->priv;
  735. return skb_dequeue(&qca->txq);
  736. }
  737. static uint8_t qca_get_baudrate_value(int speed)
  738. {
  739. switch (speed) {
  740. case 9600:
  741. return QCA_BAUDRATE_9600;
  742. case 19200:
  743. return QCA_BAUDRATE_19200;
  744. case 38400:
  745. return QCA_BAUDRATE_38400;
  746. case 57600:
  747. return QCA_BAUDRATE_57600;
  748. case 115200:
  749. return QCA_BAUDRATE_115200;
  750. case 230400:
  751. return QCA_BAUDRATE_230400;
  752. case 460800:
  753. return QCA_BAUDRATE_460800;
  754. case 500000:
  755. return QCA_BAUDRATE_500000;
  756. case 921600:
  757. return QCA_BAUDRATE_921600;
  758. case 1000000:
  759. return QCA_BAUDRATE_1000000;
  760. case 2000000:
  761. return QCA_BAUDRATE_2000000;
  762. case 3000000:
  763. return QCA_BAUDRATE_3000000;
  764. case 3200000:
  765. return QCA_BAUDRATE_3200000;
  766. case 3500000:
  767. return QCA_BAUDRATE_3500000;
  768. default:
  769. return QCA_BAUDRATE_115200;
  770. }
  771. }
  772. static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
  773. {
  774. struct hci_uart *hu = hci_get_drvdata(hdev);
  775. struct qca_data *qca = hu->priv;
  776. struct sk_buff *skb;
  777. struct qca_serdev *qcadev;
  778. u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
  779. if (baudrate > QCA_BAUDRATE_3200000)
  780. return -EINVAL;
  781. cmd[4] = baudrate;
  782. skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
  783. if (!skb) {
  784. bt_dev_err(hdev, "Failed to allocate baudrate packet");
  785. return -ENOMEM;
  786. }
  787. /* Disabling hardware flow control is mandatory while
  788. * sending change baudrate request to wcn3990 SoC.
  789. */
  790. qcadev = serdev_device_get_drvdata(hu->serdev);
  791. if (qcadev->btsoc_type == QCA_WCN3990)
  792. hci_uart_set_flow_control(hu, true);
  793. /* Assign commands to change baudrate and packet type. */
  794. skb_put_data(skb, cmd, sizeof(cmd));
  795. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  796. skb_queue_tail(&qca->txq, skb);
  797. hci_uart_tx_wakeup(hu);
  798. /* wait 300ms to change new baudrate on controller side
  799. * controller will come back after they receive this HCI command
  800. * then host can communicate with new baudrate to controller
  801. */
  802. set_current_state(TASK_UNINTERRUPTIBLE);
  803. schedule_timeout(msecs_to_jiffies(BAUDRATE_SETTLE_TIMEOUT_MS));
  804. set_current_state(TASK_RUNNING);
  805. if (qcadev->btsoc_type == QCA_WCN3990)
  806. hci_uart_set_flow_control(hu, false);
  807. return 0;
  808. }
  809. static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
  810. {
  811. if (hu->serdev)
  812. serdev_device_set_baudrate(hu->serdev, speed);
  813. else
  814. hci_uart_set_baudrate(hu, speed);
  815. }
  816. static int qca_send_power_pulse(struct hci_dev *hdev, u8 cmd)
  817. {
  818. struct hci_uart *hu = hci_get_drvdata(hdev);
  819. struct qca_data *qca = hu->priv;
  820. struct sk_buff *skb;
  821. /* These power pulses are single byte command which are sent
  822. * at required baudrate to wcn3990. On wcn3990, we have an external
  823. * circuit at Tx pin which decodes the pulse sent at specific baudrate.
  824. * For example, wcn3990 supports RF COEX antenna for both Wi-Fi/BT
  825. * and also we use the same power inputs to turn on and off for
  826. * Wi-Fi/BT. Powering up the power sources will not enable BT, until
  827. * we send a power on pulse at 115200 bps. This algorithm will help to
  828. * save power. Disabling hardware flow control is mandatory while
  829. * sending power pulses to SoC.
  830. */
  831. bt_dev_dbg(hdev, "sending power pulse %02x to SoC", cmd);
  832. skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
  833. if (!skb)
  834. return -ENOMEM;
  835. hci_uart_set_flow_control(hu, true);
  836. skb_put_u8(skb, cmd);
  837. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  838. skb_queue_tail(&qca->txq, skb);
  839. hci_uart_tx_wakeup(hu);
  840. /* Wait for 100 uS for SoC to settle down */
  841. usleep_range(100, 200);
  842. hci_uart_set_flow_control(hu, false);
  843. return 0;
  844. }
  845. static unsigned int qca_get_speed(struct hci_uart *hu,
  846. enum qca_speed_type speed_type)
  847. {
  848. unsigned int speed = 0;
  849. if (speed_type == QCA_INIT_SPEED) {
  850. if (hu->init_speed)
  851. speed = hu->init_speed;
  852. else if (hu->proto->init_speed)
  853. speed = hu->proto->init_speed;
  854. } else {
  855. if (hu->oper_speed)
  856. speed = hu->oper_speed;
  857. else if (hu->proto->oper_speed)
  858. speed = hu->proto->oper_speed;
  859. }
  860. return speed;
  861. }
  862. static int qca_check_speeds(struct hci_uart *hu)
  863. {
  864. struct qca_serdev *qcadev;
  865. qcadev = serdev_device_get_drvdata(hu->serdev);
  866. if (qcadev->btsoc_type == QCA_WCN3990) {
  867. if (!qca_get_speed(hu, QCA_INIT_SPEED) &&
  868. !qca_get_speed(hu, QCA_OPER_SPEED))
  869. return -EINVAL;
  870. } else {
  871. if (!qca_get_speed(hu, QCA_INIT_SPEED) ||
  872. !qca_get_speed(hu, QCA_OPER_SPEED))
  873. return -EINVAL;
  874. }
  875. return 0;
  876. }
  877. static int qca_set_speed(struct hci_uart *hu, enum qca_speed_type speed_type)
  878. {
  879. unsigned int speed, qca_baudrate;
  880. int ret;
  881. if (speed_type == QCA_INIT_SPEED) {
  882. speed = qca_get_speed(hu, QCA_INIT_SPEED);
  883. if (speed)
  884. host_set_baudrate(hu, speed);
  885. } else {
  886. speed = qca_get_speed(hu, QCA_OPER_SPEED);
  887. if (!speed)
  888. return 0;
  889. qca_baudrate = qca_get_baudrate_value(speed);
  890. bt_dev_dbg(hu->hdev, "Set UART speed to %d", speed);
  891. ret = qca_set_baudrate(hu->hdev, qca_baudrate);
  892. if (ret)
  893. return ret;
  894. host_set_baudrate(hu, speed);
  895. }
  896. return 0;
  897. }
  898. static int qca_wcn3990_init(struct hci_uart *hu)
  899. {
  900. struct hci_dev *hdev = hu->hdev;
  901. int ret;
  902. /* Forcefully enable wcn3990 to enter in to boot mode. */
  903. host_set_baudrate(hu, 2400);
  904. ret = qca_send_power_pulse(hdev, QCA_WCN3990_POWEROFF_PULSE);
  905. if (ret)
  906. return ret;
  907. qca_set_speed(hu, QCA_INIT_SPEED);
  908. ret = qca_send_power_pulse(hdev, QCA_WCN3990_POWERON_PULSE);
  909. if (ret)
  910. return ret;
  911. /* Wait for 100 ms for SoC to boot */
  912. msleep(100);
  913. /* Now the device is in ready state to communicate with host.
  914. * To sync host with device we need to reopen port.
  915. * Without this, we will have RTS and CTS synchronization
  916. * issues.
  917. */
  918. serdev_device_close(hu->serdev);
  919. ret = serdev_device_open(hu->serdev);
  920. if (ret) {
  921. bt_dev_err(hu->hdev, "failed to open port");
  922. return ret;
  923. }
  924. hci_uart_set_flow_control(hu, false);
  925. return 0;
  926. }
  927. static int qca_setup(struct hci_uart *hu)
  928. {
  929. struct hci_dev *hdev = hu->hdev;
  930. struct qca_data *qca = hu->priv;
  931. unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
  932. struct qca_serdev *qcadev;
  933. int ret;
  934. int soc_ver = 0;
  935. qcadev = serdev_device_get_drvdata(hu->serdev);
  936. ret = qca_check_speeds(hu);
  937. if (ret)
  938. return ret;
  939. /* Patch downloading has to be done without IBS mode */
  940. clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
  941. if (qcadev->btsoc_type == QCA_WCN3990) {
  942. bt_dev_info(hdev, "setting up wcn3990");
  943. ret = qca_wcn3990_init(hu);
  944. if (ret)
  945. return ret;
  946. ret = qca_read_soc_version(hdev, &soc_ver);
  947. if (ret)
  948. return ret;
  949. } else {
  950. bt_dev_info(hdev, "ROME setup");
  951. qca_set_speed(hu, QCA_INIT_SPEED);
  952. }
  953. /* Setup user speed if needed */
  954. speed = qca_get_speed(hu, QCA_OPER_SPEED);
  955. if (speed) {
  956. ret = qca_set_speed(hu, QCA_OPER_SPEED);
  957. if (ret)
  958. return ret;
  959. qca_baudrate = qca_get_baudrate_value(speed);
  960. }
  961. if (qcadev->btsoc_type != QCA_WCN3990) {
  962. /* Get QCA version information */
  963. ret = qca_read_soc_version(hdev, &soc_ver);
  964. if (ret)
  965. return ret;
  966. }
  967. bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
  968. /* Setup patch / NVM configurations */
  969. ret = qca_uart_setup(hdev, qca_baudrate, qcadev->btsoc_type, soc_ver);
  970. if (!ret) {
  971. set_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
  972. qca_debugfs_init(hdev);
  973. } else if (ret == -ENOENT) {
  974. /* No patch/nvm-config found, run with original fw/config */
  975. ret = 0;
  976. } else if (ret == -EAGAIN) {
  977. /*
  978. * Userspace firmware loader will return -EAGAIN in case no
  979. * patch/nvm-config is found, so run with original fw/config.
  980. */
  981. ret = 0;
  982. }
  983. /* Setup bdaddr */
  984. hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
  985. return ret;
  986. }
  987. static struct hci_uart_proto qca_proto = {
  988. .id = HCI_UART_QCA,
  989. .name = "QCA",
  990. .manufacturer = 29,
  991. .init_speed = 115200,
  992. .oper_speed = 3000000,
  993. .open = qca_open,
  994. .close = qca_close,
  995. .flush = qca_flush,
  996. .setup = qca_setup,
  997. .recv = qca_recv,
  998. .enqueue = qca_enqueue,
  999. .dequeue = qca_dequeue,
  1000. };
  1001. static const struct qca_vreg_data qca_soc_data = {
  1002. .soc_type = QCA_WCN3990,
  1003. .vregs = (struct qca_vreg []) {
  1004. { "vddio", 1800000, 1900000, 15000 },
  1005. { "vddxo", 1800000, 1900000, 80000 },
  1006. { "vddrf", 1300000, 1350000, 300000 },
  1007. { "vddch0", 3300000, 3400000, 450000 },
  1008. },
  1009. .num_vregs = 4,
  1010. };
  1011. static void qca_power_shutdown(struct hci_uart *hu)
  1012. {
  1013. struct serdev_device *serdev = hu->serdev;
  1014. unsigned char cmd = QCA_WCN3990_POWEROFF_PULSE;
  1015. host_set_baudrate(hu, 2400);
  1016. hci_uart_set_flow_control(hu, true);
  1017. serdev_device_write_buf(serdev, &cmd, sizeof(cmd));
  1018. hci_uart_set_flow_control(hu, false);
  1019. qca_power_setup(hu, false);
  1020. }
  1021. static int qca_enable_regulator(struct qca_vreg vregs,
  1022. struct regulator *regulator)
  1023. {
  1024. int ret;
  1025. ret = regulator_set_voltage(regulator, vregs.min_uV,
  1026. vregs.max_uV);
  1027. if (ret)
  1028. return ret;
  1029. if (vregs.load_uA)
  1030. ret = regulator_set_load(regulator,
  1031. vregs.load_uA);
  1032. if (ret)
  1033. return ret;
  1034. return regulator_enable(regulator);
  1035. }
  1036. static void qca_disable_regulator(struct qca_vreg vregs,
  1037. struct regulator *regulator)
  1038. {
  1039. regulator_disable(regulator);
  1040. regulator_set_voltage(regulator, 0, vregs.max_uV);
  1041. if (vregs.load_uA)
  1042. regulator_set_load(regulator, 0);
  1043. }
  1044. static int qca_power_setup(struct hci_uart *hu, bool on)
  1045. {
  1046. struct qca_vreg *vregs;
  1047. struct regulator_bulk_data *vreg_bulk;
  1048. struct qca_serdev *qcadev;
  1049. int i, num_vregs, ret = 0;
  1050. qcadev = serdev_device_get_drvdata(hu->serdev);
  1051. if (!qcadev || !qcadev->bt_power || !qcadev->bt_power->vreg_data ||
  1052. !qcadev->bt_power->vreg_bulk)
  1053. return -EINVAL;
  1054. vregs = qcadev->bt_power->vreg_data->vregs;
  1055. vreg_bulk = qcadev->bt_power->vreg_bulk;
  1056. num_vregs = qcadev->bt_power->vreg_data->num_vregs;
  1057. BT_DBG("on: %d", on);
  1058. if (on && !qcadev->bt_power->vregs_on) {
  1059. for (i = 0; i < num_vregs; i++) {
  1060. ret = qca_enable_regulator(vregs[i],
  1061. vreg_bulk[i].consumer);
  1062. if (ret)
  1063. break;
  1064. }
  1065. if (ret) {
  1066. BT_ERR("failed to enable regulator:%s", vregs[i].name);
  1067. /* turn off regulators which are enabled */
  1068. for (i = i - 1; i >= 0; i--)
  1069. qca_disable_regulator(vregs[i],
  1070. vreg_bulk[i].consumer);
  1071. } else {
  1072. qcadev->bt_power->vregs_on = true;
  1073. }
  1074. } else if (!on && qcadev->bt_power->vregs_on) {
  1075. /* turn off regulator in reverse order */
  1076. i = qcadev->bt_power->vreg_data->num_vregs - 1;
  1077. for ( ; i >= 0; i--)
  1078. qca_disable_regulator(vregs[i], vreg_bulk[i].consumer);
  1079. qcadev->bt_power->vregs_on = false;
  1080. }
  1081. return ret;
  1082. }
  1083. static int qca_init_regulators(struct qca_power *qca,
  1084. const struct qca_vreg *vregs, size_t num_vregs)
  1085. {
  1086. int i;
  1087. qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
  1088. sizeof(struct regulator_bulk_data),
  1089. GFP_KERNEL);
  1090. if (!qca->vreg_bulk)
  1091. return -ENOMEM;
  1092. for (i = 0; i < num_vregs; i++)
  1093. qca->vreg_bulk[i].supply = vregs[i].name;
  1094. return devm_regulator_bulk_get(qca->dev, num_vregs, qca->vreg_bulk);
  1095. }
  1096. static int qca_serdev_probe(struct serdev_device *serdev)
  1097. {
  1098. struct qca_serdev *qcadev;
  1099. const struct qca_vreg_data *data;
  1100. int err;
  1101. qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
  1102. if (!qcadev)
  1103. return -ENOMEM;
  1104. qcadev->serdev_hu.serdev = serdev;
  1105. data = of_device_get_match_data(&serdev->dev);
  1106. serdev_device_set_drvdata(serdev, qcadev);
  1107. if (data && data->soc_type == QCA_WCN3990) {
  1108. qcadev->btsoc_type = QCA_WCN3990;
  1109. qcadev->bt_power = devm_kzalloc(&serdev->dev,
  1110. sizeof(struct qca_power),
  1111. GFP_KERNEL);
  1112. if (!qcadev->bt_power)
  1113. return -ENOMEM;
  1114. qcadev->bt_power->dev = &serdev->dev;
  1115. qcadev->bt_power->vreg_data = data;
  1116. err = qca_init_regulators(qcadev->bt_power, data->vregs,
  1117. data->num_vregs);
  1118. if (err) {
  1119. BT_ERR("Failed to init regulators:%d", err);
  1120. goto out;
  1121. }
  1122. qcadev->bt_power->vregs_on = false;
  1123. device_property_read_u32(&serdev->dev, "max-speed",
  1124. &qcadev->oper_speed);
  1125. if (!qcadev->oper_speed)
  1126. BT_DBG("UART will pick default operating speed");
  1127. err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
  1128. if (err) {
  1129. BT_ERR("wcn3990 serdev registration failed");
  1130. goto out;
  1131. }
  1132. } else {
  1133. qcadev->btsoc_type = QCA_ROME;
  1134. qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
  1135. GPIOD_OUT_LOW);
  1136. if (IS_ERR(qcadev->bt_en)) {
  1137. dev_err(&serdev->dev, "failed to acquire enable gpio\n");
  1138. return PTR_ERR(qcadev->bt_en);
  1139. }
  1140. qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
  1141. if (IS_ERR(qcadev->susclk)) {
  1142. dev_err(&serdev->dev, "failed to acquire clk\n");
  1143. return PTR_ERR(qcadev->susclk);
  1144. }
  1145. err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
  1146. if (err)
  1147. return err;
  1148. err = clk_prepare_enable(qcadev->susclk);
  1149. if (err)
  1150. return err;
  1151. err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
  1152. if (err)
  1153. clk_disable_unprepare(qcadev->susclk);
  1154. }
  1155. out: return err;
  1156. }
  1157. static void qca_serdev_remove(struct serdev_device *serdev)
  1158. {
  1159. struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
  1160. if (qcadev->btsoc_type == QCA_WCN3990)
  1161. qca_power_shutdown(&qcadev->serdev_hu);
  1162. else
  1163. clk_disable_unprepare(qcadev->susclk);
  1164. hci_uart_unregister_device(&qcadev->serdev_hu);
  1165. }
  1166. static const struct of_device_id qca_bluetooth_of_match[] = {
  1167. { .compatible = "qcom,qca6174-bt" },
  1168. { .compatible = "qcom,wcn3990-bt", .data = &qca_soc_data},
  1169. { /* sentinel */ }
  1170. };
  1171. MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
  1172. static struct serdev_device_driver qca_serdev_driver = {
  1173. .probe = qca_serdev_probe,
  1174. .remove = qca_serdev_remove,
  1175. .driver = {
  1176. .name = "hci_uart_qca",
  1177. .of_match_table = qca_bluetooth_of_match,
  1178. },
  1179. };
  1180. int __init qca_init(void)
  1181. {
  1182. serdev_device_driver_register(&qca_serdev_driver);
  1183. return hci_uart_register_proto(&qca_proto);
  1184. }
  1185. int __exit qca_deinit(void)
  1186. {
  1187. serdev_device_driver_unregister(&qca_serdev_driver);
  1188. return hci_uart_unregister_proto(&qca_proto);
  1189. }