85f61e5dc3127a1dca655acb56eeae058df4189f
[sfrench/cifs-2.6.git] / drivers / regulator / core.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * core.c  --  Voltage/Current Regulator framework.
4  *
5  * Copyright 2007, 2008 Wolfson Microelectronics PLC.
6  * Copyright 2008 SlimLogic Ltd.
7  *
8  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/debugfs.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/async.h>
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include <linux/suspend.h>
20 #include <linux/delay.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/of.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/module.h>
29
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
32
33 #include "dummy.h"
34 #include "internal.h"
35
36 #define rdev_crit(rdev, fmt, ...)                                       \
37         pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_err(rdev, fmt, ...)                                        \
39         pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_warn(rdev, fmt, ...)                                       \
41         pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_info(rdev, fmt, ...)                                       \
43         pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_dbg(rdev, fmt, ...)                                        \
45         pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46
47 static DEFINE_WW_CLASS(regulator_ww_class);
48 static DEFINE_MUTEX(regulator_nesting_mutex);
49 static DEFINE_MUTEX(regulator_list_mutex);
50 static LIST_HEAD(regulator_map_list);
51 static LIST_HEAD(regulator_ena_gpio_list);
52 static LIST_HEAD(regulator_supply_alias_list);
53 static bool has_full_constraints;
54
55 static struct dentry *debugfs_root;
56
57 /*
58  * struct regulator_map
59  *
60  * Used to provide symbolic supply names to devices.
61  */
62 struct regulator_map {
63         struct list_head list;
64         const char *dev_name;   /* The dev_name() for the consumer */
65         const char *supply;
66         struct regulator_dev *regulator;
67 };
68
69 /*
70  * struct regulator_enable_gpio
71  *
72  * Management for shared enable GPIO pin
73  */
74 struct regulator_enable_gpio {
75         struct list_head list;
76         struct gpio_desc *gpiod;
77         u32 enable_count;       /* a number of enabled shared GPIO */
78         u32 request_count;      /* a number of requested shared GPIO */
79 };
80
81 /*
82  * struct regulator_supply_alias
83  *
84  * Used to map lookups for a supply onto an alternative device.
85  */
86 struct regulator_supply_alias {
87         struct list_head list;
88         struct device *src_dev;
89         const char *src_supply;
90         struct device *alias_dev;
91         const char *alias_supply;
92 };
93
94 static int _regulator_is_enabled(struct regulator_dev *rdev);
95 static int _regulator_disable(struct regulator *regulator);
96 static int _regulator_get_voltage(struct regulator_dev *rdev);
97 static int _regulator_get_current_limit(struct regulator_dev *rdev);
98 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
99 static int _notifier_call_chain(struct regulator_dev *rdev,
100                                   unsigned long event, void *data);
101 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
102                                      int min_uV, int max_uV);
103 static int regulator_balance_voltage(struct regulator_dev *rdev,
104                                      suspend_state_t state);
105 static int regulator_set_voltage_rdev(struct regulator_dev *rdev,
106                                       int min_uV, int max_uV,
107                                       suspend_state_t state);
108 static struct regulator *create_regulator(struct regulator_dev *rdev,
109                                           struct device *dev,
110                                           const char *supply_name);
111 static void _regulator_put(struct regulator *regulator);
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 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
129 {
130         if (!rdev->constraints) {
131                 rdev_err(rdev, "no constraints\n");
132                 return false;
133         }
134
135         if (rdev->constraints->valid_ops_mask & ops)
136                 return true;
137
138         return false;
139 }
140
141 /**
142  * regulator_lock_nested - lock a single regulator
143  * @rdev:               regulator source
144  * @ww_ctx:             w/w mutex acquire context
145  *
146  * This function can be called many times by one task on
147  * a single regulator and its mutex will be locked only
148  * once. If a task, which is calling this function is other
149  * than the one, which initially locked the mutex, it will
150  * wait on mutex.
151  */
152 static inline int regulator_lock_nested(struct regulator_dev *rdev,
153                                         struct ww_acquire_ctx *ww_ctx)
154 {
155         bool lock = false;
156         int ret = 0;
157
158         mutex_lock(&regulator_nesting_mutex);
159
160         if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
161                 if (rdev->mutex_owner == current)
162                         rdev->ref_cnt++;
163                 else
164                         lock = true;
165
166                 if (lock) {
167                         mutex_unlock(&regulator_nesting_mutex);
168                         ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
169                         mutex_lock(&regulator_nesting_mutex);
170                 }
171         } else {
172                 lock = true;
173         }
174
175         if (lock && ret != -EDEADLK) {
176                 rdev->ref_cnt++;
177                 rdev->mutex_owner = current;
178         }
179
180         mutex_unlock(&regulator_nesting_mutex);
181
182         return ret;
183 }
184
185 /**
186  * regulator_lock - lock a single regulator
187  * @rdev:               regulator source
188  *
189  * This function can be called many times by one task on
190  * a single regulator and its mutex will be locked only
191  * once. If a task, which is calling this function is other
192  * than the one, which initially locked the mutex, it will
193  * wait on mutex.
194  */
195 void regulator_lock(struct regulator_dev *rdev)
196 {
197         regulator_lock_nested(rdev, NULL);
198 }
199 EXPORT_SYMBOL_GPL(regulator_lock);
200
201 /**
202  * regulator_unlock - unlock a single regulator
203  * @rdev:               regulator_source
204  *
205  * This function unlocks the mutex when the
206  * reference counter reaches 0.
207  */
208 void regulator_unlock(struct regulator_dev *rdev)
209 {
210         mutex_lock(&regulator_nesting_mutex);
211
212         if (--rdev->ref_cnt == 0) {
213                 rdev->mutex_owner = NULL;
214                 ww_mutex_unlock(&rdev->mutex);
215         }
216
217         WARN_ON_ONCE(rdev->ref_cnt < 0);
218
219         mutex_unlock(&regulator_nesting_mutex);
220 }
221 EXPORT_SYMBOL_GPL(regulator_unlock);
222
223 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
224 {
225         struct regulator_dev *c_rdev;
226         int i;
227
228         for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
229                 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
230
231                 if (rdev->supply->rdev == c_rdev)
232                         return true;
233         }
234
235         return false;
236 }
237
238 static void regulator_unlock_recursive(struct regulator_dev *rdev,
239                                        unsigned int n_coupled)
240 {
241         struct regulator_dev *c_rdev;
242         int i;
243
244         for (i = n_coupled; i > 0; i--) {
245                 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
246
247                 if (!c_rdev)
248                         continue;
249
250                 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
251                         regulator_unlock_recursive(
252                                         c_rdev->supply->rdev,
253                                         c_rdev->coupling_desc.n_coupled);
254
255                 regulator_unlock(c_rdev);
256         }
257 }
258
259 static int regulator_lock_recursive(struct regulator_dev *rdev,
260                                     struct regulator_dev **new_contended_rdev,
261                                     struct regulator_dev **old_contended_rdev,
262                                     struct ww_acquire_ctx *ww_ctx)
263 {
264         struct regulator_dev *c_rdev;
265         int i, err;
266
267         for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
268                 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
269
270                 if (!c_rdev)
271                         continue;
272
273                 if (c_rdev != *old_contended_rdev) {
274                         err = regulator_lock_nested(c_rdev, ww_ctx);
275                         if (err) {
276                                 if (err == -EDEADLK) {
277                                         *new_contended_rdev = c_rdev;
278                                         goto err_unlock;
279                                 }
280
281                                 /* shouldn't happen */
282                                 WARN_ON_ONCE(err != -EALREADY);
283                         }
284                 } else {
285                         *old_contended_rdev = NULL;
286                 }
287
288                 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
289                         err = regulator_lock_recursive(c_rdev->supply->rdev,
290                                                        new_contended_rdev,
291                                                        old_contended_rdev,
292                                                        ww_ctx);
293                         if (err) {
294                                 regulator_unlock(c_rdev);
295                                 goto err_unlock;
296                         }
297                 }
298         }
299
300         return 0;
301
302 err_unlock:
303         regulator_unlock_recursive(rdev, i);
304
305         return err;
306 }
307
308 /**
309  * regulator_unlock_dependent - unlock regulator's suppliers and coupled
310  *                              regulators
311  * @rdev:                       regulator source
312  * @ww_ctx:                     w/w mutex acquire context
313  *
314  * Unlock all regulators related with rdev by coupling or supplying.
315  */
316 static void regulator_unlock_dependent(struct regulator_dev *rdev,
317                                        struct ww_acquire_ctx *ww_ctx)
318 {
319         regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
320         ww_acquire_fini(ww_ctx);
321 }
322
323 /**
324  * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
325  * @rdev:                       regulator source
326  * @ww_ctx:                     w/w mutex acquire context
327  *
328  * This function as a wrapper on regulator_lock_recursive(), which locks
329  * all regulators related with rdev by coupling or supplying.
330  */
331 static void regulator_lock_dependent(struct regulator_dev *rdev,
332                                      struct ww_acquire_ctx *ww_ctx)
333 {
334         struct regulator_dev *new_contended_rdev = NULL;
335         struct regulator_dev *old_contended_rdev = NULL;
336         int err;
337
338         mutex_lock(&regulator_list_mutex);
339
340         ww_acquire_init(ww_ctx, &regulator_ww_class);
341
342         do {
343                 if (new_contended_rdev) {
344                         ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
345                         old_contended_rdev = new_contended_rdev;
346                         old_contended_rdev->ref_cnt++;
347                 }
348
349                 err = regulator_lock_recursive(rdev,
350                                                &new_contended_rdev,
351                                                &old_contended_rdev,
352                                                ww_ctx);
353
354                 if (old_contended_rdev)
355                         regulator_unlock(old_contended_rdev);
356
357         } while (err == -EDEADLK);
358
359         ww_acquire_done(ww_ctx);
360
361         mutex_unlock(&regulator_list_mutex);
362 }
363
364 /**
365  * of_get_child_regulator - get a child regulator device node
366  * based on supply name
367  * @parent: Parent device node
368  * @prop_name: Combination regulator supply name and "-supply"
369  *
370  * Traverse all child nodes.
371  * Extract the child regulator device node corresponding to the supply name.
372  * returns the device node corresponding to the regulator if found, else
373  * returns NULL.
374  */
375 static struct device_node *of_get_child_regulator(struct device_node *parent,
376                                                   const char *prop_name)
377 {
378         struct device_node *regnode = NULL;
379         struct device_node *child = NULL;
380
381         for_each_child_of_node(parent, child) {
382                 regnode = of_parse_phandle(child, prop_name, 0);
383
384                 if (!regnode) {
385                         regnode = of_get_child_regulator(child, prop_name);
386                         if (regnode)
387                                 return regnode;
388                 } else {
389                         return regnode;
390                 }
391         }
392         return NULL;
393 }
394
395 /**
396  * of_get_regulator - get a regulator device node based on supply name
397  * @dev: Device pointer for the consumer (of regulator) device
398  * @supply: regulator supply name
399  *
400  * Extract the regulator device node corresponding to the supply name.
401  * returns the device node corresponding to the regulator if found, else
402  * returns NULL.
403  */
404 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
405 {
406         struct device_node *regnode = NULL;
407         char prop_name[32]; /* 32 is max size of property name */
408
409         dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
410
411         snprintf(prop_name, 32, "%s-supply", supply);
412         regnode = of_parse_phandle(dev->of_node, prop_name, 0);
413
414         if (!regnode) {
415                 regnode = of_get_child_regulator(dev->of_node, prop_name);
416                 if (regnode)
417                         return regnode;
418
419                 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
420                                 prop_name, dev->of_node);
421                 return NULL;
422         }
423         return regnode;
424 }
425
426 /* Platform voltage constraint check */
427 static int regulator_check_voltage(struct regulator_dev *rdev,
428                                    int *min_uV, int *max_uV)
429 {
430         BUG_ON(*min_uV > *max_uV);
431
432         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
433                 rdev_err(rdev, "voltage operation not allowed\n");
434                 return -EPERM;
435         }
436
437         if (*max_uV > rdev->constraints->max_uV)
438                 *max_uV = rdev->constraints->max_uV;
439         if (*min_uV < rdev->constraints->min_uV)
440                 *min_uV = rdev->constraints->min_uV;
441
442         if (*min_uV > *max_uV) {
443                 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
444                          *min_uV, *max_uV);
445                 return -EINVAL;
446         }
447
448         return 0;
449 }
450
451 /* return 0 if the state is valid */
452 static int regulator_check_states(suspend_state_t state)
453 {
454         return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
455 }
456
457 /* Make sure we select a voltage that suits the needs of all
458  * regulator consumers
459  */
460 static int regulator_check_consumers(struct regulator_dev *rdev,
461                                      int *min_uV, int *max_uV,
462                                      suspend_state_t state)
463 {
464         struct regulator *regulator;
465         struct regulator_voltage *voltage;
466
467         list_for_each_entry(regulator, &rdev->consumer_list, list) {
468                 voltage = &regulator->voltage[state];
469                 /*
470                  * Assume consumers that didn't say anything are OK
471                  * with anything in the constraint range.
472                  */
473                 if (!voltage->min_uV && !voltage->max_uV)
474                         continue;
475
476                 if (*max_uV > voltage->max_uV)
477                         *max_uV = voltage->max_uV;
478                 if (*min_uV < voltage->min_uV)
479                         *min_uV = voltage->min_uV;
480         }
481
482         if (*min_uV > *max_uV) {
483                 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
484                         *min_uV, *max_uV);
485                 return -EINVAL;
486         }
487
488         return 0;
489 }
490
491 /* current constraint check */
492 static int regulator_check_current_limit(struct regulator_dev *rdev,
493                                         int *min_uA, int *max_uA)
494 {
495         BUG_ON(*min_uA > *max_uA);
496
497         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
498                 rdev_err(rdev, "current operation not allowed\n");
499                 return -EPERM;
500         }
501
502         if (*max_uA > rdev->constraints->max_uA)
503                 *max_uA = rdev->constraints->max_uA;
504         if (*min_uA < rdev->constraints->min_uA)
505                 *min_uA = rdev->constraints->min_uA;
506
507         if (*min_uA > *max_uA) {
508                 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
509                          *min_uA, *max_uA);
510                 return -EINVAL;
511         }
512
513         return 0;
514 }
515
516 /* operating mode constraint check */
517 static int regulator_mode_constrain(struct regulator_dev *rdev,
518                                     unsigned int *mode)
519 {
520         switch (*mode) {
521         case REGULATOR_MODE_FAST:
522         case REGULATOR_MODE_NORMAL:
523         case REGULATOR_MODE_IDLE:
524         case REGULATOR_MODE_STANDBY:
525                 break;
526         default:
527                 rdev_err(rdev, "invalid mode %x specified\n", *mode);
528                 return -EINVAL;
529         }
530
531         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
532                 rdev_err(rdev, "mode operation not allowed\n");
533                 return -EPERM;
534         }
535
536         /* The modes are bitmasks, the most power hungry modes having
537          * the lowest values. If the requested mode isn't supported
538          * try higher modes. */
539         while (*mode) {
540                 if (rdev->constraints->valid_modes_mask & *mode)
541                         return 0;
542                 *mode /= 2;
543         }
544
545         return -EINVAL;
546 }
547
548 static inline struct regulator_state *
549 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
550 {
551         if (rdev->constraints == NULL)
552                 return NULL;
553
554         switch (state) {
555         case PM_SUSPEND_STANDBY:
556                 return &rdev->constraints->state_standby;
557         case PM_SUSPEND_MEM:
558                 return &rdev->constraints->state_mem;
559         case PM_SUSPEND_MAX:
560                 return &rdev->constraints->state_disk;
561         default:
562                 return NULL;
563         }
564 }
565
566 static ssize_t regulator_uV_show(struct device *dev,
567                                 struct device_attribute *attr, char *buf)
568 {
569         struct regulator_dev *rdev = dev_get_drvdata(dev);
570         ssize_t ret;
571
572         regulator_lock(rdev);
573         ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
574         regulator_unlock(rdev);
575
576         return ret;
577 }
578 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
579
580 static ssize_t regulator_uA_show(struct device *dev,
581                                 struct device_attribute *attr, char *buf)
582 {
583         struct regulator_dev *rdev = dev_get_drvdata(dev);
584
585         return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
586 }
587 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
588
589 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
590                          char *buf)
591 {
592         struct regulator_dev *rdev = dev_get_drvdata(dev);
593
594         return sprintf(buf, "%s\n", rdev_get_name(rdev));
595 }
596 static DEVICE_ATTR_RO(name);
597
598 static const char *regulator_opmode_to_str(int mode)
599 {
600         switch (mode) {
601         case REGULATOR_MODE_FAST:
602                 return "fast";
603         case REGULATOR_MODE_NORMAL:
604                 return "normal";
605         case REGULATOR_MODE_IDLE:
606                 return "idle";
607         case REGULATOR_MODE_STANDBY:
608                 return "standby";
609         }
610         return "unknown";
611 }
612
613 static ssize_t regulator_print_opmode(char *buf, int mode)
614 {
615         return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
616 }
617
618 static ssize_t regulator_opmode_show(struct device *dev,
619                                     struct device_attribute *attr, char *buf)
620 {
621         struct regulator_dev *rdev = dev_get_drvdata(dev);
622
623         return regulator_print_opmode(buf, _regulator_get_mode(rdev));
624 }
625 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
626
627 static ssize_t regulator_print_state(char *buf, int state)
628 {
629         if (state > 0)
630                 return sprintf(buf, "enabled\n");
631         else if (state == 0)
632                 return sprintf(buf, "disabled\n");
633         else
634                 return sprintf(buf, "unknown\n");
635 }
636
637 static ssize_t regulator_state_show(struct device *dev,
638                                    struct device_attribute *attr, char *buf)
639 {
640         struct regulator_dev *rdev = dev_get_drvdata(dev);
641         ssize_t ret;
642
643         regulator_lock(rdev);
644         ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
645         regulator_unlock(rdev);
646
647         return ret;
648 }
649 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
650
651 static ssize_t regulator_status_show(struct device *dev,
652                                    struct device_attribute *attr, char *buf)
653 {
654         struct regulator_dev *rdev = dev_get_drvdata(dev);
655         int status;
656         char *label;
657
658         status = rdev->desc->ops->get_status(rdev);
659         if (status < 0)
660                 return status;
661
662         switch (status) {
663         case REGULATOR_STATUS_OFF:
664                 label = "off";
665                 break;
666         case REGULATOR_STATUS_ON:
667                 label = "on";
668                 break;
669         case REGULATOR_STATUS_ERROR:
670                 label = "error";
671                 break;
672         case REGULATOR_STATUS_FAST:
673                 label = "fast";
674                 break;
675         case REGULATOR_STATUS_NORMAL:
676                 label = "normal";
677                 break;
678         case REGULATOR_STATUS_IDLE:
679                 label = "idle";
680                 break;
681         case REGULATOR_STATUS_STANDBY:
682                 label = "standby";
683                 break;
684         case REGULATOR_STATUS_BYPASS:
685                 label = "bypass";
686                 break;
687         case REGULATOR_STATUS_UNDEFINED:
688                 label = "undefined";
689                 break;
690         default:
691                 return -ERANGE;
692         }
693
694         return sprintf(buf, "%s\n", label);
695 }
696 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
697
698 static ssize_t regulator_min_uA_show(struct device *dev,
699                                     struct device_attribute *attr, char *buf)
700 {
701         struct regulator_dev *rdev = dev_get_drvdata(dev);
702
703         if (!rdev->constraints)
704                 return sprintf(buf, "constraint not defined\n");
705
706         return sprintf(buf, "%d\n", rdev->constraints->min_uA);
707 }
708 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
709
710 static ssize_t regulator_max_uA_show(struct device *dev,
711                                     struct device_attribute *attr, char *buf)
712 {
713         struct regulator_dev *rdev = dev_get_drvdata(dev);
714
715         if (!rdev->constraints)
716                 return sprintf(buf, "constraint not defined\n");
717
718         return sprintf(buf, "%d\n", rdev->constraints->max_uA);
719 }
720 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
721
722 static ssize_t regulator_min_uV_show(struct device *dev,
723                                     struct device_attribute *attr, char *buf)
724 {
725         struct regulator_dev *rdev = dev_get_drvdata(dev);
726
727         if (!rdev->constraints)
728                 return sprintf(buf, "constraint not defined\n");
729
730         return sprintf(buf, "%d\n", rdev->constraints->min_uV);
731 }
732 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
733
734 static ssize_t regulator_max_uV_show(struct device *dev,
735                                     struct device_attribute *attr, char *buf)
736 {
737         struct regulator_dev *rdev = dev_get_drvdata(dev);
738
739         if (!rdev->constraints)
740                 return sprintf(buf, "constraint not defined\n");
741
742         return sprintf(buf, "%d\n", rdev->constraints->max_uV);
743 }
744 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
745
746 static ssize_t regulator_total_uA_show(struct device *dev,
747                                       struct device_attribute *attr, char *buf)
748 {
749         struct regulator_dev *rdev = dev_get_drvdata(dev);
750         struct regulator *regulator;
751         int uA = 0;
752
753         regulator_lock(rdev);
754         list_for_each_entry(regulator, &rdev->consumer_list, list) {
755                 if (regulator->enable_count)
756                         uA += regulator->uA_load;
757         }
758         regulator_unlock(rdev);
759         return sprintf(buf, "%d\n", uA);
760 }
761 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
762
763 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
764                               char *buf)
765 {
766         struct regulator_dev *rdev = dev_get_drvdata(dev);
767         return sprintf(buf, "%d\n", rdev->use_count);
768 }
769 static DEVICE_ATTR_RO(num_users);
770
771 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
772                          char *buf)
773 {
774         struct regulator_dev *rdev = dev_get_drvdata(dev);
775
776         switch (rdev->desc->type) {
777         case REGULATOR_VOLTAGE:
778                 return sprintf(buf, "voltage\n");
779         case REGULATOR_CURRENT:
780                 return sprintf(buf, "current\n");
781         }
782         return sprintf(buf, "unknown\n");
783 }
784 static DEVICE_ATTR_RO(type);
785
786 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
787                                 struct device_attribute *attr, char *buf)
788 {
789         struct regulator_dev *rdev = dev_get_drvdata(dev);
790
791         return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
792 }
793 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
794                 regulator_suspend_mem_uV_show, NULL);
795
796 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
797                                 struct device_attribute *attr, char *buf)
798 {
799         struct regulator_dev *rdev = dev_get_drvdata(dev);
800
801         return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
802 }
803 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
804                 regulator_suspend_disk_uV_show, NULL);
805
806 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
807                                 struct device_attribute *attr, char *buf)
808 {
809         struct regulator_dev *rdev = dev_get_drvdata(dev);
810
811         return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
812 }
813 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
814                 regulator_suspend_standby_uV_show, NULL);
815
816 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
817                                 struct device_attribute *attr, char *buf)
818 {
819         struct regulator_dev *rdev = dev_get_drvdata(dev);
820
821         return regulator_print_opmode(buf,
822                 rdev->constraints->state_mem.mode);
823 }
824 static DEVICE_ATTR(suspend_mem_mode, 0444,
825                 regulator_suspend_mem_mode_show, NULL);
826
827 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
828                                 struct device_attribute *attr, char *buf)
829 {
830         struct regulator_dev *rdev = dev_get_drvdata(dev);
831
832         return regulator_print_opmode(buf,
833                 rdev->constraints->state_disk.mode);
834 }
835 static DEVICE_ATTR(suspend_disk_mode, 0444,
836                 regulator_suspend_disk_mode_show, NULL);
837
838 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
839                                 struct device_attribute *attr, char *buf)
840 {
841         struct regulator_dev *rdev = dev_get_drvdata(dev);
842
843         return regulator_print_opmode(buf,
844                 rdev->constraints->state_standby.mode);
845 }
846 static DEVICE_ATTR(suspend_standby_mode, 0444,
847                 regulator_suspend_standby_mode_show, NULL);
848
849 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
850                                    struct device_attribute *attr, char *buf)
851 {
852         struct regulator_dev *rdev = dev_get_drvdata(dev);
853
854         return regulator_print_state(buf,
855                         rdev->constraints->state_mem.enabled);
856 }
857 static DEVICE_ATTR(suspend_mem_state, 0444,
858                 regulator_suspend_mem_state_show, NULL);
859
860 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
861                                    struct device_attribute *attr, char *buf)
862 {
863         struct regulator_dev *rdev = dev_get_drvdata(dev);
864
865         return regulator_print_state(buf,
866                         rdev->constraints->state_disk.enabled);
867 }
868 static DEVICE_ATTR(suspend_disk_state, 0444,
869                 regulator_suspend_disk_state_show, NULL);
870
871 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
872                                    struct device_attribute *attr, char *buf)
873 {
874         struct regulator_dev *rdev = dev_get_drvdata(dev);
875
876         return regulator_print_state(buf,
877                         rdev->constraints->state_standby.enabled);
878 }
879 static DEVICE_ATTR(suspend_standby_state, 0444,
880                 regulator_suspend_standby_state_show, NULL);
881
882 static ssize_t regulator_bypass_show(struct device *dev,
883                                      struct device_attribute *attr, char *buf)
884 {
885         struct regulator_dev *rdev = dev_get_drvdata(dev);
886         const char *report;
887         bool bypass;
888         int ret;
889
890         ret = rdev->desc->ops->get_bypass(rdev, &bypass);
891
892         if (ret != 0)
893                 report = "unknown";
894         else if (bypass)
895                 report = "enabled";
896         else
897                 report = "disabled";
898
899         return sprintf(buf, "%s\n", report);
900 }
901 static DEVICE_ATTR(bypass, 0444,
902                    regulator_bypass_show, NULL);
903
904 /* Calculate the new optimum regulator operating mode based on the new total
905  * consumer load. All locks held by caller */
906 static int drms_uA_update(struct regulator_dev *rdev)
907 {
908         struct regulator *sibling;
909         int current_uA = 0, output_uV, input_uV, err;
910         unsigned int mode;
911
912         /*
913          * first check to see if we can set modes at all, otherwise just
914          * tell the consumer everything is OK.
915          */
916         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
917                 rdev_dbg(rdev, "DRMS operation not allowed\n");
918                 return 0;
919         }
920
921         if (!rdev->desc->ops->get_optimum_mode &&
922             !rdev->desc->ops->set_load)
923                 return 0;
924
925         if (!rdev->desc->ops->set_mode &&
926             !rdev->desc->ops->set_load)
927                 return -EINVAL;
928
929         /* calc total requested load */
930         list_for_each_entry(sibling, &rdev->consumer_list, list) {
931                 if (sibling->enable_count)
932                         current_uA += sibling->uA_load;
933         }
934
935         current_uA += rdev->constraints->system_load;
936
937         if (rdev->desc->ops->set_load) {
938                 /* set the optimum mode for our new total regulator load */
939                 err = rdev->desc->ops->set_load(rdev, current_uA);
940                 if (err < 0)
941                         rdev_err(rdev, "failed to set load %d\n", current_uA);
942         } else {
943                 /* get output voltage */
944                 output_uV = _regulator_get_voltage(rdev);
945                 if (output_uV <= 0) {
946                         rdev_err(rdev, "invalid output voltage found\n");
947                         return -EINVAL;
948                 }
949
950                 /* get input voltage */
951                 input_uV = 0;
952                 if (rdev->supply)
953                         input_uV = regulator_get_voltage(rdev->supply);
954                 if (input_uV <= 0)
955                         input_uV = rdev->constraints->input_uV;
956                 if (input_uV <= 0) {
957                         rdev_err(rdev, "invalid input voltage found\n");
958                         return -EINVAL;
959                 }
960
961                 /* now get the optimum mode for our new total regulator load */
962                 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
963                                                          output_uV, current_uA);
964
965                 /* check the new mode is allowed */
966                 err = regulator_mode_constrain(rdev, &mode);
967                 if (err < 0) {
968                         rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
969                                  current_uA, input_uV, output_uV);
970                         return err;
971                 }
972
973                 err = rdev->desc->ops->set_mode(rdev, mode);
974                 if (err < 0)
975                         rdev_err(rdev, "failed to set optimum mode %x\n", mode);
976         }
977
978         return err;
979 }
980
981 static int suspend_set_state(struct regulator_dev *rdev,
982                                     suspend_state_t state)
983 {
984         int ret = 0;
985         struct regulator_state *rstate;
986
987         rstate = regulator_get_suspend_state(rdev, state);
988         if (rstate == NULL)
989                 return 0;
990
991         /* If we have no suspend mode configuration don't set anything;
992          * only warn if the driver implements set_suspend_voltage or
993          * set_suspend_mode callback.
994          */
995         if (rstate->enabled != ENABLE_IN_SUSPEND &&
996             rstate->enabled != DISABLE_IN_SUSPEND) {
997                 if (rdev->desc->ops->set_suspend_voltage ||
998                     rdev->desc->ops->set_suspend_mode)
999                         rdev_warn(rdev, "No configuration\n");
1000                 return 0;
1001         }
1002
1003         if (rstate->enabled == ENABLE_IN_SUSPEND &&
1004                 rdev->desc->ops->set_suspend_enable)
1005                 ret = rdev->desc->ops->set_suspend_enable(rdev);
1006         else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1007                 rdev->desc->ops->set_suspend_disable)
1008                 ret = rdev->desc->ops->set_suspend_disable(rdev);
1009         else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1010                 ret = 0;
1011
1012         if (ret < 0) {
1013                 rdev_err(rdev, "failed to enabled/disable\n");
1014                 return ret;
1015         }
1016
1017         if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1018                 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1019                 if (ret < 0) {
1020                         rdev_err(rdev, "failed to set voltage\n");
1021                         return ret;
1022                 }
1023         }
1024
1025         if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1026                 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1027                 if (ret < 0) {
1028                         rdev_err(rdev, "failed to set mode\n");
1029                         return ret;
1030                 }
1031         }
1032
1033         return ret;
1034 }
1035
1036 static void print_constraints(struct regulator_dev *rdev)
1037 {
1038         struct regulation_constraints *constraints = rdev->constraints;
1039         char buf[160] = "";
1040         size_t len = sizeof(buf) - 1;
1041         int count = 0;
1042         int ret;
1043
1044         if (constraints->min_uV && constraints->max_uV) {
1045                 if (constraints->min_uV == constraints->max_uV)
1046                         count += scnprintf(buf + count, len - count, "%d mV ",
1047                                            constraints->min_uV / 1000);
1048                 else
1049                         count += scnprintf(buf + count, len - count,
1050                                            "%d <--> %d mV ",
1051                                            constraints->min_uV / 1000,
1052                                            constraints->max_uV / 1000);
1053         }
1054
1055         if (!constraints->min_uV ||
1056             constraints->min_uV != constraints->max_uV) {
1057                 ret = _regulator_get_voltage(rdev);
1058                 if (ret > 0)
1059                         count += scnprintf(buf + count, len - count,
1060                                            "at %d mV ", ret / 1000);
1061         }
1062
1063         if (constraints->uV_offset)
1064                 count += scnprintf(buf + count, len - count, "%dmV offset ",
1065                                    constraints->uV_offset / 1000);
1066
1067         if (constraints->min_uA && constraints->max_uA) {
1068                 if (constraints->min_uA == constraints->max_uA)
1069                         count += scnprintf(buf + count, len - count, "%d mA ",
1070                                            constraints->min_uA / 1000);
1071                 else
1072                         count += scnprintf(buf + count, len - count,
1073                                            "%d <--> %d mA ",
1074                                            constraints->min_uA / 1000,
1075                                            constraints->max_uA / 1000);
1076         }
1077
1078         if (!constraints->min_uA ||
1079             constraints->min_uA != constraints->max_uA) {
1080                 ret = _regulator_get_current_limit(rdev);
1081                 if (ret > 0)
1082                         count += scnprintf(buf + count, len - count,
1083                                            "at %d mA ", ret / 1000);
1084         }
1085
1086         if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1087                 count += scnprintf(buf + count, len - count, "fast ");
1088         if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1089                 count += scnprintf(buf + count, len - count, "normal ");
1090         if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1091                 count += scnprintf(buf + count, len - count, "idle ");
1092         if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1093                 count += scnprintf(buf + count, len - count, "standby");
1094
1095         if (!count)
1096                 scnprintf(buf, len, "no parameters");
1097
1098         rdev_dbg(rdev, "%s\n", buf);
1099
1100         if ((constraints->min_uV != constraints->max_uV) &&
1101             !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1102                 rdev_warn(rdev,
1103                           "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1104 }
1105
1106 static int machine_constraints_voltage(struct regulator_dev *rdev,
1107         struct regulation_constraints *constraints)
1108 {
1109         const struct regulator_ops *ops = rdev->desc->ops;
1110         int ret;
1111
1112         /* do we need to apply the constraint voltage */
1113         if (rdev->constraints->apply_uV &&
1114             rdev->constraints->min_uV && rdev->constraints->max_uV) {
1115                 int target_min, target_max;
1116                 int current_uV = _regulator_get_voltage(rdev);
1117
1118                 if (current_uV == -ENOTRECOVERABLE) {
1119                         /* This regulator can't be read and must be initialized */
1120                         rdev_info(rdev, "Setting %d-%duV\n",
1121                                   rdev->constraints->min_uV,
1122                                   rdev->constraints->max_uV);
1123                         _regulator_do_set_voltage(rdev,
1124                                                   rdev->constraints->min_uV,
1125                                                   rdev->constraints->max_uV);
1126                         current_uV = _regulator_get_voltage(rdev);
1127                 }
1128
1129                 if (current_uV < 0) {
1130                         rdev_err(rdev,
1131                                  "failed to get the current voltage(%d)\n",
1132                                  current_uV);
1133                         return current_uV;
1134                 }
1135
1136                 /*
1137                  * If we're below the minimum voltage move up to the
1138                  * minimum voltage, if we're above the maximum voltage
1139                  * then move down to the maximum.
1140                  */
1141                 target_min = current_uV;
1142                 target_max = current_uV;
1143
1144                 if (current_uV < rdev->constraints->min_uV) {
1145                         target_min = rdev->constraints->min_uV;
1146                         target_max = rdev->constraints->min_uV;
1147                 }
1148
1149                 if (current_uV > rdev->constraints->max_uV) {
1150                         target_min = rdev->constraints->max_uV;
1151                         target_max = rdev->constraints->max_uV;
1152                 }
1153
1154                 if (target_min != current_uV || target_max != current_uV) {
1155                         rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1156                                   current_uV, target_min, target_max);
1157                         ret = _regulator_do_set_voltage(
1158                                 rdev, target_min, target_max);
1159                         if (ret < 0) {
1160                                 rdev_err(rdev,
1161                                         "failed to apply %d-%duV constraint(%d)\n",
1162                                         target_min, target_max, ret);
1163                                 return ret;
1164                         }
1165                 }
1166         }
1167
1168         /* constrain machine-level voltage specs to fit
1169          * the actual range supported by this regulator.
1170          */
1171         if (ops->list_voltage && rdev->desc->n_voltages) {
1172                 int     count = rdev->desc->n_voltages;
1173                 int     i;
1174                 int     min_uV = INT_MAX;
1175                 int     max_uV = INT_MIN;
1176                 int     cmin = constraints->min_uV;
1177                 int     cmax = constraints->max_uV;
1178
1179                 /* it's safe to autoconfigure fixed-voltage supplies
1180                    and the constraints are used by list_voltage. */
1181                 if (count == 1 && !cmin) {
1182                         cmin = 1;
1183                         cmax = INT_MAX;
1184                         constraints->min_uV = cmin;
1185                         constraints->max_uV = cmax;
1186                 }
1187
1188                 /* voltage constraints are optional */
1189                 if ((cmin == 0) && (cmax == 0))
1190                         return 0;
1191
1192                 /* else require explicit machine-level constraints */
1193                 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1194                         rdev_err(rdev, "invalid voltage constraints\n");
1195                         return -EINVAL;
1196                 }
1197
1198                 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1199                 for (i = 0; i < count; i++) {
1200                         int     value;
1201
1202                         value = ops->list_voltage(rdev, i);
1203                         if (value <= 0)
1204                                 continue;
1205
1206                         /* maybe adjust [min_uV..max_uV] */
1207                         if (value >= cmin && value < min_uV)
1208                                 min_uV = value;
1209                         if (value <= cmax && value > max_uV)
1210                                 max_uV = value;
1211                 }
1212
1213                 /* final: [min_uV..max_uV] valid iff constraints valid */
1214                 if (max_uV < min_uV) {
1215                         rdev_err(rdev,
1216                                  "unsupportable voltage constraints %u-%uuV\n",
1217                                  min_uV, max_uV);
1218                         return -EINVAL;
1219                 }
1220
1221                 /* use regulator's subset of machine constraints */
1222                 if (constraints->min_uV < min_uV) {
1223                         rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1224                                  constraints->min_uV, min_uV);
1225                         constraints->min_uV = min_uV;
1226                 }
1227                 if (constraints->max_uV > max_uV) {
1228                         rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1229                                  constraints->max_uV, max_uV);
1230                         constraints->max_uV = max_uV;
1231                 }
1232         }
1233
1234         return 0;
1235 }
1236
1237 static int machine_constraints_current(struct regulator_dev *rdev,
1238         struct regulation_constraints *constraints)
1239 {
1240         const struct regulator_ops *ops = rdev->desc->ops;
1241         int ret;
1242
1243         if (!constraints->min_uA && !constraints->max_uA)
1244                 return 0;
1245
1246         if (constraints->min_uA > constraints->max_uA) {
1247                 rdev_err(rdev, "Invalid current constraints\n");
1248                 return -EINVAL;
1249         }
1250
1251         if (!ops->set_current_limit || !ops->get_current_limit) {
1252                 rdev_warn(rdev, "Operation of current configuration missing\n");
1253                 return 0;
1254         }
1255
1256         /* Set regulator current in constraints range */
1257         ret = ops->set_current_limit(rdev, constraints->min_uA,
1258                         constraints->max_uA);
1259         if (ret < 0) {
1260                 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1261                 return ret;
1262         }
1263
1264         return 0;
1265 }
1266
1267 static int _regulator_do_enable(struct regulator_dev *rdev);
1268
1269 /**
1270  * set_machine_constraints - sets regulator constraints
1271  * @rdev: regulator source
1272  * @constraints: constraints to apply
1273  *
1274  * Allows platform initialisation code to define and constrain
1275  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
1276  * Constraints *must* be set by platform code in order for some
1277  * regulator operations to proceed i.e. set_voltage, set_current_limit,
1278  * set_mode.
1279  */
1280 static int set_machine_constraints(struct regulator_dev *rdev,
1281         const struct regulation_constraints *constraints)
1282 {
1283         int ret = 0;
1284         const struct regulator_ops *ops = rdev->desc->ops;
1285
1286         if (constraints)
1287                 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1288                                             GFP_KERNEL);
1289         else
1290                 rdev->constraints = kzalloc(sizeof(*constraints),
1291                                             GFP_KERNEL);
1292         if (!rdev->constraints)
1293                 return -ENOMEM;
1294
1295         ret = machine_constraints_voltage(rdev, rdev->constraints);
1296         if (ret != 0)
1297                 return ret;
1298
1299         ret = machine_constraints_current(rdev, rdev->constraints);
1300         if (ret != 0)
1301                 return ret;
1302
1303         if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1304                 ret = ops->set_input_current_limit(rdev,
1305                                                    rdev->constraints->ilim_uA);
1306                 if (ret < 0) {
1307                         rdev_err(rdev, "failed to set input limit\n");
1308                         return ret;
1309                 }
1310         }
1311
1312         /* do we need to setup our suspend state */
1313         if (rdev->constraints->initial_state) {
1314                 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1315                 if (ret < 0) {
1316                         rdev_err(rdev, "failed to set suspend state\n");
1317                         return ret;
1318                 }
1319         }
1320
1321         if (rdev->constraints->initial_mode) {
1322                 if (!ops->set_mode) {
1323                         rdev_err(rdev, "no set_mode operation\n");
1324                         return -EINVAL;
1325                 }
1326
1327                 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1328                 if (ret < 0) {
1329                         rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1330                         return ret;
1331                 }
1332         } else if (rdev->constraints->system_load) {
1333                 /*
1334                  * We'll only apply the initial system load if an
1335                  * initial mode wasn't specified.
1336                  */
1337                 drms_uA_update(rdev);
1338         }
1339
1340         if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1341                 && ops->set_ramp_delay) {
1342                 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1343                 if (ret < 0) {
1344                         rdev_err(rdev, "failed to set ramp_delay\n");
1345                         return ret;
1346                 }
1347         }
1348
1349         if (rdev->constraints->pull_down && ops->set_pull_down) {
1350                 ret = ops->set_pull_down(rdev);
1351                 if (ret < 0) {
1352                         rdev_err(rdev, "failed to set pull down\n");
1353                         return ret;
1354                 }
1355         }
1356
1357         if (rdev->constraints->soft_start && ops->set_soft_start) {
1358                 ret = ops->set_soft_start(rdev);
1359                 if (ret < 0) {
1360                         rdev_err(rdev, "failed to set soft start\n");
1361                         return ret;
1362                 }
1363         }
1364
1365         if (rdev->constraints->over_current_protection
1366                 && ops->set_over_current_protection) {
1367                 ret = ops->set_over_current_protection(rdev);
1368                 if (ret < 0) {
1369                         rdev_err(rdev, "failed to set over current protection\n");
1370                         return ret;
1371                 }
1372         }
1373
1374         if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1375                 bool ad_state = (rdev->constraints->active_discharge ==
1376                               REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1377
1378                 ret = ops->set_active_discharge(rdev, ad_state);
1379                 if (ret < 0) {
1380                         rdev_err(rdev, "failed to set active discharge\n");
1381                         return ret;
1382                 }
1383         }
1384
1385         /* If the constraints say the regulator should be on at this point
1386          * and we have control then make sure it is enabled.
1387          */
1388         if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1389                 if (rdev->supply) {
1390                         ret = regulator_enable(rdev->supply);
1391                         if (ret < 0) {
1392                                 _regulator_put(rdev->supply);
1393                                 rdev->supply = NULL;
1394                                 return ret;
1395                         }
1396                 }
1397
1398                 ret = _regulator_do_enable(rdev);
1399                 if (ret < 0 && ret != -EINVAL) {
1400                         rdev_err(rdev, "failed to enable\n");
1401                         return ret;
1402                 }
1403                 rdev->use_count++;
1404         }
1405
1406         print_constraints(rdev);
1407         return 0;
1408 }
1409
1410 /**
1411  * set_supply - set regulator supply regulator
1412  * @rdev: regulator name
1413  * @supply_rdev: supply regulator name
1414  *
1415  * Called by platform initialisation code to set the supply regulator for this
1416  * regulator. This ensures that a regulators supply will also be enabled by the
1417  * core if it's child is enabled.
1418  */
1419 static int set_supply(struct regulator_dev *rdev,
1420                       struct regulator_dev *supply_rdev)
1421 {
1422         int err;
1423
1424         rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1425
1426         if (!try_module_get(supply_rdev->owner))
1427                 return -ENODEV;
1428
1429         rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1430         if (rdev->supply == NULL) {
1431                 err = -ENOMEM;
1432                 return err;
1433         }
1434         supply_rdev->open_count++;
1435
1436         return 0;
1437 }
1438
1439 /**
1440  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1441  * @rdev:         regulator source
1442  * @consumer_dev_name: dev_name() string for device supply applies to
1443  * @supply:       symbolic name for supply
1444  *
1445  * Allows platform initialisation code to map physical regulator
1446  * sources to symbolic names for supplies for use by devices.  Devices
1447  * should use these symbolic names to request regulators, avoiding the
1448  * need to provide board-specific regulator names as platform data.
1449  */
1450 static int set_consumer_device_supply(struct regulator_dev *rdev,
1451                                       const char *consumer_dev_name,
1452                                       const char *supply)
1453 {
1454         struct regulator_map *node;
1455         int has_dev;
1456
1457         if (supply == NULL)
1458                 return -EINVAL;
1459
1460         if (consumer_dev_name != NULL)
1461                 has_dev = 1;
1462         else
1463                 has_dev = 0;
1464
1465         list_for_each_entry(node, &regulator_map_list, list) {
1466                 if (node->dev_name && consumer_dev_name) {
1467                         if (strcmp(node->dev_name, consumer_dev_name) != 0)
1468                                 continue;
1469                 } else if (node->dev_name || consumer_dev_name) {
1470                         continue;
1471                 }
1472
1473                 if (strcmp(node->supply, supply) != 0)
1474                         continue;
1475
1476                 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1477                          consumer_dev_name,
1478                          dev_name(&node->regulator->dev),
1479                          node->regulator->desc->name,
1480                          supply,
1481                          dev_name(&rdev->dev), rdev_get_name(rdev));
1482                 return -EBUSY;
1483         }
1484
1485         node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1486         if (node == NULL)
1487                 return -ENOMEM;
1488
1489         node->regulator = rdev;
1490         node->supply = supply;
1491
1492         if (has_dev) {
1493                 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1494                 if (node->dev_name == NULL) {
1495                         kfree(node);
1496                         return -ENOMEM;
1497                 }
1498         }
1499
1500         list_add(&node->list, &regulator_map_list);
1501         return 0;
1502 }
1503
1504 static void unset_regulator_supplies(struct regulator_dev *rdev)
1505 {
1506         struct regulator_map *node, *n;
1507
1508         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1509                 if (rdev == node->regulator) {
1510                         list_del(&node->list);
1511                         kfree(node->dev_name);
1512                         kfree(node);
1513                 }
1514         }
1515 }
1516
1517 #ifdef CONFIG_DEBUG_FS
1518 static ssize_t constraint_flags_read_file(struct file *file,
1519                                           char __user *user_buf,
1520                                           size_t count, loff_t *ppos)
1521 {
1522         const struct regulator *regulator = file->private_data;
1523         const struct regulation_constraints *c = regulator->rdev->constraints;
1524         char *buf;
1525         ssize_t ret;
1526
1527         if (!c)
1528                 return 0;
1529
1530         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1531         if (!buf)
1532                 return -ENOMEM;
1533
1534         ret = snprintf(buf, PAGE_SIZE,
1535                         "always_on: %u\n"
1536                         "boot_on: %u\n"
1537                         "apply_uV: %u\n"
1538                         "ramp_disable: %u\n"
1539                         "soft_start: %u\n"
1540                         "pull_down: %u\n"
1541                         "over_current_protection: %u\n",
1542                         c->always_on,
1543                         c->boot_on,
1544                         c->apply_uV,
1545                         c->ramp_disable,
1546                         c->soft_start,
1547                         c->pull_down,
1548                         c->over_current_protection);
1549
1550         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1551         kfree(buf);
1552
1553         return ret;
1554 }
1555
1556 #endif
1557
1558 static const struct file_operations constraint_flags_fops = {
1559 #ifdef CONFIG_DEBUG_FS
1560         .open = simple_open,
1561         .read = constraint_flags_read_file,
1562         .llseek = default_llseek,
1563 #endif
1564 };
1565
1566 #define REG_STR_SIZE    64
1567
1568 static struct regulator *create_regulator(struct regulator_dev *rdev,
1569                                           struct device *dev,
1570                                           const char *supply_name)
1571 {
1572         struct regulator *regulator;
1573         char buf[REG_STR_SIZE];
1574         int err, size;
1575
1576         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1577         if (regulator == NULL)
1578                 return NULL;
1579
1580         regulator_lock(rdev);
1581         regulator->rdev = rdev;
1582         list_add(&regulator->list, &rdev->consumer_list);
1583
1584         if (dev) {
1585                 regulator->dev = dev;
1586
1587                 /* Add a link to the device sysfs entry */
1588                 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1589                                 dev->kobj.name, supply_name);
1590                 if (size >= REG_STR_SIZE)
1591                         goto overflow_err;
1592
1593                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1594                 if (regulator->supply_name == NULL)
1595                         goto overflow_err;
1596
1597                 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1598                                         buf);
1599                 if (err) {
1600                         rdev_dbg(rdev, "could not add device link %s err %d\n",
1601                                   dev->kobj.name, err);
1602                         /* non-fatal */
1603                 }
1604         } else {
1605                 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1606                 if (regulator->supply_name == NULL)
1607                         goto overflow_err;
1608         }
1609
1610         regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1611                                                 rdev->debugfs);
1612         if (!regulator->debugfs) {
1613                 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1614         } else {
1615                 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1616                                    &regulator->uA_load);
1617                 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1618                                    &regulator->voltage[PM_SUSPEND_ON].min_uV);
1619                 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1620                                    &regulator->voltage[PM_SUSPEND_ON].max_uV);
1621                 debugfs_create_file("constraint_flags", 0444,
1622                                     regulator->debugfs, regulator,
1623                                     &constraint_flags_fops);
1624         }
1625
1626         /*
1627          * Check now if the regulator is an always on regulator - if
1628          * it is then we don't need to do nearly so much work for
1629          * enable/disable calls.
1630          */
1631         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1632             _regulator_is_enabled(rdev))
1633                 regulator->always_on = true;
1634
1635         regulator_unlock(rdev);
1636         return regulator;
1637 overflow_err:
1638         list_del(&regulator->list);
1639         kfree(regulator);
1640         regulator_unlock(rdev);
1641         return NULL;
1642 }
1643
1644 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1645 {
1646         if (rdev->constraints && rdev->constraints->enable_time)
1647                 return rdev->constraints->enable_time;
1648         if (rdev->desc->ops->enable_time)
1649                 return rdev->desc->ops->enable_time(rdev);
1650         return rdev->desc->enable_time;
1651 }
1652
1653 static struct regulator_supply_alias *regulator_find_supply_alias(
1654                 struct device *dev, const char *supply)
1655 {
1656         struct regulator_supply_alias *map;
1657
1658         list_for_each_entry(map, &regulator_supply_alias_list, list)
1659                 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1660                         return map;
1661
1662         return NULL;
1663 }
1664
1665 static void regulator_supply_alias(struct device **dev, const char **supply)
1666 {
1667         struct regulator_supply_alias *map;
1668
1669         map = regulator_find_supply_alias(*dev, *supply);
1670         if (map) {
1671                 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1672                                 *supply, map->alias_supply,
1673                                 dev_name(map->alias_dev));
1674                 *dev = map->alias_dev;
1675                 *supply = map->alias_supply;
1676         }
1677 }
1678
1679 static int regulator_match(struct device *dev, const void *data)
1680 {
1681         struct regulator_dev *r = dev_to_rdev(dev);
1682
1683         return strcmp(rdev_get_name(r), data) == 0;
1684 }
1685
1686 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1687 {
1688         struct device *dev;
1689
1690         dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1691
1692         return dev ? dev_to_rdev(dev) : NULL;
1693 }
1694
1695 /**
1696  * regulator_dev_lookup - lookup a regulator device.
1697  * @dev: device for regulator "consumer".
1698  * @supply: Supply name or regulator ID.
1699  *
1700  * If successful, returns a struct regulator_dev that corresponds to the name
1701  * @supply and with the embedded struct device refcount incremented by one.
1702  * The refcount must be dropped by calling put_device().
1703  * On failure one of the following ERR-PTR-encoded values is returned:
1704  * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1705  * in the future.
1706  */
1707 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1708                                                   const char *supply)
1709 {
1710         struct regulator_dev *r = NULL;
1711         struct device_node *node;
1712         struct regulator_map *map;
1713         const char *devname = NULL;
1714
1715         regulator_supply_alias(&dev, &supply);
1716
1717         /* first do a dt based lookup */
1718         if (dev && dev->of_node) {
1719                 node = of_get_regulator(dev, supply);
1720                 if (node) {
1721                         r = of_find_regulator_by_node(node);
1722                         if (r)
1723                                 return r;
1724
1725                         /*
1726                          * We have a node, but there is no device.
1727                          * assume it has not registered yet.
1728                          */
1729                         return ERR_PTR(-EPROBE_DEFER);
1730                 }
1731         }
1732
1733         /* if not found, try doing it non-dt way */
1734         if (dev)
1735                 devname = dev_name(dev);
1736
1737         mutex_lock(&regulator_list_mutex);
1738         list_for_each_entry(map, &regulator_map_list, list) {
1739                 /* If the mapping has a device set up it must match */
1740                 if (map->dev_name &&
1741                     (!devname || strcmp(map->dev_name, devname)))
1742                         continue;
1743
1744                 if (strcmp(map->supply, supply) == 0 &&
1745                     get_device(&map->regulator->dev)) {
1746                         r = map->regulator;
1747                         break;
1748                 }
1749         }
1750         mutex_unlock(&regulator_list_mutex);
1751
1752         if (r)
1753                 return r;
1754
1755         r = regulator_lookup_by_name(supply);
1756         if (r)
1757                 return r;
1758
1759         return ERR_PTR(-ENODEV);
1760 }
1761
1762 static int regulator_resolve_supply(struct regulator_dev *rdev)
1763 {
1764         struct regulator_dev *r;
1765         struct device *dev = rdev->dev.parent;
1766         int ret;
1767
1768         /* No supply to resolve? */
1769         if (!rdev->supply_name)
1770                 return 0;
1771
1772         /* Supply already resolved? */
1773         if (rdev->supply)
1774                 return 0;
1775
1776         r = regulator_dev_lookup(dev, rdev->supply_name);
1777         if (IS_ERR(r)) {
1778                 ret = PTR_ERR(r);
1779
1780                 /* Did the lookup explicitly defer for us? */
1781                 if (ret == -EPROBE_DEFER)
1782                         return ret;
1783
1784                 if (have_full_constraints()) {
1785                         r = dummy_regulator_rdev;
1786                         get_device(&r->dev);
1787                 } else {
1788                         dev_err(dev, "Failed to resolve %s-supply for %s\n",
1789                                 rdev->supply_name, rdev->desc->name);
1790                         return -EPROBE_DEFER;
1791                 }
1792         }
1793
1794         /*
1795          * If the supply's parent device is not the same as the
1796          * regulator's parent device, then ensure the parent device
1797          * is bound before we resolve the supply, in case the parent
1798          * device get probe deferred and unregisters the supply.
1799          */
1800         if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1801                 if (!device_is_bound(r->dev.parent)) {
1802                         put_device(&r->dev);
1803                         return -EPROBE_DEFER;
1804                 }
1805         }
1806
1807         /* Recursively resolve the supply of the supply */
1808         ret = regulator_resolve_supply(r);
1809         if (ret < 0) {
1810                 put_device(&r->dev);
1811                 return ret;
1812         }
1813
1814         ret = set_supply(rdev, r);
1815         if (ret < 0) {
1816                 put_device(&r->dev);
1817                 return ret;
1818         }
1819
1820         /*
1821          * In set_machine_constraints() we may have turned this regulator on
1822          * but we couldn't propagate to the supply if it hadn't been resolved
1823          * yet.  Do it now.
1824          */
1825         if (rdev->use_count) {
1826                 ret = regulator_enable(rdev->supply);
1827                 if (ret < 0) {
1828                         _regulator_put(rdev->supply);
1829                         rdev->supply = NULL;
1830                         return ret;
1831                 }
1832         }
1833
1834         return 0;
1835 }
1836
1837 /* Internal regulator request function */
1838 struct regulator *_regulator_get(struct device *dev, const char *id,
1839                                  enum regulator_get_type get_type)
1840 {
1841         struct regulator_dev *rdev;
1842         struct regulator *regulator;
1843         const char *devname = dev ? dev_name(dev) : "deviceless";
1844         int ret;
1845
1846         if (get_type >= MAX_GET_TYPE) {
1847                 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1848                 return ERR_PTR(-EINVAL);
1849         }
1850
1851         if (id == NULL) {
1852                 pr_err("get() with no identifier\n");
1853                 return ERR_PTR(-EINVAL);
1854         }
1855
1856         rdev = regulator_dev_lookup(dev, id);
1857         if (IS_ERR(rdev)) {
1858                 ret = PTR_ERR(rdev);
1859
1860                 /*
1861                  * If regulator_dev_lookup() fails with error other
1862                  * than -ENODEV our job here is done, we simply return it.
1863                  */
1864                 if (ret != -ENODEV)
1865                         return ERR_PTR(ret);
1866
1867                 if (!have_full_constraints()) {
1868                         dev_warn(dev,
1869                                  "incomplete constraints, dummy supplies not allowed\n");
1870                         return ERR_PTR(-ENODEV);
1871                 }
1872
1873                 switch (get_type) {
1874                 case NORMAL_GET:
1875                         /*
1876                          * Assume that a regulator is physically present and
1877                          * enabled, even if it isn't hooked up, and just
1878                          * provide a dummy.
1879                          */
1880                         dev_warn(dev,
1881                                  "%s supply %s not found, using dummy regulator\n",
1882                                  devname, id);
1883                         rdev = dummy_regulator_rdev;
1884                         get_device(&rdev->dev);
1885                         break;
1886
1887                 case EXCLUSIVE_GET:
1888                         dev_warn(dev,
1889                                  "dummy supplies not allowed for exclusive requests\n");
1890                         /* fall through */
1891
1892                 default:
1893                         return ERR_PTR(-ENODEV);
1894                 }
1895         }
1896
1897         if (rdev->exclusive) {
1898                 regulator = ERR_PTR(-EPERM);
1899                 put_device(&rdev->dev);
1900                 return regulator;
1901         }
1902
1903         if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1904                 regulator = ERR_PTR(-EBUSY);
1905                 put_device(&rdev->dev);
1906                 return regulator;
1907         }
1908
1909         mutex_lock(&regulator_list_mutex);
1910         ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1911         mutex_unlock(&regulator_list_mutex);
1912
1913         if (ret != 0) {
1914                 regulator = ERR_PTR(-EPROBE_DEFER);
1915                 put_device(&rdev->dev);
1916                 return regulator;
1917         }
1918
1919         ret = regulator_resolve_supply(rdev);
1920         if (ret < 0) {
1921                 regulator = ERR_PTR(ret);
1922                 put_device(&rdev->dev);
1923                 return regulator;
1924         }
1925
1926         if (!try_module_get(rdev->owner)) {
1927                 regulator = ERR_PTR(-EPROBE_DEFER);
1928                 put_device(&rdev->dev);
1929                 return regulator;
1930         }
1931
1932         regulator = create_regulator(rdev, dev, id);
1933         if (regulator == NULL) {
1934                 regulator = ERR_PTR(-ENOMEM);
1935                 put_device(&rdev->dev);
1936                 module_put(rdev->owner);
1937                 return regulator;
1938         }
1939
1940         rdev->open_count++;
1941         if (get_type == EXCLUSIVE_GET) {
1942                 rdev->exclusive = 1;
1943
1944                 ret = _regulator_is_enabled(rdev);
1945                 if (ret > 0)
1946                         rdev->use_count = 1;
1947                 else
1948                         rdev->use_count = 0;
1949         }
1950
1951         device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1952
1953         return regulator;
1954 }
1955
1956 /**
1957  * regulator_get - lookup and obtain a reference to a regulator.
1958  * @dev: device for regulator "consumer"
1959  * @id: Supply name or regulator ID.
1960  *
1961  * Returns a struct regulator corresponding to the regulator producer,
1962  * or IS_ERR() condition containing errno.
1963  *
1964  * Use of supply names configured via regulator_set_device_supply() is
1965  * strongly encouraged.  It is recommended that the supply name used
1966  * should match the name used for the supply and/or the relevant
1967  * device pins in the datasheet.
1968  */
1969 struct regulator *regulator_get(struct device *dev, const char *id)
1970 {
1971         return _regulator_get(dev, id, NORMAL_GET);
1972 }
1973 EXPORT_SYMBOL_GPL(regulator_get);
1974
1975 /**
1976  * regulator_get_exclusive - obtain exclusive access to a regulator.
1977  * @dev: device for regulator "consumer"
1978  * @id: Supply name or regulator ID.
1979  *
1980  * Returns a struct regulator corresponding to the regulator producer,
1981  * or IS_ERR() condition containing errno.  Other consumers will be
1982  * unable to obtain this regulator while this reference is held and the
1983  * use count for the regulator will be initialised to reflect the current
1984  * state of the regulator.
1985  *
1986  * This is intended for use by consumers which cannot tolerate shared
1987  * use of the regulator such as those which need to force the
1988  * regulator off for correct operation of the hardware they are
1989  * controlling.
1990  *
1991  * Use of supply names configured via regulator_set_device_supply() is
1992  * strongly encouraged.  It is recommended that the supply name used
1993  * should match the name used for the supply and/or the relevant
1994  * device pins in the datasheet.
1995  */
1996 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1997 {
1998         return _regulator_get(dev, id, EXCLUSIVE_GET);
1999 }
2000 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2001
2002 /**
2003  * regulator_get_optional - obtain optional access to a regulator.
2004  * @dev: device for regulator "consumer"
2005  * @id: Supply name or regulator ID.
2006  *
2007  * Returns a struct regulator corresponding to the regulator producer,
2008  * or IS_ERR() condition containing errno.
2009  *
2010  * This is intended for use by consumers for devices which can have
2011  * some supplies unconnected in normal use, such as some MMC devices.
2012  * It can allow the regulator core to provide stub supplies for other
2013  * supplies requested using normal regulator_get() calls without
2014  * disrupting the operation of drivers that can handle absent
2015  * supplies.
2016  *
2017  * Use of supply names configured via regulator_set_device_supply() is
2018  * strongly encouraged.  It is recommended that the supply name used
2019  * should match the name used for the supply and/or the relevant
2020  * device pins in the datasheet.
2021  */
2022 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2023 {
2024         return _regulator_get(dev, id, OPTIONAL_GET);
2025 }
2026 EXPORT_SYMBOL_GPL(regulator_get_optional);
2027
2028 /* regulator_list_mutex lock held by regulator_put() */
2029 static void _regulator_put(struct regulator *regulator)
2030 {
2031         struct regulator_dev *rdev;
2032
2033         if (IS_ERR_OR_NULL(regulator))
2034                 return;
2035
2036         lockdep_assert_held_once(&regulator_list_mutex);
2037
2038         /* Docs say you must disable before calling regulator_put() */
2039         WARN_ON(regulator->enable_count);
2040
2041         rdev = regulator->rdev;
2042
2043         debugfs_remove_recursive(regulator->debugfs);
2044
2045         if (regulator->dev) {
2046                 device_link_remove(regulator->dev, &rdev->dev);
2047
2048                 /* remove any sysfs entries */
2049                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2050         }
2051
2052         regulator_lock(rdev);
2053         list_del(&regulator->list);
2054
2055         rdev->open_count--;
2056         rdev->exclusive = 0;
2057         put_device(&rdev->dev);
2058         regulator_unlock(rdev);
2059
2060         kfree_const(regulator->supply_name);
2061         kfree(regulator);
2062
2063         module_put(rdev->owner);
2064 }
2065
2066 /**
2067  * regulator_put - "free" the regulator source
2068  * @regulator: regulator source
2069  *
2070  * Note: drivers must ensure that all regulator_enable calls made on this
2071  * regulator source are balanced by regulator_disable calls prior to calling
2072  * this function.
2073  */
2074 void regulator_put(struct regulator *regulator)
2075 {
2076         mutex_lock(&regulator_list_mutex);
2077         _regulator_put(regulator);
2078         mutex_unlock(&regulator_list_mutex);
2079 }
2080 EXPORT_SYMBOL_GPL(regulator_put);
2081
2082 /**
2083  * regulator_register_supply_alias - Provide device alias for supply lookup
2084  *
2085  * @dev: device that will be given as the regulator "consumer"
2086  * @id: Supply name or regulator ID
2087  * @alias_dev: device that should be used to lookup the supply
2088  * @alias_id: Supply name or regulator ID that should be used to lookup the
2089  * supply
2090  *
2091  * All lookups for id on dev will instead be conducted for alias_id on
2092  * alias_dev.
2093  */
2094 int regulator_register_supply_alias(struct device *dev, const char *id,
2095                                     struct device *alias_dev,
2096                                     const char *alias_id)
2097 {
2098         struct regulator_supply_alias *map;
2099
2100         map = regulator_find_supply_alias(dev, id);
2101         if (map)
2102                 return -EEXIST;
2103
2104         map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2105         if (!map)
2106                 return -ENOMEM;
2107
2108         map->src_dev = dev;
2109         map->src_supply = id;
2110         map->alias_dev = alias_dev;
2111         map->alias_supply = alias_id;
2112
2113         list_add(&map->list, &regulator_supply_alias_list);
2114
2115         pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2116                 id, dev_name(dev), alias_id, dev_name(alias_dev));
2117
2118         return 0;
2119 }
2120 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2121
2122 /**
2123  * regulator_unregister_supply_alias - Remove device alias
2124  *
2125  * @dev: device that will be given as the regulator "consumer"
2126  * @id: Supply name or regulator ID
2127  *
2128  * Remove a lookup alias if one exists for id on dev.
2129  */
2130 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2131 {
2132         struct regulator_supply_alias *map;
2133
2134         map = regulator_find_supply_alias(dev, id);
2135         if (map) {
2136                 list_del(&map->list);
2137                 kfree(map);
2138         }
2139 }
2140 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2141
2142 /**
2143  * regulator_bulk_register_supply_alias - register multiple aliases
2144  *
2145  * @dev: device that will be given as the regulator "consumer"
2146  * @id: List of supply names or regulator IDs
2147  * @alias_dev: device that should be used to lookup the supply
2148  * @alias_id: List of supply names or regulator IDs that should be used to
2149  * lookup the supply
2150  * @num_id: Number of aliases to register
2151  *
2152  * @return 0 on success, an errno on failure.
2153  *
2154  * This helper function allows drivers to register several supply
2155  * aliases in one operation.  If any of the aliases cannot be
2156  * registered any aliases that were registered will be removed
2157  * before returning to the caller.
2158  */
2159 int regulator_bulk_register_supply_alias(struct device *dev,
2160                                          const char *const *id,
2161                                          struct device *alias_dev,
2162                                          const char *const *alias_id,
2163                                          int num_id)
2164 {
2165         int i;
2166         int ret;
2167
2168         for (i = 0; i < num_id; ++i) {
2169                 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2170                                                       alias_id[i]);
2171                 if (ret < 0)
2172                         goto err;
2173         }
2174
2175         return 0;
2176
2177 err:
2178         dev_err(dev,
2179                 "Failed to create supply alias %s,%s -> %s,%s\n",
2180                 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2181
2182         while (--i >= 0)
2183                 regulator_unregister_supply_alias(dev, id[i]);
2184
2185         return ret;
2186 }
2187 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2188
2189 /**
2190  * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2191  *
2192  * @dev: device that will be given as the regulator "consumer"
2193  * @id: List of supply names or regulator IDs
2194  * @num_id: Number of aliases to unregister
2195  *
2196  * This helper function allows drivers to unregister several supply
2197  * aliases in one operation.
2198  */
2199 void regulator_bulk_unregister_supply_alias(struct device *dev,
2200                                             const char *const *id,
2201                                             int num_id)
2202 {
2203         int i;
2204
2205         for (i = 0; i < num_id; ++i)
2206                 regulator_unregister_supply_alias(dev, id[i]);
2207 }
2208 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2209
2210
2211 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2212 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2213                                 const struct regulator_config *config)
2214 {
2215         struct regulator_enable_gpio *pin;
2216         struct gpio_desc *gpiod;
2217
2218         gpiod = config->ena_gpiod;
2219
2220         list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
2221                 if (pin->gpiod == gpiod) {
2222                         rdev_dbg(rdev, "GPIO is already used\n");
2223                         goto update_ena_gpio_to_rdev;
2224                 }
2225         }
2226
2227         pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2228         if (pin == NULL)
2229                 return -ENOMEM;
2230
2231         pin->gpiod = gpiod;
2232         list_add(&pin->list, &regulator_ena_gpio_list);
2233
2234 update_ena_gpio_to_rdev:
2235         pin->request_count++;
2236         rdev->ena_pin = pin;
2237         return 0;
2238 }
2239
2240 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2241 {
2242         struct regulator_enable_gpio *pin, *n;
2243
2244         if (!rdev->ena_pin)
2245                 return;
2246
2247         /* Free the GPIO only in case of no use */
2248         list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
2249                 if (pin->gpiod == rdev->ena_pin->gpiod) {
2250                         if (pin->request_count <= 1) {
2251                                 pin->request_count = 0;
2252                                 gpiod_put(pin->gpiod);
2253                                 list_del(&pin->list);
2254                                 kfree(pin);
2255                                 rdev->ena_pin = NULL;
2256                                 return;
2257                         } else {
2258                                 pin->request_count--;
2259                         }
2260                 }
2261         }
2262 }
2263
2264 /**
2265  * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2266  * @rdev: regulator_dev structure
2267  * @enable: enable GPIO at initial use?
2268  *
2269  * GPIO is enabled in case of initial use. (enable_count is 0)
2270  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2271  */
2272 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2273 {
2274         struct regulator_enable_gpio *pin = rdev->ena_pin;
2275
2276         if (!pin)
2277                 return -EINVAL;
2278
2279         if (enable) {
2280                 /* Enable GPIO at initial use */
2281                 if (pin->enable_count == 0)
2282                         gpiod_set_value_cansleep(pin->gpiod, 1);
2283
2284                 pin->enable_count++;
2285         } else {
2286                 if (pin->enable_count > 1) {
2287                         pin->enable_count--;
2288                         return 0;
2289                 }
2290
2291                 /* Disable GPIO if not used */
2292                 if (pin->enable_count <= 1) {
2293                         gpiod_set_value_cansleep(pin->gpiod, 0);
2294                         pin->enable_count = 0;
2295                 }
2296         }
2297
2298         return 0;
2299 }
2300
2301 /**
2302  * _regulator_enable_delay - a delay helper function
2303  * @delay: time to delay in microseconds
2304  *
2305  * Delay for the requested amount of time as per the guidelines in:
2306  *
2307  *     Documentation/timers/timers-howto.txt
2308  *
2309  * The assumption here is that regulators will never be enabled in
2310  * atomic context and therefore sleeping functions can be used.
2311  */
2312 static void _regulator_enable_delay(unsigned int delay)
2313 {
2314         unsigned int ms = delay / 1000;
2315         unsigned int us = delay % 1000;
2316
2317         if (ms > 0) {
2318                 /*
2319                  * For small enough values, handle super-millisecond
2320                  * delays in the usleep_range() call below.
2321                  */
2322                 if (ms < 20)
2323                         us += ms * 1000;
2324                 else
2325                         msleep(ms);
2326         }
2327
2328         /*
2329          * Give the scheduler some room to coalesce with any other
2330          * wakeup sources. For delays shorter than 10 us, don't even
2331          * bother setting up high-resolution timers and just busy-
2332          * loop.
2333          */
2334         if (us >= 10)
2335                 usleep_range(us, us + 100);
2336         else
2337                 udelay(us);
2338 }
2339
2340 static int _regulator_do_enable(struct regulator_dev *rdev)
2341 {
2342         int ret, delay;
2343
2344         /* Query before enabling in case configuration dependent.  */
2345         ret = _regulator_get_enable_time(rdev);
2346         if (ret >= 0) {
2347                 delay = ret;
2348         } else {
2349                 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2350                 delay = 0;
2351         }
2352
2353         trace_regulator_enable(rdev_get_name(rdev));
2354
2355         if (rdev->desc->off_on_delay) {
2356                 /* if needed, keep a distance of off_on_delay from last time
2357                  * this regulator was disabled.
2358                  */
2359                 unsigned long start_jiffy = jiffies;
2360                 unsigned long intended, max_delay, remaining;
2361
2362                 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2363                 intended = rdev->last_off_jiffy + max_delay;
2364
2365                 if (time_before(start_jiffy, intended)) {
2366                         /* calc remaining jiffies to deal with one-time
2367                          * timer wrapping.
2368                          * in case of multiple timer wrapping, either it can be
2369                          * detected by out-of-range remaining, or it cannot be
2370                          * detected and we get a penalty of
2371                          * _regulator_enable_delay().
2372                          */
2373                         remaining = intended - start_jiffy;
2374                         if (remaining <= max_delay)
2375                                 _regulator_enable_delay(
2376                                                 jiffies_to_usecs(remaining));
2377                 }
2378         }
2379
2380         if (rdev->ena_pin) {
2381                 if (!rdev->ena_gpio_state) {
2382                         ret = regulator_ena_gpio_ctrl(rdev, true);
2383                         if (ret < 0)
2384                                 return ret;
2385                         rdev->ena_gpio_state = 1;
2386                 }
2387         } else if (rdev->desc->ops->enable) {
2388                 ret = rdev->desc->ops->enable(rdev);
2389                 if (ret < 0)
2390                         return ret;
2391         } else {
2392                 return -EINVAL;
2393         }
2394
2395         /* Allow the regulator to ramp; it would be useful to extend
2396          * this for bulk operations so that the regulators can ramp
2397          * together.  */
2398         trace_regulator_enable_delay(rdev_get_name(rdev));
2399
2400         _regulator_enable_delay(delay);
2401
2402         trace_regulator_enable_complete(rdev_get_name(rdev));
2403
2404         return 0;
2405 }
2406
2407 /**
2408  * _regulator_handle_consumer_enable - handle that a consumer enabled
2409  * @regulator: regulator source
2410  *
2411  * Some things on a regulator consumer (like the contribution towards total
2412  * load on the regulator) only have an effect when the consumer wants the
2413  * regulator enabled.  Explained in example with two consumers of the same
2414  * regulator:
2415  *   consumer A: set_load(100);       => total load = 0
2416  *   consumer A: regulator_enable();  => total load = 100
2417  *   consumer B: set_load(1000);      => total load = 100
2418  *   consumer B: regulator_enable();  => total load = 1100
2419  *   consumer A: regulator_disable(); => total_load = 1000
2420  *
2421  * This function (together with _regulator_handle_consumer_disable) is
2422  * responsible for keeping track of the refcount for a given regulator consumer
2423  * and applying / unapplying these things.
2424  *
2425  * Returns 0 upon no error; -error upon error.
2426  */
2427 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2428 {
2429         struct regulator_dev *rdev = regulator->rdev;
2430
2431         lockdep_assert_held_once(&rdev->mutex.base);
2432
2433         regulator->enable_count++;
2434         if (regulator->uA_load && regulator->enable_count == 1)
2435                 return drms_uA_update(rdev);
2436
2437         return 0;
2438 }
2439
2440 /**
2441  * _regulator_handle_consumer_disable - handle that a consumer disabled
2442  * @regulator: regulator source
2443  *
2444  * The opposite of _regulator_handle_consumer_enable().
2445  *
2446  * Returns 0 upon no error; -error upon error.
2447  */
2448 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2449 {
2450         struct regulator_dev *rdev = regulator->rdev;
2451
2452         lockdep_assert_held_once(&rdev->mutex.base);
2453
2454         if (!regulator->enable_count) {
2455                 rdev_err(rdev, "Underflow of regulator enable count\n");
2456                 return -EINVAL;
2457         }
2458
2459         regulator->enable_count--;
2460         if (regulator->uA_load && regulator->enable_count == 0)
2461                 return drms_uA_update(rdev);
2462
2463         return 0;
2464 }
2465
2466 /* locks held by regulator_enable() */
2467 static int _regulator_enable(struct regulator *regulator)
2468 {
2469         struct regulator_dev *rdev = regulator->rdev;
2470         int ret;
2471
2472         lockdep_assert_held_once(&rdev->mutex.base);
2473
2474         if (rdev->use_count == 0 && rdev->supply) {
2475                 ret = _regulator_enable(rdev->supply);
2476                 if (ret < 0)
2477                         return ret;
2478         }
2479
2480         /* balance only if there are regulators coupled */
2481         if (rdev->coupling_desc.n_coupled > 1) {
2482                 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2483                 if (ret < 0)
2484                         goto err_disable_supply;
2485         }
2486
2487         ret = _regulator_handle_consumer_enable(regulator);
2488         if (ret < 0)
2489                 goto err_disable_supply;
2490
2491         if (rdev->use_count == 0) {
2492                 /* The regulator may on if it's not switchable or left on */
2493                 ret = _regulator_is_enabled(rdev);
2494                 if (ret == -EINVAL || ret == 0) {
2495                         if (!regulator_ops_is_valid(rdev,
2496                                         REGULATOR_CHANGE_STATUS)) {
2497                                 ret = -EPERM;
2498                                 goto err_consumer_disable;
2499                         }
2500
2501                         ret = _regulator_do_enable(rdev);
2502                         if (ret < 0)
2503                                 goto err_consumer_disable;
2504
2505                         _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2506                                              NULL);
2507                 } else if (ret < 0) {
2508                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2509                         goto err_consumer_disable;
2510                 }
2511                 /* Fallthrough on positive return values - already enabled */
2512         }
2513
2514         rdev->use_count++;
2515
2516         return 0;
2517
2518 err_consumer_disable:
2519         _regulator_handle_consumer_disable(regulator);
2520
2521 err_disable_supply:
2522         if (rdev->use_count == 0 && rdev->supply)
2523                 _regulator_disable(rdev->supply);
2524
2525         return ret;
2526 }
2527
2528 /**
2529  * regulator_enable - enable regulator output
2530  * @regulator: regulator source
2531  *
2532  * Request that the regulator be enabled with the regulator output at
2533  * the predefined voltage or current value.  Calls to regulator_enable()
2534  * must be balanced with calls to regulator_disable().
2535  *
2536  * NOTE: the output value can be set by other drivers, boot loader or may be
2537  * hardwired in the regulator.
2538  */
2539 int regulator_enable(struct regulator *regulator)
2540 {
2541         struct regulator_dev *rdev = regulator->rdev;
2542         struct ww_acquire_ctx ww_ctx;
2543         int ret;
2544
2545         regulator_lock_dependent(rdev, &ww_ctx);
2546         ret = _regulator_enable(regulator);
2547         regulator_unlock_dependent(rdev, &ww_ctx);
2548
2549         return ret;
2550 }
2551 EXPORT_SYMBOL_GPL(regulator_enable);
2552
2553 static int _regulator_do_disable(struct regulator_dev *rdev)
2554 {
2555         int ret;
2556
2557         trace_regulator_disable(rdev_get_name(rdev));
2558
2559         if (rdev->ena_pin) {
2560                 if (rdev->ena_gpio_state) {
2561                         ret = regulator_ena_gpio_ctrl(rdev, false);
2562                         if (ret < 0)
2563                                 return ret;
2564                         rdev->ena_gpio_state = 0;
2565                 }
2566
2567         } else if (rdev->desc->ops->disable) {
2568                 ret = rdev->desc->ops->disable(rdev);
2569                 if (ret != 0)
2570                         return ret;
2571         }
2572
2573         /* cares about last_off_jiffy only if off_on_delay is required by
2574          * device.
2575          */
2576         if (rdev->desc->off_on_delay)
2577                 rdev->last_off_jiffy = jiffies;
2578
2579         trace_regulator_disable_complete(rdev_get_name(rdev));
2580
2581         return 0;
2582 }
2583
2584 /* locks held by regulator_disable() */
2585 static int _regulator_disable(struct regulator *regulator)
2586 {
2587         struct regulator_dev *rdev = regulator->rdev;
2588         int ret = 0;
2589
2590         lockdep_assert_held_once(&rdev->mutex.base);
2591
2592         if (WARN(rdev->use_count <= 0,
2593                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
2594                 return -EIO;
2595
2596         /* are we the last user and permitted to disable ? */
2597         if (rdev->use_count == 1 &&
2598             (rdev->constraints && !rdev->constraints->always_on)) {
2599
2600                 /* we are last user */
2601                 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2602                         ret = _notifier_call_chain(rdev,
2603                                                    REGULATOR_EVENT_PRE_DISABLE,
2604                                                    NULL);
2605                         if (ret & NOTIFY_STOP_MASK)
2606                                 return -EINVAL;
2607
2608                         ret = _regulator_do_disable(rdev);
2609                         if (ret < 0) {
2610                                 rdev_err(rdev, "failed to disable\n");
2611                                 _notifier_call_chain(rdev,
2612                                                 REGULATOR_EVENT_ABORT_DISABLE,
2613                                                 NULL);
2614                                 return ret;
2615                         }
2616                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2617                                         NULL);
2618                 }
2619
2620                 rdev->use_count = 0;
2621         } else if (rdev->use_count > 1) {
2622                 rdev->use_count--;
2623         }
2624
2625         if (ret == 0)
2626                 ret = _regulator_handle_consumer_disable(regulator);
2627
2628         if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2629                 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2630
2631         if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2632                 ret = _regulator_disable(rdev->supply);
2633
2634         return ret;
2635 }
2636
2637 /**
2638  * regulator_disable - disable regulator output
2639  * @regulator: regulator source
2640  *
2641  * Disable the regulator output voltage or current.  Calls to
2642  * regulator_enable() must be balanced with calls to
2643  * regulator_disable().
2644  *
2645  * NOTE: this will only disable the regulator output if no other consumer
2646  * devices have it enabled, the regulator device supports disabling and
2647  * machine constraints permit this operation.
2648  */
2649 int regulator_disable(struct regulator *regulator)
2650 {
2651         struct regulator_dev *rdev = regulator->rdev;
2652         struct ww_acquire_ctx ww_ctx;
2653         int ret;
2654
2655         regulator_lock_dependent(rdev, &ww_ctx);
2656         ret = _regulator_disable(regulator);
2657         regulator_unlock_dependent(rdev, &ww_ctx);
2658
2659         return ret;
2660 }
2661 EXPORT_SYMBOL_GPL(regulator_disable);
2662
2663 /* locks held by regulator_force_disable() */
2664 static int _regulator_force_disable(struct regulator_dev *rdev)
2665 {
2666         int ret = 0;
2667
2668         lockdep_assert_held_once(&rdev->mutex.base);
2669
2670         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2671                         REGULATOR_EVENT_PRE_DISABLE, NULL);
2672         if (ret & NOTIFY_STOP_MASK)
2673                 return -EINVAL;
2674
2675         ret = _regulator_do_disable(rdev);
2676         if (ret < 0) {
2677                 rdev_err(rdev, "failed to force disable\n");
2678                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2679                                 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2680                 return ret;
2681         }
2682
2683         _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2684                         REGULATOR_EVENT_DISABLE, NULL);
2685
2686         return 0;
2687 }
2688
2689 /**
2690  * regulator_force_disable - force disable regulator output
2691  * @regulator: regulator source
2692  *
2693  * Forcibly disable the regulator output voltage or current.
2694  * NOTE: this *will* disable the regulator output even if other consumer
2695  * devices have it enabled. This should be used for situations when device
2696  * damage will likely occur if the regulator is not disabled (e.g. over temp).
2697  */
2698 int regulator_force_disable(struct regulator *regulator)
2699 {
2700         struct regulator_dev *rdev = regulator->rdev;
2701         struct ww_acquire_ctx ww_ctx;
2702         int ret;
2703
2704         regulator_lock_dependent(rdev, &ww_ctx);
2705
2706         ret = _regulator_force_disable(regulator->rdev);
2707
2708         if (rdev->coupling_desc.n_coupled > 1)
2709                 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2710
2711         if (regulator->uA_load) {
2712                 regulator->uA_load = 0;
2713                 ret = drms_uA_update(rdev);
2714         }
2715
2716         if (rdev->use_count != 0 && rdev->supply)
2717                 _regulator_disable(rdev->supply);
2718
2719         regulator_unlock_dependent(rdev, &ww_ctx);
2720
2721         return ret;
2722 }
2723 EXPORT_SYMBOL_GPL(regulator_force_disable);
2724
2725 static void regulator_disable_work(struct work_struct *work)
2726 {
2727         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2728                                                   disable_work.work);
2729         struct ww_acquire_ctx ww_ctx;
2730         int count, i, ret;
2731         struct regulator *regulator;
2732         int total_count = 0;
2733
2734         regulator_lock_dependent(rdev, &ww_ctx);
2735
2736         /*
2737          * Workqueue functions queue the new work instance while the previous
2738          * work instance is being processed. Cancel the queued work instance
2739          * as the work instance under processing does the job of the queued
2740          * work instance.
2741          */
2742         cancel_delayed_work(&rdev->disable_work);
2743
2744         list_for_each_entry(regulator, &rdev->consumer_list, list) {
2745                 count = regulator->deferred_disables;
2746
2747                 if (!count)
2748                         continue;
2749
2750                 total_count += count;
2751                 regulator->deferred_disables = 0;
2752
2753                 for (i = 0; i < count; i++) {
2754                         ret = _regulator_disable(regulator);
2755                         if (ret != 0)
2756                                 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2757                 }
2758         }
2759         WARN_ON(!total_count);
2760
2761         if (rdev->coupling_desc.n_coupled > 1)
2762                 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2763
2764         regulator_unlock_dependent(rdev, &ww_ctx);
2765 }
2766
2767 /**
2768  * regulator_disable_deferred - disable regulator output with delay
2769  * @regulator: regulator source
2770  * @ms: milliseconds until the regulator is disabled
2771  *
2772  * Execute regulator_disable() on the regulator after a delay.  This
2773  * is intended for use with devices that require some time to quiesce.
2774  *
2775  * NOTE: this will only disable the regulator output if no other consumer
2776  * devices have it enabled, the regulator device supports disabling and
2777  * machine constraints permit this operation.
2778  */
2779 int regulator_disable_deferred(struct regulator *regulator, int ms)
2780 {
2781         struct regulator_dev *rdev = regulator->rdev;
2782
2783         if (!ms)
2784                 return regulator_disable(regulator);
2785
2786         regulator_lock(rdev);
2787         regulator->deferred_disables++;
2788         mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2789                          msecs_to_jiffies(ms));
2790         regulator_unlock(rdev);
2791
2792         return 0;
2793 }
2794 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2795
2796 static int _regulator_is_enabled(struct regulator_dev *rdev)
2797 {
2798         /* A GPIO control always takes precedence */
2799         if (rdev->ena_pin)
2800                 return rdev->ena_gpio_state;
2801
2802         /* If we don't know then assume that the regulator is always on */
2803         if (!rdev->desc->ops->is_enabled)
2804                 return 1;
2805
2806         return rdev->desc->ops->is_enabled(rdev);
2807 }
2808
2809 static int _regulator_list_voltage(struct regulator_dev *rdev,
2810                                    unsigned selector, int lock)
2811 {
2812         const struct regulator_ops *ops = rdev->desc->ops;
2813         int ret;
2814
2815         if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2816                 return rdev->desc->fixed_uV;
2817
2818         if (ops->list_voltage) {
2819                 if (selector >= rdev->desc->n_voltages)
2820                         return -EINVAL;
2821                 if (lock)
2822                         regulator_lock(rdev);
2823                 ret = ops->list_voltage(rdev, selector);
2824                 if (lock)
2825                         regulator_unlock(rdev);
2826         } else if (rdev->is_switch && rdev->supply) {
2827                 ret = _regulator_list_voltage(rdev->supply->rdev,
2828                                               selector, lock);
2829         } else {
2830                 return -EINVAL;
2831         }
2832
2833         if (ret > 0) {
2834                 if (ret < rdev->constraints->min_uV)
2835                         ret = 0;
2836                 else if (ret > rdev->constraints->max_uV)
2837                         ret = 0;
2838         }
2839
2840         return ret;
2841 }
2842
2843 /**
2844  * regulator_is_enabled - is the regulator output enabled
2845  * @regulator: regulator source
2846  *
2847  * Returns positive if the regulator driver backing the source/client
2848  * has requested that the device be enabled, zero if it hasn't, else a
2849  * negative errno code.
2850  *
2851  * Note that the device backing this regulator handle can have multiple
2852  * users, so it might be enabled even if regulator_enable() was never
2853  * called for this particular source.
2854  */
2855 int regulator_is_enabled(struct regulator *regulator)
2856 {
2857         int ret;
2858
2859         if (regulator->always_on)
2860                 return 1;
2861
2862         regulator_lock(regulator->rdev);
2863         ret = _regulator_is_enabled(regulator->rdev);
2864         regulator_unlock(regulator->rdev);
2865
2866         return ret;
2867 }
2868 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2869
2870 /**
2871  * regulator_count_voltages - count regulator_list_voltage() selectors
2872  * @regulator: regulator source
2873  *
2874  * Returns number of selectors, or negative errno.  Selectors are
2875  * numbered starting at zero, and typically correspond to bitfields
2876  * in hardware registers.
2877  */
2878 int regulator_count_voltages(struct regulator *regulator)
2879 {
2880         struct regulator_dev    *rdev = regulator->rdev;
2881
2882         if (rdev->desc->n_voltages)
2883                 return rdev->desc->n_voltages;
2884
2885         if (!rdev->is_switch || !rdev->supply)
2886                 return -EINVAL;
2887
2888         return regulator_count_voltages(rdev->supply);
2889 }
2890 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2891
2892 /**
2893  * regulator_list_voltage - enumerate supported voltages
2894  * @regulator: regulator source
2895  * @selector: identify voltage to list
2896  * Context: can sleep
2897  *
2898  * Returns a voltage that can be passed to @regulator_set_voltage(),
2899  * zero if this selector code can't be used on this system, or a
2900  * negative errno.
2901  */
2902 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2903 {
2904         return _regulator_list_voltage(regulator->rdev, selector, 1);
2905 }
2906 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2907
2908 /**
2909  * regulator_get_regmap - get the regulator's register map
2910  * @regulator: regulator source
2911  *
2912  * Returns the register map for the given regulator, or an ERR_PTR value
2913  * if the regulator doesn't use regmap.
2914  */
2915 struct regmap *regulator_get_regmap(struct regulator *regulator)
2916 {
2917         struct regmap *map = regulator->rdev->regmap;
2918
2919         return map ? map : ERR_PTR(-EOPNOTSUPP);
2920 }
2921
2922 /**
2923  * regulator_get_hardware_vsel_register - get the HW voltage selector register
2924  * @regulator: regulator source
2925  * @vsel_reg: voltage selector register, output parameter
2926  * @vsel_mask: mask for voltage selector bitfield, output parameter
2927  *
2928  * Returns the hardware register offset and bitmask used for setting the
2929  * regulator voltage. This might be useful when configuring voltage-scaling
2930  * hardware or firmware that can make I2C requests behind the kernel's back,
2931  * for example.
2932  *
2933  * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2934  * and 0 is returned, otherwise a negative errno is returned.
2935  */
2936 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2937                                          unsigned *vsel_reg,
2938                                          unsigned *vsel_mask)
2939 {
2940         struct regulator_dev *rdev = regulator->rdev;
2941         const struct regulator_ops *ops = rdev->desc->ops;
2942
2943         if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2944                 return -EOPNOTSUPP;
2945
2946         *vsel_reg = rdev->desc->vsel_reg;
2947         *vsel_mask = rdev->desc->vsel_mask;
2948
2949          return 0;
2950 }
2951 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2952
2953 /**
2954  * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2955  * @regulator: regulator source
2956  * @selector: identify voltage to list
2957  *
2958  * Converts the selector to a hardware-specific voltage selector that can be
2959  * directly written to the regulator registers. The address of the voltage
2960  * register can be determined by calling @regulator_get_hardware_vsel_register.
2961  *
2962  * On error a negative errno is returned.
2963  */
2964 int regulator_list_hardware_vsel(struct regulator *regulator,
2965                                  unsigned selector)
2966 {
2967         struct regulator_dev *rdev = regulator->rdev;
2968         const struct regulator_ops *ops = rdev->desc->ops;
2969
2970         if (selector >= rdev->desc->n_voltages)
2971                 return -EINVAL;
2972         if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2973                 return -EOPNOTSUPP;
2974
2975         return selector;
2976 }
2977 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2978
2979 /**
2980  * regulator_get_linear_step - return the voltage step size between VSEL values
2981  * @regulator: regulator source
2982  *
2983  * Returns the voltage step size between VSEL values for linear
2984  * regulators, or return 0 if the regulator isn't a linear regulator.
2985  */
2986 unsigned int regulator_get_linear_step(struct regulator *regulator)
2987 {
2988         struct regulator_dev *rdev = regulator->rdev;
2989
2990         return rdev->desc->uV_step;
2991 }
2992 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2993
2994 /**
2995  * regulator_is_supported_voltage - check if a voltage range can be supported
2996  *
2997  * @regulator: Regulator to check.
2998  * @min_uV: Minimum required voltage in uV.
2999  * @max_uV: Maximum required voltage in uV.
3000  *
3001  * Returns a boolean.
3002  */
3003 int regulator_is_supported_voltage(struct regulator *regulator,
3004                                    int min_uV, int max_uV)
3005 {
3006         struct regulator_dev *rdev = regulator->rdev;
3007         int i, voltages, ret;
3008
3009         /* If we can't change voltage check the current voltage */
3010         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3011                 ret = regulator_get_voltage(regulator);
3012                 if (ret >= 0)
3013                         return min_uV <= ret && ret <= max_uV;
3014                 else
3015                         return ret;
3016         }
3017
3018         /* Any voltage within constrains range is fine? */
3019         if (rdev->desc->continuous_voltage_range)
3020                 return min_uV >= rdev->constraints->min_uV &&
3021                                 max_uV <= rdev->constraints->max_uV;
3022
3023         ret = regulator_count_voltages(regulator);
3024         if (ret < 0)
3025                 return 0;
3026         voltages = ret;
3027
3028         for (i = 0; i < voltages; i++) {
3029                 ret = regulator_list_voltage(regulator, i);
3030
3031                 if (ret >= min_uV && ret <= max_uV)
3032                         return 1;
3033         }
3034
3035         return 0;
3036 }
3037 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3038
3039 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3040                                  int max_uV)
3041 {
3042         const struct regulator_desc *desc = rdev->desc;
3043
3044         if (desc->ops->map_voltage)
3045                 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3046
3047         if (desc->ops->list_voltage == regulator_list_voltage_linear)
3048                 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3049
3050         if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3051                 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3052
3053         if (desc->ops->list_voltage ==
3054                 regulator_list_voltage_pickable_linear_range)
3055                 return regulator_map_voltage_pickable_linear_range(rdev,
3056                                                         min_uV, max_uV);
3057
3058         return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3059 }
3060
3061 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3062                                        int min_uV, int max_uV,
3063                                        unsigned *selector)
3064 {
3065         struct pre_voltage_change_data data;
3066         int ret;
3067
3068         data.old_uV = _regulator_get_voltage(rdev);
3069         data.min_uV = min_uV;
3070         data.max_uV = max_uV;
3071         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3072                                    &data);
3073         if (ret & NOTIFY_STOP_MASK)
3074                 return -EINVAL;
3075
3076         ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3077         if (ret >= 0)
3078                 return ret;
3079
3080         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3081                              (void *)data.old_uV);
3082
3083         return ret;
3084 }
3085
3086 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3087                                            int uV, unsigned selector)
3088 {
3089         struct pre_voltage_change_data data;
3090         int ret;
3091
3092         data.old_uV = _regulator_get_voltage(rdev);
3093         data.min_uV = uV;
3094         data.max_uV = uV;
3095         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3096                                    &data);
3097         if (ret & NOTIFY_STOP_MASK)
3098                 return -EINVAL;
3099
3100         ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3101         if (ret >= 0)
3102                 return ret;
3103
3104         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3105                              (void *)data.old_uV);
3106
3107         return ret;
3108 }
3109
3110 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3111                                        int old_uV, int new_uV)
3112 {
3113         unsigned int ramp_delay = 0;
3114
3115         if (rdev->constraints->ramp_delay)
3116                 ramp_delay = rdev->constraints->ramp_delay;
3117         else if (rdev->desc->ramp_delay)
3118                 ramp_delay = rdev->desc->ramp_delay;
3119         else if (rdev->constraints->settling_time)
3120                 return rdev->constraints->settling_time;
3121         else if (rdev->constraints->settling_time_up &&
3122                  (new_uV > old_uV))
3123                 return rdev->constraints->settling_time_up;
3124         else if (rdev->constraints->settling_time_down &&
3125                  (new_uV < old_uV))
3126                 return rdev->constraints->settling_time_down;
3127
3128         if (ramp_delay == 0) {
3129                 rdev_dbg(rdev, "ramp_delay not set\n");
3130                 return 0;
3131         }
3132
3133         return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3134 }
3135
3136 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3137                                      int min_uV, int max_uV)
3138 {
3139         int ret;
3140         int delay = 0;
3141         int best_val = 0;
3142         unsigned int selector;
3143         int old_selector = -1;
3144         const struct regulator_ops *ops = rdev->desc->ops;
3145         int old_uV = _regulator_get_voltage(rdev);
3146
3147         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3148
3149         min_uV += rdev->constraints->uV_offset;
3150         max_uV += rdev->constraints->uV_offset;
3151
3152         /*
3153          * If we can't obtain the old selector there is not enough
3154          * info to call set_voltage_time_sel().
3155          */
3156         if (_regulator_is_enabled(rdev) &&
3157             ops->set_voltage_time_sel && ops->get_voltage_sel) {
3158                 old_selector = ops->get_voltage_sel(rdev);
3159                 if (old_selector < 0)
3160                         return old_selector;
3161         }
3162
3163         if (ops->set_voltage) {
3164                 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3165                                                   &selector);
3166
3167                 if (ret >= 0) {
3168                         if (ops->list_voltage)
3169                                 best_val = ops->list_voltage(rdev,
3170                                                              selector);
3171                         else
3172                                 best_val = _regulator_get_voltage(rdev);
3173                 }
3174
3175         } else if (ops->set_voltage_sel) {
3176                 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3177                 if (ret >= 0) {
3178                         best_val = ops->list_voltage(rdev, ret);
3179                         if (min_uV <= best_val && max_uV >= best_val) {
3180                                 selector = ret;
3181                                 if (old_selector == selector)
3182                                         ret = 0;
3183                                 else
3184                                         ret = _regulator_call_set_voltage_sel(
3185                                                 rdev, best_val, selector);
3186                         } else {
3187                                 ret = -EINVAL;
3188                         }
3189                 }
3190         } else {
3191                 ret = -EINVAL;
3192         }
3193
3194         if (ret)
3195                 goto out;
3196
3197         if (ops->set_voltage_time_sel) {
3198                 /*
3199                  * Call set_voltage_time_sel if successfully obtained
3200                  * old_selector
3201                  */
3202                 if (old_selector >= 0 && old_selector != selector)
3203                         delay = ops->set_voltage_time_sel(rdev, old_selector,
3204                                                           selector);
3205         } else {
3206                 if (old_uV != best_val) {
3207                         if (ops->set_voltage_time)
3208                                 delay = ops->set_voltage_time(rdev, old_uV,
3209                                                               best_val);
3210                         else
3211                                 delay = _regulator_set_voltage_time(rdev,
3212                                                                     old_uV,
3213                                                                     best_val);
3214                 }
3215         }
3216
3217         if (delay < 0) {
3218                 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3219                 delay = 0;
3220         }
3221
3222         /* Insert any necessary delays */
3223         if (delay >= 1000) {
3224                 mdelay(delay / 1000);
3225                 udelay(delay % 1000);
3226         } else if (delay) {
3227                 udelay(delay);
3228         }
3229
3230         if (best_val >= 0) {
3231                 unsigned long data = best_val;
3232
3233                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3234                                      (void *)data);
3235         }
3236
3237 out:
3238         trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3239
3240         return ret;
3241 }
3242
3243 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3244                                   int min_uV, int max_uV, suspend_state_t state)
3245 {
3246         struct regulator_state *rstate;
3247         int uV, sel;
3248
3249         rstate = regulator_get_suspend_state(rdev, state);
3250         if (rstate == NULL)
3251                 return -EINVAL;
3252
3253         if (min_uV < rstate->min_uV)
3254                 min_uV = rstate->min_uV;
3255         if (max_uV > rstate->max_uV)
3256                 max_uV = rstate->max_uV;
3257
3258         sel = regulator_map_voltage(rdev, min_uV, max_uV);
3259         if (sel < 0)
3260                 return sel;
3261
3262         uV = rdev->desc->ops->list_voltage(rdev, sel);
3263         if (uV >= min_uV && uV <= max_uV)
3264                 rstate->uV = uV;
3265
3266         return 0;
3267 }
3268
3269 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3270                                           int min_uV, int max_uV,
3271                                           suspend_state_t state)
3272 {
3273         struct regulator_dev *rdev = regulator->rdev;
3274         struct regulator_voltage *voltage = &regulator->voltage[state];
3275         int ret = 0;
3276         int old_min_uV, old_max_uV;
3277         int current_uV;
3278
3279         /* If we're setting the same range as last time the change
3280          * should be a noop (some cpufreq implementations use the same
3281          * voltage for multiple frequencies, for example).
3282          */
3283         if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3284                 goto out;
3285
3286         /* If we're trying to set a range that overlaps the current voltage,
3287          * return successfully even though the regulator does not support
3288          * changing the voltage.
3289          */
3290         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3291                 current_uV = _regulator_get_voltage(rdev);
3292                 if (min_uV <= current_uV && current_uV <= max_uV) {
3293                         voltage->min_uV = min_uV;
3294                         voltage->max_uV = max_uV;
3295                         goto out;
3296                 }
3297         }
3298
3299         /* sanity check */
3300         if (!rdev->desc->ops->set_voltage &&
3301             !rdev->desc->ops->set_voltage_sel) {
3302                 ret = -EINVAL;
3303                 goto out;
3304         }
3305
3306         /* constraints check */
3307         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3308         if (ret < 0)
3309                 goto out;
3310
3311         /* restore original values in case of error */
3312         old_min_uV = voltage->min_uV;
3313         old_max_uV = voltage->max_uV;
3314         voltage->min_uV = min_uV;
3315         voltage->max_uV = max_uV;
3316
3317         /* for not coupled regulators this will just set the voltage */
3318         ret = regulator_balance_voltage(rdev, state);
3319         if (ret < 0) {
3320                 voltage->min_uV = old_min_uV;
3321                 voltage->max_uV = old_max_uV;
3322         }
3323
3324 out:
3325         return ret;
3326 }
3327
3328 static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3329                                       int max_uV, suspend_state_t state)
3330 {
3331         int best_supply_uV = 0;
3332         int supply_change_uV = 0;
3333         int ret;
3334
3335         if (rdev->supply &&
3336             regulator_ops_is_valid(rdev->supply->rdev,
3337                                    REGULATOR_CHANGE_VOLTAGE) &&
3338             (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3339                                            rdev->desc->ops->get_voltage_sel))) {
3340                 int current_supply_uV;
3341                 int selector;
3342
3343                 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3344                 if (selector < 0) {
3345                         ret = selector;
3346                         goto out;
3347                 }
3348
3349                 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3350                 if (best_supply_uV < 0) {
3351                         ret = best_supply_uV;
3352                         goto out;
3353                 }
3354
3355                 best_supply_uV += rdev->desc->min_dropout_uV;
3356
3357                 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3358                 if (current_supply_uV < 0) {
3359                         ret = current_supply_uV;
3360                         goto out;
3361                 }
3362
3363                 supply_change_uV = best_supply_uV - current_supply_uV;
3364         }
3365
3366         if (supply_change_uV > 0) {
3367                 ret = regulator_set_voltage_unlocked(rdev->supply,
3368                                 best_supply_uV, INT_MAX, state);
3369                 if (ret) {
3370                         dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3371                                         ret);
3372                         goto out;
3373                 }
3374         }
3375
3376         if (state == PM_SUSPEND_ON)
3377                 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3378         else
3379                 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3380                                                         max_uV, state);
3381         if (ret < 0)
3382                 goto out;
3383
3384         if (supply_change_uV < 0) {
3385                 ret = regulator_set_voltage_unlocked(rdev->supply,
3386                                 best_supply_uV, INT_MAX, state);
3387                 if (ret)
3388                         dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3389                                         ret);
3390                 /* No need to fail here */
3391                 ret = 0;
3392         }
3393
3394 out:
3395         return ret;
3396 }
3397
3398 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3399                                         int *current_uV, int *min_uV)
3400 {
3401         struct regulation_constraints *constraints = rdev->constraints;
3402
3403         /* Limit voltage change only if necessary */
3404         if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3405                 return 1;
3406
3407         if (*current_uV < 0) {
3408                 *current_uV = _regulator_get_voltage(rdev);
3409
3410                 if (*current_uV < 0)
3411                         return *current_uV;
3412         }
3413
3414         if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3415                 return 1;
3416
3417         /* Clamp target voltage within the given step */
3418         if (*current_uV < *min_uV)
3419                 *min_uV = min(*current_uV + constraints->max_uV_step,
3420                               *min_uV);
3421         else
3422                 *min_uV = max(*current_uV - constraints->max_uV_step,
3423                               *min_uV);
3424
3425         return 0;
3426 }
3427
3428 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3429                                          int *current_uV,
3430                                          int *min_uV, int *max_uV,
3431                                          suspend_state_t state,
3432                                          int n_coupled)
3433 {
3434         struct coupling_desc *c_desc = &rdev->coupling_desc;
3435         struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3436         struct regulation_constraints *constraints = rdev->constraints;
3437         int max_spread = constraints->max_spread;
3438         int desired_min_uV = 0, desired_max_uV = INT_MAX;
3439         int max_current_uV = 0, min_current_uV = INT_MAX;
3440         int highest_min_uV = 0, target_uV, possible_uV;
3441         int i, ret;
3442         bool done;
3443
3444         *current_uV = -1;
3445
3446         /*
3447          * If there are no coupled regulators, simply set the voltage
3448          * demanded by consumers.
3449          */
3450         if (n_coupled == 1) {
3451                 /*
3452                  * If consumers don't provide any demands, set voltage
3453                  * to min_uV
3454                  */
3455                 desired_min_uV = constraints->min_uV;
3456                 desired_max_uV = constraints->max_uV;
3457
3458                 ret = regulator_check_consumers(rdev,
3459                                                 &desired_min_uV,
3460                                                 &desired_max_uV, state);
3461                 if (ret < 0)
3462                         return ret;
3463
3464                 possible_uV = desired_min_uV;
3465                 done = true;
3466
3467                 goto finish;
3468         }
3469
3470         /* Find highest min desired voltage */
3471         for (i = 0; i < n_coupled; i++) {
3472                 int tmp_min = 0;
3473                 int tmp_max = INT_MAX;
3474
3475                 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3476
3477                 ret = regulator_check_consumers(c_rdevs[i],
3478                                                 &tmp_min,
3479                                                 &tmp_max, state);
3480                 if (ret < 0)
3481                         return ret;
3482
3483                 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3484                 if (ret < 0)
3485                         return ret;
3486
3487                 highest_min_uV = max(highest_min_uV, tmp_min);
3488
3489                 if (i == 0) {
3490                         desired_min_uV = tmp_min;
3491                         desired_max_uV = tmp_max;
3492                 }
3493         }
3494
3495         /*
3496          * Let target_uV be equal to the desired one if possible.
3497          * If not, set it to minimum voltage, allowed by other coupled
3498          * regulators.
3499          */
3500         target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3501
3502         /*
3503          * Find min and max voltages, which currently aren't violating
3504          * max_spread.
3505          */
3506         for (i = 1; i < n_coupled; i++) {
3507                 int tmp_act;
3508
3509                 if (!_regulator_is_enabled(c_rdevs[i]))
3510                         continue;
3511
3512                 tmp_act = _regulator_get_voltage(c_rdevs[i]);
3513                 if (tmp_act < 0)
3514                         return tmp_act;
3515
3516                 min_current_uV = min(tmp_act, min_current_uV);
3517                 max_current_uV = max(tmp_act, max_current_uV);
3518         }
3519
3520         /* There aren't any other regulators enabled */
3521         if (max_current_uV == 0) {
3522                 possible_uV = target_uV;
3523         } else {
3524                 /*
3525                  * Correct target voltage, so as it currently isn't
3526                  * violating max_spread
3527                  */
3528                 possible_uV = max(target_uV, max_current_uV - max_spread);
3529                 possible_uV = min(possible_uV, min_current_uV + max_spread);
3530         }
3531
3532         if (possible_uV > desired_max_uV)
3533                 return -EINVAL;
3534
3535         done = (possible_uV == target_uV);
3536         desired_min_uV = possible_uV;
3537
3538 finish:
3539         /* Apply max_uV_step constraint if necessary */
3540         if (state == PM_SUSPEND_ON) {
3541                 ret = regulator_limit_voltage_step(rdev, current_uV,
3542                                                    &desired_min_uV);
3543                 if (ret < 0)
3544                         return ret;
3545
3546                 if (ret == 0)
3547                         done = false;
3548         }
3549
3550         /* Set current_uV if wasn't done earlier in the code and if necessary */
3551         if (n_coupled > 1 && *current_uV == -1) {
3552
3553                 if (_regulator_is_enabled(rdev)) {
3554                         ret = _regulator_get_voltage(rdev);
3555                         if (ret < 0)
3556                                 return ret;
3557
3558                         *current_uV = ret;
3559                 } else {
3560                         *current_uV = desired_min_uV;
3561                 }
3562         }
3563
3564         *min_uV = desired_min_uV;
3565         *max_uV = desired_max_uV;
3566
3567         return done;
3568 }
3569
3570 static int regulator_balance_voltage(struct regulator_dev *rdev,
3571                                      suspend_state_t state)
3572 {
3573         struct regulator_dev **c_rdevs;
3574         struct regulator_dev *best_rdev;
3575         struct coupling_desc *c_desc = &rdev->coupling_desc;
3576         int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3577         bool best_c_rdev_done, c_rdev_done[MAX_COUPLED];
3578         unsigned int delta, best_delta;
3579
3580         c_rdevs = c_desc->coupled_rdevs;
3581         n_coupled = c_desc->n_coupled;
3582
3583         /*
3584          * If system is in a state other than PM_SUSPEND_ON, don't check
3585          * other coupled regulators.
3586          */
3587         if (state != PM_SUSPEND_ON)
3588                 n_coupled = 1;
3589
3590         if (c_desc->n_resolved < n_coupled) {
3591                 rdev_err(rdev, "Not all coupled regulators registered\n");
3592                 return -EPERM;
3593         }
3594
3595         for (i = 0; i < n_coupled; i++)
3596                 c_rdev_done[i] = false;
3597
3598         /*
3599          * Find the best possible voltage change on each loop. Leave the loop
3600          * if there isn't any possible change.
3601          */
3602         do {
3603                 best_c_rdev_done = false;
3604                 best_delta = 0;
3605                 best_min_uV = 0;
3606                 best_max_uV = 0;
3607                 best_c_rdev = 0;
3608                 best_rdev = NULL;
3609
3610                 /*
3611                  * Find highest difference between optimal voltage
3612                  * and current voltage.
3613                  */
3614                 for (i = 0; i < n_coupled; i++) {
3615                         /*
3616                          * optimal_uV is the best voltage that can be set for
3617                          * i-th regulator at the moment without violating
3618                          * max_spread constraint in order to balance
3619                          * the coupled voltages.
3620                          */
3621                         int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3622
3623                         if (c_rdev_done[i])
3624                                 continue;
3625
3626                         ret = regulator_get_optimal_voltage(c_rdevs[i],
3627                                                             &current_uV,
3628                                                             &optimal_uV,
3629                                                             &optimal_max_uV,
3630                                                             state, n_coupled);
3631                         if (ret < 0)
3632                                 goto out;
3633
3634                         delta = abs(optimal_uV - current_uV);
3635
3636                         if (delta && best_delta <= delta) {
3637                                 best_c_rdev_done = ret;
3638                                 best_delta = delta;
3639                                 best_rdev = c_rdevs[i];
3640                                 best_min_uV = optimal_uV;
3641                                 best_max_uV = optimal_max_uV;
3642                                 best_c_rdev = i;
3643                         }
3644                 }
3645
3646                 /* Nothing to change, return successfully */
3647                 if (!best_rdev) {
3648                         ret = 0;
3649                         goto out;
3650                 }
3651
3652                 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3653                                                  best_max_uV, state);
3654
3655                 if (ret < 0)
3656                         goto out;
3657
3658                 c_rdev_done[best_c_rdev] = best_c_rdev_done;
3659
3660         } while (n_coupled > 1);
3661
3662 out:
3663         return ret;
3664 }
3665
3666 /**
3667  * regulator_set_voltage - set regulator output voltage
3668  * @regulator: regulator source
3669  * @min_uV: Minimum required voltage in uV
3670  * @max_uV: Maximum acceptable voltage in uV
3671  *
3672  * Sets a voltage regulator to the desired output voltage. This can be set
3673  * during any regulator state. IOW, regulator can be disabled or enabled.
3674  *
3675  * If the regulator is enabled then the voltage will change to the new value
3676  * immediately otherwise if the regulator is disabled the regulator will
3677  * output at the new voltage when enabled.
3678  *
3679  * NOTE: If the regulator is shared between several devices then the lowest
3680  * request voltage that meets the system constraints will be used.
3681  * Regulator system constraints must be set for this regulator before
3682  * calling this function otherwise this call will fail.
3683  */
3684 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3685 {
3686         struct ww_acquire_ctx ww_ctx;
3687         int ret;
3688
3689         regulator_lock_dependent(regulator->rdev, &ww_ctx);
3690
3691         ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3692                                              PM_SUSPEND_ON);
3693
3694         regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3695
3696         return ret;
3697 }
3698 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3699
3700 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3701                                            suspend_state_t state, bool en)
3702 {
3703         struct regulator_state *rstate;
3704
3705         rstate = regulator_get_suspend_state(rdev, state);
3706         if (rstate == NULL)
3707                 return -EINVAL;
3708
3709         if (!rstate->changeable)
3710                 return -EPERM;
3711
3712         rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3713
3714         return 0;
3715 }
3716
3717 int regulator_suspend_enable(struct regulator_dev *rdev,
3718                                     suspend_state_t state)
3719 {
3720         return regulator_suspend_toggle(rdev, state, true);