of_regulator.c 26 KB

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