of_regulator.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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, len;
  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. len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
  116. sizeof(u32));
  117. if (len > 0) {
  118. if (desc && desc->of_map_mode) {
  119. for (i = 0; i < len; i++) {
  120. ret = of_property_read_u32_index(np,
  121. "regulator-allowed-modes", i, &pval);
  122. if (ret) {
  123. pr_err("%s: couldn't read allowed modes index %d, ret=%d\n",
  124. np->name, i, ret);
  125. break;
  126. }
  127. mode = desc->of_map_mode(pval);
  128. if (mode == REGULATOR_MODE_INVALID)
  129. pr_err("%s: invalid regulator-allowed-modes element %u\n",
  130. np->name, pval);
  131. else
  132. constraints->valid_modes_mask |= mode;
  133. }
  134. if (constraints->valid_modes_mask)
  135. constraints->valid_ops_mask
  136. |= REGULATOR_CHANGE_MODE;
  137. } else {
  138. pr_warn("%s: mode mapping not defined\n", np->name);
  139. }
  140. }
  141. if (!of_property_read_u32(np, "regulator-system-load", &pval))
  142. constraints->system_load = pval;
  143. if (!of_property_read_u32(np, "regulator-coupled-max-spread",
  144. &pval))
  145. constraints->max_spread = pval;
  146. constraints->over_current_protection = of_property_read_bool(np,
  147. "regulator-over-current-protection");
  148. for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
  149. switch (i) {
  150. case PM_SUSPEND_MEM:
  151. suspend_state = &constraints->state_mem;
  152. break;
  153. case PM_SUSPEND_MAX:
  154. suspend_state = &constraints->state_disk;
  155. break;
  156. case PM_SUSPEND_ON:
  157. case PM_SUSPEND_TO_IDLE:
  158. case PM_SUSPEND_STANDBY:
  159. default:
  160. continue;
  161. }
  162. suspend_np = of_get_child_by_name(np, regulator_states[i]);
  163. if (!suspend_np || !suspend_state)
  164. continue;
  165. if (!of_property_read_u32(suspend_np, "regulator-mode",
  166. &pval)) {
  167. if (desc && desc->of_map_mode) {
  168. mode = desc->of_map_mode(pval);
  169. if (mode == REGULATOR_MODE_INVALID)
  170. pr_err("%s: invalid mode %u\n",
  171. np->name, pval);
  172. else
  173. suspend_state->mode = mode;
  174. } else {
  175. pr_warn("%s: mapping for mode %d not defined\n",
  176. np->name, pval);
  177. }
  178. }
  179. if (of_property_read_bool(suspend_np,
  180. "regulator-on-in-suspend"))
  181. suspend_state->enabled = ENABLE_IN_SUSPEND;
  182. else if (of_property_read_bool(suspend_np,
  183. "regulator-off-in-suspend"))
  184. suspend_state->enabled = DISABLE_IN_SUSPEND;
  185. if (!of_property_read_u32(suspend_np,
  186. "regulator-suspend-min-microvolt", &pval))
  187. suspend_state->min_uV = pval;
  188. if (!of_property_read_u32(suspend_np,
  189. "regulator-suspend-max-microvolt", &pval))
  190. suspend_state->max_uV = pval;
  191. if (!of_property_read_u32(suspend_np,
  192. "regulator-suspend-microvolt", &pval))
  193. suspend_state->uV = pval;
  194. else /* otherwise use min_uV as default suspend voltage */
  195. suspend_state->uV = suspend_state->min_uV;
  196. if (of_property_read_bool(suspend_np,
  197. "regulator-changeable-in-suspend"))
  198. suspend_state->changeable = true;
  199. if (i == PM_SUSPEND_MEM)
  200. constraints->initial_state = PM_SUSPEND_MEM;
  201. of_node_put(suspend_np);
  202. suspend_state = NULL;
  203. suspend_np = NULL;
  204. }
  205. }
  206. /**
  207. * of_get_regulator_init_data - extract regulator_init_data structure info
  208. * @dev: device requesting for regulator_init_data
  209. * @node: regulator device node
  210. * @desc: regulator description
  211. *
  212. * Populates regulator_init_data structure by extracting data from device
  213. * tree node, returns a pointer to the populated struture or NULL if memory
  214. * alloc fails.
  215. */
  216. struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
  217. struct device_node *node,
  218. const struct regulator_desc *desc)
  219. {
  220. struct regulator_init_data *init_data;
  221. if (!node)
  222. return NULL;
  223. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  224. if (!init_data)
  225. return NULL; /* Out of memory? */
  226. of_get_regulation_constraints(node, &init_data, desc);
  227. return init_data;
  228. }
  229. EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
  230. struct devm_of_regulator_matches {
  231. struct of_regulator_match *matches;
  232. unsigned int num_matches;
  233. };
  234. static void devm_of_regulator_put_matches(struct device *dev, void *res)
  235. {
  236. struct devm_of_regulator_matches *devm_matches = res;
  237. int i;
  238. for (i = 0; i < devm_matches->num_matches; i++)
  239. of_node_put(devm_matches->matches[i].of_node);
  240. }
  241. /**
  242. * of_regulator_match - extract multiple regulator init data from device tree.
  243. * @dev: device requesting the data
  244. * @node: parent device node of the regulators
  245. * @matches: match table for the regulators
  246. * @num_matches: number of entries in match table
  247. *
  248. * This function uses a match table specified by the regulator driver to
  249. * parse regulator init data from the device tree. @node is expected to
  250. * contain a set of child nodes, each providing the init data for one
  251. * regulator. The data parsed from a child node will be matched to a regulator
  252. * based on either the deprecated property regulator-compatible if present,
  253. * or otherwise the child node's name. Note that the match table is modified
  254. * in place and an additional of_node reference is taken for each matched
  255. * regulator.
  256. *
  257. * Returns the number of matches found or a negative error code on failure.
  258. */
  259. int of_regulator_match(struct device *dev, struct device_node *node,
  260. struct of_regulator_match *matches,
  261. unsigned int num_matches)
  262. {
  263. unsigned int count = 0;
  264. unsigned int i;
  265. const char *name;
  266. struct device_node *child;
  267. struct devm_of_regulator_matches *devm_matches;
  268. if (!dev || !node)
  269. return -EINVAL;
  270. devm_matches = devres_alloc(devm_of_regulator_put_matches,
  271. sizeof(struct devm_of_regulator_matches),
  272. GFP_KERNEL);
  273. if (!devm_matches)
  274. return -ENOMEM;
  275. devm_matches->matches = matches;
  276. devm_matches->num_matches = num_matches;
  277. devres_add(dev, devm_matches);
  278. for (i = 0; i < num_matches; i++) {
  279. struct of_regulator_match *match = &matches[i];
  280. match->init_data = NULL;
  281. match->of_node = NULL;
  282. }
  283. for_each_child_of_node(node, child) {
  284. name = of_get_property(child,
  285. "regulator-compatible", NULL);
  286. if (!name)
  287. name = child->name;
  288. for (i = 0; i < num_matches; i++) {
  289. struct of_regulator_match *match = &matches[i];
  290. if (match->of_node)
  291. continue;
  292. if (strcmp(match->name, name))
  293. continue;
  294. match->init_data =
  295. of_get_regulator_init_data(dev, child,
  296. match->desc);
  297. if (!match->init_data) {
  298. dev_err(dev,
  299. "failed to parse DT for regulator %s\n",
  300. child->name);
  301. of_node_put(child);
  302. return -EINVAL;
  303. }
  304. match->of_node = of_node_get(child);
  305. count++;
  306. break;
  307. }
  308. }
  309. return count;
  310. }
  311. EXPORT_SYMBOL_GPL(of_regulator_match);
  312. struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
  313. const struct regulator_desc *desc,
  314. struct regulator_config *config,
  315. struct device_node **node)
  316. {
  317. struct device_node *search, *child;
  318. struct regulator_init_data *init_data = NULL;
  319. const char *name;
  320. if (!dev->of_node || !desc->of_match)
  321. return NULL;
  322. if (desc->regulators_node)
  323. search = of_get_child_by_name(dev->of_node,
  324. desc->regulators_node);
  325. else
  326. search = of_node_get(dev->of_node);
  327. if (!search) {
  328. dev_dbg(dev, "Failed to find regulator container node '%s'\n",
  329. desc->regulators_node);
  330. return NULL;
  331. }
  332. for_each_available_child_of_node(search, child) {
  333. name = of_get_property(child, "regulator-compatible", NULL);
  334. if (!name)
  335. name = child->name;
  336. if (strcmp(desc->of_match, name))
  337. continue;
  338. init_data = of_get_regulator_init_data(dev, child, desc);
  339. if (!init_data) {
  340. dev_err(dev,
  341. "failed to parse DT for regulator %s\n",
  342. child->name);
  343. break;
  344. }
  345. if (desc->of_parse_cb) {
  346. if (desc->of_parse_cb(child, desc, config)) {
  347. dev_err(dev,
  348. "driver callback failed to parse DT for regulator %s\n",
  349. child->name);
  350. init_data = NULL;
  351. break;
  352. }
  353. }
  354. of_node_get(child);
  355. *node = child;
  356. break;
  357. }
  358. of_node_put(search);
  359. return init_data;
  360. }
  361. static int of_node_match(struct device *dev, const void *data)
  362. {
  363. return dev->of_node == data;
  364. }
  365. struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
  366. {
  367. struct device *dev;
  368. dev = class_find_device(&regulator_class, NULL, np, of_node_match);
  369. return dev ? dev_to_rdev(dev) : NULL;
  370. }
  371. /*
  372. * Returns number of regulators coupled with rdev.
  373. */
  374. int of_get_n_coupled(struct regulator_dev *rdev)
  375. {
  376. struct device_node *node = rdev->dev.of_node;
  377. int n_phandles;
  378. n_phandles = of_count_phandle_with_args(node,
  379. "regulator-coupled-with",
  380. NULL);
  381. return (n_phandles > 0) ? n_phandles : 0;
  382. }
  383. /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
  384. static bool of_coupling_find_node(struct device_node *src,
  385. struct device_node *to_find)
  386. {
  387. int n_phandles, i;
  388. bool found = false;
  389. n_phandles = of_count_phandle_with_args(src,
  390. "regulator-coupled-with",
  391. NULL);
  392. for (i = 0; i < n_phandles; i++) {
  393. struct device_node *tmp = of_parse_phandle(src,
  394. "regulator-coupled-with", i);
  395. if (!tmp)
  396. break;
  397. /* found */
  398. if (tmp == to_find)
  399. found = true;
  400. of_node_put(tmp);
  401. if (found)
  402. break;
  403. }
  404. return found;
  405. }
  406. /**
  407. * of_check_coupling_data - Parse rdev's coupling properties and check data
  408. * consistency
  409. * @rdev - pointer to regulator_dev whose data is checked
  410. *
  411. * Function checks if all the following conditions are met:
  412. * - rdev's max_spread is greater than 0
  413. * - all coupled regulators have the same max_spread
  414. * - all coupled regulators have the same number of regulator_dev phandles
  415. * - all regulators are linked to each other
  416. *
  417. * Returns true if all conditions are met.
  418. */
  419. bool of_check_coupling_data(struct regulator_dev *rdev)
  420. {
  421. int max_spread = rdev->constraints->max_spread;
  422. struct device_node *node = rdev->dev.of_node;
  423. int n_phandles = of_get_n_coupled(rdev);
  424. struct device_node *c_node;
  425. int i;
  426. bool ret = true;
  427. if (max_spread <= 0) {
  428. dev_err(&rdev->dev, "max_spread value invalid\n");
  429. return false;
  430. }
  431. /* iterate over rdev's phandles */
  432. for (i = 0; i < n_phandles; i++) {
  433. int c_max_spread, c_n_phandles;
  434. c_node = of_parse_phandle(node,
  435. "regulator-coupled-with", i);
  436. if (!c_node)
  437. ret = false;
  438. c_n_phandles = of_count_phandle_with_args(c_node,
  439. "regulator-coupled-with",
  440. NULL);
  441. if (c_n_phandles != n_phandles) {
  442. dev_err(&rdev->dev, "number of couped reg phandles mismatch\n");
  443. ret = false;
  444. goto clean;
  445. }
  446. if (of_property_read_u32(c_node, "regulator-coupled-max-spread",
  447. &c_max_spread)) {
  448. ret = false;
  449. goto clean;
  450. }
  451. if (c_max_spread != max_spread) {
  452. dev_err(&rdev->dev,
  453. "coupled regulators max_spread mismatch\n");
  454. ret = false;
  455. goto clean;
  456. }
  457. if (!of_coupling_find_node(c_node, node)) {
  458. dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
  459. ret = false;
  460. }
  461. clean:
  462. of_node_put(c_node);
  463. if (!ret)
  464. break;
  465. }
  466. return ret;
  467. }
  468. /**
  469. * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
  470. * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
  471. * "regulator-coupled-with" property
  472. * @index: Index in phandles array
  473. *
  474. * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
  475. * registered, returns NULL
  476. */
  477. struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
  478. int index)
  479. {
  480. struct device_node *node = rdev->dev.of_node;
  481. struct device_node *c_node;
  482. struct regulator_dev *c_rdev;
  483. c_node = of_parse_phandle(node, "regulator-coupled-with", index);
  484. if (!c_node)
  485. return NULL;
  486. c_rdev = of_find_regulator_by_node(c_node);
  487. of_node_put(c_node);
  488. return c_rdev;
  489. }