power.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #if IS_ENABLED(CONFIG_MSM_QMP)
  9. #include <linux/mailbox/qmp.h>
  10. #endif
  11. #include <linux/of.h>
  12. #include <linux/of_gpio.h>
  13. #include <linux/pinctrl/consumer.h>
  14. #include <linux/regulator/consumer.h>
  15. #if IS_ENABLED(CONFIG_QCOM_COMMAND_DB)
  16. #include <soc/qcom/cmd-db.h>
  17. #endif
  18. #include "main.h"
  19. #include "debug.h"
  20. #include "bus.h"
  21. #if IS_ENABLED(CONFIG_ARCH_QCOM)
  22. static struct cnss_vreg_cfg cnss_vreg_list[] = {
  23. {"vdd-wlan-core", 1300000, 1300000, 0, 0, 0},
  24. {"vdd-wlan-io", 1800000, 1800000, 0, 0, 0},
  25. {"vdd-wlan-xtal-aon", 0, 0, 0, 0, 0},
  26. {"vdd-wlan-xtal", 1800000, 1800000, 0, 2, 0},
  27. {"vdd-wlan", 0, 0, 0, 0, 0},
  28. {"vdd-wlan-ctrl1", 0, 0, 0, 0, 0},
  29. {"vdd-wlan-ctrl2", 0, 0, 0, 0, 0},
  30. {"vdd-wlan-sp2t", 2700000, 2700000, 0, 0, 0},
  31. {"wlan-ant-switch", 1800000, 1800000, 0, 0, 0},
  32. {"wlan-soc-swreg", 1200000, 1200000, 0, 0, 0},
  33. {"vdd-wlan-aon", 950000, 950000, 0, 0, 0},
  34. {"vdd-wlan-dig", 950000, 952000, 0, 0, 0},
  35. {"vdd-wlan-rfa1", 1900000, 1900000, 0, 0, 0},
  36. {"vdd-wlan-rfa2", 1350000, 1350000, 0, 0, 0},
  37. {"alt-sleep-clk", 0, 0, 0, 0, 0},
  38. {"vdd-wlan-en", 0, 0, 0, 10, 0},
  39. };
  40. static struct cnss_clk_cfg cnss_clk_list[] = {
  41. {"rf_clk", 0, 0},
  42. };
  43. #else
  44. static struct cnss_vreg_cfg cnss_vreg_list[] = {
  45. };
  46. static struct cnss_clk_cfg cnss_clk_list[] = {
  47. };
  48. #endif
  49. #define CNSS_VREG_INFO_SIZE ARRAY_SIZE(cnss_vreg_list)
  50. #define CNSS_CLK_INFO_SIZE ARRAY_SIZE(cnss_clk_list)
  51. #define MAX_PROP_SIZE 32
  52. #define BOOTSTRAP_GPIO "qcom,enable-bootstrap-gpio"
  53. #define BOOTSTRAP_ACTIVE "bootstrap_active"
  54. #define HOST_SOL_GPIO "wlan-host-sol-gpio"
  55. #define DEV_SOL_GPIO "wlan-dev-sol-gpio"
  56. #define SOL_DEFAULT "sol_default"
  57. #define WLAN_EN_GPIO "wlan-en-gpio"
  58. #define BT_EN_GPIO "qcom,bt-en-gpio"
  59. #define XO_CLK_GPIO "qcom,xo-clk-gpio"
  60. #define WLAN_EN_ACTIVE "wlan_en_active"
  61. #define WLAN_EN_SLEEP "wlan_en_sleep"
  62. #define BOOTSTRAP_DELAY 1000
  63. #define WLAN_ENABLE_DELAY 1000
  64. #define TCS_CMD_DATA_ADDR_OFFSET 0x4
  65. #define TCS_OFFSET 0xC8
  66. #define TCS_CMD_OFFSET 0x10
  67. #define MAX_TCS_NUM 8
  68. #define MAX_TCS_CMD_NUM 5
  69. #define BT_CXMX_VOLTAGE_MV 950
  70. #define CNSS_MBOX_MSG_MAX_LEN 64
  71. #define CNSS_MBOX_TIMEOUT_MS 1000
  72. /**
  73. * enum cnss_vreg_param: Voltage regulator TCS param
  74. * @CNSS_VREG_VOLTAGE: Provides voltage level to be configured in TCS
  75. * @CNSS_VREG_MODE: Regulator mode
  76. * @CNSS_VREG_TCS_ENABLE: Set Voltage regulator enable config in TCS
  77. */
  78. enum cnss_vreg_param {
  79. CNSS_VREG_VOLTAGE,
  80. CNSS_VREG_MODE,
  81. CNSS_VREG_ENABLE,
  82. };
  83. /**
  84. * enum cnss_tcs_seq: TCS sequence ID for trigger
  85. * CNSS_TCS_UP_SEQ: TCS Sequence based on up trigger / Wake TCS
  86. * CNSS_TCS_DOWN_SEQ: TCS Sequence based on down trigger / Sleep TCS
  87. * CNSS_TCS_ALL_SEQ: Update for both up and down triggers
  88. */
  89. enum cnss_tcs_seq {
  90. CNSS_TCS_UP_SEQ,
  91. CNSS_TCS_DOWN_SEQ,
  92. CNSS_TCS_ALL_SEQ,
  93. };
  94. static int cnss_get_vreg_single(struct cnss_plat_data *plat_priv,
  95. struct cnss_vreg_info *vreg)
  96. {
  97. int ret = 0;
  98. struct device *dev;
  99. struct regulator *reg;
  100. const __be32 *prop;
  101. char prop_name[MAX_PROP_SIZE] = {0};
  102. int len;
  103. dev = &plat_priv->plat_dev->dev;
  104. reg = devm_regulator_get_optional(dev, vreg->cfg.name);
  105. if (IS_ERR(reg)) {
  106. ret = PTR_ERR(reg);
  107. if (ret == -ENODEV)
  108. return ret;
  109. else if (ret == -EPROBE_DEFER)
  110. cnss_pr_info("EPROBE_DEFER for regulator: %s\n",
  111. vreg->cfg.name);
  112. else
  113. cnss_pr_err("Failed to get regulator %s, err = %d\n",
  114. vreg->cfg.name, ret);
  115. return ret;
  116. }
  117. vreg->reg = reg;
  118. snprintf(prop_name, MAX_PROP_SIZE, "qcom,%s-config",
  119. vreg->cfg.name);
  120. prop = of_get_property(dev->of_node, prop_name, &len);
  121. if (!prop || len != (5 * sizeof(__be32))) {
  122. cnss_pr_dbg("Property %s %s, use default\n", prop_name,
  123. prop ? "invalid format" : "doesn't exist");
  124. } else {
  125. vreg->cfg.min_uv = be32_to_cpup(&prop[0]);
  126. vreg->cfg.max_uv = be32_to_cpup(&prop[1]);
  127. vreg->cfg.load_ua = be32_to_cpup(&prop[2]);
  128. vreg->cfg.delay_us = be32_to_cpup(&prop[3]);
  129. vreg->cfg.need_unvote = be32_to_cpup(&prop[4]);
  130. }
  131. cnss_pr_dbg("Got regulator: %s, min_uv: %u, max_uv: %u, load_ua: %u, delay_us: %u, need_unvote: %u\n",
  132. vreg->cfg.name, vreg->cfg.min_uv,
  133. vreg->cfg.max_uv, vreg->cfg.load_ua,
  134. vreg->cfg.delay_us, vreg->cfg.need_unvote);
  135. return 0;
  136. }
  137. static void cnss_put_vreg_single(struct cnss_plat_data *plat_priv,
  138. struct cnss_vreg_info *vreg)
  139. {
  140. struct device *dev = &plat_priv->plat_dev->dev;
  141. cnss_pr_dbg("Put regulator: %s\n", vreg->cfg.name);
  142. devm_regulator_put(vreg->reg);
  143. devm_kfree(dev, vreg);
  144. }
  145. static int cnss_vreg_on_single(struct cnss_vreg_info *vreg)
  146. {
  147. int ret = 0;
  148. if (vreg->enabled) {
  149. cnss_pr_dbg("Regulator %s is already enabled\n",
  150. vreg->cfg.name);
  151. return 0;
  152. }
  153. cnss_pr_dbg("Regulator %s is being enabled\n", vreg->cfg.name);
  154. if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
  155. ret = regulator_set_voltage(vreg->reg,
  156. vreg->cfg.min_uv,
  157. vreg->cfg.max_uv);
  158. if (ret) {
  159. cnss_pr_err("Failed to set voltage for regulator %s, min_uv: %u, max_uv: %u, err = %d\n",
  160. vreg->cfg.name, vreg->cfg.min_uv,
  161. vreg->cfg.max_uv, ret);
  162. goto out;
  163. }
  164. }
  165. if (vreg->cfg.load_ua) {
  166. ret = regulator_set_load(vreg->reg,
  167. vreg->cfg.load_ua);
  168. if (ret < 0) {
  169. cnss_pr_err("Failed to set load for regulator %s, load: %u, err = %d\n",
  170. vreg->cfg.name, vreg->cfg.load_ua,
  171. ret);
  172. goto out;
  173. }
  174. }
  175. if (vreg->cfg.delay_us)
  176. udelay(vreg->cfg.delay_us);
  177. ret = regulator_enable(vreg->reg);
  178. if (ret) {
  179. cnss_pr_err("Failed to enable regulator %s, err = %d\n",
  180. vreg->cfg.name, ret);
  181. goto out;
  182. }
  183. vreg->enabled = true;
  184. out:
  185. return ret;
  186. }
  187. static int cnss_vreg_unvote_single(struct cnss_vreg_info *vreg)
  188. {
  189. int ret = 0;
  190. if (!vreg->enabled) {
  191. cnss_pr_dbg("Regulator %s is already disabled\n",
  192. vreg->cfg.name);
  193. return 0;
  194. }
  195. cnss_pr_dbg("Removing vote for Regulator %s\n", vreg->cfg.name);
  196. if (vreg->cfg.load_ua) {
  197. ret = regulator_set_load(vreg->reg, 0);
  198. if (ret < 0)
  199. cnss_pr_err("Failed to set load for regulator %s, err = %d\n",
  200. vreg->cfg.name, ret);
  201. }
  202. if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
  203. ret = regulator_set_voltage(vreg->reg, 0,
  204. vreg->cfg.max_uv);
  205. if (ret)
  206. cnss_pr_err("Failed to set voltage for regulator %s, err = %d\n",
  207. vreg->cfg.name, ret);
  208. }
  209. return ret;
  210. }
  211. static int cnss_vreg_off_single(struct cnss_vreg_info *vreg)
  212. {
  213. int ret = 0;
  214. if (!vreg->enabled) {
  215. cnss_pr_dbg("Regulator %s is already disabled\n",
  216. vreg->cfg.name);
  217. return 0;
  218. }
  219. cnss_pr_dbg("Regulator %s is being disabled\n",
  220. vreg->cfg.name);
  221. ret = regulator_disable(vreg->reg);
  222. if (ret)
  223. cnss_pr_err("Failed to disable regulator %s, err = %d\n",
  224. vreg->cfg.name, ret);
  225. if (vreg->cfg.load_ua) {
  226. ret = regulator_set_load(vreg->reg, 0);
  227. if (ret < 0)
  228. cnss_pr_err("Failed to set load for regulator %s, err = %d\n",
  229. vreg->cfg.name, ret);
  230. }
  231. if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
  232. ret = regulator_set_voltage(vreg->reg, 0,
  233. vreg->cfg.max_uv);
  234. if (ret)
  235. cnss_pr_err("Failed to set voltage for regulator %s, err = %d\n",
  236. vreg->cfg.name, ret);
  237. }
  238. vreg->enabled = false;
  239. return ret;
  240. }
  241. static struct cnss_vreg_cfg *get_vreg_list(u32 *vreg_list_size,
  242. enum cnss_vreg_type type)
  243. {
  244. switch (type) {
  245. case CNSS_VREG_PRIM:
  246. *vreg_list_size = CNSS_VREG_INFO_SIZE;
  247. return cnss_vreg_list;
  248. default:
  249. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  250. *vreg_list_size = 0;
  251. return NULL;
  252. }
  253. }
  254. static int cnss_get_vreg(struct cnss_plat_data *plat_priv,
  255. struct list_head *vreg_list,
  256. struct cnss_vreg_cfg *vreg_cfg,
  257. u32 vreg_list_size)
  258. {
  259. int ret = 0;
  260. int i;
  261. struct cnss_vreg_info *vreg;
  262. struct device *dev = &plat_priv->plat_dev->dev;
  263. if (!list_empty(vreg_list)) {
  264. cnss_pr_dbg("Vregs have already been updated\n");
  265. return 0;
  266. }
  267. for (i = 0; i < vreg_list_size; i++) {
  268. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  269. if (!vreg)
  270. return -ENOMEM;
  271. memcpy(&vreg->cfg, &vreg_cfg[i], sizeof(vreg->cfg));
  272. ret = cnss_get_vreg_single(plat_priv, vreg);
  273. if (ret != 0) {
  274. if (ret == -ENODEV) {
  275. devm_kfree(dev, vreg);
  276. continue;
  277. } else {
  278. devm_kfree(dev, vreg);
  279. return ret;
  280. }
  281. }
  282. list_add_tail(&vreg->list, vreg_list);
  283. }
  284. return 0;
  285. }
  286. static void cnss_put_vreg(struct cnss_plat_data *plat_priv,
  287. struct list_head *vreg_list)
  288. {
  289. struct cnss_vreg_info *vreg;
  290. while (!list_empty(vreg_list)) {
  291. vreg = list_first_entry(vreg_list,
  292. struct cnss_vreg_info, list);
  293. list_del(&vreg->list);
  294. if (IS_ERR_OR_NULL(vreg->reg))
  295. continue;
  296. cnss_put_vreg_single(plat_priv, vreg);
  297. }
  298. }
  299. static int cnss_vreg_on(struct cnss_plat_data *plat_priv,
  300. struct list_head *vreg_list)
  301. {
  302. struct cnss_vreg_info *vreg;
  303. int ret = 0;
  304. list_for_each_entry(vreg, vreg_list, list) {
  305. if (IS_ERR_OR_NULL(vreg->reg))
  306. continue;
  307. ret = cnss_vreg_on_single(vreg);
  308. if (ret)
  309. break;
  310. }
  311. if (!ret)
  312. return 0;
  313. list_for_each_entry_continue_reverse(vreg, vreg_list, list) {
  314. if (IS_ERR_OR_NULL(vreg->reg) || !vreg->enabled)
  315. continue;
  316. cnss_vreg_off_single(vreg);
  317. }
  318. return ret;
  319. }
  320. static int cnss_vreg_off(struct cnss_plat_data *plat_priv,
  321. struct list_head *vreg_list)
  322. {
  323. struct cnss_vreg_info *vreg;
  324. list_for_each_entry_reverse(vreg, vreg_list, list) {
  325. if (IS_ERR_OR_NULL(vreg->reg))
  326. continue;
  327. cnss_vreg_off_single(vreg);
  328. }
  329. return 0;
  330. }
  331. static int cnss_vreg_unvote(struct cnss_plat_data *plat_priv,
  332. struct list_head *vreg_list)
  333. {
  334. struct cnss_vreg_info *vreg;
  335. list_for_each_entry_reverse(vreg, vreg_list, list) {
  336. if (IS_ERR_OR_NULL(vreg->reg))
  337. continue;
  338. if (vreg->cfg.need_unvote)
  339. cnss_vreg_unvote_single(vreg);
  340. }
  341. return 0;
  342. }
  343. int cnss_get_vreg_type(struct cnss_plat_data *plat_priv,
  344. enum cnss_vreg_type type)
  345. {
  346. struct cnss_vreg_cfg *vreg_cfg;
  347. u32 vreg_list_size = 0;
  348. int ret = 0;
  349. vreg_cfg = get_vreg_list(&vreg_list_size, type);
  350. if (!vreg_cfg)
  351. return -EINVAL;
  352. switch (type) {
  353. case CNSS_VREG_PRIM:
  354. ret = cnss_get_vreg(plat_priv, &plat_priv->vreg_list,
  355. vreg_cfg, vreg_list_size);
  356. break;
  357. default:
  358. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  359. return -EINVAL;
  360. }
  361. return ret;
  362. }
  363. void cnss_put_vreg_type(struct cnss_plat_data *plat_priv,
  364. enum cnss_vreg_type type)
  365. {
  366. switch (type) {
  367. case CNSS_VREG_PRIM:
  368. cnss_put_vreg(plat_priv, &plat_priv->vreg_list);
  369. break;
  370. default:
  371. return;
  372. }
  373. }
  374. int cnss_vreg_on_type(struct cnss_plat_data *plat_priv,
  375. enum cnss_vreg_type type)
  376. {
  377. int ret = 0;
  378. switch (type) {
  379. case CNSS_VREG_PRIM:
  380. ret = cnss_vreg_on(plat_priv, &plat_priv->vreg_list);
  381. break;
  382. default:
  383. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  384. return -EINVAL;
  385. }
  386. return ret;
  387. }
  388. int cnss_vreg_off_type(struct cnss_plat_data *plat_priv,
  389. enum cnss_vreg_type type)
  390. {
  391. int ret = 0;
  392. switch (type) {
  393. case CNSS_VREG_PRIM:
  394. ret = cnss_vreg_off(plat_priv, &plat_priv->vreg_list);
  395. break;
  396. default:
  397. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  398. return -EINVAL;
  399. }
  400. return ret;
  401. }
  402. int cnss_vreg_unvote_type(struct cnss_plat_data *plat_priv,
  403. enum cnss_vreg_type type)
  404. {
  405. int ret = 0;
  406. switch (type) {
  407. case CNSS_VREG_PRIM:
  408. ret = cnss_vreg_unvote(plat_priv, &plat_priv->vreg_list);
  409. break;
  410. default:
  411. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  412. return -EINVAL;
  413. }
  414. return ret;
  415. }
  416. static int cnss_get_clk_single(struct cnss_plat_data *plat_priv,
  417. struct cnss_clk_info *clk_info)
  418. {
  419. struct device *dev = &plat_priv->plat_dev->dev;
  420. struct clk *clk;
  421. int ret;
  422. clk = devm_clk_get(dev, clk_info->cfg.name);
  423. if (IS_ERR(clk)) {
  424. ret = PTR_ERR(clk);
  425. if (clk_info->cfg.required)
  426. cnss_pr_err("Failed to get clock %s, err = %d\n",
  427. clk_info->cfg.name, ret);
  428. else
  429. cnss_pr_dbg("Failed to get optional clock %s, err = %d\n",
  430. clk_info->cfg.name, ret);
  431. return ret;
  432. }
  433. clk_info->clk = clk;
  434. cnss_pr_dbg("Got clock: %s, freq: %u\n",
  435. clk_info->cfg.name, clk_info->cfg.freq);
  436. return 0;
  437. }
  438. static void cnss_put_clk_single(struct cnss_plat_data *plat_priv,
  439. struct cnss_clk_info *clk_info)
  440. {
  441. struct device *dev = &plat_priv->plat_dev->dev;
  442. cnss_pr_dbg("Put clock: %s\n", clk_info->cfg.name);
  443. devm_clk_put(dev, clk_info->clk);
  444. }
  445. static int cnss_clk_on_single(struct cnss_clk_info *clk_info)
  446. {
  447. int ret;
  448. if (clk_info->enabled) {
  449. cnss_pr_dbg("Clock %s is already enabled\n",
  450. clk_info->cfg.name);
  451. return 0;
  452. }
  453. cnss_pr_dbg("Clock %s is being enabled\n", clk_info->cfg.name);
  454. if (clk_info->cfg.freq) {
  455. ret = clk_set_rate(clk_info->clk, clk_info->cfg.freq);
  456. if (ret) {
  457. cnss_pr_err("Failed to set frequency %u for clock %s, err = %d\n",
  458. clk_info->cfg.freq, clk_info->cfg.name,
  459. ret);
  460. return ret;
  461. }
  462. }
  463. ret = clk_prepare_enable(clk_info->clk);
  464. if (ret) {
  465. cnss_pr_err("Failed to enable clock %s, err = %d\n",
  466. clk_info->cfg.name, ret);
  467. return ret;
  468. }
  469. clk_info->enabled = true;
  470. return 0;
  471. }
  472. static int cnss_clk_off_single(struct cnss_clk_info *clk_info)
  473. {
  474. if (!clk_info->enabled) {
  475. cnss_pr_dbg("Clock %s is already disabled\n",
  476. clk_info->cfg.name);
  477. return 0;
  478. }
  479. cnss_pr_dbg("Clock %s is being disabled\n", clk_info->cfg.name);
  480. clk_disable_unprepare(clk_info->clk);
  481. clk_info->enabled = false;
  482. return 0;
  483. }
  484. int cnss_get_clk(struct cnss_plat_data *plat_priv)
  485. {
  486. struct device *dev;
  487. struct list_head *clk_list;
  488. struct cnss_clk_info *clk_info;
  489. int ret, i;
  490. if (!plat_priv)
  491. return -ENODEV;
  492. dev = &plat_priv->plat_dev->dev;
  493. clk_list = &plat_priv->clk_list;
  494. if (!list_empty(clk_list)) {
  495. cnss_pr_dbg("Clocks have already been updated\n");
  496. return 0;
  497. }
  498. for (i = 0; i < CNSS_CLK_INFO_SIZE; i++) {
  499. clk_info = devm_kzalloc(dev, sizeof(*clk_info), GFP_KERNEL);
  500. if (!clk_info) {
  501. ret = -ENOMEM;
  502. goto cleanup;
  503. }
  504. memcpy(&clk_info->cfg, &cnss_clk_list[i],
  505. sizeof(clk_info->cfg));
  506. ret = cnss_get_clk_single(plat_priv, clk_info);
  507. if (ret != 0) {
  508. if (clk_info->cfg.required) {
  509. devm_kfree(dev, clk_info);
  510. goto cleanup;
  511. } else {
  512. devm_kfree(dev, clk_info);
  513. continue;
  514. }
  515. }
  516. list_add_tail(&clk_info->list, clk_list);
  517. }
  518. return 0;
  519. cleanup:
  520. while (!list_empty(clk_list)) {
  521. clk_info = list_first_entry(clk_list, struct cnss_clk_info,
  522. list);
  523. list_del(&clk_info->list);
  524. if (IS_ERR_OR_NULL(clk_info->clk))
  525. continue;
  526. cnss_put_clk_single(plat_priv, clk_info);
  527. devm_kfree(dev, clk_info);
  528. }
  529. return ret;
  530. }
  531. void cnss_put_clk(struct cnss_plat_data *plat_priv)
  532. {
  533. struct device *dev;
  534. struct list_head *clk_list;
  535. struct cnss_clk_info *clk_info;
  536. if (!plat_priv)
  537. return;
  538. dev = &plat_priv->plat_dev->dev;
  539. clk_list = &plat_priv->clk_list;
  540. while (!list_empty(clk_list)) {
  541. clk_info = list_first_entry(clk_list, struct cnss_clk_info,
  542. list);
  543. list_del(&clk_info->list);
  544. if (IS_ERR_OR_NULL(clk_info->clk))
  545. continue;
  546. cnss_put_clk_single(plat_priv, clk_info);
  547. devm_kfree(dev, clk_info);
  548. }
  549. }
  550. static int cnss_clk_on(struct cnss_plat_data *plat_priv,
  551. struct list_head *clk_list)
  552. {
  553. struct cnss_clk_info *clk_info;
  554. int ret = 0;
  555. list_for_each_entry(clk_info, clk_list, list) {
  556. if (IS_ERR_OR_NULL(clk_info->clk))
  557. continue;
  558. ret = cnss_clk_on_single(clk_info);
  559. if (ret)
  560. break;
  561. }
  562. if (!ret)
  563. return 0;
  564. list_for_each_entry_continue_reverse(clk_info, clk_list, list) {
  565. if (IS_ERR_OR_NULL(clk_info->clk))
  566. continue;
  567. cnss_clk_off_single(clk_info);
  568. }
  569. return ret;
  570. }
  571. static int cnss_clk_off(struct cnss_plat_data *plat_priv,
  572. struct list_head *clk_list)
  573. {
  574. struct cnss_clk_info *clk_info;
  575. list_for_each_entry_reverse(clk_info, clk_list, list) {
  576. if (IS_ERR_OR_NULL(clk_info->clk))
  577. continue;
  578. cnss_clk_off_single(clk_info);
  579. }
  580. return 0;
  581. }
  582. int cnss_get_pinctrl(struct cnss_plat_data *plat_priv)
  583. {
  584. int ret = 0;
  585. struct device *dev;
  586. struct cnss_pinctrl_info *pinctrl_info;
  587. dev = &plat_priv->plat_dev->dev;
  588. pinctrl_info = &plat_priv->pinctrl_info;
  589. pinctrl_info->pinctrl = devm_pinctrl_get(dev);
  590. if (IS_ERR_OR_NULL(pinctrl_info->pinctrl)) {
  591. ret = PTR_ERR(pinctrl_info->pinctrl);
  592. cnss_pr_err("Failed to get pinctrl, err = %d\n", ret);
  593. goto out;
  594. }
  595. if (of_find_property(dev->of_node, BOOTSTRAP_GPIO, NULL)) {
  596. pinctrl_info->bootstrap_active =
  597. pinctrl_lookup_state(pinctrl_info->pinctrl,
  598. BOOTSTRAP_ACTIVE);
  599. if (IS_ERR_OR_NULL(pinctrl_info->bootstrap_active)) {
  600. ret = PTR_ERR(pinctrl_info->bootstrap_active);
  601. cnss_pr_err("Failed to get bootstrap active state, err = %d\n",
  602. ret);
  603. goto out;
  604. }
  605. }
  606. if (of_find_property(dev->of_node, HOST_SOL_GPIO, NULL) &&
  607. of_find_property(dev->of_node, DEV_SOL_GPIO, NULL)) {
  608. pinctrl_info->sol_default =
  609. pinctrl_lookup_state(pinctrl_info->pinctrl,
  610. SOL_DEFAULT);
  611. if (IS_ERR_OR_NULL(pinctrl_info->sol_default)) {
  612. ret = PTR_ERR(pinctrl_info->sol_default);
  613. cnss_pr_err("Failed to get sol default state, err = %d\n",
  614. ret);
  615. goto out;
  616. }
  617. cnss_pr_dbg("Got sol default state\n");
  618. }
  619. if (of_find_property(dev->of_node, WLAN_EN_GPIO, NULL)) {
  620. pinctrl_info->wlan_en_active =
  621. pinctrl_lookup_state(pinctrl_info->pinctrl,
  622. WLAN_EN_ACTIVE);
  623. if (IS_ERR_OR_NULL(pinctrl_info->wlan_en_active)) {
  624. ret = PTR_ERR(pinctrl_info->wlan_en_active);
  625. cnss_pr_err("Failed to get wlan_en active state, err = %d\n",
  626. ret);
  627. goto out;
  628. }
  629. pinctrl_info->wlan_en_sleep =
  630. pinctrl_lookup_state(pinctrl_info->pinctrl,
  631. WLAN_EN_SLEEP);
  632. if (IS_ERR_OR_NULL(pinctrl_info->wlan_en_sleep)) {
  633. ret = PTR_ERR(pinctrl_info->wlan_en_sleep);
  634. cnss_pr_err("Failed to get wlan_en sleep state, err = %d\n",
  635. ret);
  636. goto out;
  637. }
  638. }
  639. /* Added for QCA6490 PMU delayed WLAN_EN_GPIO */
  640. if (of_find_property(dev->of_node, BT_EN_GPIO, NULL)) {
  641. pinctrl_info->bt_en_gpio = of_get_named_gpio(dev->of_node,
  642. BT_EN_GPIO, 0);
  643. cnss_pr_dbg("BT GPIO: %d\n", pinctrl_info->bt_en_gpio);
  644. } else {
  645. pinctrl_info->bt_en_gpio = -EINVAL;
  646. }
  647. /* Added for QCA6490 to minimize XO CLK selection leakage prevention */
  648. if (of_find_property(dev->of_node, XO_CLK_GPIO, NULL)) {
  649. pinctrl_info->xo_clk_gpio = of_get_named_gpio(dev->of_node,
  650. XO_CLK_GPIO, 0);
  651. cnss_pr_dbg("QCA6490 XO_CLK GPIO: %d\n",
  652. pinctrl_info->xo_clk_gpio);
  653. cnss_set_feature_list(plat_priv, BOOTSTRAP_CLOCK_SELECT_V01);
  654. } else {
  655. pinctrl_info->xo_clk_gpio = -EINVAL;
  656. }
  657. return 0;
  658. out:
  659. return ret;
  660. }
  661. #define CNSS_XO_CLK_RETRY_COUNT_MAX 5
  662. static void cnss_set_xo_clk_gpio_state(struct cnss_plat_data *plat_priv,
  663. bool enable)
  664. {
  665. int xo_clk_gpio = plat_priv->pinctrl_info.xo_clk_gpio, retry = 0, ret;
  666. if (xo_clk_gpio < 0 || plat_priv->device_id != QCA6490_DEVICE_ID)
  667. return;
  668. retry_gpio_req:
  669. ret = gpio_request(xo_clk_gpio, "XO_CLK_GPIO");
  670. if (ret) {
  671. if (retry++ < CNSS_XO_CLK_RETRY_COUNT_MAX) {
  672. /* wait for ~(10 - 20) ms */
  673. usleep_range(10000, 20000);
  674. goto retry_gpio_req;
  675. }
  676. }
  677. if (ret) {
  678. cnss_pr_err("QCA6490 XO CLK Gpio request failed\n");
  679. return;
  680. }
  681. if (enable) {
  682. gpio_direction_output(xo_clk_gpio, 1);
  683. /*XO CLK must be asserted for some time before WLAN_EN */
  684. usleep_range(100, 200);
  685. } else {
  686. /* Assert XO CLK ~(2-5)ms before off for valid latch in HW */
  687. usleep_range(2000, 5000);
  688. gpio_direction_output(xo_clk_gpio, 0);
  689. }
  690. gpio_free(xo_clk_gpio);
  691. }
  692. static int cnss_select_pinctrl_state(struct cnss_plat_data *plat_priv,
  693. bool state)
  694. {
  695. int ret = 0;
  696. struct cnss_pinctrl_info *pinctrl_info;
  697. if (!plat_priv) {
  698. cnss_pr_err("plat_priv is NULL!\n");
  699. ret = -ENODEV;
  700. goto out;
  701. }
  702. pinctrl_info = &plat_priv->pinctrl_info;
  703. if (state) {
  704. if (!IS_ERR_OR_NULL(pinctrl_info->bootstrap_active)) {
  705. ret = pinctrl_select_state
  706. (pinctrl_info->pinctrl,
  707. pinctrl_info->bootstrap_active);
  708. if (ret) {
  709. cnss_pr_err("Failed to select bootstrap active state, err = %d\n",
  710. ret);
  711. goto out;
  712. }
  713. udelay(BOOTSTRAP_DELAY);
  714. }
  715. if (!IS_ERR_OR_NULL(pinctrl_info->sol_default)) {
  716. ret = pinctrl_select_state
  717. (pinctrl_info->pinctrl,
  718. pinctrl_info->sol_default);
  719. if (ret) {
  720. cnss_pr_err("Failed to select sol default state, err = %d\n",
  721. ret);
  722. goto out;
  723. }
  724. cnss_pr_dbg("Selected sol default state\n");
  725. }
  726. cnss_set_xo_clk_gpio_state(plat_priv, true);
  727. if (!IS_ERR_OR_NULL(pinctrl_info->wlan_en_active)) {
  728. ret = pinctrl_select_state
  729. (pinctrl_info->pinctrl,
  730. pinctrl_info->wlan_en_active);
  731. if (ret) {
  732. cnss_pr_err("Failed to select wlan_en active state, err = %d\n",
  733. ret);
  734. goto out;
  735. }
  736. udelay(WLAN_ENABLE_DELAY);
  737. }
  738. cnss_set_xo_clk_gpio_state(plat_priv, false);
  739. } else {
  740. if (!IS_ERR_OR_NULL(pinctrl_info->wlan_en_sleep)) {
  741. ret = pinctrl_select_state(pinctrl_info->pinctrl,
  742. pinctrl_info->wlan_en_sleep);
  743. if (ret) {
  744. cnss_pr_err("Failed to select wlan_en sleep state, err = %d\n",
  745. ret);
  746. goto out;
  747. }
  748. }
  749. }
  750. cnss_pr_dbg("%s WLAN_EN GPIO successfully\n",
  751. state ? "Assert" : "De-assert");
  752. return 0;
  753. out:
  754. return ret;
  755. }
  756. /**
  757. * cnss_select_pinctrl_enable - select WLAN_GPIO for Active pinctrl status
  758. * @plat_priv: Platform private data structure pointer
  759. *
  760. * For QCA6490, PMU requires minimum 100ms delay between BT_EN_GPIO off and
  761. * WLAN_EN_GPIO on. This is done to avoid power up issues.
  762. *
  763. * Return: Status of pinctrl select operation. 0 - Success.
  764. */
  765. static int cnss_select_pinctrl_enable(struct cnss_plat_data *plat_priv)
  766. {
  767. int ret = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  768. u8 wlan_en_state = 0;
  769. if (bt_en_gpio < 0 || plat_priv->device_id != QCA6490_DEVICE_ID)
  770. goto set_wlan_en;
  771. if (gpio_get_value(bt_en_gpio)) {
  772. cnss_pr_dbg("BT_EN_GPIO State: On\n");
  773. ret = cnss_select_pinctrl_state(plat_priv, true);
  774. if (!ret)
  775. return ret;
  776. wlan_en_state = 1;
  777. }
  778. if (!gpio_get_value(bt_en_gpio)) {
  779. cnss_pr_dbg("BT_EN_GPIO State: Off. Delay WLAN_GPIO enable\n");
  780. /* check for BT_EN_GPIO down race during above operation */
  781. if (wlan_en_state) {
  782. cnss_pr_dbg("Reset WLAN_EN as BT got turned off during enable\n");
  783. cnss_select_pinctrl_state(plat_priv, false);
  784. wlan_en_state = 0;
  785. }
  786. /* 100 ms delay for BT_EN and WLAN_EN QCA6490 PMU sequencing */
  787. msleep(100);
  788. }
  789. set_wlan_en:
  790. if (!wlan_en_state)
  791. ret = cnss_select_pinctrl_state(plat_priv, true);
  792. return ret;
  793. }
  794. int cnss_power_on_device(struct cnss_plat_data *plat_priv)
  795. {
  796. int ret = 0;
  797. if (plat_priv->powered_on) {
  798. cnss_pr_dbg("Already powered up");
  799. return 0;
  800. }
  801. ret = cnss_vreg_on_type(plat_priv, CNSS_VREG_PRIM);
  802. if (ret) {
  803. cnss_pr_err("Failed to turn on vreg, err = %d\n", ret);
  804. goto out;
  805. }
  806. ret = cnss_clk_on(plat_priv, &plat_priv->clk_list);
  807. if (ret) {
  808. cnss_pr_err("Failed to turn on clocks, err = %d\n", ret);
  809. goto vreg_off;
  810. }
  811. ret = cnss_select_pinctrl_enable(plat_priv);
  812. if (ret) {
  813. cnss_pr_err("Failed to select pinctrl state, err = %d\n", ret);
  814. goto clk_off;
  815. }
  816. plat_priv->powered_on = true;
  817. cnss_enable_dev_sol_irq(plat_priv);
  818. cnss_set_host_sol_value(plat_priv, 0);
  819. return 0;
  820. clk_off:
  821. cnss_clk_off(plat_priv, &plat_priv->clk_list);
  822. vreg_off:
  823. cnss_vreg_off_type(plat_priv, CNSS_VREG_PRIM);
  824. out:
  825. return ret;
  826. }
  827. void cnss_power_off_device(struct cnss_plat_data *plat_priv)
  828. {
  829. if (!plat_priv->powered_on) {
  830. cnss_pr_dbg("Already powered down");
  831. return;
  832. }
  833. cnss_disable_dev_sol_irq(plat_priv);
  834. cnss_select_pinctrl_state(plat_priv, false);
  835. cnss_clk_off(plat_priv, &plat_priv->clk_list);
  836. cnss_vreg_off_type(plat_priv, CNSS_VREG_PRIM);
  837. plat_priv->powered_on = false;
  838. }
  839. bool cnss_is_device_powered_on(struct cnss_plat_data *plat_priv)
  840. {
  841. return plat_priv->powered_on;
  842. }
  843. void cnss_set_pin_connect_status(struct cnss_plat_data *plat_priv)
  844. {
  845. unsigned long pin_status = 0;
  846. set_bit(CNSS_WLAN_EN, &pin_status);
  847. set_bit(CNSS_PCIE_TXN, &pin_status);
  848. set_bit(CNSS_PCIE_TXP, &pin_status);
  849. set_bit(CNSS_PCIE_RXN, &pin_status);
  850. set_bit(CNSS_PCIE_RXP, &pin_status);
  851. set_bit(CNSS_PCIE_REFCLKN, &pin_status);
  852. set_bit(CNSS_PCIE_REFCLKP, &pin_status);
  853. set_bit(CNSS_PCIE_RST, &pin_status);
  854. plat_priv->pin_result.host_pin_result = pin_status;
  855. }
  856. #if IS_ENABLED(CONFIG_QCOM_COMMAND_DB)
  857. static int cnss_cmd_db_ready(struct cnss_plat_data *plat_priv)
  858. {
  859. return cmd_db_ready();
  860. }
  861. static u32 cnss_cmd_db_read_addr(struct cnss_plat_data *plat_priv,
  862. const char *res_id)
  863. {
  864. return cmd_db_read_addr(res_id);
  865. }
  866. #else
  867. static int cnss_cmd_db_ready(struct cnss_plat_data *plat_priv)
  868. {
  869. return -EOPNOTSUPP;
  870. }
  871. static u32 cnss_cmd_db_read_addr(struct cnss_plat_data *plat_priv,
  872. const char *res_id)
  873. {
  874. return 0;
  875. }
  876. #endif
  877. int cnss_get_tcs_info(struct cnss_plat_data *plat_priv)
  878. {
  879. struct platform_device *plat_dev = plat_priv->plat_dev;
  880. struct resource *res;
  881. resource_size_t addr_len;
  882. void __iomem *tcs_cmd_base_addr;
  883. int ret = 0;
  884. res = platform_get_resource_byname(plat_dev, IORESOURCE_MEM, "tcs_cmd");
  885. if (!res) {
  886. cnss_pr_dbg("TCS CMD address is not present for CPR\n");
  887. goto out;
  888. }
  889. plat_priv->tcs_info.cmd_base_addr = res->start;
  890. addr_len = resource_size(res);
  891. cnss_pr_dbg("TCS CMD base address is %pa with length %pa\n",
  892. &plat_priv->tcs_info.cmd_base_addr, &addr_len);
  893. tcs_cmd_base_addr = devm_ioremap(&plat_dev->dev, res->start, addr_len);
  894. if (!tcs_cmd_base_addr) {
  895. ret = -EINVAL;
  896. cnss_pr_err("Failed to map TCS CMD address, err = %d\n",
  897. ret);
  898. goto out;
  899. }
  900. plat_priv->tcs_info.cmd_base_addr_io = tcs_cmd_base_addr;
  901. return 0;
  902. out:
  903. return ret;
  904. }
  905. int cnss_get_cpr_info(struct cnss_plat_data *plat_priv)
  906. {
  907. struct platform_device *plat_dev = plat_priv->plat_dev;
  908. struct cnss_cpr_info *cpr_info = &plat_priv->cpr_info;
  909. const char *cmd_db_name;
  910. u32 cpr_pmic_addr = 0;
  911. int ret = 0;
  912. if (plat_priv->tcs_info.cmd_base_addr == 0) {
  913. cnss_pr_dbg("TCS CMD not configured\n");
  914. return 0;
  915. }
  916. ret = of_property_read_string(plat_dev->dev.of_node,
  917. "qcom,cmd_db_name", &cmd_db_name);
  918. if (ret) {
  919. cnss_pr_dbg("CommandDB name is not present for CPR\n");
  920. goto out;
  921. }
  922. ret = cnss_cmd_db_ready(plat_priv);
  923. if (ret) {
  924. cnss_pr_err("CommandDB is not ready, err = %d\n", ret);
  925. goto out;
  926. }
  927. cpr_pmic_addr = cnss_cmd_db_read_addr(plat_priv, cmd_db_name);
  928. if (cpr_pmic_addr > 0) {
  929. cpr_info->cpr_pmic_addr = cpr_pmic_addr;
  930. cnss_pr_dbg("Get CPR PMIC address 0x%x from %s\n",
  931. cpr_info->cpr_pmic_addr, cmd_db_name);
  932. } else {
  933. cnss_pr_err("CPR PMIC address is not available for %s\n",
  934. cmd_db_name);
  935. ret = -EINVAL;
  936. goto out;
  937. }
  938. return 0;
  939. out:
  940. return ret;
  941. }
  942. int cnss_aop_mbox_init(struct cnss_plat_data *plat_priv)
  943. {
  944. struct mbox_client *mbox = &plat_priv->mbox_client_data;
  945. struct mbox_chan *chan;
  946. int ret = 0;
  947. mbox->dev = &plat_priv->plat_dev->dev;
  948. mbox->tx_block = true;
  949. mbox->tx_tout = CNSS_MBOX_TIMEOUT_MS;
  950. mbox->knows_txdone = false;
  951. plat_priv->mbox_chan = NULL;
  952. ret = of_property_read_string(plat_priv->plat_dev->dev.of_node,
  953. "qcom,vreg_ol_cpr",
  954. &plat_priv->vreg_ol_cpr);
  955. if (ret)
  956. cnss_pr_dbg("Vreg for OL CPR not configured\n");
  957. ret = of_property_read_string(plat_priv->plat_dev->dev.of_node,
  958. "qcom,vreg_ipa",
  959. &plat_priv->vreg_ipa);
  960. if (ret)
  961. cnss_pr_dbg("Volt regulator for Int Power Amp not configured\n");
  962. if (!plat_priv->vreg_ol_cpr && !plat_priv->vreg_ipa)
  963. return 0;
  964. chan = mbox_request_channel(mbox, 0);
  965. if (IS_ERR(chan)) {
  966. cnss_pr_err("Failed to get mbox channel\n");
  967. return PTR_ERR(chan);
  968. }
  969. plat_priv->mbox_chan = chan;
  970. cnss_pr_dbg("Mbox channel initialized\n");
  971. return 0;
  972. }
  973. #if IS_ENABLED(CONFIG_MSM_QMP)
  974. static int cnss_aop_set_vreg_param(struct cnss_plat_data *plat_priv,
  975. const char *vreg_name,
  976. enum cnss_vreg_param param,
  977. enum cnss_tcs_seq seq, int val)
  978. {
  979. struct qmp_pkt pkt;
  980. char mbox_msg[CNSS_MBOX_MSG_MAX_LEN];
  981. static const char * const vreg_param_str[] = {"v", "m", "e"};
  982. static const char *const tcs_seq_str[] = {"upval", "dwnval", "enable"};
  983. int ret = 0;
  984. if (param > CNSS_VREG_ENABLE || seq > CNSS_TCS_ALL_SEQ || !vreg_name)
  985. return -EINVAL;
  986. snprintf(mbox_msg, CNSS_MBOX_MSG_MAX_LEN,
  987. "{class: wlan_pdc, res: %s.%s, %s: %d}", vreg_name,
  988. vreg_param_str[param], tcs_seq_str[seq], val);
  989. cnss_pr_dbg("Sending AOP Mbox msg: %s\n", mbox_msg);
  990. pkt.size = CNSS_MBOX_MSG_MAX_LEN;
  991. pkt.data = mbox_msg;
  992. ret = mbox_send_message(plat_priv->mbox_chan, &pkt);
  993. if (ret < 0)
  994. cnss_pr_err("Failed to send AOP mbox msg: %s\n", mbox_msg);
  995. else
  996. ret = 0;
  997. return ret;
  998. }
  999. #else
  1000. static int cnss_aop_set_vreg_param(struct cnss_plat_data *plat_priv,
  1001. const char *vreg_name,
  1002. enum cnss_vreg_param param,
  1003. enum cnss_tcs_seq seq, int val)
  1004. {
  1005. return 0;
  1006. }
  1007. #endif
  1008. int cnss_update_cpr_info(struct cnss_plat_data *plat_priv)
  1009. {
  1010. struct cnss_cpr_info *cpr_info = &plat_priv->cpr_info;
  1011. u32 pmic_addr, voltage = 0, voltage_tmp, offset;
  1012. void __iomem *tcs_cmd_addr, *tcs_cmd_data_addr;
  1013. int i, j;
  1014. if (cpr_info->voltage == 0) {
  1015. cnss_pr_err("OL CPR Voltage %dm is not valid\n",
  1016. cpr_info->voltage);
  1017. return -EINVAL;
  1018. }
  1019. if (!plat_priv->vreg_ol_cpr || !plat_priv->mbox_chan) {
  1020. cnss_pr_dbg("Mbox channel / OL CPR Vreg not configured\n");
  1021. } else {
  1022. return cnss_aop_set_vreg_param(plat_priv,
  1023. plat_priv->vreg_ol_cpr,
  1024. CNSS_VREG_VOLTAGE,
  1025. CNSS_TCS_UP_SEQ,
  1026. cpr_info->voltage);
  1027. }
  1028. if (plat_priv->tcs_info.cmd_base_addr == 0) {
  1029. cnss_pr_dbg("TCS CMD not configured for OL CPR update\n");
  1030. return 0;
  1031. }
  1032. if (cpr_info->cpr_pmic_addr == 0) {
  1033. cnss_pr_err("PMIC address 0x%x is not valid\n",
  1034. cpr_info->cpr_pmic_addr);
  1035. return -EINVAL;
  1036. }
  1037. if (cpr_info->tcs_cmd_data_addr_io)
  1038. goto update_cpr;
  1039. for (i = 0; i < MAX_TCS_NUM; i++) {
  1040. for (j = 0; j < MAX_TCS_CMD_NUM; j++) {
  1041. offset = i * TCS_OFFSET + j * TCS_CMD_OFFSET;
  1042. tcs_cmd_addr = plat_priv->tcs_info.cmd_base_addr_io +
  1043. offset;
  1044. pmic_addr = readl_relaxed(tcs_cmd_addr);
  1045. if (pmic_addr == cpr_info->cpr_pmic_addr) {
  1046. tcs_cmd_data_addr = tcs_cmd_addr +
  1047. TCS_CMD_DATA_ADDR_OFFSET;
  1048. voltage_tmp = readl_relaxed(tcs_cmd_data_addr);
  1049. cnss_pr_dbg("Got voltage %dmV from i: %d, j: %d\n",
  1050. voltage_tmp, i, j);
  1051. if (voltage_tmp > voltage) {
  1052. voltage = voltage_tmp;
  1053. cpr_info->tcs_cmd_data_addr =
  1054. plat_priv->tcs_info.cmd_base_addr +
  1055. offset + TCS_CMD_DATA_ADDR_OFFSET;
  1056. cpr_info->tcs_cmd_data_addr_io =
  1057. tcs_cmd_data_addr;
  1058. }
  1059. }
  1060. }
  1061. }
  1062. if (!cpr_info->tcs_cmd_data_addr_io) {
  1063. cnss_pr_err("Failed to find proper TCS CMD data address\n");
  1064. return -EINVAL;
  1065. }
  1066. update_cpr:
  1067. cpr_info->voltage = cpr_info->voltage > BT_CXMX_VOLTAGE_MV ?
  1068. cpr_info->voltage : BT_CXMX_VOLTAGE_MV;
  1069. cnss_pr_dbg("Update TCS CMD data address %pa with voltage %dmV\n",
  1070. &cpr_info->tcs_cmd_data_addr, cpr_info->voltage);
  1071. writel_relaxed(cpr_info->voltage, cpr_info->tcs_cmd_data_addr_io);
  1072. return 0;
  1073. }
  1074. int cnss_enable_int_pow_amp_vreg(struct cnss_plat_data *plat_priv)
  1075. {
  1076. struct platform_device *plat_dev = plat_priv->plat_dev;
  1077. u32 offset, addr_val, data_val;
  1078. void __iomem *tcs_cmd;
  1079. int ret;
  1080. static bool config_done;
  1081. if (plat_priv->device_id != QCA6490_DEVICE_ID)
  1082. return -EINVAL;
  1083. if (config_done) {
  1084. cnss_pr_dbg("IPA Vreg already configured\n");
  1085. return 0;
  1086. }
  1087. if (!plat_priv->vreg_ipa || !plat_priv->mbox_chan) {
  1088. cnss_pr_dbg("Mbox channel / IPA Vreg not configured\n");
  1089. } else {
  1090. ret = cnss_aop_set_vreg_param(plat_priv,
  1091. plat_priv->vreg_ipa,
  1092. CNSS_VREG_ENABLE,
  1093. CNSS_TCS_UP_SEQ, 1);
  1094. if (ret == 0)
  1095. config_done = true;
  1096. return ret;
  1097. }
  1098. if (!plat_priv->tcs_info.cmd_base_addr_io) {
  1099. cnss_pr_err("TCS CMD not configured for IPA Vreg enable\n");
  1100. return -EINVAL;
  1101. }
  1102. ret = of_property_read_u32(plat_dev->dev.of_node,
  1103. "qcom,tcs_offset_int_pow_amp_vreg",
  1104. &offset);
  1105. if (ret) {
  1106. cnss_pr_dbg("Internal Power Amp Vreg not configured\n");
  1107. return -EINVAL;
  1108. }
  1109. tcs_cmd = plat_priv->tcs_info.cmd_base_addr_io + offset;
  1110. addr_val = readl_relaxed(tcs_cmd);
  1111. tcs_cmd += TCS_CMD_DATA_ADDR_OFFSET;
  1112. /* 1 = enable Vreg */
  1113. writel_relaxed(1, tcs_cmd);
  1114. data_val = readl_relaxed(tcs_cmd);
  1115. cnss_pr_dbg("Setup S3E TCS Addr: %x Data: %d\n", addr_val, data_val);
  1116. config_done = true;
  1117. return 0;
  1118. }