power.c 48 KB

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