power.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/of.h>
  9. #include <linux/of_gpio.h>
  10. #include <linux/pinctrl/consumer.h>
  11. #include <linux/pinctrl/qcom-pinctrl.h>
  12. #include <linux/regulator/consumer.h>
  13. #if IS_ENABLED(CONFIG_QCOM_COMMAND_DB)
  14. #include <soc/qcom/cmd-db.h>
  15. #endif
  16. #include "main.h"
  17. #include "debug.h"
  18. #include "bus.h"
  19. #include <linux/soc/qcom/qcom_aoss.h>
  20. #if IS_ENABLED(CONFIG_ARCH_QCOM)
  21. static struct cnss_vreg_cfg cnss_vreg_list[] = {
  22. {"vdd-wlan-core", 1300000, 1300000, 0, 0, 0},
  23. {"vdd-wlan-io", 1800000, 1800000, 0, 0, 0},
  24. {"vdd-wlan-io12", 1200000, 1200000, 0, 0, 0},
  25. {"vdd-wlan-ant-share", 1800000, 1800000, 0, 0, 0},
  26. {"vdd-wlan-xtal-aon", 0, 0, 0, 0, 0},
  27. {"vdd-wlan-xtal", 1800000, 1800000, 0, 2, 0},
  28. {"vdd-wlan", 0, 0, 0, 0, 0},
  29. {"vdd-wlan-ctrl1", 0, 0, 0, 0, 0},
  30. {"vdd-wlan-ctrl2", 0, 0, 0, 0, 0},
  31. {"vdd-wlan-sp2t", 2700000, 2700000, 0, 0, 0},
  32. {"wlan-ant-switch", 1800000, 1800000, 0, 0, 0},
  33. {"wlan-soc-swreg", 1200000, 1200000, 0, 0, 0},
  34. {"vdd-wlan-aon", 950000, 950000, 0, 0, 0},
  35. {"vdd-wlan-dig", 950000, 952000, 0, 0, 0},
  36. {"vdd-wlan-rfa1", 1900000, 1900000, 0, 0, 0},
  37. {"vdd-wlan-rfa2", 1350000, 1350000, 0, 0, 0},
  38. {"vdd-wlan-rfa3", 1900000, 1900000, 450000, 0, 0},
  39. {"alt-sleep-clk", 0, 0, 0, 0, 0},
  40. {"vdd-wlan-en", 0, 0, 0, 10, 0},
  41. };
  42. static struct cnss_clk_cfg cnss_clk_list[] = {
  43. {"rf_clk", 0, 0},
  44. };
  45. #else
  46. static struct cnss_vreg_cfg cnss_vreg_list[] = {
  47. };
  48. static struct cnss_clk_cfg cnss_clk_list[] = {
  49. };
  50. #endif
  51. #define CNSS_VREG_INFO_SIZE ARRAY_SIZE(cnss_vreg_list)
  52. #define CNSS_CLK_INFO_SIZE ARRAY_SIZE(cnss_clk_list)
  53. #define MAX_PROP_SIZE 32
  54. #define BOOTSTRAP_GPIO "qcom,enable-bootstrap-gpio"
  55. #define BOOTSTRAP_ACTIVE "bootstrap_active"
  56. #define HOST_SOL_GPIO "wlan-host-sol-gpio"
  57. #define DEV_SOL_GPIO "wlan-dev-sol-gpio"
  58. #define SOL_DEFAULT "sol_default"
  59. #define WLAN_EN_GPIO "wlan-en-gpio"
  60. #define BT_EN_GPIO "qcom,bt-en-gpio"
  61. #define XO_CLK_GPIO "qcom,xo-clk-gpio"
  62. #define SW_CTRL_GPIO "qcom,sw-ctrl-gpio"
  63. #define WLAN_SW_CTRL_GPIO "qcom,wlan-sw-ctrl-gpio"
  64. #define WLAN_EN_ACTIVE "wlan_en_active"
  65. #define WLAN_EN_SLEEP "wlan_en_sleep"
  66. #define WLAN_VREGS_PROP "wlan_vregs"
  67. /* unit us */
  68. #define BOOTSTRAP_DELAY 1000
  69. #define WLAN_ENABLE_DELAY 1000
  70. /* unit ms */
  71. #define WLAN_ENABLE_DELAY_ROME 10
  72. #define TCS_CMD_DATA_ADDR_OFFSET 0x4
  73. #define TCS_OFFSET 0xC8
  74. #define TCS_CMD_OFFSET 0x10
  75. #define MAX_TCS_NUM 8
  76. #define MAX_TCS_CMD_NUM 5
  77. #define BT_CXMX_VOLTAGE_MV 950
  78. #define CNSS_MBOX_MSG_MAX_LEN 64
  79. #define CNSS_MBOX_TIMEOUT_MS 1000
  80. /* Platform HW config */
  81. #define CNSS_PMIC_VOLTAGE_STEP 4
  82. #define CNSS_PMIC_AUTO_HEADROOM 16
  83. #define CNSS_IR_DROP_WAKE 30
  84. #define CNSS_IR_DROP_SLEEP 10
  85. #define VREG_NOTFOUND 1
  86. /**
  87. * enum cnss_aop_vreg_param: Voltage regulator TCS param
  88. * @CNSS_VREG_VOLTAGE: Provides voltage level in mV to be configured in TCS
  89. * @CNSS_VREG_MODE: Regulator mode
  90. * @CNSS_VREG_TCS_ENABLE: Set bool Voltage regulator enable config in TCS.
  91. */
  92. enum cnss_aop_vreg_param {
  93. CNSS_VREG_VOLTAGE,
  94. CNSS_VREG_MODE,
  95. CNSS_VREG_ENABLE,
  96. CNSS_VREG_PARAM_MAX
  97. };
  98. /** enum cnss_aop_vreg_param_mode: Voltage modes supported by AOP*/
  99. enum cnss_aop_vreg_param_mode {
  100. CNSS_VREG_RET_MODE = 3,
  101. CNSS_VREG_LPM_MODE = 4,
  102. CNSS_VREG_AUTO_MODE = 6,
  103. CNSS_VREG_NPM_MODE = 7,
  104. CNSS_VREG_MODE_MAX
  105. };
  106. /**
  107. * enum cnss_aop_tcs_seq: TCS sequence ID for trigger
  108. * @CNSS_TCS_UP_SEQ: TCS Sequence based on up trigger / Wake TCS
  109. * @CNSS_TCS_DOWN_SEQ: TCS Sequence based on down trigger / Sleep TCS
  110. * @CNSS_TCS_ENABLE_SEQ: Enable this TCS seq entry
  111. */
  112. enum cnss_aop_tcs_seq_param {
  113. CNSS_TCS_UP_SEQ,
  114. CNSS_TCS_DOWN_SEQ,
  115. CNSS_TCS_ENABLE_SEQ,
  116. CNSS_TCS_SEQ_MAX
  117. };
  118. static int cnss_get_vreg_single(struct cnss_plat_data *plat_priv,
  119. struct cnss_vreg_info *vreg)
  120. {
  121. int ret = 0;
  122. struct device *dev;
  123. struct regulator *reg;
  124. const __be32 *prop;
  125. char prop_name[MAX_PROP_SIZE] = {0};
  126. int len;
  127. struct device_node *dt_node;
  128. dev = &plat_priv->plat_dev->dev;
  129. dt_node = (plat_priv->dev_node ? plat_priv->dev_node : dev->of_node);
  130. reg = devm_regulator_get_optional(dev, vreg->cfg.name);
  131. if (IS_ERR(reg)) {
  132. ret = PTR_ERR(reg);
  133. if (ret == -ENODEV)
  134. return ret;
  135. else if (ret == -EPROBE_DEFER)
  136. cnss_pr_info("EPROBE_DEFER for regulator: %s\n",
  137. vreg->cfg.name);
  138. else
  139. cnss_pr_err("Failed to get regulator %s, err = %d\n",
  140. vreg->cfg.name, ret);
  141. return ret;
  142. }
  143. vreg->reg = reg;
  144. snprintf(prop_name, MAX_PROP_SIZE, "qcom,%s-config",
  145. vreg->cfg.name);
  146. prop = of_get_property(dt_node, prop_name, &len);
  147. if (!prop || len != (5 * sizeof(__be32))) {
  148. cnss_pr_dbg("Property %s %s, use default\n", prop_name,
  149. prop ? "invalid format" : "doesn't exist");
  150. } else {
  151. vreg->cfg.min_uv = be32_to_cpup(&prop[0]);
  152. vreg->cfg.max_uv = be32_to_cpup(&prop[1]);
  153. vreg->cfg.load_ua = be32_to_cpup(&prop[2]);
  154. vreg->cfg.delay_us = be32_to_cpup(&prop[3]);
  155. vreg->cfg.need_unvote = be32_to_cpup(&prop[4]);
  156. }
  157. cnss_pr_dbg("Got regulator: %s, min_uv: %u, max_uv: %u, load_ua: %u, delay_us: %u, need_unvote: %u\n",
  158. vreg->cfg.name, vreg->cfg.min_uv,
  159. vreg->cfg.max_uv, vreg->cfg.load_ua,
  160. vreg->cfg.delay_us, vreg->cfg.need_unvote);
  161. return 0;
  162. }
  163. static void cnss_put_vreg_single(struct cnss_plat_data *plat_priv,
  164. struct cnss_vreg_info *vreg)
  165. {
  166. struct device *dev = &plat_priv->plat_dev->dev;
  167. cnss_pr_dbg("Put regulator: %s\n", vreg->cfg.name);
  168. devm_regulator_put(vreg->reg);
  169. devm_kfree(dev, vreg);
  170. }
  171. static int cnss_vreg_on_single(struct cnss_vreg_info *vreg)
  172. {
  173. int ret = 0;
  174. if (vreg->enabled) {
  175. cnss_pr_dbg("Regulator %s is already enabled\n",
  176. vreg->cfg.name);
  177. return 0;
  178. }
  179. cnss_pr_dbg("Regulator %s is being enabled\n", vreg->cfg.name);
  180. if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
  181. ret = regulator_set_voltage(vreg->reg,
  182. vreg->cfg.min_uv,
  183. vreg->cfg.max_uv);
  184. if (ret) {
  185. cnss_pr_err("Failed to set voltage for regulator %s, min_uv: %u, max_uv: %u, err = %d\n",
  186. vreg->cfg.name, vreg->cfg.min_uv,
  187. vreg->cfg.max_uv, ret);
  188. goto out;
  189. }
  190. }
  191. if (vreg->cfg.load_ua) {
  192. ret = regulator_set_load(vreg->reg,
  193. vreg->cfg.load_ua);
  194. if (ret < 0) {
  195. cnss_pr_err("Failed to set load for regulator %s, load: %u, err = %d\n",
  196. vreg->cfg.name, vreg->cfg.load_ua,
  197. ret);
  198. goto out;
  199. }
  200. }
  201. if (vreg->cfg.delay_us)
  202. udelay(vreg->cfg.delay_us);
  203. ret = regulator_enable(vreg->reg);
  204. if (ret) {
  205. cnss_pr_err("Failed to enable regulator %s, err = %d\n",
  206. vreg->cfg.name, ret);
  207. goto out;
  208. }
  209. vreg->enabled = true;
  210. out:
  211. return ret;
  212. }
  213. static int cnss_vreg_unvote_single(struct cnss_vreg_info *vreg)
  214. {
  215. int ret = 0;
  216. if (!vreg->enabled) {
  217. cnss_pr_dbg("Regulator %s is already disabled\n",
  218. vreg->cfg.name);
  219. return 0;
  220. }
  221. cnss_pr_dbg("Removing vote for Regulator %s\n", vreg->cfg.name);
  222. if (vreg->cfg.load_ua) {
  223. ret = regulator_set_load(vreg->reg, 0);
  224. if (ret < 0)
  225. cnss_pr_err("Failed to set load for regulator %s, err = %d\n",
  226. vreg->cfg.name, ret);
  227. }
  228. if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
  229. ret = regulator_set_voltage(vreg->reg, 0,
  230. vreg->cfg.max_uv);
  231. if (ret)
  232. cnss_pr_err("Failed to set voltage for regulator %s, err = %d\n",
  233. vreg->cfg.name, ret);
  234. }
  235. return ret;
  236. }
  237. static int cnss_vreg_off_single(struct cnss_vreg_info *vreg)
  238. {
  239. int ret = 0;
  240. if (!vreg->enabled) {
  241. cnss_pr_dbg("Regulator %s is already disabled\n",
  242. vreg->cfg.name);
  243. return 0;
  244. }
  245. cnss_pr_dbg("Regulator %s is being disabled\n",
  246. vreg->cfg.name);
  247. ret = regulator_disable(vreg->reg);
  248. if (ret)
  249. cnss_pr_err("Failed to disable regulator %s, err = %d\n",
  250. vreg->cfg.name, ret);
  251. if (vreg->cfg.load_ua) {
  252. ret = regulator_set_load(vreg->reg, 0);
  253. if (ret < 0)
  254. cnss_pr_err("Failed to set load for regulator %s, err = %d\n",
  255. vreg->cfg.name, ret);
  256. }
  257. if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
  258. ret = regulator_set_voltage(vreg->reg, 0,
  259. vreg->cfg.max_uv);
  260. if (ret)
  261. cnss_pr_err("Failed to set voltage for regulator %s, err = %d\n",
  262. vreg->cfg.name, ret);
  263. }
  264. vreg->enabled = false;
  265. return ret;
  266. }
  267. static struct cnss_vreg_cfg *get_vreg_list(u32 *vreg_list_size,
  268. enum cnss_vreg_type type)
  269. {
  270. switch (type) {
  271. case CNSS_VREG_PRIM:
  272. *vreg_list_size = CNSS_VREG_INFO_SIZE;
  273. return cnss_vreg_list;
  274. default:
  275. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  276. *vreg_list_size = 0;
  277. return NULL;
  278. }
  279. }
  280. /*
  281. * For multi-exchg dt node, get the required vregs' names from property
  282. * 'wlan_vregs', which is string array;
  283. *
  284. * If the property is not present or present but no value is set, then no
  285. * additional wlan verg is required, function return VREG_NOTFOUND.
  286. * If property is present with valid value, function return 0.
  287. * Other cases a negative value is returned.
  288. *
  289. * For non-multi-exchg dt, go through all vregs in the static array
  290. * 'cnss_vreg_list'.
  291. */
  292. static int cnss_get_vreg(struct cnss_plat_data *plat_priv,
  293. struct list_head *vreg_list,
  294. struct cnss_vreg_cfg *vreg_cfg,
  295. u32 vreg_list_size)
  296. {
  297. int ret = 0;
  298. int i;
  299. struct cnss_vreg_info *vreg;
  300. struct device *dev = &plat_priv->plat_dev->dev;
  301. int id_n;
  302. struct device_node *dt_node;
  303. if (!list_empty(vreg_list) &&
  304. (plat_priv->dt_type != CNSS_DTT_MULTIEXCHG)) {
  305. cnss_pr_dbg("Vregs have already been updated\n");
  306. return 0;
  307. }
  308. dt_node = (plat_priv->dev_node ? plat_priv->dev_node : dev->of_node);
  309. if (plat_priv->dt_type == CNSS_DTT_MULTIEXCHG) {
  310. id_n = of_property_count_strings(dt_node,
  311. WLAN_VREGS_PROP);
  312. if (id_n <= 0) {
  313. if (id_n == -ENODATA || id_n == -EINVAL) {
  314. cnss_pr_dbg("No additional vregs for: %s:%lx\n",
  315. dt_node->name,
  316. plat_priv->device_id);
  317. /* By returning a positive value, give the caller a
  318. * chance to know no additional regulator is needed
  319. * by this device, and shall not treat this case as
  320. * an error.
  321. */
  322. return VREG_NOTFOUND;
  323. }
  324. cnss_pr_err("property %s is invalid: %s:%lx\n",
  325. WLAN_VREGS_PROP, dt_node->name,
  326. plat_priv->device_id);
  327. return -EINVAL;
  328. }
  329. } else {
  330. id_n = vreg_list_size;
  331. }
  332. for (i = 0; i < id_n; i++) {
  333. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  334. if (!vreg)
  335. return -ENOMEM;
  336. if (plat_priv->dt_type == CNSS_DTT_MULTIEXCHG) {
  337. ret = of_property_read_string_index(dt_node,
  338. WLAN_VREGS_PROP, i,
  339. &vreg->cfg.name);
  340. if (ret) {
  341. cnss_pr_err("Failed to read vreg ids\n");
  342. return ret;
  343. }
  344. } else {
  345. memcpy(&vreg->cfg, &vreg_cfg[i], sizeof(vreg->cfg));
  346. }
  347. ret = cnss_get_vreg_single(plat_priv, vreg);
  348. if (ret != 0) {
  349. if (ret == -ENODEV) {
  350. devm_kfree(dev, vreg);
  351. continue;
  352. } else {
  353. devm_kfree(dev, vreg);
  354. return ret;
  355. }
  356. }
  357. list_add_tail(&vreg->list, vreg_list);
  358. }
  359. return 0;
  360. }
  361. static void cnss_put_vreg(struct cnss_plat_data *plat_priv,
  362. struct list_head *vreg_list)
  363. {
  364. struct cnss_vreg_info *vreg;
  365. while (!list_empty(vreg_list)) {
  366. vreg = list_first_entry(vreg_list,
  367. struct cnss_vreg_info, list);
  368. list_del(&vreg->list);
  369. if (IS_ERR_OR_NULL(vreg->reg))
  370. continue;
  371. cnss_put_vreg_single(plat_priv, vreg);
  372. }
  373. }
  374. static int cnss_vreg_on(struct cnss_plat_data *plat_priv,
  375. struct list_head *vreg_list)
  376. {
  377. struct cnss_vreg_info *vreg;
  378. int ret = 0;
  379. list_for_each_entry(vreg, vreg_list, list) {
  380. if (IS_ERR_OR_NULL(vreg->reg))
  381. continue;
  382. ret = cnss_vreg_on_single(vreg);
  383. if (ret)
  384. break;
  385. }
  386. if (!ret)
  387. return 0;
  388. list_for_each_entry_continue_reverse(vreg, vreg_list, list) {
  389. if (IS_ERR_OR_NULL(vreg->reg) || !vreg->enabled)
  390. continue;
  391. cnss_vreg_off_single(vreg);
  392. }
  393. return ret;
  394. }
  395. static int cnss_vreg_off(struct cnss_plat_data *plat_priv,
  396. struct list_head *vreg_list)
  397. {
  398. struct cnss_vreg_info *vreg;
  399. list_for_each_entry_reverse(vreg, vreg_list, list) {
  400. if (IS_ERR_OR_NULL(vreg->reg))
  401. continue;
  402. cnss_vreg_off_single(vreg);
  403. }
  404. return 0;
  405. }
  406. static int cnss_vreg_unvote(struct cnss_plat_data *plat_priv,
  407. struct list_head *vreg_list)
  408. {
  409. struct cnss_vreg_info *vreg;
  410. list_for_each_entry_reverse(vreg, vreg_list, list) {
  411. if (IS_ERR_OR_NULL(vreg->reg))
  412. continue;
  413. if (vreg->cfg.need_unvote)
  414. cnss_vreg_unvote_single(vreg);
  415. }
  416. return 0;
  417. }
  418. int cnss_get_vreg_type(struct cnss_plat_data *plat_priv,
  419. enum cnss_vreg_type type)
  420. {
  421. struct cnss_vreg_cfg *vreg_cfg;
  422. u32 vreg_list_size = 0;
  423. int ret = 0;
  424. vreg_cfg = get_vreg_list(&vreg_list_size, type);
  425. if (!vreg_cfg)
  426. return -EINVAL;
  427. switch (type) {
  428. case CNSS_VREG_PRIM:
  429. ret = cnss_get_vreg(plat_priv, &plat_priv->vreg_list,
  430. vreg_cfg, vreg_list_size);
  431. break;
  432. default:
  433. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  434. return -EINVAL;
  435. }
  436. return ret;
  437. }
  438. void cnss_put_vreg_type(struct cnss_plat_data *plat_priv,
  439. enum cnss_vreg_type type)
  440. {
  441. switch (type) {
  442. case CNSS_VREG_PRIM:
  443. cnss_put_vreg(plat_priv, &plat_priv->vreg_list);
  444. break;
  445. default:
  446. return;
  447. }
  448. }
  449. int cnss_vreg_on_type(struct cnss_plat_data *plat_priv,
  450. enum cnss_vreg_type type)
  451. {
  452. int ret = 0;
  453. switch (type) {
  454. case CNSS_VREG_PRIM:
  455. ret = cnss_vreg_on(plat_priv, &plat_priv->vreg_list);
  456. break;
  457. default:
  458. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  459. return -EINVAL;
  460. }
  461. return ret;
  462. }
  463. int cnss_vreg_off_type(struct cnss_plat_data *plat_priv,
  464. enum cnss_vreg_type type)
  465. {
  466. int ret = 0;
  467. switch (type) {
  468. case CNSS_VREG_PRIM:
  469. ret = cnss_vreg_off(plat_priv, &plat_priv->vreg_list);
  470. break;
  471. default:
  472. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  473. return -EINVAL;
  474. }
  475. return ret;
  476. }
  477. int cnss_vreg_unvote_type(struct cnss_plat_data *plat_priv,
  478. enum cnss_vreg_type type)
  479. {
  480. int ret = 0;
  481. switch (type) {
  482. case CNSS_VREG_PRIM:
  483. ret = cnss_vreg_unvote(plat_priv, &plat_priv->vreg_list);
  484. break;
  485. default:
  486. cnss_pr_err("Unsupported vreg type 0x%x\n", type);
  487. return -EINVAL;
  488. }
  489. return ret;
  490. }
  491. static int cnss_get_clk_single(struct cnss_plat_data *plat_priv,
  492. struct cnss_clk_info *clk_info)
  493. {
  494. struct device *dev = &plat_priv->plat_dev->dev;
  495. struct clk *clk;
  496. int ret;
  497. clk = devm_clk_get(dev, clk_info->cfg.name);
  498. if (IS_ERR(clk)) {
  499. ret = PTR_ERR(clk);
  500. if (clk_info->cfg.required)
  501. cnss_pr_err("Failed to get clock %s, err = %d\n",
  502. clk_info->cfg.name, ret);
  503. else
  504. cnss_pr_dbg("Failed to get optional clock %s, err = %d\n",
  505. clk_info->cfg.name, ret);
  506. return ret;
  507. }
  508. clk_info->clk = clk;
  509. cnss_pr_dbg("Got clock: %s, freq: %u\n",
  510. clk_info->cfg.name, clk_info->cfg.freq);
  511. return 0;
  512. }
  513. static void cnss_put_clk_single(struct cnss_plat_data *plat_priv,
  514. struct cnss_clk_info *clk_info)
  515. {
  516. struct device *dev = &plat_priv->plat_dev->dev;
  517. cnss_pr_dbg("Put clock: %s\n", clk_info->cfg.name);
  518. devm_clk_put(dev, clk_info->clk);
  519. }
  520. static int cnss_clk_on_single(struct cnss_clk_info *clk_info)
  521. {
  522. int ret;
  523. if (clk_info->enabled) {
  524. cnss_pr_dbg("Clock %s is already enabled\n",
  525. clk_info->cfg.name);
  526. return 0;
  527. }
  528. cnss_pr_dbg("Clock %s is being enabled\n", clk_info->cfg.name);
  529. if (clk_info->cfg.freq) {
  530. ret = clk_set_rate(clk_info->clk, clk_info->cfg.freq);
  531. if (ret) {
  532. cnss_pr_err("Failed to set frequency %u for clock %s, err = %d\n",
  533. clk_info->cfg.freq, clk_info->cfg.name,
  534. ret);
  535. return ret;
  536. }
  537. }
  538. ret = clk_prepare_enable(clk_info->clk);
  539. if (ret) {
  540. cnss_pr_err("Failed to enable clock %s, err = %d\n",
  541. clk_info->cfg.name, ret);
  542. return ret;
  543. }
  544. clk_info->enabled = true;
  545. return 0;
  546. }
  547. static int cnss_clk_off_single(struct cnss_clk_info *clk_info)
  548. {
  549. if (!clk_info->enabled) {
  550. cnss_pr_dbg("Clock %s is already disabled\n",
  551. clk_info->cfg.name);
  552. return 0;
  553. }
  554. cnss_pr_dbg("Clock %s is being disabled\n", clk_info->cfg.name);
  555. clk_disable_unprepare(clk_info->clk);
  556. clk_info->enabled = false;
  557. return 0;
  558. }
  559. int cnss_get_clk(struct cnss_plat_data *plat_priv)
  560. {
  561. struct device *dev;
  562. struct list_head *clk_list;
  563. struct cnss_clk_info *clk_info;
  564. int ret, i;
  565. if (!plat_priv)
  566. return -ENODEV;
  567. dev = &plat_priv->plat_dev->dev;
  568. clk_list = &plat_priv->clk_list;
  569. if (!list_empty(clk_list)) {
  570. cnss_pr_dbg("Clocks have already been updated\n");
  571. return 0;
  572. }
  573. for (i = 0; i < CNSS_CLK_INFO_SIZE; i++) {
  574. clk_info = devm_kzalloc(dev, sizeof(*clk_info), GFP_KERNEL);
  575. if (!clk_info) {
  576. ret = -ENOMEM;
  577. goto cleanup;
  578. }
  579. memcpy(&clk_info->cfg, &cnss_clk_list[i],
  580. sizeof(clk_info->cfg));
  581. ret = cnss_get_clk_single(plat_priv, clk_info);
  582. if (ret != 0) {
  583. if (clk_info->cfg.required) {
  584. devm_kfree(dev, clk_info);
  585. goto cleanup;
  586. } else {
  587. devm_kfree(dev, clk_info);
  588. continue;
  589. }
  590. }
  591. list_add_tail(&clk_info->list, clk_list);
  592. }
  593. return 0;
  594. cleanup:
  595. while (!list_empty(clk_list)) {
  596. clk_info = list_first_entry(clk_list, struct cnss_clk_info,
  597. list);
  598. list_del(&clk_info->list);
  599. if (IS_ERR_OR_NULL(clk_info->clk))
  600. continue;
  601. cnss_put_clk_single(plat_priv, clk_info);
  602. devm_kfree(dev, clk_info);
  603. }
  604. return ret;
  605. }
  606. void cnss_put_clk(struct cnss_plat_data *plat_priv)
  607. {
  608. struct device *dev;
  609. struct list_head *clk_list;
  610. struct cnss_clk_info *clk_info;
  611. if (!plat_priv)
  612. return;
  613. dev = &plat_priv->plat_dev->dev;
  614. clk_list = &plat_priv->clk_list;
  615. while (!list_empty(clk_list)) {
  616. clk_info = list_first_entry(clk_list, struct cnss_clk_info,
  617. list);
  618. list_del(&clk_info->list);
  619. if (IS_ERR_OR_NULL(clk_info->clk))
  620. continue;
  621. cnss_put_clk_single(plat_priv, clk_info);
  622. devm_kfree(dev, clk_info);
  623. }
  624. }
  625. static int cnss_clk_on(struct cnss_plat_data *plat_priv,
  626. struct list_head *clk_list)
  627. {
  628. struct cnss_clk_info *clk_info;
  629. int ret = 0;
  630. list_for_each_entry(clk_info, clk_list, list) {
  631. if (IS_ERR_OR_NULL(clk_info->clk))
  632. continue;
  633. ret = cnss_clk_on_single(clk_info);
  634. if (ret)
  635. break;
  636. }
  637. if (!ret)
  638. return 0;
  639. list_for_each_entry_continue_reverse(clk_info, clk_list, list) {
  640. if (IS_ERR_OR_NULL(clk_info->clk))
  641. continue;
  642. cnss_clk_off_single(clk_info);
  643. }
  644. return ret;
  645. }
  646. static int cnss_clk_off(struct cnss_plat_data *plat_priv,
  647. struct list_head *clk_list)
  648. {
  649. struct cnss_clk_info *clk_info;
  650. list_for_each_entry_reverse(clk_info, clk_list, list) {
  651. if (IS_ERR_OR_NULL(clk_info->clk))
  652. continue;
  653. cnss_clk_off_single(clk_info);
  654. }
  655. return 0;
  656. }
  657. int cnss_get_pinctrl(struct cnss_plat_data *plat_priv)
  658. {
  659. int ret = 0;
  660. struct device *dev;
  661. struct cnss_pinctrl_info *pinctrl_info;
  662. u32 gpio_id, i;
  663. int gpio_id_n;
  664. dev = &plat_priv->plat_dev->dev;
  665. pinctrl_info = &plat_priv->pinctrl_info;
  666. pinctrl_info->pinctrl = devm_pinctrl_get(dev);
  667. if (IS_ERR_OR_NULL(pinctrl_info->pinctrl)) {
  668. ret = PTR_ERR(pinctrl_info->pinctrl);
  669. cnss_pr_err("Failed to get pinctrl, err = %d\n", ret);
  670. goto out;
  671. }
  672. if (of_find_property(dev->of_node, BOOTSTRAP_GPIO, NULL)) {
  673. pinctrl_info->bootstrap_active =
  674. pinctrl_lookup_state(pinctrl_info->pinctrl,
  675. BOOTSTRAP_ACTIVE);
  676. if (IS_ERR_OR_NULL(pinctrl_info->bootstrap_active)) {
  677. ret = PTR_ERR(pinctrl_info->bootstrap_active);
  678. cnss_pr_err("Failed to get bootstrap active state, err = %d\n",
  679. ret);
  680. goto out;
  681. }
  682. }
  683. if (of_find_property(dev->of_node, HOST_SOL_GPIO, NULL) &&
  684. of_find_property(dev->of_node, DEV_SOL_GPIO, NULL)) {
  685. pinctrl_info->sol_default =
  686. pinctrl_lookup_state(pinctrl_info->pinctrl,
  687. SOL_DEFAULT);
  688. if (IS_ERR_OR_NULL(pinctrl_info->sol_default)) {
  689. ret = PTR_ERR(pinctrl_info->sol_default);
  690. cnss_pr_err("Failed to get sol default state, err = %d\n",
  691. ret);
  692. goto out;
  693. }
  694. cnss_pr_dbg("Got sol default state\n");
  695. }
  696. if (of_find_property(dev->of_node, WLAN_EN_GPIO, NULL)) {
  697. pinctrl_info->wlan_en_gpio = of_get_named_gpio(dev->of_node,
  698. WLAN_EN_GPIO, 0);
  699. cnss_pr_dbg("WLAN_EN GPIO: %d\n", pinctrl_info->wlan_en_gpio);
  700. pinctrl_info->wlan_en_active =
  701. pinctrl_lookup_state(pinctrl_info->pinctrl,
  702. WLAN_EN_ACTIVE);
  703. if (IS_ERR_OR_NULL(pinctrl_info->wlan_en_active)) {
  704. ret = PTR_ERR(pinctrl_info->wlan_en_active);
  705. cnss_pr_err("Failed to get wlan_en active state, err = %d\n",
  706. ret);
  707. goto out;
  708. }
  709. pinctrl_info->wlan_en_sleep =
  710. pinctrl_lookup_state(pinctrl_info->pinctrl,
  711. WLAN_EN_SLEEP);
  712. if (IS_ERR_OR_NULL(pinctrl_info->wlan_en_sleep)) {
  713. ret = PTR_ERR(pinctrl_info->wlan_en_sleep);
  714. cnss_pr_err("Failed to get wlan_en sleep state, err = %d\n",
  715. ret);
  716. goto out;
  717. }
  718. cnss_set_feature_list(plat_priv, CNSS_WLAN_EN_SUPPORT_V01);
  719. } else {
  720. pinctrl_info->wlan_en_gpio = -EINVAL;
  721. }
  722. /* Added for QCA6490 PMU delayed WLAN_EN_GPIO */
  723. if (of_find_property(dev->of_node, BT_EN_GPIO, NULL)) {
  724. pinctrl_info->bt_en_gpio = of_get_named_gpio(dev->of_node,
  725. BT_EN_GPIO, 0);
  726. cnss_pr_dbg("BT GPIO: %d\n", pinctrl_info->bt_en_gpio);
  727. } else {
  728. pinctrl_info->bt_en_gpio = -EINVAL;
  729. }
  730. /* Added for QCA6490 to minimize XO CLK selection leakage prevention */
  731. if (of_find_property(dev->of_node, XO_CLK_GPIO, NULL)) {
  732. pinctrl_info->xo_clk_gpio = of_get_named_gpio(dev->of_node,
  733. XO_CLK_GPIO, 0);
  734. cnss_pr_dbg("QCA6490 XO_CLK GPIO: %d\n",
  735. pinctrl_info->xo_clk_gpio);
  736. cnss_set_feature_list(plat_priv, BOOTSTRAP_CLOCK_SELECT_V01);
  737. } else {
  738. pinctrl_info->xo_clk_gpio = -EINVAL;
  739. }
  740. if (of_find_property(dev->of_node, SW_CTRL_GPIO, NULL)) {
  741. pinctrl_info->sw_ctrl_gpio = of_get_named_gpio(dev->of_node,
  742. SW_CTRL_GPIO,
  743. 0);
  744. cnss_pr_dbg("Switch control GPIO: %d\n",
  745. pinctrl_info->sw_ctrl_gpio);
  746. pinctrl_info->sw_ctrl =
  747. pinctrl_lookup_state(pinctrl_info->pinctrl,
  748. "sw_ctrl");
  749. if (IS_ERR_OR_NULL(pinctrl_info->sw_ctrl)) {
  750. ret = PTR_ERR(pinctrl_info->sw_ctrl);
  751. cnss_pr_dbg("Failed to get sw_ctrl state, err = %d\n",
  752. ret);
  753. } else {
  754. ret = pinctrl_select_state(pinctrl_info->pinctrl,
  755. pinctrl_info->sw_ctrl);
  756. if (ret)
  757. cnss_pr_err("Failed to select sw_ctrl state, err = %d\n",
  758. ret);
  759. }
  760. } else {
  761. pinctrl_info->sw_ctrl_gpio = -EINVAL;
  762. }
  763. if (of_find_property(dev->of_node, WLAN_SW_CTRL_GPIO, NULL)) {
  764. pinctrl_info->sw_ctrl_wl_cx =
  765. pinctrl_lookup_state(pinctrl_info->pinctrl,
  766. "sw_ctrl_wl_cx");
  767. if (IS_ERR_OR_NULL(pinctrl_info->sw_ctrl_wl_cx)) {
  768. ret = PTR_ERR(pinctrl_info->sw_ctrl_wl_cx);
  769. cnss_pr_dbg("Failed to get sw_ctrl_wl_cx state, err = %d\n",
  770. ret);
  771. } else {
  772. ret = pinctrl_select_state(pinctrl_info->pinctrl,
  773. pinctrl_info->sw_ctrl_wl_cx);
  774. if (ret)
  775. cnss_pr_err("Failed to select sw_ctrl_wl_cx state, err = %d\n",
  776. ret);
  777. }
  778. }
  779. /* Find out and configure all those GPIOs which need to be setup
  780. * for interrupt wakeup capable
  781. */
  782. gpio_id_n = of_property_count_u32_elems(dev->of_node, "mpm_wake_set_gpios");
  783. if (gpio_id_n > 0) {
  784. cnss_pr_dbg("Num of GPIOs to be setup for interrupt wakeup capable: %d\n",
  785. gpio_id_n);
  786. for (i = 0; i < gpio_id_n; i++) {
  787. ret = of_property_read_u32_index(dev->of_node,
  788. "mpm_wake_set_gpios",
  789. i, &gpio_id);
  790. if (ret) {
  791. cnss_pr_err("Failed to read gpio_id at index: %d\n", i);
  792. continue;
  793. }
  794. ret = msm_gpio_mpm_wake_set(gpio_id, 1);
  795. if (ret < 0) {
  796. cnss_pr_err("Failed to setup gpio_id: %d as interrupt wakeup capable, ret: %d\n",
  797. ret);
  798. } else {
  799. cnss_pr_dbg("gpio_id: %d successfully setup for interrupt wakeup capable\n",
  800. gpio_id);
  801. }
  802. }
  803. } else {
  804. cnss_pr_dbg("No GPIOs to be setup for interrupt wakeup capable\n");
  805. }
  806. return 0;
  807. out:
  808. return ret;
  809. }
  810. int cnss_get_wlan_sw_ctrl(struct cnss_plat_data *plat_priv)
  811. {
  812. struct device *dev;
  813. struct cnss_pinctrl_info *pinctrl_info;
  814. dev = &plat_priv->plat_dev->dev;
  815. pinctrl_info = &plat_priv->pinctrl_info;
  816. if (of_find_property(dev->of_node, WLAN_SW_CTRL_GPIO, NULL)) {
  817. pinctrl_info->wlan_sw_ctrl_gpio = of_get_named_gpio(dev->of_node,
  818. WLAN_SW_CTRL_GPIO,
  819. 0);
  820. cnss_pr_dbg("WLAN Switch control GPIO: %d\n",
  821. pinctrl_info->wlan_sw_ctrl_gpio);
  822. } else {
  823. pinctrl_info->wlan_sw_ctrl_gpio = -EINVAL;
  824. }
  825. return 0;
  826. }
  827. #define CNSS_XO_CLK_RETRY_COUNT_MAX 5
  828. static void cnss_set_xo_clk_gpio_state(struct cnss_plat_data *plat_priv,
  829. bool enable)
  830. {
  831. int xo_clk_gpio = plat_priv->pinctrl_info.xo_clk_gpio, retry = 0, ret;
  832. if (xo_clk_gpio < 0 || plat_priv->device_id != QCA6490_DEVICE_ID)
  833. return;
  834. retry_gpio_req:
  835. ret = gpio_request(xo_clk_gpio, "XO_CLK_GPIO");
  836. if (ret) {
  837. if (retry++ < CNSS_XO_CLK_RETRY_COUNT_MAX) {
  838. /* wait for ~(10 - 20) ms */
  839. usleep_range(10000, 20000);
  840. goto retry_gpio_req;
  841. }
  842. }
  843. if (ret) {
  844. cnss_pr_err("QCA6490 XO CLK Gpio request failed\n");
  845. return;
  846. }
  847. if (enable) {
  848. gpio_direction_output(xo_clk_gpio, 1);
  849. /*XO CLK must be asserted for some time before WLAN_EN */
  850. usleep_range(100, 200);
  851. } else {
  852. /* Assert XO CLK ~(2-5)ms before off for valid latch in HW */
  853. usleep_range(2000, 5000);
  854. gpio_direction_output(xo_clk_gpio, 0);
  855. }
  856. gpio_free(xo_clk_gpio);
  857. }
  858. static int cnss_select_pinctrl_state(struct cnss_plat_data *plat_priv,
  859. bool state)
  860. {
  861. int ret = 0;
  862. struct cnss_pinctrl_info *pinctrl_info;
  863. if (!plat_priv) {
  864. cnss_pr_err("plat_priv is NULL!\n");
  865. ret = -ENODEV;
  866. goto out;
  867. }
  868. pinctrl_info = &plat_priv->pinctrl_info;
  869. if (state) {
  870. if (!IS_ERR_OR_NULL(pinctrl_info->bootstrap_active)) {
  871. ret = pinctrl_select_state
  872. (pinctrl_info->pinctrl,
  873. pinctrl_info->bootstrap_active);
  874. if (ret) {
  875. cnss_pr_err("Failed to select bootstrap active state, err = %d\n",
  876. ret);
  877. goto out;
  878. }
  879. udelay(BOOTSTRAP_DELAY);
  880. }
  881. if (!IS_ERR_OR_NULL(pinctrl_info->sol_default)) {
  882. ret = pinctrl_select_state
  883. (pinctrl_info->pinctrl,
  884. pinctrl_info->sol_default);
  885. if (ret) {
  886. cnss_pr_err("Failed to select sol default state, err = %d\n",
  887. ret);
  888. goto out;
  889. }
  890. cnss_pr_dbg("Selected sol default state\n");
  891. }
  892. cnss_set_xo_clk_gpio_state(plat_priv, true);
  893. if (!IS_ERR_OR_NULL(pinctrl_info->wlan_en_active)) {
  894. ret = pinctrl_select_state
  895. (pinctrl_info->pinctrl,
  896. pinctrl_info->wlan_en_active);
  897. if (ret) {
  898. cnss_pr_err("Failed to select wlan_en active state, err = %d\n",
  899. ret);
  900. goto out;
  901. }
  902. if (plat_priv->device_id == QCA6174_DEVICE_ID ||
  903. plat_priv->device_id == 0)
  904. mdelay(WLAN_ENABLE_DELAY_ROME);
  905. else
  906. udelay(WLAN_ENABLE_DELAY);
  907. cnss_set_xo_clk_gpio_state(plat_priv, false);
  908. } else {
  909. cnss_set_xo_clk_gpio_state(plat_priv, false);
  910. goto out;
  911. }
  912. } else {
  913. if (!IS_ERR_OR_NULL(pinctrl_info->wlan_en_sleep)) {
  914. cnss_wlan_hw_disable_check(plat_priv);
  915. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  916. cnss_pr_dbg("Avoid WLAN_EN low. WLAN HW Disbaled");
  917. goto out;
  918. }
  919. ret = pinctrl_select_state(pinctrl_info->pinctrl,
  920. pinctrl_info->wlan_en_sleep);
  921. if (ret) {
  922. cnss_pr_err("Failed to select wlan_en sleep state, err = %d\n",
  923. ret);
  924. goto out;
  925. }
  926. } else {
  927. goto out;
  928. }
  929. }
  930. cnss_pr_dbg("WLAN_EN Value: %d\n", gpio_get_value(pinctrl_info->wlan_en_gpio));
  931. cnss_pr_dbg("%s WLAN_EN GPIO successfully\n",
  932. state ? "Assert" : "De-assert");
  933. return 0;
  934. out:
  935. return ret;
  936. }
  937. /**
  938. * cnss_select_pinctrl_enable - select WLAN_GPIO for Active pinctrl status
  939. * @plat_priv: Platform private data structure pointer
  940. *
  941. * For QCA6490, PMU requires minimum 100ms delay between BT_EN_GPIO off and
  942. * WLAN_EN_GPIO on. This is done to avoid power up issues.
  943. *
  944. * Return: Status of pinctrl select operation. 0 - Success.
  945. */
  946. static int cnss_select_pinctrl_enable(struct cnss_plat_data *plat_priv)
  947. {
  948. int ret = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  949. u8 wlan_en_state = 0;
  950. if (bt_en_gpio < 0 || plat_priv->device_id != QCA6490_DEVICE_ID)
  951. goto set_wlan_en;
  952. if (gpio_get_value(bt_en_gpio)) {
  953. cnss_pr_dbg("BT_EN_GPIO State: On\n");
  954. ret = cnss_select_pinctrl_state(plat_priv, true);
  955. if (!ret)
  956. return ret;
  957. wlan_en_state = 1;
  958. }
  959. if (!gpio_get_value(bt_en_gpio)) {
  960. cnss_pr_dbg("BT_EN_GPIO State: Off. Delay WLAN_GPIO enable\n");
  961. /* check for BT_EN_GPIO down race during above operation */
  962. if (wlan_en_state) {
  963. cnss_pr_dbg("Reset WLAN_EN as BT got turned off during enable\n");
  964. cnss_select_pinctrl_state(plat_priv, false);
  965. wlan_en_state = 0;
  966. }
  967. /* 100 ms delay for BT_EN and WLAN_EN QCA6490 PMU sequencing */
  968. msleep(100);
  969. }
  970. set_wlan_en:
  971. if (!wlan_en_state)
  972. ret = cnss_select_pinctrl_state(plat_priv, true);
  973. return ret;
  974. }
  975. int cnss_get_input_gpio_value(struct cnss_plat_data *plat_priv, int gpio_num)
  976. {
  977. int ret;
  978. if (gpio_num < 0)
  979. return -EINVAL;
  980. ret = gpio_direction_input(gpio_num);
  981. if (ret) {
  982. cnss_pr_err("Failed to set direction of GPIO(%d), err = %d",
  983. gpio_num, ret);
  984. return -EINVAL;
  985. }
  986. return gpio_get_value(gpio_num);
  987. }
  988. int cnss_power_on_device(struct cnss_plat_data *plat_priv, bool reset)
  989. {
  990. int ret = 0;
  991. if (plat_priv->powered_on) {
  992. cnss_pr_dbg("Already powered up");
  993. return 0;
  994. }
  995. cnss_wlan_hw_disable_check(plat_priv);
  996. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  997. cnss_pr_dbg("Avoid WLAN Power On. WLAN HW Disbaled");
  998. return -EINVAL;
  999. }
  1000. ret = cnss_vreg_on_type(plat_priv, CNSS_VREG_PRIM);
  1001. if (ret) {
  1002. cnss_pr_err("Failed to turn on vreg, err = %d\n", ret);
  1003. goto out;
  1004. }
  1005. ret = cnss_clk_on(plat_priv, &plat_priv->clk_list);
  1006. if (ret) {
  1007. cnss_pr_err("Failed to turn on clocks, err = %d\n", ret);
  1008. goto vreg_off;
  1009. }
  1010. #ifdef CONFIG_PULLDOWN_WLANEN
  1011. if (reset) {
  1012. /* The default state of wlan_en maybe not low,
  1013. * according to datasheet, we should put wlan_en
  1014. * to low first, and trigger high.
  1015. * And the default delay for qca6390 is at least 4ms,
  1016. * for qcn7605/qca6174, it is 10us. For safe, set 5ms delay
  1017. * here.
  1018. */
  1019. ret = cnss_select_pinctrl_state(plat_priv, false);
  1020. if (ret) {
  1021. cnss_pr_err("Failed to select pinctrl state, err = %d\n",
  1022. ret);
  1023. goto clk_off;
  1024. }
  1025. usleep_range(4000, 5000);
  1026. }
  1027. #endif
  1028. ret = cnss_select_pinctrl_enable(plat_priv);
  1029. if (ret) {
  1030. cnss_pr_err("Failed to select pinctrl state, err = %d\n", ret);
  1031. goto clk_off;
  1032. }
  1033. plat_priv->powered_on = true;
  1034. clear_bit(CNSS_POWER_OFF, &plat_priv->driver_state);
  1035. cnss_enable_dev_sol_irq(plat_priv);
  1036. cnss_set_host_sol_value(plat_priv, 0);
  1037. return 0;
  1038. clk_off:
  1039. cnss_clk_off(plat_priv, &plat_priv->clk_list);
  1040. vreg_off:
  1041. cnss_vreg_off_type(plat_priv, CNSS_VREG_PRIM);
  1042. out:
  1043. return ret;
  1044. }
  1045. void cnss_power_off_device(struct cnss_plat_data *plat_priv)
  1046. {
  1047. if (!plat_priv->powered_on) {
  1048. cnss_pr_dbg("Already powered down");
  1049. return;
  1050. }
  1051. set_bit(CNSS_POWER_OFF, &plat_priv->driver_state);
  1052. cnss_bus_shutdown_cleanup(plat_priv);
  1053. cnss_disable_dev_sol_irq(plat_priv);
  1054. cnss_select_pinctrl_state(plat_priv, false);
  1055. cnss_clk_off(plat_priv, &plat_priv->clk_list);
  1056. cnss_vreg_off_type(plat_priv, CNSS_VREG_PRIM);
  1057. plat_priv->powered_on = false;
  1058. }
  1059. bool cnss_is_device_powered_on(struct cnss_plat_data *plat_priv)
  1060. {
  1061. return plat_priv->powered_on;
  1062. }
  1063. void cnss_set_pin_connect_status(struct cnss_plat_data *plat_priv)
  1064. {
  1065. unsigned long pin_status = 0;
  1066. set_bit(CNSS_WLAN_EN, &pin_status);
  1067. set_bit(CNSS_PCIE_TXN, &pin_status);
  1068. set_bit(CNSS_PCIE_TXP, &pin_status);
  1069. set_bit(CNSS_PCIE_RXN, &pin_status);
  1070. set_bit(CNSS_PCIE_RXP, &pin_status);
  1071. set_bit(CNSS_PCIE_REFCLKN, &pin_status);
  1072. set_bit(CNSS_PCIE_REFCLKP, &pin_status);
  1073. set_bit(CNSS_PCIE_RST, &pin_status);
  1074. plat_priv->pin_result.host_pin_result = pin_status;
  1075. }
  1076. #if IS_ENABLED(CONFIG_QCOM_COMMAND_DB)
  1077. static int cnss_cmd_db_ready(struct cnss_plat_data *plat_priv)
  1078. {
  1079. return cmd_db_ready();
  1080. }
  1081. static u32 cnss_cmd_db_read_addr(struct cnss_plat_data *plat_priv,
  1082. const char *res_id)
  1083. {
  1084. return cmd_db_read_addr(res_id);
  1085. }
  1086. #else
  1087. static int cnss_cmd_db_ready(struct cnss_plat_data *plat_priv)
  1088. {
  1089. return -EOPNOTSUPP;
  1090. }
  1091. static u32 cnss_cmd_db_read_addr(struct cnss_plat_data *plat_priv,
  1092. const char *res_id)
  1093. {
  1094. return 0;
  1095. }
  1096. #endif
  1097. int cnss_get_tcs_info(struct cnss_plat_data *plat_priv)
  1098. {
  1099. struct platform_device *plat_dev = plat_priv->plat_dev;
  1100. struct resource *res;
  1101. resource_size_t addr_len;
  1102. void __iomem *tcs_cmd_base_addr;
  1103. int ret = 0;
  1104. res = platform_get_resource_byname(plat_dev, IORESOURCE_MEM, "tcs_cmd");
  1105. if (!res) {
  1106. cnss_pr_dbg("TCS CMD address is not present for CPR\n");
  1107. goto out;
  1108. }
  1109. plat_priv->tcs_info.cmd_base_addr = res->start;
  1110. addr_len = resource_size(res);
  1111. cnss_pr_dbg("TCS CMD base address is %pa with length %pa\n",
  1112. &plat_priv->tcs_info.cmd_base_addr, &addr_len);
  1113. tcs_cmd_base_addr = devm_ioremap(&plat_dev->dev, res->start, addr_len);
  1114. if (!tcs_cmd_base_addr) {
  1115. ret = -EINVAL;
  1116. cnss_pr_err("Failed to map TCS CMD address, err = %d\n",
  1117. ret);
  1118. goto out;
  1119. }
  1120. plat_priv->tcs_info.cmd_base_addr_io = tcs_cmd_base_addr;
  1121. return 0;
  1122. out:
  1123. return ret;
  1124. }
  1125. int cnss_get_cpr_info(struct cnss_plat_data *plat_priv)
  1126. {
  1127. struct platform_device *plat_dev = plat_priv->plat_dev;
  1128. struct cnss_cpr_info *cpr_info = &plat_priv->cpr_info;
  1129. const char *cmd_db_name;
  1130. u32 cpr_pmic_addr = 0;
  1131. int ret = 0;
  1132. if (plat_priv->tcs_info.cmd_base_addr == 0) {
  1133. cnss_pr_dbg("TCS CMD not configured\n");
  1134. return 0;
  1135. }
  1136. ret = of_property_read_string(plat_dev->dev.of_node,
  1137. "qcom,cmd_db_name", &cmd_db_name);
  1138. if (ret) {
  1139. cnss_pr_dbg("CommandDB name is not present for CPR\n");
  1140. goto out;
  1141. }
  1142. ret = cnss_cmd_db_ready(plat_priv);
  1143. if (ret) {
  1144. cnss_pr_err("CommandDB is not ready, err = %d\n", ret);
  1145. goto out;
  1146. }
  1147. cpr_pmic_addr = cnss_cmd_db_read_addr(plat_priv, cmd_db_name);
  1148. if (cpr_pmic_addr > 0) {
  1149. cpr_info->cpr_pmic_addr = cpr_pmic_addr;
  1150. cnss_pr_dbg("Get CPR PMIC address 0x%x from %s\n",
  1151. cpr_info->cpr_pmic_addr, cmd_db_name);
  1152. } else {
  1153. cnss_pr_err("CPR PMIC address is not available for %s\n",
  1154. cmd_db_name);
  1155. ret = -EINVAL;
  1156. goto out;
  1157. }
  1158. return 0;
  1159. out:
  1160. return ret;
  1161. }
  1162. #if IS_ENABLED(CONFIG_MSM_QMP)
  1163. /**
  1164. * cnss_mbox_init: Initialize mbox interface
  1165. * @plat_priv: Pointer to cnss platform data
  1166. *
  1167. * Try to get property 'mboxes' from device tree and
  1168. * initialize the interface for AOP configuration.
  1169. *
  1170. * Return: 0 for success, otherwise error code
  1171. */
  1172. static int cnss_mbox_init(struct cnss_plat_data *plat_priv)
  1173. {
  1174. struct mbox_client *mbox = &plat_priv->mbox_client_data;
  1175. struct mbox_chan *chan;
  1176. int ret = 0;
  1177. plat_priv->mbox_chan = NULL;
  1178. mbox->dev = &plat_priv->plat_dev->dev;
  1179. mbox->tx_block = true;
  1180. mbox->tx_tout = CNSS_MBOX_TIMEOUT_MS;
  1181. mbox->knows_txdone = false;
  1182. chan = mbox_request_channel(mbox, 0);
  1183. if (IS_ERR(chan)) {
  1184. ret = PTR_ERR(chan);
  1185. cnss_pr_dbg("Failed to get mbox channel[%d]\n", ret);
  1186. } else {
  1187. plat_priv->mbox_chan = chan;
  1188. cnss_pr_dbg("Mbox channel initialized\n");
  1189. }
  1190. return ret;
  1191. }
  1192. /**
  1193. * cnss_mbox_deinit: De-Initialize mbox interface
  1194. * @plat_priv: Pointer to cnss platform data
  1195. *
  1196. * Return: None
  1197. */
  1198. static void cnss_mbox_deinit(struct cnss_plat_data *plat_priv)
  1199. {
  1200. if (!plat_priv->mbox_chan) {
  1201. mbox_free_channel(plat_priv->mbox_chan);
  1202. plat_priv->mbox_chan = NULL;
  1203. }
  1204. }
  1205. /**
  1206. * cnss_mbox_send_msg: Send json message to AOP using mbox channel
  1207. * @plat_priv: Pointer to cnss platform data
  1208. * @msg: String in json format
  1209. *
  1210. * Return: 0 for success, otherwise error code
  1211. */
  1212. static int
  1213. cnss_mbox_send_msg(struct cnss_plat_data *plat_priv, char *mbox_msg)
  1214. {
  1215. struct qmp_pkt pkt;
  1216. int ret = 0;
  1217. if (!plat_priv->mbox_chan)
  1218. return -ENODEV;
  1219. cnss_pr_dbg("Sending AOP Mbox msg: %s\n", mbox_msg);
  1220. pkt.size = CNSS_MBOX_MSG_MAX_LEN;
  1221. pkt.data = mbox_msg;
  1222. ret = mbox_send_message(plat_priv->mbox_chan, &pkt);
  1223. if (ret < 0)
  1224. cnss_pr_err("Failed to send AOP mbox msg: %s\n", mbox_msg);
  1225. return ret;
  1226. }
  1227. #else
  1228. static inline int cnss_mbox_init(struct cnss_plat_data *plat_priv)
  1229. {
  1230. return -EOPNOTSUPP;
  1231. }
  1232. static inline void cnss_mbox_deinit(struct cnss_plat_data *plat_priv)
  1233. {
  1234. }
  1235. static inline int
  1236. cnss_mbox_send_msg(struct cnss_plat_data *plat_priv, char *mbox_msg)
  1237. {
  1238. return -EOPNOTSUPP;
  1239. }
  1240. #endif
  1241. /**
  1242. * cnss_qmp_init: Initialize direct QMP interface
  1243. * @plat_priv: Pointer to cnss platform data
  1244. *
  1245. * Try to get property 'qcom,qmp' from device tree and
  1246. * initialize the interface for AOP configuration.
  1247. *
  1248. * Return: 0 for success, otherwise error code
  1249. */
  1250. static int cnss_qmp_init(struct cnss_plat_data *plat_priv)
  1251. {
  1252. struct qmp *qmp;
  1253. plat_priv->qmp = NULL;
  1254. qmp = qmp_get(&plat_priv->plat_dev->dev);
  1255. if (IS_ERR(qmp)) {
  1256. cnss_pr_err("Failed to get qmp: %d\n",
  1257. PTR_ERR(qmp));
  1258. return PTR_ERR(qmp);
  1259. }
  1260. plat_priv->qmp = qmp;
  1261. cnss_pr_dbg("QMP initialized\n");
  1262. return 0;
  1263. }
  1264. /**
  1265. * cnss_qmp_deinit: De-Initialize direct QMP interface
  1266. * @plat_priv: Pointer to cnss platform data
  1267. *
  1268. * Return: None
  1269. */
  1270. static void cnss_qmp_deinit(struct cnss_plat_data *plat_priv)
  1271. {
  1272. if (plat_priv->qmp) {
  1273. qmp_put(plat_priv->qmp);
  1274. plat_priv->qmp = NULL;
  1275. }
  1276. }
  1277. /**
  1278. * cnss_qmp_send_msg: Send json message to AOP using direct QMP
  1279. * @plat_priv: Pointer to cnss platform data
  1280. * @msg: String in json format
  1281. *
  1282. * Return: 0 for success, otherwise error code
  1283. */
  1284. static int
  1285. cnss_qmp_send_msg(struct cnss_plat_data *plat_priv, char *mbox_msg)
  1286. {
  1287. int ret;
  1288. if (!plat_priv->qmp)
  1289. return -ENODEV;
  1290. cnss_pr_dbg("Sending AOP QMP msg: %s\n", mbox_msg);
  1291. ret = qmp_send(plat_priv->qmp, mbox_msg, CNSS_MBOX_MSG_MAX_LEN);
  1292. if (ret)
  1293. cnss_pr_err("Failed to send AOP QMP msg: %d[%s]\n", ret, mbox_msg);
  1294. return ret;
  1295. }
  1296. /**
  1297. * cnss_aop_interface_init: Initialize AOP interface: either mbox channel or direct QMP
  1298. * @plat_priv: Pointer to cnss platform data
  1299. *
  1300. * Device tree file should have either mbox or qmp configured, but not both.
  1301. * Based on device tree configuration setup mbox channel or QMP
  1302. *
  1303. * Return: 0 for success, otherwise error code
  1304. */
  1305. int cnss_aop_interface_init(struct cnss_plat_data *plat_priv)
  1306. {
  1307. int ret;
  1308. /* First try to get mbox channel, if it fails then try qmp_get
  1309. * In device tree file there should be either mboxes or qmp,
  1310. * cannot have both properties at the same time.
  1311. */
  1312. ret = cnss_mbox_init(plat_priv);
  1313. if (ret) {
  1314. ret = cnss_qmp_init(plat_priv);
  1315. if (ret)
  1316. return ret;
  1317. }
  1318. ret = cnss_aop_pdc_reconfig(plat_priv);
  1319. if (ret)
  1320. cnss_pr_err("Failed to reconfig WLAN PDC, err = %d\n", ret);
  1321. return ret;
  1322. }
  1323. /**
  1324. * cnss_aop_interface_deinit: Cleanup AOP interface
  1325. * @plat_priv: Pointer to cnss platform data
  1326. *
  1327. * Cleanup mbox channel or QMP whichever was configured during initialization.
  1328. *
  1329. * Return: None
  1330. */
  1331. void cnss_aop_interface_deinit(struct cnss_plat_data *plat_priv)
  1332. {
  1333. cnss_mbox_deinit(plat_priv);
  1334. cnss_qmp_deinit(plat_priv);
  1335. }
  1336. /**
  1337. * cnss_aop_send_msg: Sends json message to AOP using either mbox channel or direct QMP
  1338. * @plat_priv: Pointer to cnss platform data
  1339. * @msg: String in json format
  1340. *
  1341. * AOP accepts JSON message to configure WLAN resources. Format as follows:
  1342. * To send VReg config: {class: wlan_pdc, ss: <pdc_name>,
  1343. * res: <VReg_name>.<param>, <seq_param>: <value>}
  1344. * To send PDC Config: {class: wlan_pdc, ss: <pdc_name>, res: pdc,
  1345. * enable: <Value>}
  1346. * QMP returns timeout error if format not correct or AOP operation fails.
  1347. *
  1348. * Return: 0 for success
  1349. */
  1350. int cnss_aop_send_msg(struct cnss_plat_data *plat_priv, char *mbox_msg)
  1351. {
  1352. int ret;
  1353. ret = cnss_mbox_send_msg(plat_priv, mbox_msg);
  1354. if (ret)
  1355. ret = cnss_qmp_send_msg(plat_priv, mbox_msg);
  1356. if (ret)
  1357. cnss_pr_err("Failed to send AOP msg: %d\n", ret);
  1358. return ret;
  1359. }
  1360. static inline bool cnss_aop_interface_ready(struct cnss_plat_data *plat_priv)
  1361. {
  1362. return (plat_priv->mbox_chan || plat_priv->qmp);
  1363. }
  1364. /* cnss_pdc_reconfig: Send PDC init table as configured in DT for wlan device */
  1365. int cnss_aop_pdc_reconfig(struct cnss_plat_data *plat_priv)
  1366. {
  1367. u32 i;
  1368. int ret;
  1369. if (plat_priv->pdc_init_table_len <= 0 || !plat_priv->pdc_init_table)
  1370. return 0;
  1371. cnss_pr_dbg("Setting PDC defaults for device ID: %d\n",
  1372. plat_priv->device_id);
  1373. for (i = 0; i < plat_priv->pdc_init_table_len; i++) {
  1374. ret = cnss_aop_send_msg(plat_priv,
  1375. (char *)plat_priv->pdc_init_table[i]);
  1376. if (ret < 0)
  1377. break;
  1378. }
  1379. return ret;
  1380. }
  1381. /* cnss_aop_pdc_name_str: Get PDC name corresponding to VReg from DT Mapiping */
  1382. static const char *cnss_aop_pdc_name_str(struct cnss_plat_data *plat_priv,
  1383. const char *vreg_name)
  1384. {
  1385. u32 i;
  1386. static const char * const aop_pdc_ss_str[] = {"rf", "bb"};
  1387. const char *pdc = aop_pdc_ss_str[0], *vreg_map_name;
  1388. if (plat_priv->vreg_pdc_map_len <= 0 || !plat_priv->vreg_pdc_map)
  1389. goto end;
  1390. for (i = 0; i < plat_priv->vreg_pdc_map_len; i++) {
  1391. vreg_map_name = plat_priv->vreg_pdc_map[i];
  1392. if (strnstr(vreg_map_name, vreg_name, strlen(vreg_map_name))) {
  1393. pdc = plat_priv->vreg_pdc_map[i + 1];
  1394. break;
  1395. }
  1396. }
  1397. end:
  1398. cnss_pr_dbg("%s mapped to %s\n", vreg_name, pdc);
  1399. return pdc;
  1400. }
  1401. static int cnss_aop_set_vreg_param(struct cnss_plat_data *plat_priv,
  1402. const char *vreg_name,
  1403. enum cnss_aop_vreg_param param,
  1404. enum cnss_aop_tcs_seq_param seq_param,
  1405. int val)
  1406. {
  1407. char msg[CNSS_MBOX_MSG_MAX_LEN];
  1408. static const char * const aop_vreg_param_str[] = {
  1409. [CNSS_VREG_VOLTAGE] = "v", [CNSS_VREG_MODE] = "m",
  1410. [CNSS_VREG_ENABLE] = "e",};
  1411. static const char * const aop_tcs_seq_str[] = {
  1412. [CNSS_TCS_UP_SEQ] = "upval", [CNSS_TCS_DOWN_SEQ] = "dwnval",
  1413. [CNSS_TCS_ENABLE_SEQ] = "enable",};
  1414. if (param >= CNSS_VREG_PARAM_MAX || seq_param >= CNSS_TCS_SEQ_MAX ||
  1415. !vreg_name)
  1416. return -EINVAL;
  1417. snprintf(msg, CNSS_MBOX_MSG_MAX_LEN,
  1418. "{class: wlan_pdc, ss: %s, res: %s.%s, %s: %d}",
  1419. cnss_aop_pdc_name_str(plat_priv, vreg_name),
  1420. vreg_name, aop_vreg_param_str[param],
  1421. aop_tcs_seq_str[seq_param], val);
  1422. return cnss_aop_send_msg(plat_priv, msg);
  1423. }
  1424. int cnss_aop_ol_cpr_cfg_setup(struct cnss_plat_data *plat_priv,
  1425. struct wlfw_pmu_cfg_v01 *fw_pmu_cfg)
  1426. {
  1427. const char *pmu_pin, *vreg;
  1428. struct wlfw_pmu_param_v01 *fw_pmu_param;
  1429. u32 fw_pmu_param_len, i, j, plat_vreg_param_len = 0;
  1430. int ret = 0;
  1431. struct platform_vreg_param {
  1432. char vreg[MAX_PROP_SIZE];
  1433. u32 wake_volt;
  1434. u32 sleep_volt;
  1435. } plat_vreg_param[QMI_WLFW_PMU_PARAMS_MAX_V01] = {0};
  1436. static bool config_done;
  1437. if (config_done)
  1438. return 0;
  1439. if (plat_priv->pmu_vreg_map_len <= 0 ||
  1440. !plat_priv->pmu_vreg_map ||
  1441. (!plat_priv->mbox_chan && !plat_priv->qmp)) {
  1442. cnss_pr_dbg("Mbox channel / QMP / PMU VReg Map not configured\n");
  1443. goto end;
  1444. }
  1445. if (!fw_pmu_cfg)
  1446. return -EINVAL;
  1447. fw_pmu_param = fw_pmu_cfg->pmu_param;
  1448. fw_pmu_param_len = fw_pmu_cfg->pmu_param_len;
  1449. /* Get PMU Pin name to Platfom Vreg Mapping */
  1450. for (i = 0; i < fw_pmu_param_len; i++) {
  1451. cnss_pr_dbg("FW_PMU Data: %s %d %d %d %d\n",
  1452. fw_pmu_param[i].pin_name,
  1453. fw_pmu_param[i].wake_volt_valid,
  1454. fw_pmu_param[i].wake_volt,
  1455. fw_pmu_param[i].sleep_volt_valid,
  1456. fw_pmu_param[i].sleep_volt);
  1457. if (!fw_pmu_param[i].wake_volt_valid &&
  1458. !fw_pmu_param[i].sleep_volt_valid)
  1459. continue;
  1460. vreg = NULL;
  1461. for (j = 0; j < plat_priv->pmu_vreg_map_len; j += 2) {
  1462. pmu_pin = plat_priv->pmu_vreg_map[j];
  1463. if (strnstr(pmu_pin, fw_pmu_param[i].pin_name,
  1464. strlen(pmu_pin))) {
  1465. vreg = plat_priv->pmu_vreg_map[j + 1];
  1466. break;
  1467. }
  1468. }
  1469. if (!vreg) {
  1470. cnss_pr_err("No VREG mapping for %s\n",
  1471. fw_pmu_param[i].pin_name);
  1472. continue;
  1473. } else {
  1474. cnss_pr_dbg("%s mapped to %s\n",
  1475. fw_pmu_param[i].pin_name, vreg);
  1476. }
  1477. for (j = 0; j < QMI_WLFW_PMU_PARAMS_MAX_V01; j++) {
  1478. u32 wake_volt = 0, sleep_volt = 0;
  1479. if (plat_vreg_param[j].vreg[0] == '\0')
  1480. strlcpy(plat_vreg_param[j].vreg, vreg,
  1481. sizeof(plat_vreg_param[j].vreg));
  1482. else if (!strnstr(plat_vreg_param[j].vreg, vreg,
  1483. strlen(plat_vreg_param[j].vreg)))
  1484. continue;
  1485. if (fw_pmu_param[i].wake_volt_valid)
  1486. wake_volt = roundup(fw_pmu_param[i].wake_volt,
  1487. CNSS_PMIC_VOLTAGE_STEP) -
  1488. CNSS_PMIC_AUTO_HEADROOM +
  1489. CNSS_IR_DROP_WAKE;
  1490. if (fw_pmu_param[i].sleep_volt_valid)
  1491. sleep_volt = roundup(fw_pmu_param[i].sleep_volt,
  1492. CNSS_PMIC_VOLTAGE_STEP) -
  1493. CNSS_PMIC_AUTO_HEADROOM +
  1494. CNSS_IR_DROP_SLEEP;
  1495. plat_vreg_param[j].wake_volt =
  1496. (wake_volt > plat_vreg_param[j].wake_volt ?
  1497. wake_volt : plat_vreg_param[j].wake_volt);
  1498. plat_vreg_param[j].sleep_volt =
  1499. (sleep_volt > plat_vreg_param[j].sleep_volt ?
  1500. sleep_volt : plat_vreg_param[j].sleep_volt);
  1501. plat_vreg_param_len = (plat_vreg_param_len > j ?
  1502. plat_vreg_param_len : j);
  1503. cnss_pr_dbg("Plat VReg Data: %s %d %d\n",
  1504. plat_vreg_param[j].vreg,
  1505. plat_vreg_param[j].wake_volt,
  1506. plat_vreg_param[j].sleep_volt);
  1507. break;
  1508. }
  1509. }
  1510. for (i = 0; i <= plat_vreg_param_len; i++) {
  1511. if (plat_vreg_param[i].wake_volt > 0) {
  1512. ret =
  1513. cnss_aop_set_vreg_param(plat_priv,
  1514. plat_vreg_param[i].vreg,
  1515. CNSS_VREG_VOLTAGE,
  1516. CNSS_TCS_UP_SEQ,
  1517. plat_vreg_param[i].wake_volt);
  1518. }
  1519. if (plat_vreg_param[i].sleep_volt > 0) {
  1520. ret =
  1521. cnss_aop_set_vreg_param(plat_priv,
  1522. plat_vreg_param[i].vreg,
  1523. CNSS_VREG_VOLTAGE,
  1524. CNSS_TCS_DOWN_SEQ,
  1525. plat_vreg_param[i].sleep_volt);
  1526. }
  1527. if (ret < 0)
  1528. break;
  1529. }
  1530. end:
  1531. config_done = true;
  1532. return ret;
  1533. }
  1534. void cnss_power_misc_params_init(struct cnss_plat_data *plat_priv)
  1535. {
  1536. struct device *dev = &plat_priv->plat_dev->dev;
  1537. int ret;
  1538. u32 cfg_arr_size = 0, *cfg_arr = NULL;
  1539. /* common DT Entries */
  1540. plat_priv->pdc_init_table_len =
  1541. of_property_count_strings(dev->of_node,
  1542. "qcom,pdc_init_table");
  1543. if (plat_priv->pdc_init_table_len > 0) {
  1544. plat_priv->pdc_init_table =
  1545. kcalloc(plat_priv->pdc_init_table_len,
  1546. sizeof(char *), GFP_KERNEL);
  1547. if (plat_priv->pdc_init_table) {
  1548. ret = of_property_read_string_array(dev->of_node,
  1549. "qcom,pdc_init_table",
  1550. plat_priv->pdc_init_table,
  1551. plat_priv->pdc_init_table_len);
  1552. if (ret < 0)
  1553. cnss_pr_err("Failed to get PDC Init Table\n");
  1554. } else {
  1555. cnss_pr_err("Failed to alloc PDC Init Table mem\n");
  1556. }
  1557. } else {
  1558. cnss_pr_dbg("PDC Init Table not configured\n");
  1559. }
  1560. plat_priv->vreg_pdc_map_len =
  1561. of_property_count_strings(dev->of_node,
  1562. "qcom,vreg_pdc_map");
  1563. if (plat_priv->vreg_pdc_map_len > 0) {
  1564. plat_priv->vreg_pdc_map =
  1565. kcalloc(plat_priv->vreg_pdc_map_len,
  1566. sizeof(char *), GFP_KERNEL);
  1567. if (plat_priv->vreg_pdc_map) {
  1568. ret = of_property_read_string_array(dev->of_node,
  1569. "qcom,vreg_pdc_map",
  1570. plat_priv->vreg_pdc_map,
  1571. plat_priv->vreg_pdc_map_len);
  1572. if (ret < 0)
  1573. cnss_pr_err("Failed to get VReg PDC Mapping\n");
  1574. } else {
  1575. cnss_pr_err("Failed to alloc VReg PDC mem\n");
  1576. }
  1577. } else {
  1578. cnss_pr_dbg("VReg PDC Mapping not configured\n");
  1579. }
  1580. plat_priv->pmu_vreg_map_len =
  1581. of_property_count_strings(dev->of_node,
  1582. "qcom,pmu_vreg_map");
  1583. if (plat_priv->pmu_vreg_map_len > 0) {
  1584. plat_priv->pmu_vreg_map = kcalloc(plat_priv->pmu_vreg_map_len,
  1585. sizeof(char *), GFP_KERNEL);
  1586. if (plat_priv->pmu_vreg_map) {
  1587. ret = of_property_read_string_array(dev->of_node,
  1588. "qcom,pmu_vreg_map",
  1589. plat_priv->pmu_vreg_map,
  1590. plat_priv->pmu_vreg_map_len);
  1591. if (ret < 0)
  1592. cnss_pr_err("Fail to get PMU VReg Mapping\n");
  1593. } else {
  1594. cnss_pr_err("Failed to alloc PMU VReg mem\n");
  1595. }
  1596. } else {
  1597. cnss_pr_dbg("PMU VReg Mapping not configured\n");
  1598. }
  1599. /* Device DT Specific */
  1600. if (plat_priv->device_id == QCA6390_DEVICE_ID ||
  1601. plat_priv->device_id == QCA6490_DEVICE_ID) {
  1602. ret = of_property_read_string(dev->of_node,
  1603. "qcom,vreg_ol_cpr",
  1604. &plat_priv->vreg_ol_cpr);
  1605. if (ret)
  1606. cnss_pr_dbg("VReg for QCA6490 OL CPR not configured\n");
  1607. ret = of_property_read_string(dev->of_node,
  1608. "qcom,vreg_ipa",
  1609. &plat_priv->vreg_ipa);
  1610. if (ret)
  1611. cnss_pr_dbg("VReg for QCA6490 Int Power Amp not configured\n");
  1612. }
  1613. ret = of_property_count_u32_elems(plat_priv->plat_dev->dev.of_node,
  1614. "qcom,on-chip-pmic-support");
  1615. if (ret > 0) {
  1616. cfg_arr_size = ret;
  1617. cfg_arr = kcalloc(cfg_arr_size, sizeof(*cfg_arr), GFP_KERNEL);
  1618. if (cfg_arr) {
  1619. ret = of_property_read_u32_array(plat_priv->plat_dev->dev.of_node,
  1620. "qcom,on-chip-pmic-support",
  1621. cfg_arr, cfg_arr_size);
  1622. if (!ret) {
  1623. plat_priv->on_chip_pmic_devices_count = cfg_arr_size;
  1624. plat_priv->on_chip_pmic_board_ids = cfg_arr;
  1625. }
  1626. } else {
  1627. cnss_pr_err("Failed to alloc cfg table mem\n");
  1628. }
  1629. } else {
  1630. cnss_pr_dbg("On chip PMIC device ids not configured\n");
  1631. }
  1632. }
  1633. int cnss_update_cpr_info(struct cnss_plat_data *plat_priv)
  1634. {
  1635. struct cnss_cpr_info *cpr_info = &plat_priv->cpr_info;
  1636. u32 pmic_addr, voltage = 0, voltage_tmp, offset;
  1637. void __iomem *tcs_cmd_addr, *tcs_cmd_data_addr;
  1638. int i, j;
  1639. if (cpr_info->voltage == 0) {
  1640. cnss_pr_err("OL CPR Voltage %dm is not valid\n",
  1641. cpr_info->voltage);
  1642. return -EINVAL;
  1643. }
  1644. if (plat_priv->device_id != QCA6490_DEVICE_ID)
  1645. return -EINVAL;
  1646. if (!plat_priv->vreg_ol_cpr ||
  1647. !cnss_aop_interface_ready(plat_priv)) {
  1648. cnss_pr_dbg("AOP interface / OL CPR Vreg not configured\n");
  1649. } else {
  1650. return cnss_aop_set_vreg_param(plat_priv,
  1651. plat_priv->vreg_ol_cpr,
  1652. CNSS_VREG_VOLTAGE,
  1653. CNSS_TCS_DOWN_SEQ,
  1654. cpr_info->voltage);
  1655. }
  1656. if (plat_priv->tcs_info.cmd_base_addr == 0) {
  1657. cnss_pr_dbg("TCS CMD not configured for OL CPR update\n");
  1658. return 0;
  1659. }
  1660. if (cpr_info->cpr_pmic_addr == 0) {
  1661. cnss_pr_err("PMIC address 0x%x is not valid\n",
  1662. cpr_info->cpr_pmic_addr);
  1663. return -EINVAL;
  1664. }
  1665. if (cpr_info->tcs_cmd_data_addr_io)
  1666. goto update_cpr;
  1667. for (i = 0; i < MAX_TCS_NUM; i++) {
  1668. for (j = 0; j < MAX_TCS_CMD_NUM; j++) {
  1669. offset = i * TCS_OFFSET + j * TCS_CMD_OFFSET;
  1670. tcs_cmd_addr = plat_priv->tcs_info.cmd_base_addr_io +
  1671. offset;
  1672. pmic_addr = readl_relaxed(tcs_cmd_addr);
  1673. if (pmic_addr == cpr_info->cpr_pmic_addr) {
  1674. tcs_cmd_data_addr = tcs_cmd_addr +
  1675. TCS_CMD_DATA_ADDR_OFFSET;
  1676. voltage_tmp = readl_relaxed(tcs_cmd_data_addr);
  1677. cnss_pr_dbg("Got voltage %dmV from i: %d, j: %d\n",
  1678. voltage_tmp, i, j);
  1679. if (voltage_tmp > voltage) {
  1680. voltage = voltage_tmp;
  1681. cpr_info->tcs_cmd_data_addr =
  1682. plat_priv->tcs_info.cmd_base_addr +
  1683. offset + TCS_CMD_DATA_ADDR_OFFSET;
  1684. cpr_info->tcs_cmd_data_addr_io =
  1685. tcs_cmd_data_addr;
  1686. }
  1687. }
  1688. }
  1689. }
  1690. if (!cpr_info->tcs_cmd_data_addr_io) {
  1691. cnss_pr_err("Failed to find proper TCS CMD data address\n");
  1692. return -EINVAL;
  1693. }
  1694. update_cpr:
  1695. cpr_info->voltage = cpr_info->voltage > BT_CXMX_VOLTAGE_MV ?
  1696. cpr_info->voltage : BT_CXMX_VOLTAGE_MV;
  1697. cnss_pr_dbg("Update TCS CMD data address %pa with voltage %dmV\n",
  1698. &cpr_info->tcs_cmd_data_addr, cpr_info->voltage);
  1699. writel_relaxed(cpr_info->voltage, cpr_info->tcs_cmd_data_addr_io);
  1700. return 0;
  1701. }
  1702. int cnss_enable_int_pow_amp_vreg(struct cnss_plat_data *plat_priv)
  1703. {
  1704. struct platform_device *plat_dev = plat_priv->plat_dev;
  1705. u32 offset, addr_val, data_val;
  1706. void __iomem *tcs_cmd;
  1707. int ret;
  1708. static bool config_done;
  1709. if (plat_priv->device_id != QCA6490_DEVICE_ID)
  1710. return -EINVAL;
  1711. if (config_done) {
  1712. cnss_pr_dbg("IPA Vreg already configured\n");
  1713. return 0;
  1714. }
  1715. if (!plat_priv->vreg_ipa ||
  1716. !cnss_aop_interface_ready(plat_priv)) {
  1717. cnss_pr_dbg("AOP interface / IPA Vreg not configured\n");
  1718. } else {
  1719. ret = cnss_aop_set_vreg_param(plat_priv,
  1720. plat_priv->vreg_ipa,
  1721. CNSS_VREG_ENABLE,
  1722. CNSS_TCS_UP_SEQ, 1);
  1723. if (ret == 0)
  1724. config_done = true;
  1725. return ret;
  1726. }
  1727. if (!plat_priv->tcs_info.cmd_base_addr_io) {
  1728. cnss_pr_err("TCS CMD not configured for IPA Vreg enable\n");
  1729. return -EINVAL;
  1730. }
  1731. ret = of_property_read_u32(plat_dev->dev.of_node,
  1732. "qcom,tcs_offset_int_pow_amp_vreg",
  1733. &offset);
  1734. if (ret) {
  1735. cnss_pr_dbg("Internal Power Amp Vreg not configured\n");
  1736. return -EINVAL;
  1737. }
  1738. tcs_cmd = plat_priv->tcs_info.cmd_base_addr_io + offset;
  1739. addr_val = readl_relaxed(tcs_cmd);
  1740. tcs_cmd += TCS_CMD_DATA_ADDR_OFFSET;
  1741. /* 1 = enable Vreg */
  1742. writel_relaxed(1, tcs_cmd);
  1743. data_val = readl_relaxed(tcs_cmd);
  1744. cnss_pr_dbg("Setup S3E TCS Addr: %x Data: %d\n", addr_val, data_val);
  1745. config_done = true;
  1746. return 0;
  1747. }
  1748. int cnss_dev_specific_power_on(struct cnss_plat_data *plat_priv)
  1749. {
  1750. int ret;
  1751. if (plat_priv->dt_type != CNSS_DTT_MULTIEXCHG)
  1752. return 0;
  1753. ret = cnss_get_vreg_type(plat_priv, CNSS_VREG_PRIM);
  1754. if (ret)
  1755. return ret;
  1756. plat_priv->powered_on = false;
  1757. return cnss_power_on_device(plat_priv, false);
  1758. }