upload tizen1.0 source
[kernel/linux-2.6.36.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/slab.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include <linux/suspend.h>
23 #include <linux/delay.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
27 #ifdef CONFIG_DEBUG_FS
28 #include <linux/debugfs.h>
29 #endif
30
31 #include "dummy.h"
32
33 #define REGULATOR_VERSION "0.5"
34
35 static DEFINE_MUTEX(regulator_list_mutex);
36 static LIST_HEAD(regulator_list);
37 static LIST_HEAD(regulator_map_list);
38 static int has_full_constraints;
39
40 /*
41  * struct regulator_map
42  *
43  * Used to provide symbolic supply names to devices.
44  */
45 struct regulator_map {
46         struct list_head list;
47         const char *dev_name;   /* The dev_name() for the consumer */
48         const char *supply;
49         struct regulator_dev *regulator;
50 };
51
52 /*
53  * struct regulator
54  *
55  * One for each consumer device.
56  */
57 struct regulator {
58         struct device *dev;
59         struct list_head list;
60         int uA_load;
61         int min_uV;
62         int max_uV;
63         char *supply_name;
64         struct device_attribute dev_attr;
65         struct regulator_dev *rdev;
66 };
67
68 static int _regulator_is_enabled(struct regulator_dev *rdev);
69 static int _regulator_disable(struct regulator_dev *rdev);
70 static int _regulator_get_voltage(struct regulator_dev *rdev);
71 static int _regulator_get_current_limit(struct regulator_dev *rdev);
72 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
73 static void _notifier_call_chain(struct regulator_dev *rdev,
74                                   unsigned long event, void *data);
75
76 static const char *rdev_get_name(struct regulator_dev *rdev)
77 {
78         if (rdev->constraints && rdev->constraints->name)
79                 return rdev->constraints->name;
80         else if (rdev->desc->name)
81                 return rdev->desc->name;
82         else
83                 return "";
84 }
85
86 /* gets the regulator for a given consumer device */
87 static struct regulator *get_device_regulator(struct device *dev)
88 {
89         struct regulator *regulator = NULL;
90         struct regulator_dev *rdev;
91
92         mutex_lock(&regulator_list_mutex);
93         list_for_each_entry(rdev, &regulator_list, list) {
94                 mutex_lock(&rdev->mutex);
95                 list_for_each_entry(regulator, &rdev->consumer_list, list) {
96                         if (regulator->dev == dev) {
97                                 mutex_unlock(&rdev->mutex);
98                                 mutex_unlock(&regulator_list_mutex);
99                                 return regulator;
100                         }
101                 }
102                 mutex_unlock(&rdev->mutex);
103         }
104         mutex_unlock(&regulator_list_mutex);
105         return NULL;
106 }
107
108 /* Platform voltage constraint check */
109 static int regulator_check_voltage(struct regulator_dev *rdev,
110                                    int *min_uV, int *max_uV)
111 {
112         BUG_ON(*min_uV > *max_uV);
113
114         if (!rdev->constraints) {
115                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
116                        rdev_get_name(rdev));
117                 return -ENODEV;
118         }
119         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
120                 printk(KERN_ERR "%s: operation not allowed for %s\n",
121                        __func__, rdev_get_name(rdev));
122                 return -EPERM;
123         }
124
125         if (*max_uV > rdev->constraints->max_uV)
126                 *max_uV = rdev->constraints->max_uV;
127         if (*min_uV < rdev->constraints->min_uV)
128                 *min_uV = rdev->constraints->min_uV;
129
130         if (*min_uV > *max_uV)
131                 return -EINVAL;
132
133         return 0;
134 }
135
136 /* current constraint check */
137 static int regulator_check_current_limit(struct regulator_dev *rdev,
138                                         int *min_uA, int *max_uA)
139 {
140         BUG_ON(*min_uA > *max_uA);
141
142         if (!rdev->constraints) {
143                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
144                        rdev_get_name(rdev));
145                 return -ENODEV;
146         }
147         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
148                 printk(KERN_ERR "%s: operation not allowed for %s\n",
149                        __func__, rdev_get_name(rdev));
150                 return -EPERM;
151         }
152
153         if (*max_uA > rdev->constraints->max_uA)
154                 *max_uA = rdev->constraints->max_uA;
155         if (*min_uA < rdev->constraints->min_uA)
156                 *min_uA = rdev->constraints->min_uA;
157
158         if (*min_uA > *max_uA)
159                 return -EINVAL;
160
161         return 0;
162 }
163
164 /* operating mode constraint check */
165 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
166 {
167         switch (mode) {
168         case REGULATOR_MODE_FAST:
169         case REGULATOR_MODE_NORMAL:
170         case REGULATOR_MODE_IDLE:
171         case REGULATOR_MODE_STANDBY:
172                 break;
173         default:
174                 return -EINVAL;
175         }
176
177         if (!rdev->constraints) {
178                 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
179                        rdev_get_name(rdev));
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_get_name(rdev));
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_get_name(rdev));
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_get_name(rdev));
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_get_name(rdev));
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 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
236
237 static ssize_t regulator_uA_show(struct device *dev,
238                                 struct device_attribute *attr, char *buf)
239 {
240         struct regulator_dev *rdev = dev_get_drvdata(dev);
241
242         return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
243 }
244 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
245
246 static ssize_t regulator_name_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, "%s\n", rdev_get_name(rdev));
252 }
253
254 static ssize_t regulator_print_opmode(char *buf, int mode)
255 {
256         switch (mode) {
257         case REGULATOR_MODE_FAST:
258                 return sprintf(buf, "fast\n");
259         case REGULATOR_MODE_NORMAL:
260                 return sprintf(buf, "normal\n");
261         case REGULATOR_MODE_IDLE:
262                 return sprintf(buf, "idle\n");
263         case REGULATOR_MODE_STANDBY:
264                 return sprintf(buf, "standby\n");
265         }
266         return sprintf(buf, "unknown\n");
267 }
268
269 static ssize_t regulator_opmode_show(struct device *dev,
270                                     struct device_attribute *attr, char *buf)
271 {
272         struct regulator_dev *rdev = dev_get_drvdata(dev);
273
274         return regulator_print_opmode(buf, _regulator_get_mode(rdev));
275 }
276 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
277
278 static ssize_t regulator_print_state(char *buf, int state)
279 {
280         if (state > 0)
281                 return sprintf(buf, "enabled\n");
282         else if (state == 0)
283                 return sprintf(buf, "disabled\n");
284         else
285                 return sprintf(buf, "unknown\n");
286 }
287
288 static ssize_t regulator_state_show(struct device *dev,
289                                    struct device_attribute *attr, char *buf)
290 {
291         struct regulator_dev *rdev = dev_get_drvdata(dev);
292         ssize_t ret;
293
294         mutex_lock(&rdev->mutex);
295         ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
296         mutex_unlock(&rdev->mutex);
297
298         return ret;
299 }
300 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
301
302 static ssize_t regulator_status_show(struct device *dev,
303                                    struct device_attribute *attr, char *buf)
304 {
305         struct regulator_dev *rdev = dev_get_drvdata(dev);
306         int status;
307         char *label;
308
309         status = rdev->desc->ops->get_status(rdev);
310         if (status < 0)
311                 return status;
312
313         switch (status) {
314         case REGULATOR_STATUS_OFF:
315                 label = "off";
316                 break;
317         case REGULATOR_STATUS_ON:
318                 label = "on";
319                 break;
320         case REGULATOR_STATUS_ERROR:
321                 label = "error";
322                 break;
323         case REGULATOR_STATUS_FAST:
324                 label = "fast";
325                 break;
326         case REGULATOR_STATUS_NORMAL:
327                 label = "normal";
328                 break;
329         case REGULATOR_STATUS_IDLE:
330                 label = "idle";
331                 break;
332         case REGULATOR_STATUS_STANDBY:
333                 label = "standby";
334                 break;
335         default:
336                 return -ERANGE;
337         }
338
339         return sprintf(buf, "%s\n", label);
340 }
341 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
342
343 static ssize_t regulator_min_uA_show(struct device *dev,
344                                     struct device_attribute *attr, char *buf)
345 {
346         struct regulator_dev *rdev = dev_get_drvdata(dev);
347
348         if (!rdev->constraints)
349                 return sprintf(buf, "constraint not defined\n");
350
351         return sprintf(buf, "%d\n", rdev->constraints->min_uA);
352 }
353 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
354
355 static ssize_t regulator_max_uA_show(struct device *dev,
356                                     struct device_attribute *attr, char *buf)
357 {
358         struct regulator_dev *rdev = dev_get_drvdata(dev);
359
360         if (!rdev->constraints)
361                 return sprintf(buf, "constraint not defined\n");
362
363         return sprintf(buf, "%d\n", rdev->constraints->max_uA);
364 }
365 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
366
367 static ssize_t regulator_min_uV_show(struct device *dev,
368                                     struct device_attribute *attr, char *buf)
369 {
370         struct regulator_dev *rdev = dev_get_drvdata(dev);
371
372         if (!rdev->constraints)
373                 return sprintf(buf, "constraint not defined\n");
374
375         return sprintf(buf, "%d\n", rdev->constraints->min_uV);
376 }
377 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
378
379 static ssize_t regulator_max_uV_show(struct device *dev,
380                                     struct device_attribute *attr, char *buf)
381 {
382         struct regulator_dev *rdev = dev_get_drvdata(dev);
383
384         if (!rdev->constraints)
385                 return sprintf(buf, "constraint not defined\n");
386
387         return sprintf(buf, "%d\n", rdev->constraints->max_uV);
388 }
389 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
390
391 static ssize_t regulator_total_uA_show(struct device *dev,
392                                       struct device_attribute *attr, char *buf)
393 {
394         struct regulator_dev *rdev = dev_get_drvdata(dev);
395         struct regulator *regulator;
396         int uA = 0;
397
398         mutex_lock(&rdev->mutex);
399         list_for_each_entry(regulator, &rdev->consumer_list, list)
400                 uA += regulator->uA_load;
401         mutex_unlock(&rdev->mutex);
402         return sprintf(buf, "%d\n", uA);
403 }
404 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
405
406 static ssize_t regulator_num_users_show(struct device *dev,
407                                       struct device_attribute *attr, char *buf)
408 {
409         struct regulator_dev *rdev = dev_get_drvdata(dev);
410         return sprintf(buf, "%d\n", rdev->use_count);
411 }
412
413 static ssize_t regulator_type_show(struct device *dev,
414                                   struct device_attribute *attr, char *buf)
415 {
416         struct regulator_dev *rdev = dev_get_drvdata(dev);
417
418         switch (rdev->desc->type) {
419         case REGULATOR_VOLTAGE:
420                 return sprintf(buf, "voltage\n");
421         case REGULATOR_CURRENT:
422                 return sprintf(buf, "current\n");
423         }
424         return sprintf(buf, "unknown\n");
425 }
426
427 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
428                                 struct device_attribute *attr, char *buf)
429 {
430         struct regulator_dev *rdev = dev_get_drvdata(dev);
431
432         return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
433 }
434 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
435                 regulator_suspend_mem_uV_show, NULL);
436
437 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
438                                 struct device_attribute *attr, char *buf)
439 {
440         struct regulator_dev *rdev = dev_get_drvdata(dev);
441
442         return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
443 }
444 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
445                 regulator_suspend_disk_uV_show, NULL);
446
447 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
448                                 struct device_attribute *attr, char *buf)
449 {
450         struct regulator_dev *rdev = dev_get_drvdata(dev);
451
452         return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
453 }
454 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
455                 regulator_suspend_standby_uV_show, NULL);
456
457 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
458                                 struct device_attribute *attr, char *buf)
459 {
460         struct regulator_dev *rdev = dev_get_drvdata(dev);
461
462         return regulator_print_opmode(buf,
463                 rdev->constraints->state_mem.mode);
464 }
465 static DEVICE_ATTR(suspend_mem_mode, 0444,
466                 regulator_suspend_mem_mode_show, NULL);
467
468 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
469                                 struct device_attribute *attr, char *buf)
470 {
471         struct regulator_dev *rdev = dev_get_drvdata(dev);
472
473         return regulator_print_opmode(buf,
474                 rdev->constraints->state_disk.mode);
475 }
476 static DEVICE_ATTR(suspend_disk_mode, 0444,
477                 regulator_suspend_disk_mode_show, NULL);
478
479 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
480                                 struct device_attribute *attr, char *buf)
481 {
482         struct regulator_dev *rdev = dev_get_drvdata(dev);
483
484         return regulator_print_opmode(buf,
485                 rdev->constraints->state_standby.mode);
486 }
487 static DEVICE_ATTR(suspend_standby_mode, 0444,
488                 regulator_suspend_standby_mode_show, NULL);
489
490 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
491                                    struct device_attribute *attr, char *buf)
492 {
493         struct regulator_dev *rdev = dev_get_drvdata(dev);
494
495         return regulator_print_state(buf,
496                         rdev->constraints->state_mem.enabled);
497 }
498 static DEVICE_ATTR(suspend_mem_state, 0444,
499                 regulator_suspend_mem_state_show, NULL);
500
501 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
502                                    struct device_attribute *attr, char *buf)
503 {
504         struct regulator_dev *rdev = dev_get_drvdata(dev);
505
506         return regulator_print_state(buf,
507                         rdev->constraints->state_disk.enabled);
508 }
509 static DEVICE_ATTR(suspend_disk_state, 0444,
510                 regulator_suspend_disk_state_show, NULL);
511
512 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
513                                    struct device_attribute *attr, char *buf)
514 {
515         struct regulator_dev *rdev = dev_get_drvdata(dev);
516
517         return regulator_print_state(buf,
518                         rdev->constraints->state_standby.enabled);
519 }
520 static DEVICE_ATTR(suspend_standby_state, 0444,
521                 regulator_suspend_standby_state_show, NULL);
522
523
524 /*
525  * These are the only attributes are present for all regulators.
526  * Other attributes are a function of regulator functionality.
527  */
528 static struct device_attribute regulator_dev_attrs[] = {
529         __ATTR(name, 0444, regulator_name_show, NULL),
530         __ATTR(num_users, 0444, regulator_num_users_show, NULL),
531         __ATTR(type, 0444, regulator_type_show, NULL),
532         __ATTR_NULL,
533 };
534
535 static void regulator_dev_release(struct device *dev)
536 {
537         struct regulator_dev *rdev = dev_get_drvdata(dev);
538         kfree(rdev);
539 }
540
541 static struct class regulator_class = {
542         .name = "regulator",
543         .dev_release = regulator_dev_release,
544         .dev_attrs = regulator_dev_attrs,
545 };
546
547 /* Calculate the new optimum regulator operating mode based on the new total
548  * consumer load. All locks held by caller */
549 static void drms_uA_update(struct regulator_dev *rdev)
550 {
551         struct regulator *sibling;
552         int current_uA = 0, output_uV, input_uV, err;
553         unsigned int mode;
554
555         err = regulator_check_drms(rdev);
556         if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
557             !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
558                 return;
559
560         /* get output voltage */
561         output_uV = rdev->desc->ops->get_voltage(rdev);
562         if (output_uV <= 0)
563                 return;
564
565         /* get input voltage */
566         if (rdev->supply && rdev->supply->desc->ops->get_voltage)
567                 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
568         else
569                 input_uV = rdev->constraints->input_uV;
570         if (input_uV <= 0)
571                 return;
572
573         /* calc total requested load */
574         list_for_each_entry(sibling, &rdev->consumer_list, list)
575                 current_uA += sibling->uA_load;
576
577         /* now get the optimum mode for our new total regulator load */
578         mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
579                                                   output_uV, current_uA);
580
581         /* check the new mode is allowed */
582         err = regulator_check_mode(rdev, mode);
583         if (err == 0)
584                 rdev->desc->ops->set_mode(rdev, mode);
585 }
586
587 static int suspend_set_state(struct regulator_dev *rdev,
588         struct regulator_state *rstate)
589 {
590         int ret = 0;
591         bool can_set_state;
592
593         can_set_state = rdev->desc->ops->set_suspend_enable &&
594                 rdev->desc->ops->set_suspend_disable;
595
596         /* If we have no suspend mode configration don't set anything;
597          * only warn if the driver actually makes the suspend mode
598          * configurable.
599          */
600         if (!rstate->enabled && !rstate->disabled) {
601                 if (can_set_state)
602                         printk(KERN_WARNING "%s: No configuration for %s\n",
603                                __func__, rdev_get_name(rdev));
604                 return 0;
605         }
606
607         if (rstate->enabled && rstate->disabled) {
608                 printk(KERN_ERR "%s: invalid configuration for %s\n",
609                        __func__, rdev_get_name(rdev));
610                 return -EINVAL;
611         }
612
613         if (!can_set_state) {
614                 printk(KERN_ERR "%s: no way to set suspend state\n",
615                         __func__);
616                 return -EINVAL;
617         }
618
619         if (rstate->enabled)
620                 ret = rdev->desc->ops->set_suspend_enable(rdev);
621         else {
622
623                 if (rdev->use_count > 0)
624                         printk(KERN_ERR "%s: is still enabled.. please turn it"
625                                         " off before entering suspend (use_count"
626                                         "  = %d)\n", rdev->desc->name,
627                                         rdev->use_count);
628                 ret = rdev->desc->ops->set_suspend_disable(rdev);
629         }
630         if (ret < 0) {
631                 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
632                 return ret;
633         }
634
635         if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
636                 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
637                 if (ret < 0) {
638                         printk(KERN_ERR "%s: failed to set voltage\n",
639                                 __func__);
640                         return ret;
641                 }
642         }
643
644         if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
645                 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
646                 if (ret < 0) {
647                         printk(KERN_ERR "%s: failed to set mode\n", __func__);
648                         return ret;
649                 }
650         }
651         return ret;
652 }
653
654 /* locks held by caller */
655 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
656 {
657         if (!rdev->constraints)
658                 return -EINVAL;
659
660         switch (state) {
661         case PM_SUSPEND_STANDBY:
662                 return suspend_set_state(rdev,
663                         &rdev->constraints->state_standby);
664         case PM_SUSPEND_MEM:
665                 return suspend_set_state(rdev,
666                         &rdev->constraints->state_mem);
667         case PM_SUSPEND_MAX:
668                 return suspend_set_state(rdev,
669                         &rdev->constraints->state_disk);
670         default:
671                 return -EINVAL;
672         }
673 }
674
675 static void print_constraints(struct regulator_dev *rdev)
676 {
677         struct regulation_constraints *constraints = rdev->constraints;
678         char buf[80] = "";
679         int count = 0;
680         int ret;
681
682         if (constraints->min_uV && constraints->max_uV) {
683                 if (constraints->min_uV == constraints->max_uV)
684                         count += sprintf(buf + count, "%d mV ",
685                                          constraints->min_uV / 1000);
686                 else
687                         count += sprintf(buf + count, "%d <--> %d mV ",
688                                          constraints->min_uV / 1000,
689                                          constraints->max_uV / 1000);
690         }
691
692         if (!constraints->min_uV ||
693             constraints->min_uV != constraints->max_uV) {
694                 ret = _regulator_get_voltage(rdev);
695                 if (ret > 0)
696                         count += sprintf(buf + count, "at %d mV ", ret / 1000);
697         }
698
699         if (constraints->min_uA && constraints->max_uA) {
700                 if (constraints->min_uA == constraints->max_uA)
701                         count += sprintf(buf + count, "%d mA ",
702                                          constraints->min_uA / 1000);
703                 else
704                         count += sprintf(buf + count, "%d <--> %d mA ",
705                                          constraints->min_uA / 1000,
706                                          constraints->max_uA / 1000);
707         }
708
709         if (!constraints->min_uA ||
710             constraints->min_uA != constraints->max_uA) {
711                 ret = _regulator_get_current_limit(rdev);
712                 if (ret > 0)
713                         count += sprintf(buf + count, "at %d mA ", ret / 1000);
714         }
715
716         if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
717                 count += sprintf(buf + count, "fast ");
718         if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
719                 count += sprintf(buf + count, "normal ");
720         if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
721                 count += sprintf(buf + count, "idle ");
722         if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
723                 count += sprintf(buf + count, "standby");
724
725         printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
726 }
727
728 static int machine_constraints_voltage(struct regulator_dev *rdev,
729         struct regulation_constraints *constraints)
730 {
731         struct regulator_ops *ops = rdev->desc->ops;
732         const char *name = rdev_get_name(rdev);
733         int ret;
734
735         /* do we need to apply the constraint voltage */
736         if (rdev->constraints->apply_uV &&
737                 rdev->constraints->min_uV == rdev->constraints->max_uV &&
738                 ops->set_voltage) {
739                 ret = ops->set_voltage(rdev,
740                         rdev->constraints->min_uV, rdev->constraints->max_uV);
741                         if (ret < 0) {
742                                 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
743                                        __func__,
744                                        rdev->constraints->min_uV, name);
745                                 rdev->constraints = NULL;
746                                 return ret;
747                         }
748         }
749
750         /* constrain machine-level voltage specs to fit
751          * the actual range supported by this regulator.
752          */
753         if (ops->list_voltage && rdev->desc->n_voltages) {
754                 int     count = rdev->desc->n_voltages;
755                 int     i;
756                 int     min_uV = INT_MAX;
757                 int     max_uV = INT_MIN;
758                 int     cmin = constraints->min_uV;
759                 int     cmax = constraints->max_uV;
760
761                 /* it's safe to autoconfigure fixed-voltage supplies
762                    and the constraints are used by list_voltage. */
763                 if (count == 1 && !cmin) {
764                         cmin = 1;
765                         cmax = INT_MAX;
766                         constraints->min_uV = cmin;
767                         constraints->max_uV = cmax;
768                 }
769
770                 /* voltage constraints are optional */
771                 if ((cmin == 0) && (cmax == 0))
772                         return 0;
773
774                 /* else require explicit machine-level constraints */
775                 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
776                         pr_err("%s: %s '%s' voltage constraints\n",
777                                        __func__, "invalid", name);
778                         return -EINVAL;
779                 }
780
781                 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
782                 for (i = 0; i < count; i++) {
783                         int     value;
784
785                         value = ops->list_voltage(rdev, i);
786                         if (value <= 0)
787                                 continue;
788
789                         /* maybe adjust [min_uV..max_uV] */
790                         if (value >= cmin && value < min_uV)
791                                 min_uV = value;
792                         if (value <= cmax && value > max_uV)
793                                 max_uV = value;
794                 }
795
796                 /* final: [min_uV..max_uV] valid iff constraints valid */
797                 if (max_uV < min_uV) {
798                         pr_err("%s: %s '%s' voltage constraints\n",
799                                        __func__, "unsupportable", name);
800                         return -EINVAL;
801                 }
802
803                 /* use regulator's subset of machine constraints */
804                 if (constraints->min_uV < min_uV) {
805                         pr_debug("%s: override '%s' %s, %d -> %d\n",
806                                        __func__, name, "min_uV",
807                                         constraints->min_uV, min_uV);
808                         constraints->min_uV = min_uV;
809                 }
810                 if (constraints->max_uV > max_uV) {
811                         pr_debug("%s: override '%s' %s, %d -> %d\n",
812                                        __func__, name, "max_uV",
813                                         constraints->max_uV, max_uV);
814                         constraints->max_uV = max_uV;
815                 }
816         }
817
818         return 0;
819 }
820
821 /**
822  * set_machine_constraints - sets regulator constraints
823  * @rdev: regulator source
824  * @constraints: constraints to apply
825  *
826  * Allows platform initialisation code to define and constrain
827  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
828  * Constraints *must* be set by platform code in order for some
829  * regulator operations to proceed i.e. set_voltage, set_current_limit,
830  * set_mode.
831  */
832 static int set_machine_constraints(struct regulator_dev *rdev,
833         struct regulation_constraints *constraints)
834 {
835         int ret = 0;
836         const char *name;
837         struct regulator_ops *ops = rdev->desc->ops;
838
839         rdev->constraints = constraints;
840
841         name = rdev_get_name(rdev);
842
843         ret = machine_constraints_voltage(rdev, constraints);
844         if (ret != 0)
845                 goto out;
846
847         /* do we need to setup our suspend state */
848         if (constraints->initial_state) {
849                 ret = suspend_prepare(rdev, constraints->initial_state);
850                 if (ret < 0) {
851                         printk(KERN_ERR "%s: failed to set suspend state for %s\n",
852                                __func__, name);
853                         rdev->constraints = NULL;
854                         goto out;
855                 }
856         }
857
858         if (constraints->initial_mode) {
859                 if (!ops->set_mode) {
860                         printk(KERN_ERR "%s: no set_mode operation for %s\n",
861                                __func__, name);
862                         ret = -EINVAL;
863                         goto out;
864                 }
865
866                 ret = ops->set_mode(rdev, constraints->initial_mode);
867                 if (ret < 0) {
868                         printk(KERN_ERR
869                                "%s: failed to set initial mode for %s: %d\n",
870                                __func__, name, ret);
871                         goto out;
872                 }
873         }
874
875         /* If the constraints say the regulator should be on at this point
876          * and we have control then make sure it is enabled.
877          */
878         if ((constraints->always_on || constraints->boot_on) && ops->enable) {
879                 ret = ops->enable(rdev);
880                 if (ret < 0) {
881                         printk(KERN_ERR "%s: failed to enable %s\n",
882                                __func__, name);
883                         rdev->constraints = NULL;
884                         goto out;
885                 }
886         }
887
888         print_constraints(rdev);
889 out:
890         return ret;
891 }
892
893 /**
894  * set_supply - set regulator supply regulator
895  * @rdev: regulator name
896  * @supply_rdev: supply regulator name
897  *
898  * Called by platform initialisation code to set the supply regulator for this
899  * regulator. This ensures that a regulators supply will also be enabled by the
900  * core if it's child is enabled.
901  */
902 static int set_supply(struct regulator_dev *rdev,
903         struct regulator_dev *supply_rdev)
904 {
905         int err;
906
907         err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
908                                 "supply");
909         if (err) {
910                 printk(KERN_ERR
911                        "%s: could not add device link %s err %d\n",
912                        __func__, supply_rdev->dev.kobj.name, err);
913                        goto out;
914         }
915         rdev->supply = supply_rdev;
916         list_add(&rdev->slist, &supply_rdev->supply_list);
917 out:
918         return err;
919 }
920
921 /**
922  * set_consumer_device_supply: Bind a regulator to a symbolic supply
923  * @rdev:         regulator source
924  * @consumer_dev: device the supply applies to
925  * @consumer_dev_name: dev_name() string for device supply applies to
926  * @supply:       symbolic name for supply
927  *
928  * Allows platform initialisation code to map physical regulator
929  * sources to symbolic names for supplies for use by devices.  Devices
930  * should use these symbolic names to request regulators, avoiding the
931  * need to provide board-specific regulator names as platform data.
932  *
933  * Only one of consumer_dev and consumer_dev_name may be specified.
934  */
935 static int set_consumer_device_supply(struct regulator_dev *rdev,
936         struct device *consumer_dev, const char *consumer_dev_name,
937         const char *supply)
938 {
939         struct regulator_map *node;
940         int has_dev;
941
942         if (consumer_dev && consumer_dev_name)
943                 return -EINVAL;
944
945         if (!consumer_dev_name && consumer_dev)
946                 consumer_dev_name = dev_name(consumer_dev);
947
948         if (supply == NULL)
949                 return -EINVAL;
950
951         if (consumer_dev_name != NULL)
952                 has_dev = 1;
953         else
954                 has_dev = 0;
955
956         list_for_each_entry(node, &regulator_map_list, list) {
957                 if (node->dev_name && consumer_dev_name) {
958                         if (strcmp(node->dev_name, consumer_dev_name) != 0)
959                                 continue;
960                 } else if (node->dev_name || consumer_dev_name) {
961                         continue;
962                 }
963
964                 if (strcmp(node->supply, supply) != 0)
965                         continue;
966
967                 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
968                                 dev_name(&node->regulator->dev),
969                                 node->regulator->desc->name,
970                                 supply,
971                                 dev_name(&rdev->dev), rdev_get_name(rdev));
972                 return -EBUSY;
973         }
974
975         node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
976         if (node == NULL)
977                 return -ENOMEM;
978
979         node->regulator = rdev;
980         node->supply = supply;
981
982         if (has_dev) {
983                 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
984                 if (node->dev_name == NULL) {
985                         kfree(node);
986                         return -ENOMEM;
987                 }
988         }
989
990         list_add(&node->list, &regulator_map_list);
991         return 0;
992 }
993
994 static void unset_regulator_supplies(struct regulator_dev *rdev)
995 {
996         struct regulator_map *node, *n;
997
998         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
999                 if (rdev == node->regulator) {
1000                         list_del(&node->list);
1001                         kfree(node->dev_name);
1002                         kfree(node);
1003                 }
1004         }
1005 }
1006
1007 #define REG_STR_SIZE    32
1008
1009 static struct regulator *create_regulator(struct regulator_dev *rdev,
1010                                           struct device *dev,
1011                                           const char *supply_name)
1012 {
1013         struct regulator *regulator;
1014         char buf[REG_STR_SIZE];
1015         int err, size;
1016
1017         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1018         if (regulator == NULL)
1019                 return NULL;
1020
1021         mutex_lock(&rdev->mutex);
1022         regulator->rdev = rdev;
1023         list_add(&regulator->list, &rdev->consumer_list);
1024
1025         if (dev) {
1026                 /* create a 'requested_microamps_name' sysfs entry */
1027                 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1028                         supply_name);
1029                 if (size >= REG_STR_SIZE)
1030                         goto overflow_err;
1031
1032                 regulator->dev = dev;
1033                 sysfs_attr_init(&regulator->dev_attr.attr);
1034                 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1035                 if (regulator->dev_attr.attr.name == NULL)
1036                         goto attr_name_err;
1037
1038                 regulator->dev_attr.attr.mode = 0444;
1039                 regulator->dev_attr.show = device_requested_uA_show;
1040                 err = device_create_file(dev, &regulator->dev_attr);
1041                 if (err < 0) {
1042                         printk(KERN_WARNING "%s: could not add regulator_dev"
1043                                 " load sysfs\n", __func__);
1044                         goto attr_name_err;
1045                 }
1046
1047                 /* also add a link to the device sysfs entry */
1048                 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1049                                  dev->kobj.name, supply_name);
1050                 if (size >= REG_STR_SIZE)
1051                         goto attr_err;
1052
1053                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1054                 if (regulator->supply_name == NULL)
1055                         goto attr_err;
1056
1057                 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1058                                         buf);
1059                 if (err) {
1060                         printk(KERN_WARNING
1061                                "%s: could not add device link %s err %d\n",
1062                                __func__, dev->kobj.name, err);
1063                         device_remove_file(dev, &regulator->dev_attr);
1064                         goto link_name_err;
1065                 }
1066         }
1067         mutex_unlock(&rdev->mutex);
1068         return regulator;
1069 link_name_err:
1070         kfree(regulator->supply_name);
1071 attr_err:
1072         device_remove_file(regulator->dev, &regulator->dev_attr);
1073 attr_name_err:
1074         kfree(regulator->dev_attr.attr.name);
1075 overflow_err:
1076         list_del(&regulator->list);
1077         kfree(regulator);
1078         mutex_unlock(&rdev->mutex);
1079         return NULL;
1080 }
1081
1082 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1083 {
1084         if (!rdev->desc->ops->enable_time)
1085                 return 0;
1086         return rdev->desc->ops->enable_time(rdev);
1087 }
1088
1089 /* Internal regulator request function */
1090 static struct regulator *_regulator_get(struct device *dev, const char *id,
1091                                         int exclusive)
1092 {
1093         struct regulator_dev *rdev;
1094         struct regulator_map *map;
1095         struct regulator *regulator = ERR_PTR(-ENODEV);
1096         const char *devname = NULL;
1097         int ret;
1098
1099         if (id == NULL) {
1100                 printk(KERN_ERR "regulator: get() with no identifier\n");
1101                 return regulator;
1102         }
1103
1104         if (dev)
1105                 devname = dev_name(dev);
1106
1107         mutex_lock(&regulator_list_mutex);
1108
1109         list_for_each_entry(map, &regulator_map_list, list) {
1110                 /* If the mapping has a device set up it must match */
1111                 if (map->dev_name &&
1112                     (!devname || strcmp(map->dev_name, devname)))
1113                         continue;
1114
1115                 if (strcmp(map->supply, id) == 0) {
1116                         rdev = map->regulator;
1117                         goto found;
1118                 }
1119         }
1120
1121 #ifdef CONFIG_REGULATOR_DUMMY
1122         if (!devname)
1123                 devname = "deviceless";
1124
1125         /* If the board didn't flag that it was fully constrained then
1126          * substitute in a dummy regulator so consumers can continue.
1127          */
1128         if (!has_full_constraints) {
1129                 pr_warning("%s supply %s not found, using dummy regulator\n",
1130                            devname, id);
1131                 rdev = dummy_regulator_rdev;
1132                 goto found;
1133         }
1134 #endif
1135
1136         mutex_unlock(&regulator_list_mutex);
1137         return regulator;
1138
1139 found:
1140         if (rdev->exclusive) {
1141                 regulator = ERR_PTR(-EPERM);
1142                 goto out;
1143         }
1144
1145         if (exclusive && rdev->open_count) {
1146                 regulator = ERR_PTR(-EBUSY);
1147                 goto out;
1148         }
1149
1150         if (!try_module_get(rdev->owner))
1151                 goto out;
1152
1153         regulator = create_regulator(rdev, dev, id);
1154         if (regulator == NULL) {
1155                 regulator = ERR_PTR(-ENOMEM);
1156                 module_put(rdev->owner);
1157         }
1158
1159         rdev->open_count++;
1160         if (exclusive) {
1161                 rdev->exclusive = 1;
1162
1163                 ret = _regulator_is_enabled(rdev);
1164                 if (ret > 0)
1165                         rdev->use_count = 1;
1166                 else
1167                         rdev->use_count = 0;
1168         }
1169
1170 out:
1171         mutex_unlock(&regulator_list_mutex);
1172
1173         return regulator;
1174 }
1175
1176 /**
1177  * regulator_get - lookup and obtain a reference to a regulator.
1178  * @dev: device for regulator "consumer"
1179  * @id: Supply name or regulator ID.
1180  *
1181  * Returns a struct regulator corresponding to the regulator producer,
1182  * or IS_ERR() condition containing errno.
1183  *
1184  * Use of supply names configured via regulator_set_device_supply() is
1185  * strongly encouraged.  It is recommended that the supply name used
1186  * should match the name used for the supply and/or the relevant
1187  * device pins in the datasheet.
1188  */
1189 struct regulator *regulator_get(struct device *dev, const char *id)
1190 {
1191         return _regulator_get(dev, id, 0);
1192 }
1193 EXPORT_SYMBOL_GPL(regulator_get);
1194
1195 /**
1196  * regulator_get_exclusive - obtain exclusive access to a regulator.
1197  * @dev: device for regulator "consumer"
1198  * @id: Supply name or regulator ID.
1199  *
1200  * Returns a struct regulator corresponding to the regulator producer,
1201  * or IS_ERR() condition containing errno.  Other consumers will be
1202  * unable to obtain this reference is held and the use count for the
1203  * regulator will be initialised to reflect the current state of the
1204  * regulator.
1205  *
1206  * This is intended for use by consumers which cannot tolerate shared
1207  * use of the regulator such as those which need to force the
1208  * regulator off for correct operation of the hardware they are
1209  * controlling.
1210  *
1211  * Use of supply names configured via regulator_set_device_supply() is
1212  * strongly encouraged.  It is recommended that the supply name used
1213  * should match the name used for the supply and/or the relevant
1214  * device pins in the datasheet.
1215  */
1216 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1217 {
1218         return _regulator_get(dev, id, 1);
1219 }
1220 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1221
1222 /**
1223  * regulator_put - "free" the regulator source
1224  * @regulator: regulator source
1225  *
1226  * Note: drivers must ensure that all regulator_enable calls made on this
1227  * regulator source are balanced by regulator_disable calls prior to calling
1228  * this function.
1229  */
1230 void regulator_put(struct regulator *regulator)
1231 {
1232         struct regulator_dev *rdev;
1233
1234         if (regulator == NULL || IS_ERR(regulator))
1235                 return;
1236
1237         mutex_lock(&regulator_list_mutex);
1238         rdev = regulator->rdev;
1239
1240         /* remove any sysfs entries */
1241         if (regulator->dev) {
1242                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1243                 kfree(regulator->supply_name);
1244                 device_remove_file(regulator->dev, &regulator->dev_attr);
1245                 kfree(regulator->dev_attr.attr.name);
1246         }
1247         list_del(&regulator->list);
1248         kfree(regulator);
1249
1250         rdev->open_count--;
1251         rdev->exclusive = 0;
1252
1253         module_put(rdev->owner);
1254         mutex_unlock(&regulator_list_mutex);
1255 }
1256 EXPORT_SYMBOL_GPL(regulator_put);
1257
1258 static int _regulator_can_change_status(struct regulator_dev *rdev)
1259 {
1260         if (!rdev->constraints)
1261                 return 0;
1262
1263         if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1264                 return 1;
1265         else
1266                 return 0;
1267 }
1268
1269 /* locks held by regulator_enable() */
1270 static int _regulator_enable(struct regulator_dev *rdev)
1271 {
1272         int ret, delay;
1273
1274         /* do we need to enable the supply regulator first */
1275         if (rdev->supply) {
1276                 ret = _regulator_enable(rdev->supply);
1277                 if (ret < 0) {
1278                         printk(KERN_ERR "%s: failed to enable %s: %d\n",
1279                                __func__, rdev_get_name(rdev), ret);
1280                         return ret;
1281                 }
1282         }
1283
1284         /* check voltage and requested load before enabling */
1285         if (rdev->constraints &&
1286             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1287                 drms_uA_update(rdev);
1288
1289         if (rdev->use_count == 0) {
1290                 /* The regulator may on if it's not switchable or left on */
1291                 ret = _regulator_is_enabled(rdev);
1292                 if (ret == -EINVAL || ret == 0) {
1293                         if (!_regulator_can_change_status(rdev))
1294                                 return -EPERM;
1295
1296                         if (!rdev->desc->ops->enable)
1297                                 return -EINVAL;
1298
1299                         /* Query before enabling in case configuration
1300                          * dependant.  */
1301                         ret = _regulator_get_enable_time(rdev);
1302                         if (ret >= 0) {
1303                                 delay = ret;
1304                         } else {
1305                                 printk(KERN_WARNING
1306                                         "%s: enable_time() failed for %s: %d\n",
1307                                         __func__, rdev_get_name(rdev),
1308                                         ret);
1309                                 delay = 0;
1310                         }
1311
1312                         /* Allow the regulator to ramp; it would be useful
1313                          * to extend this for bulk operations so that the
1314                          * regulators can ramp together.  */
1315                         ret = rdev->desc->ops->enable(rdev);
1316                         if (ret < 0)
1317                                 return ret;
1318
1319                         if (delay >= 1000)
1320                                 mdelay(delay / 1000);
1321                         else if (delay)
1322                                 udelay(delay);
1323
1324                 } else if (ret < 0) {
1325                         printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
1326                                __func__, rdev_get_name(rdev), ret);
1327                         return ret;
1328                 }
1329                 /* Fallthrough on positive return values - already enabled */
1330         }
1331
1332         rdev->use_count++;
1333
1334         return 0;
1335 }
1336
1337 /**
1338  * regulator_enable - enable regulator output
1339  * @regulator: regulator source
1340  *
1341  * Request that the regulator be enabled with the regulator output at
1342  * the predefined voltage or current value.  Calls to regulator_enable()
1343  * must be balanced with calls to regulator_disable().
1344  *
1345  * NOTE: the output value can be set by other drivers, boot loader or may be
1346  * hardwired in the regulator.
1347  */
1348 int regulator_enable(struct regulator *regulator)
1349 {
1350         struct regulator_dev *rdev = regulator->rdev;
1351         int ret = 0;
1352
1353         mutex_lock(&rdev->mutex);
1354         ret = _regulator_enable(rdev);
1355         mutex_unlock(&rdev->mutex);
1356         return ret;
1357 }
1358 EXPORT_SYMBOL_GPL(regulator_enable);
1359
1360 /* locks held by regulator_disable() */
1361 static int _regulator_disable(struct regulator_dev *rdev)
1362 {
1363         int ret = 0;
1364
1365         if (WARN(rdev->use_count <= 0,
1366                         "unbalanced disables for %s\n",
1367                         rdev_get_name(rdev)))
1368                 return -EIO;
1369
1370         /* are we the last user and permitted to disable ? */
1371         if (rdev->use_count == 1 &&
1372             (rdev->constraints && !rdev->constraints->always_on)) {
1373
1374                 /* we are last user */
1375                 if (_regulator_can_change_status(rdev) &&
1376                     rdev->desc->ops->disable) {
1377                         ret = rdev->desc->ops->disable(rdev);
1378                         if (ret < 0) {
1379                                 printk(KERN_ERR "%s: failed to disable %s\n",
1380                                        __func__, rdev_get_name(rdev));
1381                                 return ret;
1382                         }
1383
1384                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1385                                              NULL);
1386                 }
1387
1388                 /* decrease our supplies ref count and disable if required */
1389                 if (rdev->supply)
1390                         _regulator_disable(rdev->supply);
1391
1392                 rdev->use_count = 0;
1393         } else if (rdev->use_count > 1) {
1394
1395                 if (rdev->constraints &&
1396                         (rdev->constraints->valid_ops_mask &
1397                         REGULATOR_CHANGE_DRMS))
1398                         drms_uA_update(rdev);
1399
1400                 rdev->use_count--;
1401         }
1402         return ret;
1403 }
1404
1405 /**
1406  * regulator_disable - disable regulator output
1407  * @regulator: regulator source
1408  *
1409  * Disable the regulator output voltage or current.  Calls to
1410  * regulator_enable() must be balanced with calls to
1411  * regulator_disable().
1412  *
1413  * NOTE: this will only disable the regulator output if no other consumer
1414  * devices have it enabled, the regulator device supports disabling and
1415  * machine constraints permit this operation.
1416  */
1417 int regulator_disable(struct regulator *regulator)
1418 {
1419         struct regulator_dev *rdev = regulator->rdev;
1420         int ret = 0;
1421
1422         mutex_lock(&rdev->mutex);
1423         ret = _regulator_disable(rdev);
1424         mutex_unlock(&rdev->mutex);
1425         return ret;
1426 }
1427 EXPORT_SYMBOL_GPL(regulator_disable);
1428
1429 /* locks held by regulator_force_disable() */
1430 static int _regulator_force_disable(struct regulator_dev *rdev)
1431 {
1432         int ret = 0;
1433
1434         /* force disable */
1435         if (rdev->desc->ops->disable) {
1436                 /* ah well, who wants to live forever... */
1437                 ret = rdev->desc->ops->disable(rdev);
1438                 if (ret < 0) {
1439                         printk(KERN_ERR "%s: failed to force disable %s\n",
1440                                __func__, rdev_get_name(rdev));
1441                         return ret;
1442                 }
1443                 /* notify other consumers that power has been forced off */
1444                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1445                         REGULATOR_EVENT_DISABLE, NULL);
1446         }
1447
1448         /* decrease our supplies ref count and disable if required */
1449         if (rdev->supply)
1450                 _regulator_disable(rdev->supply);
1451
1452         rdev->use_count = 0;
1453         return ret;
1454 }
1455
1456 /**
1457  * regulator_force_disable - force disable regulator output
1458  * @regulator: regulator source
1459  *
1460  * Forcibly disable the regulator output voltage or current.
1461  * NOTE: this *will* disable the regulator output even if other consumer
1462  * devices have it enabled. This should be used for situations when device
1463  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1464  */
1465 int regulator_force_disable(struct regulator *regulator)
1466 {
1467         int ret;
1468
1469         mutex_lock(&regulator->rdev->mutex);
1470         regulator->uA_load = 0;
1471         ret = _regulator_force_disable(regulator->rdev);
1472         mutex_unlock(&regulator->rdev->mutex);
1473         return ret;
1474 }
1475 EXPORT_SYMBOL_GPL(regulator_force_disable);
1476
1477 static int _regulator_is_enabled(struct regulator_dev *rdev)
1478 {
1479         /* If we don't know then assume that the regulator is always on */
1480         if (!rdev->desc->ops->is_enabled)
1481                 return 1;
1482
1483         return rdev->desc->ops->is_enabled(rdev);
1484 }
1485
1486 /**
1487  * regulator_is_enabled - is the regulator output enabled
1488  * @regulator: regulator source
1489  *
1490  * Returns positive if the regulator driver backing the source/client
1491  * has requested that the device be enabled, zero if it hasn't, else a
1492  * negative errno code.
1493  *
1494  * Note that the device backing this regulator handle can have multiple
1495  * users, so it might be enabled even if regulator_enable() was never
1496  * called for this particular source.
1497  */
1498 int regulator_is_enabled(struct regulator *regulator)
1499 {
1500         int ret;
1501
1502         mutex_lock(&regulator->rdev->mutex);
1503         ret = _regulator_is_enabled(regulator->rdev);
1504         mutex_unlock(&regulator->rdev->mutex);
1505
1506         return ret;
1507 }
1508 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1509
1510 /**
1511  * regulator_count_voltages - count regulator_list_voltage() selectors
1512  * @regulator: regulator source
1513  *
1514  * Returns number of selectors, or negative errno.  Selectors are
1515  * numbered starting at zero, and typically correspond to bitfields
1516  * in hardware registers.
1517  */
1518 int regulator_count_voltages(struct regulator *regulator)
1519 {
1520         struct regulator_dev    *rdev = regulator->rdev;
1521
1522         return rdev->desc->n_voltages ? : -EINVAL;
1523 }
1524 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1525
1526 /**
1527  * regulator_list_voltage - enumerate supported voltages
1528  * @regulator: regulator source
1529  * @selector: identify voltage to list
1530  * Context: can sleep
1531  *
1532  * Returns a voltage that can be passed to @regulator_set_voltage(),
1533  * zero if this selector code can't be used on this system, or a
1534  * negative errno.
1535  */
1536 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1537 {
1538         struct regulator_dev    *rdev = regulator->rdev;
1539         struct regulator_ops    *ops = rdev->desc->ops;
1540         int                     ret;
1541
1542         if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1543                 return -EINVAL;
1544
1545         mutex_lock(&rdev->mutex);
1546         ret = ops->list_voltage(rdev, selector);
1547         mutex_unlock(&rdev->mutex);
1548
1549         if (ret > 0) {
1550                 if (ret < rdev->constraints->min_uV)
1551                         ret = 0;
1552                 else if (ret > rdev->constraints->max_uV)
1553                         ret = 0;
1554         }
1555
1556         return ret;
1557 }
1558 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1559
1560 /**
1561  * regulator_is_supported_voltage - check if a voltage range can be supported
1562  *
1563  * @regulator: Regulator to check.
1564  * @min_uV: Minimum required voltage in uV.
1565  * @max_uV: Maximum required voltage in uV.
1566  *
1567  * Returns a boolean or a negative error code.
1568  */
1569 int regulator_is_supported_voltage(struct regulator *regulator,
1570                                    int min_uV, int max_uV)
1571 {
1572         int i, voltages, ret;
1573
1574         ret = regulator_count_voltages(regulator);
1575         if (ret < 0)
1576                 return ret;
1577         voltages = ret;
1578
1579         for (i = 0; i < voltages; i++) {
1580                 ret = regulator_list_voltage(regulator, i);
1581
1582                 if (ret >= min_uV && ret <= max_uV)
1583                         return 1;
1584         }
1585
1586         return 0;
1587 }
1588
1589 /**
1590  * regulator_set_voltage - set regulator output voltage
1591  * @regulator: regulator source
1592  * @min_uV: Minimum required voltage in uV
1593  * @max_uV: Maximum acceptable voltage in uV
1594  *
1595  * Sets a voltage regulator to the desired output voltage. This can be set
1596  * during any regulator state. IOW, regulator can be disabled or enabled.
1597  *
1598  * If the regulator is enabled then the voltage will change to the new value
1599  * immediately otherwise if the regulator is disabled the regulator will
1600  * output at the new voltage when enabled.
1601  *
1602  * NOTE: If the regulator is shared between several devices then the lowest
1603  * request voltage that meets the system constraints will be used.
1604  * Regulator system constraints must be set for this regulator before
1605  * calling this function otherwise this call will fail.
1606  */
1607 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1608 {
1609         struct regulator_dev *rdev = regulator->rdev;
1610         int ret;
1611
1612         mutex_lock(&rdev->mutex);
1613
1614         /* sanity check */
1615         if (!rdev->desc->ops->set_voltage) {
1616                 ret = -EINVAL;
1617                 goto out;
1618         }
1619
1620         /* constraints check */
1621         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1622         if (ret < 0)
1623                 goto out;
1624         regulator->min_uV = min_uV;
1625         regulator->max_uV = max_uV;
1626         ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1627
1628 out:
1629         _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1630         mutex_unlock(&rdev->mutex);
1631         return ret;
1632 }
1633 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1634
1635 static int _regulator_get_voltage(struct regulator_dev *rdev)
1636 {
1637         /* sanity check */
1638         if (rdev->desc->ops->get_voltage)
1639                 return rdev->desc->ops->get_voltage(rdev);
1640         else
1641                 return -EINVAL;
1642 }
1643
1644 /**
1645  * regulator_get_voltage - get regulator output voltage
1646  * @regulator: regulator source
1647  *
1648  * This returns the current regulator voltage in uV.
1649  *
1650  * NOTE: If the regulator is disabled it will return the voltage value. This
1651  * function should not be used to determine regulator state.
1652  */
1653 int regulator_get_voltage(struct regulator *regulator)
1654 {
1655         int ret;
1656
1657         mutex_lock(&regulator->rdev->mutex);
1658
1659         ret = _regulator_get_voltage(regulator->rdev);
1660
1661         mutex_unlock(&regulator->rdev->mutex);
1662
1663         return ret;
1664 }
1665 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1666
1667 /**
1668  * regulator_set_current_limit - set regulator output current limit
1669  * @regulator: regulator source
1670  * @min_uA: Minimuum supported current in uA
1671  * @max_uA: Maximum supported current in uA
1672  *
1673  * Sets current sink to the desired output current. This can be set during
1674  * any regulator state. IOW, regulator can be disabled or enabled.
1675  *
1676  * If the regulator is enabled then the current will change to the new value
1677  * immediately otherwise if the regulator is disabled the regulator will
1678  * output at the new current when enabled.
1679  *
1680  * NOTE: Regulator system constraints must be set for this regulator before
1681  * calling this function otherwise this call will fail.
1682  */
1683 int regulator_set_current_limit(struct regulator *regulator,
1684                                int min_uA, int max_uA)
1685 {
1686         struct regulator_dev *rdev = regulator->rdev;
1687         int ret;
1688
1689         mutex_lock(&rdev->mutex);
1690
1691         /* sanity check */
1692         if (!rdev->desc->ops->set_current_limit) {
1693                 ret = -EINVAL;
1694                 goto out;
1695         }
1696
1697         /* constraints check */
1698         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1699         if (ret < 0)
1700                 goto out;
1701
1702         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1703 out:
1704         mutex_unlock(&rdev->mutex);
1705         return ret;
1706 }
1707 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1708
1709 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1710 {
1711         int ret;
1712
1713         mutex_lock(&rdev->mutex);
1714
1715         /* sanity check */
1716         if (!rdev->desc->ops->get_current_limit) {
1717                 ret = -EINVAL;
1718                 goto out;
1719         }
1720
1721         ret = rdev->desc->ops->get_current_limit(rdev);
1722 out:
1723         mutex_unlock(&rdev->mutex);
1724         return ret;
1725 }
1726
1727 /**
1728  * regulator_get_current_limit - get regulator output current
1729  * @regulator: regulator source
1730  *
1731  * This returns the current supplied by the specified current sink in uA.
1732  *
1733  * NOTE: If the regulator is disabled it will return the current value. This
1734  * function should not be used to determine regulator state.
1735  */
1736 int regulator_get_current_limit(struct regulator *regulator)
1737 {
1738         return _regulator_get_current_limit(regulator->rdev);
1739 }
1740 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1741
1742 /**
1743  * regulator_set_mode - set regulator operating mode
1744  * @regulator: regulator source
1745  * @mode: operating mode - one of the REGULATOR_MODE constants
1746  *
1747  * Set regulator operating mode to increase regulator efficiency or improve
1748  * regulation performance.
1749  *
1750  * NOTE: Regulator system constraints must be set for this regulator before
1751  * calling this function otherwise this call will fail.
1752  */
1753 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1754 {
1755         struct regulator_dev *rdev = regulator->rdev;
1756         int ret;
1757         int regulator_curr_mode;
1758
1759         mutex_lock(&rdev->mutex);
1760
1761         /* sanity check */
1762         if (!rdev->desc->ops->set_mode) {
1763                 ret = -EINVAL;
1764                 goto out;
1765         }
1766
1767         /* return if the same mode is requested */
1768         if (rdev->desc->ops->get_mode) {
1769                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1770                 if (regulator_curr_mode == mode) {
1771                         ret = 0;
1772                         goto out;
1773                 }
1774         }
1775
1776         /* constraints check */
1777         ret = regulator_check_mode(rdev, mode);
1778         if (ret < 0)
1779                 goto out;
1780
1781         ret = rdev->desc->ops->set_mode(rdev, mode);
1782 out:
1783         mutex_unlock(&rdev->mutex);
1784         return ret;
1785 }
1786 EXPORT_SYMBOL_GPL(regulator_set_mode);
1787
1788 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1789 {
1790         int ret;
1791
1792         mutex_lock(&rdev->mutex);
1793
1794         /* sanity check */
1795         if (!rdev->desc->ops->get_mode) {
1796                 ret = -EINVAL;
1797                 goto out;
1798         }
1799
1800         ret = rdev->desc->ops->get_mode(rdev);
1801 out:
1802         mutex_unlock(&rdev->mutex);
1803         return ret;
1804 }
1805
1806 /**
1807  * regulator_get_mode - get regulator operating mode
1808  * @regulator: regulator source
1809  *
1810  * Get the current regulator operating mode.
1811  */
1812 unsigned int regulator_get_mode(struct regulator *regulator)
1813 {
1814         return _regulator_get_mode(regulator->rdev);
1815 }
1816 EXPORT_SYMBOL_GPL(regulator_get_mode);
1817
1818 /**
1819  * regulator_set_optimum_mode - set regulator optimum operating mode
1820  * @regulator: regulator source
1821  * @uA_load: load current
1822  *
1823  * Notifies the regulator core of a new device load. This is then used by
1824  * DRMS (if enabled by constraints) to set the most efficient regulator
1825  * operating mode for the new regulator loading.
1826  *
1827  * Consumer devices notify their supply regulator of the maximum power
1828  * they will require (can be taken from device datasheet in the power
1829  * consumption tables) when they change operational status and hence power
1830  * state. Examples of operational state changes that can affect power
1831  * consumption are :-
1832  *
1833  *    o Device is opened / closed.
1834  *    o Device I/O is about to begin or has just finished.
1835  *    o Device is idling in between work.
1836  *
1837  * This information is also exported via sysfs to userspace.
1838  *
1839  * DRMS will sum the total requested load on the regulator and change
1840  * to the most efficient operating mode if platform constraints allow.
1841  *
1842  * Returns the new regulator mode or error.
1843  */
1844 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1845 {
1846         struct regulator_dev *rdev = regulator->rdev;
1847         struct regulator *consumer;
1848         int ret, output_uV, input_uV, total_uA_load = 0;
1849         unsigned int mode;
1850
1851         mutex_lock(&rdev->mutex);
1852
1853         regulator->uA_load = uA_load;
1854         ret = regulator_check_drms(rdev);
1855         if (ret < 0)
1856                 goto out;
1857         ret = -EINVAL;
1858
1859         /* sanity check */
1860         if (!rdev->desc->ops->get_optimum_mode)
1861                 goto out;
1862
1863         /* get output voltage */
1864         output_uV = rdev->desc->ops->get_voltage(rdev);
1865         if (output_uV <= 0) {
1866                 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1867                         __func__, rdev_get_name(rdev));
1868                 goto out;
1869         }
1870
1871         /* get input voltage */
1872         if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1873                 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1874         else
1875                 input_uV = rdev->constraints->input_uV;
1876         if (input_uV <= 0) {
1877                 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1878                         __func__, rdev_get_name(rdev));
1879                 goto out;
1880         }
1881
1882         /* calc total requested load for this regulator */
1883         list_for_each_entry(consumer, &rdev->consumer_list, list)
1884                 total_uA_load += consumer->uA_load;
1885
1886         mode = rdev->desc->ops->get_optimum_mode(rdev,
1887                                                  input_uV, output_uV,
1888                                                  total_uA_load);
1889         ret = regulator_check_mode(rdev, mode);
1890         if (ret < 0) {
1891                 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1892                         " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
1893                         total_uA_load, input_uV, output_uV);
1894                 goto out;
1895         }
1896
1897         ret = rdev->desc->ops->set_mode(rdev, mode);
1898         if (ret < 0) {
1899                 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1900                         __func__, mode, rdev_get_name(rdev));
1901                 goto out;
1902         }
1903         ret = mode;
1904 out:
1905         mutex_unlock(&rdev->mutex);
1906         return ret;
1907 }
1908 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1909
1910 /**
1911  * regulator_register_notifier - register regulator event notifier
1912  * @regulator: regulator source
1913  * @nb: notifier block
1914  *
1915  * Register notifier block to receive regulator events.
1916  */
1917 int regulator_register_notifier(struct regulator *regulator,
1918                               struct notifier_block *nb)
1919 {
1920         return blocking_notifier_chain_register(&regulator->rdev->notifier,
1921                                                 nb);
1922 }
1923 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1924
1925 /**
1926  * regulator_unregister_notifier - unregister regulator event notifier
1927  * @regulator: regulator source
1928  * @nb: notifier block
1929  *
1930  * Unregister regulator event notifier block.
1931  */
1932 int regulator_unregister_notifier(struct regulator *regulator,
1933                                 struct notifier_block *nb)
1934 {
1935         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1936                                                   nb);
1937 }
1938 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1939
1940 /* notify regulator consumers and downstream regulator consumers.
1941  * Note mutex must be held by caller.
1942  */
1943 static void _notifier_call_chain(struct regulator_dev *rdev,
1944                                   unsigned long event, void *data)
1945 {
1946         struct regulator_dev *_rdev;
1947
1948         /* call rdev chain first */
1949         blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1950
1951         /* now notify regulator we supply */
1952         list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1953                 if (_rdev->supply == rdev)
1954                      continue;
1955                 mutex_lock(&_rdev->mutex);
1956                 _notifier_call_chain(_rdev, event, data);
1957                 mutex_unlock(&_rdev->mutex);
1958         }
1959 }
1960
1961 /**
1962  * regulator_bulk_get - get multiple regulator consumers
1963  *
1964  * @dev:           Device to supply
1965  * @num_consumers: Number of consumers to register
1966  * @consumers:     Configuration of consumers; clients are stored here.
1967  *
1968  * @return 0 on success, an errno on failure.
1969  *
1970  * This helper function allows drivers to get several regulator
1971  * consumers in one operation.  If any of the regulators cannot be
1972  * acquired then any regulators that were allocated will be freed
1973  * before returning to the caller.
1974  */
1975 int regulator_bulk_get(struct device *dev, int num_consumers,
1976                        struct regulator_bulk_data *consumers)
1977 {
1978         int i;
1979         int ret;
1980
1981         for (i = 0; i < num_consumers; i++)
1982                 consumers[i].consumer = NULL;
1983
1984         for (i = 0; i < num_consumers; i++) {
1985                 consumers[i].consumer = regulator_get(dev,
1986                                                       consumers[i].supply);
1987                 if (IS_ERR(consumers[i].consumer)) {
1988                         ret = PTR_ERR(consumers[i].consumer);
1989                         dev_err(dev, "Failed to get supply '%s': %d\n",
1990                                 consumers[i].supply, ret);
1991                         consumers[i].consumer = NULL;
1992                         goto err;
1993                 }
1994         }
1995
1996         return 0;
1997
1998 err:
1999         for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2000                 regulator_put(consumers[i].consumer);
2001
2002         return ret;
2003 }
2004 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2005
2006 /**
2007  * regulator_bulk_enable - enable multiple regulator consumers
2008  *
2009  * @num_consumers: Number of consumers
2010  * @consumers:     Consumer data; clients are stored here.
2011  * @return         0 on success, an errno on failure
2012  *
2013  * This convenience API allows consumers to enable multiple regulator
2014  * clients in a single API call.  If any consumers cannot be enabled
2015  * then any others that were enabled will be disabled again prior to
2016  * return.
2017  */
2018 int regulator_bulk_enable(int num_consumers,
2019                           struct regulator_bulk_data *consumers)
2020 {
2021         int i;
2022         int ret;
2023
2024         for (i = 0; i < num_consumers; i++) {
2025                 ret = regulator_enable(consumers[i].consumer);
2026                 if (ret != 0)
2027                         goto err;
2028         }
2029
2030         return 0;
2031
2032 err:
2033         printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
2034         for (--i; i >= 0; --i)
2035                 regulator_disable(consumers[i].consumer);
2036
2037         return ret;
2038 }
2039 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2040
2041 /**
2042  * regulator_bulk_disable - disable multiple regulator consumers
2043  *
2044  * @num_consumers: Number of consumers
2045  * @consumers:     Consumer data; clients are stored here.
2046  * @return         0 on success, an errno on failure
2047  *
2048  * This convenience API allows consumers to disable multiple regulator
2049  * clients in a single API call.  If any consumers cannot be enabled
2050  * then any others that were disabled will be enabled again prior to
2051  * return.
2052  */
2053 int regulator_bulk_disable(int num_consumers,
2054                            struct regulator_bulk_data *consumers)
2055 {
2056         int i;
2057         int ret;
2058
2059         for (i = num_consumers - 1; i >= 0; --i) {
2060                 ret = regulator_disable(consumers[i].consumer);
2061                 if (ret != 0)
2062                         goto err;
2063         }
2064
2065         return 0;
2066
2067 err:
2068         printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2069                ret);
2070         for (++i; i < num_consumers; ++i)
2071                 regulator_enable(consumers[i].consumer);
2072
2073         return ret;
2074 }
2075 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2076
2077 /**
2078  * regulator_bulk_free - free multiple regulator consumers
2079  *
2080  * @num_consumers: Number of consumers
2081  * @consumers:     Consumer data; clients are stored here.
2082  *
2083  * This convenience API allows consumers to free multiple regulator
2084  * clients in a single API call.
2085  */
2086 void regulator_bulk_free(int num_consumers,
2087                          struct regulator_bulk_data *consumers)
2088 {
2089         int i;
2090
2091         for (i = 0; i < num_consumers; i++) {
2092                 regulator_put(consumers[i].consumer);
2093                 consumers[i].consumer = NULL;
2094         }
2095 }
2096 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2097
2098 /**
2099  * regulator_notifier_call_chain - call regulator event notifier
2100  * @rdev: regulator source
2101  * @event: notifier block
2102  * @data: callback-specific data.
2103  *
2104  * Called by regulator drivers to notify clients a regulator event has
2105  * occurred. We also notify regulator clients downstream.
2106  * Note lock must be held by caller.
2107  */
2108 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2109                                   unsigned long event, void *data)
2110 {
2111         _notifier_call_chain(rdev, event, data);
2112         return NOTIFY_DONE;
2113
2114 }
2115 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2116
2117 /**
2118  * regulator_mode_to_status - convert a regulator mode into a status
2119  *
2120  * @mode: Mode to convert
2121  *
2122  * Convert a regulator mode into a status.
2123  */
2124 int regulator_mode_to_status(unsigned int mode)
2125 {
2126         switch (mode) {
2127         case REGULATOR_MODE_FAST:
2128                 return REGULATOR_STATUS_FAST;
2129         case REGULATOR_MODE_NORMAL:
2130                 return REGULATOR_STATUS_NORMAL;
2131         case REGULATOR_MODE_IDLE:
2132                 return REGULATOR_STATUS_IDLE;
2133         case REGULATOR_STATUS_STANDBY:
2134                 return REGULATOR_STATUS_STANDBY;
2135         default:
2136                 return 0;
2137         }
2138 }
2139 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2140
2141 /*
2142  * To avoid cluttering sysfs (and memory) with useless state, only
2143  * create attributes that can be meaningfully displayed.
2144  */
2145 static int add_regulator_attributes(struct regulator_dev *rdev)
2146 {
2147         struct device           *dev = &rdev->dev;
2148         struct regulator_ops    *ops = rdev->desc->ops;
2149         int                     status = 0;
2150
2151         /* some attributes need specific methods to be displayed */
2152         if (ops->get_voltage) {
2153                 status = device_create_file(dev, &dev_attr_microvolts);
2154                 if (status < 0)
2155                         return status;
2156         }
2157         if (ops->get_current_limit) {
2158                 status = device_create_file(dev, &dev_attr_microamps);
2159                 if (status < 0)
2160                         return status;
2161         }
2162         if (ops->get_mode) {
2163                 status = device_create_file(dev, &dev_attr_opmode);
2164                 if (status < 0)
2165                         return status;
2166         }
2167         if (ops->is_enabled) {
2168                 status = device_create_file(dev, &dev_attr_state);
2169                 if (status < 0)
2170                         return status;
2171         }
2172         if (ops->get_status) {
2173                 status = device_create_file(dev, &dev_attr_status);
2174                 if (status < 0)
2175                         return status;
2176         }
2177
2178         /* some attributes are type-specific */
2179         if (rdev->desc->type == REGULATOR_CURRENT) {
2180                 status = device_create_file(dev, &dev_attr_requested_microamps);
2181                 if (status < 0)
2182                         return status;
2183         }
2184
2185         /* all the other attributes exist to support constraints;
2186          * don't show them if there are no constraints, or if the
2187          * relevant supporting methods are missing.
2188          */
2189         if (!rdev->constraints)
2190                 return status;
2191
2192         /* constraints need specific supporting methods */
2193         if (ops->set_voltage) {
2194                 status = device_create_file(dev, &dev_attr_min_microvolts);
2195                 if (status < 0)
2196                         return status;
2197                 status = device_create_file(dev, &dev_attr_max_microvolts);
2198                 if (status < 0)
2199                         return status;
2200         }
2201         if (ops->set_current_limit) {
2202                 status = device_create_file(dev, &dev_attr_min_microamps);
2203                 if (status < 0)
2204                         return status;
2205                 status = device_create_file(dev, &dev_attr_max_microamps);
2206                 if (status < 0)
2207                         return status;
2208         }
2209
2210         /* suspend mode constraints need multiple supporting methods */
2211         if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2212                 return status;
2213
2214         status = device_create_file(dev, &dev_attr_suspend_standby_state);
2215         if (status < 0)
2216                 return status;
2217         status = device_create_file(dev, &dev_attr_suspend_mem_state);
2218         if (status < 0)
2219                 return status;
2220         status = device_create_file(dev, &dev_attr_suspend_disk_state);
2221         if (status < 0)
2222                 return status;
2223
2224         if (ops->set_suspend_voltage) {
2225                 status = device_create_file(dev,
2226                                 &dev_attr_suspend_standby_microvolts);
2227                 if (status < 0)
2228                         return status;
2229                 status = device_create_file(dev,
2230                                 &dev_attr_suspend_mem_microvolts);
2231                 if (status < 0)
2232                         return status;
2233                 status = device_create_file(dev,
2234                                 &dev_attr_suspend_disk_microvolts);
2235                 if (status < 0)
2236                         return status;
2237         }
2238
2239         if (ops->set_suspend_mode) {
2240                 status = device_create_file(dev,
2241                                 &dev_attr_suspend_standby_mode);
2242                 if (status < 0)
2243                         return status;
2244                 status = device_create_file(dev,
2245                                 &dev_attr_suspend_mem_mode);
2246                 if (status < 0)
2247                         return status;
2248                 status = device_create_file(dev,
2249                                 &dev_attr_suspend_disk_mode);
2250                 if (status < 0)
2251                         return status;
2252         }
2253
2254         return status;
2255 }
2256
2257 /**
2258  * regulator_register - register regulator
2259  * @regulator_desc: regulator to register
2260  * @dev: struct device for the regulator
2261  * @init_data: platform provided init data, passed through by driver
2262  * @driver_data: private regulator data
2263  *
2264  * Called by regulator drivers to register a regulator.
2265  * Returns 0 on success.
2266  */
2267 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2268         struct device *dev, struct regulator_init_data *init_data,
2269         void *driver_data)
2270 {
2271         static atomic_t regulator_no = ATOMIC_INIT(0);
2272         struct regulator_dev *rdev;
2273         int ret, i;
2274
2275         if (regulator_desc == NULL)
2276                 return ERR_PTR(-EINVAL);
2277
2278         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2279                 return ERR_PTR(-EINVAL);
2280
2281         if (regulator_desc->type != REGULATOR_VOLTAGE &&
2282             regulator_desc->type != REGULATOR_CURRENT)
2283                 return ERR_PTR(-EINVAL);
2284
2285         if (!init_data)
2286                 return ERR_PTR(-EINVAL);
2287
2288         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2289         if (rdev == NULL)
2290                 return ERR_PTR(-ENOMEM);
2291
2292         mutex_lock(&regulator_list_mutex);
2293
2294         mutex_init(&rdev->mutex);
2295         rdev->reg_data = driver_data;
2296         rdev->owner = regulator_desc->owner;
2297         rdev->desc = regulator_desc;
2298         INIT_LIST_HEAD(&rdev->consumer_list);
2299         INIT_LIST_HEAD(&rdev->supply_list);
2300         INIT_LIST_HEAD(&rdev->list);
2301         INIT_LIST_HEAD(&rdev->slist);
2302         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2303
2304         /* preform any regulator specific init */
2305         if (init_data->regulator_init) {
2306                 ret = init_data->regulator_init(rdev->reg_data);
2307                 if (ret < 0)
2308                         goto clean;
2309         }
2310
2311         /* register with sysfs */
2312         rdev->dev.class = &regulator_class;
2313         rdev->dev.parent = dev;
2314         dev_set_name(&rdev->dev, "regulator.%d",
2315                      atomic_inc_return(&regulator_no) - 1);
2316         ret = device_register(&rdev->dev);
2317         if (ret != 0) {
2318                 put_device(&rdev->dev);
2319                 goto clean;
2320         }
2321
2322         dev_set_drvdata(&rdev->dev, rdev);
2323
2324         /* set regulator constraints */
2325         ret = set_machine_constraints(rdev, &init_data->constraints);
2326         if (ret < 0)
2327                 goto scrub;
2328
2329         /* add attributes supported by this regulator */
2330         ret = add_regulator_attributes(rdev);
2331         if (ret < 0)
2332                 goto scrub;
2333
2334         /* set supply regulator if it exists */
2335         if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2336                 dev_err(dev,
2337                         "Supply regulator specified by both name and dev\n");
2338                 goto scrub;
2339         }
2340
2341         if (init_data->supply_regulator) {
2342                 struct regulator_dev *r;
2343                 int found = 0;
2344
2345                 list_for_each_entry(r, &regulator_list, list) {
2346                         if (strcmp(rdev_get_name(r),
2347                                    init_data->supply_regulator) == 0) {
2348                                 found = 1;
2349                                 break;
2350                         }
2351                 }
2352
2353                 if (!found) {
2354                         dev_err(dev, "Failed to find supply %s\n",
2355                                 init_data->supply_regulator);
2356                         goto scrub;
2357                 }
2358
2359                 ret = set_supply(rdev, r);
2360                 if (ret < 0)
2361                         goto scrub;
2362         }
2363
2364         if (init_data->supply_regulator_dev) {
2365                 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
2366                 ret = set_supply(rdev,
2367                         dev_get_drvdata(init_data->supply_regulator_dev));
2368                 if (ret < 0)
2369                         goto scrub;
2370         }
2371
2372         /* add consumers devices */
2373         for (i = 0; i < init_data->num_consumer_supplies; i++) {
2374                 ret = set_consumer_device_supply(rdev,
2375                         init_data->consumer_supplies[i].dev,
2376                         init_data->consumer_supplies[i].dev_name,
2377                         init_data->consumer_supplies[i].supply);
2378                 if (ret < 0)
2379                         goto unset_supplies;
2380         }
2381
2382         list_add(&rdev->list, &regulator_list);
2383 out:
2384         mutex_unlock(&regulator_list_mutex);
2385         return rdev;
2386
2387 unset_supplies:
2388         unset_regulator_supplies(rdev);
2389
2390 scrub:
2391         device_unregister(&rdev->dev);
2392         /* device core frees rdev */
2393         rdev = ERR_PTR(ret);
2394         goto out;
2395
2396 clean:
2397         kfree(rdev);
2398         rdev = ERR_PTR(ret);
2399         goto out;
2400 }
2401 EXPORT_SYMBOL_GPL(regulator_register);
2402
2403 /**
2404  * regulator_unregister - unregister regulator
2405  * @rdev: regulator to unregister
2406  *
2407  * Called by regulator drivers to unregister a regulator.
2408  */
2409 void regulator_unregister(struct regulator_dev *rdev)
2410 {
2411         if (rdev == NULL)
2412                 return;
2413
2414         mutex_lock(&regulator_list_mutex);
2415         WARN_ON(rdev->open_count);
2416         unset_regulator_supplies(rdev);
2417         list_del(&rdev->list);
2418         if (rdev->supply)
2419                 sysfs_remove_link(&rdev->dev.kobj, "supply");
2420         device_unregister(&rdev->dev);
2421         mutex_unlock(&regulator_list_mutex);
2422 }
2423 EXPORT_SYMBOL_GPL(regulator_unregister);
2424
2425 static suspend_state_t last_suspend_attempt;
2426 /**
2427  * regulator_suspend_prepare - prepare regulators for system wide suspend
2428  * @state: system suspend state
2429  *
2430  * Configure each regulator with it's suspend operating parameters for state.
2431  * This will usually be called by machine suspend code prior to supending.
2432  */
2433 int regulator_suspend_prepare(suspend_state_t state)
2434 {
2435         struct regulator_dev *rdev;
2436         int ret = 0;
2437
2438         last_suspend_attempt = state;
2439
2440         /* ON is handled by regulator active state */
2441         if (state == PM_SUSPEND_ON)
2442                 return -EINVAL;
2443
2444         mutex_lock(&regulator_list_mutex);
2445         list_for_each_entry(rdev, &regulator_list, list) {
2446
2447                 mutex_lock(&rdev->mutex);
2448                 ret = suspend_prepare(rdev, state);
2449                 mutex_unlock(&rdev->mutex);
2450
2451                 if (ret < 0) {
2452                         printk(KERN_ERR "%s: failed to prepare %s\n",
2453                                 __func__, rdev_get_name(rdev));
2454                         goto out;
2455                 }
2456         }
2457 out:
2458         mutex_unlock(&regulator_list_mutex);
2459         return ret;
2460 }
2461 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2462
2463 /**
2464  * regulator_suspend_finish - resume regulators from system wide suspend
2465  *
2466  * Turn on regulators that might be turned off by regulator_suspend_prepare
2467  * and that should be turned on according to the regulators properties.
2468  */
2469 int regulator_suspend_finish(void)
2470 {
2471         struct regulator_dev *rdev;
2472         int ret = 0, error;
2473
2474         mutex_lock(&regulator_list_mutex);
2475         list_for_each_entry(rdev, &regulator_list, list) {
2476                 struct regulator_ops *ops = rdev->desc->ops;
2477                 struct regulator_state *rstate;
2478
2479                 mutex_lock(&rdev->mutex);
2480                 if (rdev->use_count > 0 ||
2481                     (rdev->constraints && rdev->constraints->always_on)) {
2482
2483                         switch (last_suspend_attempt) {
2484                         case PM_SUSPEND_STANDBY:
2485                                 rstate = &rdev->constraints->state_standby;
2486                                 break;
2487                         case PM_SUSPEND_MEM:
2488                                 rstate = &rdev->constraints->state_mem;
2489                                 break;
2490                         case PM_SUSPEND_MAX:
2491                                 rstate = &rdev->constraints->state_disk;
2492                                 break;
2493                         default:
2494                                 rstate = NULL;
2495                         }
2496
2497                         if (ops->enable && rdev->constraints && rstate &&
2498                             rstate->disabled && ops->set_suspend_disable &&
2499                             ops->set_suspend_enable) {
2500                                 error = ops->enable(rdev);
2501                                 if (error)
2502                                         ret = error;
2503                         }
2504                 } else {
2505                         if (!has_full_constraints)
2506                                 goto unlock;
2507                         if (!ops->disable)
2508                                 goto unlock;
2509                         if (ops->is_enabled && !ops->is_enabled(rdev))
2510                                 goto unlock;
2511
2512                         error = ops->disable(rdev);
2513                         if (error)
2514                                 ret = error;
2515                 }
2516 unlock:
2517                 mutex_unlock(&rdev->mutex);
2518         }
2519         mutex_unlock(&regulator_list_mutex);
2520         return ret;
2521 }
2522 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2523
2524 /**
2525  * regulator_has_full_constraints - the system has fully specified constraints
2526  *
2527  * Calling this function will cause the regulator API to disable all
2528  * regulators which have a zero use count and don't have an always_on
2529  * constraint in a late_initcall.
2530  *
2531  * The intention is that this will become the default behaviour in a
2532  * future kernel release so users are encouraged to use this facility
2533  * now.
2534  */
2535 void regulator_has_full_constraints(void)
2536 {
2537         has_full_constraints = 1;
2538 }
2539 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2540
2541 /**
2542  * rdev_get_drvdata - get rdev regulator driver data
2543  * @rdev: regulator
2544  *
2545  * Get rdev regulator driver private data. This call can be used in the
2546  * regulator driver context.
2547  */
2548 void *rdev_get_drvdata(struct regulator_dev *rdev)
2549 {
2550         return rdev->reg_data;
2551 }
2552 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2553
2554 /**
2555  * regulator_get_drvdata - get regulator driver data
2556  * @regulator: regulator
2557  *
2558  * Get regulator driver private data. This call can be used in the consumer
2559  * driver context when non API regulator specific functions need to be called.
2560  */
2561 void *regulator_get_drvdata(struct regulator *regulator)
2562 {
2563         return regulator->rdev->reg_data;
2564 }
2565 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2566
2567 /**
2568  * regulator_set_drvdata - set regulator driver data
2569  * @regulator: regulator
2570  * @data: data
2571  */
2572 void regulator_set_drvdata(struct regulator *regulator, void *data)
2573 {
2574         regulator->rdev->reg_data = data;
2575 }
2576 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2577
2578 /**
2579  * regulator_get_id - get regulator ID
2580  * @rdev: regulator
2581  */
2582 int rdev_get_id(struct regulator_dev *rdev)
2583 {
2584         return rdev->desc->id;
2585 }
2586 EXPORT_SYMBOL_GPL(rdev_get_id);
2587
2588 struct device *rdev_get_dev(struct regulator_dev *rdev)
2589 {
2590         return &rdev->dev;
2591 }
2592 EXPORT_SYMBOL_GPL(rdev_get_dev);
2593
2594 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2595 {
2596         return reg_init_data->driver_data;
2597 }
2598 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2599
2600 static int __init regulator_init(void)
2601 {
2602         int ret;
2603
2604         printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2605
2606         ret = class_register(&regulator_class);
2607
2608         regulator_dummy_init();
2609
2610         return ret;
2611 }
2612
2613 /* init early to allow our consumers to complete system booting */
2614 core_initcall(regulator_init);
2615
2616 #ifdef CONFIG_REGULATOR_RESUME
2617 int regulator_init_complete(void)
2618 #else
2619 static int __init regulator_init_complete(void)
2620 #endif
2621 {
2622         struct regulator_dev *rdev;
2623         struct regulator_ops *ops;
2624         struct regulation_constraints *c;
2625         int enabled, ret;
2626         const char *name;
2627
2628         mutex_lock(&regulator_list_mutex);
2629
2630         /* If we have a full configuration then disable any regulators
2631          * which are not in use or always_on.  This will become the
2632          * default behaviour in the future.
2633          */
2634         list_for_each_entry(rdev, &regulator_list, list) {
2635                 ops = rdev->desc->ops;
2636                 c = rdev->constraints;
2637
2638                 name = rdev_get_name(rdev);
2639
2640                 if (!ops->disable || (c && c->always_on))
2641                         continue;
2642
2643                 mutex_lock(&rdev->mutex);
2644
2645                 if (rdev->use_count)
2646                         goto unlock;
2647
2648                 /* If we can't read the status assume it's on. */
2649                 if (ops->is_enabled)
2650                         enabled = ops->is_enabled(rdev);
2651                 else
2652                         enabled = 1;
2653
2654                 if (!enabled)
2655                         goto unlock;
2656
2657                 if (has_full_constraints) {
2658                         /* We log since this may kill the system if it
2659                          * goes wrong. */
2660                         printk(KERN_INFO "%s: disabling %s\n",
2661                                __func__, name);
2662                         ret = ops->disable(rdev);
2663                         if (ret != 0) {
2664                                 printk(KERN_ERR
2665                                        "%s: couldn't disable %s: %d\n",
2666                                        __func__, name, ret);
2667                         }
2668                 } else {
2669                         /* The intention is that in future we will
2670                          * assume that full constraints are provided
2671                          * so warn even if we aren't going to do
2672                          * anything here.
2673                          */
2674                         printk(KERN_WARNING
2675                                "%s: incomplete constraints, leaving %s on\n",
2676                                __func__, name);
2677                 }
2678
2679 unlock:
2680                 mutex_unlock(&rdev->mutex);
2681         }
2682
2683         mutex_unlock(&regulator_list_mutex);
2684
2685         return 0;
2686 }
2687 late_initcall(regulator_init_complete);
2688
2689 #ifdef CONFIG_DEBUG_FS
2690 /*
2691  *      debugfs support to trace regulator tree hierachy and attributes
2692  */
2693 static struct dentry *regulator_debugfs_root;
2694
2695 static int regulator_debugfs_register_one(struct regulator_dev *rdev)
2696 {
2697         int err;
2698         struct dentry *d, *child, *child_tmp;
2699         struct regulator_dev *pa = rdev->supply;
2700         char s[255];
2701         char *p = s;
2702
2703         p += sprintf(p, "%s", rdev->desc->name);
2704         d = debugfs_create_dir(s, pa ? pa->dent : regulator_debugfs_root);
2705         if (!d)
2706                 return -ENOMEM;
2707         rdev->dent = d;
2708
2709         d = debugfs_create_u32("use_count", S_IRUGO, rdev->dent, (u32 *)&rdev->use_count);
2710         if (!d) {
2711                 err = -ENOMEM;
2712                 goto err_out;
2713         }
2714         d = debugfs_create_u32("open_count", S_IRUGO, rdev->dent, (u32 *)&rdev->open_count);
2715         if (!d) {
2716                 err = -ENOMEM;
2717                 goto err_out;
2718         }
2719         return 0;
2720
2721 err_out:
2722         d = rdev->dent;
2723         list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
2724                 debugfs_remove(child);
2725         debugfs_remove(rdev->dent);
2726         return err;
2727 }
2728
2729 static int regulator_debugfs_register(struct regulator_dev *rdev)
2730 {
2731         int err;
2732         struct regulator_dev *pa = rdev->supply;
2733
2734         if (pa && !pa->dent) {
2735                 err = regulator_debugfs_register(pa);
2736                 if (err) {
2737                         pr_err("ERROR registering parent %s(%d)\n",
2738                                         pa->desc->name, err);
2739                         return err;
2740                 }
2741         }
2742
2743         if (!rdev->dent) {
2744                 err = regulator_debugfs_register_one(rdev);
2745                 if (err) {
2746                         pr_err("ERROR registering %s(%d)\n",
2747                                         rdev->desc->name, err);
2748                         return err;
2749                 }
2750         }
2751         return 0;
2752 }
2753
2754 static int __init regulator_debugfs_init(void)
2755 {
2756         struct regulator_dev *rdev;
2757         struct dentry *d;
2758         int err;
2759
2760         d = debugfs_create_dir("regulator", NULL);
2761         if (!d)
2762                 return -ENOMEM;
2763         regulator_debugfs_root = d;
2764
2765         list_for_each_entry(rdev, &regulator_list, list) {
2766                 err = regulator_debugfs_register(rdev);
2767                 if (err)
2768                         goto err_out;
2769         }
2770         return 0;
2771 err_out:
2772         debugfs_remove_recursive(regulator_debugfs_root);
2773         return err;
2774 }
2775 late_initcall_sync(regulator_debugfs_init);
2776
2777 #endif /* CONFIG_DEBUG_FS */