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