of_regulator.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * OF helpers for regulator framework
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Rajendra Nayak <rnayak@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/of.h>
  15. #include <linux/regulator/machine.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regulator/of_regulator.h>
  18. #include "internal.h"
  19. static const char *const regulator_states[PM_SUSPEND_MAX + 1] = {
  20. [PM_SUSPEND_MEM] = "regulator-state-mem",
  21. [PM_SUSPEND_MAX] = "regulator-state-disk",
  22. };
  23. static void of_get_regulation_constraints(struct device_node *np,
  24. struct regulator_init_data **init_data,
  25. const struct regulator_desc *desc)
  26. {
  27. struct regulation_constraints *constraints = &(*init_data)->constraints;
  28. struct regulator_state *suspend_state;
  29. struct device_node *suspend_np;
  30. int ret, i;
  31. u32 pval;
  32. constraints->name = of_get_property(np, "regulator-name", NULL);
  33. if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
  34. constraints->min_uV = pval;
  35. if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
  36. constraints->max_uV = pval;
  37. /* Voltage change possible? */
  38. if (constraints->min_uV != constraints->max_uV)
  39. constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  40. /* Do we have a voltage range, if so try to apply it? */
  41. if (constraints->min_uV && constraints->max_uV)
  42. constraints->apply_uV = true;
  43. if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
  44. constraints->uV_offset = pval;
  45. if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
  46. constraints->min_uA = pval;
  47. if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
  48. constraints->max_uA = pval;
  49. if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
  50. &pval))
  51. constraints->ilim_uA = pval;
  52. /* Current change possible? */
  53. if (constraints->min_uA != constraints->max_uA)
  54. constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
  55. constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
  56. constraints->always_on = of_property_read_bool(np, "regulator-always-on");
  57. if (!constraints->always_on) /* status change should be possible. */
  58. constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
  59. constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
  60. if (of_property_read_bool(np, "regulator-allow-bypass"))
  61. constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
  62. if (of_property_read_bool(np, "regulator-allow-set-load"))
  63. constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
  64. ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
  65. if (!ret) {
  66. if (pval)
  67. constraints->ramp_delay = pval;
  68. else
  69. constraints->ramp_disable = true;
  70. }
  71. ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
  72. if (!ret)
  73. constraints->settling_time = pval;
  74. ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
  75. if (!ret)
  76. constraints->enable_time = pval;
  77. constraints->soft_start = of_property_read_bool(np,
  78. "regulator-soft-start");
  79. ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
  80. if (!ret) {
  81. constraints->active_discharge =
  82. (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
  83. REGULATOR_ACTIVE_DISCHARGE_DISABLE;
  84. }
  85. if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
  86. if (desc && desc->of_map_mode) {
  87. ret = desc->of_map_mode(pval);
  88. if (ret == -EINVAL)
  89. pr_err("%s: invalid mode %u\n", np->name, pval);
  90. else
  91. constraints->initial_mode = ret;
  92. } else {
  93. pr_warn("%s: mapping for mode %d not defined\n",
  94. np->name, pval);
  95. }
  96. }
  97. if (!of_property_read_u32(np, "regulator-system-load", &pval))
  98. constraints->system_load = pval;
  99. constraints->over_current_protection = of_property_read_bool(np,
  100. "regulator-over-current-protection");
  101. for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
  102. switch (i) {
  103. case PM_SUSPEND_MEM:
  104. suspend_state = &constraints->state_mem;
  105. break;
  106. case PM_SUSPEND_MAX:
  107. suspend_state = &constraints->state_disk;
  108. break;
  109. case PM_SUSPEND_ON:
  110. case PM_SUSPEND_FREEZE:
  111. case PM_SUSPEND_STANDBY:
  112. default:
  113. continue;
  114. }
  115. suspend_np = of_get_child_by_name(np, regulator_states[i]);
  116. if (!suspend_np || !suspend_state)
  117. continue;
  118. if (!of_property_read_u32(suspend_np, "regulator-mode",
  119. &pval)) {
  120. if (desc && desc->of_map_mode) {
  121. ret = desc->of_map_mode(pval);
  122. if (ret == -EINVAL)
  123. pr_err("%s: invalid mode %u\n",
  124. np->name, pval);
  125. else
  126. suspend_state->mode = ret;
  127. } else {
  128. pr_warn("%s: mapping for mode %d not defined\n",
  129. np->name, pval);
  130. }
  131. }
  132. if (of_property_read_bool(suspend_np,
  133. "regulator-on-in-suspend"))
  134. suspend_state->enabled = true;
  135. else if (of_property_read_bool(suspend_np,
  136. "regulator-off-in-suspend"))
  137. suspend_state->disabled = true;
  138. if (!of_property_read_u32(suspend_np,
  139. "regulator-suspend-microvolt", &pval))
  140. suspend_state->uV = pval;
  141. if (i == PM_SUSPEND_MEM)
  142. constraints->initial_state = PM_SUSPEND_MEM;
  143. of_node_put(suspend_np);
  144. suspend_state = NULL;
  145. suspend_np = NULL;
  146. }
  147. }
  148. /**
  149. * of_get_regulator_init_data - extract regulator_init_data structure info
  150. * @dev: device requesting for regulator_init_data
  151. * @node: regulator device node
  152. * @desc: regulator description
  153. *
  154. * Populates regulator_init_data structure by extracting data from device
  155. * tree node, returns a pointer to the populated struture or NULL if memory
  156. * alloc fails.
  157. */
  158. struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
  159. struct device_node *node,
  160. const struct regulator_desc *desc)
  161. {
  162. struct regulator_init_data *init_data;
  163. if (!node)
  164. return NULL;
  165. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  166. if (!init_data)
  167. return NULL; /* Out of memory? */
  168. of_get_regulation_constraints(node, &init_data, desc);
  169. return init_data;
  170. }
  171. EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
  172. struct devm_of_regulator_matches {
  173. struct of_regulator_match *matches;
  174. unsigned int num_matches;
  175. };
  176. static void devm_of_regulator_put_matches(struct device *dev, void *res)
  177. {
  178. struct devm_of_regulator_matches *devm_matches = res;
  179. int i;
  180. for (i = 0; i < devm_matches->num_matches; i++)
  181. of_node_put(devm_matches->matches[i].of_node);
  182. }
  183. /**
  184. * of_regulator_match - extract multiple regulator init data from device tree.
  185. * @dev: device requesting the data
  186. * @node: parent device node of the regulators
  187. * @matches: match table for the regulators
  188. * @num_matches: number of entries in match table
  189. *
  190. * This function uses a match table specified by the regulator driver to
  191. * parse regulator init data from the device tree. @node is expected to
  192. * contain a set of child nodes, each providing the init data for one
  193. * regulator. The data parsed from a child node will be matched to a regulator
  194. * based on either the deprecated property regulator-compatible if present,
  195. * or otherwise the child node's name. Note that the match table is modified
  196. * in place and an additional of_node reference is taken for each matched
  197. * regulator.
  198. *
  199. * Returns the number of matches found or a negative error code on failure.
  200. */
  201. int of_regulator_match(struct device *dev, struct device_node *node,
  202. struct of_regulator_match *matches,
  203. unsigned int num_matches)
  204. {
  205. unsigned int count = 0;
  206. unsigned int i;
  207. const char *name;
  208. struct device_node *child;
  209. struct devm_of_regulator_matches *devm_matches;
  210. if (!dev || !node)
  211. return -EINVAL;
  212. devm_matches = devres_alloc(devm_of_regulator_put_matches,
  213. sizeof(struct devm_of_regulator_matches),
  214. GFP_KERNEL);
  215. if (!devm_matches)
  216. return -ENOMEM;
  217. devm_matches->matches = matches;
  218. devm_matches->num_matches = num_matches;
  219. devres_add(dev, devm_matches);
  220. for (i = 0; i < num_matches; i++) {
  221. struct of_regulator_match *match = &matches[i];
  222. match->init_data = NULL;
  223. match->of_node = NULL;
  224. }
  225. for_each_child_of_node(node, child) {
  226. name = of_get_property(child,
  227. "regulator-compatible", NULL);
  228. if (!name)
  229. name = child->name;
  230. for (i = 0; i < num_matches; i++) {
  231. struct of_regulator_match *match = &matches[i];
  232. if (match->of_node)
  233. continue;
  234. if (strcmp(match->name, name))
  235. continue;
  236. match->init_data =
  237. of_get_regulator_init_data(dev, child,
  238. match->desc);
  239. if (!match->init_data) {
  240. dev_err(dev,
  241. "failed to parse DT for regulator %s\n",
  242. child->name);
  243. return -EINVAL;
  244. }
  245. match->of_node = of_node_get(child);
  246. count++;
  247. break;
  248. }
  249. }
  250. return count;
  251. }
  252. EXPORT_SYMBOL_GPL(of_regulator_match);
  253. struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
  254. const struct regulator_desc *desc,
  255. struct regulator_config *config,
  256. struct device_node **node)
  257. {
  258. struct device_node *search, *child;
  259. struct regulator_init_data *init_data = NULL;
  260. const char *name;
  261. if (!dev->of_node || !desc->of_match)
  262. return NULL;
  263. if (desc->regulators_node)
  264. search = of_get_child_by_name(dev->of_node,
  265. desc->regulators_node);
  266. else
  267. search = dev->of_node;
  268. if (!search) {
  269. dev_dbg(dev, "Failed to find regulator container node '%s'\n",
  270. desc->regulators_node);
  271. return NULL;
  272. }
  273. for_each_available_child_of_node(search, child) {
  274. name = of_get_property(child, "regulator-compatible", NULL);
  275. if (!name)
  276. name = child->name;
  277. if (strcmp(desc->of_match, name))
  278. continue;
  279. init_data = of_get_regulator_init_data(dev, child, desc);
  280. if (!init_data) {
  281. dev_err(dev,
  282. "failed to parse DT for regulator %s\n",
  283. child->name);
  284. break;
  285. }
  286. if (desc->of_parse_cb) {
  287. if (desc->of_parse_cb(child, desc, config)) {
  288. dev_err(dev,
  289. "driver callback failed to parse DT for regulator %s\n",
  290. child->name);
  291. init_data = NULL;
  292. break;
  293. }
  294. }
  295. of_node_get(child);
  296. *node = child;
  297. break;
  298. }
  299. of_node_put(search);
  300. return init_data;
  301. }