of_regulator.c 9.0 KB

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