of_regulator.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 void of_get_regulation_constraints(struct device_node *np,
  20. struct regulator_init_data **init_data)
  21. {
  22. const __be32 *min_uV, *max_uV;
  23. struct regulation_constraints *constraints = &(*init_data)->constraints;
  24. int ret;
  25. u32 pval;
  26. constraints->name = of_get_property(np, "regulator-name", NULL);
  27. min_uV = of_get_property(np, "regulator-min-microvolt", NULL);
  28. if (min_uV)
  29. constraints->min_uV = be32_to_cpu(*min_uV);
  30. max_uV = of_get_property(np, "regulator-max-microvolt", NULL);
  31. if (max_uV)
  32. constraints->max_uV = be32_to_cpu(*max_uV);
  33. /* Voltage change possible? */
  34. if (constraints->min_uV != constraints->max_uV)
  35. constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  36. /* Only one voltage? Then make sure it's set. */
  37. if (min_uV && max_uV && constraints->min_uV == constraints->max_uV)
  38. constraints->apply_uV = true;
  39. if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
  40. constraints->uV_offset = pval;
  41. if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
  42. constraints->min_uA = pval;
  43. if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
  44. constraints->max_uA = pval;
  45. /* Current change possible? */
  46. if (constraints->min_uA != constraints->max_uA)
  47. constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
  48. constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
  49. constraints->always_on = of_property_read_bool(np, "regulator-always-on");
  50. if (!constraints->always_on) /* status change should be possible. */
  51. constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
  52. if (of_property_read_bool(np, "regulator-allow-bypass"))
  53. constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
  54. ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
  55. if (!ret) {
  56. if (pval)
  57. constraints->ramp_delay = pval;
  58. else
  59. constraints->ramp_disable = true;
  60. }
  61. ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
  62. if (!ret)
  63. constraints->enable_time = pval;
  64. }
  65. /**
  66. * of_get_regulator_init_data - extract regulator_init_data structure info
  67. * @dev: device requesting for regulator_init_data
  68. *
  69. * Populates regulator_init_data structure by extracting data from device
  70. * tree node, returns a pointer to the populated struture or NULL if memory
  71. * alloc fails.
  72. */
  73. struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
  74. struct device_node *node)
  75. {
  76. struct regulator_init_data *init_data;
  77. if (!node)
  78. return NULL;
  79. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  80. if (!init_data)
  81. return NULL; /* Out of memory? */
  82. of_get_regulation_constraints(node, &init_data);
  83. return init_data;
  84. }
  85. EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
  86. struct devm_of_regulator_matches {
  87. struct of_regulator_match *matches;
  88. unsigned int num_matches;
  89. };
  90. static void devm_of_regulator_put_matches(struct device *dev, void *res)
  91. {
  92. struct devm_of_regulator_matches *devm_matches = res;
  93. int i;
  94. for (i = 0; i < devm_matches->num_matches; i++)
  95. of_node_put(devm_matches->matches[i].of_node);
  96. }
  97. /**
  98. * of_regulator_match - extract multiple regulator init data from device tree.
  99. * @dev: device requesting the data
  100. * @node: parent device node of the regulators
  101. * @matches: match table for the regulators
  102. * @num_matches: number of entries in match table
  103. *
  104. * This function uses a match table specified by the regulator driver to
  105. * parse regulator init data from the device tree. @node is expected to
  106. * contain a set of child nodes, each providing the init data for one
  107. * regulator. The data parsed from a child node will be matched to a regulator
  108. * based on either the deprecated property regulator-compatible if present,
  109. * or otherwise the child node's name. Note that the match table is modified
  110. * in place and an additional of_node reference is taken for each matched
  111. * regulator.
  112. *
  113. * Returns the number of matches found or a negative error code on failure.
  114. */
  115. int of_regulator_match(struct device *dev, struct device_node *node,
  116. struct of_regulator_match *matches,
  117. unsigned int num_matches)
  118. {
  119. unsigned int count = 0;
  120. unsigned int i;
  121. const char *name;
  122. struct device_node *child;
  123. struct devm_of_regulator_matches *devm_matches;
  124. if (!dev || !node)
  125. return -EINVAL;
  126. devm_matches = devres_alloc(devm_of_regulator_put_matches,
  127. sizeof(struct devm_of_regulator_matches),
  128. GFP_KERNEL);
  129. if (!devm_matches)
  130. return -ENOMEM;
  131. devm_matches->matches = matches;
  132. devm_matches->num_matches = num_matches;
  133. devres_add(dev, devm_matches);
  134. for (i = 0; i < num_matches; i++) {
  135. struct of_regulator_match *match = &matches[i];
  136. match->init_data = NULL;
  137. match->of_node = NULL;
  138. }
  139. for_each_child_of_node(node, child) {
  140. name = of_get_property(child,
  141. "regulator-compatible", NULL);
  142. if (!name)
  143. name = child->name;
  144. for (i = 0; i < num_matches; i++) {
  145. struct of_regulator_match *match = &matches[i];
  146. if (match->of_node)
  147. continue;
  148. if (strcmp(match->name, name))
  149. continue;
  150. match->init_data =
  151. of_get_regulator_init_data(dev, child);
  152. if (!match->init_data) {
  153. dev_err(dev,
  154. "failed to parse DT for regulator %s\n",
  155. child->name);
  156. return -EINVAL;
  157. }
  158. match->of_node = of_node_get(child);
  159. count++;
  160. break;
  161. }
  162. }
  163. return count;
  164. }
  165. EXPORT_SYMBOL_GPL(of_regulator_match);
  166. struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
  167. const struct regulator_desc *desc,
  168. struct device_node **node)
  169. {
  170. struct device_node *search, *child;
  171. struct regulator_init_data *init_data = NULL;
  172. const char *name;
  173. if (!dev->of_node || !desc->of_match)
  174. return NULL;
  175. if (desc->regulators_node)
  176. search = of_get_child_by_name(dev->of_node,
  177. desc->regulators_node);
  178. else
  179. search = dev->of_node;
  180. if (!search) {
  181. dev_err(dev, "Failed to find regulator container node\n");
  182. return NULL;
  183. }
  184. for_each_child_of_node(search, child) {
  185. name = of_get_property(child, "regulator-compatible", NULL);
  186. if (!name)
  187. name = child->name;
  188. if (strcmp(desc->of_match, name))
  189. continue;
  190. init_data = of_get_regulator_init_data(dev, child);
  191. if (!init_data) {
  192. dev_err(dev,
  193. "failed to parse DT for regulator %s\n",
  194. child->name);
  195. break;
  196. }
  197. of_node_get(child);
  198. *node = child;
  199. break;
  200. }
  201. of_node_put(search);
  202. return init_data;
  203. }