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