of_regulator.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 regulation_constraints *constraints = &(*init_data)->constraints;
  23. constraints->name = of_get_property(np, "regulator-name", NULL);
  24. min_uV = of_get_property(np, "regulator-min-microvolt", NULL);
  25. if (min_uV)
  26. constraints->min_uV = be32_to_cpu(*min_uV);
  27. max_uV = of_get_property(np, "regulator-max-microvolt", NULL);
  28. if (max_uV)
  29. constraints->max_uV = be32_to_cpu(*max_uV);
  30. /* Voltage change possible? */
  31. if (constraints->min_uV != constraints->max_uV)
  32. constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  33. /* Only one voltage? Then make sure it's set. */
  34. if (min_uV && max_uV && constraints->min_uV == constraints->max_uV)
  35. constraints->apply_uV = true;
  36. uV_offset = of_get_property(np, "regulator-microvolt-offset", NULL);
  37. if (uV_offset)
  38. constraints->uV_offset = be32_to_cpu(*uV_offset);
  39. min_uA = of_get_property(np, "regulator-min-microamp", NULL);
  40. if (min_uA)
  41. constraints->min_uA = be32_to_cpu(*min_uA);
  42. max_uA = of_get_property(np, "regulator-max-microamp", NULL);
  43. if (max_uA)
  44. constraints->max_uA = be32_to_cpu(*max_uA);
  45. /* Current change possible? */
  46. if (constraints->min_uA != constraints->max_uA)
  47. constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
  48. if (of_find_property(np, "regulator-boot-on", NULL))
  49. constraints->boot_on = true;
  50. if (of_find_property(np, "regulator-always-on", NULL))
  51. constraints->always_on = true;
  52. else /* status change should be possible if not always on. */
  53. constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
  54. ramp_delay = of_get_property(np, "regulator-ramp-delay", NULL);
  55. if (ramp_delay)
  56. constraints->ramp_delay = be32_to_cpu(*ramp_delay);
  57. }
  58. /**
  59. * of_get_regulator_init_data - extract regulator_init_data structure info
  60. * @dev: device requesting for regulator_init_data
  61. *
  62. * Populates regulator_init_data structure by extracting data from device
  63. * tree node, returns a pointer to the populated struture or NULL if memory
  64. * alloc fails.
  65. */
  66. struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
  67. struct device_node *node)
  68. {
  69. struct regulator_init_data *init_data;
  70. if (!node)
  71. return NULL;
  72. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  73. if (!init_data)
  74. return NULL; /* Out of memory? */
  75. of_get_regulation_constraints(node, &init_data);
  76. return init_data;
  77. }
  78. EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
  79. /**
  80. * of_regulator_match - extract regulator init data when node
  81. * property "regulator-compatible" matches with the regulator name.
  82. * @dev: device requesting the data
  83. * @node: parent device node of the regulators
  84. * @matches: match table for the regulators
  85. * @num_matches: number of entries in match table
  86. *
  87. * This function uses a match table specified by the regulator driver and
  88. * looks up the corresponding init data in the device tree if
  89. * regulator-compatible matches. Note that the match table is modified
  90. * in place.
  91. *
  92. * Returns the number of matches found or a negative error code on failure.
  93. */
  94. int of_regulator_match(struct device *dev, struct device_node *node,
  95. struct of_regulator_match *matches,
  96. unsigned int num_matches)
  97. {
  98. unsigned int count = 0;
  99. unsigned int i;
  100. const char *regulator_comp;
  101. struct device_node *child;
  102. if (!dev || !node)
  103. return -EINVAL;
  104. for_each_child_of_node(node, child) {
  105. regulator_comp = of_get_property(child,
  106. "regulator-compatible", NULL);
  107. if (!regulator_comp) {
  108. dev_err(dev, "regulator-compatible is missing for node %s\n",
  109. child->name);
  110. continue;
  111. }
  112. for (i = 0; i < num_matches; i++) {
  113. struct of_regulator_match *match = &matches[i];
  114. if (match->of_node)
  115. continue;
  116. if (strcmp(match->name, regulator_comp))
  117. continue;
  118. match->init_data =
  119. of_get_regulator_init_data(dev, child);
  120. if (!match->init_data) {
  121. dev_err(dev,
  122. "failed to parse DT for regulator %s\n",
  123. child->name);
  124. return -EINVAL;
  125. }
  126. match->of_node = child;
  127. count++;
  128. break;
  129. }
  130. }
  131. return count;
  132. }
  133. EXPORT_SYMBOL_GPL(of_regulator_match);