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