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