helpers.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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, i;
  118. unsigned int voltages_in_range = 0;
  119. if (!rdev->desc->linear_ranges)
  120. return -EINVAL;
  121. ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
  122. if (ret != 0)
  123. return ret;
  124. ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val);
  125. if (ret != 0)
  126. return ret;
  127. val &= rdev->desc->vsel_mask;
  128. val >>= ffs(rdev->desc->vsel_mask) - 1;
  129. range = regulator_range_selector_to_index(rdev, r_val);
  130. if (range < 0)
  131. return -EINVAL;
  132. for (i = 0; i < range; i++)
  133. voltages_in_range += (rdev->desc->linear_ranges[i].max_sel -
  134. rdev->desc->linear_ranges[i].min_sel) + 1;
  135. return val + voltages_in_range;
  136. }
  137. EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_pickable_regmap);
  138. /**
  139. * regulator_set_voltage_sel_pickable_regmap - pickable range set_voltage_sel
  140. *
  141. * @rdev: regulator to operate on
  142. * @sel: Selector to set
  143. *
  144. * Regulators that use regmap for their register I/O and use pickable
  145. * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
  146. * fields in their descriptor and then use this as their set_voltage_vsel
  147. * operation, saving some code.
  148. */
  149. int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
  150. unsigned int sel)
  151. {
  152. unsigned int range;
  153. int ret, i;
  154. unsigned int voltages_in_range = 0;
  155. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  156. voltages_in_range = (rdev->desc->linear_ranges[i].max_sel -
  157. rdev->desc->linear_ranges[i].min_sel) + 1;
  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 regulator_linear_range *range;
  350. int ret = -EINVAL;
  351. int voltage, i;
  352. if (!rdev->desc->n_linear_ranges) {
  353. BUG_ON(!rdev->desc->n_linear_ranges);
  354. return -EINVAL;
  355. }
  356. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  357. int linear_max_uV;
  358. range = &rdev->desc->linear_ranges[i];
  359. linear_max_uV = range->min_uV +
  360. (range->max_sel - range->min_sel) * range->uV_step;
  361. if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV))
  362. continue;
  363. if (min_uV <= range->min_uV)
  364. min_uV = range->min_uV;
  365. /* range->uV_step == 0 means fixed voltage range */
  366. if (range->uV_step == 0) {
  367. ret = 0;
  368. } else {
  369. ret = DIV_ROUND_UP(min_uV - range->min_uV,
  370. range->uV_step);
  371. if (ret < 0)
  372. return ret;
  373. }
  374. ret += range->min_sel;
  375. /*
  376. * Map back into a voltage to verify we're still in bounds.
  377. * If we are not, then continue checking rest of the ranges.
  378. */
  379. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  380. if (voltage >= min_uV && voltage <= max_uV)
  381. break;
  382. }
  383. if (i == rdev->desc->n_linear_ranges)
  384. return -EINVAL;
  385. return ret;
  386. }
  387. EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
  388. /**
  389. * regulator_map_voltage_pickable_linear_range - map_voltage, pickable ranges
  390. *
  391. * @rdev: Regulator to operate on
  392. * @min_uV: Lower bound for voltage
  393. * @max_uV: Upper bound for voltage
  394. *
  395. * Drivers providing pickable linear_ranges in their descriptor can use
  396. * this as their map_voltage() callback.
  397. */
  398. int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
  399. int min_uV, int max_uV)
  400. {
  401. const struct regulator_linear_range *range;
  402. int ret = -EINVAL;
  403. int voltage, i;
  404. unsigned int selector = 0;
  405. if (!rdev->desc->n_linear_ranges) {
  406. BUG_ON(!rdev->desc->n_linear_ranges);
  407. return -EINVAL;
  408. }
  409. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  410. int linear_max_uV;
  411. range = &rdev->desc->linear_ranges[i];
  412. linear_max_uV = range->min_uV +
  413. (range->max_sel - range->min_sel) * range->uV_step;
  414. if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV)) {
  415. selector += (range->max_sel - range->min_sel + 1);
  416. continue;
  417. }
  418. if (min_uV <= range->min_uV)
  419. min_uV = range->min_uV;
  420. /* range->uV_step == 0 means fixed voltage range */
  421. if (range->uV_step == 0) {
  422. ret = 0;
  423. } else {
  424. ret = DIV_ROUND_UP(min_uV - range->min_uV,
  425. range->uV_step);
  426. if (ret < 0)
  427. return ret;
  428. }
  429. ret += selector;
  430. voltage = rdev->desc->ops->list_voltage(rdev, ret);
  431. /*
  432. * Map back into a voltage to verify we're still in bounds.
  433. * We may have overlapping voltage ranges. Hence we don't
  434. * exit but retry until we have checked all ranges.
  435. */
  436. if (voltage < min_uV || voltage > max_uV)
  437. selector += (range->max_sel - range->min_sel + 1);
  438. else
  439. break;
  440. }
  441. if (i == rdev->desc->n_linear_ranges)
  442. return -EINVAL;
  443. return ret;
  444. }
  445. EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range);
  446. /**
  447. * regulator_list_voltage_linear - List voltages with simple calculation
  448. *
  449. * @rdev: Regulator device
  450. * @selector: Selector to convert into a voltage
  451. *
  452. * Regulators with a simple linear mapping between voltages and
  453. * selectors can set min_uV and uV_step in the regulator descriptor
  454. * and then use this function as their list_voltage() operation,
  455. */
  456. int regulator_list_voltage_linear(struct regulator_dev *rdev,
  457. unsigned int selector)
  458. {
  459. if (selector >= rdev->desc->n_voltages)
  460. return -EINVAL;
  461. if (selector < rdev->desc->linear_min_sel)
  462. return 0;
  463. selector -= rdev->desc->linear_min_sel;
  464. return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
  465. }
  466. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
  467. /**
  468. * regulator_list_voltage_pickable_linear_range - pickable range list voltages
  469. *
  470. * @rdev: Regulator device
  471. * @selector: Selector to convert into a voltage
  472. *
  473. * list_voltage() operation, intended to be used by drivers utilizing pickable
  474. * ranges helpers.
  475. */
  476. int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
  477. unsigned int selector)
  478. {
  479. const struct regulator_linear_range *range;
  480. int i;
  481. unsigned int all_sels = 0;
  482. if (!rdev->desc->n_linear_ranges) {
  483. BUG_ON(!rdev->desc->n_linear_ranges);
  484. return -EINVAL;
  485. }
  486. for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
  487. unsigned int sels_in_range;
  488. range = &rdev->desc->linear_ranges[i];
  489. sels_in_range = range->max_sel - range->min_sel;
  490. if (all_sels + sels_in_range >= selector) {
  491. selector -= all_sels;
  492. return range->min_uV + (range->uV_step * selector);
  493. }
  494. all_sels += (sels_in_range + 1);
  495. }
  496. return -EINVAL;
  497. }
  498. EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range);
  499. /**
  500. * regulator_desc_list_voltage_linear_range - List voltages for linear ranges
  501. *
  502. * @desc: Regulator desc for regulator which volatges are to be listed
  503. * @selector: Selector to convert into a voltage
  504. *
  505. * Regulators with a series of simple linear mappings between voltages
  506. * and selectors who have set linear_ranges in the regulator descriptor
  507. * can use this function prior regulator registration to list voltages.
  508. * This is useful when voltages need to be listed during device-tree
  509. * parsing.
  510. */
  511. int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
  512. unsigned int selector)
  513. {
  514. const struct regulator_linear_range *range;
  515. int i;
  516. if (!desc->n_linear_ranges) {
  517. BUG_ON(!desc->n_linear_ranges);
  518. return -EINVAL;
  519. }
  520. for (i = 0; i < desc->n_linear_ranges; i++) {
  521. range = &desc->linear_ranges[i];
  522. if (!(selector >= range->min_sel &&
  523. selector <= range->max_sel))
  524. continue;
  525. selector -= range->min_sel;
  526. return range->min_uV + (range->uV_step * selector);
  527. }
  528. return -EINVAL;
  529. }
  530. EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range);
  531. /**
  532. * regulator_list_voltage_linear_range - List voltages for linear ranges
  533. *
  534. * @rdev: Regulator device
  535. * @selector: Selector to convert into a voltage
  536. *
  537. * Regulators with a series of simple linear mappings between voltages
  538. * and selectors can set linear_ranges in the regulator descriptor and
  539. * then use this function as their list_voltage() operation,
  540. */
  541. int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
  542. unsigned int selector)
  543. {
  544. return regulator_desc_list_voltage_linear_range(rdev->desc, selector);
  545. }
  546. EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
  547. /**
  548. * regulator_list_voltage_table - List voltages with table based mapping
  549. *
  550. * @rdev: Regulator device
  551. * @selector: Selector to convert into a voltage
  552. *
  553. * Regulators with table based mapping between voltages and
  554. * selectors can set volt_table in the regulator descriptor
  555. * and then use this function as their list_voltage() operation.
  556. */
  557. int regulator_list_voltage_table(struct regulator_dev *rdev,
  558. unsigned int selector)
  559. {
  560. if (!rdev->desc->volt_table) {
  561. BUG_ON(!rdev->desc->volt_table);
  562. return -EINVAL;
  563. }
  564. if (selector >= rdev->desc->n_voltages)
  565. return -EINVAL;
  566. return rdev->desc->volt_table[selector];
  567. }
  568. EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
  569. /**
  570. * regulator_set_bypass_regmap - Default set_bypass() using regmap
  571. *
  572. * @rdev: device to operate on.
  573. * @enable: state to set.
  574. */
  575. int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
  576. {
  577. unsigned int val;
  578. if (enable) {
  579. val = rdev->desc->bypass_val_on;
  580. if (!val)
  581. val = rdev->desc->bypass_mask;
  582. } else {
  583. val = rdev->desc->bypass_val_off;
  584. }
  585. return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
  586. rdev->desc->bypass_mask, val);
  587. }
  588. EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
  589. /**
  590. * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
  591. *
  592. * @rdev: device to operate on.
  593. */
  594. int regulator_set_soft_start_regmap(struct regulator_dev *rdev)
  595. {
  596. unsigned int val;
  597. val = rdev->desc->soft_start_val_on;
  598. if (!val)
  599. val = rdev->desc->soft_start_mask;
  600. return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg,
  601. rdev->desc->soft_start_mask, val);
  602. }
  603. EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap);
  604. /**
  605. * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
  606. *
  607. * @rdev: device to operate on.
  608. */
  609. int regulator_set_pull_down_regmap(struct regulator_dev *rdev)
  610. {
  611. unsigned int val;
  612. val = rdev->desc->pull_down_val_on;
  613. if (!val)
  614. val = rdev->desc->pull_down_mask;
  615. return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg,
  616. rdev->desc->pull_down_mask, val);
  617. }
  618. EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap);
  619. /**
  620. * regulator_get_bypass_regmap - Default get_bypass() using regmap
  621. *
  622. * @rdev: device to operate on.
  623. * @enable: current state.
  624. */
  625. int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
  626. {
  627. unsigned int val;
  628. unsigned int val_on = rdev->desc->bypass_val_on;
  629. int ret;
  630. ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
  631. if (ret != 0)
  632. return ret;
  633. if (!val_on)
  634. val_on = rdev->desc->bypass_mask;
  635. *enable = (val & rdev->desc->bypass_mask) == val_on;
  636. return 0;
  637. }
  638. EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
  639. /**
  640. * regulator_set_active_discharge_regmap - Default set_active_discharge()
  641. * using regmap
  642. *
  643. * @rdev: device to operate on.
  644. * @enable: state to set, 0 to disable and 1 to enable.
  645. */
  646. int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
  647. bool enable)
  648. {
  649. unsigned int val;
  650. if (enable)
  651. val = rdev->desc->active_discharge_on;
  652. else
  653. val = rdev->desc->active_discharge_off;
  654. return regmap_update_bits(rdev->regmap,
  655. rdev->desc->active_discharge_reg,
  656. rdev->desc->active_discharge_mask, val);
  657. }
  658. EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);
  659. /**
  660. * regulator_set_current_limit_regmap - set_current_limit for regmap users
  661. *
  662. * @rdev: regulator to operate on
  663. * @min_uA: Lower bound for current limit
  664. * @max_uA: Upper bound for current limit
  665. *
  666. * Regulators that use regmap for their register I/O can set curr_table,
  667. * csel_reg and csel_mask fields in their descriptor and then use this
  668. * as their set_current_limit operation, saving some code.
  669. */
  670. int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
  671. int min_uA, int max_uA)
  672. {
  673. unsigned int n_currents = rdev->desc->n_current_limits;
  674. int i, sel = -1;
  675. if (n_currents == 0)
  676. return -EINVAL;
  677. if (rdev->desc->curr_table) {
  678. const unsigned int *curr_table = rdev->desc->curr_table;
  679. bool ascend = curr_table[n_currents - 1] > curr_table[0];
  680. /* search for closest to maximum */
  681. if (ascend) {
  682. for (i = n_currents - 1; i >= 0; i--) {
  683. if (min_uA <= curr_table[i] &&
  684. curr_table[i] <= max_uA) {
  685. sel = i;
  686. break;
  687. }
  688. }
  689. } else {
  690. for (i = 0; i < n_currents; i++) {
  691. if (min_uA <= curr_table[i] &&
  692. curr_table[i] <= max_uA) {
  693. sel = i;
  694. break;
  695. }
  696. }
  697. }
  698. }
  699. if (sel < 0)
  700. return -EINVAL;
  701. sel <<= ffs(rdev->desc->csel_mask) - 1;
  702. return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
  703. rdev->desc->csel_mask, sel);
  704. }
  705. EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap);
  706. /**
  707. * regulator_get_current_limit_regmap - get_current_limit for regmap users
  708. *
  709. * @rdev: regulator to operate on
  710. *
  711. * Regulators that use regmap for their register I/O can set the
  712. * csel_reg and csel_mask fields in their descriptor and then use this
  713. * as their get_current_limit operation, saving some code.
  714. */
  715. int regulator_get_current_limit_regmap(struct regulator_dev *rdev)
  716. {
  717. unsigned int val;
  718. int ret;
  719. ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
  720. if (ret != 0)
  721. return ret;
  722. val &= rdev->desc->csel_mask;
  723. val >>= ffs(rdev->desc->csel_mask) - 1;
  724. if (rdev->desc->curr_table) {
  725. if (val >= rdev->desc->n_current_limits)
  726. return -EINVAL;
  727. return rdev->desc->curr_table[val];
  728. }
  729. return -EINVAL;
  730. }
  731. EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap);
  732. /**
  733. * regulator_bulk_set_supply_names - initialize the 'supply' fields in an array
  734. * of regulator_bulk_data structs
  735. *
  736. * @consumers: array of regulator_bulk_data entries to initialize
  737. * @supply_names: array of supply name strings
  738. * @num_supplies: number of supply names to initialize
  739. *
  740. * Note: the 'consumers' array must be the size of 'num_supplies'.
  741. */
  742. void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
  743. const char *const *supply_names,
  744. unsigned int num_supplies)
  745. {
  746. unsigned int i;
  747. for (i = 0; i < num_supplies; i++)
  748. consumers[i].supply = supply_names[i];
  749. }
  750. EXPORT_SYMBOL_GPL(regulator_bulk_set_supply_names);
  751. /**
  752. * regulator_is_equal - test whether two regulators are the same
  753. *
  754. * @reg1: first regulator to operate on
  755. * @reg2: second regulator to operate on
  756. */
  757. bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
  758. {
  759. return reg1->rdev == reg2->rdev;
  760. }
  761. EXPORT_SYMBOL_GPL(regulator_is_equal);