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