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