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