power.c 46 KB

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