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