of_regulator.c 9.5 KB

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