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