dsi_pwr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2019, 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. if (regs->vregs[i].off_min_voltage)
  158. (void)regulator_set_voltage(regs->vregs[i].vreg,
  159. regs->vregs[i].off_min_voltage,
  160. regs->vregs[i].max_voltage);
  161. (void)regulator_set_load(regs->vregs[i].vreg,
  162. regs->vregs[i].disable_load);
  163. (void)regulator_disable(regs->vregs[i].vreg);
  164. if (post_off_ms)
  165. usleep_range((post_off_ms * 1000),
  166. (post_off_ms * 1000) + 10);
  167. }
  168. }
  169. return 0;
  170. error_disable_opt_mode:
  171. (void)regulator_set_load(regs->vregs[i].vreg,
  172. regs->vregs[i].disable_load);
  173. error_disable_voltage:
  174. if (num_of_v > 0)
  175. (void)regulator_set_voltage(regs->vregs[i].vreg,
  176. 0, regs->vregs[i].max_voltage);
  177. error:
  178. for (i--; i >= 0; i--) {
  179. vreg = &regs->vregs[i];
  180. pre_off_ms = vreg->pre_off_sleep;
  181. post_off_ms = vreg->post_off_sleep;
  182. if (pre_off_ms)
  183. usleep_range((pre_off_ms * 1000),
  184. (pre_off_ms * 1000) + 10);
  185. (void)regulator_set_load(regs->vregs[i].vreg,
  186. regs->vregs[i].disable_load);
  187. num_of_v = regulator_count_voltages(regs->vregs[i].vreg);
  188. if (num_of_v > 0)
  189. (void)regulator_set_voltage(regs->vregs[i].vreg,
  190. 0, regs->vregs[i].max_voltage);
  191. (void)regulator_disable(regs->vregs[i].vreg);
  192. if (post_off_ms)
  193. usleep_range((post_off_ms * 1000),
  194. (post_off_ms * 1000) + 10);
  195. }
  196. return rc;
  197. }
  198. /**
  199. * dsi_pwr_of_get_vreg_data - Parse regulator supply information
  200. * @of_node: Device of node to parse for supply information.
  201. * @regs: Pointer where regulator information will be copied to.
  202. * @supply_name: Name of the supply node.
  203. *
  204. * return: error code in case of failure or 0 for success.
  205. */
  206. int dsi_pwr_of_get_vreg_data(struct dsi_parser_utils *utils,
  207. struct dsi_regulator_info *regs,
  208. char *supply_name)
  209. {
  210. int rc = 0;
  211. struct device_node *supply_root_node = NULL;
  212. if (!utils || !regs) {
  213. DSI_ERR("Bad params\n");
  214. return -EINVAL;
  215. }
  216. regs->count = 0;
  217. supply_root_node = utils->get_child_by_name(utils->data, supply_name);
  218. if (!supply_root_node) {
  219. supply_root_node = of_parse_phandle(utils->node,
  220. supply_name, 0);
  221. if (!supply_root_node) {
  222. DSI_DEBUG("No supply entry present for %s\n",
  223. supply_name);
  224. return -EINVAL;
  225. }
  226. }
  227. regs->count = utils->get_available_child_count(supply_root_node);
  228. if (regs->count == 0) {
  229. DSI_ERR("No vregs defined for %s\n", supply_name);
  230. return -EINVAL;
  231. }
  232. regs->vregs = kcalloc(regs->count, sizeof(*regs->vregs), GFP_KERNEL);
  233. if (!regs->vregs) {
  234. regs->count = 0;
  235. return -ENOMEM;
  236. }
  237. rc = dsi_pwr_parse_supply_node(utils, supply_root_node, regs);
  238. if (rc) {
  239. DSI_ERR("failed to parse supply node for %s, rc = %d\n",
  240. supply_name, rc);
  241. kfree(regs->vregs);
  242. regs->vregs = NULL;
  243. regs->count = 0;
  244. }
  245. return rc;
  246. }
  247. /**
  248. * dsi_pwr_get_dt_vreg_data - parse regulator supply information
  249. * @dev: Device whose of_node needs to be parsed.
  250. * @regs: Pointer where regulator information will be copied to.
  251. * @supply_name: Name of the supply node.
  252. *
  253. * return: error code in case of failure or 0 for success.
  254. */
  255. int dsi_pwr_get_dt_vreg_data(struct device *dev,
  256. struct dsi_regulator_info *regs,
  257. char *supply_name)
  258. {
  259. int rc = 0;
  260. struct device_node *of_node = NULL;
  261. struct device_node *supply_node = NULL;
  262. struct device_node *supply_root_node = NULL;
  263. struct dsi_parser_utils utils = *dsi_parser_get_of_utils();
  264. if (!dev || !regs) {
  265. DSI_ERR("Bad params\n");
  266. return -EINVAL;
  267. }
  268. of_node = dev->of_node;
  269. regs->count = 0;
  270. supply_root_node = of_get_child_by_name(of_node, supply_name);
  271. if (!supply_root_node) {
  272. supply_root_node = of_parse_phandle(of_node, supply_name, 0);
  273. if (!supply_root_node) {
  274. DSI_DEBUG("No supply entry present for %s\n",
  275. supply_name);
  276. return -EINVAL;
  277. }
  278. }
  279. for_each_child_of_node(supply_root_node, supply_node)
  280. regs->count++;
  281. if (regs->count == 0) {
  282. DSI_ERR("No vregs defined for %s\n", supply_name);
  283. return -EINVAL;
  284. }
  285. regs->vregs = devm_kcalloc(dev, regs->count, sizeof(*regs->vregs),
  286. GFP_KERNEL);
  287. if (!regs->vregs) {
  288. regs->count = 0;
  289. return -ENOMEM;
  290. }
  291. utils.data = of_node;
  292. utils.node = of_node;
  293. rc = dsi_pwr_parse_supply_node(&utils, supply_root_node, regs);
  294. if (rc) {
  295. DSI_ERR("failed to parse supply node for %s, rc = %d\n",
  296. supply_name, rc);
  297. devm_kfree(dev, regs->vregs);
  298. regs->vregs = NULL;
  299. regs->count = 0;
  300. }
  301. return rc;
  302. }
  303. /**
  304. * dsi_pwr_enable_regulator() - enable a set of regulators
  305. * @regs: Pointer to set of regulators to enable or disable.
  306. * @enable: Enable/Disable regulators.
  307. *
  308. * return: error code in case of failure or 0 for success.
  309. */
  310. int dsi_pwr_enable_regulator(struct dsi_regulator_info *regs, bool enable)
  311. {
  312. int rc = 0;
  313. if (regs->count == 0) {
  314. DSI_DEBUG("No valid regulators to enable\n");
  315. return 0;
  316. }
  317. if (!regs->vregs) {
  318. DSI_ERR("Invalid params\n");
  319. return -EINVAL;
  320. }
  321. if (enable) {
  322. if (regs->refcount == 0) {
  323. rc = dsi_pwr_enable_vregs(regs, true);
  324. if (rc)
  325. DSI_ERR("failed to enable regulators\n");
  326. }
  327. regs->refcount++;
  328. } else {
  329. if (regs->refcount == 0) {
  330. DSI_ERR("Unbalanced regulator off:%s\n",
  331. regs->vregs->vreg_name);
  332. } else {
  333. regs->refcount--;
  334. if (regs->refcount == 0) {
  335. rc = dsi_pwr_enable_vregs(regs, false);
  336. if (rc)
  337. DSI_ERR("failed to disable vregs\n");
  338. }
  339. }
  340. }
  341. return rc;
  342. }
  343. /*
  344. * dsi_pwr_panel_regulator_mode_set()
  345. * set the AB/IBB regulator mode for OLED panel
  346. * AOD mode entry and exit
  347. * @regs: Pointer to set of regulators to enable or disable.
  348. * @reg_name: Name of panel power we want to set.
  349. * @retulator_mode: Regulator mode values, like:
  350. * REGULATOR_MODE_INVALID
  351. * REGULATOR_MODE_FAST
  352. * REGULATOR_MODE_NORMAL
  353. * REGULATOR_MODE_IDLE
  354. * REGULATOR_MODE_STANDBY
  355. *
  356. * return: error code in case of failure or 0 for success.
  357. */
  358. int dsi_pwr_panel_regulator_mode_set(struct dsi_regulator_info *regs,
  359. const char *reg_name,
  360. int regulator_mode)
  361. {
  362. int i = 0, rc = 0;
  363. struct dsi_vreg *vreg;
  364. if (regs->count == 0)
  365. return -EINVAL;
  366. if (!regs->vregs)
  367. return -EINVAL;
  368. for (i = 0; i < regs->count; i++) {
  369. vreg = &regs->vregs[i];
  370. if (!strcmp(vreg->vreg_name, reg_name)) {
  371. rc = regulator_set_mode(vreg->vreg,
  372. regulator_mode);
  373. if (rc)
  374. DSI_ERR("Regulator %s set mode %d failed\n",
  375. vreg->vreg_name, rc);
  376. break;
  377. }
  378. }
  379. if (i >= regs->count) {
  380. DSI_ERR("Regulator %s was not found\n", reg_name);
  381. return -EINVAL;
  382. }
  383. return rc;
  384. }