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