regulator: palmas: implement errata for ES1.0,ES2.0 and ES2.1
[linux-2.6.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/of.h>
27 #include <linux/regulator/of_regulator.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/module.h>
32
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/regulator.h>
35 #include <linux/debugfs.h>
36 #include <linux/seq_file.h>
37 #include <linux/uaccess.h>
38
39 #include "dummy.h"
40
41 #define rdev_crit(rdev, fmt, ...)                                       \
42         pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43 #define rdev_err(rdev, fmt, ...)                                        \
44         pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45 #define rdev_warn(rdev, fmt, ...)                                       \
46         pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 #define rdev_info(rdev, fmt, ...)                                       \
48         pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 #define rdev_dbg(rdev, fmt, ...)                                        \
50         pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51
52 static DEFINE_MUTEX(regulator_list_mutex);
53 static LIST_HEAD(regulator_list);
54 static LIST_HEAD(regulator_map_list);
55 static bool has_full_constraints;
56 static bool board_wants_dummy_regulator;
57
58 static struct dentry *debugfs_root;
59
60 /*
61  * struct regulator_map
62  *
63  * Used to provide symbolic supply names to devices.
64  */
65 struct regulator_map {
66         struct list_head list;
67         const char *dev_name;   /* The dev_name() for the consumer */
68         const char *supply;
69         struct regulator_dev *regulator;
70 };
71
72 /*
73  * struct regulator
74  *
75  * One for each consumer device.
76  */
77 struct regulator {
78         struct device *dev;
79         struct list_head list;
80         int uA_load;
81         int min_uV;
82         int max_uV;
83         char *supply_name;
84         struct device_attribute dev_attr;
85         struct regulator_dev *rdev;
86         struct dentry *debugfs;
87 };
88
89 static int _regulator_is_enabled(struct regulator_dev *rdev);
90 static int _regulator_disable(struct regulator_dev *rdev);
91 static int _regulator_enable(struct regulator_dev *rdev);
92 static int _regulator_get_enable_time(struct regulator_dev *rdev);
93 static int _regulator_get_voltage(struct regulator_dev *rdev);
94 static int _regulator_get_current_limit(struct regulator_dev *rdev);
95 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
96 static void _notifier_call_chain(struct regulator_dev *rdev,
97                                   unsigned long event, void *data);
98 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
99                                      int min_uV, int max_uV);
100 static struct regulator *create_regulator(struct regulator_dev *rdev,
101                                           struct device *dev,
102                                           const char *supply_name);
103
104 static const char *rdev_get_name(struct regulator_dev *rdev)
105 {
106         if (rdev->constraints && rdev->constraints->name)
107                 return rdev->constraints->name;
108         else if (rdev->desc->name)
109                 return rdev->desc->name;
110         else
111                 return "";
112 }
113
114 /* gets the regulator for a given consumer device */
115 static struct regulator *get_device_regulator(struct device *dev)
116 {
117         struct regulator *regulator = NULL;
118         struct regulator_dev *rdev;
119
120         mutex_lock(&regulator_list_mutex);
121         list_for_each_entry(rdev, &regulator_list, list) {
122                 mutex_lock(&rdev->mutex);
123                 list_for_each_entry(regulator, &rdev->consumer_list, list) {
124                         if (regulator->dev == dev) {
125                                 mutex_unlock(&rdev->mutex);
126                                 mutex_unlock(&regulator_list_mutex);
127                                 return regulator;
128                         }
129                 }
130                 mutex_unlock(&rdev->mutex);
131         }
132         mutex_unlock(&regulator_list_mutex);
133         return NULL;
134 }
135
136 /**
137  * of_get_regulator - get a regulator device node based on supply name
138  * @dev: Device pointer for the consumer (of regulator) device
139  * @supply: regulator supply name
140  *
141  * Extract the regulator device node corresponding to the supply name.
142  * retruns the device node corresponding to the regulator if found, else
143  * returns NULL.
144  */
145 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
146 {
147         struct device_node *regnode = NULL;
148         char prop_name[32]; /* 32 is max size of property name */
149
150         dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
151
152         snprintf(prop_name, 32, "%s-supply", supply);
153         regnode = of_parse_phandle(dev->of_node, prop_name, 0);
154
155         if (!regnode) {
156                 dev_dbg(dev, "Looking up %s property in node %s failed",
157                                 prop_name, dev->of_node->full_name);
158                 return NULL;
159         }
160         return regnode;
161 }
162
163 /* Platform voltage constraint check */
164 static int regulator_check_voltage(struct regulator_dev *rdev,
165                                    int *min_uV, int *max_uV)
166 {
167         BUG_ON(*min_uV > *max_uV);
168
169         if (!rdev->constraints) {
170                 rdev_err(rdev, "no constraints\n");
171                 return -ENODEV;
172         }
173         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
174                 rdev_err(rdev, "operation not allowed\n");
175                 return -EPERM;
176         }
177
178         if (*max_uV > rdev->constraints->max_uV)
179                 *max_uV = rdev->constraints->max_uV;
180         if (*min_uV < rdev->constraints->min_uV)
181                 *min_uV = rdev->constraints->min_uV;
182
183         if (*min_uV > *max_uV) {
184                 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
185                          *min_uV, *max_uV);
186                 return -EINVAL;
187         }
188
189         return 0;
190 }
191
192 /* Make sure we select a voltage that suits the needs of all
193  * regulator consumers
194  */
195 static int regulator_check_consumers(struct regulator_dev *rdev,
196                                      int *min_uV, int *max_uV)
197 {
198         struct regulator *regulator;
199
200         list_for_each_entry(regulator, &rdev->consumer_list, list) {
201                 /*
202                  * Assume consumers that didn't say anything are OK
203                  * with anything in the constraint range.
204                  */
205                 if (!regulator->min_uV && !regulator->max_uV)
206                         continue;
207
208                 if (*max_uV > regulator->max_uV)
209                         *max_uV = regulator->max_uV;
210                 if (*min_uV < regulator->min_uV)
211                         *min_uV = regulator->min_uV;
212         }
213
214         if (*min_uV > *max_uV)
215                 return -EINVAL;
216
217         return 0;
218 }
219
220 /* current constraint check */
221 static int regulator_check_current_limit(struct regulator_dev *rdev,
222                                         int *min_uA, int *max_uA)
223 {
224         BUG_ON(*min_uA > *max_uA);
225
226         if (!rdev->constraints) {
227                 rdev_err(rdev, "no constraints\n");
228                 return -ENODEV;
229         }
230         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
231                 rdev_err(rdev, "operation not allowed\n");
232                 return -EPERM;
233         }
234
235         if (*max_uA > rdev->constraints->max_uA)
236                 *max_uA = rdev->constraints->max_uA;
237         if (*min_uA < rdev->constraints->min_uA)
238                 *min_uA = rdev->constraints->min_uA;
239
240         if (*min_uA > *max_uA) {
241                 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
242                          *min_uA, *max_uA);
243                 return -EINVAL;
244         }
245
246         return 0;
247 }
248
249 /* operating mode constraint check */
250 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
251 {
252         switch (*mode) {
253         case REGULATOR_MODE_FAST:
254         case REGULATOR_MODE_NORMAL:
255         case REGULATOR_MODE_IDLE:
256         case REGULATOR_MODE_STANDBY:
257                 break;
258         default:
259                 rdev_err(rdev, "invalid mode %x specified\n", *mode);
260                 return -EINVAL;
261         }
262
263         if (!rdev->constraints) {
264                 rdev_err(rdev, "no constraints\n");
265                 return -ENODEV;
266         }
267         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
268                 rdev_err(rdev, "operation not allowed\n");
269                 return -EPERM;
270         }
271
272         /* The modes are bitmasks, the most power hungry modes having
273          * the lowest values. If the requested mode isn't supported
274          * try higher modes. */
275         while (*mode) {
276                 if (rdev->constraints->valid_modes_mask & *mode)
277                         return 0;
278                 *mode /= 2;
279         }
280
281         return -EINVAL;
282 }
283
284 /* dynamic regulator mode switching constraint check */
285 static int regulator_check_drms(struct regulator_dev *rdev)
286 {
287         if (!rdev->constraints) {
288                 rdev_err(rdev, "no constraints\n");
289                 return -ENODEV;
290         }
291         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
292                 rdev_err(rdev, "operation not allowed\n");
293                 return -EPERM;
294         }
295         return 0;
296 }
297
298 /* dynamic regulator control mode switching constraint check */
299 static int regulator_check_control(struct regulator_dev *rdev)
300 {
301         if (!rdev->constraints) {
302                 rdev_err(rdev, "no constraints\n");
303                 return -ENODEV;
304         }
305         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CONTROL)) {
306                 rdev_err(rdev, "operation not allowed\n");
307                 return -EPERM;
308         }
309         return 0;
310 }
311
312 static ssize_t device_requested_uA_show(struct device *dev,
313                              struct device_attribute *attr, char *buf)
314 {
315         struct regulator *regulator;
316
317         regulator = get_device_regulator(dev);
318         if (regulator == NULL)
319                 return 0;
320
321         return sprintf(buf, "%d\n", regulator->uA_load);
322 }
323
324 static ssize_t regulator_uV_set(struct device *dev,
325         struct device_attribute *attr, const char *buf, size_t count)
326 {
327         struct regulator_dev *rdev = dev_get_drvdata(dev);
328         int ret;
329         int min_uV;
330         int max_uV = rdev->constraints->max_uV;
331         char *p = (char *)buf;
332
333         min_uV = memparse(p, &p);
334         mutex_lock(&rdev->mutex);
335
336         /* sanity check */
337         if (!rdev->desc->ops->set_voltage &&
338                 !rdev->desc->ops->set_voltage_sel) {
339                 rdev_err(rdev, "The operation is not supported\n");
340                 goto out;
341         }
342
343         /* constraints check */
344         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
345         if (ret < 0) {
346                 rdev_err(rdev, "Voltage is out of range min:max= %d:%d\n",
347                         rdev->constraints->min_uV, rdev->constraints->max_uV);
348                 goto out;
349         }
350
351         /* Consumer check */
352         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
353         if (ret < 0) {
354                 rdev_warn(rdev, "new voltage is out-range for some consumer\n");
355                 rdev_warn(rdev, "min: max = %d:%d\n", min_uV, max_uV);
356         }
357
358         rdev_info(rdev, "Setting voltage min:max = %d:%d\n", min_uV, max_uV);
359         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
360         if (ret < 0)
361                 rdev_warn(rdev, "Can not set voltage %d:%d\n",  min_uV, max_uV);
362
363 out:
364         mutex_unlock(&rdev->mutex);
365         return count;
366 }
367
368 static ssize_t regulator_uV_show(struct device *dev,
369                                 struct device_attribute *attr, char *buf)
370 {
371         struct regulator_dev *rdev = dev_get_drvdata(dev);
372         ssize_t ret;
373
374         mutex_lock(&rdev->mutex);
375         ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
376         mutex_unlock(&rdev->mutex);
377
378         return ret;
379 }
380 static DEVICE_ATTR(microvolts, 0644, regulator_uV_show, regulator_uV_set);
381
382 static ssize_t regulator_uA_show(struct device *dev,
383                                 struct device_attribute *attr, char *buf)
384 {
385         struct regulator_dev *rdev = dev_get_drvdata(dev);
386
387         return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
388 }
389 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
390
391 static ssize_t regulator_name_show(struct device *dev,
392                              struct device_attribute *attr, char *buf)
393 {
394         struct regulator_dev *rdev = dev_get_drvdata(dev);
395
396         return sprintf(buf, "%s\n", rdev_get_name(rdev));
397 }
398
399 static ssize_t regulator_print_opmode(char *buf, int mode)
400 {
401         switch (mode) {
402         case REGULATOR_MODE_FAST:
403                 return sprintf(buf, "fast\n");
404         case REGULATOR_MODE_NORMAL:
405                 return sprintf(buf, "normal\n");
406         case REGULATOR_MODE_IDLE:
407                 return sprintf(buf, "idle\n");
408         case REGULATOR_MODE_STANDBY:
409                 return sprintf(buf, "standby\n");
410         }
411         return sprintf(buf, "unknown\n");
412 }
413
414 static ssize_t regulator_opmode_show(struct device *dev,
415                                     struct device_attribute *attr, char *buf)
416 {
417         struct regulator_dev *rdev = dev_get_drvdata(dev);
418
419         return regulator_print_opmode(buf, _regulator_get_mode(rdev));
420 }
421 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
422
423 static ssize_t regulator_print_state(char *buf, int state)
424 {
425         if (state > 0)
426                 return sprintf(buf, "enabled\n");
427         else if (state == 0)
428                 return sprintf(buf, "disabled\n");
429         else
430                 return sprintf(buf, "unknown\n");
431 }
432
433 static ssize_t regulator_state_show(struct device *dev,
434                                    struct device_attribute *attr, char *buf)
435 {
436         struct regulator_dev *rdev = dev_get_drvdata(dev);
437         ssize_t ret;
438
439         mutex_lock(&rdev->mutex);
440         ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
441         mutex_unlock(&rdev->mutex);
442
443         return ret;
444 }
445
446 static ssize_t regulator_state_set(struct device *dev,
447                    struct device_attribute *attr, const char *buf, size_t count)
448 {
449         struct regulator_dev *rdev = dev_get_drvdata(dev);
450         int ret;
451         bool enabled;
452
453         if ((*buf == 'E') || (*buf == 'e'))
454                 enabled = true;
455         else if ((*buf == 'D') || (*buf == 'd'))
456                 enabled = false;
457         else
458                 return -EINVAL;
459
460         if ((_regulator_is_enabled(rdev) && enabled) ||
461                 (!_regulator_is_enabled(rdev) && !enabled))
462                 return count;
463
464         mutex_lock(&rdev->mutex);
465         if (enabled) {
466                 int delay = 0;
467                 if (!rdev->desc->ops->enable) {
468                         ret = -EINVAL;
469                         goto end;
470                 }
471                 ret = _regulator_get_enable_time(rdev);
472                 if (ret >= 0)
473                         delay = ret;
474                 ret = rdev->desc->ops->enable(rdev);
475                 if (ret < 0) {
476                         rdev_warn(rdev, "enable() failed: %d\n", ret);
477                         goto end;
478                 }
479                 if (delay >= 1000) {
480                         mdelay(delay / 1000);
481                         udelay(delay % 1000);
482                 } else if (delay) {
483                         udelay(delay);
484                 }
485         } else {
486                 if (!rdev->desc->ops->disable) {
487                         ret = -EINVAL;
488                         goto end;
489                 }
490                 ret = rdev->desc->ops->disable(rdev);
491                 if (ret < 0) {
492                         rdev_warn(rdev, "disable() failed: %d\n", ret);
493                         goto end;
494                 }
495         }
496
497 end:
498         mutex_unlock(&rdev->mutex);
499         if (ret < 0)
500                 return ret;
501         return count;
502 }
503 static DEVICE_ATTR(state, 0644, regulator_state_show, regulator_state_set);
504
505 static ssize_t regulator_status_show(struct device *dev,
506                                    struct device_attribute *attr, char *buf)
507 {
508         struct regulator_dev *rdev = dev_get_drvdata(dev);
509         int status;
510         char *label;
511
512         status = rdev->desc->ops->get_status(rdev);
513         if (status < 0)
514                 return status;
515
516         switch (status) {
517         case REGULATOR_STATUS_OFF:
518                 label = "off";
519                 break;
520         case REGULATOR_STATUS_ON:
521                 label = "on";
522                 break;
523         case REGULATOR_STATUS_ERROR:
524                 label = "error";
525                 break;
526         case REGULATOR_STATUS_FAST:
527                 label = "fast";
528                 break;
529         case REGULATOR_STATUS_NORMAL:
530                 label = "normal";
531                 break;
532         case REGULATOR_STATUS_IDLE:
533                 label = "idle";
534                 break;
535         case REGULATOR_STATUS_STANDBY:
536                 label = "standby";
537                 break;
538         default:
539                 return -ERANGE;
540         }
541
542         return sprintf(buf, "%s\n", label);
543 }
544 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
545
546 static ssize_t regulator_min_uA_show(struct device *dev,
547                                     struct device_attribute *attr, char *buf)
548 {
549         struct regulator_dev *rdev = dev_get_drvdata(dev);
550
551         if (!rdev->constraints)
552                 return sprintf(buf, "constraint not defined\n");
553
554         return sprintf(buf, "%d\n", rdev->constraints->min_uA);
555 }
556 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
557
558 static ssize_t regulator_max_uA_show(struct device *dev,
559                                     struct device_attribute *attr, char *buf)
560 {
561         struct regulator_dev *rdev = dev_get_drvdata(dev);
562
563         if (!rdev->constraints)
564                 return sprintf(buf, "constraint not defined\n");
565
566         return sprintf(buf, "%d\n", rdev->constraints->max_uA);
567 }
568 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
569
570 static ssize_t regulator_min_uV_show(struct device *dev,
571                                     struct device_attribute *attr, char *buf)
572 {
573         struct regulator_dev *rdev = dev_get_drvdata(dev);
574
575         if (!rdev->constraints)
576                 return sprintf(buf, "constraint not defined\n");
577
578         return sprintf(buf, "%d\n", rdev->constraints->min_uV);
579 }
580 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
581
582 static ssize_t regulator_max_uV_show(struct device *dev,
583                                     struct device_attribute *attr, char *buf)
584 {
585         struct regulator_dev *rdev = dev_get_drvdata(dev);
586
587         if (!rdev->constraints)
588                 return sprintf(buf, "constraint not defined\n");
589
590         return sprintf(buf, "%d\n", rdev->constraints->max_uV);
591 }
592 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
593
594 static ssize_t regulator_total_uA_show(struct device *dev,
595                                       struct device_attribute *attr, char *buf)
596 {
597         struct regulator_dev *rdev = dev_get_drvdata(dev);
598         struct regulator *regulator;
599         int uA = 0;
600
601         mutex_lock(&rdev->mutex);
602         list_for_each_entry(regulator, &rdev->consumer_list, list)
603                 uA += regulator->uA_load;
604         mutex_unlock(&rdev->mutex);
605         return sprintf(buf, "%d\n", uA);
606 }
607 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
608
609 static ssize_t regulator_num_users_show(struct device *dev,
610                                       struct device_attribute *attr, char *buf)
611 {
612         struct regulator_dev *rdev = dev_get_drvdata(dev);
613         return sprintf(buf, "%d\n", rdev->use_count);
614 }
615
616 static ssize_t regulator_type_show(struct device *dev,
617                                   struct device_attribute *attr, char *buf)
618 {
619         struct regulator_dev *rdev = dev_get_drvdata(dev);
620
621         switch (rdev->desc->type) {
622         case REGULATOR_VOLTAGE:
623                 return sprintf(buf, "voltage\n");
624         case REGULATOR_CURRENT:
625                 return sprintf(buf, "current\n");
626         }
627         return sprintf(buf, "unknown\n");
628 }
629
630 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
631                                 struct device_attribute *attr, char *buf)
632 {
633         struct regulator_dev *rdev = dev_get_drvdata(dev);
634
635         return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
636 }
637 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
638                 regulator_suspend_mem_uV_show, NULL);
639
640 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
641                                 struct device_attribute *attr, char *buf)
642 {
643         struct regulator_dev *rdev = dev_get_drvdata(dev);
644
645         return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
646 }
647 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
648                 regulator_suspend_disk_uV_show, NULL);
649
650 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
651                                 struct device_attribute *attr, char *buf)
652 {
653         struct regulator_dev *rdev = dev_get_drvdata(dev);
654
655         return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
656 }
657 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
658                 regulator_suspend_standby_uV_show, NULL);
659
660 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
661                                 struct device_attribute *attr, char *buf)
662 {
663         struct regulator_dev *rdev = dev_get_drvdata(dev);
664
665         return regulator_print_opmode(buf,
666                 rdev->constraints->state_mem.mode);
667 }
668 static DEVICE_ATTR(suspend_mem_mode, 0444,
669                 regulator_suspend_mem_mode_show, NULL);
670
671 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
672                                 struct device_attribute *attr, char *buf)
673 {
674         struct regulator_dev *rdev = dev_get_drvdata(dev);
675
676         return regulator_print_opmode(buf,
677                 rdev->constraints->state_disk.mode);
678 }
679 static DEVICE_ATTR(suspend_disk_mode, 0444,
680                 regulator_suspend_disk_mode_show, NULL);
681
682 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
683                                 struct device_attribute *attr, char *buf)
684 {
685         struct regulator_dev *rdev = dev_get_drvdata(dev);
686
687         return regulator_print_opmode(buf,
688                 rdev->constraints->state_standby.mode);
689 }
690 static DEVICE_ATTR(suspend_standby_mode, 0444,
691                 regulator_suspend_standby_mode_show, NULL);
692
693 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
694                                    struct device_attribute *attr, char *buf)
695 {
696         struct regulator_dev *rdev = dev_get_drvdata(dev);
697
698         return regulator_print_state(buf,
699                         rdev->constraints->state_mem.enabled);
700 }
701 static DEVICE_ATTR(suspend_mem_state, 0444,
702                 regulator_suspend_mem_state_show, NULL);
703
704 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
705                                    struct device_attribute *attr, char *buf)
706 {
707         struct regulator_dev *rdev = dev_get_drvdata(dev);
708
709         return regulator_print_state(buf,
710                         rdev->constraints->state_disk.enabled);
711 }
712 static DEVICE_ATTR(suspend_disk_state, 0444,
713                 regulator_suspend_disk_state_show, NULL);
714
715 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
716                                    struct device_attribute *attr, char *buf)
717 {
718         struct regulator_dev *rdev = dev_get_drvdata(dev);
719
720         return regulator_print_state(buf,
721                         rdev->constraints->state_standby.enabled);
722 }
723 static DEVICE_ATTR(suspend_standby_state, 0444,
724                 regulator_suspend_standby_state_show, NULL);
725
726
727 /*
728  * These are the only attributes are present for all regulators.
729  * Other attributes are a function of regulator functionality.
730  */
731 static struct device_attribute regulator_dev_attrs[] = {
732         __ATTR(name, 0444, regulator_name_show, NULL),
733         __ATTR(num_users, 0444, regulator_num_users_show, NULL),
734         __ATTR(type, 0444, regulator_type_show, NULL),
735         __ATTR_NULL,
736 };
737
738 static void regulator_dev_release(struct device *dev)
739 {
740         struct regulator_dev *rdev = dev_get_drvdata(dev);
741         kfree(rdev);
742 }
743
744 static struct class regulator_class = {
745         .name = "regulator",
746         .dev_release = regulator_dev_release,
747         .dev_attrs = regulator_dev_attrs,
748 };
749
750 /* Calculate the new optimum regulator operating mode based on the new total
751  * consumer load. All locks held by caller */
752 static void drms_uA_update(struct regulator_dev *rdev)
753 {
754         struct regulator *sibling;
755         int current_uA = 0, output_uV, input_uV, err;
756         unsigned int mode;
757
758         err = regulator_check_drms(rdev);
759         if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
760             (!rdev->desc->ops->get_voltage &&
761              !rdev->desc->ops->get_voltage_sel) ||
762             !rdev->desc->ops->set_mode)
763                 return;
764
765         /* get output voltage */
766         output_uV = _regulator_get_voltage(rdev);
767         if (output_uV <= 0)
768                 return;
769
770         /* get input voltage */
771         input_uV = 0;
772         if (rdev->supply)
773                 input_uV = _regulator_get_voltage(rdev);
774         if (input_uV <= 0)
775                 input_uV = rdev->constraints->input_uV;
776         if (input_uV <= 0)
777                 return;
778
779         /* calc total requested load */
780         list_for_each_entry(sibling, &rdev->consumer_list, list)
781                 current_uA += sibling->uA_load;
782
783         /* now get the optimum mode for our new total regulator load */
784         mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
785                                                   output_uV, current_uA);
786
787         /* check the new mode is allowed */
788         err = regulator_mode_constrain(rdev, &mode);
789         if (err == 0)
790                 rdev->desc->ops->set_mode(rdev, mode);
791 }
792
793 static int suspend_set_state(struct regulator_dev *rdev,
794         struct regulator_state *rstate)
795 {
796         int ret = 0;
797         bool can_set_state;
798
799         can_set_state = rdev->desc->ops->set_suspend_enable &&
800                 rdev->desc->ops->set_suspend_disable;
801
802         /* If we have no suspend mode configration don't set anything;
803          * only warn if the driver actually makes the suspend mode
804          * configurable.
805          */
806         if (!rstate->enabled && !rstate->disabled) {
807                 if (can_set_state)
808                         rdev_warn(rdev, "No configuration\n");
809                 return 0;
810         }
811
812         if (rstate->enabled && rstate->disabled) {
813                 rdev_err(rdev, "invalid configuration\n");
814                 return -EINVAL;
815         }
816
817         if (!can_set_state) {
818                 rdev_err(rdev, "no way to set suspend state\n");
819                 return -EINVAL;
820         }
821
822         if (rstate->enabled)
823                 ret = rdev->desc->ops->set_suspend_enable(rdev);
824         else
825                 ret = rdev->desc->ops->set_suspend_disable(rdev);
826         if (ret < 0) {
827                 rdev_err(rdev, "failed to enabled/disable\n");
828                 return ret;
829         }
830
831         if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
832                 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
833                 if (ret < 0) {
834                         rdev_err(rdev, "failed to set voltage\n");
835                         return ret;
836                 }
837         }
838
839         if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
840                 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
841                 if (ret < 0) {
842                         rdev_err(rdev, "failed to set mode\n");
843                         return ret;
844                 }
845         }
846         return ret;
847 }
848
849 /* locks held by caller */
850 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
851 {
852         if (!rdev->constraints)
853                 return -EINVAL;
854
855         switch (state) {
856         case PM_SUSPEND_STANDBY:
857                 return suspend_set_state(rdev,
858                         &rdev->constraints->state_standby);
859         case PM_SUSPEND_MEM:
860                 return suspend_set_state(rdev,
861                         &rdev->constraints->state_mem);
862         case PM_SUSPEND_MAX:
863                 return suspend_set_state(rdev,
864                         &rdev->constraints->state_disk);
865         default:
866                 return -EINVAL;
867         }
868 }
869
870 static void print_constraints(struct regulator_dev *rdev)
871 {
872         struct regulation_constraints *constraints = rdev->constraints;
873         char buf[80] = "";
874         int count = 0;
875         int ret;
876
877         if (constraints->min_uV && constraints->max_uV) {
878                 if (constraints->min_uV == constraints->max_uV)
879                         count += sprintf(buf + count, "%d mV ",
880                                          constraints->min_uV / 1000);
881                 else
882                         count += sprintf(buf + count, "%d <--> %d mV ",
883                                          constraints->min_uV / 1000,
884                                          constraints->max_uV / 1000);
885         }
886
887         if (!constraints->min_uV ||
888             constraints->min_uV != constraints->max_uV) {
889                 ret = _regulator_get_voltage(rdev);
890                 if (ret > 0)
891                         count += sprintf(buf + count, "at %d mV ", ret / 1000);
892         }
893
894         if (constraints->uV_offset)
895                 count += sprintf(buf, "%dmV offset ",
896                                  constraints->uV_offset / 1000);
897
898         if (constraints->min_uA && constraints->max_uA) {
899                 if (constraints->min_uA == constraints->max_uA)
900                         count += sprintf(buf + count, "%d mA ",
901                                          constraints->min_uA / 1000);
902                 else
903                         count += sprintf(buf + count, "%d <--> %d mA ",
904                                          constraints->min_uA / 1000,
905                                          constraints->max_uA / 1000);
906         }
907
908         if (!constraints->min_uA ||
909             constraints->min_uA != constraints->max_uA) {
910                 ret = _regulator_get_current_limit(rdev);
911                 if (ret > 0)
912                         count += sprintf(buf + count, "at %d mA ", ret / 1000);
913         }
914
915         if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
916                 count += sprintf(buf + count, "fast ");
917         if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
918                 count += sprintf(buf + count, "normal ");
919         if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
920                 count += sprintf(buf + count, "idle ");
921         if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
922                 count += sprintf(buf + count, "standby");
923
924         rdev_info(rdev, "%s\n", buf);
925
926         if ((constraints->min_uV != constraints->max_uV) &&
927             !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
928                 rdev_warn(rdev,
929                           "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
930 }
931
932 static int machine_constraints_voltage(struct regulator_dev *rdev,
933         struct regulation_constraints *constraints)
934 {
935         struct regulator_ops *ops = rdev->desc->ops;
936         int ret;
937
938         /* do we need to apply the constraint voltage */
939         if (rdev->constraints->apply_uV &&
940             rdev->constraints->min_uV == rdev->constraints->max_uV) {
941                 ret = _regulator_do_set_voltage(rdev,
942                                                 rdev->constraints->min_uV,
943                                                 rdev->constraints->max_uV);
944                 if (ret < 0) {
945                         rdev_err(rdev, "failed to apply %duV constraint\n",
946                                  rdev->constraints->min_uV);
947                         return ret;
948                 }
949         }
950
951         /* constrain machine-level voltage specs to fit
952          * the actual range supported by this regulator.
953          */
954         if (ops->list_voltage && rdev->desc->n_voltages) {
955                 int     count = rdev->desc->n_voltages;
956                 int     i;
957                 int     min_uV = INT_MAX;
958                 int     max_uV = INT_MIN;
959                 int     cmin = constraints->min_uV;
960                 int     cmax = constraints->max_uV;
961
962                 /* it's safe to autoconfigure fixed-voltage supplies
963                    and the constraints are used by list_voltage. */
964                 if (count == 1 && !cmin) {
965                         cmin = 1;
966                         cmax = INT_MAX;
967                         constraints->min_uV = cmin;
968                         constraints->max_uV = cmax;
969                 }
970
971                 /* voltage constraints are optional */
972                 if ((cmin == 0) && (cmax == 0))
973                         return 0;
974
975                 /* else require explicit machine-level constraints */
976                 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
977                         rdev_err(rdev, "invalid voltage constraints\n");
978                         return -EINVAL;
979                 }
980
981                 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
982                 for (i = 0; i < count; i++) {
983                         int     value;
984
985                         value = ops->list_voltage(rdev, i);
986                         if (value <= 0)
987                                 continue;
988
989                         /* maybe adjust [min_uV..max_uV] */
990                         if (value >= cmin && value < min_uV)
991                                 min_uV = value;
992                         if (value <= cmax && value > max_uV)
993                                 max_uV = value;
994                 }
995
996                 /* final: [min_uV..max_uV] valid iff constraints valid */
997                 if (max_uV < min_uV) {
998                         rdev_err(rdev, "unsupportable voltage constraints\n");
999                         return -EINVAL;
1000                 }
1001
1002                 /* use regulator's subset of machine constraints */
1003                 if (constraints->min_uV < min_uV) {
1004                         rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1005                                  constraints->min_uV, min_uV);
1006                         constraints->min_uV = min_uV;
1007                 }
1008                 if (constraints->max_uV > max_uV) {
1009                         rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1010                                  constraints->max_uV, max_uV);
1011                         constraints->max_uV = max_uV;
1012                 }
1013         }
1014
1015         return 0;
1016 }
1017
1018 /**
1019  * set_machine_constraints - sets regulator constraints
1020  * @rdev: regulator source
1021  * @constraints: constraints to apply
1022  *
1023  * Allows platform initialisation code to define and constrain
1024  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
1025  * Constraints *must* be set by platform code in order for some
1026  * regulator operations to proceed i.e. set_voltage, set_current_limit,
1027  * set_mode.
1028  */
1029 static int set_machine_constraints(struct regulator_dev *rdev,
1030         const struct regulation_constraints *constraints)
1031 {
1032         int ret = 0;
1033         struct regulator_ops *ops = rdev->desc->ops;
1034
1035         if (constraints)
1036                 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1037                                             GFP_KERNEL);
1038         else
1039                 rdev->constraints = kzalloc(sizeof(*constraints),
1040                                             GFP_KERNEL);
1041         if (!rdev->constraints)
1042                 return -ENOMEM;
1043
1044         ret = machine_constraints_voltage(rdev, rdev->constraints);
1045         if (ret != 0)
1046                 goto out;
1047
1048         /* do we need to setup our suspend state */
1049         if (rdev->constraints->initial_state) {
1050                 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
1051                 if (ret < 0) {
1052                         rdev_err(rdev, "failed to set suspend state\n");
1053                         goto out;
1054                 }
1055         }
1056
1057         if (rdev->constraints->initial_mode) {
1058                 if (!ops->set_mode) {
1059                         rdev_err(rdev, "no set_mode operation\n");
1060                         ret = -EINVAL;
1061                         goto out;
1062                 }
1063
1064                 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1065                 if (ret < 0) {
1066                         rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1067                         goto out;
1068                 }
1069         }
1070
1071         /* If the constraints say the regulator should be on at this point
1072          * and we have control then make sure it is enabled.
1073          */
1074         if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
1075             ops->enable) {
1076                 ret = ops->enable(rdev);
1077                 if (ret < 0) {
1078                         rdev_err(rdev, "failed to enable\n");
1079                         goto out;
1080                 }
1081         }
1082
1083         if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
1084                 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1085                 if (ret < 0) {
1086                         rdev_err(rdev, "failed to set ramp_delay\n");
1087                         goto out;
1088                 }
1089         }
1090
1091         print_constraints(rdev);
1092         return 0;
1093 out:
1094         kfree(rdev->constraints);
1095         rdev->constraints = NULL;
1096         return ret;
1097 }
1098
1099 /**
1100  * set_supply - set regulator supply regulator
1101  * @rdev: regulator name
1102  * @supply_rdev: supply regulator name
1103  *
1104  * Called by platform initialisation code to set the supply regulator for this
1105  * regulator. This ensures that a regulators supply will also be enabled by the
1106  * core if it's child is enabled.
1107  */
1108 static int set_supply(struct regulator_dev *rdev,
1109                       struct regulator_dev *supply_rdev)
1110 {
1111         int err;
1112
1113         rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1114
1115         rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1116         if (rdev->supply == NULL) {
1117                 err = -ENOMEM;
1118                 return err;
1119         }
1120
1121         return 0;
1122 }
1123
1124 /**
1125  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1126  * @rdev:         regulator source
1127  * @consumer_dev_name: dev_name() string for device supply applies to
1128  * @supply:       symbolic name for supply
1129  *
1130  * Allows platform initialisation code to map physical regulator
1131  * sources to symbolic names for supplies for use by devices.  Devices
1132  * should use these symbolic names to request regulators, avoiding the
1133  * need to provide board-specific regulator names as platform data.
1134  */
1135 static int set_consumer_device_supply(struct regulator_dev *rdev,
1136                                       const char *consumer_dev_name,
1137                                       const char *supply)
1138 {
1139         struct regulator_map *node;
1140         int has_dev;
1141
1142         if (supply == NULL)
1143                 return -EINVAL;
1144
1145         if (consumer_dev_name != NULL)
1146                 has_dev = 1;
1147         else
1148                 has_dev = 0;
1149
1150         list_for_each_entry(node, &regulator_map_list, list) {
1151                 if (node->dev_name && consumer_dev_name) {
1152                         if (strcmp(node->dev_name, consumer_dev_name) != 0)
1153                                 continue;
1154                 } else if (node->dev_name || consumer_dev_name) {
1155                         continue;
1156                 }
1157
1158                 if (strcmp(node->supply, supply) != 0)
1159                         continue;
1160
1161                 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1162                          consumer_dev_name,
1163                          dev_name(&node->regulator->dev),
1164                          node->regulator->desc->name,
1165                          supply,
1166                          dev_name(&rdev->dev), rdev_get_name(rdev));
1167                 return -EBUSY;
1168         }
1169
1170         node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1171         if (node == NULL)
1172                 return -ENOMEM;
1173
1174         node->regulator = rdev;
1175         node->supply = supply;
1176
1177         if (has_dev) {
1178                 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1179                 if (node->dev_name == NULL) {
1180                         kfree(node);
1181                         return -ENOMEM;
1182                 }
1183         }
1184
1185         list_add(&node->list, &regulator_map_list);
1186         return 0;
1187 }
1188
1189 static void unset_regulator_supplies(struct regulator_dev *rdev)
1190 {
1191         struct regulator_map *node, *n;
1192
1193         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1194                 if (rdev == node->regulator) {
1195                         list_del(&node->list);
1196                         kfree(node->dev_name);
1197                         kfree(node);
1198                 }
1199         }
1200 }
1201
1202 #define REG_STR_SIZE    64
1203
1204 static struct regulator *create_regulator(struct regulator_dev *rdev,
1205                                           struct device *dev,
1206                                           const char *supply_name)
1207 {
1208         struct regulator *regulator;
1209         char buf[REG_STR_SIZE];
1210         int err, size;
1211
1212         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1213         if (regulator == NULL)
1214                 return NULL;
1215
1216         mutex_lock(&rdev->mutex);
1217         regulator->rdev = rdev;
1218         list_add(&regulator->list, &rdev->consumer_list);
1219
1220         if (dev) {
1221                 /* create a 'requested_microamps_name' sysfs entry */
1222                 size = scnprintf(buf, REG_STR_SIZE,
1223                                  "microamps_requested_%s-%s",
1224                                  dev_name(dev), supply_name);
1225                 if (size >= REG_STR_SIZE)
1226                         goto overflow_err;
1227
1228                 regulator->dev = dev;
1229                 sysfs_attr_init(&regulator->dev_attr.attr);
1230                 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1231                 if (regulator->dev_attr.attr.name == NULL)
1232                         goto attr_name_err;
1233
1234                 regulator->dev_attr.attr.mode = 0444;
1235                 regulator->dev_attr.show = device_requested_uA_show;
1236                 err = device_create_file(dev, &regulator->dev_attr);
1237                 if (err < 0) {
1238                         rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1239                         goto attr_name_err;
1240                 }
1241
1242                 /* also add a link to the device sysfs entry */
1243                 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1244                                  dev->kobj.name, supply_name);
1245                 if (size >= REG_STR_SIZE)
1246                         goto attr_err;
1247
1248                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1249                 if (regulator->supply_name == NULL)
1250                         goto attr_err;
1251
1252                 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1253                                         buf);
1254                 if (err) {
1255                         rdev_warn(rdev, "could not add device link %s err %d\n",
1256                                   dev->kobj.name, err);
1257                         goto link_name_err;
1258                 }
1259         } else {
1260                 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1261                 if (regulator->supply_name == NULL)
1262                         goto attr_err;
1263         }
1264
1265         regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1266                                                 rdev->debugfs);
1267         if (!regulator->debugfs) {
1268                 rdev_warn(rdev, "Failed to create debugfs directory\n");
1269         } else {
1270                 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1271                                    &regulator->uA_load);
1272                 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1273                                    &regulator->min_uV);
1274                 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1275                                    &regulator->max_uV);
1276         }
1277
1278         mutex_unlock(&rdev->mutex);
1279         return regulator;
1280 link_name_err:
1281         kfree(regulator->supply_name);
1282 attr_err:
1283         device_remove_file(regulator->dev, &regulator->dev_attr);
1284 attr_name_err:
1285         kfree(regulator->dev_attr.attr.name);
1286 overflow_err:
1287         list_del(&regulator->list);
1288         kfree(regulator);
1289         mutex_unlock(&rdev->mutex);
1290         return NULL;
1291 }
1292
1293 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1294 {
1295         if (!rdev->desc->ops->enable_time)
1296                 return 0;
1297         return rdev->desc->ops->enable_time(rdev);
1298 }
1299
1300 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1301                                                          const char *supply)
1302 {
1303         struct regulator_dev *r;
1304         struct device_node *node;
1305
1306         /* first do a dt based lookup */
1307         if (dev && dev->of_node) {
1308                 node = of_get_regulator(dev, supply);
1309                 if (node)
1310                         list_for_each_entry(r, &regulator_list, list)
1311                                 if (r->dev.parent &&
1312                                         node == r->dev.of_node)
1313                                         return r;
1314         }
1315
1316         /* if not found, try doing it non-dt way */
1317         list_for_each_entry(r, &regulator_list, list)
1318                 if (strcmp(rdev_get_name(r), supply) == 0)
1319                         return r;
1320
1321         return NULL;
1322 }
1323
1324 /* Internal regulator request function */
1325 static struct regulator *_regulator_get(struct device *dev, const char *id,
1326                                         int exclusive)
1327 {
1328         struct regulator_dev *rdev;
1329         struct regulator_map *map;
1330         struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1331         const char *devname = NULL;
1332         int ret;
1333
1334         if (id == NULL) {
1335                 pr_err("get() with no identifier\n");
1336                 return regulator;
1337         }
1338
1339         if (dev)
1340                 devname = dev_name(dev);
1341
1342         mutex_lock(&regulator_list_mutex);
1343
1344         rdev = regulator_dev_lookup(dev, id);
1345         if (rdev)
1346                 goto found;
1347
1348         list_for_each_entry(map, &regulator_map_list, list) {
1349                 /* If the mapping has a device set up it must match */
1350                 if (map->dev_name &&
1351                     (!devname || strcmp(map->dev_name, devname)))
1352                         continue;
1353
1354                 if (strcmp(map->supply, id) == 0) {
1355                         rdev = map->regulator;
1356                         goto found;
1357                 }
1358         }
1359
1360         if (board_wants_dummy_regulator) {
1361                 rdev = dummy_regulator_rdev;
1362                 goto found;
1363         }
1364
1365 #ifdef CONFIG_REGULATOR_DUMMY
1366         if (!devname)
1367                 devname = "deviceless";
1368
1369         /* If the board didn't flag that it was fully constrained then
1370          * substitute in a dummy regulator so consumers can continue.
1371          */
1372         if (!has_full_constraints) {
1373                 pr_warn("%s supply %s not found, using dummy regulator\n",
1374                         devname, id);
1375                 rdev = dummy_regulator_rdev;
1376                 goto found;
1377         }
1378 #endif
1379
1380         mutex_unlock(&regulator_list_mutex);
1381         return regulator;
1382
1383 found:
1384         if (rdev->exclusive) {
1385                 regulator = ERR_PTR(-EPERM);
1386                 goto out;
1387         }
1388
1389         if (exclusive && rdev->open_count) {
1390                 regulator = ERR_PTR(-EBUSY);
1391                 goto out;
1392         }
1393
1394         if (!try_module_get(rdev->owner))
1395                 goto out;
1396
1397         regulator = create_regulator(rdev, dev, id);
1398         if (regulator == NULL) {
1399                 regulator = ERR_PTR(-ENOMEM);
1400                 module_put(rdev->owner);
1401                 goto out;
1402         }
1403
1404         rdev->open_count++;
1405         if (exclusive) {
1406                 rdev->exclusive = 1;
1407
1408                 ret = _regulator_is_enabled(rdev);
1409                 if (ret > 0)
1410                         rdev->use_count = 1;
1411                 else
1412                         rdev->use_count = 0;
1413         }
1414
1415 out:
1416         mutex_unlock(&regulator_list_mutex);
1417
1418         return regulator;
1419 }
1420
1421 /**
1422  * regulator_get - lookup and obtain a reference to a regulator.
1423  * @dev: device for regulator "consumer"
1424  * @id: Supply name or regulator ID.
1425  *
1426  * Returns a struct regulator corresponding to the regulator producer,
1427  * or IS_ERR() condition containing errno.
1428  *
1429  * Use of supply names configured via regulator_set_device_supply() is
1430  * strongly encouraged.  It is recommended that the supply name used
1431  * should match the name used for the supply and/or the relevant
1432  * device pins in the datasheet.
1433  */
1434 struct regulator *regulator_get(struct device *dev, const char *id)
1435 {
1436         return _regulator_get(dev, id, 0);
1437 }
1438 EXPORT_SYMBOL_GPL(regulator_get);
1439
1440 static void devm_regulator_release(struct device *dev, void *res)
1441 {
1442         regulator_put(*(struct regulator **)res);
1443 }
1444
1445 /**
1446  * devm_regulator_get - Resource managed regulator_get()
1447  * @dev: device for regulator "consumer"
1448  * @id: Supply name or regulator ID.
1449  *
1450  * Managed regulator_get(). Regulators returned from this function are
1451  * automatically regulator_put() on driver detach. See regulator_get() for more
1452  * information.
1453  */
1454 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1455 {
1456         struct regulator **ptr, *regulator;
1457
1458         ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1459         if (!ptr)
1460                 return ERR_PTR(-ENOMEM);
1461
1462         regulator = regulator_get(dev, id);
1463         if (!IS_ERR(regulator)) {
1464                 *ptr = regulator;
1465                 devres_add(dev, ptr);
1466         } else {
1467                 devres_free(ptr);
1468         }
1469
1470         return regulator;
1471 }
1472 EXPORT_SYMBOL_GPL(devm_regulator_get);
1473
1474 /**
1475  * regulator_get_exclusive - obtain exclusive access to a regulator.
1476  * @dev: device for regulator "consumer"
1477  * @id: Supply name or regulator ID.
1478  *
1479  * Returns a struct regulator corresponding to the regulator producer,
1480  * or IS_ERR() condition containing errno.  Other consumers will be
1481  * unable to obtain this reference is held and the use count for the
1482  * regulator will be initialised to reflect the current state of the
1483  * regulator.
1484  *
1485  * This is intended for use by consumers which cannot tolerate shared
1486  * use of the regulator such as those which need to force the
1487  * regulator off for correct operation of the hardware they are
1488  * controlling.
1489  *
1490  * Use of supply names configured via regulator_set_device_supply() is
1491  * strongly encouraged.  It is recommended that the supply name used
1492  * should match the name used for the supply and/or the relevant
1493  * device pins in the datasheet.
1494  */
1495 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1496 {
1497         return _regulator_get(dev, id, 1);
1498 }
1499 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1500
1501 /**
1502  * regulator_put - "free" the regulator source
1503  * @regulator: regulator source
1504  *
1505  * Note: drivers must ensure that all regulator_enable calls made on this
1506  * regulator source are balanced by regulator_disable calls prior to calling
1507  * this function.
1508  */
1509 void regulator_put(struct regulator *regulator)
1510 {
1511         struct regulator_dev *rdev;
1512
1513         if (regulator == NULL || IS_ERR(regulator))
1514                 return;
1515
1516         mutex_lock(&regulator_list_mutex);
1517         rdev = regulator->rdev;
1518
1519         debugfs_remove_recursive(regulator->debugfs);
1520
1521         /* remove any sysfs entries */
1522         if (regulator->dev) {
1523                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1524                 device_remove_file(regulator->dev, &regulator->dev_attr);
1525                 kfree(regulator->dev_attr.attr.name);
1526         }
1527         kfree(regulator->supply_name);
1528         list_del(&regulator->list);
1529         kfree(regulator);
1530
1531         rdev->open_count--;
1532         rdev->exclusive = 0;
1533
1534         module_put(rdev->owner);
1535         mutex_unlock(&regulator_list_mutex);
1536 }
1537 EXPORT_SYMBOL_GPL(regulator_put);
1538
1539 static int devm_regulator_match(struct device *dev, void *res, void *data)
1540 {
1541         struct regulator **r = res;
1542         if (!r || !*r) {
1543                 WARN_ON(!r || !*r);
1544                 return 0;
1545         }
1546         return *r == data;
1547 }
1548
1549 /**
1550  * devm_regulator_put - Resource managed regulator_put()
1551  * @regulator: regulator to free
1552  *
1553  * Deallocate a regulator allocated with devm_regulator_get(). Normally
1554  * this function will not need to be called and the resource management
1555  * code will ensure that the resource is freed.
1556  */
1557 void devm_regulator_put(struct regulator *regulator)
1558 {
1559         int rc;
1560
1561         rc = devres_destroy(regulator->dev, devm_regulator_release,
1562                             devm_regulator_match, regulator);
1563         if (rc == 0)
1564                 regulator_put(regulator);
1565         else
1566                 WARN_ON(rc);
1567 }
1568 EXPORT_SYMBOL_GPL(devm_regulator_put);
1569
1570 static int _regulator_can_change_status(struct regulator_dev *rdev)
1571 {
1572         if (!rdev->constraints)
1573                 return 0;
1574
1575         if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1576                 return 1;
1577         else
1578                 return 0;
1579 }
1580
1581 /* locks held by regulator_enable() */
1582 static int _regulator_enable(struct regulator_dev *rdev)
1583 {
1584         int ret, delay;
1585
1586         /* check voltage and requested load before enabling */
1587         if (rdev->constraints &&
1588             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1589                 drms_uA_update(rdev);
1590
1591         if (rdev->use_count == 0) {
1592                 /* The regulator may on if it's not switchable or left on */
1593                 ret = _regulator_is_enabled(rdev);
1594                 if (ret == -EINVAL || ret == 0) {
1595                         if (!_regulator_can_change_status(rdev))
1596                                 return -EPERM;
1597
1598                         if (!rdev->desc->ops->enable)
1599                                 return -EINVAL;
1600
1601                         /* Query before enabling in case configuration
1602                          * dependent.  */
1603                         ret = _regulator_get_enable_time(rdev);
1604                         if (ret >= 0) {
1605                                 delay = ret;
1606                         } else {
1607                                 rdev_warn(rdev, "enable_time() failed: %d\n",
1608                                            ret);
1609                                 delay = 0;
1610                         }
1611
1612                         trace_regulator_enable(rdev_get_name(rdev));
1613                         _notifier_call_chain(
1614                                 rdev, REGULATOR_EVENT_PRE_ENABLE, NULL);
1615
1616                         /* Allow the regulator to ramp; it would be useful
1617                          * to extend this for bulk operations so that the
1618                          * regulators can ramp together.  */
1619                         ret = rdev->desc->ops->enable(rdev);
1620                         if (ret < 0)
1621                                 return ret;
1622
1623                         trace_regulator_enable_delay(rdev_get_name(rdev));
1624
1625                         if (delay >= 1000) {
1626                                 mdelay(delay / 1000);
1627                                 udelay(delay % 1000);
1628                         } else if (delay) {
1629                                 udelay(delay);
1630                         }
1631
1632                         _notifier_call_chain(
1633                                 rdev, REGULATOR_EVENT_POST_ENABLE, NULL);
1634                         trace_regulator_enable_complete(rdev_get_name(rdev));
1635
1636                 } else if (ret < 0) {
1637                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1638                         return ret;
1639                 }
1640                 /* Fallthrough on positive return values - already enabled */
1641         }
1642
1643         rdev->use_count++;
1644
1645         return 0;
1646 }
1647
1648 /**
1649  * regulator_enable - enable regulator output
1650  * @regulator: regulator source
1651  *
1652  * Request that the regulator be enabled with the regulator output at
1653  * the predefined voltage or current value.  Calls to regulator_enable()
1654  * must be balanced with calls to regulator_disable().
1655  *
1656  * NOTE: the output value can be set by other drivers, boot loader or may be
1657  * hardwired in the regulator.
1658  */
1659 int regulator_enable(struct regulator *regulator)
1660 {
1661         struct regulator_dev *rdev = regulator->rdev;
1662         int ret = 0;
1663
1664         if (rdev->supply) {
1665                 ret = regulator_enable(rdev->supply);
1666                 if (ret != 0)
1667                         return ret;
1668         }
1669
1670         mutex_lock(&rdev->mutex);
1671         ret = _regulator_enable(rdev);
1672         mutex_unlock(&rdev->mutex);
1673
1674         if (ret != 0 && rdev->supply)
1675                 regulator_disable(rdev->supply);
1676
1677         return ret;
1678 }
1679 EXPORT_SYMBOL_GPL(regulator_enable);
1680
1681 /* locks held by regulator_disable() */
1682 static int _regulator_disable(struct regulator_dev *rdev)
1683 {
1684         int ret = 0;
1685
1686         if (WARN(rdev->use_count <= 0,
1687                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
1688                 return -EIO;
1689
1690         /* are we the last user and permitted to disable ? */
1691         if (rdev->use_count == 1 &&
1692             (rdev->constraints && !rdev->constraints->always_on)) {
1693
1694                 /* we are last user */
1695                 if (_regulator_can_change_status(rdev) &&
1696                     rdev->desc->ops->disable) {
1697                         trace_regulator_disable(rdev_get_name(rdev));
1698
1699                         ret = rdev->desc->ops->disable(rdev);
1700                         if (ret < 0) {
1701                                 rdev_err(rdev, "failed to disable\n");
1702                                 return ret;
1703                         }
1704
1705                         trace_regulator_disable_complete(rdev_get_name(rdev));
1706
1707                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1708                                              NULL);
1709                 }
1710
1711                 rdev->use_count = 0;
1712         } else if (rdev->use_count > 1) {
1713
1714                 if (rdev->constraints &&
1715                         (rdev->constraints->valid_ops_mask &
1716                         REGULATOR_CHANGE_DRMS))
1717                         drms_uA_update(rdev);
1718
1719                 rdev->use_count--;
1720         }
1721
1722         return ret;
1723 }
1724
1725 /**
1726  * regulator_disable - disable regulator output
1727  * @regulator: regulator source
1728  *
1729  * Disable the regulator output voltage or current.  Calls to
1730  * regulator_enable() must be balanced with calls to
1731  * regulator_disable().
1732  *
1733  * NOTE: this will only disable the regulator output if no other consumer
1734  * devices have it enabled, the regulator device supports disabling and
1735  * machine constraints permit this operation.
1736  */
1737 int regulator_disable(struct regulator *regulator)
1738 {
1739         struct regulator_dev *rdev = regulator->rdev;
1740         int ret = 0;
1741
1742         mutex_lock(&rdev->mutex);
1743         ret = _regulator_disable(rdev);
1744         mutex_unlock(&rdev->mutex);
1745
1746         if (ret == 0 && rdev->supply)
1747                 regulator_disable(rdev->supply);
1748
1749         return ret;
1750 }
1751 EXPORT_SYMBOL_GPL(regulator_disable);
1752
1753 /* locks held by regulator_force_disable() */
1754 static int _regulator_force_disable(struct regulator_dev *rdev)
1755 {
1756         int ret = 0;
1757
1758         /* force disable */
1759         if (rdev->desc->ops->disable) {
1760                 /* ah well, who wants to live forever... */
1761                 ret = rdev->desc->ops->disable(rdev);
1762                 if (ret < 0) {
1763                         rdev_err(rdev, "failed to force disable\n");
1764                         return ret;
1765                 }
1766                 /* notify other consumers that power has been forced off */
1767                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1768                         REGULATOR_EVENT_DISABLE, NULL);
1769         }
1770
1771         return ret;
1772 }
1773
1774 /**
1775  * regulator_force_disable - force disable regulator output
1776  * @regulator: regulator source
1777  *
1778  * Forcibly disable the regulator output voltage or current.
1779  * NOTE: this *will* disable the regulator output even if other consumer
1780  * devices have it enabled. This should be used for situations when device
1781  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1782  */
1783 int regulator_force_disable(struct regulator *regulator)
1784 {
1785         struct regulator_dev *rdev = regulator->rdev;
1786         int ret;
1787
1788         mutex_lock(&rdev->mutex);
1789         regulator->uA_load = 0;
1790         ret = _regulator_force_disable(regulator->rdev);
1791         mutex_unlock(&rdev->mutex);
1792
1793         if (rdev->supply)
1794                 while (rdev->open_count--)
1795                         regulator_disable(rdev->supply);
1796
1797         return ret;
1798 }
1799 EXPORT_SYMBOL_GPL(regulator_force_disable);
1800
1801 static void regulator_disable_work(struct work_struct *work)
1802 {
1803         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1804                                                   disable_work.work);
1805         int count, i, ret;
1806
1807         mutex_lock(&rdev->mutex);
1808
1809         BUG_ON(!rdev->deferred_disables);
1810
1811         count = rdev->deferred_disables;
1812         rdev->deferred_disables = 0;
1813
1814         for (i = 0; i < count; i++) {
1815                 ret = _regulator_disable(rdev);
1816                 if (ret != 0)
1817                         rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1818         }
1819
1820         mutex_unlock(&rdev->mutex);
1821
1822         if (rdev->supply) {
1823                 for (i = 0; i < count; i++) {
1824                         ret = regulator_disable(rdev->supply);
1825                         if (ret != 0) {
1826                                 rdev_err(rdev,
1827                                          "Supply disable failed: %d\n", ret);
1828                         }
1829                 }
1830         }
1831 }
1832
1833 /**
1834  * regulator_disable_deferred - disable regulator output with delay
1835  * @regulator: regulator source
1836  * @ms: miliseconds until the regulator is disabled
1837  *
1838  * Execute regulator_disable() on the regulator after a delay.  This
1839  * is intended for use with devices that require some time to quiesce.
1840  *
1841  * NOTE: this will only disable the regulator output if no other consumer
1842  * devices have it enabled, the regulator device supports disabling and
1843  * machine constraints permit this operation.
1844  */
1845 int regulator_disable_deferred(struct regulator *regulator, int ms)
1846 {
1847         struct regulator_dev *rdev = regulator->rdev;
1848         int ret;
1849
1850         mutex_lock(&rdev->mutex);
1851         rdev->deferred_disables++;
1852         mutex_unlock(&rdev->mutex);
1853
1854         ret = schedule_delayed_work(&rdev->disable_work,
1855                                     msecs_to_jiffies(ms));
1856         if (ret < 0)
1857                 return ret;
1858         else
1859                 return 0;
1860 }
1861 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1862
1863 static int _regulator_is_enabled(struct regulator_dev *rdev)
1864 {
1865         /* If we don't know then assume that the regulator is always on */
1866         if (!rdev->desc->ops->is_enabled)
1867                 return 1;
1868
1869         return rdev->desc->ops->is_enabled(rdev);
1870 }
1871
1872 /**
1873  * regulator_is_enabled - is the regulator output enabled
1874  * @regulator: regulator source
1875  *
1876  * Returns positive if the regulator driver backing the source/client
1877  * has requested that the device be enabled, zero if it hasn't, else a
1878  * negative errno code.
1879  *
1880  * Note that the device backing this regulator handle can have multiple
1881  * users, so it might be enabled even if regulator_enable() was never
1882  * called for this particular source.
1883  */
1884 int regulator_is_enabled(struct regulator *regulator)
1885 {
1886         int ret;
1887
1888         mutex_lock(&regulator->rdev->mutex);
1889         ret = _regulator_is_enabled(regulator->rdev);
1890         mutex_unlock(&regulator->rdev->mutex);
1891
1892         return ret;
1893 }
1894 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1895
1896 /**
1897  * regulator_count_voltages - count regulator_list_voltage() selectors
1898  * @regulator: regulator source
1899  *
1900  * Returns number of selectors, or negative errno.  Selectors are
1901  * numbered starting at zero, and typically correspond to bitfields
1902  * in hardware registers.
1903  */
1904 int regulator_count_voltages(struct regulator *regulator)
1905 {
1906         struct regulator_dev    *rdev = regulator->rdev;
1907
1908         return rdev->desc->n_voltages ? : -EINVAL;
1909 }
1910 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1911
1912 /**
1913  * regulator_list_voltage - enumerate supported voltages
1914  * @regulator: regulator source
1915  * @selector: identify voltage to list
1916  * Context: can sleep
1917  *
1918  * Returns a voltage that can be passed to @regulator_set_voltage(),
1919  * zero if this selector code can't be used on this system, or a
1920  * negative errno.
1921  */
1922 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1923 {
1924         struct regulator_dev    *rdev = regulator->rdev;
1925         struct regulator_ops    *ops = rdev->desc->ops;
1926         int                     ret;
1927
1928         if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1929                 return -EINVAL;
1930
1931         mutex_lock(&rdev->mutex);
1932         ret = ops->list_voltage(rdev, selector);
1933         mutex_unlock(&rdev->mutex);
1934
1935         if (ret > 0) {
1936                 if (ret < rdev->constraints->min_uV)
1937                         ret = 0;
1938                 else if (ret > rdev->constraints->max_uV)
1939                         ret = 0;
1940         }
1941
1942         return ret;
1943 }
1944 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1945
1946 /**
1947  * regulator_is_supported_voltage - check if a voltage range can be supported
1948  *
1949  * @regulator: Regulator to check.
1950  * @min_uV: Minimum required voltage in uV.
1951  * @max_uV: Maximum required voltage in uV.
1952  *
1953  * Returns a boolean or a negative error code.
1954  */
1955 int regulator_is_supported_voltage(struct regulator *regulator,
1956                                    int min_uV, int max_uV)
1957 {
1958         int i, voltages, ret;
1959
1960         ret = regulator_count_voltages(regulator);
1961         if (ret < 0)
1962                 return ret;
1963         voltages = ret;
1964
1965         for (i = 0; i < voltages; i++) {
1966                 ret = regulator_list_voltage(regulator, i);
1967
1968                 if (ret >= min_uV && ret <= max_uV)
1969                         return 1;
1970         }
1971
1972         return 0;
1973 }
1974 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
1975
1976 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1977                                      int min_uV, int max_uV)
1978 {
1979         int ret;
1980         int delay = 0;
1981         unsigned int selector;
1982
1983         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1984
1985         min_uV += rdev->constraints->uV_offset;
1986         max_uV += rdev->constraints->uV_offset;
1987
1988         if (rdev->desc->ops->set_voltage) {
1989                 if (_regulator_is_enabled(rdev))
1990                         _notifier_call_chain(rdev,
1991                         REGULATOR_EVENT_OUT_PRECHANGE, (void *)min_uV);
1992
1993                 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1994                                                    &selector);
1995
1996                 if (rdev->desc->ops->list_voltage)
1997                         selector = rdev->desc->ops->list_voltage(rdev,
1998                                                                  selector);
1999                 else
2000                         selector = -1;
2001         } else if (rdev->desc->ops->set_voltage_sel) {
2002                 int best_val = INT_MAX;
2003                 int i;
2004
2005                 selector = 0;
2006
2007                 /* Find the smallest voltage that falls within the specified
2008                  * range.
2009                  */
2010                 for (i = 0; i < rdev->desc->n_voltages; i++) {
2011                         ret = rdev->desc->ops->list_voltage(rdev, i);
2012                         if (ret < 0)
2013                                 continue;
2014
2015                         if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2016                                 best_val = ret;
2017                                 selector = i;
2018                         }
2019                 }
2020
2021                 /*
2022                  * If we can't obtain the old selector there is not enough
2023                  * info to call set_voltage_time_sel().
2024                  */
2025                 if (rdev->desc->ops->set_voltage_time_sel &&
2026                     rdev->desc->ops->get_voltage_sel) {
2027                         unsigned int old_selector = 0;
2028
2029                         ret = rdev->desc->ops->get_voltage_sel(rdev);
2030                         if (ret < 0)
2031                                 return ret;
2032                         old_selector = ret;
2033                         ret = rdev->desc->ops->set_voltage_time_sel(rdev,
2034                                                 old_selector, selector);
2035                         if (ret < 0)
2036                                 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
2037                         else
2038                                 delay = ret;
2039                 }
2040
2041                 if (best_val != INT_MAX) {
2042                         if (_regulator_is_enabled(rdev))
2043                                 _notifier_call_chain(rdev,
2044                                 REGULATOR_EVENT_OUT_PRECHANGE, (void *)best_val);
2045
2046                         ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2047                         selector = best_val;
2048                 } else {
2049                         ret = -EINVAL;
2050                 }
2051         } else {
2052                 ret = -EINVAL;
2053         }
2054
2055         /* Insert any necessary delays */
2056         if (delay >= 1000) {
2057                 mdelay(delay / 1000);
2058                 udelay(delay % 1000);
2059         } else if (delay) {
2060                 udelay(delay);
2061         }
2062
2063         if (ret == 0)
2064                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2065                                      NULL);
2066
2067         if (_regulator_is_enabled(rdev)) {
2068                 if (selector != -1)
2069                         min_uV = selector;
2070                 _notifier_call_chain(rdev, REGULATOR_EVENT_OUT_POSTCHANGE,
2071                                      (void *)min_uV);
2072         }
2073
2074         trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
2075
2076         return ret;
2077 }
2078
2079 /**
2080  * regulator_set_voltage - set regulator output voltage
2081  * @regulator: regulator source
2082  * @min_uV: Minimum required voltage in uV
2083  * @max_uV: Maximum acceptable voltage in uV
2084  *
2085  * Sets a voltage regulator to the desired output voltage. This can be set
2086  * during any regulator state. IOW, regulator can be disabled or enabled.
2087  *
2088  * If the regulator is enabled then the voltage will change to the new value
2089  * immediately otherwise if the regulator is disabled the regulator will
2090  * output at the new voltage when enabled.
2091  *
2092  * NOTE: If the regulator is shared between several devices then the lowest
2093  * request voltage that meets the system constraints will be used.
2094  * Regulator system constraints must be set for this regulator before
2095  * calling this function otherwise this call will fail.
2096  */
2097 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2098 {
2099         struct regulator_dev *rdev = regulator->rdev;
2100         int ret = 0;
2101
2102         mutex_lock(&rdev->mutex);
2103
2104         /* If we're setting the same range as last time the change
2105          * should be a noop (some cpufreq implementations use the same
2106          * voltage for multiple frequencies, for example).
2107          */
2108         if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2109                 goto out;
2110
2111         /* sanity check */
2112         if (!rdev->desc->ops->set_voltage &&
2113             !rdev->desc->ops->set_voltage_sel) {
2114                 ret = -EINVAL;
2115                 goto out;
2116         }
2117
2118         /* constraints check */
2119         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2120         if (ret < 0)
2121                 goto out;
2122         regulator->min_uV = min_uV;
2123         regulator->max_uV = max_uV;
2124
2125         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2126         if (ret < 0)
2127                 goto out;
2128
2129         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2130
2131 out:
2132         mutex_unlock(&rdev->mutex);
2133         return ret;
2134 }
2135 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2136
2137 /**
2138  * regulator_set_voltage_time - get raise/fall time
2139  * @regulator: regulator source
2140  * @old_uV: starting voltage in microvolts
2141  * @new_uV: target voltage in microvolts
2142  *
2143  * Provided with the starting and ending voltage, this function attempts to
2144  * calculate the time in microseconds required to rise or fall to this new
2145  * voltage.
2146  */
2147 int regulator_set_voltage_time(struct regulator *regulator,
2148                                int old_uV, int new_uV)
2149 {
2150         struct regulator_dev    *rdev = regulator->rdev;
2151         struct regulator_ops    *ops = rdev->desc->ops;
2152         int old_sel = -1;
2153         int new_sel = -1;
2154         int voltage;
2155         int i;
2156
2157         /* Currently requires operations to do this */
2158         if (!ops->list_voltage || !ops->set_voltage_time_sel
2159             || !rdev->desc->n_voltages)
2160                 return -EINVAL;
2161
2162         for (i = 0; i < rdev->desc->n_voltages; i++) {
2163                 /* We only look for exact voltage matches here */
2164                 voltage = regulator_list_voltage(regulator, i);
2165                 if (voltage < 0)
2166                         return -EINVAL;
2167                 if (voltage == 0)
2168                         continue;
2169                 if (voltage == old_uV)
2170                         old_sel = i;
2171                 if (voltage == new_uV)
2172                         new_sel = i;
2173         }
2174
2175         if (old_sel < 0 || new_sel < 0)
2176                 return -EINVAL;
2177
2178         return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2179 }
2180 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2181
2182 /**
2183  * regulator_set_voltage_time_sel - get raise/fall time
2184  * @rdev: regulator source device
2185  * @old_selector: selector for starting voltage
2186  * @new_selector: selector for target voltage
2187  *
2188  * Provided with the starting and target voltage selectors, this function
2189  * returns time in microseconds required to rise or fall to this new voltage
2190  *
2191  * Drivers providing ramp_delay in regulation_constraints can use this as their
2192  * set_voltage_time_sel() operation.
2193  */
2194 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2195                                    unsigned int old_selector,
2196                                    unsigned int new_selector)
2197 {
2198         unsigned int ramp_delay = 0;
2199         int old_volt, new_volt;
2200
2201         if (rdev->constraints->ramp_delay)
2202                 ramp_delay = rdev->constraints->ramp_delay;
2203         else if (rdev->desc->ramp_delay)
2204                 ramp_delay = rdev->desc->ramp_delay;
2205
2206         if (ramp_delay == 0) {
2207                 rdev_warn(rdev, "ramp_delay not set\n");
2208                 return 0;
2209         }
2210
2211         /* sanity check */
2212         if (!rdev->desc->ops->list_voltage)
2213                 return -EINVAL;
2214
2215         old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2216         new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2217
2218         return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2219 }
2220 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2221
2222 /**
2223  * regulator_sync_voltage - re-apply last regulator output voltage
2224  * @regulator: regulator source
2225  *
2226  * Re-apply the last configured voltage.  This is intended to be used
2227  * where some external control source the consumer is cooperating with
2228  * has caused the configured voltage to change.
2229  */
2230 int regulator_sync_voltage(struct regulator *regulator)
2231 {
2232         struct regulator_dev *rdev = regulator->rdev;
2233         int ret, min_uV, max_uV;
2234
2235         mutex_lock(&rdev->mutex);
2236
2237         if (!rdev->desc->ops->set_voltage &&
2238             !rdev->desc->ops->set_voltage_sel) {
2239                 ret = -EINVAL;
2240                 goto out;
2241         }
2242
2243         /* This is only going to work if we've had a voltage configured. */
2244         if (!regulator->min_uV && !regulator->max_uV) {
2245                 ret = -EINVAL;
2246                 goto out;
2247         }
2248
2249         min_uV = regulator->min_uV;
2250         max_uV = regulator->max_uV;
2251
2252         /* This should be a paranoia check... */
2253         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2254         if (ret < 0)
2255                 goto out;
2256
2257         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2258         if (ret < 0)
2259                 goto out;
2260
2261         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2262
2263 out:
2264         mutex_unlock(&rdev->mutex);
2265         return ret;
2266 }
2267 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2268
2269 static int _regulator_get_voltage(struct regulator_dev *rdev)
2270 {
2271         int sel, ret;
2272
2273         if (rdev->desc->ops->get_voltage_sel) {
2274                 sel = rdev->desc->ops->get_voltage_sel(rdev);
2275                 if (sel < 0)
2276                         return sel;
2277                 ret = rdev->desc->ops->list_voltage(rdev, sel);
2278         } else if (rdev->desc->ops->get_voltage) {
2279                 ret = rdev->desc->ops->get_voltage(rdev);
2280         } else {
2281                 return -EINVAL;
2282         }
2283
2284         if (ret < 0)
2285                 return ret;
2286         return ret - rdev->constraints->uV_offset;
2287 }
2288
2289 /**
2290  * regulator_get_voltage - get regulator output voltage
2291  * @regulator: regulator source
2292  *
2293  * This returns the current regulator voltage in uV.
2294  *
2295  * NOTE: If the regulator is disabled it will return the voltage value. This
2296  * function should not be used to determine regulator state.
2297  */
2298 int regulator_get_voltage(struct regulator *regulator)
2299 {
2300         int ret;
2301
2302         mutex_lock(&regulator->rdev->mutex);
2303
2304         ret = _regulator_get_voltage(regulator->rdev);
2305
2306         mutex_unlock(&regulator->rdev->mutex);
2307
2308         return ret;
2309 }
2310 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2311
2312 /**
2313  * regulator_set_current_limit - set regulator output current limit
2314  * @regulator: regulator source
2315  * @min_uA: Minimuum supported current in uA
2316  * @max_uA: Maximum supported current in uA
2317  *
2318  * Sets current sink to the desired output current. This can be set during
2319  * any regulator state. IOW, regulator can be disabled or enabled.
2320  *
2321  * If the regulator is enabled then the current will change to the new value
2322  * immediately otherwise if the regulator is disabled the regulator will
2323  * output at the new current when enabled.
2324  *
2325  * NOTE: Regulator system constraints must be set for this regulator before
2326  * calling this function otherwise this call will fail.
2327  */
2328 int regulator_set_current_limit(struct regulator *regulator,
2329                                int min_uA, int max_uA)
2330 {
2331         struct regulator_dev *rdev = regulator->rdev;
2332         int ret;
2333
2334         mutex_lock(&rdev->mutex);
2335
2336         /* sanity check */
2337         if (!rdev->desc->ops->set_current_limit) {
2338                 ret = -EINVAL;
2339                 goto out;
2340         }
2341
2342         /* constraints check */
2343         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2344         if (ret < 0)
2345                 goto out;
2346
2347         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2348 out:
2349         mutex_unlock(&rdev->mutex);
2350         return ret;
2351 }
2352 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2353
2354 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2355 {
2356         int ret;
2357
2358         mutex_lock(&rdev->mutex);
2359
2360         /* sanity check */
2361         if (!rdev->desc->ops->get_current_limit) {
2362                 ret = -EINVAL;
2363                 goto out;
2364         }
2365
2366         ret = rdev->desc->ops->get_current_limit(rdev);
2367 out:
2368         mutex_unlock(&rdev->mutex);
2369         return ret;
2370 }
2371
2372 /**
2373  * regulator_get_current_limit - get regulator output current
2374  * @regulator: regulator source
2375  *
2376  * This returns the current supplied by the specified current sink in uA.
2377  *
2378  * NOTE: If the regulator is disabled it will return the current value. This
2379  * function should not be used to determine regulator state.
2380  */
2381 int regulator_get_current_limit(struct regulator *regulator)
2382 {
2383         return _regulator_get_current_limit(regulator->rdev);
2384 }
2385 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2386
2387 /**
2388  * regulator_set_mode - set regulator operating mode
2389  * @regulator: regulator source
2390  * @mode: operating mode - one of the REGULATOR_MODE constants
2391  *
2392  * Set regulator operating mode to increase regulator efficiency or improve
2393  * regulation performance.
2394  *
2395  * NOTE: Regulator system constraints must be set for this regulator before
2396  * calling this function otherwise this call will fail.
2397  */
2398 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2399 {
2400         struct regulator_dev *rdev = regulator->rdev;
2401         int ret;
2402         int regulator_curr_mode;
2403
2404         mutex_lock(&rdev->mutex);
2405
2406         /* sanity check */
2407         if (!rdev->desc->ops->set_mode) {
2408                 ret = -EINVAL;
2409                 goto out;
2410         }
2411
2412         /* return if the same mode is requested */
2413         if (rdev->desc->ops->get_mode) {
2414                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2415                 if (regulator_curr_mode == mode) {
2416                         ret = 0;
2417                         goto out;
2418                 }
2419         }
2420
2421         /* constraints check */
2422         ret = regulator_mode_constrain(rdev, &mode);
2423         if (ret < 0)
2424                 goto out;
2425
2426         ret = rdev->desc->ops->set_mode(rdev, mode);
2427 out:
2428         mutex_unlock(&rdev->mutex);
2429         return ret;
2430 }
2431 EXPORT_SYMBOL_GPL(regulator_set_mode);
2432
2433 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2434 {
2435         int ret;
2436
2437         mutex_lock(&rdev->mutex);
2438
2439         /* sanity check */
2440         if (!rdev->desc->ops->get_mode) {
2441                 ret = -EINVAL;
2442                 goto out;
2443         }
2444
2445         ret = rdev->desc->ops->get_mode(rdev);
2446 out:
2447         mutex_unlock(&rdev->mutex);
2448         return ret;
2449 }
2450
2451 /**
2452  * regulator_get_mode - get regulator operating mode
2453  * @regulator: regulator source
2454  *
2455  * Get the current regulator operating mode.
2456  */
2457 unsigned int regulator_get_mode(struct regulator *regulator)
2458 {
2459         return _regulator_get_mode(regulator->rdev);
2460 }
2461 EXPORT_SYMBOL_GPL(regulator_get_mode);
2462
2463 /**
2464  * regulator_set_optimum_mode - set regulator optimum operating mode
2465  * @regulator: regulator source
2466  * @uA_load: load current
2467  *
2468  * Notifies the regulator core of a new device load. This is then used by
2469  * DRMS (if enabled by constraints) to set the most efficient regulator
2470  * operating mode for the new regulator loading.
2471  *
2472  * Consumer devices notify their supply regulator of the maximum power
2473  * they will require (can be taken from device datasheet in the power
2474  * consumption tables) when they change operational status and hence power
2475  * state. Examples of operational state changes that can affect power
2476  * consumption are :-
2477  *
2478  *    o Device is opened / closed.
2479  *    o Device I/O is about to begin or has just finished.
2480  *    o Device is idling in between work.
2481  *
2482  * This information is also exported via sysfs to userspace.
2483  *
2484  * DRMS will sum the total requested load on the regulator and change
2485  * to the most efficient operating mode if platform constraints allow.
2486  *
2487  * Returns the new regulator mode or error.
2488  */
2489 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2490 {
2491         struct regulator_dev *rdev = regulator->rdev;
2492         struct regulator *consumer;
2493         int ret, output_uV, input_uV, total_uA_load = 0;
2494         unsigned int mode;
2495
2496         mutex_lock(&rdev->mutex);
2497
2498         /*
2499          * first check to see if we can set modes at all, otherwise just
2500          * tell the consumer everything is OK.
2501          */
2502         regulator->uA_load = uA_load;
2503         ret = regulator_check_drms(rdev);
2504         if (ret < 0) {
2505                 ret = 0;
2506                 goto out;
2507         }
2508
2509         if (!rdev->desc->ops->get_optimum_mode)
2510                 goto out;
2511
2512         /*
2513          * we can actually do this so any errors are indicators of
2514          * potential real failure.
2515          */
2516         ret = -EINVAL;
2517
2518         /* get output voltage */
2519         output_uV = _regulator_get_voltage(rdev);
2520         if (output_uV <= 0) {
2521                 rdev_err(rdev, "invalid output voltage found\n");
2522                 goto out;
2523         }
2524
2525         /* get input voltage */
2526         input_uV = 0;
2527         if (rdev->supply)
2528                 input_uV = regulator_get_voltage(rdev->supply);
2529         if (input_uV <= 0)
2530                 input_uV = rdev->constraints->input_uV;
2531         if (input_uV <= 0) {
2532                 rdev_err(rdev, "invalid input voltage found\n");
2533                 goto out;
2534         }
2535
2536         /* calc total requested load for this regulator */
2537         list_for_each_entry(consumer, &rdev->consumer_list, list)
2538                 total_uA_load += consumer->uA_load;
2539
2540         mode = rdev->desc->ops->get_optimum_mode(rdev,
2541                                                  input_uV, output_uV,
2542                                                  total_uA_load);
2543         ret = regulator_mode_constrain(rdev, &mode);
2544         if (ret < 0) {
2545                 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2546                          total_uA_load, input_uV, output_uV);
2547                 goto out;
2548         }
2549
2550         ret = rdev->desc->ops->set_mode(rdev, mode);
2551         if (ret < 0) {
2552                 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2553                 goto out;
2554         }
2555         ret = mode;
2556 out:
2557         mutex_unlock(&rdev->mutex);
2558         return ret;
2559 }
2560 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2561
2562 /**
2563  * regulator_set_control_mode - set regulator control mode
2564  * @regulator: regulator source
2565  * @mode: control mode - one of the REGULATOR_CONTROL_MODE constants
2566  *
2567  * Set regulator control mode to regulate the regulator output.
2568  *
2569  * NOTE: Regulator system constraints must be set for this regulator before
2570  * calling this function otherwise this call will fail.
2571  */
2572 int regulator_set_control_mode(struct regulator *regulator, unsigned int mode)
2573 {
2574         struct regulator_dev *rdev = regulator->rdev;
2575         int ret;
2576         int regulator_curr_mode;
2577
2578         mutex_lock(&rdev->mutex);
2579
2580         /* sanity check */
2581         if (!rdev->desc->ops->set_control_mode) {
2582                 ret = -EINVAL;
2583                 goto out;
2584         }
2585
2586         /* return if the same mode is requested */
2587         if (rdev->desc->ops->get_control_mode) {
2588                 regulator_curr_mode = rdev->desc->ops->get_control_mode(rdev);
2589                 if (regulator_curr_mode == mode) {
2590                         ret = 0;
2591                         goto out;
2592                 }
2593         }
2594
2595         /* constraints check */
2596         ret = regulator_check_control(rdev);
2597         if (ret < 0)
2598                 goto out;
2599
2600         ret = rdev->desc->ops->set_control_mode(rdev, mode);
2601 out:
2602         mutex_unlock(&rdev->mutex);
2603         return ret;
2604 }
2605 EXPORT_SYMBOL_GPL(regulator_set_control_mode);
2606
2607 /**
2608  * regulator_get_control_mode - get regulator control mode
2609  * @regulator: regulator source
2610  *
2611  * Get the current regulator control mode.
2612  */
2613 unsigned int regulator_get_control_mode(struct regulator *regulator)
2614 {
2615         struct regulator_dev *rdev = regulator->rdev;
2616         int ret = -EINVAL;
2617
2618         mutex_lock(&rdev->mutex);
2619
2620         /* sanity check */
2621         if (!rdev->desc->ops->get_control_mode)
2622                 goto out;
2623
2624         ret = rdev->desc->ops->get_control_mode(rdev);
2625 out:
2626         mutex_unlock(&rdev->mutex);
2627         return ret;
2628 }
2629 EXPORT_SYMBOL_GPL(regulator_get_control_mode);
2630
2631 /**
2632  * regulator_register_notifier - register regulator event notifier
2633  * @regulator: regulator source
2634  * @nb: notifier block
2635  *
2636  * Register notifier block to receive regulator events.
2637  */
2638 int regulator_register_notifier(struct regulator *regulator,
2639                               struct notifier_block *nb)
2640 {
2641         return blocking_notifier_chain_register(&regulator->rdev->notifier,
2642                                                 nb);
2643 }
2644 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2645
2646 /**
2647  * regulator_unregister_notifier - unregister regulator event notifier
2648  * @regulator: regulator source
2649  * @nb: notifier block
2650  *
2651  * Unregister regulator event notifier block.
2652  */
2653 int regulator_unregister_notifier(struct regulator *regulator,
2654                                 struct notifier_block *nb)
2655 {
2656         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2657                                                   nb);
2658 }
2659 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2660
2661 /* notify regulator consumers and downstream regulator consumers.
2662  * Note mutex must be held by caller.
2663  */
2664 static void _notifier_call_chain(struct regulator_dev *rdev,
2665                                   unsigned long event, void *data)
2666 {
2667         /* call rdev chain first */
2668         blocking_notifier_call_chain(&rdev->notifier, event, data);
2669 }
2670
2671 /**
2672  * regulator_bulk_get - get multiple regulator consumers
2673  *
2674  * @dev:           Device to supply
2675  * @num_consumers: Number of consumers to register
2676  * @consumers:     Configuration of consumers; clients are stored here.
2677  *
2678  * @return 0 on success, an errno on failure.
2679  *
2680  * This helper function allows drivers to get several regulator
2681  * consumers in one operation.  If any of the regulators cannot be
2682  * acquired then any regulators that were allocated will be freed
2683  * before returning to the caller.
2684  */
2685 int regulator_bulk_get(struct device *dev, int num_consumers,
2686                        struct regulator_bulk_data *consumers)
2687 {
2688         int i;
2689         int ret;
2690
2691         for (i = 0; i < num_consumers; i++)
2692                 consumers[i].consumer = NULL;
2693
2694         for (i = 0; i < num_consumers; i++) {
2695                 consumers[i].consumer = regulator_get(dev,
2696                                                       consumers[i].supply);
2697                 if (IS_ERR(consumers[i].consumer)) {
2698                         ret = PTR_ERR(consumers[i].consumer);
2699                         dev_err(dev, "Failed to get supply '%s': %d\n",
2700                                 consumers[i].supply, ret);
2701                         consumers[i].consumer = NULL;
2702                         goto err;
2703                 }
2704         }
2705
2706         return 0;
2707
2708 err:
2709         while (--i >= 0)
2710                 regulator_put(consumers[i].consumer);
2711
2712         return ret;
2713 }
2714 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2715
2716 /**
2717  * devm_regulator_bulk_get - managed get multiple regulator consumers
2718  *
2719  * @dev:           Device to supply
2720  * @num_consumers: Number of consumers to register
2721  * @consumers:     Configuration of consumers; clients are stored here.
2722  *
2723  * @return 0 on success, an errno on failure.
2724  *
2725  * This helper function allows drivers to get several regulator
2726  * consumers in one operation with management, the regulators will
2727  * automatically be freed when the device is unbound.  If any of the
2728  * regulators cannot be acquired then any regulators that were
2729  * allocated will be freed before returning to the caller.
2730  */
2731 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2732                             struct regulator_bulk_data *consumers)
2733 {
2734         int i;
2735         int ret;
2736
2737         for (i = 0; i < num_consumers; i++)
2738                 consumers[i].consumer = NULL;
2739
2740         for (i = 0; i < num_consumers; i++) {
2741                 consumers[i].consumer = devm_regulator_get(dev,
2742                                                            consumers[i].supply);
2743                 if (IS_ERR(consumers[i].consumer)) {
2744                         ret = PTR_ERR(consumers[i].consumer);
2745                         dev_err(dev, "Failed to get supply '%s': %d\n",
2746                                 consumers[i].supply, ret);
2747                         consumers[i].consumer = NULL;
2748                         goto err;
2749                 }
2750         }
2751
2752         return 0;
2753
2754 err:
2755         for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2756                 devm_regulator_put(consumers[i].consumer);
2757
2758         return ret;
2759 }
2760 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2761
2762 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2763 {
2764         struct regulator_bulk_data *bulk = data;
2765
2766         bulk->ret = regulator_enable(bulk->consumer);
2767 }
2768
2769 /**
2770  * regulator_bulk_enable - enable multiple regulator consumers
2771  *
2772  * @num_consumers: Number of consumers
2773  * @consumers:     Consumer data; clients are stored here.
2774  * @return         0 on success, an errno on failure
2775  *
2776  * This convenience API allows consumers to enable multiple regulator
2777  * clients in a single API call.  If any consumers cannot be enabled
2778  * then any others that were enabled will be disabled again prior to
2779  * return.
2780  */
2781 int regulator_bulk_enable(int num_consumers,
2782                           struct regulator_bulk_data *consumers)
2783 {
2784         LIST_HEAD(async_domain);
2785         int i;
2786         int ret = 0;
2787
2788         for (i = 0; i < num_consumers; i++)
2789                 async_schedule_domain(regulator_bulk_enable_async,
2790                                       &consumers[i], &async_domain);
2791
2792         async_synchronize_full_domain(&async_domain);
2793
2794         /* If any consumer failed we need to unwind any that succeeded */
2795         for (i = 0; i < num_consumers; i++) {
2796                 if (consumers[i].ret != 0) {
2797                         ret = consumers[i].ret;
2798                         goto err;
2799                 }
2800         }
2801
2802         return 0;
2803
2804 err:
2805         pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2806         while (--i >= 0)
2807                 regulator_disable(consumers[i].consumer);
2808
2809         return ret;
2810 }
2811 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2812
2813 /**
2814  * regulator_bulk_disable - disable multiple regulator consumers
2815  *
2816  * @num_consumers: Number of consumers
2817  * @consumers:     Consumer data; clients are stored here.
2818  * @return         0 on success, an errno on failure
2819  *
2820  * This convenience API allows consumers to disable multiple regulator
2821  * clients in a single API call.  If any consumers cannot be disabled
2822  * then any others that were disabled will be enabled again prior to
2823  * return.
2824  */
2825 int regulator_bulk_disable(int num_consumers,
2826                            struct regulator_bulk_data *consumers)
2827 {
2828         int i;
2829         int ret;
2830
2831         for (i = num_consumers - 1; i >= 0; --i) {
2832                 ret = regulator_disable(consumers[i].consumer);
2833                 if (ret != 0)
2834                         goto err;
2835         }
2836
2837         return 0;
2838
2839 err:
2840         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2841         for (++i; i < num_consumers; ++i)
2842                 regulator_enable(consumers[i].consumer);
2843
2844         return ret;
2845 }
2846 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2847
2848 /**
2849  * regulator_bulk_force_disable - force disable multiple regulator consumers
2850  *
2851  * @num_consumers: Number of consumers
2852  * @consumers:     Consumer data; clients are stored here.
2853  * @return         0 on success, an errno on failure
2854  *
2855  * This convenience API allows consumers to forcibly disable multiple regulator
2856  * clients in a single API call.
2857  * NOTE: This should be used for situations when device damage will
2858  * likely occur if the regulators are not disabled (e.g. over temp).
2859  * Although regulator_force_disable function call for some consumers can
2860  * return error numbers, the function is called for all consumers.
2861  */
2862 int regulator_bulk_force_disable(int num_consumers,
2863                            struct regulator_bulk_data *consumers)
2864 {
2865         int i;
2866         int ret;
2867
2868         for (i = 0; i < num_consumers; i++)
2869                 consumers[i].ret =
2870                             regulator_force_disable(consumers[i].consumer);
2871
2872         for (i = 0; i < num_consumers; i++) {
2873                 if (consumers[i].ret != 0) {
2874                         ret = consumers[i].ret;
2875                         goto out;
2876                 }
2877         }
2878
2879         return 0;
2880 out:
2881         return ret;
2882 }
2883 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2884
2885 /**
2886  * regulator_bulk_free - free multiple regulator consumers
2887  *
2888  * @num_consumers: Number of consumers
2889  * @consumers:     Consumer data; clients are stored here.
2890  *
2891  * This convenience API allows consumers to free multiple regulator
2892  * clients in a single API call.
2893  */
2894 void regulator_bulk_free(int num_consumers,
2895                          struct regulator_bulk_data *consumers)
2896 {
2897         int i;
2898
2899         for (i = 0; i < num_consumers; i++) {
2900                 regulator_put(consumers[i].consumer);
2901                 consumers[i].consumer = NULL;
2902         }
2903 }
2904 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2905
2906 /**
2907  * regulator_notifier_call_chain - call regulator event notifier
2908  * @rdev: regulator source
2909  * @event: notifier block
2910  * @data: callback-specific data.
2911  *
2912  * Called by regulator drivers to notify clients a regulator event has
2913  * occurred. We also notify regulator clients downstream.
2914  * Note lock must be held by caller.
2915  */
2916 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2917                                   unsigned long event, void *data)
2918 {
2919         _notifier_call_chain(rdev, event, data);
2920         return NOTIFY_DONE;
2921
2922 }
2923 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2924
2925 /**
2926  * regulator_mode_to_status - convert a regulator mode into a status
2927  *
2928  * @mode: Mode to convert
2929  *
2930  * Convert a regulator mode into a status.
2931  */
2932 int regulator_mode_to_status(unsigned int mode)
2933 {
2934         switch (mode) {
2935         case REGULATOR_MODE_FAST:
2936                 return REGULATOR_STATUS_FAST;
2937         case REGULATOR_MODE_NORMAL:
2938                 return REGULATOR_STATUS_NORMAL;
2939         case REGULATOR_MODE_IDLE:
2940                 return REGULATOR_STATUS_IDLE;
2941         case REGULATOR_STATUS_STANDBY:
2942                 return REGULATOR_STATUS_STANDBY;
2943         default:
2944                 return 0;
2945         }
2946 }
2947 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2948
2949 /*
2950  * To avoid cluttering sysfs (and memory) with useless state, only
2951  * create attributes that can be meaningfully displayed.
2952  */
2953 static int add_regulator_attributes(struct regulator_dev *rdev)
2954 {
2955         struct device           *dev = &rdev->dev;
2956         struct regulator_ops    *ops = rdev->desc->ops;
2957         int                     status = 0;
2958
2959         /* some attributes need specific methods to be displayed */
2960         if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2961             (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
2962                 status = device_create_file(dev, &dev_attr_microvolts);
2963                 if (status < 0)
2964                         return status;
2965         }
2966         if (ops->get_current_limit) {
2967                 status = device_create_file(dev, &dev_attr_microamps);
2968                 if (status < 0)
2969                         return status;
2970         }
2971         if (ops->get_mode) {
2972                 status = device_create_file(dev, &dev_attr_opmode);
2973                 if (status < 0)
2974                         return status;
2975         }
2976         if (ops->is_enabled) {
2977                 status = device_create_file(dev, &dev_attr_state);
2978                 if (status < 0)
2979                         return status;
2980         }
2981         if (ops->get_status) {
2982                 status = device_create_file(dev, &dev_attr_status);
2983                 if (status < 0)
2984                         return status;
2985         }
2986
2987         /* some attributes are type-specific */
2988         if (rdev->desc->type == REGULATOR_CURRENT) {
2989                 status = device_create_file(dev, &dev_attr_requested_microamps);
2990                 if (status < 0)
2991                         return status;
2992         }
2993
2994         /* all the other attributes exist to support constraints;
2995          * don't show them if there are no constraints, or if the
2996          * relevant supporting methods are missing.
2997          */
2998         if (!rdev->constraints)
2999                 return status;
3000
3001         /* constraints need specific supporting methods */
3002         if (ops->set_voltage || ops->set_voltage_sel) {
3003                 status = device_create_file(dev, &dev_attr_min_microvolts);
3004                 if (status < 0)
3005                         return status;
3006                 status = device_create_file(dev, &dev_attr_max_microvolts);
3007                 if (status < 0)
3008                         return status;
3009         }
3010         if (ops->set_current_limit) {
3011                 status = device_create_file(dev, &dev_attr_min_microamps);
3012                 if (status < 0)
3013                         return status;
3014                 status = device_create_file(dev, &dev_attr_max_microamps);
3015                 if (status < 0)
3016                         return status;
3017         }
3018
3019         /* suspend mode constraints need multiple supporting methods */
3020         if (!(ops->set_suspend_enable && ops->set_suspend_disable))
3021                 return status;
3022
3023         status = device_create_file(dev, &dev_attr_suspend_standby_state);
3024         if (status < 0)
3025                 return status;
3026         status = device_create_file(dev, &dev_attr_suspend_mem_state);
3027         if (status < 0)
3028                 return status;
3029         status = device_create_file(dev, &dev_attr_suspend_disk_state);
3030         if (status < 0)
3031                 return status;
3032
3033         if (ops->set_suspend_voltage) {
3034                 status = device_create_file(dev,
3035                                 &dev_attr_suspend_standby_microvolts);
3036                 if (status < 0)
3037                         return status;
3038                 status = device_create_file(dev,
3039                                 &dev_attr_suspend_mem_microvolts);
3040                 if (status < 0)
3041                         return status;
3042                 status = device_create_file(dev,
3043                                 &dev_attr_suspend_disk_microvolts);
3044                 if (status < 0)
3045                         return status;
3046         }
3047
3048         if (ops->set_suspend_mode) {
3049                 status = device_create_file(dev,
3050                                 &dev_attr_suspend_standby_mode);
3051                 if (status < 0)
3052                         return status;
3053                 status = device_create_file(dev,
3054                                 &dev_attr_suspend_mem_mode);
3055                 if (status < 0)
3056                         return status;
3057                 status = device_create_file(dev,
3058                                 &dev_attr_suspend_disk_mode);
3059                 if (status < 0)
3060                         return status;
3061         }
3062
3063         return status;
3064 }
3065
3066 static void rdev_init_debugfs(struct regulator_dev *rdev)
3067 {
3068         rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3069         if (!rdev->debugfs) {
3070                 rdev_warn(rdev, "Failed to create debugfs directory\n");
3071                 return;
3072         }
3073
3074         debugfs_create_u32("use_count", 0444, rdev->debugfs,
3075                            &rdev->use_count);
3076         debugfs_create_u32("open_count", 0444, rdev->debugfs,
3077                            &rdev->open_count);
3078 }
3079
3080 /**
3081  * regulator_register - register regulator
3082  * @regulator_desc: regulator to register
3083  * @dev: struct device for the regulator
3084  * @init_data: platform provided init data, passed through by driver
3085  * @driver_data: private regulator data
3086  * @of_node: OpenFirmware node to parse for device tree bindings (may be
3087  *           NULL).
3088  *
3089  * Called by regulator drivers to register a regulator.
3090  * Returns 0 on success.
3091  */
3092 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
3093         struct device *dev, const struct regulator_init_data *init_data,
3094         void *driver_data, struct device_node *of_node)
3095 {
3096         const struct regulation_constraints *constraints = NULL;
3097         static atomic_t regulator_no = ATOMIC_INIT(0);
3098         struct regulator_dev *rdev;
3099         int ret, i;
3100         const char *supply = NULL;
3101
3102         if (regulator_desc == NULL)
3103                 return ERR_PTR(-EINVAL);
3104
3105         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3106                 return ERR_PTR(-EINVAL);
3107
3108         if (regulator_desc->type != REGULATOR_VOLTAGE &&
3109             regulator_desc->type != REGULATOR_CURRENT)
3110                 return ERR_PTR(-EINVAL);
3111
3112         /* Only one of each should be implemented */
3113         WARN_ON(regulator_desc->ops->get_voltage &&
3114                 regulator_desc->ops->get_voltage_sel);
3115         WARN_ON(regulator_desc->ops->set_voltage &&
3116                 regulator_desc->ops->set_voltage_sel);
3117
3118         /* If we're using selectors we must implement list_voltage. */
3119         if (regulator_desc->ops->get_voltage_sel &&
3120             !regulator_desc->ops->list_voltage) {
3121                 return ERR_PTR(-EINVAL);
3122         }
3123         if (regulator_desc->ops->set_voltage_sel &&
3124             !regulator_desc->ops->list_voltage) {
3125                 return ERR_PTR(-EINVAL);
3126         }
3127
3128         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3129         if (rdev == NULL)
3130                 return ERR_PTR(-ENOMEM);
3131
3132         mutex_lock(&regulator_list_mutex);
3133
3134         mutex_init(&rdev->mutex);
3135         rdev->reg_data = driver_data;
3136         rdev->owner = regulator_desc->owner;
3137         rdev->desc = regulator_desc;
3138         INIT_LIST_HEAD(&rdev->consumer_list);
3139         INIT_LIST_HEAD(&rdev->list);
3140         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3141         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3142
3143         /* preform any regulator specific init */
3144         if (init_data && init_data->regulator_init) {
3145                 ret = init_data->regulator_init(rdev->reg_data);
3146                 if (ret < 0)
3147                         goto clean;
3148         }
3149
3150         /* register with sysfs */
3151         rdev->dev.class = &regulator_class;
3152         rdev->dev.of_node = of_node;
3153         rdev->dev.parent = dev;
3154         dev_set_name(&rdev->dev, "regulator.%d",
3155                      atomic_inc_return(&regulator_no) - 1);
3156         ret = device_register(&rdev->dev);
3157         if (ret != 0) {
3158                 put_device(&rdev->dev);
3159                 goto clean;
3160         }
3161
3162         dev_set_drvdata(&rdev->dev, rdev);
3163
3164         /* set regulator constraints */
3165         if (init_data)
3166                 constraints = &init_data->constraints;
3167
3168         ret = set_machine_constraints(rdev, constraints);
3169         if (ret < 0)
3170                 goto scrub;
3171
3172         /* add attributes supported by this regulator */
3173         ret = add_regulator_attributes(rdev);
3174         if (ret < 0)
3175                 goto scrub;
3176
3177         if (init_data && init_data->supply_regulator)
3178                 supply = init_data->supply_regulator;
3179 #if 0 /* Reenable when EPROBE_DEFER is pulled. */
3180         else if (regulator_desc->supply_name)
3181                 supply = regulator_desc->supply_name;
3182 #endif
3183
3184         if (supply) {
3185                 struct regulator_dev *r;
3186
3187                 r = regulator_dev_lookup(dev, supply);
3188                 if (!r) {
3189                         dev_err(dev, "Failed to find supply %s\n", supply);
3190                         ret = -EPROBE_DEFER;
3191                         goto scrub;
3192                 }
3193
3194                 ret = set_supply(rdev, r);
3195                 if (ret < 0)
3196                         goto scrub;
3197
3198                 /* Enable supply if rail is enabled */
3199                 if (rdev->desc->ops->is_enabled &&
3200                                 rdev->desc->ops->is_enabled(rdev)) {
3201                         ret = regulator_enable(rdev->supply);
3202                         if (ret < 0)
3203                                 goto scrub;
3204                 }
3205         }
3206
3207         /* add consumers devices */
3208         if (init_data) {
3209                 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3210                         ret = set_consumer_device_supply(rdev,
3211                                 init_data->consumer_supplies[i].dev_name,
3212                                 init_data->consumer_supplies[i].supply);
3213                         if (ret < 0) {
3214                                 dev_err(dev, "Failed to set supply %s\n",
3215                                         init_data->consumer_supplies[i].supply);
3216                                 goto unset_supplies;
3217                         }
3218                 }
3219         }
3220
3221         list_add(&rdev->list, &regulator_list);
3222
3223         rdev_init_debugfs(rdev);
3224 out:
3225         mutex_unlock(&regulator_list_mutex);
3226         return rdev;
3227
3228 unset_supplies:
3229         unset_regulator_supplies(rdev);
3230
3231 scrub:
3232         if (rdev->supply)
3233                 regulator_put(rdev->supply);
3234         kfree(rdev->constraints);
3235         device_unregister(&rdev->dev);
3236         /* device core frees rdev */
3237         rdev = ERR_PTR(ret);
3238         goto out;
3239
3240 clean:
3241         kfree(rdev);
3242         rdev = ERR_PTR(ret);
3243         goto out;
3244 }
3245 EXPORT_SYMBOL_GPL(regulator_register);
3246
3247 /**
3248  * regulator_unregister - unregister regulator
3249  * @rdev: regulator to unregister
3250  *
3251  * Called by regulator drivers to unregister a regulator.
3252  */
3253 void regulator_unregister(struct regulator_dev *rdev)
3254 {
3255         if (rdev == NULL)
3256                 return;
3257
3258         if (rdev->supply)
3259                 regulator_put(rdev->supply);
3260         mutex_lock(&regulator_list_mutex);
3261         debugfs_remove_recursive(rdev->debugfs);
3262         flush_work_sync(&rdev->disable_work.work);
3263         WARN_ON(rdev->open_count);
3264         unset_regulator_supplies(rdev);
3265         list_del(&rdev->list);
3266         kfree(rdev->constraints);
3267         device_unregister(&rdev->dev);
3268         mutex_unlock(&regulator_list_mutex);
3269 }
3270 EXPORT_SYMBOL_GPL(regulator_unregister);
3271
3272 /**
3273  * regulator_suspend_prepare - prepare regulators for system wide suspend
3274  * @state: system suspend state
3275  *
3276  * Configure each regulator with it's suspend operating parameters for state.
3277  * This will usually be called by machine suspend code prior to supending.
3278  */
3279 int regulator_suspend_prepare(suspend_state_t state)
3280 {
3281         struct regulator_dev *rdev;
3282         int ret = 0;
3283
3284         /* ON is handled by regulator active state */
3285         if (state == PM_SUSPEND_ON)
3286                 return -EINVAL;
3287
3288         mutex_lock(&regulator_list_mutex);
3289         list_for_each_entry(rdev, &regulator_list, list) {
3290
3291                 mutex_lock(&rdev->mutex);
3292                 ret = suspend_prepare(rdev, state);
3293                 mutex_unlock(&rdev->mutex);
3294
3295                 if (ret < 0) {
3296                         rdev_err(rdev, "failed to prepare\n");
3297                         goto out;
3298                 }
3299         }
3300 out:
3301         mutex_unlock(&regulator_list_mutex);
3302         return ret;
3303 }
3304 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3305
3306 /**
3307  * regulator_suspend_finish - resume regulators from system wide suspend
3308  *
3309  * Turn on regulators that might be turned off by regulator_suspend_prepare
3310  * and that should be turned on according to the regulators properties.
3311  */
3312 int regulator_suspend_finish(void)
3313 {
3314         struct regulator_dev *rdev;
3315         int ret = 0, error;
3316
3317         mutex_lock(&regulator_list_mutex);
3318         list_for_each_entry(rdev, &regulator_list, list) {
3319                 struct regulator_ops *ops = rdev->desc->ops;
3320
3321                 mutex_lock(&rdev->mutex);
3322                 if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
3323                                 ops->enable) {
3324                         error = ops->enable(rdev);
3325                         if (error)
3326                                 ret = error;
3327                 } else {
3328                         if (!has_full_constraints)
3329                                 goto unlock;
3330                         if (!ops->disable)
3331                                 goto unlock;
3332                         if (ops->is_enabled && !ops->is_enabled(rdev))
3333                                 goto unlock;
3334
3335                         error = ops->disable(rdev);
3336                         if (error)
3337                                 ret = error;
3338                 }
3339 unlock:
3340                 mutex_unlock(&rdev->mutex);
3341         }
3342         mutex_unlock(&regulator_list_mutex);
3343         return ret;
3344 }
3345 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3346
3347 /**
3348  * regulator_has_full_constraints - the system has fully specified constraints
3349  *
3350  * Calling this function will cause the regulator API to disable all
3351  * regulators which have a zero use count and don't have an always_on
3352  * constraint in a late_initcall.
3353  *
3354  * The intention is that this will become the default behaviour in a
3355  * future kernel release so users are encouraged to use this facility
3356  * now.
3357  */
3358 void regulator_has_full_constraints(void)
3359 {
3360         has_full_constraints = 1;
3361 }
3362 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3363
3364 /**
3365  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3366  *
3367  * Calling this function will cause the regulator API to provide a
3368  * dummy regulator to consumers if no physical regulator is found,
3369  * allowing most consumers to proceed as though a regulator were
3370  * configured.  This allows systems such as those with software
3371  * controllable regulators for the CPU core only to be brought up more
3372  * readily.
3373  */
3374 void regulator_use_dummy_regulator(void)
3375 {
3376         board_wants_dummy_regulator = true;
3377 }
3378 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3379
3380 /**
3381  * rdev_get_drvdata - get rdev regulator driver data
3382  * @rdev: regulator
3383  *
3384  * Get rdev regulator driver private data. This call can be used in the
3385  * regulator driver context.
3386  */
3387 void *rdev_get_drvdata(struct regulator_dev *rdev)
3388 {
3389         return rdev->reg_data;
3390 }
3391 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3392
3393 /**
3394  * regulator_get_drvdata - get regulator driver data
3395  * @regulator: regulator
3396  *
3397  * Get regulator driver private data. This call can be used in the consumer
3398  * driver context when non API regulator specific functions need to be called.
3399  */
3400 void *regulator_get_drvdata(struct regulator *regulator)
3401 {
3402         return regulator->rdev->reg_data;
3403 }
3404 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3405
3406 /**
3407  * regulator_set_drvdata - set regulator driver data
3408  * @regulator: regulator
3409  * @data: data
3410  */
3411 void regulator_set_drvdata(struct regulator *regulator, void *data)
3412 {
3413         regulator->rdev->reg_data = data;
3414 }
3415 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3416
3417 /**
3418  * regulator_get_id - get regulator ID
3419  * @rdev: regulator
3420  */
3421 int rdev_get_id(struct regulator_dev *rdev)
3422 {
3423         return rdev->desc->id;
3424 }
3425 EXPORT_SYMBOL_GPL(rdev_get_id);
3426
3427 struct device *rdev_get_dev(struct regulator_dev *rdev)
3428 {
3429         return &rdev->dev;
3430 }
3431 EXPORT_SYMBOL_GPL(rdev_get_dev);
3432
3433 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3434 {
3435         return reg_init_data->driver_data;
3436 }
3437 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3438
3439 #ifdef CONFIG_DEBUG_FS
3440 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3441                                     size_t count, loff_t *ppos)
3442 {
3443         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3444         ssize_t len, ret = 0;
3445         struct regulator_map *map;
3446
3447         if (!buf)
3448                 return -ENOMEM;
3449
3450         list_for_each_entry(map, &regulator_map_list, list) {
3451                 len = snprintf(buf + ret, PAGE_SIZE - ret,
3452                                "%s -> %s.%s\n",
3453                                rdev_get_name(map->regulator), map->dev_name,
3454                                map->supply);
3455                 if (len >= 0)
3456                         ret += len;
3457                 if (ret > PAGE_SIZE) {
3458                         ret = PAGE_SIZE;
3459                         break;
3460                 }
3461         }
3462
3463         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3464
3465         kfree(buf);
3466
3467         return ret;
3468 }
3469 #endif
3470
3471 static const struct file_operations supply_map_fops = {
3472 #ifdef CONFIG_DEBUG_FS
3473         .read = supply_map_read_file,
3474         .llseek = default_llseek,
3475 #endif
3476 };
3477
3478 static int __init regulator_init(void)
3479 {
3480         int ret;
3481
3482         ret = class_register(&regulator_class);
3483
3484         debugfs_root = debugfs_create_dir("regulator", NULL);
3485         if (!debugfs_root)
3486                 pr_warn("regulator: Failed to create debugfs directory\n");
3487
3488         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3489                             &supply_map_fops);
3490
3491         regulator_dummy_init();
3492
3493         return ret;
3494 }
3495
3496 /* init early to allow our consumers to complete system booting */
3497 core_initcall(regulator_init);
3498
3499 static int __init regulator_init_complete(void)
3500 {
3501         struct regulator_dev *rdev;
3502         struct regulator_ops *ops;
3503         struct regulation_constraints *c;
3504         int enabled, ret;
3505
3506         mutex_lock(&regulator_list_mutex);
3507
3508         /* If we have a full configuration then disable any regulators
3509          * which are not in use or always_on.  This will become the
3510          * default behaviour in the future.
3511          */
3512         list_for_each_entry(rdev, &regulator_list, list) {
3513                 ops = rdev->desc->ops;
3514                 c = rdev->constraints;
3515
3516                 if (!ops->disable || (c && c->always_on))
3517                         continue;
3518
3519                 mutex_lock(&rdev->mutex);
3520
3521                 if (rdev->use_count)
3522                         goto unlock;
3523
3524                 /* If we can't read the status assume it's on. */
3525                 if (ops->is_enabled)
3526                         enabled = ops->is_enabled(rdev);
3527                 else
3528                         enabled = 1;
3529
3530                 if (!enabled)
3531                         goto unlock;
3532
3533                 if (has_full_constraints) {
3534                         /* We log since this may kill the system if it
3535                          * goes wrong. */
3536                         rdev_info(rdev, "disabling\n");
3537                         ret = ops->disable(rdev);
3538                         if (ret != 0) {
3539                                 rdev_err(rdev, "couldn't disable: %d\n", ret);
3540                         }
3541                 } else {
3542                         /* The intention is that in future we will
3543                          * assume that full constraints are provided
3544                          * so warn even if we aren't going to do
3545                          * anything here.
3546                          */
3547                         rdev_warn(rdev, "incomplete constraints, leaving on\n");
3548                 }
3549
3550 unlock:
3551                 mutex_unlock(&rdev->mutex);
3552         }
3553
3554         mutex_unlock(&regulator_list_mutex);
3555
3556         return 0;
3557 }
3558
3559 #ifdef CONFIG_DEBUG_FS
3560 static int regulator_syncevent(struct file *file, const char __user *user_buf,
3561                                 size_t count, loff_t *ppos)
3562 {
3563         struct regulator_dev *rdev;
3564         char buffer[40];
3565         int buf_size;
3566
3567         memset(buffer, 0, sizeof(buffer));
3568         buf_size = min(count, (sizeof(buffer)-1));
3569
3570         if (copy_from_user(buffer, user_buf, buf_size))
3571                 return -EFAULT;
3572
3573         if (!strnicmp("all", buffer, 3)) {
3574
3575                 mutex_lock(&regulator_list_mutex);
3576
3577                 list_for_each_entry(rdev, &regulator_list, list) {
3578                         mutex_lock(&rdev->mutex);
3579
3580                         if (_regulator_is_enabled(rdev))
3581                                 trace_regulator_enable(rdev_get_name(rdev));
3582                         else
3583                                 trace_regulator_disable(rdev_get_name(rdev));
3584
3585                         trace_regulator_set_voltage(rdev_get_name(rdev),
3586                                 _regulator_get_voltage(rdev),
3587                                 _regulator_get_voltage(rdev));
3588
3589                         mutex_unlock(&rdev->mutex);
3590                 }
3591         }
3592
3593         mutex_unlock(&regulator_list_mutex);
3594
3595         return count;
3596 }
3597
3598 static const struct file_operations regulator_syncevent_fops = {
3599         .write          = regulator_syncevent,
3600 };
3601
3602 static int __init regulator_init_debugfs(void)
3603 {
3604         debugfs_create_file("syncevent_regulators", S_IWUSR, NULL, NULL,
3605                         &regulator_syncevent_fops);
3606
3607         return 0;
3608 }
3609
3610 late_initcall(regulator_init_debugfs);
3611 #endif
3612
3613 late_initcall(regulator_init_complete);