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