msm-cdc-supply.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/of_irq.h>
  8. #include <linux/of_device.h>
  9. #include <linux/slab.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <asoc/msm-cdc-supply.h>
  12. #define CODEC_DT_MAX_PROP_SIZE 40
  13. static int msm_cdc_dt_parse_vreg_info(struct device *dev,
  14. struct cdc_regulator *cdc_vreg,
  15. const char *name, bool is_ond)
  16. {
  17. char prop_name[CODEC_DT_MAX_PROP_SIZE];
  18. struct device_node *regulator_node = NULL;
  19. const __be32 *prop;
  20. int len, rc;
  21. u32 prop_val;
  22. /* Parse supply name */
  23. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "%s-supply", name);
  24. regulator_node = of_parse_phandle(dev->of_node, prop_name, 0);
  25. if (!regulator_node) {
  26. dev_err(dev, "%s: Looking up %s property in node %s failed",
  27. __func__, prop_name, dev->of_node->full_name);
  28. rc = -EINVAL;
  29. goto done;
  30. }
  31. cdc_vreg->name = name;
  32. cdc_vreg->ondemand = is_ond;
  33. /* Parse supply - voltage */
  34. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "qcom,%s-voltage", name);
  35. prop = of_get_property(dev->of_node, prop_name, &len);
  36. if (!prop || (len != (2 * sizeof(__be32)))) {
  37. dev_err(dev, "%s: %s %s property\n", __func__,
  38. prop ? "invalid format" : "no", prop_name);
  39. rc = -EINVAL;
  40. goto done;
  41. } else {
  42. cdc_vreg->min_uV = be32_to_cpup(&prop[0]);
  43. cdc_vreg->max_uV = be32_to_cpup(&prop[1]);
  44. }
  45. /* Parse supply - current */
  46. snprintf(prop_name, CODEC_DT_MAX_PROP_SIZE, "qcom,%s-current", name);
  47. rc = of_property_read_u32(dev->of_node, prop_name, &prop_val);
  48. if (rc) {
  49. dev_err(dev, "%s: Looking up %s property in node %s failed",
  50. __func__, prop_name, dev->of_node->full_name);
  51. goto done;
  52. }
  53. cdc_vreg->optimum_uA = prop_val;
  54. dev_info(dev, "%s: %s: vol=[%d %d]uV, curr=[%d]uA, ond %d\n",
  55. __func__, cdc_vreg->name, cdc_vreg->min_uV, cdc_vreg->max_uV,
  56. cdc_vreg->optimum_uA, cdc_vreg->ondemand);
  57. done:
  58. return rc;
  59. }
  60. static int msm_cdc_parse_supplies(struct device *dev,
  61. struct cdc_regulator *cdc_reg,
  62. const char *sup_list, int sup_cnt,
  63. bool is_ond)
  64. {
  65. int idx, rc = 0;
  66. const char *name = NULL;
  67. for (idx = 0; idx < sup_cnt; idx++) {
  68. rc = of_property_read_string_index(dev->of_node, sup_list, idx,
  69. &name);
  70. if (rc) {
  71. dev_err(dev, "%s: read string %s[%d] error (%d)\n",
  72. __func__, sup_list, idx, rc);
  73. goto done;
  74. }
  75. dev_dbg(dev, "%s: Found cdc supply %s as part of %s\n",
  76. __func__, name, sup_list);
  77. rc = msm_cdc_dt_parse_vreg_info(dev, &cdc_reg[idx], name,
  78. is_ond);
  79. if (rc) {
  80. dev_err(dev, "%s: parse %s vreg info failed (%d)\n",
  81. __func__, name, rc);
  82. goto done;
  83. }
  84. }
  85. done:
  86. return rc;
  87. }
  88. static int msm_cdc_check_supply_param(struct device *dev,
  89. struct cdc_regulator *cdc_vreg,
  90. int num_supplies)
  91. {
  92. if (!dev) {
  93. pr_err("%s: device is NULL\n", __func__);
  94. return -ENODEV;
  95. }
  96. if (!cdc_vreg || (num_supplies <= 0)) {
  97. dev_err(dev, "%s: supply check failed: vreg: %pK, num_supplies: %d\n",
  98. __func__, cdc_vreg, num_supplies);
  99. return -EINVAL;
  100. }
  101. return 0;
  102. }
  103. /*
  104. * msm_cdc_disable_ondemand_supply:
  105. * Disable codec ondemand supply
  106. *
  107. * @dev: pointer to codec device
  108. * @supplies: pointer to regulator bulk data
  109. * @cdc_vreg: pointer to platform regulator data
  110. * @num_supplies: number of supplies
  111. * @supply_name: Ondemand supply name to be enabled
  112. *
  113. * Return error code if supply disable is failed
  114. */
  115. int msm_cdc_disable_ondemand_supply(struct device *dev,
  116. struct regulator_bulk_data *supplies,
  117. struct cdc_regulator *cdc_vreg,
  118. int num_supplies,
  119. char *supply_name)
  120. {
  121. int rc, i;
  122. if ((!supply_name) || (!supplies)) {
  123. pr_err("%s: either dev or supplies or cdc_vreg is NULL\n",
  124. __func__);
  125. return -EINVAL;
  126. }
  127. /* input parameter validation */
  128. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  129. if (rc)
  130. return rc;
  131. for (i = 0; i < num_supplies; i++) {
  132. if (cdc_vreg[i].ondemand &&
  133. !strcmp(cdc_vreg[i].name, supply_name)) {
  134. rc = regulator_disable(supplies[i].consumer);
  135. if (rc)
  136. dev_err(dev, "%s: failed to disable supply %s, err:%d\n",
  137. __func__, supplies[i].supply, rc);
  138. break;
  139. }
  140. }
  141. if (i == num_supplies) {
  142. dev_err(dev, "%s: not able to find supply %s\n",
  143. __func__, supply_name);
  144. rc = -EINVAL;
  145. }
  146. return rc;
  147. }
  148. EXPORT_SYMBOL(msm_cdc_disable_ondemand_supply);
  149. /*
  150. * msm_cdc_enable_ondemand_supply:
  151. * Enable codec ondemand supply
  152. *
  153. * @dev: pointer to codec device
  154. * @supplies: pointer to regulator bulk data
  155. * @cdc_vreg: pointer to platform regulator data
  156. * @num_supplies: number of supplies
  157. * @supply_name: Ondemand supply name to be enabled
  158. *
  159. * Return error code if supply enable is failed
  160. */
  161. int msm_cdc_enable_ondemand_supply(struct device *dev,
  162. struct regulator_bulk_data *supplies,
  163. struct cdc_regulator *cdc_vreg,
  164. int num_supplies,
  165. char *supply_name)
  166. {
  167. int rc, i;
  168. if ((!supply_name) || (!supplies)) {
  169. pr_err("%s: either dev or supplies or cdc_vreg is NULL\n",
  170. __func__);
  171. return -EINVAL;
  172. }
  173. /* input parameter validation */
  174. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  175. if (rc)
  176. return rc;
  177. for (i = 0; i < num_supplies; i++) {
  178. if (cdc_vreg[i].ondemand &&
  179. !strcmp(cdc_vreg[i].name, supply_name)) {
  180. rc = regulator_enable(supplies[i].consumer);
  181. if (rc)
  182. dev_err(dev, "%s: failed to enable supply %s, rc: %d\n",
  183. __func__, supplies[i].supply, rc);
  184. break;
  185. }
  186. }
  187. if (i == num_supplies) {
  188. dev_err(dev, "%s: not able to find supply %s\n",
  189. __func__, supply_name);
  190. rc = -EINVAL;
  191. }
  192. return rc;
  193. }
  194. EXPORT_SYMBOL(msm_cdc_enable_ondemand_supply);
  195. /*
  196. * msm_cdc_disable_static_supplies:
  197. * Disable codec static supplies
  198. *
  199. * @dev: pointer to codec device
  200. * @supplies: pointer to regulator bulk data
  201. * @cdc_vreg: pointer to platform regulator data
  202. * @num_supplies: number of supplies
  203. *
  204. * Return error code if supply disable is failed
  205. */
  206. int msm_cdc_disable_static_supplies(struct device *dev,
  207. struct regulator_bulk_data *supplies,
  208. struct cdc_regulator *cdc_vreg,
  209. int num_supplies)
  210. {
  211. int rc, i;
  212. if ((!dev) || (!supplies) || (!cdc_vreg)) {
  213. pr_err("%s: either dev or supplies or cdc_vreg is NULL\n",
  214. __func__);
  215. return -EINVAL;
  216. }
  217. /* input parameter validation */
  218. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  219. if (rc)
  220. return rc;
  221. for (i = 0; i < num_supplies; i++) {
  222. if (cdc_vreg[i].ondemand)
  223. continue;
  224. rc = regulator_disable(supplies[i].consumer);
  225. if (rc)
  226. dev_err(dev, "%s: failed to disable supply %s, err:%d\n",
  227. __func__, supplies[i].supply, rc);
  228. else
  229. dev_dbg(dev, "%s: disabled regulator %s\n",
  230. __func__, supplies[i].supply);
  231. }
  232. return rc;
  233. }
  234. EXPORT_SYMBOL(msm_cdc_disable_static_supplies);
  235. /*
  236. * msm_cdc_release_supplies:
  237. * Release codec power supplies
  238. *
  239. * @dev: pointer to codec device
  240. * @supplies: pointer to regulator bulk data
  241. * @cdc_vreg: pointer to platform regulator data
  242. * @num_supplies: number of supplies
  243. *
  244. * Return error code if supply disable is failed
  245. */
  246. int msm_cdc_release_supplies(struct device *dev,
  247. struct regulator_bulk_data *supplies,
  248. struct cdc_regulator *cdc_vreg,
  249. int num_supplies)
  250. {
  251. int rc = 0;
  252. int i;
  253. if ((!dev) || (!supplies) || (!cdc_vreg)) {
  254. pr_err("%s: either dev or supplies or cdc_vreg is NULL\n",
  255. __func__);
  256. return -EINVAL;
  257. }
  258. /* input parameter validation */
  259. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  260. if (rc)
  261. return rc;
  262. msm_cdc_disable_static_supplies(dev, supplies, cdc_vreg,
  263. num_supplies);
  264. for (i = 0; i < num_supplies; i++) {
  265. if (regulator_count_voltages(supplies[i].consumer) < 0)
  266. continue;
  267. regulator_set_voltage(supplies[i].consumer, 0,
  268. cdc_vreg[i].max_uV);
  269. regulator_set_load(supplies[i].consumer, 0);
  270. }
  271. return rc;
  272. }
  273. EXPORT_SYMBOL(msm_cdc_release_supplies);
  274. /*
  275. * msm_cdc_enable_static_supplies:
  276. * Enable codec static supplies
  277. *
  278. * @dev: pointer to codec device
  279. * @supplies: pointer to regulator bulk data
  280. * @cdc_vreg: pointer to platform regulator data
  281. * @num_supplies: number of supplies
  282. *
  283. * Return error code if supply enable is failed
  284. */
  285. int msm_cdc_enable_static_supplies(struct device *dev,
  286. struct regulator_bulk_data *supplies,
  287. struct cdc_regulator *cdc_vreg,
  288. int num_supplies)
  289. {
  290. int rc, i;
  291. if ((!dev) || (!supplies) || (!cdc_vreg)) {
  292. pr_err("%s: either dev or supplies or cdc_vreg is NULL\n",
  293. __func__);
  294. return -EINVAL;
  295. }
  296. /* input parameter validation */
  297. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  298. if (rc)
  299. return rc;
  300. for (i = 0; i < num_supplies; i++) {
  301. if (cdc_vreg[i].ondemand)
  302. continue;
  303. rc = regulator_enable(supplies[i].consumer);
  304. if (rc) {
  305. dev_err(dev, "%s: failed to enable supply %s, rc: %d\n",
  306. __func__, supplies[i].supply, rc);
  307. break;
  308. }
  309. }
  310. while (rc && i--)
  311. if (!cdc_vreg[i].ondemand)
  312. regulator_disable(supplies[i].consumer);
  313. return rc;
  314. }
  315. EXPORT_SYMBOL(msm_cdc_enable_static_supplies);
  316. /*
  317. * msm_cdc_init_supplies:
  318. * Initialize codec static supplies with regulator get
  319. *
  320. * @dev: pointer to codec device
  321. * @supplies: pointer to regulator bulk data
  322. * @cdc_vreg: pointer to platform regulator data
  323. * @num_supplies: number of supplies
  324. *
  325. * Return error code if supply init is failed
  326. */
  327. int msm_cdc_init_supplies(struct device *dev,
  328. struct regulator_bulk_data **supplies,
  329. struct cdc_regulator *cdc_vreg,
  330. int num_supplies)
  331. {
  332. struct regulator_bulk_data *vsup;
  333. int rc;
  334. int i;
  335. if (!dev || !cdc_vreg) {
  336. pr_err("%s: device pointer or dce_vreg is NULL\n",
  337. __func__);
  338. return -EINVAL;
  339. }
  340. /* input parameter validation */
  341. rc = msm_cdc_check_supply_param(dev, cdc_vreg, num_supplies);
  342. if (rc)
  343. return rc;
  344. vsup = devm_kcalloc(dev, num_supplies,
  345. sizeof(struct regulator_bulk_data),
  346. GFP_KERNEL);
  347. if (!vsup)
  348. return -ENOMEM;
  349. for (i = 0; i < num_supplies; i++) {
  350. if (!cdc_vreg[i].name) {
  351. dev_err(dev, "%s: supply name not defined\n",
  352. __func__);
  353. rc = -EINVAL;
  354. goto err_supply;
  355. }
  356. vsup[i].supply = cdc_vreg[i].name;
  357. }
  358. rc = devm_regulator_bulk_get(dev, num_supplies, vsup);
  359. if (rc) {
  360. dev_err(dev, "%s: failed to get supplies (%d)\n",
  361. __func__, rc);
  362. goto err_supply;
  363. }
  364. /* Set voltage and current on regulators */
  365. for (i = 0; i < num_supplies; i++) {
  366. if (regulator_count_voltages(vsup[i].consumer) < 0)
  367. continue;
  368. rc = regulator_set_voltage(vsup[i].consumer,
  369. cdc_vreg[i].min_uV,
  370. cdc_vreg[i].max_uV);
  371. if (rc) {
  372. dev_err(dev, "%s: set regulator voltage failed for %s, err:%d\n",
  373. __func__, vsup[i].supply, rc);
  374. goto err_supply;
  375. }
  376. rc = regulator_set_load(vsup[i].consumer,
  377. cdc_vreg[i].optimum_uA);
  378. if (rc < 0) {
  379. dev_err(dev, "%s: set regulator optimum mode failed for %s, err:%d\n",
  380. __func__, vsup[i].supply, rc);
  381. goto err_supply;
  382. }
  383. }
  384. *supplies = vsup;
  385. return 0;
  386. err_supply:
  387. return rc;
  388. }
  389. EXPORT_SYMBOL(msm_cdc_init_supplies);
  390. /*
  391. * msm_cdc_get_power_supplies:
  392. * Get codec power supplies from device tree.
  393. * Allocate memory to hold regulator data for
  394. * all power supplies.
  395. *
  396. * @dev: pointer to codec device
  397. * @cdc_vreg: pointer to codec regulator
  398. * @total_num_supplies: total number of supplies read from DT
  399. *
  400. * Return error code if supply disable is failed
  401. */
  402. int msm_cdc_get_power_supplies(struct device *dev,
  403. struct cdc_regulator **cdc_vreg,
  404. int *total_num_supplies)
  405. {
  406. const char *static_prop_name = "qcom,cdc-static-supplies";
  407. const char *ond_prop_name = "qcom,cdc-on-demand-supplies";
  408. const char *cp_prop_name = "qcom,cdc-cp-supplies";
  409. int static_sup_cnt = 0;
  410. int ond_sup_cnt = 0;
  411. int cp_sup_cnt = 0;
  412. int num_supplies = 0;
  413. struct cdc_regulator *cdc_reg;
  414. int rc;
  415. if (!dev) {
  416. pr_err("%s: device pointer is NULL\n", __func__);
  417. return -EINVAL;
  418. }
  419. static_sup_cnt = of_property_count_strings(dev->of_node,
  420. static_prop_name);
  421. if (static_sup_cnt < 0) {
  422. dev_err(dev, "%s: Failed to get static supplies(%d)\n",
  423. __func__, static_sup_cnt);
  424. rc = static_sup_cnt;
  425. goto err_supply_cnt;
  426. }
  427. ond_sup_cnt = of_property_count_strings(dev->of_node, ond_prop_name);
  428. if (ond_sup_cnt < 0)
  429. ond_sup_cnt = 0;
  430. cp_sup_cnt = of_property_count_strings(dev->of_node,
  431. cp_prop_name);
  432. if (cp_sup_cnt < 0)
  433. cp_sup_cnt = 0;
  434. num_supplies = static_sup_cnt + ond_sup_cnt + cp_sup_cnt;
  435. if (num_supplies <= 0) {
  436. dev_err(dev, "%s: supply count is 0 or negative\n", __func__);
  437. rc = -EINVAL;
  438. goto err_supply_cnt;
  439. }
  440. cdc_reg = devm_kcalloc(dev, num_supplies,
  441. sizeof(struct cdc_regulator),
  442. GFP_KERNEL);
  443. if (!cdc_reg) {
  444. rc = -ENOMEM;
  445. goto err_mem_alloc;
  446. }
  447. rc = msm_cdc_parse_supplies(dev, cdc_reg, static_prop_name,
  448. static_sup_cnt, false);
  449. if (rc) {
  450. dev_err(dev, "%s: failed to parse static supplies(%d)\n",
  451. __func__, rc);
  452. goto err_sup;
  453. }
  454. rc = msm_cdc_parse_supplies(dev, &cdc_reg[static_sup_cnt],
  455. ond_prop_name, ond_sup_cnt,
  456. true);
  457. if (rc) {
  458. dev_err(dev, "%s: failed to parse demand supplies(%d)\n",
  459. __func__, rc);
  460. goto err_sup;
  461. }
  462. rc = msm_cdc_parse_supplies(dev,
  463. &cdc_reg[static_sup_cnt + ond_sup_cnt],
  464. cp_prop_name, cp_sup_cnt, true);
  465. if (rc) {
  466. dev_err(dev, "%s: failed to parse cp supplies(%d)\n",
  467. __func__, rc);
  468. goto err_sup;
  469. }
  470. *cdc_vreg = cdc_reg;
  471. *total_num_supplies = num_supplies;
  472. return 0;
  473. err_sup:
  474. err_supply_cnt:
  475. err_mem_alloc:
  476. return rc;
  477. }
  478. EXPORT_SYMBOL(msm_cdc_get_power_supplies);