hci_qca.c 37 KB

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