Skip to content

Commit a32e0c7

Browse files
AxelLinbroonie
authored andcommitted
regulator: core: Add set/get_current_limit helpers for regmap users
By setting curr_table, n_current_limits, csel_reg and csel_mask, the regmap users can use regulator_set_current_limit_regmap and regulator_get_current_limit_regmap for set/get_current_limit callbacks. Signed-off-by: Axel Lin <axel.lin@ingics.com> Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 35d838f commit a32e0c7

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

‎drivers/regulator/helpers.c‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,89 @@ int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
780780
rdev->desc->active_discharge_mask, val);
781781
}
782782
EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);
783+
784+
/**
785+
* regulator_set_current_limit_regmap - set_current_limit for regmap users
786+
*
787+
* @rdev: regulator to operate on
788+
* @min_uA: Lower bound for current limit
789+
* @max_uA: Upper bound for current limit
790+
*
791+
* Regulators that use regmap for their register I/O can set curr_table,
792+
* csel_reg and csel_mask fields in their descriptor and then use this
793+
* as their set_current_limit operation, saving some code.
794+
*/
795+
int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
796+
int min_uA, int max_uA)
797+
{
798+
unsigned int n_currents = rdev->desc->n_current_limits;
799+
int i, sel = -1;
800+
801+
if (n_currents == 0)
802+
return -EINVAL;
803+
804+
if (rdev->desc->curr_table) {
805+
const unsigned int *curr_table = rdev->desc->curr_table;
806+
bool ascend = curr_table[n_currents - 1] > curr_table[0];
807+
808+
/* search for closest to maximum */
809+
if (ascend) {
810+
for (i = n_currents - 1; i >= 0; i--) {
811+
if (min_uA <= curr_table[i] &&
812+
curr_table[i] <= max_uA) {
813+
sel = i;
814+
break;
815+
}
816+
}
817+
} else {
818+
for (i = 0; i < n_currents; i++) {
819+
if (min_uA <= curr_table[i] &&
820+
curr_table[i] <= max_uA) {
821+
sel = i;
822+
break;
823+
}
824+
}
825+
}
826+
}
827+
828+
if (sel < 0)
829+
return -EINVAL;
830+
831+
sel <<= ffs(rdev->desc->csel_mask) - 1;
832+
833+
return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
834+
rdev->desc->csel_mask, sel);
835+
}
836+
EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap);
837+
838+
/**
839+
* regulator_get_current_limit_regmap - get_current_limit for regmap users
840+
*
841+
* @rdev: regulator to operate on
842+
*
843+
* Regulators that use regmap for their register I/O can set the
844+
* csel_reg and csel_mask fields in their descriptor and then use this
845+
* as their get_current_limit operation, saving some code.
846+
*/
847+
int regulator_get_current_limit_regmap(struct regulator_dev *rdev)
848+
{
849+
unsigned int val;
850+
int ret;
851+
852+
ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
853+
if (ret != 0)
854+
return ret;
855+
856+
val &= rdev->desc->csel_mask;
857+
val >>= ffs(rdev->desc->csel_mask) - 1;
858+
859+
if (rdev->desc->curr_table) {
860+
if (val >= rdev->desc->n_current_limits)
861+
return -EINVAL;
862+
863+
return rdev->desc->curr_table[val];
864+
}
865+
866+
return -EINVAL;
867+
}
868+
EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap);

‎include/linux/regulator/driver.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ enum regulator_type {
264264
* @continuous_voltage_range: Indicates if the regulator can set any
265265
* voltage within constrains range.
266266
* @n_voltages: Number of selectors available for ops.list_voltage().
267+
* @n_current_limits: Number of selectors available for current limits
267268
*
268269
* @min_uV: Voltage given by the lowest selector (if linear mapping)
269270
* @uV_step: Voltage increase with each selector (if linear mapping)
@@ -278,6 +279,7 @@ enum regulator_type {
278279
* @n_linear_ranges: Number of entries in the @linear_ranges (and in
279280
* linear_range_selectors if used) table(s).
280281
* @volt_table: Voltage mapping table (if table based mapping)
282+
* @curr_table: Current limit mapping table (if table based mapping)
281283
*
282284
* @vsel_range_reg: Register for range selector when using pickable ranges
283285
* and regulator_regmap_X_voltage_X_pickable functions.
@@ -333,6 +335,7 @@ struct regulator_desc {
333335
int id;
334336
unsigned int continuous_voltage_range:1;
335337
unsigned n_voltages;
338+
unsigned int n_current_limits;
336339
const struct regulator_ops *ops;
337340
int irq;
338341
enum regulator_type type;
@@ -351,6 +354,7 @@ struct regulator_desc {
351354
int n_linear_ranges;
352355

353356
const unsigned int *volt_table;
357+
const unsigned int *curr_table;
354358

355359
unsigned int vsel_range_reg;
356360
unsigned int vsel_range_mask;
@@ -534,6 +538,9 @@ int regulator_set_pull_down_regmap(struct regulator_dev *rdev);
534538

535539
int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
536540
bool enable);
541+
int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
542+
int min_uA, int max_uA);
543+
int regulator_get_current_limit_regmap(struct regulator_dev *rdev);
537544
void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data);
538545

539546
void regulator_lock(struct regulator_dev *rdev);

0 commit comments

Comments
 (0)