of_regulator.c 5.2 KB

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