2 * helpers.c -- Voltage/Current Regulator framework helper functions.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/module.h>
23 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
25 * @rdev: regulator to operate on
27 * Regulators that use regmap for their register I/O can set the
28 * enable_reg and enable_mask fields in their descriptor and then use
29 * this as their is_enabled operation, saving some code.
31 int regulator_is_enabled_regmap(struct regulator_dev
*rdev
)
36 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->enable_reg
, &val
);
40 val
&= rdev
->desc
->enable_mask
;
42 if (rdev
->desc
->enable_is_inverted
) {
43 if (rdev
->desc
->enable_val
)
44 return val
!= rdev
->desc
->enable_val
;
47 if (rdev
->desc
->enable_val
)
48 return val
== rdev
->desc
->enable_val
;
52 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap
);
55 * regulator_enable_regmap - standard enable() for regmap users
57 * @rdev: regulator to operate on
59 * Regulators that use regmap for their register I/O can set the
60 * enable_reg and enable_mask fields in their descriptor and then use
61 * this as their enable() operation, saving some code.
63 int regulator_enable_regmap(struct regulator_dev
*rdev
)
67 if (rdev
->desc
->enable_is_inverted
) {
68 val
= rdev
->desc
->disable_val
;
70 val
= rdev
->desc
->enable_val
;
72 val
= rdev
->desc
->enable_mask
;
75 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
76 rdev
->desc
->enable_mask
, val
);
78 EXPORT_SYMBOL_GPL(regulator_enable_regmap
);
81 * regulator_disable_regmap - standard disable() for regmap users
83 * @rdev: regulator to operate on
85 * Regulators that use regmap for their register I/O can set the
86 * enable_reg and enable_mask fields in their descriptor and then use
87 * this as their disable() operation, saving some code.
89 int regulator_disable_regmap(struct regulator_dev
*rdev
)
93 if (rdev
->desc
->enable_is_inverted
) {
94 val
= rdev
->desc
->enable_val
;
96 val
= rdev
->desc
->enable_mask
;
98 val
= rdev
->desc
->disable_val
;
101 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
102 rdev
->desc
->enable_mask
, val
);
104 EXPORT_SYMBOL_GPL(regulator_disable_regmap
);
107 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
109 * @rdev: regulator to operate on
111 * Regulators that use regmap for their register I/O can set the
112 * vsel_reg and vsel_mask fields in their descriptor and then use this
113 * as their get_voltage_vsel operation, saving some code.
115 int regulator_get_voltage_sel_regmap(struct regulator_dev
*rdev
)
120 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->vsel_reg
, &val
);
124 val
&= rdev
->desc
->vsel_mask
;
125 val
>>= ffs(rdev
->desc
->vsel_mask
) - 1;
129 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap
);
132 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
134 * @rdev: regulator to operate on
135 * @sel: Selector to set
137 * Regulators that use regmap for their register I/O can set the
138 * vsel_reg and vsel_mask fields in their descriptor and then use this
139 * as their set_voltage_vsel operation, saving some code.
141 int regulator_set_voltage_sel_regmap(struct regulator_dev
*rdev
, unsigned sel
)
145 sel
<<= ffs(rdev
->desc
->vsel_mask
) - 1;
147 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->vsel_reg
,
148 rdev
->desc
->vsel_mask
, sel
);
152 if (rdev
->desc
->apply_bit
)
153 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->apply_reg
,
154 rdev
->desc
->apply_bit
,
155 rdev
->desc
->apply_bit
);
158 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap
);
161 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
163 * @rdev: Regulator to operate on
164 * @min_uV: Lower bound for voltage
165 * @max_uV: Upper bound for voltage
167 * Drivers implementing set_voltage_sel() and list_voltage() can use
168 * this as their map_voltage() operation. It will find a suitable
169 * voltage by calling list_voltage() until it gets something in bounds
170 * for the requested voltages.
172 int regulator_map_voltage_iterate(struct regulator_dev
*rdev
,
173 int min_uV
, int max_uV
)
175 int best_val
= INT_MAX
;
179 /* Find the smallest voltage that falls within the specified
182 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
183 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
187 if (ret
< best_val
&& ret
>= min_uV
&& ret
<= max_uV
) {
193 if (best_val
!= INT_MAX
)
198 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate
);
201 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
203 * @rdev: Regulator to operate on
204 * @min_uV: Lower bound for voltage
205 * @max_uV: Upper bound for voltage
207 * Drivers that have ascendant voltage list can use this as their
208 * map_voltage() operation.
210 int regulator_map_voltage_ascend(struct regulator_dev
*rdev
,
211 int min_uV
, int max_uV
)
215 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
216 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
223 if (ret
>= min_uV
&& ret
<= max_uV
)
229 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend
);
232 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
234 * @rdev: Regulator to operate on
235 * @min_uV: Lower bound for voltage
236 * @max_uV: Upper bound for voltage
238 * Drivers providing min_uV and uV_step in their regulator_desc can
239 * use this as their map_voltage() operation.
241 int regulator_map_voltage_linear(struct regulator_dev
*rdev
,
242 int min_uV
, int max_uV
)
246 /* Allow uV_step to be 0 for fixed voltage */
247 if (rdev
->desc
->n_voltages
== 1 && rdev
->desc
->uV_step
== 0) {
248 if (min_uV
<= rdev
->desc
->min_uV
&& rdev
->desc
->min_uV
<= max_uV
)
254 if (!rdev
->desc
->uV_step
) {
255 BUG_ON(!rdev
->desc
->uV_step
);
259 if (min_uV
< rdev
->desc
->min_uV
)
260 min_uV
= rdev
->desc
->min_uV
;
262 ret
= DIV_ROUND_UP(min_uV
- rdev
->desc
->min_uV
, rdev
->desc
->uV_step
);
266 ret
+= rdev
->desc
->linear_min_sel
;
268 /* Map back into a voltage to verify we're still in bounds */
269 voltage
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
270 if (voltage
< min_uV
|| voltage
> max_uV
)
275 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear
);
278 * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
280 * @rdev: Regulator to operate on
281 * @min_uV: Lower bound for voltage
282 * @max_uV: Upper bound for voltage
284 * Drivers providing linear_ranges in their descriptor can use this as
285 * their map_voltage() callback.
287 int regulator_map_voltage_linear_range(struct regulator_dev
*rdev
,
288 int min_uV
, int max_uV
)
290 const struct regulator_linear_range
*range
;
294 if (!rdev
->desc
->n_linear_ranges
) {
295 BUG_ON(!rdev
->desc
->n_linear_ranges
);
299 for (i
= 0; i
< rdev
->desc
->n_linear_ranges
; i
++) {
302 range
= &rdev
->desc
->linear_ranges
[i
];
303 linear_max_uV
= range
->min_uV
+
304 (range
->max_sel
- range
->min_sel
) * range
->uV_step
;
306 if (!(min_uV
<= linear_max_uV
&& max_uV
>= range
->min_uV
))
309 if (min_uV
<= range
->min_uV
)
310 min_uV
= range
->min_uV
;
312 /* range->uV_step == 0 means fixed voltage range */
313 if (range
->uV_step
== 0) {
316 ret
= DIV_ROUND_UP(min_uV
- range
->min_uV
,
322 ret
+= range
->min_sel
;
327 if (i
== rdev
->desc
->n_linear_ranges
)
330 /* Map back into a voltage to verify we're still in bounds */
331 voltage
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
332 if (voltage
< min_uV
|| voltage
> max_uV
)
337 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range
);
340 * regulator_list_voltage_linear - List voltages with simple calculation
342 * @rdev: Regulator device
343 * @selector: Selector to convert into a voltage
345 * Regulators with a simple linear mapping between voltages and
346 * selectors can set min_uV and uV_step in the regulator descriptor
347 * and then use this function as their list_voltage() operation,
349 int regulator_list_voltage_linear(struct regulator_dev
*rdev
,
350 unsigned int selector
)
352 if (selector
>= rdev
->desc
->n_voltages
)
354 if (selector
< rdev
->desc
->linear_min_sel
)
357 selector
-= rdev
->desc
->linear_min_sel
;
359 return rdev
->desc
->min_uV
+ (rdev
->desc
->uV_step
* selector
);
361 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear
);
364 * regulator_list_voltage_linear_range - List voltages for linear ranges
366 * @rdev: Regulator device
367 * @selector: Selector to convert into a voltage
369 * Regulators with a series of simple linear mappings between voltages
370 * and selectors can set linear_ranges in the regulator descriptor and
371 * then use this function as their list_voltage() operation,
373 int regulator_list_voltage_linear_range(struct regulator_dev
*rdev
,
374 unsigned int selector
)
376 const struct regulator_linear_range
*range
;
379 if (!rdev
->desc
->n_linear_ranges
) {
380 BUG_ON(!rdev
->desc
->n_linear_ranges
);
384 for (i
= 0; i
< rdev
->desc
->n_linear_ranges
; i
++) {
385 range
= &rdev
->desc
->linear_ranges
[i
];
387 if (!(selector
>= range
->min_sel
&&
388 selector
<= range
->max_sel
))
391 selector
-= range
->min_sel
;
393 return range
->min_uV
+ (range
->uV_step
* selector
);
398 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range
);
401 * regulator_list_voltage_table - List voltages with table based mapping
403 * @rdev: Regulator device
404 * @selector: Selector to convert into a voltage
406 * Regulators with table based mapping between voltages and
407 * selectors can set volt_table in the regulator descriptor
408 * and then use this function as their list_voltage() operation.
410 int regulator_list_voltage_table(struct regulator_dev
*rdev
,
411 unsigned int selector
)
413 if (!rdev
->desc
->volt_table
) {
414 BUG_ON(!rdev
->desc
->volt_table
);
418 if (selector
>= rdev
->desc
->n_voltages
)
421 return rdev
->desc
->volt_table
[selector
];
423 EXPORT_SYMBOL_GPL(regulator_list_voltage_table
);
426 * regulator_set_bypass_regmap - Default set_bypass() using regmap
428 * @rdev: device to operate on.
429 * @enable: state to set.
431 int regulator_set_bypass_regmap(struct regulator_dev
*rdev
, bool enable
)
436 val
= rdev
->desc
->bypass_val_on
;
438 val
= rdev
->desc
->bypass_mask
;
440 val
= rdev
->desc
->bypass_val_off
;
443 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->bypass_reg
,
444 rdev
->desc
->bypass_mask
, val
);
446 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap
);
449 * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
451 * @rdev: device to operate on.
453 int regulator_set_soft_start_regmap(struct regulator_dev
*rdev
)
457 val
= rdev
->desc
->soft_start_val_on
;
459 val
= rdev
->desc
->soft_start_mask
;
461 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->soft_start_reg
,
462 rdev
->desc
->soft_start_mask
, val
);
464 EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap
);
467 * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
469 * @rdev: device to operate on.
471 int regulator_set_pull_down_regmap(struct regulator_dev
*rdev
)
475 val
= rdev
->desc
->pull_down_val_on
;
477 val
= rdev
->desc
->pull_down_mask
;
479 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->pull_down_reg
,
480 rdev
->desc
->pull_down_mask
, val
);
482 EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap
);
485 * regulator_get_bypass_regmap - Default get_bypass() using regmap
487 * @rdev: device to operate on.
488 * @enable: current state.
490 int regulator_get_bypass_regmap(struct regulator_dev
*rdev
, bool *enable
)
493 unsigned int val_on
= rdev
->desc
->bypass_val_on
;
496 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->bypass_reg
, &val
);
501 val_on
= rdev
->desc
->bypass_mask
;
503 *enable
= (val
& rdev
->desc
->bypass_mask
) == val_on
;
507 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap
);
510 * regulator_set_active_discharge_regmap - Default set_active_discharge()
513 * @rdev: device to operate on.
514 * @enable: state to set, 0 to disable and 1 to enable.
516 int regulator_set_active_discharge_regmap(struct regulator_dev
*rdev
,
522 val
= rdev
->desc
->active_discharge_on
;
524 val
= rdev
->desc
->active_discharge_off
;
526 return regmap_update_bits(rdev
->regmap
,
527 rdev
->desc
->active_discharge_reg
,
528 rdev
->desc
->active_discharge_mask
, val
);
530 EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap
);