1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // helpers.c -- Voltage/Current Regulator framework helper functions.
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
8 #include <linux/bitops.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/export.h>
12 #include <linux/kernel.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/regulator/driver.h>
20 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
22 * @rdev: regulator to operate on
24 * Regulators that use regmap for their register I/O can set the
25 * enable_reg and enable_mask fields in their descriptor and then use
26 * this as their is_enabled operation, saving some code.
28 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
33 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
37 val &= rdev->desc->enable_mask;
39 if (rdev->desc->enable_is_inverted) {
40 if (rdev->desc->enable_val)
41 return val != rdev->desc->enable_val;
44 if (rdev->desc->enable_val)
45 return val == rdev->desc->enable_val;
49 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
52 * regulator_enable_regmap - standard enable() for regmap users
54 * @rdev: regulator to operate on
56 * Regulators that use regmap for their register I/O can set the
57 * enable_reg and enable_mask fields in their descriptor and then use
58 * this as their enable() operation, saving some code.
60 int regulator_enable_regmap(struct regulator_dev *rdev)
64 if (rdev->desc->enable_is_inverted) {
65 val = rdev->desc->disable_val;
67 val = rdev->desc->enable_val;
69 val = rdev->desc->enable_mask;
72 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
73 rdev->desc->enable_mask, val);
75 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
78 * regulator_disable_regmap - standard disable() for regmap users
80 * @rdev: regulator to operate on
82 * Regulators that use regmap for their register I/O can set the
83 * enable_reg and enable_mask fields in their descriptor and then use
84 * this as their disable() operation, saving some code.
86 int regulator_disable_regmap(struct regulator_dev *rdev)
90 if (rdev->desc->enable_is_inverted) {
91 val = rdev->desc->enable_val;
93 val = rdev->desc->enable_mask;
95 val = rdev->desc->disable_val;
98 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
99 rdev->desc->enable_mask, val);
101 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
103 static int regulator_range_selector_to_index(struct regulator_dev *rdev,
108 if (!rdev->desc->linear_range_selectors_bitfield)
111 rval &= rdev->desc->vsel_range_mask;
112 rval >>= ffs(rdev->desc->vsel_range_mask) - 1;
114 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
115 if (rdev->desc->linear_range_selectors_bitfield[i] == rval)
122 * regulator_get_voltage_sel_pickable_regmap - pickable range get_voltage_sel
124 * @rdev: regulator to operate on
126 * Regulators that use regmap for their register I/O and use pickable
127 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
128 * fields in their descriptor and then use this as their get_voltage_vsel
129 * operation, saving some code.
131 int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev)
137 unsigned int voltages = 0;
138 const struct linear_range *r = rdev->desc->linear_ranges;
143 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
147 ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val);
151 val &= rdev->desc->vsel_mask;
152 val >>= ffs(rdev->desc->vsel_mask) - 1;
154 range = regulator_range_selector_to_index(rdev, r_val);
158 voltages = linear_range_values_in_range_array(r, range);
160 return val + voltages;
162 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_pickable_regmap);
165 * regulator_set_voltage_sel_pickable_regmap - pickable range set_voltage_sel
167 * @rdev: regulator to operate on
168 * @sel: Selector to set
170 * Regulators that use regmap for their register I/O and use pickable
171 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
172 * fields in their descriptor and then use this as their set_voltage_vsel
173 * operation, saving some code.
175 int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
180 unsigned int voltages_in_range = 0;
182 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
183 const struct linear_range *r;
185 r = &rdev->desc->linear_ranges[i];
186 voltages_in_range = linear_range_values_in_range(r);
188 if (sel < voltages_in_range)
190 sel -= voltages_in_range;
193 if (i == rdev->desc->n_linear_ranges)
196 sel <<= ffs(rdev->desc->vsel_mask) - 1;
197 sel += rdev->desc->linear_ranges[i].min_sel;
199 range = rdev->desc->linear_range_selectors_bitfield[i];
200 range <<= ffs(rdev->desc->vsel_mask) - 1;
202 if (rdev->desc->vsel_reg == rdev->desc->vsel_range_reg) {
203 ret = regmap_update_bits(rdev->regmap,
204 rdev->desc->vsel_reg,
205 rdev->desc->vsel_range_mask |
206 rdev->desc->vsel_mask, sel | range);
208 ret = regmap_update_bits(rdev->regmap,
209 rdev->desc->vsel_range_reg,
210 rdev->desc->vsel_range_mask, range);
214 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
215 rdev->desc->vsel_mask, sel);
221 if (rdev->desc->apply_bit)
222 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
223 rdev->desc->apply_bit,
224 rdev->desc->apply_bit);
227 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_pickable_regmap);
230 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
232 * @rdev: regulator to operate on
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 get_voltage_vsel operation, saving some code.
238 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
243 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
247 val &= rdev->desc->vsel_mask;
248 val >>= ffs(rdev->desc->vsel_mask) - 1;
252 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
255 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
257 * @rdev: regulator to operate on
258 * @sel: Selector to set
260 * Regulators that use regmap for their register I/O can set the
261 * vsel_reg and vsel_mask fields in their descriptor and then use this
262 * as their set_voltage_vsel operation, saving some code.
264 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
268 sel <<= ffs(rdev->desc->vsel_mask) - 1;
270 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
271 rdev->desc->vsel_mask, sel);
275 if (rdev->desc->apply_bit)
276 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
277 rdev->desc->apply_bit,
278 rdev->desc->apply_bit);
281 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
284 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
286 * @rdev: Regulator to operate on
287 * @min_uV: Lower bound for voltage
288 * @max_uV: Upper bound for voltage
290 * Drivers implementing set_voltage_sel() and list_voltage() can use
291 * this as their map_voltage() operation. It will find a suitable
292 * voltage by calling list_voltage() until it gets something in bounds
293 * for the requested voltages.
295 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
296 int min_uV, int max_uV)
298 int best_val = INT_MAX;
302 /* Find the smallest voltage that falls within the specified
305 for (i = 0; i < rdev->desc->n_voltages; i++) {
306 ret = rdev->desc->ops->list_voltage(rdev, i);
310 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
316 if (best_val != INT_MAX)
321 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
324 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
326 * @rdev: Regulator to operate on
327 * @min_uV: Lower bound for voltage
328 * @max_uV: Upper bound for voltage
330 * Drivers that have ascendant voltage list can use this as their
331 * map_voltage() operation.
333 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
334 int min_uV, int max_uV)
338 for (i = 0; i < rdev->desc->n_voltages; i++) {
339 ret = rdev->desc->ops->list_voltage(rdev, i);
346 if (ret >= min_uV && ret <= max_uV)
352 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
355 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
357 * @rdev: Regulator to operate on
358 * @min_uV: Lower bound for voltage
359 * @max_uV: Upper bound for voltage
361 * Drivers providing min_uV and uV_step in their regulator_desc can
362 * use this as their map_voltage() operation.
364 int regulator_map_voltage_linear(struct regulator_dev *rdev,
365 int min_uV, int max_uV)
369 /* Allow uV_step to be 0 for fixed voltage */
370 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
371 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
377 if (!rdev->desc->uV_step) {
378 BUG_ON(!rdev->desc->uV_step);
382 if (min_uV < rdev->desc->min_uV)
383 min_uV = rdev->desc->min_uV;
385 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
389 ret += rdev->desc->linear_min_sel;
391 /* Map back into a voltage to verify we're still in bounds */
392 voltage = rdev->desc->ops->list_voltage(rdev, ret);
393 if (voltage < min_uV || voltage > max_uV)
398 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
401 * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
403 * @rdev: Regulator to operate on
404 * @min_uV: Lower bound for voltage
405 * @max_uV: Upper bound for voltage
407 * Drivers providing linear_ranges in their descriptor can use this as
408 * their map_voltage() callback.
410 int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
411 int min_uV, int max_uV)
413 const struct linear_range *range;
419 if (!rdev->desc->n_linear_ranges) {
420 BUG_ON(!rdev->desc->n_linear_ranges);
424 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
425 range = &rdev->desc->linear_ranges[i];
427 ret = linear_range_get_selector_high(range, min_uV, &sel,
434 * Map back into a voltage to verify we're still in bounds.
435 * If we are not, then continue checking rest of the ranges.
437 voltage = rdev->desc->ops->list_voltage(rdev, sel);
438 if (voltage >= min_uV && voltage <= max_uV)
442 if (i == rdev->desc->n_linear_ranges)
447 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
450 * regulator_map_voltage_pickable_linear_range - map_voltage, pickable ranges
452 * @rdev: Regulator to operate on
453 * @min_uV: Lower bound for voltage
454 * @max_uV: Upper bound for voltage
456 * Drivers providing pickable linear_ranges in their descriptor can use
457 * this as their map_voltage() callback.
459 int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
460 int min_uV, int max_uV)
462 const struct linear_range *range;
465 unsigned int selector = 0;
467 if (!rdev->desc->n_linear_ranges) {
468 BUG_ON(!rdev->desc->n_linear_ranges);
472 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
477 range = &rdev->desc->linear_ranges[i];
478 linear_max_uV = linear_range_get_max_value(range);
480 if (!(min_uV <= linear_max_uV && max_uV >= range->min)) {
481 selector += linear_range_values_in_range(range);
485 ret = linear_range_get_selector_high(range, min_uV, &sel,
488 selector += linear_range_values_in_range(range);
492 ret = selector + sel - range->min_sel;
494 voltage = rdev->desc->ops->list_voltage(rdev, ret);
497 * Map back into a voltage to verify we're still in bounds.
498 * We may have overlapping voltage ranges. Hence we don't
499 * exit but retry until we have checked all ranges.
501 if (voltage < min_uV || voltage > max_uV)
502 selector += linear_range_values_in_range(range);
507 if (i == rdev->desc->n_linear_ranges)
512 EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range);
515 * regulator_desc_list_voltage_linear - List voltages with simple calculation
517 * @desc: Regulator desc for regulator which volatges are to be listed
518 * @selector: Selector to convert into a voltage
520 * Regulators with a simple linear mapping between voltages and
521 * selectors can set min_uV and uV_step in the regulator descriptor
522 * and then use this function prior regulator registration to list
523 * the voltages. This is useful when voltages need to be listed during
524 * device-tree parsing.
526 int regulator_desc_list_voltage_linear(const struct regulator_desc *desc,
527 unsigned int selector)
529 if (selector >= desc->n_voltages)
532 if (selector < desc->linear_min_sel)
535 selector -= desc->linear_min_sel;
537 return desc->min_uV + (desc->uV_step * selector);
539 EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear);
542 * regulator_list_voltage_linear - List voltages with simple calculation
544 * @rdev: Regulator device
545 * @selector: Selector to convert into a voltage
547 * Regulators with a simple linear mapping between voltages and
548 * selectors can set min_uV and uV_step in the regulator descriptor
549 * and then use this function as their list_voltage() operation,
551 int regulator_list_voltage_linear(struct regulator_dev *rdev,
552 unsigned int selector)
554 return regulator_desc_list_voltage_linear(rdev->desc, selector);
556 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
559 * regulator_list_voltage_pickable_linear_range - pickable range list voltages
561 * @rdev: Regulator device
562 * @selector: Selector to convert into a voltage
564 * list_voltage() operation, intended to be used by drivers utilizing pickable
567 int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
568 unsigned int selector)
570 const struct linear_range *range;
572 unsigned int all_sels = 0;
574 if (!rdev->desc->n_linear_ranges) {
575 BUG_ON(!rdev->desc->n_linear_ranges);
579 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
580 unsigned int sel_indexes;
582 range = &rdev->desc->linear_ranges[i];
584 sel_indexes = linear_range_values_in_range(range) - 1;
586 if (all_sels + sel_indexes >= selector) {
587 selector -= all_sels;
589 * As we see here, pickable ranges work only as
590 * long as the first selector for each pickable
591 * range is 0, and the each subsequent range for
592 * this 'pick' follow immediately at next unused
593 * selector (Eg. there is no gaps between ranges).
594 * I think this is fine but it probably should be
595 * documented. OTOH, whole pickable range stuff
596 * might benefit from some documentation
598 return range->min + (range->step * selector);
601 all_sels += (sel_indexes + 1);
606 EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range);
609 * regulator_desc_list_voltage_linear_range - List voltages for linear ranges
611 * @desc: Regulator desc for regulator which volatges are to be listed
612 * @selector: Selector to convert into a voltage
614 * Regulators with a series of simple linear mappings between voltages
615 * and selectors who have set linear_ranges in the regulator descriptor
616 * can use this function prior regulator registration to list voltages.
617 * This is useful when voltages need to be listed during device-tree
620 int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
621 unsigned int selector)
626 BUG_ON(!desc->n_linear_ranges);
628 ret = linear_range_get_value_array(desc->linear_ranges,
629 desc->n_linear_ranges, selector,
636 EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range);
639 * regulator_list_voltage_linear_range - List voltages for linear ranges
641 * @rdev: Regulator device
642 * @selector: Selector to convert into a voltage
644 * Regulators with a series of simple linear mappings between voltages
645 * and selectors can set linear_ranges in the regulator descriptor and
646 * then use this function as their list_voltage() operation,
648 int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
649 unsigned int selector)
651 return regulator_desc_list_voltage_linear_range(rdev->desc, selector);
653 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
656 * regulator_list_voltage_table - List voltages with table based mapping
658 * @rdev: Regulator device
659 * @selector: Selector to convert into a voltage
661 * Regulators with table based mapping between voltages and
662 * selectors can set volt_table in the regulator descriptor
663 * and then use this function as their list_voltage() operation.
665 int regulator_list_voltage_table(struct regulator_dev *rdev,
666 unsigned int selector)
668 if (!rdev->desc->volt_table) {
669 BUG_ON(!rdev->desc->volt_table);
673 if (selector >= rdev->desc->n_voltages)
675 if (selector < rdev->desc->linear_min_sel)
678 return rdev->desc->volt_table[selector];
680 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
683 * regulator_set_bypass_regmap - Default set_bypass() using regmap
685 * @rdev: device to operate on.
686 * @enable: state to set.
688 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
693 val = rdev->desc->bypass_val_on;
695 val = rdev->desc->bypass_mask;
697 val = rdev->desc->bypass_val_off;
700 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
701 rdev->desc->bypass_mask, val);
703 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
706 * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
708 * @rdev: device to operate on.
710 int regulator_set_soft_start_regmap(struct regulator_dev *rdev)
714 val = rdev->desc->soft_start_val_on;
716 val = rdev->desc->soft_start_mask;
718 return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg,
719 rdev->desc->soft_start_mask, val);
721 EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap);
724 * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
726 * @rdev: device to operate on.
728 int regulator_set_pull_down_regmap(struct regulator_dev *rdev)
732 val = rdev->desc->pull_down_val_on;
734 val = rdev->desc->pull_down_mask;
736 return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg,
737 rdev->desc->pull_down_mask, val);
739 EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap);
742 * regulator_get_bypass_regmap - Default get_bypass() using regmap
744 * @rdev: device to operate on.
745 * @enable: current state.
747 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
750 unsigned int val_on = rdev->desc->bypass_val_on;
753 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
758 val_on = rdev->desc->bypass_mask;
760 *enable = (val & rdev->desc->bypass_mask) == val_on;
764 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
767 * regulator_set_active_discharge_regmap - Default set_active_discharge()
770 * @rdev: device to operate on.
771 * @enable: state to set, 0 to disable and 1 to enable.
773 int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
779 val = rdev->desc->active_discharge_on;
781 val = rdev->desc->active_discharge_off;
783 return regmap_update_bits(rdev->regmap,
784 rdev->desc->active_discharge_reg,
785 rdev->desc->active_discharge_mask, val);
787 EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);
790 * regulator_set_current_limit_regmap - set_current_limit for regmap users
792 * @rdev: regulator to operate on
793 * @min_uA: Lower bound for current limit
794 * @max_uA: Upper bound for current limit
796 * Regulators that use regmap for their register I/O can set curr_table,
797 * csel_reg and csel_mask fields in their descriptor and then use this
798 * as their set_current_limit operation, saving some code.
800 int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
801 int min_uA, int max_uA)
803 unsigned int n_currents = rdev->desc->n_current_limits;
809 if (rdev->desc->curr_table) {
810 const unsigned int *curr_table = rdev->desc->curr_table;
811 bool ascend = curr_table[n_currents - 1] > curr_table[0];
813 /* search for closest to maximum */
815 for (i = n_currents - 1; i >= 0; i--) {
816 if (min_uA <= curr_table[i] &&
817 curr_table[i] <= max_uA) {
823 for (i = 0; i < n_currents; i++) {
824 if (min_uA <= curr_table[i] &&
825 curr_table[i] <= max_uA) {
836 sel <<= ffs(rdev->desc->csel_mask) - 1;
838 return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
839 rdev->desc->csel_mask, sel);
841 EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap);
844 * regulator_get_current_limit_regmap - get_current_limit for regmap users
846 * @rdev: regulator to operate on
848 * Regulators that use regmap for their register I/O can set the
849 * csel_reg and csel_mask fields in their descriptor and then use this
850 * as their get_current_limit operation, saving some code.
852 int regulator_get_current_limit_regmap(struct regulator_dev *rdev)
857 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
861 val &= rdev->desc->csel_mask;
862 val >>= ffs(rdev->desc->csel_mask) - 1;
864 if (rdev->desc->curr_table) {
865 if (val >= rdev->desc->n_current_limits)
868 return rdev->desc->curr_table[val];
873 EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap);
876 * regulator_bulk_set_supply_names - initialize the 'supply' fields in an array
877 * of regulator_bulk_data structs
879 * @consumers: array of regulator_bulk_data entries to initialize
880 * @supply_names: array of supply name strings
881 * @num_supplies: number of supply names to initialize
883 * Note: the 'consumers' array must be the size of 'num_supplies'.
885 void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
886 const char *const *supply_names,
887 unsigned int num_supplies)
891 for (i = 0; i < num_supplies; i++)
892 consumers[i].supply = supply_names[i];
894 EXPORT_SYMBOL_GPL(regulator_bulk_set_supply_names);
897 * regulator_is_equal - test whether two regulators are the same
899 * @reg1: first regulator to operate on
900 * @reg2: second regulator to operate on
902 bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
904 return reg1->rdev == reg2->rdev;
906 EXPORT_SYMBOL_GPL(regulator_is_equal);
909 * regulator_find_closest_bigger - helper to find offset in ramp delay table
911 * @target: targeted ramp_delay
912 * @table: table with supported ramp delays
913 * @num_sel: number of entries in the table
914 * @sel: Pointer to store table offset
916 * This is the internal helper used by regulator_set_ramp_delay_regmap to
917 * map ramp delay to register value. It should only be used directly if
918 * regulator_set_ramp_delay_regmap cannot handle a specific device setup
919 * (e.g. because the value is split over multiple registers).
921 int regulator_find_closest_bigger(unsigned int target, const unsigned int *table,
922 unsigned int num_sel, unsigned int *sel)
924 unsigned int s, tmp, max, maxsel = 0;
929 for (s = 0; s < num_sel; s++) {
930 if (table[s] > max) {
934 if (table[s] >= target) {
935 if (!found || table[s] - target < tmp - target) {
952 EXPORT_SYMBOL_GPL(regulator_find_closest_bigger);
955 * regulator_set_ramp_delay_regmap - set_ramp_delay() helper
957 * @rdev: regulator to operate on
958 * @ramp_delay: ramp-rate value given in units V/S (uV/uS)
960 * Regulators that use regmap for their register I/O can set the ramp_reg
961 * and ramp_mask fields in their descriptor and then use this as their
962 * set_ramp_delay operation, saving some code.
964 int regulator_set_ramp_delay_regmap(struct regulator_dev *rdev, int ramp_delay)
969 if (WARN_ON(!rdev->desc->n_ramp_values || !rdev->desc->ramp_delay_table))
972 ret = regulator_find_closest_bigger(ramp_delay, rdev->desc->ramp_delay_table,
973 rdev->desc->n_ramp_values, &sel);
976 dev_warn(rdev_get_dev(rdev),
977 "Can't set ramp-delay %u, setting %u\n", ramp_delay,
978 rdev->desc->ramp_delay_table[sel]);
981 sel <<= ffs(rdev->desc->ramp_mask) - 1;
983 return regmap_update_bits(rdev->regmap, rdev->desc->ramp_reg,
984 rdev->desc->ramp_mask, sel);
986 EXPORT_SYMBOL_GPL(regulator_set_ramp_delay_regmap);