of_regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 const char *const regulator_states[PM_SUSPEND_MAX + 1] = {
  20. [PM_SUSPEND_MEM] = "regulator-state-mem",
  21. [PM_SUSPEND_MAX] = "regulator-state-disk",
  22. };
  23. static void of_get_regulation_constraints(struct device_node *np,
  24. struct regulator_init_data **init_data,
  25. const struct regulator_desc *desc)
  26. {
  27. struct regulation_constraints *constraints = &(*init_data)->constraints;
  28. struct regulator_state *suspend_state;
  29. struct device_node *suspend_np;
  30. unsigned int mode;
  31. int ret, i;
  32. u32 pval;
  33. constraints->name = of_get_property(np, "regulator-name", NULL);
  34. if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
  35. constraints->min_uV = pval;
  36. if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
  37. constraints->max_uV = pval;
  38. /* Voltage change possible? */
  39. if (constraints->min_uV != constraints->max_uV)
  40. constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  41. /* Do we have a voltage range, if so try to apply it? */
  42. if (constraints->min_uV && constraints->max_uV)
  43. constraints->apply_uV = true;
  44. if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
  45. constraints->uV_offset = pval;
  46. if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
  47. constraints->min_uA = pval;
  48. if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
  49. constraints->max_uA = pval;
  50. if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
  51. &pval))
  52. constraints->ilim_uA = pval;
  53. /* Current change possible? */
  54. if (constraints->min_uA != constraints->max_uA)
  55. constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
  56. constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
  57. constraints->always_on = of_property_read_bool(np, "regulator-always-on");
  58. if (!constraints->always_on) /* status change should be possible. */
  59. constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
  60. constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
  61. if (of_property_read_bool(np, "regulator-allow-bypass"))
  62. constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
  63. if (of_property_read_bool(np, "regulator-allow-set-load"))
  64. constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
  65. ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
  66. if (!ret) {
  67. if (pval)
  68. constraints->ramp_delay = pval;
  69. else
  70. constraints->ramp_disable = true;
  71. }
  72. ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
  73. if (!ret)
  74. constraints->settling_time = pval;
  75. ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
  76. if (!ret)
  77. constraints->settling_time_up = pval;
  78. if (constraints->settling_time_up && constraints->settling_time) {
  79. pr_warn("%s: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
  80. np->name);
  81. constraints->settling_time_up = 0;
  82. }
  83. ret = of_property_read_u32(np, "regulator-settling-time-down-us",
  84. &pval);
  85. if (!ret)
  86. constraints->settling_time_down = pval;
  87. if (constraints->settling_time_down && constraints->settling_time) {
  88. pr_warn("%s: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
  89. np->name);
  90. constraints->settling_time_down = 0;
  91. }
  92. ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
  93. if (!ret)
  94. constraints->enable_time = pval;
  95. constraints->soft_start = of_property_read_bool(np,
  96. "regulator-soft-start");
  97. ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
  98. if (!ret) {
  99. constraints->active_discharge =
  100. (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
  101. REGULATOR_ACTIVE_DISCHARGE_DISABLE;
  102. }
  103. if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
  104. if (desc && desc->of_map_mode) {
  105. mode = desc->of_map_mode(pval);
  106. if (mode == REGULATOR_MODE_INVALID)
  107. pr_err("%s: invalid mode %u\n", np->name, pval);
  108. else
  109. constraints->initial_mode = mode;
  110. } else {
  111. pr_warn("%s: mapping for mode %d not defined\n",
  112. np->name, pval);
  113. }
  114. }
  115. if (!of_property_read_u32(np, "regulator-system-load", &pval))
  116. constraints->system_load = pval;
  117. constraints->over_current_protection = of_property_read_bool(np,
  118. "regulator-over-current-protection");
  119. for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
  120. switch (i) {
  121. case PM_SUSPEND_MEM:
  122. suspend_state = &constraints->state_mem;
  123. break;
  124. case PM_SUSPEND_MAX:
  125. suspend_state = &constraints->state_disk;
  126. break;
  127. case PM_SUSPEND_ON:
  128. case PM_SUSPEND_TO_IDLE:
  129. case PM_SUSPEND_STANDBY:
  130. default:
  131. continue;
  132. }
  133. suspend_np = of_get_child_by_name(np, regulator_states[i]);
  134. if (!suspend_np || !suspend_state)
  135. continue;
  136. if (!of_property_read_u32(suspend_np, "regulator-mode",
  137. &pval)) {
  138. if (desc && desc->of_map_mode) {
  139. mode = desc->of_map_mode(pval);
  140. if (mode == REGULATOR_MODE_INVALID)
  141. pr_err("%s: invalid mode %u\n",
  142. np->name, pval);
  143. else
  144. suspend_state->mode = mode;
  145. } else {
  146. pr_warn("%s: mapping for mode %d not defined\n",
  147. np->name, pval);
  148. }
  149. }
  150. if (of_property_read_bool(suspend_np,
  151. "regulator-on-in-suspend"))
  152. suspend_state->enabled = true;
  153. else if (of_property_read_bool(suspend_np,
  154. "regulator-off-in-suspend"))
  155. suspend_state->disabled = true;
  156. if (!of_property_read_u32(suspend_np,
  157. "regulator-suspend-microvolt", &pval))
  158. suspend_state->uV = pval;
  159. if (i == PM_SUSPEND_MEM)
  160. constraints->initial_state = PM_SUSPEND_MEM;
  161. of_node_put(suspend_np);
  162. suspend_state = NULL;
  163. suspend_np = NULL;
  164. }
  165. }
  166. /**
  167. * of_get_regulator_init_data - extract regulator_init_data structure info
  168. * @dev: device requesting for regulator_init_data
  169. * @node: regulator device node
  170. * @desc: regulator description
  171. *
  172. * Populates regulator_init_data structure by extracting data from device
  173. * tree node, returns a pointer to the populated struture or NULL if memory
  174. * alloc fails.
  175. */
  176. struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
  177. struct device_node *node,
  178. const struct regulator_desc *desc)
  179. {
  180. struct regulator_init_data *init_data;
  181. if (!node)
  182. return NULL;
  183. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  184. if (!init_data)
  185. return NULL; /* Out of memory? */
  186. of_get_regulation_constraints(node, &init_data, desc);
  187. return init_data;
  188. }
  189. EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
  190. struct devm_of_regulator_matches {
  191. struct of_regulator_match *matches;
  192. unsigned int num_matches;
  193. };
  194. static void devm_of_regulator_put_matches(struct device *dev, void *res)
  195. {
  196. struct devm_of_regulator_matches *devm_matches = res;
  197. int i;
  198. for (i = 0; i < devm_matches->num_matches; i++)
  199. of_node_put(devm_matches->matches[i].of_node);
  200. }
  201. /**
  202. * of_regulator_match - extract multiple regulator init data from device tree.
  203. * @dev: device requesting the data
  204. * @node: parent device node of the regulators
  205. * @matches: match table for the regulators
  206. * @num_matches: number of entries in match table
  207. *
  208. * This function uses a match table specified by the regulator driver to
  209. * parse regulator init data from the device tree. @node is expected to
  210. * contain a set of child nodes, each providing the init data for one
  211. * regulator. The data parsed from a child node will be matched to a regulator
  212. * based on either the deprecated property regulator-compatible if present,
  213. * or otherwise the child node's name. Note that the match table is modified
  214. * in place and an additional of_node reference is taken for each matched
  215. * regulator.
  216. *
  217. * Returns the number of matches found or a negative error code on failure.
  218. */
  219. int of_regulator_match(struct device *dev, struct device_node *node,
  220. struct of_regulator_match *matches,
  221. unsigned int num_matches)
  222. {
  223. unsigned int count = 0;
  224. unsigned int i;
  225. const char *name;
  226. struct device_node *child;
  227. struct devm_of_regulator_matches *devm_matches;
  228. if (!dev || !node)
  229. return -EINVAL;
  230. devm_matches = devres_alloc(devm_of_regulator_put_matches,
  231. sizeof(struct devm_of_regulator_matches),
  232. GFP_KERNEL);
  233. if (!devm_matches)
  234. return -ENOMEM;
  235. devm_matches->matches = matches;
  236. devm_matches->num_matches = num_matches;
  237. devres_add(dev, devm_matches);
  238. for (i = 0; i < num_matches; i++) {
  239. struct of_regulator_match *match = &matches[i];
  240. match->init_data = NULL;
  241. match->of_node = NULL;
  242. }
  243. for_each_child_of_node(node, child) {
  244. name = of_get_property(child,
  245. "regulator-compatible", NULL);
  246. if (!name)
  247. name = child->name;
  248. for (i = 0; i < num_matches; i++) {
  249. struct of_regulator_match *match = &matches[i];
  250. if (match->of_node)
  251. continue;
  252. if (strcmp(match->name, name))
  253. continue;
  254. match->init_data =
  255. of_get_regulator_init_data(dev, child,
  256. match->desc);
  257. if (!match->init_data) {
  258. dev_err(dev,
  259. "failed to parse DT for regulator %s\n",
  260. child->name);
  261. of_node_put(child);
  262. return -EINVAL;
  263. }
  264. match->of_node = of_node_get(child);
  265. count++;
  266. break;
  267. }
  268. }
  269. return count;
  270. }
  271. EXPORT_SYMBOL_GPL(of_regulator_match);
  272. struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
  273. const struct regulator_desc *desc,
  274. struct regulator_config *config,
  275. struct device_node **node)
  276. {
  277. struct device_node *search, *child;
  278. struct regulator_init_data *init_data = NULL;
  279. const char *name;
  280. if (!dev->of_node || !desc->of_match)
  281. return NULL;
  282. if (desc->regulators_node)
  283. search = of_get_child_by_name(dev->of_node,
  284. desc->regulators_node);
  285. else
  286. search = of_node_get(dev->of_node);
  287. if (!search) {
  288. dev_dbg(dev, "Failed to find regulator container node '%s'\n",
  289. desc->regulators_node);
  290. return NULL;
  291. }
  292. for_each_available_child_of_node(search, child) {
  293. name = of_get_property(child, "regulator-compatible", NULL);
  294. if (!name)
  295. name = child->name;
  296. if (strcmp(desc->of_match, name))
  297. continue;
  298. init_data = of_get_regulator_init_data(dev, child, desc);
  299. if (!init_data) {
  300. dev_err(dev,
  301. "failed to parse DT for regulator %s\n",
  302. child->name);
  303. break;
  304. }
  305. if (desc->of_parse_cb) {
  306. if (desc->of_parse_cb(child, desc, config)) {
  307. dev_err(dev,
  308. "driver callback failed to parse DT for regulator %s\n",
  309. child->name);
  310. init_data = NULL;
  311. break;
  312. }
  313. }
  314. of_node_get(child);
  315. *node = child;
  316. break;
  317. }
  318. of_node_put(search);
  319. return init_data;
  320. }