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