Merge remote-tracking branch 'regulator/topic/list' into regulator-next
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / core.c
1 /*
2  * core.c  --  Voltage/Current Regulator framework.
3  *
4  * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5  * Copyright 2008 SlimLogic Ltd.
6  *
7  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
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
53 static DEFINE_MUTEX(regulator_list_mutex);
54 static LIST_HEAD(regulator_map_list);
55 static LIST_HEAD(regulator_ena_gpio_list);
56 static LIST_HEAD(regulator_supply_alias_list);
57 static bool has_full_constraints;
58
59 static struct dentry *debugfs_root;
60
61 static struct class regulator_class;
62
63 /*
64  * struct regulator_map
65  *
66  * Used to provide symbolic supply names to devices.
67  */
68 struct regulator_map {
69         struct list_head list;
70         const char *dev_name;   /* The dev_name() for the consumer */
71         const char *supply;
72         struct regulator_dev *regulator;
73 };
74
75 /*
76  * struct regulator_enable_gpio
77  *
78  * Management for shared enable GPIO pin
79  */
80 struct regulator_enable_gpio {
81         struct list_head list;
82         struct gpio_desc *gpiod;
83         u32 enable_count;       /* a number of enabled shared GPIO */
84         u32 request_count;      /* a number of requested shared GPIO */
85         unsigned int ena_gpio_invert:1;
86 };
87
88 /*
89  * struct regulator_supply_alias
90  *
91  * Used to map lookups for a supply onto an alternative device.
92  */
93 struct regulator_supply_alias {
94         struct list_head list;
95         struct device *src_dev;
96         const char *src_supply;
97         struct device *alias_dev;
98         const char *alias_supply;
99 };
100
101 static int _regulator_is_enabled(struct regulator_dev *rdev);
102 static int _regulator_disable(struct regulator_dev *rdev);
103 static int _regulator_get_voltage(struct regulator_dev *rdev);
104 static int _regulator_get_current_limit(struct regulator_dev *rdev);
105 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
106 static int _notifier_call_chain(struct regulator_dev *rdev,
107                                   unsigned long event, void *data);
108 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
109                                      int min_uV, int max_uV);
110 static struct regulator *create_regulator(struct regulator_dev *rdev,
111                                           struct device *dev,
112                                           const char *supply_name);
113 static void _regulator_put(struct regulator *regulator);
114
115 static struct regulator_dev *dev_to_rdev(struct device *dev)
116 {
117         return container_of(dev, struct regulator_dev, dev);
118 }
119
120 static const char *rdev_get_name(struct regulator_dev *rdev)
121 {
122         if (rdev->constraints && rdev->constraints->name)
123                 return rdev->constraints->name;
124         else if (rdev->desc->name)
125                 return rdev->desc->name;
126         else
127                 return "";
128 }
129
130 static bool have_full_constraints(void)
131 {
132         return has_full_constraints || of_have_populated_dt();
133 }
134
135 /**
136  * of_get_regulator - get a regulator device node based on supply name
137  * @dev: Device pointer for the consumer (of regulator) device
138  * @supply: regulator supply name
139  *
140  * Extract the regulator device node corresponding to the supply name.
141  * returns the device node corresponding to the regulator if found, else
142  * returns NULL.
143  */
144 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
145 {
146         struct device_node *regnode = NULL;
147         char prop_name[32]; /* 32 is max size of property name */
148
149         dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
150
151         snprintf(prop_name, 32, "%s-supply", supply);
152         regnode = of_parse_phandle(dev->of_node, prop_name, 0);
153
154         if (!regnode) {
155                 dev_dbg(dev, "Looking up %s property in node %s failed",
156                                 prop_name, dev->of_node->full_name);
157                 return NULL;
158         }
159         return regnode;
160 }
161
162 static int _regulator_can_change_status(struct regulator_dev *rdev)
163 {
164         if (!rdev->constraints)
165                 return 0;
166
167         if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
168                 return 1;
169         else
170                 return 0;
171 }
172
173 /* Platform voltage constraint check */
174 static int regulator_check_voltage(struct regulator_dev *rdev,
175                                    int *min_uV, int *max_uV)
176 {
177         BUG_ON(*min_uV > *max_uV);
178
179         if (!rdev->constraints) {
180                 rdev_err(rdev, "no constraints\n");
181                 return -ENODEV;
182         }
183         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
184                 rdev_err(rdev, "voltage operation not allowed\n");
185                 return -EPERM;
186         }
187
188         if (*max_uV > rdev->constraints->max_uV)
189                 *max_uV = rdev->constraints->max_uV;
190         if (*min_uV < rdev->constraints->min_uV)
191                 *min_uV = rdev->constraints->min_uV;
192
193         if (*min_uV > *max_uV) {
194                 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
195                          *min_uV, *max_uV);
196                 return -EINVAL;
197         }
198
199         return 0;
200 }
201
202 /* Make sure we select a voltage that suits the needs of all
203  * regulator consumers
204  */
205 static int regulator_check_consumers(struct regulator_dev *rdev,
206                                      int *min_uV, int *max_uV)
207 {
208         struct regulator *regulator;
209
210         list_for_each_entry(regulator, &rdev->consumer_list, list) {
211                 /*
212                  * Assume consumers that didn't say anything are OK
213                  * with anything in the constraint range.
214                  */
215                 if (!regulator->min_uV && !regulator->max_uV)
216                         continue;
217
218                 if (*max_uV > regulator->max_uV)
219                         *max_uV = regulator->max_uV;
220                 if (*min_uV < regulator->min_uV)
221                         *min_uV = regulator->min_uV;
222         }
223
224         if (*min_uV > *max_uV) {
225                 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
226                         *min_uV, *max_uV);
227                 return -EINVAL;
228         }
229
230         return 0;
231 }
232
233 /* current constraint check */
234 static int regulator_check_current_limit(struct regulator_dev *rdev,
235                                         int *min_uA, int *max_uA)
236 {
237         BUG_ON(*min_uA > *max_uA);
238
239         if (!rdev->constraints) {
240                 rdev_err(rdev, "no constraints\n");
241                 return -ENODEV;
242         }
243         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
244                 rdev_err(rdev, "current operation not allowed\n");
245                 return -EPERM;
246         }
247
248         if (*max_uA > rdev->constraints->max_uA)
249                 *max_uA = rdev->constraints->max_uA;
250         if (*min_uA < rdev->constraints->min_uA)
251                 *min_uA = rdev->constraints->min_uA;
252
253         if (*min_uA > *max_uA) {
254                 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
255                          *min_uA, *max_uA);
256                 return -EINVAL;
257         }
258
259         return 0;
260 }
261
262 /* operating mode constraint check */
263 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
264 {
265         switch (*mode) {
266         case REGULATOR_MODE_FAST:
267         case REGULATOR_MODE_NORMAL:
268         case REGULATOR_MODE_IDLE:
269         case REGULATOR_MODE_STANDBY:
270                 break;
271         default:
272                 rdev_err(rdev, "invalid mode %x specified\n", *mode);
273                 return -EINVAL;
274         }
275
276         if (!rdev->constraints) {
277                 rdev_err(rdev, "no constraints\n");
278                 return -ENODEV;
279         }
280         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
281                 rdev_err(rdev, "mode operation not allowed\n");
282                 return -EPERM;
283         }
284
285         /* The modes are bitmasks, the most power hungry modes having
286          * the lowest values. If the requested mode isn't supported
287          * try higher modes. */
288         while (*mode) {
289                 if (rdev->constraints->valid_modes_mask & *mode)
290                         return 0;
291                 *mode /= 2;
292         }
293
294         return -EINVAL;
295 }
296
297 /* dynamic regulator mode switching constraint check */
298 static int regulator_check_drms(struct regulator_dev *rdev)
299 {
300         if (!rdev->constraints) {
301                 rdev_err(rdev, "no constraints\n");
302                 return -ENODEV;
303         }
304         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
305                 rdev_dbg(rdev, "drms operation not allowed\n");
306                 return -EPERM;
307         }
308         return 0;
309 }
310
311 static ssize_t regulator_uV_show(struct device *dev,
312                                 struct device_attribute *attr, char *buf)
313 {
314         struct regulator_dev *rdev = dev_get_drvdata(dev);
315         ssize_t ret;
316
317         mutex_lock(&rdev->mutex);
318         ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
319         mutex_unlock(&rdev->mutex);
320
321         return ret;
322 }
323 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
324
325 static ssize_t regulator_uA_show(struct device *dev,
326                                 struct device_attribute *attr, char *buf)
327 {
328         struct regulator_dev *rdev = dev_get_drvdata(dev);
329
330         return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
331 }
332 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
333
334 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
335                          char *buf)
336 {
337         struct regulator_dev *rdev = dev_get_drvdata(dev);
338
339         return sprintf(buf, "%s\n", rdev_get_name(rdev));
340 }
341 static DEVICE_ATTR_RO(name);
342
343 static ssize_t regulator_print_opmode(char *buf, int mode)
344 {
345         switch (mode) {
346         case REGULATOR_MODE_FAST:
347                 return sprintf(buf, "fast\n");
348         case REGULATOR_MODE_NORMAL:
349                 return sprintf(buf, "normal\n");
350         case REGULATOR_MODE_IDLE:
351                 return sprintf(buf, "idle\n");
352         case REGULATOR_MODE_STANDBY:
353                 return sprintf(buf, "standby\n");
354         }
355         return sprintf(buf, "unknown\n");
356 }
357
358 static ssize_t regulator_opmode_show(struct device *dev,
359                                     struct device_attribute *attr, char *buf)
360 {
361         struct regulator_dev *rdev = dev_get_drvdata(dev);
362
363         return regulator_print_opmode(buf, _regulator_get_mode(rdev));
364 }
365 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
366
367 static ssize_t regulator_print_state(char *buf, int state)
368 {
369         if (state > 0)
370                 return sprintf(buf, "enabled\n");
371         else if (state == 0)
372                 return sprintf(buf, "disabled\n");
373         else
374                 return sprintf(buf, "unknown\n");
375 }
376
377 static ssize_t regulator_state_show(struct device *dev,
378                                    struct device_attribute *attr, char *buf)
379 {
380         struct regulator_dev *rdev = dev_get_drvdata(dev);
381         ssize_t ret;
382
383         mutex_lock(&rdev->mutex);
384         ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
385         mutex_unlock(&rdev->mutex);
386
387         return ret;
388 }
389 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
390
391 static ssize_t regulator_status_show(struct device *dev,
392                                    struct device_attribute *attr, char *buf)
393 {
394         struct regulator_dev *rdev = dev_get_drvdata(dev);
395         int status;
396         char *label;
397
398         status = rdev->desc->ops->get_status(rdev);
399         if (status < 0)
400                 return status;
401
402         switch (status) {
403         case REGULATOR_STATUS_OFF:
404                 label = "off";
405                 break;
406         case REGULATOR_STATUS_ON:
407                 label = "on";
408                 break;
409         case REGULATOR_STATUS_ERROR:
410                 label = "error";
411                 break;
412         case REGULATOR_STATUS_FAST:
413                 label = "fast";
414                 break;
415         case REGULATOR_STATUS_NORMAL:
416                 label = "normal";
417                 break;
418         case REGULATOR_STATUS_IDLE:
419                 label = "idle";
420                 break;
421         case REGULATOR_STATUS_STANDBY:
422                 label = "standby";
423                 break;
424         case REGULATOR_STATUS_BYPASS:
425                 label = "bypass";
426                 break;
427         case REGULATOR_STATUS_UNDEFINED:
428                 label = "undefined";
429                 break;
430         default:
431                 return -ERANGE;
432         }
433
434         return sprintf(buf, "%s\n", label);
435 }
436 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
437
438 static ssize_t regulator_min_uA_show(struct device *dev,
439                                     struct device_attribute *attr, char *buf)
440 {
441         struct regulator_dev *rdev = dev_get_drvdata(dev);
442
443         if (!rdev->constraints)
444                 return sprintf(buf, "constraint not defined\n");
445
446         return sprintf(buf, "%d\n", rdev->constraints->min_uA);
447 }
448 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
449
450 static ssize_t regulator_max_uA_show(struct device *dev,
451                                     struct device_attribute *attr, char *buf)
452 {
453         struct regulator_dev *rdev = dev_get_drvdata(dev);
454
455         if (!rdev->constraints)
456                 return sprintf(buf, "constraint not defined\n");
457
458         return sprintf(buf, "%d\n", rdev->constraints->max_uA);
459 }
460 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
461
462 static ssize_t regulator_min_uV_show(struct device *dev,
463                                     struct device_attribute *attr, char *buf)
464 {
465         struct regulator_dev *rdev = dev_get_drvdata(dev);
466
467         if (!rdev->constraints)
468                 return sprintf(buf, "constraint not defined\n");
469
470         return sprintf(buf, "%d\n", rdev->constraints->min_uV);
471 }
472 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
473
474 static ssize_t regulator_max_uV_show(struct device *dev,
475                                     struct device_attribute *attr, char *buf)
476 {
477         struct regulator_dev *rdev = dev_get_drvdata(dev);
478
479         if (!rdev->constraints)
480                 return sprintf(buf, "constraint not defined\n");
481
482         return sprintf(buf, "%d\n", rdev->constraints->max_uV);
483 }
484 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
485
486 static ssize_t regulator_total_uA_show(struct device *dev,
487                                       struct device_attribute *attr, char *buf)
488 {
489         struct regulator_dev *rdev = dev_get_drvdata(dev);
490         struct regulator *regulator;
491         int uA = 0;
492
493         mutex_lock(&rdev->mutex);
494         list_for_each_entry(regulator, &rdev->consumer_list, list)
495                 uA += regulator->uA_load;
496         mutex_unlock(&rdev->mutex);
497         return sprintf(buf, "%d\n", uA);
498 }
499 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
500
501 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
502                               char *buf)
503 {
504         struct regulator_dev *rdev = dev_get_drvdata(dev);
505         return sprintf(buf, "%d\n", rdev->use_count);
506 }
507 static DEVICE_ATTR_RO(num_users);
508
509 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
510                          char *buf)
511 {
512         struct regulator_dev *rdev = dev_get_drvdata(dev);
513
514         switch (rdev->desc->type) {
515         case REGULATOR_VOLTAGE:
516                 return sprintf(buf, "voltage\n");
517         case REGULATOR_CURRENT:
518                 return sprintf(buf, "current\n");
519         }
520         return sprintf(buf, "unknown\n");
521 }
522 static DEVICE_ATTR_RO(type);
523
524 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
525                                 struct device_attribute *attr, char *buf)
526 {
527         struct regulator_dev *rdev = dev_get_drvdata(dev);
528
529         return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
530 }
531 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
532                 regulator_suspend_mem_uV_show, NULL);
533
534 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
535                                 struct device_attribute *attr, char *buf)
536 {
537         struct regulator_dev *rdev = dev_get_drvdata(dev);
538
539         return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
540 }
541 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
542                 regulator_suspend_disk_uV_show, NULL);
543
544 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
545                                 struct device_attribute *attr, char *buf)
546 {
547         struct regulator_dev *rdev = dev_get_drvdata(dev);
548
549         return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
550 }
551 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
552                 regulator_suspend_standby_uV_show, NULL);
553
554 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
555                                 struct device_attribute *attr, char *buf)
556 {
557         struct regulator_dev *rdev = dev_get_drvdata(dev);
558
559         return regulator_print_opmode(buf,
560                 rdev->constraints->state_mem.mode);
561 }
562 static DEVICE_ATTR(suspend_mem_mode, 0444,
563                 regulator_suspend_mem_mode_show, NULL);
564
565 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
566                                 struct device_attribute *attr, char *buf)
567 {
568         struct regulator_dev *rdev = dev_get_drvdata(dev);
569
570         return regulator_print_opmode(buf,
571                 rdev->constraints->state_disk.mode);
572 }
573 static DEVICE_ATTR(suspend_disk_mode, 0444,
574                 regulator_suspend_disk_mode_show, NULL);
575
576 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
577                                 struct device_attribute *attr, char *buf)
578 {
579         struct regulator_dev *rdev = dev_get_drvdata(dev);
580
581         return regulator_print_opmode(buf,
582                 rdev->constraints->state_standby.mode);
583 }
584 static DEVICE_ATTR(suspend_standby_mode, 0444,
585                 regulator_suspend_standby_mode_show, NULL);
586
587 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
588                                    struct device_attribute *attr, char *buf)
589 {
590         struct regulator_dev *rdev = dev_get_drvdata(dev);
591
592         return regulator_print_state(buf,
593                         rdev->constraints->state_mem.enabled);
594 }
595 static DEVICE_ATTR(suspend_mem_state, 0444,
596                 regulator_suspend_mem_state_show, NULL);
597
598 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
599                                    struct device_attribute *attr, char *buf)
600 {
601         struct regulator_dev *rdev = dev_get_drvdata(dev);
602
603         return regulator_print_state(buf,
604                         rdev->constraints->state_disk.enabled);
605 }
606 static DEVICE_ATTR(suspend_disk_state, 0444,
607                 regulator_suspend_disk_state_show, NULL);
608
609 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
610                                    struct device_attribute *attr, char *buf)
611 {
612         struct regulator_dev *rdev = dev_get_drvdata(dev);
613
614         return regulator_print_state(buf,
615                         rdev->constraints->state_standby.enabled);
616 }
617 static DEVICE_ATTR(suspend_standby_state, 0444,
618                 regulator_suspend_standby_state_show, NULL);
619
620 static ssize_t regulator_bypass_show(struct device *dev,
621                                      struct device_attribute *attr, char *buf)
622 {
623         struct regulator_dev *rdev = dev_get_drvdata(dev);
624         const char *report;
625         bool bypass;
626         int ret;
627
628         ret = rdev->desc->ops->get_bypass(rdev, &bypass);
629
630         if (ret != 0)
631                 report = "unknown";
632         else if (bypass)
633                 report = "enabled";
634         else
635                 report = "disabled";
636
637         return sprintf(buf, "%s\n", report);
638 }
639 static DEVICE_ATTR(bypass, 0444,
640                    regulator_bypass_show, NULL);
641
642 /* Calculate the new optimum regulator operating mode based on the new total
643  * consumer load. All locks held by caller */
644 static int drms_uA_update(struct regulator_dev *rdev)
645 {
646         struct regulator *sibling;
647         int current_uA = 0, output_uV, input_uV, err;
648         unsigned int mode;
649
650         lockdep_assert_held_once(&rdev->mutex);
651
652         /*
653          * first check to see if we can set modes at all, otherwise just
654          * tell the consumer everything is OK.
655          */
656         err = regulator_check_drms(rdev);
657         if (err < 0)
658                 return 0;
659
660         if (!rdev->desc->ops->get_optimum_mode &&
661             !rdev->desc->ops->set_load)
662                 return 0;
663
664         if (!rdev->desc->ops->set_mode &&
665             !rdev->desc->ops->set_load)
666                 return -EINVAL;
667
668         /* get output voltage */
669         output_uV = _regulator_get_voltage(rdev);
670         if (output_uV <= 0) {
671                 rdev_err(rdev, "invalid output voltage found\n");
672                 return -EINVAL;
673         }
674
675         /* get input voltage */
676         input_uV = 0;
677         if (rdev->supply)
678                 input_uV = regulator_get_voltage(rdev->supply);
679         if (input_uV <= 0)
680                 input_uV = rdev->constraints->input_uV;
681         if (input_uV <= 0) {
682                 rdev_err(rdev, "invalid input voltage found\n");
683                 return -EINVAL;
684         }
685
686         /* calc total requested load */
687         list_for_each_entry(sibling, &rdev->consumer_list, list)
688                 current_uA += sibling->uA_load;
689
690         current_uA += rdev->constraints->system_load;
691
692         if (rdev->desc->ops->set_load) {
693                 /* set the optimum mode for our new total regulator load */
694                 err = rdev->desc->ops->set_load(rdev, current_uA);
695                 if (err < 0)
696                         rdev_err(rdev, "failed to set load %d\n", current_uA);
697         } else {
698                 /* now get the optimum mode for our new total regulator load */
699                 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
700                                                          output_uV, current_uA);
701
702                 /* check the new mode is allowed */
703                 err = regulator_mode_constrain(rdev, &mode);
704                 if (err < 0) {
705                         rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
706                                  current_uA, input_uV, output_uV);
707                         return err;
708                 }
709
710                 err = rdev->desc->ops->set_mode(rdev, mode);
711                 if (err < 0)
712                         rdev_err(rdev, "failed to set optimum mode %x\n", mode);
713         }
714
715         return err;
716 }
717
718 static int suspend_set_state(struct regulator_dev *rdev,
719         struct regulator_state *rstate)
720 {
721         int ret = 0;
722
723         /* If we have no suspend mode configration don't set anything;
724          * only warn if the driver implements set_suspend_voltage or
725          * set_suspend_mode callback.
726          */
727         if (!rstate->enabled && !rstate->disabled) {
728                 if (rdev->desc->ops->set_suspend_voltage ||
729                     rdev->desc->ops->set_suspend_mode)
730                         rdev_warn(rdev, "No configuration\n");
731                 return 0;
732         }
733
734         if (rstate->enabled && rstate->disabled) {
735                 rdev_err(rdev, "invalid configuration\n");
736                 return -EINVAL;
737         }
738
739         if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
740                 ret = rdev->desc->ops->set_suspend_enable(rdev);
741         else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
742                 ret = rdev->desc->ops->set_suspend_disable(rdev);
743         else /* OK if set_suspend_enable or set_suspend_disable is NULL */
744                 ret = 0;
745
746         if (ret < 0) {
747                 rdev_err(rdev, "failed to enabled/disable\n");
748                 return ret;
749         }
750
751         if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
752                 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
753                 if (ret < 0) {
754                         rdev_err(rdev, "failed to set voltage\n");
755                         return ret;
756                 }
757         }
758
759         if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
760                 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
761                 if (ret < 0) {
762                         rdev_err(rdev, "failed to set mode\n");
763                         return ret;
764                 }
765         }
766         return ret;
767 }
768
769 /* locks held by caller */
770 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
771 {
772         lockdep_assert_held_once(&rdev->mutex);
773
774         if (!rdev->constraints)
775                 return -EINVAL;
776
777         switch (state) {
778         case PM_SUSPEND_STANDBY:
779                 return suspend_set_state(rdev,
780                         &rdev->constraints->state_standby);
781         case PM_SUSPEND_MEM:
782                 return suspend_set_state(rdev,
783                         &rdev->constraints->state_mem);
784         case PM_SUSPEND_MAX:
785                 return suspend_set_state(rdev,
786                         &rdev->constraints->state_disk);
787         default:
788                 return -EINVAL;
789         }
790 }
791
792 static void print_constraints(struct regulator_dev *rdev)
793 {
794         struct regulation_constraints *constraints = rdev->constraints;
795         char buf[160] = "";
796         size_t len = sizeof(buf) - 1;
797         int count = 0;
798         int ret;
799
800         if (constraints->min_uV && constraints->max_uV) {
801                 if (constraints->min_uV == constraints->max_uV)
802                         count += scnprintf(buf + count, len - count, "%d mV ",
803                                            constraints->min_uV / 1000);
804                 else
805                         count += scnprintf(buf + count, len - count,
806                                            "%d <--> %d mV ",
807                                            constraints->min_uV / 1000,
808                                            constraints->max_uV / 1000);
809         }
810
811         if (!constraints->min_uV ||
812             constraints->min_uV != constraints->max_uV) {
813                 ret = _regulator_get_voltage(rdev);
814                 if (ret > 0)
815                         count += scnprintf(buf + count, len - count,
816                                            "at %d mV ", ret / 1000);
817         }
818
819         if (constraints->uV_offset)
820                 count += scnprintf(buf + count, len - count, "%dmV offset ",
821                                    constraints->uV_offset / 1000);
822
823         if (constraints->min_uA && constraints->max_uA) {
824                 if (constraints->min_uA == constraints->max_uA)
825                         count += scnprintf(buf + count, len - count, "%d mA ",
826                                            constraints->min_uA / 1000);
827                 else
828                         count += scnprintf(buf + count, len - count,
829                                            "%d <--> %d mA ",
830                                            constraints->min_uA / 1000,
831                                            constraints->max_uA / 1000);
832         }
833
834         if (!constraints->min_uA ||
835             constraints->min_uA != constraints->max_uA) {
836                 ret = _regulator_get_current_limit(rdev);
837                 if (ret > 0)
838                         count += scnprintf(buf + count, len - count,
839                                            "at %d mA ", ret / 1000);
840         }
841
842         if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
843                 count += scnprintf(buf + count, len - count, "fast ");
844         if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
845                 count += scnprintf(buf + count, len - count, "normal ");
846         if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
847                 count += scnprintf(buf + count, len - count, "idle ");
848         if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
849                 count += scnprintf(buf + count, len - count, "standby");
850
851         if (!count)
852                 scnprintf(buf, len, "no parameters");
853
854         rdev_dbg(rdev, "%s\n", buf);
855
856         if ((constraints->min_uV != constraints->max_uV) &&
857             !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
858                 rdev_warn(rdev,
859                           "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
860 }
861
862 static int machine_constraints_voltage(struct regulator_dev *rdev,
863         struct regulation_constraints *constraints)
864 {
865         const struct regulator_ops *ops = rdev->desc->ops;
866         int ret;
867
868         /* do we need to apply the constraint voltage */
869         if (rdev->constraints->apply_uV &&
870             rdev->constraints->min_uV == rdev->constraints->max_uV) {
871                 int current_uV = _regulator_get_voltage(rdev);
872                 if (current_uV < 0) {
873                         rdev_err(rdev,
874                                  "failed to get the current voltage(%d)\n",
875                                  current_uV);
876                         return current_uV;
877                 }
878                 if (current_uV < rdev->constraints->min_uV ||
879                     current_uV > rdev->constraints->max_uV) {
880                         ret = _regulator_do_set_voltage(
881                                 rdev, rdev->constraints->min_uV,
882                                 rdev->constraints->max_uV);
883                         if (ret < 0) {
884                                 rdev_err(rdev,
885                                         "failed to apply %duV constraint(%d)\n",
886                                         rdev->constraints->min_uV, ret);
887                                 return ret;
888                         }
889                 }
890         }
891
892         /* constrain machine-level voltage specs to fit
893          * the actual range supported by this regulator.
894          */
895         if (ops->list_voltage && rdev->desc->n_voltages) {
896                 int     count = rdev->desc->n_voltages;
897                 int     i;
898                 int     min_uV = INT_MAX;
899                 int     max_uV = INT_MIN;
900                 int     cmin = constraints->min_uV;
901                 int     cmax = constraints->max_uV;
902
903                 /* it's safe to autoconfigure fixed-voltage supplies
904                    and the constraints are used by list_voltage. */
905                 if (count == 1 && !cmin) {
906                         cmin = 1;
907                         cmax = INT_MAX;
908                         constraints->min_uV = cmin;
909                         constraints->max_uV = cmax;
910                 }
911
912                 /* voltage constraints are optional */
913                 if ((cmin == 0) && (cmax == 0))
914                         return 0;
915
916                 /* else require explicit machine-level constraints */
917                 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
918                         rdev_err(rdev, "invalid voltage constraints\n");
919                         return -EINVAL;
920                 }
921
922                 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
923                 for (i = 0; i < count; i++) {
924                         int     value;
925
926                         value = ops->list_voltage(rdev, i);
927                         if (value <= 0)
928                                 continue;
929
930                         /* maybe adjust [min_uV..max_uV] */
931                         if (value >= cmin && value < min_uV)
932                                 min_uV = value;
933                         if (value <= cmax && value > max_uV)
934                                 max_uV = value;
935                 }
936
937                 /* final: [min_uV..max_uV] valid iff constraints valid */
938                 if (max_uV < min_uV) {
939                         rdev_err(rdev,
940                                  "unsupportable voltage constraints %u-%uuV\n",
941                                  min_uV, max_uV);
942                         return -EINVAL;
943                 }
944
945                 /* use regulator's subset of machine constraints */
946                 if (constraints->min_uV < min_uV) {
947                         rdev_dbg(rdev, "override min_uV, %d -> %d\n",
948                                  constraints->min_uV, min_uV);
949                         constraints->min_uV = min_uV;
950                 }
951                 if (constraints->max_uV > max_uV) {
952                         rdev_dbg(rdev, "override max_uV, %d -> %d\n",
953                                  constraints->max_uV, max_uV);
954                         constraints->max_uV = max_uV;
955                 }
956         }
957
958         return 0;
959 }
960
961 static int machine_constraints_current(struct regulator_dev *rdev,
962         struct regulation_constraints *constraints)
963 {
964         const struct regulator_ops *ops = rdev->desc->ops;
965         int ret;
966
967         if (!constraints->min_uA && !constraints->max_uA)
968                 return 0;
969
970         if (constraints->min_uA > constraints->max_uA) {
971                 rdev_err(rdev, "Invalid current constraints\n");
972                 return -EINVAL;
973         }
974
975         if (!ops->set_current_limit || !ops->get_current_limit) {
976                 rdev_warn(rdev, "Operation of current configuration missing\n");
977                 return 0;
978         }
979
980         /* Set regulator current in constraints range */
981         ret = ops->set_current_limit(rdev, constraints->min_uA,
982                         constraints->max_uA);
983         if (ret < 0) {
984                 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
985                 return ret;
986         }
987
988         return 0;
989 }
990
991 static int _regulator_do_enable(struct regulator_dev *rdev);
992
993 /**
994  * set_machine_constraints - sets regulator constraints
995  * @rdev: regulator source
996  * @constraints: constraints to apply
997  *
998  * Allows platform initialisation code to define and constrain
999  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
1000  * Constraints *must* be set by platform code in order for some
1001  * regulator operations to proceed i.e. set_voltage, set_current_limit,
1002  * set_mode.
1003  */
1004 static int set_machine_constraints(struct regulator_dev *rdev,
1005         const struct regulation_constraints *constraints)
1006 {
1007         int ret = 0;
1008         const struct regulator_ops *ops = rdev->desc->ops;
1009
1010         if (constraints)
1011                 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1012                                             GFP_KERNEL);
1013         else
1014                 rdev->constraints = kzalloc(sizeof(*constraints),
1015                                             GFP_KERNEL);
1016         if (!rdev->constraints)
1017                 return -ENOMEM;
1018
1019         ret = machine_constraints_voltage(rdev, rdev->constraints);
1020         if (ret != 0)
1021                 goto out;
1022
1023         ret = machine_constraints_current(rdev, rdev->constraints);
1024         if (ret != 0)
1025                 goto out;
1026
1027         if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1028                 ret = ops->set_input_current_limit(rdev,
1029                                                    rdev->constraints->ilim_uA);
1030                 if (ret < 0) {
1031                         rdev_err(rdev, "failed to set input limit\n");
1032                         goto out;
1033                 }
1034         }
1035
1036         /* do we need to setup our suspend state */
1037         if (rdev->constraints->initial_state) {
1038                 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
1039                 if (ret < 0) {
1040                         rdev_err(rdev, "failed to set suspend state\n");
1041                         goto out;
1042                 }
1043         }
1044
1045         if (rdev->constraints->initial_mode) {
1046                 if (!ops->set_mode) {
1047                         rdev_err(rdev, "no set_mode operation\n");
1048                         ret = -EINVAL;
1049                         goto out;
1050                 }
1051
1052                 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1053                 if (ret < 0) {
1054                         rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1055                         goto out;
1056                 }
1057         }
1058
1059         /* If the constraints say the regulator should be on at this point
1060          * and we have control then make sure it is enabled.
1061          */
1062         if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1063                 ret = _regulator_do_enable(rdev);
1064                 if (ret < 0 && ret != -EINVAL) {
1065                         rdev_err(rdev, "failed to enable\n");
1066                         goto out;
1067                 }
1068         }
1069
1070         if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1071                 && ops->set_ramp_delay) {
1072                 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1073                 if (ret < 0) {
1074                         rdev_err(rdev, "failed to set ramp_delay\n");
1075                         goto out;
1076                 }
1077         }
1078
1079         if (rdev->constraints->pull_down && ops->set_pull_down) {
1080                 ret = ops->set_pull_down(rdev);
1081                 if (ret < 0) {
1082                         rdev_err(rdev, "failed to set pull down\n");
1083                         goto out;
1084                 }
1085         }
1086
1087         if (rdev->constraints->soft_start && ops->set_soft_start) {
1088                 ret = ops->set_soft_start(rdev);
1089                 if (ret < 0) {
1090                         rdev_err(rdev, "failed to set soft start\n");
1091                         goto out;
1092                 }
1093         }
1094
1095         if (rdev->constraints->over_current_protection
1096                 && ops->set_over_current_protection) {
1097                 ret = ops->set_over_current_protection(rdev);
1098                 if (ret < 0) {
1099                         rdev_err(rdev, "failed to set over current protection\n");
1100                         goto out;
1101                 }
1102         }
1103
1104         print_constraints(rdev);
1105         return 0;
1106 out:
1107         kfree(rdev->constraints);
1108         rdev->constraints = NULL;
1109         return ret;
1110 }
1111
1112 /**
1113  * set_supply - set regulator supply regulator
1114  * @rdev: regulator name
1115  * @supply_rdev: supply regulator name
1116  *
1117  * Called by platform initialisation code to set the supply regulator for this
1118  * regulator. This ensures that a regulators supply will also be enabled by the
1119  * core if it's child is enabled.
1120  */
1121 static int set_supply(struct regulator_dev *rdev,
1122                       struct regulator_dev *supply_rdev)
1123 {
1124         int err;
1125
1126         rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1127
1128         if (!try_module_get(supply_rdev->owner))
1129                 return -ENODEV;
1130
1131         rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1132         if (rdev->supply == NULL) {
1133                 err = -ENOMEM;
1134                 return err;
1135         }
1136         supply_rdev->open_count++;
1137
1138         return 0;
1139 }
1140
1141 /**
1142  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1143  * @rdev:         regulator source
1144  * @consumer_dev_name: dev_name() string for device supply applies to
1145  * @supply:       symbolic name for supply
1146  *
1147  * Allows platform initialisation code to map physical regulator
1148  * sources to symbolic names for supplies for use by devices.  Devices
1149  * should use these symbolic names to request regulators, avoiding the
1150  * need to provide board-specific regulator names as platform data.
1151  */
1152 static int set_consumer_device_supply(struct regulator_dev *rdev,
1153                                       const char *consumer_dev_name,
1154                                       const char *supply)
1155 {
1156         struct regulator_map *node;
1157         int has_dev;
1158
1159         if (supply == NULL)
1160                 return -EINVAL;
1161
1162         if (consumer_dev_name != NULL)
1163                 has_dev = 1;
1164         else
1165                 has_dev = 0;
1166
1167         list_for_each_entry(node, &regulator_map_list, list) {
1168                 if (node->dev_name && consumer_dev_name) {
1169                         if (strcmp(node->dev_name, consumer_dev_name) != 0)
1170                                 continue;
1171                 } else if (node->dev_name || consumer_dev_name) {
1172                         continue;
1173                 }
1174
1175                 if (strcmp(node->supply, supply) != 0)
1176                         continue;
1177
1178                 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1179                          consumer_dev_name,
1180                          dev_name(&node->regulator->dev),
1181                          node->regulator->desc->name,
1182                          supply,
1183                          dev_name(&rdev->dev), rdev_get_name(rdev));
1184                 return -EBUSY;
1185         }
1186
1187         node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1188         if (node == NULL)
1189                 return -ENOMEM;
1190
1191         node->regulator = rdev;
1192         node->supply = supply;
1193
1194         if (has_dev) {
1195                 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1196                 if (node->dev_name == NULL) {
1197                         kfree(node);
1198                         return -ENOMEM;
1199                 }
1200         }
1201
1202         list_add(&node->list, &regulator_map_list);
1203         return 0;
1204 }
1205
1206 static void unset_regulator_supplies(struct regulator_dev *rdev)
1207 {
1208         struct regulator_map *node, *n;
1209
1210         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1211                 if (rdev == node->regulator) {
1212                         list_del(&node->list);
1213                         kfree(node->dev_name);
1214                         kfree(node);
1215                 }
1216         }
1217 }
1218
1219 #define REG_STR_SIZE    64
1220
1221 static struct regulator *create_regulator(struct regulator_dev *rdev,
1222                                           struct device *dev,
1223                                           const char *supply_name)
1224 {
1225         struct regulator *regulator;
1226         char buf[REG_STR_SIZE];
1227         int err, size;
1228
1229         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1230         if (regulator == NULL)
1231                 return NULL;
1232
1233         mutex_lock(&rdev->mutex);
1234         regulator->rdev = rdev;
1235         list_add(&regulator->list, &rdev->consumer_list);
1236
1237         if (dev) {
1238                 regulator->dev = dev;
1239
1240                 /* Add a link to the device sysfs entry */
1241                 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1242                                  dev->kobj.name, supply_name);
1243                 if (size >= REG_STR_SIZE)
1244                         goto overflow_err;
1245
1246                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1247                 if (regulator->supply_name == NULL)
1248                         goto overflow_err;
1249
1250                 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1251                                         buf);
1252                 if (err) {
1253                         rdev_dbg(rdev, "could not add device link %s err %d\n",
1254                                   dev->kobj.name, err);
1255                         /* non-fatal */
1256                 }
1257         } else {
1258                 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1259                 if (regulator->supply_name == NULL)
1260                         goto overflow_err;
1261         }
1262
1263         regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1264                                                 rdev->debugfs);
1265         if (!regulator->debugfs) {
1266                 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1267         } else {
1268                 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1269                                    &regulator->uA_load);
1270                 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1271                                    &regulator->min_uV);
1272                 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1273                                    &regulator->max_uV);
1274         }
1275
1276         /*
1277          * Check now if the regulator is an always on regulator - if
1278          * it is then we don't need to do nearly so much work for
1279          * enable/disable calls.
1280          */
1281         if (!_regulator_can_change_status(rdev) &&
1282             _regulator_is_enabled(rdev))
1283                 regulator->always_on = true;
1284
1285         mutex_unlock(&rdev->mutex);
1286         return regulator;
1287 overflow_err:
1288         list_del(&regulator->list);
1289         kfree(regulator);
1290         mutex_unlock(&rdev->mutex);
1291         return NULL;
1292 }
1293
1294 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1295 {
1296         if (rdev->constraints && rdev->constraints->enable_time)
1297                 return rdev->constraints->enable_time;
1298         if (!rdev->desc->ops->enable_time)
1299                 return rdev->desc->enable_time;
1300         return rdev->desc->ops->enable_time(rdev);
1301 }
1302
1303 static struct regulator_supply_alias *regulator_find_supply_alias(
1304                 struct device *dev, const char *supply)
1305 {
1306         struct regulator_supply_alias *map;
1307
1308         list_for_each_entry(map, &regulator_supply_alias_list, list)
1309                 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1310                         return map;
1311
1312         return NULL;
1313 }
1314
1315 static void regulator_supply_alias(struct device **dev, const char **supply)
1316 {
1317         struct regulator_supply_alias *map;
1318
1319         map = regulator_find_supply_alias(*dev, *supply);
1320         if (map) {
1321                 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1322                                 *supply, map->alias_supply,
1323                                 dev_name(map->alias_dev));
1324                 *dev = map->alias_dev;
1325                 *supply = map->alias_supply;
1326         }
1327 }
1328
1329 static int of_node_match(struct device *dev, const void *data)
1330 {
1331         return dev->of_node == data;
1332 }
1333
1334 static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
1335 {
1336         struct device *dev;
1337
1338         dev = class_find_device(&regulator_class, NULL, np, of_node_match);
1339
1340         return dev ? dev_to_rdev(dev) : NULL;
1341 }
1342
1343 static int regulator_match(struct device *dev, const void *data)
1344 {
1345         struct regulator_dev *r = dev_to_rdev(dev);
1346
1347         return strcmp(rdev_get_name(r), data) == 0;
1348 }
1349
1350 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1351 {
1352         struct device *dev;
1353
1354         dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1355
1356         return dev ? dev_to_rdev(dev) : NULL;
1357 }
1358
1359 /**
1360  * regulator_dev_lookup - lookup a regulator device.
1361  * @dev: device for regulator "consumer".
1362  * @supply: Supply name or regulator ID.
1363  * @ret: 0 on success, -ENODEV if lookup fails permanently, -EPROBE_DEFER if
1364  * lookup could succeed in the future.
1365  *
1366  * If successful, returns a struct regulator_dev that corresponds to the name
1367  * @supply and with the embedded struct device refcount incremented by one,
1368  * or NULL on failure. The refcount must be dropped by calling put_device().
1369  */
1370 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1371                                                   const char *supply,
1372                                                   int *ret)
1373 {
1374         struct regulator_dev *r;
1375         struct device_node *node;
1376         struct regulator_map *map;
1377         const char *devname = NULL;
1378
1379         regulator_supply_alias(&dev, &supply);
1380
1381         /* first do a dt based lookup */
1382         if (dev && dev->of_node) {
1383                 node = of_get_regulator(dev, supply);
1384                 if (node) {
1385                         r = of_find_regulator_by_node(node);
1386                         if (r)
1387                                 return r;
1388                         *ret = -EPROBE_DEFER;
1389                         return NULL;
1390                 } else {
1391                         /*
1392                          * If we couldn't even get the node then it's
1393                          * not just that the device didn't register
1394                          * yet, there's no node and we'll never
1395                          * succeed.
1396                          */
1397                         *ret = -ENODEV;
1398                 }
1399         }
1400
1401         /* if not found, try doing it non-dt way */
1402         if (dev)
1403                 devname = dev_name(dev);
1404
1405         r = regulator_lookup_by_name(supply);
1406         if (r)
1407                 return r;
1408
1409         mutex_lock(&regulator_list_mutex);
1410         list_for_each_entry(map, &regulator_map_list, list) {
1411                 /* If the mapping has a device set up it must match */
1412                 if (map->dev_name &&
1413                     (!devname || strcmp(map->dev_name, devname)))
1414                         continue;
1415
1416                 if (strcmp(map->supply, supply) == 0 &&
1417                     get_device(&map->regulator->dev)) {
1418                         mutex_unlock(&regulator_list_mutex);
1419                         return map->regulator;
1420                 }
1421         }
1422         mutex_unlock(&regulator_list_mutex);
1423
1424         return NULL;
1425 }
1426
1427 static int regulator_resolve_supply(struct regulator_dev *rdev)
1428 {
1429         struct regulator_dev *r;
1430         struct device *dev = rdev->dev.parent;
1431         int ret;
1432
1433         /* No supply to resovle? */
1434         if (!rdev->supply_name)
1435                 return 0;
1436
1437         /* Supply already resolved? */
1438         if (rdev->supply)
1439                 return 0;
1440
1441         r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1442         if (!r) {
1443                 if (ret == -ENODEV) {
1444                         /*
1445                          * No supply was specified for this regulator and
1446                          * there will never be one.
1447                          */
1448                         return 0;
1449                 }
1450
1451                 /* Did the lookup explicitly defer for us? */
1452                 if (ret == -EPROBE_DEFER)
1453                         return ret;
1454
1455                 if (have_full_constraints()) {
1456                         r = dummy_regulator_rdev;
1457                         get_device(&r->dev);
1458                 } else {
1459                         dev_err(dev, "Failed to resolve %s-supply for %s\n",
1460                                 rdev->supply_name, rdev->desc->name);
1461                         return -EPROBE_DEFER;
1462                 }
1463         }
1464
1465         /* Recursively resolve the supply of the supply */
1466         ret = regulator_resolve_supply(r);
1467         if (ret < 0) {
1468                 put_device(&r->dev);
1469                 return ret;
1470         }
1471
1472         ret = set_supply(rdev, r);
1473         if (ret < 0) {
1474                 put_device(&r->dev);
1475                 return ret;
1476         }
1477
1478         /* Cascade always-on state to supply */
1479         if (_regulator_is_enabled(rdev) && rdev->supply) {
1480                 ret = regulator_enable(rdev->supply);
1481                 if (ret < 0) {
1482                         _regulator_put(rdev->supply);
1483                         return ret;
1484                 }
1485         }
1486
1487         return 0;
1488 }
1489
1490 /* Internal regulator request function */
1491 static struct regulator *_regulator_get(struct device *dev, const char *id,
1492                                         bool exclusive, bool allow_dummy)
1493 {
1494         struct regulator_dev *rdev;
1495         struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1496         const char *devname = NULL;
1497         int ret;
1498
1499         if (id == NULL) {
1500                 pr_err("get() with no identifier\n");
1501                 return ERR_PTR(-EINVAL);
1502         }
1503
1504         if (dev)
1505                 devname = dev_name(dev);
1506
1507         if (have_full_constraints())
1508                 ret = -ENODEV;
1509         else
1510                 ret = -EPROBE_DEFER;
1511
1512         rdev = regulator_dev_lookup(dev, id, &ret);
1513         if (rdev)
1514                 goto found;
1515
1516         regulator = ERR_PTR(ret);
1517
1518         /*
1519          * If we have return value from dev_lookup fail, we do not expect to
1520          * succeed, so, quit with appropriate error value
1521          */
1522         if (ret && ret != -ENODEV)
1523                 return regulator;
1524
1525         if (!devname)
1526                 devname = "deviceless";
1527
1528         /*
1529          * Assume that a regulator is physically present and enabled
1530          * even if it isn't hooked up and just provide a dummy.
1531          */
1532         if (have_full_constraints() && allow_dummy) {
1533                 pr_warn("%s supply %s not found, using dummy regulator\n",
1534                         devname, id);
1535
1536                 rdev = dummy_regulator_rdev;
1537                 get_device(&rdev->dev);
1538                 goto found;
1539         /* Don't log an error when called from regulator_get_optional() */
1540         } else if (!have_full_constraints() || exclusive) {
1541                 dev_warn(dev, "dummy supplies not allowed\n");
1542         }
1543
1544         return regulator;
1545
1546 found:
1547         if (rdev->exclusive) {
1548                 regulator = ERR_PTR(-EPERM);
1549                 put_device(&rdev->dev);
1550                 return regulator;
1551         }
1552
1553         if (exclusive && rdev->open_count) {
1554                 regulator = ERR_PTR(-EBUSY);
1555                 put_device(&rdev->dev);
1556                 return regulator;
1557         }
1558
1559         ret = regulator_resolve_supply(rdev);
1560         if (ret < 0) {
1561                 regulator = ERR_PTR(ret);
1562                 put_device(&rdev->dev);
1563                 return regulator;
1564         }
1565
1566         if (!try_module_get(rdev->owner)) {
1567                 put_device(&rdev->dev);
1568                 return regulator;
1569         }
1570
1571         regulator = create_regulator(rdev, dev, id);
1572         if (regulator == NULL) {
1573                 regulator = ERR_PTR(-ENOMEM);
1574                 put_device(&rdev->dev);
1575                 module_put(rdev->owner);
1576                 return regulator;
1577         }
1578
1579         rdev->open_count++;
1580         if (exclusive) {
1581                 rdev->exclusive = 1;
1582
1583                 ret = _regulator_is_enabled(rdev);
1584                 if (ret > 0)
1585                         rdev->use_count = 1;
1586                 else
1587                         rdev->use_count = 0;
1588         }
1589
1590         return regulator;
1591 }
1592
1593 /**
1594  * regulator_get - lookup and obtain a reference to a regulator.
1595  * @dev: device for regulator "consumer"
1596  * @id: Supply name or regulator ID.
1597  *
1598  * Returns a struct regulator corresponding to the regulator producer,
1599  * or IS_ERR() condition containing errno.
1600  *
1601  * Use of supply names configured via regulator_set_device_supply() is
1602  * strongly encouraged.  It is recommended that the supply name used
1603  * should match the name used for the supply and/or the relevant
1604  * device pins in the datasheet.
1605  */
1606 struct regulator *regulator_get(struct device *dev, const char *id)
1607 {
1608         return _regulator_get(dev, id, false, true);
1609 }
1610 EXPORT_SYMBOL_GPL(regulator_get);
1611
1612 /**
1613  * regulator_get_exclusive - obtain exclusive access to a regulator.
1614  * @dev: device for regulator "consumer"
1615  * @id: Supply name or regulator ID.
1616  *
1617  * Returns a struct regulator corresponding to the regulator producer,
1618  * or IS_ERR() condition containing errno.  Other consumers will be
1619  * unable to obtain this regulator while this reference is held and the
1620  * use count for the regulator will be initialised to reflect the current
1621  * state of the regulator.
1622  *
1623  * This is intended for use by consumers which cannot tolerate shared
1624  * use of the regulator such as those which need to force the
1625  * regulator off for correct operation of the hardware they are
1626  * controlling.
1627  *
1628  * Use of supply names configured via regulator_set_device_supply() is
1629  * strongly encouraged.  It is recommended that the supply name used
1630  * should match the name used for the supply and/or the relevant
1631  * device pins in the datasheet.
1632  */
1633 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1634 {
1635         return _regulator_get(dev, id, true, false);
1636 }
1637 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1638
1639 /**
1640  * regulator_get_optional - obtain optional access to a regulator.
1641  * @dev: device for regulator "consumer"
1642  * @id: Supply name or regulator ID.
1643  *
1644  * Returns a struct regulator corresponding to the regulator producer,
1645  * or IS_ERR() condition containing errno.
1646  *
1647  * This is intended for use by consumers for devices which can have
1648  * some supplies unconnected in normal use, such as some MMC devices.
1649  * It can allow the regulator core to provide stub supplies for other
1650  * supplies requested using normal regulator_get() calls without
1651  * disrupting the operation of drivers that can handle absent
1652  * supplies.
1653  *
1654  * Use of supply names configured via regulator_set_device_supply() is
1655  * strongly encouraged.  It is recommended that the supply name used
1656  * should match the name used for the supply and/or the relevant
1657  * device pins in the datasheet.
1658  */
1659 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1660 {
1661         return _regulator_get(dev, id, false, false);
1662 }
1663 EXPORT_SYMBOL_GPL(regulator_get_optional);
1664
1665 /* regulator_list_mutex lock held by regulator_put() */
1666 static void _regulator_put(struct regulator *regulator)
1667 {
1668         struct regulator_dev *rdev;
1669
1670         if (IS_ERR_OR_NULL(regulator))
1671                 return;
1672
1673         lockdep_assert_held_once(&regulator_list_mutex);
1674
1675         rdev = regulator->rdev;
1676
1677         debugfs_remove_recursive(regulator->debugfs);
1678
1679         /* remove any sysfs entries */
1680         if (regulator->dev)
1681                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1682         mutex_lock(&rdev->mutex);
1683         list_del(&regulator->list);
1684
1685         rdev->open_count--;
1686         rdev->exclusive = 0;
1687         put_device(&rdev->dev);
1688         mutex_unlock(&rdev->mutex);
1689
1690         kfree(regulator->supply_name);
1691         kfree(regulator);
1692
1693         module_put(rdev->owner);
1694 }
1695
1696 /**
1697  * regulator_put - "free" the regulator source
1698  * @regulator: regulator source
1699  *
1700  * Note: drivers must ensure that all regulator_enable calls made on this
1701  * regulator source are balanced by regulator_disable calls prior to calling
1702  * this function.
1703  */
1704 void regulator_put(struct regulator *regulator)
1705 {
1706         mutex_lock(&regulator_list_mutex);
1707         _regulator_put(regulator);
1708         mutex_unlock(&regulator_list_mutex);
1709 }
1710 EXPORT_SYMBOL_GPL(regulator_put);
1711
1712 /**
1713  * regulator_register_supply_alias - Provide device alias for supply lookup
1714  *
1715  * @dev: device that will be given as the regulator "consumer"
1716  * @id: Supply name or regulator ID
1717  * @alias_dev: device that should be used to lookup the supply
1718  * @alias_id: Supply name or regulator ID that should be used to lookup the
1719  * supply
1720  *
1721  * All lookups for id on dev will instead be conducted for alias_id on
1722  * alias_dev.
1723  */
1724 int regulator_register_supply_alias(struct device *dev, const char *id,
1725                                     struct device *alias_dev,
1726                                     const char *alias_id)
1727 {
1728         struct regulator_supply_alias *map;
1729
1730         map = regulator_find_supply_alias(dev, id);
1731         if (map)
1732                 return -EEXIST;
1733
1734         map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1735         if (!map)
1736                 return -ENOMEM;
1737
1738         map->src_dev = dev;
1739         map->src_supply = id;
1740         map->alias_dev = alias_dev;
1741         map->alias_supply = alias_id;
1742
1743         list_add(&map->list, &regulator_supply_alias_list);
1744
1745         pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1746                 id, dev_name(dev), alias_id, dev_name(alias_dev));
1747
1748         return 0;
1749 }
1750 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1751
1752 /**
1753  * regulator_unregister_supply_alias - Remove device alias
1754  *
1755  * @dev: device that will be given as the regulator "consumer"
1756  * @id: Supply name or regulator ID
1757  *
1758  * Remove a lookup alias if one exists for id on dev.
1759  */
1760 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1761 {
1762         struct regulator_supply_alias *map;
1763
1764         map = regulator_find_supply_alias(dev, id);
1765         if (map) {
1766                 list_del(&map->list);
1767                 kfree(map);
1768         }
1769 }
1770 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1771
1772 /**
1773  * regulator_bulk_register_supply_alias - register multiple aliases
1774  *
1775  * @dev: device that will be given as the regulator "consumer"
1776  * @id: List of supply names or regulator IDs
1777  * @alias_dev: device that should be used to lookup the supply
1778  * @alias_id: List of supply names or regulator IDs that should be used to
1779  * lookup the supply
1780  * @num_id: Number of aliases to register
1781  *
1782  * @return 0 on success, an errno on failure.
1783  *
1784  * This helper function allows drivers to register several supply
1785  * aliases in one operation.  If any of the aliases cannot be
1786  * registered any aliases that were registered will be removed
1787  * before returning to the caller.
1788  */
1789 int regulator_bulk_register_supply_alias(struct device *dev,
1790                                          const char *const *id,
1791                                          struct device *alias_dev,
1792                                          const char *const *alias_id,
1793                                          int num_id)
1794 {
1795         int i;
1796         int ret;
1797
1798         for (i = 0; i < num_id; ++i) {
1799                 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1800                                                       alias_id[i]);
1801                 if (ret < 0)
1802                         goto err;
1803         }
1804
1805         return 0;
1806
1807 err:
1808         dev_err(dev,
1809                 "Failed to create supply alias %s,%s -> %s,%s\n",
1810                 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1811
1812         while (--i >= 0)
1813                 regulator_unregister_supply_alias(dev, id[i]);
1814
1815         return ret;
1816 }
1817 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1818
1819 /**
1820  * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1821  *
1822  * @dev: device that will be given as the regulator "consumer"
1823  * @id: List of supply names or regulator IDs
1824  * @num_id: Number of aliases to unregister
1825  *
1826  * This helper function allows drivers to unregister several supply
1827  * aliases in one operation.
1828  */
1829 void regulator_bulk_unregister_supply_alias(struct device *dev,
1830                                             const char *const *id,
1831                                             int num_id)
1832 {
1833         int i;
1834
1835         for (i = 0; i < num_id; ++i)
1836                 regulator_unregister_supply_alias(dev, id[i]);
1837 }
1838 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1839
1840
1841 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1842 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1843                                 const struct regulator_config *config)
1844 {
1845         struct regulator_enable_gpio *pin;
1846         struct gpio_desc *gpiod;
1847         int ret;
1848
1849         gpiod = gpio_to_desc(config->ena_gpio);
1850
1851         list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1852                 if (pin->gpiod == gpiod) {
1853                         rdev_dbg(rdev, "GPIO %d is already used\n",
1854                                 config->ena_gpio);
1855                         goto update_ena_gpio_to_rdev;
1856                 }
1857         }
1858
1859         ret = gpio_request_one(config->ena_gpio,
1860                                 GPIOF_DIR_OUT | config->ena_gpio_flags,
1861                                 rdev_get_name(rdev));
1862         if (ret)
1863                 return ret;
1864
1865         pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1866         if (pin == NULL) {
1867                 gpio_free(config->ena_gpio);
1868                 return -ENOMEM;
1869         }
1870
1871         pin->gpiod = gpiod;
1872         pin->ena_gpio_invert = config->ena_gpio_invert;
1873         list_add(&pin->list, &regulator_ena_gpio_list);
1874
1875 update_ena_gpio_to_rdev:
1876         pin->request_count++;
1877         rdev->ena_pin = pin;
1878         return 0;
1879 }
1880
1881 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1882 {
1883         struct regulator_enable_gpio *pin, *n;
1884
1885         if (!rdev->ena_pin)
1886                 return;
1887
1888         /* Free the GPIO only in case of no use */
1889         list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1890                 if (pin->gpiod == rdev->ena_pin->gpiod) {
1891                         if (pin->request_count <= 1) {
1892                                 pin->request_count = 0;
1893                                 gpiod_put(pin->gpiod);
1894                                 list_del(&pin->list);
1895                                 kfree(pin);
1896                                 rdev->ena_pin = NULL;
1897                                 return;
1898                         } else {
1899                                 pin->request_count--;
1900                         }
1901                 }
1902         }
1903 }
1904
1905 /**
1906  * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1907  * @rdev: regulator_dev structure
1908  * @enable: enable GPIO at initial use?
1909  *
1910  * GPIO is enabled in case of initial use. (enable_count is 0)
1911  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1912  */
1913 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1914 {
1915         struct regulator_enable_gpio *pin = rdev->ena_pin;
1916
1917         if (!pin)
1918                 return -EINVAL;
1919
1920         if (enable) {
1921                 /* Enable GPIO at initial use */
1922                 if (pin->enable_count == 0)
1923                         gpiod_set_value_cansleep(pin->gpiod,
1924                                                  !pin->ena_gpio_invert);
1925
1926                 pin->enable_count++;
1927         } else {
1928                 if (pin->enable_count > 1) {
1929                         pin->enable_count--;
1930                         return 0;
1931                 }
1932
1933                 /* Disable GPIO if not used */
1934                 if (pin->enable_count <= 1) {
1935                         gpiod_set_value_cansleep(pin->gpiod,
1936                                                  pin->ena_gpio_invert);
1937                         pin->enable_count = 0;
1938                 }
1939         }
1940
1941         return 0;
1942 }
1943
1944 /**
1945  * _regulator_enable_delay - a delay helper function
1946  * @delay: time to delay in microseconds
1947  *
1948  * Delay for the requested amount of time as per the guidelines in:
1949  *
1950  *     Documentation/timers/timers-howto.txt
1951  *
1952  * The assumption here is that regulators will never be enabled in
1953  * atomic context and therefore sleeping functions can be used.
1954  */
1955 static void _regulator_enable_delay(unsigned int delay)
1956 {
1957         unsigned int ms = delay / 1000;
1958         unsigned int us = delay % 1000;
1959
1960         if (ms > 0) {
1961                 /*
1962                  * For small enough values, handle super-millisecond
1963                  * delays in the usleep_range() call below.
1964                  */
1965                 if (ms < 20)
1966                         us += ms * 1000;
1967                 else
1968                         msleep(ms);
1969         }
1970
1971         /*
1972          * Give the scheduler some room to coalesce with any other
1973          * wakeup sources. For delays shorter than 10 us, don't even
1974          * bother setting up high-resolution timers and just busy-
1975          * loop.
1976          */
1977         if (us >= 10)
1978                 usleep_range(us, us + 100);
1979         else
1980                 udelay(us);
1981 }
1982
1983 static int _regulator_do_enable(struct regulator_dev *rdev)
1984 {
1985         int ret, delay;
1986
1987         /* Query before enabling in case configuration dependent.  */
1988         ret = _regulator_get_enable_time(rdev);
1989         if (ret >= 0) {
1990                 delay = ret;
1991         } else {
1992                 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1993                 delay = 0;
1994         }
1995
1996         trace_regulator_enable(rdev_get_name(rdev));
1997
1998         if (rdev->desc->off_on_delay) {
1999                 /* if needed, keep a distance of off_on_delay from last time
2000                  * this regulator was disabled.
2001                  */
2002                 unsigned long start_jiffy = jiffies;
2003                 unsigned long intended, max_delay, remaining;
2004
2005                 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2006                 intended = rdev->last_off_jiffy + max_delay;
2007
2008                 if (time_before(start_jiffy, intended)) {
2009                         /* calc remaining jiffies to deal with one-time
2010                          * timer wrapping.
2011                          * in case of multiple timer wrapping, either it can be
2012                          * detected by out-of-range remaining, or it cannot be
2013                          * detected and we gets a panelty of
2014                          * _regulator_enable_delay().
2015                          */
2016                         remaining = intended - start_jiffy;
2017                         if (remaining <= max_delay)
2018                                 _regulator_enable_delay(
2019                                                 jiffies_to_usecs(remaining));
2020                 }
2021         }
2022
2023         if (rdev->ena_pin) {
2024                 if (!rdev->ena_gpio_state) {
2025                         ret = regulator_ena_gpio_ctrl(rdev, true);
2026                         if (ret < 0)
2027                                 return ret;
2028                         rdev->ena_gpio_state = 1;
2029                 }
2030         } else if (rdev->desc->ops->enable) {
2031                 ret = rdev->desc->ops->enable(rdev);
2032                 if (ret < 0)
2033                         return ret;
2034         } else {
2035                 return -EINVAL;
2036         }
2037
2038         /* Allow the regulator to ramp; it would be useful to extend
2039          * this for bulk operations so that the regulators can ramp
2040          * together.  */
2041         trace_regulator_enable_delay(rdev_get_name(rdev));
2042
2043         _regulator_enable_delay(delay);
2044
2045         trace_regulator_enable_complete(rdev_get_name(rdev));
2046
2047         return 0;
2048 }
2049
2050 /* locks held by regulator_enable() */
2051 static int _regulator_enable(struct regulator_dev *rdev)
2052 {
2053         int ret;
2054
2055         lockdep_assert_held_once(&rdev->mutex);
2056
2057         /* check voltage and requested load before enabling */
2058         if (rdev->constraints &&
2059             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
2060                 drms_uA_update(rdev);
2061
2062         if (rdev->use_count == 0) {
2063                 /* The regulator may on if it's not switchable or left on */
2064                 ret = _regulator_is_enabled(rdev);
2065                 if (ret == -EINVAL || ret == 0) {
2066                         if (!_regulator_can_change_status(rdev))
2067                                 return -EPERM;
2068
2069                         ret = _regulator_do_enable(rdev);
2070                         if (ret < 0)
2071                                 return ret;
2072
2073                 } else if (ret < 0) {
2074                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2075                         return ret;
2076                 }
2077                 /* Fallthrough on positive return values - already enabled */
2078         }
2079
2080         rdev->use_count++;
2081
2082         return 0;
2083 }
2084
2085 /**
2086  * regulator_enable - enable regulator output
2087  * @regulator: regulator source
2088  *
2089  * Request that the regulator be enabled with the regulator output at
2090  * the predefined voltage or current value.  Calls to regulator_enable()
2091  * must be balanced with calls to regulator_disable().
2092  *
2093  * NOTE: the output value can be set by other drivers, boot loader or may be
2094  * hardwired in the regulator.
2095  */
2096 int regulator_enable(struct regulator *regulator)
2097 {
2098         struct regulator_dev *rdev = regulator->rdev;
2099         int ret = 0;
2100
2101         if (regulator->always_on)
2102                 return 0;
2103
2104         if (rdev->supply) {
2105                 ret = regulator_enable(rdev->supply);
2106                 if (ret != 0)
2107                         return ret;
2108         }
2109
2110         mutex_lock(&rdev->mutex);
2111         ret = _regulator_enable(rdev);
2112         mutex_unlock(&rdev->mutex);
2113
2114         if (ret != 0 && rdev->supply)
2115                 regulator_disable(rdev->supply);
2116
2117         return ret;
2118 }
2119 EXPORT_SYMBOL_GPL(regulator_enable);
2120
2121 static int _regulator_do_disable(struct regulator_dev *rdev)
2122 {
2123         int ret;
2124
2125         trace_regulator_disable(rdev_get_name(rdev));
2126
2127         if (rdev->ena_pin) {
2128                 if (rdev->ena_gpio_state) {
2129                         ret = regulator_ena_gpio_ctrl(rdev, false);
2130                         if (ret < 0)
2131                                 return ret;
2132                         rdev->ena_gpio_state = 0;
2133                 }
2134
2135         } else if (rdev->desc->ops->disable) {
2136                 ret = rdev->desc->ops->disable(rdev);
2137                 if (ret != 0)
2138                         return ret;
2139         }
2140
2141         /* cares about last_off_jiffy only if off_on_delay is required by
2142          * device.
2143          */
2144         if (rdev->desc->off_on_delay)
2145                 rdev->last_off_jiffy = jiffies;
2146
2147         trace_regulator_disable_complete(rdev_get_name(rdev));
2148
2149         return 0;
2150 }
2151
2152 /* locks held by regulator_disable() */
2153 static int _regulator_disable(struct regulator_dev *rdev)
2154 {
2155         int ret = 0;
2156
2157         lockdep_assert_held_once(&rdev->mutex);
2158
2159         if (WARN(rdev->use_count <= 0,
2160                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
2161                 return -EIO;
2162
2163         /* are we the last user and permitted to disable ? */
2164         if (rdev->use_count == 1 &&
2165             (rdev->constraints && !rdev->constraints->always_on)) {
2166
2167                 /* we are last user */
2168                 if (_regulator_can_change_status(rdev)) {
2169                         ret = _notifier_call_chain(rdev,
2170                                                    REGULATOR_EVENT_PRE_DISABLE,
2171                                                    NULL);
2172                         if (ret & NOTIFY_STOP_MASK)
2173                                 return -EINVAL;
2174
2175                         ret = _regulator_do_disable(rdev);
2176                         if (ret < 0) {
2177                                 rdev_err(rdev, "failed to disable\n");
2178                                 _notifier_call_chain(rdev,
2179                                                 REGULATOR_EVENT_ABORT_DISABLE,
2180                                                 NULL);
2181                                 return ret;
2182                         }
2183                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2184                                         NULL);
2185                 }
2186
2187                 rdev->use_count = 0;
2188         } else if (rdev->use_count > 1) {
2189
2190                 if (rdev->constraints &&
2191                         (rdev->constraints->valid_ops_mask &
2192                         REGULATOR_CHANGE_DRMS))
2193                         drms_uA_update(rdev);
2194
2195                 rdev->use_count--;
2196         }
2197
2198         return ret;
2199 }
2200
2201 /**
2202  * regulator_disable - disable regulator output
2203  * @regulator: regulator source
2204  *
2205  * Disable the regulator output voltage or current.  Calls to
2206  * regulator_enable() must be balanced with calls to
2207  * regulator_disable().
2208  *
2209  * NOTE: this will only disable the regulator output if no other consumer
2210  * devices have it enabled, the regulator device supports disabling and
2211  * machine constraints permit this operation.
2212  */
2213 int regulator_disable(struct regulator *regulator)
2214 {
2215         struct regulator_dev *rdev = regulator->rdev;
2216         int ret = 0;
2217
2218         if (regulator->always_on)
2219                 return 0;
2220
2221         mutex_lock(&rdev->mutex);
2222         ret = _regulator_disable(rdev);
2223         mutex_unlock(&rdev->mutex);
2224
2225         if (ret == 0 && rdev->supply)
2226                 regulator_disable(rdev->supply);
2227
2228         return ret;
2229 }
2230 EXPORT_SYMBOL_GPL(regulator_disable);
2231
2232 /* locks held by regulator_force_disable() */
2233 static int _regulator_force_disable(struct regulator_dev *rdev)
2234 {
2235         int ret = 0;
2236
2237         lockdep_assert_held_once(&rdev->mutex);
2238
2239         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2240                         REGULATOR_EVENT_PRE_DISABLE, NULL);
2241         if (ret & NOTIFY_STOP_MASK)
2242                 return -EINVAL;
2243
2244         ret = _regulator_do_disable(rdev);
2245         if (ret < 0) {
2246                 rdev_err(rdev, "failed to force disable\n");
2247                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2248                                 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2249                 return ret;
2250         }
2251
2252         _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2253                         REGULATOR_EVENT_DISABLE, NULL);
2254
2255         return 0;
2256 }
2257
2258 /**
2259  * regulator_force_disable - force disable regulator output
2260  * @regulator: regulator source
2261  *
2262  * Forcibly disable the regulator output voltage or current.
2263  * NOTE: this *will* disable the regulator output even if other consumer
2264  * devices have it enabled. This should be used for situations when device
2265  * damage will likely occur if the regulator is not disabled (e.g. over temp).
2266  */
2267 int regulator_force_disable(struct regulator *regulator)
2268 {
2269         struct regulator_dev *rdev = regulator->rdev;
2270         int ret;
2271
2272         mutex_lock(&rdev->mutex);
2273         regulator->uA_load = 0;
2274         ret = _regulator_force_disable(regulator->rdev);
2275         mutex_unlock(&rdev->mutex);
2276
2277         if (rdev->supply)
2278                 while (rdev->open_count--)
2279                         regulator_disable(rdev->supply);
2280
2281         return ret;
2282 }
2283 EXPORT_SYMBOL_GPL(regulator_force_disable);
2284
2285 static void regulator_disable_work(struct work_struct *work)
2286 {
2287         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2288                                                   disable_work.work);
2289         int count, i, ret;
2290
2291         mutex_lock(&rdev->mutex);
2292
2293         BUG_ON(!rdev->deferred_disables);
2294
2295         count = rdev->deferred_disables;
2296         rdev->deferred_disables = 0;
2297
2298         for (i = 0; i < count; i++) {
2299                 ret = _regulator_disable(rdev);
2300                 if (ret != 0)
2301                         rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2302         }
2303
2304         mutex_unlock(&rdev->mutex);
2305
2306         if (rdev->supply) {
2307                 for (i = 0; i < count; i++) {
2308                         ret = regulator_disable(rdev->supply);
2309                         if (ret != 0) {
2310                                 rdev_err(rdev,
2311                                          "Supply disable failed: %d\n", ret);
2312                         }
2313                 }
2314         }
2315 }
2316
2317 /**
2318  * regulator_disable_deferred - disable regulator output with delay
2319  * @regulator: regulator source
2320  * @ms: miliseconds until the regulator is disabled
2321  *
2322  * Execute regulator_disable() on the regulator after a delay.  This
2323  * is intended for use with devices that require some time to quiesce.
2324  *
2325  * NOTE: this will only disable the regulator output if no other consumer
2326  * devices have it enabled, the regulator device supports disabling and
2327  * machine constraints permit this operation.
2328  */
2329 int regulator_disable_deferred(struct regulator *regulator, int ms)
2330 {
2331         struct regulator_dev *rdev = regulator->rdev;
2332         int ret;
2333
2334         if (regulator->always_on)
2335                 return 0;
2336
2337         if (!ms)
2338                 return regulator_disable(regulator);
2339
2340         mutex_lock(&rdev->mutex);
2341         rdev->deferred_disables++;
2342         mutex_unlock(&rdev->mutex);
2343
2344         ret = queue_delayed_work(system_power_efficient_wq,
2345                                  &rdev->disable_work,
2346                                  msecs_to_jiffies(ms));
2347         if (ret < 0)
2348                 return ret;
2349         else
2350                 return 0;
2351 }
2352 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2353
2354 static int _regulator_is_enabled(struct regulator_dev *rdev)
2355 {
2356         /* A GPIO control always takes precedence */
2357         if (rdev->ena_pin)
2358                 return rdev->ena_gpio_state;
2359
2360         /* If we don't know then assume that the regulator is always on */
2361         if (!rdev->desc->ops->is_enabled)
2362                 return 1;
2363
2364         return rdev->desc->ops->is_enabled(rdev);
2365 }
2366
2367 /**
2368  * regulator_is_enabled - is the regulator output enabled
2369  * @regulator: regulator source
2370  *
2371  * Returns positive if the regulator driver backing the source/client
2372  * has requested that the device be enabled, zero if it hasn't, else a
2373  * negative errno code.
2374  *
2375  * Note that the device backing this regulator handle can have multiple
2376  * users, so it might be enabled even if regulator_enable() was never
2377  * called for this particular source.
2378  */
2379 int regulator_is_enabled(struct regulator *regulator)
2380 {
2381         int ret;
2382
2383         if (regulator->always_on)
2384                 return 1;
2385
2386         mutex_lock(&regulator->rdev->mutex);
2387         ret = _regulator_is_enabled(regulator->rdev);
2388         mutex_unlock(&regulator->rdev->mutex);
2389
2390         return ret;
2391 }
2392 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2393
2394 /**
2395  * regulator_can_change_voltage - check if regulator can change voltage
2396  * @regulator: regulator source
2397  *
2398  * Returns positive if the regulator driver backing the source/client
2399  * can change its voltage, false otherwise. Useful for detecting fixed
2400  * or dummy regulators and disabling voltage change logic in the client
2401  * driver.
2402  */
2403 int regulator_can_change_voltage(struct regulator *regulator)
2404 {
2405         struct regulator_dev    *rdev = regulator->rdev;
2406
2407         if (rdev->constraints &&
2408             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2409                 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2410                         return 1;
2411
2412                 if (rdev->desc->continuous_voltage_range &&
2413                     rdev->constraints->min_uV && rdev->constraints->max_uV &&
2414                     rdev->constraints->min_uV != rdev->constraints->max_uV)
2415                         return 1;
2416         }
2417
2418         return 0;
2419 }
2420 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2421
2422 /**
2423  * regulator_count_voltages - count regulator_list_voltage() selectors
2424  * @regulator: regulator source
2425  *
2426  * Returns number of selectors, or negative errno.  Selectors are
2427  * numbered starting at zero, and typically correspond to bitfields
2428  * in hardware registers.
2429  */
2430 int regulator_count_voltages(struct regulator *regulator)
2431 {
2432         struct regulator_dev    *rdev = regulator->rdev;
2433
2434         if (rdev->desc->n_voltages)
2435                 return rdev->desc->n_voltages;
2436
2437         if (!rdev->supply)
2438                 return -EINVAL;
2439
2440         return regulator_count_voltages(rdev->supply);
2441 }
2442 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2443
2444 /**
2445  * regulator_list_voltage - enumerate supported voltages
2446  * @regulator: regulator source
2447  * @selector: identify voltage to list
2448  * Context: can sleep
2449  *
2450  * Returns a voltage that can be passed to @regulator_set_voltage(),
2451  * zero if this selector code can't be used on this system, or a
2452  * negative errno.
2453  */
2454 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2455 {
2456         struct regulator_dev *rdev = regulator->rdev;
2457         const struct regulator_ops *ops = rdev->desc->ops;
2458         int ret;
2459
2460         if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2461                 return rdev->desc->fixed_uV;
2462
2463         if (ops->list_voltage) {
2464                 if (selector >= rdev->desc->n_voltages)
2465                         return -EINVAL;
2466                 mutex_lock(&rdev->mutex);
2467                 ret = ops->list_voltage(rdev, selector);
2468                 mutex_unlock(&rdev->mutex);
2469         } else if (rdev->supply) {
2470                 ret = regulator_list_voltage(rdev->supply, selector);
2471         } else {
2472                 return -EINVAL;
2473         }
2474
2475         if (ret > 0) {
2476                 if (ret < rdev->constraints->min_uV)
2477                         ret = 0;
2478                 else if (ret > rdev->constraints->max_uV)
2479                         ret = 0;
2480         }
2481
2482         return ret;
2483 }
2484 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2485
2486 /**
2487  * regulator_get_regmap - get the regulator's register map
2488  * @regulator: regulator source
2489  *
2490  * Returns the register map for the given regulator, or an ERR_PTR value
2491  * if the regulator doesn't use regmap.
2492  */
2493 struct regmap *regulator_get_regmap(struct regulator *regulator)
2494 {
2495         struct regmap *map = regulator->rdev->regmap;
2496
2497         return map ? map : ERR_PTR(-EOPNOTSUPP);
2498 }
2499
2500 /**
2501  * regulator_get_hardware_vsel_register - get the HW voltage selector register
2502  * @regulator: regulator source
2503  * @vsel_reg: voltage selector register, output parameter
2504  * @vsel_mask: mask for voltage selector bitfield, output parameter
2505  *
2506  * Returns the hardware register offset and bitmask used for setting the
2507  * regulator voltage. This might be useful when configuring voltage-scaling
2508  * hardware or firmware that can make I2C requests behind the kernel's back,
2509  * for example.
2510  *
2511  * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2512  * and 0 is returned, otherwise a negative errno is returned.
2513  */
2514 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2515                                          unsigned *vsel_reg,
2516                                          unsigned *vsel_mask)
2517 {
2518         struct regulator_dev *rdev = regulator->rdev;
2519         const struct regulator_ops *ops = rdev->desc->ops;
2520
2521         if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2522                 return -EOPNOTSUPP;
2523
2524          *vsel_reg = rdev->desc->vsel_reg;
2525          *vsel_mask = rdev->desc->vsel_mask;
2526
2527          return 0;
2528 }
2529 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2530
2531 /**
2532  * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2533  * @regulator: regulator source
2534  * @selector: identify voltage to list
2535  *
2536  * Converts the selector to a hardware-specific voltage selector that can be
2537  * directly written to the regulator registers. The address of the voltage
2538  * register can be determined by calling @regulator_get_hardware_vsel_register.
2539  *
2540  * On error a negative errno is returned.
2541  */
2542 int regulator_list_hardware_vsel(struct regulator *regulator,
2543                                  unsigned selector)
2544 {
2545         struct regulator_dev *rdev = regulator->rdev;
2546         const struct regulator_ops *ops = rdev->desc->ops;
2547
2548         if (selector >= rdev->desc->n_voltages)
2549                 return -EINVAL;
2550         if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2551                 return -EOPNOTSUPP;
2552
2553         return selector;
2554 }
2555 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2556
2557 /**
2558  * regulator_get_linear_step - return the voltage step size between VSEL values
2559  * @regulator: regulator source
2560  *
2561  * Returns the voltage step size between VSEL values for linear
2562  * regulators, or return 0 if the regulator isn't a linear regulator.
2563  */
2564 unsigned int regulator_get_linear_step(struct regulator *regulator)
2565 {
2566         struct regulator_dev *rdev = regulator->rdev;
2567
2568         return rdev->desc->uV_step;
2569 }
2570 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2571
2572 /**
2573  * regulator_is_supported_voltage - check if a voltage range can be supported
2574  *
2575  * @regulator: Regulator to check.
2576  * @min_uV: Minimum required voltage in uV.
2577  * @max_uV: Maximum required voltage in uV.
2578  *
2579  * Returns a boolean or a negative error code.
2580  */
2581 int regulator_is_supported_voltage(struct regulator *regulator,
2582                                    int min_uV, int max_uV)
2583 {
2584         struct regulator_dev *rdev = regulator->rdev;
2585         int i, voltages, ret;
2586
2587         /* If we can't change voltage check the current voltage */
2588         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2589                 ret = regulator_get_voltage(regulator);
2590                 if (ret >= 0)
2591                         return min_uV <= ret && ret <= max_uV;
2592                 else
2593                         return ret;
2594         }
2595
2596         /* Any voltage within constrains range is fine? */
2597         if (rdev->desc->continuous_voltage_range)
2598                 return min_uV >= rdev->constraints->min_uV &&
2599                                 max_uV <= rdev->constraints->max_uV;
2600
2601         ret = regulator_count_voltages(regulator);
2602         if (ret < 0)
2603                 return ret;
2604         voltages = ret;
2605
2606         for (i = 0; i < voltages; i++) {
2607                 ret = regulator_list_voltage(regulator, i);
2608
2609                 if (ret >= min_uV && ret <= max_uV)
2610                         return 1;
2611         }
2612
2613         return 0;
2614 }
2615 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2616
2617 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2618                                        int min_uV, int max_uV,
2619                                        unsigned *selector)
2620 {
2621         struct pre_voltage_change_data data;
2622         int ret;
2623
2624         data.old_uV = _regulator_get_voltage(rdev);
2625         data.min_uV = min_uV;
2626         data.max_uV = max_uV;
2627         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2628                                    &data);
2629         if (ret & NOTIFY_STOP_MASK)
2630                 return -EINVAL;
2631
2632         ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2633         if (ret >= 0)
2634                 return ret;
2635
2636         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2637                              (void *)data.old_uV);
2638
2639         return ret;
2640 }
2641
2642 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2643                                            int uV, unsigned selector)
2644 {
2645         struct pre_voltage_change_data data;
2646         int ret;
2647
2648         data.old_uV = _regulator_get_voltage(rdev);
2649         data.min_uV = uV;
2650         data.max_uV = uV;
2651         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2652                                    &data);
2653         if (ret & NOTIFY_STOP_MASK)
2654                 return -EINVAL;
2655
2656         ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2657         if (ret >= 0)
2658                 return ret;
2659
2660         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2661                              (void *)data.old_uV);
2662
2663         return ret;
2664 }
2665
2666 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2667                                      int min_uV, int max_uV)
2668 {
2669         int ret;
2670         int delay = 0;
2671         int best_val = 0;
2672         unsigned int selector;
2673         int old_selector = -1;
2674
2675         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2676
2677         min_uV += rdev->constraints->uV_offset;
2678         max_uV += rdev->constraints->uV_offset;
2679
2680         /*
2681          * If we can't obtain the old selector there is not enough
2682          * info to call set_voltage_time_sel().
2683          */
2684         if (_regulator_is_enabled(rdev) &&
2685             rdev->desc->ops->set_voltage_time_sel &&
2686             rdev->desc->ops->get_voltage_sel) {
2687                 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2688                 if (old_selector < 0)
2689                         return old_selector;
2690         }
2691
2692         if (rdev->desc->ops->set_voltage) {
2693                 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2694                                                   &selector);
2695
2696                 if (ret >= 0) {
2697                         if (rdev->desc->ops->list_voltage)
2698                                 best_val = rdev->desc->ops->list_voltage(rdev,
2699                                                                          selector);
2700                         else
2701                                 best_val = _regulator_get_voltage(rdev);
2702                 }
2703
2704         } else if (rdev->desc->ops->set_voltage_sel) {
2705                 if (rdev->desc->ops->map_voltage) {
2706                         ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2707                                                            max_uV);
2708                 } else {
2709                         if (rdev->desc->ops->list_voltage ==
2710                             regulator_list_voltage_linear)
2711                                 ret = regulator_map_voltage_linear(rdev,
2712                                                                 min_uV, max_uV);
2713                         else if (rdev->desc->ops->list_voltage ==
2714                                  regulator_list_voltage_linear_range)
2715                                 ret = regulator_map_voltage_linear_range(rdev,
2716                                                                 min_uV, max_uV);
2717                         else
2718                                 ret = regulator_map_voltage_iterate(rdev,
2719                                                                 min_uV, max_uV);
2720                 }
2721
2722                 if (ret >= 0) {
2723                         best_val = rdev->desc->ops->list_voltage(rdev, ret);
2724                         if (min_uV <= best_val && max_uV >= best_val) {
2725                                 selector = ret;
2726                                 if (old_selector == selector)
2727                                         ret = 0;
2728                                 else
2729                                         ret = _regulator_call_set_voltage_sel(
2730                                                 rdev, best_val, selector);
2731                         } else {
2732                                 ret = -EINVAL;
2733                         }
2734                 }
2735         } else {
2736                 ret = -EINVAL;
2737         }
2738
2739         /* Call set_voltage_time_sel if successfully obtained old_selector */
2740         if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2741                 && old_selector != selector) {
2742
2743                 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2744                                                 old_selector, selector);
2745                 if (delay < 0) {
2746                         rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2747                                   delay);
2748                         delay = 0;
2749                 }
2750
2751                 /* Insert any necessary delays */
2752                 if (delay >= 1000) {
2753                         mdelay(delay / 1000);
2754                         udelay(delay % 1000);
2755                 } else if (delay) {
2756                         udelay(delay);
2757                 }
2758         }
2759
2760         if (ret == 0 && best_val >= 0) {
2761                 unsigned long data = best_val;
2762
2763                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2764                                      (void *)data);
2765         }
2766
2767         trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2768
2769         return ret;
2770 }
2771
2772 /**
2773  * regulator_set_voltage - set regulator output voltage
2774  * @regulator: regulator source
2775  * @min_uV: Minimum required voltage in uV
2776  * @max_uV: Maximum acceptable voltage in uV
2777  *
2778  * Sets a voltage regulator to the desired output voltage. This can be set
2779  * during any regulator state. IOW, regulator can be disabled or enabled.
2780  *
2781  * If the regulator is enabled then the voltage will change to the new value
2782  * immediately otherwise if the regulator is disabled the regulator will
2783  * output at the new voltage when enabled.
2784  *
2785  * NOTE: If the regulator is shared between several devices then the lowest
2786  * request voltage that meets the system constraints will be used.
2787  * Regulator system constraints must be set for this regulator before
2788  * calling this function otherwise this call will fail.
2789  */
2790 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2791 {
2792         struct regulator_dev *rdev = regulator->rdev;
2793         int ret = 0;
2794         int old_min_uV, old_max_uV;
2795         int current_uV;
2796
2797         mutex_lock(&rdev->mutex);
2798
2799         /* If we're setting the same range as last time the change
2800          * should be a noop (some cpufreq implementations use the same
2801          * voltage for multiple frequencies, for example).
2802          */
2803         if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2804                 goto out;
2805
2806         /* If we're trying to set a range that overlaps the current voltage,
2807          * return successfully even though the regulator does not support
2808          * changing the voltage.
2809          */
2810         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2811                 current_uV = _regulator_get_voltage(rdev);
2812                 if (min_uV <= current_uV && current_uV <= max_uV) {
2813                         regulator->min_uV = min_uV;
2814                         regulator->max_uV = max_uV;
2815                         goto out;
2816                 }
2817         }
2818
2819         /* sanity check */
2820         if (!rdev->desc->ops->set_voltage &&
2821             !rdev->desc->ops->set_voltage_sel) {
2822                 ret = -EINVAL;
2823                 goto out;
2824         }
2825
2826         /* constraints check */
2827         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2828         if (ret < 0)
2829                 goto out;
2830
2831         /* restore original values in case of error */
2832         old_min_uV = regulator->min_uV;
2833         old_max_uV = regulator->max_uV;
2834         regulator->min_uV = min_uV;
2835         regulator->max_uV = max_uV;
2836
2837         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2838         if (ret < 0)
2839                 goto out2;
2840
2841         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2842         if (ret < 0)
2843                 goto out2;
2844
2845 out:
2846         mutex_unlock(&rdev->mutex);
2847         return ret;
2848 out2:
2849         regulator->min_uV = old_min_uV;
2850         regulator->max_uV = old_max_uV;
2851         mutex_unlock(&rdev->mutex);
2852         return ret;
2853 }
2854 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2855
2856 /**
2857  * regulator_set_voltage_time - get raise/fall time
2858  * @regulator: regulator source
2859  * @old_uV: starting voltage in microvolts
2860  * @new_uV: target voltage in microvolts
2861  *
2862  * Provided with the starting and ending voltage, this function attempts to
2863  * calculate the time in microseconds required to rise or fall to this new
2864  * voltage.
2865  */
2866 int regulator_set_voltage_time(struct regulator *regulator,
2867                                int old_uV, int new_uV)
2868 {
2869         struct regulator_dev *rdev = regulator->rdev;
2870         const struct regulator_ops *ops = rdev->desc->ops;
2871         int old_sel = -1;
2872         int new_sel = -1;
2873         int voltage;
2874         int i;
2875
2876         /* Currently requires operations to do this */
2877         if (!ops->list_voltage || !ops->set_voltage_time_sel
2878             || !rdev->desc->n_voltages)
2879                 return -EINVAL;
2880
2881         for (i = 0; i < rdev->desc->n_voltages; i++) {
2882                 /* We only look for exact voltage matches here */
2883                 voltage = regulator_list_voltage(regulator, i);
2884                 if (voltage < 0)
2885                         return -EINVAL;
2886                 if (voltage == 0)
2887                         continue;
2888                 if (voltage == old_uV)
2889                         old_sel = i;
2890                 if (voltage == new_uV)
2891                         new_sel = i;
2892         }
2893
2894         if (old_sel < 0 || new_sel < 0)
2895                 return -EINVAL;
2896
2897         return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2898 }
2899 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2900
2901 /**
2902  * regulator_set_voltage_time_sel - get raise/fall time
2903  * @rdev: regulator source device
2904  * @old_selector: selector for starting voltage
2905  * @new_selector: selector for target voltage
2906  *
2907  * Provided with the starting and target voltage selectors, this function
2908  * returns time in microseconds required to rise or fall to this new voltage
2909  *
2910  * Drivers providing ramp_delay in regulation_constraints can use this as their
2911  * set_voltage_time_sel() operation.
2912  */
2913 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2914                                    unsigned int old_selector,
2915                                    unsigned int new_selector)
2916 {
2917         unsigned int ramp_delay = 0;
2918         int old_volt, new_volt;
2919
2920         if (rdev->constraints->ramp_delay)
2921                 ramp_delay = rdev->constraints->ramp_delay;
2922         else if (rdev->desc->ramp_delay)
2923                 ramp_delay = rdev->desc->ramp_delay;
2924
2925         if (ramp_delay == 0) {
2926                 rdev_warn(rdev, "ramp_delay not set\n");
2927                 return 0;
2928         }
2929
2930         /* sanity check */
2931         if (!rdev->desc->ops->list_voltage)
2932                 return -EINVAL;
2933
2934         old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2935         new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2936
2937         return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2938 }
2939 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2940
2941 /**
2942  * regulator_sync_voltage - re-apply last regulator output voltage
2943  * @regulator: regulator source
2944  *
2945  * Re-apply the last configured voltage.  This is intended to be used
2946  * where some external control source the consumer is cooperating with
2947  * has caused the configured voltage to change.
2948  */
2949 int regulator_sync_voltage(struct regulator *regulator)
2950 {
2951         struct regulator_dev *rdev = regulator->rdev;
2952         int ret, min_uV, max_uV;
2953
2954         mutex_lock(&rdev->mutex);
2955
2956         if (!rdev->desc->ops->set_voltage &&
2957             !rdev->desc->ops->set_voltage_sel) {
2958                 ret = -EINVAL;
2959                 goto out;
2960         }
2961
2962         /* This is only going to work if we've had a voltage configured. */
2963         if (!regulator->min_uV && !regulator->max_uV) {
2964                 ret = -EINVAL;
2965                 goto out;
2966         }
2967
2968         min_uV = regulator->min_uV;
2969         max_uV = regulator->max_uV;
2970
2971         /* This should be a paranoia check... */
2972         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2973         if (ret < 0)
2974                 goto out;
2975
2976         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2977         if (ret < 0)
2978                 goto out;
2979
2980         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2981
2982 out:
2983         mutex_unlock(&rdev->mutex);
2984         return ret;
2985 }
2986 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2987
2988 static int _regulator_get_voltage(struct regulator_dev *rdev)
2989 {
2990         int sel, ret;
2991
2992         if (rdev->desc->ops->get_voltage_sel) {
2993                 sel = rdev->desc->ops->get_voltage_sel(rdev);
2994                 if (sel < 0)
2995                         return sel;
2996                 ret = rdev->desc->ops->list_voltage(rdev, sel);
2997         } else if (rdev->desc->ops->get_voltage) {
2998                 ret = rdev->desc->ops->get_voltage(rdev);
2999         } else if (rdev->desc->ops->list_voltage) {
3000                 ret = rdev->desc->ops->list_voltage(rdev, 0);
3001         } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3002                 ret = rdev->desc->fixed_uV;
3003         } else if (rdev->supply) {
3004                 ret = regulator_get_voltage(rdev->supply);
3005         } else {
3006                 return -EINVAL;
3007         }
3008
3009         if (ret < 0)
3010                 return ret;
3011         return ret - rdev->constraints->uV_offset;
3012 }
3013
3014 /**
3015  * regulator_get_voltage - get regulator output voltage
3016  * @regulator: regulator source
3017  *
3018  * This returns the current regulator voltage in uV.
3019  *
3020  * NOTE: If the regulator is disabled it will return the voltage value. This
3021  * function should not be used to determine regulator state.
3022  */
3023 int regulator_get_voltage(struct regulator *regulator)
3024 {
3025         int ret;
3026
3027         mutex_lock(&regulator->rdev->mutex);
3028
3029         ret = _regulator_get_voltage(regulator->rdev);
3030
3031         mutex_unlock(&regulator->rdev->mutex);
3032
3033         return ret;
3034 }
3035 EXPORT_SYMBOL_GPL(regulator_get_voltage);
3036
3037 /**
3038  * regulator_set_current_limit - set regulator output current limit
3039  * @regulator: regulator source
3040  * @min_uA: Minimum supported current in uA
3041  * @max_uA: Maximum supported current in uA
3042  *
3043  * Sets current sink to the desired output current. This can be set during
3044  * any regulator state. IOW, regulator can be disabled or enabled.
3045  *
3046  * If the regulator is enabled then the current will change to the new value
3047  * immediately otherwise if the regulator is disabled the regulator will
3048  * output at the new current when enabled.
3049  *
3050  * NOTE: Regulator system constraints must be set for this regulator before
3051  * calling this function otherwise this call will fail.
3052  */
3053 int regulator_set_current_limit(struct regulator *regulator,
3054                                int min_uA, int max_uA)
3055 {
3056         struct regulator_dev *rdev = regulator->rdev;
3057         int ret;
3058
3059         mutex_lock(&rdev->mutex);
3060
3061         /* sanity check */
3062         if (!rdev->desc->ops->set_current_limit) {
3063                 ret = -EINVAL;
3064                 goto out;
3065         }
3066
3067         /* constraints check */
3068         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3069         if (ret < 0)
3070                 goto out;
3071
3072         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3073 out:
3074         mutex_unlock(&rdev->mutex);
3075         return ret;
3076 }
3077 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3078
3079 static int _regulator_get_current_limit(struct regulator_dev *rdev)
3080 {
3081         int ret;
3082
3083         mutex_lock(&rdev->mutex);
3084
3085         /* sanity check */
3086         if (!rdev->desc->ops->get_current_limit) {
3087                 ret = -EINVAL;
3088                 goto out;
3089         }
3090
3091         ret = rdev->desc->ops->get_current_limit(rdev);
3092 out:
3093         mutex_unlock(&rdev->mutex);
3094         return ret;
3095 }
3096
3097 /**
3098  * regulator_get_current_limit - get regulator output current
3099  * @regulator: regulator source
3100  *
3101  * This returns the current supplied by the specified current sink in uA.
3102  *
3103  * NOTE: If the regulator is disabled it will return the current value. This
3104  * function should not be used to determine regulator state.
3105  */
3106 int regulator_get_current_limit(struct regulator *regulator)
3107 {
3108         return _regulator_get_current_limit(regulator->rdev);
3109 }
3110 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3111
3112 /**
3113  * regulator_set_mode - set regulator operating mode
3114  * @regulator: regulator source
3115  * @mode: operating mode - one of the REGULATOR_MODE constants
3116  *
3117  * Set regulator operating mode to increase regulator efficiency or improve
3118  * regulation performance.
3119  *
3120  * NOTE: Regulator system constraints must be set for this regulator before
3121  * calling this function otherwise this call will fail.
3122  */
3123 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3124 {
3125         struct regulator_dev *rdev = regulator->rdev;
3126         int ret;
3127         int regulator_curr_mode;
3128
3129         mutex_lock(&rdev->mutex);
3130
3131         /* sanity check */
3132         if (!rdev->desc->ops->set_mode) {
3133                 ret = -EINVAL;
3134                 goto out;
3135         }
3136
3137         /* return if the same mode is requested */
3138         if (rdev->desc->ops->get_mode) {
3139                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3140                 if (regulator_curr_mode == mode) {
3141                         ret = 0;
3142                         goto out;
3143                 }
3144         }
3145
3146         /* constraints check */
3147         ret = regulator_mode_constrain(rdev, &mode);
3148         if (ret < 0)
3149                 goto out;
3150
3151         ret = rdev->desc->ops->set_mode(rdev, mode);
3152 out:
3153         mutex_unlock(&rdev->mutex);
3154         return ret;
3155 }
3156 EXPORT_SYMBOL_GPL(regulator_set_mode);
3157
3158 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3159 {
3160         int ret;
3161
3162         mutex_lock(&rdev->mutex);
3163
3164         /* sanity check */
3165         if (!rdev->desc->ops->get_mode) {
3166                 ret = -EINVAL;
3167                 goto out;
3168         }
3169
3170         ret = rdev->desc->ops->get_mode(rdev);
3171 out:
3172         mutex_unlock(&rdev->mutex);
3173         return ret;
3174 }
3175
3176 /**
3177  * regulator_get_mode - get regulator operating mode
3178  * @regulator: regulator source
3179  *
3180  * Get the current regulator operating mode.
3181  */
3182 unsigned int regulator_get_mode(struct regulator *regulator)
3183 {
3184         return _regulator_get_mode(regulator->rdev);
3185 }
3186 EXPORT_SYMBOL_GPL(regulator_get_mode);
3187
3188 /**
3189  * regulator_set_load - set regulator load
3190  * @regulator: regulator source
3191  * @uA_load: load current
3192  *
3193  * Notifies the regulator core of a new device load. This is then used by
3194  * DRMS (if enabled by constraints) to set the most efficient regulator
3195  * operating mode for the new regulator loading.
3196  *
3197  * Consumer devices notify their supply regulator of the maximum power
3198  * they will require (can be taken from device datasheet in the power
3199  * consumption tables) when they change operational status and hence power
3200  * state. Examples of operational state changes that can affect power
3201  * consumption are :-
3202  *
3203  *    o Device is opened / closed.
3204  *    o Device I/O is about to begin or has just finished.
3205  *    o Device is idling in between work.
3206  *
3207  * This information is also exported via sysfs to userspace.
3208  *
3209  * DRMS will sum the total requested load on the regulator and change
3210  * to the most efficient operating mode if platform constraints allow.
3211  *
3212  * On error a negative errno is returned.
3213  */
3214 int regulator_set_load(struct regulator *regulator, int uA_load)
3215 {
3216         struct regulator_dev *rdev = regulator->rdev;
3217         int ret;
3218
3219         mutex_lock(&rdev->mutex);
3220         regulator->uA_load = uA_load;
3221         ret = drms_uA_update(rdev);
3222         mutex_unlock(&rdev->mutex);
3223
3224         return ret;
3225 }
3226 EXPORT_SYMBOL_GPL(regulator_set_load);
3227
3228 /**
3229  * regulator_allow_bypass - allow the regulator to go into bypass mode
3230  *
3231  * @regulator: Regulator to configure
3232  * @enable: enable or disable bypass mode
3233  *
3234  * Allow the regulator to go into bypass mode if all other consumers
3235  * for the regulator also enable bypass mode and the machine
3236  * constraints allow this.  Bypass mode means that the regulator is
3237  * simply passing the input directly to the output with no regulation.
3238  */
3239 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3240 {
3241         struct regulator_dev *rdev = regulator->rdev;
3242         int ret = 0;
3243
3244         if (!rdev->desc->ops->set_bypass)
3245                 return 0;
3246
3247         if (rdev->constraints &&
3248             !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3249                 return 0;
3250
3251         mutex_lock(&rdev->mutex);
3252
3253         if (enable && !regulator->bypass) {
3254                 rdev->bypass_count++;
3255
3256                 if (rdev->bypass_count == rdev->open_count) {
3257                         ret = rdev->desc->ops->set_bypass(rdev, enable);
3258                         if (ret != 0)
3259                                 rdev->bypass_count--;
3260                 }
3261
3262         } else if (!enable && regulator->bypass) {
3263                 rdev->bypass_count--;
3264
3265                 if (rdev->bypass_count != rdev->open_count) {
3266                         ret = rdev->desc->ops->set_bypass(rdev, enable);
3267                         if (ret != 0)
3268                                 rdev->bypass_count++;
3269                 }
3270         }
3271
3272         if (ret == 0)
3273                 regulator->bypass = enable;
3274
3275         mutex_unlock(&rdev->mutex);
3276
3277         return ret;
3278 }
3279 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3280
3281 /**
3282  * regulator_register_notifier - register regulator event notifier
3283  * @regulator: regulator source
3284  * @nb: notifier block
3285  *
3286  * Register notifier block to receive regulator events.
3287  */
3288 int regulator_register_notifier(struct regulator *regulator,
3289                               struct notifier_block *nb)
3290 {
3291         return blocking_notifier_chain_register(&regulator->rdev->notifier,
3292                                                 nb);
3293 }
3294 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3295
3296 /**
3297  * regulator_unregister_notifier - unregister regulator event notifier
3298  * @regulator: regulator source
3299  * @nb: notifier block
3300  *
3301  * Unregister regulator event notifier block.
3302  */
3303 int regulator_unregister_notifier(struct regulator *regulator,
3304                                 struct notifier_block *nb)
3305 {
3306         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3307                                                   nb);
3308 }
3309 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3310
3311 /* notify regulator consumers and downstream regulator consumers.
3312  * Note mutex must be held by caller.
3313  */
3314 static int _notifier_call_chain(struct regulator_dev *rdev,
3315                                   unsigned long event, void *data)
3316 {
3317         /* call rdev chain first */
3318         return blocking_notifier_call_chain(&rdev->notifier, event, data);
3319 }
3320
3321 /**
3322  * regulator_bulk_get - get multiple regulator consumers
3323  *
3324  * @dev:           Device to supply
3325  * @num_consumers: Number of consumers to register
3326  * @consumers:     Configuration of consumers; clients are stored here.
3327  *
3328  * @return 0 on success, an errno on failure.
3329  *
3330  * This helper function allows drivers to get several regulator
3331  * consumers in one operation.  If any of the regulators cannot be
3332  * acquired then any regulators that were allocated will be freed
3333  * before returning to the caller.
3334  */
3335 int regulator_bulk_get(struct device *dev, int num_consumers,
3336                        struct regulator_bulk_data *consumers)
3337 {
3338         int i;
3339         int ret;
3340
3341         for (i = 0; i < num_consumers; i++)
3342                 consumers[i].consumer = NULL;
3343
3344         for (i = 0; i < num_consumers; i++) {
3345                 consumers[i].consumer = regulator_get(dev,
3346                                                       consumers[i].supply);
3347                 if (IS_ERR(consumers[i].consumer)) {
3348                         ret = PTR_ERR(consumers[i].consumer);
3349                         dev_err(dev, "Failed to get supply '%s': %d\n",
3350                                 consumers[i].supply, ret);
3351                         consumers[i].consumer = NULL;
3352                         goto err;
3353                 }
3354         }
3355
3356         return 0;
3357
3358 err:
3359         while (--i >= 0)
3360                 regulator_put(consumers[i].consumer);
3361
3362         return ret;
3363 }
3364 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3365
3366 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3367 {
3368         struct regulator_bulk_data *bulk = data;
3369
3370         bulk->ret = regulator_enable(bulk->consumer);
3371 }
3372
3373 /**
3374  * regulator_bulk_enable - enable multiple regulator consumers
3375  *
3376  * @num_consumers: Number of consumers
3377  * @consumers:     Consumer data; clients are stored here.
3378  * @return         0 on success, an errno on failure
3379  *
3380  * This convenience API allows consumers to enable multiple regulator
3381  * clients in a single API call.  If any consumers cannot be enabled
3382  * then any others that were enabled will be disabled again prior to
3383  * return.
3384  */
3385 int regulator_bulk_enable(int num_consumers,
3386                           struct regulator_bulk_data *consumers)
3387 {
3388         ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3389         int i;
3390         int ret = 0;
3391
3392         for (i = 0; i < num_consumers; i++) {
3393                 if (consumers[i].consumer->always_on)
3394                         consumers[i].ret = 0;
3395                 else
3396                         async_schedule_domain(regulator_bulk_enable_async,
3397                                               &consumers[i], &async_domain);
3398         }
3399
3400         async_synchronize_full_domain(&async_domain);
3401
3402         /* If any consumer failed we need to unwind any that succeeded */
3403         for (i = 0; i < num_consumers; i++) {
3404                 if (consumers[i].ret != 0) {
3405                         ret = consumers[i].ret;
3406                         goto err;
3407                 }
3408         }
3409
3410         return 0;
3411
3412 err:
3413         for (i = 0; i < num_consumers; i++) {
3414                 if (consumers[i].ret < 0)
3415                         pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3416                                consumers[i].ret);
3417                 else
3418                         regulator_disable(consumers[i].consumer);
3419         }
3420
3421         return ret;
3422 }
3423 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3424
3425 /**
3426  * regulator_bulk_disable - disable multiple regulator consumers
3427  *
3428  * @num_consumers: Number of consumers
3429  * @consumers:     Consumer data; clients are stored here.
3430  * @return         0 on success, an errno on failure
3431  *
3432  * This convenience API allows consumers to disable multiple regulator
3433  * clients in a single API call.  If any consumers cannot be disabled
3434  * then any others that were disabled will be enabled again prior to
3435  * return.
3436  */
3437 int regulator_bulk_disable(int num_consumers,
3438                            struct regulator_bulk_data *consumers)
3439 {
3440         int i;
3441         int ret, r;
3442
3443         for (i = num_consumers - 1; i >= 0; --i) {
3444                 ret = regulator_disable(consumers[i].consumer);
3445                 if (ret != 0)
3446                         goto err;
3447         }
3448
3449         return 0;
3450
3451 err:
3452         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3453         for (++i; i < num_consumers; ++i) {
3454                 r = regulator_enable(consumers[i].consumer);
3455                 if (r != 0)
3456                         pr_err("Failed to reename %s: %d\n",
3457                                consumers[i].supply, r);
3458         }
3459
3460         return ret;
3461 }
3462 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3463
3464 /**
3465  * regulator_bulk_force_disable - force disable multiple regulator consumers
3466  *
3467  * @num_consumers: Number of consumers
3468  * @consumers:     Consumer data; clients are stored here.
3469  * @return         0 on success, an errno on failure
3470  *
3471  * This convenience API allows consumers to forcibly disable multiple regulator
3472  * clients in a single API call.
3473  * NOTE: This should be used for situations when device damage will
3474  * likely occur if the regulators are not disabled (e.g. over temp).
3475  * Although regulator_force_disable function call for some consumers can
3476  * return error numbers, the function is called for all consumers.
3477  */
3478 int regulator_bulk_force_disable(int num_consumers,
3479                            struct regulator_bulk_data *consumers)
3480 {
3481         int i;
3482         int ret;
3483
3484         for (i = 0; i < num_consumers; i++)
3485                 consumers[i].ret =
3486                             regulator_force_disable(consumers[i].consumer);
3487
3488         for (i = 0; i < num_consumers; i++) {
3489                 if (consumers[i].ret != 0) {
3490                         ret = consumers[i].ret;
3491                         goto out;
3492                 }
3493         }
3494
3495         return 0;
3496 out:
3497         return ret;
3498 }
3499 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3500
3501 /**
3502  * regulator_bulk_free - free multiple regulator consumers
3503  *
3504  * @num_consumers: Number of consumers
3505  * @consumers:     Consumer data; clients are stored here.
3506  *
3507  * This convenience API allows consumers to free multiple regulator
3508  * clients in a single API call.
3509  */
3510 void regulator_bulk_free(int num_consumers,
3511                          struct regulator_bulk_data *consumers)
3512 {
3513         int i;
3514
3515         for (i = 0; i < num_consumers; i++) {
3516                 regulator_put(consumers[i].consumer);
3517                 consumers[i].consumer = NULL;
3518         }
3519 }
3520 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3521
3522 /**
3523  * regulator_notifier_call_chain - call regulator event notifier
3524  * @rdev: regulator source
3525  * @event: notifier block
3526  * @data: callback-specific data.
3527  *
3528  * Called by regulator drivers to notify clients a regulator event has
3529  * occurred. We also notify regulator clients downstream.
3530  * Note lock must be held by caller.
3531  */
3532 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3533                                   unsigned long event, void *data)
3534 {
3535         lockdep_assert_held_once(&rdev->mutex);
3536
3537         _notifier_call_chain(rdev, event, data);
3538         return NOTIFY_DONE;
3539
3540 }
3541 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3542
3543 /**
3544  * regulator_mode_to_status - convert a regulator mode into a status
3545  *
3546  * @mode: Mode to convert
3547  *
3548  * Convert a regulator mode into a status.
3549  */
3550 int regulator_mode_to_status(unsigned int mode)
3551 {
3552         switch (mode) {
3553         case REGULATOR_MODE_FAST:
3554                 return REGULATOR_STATUS_FAST;
3555         case REGULATOR_MODE_NORMAL:
3556                 return REGULATOR_STATUS_NORMAL;
3557         case REGULATOR_MODE_IDLE:
3558                 return REGULATOR_STATUS_IDLE;
3559         case REGULATOR_MODE_STANDBY:
3560                 return REGULATOR_STATUS_STANDBY;
3561         default:
3562                 return REGULATOR_STATUS_UNDEFINED;
3563         }
3564 }
3565 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3566
3567 static struct attribute *regulator_dev_attrs[] = {
3568         &dev_attr_name.attr,
3569         &dev_attr_num_users.attr,
3570         &dev_attr_type.attr,
3571         &dev_attr_microvolts.attr,
3572         &dev_attr_microamps.attr,
3573         &dev_attr_opmode.attr,
3574         &dev_attr_state.attr,
3575         &dev_attr_status.attr,
3576         &dev_attr_bypass.attr,
3577         &dev_attr_requested_microamps.attr,
3578         &dev_attr_min_microvolts.attr,
3579         &dev_attr_max_microvolts.attr,
3580         &dev_attr_min_microamps.attr,
3581         &dev_attr_max_microamps.attr,
3582         &dev_attr_suspend_standby_state.attr,
3583         &dev_attr_suspend_mem_state.attr,
3584         &dev_attr_suspend_disk_state.attr,
3585         &dev_attr_suspend_standby_microvolts.attr,
3586         &dev_attr_suspend_mem_microvolts.attr,
3587         &dev_attr_suspend_disk_microvolts.attr,
3588         &dev_attr_suspend_standby_mode.attr,
3589         &dev_attr_suspend_mem_mode.attr,
3590         &dev_attr_suspend_disk_mode.attr,
3591         NULL
3592 };
3593
3594 /*
3595  * To avoid cluttering sysfs (and memory) with useless state, only
3596  * create attributes that can be meaningfully displayed.
3597  */
3598 static umode_t regulator_attr_is_visible(struct kobject *kobj,
3599                                          struct attribute *attr, int idx)
3600 {
3601         struct device *dev = kobj_to_dev(kobj);
3602         struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
3603         const struct regulator_ops *ops = rdev->desc->ops;
3604         umode_t mode = attr->mode;
3605
3606         /* these three are always present */
3607         if (attr == &dev_attr_name.attr ||
3608             attr == &dev_attr_num_users.attr ||
3609             attr == &dev_attr_type.attr)
3610                 return mode;
3611
3612         /* some attributes need specific methods to be displayed */
3613         if (attr == &dev_attr_microvolts.attr) {
3614                 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3615                     (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3616                     (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3617                     (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3618                         return mode;
3619                 return 0;
3620         }
3621
3622         if (attr == &dev_attr_microamps.attr)
3623                 return ops->get_current_limit ? mode : 0;
3624
3625         if (attr == &dev_attr_opmode.attr)
3626                 return ops->get_mode ? mode : 0;
3627
3628         if (attr == &dev_attr_state.attr)
3629                 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3630
3631         if (attr == &dev_attr_status.attr)
3632                 return ops->get_status ? mode : 0;
3633
3634         if (attr == &dev_attr_bypass.attr)
3635                 return ops->get_bypass ? mode : 0;
3636
3637         /* some attributes are type-specific */
3638         if (attr == &dev_attr_requested_microamps.attr)
3639                 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
3640
3641         /* constraints need specific supporting methods */
3642         if (attr == &dev_attr_min_microvolts.attr ||
3643             attr == &dev_attr_max_microvolts.attr)
3644                 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
3645
3646         if (attr == &dev_attr_min_microamps.attr ||
3647             attr == &dev_attr_max_microamps.attr)
3648                 return ops->set_current_limit ? mode : 0;
3649
3650         if (attr == &dev_attr_suspend_standby_state.attr ||
3651             attr == &dev_attr_suspend_mem_state.attr ||
3652             attr == &dev_attr_suspend_disk_state.attr)
3653                 return mode;
3654
3655         if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3656             attr == &dev_attr_suspend_mem_microvolts.attr ||
3657             attr == &dev_attr_suspend_disk_microvolts.attr)
3658                 return ops->set_suspend_voltage ? mode : 0;
3659
3660         if (attr == &dev_attr_suspend_standby_mode.attr ||
3661             attr == &dev_attr_suspend_mem_mode.attr ||
3662             attr == &dev_attr_suspend_disk_mode.attr)
3663                 return ops->set_suspend_mode ? mode : 0;
3664
3665         return mode;
3666 }
3667
3668 static const struct attribute_group regulator_dev_group = {
3669         .attrs = regulator_dev_attrs,
3670         .is_visible = regulator_attr_is_visible,
3671 };
3672
3673 static const struct attribute_group *regulator_dev_groups[] = {
3674         &regulator_dev_group,
3675         NULL
3676 };
3677
3678 static void regulator_dev_release(struct device *dev)
3679 {
3680         struct regulator_dev *rdev = dev_get_drvdata(dev);
3681
3682         kfree(rdev->constraints);
3683         of_node_put(rdev->dev.of_node);
3684         kfree(rdev);
3685 }
3686
3687 static struct class regulator_class = {
3688         .name = "regulator",
3689         .dev_release = regulator_dev_release,
3690         .dev_groups = regulator_dev_groups,
3691 };
3692
3693 static void rdev_init_debugfs(struct regulator_dev *rdev)
3694 {
3695         struct device *parent = rdev->dev.parent;
3696         const char *rname = rdev_get_name(rdev);
3697         char name[NAME_MAX];
3698
3699         /* Avoid duplicate debugfs directory names */
3700         if (parent && rname == rdev->desc->name) {
3701                 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3702                          rname);
3703                 rname = name;
3704         }
3705
3706         rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
3707         if (!rdev->debugfs) {
3708                 rdev_warn(rdev, "Failed to create debugfs directory\n");
3709                 return;
3710         }
3711
3712         debugfs_create_u32("use_count", 0444, rdev->debugfs,
3713                            &rdev->use_count);
3714         debugfs_create_u32("open_count", 0444, rdev->debugfs,
3715                            &rdev->open_count);
3716         debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3717                            &rdev->bypass_count);
3718 }
3719
3720 /**
3721  * regulator_register - register regulator
3722  * @regulator_desc: regulator to register
3723  * @cfg: runtime configuration for regulator
3724  *
3725  * Called by regulator drivers to register a regulator.
3726  * Returns a valid pointer to struct regulator_dev on success
3727  * or an ERR_PTR() on error.
3728  */
3729 struct regulator_dev *
3730 regulator_register(const struct regulator_desc *regulator_desc,
3731                    const struct regulator_config *cfg)
3732 {
3733         const struct regulation_constraints *constraints = NULL;
3734         const struct regulator_init_data *init_data;
3735         struct regulator_config *config = NULL;
3736         static atomic_t regulator_no = ATOMIC_INIT(-1);
3737         struct regulator_dev *rdev;
3738         struct device *dev;
3739         int ret, i;
3740
3741         if (regulator_desc == NULL || cfg == NULL)
3742                 return ERR_PTR(-EINVAL);
3743
3744         dev = cfg->dev;
3745         WARN_ON(!dev);
3746
3747         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3748                 return ERR_PTR(-EINVAL);
3749
3750         if (regulator_desc->type != REGULATOR_VOLTAGE &&
3751             regulator_desc->type != REGULATOR_CURRENT)
3752                 return ERR_PTR(-EINVAL);
3753
3754         /* Only one of each should be implemented */
3755         WARN_ON(regulator_desc->ops->get_voltage &&
3756                 regulator_desc->ops->get_voltage_sel);
3757         WARN_ON(regulator_desc->ops->set_voltage &&
3758                 regulator_desc->ops->set_voltage_sel);
3759
3760         /* If we're using selectors we must implement list_voltage. */
3761         if (regulator_desc->ops->get_voltage_sel &&
3762             !regulator_desc->ops->list_voltage) {
3763                 return ERR_PTR(-EINVAL);
3764         }
3765         if (regulator_desc->ops->set_voltage_sel &&
3766             !regulator_desc->ops->list_voltage) {
3767                 return ERR_PTR(-EINVAL);
3768         }
3769
3770         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3771         if (rdev == NULL)
3772                 return ERR_PTR(-ENOMEM);
3773
3774         /*
3775          * Duplicate the config so the driver could override it after
3776          * parsing init data.
3777          */
3778         config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3779         if (config == NULL) {
3780                 kfree(rdev);
3781                 return ERR_PTR(-ENOMEM);
3782         }
3783
3784         init_data = regulator_of_get_init_data(dev, regulator_desc, config,
3785                                                &rdev->dev.of_node);
3786         if (!init_data) {
3787                 init_data = config->init_data;
3788                 rdev->dev.of_node = of_node_get(config->of_node);
3789         }
3790
3791         mutex_lock(&regulator_list_mutex);
3792
3793         mutex_init(&rdev->mutex);
3794         rdev->reg_data = config->driver_data;
3795         rdev->owner = regulator_desc->owner;
3796         rdev->desc = regulator_desc;
3797         if (config->regmap)
3798                 rdev->regmap = config->regmap;
3799         else if (dev_get_regmap(dev, NULL))
3800                 rdev->regmap = dev_get_regmap(dev, NULL);
3801         else if (dev->parent)
3802                 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3803         INIT_LIST_HEAD(&rdev->consumer_list);
3804         INIT_LIST_HEAD(&rdev->list);
3805         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3806         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3807
3808         /* preform any regulator specific init */
3809         if (init_data && init_data->regulator_init) {
3810                 ret = init_data->regulator_init(rdev->reg_data);
3811                 if (ret < 0)
3812                         goto clean;
3813         }
3814
3815         /* register with sysfs */
3816         rdev->dev.class = &regulator_class;
3817         rdev->dev.parent = dev;
3818         dev_set_name(&rdev->dev, "regulator.%lu",
3819                     (unsigned long) atomic_inc_return(&regulator_no));
3820         ret = device_register(&rdev->dev);
3821         if (ret != 0) {
3822                 put_device(&rdev->dev);
3823                 goto clean;
3824         }
3825
3826         dev_set_drvdata(&rdev->dev, rdev);
3827
3828         if ((config->ena_gpio || config->ena_gpio_initialized) &&
3829             gpio_is_valid(config->ena_gpio)) {
3830                 ret = regulator_ena_gpio_request(rdev, config);
3831                 if (ret != 0) {
3832                         rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3833                                  config->ena_gpio, ret);
3834                         goto wash;
3835                 }
3836         }
3837
3838         /* set regulator constraints */
3839         if (init_data)
3840                 constraints = &init_data->constraints;
3841
3842         ret = set_machine_constraints(rdev, constraints);
3843         if (ret < 0)
3844                 goto scrub;
3845
3846         if (init_data && init_data->supply_regulator)
3847                 rdev->supply_name = init_data->supply_regulator;
3848         else if (regulator_desc->supply_name)
3849                 rdev->supply_name = regulator_desc->supply_name;
3850
3851         /* add consumers devices */
3852         if (init_data) {
3853                 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3854                         ret = set_consumer_device_supply(rdev,
3855                                 init_data->consumer_supplies[i].dev_name,
3856                                 init_data->consumer_supplies[i].supply);
3857                         if (ret < 0) {
3858                                 dev_err(dev, "Failed to set supply %s\n",
3859                                         init_data->consumer_supplies[i].supply);
3860                                 goto unset_supplies;
3861                         }
3862                 }
3863         }
3864
3865         rdev_init_debugfs(rdev);
3866 out:
3867         mutex_unlock(&regulator_list_mutex);
3868         kfree(config);
3869         return rdev;
3870
3871 unset_supplies:
3872         unset_regulator_supplies(rdev);
3873
3874 scrub:
3875         regulator_ena_gpio_free(rdev);
3876         kfree(rdev->constraints);
3877 wash:
3878         device_unregister(&rdev->dev);
3879         /* device core frees rdev */
3880         rdev = ERR_PTR(ret);
3881         goto out;
3882
3883 clean:
3884         kfree(rdev);
3885         rdev = ERR_PTR(ret);
3886         goto out;
3887 }
3888 EXPORT_SYMBOL_GPL(regulator_register);
3889
3890 /**
3891  * regulator_unregister - unregister regulator
3892  * @rdev: regulator to unregister
3893  *
3894  * Called by regulator drivers to unregister a regulator.
3895  */
3896 void regulator_unregister(struct regulator_dev *rdev)
3897 {
3898         if (rdev == NULL)
3899                 return;
3900
3901         if (rdev->supply) {
3902                 while (rdev->use_count--)
3903                         regulator_disable(rdev->supply);
3904                 regulator_put(rdev->supply);
3905         }
3906         mutex_lock(&regulator_list_mutex);
3907         debugfs_remove_recursive(rdev->debugfs);
3908         flush_work(&rdev->disable_work.work);
3909         WARN_ON(rdev->open_count);
3910         unset_regulator_supplies(rdev);
3911         list_del(&rdev->list);
3912         mutex_unlock(&regulator_list_mutex);
3913         regulator_ena_gpio_free(rdev);
3914         device_unregister(&rdev->dev);
3915 }
3916 EXPORT_SYMBOL_GPL(regulator_unregister);
3917
3918 static int _regulator_suspend_prepare(struct device *dev, void *data)
3919 {
3920         struct regulator_dev *rdev = dev_to_rdev(dev);
3921         const suspend_state_t *state = data;
3922         int ret;
3923
3924         mutex_lock(&rdev->mutex);
3925         ret = suspend_prepare(rdev, *state);
3926         mutex_unlock(&rdev->mutex);
3927
3928         return ret;
3929 }
3930
3931 /**
3932  * regulator_suspend_prepare - prepare regulators for system wide suspend
3933  * @state: system suspend state
3934  *
3935  * Configure each regulator with it's suspend operating parameters for state.
3936  * This will usually be called by machine suspend code prior to supending.
3937  */
3938 int regulator_suspend_prepare(suspend_state_t state)
3939 {
3940         /* ON is handled by regulator active state */
3941         if (state == PM_SUSPEND_ON)
3942                 return -EINVAL;
3943
3944         return class_for_each_device(&regulator_class, NULL, &state,
3945                                      _regulator_suspend_prepare);
3946 }
3947 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3948
3949 static int _regulator_suspend_finish(struct device *dev, void *data)
3950 {
3951         struct regulator_dev *rdev = dev_to_rdev(dev);
3952         int ret;
3953
3954         mutex_lock(&rdev->mutex);
3955         if (rdev->use_count > 0  || rdev->constraints->always_on) {
3956                 if (!_regulator_is_enabled(rdev)) {
3957                         ret = _regulator_do_enable(rdev);
3958                         if (ret)
3959                                 dev_err(dev,
3960                                         "Failed to resume regulator %d\n",
3961                                         ret);
3962                 }
3963         } else {
3964                 if (!have_full_constraints())
3965                         goto unlock;
3966                 if (!_regulator_is_enabled(rdev))
3967                         goto unlock;
3968
3969                 ret = _regulator_do_disable(rdev);
3970                 if (ret)
3971                         dev_err(dev, "Failed to suspend regulator %d\n", ret);
3972         }
3973 unlock:
3974         mutex_unlock(&rdev->mutex);
3975
3976         /* Keep processing regulators in spite of any errors */
3977         return 0;
3978 }
3979
3980 /**
3981  * regulator_suspend_finish - resume regulators from system wide suspend
3982  *
3983  * Turn on regulators that might be turned off by regulator_suspend_prepare
3984  * and that should be turned on according to the regulators properties.
3985  */
3986 int regulator_suspend_finish(void)
3987 {
3988         return class_for_each_device(&regulator_class, NULL, NULL,
3989                                      _regulator_suspend_finish);
3990 }
3991 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3992
3993 /**
3994  * regulator_has_full_constraints - the system has fully specified constraints
3995  *
3996  * Calling this function will cause the regulator API to disable all
3997  * regulators which have a zero use count and don't have an always_on
3998  * constraint in a late_initcall.
3999  *
4000  * The intention is that this will become the default behaviour in a
4001  * future kernel release so users are encouraged to use this facility
4002  * now.
4003  */
4004 void regulator_has_full_constraints(void)
4005 {
4006         has_full_constraints = 1;
4007 }
4008 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4009
4010 /**
4011  * rdev_get_drvdata - get rdev regulator driver data
4012  * @rdev: regulator
4013  *
4014  * Get rdev regulator driver private data. This call can be used in the
4015  * regulator driver context.
4016  */
4017 void *rdev_get_drvdata(struct regulator_dev *rdev)
4018 {
4019         return rdev->reg_data;
4020 }
4021 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4022
4023 /**
4024  * regulator_get_drvdata - get regulator driver data
4025  * @regulator: regulator
4026  *
4027  * Get regulator driver private data. This call can be used in the consumer
4028  * driver context when non API regulator specific functions need to be called.
4029  */
4030 void *regulator_get_drvdata(struct regulator *regulator)
4031 {
4032         return regulator->rdev->reg_data;
4033 }
4034 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4035
4036 /**
4037  * regulator_set_drvdata - set regulator driver data
4038  * @regulator: regulator
4039  * @data: data
4040  */
4041 void regulator_set_drvdata(struct regulator *regulator, void *data)
4042 {
4043         regulator->rdev->reg_data = data;
4044 }
4045 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4046
4047 /**
4048  * regulator_get_id - get regulator ID
4049  * @rdev: regulator
4050  */
4051 int rdev_get_id(struct regulator_dev *rdev)
4052 {
4053         return rdev->desc->id;
4054 }
4055 EXPORT_SYMBOL_GPL(rdev_get_id);
4056
4057 struct device *rdev_get_dev(struct regulator_dev *rdev)
4058 {
4059         return &rdev->dev;
4060 }
4061 EXPORT_SYMBOL_GPL(rdev_get_dev);
4062
4063 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4064 {
4065         return reg_init_data->driver_data;
4066 }
4067 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4068
4069 #ifdef CONFIG_DEBUG_FS
4070 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4071                                     size_t count, loff_t *ppos)
4072 {
4073         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4074         ssize_t len, ret = 0;
4075         struct regulator_map *map;
4076
4077         if (!buf)
4078                 return -ENOMEM;
4079
4080         list_for_each_entry(map, &regulator_map_list, list) {
4081                 len = snprintf(buf + ret, PAGE_SIZE - ret,
4082                                "%s -> %s.%s\n",
4083                                rdev_get_name(map->regulator), map->dev_name,
4084                                map->supply);
4085                 if (len >= 0)
4086                         ret += len;
4087                 if (ret > PAGE_SIZE) {
4088                         ret = PAGE_SIZE;
4089                         break;
4090                 }
4091         }
4092
4093         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4094
4095         kfree(buf);
4096
4097         return ret;
4098 }
4099 #endif
4100
4101 static const struct file_operations supply_map_fops = {
4102 #ifdef CONFIG_DEBUG_FS
4103         .read = supply_map_read_file,
4104         .llseek = default_llseek,
4105 #endif
4106 };
4107
4108 #ifdef CONFIG_DEBUG_FS
4109 struct summary_data {
4110         struct seq_file *s;
4111         struct regulator_dev *parent;
4112         int level;
4113 };
4114
4115 static void regulator_summary_show_subtree(struct seq_file *s,
4116                                            struct regulator_dev *rdev,
4117                                            int level);
4118
4119 static int regulator_summary_show_children(struct device *dev, void *data)
4120 {
4121         struct regulator_dev *rdev = dev_to_rdev(dev);
4122         struct summary_data *summary_data = data;
4123
4124         if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4125                 regulator_summary_show_subtree(summary_data->s, rdev,
4126                                                summary_data->level + 1);
4127
4128         return 0;
4129 }
4130
4131 static void regulator_summary_show_subtree(struct seq_file *s,
4132                                            struct regulator_dev *rdev,
4133                                            int level)
4134 {
4135         struct regulation_constraints *c;
4136         struct regulator *consumer;
4137         struct summary_data summary_data;
4138
4139         if (!rdev)
4140                 return;
4141
4142         seq_printf(s, "%*s%-*s %3d %4d %6d ",
4143                    level * 3 + 1, "",
4144                    30 - level * 3, rdev_get_name(rdev),
4145                    rdev->use_count, rdev->open_count, rdev->bypass_count);
4146
4147         seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4148         seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4149
4150         c = rdev->constraints;
4151         if (c) {
4152                 switch (rdev->desc->type) {
4153                 case REGULATOR_VOLTAGE:
4154                         seq_printf(s, "%5dmV %5dmV ",
4155                                    c->min_uV / 1000, c->max_uV / 1000);
4156                         break;
4157                 case REGULATOR_CURRENT:
4158                         seq_printf(s, "%5dmA %5dmA ",
4159                                    c->min_uA / 1000, c->max_uA / 1000);
4160                         break;
4161                 }
4162         }
4163
4164         seq_puts(s, "\n");
4165
4166         list_for_each_entry(consumer, &rdev->consumer_list, list) {
4167                 if (consumer->dev->class == &regulator_class)
4168                         continue;
4169
4170                 seq_printf(s, "%*s%-*s ",
4171                            (level + 1) * 3 + 1, "",
4172                            30 - (level + 1) * 3, dev_name(consumer->dev));
4173
4174                 switch (rdev->desc->type) {
4175                 case REGULATOR_VOLTAGE:
4176                         seq_printf(s, "%37dmV %5dmV",
4177                                    consumer->min_uV / 1000,
4178                                    consumer->max_uV / 1000);
4179                         break;
4180                 case REGULATOR_CURRENT:
4181                         break;
4182                 }
4183
4184                 seq_puts(s, "\n");
4185         }
4186
4187         summary_data.s = s;
4188         summary_data.level = level;
4189         summary_data.parent = rdev;
4190
4191         class_for_each_device(&regulator_class, NULL, &summary_data,
4192                               regulator_summary_show_children);
4193 }
4194
4195 static int regulator_summary_show_roots(struct device *dev, void *data)
4196 {
4197         struct regulator_dev *rdev = dev_to_rdev(dev);
4198         struct seq_file *s = data;
4199
4200         if (!rdev->supply)
4201                 regulator_summary_show_subtree(s, rdev, 0);
4202
4203         return 0;
4204 }
4205
4206 static int regulator_summary_show(struct seq_file *s, void *data)
4207 {
4208         seq_puts(s, " regulator                      use open bypass voltage current     min     max\n");
4209         seq_puts(s, "-------------------------------------------------------------------------------\n");
4210
4211         class_for_each_device(&regulator_class, NULL, s,
4212                               regulator_summary_show_roots);
4213
4214         return 0;
4215 }
4216
4217 static int regulator_summary_open(struct inode *inode, struct file *file)
4218 {
4219         return single_open(file, regulator_summary_show, inode->i_private);
4220 }
4221 #endif
4222
4223 static const struct file_operations regulator_summary_fops = {
4224 #ifdef CONFIG_DEBUG_FS
4225         .open           = regulator_summary_open,
4226         .read           = seq_read,
4227         .llseek         = seq_lseek,
4228         .release        = single_release,
4229 #endif
4230 };
4231
4232 static int __init regulator_init(void)
4233 {
4234         int ret;
4235
4236         ret = class_register(&regulator_class);
4237
4238         debugfs_root = debugfs_create_dir("regulator", NULL);
4239         if (!debugfs_root)
4240                 pr_warn("regulator: Failed to create debugfs directory\n");
4241
4242         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4243                             &supply_map_fops);
4244
4245         debugfs_create_file("regulator_summary", 0444, debugfs_root,
4246                             NULL, &regulator_summary_fops);
4247
4248         regulator_dummy_init();
4249
4250         return ret;
4251 }
4252
4253 /* init early to allow our consumers to complete system booting */
4254 core_initcall(regulator_init);
4255
4256 static int __init regulator_late_cleanup(struct device *dev, void *data)
4257 {
4258         struct regulator_dev *rdev = dev_to_rdev(dev);
4259         const struct regulator_ops *ops = rdev->desc->ops;
4260         struct regulation_constraints *c = rdev->constraints;
4261         int enabled, ret;
4262
4263         if (c && c->always_on)
4264                 return 0;
4265
4266         if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4267                 return 0;
4268
4269         mutex_lock(&rdev->mutex);
4270
4271         if (rdev->use_count)
4272                 goto unlock;
4273
4274         /* If we can't read the status assume it's on. */
4275         if (ops->is_enabled)
4276                 enabled = ops->is_enabled(rdev);
4277         else
4278                 enabled = 1;
4279
4280         if (!enabled)
4281                 goto unlock;
4282
4283         if (have_full_constraints()) {
4284                 /* We log since this may kill the system if it goes
4285                  * wrong. */
4286                 rdev_info(rdev, "disabling\n");
4287                 ret = _regulator_do_disable(rdev);
4288                 if (ret != 0)
4289                         rdev_err(rdev, "couldn't disable: %d\n", ret);
4290         } else {
4291                 /* The intention is that in future we will
4292                  * assume that full constraints are provided
4293                  * so warn even if we aren't going to do
4294                  * anything here.
4295                  */
4296                 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4297         }
4298
4299 unlock:
4300         mutex_unlock(&rdev->mutex);
4301
4302         return 0;
4303 }
4304
4305 static int __init regulator_init_complete(void)
4306 {
4307         /*
4308          * Since DT doesn't provide an idiomatic mechanism for
4309          * enabling full constraints and since it's much more natural
4310          * with DT to provide them just assume that a DT enabled
4311          * system has full constraints.
4312          */
4313         if (of_have_populated_dt())
4314                 has_full_constraints = true;
4315
4316         /* If we have a full configuration then disable any regulators
4317          * we have permission to change the status for and which are
4318          * not in use or always_on.  This is effectively the default
4319          * for DT and ACPI as they have full constraints.
4320          */
4321         class_for_each_device(&regulator_class, NULL, NULL,
4322                               regulator_late_cleanup);
4323
4324         return 0;
4325 }
4326 late_initcall_sync(regulator_init_complete);