of_regulator.c 5.7 KB

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