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