dsi_pwr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/of.h>
  6. #include <linux/delay.h>
  7. #include <linux/slab.h>
  8. #include "dsi_pwr.h"
  9. #include "dsi_parser.h"
  10. #include "dsi_defs.h"
  11. /*
  12. * dsi_pwr_parse_supply_node() - parse power supply node from root device node
  13. */
  14. static int dsi_pwr_parse_supply_node(struct dsi_parser_utils *utils,
  15. struct device_node *root,
  16. struct dsi_regulator_info *regs)
  17. {
  18. int rc = 0;
  19. int i = 0;
  20. u32 tmp = 0;
  21. struct device_node *node = NULL;
  22. dsi_for_each_child_node(root, node) {
  23. const char *st = NULL;
  24. rc = utils->read_string(node, "qcom,supply-name", &st);
  25. if (rc) {
  26. DSI_ERR("failed to read name, rc = %d\n", rc);
  27. goto error;
  28. }
  29. snprintf(regs->vregs[i].vreg_name,
  30. ARRAY_SIZE(regs->vregs[i].vreg_name),
  31. "%s", st);
  32. rc = utils->read_u32(node, "qcom,supply-min-voltage", &tmp);
  33. if (rc) {
  34. DSI_ERR("failed to read min voltage, rc = %d\n", rc);
  35. goto error;
  36. }
  37. regs->vregs[i].min_voltage = tmp;
  38. rc = utils->read_u32(node, "qcom,supply-max-voltage", &tmp);
  39. if (rc) {
  40. DSI_ERR("failed to read max voltage, rc = %d\n", rc);
  41. goto error;
  42. }
  43. regs->vregs[i].max_voltage = tmp;
  44. rc = utils->read_u32(node, "qcom,supply-enable-load", &tmp);
  45. if (rc) {
  46. DSI_ERR("failed to read enable load, rc = %d\n", rc);
  47. goto error;
  48. }
  49. regs->vregs[i].enable_load = tmp;
  50. rc = utils->read_u32(node, "qcom,supply-disable-load", &tmp);
  51. if (rc) {
  52. DSI_ERR("failed to read disable load, rc = %d\n", rc);
  53. goto error;
  54. }
  55. regs->vregs[i].disable_load = tmp;
  56. /* Optional values */
  57. rc = utils->read_u32(node, "qcom,supply-off-min-voltage", &tmp);
  58. if (rc) {
  59. DSI_DEBUG("off-min-voltage not specified\n");
  60. rc = 0;
  61. } else {
  62. regs->vregs[i].off_min_voltage = tmp;
  63. }
  64. rc = utils->read_u32(node, "qcom,supply-pre-on-sleep", &tmp);
  65. if (rc) {
  66. DSI_DEBUG("pre-on-sleep not specified\n");
  67. rc = 0;
  68. } else {
  69. regs->vregs[i].pre_on_sleep = tmp;
  70. }
  71. rc = utils->read_u32(node, "qcom,supply-pre-off-sleep", &tmp);
  72. if (rc) {
  73. DSI_DEBUG("pre-off-sleep not specified\n");
  74. rc = 0;
  75. } else {
  76. regs->vregs[i].pre_off_sleep = tmp;
  77. }
  78. rc = utils->read_u32(node, "qcom,supply-post-on-sleep", &tmp);
  79. if (rc) {
  80. DSI_DEBUG("post-on-sleep not specified\n");
  81. rc = 0;
  82. } else {
  83. regs->vregs[i].post_on_sleep = tmp;
  84. }
  85. rc = utils->read_u32(node, "qcom,supply-post-off-sleep", &tmp);
  86. if (rc) {
  87. DSI_DEBUG("post-off-sleep not specified\n");
  88. rc = 0;
  89. } else {
  90. regs->vregs[i].post_off_sleep = tmp;
  91. }
  92. DSI_DEBUG("[%s] minv=%d maxv=%d, en_load=%d, dis_load=%d\n",
  93. regs->vregs[i].vreg_name,
  94. regs->vregs[i].min_voltage,
  95. regs->vregs[i].max_voltage,
  96. regs->vregs[i].enable_load,
  97. regs->vregs[i].disable_load);
  98. ++i;
  99. }
  100. error:
  101. return rc;
  102. }
  103. /**
  104. * dsi_pwr_enable_vregs() - enable/disable regulators
  105. */
  106. static int dsi_pwr_enable_vregs(struct dsi_regulator_info *regs, bool enable)
  107. {
  108. int rc = 0, i = 0;
  109. struct dsi_vreg *vreg;
  110. int num_of_v = 0;
  111. u32 pre_on_ms, post_on_ms;
  112. u32 pre_off_ms, post_off_ms;
  113. if (enable) {
  114. for (i = 0; i < regs->count; i++) {
  115. vreg = &regs->vregs[i];
  116. pre_on_ms = vreg->pre_on_sleep;
  117. post_on_ms = vreg->post_on_sleep;
  118. if (vreg->pre_on_sleep)
  119. usleep_range((pre_on_ms * 1000),
  120. (pre_on_ms * 1000) + 10);
  121. rc = regulator_set_load(vreg->vreg,
  122. vreg->enable_load);
  123. if (rc < 0) {
  124. DSI_ERR("Setting optimum mode failed for %s\n",
  125. vreg->vreg_name);
  126. goto error;
  127. }
  128. num_of_v = regulator_count_voltages(vreg->vreg);
  129. if (num_of_v > 0) {
  130. rc = regulator_set_voltage(vreg->vreg,
  131. vreg->min_voltage,
  132. vreg->max_voltage);
  133. if (rc) {
  134. DSI_ERR("Set voltage(%s) fail, rc=%d\n",
  135. vreg->vreg_name, rc);
  136. goto error_disable_opt_mode;
  137. }
  138. }
  139. rc = regulator_enable(vreg->vreg);
  140. if (rc) {
  141. DSI_ERR("enable failed for %s, rc=%d\n",
  142. vreg->vreg_name, rc);
  143. goto error_disable_voltage;
  144. }
  145. if (vreg->post_on_sleep)
  146. usleep_range((post_on_ms * 1000),
  147. (post_on_ms * 1000) + 10);
  148. }
  149. } else {
  150. for (i = (regs->count - 1); i >= 0; i--) {
  151. vreg = &regs->vregs[i];
  152. pre_off_ms = vreg->pre_off_sleep;
  153. post_off_ms = vreg->post_off_sleep;
  154. if (pre_off_ms)
  155. usleep_range((pre_off_ms * 1000),
  156. (pre_off_ms * 1000) + 10);
  157. (void)regulator_disable(regs->vregs[i].vreg);
  158. if (post_off_ms)
  159. usleep_range((post_off_ms * 1000),
  160. (post_off_ms * 1000) + 10);
  161. (void)regulator_set_load(regs->vregs[i].vreg,
  162. regs->vregs[i].disable_load);
  163. num_of_v = regulator_count_voltages(vreg->vreg);
  164. if (num_of_v > 0)
  165. (void)regulator_set_voltage(regs->vregs[i].vreg,
  166. regs->vregs[i].off_min_voltage,
  167. regs->vregs[i].max_voltage);
  168. }
  169. }
  170. return 0;
  171. error_disable_opt_mode:
  172. (void)regulator_set_load(regs->vregs[i].vreg,
  173. regs->vregs[i].disable_load);
  174. error_disable_voltage:
  175. if (num_of_v > 0)
  176. (void)regulator_set_voltage(regs->vregs[i].vreg,
  177. 0, regs->vregs[i].max_voltage);
  178. error:
  179. for (i--; i >= 0; i--) {
  180. vreg = &regs->vregs[i];
  181. pre_off_ms = vreg->pre_off_sleep;
  182. post_off_ms = vreg->post_off_sleep;
  183. if (pre_off_ms)
  184. usleep_range((pre_off_ms * 1000),
  185. (pre_off_ms * 1000) + 10);
  186. (void)regulator_disable(regs->vregs[i].vreg);
  187. if (post_off_ms)
  188. usleep_range((post_off_ms * 1000),
  189. (post_off_ms * 1000) + 10);
  190. (void)regulator_set_load(regs->vregs[i].vreg,
  191. regs->vregs[i].disable_load);
  192. num_of_v = regulator_count_voltages(regs->vregs[i].vreg);
  193. if (num_of_v > 0)
  194. (void)regulator_set_voltage(regs->vregs[i].vreg,
  195. 0, regs->vregs[i].max_voltage);
  196. }
  197. return rc;
  198. }
  199. /**
  200. * dsi_pwr_of_get_vreg_data - Parse regulator supply information
  201. * @of_node: Device of node to parse for supply information.
  202. * @regs: Pointer where regulator information will be copied to.
  203. * @supply_name: Name of the supply node.
  204. *
  205. * return: error code in case of failure or 0 for success.
  206. */
  207. int dsi_pwr_of_get_vreg_data(struct dsi_parser_utils *utils,
  208. struct dsi_regulator_info *regs,
  209. char *supply_name)
  210. {
  211. int rc = 0;
  212. struct device_node *supply_root_node = NULL;
  213. if (!utils || !regs) {
  214. DSI_ERR("Bad params\n");
  215. return -EINVAL;
  216. }
  217. regs->count = 0;
  218. supply_root_node = utils->get_child_by_name(utils->data, supply_name);
  219. if (!supply_root_node) {
  220. supply_root_node = of_parse_phandle(utils->node,
  221. supply_name, 0);
  222. if (!supply_root_node) {
  223. DSI_DEBUG("No supply entry present for %s\n",
  224. supply_name);
  225. return -EINVAL;
  226. }
  227. }
  228. regs->count = utils->get_available_child_count(supply_root_node);
  229. if (regs->count == 0) {
  230. DSI_ERR("No vregs defined for %s\n", supply_name);
  231. return -EINVAL;
  232. }
  233. regs->vregs = kcalloc(regs->count, sizeof(*regs->vregs), GFP_KERNEL);
  234. if (!regs->vregs) {
  235. regs->count = 0;
  236. return -ENOMEM;
  237. }
  238. rc = dsi_pwr_parse_supply_node(utils, supply_root_node, regs);
  239. if (rc) {
  240. DSI_ERR("failed to parse supply node for %s, rc = %d\n",
  241. supply_name, rc);
  242. kfree(regs->vregs);
  243. regs->vregs = NULL;
  244. regs->count = 0;
  245. }
  246. return rc;
  247. }
  248. /**
  249. * dsi_pwr_get_dt_vreg_data - parse regulator supply information
  250. * @dev: Device whose of_node needs to be parsed.
  251. * @regs: Pointer where regulator information will be copied to.
  252. * @supply_name: Name of the supply node.
  253. *
  254. * return: error code in case of failure or 0 for success.
  255. */
  256. int dsi_pwr_get_dt_vreg_data(struct device *dev,
  257. struct dsi_regulator_info *regs,
  258. char *supply_name)
  259. {
  260. int rc = 0;
  261. struct device_node *of_node = NULL;
  262. struct device_node *supply_node = NULL;
  263. struct device_node *supply_root_node = NULL;
  264. struct dsi_parser_utils utils = *dsi_parser_get_of_utils();
  265. if (!dev || !regs) {
  266. DSI_ERR("Bad params\n");
  267. return -EINVAL;
  268. }
  269. of_node = dev->of_node;
  270. regs->count = 0;
  271. supply_root_node = of_get_child_by_name(of_node, supply_name);
  272. if (!supply_root_node) {
  273. supply_root_node = of_parse_phandle(of_node, supply_name, 0);
  274. if (!supply_root_node) {
  275. DSI_DEBUG("No supply entry present for %s\n",
  276. supply_name);
  277. return -EINVAL;
  278. }
  279. }
  280. for_each_child_of_node(supply_root_node, supply_node)
  281. regs->count++;
  282. if (regs->count == 0) {
  283. DSI_ERR("No vregs defined for %s\n", supply_name);
  284. return -EINVAL;
  285. }
  286. regs->vregs = devm_kcalloc(dev, regs->count, sizeof(*regs->vregs),
  287. GFP_KERNEL);
  288. if (!regs->vregs) {
  289. regs->count = 0;
  290. return -ENOMEM;
  291. }
  292. utils.data = of_node;
  293. utils.node = of_node;
  294. rc = dsi_pwr_parse_supply_node(&utils, supply_root_node, regs);
  295. if (rc) {
  296. DSI_ERR("failed to parse supply node for %s, rc = %d\n",
  297. supply_name, rc);
  298. devm_kfree(dev, regs->vregs);
  299. regs->vregs = NULL;
  300. regs->count = 0;
  301. }
  302. return rc;
  303. }
  304. /**
  305. * dsi_pwr_enable_regulator() - enable a set of regulators
  306. * @regs: Pointer to set of regulators to enable or disable.
  307. * @enable: Enable/Disable regulators.
  308. *
  309. * return: error code in case of failure or 0 for success.
  310. */
  311. int dsi_pwr_enable_regulator(struct dsi_regulator_info *regs, bool enable)
  312. {
  313. int rc = 0;
  314. if (regs->count == 0) {
  315. DSI_DEBUG("No valid regulators to enable\n");
  316. return 0;
  317. }
  318. if (!regs->vregs) {
  319. DSI_ERR("Invalid params\n");
  320. return -EINVAL;
  321. }
  322. if (enable) {
  323. if (regs->refcount == 0) {
  324. rc = dsi_pwr_enable_vregs(regs, true);
  325. if (rc)
  326. DSI_ERR("failed to enable regulators\n");
  327. }
  328. regs->refcount++;
  329. } else {
  330. if (regs->refcount == 0) {
  331. DSI_ERR("Unbalanced regulator off:%s\n",
  332. regs->vregs->vreg_name);
  333. } else {
  334. regs->refcount--;
  335. if (regs->refcount == 0) {
  336. rc = dsi_pwr_enable_vregs(regs, false);
  337. if (rc)
  338. DSI_ERR("failed to disable vregs\n");
  339. }
  340. }
  341. }
  342. return rc;
  343. }
  344. /*
  345. * dsi_pwr_panel_regulator_mode_set()
  346. * set the AB/IBB regulator mode for OLED panel
  347. * AOD mode entry and exit
  348. * @regs: Pointer to set of regulators to enable or disable.
  349. * @reg_name: Name of panel power we want to set.
  350. * @retulator_mode: Regulator mode values, like:
  351. * REGULATOR_MODE_INVALID
  352. * REGULATOR_MODE_FAST
  353. * REGULATOR_MODE_NORMAL
  354. * REGULATOR_MODE_IDLE
  355. * REGULATOR_MODE_STANDBY
  356. *
  357. * return: error code in case of failure or 0 for success.
  358. */
  359. int dsi_pwr_panel_regulator_mode_set(struct dsi_regulator_info *regs,
  360. const char *reg_name,
  361. int regulator_mode)
  362. {
  363. int i = 0, rc = 0;
  364. struct dsi_vreg *vreg;
  365. if (regs->count == 0)
  366. return -EINVAL;
  367. if (!regs->vregs)
  368. return -EINVAL;
  369. for (i = 0; i < regs->count; i++) {
  370. vreg = &regs->vregs[i];
  371. if (!strcmp(vreg->vreg_name, reg_name)) {
  372. rc = regulator_set_mode(vreg->vreg,
  373. regulator_mode);
  374. if (rc)
  375. DSI_ERR("Regulator %s set mode %d failed\n",
  376. vreg->vreg_name, rc);
  377. break;
  378. }
  379. }
  380. if (i >= regs->count) {
  381. DSI_DEBUG("Regulator %s was not found\n", reg_name);
  382. return -EINVAL;
  383. }
  384. return rc;
  385. }