]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/regulator/core.c
regulator: enumerate voltages (v2)
[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_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                 /* else require explicit machine-level constraints */
713                 else if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
714                         pr_err("%s: %s '%s' voltage constraints\n",
715                                        __func__, "invalid", name);
716                         ret = -EINVAL;
717                         goto out;
718                 }
719
720                 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
721                 for (i = 0; i < count; i++) {
722                         int     value;
723
724                         value = ops->list_voltage(rdev, i);
725                         if (value <= 0)
726                                 continue;
727
728                         /* maybe adjust [min_uV..max_uV] */
729                         if (value >= cmin && value < min_uV)
730                                 min_uV = value;
731                         if (value <= cmax && value > max_uV)
732                                 max_uV = value;
733                 }
734
735                 /* final: [min_uV..max_uV] valid iff constraints valid */
736                 if (max_uV < min_uV) {
737                         pr_err("%s: %s '%s' voltage constraints\n",
738                                        __func__, "unsupportable", name);
739                         ret = -EINVAL;
740                         goto out;
741                 }
742
743                 /* use regulator's subset of machine constraints */
744                 if (constraints->min_uV < min_uV) {
745                         pr_debug("%s: override '%s' %s, %d -> %d\n",
746                                        __func__, name, "min_uV",
747                                         constraints->min_uV, min_uV);
748                         constraints->min_uV = min_uV;
749                 }
750                 if (constraints->max_uV > max_uV) {
751                         pr_debug("%s: override '%s' %s, %d -> %d\n",
752                                        __func__, name, "max_uV",
753                                         constraints->max_uV, max_uV);
754                         constraints->max_uV = max_uV;
755                 }
756         }
757
758         rdev->constraints = constraints;
759
760         /* do we need to apply the constraint voltage */
761         if (rdev->constraints->apply_uV &&
762                 rdev->constraints->min_uV == rdev->constraints->max_uV &&
763                 ops->set_voltage) {
764                 ret = ops->set_voltage(rdev,
765                         rdev->constraints->min_uV, rdev->constraints->max_uV);
766                         if (ret < 0) {
767                                 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
768                                        __func__,
769                                        rdev->constraints->min_uV, name);
770                                 rdev->constraints = NULL;
771                                 goto out;
772                         }
773         }
774
775         /* are we enabled at boot time by firmware / bootloader */
776         if (rdev->constraints->boot_on)
777                 rdev->use_count = 1;
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 always_on is set then turn the regulator on if it's not
808          * already on. */
809         if (constraints->always_on && ops->enable &&
810             ((ops->is_enabled && !ops->is_enabled(rdev)) ||
811              (!ops->is_enabled && !constraints->boot_on))) {
812                 ret = ops->enable(rdev);
813                 if (ret < 0) {
814                         printk(KERN_ERR "%s: failed to enable %s\n",
815                                __func__, name);
816                         rdev->constraints = NULL;
817                         goto out;
818                 }
819         }
820
821         print_constraints(rdev);
822 out:
823         return ret;
824 }
825
826 /**
827  * set_supply - set regulator supply regulator
828  * @rdev: regulator name
829  * @supply_rdev: supply regulator name
830  *
831  * Called by platform initialisation code to set the supply regulator for this
832  * regulator. This ensures that a regulators supply will also be enabled by the
833  * core if it's child is enabled.
834  */
835 static int set_supply(struct regulator_dev *rdev,
836         struct regulator_dev *supply_rdev)
837 {
838         int err;
839
840         err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
841                                 "supply");
842         if (err) {
843                 printk(KERN_ERR
844                        "%s: could not add device link %s err %d\n",
845                        __func__, supply_rdev->dev.kobj.name, err);
846                        goto out;
847         }
848         rdev->supply = supply_rdev;
849         list_add(&rdev->slist, &supply_rdev->supply_list);
850 out:
851         return err;
852 }
853
854 /**
855  * set_consumer_device_supply: Bind a regulator to a symbolic supply
856  * @rdev:         regulator source
857  * @consumer_dev: device the supply applies to
858  * @supply:       symbolic name for supply
859  *
860  * Allows platform initialisation code to map physical regulator
861  * sources to symbolic names for supplies for use by devices.  Devices
862  * should use these symbolic names to request regulators, avoiding the
863  * need to provide board-specific regulator names as platform data.
864  */
865 static int set_consumer_device_supply(struct regulator_dev *rdev,
866         struct device *consumer_dev, const char *supply)
867 {
868         struct regulator_map *node;
869
870         if (supply == NULL)
871                 return -EINVAL;
872
873         list_for_each_entry(node, &regulator_map_list, list) {
874                 if (consumer_dev != node->dev)
875                         continue;
876                 if (strcmp(node->supply, supply) != 0)
877                         continue;
878
879                 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
880                                 dev_name(&node->regulator->dev),
881                                 node->regulator->desc->name,
882                                 supply,
883                                 dev_name(&rdev->dev), rdev->desc->name);
884                 return -EBUSY;
885         }
886
887         node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
888         if (node == NULL)
889                 return -ENOMEM;
890
891         node->regulator = rdev;
892         node->dev = consumer_dev;
893         node->supply = supply;
894
895         list_add(&node->list, &regulator_map_list);
896         return 0;
897 }
898
899 static void unset_consumer_device_supply(struct regulator_dev *rdev,
900         struct device *consumer_dev)
901 {
902         struct regulator_map *node, *n;
903
904         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
905                 if (rdev == node->regulator &&
906                         consumer_dev == node->dev) {
907                         list_del(&node->list);
908                         kfree(node);
909                         return;
910                 }
911         }
912 }
913
914 static void unset_regulator_supplies(struct regulator_dev *rdev)
915 {
916         struct regulator_map *node, *n;
917
918         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
919                 if (rdev == node->regulator) {
920                         list_del(&node->list);
921                         kfree(node);
922                         return;
923                 }
924         }
925 }
926
927 #define REG_STR_SIZE    32
928
929 static struct regulator *create_regulator(struct regulator_dev *rdev,
930                                           struct device *dev,
931                                           const char *supply_name)
932 {
933         struct regulator *regulator;
934         char buf[REG_STR_SIZE];
935         int err, size;
936
937         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
938         if (regulator == NULL)
939                 return NULL;
940
941         mutex_lock(&rdev->mutex);
942         regulator->rdev = rdev;
943         list_add(&regulator->list, &rdev->consumer_list);
944
945         if (dev) {
946                 /* create a 'requested_microamps_name' sysfs entry */
947                 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
948                         supply_name);
949                 if (size >= REG_STR_SIZE)
950                         goto overflow_err;
951
952                 regulator->dev = dev;
953                 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
954                 if (regulator->dev_attr.attr.name == NULL)
955                         goto attr_name_err;
956
957                 regulator->dev_attr.attr.owner = THIS_MODULE;
958                 regulator->dev_attr.attr.mode = 0444;
959                 regulator->dev_attr.show = device_requested_uA_show;
960                 err = device_create_file(dev, &regulator->dev_attr);
961                 if (err < 0) {
962                         printk(KERN_WARNING "%s: could not add regulator_dev"
963                                 " load sysfs\n", __func__);
964                         goto attr_name_err;
965                 }
966
967                 /* also add a link to the device sysfs entry */
968                 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
969                                  dev->kobj.name, supply_name);
970                 if (size >= REG_STR_SIZE)
971                         goto attr_err;
972
973                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
974                 if (regulator->supply_name == NULL)
975                         goto attr_err;
976
977                 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
978                                         buf);
979                 if (err) {
980                         printk(KERN_WARNING
981                                "%s: could not add device link %s err %d\n",
982                                __func__, dev->kobj.name, err);
983                         device_remove_file(dev, &regulator->dev_attr);
984                         goto link_name_err;
985                 }
986         }
987         mutex_unlock(&rdev->mutex);
988         return regulator;
989 link_name_err:
990         kfree(regulator->supply_name);
991 attr_err:
992         device_remove_file(regulator->dev, &regulator->dev_attr);
993 attr_name_err:
994         kfree(regulator->dev_attr.attr.name);
995 overflow_err:
996         list_del(&regulator->list);
997         kfree(regulator);
998         mutex_unlock(&rdev->mutex);
999         return NULL;
1000 }
1001
1002 /**
1003  * regulator_get - lookup and obtain a reference to a regulator.
1004  * @dev: device for regulator "consumer"
1005  * @id: Supply name or regulator ID.
1006  *
1007  * Returns a struct regulator corresponding to the regulator producer,
1008  * or IS_ERR() condition containing errno.
1009  *
1010  * Use of supply names configured via regulator_set_device_supply() is
1011  * strongly encouraged.  It is recommended that the supply name used
1012  * should match the name used for the supply and/or the relevant
1013  * device pins in the datasheet.
1014  */
1015 struct regulator *regulator_get(struct device *dev, const char *id)
1016 {
1017         struct regulator_dev *rdev;
1018         struct regulator_map *map;
1019         struct regulator *regulator = ERR_PTR(-ENODEV);
1020
1021         if (id == NULL) {
1022                 printk(KERN_ERR "regulator: get() with no identifier\n");
1023                 return regulator;
1024         }
1025
1026         mutex_lock(&regulator_list_mutex);
1027
1028         list_for_each_entry(map, &regulator_map_list, list) {
1029                 if (dev == map->dev &&
1030                     strcmp(map->supply, id) == 0) {
1031                         rdev = map->regulator;
1032                         goto found;
1033                 }
1034         }
1035         printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
1036                id);
1037         mutex_unlock(&regulator_list_mutex);
1038         return regulator;
1039
1040 found:
1041         if (!try_module_get(rdev->owner))
1042                 goto out;
1043
1044         regulator = create_regulator(rdev, dev, id);
1045         if (regulator == NULL) {
1046                 regulator = ERR_PTR(-ENOMEM);
1047                 module_put(rdev->owner);
1048         }
1049
1050 out:
1051         mutex_unlock(&regulator_list_mutex);
1052         return regulator;
1053 }
1054 EXPORT_SYMBOL_GPL(regulator_get);
1055
1056 /**
1057  * regulator_put - "free" the regulator source
1058  * @regulator: regulator source
1059  *
1060  * Note: drivers must ensure that all regulator_enable calls made on this
1061  * regulator source are balanced by regulator_disable calls prior to calling
1062  * this function.
1063  */
1064 void regulator_put(struct regulator *regulator)
1065 {
1066         struct regulator_dev *rdev;
1067
1068         if (regulator == NULL || IS_ERR(regulator))
1069                 return;
1070
1071         mutex_lock(&regulator_list_mutex);
1072         rdev = regulator->rdev;
1073
1074         if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
1075                                regulator->supply_name))
1076                 _regulator_disable(rdev);
1077
1078         /* remove any sysfs entries */
1079         if (regulator->dev) {
1080                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1081                 kfree(regulator->supply_name);
1082                 device_remove_file(regulator->dev, &regulator->dev_attr);
1083                 kfree(regulator->dev_attr.attr.name);
1084         }
1085         list_del(&regulator->list);
1086         kfree(regulator);
1087
1088         module_put(rdev->owner);
1089         mutex_unlock(&regulator_list_mutex);
1090 }
1091 EXPORT_SYMBOL_GPL(regulator_put);
1092
1093 /* locks held by regulator_enable() */
1094 static int _regulator_enable(struct regulator_dev *rdev)
1095 {
1096         int ret = -EINVAL;
1097
1098         if (!rdev->constraints) {
1099                 printk(KERN_ERR "%s: %s has no constraints\n",
1100                        __func__, rdev->desc->name);
1101                 return ret;
1102         }
1103
1104         /* do we need to enable the supply regulator first */
1105         if (rdev->supply) {
1106                 ret = _regulator_enable(rdev->supply);
1107                 if (ret < 0) {
1108                         printk(KERN_ERR "%s: failed to enable %s: %d\n",
1109                                __func__, rdev->desc->name, ret);
1110                         return ret;
1111                 }
1112         }
1113
1114         /* check voltage and requested load before enabling */
1115         if (rdev->desc->ops->enable) {
1116
1117                 if (rdev->constraints &&
1118                         (rdev->constraints->valid_ops_mask &
1119                         REGULATOR_CHANGE_DRMS))
1120                         drms_uA_update(rdev);
1121
1122                 ret = rdev->desc->ops->enable(rdev);
1123                 if (ret < 0) {
1124                         printk(KERN_ERR "%s: failed to enable %s: %d\n",
1125                                __func__, rdev->desc->name, ret);
1126                         return ret;
1127                 }
1128                 rdev->use_count++;
1129                 return ret;
1130         }
1131
1132         return ret;
1133 }
1134
1135 /**
1136  * regulator_enable - enable regulator output
1137  * @regulator: regulator source
1138  *
1139  * Request that the regulator be enabled with the regulator output at
1140  * the predefined voltage or current value.  Calls to regulator_enable()
1141  * must be balanced with calls to regulator_disable().
1142  *
1143  * NOTE: the output value can be set by other drivers, boot loader or may be
1144  * hardwired in the regulator.
1145  */
1146 int regulator_enable(struct regulator *regulator)
1147 {
1148         struct regulator_dev *rdev = regulator->rdev;
1149         int ret = 0;
1150
1151         mutex_lock(&rdev->mutex);
1152         if (regulator->enabled == 0)
1153                 ret = _regulator_enable(rdev);
1154         else if (regulator->enabled < 0)
1155                 ret = -EIO;
1156         if (ret == 0)
1157                 regulator->enabled++;
1158         mutex_unlock(&rdev->mutex);
1159         return ret;
1160 }
1161 EXPORT_SYMBOL_GPL(regulator_enable);
1162
1163 /* locks held by regulator_disable() */
1164 static int _regulator_disable(struct regulator_dev *rdev)
1165 {
1166         int ret = 0;
1167
1168         /* are we the last user and permitted to disable ? */
1169         if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1170
1171                 /* we are last user */
1172                 if (rdev->desc->ops->disable) {
1173                         ret = rdev->desc->ops->disable(rdev);
1174                         if (ret < 0) {
1175                                 printk(KERN_ERR "%s: failed to disable %s\n",
1176                                        __func__, rdev->desc->name);
1177                                 return ret;
1178                         }
1179                 }
1180
1181                 /* decrease our supplies ref count and disable if required */
1182                 if (rdev->supply)
1183                         _regulator_disable(rdev->supply);
1184
1185                 rdev->use_count = 0;
1186         } else if (rdev->use_count > 1) {
1187
1188                 if (rdev->constraints &&
1189                         (rdev->constraints->valid_ops_mask &
1190                         REGULATOR_CHANGE_DRMS))
1191                         drms_uA_update(rdev);
1192
1193                 rdev->use_count--;
1194         }
1195         return ret;
1196 }
1197
1198 /**
1199  * regulator_disable - disable regulator output
1200  * @regulator: regulator source
1201  *
1202  * Disable the regulator output voltage or current.  Calls to
1203  * regulator_enable() must be balanced with calls to
1204  * regulator_disable().
1205  *
1206  * NOTE: this will only disable the regulator output if no other consumer
1207  * devices have it enabled, the regulator device supports disabling and
1208  * machine constraints permit this operation.
1209  */
1210 int regulator_disable(struct regulator *regulator)
1211 {
1212         struct regulator_dev *rdev = regulator->rdev;
1213         int ret = 0;
1214
1215         mutex_lock(&rdev->mutex);
1216         if (regulator->enabled == 1) {
1217                 ret = _regulator_disable(rdev);
1218                 if (ret == 0)
1219                         regulator->uA_load = 0;
1220         } else if (WARN(regulator->enabled <= 0,
1221                         "unbalanced disables for supply %s\n",
1222                         regulator->supply_name))
1223                 ret = -EIO;
1224         if (ret == 0)
1225                 regulator->enabled--;
1226         mutex_unlock(&rdev->mutex);
1227         return ret;
1228 }
1229 EXPORT_SYMBOL_GPL(regulator_disable);
1230
1231 /* locks held by regulator_force_disable() */
1232 static int _regulator_force_disable(struct regulator_dev *rdev)
1233 {
1234         int ret = 0;
1235
1236         /* force disable */
1237         if (rdev->desc->ops->disable) {
1238                 /* ah well, who wants to live forever... */
1239                 ret = rdev->desc->ops->disable(rdev);
1240                 if (ret < 0) {
1241                         printk(KERN_ERR "%s: failed to force disable %s\n",
1242                                __func__, rdev->desc->name);
1243                         return ret;
1244                 }
1245                 /* notify other consumers that power has been forced off */
1246                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1247                         NULL);
1248         }
1249
1250         /* decrease our supplies ref count and disable if required */
1251         if (rdev->supply)
1252                 _regulator_disable(rdev->supply);
1253
1254         rdev->use_count = 0;
1255         return ret;
1256 }
1257
1258 /**
1259  * regulator_force_disable - force disable regulator output
1260  * @regulator: regulator source
1261  *
1262  * Forcibly disable the regulator output voltage or current.
1263  * NOTE: this *will* disable the regulator output even if other consumer
1264  * devices have it enabled. This should be used for situations when device
1265  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1266  */
1267 int regulator_force_disable(struct regulator *regulator)
1268 {
1269         int ret;
1270
1271         mutex_lock(&regulator->rdev->mutex);
1272         regulator->enabled = 0;
1273         regulator->uA_load = 0;
1274         ret = _regulator_force_disable(regulator->rdev);
1275         mutex_unlock(&regulator->rdev->mutex);
1276         return ret;
1277 }
1278 EXPORT_SYMBOL_GPL(regulator_force_disable);
1279
1280 static int _regulator_is_enabled(struct regulator_dev *rdev)
1281 {
1282         int ret;
1283
1284         mutex_lock(&rdev->mutex);
1285
1286         /* sanity check */
1287         if (!rdev->desc->ops->is_enabled) {
1288                 ret = -EINVAL;
1289                 goto out;
1290         }
1291
1292         ret = rdev->desc->ops->is_enabled(rdev);
1293 out:
1294         mutex_unlock(&rdev->mutex);
1295         return ret;
1296 }
1297
1298 /**
1299  * regulator_is_enabled - is the regulator output enabled
1300  * @regulator: regulator source
1301  *
1302  * Returns positive if the regulator driver backing the source/client
1303  * has requested that the device be enabled, zero if it hasn't, else a
1304  * negative errno code.
1305  *
1306  * Note that the device backing this regulator handle can have multiple
1307  * users, so it might be enabled even if regulator_enable() was never
1308  * called for this particular source.
1309  */
1310 int regulator_is_enabled(struct regulator *regulator)
1311 {
1312         return _regulator_is_enabled(regulator->rdev);
1313 }
1314 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1315
1316 /**
1317  * regulator_count_voltages - count regulator_list_voltage() selectors
1318  * @regulator: regulator source
1319  *
1320  * Returns number of selectors, or negative errno.  Selectors are
1321  * numbered starting at zero, and typically correspond to bitfields
1322  * in hardware registers.
1323  */
1324 int regulator_count_voltages(struct regulator *regulator)
1325 {
1326         struct regulator_dev    *rdev = regulator->rdev;
1327
1328         return rdev->desc->n_voltages ? : -EINVAL;
1329 }
1330 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1331
1332 /**
1333  * regulator_list_voltage - enumerate supported voltages
1334  * @regulator: regulator source
1335  * @selector: identify voltage to list
1336  * Context: can sleep
1337  *
1338  * Returns a voltage that can be passed to @regulator_set_voltage(),
1339  * zero if this selector code can't be used on this sytem, or a
1340  * negative errno.
1341  */
1342 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1343 {
1344         struct regulator_dev    *rdev = regulator->rdev;
1345         struct regulator_ops    *ops = rdev->desc->ops;
1346         int                     ret;
1347
1348         if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1349                 return -EINVAL;
1350
1351         mutex_lock(&rdev->mutex);
1352         ret = ops->list_voltage(rdev, selector);
1353         mutex_unlock(&rdev->mutex);
1354
1355         if (ret > 0) {
1356                 if (ret < rdev->constraints->min_uV)
1357                         ret = 0;
1358                 else if (ret > rdev->constraints->max_uV)
1359                         ret = 0;
1360         }
1361
1362         return ret;
1363 }
1364 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1365
1366 /**
1367  * regulator_set_voltage - set regulator output voltage
1368  * @regulator: regulator source
1369  * @min_uV: Minimum required voltage in uV
1370  * @max_uV: Maximum acceptable voltage in uV
1371  *
1372  * Sets a voltage regulator to the desired output voltage. This can be set
1373  * during any regulator state. IOW, regulator can be disabled or enabled.
1374  *
1375  * If the regulator is enabled then the voltage will change to the new value
1376  * immediately otherwise if the regulator is disabled the regulator will
1377  * output at the new voltage when enabled.
1378  *
1379  * NOTE: If the regulator is shared between several devices then the lowest
1380  * request voltage that meets the system constraints will be used.
1381  * Regulator system constraints must be set for this regulator before
1382  * calling this function otherwise this call will fail.
1383  */
1384 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1385 {
1386         struct regulator_dev *rdev = regulator->rdev;
1387         int ret;
1388
1389         mutex_lock(&rdev->mutex);
1390
1391         /* sanity check */
1392         if (!rdev->desc->ops->set_voltage) {
1393                 ret = -EINVAL;
1394                 goto out;
1395         }
1396
1397         /* constraints check */
1398         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1399         if (ret < 0)
1400                 goto out;
1401         regulator->min_uV = min_uV;
1402         regulator->max_uV = max_uV;
1403         ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1404
1405 out:
1406         _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1407         mutex_unlock(&rdev->mutex);
1408         return ret;
1409 }
1410 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1411
1412 static int _regulator_get_voltage(struct regulator_dev *rdev)
1413 {
1414         /* sanity check */
1415         if (rdev->desc->ops->get_voltage)
1416                 return rdev->desc->ops->get_voltage(rdev);
1417         else
1418                 return -EINVAL;
1419 }
1420
1421 /**
1422  * regulator_get_voltage - get regulator output voltage
1423  * @regulator: regulator source
1424  *
1425  * This returns the current regulator voltage in uV.
1426  *
1427  * NOTE: If the regulator is disabled it will return the voltage value. This
1428  * function should not be used to determine regulator state.
1429  */
1430 int regulator_get_voltage(struct regulator *regulator)
1431 {
1432         int ret;
1433
1434         mutex_lock(&regulator->rdev->mutex);
1435
1436         ret = _regulator_get_voltage(regulator->rdev);
1437
1438         mutex_unlock(&regulator->rdev->mutex);
1439
1440         return ret;
1441 }
1442 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1443
1444 /**
1445  * regulator_set_current_limit - set regulator output current limit
1446  * @regulator: regulator source
1447  * @min_uA: Minimuum supported current in uA
1448  * @max_uA: Maximum supported current in uA
1449  *
1450  * Sets current sink to the desired output current. This can be set during
1451  * any regulator state. IOW, regulator can be disabled or enabled.
1452  *
1453  * If the regulator is enabled then the current will change to the new value
1454  * immediately otherwise if the regulator is disabled the regulator will
1455  * output at the new current when enabled.
1456  *
1457  * NOTE: Regulator system constraints must be set for this regulator before
1458  * calling this function otherwise this call will fail.
1459  */
1460 int regulator_set_current_limit(struct regulator *regulator,
1461                                int min_uA, int max_uA)
1462 {
1463         struct regulator_dev *rdev = regulator->rdev;
1464         int ret;
1465
1466         mutex_lock(&rdev->mutex);
1467
1468         /* sanity check */
1469         if (!rdev->desc->ops->set_current_limit) {
1470                 ret = -EINVAL;
1471                 goto out;
1472         }
1473
1474         /* constraints check */
1475         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1476         if (ret < 0)
1477                 goto out;
1478
1479         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1480 out:
1481         mutex_unlock(&rdev->mutex);
1482         return ret;
1483 }
1484 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1485
1486 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1487 {
1488         int ret;
1489
1490         mutex_lock(&rdev->mutex);
1491
1492         /* sanity check */
1493         if (!rdev->desc->ops->get_current_limit) {
1494                 ret = -EINVAL;
1495                 goto out;
1496         }
1497
1498         ret = rdev->desc->ops->get_current_limit(rdev);
1499 out:
1500         mutex_unlock(&rdev->mutex);
1501         return ret;
1502 }
1503
1504 /**
1505  * regulator_get_current_limit - get regulator output current
1506  * @regulator: regulator source
1507  *
1508  * This returns the current supplied by the specified current sink in uA.
1509  *
1510  * NOTE: If the regulator is disabled it will return the current value. This
1511  * function should not be used to determine regulator state.
1512  */
1513 int regulator_get_current_limit(struct regulator *regulator)
1514 {
1515         return _regulator_get_current_limit(regulator->rdev);
1516 }
1517 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1518
1519 /**
1520  * regulator_set_mode - set regulator operating mode
1521  * @regulator: regulator source
1522  * @mode: operating mode - one of the REGULATOR_MODE constants
1523  *
1524  * Set regulator operating mode to increase regulator efficiency or improve
1525  * regulation performance.
1526  *
1527  * NOTE: Regulator system constraints must be set for this regulator before
1528  * calling this function otherwise this call will fail.
1529  */
1530 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1531 {
1532         struct regulator_dev *rdev = regulator->rdev;
1533         int ret;
1534
1535         mutex_lock(&rdev->mutex);
1536
1537         /* sanity check */
1538         if (!rdev->desc->ops->set_mode) {
1539                 ret = -EINVAL;
1540                 goto out;
1541         }
1542
1543         /* constraints check */
1544         ret = regulator_check_mode(rdev, mode);
1545         if (ret < 0)
1546                 goto out;
1547
1548         ret = rdev->desc->ops->set_mode(rdev, mode);
1549 out:
1550         mutex_unlock(&rdev->mutex);
1551         return ret;
1552 }
1553 EXPORT_SYMBOL_GPL(regulator_set_mode);
1554
1555 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1556 {
1557         int ret;
1558
1559         mutex_lock(&rdev->mutex);
1560
1561         /* sanity check */
1562         if (!rdev->desc->ops->get_mode) {
1563                 ret = -EINVAL;
1564                 goto out;
1565         }
1566
1567         ret = rdev->desc->ops->get_mode(rdev);
1568 out:
1569         mutex_unlock(&rdev->mutex);
1570         return ret;
1571 }
1572
1573 /**
1574  * regulator_get_mode - get regulator operating mode
1575  * @regulator: regulator source
1576  *
1577  * Get the current regulator operating mode.
1578  */
1579 unsigned int regulator_get_mode(struct regulator *regulator)
1580 {
1581         return _regulator_get_mode(regulator->rdev);
1582 }
1583 EXPORT_SYMBOL_GPL(regulator_get_mode);
1584
1585 /**
1586  * regulator_set_optimum_mode - set regulator optimum operating mode
1587  * @regulator: regulator source
1588  * @uA_load: load current
1589  *
1590  * Notifies the regulator core of a new device load. This is then used by
1591  * DRMS (if enabled by constraints) to set the most efficient regulator
1592  * operating mode for the new regulator loading.
1593  *
1594  * Consumer devices notify their supply regulator of the maximum power
1595  * they will require (can be taken from device datasheet in the power
1596  * consumption tables) when they change operational status and hence power
1597  * state. Examples of operational state changes that can affect power
1598  * consumption are :-
1599  *
1600  *    o Device is opened / closed.
1601  *    o Device I/O is about to begin or has just finished.
1602  *    o Device is idling in between work.
1603  *
1604  * This information is also exported via sysfs to userspace.
1605  *
1606  * DRMS will sum the total requested load on the regulator and change
1607  * to the most efficient operating mode if platform constraints allow.
1608  *
1609  * Returns the new regulator mode or error.
1610  */
1611 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1612 {
1613         struct regulator_dev *rdev = regulator->rdev;
1614         struct regulator *consumer;
1615         int ret, output_uV, input_uV, total_uA_load = 0;
1616         unsigned int mode;
1617
1618         mutex_lock(&rdev->mutex);
1619
1620         regulator->uA_load = uA_load;
1621         ret = regulator_check_drms(rdev);
1622         if (ret < 0)
1623                 goto out;
1624         ret = -EINVAL;
1625
1626         /* sanity check */
1627         if (!rdev->desc->ops->get_optimum_mode)
1628                 goto out;
1629
1630         /* get output voltage */
1631         output_uV = rdev->desc->ops->get_voltage(rdev);
1632         if (output_uV <= 0) {
1633                 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1634                         __func__, rdev->desc->name);
1635                 goto out;
1636         }
1637
1638         /* get input voltage */
1639         if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1640                 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1641         else
1642                 input_uV = rdev->constraints->input_uV;
1643         if (input_uV <= 0) {
1644                 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1645                         __func__, rdev->desc->name);
1646                 goto out;
1647         }
1648
1649         /* calc total requested load for this regulator */
1650         list_for_each_entry(consumer, &rdev->consumer_list, list)
1651             total_uA_load += consumer->uA_load;
1652
1653         mode = rdev->desc->ops->get_optimum_mode(rdev,
1654                                                  input_uV, output_uV,
1655                                                  total_uA_load);
1656         ret = regulator_check_mode(rdev, mode);
1657         if (ret < 0) {
1658                 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1659                         " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1660                         total_uA_load, input_uV, output_uV);
1661                 goto out;
1662         }
1663
1664         ret = rdev->desc->ops->set_mode(rdev, mode);
1665         if (ret < 0) {
1666                 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1667                         __func__, mode, rdev->desc->name);
1668                 goto out;
1669         }
1670         ret = mode;
1671 out:
1672         mutex_unlock(&rdev->mutex);
1673         return ret;
1674 }
1675 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1676
1677 /**
1678  * regulator_register_notifier - register regulator event notifier
1679  * @regulator: regulator source
1680  * @nb: notifier block
1681  *
1682  * Register notifier block to receive regulator events.
1683  */
1684 int regulator_register_notifier(struct regulator *regulator,
1685                               struct notifier_block *nb)
1686 {
1687         return blocking_notifier_chain_register(&regulator->rdev->notifier,
1688                                                 nb);
1689 }
1690 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1691
1692 /**
1693  * regulator_unregister_notifier - unregister regulator event notifier
1694  * @regulator: regulator source
1695  * @nb: notifier block
1696  *
1697  * Unregister regulator event notifier block.
1698  */
1699 int regulator_unregister_notifier(struct regulator *regulator,
1700                                 struct notifier_block *nb)
1701 {
1702         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1703                                                   nb);
1704 }
1705 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1706
1707 /* notify regulator consumers and downstream regulator consumers.
1708  * Note mutex must be held by caller.
1709  */
1710 static void _notifier_call_chain(struct regulator_dev *rdev,
1711                                   unsigned long event, void *data)
1712 {
1713         struct regulator_dev *_rdev;
1714
1715         /* call rdev chain first */
1716         blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1717
1718         /* now notify regulator we supply */
1719         list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1720           mutex_lock(&_rdev->mutex);
1721           _notifier_call_chain(_rdev, event, data);
1722           mutex_unlock(&_rdev->mutex);
1723         }
1724 }
1725
1726 /**
1727  * regulator_bulk_get - get multiple regulator consumers
1728  *
1729  * @dev:           Device to supply
1730  * @num_consumers: Number of consumers to register
1731  * @consumers:     Configuration of consumers; clients are stored here.
1732  *
1733  * @return 0 on success, an errno on failure.
1734  *
1735  * This helper function allows drivers to get several regulator
1736  * consumers in one operation.  If any of the regulators cannot be
1737  * acquired then any regulators that were allocated will be freed
1738  * before returning to the caller.
1739  */
1740 int regulator_bulk_get(struct device *dev, int num_consumers,
1741                        struct regulator_bulk_data *consumers)
1742 {
1743         int i;
1744         int ret;
1745
1746         for (i = 0; i < num_consumers; i++)
1747                 consumers[i].consumer = NULL;
1748
1749         for (i = 0; i < num_consumers; i++) {
1750                 consumers[i].consumer = regulator_get(dev,
1751                                                       consumers[i].supply);
1752                 if (IS_ERR(consumers[i].consumer)) {
1753                         dev_err(dev, "Failed to get supply '%s'\n",
1754                                 consumers[i].supply);
1755                         ret = PTR_ERR(consumers[i].consumer);
1756                         consumers[i].consumer = NULL;
1757                         goto err;
1758                 }
1759         }
1760
1761         return 0;
1762
1763 err:
1764         for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1765                 regulator_put(consumers[i].consumer);
1766
1767         return ret;
1768 }
1769 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1770
1771 /**
1772  * regulator_bulk_enable - enable multiple regulator consumers
1773  *
1774  * @num_consumers: Number of consumers
1775  * @consumers:     Consumer data; clients are stored here.
1776  * @return         0 on success, an errno on failure
1777  *
1778  * This convenience API allows consumers to enable multiple regulator
1779  * clients in a single API call.  If any consumers cannot be enabled
1780  * then any others that were enabled will be disabled again prior to
1781  * return.
1782  */
1783 int regulator_bulk_enable(int num_consumers,
1784                           struct regulator_bulk_data *consumers)
1785 {
1786         int i;
1787         int ret;
1788
1789         for (i = 0; i < num_consumers; i++) {
1790                 ret = regulator_enable(consumers[i].consumer);
1791                 if (ret != 0)
1792                         goto err;
1793         }
1794
1795         return 0;
1796
1797 err:
1798         printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1799         for (i = 0; i < num_consumers; i++)
1800                 regulator_disable(consumers[i].consumer);
1801
1802         return ret;
1803 }
1804 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1805
1806 /**
1807  * regulator_bulk_disable - disable multiple regulator consumers
1808  *
1809  * @num_consumers: Number of consumers
1810  * @consumers:     Consumer data; clients are stored here.
1811  * @return         0 on success, an errno on failure
1812  *
1813  * This convenience API allows consumers to disable multiple regulator
1814  * clients in a single API call.  If any consumers cannot be enabled
1815  * then any others that were disabled will be disabled again prior to
1816  * return.
1817  */
1818 int regulator_bulk_disable(int num_consumers,
1819                            struct regulator_bulk_data *consumers)
1820 {
1821         int i;
1822         int ret;
1823
1824         for (i = 0; i < num_consumers; i++) {
1825                 ret = regulator_disable(consumers[i].consumer);
1826                 if (ret != 0)
1827                         goto err;
1828         }
1829
1830         return 0;
1831
1832 err:
1833         printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1834         for (i = 0; i < num_consumers; i++)
1835                 regulator_enable(consumers[i].consumer);
1836
1837         return ret;
1838 }
1839 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1840
1841 /**
1842  * regulator_bulk_free - free multiple regulator consumers
1843  *
1844  * @num_consumers: Number of consumers
1845  * @consumers:     Consumer data; clients are stored here.
1846  *
1847  * This convenience API allows consumers to free multiple regulator
1848  * clients in a single API call.
1849  */
1850 void regulator_bulk_free(int num_consumers,
1851                          struct regulator_bulk_data *consumers)
1852 {
1853         int i;
1854
1855         for (i = 0; i < num_consumers; i++) {
1856                 regulator_put(consumers[i].consumer);
1857                 consumers[i].consumer = NULL;
1858         }
1859 }
1860 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1861
1862 /**
1863  * regulator_notifier_call_chain - call regulator event notifier
1864  * @rdev: regulator source
1865  * @event: notifier block
1866  * @data: callback-specific data.
1867  *
1868  * Called by regulator drivers to notify clients a regulator event has
1869  * occurred. We also notify regulator clients downstream.
1870  * Note lock must be held by caller.
1871  */
1872 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1873                                   unsigned long event, void *data)
1874 {
1875         _notifier_call_chain(rdev, event, data);
1876         return NOTIFY_DONE;
1877
1878 }
1879 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1880
1881 /*
1882  * To avoid cluttering sysfs (and memory) with useless state, only
1883  * create attributes that can be meaningfully displayed.
1884  */
1885 static int add_regulator_attributes(struct regulator_dev *rdev)
1886 {
1887         struct device           *dev = &rdev->dev;
1888         struct regulator_ops    *ops = rdev->desc->ops;
1889         int                     status = 0;
1890
1891         /* some attributes need specific methods to be displayed */
1892         if (ops->get_voltage) {
1893                 status = device_create_file(dev, &dev_attr_microvolts);
1894                 if (status < 0)
1895                         return status;
1896         }
1897         if (ops->get_current_limit) {
1898                 status = device_create_file(dev, &dev_attr_microamps);
1899                 if (status < 0)
1900                         return status;
1901         }
1902         if (ops->get_mode) {
1903                 status = device_create_file(dev, &dev_attr_opmode);
1904                 if (status < 0)
1905                         return status;
1906         }
1907         if (ops->is_enabled) {
1908                 status = device_create_file(dev, &dev_attr_state);
1909                 if (status < 0)
1910                         return status;
1911         }
1912         if (ops->get_status) {
1913                 status = device_create_file(dev, &dev_attr_status);
1914                 if (status < 0)
1915                         return status;
1916         }
1917
1918         /* some attributes are type-specific */
1919         if (rdev->desc->type == REGULATOR_CURRENT) {
1920                 status = device_create_file(dev, &dev_attr_requested_microamps);
1921                 if (status < 0)
1922                         return status;
1923         }
1924
1925         /* all the other attributes exist to support constraints;
1926          * don't show them if there are no constraints, or if the
1927          * relevant supporting methods are missing.
1928          */
1929         if (!rdev->constraints)
1930                 return status;
1931
1932         /* constraints need specific supporting methods */
1933         if (ops->set_voltage) {
1934                 status = device_create_file(dev, &dev_attr_min_microvolts);
1935                 if (status < 0)
1936                         return status;
1937                 status = device_create_file(dev, &dev_attr_max_microvolts);
1938                 if (status < 0)
1939                         return status;
1940         }
1941         if (ops->set_current_limit) {
1942                 status = device_create_file(dev, &dev_attr_min_microamps);
1943                 if (status < 0)
1944                         return status;
1945                 status = device_create_file(dev, &dev_attr_max_microamps);
1946                 if (status < 0)
1947                         return status;
1948         }
1949
1950         /* suspend mode constraints need multiple supporting methods */
1951         if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1952                 return status;
1953
1954         status = device_create_file(dev, &dev_attr_suspend_standby_state);
1955         if (status < 0)
1956                 return status;
1957         status = device_create_file(dev, &dev_attr_suspend_mem_state);
1958         if (status < 0)
1959                 return status;
1960         status = device_create_file(dev, &dev_attr_suspend_disk_state);
1961         if (status < 0)
1962                 return status;
1963
1964         if (ops->set_suspend_voltage) {
1965                 status = device_create_file(dev,
1966                                 &dev_attr_suspend_standby_microvolts);
1967                 if (status < 0)
1968                         return status;
1969                 status = device_create_file(dev,
1970                                 &dev_attr_suspend_mem_microvolts);
1971                 if (status < 0)
1972                         return status;
1973                 status = device_create_file(dev,
1974                                 &dev_attr_suspend_disk_microvolts);
1975                 if (status < 0)
1976                         return status;
1977         }
1978
1979         if (ops->set_suspend_mode) {
1980                 status = device_create_file(dev,
1981                                 &dev_attr_suspend_standby_mode);
1982                 if (status < 0)
1983                         return status;
1984                 status = device_create_file(dev,
1985                                 &dev_attr_suspend_mem_mode);
1986                 if (status < 0)
1987                         return status;
1988                 status = device_create_file(dev,
1989                                 &dev_attr_suspend_disk_mode);
1990                 if (status < 0)
1991                         return status;
1992         }
1993
1994         return status;
1995 }
1996
1997 /**
1998  * regulator_register - register regulator
1999  * @regulator_desc: regulator to register
2000  * @dev: struct device for the regulator
2001  * @init_data: platform provided init data, passed through by driver
2002  * @driver_data: private regulator data
2003  *
2004  * Called by regulator drivers to register a regulator.
2005  * Returns 0 on success.
2006  */
2007 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2008         struct device *dev, struct regulator_init_data *init_data,
2009         void *driver_data)
2010 {
2011         static atomic_t regulator_no = ATOMIC_INIT(0);
2012         struct regulator_dev *rdev;
2013         int ret, i;
2014
2015         if (regulator_desc == NULL)
2016                 return ERR_PTR(-EINVAL);
2017
2018         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2019                 return ERR_PTR(-EINVAL);
2020
2021         if (!regulator_desc->type == REGULATOR_VOLTAGE &&
2022             !regulator_desc->type == REGULATOR_CURRENT)
2023                 return ERR_PTR(-EINVAL);
2024
2025         if (!init_data)
2026                 return ERR_PTR(-EINVAL);
2027
2028         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2029         if (rdev == NULL)
2030                 return ERR_PTR(-ENOMEM);
2031
2032         mutex_lock(&regulator_list_mutex);
2033
2034         mutex_init(&rdev->mutex);
2035         rdev->reg_data = driver_data;
2036         rdev->owner = regulator_desc->owner;
2037         rdev->desc = regulator_desc;
2038         INIT_LIST_HEAD(&rdev->consumer_list);
2039         INIT_LIST_HEAD(&rdev->supply_list);
2040         INIT_LIST_HEAD(&rdev->list);
2041         INIT_LIST_HEAD(&rdev->slist);
2042         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2043
2044         /* preform any regulator specific init */
2045         if (init_data->regulator_init) {
2046                 ret = init_data->regulator_init(rdev->reg_data);
2047                 if (ret < 0)
2048                         goto clean;
2049         }
2050
2051         /* register with sysfs */
2052         rdev->dev.class = &regulator_class;
2053         rdev->dev.parent = dev;
2054         dev_set_name(&rdev->dev, "regulator.%d",
2055                      atomic_inc_return(&regulator_no) - 1);
2056         ret = device_register(&rdev->dev);
2057         if (ret != 0)
2058                 goto clean;
2059
2060         dev_set_drvdata(&rdev->dev, rdev);
2061
2062         /* set regulator constraints */
2063         ret = set_machine_constraints(rdev, &init_data->constraints);
2064         if (ret < 0)
2065                 goto scrub;
2066
2067         /* add attributes supported by this regulator */
2068         ret = add_regulator_attributes(rdev);
2069         if (ret < 0)
2070                 goto scrub;
2071
2072         /* set supply regulator if it exists */
2073         if (init_data->supply_regulator_dev) {
2074                 ret = set_supply(rdev,
2075                         dev_get_drvdata(init_data->supply_regulator_dev));
2076                 if (ret < 0)
2077                         goto scrub;
2078         }
2079
2080         /* add consumers devices */
2081         for (i = 0; i < init_data->num_consumer_supplies; i++) {
2082                 ret = set_consumer_device_supply(rdev,
2083                         init_data->consumer_supplies[i].dev,
2084                         init_data->consumer_supplies[i].supply);
2085                 if (ret < 0) {
2086                         for (--i; i >= 0; i--)
2087                                 unset_consumer_device_supply(rdev,
2088                                         init_data->consumer_supplies[i].dev);
2089                         goto scrub;
2090                 }
2091         }
2092
2093         list_add(&rdev->list, &regulator_list);
2094 out:
2095         mutex_unlock(&regulator_list_mutex);
2096         return rdev;
2097
2098 scrub:
2099         device_unregister(&rdev->dev);
2100 clean:
2101         kfree(rdev);
2102         rdev = ERR_PTR(ret);
2103         goto out;
2104 }
2105 EXPORT_SYMBOL_GPL(regulator_register);
2106
2107 /**
2108  * regulator_unregister - unregister regulator
2109  * @rdev: regulator to unregister
2110  *
2111  * Called by regulator drivers to unregister a regulator.
2112  */
2113 void regulator_unregister(struct regulator_dev *rdev)
2114 {
2115         if (rdev == NULL)
2116                 return;
2117
2118         mutex_lock(&regulator_list_mutex);
2119         unset_regulator_supplies(rdev);
2120         list_del(&rdev->list);
2121         if (rdev->supply)
2122                 sysfs_remove_link(&rdev->dev.kobj, "supply");
2123         device_unregister(&rdev->dev);
2124         mutex_unlock(&regulator_list_mutex);
2125 }
2126 EXPORT_SYMBOL_GPL(regulator_unregister);
2127
2128 /**
2129  * regulator_suspend_prepare - prepare regulators for system wide suspend
2130  * @state: system suspend state
2131  *
2132  * Configure each regulator with it's suspend operating parameters for state.
2133  * This will usually be called by machine suspend code prior to supending.
2134  */
2135 int regulator_suspend_prepare(suspend_state_t state)
2136 {
2137         struct regulator_dev *rdev;
2138         int ret = 0;
2139
2140         /* ON is handled by regulator active state */
2141         if (state == PM_SUSPEND_ON)
2142                 return -EINVAL;
2143
2144         mutex_lock(&regulator_list_mutex);
2145         list_for_each_entry(rdev, &regulator_list, list) {
2146
2147                 mutex_lock(&rdev->mutex);
2148                 ret = suspend_prepare(rdev, state);
2149                 mutex_unlock(&rdev->mutex);
2150
2151                 if (ret < 0) {
2152                         printk(KERN_ERR "%s: failed to prepare %s\n",
2153                                 __func__, rdev->desc->name);
2154                         goto out;
2155                 }
2156         }
2157 out:
2158         mutex_unlock(&regulator_list_mutex);
2159         return ret;
2160 }
2161 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2162
2163 /**
2164  * rdev_get_drvdata - get rdev regulator driver data
2165  * @rdev: regulator
2166  *
2167  * Get rdev regulator driver private data. This call can be used in the
2168  * regulator driver context.
2169  */
2170 void *rdev_get_drvdata(struct regulator_dev *rdev)
2171 {
2172         return rdev->reg_data;
2173 }
2174 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2175
2176 /**
2177  * regulator_get_drvdata - get regulator driver data
2178  * @regulator: regulator
2179  *
2180  * Get regulator driver private data. This call can be used in the consumer
2181  * driver context when non API regulator specific functions need to be called.
2182  */
2183 void *regulator_get_drvdata(struct regulator *regulator)
2184 {
2185         return regulator->rdev->reg_data;
2186 }
2187 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2188
2189 /**
2190  * regulator_set_drvdata - set regulator driver data
2191  * @regulator: regulator
2192  * @data: data
2193  */
2194 void regulator_set_drvdata(struct regulator *regulator, void *data)
2195 {
2196         regulator->rdev->reg_data = data;
2197 }
2198 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2199
2200 /**
2201  * regulator_get_id - get regulator ID
2202  * @rdev: regulator
2203  */
2204 int rdev_get_id(struct regulator_dev *rdev)
2205 {
2206         return rdev->desc->id;
2207 }
2208 EXPORT_SYMBOL_GPL(rdev_get_id);
2209
2210 struct device *rdev_get_dev(struct regulator_dev *rdev)
2211 {
2212         return &rdev->dev;
2213 }
2214 EXPORT_SYMBOL_GPL(rdev_get_dev);
2215
2216 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2217 {
2218         return reg_init_data->driver_data;
2219 }
2220 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2221
2222 static int __init regulator_init(void)
2223 {
2224         printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2225         return class_register(&regulator_class);
2226 }
2227
2228 /* init early to allow our consumers to complete system booting */
2229 core_initcall(regulator_init);