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