1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/debugfs.h>
19#include <linux/device.h>
20#include <linux/slab.h>
21#include <linux/async.h>
22#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
25#include <linux/delay.h>
26#include <linux/gpio.h>
27#include <linux/gpio/consumer.h>
28#include <linux/of.h>
29#include <linux/regmap.h>
30#include <linux/regulator/of_regulator.h>
31#include <linux/regulator/consumer.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
34#include <linux/module.h>
35
36#define CREATE_TRACE_POINTS
37#include <trace/events/regulator.h>
38
39#include "dummy.h"
40#include "internal.h"
41
42#define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50#define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52
53static DEFINE_MUTEX(regulator_list_mutex);
54static LIST_HEAD(regulator_map_list);
55static LIST_HEAD(regulator_ena_gpio_list);
56static LIST_HEAD(regulator_supply_alias_list);
57static bool has_full_constraints;
58
59static struct dentry *debugfs_root;
60
61
62
63
64
65
66struct regulator_map {
67 struct list_head list;
68 const char *dev_name;
69 const char *supply;
70 struct regulator_dev *regulator;
71};
72
73
74
75
76
77
78struct regulator_enable_gpio {
79 struct list_head list;
80 struct gpio_desc *gpiod;
81 u32 enable_count;
82 u32 request_count;
83 unsigned int ena_gpio_invert:1;
84};
85
86
87
88
89
90
91struct regulator_supply_alias {
92 struct list_head list;
93 struct device *src_dev;
94 const char *src_supply;
95 struct device *alias_dev;
96 const char *alias_supply;
97};
98
99static int _regulator_is_enabled(struct regulator_dev *rdev);
100static int _regulator_disable(struct regulator_dev *rdev);
101static int _regulator_get_voltage(struct regulator_dev *rdev);
102static int _regulator_get_current_limit(struct regulator_dev *rdev);
103static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
104static int _notifier_call_chain(struct regulator_dev *rdev,
105 unsigned long event, void *data);
106static int _regulator_do_set_voltage(struct regulator_dev *rdev,
107 int min_uV, int max_uV);
108static struct regulator *create_regulator(struct regulator_dev *rdev,
109 struct device *dev,
110 const char *supply_name);
111static void _regulator_put(struct regulator *regulator);
112
113static const char *rdev_get_name(struct regulator_dev *rdev)
114{
115 if (rdev->constraints && rdev->constraints->name)
116 return rdev->constraints->name;
117 else if (rdev->desc->name)
118 return rdev->desc->name;
119 else
120 return "";
121}
122
123static bool have_full_constraints(void)
124{
125 return has_full_constraints || of_have_populated_dt();
126}
127
128static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
129{
130 if (!rdev->constraints) {
131 rdev_err(rdev, "no constraints\n");
132 return false;
133 }
134
135 if (rdev->constraints->valid_ops_mask & ops)
136 return true;
137
138 return false;
139}
140
141static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
142{
143 if (rdev && rdev->supply)
144 return rdev->supply->rdev;
145
146 return NULL;
147}
148
149
150
151
152
153
154
155
156
157
158
159
160static void regulator_lock_nested(struct regulator_dev *rdev,
161 unsigned int subclass)
162{
163 if (!mutex_trylock(&rdev->mutex)) {
164 if (rdev->mutex_owner == current) {
165 rdev->ref_cnt++;
166 return;
167 }
168 mutex_lock_nested(&rdev->mutex, subclass);
169 }
170
171 rdev->ref_cnt = 1;
172 rdev->mutex_owner = current;
173}
174
175static inline void regulator_lock(struct regulator_dev *rdev)
176{
177 regulator_lock_nested(rdev, 0);
178}
179
180
181
182
183
184
185
186
187static void regulator_unlock(struct regulator_dev *rdev)
188{
189 if (rdev->ref_cnt != 0) {
190 rdev->ref_cnt--;
191
192 if (!rdev->ref_cnt) {
193 rdev->mutex_owner = NULL;
194 mutex_unlock(&rdev->mutex);
195 }
196 }
197}
198
199
200
201
202
203static void regulator_lock_supply(struct regulator_dev *rdev)
204{
205 int i;
206
207 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
208 regulator_lock_nested(rdev, i);
209}
210
211
212
213
214
215static void regulator_unlock_supply(struct regulator_dev *rdev)
216{
217 struct regulator *supply;
218
219 while (1) {
220 regulator_unlock(rdev);
221 supply = rdev->supply;
222
223 if (!rdev->supply)
224 return;
225
226 rdev = supply->rdev;
227 }
228}
229
230
231
232
233
234
235
236
237
238
239static struct device_node *of_get_regulator(struct device *dev, const char *supply)
240{
241 struct device_node *regnode = NULL;
242 char prop_name[32];
243
244 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
245
246 snprintf(prop_name, 32, "%s-supply", supply);
247 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
248
249 if (!regnode) {
250 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
251 prop_name, dev->of_node);
252 return NULL;
253 }
254 return regnode;
255}
256
257
258static int regulator_check_voltage(struct regulator_dev *rdev,
259 int *min_uV, int *max_uV)
260{
261 BUG_ON(*min_uV > *max_uV);
262
263 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
264 rdev_err(rdev, "voltage operation not allowed\n");
265 return -EPERM;
266 }
267
268 if (*max_uV > rdev->constraints->max_uV)
269 *max_uV = rdev->constraints->max_uV;
270 if (*min_uV < rdev->constraints->min_uV)
271 *min_uV = rdev->constraints->min_uV;
272
273 if (*min_uV > *max_uV) {
274 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
275 *min_uV, *max_uV);
276 return -EINVAL;
277 }
278
279 return 0;
280}
281
282
283static int regulator_check_states(suspend_state_t state)
284{
285 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
286}
287
288
289
290
291static int regulator_check_consumers(struct regulator_dev *rdev,
292 int *min_uV, int *max_uV,
293 suspend_state_t state)
294{
295 struct regulator *regulator;
296 struct regulator_voltage *voltage;
297
298 list_for_each_entry(regulator, &rdev->consumer_list, list) {
299 voltage = ®ulator->voltage[state];
300
301
302
303
304 if (!voltage->min_uV && !voltage->max_uV)
305 continue;
306
307 if (*max_uV > voltage->max_uV)
308 *max_uV = voltage->max_uV;
309 if (*min_uV < voltage->min_uV)
310 *min_uV = voltage->min_uV;
311 }
312
313 if (*min_uV > *max_uV) {
314 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
315 *min_uV, *max_uV);
316 return -EINVAL;
317 }
318
319 return 0;
320}
321
322
323static int regulator_check_current_limit(struct regulator_dev *rdev,
324 int *min_uA, int *max_uA)
325{
326 BUG_ON(*min_uA > *max_uA);
327
328 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
329 rdev_err(rdev, "current operation not allowed\n");
330 return -EPERM;
331 }
332
333 if (*max_uA > rdev->constraints->max_uA)
334 *max_uA = rdev->constraints->max_uA;
335 if (*min_uA < rdev->constraints->min_uA)
336 *min_uA = rdev->constraints->min_uA;
337
338 if (*min_uA > *max_uA) {
339 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
340 *min_uA, *max_uA);
341 return -EINVAL;
342 }
343
344 return 0;
345}
346
347
348static int regulator_mode_constrain(struct regulator_dev *rdev,
349 unsigned int *mode)
350{
351 switch (*mode) {
352 case REGULATOR_MODE_FAST:
353 case REGULATOR_MODE_NORMAL:
354 case REGULATOR_MODE_IDLE:
355 case REGULATOR_MODE_STANDBY:
356 break;
357 default:
358 rdev_err(rdev, "invalid mode %x specified\n", *mode);
359 return -EINVAL;
360 }
361
362 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
363 rdev_err(rdev, "mode operation not allowed\n");
364 return -EPERM;
365 }
366
367
368
369
370 while (*mode) {
371 if (rdev->constraints->valid_modes_mask & *mode)
372 return 0;
373 *mode /= 2;
374 }
375
376 return -EINVAL;
377}
378
379static inline struct regulator_state *
380regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
381{
382 if (rdev->constraints == NULL)
383 return NULL;
384
385 switch (state) {
386 case PM_SUSPEND_STANDBY:
387 return &rdev->constraints->state_standby;
388 case PM_SUSPEND_MEM:
389 return &rdev->constraints->state_mem;
390 case PM_SUSPEND_MAX:
391 return &rdev->constraints->state_disk;
392 default:
393 return NULL;
394 }
395}
396
397static ssize_t regulator_uV_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
399{
400 struct regulator_dev *rdev = dev_get_drvdata(dev);
401 ssize_t ret;
402
403 regulator_lock(rdev);
404 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
405 regulator_unlock(rdev);
406
407 return ret;
408}
409static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
410
411static ssize_t regulator_uA_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
413{
414 struct regulator_dev *rdev = dev_get_drvdata(dev);
415
416 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
417}
418static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
419
420static ssize_t name_show(struct device *dev, struct device_attribute *attr,
421 char *buf)
422{
423 struct regulator_dev *rdev = dev_get_drvdata(dev);
424
425 return sprintf(buf, "%s\n", rdev_get_name(rdev));
426}
427static DEVICE_ATTR_RO(name);
428
429static ssize_t regulator_print_opmode(char *buf, int mode)
430{
431 switch (mode) {
432 case REGULATOR_MODE_FAST:
433 return sprintf(buf, "fast\n");
434 case REGULATOR_MODE_NORMAL:
435 return sprintf(buf, "normal\n");
436 case REGULATOR_MODE_IDLE:
437 return sprintf(buf, "idle\n");
438 case REGULATOR_MODE_STANDBY:
439 return sprintf(buf, "standby\n");
440 }
441 return sprintf(buf, "unknown\n");
442}
443
444static ssize_t regulator_opmode_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
446{
447 struct regulator_dev *rdev = dev_get_drvdata(dev);
448
449 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
450}
451static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
452
453static ssize_t regulator_print_state(char *buf, int state)
454{
455 if (state > 0)
456 return sprintf(buf, "enabled\n");
457 else if (state == 0)
458 return sprintf(buf, "disabled\n");
459 else
460 return sprintf(buf, "unknown\n");
461}
462
463static ssize_t regulator_state_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
466 struct regulator_dev *rdev = dev_get_drvdata(dev);
467 ssize_t ret;
468
469 regulator_lock(rdev);
470 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
471 regulator_unlock(rdev);
472
473 return ret;
474}
475static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
476
477static ssize_t regulator_status_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
479{
480 struct regulator_dev *rdev = dev_get_drvdata(dev);
481 int status;
482 char *label;
483
484 status = rdev->desc->ops->get_status(rdev);
485 if (status < 0)
486 return status;
487
488 switch (status) {
489 case REGULATOR_STATUS_OFF:
490 label = "off";
491 break;
492 case REGULATOR_STATUS_ON:
493 label = "on";
494 break;
495 case REGULATOR_STATUS_ERROR:
496 label = "error";
497 break;
498 case REGULATOR_STATUS_FAST:
499 label = "fast";
500 break;
501 case REGULATOR_STATUS_NORMAL:
502 label = "normal";
503 break;
504 case REGULATOR_STATUS_IDLE:
505 label = "idle";
506 break;
507 case REGULATOR_STATUS_STANDBY:
508 label = "standby";
509 break;
510 case REGULATOR_STATUS_BYPASS:
511 label = "bypass";
512 break;
513 case REGULATOR_STATUS_UNDEFINED:
514 label = "undefined";
515 break;
516 default:
517 return -ERANGE;
518 }
519
520 return sprintf(buf, "%s\n", label);
521}
522static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
523
524static ssize_t regulator_min_uA_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
527 struct regulator_dev *rdev = dev_get_drvdata(dev);
528
529 if (!rdev->constraints)
530 return sprintf(buf, "constraint not defined\n");
531
532 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
533}
534static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
535
536static ssize_t regulator_max_uA_show(struct device *dev,
537 struct device_attribute *attr, char *buf)
538{
539 struct regulator_dev *rdev = dev_get_drvdata(dev);
540
541 if (!rdev->constraints)
542 return sprintf(buf, "constraint not defined\n");
543
544 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
545}
546static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
547
548static ssize_t regulator_min_uV_show(struct device *dev,
549 struct device_attribute *attr, char *buf)
550{
551 struct regulator_dev *rdev = dev_get_drvdata(dev);
552
553 if (!rdev->constraints)
554 return sprintf(buf, "constraint not defined\n");
555
556 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
557}
558static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
559
560static ssize_t regulator_max_uV_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 struct regulator_dev *rdev = dev_get_drvdata(dev);
564
565 if (!rdev->constraints)
566 return sprintf(buf, "constraint not defined\n");
567
568 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
569}
570static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
571
572static ssize_t regulator_total_uA_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
574{
575 struct regulator_dev *rdev = dev_get_drvdata(dev);
576 struct regulator *regulator;
577 int uA = 0;
578
579 regulator_lock(rdev);
580 list_for_each_entry(regulator, &rdev->consumer_list, list)
581 uA += regulator->uA_load;
582 regulator_unlock(rdev);
583 return sprintf(buf, "%d\n", uA);
584}
585static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
586
587static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
588 char *buf)
589{
590 struct regulator_dev *rdev = dev_get_drvdata(dev);
591 return sprintf(buf, "%d\n", rdev->use_count);
592}
593static DEVICE_ATTR_RO(num_users);
594
595static ssize_t type_show(struct device *dev, struct device_attribute *attr,
596 char *buf)
597{
598 struct regulator_dev *rdev = dev_get_drvdata(dev);
599
600 switch (rdev->desc->type) {
601 case REGULATOR_VOLTAGE:
602 return sprintf(buf, "voltage\n");
603 case REGULATOR_CURRENT:
604 return sprintf(buf, "current\n");
605 }
606 return sprintf(buf, "unknown\n");
607}
608static DEVICE_ATTR_RO(type);
609
610static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
611 struct device_attribute *attr, char *buf)
612{
613 struct regulator_dev *rdev = dev_get_drvdata(dev);
614
615 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
616}
617static DEVICE_ATTR(suspend_mem_microvolts, 0444,
618 regulator_suspend_mem_uV_show, NULL);
619
620static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
621 struct device_attribute *attr, char *buf)
622{
623 struct regulator_dev *rdev = dev_get_drvdata(dev);
624
625 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
626}
627static DEVICE_ATTR(suspend_disk_microvolts, 0444,
628 regulator_suspend_disk_uV_show, NULL);
629
630static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
631 struct device_attribute *attr, char *buf)
632{
633 struct regulator_dev *rdev = dev_get_drvdata(dev);
634
635 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
636}
637static DEVICE_ATTR(suspend_standby_microvolts, 0444,
638 regulator_suspend_standby_uV_show, NULL);
639
640static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
641 struct device_attribute *attr, char *buf)
642{
643 struct regulator_dev *rdev = dev_get_drvdata(dev);
644
645 return regulator_print_opmode(buf,
646 rdev->constraints->state_mem.mode);
647}
648static DEVICE_ATTR(suspend_mem_mode, 0444,
649 regulator_suspend_mem_mode_show, NULL);
650
651static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
652 struct device_attribute *attr, char *buf)
653{
654 struct regulator_dev *rdev = dev_get_drvdata(dev);
655
656 return regulator_print_opmode(buf,
657 rdev->constraints->state_disk.mode);
658}
659static DEVICE_ATTR(suspend_disk_mode, 0444,
660 regulator_suspend_disk_mode_show, NULL);
661
662static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
663 struct device_attribute *attr, char *buf)
664{
665 struct regulator_dev *rdev = dev_get_drvdata(dev);
666
667 return regulator_print_opmode(buf,
668 rdev->constraints->state_standby.mode);
669}
670static DEVICE_ATTR(suspend_standby_mode, 0444,
671 regulator_suspend_standby_mode_show, NULL);
672
673static ssize_t regulator_suspend_mem_state_show(struct device *dev,
674 struct device_attribute *attr, char *buf)
675{
676 struct regulator_dev *rdev = dev_get_drvdata(dev);
677
678 return regulator_print_state(buf,
679 rdev->constraints->state_mem.enabled);
680}
681static DEVICE_ATTR(suspend_mem_state, 0444,
682 regulator_suspend_mem_state_show, NULL);
683
684static ssize_t regulator_suspend_disk_state_show(struct device *dev,
685 struct device_attribute *attr, char *buf)
686{
687 struct regulator_dev *rdev = dev_get_drvdata(dev);
688
689 return regulator_print_state(buf,
690 rdev->constraints->state_disk.enabled);
691}
692static DEVICE_ATTR(suspend_disk_state, 0444,
693 regulator_suspend_disk_state_show, NULL);
694
695static ssize_t regulator_suspend_standby_state_show(struct device *dev,
696 struct device_attribute *attr, char *buf)
697{
698 struct regulator_dev *rdev = dev_get_drvdata(dev);
699
700 return regulator_print_state(buf,
701 rdev->constraints->state_standby.enabled);
702}
703static DEVICE_ATTR(suspend_standby_state, 0444,
704 regulator_suspend_standby_state_show, NULL);
705
706static ssize_t regulator_bypass_show(struct device *dev,
707 struct device_attribute *attr, char *buf)
708{
709 struct regulator_dev *rdev = dev_get_drvdata(dev);
710 const char *report;
711 bool bypass;
712 int ret;
713
714 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
715
716 if (ret != 0)
717 report = "unknown";
718 else if (bypass)
719 report = "enabled";
720 else
721 report = "disabled";
722
723 return sprintf(buf, "%s\n", report);
724}
725static DEVICE_ATTR(bypass, 0444,
726 regulator_bypass_show, NULL);
727
728
729
730static int drms_uA_update(struct regulator_dev *rdev)
731{
732 struct regulator *sibling;
733 int current_uA = 0, output_uV, input_uV, err;
734 unsigned int mode;
735
736 lockdep_assert_held_once(&rdev->mutex);
737
738
739
740
741
742 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
743 return 0;
744
745 if (!rdev->desc->ops->get_optimum_mode &&
746 !rdev->desc->ops->set_load)
747 return 0;
748
749 if (!rdev->desc->ops->set_mode &&
750 !rdev->desc->ops->set_load)
751 return -EINVAL;
752
753
754 list_for_each_entry(sibling, &rdev->consumer_list, list)
755 current_uA += sibling->uA_load;
756
757 current_uA += rdev->constraints->system_load;
758
759 if (rdev->desc->ops->set_load) {
760
761 err = rdev->desc->ops->set_load(rdev, current_uA);
762 if (err < 0)
763 rdev_err(rdev, "failed to set load %d\n", current_uA);
764 } else {
765
766 output_uV = _regulator_get_voltage(rdev);
767 if (output_uV <= 0) {
768 rdev_err(rdev, "invalid output voltage found\n");
769 return -EINVAL;
770 }
771
772
773 input_uV = 0;
774 if (rdev->supply)
775 input_uV = regulator_get_voltage(rdev->supply);
776 if (input_uV <= 0)
777 input_uV = rdev->constraints->input_uV;
778 if (input_uV <= 0) {
779 rdev_err(rdev, "invalid input voltage found\n");
780 return -EINVAL;
781 }
782
783
784 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
785 output_uV, current_uA);
786
787
788 err = regulator_mode_constrain(rdev, &mode);
789 if (err < 0) {
790 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
791 current_uA, input_uV, output_uV);
792 return err;
793 }
794
795 err = rdev->desc->ops->set_mode(rdev, mode);
796 if (err < 0)
797 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
798 }
799
800 return err;
801}
802
803static int suspend_set_state(struct regulator_dev *rdev,
804 suspend_state_t state)
805{
806 int ret = 0;
807 struct regulator_state *rstate;
808
809 rstate = regulator_get_suspend_state(rdev, state);
810 if (rstate == NULL)
811 return 0;
812
813
814
815
816
817 if (rstate->enabled != ENABLE_IN_SUSPEND &&
818 rstate->enabled != DISABLE_IN_SUSPEND) {
819 if (rdev->desc->ops->set_suspend_voltage ||
820 rdev->desc->ops->set_suspend_mode)
821 rdev_warn(rdev, "No configuration\n");
822 return 0;
823 }
824
825 if (rstate->enabled == ENABLE_IN_SUSPEND &&
826 rdev->desc->ops->set_suspend_enable)
827 ret = rdev->desc->ops->set_suspend_enable(rdev);
828 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
829 rdev->desc->ops->set_suspend_disable)
830 ret = rdev->desc->ops->set_suspend_disable(rdev);
831 else
832 ret = 0;
833
834 if (ret < 0) {
835 rdev_err(rdev, "failed to enabled/disable\n");
836 return ret;
837 }
838
839 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
840 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
841 if (ret < 0) {
842 rdev_err(rdev, "failed to set voltage\n");
843 return ret;
844 }
845 }
846
847 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
848 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
849 if (ret < 0) {
850 rdev_err(rdev, "failed to set mode\n");
851 return ret;
852 }
853 }
854
855 return ret;
856}
857
858static void print_constraints(struct regulator_dev *rdev)
859{
860 struct regulation_constraints *constraints = rdev->constraints;
861 char buf[160] = "";
862 size_t len = sizeof(buf) - 1;
863 int count = 0;
864 int ret;
865
866 if (constraints->min_uV && constraints->max_uV) {
867 if (constraints->min_uV == constraints->max_uV)
868 count += scnprintf(buf + count, len - count, "%d mV ",
869 constraints->min_uV / 1000);
870 else
871 count += scnprintf(buf + count, len - count,
872 "%d <--> %d mV ",
873 constraints->min_uV / 1000,
874 constraints->max_uV / 1000);
875 }
876
877 if (!constraints->min_uV ||
878 constraints->min_uV != constraints->max_uV) {
879 ret = _regulator_get_voltage(rdev);
880 if (ret > 0)
881 count += scnprintf(buf + count, len - count,
882 "at %d mV ", ret / 1000);
883 }
884
885 if (constraints->uV_offset)
886 count += scnprintf(buf + count, len - count, "%dmV offset ",
887 constraints->uV_offset / 1000);
888
889 if (constraints->min_uA && constraints->max_uA) {
890 if (constraints->min_uA == constraints->max_uA)
891 count += scnprintf(buf + count, len - count, "%d mA ",
892 constraints->min_uA / 1000);
893 else
894 count += scnprintf(buf + count, len - count,
895 "%d <--> %d mA ",
896 constraints->min_uA / 1000,
897 constraints->max_uA / 1000);
898 }
899
900 if (!constraints->min_uA ||
901 constraints->min_uA != constraints->max_uA) {
902 ret = _regulator_get_current_limit(rdev);
903 if (ret > 0)
904 count += scnprintf(buf + count, len - count,
905 "at %d mA ", ret / 1000);
906 }
907
908 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
909 count += scnprintf(buf + count, len - count, "fast ");
910 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
911 count += scnprintf(buf + count, len - count, "normal ");
912 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
913 count += scnprintf(buf + count, len - count, "idle ");
914 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
915 count += scnprintf(buf + count, len - count, "standby");
916
917 if (!count)
918 scnprintf(buf, len, "no parameters");
919
920 rdev_dbg(rdev, "%s\n", buf);
921
922 if ((constraints->min_uV != constraints->max_uV) &&
923 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
924 rdev_warn(rdev,
925 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
926}
927
928static int machine_constraints_voltage(struct regulator_dev *rdev,
929 struct regulation_constraints *constraints)
930{
931 const struct regulator_ops *ops = rdev->desc->ops;
932 int ret;
933
934
935 if (rdev->constraints->apply_uV &&
936 rdev->constraints->min_uV && rdev->constraints->max_uV) {
937 int target_min, target_max;
938 int current_uV = _regulator_get_voltage(rdev);
939
940 if (current_uV == -ENOTRECOVERABLE) {
941
942 rdev_info(rdev, "Setting %d-%duV\n",
943 rdev->constraints->min_uV,
944 rdev->constraints->max_uV);
945 _regulator_do_set_voltage(rdev,
946 rdev->constraints->min_uV,
947 rdev->constraints->max_uV);
948 current_uV = _regulator_get_voltage(rdev);
949 }
950
951 if (current_uV < 0) {
952 rdev_err(rdev,
953 "failed to get the current voltage(%d)\n",
954 current_uV);
955 return current_uV;
956 }
957
958
959
960
961
962
963 target_min = current_uV;
964 target_max = current_uV;
965
966 if (current_uV < rdev->constraints->min_uV) {
967 target_min = rdev->constraints->min_uV;
968 target_max = rdev->constraints->min_uV;
969 }
970
971 if (current_uV > rdev->constraints->max_uV) {
972 target_min = rdev->constraints->max_uV;
973 target_max = rdev->constraints->max_uV;
974 }
975
976 if (target_min != current_uV || target_max != current_uV) {
977 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
978 current_uV, target_min, target_max);
979 ret = _regulator_do_set_voltage(
980 rdev, target_min, target_max);
981 if (ret < 0) {
982 rdev_err(rdev,
983 "failed to apply %d-%duV constraint(%d)\n",
984 target_min, target_max, ret);
985 return ret;
986 }
987 }
988 }
989
990
991
992
993 if (ops->list_voltage && rdev->desc->n_voltages) {
994 int count = rdev->desc->n_voltages;
995 int i;
996 int min_uV = INT_MAX;
997 int max_uV = INT_MIN;
998 int cmin = constraints->min_uV;
999 int cmax = constraints->max_uV;
1000
1001
1002
1003 if (count == 1 && !cmin) {
1004 cmin = 1;
1005 cmax = INT_MAX;
1006 constraints->min_uV = cmin;
1007 constraints->max_uV = cmax;
1008 }
1009
1010
1011 if ((cmin == 0) && (cmax == 0))
1012 return 0;
1013
1014
1015 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1016 rdev_err(rdev, "invalid voltage constraints\n");
1017 return -EINVAL;
1018 }
1019
1020
1021 for (i = 0; i < count; i++) {
1022 int value;
1023
1024 value = ops->list_voltage(rdev, i);
1025 if (value <= 0)
1026 continue;
1027
1028
1029 if (value >= cmin && value < min_uV)
1030 min_uV = value;
1031 if (value <= cmax && value > max_uV)
1032 max_uV = value;
1033 }
1034
1035
1036 if (max_uV < min_uV) {
1037 rdev_err(rdev,
1038 "unsupportable voltage constraints %u-%uuV\n",
1039 min_uV, max_uV);
1040 return -EINVAL;
1041 }
1042
1043
1044 if (constraints->min_uV < min_uV) {
1045 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1046 constraints->min_uV, min_uV);
1047 constraints->min_uV = min_uV;
1048 }
1049 if (constraints->max_uV > max_uV) {
1050 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1051 constraints->max_uV, max_uV);
1052 constraints->max_uV = max_uV;
1053 }
1054 }
1055
1056 return 0;
1057}
1058
1059static int machine_constraints_current(struct regulator_dev *rdev,
1060 struct regulation_constraints *constraints)
1061{
1062 const struct regulator_ops *ops = rdev->desc->ops;
1063 int ret;
1064
1065 if (!constraints->min_uA && !constraints->max_uA)
1066 return 0;
1067
1068 if (constraints->min_uA > constraints->max_uA) {
1069 rdev_err(rdev, "Invalid current constraints\n");
1070 return -EINVAL;
1071 }
1072
1073 if (!ops->set_current_limit || !ops->get_current_limit) {
1074 rdev_warn(rdev, "Operation of current configuration missing\n");
1075 return 0;
1076 }
1077
1078
1079 ret = ops->set_current_limit(rdev, constraints->min_uA,
1080 constraints->max_uA);
1081 if (ret < 0) {
1082 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1083 return ret;
1084 }
1085
1086 return 0;
1087}
1088
1089static int _regulator_do_enable(struct regulator_dev *rdev);
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102static int set_machine_constraints(struct regulator_dev *rdev,
1103 const struct regulation_constraints *constraints)
1104{
1105 int ret = 0;
1106 const struct regulator_ops *ops = rdev->desc->ops;
1107
1108 if (constraints)
1109 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1110 GFP_KERNEL);
1111 else
1112 rdev->constraints = kzalloc(sizeof(*constraints),
1113 GFP_KERNEL);
1114 if (!rdev->constraints)
1115 return -ENOMEM;
1116
1117 ret = machine_constraints_voltage(rdev, rdev->constraints);
1118 if (ret != 0)
1119 return ret;
1120
1121 ret = machine_constraints_current(rdev, rdev->constraints);
1122 if (ret != 0)
1123 return ret;
1124
1125 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1126 ret = ops->set_input_current_limit(rdev,
1127 rdev->constraints->ilim_uA);
1128 if (ret < 0) {
1129 rdev_err(rdev, "failed to set input limit\n");
1130 return ret;
1131 }
1132 }
1133
1134
1135 if (rdev->constraints->initial_state) {
1136 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1137 if (ret < 0) {
1138 rdev_err(rdev, "failed to set suspend state\n");
1139 return ret;
1140 }
1141 }
1142
1143 if (rdev->constraints->initial_mode) {
1144 if (!ops->set_mode) {
1145 rdev_err(rdev, "no set_mode operation\n");
1146 return -EINVAL;
1147 }
1148
1149 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1150 if (ret < 0) {
1151 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1152 return ret;
1153 }
1154 }
1155
1156
1157
1158
1159 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1160 ret = _regulator_do_enable(rdev);
1161 if (ret < 0 && ret != -EINVAL) {
1162 rdev_err(rdev, "failed to enable\n");
1163 return ret;
1164 }
1165 }
1166
1167 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1168 && ops->set_ramp_delay) {
1169 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1170 if (ret < 0) {
1171 rdev_err(rdev, "failed to set ramp_delay\n");
1172 return ret;
1173 }
1174 }
1175
1176 if (rdev->constraints->pull_down && ops->set_pull_down) {
1177 ret = ops->set_pull_down(rdev);
1178 if (ret < 0) {
1179 rdev_err(rdev, "failed to set pull down\n");
1180 return ret;
1181 }
1182 }
1183
1184 if (rdev->constraints->soft_start && ops->set_soft_start) {
1185 ret = ops->set_soft_start(rdev);
1186 if (ret < 0) {
1187 rdev_err(rdev, "failed to set soft start\n");
1188 return ret;
1189 }
1190 }
1191
1192 if (rdev->constraints->over_current_protection
1193 && ops->set_over_current_protection) {
1194 ret = ops->set_over_current_protection(rdev);
1195 if (ret < 0) {
1196 rdev_err(rdev, "failed to set over current protection\n");
1197 return ret;
1198 }
1199 }
1200
1201 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1202 bool ad_state = (rdev->constraints->active_discharge ==
1203 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1204
1205 ret = ops->set_active_discharge(rdev, ad_state);
1206 if (ret < 0) {
1207 rdev_err(rdev, "failed to set active discharge\n");
1208 return ret;
1209 }
1210 }
1211
1212 print_constraints(rdev);
1213 return 0;
1214}
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225static int set_supply(struct regulator_dev *rdev,
1226 struct regulator_dev *supply_rdev)
1227{
1228 int err;
1229
1230 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1231
1232 if (!try_module_get(supply_rdev->owner))
1233 return -ENODEV;
1234
1235 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1236 if (rdev->supply == NULL) {
1237 err = -ENOMEM;
1238 return err;
1239 }
1240 supply_rdev->open_count++;
1241
1242 return 0;
1243}
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256static int set_consumer_device_supply(struct regulator_dev *rdev,
1257 const char *consumer_dev_name,
1258 const char *supply)
1259{
1260 struct regulator_map *node;
1261 int has_dev;
1262
1263 if (supply == NULL)
1264 return -EINVAL;
1265
1266 if (consumer_dev_name != NULL)
1267 has_dev = 1;
1268 else
1269 has_dev = 0;
1270
1271 list_for_each_entry(node, ®ulator_map_list, list) {
1272 if (node->dev_name && consumer_dev_name) {
1273 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1274 continue;
1275 } else if (node->dev_name || consumer_dev_name) {
1276 continue;
1277 }
1278
1279 if (strcmp(node->supply, supply) != 0)
1280 continue;
1281
1282 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1283 consumer_dev_name,
1284 dev_name(&node->regulator->dev),
1285 node->regulator->desc->name,
1286 supply,
1287 dev_name(&rdev->dev), rdev_get_name(rdev));
1288 return -EBUSY;
1289 }
1290
1291 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1292 if (node == NULL)
1293 return -ENOMEM;
1294
1295 node->regulator = rdev;
1296 node->supply = supply;
1297
1298 if (has_dev) {
1299 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1300 if (node->dev_name == NULL) {
1301 kfree(node);
1302 return -ENOMEM;
1303 }
1304 }
1305
1306 list_add(&node->list, ®ulator_map_list);
1307 return 0;
1308}
1309
1310static void unset_regulator_supplies(struct regulator_dev *rdev)
1311{
1312 struct regulator_map *node, *n;
1313
1314 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1315 if (rdev == node->regulator) {
1316 list_del(&node->list);
1317 kfree(node->dev_name);
1318 kfree(node);
1319 }
1320 }
1321}
1322
1323#ifdef CONFIG_DEBUG_FS
1324static ssize_t constraint_flags_read_file(struct file *file,
1325 char __user *user_buf,
1326 size_t count, loff_t *ppos)
1327{
1328 const struct regulator *regulator = file->private_data;
1329 const struct regulation_constraints *c = regulator->rdev->constraints;
1330 char *buf;
1331 ssize_t ret;
1332
1333 if (!c)
1334 return 0;
1335
1336 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1337 if (!buf)
1338 return -ENOMEM;
1339
1340 ret = snprintf(buf, PAGE_SIZE,
1341 "always_on: %u\n"
1342 "boot_on: %u\n"
1343 "apply_uV: %u\n"
1344 "ramp_disable: %u\n"
1345 "soft_start: %u\n"
1346 "pull_down: %u\n"
1347 "over_current_protection: %u\n",
1348 c->always_on,
1349 c->boot_on,
1350 c->apply_uV,
1351 c->ramp_disable,
1352 c->soft_start,
1353 c->pull_down,
1354 c->over_current_protection);
1355
1356 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1357 kfree(buf);
1358
1359 return ret;
1360}
1361
1362#endif
1363
1364static const struct file_operations constraint_flags_fops = {
1365#ifdef CONFIG_DEBUG_FS
1366 .open = simple_open,
1367 .read = constraint_flags_read_file,
1368 .llseek = default_llseek,
1369#endif
1370};
1371
1372#define REG_STR_SIZE 64
1373
1374static struct regulator *create_regulator(struct regulator_dev *rdev,
1375 struct device *dev,
1376 const char *supply_name)
1377{
1378 struct regulator *regulator;
1379 char buf[REG_STR_SIZE];
1380 int err, size;
1381
1382 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1383 if (regulator == NULL)
1384 return NULL;
1385
1386 regulator_lock(rdev);
1387 regulator->rdev = rdev;
1388 list_add(®ulator->list, &rdev->consumer_list);
1389
1390 if (dev) {
1391 regulator->dev = dev;
1392
1393
1394 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1395 dev->kobj.name, supply_name);
1396 if (size >= REG_STR_SIZE)
1397 goto overflow_err;
1398
1399 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1400 if (regulator->supply_name == NULL)
1401 goto overflow_err;
1402
1403 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1404 buf);
1405 if (err) {
1406 rdev_dbg(rdev, "could not add device link %s err %d\n",
1407 dev->kobj.name, err);
1408
1409 }
1410 } else {
1411 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1412 if (regulator->supply_name == NULL)
1413 goto overflow_err;
1414 }
1415
1416 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1417 rdev->debugfs);
1418 if (!regulator->debugfs) {
1419 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1420 } else {
1421 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1422 ®ulator->uA_load);
1423 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1424 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1425 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1426 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1427 debugfs_create_file("constraint_flags", 0444,
1428 regulator->debugfs, regulator,
1429 &constraint_flags_fops);
1430 }
1431
1432
1433
1434
1435
1436
1437 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1438 _regulator_is_enabled(rdev))
1439 regulator->always_on = true;
1440
1441 regulator_unlock(rdev);
1442 return regulator;
1443overflow_err:
1444 list_del(®ulator->list);
1445 kfree(regulator);
1446 regulator_unlock(rdev);
1447 return NULL;
1448}
1449
1450static int _regulator_get_enable_time(struct regulator_dev *rdev)
1451{
1452 if (rdev->constraints && rdev->constraints->enable_time)
1453 return rdev->constraints->enable_time;
1454 if (!rdev->desc->ops->enable_time)
1455 return rdev->desc->enable_time;
1456 return rdev->desc->ops->enable_time(rdev);
1457}
1458
1459static struct regulator_supply_alias *regulator_find_supply_alias(
1460 struct device *dev, const char *supply)
1461{
1462 struct regulator_supply_alias *map;
1463
1464 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1465 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1466 return map;
1467
1468 return NULL;
1469}
1470
1471static void regulator_supply_alias(struct device **dev, const char **supply)
1472{
1473 struct regulator_supply_alias *map;
1474
1475 map = regulator_find_supply_alias(*dev, *supply);
1476 if (map) {
1477 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1478 *supply, map->alias_supply,
1479 dev_name(map->alias_dev));
1480 *dev = map->alias_dev;
1481 *supply = map->alias_supply;
1482 }
1483}
1484
1485static int regulator_match(struct device *dev, const void *data)
1486{
1487 struct regulator_dev *r = dev_to_rdev(dev);
1488
1489 return strcmp(rdev_get_name(r), data) == 0;
1490}
1491
1492static struct regulator_dev *regulator_lookup_by_name(const char *name)
1493{
1494 struct device *dev;
1495
1496 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1497
1498 return dev ? dev_to_rdev(dev) : NULL;
1499}
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1514 const char *supply)
1515{
1516 struct regulator_dev *r = NULL;
1517 struct device_node *node;
1518 struct regulator_map *map;
1519 const char *devname = NULL;
1520
1521 regulator_supply_alias(&dev, &supply);
1522
1523
1524 if (dev && dev->of_node) {
1525 node = of_get_regulator(dev, supply);
1526 if (node) {
1527 r = of_find_regulator_by_node(node);
1528 if (r)
1529 return r;
1530
1531
1532
1533
1534
1535 return ERR_PTR(-EPROBE_DEFER);
1536 }
1537 }
1538
1539
1540 if (dev)
1541 devname = dev_name(dev);
1542
1543 mutex_lock(®ulator_list_mutex);
1544 list_for_each_entry(map, ®ulator_map_list, list) {
1545
1546 if (map->dev_name &&
1547 (!devname || strcmp(map->dev_name, devname)))
1548 continue;
1549
1550 if (strcmp(map->supply, supply) == 0 &&
1551 get_device(&map->regulator->dev)) {
1552 r = map->regulator;
1553 break;
1554 }
1555 }
1556 mutex_unlock(®ulator_list_mutex);
1557
1558 if (r)
1559 return r;
1560
1561 r = regulator_lookup_by_name(supply);
1562 if (r)
1563 return r;
1564
1565 return ERR_PTR(-ENODEV);
1566}
1567
1568static int regulator_resolve_supply(struct regulator_dev *rdev)
1569{
1570 struct regulator_dev *r;
1571 struct device *dev = rdev->dev.parent;
1572 int ret;
1573
1574
1575 if (!rdev->supply_name)
1576 return 0;
1577
1578
1579 if (rdev->supply)
1580 return 0;
1581
1582 r = regulator_dev_lookup(dev, rdev->supply_name);
1583 if (IS_ERR(r)) {
1584 ret = PTR_ERR(r);
1585
1586
1587 if (ret == -EPROBE_DEFER)
1588 return ret;
1589
1590 if (have_full_constraints()) {
1591 r = dummy_regulator_rdev;
1592 get_device(&r->dev);
1593 } else {
1594 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1595 rdev->supply_name, rdev->desc->name);
1596 return -EPROBE_DEFER;
1597 }
1598 }
1599
1600
1601
1602
1603
1604
1605
1606 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1607 if (!device_is_bound(r->dev.parent)) {
1608 put_device(&r->dev);
1609 return -EPROBE_DEFER;
1610 }
1611 }
1612
1613
1614 ret = regulator_resolve_supply(r);
1615 if (ret < 0) {
1616 put_device(&r->dev);
1617 return ret;
1618 }
1619
1620 ret = set_supply(rdev, r);
1621 if (ret < 0) {
1622 put_device(&r->dev);
1623 return ret;
1624 }
1625
1626
1627 if (_regulator_is_enabled(rdev)) {
1628 ret = regulator_enable(rdev->supply);
1629 if (ret < 0) {
1630 _regulator_put(rdev->supply);
1631 rdev->supply = NULL;
1632 return ret;
1633 }
1634 }
1635
1636 return 0;
1637}
1638
1639
1640struct regulator *_regulator_get(struct device *dev, const char *id,
1641 enum regulator_get_type get_type)
1642{
1643 struct regulator_dev *rdev;
1644 struct regulator *regulator;
1645 const char *devname = dev ? dev_name(dev) : "deviceless";
1646 int ret;
1647
1648 if (get_type >= MAX_GET_TYPE) {
1649 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1650 return ERR_PTR(-EINVAL);
1651 }
1652
1653 if (id == NULL) {
1654 pr_err("get() with no identifier\n");
1655 return ERR_PTR(-EINVAL);
1656 }
1657
1658 rdev = regulator_dev_lookup(dev, id);
1659 if (IS_ERR(rdev)) {
1660 ret = PTR_ERR(rdev);
1661
1662
1663
1664
1665
1666 if (ret != -ENODEV)
1667 return ERR_PTR(ret);
1668
1669 if (!have_full_constraints()) {
1670 dev_warn(dev,
1671 "incomplete constraints, dummy supplies not allowed\n");
1672 return ERR_PTR(-ENODEV);
1673 }
1674
1675 switch (get_type) {
1676 case NORMAL_GET:
1677
1678
1679
1680
1681
1682 dev_warn(dev,
1683 "%s supply %s not found, using dummy regulator\n",
1684 devname, id);
1685 rdev = dummy_regulator_rdev;
1686 get_device(&rdev->dev);
1687 break;
1688
1689 case EXCLUSIVE_GET:
1690 dev_warn(dev,
1691 "dummy supplies not allowed for exclusive requests\n");
1692
1693
1694 default:
1695 return ERR_PTR(-ENODEV);
1696 }
1697 }
1698
1699 if (rdev->exclusive) {
1700 regulator = ERR_PTR(-EPERM);
1701 put_device(&rdev->dev);
1702 return regulator;
1703 }
1704
1705 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1706 regulator = ERR_PTR(-EBUSY);
1707 put_device(&rdev->dev);
1708 return regulator;
1709 }
1710
1711 ret = regulator_resolve_supply(rdev);
1712 if (ret < 0) {
1713 regulator = ERR_PTR(ret);
1714 put_device(&rdev->dev);
1715 return regulator;
1716 }
1717
1718 if (!try_module_get(rdev->owner)) {
1719 regulator = ERR_PTR(-EPROBE_DEFER);
1720 put_device(&rdev->dev);
1721 return regulator;
1722 }
1723
1724 regulator = create_regulator(rdev, dev, id);
1725 if (regulator == NULL) {
1726 regulator = ERR_PTR(-ENOMEM);
1727 put_device(&rdev->dev);
1728 module_put(rdev->owner);
1729 return regulator;
1730 }
1731
1732 rdev->open_count++;
1733 if (get_type == EXCLUSIVE_GET) {
1734 rdev->exclusive = 1;
1735
1736 ret = _regulator_is_enabled(rdev);
1737 if (ret > 0)
1738 rdev->use_count = 1;
1739 else
1740 rdev->use_count = 0;
1741 }
1742
1743 return regulator;
1744}
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759struct regulator *regulator_get(struct device *dev, const char *id)
1760{
1761 return _regulator_get(dev, id, NORMAL_GET);
1762}
1763EXPORT_SYMBOL_GPL(regulator_get);
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1787{
1788 return _regulator_get(dev, id, EXCLUSIVE_GET);
1789}
1790EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812struct regulator *regulator_get_optional(struct device *dev, const char *id)
1813{
1814 return _regulator_get(dev, id, OPTIONAL_GET);
1815}
1816EXPORT_SYMBOL_GPL(regulator_get_optional);
1817
1818
1819static void _regulator_put(struct regulator *regulator)
1820{
1821 struct regulator_dev *rdev;
1822
1823 if (IS_ERR_OR_NULL(regulator))
1824 return;
1825
1826 lockdep_assert_held_once(®ulator_list_mutex);
1827
1828 rdev = regulator->rdev;
1829
1830 debugfs_remove_recursive(regulator->debugfs);
1831
1832
1833 if (regulator->dev)
1834 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1835 regulator_lock(rdev);
1836 list_del(®ulator->list);
1837
1838 rdev->open_count--;
1839 rdev->exclusive = 0;
1840 put_device(&rdev->dev);
1841 regulator_unlock(rdev);
1842
1843 kfree_const(regulator->supply_name);
1844 kfree(regulator);
1845
1846 module_put(rdev->owner);
1847}
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857void regulator_put(struct regulator *regulator)
1858{
1859 mutex_lock(®ulator_list_mutex);
1860 _regulator_put(regulator);
1861 mutex_unlock(®ulator_list_mutex);
1862}
1863EXPORT_SYMBOL_GPL(regulator_put);
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877int regulator_register_supply_alias(struct device *dev, const char *id,
1878 struct device *alias_dev,
1879 const char *alias_id)
1880{
1881 struct regulator_supply_alias *map;
1882
1883 map = regulator_find_supply_alias(dev, id);
1884 if (map)
1885 return -EEXIST;
1886
1887 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1888 if (!map)
1889 return -ENOMEM;
1890
1891 map->src_dev = dev;
1892 map->src_supply = id;
1893 map->alias_dev = alias_dev;
1894 map->alias_supply = alias_id;
1895
1896 list_add(&map->list, ®ulator_supply_alias_list);
1897
1898 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1899 id, dev_name(dev), alias_id, dev_name(alias_dev));
1900
1901 return 0;
1902}
1903EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913void regulator_unregister_supply_alias(struct device *dev, const char *id)
1914{
1915 struct regulator_supply_alias *map;
1916
1917 map = regulator_find_supply_alias(dev, id);
1918 if (map) {
1919 list_del(&map->list);
1920 kfree(map);
1921 }
1922}
1923EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942int regulator_bulk_register_supply_alias(struct device *dev,
1943 const char *const *id,
1944 struct device *alias_dev,
1945 const char *const *alias_id,
1946 int num_id)
1947{
1948 int i;
1949 int ret;
1950
1951 for (i = 0; i < num_id; ++i) {
1952 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1953 alias_id[i]);
1954 if (ret < 0)
1955 goto err;
1956 }
1957
1958 return 0;
1959
1960err:
1961 dev_err(dev,
1962 "Failed to create supply alias %s,%s -> %s,%s\n",
1963 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1964
1965 while (--i >= 0)
1966 regulator_unregister_supply_alias(dev, id[i]);
1967
1968 return ret;
1969}
1970EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982void regulator_bulk_unregister_supply_alias(struct device *dev,
1983 const char *const *id,
1984 int num_id)
1985{
1986 int i;
1987
1988 for (i = 0; i < num_id; ++i)
1989 regulator_unregister_supply_alias(dev, id[i]);
1990}
1991EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1992
1993
1994
1995static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1996 const struct regulator_config *config)
1997{
1998 struct regulator_enable_gpio *pin;
1999 struct gpio_desc *gpiod;
2000 int ret;
2001
2002 if (config->ena_gpiod)
2003 gpiod = config->ena_gpiod;
2004 else
2005 gpiod = gpio_to_desc(config->ena_gpio);
2006
2007 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2008 if (pin->gpiod == gpiod) {
2009 rdev_dbg(rdev, "GPIO %d is already used\n",
2010 config->ena_gpio);
2011 goto update_ena_gpio_to_rdev;
2012 }
2013 }
2014
2015 if (!config->ena_gpiod) {
2016 ret = gpio_request_one(config->ena_gpio,
2017 GPIOF_DIR_OUT | config->ena_gpio_flags,
2018 rdev_get_name(rdev));
2019 if (ret)
2020 return ret;
2021 }
2022
2023 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2024 if (pin == NULL) {
2025 if (!config->ena_gpiod)
2026 gpio_free(config->ena_gpio);
2027 return -ENOMEM;
2028 }
2029
2030 pin->gpiod = gpiod;
2031 pin->ena_gpio_invert = config->ena_gpio_invert;
2032 list_add(&pin->list, ®ulator_ena_gpio_list);
2033
2034update_ena_gpio_to_rdev:
2035 pin->request_count++;
2036 rdev->ena_pin = pin;
2037 return 0;
2038}
2039
2040static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2041{
2042 struct regulator_enable_gpio *pin, *n;
2043
2044 if (!rdev->ena_pin)
2045 return;
2046
2047
2048 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2049 if (pin->gpiod == rdev->ena_pin->gpiod) {
2050 if (pin->request_count <= 1) {
2051 pin->request_count = 0;
2052 gpiod_put(pin->gpiod);
2053 list_del(&pin->list);
2054 kfree(pin);
2055 rdev->ena_pin = NULL;
2056 return;
2057 } else {
2058 pin->request_count--;
2059 }
2060 }
2061 }
2062}
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2073{
2074 struct regulator_enable_gpio *pin = rdev->ena_pin;
2075
2076 if (!pin)
2077 return -EINVAL;
2078
2079 if (enable) {
2080
2081 if (pin->enable_count == 0)
2082 gpiod_set_value_cansleep(pin->gpiod,
2083 !pin->ena_gpio_invert);
2084
2085 pin->enable_count++;
2086 } else {
2087 if (pin->enable_count > 1) {
2088 pin->enable_count--;
2089 return 0;
2090 }
2091
2092
2093 if (pin->enable_count <= 1) {
2094 gpiod_set_value_cansleep(pin->gpiod,
2095 pin->ena_gpio_invert);
2096 pin->enable_count = 0;
2097 }
2098 }
2099
2100 return 0;
2101}
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114static void _regulator_enable_delay(unsigned int delay)
2115{
2116 unsigned int ms = delay / 1000;
2117 unsigned int us = delay % 1000;
2118
2119 if (ms > 0) {
2120
2121
2122
2123
2124 if (ms < 20)
2125 us += ms * 1000;
2126 else
2127 msleep(ms);
2128 }
2129
2130
2131
2132
2133
2134
2135
2136 if (us >= 10)
2137 usleep_range(us, us + 100);
2138 else
2139 udelay(us);
2140}
2141
2142static int _regulator_do_enable(struct regulator_dev *rdev)
2143{
2144 int ret, delay;
2145
2146
2147 ret = _regulator_get_enable_time(rdev);
2148 if (ret >= 0) {
2149 delay = ret;
2150 } else {
2151 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2152 delay = 0;
2153 }
2154
2155 trace_regulator_enable(rdev_get_name(rdev));
2156
2157 if (rdev->desc->off_on_delay) {
2158
2159
2160
2161 unsigned long start_jiffy = jiffies;
2162 unsigned long intended, max_delay, remaining;
2163
2164 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2165 intended = rdev->last_off_jiffy + max_delay;
2166
2167 if (time_before(start_jiffy, intended)) {
2168
2169
2170
2171
2172
2173
2174
2175 remaining = intended - start_jiffy;
2176 if (remaining <= max_delay)
2177 _regulator_enable_delay(
2178 jiffies_to_usecs(remaining));
2179 }
2180 }
2181
2182 if (rdev->ena_pin) {
2183 if (!rdev->ena_gpio_state) {
2184 ret = regulator_ena_gpio_ctrl(rdev, true);
2185 if (ret < 0)
2186 return ret;
2187 rdev->ena_gpio_state = 1;
2188 }
2189 } else if (rdev->desc->ops->enable) {
2190 ret = rdev->desc->ops->enable(rdev);
2191 if (ret < 0)
2192 return ret;
2193 } else {
2194 return -EINVAL;
2195 }
2196
2197
2198
2199
2200 trace_regulator_enable_delay(rdev_get_name(rdev));
2201
2202 _regulator_enable_delay(delay);
2203
2204 trace_regulator_enable_complete(rdev_get_name(rdev));
2205
2206 return 0;
2207}
2208
2209
2210static int _regulator_enable(struct regulator_dev *rdev)
2211{
2212 int ret;
2213
2214 lockdep_assert_held_once(&rdev->mutex);
2215
2216
2217 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2218 drms_uA_update(rdev);
2219
2220 if (rdev->use_count == 0) {
2221
2222 ret = _regulator_is_enabled(rdev);
2223 if (ret == -EINVAL || ret == 0) {
2224 if (!regulator_ops_is_valid(rdev,
2225 REGULATOR_CHANGE_STATUS))
2226 return -EPERM;
2227
2228 ret = _regulator_do_enable(rdev);
2229 if (ret < 0)
2230 return ret;
2231
2232 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2233 NULL);
2234 } else if (ret < 0) {
2235 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2236 return ret;
2237 }
2238
2239 }
2240
2241 rdev->use_count++;
2242
2243 return 0;
2244}
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257int regulator_enable(struct regulator *regulator)
2258{
2259 struct regulator_dev *rdev = regulator->rdev;
2260 int ret = 0;
2261
2262 if (regulator->always_on)
2263 return 0;
2264
2265 if (rdev->supply) {
2266 ret = regulator_enable(rdev->supply);
2267 if (ret != 0)
2268 return ret;
2269 }
2270
2271 mutex_lock(&rdev->mutex);
2272 ret = _regulator_enable(rdev);
2273 mutex_unlock(&rdev->mutex);
2274
2275 if (ret != 0 && rdev->supply)
2276 regulator_disable(rdev->supply);
2277
2278 return ret;
2279}
2280EXPORT_SYMBOL_GPL(regulator_enable);
2281
2282static int _regulator_do_disable(struct regulator_dev *rdev)
2283{
2284 int ret;
2285
2286 trace_regulator_disable(rdev_get_name(rdev));
2287
2288 if (rdev->ena_pin) {
2289 if (rdev->ena_gpio_state) {
2290 ret = regulator_ena_gpio_ctrl(rdev, false);
2291 if (ret < 0)
2292 return ret;
2293 rdev->ena_gpio_state = 0;
2294 }
2295
2296 } else if (rdev->desc->ops->disable) {
2297 ret = rdev->desc->ops->disable(rdev);
2298 if (ret != 0)
2299 return ret;
2300 }
2301
2302
2303
2304
2305 if (rdev->desc->off_on_delay)
2306 rdev->last_off_jiffy = jiffies;
2307
2308 trace_regulator_disable_complete(rdev_get_name(rdev));
2309
2310 return 0;
2311}
2312
2313
2314static int _regulator_disable(struct regulator_dev *rdev)
2315{
2316 int ret = 0;
2317
2318 lockdep_assert_held_once(&rdev->mutex);
2319
2320 if (WARN(rdev->use_count <= 0,
2321 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2322 return -EIO;
2323
2324
2325 if (rdev->use_count == 1 &&
2326 (rdev->constraints && !rdev->constraints->always_on)) {
2327
2328
2329 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2330 ret = _notifier_call_chain(rdev,
2331 REGULATOR_EVENT_PRE_DISABLE,
2332 NULL);
2333 if (ret & NOTIFY_STOP_MASK)
2334 return -EINVAL;
2335
2336 ret = _regulator_do_disable(rdev);
2337 if (ret < 0) {
2338 rdev_err(rdev, "failed to disable\n");
2339 _notifier_call_chain(rdev,
2340 REGULATOR_EVENT_ABORT_DISABLE,
2341 NULL);
2342 return ret;
2343 }
2344 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2345 NULL);
2346 }
2347
2348 rdev->use_count = 0;
2349 } else if (rdev->use_count > 1) {
2350 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2351 drms_uA_update(rdev);
2352
2353 rdev->use_count--;
2354 }
2355
2356 return ret;
2357}
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371int regulator_disable(struct regulator *regulator)
2372{
2373 struct regulator_dev *rdev = regulator->rdev;
2374 int ret = 0;
2375
2376 if (regulator->always_on)
2377 return 0;
2378
2379 mutex_lock(&rdev->mutex);
2380 ret = _regulator_disable(rdev);
2381 mutex_unlock(&rdev->mutex);
2382
2383 if (ret == 0 && rdev->supply)
2384 regulator_disable(rdev->supply);
2385
2386 return ret;
2387}
2388EXPORT_SYMBOL_GPL(regulator_disable);
2389
2390
2391static int _regulator_force_disable(struct regulator_dev *rdev)
2392{
2393 int ret = 0;
2394
2395 lockdep_assert_held_once(&rdev->mutex);
2396
2397 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2398 REGULATOR_EVENT_PRE_DISABLE, NULL);
2399 if (ret & NOTIFY_STOP_MASK)
2400 return -EINVAL;
2401
2402 ret = _regulator_do_disable(rdev);
2403 if (ret < 0) {
2404 rdev_err(rdev, "failed to force disable\n");
2405 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2406 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2407 return ret;
2408 }
2409
2410 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2411 REGULATOR_EVENT_DISABLE, NULL);
2412
2413 return 0;
2414}
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425int regulator_force_disable(struct regulator *regulator)
2426{
2427 struct regulator_dev *rdev = regulator->rdev;
2428 int ret;
2429
2430 mutex_lock(&rdev->mutex);
2431 regulator->uA_load = 0;
2432 ret = _regulator_force_disable(regulator->rdev);
2433 mutex_unlock(&rdev->mutex);
2434
2435 if (rdev->supply)
2436 while (rdev->open_count--)
2437 regulator_disable(rdev->supply);
2438
2439 return ret;
2440}
2441EXPORT_SYMBOL_GPL(regulator_force_disable);
2442
2443static void regulator_disable_work(struct work_struct *work)
2444{
2445 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2446 disable_work.work);
2447 int count, i, ret;
2448
2449 regulator_lock(rdev);
2450
2451 BUG_ON(!rdev->deferred_disables);
2452
2453 count = rdev->deferred_disables;
2454 rdev->deferred_disables = 0;
2455
2456
2457
2458
2459
2460
2461
2462 cancel_delayed_work(&rdev->disable_work);
2463
2464 for (i = 0; i < count; i++) {
2465 ret = _regulator_disable(rdev);
2466 if (ret != 0)
2467 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2468 }
2469
2470 regulator_unlock(rdev);
2471
2472 if (rdev->supply) {
2473 for (i = 0; i < count; i++) {
2474 ret = regulator_disable(rdev->supply);
2475 if (ret != 0) {
2476 rdev_err(rdev,
2477 "Supply disable failed: %d\n", ret);
2478 }
2479 }
2480 }
2481}
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495int regulator_disable_deferred(struct regulator *regulator, int ms)
2496{
2497 struct regulator_dev *rdev = regulator->rdev;
2498
2499 if (regulator->always_on)
2500 return 0;
2501
2502 if (!ms)
2503 return regulator_disable(regulator);
2504
2505 regulator_lock(rdev);
2506 rdev->deferred_disables++;
2507 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2508 msecs_to_jiffies(ms));
2509 regulator_unlock(rdev);
2510
2511 return 0;
2512}
2513EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2514
2515static int _regulator_is_enabled(struct regulator_dev *rdev)
2516{
2517
2518 if (rdev->ena_pin)
2519 return rdev->ena_gpio_state;
2520
2521
2522 if (!rdev->desc->ops->is_enabled)
2523 return 1;
2524
2525 return rdev->desc->ops->is_enabled(rdev);
2526}
2527
2528static int _regulator_list_voltage(struct regulator_dev *rdev,
2529 unsigned selector, int lock)
2530{
2531 const struct regulator_ops *ops = rdev->desc->ops;
2532 int ret;
2533
2534 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2535 return rdev->desc->fixed_uV;
2536
2537 if (ops->list_voltage) {
2538 if (selector >= rdev->desc->n_voltages)
2539 return -EINVAL;
2540 if (lock)
2541 regulator_lock(rdev);
2542 ret = ops->list_voltage(rdev, selector);
2543 if (lock)
2544 regulator_unlock(rdev);
2545 } else if (rdev->is_switch && rdev->supply) {
2546 ret = _regulator_list_voltage(rdev->supply->rdev,
2547 selector, lock);
2548 } else {
2549 return -EINVAL;
2550 }
2551
2552 if (ret > 0) {
2553 if (ret < rdev->constraints->min_uV)
2554 ret = 0;
2555 else if (ret > rdev->constraints->max_uV)
2556 ret = 0;
2557 }
2558
2559 return ret;
2560}
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574int regulator_is_enabled(struct regulator *regulator)
2575{
2576 int ret;
2577
2578 if (regulator->always_on)
2579 return 1;
2580
2581 mutex_lock(®ulator->rdev->mutex);
2582 ret = _regulator_is_enabled(regulator->rdev);
2583 mutex_unlock(®ulator->rdev->mutex);
2584
2585 return ret;
2586}
2587EXPORT_SYMBOL_GPL(regulator_is_enabled);
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597int regulator_count_voltages(struct regulator *regulator)
2598{
2599 struct regulator_dev *rdev = regulator->rdev;
2600
2601 if (rdev->desc->n_voltages)
2602 return rdev->desc->n_voltages;
2603
2604 if (!rdev->is_switch || !rdev->supply)
2605 return -EINVAL;
2606
2607 return regulator_count_voltages(rdev->supply);
2608}
2609EXPORT_SYMBOL_GPL(regulator_count_voltages);
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2622{
2623 return _regulator_list_voltage(regulator->rdev, selector, 1);
2624}
2625EXPORT_SYMBOL_GPL(regulator_list_voltage);
2626
2627
2628
2629
2630
2631
2632
2633
2634struct regmap *regulator_get_regmap(struct regulator *regulator)
2635{
2636 struct regmap *map = regulator->rdev->regmap;
2637
2638 return map ? map : ERR_PTR(-EOPNOTSUPP);
2639}
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655int regulator_get_hardware_vsel_register(struct regulator *regulator,
2656 unsigned *vsel_reg,
2657 unsigned *vsel_mask)
2658{
2659 struct regulator_dev *rdev = regulator->rdev;
2660 const struct regulator_ops *ops = rdev->desc->ops;
2661
2662 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2663 return -EOPNOTSUPP;
2664
2665 *vsel_reg = rdev->desc->vsel_reg;
2666 *vsel_mask = rdev->desc->vsel_mask;
2667
2668 return 0;
2669}
2670EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683int regulator_list_hardware_vsel(struct regulator *regulator,
2684 unsigned selector)
2685{
2686 struct regulator_dev *rdev = regulator->rdev;
2687 const struct regulator_ops *ops = rdev->desc->ops;
2688
2689 if (selector >= rdev->desc->n_voltages)
2690 return -EINVAL;
2691 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2692 return -EOPNOTSUPP;
2693
2694 return selector;
2695}
2696EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2697
2698
2699
2700
2701
2702
2703
2704
2705unsigned int regulator_get_linear_step(struct regulator *regulator)
2706{
2707 struct regulator_dev *rdev = regulator->rdev;
2708
2709 return rdev->desc->uV_step;
2710}
2711EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722int regulator_is_supported_voltage(struct regulator *regulator,
2723 int min_uV, int max_uV)
2724{
2725 struct regulator_dev *rdev = regulator->rdev;
2726 int i, voltages, ret;
2727
2728
2729 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2730 ret = regulator_get_voltage(regulator);
2731 if (ret >= 0)
2732 return min_uV <= ret && ret <= max_uV;
2733 else
2734 return ret;
2735 }
2736
2737
2738 if (rdev->desc->continuous_voltage_range)
2739 return min_uV >= rdev->constraints->min_uV &&
2740 max_uV <= rdev->constraints->max_uV;
2741
2742 ret = regulator_count_voltages(regulator);
2743 if (ret < 0)
2744 return ret;
2745 voltages = ret;
2746
2747 for (i = 0; i < voltages; i++) {
2748 ret = regulator_list_voltage(regulator, i);
2749
2750 if (ret >= min_uV && ret <= max_uV)
2751 return 1;
2752 }
2753
2754 return 0;
2755}
2756EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2757
2758static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2759 int max_uV)
2760{
2761 const struct regulator_desc *desc = rdev->desc;
2762
2763 if (desc->ops->map_voltage)
2764 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2765
2766 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2767 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2768
2769 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2770 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2771
2772 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2773}
2774
2775static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2776 int min_uV, int max_uV,
2777 unsigned *selector)
2778{
2779 struct pre_voltage_change_data data;
2780 int ret;
2781
2782 data.old_uV = _regulator_get_voltage(rdev);
2783 data.min_uV = min_uV;
2784 data.max_uV = max_uV;
2785 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2786 &data);
2787 if (ret & NOTIFY_STOP_MASK)
2788 return -EINVAL;
2789
2790 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2791 if (ret >= 0)
2792 return ret;
2793
2794 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2795 (void *)data.old_uV);
2796
2797 return ret;
2798}
2799
2800static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2801 int uV, unsigned selector)
2802{
2803 struct pre_voltage_change_data data;
2804 int ret;
2805
2806 data.old_uV = _regulator_get_voltage(rdev);
2807 data.min_uV = uV;
2808 data.max_uV = uV;
2809 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2810 &data);
2811 if (ret & NOTIFY_STOP_MASK)
2812 return -EINVAL;
2813
2814 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2815 if (ret >= 0)
2816 return ret;
2817
2818 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2819 (void *)data.old_uV);
2820
2821 return ret;
2822}
2823
2824static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2825 int old_uV, int new_uV)
2826{
2827 unsigned int ramp_delay = 0;
2828
2829 if (rdev->constraints->ramp_delay)
2830 ramp_delay = rdev->constraints->ramp_delay;
2831 else if (rdev->desc->ramp_delay)
2832 ramp_delay = rdev->desc->ramp_delay;
2833 else if (rdev->constraints->settling_time)
2834 return rdev->constraints->settling_time;
2835 else if (rdev->constraints->settling_time_up &&
2836 (new_uV > old_uV))
2837 return rdev->constraints->settling_time_up;
2838 else if (rdev->constraints->settling_time_down &&
2839 (new_uV < old_uV))
2840 return rdev->constraints->settling_time_down;
2841
2842 if (ramp_delay == 0) {
2843 rdev_dbg(rdev, "ramp_delay not set\n");
2844 return 0;
2845 }
2846
2847 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2848}
2849
2850static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2851 int min_uV, int max_uV)
2852{
2853 int ret;
2854 int delay = 0;
2855 int best_val = 0;
2856 unsigned int selector;
2857 int old_selector = -1;
2858 const struct regulator_ops *ops = rdev->desc->ops;
2859 int old_uV = _regulator_get_voltage(rdev);
2860
2861 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2862
2863 min_uV += rdev->constraints->uV_offset;
2864 max_uV += rdev->constraints->uV_offset;
2865
2866
2867
2868
2869
2870 if (_regulator_is_enabled(rdev) &&
2871 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2872 old_selector = ops->get_voltage_sel(rdev);
2873 if (old_selector < 0)
2874 return old_selector;
2875 }
2876
2877 if (ops->set_voltage) {
2878 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2879 &selector);
2880
2881 if (ret >= 0) {
2882 if (ops->list_voltage)
2883 best_val = ops->list_voltage(rdev,
2884 selector);
2885 else
2886 best_val = _regulator_get_voltage(rdev);
2887 }
2888
2889 } else if (ops->set_voltage_sel) {
2890 ret = regulator_map_voltage(rdev, min_uV, max_uV);
2891 if (ret >= 0) {
2892 best_val = ops->list_voltage(rdev, ret);
2893 if (min_uV <= best_val && max_uV >= best_val) {
2894 selector = ret;
2895 if (old_selector == selector)
2896 ret = 0;
2897 else
2898 ret = _regulator_call_set_voltage_sel(
2899 rdev, best_val, selector);
2900 } else {
2901 ret = -EINVAL;
2902 }
2903 }
2904 } else {
2905 ret = -EINVAL;
2906 }
2907
2908 if (ret)
2909 goto out;
2910
2911 if (ops->set_voltage_time_sel) {
2912
2913
2914
2915
2916 if (old_selector >= 0 && old_selector != selector)
2917 delay = ops->set_voltage_time_sel(rdev, old_selector,
2918 selector);
2919 } else {
2920 if (old_uV != best_val) {
2921 if (ops->set_voltage_time)
2922 delay = ops->set_voltage_time(rdev, old_uV,
2923 best_val);
2924 else
2925 delay = _regulator_set_voltage_time(rdev,
2926 old_uV,
2927 best_val);
2928 }
2929 }
2930
2931 if (delay < 0) {
2932 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2933 delay = 0;
2934 }
2935
2936
2937 if (delay >= 1000) {
2938 mdelay(delay / 1000);
2939 udelay(delay % 1000);
2940 } else if (delay) {
2941 udelay(delay);
2942 }
2943
2944 if (best_val >= 0) {
2945 unsigned long data = best_val;
2946
2947 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2948 (void *)data);
2949 }
2950
2951out:
2952 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2953
2954 return ret;
2955}
2956
2957static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
2958 int min_uV, int max_uV, suspend_state_t state)
2959{
2960 struct regulator_state *rstate;
2961 int uV, sel;
2962
2963 rstate = regulator_get_suspend_state(rdev, state);
2964 if (rstate == NULL)
2965 return -EINVAL;
2966
2967 if (min_uV < rstate->min_uV)
2968 min_uV = rstate->min_uV;
2969 if (max_uV > rstate->max_uV)
2970 max_uV = rstate->max_uV;
2971
2972 sel = regulator_map_voltage(rdev, min_uV, max_uV);
2973 if (sel < 0)
2974 return sel;
2975
2976 uV = rdev->desc->ops->list_voltage(rdev, sel);
2977 if (uV >= min_uV && uV <= max_uV)
2978 rstate->uV = uV;
2979
2980 return 0;
2981}
2982
2983static int regulator_set_voltage_unlocked(struct regulator *regulator,
2984 int min_uV, int max_uV,
2985 suspend_state_t state)
2986{
2987 struct regulator_dev *rdev = regulator->rdev;
2988 struct regulator_voltage *voltage = ®ulator->voltage[state];
2989 int ret = 0;
2990 int old_min_uV, old_max_uV;
2991 int current_uV;
2992 int best_supply_uV = 0;
2993 int supply_change_uV = 0;
2994
2995
2996
2997
2998
2999 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3000 goto out;
3001
3002
3003
3004
3005
3006 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3007 current_uV = _regulator_get_voltage(rdev);
3008 if (min_uV <= current_uV && current_uV <= max_uV) {
3009 voltage->min_uV = min_uV;
3010 voltage->max_uV = max_uV;
3011 goto out;
3012 }
3013 }
3014
3015
3016 if (!rdev->desc->ops->set_voltage &&
3017 !rdev->desc->ops->set_voltage_sel) {
3018 ret = -EINVAL;
3019 goto out;
3020 }
3021
3022
3023 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3024 if (ret < 0)
3025 goto out;
3026
3027
3028 old_min_uV = voltage->min_uV;
3029 old_max_uV = voltage->max_uV;
3030 voltage->min_uV = min_uV;
3031 voltage->max_uV = max_uV;
3032
3033 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
3034 if (ret < 0)
3035 goto out2;
3036
3037 if (rdev->supply &&
3038 regulator_ops_is_valid(rdev->supply->rdev,
3039 REGULATOR_CHANGE_VOLTAGE) &&
3040 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3041 rdev->desc->ops->get_voltage_sel))) {
3042 int current_supply_uV;
3043 int selector;
3044
3045 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3046 if (selector < 0) {
3047 ret = selector;
3048 goto out2;
3049 }
3050
3051 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3052 if (best_supply_uV < 0) {
3053 ret = best_supply_uV;
3054 goto out2;
3055 }
3056
3057 best_supply_uV += rdev->desc->min_dropout_uV;
3058
3059 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3060 if (current_supply_uV < 0) {
3061 ret = current_supply_uV;
3062 goto out2;
3063 }
3064
3065 supply_change_uV = best_supply_uV - current_supply_uV;
3066 }
3067
3068 if (supply_change_uV > 0) {
3069 ret = regulator_set_voltage_unlocked(rdev->supply,
3070 best_supply_uV, INT_MAX, state);
3071 if (ret) {
3072 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3073 ret);
3074 goto out2;
3075 }
3076 }
3077
3078 if (state == PM_SUSPEND_ON)
3079 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3080 else
3081 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3082 max_uV, state);
3083 if (ret < 0)
3084 goto out2;
3085
3086 if (supply_change_uV < 0) {
3087 ret = regulator_set_voltage_unlocked(rdev->supply,
3088 best_supply_uV, INT_MAX, state);
3089 if (ret)
3090 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3091 ret);
3092
3093 ret = 0;
3094 }
3095
3096out:
3097 return ret;
3098out2:
3099 voltage->min_uV = old_min_uV;
3100 voltage->max_uV = old_max_uV;
3101
3102 return ret;
3103}
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3124{
3125 int ret = 0;
3126
3127 regulator_lock_supply(regulator->rdev);
3128
3129 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3130 PM_SUSPEND_ON);
3131
3132 regulator_unlock_supply(regulator->rdev);
3133
3134 return ret;
3135}
3136EXPORT_SYMBOL_GPL(regulator_set_voltage);
3137
3138static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3139 suspend_state_t state, bool en)
3140{
3141 struct regulator_state *rstate;
3142
3143 rstate = regulator_get_suspend_state(rdev, state);
3144 if (rstate == NULL)
3145 return -EINVAL;
3146
3147 if (!rstate->changeable)
3148 return -EPERM;
3149
3150 rstate->enabled = en;
3151
3152 return 0;
3153}
3154
3155int regulator_suspend_enable(struct regulator_dev *rdev,
3156 suspend_state_t state)
3157{
3158 return regulator_suspend_toggle(rdev, state, true);
3159}
3160EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3161
3162int regulator_suspend_disable(struct regulator_dev *rdev,
3163 suspend_state_t state)
3164{
3165 struct regulator *regulator;
3166 struct regulator_voltage *voltage;
3167
3168
3169
3170
3171
3172 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3173 voltage = ®ulator->voltage[state];
3174 if (voltage->min_uV || voltage->max_uV)
3175 return 0;
3176 }
3177
3178 return regulator_suspend_toggle(rdev, state, false);
3179}
3180EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3181
3182static int _regulator_set_suspend_voltage(struct regulator *regulator,
3183 int min_uV, int max_uV,
3184 suspend_state_t state)
3185{
3186 struct regulator_dev *rdev = regulator->rdev;
3187 struct regulator_state *rstate;
3188
3189 rstate = regulator_get_suspend_state(rdev, state);
3190 if (rstate == NULL)
3191 return -EINVAL;
3192
3193 if (rstate->min_uV == rstate->max_uV) {
3194 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3195 return -EPERM;
3196 }
3197
3198 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3199}
3200
3201int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3202 int max_uV, suspend_state_t state)
3203{
3204 int ret = 0;
3205
3206
3207 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3208 return -EINVAL;
3209
3210 regulator_lock_supply(regulator->rdev);
3211
3212 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3213 max_uV, state);
3214
3215 regulator_unlock_supply(regulator->rdev);
3216
3217 return ret;
3218}
3219EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231int regulator_set_voltage_time(struct regulator *regulator,
3232 int old_uV, int new_uV)
3233{
3234 struct regulator_dev *rdev = regulator->rdev;
3235 const struct regulator_ops *ops = rdev->desc->ops;
3236 int old_sel = -1;
3237 int new_sel = -1;
3238 int voltage;
3239 int i;
3240
3241 if (ops->set_voltage_time)
3242 return ops->set_voltage_time(rdev, old_uV, new_uV);
3243 else if (!ops->set_voltage_time_sel)
3244 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3245
3246
3247 if (!ops->list_voltage || !rdev->desc->n_voltages)
3248 return -EINVAL;
3249
3250 for (i = 0; i < rdev->desc->n_voltages; i++) {
3251
3252 voltage = regulator_list_voltage(regulator, i);
3253 if (voltage < 0)
3254 return -EINVAL;
3255 if (voltage == 0)
3256 continue;
3257 if (voltage == old_uV)
3258 old_sel = i;
3259 if (voltage == new_uV)
3260 new_sel = i;
3261 }
3262
3263 if (old_sel < 0 || new_sel < 0)
3264 return -EINVAL;
3265
3266 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3267}
3268EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3283 unsigned int old_selector,
3284 unsigned int new_selector)
3285{
3286 int old_volt, new_volt;
3287
3288
3289 if (!rdev->desc->ops->list_voltage)
3290 return -EINVAL;
3291
3292 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3293 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3294
3295 if (rdev->desc->ops->set_voltage_time)
3296 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3297 new_volt);
3298 else
3299 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3300}
3301EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311int regulator_sync_voltage(struct regulator *regulator)
3312{
3313 struct regulator_dev *rdev = regulator->rdev;
3314 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
3315 int ret, min_uV, max_uV;
3316
3317 regulator_lock(rdev);
3318
3319 if (!rdev->desc->ops->set_voltage &&
3320 !rdev->desc->ops->set_voltage_sel) {
3321 ret = -EINVAL;
3322 goto out;
3323 }
3324
3325
3326 if (!voltage->min_uV && !voltage->max_uV) {
3327 ret = -EINVAL;
3328 goto out;
3329 }
3330
3331 min_uV = voltage->min_uV;
3332 max_uV = voltage->max_uV;
3333
3334
3335 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3336 if (ret < 0)
3337 goto out;
3338
3339 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3340 if (ret < 0)
3341 goto out;
3342
3343 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3344
3345out:
3346 regulator_unlock(rdev);
3347 return ret;
3348}
3349EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3350
3351static int _regulator_get_voltage(struct regulator_dev *rdev)
3352{
3353 int sel, ret;
3354 bool bypassed;
3355
3356 if (rdev->desc->ops->get_bypass) {
3357 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3358 if (ret < 0)
3359 return ret;
3360 if (bypassed) {
3361
3362 if (!rdev->supply) {
3363 rdev_err(rdev,
3364 "bypassed regulator has no supply!\n");
3365 return -EPROBE_DEFER;
3366 }
3367
3368 return _regulator_get_voltage(rdev->supply->rdev);
3369 }
3370 }
3371
3372 if (rdev->desc->ops->get_voltage_sel) {
3373 sel = rdev->desc->ops->get_voltage_sel(rdev);
3374 if (sel < 0)
3375 return sel;
3376 ret = rdev->desc->ops->list_voltage(rdev, sel);
3377 } else if (rdev->desc->ops->get_voltage) {
3378 ret = rdev->desc->ops->get_voltage(rdev);
3379 } else if (rdev->desc->ops->list_voltage) {
3380 ret = rdev->desc->ops->list_voltage(rdev, 0);
3381 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3382 ret = rdev->desc->fixed_uV;
3383 } else if (rdev->supply) {
3384 ret = _regulator_get_voltage(rdev->supply->rdev);
3385 } else {
3386 return -EINVAL;
3387 }
3388
3389 if (ret < 0)
3390 return ret;
3391 return ret - rdev->constraints->uV_offset;
3392}
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403int regulator_get_voltage(struct regulator *regulator)
3404{
3405 int ret;
3406
3407 regulator_lock_supply(regulator->rdev);
3408
3409 ret = _regulator_get_voltage(regulator->rdev);
3410
3411 regulator_unlock_supply(regulator->rdev);
3412
3413 return ret;
3414}
3415EXPORT_SYMBOL_GPL(regulator_get_voltage);
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433int regulator_set_current_limit(struct regulator *regulator,
3434 int min_uA, int max_uA)
3435{
3436 struct regulator_dev *rdev = regulator->rdev;
3437 int ret;
3438
3439 regulator_lock(rdev);
3440
3441
3442 if (!rdev->desc->ops->set_current_limit) {
3443 ret = -EINVAL;
3444 goto out;
3445 }
3446
3447
3448 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3449 if (ret < 0)
3450 goto out;
3451
3452 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3453out:
3454 regulator_unlock(rdev);
3455 return ret;
3456}
3457EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3458
3459static int _regulator_get_current_limit(struct regulator_dev *rdev)
3460{
3461 int ret;
3462
3463 regulator_lock(rdev);
3464
3465
3466 if (!rdev->desc->ops->get_current_limit) {
3467 ret = -EINVAL;
3468 goto out;
3469 }
3470
3471 ret = rdev->desc->ops->get_current_limit(rdev);
3472out:
3473 regulator_unlock(rdev);
3474 return ret;
3475}
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486int regulator_get_current_limit(struct regulator *regulator)
3487{
3488 return _regulator_get_current_limit(regulator->rdev);
3489}
3490EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3504{
3505 struct regulator_dev *rdev = regulator->rdev;
3506 int ret;
3507 int regulator_curr_mode;
3508
3509 regulator_lock(rdev);
3510
3511
3512 if (!rdev->desc->ops->set_mode) {
3513 ret = -EINVAL;
3514 goto out;
3515 }
3516
3517
3518 if (rdev->desc->ops->get_mode) {
3519 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3520 if (regulator_curr_mode == mode) {
3521 ret = 0;
3522 goto out;
3523 }
3524 }
3525
3526
3527 ret = regulator_mode_constrain(rdev, &mode);
3528 if (ret < 0)
3529 goto out;
3530
3531 ret = rdev->desc->ops->set_mode(rdev, mode);
3532out:
3533 regulator_unlock(rdev);
3534 return ret;
3535}
3536EXPORT_SYMBOL_GPL(regulator_set_mode);
3537
3538static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3539{
3540 int ret;
3541
3542 regulator_lock(rdev);
3543
3544
3545 if (!rdev->desc->ops->get_mode) {
3546 ret = -EINVAL;
3547 goto out;
3548 }
3549
3550 ret = rdev->desc->ops->get_mode(rdev);
3551out:
3552 regulator_unlock(rdev);
3553 return ret;
3554}
3555
3556
3557
3558
3559
3560
3561
3562unsigned int regulator_get_mode(struct regulator *regulator)
3563{
3564 return _regulator_get_mode(regulator->rdev);
3565}
3566EXPORT_SYMBOL_GPL(regulator_get_mode);
3567
3568static int _regulator_get_error_flags(struct regulator_dev *rdev,
3569 unsigned int *flags)
3570{
3571 int ret;
3572
3573 regulator_lock(rdev);
3574
3575
3576 if (!rdev->desc->ops->get_error_flags) {
3577 ret = -EINVAL;
3578 goto out;
3579 }
3580
3581 ret = rdev->desc->ops->get_error_flags(rdev, flags);
3582out:
3583 regulator_unlock(rdev);
3584 return ret;
3585}
3586
3587
3588
3589
3590
3591
3592
3593
3594int regulator_get_error_flags(struct regulator *regulator,
3595 unsigned int *flags)
3596{
3597 return _regulator_get_error_flags(regulator->rdev, flags);
3598}
3599EXPORT_SYMBOL_GPL(regulator_get_error_flags);
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627int regulator_set_load(struct regulator *regulator, int uA_load)
3628{
3629 struct regulator_dev *rdev = regulator->rdev;
3630 int ret;
3631
3632 regulator_lock(rdev);
3633 regulator->uA_load = uA_load;
3634 ret = drms_uA_update(rdev);
3635 regulator_unlock(rdev);
3636
3637 return ret;
3638}
3639EXPORT_SYMBOL_GPL(regulator_set_load);
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652int regulator_allow_bypass(struct regulator *regulator, bool enable)
3653{
3654 struct regulator_dev *rdev = regulator->rdev;
3655 int ret = 0;
3656
3657 if (!rdev->desc->ops->set_bypass)
3658 return 0;
3659
3660 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
3661 return 0;
3662
3663 regulator_lock(rdev);
3664
3665 if (enable && !regulator->bypass) {
3666 rdev->bypass_count++;
3667
3668 if (rdev->bypass_count == rdev->open_count) {
3669 ret = rdev->desc->ops->set_bypass(rdev, enable);
3670 if (ret != 0)
3671 rdev->bypass_count--;
3672 }
3673
3674 } else if (!enable && regulator->bypass) {
3675 rdev->bypass_count--;
3676
3677 if (rdev->bypass_count != rdev->open_count) {
3678 ret = rdev->desc->ops->set_bypass(rdev, enable);
3679 if (ret != 0)
3680 rdev->bypass_count++;
3681 }
3682 }
3683
3684 if (ret == 0)
3685 regulator->bypass = enable;
3686
3687 regulator_unlock(rdev);
3688
3689 return ret;
3690}
3691EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3692
3693
3694
3695
3696
3697
3698
3699
3700int regulator_register_notifier(struct regulator *regulator,
3701 struct notifier_block *nb)
3702{
3703 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3704 nb);
3705}
3706EXPORT_SYMBOL_GPL(regulator_register_notifier);
3707
3708
3709
3710
3711
3712
3713
3714
3715int regulator_unregister_notifier(struct regulator *regulator,
3716 struct notifier_block *nb)
3717{
3718 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3719 nb);
3720}
3721EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3722
3723
3724
3725
3726static int _notifier_call_chain(struct regulator_dev *rdev,
3727 unsigned long event, void *data)
3728{
3729
3730 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3731}
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747int regulator_bulk_get(struct device *dev, int num_consumers,
3748 struct regulator_bulk_data *consumers)
3749{
3750 int i;
3751 int ret;
3752
3753 for (i = 0; i < num_consumers; i++)
3754 consumers[i].consumer = NULL;
3755
3756 for (i = 0; i < num_consumers; i++) {
3757 consumers[i].consumer = regulator_get(dev,
3758 consumers[i].supply);
3759 if (IS_ERR(consumers[i].consumer)) {
3760 ret = PTR_ERR(consumers[i].consumer);
3761 dev_err(dev, "Failed to get supply '%s': %d\n",
3762 consumers[i].supply, ret);
3763 consumers[i].consumer = NULL;
3764 goto err;
3765 }
3766 }
3767
3768 return 0;
3769
3770err:
3771 while (--i >= 0)
3772 regulator_put(consumers[i].consumer);
3773
3774 return ret;
3775}
3776EXPORT_SYMBOL_GPL(regulator_bulk_get);
3777
3778static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3779{
3780 struct regulator_bulk_data *bulk = data;
3781
3782 bulk->ret = regulator_enable(bulk->consumer);
3783}
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797int regulator_bulk_enable(int num_consumers,
3798 struct regulator_bulk_data *consumers)
3799{
3800 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3801 int i;
3802 int ret = 0;
3803
3804 for (i = 0; i < num_consumers; i++) {
3805 if (consumers[i].consumer->always_on)
3806 consumers[i].ret = 0;
3807 else
3808 async_schedule_domain(regulator_bulk_enable_async,
3809 &consumers[i], &async_domain);
3810 }
3811
3812 async_synchronize_full_domain(&async_domain);
3813
3814
3815 for (i = 0; i < num_consumers; i++) {
3816 if (consumers[i].ret != 0) {
3817 ret = consumers[i].ret;
3818 goto err;
3819 }
3820 }
3821
3822 return 0;
3823
3824err:
3825 for (i = 0; i < num_consumers; i++) {
3826 if (consumers[i].ret < 0)
3827 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3828 consumers[i].ret);
3829 else
3830 regulator_disable(consumers[i].consumer);
3831 }
3832
3833 return ret;
3834}
3835EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849int regulator_bulk_disable(int num_consumers,
3850 struct regulator_bulk_data *consumers)
3851{
3852 int i;
3853 int ret, r;
3854
3855 for (i = num_consumers - 1; i >= 0; --i) {
3856 ret = regulator_disable(consumers[i].consumer);
3857 if (ret != 0)
3858 goto err;
3859 }
3860
3861 return 0;
3862
3863err:
3864 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3865 for (++i; i < num_consumers; ++i) {
3866 r = regulator_enable(consumers[i].consumer);
3867 if (r != 0)
3868 pr_err("Failed to re-enable %s: %d\n",
3869 consumers[i].supply, r);
3870 }
3871
3872 return ret;
3873}
3874EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890int regulator_bulk_force_disable(int num_consumers,
3891 struct regulator_bulk_data *consumers)
3892{
3893 int i;
3894 int ret = 0;
3895
3896 for (i = 0; i < num_consumers; i++) {
3897 consumers[i].ret =
3898 regulator_force_disable(consumers[i].consumer);
3899
3900
3901 if (consumers[i].ret && !ret)
3902 ret = consumers[i].ret;
3903 }
3904
3905 return ret;
3906}
3907EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918void regulator_bulk_free(int num_consumers,
3919 struct regulator_bulk_data *consumers)
3920{
3921 int i;
3922
3923 for (i = 0; i < num_consumers; i++) {
3924 regulator_put(consumers[i].consumer);
3925 consumers[i].consumer = NULL;
3926 }
3927}
3928EXPORT_SYMBOL_GPL(regulator_bulk_free);
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940int regulator_notifier_call_chain(struct regulator_dev *rdev,
3941 unsigned long event, void *data)
3942{
3943 lockdep_assert_held_once(&rdev->mutex);
3944
3945 _notifier_call_chain(rdev, event, data);
3946 return NOTIFY_DONE;
3947
3948}
3949EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3950
3951
3952
3953
3954
3955
3956
3957
3958int regulator_mode_to_status(unsigned int mode)
3959{
3960 switch (mode) {
3961 case REGULATOR_MODE_FAST:
3962 return REGULATOR_STATUS_FAST;
3963 case REGULATOR_MODE_NORMAL:
3964 return REGULATOR_STATUS_NORMAL;
3965 case REGULATOR_MODE_IDLE:
3966 return REGULATOR_STATUS_IDLE;
3967 case REGULATOR_MODE_STANDBY:
3968 return REGULATOR_STATUS_STANDBY;
3969 default:
3970 return REGULATOR_STATUS_UNDEFINED;
3971 }
3972}
3973EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3974
3975static struct attribute *regulator_dev_attrs[] = {
3976 &dev_attr_name.attr,
3977 &dev_attr_num_users.attr,
3978 &dev_attr_type.attr,
3979 &dev_attr_microvolts.attr,
3980 &dev_attr_microamps.attr,
3981 &dev_attr_opmode.attr,
3982 &dev_attr_state.attr,
3983 &dev_attr_status.attr,
3984 &dev_attr_bypass.attr,
3985 &dev_attr_requested_microamps.attr,
3986 &dev_attr_min_microvolts.attr,
3987 &dev_attr_max_microvolts.attr,
3988 &dev_attr_min_microamps.attr,
3989 &dev_attr_max_microamps.attr,
3990 &dev_attr_suspend_standby_state.attr,
3991 &dev_attr_suspend_mem_state.attr,
3992 &dev_attr_suspend_disk_state.attr,
3993 &dev_attr_suspend_standby_microvolts.attr,
3994 &dev_attr_suspend_mem_microvolts.attr,
3995 &dev_attr_suspend_disk_microvolts.attr,
3996 &dev_attr_suspend_standby_mode.attr,
3997 &dev_attr_suspend_mem_mode.attr,
3998 &dev_attr_suspend_disk_mode.attr,
3999 NULL
4000};
4001
4002
4003
4004
4005
4006static umode_t regulator_attr_is_visible(struct kobject *kobj,
4007 struct attribute *attr, int idx)
4008{
4009 struct device *dev = kobj_to_dev(kobj);
4010 struct regulator_dev *rdev = dev_to_rdev(dev);
4011 const struct regulator_ops *ops = rdev->desc->ops;
4012 umode_t mode = attr->mode;
4013
4014
4015 if (attr == &dev_attr_name.attr ||
4016 attr == &dev_attr_num_users.attr ||
4017 attr == &dev_attr_type.attr)
4018 return mode;
4019
4020
4021 if (attr == &dev_attr_microvolts.attr) {
4022 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4023 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4024 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4025 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4026 return mode;
4027 return 0;
4028 }
4029
4030 if (attr == &dev_attr_microamps.attr)
4031 return ops->get_current_limit ? mode : 0;
4032
4033 if (attr == &dev_attr_opmode.attr)
4034 return ops->get_mode ? mode : 0;
4035
4036 if (attr == &dev_attr_state.attr)
4037 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4038
4039 if (attr == &dev_attr_status.attr)
4040 return ops->get_status ? mode : 0;
4041
4042 if (attr == &dev_attr_bypass.attr)
4043 return ops->get_bypass ? mode : 0;
4044
4045
4046 if (attr == &dev_attr_requested_microamps.attr)
4047 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
4048
4049
4050 if (attr == &dev_attr_min_microvolts.attr ||
4051 attr == &dev_attr_max_microvolts.attr)
4052 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4053
4054 if (attr == &dev_attr_min_microamps.attr ||
4055 attr == &dev_attr_max_microamps.attr)
4056 return ops->set_current_limit ? mode : 0;
4057
4058 if (attr == &dev_attr_suspend_standby_state.attr ||
4059 attr == &dev_attr_suspend_mem_state.attr ||
4060 attr == &dev_attr_suspend_disk_state.attr)
4061 return mode;
4062
4063 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4064 attr == &dev_attr_suspend_mem_microvolts.attr ||
4065 attr == &dev_attr_suspend_disk_microvolts.attr)
4066 return ops->set_suspend_voltage ? mode : 0;
4067
4068 if (attr == &dev_attr_suspend_standby_mode.attr ||
4069 attr == &dev_attr_suspend_mem_mode.attr ||
4070 attr == &dev_attr_suspend_disk_mode.attr)
4071 return ops->set_suspend_mode ? mode : 0;
4072
4073 return mode;
4074}
4075
4076static const struct attribute_group regulator_dev_group = {
4077 .attrs = regulator_dev_attrs,
4078 .is_visible = regulator_attr_is_visible,
4079};
4080
4081static const struct attribute_group *regulator_dev_groups[] = {
4082 ®ulator_dev_group,
4083 NULL
4084};
4085
4086static void regulator_dev_release(struct device *dev)
4087{
4088 struct regulator_dev *rdev = dev_get_drvdata(dev);
4089
4090 kfree(rdev->constraints);
4091 of_node_put(rdev->dev.of_node);
4092 kfree(rdev);
4093}
4094
4095static void rdev_init_debugfs(struct regulator_dev *rdev)
4096{
4097 struct device *parent = rdev->dev.parent;
4098 const char *rname = rdev_get_name(rdev);
4099 char name[NAME_MAX];
4100
4101
4102 if (parent && rname == rdev->desc->name) {
4103 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4104 rname);
4105 rname = name;
4106 }
4107
4108 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4109 if (!rdev->debugfs) {
4110 rdev_warn(rdev, "Failed to create debugfs directory\n");
4111 return;
4112 }
4113
4114 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4115 &rdev->use_count);
4116 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4117 &rdev->open_count);
4118 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4119 &rdev->bypass_count);
4120}
4121
4122static int regulator_register_resolve_supply(struct device *dev, void *data)
4123{
4124 struct regulator_dev *rdev = dev_to_rdev(dev);
4125
4126 if (regulator_resolve_supply(rdev))
4127 rdev_dbg(rdev, "unable to resolve supply\n");
4128
4129 return 0;
4130}
4131
4132static int regulator_fill_coupling_array(struct regulator_dev *rdev)
4133{
4134 struct coupling_desc *c_desc = &rdev->coupling_desc;
4135 int n_coupled = c_desc->n_coupled;
4136 struct regulator_dev *c_rdev;
4137 int i;
4138
4139 for (i = 1; i < n_coupled; i++) {
4140
4141 if (c_desc->coupled_rdevs[i])
4142 continue;
4143
4144 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4145
4146 if (c_rdev) {
4147 c_desc->coupled_rdevs[i] = c_rdev;
4148 c_desc->n_resolved++;
4149 }
4150 }
4151
4152 if (rdev->coupling_desc.n_resolved < n_coupled)
4153 return -1;
4154 else
4155 return 0;
4156}
4157
4158static int regulator_register_fill_coupling_array(struct device *dev,
4159 void *data)
4160{
4161 struct regulator_dev *rdev = dev_to_rdev(dev);
4162
4163 if (!IS_ENABLED(CONFIG_OF))
4164 return 0;
4165
4166 if (regulator_fill_coupling_array(rdev))
4167 rdev_dbg(rdev, "unable to resolve coupling\n");
4168
4169 return 0;
4170}
4171
4172static int regulator_resolve_coupling(struct regulator_dev *rdev)
4173{
4174 int n_phandles;
4175
4176 if (!IS_ENABLED(CONFIG_OF))
4177 n_phandles = 0;
4178 else
4179 n_phandles = of_get_n_coupled(rdev);
4180
4181 if (n_phandles + 1 > MAX_COUPLED) {
4182 rdev_err(rdev, "too many regulators coupled\n");
4183 return -EPERM;
4184 }
4185
4186
4187
4188
4189
4190 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4191 rdev->coupling_desc.n_coupled = n_phandles + 1;
4192 rdev->coupling_desc.n_resolved++;
4193
4194
4195 if (n_phandles == 0)
4196 return 0;
4197
4198
4199 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
4200 rdev_err(rdev, "voltage operation not allowed\n");
4201 return -EPERM;
4202 }
4203
4204 if (rdev->constraints->max_spread <= 0) {
4205 rdev_err(rdev, "wrong max_spread value\n");
4206 return -EPERM;
4207 }
4208
4209 if (!of_check_coupling_data(rdev))
4210 return -EPERM;
4211
4212
4213
4214
4215
4216
4217 regulator_fill_coupling_array(rdev);
4218
4219 return 0;
4220}
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231struct regulator_dev *
4232regulator_register(const struct regulator_desc *regulator_desc,
4233 const struct regulator_config *cfg)
4234{
4235 const struct regulation_constraints *constraints = NULL;
4236 const struct regulator_init_data *init_data;
4237 struct regulator_config *config = NULL;
4238 static atomic_t regulator_no = ATOMIC_INIT(-1);
4239 struct regulator_dev *rdev;
4240 struct device *dev;
4241 int ret, i;
4242
4243 if (regulator_desc == NULL || cfg == NULL)
4244 return ERR_PTR(-EINVAL);
4245
4246 dev = cfg->dev;
4247 WARN_ON(!dev);
4248
4249 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4250 return ERR_PTR(-EINVAL);
4251
4252 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4253 regulator_desc->type != REGULATOR_CURRENT)
4254 return ERR_PTR(-EINVAL);
4255
4256
4257 WARN_ON(regulator_desc->ops->get_voltage &&
4258 regulator_desc->ops->get_voltage_sel);
4259 WARN_ON(regulator_desc->ops->set_voltage &&
4260 regulator_desc->ops->set_voltage_sel);
4261
4262
4263 if (regulator_desc->ops->get_voltage_sel &&
4264 !regulator_desc->ops->list_voltage) {
4265 return ERR_PTR(-EINVAL);
4266 }
4267 if (regulator_desc->ops->set_voltage_sel &&
4268 !regulator_desc->ops->list_voltage) {
4269 return ERR_PTR(-EINVAL);
4270 }
4271
4272 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4273 if (rdev == NULL)
4274 return ERR_PTR(-ENOMEM);
4275
4276
4277
4278
4279
4280 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4281 if (config == NULL) {
4282 kfree(rdev);
4283 return ERR_PTR(-ENOMEM);
4284 }
4285
4286 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4287 &rdev->dev.of_node);
4288 if (!init_data) {
4289 init_data = config->init_data;
4290 rdev->dev.of_node = of_node_get(config->of_node);
4291 }
4292
4293 mutex_init(&rdev->mutex);
4294 rdev->reg_data = config->driver_data;
4295 rdev->owner = regulator_desc->owner;
4296 rdev->desc = regulator_desc;
4297 if (config->regmap)
4298 rdev->regmap = config->regmap;
4299 else if (dev_get_regmap(dev, NULL))
4300 rdev->regmap = dev_get_regmap(dev, NULL);
4301 else if (dev->parent)
4302 rdev->regmap = dev_get_regmap(dev->parent, NULL);
4303 INIT_LIST_HEAD(&rdev->consumer_list);
4304 INIT_LIST_HEAD(&rdev->list);
4305 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4306 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4307
4308
4309 if (init_data && init_data->regulator_init) {
4310 ret = init_data->regulator_init(rdev->reg_data);
4311 if (ret < 0)
4312 goto clean;
4313 }
4314
4315 if (config->ena_gpiod ||
4316 ((config->ena_gpio || config->ena_gpio_initialized) &&
4317 gpio_is_valid(config->ena_gpio))) {
4318 mutex_lock(®ulator_list_mutex);
4319 ret = regulator_ena_gpio_request(rdev, config);
4320 mutex_unlock(®ulator_list_mutex);
4321 if (ret != 0) {
4322 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4323 config->ena_gpio, ret);
4324 goto clean;
4325 }
4326 }
4327
4328
4329 rdev->dev.class = ®ulator_class;
4330 rdev->dev.parent = dev;
4331 dev_set_name(&rdev->dev, "regulator.%lu",
4332 (unsigned long) atomic_inc_return(®ulator_no));
4333
4334
4335 if (init_data)
4336 constraints = &init_data->constraints;
4337
4338 if (init_data && init_data->supply_regulator)
4339 rdev->supply_name = init_data->supply_regulator;
4340 else if (regulator_desc->supply_name)
4341 rdev->supply_name = regulator_desc->supply_name;
4342
4343
4344
4345
4346
4347
4348 if (regulator_resolve_supply(rdev))
4349 rdev_dbg(rdev, "unable to resolve supply\n");
4350
4351 ret = set_machine_constraints(rdev, constraints);
4352 if (ret < 0)
4353 goto wash;
4354
4355 mutex_lock(®ulator_list_mutex);
4356 ret = regulator_resolve_coupling(rdev);
4357 mutex_unlock(®ulator_list_mutex);
4358
4359 if (ret != 0)
4360 goto wash;
4361
4362
4363 if (init_data) {
4364 mutex_lock(®ulator_list_mutex);
4365 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4366 ret = set_consumer_device_supply(rdev,
4367 init_data->consumer_supplies[i].dev_name,
4368 init_data->consumer_supplies[i].supply);
4369 if (ret < 0) {
4370 mutex_unlock(®ulator_list_mutex);
4371 dev_err(dev, "Failed to set supply %s\n",
4372 init_data->consumer_supplies[i].supply);
4373 goto unset_supplies;
4374 }
4375 }
4376 mutex_unlock(®ulator_list_mutex);
4377 }
4378
4379 if (!rdev->desc->ops->get_voltage &&
4380 !rdev->desc->ops->list_voltage &&
4381 !rdev->desc->fixed_uV)
4382 rdev->is_switch = true;
4383
4384 ret = device_register(&rdev->dev);
4385 if (ret != 0) {
4386 put_device(&rdev->dev);
4387 goto unset_supplies;
4388 }
4389
4390 dev_set_drvdata(&rdev->dev, rdev);
4391 rdev_init_debugfs(rdev);
4392
4393
4394 class_for_each_device(®ulator_class, NULL, NULL,
4395 regulator_register_resolve_supply);
4396 kfree(config);
4397 return rdev;
4398
4399unset_supplies:
4400 mutex_lock(®ulator_list_mutex);
4401 unset_regulator_supplies(rdev);
4402 mutex_unlock(®ulator_list_mutex);
4403wash:
4404 kfree(rdev->constraints);
4405 mutex_lock(®ulator_list_mutex);
4406 regulator_ena_gpio_free(rdev);
4407 mutex_unlock(®ulator_list_mutex);
4408clean:
4409 kfree(rdev);
4410 kfree(config);
4411 return ERR_PTR(ret);
4412}
4413EXPORT_SYMBOL_GPL(regulator_register);
4414
4415
4416
4417
4418
4419
4420
4421void regulator_unregister(struct regulator_dev *rdev)
4422{
4423 if (rdev == NULL)
4424 return;
4425
4426 if (rdev->supply) {
4427 while (rdev->use_count--)
4428 regulator_disable(rdev->supply);
4429 regulator_put(rdev->supply);
4430 }
4431 mutex_lock(®ulator_list_mutex);
4432 debugfs_remove_recursive(rdev->debugfs);
4433 flush_work(&rdev->disable_work.work);
4434 WARN_ON(rdev->open_count);
4435 unset_regulator_supplies(rdev);
4436 list_del(&rdev->list);
4437 regulator_ena_gpio_free(rdev);
4438 mutex_unlock(®ulator_list_mutex);
4439 device_unregister(&rdev->dev);
4440}
4441EXPORT_SYMBOL_GPL(regulator_unregister);
4442
4443#ifdef CONFIG_SUSPEND
4444static int _regulator_suspend_late(struct device *dev, void *data)
4445{
4446 struct regulator_dev *rdev = dev_to_rdev(dev);
4447 suspend_state_t *state = data;
4448 int ret;
4449
4450 regulator_lock(rdev);
4451 ret = suspend_set_state(rdev, *state);
4452 regulator_unlock(rdev);
4453
4454 return ret;
4455}
4456
4457
4458
4459
4460
4461
4462
4463static int regulator_suspend_late(struct device *dev)
4464{
4465 suspend_state_t state = pm_suspend_target_state;
4466
4467 return class_for_each_device(®ulator_class, NULL, &state,
4468 _regulator_suspend_late);
4469}
4470
4471static int _regulator_resume_early(struct device *dev, void *data)
4472{
4473 int ret = 0;
4474 struct regulator_dev *rdev = dev_to_rdev(dev);
4475 suspend_state_t *state = data;
4476 struct regulator_state *rstate;
4477
4478 rstate = regulator_get_suspend_state(rdev, *state);
4479 if (rstate == NULL)
4480 return 0;
4481
4482 regulator_lock(rdev);
4483
4484 if (rdev->desc->ops->resume_early &&
4485 (rstate->enabled == ENABLE_IN_SUSPEND ||
4486 rstate->enabled == DISABLE_IN_SUSPEND))
4487 ret = rdev->desc->ops->resume_early(rdev);
4488
4489 regulator_unlock(rdev);
4490
4491 return ret;
4492}
4493
4494static int regulator_resume_early(struct device *dev)
4495{
4496 suspend_state_t state = pm_suspend_target_state;
4497
4498 return class_for_each_device(®ulator_class, NULL, &state,
4499 _regulator_resume_early);
4500}
4501
4502#else
4503
4504#define regulator_suspend_late NULL
4505#define regulator_resume_early NULL
4506
4507#endif
4508
4509#ifdef CONFIG_PM
4510static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
4511 .suspend_late = regulator_suspend_late,
4512 .resume_early = regulator_resume_early,
4513};
4514#endif
4515
4516struct class regulator_class = {
4517 .name = "regulator",
4518 .dev_release = regulator_dev_release,
4519 .dev_groups = regulator_dev_groups,
4520#ifdef CONFIG_PM
4521 .pm = ®ulator_pm_ops,
4522#endif
4523};
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535void regulator_has_full_constraints(void)
4536{
4537 has_full_constraints = 1;
4538}
4539EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4540
4541
4542
4543
4544
4545
4546
4547
4548void *rdev_get_drvdata(struct regulator_dev *rdev)
4549{
4550 return rdev->reg_data;
4551}
4552EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4553
4554
4555
4556
4557
4558
4559
4560
4561void *regulator_get_drvdata(struct regulator *regulator)
4562{
4563 return regulator->rdev->reg_data;
4564}
4565EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4566
4567
4568
4569
4570
4571
4572void regulator_set_drvdata(struct regulator *regulator, void *data)
4573{
4574 regulator->rdev->reg_data = data;
4575}
4576EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4577
4578
4579
4580
4581
4582int rdev_get_id(struct regulator_dev *rdev)
4583{
4584 return rdev->desc->id;
4585}
4586EXPORT_SYMBOL_GPL(rdev_get_id);
4587
4588struct device *rdev_get_dev(struct regulator_dev *rdev)
4589{
4590 return &rdev->dev;
4591}
4592EXPORT_SYMBOL_GPL(rdev_get_dev);
4593
4594void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4595{
4596 return reg_init_data->driver_data;
4597}
4598EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4599
4600#ifdef CONFIG_DEBUG_FS
4601static int supply_map_show(struct seq_file *sf, void *data)
4602{
4603 struct regulator_map *map;
4604
4605 list_for_each_entry(map, ®ulator_map_list, list) {
4606 seq_printf(sf, "%s -> %s.%s\n",
4607 rdev_get_name(map->regulator), map->dev_name,
4608 map->supply);
4609 }
4610
4611 return 0;
4612}
4613
4614static int supply_map_open(struct inode *inode, struct file *file)
4615{
4616 return single_open(file, supply_map_show, inode->i_private);
4617}
4618#endif
4619
4620static const struct file_operations supply_map_fops = {
4621#ifdef CONFIG_DEBUG_FS
4622 .open = supply_map_open,
4623 .read = seq_read,
4624 .llseek = seq_lseek,
4625 .release = single_release,
4626#endif
4627};
4628
4629#ifdef CONFIG_DEBUG_FS
4630struct summary_data {
4631 struct seq_file *s;
4632 struct regulator_dev *parent;
4633 int level;
4634};
4635
4636static void regulator_summary_show_subtree(struct seq_file *s,
4637 struct regulator_dev *rdev,
4638 int level);
4639
4640static int regulator_summary_show_children(struct device *dev, void *data)
4641{
4642 struct regulator_dev *rdev = dev_to_rdev(dev);
4643 struct summary_data *summary_data = data;
4644
4645 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4646 regulator_summary_show_subtree(summary_data->s, rdev,
4647 summary_data->level + 1);
4648
4649 return 0;
4650}
4651
4652static void regulator_summary_show_subtree(struct seq_file *s,
4653 struct regulator_dev *rdev,
4654 int level)
4655{
4656 struct regulation_constraints *c;
4657 struct regulator *consumer;
4658 struct summary_data summary_data;
4659
4660 if (!rdev)
4661 return;
4662
4663 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4664 level * 3 + 1, "",
4665 30 - level * 3, rdev_get_name(rdev),
4666 rdev->use_count, rdev->open_count, rdev->bypass_count);
4667
4668 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4669 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4670
4671 c = rdev->constraints;
4672 if (c) {
4673 switch (rdev->desc->type) {
4674 case REGULATOR_VOLTAGE:
4675 seq_printf(s, "%5dmV %5dmV ",
4676 c->min_uV / 1000, c->max_uV / 1000);
4677 break;
4678 case REGULATOR_CURRENT:
4679 seq_printf(s, "%5dmA %5dmA ",
4680 c->min_uA / 1000, c->max_uA / 1000);
4681 break;
4682 }
4683 }
4684
4685 seq_puts(s, "\n");
4686
4687 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4688 if (consumer->dev && consumer->dev->class == ®ulator_class)
4689 continue;
4690
4691 seq_printf(s, "%*s%-*s ",
4692 (level + 1) * 3 + 1, "",
4693 30 - (level + 1) * 3,
4694 consumer->dev ? dev_name(consumer->dev) : "deviceless");
4695
4696 switch (rdev->desc->type) {
4697 case REGULATOR_VOLTAGE:
4698 seq_printf(s, "%37dmV %5dmV",
4699 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
4700 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
4701 break;
4702 case REGULATOR_CURRENT:
4703 break;
4704 }
4705
4706 seq_puts(s, "\n");
4707 }
4708
4709 summary_data.s = s;
4710 summary_data.level = level;
4711 summary_data.parent = rdev;
4712
4713 class_for_each_device(®ulator_class, NULL, &summary_data,
4714 regulator_summary_show_children);
4715}
4716
4717static int regulator_summary_show_roots(struct device *dev, void *data)
4718{
4719 struct regulator_dev *rdev = dev_to_rdev(dev);
4720 struct seq_file *s = data;
4721
4722 if (!rdev->supply)
4723 regulator_summary_show_subtree(s, rdev, 0);
4724
4725 return 0;
4726}
4727
4728static int regulator_summary_show(struct seq_file *s, void *data)
4729{
4730 seq_puts(s, " regulator use open bypass voltage current min max\n");
4731 seq_puts(s, "-------------------------------------------------------------------------------\n");
4732
4733 class_for_each_device(®ulator_class, NULL, s,
4734 regulator_summary_show_roots);
4735
4736 return 0;
4737}
4738
4739static int regulator_summary_open(struct inode *inode, struct file *file)
4740{
4741 return single_open(file, regulator_summary_show, inode->i_private);
4742}
4743#endif
4744
4745static const struct file_operations regulator_summary_fops = {
4746#ifdef CONFIG_DEBUG_FS
4747 .open = regulator_summary_open,
4748 .read = seq_read,
4749 .llseek = seq_lseek,
4750 .release = single_release,
4751#endif
4752};
4753
4754static int __init regulator_init(void)
4755{
4756 int ret;
4757
4758 ret = class_register(®ulator_class);
4759
4760 debugfs_root = debugfs_create_dir("regulator", NULL);
4761 if (!debugfs_root)
4762 pr_warn("regulator: Failed to create debugfs directory\n");
4763
4764 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4765 &supply_map_fops);
4766
4767 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4768 NULL, ®ulator_summary_fops);
4769
4770 regulator_dummy_init();
4771
4772 return ret;
4773}
4774
4775
4776core_initcall(regulator_init);
4777
4778static int __init regulator_late_cleanup(struct device *dev, void *data)
4779{
4780 struct regulator_dev *rdev = dev_to_rdev(dev);
4781 const struct regulator_ops *ops = rdev->desc->ops;
4782 struct regulation_constraints *c = rdev->constraints;
4783 int enabled, ret;
4784
4785 if (c && c->always_on)
4786 return 0;
4787
4788 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
4789 return 0;
4790
4791 regulator_lock(rdev);
4792
4793 if (rdev->use_count)
4794 goto unlock;
4795
4796
4797 if (ops->is_enabled)
4798 enabled = ops->is_enabled(rdev);
4799 else
4800 enabled = 1;
4801
4802 if (!enabled)
4803 goto unlock;
4804
4805 if (have_full_constraints()) {
4806
4807
4808 rdev_info(rdev, "disabling\n");
4809 ret = _regulator_do_disable(rdev);
4810 if (ret != 0)
4811 rdev_err(rdev, "couldn't disable: %d\n", ret);
4812 } else {
4813
4814
4815
4816
4817
4818 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4819 }
4820
4821unlock:
4822 regulator_unlock(rdev);
4823
4824 return 0;
4825}
4826
4827static int __init regulator_init_complete(void)
4828{
4829
4830
4831
4832
4833
4834
4835 if (of_have_populated_dt())
4836 has_full_constraints = true;
4837
4838
4839
4840
4841
4842
4843
4844
4845 class_for_each_device(®ulator_class, NULL, NULL,
4846 regulator_register_resolve_supply);
4847
4848
4849
4850
4851
4852
4853 class_for_each_device(®ulator_class, NULL, NULL,
4854 regulator_late_cleanup);
4855
4856 class_for_each_device(®ulator_class, NULL, NULL,
4857 regulator_register_fill_coupling_array);
4858
4859 return 0;
4860}
4861late_initcall_sync(regulator_init_complete);
4862