helpers.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // helpers.c -- Voltage/Current Regulator framework helper functions.
  4. //
  5. // Copyright 2007, 2008 Wolfson Microelectronics PLC.
  6. // Copyright 2008 SlimLogic Ltd.
  7. #include <linux/kernel.h>
  8. #include <linux/err.h>
  9. #include <linux/delay.h>
  10. #include <linux/regmap.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/module.h>
  14. #include "internal.h"
  15. /**
  16. * regulator_is_enabled_regmap - standard is_enabled() for regmap users
  17. *
  18. * @rdev: regulator to operate on
  19. *
  20. * Regulators that use regmap for their register I/O can set the
  21. * enable_reg and enable_mask fields in their descriptor and then use
  22. * this as their is_enabled operation, saving some code.
  23. */
  24. int regulator_is_enabled_regmap(struct regulator_dev *rdev)
  25. {
  26. unsigned int val;
  27. int ret;
  28. ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
  29. if (ret != 0)
  30. return ret;
  31. val &= rdev->desc->enable_mask;
  32. if (rdev->desc->enable_is_inverted) {
  33. if (rdev->desc->enable_val)
  34. return val != rdev->desc->enable_val;
  35. return val == 0;
  36. } else {
  37. if (rdev->desc->enable_val)
  38. return val == rdev->desc->enable_val;
  39. return val != 0;
  40. }
  41. }
  42. EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
  43. /**
  44. * regulator_enable_regmap - standard enable() for regmap users
  45. *
  46. * @rdev: regulator to operate on
  47. *
  48. * Regulators that use regmap for their register I/O can set the
  49. * enable_reg and enable_mask fields in their descriptor and then use
  50. * this as their enable() operation, saving some code.
  51. */
  52. int regulator_enable_regmap(struct regulator_dev *rdev)
  53. {
  54. unsigned int val;
  55. if (rdev->desc->enable_is_inverted) {
  56. val = rdev->desc->disable_val;
  57. } else {
  58. val = rdev->desc->enable_val;
  59. if (!val)
  60. val = rdev->desc->enable_mask;
  61. }
  62. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  63. rdev->desc->enable_mask, val);
  64. }
  65. EXPORT_SYMBOL_GPL(regulator_enable_regmap);
  66. /**
  67. * regulator_disable_regmap - standard disable() for regmap users
  68. *
  69. * @rdev: regulator to operate on
  70. *
  71. * Regulators that use regmap for their register I/O can set the
  72. * enable_reg and enable_mask fields in their descriptor and then use
  73. * this as their disable() operation, saving some code.
  74. */
  75. int regulator_disable_regmap(struct regulator_dev *rdev)
  76. {
  77. unsigned int val;
  78. if (rdev->desc->enable_is_inverted) {
  79. val = rdev->desc->enable_val;
  80. if (!val)
  81. val = rdev->desc->enable_mask;
  82. } else {
  83. val = rdev->desc->disable_val;
  84. }
  85. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  86. rdev->desc->enable_mask, val);
  87. }
  88. EXPORT_SYMBOL_GPL(regulator_disable_regmap);
  89. static int regulator_range_selector_to_index(struct regulator_dev *rdev,
  90. unsigned int rval)
  91. {
  92. int i;
  93. if (!rdev->desc->linear_range_selectors)
  94. return -EINVAL;
  95. rval &= rdev->desc->vsel_range_mask;
  96. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  97. if (rdev->desc->linear_range_selectors[i] == rval)
  98. return i;
  99. }
  100. return -EINVAL;
  101. }
  102. /**
  103. * regulator_get_voltage_sel_pickable_regmap - pickable range get_voltage_sel
  104. *
  105. * @rdev: regulator to operate on
  106. *
  107. * Regulators that use regmap for their register I/O and use pickable
  108. * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
  109. * fields in their descriptor and then use this as their get_voltage_vsel
  110. * operation, saving some code.
  111. */
  112. int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev)
  113. {
  114. unsigned int r_val;
  115. int range;
  116. unsigned int val;
  117. int ret;
  118. unsigned int voltages = 0;
  119. const struct linear_range *r = rdev->desc->linear_ranges;
  120. if (!r)
  121. return -EINVAL;
  122. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
  123. if (ret != 0)
  124. return ret;
  125. ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val);
  126. if (ret != 0)
  127. return ret;
  128. val &= rdev->desc->vsel_mask;
  129. val >>= ffs(rdev->desc->vsel_mask) - 1;
  130. range = regulator_range_selector_to_index(rdev, r_val);
  131. if (range < 0)
  132. return -EINVAL;
  133. voltages = linear_range_values_in_range_array(r, range);
  134. return val + voltages;
  135. }
  136. EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_pickable_regmap);
  137. /**
  138. * regulator_set_voltage_sel_pickable_regmap - pickable range set_voltage_sel
  139. *
  140. * @rdev: regulator to operate on
  141. * @sel: Selector to set
  142. *
  143. * Regulators that use regmap for their register I/O and use pickable
  144. * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
  145. * fields in their descriptor and then use this as their set_voltage_vsel
  146. * operation, saving some code.
  147. */
  148. int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
  149. unsigned int sel)
  150. {
  151. unsigned int range;
  152. int ret, i;
  153. unsigned int voltages_in_range = 0;
  154. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  155. const struct linear_range *r;
  156. r = &rdev->desc->linear_ranges[i];
  157. voltages_in_range = linear_range_values_in_range(r);
  158. if (sel < voltages_in_range)
  159. break;
  160. sel -= voltages_in_range;
  161. }
  162. if (i == rdev->desc->n_linear_ranges)
  163. return -EINVAL;
  164. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  165. sel += rdev->desc->linear_ranges[i].min_sel;
  166. range = rdev->desc->linear_range_selectors[i];
  167. if (rdev->desc->vsel_reg == rdev->desc->vsel_range_reg) {
  168. ret = regmap_update_bits(rdev->regmap,
  169. rdev->desc->vsel_reg,
  170. rdev->desc->vsel_range_mask |
  171. rdev->desc->vsel_mask, sel | range);
  172. } else {
  173. ret = regmap_update_bits(rdev->regmap,
  174. rdev->desc->vsel_range_reg,
  175. rdev->desc->vsel_range_mask, range);
  176. if (ret)
  177. return ret;
  178. ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
  179. rdev->desc->vsel_mask, sel);
  180. }
  181. if (ret)
  182. return ret;
  183. if (rdev->desc->apply_bit)
  184. ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
  185. rdev->desc->apply_bit,
  186. rdev->desc->apply_bit);
  187. return ret;
  188. }
  189. EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_pickable_regmap);
  190. /**
  191. * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
  192. *
  193. * @rdev: regulator to operate on
  194. *
  195. * Regulators that use regmap for their register I/O can set the
  196. * vsel_reg and vsel_mask fields in their descriptor and then use this
  197. * as their get_voltage_vsel operation, saving some code.
  198. */
  199. int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
  200. {
  201. unsigned int val;
  202. int ret;
  203. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
  204. if (ret != 0)
  205. return ret;
  206. val &= rdev->desc->vsel_mask;
  207. val >>= ffs(rdev->desc->vsel_mask) - 1;
  208. return val;
  209. }
  210. EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
  211. /**
  212. * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
  213. *
  214. * @rdev: regulator to operate on
  215. * @sel: Selector to set
  216. *
  217. * Regulators that use regmap for their register I/O can set the
  218. * vsel_reg and vsel_mask fields in their descriptor and then use this
  219. * as their set_voltage_vsel operation, saving some code.
  220. */
  221. int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
  222. {
  223. int ret;
  224. sel <<= ffs(rdev->desc->vsel_mask) - 1;
  225. ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
  226. rdev->desc->vsel_mask, sel);
  227. if (ret)
  228. return ret;
  229. if (rdev->desc->apply_bit)
  230. ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
  231. rdev->desc->apply_bit,
  232. rdev->desc->apply_bit);
  233. return ret;
  234. }
  235. EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
  236. /**
  237. * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
  238. *
  239. * @rdev: Regulator to operate on
  240. * @min_uV: Lower bound for voltage
  241. * @max_uV: Upper bound for voltage
  242. *
  243. * Drivers implementing set_voltage_sel() and list_voltage() can use
  244. * this as their map_voltage() operation. It will find a suitable
  245. * voltage by calling list_voltage() until it gets something in bounds
  246. * for the requested voltages.
  247. */
  248. int regulator_map_voltage_iterate(struct regulator_dev *rdev,
  249. int min_uV, int max_uV)
  250. {
  251. int best_val = INT_MAX;
  252. int selector = 0;
  253. int i, ret;
  254. /* Find the smallest voltage that falls within the specified
  255. * range.
  256. */
  257. for (i = 0; i < rdev->desc->n_voltages; i++) {
  258. ret = rdev->desc->ops->list_voltage(rdev, i);
  259. if (ret < 0)
  260. continue;
  261. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  262. best_val = ret;
  263. selector = i;
  264. }
  265. }
  266. if (best_val != INT_MAX)
  267. return selector;
  268. else
  269. return -EINVAL;
  270. }
  271. EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
  272. /**
  273. * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
  274. *
  275. * @rdev: Regulator to operate on
  276. * @min_uV: Lower bound for voltage
  277. * @max_uV: Upper bound for voltage
  278. *
  279. * Drivers that have ascendant voltage list can use this as their
  280. * map_voltage() operation.
  281. */
  282. int regulator_map_voltage_ascend(struct regulator_dev *rdev,
  283. int min_uV, int max_uV)
  284. {
  285. int i, ret;
  286. for (i = 0; i < rdev->desc->n_voltages; i++) {
  287. ret = rdev->desc->ops->list_voltage(rdev, i);
  288. if (ret < 0)
  289. continue;
  290. if (ret > max_uV)
  291. break;
  292. if (ret >= min_uV && ret <= max_uV)
  293. return i;
  294. }
  295. return -EINVAL;
  296. }
  297. EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
  298. /**
  299. * regulator_map_voltage_linear - map_voltage() for simple linear mappings
  300. *
  301. * @rdev: Regulator to operate on
  302. * @min_uV: Lower bound for voltage
  303. * @max_uV: Upper bound for voltage
  304. *
  305. * Drivers providing min_uV and uV_step in their regulator_desc can
  306. * use this as their map_voltage() operation.
  307. */
  308. int regulator_map_voltage_linear(struct regulator_dev *rdev,
  309. int min_uV, int max_uV)
  310. {
  311. int ret, voltage;
  312. /* Allow uV_step to be 0 for fixed voltage */
  313. if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
  314. if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
  315. return 0;
  316. else
  317. return -EINVAL;
  318. }
  319. if (!rdev->desc->uV_step) {
  320. BUG_ON(!rdev->desc->uV_step);
  321. return -EINVAL;
  322. }
  323. if (min_uV < rdev->desc->min_uV)
  324. min_uV = rdev->desc->min_uV;
  325. ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
  326. if (ret < 0)
  327. return ret;
  328. ret += rdev->desc->linear_min_sel;
  329. /* Map back into a voltage to verify we're still in bounds */
  330. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  331. if (voltage < min_uV || voltage > max_uV)
  332. return -EINVAL;
  333. return ret;
  334. }
  335. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
  336. /**
  337. * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
  338. *
  339. * @rdev: Regulator to operate on
  340. * @min_uV: Lower bound for voltage
  341. * @max_uV: Upper bound for voltage
  342. *
  343. * Drivers providing linear_ranges in their descriptor can use this as
  344. * their map_voltage() callback.
  345. */
  346. int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
  347. int min_uV, int max_uV)
  348. {
  349. const struct linear_range *range;
  350. int ret = -EINVAL;
  351. unsigned int sel;
  352. bool found;
  353. int voltage, i;
  354. if (!rdev->desc->n_linear_ranges) {
  355. BUG_ON(!rdev->desc->n_linear_ranges);
  356. return -EINVAL;
  357. }
  358. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  359. range = &rdev->desc->linear_ranges[i];
  360. ret = linear_range_get_selector_high(range, min_uV, &sel,
  361. &found);
  362. if (ret)
  363. continue;
  364. ret = sel;
  365. /*
  366. * Map back into a voltage to verify we're still in bounds.
  367. * If we are not, then continue checking rest of the ranges.
  368. */
  369. voltage = rdev->desc->ops->list_voltage(rdev, sel);
  370. if (voltage >= min_uV && voltage <= max_uV)
  371. break;
  372. }
  373. if (i == rdev->desc->n_linear_ranges)
  374. return -EINVAL;
  375. return ret;
  376. }
  377. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
  378. /**
  379. * regulator_map_voltage_pickable_linear_range - map_voltage, pickable ranges
  380. *
  381. * @rdev: Regulator to operate on
  382. * @min_uV: Lower bound for voltage
  383. * @max_uV: Upper bound for voltage
  384. *
  385. * Drivers providing pickable linear_ranges in their descriptor can use
  386. * this as their map_voltage() callback.
  387. */
  388. int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
  389. int min_uV, int max_uV)
  390. {
  391. const struct linear_range *range;
  392. int ret = -EINVAL;
  393. int voltage, i;
  394. unsigned int selector = 0;
  395. if (!rdev->desc->n_linear_ranges) {
  396. BUG_ON(!rdev->desc->n_linear_ranges);
  397. return -EINVAL;
  398. }
  399. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  400. int linear_max_uV;
  401. bool found;
  402. unsigned int sel;
  403. range = &rdev->desc->linear_ranges[i];
  404. linear_max_uV = linear_range_get_max_value(range);
  405. if (!(min_uV <= linear_max_uV && max_uV >= range->min)) {
  406. selector += linear_range_values_in_range(range);
  407. continue;
  408. }
  409. ret = linear_range_get_selector_high(range, min_uV, &sel,
  410. &found);
  411. if (ret) {
  412. selector += linear_range_values_in_range(range);
  413. continue;
  414. }
  415. ret = selector + sel - range->min_sel;
  416. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  417. /*
  418. * Map back into a voltage to verify we're still in bounds.
  419. * We may have overlapping voltage ranges. Hence we don't
  420. * exit but retry until we have checked all ranges.
  421. */
  422. if (voltage < min_uV || voltage > max_uV)
  423. selector += linear_range_values_in_range(range);
  424. else
  425. break;
  426. }
  427. if (i == rdev->desc->n_linear_ranges)
  428. return -EINVAL;
  429. return ret;
  430. }
  431. EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range);
  432. /**
  433. * regulator_desc_list_voltage_linear - List voltages with simple calculation
  434. *
  435. * @desc: Regulator desc for regulator which volatges are to be listed
  436. * @selector: Selector to convert into a voltage
  437. *
  438. * Regulators with a simple linear mapping between voltages and
  439. * selectors can set min_uV and uV_step in the regulator descriptor
  440. * and then use this function prior regulator registration to list
  441. * the voltages. This is useful when voltages need to be listed during
  442. * device-tree parsing.
  443. */
  444. int regulator_desc_list_voltage_linear(const struct regulator_desc *desc,
  445. unsigned int selector)
  446. {
  447. if (selector >= desc->n_voltages)
  448. return -EINVAL;
  449. if (selector < desc->linear_min_sel)
  450. return 0;
  451. selector -= desc->linear_min_sel;
  452. return desc->min_uV + (desc->uV_step * selector);
  453. }
  454. EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear);
  455. /**
  456. * regulator_list_voltage_linear - List voltages with simple calculation
  457. *
  458. * @rdev: Regulator device
  459. * @selector: Selector to convert into a voltage
  460. *
  461. * Regulators with a simple linear mapping between voltages and
  462. * selectors can set min_uV and uV_step in the regulator descriptor
  463. * and then use this function as their list_voltage() operation,
  464. */
  465. int regulator_list_voltage_linear(struct regulator_dev *rdev,
  466. unsigned int selector)
  467. {
  468. return regulator_desc_list_voltage_linear(rdev->desc, selector);
  469. }
  470. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
  471. /**
  472. * regulator_list_voltage_pickable_linear_range - pickable range list voltages
  473. *
  474. * @rdev: Regulator device
  475. * @selector: Selector to convert into a voltage
  476. *
  477. * list_voltage() operation, intended to be used by drivers utilizing pickable
  478. * ranges helpers.
  479. */
  480. int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
  481. unsigned int selector)
  482. {
  483. const struct linear_range *range;
  484. int i;
  485. unsigned int all_sels = 0;
  486. if (!rdev->desc->n_linear_ranges) {
  487. BUG_ON(!rdev->desc->n_linear_ranges);
  488. return -EINVAL;
  489. }
  490. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  491. unsigned int sel_indexes;
  492. range = &rdev->desc->linear_ranges[i];
  493. sel_indexes = linear_range_values_in_range(range) - 1;
  494. if (all_sels + sel_indexes >= selector) {
  495. selector -= all_sels;
  496. /*
  497. * As we see here, pickable ranges work only as
  498. * long as the first selector for each pickable
  499. * range is 0, and the each subsequent range for
  500. * this 'pick' follow immediately at next unused
  501. * selector (Eg. there is no gaps between ranges).
  502. * I think this is fine but it probably should be
  503. * documented. OTOH, whole pickable range stuff
  504. * might benefit from some documentation
  505. */
  506. return range->min + (range->step * selector);
  507. }
  508. all_sels += (sel_indexes + 1);
  509. }
  510. return -EINVAL;
  511. }
  512. EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range);
  513. /**
  514. * regulator_desc_list_voltage_linear_range - List voltages for linear ranges
  515. *
  516. * @desc: Regulator desc for regulator which volatges are to be listed
  517. * @selector: Selector to convert into a voltage
  518. *
  519. * Regulators with a series of simple linear mappings between voltages
  520. * and selectors who have set linear_ranges in the regulator descriptor
  521. * can use this function prior regulator registration to list voltages.
  522. * This is useful when voltages need to be listed during device-tree
  523. * parsing.
  524. */
  525. int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
  526. unsigned int selector)
  527. {
  528. unsigned int val;
  529. int ret;
  530. BUG_ON(!desc->n_linear_ranges);
  531. ret = linear_range_get_value_array(desc->linear_ranges,
  532. desc->n_linear_ranges, selector,
  533. &val);
  534. if (ret)
  535. return ret;
  536. return val;
  537. }
  538. EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range);
  539. /**
  540. * regulator_list_voltage_linear_range - List voltages for linear ranges
  541. *
  542. * @rdev: Regulator device
  543. * @selector: Selector to convert into a voltage
  544. *
  545. * Regulators with a series of simple linear mappings between voltages
  546. * and selectors can set linear_ranges in the regulator descriptor and
  547. * then use this function as their list_voltage() operation,
  548. */
  549. int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
  550. unsigned int selector)
  551. {
  552. return regulator_desc_list_voltage_linear_range(rdev->desc, selector);
  553. }
  554. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
  555. /**
  556. * regulator_list_voltage_table - List voltages with table based mapping
  557. *
  558. * @rdev: Regulator device
  559. * @selector: Selector to convert into a voltage
  560. *
  561. * Regulators with table based mapping between voltages and
  562. * selectors can set volt_table in the regulator descriptor
  563. * and then use this function as their list_voltage() operation.
  564. */
  565. int regulator_list_voltage_table(struct regulator_dev *rdev,
  566. unsigned int selector)
  567. {
  568. if (!rdev->desc->volt_table) {
  569. BUG_ON(!rdev->desc->volt_table);
  570. return -EINVAL;
  571. }
  572. if (selector >= rdev->desc->n_voltages)
  573. return -EINVAL;
  574. if (selector < rdev->desc->linear_min_sel)
  575. return 0;
  576. return rdev->desc->volt_table[selector];
  577. }
  578. EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
  579. /**
  580. * regulator_set_bypass_regmap - Default set_bypass() using regmap
  581. *
  582. * @rdev: device to operate on.
  583. * @enable: state to set.
  584. */
  585. int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
  586. {
  587. unsigned int val;
  588. if (enable) {
  589. val = rdev->desc->bypass_val_on;
  590. if (!val)
  591. val = rdev->desc->bypass_mask;
  592. } else {
  593. val = rdev->desc->bypass_val_off;
  594. }
  595. return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
  596. rdev->desc->bypass_mask, val);
  597. }
  598. EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
  599. /**
  600. * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
  601. *
  602. * @rdev: device to operate on.
  603. */
  604. int regulator_set_soft_start_regmap(struct regulator_dev *rdev)
  605. {
  606. unsigned int val;
  607. val = rdev->desc->soft_start_val_on;
  608. if (!val)
  609. val = rdev->desc->soft_start_mask;
  610. return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg,
  611. rdev->desc->soft_start_mask, val);
  612. }
  613. EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap);
  614. /**
  615. * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
  616. *
  617. * @rdev: device to operate on.
  618. */
  619. int regulator_set_pull_down_regmap(struct regulator_dev *rdev)
  620. {
  621. unsigned int val;
  622. val = rdev->desc->pull_down_val_on;
  623. if (!val)
  624. val = rdev->desc->pull_down_mask;
  625. return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg,
  626. rdev->desc->pull_down_mask, val);
  627. }
  628. EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap);
  629. /**
  630. * regulator_get_bypass_regmap - Default get_bypass() using regmap
  631. *
  632. * @rdev: device to operate on.
  633. * @enable: current state.
  634. */
  635. int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
  636. {
  637. unsigned int val;
  638. unsigned int val_on = rdev->desc->bypass_val_on;
  639. int ret;
  640. ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
  641. if (ret != 0)
  642. return ret;
  643. if (!val_on)
  644. val_on = rdev->desc->bypass_mask;
  645. *enable = (val & rdev->desc->bypass_mask) == val_on;
  646. return 0;
  647. }
  648. EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
  649. /**
  650. * regulator_set_active_discharge_regmap - Default set_active_discharge()
  651. * using regmap
  652. *
  653. * @rdev: device to operate on.
  654. * @enable: state to set, 0 to disable and 1 to enable.
  655. */
  656. int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
  657. bool enable)
  658. {
  659. unsigned int val;
  660. if (enable)
  661. val = rdev->desc->active_discharge_on;
  662. else
  663. val = rdev->desc->active_discharge_off;
  664. return regmap_update_bits(rdev->regmap,
  665. rdev->desc->active_discharge_reg,
  666. rdev->desc->active_discharge_mask, val);
  667. }
  668. EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);
  669. /**
  670. * regulator_set_current_limit_regmap - set_current_limit for regmap users
  671. *
  672. * @rdev: regulator to operate on
  673. * @min_uA: Lower bound for current limit
  674. * @max_uA: Upper bound for current limit
  675. *
  676. * Regulators that use regmap for their register I/O can set curr_table,
  677. * csel_reg and csel_mask fields in their descriptor and then use this
  678. * as their set_current_limit operation, saving some code.
  679. */
  680. int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
  681. int min_uA, int max_uA)
  682. {
  683. unsigned int n_currents = rdev->desc->n_current_limits;
  684. int i, sel = -1;
  685. if (n_currents == 0)
  686. return -EINVAL;
  687. if (rdev->desc->curr_table) {
  688. const unsigned int *curr_table = rdev->desc->curr_table;
  689. bool ascend = curr_table[n_currents - 1] > curr_table[0];
  690. /* search for closest to maximum */
  691. if (ascend) {
  692. for (i = n_currents - 1; i >= 0; i--) {
  693. if (min_uA <= curr_table[i] &&
  694. curr_table[i] <= max_uA) {
  695. sel = i;
  696. break;
  697. }
  698. }
  699. } else {
  700. for (i = 0; i < n_currents; i++) {
  701. if (min_uA <= curr_table[i] &&
  702. curr_table[i] <= max_uA) {
  703. sel = i;
  704. break;
  705. }
  706. }
  707. }
  708. }
  709. if (sel < 0)
  710. return -EINVAL;
  711. sel <<= ffs(rdev->desc->csel_mask) - 1;
  712. return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
  713. rdev->desc->csel_mask, sel);
  714. }
  715. EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap);
  716. /**
  717. * regulator_get_current_limit_regmap - get_current_limit for regmap users
  718. *
  719. * @rdev: regulator to operate on
  720. *
  721. * Regulators that use regmap for their register I/O can set the
  722. * csel_reg and csel_mask fields in their descriptor and then use this
  723. * as their get_current_limit operation, saving some code.
  724. */
  725. int regulator_get_current_limit_regmap(struct regulator_dev *rdev)
  726. {
  727. unsigned int val;
  728. int ret;
  729. ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
  730. if (ret != 0)
  731. return ret;
  732. val &= rdev->desc->csel_mask;
  733. val >>= ffs(rdev->desc->csel_mask) - 1;
  734. if (rdev->desc->curr_table) {
  735. if (val >= rdev->desc->n_current_limits)
  736. return -EINVAL;
  737. return rdev->desc->curr_table[val];
  738. }
  739. return -EINVAL;
  740. }
  741. EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap);
  742. /**
  743. * regulator_bulk_set_supply_names - initialize the 'supply' fields in an array
  744. * of regulator_bulk_data structs
  745. *
  746. * @consumers: array of regulator_bulk_data entries to initialize
  747. * @supply_names: array of supply name strings
  748. * @num_supplies: number of supply names to initialize
  749. *
  750. * Note: the 'consumers' array must be the size of 'num_supplies'.
  751. */
  752. void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
  753. const char *const *supply_names,
  754. unsigned int num_supplies)
  755. {
  756. unsigned int i;
  757. for (i = 0; i < num_supplies; i++)
  758. consumers[i].supply = supply_names[i];
  759. }
  760. EXPORT_SYMBOL_GPL(regulator_bulk_set_supply_names);
  761. /**
  762. * regulator_is_equal - test whether two regulators are the same
  763. *
  764. * @reg1: first regulator to operate on
  765. * @reg2: second regulator to operate on
  766. */
  767. bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
  768. {
  769. return reg1->rdev == reg2->rdev;
  770. }
  771. EXPORT_SYMBOL_GPL(regulator_is_equal);
  772. static int find_closest_bigger(unsigned int target, const unsigned int *table,
  773. unsigned int num_sel, unsigned int *sel)
  774. {
  775. unsigned int s, tmp, max, maxsel = 0;
  776. bool found = false;
  777. max = table[0];
  778. for (s = 0; s < num_sel; s++) {
  779. if (table[s] > max) {
  780. max = table[s];
  781. maxsel = s;
  782. }
  783. if (table[s] >= target) {
  784. if (!found || table[s] - target < tmp - target) {
  785. tmp = table[s];
  786. *sel = s;
  787. found = true;
  788. if (tmp == target)
  789. break;
  790. }
  791. }
  792. }
  793. if (!found) {
  794. *sel = maxsel;
  795. return -EINVAL;
  796. }
  797. return 0;
  798. }
  799. /**
  800. * regulator_set_ramp_delay_regmap - set_ramp_delay() helper
  801. *
  802. * @rdev: regulator to operate on
  803. *
  804. * Regulators that use regmap for their register I/O can set the ramp_reg
  805. * and ramp_mask fields in their descriptor and then use this as their
  806. * set_ramp_delay operation, saving some code.
  807. */
  808. int regulator_set_ramp_delay_regmap(struct regulator_dev *rdev, int ramp_delay)
  809. {
  810. int ret;
  811. unsigned int sel;
  812. if (WARN_ON(!rdev->desc->n_ramp_values || !rdev->desc->ramp_delay_table))
  813. return -EINVAL;
  814. ret = find_closest_bigger(ramp_delay, rdev->desc->ramp_delay_table,
  815. rdev->desc->n_ramp_values, &sel);
  816. if (ret) {
  817. dev_warn(rdev_get_dev(rdev),
  818. "Can't set ramp-delay %u, setting %u\n", ramp_delay,
  819. rdev->desc->ramp_delay_table[sel]);
  820. }
  821. sel <<= ffs(rdev->desc->ramp_mask) - 1;
  822. return regmap_update_bits(rdev->regmap, rdev->desc->ramp_reg,
  823. rdev->desc->ramp_mask, sel);
  824. }
  825. EXPORT_SYMBOL_GPL(regulator_set_ramp_delay_regmap);