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