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