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