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/kernel.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>
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)
32 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
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;
43 if (rdev->desc->enable_val)
44 return val == rdev->desc->enable_val;
48 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
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)
63 if (rdev->desc->enable_is_inverted) {
64 val = rdev->desc->disable_val;
66 val = rdev->desc->enable_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);
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)
89 if (rdev->desc->enable_is_inverted) {
90 val = rdev->desc->enable_val;
92 val = rdev->desc->enable_mask;
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,
107 if (!rdev->desc->linear_range_selectors)
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)
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)
135 unsigned int voltages_in_range = 0;
137 if (!rdev->desc->linear_ranges)
140 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
144 ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val);
148 val &= rdev->desc->vsel_mask;
149 val >>= ffs(rdev->desc->vsel_mask) - 1;
151 range = regulator_range_selector_to_index(rdev, r_val);
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,
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)
186 sel -= voltages_in_range;
189 if (i == rdev->desc->n_linear_ranges)
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);
203 ret = regmap_update_bits(rdev->regmap,
204 rdev->desc->vsel_range_reg,
205 rdev->desc->vsel_range_mask, range);
209 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
210 rdev->desc->vsel_mask, sel);
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);
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)
238 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
242 val &= rdev->desc->vsel_mask;
243 val >>= ffs(rdev->desc->vsel_mask) - 1;
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)
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);
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);
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;
297 /* Find the smallest voltage that falls within the specified
300 for (i = 0; i < rdev->desc->n_voltages; i++) {
301 ret = rdev->desc->ops->list_voltage(rdev, i);
305 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
311 if (best_val != INT_MAX)
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)
333 for (i = 0; i < rdev->desc->n_voltages; i++) {
334 ret = rdev->desc->ops->list_voltage(rdev, i);
341 if (ret >= min_uV && ret <= max_uV)
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)
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)
372 if (!rdev->desc->uV_step) {
373 BUG_ON(!rdev->desc->uV_step);
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);
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)
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;
412 if (!rdev->desc->n_linear_ranges) {
413 BUG_ON(!rdev->desc->n_linear_ranges);
417 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
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))
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) {
434 ret = DIV_ROUND_UP(min_uV - range->min_uV,
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)
451 if (i == rdev->desc->n_linear_ranges)
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;
474 unsigned int selector = 0;
476 if (!rdev->desc->n_linear_ranges) {
477 BUG_ON(!rdev->desc->n_linear_ranges);
481 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
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);
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) {
500 ret = DIV_ROUND_UP(min_uV - range->min_uV,
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);
521 if (i == rdev->desc->n_linear_ranges)
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)
543 if (selector < rdev->desc->linear_min_sel)
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
561 int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
562 unsigned int selector)
564 const struct regulator_linear_range *range;
566 unsigned int all_sels = 0;
568 if (!rdev->desc->n_linear_ranges) {
569 BUG_ON(!rdev->desc->n_linear_ranges);
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);
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
604 int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
605 unsigned int selector)
607 const struct regulator_linear_range *range;
610 if (!desc->n_linear_ranges) {
611 BUG_ON(!desc->n_linear_ranges);
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))
622 selector -= range->min_sel;
624 return range->min_uV + (range->uV_step * selector);
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);
666 if (selector >= rdev->desc->n_voltages)
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)
684 val = rdev->desc->bypass_val_on;
686 val = rdev->desc->bypass_mask;
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)
705 val = rdev->desc->soft_start_val_on;
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)
723 val = rdev->desc->pull_down_val_on;
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)
741 unsigned int val_on = rdev->desc->bypass_val_on;
744 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
749 val_on = rdev->desc->bypass_mask;
751 *enable = (val & rdev->desc->bypass_mask) == val_on;
755 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
758 * regulator_set_active_discharge_regmap - Default set_active_discharge()
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,
770 val = rdev->desc->active_discharge_on;
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;
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 */
806 for (i = n_currents - 1; i >= 0; i--) {
807 if (min_uA <= curr_table[i] &&
808 curr_table[i] <= max_uA) {
814 for (i = 0; i < n_currents; i++) {
815 if (min_uA <= curr_table[i] &&
816 curr_table[i] <= max_uA) {
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)
848 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
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)
859 return rdev->desc->curr_table[val];
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)
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);