regulator: sysfs attribute reduction (v2)
[safe/jmp/linux-2.6] / drivers / regulator / core.c
1 /*
2  * core.c  --  Voltage/Current Regulator framework.
3  *
4  * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5  * Copyright 2008 SlimLogic Ltd.
6  *
7  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/mutex.h>
21 #include <linux/suspend.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
25
26 #define REGULATOR_VERSION "0.5"
27
28 static DEFINE_MUTEX(regulator_list_mutex);
29 static LIST_HEAD(regulator_list);
30 static LIST_HEAD(regulator_map_list);
31
32 /**
33  * struct regulator_dev
34  *
35  * Voltage / Current regulator class device. One for each regulator.
36  */
37 struct regulator_dev {
38         struct regulator_desc *desc;
39         int use_count;
40
41         /* lists we belong to */
42         struct list_head list; /* list of all regulators */
43         struct list_head slist; /* list of supplied regulators */
44
45         /* lists we own */
46         struct list_head consumer_list; /* consumers we supply */
47         struct list_head supply_list; /* regulators we supply */
48
49         struct blocking_notifier_head notifier;
50         struct mutex mutex; /* consumer lock */
51         struct module *owner;
52         struct device dev;
53         struct regulation_constraints *constraints;
54         struct regulator_dev *supply;   /* for tree */
55
56         void *reg_data;         /* regulator_dev data */
57 };
58
59 /**
60  * struct regulator_map
61  *
62  * Used to provide symbolic supply names to devices.
63  */
64 struct regulator_map {
65         struct list_head list;
66         struct device *dev;
67         const char *supply;
68         struct regulator_dev *regulator;
69 };
70
71 /*
72  * struct regulator
73  *
74  * One for each consumer device.
75  */
76 struct regulator {
77         struct device *dev;
78         struct list_head list;
79         int uA_load;
80         int min_uV;
81         int max_uV;
82         int enabled; /* count of client enables */
83         char *supply_name;
84         struct device_attribute dev_attr;
85         struct regulator_dev *rdev;
86 };
87
88 static int _regulator_is_enabled(struct regulator_dev *rdev);
89 static int _regulator_disable(struct regulator_dev *rdev);
90 static int _regulator_get_voltage(struct regulator_dev *rdev);
91 static int _regulator_get_current_limit(struct regulator_dev *rdev);
92 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
93 static void _notifier_call_chain(struct regulator_dev *rdev,
94                                   unsigned long event, void *data);
95
96 /* gets the regulator for a given consumer device */
97 static struct regulator *get_device_regulator(struct device *dev)
98 {
99         struct regulator *regulator = NULL;
100         struct regulator_dev *rdev;
101
102         mutex_lock(&regulator_list_mutex);
103         list_for_each_entry(rdev, &regulator_list, list) {
104                 mutex_lock(&rdev->mutex);
105                 list_for_each_entry(regulator, &rdev->consumer_list, list) {
106                         if (regulator->dev == dev) {
107                                 mutex_unlock(&rdev->mutex);
108                                 mutex_unlock(&regulator_list_mutex);
109                                 return regulator;
110                         }
111                 }
112                 mutex_unlock(&rdev->mutex);
113         }
114         mutex_unlock(&regulator_list_mutex);
115         return NULL;
116 }
117
118 /* Platform voltage constraint check */
119 static int regulator_check_voltage(struct regulator_dev *rdev,
120                                    int *min_uV, int *max_uV)
121 {
122         BUG_ON(*min_uV > *max_uV);
123
124         if (!rdev->constraints) {
125                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
126                        rdev->desc->name);
127                 return -ENODEV;
128         }
129         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
130                 printk(KERN_ERR "%s: operation not allowed for %s\n",
131                        __func__, rdev->desc->name);
132                 return -EPERM;
133         }
134
135         if (*max_uV > rdev->constraints->max_uV)
136                 *max_uV = rdev->constraints->max_uV;
137         if (*min_uV < rdev->constraints->min_uV)
138                 *min_uV = rdev->constraints->min_uV;
139
140         if (*min_uV > *max_uV)
141                 return -EINVAL;
142
143         return 0;
144 }
145
146 /* current constraint check */
147 static int regulator_check_current_limit(struct regulator_dev *rdev,
148                                         int *min_uA, int *max_uA)
149 {
150         BUG_ON(*min_uA > *max_uA);
151
152         if (!rdev->constraints) {
153                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
154                        rdev->desc->name);
155                 return -ENODEV;
156         }
157         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
158                 printk(KERN_ERR "%s: operation not allowed for %s\n",
159                        __func__, rdev->desc->name);
160                 return -EPERM;
161         }
162
163         if (*max_uA > rdev->constraints->max_uA)
164                 *max_uA = rdev->constraints->max_uA;
165         if (*min_uA < rdev->constraints->min_uA)
166                 *min_uA = rdev->constraints->min_uA;
167
168         if (*min_uA > *max_uA)
169                 return -EINVAL;
170
171         return 0;
172 }
173
174 /* operating mode constraint check */
175 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
176 {
177         switch (mode) {
178         case REGULATOR_MODE_FAST:
179         case REGULATOR_MODE_NORMAL:
180         case REGULATOR_MODE_IDLE:
181         case REGULATOR_MODE_STANDBY:
182                 break;
183         default:
184                 return -EINVAL;
185         }
186
187         if (!rdev->constraints) {
188                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
189                        rdev->desc->name);
190                 return -ENODEV;
191         }
192         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
193                 printk(KERN_ERR "%s: operation not allowed for %s\n",
194                        __func__, rdev->desc->name);
195                 return -EPERM;
196         }
197         if (!(rdev->constraints->valid_modes_mask & mode)) {
198                 printk(KERN_ERR "%s: invalid mode %x for %s\n",
199                        __func__, mode, rdev->desc->name);
200                 return -EINVAL;
201         }
202         return 0;
203 }
204
205 /* dynamic regulator mode switching constraint check */
206 static int regulator_check_drms(struct regulator_dev *rdev)
207 {
208         if (!rdev->constraints) {
209                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
210                        rdev->desc->name);
211                 return -ENODEV;
212         }
213         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
214                 printk(KERN_ERR "%s: operation not allowed for %s\n",
215                        __func__, rdev->desc->name);
216                 return -EPERM;
217         }
218         return 0;
219 }
220
221 static ssize_t device_requested_uA_show(struct device *dev,
222                              struct device_attribute *attr, char *buf)
223 {
224         struct regulator *regulator;
225
226         regulator = get_device_regulator(dev);
227         if (regulator == NULL)
228                 return 0;
229
230         return sprintf(buf, "%d\n", regulator->uA_load);
231 }
232
233 static ssize_t regulator_uV_show(struct device *dev,
234                                 struct device_attribute *attr, char *buf)
235 {
236         struct regulator_dev *rdev = dev_get_drvdata(dev);
237         ssize_t ret;
238
239         mutex_lock(&rdev->mutex);
240         ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
241         mutex_unlock(&rdev->mutex);
242
243         return ret;
244 }
245 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
246
247 static ssize_t regulator_uA_show(struct device *dev,
248                                 struct device_attribute *attr, char *buf)
249 {
250         struct regulator_dev *rdev = dev_get_drvdata(dev);
251
252         return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
253 }
254 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
255
256 static ssize_t regulator_name_show(struct device *dev,
257                              struct device_attribute *attr, char *buf)
258 {
259         struct regulator_dev *rdev = dev_get_drvdata(dev);
260         const char *name;
261
262         if (rdev->constraints->name)
263                 name = rdev->constraints->name;
264         else if (rdev->desc->name)
265                 name = rdev->desc->name;
266         else
267                 name = "";
268
269         return sprintf(buf, "%s\n", name);
270 }
271
272 static ssize_t regulator_print_opmode(char *buf, int mode)
273 {
274         switch (mode) {
275         case REGULATOR_MODE_FAST:
276                 return sprintf(buf, "fast\n");
277         case REGULATOR_MODE_NORMAL:
278                 return sprintf(buf, "normal\n");
279         case REGULATOR_MODE_IDLE:
280                 return sprintf(buf, "idle\n");
281         case REGULATOR_MODE_STANDBY:
282                 return sprintf(buf, "standby\n");
283         }
284         return sprintf(buf, "unknown\n");
285 }
286
287 static ssize_t regulator_opmode_show(struct device *dev,
288                                     struct device_attribute *attr, char *buf)
289 {
290         struct regulator_dev *rdev = dev_get_drvdata(dev);
291
292         return regulator_print_opmode(buf, _regulator_get_mode(rdev));
293 }
294 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
295
296 static ssize_t regulator_print_state(char *buf, int state)
297 {
298         if (state > 0)
299                 return sprintf(buf, "enabled\n");
300         else if (state == 0)
301                 return sprintf(buf, "disabled\n");
302         else
303                 return sprintf(buf, "unknown\n");
304 }
305
306 static ssize_t regulator_state_show(struct device *dev,
307                                    struct device_attribute *attr, char *buf)
308 {
309         struct regulator_dev *rdev = dev_get_drvdata(dev);
310
311         return regulator_print_state(buf, _regulator_is_enabled(rdev));
312 }
313 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
314
315 static ssize_t regulator_min_uA_show(struct device *dev,
316                                     struct device_attribute *attr, char *buf)
317 {
318         struct regulator_dev *rdev = dev_get_drvdata(dev);
319
320         if (!rdev->constraints)
321                 return sprintf(buf, "constraint not defined\n");
322
323         return sprintf(buf, "%d\n", rdev->constraints->min_uA);
324 }
325 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
326
327 static ssize_t regulator_max_uA_show(struct device *dev,
328                                     struct device_attribute *attr, char *buf)
329 {
330         struct regulator_dev *rdev = dev_get_drvdata(dev);
331
332         if (!rdev->constraints)
333                 return sprintf(buf, "constraint not defined\n");
334
335         return sprintf(buf, "%d\n", rdev->constraints->max_uA);
336 }
337 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
338
339 static ssize_t regulator_min_uV_show(struct device *dev,
340                                     struct device_attribute *attr, char *buf)
341 {
342         struct regulator_dev *rdev = dev_get_drvdata(dev);
343
344         if (!rdev->constraints)
345                 return sprintf(buf, "constraint not defined\n");
346
347         return sprintf(buf, "%d\n", rdev->constraints->min_uV);
348 }
349 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
350
351 static ssize_t regulator_max_uV_show(struct device *dev,
352                                     struct device_attribute *attr, char *buf)
353 {
354         struct regulator_dev *rdev = dev_get_drvdata(dev);
355
356         if (!rdev->constraints)
357                 return sprintf(buf, "constraint not defined\n");
358
359         return sprintf(buf, "%d\n", rdev->constraints->max_uV);
360 }
361 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
362
363 static ssize_t regulator_total_uA_show(struct device *dev,
364                                       struct device_attribute *attr, char *buf)
365 {
366         struct regulator_dev *rdev = dev_get_drvdata(dev);
367         struct regulator *regulator;
368         int uA = 0;
369
370         mutex_lock(&rdev->mutex);
371         list_for_each_entry(regulator, &rdev->consumer_list, list)
372             uA += regulator->uA_load;
373         mutex_unlock(&rdev->mutex);
374         return sprintf(buf, "%d\n", uA);
375 }
376 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
377
378 static ssize_t regulator_num_users_show(struct device *dev,
379                                       struct device_attribute *attr, char *buf)
380 {
381         struct regulator_dev *rdev = dev_get_drvdata(dev);
382         return sprintf(buf, "%d\n", rdev->use_count);
383 }
384
385 static ssize_t regulator_type_show(struct device *dev,
386                                   struct device_attribute *attr, char *buf)
387 {
388         struct regulator_dev *rdev = dev_get_drvdata(dev);
389
390         switch (rdev->desc->type) {
391         case REGULATOR_VOLTAGE:
392                 return sprintf(buf, "voltage\n");
393         case REGULATOR_CURRENT:
394                 return sprintf(buf, "current\n");
395         }
396         return sprintf(buf, "unknown\n");
397 }
398
399 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
400                                 struct device_attribute *attr, char *buf)
401 {
402         struct regulator_dev *rdev = dev_get_drvdata(dev);
403
404         return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
405 }
406 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
407                 regulator_suspend_mem_uV_show, NULL);
408
409 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
410                                 struct device_attribute *attr, char *buf)
411 {
412         struct regulator_dev *rdev = dev_get_drvdata(dev);
413
414         return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
415 }
416 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
417                 regulator_suspend_disk_uV_show, NULL);
418
419 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
420                                 struct device_attribute *attr, char *buf)
421 {
422         struct regulator_dev *rdev = dev_get_drvdata(dev);
423
424         return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
425 }
426 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
427                 regulator_suspend_standby_uV_show, NULL);
428
429 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
430                                 struct device_attribute *attr, char *buf)
431 {
432         struct regulator_dev *rdev = dev_get_drvdata(dev);
433
434         return regulator_print_opmode(buf,
435                 rdev->constraints->state_mem.mode);
436 }
437 static DEVICE_ATTR(suspend_mem_mode, 0444,
438                 regulator_suspend_mem_mode_show, NULL);
439
440 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
441                                 struct device_attribute *attr, char *buf)
442 {
443         struct regulator_dev *rdev = dev_get_drvdata(dev);
444
445         return regulator_print_opmode(buf,
446                 rdev->constraints->state_disk.mode);
447 }
448 static DEVICE_ATTR(suspend_disk_mode, 0444,
449                 regulator_suspend_disk_mode_show, NULL);
450
451 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
452                                 struct device_attribute *attr, char *buf)
453 {
454         struct regulator_dev *rdev = dev_get_drvdata(dev);
455
456         return regulator_print_opmode(buf,
457                 rdev->constraints->state_standby.mode);
458 }
459 static DEVICE_ATTR(suspend_standby_mode, 0444,
460                 regulator_suspend_standby_mode_show, NULL);
461
462 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
463                                    struct device_attribute *attr, char *buf)
464 {
465         struct regulator_dev *rdev = dev_get_drvdata(dev);
466
467         return regulator_print_state(buf,
468                         rdev->constraints->state_mem.enabled);
469 }
470 static DEVICE_ATTR(suspend_mem_state, 0444,
471                 regulator_suspend_mem_state_show, NULL);
472
473 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
474                                    struct device_attribute *attr, char *buf)
475 {
476         struct regulator_dev *rdev = dev_get_drvdata(dev);
477
478         return regulator_print_state(buf,
479                         rdev->constraints->state_disk.enabled);
480 }
481 static DEVICE_ATTR(suspend_disk_state, 0444,
482                 regulator_suspend_disk_state_show, NULL);
483
484 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
485                                    struct device_attribute *attr, char *buf)
486 {
487         struct regulator_dev *rdev = dev_get_drvdata(dev);
488
489         return regulator_print_state(buf,
490                         rdev->constraints->state_standby.enabled);
491 }
492 static DEVICE_ATTR(suspend_standby_state, 0444,
493                 regulator_suspend_standby_state_show, NULL);
494
495
496 /*
497  * These are the only attributes are present for all regulators.
498  * Other attributes are a function of regulator functionality.
499  */
500 static struct device_attribute regulator_dev_attrs[] = {
501         __ATTR(name, 0444, regulator_name_show, NULL),
502         __ATTR(num_users, 0444, regulator_num_users_show, NULL),
503         __ATTR(type, 0444, regulator_type_show, NULL),
504         __ATTR_NULL,
505 };
506
507 static void regulator_dev_release(struct device *dev)
508 {
509         struct regulator_dev *rdev = dev_get_drvdata(dev);
510         kfree(rdev);
511 }
512
513 static struct class regulator_class = {
514         .name = "regulator",
515         .dev_release = regulator_dev_release,
516         .dev_attrs = regulator_dev_attrs,
517 };
518
519 /* Calculate the new optimum regulator operating mode based on the new total
520  * consumer load. All locks held by caller */
521 static void drms_uA_update(struct regulator_dev *rdev)
522 {
523         struct regulator *sibling;
524         int current_uA = 0, output_uV, input_uV, err;
525         unsigned int mode;
526
527         err = regulator_check_drms(rdev);
528         if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
529             !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
530         return;
531
532         /* get output voltage */
533         output_uV = rdev->desc->ops->get_voltage(rdev);
534         if (output_uV <= 0)
535                 return;
536
537         /* get input voltage */
538         if (rdev->supply && rdev->supply->desc->ops->get_voltage)
539                 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
540         else
541                 input_uV = rdev->constraints->input_uV;
542         if (input_uV <= 0)
543                 return;
544
545         /* calc total requested load */
546         list_for_each_entry(sibling, &rdev->consumer_list, list)
547             current_uA += sibling->uA_load;
548
549         /* now get the optimum mode for our new total regulator load */
550         mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
551                                                   output_uV, current_uA);
552
553         /* check the new mode is allowed */
554         err = regulator_check_mode(rdev, mode);
555         if (err == 0)
556                 rdev->desc->ops->set_mode(rdev, mode);
557 }
558
559 static int suspend_set_state(struct regulator_dev *rdev,
560         struct regulator_state *rstate)
561 {
562         int ret = 0;
563
564         /* enable & disable are mandatory for suspend control */
565         if (!rdev->desc->ops->set_suspend_enable ||
566                 !rdev->desc->ops->set_suspend_disable) {
567                 printk(KERN_ERR "%s: no way to set suspend state\n",
568                         __func__);
569                 return -EINVAL;
570         }
571
572         if (rstate->enabled)
573                 ret = rdev->desc->ops->set_suspend_enable(rdev);
574         else
575                 ret = rdev->desc->ops->set_suspend_disable(rdev);
576         if (ret < 0) {
577                 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
578                 return ret;
579         }
580
581         if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
582                 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
583                 if (ret < 0) {
584                         printk(KERN_ERR "%s: failed to set voltage\n",
585                                 __func__);
586                         return ret;
587                 }
588         }
589
590         if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
591                 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
592                 if (ret < 0) {
593                         printk(KERN_ERR "%s: failed to set mode\n", __func__);
594                         return ret;
595                 }
596         }
597         return ret;
598 }
599
600 /* locks held by caller */
601 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
602 {
603         if (!rdev->constraints)
604                 return -EINVAL;
605
606         switch (state) {
607         case PM_SUSPEND_STANDBY:
608                 return suspend_set_state(rdev,
609                         &rdev->constraints->state_standby);
610         case PM_SUSPEND_MEM:
611                 return suspend_set_state(rdev,
612                         &rdev->constraints->state_mem);
613         case PM_SUSPEND_MAX:
614                 return suspend_set_state(rdev,
615                         &rdev->constraints->state_disk);
616         default:
617                 return -EINVAL;
618         }
619 }
620
621 static void print_constraints(struct regulator_dev *rdev)
622 {
623         struct regulation_constraints *constraints = rdev->constraints;
624         char buf[80];
625         int count;
626
627         if (rdev->desc->type == REGULATOR_VOLTAGE) {
628                 if (constraints->min_uV == constraints->max_uV)
629                         count = sprintf(buf, "%d mV ",
630                                         constraints->min_uV / 1000);
631                 else
632                         count = sprintf(buf, "%d <--> %d mV ",
633                                         constraints->min_uV / 1000,
634                                         constraints->max_uV / 1000);
635         } else {
636                 if (constraints->min_uA == constraints->max_uA)
637                         count = sprintf(buf, "%d mA ",
638                                         constraints->min_uA / 1000);
639                 else
640                         count = sprintf(buf, "%d <--> %d mA ",
641                                         constraints->min_uA / 1000,
642                                         constraints->max_uA / 1000);
643         }
644         if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
645                 count += sprintf(buf + count, "fast ");
646         if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
647                 count += sprintf(buf + count, "normal ");
648         if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
649                 count += sprintf(buf + count, "idle ");
650         if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
651                 count += sprintf(buf + count, "standby");
652
653         printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
654 }
655
656 /**
657  * set_machine_constraints - sets regulator constraints
658  * @regulator: regulator source
659  *
660  * Allows platform initialisation code to define and constrain
661  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
662  * Constraints *must* be set by platform code in order for some
663  * regulator operations to proceed i.e. set_voltage, set_current_limit,
664  * set_mode.
665  */
666 static int set_machine_constraints(struct regulator_dev *rdev,
667         struct regulation_constraints *constraints)
668 {
669         int ret = 0;
670         const char *name;
671         struct regulator_ops *ops = rdev->desc->ops;
672
673         if (constraints->name)
674                 name = constraints->name;
675         else if (rdev->desc->name)
676                 name = rdev->desc->name;
677         else
678                 name = "regulator";
679
680         rdev->constraints = constraints;
681
682         /* do we need to apply the constraint voltage */
683         if (rdev->constraints->apply_uV &&
684                 rdev->constraints->min_uV == rdev->constraints->max_uV &&
685                 ops->set_voltage) {
686                 ret = ops->set_voltage(rdev,
687                         rdev->constraints->min_uV, rdev->constraints->max_uV);
688                         if (ret < 0) {
689                                 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
690                                        __func__,
691                                        rdev->constraints->min_uV, name);
692                                 rdev->constraints = NULL;
693                                 goto out;
694                         }
695         }
696
697         /* are we enabled at boot time by firmware / bootloader */
698         if (rdev->constraints->boot_on)
699                 rdev->use_count = 1;
700
701         /* do we need to setup our suspend state */
702         if (constraints->initial_state) {
703                 ret = suspend_prepare(rdev, constraints->initial_state);
704                 if (ret < 0) {
705                         printk(KERN_ERR "%s: failed to set suspend state for %s\n",
706                                __func__, name);
707                         rdev->constraints = NULL;
708                         goto out;
709                 }
710         }
711
712         /* if always_on is set then turn the regulator on if it's not
713          * already on. */
714         if (constraints->always_on && ops->enable &&
715             ((ops->is_enabled && !ops->is_enabled(rdev)) ||
716              (!ops->is_enabled && !constraints->boot_on))) {
717                 ret = ops->enable(rdev);
718                 if (ret < 0) {
719                         printk(KERN_ERR "%s: failed to enable %s\n",
720                                __func__, name);
721                         rdev->constraints = NULL;
722                         goto out;
723                 }
724         }
725
726         print_constraints(rdev);
727 out:
728         return ret;
729 }
730
731 /**
732  * set_supply - set regulator supply regulator
733  * @regulator: regulator name
734  * @supply: supply regulator name
735  *
736  * Called by platform initialisation code to set the supply regulator for this
737  * regulator. This ensures that a regulators supply will also be enabled by the
738  * core if it's child is enabled.
739  */
740 static int set_supply(struct regulator_dev *rdev,
741         struct regulator_dev *supply_rdev)
742 {
743         int err;
744
745         err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
746                                 "supply");
747         if (err) {
748                 printk(KERN_ERR
749                        "%s: could not add device link %s err %d\n",
750                        __func__, supply_rdev->dev.kobj.name, err);
751                        goto out;
752         }
753         rdev->supply = supply_rdev;
754         list_add(&rdev->slist, &supply_rdev->supply_list);
755 out:
756         return err;
757 }
758
759 /**
760  * set_consumer_device_supply: Bind a regulator to a symbolic supply
761  * @regulator: regulator source
762  * @dev:       device the supply applies to
763  * @supply:    symbolic name for supply
764  *
765  * Allows platform initialisation code to map physical regulator
766  * sources to symbolic names for supplies for use by devices.  Devices
767  * should use these symbolic names to request regulators, avoiding the
768  * need to provide board-specific regulator names as platform data.
769  */
770 static int set_consumer_device_supply(struct regulator_dev *rdev,
771         struct device *consumer_dev, const char *supply)
772 {
773         struct regulator_map *node;
774
775         if (supply == NULL)
776                 return -EINVAL;
777
778         node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
779         if (node == NULL)
780                 return -ENOMEM;
781
782         node->regulator = rdev;
783         node->dev = consumer_dev;
784         node->supply = supply;
785
786         list_add(&node->list, &regulator_map_list);
787         return 0;
788 }
789
790 static void unset_consumer_device_supply(struct regulator_dev *rdev,
791         struct device *consumer_dev)
792 {
793         struct regulator_map *node, *n;
794
795         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
796                 if (rdev == node->regulator &&
797                         consumer_dev == node->dev) {
798                         list_del(&node->list);
799                         kfree(node);
800                         return;
801                 }
802         }
803 }
804
805 #define REG_STR_SIZE    32
806
807 static struct regulator *create_regulator(struct regulator_dev *rdev,
808                                           struct device *dev,
809                                           const char *supply_name)
810 {
811         struct regulator *regulator;
812         char buf[REG_STR_SIZE];
813         int err, size;
814
815         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
816         if (regulator == NULL)
817                 return NULL;
818
819         mutex_lock(&rdev->mutex);
820         regulator->rdev = rdev;
821         list_add(&regulator->list, &rdev->consumer_list);
822
823         if (dev) {
824                 /* create a 'requested_microamps_name' sysfs entry */
825                 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
826                         supply_name);
827                 if (size >= REG_STR_SIZE)
828                         goto overflow_err;
829
830                 regulator->dev = dev;
831                 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
832                 if (regulator->dev_attr.attr.name == NULL)
833                         goto attr_name_err;
834
835                 regulator->dev_attr.attr.owner = THIS_MODULE;
836                 regulator->dev_attr.attr.mode = 0444;
837                 regulator->dev_attr.show = device_requested_uA_show;
838                 err = device_create_file(dev, &regulator->dev_attr);
839                 if (err < 0) {
840                         printk(KERN_WARNING "%s: could not add regulator_dev"
841                                 " load sysfs\n", __func__);
842                         goto attr_name_err;
843                 }
844
845                 /* also add a link to the device sysfs entry */
846                 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
847                                  dev->kobj.name, supply_name);
848                 if (size >= REG_STR_SIZE)
849                         goto attr_err;
850
851                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
852                 if (regulator->supply_name == NULL)
853                         goto attr_err;
854
855                 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
856                                         buf);
857                 if (err) {
858                         printk(KERN_WARNING
859                                "%s: could not add device link %s err %d\n",
860                                __func__, dev->kobj.name, err);
861                         device_remove_file(dev, &regulator->dev_attr);
862                         goto link_name_err;
863                 }
864         }
865         mutex_unlock(&rdev->mutex);
866         return regulator;
867 link_name_err:
868         kfree(regulator->supply_name);
869 attr_err:
870         device_remove_file(regulator->dev, &regulator->dev_attr);
871 attr_name_err:
872         kfree(regulator->dev_attr.attr.name);
873 overflow_err:
874         list_del(&regulator->list);
875         kfree(regulator);
876         mutex_unlock(&rdev->mutex);
877         return NULL;
878 }
879
880 /**
881  * regulator_get - lookup and obtain a reference to a regulator.
882  * @dev: device for regulator "consumer"
883  * @id: Supply name or regulator ID.
884  *
885  * Returns a struct regulator corresponding to the regulator producer,
886  * or IS_ERR() condition containing errno.  Use of supply names
887  * configured via regulator_set_device_supply() is strongly
888  * encouraged.
889  */
890 struct regulator *regulator_get(struct device *dev, const char *id)
891 {
892         struct regulator_dev *rdev;
893         struct regulator_map *map;
894         struct regulator *regulator = ERR_PTR(-ENODEV);
895
896         if (id == NULL) {
897                 printk(KERN_ERR "regulator: get() with no identifier\n");
898                 return regulator;
899         }
900
901         mutex_lock(&regulator_list_mutex);
902
903         list_for_each_entry(map, &regulator_map_list, list) {
904                 if (dev == map->dev &&
905                     strcmp(map->supply, id) == 0) {
906                         rdev = map->regulator;
907                         goto found;
908                 }
909         }
910         printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
911                id);
912         mutex_unlock(&regulator_list_mutex);
913         return regulator;
914
915 found:
916         if (!try_module_get(rdev->owner))
917                 goto out;
918
919         regulator = create_regulator(rdev, dev, id);
920         if (regulator == NULL) {
921                 regulator = ERR_PTR(-ENOMEM);
922                 module_put(rdev->owner);
923         }
924
925 out:
926         mutex_unlock(&regulator_list_mutex);
927         return regulator;
928 }
929 EXPORT_SYMBOL_GPL(regulator_get);
930
931 /**
932  * regulator_put - "free" the regulator source
933  * @regulator: regulator source
934  *
935  * Note: drivers must ensure that all regulator_enable calls made on this
936  * regulator source are balanced by regulator_disable calls prior to calling
937  * this function.
938  */
939 void regulator_put(struct regulator *regulator)
940 {
941         struct regulator_dev *rdev;
942
943         if (regulator == NULL || IS_ERR(regulator))
944                 return;
945
946         mutex_lock(&regulator_list_mutex);
947         rdev = regulator->rdev;
948
949         if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
950                                regulator->supply_name))
951                 _regulator_disable(rdev);
952
953         /* remove any sysfs entries */
954         if (regulator->dev) {
955                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
956                 kfree(regulator->supply_name);
957                 device_remove_file(regulator->dev, &regulator->dev_attr);
958                 kfree(regulator->dev_attr.attr.name);
959         }
960         list_del(&regulator->list);
961         kfree(regulator);
962
963         module_put(rdev->owner);
964         mutex_unlock(&regulator_list_mutex);
965 }
966 EXPORT_SYMBOL_GPL(regulator_put);
967
968 /* locks held by regulator_enable() */
969 static int _regulator_enable(struct regulator_dev *rdev)
970 {
971         int ret = -EINVAL;
972
973         if (!rdev->constraints) {
974                 printk(KERN_ERR "%s: %s has no constraints\n",
975                        __func__, rdev->desc->name);
976                 return ret;
977         }
978
979         /* do we need to enable the supply regulator first */
980         if (rdev->supply) {
981                 ret = _regulator_enable(rdev->supply);
982                 if (ret < 0) {
983                         printk(KERN_ERR "%s: failed to enable %s: %d\n",
984                                __func__, rdev->desc->name, ret);
985                         return ret;
986                 }
987         }
988
989         /* check voltage and requested load before enabling */
990         if (rdev->desc->ops->enable) {
991
992                 if (rdev->constraints &&
993                         (rdev->constraints->valid_ops_mask &
994                         REGULATOR_CHANGE_DRMS))
995                         drms_uA_update(rdev);
996
997                 ret = rdev->desc->ops->enable(rdev);
998                 if (ret < 0) {
999                         printk(KERN_ERR "%s: failed to enable %s: %d\n",
1000                                __func__, rdev->desc->name, ret);
1001                         return ret;
1002                 }
1003                 rdev->use_count++;
1004                 return ret;
1005         }
1006
1007         return ret;
1008 }
1009
1010 /**
1011  * regulator_enable - enable regulator output
1012  * @regulator: regulator source
1013  *
1014  * Enable the regulator output at the predefined voltage or current value.
1015  * NOTE: the output value can be set by other drivers, boot loader or may be
1016  * hardwired in the regulator.
1017  * NOTE: calls to regulator_enable() must be balanced with calls to
1018  * 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  * NOTE: this will only disable the regulator output if no other consumer
1078  * devices have it enabled.
1079  * NOTE: calls to regulator_enable() must be balanced with calls to
1080  * 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  * NOTE: 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  * @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  * 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: regulator source
1812  * @reg_data: private regulator data
1813  *
1814  * Called by regulator drivers to register a regulator.
1815  * Returns 0 on success.
1816  */
1817 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1818         struct device *dev, void *driver_data)
1819 {
1820         static atomic_t regulator_no = ATOMIC_INIT(0);
1821         struct regulator_dev *rdev;
1822         struct regulator_init_data *init_data = dev->platform_data;
1823         int ret, i;
1824
1825         if (regulator_desc == NULL)
1826                 return ERR_PTR(-EINVAL);
1827
1828         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1829                 return ERR_PTR(-EINVAL);
1830
1831         if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1832             !regulator_desc->type == REGULATOR_CURRENT)
1833                 return ERR_PTR(-EINVAL);
1834
1835         if (!init_data)
1836                 return ERR_PTR(-EINVAL);
1837
1838         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1839         if (rdev == NULL)
1840                 return ERR_PTR(-ENOMEM);
1841
1842         mutex_lock(&regulator_list_mutex);
1843
1844         mutex_init(&rdev->mutex);
1845         rdev->reg_data = driver_data;
1846         rdev->owner = regulator_desc->owner;
1847         rdev->desc = regulator_desc;
1848         INIT_LIST_HEAD(&rdev->consumer_list);
1849         INIT_LIST_HEAD(&rdev->supply_list);
1850         INIT_LIST_HEAD(&rdev->list);
1851         INIT_LIST_HEAD(&rdev->slist);
1852         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1853
1854         /* preform any regulator specific init */
1855         if (init_data->regulator_init) {
1856                 ret = init_data->regulator_init(rdev->reg_data);
1857                 if (ret < 0)
1858                         goto clean;
1859         }
1860
1861         /* set regulator constraints */
1862         ret = set_machine_constraints(rdev, &init_data->constraints);
1863         if (ret < 0)
1864                 goto clean;
1865
1866         /* register with sysfs */
1867         rdev->dev.class = &regulator_class;
1868         rdev->dev.parent = dev;
1869         dev_set_name(&rdev->dev, "regulator.%d",
1870                      atomic_inc_return(&regulator_no) - 1);
1871         ret = device_register(&rdev->dev);
1872         if (ret != 0)
1873                 goto clean;
1874
1875         dev_set_drvdata(&rdev->dev, rdev);
1876
1877         /* add attributes supported by this regulator */
1878         ret = add_regulator_attributes(rdev);
1879         if (ret < 0)
1880                 goto scrub;
1881
1882         /* set supply regulator if it exists */
1883         if (init_data->supply_regulator_dev) {
1884                 ret = set_supply(rdev,
1885                         dev_get_drvdata(init_data->supply_regulator_dev));
1886                 if (ret < 0)
1887                         goto scrub;
1888         }
1889
1890         /* add consumers devices */
1891         for (i = 0; i < init_data->num_consumer_supplies; i++) {
1892                 ret = set_consumer_device_supply(rdev,
1893                         init_data->consumer_supplies[i].dev,
1894                         init_data->consumer_supplies[i].supply);
1895                 if (ret < 0) {
1896                         for (--i; i >= 0; i--)
1897                                 unset_consumer_device_supply(rdev,
1898                                         init_data->consumer_supplies[i].dev);
1899                         goto scrub;
1900                 }
1901         }
1902
1903         list_add(&rdev->list, &regulator_list);
1904 out:
1905         mutex_unlock(&regulator_list_mutex);
1906         return rdev;
1907
1908 scrub:
1909         device_unregister(&rdev->dev);
1910 clean:
1911         kfree(rdev);
1912         rdev = ERR_PTR(ret);
1913         goto out;
1914 }
1915 EXPORT_SYMBOL_GPL(regulator_register);
1916
1917 /**
1918  * regulator_unregister - unregister regulator
1919  * @regulator: regulator source
1920  *
1921  * Called by regulator drivers to unregister a regulator.
1922  */
1923 void regulator_unregister(struct regulator_dev *rdev)
1924 {
1925         if (rdev == NULL)
1926                 return;
1927
1928         mutex_lock(&regulator_list_mutex);
1929         list_del(&rdev->list);
1930         if (rdev->supply)
1931                 sysfs_remove_link(&rdev->dev.kobj, "supply");
1932         device_unregister(&rdev->dev);
1933         mutex_unlock(&regulator_list_mutex);
1934 }
1935 EXPORT_SYMBOL_GPL(regulator_unregister);
1936
1937 /**
1938  * regulator_suspend_prepare: prepare regulators for system wide suspend
1939  * @state: system suspend state
1940  *
1941  * Configure each regulator with it's suspend operating parameters for state.
1942  * This will usually be called by machine suspend code prior to supending.
1943  */
1944 int regulator_suspend_prepare(suspend_state_t state)
1945 {
1946         struct regulator_dev *rdev;
1947         int ret = 0;
1948
1949         /* ON is handled by regulator active state */
1950         if (state == PM_SUSPEND_ON)
1951                 return -EINVAL;
1952
1953         mutex_lock(&regulator_list_mutex);
1954         list_for_each_entry(rdev, &regulator_list, list) {
1955
1956                 mutex_lock(&rdev->mutex);
1957                 ret = suspend_prepare(rdev, state);
1958                 mutex_unlock(&rdev->mutex);
1959
1960                 if (ret < 0) {
1961                         printk(KERN_ERR "%s: failed to prepare %s\n",
1962                                 __func__, rdev->desc->name);
1963                         goto out;
1964                 }
1965         }
1966 out:
1967         mutex_unlock(&regulator_list_mutex);
1968         return ret;
1969 }
1970 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1971
1972 /**
1973  * rdev_get_drvdata - get rdev regulator driver data
1974  * @regulator: regulator
1975  *
1976  * Get rdev regulator driver private data. This call can be used in the
1977  * regulator driver context.
1978  */
1979 void *rdev_get_drvdata(struct regulator_dev *rdev)
1980 {
1981         return rdev->reg_data;
1982 }
1983 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1984
1985 /**
1986  * regulator_get_drvdata - get regulator driver data
1987  * @regulator: regulator
1988  *
1989  * Get regulator driver private data. This call can be used in the consumer
1990  * driver context when non API regulator specific functions need to be called.
1991  */
1992 void *regulator_get_drvdata(struct regulator *regulator)
1993 {
1994         return regulator->rdev->reg_data;
1995 }
1996 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1997
1998 /**
1999  * regulator_set_drvdata - set regulator driver data
2000  * @regulator: regulator
2001  * @data: data
2002  */
2003 void regulator_set_drvdata(struct regulator *regulator, void *data)
2004 {
2005         regulator->rdev->reg_data = data;
2006 }
2007 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2008
2009 /**
2010  * regulator_get_id - get regulator ID
2011  * @regulator: regulator
2012  */
2013 int rdev_get_id(struct regulator_dev *rdev)
2014 {
2015         return rdev->desc->id;
2016 }
2017 EXPORT_SYMBOL_GPL(rdev_get_id);
2018
2019 struct device *rdev_get_dev(struct regulator_dev *rdev)
2020 {
2021         return &rdev->dev;
2022 }
2023 EXPORT_SYMBOL_GPL(rdev_get_dev);
2024
2025 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2026 {
2027         return reg_init_data->driver_data;
2028 }
2029 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2030
2031 static int __init regulator_init(void)
2032 {
2033         printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2034         return class_register(&regulator_class);
2035 }
2036
2037 /* init early to allow our consumers to complete system booting */
2038 core_initcall(regulator_init);