]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/regulator/core.c
57a806a0a8e4137002bae355d5fcde890753ad09
[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 (rdev->constraints->sleep_mode) {
1072                 if (!ops->set_sleep_mode) {
1073                         rdev_err(rdev, "no set_sleep_mode operation\n");
1074                         ret = -EINVAL;
1075                         goto out;
1076                 }
1077
1078                 ret = ops->set_sleep_mode(rdev, rdev->constraints->sleep_mode);
1079                 if (ret < 0) {
1080                         rdev_err(rdev, "failed to set sleep mode: %d\n", ret);
1081                         goto out;
1082                 }
1083         }
1084
1085         /* If the constraints say the regulator should be on at this point
1086          * and we have control then make sure it is enabled.
1087          */
1088         if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
1089             ops->enable) {
1090                 ret = ops->enable(rdev);
1091                 if (ret < 0) {
1092                         rdev_err(rdev, "failed to enable\n");
1093                         goto out;
1094                 }
1095         }
1096
1097         if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
1098                 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1099                 if (ret < 0) {
1100                         rdev_err(rdev, "failed to set ramp_delay\n");
1101                         goto out;
1102                 }
1103         }
1104
1105         print_constraints(rdev);
1106         return 0;
1107 out:
1108         kfree(rdev->constraints);
1109         rdev->constraints = NULL;
1110         return ret;
1111 }
1112
1113 /**
1114  * set_supply - set regulator supply regulator
1115  * @rdev: regulator name
1116  * @supply_rdev: supply regulator name
1117  *
1118  * Called by platform initialisation code to set the supply regulator for this
1119  * regulator. This ensures that a regulators supply will also be enabled by the
1120  * core if it's child is enabled.
1121  */
1122 static int set_supply(struct regulator_dev *rdev,
1123                       struct regulator_dev *supply_rdev)
1124 {
1125         int err;
1126
1127         rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1128
1129         rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1130         if (rdev->supply == NULL) {
1131                 err = -ENOMEM;
1132                 return err;
1133         }
1134
1135         return 0;
1136 }
1137
1138 /**
1139  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1140  * @rdev:         regulator source
1141  * @consumer_dev_name: dev_name() string for device supply applies to
1142  * @supply:       symbolic name for supply
1143  *
1144  * Allows platform initialisation code to map physical regulator
1145  * sources to symbolic names for supplies for use by devices.  Devices
1146  * should use these symbolic names to request regulators, avoiding the
1147  * need to provide board-specific regulator names as platform data.
1148  */
1149 static int set_consumer_device_supply(struct regulator_dev *rdev,
1150                                       const char *consumer_dev_name,
1151                                       const char *supply)
1152 {
1153         struct regulator_map *node;
1154         int has_dev;
1155
1156         if (supply == NULL)
1157                 return -EINVAL;
1158
1159         if (consumer_dev_name != NULL)
1160                 has_dev = 1;
1161         else
1162                 has_dev = 0;
1163
1164         list_for_each_entry(node, &regulator_map_list, list) {
1165                 if (node->dev_name && consumer_dev_name) {
1166                         if (strcmp(node->dev_name, consumer_dev_name) != 0)
1167                                 continue;
1168                 } else if (node->dev_name || consumer_dev_name) {
1169                         continue;
1170                 }
1171
1172                 if (strcmp(node->supply, supply) != 0)
1173                         continue;
1174
1175                 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1176                          consumer_dev_name,
1177                          dev_name(&node->regulator->dev),
1178                          node->regulator->desc->name,
1179                          supply,
1180                          dev_name(&rdev->dev), rdev_get_name(rdev));
1181                 return -EBUSY;
1182         }
1183
1184         node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1185         if (node == NULL)
1186                 return -ENOMEM;
1187
1188         node->regulator = rdev;
1189         node->supply = supply;
1190
1191         if (has_dev) {
1192                 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1193                 if (node->dev_name == NULL) {
1194                         kfree(node);
1195                         return -ENOMEM;
1196                 }
1197         }
1198
1199         list_add(&node->list, &regulator_map_list);
1200         return 0;
1201 }
1202
1203 static void unset_regulator_supplies(struct regulator_dev *rdev)
1204 {
1205         struct regulator_map *node, *n;
1206
1207         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1208                 if (rdev == node->regulator) {
1209                         list_del(&node->list);
1210                         kfree(node->dev_name);
1211                         kfree(node);
1212                 }
1213         }
1214 }
1215
1216 #define REG_STR_SIZE    64
1217
1218 static struct regulator *create_regulator(struct regulator_dev *rdev,
1219                                           struct device *dev,
1220                                           const char *supply_name)
1221 {
1222         struct regulator *regulator;
1223         char buf[REG_STR_SIZE];
1224         int err, size;
1225
1226         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1227         if (regulator == NULL)
1228                 return NULL;
1229
1230         mutex_lock(&rdev->mutex);
1231         regulator->rdev = rdev;
1232         list_add(&regulator->list, &rdev->consumer_list);
1233
1234         if (dev) {
1235                 /* create a 'requested_microamps_name' sysfs entry */
1236                 size = scnprintf(buf, REG_STR_SIZE,
1237                                  "microamps_requested_%s-%s",
1238                                  dev_name(dev), supply_name);
1239                 if (size >= REG_STR_SIZE)
1240                         goto overflow_err;
1241
1242                 regulator->dev = dev;
1243                 sysfs_attr_init(&regulator->dev_attr.attr);
1244                 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1245                 if (regulator->dev_attr.attr.name == NULL)
1246                         goto attr_name_err;
1247
1248                 regulator->dev_attr.attr.mode = 0444;
1249                 regulator->dev_attr.show = device_requested_uA_show;
1250                 err = device_create_file(dev, &regulator->dev_attr);
1251                 if (err < 0) {
1252                         rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1253                         goto attr_name_err;
1254                 }
1255
1256                 /* also add a link to the device sysfs entry */
1257                 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1258                                  dev->kobj.name, supply_name);
1259                 if (size >= REG_STR_SIZE)
1260                         goto attr_err;
1261
1262                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1263                 if (regulator->supply_name == NULL)
1264                         goto attr_err;
1265
1266                 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1267                                         buf);
1268                 if (err) {
1269                         rdev_warn(rdev, "could not add device link %s err %d\n",
1270                                   dev->kobj.name, err);
1271                         goto link_name_err;
1272                 }
1273         } else {
1274                 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1275                 if (regulator->supply_name == NULL)
1276                         goto attr_err;
1277         }
1278
1279         regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1280                                                 rdev->debugfs);
1281         if (!regulator->debugfs) {
1282                 rdev_warn(rdev, "Failed to create debugfs directory\n");
1283         } else {
1284                 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1285                                    &regulator->uA_load);
1286                 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1287                                    &regulator->min_uV);
1288                 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1289                                    &regulator->max_uV);
1290         }
1291
1292         mutex_unlock(&rdev->mutex);
1293         return regulator;
1294 link_name_err:
1295         kfree(regulator->supply_name);
1296 attr_err:
1297         device_remove_file(regulator->dev, &regulator->dev_attr);
1298 attr_name_err:
1299         kfree(regulator->dev_attr.attr.name);
1300 overflow_err:
1301         list_del(&regulator->list);
1302         kfree(regulator);
1303         mutex_unlock(&rdev->mutex);
1304         return NULL;
1305 }
1306
1307 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1308 {
1309         if (!rdev->desc->ops->enable_time)
1310                 return 0;
1311         return rdev->desc->ops->enable_time(rdev);
1312 }
1313
1314 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1315                                                          const char *supply)
1316 {
1317         struct regulator_dev *r;
1318         struct device_node *node;
1319
1320         /* first do a dt based lookup */
1321         if (dev && dev->of_node) {
1322                 node = of_get_regulator(dev, supply);
1323                 if (node)
1324                         list_for_each_entry(r, &regulator_list, list)
1325                                 if (r->dev.parent &&
1326                                         node == r->dev.of_node)
1327                                         return r;
1328         }
1329
1330         /* if not found, try doing it non-dt way */
1331         list_for_each_entry(r, &regulator_list, list)
1332                 if (strcmp(rdev_get_name(r), supply) == 0)
1333                         return r;
1334
1335         return NULL;
1336 }
1337
1338 /* Internal regulator request function */
1339 static struct regulator *_regulator_get(struct device *dev, const char *id,
1340                                         int exclusive)
1341 {
1342         struct regulator_dev *rdev;
1343         struct regulator_map *map;
1344         struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1345         const char *devname = NULL;
1346         int ret;
1347
1348         if (id == NULL) {
1349                 pr_err("get() with no identifier\n");
1350                 return regulator;
1351         }
1352
1353         if (dev)
1354                 devname = dev_name(dev);
1355
1356         mutex_lock(&regulator_list_mutex);
1357
1358         rdev = regulator_dev_lookup(dev, id);
1359         if (rdev)
1360                 goto found;
1361
1362         list_for_each_entry(map, &regulator_map_list, list) {
1363                 /* If the mapping has a device set up it must match */
1364                 if (map->dev_name &&
1365                     (!devname || strcmp(map->dev_name, devname)))
1366                         continue;
1367
1368                 if (strcmp(map->supply, id) == 0) {
1369                         rdev = map->regulator;
1370                         goto found;
1371                 }
1372         }
1373
1374         if (board_wants_dummy_regulator) {
1375                 rdev = dummy_regulator_rdev;
1376                 goto found;
1377         }
1378
1379 #ifdef CONFIG_REGULATOR_DUMMY
1380         if (!devname)
1381                 devname = "deviceless";
1382
1383         /* If the board didn't flag that it was fully constrained then
1384          * substitute in a dummy regulator so consumers can continue.
1385          */
1386         if (!has_full_constraints) {
1387                 pr_warn("%s supply %s not found, using dummy regulator\n",
1388                         devname, id);
1389                 rdev = dummy_regulator_rdev;
1390                 goto found;
1391         }
1392 #endif
1393
1394         mutex_unlock(&regulator_list_mutex);
1395         return regulator;
1396
1397 found:
1398         if (rdev->exclusive) {
1399                 regulator = ERR_PTR(-EPERM);
1400                 goto out;
1401         }
1402
1403         if (exclusive && rdev->open_count) {
1404                 regulator = ERR_PTR(-EBUSY);
1405                 goto out;
1406         }
1407
1408         if (!try_module_get(rdev->owner))
1409                 goto out;
1410
1411         regulator = create_regulator(rdev, dev, id);
1412         if (regulator == NULL) {
1413                 regulator = ERR_PTR(-ENOMEM);
1414                 module_put(rdev->owner);
1415                 goto out;
1416         }
1417
1418         rdev->open_count++;
1419         if (exclusive) {
1420                 rdev->exclusive = 1;
1421
1422                 ret = _regulator_is_enabled(rdev);
1423                 if (ret > 0)
1424                         rdev->use_count = 1;
1425                 else
1426                         rdev->use_count = 0;
1427         }
1428
1429 out:
1430         mutex_unlock(&regulator_list_mutex);
1431
1432         return regulator;
1433 }
1434
1435 /**
1436  * regulator_get - lookup and obtain a reference to a regulator.
1437  * @dev: device for regulator "consumer"
1438  * @id: Supply name or regulator ID.
1439  *
1440  * Returns a struct regulator corresponding to the regulator producer,
1441  * or IS_ERR() condition containing errno.
1442  *
1443  * Use of supply names configured via regulator_set_device_supply() is
1444  * strongly encouraged.  It is recommended that the supply name used
1445  * should match the name used for the supply and/or the relevant
1446  * device pins in the datasheet.
1447  */
1448 struct regulator *regulator_get(struct device *dev, const char *id)
1449 {
1450         return _regulator_get(dev, id, 0);
1451 }
1452 EXPORT_SYMBOL_GPL(regulator_get);
1453
1454 static void devm_regulator_release(struct device *dev, void *res)
1455 {
1456         regulator_put(*(struct regulator **)res);
1457 }
1458
1459 /**
1460  * devm_regulator_get - Resource managed regulator_get()
1461  * @dev: device for regulator "consumer"
1462  * @id: Supply name or regulator ID.
1463  *
1464  * Managed regulator_get(). Regulators returned from this function are
1465  * automatically regulator_put() on driver detach. See regulator_get() for more
1466  * information.
1467  */
1468 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1469 {
1470         struct regulator **ptr, *regulator;
1471
1472         ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1473         if (!ptr)
1474                 return ERR_PTR(-ENOMEM);
1475
1476         regulator = regulator_get(dev, id);
1477         if (!IS_ERR(regulator)) {
1478                 *ptr = regulator;
1479                 devres_add(dev, ptr);
1480         } else {
1481                 devres_free(ptr);
1482         }
1483
1484         return regulator;
1485 }
1486 EXPORT_SYMBOL_GPL(devm_regulator_get);
1487
1488 /**
1489  * regulator_get_exclusive - obtain exclusive access to a regulator.
1490  * @dev: device for regulator "consumer"
1491  * @id: Supply name or regulator ID.
1492  *
1493  * Returns a struct regulator corresponding to the regulator producer,
1494  * or IS_ERR() condition containing errno.  Other consumers will be
1495  * unable to obtain this reference is held and the use count for the
1496  * regulator will be initialised to reflect the current state of the
1497  * regulator.
1498  *
1499  * This is intended for use by consumers which cannot tolerate shared
1500  * use of the regulator such as those which need to force the
1501  * regulator off for correct operation of the hardware they are
1502  * controlling.
1503  *
1504  * Use of supply names configured via regulator_set_device_supply() is
1505  * strongly encouraged.  It is recommended that the supply name used
1506  * should match the name used for the supply and/or the relevant
1507  * device pins in the datasheet.
1508  */
1509 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1510 {
1511         return _regulator_get(dev, id, 1);
1512 }
1513 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1514
1515 /**
1516  * regulator_put - "free" the regulator source
1517  * @regulator: regulator source
1518  *
1519  * Note: drivers must ensure that all regulator_enable calls made on this
1520  * regulator source are balanced by regulator_disable calls prior to calling
1521  * this function.
1522  */
1523 void regulator_put(struct regulator *regulator)
1524 {
1525         struct regulator_dev *rdev;
1526
1527         if (regulator == NULL || IS_ERR(regulator))
1528                 return;
1529
1530         mutex_lock(&regulator_list_mutex);
1531         rdev = regulator->rdev;
1532
1533         debugfs_remove_recursive(regulator->debugfs);
1534
1535         /* remove any sysfs entries */
1536         if (regulator->dev) {
1537                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1538                 device_remove_file(regulator->dev, &regulator->dev_attr);
1539                 kfree(regulator->dev_attr.attr.name);
1540         }
1541         kfree(regulator->supply_name);
1542         list_del(&regulator->list);
1543         kfree(regulator);
1544
1545         rdev->open_count--;
1546         rdev->exclusive = 0;
1547
1548         module_put(rdev->owner);
1549         mutex_unlock(&regulator_list_mutex);
1550 }
1551 EXPORT_SYMBOL_GPL(regulator_put);
1552
1553 static int devm_regulator_match(struct device *dev, void *res, void *data)
1554 {
1555         struct regulator **r = res;
1556         if (!r || !*r) {
1557                 WARN_ON(!r || !*r);
1558                 return 0;
1559         }
1560         return *r == data;
1561 }
1562
1563 /**
1564  * devm_regulator_put - Resource managed regulator_put()
1565  * @regulator: regulator to free
1566  *
1567  * Deallocate a regulator allocated with devm_regulator_get(). Normally
1568  * this function will not need to be called and the resource management
1569  * code will ensure that the resource is freed.
1570  */
1571 void devm_regulator_put(struct regulator *regulator)
1572 {
1573         int rc;
1574
1575         rc = devres_destroy(regulator->dev, devm_regulator_release,
1576                             devm_regulator_match, regulator);
1577         if (rc == 0)
1578                 regulator_put(regulator);
1579         else
1580                 WARN_ON(rc);
1581 }
1582 EXPORT_SYMBOL_GPL(devm_regulator_put);
1583
1584 static int _regulator_can_change_status(struct regulator_dev *rdev)
1585 {
1586         if (!rdev->constraints)
1587                 return 0;
1588
1589         if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1590                 return 1;
1591         else
1592                 return 0;
1593 }
1594
1595 /* locks held by regulator_enable() */
1596 static int _regulator_enable(struct regulator_dev *rdev)
1597 {
1598         int ret, delay;
1599
1600         /* check voltage and requested load before enabling */
1601         if (rdev->constraints &&
1602             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1603                 drms_uA_update(rdev);
1604
1605         if (rdev->use_count == 0) {
1606                 /* The regulator may on if it's not switchable or left on */
1607                 ret = _regulator_is_enabled(rdev);
1608                 if (ret == -EINVAL || ret == 0) {
1609                         if (!_regulator_can_change_status(rdev))
1610                                 return -EPERM;
1611
1612                         if (!rdev->desc->ops->enable)
1613                                 return -EINVAL;
1614
1615                         /* Query before enabling in case configuration
1616                          * dependent.  */
1617                         ret = _regulator_get_enable_time(rdev);
1618                         if (ret >= 0) {
1619                                 delay = ret;
1620                         } else {
1621                                 rdev_warn(rdev, "enable_time() failed: %d\n",
1622                                            ret);
1623                                 delay = 0;
1624                         }
1625
1626                         trace_regulator_enable(rdev_get_name(rdev));
1627                         _notifier_call_chain(
1628                                 rdev, REGULATOR_EVENT_PRE_ENABLE, NULL);
1629
1630                         /* Allow the regulator to ramp; it would be useful
1631                          * to extend this for bulk operations so that the
1632                          * regulators can ramp together.  */
1633                         ret = rdev->desc->ops->enable(rdev);
1634                         if (ret < 0)
1635                                 return ret;
1636
1637                         trace_regulator_enable_delay(rdev_get_name(rdev));
1638
1639                         if (delay >= 1000) {
1640                                 mdelay(delay / 1000);
1641                                 udelay(delay % 1000);
1642                         } else if (delay) {
1643                                 udelay(delay);
1644                         }
1645
1646                         _notifier_call_chain(
1647                                 rdev, REGULATOR_EVENT_POST_ENABLE, NULL);
1648                         trace_regulator_enable_complete(rdev_get_name(rdev));
1649
1650                 } else if (ret < 0) {
1651                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1652                         return ret;
1653                 }
1654                 /* Fallthrough on positive return values - already enabled */
1655         }
1656
1657         rdev->use_count++;
1658
1659         return 0;
1660 }
1661
1662 /**
1663  * regulator_enable - enable regulator output
1664  * @regulator: regulator source
1665  *
1666  * Request that the regulator be enabled with the regulator output at
1667  * the predefined voltage or current value.  Calls to regulator_enable()
1668  * must be balanced with calls to regulator_disable().
1669  *
1670  * NOTE: the output value can be set by other drivers, boot loader or may be
1671  * hardwired in the regulator.
1672  */
1673 int regulator_enable(struct regulator *regulator)
1674 {
1675         struct regulator_dev *rdev = regulator->rdev;
1676         int ret = 0;
1677
1678         if (rdev->supply) {
1679                 ret = regulator_enable(rdev->supply);
1680                 if (ret != 0)
1681                         return ret;
1682         }
1683
1684         mutex_lock(&rdev->mutex);
1685         ret = _regulator_enable(rdev);
1686         mutex_unlock(&rdev->mutex);
1687
1688         if (ret != 0 && rdev->supply)
1689                 regulator_disable(rdev->supply);
1690
1691         return ret;
1692 }
1693 EXPORT_SYMBOL_GPL(regulator_enable);
1694
1695 /* locks held by regulator_disable() */
1696 static int _regulator_disable(struct regulator_dev *rdev)
1697 {
1698         int ret = 0;
1699
1700         if (WARN(rdev->use_count <= 0,
1701                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
1702                 return -EIO;
1703
1704         /* are we the last user and permitted to disable ? */
1705         if (rdev->use_count == 1 &&
1706             (rdev->constraints && !rdev->constraints->always_on)) {
1707
1708                 /* we are last user */
1709                 if (_regulator_can_change_status(rdev) &&
1710                     rdev->desc->ops->disable) {
1711                         trace_regulator_disable(rdev_get_name(rdev));
1712
1713                         ret = rdev->desc->ops->disable(rdev);
1714                         if (ret < 0) {
1715                                 rdev_err(rdev, "failed to disable\n");
1716                                 return ret;
1717                         }
1718
1719                         trace_regulator_disable_complete(rdev_get_name(rdev));
1720
1721                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1722                                              NULL);
1723                 }
1724
1725                 rdev->use_count = 0;
1726         } else if (rdev->use_count > 1) {
1727
1728                 if (rdev->constraints &&
1729                         (rdev->constraints->valid_ops_mask &
1730                         REGULATOR_CHANGE_DRMS))
1731                         drms_uA_update(rdev);
1732
1733                 rdev->use_count--;
1734         }
1735
1736         return ret;
1737 }
1738
1739 /**
1740  * regulator_disable - disable regulator output
1741  * @regulator: regulator source
1742  *
1743  * Disable the regulator output voltage or current.  Calls to
1744  * regulator_enable() must be balanced with calls to
1745  * regulator_disable().
1746  *
1747  * NOTE: this will only disable the regulator output if no other consumer
1748  * devices have it enabled, the regulator device supports disabling and
1749  * machine constraints permit this operation.
1750  */
1751 int regulator_disable(struct regulator *regulator)
1752 {
1753         struct regulator_dev *rdev = regulator->rdev;
1754         int ret = 0;
1755
1756         mutex_lock(&rdev->mutex);
1757         ret = _regulator_disable(rdev);
1758         mutex_unlock(&rdev->mutex);
1759
1760         if (ret == 0 && rdev->supply)
1761                 regulator_disable(rdev->supply);
1762
1763         return ret;
1764 }
1765 EXPORT_SYMBOL_GPL(regulator_disable);
1766
1767 /* locks held by regulator_force_disable() */
1768 static int _regulator_force_disable(struct regulator_dev *rdev)
1769 {
1770         int ret = 0;
1771
1772         /* force disable */
1773         if (rdev->desc->ops->disable) {
1774                 /* ah well, who wants to live forever... */
1775                 ret = rdev->desc->ops->disable(rdev);
1776                 if (ret < 0) {
1777                         rdev_err(rdev, "failed to force disable\n");
1778                         return ret;
1779                 }
1780                 /* notify other consumers that power has been forced off */
1781                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1782                         REGULATOR_EVENT_DISABLE, NULL);
1783         }
1784
1785         return ret;
1786 }
1787
1788 /**
1789  * regulator_force_disable - force disable regulator output
1790  * @regulator: regulator source
1791  *
1792  * Forcibly disable the regulator output voltage or current.
1793  * NOTE: this *will* disable the regulator output even if other consumer
1794  * devices have it enabled. This should be used for situations when device
1795  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1796  */
1797 int regulator_force_disable(struct regulator *regulator)
1798 {
1799         struct regulator_dev *rdev = regulator->rdev;
1800         int ret;
1801
1802         mutex_lock(&rdev->mutex);
1803         regulator->uA_load = 0;
1804         ret = _regulator_force_disable(regulator->rdev);
1805         mutex_unlock(&rdev->mutex);
1806
1807         if (rdev->supply)
1808                 while (rdev->open_count--)
1809                         regulator_disable(rdev->supply);
1810
1811         return ret;
1812 }
1813 EXPORT_SYMBOL_GPL(regulator_force_disable);
1814
1815 static void regulator_disable_work(struct work_struct *work)
1816 {
1817         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1818                                                   disable_work.work);
1819         int count, i, ret;
1820
1821         mutex_lock(&rdev->mutex);
1822
1823         BUG_ON(!rdev->deferred_disables);
1824
1825         count = rdev->deferred_disables;
1826         rdev->deferred_disables = 0;
1827
1828         for (i = 0; i < count; i++) {
1829                 ret = _regulator_disable(rdev);
1830                 if (ret != 0)
1831                         rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1832         }
1833
1834         mutex_unlock(&rdev->mutex);
1835
1836         if (rdev->supply) {
1837                 for (i = 0; i < count; i++) {
1838                         ret = regulator_disable(rdev->supply);
1839                         if (ret != 0) {
1840                                 rdev_err(rdev,
1841                                          "Supply disable failed: %d\n", ret);
1842                         }
1843                 }
1844         }
1845 }
1846
1847 /**
1848  * regulator_disable_deferred - disable regulator output with delay
1849  * @regulator: regulator source
1850  * @ms: miliseconds until the regulator is disabled
1851  *
1852  * Execute regulator_disable() on the regulator after a delay.  This
1853  * is intended for use with devices that require some time to quiesce.
1854  *
1855  * NOTE: this will only disable the regulator output if no other consumer
1856  * devices have it enabled, the regulator device supports disabling and
1857  * machine constraints permit this operation.
1858  */
1859 int regulator_disable_deferred(struct regulator *regulator, int ms)
1860 {
1861         struct regulator_dev *rdev = regulator->rdev;
1862         int ret;
1863
1864         mutex_lock(&rdev->mutex);
1865         rdev->deferred_disables++;
1866         mutex_unlock(&rdev->mutex);
1867
1868         ret = schedule_delayed_work(&rdev->disable_work,
1869                                     msecs_to_jiffies(ms));
1870         if (ret < 0)
1871                 return ret;
1872         else
1873                 return 0;
1874 }
1875 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1876
1877 static int _regulator_is_enabled(struct regulator_dev *rdev)
1878 {
1879         /* If we don't know then assume that the regulator is always on */
1880         if (!rdev->desc->ops->is_enabled)
1881                 return 1;
1882
1883         return rdev->desc->ops->is_enabled(rdev);
1884 }
1885
1886 /**
1887  * regulator_is_enabled - is the regulator output enabled
1888  * @regulator: regulator source
1889  *
1890  * Returns positive if the regulator driver backing the source/client
1891  * has requested that the device be enabled, zero if it hasn't, else a
1892  * negative errno code.
1893  *
1894  * Note that the device backing this regulator handle can have multiple
1895  * users, so it might be enabled even if regulator_enable() was never
1896  * called for this particular source.
1897  */
1898 int regulator_is_enabled(struct regulator *regulator)
1899 {
1900         int ret;
1901
1902         mutex_lock(&regulator->rdev->mutex);
1903         ret = _regulator_is_enabled(regulator->rdev);
1904         mutex_unlock(&regulator->rdev->mutex);
1905
1906         return ret;
1907 }
1908 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1909
1910 /**
1911  * regulator_count_voltages - count regulator_list_voltage() selectors
1912  * @regulator: regulator source
1913  *
1914  * Returns number of selectors, or negative errno.  Selectors are
1915  * numbered starting at zero, and typically correspond to bitfields
1916  * in hardware registers.
1917  */
1918 int regulator_count_voltages(struct regulator *regulator)
1919 {
1920         struct regulator_dev    *rdev = regulator->rdev;
1921
1922         return rdev->desc->n_voltages ? : -EINVAL;
1923 }
1924 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1925
1926 /**
1927  * regulator_list_voltage - enumerate supported voltages
1928  * @regulator: regulator source
1929  * @selector: identify voltage to list
1930  * Context: can sleep
1931  *
1932  * Returns a voltage that can be passed to @regulator_set_voltage(),
1933  * zero if this selector code can't be used on this system, or a
1934  * negative errno.
1935  */
1936 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1937 {
1938         struct regulator_dev    *rdev = regulator->rdev;
1939         struct regulator_ops    *ops = rdev->desc->ops;
1940         int                     ret;
1941
1942         if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1943                 return -EINVAL;
1944
1945         mutex_lock(&rdev->mutex);
1946         ret = ops->list_voltage(rdev, selector);
1947         mutex_unlock(&rdev->mutex);
1948
1949         if (ret > 0) {
1950                 if (ret < rdev->constraints->min_uV)
1951                         ret = 0;
1952                 else if (ret > rdev->constraints->max_uV)
1953                         ret = 0;
1954         }
1955
1956         return ret;
1957 }
1958 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1959
1960 /**
1961  * regulator_is_supported_voltage - check if a voltage range can be supported
1962  *
1963  * @regulator: Regulator to check.
1964  * @min_uV: Minimum required voltage in uV.
1965  * @max_uV: Maximum required voltage in uV.
1966  *
1967  * Returns a boolean or a negative error code.
1968  */
1969 int regulator_is_supported_voltage(struct regulator *regulator,
1970                                    int min_uV, int max_uV)
1971 {
1972         int i, voltages, ret;
1973
1974         ret = regulator_count_voltages(regulator);
1975         if (ret < 0)
1976                 return ret;
1977         voltages = ret;
1978
1979         for (i = 0; i < voltages; i++) {
1980                 ret = regulator_list_voltage(regulator, i);
1981
1982                 if (ret >= min_uV && ret <= max_uV)
1983                         return 1;
1984         }
1985
1986         return 0;
1987 }
1988 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
1989
1990 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1991                                      int min_uV, int max_uV)
1992 {
1993         int ret;
1994         int delay = 0;
1995         unsigned int selector;
1996
1997         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1998
1999         min_uV += rdev->constraints->uV_offset;
2000         max_uV += rdev->constraints->uV_offset;
2001
2002         if (rdev->desc->ops->set_voltage) {
2003                 if (_regulator_is_enabled(rdev))
2004                         _notifier_call_chain(rdev,
2005                         REGULATOR_EVENT_OUT_PRECHANGE, (void *)min_uV);
2006
2007                 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2008                                                    &selector);
2009
2010                 if (rdev->desc->ops->list_voltage)
2011                         selector = rdev->desc->ops->list_voltage(rdev,
2012                                                                  selector);
2013                 else
2014                         selector = -1;
2015         } else if (rdev->desc->ops->set_voltage_sel) {
2016                 int best_val = INT_MAX;
2017                 int i;
2018
2019                 selector = 0;
2020
2021                 /* Find the smallest voltage that falls within the specified
2022                  * range.
2023                  */
2024                 for (i = 0; i < rdev->desc->n_voltages; i++) {
2025                         ret = rdev->desc->ops->list_voltage(rdev, i);
2026                         if (ret < 0)
2027                                 continue;
2028
2029                         if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2030                                 best_val = ret;
2031                                 selector = i;
2032                         }
2033                 }
2034
2035                 /*
2036                  * If we can't obtain the old selector there is not enough
2037                  * info to call set_voltage_time_sel().
2038                  */
2039                 if (rdev->desc->ops->set_voltage_time_sel &&
2040                     rdev->desc->ops->get_voltage_sel) {
2041                         unsigned int old_selector = 0;
2042
2043                         ret = rdev->desc->ops->get_voltage_sel(rdev);
2044                         if (ret < 0)
2045                                 return ret;
2046                         old_selector = ret;
2047                         ret = rdev->desc->ops->set_voltage_time_sel(rdev,
2048                                                 old_selector, selector);
2049                         if (ret < 0)
2050                                 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
2051                         else
2052                                 delay = ret;
2053                 }
2054
2055                 if (best_val != INT_MAX) {
2056                         if (_regulator_is_enabled(rdev))
2057                                 _notifier_call_chain(rdev,
2058                                 REGULATOR_EVENT_OUT_PRECHANGE, (void *)best_val);
2059
2060                         ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2061                         selector = best_val;
2062                 } else {
2063                         ret = -EINVAL;
2064                 }
2065         } else {
2066                 ret = -EINVAL;
2067         }
2068
2069         /* Insert any necessary delays */
2070         if (delay >= 1000) {
2071                 mdelay(delay / 1000);
2072                 udelay(delay % 1000);
2073         } else if (delay) {
2074                 udelay(delay);
2075         }
2076
2077         if (ret == 0)
2078                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2079                                      NULL);
2080
2081         if (_regulator_is_enabled(rdev)) {
2082                 if (selector != -1)
2083                         min_uV = selector;
2084                 _notifier_call_chain(rdev, REGULATOR_EVENT_OUT_POSTCHANGE,
2085                                      (void *)min_uV);
2086         }
2087
2088         trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
2089
2090         return ret;
2091 }
2092
2093 /**
2094  * regulator_set_voltage - set regulator output voltage
2095  * @regulator: regulator source
2096  * @min_uV: Minimum required voltage in uV
2097  * @max_uV: Maximum acceptable voltage in uV
2098  *
2099  * Sets a voltage regulator to the desired output voltage. This can be set
2100  * during any regulator state. IOW, regulator can be disabled or enabled.
2101  *
2102  * If the regulator is enabled then the voltage will change to the new value
2103  * immediately otherwise if the regulator is disabled the regulator will
2104  * output at the new voltage when enabled.
2105  *
2106  * NOTE: If the regulator is shared between several devices then the lowest
2107  * request voltage that meets the system constraints will be used.
2108  * Regulator system constraints must be set for this regulator before
2109  * calling this function otherwise this call will fail.
2110  */
2111 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2112 {
2113         struct regulator_dev *rdev = regulator->rdev;
2114         int ret = 0;
2115
2116         mutex_lock(&rdev->mutex);
2117
2118         /* If we're setting the same range as last time the change
2119          * should be a noop (some cpufreq implementations use the same
2120          * voltage for multiple frequencies, for example).
2121          */
2122         if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2123                 goto out;
2124
2125         /* sanity check */
2126         if (!rdev->desc->ops->set_voltage &&
2127             !rdev->desc->ops->set_voltage_sel) {
2128                 ret = -EINVAL;
2129                 goto out;
2130         }
2131
2132         /* constraints check */
2133         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2134         if (ret < 0)
2135                 goto out;
2136         regulator->min_uV = min_uV;
2137         regulator->max_uV = max_uV;
2138
2139         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2140         if (ret < 0)
2141                 goto out;
2142
2143         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2144
2145 out:
2146         mutex_unlock(&rdev->mutex);
2147         return ret;
2148 }
2149 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2150
2151 /**
2152  * regulator_set_voltage_time - get raise/fall time
2153  * @regulator: regulator source
2154  * @old_uV: starting voltage in microvolts
2155  * @new_uV: target voltage in microvolts
2156  *
2157  * Provided with the starting and ending voltage, this function attempts to
2158  * calculate the time in microseconds required to rise or fall to this new
2159  * voltage.
2160  */
2161 int regulator_set_voltage_time(struct regulator *regulator,
2162                                int old_uV, int new_uV)
2163 {
2164         struct regulator_dev    *rdev = regulator->rdev;
2165         struct regulator_ops    *ops = rdev->desc->ops;
2166         int old_sel = -1;
2167         int new_sel = -1;
2168         int voltage;
2169         int i;
2170
2171         /* Currently requires operations to do this */
2172         if (!ops->list_voltage || !ops->set_voltage_time_sel
2173             || !rdev->desc->n_voltages)
2174                 return -EINVAL;
2175
2176         for (i = 0; i < rdev->desc->n_voltages; i++) {
2177                 /* We only look for exact voltage matches here */
2178                 voltage = regulator_list_voltage(regulator, i);
2179                 if (voltage < 0)
2180                         return -EINVAL;
2181                 if (voltage == 0)
2182                         continue;
2183                 if (voltage == old_uV)
2184                         old_sel = i;
2185                 if (voltage == new_uV)
2186                         new_sel = i;
2187         }
2188
2189         if (old_sel < 0 || new_sel < 0)
2190                 return -EINVAL;
2191
2192         return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2193 }
2194 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2195
2196 /**
2197  * regulator_set_voltage_time_sel - get raise/fall time
2198  * @rdev: regulator source device
2199  * @old_selector: selector for starting voltage
2200  * @new_selector: selector for target voltage
2201  *
2202  * Provided with the starting and target voltage selectors, this function
2203  * returns time in microseconds required to rise or fall to this new voltage
2204  *
2205  * Drivers providing ramp_delay in regulation_constraints can use this as their
2206  * set_voltage_time_sel() operation.
2207  */
2208 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2209                                    unsigned int old_selector,
2210                                    unsigned int new_selector)
2211 {
2212         unsigned int ramp_delay = 0;
2213         int old_volt, new_volt;
2214
2215         if (rdev->constraints->ramp_delay)
2216                 ramp_delay = rdev->constraints->ramp_delay;
2217         else if (rdev->desc->ramp_delay)
2218                 ramp_delay = rdev->desc->ramp_delay;
2219
2220         if (ramp_delay == 0) {
2221                 rdev_warn(rdev, "ramp_delay not set\n");
2222                 return 0;
2223         }
2224
2225         /* sanity check */
2226         if (!rdev->desc->ops->list_voltage)
2227                 return -EINVAL;
2228
2229         old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2230         new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2231
2232         return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2233 }
2234 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2235
2236 /**
2237  * regulator_sync_voltage - re-apply last regulator output voltage
2238  * @regulator: regulator source
2239  *
2240  * Re-apply the last configured voltage.  This is intended to be used
2241  * where some external control source the consumer is cooperating with
2242  * has caused the configured voltage to change.
2243  */
2244 int regulator_sync_voltage(struct regulator *regulator)
2245 {
2246         struct regulator_dev *rdev = regulator->rdev;
2247         int ret, min_uV, max_uV;
2248
2249         mutex_lock(&rdev->mutex);
2250
2251         if (!rdev->desc->ops->set_voltage &&
2252             !rdev->desc->ops->set_voltage_sel) {
2253                 ret = -EINVAL;
2254                 goto out;
2255         }
2256
2257         /* This is only going to work if we've had a voltage configured. */
2258         if (!regulator->min_uV && !regulator->max_uV) {
2259                 ret = -EINVAL;
2260                 goto out;
2261         }
2262
2263         min_uV = regulator->min_uV;
2264         max_uV = regulator->max_uV;
2265
2266         /* This should be a paranoia check... */
2267         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2268         if (ret < 0)
2269                 goto out;
2270
2271         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2272         if (ret < 0)
2273                 goto out;
2274
2275         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2276
2277 out:
2278         mutex_unlock(&rdev->mutex);
2279         return ret;
2280 }
2281 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2282
2283 static int _regulator_get_voltage(struct regulator_dev *rdev)
2284 {
2285         int sel, ret;
2286
2287         if (rdev->desc->ops->get_voltage_sel) {
2288                 sel = rdev->desc->ops->get_voltage_sel(rdev);
2289                 if (sel < 0)
2290                         return sel;
2291                 ret = rdev->desc->ops->list_voltage(rdev, sel);
2292         } else if (rdev->desc->ops->get_voltage) {
2293                 ret = rdev->desc->ops->get_voltage(rdev);
2294         } else {
2295                 return -EINVAL;
2296         }
2297
2298         if (ret < 0)
2299                 return ret;
2300         return ret - rdev->constraints->uV_offset;
2301 }
2302
2303 /**
2304  * regulator_get_voltage - get regulator output voltage
2305  * @regulator: regulator source
2306  *
2307  * This returns the current regulator voltage in uV.
2308  *
2309  * NOTE: If the regulator is disabled it will return the voltage value. This
2310  * function should not be used to determine regulator state.
2311  */
2312 int regulator_get_voltage(struct regulator *regulator)
2313 {
2314         int ret;
2315
2316         mutex_lock(&regulator->rdev->mutex);
2317
2318         ret = _regulator_get_voltage(regulator->rdev);
2319
2320         mutex_unlock(&regulator->rdev->mutex);
2321
2322         return ret;
2323 }
2324 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2325
2326 /**
2327  * regulator_set_current_limit - set regulator output current limit
2328  * @regulator: regulator source
2329  * @min_uA: Minimuum supported current in uA
2330  * @max_uA: Maximum supported current in uA
2331  *
2332  * Sets current sink to the desired output current. This can be set during
2333  * any regulator state. IOW, regulator can be disabled or enabled.
2334  *
2335  * If the regulator is enabled then the current will change to the new value
2336  * immediately otherwise if the regulator is disabled the regulator will
2337  * output at the new current when enabled.
2338  *
2339  * NOTE: Regulator system constraints must be set for this regulator before
2340  * calling this function otherwise this call will fail.
2341  */
2342 int regulator_set_current_limit(struct regulator *regulator,
2343                                int min_uA, int max_uA)
2344 {
2345         struct regulator_dev *rdev = regulator->rdev;
2346         int ret;
2347
2348         mutex_lock(&rdev->mutex);
2349
2350         /* sanity check */
2351         if (!rdev->desc->ops->set_current_limit) {
2352                 ret = -EINVAL;
2353                 goto out;
2354         }
2355
2356         /* constraints check */
2357         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2358         if (ret < 0)
2359                 goto out;
2360
2361         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2362 out:
2363         mutex_unlock(&rdev->mutex);
2364         return ret;
2365 }
2366 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2367
2368 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2369 {
2370         int ret;
2371
2372         mutex_lock(&rdev->mutex);
2373
2374         /* sanity check */
2375         if (!rdev->desc->ops->get_current_limit) {
2376                 ret = -EINVAL;
2377                 goto out;
2378         }
2379
2380         ret = rdev->desc->ops->get_current_limit(rdev);
2381 out:
2382         mutex_unlock(&rdev->mutex);
2383         return ret;
2384 }
2385
2386 /**
2387  * regulator_get_current_limit - get regulator output current
2388  * @regulator: regulator source
2389  *
2390  * This returns the current supplied by the specified current sink in uA.
2391  *
2392  * NOTE: If the regulator is disabled it will return the current value. This
2393  * function should not be used to determine regulator state.
2394  */
2395 int regulator_get_current_limit(struct regulator *regulator)
2396 {
2397         return _regulator_get_current_limit(regulator->rdev);
2398 }
2399 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2400
2401 /**
2402  * regulator_set_mode - set regulator operating mode
2403  * @regulator: regulator source
2404  * @mode: operating mode - one of the REGULATOR_MODE constants
2405  *
2406  * Set regulator operating mode to increase regulator efficiency or improve
2407  * regulation performance.
2408  *
2409  * NOTE: Regulator system constraints must be set for this regulator before
2410  * calling this function otherwise this call will fail.
2411  */
2412 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2413 {
2414         struct regulator_dev *rdev = regulator->rdev;
2415         int ret;
2416         int regulator_curr_mode;
2417
2418         mutex_lock(&rdev->mutex);
2419
2420         /* sanity check */
2421         if (!rdev->desc->ops->set_mode) {
2422                 ret = -EINVAL;
2423                 goto out;
2424         }
2425
2426         /* return if the same mode is requested */
2427         if (rdev->desc->ops->get_mode) {
2428                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2429                 if (regulator_curr_mode == mode) {
2430                         ret = 0;
2431                         goto out;
2432                 }
2433         }
2434
2435         /* constraints check */
2436         ret = regulator_mode_constrain(rdev, &mode);
2437         if (ret < 0)
2438                 goto out;
2439
2440         ret = rdev->desc->ops->set_mode(rdev, mode);
2441 out:
2442         mutex_unlock(&rdev->mutex);
2443         return ret;
2444 }
2445 EXPORT_SYMBOL_GPL(regulator_set_mode);
2446
2447 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2448 {
2449         int ret;
2450
2451         mutex_lock(&rdev->mutex);
2452
2453         /* sanity check */
2454         if (!rdev->desc->ops->get_mode) {
2455                 ret = -EINVAL;
2456                 goto out;
2457         }
2458
2459         ret = rdev->desc->ops->get_mode(rdev);
2460 out:
2461         mutex_unlock(&rdev->mutex);
2462         return ret;
2463 }
2464
2465 /**
2466  * regulator_get_mode - get regulator operating mode
2467  * @regulator: regulator source
2468  *
2469  * Get the current regulator operating mode.
2470  */
2471 unsigned int regulator_get_mode(struct regulator *regulator)
2472 {
2473         return _regulator_get_mode(regulator->rdev);
2474 }
2475 EXPORT_SYMBOL_GPL(regulator_get_mode);
2476
2477 /**
2478  * regulator_set_optimum_mode - set regulator optimum operating mode
2479  * @regulator: regulator source
2480  * @uA_load: load current
2481  *
2482  * Notifies the regulator core of a new device load. This is then used by
2483  * DRMS (if enabled by constraints) to set the most efficient regulator
2484  * operating mode for the new regulator loading.
2485  *
2486  * Consumer devices notify their supply regulator of the maximum power
2487  * they will require (can be taken from device datasheet in the power
2488  * consumption tables) when they change operational status and hence power
2489  * state. Examples of operational state changes that can affect power
2490  * consumption are :-
2491  *
2492  *    o Device is opened / closed.
2493  *    o Device I/O is about to begin or has just finished.
2494  *    o Device is idling in between work.
2495  *
2496  * This information is also exported via sysfs to userspace.
2497  *
2498  * DRMS will sum the total requested load on the regulator and change
2499  * to the most efficient operating mode if platform constraints allow.
2500  *
2501  * Returns the new regulator mode or error.
2502  */
2503 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2504 {
2505         struct regulator_dev *rdev = regulator->rdev;
2506         struct regulator *consumer;
2507         int ret, output_uV, input_uV, total_uA_load = 0;
2508         unsigned int mode;
2509
2510         mutex_lock(&rdev->mutex);
2511
2512         /*
2513          * first check to see if we can set modes at all, otherwise just
2514          * tell the consumer everything is OK.
2515          */
2516         regulator->uA_load = uA_load;
2517         ret = regulator_check_drms(rdev);
2518         if (ret < 0) {
2519                 ret = 0;
2520                 goto out;
2521         }
2522
2523         if (!rdev->desc->ops->get_optimum_mode)
2524                 goto out;
2525
2526         /*
2527          * we can actually do this so any errors are indicators of
2528          * potential real failure.
2529          */
2530         ret = -EINVAL;
2531
2532         /* get output voltage */
2533         output_uV = _regulator_get_voltage(rdev);
2534         if (output_uV <= 0) {
2535                 rdev_err(rdev, "invalid output voltage found\n");
2536                 goto out;
2537         }
2538
2539         /* get input voltage */
2540         input_uV = 0;
2541         if (rdev->supply)
2542                 input_uV = regulator_get_voltage(rdev->supply);
2543         if (input_uV <= 0)
2544                 input_uV = rdev->constraints->input_uV;
2545         if (input_uV <= 0) {
2546                 rdev_err(rdev, "invalid input voltage found\n");
2547                 goto out;
2548         }
2549
2550         /* calc total requested load for this regulator */
2551         list_for_each_entry(consumer, &rdev->consumer_list, list)
2552                 total_uA_load += consumer->uA_load;
2553
2554         mode = rdev->desc->ops->get_optimum_mode(rdev,
2555                                                  input_uV, output_uV,
2556                                                  total_uA_load);
2557         ret = regulator_mode_constrain(rdev, &mode);
2558         if (ret < 0) {
2559                 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2560                          total_uA_load, input_uV, output_uV);
2561                 goto out;
2562         }
2563
2564         ret = rdev->desc->ops->set_mode(rdev, mode);
2565         if (ret < 0) {
2566                 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2567                 goto out;
2568         }
2569         ret = mode;
2570 out:
2571         mutex_unlock(&rdev->mutex);
2572         return ret;
2573 }
2574 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2575
2576 /**
2577  * regulator_set_control_mode - set regulator control mode
2578  * @regulator: regulator source
2579  * @mode: control mode - one of the REGULATOR_CONTROL_MODE constants
2580  *
2581  * Set regulator control mode to regulate the regulator output.
2582  *
2583  * NOTE: Regulator system constraints must be set for this regulator before
2584  * calling this function otherwise this call will fail.
2585  */
2586 int regulator_set_control_mode(struct regulator *regulator, unsigned int mode)
2587 {
2588         struct regulator_dev *rdev = regulator->rdev;
2589         int ret;
2590         int regulator_curr_mode;
2591
2592         mutex_lock(&rdev->mutex);
2593
2594         /* sanity check */
2595         if (!rdev->desc->ops->set_control_mode) {
2596                 ret = -EINVAL;
2597                 goto out;
2598         }
2599
2600         /* return if the same mode is requested */
2601         if (rdev->desc->ops->get_control_mode) {
2602                 regulator_curr_mode = rdev->desc->ops->get_control_mode(rdev);
2603                 if (regulator_curr_mode == mode) {
2604                         ret = 0;
2605                         goto out;
2606                 }
2607         }
2608
2609         /* constraints check */
2610         ret = regulator_check_control(rdev);
2611         if (ret < 0)
2612                 goto out;
2613
2614         ret = rdev->desc->ops->set_control_mode(rdev, mode);
2615 out:
2616         mutex_unlock(&rdev->mutex);
2617         return ret;
2618 }
2619 EXPORT_SYMBOL_GPL(regulator_set_control_mode);
2620
2621 /**
2622  * regulator_get_control_mode - get regulator control mode
2623  * @regulator: regulator source
2624  *
2625  * Get the current regulator control mode.
2626  */
2627 unsigned int regulator_get_control_mode(struct regulator *regulator)
2628 {
2629         struct regulator_dev *rdev = regulator->rdev;
2630         int ret = -EINVAL;
2631
2632         mutex_lock(&rdev->mutex);
2633
2634         /* sanity check */
2635         if (!rdev->desc->ops->get_control_mode)
2636                 goto out;
2637
2638         ret = rdev->desc->ops->get_control_mode(rdev);
2639 out:
2640         mutex_unlock(&rdev->mutex);
2641         return ret;
2642 }
2643 EXPORT_SYMBOL_GPL(regulator_get_control_mode);
2644
2645 /**
2646  * regulator_register_notifier - register regulator event notifier
2647  * @regulator: regulator source
2648  * @nb: notifier block
2649  *
2650  * Register notifier block to receive regulator events.
2651  */
2652 int regulator_register_notifier(struct regulator *regulator,
2653                               struct notifier_block *nb)
2654 {
2655         return blocking_notifier_chain_register(&regulator->rdev->notifier,
2656                                                 nb);
2657 }
2658 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2659
2660 /**
2661  * regulator_unregister_notifier - unregister regulator event notifier
2662  * @regulator: regulator source
2663  * @nb: notifier block
2664  *
2665  * Unregister regulator event notifier block.
2666  */
2667 int regulator_unregister_notifier(struct regulator *regulator,
2668                                 struct notifier_block *nb)
2669 {
2670         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2671                                                   nb);
2672 }
2673 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2674
2675 /* notify regulator consumers and downstream regulator consumers.
2676  * Note mutex must be held by caller.
2677  */
2678 static void _notifier_call_chain(struct regulator_dev *rdev,
2679                                   unsigned long event, void *data)
2680 {
2681         /* call rdev chain first */
2682         blocking_notifier_call_chain(&rdev->notifier, event, data);
2683 }
2684
2685 /**
2686  * regulator_bulk_get - get multiple regulator consumers
2687  *
2688  * @dev:           Device to supply
2689  * @num_consumers: Number of consumers to register
2690  * @consumers:     Configuration of consumers; clients are stored here.
2691  *
2692  * @return 0 on success, an errno on failure.
2693  *
2694  * This helper function allows drivers to get several regulator
2695  * consumers in one operation.  If any of the regulators cannot be
2696  * acquired then any regulators that were allocated will be freed
2697  * before returning to the caller.
2698  */
2699 int regulator_bulk_get(struct device *dev, int num_consumers,
2700                        struct regulator_bulk_data *consumers)
2701 {
2702         int i;
2703         int ret;
2704
2705         for (i = 0; i < num_consumers; i++)
2706                 consumers[i].consumer = NULL;
2707
2708         for (i = 0; i < num_consumers; i++) {
2709                 consumers[i].consumer = regulator_get(dev,
2710                                                       consumers[i].supply);
2711                 if (IS_ERR(consumers[i].consumer)) {
2712                         ret = PTR_ERR(consumers[i].consumer);
2713                         dev_err(dev, "Failed to get supply '%s': %d\n",
2714                                 consumers[i].supply, ret);
2715                         consumers[i].consumer = NULL;
2716                         goto err;
2717                 }
2718         }
2719
2720         return 0;
2721
2722 err:
2723         while (--i >= 0)
2724                 regulator_put(consumers[i].consumer);
2725
2726         return ret;
2727 }
2728 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2729
2730 /**
2731  * devm_regulator_bulk_get - managed get multiple regulator consumers
2732  *
2733  * @dev:           Device to supply
2734  * @num_consumers: Number of consumers to register
2735  * @consumers:     Configuration of consumers; clients are stored here.
2736  *
2737  * @return 0 on success, an errno on failure.
2738  *
2739  * This helper function allows drivers to get several regulator
2740  * consumers in one operation with management, the regulators will
2741  * automatically be freed when the device is unbound.  If any of the
2742  * regulators cannot be acquired then any regulators that were
2743  * allocated will be freed before returning to the caller.
2744  */
2745 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2746                             struct regulator_bulk_data *consumers)
2747 {
2748         int i;
2749         int ret;
2750
2751         for (i = 0; i < num_consumers; i++)
2752                 consumers[i].consumer = NULL;
2753
2754         for (i = 0; i < num_consumers; i++) {
2755                 consumers[i].consumer = devm_regulator_get(dev,
2756                                                            consumers[i].supply);
2757                 if (IS_ERR(consumers[i].consumer)) {
2758                         ret = PTR_ERR(consumers[i].consumer);
2759                         dev_err(dev, "Failed to get supply '%s': %d\n",
2760                                 consumers[i].supply, ret);
2761                         consumers[i].consumer = NULL;
2762                         goto err;
2763                 }
2764         }
2765
2766         return 0;
2767
2768 err:
2769         for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2770                 devm_regulator_put(consumers[i].consumer);
2771
2772         return ret;
2773 }
2774 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2775
2776 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2777 {
2778         struct regulator_bulk_data *bulk = data;
2779
2780         bulk->ret = regulator_enable(bulk->consumer);
2781 }
2782
2783 /**
2784  * regulator_bulk_enable - enable multiple regulator consumers
2785  *
2786  * @num_consumers: Number of consumers
2787  * @consumers:     Consumer data; clients are stored here.
2788  * @return         0 on success, an errno on failure
2789  *
2790  * This convenience API allows consumers to enable multiple regulator
2791  * clients in a single API call.  If any consumers cannot be enabled
2792  * then any others that were enabled will be disabled again prior to
2793  * return.
2794  */
2795 int regulator_bulk_enable(int num_consumers,
2796                           struct regulator_bulk_data *consumers)
2797 {
2798         LIST_HEAD(async_domain);
2799         int i;
2800         int ret = 0;
2801
2802         for (i = 0; i < num_consumers; i++)
2803                 async_schedule_domain(regulator_bulk_enable_async,
2804                                       &consumers[i], &async_domain);
2805
2806         async_synchronize_full_domain(&async_domain);
2807
2808         /* If any consumer failed we need to unwind any that succeeded */
2809         for (i = 0; i < num_consumers; i++) {
2810                 if (consumers[i].ret != 0) {
2811                         ret = consumers[i].ret;
2812                         goto err;
2813                 }
2814         }
2815
2816         return 0;
2817
2818 err:
2819         pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2820         while (--i >= 0)
2821                 regulator_disable(consumers[i].consumer);
2822
2823         return ret;
2824 }
2825 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2826
2827 /**
2828  * regulator_bulk_disable - disable multiple regulator consumers
2829  *
2830  * @num_consumers: Number of consumers
2831  * @consumers:     Consumer data; clients are stored here.
2832  * @return         0 on success, an errno on failure
2833  *
2834  * This convenience API allows consumers to disable multiple regulator
2835  * clients in a single API call.  If any consumers cannot be disabled
2836  * then any others that were disabled will be enabled again prior to
2837  * return.
2838  */
2839 int regulator_bulk_disable(int num_consumers,
2840                            struct regulator_bulk_data *consumers)
2841 {
2842         int i;
2843         int ret;
2844
2845         for (i = num_consumers - 1; i >= 0; --i) {
2846                 ret = regulator_disable(consumers[i].consumer);
2847                 if (ret != 0)
2848                         goto err;
2849         }
2850
2851         return 0;
2852
2853 err:
2854         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2855         for (++i; i < num_consumers; ++i)
2856                 regulator_enable(consumers[i].consumer);
2857
2858         return ret;
2859 }
2860 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2861
2862 /**
2863  * regulator_bulk_force_disable - force disable multiple regulator consumers
2864  *
2865  * @num_consumers: Number of consumers
2866  * @consumers:     Consumer data; clients are stored here.
2867  * @return         0 on success, an errno on failure
2868  *
2869  * This convenience API allows consumers to forcibly disable multiple regulator
2870  * clients in a single API call.
2871  * NOTE: This should be used for situations when device damage will
2872  * likely occur if the regulators are not disabled (e.g. over temp).
2873  * Although regulator_force_disable function call for some consumers can
2874  * return error numbers, the function is called for all consumers.
2875  */
2876 int regulator_bulk_force_disable(int num_consumers,
2877                            struct regulator_bulk_data *consumers)
2878 {
2879         int i;
2880         int ret;
2881
2882         for (i = 0; i < num_consumers; i++)
2883                 consumers[i].ret =
2884                             regulator_force_disable(consumers[i].consumer);
2885
2886         for (i = 0; i < num_consumers; i++) {
2887                 if (consumers[i].ret != 0) {
2888                         ret = consumers[i].ret;
2889                         goto out;
2890                 }
2891         }
2892
2893         return 0;
2894 out:
2895         return ret;
2896 }
2897 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2898
2899 /**
2900  * regulator_bulk_free - free multiple regulator consumers
2901  *
2902  * @num_consumers: Number of consumers
2903  * @consumers:     Consumer data; clients are stored here.
2904  *
2905  * This convenience API allows consumers to free multiple regulator
2906  * clients in a single API call.
2907  */
2908 void regulator_bulk_free(int num_consumers,
2909                          struct regulator_bulk_data *consumers)
2910 {
2911         int i;
2912
2913         for (i = 0; i < num_consumers; i++) {
2914                 regulator_put(consumers[i].consumer);
2915                 consumers[i].consumer = NULL;
2916         }
2917 }
2918 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2919
2920 /**
2921  * regulator_notifier_call_chain - call regulator event notifier
2922  * @rdev: regulator source
2923  * @event: notifier block
2924  * @data: callback-specific data.
2925  *
2926  * Called by regulator drivers to notify clients a regulator event has
2927  * occurred. We also notify regulator clients downstream.
2928  * Note lock must be held by caller.
2929  */
2930 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2931                                   unsigned long event, void *data)
2932 {
2933         _notifier_call_chain(rdev, event, data);
2934         return NOTIFY_DONE;
2935
2936 }
2937 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2938
2939 /**
2940  * regulator_mode_to_status - convert a regulator mode into a status
2941  *
2942  * @mode: Mode to convert
2943  *
2944  * Convert a regulator mode into a status.
2945  */
2946 int regulator_mode_to_status(unsigned int mode)
2947 {
2948         switch (mode) {
2949         case REGULATOR_MODE_FAST:
2950                 return REGULATOR_STATUS_FAST;
2951         case REGULATOR_MODE_NORMAL:
2952                 return REGULATOR_STATUS_NORMAL;
2953         case REGULATOR_MODE_IDLE:
2954                 return REGULATOR_STATUS_IDLE;
2955         case REGULATOR_STATUS_STANDBY:
2956                 return REGULATOR_STATUS_STANDBY;
2957         default:
2958                 return 0;
2959         }
2960 }
2961 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2962
2963 /*
2964  * To avoid cluttering sysfs (and memory) with useless state, only
2965  * create attributes that can be meaningfully displayed.
2966  */
2967 static int add_regulator_attributes(struct regulator_dev *rdev)
2968 {
2969         struct device           *dev = &rdev->dev;
2970         struct regulator_ops    *ops = rdev->desc->ops;
2971         int                     status = 0;
2972
2973         /* some attributes need specific methods to be displayed */
2974         if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2975             (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
2976                 status = device_create_file(dev, &dev_attr_microvolts);
2977                 if (status < 0)
2978                         return status;
2979         }
2980         if (ops->get_current_limit) {
2981                 status = device_create_file(dev, &dev_attr_microamps);
2982                 if (status < 0)
2983                         return status;
2984         }
2985         if (ops->get_mode) {
2986                 status = device_create_file(dev, &dev_attr_opmode);
2987                 if (status < 0)
2988                         return status;
2989         }
2990         if (ops->is_enabled) {
2991                 status = device_create_file(dev, &dev_attr_state);
2992                 if (status < 0)
2993                         return status;
2994         }
2995         if (ops->get_status) {
2996                 status = device_create_file(dev, &dev_attr_status);
2997                 if (status < 0)
2998                         return status;
2999         }
3000
3001         /* some attributes are type-specific */
3002         if (rdev->desc->type == REGULATOR_CURRENT) {
3003                 status = device_create_file(dev, &dev_attr_requested_microamps);
3004                 if (status < 0)
3005                         return status;
3006         }
3007
3008         /* all the other attributes exist to support constraints;
3009          * don't show them if there are no constraints, or if the
3010          * relevant supporting methods are missing.
3011          */
3012         if (!rdev->constraints)
3013                 return status;
3014
3015         /* constraints need specific supporting methods */
3016         if (ops->set_voltage || ops->set_voltage_sel) {
3017                 status = device_create_file(dev, &dev_attr_min_microvolts);
3018                 if (status < 0)
3019                         return status;
3020                 status = device_create_file(dev, &dev_attr_max_microvolts);
3021                 if (status < 0)
3022                         return status;
3023         }
3024         if (ops->set_current_limit) {
3025                 status = device_create_file(dev, &dev_attr_min_microamps);
3026                 if (status < 0)
3027                         return status;
3028                 status = device_create_file(dev, &dev_attr_max_microamps);
3029                 if (status < 0)
3030                         return status;
3031         }
3032
3033         /* suspend mode constraints need multiple supporting methods */
3034         if (!(ops->set_suspend_enable && ops->set_suspend_disable))
3035                 return status;
3036
3037         status = device_create_file(dev, &dev_attr_suspend_standby_state);
3038         if (status < 0)
3039                 return status;
3040         status = device_create_file(dev, &dev_attr_suspend_mem_state);
3041         if (status < 0)
3042                 return status;
3043         status = device_create_file(dev, &dev_attr_suspend_disk_state);
3044         if (status < 0)
3045                 return status;
3046
3047         if (ops->set_suspend_voltage) {
3048                 status = device_create_file(dev,
3049                                 &dev_attr_suspend_standby_microvolts);
3050                 if (status < 0)
3051                         return status;
3052                 status = device_create_file(dev,
3053                                 &dev_attr_suspend_mem_microvolts);
3054                 if (status < 0)
3055                         return status;
3056                 status = device_create_file(dev,
3057                                 &dev_attr_suspend_disk_microvolts);
3058                 if (status < 0)
3059                         return status;
3060         }
3061
3062         if (ops->set_suspend_mode) {
3063                 status = device_create_file(dev,
3064                                 &dev_attr_suspend_standby_mode);
3065                 if (status < 0)
3066                         return status;
3067                 status = device_create_file(dev,
3068                                 &dev_attr_suspend_mem_mode);
3069                 if (status < 0)
3070                         return status;
3071                 status = device_create_file(dev,
3072                                 &dev_attr_suspend_disk_mode);
3073                 if (status < 0)
3074                         return status;
3075         }
3076
3077         return status;
3078 }
3079
3080 static void rdev_init_debugfs(struct regulator_dev *rdev)
3081 {
3082         rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3083         if (!rdev->debugfs) {
3084                 rdev_warn(rdev, "Failed to create debugfs directory\n");
3085                 return;
3086         }
3087
3088         debugfs_create_u32("use_count", 0444, rdev->debugfs,
3089                            &rdev->use_count);
3090         debugfs_create_u32("open_count", 0444, rdev->debugfs,
3091                            &rdev->open_count);
3092 }
3093
3094 /**
3095  * regulator_register - register regulator
3096  * @regulator_desc: regulator to register
3097  * @dev: struct device for the regulator
3098  * @init_data: platform provided init data, passed through by driver
3099  * @driver_data: private regulator data
3100  * @of_node: OpenFirmware node to parse for device tree bindings (may be
3101  *           NULL).
3102  *
3103  * Called by regulator drivers to register a regulator.
3104  * Returns 0 on success.
3105  */
3106 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
3107         struct device *dev, const struct regulator_init_data *init_data,
3108         void *driver_data, struct device_node *of_node)
3109 {
3110         const struct regulation_constraints *constraints = NULL;
3111         static atomic_t regulator_no = ATOMIC_INIT(0);
3112         struct regulator_dev *rdev;
3113         int ret, i;
3114         const char *supply = NULL;
3115         bool parent_enable = false;
3116
3117         if (regulator_desc == NULL)
3118                 return ERR_PTR(-EINVAL);
3119
3120         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3121                 return ERR_PTR(-EINVAL);
3122
3123         if (regulator_desc->type != REGULATOR_VOLTAGE &&
3124             regulator_desc->type != REGULATOR_CURRENT)
3125                 return ERR_PTR(-EINVAL);
3126
3127         /* Only one of each should be implemented */
3128         WARN_ON(regulator_desc->ops->get_voltage &&
3129                 regulator_desc->ops->get_voltage_sel);
3130         WARN_ON(regulator_desc->ops->set_voltage &&
3131                 regulator_desc->ops->set_voltage_sel);
3132
3133         /* If we're using selectors we must implement list_voltage. */
3134         if (regulator_desc->ops->get_voltage_sel &&
3135             !regulator_desc->ops->list_voltage) {
3136                 return ERR_PTR(-EINVAL);
3137         }
3138         if (regulator_desc->ops->set_voltage_sel &&
3139             !regulator_desc->ops->list_voltage) {
3140                 return ERR_PTR(-EINVAL);
3141         }
3142
3143         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3144         if (rdev == NULL)
3145                 return ERR_PTR(-ENOMEM);
3146
3147         mutex_lock(&regulator_list_mutex);
3148
3149         mutex_init(&rdev->mutex);
3150         rdev->reg_data = driver_data;
3151         rdev->owner = regulator_desc->owner;
3152         rdev->desc = regulator_desc;
3153         INIT_LIST_HEAD(&rdev->consumer_list);
3154         INIT_LIST_HEAD(&rdev->list);
3155         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3156         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3157
3158         /* preform any regulator specific init */
3159         if (init_data && init_data->regulator_init) {
3160                 ret = init_data->regulator_init(rdev->reg_data);
3161                 if (ret < 0)
3162                         goto clean;
3163         }
3164
3165         /* register with sysfs */
3166         rdev->dev.class = &regulator_class;
3167         rdev->dev.of_node = of_node;
3168         rdev->dev.parent = dev;
3169         dev_set_name(&rdev->dev, "regulator.%d",
3170                      atomic_inc_return(&regulator_no) - 1);
3171         ret = device_register(&rdev->dev);
3172         if (ret != 0) {
3173                 put_device(&rdev->dev);
3174                 goto clean;
3175         }
3176
3177         dev_set_drvdata(&rdev->dev, rdev);
3178
3179         /* set regulator constraints */
3180         if (init_data)
3181                 constraints = &init_data->constraints;
3182
3183         ret = set_machine_constraints(rdev, constraints);
3184         if (ret < 0)
3185                 goto scrub;
3186
3187         /* add attributes supported by this regulator */
3188         ret = add_regulator_attributes(rdev);
3189         if (ret < 0)
3190                 goto scrub;
3191
3192         if (init_data && init_data->supply_regulator)
3193                 supply = init_data->supply_regulator;
3194 #if 0 /* Reenable when EPROBE_DEFER is pulled. */
3195         else if (regulator_desc->supply_name)
3196                 supply = regulator_desc->supply_name;
3197 #endif
3198
3199         if (supply) {
3200                 struct regulator_dev *r;
3201
3202                 r = regulator_dev_lookup(dev, supply);
3203                 if (!r) {
3204                         dev_err(dev, "Failed to find supply %s\n", supply);
3205                         ret = -EPROBE_DEFER;
3206                         goto scrub;
3207                 }
3208
3209                 ret = set_supply(rdev, r);
3210                 if (ret < 0)
3211                         goto scrub;
3212
3213                 /* Enable supply if rail is enabled */
3214                 if (rdev->desc->ops->is_enabled &&
3215                                 rdev->desc->ops->is_enabled(rdev)) {
3216                         ret = regulator_enable(rdev->supply);
3217                         if (ret < 0)
3218                                 goto scrub;
3219                         parent_enable = true;
3220                 }
3221         }
3222
3223         /* add consumers devices */
3224         if (init_data) {
3225                 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3226                         ret = set_consumer_device_supply(rdev,
3227                                 init_data->consumer_supplies[i].dev_name,
3228                                 init_data->consumer_supplies[i].supply);
3229                         if (ret < 0) {
3230                                 dev_err(dev, "Failed to set supply %s\n",
3231                                         init_data->consumer_supplies[i].supply);
3232                                 goto unset_supplies;
3233                         }
3234                 }
3235         }
3236
3237         list_add(&rdev->list, &regulator_list);
3238
3239         rdev_init_debugfs(rdev);
3240 out:
3241         mutex_unlock(&regulator_list_mutex);
3242         return rdev;
3243
3244 unset_supplies:
3245         unset_regulator_supplies(rdev);
3246
3247         if (rdev->supply && parent_enable)
3248                 regulator_disable(rdev->supply);
3249 scrub:
3250         if (rdev->supply)
3251                 regulator_put(rdev->supply);
3252         kfree(rdev->constraints);
3253         device_unregister(&rdev->dev);
3254         /* device core frees rdev */
3255         rdev = ERR_PTR(ret);
3256         goto out;
3257
3258 clean:
3259         kfree(rdev);
3260         rdev = ERR_PTR(ret);
3261         goto out;
3262 }
3263 EXPORT_SYMBOL_GPL(regulator_register);
3264
3265 /**
3266  * regulator_unregister - unregister regulator
3267  * @rdev: regulator to unregister
3268  *
3269  * Called by regulator drivers to unregister a regulator.
3270  */
3271 void regulator_unregister(struct regulator_dev *rdev)
3272 {
3273         if (rdev == NULL)
3274                 return;
3275
3276         if (rdev->supply)
3277                 regulator_put(rdev->supply);
3278         mutex_lock(&regulator_list_mutex);
3279         debugfs_remove_recursive(rdev->debugfs);
3280         flush_work_sync(&rdev->disable_work.work);
3281         WARN_ON(rdev->open_count);
3282         unset_regulator_supplies(rdev);
3283         list_del(&rdev->list);
3284         kfree(rdev->constraints);
3285         device_unregister(&rdev->dev);
3286         mutex_unlock(&regulator_list_mutex);
3287 }
3288 EXPORT_SYMBOL_GPL(regulator_unregister);
3289
3290 /**
3291  * regulator_suspend_prepare - prepare regulators for system wide suspend
3292  * @state: system suspend state
3293  *
3294  * Configure each regulator with it's suspend operating parameters for state.
3295  * This will usually be called by machine suspend code prior to supending.
3296  */
3297 int regulator_suspend_prepare(suspend_state_t state)
3298 {
3299         struct regulator_dev *rdev;
3300         int ret = 0;
3301
3302         /* ON is handled by regulator active state */
3303         if (state == PM_SUSPEND_ON)
3304                 return -EINVAL;
3305
3306         mutex_lock(&regulator_list_mutex);
3307         list_for_each_entry(rdev, &regulator_list, list) {
3308
3309                 mutex_lock(&rdev->mutex);
3310                 ret = suspend_prepare(rdev, state);
3311                 mutex_unlock(&rdev->mutex);
3312
3313                 if (ret < 0) {
3314                         rdev_err(rdev, "failed to prepare\n");
3315                         goto out;
3316                 }
3317         }
3318 out:
3319         mutex_unlock(&regulator_list_mutex);
3320         return ret;
3321 }
3322 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3323
3324 /**
3325  * regulator_suspend_finish - resume regulators from system wide suspend
3326  *
3327  * Turn on regulators that might be turned off by regulator_suspend_prepare
3328  * and that should be turned on according to the regulators properties.
3329  */
3330 int regulator_suspend_finish(void)
3331 {
3332         struct regulator_dev *rdev;
3333         int ret = 0, error;
3334
3335         mutex_lock(&regulator_list_mutex);
3336         list_for_each_entry(rdev, &regulator_list, list) {
3337                 struct regulator_ops *ops = rdev->desc->ops;
3338
3339                 mutex_lock(&rdev->mutex);
3340                 if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
3341                                 ops->enable) {
3342                         error = ops->enable(rdev);
3343                         if (error)
3344                                 ret = error;
3345                 } else {
3346                         if (!has_full_constraints)
3347                                 goto unlock;
3348                         if (!ops->disable)
3349                                 goto unlock;
3350                         if (ops->is_enabled && !ops->is_enabled(rdev))
3351                                 goto unlock;
3352
3353                         error = ops->disable(rdev);
3354                         if (error)
3355                                 ret = error;
3356                 }
3357 unlock:
3358                 mutex_unlock(&rdev->mutex);
3359         }
3360         mutex_unlock(&regulator_list_mutex);
3361         return ret;
3362 }
3363 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3364
3365 /**
3366  * regulator_has_full_constraints - the system has fully specified constraints
3367  *
3368  * Calling this function will cause the regulator API to disable all
3369  * regulators which have a zero use count and don't have an always_on
3370  * constraint in a late_initcall.
3371  *
3372  * The intention is that this will become the default behaviour in a
3373  * future kernel release so users are encouraged to use this facility
3374  * now.
3375  */
3376 void regulator_has_full_constraints(void)
3377 {
3378         has_full_constraints = 1;
3379 }
3380 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3381
3382 /**
3383  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3384  *
3385  * Calling this function will cause the regulator API to provide a
3386  * dummy regulator to consumers if no physical regulator is found,
3387  * allowing most consumers to proceed as though a regulator were
3388  * configured.  This allows systems such as those with software
3389  * controllable regulators for the CPU core only to be brought up more
3390  * readily.
3391  */
3392 void regulator_use_dummy_regulator(void)
3393 {
3394         board_wants_dummy_regulator = true;
3395 }
3396 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3397
3398 /**
3399  * rdev_get_drvdata - get rdev regulator driver data
3400  * @rdev: regulator
3401  *
3402  * Get rdev regulator driver private data. This call can be used in the
3403  * regulator driver context.
3404  */
3405 void *rdev_get_drvdata(struct regulator_dev *rdev)
3406 {
3407         return rdev->reg_data;
3408 }
3409 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3410
3411 /**
3412  * regulator_get_drvdata - get regulator driver data
3413  * @regulator: regulator
3414  *
3415  * Get regulator driver private data. This call can be used in the consumer
3416  * driver context when non API regulator specific functions need to be called.
3417  */
3418 void *regulator_get_drvdata(struct regulator *regulator)
3419 {
3420         return regulator->rdev->reg_data;
3421 }
3422 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3423
3424 /**
3425  * regulator_set_drvdata - set regulator driver data
3426  * @regulator: regulator
3427  * @data: data
3428  */
3429 void regulator_set_drvdata(struct regulator *regulator, void *data)
3430 {
3431         regulator->rdev->reg_data = data;
3432 }
3433 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3434
3435 /**
3436  * regulator_get_id - get regulator ID
3437  * @rdev: regulator
3438  */
3439 int rdev_get_id(struct regulator_dev *rdev)
3440 {
3441         return rdev->desc->id;
3442 }
3443 EXPORT_SYMBOL_GPL(rdev_get_id);
3444
3445 struct device *rdev_get_dev(struct regulator_dev *rdev)
3446 {
3447         return &rdev->dev;
3448 }
3449 EXPORT_SYMBOL_GPL(rdev_get_dev);
3450
3451 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3452 {
3453         return reg_init_data->driver_data;
3454 }
3455 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3456
3457 #ifdef CONFIG_DEBUG_FS
3458 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3459                                     size_t count, loff_t *ppos)
3460 {
3461         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3462         ssize_t len, ret = 0;
3463         struct regulator_map *map;
3464
3465         if (!buf)
3466                 return -ENOMEM;
3467
3468         list_for_each_entry(map, &regulator_map_list, list) {
3469                 len = snprintf(buf + ret, PAGE_SIZE - ret,
3470                                "%s -> %s.%s\n",
3471                                rdev_get_name(map->regulator), map->dev_name,
3472                                map->supply);
3473                 if (len >= 0)
3474                         ret += len;
3475                 if (ret > PAGE_SIZE) {
3476                         ret = PAGE_SIZE;
3477                         break;
3478                 }
3479         }
3480
3481         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3482
3483         kfree(buf);
3484
3485         return ret;
3486 }
3487 #endif
3488
3489 static const struct file_operations supply_map_fops = {
3490 #ifdef CONFIG_DEBUG_FS
3491         .read = supply_map_read_file,
3492         .llseek = default_llseek,
3493 #endif
3494 };
3495
3496 static int __init regulator_init(void)
3497 {
3498         int ret;
3499
3500         ret = class_register(&regulator_class);
3501
3502         debugfs_root = debugfs_create_dir("regulator", NULL);
3503         if (!debugfs_root)
3504                 pr_warn("regulator: Failed to create debugfs directory\n");
3505
3506         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3507                             &supply_map_fops);
3508
3509         regulator_dummy_init();
3510
3511         return ret;
3512 }
3513
3514 /* init early to allow our consumers to complete system booting */
3515 core_initcall(regulator_init);
3516
3517 static int __init regulator_init_complete(void)
3518 {
3519         struct regulator_dev *rdev;
3520         struct regulator_ops *ops;
3521         struct regulation_constraints *c;
3522         int enabled, ret;
3523
3524         mutex_lock(&regulator_list_mutex);
3525
3526         /* If we have a full configuration then disable any regulators
3527          * which are not in use or always_on.  This will become the
3528          * default behaviour in the future.
3529          */
3530         list_for_each_entry(rdev, &regulator_list, list) {
3531                 bool parent_disable = false;
3532
3533                 ops = rdev->desc->ops;
3534                 c = rdev->constraints;
3535
3536                 if (!ops->disable || (c && c->always_on))
3537                         continue;
3538
3539                 mutex_lock(&rdev->mutex);
3540
3541                 if (rdev->use_count)
3542                         goto unlock;
3543
3544                 /* If we can't read the status assume it's on. */
3545                 if (ops->is_enabled)
3546                         enabled = ops->is_enabled(rdev);
3547                 else
3548                         enabled = 1;
3549
3550                 if (!enabled)
3551                         goto unlock;
3552
3553                 if (has_full_constraints) {
3554                         /* We log since this may kill the system if it
3555                          * goes wrong. */
3556                         rdev_info(rdev, "disabling\n");
3557                         ret = ops->disable(rdev);
3558                         if (ret != 0) {
3559                                 rdev_err(rdev, "couldn't disable: %d\n", ret);
3560                         }
3561                         if (ops->is_enabled)
3562                                 parent_disable = true;
3563                 } else {
3564                         /* The intention is that in future we will
3565                          * assume that full constraints are provided
3566                          * so warn even if we aren't going to do
3567                          * anything here.
3568                          */
3569                         rdev_warn(rdev, "incomplete constraints, leaving on\n");
3570                 }
3571
3572 unlock:
3573                 mutex_unlock(&rdev->mutex);
3574                 if (parent_disable && rdev->supply) {
3575                         ret = regulator_disable(rdev->supply);
3576                         if (ret < 0)
3577                                 rdev_err(rdev, "couldn't disable parent: %d\n",
3578                                         ret);
3579                 }
3580         }
3581
3582         mutex_unlock(&regulator_list_mutex);
3583
3584         return 0;
3585 }
3586
3587 #ifdef CONFIG_DEBUG_FS
3588 static int regulator_syncevent(struct file *file, const char __user *user_buf,
3589                                 size_t count, loff_t *ppos)
3590 {
3591         struct regulator_dev *rdev;
3592         char buffer[40];
3593         int buf_size;
3594
3595         memset(buffer, 0, sizeof(buffer));
3596         buf_size = min(count, (sizeof(buffer)-1));
3597
3598         if (copy_from_user(buffer, user_buf, buf_size))
3599                 return -EFAULT;
3600
3601         if (!strnicmp("all", buffer, 3)) {
3602
3603                 mutex_lock(&regulator_list_mutex);
3604
3605                 list_for_each_entry(rdev, &regulator_list, list) {
3606                         mutex_lock(&rdev->mutex);
3607
3608                         if (_regulator_is_enabled(rdev))
3609                                 trace_regulator_enable(rdev_get_name(rdev));
3610                         else
3611                                 trace_regulator_disable(rdev_get_name(rdev));
3612
3613                         trace_regulator_set_voltage(rdev_get_name(rdev),
3614                                 _regulator_get_voltage(rdev),
3615                                 _regulator_get_voltage(rdev));
3616
3617                         mutex_unlock(&rdev->mutex);
3618                 }
3619         }
3620
3621         mutex_unlock(&regulator_list_mutex);
3622
3623         return count;
3624 }
3625
3626 static const struct file_operations regulator_syncevent_fops = {
3627         .write          = regulator_syncevent,
3628 };
3629
3630 static int __init regulator_init_debugfs(void)
3631 {
3632         debugfs_create_file("syncevent_regulators", S_IWUSR, NULL, NULL,
3633                         &regulator_syncevent_fops);
3634
3635         return 0;
3636 }
3637
3638 late_initcall(regulator_init_debugfs);
3639 #endif
3640
3641 late_initcall(regulator_init_complete);