power.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2022 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. {"vdd-wlan-rfa3", 1900000, 1900000, 450000, 0, 0},
  38. {"alt-sleep-clk", 0, 0, 0, 0, 0},
  39. {"vdd-wlan-en", 0, 0, 0, 10, 0},
  40. };
  41. static struct cnss_clk_cfg cnss_clk_list[] = {
  42. {"rf_clk", 0, 0},
  43. };
  44. #else
  45. static struct cnss_vreg_cfg cnss_vreg_list[] = {
  46. };
  47. static struct cnss_clk_cfg cnss_clk_list[] = {
  48. };
  49. #endif
  50. #define CNSS_VREG_INFO_SIZE ARRAY_SIZE(cnss_vreg_list)
  51. #define CNSS_CLK_INFO_SIZE ARRAY_SIZE(cnss_clk_list)
  52. #define MAX_PROP_SIZE 32
  53. #define BOOTSTRAP_GPIO "qcom,enable-bootstrap-gpio"
  54. #define BOOTSTRAP_ACTIVE "bootstrap_active"
  55. #define HOST_SOL_GPIO "wlan-host-sol-gpio"
  56. #define DEV_SOL_GPIO "wlan-dev-sol-gpio"
  57. #define SOL_DEFAULT "sol_default"
  58. #define WLAN_EN_GPIO "wlan-en-gpio"
  59. #define BT_EN_GPIO "qcom,bt-en-gpio"
  60. #define XO_CLK_GPIO "qcom,xo-clk-gpio"
  61. #define SW_CTRL_GPIO "qcom,sw-ctrl-gpio"
  62. #define WLAN_SW_CTRL_GPIO "qcom,wlan-sw-ctrl-gpio"
  63. #define WLAN_EN_ACTIVE "wlan_en_active"
  64. #define WLAN_EN_SLEEP "wlan_en_sleep"
  65. #define BOOTSTRAP_DELAY 1000
  66. #define WLAN_ENABLE_DELAY 1000
  67. #define TCS_CMD_DATA_ADDR_OFFSET 0x4
  68. #define TCS_OFFSET 0xC8
  69. #define TCS_CMD_OFFSET 0x10
  70. #define MAX_TCS_NUM 8
  71. #define MAX_TCS_CMD_NUM 5
  72. #define BT_CXMX_VOLTAGE_MV 950
  73. #define CNSS_MBOX_MSG_MAX_LEN 64
  74. #define CNSS_MBOX_TIMEOUT_MS 1000
  75. /* Platform HW config */
  76. #define CNSS_PMIC_VOLTAGE_STEP 4
  77. #define CNSS_PMIC_AUTO_HEADROOM 16
  78. #define CNSS_IR_DROP_WAKE 30
  79. #define CNSS_IR_DROP_SLEEP 10
  80. /**
  81. * enum cnss_aop_vreg_param: Voltage regulator TCS param
  82. * @CNSS_VREG_VOLTAGE: Provides voltage level in mV to be configured in TCS
  83. * @CNSS_VREG_MODE: Regulator mode
  84. * @CNSS_VREG_TCS_ENABLE: Set bool Voltage regulator enable config in TCS.
  85. */
  86. enum cnss_aop_vreg_param {
  87. CNSS_VREG_VOLTAGE,
  88. CNSS_VREG_MODE,
  89. CNSS_VREG_ENABLE,
  90. CNSS_VREG_PARAM_MAX
  91. };
  92. /** enum cnss_aop_vreg_param_mode: Voltage modes supported by AOP*/
  93. enum cnss_aop_vreg_param_mode {
  94. CNSS_VREG_RET_MODE = 3,
  95. CNSS_VREG_LPM_MODE = 4,
  96. CNSS_VREG_AUTO_MODE = 6,
  97. CNSS_VREG_NPM_MODE = 7,
  98. CNSS_VREG_MODE_MAX
  99. };
  100. /**
  101. * enum cnss_aop_tcs_seq: TCS sequence ID for trigger
  102. * @CNSS_TCS_UP_SEQ: TCS Sequence based on up trigger / Wake TCS
  103. * @CNSS_TCS_DOWN_SEQ: TCS Sequence based on down trigger / Sleep TCS
  104. * @CNSS_TCS_ENABLE_SEQ: Enable this TCS seq entry
  105. */
  106. enum cnss_aop_tcs_seq_param {
  107. CNSS_TCS_UP_SEQ,
  108. CNSS_TCS_DOWN_SEQ,
  109. CNSS_TCS_ENABLE_SEQ,
  110. CNSS_TCS_SEQ_MAX
  111. };
  112. static int cnss_get_vreg_single(struct cnss_plat_data *plat_priv,
  113. struct cnss_vreg_info *vreg)
  114. {
  115. int ret = 0;
  116. struct device *dev;
  117. struct regulator *reg;
  118. const __be32 *prop;
  119. char prop_name[MAX_PROP_SIZE] = {0};
  120. int len;
  121. dev = &plat_priv->plat_dev->dev;
  122. reg = devm_regulator_get_optional(dev, vreg->cfg.name);
  123. if (IS_ERR(reg)) {
  124. ret = PTR_ERR(reg);
  125. if (ret == -ENODEV)
  126. return ret;
  127. else if (ret == -EPROBE_DEFER)
  128. cnss_pr_info("EPROBE_DEFER for regulator: %s\n",
  129. vreg->cfg.name);
  130. else
  131. cnss_pr_err("Failed to get regulator %s, err = %d\n",
  132. vreg->cfg.name, ret);
  133. return ret;
  134. }
  135. vreg->reg = reg;
  136. snprintf(prop_name, MAX_PROP_SIZE, "qcom,%s-config",
  137. vreg->cfg.name);
  138. prop = of_get_property(dev->of_node, prop_name, &len);
  139. if (!prop || len != (5 * sizeof(__be32))) {
  140. cnss_pr_dbg("Property %s %s, use default\n", prop_name,
  141. prop ? "invalid format" : "doesn't exist");
  142. } else {
  143. vreg->cfg.min_uv = be32_to_cpup(&prop[0]);
  144. vreg->cfg.max_uv = be32_to_cpup(&prop[1]);
  145. vreg->cfg.load_ua = be32_to_cpup(&prop[2]);
  146. vreg->cfg.delay_us = be32_to_cpup(&prop[3]);
  147. vreg->cfg.need_unvote = be32_to_cpup(&prop[4]);
  148. }
  149. cnss_pr_dbg("Got regulator: %s, min_uv: %u, max_uv: %u, load_ua: %u, delay_us: %u, need_unvote: %u\n",
  150. vreg->cfg.name, vreg->cfg.min_uv,
  151. vreg->cfg.max_uv, vreg->cfg.load_ua,
  152. vreg->cfg.delay_us, vreg->cfg.need_unvote);
  153. return 0;
  154. }
  155. static void cnss_put_vreg_single(struct cnss_plat_data *plat_priv,
  156. struct cnss_vreg_info *vreg)
  157. {
  158. struct device *dev = &plat_priv->plat_dev->dev;
  159. cnss_pr_dbg("Put regulator: %s\n", vreg->cfg.name);
  160. devm_regulator_put(vreg->reg);
  161. devm_kfree(dev, vreg);
  162. }
  163. static int cnss_vreg_on_single(struct cnss_vreg_info *vreg)
  164. {
  165. int ret = 0;
  166. if (vreg->enabled) {
  167. cnss_pr_dbg("Regulator %s is already enabled\n",
  168. vreg->cfg.name);
  169. return 0;
  170. }
  171. cnss_pr_dbg("Regulator %s is being enabled\n", vreg->cfg.name);
  172. if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
  173. ret = regulator_set_voltage(vreg->reg,
  174. vreg->cfg.min_uv,
  175. vreg->cfg.max_uv);
  176. if (ret) {
  177. cnss_pr_err("Failed to set voltage for regulator %s, min_uv: %u, max_uv: %u, err = %d\n",
  178. vreg->cfg.name, vreg->cfg.min_uv,
  179. vreg->cfg.max_uv, ret);
  180. goto out;
  181. }
  182. }
  183. if (vreg->cfg.load_ua) {
  184. ret = regulator_set_load(vreg->reg,
  185. vreg->cfg.load_ua);
  186. if (ret < 0) {
  187. cnss_pr_err("Failed to set load for regulator %s, load: %u, err = %d\n",
  188. vreg->cfg.name, vreg->cfg.load_ua,
  189. ret);
  190. goto out;
  191. }
  192. }
  193. if (vreg->cfg.delay_us)
  194. udelay(vreg->cfg.delay_us);
  195. ret = regulator_enable(vreg->reg);
  196. if (ret) {
  197. cnss_pr_err("Failed to enable regulator %s, err = %d\n",
  198. vreg->cfg.name, ret);
  199. goto out;
  200. }
  201. vreg->enabled = true;
  202. out:
  203. return ret;
  204. }
  205. static int cnss_vreg_unvote_single(struct cnss_vreg_info *vreg)
  206. {
  207. int ret = 0;
  208. if (!vreg->enabled) {
  209. cnss_pr_dbg("Regulator %s is already disabled\n",
  210. vreg->cfg.name);
  211. return 0;
  212. }
  213. cnss_pr_dbg("Removing vote for Regulator %s\n", vreg->cfg.name);
  214. if (vreg->cfg.load_ua) {
  215. ret = regulator_set_load(vreg->reg, 0);
  216. if (ret < 0)
  217. cnss_pr_err("Failed to set load for regulator %s, err = %d\n",
  218. vreg->cfg.name, ret);
  219. }
  220. if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
  221. ret = regulator_set_voltage(vreg->reg, 0,
  222. vreg->cfg.max_uv);
  223. if (ret)
  224. cnss_pr_err("Failed to set voltage for regulator %s, err = %d\n",
  225. vreg->cfg.name, ret);
  226. }
  227. return ret;
  228. }
  229. static int cnss_vreg_off_single(struct cnss_vreg_info *vreg)
  230. {
  231. int ret = 0;
  232. if (!vreg->enabled) {
  233. cnss_pr_dbg("Regulator %s is already disabled\n",
  234. vreg->cfg.name);
  235. return 0;
  236. }
  237. cnss_pr_dbg("Regulator %s is being disabled\n",
  238. vreg->cfg.name);
  239. ret = regulator_disable(vreg->reg);
  240. if (ret)
  241. cnss_pr_err("Failed to disable regulator %s, err = %d\n",
  242. vreg->cfg.name, ret);
  243. if (vreg->cfg.load_ua) {
  244. ret = regulator_set_load(vreg->reg, 0);
  245. if (ret < 0)
  246. cnss_pr_err("Failed to set load for regulator %s, err = %d\n",
  247. vreg->cfg.name, ret);
  248. }
  249. if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
  250. ret = regulator_set_voltage(vreg->reg, 0,
  251. vreg->cfg.max_uv);
  252. if (ret)
  253. cnss_pr_err("Failed to set voltage for regulator %s, err = %d\n",
  254. vreg->cfg.name, ret);
  255. }
  256. vreg->enabled = false;
  257. return ret;
  258. }
  259. static struct cnss_vreg_cfg *get_vreg_list(u32 *vreg_list_size,
  260. enum cnss_vreg_type type)
  261. {
  262. switch (type) {
  263. case CNSS_VREG_PRIM:
  264. *vreg_list_size = CNSS_VREG_INFO_SIZE;
  265. return cnss_vreg_list;
  266. default:
  267. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  268. *vreg_list_size = 0;
  269. return NULL;
  270. }
  271. }
  272. static int cnss_get_vreg(struct cnss_plat_data *plat_priv,
  273. struct list_head *vreg_list,
  274. struct cnss_vreg_cfg *vreg_cfg,
  275. u32 vreg_list_size)
  276. {
  277. int ret = 0;
  278. int i;
  279. struct cnss_vreg_info *vreg;
  280. struct device *dev = &plat_priv->plat_dev->dev;
  281. if (!list_empty(vreg_list)) {
  282. cnss_pr_dbg("Vregs have already been updated\n");
  283. return 0;
  284. }
  285. for (i = 0; i < vreg_list_size; i++) {
  286. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  287. if (!vreg)
  288. return -ENOMEM;
  289. memcpy(&vreg->cfg, &vreg_cfg[i], sizeof(vreg->cfg));
  290. ret = cnss_get_vreg_single(plat_priv, vreg);
  291. if (ret != 0) {
  292. if (ret == -ENODEV) {
  293. devm_kfree(dev, vreg);
  294. continue;
  295. } else {
  296. devm_kfree(dev, vreg);
  297. return ret;
  298. }
  299. }
  300. list_add_tail(&vreg->list, vreg_list);
  301. }
  302. return 0;
  303. }
  304. static void cnss_put_vreg(struct cnss_plat_data *plat_priv,
  305. struct list_head *vreg_list)
  306. {
  307. struct cnss_vreg_info *vreg;
  308. while (!list_empty(vreg_list)) {
  309. vreg = list_first_entry(vreg_list,
  310. struct cnss_vreg_info, list);
  311. list_del(&vreg->list);
  312. if (IS_ERR_OR_NULL(vreg->reg))
  313. continue;
  314. cnss_put_vreg_single(plat_priv, vreg);
  315. }
  316. }
  317. static int cnss_vreg_on(struct cnss_plat_data *plat_priv,
  318. struct list_head *vreg_list)
  319. {
  320. struct cnss_vreg_info *vreg;
  321. int ret = 0;
  322. list_for_each_entry(vreg, vreg_list, list) {
  323. if (IS_ERR_OR_NULL(vreg->reg))
  324. continue;
  325. ret = cnss_vreg_on_single(vreg);
  326. if (ret)
  327. break;
  328. }
  329. if (!ret)
  330. return 0;
  331. list_for_each_entry_continue_reverse(vreg, vreg_list, list) {
  332. if (IS_ERR_OR_NULL(vreg->reg) || !vreg->enabled)
  333. continue;
  334. cnss_vreg_off_single(vreg);
  335. }
  336. return ret;
  337. }
  338. static int cnss_vreg_off(struct cnss_plat_data *plat_priv,
  339. struct list_head *vreg_list)
  340. {
  341. struct cnss_vreg_info *vreg;
  342. list_for_each_entry_reverse(vreg, vreg_list, list) {
  343. if (IS_ERR_OR_NULL(vreg->reg))
  344. continue;
  345. cnss_vreg_off_single(vreg);
  346. }
  347. return 0;
  348. }
  349. static int cnss_vreg_unvote(struct cnss_plat_data *plat_priv,
  350. struct list_head *vreg_list)
  351. {
  352. struct cnss_vreg_info *vreg;
  353. list_for_each_entry_reverse(vreg, vreg_list, list) {
  354. if (IS_ERR_OR_NULL(vreg->reg))
  355. continue;
  356. if (vreg->cfg.need_unvote)
  357. cnss_vreg_unvote_single(vreg);
  358. }
  359. return 0;
  360. }
  361. int cnss_get_vreg_type(struct cnss_plat_data *plat_priv,
  362. enum cnss_vreg_type type)
  363. {
  364. struct cnss_vreg_cfg *vreg_cfg;
  365. u32 vreg_list_size = 0;
  366. int ret = 0;
  367. vreg_cfg = get_vreg_list(&vreg_list_size, type);
  368. if (!vreg_cfg)
  369. return -EINVAL;
  370. switch (type) {
  371. case CNSS_VREG_PRIM:
  372. ret = cnss_get_vreg(plat_priv, &plat_priv->vreg_list,
  373. vreg_cfg, vreg_list_size);
  374. break;
  375. default:
  376. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  377. return -EINVAL;
  378. }
  379. return ret;
  380. }
  381. void cnss_put_vreg_type(struct cnss_plat_data *plat_priv,
  382. enum cnss_vreg_type type)
  383. {
  384. switch (type) {
  385. case CNSS_VREG_PRIM:
  386. cnss_put_vreg(plat_priv, &plat_priv->vreg_list);
  387. break;
  388. default:
  389. return;
  390. }
  391. }
  392. int cnss_vreg_on_type(struct cnss_plat_data *plat_priv,
  393. enum cnss_vreg_type type)
  394. {
  395. int ret = 0;
  396. switch (type) {
  397. case CNSS_VREG_PRIM:
  398. ret = cnss_vreg_on(plat_priv, &plat_priv->vreg_list);
  399. break;
  400. default:
  401. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  402. return -EINVAL;
  403. }
  404. return ret;
  405. }
  406. int cnss_vreg_off_type(struct cnss_plat_data *plat_priv,
  407. enum cnss_vreg_type type)
  408. {
  409. int ret = 0;
  410. switch (type) {
  411. case CNSS_VREG_PRIM:
  412. ret = cnss_vreg_off(plat_priv, &plat_priv->vreg_list);
  413. break;
  414. default:
  415. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  416. return -EINVAL;
  417. }
  418. return ret;
  419. }
  420. int cnss_vreg_unvote_type(struct cnss_plat_data *plat_priv,
  421. enum cnss_vreg_type type)
  422. {
  423. int ret = 0;
  424. switch (type) {
  425. case CNSS_VREG_PRIM:
  426. ret = cnss_vreg_unvote(plat_priv, &plat_priv->vreg_list);
  427. break;
  428. default:
  429. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  430. return -EINVAL;
  431. }
  432. return ret;
  433. }
  434. static int cnss_get_clk_single(struct cnss_plat_data *plat_priv,
  435. struct cnss_clk_info *clk_info)
  436. {
  437. struct device *dev = &plat_priv->plat_dev->dev;
  438. struct clk *clk;
  439. int ret;
  440. clk = devm_clk_get(dev, clk_info->cfg.name);
  441. if (IS_ERR(clk)) {
  442. ret = PTR_ERR(clk);
  443. if (clk_info->cfg.required)
  444. cnss_pr_err("Failed to get clock %s, err = %d\n",
  445. clk_info->cfg.name, ret);
  446. else
  447. cnss_pr_dbg("Failed to get optional clock %s, err = %d\n",
  448. clk_info->cfg.name, ret);
  449. return ret;
  450. }
  451. clk_info->clk = clk;
  452. cnss_pr_dbg("Got clock: %s, freq: %u\n",
  453. clk_info->cfg.name, clk_info->cfg.freq);
  454. return 0;
  455. }
  456. static void cnss_put_clk_single(struct cnss_plat_data *plat_priv,
  457. struct cnss_clk_info *clk_info)
  458. {
  459. struct device *dev = &plat_priv->plat_dev->dev;
  460. cnss_pr_dbg("Put clock: %s\n", clk_info->cfg.name);
  461. devm_clk_put(dev, clk_info->clk);
  462. }
  463. static int cnss_clk_on_single(struct cnss_clk_info *clk_info)
  464. {
  465. int ret;
  466. if (clk_info->enabled) {
  467. cnss_pr_dbg("Clock %s is already enabled\n",
  468. clk_info->cfg.name);
  469. return 0;
  470. }
  471. cnss_pr_dbg("Clock %s is being enabled\n", clk_info->cfg.name);
  472. if (clk_info->cfg.freq) {
  473. ret = clk_set_rate(clk_info->clk, clk_info->cfg.freq);
  474. if (ret) {
  475. cnss_pr_err("Failed to set frequency %u for clock %s, err = %d\n",
  476. clk_info->cfg.freq, clk_info->cfg.name,
  477. ret);
  478. return ret;
  479. }
  480. }
  481. ret = clk_prepare_enable(clk_info->clk);
  482. if (ret) {
  483. cnss_pr_err("Failed to enable clock %s, err = %d\n",
  484. clk_info->cfg.name, ret);
  485. return ret;
  486. }
  487. clk_info->enabled = true;
  488. return 0;
  489. }
  490. static int cnss_clk_off_single(struct cnss_clk_info *clk_info)
  491. {
  492. if (!clk_info->enabled) {
  493. cnss_pr_dbg("Clock %s is already disabled\n",
  494. clk_info->cfg.name);
  495. return 0;
  496. }
  497. cnss_pr_dbg("Clock %s is being disabled\n", clk_info->cfg.name);
  498. clk_disable_unprepare(clk_info->clk);
  499. clk_info->enabled = false;
  500. return 0;
  501. }
  502. int cnss_get_clk(struct cnss_plat_data *plat_priv)
  503. {
  504. struct device *dev;
  505. struct list_head *clk_list;
  506. struct cnss_clk_info *clk_info;
  507. int ret, i;
  508. if (!plat_priv)
  509. return -ENODEV;
  510. dev = &plat_priv->plat_dev->dev;
  511. clk_list = &plat_priv->clk_list;
  512. if (!list_empty(clk_list)) {
  513. cnss_pr_dbg("Clocks have already been updated\n");
  514. return 0;
  515. }
  516. for (i = 0; i < CNSS_CLK_INFO_SIZE; i++) {
  517. clk_info = devm_kzalloc(dev, sizeof(*clk_info), GFP_KERNEL);
  518. if (!clk_info) {
  519. ret = -ENOMEM;
  520. goto cleanup;
  521. }
  522. memcpy(&clk_info->cfg, &cnss_clk_list[i],
  523. sizeof(clk_info->cfg));
  524. ret = cnss_get_clk_single(plat_priv, clk_info);
  525. if (ret != 0) {
  526. if (clk_info->cfg.required) {
  527. devm_kfree(dev, clk_info);
  528. goto cleanup;
  529. } else {
  530. devm_kfree(dev, clk_info);
  531. continue;
  532. }
  533. }
  534. list_add_tail(&clk_info->list, clk_list);
  535. }
  536. return 0;
  537. cleanup:
  538. while (!list_empty(clk_list)) {
  539. clk_info = list_first_entry(clk_list, struct cnss_clk_info,
  540. list);
  541. list_del(&clk_info->list);
  542. if (IS_ERR_OR_NULL(clk_info->clk))
  543. continue;
  544. cnss_put_clk_single(plat_priv, clk_info);
  545. devm_kfree(dev, clk_info);
  546. }
  547. return ret;
  548. }
  549. void cnss_put_clk(struct cnss_plat_data *plat_priv)
  550. {
  551. struct device *dev;
  552. struct list_head *clk_list;
  553. struct cnss_clk_info *clk_info;
  554. if (!plat_priv)
  555. return;
  556. dev = &plat_priv->plat_dev->dev;
  557. clk_list = &plat_priv->clk_list;
  558. while (!list_empty(clk_list)) {
  559. clk_info = list_first_entry(clk_list, struct cnss_clk_info,
  560. list);
  561. list_del(&clk_info->list);
  562. if (IS_ERR_OR_NULL(clk_info->clk))
  563. continue;
  564. cnss_put_clk_single(plat_priv, clk_info);
  565. devm_kfree(dev, clk_info);
  566. }
  567. }
  568. static int cnss_clk_on(struct cnss_plat_data *plat_priv,
  569. struct list_head *clk_list)
  570. {
  571. struct cnss_clk_info *clk_info;
  572. int ret = 0;
  573. list_for_each_entry(clk_info, clk_list, list) {
  574. if (IS_ERR_OR_NULL(clk_info->clk))
  575. continue;
  576. ret = cnss_clk_on_single(clk_info);
  577. if (ret)
  578. break;
  579. }
  580. if (!ret)
  581. return 0;
  582. list_for_each_entry_continue_reverse(clk_info, clk_list, list) {
  583. if (IS_ERR_OR_NULL(clk_info->clk))
  584. continue;
  585. cnss_clk_off_single(clk_info);
  586. }
  587. return ret;
  588. }
  589. static int cnss_clk_off(struct cnss_plat_data *plat_priv,
  590. struct list_head *clk_list)
  591. {
  592. struct cnss_clk_info *clk_info;
  593. list_for_each_entry_reverse(clk_info, clk_list, list) {
  594. if (IS_ERR_OR_NULL(clk_info->clk))
  595. continue;
  596. cnss_clk_off_single(clk_info);
  597. }
  598. return 0;
  599. }
  600. int cnss_get_pinctrl(struct cnss_plat_data *plat_priv)
  601. {
  602. int ret = 0;
  603. struct device *dev;
  604. struct cnss_pinctrl_info *pinctrl_info;
  605. dev = &plat_priv->plat_dev->dev;
  606. pinctrl_info = &plat_priv->pinctrl_info;
  607. pinctrl_info->pinctrl = devm_pinctrl_get(dev);
  608. if (IS_ERR_OR_NULL(pinctrl_info->pinctrl)) {
  609. ret = PTR_ERR(pinctrl_info->pinctrl);
  610. cnss_pr_err("Failed to get pinctrl, err = %d\n", ret);
  611. goto out;
  612. }
  613. if (of_find_property(dev->of_node, BOOTSTRAP_GPIO, NULL)) {
  614. pinctrl_info->bootstrap_active =
  615. pinctrl_lookup_state(pinctrl_info->pinctrl,
  616. BOOTSTRAP_ACTIVE);
  617. if (IS_ERR_OR_NULL(pinctrl_info->bootstrap_active)) {
  618. ret = PTR_ERR(pinctrl_info->bootstrap_active);
  619. cnss_pr_err("Failed to get bootstrap active state, err = %d\n",
  620. ret);
  621. goto out;
  622. }
  623. }
  624. if (of_find_property(dev->of_node, HOST_SOL_GPIO, NULL) &&
  625. of_find_property(dev->of_node, DEV_SOL_GPIO, NULL)) {
  626. pinctrl_info->sol_default =
  627. pinctrl_lookup_state(pinctrl_info->pinctrl,
  628. SOL_DEFAULT);
  629. if (IS_ERR_OR_NULL(pinctrl_info->sol_default)) {
  630. ret = PTR_ERR(pinctrl_info->sol_default);
  631. cnss_pr_err("Failed to get sol default state, err = %d\n",
  632. ret);
  633. goto out;
  634. }
  635. cnss_pr_dbg("Got sol default state\n");
  636. }
  637. if (of_find_property(dev->of_node, WLAN_EN_GPIO, NULL)) {
  638. pinctrl_info->wlan_en_gpio = of_get_named_gpio(dev->of_node,
  639. WLAN_EN_GPIO, 0);
  640. cnss_pr_dbg("WLAN_EN GPIO: %d\n", pinctrl_info->wlan_en_gpio);
  641. pinctrl_info->wlan_en_active =
  642. pinctrl_lookup_state(pinctrl_info->pinctrl,
  643. WLAN_EN_ACTIVE);
  644. if (IS_ERR_OR_NULL(pinctrl_info->wlan_en_active)) {
  645. ret = PTR_ERR(pinctrl_info->wlan_en_active);
  646. cnss_pr_err("Failed to get wlan_en active state, err = %d\n",
  647. ret);
  648. goto out;
  649. }
  650. pinctrl_info->wlan_en_sleep =
  651. pinctrl_lookup_state(pinctrl_info->pinctrl,
  652. WLAN_EN_SLEEP);
  653. if (IS_ERR_OR_NULL(pinctrl_info->wlan_en_sleep)) {
  654. ret = PTR_ERR(pinctrl_info->wlan_en_sleep);
  655. cnss_pr_err("Failed to get wlan_en sleep state, err = %d\n",
  656. ret);
  657. goto out;
  658. }
  659. } else {
  660. pinctrl_info->wlan_en_gpio = -EINVAL;
  661. }
  662. /* Added for QCA6490 PMU delayed WLAN_EN_GPIO */
  663. if (of_find_property(dev->of_node, BT_EN_GPIO, NULL)) {
  664. pinctrl_info->bt_en_gpio = of_get_named_gpio(dev->of_node,
  665. BT_EN_GPIO, 0);
  666. cnss_pr_dbg("BT GPIO: %d\n", pinctrl_info->bt_en_gpio);
  667. } else {
  668. pinctrl_info->bt_en_gpio = -EINVAL;
  669. }
  670. /* Added for QCA6490 to minimize XO CLK selection leakage prevention */
  671. if (of_find_property(dev->of_node, XO_CLK_GPIO, NULL)) {
  672. pinctrl_info->xo_clk_gpio = of_get_named_gpio(dev->of_node,
  673. XO_CLK_GPIO, 0);
  674. cnss_pr_dbg("QCA6490 XO_CLK GPIO: %d\n",
  675. pinctrl_info->xo_clk_gpio);
  676. cnss_set_feature_list(plat_priv, BOOTSTRAP_CLOCK_SELECT_V01);
  677. } else {
  678. pinctrl_info->xo_clk_gpio = -EINVAL;
  679. }
  680. if (of_find_property(dev->of_node, SW_CTRL_GPIO, NULL)) {
  681. pinctrl_info->sw_ctrl_gpio = of_get_named_gpio(dev->of_node,
  682. SW_CTRL_GPIO,
  683. 0);
  684. cnss_pr_dbg("Switch control GPIO: %d\n",
  685. pinctrl_info->sw_ctrl_gpio);
  686. } else {
  687. pinctrl_info->sw_ctrl_gpio = -EINVAL;
  688. }
  689. return 0;
  690. out:
  691. return ret;
  692. }
  693. int cnss_get_wlan_sw_ctrl(struct cnss_plat_data *plat_priv)
  694. {
  695. struct device *dev;
  696. struct cnss_pinctrl_info *pinctrl_info;
  697. dev = &plat_priv->plat_dev->dev;
  698. pinctrl_info = &plat_priv->pinctrl_info;
  699. if (of_find_property(dev->of_node, WLAN_SW_CTRL_GPIO, NULL)) {
  700. pinctrl_info->wlan_sw_ctrl_gpio = of_get_named_gpio(dev->of_node,
  701. WLAN_SW_CTRL_GPIO,
  702. 0);
  703. cnss_pr_dbg("WLAN Switch control GPIO: %d\n",
  704. pinctrl_info->wlan_sw_ctrl_gpio);
  705. } else {
  706. pinctrl_info->wlan_sw_ctrl_gpio = -EINVAL;
  707. }
  708. return 0;
  709. }
  710. #define CNSS_XO_CLK_RETRY_COUNT_MAX 5
  711. static void cnss_set_xo_clk_gpio_state(struct cnss_plat_data *plat_priv,
  712. bool enable)
  713. {
  714. int xo_clk_gpio = plat_priv->pinctrl_info.xo_clk_gpio, retry = 0, ret;
  715. if (xo_clk_gpio < 0 || plat_priv->device_id != QCA6490_DEVICE_ID)
  716. return;
  717. retry_gpio_req:
  718. ret = gpio_request(xo_clk_gpio, "XO_CLK_GPIO");
  719. if (ret) {
  720. if (retry++ < CNSS_XO_CLK_RETRY_COUNT_MAX) {
  721. /* wait for ~(10 - 20) ms */
  722. usleep_range(10000, 20000);
  723. goto retry_gpio_req;
  724. }
  725. }
  726. if (ret) {
  727. cnss_pr_err("QCA6490 XO CLK Gpio request failed\n");
  728. return;
  729. }
  730. if (enable) {
  731. gpio_direction_output(xo_clk_gpio, 1);
  732. /*XO CLK must be asserted for some time before WLAN_EN */
  733. usleep_range(100, 200);
  734. } else {
  735. /* Assert XO CLK ~(2-5)ms before off for valid latch in HW */
  736. usleep_range(2000, 5000);
  737. gpio_direction_output(xo_clk_gpio, 0);
  738. }
  739. gpio_free(xo_clk_gpio);
  740. }
  741. static int cnss_select_pinctrl_state(struct cnss_plat_data *plat_priv,
  742. bool state)
  743. {
  744. int ret = 0;
  745. struct cnss_pinctrl_info *pinctrl_info;
  746. if (!plat_priv) {
  747. cnss_pr_err("plat_priv is NULL!\n");
  748. ret = -ENODEV;
  749. goto out;
  750. }
  751. pinctrl_info = &plat_priv->pinctrl_info;
  752. if (state) {
  753. if (!IS_ERR_OR_NULL(pinctrl_info->bootstrap_active)) {
  754. ret = pinctrl_select_state
  755. (pinctrl_info->pinctrl,
  756. pinctrl_info->bootstrap_active);
  757. if (ret) {
  758. cnss_pr_err("Failed to select bootstrap active state, err = %d\n",
  759. ret);
  760. goto out;
  761. }
  762. udelay(BOOTSTRAP_DELAY);
  763. }
  764. if (!IS_ERR_OR_NULL(pinctrl_info->sol_default)) {
  765. ret = pinctrl_select_state
  766. (pinctrl_info->pinctrl,
  767. pinctrl_info->sol_default);
  768. if (ret) {
  769. cnss_pr_err("Failed to select sol default state, err = %d\n",
  770. ret);
  771. goto out;
  772. }
  773. cnss_pr_dbg("Selected sol default state\n");
  774. }
  775. cnss_set_xo_clk_gpio_state(plat_priv, true);
  776. if (!IS_ERR_OR_NULL(pinctrl_info->wlan_en_active)) {
  777. ret = pinctrl_select_state
  778. (pinctrl_info->pinctrl,
  779. pinctrl_info->wlan_en_active);
  780. if (ret) {
  781. cnss_pr_err("Failed to select wlan_en active state, err = %d\n",
  782. ret);
  783. goto out;
  784. }
  785. udelay(WLAN_ENABLE_DELAY);
  786. cnss_set_xo_clk_gpio_state(plat_priv, false);
  787. } else {
  788. cnss_set_xo_clk_gpio_state(plat_priv, false);
  789. goto out;
  790. }
  791. } else {
  792. if (!IS_ERR_OR_NULL(pinctrl_info->wlan_en_sleep)) {
  793. cnss_wlan_hw_disable_check(plat_priv);
  794. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  795. cnss_pr_dbg("Avoid WLAN_EN low. WLAN HW Disbaled");
  796. goto out;
  797. }
  798. ret = pinctrl_select_state(pinctrl_info->pinctrl,
  799. pinctrl_info->wlan_en_sleep);
  800. if (ret) {
  801. cnss_pr_err("Failed to select wlan_en sleep state, err = %d\n",
  802. ret);
  803. goto out;
  804. }
  805. } else {
  806. goto out;
  807. }
  808. }
  809. cnss_pr_dbg("WLAN_EN Value: %d\n", gpio_get_value(pinctrl_info->wlan_en_gpio));
  810. cnss_pr_dbg("%s WLAN_EN GPIO successfully\n",
  811. state ? "Assert" : "De-assert");
  812. return 0;
  813. out:
  814. return ret;
  815. }
  816. /**
  817. * cnss_select_pinctrl_enable - select WLAN_GPIO for Active pinctrl status
  818. * @plat_priv: Platform private data structure pointer
  819. *
  820. * For QCA6490, PMU requires minimum 100ms delay between BT_EN_GPIO off and
  821. * WLAN_EN_GPIO on. This is done to avoid power up issues.
  822. *
  823. * Return: Status of pinctrl select operation. 0 - Success.
  824. */
  825. static int cnss_select_pinctrl_enable(struct cnss_plat_data *plat_priv)
  826. {
  827. int ret = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  828. u8 wlan_en_state = 0;
  829. if (bt_en_gpio < 0 || plat_priv->device_id != QCA6490_DEVICE_ID)
  830. goto set_wlan_en;
  831. if (gpio_get_value(bt_en_gpio)) {
  832. cnss_pr_dbg("BT_EN_GPIO State: On\n");
  833. ret = cnss_select_pinctrl_state(plat_priv, true);
  834. if (!ret)
  835. return ret;
  836. wlan_en_state = 1;
  837. }
  838. if (!gpio_get_value(bt_en_gpio)) {
  839. cnss_pr_dbg("BT_EN_GPIO State: Off. Delay WLAN_GPIO enable\n");
  840. /* check for BT_EN_GPIO down race during above operation */
  841. if (wlan_en_state) {
  842. cnss_pr_dbg("Reset WLAN_EN as BT got turned off during enable\n");
  843. cnss_select_pinctrl_state(plat_priv, false);
  844. wlan_en_state = 0;
  845. }
  846. /* 100 ms delay for BT_EN and WLAN_EN QCA6490 PMU sequencing */
  847. msleep(100);
  848. }
  849. set_wlan_en:
  850. if (!wlan_en_state)
  851. ret = cnss_select_pinctrl_state(plat_priv, true);
  852. return ret;
  853. }
  854. int cnss_get_input_gpio_value(struct cnss_plat_data *plat_priv, int gpio_num)
  855. {
  856. int ret;
  857. if (gpio_num < 0)
  858. return -EINVAL;
  859. ret = gpio_direction_input(gpio_num);
  860. if (ret) {
  861. cnss_pr_err("Failed to set direction of GPIO(%d), err = %d",
  862. gpio_num, ret);
  863. return -EINVAL;
  864. }
  865. return gpio_get_value(gpio_num);
  866. }
  867. int cnss_power_on_device(struct cnss_plat_data *plat_priv)
  868. {
  869. int ret = 0;
  870. if (plat_priv->powered_on) {
  871. cnss_pr_dbg("Already powered up");
  872. return 0;
  873. }
  874. cnss_wlan_hw_disable_check(plat_priv);
  875. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  876. cnss_pr_dbg("Avoid WLAN Power On. WLAN HW Disbaled");
  877. return -EINVAL;
  878. }
  879. ret = cnss_vreg_on_type(plat_priv, CNSS_VREG_PRIM);
  880. if (ret) {
  881. cnss_pr_err("Failed to turn on vreg, err = %d\n", ret);
  882. goto out;
  883. }
  884. ret = cnss_clk_on(plat_priv, &plat_priv->clk_list);
  885. if (ret) {
  886. cnss_pr_err("Failed to turn on clocks, err = %d\n", ret);
  887. goto vreg_off;
  888. }
  889. ret = cnss_select_pinctrl_enable(plat_priv);
  890. if (ret) {
  891. cnss_pr_err("Failed to select pinctrl state, err = %d\n", ret);
  892. goto clk_off;
  893. }
  894. plat_priv->powered_on = true;
  895. cnss_enable_dev_sol_irq(plat_priv);
  896. cnss_set_host_sol_value(plat_priv, 0);
  897. return 0;
  898. clk_off:
  899. cnss_clk_off(plat_priv, &plat_priv->clk_list);
  900. vreg_off:
  901. cnss_vreg_off_type(plat_priv, CNSS_VREG_PRIM);
  902. out:
  903. return ret;
  904. }
  905. void cnss_power_off_device(struct cnss_plat_data *plat_priv)
  906. {
  907. if (!plat_priv->powered_on) {
  908. cnss_pr_dbg("Already powered down");
  909. return;
  910. }
  911. cnss_disable_dev_sol_irq(plat_priv);
  912. cnss_select_pinctrl_state(plat_priv, false);
  913. cnss_clk_off(plat_priv, &plat_priv->clk_list);
  914. cnss_vreg_off_type(plat_priv, CNSS_VREG_PRIM);
  915. plat_priv->powered_on = false;
  916. }
  917. bool cnss_is_device_powered_on(struct cnss_plat_data *plat_priv)
  918. {
  919. return plat_priv->powered_on;
  920. }
  921. void cnss_set_pin_connect_status(struct cnss_plat_data *plat_priv)
  922. {
  923. unsigned long pin_status = 0;
  924. set_bit(CNSS_WLAN_EN, &pin_status);
  925. set_bit(CNSS_PCIE_TXN, &pin_status);
  926. set_bit(CNSS_PCIE_TXP, &pin_status);
  927. set_bit(CNSS_PCIE_RXN, &pin_status);
  928. set_bit(CNSS_PCIE_RXP, &pin_status);
  929. set_bit(CNSS_PCIE_REFCLKN, &pin_status);
  930. set_bit(CNSS_PCIE_REFCLKP, &pin_status);
  931. set_bit(CNSS_PCIE_RST, &pin_status);
  932. plat_priv->pin_result.host_pin_result = pin_status;
  933. }
  934. #if IS_ENABLED(CONFIG_QCOM_COMMAND_DB)
  935. static int cnss_cmd_db_ready(struct cnss_plat_data *plat_priv)
  936. {
  937. return cmd_db_ready();
  938. }
  939. static u32 cnss_cmd_db_read_addr(struct cnss_plat_data *plat_priv,
  940. const char *res_id)
  941. {
  942. return cmd_db_read_addr(res_id);
  943. }
  944. #else
  945. static int cnss_cmd_db_ready(struct cnss_plat_data *plat_priv)
  946. {
  947. return -EOPNOTSUPP;
  948. }
  949. static u32 cnss_cmd_db_read_addr(struct cnss_plat_data *plat_priv,
  950. const char *res_id)
  951. {
  952. return 0;
  953. }
  954. #endif
  955. int cnss_get_tcs_info(struct cnss_plat_data *plat_priv)
  956. {
  957. struct platform_device *plat_dev = plat_priv->plat_dev;
  958. struct resource *res;
  959. resource_size_t addr_len;
  960. void __iomem *tcs_cmd_base_addr;
  961. int ret = 0;
  962. res = platform_get_resource_byname(plat_dev, IORESOURCE_MEM, "tcs_cmd");
  963. if (!res) {
  964. cnss_pr_dbg("TCS CMD address is not present for CPR\n");
  965. goto out;
  966. }
  967. plat_priv->tcs_info.cmd_base_addr = res->start;
  968. addr_len = resource_size(res);
  969. cnss_pr_dbg("TCS CMD base address is %pa with length %pa\n",
  970. &plat_priv->tcs_info.cmd_base_addr, &addr_len);
  971. tcs_cmd_base_addr = devm_ioremap(&plat_dev->dev, res->start, addr_len);
  972. if (!tcs_cmd_base_addr) {
  973. ret = -EINVAL;
  974. cnss_pr_err("Failed to map TCS CMD address, err = %d\n",
  975. ret);
  976. goto out;
  977. }
  978. plat_priv->tcs_info.cmd_base_addr_io = tcs_cmd_base_addr;
  979. return 0;
  980. out:
  981. return ret;
  982. }
  983. int cnss_get_cpr_info(struct cnss_plat_data *plat_priv)
  984. {
  985. struct platform_device *plat_dev = plat_priv->plat_dev;
  986. struct cnss_cpr_info *cpr_info = &plat_priv->cpr_info;
  987. const char *cmd_db_name;
  988. u32 cpr_pmic_addr = 0;
  989. int ret = 0;
  990. if (plat_priv->tcs_info.cmd_base_addr == 0) {
  991. cnss_pr_dbg("TCS CMD not configured\n");
  992. return 0;
  993. }
  994. ret = of_property_read_string(plat_dev->dev.of_node,
  995. "qcom,cmd_db_name", &cmd_db_name);
  996. if (ret) {
  997. cnss_pr_dbg("CommandDB name is not present for CPR\n");
  998. goto out;
  999. }
  1000. ret = cnss_cmd_db_ready(plat_priv);
  1001. if (ret) {
  1002. cnss_pr_err("CommandDB is not ready, err = %d\n", ret);
  1003. goto out;
  1004. }
  1005. cpr_pmic_addr = cnss_cmd_db_read_addr(plat_priv, cmd_db_name);
  1006. if (cpr_pmic_addr > 0) {
  1007. cpr_info->cpr_pmic_addr = cpr_pmic_addr;
  1008. cnss_pr_dbg("Get CPR PMIC address 0x%x from %s\n",
  1009. cpr_info->cpr_pmic_addr, cmd_db_name);
  1010. } else {
  1011. cnss_pr_err("CPR PMIC address is not available for %s\n",
  1012. cmd_db_name);
  1013. ret = -EINVAL;
  1014. goto out;
  1015. }
  1016. return 0;
  1017. out:
  1018. return ret;
  1019. }
  1020. #if IS_ENABLED(CONFIG_MSM_QMP)
  1021. int cnss_aop_mbox_init(struct cnss_plat_data *plat_priv)
  1022. {
  1023. struct mbox_client *mbox = &plat_priv->mbox_client_data;
  1024. struct mbox_chan *chan;
  1025. int ret;
  1026. plat_priv->mbox_chan = NULL;
  1027. mbox->dev = &plat_priv->plat_dev->dev;
  1028. mbox->tx_block = true;
  1029. mbox->tx_tout = CNSS_MBOX_TIMEOUT_MS;
  1030. mbox->knows_txdone = false;
  1031. chan = mbox_request_channel(mbox, 0);
  1032. if (IS_ERR(chan)) {
  1033. cnss_pr_err("Failed to get mbox channel\n");
  1034. return PTR_ERR(chan);
  1035. }
  1036. plat_priv->mbox_chan = chan;
  1037. cnss_pr_dbg("Mbox channel initialized\n");
  1038. ret = cnss_aop_pdc_reconfig(plat_priv);
  1039. if (ret)
  1040. cnss_pr_err("Failed to reconfig WLAN PDC, err = %d\n", ret);
  1041. return 0;
  1042. }
  1043. /**
  1044. * cnss_aop_send_msg: Sends json message to AOP using QMP
  1045. * @plat_priv: Pointer to cnss platform data
  1046. * @msg: String in json format
  1047. *
  1048. * AOP accepts JSON message to configure WLAN resources. Format as follows:
  1049. * To send VReg config: {class: wlan_pdc, ss: <pdc_name>,
  1050. * res: <VReg_name>.<param>, <seq_param>: <value>}
  1051. * To send PDC Config: {class: wlan_pdc, ss: <pdc_name>, res: pdc,
  1052. * enable: <Value>}
  1053. * QMP returns timeout error if format not correct or AOP operation fails.
  1054. *
  1055. * Return: 0 for success
  1056. */
  1057. int cnss_aop_send_msg(struct cnss_plat_data *plat_priv, char *mbox_msg)
  1058. {
  1059. struct qmp_pkt pkt;
  1060. int ret = 0;
  1061. cnss_pr_dbg("Sending AOP Mbox msg: %s\n", mbox_msg);
  1062. pkt.size = CNSS_MBOX_MSG_MAX_LEN;
  1063. pkt.data = mbox_msg;
  1064. ret = mbox_send_message(plat_priv->mbox_chan, &pkt);
  1065. if (ret < 0)
  1066. cnss_pr_err("Failed to send AOP mbox msg: %s\n", mbox_msg);
  1067. else
  1068. ret = 0;
  1069. return ret;
  1070. }
  1071. /* cnss_pdc_reconfig: Send PDC init table as configured in DT for wlan device */
  1072. int cnss_aop_pdc_reconfig(struct cnss_plat_data *plat_priv)
  1073. {
  1074. u32 i;
  1075. int ret;
  1076. if (plat_priv->pdc_init_table_len <= 0 || !plat_priv->pdc_init_table)
  1077. return 0;
  1078. cnss_pr_dbg("Setting PDC defaults for device ID: %d\n",
  1079. plat_priv->device_id);
  1080. for (i = 0; i < plat_priv->pdc_init_table_len; i++) {
  1081. ret = cnss_aop_send_msg(plat_priv,
  1082. (char *)plat_priv->pdc_init_table[i]);
  1083. if (ret < 0)
  1084. break;
  1085. }
  1086. return ret;
  1087. }
  1088. /* cnss_aop_pdc_name_str: Get PDC name corresponding to VReg from DT Mapiping */
  1089. static const char *cnss_aop_pdc_name_str(struct cnss_plat_data *plat_priv,
  1090. const char *vreg_name)
  1091. {
  1092. u32 i;
  1093. static const char * const aop_pdc_ss_str[] = {"rf", "bb"};
  1094. const char *pdc = aop_pdc_ss_str[0], *vreg_map_name;
  1095. if (plat_priv->vreg_pdc_map_len <= 0 || !plat_priv->vreg_pdc_map)
  1096. goto end;
  1097. for (i = 0; i < plat_priv->vreg_pdc_map_len; i++) {
  1098. vreg_map_name = plat_priv->vreg_pdc_map[i];
  1099. if (strnstr(vreg_map_name, vreg_name, strlen(vreg_map_name))) {
  1100. pdc = plat_priv->vreg_pdc_map[i + 1];
  1101. break;
  1102. }
  1103. }
  1104. end:
  1105. cnss_pr_dbg("%s mapped to %s\n", vreg_name, pdc);
  1106. return pdc;
  1107. }
  1108. static int cnss_aop_set_vreg_param(struct cnss_plat_data *plat_priv,
  1109. const char *vreg_name,
  1110. enum cnss_aop_vreg_param param,
  1111. enum cnss_aop_tcs_seq_param seq_param,
  1112. int val)
  1113. {
  1114. char msg[CNSS_MBOX_MSG_MAX_LEN];
  1115. static const char * const aop_vreg_param_str[] = {
  1116. [CNSS_VREG_VOLTAGE] = "v", [CNSS_VREG_MODE] = "m",
  1117. [CNSS_VREG_ENABLE] = "e",};
  1118. static const char * const aop_tcs_seq_str[] = {
  1119. [CNSS_TCS_UP_SEQ] = "upval", [CNSS_TCS_DOWN_SEQ] = "dwnval",
  1120. [CNSS_TCS_ENABLE_SEQ] = "enable",};
  1121. if (param >= CNSS_VREG_PARAM_MAX || seq_param >= CNSS_TCS_SEQ_MAX ||
  1122. !vreg_name)
  1123. return -EINVAL;
  1124. snprintf(msg, CNSS_MBOX_MSG_MAX_LEN,
  1125. "{class: wlan_pdc, ss: %s, res: %s.%s, %s: %d}",
  1126. cnss_aop_pdc_name_str(plat_priv, vreg_name),
  1127. vreg_name, aop_vreg_param_str[param],
  1128. aop_tcs_seq_str[seq_param], val);
  1129. return cnss_aop_send_msg(plat_priv, msg);
  1130. }
  1131. int cnss_aop_ol_cpr_cfg_setup(struct cnss_plat_data *plat_priv,
  1132. struct wlfw_pmu_cfg_v01 *fw_pmu_cfg)
  1133. {
  1134. const char *pmu_pin, *vreg;
  1135. struct wlfw_pmu_param_v01 *fw_pmu_param;
  1136. u32 fw_pmu_param_len, i, j, plat_vreg_param_len = 0;
  1137. int ret = 0;
  1138. struct platform_vreg_param {
  1139. char vreg[MAX_PROP_SIZE];
  1140. u32 wake_volt;
  1141. u32 sleep_volt;
  1142. } plat_vreg_param[QMI_WLFW_PMU_PARAMS_MAX_V01] = {0};
  1143. static bool config_done;
  1144. if (config_done)
  1145. return 0;
  1146. if (plat_priv->pmu_vreg_map_len <= 0 || !plat_priv->mbox_chan ||
  1147. !plat_priv->pmu_vreg_map) {
  1148. cnss_pr_dbg("Mbox channel / PMU VReg Map not configured\n");
  1149. goto end;
  1150. }
  1151. if (!fw_pmu_cfg)
  1152. return -EINVAL;
  1153. fw_pmu_param = fw_pmu_cfg->pmu_param;
  1154. fw_pmu_param_len = fw_pmu_cfg->pmu_param_len;
  1155. /* Get PMU Pin name to Platfom Vreg Mapping */
  1156. for (i = 0; i < fw_pmu_param_len; i++) {
  1157. cnss_pr_dbg("FW_PMU Data: %s %d %d %d %d\n",
  1158. fw_pmu_param[i].pin_name,
  1159. fw_pmu_param[i].wake_volt_valid,
  1160. fw_pmu_param[i].wake_volt,
  1161. fw_pmu_param[i].sleep_volt_valid,
  1162. fw_pmu_param[i].sleep_volt);
  1163. if (!fw_pmu_param[i].wake_volt_valid &&
  1164. !fw_pmu_param[i].sleep_volt_valid)
  1165. continue;
  1166. vreg = NULL;
  1167. for (j = 0; j < plat_priv->pmu_vreg_map_len; j += 2) {
  1168. pmu_pin = plat_priv->pmu_vreg_map[j];
  1169. if (strnstr(pmu_pin, fw_pmu_param[i].pin_name,
  1170. strlen(pmu_pin))) {
  1171. vreg = plat_priv->pmu_vreg_map[j + 1];
  1172. break;
  1173. }
  1174. }
  1175. if (!vreg) {
  1176. cnss_pr_err("No VREG mapping for %s\n",
  1177. fw_pmu_param[i].pin_name);
  1178. continue;
  1179. } else {
  1180. cnss_pr_dbg("%s mapped to %s\n",
  1181. fw_pmu_param[i].pin_name, vreg);
  1182. }
  1183. for (j = 0; j < QMI_WLFW_PMU_PARAMS_MAX_V01; j++) {
  1184. u32 wake_volt = 0, sleep_volt = 0;
  1185. if (plat_vreg_param[j].vreg[0] == '\0')
  1186. strlcpy(plat_vreg_param[j].vreg, vreg,
  1187. sizeof(plat_vreg_param[j].vreg));
  1188. else if (!strnstr(plat_vreg_param[j].vreg, vreg,
  1189. strlen(plat_vreg_param[j].vreg)))
  1190. continue;
  1191. if (fw_pmu_param[i].wake_volt_valid)
  1192. wake_volt = roundup(fw_pmu_param[i].wake_volt,
  1193. CNSS_PMIC_VOLTAGE_STEP) -
  1194. CNSS_PMIC_AUTO_HEADROOM +
  1195. CNSS_IR_DROP_WAKE;
  1196. if (fw_pmu_param[i].sleep_volt_valid)
  1197. sleep_volt = roundup(fw_pmu_param[i].sleep_volt,
  1198. CNSS_PMIC_VOLTAGE_STEP) -
  1199. CNSS_PMIC_AUTO_HEADROOM +
  1200. CNSS_IR_DROP_SLEEP;
  1201. plat_vreg_param[j].wake_volt =
  1202. (wake_volt > plat_vreg_param[j].wake_volt ?
  1203. wake_volt : plat_vreg_param[j].wake_volt);
  1204. plat_vreg_param[j].sleep_volt =
  1205. (sleep_volt > plat_vreg_param[j].sleep_volt ?
  1206. sleep_volt : plat_vreg_param[j].sleep_volt);
  1207. plat_vreg_param_len = (plat_vreg_param_len > j ?
  1208. plat_vreg_param_len : j);
  1209. cnss_pr_dbg("Plat VReg Data: %s %d %d\n",
  1210. plat_vreg_param[j].vreg,
  1211. plat_vreg_param[j].wake_volt,
  1212. plat_vreg_param[j].sleep_volt);
  1213. break;
  1214. }
  1215. }
  1216. for (i = 0; i <= plat_vreg_param_len; i++) {
  1217. if (plat_vreg_param[i].wake_volt > 0) {
  1218. ret =
  1219. cnss_aop_set_vreg_param(plat_priv,
  1220. plat_vreg_param[i].vreg,
  1221. CNSS_VREG_VOLTAGE,
  1222. CNSS_TCS_UP_SEQ,
  1223. plat_vreg_param[i].wake_volt);
  1224. }
  1225. if (plat_vreg_param[i].sleep_volt > 0) {
  1226. ret =
  1227. cnss_aop_set_vreg_param(plat_priv,
  1228. plat_vreg_param[i].vreg,
  1229. CNSS_VREG_VOLTAGE,
  1230. CNSS_TCS_DOWN_SEQ,
  1231. plat_vreg_param[i].sleep_volt);
  1232. }
  1233. if (ret < 0)
  1234. break;
  1235. }
  1236. end:
  1237. config_done = true;
  1238. return ret;
  1239. }
  1240. #else
  1241. int cnss_aop_mbox_init(struct cnss_plat_data *plat_priv)
  1242. {
  1243. return 0;
  1244. }
  1245. int cnss_aop_send_msg(struct cnss_plat_data *plat_priv, char *msg)
  1246. {
  1247. return 0;
  1248. }
  1249. int cnss_aop_pdc_reconfig(struct cnss_plat_data *plat_priv)
  1250. {
  1251. return 0;
  1252. }
  1253. static int cnss_aop_set_vreg_param(struct cnss_plat_data *plat_priv,
  1254. const char *vreg_name,
  1255. enum cnss_aop_vreg_param param,
  1256. enum cnss_aop_tcs_seq_pram seq_param,
  1257. int val)
  1258. {
  1259. return 0;
  1260. }
  1261. int cnss_aop_ol_cpr_cfg_setup(struct cnss_plat_data *plat_priv,
  1262. struct wlfw_pmu_cfg_v01 *fw_pmu_cfg)
  1263. {
  1264. return 0;
  1265. }
  1266. #endif
  1267. void cnss_power_misc_params_init(struct cnss_plat_data *plat_priv)
  1268. {
  1269. struct device *dev = &plat_priv->plat_dev->dev;
  1270. int ret;
  1271. /* common DT Entries */
  1272. plat_priv->pdc_init_table_len =
  1273. of_property_count_strings(dev->of_node,
  1274. "qcom,pdc_init_table");
  1275. if (plat_priv->pdc_init_table_len > 0) {
  1276. plat_priv->pdc_init_table =
  1277. kcalloc(plat_priv->pdc_init_table_len,
  1278. sizeof(char *), GFP_KERNEL);
  1279. ret =
  1280. of_property_read_string_array(dev->of_node,
  1281. "qcom,pdc_init_table",
  1282. plat_priv->pdc_init_table,
  1283. plat_priv->pdc_init_table_len);
  1284. if (ret < 0)
  1285. cnss_pr_err("Failed to get PDC Init Table\n");
  1286. } else {
  1287. cnss_pr_dbg("PDC Init Table not configured\n");
  1288. }
  1289. plat_priv->vreg_pdc_map_len =
  1290. of_property_count_strings(dev->of_node,
  1291. "qcom,vreg_pdc_map");
  1292. if (plat_priv->vreg_pdc_map_len > 0) {
  1293. plat_priv->vreg_pdc_map =
  1294. kcalloc(plat_priv->vreg_pdc_map_len,
  1295. sizeof(char *), GFP_KERNEL);
  1296. ret =
  1297. of_property_read_string_array(dev->of_node,
  1298. "qcom,vreg_pdc_map",
  1299. plat_priv->vreg_pdc_map,
  1300. plat_priv->vreg_pdc_map_len);
  1301. if (ret < 0)
  1302. cnss_pr_err("Failed to get VReg PDC Mapping\n");
  1303. } else {
  1304. cnss_pr_dbg("VReg PDC Mapping not configured\n");
  1305. }
  1306. plat_priv->pmu_vreg_map_len =
  1307. of_property_count_strings(dev->of_node,
  1308. "qcom,pmu_vreg_map");
  1309. if (plat_priv->pmu_vreg_map_len > 0) {
  1310. plat_priv->pmu_vreg_map = kcalloc(plat_priv->pmu_vreg_map_len,
  1311. sizeof(char *), GFP_KERNEL);
  1312. ret =
  1313. of_property_read_string_array(dev->of_node, "qcom,pmu_vreg_map",
  1314. plat_priv->pmu_vreg_map,
  1315. plat_priv->pmu_vreg_map_len);
  1316. if (ret < 0)
  1317. cnss_pr_err("Fail to get PMU VReg Mapping\n");
  1318. } else {
  1319. cnss_pr_dbg("PMU VReg Mapping not configured\n");
  1320. }
  1321. /* Device DT Specific */
  1322. if (plat_priv->device_id == QCA6390_DEVICE_ID ||
  1323. plat_priv->device_id == QCA6490_DEVICE_ID) {
  1324. ret = of_property_read_string(dev->of_node,
  1325. "qcom,vreg_ol_cpr",
  1326. &plat_priv->vreg_ol_cpr);
  1327. if (ret)
  1328. cnss_pr_dbg("VReg for QCA6490 OL CPR not configured\n");
  1329. ret = of_property_read_string(dev->of_node,
  1330. "qcom,vreg_ipa",
  1331. &plat_priv->vreg_ipa);
  1332. if (ret)
  1333. cnss_pr_dbg("VReg for QCA6490 Int Power Amp not configured\n");
  1334. }
  1335. }
  1336. int cnss_update_cpr_info(struct cnss_plat_data *plat_priv)
  1337. {
  1338. struct cnss_cpr_info *cpr_info = &plat_priv->cpr_info;
  1339. u32 pmic_addr, voltage = 0, voltage_tmp, offset;
  1340. void __iomem *tcs_cmd_addr, *tcs_cmd_data_addr;
  1341. int i, j;
  1342. if (cpr_info->voltage == 0) {
  1343. cnss_pr_err("OL CPR Voltage %dm is not valid\n",
  1344. cpr_info->voltage);
  1345. return -EINVAL;
  1346. }
  1347. if (plat_priv->device_id != QCA6490_DEVICE_ID)
  1348. return -EINVAL;
  1349. if (!plat_priv->vreg_ol_cpr || !plat_priv->mbox_chan) {
  1350. cnss_pr_dbg("Mbox channel / OL CPR Vreg not configured\n");
  1351. } else {
  1352. return cnss_aop_set_vreg_param(plat_priv,
  1353. plat_priv->vreg_ol_cpr,
  1354. CNSS_VREG_VOLTAGE,
  1355. CNSS_TCS_UP_SEQ,
  1356. cpr_info->voltage);
  1357. }
  1358. if (plat_priv->tcs_info.cmd_base_addr == 0) {
  1359. cnss_pr_dbg("TCS CMD not configured for OL CPR update\n");
  1360. return 0;
  1361. }
  1362. if (cpr_info->cpr_pmic_addr == 0) {
  1363. cnss_pr_err("PMIC address 0x%x is not valid\n",
  1364. cpr_info->cpr_pmic_addr);
  1365. return -EINVAL;
  1366. }
  1367. if (cpr_info->tcs_cmd_data_addr_io)
  1368. goto update_cpr;
  1369. for (i = 0; i < MAX_TCS_NUM; i++) {
  1370. for (j = 0; j < MAX_TCS_CMD_NUM; j++) {
  1371. offset = i * TCS_OFFSET + j * TCS_CMD_OFFSET;
  1372. tcs_cmd_addr = plat_priv->tcs_info.cmd_base_addr_io +
  1373. offset;
  1374. pmic_addr = readl_relaxed(tcs_cmd_addr);
  1375. if (pmic_addr == cpr_info->cpr_pmic_addr) {
  1376. tcs_cmd_data_addr = tcs_cmd_addr +
  1377. TCS_CMD_DATA_ADDR_OFFSET;
  1378. voltage_tmp = readl_relaxed(tcs_cmd_data_addr);
  1379. cnss_pr_dbg("Got voltage %dmV from i: %d, j: %d\n",
  1380. voltage_tmp, i, j);
  1381. if (voltage_tmp > voltage) {
  1382. voltage = voltage_tmp;
  1383. cpr_info->tcs_cmd_data_addr =
  1384. plat_priv->tcs_info.cmd_base_addr +
  1385. offset + TCS_CMD_DATA_ADDR_OFFSET;
  1386. cpr_info->tcs_cmd_data_addr_io =
  1387. tcs_cmd_data_addr;
  1388. }
  1389. }
  1390. }
  1391. }
  1392. if (!cpr_info->tcs_cmd_data_addr_io) {
  1393. cnss_pr_err("Failed to find proper TCS CMD data address\n");
  1394. return -EINVAL;
  1395. }
  1396. update_cpr:
  1397. cpr_info->voltage = cpr_info->voltage > BT_CXMX_VOLTAGE_MV ?
  1398. cpr_info->voltage : BT_CXMX_VOLTAGE_MV;
  1399. cnss_pr_dbg("Update TCS CMD data address %pa with voltage %dmV\n",
  1400. &cpr_info->tcs_cmd_data_addr, cpr_info->voltage);
  1401. writel_relaxed(cpr_info->voltage, cpr_info->tcs_cmd_data_addr_io);
  1402. return 0;
  1403. }
  1404. int cnss_enable_int_pow_amp_vreg(struct cnss_plat_data *plat_priv)
  1405. {
  1406. struct platform_device *plat_dev = plat_priv->plat_dev;
  1407. u32 offset, addr_val, data_val;
  1408. void __iomem *tcs_cmd;
  1409. int ret;
  1410. static bool config_done;
  1411. if (plat_priv->device_id != QCA6490_DEVICE_ID)
  1412. return -EINVAL;
  1413. if (config_done) {
  1414. cnss_pr_dbg("IPA Vreg already configured\n");
  1415. return 0;
  1416. }
  1417. if (!plat_priv->vreg_ipa || !plat_priv->mbox_chan) {
  1418. cnss_pr_dbg("Mbox channel / IPA Vreg not configured\n");
  1419. } else {
  1420. ret = cnss_aop_set_vreg_param(plat_priv,
  1421. plat_priv->vreg_ipa,
  1422. CNSS_VREG_ENABLE,
  1423. CNSS_TCS_UP_SEQ, 1);
  1424. if (ret == 0)
  1425. config_done = true;
  1426. return ret;
  1427. }
  1428. if (!plat_priv->tcs_info.cmd_base_addr_io) {
  1429. cnss_pr_err("TCS CMD not configured for IPA Vreg enable\n");
  1430. return -EINVAL;
  1431. }
  1432. ret = of_property_read_u32(plat_dev->dev.of_node,
  1433. "qcom,tcs_offset_int_pow_amp_vreg",
  1434. &offset);
  1435. if (ret) {
  1436. cnss_pr_dbg("Internal Power Amp Vreg not configured\n");
  1437. return -EINVAL;
  1438. }
  1439. tcs_cmd = plat_priv->tcs_info.cmd_base_addr_io + offset;
  1440. addr_val = readl_relaxed(tcs_cmd);
  1441. tcs_cmd += TCS_CMD_DATA_ADDR_OFFSET;
  1442. /* 1 = enable Vreg */
  1443. writel_relaxed(1, tcs_cmd);
  1444. data_val = readl_relaxed(tcs_cmd);
  1445. cnss_pr_dbg("Setup S3E TCS Addr: %x Data: %d\n", addr_val, data_val);
  1446. config_done = true;
  1447. return 0;
  1448. }