helpers.c 26 KB

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