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