of_regulator.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OF helpers for regulator framework
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Rajendra Nayak <rnayak@ti.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/of.h>
  11. #include <linux/regulator/machine.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/regulator/of_regulator.h>
  14. #include "internal.h"
  15. static const char *const regulator_states[PM_SUSPEND_MAX + 1] = {
  16. [PM_SUSPEND_STANDBY] = "regulator-state-standby",
  17. [PM_SUSPEND_MEM] = "regulator-state-mem",
  18. [PM_SUSPEND_MAX] = "regulator-state-disk",
  19. };
  20. static int of_get_regulation_constraints(struct device *dev,
  21. struct device_node *np,
  22. struct regulator_init_data **init_data,
  23. const struct regulator_desc *desc)
  24. {
  25. struct regulation_constraints *constraints = &(*init_data)->constraints;
  26. struct regulator_state *suspend_state;
  27. struct device_node *suspend_np;
  28. unsigned int mode;
  29. int ret, i, len;
  30. int n_phandles;
  31. u32 pval;
  32. n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with",
  33. NULL);
  34. n_phandles = max(n_phandles, 0);
  35. constraints->name = of_get_property(np, "regulator-name", NULL);
  36. if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
  37. constraints->min_uV = pval;
  38. if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
  39. constraints->max_uV = pval;
  40. /* Voltage change possible? */
  41. if (constraints->min_uV != constraints->max_uV)
  42. constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  43. /* Do we have a voltage range, if so try to apply it? */
  44. if (constraints->min_uV && constraints->max_uV)
  45. constraints->apply_uV = true;
  46. if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
  47. constraints->uV_offset = pval;
  48. if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
  49. constraints->min_uA = pval;
  50. if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
  51. constraints->max_uA = pval;
  52. if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
  53. &pval))
  54. constraints->ilim_uA = pval;
  55. /* Current change possible? */
  56. if (constraints->min_uA != constraints->max_uA)
  57. constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
  58. constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
  59. constraints->always_on = of_property_read_bool(np, "regulator-always-on");
  60. if (!constraints->always_on) /* status change should be possible. */
  61. constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
  62. constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
  63. if (of_property_read_bool(np, "regulator-allow-bypass"))
  64. constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
  65. if (of_property_read_bool(np, "regulator-allow-set-load"))
  66. constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
  67. ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
  68. if (!ret) {
  69. if (pval)
  70. constraints->ramp_delay = pval;
  71. else
  72. constraints->ramp_disable = true;
  73. }
  74. ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
  75. if (!ret)
  76. constraints->settling_time = pval;
  77. ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
  78. if (!ret)
  79. constraints->settling_time_up = pval;
  80. if (constraints->settling_time_up && constraints->settling_time) {
  81. pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
  82. np);
  83. constraints->settling_time_up = 0;
  84. }
  85. ret = of_property_read_u32(np, "regulator-settling-time-down-us",
  86. &pval);
  87. if (!ret)
  88. constraints->settling_time_down = pval;
  89. if (constraints->settling_time_down && constraints->settling_time) {
  90. pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
  91. np);
  92. constraints->settling_time_down = 0;
  93. }
  94. ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
  95. if (!ret)
  96. constraints->enable_time = pval;
  97. constraints->soft_start = of_property_read_bool(np,
  98. "regulator-soft-start");
  99. ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
  100. if (!ret) {
  101. constraints->active_discharge =
  102. (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
  103. REGULATOR_ACTIVE_DISCHARGE_DISABLE;
  104. }
  105. if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
  106. if (desc && desc->of_map_mode) {
  107. mode = desc->of_map_mode(pval);
  108. if (mode == REGULATOR_MODE_INVALID)
  109. pr_err("%pOFn: invalid mode %u\n", np, pval);
  110. else
  111. constraints->initial_mode = mode;
  112. } else {
  113. pr_warn("%pOFn: mapping for mode %d not defined\n",
  114. np, pval);
  115. }
  116. }
  117. len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
  118. sizeof(u32));
  119. if (len > 0) {
  120. if (desc && desc->of_map_mode) {
  121. for (i = 0; i < len; i++) {
  122. ret = of_property_read_u32_index(np,
  123. "regulator-allowed-modes", i, &pval);
  124. if (ret) {
  125. pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
  126. np, i, ret);
  127. break;
  128. }
  129. mode = desc->of_map_mode(pval);
  130. if (mode == REGULATOR_MODE_INVALID)
  131. pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
  132. np, pval);
  133. else
  134. constraints->valid_modes_mask |= mode;
  135. }
  136. if (constraints->valid_modes_mask)
  137. constraints->valid_ops_mask
  138. |= REGULATOR_CHANGE_MODE;
  139. } else {
  140. pr_warn("%pOFn: mode mapping not defined\n", np);
  141. }
  142. }
  143. if (!of_property_read_u32(np, "regulator-system-load", &pval))
  144. constraints->system_load = pval;
  145. if (n_phandles) {
  146. constraints->max_spread = devm_kzalloc(dev,
  147. sizeof(*constraints->max_spread) * n_phandles,
  148. GFP_KERNEL);
  149. if (!constraints->max_spread)
  150. return -ENOMEM;
  151. of_property_read_u32_array(np, "regulator-coupled-max-spread",
  152. constraints->max_spread, n_phandles);
  153. }
  154. if (!of_property_read_u32(np, "regulator-max-step-microvolt",
  155. &pval))
  156. constraints->max_uV_step = pval;
  157. constraints->over_current_protection = of_property_read_bool(np,
  158. "regulator-over-current-protection");
  159. for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
  160. switch (i) {
  161. case PM_SUSPEND_MEM:
  162. suspend_state = &constraints->state_mem;
  163. break;
  164. case PM_SUSPEND_MAX:
  165. suspend_state = &constraints->state_disk;
  166. break;
  167. case PM_SUSPEND_STANDBY:
  168. suspend_state = &constraints->state_standby;
  169. break;
  170. case PM_SUSPEND_ON:
  171. case PM_SUSPEND_TO_IDLE:
  172. default:
  173. continue;
  174. }
  175. suspend_np = of_get_child_by_name(np, regulator_states[i]);
  176. if (!suspend_np || !suspend_state)
  177. continue;
  178. if (!of_property_read_u32(suspend_np, "regulator-mode",
  179. &pval)) {
  180. if (desc && desc->of_map_mode) {
  181. mode = desc->of_map_mode(pval);
  182. if (mode == REGULATOR_MODE_INVALID)
  183. pr_err("%pOFn: invalid mode %u\n",
  184. np, pval);
  185. else
  186. suspend_state->mode = mode;
  187. } else {
  188. pr_warn("%pOFn: mapping for mode %d not defined\n",
  189. np, pval);
  190. }
  191. }
  192. if (of_property_read_bool(suspend_np,
  193. "regulator-on-in-suspend"))
  194. suspend_state->enabled = ENABLE_IN_SUSPEND;
  195. else if (of_property_read_bool(suspend_np,
  196. "regulator-off-in-suspend"))
  197. suspend_state->enabled = DISABLE_IN_SUSPEND;
  198. if (!of_property_read_u32(suspend_np,
  199. "regulator-suspend-min-microvolt", &pval))
  200. suspend_state->min_uV = pval;
  201. if (!of_property_read_u32(suspend_np,
  202. "regulator-suspend-max-microvolt", &pval))
  203. suspend_state->max_uV = pval;
  204. if (!of_property_read_u32(suspend_np,
  205. "regulator-suspend-microvolt", &pval))
  206. suspend_state->uV = pval;
  207. else /* otherwise use min_uV as default suspend voltage */
  208. suspend_state->uV = suspend_state->min_uV;
  209. if (of_property_read_bool(suspend_np,
  210. "regulator-changeable-in-suspend"))
  211. suspend_state->changeable = true;
  212. if (i == PM_SUSPEND_MEM)
  213. constraints->initial_state = PM_SUSPEND_MEM;
  214. of_node_put(suspend_np);
  215. suspend_state = NULL;
  216. suspend_np = NULL;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * of_get_regulator_init_data - extract regulator_init_data structure info
  222. * @dev: device requesting for regulator_init_data
  223. * @node: regulator device node
  224. * @desc: regulator description
  225. *
  226. * Populates regulator_init_data structure by extracting data from device
  227. * tree node, returns a pointer to the populated structure or NULL if memory
  228. * alloc fails.
  229. */
  230. struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
  231. struct device_node *node,
  232. const struct regulator_desc *desc)
  233. {
  234. struct regulator_init_data *init_data;
  235. if (!node)
  236. return NULL;
  237. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  238. if (!init_data)
  239. return NULL; /* Out of memory? */
  240. if (of_get_regulation_constraints(dev, node, &init_data, desc))
  241. return NULL;
  242. return init_data;
  243. }
  244. EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
  245. struct devm_of_regulator_matches {
  246. struct of_regulator_match *matches;
  247. unsigned int num_matches;
  248. };
  249. static void devm_of_regulator_put_matches(struct device *dev, void *res)
  250. {
  251. struct devm_of_regulator_matches *devm_matches = res;
  252. int i;
  253. for (i = 0; i < devm_matches->num_matches; i++)
  254. of_node_put(devm_matches->matches[i].of_node);
  255. }
  256. /**
  257. * of_regulator_match - extract multiple regulator init data from device tree.
  258. * @dev: device requesting the data
  259. * @node: parent device node of the regulators
  260. * @matches: match table for the regulators
  261. * @num_matches: number of entries in match table
  262. *
  263. * This function uses a match table specified by the regulator driver to
  264. * parse regulator init data from the device tree. @node is expected to
  265. * contain a set of child nodes, each providing the init data for one
  266. * regulator. The data parsed from a child node will be matched to a regulator
  267. * based on either the deprecated property regulator-compatible if present,
  268. * or otherwise the child node's name. Note that the match table is modified
  269. * in place and an additional of_node reference is taken for each matched
  270. * regulator.
  271. *
  272. * Returns the number of matches found or a negative error code on failure.
  273. */
  274. int of_regulator_match(struct device *dev, struct device_node *node,
  275. struct of_regulator_match *matches,
  276. unsigned int num_matches)
  277. {
  278. unsigned int count = 0;
  279. unsigned int i;
  280. const char *name;
  281. struct device_node *child;
  282. struct devm_of_regulator_matches *devm_matches;
  283. if (!dev || !node)
  284. return -EINVAL;
  285. devm_matches = devres_alloc(devm_of_regulator_put_matches,
  286. sizeof(struct devm_of_regulator_matches),
  287. GFP_KERNEL);
  288. if (!devm_matches)
  289. return -ENOMEM;
  290. devm_matches->matches = matches;
  291. devm_matches->num_matches = num_matches;
  292. devres_add(dev, devm_matches);
  293. for (i = 0; i < num_matches; i++) {
  294. struct of_regulator_match *match = &matches[i];
  295. match->init_data = NULL;
  296. match->of_node = NULL;
  297. }
  298. for_each_child_of_node(node, child) {
  299. name = of_get_property(child,
  300. "regulator-compatible", NULL);
  301. if (!name)
  302. name = child->name;
  303. for (i = 0; i < num_matches; i++) {
  304. struct of_regulator_match *match = &matches[i];
  305. if (match->of_node)
  306. continue;
  307. if (strcmp(match->name, name))
  308. continue;
  309. match->init_data =
  310. of_get_regulator_init_data(dev, child,
  311. match->desc);
  312. if (!match->init_data) {
  313. dev_err(dev,
  314. "failed to parse DT for regulator %pOFn\n",
  315. child);
  316. of_node_put(child);
  317. return -EINVAL;
  318. }
  319. match->of_node = of_node_get(child);
  320. count++;
  321. break;
  322. }
  323. }
  324. return count;
  325. }
  326. EXPORT_SYMBOL_GPL(of_regulator_match);
  327. static struct
  328. device_node *regulator_of_get_init_node(struct device *dev,
  329. const struct regulator_desc *desc)
  330. {
  331. struct device_node *search, *child;
  332. const char *name;
  333. if (!dev->of_node || !desc->of_match)
  334. return NULL;
  335. if (desc->regulators_node) {
  336. search = of_get_child_by_name(dev->of_node,
  337. desc->regulators_node);
  338. } else {
  339. search = of_node_get(dev->of_node);
  340. if (!strcmp(desc->of_match, search->name))
  341. return search;
  342. }
  343. if (!search) {
  344. dev_dbg(dev, "Failed to find regulator container node '%s'\n",
  345. desc->regulators_node);
  346. return NULL;
  347. }
  348. for_each_available_child_of_node(search, child) {
  349. name = of_get_property(child, "regulator-compatible", NULL);
  350. if (!name) {
  351. if (!desc->of_match_full_name)
  352. name = child->name;
  353. else
  354. name = child->full_name;
  355. }
  356. if (!strcmp(desc->of_match, name)) {
  357. of_node_put(search);
  358. return of_node_get(child);
  359. }
  360. }
  361. of_node_put(search);
  362. return NULL;
  363. }
  364. struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
  365. const struct regulator_desc *desc,
  366. struct regulator_config *config,
  367. struct device_node **node)
  368. {
  369. struct device_node *child;
  370. struct regulator_init_data *init_data = NULL;
  371. child = regulator_of_get_init_node(dev, desc);
  372. if (!child)
  373. return NULL;
  374. init_data = of_get_regulator_init_data(dev, child, desc);
  375. if (!init_data) {
  376. dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
  377. goto error;
  378. }
  379. if (desc->of_parse_cb) {
  380. int ret;
  381. ret = desc->of_parse_cb(child, desc, config);
  382. if (ret) {
  383. if (ret == -EPROBE_DEFER) {
  384. of_node_put(child);
  385. return ERR_PTR(-EPROBE_DEFER);
  386. }
  387. dev_err(dev,
  388. "driver callback failed to parse DT for regulator %pOFn\n",
  389. child);
  390. goto error;
  391. }
  392. }
  393. *node = child;
  394. return init_data;
  395. error:
  396. of_node_put(child);
  397. return NULL;
  398. }
  399. struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
  400. {
  401. struct device *dev;
  402. dev = class_find_device_by_of_node(&regulator_class, np);
  403. return dev ? dev_to_rdev(dev) : NULL;
  404. }
  405. /*
  406. * Returns number of regulators coupled with rdev.
  407. */
  408. int of_get_n_coupled(struct regulator_dev *rdev)
  409. {
  410. struct device_node *node = rdev->dev.of_node;
  411. int n_phandles;
  412. n_phandles = of_count_phandle_with_args(node,
  413. "regulator-coupled-with",
  414. NULL);
  415. return (n_phandles > 0) ? n_phandles : 0;
  416. }
  417. /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
  418. static bool of_coupling_find_node(struct device_node *src,
  419. struct device_node *to_find,
  420. int *index)
  421. {
  422. int n_phandles, i;
  423. bool found = false;
  424. n_phandles = of_count_phandle_with_args(src,
  425. "regulator-coupled-with",
  426. NULL);
  427. for (i = 0; i < n_phandles; i++) {
  428. struct device_node *tmp = of_parse_phandle(src,
  429. "regulator-coupled-with", i);
  430. if (!tmp)
  431. break;
  432. /* found */
  433. if (tmp == to_find)
  434. found = true;
  435. of_node_put(tmp);
  436. if (found) {
  437. *index = i;
  438. break;
  439. }
  440. }
  441. return found;
  442. }
  443. /**
  444. * of_check_coupling_data - Parse rdev's coupling properties and check data
  445. * consistency
  446. * @rdev: pointer to regulator_dev whose data is checked
  447. *
  448. * Function checks if all the following conditions are met:
  449. * - rdev's max_spread is greater than 0
  450. * - all coupled regulators have the same max_spread
  451. * - all coupled regulators have the same number of regulator_dev phandles
  452. * - all regulators are linked to each other
  453. *
  454. * Returns true if all conditions are met.
  455. */
  456. bool of_check_coupling_data(struct regulator_dev *rdev)
  457. {
  458. struct device_node *node = rdev->dev.of_node;
  459. int n_phandles = of_get_n_coupled(rdev);
  460. struct device_node *c_node;
  461. int index;
  462. int i;
  463. bool ret = true;
  464. /* iterate over rdev's phandles */
  465. for (i = 0; i < n_phandles; i++) {
  466. int max_spread = rdev->constraints->max_spread[i];
  467. int c_max_spread, c_n_phandles;
  468. if (max_spread <= 0) {
  469. dev_err(&rdev->dev, "max_spread value invalid\n");
  470. return false;
  471. }
  472. c_node = of_parse_phandle(node,
  473. "regulator-coupled-with", i);
  474. if (!c_node)
  475. ret = false;
  476. c_n_phandles = of_count_phandle_with_args(c_node,
  477. "regulator-coupled-with",
  478. NULL);
  479. if (c_n_phandles != n_phandles) {
  480. dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
  481. ret = false;
  482. goto clean;
  483. }
  484. if (!of_coupling_find_node(c_node, node, &index)) {
  485. dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
  486. ret = false;
  487. goto clean;
  488. }
  489. if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
  490. index, &c_max_spread)) {
  491. ret = false;
  492. goto clean;
  493. }
  494. if (c_max_spread != max_spread) {
  495. dev_err(&rdev->dev,
  496. "coupled regulators max_spread mismatch\n");
  497. ret = false;
  498. goto clean;
  499. }
  500. clean:
  501. of_node_put(c_node);
  502. if (!ret)
  503. break;
  504. }
  505. return ret;
  506. }
  507. /**
  508. * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
  509. * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
  510. * "regulator-coupled-with" property
  511. * @index: Index in phandles array
  512. *
  513. * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
  514. * registered, returns NULL
  515. */
  516. struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
  517. int index)
  518. {
  519. struct device_node *node = rdev->dev.of_node;
  520. struct device_node *c_node;
  521. struct regulator_dev *c_rdev;
  522. c_node = of_parse_phandle(node, "regulator-coupled-with", index);
  523. if (!c_node)
  524. return NULL;
  525. c_rdev = of_find_regulator_by_node(c_node);
  526. of_node_put(c_node);
  527. return c_rdev;
  528. }