hci_qca.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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 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/gpio/consumer.h>
  34. #include <linux/mod_devicetable.h>
  35. #include <linux/module.h>
  36. #include <linux/serdev.h>
  37. #include <net/bluetooth/bluetooth.h>
  38. #include <net/bluetooth/hci_core.h>
  39. #include "hci_uart.h"
  40. #include "btqca.h"
  41. /* HCI_IBS protocol messages */
  42. #define HCI_IBS_SLEEP_IND 0xFE
  43. #define HCI_IBS_WAKE_IND 0xFD
  44. #define HCI_IBS_WAKE_ACK 0xFC
  45. #define HCI_MAX_IBS_SIZE 10
  46. /* Controller states */
  47. #define STATE_IN_BAND_SLEEP_ENABLED 1
  48. #define IBS_WAKE_RETRANS_TIMEOUT_MS 100
  49. #define IBS_TX_IDLE_TIMEOUT_MS 2000
  50. #define BAUDRATE_SETTLE_TIMEOUT_MS 300
  51. /* susclk rate */
  52. #define SUSCLK_RATE_32KHZ 32768
  53. /* HCI_IBS transmit side sleep protocol states */
  54. enum tx_ibs_states {
  55. HCI_IBS_TX_ASLEEP,
  56. HCI_IBS_TX_WAKING,
  57. HCI_IBS_TX_AWAKE,
  58. };
  59. /* HCI_IBS receive side sleep protocol states */
  60. enum rx_states {
  61. HCI_IBS_RX_ASLEEP,
  62. HCI_IBS_RX_AWAKE,
  63. };
  64. /* HCI_IBS transmit and receive side clock state vote */
  65. enum hci_ibs_clock_state_vote {
  66. HCI_IBS_VOTE_STATS_UPDATE,
  67. HCI_IBS_TX_VOTE_CLOCK_ON,
  68. HCI_IBS_TX_VOTE_CLOCK_OFF,
  69. HCI_IBS_RX_VOTE_CLOCK_ON,
  70. HCI_IBS_RX_VOTE_CLOCK_OFF,
  71. };
  72. struct qca_data {
  73. struct hci_uart *hu;
  74. struct sk_buff *rx_skb;
  75. struct sk_buff_head txq;
  76. struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
  77. spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
  78. u8 tx_ibs_state; /* HCI_IBS transmit side power state*/
  79. u8 rx_ibs_state; /* HCI_IBS receive side power state */
  80. bool tx_vote; /* Clock must be on for TX */
  81. bool rx_vote; /* Clock must be on for RX */
  82. struct timer_list tx_idle_timer;
  83. u32 tx_idle_delay;
  84. struct timer_list wake_retrans_timer;
  85. u32 wake_retrans;
  86. struct workqueue_struct *workqueue;
  87. struct work_struct ws_awake_rx;
  88. struct work_struct ws_awake_device;
  89. struct work_struct ws_rx_vote_off;
  90. struct work_struct ws_tx_vote_off;
  91. unsigned long flags;
  92. /* For debugging purpose */
  93. u64 ibs_sent_wacks;
  94. u64 ibs_sent_slps;
  95. u64 ibs_sent_wakes;
  96. u64 ibs_recv_wacks;
  97. u64 ibs_recv_slps;
  98. u64 ibs_recv_wakes;
  99. u64 vote_last_jif;
  100. u32 vote_on_ms;
  101. u32 vote_off_ms;
  102. u64 tx_votes_on;
  103. u64 rx_votes_on;
  104. u64 tx_votes_off;
  105. u64 rx_votes_off;
  106. u64 votes_on;
  107. u64 votes_off;
  108. };
  109. struct qca_serdev {
  110. struct hci_uart serdev_hu;
  111. struct gpio_desc *bt_en;
  112. struct clk *susclk;
  113. };
  114. static void __serial_clock_on(struct tty_struct *tty)
  115. {
  116. /* TODO: Some chipset requires to enable UART clock on client
  117. * side to save power consumption or manual work is required.
  118. * Please put your code to control UART clock here if needed
  119. */
  120. }
  121. static void __serial_clock_off(struct tty_struct *tty)
  122. {
  123. /* TODO: Some chipset requires to disable UART clock on client
  124. * side to save power consumption or manual work is required.
  125. * Please put your code to control UART clock off here if needed
  126. */
  127. }
  128. /* serial_clock_vote needs to be called with the ibs lock held */
  129. static void serial_clock_vote(unsigned long vote, struct hci_uart *hu)
  130. {
  131. struct qca_data *qca = hu->priv;
  132. unsigned int diff;
  133. bool old_vote = (qca->tx_vote | qca->rx_vote);
  134. bool new_vote;
  135. switch (vote) {
  136. case HCI_IBS_VOTE_STATS_UPDATE:
  137. diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
  138. if (old_vote)
  139. qca->vote_off_ms += diff;
  140. else
  141. qca->vote_on_ms += diff;
  142. return;
  143. case HCI_IBS_TX_VOTE_CLOCK_ON:
  144. qca->tx_vote = true;
  145. qca->tx_votes_on++;
  146. new_vote = true;
  147. break;
  148. case HCI_IBS_RX_VOTE_CLOCK_ON:
  149. qca->rx_vote = true;
  150. qca->rx_votes_on++;
  151. new_vote = true;
  152. break;
  153. case HCI_IBS_TX_VOTE_CLOCK_OFF:
  154. qca->tx_vote = false;
  155. qca->tx_votes_off++;
  156. new_vote = qca->rx_vote | qca->tx_vote;
  157. break;
  158. case HCI_IBS_RX_VOTE_CLOCK_OFF:
  159. qca->rx_vote = false;
  160. qca->rx_votes_off++;
  161. new_vote = qca->rx_vote | qca->tx_vote;
  162. break;
  163. default:
  164. BT_ERR("Voting irregularity");
  165. return;
  166. }
  167. if (new_vote != old_vote) {
  168. if (new_vote)
  169. __serial_clock_on(hu->tty);
  170. else
  171. __serial_clock_off(hu->tty);
  172. BT_DBG("Vote serial clock %s(%s)", new_vote ? "true" : "false",
  173. vote ? "true" : "false");
  174. diff = jiffies_to_msecs(jiffies - qca->vote_last_jif);
  175. if (new_vote) {
  176. qca->votes_on++;
  177. qca->vote_off_ms += diff;
  178. } else {
  179. qca->votes_off++;
  180. qca->vote_on_ms += diff;
  181. }
  182. qca->vote_last_jif = jiffies;
  183. }
  184. }
  185. /* Builds and sends an HCI_IBS command packet.
  186. * These are very simple packets with only 1 cmd byte.
  187. */
  188. static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
  189. {
  190. int err = 0;
  191. struct sk_buff *skb = NULL;
  192. struct qca_data *qca = hu->priv;
  193. BT_DBG("hu %p send hci ibs cmd 0x%x", hu, cmd);
  194. skb = bt_skb_alloc(1, GFP_ATOMIC);
  195. if (!skb) {
  196. BT_ERR("Failed to allocate memory for HCI_IBS packet");
  197. return -ENOMEM;
  198. }
  199. /* Assign HCI_IBS type */
  200. skb_put_u8(skb, cmd);
  201. skb_queue_tail(&qca->txq, skb);
  202. return err;
  203. }
  204. static void qca_wq_awake_device(struct work_struct *work)
  205. {
  206. struct qca_data *qca = container_of(work, struct qca_data,
  207. ws_awake_device);
  208. struct hci_uart *hu = qca->hu;
  209. unsigned long retrans_delay;
  210. BT_DBG("hu %p wq awake device", hu);
  211. /* Vote for serial clock */
  212. serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
  213. spin_lock(&qca->hci_ibs_lock);
  214. /* Send wake indication to device */
  215. if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
  216. BT_ERR("Failed to send WAKE to device");
  217. qca->ibs_sent_wakes++;
  218. /* Start retransmit timer */
  219. retrans_delay = msecs_to_jiffies(qca->wake_retrans);
  220. mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
  221. spin_unlock(&qca->hci_ibs_lock);
  222. /* Actually send the packets */
  223. hci_uart_tx_wakeup(hu);
  224. }
  225. static void qca_wq_awake_rx(struct work_struct *work)
  226. {
  227. struct qca_data *qca = container_of(work, struct qca_data,
  228. ws_awake_rx);
  229. struct hci_uart *hu = qca->hu;
  230. BT_DBG("hu %p wq awake rx", hu);
  231. serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
  232. spin_lock(&qca->hci_ibs_lock);
  233. qca->rx_ibs_state = HCI_IBS_RX_AWAKE;
  234. /* Always acknowledge device wake up,
  235. * sending IBS message doesn't count as TX ON.
  236. */
  237. if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
  238. BT_ERR("Failed to acknowledge device wake up");
  239. qca->ibs_sent_wacks++;
  240. spin_unlock(&qca->hci_ibs_lock);
  241. /* Actually send the packets */
  242. hci_uart_tx_wakeup(hu);
  243. }
  244. static void qca_wq_serial_rx_clock_vote_off(struct work_struct *work)
  245. {
  246. struct qca_data *qca = container_of(work, struct qca_data,
  247. ws_rx_vote_off);
  248. struct hci_uart *hu = qca->hu;
  249. BT_DBG("hu %p rx clock vote off", hu);
  250. serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
  251. }
  252. static void qca_wq_serial_tx_clock_vote_off(struct work_struct *work)
  253. {
  254. struct qca_data *qca = container_of(work, struct qca_data,
  255. ws_tx_vote_off);
  256. struct hci_uart *hu = qca->hu;
  257. BT_DBG("hu %p tx clock vote off", hu);
  258. /* Run HCI tx handling unlocked */
  259. hci_uart_tx_wakeup(hu);
  260. /* Now that message queued to tty driver, vote for tty clocks off.
  261. * It is up to the tty driver to pend the clocks off until tx done.
  262. */
  263. serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
  264. }
  265. static void hci_ibs_tx_idle_timeout(struct timer_list *t)
  266. {
  267. struct qca_data *qca = from_timer(qca, t, tx_idle_timer);
  268. struct hci_uart *hu = qca->hu;
  269. unsigned long flags;
  270. BT_DBG("hu %p idle timeout in %d state", hu, qca->tx_ibs_state);
  271. spin_lock_irqsave_nested(&qca->hci_ibs_lock,
  272. flags, SINGLE_DEPTH_NESTING);
  273. switch (qca->tx_ibs_state) {
  274. case HCI_IBS_TX_AWAKE:
  275. /* TX_IDLE, go to SLEEP */
  276. if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
  277. BT_ERR("Failed to send SLEEP to device");
  278. break;
  279. }
  280. qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
  281. qca->ibs_sent_slps++;
  282. queue_work(qca->workqueue, &qca->ws_tx_vote_off);
  283. break;
  284. case HCI_IBS_TX_ASLEEP:
  285. case HCI_IBS_TX_WAKING:
  286. /* Fall through */
  287. default:
  288. BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
  289. break;
  290. }
  291. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  292. }
  293. static void hci_ibs_wake_retrans_timeout(struct timer_list *t)
  294. {
  295. struct qca_data *qca = from_timer(qca, t, wake_retrans_timer);
  296. struct hci_uart *hu = qca->hu;
  297. unsigned long flags, retrans_delay;
  298. bool retransmit = false;
  299. BT_DBG("hu %p wake retransmit timeout in %d state",
  300. hu, qca->tx_ibs_state);
  301. spin_lock_irqsave_nested(&qca->hci_ibs_lock,
  302. flags, SINGLE_DEPTH_NESTING);
  303. switch (qca->tx_ibs_state) {
  304. case HCI_IBS_TX_WAKING:
  305. /* No WAKE_ACK, retransmit WAKE */
  306. retransmit = true;
  307. if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
  308. BT_ERR("Failed to acknowledge device wake up");
  309. break;
  310. }
  311. qca->ibs_sent_wakes++;
  312. retrans_delay = msecs_to_jiffies(qca->wake_retrans);
  313. mod_timer(&qca->wake_retrans_timer, jiffies + retrans_delay);
  314. break;
  315. case HCI_IBS_TX_ASLEEP:
  316. case HCI_IBS_TX_AWAKE:
  317. /* Fall through */
  318. default:
  319. BT_ERR("Spurious timeout tx state %d", qca->tx_ibs_state);
  320. break;
  321. }
  322. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  323. if (retransmit)
  324. hci_uart_tx_wakeup(hu);
  325. }
  326. /* Initialize protocol */
  327. static int qca_open(struct hci_uart *hu)
  328. {
  329. struct qca_serdev *qcadev;
  330. struct qca_data *qca;
  331. BT_DBG("hu %p qca_open", hu);
  332. qca = kzalloc(sizeof(struct qca_data), GFP_ATOMIC);
  333. if (!qca)
  334. return -ENOMEM;
  335. skb_queue_head_init(&qca->txq);
  336. skb_queue_head_init(&qca->tx_wait_q);
  337. spin_lock_init(&qca->hci_ibs_lock);
  338. qca->workqueue = alloc_ordered_workqueue("qca_wq", 0);
  339. if (!qca->workqueue) {
  340. BT_ERR("QCA Workqueue not initialized properly");
  341. kfree(qca);
  342. return -ENOMEM;
  343. }
  344. INIT_WORK(&qca->ws_awake_rx, qca_wq_awake_rx);
  345. INIT_WORK(&qca->ws_awake_device, qca_wq_awake_device);
  346. INIT_WORK(&qca->ws_rx_vote_off, qca_wq_serial_rx_clock_vote_off);
  347. INIT_WORK(&qca->ws_tx_vote_off, qca_wq_serial_tx_clock_vote_off);
  348. qca->hu = hu;
  349. /* Assume we start with both sides asleep -- extra wakes OK */
  350. qca->tx_ibs_state = HCI_IBS_TX_ASLEEP;
  351. qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
  352. /* clocks actually on, but we start votes off */
  353. qca->tx_vote = false;
  354. qca->rx_vote = false;
  355. qca->flags = 0;
  356. qca->ibs_sent_wacks = 0;
  357. qca->ibs_sent_slps = 0;
  358. qca->ibs_sent_wakes = 0;
  359. qca->ibs_recv_wacks = 0;
  360. qca->ibs_recv_slps = 0;
  361. qca->ibs_recv_wakes = 0;
  362. qca->vote_last_jif = jiffies;
  363. qca->vote_on_ms = 0;
  364. qca->vote_off_ms = 0;
  365. qca->votes_on = 0;
  366. qca->votes_off = 0;
  367. qca->tx_votes_on = 0;
  368. qca->tx_votes_off = 0;
  369. qca->rx_votes_on = 0;
  370. qca->rx_votes_off = 0;
  371. hu->priv = qca;
  372. timer_setup(&qca->wake_retrans_timer, hci_ibs_wake_retrans_timeout, 0);
  373. qca->wake_retrans = IBS_WAKE_RETRANS_TIMEOUT_MS;
  374. timer_setup(&qca->tx_idle_timer, hci_ibs_tx_idle_timeout, 0);
  375. qca->tx_idle_delay = IBS_TX_IDLE_TIMEOUT_MS;
  376. if (hu->serdev) {
  377. serdev_device_open(hu->serdev);
  378. qcadev = serdev_device_get_drvdata(hu->serdev);
  379. gpiod_set_value_cansleep(qcadev->bt_en, 1);
  380. }
  381. BT_DBG("HCI_UART_QCA open, tx_idle_delay=%u, wake_retrans=%u",
  382. qca->tx_idle_delay, qca->wake_retrans);
  383. return 0;
  384. }
  385. static void qca_debugfs_init(struct hci_dev *hdev)
  386. {
  387. struct hci_uart *hu = hci_get_drvdata(hdev);
  388. struct qca_data *qca = hu->priv;
  389. struct dentry *ibs_dir;
  390. umode_t mode;
  391. if (!hdev->debugfs)
  392. return;
  393. ibs_dir = debugfs_create_dir("ibs", hdev->debugfs);
  394. /* read only */
  395. mode = S_IRUGO;
  396. debugfs_create_u8("tx_ibs_state", mode, ibs_dir, &qca->tx_ibs_state);
  397. debugfs_create_u8("rx_ibs_state", mode, ibs_dir, &qca->rx_ibs_state);
  398. debugfs_create_u64("ibs_sent_sleeps", mode, ibs_dir,
  399. &qca->ibs_sent_slps);
  400. debugfs_create_u64("ibs_sent_wakes", mode, ibs_dir,
  401. &qca->ibs_sent_wakes);
  402. debugfs_create_u64("ibs_sent_wake_acks", mode, ibs_dir,
  403. &qca->ibs_sent_wacks);
  404. debugfs_create_u64("ibs_recv_sleeps", mode, ibs_dir,
  405. &qca->ibs_recv_slps);
  406. debugfs_create_u64("ibs_recv_wakes", mode, ibs_dir,
  407. &qca->ibs_recv_wakes);
  408. debugfs_create_u64("ibs_recv_wake_acks", mode, ibs_dir,
  409. &qca->ibs_recv_wacks);
  410. debugfs_create_bool("tx_vote", mode, ibs_dir, &qca->tx_vote);
  411. debugfs_create_u64("tx_votes_on", mode, ibs_dir, &qca->tx_votes_on);
  412. debugfs_create_u64("tx_votes_off", mode, ibs_dir, &qca->tx_votes_off);
  413. debugfs_create_bool("rx_vote", mode, ibs_dir, &qca->rx_vote);
  414. debugfs_create_u64("rx_votes_on", mode, ibs_dir, &qca->rx_votes_on);
  415. debugfs_create_u64("rx_votes_off", mode, ibs_dir, &qca->rx_votes_off);
  416. debugfs_create_u64("votes_on", mode, ibs_dir, &qca->votes_on);
  417. debugfs_create_u64("votes_off", mode, ibs_dir, &qca->votes_off);
  418. debugfs_create_u32("vote_on_ms", mode, ibs_dir, &qca->vote_on_ms);
  419. debugfs_create_u32("vote_off_ms", mode, ibs_dir, &qca->vote_off_ms);
  420. /* read/write */
  421. mode = S_IRUGO | S_IWUSR;
  422. debugfs_create_u32("wake_retrans", mode, ibs_dir, &qca->wake_retrans);
  423. debugfs_create_u32("tx_idle_delay", mode, ibs_dir,
  424. &qca->tx_idle_delay);
  425. }
  426. /* Flush protocol data */
  427. static int qca_flush(struct hci_uart *hu)
  428. {
  429. struct qca_data *qca = hu->priv;
  430. BT_DBG("hu %p qca flush", hu);
  431. skb_queue_purge(&qca->tx_wait_q);
  432. skb_queue_purge(&qca->txq);
  433. return 0;
  434. }
  435. /* Close protocol */
  436. static int qca_close(struct hci_uart *hu)
  437. {
  438. struct qca_serdev *qcadev;
  439. struct qca_data *qca = hu->priv;
  440. BT_DBG("hu %p qca close", hu);
  441. serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
  442. skb_queue_purge(&qca->tx_wait_q);
  443. skb_queue_purge(&qca->txq);
  444. del_timer(&qca->tx_idle_timer);
  445. del_timer(&qca->wake_retrans_timer);
  446. destroy_workqueue(qca->workqueue);
  447. qca->hu = NULL;
  448. if (hu->serdev) {
  449. serdev_device_close(hu->serdev);
  450. qcadev = serdev_device_get_drvdata(hu->serdev);
  451. gpiod_set_value_cansleep(qcadev->bt_en, 0);
  452. }
  453. kfree_skb(qca->rx_skb);
  454. hu->priv = NULL;
  455. kfree(qca);
  456. return 0;
  457. }
  458. /* Called upon a wake-up-indication from the device.
  459. */
  460. static void device_want_to_wakeup(struct hci_uart *hu)
  461. {
  462. unsigned long flags;
  463. struct qca_data *qca = hu->priv;
  464. BT_DBG("hu %p want to wake up", hu);
  465. spin_lock_irqsave(&qca->hci_ibs_lock, flags);
  466. qca->ibs_recv_wakes++;
  467. switch (qca->rx_ibs_state) {
  468. case HCI_IBS_RX_ASLEEP:
  469. /* Make sure clock is on - we may have turned clock off since
  470. * receiving the wake up indicator awake rx clock.
  471. */
  472. queue_work(qca->workqueue, &qca->ws_awake_rx);
  473. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  474. return;
  475. case HCI_IBS_RX_AWAKE:
  476. /* Always acknowledge device wake up,
  477. * sending IBS message doesn't count as TX ON.
  478. */
  479. if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
  480. BT_ERR("Failed to acknowledge device wake up");
  481. break;
  482. }
  483. qca->ibs_sent_wacks++;
  484. break;
  485. default:
  486. /* Any other state is illegal */
  487. BT_ERR("Received HCI_IBS_WAKE_IND in rx state %d",
  488. qca->rx_ibs_state);
  489. break;
  490. }
  491. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  492. /* Actually send the packets */
  493. hci_uart_tx_wakeup(hu);
  494. }
  495. /* Called upon a sleep-indication from the device.
  496. */
  497. static void device_want_to_sleep(struct hci_uart *hu)
  498. {
  499. unsigned long flags;
  500. struct qca_data *qca = hu->priv;
  501. BT_DBG("hu %p want to sleep", hu);
  502. spin_lock_irqsave(&qca->hci_ibs_lock, flags);
  503. qca->ibs_recv_slps++;
  504. switch (qca->rx_ibs_state) {
  505. case HCI_IBS_RX_AWAKE:
  506. /* Update state */
  507. qca->rx_ibs_state = HCI_IBS_RX_ASLEEP;
  508. /* Vote off rx clock under workqueue */
  509. queue_work(qca->workqueue, &qca->ws_rx_vote_off);
  510. break;
  511. case HCI_IBS_RX_ASLEEP:
  512. /* Fall through */
  513. default:
  514. /* Any other state is illegal */
  515. BT_ERR("Received HCI_IBS_SLEEP_IND in rx state %d",
  516. qca->rx_ibs_state);
  517. break;
  518. }
  519. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  520. }
  521. /* Called upon wake-up-acknowledgement from the device
  522. */
  523. static void device_woke_up(struct hci_uart *hu)
  524. {
  525. unsigned long flags, idle_delay;
  526. struct qca_data *qca = hu->priv;
  527. struct sk_buff *skb = NULL;
  528. BT_DBG("hu %p woke up", hu);
  529. spin_lock_irqsave(&qca->hci_ibs_lock, flags);
  530. qca->ibs_recv_wacks++;
  531. switch (qca->tx_ibs_state) {
  532. case HCI_IBS_TX_AWAKE:
  533. /* Expect one if we send 2 WAKEs */
  534. BT_DBG("Received HCI_IBS_WAKE_ACK in tx state %d",
  535. qca->tx_ibs_state);
  536. break;
  537. case HCI_IBS_TX_WAKING:
  538. /* Send pending packets */
  539. while ((skb = skb_dequeue(&qca->tx_wait_q)))
  540. skb_queue_tail(&qca->txq, skb);
  541. /* Switch timers and change state to HCI_IBS_TX_AWAKE */
  542. del_timer(&qca->wake_retrans_timer);
  543. idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
  544. mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
  545. qca->tx_ibs_state = HCI_IBS_TX_AWAKE;
  546. break;
  547. case HCI_IBS_TX_ASLEEP:
  548. /* Fall through */
  549. default:
  550. BT_ERR("Received HCI_IBS_WAKE_ACK in tx state %d",
  551. qca->tx_ibs_state);
  552. break;
  553. }
  554. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  555. /* Actually send the packets */
  556. hci_uart_tx_wakeup(hu);
  557. }
  558. /* Enqueue frame for transmittion (padding, crc, etc) may be called from
  559. * two simultaneous tasklets.
  560. */
  561. static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  562. {
  563. unsigned long flags = 0, idle_delay;
  564. struct qca_data *qca = hu->priv;
  565. BT_DBG("hu %p qca enq skb %p tx_ibs_state %d", hu, skb,
  566. qca->tx_ibs_state);
  567. /* Prepend skb with frame type */
  568. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  569. /* Don't go to sleep in middle of patch download or
  570. * Out-Of-Band(GPIOs control) sleep is selected.
  571. */
  572. if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags)) {
  573. skb_queue_tail(&qca->txq, skb);
  574. return 0;
  575. }
  576. spin_lock_irqsave(&qca->hci_ibs_lock, flags);
  577. /* Act according to current state */
  578. switch (qca->tx_ibs_state) {
  579. case HCI_IBS_TX_AWAKE:
  580. BT_DBG("Device awake, sending normally");
  581. skb_queue_tail(&qca->txq, skb);
  582. idle_delay = msecs_to_jiffies(qca->tx_idle_delay);
  583. mod_timer(&qca->tx_idle_timer, jiffies + idle_delay);
  584. break;
  585. case HCI_IBS_TX_ASLEEP:
  586. BT_DBG("Device asleep, waking up and queueing packet");
  587. /* Save packet for later */
  588. skb_queue_tail(&qca->tx_wait_q, skb);
  589. qca->tx_ibs_state = HCI_IBS_TX_WAKING;
  590. /* Schedule a work queue to wake up device */
  591. queue_work(qca->workqueue, &qca->ws_awake_device);
  592. break;
  593. case HCI_IBS_TX_WAKING:
  594. BT_DBG("Device waking up, queueing packet");
  595. /* Transient state; just keep packet for later */
  596. skb_queue_tail(&qca->tx_wait_q, skb);
  597. break;
  598. default:
  599. BT_ERR("Illegal tx state: %d (losing packet)",
  600. qca->tx_ibs_state);
  601. kfree_skb(skb);
  602. break;
  603. }
  604. spin_unlock_irqrestore(&qca->hci_ibs_lock, flags);
  605. return 0;
  606. }
  607. static int qca_ibs_sleep_ind(struct hci_dev *hdev, struct sk_buff *skb)
  608. {
  609. struct hci_uart *hu = hci_get_drvdata(hdev);
  610. BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_SLEEP_IND);
  611. device_want_to_sleep(hu);
  612. kfree_skb(skb);
  613. return 0;
  614. }
  615. static int qca_ibs_wake_ind(struct hci_dev *hdev, struct sk_buff *skb)
  616. {
  617. struct hci_uart *hu = hci_get_drvdata(hdev);
  618. BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_IND);
  619. device_want_to_wakeup(hu);
  620. kfree_skb(skb);
  621. return 0;
  622. }
  623. static int qca_ibs_wake_ack(struct hci_dev *hdev, struct sk_buff *skb)
  624. {
  625. struct hci_uart *hu = hci_get_drvdata(hdev);
  626. BT_DBG("hu %p recv hci ibs cmd 0x%x", hu, HCI_IBS_WAKE_ACK);
  627. device_woke_up(hu);
  628. kfree_skb(skb);
  629. return 0;
  630. }
  631. #define QCA_IBS_SLEEP_IND_EVENT \
  632. .type = HCI_IBS_SLEEP_IND, \
  633. .hlen = 0, \
  634. .loff = 0, \
  635. .lsize = 0, \
  636. .maxlen = HCI_MAX_IBS_SIZE
  637. #define QCA_IBS_WAKE_IND_EVENT \
  638. .type = HCI_IBS_WAKE_IND, \
  639. .hlen = 0, \
  640. .loff = 0, \
  641. .lsize = 0, \
  642. .maxlen = HCI_MAX_IBS_SIZE
  643. #define QCA_IBS_WAKE_ACK_EVENT \
  644. .type = HCI_IBS_WAKE_ACK, \
  645. .hlen = 0, \
  646. .loff = 0, \
  647. .lsize = 0, \
  648. .maxlen = HCI_MAX_IBS_SIZE
  649. static const struct h4_recv_pkt qca_recv_pkts[] = {
  650. { H4_RECV_ACL, .recv = hci_recv_frame },
  651. { H4_RECV_SCO, .recv = hci_recv_frame },
  652. { H4_RECV_EVENT, .recv = hci_recv_frame },
  653. { QCA_IBS_WAKE_IND_EVENT, .recv = qca_ibs_wake_ind },
  654. { QCA_IBS_WAKE_ACK_EVENT, .recv = qca_ibs_wake_ack },
  655. { QCA_IBS_SLEEP_IND_EVENT, .recv = qca_ibs_sleep_ind },
  656. };
  657. static int qca_recv(struct hci_uart *hu, const void *data, int count)
  658. {
  659. struct qca_data *qca = hu->priv;
  660. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  661. return -EUNATCH;
  662. qca->rx_skb = h4_recv_buf(hu->hdev, qca->rx_skb, data, count,
  663. qca_recv_pkts, ARRAY_SIZE(qca_recv_pkts));
  664. if (IS_ERR(qca->rx_skb)) {
  665. int err = PTR_ERR(qca->rx_skb);
  666. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  667. qca->rx_skb = NULL;
  668. return err;
  669. }
  670. return count;
  671. }
  672. static struct sk_buff *qca_dequeue(struct hci_uart *hu)
  673. {
  674. struct qca_data *qca = hu->priv;
  675. return skb_dequeue(&qca->txq);
  676. }
  677. static uint8_t qca_get_baudrate_value(int speed)
  678. {
  679. switch (speed) {
  680. case 9600:
  681. return QCA_BAUDRATE_9600;
  682. case 19200:
  683. return QCA_BAUDRATE_19200;
  684. case 38400:
  685. return QCA_BAUDRATE_38400;
  686. case 57600:
  687. return QCA_BAUDRATE_57600;
  688. case 115200:
  689. return QCA_BAUDRATE_115200;
  690. case 230400:
  691. return QCA_BAUDRATE_230400;
  692. case 460800:
  693. return QCA_BAUDRATE_460800;
  694. case 500000:
  695. return QCA_BAUDRATE_500000;
  696. case 921600:
  697. return QCA_BAUDRATE_921600;
  698. case 1000000:
  699. return QCA_BAUDRATE_1000000;
  700. case 2000000:
  701. return QCA_BAUDRATE_2000000;
  702. case 3000000:
  703. return QCA_BAUDRATE_3000000;
  704. case 3500000:
  705. return QCA_BAUDRATE_3500000;
  706. default:
  707. return QCA_BAUDRATE_115200;
  708. }
  709. }
  710. static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
  711. {
  712. struct hci_uart *hu = hci_get_drvdata(hdev);
  713. struct qca_data *qca = hu->priv;
  714. struct sk_buff *skb;
  715. u8 cmd[] = { 0x01, 0x48, 0xFC, 0x01, 0x00 };
  716. if (baudrate > QCA_BAUDRATE_3000000)
  717. return -EINVAL;
  718. cmd[4] = baudrate;
  719. skb = bt_skb_alloc(sizeof(cmd), GFP_ATOMIC);
  720. if (!skb) {
  721. bt_dev_err(hdev, "Failed to allocate baudrate packet");
  722. return -ENOMEM;
  723. }
  724. /* Assign commands to change baudrate and packet type. */
  725. skb_put_data(skb, cmd, sizeof(cmd));
  726. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  727. skb_queue_tail(&qca->txq, skb);
  728. hci_uart_tx_wakeup(hu);
  729. /* wait 300ms to change new baudrate on controller side
  730. * controller will come back after they receive this HCI command
  731. * then host can communicate with new baudrate to controller
  732. */
  733. set_current_state(TASK_UNINTERRUPTIBLE);
  734. schedule_timeout(msecs_to_jiffies(BAUDRATE_SETTLE_TIMEOUT_MS));
  735. set_current_state(TASK_RUNNING);
  736. return 0;
  737. }
  738. static inline void host_set_baudrate(struct hci_uart *hu, unsigned int speed)
  739. {
  740. if (hu->serdev)
  741. serdev_device_set_baudrate(hu->serdev, speed);
  742. else
  743. hci_uart_set_baudrate(hu, speed);
  744. }
  745. static int qca_setup(struct hci_uart *hu)
  746. {
  747. struct hci_dev *hdev = hu->hdev;
  748. struct qca_data *qca = hu->priv;
  749. unsigned int speed, qca_baudrate = QCA_BAUDRATE_115200;
  750. int ret;
  751. bt_dev_info(hdev, "ROME setup");
  752. /* Patch downloading has to be done without IBS mode */
  753. clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
  754. /* Setup initial baudrate */
  755. speed = 0;
  756. if (hu->init_speed)
  757. speed = hu->init_speed;
  758. else if (hu->proto->init_speed)
  759. speed = hu->proto->init_speed;
  760. if (speed)
  761. host_set_baudrate(hu, speed);
  762. /* Setup user speed if needed */
  763. speed = 0;
  764. if (hu->oper_speed)
  765. speed = hu->oper_speed;
  766. else if (hu->proto->oper_speed)
  767. speed = hu->proto->oper_speed;
  768. if (speed) {
  769. qca_baudrate = qca_get_baudrate_value(speed);
  770. bt_dev_info(hdev, "Set UART speed to %d", speed);
  771. ret = qca_set_baudrate(hdev, qca_baudrate);
  772. if (ret) {
  773. bt_dev_err(hdev, "Failed to change the baud rate (%d)",
  774. ret);
  775. return ret;
  776. }
  777. host_set_baudrate(hu, speed);
  778. }
  779. /* Setup patch / NVM configurations */
  780. ret = qca_uart_setup_rome(hdev, qca_baudrate);
  781. if (!ret) {
  782. set_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags);
  783. qca_debugfs_init(hdev);
  784. } else if (ret == -ENOENT) {
  785. /* No patch/nvm-config found, run with original fw/config */
  786. ret = 0;
  787. } else if (ret == -EAGAIN) {
  788. /*
  789. * Userspace firmware loader will return -EAGAIN in case no
  790. * patch/nvm-config is found, so run with original fw/config.
  791. */
  792. ret = 0;
  793. }
  794. /* Setup bdaddr */
  795. hu->hdev->set_bdaddr = qca_set_bdaddr_rome;
  796. return ret;
  797. }
  798. static struct hci_uart_proto qca_proto = {
  799. .id = HCI_UART_QCA,
  800. .name = "QCA",
  801. .manufacturer = 29,
  802. .init_speed = 115200,
  803. .oper_speed = 3000000,
  804. .open = qca_open,
  805. .close = qca_close,
  806. .flush = qca_flush,
  807. .setup = qca_setup,
  808. .recv = qca_recv,
  809. .enqueue = qca_enqueue,
  810. .dequeue = qca_dequeue,
  811. };
  812. static int qca_serdev_probe(struct serdev_device *serdev)
  813. {
  814. struct qca_serdev *qcadev;
  815. int err;
  816. qcadev = devm_kzalloc(&serdev->dev, sizeof(*qcadev), GFP_KERNEL);
  817. if (!qcadev)
  818. return -ENOMEM;
  819. qcadev->serdev_hu.serdev = serdev;
  820. serdev_device_set_drvdata(serdev, qcadev);
  821. qcadev->bt_en = devm_gpiod_get(&serdev->dev, "enable",
  822. GPIOD_OUT_LOW);
  823. if (IS_ERR(qcadev->bt_en)) {
  824. dev_err(&serdev->dev, "failed to acquire enable gpio\n");
  825. return PTR_ERR(qcadev->bt_en);
  826. }
  827. qcadev->susclk = devm_clk_get(&serdev->dev, NULL);
  828. if (IS_ERR(qcadev->susclk)) {
  829. dev_err(&serdev->dev, "failed to acquire clk\n");
  830. return PTR_ERR(qcadev->susclk);
  831. }
  832. err = clk_set_rate(qcadev->susclk, SUSCLK_RATE_32KHZ);
  833. if (err)
  834. return err;
  835. err = clk_prepare_enable(qcadev->susclk);
  836. if (err)
  837. return err;
  838. err = hci_uart_register_device(&qcadev->serdev_hu, &qca_proto);
  839. if (err)
  840. clk_disable_unprepare(qcadev->susclk);
  841. return err;
  842. }
  843. static void qca_serdev_remove(struct serdev_device *serdev)
  844. {
  845. struct qca_serdev *qcadev = serdev_device_get_drvdata(serdev);
  846. hci_uart_unregister_device(&qcadev->serdev_hu);
  847. clk_disable_unprepare(qcadev->susclk);
  848. }
  849. static const struct of_device_id qca_bluetooth_of_match[] = {
  850. { .compatible = "qcom,qca6174-bt" },
  851. { /* sentinel */ }
  852. };
  853. MODULE_DEVICE_TABLE(of, qca_bluetooth_of_match);
  854. static struct serdev_device_driver qca_serdev_driver = {
  855. .probe = qca_serdev_probe,
  856. .remove = qca_serdev_remove,
  857. .driver = {
  858. .name = "hci_uart_qca",
  859. .of_match_table = qca_bluetooth_of_match,
  860. },
  861. };
  862. int __init qca_init(void)
  863. {
  864. serdev_device_driver_register(&qca_serdev_driver);
  865. return hci_uart_register_proto(&qca_proto);
  866. }
  867. int __exit qca_deinit(void)
  868. {
  869. serdev_device_driver_unregister(&qca_serdev_driver);
  870. return hci_uart_unregister_proto(&qca_proto);
  871. }