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