This source file includes following definitions.
- regulator_is_enabled_regmap
- regulator_enable_regmap
- regulator_disable_regmap
- regulator_range_selector_to_index
- regulator_get_voltage_sel_pickable_regmap
- regulator_set_voltage_sel_pickable_regmap
- regulator_get_voltage_sel_regmap
- regulator_set_voltage_sel_regmap
- regulator_map_voltage_iterate
- regulator_map_voltage_ascend
- regulator_map_voltage_linear
- regulator_map_voltage_linear_range
- regulator_map_voltage_pickable_linear_range
- regulator_list_voltage_linear
- regulator_list_voltage_pickable_linear_range
- regulator_desc_list_voltage_linear_range
- regulator_list_voltage_linear_range
- regulator_list_voltage_table
- regulator_set_bypass_regmap
- regulator_set_soft_start_regmap
- regulator_set_pull_down_regmap
- regulator_get_bypass_regmap
- regulator_set_active_discharge_regmap
- regulator_set_current_limit_regmap
- regulator_get_current_limit_regmap
- regulator_bulk_set_supply_names
- regulator_is_equal
1
2
3
4
5
6
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 #include "internal.h"
17
18
19
20
21
22
23
24
25
26
27 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
28 {
29 unsigned int val;
30 int ret;
31
32 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
33 if (ret != 0)
34 return ret;
35
36 val &= rdev->desc->enable_mask;
37
38 if (rdev->desc->enable_is_inverted) {
39 if (rdev->desc->enable_val)
40 return val != rdev->desc->enable_val;
41 return val == 0;
42 } else {
43 if (rdev->desc->enable_val)
44 return val == rdev->desc->enable_val;
45 return val != 0;
46 }
47 }
48 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
49
50
51
52
53
54
55
56
57
58
59 int regulator_enable_regmap(struct regulator_dev *rdev)
60 {
61 unsigned int val;
62
63 if (rdev->desc->enable_is_inverted) {
64 val = rdev->desc->disable_val;
65 } else {
66 val = rdev->desc->enable_val;
67 if (!val)
68 val = rdev->desc->enable_mask;
69 }
70
71 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
72 rdev->desc->enable_mask, val);
73 }
74 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
75
76
77
78
79
80
81
82
83
84
85 int regulator_disable_regmap(struct regulator_dev *rdev)
86 {
87 unsigned int val;
88
89 if (rdev->desc->enable_is_inverted) {
90 val = rdev->desc->enable_val;
91 if (!val)
92 val = rdev->desc->enable_mask;
93 } else {
94 val = rdev->desc->disable_val;
95 }
96
97 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
98 rdev->desc->enable_mask, val);
99 }
100 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
101
102 static int regulator_range_selector_to_index(struct regulator_dev *rdev,
103 unsigned int rval)
104 {
105 int i;
106
107 if (!rdev->desc->linear_range_selectors)
108 return -EINVAL;
109
110 rval &= rdev->desc->vsel_range_mask;
111
112 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
113 if (rdev->desc->linear_range_selectors[i] == rval)
114 return i;
115 }
116 return -EINVAL;
117 }
118
119
120
121
122
123
124
125
126
127
128
129 int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev)
130 {
131 unsigned int r_val;
132 int range;
133 unsigned int val;
134 int ret, i;
135 unsigned int voltages_in_range = 0;
136
137 if (!rdev->desc->linear_ranges)
138 return -EINVAL;
139
140 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
141 if (ret != 0)
142 return ret;
143
144 ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val);
145 if (ret != 0)
146 return ret;
147
148 val &= rdev->desc->vsel_mask;
149 val >>= ffs(rdev->desc->vsel_mask) - 1;
150
151 range = regulator_range_selector_to_index(rdev, r_val);
152 if (range < 0)
153 return -EINVAL;
154
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;
158
159 return val + voltages_in_range;
160 }
161 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_pickable_regmap);
162
163
164
165
166
167
168
169
170
171
172
173
174 int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
175 unsigned int sel)
176 {
177 unsigned int range;
178 int ret, i;
179 unsigned int voltages_in_range = 0;
180
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)
185 break;
186 sel -= voltages_in_range;
187 }
188
189 if (i == rdev->desc->n_linear_ranges)
190 return -EINVAL;
191
192 sel <<= ffs(rdev->desc->vsel_mask) - 1;
193 sel += rdev->desc->linear_ranges[i].min_sel;
194
195 range = rdev->desc->linear_range_selectors[i];
196
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);
202 } else {
203 ret = regmap_update_bits(rdev->regmap,
204 rdev->desc->vsel_range_reg,
205 rdev->desc->vsel_range_mask, range);
206 if (ret)
207 return ret;
208
209 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
210 rdev->desc->vsel_mask, sel);
211 }
212
213 if (ret)
214 return ret;
215
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);
220 return ret;
221 }
222 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_pickable_regmap);
223
224
225
226
227
228
229
230
231
232
233 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
234 {
235 unsigned int val;
236 int ret;
237
238 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
239 if (ret != 0)
240 return ret;
241
242 val &= rdev->desc->vsel_mask;
243 val >>= ffs(rdev->desc->vsel_mask) - 1;
244
245 return val;
246 }
247 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
248
249
250
251
252
253
254
255
256
257
258
259 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
260 {
261 int ret;
262
263 sel <<= ffs(rdev->desc->vsel_mask) - 1;
264
265 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
266 rdev->desc->vsel_mask, sel);
267 if (ret)
268 return ret;
269
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);
274 return ret;
275 }
276 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
277
278
279
280
281
282
283
284
285
286
287
288
289
290 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
291 int min_uV, int max_uV)
292 {
293 int best_val = INT_MAX;
294 int selector = 0;
295 int i, ret;
296
297
298
299
300 for (i = 0; i < rdev->desc->n_voltages; i++) {
301 ret = rdev->desc->ops->list_voltage(rdev, i);
302 if (ret < 0)
303 continue;
304
305 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
306 best_val = ret;
307 selector = i;
308 }
309 }
310
311 if (best_val != INT_MAX)
312 return selector;
313 else
314 return -EINVAL;
315 }
316 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
317
318
319
320
321
322
323
324
325
326
327
328 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
329 int min_uV, int max_uV)
330 {
331 int i, ret;
332
333 for (i = 0; i < rdev->desc->n_voltages; i++) {
334 ret = rdev->desc->ops->list_voltage(rdev, i);
335 if (ret < 0)
336 continue;
337
338 if (ret > max_uV)
339 break;
340
341 if (ret >= min_uV && ret <= max_uV)
342 return i;
343 }
344
345 return -EINVAL;
346 }
347 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
348
349
350
351
352
353
354
355
356
357
358
359 int regulator_map_voltage_linear(struct regulator_dev *rdev,
360 int min_uV, int max_uV)
361 {
362 int ret, voltage;
363
364
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)
367 return 0;
368 else
369 return -EINVAL;
370 }
371
372 if (!rdev->desc->uV_step) {
373 BUG_ON(!rdev->desc->uV_step);
374 return -EINVAL;
375 }
376
377 if (min_uV < rdev->desc->min_uV)
378 min_uV = rdev->desc->min_uV;
379
380 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
381 if (ret < 0)
382 return ret;
383
384 ret += rdev->desc->linear_min_sel;
385
386
387 voltage = rdev->desc->ops->list_voltage(rdev, ret);
388 if (voltage < min_uV || voltage > max_uV)
389 return -EINVAL;
390
391 return ret;
392 }
393 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
394
395
396
397
398
399
400
401
402
403
404
405 int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
406 int min_uV, int max_uV)
407 {
408 const struct regulator_linear_range *range;
409 int ret = -EINVAL;
410 int voltage, i;
411
412 if (!rdev->desc->n_linear_ranges) {
413 BUG_ON(!rdev->desc->n_linear_ranges);
414 return -EINVAL;
415 }
416
417 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
418 int linear_max_uV;
419
420 range = &rdev->desc->linear_ranges[i];
421 linear_max_uV = range->min_uV +
422 (range->max_sel - range->min_sel) * range->uV_step;
423
424 if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV))
425 continue;
426
427 if (min_uV <= range->min_uV)
428 min_uV = range->min_uV;
429
430
431 if (range->uV_step == 0) {
432 ret = 0;
433 } else {
434 ret = DIV_ROUND_UP(min_uV - range->min_uV,
435 range->uV_step);
436 if (ret < 0)
437 return ret;
438 }
439
440 ret += range->min_sel;
441
442
443
444
445
446 voltage = rdev->desc->ops->list_voltage(rdev, ret);
447 if (voltage >= min_uV && voltage <= max_uV)
448 break;
449 }
450
451 if (i == rdev->desc->n_linear_ranges)
452 return -EINVAL;
453
454 return ret;
455 }
456 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
457
458
459
460
461
462
463
464
465
466
467
468 int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
469 int min_uV, int max_uV)
470 {
471 const struct regulator_linear_range *range;
472 int ret = -EINVAL;
473 int voltage, i;
474 unsigned int selector = 0;
475
476 if (!rdev->desc->n_linear_ranges) {
477 BUG_ON(!rdev->desc->n_linear_ranges);
478 return -EINVAL;
479 }
480
481 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
482 int linear_max_uV;
483
484 range = &rdev->desc->linear_ranges[i];
485 linear_max_uV = range->min_uV +
486 (range->max_sel - range->min_sel) * range->uV_step;
487
488 if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV)) {
489 selector += (range->max_sel - range->min_sel + 1);
490 continue;
491 }
492
493 if (min_uV <= range->min_uV)
494 min_uV = range->min_uV;
495
496
497 if (range->uV_step == 0) {
498 ret = 0;
499 } else {
500 ret = DIV_ROUND_UP(min_uV - range->min_uV,
501 range->uV_step);
502 if (ret < 0)
503 return ret;
504 }
505
506 ret += selector;
507
508 voltage = rdev->desc->ops->list_voltage(rdev, ret);
509
510
511
512
513
514
515 if (voltage < min_uV || voltage > max_uV)
516 selector += (range->max_sel - range->min_sel + 1);
517 else
518 break;
519 }
520
521 if (i == rdev->desc->n_linear_ranges)
522 return -EINVAL;
523
524 return ret;
525 }
526 EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range);
527
528
529
530
531
532
533
534
535
536
537
538 int regulator_list_voltage_linear(struct regulator_dev *rdev,
539 unsigned int selector)
540 {
541 if (selector >= rdev->desc->n_voltages)
542 return -EINVAL;
543 if (selector < rdev->desc->linear_min_sel)
544 return 0;
545
546 selector -= rdev->desc->linear_min_sel;
547
548 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
549 }
550 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
551
552
553
554
555
556
557
558
559
560
561 int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
562 unsigned int selector)
563 {
564 const struct regulator_linear_range *range;
565 int i;
566 unsigned int all_sels = 0;
567
568 if (!rdev->desc->n_linear_ranges) {
569 BUG_ON(!rdev->desc->n_linear_ranges);
570 return -EINVAL;
571 }
572
573 for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
574 unsigned int sels_in_range;
575
576 range = &rdev->desc->linear_ranges[i];
577
578 sels_in_range = range->max_sel - range->min_sel;
579
580 if (all_sels + sels_in_range >= selector) {
581 selector -= all_sels;
582 return range->min_uV + (range->uV_step * selector);
583 }
584
585 all_sels += (sels_in_range + 1);
586 }
587
588 return -EINVAL;
589 }
590 EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range);
591
592
593
594
595
596
597
598
599
600
601
602
603
604 int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
605 unsigned int selector)
606 {
607 const struct regulator_linear_range *range;
608 int i;
609
610 if (!desc->n_linear_ranges) {
611 BUG_ON(!desc->n_linear_ranges);
612 return -EINVAL;
613 }
614
615 for (i = 0; i < desc->n_linear_ranges; i++) {
616 range = &desc->linear_ranges[i];
617
618 if (!(selector >= range->min_sel &&
619 selector <= range->max_sel))
620 continue;
621
622 selector -= range->min_sel;
623
624 return range->min_uV + (range->uV_step * selector);
625 }
626
627 return -EINVAL;
628 }
629 EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range);
630
631
632
633
634
635
636
637
638
639
640
641 int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
642 unsigned int selector)
643 {
644 return regulator_desc_list_voltage_linear_range(rdev->desc, selector);
645 }
646 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
647
648
649
650
651
652
653
654
655
656
657
658 int regulator_list_voltage_table(struct regulator_dev *rdev,
659 unsigned int selector)
660 {
661 if (!rdev->desc->volt_table) {
662 BUG_ON(!rdev->desc->volt_table);
663 return -EINVAL;
664 }
665
666 if (selector >= rdev->desc->n_voltages)
667 return -EINVAL;
668
669 return rdev->desc->volt_table[selector];
670 }
671 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
672
673
674
675
676
677
678
679 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
680 {
681 unsigned int val;
682
683 if (enable) {
684 val = rdev->desc->bypass_val_on;
685 if (!val)
686 val = rdev->desc->bypass_mask;
687 } else {
688 val = rdev->desc->bypass_val_off;
689 }
690
691 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
692 rdev->desc->bypass_mask, val);
693 }
694 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
695
696
697
698
699
700
701 int regulator_set_soft_start_regmap(struct regulator_dev *rdev)
702 {
703 unsigned int val;
704
705 val = rdev->desc->soft_start_val_on;
706 if (!val)
707 val = rdev->desc->soft_start_mask;
708
709 return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg,
710 rdev->desc->soft_start_mask, val);
711 }
712 EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap);
713
714
715
716
717
718
719 int regulator_set_pull_down_regmap(struct regulator_dev *rdev)
720 {
721 unsigned int val;
722
723 val = rdev->desc->pull_down_val_on;
724 if (!val)
725 val = rdev->desc->pull_down_mask;
726
727 return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg,
728 rdev->desc->pull_down_mask, val);
729 }
730 EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap);
731
732
733
734
735
736
737
738 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
739 {
740 unsigned int val;
741 unsigned int val_on = rdev->desc->bypass_val_on;
742 int ret;
743
744 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
745 if (ret != 0)
746 return ret;
747
748 if (!val_on)
749 val_on = rdev->desc->bypass_mask;
750
751 *enable = (val & rdev->desc->bypass_mask) == val_on;
752
753 return 0;
754 }
755 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
756
757
758
759
760
761
762
763
764 int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
765 bool enable)
766 {
767 unsigned int val;
768
769 if (enable)
770 val = rdev->desc->active_discharge_on;
771 else
772 val = rdev->desc->active_discharge_off;
773
774 return regmap_update_bits(rdev->regmap,
775 rdev->desc->active_discharge_reg,
776 rdev->desc->active_discharge_mask, val);
777 }
778 EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap);
779
780
781
782
783
784
785
786
787
788
789
790
791 int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
792 int min_uA, int max_uA)
793 {
794 unsigned int n_currents = rdev->desc->n_current_limits;
795 int i, sel = -1;
796
797 if (n_currents == 0)
798 return -EINVAL;
799
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];
803
804
805 if (ascend) {
806 for (i = n_currents - 1; i >= 0; i--) {
807 if (min_uA <= curr_table[i] &&
808 curr_table[i] <= max_uA) {
809 sel = i;
810 break;
811 }
812 }
813 } else {
814 for (i = 0; i < n_currents; i++) {
815 if (min_uA <= curr_table[i] &&
816 curr_table[i] <= max_uA) {
817 sel = i;
818 break;
819 }
820 }
821 }
822 }
823
824 if (sel < 0)
825 return -EINVAL;
826
827 sel <<= ffs(rdev->desc->csel_mask) - 1;
828
829 return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg,
830 rdev->desc->csel_mask, sel);
831 }
832 EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap);
833
834
835
836
837
838
839
840
841
842
843 int regulator_get_current_limit_regmap(struct regulator_dev *rdev)
844 {
845 unsigned int val;
846 int ret;
847
848 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val);
849 if (ret != 0)
850 return ret;
851
852 val &= rdev->desc->csel_mask;
853 val >>= ffs(rdev->desc->csel_mask) - 1;
854
855 if (rdev->desc->curr_table) {
856 if (val >= rdev->desc->n_current_limits)
857 return -EINVAL;
858
859 return rdev->desc->curr_table[val];
860 }
861
862 return -EINVAL;
863 }
864 EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap);
865
866
867
868
869
870
871
872
873
874
875
876 void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers,
877 const char *const *supply_names,
878 unsigned int num_supplies)
879 {
880 unsigned int i;
881
882 for (i = 0; i < num_supplies; i++)
883 consumers[i].supply = supply_names[i];
884 }
885 EXPORT_SYMBOL_GPL(regulator_bulk_set_supply_names);
886
887
888
889
890
891
892
893 bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
894 {
895 return reg1->rdev == reg2->rdev;
896 }
897 EXPORT_SYMBOL_GPL(regulator_is_equal);