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