msm-cdc-supply.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2020, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/of_irq.h>
  9. #include <linux/of_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <asoc/msm-cdc-supply.h>
  13. #include <sound/soc.h>
  14. #define CODEC_DT_MAX_PROP_SIZE 40
  15. static int msm_cdc_dt_parse_vreg_info(struct device *dev,
  16. struct cdc_regulator *cdc_vreg,
  17. const char *name, bool is_ond)
  18. {
  19. char prop_name[CODEC_DT_MAX_PROP_SIZE];
  20. struct device_node *regulator_node = NULL;
  21. const __be32 *prop;
  22. int len, rc;
  23. u32 prop_val;
  24. /* Parse supply name */
  25. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "%s-supply", name);
  26. regulator_node = of_parse_phandle(dev->of_node, prop_name, 0);
  27. if (!regulator_node) {
  28. dev_err(dev, "%s: Looking up %s property in node %s failed",
  29. __func__, prop_name, dev->of_node->full_name);
  30. rc = -EINVAL;
  31. goto done;
  32. }
  33. cdc_vreg->name = name;
  34. cdc_vreg->ondemand = is_ond;
  35. /* Parse supply - voltage */
  36. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "qcom,%s-voltage", name);
  37. prop = of_get_property(dev->of_node, prop_name, &len);
  38. if (!prop || (len != (2 * sizeof(__be32)))) {
  39. dev_err(dev, "%s: %s %s property\n", __func__,
  40. prop ? "invalid format" : "no", prop_name);
  41. rc = -EINVAL;
  42. goto done;
  43. } else {
  44. cdc_vreg->min_uV = be32_to_cpup(&prop[0]);
  45. cdc_vreg->max_uV = be32_to_cpup(&prop[1]);
  46. }
  47. /* Parse supply - current */
  48. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "qcom,%s-current", name);
  49. rc = of_property_read_u32(dev->of_node, prop_name, &prop_val);
  50. if (rc) {
  51. dev_err(dev, "%s: Looking up %s property in node %s failed",
  52. __func__, prop_name, dev->of_node->full_name);
  53. goto done;
  54. }
  55. cdc_vreg->optimum_uA = prop_val;
  56. /* Parse supply - LPM or NOM mode(default NOM) */
  57. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "qcom,%s-lpm-supported", name);
  58. rc = of_property_read_u32(dev->of_node, prop_name, &prop_val);
  59. if (rc) {
  60. dev_dbg(dev, "%s: Looking up %s property in node %s failed",
  61. __func__, prop_name, dev->of_node->full_name);
  62. cdc_vreg->lpm_supported = 0;
  63. rc = 0;
  64. } else {
  65. cdc_vreg->lpm_supported = prop_val;
  66. }
  67. /* Parse supply - retention mode */
  68. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "qcom,%s-rem-supported", name);
  69. rc = of_property_read_u32(dev->of_node, prop_name, &prop_val);
  70. if (rc) {
  71. dev_dbg(dev, "%s: Looking up %s property in node %s failed",
  72. __func__, prop_name, dev->of_node->full_name);
  73. cdc_vreg->rem_supported = 0;
  74. rc = 0;
  75. } else {
  76. cdc_vreg->rem_supported = prop_val;
  77. }
  78. dev_info(dev, "%s: %s: vol=[%d %d]uV, curr=[%d]uA, ond %d lpm %d rem %d\n",
  79. __func__, cdc_vreg->name, cdc_vreg->min_uV, cdc_vreg->max_uV,
  80. cdc_vreg->optimum_uA, cdc_vreg->ondemand,
  81. cdc_vreg->lpm_supported, cdc_vreg->rem_supported);
  82. done:
  83. return rc;
  84. }
  85. static int msm_cdc_parse_supplies(struct device *dev,
  86. struct cdc_regulator *cdc_reg,
  87. const char *sup_list, int sup_cnt,
  88. bool is_ond)
  89. {
  90. int idx, rc = 0;
  91. const char *name = NULL;
  92. for (idx = 0; idx < sup_cnt; idx++) {
  93. rc = of_property_read_string_index(dev->of_node, sup_list, idx,
  94. &name);
  95. if (rc) {
  96. dev_err(dev, "%s: read string %s[%d] error (%d)\n",
  97. __func__, sup_list, idx, rc);
  98. goto done;
  99. }
  100. dev_dbg(dev, "%s: Found cdc supply %s as part of %s\n",
  101. __func__, name, sup_list);
  102. rc = msm_cdc_dt_parse_vreg_info(dev, &cdc_reg[idx], name,
  103. is_ond);
  104. if (rc) {
  105. dev_err(dev, "%s: parse %s vreg info failed (%d)\n",
  106. __func__, name, rc);
  107. goto done;
  108. }
  109. }
  110. done:
  111. return rc;
  112. }
  113. static int msm_cdc_check_supply_param(struct device *dev,
  114. struct cdc_regulator *cdc_vreg,
  115. int num_supplies)
  116. {
  117. if (!dev) {
  118. pr_err_ratelimited("%s: device is NULL\n", __func__);
  119. return -ENODEV;
  120. }
  121. if (!cdc_vreg || (num_supplies <= 0)) {
  122. dev_err_ratelimited(dev, "%s: supply check failed: vreg: %pK, num_supplies: %d\n",
  123. __func__, cdc_vreg, num_supplies);
  124. return -EINVAL;
  125. }
  126. return 0;
  127. }
  128. /*
  129. * msm_cdc_is_ondemand_supply:
  130. * return if ondemand supply true or not
  131. *
  132. * @dev: pointer to codec device
  133. * @supplies: pointer to regulator bulk data
  134. * @cdc_vreg: pointer to platform regulator data
  135. * @num_supplies: number of supplies
  136. * @supply_name: supply name to be checked
  137. *
  138. * Return true/false
  139. */
  140. bool msm_cdc_is_ondemand_supply(struct device *dev,
  141. struct regulator_bulk_data *supplies,
  142. struct cdc_regulator *cdc_vreg,
  143. int num_supplies,
  144. char *supply_name)
  145. {
  146. bool rc = false;
  147. int ret, i;
  148. if ((!supply_name) || (!supplies)) {
  149. pr_err_ratelimited("%s: either dev or supplies or cdc_vreg is NULL\n",
  150. __func__);
  151. return rc;
  152. }
  153. /* input parameter validation */
  154. ret = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  155. if (ret)
  156. return rc;
  157. for (i = 0; i < num_supplies; i++) {
  158. if (cdc_vreg[i].ondemand &&
  159. !strcmp(cdc_vreg[i].name, supply_name))
  160. return true;
  161. }
  162. return rc;
  163. }
  164. EXPORT_SYMBOL(msm_cdc_is_ondemand_supply);
  165. /*
  166. * msm_cdc_supply_supports_retention_mode:
  167. * On certain hardware configurations, This means that the
  168. * PM will disable the supply and remove its power vote
  169. * if the PM enters into a suspended state.
  170. *
  171. * return if supply supports retention mode or not
  172. *
  173. * @dev: pointer to codec device
  174. * @supplies: pointer to regulator bulk data
  175. * @cdc_vreg: pointer to platform regulator data
  176. * @num_supplies: number of supplies
  177. * @supply_name: supply name to be checked
  178. *
  179. * Return true/false
  180. */
  181. bool msm_cdc_supply_supports_retention_mode(struct device *dev,
  182. struct regulator_bulk_data *supplies,
  183. struct cdc_regulator *cdc_vreg,
  184. int num_supplies, char *supply_name)
  185. {
  186. bool rc = false;
  187. int ret, i;
  188. if ((!supply_name) || (!supplies)) {
  189. pr_err_ratelimited("%s: either dev or supplies or cdc_vreg is NULL\n",
  190. __func__);
  191. return rc;
  192. }
  193. /* input parameter validation */
  194. ret = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  195. if (ret)
  196. return rc;
  197. for (i = 0; i < num_supplies; i++) {
  198. if (cdc_vreg[i].rem_supported &&
  199. !strcmp(cdc_vreg[i].name, supply_name))
  200. return true;
  201. }
  202. return rc;
  203. }
  204. EXPORT_SYMBOL(msm_cdc_supply_supports_retention_mode);
  205. /*
  206. * msm_cdc_check_supply_vote:
  207. *
  208. * return true if supply has voted for regulator enable
  209. *
  210. * @dev: pointer to codec device
  211. * @supplies: pointer to regulator bulk data
  212. * @cdc_vreg: pointer to platform regulator data
  213. * @num_supplies: number of supplies
  214. * @supply_name: supply name to be checked
  215. *
  216. * Return true/false
  217. */
  218. bool msm_cdc_check_supply_vote(struct device *dev,
  219. struct regulator_bulk_data *supplies,
  220. struct cdc_regulator *cdc_vreg,
  221. int num_supplies,
  222. char *supply_name)
  223. {
  224. bool rc = false;
  225. int ret, i;
  226. if ((!supply_name) || (!supplies)) {
  227. pr_err_ratelimited("%s: either dev or supplies or cdc_vreg is NULL\n",
  228. __func__);
  229. return rc;
  230. }
  231. /* input parameter validation */
  232. ret = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  233. if (ret)
  234. return rc;
  235. for (i = 0; i < num_supplies; i++) {
  236. if (strcmp(cdc_vreg[i].name, supply_name) != 0)
  237. continue;
  238. return cdc_vreg[i].vote;
  239. }
  240. dev_err_ratelimited(dev,
  241. "%s: Unable to find vote for supply %s\n",
  242. __func__, supply_name);
  243. return -EINVAL;
  244. }
  245. EXPORT_SYMBOL(msm_cdc_check_supply_vote);
  246. /*
  247. * msm_cdc_set_supply_min_voltage:
  248. * Set min supply voltage for particular supply
  249. *
  250. * @dev: pointer to codec device
  251. * @supplies: pointer to regulator bulk data
  252. * @cdc_vreg: pointer to platform regulator data
  253. * @num_supplies: number of supplies
  254. * @supply_name: Supply name to change voltage for
  255. * @vval_min: Min voltage to be set in uV
  256. * @override_min_vol: True if override min voltage from default
  257. * Return error code if unable to set voltage
  258. */
  259. int msm_cdc_set_supply_min_voltage(struct device *dev,
  260. struct regulator_bulk_data *supplies,
  261. struct cdc_regulator *cdc_vreg,
  262. int num_supplies, char *supply_name,
  263. int vval_min, bool override_min_vol)
  264. {
  265. int rc = 0, i;
  266. if ((!supply_name) || (!supplies)) {
  267. pr_err_ratelimited("%s: either dev or supplies or cdc_vreg is NULL\n",
  268. __func__);
  269. return -EINVAL;
  270. }
  271. /* input parameter validation */
  272. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  273. if (rc)
  274. return rc;
  275. for (i = 0; i < num_supplies; i++) {
  276. if (!strcmp(cdc_vreg[i].name, supply_name)) {
  277. if (override_min_vol)
  278. regulator_set_voltage(supplies[i].consumer,
  279. vval_min, cdc_vreg[i].max_uV);
  280. else
  281. regulator_set_voltage(supplies[i].consumer,
  282. cdc_vreg[i].min_uV, cdc_vreg[i].max_uV);
  283. break;
  284. }
  285. }
  286. return rc;
  287. }
  288. EXPORT_SYMBOL(msm_cdc_set_supply_min_voltage);
  289. /*
  290. * msm_cdc_disable_ondemand_supply:
  291. * Disable codec ondemand supply
  292. *
  293. * @dev: pointer to codec device
  294. * @supplies: pointer to regulator bulk data
  295. * @cdc_vreg: pointer to platform regulator data
  296. * @num_supplies: number of supplies
  297. * @supply_name: Ondemand supply name to be enabled
  298. *
  299. * Return error code if supply disable is failed
  300. */
  301. int msm_cdc_disable_ondemand_supply(struct device *dev,
  302. struct regulator_bulk_data *supplies,
  303. struct cdc_regulator *cdc_vreg,
  304. int num_supplies,
  305. char *supply_name)
  306. {
  307. int rc, i;
  308. if ((!supply_name) || (!supplies)) {
  309. pr_err_ratelimited("%s: either dev or supplies or cdc_vreg is NULL\n",
  310. __func__);
  311. return -EINVAL;
  312. }
  313. /* input parameter validation */
  314. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  315. if (rc)
  316. return rc;
  317. dev_dbg(dev, "%s: Disabling on-demand supply %s\n",
  318. __func__, supply_name);
  319. for (i = 0; i < num_supplies; i++) {
  320. if (cdc_vreg[i].ondemand &&
  321. !strcmp(cdc_vreg[i].name, supply_name)) {
  322. if (!cdc_vreg[i].vote) {
  323. dev_err_ratelimited(dev,
  324. "%s: Attempted to disable already disabled supply %s\n",
  325. __func__, supplies[i].supply);
  326. break;
  327. }
  328. rc = regulator_disable(supplies[i].consumer);
  329. if (rc)
  330. dev_err_ratelimited(dev,
  331. "%s: failed to disable supply %s, err:%d\n",
  332. __func__, supplies[i].supply, rc);
  333. else
  334. cdc_vreg[i].vote = false;
  335. break;
  336. }
  337. }
  338. if (i == num_supplies) {
  339. dev_err_ratelimited(dev, "%s: not able to find supply %s\n",
  340. __func__, supply_name);
  341. rc = -EINVAL;
  342. }
  343. return rc;
  344. }
  345. EXPORT_SYMBOL(msm_cdc_disable_ondemand_supply);
  346. /*
  347. * msm_cdc_enable_ondemand_supply:
  348. * Enable codec ondemand supply
  349. *
  350. * @dev: pointer to codec device
  351. * @supplies: pointer to regulator bulk data
  352. * @cdc_vreg: pointer to platform regulator data
  353. * @num_supplies: number of supplies
  354. * @supply_name: Ondemand supply name to be enabled
  355. *
  356. * Return error code if supply enable is failed
  357. */
  358. int msm_cdc_enable_ondemand_supply(struct device *dev,
  359. struct regulator_bulk_data *supplies,
  360. struct cdc_regulator *cdc_vreg,
  361. int num_supplies,
  362. char *supply_name)
  363. {
  364. int rc, i;
  365. if ((!supply_name) || (!supplies)) {
  366. pr_err_ratelimited("%s: either dev or supplies or cdc_vreg is NULL\n",
  367. __func__);
  368. return -EINVAL;
  369. }
  370. /* input parameter validation */
  371. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  372. if (rc)
  373. return rc;
  374. dev_dbg(dev, "%s: Enabling on-demand supply %s\n",
  375. __func__, supply_name);
  376. for (i = 0; i < num_supplies; i++) {
  377. if (cdc_vreg[i].ondemand &&
  378. !strcmp(cdc_vreg[i].name, supply_name)) {
  379. if (cdc_vreg[i].vote) {
  380. dev_err_ratelimited(dev,
  381. "%s: Attempted to enable already enabled supply %s\n",
  382. __func__, supplies[i].supply);
  383. break;
  384. }
  385. rc = regulator_enable(supplies[i].consumer);
  386. if (rc)
  387. dev_err_ratelimited(dev, "%s: failed to enable supply %s, rc: %d\n",
  388. __func__, supplies[i].supply, rc);
  389. else
  390. cdc_vreg[i].vote = true;
  391. break;
  392. }
  393. }
  394. if (i == num_supplies) {
  395. dev_err_ratelimited(dev, "%s: not able to find supply %s\n",
  396. __func__, supply_name);
  397. rc = -EINVAL;
  398. }
  399. return rc;
  400. }
  401. EXPORT_SYMBOL(msm_cdc_enable_ondemand_supply);
  402. /*
  403. * msm_cdc_set_supplies_lpm_mode:
  404. * Update load for given supply string
  405. *
  406. * @dev: pointer to codec device
  407. * @supplies: pointer to regulator bulk data
  408. * @cdc_vreg: pointer to platform regulator data
  409. * @num_supplies: number of supplies
  410. * @supply_name: supply name to be checked
  411. * @min_max: Apply optimum or 0 current
  412. *
  413. * Return error code if set current fail
  414. */
  415. int msm_cdc_set_supplies_lpm_mode(struct device *dev,
  416. struct regulator_bulk_data *supplies,
  417. struct cdc_regulator *cdc_vreg,
  418. int num_supplies,
  419. bool flag)
  420. {
  421. int rc = 0, i;
  422. if (!supplies) {
  423. pr_err_ratelimited("%s: supplies is NULL\n",
  424. __func__);
  425. return -EINVAL;
  426. }
  427. /* input parameter validation */
  428. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  429. if (rc)
  430. return rc;
  431. for (i = 0; i < num_supplies; i++) {
  432. if (cdc_vreg[i].lpm_supported) {
  433. rc = regulator_set_load(
  434. supplies[i].consumer,
  435. flag ? 0 : cdc_vreg[i].optimum_uA);
  436. if (rc)
  437. dev_err_ratelimited(dev,
  438. "%s: failed to set supply %s to %s, err:%d\n",
  439. __func__, supplies[i].supply,
  440. flag ? "LPM" : "NOM",
  441. rc);
  442. else
  443. dev_dbg(dev, "%s: regulator %s load set to %s\n",
  444. __func__, supplies[i].supply,
  445. flag ? "LPM" : "NOM");
  446. }
  447. }
  448. return rc;
  449. }
  450. EXPORT_SYMBOL(msm_cdc_set_supplies_lpm_mode);
  451. /*
  452. * msm_cdc_disable_static_supplies:
  453. * Disable codec static supplies
  454. *
  455. * @dev: pointer to codec device
  456. * @supplies: pointer to regulator bulk data
  457. * @cdc_vreg: pointer to platform regulator data
  458. * @num_supplies: number of supplies
  459. *
  460. * Return error code if supply disable is failed
  461. */
  462. int msm_cdc_disable_static_supplies(struct device *dev,
  463. struct regulator_bulk_data *supplies,
  464. struct cdc_regulator *cdc_vreg,
  465. int num_supplies)
  466. {
  467. int rc, i;
  468. if ((!dev) || (!supplies) || (!cdc_vreg)) {
  469. pr_err("%s: either dev or supplies or cdc_vreg is NULL\n",
  470. __func__);
  471. return -EINVAL;
  472. }
  473. /* input parameter validation */
  474. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  475. if (rc)
  476. return rc;
  477. for (i = 0; i < num_supplies; i++) {
  478. if (cdc_vreg[i].ondemand)
  479. continue;
  480. rc = regulator_disable(supplies[i].consumer);
  481. if (rc)
  482. dev_err(dev, "%s: failed to disable supply %s, err:%d\n",
  483. __func__, supplies[i].supply, rc);
  484. else {
  485. cdc_vreg[i].vote = false;
  486. dev_dbg(dev, "%s: disabled regulator %s\n",
  487. __func__, supplies[i].supply);
  488. }
  489. }
  490. return rc;
  491. }
  492. EXPORT_SYMBOL(msm_cdc_disable_static_supplies);
  493. /*
  494. * msm_cdc_release_supplies:
  495. * Release codec power supplies
  496. *
  497. * @dev: pointer to codec device
  498. * @supplies: pointer to regulator bulk data
  499. * @cdc_vreg: pointer to platform regulator data
  500. * @num_supplies: number of supplies
  501. *
  502. * Return error code if supply disable is failed
  503. */
  504. int msm_cdc_release_supplies(struct device *dev,
  505. struct regulator_bulk_data *supplies,
  506. struct cdc_regulator *cdc_vreg,
  507. int num_supplies)
  508. {
  509. int rc = 0;
  510. int i;
  511. if ((!dev) || (!supplies) || (!cdc_vreg)) {
  512. pr_err("%s: either dev or supplies or cdc_vreg is NULL\n",
  513. __func__);
  514. return -EINVAL;
  515. }
  516. /* input parameter validation */
  517. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  518. if (rc)
  519. return rc;
  520. msm_cdc_disable_static_supplies(dev, supplies, cdc_vreg,
  521. num_supplies);
  522. for (i = 0; i < num_supplies; i++) {
  523. if (regulator_count_voltages(supplies[i].consumer) < 0)
  524. continue;
  525. regulator_set_voltage(supplies[i].consumer, 0,
  526. cdc_vreg[i].max_uV);
  527. regulator_set_load(supplies[i].consumer, 0);
  528. }
  529. return rc;
  530. }
  531. EXPORT_SYMBOL(msm_cdc_release_supplies);
  532. /*
  533. * msm_cdc_enable_static_supplies:
  534. * Enable codec static supplies
  535. *
  536. * @dev: pointer to codec device
  537. * @supplies: pointer to regulator bulk data
  538. * @cdc_vreg: pointer to platform regulator data
  539. * @num_supplies: number of supplies
  540. *
  541. * Return error code if supply enable is failed
  542. */
  543. int msm_cdc_enable_static_supplies(struct device *dev,
  544. struct regulator_bulk_data *supplies,
  545. struct cdc_regulator *cdc_vreg,
  546. int num_supplies)
  547. {
  548. int rc, i;
  549. if ((!dev) || (!supplies) || (!cdc_vreg)) {
  550. pr_err("%s: either dev or supplies or cdc_vreg is NULL\n",
  551. __func__);
  552. return -EINVAL;
  553. }
  554. /* input parameter validation */
  555. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  556. if (rc)
  557. return rc;
  558. for (i = 0; i < num_supplies; i++) {
  559. if (cdc_vreg[i].ondemand)
  560. continue;
  561. rc = regulator_enable(supplies[i].consumer);
  562. if (rc) {
  563. dev_err(dev, "%s: failed to enable supply %s, rc: %d\n",
  564. __func__, supplies[i].supply, rc);
  565. break;
  566. } else
  567. cdc_vreg[i].vote = true;
  568. }
  569. if (rc) {
  570. while (i--) {
  571. if (cdc_vreg[i].ondemand)
  572. continue;
  573. if (regulator_disable(supplies[i].consumer) == 0)
  574. cdc_vreg[i].vote = false;
  575. else
  576. dev_err(dev, "%s: failed to disable supply %s during unwind\n",
  577. __func__, supplies[i].supply);
  578. }
  579. }
  580. return rc;
  581. }
  582. EXPORT_SYMBOL(msm_cdc_enable_static_supplies);
  583. /*
  584. * msm_cdc_init_supplies:
  585. * Initialize codec static supplies
  586. *
  587. * @dev: pointer to codec device
  588. * @supplies: pointer to regulator bulk data
  589. * @cdc_vreg: pointer to platform regulator data
  590. * @num_supplies: number of supplies
  591. *
  592. * Return error code if supply init is failed
  593. */
  594. int msm_cdc_init_supplies(struct device *dev,
  595. struct regulator_bulk_data **supplies,
  596. struct cdc_regulator *cdc_vreg,
  597. int num_supplies)
  598. {
  599. return msm_cdc_init_supplies_v2(dev, supplies, cdc_vreg,
  600. num_supplies, false);
  601. }
  602. EXPORT_SYMBOL(msm_cdc_init_supplies);
  603. /*
  604. * msm_cdc_init_supplies_v2:
  605. * Initialize codec static supplies.
  606. * Initialize codec dynamic supplies based on vote_regulator_on_demand
  607. *
  608. * @dev: pointer to codec device
  609. * @supplies: pointer to regulator bulk data
  610. * @cdc_vreg: pointer to platform regulator data
  611. * @num_supplies: number of supplies
  612. * @vote_regulator_on_demand: initialize codec dynamic supplies at runtime
  613. *
  614. * Return error code if supply init is failed
  615. */
  616. int msm_cdc_init_supplies_v2(struct device *dev,
  617. struct regulator_bulk_data **supplies,
  618. struct cdc_regulator *cdc_vreg,
  619. int num_supplies, u32 vote_regulator_on_demand)
  620. {
  621. struct regulator_bulk_data *vsup;
  622. int rc;
  623. int i;
  624. if (!dev || !cdc_vreg) {
  625. pr_err("%s: device pointer or dce_vreg is NULL\n",
  626. __func__);
  627. return -EINVAL;
  628. }
  629. /* input parameter validation */
  630. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  631. if (rc)
  632. return rc;
  633. vsup = devm_kcalloc(dev, num_supplies,
  634. sizeof(struct regulator_bulk_data),
  635. GFP_KERNEL);
  636. if (!vsup)
  637. return -ENOMEM;
  638. for (i = 0; i < num_supplies; i++) {
  639. if (!cdc_vreg[i].name) {
  640. dev_err(dev, "%s: supply name not defined\n",
  641. __func__);
  642. rc = -EINVAL;
  643. goto err_supply;
  644. }
  645. vsup[i].supply = cdc_vreg[i].name;
  646. }
  647. rc = devm_regulator_bulk_get(dev, num_supplies, vsup);
  648. if (rc) {
  649. dev_err(dev, "%s: failed to get supplies (%d)\n",
  650. __func__, rc);
  651. goto err_supply;
  652. }
  653. /* Set voltage and current on regulators */
  654. for (i = 0; i < num_supplies; i++) {
  655. if (regulator_count_voltages(vsup[i].consumer) < 0)
  656. continue;
  657. if (cdc_vreg[i].ondemand && vote_regulator_on_demand)
  658. continue;
  659. cdc_vreg[i].regulator = vsup[i].consumer;
  660. rc = regulator_set_voltage(vsup[i].consumer,
  661. cdc_vreg[i].min_uV,
  662. cdc_vreg[i].max_uV);
  663. if (rc) {
  664. dev_err(dev, "%s: set regulator voltage failed for %s, err:%d\n",
  665. __func__, vsup[i].supply, rc);
  666. goto err_supply;
  667. }
  668. rc = regulator_set_load(vsup[i].consumer,
  669. cdc_vreg[i].optimum_uA);
  670. if (rc < 0) {
  671. dev_err(dev, "%s: set regulator optimum mode failed for %s, err:%d\n",
  672. __func__, vsup[i].supply, rc);
  673. goto err_supply;
  674. }
  675. }
  676. *supplies = vsup;
  677. return 0;
  678. err_supply:
  679. return rc;
  680. }
  681. EXPORT_SYMBOL(msm_cdc_init_supplies_v2);
  682. /*
  683. * msm_cdc_get_power_supplies:
  684. * Get codec power supplies from device tree.
  685. * Allocate memory to hold regulator data for
  686. * all power supplies.
  687. *
  688. * @dev: pointer to codec device
  689. * @cdc_vreg: pointer to codec regulator
  690. * @total_num_supplies: total number of supplies read from DT
  691. *
  692. * Return error code if supply disable is failed
  693. */
  694. int msm_cdc_get_power_supplies(struct device *dev,
  695. struct cdc_regulator **cdc_vreg,
  696. int *total_num_supplies)
  697. {
  698. const char *static_prop_name = "qcom,cdc-static-supplies";
  699. const char *ond_prop_name = "qcom,cdc-on-demand-supplies";
  700. const char *cp_prop_name = "qcom,cdc-cp-supplies";
  701. int static_sup_cnt = 0;
  702. int ond_sup_cnt = 0;
  703. int cp_sup_cnt = 0;
  704. int num_supplies = 0;
  705. struct cdc_regulator *cdc_reg;
  706. int rc;
  707. if (!dev) {
  708. pr_err_ratelimited("%s: device pointer is NULL\n", __func__);
  709. return -EINVAL;
  710. }
  711. static_sup_cnt = of_property_count_strings(dev->of_node,
  712. static_prop_name);
  713. if (static_sup_cnt < 0) {
  714. dev_err_ratelimited(dev, "%s: Failed to get static supplies(%d)\n",
  715. __func__, static_sup_cnt);
  716. rc = static_sup_cnt;
  717. goto err_supply_cnt;
  718. }
  719. ond_sup_cnt = of_property_count_strings(dev->of_node, ond_prop_name);
  720. if (ond_sup_cnt < 0)
  721. ond_sup_cnt = 0;
  722. cp_sup_cnt = of_property_count_strings(dev->of_node,
  723. cp_prop_name);
  724. if (cp_sup_cnt < 0)
  725. cp_sup_cnt = 0;
  726. num_supplies = static_sup_cnt + ond_sup_cnt + cp_sup_cnt;
  727. if (num_supplies <= 0) {
  728. dev_err_ratelimited(dev, "%s: supply count is 0 or negative\n", __func__);
  729. rc = -EINVAL;
  730. goto err_supply_cnt;
  731. }
  732. cdc_reg = devm_kcalloc(dev, num_supplies,
  733. sizeof(struct cdc_regulator),
  734. GFP_KERNEL);
  735. if (!cdc_reg) {
  736. rc = -ENOMEM;
  737. goto err_mem_alloc;
  738. }
  739. rc = msm_cdc_parse_supplies(dev, cdc_reg, static_prop_name,
  740. static_sup_cnt, false);
  741. if (rc) {
  742. dev_err_ratelimited(dev, "%s: failed to parse static supplies(%d)\n",
  743. __func__, rc);
  744. goto err_sup;
  745. }
  746. rc = msm_cdc_parse_supplies(dev, &cdc_reg[static_sup_cnt],
  747. ond_prop_name, ond_sup_cnt,
  748. true);
  749. if (rc) {
  750. dev_err_ratelimited(dev, "%s: failed to parse demand supplies(%d)\n",
  751. __func__, rc);
  752. goto err_sup;
  753. }
  754. rc = msm_cdc_parse_supplies(dev,
  755. &cdc_reg[static_sup_cnt + ond_sup_cnt],
  756. cp_prop_name, cp_sup_cnt, true);
  757. if (rc) {
  758. dev_err_ratelimited(dev, "%s: failed to parse cp supplies(%d)\n",
  759. __func__, rc);
  760. goto err_sup;
  761. }
  762. *cdc_vreg = cdc_reg;
  763. *total_num_supplies = num_supplies;
  764. return 0;
  765. err_sup:
  766. err_supply_cnt:
  767. err_mem_alloc:
  768. return rc;
  769. }
  770. EXPORT_SYMBOL(msm_cdc_get_power_supplies);
  771. /*
  772. * msm_cdc_init_wcd_supply:
  773. * Initialize wcd supply parameters.
  774. *
  775. * @np: device node pointer to codec device
  776. * @name: power supply name
  777. * @cdc_supply: codec supply struct to hold wcd params
  778. *
  779. * Return error code if init failed
  780. */
  781. int msm_cdc_init_wcd_supply(struct device_node *np, const char *name,
  782. struct cdc_wcd_supply *cdc_supply)
  783. {
  784. struct platform_device *pdev = NULL;
  785. if (!np || !cdc_supply)
  786. return -EINVAL;
  787. pdev = of_find_device_by_node(np);
  788. if (!pdev)
  789. return -EINVAL;
  790. cdc_supply->dev = &pdev->dev;
  791. cdc_supply->name = name;
  792. cdc_supply->component = snd_soc_lookup_component(&pdev->dev, NULL);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL(msm_cdc_init_wcd_supply);
  796. /*
  797. * msm_cdc_enable_wcd_supply:
  798. * Enable/Disable wcd supply.
  799. *
  800. * @cdc_supply: codec supply struct to hold wcd params
  801. * @enable: bool to inform whether to enable or disable
  802. *
  803. * Return error code if enable/disable failed
  804. */
  805. int msm_cdc_enable_wcd_supply(struct cdc_wcd_supply *cdc_supply, bool enable)
  806. {
  807. struct snd_soc_component *component = cdc_supply->component;
  808. int rc;
  809. if (!component) {
  810. pr_err_ratelimited("%s: Component memory is NULL\n", __func__);
  811. return -EINVAL;
  812. }
  813. if (enable)
  814. rc = snd_soc_dapm_force_enable_pin(
  815. snd_soc_component_get_dapm(component),
  816. cdc_supply->name);
  817. else
  818. rc = snd_soc_dapm_disable_pin(
  819. snd_soc_component_get_dapm(component),
  820. cdc_supply->name);
  821. if (!rc)
  822. snd_soc_dapm_sync(snd_soc_component_get_dapm(component));
  823. else
  824. dev_err_ratelimited(component->dev, "%s: micbias %s force %s pin failed\n",
  825. __func__, cdc_supply->name, (enable ? "enable" : "disable"));
  826. return rc;
  827. }
  828. EXPORT_SYMBOL(msm_cdc_enable_wcd_supply);