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