of_regulator.c 5.1 KB

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