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