media: stv06xx: add missing descriptor sanity checks
[linux/fpc-iii.git] / drivers / regulator / helpers.c
blobbb16c465426ef80ad228a4f6efe7b86bc06ef10b
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.
8 #include <linux/kernel.h>
9 #include <linux/err.h>
10 #include <linux/delay.h>
11 #include <linux/regmap.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/module.h>
16 #include "internal.h"
18 /**
19 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
21 * @rdev: regulator to operate on
23 * Regulators that use regmap for their register I/O can set the
24 * enable_reg and enable_mask fields in their descriptor and then use
25 * this as their is_enabled operation, saving some code.
27 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
29 unsigned int val;
30 int ret;
32 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
33 if (ret != 0)
34 return ret;
36 val &= rdev->desc->enable_mask;
38 if (rdev->desc->enable_is_inverted) {
39 if (rdev->desc->enable_val)
40 return val != rdev->desc->enable_val;
41 return val == 0;
42 } else {
43 if (rdev->desc->enable_val)
44 return val == rdev->desc->enable_val;
45 return val != 0;
48 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
50 /**
51 * regulator_enable_regmap - standard enable() for regmap users
53 * @rdev: regulator to operate on
55 * Regulators that use regmap for their register I/O can set the
56 * enable_reg and enable_mask fields in their descriptor and then use
57 * this as their enable() operation, saving some code.
59 int regulator_enable_regmap(struct regulator_dev *rdev)
61 unsigned int val;
63 if (rdev->desc->enable_is_inverted) {
64 val = rdev->desc->disable_val;
65 } else {
66 val = rdev->desc->enable_val;
67 if (!val)
68 val = rdev->desc->enable_mask;
71 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
72 rdev->desc->enable_mask, val);
74 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
76 /**
77 * regulator_disable_regmap - standard disable() for regmap users
79 * @rdev: regulator to operate on
81 * Regulators that use regmap for their register I/O can set the
82 * enable_reg and enable_mask fields in their descriptor and then use
83 * this as their disable() operation, saving some code.
85 int regulator_disable_regmap(struct regulator_dev *rdev)
87 unsigned int val;
89 if (rdev->desc->enable_is_inverted) {
90 val = rdev->desc->enable_val;
91 if (!val)
92 val = rdev->desc->enable_mask;
93 } else {
94 val = rdev->desc->disable_val;
97 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
98 rdev->desc->enable_mask, val);
100 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
102 static int regulator_range_selector_to_index(struct regulator_dev *rdev,
103 unsigned int rval)
105 int i;
107 if (!rdev->desc->linear_range_selectors)
108 return -EINVAL;
110 rval &= rdev->desc->vsel_range_mask;
112 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
113 if (rdev->desc->linear_range_selectors[i] == rval)
114 return i;
116 return -EINVAL;
120 * regulator_get_voltage_sel_pickable_regmap - pickable range get_voltage_sel
122 * @rdev: regulator to operate on
124 * Regulators that use regmap for their register I/O and use pickable
125 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
126 * fields in their descriptor and then use this as their get_voltage_vsel
127 * operation, saving some code.
129 int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev)
131 unsigned int r_val;
132 int range;
133 unsigned int val;
134 int ret, i;
135 unsigned int voltages_in_range = 0;
137 if (!rdev->desc->linear_ranges)
138 return -EINVAL;
140 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
141 if (ret != 0)
142 return ret;
144 ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val);
145 if (ret != 0)
146 return ret;
148 val &= rdev->desc->vsel_mask;
149 val >>= ffs(rdev->desc->vsel_mask) - 1;
151 range = regulator_range_selector_to_index(rdev, r_val);
152 if (range < 0)
153 return -EINVAL;
155 for (i = 0; i < range; i++)
156 voltages_in_range += (rdev->desc->linear_ranges[i].max_sel -
157 rdev->desc->linear_ranges[i].min_sel) + 1;
159 return val + voltages_in_range;
161 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_pickable_regmap);
164 * regulator_set_voltage_sel_pickable_regmap - pickable range set_voltage_sel
166 * @rdev: regulator to operate on
167 * @sel: Selector to set
169 * Regulators that use regmap for their register I/O and use pickable
170 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
171 * fields in their descriptor and then use this as their set_voltage_vsel
172 * operation, saving some code.
174 int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
175 unsigned int sel)
177 unsigned int range;
178 int ret, i;
179 unsigned int voltages_in_range = 0;
181 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
182 voltages_in_range = (rdev->desc->linear_ranges[i].max_sel -
183 rdev->desc->linear_ranges[i].min_sel) + 1;
184 if (sel < voltages_in_range)
185 break;
186 sel -= voltages_in_range;
189 if (i == rdev->desc->n_linear_ranges)
190 return -EINVAL;
192 sel <<= ffs(rdev->desc->vsel_mask) - 1;
193 sel += rdev->desc->linear_ranges[i].min_sel;
195 range = rdev->desc->linear_range_selectors[i];
197 if (rdev->desc->vsel_reg == rdev->desc->vsel_range_reg) {
198 ret = regmap_update_bits(rdev->regmap,
199 rdev->desc->vsel_reg,
200 rdev->desc->vsel_range_mask |
201 rdev->desc->vsel_mask, sel | range);
202 } else {
203 ret = regmap_update_bits(rdev->regmap,
204 rdev->desc->vsel_range_reg,
205 rdev->desc->vsel_range_mask, range);
206 if (ret)
207 return ret;
209 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
210 rdev->desc->vsel_mask, sel);
213 if (ret)
214 return ret;
216 if (rdev->desc->apply_bit)
217 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
218 rdev->desc->apply_bit,
219 rdev->desc->apply_bit);
220 return ret;
222 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_pickable_regmap);
225 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
227 * @rdev: regulator to operate on
229 * Regulators that use regmap for their register I/O can set the
230 * vsel_reg and vsel_mask fields in their descriptor and then use this
231 * as their get_voltage_vsel operation, saving some code.
233 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
235 unsigned int val;
236 int ret;
238 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
239 if (ret != 0)
240 return ret;
242 val &= rdev->desc->vsel_mask;
243 val >>= ffs(rdev->desc->vsel_mask) - 1;
245 return val;
247 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
250 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
252 * @rdev: regulator to operate on
253 * @sel: Selector to set
255 * Regulators that use regmap for their register I/O can set the
256 * vsel_reg and vsel_mask fields in their descriptor and then use this
257 * as their set_voltage_vsel operation, saving some code.
259 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
261 int ret;
263 sel <<= ffs(rdev->desc->vsel_mask) - 1;
265 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
266 rdev->desc->vsel_mask, sel);
267 if (ret)
268 return ret;
270 if (rdev->desc->apply_bit)
271 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
272 rdev->desc->apply_bit,
273 rdev->desc->apply_bit);
274 return ret;
276 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
279 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
281 * @rdev: Regulator to operate on
282 * @min_uV: Lower bound for voltage
283 * @max_uV: Upper bound for voltage
285 * Drivers implementing set_voltage_sel() and list_voltage() can use
286 * this as their map_voltage() operation. It will find a suitable
287 * voltage by calling list_voltage() until it gets something in bounds
288 * for the requested voltages.
290 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
291 int min_uV, int max_uV)
293 int best_val = INT_MAX;
294 int selector = 0;
295 int i, ret;
297 /* Find the smallest voltage that falls within the specified
298 * range.
300 for (i = 0; i < rdev->desc->n_voltages; i++) {
301 ret = rdev->desc->ops->list_voltage(rdev, i);
302 if (ret < 0)
303 continue;
305 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
306 best_val = ret;
307 selector = i;
311 if (best_val != INT_MAX)
312 return selector;
313 else
314 return -EINVAL;
316 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
319 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
321 * @rdev: Regulator to operate on
322 * @min_uV: Lower bound for voltage
323 * @max_uV: Upper bound for voltage
325 * Drivers that have ascendant voltage list can use this as their
326 * map_voltage() operation.
328 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
329 int min_uV, int max_uV)
331 int i, ret;
333 for (i = 0; i < rdev->desc->n_voltages; i++) {
334 ret = rdev->desc->ops->list_voltage(rdev, i);
335 if (ret < 0)
336 continue;
338 if (ret > max_uV)
339 break;
341 if (ret >= min_uV && ret <= max_uV)
342 return i;
345 return -EINVAL;
347 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
350 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
352 * @rdev: Regulator to operate on
353 * @min_uV: Lower bound for voltage
354 * @max_uV: Upper bound for voltage
356 * Drivers providing min_uV and uV_step in their regulator_desc can
357 * use this as their map_voltage() operation.
359 int regulator_map_voltage_linear(struct regulator_dev *rdev,
360 int min_uV, int max_uV)
362 int ret, voltage;
364 /* Allow uV_step to be 0 for fixed voltage */
365 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
366 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
367 return 0;
368 else
369 return -EINVAL;
372 if (!rdev->desc->uV_step) {
373 BUG_ON(!rdev->desc->uV_step);
374 return -EINVAL;
377 if (min_uV < rdev->desc->min_uV)
378 min_uV = rdev->desc->min_uV;
380 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
381 if (ret < 0)
382 return ret;
384 ret += rdev->desc->linear_min_sel;
386 /* Map back into a voltage to verify we're still in bounds */
387 voltage = rdev->desc->ops->list_voltage(rdev, ret);
388 if (voltage < min_uV || voltage > max_uV)
389 return -EINVAL;
391 return ret;
393 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
396 * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
398 * @rdev: Regulator to operate on
399 * @min_uV: Lower bound for voltage
400 * @max_uV: Upper bound for voltage
402 * Drivers providing linear_ranges in their descriptor can use this as
403 * their map_voltage() callback.
405 int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
406 int min_uV, int max_uV)
408 const struct regulator_linear_range *range;
409 int ret = -EINVAL;
410 int voltage, i;
412 if (!rdev->desc->n_linear_ranges) {
413 BUG_ON(!rdev->desc->n_linear_ranges);
414 return -EINVAL;
417 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
418 int linear_max_uV;
420 range = &rdev->desc->linear_ranges[i];
421 linear_max_uV = range->min_uV +
422 (range->max_sel - range->min_sel) * range->uV_step;
424 if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV))
425 continue;
427 if (min_uV <= range->min_uV)
428 min_uV = range->min_uV;
430 /* range->uV_step == 0 means fixed voltage range */
431 if (range->uV_step == 0) {
432 ret = 0;
433 } else {
434 ret = DIV_ROUND_UP(min_uV - range->min_uV,
435 range->uV_step);
436 if (ret < 0)
437 return ret;
440 ret += range->min_sel;
443 * Map back into a voltage to verify we're still in bounds.
444 * If we are not, then continue checking rest of the ranges.
446 voltage = rdev->desc->ops->list_voltage(rdev, ret);
447 if (voltage >= min_uV && voltage <= max_uV)
448 break;
451 if (i == rdev->desc->n_linear_ranges)
452 return -EINVAL;
454 return ret;
456 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
459 * regulator_map_voltage_pickable_linear_range - map_voltage, pickable ranges
461 * @rdev: Regulator to operate on
462 * @min_uV: Lower bound for voltage
463 * @max_uV: Upper bound for voltage
465 * Drivers providing pickable linear_ranges in their descriptor can use
466 * this as their map_voltage() callback.
468 int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
469 int min_uV, int max_uV)
471 const struct regulator_linear_range *range;
472 int ret = -EINVAL;
473 int voltage, i;
474 unsigned int selector = 0;
476 if (!rdev->desc->n_linear_ranges) {
477 BUG_ON(!rdev->desc->n_linear_ranges);
478 return -EINVAL;
481 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
482 int linear_max_uV;
484 range = &rdev->desc->linear_ranges[i];
485 linear_max_uV = range->min_uV +
486 (range->max_sel - range->min_sel) * range->uV_step;
488 if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV)) {
489 selector += (range->max_sel - range->min_sel + 1);
490 continue;
493 if (min_uV <= range->min_uV)
494 min_uV = range->min_uV;
496 /* range->uV_step == 0 means fixed voltage range */
497 if (range->uV_step == 0) {
498 ret = 0;
499 } else {
500 ret = DIV_ROUND_UP(min_uV - range->min_uV,
501 range->uV_step);
502 if (ret < 0)
503 return ret;
506 ret += selector;
508 voltage = rdev->desc->ops->list_voltage(rdev, ret);
511 * Map back into a voltage to verify we're still in bounds.
512 * We may have overlapping voltage ranges. Hence we don't
513 * exit but retry until we have checked all ranges.
515 if (voltage < min_uV || voltage > max_uV)
516 selector += (range->max_sel - range->min_sel + 1);
517 else
518 break;
521 if (i == rdev->desc->n_linear_ranges)
522 return -EINVAL;
524 return ret;
526 EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range);
529 * regulator_list_voltage_linear - List voltages with simple calculation
531 * @rdev: Regulator device
532 * @selector: Selector to convert into a voltage
534 * Regulators with a simple linear mapping between voltages and
535 * selectors can set min_uV and uV_step in the regulator descriptor
536 * and then use this function as their list_voltage() operation,
538 int regulator_list_voltage_linear(struct regulator_dev *rdev,
539 unsigned int selector)
541 if (selector >= rdev->desc->n_voltages)
542 return -EINVAL;
543 if (selector < rdev->desc->linear_min_sel)
544 return 0;
546 selector -= rdev->desc->linear_min_sel;
548 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
550 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
553 * regulator_list_voltage_pickable_linear_range - pickable range list voltages
555 * @rdev: Regulator device
556 * @selector: Selector to convert into a voltage
558 * list_voltage() operation, intended to be used by drivers utilizing pickable
559 * ranges helpers.
561 int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
562 unsigned int selector)
564 const struct regulator_linear_range *range;
565 int i;
566 unsigned int all_sels = 0;
568 if (!rdev->desc->n_linear_ranges) {
569 BUG_ON(!rdev->desc->n_linear_ranges);
570 return -EINVAL;
573 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
574 unsigned int sels_in_range;
576 range = &rdev->desc->linear_ranges[i];
578 sels_in_range = range->max_sel - range->min_sel;
580 if (all_sels + sels_in_range >= selector) {
581 selector -= all_sels;
582 return range->min_uV + (range->uV_step * selector);
585 all_sels += (sels_in_range + 1);
588 return -EINVAL;
590 EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range);
593 * regulator_desc_list_voltage_linear_range - List voltages for linear ranges
595 * @desc: Regulator desc for regulator which volatges are to be listed
596 * @selector: Selector to convert into a voltage
598 * Regulators with a series of simple linear mappings between voltages
599 * and selectors who have set linear_ranges in the regulator descriptor
600 * can use this function prior regulator registration to list voltages.
601 * This is useful when voltages need to be listed during device-tree
602 * parsing.
604 int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
605 unsigned int selector)
607 const struct regulator_linear_range *range;
608 int i;
610 if (!desc->n_linear_ranges) {
611 BUG_ON(!desc->n_linear_ranges);
612 return -EINVAL;
615 for (i = 0; i < desc->n_linear_ranges; i++) {
616 range = &desc->linear_ranges[i];
618 if (!(selector >= range->min_sel &&
619 selector <= range->max_sel))
620 continue;
622 selector -= range->min_sel;
624 return range->min_uV + (range->uV_step * selector);
627 return -EINVAL;
629 EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range);
632 * regulator_list_voltage_linear_range - List voltages for linear ranges
634 * @rdev: Regulator device
635 * @selector: Selector to convert into a voltage
637 * Regulators with a series of simple linear mappings between voltages
638 * and selectors can set linear_ranges in the regulator descriptor and
639 * then use this function as their list_voltage() operation,
641 int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
642 unsigned int selector)
644 return regulator_desc_list_voltage_linear_range(rdev->desc, selector);
646 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
649 * regulator_list_voltage_table - List voltages with table based mapping
651 * @rdev: Regulator device
652 * @selector: Selector to convert into a voltage
654 * Regulators with table based mapping between voltages and
655 * selectors can set volt_table in the regulator descriptor
656 * and then use this function as their list_voltage() operation.
658 int regulator_list_voltage_table(struct regulator_dev *rdev,
659 unsigned int selector)
661 if (!rdev->desc->volt_table) {
662 BUG_ON(!rdev->desc->volt_table);
663 return -EINVAL;
666 if (selector >= rdev->desc->n_voltages)
667 return -EINVAL;
669 return rdev->desc->volt_table[selector];
671 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
674 * regulator_set_bypass_regmap - Default set_bypass() using regmap
676 * @rdev: device to operate on.
677 * @enable: state to set.
679 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
681 unsigned int val;
683 if (enable) {
684 val = rdev->desc->bypass_val_on;
685 if (!val)
686 val = rdev->desc->bypass_mask;
687 } else {
688 val = rdev->desc->bypass_val_off;
691 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
692 rdev->desc->bypass_mask, val);
694 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
697 * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
699 * @rdev: device to operate on.
701 int regulator_set_soft_start_regmap(struct regulator_dev *rdev)
703 unsigned int val;
705 val = rdev->desc->soft_start_val_on;
706 if (!val)
707 val = rdev->desc->soft_start_mask;
709 return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg,
710 rdev->desc->soft_start_mask, val);
712 EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap);
715 * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
717 * @rdev: device to operate on.
719 int regulator_set_pull_down_regmap(struct regulator_dev *rdev)
721 unsigned int val;
723 val = rdev->desc->pull_down_val_on;
724 if (!val)
725 val = rdev->desc->pull_down_mask;
727 return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg,
728 rdev->desc->pull_down_mask, val);
730 EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap);
733 * regulator_get_bypass_regmap - Default get_bypass() using regmap
735 * @rdev: device to operate on.
736 * @enable: current state.
738 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
740 unsigned int val;
741 unsigned int val_on = rdev->desc->bypass_val_on;
742 int ret;
744 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
745 if (ret != 0)
746 return ret;
748 if (!val_on)
749 val_on = rdev->desc->bypass_mask;
751 *enable = (val & rdev->desc->bypass_mask) == val_on;
753 return 0;
755 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
758 * regulator_set_active_discharge_regmap - Default set_active_discharge()
759 * using regmap
761 * @rdev: device to operate on.
762 * @enable: state to set, 0 to disable and 1 to enable.
764 int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
765 bool enable)
767 unsigned int val;
769 if (enable)
770 val = rdev->desc->active_discharge_on;
771 else
772 val = rdev->desc->active_discharge_off;
774 return regmap_update_bits(rdev->regmap,
775 rdev->desc->active_discharge_reg,
776 rdev->desc->active_discharge_mask, val);
778 EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);
781 * regulator_set_current_limit_regmap - set_current_limit for regmap users
783 * @rdev: regulator to operate on
784 * @min_uA: Lower bound for current limit
785 * @max_uA: Upper bound for current limit
787 * Regulators that use regmap for their register I/O can set curr_table,
788 * csel_reg and csel_mask fields in their descriptor and then use this
789 * as their set_current_limit operation, saving some code.
791 int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
792 int min_uA, int max_uA)
794 unsigned int n_currents = rdev->desc->n_current_limits;
795 int i, sel = -1;
797 if (n_currents == 0)
798 return -EINVAL;
800 if (rdev->desc->curr_table) {
801 const unsigned int *curr_table = rdev->desc->curr_table;
802 bool ascend = curr_table[n_currents - 1] > curr_table[0];
804 /* search for closest to maximum */
805 if (ascend) {
806 for (i = n_currents - 1; i >= 0; i--) {
807 if (min_uA <= curr_table[i] &&
808 curr_table[i] <= max_uA) {
809 sel = i;
810 break;
813 } else {
814 for (i = 0; i < n_currents; i++) {
815 if (min_uA <= curr_table[i] &&
816 curr_table[i] <= max_uA) {
817 sel = i;
818 break;
824 if (sel < 0)
825 return -EINVAL;
827 sel <<= ffs(rdev->desc->csel_mask) - 1;
829 return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
830 rdev->desc->csel_mask, sel);
832 EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap);
835 * regulator_get_current_limit_regmap - get_current_limit for regmap users
837 * @rdev: regulator to operate on
839 * Regulators that use regmap for their register I/O can set the
840 * csel_reg and csel_mask fields in their descriptor and then use this
841 * as their get_current_limit operation, saving some code.
843 int regulator_get_current_limit_regmap(struct regulator_dev *rdev)
845 unsigned int val;
846 int ret;
848 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
849 if (ret != 0)
850 return ret;
852 val &= rdev->desc->csel_mask;
853 val >>= ffs(rdev->desc->csel_mask) - 1;
855 if (rdev->desc->curr_table) {
856 if (val >= rdev->desc->n_current_limits)
857 return -EINVAL;
859 return rdev->desc->curr_table[val];
862 return -EINVAL;
864 EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap);
867 * regulator_bulk_set_supply_names - initialize the 'supply' fields in an array
868 * of regulator_bulk_data structs
870 * @consumers: array of regulator_bulk_data entries to initialize
871 * @supply_names: array of supply name strings
872 * @num_supplies: number of supply names to initialize
874 * Note: the 'consumers' array must be the size of 'num_supplies'.
876 void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
877 const char *const *supply_names,
878 unsigned int num_supplies)
880 unsigned int i;
882 for (i = 0; i < num_supplies; i++)
883 consumers[i].supply = supply_names[i];
885 EXPORT_SYMBOL_GPL(regulator_bulk_set_supply_names);
888 * regulator_is_equal - test whether two regulators are the same
890 * @reg1: first regulator to operate on
891 * @reg2: second regulator to operate on
893 bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
895 return reg1->rdev == reg2->rdev;
897 EXPORT_SYMBOL_GPL(regulator_is_equal);