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