of_regulator.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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 void fill_limit(int *limit, int val)
  21. {
  22. if (val)
  23. if (val == 1)
  24. *limit = REGULATOR_NOTIF_LIMIT_ENABLE;
  25. else
  26. *limit = val;
  27. else
  28. *limit = REGULATOR_NOTIF_LIMIT_DISABLE;
  29. }
  30. static void of_get_regulator_prot_limits(struct device_node *np,
  31. struct regulation_constraints *constraints)
  32. {
  33. u32 pval;
  34. int i;
  35. static const char *const props[] = {
  36. "regulator-oc-%s-microamp",
  37. "regulator-ov-%s-microvolt",
  38. "regulator-temp-%s-kelvin",
  39. "regulator-uv-%s-microvolt",
  40. };
  41. struct notification_limit *limits[] = {
  42. &constraints->over_curr_limits,
  43. &constraints->over_voltage_limits,
  44. &constraints->temp_limits,
  45. &constraints->under_voltage_limits,
  46. };
  47. bool set[4] = {0};
  48. /* Protection limits: */
  49. for (i = 0; i < ARRAY_SIZE(props); i++) {
  50. char prop[255];
  51. bool found;
  52. int j;
  53. static const char *const lvl[] = {
  54. "protection", "error", "warn"
  55. };
  56. int *l[] = {
  57. &limits[i]->prot, &limits[i]->err, &limits[i]->warn,
  58. };
  59. for (j = 0; j < ARRAY_SIZE(lvl); j++) {
  60. snprintf(prop, 255, props[i], lvl[j]);
  61. found = !of_property_read_u32(np, prop, &pval);
  62. if (found)
  63. fill_limit(l[j], pval);
  64. set[i] |= found;
  65. }
  66. }
  67. constraints->over_current_detection = set[0];
  68. constraints->over_voltage_detection = set[1];
  69. constraints->over_temp_detection = set[2];
  70. constraints->under_voltage_detection = set[3];
  71. }
  72. static int of_get_regulation_constraints(struct device *dev,
  73. struct device_node *np,
  74. struct regulator_init_data **init_data,
  75. const struct regulator_desc *desc)
  76. {
  77. struct regulation_constraints *constraints = &(*init_data)->constraints;
  78. struct regulator_state *suspend_state;
  79. struct device_node *suspend_np;
  80. unsigned int mode;
  81. int ret, i, len;
  82. int n_phandles;
  83. u32 pval;
  84. n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with",
  85. NULL);
  86. n_phandles = max(n_phandles, 0);
  87. constraints->name = of_get_property(np, "regulator-name", NULL);
  88. if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
  89. constraints->min_uV = pval;
  90. if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
  91. constraints->max_uV = pval;
  92. /* Voltage change possible? */
  93. if (constraints->min_uV != constraints->max_uV)
  94. constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
  95. /* Do we have a voltage range, if so try to apply it? */
  96. if (constraints->min_uV && constraints->max_uV)
  97. constraints->apply_uV = true;
  98. if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
  99. constraints->uV_offset = pval;
  100. if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
  101. constraints->min_uA = pval;
  102. if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
  103. constraints->max_uA = pval;
  104. if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
  105. &pval))
  106. constraints->ilim_uA = pval;
  107. /* Current change possible? */
  108. if (constraints->min_uA != constraints->max_uA)
  109. constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
  110. constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
  111. constraints->always_on = of_property_read_bool(np, "regulator-always-on");
  112. if (!constraints->always_on) /* status change should be possible. */
  113. constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
  114. constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
  115. constraints->system_critical = of_property_read_bool(np,
  116. "system-critical-regulator");
  117. if (of_property_read_bool(np, "regulator-allow-bypass"))
  118. constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
  119. if (of_property_read_bool(np, "regulator-allow-set-load"))
  120. constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
  121. ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
  122. if (!ret) {
  123. if (pval)
  124. constraints->ramp_delay = pval;
  125. else
  126. constraints->ramp_disable = true;
  127. }
  128. ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
  129. if (!ret)
  130. constraints->settling_time = pval;
  131. ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
  132. if (!ret)
  133. constraints->settling_time_up = pval;
  134. if (constraints->settling_time_up && constraints->settling_time) {
  135. pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
  136. np);
  137. constraints->settling_time_up = 0;
  138. }
  139. ret = of_property_read_u32(np, "regulator-settling-time-down-us",
  140. &pval);
  141. if (!ret)
  142. constraints->settling_time_down = pval;
  143. if (constraints->settling_time_down && constraints->settling_time) {
  144. pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
  145. np);
  146. constraints->settling_time_down = 0;
  147. }
  148. ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
  149. if (!ret)
  150. constraints->enable_time = pval;
  151. ret = of_property_read_u32(np, "regulator-uv-survival-time-ms", &pval);
  152. if (!ret)
  153. constraints->uv_less_critical_window_ms = pval;
  154. else
  155. constraints->uv_less_critical_window_ms =
  156. REGULATOR_DEF_UV_LESS_CRITICAL_WINDOW_MS;
  157. constraints->soft_start = of_property_read_bool(np,
  158. "regulator-soft-start");
  159. ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
  160. if (!ret) {
  161. constraints->active_discharge =
  162. (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
  163. REGULATOR_ACTIVE_DISCHARGE_DISABLE;
  164. }
  165. if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
  166. if (desc && desc->of_map_mode) {
  167. mode = desc->of_map_mode(pval);
  168. if (mode == REGULATOR_MODE_INVALID)
  169. pr_err("%pOFn: invalid mode %u\n", np, pval);
  170. else
  171. constraints->initial_mode = mode;
  172. } else {
  173. pr_warn("%pOFn: mapping for mode %d not defined\n",
  174. np, pval);
  175. }
  176. }
  177. len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
  178. sizeof(u32));
  179. if (len > 0) {
  180. if (desc && desc->of_map_mode) {
  181. for (i = 0; i < len; i++) {
  182. ret = of_property_read_u32_index(np,
  183. "regulator-allowed-modes", i, &pval);
  184. if (ret) {
  185. pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
  186. np, i, ret);
  187. break;
  188. }
  189. mode = desc->of_map_mode(pval);
  190. if (mode == REGULATOR_MODE_INVALID)
  191. pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
  192. np, pval);
  193. else
  194. constraints->valid_modes_mask |= mode;
  195. }
  196. if (constraints->valid_modes_mask)
  197. constraints->valid_ops_mask
  198. |= REGULATOR_CHANGE_MODE;
  199. } else {
  200. pr_warn("%pOFn: mode mapping not defined\n", np);
  201. }
  202. }
  203. if (!of_property_read_u32(np, "regulator-system-load", &pval))
  204. constraints->system_load = pval;
  205. if (n_phandles) {
  206. constraints->max_spread = devm_kzalloc(dev,
  207. sizeof(*constraints->max_spread) * n_phandles,
  208. GFP_KERNEL);
  209. if (!constraints->max_spread)
  210. return -ENOMEM;
  211. of_property_read_u32_array(np, "regulator-coupled-max-spread",
  212. constraints->max_spread, n_phandles);
  213. }
  214. if (!of_property_read_u32(np, "regulator-max-step-microvolt",
  215. &pval))
  216. constraints->max_uV_step = pval;
  217. constraints->over_current_protection = of_property_read_bool(np,
  218. "regulator-over-current-protection");
  219. of_get_regulator_prot_limits(np, constraints);
  220. for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
  221. switch (i) {
  222. case PM_SUSPEND_MEM:
  223. suspend_state = &constraints->state_mem;
  224. break;
  225. case PM_SUSPEND_MAX:
  226. suspend_state = &constraints->state_disk;
  227. break;
  228. case PM_SUSPEND_STANDBY:
  229. suspend_state = &constraints->state_standby;
  230. break;
  231. case PM_SUSPEND_ON:
  232. case PM_SUSPEND_TO_IDLE:
  233. default:
  234. continue;
  235. }
  236. suspend_np = of_get_child_by_name(np, regulator_states[i]);
  237. if (!suspend_np)
  238. continue;
  239. if (!suspend_state) {
  240. of_node_put(suspend_np);
  241. continue;
  242. }
  243. if (!of_property_read_u32(suspend_np, "regulator-mode",
  244. &pval)) {
  245. if (desc && desc->of_map_mode) {
  246. mode = desc->of_map_mode(pval);
  247. if (mode == REGULATOR_MODE_INVALID)
  248. pr_err("%pOFn: invalid mode %u\n",
  249. np, pval);
  250. else
  251. suspend_state->mode = mode;
  252. } else {
  253. pr_warn("%pOFn: mapping for mode %d not defined\n",
  254. np, pval);
  255. }
  256. }
  257. if (of_property_read_bool(suspend_np,
  258. "regulator-on-in-suspend"))
  259. suspend_state->enabled = ENABLE_IN_SUSPEND;
  260. else if (of_property_read_bool(suspend_np,
  261. "regulator-off-in-suspend"))
  262. suspend_state->enabled = DISABLE_IN_SUSPEND;
  263. if (!of_property_read_u32(suspend_np,
  264. "regulator-suspend-min-microvolt", &pval))
  265. suspend_state->min_uV = pval;
  266. if (!of_property_read_u32(suspend_np,
  267. "regulator-suspend-max-microvolt", &pval))
  268. suspend_state->max_uV = pval;
  269. if (!of_property_read_u32(suspend_np,
  270. "regulator-suspend-microvolt", &pval))
  271. suspend_state->uV = pval;
  272. else /* otherwise use min_uV as default suspend voltage */
  273. suspend_state->uV = suspend_state->min_uV;
  274. if (of_property_read_bool(suspend_np,
  275. "regulator-changeable-in-suspend"))
  276. suspend_state->changeable = true;
  277. if (i == PM_SUSPEND_MEM)
  278. constraints->initial_state = PM_SUSPEND_MEM;
  279. of_node_put(suspend_np);
  280. suspend_state = NULL;
  281. suspend_np = NULL;
  282. }
  283. return 0;
  284. }
  285. /**
  286. * of_get_regulator_init_data - extract regulator_init_data structure info
  287. * @dev: device requesting for regulator_init_data
  288. * @node: regulator device node
  289. * @desc: regulator description
  290. *
  291. * Populates regulator_init_data structure by extracting data from device
  292. * tree node.
  293. *
  294. * Return: Pointer to a populated &struct regulator_init_data or NULL if
  295. * memory allocation fails.
  296. */
  297. struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
  298. struct device_node *node,
  299. const struct regulator_desc *desc)
  300. {
  301. struct regulator_init_data *init_data;
  302. if (!node)
  303. return NULL;
  304. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  305. if (!init_data)
  306. return NULL; /* Out of memory? */
  307. if (of_get_regulation_constraints(dev, node, &init_data, desc))
  308. return NULL;
  309. return init_data;
  310. }
  311. EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
  312. struct devm_of_regulator_matches {
  313. struct of_regulator_match *matches;
  314. unsigned int num_matches;
  315. };
  316. static void devm_of_regulator_put_matches(struct device *dev, void *res)
  317. {
  318. struct devm_of_regulator_matches *devm_matches = res;
  319. int i;
  320. for (i = 0; i < devm_matches->num_matches; i++)
  321. of_node_put(devm_matches->matches[i].of_node);
  322. }
  323. /**
  324. * of_regulator_match - extract multiple regulator init data from device tree.
  325. * @dev: device requesting the data
  326. * @node: parent device node of the regulators
  327. * @matches: match table for the regulators
  328. * @num_matches: number of entries in match table
  329. *
  330. * This function uses a match table specified by the regulator driver to
  331. * parse regulator init data from the device tree. @node is expected to
  332. * contain a set of child nodes, each providing the init data for one
  333. * regulator. The data parsed from a child node will be matched to a regulator
  334. * based on either the deprecated property regulator-compatible if present,
  335. * or otherwise the child node's name. Note that the match table is modified
  336. * in place and an additional of_node reference is taken for each matched
  337. * regulator.
  338. *
  339. * Return: The number of matches found or a negative error number on failure.
  340. */
  341. int of_regulator_match(struct device *dev, struct device_node *node,
  342. struct of_regulator_match *matches,
  343. unsigned int num_matches)
  344. {
  345. unsigned int count = 0;
  346. unsigned int i;
  347. const char *name;
  348. struct device_node *child;
  349. struct devm_of_regulator_matches *devm_matches;
  350. if (!dev || !node)
  351. return -EINVAL;
  352. devm_matches = devres_alloc(devm_of_regulator_put_matches,
  353. sizeof(struct devm_of_regulator_matches),
  354. GFP_KERNEL);
  355. if (!devm_matches)
  356. return -ENOMEM;
  357. devm_matches->matches = matches;
  358. devm_matches->num_matches = num_matches;
  359. devres_add(dev, devm_matches);
  360. for (i = 0; i < num_matches; i++) {
  361. struct of_regulator_match *match = &matches[i];
  362. match->init_data = NULL;
  363. match->of_node = NULL;
  364. }
  365. for_each_child_of_node(node, child) {
  366. name = of_get_property(child,
  367. "regulator-compatible", NULL);
  368. if (!name)
  369. name = child->name;
  370. for (i = 0; i < num_matches; i++) {
  371. struct of_regulator_match *match = &matches[i];
  372. if (match->of_node)
  373. continue;
  374. if (strcmp(match->name, name))
  375. continue;
  376. match->init_data =
  377. of_get_regulator_init_data(dev, child,
  378. match->desc);
  379. if (!match->init_data) {
  380. dev_err(dev,
  381. "failed to parse DT for regulator %pOFn\n",
  382. child);
  383. of_node_put(child);
  384. goto err_put;
  385. }
  386. match->of_node = of_node_get(child);
  387. count++;
  388. break;
  389. }
  390. }
  391. return count;
  392. err_put:
  393. for (i = 0; i < num_matches; i++) {
  394. struct of_regulator_match *match = &matches[i];
  395. match->init_data = NULL;
  396. if (match->of_node) {
  397. of_node_put(match->of_node);
  398. match->of_node = NULL;
  399. }
  400. }
  401. return -EINVAL;
  402. }
  403. EXPORT_SYMBOL_GPL(of_regulator_match);
  404. static struct
  405. device_node *regulator_of_get_init_node(struct device *dev,
  406. const struct regulator_desc *desc)
  407. {
  408. struct device_node *search, *child;
  409. const char *name;
  410. if (!dev->of_node || !desc->of_match)
  411. return NULL;
  412. if (desc->regulators_node) {
  413. search = of_get_child_by_name(dev->of_node,
  414. desc->regulators_node);
  415. } else {
  416. search = of_node_get(dev->of_node);
  417. if (!strcmp(desc->of_match, search->name))
  418. return search;
  419. }
  420. if (!search) {
  421. dev_dbg(dev, "Failed to find regulator container node '%s'\n",
  422. desc->regulators_node);
  423. return NULL;
  424. }
  425. for_each_available_child_of_node(search, child) {
  426. name = of_get_property(child, "regulator-compatible", NULL);
  427. if (!name) {
  428. if (!desc->of_match_full_name)
  429. name = child->name;
  430. else
  431. name = child->full_name;
  432. }
  433. if (!strcmp(desc->of_match, name)) {
  434. of_node_put(search);
  435. /*
  436. * 'of_node_get(child)' is already performed by the
  437. * for_each loop.
  438. */
  439. return child;
  440. }
  441. }
  442. of_node_put(search);
  443. return NULL;
  444. }
  445. struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
  446. const struct regulator_desc *desc,
  447. struct regulator_config *config,
  448. struct device_node **node)
  449. {
  450. struct device_node *child;
  451. struct regulator_init_data *init_data = NULL;
  452. child = regulator_of_get_init_node(config->dev, desc);
  453. if (!child)
  454. return NULL;
  455. init_data = of_get_regulator_init_data(dev, child, desc);
  456. if (!init_data) {
  457. dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
  458. goto error;
  459. }
  460. if (desc->of_parse_cb) {
  461. int ret;
  462. ret = desc->of_parse_cb(child, desc, config);
  463. if (ret) {
  464. if (ret == -EPROBE_DEFER) {
  465. of_node_put(child);
  466. return ERR_PTR(-EPROBE_DEFER);
  467. }
  468. dev_err(dev,
  469. "driver callback failed to parse DT for regulator %pOFn\n",
  470. child);
  471. goto error;
  472. }
  473. }
  474. *node = child;
  475. return init_data;
  476. error:
  477. of_node_put(child);
  478. return NULL;
  479. }
  480. /**
  481. * of_get_child_regulator - get a child regulator device node
  482. * based on supply name
  483. * @parent: Parent device node
  484. * @prop_name: Combination regulator supply name and "-supply"
  485. *
  486. * Traverse all child nodes.
  487. * Extract the child regulator device node corresponding to the supply name.
  488. *
  489. * Return: Pointer to the &struct device_node corresponding to the regulator
  490. * if found, or %NULL if not found.
  491. */
  492. static struct device_node *of_get_child_regulator(struct device_node *parent,
  493. const char *prop_name)
  494. {
  495. struct device_node *regnode = NULL;
  496. struct device_node *child = NULL;
  497. for_each_child_of_node(parent, child) {
  498. regnode = of_parse_phandle(child, prop_name, 0);
  499. if (regnode)
  500. goto err_node_put;
  501. regnode = of_get_child_regulator(child, prop_name);
  502. if (regnode)
  503. goto err_node_put;
  504. }
  505. return NULL;
  506. err_node_put:
  507. of_node_put(child);
  508. return regnode;
  509. }
  510. /**
  511. * of_get_regulator - get a regulator device node based on supply name
  512. * @dev: Device pointer for the consumer (of regulator) device
  513. * @supply: regulator supply name
  514. *
  515. * Extract the regulator device node corresponding to the supply name.
  516. *
  517. * Return: Pointer to the &struct device_node corresponding to the regulator
  518. * if found, or %NULL if not found.
  519. */
  520. static struct device_node *of_get_regulator(struct device *dev, const char *supply)
  521. {
  522. struct device_node *regnode = NULL;
  523. char prop_name[64]; /* 64 is max size of property name */
  524. dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
  525. snprintf(prop_name, 64, "%s-supply", supply);
  526. regnode = of_parse_phandle(dev->of_node, prop_name, 0);
  527. if (regnode)
  528. return regnode;
  529. regnode = of_get_child_regulator(dev->of_node, prop_name);
  530. if (regnode)
  531. return regnode;
  532. dev_dbg(dev, "Looking up %s property in node %pOF failed\n", prop_name, dev->of_node);
  533. return NULL;
  534. }
  535. static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
  536. {
  537. struct device *dev;
  538. dev = class_find_device_by_of_node(&regulator_class, np);
  539. return dev ? dev_to_rdev(dev) : NULL;
  540. }
  541. /**
  542. * of_regulator_dev_lookup - lookup a regulator device with device tree only
  543. * @dev: Device pointer for regulator supply lookup.
  544. * @supply: Supply name or regulator ID.
  545. *
  546. * Return: Pointer to the &struct regulator_dev on success, or ERR_PTR()
  547. * encoded value on error.
  548. *
  549. * If successful, returns a pointer to the &struct regulator_dev that
  550. * corresponds to the name @supply and with the embedded &struct device
  551. * refcount incremented by one. The refcount must be dropped by calling
  552. * put_device().
  553. *
  554. * On failure one of the following ERR_PTR() encoded values is returned:
  555. * * -%ENODEV if lookup fails permanently.
  556. * * -%EPROBE_DEFER if lookup could succeed in the future.
  557. */
  558. struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
  559. const char *supply)
  560. {
  561. struct regulator_dev *r;
  562. struct device_node *node;
  563. node = of_get_regulator(dev, supply);
  564. if (node) {
  565. r = of_find_regulator_by_node(node);
  566. of_node_put(node);
  567. if (r)
  568. return r;
  569. /*
  570. * We have a node, but there is no device.
  571. * assume it has not registered yet.
  572. */
  573. return ERR_PTR(-EPROBE_DEFER);
  574. }
  575. return ERR_PTR(-ENODEV);
  576. }
  577. /*
  578. * Returns number of regulators coupled with rdev.
  579. */
  580. int of_get_n_coupled(struct regulator_dev *rdev)
  581. {
  582. struct device_node *node = rdev->dev.of_node;
  583. int n_phandles;
  584. n_phandles = of_count_phandle_with_args(node,
  585. "regulator-coupled-with",
  586. NULL);
  587. return (n_phandles > 0) ? n_phandles : 0;
  588. }
  589. /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
  590. static bool of_coupling_find_node(struct device_node *src,
  591. struct device_node *to_find,
  592. int *index)
  593. {
  594. int n_phandles, i;
  595. bool found = false;
  596. n_phandles = of_count_phandle_with_args(src,
  597. "regulator-coupled-with",
  598. NULL);
  599. for (i = 0; i < n_phandles; i++) {
  600. struct device_node *tmp = of_parse_phandle(src,
  601. "regulator-coupled-with", i);
  602. if (!tmp)
  603. break;
  604. /* found */
  605. if (tmp == to_find)
  606. found = true;
  607. of_node_put(tmp);
  608. if (found) {
  609. *index = i;
  610. break;
  611. }
  612. }
  613. return found;
  614. }
  615. /**
  616. * of_check_coupling_data - Parse rdev's coupling properties and check data
  617. * consistency
  618. * @rdev: pointer to regulator_dev whose data is checked
  619. *
  620. * Function checks if all the following conditions are met:
  621. * - rdev's max_spread is greater than 0
  622. * - all coupled regulators have the same max_spread
  623. * - all coupled regulators have the same number of regulator_dev phandles
  624. * - all regulators are linked to each other
  625. *
  626. * Return: True if all conditions are met; false otherwise.
  627. */
  628. bool of_check_coupling_data(struct regulator_dev *rdev)
  629. {
  630. struct device_node *node = rdev->dev.of_node;
  631. int n_phandles = of_get_n_coupled(rdev);
  632. struct device_node *c_node;
  633. int index;
  634. int i;
  635. bool ret = true;
  636. /* iterate over rdev's phandles */
  637. for (i = 0; i < n_phandles; i++) {
  638. int max_spread = rdev->constraints->max_spread[i];
  639. int c_max_spread, c_n_phandles;
  640. if (max_spread <= 0) {
  641. dev_err(&rdev->dev, "max_spread value invalid\n");
  642. return false;
  643. }
  644. c_node = of_parse_phandle(node,
  645. "regulator-coupled-with", i);
  646. if (!c_node)
  647. ret = false;
  648. c_n_phandles = of_count_phandle_with_args(c_node,
  649. "regulator-coupled-with",
  650. NULL);
  651. if (c_n_phandles != n_phandles) {
  652. dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
  653. ret = false;
  654. goto clean;
  655. }
  656. if (!of_coupling_find_node(c_node, node, &index)) {
  657. dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
  658. ret = false;
  659. goto clean;
  660. }
  661. if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread",
  662. index, &c_max_spread)) {
  663. ret = false;
  664. goto clean;
  665. }
  666. if (c_max_spread != max_spread) {
  667. dev_err(&rdev->dev,
  668. "coupled regulators max_spread mismatch\n");
  669. ret = false;
  670. goto clean;
  671. }
  672. clean:
  673. of_node_put(c_node);
  674. if (!ret)
  675. break;
  676. }
  677. return ret;
  678. }
  679. /**
  680. * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property
  681. * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
  682. * "regulator-coupled-with" property
  683. * @index: Index in phandles array
  684. *
  685. * Return: Pointer to the &struct regulator_dev parsed from DTS, or %NULL if
  686. * it has not yet been registered.
  687. */
  688. struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
  689. int index)
  690. {
  691. struct device_node *node = rdev->dev.of_node;
  692. struct device_node *c_node;
  693. struct regulator_dev *c_rdev;
  694. c_node = of_parse_phandle(node, "regulator-coupled-with", index);
  695. if (!c_node)
  696. return NULL;
  697. c_rdev = of_find_regulator_by_node(c_node);
  698. of_node_put(c_node);
  699. return c_rdev;
  700. }
  701. /*
  702. * Check if name is a supply name according to the '*-supply' pattern
  703. * return 0 if false
  704. * return length of supply name without the -supply
  705. */
  706. static int is_supply_name(const char *name)
  707. {
  708. int strs, i;
  709. strs = strlen(name);
  710. /* string need to be at minimum len(x-supply) */
  711. if (strs < 8)
  712. return 0;
  713. for (i = strs - 6; i > 0; i--) {
  714. /* find first '-' and check if right part is supply */
  715. if (name[i] != '-')
  716. continue;
  717. if (strcmp(name + i + 1, "supply") != 0)
  718. return 0;
  719. return i;
  720. }
  721. return 0;
  722. }
  723. /**
  724. * of_regulator_bulk_get_all - get multiple regulator consumers
  725. *
  726. * @dev: Device to supply
  727. * @np: device node to search for consumers
  728. * @consumers: Configuration of consumers; clients are stored here.
  729. *
  730. * This helper function allows drivers to get several regulator
  731. * consumers in one operation. If any of the regulators cannot be
  732. * acquired then any regulators that were allocated will be freed
  733. * before returning to the caller, and @consumers will not be
  734. * changed.
  735. *
  736. * Return: Number of regulators on success, or a negative error number
  737. * on failure.
  738. */
  739. int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
  740. struct regulator_bulk_data **consumers)
  741. {
  742. int num_consumers = 0;
  743. struct regulator *tmp;
  744. struct regulator_bulk_data *_consumers = NULL;
  745. struct property *prop;
  746. int i, n = 0, ret;
  747. char name[64];
  748. /*
  749. * first pass: get numbers of xxx-supply
  750. * second pass: fill consumers
  751. */
  752. restart:
  753. for_each_property_of_node(np, prop) {
  754. i = is_supply_name(prop->name);
  755. if (i == 0)
  756. continue;
  757. if (!_consumers) {
  758. num_consumers++;
  759. continue;
  760. } else {
  761. memcpy(name, prop->name, i);
  762. name[i] = '\0';
  763. tmp = regulator_get(dev, name);
  764. if (IS_ERR(tmp)) {
  765. ret = PTR_ERR(tmp);
  766. goto error;
  767. }
  768. _consumers[n].consumer = tmp;
  769. n++;
  770. continue;
  771. }
  772. }
  773. if (_consumers) {
  774. *consumers = _consumers;
  775. return num_consumers;
  776. }
  777. if (num_consumers == 0)
  778. return 0;
  779. _consumers = kmalloc_array(num_consumers,
  780. sizeof(struct regulator_bulk_data),
  781. GFP_KERNEL);
  782. if (!_consumers)
  783. return -ENOMEM;
  784. goto restart;
  785. error:
  786. while (--n >= 0)
  787. regulator_put(_consumers[n].consumer);
  788. kfree(_consumers);
  789. return ret;
  790. }
  791. EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all);