regulator: Add missing kerneldoc
[pandora-kernel.git] / drivers / regulator / core.c
1 /*
2  * core.c  --  Voltage/Current Regulator framework.
3  *
4  * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5  * Copyright 2008 SlimLogic Ltd.
6  *
7  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/mutex.h>
21 #include <linux/suspend.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
25
26 #define REGULATOR_VERSION "0.5"
27
28 static DEFINE_MUTEX(regulator_list_mutex);
29 static LIST_HEAD(regulator_list);
30 static LIST_HEAD(regulator_map_list);
31
32 /*
33  * struct regulator_dev
34  *
35  * Voltage / Current regulator class device. One for each regulator.
36  */
37 struct regulator_dev {
38         struct regulator_desc *desc;
39         int use_count;
40
41         /* lists we belong to */
42         struct list_head list; /* list of all regulators */
43         struct list_head slist; /* list of supplied regulators */
44
45         /* lists we own */
46         struct list_head consumer_list; /* consumers we supply */
47         struct list_head supply_list; /* regulators we supply */
48
49         struct blocking_notifier_head notifier;
50         struct mutex mutex; /* consumer lock */
51         struct module *owner;
52         struct device dev;
53         struct regulation_constraints *constraints;
54         struct regulator_dev *supply;   /* for tree */
55
56         void *reg_data;         /* regulator_dev data */
57 };
58
59 /*
60  * struct regulator_map
61  *
62  * Used to provide symbolic supply names to devices.
63  */
64 struct regulator_map {
65         struct list_head list;
66         struct device *dev;
67         const char *supply;
68         struct regulator_dev *regulator;
69 };
70
71 /*
72  * struct regulator
73  *
74  * One for each consumer device.
75  */
76 struct regulator {
77         struct device *dev;
78         struct list_head list;
79         int uA_load;
80         int min_uV;
81         int max_uV;
82         int enabled; /* count of client enables */
83         char *supply_name;
84         struct device_attribute dev_attr;
85         struct regulator_dev *rdev;
86 };
87
88 static int _regulator_is_enabled(struct regulator_dev *rdev);
89 static int _regulator_disable(struct regulator_dev *rdev);
90 static int _regulator_get_voltage(struct regulator_dev *rdev);
91 static int _regulator_get_current_limit(struct regulator_dev *rdev);
92 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
93 static void _notifier_call_chain(struct regulator_dev *rdev,
94                                   unsigned long event, void *data);
95
96 /* gets the regulator for a given consumer device */
97 static struct regulator *get_device_regulator(struct device *dev)
98 {
99         struct regulator *regulator = NULL;
100         struct regulator_dev *rdev;
101
102         mutex_lock(&regulator_list_mutex);
103         list_for_each_entry(rdev, &regulator_list, list) {
104                 mutex_lock(&rdev->mutex);
105                 list_for_each_entry(regulator, &rdev->consumer_list, list) {
106                         if (regulator->dev == dev) {
107                                 mutex_unlock(&rdev->mutex);
108                                 mutex_unlock(&regulator_list_mutex);
109                                 return regulator;
110                         }
111                 }
112                 mutex_unlock(&rdev->mutex);
113         }
114         mutex_unlock(&regulator_list_mutex);
115         return NULL;
116 }
117
118 /* Platform voltage constraint check */
119 static int regulator_check_voltage(struct regulator_dev *rdev,
120                                    int *min_uV, int *max_uV)
121 {
122         BUG_ON(*min_uV > *max_uV);
123
124         if (!rdev->constraints) {
125                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
126                        rdev->desc->name);
127                 return -ENODEV;
128         }
129         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
130                 printk(KERN_ERR "%s: operation not allowed for %s\n",
131                        __func__, rdev->desc->name);
132                 return -EPERM;
133         }
134
135         if (*max_uV > rdev->constraints->max_uV)
136                 *max_uV = rdev->constraints->max_uV;
137         if (*min_uV < rdev->constraints->min_uV)
138                 *min_uV = rdev->constraints->min_uV;
139
140         if (*min_uV > *max_uV)
141                 return -EINVAL;
142
143         return 0;
144 }
145
146 /* current constraint check */
147 static int regulator_check_current_limit(struct regulator_dev *rdev,
148                                         int *min_uA, int *max_uA)
149 {
150         BUG_ON(*min_uA > *max_uA);
151
152         if (!rdev->constraints) {
153                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
154                        rdev->desc->name);
155                 return -ENODEV;
156         }
157         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
158                 printk(KERN_ERR "%s: operation not allowed for %s\n",
159                        __func__, rdev->desc->name);
160                 return -EPERM;
161         }
162
163         if (*max_uA > rdev->constraints->max_uA)
164                 *max_uA = rdev->constraints->max_uA;
165         if (*min_uA < rdev->constraints->min_uA)
166                 *min_uA = rdev->constraints->min_uA;
167
168         if (*min_uA > *max_uA)
169                 return -EINVAL;
170
171         return 0;
172 }
173
174 /* operating mode constraint check */
175 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
176 {
177         switch (mode) {
178         case REGULATOR_MODE_FAST:
179         case REGULATOR_MODE_NORMAL:
180         case REGULATOR_MODE_IDLE:
181         case REGULATOR_MODE_STANDBY:
182                 break;
183         default:
184                 return -EINVAL;
185         }
186
187         if (!rdev->constraints) {
188                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
189                        rdev->desc->name);
190                 return -ENODEV;
191         }
192         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
193                 printk(KERN_ERR "%s: operation not allowed for %s\n",
194                        __func__, rdev->desc->name);
195                 return -EPERM;
196         }
197         if (!(rdev->constraints->valid_modes_mask & mode)) {
198                 printk(KERN_ERR "%s: invalid mode %x for %s\n",
199                        __func__, mode, rdev->desc->name);
200                 return -EINVAL;
201         }
202         return 0;
203 }
204
205 /* dynamic regulator mode switching constraint check */
206 static int regulator_check_drms(struct regulator_dev *rdev)
207 {
208         if (!rdev->constraints) {
209                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
210                        rdev->desc->name);
211                 return -ENODEV;
212         }
213         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
214                 printk(KERN_ERR "%s: operation not allowed for %s\n",
215                        __func__, rdev->desc->name);
216                 return -EPERM;
217         }
218         return 0;
219 }
220
221 static ssize_t device_requested_uA_show(struct device *dev,
222                              struct device_attribute *attr, char *buf)
223 {
224         struct regulator *regulator;
225
226         regulator = get_device_regulator(dev);
227         if (regulator == NULL)
228                 return 0;
229
230         return sprintf(buf, "%d\n", regulator->uA_load);
231 }
232
233 static ssize_t regulator_uV_show(struct device *dev,
234                                 struct device_attribute *attr, char *buf)
235 {
236         struct regulator_dev *rdev = dev_get_drvdata(dev);
237         ssize_t ret;
238
239         mutex_lock(&rdev->mutex);
240         ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
241         mutex_unlock(&rdev->mutex);
242
243         return ret;
244 }
245 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
246
247 static ssize_t regulator_uA_show(struct device *dev,
248                                 struct device_attribute *attr, char *buf)
249 {
250         struct regulator_dev *rdev = dev_get_drvdata(dev);
251
252         return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
253 }
254 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
255
256 static ssize_t regulator_name_show(struct device *dev,
257                              struct device_attribute *attr, char *buf)
258 {
259         struct regulator_dev *rdev = dev_get_drvdata(dev);
260         const char *name;
261
262         if (rdev->constraints->name)
263                 name = rdev->constraints->name;
264         else if (rdev->desc->name)
265                 name = rdev->desc->name;
266         else
267                 name = "";
268
269         return sprintf(buf, "%s\n", name);
270 }
271
272 static ssize_t regulator_print_opmode(char *buf, int mode)
273 {
274         switch (mode) {
275         case REGULATOR_MODE_FAST:
276                 return sprintf(buf, "fast\n");
277         case REGULATOR_MODE_NORMAL:
278                 return sprintf(buf, "normal\n");
279         case REGULATOR_MODE_IDLE:
280                 return sprintf(buf, "idle\n");
281         case REGULATOR_MODE_STANDBY:
282                 return sprintf(buf, "standby\n");
283         }
284         return sprintf(buf, "unknown\n");
285 }
286
287 static ssize_t regulator_opmode_show(struct device *dev,
288                                     struct device_attribute *attr, char *buf)
289 {
290         struct regulator_dev *rdev = dev_get_drvdata(dev);
291
292         return regulator_print_opmode(buf, _regulator_get_mode(rdev));
293 }
294 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
295
296 static ssize_t regulator_print_state(char *buf, int state)
297 {
298         if (state > 0)
299                 return sprintf(buf, "enabled\n");
300         else if (state == 0)
301                 return sprintf(buf, "disabled\n");
302         else
303                 return sprintf(buf, "unknown\n");
304 }
305
306 static ssize_t regulator_state_show(struct device *dev,
307                                    struct device_attribute *attr, char *buf)
308 {
309         struct regulator_dev *rdev = dev_get_drvdata(dev);
310
311         return regulator_print_state(buf, _regulator_is_enabled(rdev));
312 }
313 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
314
315 static ssize_t regulator_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         node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
780         if (node == NULL)
781                 return -ENOMEM;
782
783         node->regulator = rdev;
784         node->dev = consumer_dev;
785         node->supply = supply;
786
787         list_add(&node->list, &regulator_map_list);
788         return 0;
789 }
790
791 static void unset_consumer_device_supply(struct regulator_dev *rdev,
792         struct device *consumer_dev)
793 {
794         struct regulator_map *node, *n;
795
796         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
797                 if (rdev == node->regulator &&
798                         consumer_dev == node->dev) {
799                         list_del(&node->list);
800                         kfree(node);
801                         return;
802                 }
803         }
804 }
805
806 #define REG_STR_SIZE    32
807
808 static struct regulator *create_regulator(struct regulator_dev *rdev,
809                                           struct device *dev,
810                                           const char *supply_name)
811 {
812         struct regulator *regulator;
813         char buf[REG_STR_SIZE];
814         int err, size;
815
816         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
817         if (regulator == NULL)
818                 return NULL;
819
820         mutex_lock(&rdev->mutex);
821         regulator->rdev = rdev;
822         list_add(&regulator->list, &rdev->consumer_list);
823
824         if (dev) {
825                 /* create a 'requested_microamps_name' sysfs entry */
826                 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
827                         supply_name);
828                 if (size >= REG_STR_SIZE)
829                         goto overflow_err;
830
831                 regulator->dev = dev;
832                 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
833                 if (regulator->dev_attr.attr.name == NULL)
834                         goto attr_name_err;
835
836                 regulator->dev_attr.attr.owner = THIS_MODULE;
837                 regulator->dev_attr.attr.mode = 0444;
838                 regulator->dev_attr.show = device_requested_uA_show;
839                 err = device_create_file(dev, &regulator->dev_attr);
840                 if (err < 0) {
841                         printk(KERN_WARNING "%s: could not add regulator_dev"
842                                 " load sysfs\n", __func__);
843                         goto attr_name_err;
844                 }
845
846                 /* also add a link to the device sysfs entry */
847                 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
848                                  dev->kobj.name, supply_name);
849                 if (size >= REG_STR_SIZE)
850                         goto attr_err;
851
852                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
853                 if (regulator->supply_name == NULL)
854                         goto attr_err;
855
856                 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
857                                         buf);
858                 if (err) {
859                         printk(KERN_WARNING
860                                "%s: could not add device link %s err %d\n",
861                                __func__, dev->kobj.name, err);
862                         device_remove_file(dev, &regulator->dev_attr);
863                         goto link_name_err;
864                 }
865         }
866         mutex_unlock(&rdev->mutex);
867         return regulator;
868 link_name_err:
869         kfree(regulator->supply_name);
870 attr_err:
871         device_remove_file(regulator->dev, &regulator->dev_attr);
872 attr_name_err:
873         kfree(regulator->dev_attr.attr.name);
874 overflow_err:
875         list_del(&regulator->list);
876         kfree(regulator);
877         mutex_unlock(&rdev->mutex);
878         return NULL;
879 }
880
881 /**
882  * regulator_get - lookup and obtain a reference to a regulator.
883  * @dev: device for regulator "consumer"
884  * @id: Supply name or regulator ID.
885  *
886  * Returns a struct regulator corresponding to the regulator producer,
887  * or IS_ERR() condition containing errno.  Use of supply names
888  * configured via regulator_set_device_supply() is strongly
889  * encouraged.
890  */
891 struct regulator *regulator_get(struct device *dev, const char *id)
892 {
893         struct regulator_dev *rdev;
894         struct regulator_map *map;
895         struct regulator *regulator = ERR_PTR(-ENODEV);
896
897         if (id == NULL) {
898                 printk(KERN_ERR "regulator: get() with no identifier\n");
899                 return regulator;
900         }
901
902         mutex_lock(&regulator_list_mutex);
903
904         list_for_each_entry(map, &regulator_map_list, list) {
905                 if (dev == map->dev &&
906                     strcmp(map->supply, id) == 0) {
907                         rdev = map->regulator;
908                         goto found;
909                 }
910         }
911         printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
912                id);
913         mutex_unlock(&regulator_list_mutex);
914         return regulator;
915
916 found:
917         if (!try_module_get(rdev->owner))
918                 goto out;
919
920         regulator = create_regulator(rdev, dev, id);
921         if (regulator == NULL) {
922                 regulator = ERR_PTR(-ENOMEM);
923                 module_put(rdev->owner);
924         }
925
926 out:
927         mutex_unlock(&regulator_list_mutex);
928         return regulator;
929 }
930 EXPORT_SYMBOL_GPL(regulator_get);
931
932 /**
933  * regulator_put - "free" the regulator source
934  * @regulator: regulator source
935  *
936  * Note: drivers must ensure that all regulator_enable calls made on this
937  * regulator source are balanced by regulator_disable calls prior to calling
938  * this function.
939  */
940 void regulator_put(struct regulator *regulator)
941 {
942         struct regulator_dev *rdev;
943
944         if (regulator == NULL || IS_ERR(regulator))
945                 return;
946
947         mutex_lock(&regulator_list_mutex);
948         rdev = regulator->rdev;
949
950         if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
951                                regulator->supply_name))
952                 _regulator_disable(rdev);
953
954         /* remove any sysfs entries */
955         if (regulator->dev) {
956                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
957                 kfree(regulator->supply_name);
958                 device_remove_file(regulator->dev, &regulator->dev_attr);
959                 kfree(regulator->dev_attr.attr.name);
960         }
961         list_del(&regulator->list);
962         kfree(regulator);
963
964         module_put(rdev->owner);
965         mutex_unlock(&regulator_list_mutex);
966 }
967 EXPORT_SYMBOL_GPL(regulator_put);
968
969 /* locks held by regulator_enable() */
970 static int _regulator_enable(struct regulator_dev *rdev)
971 {
972         int ret = -EINVAL;
973
974         if (!rdev->constraints) {
975                 printk(KERN_ERR "%s: %s has no constraints\n",
976                        __func__, rdev->desc->name);
977                 return ret;
978         }
979
980         /* do we need to enable the supply regulator first */
981         if (rdev->supply) {
982                 ret = _regulator_enable(rdev->supply);
983                 if (ret < 0) {
984                         printk(KERN_ERR "%s: failed to enable %s: %d\n",
985                                __func__, rdev->desc->name, ret);
986                         return ret;
987                 }
988         }
989
990         /* check voltage and requested load before enabling */
991         if (rdev->desc->ops->enable) {
992
993                 if (rdev->constraints &&
994                         (rdev->constraints->valid_ops_mask &
995                         REGULATOR_CHANGE_DRMS))
996                         drms_uA_update(rdev);
997
998                 ret = rdev->desc->ops->enable(rdev);
999                 if (ret < 0) {
1000                         printk(KERN_ERR "%s: failed to enable %s: %d\n",
1001                                __func__, rdev->desc->name, ret);
1002                         return ret;
1003                 }
1004                 rdev->use_count++;
1005                 return ret;
1006         }
1007
1008         return ret;
1009 }
1010
1011 /**
1012  * regulator_enable - enable regulator output
1013  * @regulator: regulator source
1014  *
1015  * Enable the regulator output at the predefined voltage or current value.
1016  * NOTE: the output value can be set by other drivers, boot loader or may be
1017  * hardwired in the regulator.  Calls to regulator_enable() must be balanced
1018  * with calls to regulator_disable().
1019  */
1020 int regulator_enable(struct regulator *regulator)
1021 {
1022         struct regulator_dev *rdev = regulator->rdev;
1023         int ret = 0;
1024
1025         mutex_lock(&rdev->mutex);
1026         if (regulator->enabled == 0)
1027                 ret = _regulator_enable(rdev);
1028         else if (regulator->enabled < 0)
1029                 ret = -EIO;
1030         if (ret == 0)
1031                 regulator->enabled++;
1032         mutex_unlock(&rdev->mutex);
1033         return ret;
1034 }
1035 EXPORT_SYMBOL_GPL(regulator_enable);
1036
1037 /* locks held by regulator_disable() */
1038 static int _regulator_disable(struct regulator_dev *rdev)
1039 {
1040         int ret = 0;
1041
1042         /* are we the last user and permitted to disable ? */
1043         if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1044
1045                 /* we are last user */
1046                 if (rdev->desc->ops->disable) {
1047                         ret = rdev->desc->ops->disable(rdev);
1048                         if (ret < 0) {
1049                                 printk(KERN_ERR "%s: failed to disable %s\n",
1050                                        __func__, rdev->desc->name);
1051                                 return ret;
1052                         }
1053                 }
1054
1055                 /* decrease our supplies ref count and disable if required */
1056                 if (rdev->supply)
1057                         _regulator_disable(rdev->supply);
1058
1059                 rdev->use_count = 0;
1060         } else if (rdev->use_count > 1) {
1061
1062                 if (rdev->constraints &&
1063                         (rdev->constraints->valid_ops_mask &
1064                         REGULATOR_CHANGE_DRMS))
1065                         drms_uA_update(rdev);
1066
1067                 rdev->use_count--;
1068         }
1069         return ret;
1070 }
1071
1072 /**
1073  * regulator_disable - disable regulator output
1074  * @regulator: regulator source
1075  *
1076  * Disable the regulator output voltage or current.
1077  *
1078  * NOTE: this will only disable the regulator output if no other consumer
1079  * devices have it enabled.  Calls to regulator_enable() must be balanced with
1080  * calls to regulator_disable().
1081  */
1082 int regulator_disable(struct regulator *regulator)
1083 {
1084         struct regulator_dev *rdev = regulator->rdev;
1085         int ret = 0;
1086
1087         mutex_lock(&rdev->mutex);
1088         if (regulator->enabled == 1) {
1089                 ret = _regulator_disable(rdev);
1090                 if (ret == 0)
1091                         regulator->uA_load = 0;
1092         } else if (WARN(regulator->enabled <= 0,
1093                         "unbalanced disables for supply %s\n",
1094                         regulator->supply_name))
1095                 ret = -EIO;
1096         if (ret == 0)
1097                 regulator->enabled--;
1098         mutex_unlock(&rdev->mutex);
1099         return ret;
1100 }
1101 EXPORT_SYMBOL_GPL(regulator_disable);
1102
1103 /* locks held by regulator_force_disable() */
1104 static int _regulator_force_disable(struct regulator_dev *rdev)
1105 {
1106         int ret = 0;
1107
1108         /* force disable */
1109         if (rdev->desc->ops->disable) {
1110                 /* ah well, who wants to live forever... */
1111                 ret = rdev->desc->ops->disable(rdev);
1112                 if (ret < 0) {
1113                         printk(KERN_ERR "%s: failed to force disable %s\n",
1114                                __func__, rdev->desc->name);
1115                         return ret;
1116                 }
1117                 /* notify other consumers that power has been forced off */
1118                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1119                         NULL);
1120         }
1121
1122         /* decrease our supplies ref count and disable if required */
1123         if (rdev->supply)
1124                 _regulator_disable(rdev->supply);
1125
1126         rdev->use_count = 0;
1127         return ret;
1128 }
1129
1130 /**
1131  * regulator_force_disable - force disable regulator output
1132  * @regulator: regulator source
1133  *
1134  * Forcibly disable the regulator output voltage or current.
1135  * NOTE: this *will* disable the regulator output even if other consumer
1136  * devices have it enabled. This should be used for situations when device
1137  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1138  */
1139 int regulator_force_disable(struct regulator *regulator)
1140 {
1141         int ret;
1142
1143         mutex_lock(&regulator->rdev->mutex);
1144         regulator->enabled = 0;
1145         regulator->uA_load = 0;
1146         ret = _regulator_force_disable(regulator->rdev);
1147         mutex_unlock(&regulator->rdev->mutex);
1148         return ret;
1149 }
1150 EXPORT_SYMBOL_GPL(regulator_force_disable);
1151
1152 static int _regulator_is_enabled(struct regulator_dev *rdev)
1153 {
1154         int ret;
1155
1156         mutex_lock(&rdev->mutex);
1157
1158         /* sanity check */
1159         if (!rdev->desc->ops->is_enabled) {
1160                 ret = -EINVAL;
1161                 goto out;
1162         }
1163
1164         ret = rdev->desc->ops->is_enabled(rdev);
1165 out:
1166         mutex_unlock(&rdev->mutex);
1167         return ret;
1168 }
1169
1170 /**
1171  * regulator_is_enabled - is the regulator output enabled
1172  * @regulator: regulator source
1173  *
1174  * Returns positive if the regulator driver backing the source/client
1175  * has requested that the device be enabled, zero if it hasn't, else a
1176  * negative errno code.
1177  *
1178  * Note that the device backing this regulator handle can have multiple
1179  * users, so it might be enabled even if regulator_enable() was never
1180  * called for this particular source.
1181  */
1182 int regulator_is_enabled(struct regulator *regulator)
1183 {
1184         return _regulator_is_enabled(regulator->rdev);
1185 }
1186 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1187
1188 /**
1189  * regulator_set_voltage - set regulator output voltage
1190  * @regulator: regulator source
1191  * @min_uV: Minimum required voltage in uV
1192  * @max_uV: Maximum acceptable voltage in uV
1193  *
1194  * Sets a voltage regulator to the desired output voltage. This can be set
1195  * during any regulator state. IOW, regulator can be disabled or enabled.
1196  *
1197  * If the regulator is enabled then the voltage will change to the new value
1198  * immediately otherwise if the regulator is disabled the regulator will
1199  * output at the new voltage when enabled.
1200  *
1201  * NOTE: If the regulator is shared between several devices then the lowest
1202  * request voltage that meets the system constraints will be used.
1203  * Regulator system constraints must be set for this regulator before
1204  * calling this function otherwise this call will fail.
1205  */
1206 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1207 {
1208         struct regulator_dev *rdev = regulator->rdev;
1209         int ret;
1210
1211         mutex_lock(&rdev->mutex);
1212
1213         /* sanity check */
1214         if (!rdev->desc->ops->set_voltage) {
1215                 ret = -EINVAL;
1216                 goto out;
1217         }
1218
1219         /* constraints check */
1220         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1221         if (ret < 0)
1222                 goto out;
1223         regulator->min_uV = min_uV;
1224         regulator->max_uV = max_uV;
1225         ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1226
1227 out:
1228         mutex_unlock(&rdev->mutex);
1229         return ret;
1230 }
1231 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1232
1233 static int _regulator_get_voltage(struct regulator_dev *rdev)
1234 {
1235         /* sanity check */
1236         if (rdev->desc->ops->get_voltage)
1237                 return rdev->desc->ops->get_voltage(rdev);
1238         else
1239                 return -EINVAL;
1240 }
1241
1242 /**
1243  * regulator_get_voltage - get regulator output voltage
1244  * @regulator: regulator source
1245  *
1246  * This returns the current regulator voltage in uV.
1247  *
1248  * NOTE: If the regulator is disabled it will return the voltage value. This
1249  * function should not be used to determine regulator state.
1250  */
1251 int regulator_get_voltage(struct regulator *regulator)
1252 {
1253         int ret;
1254
1255         mutex_lock(&regulator->rdev->mutex);
1256
1257         ret = _regulator_get_voltage(regulator->rdev);
1258
1259         mutex_unlock(&regulator->rdev->mutex);
1260
1261         return ret;
1262 }
1263 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1264
1265 /**
1266  * regulator_set_current_limit - set regulator output current limit
1267  * @regulator: regulator source
1268  * @min_uA: Minimuum supported current in uA
1269  * @max_uA: Maximum supported current in uA
1270  *
1271  * Sets current sink to the desired output current. This can be set during
1272  * any regulator state. IOW, regulator can be disabled or enabled.
1273  *
1274  * If the regulator is enabled then the current will change to the new value
1275  * immediately otherwise if the regulator is disabled the regulator will
1276  * output at the new current when enabled.
1277  *
1278  * NOTE: Regulator system constraints must be set for this regulator before
1279  * calling this function otherwise this call will fail.
1280  */
1281 int regulator_set_current_limit(struct regulator *regulator,
1282                                int min_uA, int max_uA)
1283 {
1284         struct regulator_dev *rdev = regulator->rdev;
1285         int ret;
1286
1287         mutex_lock(&rdev->mutex);
1288
1289         /* sanity check */
1290         if (!rdev->desc->ops->set_current_limit) {
1291                 ret = -EINVAL;
1292                 goto out;
1293         }
1294
1295         /* constraints check */
1296         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1297         if (ret < 0)
1298                 goto out;
1299
1300         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1301 out:
1302         mutex_unlock(&rdev->mutex);
1303         return ret;
1304 }
1305 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1306
1307 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1308 {
1309         int ret;
1310
1311         mutex_lock(&rdev->mutex);
1312
1313         /* sanity check */
1314         if (!rdev->desc->ops->get_current_limit) {
1315                 ret = -EINVAL;
1316                 goto out;
1317         }
1318
1319         ret = rdev->desc->ops->get_current_limit(rdev);
1320 out:
1321         mutex_unlock(&rdev->mutex);
1322         return ret;
1323 }
1324
1325 /**
1326  * regulator_get_current_limit - get regulator output current
1327  * @regulator: regulator source
1328  *
1329  * This returns the current supplied by the specified current sink in uA.
1330  *
1331  * NOTE: If the regulator is disabled it will return the current value. This
1332  * function should not be used to determine regulator state.
1333  */
1334 int regulator_get_current_limit(struct regulator *regulator)
1335 {
1336         return _regulator_get_current_limit(regulator->rdev);
1337 }
1338 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1339
1340 /**
1341  * regulator_set_mode - set regulator operating mode
1342  * @regulator: regulator source
1343  * @mode: operating mode - one of the REGULATOR_MODE constants
1344  *
1345  * Set regulator operating mode to increase regulator efficiency or improve
1346  * regulation performance.
1347  *
1348  * NOTE: Regulator system constraints must be set for this regulator before
1349  * calling this function otherwise this call will fail.
1350  */
1351 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1352 {
1353         struct regulator_dev *rdev = regulator->rdev;
1354         int ret;
1355
1356         mutex_lock(&rdev->mutex);
1357
1358         /* sanity check */
1359         if (!rdev->desc->ops->set_mode) {
1360                 ret = -EINVAL;
1361                 goto out;
1362         }
1363
1364         /* constraints check */
1365         ret = regulator_check_mode(rdev, mode);
1366         if (ret < 0)
1367                 goto out;
1368
1369         ret = rdev->desc->ops->set_mode(rdev, mode);
1370 out:
1371         mutex_unlock(&rdev->mutex);
1372         return ret;
1373 }
1374 EXPORT_SYMBOL_GPL(regulator_set_mode);
1375
1376 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1377 {
1378         int ret;
1379
1380         mutex_lock(&rdev->mutex);
1381
1382         /* sanity check */
1383         if (!rdev->desc->ops->get_mode) {
1384                 ret = -EINVAL;
1385                 goto out;
1386         }
1387
1388         ret = rdev->desc->ops->get_mode(rdev);
1389 out:
1390         mutex_unlock(&rdev->mutex);
1391         return ret;
1392 }
1393
1394 /**
1395  * regulator_get_mode - get regulator operating mode
1396  * @regulator: regulator source
1397  *
1398  * Get the current regulator operating mode.
1399  */
1400 unsigned int regulator_get_mode(struct regulator *regulator)
1401 {
1402         return _regulator_get_mode(regulator->rdev);
1403 }
1404 EXPORT_SYMBOL_GPL(regulator_get_mode);
1405
1406 /**
1407  * regulator_set_optimum_mode - set regulator optimum operating mode
1408  * @regulator: regulator source
1409  * @uA_load: load current
1410  *
1411  * Notifies the regulator core of a new device load. This is then used by
1412  * DRMS (if enabled by constraints) to set the most efficient regulator
1413  * operating mode for the new regulator loading.
1414  *
1415  * Consumer devices notify their supply regulator of the maximum power
1416  * they will require (can be taken from device datasheet in the power
1417  * consumption tables) when they change operational status and hence power
1418  * state. Examples of operational state changes that can affect power
1419  * consumption are :-
1420  *
1421  *    o Device is opened / closed.
1422  *    o Device I/O is about to begin or has just finished.
1423  *    o Device is idling in between work.
1424  *
1425  * This information is also exported via sysfs to userspace.
1426  *
1427  * DRMS will sum the total requested load on the regulator and change
1428  * to the most efficient operating mode if platform constraints allow.
1429  *
1430  * Returns the new regulator mode or error.
1431  */
1432 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1433 {
1434         struct regulator_dev *rdev = regulator->rdev;
1435         struct regulator *consumer;
1436         int ret, output_uV, input_uV, total_uA_load = 0;
1437         unsigned int mode;
1438
1439         mutex_lock(&rdev->mutex);
1440
1441         regulator->uA_load = uA_load;
1442         ret = regulator_check_drms(rdev);
1443         if (ret < 0)
1444                 goto out;
1445         ret = -EINVAL;
1446
1447         /* sanity check */
1448         if (!rdev->desc->ops->get_optimum_mode)
1449                 goto out;
1450
1451         /* get output voltage */
1452         output_uV = rdev->desc->ops->get_voltage(rdev);
1453         if (output_uV <= 0) {
1454                 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1455                         __func__, rdev->desc->name);
1456                 goto out;
1457         }
1458
1459         /* get input voltage */
1460         if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1461                 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1462         else
1463                 input_uV = rdev->constraints->input_uV;
1464         if (input_uV <= 0) {
1465                 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1466                         __func__, rdev->desc->name);
1467                 goto out;
1468         }
1469
1470         /* calc total requested load for this regulator */
1471         list_for_each_entry(consumer, &rdev->consumer_list, list)
1472             total_uA_load += consumer->uA_load;
1473
1474         mode = rdev->desc->ops->get_optimum_mode(rdev,
1475                                                  input_uV, output_uV,
1476                                                  total_uA_load);
1477         ret = regulator_check_mode(rdev, mode);
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  * @nb: 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  * @nb: 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  * @rdev: regulator source
1683  * @event: notifier block
1684  * @data: callback-specific 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  * To avoid cluttering sysfs (and memory) with useless state, only
1700  * create attributes that can be meaningfully displayed.
1701  */
1702 static int add_regulator_attributes(struct regulator_dev *rdev)
1703 {
1704         struct device           *dev = &rdev->dev;
1705         struct regulator_ops    *ops = rdev->desc->ops;
1706         int                     status = 0;
1707
1708         /* some attributes need specific methods to be displayed */
1709         if (ops->get_voltage) {
1710                 status = device_create_file(dev, &dev_attr_microvolts);
1711                 if (status < 0)
1712                         return status;
1713         }
1714         if (ops->get_current_limit) {
1715                 status = device_create_file(dev, &dev_attr_microamps);
1716                 if (status < 0)
1717                         return status;
1718         }
1719         if (ops->get_mode) {
1720                 status = device_create_file(dev, &dev_attr_opmode);
1721                 if (status < 0)
1722                         return status;
1723         }
1724         if (ops->is_enabled) {
1725                 status = device_create_file(dev, &dev_attr_state);
1726                 if (status < 0)
1727                         return status;
1728         }
1729
1730         /* some attributes are type-specific */
1731         if (rdev->desc->type == REGULATOR_CURRENT) {
1732                 status = device_create_file(dev, &dev_attr_requested_microamps);
1733                 if (status < 0)
1734                         return status;
1735         }
1736
1737         /* all the other attributes exist to support constraints;
1738          * don't show them if there are no constraints, or if the
1739          * relevant supporting methods are missing.
1740          */
1741         if (!rdev->constraints)
1742                 return status;
1743
1744         /* constraints need specific supporting methods */
1745         if (ops->set_voltage) {
1746                 status = device_create_file(dev, &dev_attr_min_microvolts);
1747                 if (status < 0)
1748                         return status;
1749                 status = device_create_file(dev, &dev_attr_max_microvolts);
1750                 if (status < 0)
1751                         return status;
1752         }
1753         if (ops->set_current_limit) {
1754                 status = device_create_file(dev, &dev_attr_min_microamps);
1755                 if (status < 0)
1756                         return status;
1757                 status = device_create_file(dev, &dev_attr_max_microamps);
1758                 if (status < 0)
1759                         return status;
1760         }
1761
1762         /* suspend mode constraints need multiple supporting methods */
1763         if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1764                 return status;
1765
1766         status = device_create_file(dev, &dev_attr_suspend_standby_state);
1767         if (status < 0)
1768                 return status;
1769         status = device_create_file(dev, &dev_attr_suspend_mem_state);
1770         if (status < 0)
1771                 return status;
1772         status = device_create_file(dev, &dev_attr_suspend_disk_state);
1773         if (status < 0)
1774                 return status;
1775
1776         if (ops->set_suspend_voltage) {
1777                 status = device_create_file(dev,
1778                                 &dev_attr_suspend_standby_microvolts);
1779                 if (status < 0)
1780                         return status;
1781                 status = device_create_file(dev,
1782                                 &dev_attr_suspend_mem_microvolts);
1783                 if (status < 0)
1784                         return status;
1785                 status = device_create_file(dev,
1786                                 &dev_attr_suspend_disk_microvolts);
1787                 if (status < 0)
1788                         return status;
1789         }
1790
1791         if (ops->set_suspend_mode) {
1792                 status = device_create_file(dev,
1793                                 &dev_attr_suspend_standby_mode);
1794                 if (status < 0)
1795                         return status;
1796                 status = device_create_file(dev,
1797                                 &dev_attr_suspend_mem_mode);
1798                 if (status < 0)
1799                         return status;
1800                 status = device_create_file(dev,
1801                                 &dev_attr_suspend_disk_mode);
1802                 if (status < 0)
1803                         return status;
1804         }
1805
1806         return status;
1807 }
1808
1809 /**
1810  * regulator_register - register regulator
1811  * @regulator_desc: regulator to register
1812  * @dev: struct device for the regulator
1813  * @driver_data: private regulator data
1814  *
1815  * Called by regulator drivers to register a regulator.
1816  * Returns 0 on success.
1817  */
1818 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1819         struct device *dev, void *driver_data)
1820 {
1821         static atomic_t regulator_no = ATOMIC_INIT(0);
1822         struct regulator_dev *rdev;
1823         struct regulator_init_data *init_data = dev->platform_data;
1824         int ret, i;
1825
1826         if (regulator_desc == NULL)
1827                 return ERR_PTR(-EINVAL);
1828
1829         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1830                 return ERR_PTR(-EINVAL);
1831
1832         if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1833             !regulator_desc->type == REGULATOR_CURRENT)
1834                 return ERR_PTR(-EINVAL);
1835
1836         if (!init_data)
1837                 return ERR_PTR(-EINVAL);
1838
1839         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1840         if (rdev == NULL)
1841                 return ERR_PTR(-ENOMEM);
1842
1843         mutex_lock(&regulator_list_mutex);
1844
1845         mutex_init(&rdev->mutex);
1846         rdev->reg_data = driver_data;
1847         rdev->owner = regulator_desc->owner;
1848         rdev->desc = regulator_desc;
1849         INIT_LIST_HEAD(&rdev->consumer_list);
1850         INIT_LIST_HEAD(&rdev->supply_list);
1851         INIT_LIST_HEAD(&rdev->list);
1852         INIT_LIST_HEAD(&rdev->slist);
1853         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1854
1855         /* preform any regulator specific init */
1856         if (init_data->regulator_init) {
1857                 ret = init_data->regulator_init(rdev->reg_data);
1858                 if (ret < 0)
1859                         goto clean;
1860         }
1861
1862         /* register with sysfs */
1863         rdev->dev.class = &regulator_class;
1864         rdev->dev.parent = dev;
1865         dev_set_name(&rdev->dev, "regulator.%d",
1866                      atomic_inc_return(&regulator_no) - 1);
1867         ret = device_register(&rdev->dev);
1868         if (ret != 0)
1869                 goto clean;
1870
1871         dev_set_drvdata(&rdev->dev, rdev);
1872
1873         /* set regulator constraints */
1874         ret = set_machine_constraints(rdev, &init_data->constraints);
1875         if (ret < 0)
1876                 goto scrub;
1877
1878         /* add attributes supported by this regulator */
1879         ret = add_regulator_attributes(rdev);
1880         if (ret < 0)
1881                 goto scrub;
1882
1883         /* set supply regulator if it exists */
1884         if (init_data->supply_regulator_dev) {
1885                 ret = set_supply(rdev,
1886                         dev_get_drvdata(init_data->supply_regulator_dev));
1887                 if (ret < 0)
1888                         goto scrub;
1889         }
1890
1891         /* add consumers devices */
1892         for (i = 0; i < init_data->num_consumer_supplies; i++) {
1893                 ret = set_consumer_device_supply(rdev,
1894                         init_data->consumer_supplies[i].dev,
1895                         init_data->consumer_supplies[i].supply);
1896                 if (ret < 0) {
1897                         for (--i; i >= 0; i--)
1898                                 unset_consumer_device_supply(rdev,
1899                                         init_data->consumer_supplies[i].dev);
1900                         goto scrub;
1901                 }
1902         }
1903
1904         list_add(&rdev->list, &regulator_list);
1905 out:
1906         mutex_unlock(&regulator_list_mutex);
1907         return rdev;
1908
1909 scrub:
1910         device_unregister(&rdev->dev);
1911 clean:
1912         kfree(rdev);
1913         rdev = ERR_PTR(ret);
1914         goto out;
1915 }
1916 EXPORT_SYMBOL_GPL(regulator_register);
1917
1918 /**
1919  * regulator_unregister - unregister regulator
1920  * @rdev: regulator to unregister
1921  *
1922  * Called by regulator drivers to unregister a regulator.
1923  */
1924 void regulator_unregister(struct regulator_dev *rdev)
1925 {
1926         if (rdev == NULL)
1927                 return;
1928
1929         mutex_lock(&regulator_list_mutex);
1930         list_del(&rdev->list);
1931         if (rdev->supply)
1932                 sysfs_remove_link(&rdev->dev.kobj, "supply");
1933         device_unregister(&rdev->dev);
1934         mutex_unlock(&regulator_list_mutex);
1935 }
1936 EXPORT_SYMBOL_GPL(regulator_unregister);
1937
1938 /**
1939  * regulator_suspend_prepare: prepare regulators for system wide suspend
1940  * @state: system suspend state
1941  *
1942  * Configure each regulator with it's suspend operating parameters for state.
1943  * This will usually be called by machine suspend code prior to supending.
1944  */
1945 int regulator_suspend_prepare(suspend_state_t state)
1946 {
1947         struct regulator_dev *rdev;
1948         int ret = 0;
1949
1950         /* ON is handled by regulator active state */
1951         if (state == PM_SUSPEND_ON)
1952                 return -EINVAL;
1953
1954         mutex_lock(&regulator_list_mutex);
1955         list_for_each_entry(rdev, &regulator_list, list) {
1956
1957                 mutex_lock(&rdev->mutex);
1958                 ret = suspend_prepare(rdev, state);
1959                 mutex_unlock(&rdev->mutex);
1960
1961                 if (ret < 0) {
1962                         printk(KERN_ERR "%s: failed to prepare %s\n",
1963                                 __func__, rdev->desc->name);
1964                         goto out;
1965                 }
1966         }
1967 out:
1968         mutex_unlock(&regulator_list_mutex);
1969         return ret;
1970 }
1971 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1972
1973 /**
1974  * rdev_get_drvdata - get rdev regulator driver data
1975  * @rdev: regulator
1976  *
1977  * Get rdev regulator driver private data. This call can be used in the
1978  * regulator driver context.
1979  */
1980 void *rdev_get_drvdata(struct regulator_dev *rdev)
1981 {
1982         return rdev->reg_data;
1983 }
1984 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1985
1986 /**
1987  * regulator_get_drvdata - get regulator driver data
1988  * @regulator: regulator
1989  *
1990  * Get regulator driver private data. This call can be used in the consumer
1991  * driver context when non API regulator specific functions need to be called.
1992  */
1993 void *regulator_get_drvdata(struct regulator *regulator)
1994 {
1995         return regulator->rdev->reg_data;
1996 }
1997 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1998
1999 /**
2000  * regulator_set_drvdata - set regulator driver data
2001  * @regulator: regulator
2002  * @data: data
2003  */
2004 void regulator_set_drvdata(struct regulator *regulator, void *data)
2005 {
2006         regulator->rdev->reg_data = data;
2007 }
2008 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2009
2010 /**
2011  * regulator_get_id - get regulator ID
2012  * @rdev: regulator
2013  */
2014 int rdev_get_id(struct regulator_dev *rdev)
2015 {
2016         return rdev->desc->id;
2017 }
2018 EXPORT_SYMBOL_GPL(rdev_get_id);
2019
2020 struct device *rdev_get_dev(struct regulator_dev *rdev)
2021 {
2022         return &rdev->dev;
2023 }
2024 EXPORT_SYMBOL_GPL(rdev_get_dev);
2025
2026 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2027 {
2028         return reg_init_data->driver_data;
2029 }
2030 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2031
2032 static int __init regulator_init(void)
2033 {
2034         printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2035         return class_register(&regulator_class);
2036 }
2037
2038 /* init early to allow our consumers to complete system booting */
2039 core_initcall(regulator_init);