Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2//
3// helpers.c -- Voltage/Current Regulator framework helper functions.
4//
5// Copyright 2007, 2008 Wolfson Microelectronics PLC.
6// Copyright 2008 SlimLogic Ltd.
7
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>
16
17#include "internal.h"
18
19/**
20 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
21 *
22 * @rdev: regulator to operate on
23 *
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.
27 */
28int regulator_is_enabled_regmap(struct regulator_dev *rdev)
29{
30 unsigned int val;
31 int ret;
32
33 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
34 if (ret != 0)
35 return ret;
36
37 val &= rdev->desc->enable_mask;
38
39 if (rdev->desc->enable_is_inverted) {
40 if (rdev->desc->enable_val)
41 return val != rdev->desc->enable_val;
42 return val == 0;
43 } else {
44 if (rdev->desc->enable_val)
45 return val == rdev->desc->enable_val;
46 return val != 0;
47 }
48}
49EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
50
51/**
52 * regulator_enable_regmap - standard enable() for regmap users
53 *
54 * @rdev: regulator to operate on
55 *
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.
59 */
60int regulator_enable_regmap(struct regulator_dev *rdev)
61{
62 unsigned int val;
63
64 if (rdev->desc->enable_is_inverted) {
65 val = rdev->desc->disable_val;
66 } else {
67 val = rdev->desc->enable_val;
68 if (!val)
69 val = rdev->desc->enable_mask;
70 }
71
72 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
73 rdev->desc->enable_mask, val);
74}
75EXPORT_SYMBOL_GPL(regulator_enable_regmap);
76
77/**
78 * regulator_disable_regmap - standard disable() for regmap users
79 *
80 * @rdev: regulator to operate on
81 *
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.
85 */
86int regulator_disable_regmap(struct regulator_dev *rdev)
87{
88 unsigned int val;
89
90 if (rdev->desc->enable_is_inverted) {
91 val = rdev->desc->enable_val;
92 if (!val)
93 val = rdev->desc->enable_mask;
94 } else {
95 val = rdev->desc->disable_val;
96 }
97
98 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
99 rdev->desc->enable_mask, val);
100}
101EXPORT_SYMBOL_GPL(regulator_disable_regmap);
102
103static int regulator_range_selector_to_index(struct regulator_dev *rdev,
104 unsigned int rval)
105{
106 int i;
107
108 if (!rdev->desc->linear_range_selectors_bitfield)
109 return -EINVAL;
110
111 rval &= rdev->desc->vsel_range_mask;
112 rval >>= ffs(rdev->desc->vsel_range_mask) - 1;
113
114 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
115 if (rdev->desc->linear_range_selectors_bitfield[i] == rval)
116 return i;
117 }
118 return -EINVAL;
119}
120
121/**
122 * regulator_get_voltage_sel_pickable_regmap - pickable range get_voltage_sel
123 *
124 * @rdev: regulator to operate on
125 *
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.
130 */
131int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev)
132{
133 unsigned int r_val;
134 int range;
135 unsigned int val;
136 int ret;
137 unsigned int voltages = 0;
138 const struct linear_range *r = rdev->desc->linear_ranges;
139
140 if (!r)
141 return -EINVAL;
142
143 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
144 if (ret != 0)
145 return ret;
146
147 ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val);
148 if (ret != 0)
149 return ret;
150
151 val &= rdev->desc->vsel_mask;
152 val >>= ffs(rdev->desc->vsel_mask) - 1;
153
154 range = regulator_range_selector_to_index(rdev, r_val);
155 if (range < 0)
156 return -EINVAL;
157
158 voltages = linear_range_values_in_range_array(r, range);
159
160 return val + voltages;
161}
162EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_pickable_regmap);
163
164/**
165 * regulator_set_voltage_sel_pickable_regmap - pickable range set_voltage_sel
166 *
167 * @rdev: regulator to operate on
168 * @sel: Selector to set
169 *
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.
174 */
175int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
176 unsigned int sel)
177{
178 unsigned int range;
179 int ret, i;
180 unsigned int voltages_in_range = 0;
181
182 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
183 const struct linear_range *r;
184
185 r = &rdev->desc->linear_ranges[i];
186 voltages_in_range = linear_range_values_in_range(r);
187
188 if (sel < voltages_in_range)
189 break;
190 sel -= voltages_in_range;
191 }
192
193 if (i == rdev->desc->n_linear_ranges)
194 return -EINVAL;
195
196 sel <<= ffs(rdev->desc->vsel_mask) - 1;
197 sel += rdev->desc->linear_ranges[i].min_sel;
198
199 range = rdev->desc->linear_range_selectors_bitfield[i];
200 range <<= ffs(rdev->desc->vsel_range_mask) - 1;
201
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);
207 } else {
208 ret = regmap_update_bits(rdev->regmap,
209 rdev->desc->vsel_range_reg,
210 rdev->desc->vsel_range_mask, range);
211 if (ret)
212 return ret;
213
214 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
215 rdev->desc->vsel_mask, sel);
216 }
217
218 if (ret)
219 return ret;
220
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);
225 return ret;
226}
227EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_pickable_regmap);
228
229/**
230 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
231 *
232 * @rdev: regulator to operate on
233 *
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.
237 */
238int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
239{
240 unsigned int val;
241 int ret;
242
243 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
244 if (ret != 0)
245 return ret;
246
247 val &= rdev->desc->vsel_mask;
248 val >>= ffs(rdev->desc->vsel_mask) - 1;
249
250 return val;
251}
252EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
253
254/**
255 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
256 *
257 * @rdev: regulator to operate on
258 * @sel: Selector to set
259 *
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.
263 */
264int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
265{
266 int ret;
267
268 sel <<= ffs(rdev->desc->vsel_mask) - 1;
269
270 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
271 rdev->desc->vsel_mask, sel);
272 if (ret)
273 return ret;
274
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);
279 return ret;
280}
281EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
282
283/**
284 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
285 *
286 * @rdev: Regulator to operate on
287 * @min_uV: Lower bound for voltage
288 * @max_uV: Upper bound for voltage
289 *
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.
294 */
295int regulator_map_voltage_iterate(struct regulator_dev *rdev,
296 int min_uV, int max_uV)
297{
298 int best_val = INT_MAX;
299 int selector = 0;
300 int i, ret;
301
302 /* Find the smallest voltage that falls within the specified
303 * range.
304 */
305 for (i = 0; i < rdev->desc->n_voltages; i++) {
306 ret = rdev->desc->ops->list_voltage(rdev, i);
307 if (ret < 0)
308 continue;
309
310 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
311 best_val = ret;
312 selector = i;
313 }
314 }
315
316 if (best_val != INT_MAX)
317 return selector;
318 else
319 return -EINVAL;
320}
321EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
322
323/**
324 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
325 *
326 * @rdev: Regulator to operate on
327 * @min_uV: Lower bound for voltage
328 * @max_uV: Upper bound for voltage
329 *
330 * Drivers that have ascendant voltage list can use this as their
331 * map_voltage() operation.
332 */
333int regulator_map_voltage_ascend(struct regulator_dev *rdev,
334 int min_uV, int max_uV)
335{
336 int i, ret;
337
338 for (i = 0; i < rdev->desc->n_voltages; i++) {
339 ret = rdev->desc->ops->list_voltage(rdev, i);
340 if (ret < 0)
341 continue;
342
343 if (ret > max_uV)
344 break;
345
346 if (ret >= min_uV && ret <= max_uV)
347 return i;
348 }
349
350 return -EINVAL;
351}
352EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
353
354/**
355 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
356 *
357 * @rdev: Regulator to operate on
358 * @min_uV: Lower bound for voltage
359 * @max_uV: Upper bound for voltage
360 *
361 * Drivers providing min_uV and uV_step in their regulator_desc can
362 * use this as their map_voltage() operation.
363 */
364int regulator_map_voltage_linear(struct regulator_dev *rdev,
365 int min_uV, int max_uV)
366{
367 int ret, voltage;
368
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)
372 return 0;
373 else
374 return -EINVAL;
375 }
376
377 if (!rdev->desc->uV_step) {
378 BUG_ON(!rdev->desc->uV_step);
379 return -EINVAL;
380 }
381
382 if (min_uV < rdev->desc->min_uV)
383 min_uV = rdev->desc->min_uV;
384
385 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
386 if (ret < 0)
387 return ret;
388
389 ret += rdev->desc->linear_min_sel;
390
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)
394 return -EINVAL;
395
396 return ret;
397}
398EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
399
400/**
401 * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
402 *
403 * @rdev: Regulator to operate on
404 * @min_uV: Lower bound for voltage
405 * @max_uV: Upper bound for voltage
406 *
407 * Drivers providing linear_ranges in their descriptor can use this as
408 * their map_voltage() callback.
409 */
410int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
411 int min_uV, int max_uV)
412{
413 const struct linear_range *range;
414 int ret = -EINVAL;
415 unsigned int sel;
416 bool found;
417 int voltage, i;
418
419 if (!rdev->desc->n_linear_ranges) {
420 BUG_ON(!rdev->desc->n_linear_ranges);
421 return -EINVAL;
422 }
423
424 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
425 range = &rdev->desc->linear_ranges[i];
426
427 ret = linear_range_get_selector_high(range, min_uV, &sel,
428 &found);
429 if (ret)
430 continue;
431 ret = sel;
432
433 /*
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.
436 */
437 voltage = rdev->desc->ops->list_voltage(rdev, sel);
438 if (voltage >= min_uV && voltage <= max_uV)
439 break;
440 }
441
442 if (i == rdev->desc->n_linear_ranges)
443 return -EINVAL;
444
445 return ret;
446}
447EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
448
449/**
450 * regulator_map_voltage_pickable_linear_range - map_voltage, pickable ranges
451 *
452 * @rdev: Regulator to operate on
453 * @min_uV: Lower bound for voltage
454 * @max_uV: Upper bound for voltage
455 *
456 * Drivers providing pickable linear_ranges in their descriptor can use
457 * this as their map_voltage() callback.
458 */
459int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
460 int min_uV, int max_uV)
461{
462 const struct linear_range *range;
463 int ret = -EINVAL;
464 int voltage, i;
465 unsigned int selector = 0;
466
467 if (!rdev->desc->n_linear_ranges) {
468 BUG_ON(!rdev->desc->n_linear_ranges);
469 return -EINVAL;
470 }
471
472 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
473 int linear_max_uV;
474 bool found;
475 unsigned int sel;
476
477 range = &rdev->desc->linear_ranges[i];
478 linear_max_uV = linear_range_get_max_value(range);
479
480 if (!(min_uV <= linear_max_uV && max_uV >= range->min)) {
481 selector += linear_range_values_in_range(range);
482 continue;
483 }
484
485 ret = linear_range_get_selector_high(range, min_uV, &sel,
486 &found);
487 if (ret) {
488 selector += linear_range_values_in_range(range);
489 continue;
490 }
491
492 ret = selector + sel - range->min_sel;
493
494 voltage = rdev->desc->ops->list_voltage(rdev, ret);
495
496 /*
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.
500 */
501 if (voltage < min_uV || voltage > max_uV)
502 selector += linear_range_values_in_range(range);
503 else
504 break;
505 }
506
507 if (i == rdev->desc->n_linear_ranges)
508 return -EINVAL;
509
510 return ret;
511}
512EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range);
513
514/**
515 * regulator_desc_list_voltage_linear - List voltages with simple calculation
516 *
517 * @desc: Regulator desc for regulator which volatges are to be listed
518 * @selector: Selector to convert into a voltage
519 *
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.
525 */
526int regulator_desc_list_voltage_linear(const struct regulator_desc *desc,
527 unsigned int selector)
528{
529 if (selector >= desc->n_voltages)
530 return -EINVAL;
531
532 if (selector < desc->linear_min_sel)
533 return 0;
534
535 selector -= desc->linear_min_sel;
536
537 return desc->min_uV + (desc->uV_step * selector);
538}
539EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear);
540
541/**
542 * regulator_list_voltage_linear - List voltages with simple calculation
543 *
544 * @rdev: Regulator device
545 * @selector: Selector to convert into a voltage
546 *
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,
550 */
551int regulator_list_voltage_linear(struct regulator_dev *rdev,
552 unsigned int selector)
553{
554 return regulator_desc_list_voltage_linear(rdev->desc, selector);
555}
556EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
557
558/**
559 * regulator_list_voltage_pickable_linear_range - pickable range list voltages
560 *
561 * @rdev: Regulator device
562 * @selector: Selector to convert into a voltage
563 *
564 * list_voltage() operation, intended to be used by drivers utilizing pickable
565 * ranges helpers.
566 */
567int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
568 unsigned int selector)
569{
570 const struct linear_range *range;
571 int i;
572 unsigned int all_sels = 0;
573
574 if (!rdev->desc->n_linear_ranges) {
575 BUG_ON(!rdev->desc->n_linear_ranges);
576 return -EINVAL;
577 }
578
579 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
580 unsigned int sel_indexes;
581
582 range = &rdev->desc->linear_ranges[i];
583
584 sel_indexes = linear_range_values_in_range(range) - 1;
585
586 if (all_sels + sel_indexes >= selector) {
587 selector -= all_sels;
588 /*
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
597 */
598 return range->min + (range->step * selector);
599 }
600
601 all_sels += (sel_indexes + 1);
602 }
603
604 return -EINVAL;
605}
606EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range);
607
608/**
609 * regulator_desc_list_voltage_linear_range - List voltages for linear ranges
610 *
611 * @desc: Regulator desc for regulator which volatges are to be listed
612 * @selector: Selector to convert into a voltage
613 *
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
618 * parsing.
619 */
620int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
621 unsigned int selector)
622{
623 unsigned int val;
624 int ret;
625
626 BUG_ON(!desc->n_linear_ranges);
627
628 ret = linear_range_get_value_array(desc->linear_ranges,
629 desc->n_linear_ranges, selector,
630 &val);
631 if (ret)
632 return ret;
633
634 return val;
635}
636EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range);
637
638/**
639 * regulator_list_voltage_linear_range - List voltages for linear ranges
640 *
641 * @rdev: Regulator device
642 * @selector: Selector to convert into a voltage
643 *
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,
647 */
648int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
649 unsigned int selector)
650{
651 return regulator_desc_list_voltage_linear_range(rdev->desc, selector);
652}
653EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
654
655/**
656 * regulator_list_voltage_table - List voltages with table based mapping
657 *
658 * @rdev: Regulator device
659 * @selector: Selector to convert into a voltage
660 *
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.
664 */
665int regulator_list_voltage_table(struct regulator_dev *rdev,
666 unsigned int selector)
667{
668 if (!rdev->desc->volt_table) {
669 BUG_ON(!rdev->desc->volt_table);
670 return -EINVAL;
671 }
672
673 if (selector >= rdev->desc->n_voltages)
674 return -EINVAL;
675 if (selector < rdev->desc->linear_min_sel)
676 return 0;
677
678 return rdev->desc->volt_table[selector];
679}
680EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
681
682/**
683 * regulator_set_bypass_regmap - Default set_bypass() using regmap
684 *
685 * @rdev: device to operate on.
686 * @enable: state to set.
687 */
688int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
689{
690 unsigned int val;
691
692 if (enable) {
693 val = rdev->desc->bypass_val_on;
694 if (!val)
695 val = rdev->desc->bypass_mask;
696 } else {
697 val = rdev->desc->bypass_val_off;
698 }
699
700 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
701 rdev->desc->bypass_mask, val);
702}
703EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
704
705/**
706 * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
707 *
708 * @rdev: device to operate on.
709 */
710int regulator_set_soft_start_regmap(struct regulator_dev *rdev)
711{
712 unsigned int val;
713
714 val = rdev->desc->soft_start_val_on;
715 if (!val)
716 val = rdev->desc->soft_start_mask;
717
718 return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg,
719 rdev->desc->soft_start_mask, val);
720}
721EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap);
722
723/**
724 * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
725 *
726 * @rdev: device to operate on.
727 */
728int regulator_set_pull_down_regmap(struct regulator_dev *rdev)
729{
730 unsigned int val;
731
732 val = rdev->desc->pull_down_val_on;
733 if (!val)
734 val = rdev->desc->pull_down_mask;
735
736 return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg,
737 rdev->desc->pull_down_mask, val);
738}
739EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap);
740
741/**
742 * regulator_get_bypass_regmap - Default get_bypass() using regmap
743 *
744 * @rdev: device to operate on.
745 * @enable: current state.
746 */
747int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
748{
749 unsigned int val;
750 unsigned int val_on = rdev->desc->bypass_val_on;
751 int ret;
752
753 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
754 if (ret != 0)
755 return ret;
756
757 if (!val_on)
758 val_on = rdev->desc->bypass_mask;
759
760 *enable = (val & rdev->desc->bypass_mask) == val_on;
761
762 return 0;
763}
764EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
765
766/**
767 * regulator_set_active_discharge_regmap - Default set_active_discharge()
768 * using regmap
769 *
770 * @rdev: device to operate on.
771 * @enable: state to set, 0 to disable and 1 to enable.
772 */
773int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
774 bool enable)
775{
776 unsigned int val;
777
778 if (enable)
779 val = rdev->desc->active_discharge_on;
780 else
781 val = rdev->desc->active_discharge_off;
782
783 return regmap_update_bits(rdev->regmap,
784 rdev->desc->active_discharge_reg,
785 rdev->desc->active_discharge_mask, val);
786}
787EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);
788
789/**
790 * regulator_set_current_limit_regmap - set_current_limit for regmap users
791 *
792 * @rdev: regulator to operate on
793 * @min_uA: Lower bound for current limit
794 * @max_uA: Upper bound for current limit
795 *
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.
799 */
800int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
801 int min_uA, int max_uA)
802{
803 unsigned int n_currents = rdev->desc->n_current_limits;
804 int i, sel = -1;
805
806 if (n_currents == 0)
807 return -EINVAL;
808
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];
812
813 /* search for closest to maximum */
814 if (ascend) {
815 for (i = n_currents - 1; i >= 0; i--) {
816 if (min_uA <= curr_table[i] &&
817 curr_table[i] <= max_uA) {
818 sel = i;
819 break;
820 }
821 }
822 } else {
823 for (i = 0; i < n_currents; i++) {
824 if (min_uA <= curr_table[i] &&
825 curr_table[i] <= max_uA) {
826 sel = i;
827 break;
828 }
829 }
830 }
831 }
832
833 if (sel < 0)
834 return -EINVAL;
835
836 sel <<= ffs(rdev->desc->csel_mask) - 1;
837
838 return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
839 rdev->desc->csel_mask, sel);
840}
841EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap);
842
843/**
844 * regulator_get_current_limit_regmap - get_current_limit for regmap users
845 *
846 * @rdev: regulator to operate on
847 *
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.
851 */
852int regulator_get_current_limit_regmap(struct regulator_dev *rdev)
853{
854 unsigned int val;
855 int ret;
856
857 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
858 if (ret != 0)
859 return ret;
860
861 val &= rdev->desc->csel_mask;
862 val >>= ffs(rdev->desc->csel_mask) - 1;
863
864 if (rdev->desc->curr_table) {
865 if (val >= rdev->desc->n_current_limits)
866 return -EINVAL;
867
868 return rdev->desc->curr_table[val];
869 }
870
871 return -EINVAL;
872}
873EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap);
874
875/**
876 * regulator_bulk_set_supply_names - initialize the 'supply' fields in an array
877 * of regulator_bulk_data structs
878 *
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
882 *
883 * Note: the 'consumers' array must be the size of 'num_supplies'.
884 */
885void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
886 const char *const *supply_names,
887 unsigned int num_supplies)
888{
889 unsigned int i;
890
891 for (i = 0; i < num_supplies; i++)
892 consumers[i].supply = supply_names[i];
893}
894EXPORT_SYMBOL_GPL(regulator_bulk_set_supply_names);
895
896/**
897 * regulator_is_equal - test whether two regulators are the same
898 *
899 * @reg1: first regulator to operate on
900 * @reg2: second regulator to operate on
901 */
902bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
903{
904 return reg1->rdev == reg2->rdev;
905}
906EXPORT_SYMBOL_GPL(regulator_is_equal);
907
908/**
909 * regulator_find_closest_bigger - helper to find offset in ramp delay table
910 *
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
915 *
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).
920 */
921int regulator_find_closest_bigger(unsigned int target, const unsigned int *table,
922 unsigned int num_sel, unsigned int *sel)
923{
924 unsigned int s, tmp, max, maxsel = 0;
925 bool found = false;
926
927 max = table[0];
928
929 for (s = 0; s < num_sel; s++) {
930 if (table[s] > max) {
931 max = table[s];
932 maxsel = s;
933 }
934 if (table[s] >= target) {
935 if (!found || table[s] - target < tmp - target) {
936 tmp = table[s];
937 *sel = s;
938 found = true;
939 if (tmp == target)
940 break;
941 }
942 }
943 }
944
945 if (!found) {
946 *sel = maxsel;
947 return -EINVAL;
948 }
949
950 return 0;
951}
952EXPORT_SYMBOL_GPL(regulator_find_closest_bigger);
953
954/**
955 * regulator_set_ramp_delay_regmap - set_ramp_delay() helper
956 *
957 * @rdev: regulator to operate on
958 * @ramp_delay: ramp-rate value given in units V/S (uV/uS)
959 *
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.
963 */
964int regulator_set_ramp_delay_regmap(struct regulator_dev *rdev, int ramp_delay)
965{
966 int ret;
967 unsigned int sel;
968
969 if (WARN_ON(!rdev->desc->n_ramp_values || !rdev->desc->ramp_delay_table))
970 return -EINVAL;
971
972 ret = regulator_find_closest_bigger(ramp_delay, rdev->desc->ramp_delay_table,
973 rdev->desc->n_ramp_values, &sel);
974
975 if (ret) {
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]);
979 }
980
981 sel <<= ffs(rdev->desc->ramp_mask) - 1;
982
983 return regmap_update_bits(rdev->regmap, rdev->desc->ramp_reg,
984 rdev->desc->ramp_mask, sel);
985}
986EXPORT_SYMBOL_GPL(regulator_set_ramp_delay_regmap);
1// SPDX-License-Identifier: GPL-2.0-or-later
2//
3// helpers.c -- Voltage/Current Regulator framework helper functions.
4//
5// Copyright 2007, 2008 Wolfson Microelectronics PLC.
6// Copyright 2008 SlimLogic Ltd.
7
8#include <linux/kernel.h>
9#include <linux/err.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>
15
16/**
17 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
18 *
19 * @rdev: regulator to operate on
20 *
21 * Regulators that use regmap for their register I/O can set the
22 * enable_reg and enable_mask fields in their descriptor and then use
23 * this as their is_enabled operation, saving some code.
24 */
25int regulator_is_enabled_regmap(struct regulator_dev *rdev)
26{
27 unsigned int val;
28 int ret;
29
30 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
31 if (ret != 0)
32 return ret;
33
34 val &= rdev->desc->enable_mask;
35
36 if (rdev->desc->enable_is_inverted) {
37 if (rdev->desc->enable_val)
38 return val != rdev->desc->enable_val;
39 return val == 0;
40 } else {
41 if (rdev->desc->enable_val)
42 return val == rdev->desc->enable_val;
43 return val != 0;
44 }
45}
46EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
47
48/**
49 * regulator_enable_regmap - standard enable() for regmap users
50 *
51 * @rdev: regulator to operate on
52 *
53 * Regulators that use regmap for their register I/O can set the
54 * enable_reg and enable_mask fields in their descriptor and then use
55 * this as their enable() operation, saving some code.
56 */
57int regulator_enable_regmap(struct regulator_dev *rdev)
58{
59 unsigned int val;
60
61 if (rdev->desc->enable_is_inverted) {
62 val = rdev->desc->disable_val;
63 } else {
64 val = rdev->desc->enable_val;
65 if (!val)
66 val = rdev->desc->enable_mask;
67 }
68
69 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
70 rdev->desc->enable_mask, val);
71}
72EXPORT_SYMBOL_GPL(regulator_enable_regmap);
73
74/**
75 * regulator_disable_regmap - standard disable() for regmap users
76 *
77 * @rdev: regulator to operate on
78 *
79 * Regulators that use regmap for their register I/O can set the
80 * enable_reg and enable_mask fields in their descriptor and then use
81 * this as their disable() operation, saving some code.
82 */
83int regulator_disable_regmap(struct regulator_dev *rdev)
84{
85 unsigned int val;
86
87 if (rdev->desc->enable_is_inverted) {
88 val = rdev->desc->enable_val;
89 if (!val)
90 val = rdev->desc->enable_mask;
91 } else {
92 val = rdev->desc->disable_val;
93 }
94
95 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
96 rdev->desc->enable_mask, val);
97}
98EXPORT_SYMBOL_GPL(regulator_disable_regmap);
99
100static int regulator_range_selector_to_index(struct regulator_dev *rdev,
101 unsigned int rval)
102{
103 int i;
104
105 if (!rdev->desc->linear_range_selectors)
106 return -EINVAL;
107
108 rval &= rdev->desc->vsel_range_mask;
109
110 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
111 if (rdev->desc->linear_range_selectors[i] == rval)
112 return i;
113 }
114 return -EINVAL;
115}
116
117/**
118 * regulator_get_voltage_sel_pickable_regmap - pickable range get_voltage_sel
119 *
120 * @rdev: regulator to operate on
121 *
122 * Regulators that use regmap for their register I/O and use pickable
123 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
124 * fields in their descriptor and then use this as their get_voltage_vsel
125 * operation, saving some code.
126 */
127int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev)
128{
129 unsigned int r_val;
130 int range;
131 unsigned int val;
132 int ret, i;
133 unsigned int voltages_in_range = 0;
134
135 if (!rdev->desc->linear_ranges)
136 return -EINVAL;
137
138 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
139 if (ret != 0)
140 return ret;
141
142 ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val);
143 if (ret != 0)
144 return ret;
145
146 val &= rdev->desc->vsel_mask;
147 val >>= ffs(rdev->desc->vsel_mask) - 1;
148
149 range = regulator_range_selector_to_index(rdev, r_val);
150 if (range < 0)
151 return -EINVAL;
152
153 for (i = 0; i < range; i++)
154 voltages_in_range += (rdev->desc->linear_ranges[i].max_sel -
155 rdev->desc->linear_ranges[i].min_sel) + 1;
156
157 return val + voltages_in_range;
158}
159EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_pickable_regmap);
160
161/**
162 * regulator_set_voltage_sel_pickable_regmap - pickable range set_voltage_sel
163 *
164 * @rdev: regulator to operate on
165 * @sel: Selector to set
166 *
167 * Regulators that use regmap for their register I/O and use pickable
168 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask
169 * fields in their descriptor and then use this as their set_voltage_vsel
170 * operation, saving some code.
171 */
172int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
173 unsigned int sel)
174{
175 unsigned int range;
176 int ret, i;
177 unsigned int voltages_in_range = 0;
178
179 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
180 voltages_in_range = (rdev->desc->linear_ranges[i].max_sel -
181 rdev->desc->linear_ranges[i].min_sel) + 1;
182 if (sel < voltages_in_range)
183 break;
184 sel -= voltages_in_range;
185 }
186
187 if (i == rdev->desc->n_linear_ranges)
188 return -EINVAL;
189
190 sel <<= ffs(rdev->desc->vsel_mask) - 1;
191 sel += rdev->desc->linear_ranges[i].min_sel;
192
193 range = rdev->desc->linear_range_selectors[i];
194
195 if (rdev->desc->vsel_reg == rdev->desc->vsel_range_reg) {
196 ret = regmap_update_bits(rdev->regmap,
197 rdev->desc->vsel_reg,
198 rdev->desc->vsel_range_mask |
199 rdev->desc->vsel_mask, sel | range);
200 } else {
201 ret = regmap_update_bits(rdev->regmap,
202 rdev->desc->vsel_range_reg,
203 rdev->desc->vsel_range_mask, range);
204 if (ret)
205 return ret;
206
207 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
208 rdev->desc->vsel_mask, sel);
209 }
210
211 if (ret)
212 return ret;
213
214 if (rdev->desc->apply_bit)
215 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
216 rdev->desc->apply_bit,
217 rdev->desc->apply_bit);
218 return ret;
219}
220EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_pickable_regmap);
221
222/**
223 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
224 *
225 * @rdev: regulator to operate on
226 *
227 * Regulators that use regmap for their register I/O can set the
228 * vsel_reg and vsel_mask fields in their descriptor and then use this
229 * as their get_voltage_vsel operation, saving some code.
230 */
231int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
232{
233 unsigned int val;
234 int ret;
235
236 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
237 if (ret != 0)
238 return ret;
239
240 val &= rdev->desc->vsel_mask;
241 val >>= ffs(rdev->desc->vsel_mask) - 1;
242
243 return val;
244}
245EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
246
247/**
248 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
249 *
250 * @rdev: regulator to operate on
251 * @sel: Selector to set
252 *
253 * Regulators that use regmap for their register I/O can set the
254 * vsel_reg and vsel_mask fields in their descriptor and then use this
255 * as their set_voltage_vsel operation, saving some code.
256 */
257int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
258{
259 int ret;
260
261 sel <<= ffs(rdev->desc->vsel_mask) - 1;
262
263 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
264 rdev->desc->vsel_mask, sel);
265 if (ret)
266 return ret;
267
268 if (rdev->desc->apply_bit)
269 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
270 rdev->desc->apply_bit,
271 rdev->desc->apply_bit);
272 return ret;
273}
274EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
275
276/**
277 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
278 *
279 * @rdev: Regulator to operate on
280 * @min_uV: Lower bound for voltage
281 * @max_uV: Upper bound for voltage
282 *
283 * Drivers implementing set_voltage_sel() and list_voltage() can use
284 * this as their map_voltage() operation. It will find a suitable
285 * voltage by calling list_voltage() until it gets something in bounds
286 * for the requested voltages.
287 */
288int regulator_map_voltage_iterate(struct regulator_dev *rdev,
289 int min_uV, int max_uV)
290{
291 int best_val = INT_MAX;
292 int selector = 0;
293 int i, ret;
294
295 /* Find the smallest voltage that falls within the specified
296 * range.
297 */
298 for (i = 0; i < rdev->desc->n_voltages; i++) {
299 ret = rdev->desc->ops->list_voltage(rdev, i);
300 if (ret < 0)
301 continue;
302
303 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
304 best_val = ret;
305 selector = i;
306 }
307 }
308
309 if (best_val != INT_MAX)
310 return selector;
311 else
312 return -EINVAL;
313}
314EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
315
316/**
317 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
318 *
319 * @rdev: Regulator to operate on
320 * @min_uV: Lower bound for voltage
321 * @max_uV: Upper bound for voltage
322 *
323 * Drivers that have ascendant voltage list can use this as their
324 * map_voltage() operation.
325 */
326int regulator_map_voltage_ascend(struct regulator_dev *rdev,
327 int min_uV, int max_uV)
328{
329 int i, ret;
330
331 for (i = 0; i < rdev->desc->n_voltages; i++) {
332 ret = rdev->desc->ops->list_voltage(rdev, i);
333 if (ret < 0)
334 continue;
335
336 if (ret > max_uV)
337 break;
338
339 if (ret >= min_uV && ret <= max_uV)
340 return i;
341 }
342
343 return -EINVAL;
344}
345EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
346
347/**
348 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
349 *
350 * @rdev: Regulator to operate on
351 * @min_uV: Lower bound for voltage
352 * @max_uV: Upper bound for voltage
353 *
354 * Drivers providing min_uV and uV_step in their regulator_desc can
355 * use this as their map_voltage() operation.
356 */
357int regulator_map_voltage_linear(struct regulator_dev *rdev,
358 int min_uV, int max_uV)
359{
360 int ret, voltage;
361
362 /* Allow uV_step to be 0 for fixed voltage */
363 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
364 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
365 return 0;
366 else
367 return -EINVAL;
368 }
369
370 if (!rdev->desc->uV_step) {
371 BUG_ON(!rdev->desc->uV_step);
372 return -EINVAL;
373 }
374
375 if (min_uV < rdev->desc->min_uV)
376 min_uV = rdev->desc->min_uV;
377
378 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
379 if (ret < 0)
380 return ret;
381
382 ret += rdev->desc->linear_min_sel;
383
384 /* Map back into a voltage to verify we're still in bounds */
385 voltage = rdev->desc->ops->list_voltage(rdev, ret);
386 if (voltage < min_uV || voltage > max_uV)
387 return -EINVAL;
388
389 return ret;
390}
391EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
392
393/**
394 * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges
395 *
396 * @rdev: Regulator to operate on
397 * @min_uV: Lower bound for voltage
398 * @max_uV: Upper bound for voltage
399 *
400 * Drivers providing linear_ranges in their descriptor can use this as
401 * their map_voltage() callback.
402 */
403int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
404 int min_uV, int max_uV)
405{
406 const struct regulator_linear_range *range;
407 int ret = -EINVAL;
408 int voltage, i;
409
410 if (!rdev->desc->n_linear_ranges) {
411 BUG_ON(!rdev->desc->n_linear_ranges);
412 return -EINVAL;
413 }
414
415 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
416 int linear_max_uV;
417
418 range = &rdev->desc->linear_ranges[i];
419 linear_max_uV = range->min_uV +
420 (range->max_sel - range->min_sel) * range->uV_step;
421
422 if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV))
423 continue;
424
425 if (min_uV <= range->min_uV)
426 min_uV = range->min_uV;
427
428 /* range->uV_step == 0 means fixed voltage range */
429 if (range->uV_step == 0) {
430 ret = 0;
431 } else {
432 ret = DIV_ROUND_UP(min_uV - range->min_uV,
433 range->uV_step);
434 if (ret < 0)
435 return ret;
436 }
437
438 ret += range->min_sel;
439
440 /*
441 * Map back into a voltage to verify we're still in bounds.
442 * If we are not, then continue checking rest of the ranges.
443 */
444 voltage = rdev->desc->ops->list_voltage(rdev, ret);
445 if (voltage >= min_uV && voltage <= max_uV)
446 break;
447 }
448
449 if (i == rdev->desc->n_linear_ranges)
450 return -EINVAL;
451
452 return ret;
453}
454EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
455
456/**
457 * regulator_map_voltage_pickable_linear_range - map_voltage, pickable ranges
458 *
459 * @rdev: Regulator to operate on
460 * @min_uV: Lower bound for voltage
461 * @max_uV: Upper bound for voltage
462 *
463 * Drivers providing pickable linear_ranges in their descriptor can use
464 * this as their map_voltage() callback.
465 */
466int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
467 int min_uV, int max_uV)
468{
469 const struct regulator_linear_range *range;
470 int ret = -EINVAL;
471 int voltage, i;
472 unsigned int selector = 0;
473
474 if (!rdev->desc->n_linear_ranges) {
475 BUG_ON(!rdev->desc->n_linear_ranges);
476 return -EINVAL;
477 }
478
479 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
480 int linear_max_uV;
481
482 range = &rdev->desc->linear_ranges[i];
483 linear_max_uV = range->min_uV +
484 (range->max_sel - range->min_sel) * range->uV_step;
485
486 if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV)) {
487 selector += (range->max_sel - range->min_sel + 1);
488 continue;
489 }
490
491 if (min_uV <= range->min_uV)
492 min_uV = range->min_uV;
493
494 /* range->uV_step == 0 means fixed voltage range */
495 if (range->uV_step == 0) {
496 ret = 0;
497 } else {
498 ret = DIV_ROUND_UP(min_uV - range->min_uV,
499 range->uV_step);
500 if (ret < 0)
501 return ret;
502 }
503
504 ret += selector;
505
506 voltage = rdev->desc->ops->list_voltage(rdev, ret);
507
508 /*
509 * Map back into a voltage to verify we're still in bounds.
510 * We may have overlapping voltage ranges. Hence we don't
511 * exit but retry until we have checked all ranges.
512 */
513 if (voltage < min_uV || voltage > max_uV)
514 selector += (range->max_sel - range->min_sel + 1);
515 else
516 break;
517 }
518
519 if (i == rdev->desc->n_linear_ranges)
520 return -EINVAL;
521
522 return ret;
523}
524EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range);
525
526/**
527 * regulator_list_voltage_linear - List voltages with simple calculation
528 *
529 * @rdev: Regulator device
530 * @selector: Selector to convert into a voltage
531 *
532 * Regulators with a simple linear mapping between voltages and
533 * selectors can set min_uV and uV_step in the regulator descriptor
534 * and then use this function as their list_voltage() operation,
535 */
536int regulator_list_voltage_linear(struct regulator_dev *rdev,
537 unsigned int selector)
538{
539 if (selector >= rdev->desc->n_voltages)
540 return -EINVAL;
541 if (selector < rdev->desc->linear_min_sel)
542 return 0;
543
544 selector -= rdev->desc->linear_min_sel;
545
546 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
547}
548EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
549
550/**
551 * regulator_list_voltage_pickable_linear_range - pickable range list voltages
552 *
553 * @rdev: Regulator device
554 * @selector: Selector to convert into a voltage
555 *
556 * list_voltage() operation, intended to be used by drivers utilizing pickable
557 * ranges helpers.
558 */
559int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
560 unsigned int selector)
561{
562 const struct regulator_linear_range *range;
563 int i;
564 unsigned int all_sels = 0;
565
566 if (!rdev->desc->n_linear_ranges) {
567 BUG_ON(!rdev->desc->n_linear_ranges);
568 return -EINVAL;
569 }
570
571 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
572 unsigned int sels_in_range;
573
574 range = &rdev->desc->linear_ranges[i];
575
576 sels_in_range = range->max_sel - range->min_sel;
577
578 if (all_sels + sels_in_range >= selector) {
579 selector -= all_sels;
580 return range->min_uV + (range->uV_step * selector);
581 }
582
583 all_sels += (sels_in_range + 1);
584 }
585
586 return -EINVAL;
587}
588EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range);
589
590/**
591 * regulator_desc_list_voltage_linear_range - List voltages for linear ranges
592 *
593 * @desc: Regulator desc for regulator which volatges are to be listed
594 * @selector: Selector to convert into a voltage
595 *
596 * Regulators with a series of simple linear mappings between voltages
597 * and selectors who have set linear_ranges in the regulator descriptor
598 * can use this function prior regulator registration to list voltages.
599 * This is useful when voltages need to be listed during device-tree
600 * parsing.
601 */
602int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
603 unsigned int selector)
604{
605 const struct regulator_linear_range *range;
606 int i;
607
608 if (!desc->n_linear_ranges) {
609 BUG_ON(!desc->n_linear_ranges);
610 return -EINVAL;
611 }
612
613 for (i = 0; i < desc->n_linear_ranges; i++) {
614 range = &desc->linear_ranges[i];
615
616 if (!(selector >= range->min_sel &&
617 selector <= range->max_sel))
618 continue;
619
620 selector -= range->min_sel;
621
622 return range->min_uV + (range->uV_step * selector);
623 }
624
625 return -EINVAL;
626}
627EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range);
628
629/**
630 * regulator_list_voltage_linear_range - List voltages for linear ranges
631 *
632 * @rdev: Regulator device
633 * @selector: Selector to convert into a voltage
634 *
635 * Regulators with a series of simple linear mappings between voltages
636 * and selectors can set linear_ranges in the regulator descriptor and
637 * then use this function as their list_voltage() operation,
638 */
639int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
640 unsigned int selector)
641{
642 return regulator_desc_list_voltage_linear_range(rdev->desc, selector);
643}
644EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
645
646/**
647 * regulator_list_voltage_table - List voltages with table based mapping
648 *
649 * @rdev: Regulator device
650 * @selector: Selector to convert into a voltage
651 *
652 * Regulators with table based mapping between voltages and
653 * selectors can set volt_table in the regulator descriptor
654 * and then use this function as their list_voltage() operation.
655 */
656int regulator_list_voltage_table(struct regulator_dev *rdev,
657 unsigned int selector)
658{
659 if (!rdev->desc->volt_table) {
660 BUG_ON(!rdev->desc->volt_table);
661 return -EINVAL;
662 }
663
664 if (selector >= rdev->desc->n_voltages)
665 return -EINVAL;
666
667 return rdev->desc->volt_table[selector];
668}
669EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
670
671/**
672 * regulator_set_bypass_regmap - Default set_bypass() using regmap
673 *
674 * @rdev: device to operate on.
675 * @enable: state to set.
676 */
677int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
678{
679 unsigned int val;
680
681 if (enable) {
682 val = rdev->desc->bypass_val_on;
683 if (!val)
684 val = rdev->desc->bypass_mask;
685 } else {
686 val = rdev->desc->bypass_val_off;
687 }
688
689 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
690 rdev->desc->bypass_mask, val);
691}
692EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
693
694/**
695 * regulator_set_soft_start_regmap - Default set_soft_start() using regmap
696 *
697 * @rdev: device to operate on.
698 */
699int regulator_set_soft_start_regmap(struct regulator_dev *rdev)
700{
701 unsigned int val;
702
703 val = rdev->desc->soft_start_val_on;
704 if (!val)
705 val = rdev->desc->soft_start_mask;
706
707 return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg,
708 rdev->desc->soft_start_mask, val);
709}
710EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap);
711
712/**
713 * regulator_set_pull_down_regmap - Default set_pull_down() using regmap
714 *
715 * @rdev: device to operate on.
716 */
717int regulator_set_pull_down_regmap(struct regulator_dev *rdev)
718{
719 unsigned int val;
720
721 val = rdev->desc->pull_down_val_on;
722 if (!val)
723 val = rdev->desc->pull_down_mask;
724
725 return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg,
726 rdev->desc->pull_down_mask, val);
727}
728EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap);
729
730/**
731 * regulator_get_bypass_regmap - Default get_bypass() using regmap
732 *
733 * @rdev: device to operate on.
734 * @enable: current state.
735 */
736int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
737{
738 unsigned int val;
739 unsigned int val_on = rdev->desc->bypass_val_on;
740 int ret;
741
742 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
743 if (ret != 0)
744 return ret;
745
746 if (!val_on)
747 val_on = rdev->desc->bypass_mask;
748
749 *enable = (val & rdev->desc->bypass_mask) == val_on;
750
751 return 0;
752}
753EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
754
755/**
756 * regulator_set_active_discharge_regmap - Default set_active_discharge()
757 * using regmap
758 *
759 * @rdev: device to operate on.
760 * @enable: state to set, 0 to disable and 1 to enable.
761 */
762int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
763 bool enable)
764{
765 unsigned int val;
766
767 if (enable)
768 val = rdev->desc->active_discharge_on;
769 else
770 val = rdev->desc->active_discharge_off;
771
772 return regmap_update_bits(rdev->regmap,
773 rdev->desc->active_discharge_reg,
774 rdev->desc->active_discharge_mask, val);
775}
776EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);
777
778/**
779 * regulator_set_current_limit_regmap - set_current_limit for regmap users
780 *
781 * @rdev: regulator to operate on
782 * @min_uA: Lower bound for current limit
783 * @max_uA: Upper bound for current limit
784 *
785 * Regulators that use regmap for their register I/O can set curr_table,
786 * csel_reg and csel_mask fields in their descriptor and then use this
787 * as their set_current_limit operation, saving some code.
788 */
789int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
790 int min_uA, int max_uA)
791{
792 unsigned int n_currents = rdev->desc->n_current_limits;
793 int i, sel = -1;
794
795 if (n_currents == 0)
796 return -EINVAL;
797
798 if (rdev->desc->curr_table) {
799 const unsigned int *curr_table = rdev->desc->curr_table;
800 bool ascend = curr_table[n_currents - 1] > curr_table[0];
801
802 /* search for closest to maximum */
803 if (ascend) {
804 for (i = n_currents - 1; i >= 0; i--) {
805 if (min_uA <= curr_table[i] &&
806 curr_table[i] <= max_uA) {
807 sel = i;
808 break;
809 }
810 }
811 } else {
812 for (i = 0; i < n_currents; i++) {
813 if (min_uA <= curr_table[i] &&
814 curr_table[i] <= max_uA) {
815 sel = i;
816 break;
817 }
818 }
819 }
820 }
821
822 if (sel < 0)
823 return -EINVAL;
824
825 sel <<= ffs(rdev->desc->csel_mask) - 1;
826
827 return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
828 rdev->desc->csel_mask, sel);
829}
830EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap);
831
832/**
833 * regulator_get_current_limit_regmap - get_current_limit for regmap users
834 *
835 * @rdev: regulator to operate on
836 *
837 * Regulators that use regmap for their register I/O can set the
838 * csel_reg and csel_mask fields in their descriptor and then use this
839 * as their get_current_limit operation, saving some code.
840 */
841int regulator_get_current_limit_regmap(struct regulator_dev *rdev)
842{
843 unsigned int val;
844 int ret;
845
846 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
847 if (ret != 0)
848 return ret;
849
850 val &= rdev->desc->csel_mask;
851 val >>= ffs(rdev->desc->csel_mask) - 1;
852
853 if (rdev->desc->curr_table) {
854 if (val >= rdev->desc->n_current_limits)
855 return -EINVAL;
856
857 return rdev->desc->curr_table[val];
858 }
859
860 return -EINVAL;
861}
862EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap);
863
864/**
865 * regulator_bulk_set_supply_names - initialize the 'supply' fields in an array
866 * of regulator_bulk_data structs
867 *
868 * @consumers: array of regulator_bulk_data entries to initialize
869 * @supply_names: array of supply name strings
870 * @num_supplies: number of supply names to initialize
871 *
872 * Note: the 'consumers' array must be the size of 'num_supplies'.
873 */
874void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
875 const char *const *supply_names,
876 unsigned int num_supplies)
877{
878 unsigned int i;
879
880 for (i = 0; i < num_supplies; i++)
881 consumers[i].supply = supply_names[i];
882}
883EXPORT_SYMBOL_GPL(regulator_bulk_set_supply_names);