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