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_sel
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
);
164 static int write_separate_vsel_and_range(struct regulator_dev
*rdev
,
165 unsigned int sel
, unsigned int range
)
170 ret
= regmap_update_bits_base(rdev
->regmap
, rdev
->desc
->vsel_range_reg
,
171 rdev
->desc
->vsel_range_mask
,
172 range
, &range_updated
, false, false);
177 * Some PMICs treat the vsel_reg same as apply-bit. Force it to be
178 * written if the range changed, even if the old selector was same as
181 if (rdev
->desc
->range_applied_by_vsel
&& range_updated
)
182 return regmap_write_bits(rdev
->regmap
,
183 rdev
->desc
->vsel_reg
,
184 rdev
->desc
->vsel_mask
, sel
);
186 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->vsel_reg
,
187 rdev
->desc
->vsel_mask
, sel
);
191 * regulator_set_voltage_sel_pickable_regmap - pickable range set_voltage_sel
193 * @rdev: regulator to operate on
194 * @sel: Selector to set
196 * Regulators that use regmap for their register I/O and use pickable
197 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
198 * fields in their descriptor and then use this as their set_voltage_sel
199 * operation, saving some code.
201 int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev
*rdev
,
206 unsigned int voltages_in_range
= 0;
208 for (i
= 0; i
< rdev
->desc
->n_linear_ranges
; i
++) {
209 const struct linear_range
*r
;
211 r
= &rdev
->desc
->linear_ranges
[i
];
212 voltages_in_range
= linear_range_values_in_range(r
);
214 if (sel
< voltages_in_range
)
216 sel
-= voltages_in_range
;
219 if (i
== rdev
->desc
->n_linear_ranges
)
222 sel
<<= ffs(rdev
->desc
->vsel_mask
) - 1;
223 sel
+= rdev
->desc
->linear_ranges
[i
].min_sel
;
225 range
= rdev
->desc
->linear_range_selectors_bitfield
[i
];
226 range
<<= ffs(rdev
->desc
->vsel_range_mask
) - 1;
228 if (rdev
->desc
->vsel_reg
== rdev
->desc
->vsel_range_reg
)
229 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->vsel_reg
,
230 rdev
->desc
->vsel_range_mask
|
231 rdev
->desc
->vsel_mask
, sel
| range
);
233 ret
= write_separate_vsel_and_range(rdev
, sel
, range
);
238 if (rdev
->desc
->apply_bit
)
239 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->apply_reg
,
240 rdev
->desc
->apply_bit
,
241 rdev
->desc
->apply_bit
);
244 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_pickable_regmap
);
247 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
249 * @rdev: regulator to operate on
251 * Regulators that use regmap for their register I/O can set the
252 * vsel_reg and vsel_mask fields in their descriptor and then use this
253 * as their get_voltage_sel operation, saving some code.
255 int regulator_get_voltage_sel_regmap(struct regulator_dev
*rdev
)
260 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->vsel_reg
, &val
);
264 val
&= rdev
->desc
->vsel_mask
;
265 val
>>= ffs(rdev
->desc
->vsel_mask
) - 1;
269 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap
);
272 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
274 * @rdev: regulator to operate on
275 * @sel: Selector to set
277 * Regulators that use regmap for their register I/O can set the
278 * vsel_reg and vsel_mask fields in their descriptor and then use this
279 * as their set_voltage_sel operation, saving some code.
281 int regulator_set_voltage_sel_regmap(struct regulator_dev
*rdev
, unsigned sel
)
285 sel
<<= ffs(rdev
->desc
->vsel_mask
) - 1;
287 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->vsel_reg
,
288 rdev
->desc
->vsel_mask
, sel
);
292 if (rdev
->desc
->apply_bit
)
293 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->apply_reg
,
294 rdev
->desc
->apply_bit
,
295 rdev
->desc
->apply_bit
);
298 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap
);
301 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
303 * @rdev: Regulator to operate on
304 * @min_uV: Lower bound for voltage
305 * @max_uV: Upper bound for voltage
307 * Drivers implementing set_voltage_sel() and list_voltage() can use
308 * this as their map_voltage() operation. It will find a suitable
309 * voltage by calling list_voltage() until it gets something in bounds
310 * for the requested voltages.
312 int regulator_map_voltage_iterate(struct regulator_dev
*rdev
,
313 int min_uV
, int max_uV
)
315 int best_val
= INT_MAX
;
319 /* Find the smallest voltage that falls within the specified
322 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
323 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
327 if (ret
< best_val
&& ret
>= min_uV
&& ret
<= max_uV
) {
333 if (best_val
!= INT_MAX
)
338 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate
);
341 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
343 * @rdev: Regulator to operate on
344 * @min_uV: Lower bound for voltage
345 * @max_uV: Upper bound for voltage
347 * Drivers that have ascendant voltage list can use this as their
348 * map_voltage() operation.
350 int regulator_map_voltage_ascend(struct regulator_dev
*rdev
,
351 int min_uV
, int max_uV
)
355 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
356 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
363 if (ret
>= min_uV
&& ret
<= max_uV
)
369 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend
);
372 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
374 * @rdev: Regulator to operate on
375 * @min_uV: Lower bound for voltage
376 * @max_uV: Upper bound for voltage
378 * Drivers providing min_uV and uV_step in their regulator_desc can
379 * use this as their map_voltage() operation.
381 int regulator_map_voltage_linear(struct regulator_dev
*rdev
,
382 int min_uV
, int max_uV
)
386 /* Allow uV_step to be 0 for fixed voltage */
387 if (rdev
->desc
->n_voltages
== 1 && rdev
->desc
->uV_step
== 0) {
388 if (min_uV
<= rdev
->desc
->min_uV
&& rdev
->desc
->min_uV
<= max_uV
)
394 if (!rdev
->desc
->uV_step
) {
395 BUG_ON(!rdev
->desc
->uV_step
);
399 if (min_uV
< rdev
->desc
->min_uV
)
400 min_uV
= rdev
->desc
->min_uV
;
402 ret
= DIV_ROUND_UP(min_uV
- rdev
->desc
->min_uV
, rdev
->desc
->uV_step
);
406 ret
+= rdev
->desc
->linear_min_sel
;
408 /* Map back into a voltage to verify we're still in bounds */
409 voltage
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
410 if (voltage
< min_uV
|| voltage
> max_uV
)
415 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear
);
418 * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
420 * @rdev: Regulator to operate on
421 * @min_uV: Lower bound for voltage
422 * @max_uV: Upper bound for voltage
424 * Drivers providing linear_ranges in their descriptor can use this as
425 * their map_voltage() callback.
427 int regulator_map_voltage_linear_range(struct regulator_dev
*rdev
,
428 int min_uV
, int max_uV
)
430 const struct linear_range
*range
;
436 if (!rdev
->desc
->n_linear_ranges
) {
437 BUG_ON(!rdev
->desc
->n_linear_ranges
);
441 for (i
= 0; i
< rdev
->desc
->n_linear_ranges
; i
++) {
442 range
= &rdev
->desc
->linear_ranges
[i
];
444 ret
= linear_range_get_selector_high(range
, min_uV
, &sel
,
451 * Map back into a voltage to verify we're still in bounds.
452 * If we are not, then continue checking rest of the ranges.
454 voltage
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
455 if (voltage
>= min_uV
&& voltage
<= max_uV
)
459 if (i
== rdev
->desc
->n_linear_ranges
)
464 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range
);
467 * regulator_map_voltage_pickable_linear_range - map_voltage, pickable ranges
469 * @rdev: Regulator to operate on
470 * @min_uV: Lower bound for voltage
471 * @max_uV: Upper bound for voltage
473 * Drivers providing pickable linear_ranges in their descriptor can use
474 * this as their map_voltage() callback.
476 int regulator_map_voltage_pickable_linear_range(struct regulator_dev
*rdev
,
477 int min_uV
, int max_uV
)
479 const struct linear_range
*range
;
482 unsigned int selector
= 0;
484 if (!rdev
->desc
->n_linear_ranges
) {
485 BUG_ON(!rdev
->desc
->n_linear_ranges
);
489 for (i
= 0; i
< rdev
->desc
->n_linear_ranges
; i
++) {
494 range
= &rdev
->desc
->linear_ranges
[i
];
495 linear_max_uV
= linear_range_get_max_value(range
);
497 if (!(min_uV
<= linear_max_uV
&& max_uV
>= range
->min
)) {
498 selector
+= linear_range_values_in_range(range
);
502 ret
= linear_range_get_selector_high(range
, min_uV
, &sel
,
505 selector
+= linear_range_values_in_range(range
);
509 ret
= selector
+ sel
- range
->min_sel
;
511 voltage
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
514 * Map back into a voltage to verify we're still in bounds.
515 * We may have overlapping voltage ranges. Hence we don't
516 * exit but retry until we have checked all ranges.
518 if (voltage
< min_uV
|| voltage
> max_uV
)
519 selector
+= linear_range_values_in_range(range
);
524 if (i
== rdev
->desc
->n_linear_ranges
)
529 EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range
);
532 * regulator_desc_list_voltage_linear - List voltages with simple calculation
534 * @desc: Regulator desc for regulator which volatges are to be listed
535 * @selector: Selector to convert into a voltage
537 * Regulators with a simple linear mapping between voltages and
538 * selectors can set min_uV and uV_step in the regulator descriptor
539 * and then use this function prior regulator registration to list
540 * the voltages. This is useful when voltages need to be listed during
541 * device-tree parsing.
543 int regulator_desc_list_voltage_linear(const struct regulator_desc
*desc
,
544 unsigned int selector
)
546 if (selector
>= desc
->n_voltages
)
549 if (selector
< desc
->linear_min_sel
)
552 selector
-= desc
->linear_min_sel
;
554 return desc
->min_uV
+ (desc
->uV_step
* selector
);
556 EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear
);
559 * regulator_list_voltage_linear - List voltages with simple calculation
561 * @rdev: Regulator device
562 * @selector: Selector to convert into a voltage
564 * Regulators with a simple linear mapping between voltages and
565 * selectors can set min_uV and uV_step in the regulator descriptor
566 * and then use this function as their list_voltage() operation,
568 int regulator_list_voltage_linear(struct regulator_dev
*rdev
,
569 unsigned int selector
)
571 return regulator_desc_list_voltage_linear(rdev
->desc
, selector
);
573 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear
);
576 * regulator_list_voltage_pickable_linear_range - pickable range list voltages
578 * @rdev: Regulator device
579 * @selector: Selector to convert into a voltage
581 * list_voltage() operation, intended to be used by drivers utilizing pickable
584 int regulator_list_voltage_pickable_linear_range(struct regulator_dev
*rdev
,
585 unsigned int selector
)
587 const struct linear_range
*range
;
589 unsigned int all_sels
= 0;
591 if (!rdev
->desc
->n_linear_ranges
) {
592 BUG_ON(!rdev
->desc
->n_linear_ranges
);
596 for (i
= 0; i
< rdev
->desc
->n_linear_ranges
; i
++) {
597 unsigned int sel_indexes
;
599 range
= &rdev
->desc
->linear_ranges
[i
];
601 sel_indexes
= linear_range_values_in_range(range
) - 1;
603 if (all_sels
+ sel_indexes
>= selector
) {
604 selector
-= all_sels
;
606 * As we see here, pickable ranges work only as
607 * long as the first selector for each pickable
608 * range is 0, and the each subsequent range for
609 * this 'pick' follow immediately at next unused
610 * selector (Eg. there is no gaps between ranges).
611 * I think this is fine but it probably should be
612 * documented. OTOH, whole pickable range stuff
613 * might benefit from some documentation
615 return range
->min
+ (range
->step
* selector
);
618 all_sels
+= (sel_indexes
+ 1);
623 EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range
);
626 * regulator_desc_list_voltage_linear_range - List voltages for linear ranges
628 * @desc: Regulator desc for regulator which volatges are to be listed
629 * @selector: Selector to convert into a voltage
631 * Regulators with a series of simple linear mappings between voltages
632 * and selectors who have set linear_ranges in the regulator descriptor
633 * can use this function prior regulator registration to list voltages.
634 * This is useful when voltages need to be listed during device-tree
637 int regulator_desc_list_voltage_linear_range(const struct regulator_desc
*desc
,
638 unsigned int selector
)
643 BUG_ON(!desc
->n_linear_ranges
);
645 ret
= linear_range_get_value_array(desc
->linear_ranges
,
646 desc
->n_linear_ranges
, selector
,
653 EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range
);
656 * regulator_list_voltage_linear_range - List voltages for linear ranges
658 * @rdev: Regulator device
659 * @selector: Selector to convert into a voltage
661 * Regulators with a series of simple linear mappings between voltages
662 * and selectors can set linear_ranges in the regulator descriptor and
663 * then use this function as their list_voltage() operation,
665 int regulator_list_voltage_linear_range(struct regulator_dev
*rdev
,
666 unsigned int selector
)
668 return regulator_desc_list_voltage_linear_range(rdev
->desc
, selector
);
670 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range
);
673 * regulator_list_voltage_table - List voltages with table based mapping
675 * @rdev: Regulator device
676 * @selector: Selector to convert into a voltage
678 * Regulators with table based mapping between voltages and
679 * selectors can set volt_table in the regulator descriptor
680 * and then use this function as their list_voltage() operation.
682 int regulator_list_voltage_table(struct regulator_dev
*rdev
,
683 unsigned int selector
)
685 if (!rdev
->desc
->volt_table
) {
686 BUG_ON(!rdev
->desc
->volt_table
);
690 if (selector
>= rdev
->desc
->n_voltages
)
692 if (selector
< rdev
->desc
->linear_min_sel
)
695 return rdev
->desc
->volt_table
[selector
];
697 EXPORT_SYMBOL_GPL(regulator_list_voltage_table
);
700 * regulator_set_bypass_regmap - Default set_bypass() using regmap
702 * @rdev: device to operate on.
703 * @enable: state to set.
705 int regulator_set_bypass_regmap(struct regulator_dev
*rdev
, bool enable
)
710 val
= rdev
->desc
->bypass_val_on
;
712 val
= rdev
->desc
->bypass_mask
;
714 val
= rdev
->desc
->bypass_val_off
;
717 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->bypass_reg
,
718 rdev
->desc
->bypass_mask
, val
);
720 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap
);
723 * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
725 * @rdev: device to operate on.
727 int regulator_set_soft_start_regmap(struct regulator_dev
*rdev
)
731 val
= rdev
->desc
->soft_start_val_on
;
733 val
= rdev
->desc
->soft_start_mask
;
735 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->soft_start_reg
,
736 rdev
->desc
->soft_start_mask
, val
);
738 EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap
);
741 * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
743 * @rdev: device to operate on.
745 int regulator_set_pull_down_regmap(struct regulator_dev
*rdev
)
749 val
= rdev
->desc
->pull_down_val_on
;
751 val
= rdev
->desc
->pull_down_mask
;
753 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->pull_down_reg
,
754 rdev
->desc
->pull_down_mask
, val
);
756 EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap
);
759 * regulator_get_bypass_regmap - Default get_bypass() using regmap
761 * @rdev: device to operate on.
762 * @enable: current state.
764 int regulator_get_bypass_regmap(struct regulator_dev
*rdev
, bool *enable
)
767 unsigned int val_on
= rdev
->desc
->bypass_val_on
;
770 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->bypass_reg
, &val
);
775 val_on
= rdev
->desc
->bypass_mask
;
777 *enable
= (val
& rdev
->desc
->bypass_mask
) == val_on
;
781 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap
);
784 * regulator_set_active_discharge_regmap - Default set_active_discharge()
787 * @rdev: device to operate on.
788 * @enable: state to set, 0 to disable and 1 to enable.
790 int regulator_set_active_discharge_regmap(struct regulator_dev
*rdev
,
796 val
= rdev
->desc
->active_discharge_on
;
798 val
= rdev
->desc
->active_discharge_off
;
800 return regmap_update_bits(rdev
->regmap
,
801 rdev
->desc
->active_discharge_reg
,
802 rdev
->desc
->active_discharge_mask
, val
);
804 EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap
);
807 * regulator_set_current_limit_regmap - set_current_limit for regmap users
809 * @rdev: regulator to operate on
810 * @min_uA: Lower bound for current limit
811 * @max_uA: Upper bound for current limit
813 * Regulators that use regmap for their register I/O can set curr_table,
814 * csel_reg and csel_mask fields in their descriptor and then use this
815 * as their set_current_limit operation, saving some code.
817 int regulator_set_current_limit_regmap(struct regulator_dev
*rdev
,
818 int min_uA
, int max_uA
)
820 unsigned int n_currents
= rdev
->desc
->n_current_limits
;
826 if (rdev
->desc
->curr_table
) {
827 const unsigned int *curr_table
= rdev
->desc
->curr_table
;
828 bool ascend
= curr_table
[n_currents
- 1] > curr_table
[0];
830 /* search for closest to maximum */
832 for (i
= n_currents
- 1; i
>= 0; i
--) {
833 if (min_uA
<= curr_table
[i
] &&
834 curr_table
[i
] <= max_uA
) {
840 for (i
= 0; i
< n_currents
; i
++) {
841 if (min_uA
<= curr_table
[i
] &&
842 curr_table
[i
] <= max_uA
) {
853 sel
<<= ffs(rdev
->desc
->csel_mask
) - 1;
855 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->csel_reg
,
856 rdev
->desc
->csel_mask
, sel
);
858 EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap
);
861 * regulator_get_current_limit_regmap - get_current_limit for regmap users
863 * @rdev: regulator to operate on
865 * Regulators that use regmap for their register I/O can set the
866 * csel_reg and csel_mask fields in their descriptor and then use this
867 * as their get_current_limit operation, saving some code.
869 int regulator_get_current_limit_regmap(struct regulator_dev
*rdev
)
874 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->csel_reg
, &val
);
878 val
&= rdev
->desc
->csel_mask
;
879 val
>>= ffs(rdev
->desc
->csel_mask
) - 1;
881 if (rdev
->desc
->curr_table
) {
882 if (val
>= rdev
->desc
->n_current_limits
)
885 return rdev
->desc
->curr_table
[val
];
890 EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap
);
893 * regulator_bulk_set_supply_names - initialize the 'supply' fields in an array
894 * of regulator_bulk_data structs
896 * @consumers: array of regulator_bulk_data entries to initialize
897 * @supply_names: array of supply name strings
898 * @num_supplies: number of supply names to initialize
900 * Note: the 'consumers' array must be the size of 'num_supplies'.
902 void regulator_bulk_set_supply_names(struct regulator_bulk_data
*consumers
,
903 const char *const *supply_names
,
904 unsigned int num_supplies
)
908 for (i
= 0; i
< num_supplies
; i
++)
909 consumers
[i
].supply
= supply_names
[i
];
911 EXPORT_SYMBOL_GPL(regulator_bulk_set_supply_names
);
914 * regulator_is_equal - test whether two regulators are the same
916 * @reg1: first regulator to operate on
917 * @reg2: second regulator to operate on
919 bool regulator_is_equal(struct regulator
*reg1
, struct regulator
*reg2
)
921 return reg1
->rdev
== reg2
->rdev
;
923 EXPORT_SYMBOL_GPL(regulator_is_equal
);
926 * regulator_find_closest_bigger - helper to find offset in ramp delay table
928 * @target: targeted ramp_delay
929 * @table: table with supported ramp delays
930 * @num_sel: number of entries in the table
931 * @sel: Pointer to store table offset
933 * This is the internal helper used by regulator_set_ramp_delay_regmap to
934 * map ramp delay to register value. It should only be used directly if
935 * regulator_set_ramp_delay_regmap cannot handle a specific device setup
936 * (e.g. because the value is split over multiple registers).
938 int regulator_find_closest_bigger(unsigned int target
, const unsigned int *table
,
939 unsigned int num_sel
, unsigned int *sel
)
941 unsigned int s
, tmp
, max
, maxsel
= 0;
946 for (s
= 0; s
< num_sel
; s
++) {
947 if (table
[s
] > max
) {
951 if (table
[s
] >= target
) {
952 if (!found
|| table
[s
] - target
< tmp
- target
) {
969 EXPORT_SYMBOL_GPL(regulator_find_closest_bigger
);
972 * regulator_set_ramp_delay_regmap - set_ramp_delay() helper
974 * @rdev: regulator to operate on
975 * @ramp_delay: ramp-rate value given in units V/S (uV/uS)
977 * Regulators that use regmap for their register I/O can set the ramp_reg
978 * and ramp_mask fields in their descriptor and then use this as their
979 * set_ramp_delay operation, saving some code.
981 int regulator_set_ramp_delay_regmap(struct regulator_dev
*rdev
, int ramp_delay
)
986 if (WARN_ON(!rdev
->desc
->n_ramp_values
|| !rdev
->desc
->ramp_delay_table
))
989 ret
= regulator_find_closest_bigger(ramp_delay
, rdev
->desc
->ramp_delay_table
,
990 rdev
->desc
->n_ramp_values
, &sel
);
993 dev_warn(rdev_get_dev(rdev
),
994 "Can't set ramp-delay %u, setting %u\n", ramp_delay
,
995 rdev
->desc
->ramp_delay_table
[sel
]);
998 sel
<<= ffs(rdev
->desc
->ramp_mask
) - 1;
1000 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->ramp_reg
,
1001 rdev
->desc
->ramp_mask
, sel
);
1003 EXPORT_SYMBOL_GPL(regulator_set_ramp_delay_regmap
);