GNU Linux-libre 5.4.241-gnu1
[releases.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 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/async.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/of.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/coupler.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 LIST_HEAD(regulator_coupler_list);
54 static bool has_full_constraints;
55
56 static struct dentry *debugfs_root;
57
58 /*
59  * struct regulator_map
60  *
61  * Used to provide symbolic supply names to devices.
62  */
63 struct regulator_map {
64         struct list_head list;
65         const char *dev_name;   /* The dev_name() for the consumer */
66         const char *supply;
67         struct regulator_dev *regulator;
68 };
69
70 /*
71  * struct regulator_enable_gpio
72  *
73  * Management for shared enable GPIO pin
74  */
75 struct regulator_enable_gpio {
76         struct list_head list;
77         struct gpio_desc *gpiod;
78         u32 enable_count;       /* a number of enabled shared GPIO */
79         u32 request_count;      /* a number of requested shared GPIO */
80 };
81
82 /*
83  * struct regulator_supply_alias
84  *
85  * Used to map lookups for a supply onto an alternative device.
86  */
87 struct regulator_supply_alias {
88         struct list_head list;
89         struct device *src_dev;
90         const char *src_supply;
91         struct device *alias_dev;
92         const char *alias_supply;
93 };
94
95 static int _regulator_is_enabled(struct regulator_dev *rdev);
96 static int _regulator_disable(struct regulator *regulator);
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 struct regulator *create_regulator(struct regulator_dev *rdev,
106                                           struct device *dev,
107                                           const char *supply_name);
108 static void _regulator_put(struct regulator *regulator);
109
110 const char *rdev_get_name(struct regulator_dev *rdev)
111 {
112         if (rdev->constraints && rdev->constraints->name)
113                 return rdev->constraints->name;
114         else if (rdev->desc->name)
115                 return rdev->desc->name;
116         else
117                 return "";
118 }
119
120 static bool have_full_constraints(void)
121 {
122         return has_full_constraints || of_have_populated_dt();
123 }
124
125 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
126 {
127         if (!rdev->constraints) {
128                 rdev_err(rdev, "no constraints\n");
129                 return false;
130         }
131
132         if (rdev->constraints->valid_ops_mask & ops)
133                 return true;
134
135         return false;
136 }
137
138 /**
139  * regulator_lock_nested - lock a single regulator
140  * @rdev:               regulator source
141  * @ww_ctx:             w/w mutex acquire context
142  *
143  * This function can be called many times by one task on
144  * a single regulator and its mutex will be locked only
145  * once. If a task, which is calling this function is other
146  * than the one, which initially locked the mutex, it will
147  * wait on mutex.
148  */
149 static inline int regulator_lock_nested(struct regulator_dev *rdev,
150                                         struct ww_acquire_ctx *ww_ctx)
151 {
152         bool lock = false;
153         int ret = 0;
154
155         mutex_lock(&regulator_nesting_mutex);
156
157         if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
158                 if (rdev->mutex_owner == current)
159                         rdev->ref_cnt++;
160                 else
161                         lock = true;
162
163                 if (lock) {
164                         mutex_unlock(&regulator_nesting_mutex);
165                         ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
166                         mutex_lock(&regulator_nesting_mutex);
167                 }
168         } else {
169                 lock = true;
170         }
171
172         if (lock && ret != -EDEADLK) {
173                 rdev->ref_cnt++;
174                 rdev->mutex_owner = current;
175         }
176
177         mutex_unlock(&regulator_nesting_mutex);
178
179         return ret;
180 }
181
182 /**
183  * regulator_lock - lock a single regulator
184  * @rdev:               regulator source
185  *
186  * This function can be called many times by one task on
187  * a single regulator and its mutex will be locked only
188  * once. If a task, which is calling this function is other
189  * than the one, which initially locked the mutex, it will
190  * wait on mutex.
191  */
192 void regulator_lock(struct regulator_dev *rdev)
193 {
194         regulator_lock_nested(rdev, NULL);
195 }
196 EXPORT_SYMBOL_GPL(regulator_lock);
197
198 /**
199  * regulator_unlock - unlock a single regulator
200  * @rdev:               regulator_source
201  *
202  * This function unlocks the mutex when the
203  * reference counter reaches 0.
204  */
205 void regulator_unlock(struct regulator_dev *rdev)
206 {
207         mutex_lock(&regulator_nesting_mutex);
208
209         if (--rdev->ref_cnt == 0) {
210                 rdev->mutex_owner = NULL;
211                 ww_mutex_unlock(&rdev->mutex);
212         }
213
214         WARN_ON_ONCE(rdev->ref_cnt < 0);
215
216         mutex_unlock(&regulator_nesting_mutex);
217 }
218 EXPORT_SYMBOL_GPL(regulator_unlock);
219
220 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
221 {
222         struct regulator_dev *c_rdev;
223         int i;
224
225         for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
226                 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
227
228                 if (rdev->supply->rdev == c_rdev)
229                         return true;
230         }
231
232         return false;
233 }
234
235 static void regulator_unlock_recursive(struct regulator_dev *rdev,
236                                        unsigned int n_coupled)
237 {
238         struct regulator_dev *c_rdev, *supply_rdev;
239         int i, supply_n_coupled;
240
241         for (i = n_coupled; i > 0; i--) {
242                 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
243
244                 if (!c_rdev)
245                         continue;
246
247                 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
248                         supply_rdev = c_rdev->supply->rdev;
249                         supply_n_coupled = supply_rdev->coupling_desc.n_coupled;
250
251                         regulator_unlock_recursive(supply_rdev,
252                                                    supply_n_coupled);
253                 }
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                                 goto err_node_put;
388                 } else {
389                         goto err_node_put;
390                 }
391         }
392         return NULL;
393
394 err_node_put:
395         of_node_put(child);
396         return regnode;
397 }
398
399 /**
400  * of_get_regulator - get a regulator device node based on supply name
401  * @dev: Device pointer for the consumer (of regulator) device
402  * @supply: regulator supply name
403  *
404  * Extract the regulator device node corresponding to the supply name.
405  * returns the device node corresponding to the regulator if found, else
406  * returns NULL.
407  */
408 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
409 {
410         struct device_node *regnode = NULL;
411         char prop_name[32]; /* 32 is max size of property name */
412
413         dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
414
415         snprintf(prop_name, 32, "%s-supply", supply);
416         regnode = of_parse_phandle(dev->of_node, prop_name, 0);
417
418         if (!regnode) {
419                 regnode = of_get_child_regulator(dev->of_node, prop_name);
420                 if (regnode)
421                         return regnode;
422
423                 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
424                                 prop_name, dev->of_node);
425                 return NULL;
426         }
427         return regnode;
428 }
429
430 /* Platform voltage constraint check */
431 int regulator_check_voltage(struct regulator_dev *rdev,
432                             int *min_uV, int *max_uV)
433 {
434         BUG_ON(*min_uV > *max_uV);
435
436         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
437                 rdev_err(rdev, "voltage operation not allowed\n");
438                 return -EPERM;
439         }
440
441         if (*max_uV > rdev->constraints->max_uV)
442                 *max_uV = rdev->constraints->max_uV;
443         if (*min_uV < rdev->constraints->min_uV)
444                 *min_uV = rdev->constraints->min_uV;
445
446         if (*min_uV > *max_uV) {
447                 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
448                          *min_uV, *max_uV);
449                 return -EINVAL;
450         }
451
452         return 0;
453 }
454
455 /* return 0 if the state is valid */
456 static int regulator_check_states(suspend_state_t state)
457 {
458         return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
459 }
460
461 /* Make sure we select a voltage that suits the needs of all
462  * regulator consumers
463  */
464 int regulator_check_consumers(struct regulator_dev *rdev,
465                               int *min_uV, int *max_uV,
466                               suspend_state_t state)
467 {
468         struct regulator *regulator;
469         struct regulator_voltage *voltage;
470
471         list_for_each_entry(regulator, &rdev->consumer_list, list) {
472                 voltage = &regulator->voltage[state];
473                 /*
474                  * Assume consumers that didn't say anything are OK
475                  * with anything in the constraint range.
476                  */
477                 if (!voltage->min_uV && !voltage->max_uV)
478                         continue;
479
480                 if (*max_uV > voltage->max_uV)
481                         *max_uV = voltage->max_uV;
482                 if (*min_uV < voltage->min_uV)
483                         *min_uV = voltage->min_uV;
484         }
485
486         if (*min_uV > *max_uV) {
487                 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
488                         *min_uV, *max_uV);
489                 return -EINVAL;
490         }
491
492         return 0;
493 }
494
495 /* current constraint check */
496 static int regulator_check_current_limit(struct regulator_dev *rdev,
497                                         int *min_uA, int *max_uA)
498 {
499         BUG_ON(*min_uA > *max_uA);
500
501         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
502                 rdev_err(rdev, "current operation not allowed\n");
503                 return -EPERM;
504         }
505
506         if (*max_uA > rdev->constraints->max_uA)
507                 *max_uA = rdev->constraints->max_uA;
508         if (*min_uA < rdev->constraints->min_uA)
509                 *min_uA = rdev->constraints->min_uA;
510
511         if (*min_uA > *max_uA) {
512                 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
513                          *min_uA, *max_uA);
514                 return -EINVAL;
515         }
516
517         return 0;
518 }
519
520 /* operating mode constraint check */
521 static int regulator_mode_constrain(struct regulator_dev *rdev,
522                                     unsigned int *mode)
523 {
524         switch (*mode) {
525         case REGULATOR_MODE_FAST:
526         case REGULATOR_MODE_NORMAL:
527         case REGULATOR_MODE_IDLE:
528         case REGULATOR_MODE_STANDBY:
529                 break;
530         default:
531                 rdev_err(rdev, "invalid mode %x specified\n", *mode);
532                 return -EINVAL;
533         }
534
535         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
536                 rdev_err(rdev, "mode operation not allowed\n");
537                 return -EPERM;
538         }
539
540         /* The modes are bitmasks, the most power hungry modes having
541          * the lowest values. If the requested mode isn't supported
542          * try higher modes. */
543         while (*mode) {
544                 if (rdev->constraints->valid_modes_mask & *mode)
545                         return 0;
546                 *mode /= 2;
547         }
548
549         return -EINVAL;
550 }
551
552 static inline struct regulator_state *
553 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
554 {
555         if (rdev->constraints == NULL)
556                 return NULL;
557
558         switch (state) {
559         case PM_SUSPEND_STANDBY:
560                 return &rdev->constraints->state_standby;
561         case PM_SUSPEND_MEM:
562                 return &rdev->constraints->state_mem;
563         case PM_SUSPEND_MAX:
564                 return &rdev->constraints->state_disk;
565         default:
566                 return NULL;
567         }
568 }
569
570 static ssize_t regulator_uV_show(struct device *dev,
571                                 struct device_attribute *attr, char *buf)
572 {
573         struct regulator_dev *rdev = dev_get_drvdata(dev);
574         int uV;
575
576         regulator_lock(rdev);
577         uV = regulator_get_voltage_rdev(rdev);
578         regulator_unlock(rdev);
579
580         if (uV < 0)
581                 return uV;
582         return sprintf(buf, "%d\n", uV);
583 }
584 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
585
586 static ssize_t regulator_uA_show(struct device *dev,
587                                 struct device_attribute *attr, char *buf)
588 {
589         struct regulator_dev *rdev = dev_get_drvdata(dev);
590
591         return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
592 }
593 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
594
595 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
596                          char *buf)
597 {
598         struct regulator_dev *rdev = dev_get_drvdata(dev);
599
600         return sprintf(buf, "%s\n", rdev_get_name(rdev));
601 }
602 static DEVICE_ATTR_RO(name);
603
604 static const char *regulator_opmode_to_str(int mode)
605 {
606         switch (mode) {
607         case REGULATOR_MODE_FAST:
608                 return "fast";
609         case REGULATOR_MODE_NORMAL:
610                 return "normal";
611         case REGULATOR_MODE_IDLE:
612                 return "idle";
613         case REGULATOR_MODE_STANDBY:
614                 return "standby";
615         }
616         return "unknown";
617 }
618
619 static ssize_t regulator_print_opmode(char *buf, int mode)
620 {
621         return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
622 }
623
624 static ssize_t regulator_opmode_show(struct device *dev,
625                                     struct device_attribute *attr, char *buf)
626 {
627         struct regulator_dev *rdev = dev_get_drvdata(dev);
628
629         return regulator_print_opmode(buf, _regulator_get_mode(rdev));
630 }
631 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
632
633 static ssize_t regulator_print_state(char *buf, int state)
634 {
635         if (state > 0)
636                 return sprintf(buf, "enabled\n");
637         else if (state == 0)
638                 return sprintf(buf, "disabled\n");
639         else
640                 return sprintf(buf, "unknown\n");
641 }
642
643 static ssize_t regulator_state_show(struct device *dev,
644                                    struct device_attribute *attr, char *buf)
645 {
646         struct regulator_dev *rdev = dev_get_drvdata(dev);
647         ssize_t ret;
648
649         regulator_lock(rdev);
650         ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
651         regulator_unlock(rdev);
652
653         return ret;
654 }
655 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
656
657 static ssize_t regulator_status_show(struct device *dev,
658                                    struct device_attribute *attr, char *buf)
659 {
660         struct regulator_dev *rdev = dev_get_drvdata(dev);
661         int status;
662         char *label;
663
664         status = rdev->desc->ops->get_status(rdev);
665         if (status < 0)
666                 return status;
667
668         switch (status) {
669         case REGULATOR_STATUS_OFF:
670                 label = "off";
671                 break;
672         case REGULATOR_STATUS_ON:
673                 label = "on";
674                 break;
675         case REGULATOR_STATUS_ERROR:
676                 label = "error";
677                 break;
678         case REGULATOR_STATUS_FAST:
679                 label = "fast";
680                 break;
681         case REGULATOR_STATUS_NORMAL:
682                 label = "normal";
683                 break;
684         case REGULATOR_STATUS_IDLE:
685                 label = "idle";
686                 break;
687         case REGULATOR_STATUS_STANDBY:
688                 label = "standby";
689                 break;
690         case REGULATOR_STATUS_BYPASS:
691                 label = "bypass";
692                 break;
693         case REGULATOR_STATUS_UNDEFINED:
694                 label = "undefined";
695                 break;
696         default:
697                 return -ERANGE;
698         }
699
700         return sprintf(buf, "%s\n", label);
701 }
702 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
703
704 static ssize_t regulator_min_uA_show(struct device *dev,
705                                     struct device_attribute *attr, char *buf)
706 {
707         struct regulator_dev *rdev = dev_get_drvdata(dev);
708
709         if (!rdev->constraints)
710                 return sprintf(buf, "constraint not defined\n");
711
712         return sprintf(buf, "%d\n", rdev->constraints->min_uA);
713 }
714 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
715
716 static ssize_t regulator_max_uA_show(struct device *dev,
717                                     struct device_attribute *attr, char *buf)
718 {
719         struct regulator_dev *rdev = dev_get_drvdata(dev);
720
721         if (!rdev->constraints)
722                 return sprintf(buf, "constraint not defined\n");
723
724         return sprintf(buf, "%d\n", rdev->constraints->max_uA);
725 }
726 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
727
728 static ssize_t regulator_min_uV_show(struct device *dev,
729                                     struct device_attribute *attr, char *buf)
730 {
731         struct regulator_dev *rdev = dev_get_drvdata(dev);
732
733         if (!rdev->constraints)
734                 return sprintf(buf, "constraint not defined\n");
735
736         return sprintf(buf, "%d\n", rdev->constraints->min_uV);
737 }
738 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
739
740 static ssize_t regulator_max_uV_show(struct device *dev,
741                                     struct device_attribute *attr, char *buf)
742 {
743         struct regulator_dev *rdev = dev_get_drvdata(dev);
744
745         if (!rdev->constraints)
746                 return sprintf(buf, "constraint not defined\n");
747
748         return sprintf(buf, "%d\n", rdev->constraints->max_uV);
749 }
750 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
751
752 static ssize_t regulator_total_uA_show(struct device *dev,
753                                       struct device_attribute *attr, char *buf)
754 {
755         struct regulator_dev *rdev = dev_get_drvdata(dev);
756         struct regulator *regulator;
757         int uA = 0;
758
759         regulator_lock(rdev);
760         list_for_each_entry(regulator, &rdev->consumer_list, list) {
761                 if (regulator->enable_count)
762                         uA += regulator->uA_load;
763         }
764         regulator_unlock(rdev);
765         return sprintf(buf, "%d\n", uA);
766 }
767 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
768
769 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
770                               char *buf)
771 {
772         struct regulator_dev *rdev = dev_get_drvdata(dev);
773         return sprintf(buf, "%d\n", rdev->use_count);
774 }
775 static DEVICE_ATTR_RO(num_users);
776
777 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
778                          char *buf)
779 {
780         struct regulator_dev *rdev = dev_get_drvdata(dev);
781
782         switch (rdev->desc->type) {
783         case REGULATOR_VOLTAGE:
784                 return sprintf(buf, "voltage\n");
785         case REGULATOR_CURRENT:
786                 return sprintf(buf, "current\n");
787         }
788         return sprintf(buf, "unknown\n");
789 }
790 static DEVICE_ATTR_RO(type);
791
792 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
793                                 struct device_attribute *attr, char *buf)
794 {
795         struct regulator_dev *rdev = dev_get_drvdata(dev);
796
797         return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
798 }
799 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
800                 regulator_suspend_mem_uV_show, NULL);
801
802 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
803                                 struct device_attribute *attr, char *buf)
804 {
805         struct regulator_dev *rdev = dev_get_drvdata(dev);
806
807         return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
808 }
809 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
810                 regulator_suspend_disk_uV_show, NULL);
811
812 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
813                                 struct device_attribute *attr, char *buf)
814 {
815         struct regulator_dev *rdev = dev_get_drvdata(dev);
816
817         return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
818 }
819 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
820                 regulator_suspend_standby_uV_show, NULL);
821
822 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
823                                 struct device_attribute *attr, char *buf)
824 {
825         struct regulator_dev *rdev = dev_get_drvdata(dev);
826
827         return regulator_print_opmode(buf,
828                 rdev->constraints->state_mem.mode);
829 }
830 static DEVICE_ATTR(suspend_mem_mode, 0444,
831                 regulator_suspend_mem_mode_show, NULL);
832
833 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
834                                 struct device_attribute *attr, char *buf)
835 {
836         struct regulator_dev *rdev = dev_get_drvdata(dev);
837
838         return regulator_print_opmode(buf,
839                 rdev->constraints->state_disk.mode);
840 }
841 static DEVICE_ATTR(suspend_disk_mode, 0444,
842                 regulator_suspend_disk_mode_show, NULL);
843
844 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
845                                 struct device_attribute *attr, char *buf)
846 {
847         struct regulator_dev *rdev = dev_get_drvdata(dev);
848
849         return regulator_print_opmode(buf,
850                 rdev->constraints->state_standby.mode);
851 }
852 static DEVICE_ATTR(suspend_standby_mode, 0444,
853                 regulator_suspend_standby_mode_show, NULL);
854
855 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
856                                    struct device_attribute *attr, char *buf)
857 {
858         struct regulator_dev *rdev = dev_get_drvdata(dev);
859
860         return regulator_print_state(buf,
861                         rdev->constraints->state_mem.enabled);
862 }
863 static DEVICE_ATTR(suspend_mem_state, 0444,
864                 regulator_suspend_mem_state_show, NULL);
865
866 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
867                                    struct device_attribute *attr, char *buf)
868 {
869         struct regulator_dev *rdev = dev_get_drvdata(dev);
870
871         return regulator_print_state(buf,
872                         rdev->constraints->state_disk.enabled);
873 }
874 static DEVICE_ATTR(suspend_disk_state, 0444,
875                 regulator_suspend_disk_state_show, NULL);
876
877 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
878                                    struct device_attribute *attr, char *buf)
879 {
880         struct regulator_dev *rdev = dev_get_drvdata(dev);
881
882         return regulator_print_state(buf,
883                         rdev->constraints->state_standby.enabled);
884 }
885 static DEVICE_ATTR(suspend_standby_state, 0444,
886                 regulator_suspend_standby_state_show, NULL);
887
888 static ssize_t regulator_bypass_show(struct device *dev,
889                                      struct device_attribute *attr, char *buf)
890 {
891         struct regulator_dev *rdev = dev_get_drvdata(dev);
892         const char *report;
893         bool bypass;
894         int ret;
895
896         ret = rdev->desc->ops->get_bypass(rdev, &bypass);
897
898         if (ret != 0)
899                 report = "unknown";
900         else if (bypass)
901                 report = "enabled";
902         else
903                 report = "disabled";
904
905         return sprintf(buf, "%s\n", report);
906 }
907 static DEVICE_ATTR(bypass, 0444,
908                    regulator_bypass_show, NULL);
909
910 /* Calculate the new optimum regulator operating mode based on the new total
911  * consumer load. All locks held by caller */
912 static int drms_uA_update(struct regulator_dev *rdev)
913 {
914         struct regulator *sibling;
915         int current_uA = 0, output_uV, input_uV, err;
916         unsigned int mode;
917
918         /*
919          * first check to see if we can set modes at all, otherwise just
920          * tell the consumer everything is OK.
921          */
922         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
923                 rdev_dbg(rdev, "DRMS operation not allowed\n");
924                 return 0;
925         }
926
927         if (!rdev->desc->ops->get_optimum_mode &&
928             !rdev->desc->ops->set_load)
929                 return 0;
930
931         if (!rdev->desc->ops->set_mode &&
932             !rdev->desc->ops->set_load)
933                 return -EINVAL;
934
935         /* calc total requested load */
936         list_for_each_entry(sibling, &rdev->consumer_list, list) {
937                 if (sibling->enable_count)
938                         current_uA += sibling->uA_load;
939         }
940
941         current_uA += rdev->constraints->system_load;
942
943         if (rdev->desc->ops->set_load) {
944                 /* set the optimum mode for our new total regulator load */
945                 err = rdev->desc->ops->set_load(rdev, current_uA);
946                 if (err < 0)
947                         rdev_err(rdev, "failed to set load %d\n", current_uA);
948         } else {
949                 /* get output voltage */
950                 output_uV = regulator_get_voltage_rdev(rdev);
951                 if (output_uV <= 0) {
952                         rdev_err(rdev, "invalid output voltage found\n");
953                         return -EINVAL;
954                 }
955
956                 /* get input voltage */
957                 input_uV = 0;
958                 if (rdev->supply)
959                         input_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
960                 if (input_uV <= 0)
961                         input_uV = rdev->constraints->input_uV;
962                 if (input_uV <= 0) {
963                         rdev_err(rdev, "invalid input voltage found\n");
964                         return -EINVAL;
965                 }
966
967                 /* now get the optimum mode for our new total regulator load */
968                 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
969                                                          output_uV, current_uA);
970
971                 /* check the new mode is allowed */
972                 err = regulator_mode_constrain(rdev, &mode);
973                 if (err < 0) {
974                         rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
975                                  current_uA, input_uV, output_uV);
976                         return err;
977                 }
978
979                 err = rdev->desc->ops->set_mode(rdev, mode);
980                 if (err < 0)
981                         rdev_err(rdev, "failed to set optimum mode %x\n", mode);
982         }
983
984         return err;
985 }
986
987 static int suspend_set_state(struct regulator_dev *rdev,
988                                     suspend_state_t state)
989 {
990         int ret = 0;
991         struct regulator_state *rstate;
992
993         rstate = regulator_get_suspend_state(rdev, state);
994         if (rstate == NULL)
995                 return 0;
996
997         /* If we have no suspend mode configuration don't set anything;
998          * only warn if the driver implements set_suspend_voltage or
999          * set_suspend_mode callback.
1000          */
1001         if (rstate->enabled != ENABLE_IN_SUSPEND &&
1002             rstate->enabled != DISABLE_IN_SUSPEND) {
1003                 if (rdev->desc->ops->set_suspend_voltage ||
1004                     rdev->desc->ops->set_suspend_mode)
1005                         rdev_warn(rdev, "No configuration\n");
1006                 return 0;
1007         }
1008
1009         if (rstate->enabled == ENABLE_IN_SUSPEND &&
1010                 rdev->desc->ops->set_suspend_enable)
1011                 ret = rdev->desc->ops->set_suspend_enable(rdev);
1012         else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1013                 rdev->desc->ops->set_suspend_disable)
1014                 ret = rdev->desc->ops->set_suspend_disable(rdev);
1015         else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1016                 ret = 0;
1017
1018         if (ret < 0) {
1019                 rdev_err(rdev, "failed to enabled/disable\n");
1020                 return ret;
1021         }
1022
1023         if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1024                 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1025                 if (ret < 0) {
1026                         rdev_err(rdev, "failed to set voltage\n");
1027                         return ret;
1028                 }
1029         }
1030
1031         if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1032                 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1033                 if (ret < 0) {
1034                         rdev_err(rdev, "failed to set mode\n");
1035                         return ret;
1036                 }
1037         }
1038
1039         return ret;
1040 }
1041
1042 static void print_constraints(struct regulator_dev *rdev)
1043 {
1044         struct regulation_constraints *constraints = rdev->constraints;
1045         char buf[160] = "";
1046         size_t len = sizeof(buf) - 1;
1047         int count = 0;
1048         int ret;
1049
1050         if (constraints->min_uV && constraints->max_uV) {
1051                 if (constraints->min_uV == constraints->max_uV)
1052                         count += scnprintf(buf + count, len - count, "%d mV ",
1053                                            constraints->min_uV / 1000);
1054                 else
1055                         count += scnprintf(buf + count, len - count,
1056                                            "%d <--> %d mV ",
1057                                            constraints->min_uV / 1000,
1058                                            constraints->max_uV / 1000);
1059         }
1060
1061         if (!constraints->min_uV ||
1062             constraints->min_uV != constraints->max_uV) {
1063                 ret = regulator_get_voltage_rdev(rdev);
1064                 if (ret > 0)
1065                         count += scnprintf(buf + count, len - count,
1066                                            "at %d mV ", ret / 1000);
1067         }
1068
1069         if (constraints->uV_offset)
1070                 count += scnprintf(buf + count, len - count, "%dmV offset ",
1071                                    constraints->uV_offset / 1000);
1072
1073         if (constraints->min_uA && constraints->max_uA) {
1074                 if (constraints->min_uA == constraints->max_uA)
1075                         count += scnprintf(buf + count, len - count, "%d mA ",
1076                                            constraints->min_uA / 1000);
1077                 else
1078                         count += scnprintf(buf + count, len - count,
1079                                            "%d <--> %d mA ",
1080                                            constraints->min_uA / 1000,
1081                                            constraints->max_uA / 1000);
1082         }
1083
1084         if (!constraints->min_uA ||
1085             constraints->min_uA != constraints->max_uA) {
1086                 ret = _regulator_get_current_limit(rdev);
1087                 if (ret > 0)
1088                         count += scnprintf(buf + count, len - count,
1089                                            "at %d mA ", ret / 1000);
1090         }
1091
1092         if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1093                 count += scnprintf(buf + count, len - count, "fast ");
1094         if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1095                 count += scnprintf(buf + count, len - count, "normal ");
1096         if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1097                 count += scnprintf(buf + count, len - count, "idle ");
1098         if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1099                 count += scnprintf(buf + count, len - count, "standby");
1100
1101         if (!count)
1102                 scnprintf(buf, len, "no parameters");
1103
1104         rdev_dbg(rdev, "%s\n", buf);
1105
1106         if ((constraints->min_uV != constraints->max_uV) &&
1107             !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1108                 rdev_warn(rdev,
1109                           "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1110 }
1111
1112 static int machine_constraints_voltage(struct regulator_dev *rdev,
1113         struct regulation_constraints *constraints)
1114 {
1115         const struct regulator_ops *ops = rdev->desc->ops;
1116         int ret;
1117
1118         /* do we need to apply the constraint voltage */
1119         if (rdev->constraints->apply_uV &&
1120             rdev->constraints->min_uV && rdev->constraints->max_uV) {
1121                 int target_min, target_max;
1122                 int current_uV = regulator_get_voltage_rdev(rdev);
1123
1124                 if (current_uV == -ENOTRECOVERABLE) {
1125                         /* This regulator can't be read and must be initialized */
1126                         rdev_info(rdev, "Setting %d-%duV\n",
1127                                   rdev->constraints->min_uV,
1128                                   rdev->constraints->max_uV);
1129                         _regulator_do_set_voltage(rdev,
1130                                                   rdev->constraints->min_uV,
1131                                                   rdev->constraints->max_uV);
1132                         current_uV = regulator_get_voltage_rdev(rdev);
1133                 }
1134
1135                 if (current_uV < 0) {
1136                         rdev_err(rdev,
1137                                  "failed to get the current voltage(%d)\n",
1138                                  current_uV);
1139                         return current_uV;
1140                 }
1141
1142                 /*
1143                  * If we're below the minimum voltage move up to the
1144                  * minimum voltage, if we're above the maximum voltage
1145                  * then move down to the maximum.
1146                  */
1147                 target_min = current_uV;
1148                 target_max = current_uV;
1149
1150                 if (current_uV < rdev->constraints->min_uV) {
1151                         target_min = rdev->constraints->min_uV;
1152                         target_max = rdev->constraints->min_uV;
1153                 }
1154
1155                 if (current_uV > rdev->constraints->max_uV) {
1156                         target_min = rdev->constraints->max_uV;
1157                         target_max = rdev->constraints->max_uV;
1158                 }
1159
1160                 if (target_min != current_uV || target_max != current_uV) {
1161                         rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1162                                   current_uV, target_min, target_max);
1163                         ret = _regulator_do_set_voltage(
1164                                 rdev, target_min, target_max);
1165                         if (ret < 0) {
1166                                 rdev_err(rdev,
1167                                         "failed to apply %d-%duV constraint(%d)\n",
1168                                         target_min, target_max, ret);
1169                                 return ret;
1170                         }
1171                 }
1172         }
1173
1174         /* constrain machine-level voltage specs to fit
1175          * the actual range supported by this regulator.
1176          */
1177         if (ops->list_voltage && rdev->desc->n_voltages) {
1178                 int     count = rdev->desc->n_voltages;
1179                 int     i;
1180                 int     min_uV = INT_MAX;
1181                 int     max_uV = INT_MIN;
1182                 int     cmin = constraints->min_uV;
1183                 int     cmax = constraints->max_uV;
1184
1185                 /* it's safe to autoconfigure fixed-voltage supplies
1186                    and the constraints are used by list_voltage. */
1187                 if (count == 1 && !cmin) {
1188                         cmin = 1;
1189                         cmax = INT_MAX;
1190                         constraints->min_uV = cmin;
1191                         constraints->max_uV = cmax;
1192                 }
1193
1194                 /* voltage constraints are optional */
1195                 if ((cmin == 0) && (cmax == 0))
1196                         return 0;
1197
1198                 /* else require explicit machine-level constraints */
1199                 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1200                         rdev_err(rdev, "invalid voltage constraints\n");
1201                         return -EINVAL;
1202                 }
1203
1204                 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1205                 for (i = 0; i < count; i++) {
1206                         int     value;
1207
1208                         value = ops->list_voltage(rdev, i);
1209                         if (value <= 0)
1210                                 continue;
1211
1212                         /* maybe adjust [min_uV..max_uV] */
1213                         if (value >= cmin && value < min_uV)
1214                                 min_uV = value;
1215                         if (value <= cmax && value > max_uV)
1216                                 max_uV = value;
1217                 }
1218
1219                 /* final: [min_uV..max_uV] valid iff constraints valid */
1220                 if (max_uV < min_uV) {
1221                         rdev_err(rdev,
1222                                  "unsupportable voltage constraints %u-%uuV\n",
1223                                  min_uV, max_uV);
1224                         return -EINVAL;
1225                 }
1226
1227                 /* use regulator's subset of machine constraints */
1228                 if (constraints->min_uV < min_uV) {
1229                         rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1230                                  constraints->min_uV, min_uV);
1231                         constraints->min_uV = min_uV;
1232                 }
1233                 if (constraints->max_uV > max_uV) {
1234                         rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1235                                  constraints->max_uV, max_uV);
1236                         constraints->max_uV = max_uV;
1237                 }
1238         }
1239
1240         return 0;
1241 }
1242
1243 static int machine_constraints_current(struct regulator_dev *rdev,
1244         struct regulation_constraints *constraints)
1245 {
1246         const struct regulator_ops *ops = rdev->desc->ops;
1247         int ret;
1248
1249         if (!constraints->min_uA && !constraints->max_uA)
1250                 return 0;
1251
1252         if (constraints->min_uA > constraints->max_uA) {
1253                 rdev_err(rdev, "Invalid current constraints\n");
1254                 return -EINVAL;
1255         }
1256
1257         if (!ops->set_current_limit || !ops->get_current_limit) {
1258                 rdev_warn(rdev, "Operation of current configuration missing\n");
1259                 return 0;
1260         }
1261
1262         /* Set regulator current in constraints range */
1263         ret = ops->set_current_limit(rdev, constraints->min_uA,
1264                         constraints->max_uA);
1265         if (ret < 0) {
1266                 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1267                 return ret;
1268         }
1269
1270         return 0;
1271 }
1272
1273 static int _regulator_do_enable(struct regulator_dev *rdev);
1274
1275 /**
1276  * set_machine_constraints - sets regulator constraints
1277  * @rdev: regulator source
1278  *
1279  * Allows platform initialisation code to define and constrain
1280  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
1281  * Constraints *must* be set by platform code in order for some
1282  * regulator operations to proceed i.e. set_voltage, set_current_limit,
1283  * set_mode.
1284  */
1285 static int set_machine_constraints(struct regulator_dev *rdev)
1286 {
1287         int ret = 0;
1288         const struct regulator_ops *ops = rdev->desc->ops;
1289
1290         ret = machine_constraints_voltage(rdev, rdev->constraints);
1291         if (ret != 0)
1292                 return ret;
1293
1294         ret = machine_constraints_current(rdev, rdev->constraints);
1295         if (ret != 0)
1296                 return ret;
1297
1298         if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1299                 ret = ops->set_input_current_limit(rdev,
1300                                                    rdev->constraints->ilim_uA);
1301                 if (ret < 0) {
1302                         rdev_err(rdev, "failed to set input limit\n");
1303                         return ret;
1304                 }
1305         }
1306
1307         /* do we need to setup our suspend state */
1308         if (rdev->constraints->initial_state) {
1309                 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1310                 if (ret < 0) {
1311                         rdev_err(rdev, "failed to set suspend state\n");
1312                         return ret;
1313                 }
1314         }
1315
1316         if (rdev->constraints->initial_mode) {
1317                 if (!ops->set_mode) {
1318                         rdev_err(rdev, "no set_mode operation\n");
1319                         return -EINVAL;
1320                 }
1321
1322                 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1323                 if (ret < 0) {
1324                         rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1325                         return ret;
1326                 }
1327         } else if (rdev->constraints->system_load) {
1328                 /*
1329                  * We'll only apply the initial system load if an
1330                  * initial mode wasn't specified.
1331                  */
1332                 drms_uA_update(rdev);
1333         }
1334
1335         if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1336                 && ops->set_ramp_delay) {
1337                 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1338                 if (ret < 0) {
1339                         rdev_err(rdev, "failed to set ramp_delay\n");
1340                         return ret;
1341                 }
1342         }
1343
1344         if (rdev->constraints->pull_down && ops->set_pull_down) {
1345                 ret = ops->set_pull_down(rdev);
1346                 if (ret < 0) {
1347                         rdev_err(rdev, "failed to set pull down\n");
1348                         return ret;
1349                 }
1350         }
1351
1352         if (rdev->constraints->soft_start && ops->set_soft_start) {
1353                 ret = ops->set_soft_start(rdev);
1354                 if (ret < 0) {
1355                         rdev_err(rdev, "failed to set soft start\n");
1356                         return ret;
1357                 }
1358         }
1359
1360         if (rdev->constraints->over_current_protection
1361                 && ops->set_over_current_protection) {
1362                 ret = ops->set_over_current_protection(rdev);
1363                 if (ret < 0) {
1364                         rdev_err(rdev, "failed to set over current protection\n");
1365                         return ret;
1366                 }
1367         }
1368
1369         if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1370                 bool ad_state = (rdev->constraints->active_discharge ==
1371                               REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1372
1373                 ret = ops->set_active_discharge(rdev, ad_state);
1374                 if (ret < 0) {
1375                         rdev_err(rdev, "failed to set active discharge\n");
1376                         return ret;
1377                 }
1378         }
1379
1380         /* If the constraints say the regulator should be on at this point
1381          * and we have control then make sure it is enabled.
1382          */
1383         if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1384                 /* If we want to enable this regulator, make sure that we know
1385                  * the supplying regulator.
1386                  */
1387                 if (rdev->supply_name && !rdev->supply)
1388                         return -EPROBE_DEFER;
1389
1390                 /* If supplying regulator has already been enabled,
1391                  * it's not intended to have use_count increment
1392                  * when rdev is only boot-on.
1393                  */
1394                 if (rdev->supply &&
1395                     (rdev->constraints->always_on ||
1396                      !regulator_is_enabled(rdev->supply))) {
1397                         ret = regulator_enable(rdev->supply);
1398                         if (ret < 0) {
1399                                 _regulator_put(rdev->supply);
1400                                 rdev->supply = NULL;
1401                                 return ret;
1402                         }
1403                 }
1404
1405                 ret = _regulator_do_enable(rdev);
1406                 if (ret < 0 && ret != -EINVAL) {
1407                         rdev_err(rdev, "failed to enable\n");
1408                         return ret;
1409                 }
1410
1411                 if (rdev->constraints->always_on)
1412                         rdev->use_count++;
1413         }
1414
1415         print_constraints(rdev);
1416         return 0;
1417 }
1418
1419 /**
1420  * set_supply - set regulator supply regulator
1421  * @rdev: regulator name
1422  * @supply_rdev: supply regulator name
1423  *
1424  * Called by platform initialisation code to set the supply regulator for this
1425  * regulator. This ensures that a regulators supply will also be enabled by the
1426  * core if it's child is enabled.
1427  */
1428 static int set_supply(struct regulator_dev *rdev,
1429                       struct regulator_dev *supply_rdev)
1430 {
1431         int err;
1432
1433         rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1434
1435         if (!try_module_get(supply_rdev->owner))
1436                 return -ENODEV;
1437
1438         rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1439         if (rdev->supply == NULL) {
1440                 module_put(supply_rdev->owner);
1441                 err = -ENOMEM;
1442                 return err;
1443         }
1444         supply_rdev->open_count++;
1445
1446         return 0;
1447 }
1448
1449 /**
1450  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1451  * @rdev:         regulator source
1452  * @consumer_dev_name: dev_name() string for device supply applies to
1453  * @supply:       symbolic name for supply
1454  *
1455  * Allows platform initialisation code to map physical regulator
1456  * sources to symbolic names for supplies for use by devices.  Devices
1457  * should use these symbolic names to request regulators, avoiding the
1458  * need to provide board-specific regulator names as platform data.
1459  */
1460 static int set_consumer_device_supply(struct regulator_dev *rdev,
1461                                       const char *consumer_dev_name,
1462                                       const char *supply)
1463 {
1464         struct regulator_map *node, *new_node;
1465         int has_dev;
1466
1467         if (supply == NULL)
1468                 return -EINVAL;
1469
1470         if (consumer_dev_name != NULL)
1471                 has_dev = 1;
1472         else
1473                 has_dev = 0;
1474
1475         new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1476         if (new_node == NULL)
1477                 return -ENOMEM;
1478
1479         new_node->regulator = rdev;
1480         new_node->supply = supply;
1481
1482         if (has_dev) {
1483                 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1484                 if (new_node->dev_name == NULL) {
1485                         kfree(new_node);
1486                         return -ENOMEM;
1487                 }
1488         }
1489
1490         mutex_lock(&regulator_list_mutex);
1491         list_for_each_entry(node, &regulator_map_list, list) {
1492                 if (node->dev_name && consumer_dev_name) {
1493                         if (strcmp(node->dev_name, consumer_dev_name) != 0)
1494                                 continue;
1495                 } else if (node->dev_name || consumer_dev_name) {
1496                         continue;
1497                 }
1498
1499                 if (strcmp(node->supply, supply) != 0)
1500                         continue;
1501
1502                 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1503                          consumer_dev_name,
1504                          dev_name(&node->regulator->dev),
1505                          node->regulator->desc->name,
1506                          supply,
1507                          dev_name(&rdev->dev), rdev_get_name(rdev));
1508                 goto fail;
1509         }
1510
1511         list_add(&new_node->list, &regulator_map_list);
1512         mutex_unlock(&regulator_list_mutex);
1513
1514         return 0;
1515
1516 fail:
1517         mutex_unlock(&regulator_list_mutex);
1518         kfree(new_node->dev_name);
1519         kfree(new_node);
1520         return -EBUSY;
1521 }
1522
1523 static void unset_regulator_supplies(struct regulator_dev *rdev)
1524 {
1525         struct regulator_map *node, *n;
1526
1527         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1528                 if (rdev == node->regulator) {
1529                         list_del(&node->list);
1530                         kfree(node->dev_name);
1531                         kfree(node);
1532                 }
1533         }
1534 }
1535
1536 #ifdef CONFIG_DEBUG_FS
1537 static ssize_t constraint_flags_read_file(struct file *file,
1538                                           char __user *user_buf,
1539                                           size_t count, loff_t *ppos)
1540 {
1541         const struct regulator *regulator = file->private_data;
1542         const struct regulation_constraints *c = regulator->rdev->constraints;
1543         char *buf;
1544         ssize_t ret;
1545
1546         if (!c)
1547                 return 0;
1548
1549         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1550         if (!buf)
1551                 return -ENOMEM;
1552
1553         ret = snprintf(buf, PAGE_SIZE,
1554                         "always_on: %u\n"
1555                         "boot_on: %u\n"
1556                         "apply_uV: %u\n"
1557                         "ramp_disable: %u\n"
1558                         "soft_start: %u\n"
1559                         "pull_down: %u\n"
1560                         "over_current_protection: %u\n",
1561                         c->always_on,
1562                         c->boot_on,
1563                         c->apply_uV,
1564                         c->ramp_disable,
1565                         c->soft_start,
1566                         c->pull_down,
1567                         c->over_current_protection);
1568
1569         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1570         kfree(buf);
1571
1572         return ret;
1573 }
1574
1575 #endif
1576
1577 static const struct file_operations constraint_flags_fops = {
1578 #ifdef CONFIG_DEBUG_FS
1579         .open = simple_open,
1580         .read = constraint_flags_read_file,
1581         .llseek = default_llseek,
1582 #endif
1583 };
1584
1585 #define REG_STR_SIZE    64
1586
1587 static struct regulator *create_regulator(struct regulator_dev *rdev,
1588                                           struct device *dev,
1589                                           const char *supply_name)
1590 {
1591         struct regulator *regulator;
1592         int err = 0;
1593
1594         if (dev) {
1595                 char buf[REG_STR_SIZE];
1596                 int size;
1597
1598                 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1599                                 dev->kobj.name, supply_name);
1600                 if (size >= REG_STR_SIZE)
1601                         return NULL;
1602
1603                 supply_name = kstrdup(buf, GFP_KERNEL);
1604                 if (supply_name == NULL)
1605                         return NULL;
1606         } else {
1607                 supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1608                 if (supply_name == NULL)
1609                         return NULL;
1610         }
1611
1612         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1613         if (regulator == NULL) {
1614                 kfree_const(supply_name);
1615                 return NULL;
1616         }
1617
1618         regulator->rdev = rdev;
1619         regulator->supply_name = supply_name;
1620
1621         regulator_lock(rdev);
1622         list_add(&regulator->list, &rdev->consumer_list);
1623         regulator_unlock(rdev);
1624
1625         if (dev) {
1626                 regulator->dev = dev;
1627
1628                 /* Add a link to the device sysfs entry */
1629                 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1630                                                supply_name);
1631                 if (err) {
1632                         rdev_dbg(rdev, "could not add device link %s err %d\n",
1633                                   dev->kobj.name, err);
1634                         /* non-fatal */
1635                 }
1636         }
1637
1638         if (err != -EEXIST)
1639                 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs);
1640         if (!regulator->debugfs) {
1641                 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1642         } else {
1643                 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1644                                    &regulator->uA_load);
1645                 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1646                                    &regulator->voltage[PM_SUSPEND_ON].min_uV);
1647                 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1648                                    &regulator->voltage[PM_SUSPEND_ON].max_uV);
1649                 debugfs_create_file("constraint_flags", 0444,
1650                                     regulator->debugfs, regulator,
1651                                     &constraint_flags_fops);
1652         }
1653
1654         /*
1655          * Check now if the regulator is an always on regulator - if
1656          * it is then we don't need to do nearly so much work for
1657          * enable/disable calls.
1658          */
1659         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1660             _regulator_is_enabled(rdev))
1661                 regulator->always_on = true;
1662
1663         return regulator;
1664 }
1665
1666 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1667 {
1668         if (rdev->constraints && rdev->constraints->enable_time)
1669                 return rdev->constraints->enable_time;
1670         if (rdev->desc->ops->enable_time)
1671                 return rdev->desc->ops->enable_time(rdev);
1672         return rdev->desc->enable_time;
1673 }
1674
1675 static struct regulator_supply_alias *regulator_find_supply_alias(
1676                 struct device *dev, const char *supply)
1677 {
1678         struct regulator_supply_alias *map;
1679
1680         list_for_each_entry(map, &regulator_supply_alias_list, list)
1681                 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1682                         return map;
1683
1684         return NULL;
1685 }
1686
1687 static void regulator_supply_alias(struct device **dev, const char **supply)
1688 {
1689         struct regulator_supply_alias *map;
1690
1691         map = regulator_find_supply_alias(*dev, *supply);
1692         if (map) {
1693                 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1694                                 *supply, map->alias_supply,
1695                                 dev_name(map->alias_dev));
1696                 *dev = map->alias_dev;
1697                 *supply = map->alias_supply;
1698         }
1699 }
1700
1701 static int regulator_match(struct device *dev, const void *data)
1702 {
1703         struct regulator_dev *r = dev_to_rdev(dev);
1704
1705         return strcmp(rdev_get_name(r), data) == 0;
1706 }
1707
1708 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1709 {
1710         struct device *dev;
1711
1712         dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1713
1714         return dev ? dev_to_rdev(dev) : NULL;
1715 }
1716
1717 /**
1718  * regulator_dev_lookup - lookup a regulator device.
1719  * @dev: device for regulator "consumer".
1720  * @supply: Supply name or regulator ID.
1721  *
1722  * If successful, returns a struct regulator_dev that corresponds to the name
1723  * @supply and with the embedded struct device refcount incremented by one.
1724  * The refcount must be dropped by calling put_device().
1725  * On failure one of the following ERR-PTR-encoded values is returned:
1726  * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1727  * in the future.
1728  */
1729 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1730                                                   const char *supply)
1731 {
1732         struct regulator_dev *r = NULL;
1733         struct device_node *node;
1734         struct regulator_map *map;
1735         const char *devname = NULL;
1736
1737         regulator_supply_alias(&dev, &supply);
1738
1739         /* first do a dt based lookup */
1740         if (dev && dev->of_node) {
1741                 node = of_get_regulator(dev, supply);
1742                 if (node) {
1743                         r = of_find_regulator_by_node(node);
1744                         of_node_put(node);
1745                         if (r)
1746                                 return r;
1747
1748                         /*
1749                          * We have a node, but there is no device.
1750                          * assume it has not registered yet.
1751                          */
1752                         return ERR_PTR(-EPROBE_DEFER);
1753                 }
1754         }
1755
1756         /* if not found, try doing it non-dt way */
1757         if (dev)
1758                 devname = dev_name(dev);
1759
1760         mutex_lock(&regulator_list_mutex);
1761         list_for_each_entry(map, &regulator_map_list, list) {
1762                 /* If the mapping has a device set up it must match */
1763                 if (map->dev_name &&
1764                     (!devname || strcmp(map->dev_name, devname)))
1765                         continue;
1766
1767                 if (strcmp(map->supply, supply) == 0 &&
1768                     get_device(&map->regulator->dev)) {
1769                         r = map->regulator;
1770                         break;
1771                 }
1772         }
1773         mutex_unlock(&regulator_list_mutex);
1774
1775         if (r)
1776                 return r;
1777
1778         r = regulator_lookup_by_name(supply);
1779         if (r)
1780                 return r;
1781
1782         return ERR_PTR(-ENODEV);
1783 }
1784
1785 static int regulator_resolve_supply(struct regulator_dev *rdev)
1786 {
1787         struct regulator_dev *r;
1788         struct device *dev = rdev->dev.parent;
1789         int ret = 0;
1790
1791         /* No supply to resolve? */
1792         if (!rdev->supply_name)
1793                 return 0;
1794
1795         /* Supply already resolved? (fast-path without locking contention) */
1796         if (rdev->supply)
1797                 return 0;
1798
1799         r = regulator_dev_lookup(dev, rdev->supply_name);
1800         if (IS_ERR(r)) {
1801                 ret = PTR_ERR(r);
1802
1803                 /* Did the lookup explicitly defer for us? */
1804                 if (ret == -EPROBE_DEFER)
1805                         goto out;
1806
1807                 if (have_full_constraints()) {
1808                         r = dummy_regulator_rdev;
1809                         get_device(&r->dev);
1810                 } else {
1811                         dev_err(dev, "Failed to resolve %s-supply for %s\n",
1812                                 rdev->supply_name, rdev->desc->name);
1813                         ret = -EPROBE_DEFER;
1814                         goto out;
1815                 }
1816         }
1817
1818         if (r == rdev) {
1819                 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
1820                         rdev->desc->name, rdev->supply_name);
1821                 if (!have_full_constraints()) {
1822                         ret = -EINVAL;
1823                         goto out;
1824                 }
1825                 r = dummy_regulator_rdev;
1826                 get_device(&r->dev);
1827         }
1828
1829         /*
1830          * If the supply's parent device is not the same as the
1831          * regulator's parent device, then ensure the parent device
1832          * is bound before we resolve the supply, in case the parent
1833          * device get probe deferred and unregisters the supply.
1834          */
1835         if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1836                 if (!device_is_bound(r->dev.parent)) {
1837                         put_device(&r->dev);
1838                         ret = -EPROBE_DEFER;
1839                         goto out;
1840                 }
1841         }
1842
1843         /* Recursively resolve the supply of the supply */
1844         ret = regulator_resolve_supply(r);
1845         if (ret < 0) {
1846                 put_device(&r->dev);
1847                 goto out;
1848         }
1849
1850         /*
1851          * Recheck rdev->supply with rdev->mutex lock held to avoid a race
1852          * between rdev->supply null check and setting rdev->supply in
1853          * set_supply() from concurrent tasks.
1854          */
1855         regulator_lock(rdev);
1856
1857         /* Supply just resolved by a concurrent task? */
1858         if (rdev->supply) {
1859                 regulator_unlock(rdev);
1860                 put_device(&r->dev);
1861                 goto out;
1862         }
1863
1864         ret = set_supply(rdev, r);
1865         if (ret < 0) {
1866                 regulator_unlock(rdev);
1867                 put_device(&r->dev);
1868                 goto out;
1869         }
1870
1871         regulator_unlock(rdev);
1872
1873         /*
1874          * In set_machine_constraints() we may have turned this regulator on
1875          * but we couldn't propagate to the supply if it hadn't been resolved
1876          * yet.  Do it now.
1877          */
1878         if (rdev->use_count) {
1879                 ret = regulator_enable(rdev->supply);
1880                 if (ret < 0) {
1881                         _regulator_put(rdev->supply);
1882                         rdev->supply = NULL;
1883                         goto out;
1884                 }
1885         }
1886
1887 out:
1888         return ret;
1889 }
1890
1891 /* Internal regulator request function */
1892 struct regulator *_regulator_get(struct device *dev, const char *id,
1893                                  enum regulator_get_type get_type)
1894 {
1895         struct regulator_dev *rdev;
1896         struct regulator *regulator;
1897         const char *devname = dev ? dev_name(dev) : "deviceless";
1898         int ret;
1899
1900         if (get_type >= MAX_GET_TYPE) {
1901                 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1902                 return ERR_PTR(-EINVAL);
1903         }
1904
1905         if (id == NULL) {
1906                 pr_err("get() with no identifier\n");
1907                 return ERR_PTR(-EINVAL);
1908         }
1909
1910         rdev = regulator_dev_lookup(dev, id);
1911         if (IS_ERR(rdev)) {
1912                 ret = PTR_ERR(rdev);
1913
1914                 /*
1915                  * If regulator_dev_lookup() fails with error other
1916                  * than -ENODEV our job here is done, we simply return it.
1917                  */
1918                 if (ret != -ENODEV)
1919                         return ERR_PTR(ret);
1920
1921                 if (!have_full_constraints()) {
1922                         dev_warn(dev,
1923                                  "incomplete constraints, dummy supplies not allowed\n");
1924                         return ERR_PTR(-ENODEV);
1925                 }
1926
1927                 switch (get_type) {
1928                 case NORMAL_GET:
1929                         /*
1930                          * Assume that a regulator is physically present and
1931                          * enabled, even if it isn't hooked up, and just
1932                          * provide a dummy.
1933                          */
1934                         dev_warn(dev,
1935                                  "%s supply %s not found, using dummy regulator\n",
1936                                  devname, id);
1937                         rdev = dummy_regulator_rdev;
1938                         get_device(&rdev->dev);
1939                         break;
1940
1941                 case EXCLUSIVE_GET:
1942                         dev_warn(dev,
1943                                  "dummy supplies not allowed for exclusive requests\n");
1944                         /* fall through */
1945
1946                 default:
1947                         return ERR_PTR(-ENODEV);
1948                 }
1949         }
1950
1951         if (rdev->exclusive) {
1952                 regulator = ERR_PTR(-EPERM);
1953                 put_device(&rdev->dev);
1954                 return regulator;
1955         }
1956
1957         if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1958                 regulator = ERR_PTR(-EBUSY);
1959                 put_device(&rdev->dev);
1960                 return regulator;
1961         }
1962
1963         mutex_lock(&regulator_list_mutex);
1964         ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1965         mutex_unlock(&regulator_list_mutex);
1966
1967         if (ret != 0) {
1968                 regulator = ERR_PTR(-EPROBE_DEFER);
1969                 put_device(&rdev->dev);
1970                 return regulator;
1971         }
1972
1973         ret = regulator_resolve_supply(rdev);
1974         if (ret < 0) {
1975                 regulator = ERR_PTR(ret);
1976                 put_device(&rdev->dev);
1977                 return regulator;
1978         }
1979
1980         if (!try_module_get(rdev->owner)) {
1981                 regulator = ERR_PTR(-EPROBE_DEFER);
1982                 put_device(&rdev->dev);
1983                 return regulator;
1984         }
1985
1986         regulator = create_regulator(rdev, dev, id);
1987         if (regulator == NULL) {
1988                 regulator = ERR_PTR(-ENOMEM);
1989                 module_put(rdev->owner);
1990                 put_device(&rdev->dev);
1991                 return regulator;
1992         }
1993
1994         rdev->open_count++;
1995         if (get_type == EXCLUSIVE_GET) {
1996                 rdev->exclusive = 1;
1997
1998                 ret = _regulator_is_enabled(rdev);
1999                 if (ret > 0) {
2000                         rdev->use_count = 1;
2001                         regulator->enable_count = 1;
2002                 } else {
2003                         rdev->use_count = 0;
2004                         regulator->enable_count = 0;
2005                 }
2006         }
2007
2008         device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
2009
2010         return regulator;
2011 }
2012
2013 /**
2014  * regulator_get - lookup and obtain a reference to a regulator.
2015  * @dev: device for regulator "consumer"
2016  * @id: Supply name or regulator ID.
2017  *
2018  * Returns a struct regulator corresponding to the regulator producer,
2019  * or IS_ERR() condition containing errno.
2020  *
2021  * Use of supply names configured via regulator_set_device_supply() is
2022  * strongly encouraged.  It is recommended that the supply name used
2023  * should match the name used for the supply and/or the relevant
2024  * device pins in the datasheet.
2025  */
2026 struct regulator *regulator_get(struct device *dev, const char *id)
2027 {
2028         return _regulator_get(dev, id, NORMAL_GET);
2029 }
2030 EXPORT_SYMBOL_GPL(regulator_get);
2031
2032 /**
2033  * regulator_get_exclusive - obtain exclusive access to a regulator.
2034  * @dev: device for regulator "consumer"
2035  * @id: Supply name or regulator ID.
2036  *
2037  * Returns a struct regulator corresponding to the regulator producer,
2038  * or IS_ERR() condition containing errno.  Other consumers will be
2039  * unable to obtain this regulator while this reference is held and the
2040  * use count for the regulator will be initialised to reflect the current
2041  * state of the regulator.
2042  *
2043  * This is intended for use by consumers which cannot tolerate shared
2044  * use of the regulator such as those which need to force the
2045  * regulator off for correct operation of the hardware they are
2046  * controlling.
2047  *
2048  * Use of supply names configured via regulator_set_device_supply() is
2049  * strongly encouraged.  It is recommended that the supply name used
2050  * should match the name used for the supply and/or the relevant
2051  * device pins in the datasheet.
2052  */
2053 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2054 {
2055         return _regulator_get(dev, id, EXCLUSIVE_GET);
2056 }
2057 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2058
2059 /**
2060  * regulator_get_optional - obtain optional access to a regulator.
2061  * @dev: device for regulator "consumer"
2062  * @id: Supply name or regulator ID.
2063  *
2064  * Returns a struct regulator corresponding to the regulator producer,
2065  * or IS_ERR() condition containing errno.
2066  *
2067  * This is intended for use by consumers for devices which can have
2068  * some supplies unconnected in normal use, such as some MMC devices.
2069  * It can allow the regulator core to provide stub supplies for other
2070  * supplies requested using normal regulator_get() calls without
2071  * disrupting the operation of drivers that can handle absent
2072  * supplies.
2073  *
2074  * Use of supply names configured via regulator_set_device_supply() is
2075  * strongly encouraged.  It is recommended that the supply name used
2076  * should match the name used for the supply and/or the relevant
2077  * device pins in the datasheet.
2078  */
2079 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2080 {
2081         return _regulator_get(dev, id, OPTIONAL_GET);
2082 }
2083 EXPORT_SYMBOL_GPL(regulator_get_optional);
2084
2085 /* regulator_list_mutex lock held by regulator_put() */
2086 static void _regulator_put(struct regulator *regulator)
2087 {
2088         struct regulator_dev *rdev;
2089
2090         if (IS_ERR_OR_NULL(regulator))
2091                 return;
2092
2093         lockdep_assert_held_once(&regulator_list_mutex);
2094
2095         /* Docs say you must disable before calling regulator_put() */
2096         WARN_ON(regulator->enable_count);
2097
2098         rdev = regulator->rdev;
2099
2100         debugfs_remove_recursive(regulator->debugfs);
2101
2102         if (regulator->dev) {
2103                 device_link_remove(regulator->dev, &rdev->dev);
2104
2105                 /* remove any sysfs entries */
2106                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2107         }
2108
2109         regulator_lock(rdev);
2110         list_del(&regulator->list);
2111
2112         rdev->open_count--;
2113         rdev->exclusive = 0;
2114         regulator_unlock(rdev);
2115
2116         kfree_const(regulator->supply_name);
2117         kfree(regulator);
2118
2119         module_put(rdev->owner);
2120         put_device(&rdev->dev);
2121 }
2122
2123 /**
2124  * regulator_put - "free" the regulator source
2125  * @regulator: regulator source
2126  *
2127  * Note: drivers must ensure that all regulator_enable calls made on this
2128  * regulator source are balanced by regulator_disable calls prior to calling
2129  * this function.
2130  */
2131 void regulator_put(struct regulator *regulator)
2132 {
2133         mutex_lock(&regulator_list_mutex);
2134         _regulator_put(regulator);
2135         mutex_unlock(&regulator_list_mutex);
2136 }
2137 EXPORT_SYMBOL_GPL(regulator_put);
2138
2139 /**
2140  * regulator_register_supply_alias - Provide device alias for supply lookup
2141  *
2142  * @dev: device that will be given as the regulator "consumer"
2143  * @id: Supply name or regulator ID
2144  * @alias_dev: device that should be used to lookup the supply
2145  * @alias_id: Supply name or regulator ID that should be used to lookup the
2146  * supply
2147  *
2148  * All lookups for id on dev will instead be conducted for alias_id on
2149  * alias_dev.
2150  */
2151 int regulator_register_supply_alias(struct device *dev, const char *id,
2152                                     struct device *alias_dev,
2153                                     const char *alias_id)
2154 {
2155         struct regulator_supply_alias *map;
2156
2157         map = regulator_find_supply_alias(dev, id);
2158         if (map)
2159                 return -EEXIST;
2160
2161         map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2162         if (!map)
2163                 return -ENOMEM;
2164
2165         map->src_dev = dev;
2166         map->src_supply = id;
2167         map->alias_dev = alias_dev;
2168         map->alias_supply = alias_id;
2169
2170         list_add(&map->list, &regulator_supply_alias_list);
2171
2172         pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2173                 id, dev_name(dev), alias_id, dev_name(alias_dev));
2174
2175         return 0;
2176 }
2177 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2178
2179 /**
2180  * regulator_unregister_supply_alias - Remove device alias
2181  *
2182  * @dev: device that will be given as the regulator "consumer"
2183  * @id: Supply name or regulator ID
2184  *
2185  * Remove a lookup alias if one exists for id on dev.
2186  */
2187 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2188 {
2189         struct regulator_supply_alias *map;
2190
2191         map = regulator_find_supply_alias(dev, id);
2192         if (map) {
2193                 list_del(&map->list);
2194                 kfree(map);
2195         }
2196 }
2197 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2198
2199 /**
2200  * regulator_bulk_register_supply_alias - register multiple aliases
2201  *
2202  * @dev: device that will be given as the regulator "consumer"
2203  * @id: List of supply names or regulator IDs
2204  * @alias_dev: device that should be used to lookup the supply
2205  * @alias_id: List of supply names or regulator IDs that should be used to
2206  * lookup the supply
2207  * @num_id: Number of aliases to register
2208  *
2209  * @return 0 on success, an errno on failure.
2210  *
2211  * This helper function allows drivers to register several supply
2212  * aliases in one operation.  If any of the aliases cannot be
2213  * registered any aliases that were registered will be removed
2214  * before returning to the caller.
2215  */
2216 int regulator_bulk_register_supply_alias(struct device *dev,
2217                                          const char *const *id,
2218                                          struct device *alias_dev,
2219                                          const char *const *alias_id,
2220                                          int num_id)
2221 {
2222         int i;
2223         int ret;
2224
2225         for (i = 0; i < num_id; ++i) {
2226                 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2227                                                       alias_id[i]);
2228                 if (ret < 0)
2229                         goto err;
2230         }
2231
2232         return 0;
2233
2234 err:
2235         dev_err(dev,
2236                 "Failed to create supply alias %s,%s -> %s,%s\n",
2237                 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2238
2239         while (--i >= 0)
2240                 regulator_unregister_supply_alias(dev, id[i]);
2241
2242         return ret;
2243 }
2244 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2245
2246 /**
2247  * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2248  *
2249  * @dev: device that will be given as the regulator "consumer"
2250  * @id: List of supply names or regulator IDs
2251  * @num_id: Number of aliases to unregister
2252  *
2253  * This helper function allows drivers to unregister several supply
2254  * aliases in one operation.
2255  */
2256 void regulator_bulk_unregister_supply_alias(struct device *dev,
2257                                             const char *const *id,
2258                                             int num_id)
2259 {
2260         int i;
2261
2262         for (i = 0; i < num_id; ++i)
2263                 regulator_unregister_supply_alias(dev, id[i]);
2264 }
2265 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2266
2267
2268 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2269 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2270                                 const struct regulator_config *config)
2271 {
2272         struct regulator_enable_gpio *pin, *new_pin;
2273         struct gpio_desc *gpiod;
2274
2275         gpiod = config->ena_gpiod;
2276         new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL);
2277
2278         mutex_lock(&regulator_list_mutex);
2279
2280         list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
2281                 if (pin->gpiod == gpiod) {
2282                         rdev_dbg(rdev, "GPIO is already used\n");
2283                         goto update_ena_gpio_to_rdev;
2284                 }
2285         }
2286
2287         if (new_pin == NULL) {
2288                 mutex_unlock(&regulator_list_mutex);
2289                 return -ENOMEM;
2290         }
2291
2292         pin = new_pin;
2293         new_pin = NULL;
2294
2295         pin->gpiod = gpiod;
2296         list_add(&pin->list, &regulator_ena_gpio_list);
2297
2298 update_ena_gpio_to_rdev:
2299         pin->request_count++;
2300         rdev->ena_pin = pin;
2301
2302         mutex_unlock(&regulator_list_mutex);
2303         kfree(new_pin);
2304
2305         return 0;
2306 }
2307
2308 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2309 {
2310         struct regulator_enable_gpio *pin, *n;
2311
2312         if (!rdev->ena_pin)
2313                 return;
2314
2315         /* Free the GPIO only in case of no use */
2316         list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
2317                 if (pin->gpiod == rdev->ena_pin->gpiod) {
2318                         if (pin->request_count <= 1) {
2319                                 pin->request_count = 0;
2320                                 gpiod_put(pin->gpiod);
2321                                 list_del(&pin->list);
2322                                 kfree(pin);
2323                                 rdev->ena_pin = NULL;
2324                                 return;
2325                         } else {
2326                                 pin->request_count--;
2327                         }
2328                 }
2329         }
2330 }
2331
2332 /**
2333  * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2334  * @rdev: regulator_dev structure
2335  * @enable: enable GPIO at initial use?
2336  *
2337  * GPIO is enabled in case of initial use. (enable_count is 0)
2338  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2339  */
2340 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2341 {
2342         struct regulator_enable_gpio *pin = rdev->ena_pin;
2343
2344         if (!pin)
2345                 return -EINVAL;
2346
2347         if (enable) {
2348                 /* Enable GPIO at initial use */
2349                 if (pin->enable_count == 0)
2350                         gpiod_set_value_cansleep(pin->gpiod, 1);
2351
2352                 pin->enable_count++;
2353         } else {
2354                 if (pin->enable_count > 1) {
2355                         pin->enable_count--;
2356                         return 0;
2357                 }
2358
2359                 /* Disable GPIO if not used */
2360                 if (pin->enable_count <= 1) {
2361                         gpiod_set_value_cansleep(pin->gpiod, 0);
2362                         pin->enable_count = 0;
2363                 }
2364         }
2365
2366         return 0;
2367 }
2368
2369 /**
2370  * _regulator_enable_delay - a delay helper function
2371  * @delay: time to delay in microseconds
2372  *
2373  * Delay for the requested amount of time as per the guidelines in:
2374  *
2375  *     Documentation/timers/timers-howto.rst
2376  *
2377  * The assumption here is that regulators will never be enabled in
2378  * atomic context and therefore sleeping functions can be used.
2379  */
2380 static void _regulator_enable_delay(unsigned int delay)
2381 {
2382         unsigned int ms = delay / 1000;
2383         unsigned int us = delay % 1000;
2384
2385         if (ms > 0) {
2386                 /*
2387                  * For small enough values, handle super-millisecond
2388                  * delays in the usleep_range() call below.
2389                  */
2390                 if (ms < 20)
2391                         us += ms * 1000;
2392                 else
2393                         msleep(ms);
2394         }
2395
2396         /*
2397          * Give the scheduler some room to coalesce with any other
2398          * wakeup sources. For delays shorter than 10 us, don't even
2399          * bother setting up high-resolution timers and just busy-
2400          * loop.
2401          */
2402         if (us >= 10)
2403                 usleep_range(us, us + 100);
2404         else
2405                 udelay(us);
2406 }
2407
2408 static int _regulator_do_enable(struct regulator_dev *rdev)
2409 {
2410         int ret, delay;
2411
2412         /* Query before enabling in case configuration dependent.  */
2413         ret = _regulator_get_enable_time(rdev);
2414         if (ret >= 0) {
2415                 delay = ret;
2416         } else {
2417                 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2418                 delay = 0;
2419         }
2420
2421         trace_regulator_enable(rdev_get_name(rdev));
2422
2423         if (rdev->desc->off_on_delay) {
2424                 /* if needed, keep a distance of off_on_delay from last time
2425                  * this regulator was disabled.
2426                  */
2427                 unsigned long start_jiffy = jiffies;
2428                 unsigned long intended, max_delay, remaining;
2429
2430                 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2431                 intended = rdev->last_off_jiffy + max_delay;
2432
2433                 if (time_before(start_jiffy, intended)) {
2434                         /* calc remaining jiffies to deal with one-time
2435                          * timer wrapping.
2436                          * in case of multiple timer wrapping, either it can be
2437                          * detected by out-of-range remaining, or it cannot be
2438                          * detected and we get a penalty of
2439                          * _regulator_enable_delay().
2440                          */
2441                         remaining = intended - start_jiffy;
2442                         if (remaining <= max_delay)
2443                                 _regulator_enable_delay(
2444                                                 jiffies_to_usecs(remaining));
2445                 }
2446         }
2447
2448         if (rdev->ena_pin) {
2449                 if (!rdev->ena_gpio_state) {
2450                         ret = regulator_ena_gpio_ctrl(rdev, true);
2451                         if (ret < 0)
2452                                 return ret;
2453                         rdev->ena_gpio_state = 1;
2454                 }
2455         } else if (rdev->desc->ops->enable) {
2456                 ret = rdev->desc->ops->enable(rdev);
2457                 if (ret < 0)
2458                         return ret;
2459         } else {
2460                 return -EINVAL;
2461         }
2462
2463         /* Allow the regulator to ramp; it would be useful to extend
2464          * this for bulk operations so that the regulators can ramp
2465          * together.  */
2466         trace_regulator_enable_delay(rdev_get_name(rdev));
2467
2468         _regulator_enable_delay(delay);
2469
2470         trace_regulator_enable_complete(rdev_get_name(rdev));
2471
2472         return 0;
2473 }
2474
2475 /**
2476  * _regulator_handle_consumer_enable - handle that a consumer enabled
2477  * @regulator: regulator source
2478  *
2479  * Some things on a regulator consumer (like the contribution towards total
2480  * load on the regulator) only have an effect when the consumer wants the
2481  * regulator enabled.  Explained in example with two consumers of the same
2482  * regulator:
2483  *   consumer A: set_load(100);       => total load = 0
2484  *   consumer A: regulator_enable();  => total load = 100
2485  *   consumer B: set_load(1000);      => total load = 100
2486  *   consumer B: regulator_enable();  => total load = 1100
2487  *   consumer A: regulator_disable(); => total_load = 1000
2488  *
2489  * This function (together with _regulator_handle_consumer_disable) is
2490  * responsible for keeping track of the refcount for a given regulator consumer
2491  * and applying / unapplying these things.
2492  *
2493  * Returns 0 upon no error; -error upon error.
2494  */
2495 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2496 {
2497         int ret;
2498         struct regulator_dev *rdev = regulator->rdev;
2499
2500         lockdep_assert_held_once(&rdev->mutex.base);
2501
2502         regulator->enable_count++;
2503         if (regulator->uA_load && regulator->enable_count == 1) {
2504                 ret = drms_uA_update(rdev);
2505                 if (ret)
2506                         regulator->enable_count--;
2507                 return ret;
2508         }
2509
2510         return 0;
2511 }
2512
2513 /**
2514  * _regulator_handle_consumer_disable - handle that a consumer disabled
2515  * @regulator: regulator source
2516  *
2517  * The opposite of _regulator_handle_consumer_enable().
2518  *
2519  * Returns 0 upon no error; -error upon error.
2520  */
2521 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2522 {
2523         struct regulator_dev *rdev = regulator->rdev;
2524
2525         lockdep_assert_held_once(&rdev->mutex.base);
2526
2527         if (!regulator->enable_count) {
2528                 rdev_err(rdev, "Underflow of regulator enable count\n");
2529                 return -EINVAL;
2530         }
2531
2532         regulator->enable_count--;
2533         if (regulator->uA_load && regulator->enable_count == 0)
2534                 return drms_uA_update(rdev);
2535
2536         return 0;
2537 }
2538
2539 /* locks held by regulator_enable() */
2540 static int _regulator_enable(struct regulator *regulator)
2541 {
2542         struct regulator_dev *rdev = regulator->rdev;
2543         int ret;
2544
2545         lockdep_assert_held_once(&rdev->mutex.base);
2546
2547         if (rdev->use_count == 0 && rdev->supply) {
2548                 ret = _regulator_enable(rdev->supply);
2549                 if (ret < 0)
2550                         return ret;
2551         }
2552
2553         /* balance only if there are regulators coupled */
2554         if (rdev->coupling_desc.n_coupled > 1) {
2555                 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2556                 if (ret < 0)
2557                         goto err_disable_supply;
2558         }
2559
2560         ret = _regulator_handle_consumer_enable(regulator);
2561         if (ret < 0)
2562                 goto err_disable_supply;
2563
2564         if (rdev->use_count == 0) {
2565                 /* The regulator may on if it's not switchable or left on */
2566                 ret = _regulator_is_enabled(rdev);
2567                 if (ret == -EINVAL || ret == 0) {
2568                         if (!regulator_ops_is_valid(rdev,
2569                                         REGULATOR_CHANGE_STATUS)) {
2570                                 ret = -EPERM;
2571                                 goto err_consumer_disable;
2572                         }
2573
2574                         ret = _regulator_do_enable(rdev);
2575                         if (ret < 0)
2576                                 goto err_consumer_disable;
2577
2578                         _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2579                                              NULL);
2580                 } else if (ret < 0) {
2581                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2582                         goto err_consumer_disable;
2583                 }
2584                 /* Fallthrough on positive return values - already enabled */
2585         }
2586
2587         rdev->use_count++;
2588
2589         return 0;
2590
2591 err_consumer_disable:
2592         _regulator_handle_consumer_disable(regulator);
2593
2594 err_disable_supply:
2595         if (rdev->use_count == 0 && rdev->supply)
2596                 _regulator_disable(rdev->supply);
2597
2598         return ret;
2599 }
2600
2601 /**
2602  * regulator_enable - enable regulator output
2603  * @regulator: regulator source
2604  *
2605  * Request that the regulator be enabled with the regulator output at
2606  * the predefined voltage or current value.  Calls to regulator_enable()
2607  * must be balanced with calls to regulator_disable().
2608  *
2609  * NOTE: the output value can be set by other drivers, boot loader or may be
2610  * hardwired in the regulator.
2611  */
2612 int regulator_enable(struct regulator *regulator)
2613 {
2614         struct regulator_dev *rdev = regulator->rdev;
2615         struct ww_acquire_ctx ww_ctx;
2616         int ret;
2617
2618         regulator_lock_dependent(rdev, &ww_ctx);
2619         ret = _regulator_enable(regulator);
2620         regulator_unlock_dependent(rdev, &ww_ctx);
2621
2622         return ret;
2623 }
2624 EXPORT_SYMBOL_GPL(regulator_enable);
2625
2626 static int _regulator_do_disable(struct regulator_dev *rdev)
2627 {
2628         int ret;
2629
2630         trace_regulator_disable(rdev_get_name(rdev));
2631
2632         if (rdev->ena_pin) {
2633                 if (rdev->ena_gpio_state) {
2634                         ret = regulator_ena_gpio_ctrl(rdev, false);
2635                         if (ret < 0)
2636                                 return ret;
2637                         rdev->ena_gpio_state = 0;
2638                 }
2639
2640         } else if (rdev->desc->ops->disable) {
2641                 ret = rdev->desc->ops->disable(rdev);
2642                 if (ret != 0)
2643                         return ret;
2644         }
2645
2646         /* cares about last_off_jiffy only if off_on_delay is required by
2647          * device.
2648          */
2649         if (rdev->desc->off_on_delay)
2650                 rdev->last_off_jiffy = jiffies;
2651
2652         trace_regulator_disable_complete(rdev_get_name(rdev));
2653
2654         return 0;
2655 }
2656
2657 /* locks held by regulator_disable() */
2658 static int _regulator_disable(struct regulator *regulator)
2659 {
2660         struct regulator_dev *rdev = regulator->rdev;
2661         int ret = 0;
2662
2663         lockdep_assert_held_once(&rdev->mutex.base);
2664
2665         if (WARN(rdev->use_count <= 0,
2666                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
2667                 return -EIO;
2668
2669         /* are we the last user and permitted to disable ? */
2670         if (rdev->use_count == 1 &&
2671             (rdev->constraints && !rdev->constraints->always_on)) {
2672
2673                 /* we are last user */
2674                 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2675                         ret = _notifier_call_chain(rdev,
2676                                                    REGULATOR_EVENT_PRE_DISABLE,
2677                                                    NULL);
2678                         if (ret & NOTIFY_STOP_MASK)
2679                                 return -EINVAL;
2680
2681                         ret = _regulator_do_disable(rdev);
2682                         if (ret < 0) {
2683                                 rdev_err(rdev, "failed to disable\n");
2684                                 _notifier_call_chain(rdev,
2685                                                 REGULATOR_EVENT_ABORT_DISABLE,
2686                                                 NULL);
2687                                 return ret;
2688                         }
2689                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2690                                         NULL);
2691                 }
2692
2693                 rdev->use_count = 0;
2694         } else if (rdev->use_count > 1) {
2695                 rdev->use_count--;
2696         }
2697
2698         if (ret == 0)
2699                 ret = _regulator_handle_consumer_disable(regulator);
2700
2701         if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2702                 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2703
2704         if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2705                 ret = _regulator_disable(rdev->supply);
2706
2707         return ret;
2708 }
2709
2710 /**
2711  * regulator_disable - disable regulator output
2712  * @regulator: regulator source
2713  *
2714  * Disable the regulator output voltage or current.  Calls to
2715  * regulator_enable() must be balanced with calls to
2716  * regulator_disable().
2717  *
2718  * NOTE: this will only disable the regulator output if no other consumer
2719  * devices have it enabled, the regulator device supports disabling and
2720  * machine constraints permit this operation.
2721  */
2722 int regulator_disable(struct regulator *regulator)
2723 {
2724         struct regulator_dev *rdev = regulator->rdev;
2725         struct ww_acquire_ctx ww_ctx;
2726         int ret;
2727
2728         regulator_lock_dependent(rdev, &ww_ctx);
2729         ret = _regulator_disable(regulator);
2730         regulator_unlock_dependent(rdev, &ww_ctx);
2731
2732         return ret;
2733 }
2734 EXPORT_SYMBOL_GPL(regulator_disable);
2735
2736 /* locks held by regulator_force_disable() */
2737 static int _regulator_force_disable(struct regulator_dev *rdev)
2738 {
2739         int ret = 0;
2740
2741         lockdep_assert_held_once(&rdev->mutex.base);
2742
2743         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2744                         REGULATOR_EVENT_PRE_DISABLE, NULL);
2745         if (ret & NOTIFY_STOP_MASK)
2746                 return -EINVAL;
2747
2748         ret = _regulator_do_disable(rdev);
2749         if (ret < 0) {
2750                 rdev_err(rdev, "failed to force disable\n");
2751                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2752                                 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2753                 return ret;
2754         }
2755
2756         _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2757                         REGULATOR_EVENT_DISABLE, NULL);
2758
2759         return 0;
2760 }
2761
2762 /**
2763  * regulator_force_disable - force disable regulator output
2764  * @regulator: regulator source
2765  *
2766  * Forcibly disable the regulator output voltage or current.
2767  * NOTE: this *will* disable the regulator output even if other consumer
2768  * devices have it enabled. This should be used for situations when device
2769  * damage will likely occur if the regulator is not disabled (e.g. over temp).
2770  */
2771 int regulator_force_disable(struct regulator *regulator)
2772 {
2773         struct regulator_dev *rdev = regulator->rdev;
2774         struct ww_acquire_ctx ww_ctx;
2775         int ret;
2776
2777         regulator_lock_dependent(rdev, &ww_ctx);
2778
2779         ret = _regulator_force_disable(regulator->rdev);
2780
2781         if (rdev->coupling_desc.n_coupled > 1)
2782                 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2783
2784         if (regulator->uA_load) {
2785                 regulator->uA_load = 0;
2786                 ret = drms_uA_update(rdev);
2787         }
2788
2789         if (rdev->use_count != 0 && rdev->supply)
2790                 _regulator_disable(rdev->supply);
2791
2792         regulator_unlock_dependent(rdev, &ww_ctx);
2793
2794         return ret;
2795 }
2796 EXPORT_SYMBOL_GPL(regulator_force_disable);
2797
2798 static void regulator_disable_work(struct work_struct *work)
2799 {
2800         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2801                                                   disable_work.work);
2802         struct ww_acquire_ctx ww_ctx;
2803         int count, i, ret;
2804         struct regulator *regulator;
2805         int total_count = 0;
2806
2807         regulator_lock_dependent(rdev, &ww_ctx);
2808
2809         /*
2810          * Workqueue functions queue the new work instance while the previous
2811          * work instance is being processed. Cancel the queued work instance
2812          * as the work instance under processing does the job of the queued
2813          * work instance.
2814          */
2815         cancel_delayed_work(&rdev->disable_work);
2816
2817         list_for_each_entry(regulator, &rdev->consumer_list, list) {
2818                 count = regulator->deferred_disables;
2819
2820                 if (!count)
2821                         continue;
2822
2823                 total_count += count;
2824                 regulator->deferred_disables = 0;
2825
2826                 for (i = 0; i < count; i++) {
2827                         ret = _regulator_disable(regulator);
2828                         if (ret != 0)
2829                                 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2830                 }
2831         }
2832         WARN_ON(!total_count);
2833
2834         if (rdev->coupling_desc.n_coupled > 1)
2835                 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2836
2837         regulator_unlock_dependent(rdev, &ww_ctx);
2838 }
2839
2840 /**
2841  * regulator_disable_deferred - disable regulator output with delay
2842  * @regulator: regulator source
2843  * @ms: milliseconds until the regulator is disabled
2844  *
2845  * Execute regulator_disable() on the regulator after a delay.  This
2846  * is intended for use with devices that require some time to quiesce.
2847  *
2848  * NOTE: this will only disable the regulator output if no other consumer
2849  * devices have it enabled, the regulator device supports disabling and
2850  * machine constraints permit this operation.
2851  */
2852 int regulator_disable_deferred(struct regulator *regulator, int ms)
2853 {
2854         struct regulator_dev *rdev = regulator->rdev;
2855
2856         if (!ms)
2857                 return regulator_disable(regulator);
2858
2859         regulator_lock(rdev);
2860         regulator->deferred_disables++;
2861         mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2862                          msecs_to_jiffies(ms));
2863         regulator_unlock(rdev);
2864
2865         return 0;
2866 }
2867 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2868
2869 static int _regulator_is_enabled(struct regulator_dev *rdev)
2870 {
2871         /* A GPIO control always takes precedence */
2872         if (rdev->ena_pin)
2873                 return rdev->ena_gpio_state;
2874
2875         /* If we don't know then assume that the regulator is always on */
2876         if (!rdev->desc->ops->is_enabled)
2877                 return 1;
2878
2879         return rdev->desc->ops->is_enabled(rdev);
2880 }
2881
2882 static int _regulator_list_voltage(struct regulator_dev *rdev,
2883                                    unsigned selector, int lock)
2884 {
2885         const struct regulator_ops *ops = rdev->desc->ops;
2886         int ret;
2887
2888         if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2889                 return rdev->desc->fixed_uV;
2890
2891         if (ops->list_voltage) {
2892                 if (selector >= rdev->desc->n_voltages)
2893                         return -EINVAL;
2894                 if (lock)
2895                         regulator_lock(rdev);
2896                 ret = ops->list_voltage(rdev, selector);
2897                 if (lock)
2898                         regulator_unlock(rdev);
2899         } else if (rdev->is_switch && rdev->supply) {
2900                 ret = _regulator_list_voltage(rdev->supply->rdev,
2901                                               selector, lock);
2902         } else {
2903                 return -EINVAL;
2904         }
2905
2906         if (ret > 0) {
2907                 if (ret < rdev->constraints->min_uV)
2908                         ret = 0;
2909                 else if (ret > rdev->constraints->max_uV)
2910                         ret = 0;
2911         }
2912
2913         return ret;
2914 }
2915
2916 /**
2917  * regulator_is_enabled - is the regulator output enabled
2918  * @regulator: regulator source
2919  *
2920  * Returns positive if the regulator driver backing the source/client
2921  * has requested that the device be enabled, zero if it hasn't, else a
2922  * negative errno code.
2923  *
2924  * Note that the device backing this regulator handle can have multiple
2925  * users, so it might be enabled even if regulator_enable() was never
2926  * called for this particular source.
2927  */
2928 int regulator_is_enabled(struct regulator *regulator)
2929 {
2930         int ret;
2931
2932         if (regulator->always_on)
2933                 return 1;
2934
2935         regulator_lock(regulator->rdev);
2936         ret = _regulator_is_enabled(regulator->rdev);
2937         regulator_unlock(regulator->rdev);
2938
2939         return ret;
2940 }
2941 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2942
2943 /**
2944  * regulator_count_voltages - count regulator_list_voltage() selectors
2945  * @regulator: regulator source
2946  *
2947  * Returns number of selectors, or negative errno.  Selectors are
2948  * numbered starting at zero, and typically correspond to bitfields
2949  * in hardware registers.
2950  */
2951 int regulator_count_voltages(struct regulator *regulator)
2952 {
2953         struct regulator_dev    *rdev = regulator->rdev;
2954
2955         if (rdev->desc->n_voltages)
2956                 return rdev->desc->n_voltages;
2957
2958         if (!rdev->is_switch || !rdev->supply)
2959                 return -EINVAL;
2960
2961         return regulator_count_voltages(rdev->supply);
2962 }
2963 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2964
2965 /**
2966  * regulator_list_voltage - enumerate supported voltages
2967  * @regulator: regulator source
2968  * @selector: identify voltage to list
2969  * Context: can sleep
2970  *
2971  * Returns a voltage that can be passed to @regulator_set_voltage(),
2972  * zero if this selector code can't be used on this system, or a
2973  * negative errno.
2974  */
2975 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2976 {
2977         return _regulator_list_voltage(regulator->rdev, selector, 1);
2978 }
2979 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2980
2981 /**
2982  * regulator_get_regmap - get the regulator's register map
2983  * @regulator: regulator source
2984  *
2985  * Returns the register map for the given regulator, or an ERR_PTR value
2986  * if the regulator doesn't use regmap.
2987  */
2988 struct regmap *regulator_get_regmap(struct regulator *regulator)
2989 {
2990         struct regmap *map = regulator->rdev->regmap;
2991
2992         return map ? map : ERR_PTR(-EOPNOTSUPP);
2993 }
2994
2995 /**
2996  * regulator_get_hardware_vsel_register - get the HW voltage selector register
2997  * @regulator: regulator source
2998  * @vsel_reg: voltage selector register, output parameter
2999  * @vsel_mask: mask for voltage selector bitfield, output parameter
3000  *
3001  * Returns the hardware register offset and bitmask used for setting the
3002  * regulator voltage. This might be useful when configuring voltage-scaling
3003  * hardware or firmware that can make I2C requests behind the kernel's back,
3004  * for example.
3005  *
3006  * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3007  * and 0 is returned, otherwise a negative errno is returned.
3008  */
3009 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3010                                          unsigned *vsel_reg,
3011                                          unsigned *vsel_mask)
3012 {
3013         struct regulator_dev *rdev = regulator->rdev;
3014         const struct regulator_ops *ops = rdev->desc->ops;
3015
3016         if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3017                 return -EOPNOTSUPP;
3018
3019         *vsel_reg = rdev->desc->vsel_reg;
3020         *vsel_mask = rdev->desc->vsel_mask;
3021
3022          return 0;
3023 }
3024 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3025
3026 /**
3027  * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3028  * @regulator: regulator source
3029  * @selector: identify voltage to list
3030  *
3031  * Converts the selector to a hardware-specific voltage selector that can be
3032  * directly written to the regulator registers. The address of the voltage
3033  * register can be determined by calling @regulator_get_hardware_vsel_register.
3034  *
3035  * On error a negative errno is returned.
3036  */
3037 int regulator_list_hardware_vsel(struct regulator *regulator,
3038                                  unsigned selector)
3039 {
3040         struct regulator_dev *rdev = regulator->rdev;
3041         const struct regulator_ops *ops = rdev->desc->ops;
3042
3043         if (selector >= rdev->desc->n_voltages)
3044                 return -EINVAL;
3045         if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3046                 return -EOPNOTSUPP;
3047
3048         return selector;
3049 }
3050 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3051
3052 /**
3053  * regulator_get_linear_step - return the voltage step size between VSEL values
3054  * @regulator: regulator source
3055  *
3056  * Returns the voltage step size between VSEL values for linear
3057  * regulators, or return 0 if the regulator isn't a linear regulator.
3058  */
3059 unsigned int regulator_get_linear_step(struct regulator *regulator)
3060 {
3061         struct regulator_dev *rdev = regulator->rdev;
3062
3063         return rdev->desc->uV_step;
3064 }
3065 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3066
3067 /**
3068  * regulator_is_supported_voltage - check if a voltage range can be supported
3069  *
3070  * @regulator: Regulator to check.
3071  * @min_uV: Minimum required voltage in uV.
3072  * @max_uV: Maximum required voltage in uV.
3073  *
3074  * Returns a boolean.
3075  */
3076 int regulator_is_supported_voltage(struct regulator *regulator,
3077                                    int min_uV, int max_uV)
3078 {
3079         struct regulator_dev *rdev = regulator->rdev;
3080         int i, voltages, ret;
3081
3082         /* If we can't change voltage check the current voltage */
3083         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3084                 ret = regulator_get_voltage(regulator);
3085                 if (ret >= 0)
3086                         return min_uV <= ret && ret <= max_uV;
3087                 else
3088                         return ret;
3089         }
3090
3091         /* Any voltage within constrains range is fine? */
3092         if (rdev->desc->continuous_voltage_range)
3093                 return min_uV >= rdev->constraints->min_uV &&
3094                                 max_uV <= rdev->constraints->max_uV;
3095
3096         ret = regulator_count_voltages(regulator);
3097         if (ret < 0)
3098                 return 0;
3099         voltages = ret;
3100
3101         for (i = 0; i < voltages; i++) {
3102                 ret = regulator_list_voltage(regulator, i);
3103
3104                 if (ret >= min_uV && ret <= max_uV)
3105                         return 1;
3106         }
3107
3108         return 0;
3109 }
3110 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3111
3112 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3113                                  int max_uV)
3114 {
3115         const struct regulator_desc *desc = rdev->desc;
3116
3117         if (desc->ops->map_voltage)
3118                 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3119
3120         if (desc->ops->list_voltage == regulator_list_voltage_linear)
3121                 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3122
3123         if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3124                 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3125
3126         if (desc->ops->list_voltage ==
3127                 regulator_list_voltage_pickable_linear_range)
3128                 return regulator_map_voltage_pickable_linear_range(rdev,
3129                                                         min_uV, max_uV);
3130
3131         return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3132 }
3133
3134 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3135                                        int min_uV, int max_uV,
3136                                        unsigned *selector)
3137 {
3138         struct pre_voltage_change_data data;
3139         int ret;
3140
3141         data.old_uV = regulator_get_voltage_rdev(rdev);
3142         data.min_uV = min_uV;
3143         data.max_uV = max_uV;
3144         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3145                                    &data);
3146         if (ret & NOTIFY_STOP_MASK)
3147                 return -EINVAL;
3148
3149         ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3150         if (ret >= 0)
3151                 return ret;
3152
3153         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3154                              (void *)data.old_uV);
3155
3156         return ret;
3157 }
3158
3159 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3160                                            int uV, unsigned selector)
3161 {
3162         struct pre_voltage_change_data data;
3163         int ret;
3164
3165         data.old_uV = regulator_get_voltage_rdev(rdev);
3166         data.min_uV = uV;
3167         data.max_uV = uV;
3168         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3169                                    &data);
3170         if (ret & NOTIFY_STOP_MASK)
3171                 return -EINVAL;
3172
3173         ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3174         if (ret >= 0)
3175                 return ret;
3176
3177         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3178                              (void *)data.old_uV);
3179
3180         return ret;
3181 }
3182
3183 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3184                                            int uV, int new_selector)
3185 {
3186         const struct regulator_ops *ops = rdev->desc->ops;
3187         int diff, old_sel, curr_sel, ret;
3188
3189         /* Stepping is only needed if the regulator is enabled. */
3190         if (!_regulator_is_enabled(rdev))
3191                 goto final_set;
3192
3193         if (!ops->get_voltage_sel)
3194                 return -EINVAL;
3195
3196         old_sel = ops->get_voltage_sel(rdev);
3197         if (old_sel < 0)
3198                 return old_sel;
3199
3200         diff = new_selector - old_sel;
3201         if (diff == 0)
3202                 return 0; /* No change needed. */
3203
3204         if (diff > 0) {
3205                 /* Stepping up. */
3206                 for (curr_sel = old_sel + rdev->desc->vsel_step;
3207                      curr_sel < new_selector;
3208                      curr_sel += rdev->desc->vsel_step) {
3209                         /*
3210                          * Call the callback directly instead of using
3211                          * _regulator_call_set_voltage_sel() as we don't
3212                          * want to notify anyone yet. Same in the branch
3213                          * below.
3214                          */
3215                         ret = ops->set_voltage_sel(rdev, curr_sel);
3216                         if (ret)
3217                                 goto try_revert;
3218                 }
3219         } else {
3220                 /* Stepping down. */
3221                 for (curr_sel = old_sel - rdev->desc->vsel_step;
3222                      curr_sel > new_selector;
3223                      curr_sel -= rdev->desc->vsel_step) {
3224                         ret = ops->set_voltage_sel(rdev, curr_sel);
3225                         if (ret)
3226                                 goto try_revert;
3227                 }
3228         }
3229
3230 final_set:
3231         /* The final selector will trigger the notifiers. */
3232         return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3233
3234 try_revert:
3235         /*
3236          * At least try to return to the previous voltage if setting a new
3237          * one failed.
3238          */
3239         (void)ops->set_voltage_sel(rdev, old_sel);
3240         return ret;
3241 }
3242
3243 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3244                                        int old_uV, int new_uV)
3245 {
3246         unsigned int ramp_delay = 0;
3247
3248         if (rdev->constraints->ramp_delay)
3249                 ramp_delay = rdev->constraints->ramp_delay;
3250         else if (rdev->desc->ramp_delay)
3251                 ramp_delay = rdev->desc->ramp_delay;
3252         else if (rdev->constraints->settling_time)
3253                 return rdev->constraints->settling_time;
3254         else if (rdev->constraints->settling_time_up &&
3255                  (new_uV > old_uV))
3256                 return rdev->constraints->settling_time_up;
3257         else if (rdev->constraints->settling_time_down &&
3258                  (new_uV < old_uV))
3259                 return rdev->constraints->settling_time_down;
3260
3261         if (ramp_delay == 0) {
3262                 rdev_dbg(rdev, "ramp_delay not set\n");
3263                 return 0;
3264         }
3265
3266         return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3267 }
3268
3269 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3270                                      int min_uV, int max_uV)
3271 {
3272         int ret;
3273         int delay = 0;
3274         int best_val = 0;
3275         unsigned int selector;
3276         int old_selector = -1;
3277         const struct regulator_ops *ops = rdev->desc->ops;
3278         int old_uV = regulator_get_voltage_rdev(rdev);
3279
3280         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3281
3282         min_uV += rdev->constraints->uV_offset;
3283         max_uV += rdev->constraints->uV_offset;
3284
3285         /*
3286          * If we can't obtain the old selector there is not enough
3287          * info to call set_voltage_time_sel().
3288          */
3289         if (_regulator_is_enabled(rdev) &&
3290             ops->set_voltage_time_sel && ops->get_voltage_sel) {
3291                 old_selector = ops->get_voltage_sel(rdev);
3292                 if (old_selector < 0)
3293                         return old_selector;
3294         }
3295
3296         if (ops->set_voltage) {
3297                 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3298                                                   &selector);
3299
3300                 if (ret >= 0) {
3301                         if (ops->list_voltage)
3302                                 best_val = ops->list_voltage(rdev,
3303                                                              selector);
3304                         else
3305                                 best_val = regulator_get_voltage_rdev(rdev);
3306                 }
3307
3308         } else if (ops->set_voltage_sel) {
3309                 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3310                 if (ret >= 0) {
3311                         best_val = ops->list_voltage(rdev, ret);
3312                         if (min_uV <= best_val && max_uV >= best_val) {
3313                                 selector = ret;
3314                                 if (old_selector == selector)
3315                                         ret = 0;
3316                                 else if (rdev->desc->vsel_step)
3317                                         ret = _regulator_set_voltage_sel_step(
3318                                                 rdev, best_val, selector);
3319                                 else
3320                                         ret = _regulator_call_set_voltage_sel(
3321                                                 rdev, best_val, selector);
3322                         } else {
3323                                 ret = -EINVAL;
3324                         }
3325                 }
3326         } else {
3327                 ret = -EINVAL;
3328         }
3329
3330         if (ret)
3331                 goto out;
3332
3333         if (ops->set_voltage_time_sel) {
3334                 /*
3335                  * Call set_voltage_time_sel if successfully obtained
3336                  * old_selector
3337                  */
3338                 if (old_selector >= 0 && old_selector != selector)
3339                         delay = ops->set_voltage_time_sel(rdev, old_selector,
3340                                                           selector);
3341         } else {
3342                 if (old_uV != best_val) {
3343                         if (ops->set_voltage_time)
3344                                 delay = ops->set_voltage_time(rdev, old_uV,
3345                                                               best_val);
3346                         else
3347                                 delay = _regulator_set_voltage_time(rdev,
3348                                                                     old_uV,
3349                                                                     best_val);
3350                 }
3351         }
3352
3353         if (delay < 0) {
3354                 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3355                 delay = 0;
3356         }
3357
3358         /* Insert any necessary delays */
3359         if (delay >= 1000) {
3360                 mdelay(delay / 1000);
3361                 udelay(delay % 1000);
3362         } else if (delay) {
3363                 udelay(delay);
3364         }
3365
3366         if (best_val >= 0) {
3367                 unsigned long data = best_val;
3368
3369                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3370                                      (void *)data);
3371         }
3372
3373 out:
3374         trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3375
3376         return ret;
3377 }
3378
3379 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3380                                   int min_uV, int max_uV, suspend_state_t state)
3381 {
3382         struct regulator_state *rstate;
3383         int uV, sel;
3384
3385         rstate = regulator_get_suspend_state(rdev, state);
3386         if (rstate == NULL)
3387                 return -EINVAL;
3388
3389         if (min_uV < rstate->min_uV)
3390                 min_uV = rstate->min_uV;
3391         if (max_uV > rstate->max_uV)
3392                 max_uV = rstate->max_uV;
3393
3394         sel = regulator_map_voltage(rdev, min_uV, max_uV);
3395         if (sel < 0)
3396                 return sel;
3397
3398         uV = rdev->desc->ops->list_voltage(rdev, sel);
3399         if (uV >= min_uV && uV <= max_uV)
3400                 rstate->uV = uV;
3401
3402         return 0;
3403 }
3404
3405 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3406                                           int min_uV, int max_uV,
3407                                           suspend_state_t state)
3408 {
3409         struct regulator_dev *rdev = regulator->rdev;
3410         struct regulator_voltage *voltage = &regulator->voltage[state];
3411         int ret = 0;
3412         int old_min_uV, old_max_uV;
3413         int current_uV;
3414
3415         /* If we're setting the same range as last time the change
3416          * should be a noop (some cpufreq implementations use the same
3417          * voltage for multiple frequencies, for example).
3418          */
3419         if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3420                 goto out;
3421
3422         /* If we're trying to set a range that overlaps the current voltage,
3423          * return successfully even though the regulator does not support
3424          * changing the voltage.
3425          */
3426         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3427                 current_uV = regulator_get_voltage_rdev(rdev);
3428                 if (min_uV <= current_uV && current_uV <= max_uV) {
3429                         voltage->min_uV = min_uV;
3430                         voltage->max_uV = max_uV;
3431                         goto out;
3432                 }
3433         }
3434
3435         /* sanity check */
3436         if (!rdev->desc->ops->set_voltage &&
3437             !rdev->desc->ops->set_voltage_sel) {
3438                 ret = -EINVAL;
3439                 goto out;
3440         }
3441
3442         /* constraints check */
3443         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3444         if (ret < 0)
3445                 goto out;
3446
3447         /* restore original values in case of error */
3448         old_min_uV = voltage->min_uV;
3449         old_max_uV = voltage->max_uV;
3450         voltage->min_uV = min_uV;
3451         voltage->max_uV = max_uV;
3452
3453         /* for not coupled regulators this will just set the voltage */
3454         ret = regulator_balance_voltage(rdev, state);
3455         if (ret < 0) {
3456                 voltage->min_uV = old_min_uV;
3457                 voltage->max_uV = old_max_uV;
3458         }
3459
3460 out:
3461         return ret;
3462 }
3463
3464 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3465                                int max_uV, suspend_state_t state)
3466 {
3467         int best_supply_uV = 0;
3468         int supply_change_uV = 0;
3469         int ret;
3470
3471         if (rdev->supply &&
3472             regulator_ops_is_valid(rdev->supply->rdev,
3473                                    REGULATOR_CHANGE_VOLTAGE) &&
3474             (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3475                                            rdev->desc->ops->get_voltage_sel))) {
3476                 int current_supply_uV;
3477                 int selector;
3478
3479                 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3480                 if (selector < 0) {
3481                         ret = selector;
3482                         goto out;
3483                 }
3484
3485                 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3486                 if (best_supply_uV < 0) {
3487                         ret = best_supply_uV;
3488                         goto out;
3489                 }
3490
3491                 best_supply_uV += rdev->desc->min_dropout_uV;
3492
3493                 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3494                 if (current_supply_uV < 0) {
3495                         ret = current_supply_uV;
3496                         goto out;
3497                 }
3498
3499                 supply_change_uV = best_supply_uV - current_supply_uV;
3500         }
3501
3502         if (supply_change_uV > 0) {
3503                 ret = regulator_set_voltage_unlocked(rdev->supply,
3504                                 best_supply_uV, INT_MAX, state);
3505                 if (ret) {
3506                         dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3507                                         ret);
3508                         goto out;
3509                 }
3510         }
3511
3512         if (state == PM_SUSPEND_ON)
3513                 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3514         else
3515                 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3516                                                         max_uV, state);
3517         if (ret < 0)
3518                 goto out;
3519
3520         if (supply_change_uV < 0) {
3521                 ret = regulator_set_voltage_unlocked(rdev->supply,
3522                                 best_supply_uV, INT_MAX, state);
3523                 if (ret)
3524                         dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3525                                         ret);
3526                 /* No need to fail here */
3527                 ret = 0;
3528         }
3529
3530 out:
3531         return ret;
3532 }
3533 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3534
3535 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3536                                         int *current_uV, int *min_uV)
3537 {
3538         struct regulation_constraints *constraints = rdev->constraints;
3539
3540         /* Limit voltage change only if necessary */
3541         if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3542                 return 1;
3543
3544         if (*current_uV < 0) {
3545                 *current_uV = regulator_get_voltage_rdev(rdev);
3546
3547                 if (*current_uV < 0)
3548                         return *current_uV;
3549         }
3550
3551         if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3552                 return 1;
3553
3554         /* Clamp target voltage within the given step */
3555         if (*current_uV < *min_uV)
3556                 *min_uV = min(*current_uV + constraints->max_uV_step,
3557                               *min_uV);
3558         else
3559                 *min_uV = max(*current_uV - constraints->max_uV_step,
3560                               *min_uV);
3561
3562         return 0;
3563 }
3564
3565 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3566                                          int *current_uV,
3567                                          int *min_uV, int *max_uV,
3568                                          suspend_state_t state,
3569                                          int n_coupled)
3570 {
3571         struct coupling_desc *c_desc = &rdev->coupling_desc;
3572         struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3573         struct regulation_constraints *constraints = rdev->constraints;
3574         int desired_min_uV = 0, desired_max_uV = INT_MAX;
3575         int max_current_uV = 0, min_current_uV = INT_MAX;
3576         int highest_min_uV = 0, target_uV, possible_uV;
3577         int i, ret, max_spread;
3578         bool done;
3579
3580         *current_uV = -1;
3581
3582         /*
3583          * If there are no coupled regulators, simply set the voltage
3584          * demanded by consumers.
3585          */
3586         if (n_coupled == 1) {
3587                 /*
3588                  * If consumers don't provide any demands, set voltage
3589                  * to min_uV
3590                  */
3591                 desired_min_uV = constraints->min_uV;
3592                 desired_max_uV = constraints->max_uV;
3593
3594                 ret = regulator_check_consumers(rdev,
3595                                                 &desired_min_uV,
3596                                                 &desired_max_uV, state);
3597                 if (ret < 0)
3598                         return ret;
3599
3600                 possible_uV = desired_min_uV;
3601                 done = true;
3602
3603                 goto finish;
3604         }
3605
3606         /* Find highest min desired voltage */
3607         for (i = 0; i < n_coupled; i++) {
3608                 int tmp_min = 0;
3609                 int tmp_max = INT_MAX;
3610
3611                 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3612
3613                 ret = regulator_check_consumers(c_rdevs[i],
3614                                                 &tmp_min,
3615                                                 &tmp_max, state);
3616                 if (ret < 0)
3617                         return ret;
3618
3619                 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3620                 if (ret < 0)
3621                         return ret;
3622
3623                 highest_min_uV = max(highest_min_uV, tmp_min);
3624
3625                 if (i == 0) {
3626                         desired_min_uV = tmp_min;
3627                         desired_max_uV = tmp_max;
3628                 }
3629         }
3630
3631         max_spread = constraints->max_spread[0];
3632
3633         /*
3634          * Let target_uV be equal to the desired one if possible.
3635          * If not, set it to minimum voltage, allowed by other coupled
3636          * regulators.
3637          */
3638         target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3639
3640         /*
3641          * Find min and max voltages, which currently aren't violating
3642          * max_spread.
3643          */
3644         for (i = 1; i < n_coupled; i++) {
3645                 int tmp_act;
3646
3647                 if (!_regulator_is_enabled(c_rdevs[i]))
3648                         continue;
3649
3650                 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3651                 if (tmp_act < 0)
3652                         return tmp_act;
3653
3654                 min_current_uV = min(tmp_act, min_current_uV);
3655                 max_current_uV = max(tmp_act, max_current_uV);
3656         }
3657
3658         /* There aren't any other regulators enabled */
3659         if (max_current_uV == 0) {
3660                 possible_uV = target_uV;
3661         } else {
3662                 /*
3663                  * Correct target voltage, so as it currently isn't
3664                  * violating max_spread
3665                  */
3666                 possible_uV = max(target_uV, max_current_uV - max_spread);
3667                 possible_uV = min(possible_uV, min_current_uV + max_spread);
3668         }
3669
3670         if (possible_uV > desired_max_uV)
3671                 return -EINVAL;
3672
3673         done = (possible_uV == target_uV);
3674         desired_min_uV = possible_uV;
3675
3676 finish:
3677         /* Apply max_uV_step constraint if necessary */
3678         if (state == PM_SUSPEND_ON) {
3679                 ret = regulator_limit_voltage_step(rdev, current_uV,
3680                                                    &desired_min_uV);
3681                 if (ret < 0)
3682                         return ret;
3683
3684                 if (ret == 0)
3685                         done = false;
3686         }
3687
3688         /* Set current_uV if wasn't done earlier in the code and if necessary */
3689         if (n_coupled > 1 && *current_uV == -1) {
3690
3691                 if (_regulator_is_enabled(rdev)) {
3692                         ret = regulator_get_voltage_rdev(rdev);
3693                         if (ret < 0)
3694                                 return ret;
3695
3696                         *current_uV = ret;
3697                 } else {
3698                         *current_uV = desired_min_uV;
3699                 }
3700         }
3701
3702         *min_uV = desired_min_uV;
3703         *max_uV = desired_max_uV;
3704
3705         return done;
3706 }
3707
3708 static int regulator_balance_voltage(struct regulator_dev *rdev,
3709                                      suspend_state_t state)
3710 {
3711         struct regulator_dev **c_rdevs;
3712         struct regulator_dev *best_rdev;
3713         struct coupling_desc *c_desc = &rdev->coupling_desc;
3714         struct regulator_coupler *coupler = c_desc->coupler;
3715         int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3716         unsigned int delta, best_delta;
3717         unsigned long c_rdev_done = 0;
3718         bool best_c_rdev_done;
3719
3720         c_rdevs = c_desc->coupled_rdevs;
3721         n_coupled = c_desc->n_coupled;
3722
3723         /*
3724          * If system is in a state other than PM_SUSPEND_ON, don't check
3725          * other coupled regulators.
3726          */
3727         if (state != PM_SUSPEND_ON)
3728                 n_coupled = 1;
3729
3730         if (c_desc->n_resolved < n_coupled) {
3731                 rdev_err(rdev, "Not all coupled regulators registered\n");
3732                 return -EPERM;
3733         }
3734
3735         /* Invoke custom balancer for customized couplers */
3736         if (coupler && coupler->balance_voltage)
3737                 return coupler->balance_voltage(coupler, rdev, state);
3738
3739         /*
3740          * Find the best possible voltage change on each loop. Leave the loop
3741          * if there isn't any possible change.
3742          */
3743         do {
3744                 best_c_rdev_done = false;
3745                 best_delta = 0;
3746                 best_min_uV = 0;
3747                 best_max_uV = 0;
3748                 best_c_rdev = 0;
3749                 best_rdev = NULL;
3750
3751                 /*
3752                  * Find highest difference between optimal voltage
3753                  * and current voltage.
3754                  */
3755                 for (i = 0; i < n_coupled; i++) {
3756                         /*
3757                          * optimal_uV is the best voltage that can be set for
3758                          * i-th regulator at the moment without violating
3759                          * max_spread constraint in order to balance
3760                          * the coupled voltages.
3761                          */
3762                         int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3763
3764                         if (test_bit(i, &c_rdev_done))
3765                                 continue;
3766
3767                         ret = regulator_get_optimal_voltage(c_rdevs[i],
3768                                                             &current_uV,
3769                                                             &optimal_uV,
3770                                                             &optimal_max_uV,
3771                                                             state, n_coupled);
3772                         if (ret < 0)
3773                                 goto out;
3774
3775                         delta = abs(optimal_uV - current_uV);
3776
3777                         if (delta && best_delta <= delta) {
3778                                 best_c_rdev_done = ret;
3779                                 best_delta = delta;
3780                                 best_rdev = c_rdevs[i];
3781                                 best_min_uV = optimal_uV;
3782                                 best_max_uV = optimal_max_uV;
3783                                 best_c_rdev = i;
3784                         }
3785                 }
3786
3787                 /* Nothing to change, return successfully */
3788                 if (!best_rdev) {
3789                         ret = 0;
3790                         goto out;
3791                 }
3792
3793                 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3794                                                  best_max_uV, state);
3795
3796                 if (ret < 0)
3797                         goto out;
3798
3799                 if (best_c_rdev_done)
3800                         set_bit(best_c_rdev, &c_rdev_done);
3801
3802         } while (n_coupled > 1);
3803
3804 out:
3805         return ret;
3806 }
3807
3808 /**
3809  * regulator_set_voltage - set regulator output voltage
3810  * @regulator: regulator source
3811  * @min_uV: Minimum required voltage in uV
3812  * @max_uV: Maximum acceptable voltage in uV
3813  *
3814  * Sets a voltage regulator to the desired output voltage. This can be set
3815  * during any regulator state. IOW, regulator can be disabled or enabled.
3816  *
3817  * If the regulator is enabled then the voltage will change to the new value
3818  * immediately otherwise if the regulator is disabled the regulator will
3819  * output at the new voltage when enabled.
3820  *
3821  * NOTE: If the regulator is shared between several devices then the lowest
3822  * request voltage that meets the system constraints will be used.
3823  * Regulator system constraints must be set for this regulator before
3824  * calling this function otherwise this call will fail.
3825  */
3826 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3827 {
3828         struct ww_acquire_ctx ww_ctx;
3829         int ret;
3830
3831         regulator_lock_dependent(regulator->rdev, &ww_ctx);
3832
3833         ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3834                                              PM_SUSPEND_ON);
3835
3836         regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3837
3838         return ret;
3839 }
3840 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3841
3842 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3843                                            suspend_state_t state, bool en)
3844 {
3845         struct regulator_state *rstate;
3846
3847         rstate = regulator_get_suspend_state(rdev, state);
3848         if (rstate == NULL)
3849                 return -EINVAL;
3850
3851         if (!rstate->changeable)
3852                 return -EPERM;
3853
3854         rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3855
3856         return 0;
3857 }
3858
3859 int regulator_suspend_enable(struct regulator_dev *rdev,
3860                                     suspend_state_t state)
3861 {
3862         return regulator_suspend_toggle(rdev, state, true);
3863 }
3864 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3865
3866 int regulator_suspend_disable(struct regulator_dev *rdev,
3867                                      suspend_state_t state)
3868 {
3869         struct regulator *regulator;
3870         struct regulator_voltage *voltage;
3871
3872         /*
3873          * if any consumer wants this regulator device keeping on in
3874          * suspend states, don't set it as disabled.
3875          */
3876         list_for_each_entry(regulator, &rdev->consumer_list, list) {
3877                 voltage = &regulator->voltage[state];
3878                 if (voltage->min_uV || voltage->max_uV)
3879                         return 0;
3880         }
3881
3882         return regulator_suspend_toggle(rdev, state, false);
3883 }
3884 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3885
3886 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3887                                           int min_uV, int max_uV,
3888                                           suspend_state_t state)
3889 {
3890         struct regulator_dev *rdev = regulator->rdev;
3891         struct regulator_state *rstate;
3892
3893         rstate = regulator_get_suspend_state(rdev, state);
3894         if (rstate == NULL)
3895                 return -EINVAL;
3896
3897         if (rstate->min_uV == rstate->max_uV) {
3898                 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3899                 return -EPERM;
3900         }
3901
3902         return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3903 }
3904
3905 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3906                                   int max_uV, suspend_state_t state)
3907 {
3908         struct ww_acquire_ctx ww_ctx;
3909         int ret;
3910
3911         /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3912         if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3913                 return -EINVAL;
3914
3915         regulator_lock_dependent(regulator->rdev, &ww_ctx);
3916
3917         ret = _regulator_set_suspend_voltage(regulator, min_uV,
3918                                              max_uV, state);
3919
3920         regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3921
3922         return ret;
3923 }
3924 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3925
3926 /**
3927  * regulator_set_voltage_time - get raise/fall time
3928  * @regulator: regulator source
3929  * @old_uV: starting voltage in microvolts
3930  * @new_uV: target voltage in microvolts
3931  *
3932  * Provided with the starting and ending voltage, this function attempts to
3933  * calculate the time in microseconds required to rise or fall to this new
3934  * voltage.
3935  */
3936 int regulator_set_voltage_time(struct regulator *regulator,
3937                                int old_uV, int new_uV)
3938 {
3939         struct regulator_dev *rdev = regulator->rdev;
3940         const struct regulator_ops *ops = rdev->desc->ops;
3941         int old_sel = -1;
3942         int new_sel = -1;
3943         int voltage;
3944         int i;
3945
3946         if (ops->set_voltage_time)
3947                 return ops->set_voltage_time(rdev, old_uV, new_uV);
3948         else if (!ops->set_voltage_time_sel)
3949                 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3950
3951         /* Currently requires operations to do this */
3952         if (!ops->list_voltage || !rdev->desc->n_voltages)
3953                 return -EINVAL;
3954
3955         for (i = 0; i < rdev->desc->n_voltages; i++) {
3956                 /* We only look for exact voltage matches here */
3957                 voltage = regulator_list_voltage(regulator, i);
3958                 if (voltage < 0)
3959                         return -EINVAL;
3960                 if (voltage == 0)
3961                         continue;
3962                 if (voltage == old_uV)
3963                         old_sel = i;
3964                 if (voltage == new_uV)
3965                         new_sel = i;
3966         }
3967
3968         if (old_sel < 0 || new_sel < 0)
3969                 return -EINVAL;
3970
3971         return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3972 }
3973 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3974
3975 /**
3976  * regulator_set_voltage_time_sel - get raise/fall time
3977  * @rdev: regulator source device
3978  * @old_selector: selector for starting voltage
3979  * @new_selector: selector for target voltage
3980  *
3981  * Provided with the starting and target voltage selectors, this function
3982  * returns time in microseconds required to rise or fall to this new voltage
3983  *
3984  * Drivers providing ramp_delay in regulation_constraints can use this as their
3985  * set_voltage_time_sel() operation.
3986  */
3987 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3988                                    unsigned int old_selector,
3989                                    unsigned int new_selector)
3990 {
3991         int old_volt, new_volt;
3992
3993         /* sanity check */
3994         if (!rdev->desc->ops->list_voltage)
3995                 return -EINVAL;
3996
3997         old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3998         new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3999
4000         if (rdev->desc->ops->set_voltage_time)
4001                 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4002                                                          new_volt);
4003         else
4004                 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4005 }
4006 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4007
4008 /**
4009  * regulator_sync_voltage - re-apply last regulator output voltage
4010  * @regulator: regulator source
4011  *
4012  * Re-apply the last configured voltage.  This is intended to be used
4013  * where some external control source the consumer is cooperating with
4014  * has caused the configured voltage to change.
4015  */
4016 int regulator_sync_voltage(struct regulator *regulator)
4017 {
4018         struct regulator_dev *rdev = regulator->rdev;
4019         struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
4020         int ret, min_uV, max_uV;
4021
4022         regulator_lock(rdev);
4023
4024         if (!rdev->desc->ops->set_voltage &&
4025             !rdev->desc->ops->set_voltage_sel) {
4026                 ret = -EINVAL;
4027                 goto out;
4028         }
4029
4030         /* This is only going to work if we've had a voltage configured. */
4031         if (!voltage->min_uV && !voltage->max_uV) {
4032                 ret = -EINVAL;
4033                 goto out;
4034         }
4035
4036         min_uV = voltage->min_uV;
4037         max_uV = voltage->max_uV;
4038
4039         /* This should be a paranoia check... */
4040         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4041         if (ret < 0)
4042                 goto out;
4043
4044         ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4045         if (ret < 0)
4046                 goto out;
4047
4048         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4049
4050 out:
4051         regulator_unlock(rdev);
4052         return ret;
4053 }
4054 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4055
4056 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4057 {
4058         int sel, ret;
4059         bool bypassed;
4060
4061         if (rdev->desc->ops->get_bypass) {
4062                 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4063                 if (ret < 0)
4064                         return ret;
4065                 if (bypassed) {
4066                         /* if bypassed the regulator must have a supply */
4067                         if (!rdev->supply) {
4068                                 rdev_err(rdev,
4069                                          "bypassed regulator has no supply!\n");
4070                                 return -EPROBE_DEFER;
4071                         }
4072
4073                         return regulator_get_voltage_rdev(rdev->supply->rdev);
4074                 }
4075         }
4076
4077         if (rdev->desc->ops->get_voltage_sel) {
4078                 sel = rdev->desc->ops->get_voltage_sel(rdev);
4079                 if (sel < 0)
4080                         return sel;
4081                 ret = rdev->desc->ops->list_voltage(rdev, sel);
4082         } else if (rdev->desc->ops->get_voltage) {
4083                 ret = rdev->desc->ops->get_voltage(rdev);
4084         } else if (rdev->desc->ops->list_voltage) {
4085                 ret = rdev->desc->ops->list_voltage(rdev, 0);
4086         } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4087                 ret = rdev->desc->fixed_uV;
4088         } else if (rdev->supply) {
4089                 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4090         } else if (rdev->supply_name) {
4091                 return -EPROBE_DEFER;
4092         } else {
4093                 return -EINVAL;
4094         }
4095
4096         if (ret < 0)
4097                 return ret;
4098         return ret - rdev->constraints->uV_offset;
4099 }
4100 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4101
4102 /**
4103  * regulator_get_voltage - get regulator output voltage
4104  * @regulator: regulator source
4105  *
4106  * This returns the current regulator voltage in uV.
4107  *
4108  * NOTE: If the regulator is disabled it will return the voltage value. This
4109  * function should not be used to determine regulator state.
4110  */
4111 int regulator_get_voltage(struct regulator *regulator)
4112 {
4113         struct ww_acquire_ctx ww_ctx;
4114         int ret;
4115
4116         regulator_lock_dependent(regulator->rdev, &ww_ctx);
4117         ret = regulator_get_voltage_rdev(regulator->rdev);
4118         regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4119
4120         return ret;
4121 }
4122 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4123
4124 /**
4125  * regulator_set_current_limit - set regulator output current limit
4126  * @regulator: regulator source
4127  * @min_uA: Minimum supported current in uA
4128  * @max_uA: Maximum supported current in uA
4129  *
4130  * Sets current sink to the desired output current. This can be set during
4131  * any regulator state. IOW, regulator can be disabled or enabled.
4132  *
4133  * If the regulator is enabled then the current will change to the new value
4134  * immediately otherwise if the regulator is disabled the regulator will
4135  * output at the new current when enabled.
4136  *
4137  * NOTE: Regulator system constraints must be set for this regulator before
4138  * calling this function otherwise this call will fail.
4139  */
4140 int regulator_set_current_limit(struct regulator *regulator,
4141                                int min_uA, int max_uA)
4142 {
4143         struct regulator_dev *rdev = regulator->rdev;
4144         int ret;
4145
4146         regulator_lock(rdev);
4147
4148         /* sanity check */
4149         if (!rdev->desc->ops->set_current_limit) {
4150                 ret = -EINVAL;
4151                 goto out;
4152         }
4153
4154         /* constraints check */
4155         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4156         if (ret < 0)
4157                 goto out;
4158
4159         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4160 out:
4161         regulator_unlock(rdev);
4162         return ret;
4163 }
4164 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4165
4166 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4167 {
4168         /* sanity check */
4169         if (!rdev->desc->ops->get_current_limit)
4170                 return -EINVAL;
4171
4172         return rdev->desc->ops->get_current_limit(rdev);
4173 }
4174
4175 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4176 {
4177         int ret;
4178
4179         regulator_lock(rdev);
4180         ret = _regulator_get_current_limit_unlocked(rdev);
4181         regulator_unlock(rdev);
4182
4183         return ret;
4184 }
4185
4186 /**
4187  * regulator_get_current_limit - get regulator output current
4188  * @regulator: regulator source
4189  *
4190  * This returns the current supplied by the specified current sink in uA.
4191  *
4192  * NOTE: If the regulator is disabled it will return the current value. This
4193  * function should not be used to determine regulator state.
4194  */
4195 int regulator_get_current_limit(struct regulator *regulator)
4196 {
4197         return _regulator_get_current_limit(regulator->rdev);
4198 }
4199 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4200
4201 /**
4202  * regulator_set_mode - set regulator operating mode
4203  * @regulator: regulator source
4204  * @mode: operating mode - one of the REGULATOR_MODE constants
4205  *
4206  * Set regulator operating mode to increase regulator efficiency or improve
4207  * regulation performance.
4208  *
4209  * NOTE: Regulator system constraints must be set for this regulator before
4210  * calling this function otherwise this call will fail.
4211  */
4212 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4213 {
4214         struct regulator_dev *rdev = regulator->rdev;
4215         int ret;
4216         int regulator_curr_mode;
4217
4218         regulator_lock(rdev);
4219
4220         /* sanity check */
4221         if (!rdev->desc->ops->set_mode) {
4222                 ret = -EINVAL;
4223                 goto out;
4224         }
4225
4226         /* return if the same mode is requested */
4227         if (rdev->desc->ops->get_mode) {
4228                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4229                 if (regulator_curr_mode == mode) {
4230                         ret = 0;
4231                         goto out;
4232                 }
4233         }
4234
4235         /* constraints check */
4236         ret = regulator_mode_constrain(rdev, &mode);
4237         if (ret < 0)
4238                 goto out;
4239
4240         ret = rdev->desc->ops->set_mode(rdev, mode);
4241 out:
4242         regulator_unlock(rdev);
4243         return ret;
4244 }
4245 EXPORT_SYMBOL_GPL(regulator_set_mode);
4246
4247 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4248 {
4249         /* sanity check */
4250         if (!rdev->desc->ops->get_mode)
4251                 return -EINVAL;
4252
4253         return rdev->desc->ops->get_mode(rdev);
4254 }
4255
4256 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4257 {
4258         int ret;
4259
4260         regulator_lock(rdev);
4261         ret = _regulator_get_mode_unlocked(rdev);
4262         regulator_unlock(rdev);
4263
4264         return ret;
4265 }
4266
4267 /**
4268  * regulator_get_mode - get regulator operating mode
4269  * @regulator: regulator source
4270  *
4271  * Get the current regulator operating mode.
4272  */
4273 unsigned int regulator_get_mode(struct regulator *regulator)
4274 {
4275         return _regulator_get_mode(regulator->rdev);
4276 }
4277 EXPORT_SYMBOL_GPL(regulator_get_mode);
4278
4279 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4280                                         unsigned int *flags)
4281 {
4282         int ret;
4283
4284         regulator_lock(rdev);
4285
4286         /* sanity check */
4287         if (!rdev->desc->ops->get_error_flags) {
4288                 ret = -EINVAL;
4289                 goto out;
4290         }
4291
4292         ret = rdev->desc->ops->get_error_flags(rdev, flags);
4293 out:
4294         regulator_unlock(rdev);
4295         return ret;
4296 }
4297
4298 /**
4299  * regulator_get_error_flags - get regulator error information
4300  * @regulator: regulator source
4301  * @flags: pointer to store error flags
4302  *
4303  * Get the current regulator error information.
4304  */
4305 int regulator_get_error_flags(struct regulator *regulator,
4306                                 unsigned int *flags)
4307 {
4308         return _regulator_get_error_flags(regulator->rdev, flags);
4309 }
4310 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4311
4312 /**
4313  * regulator_set_load - set regulator load
4314  * @regulator: regulator source
4315  * @uA_load: load current
4316  *
4317  * Notifies the regulator core of a new device load. This is then used by
4318  * DRMS (if enabled by constraints) to set the most efficient regulator
4319  * operating mode for the new regulator loading.
4320  *
4321  * Consumer devices notify their supply regulator of the maximum power
4322  * they will require (can be taken from device datasheet in the power
4323  * consumption tables) when they change operational status and hence power
4324  * state. Examples of operational state changes that can affect power
4325  * consumption are :-
4326  *
4327  *    o Device is opened / closed.
4328  *    o Device I/O is about to begin or has just finished.
4329  *    o Device is idling in between work.
4330  *
4331  * This information is also exported via sysfs to userspace.
4332  *
4333  * DRMS will sum the total requested load on the regulator and change
4334  * to the most efficient operating mode if platform constraints allow.
4335  *
4336  * NOTE: when a regulator consumer requests to have a regulator
4337  * disabled then any load that consumer requested no longer counts
4338  * toward the total requested load.  If the regulator is re-enabled
4339  * then the previously requested load will start counting again.
4340  *
4341  * If a regulator is an always-on regulator then an individual consumer's
4342  * load will still be removed if that consumer is fully disabled.
4343  *
4344  * On error a negative errno is returned.
4345  */
4346 int regulator_set_load(struct regulator *regulator, int uA_load)
4347 {
4348         struct regulator_dev *rdev = regulator->rdev;
4349         int old_uA_load;
4350         int ret = 0;
4351
4352         regulator_lock(rdev);
4353         old_uA_load = regulator->uA_load;
4354         regulator->uA_load = uA_load;
4355         if (regulator->enable_count && old_uA_load != uA_load) {
4356                 ret = drms_uA_update(rdev);
4357                 if (ret < 0)
4358                         regulator->uA_load = old_uA_load;
4359         }
4360         regulator_unlock(rdev);
4361
4362         return ret;
4363 }
4364 EXPORT_SYMBOL_GPL(regulator_set_load);
4365
4366 /**
4367  * regulator_allow_bypass - allow the regulator to go into bypass mode
4368  *
4369  * @regulator: Regulator to configure
4370  * @enable: enable or disable bypass mode
4371  *
4372  * Allow the regulator to go into bypass mode if all other consumers
4373  * for the regulator also enable bypass mode and the machine
4374  * constraints allow this.  Bypass mode means that the regulator is
4375  * simply passing the input directly to the output with no regulation.
4376  */
4377 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4378 {
4379         struct regulator_dev *rdev = regulator->rdev;
4380         int ret = 0;
4381
4382         if (!rdev->desc->ops->set_bypass)
4383                 return 0;
4384
4385         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4386                 return 0;
4387
4388         regulator_lock(rdev);
4389
4390         if (enable && !regulator->bypass) {
4391                 rdev->bypass_count++;
4392
4393                 if (rdev->bypass_count == rdev->open_count) {
4394                         ret = rdev->desc->ops->set_bypass(rdev, enable);
4395                         if (ret != 0)
4396                                 rdev->bypass_count--;
4397                 }
4398
4399         } else if (!enable && regulator->bypass) {
4400                 rdev->bypass_count--;
4401
4402                 if (rdev->bypass_count != rdev->open_count) {
4403                         ret = rdev->desc->ops->set_bypass(rdev, enable);
4404                         if (ret != 0)
4405                                 rdev->bypass_count++;
4406                 }
4407         }
4408
4409         if (ret == 0)
4410                 regulator->bypass = enable;
4411
4412         regulator_unlock(rdev);
4413
4414         return ret;
4415 }
4416 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4417
4418 /**
4419  * regulator_register_notifier - register regulator event notifier
4420  * @regulator: regulator source
4421  * @nb: notifier block
4422  *
4423  * Register notifier block to receive regulator events.
4424  */
4425 int regulator_register_notifier(struct regulator *regulator,
4426                               struct notifier_block *nb)
4427 {
4428         return blocking_notifier_chain_register(&regulator->rdev->notifier,
4429                                                 nb);
4430 }
4431 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4432
4433 /**
4434  * regulator_unregister_notifier - unregister regulator event notifier
4435  * @regulator: regulator source
4436  * @nb: notifier block
4437  *
4438  * Unregister regulator event notifier block.
4439  */
4440 int regulator_unregister_notifier(struct regulator *regulator,
4441                                 struct notifier_block *nb)
4442 {
4443         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
4444                                                   nb);
4445 }
4446 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4447
4448 /* notify regulator consumers and downstream regulator consumers.
4449  * Note mutex must be held by caller.
4450  */
4451 static int _notifier_call_chain(struct regulator_dev *rdev,
4452                                   unsigned long event, void *data)
4453 {
4454         /* call rdev chain first */
4455         return blocking_notifier_call_chain(&rdev->notifier, event, data);
4456 }
4457
4458 /**
4459  * regulator_bulk_get - get multiple regulator consumers
4460  *
4461  * @dev:           Device to supply
4462  * @num_consumers: Number of consumers to register
4463  * @consumers:     Configuration of consumers; clients are stored here.
4464  *
4465  * @return 0 on success, an errno on failure.
4466  *
4467  * This helper function allows drivers to get several regulator
4468  * consumers in one operation.  If any of the regulators cannot be
4469  * acquired then any regulators that were allocated will be freed
4470  * before returning to the caller.
4471  */
4472 int regulator_bulk_get(struct device *dev, int num_consumers,
4473                        struct regulator_bulk_data *consumers)
4474 {
4475         int i;
4476         int ret;
4477
4478         for (i = 0; i < num_consumers; i++)
4479                 consumers[i].consumer = NULL;
4480
4481         for (i = 0; i < num_consumers; i++) {
4482                 consumers[i].consumer = regulator_get(dev,
4483                                                       consumers[i].supply);
4484                 if (IS_ERR(consumers[i].consumer)) {
4485                         ret = PTR_ERR(consumers[i].consumer);
4486                         consumers[i].consumer = NULL;
4487                         goto err;
4488                 }
4489         }
4490
4491         return 0;
4492
4493 err:
4494         if (ret != -EPROBE_DEFER)
4495                 dev_err(dev, "Failed to get supply '%s': %d\n",
4496                         consumers[i].supply, ret);
4497         else
4498                 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4499                         consumers[i].supply);
4500
4501         while (--i >= 0)
4502                 regulator_put(consumers[i].consumer);
4503
4504         return ret;
4505 }
4506 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4507
4508 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4509 {
4510         struct regulator_bulk_data *bulk = data;
4511
4512         bulk->ret = regulator_enable(bulk->consumer);
4513 }
4514
4515 /**
4516  * regulator_bulk_enable - enable multiple regulator consumers
4517  *
4518  * @num_consumers: Number of consumers
4519  * @consumers:     Consumer data; clients are stored here.
4520  * @return         0 on success, an errno on failure
4521  *
4522  * This convenience API allows consumers to enable multiple regulator
4523  * clients in a single API call.  If any consumers cannot be enabled
4524  * then any others that were enabled will be disabled again prior to
4525  * return.
4526  */
4527 int regulator_bulk_enable(int num_consumers,
4528                           struct regulator_bulk_data *consumers)
4529 {
4530         ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4531         int i;
4532         int ret = 0;
4533
4534         for (i = 0; i < num_consumers; i++) {
4535                 async_schedule_domain(regulator_bulk_enable_async,
4536                                       &consumers[i], &async_domain);
4537         }
4538
4539         async_synchronize_full_domain(&async_domain);
4540
4541         /* If any consumer failed we need to unwind any that succeeded */
4542         for (i = 0; i < num_consumers; i++) {
4543                 if (consumers[i].ret != 0) {
4544                         ret = consumers[i].ret;
4545                         goto err;
4546                 }
4547         }
4548
4549         return 0;
4550
4551 err:
4552         for (i = 0; i < num_consumers; i++) {
4553                 if (consumers[i].ret < 0)
4554                         pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4555                                consumers[i].ret);
4556                 else
4557                         regulator_disable(consumers[i].consumer);
4558         }
4559
4560         return ret;
4561 }
4562 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4563
4564 /**
4565  * regulator_bulk_disable - disable multiple regulator consumers
4566  *
4567  * @num_consumers: Number of consumers
4568  * @consumers:     Consumer data; clients are stored here.
4569  * @return         0 on success, an errno on failure
4570  *
4571  * This convenience API allows consumers to disable multiple regulator
4572  * clients in a single API call.  If any consumers cannot be disabled
4573  * then any others that were disabled will be enabled again prior to
4574  * return.
4575  */
4576 int regulator_bulk_disable(int num_consumers,
4577                            struct regulator_bulk_data *consumers)
4578 {
4579         int i;
4580         int ret, r;
4581
4582         for (i = num_consumers - 1; i >= 0; --i) {
4583                 ret = regulator_disable(consumers[i].consumer);
4584                 if (ret != 0)
4585                         goto err;
4586         }
4587
4588         return 0;
4589
4590 err:
4591         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4592         for (++i; i < num_consumers; ++i) {
4593                 r = regulator_enable(consumers[i].consumer);
4594                 if (r != 0)
4595                         pr_err("Failed to re-enable %s: %d\n",
4596                                consumers[i].supply, r);
4597         }
4598
4599         return ret;
4600 }
4601 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4602
4603 /**
4604  * regulator_bulk_force_disable - force disable multiple regulator consumers
4605  *
4606  * @num_consumers: Number of consumers
4607  * @consumers:     Consumer data; clients are stored here.
4608  * @return         0 on success, an errno on failure
4609  *
4610  * This convenience API allows consumers to forcibly disable multiple regulator
4611  * clients in a single API call.
4612  * NOTE: This should be used for situations when device damage will
4613  * likely occur if the regulators are not disabled (e.g. over temp).
4614  * Although regulator_force_disable function call for some consumers can
4615  * return error numbers, the function is called for all consumers.
4616  */
4617 int regulator_bulk_force_disable(int num_consumers,
4618                            struct regulator_bulk_data *consumers)
4619 {
4620         int i;
4621         int ret = 0;
4622
4623         for (i = 0; i < num_consumers; i++) {
4624                 consumers[i].ret =
4625                             regulator_force_disable(consumers[i].consumer);
4626
4627                 /* Store first error for reporting */
4628                 if (consumers[i].ret && !ret)
4629                         ret = consumers[i].ret;
4630         }
4631
4632         return ret;
4633 }
4634 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4635
4636 /**
4637  * regulator_bulk_free - free multiple regulator consumers
4638  *
4639  * @num_consumers: Number of consumers
4640  * @consumers:     Consumer data; clients are stored here.
4641  *
4642  * This convenience API allows consumers to free multiple regulator
4643  * clients in a single API call.
4644  */
4645 void regulator_bulk_free(int num_consumers,
4646                          struct regulator_bulk_data *consumers)
4647 {
4648         int i;
4649
4650         for (i = 0; i < num_consumers; i++) {
4651                 regulator_put(consumers[i].consumer);
4652                 consumers[i].consumer = NULL;
4653         }
4654 }
4655 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4656
4657 /**
4658  * regulator_notifier_call_chain - call regulator event notifier
4659  * @rdev: regulator source
4660  * @event: notifier block
4661  * @data: callback-specific data.
4662  *
4663  * Called by regulator drivers to notify clients a regulator event has
4664  * occurred. We also notify regulator clients downstream.
4665  * Note lock must be held by caller.
4666  */
4667 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4668                                   unsigned long event, void *data)
4669 {
4670         lockdep_assert_held_once(&rdev->mutex.base);
4671
4672         _notifier_call_chain(rdev, event, data);
4673         return NOTIFY_DONE;
4674
4675 }
4676 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4677
4678 /**
4679  * regulator_mode_to_status - convert a regulator mode into a status
4680  *
4681  * @mode: Mode to convert
4682  *
4683  * Convert a regulator mode into a status.
4684  */
4685 int regulator_mode_to_status(unsigned int mode)
4686 {
4687         switch (mode) {
4688         case REGULATOR_MODE_FAST:
4689                 return REGULATOR_STATUS_FAST;
4690         case REGULATOR_MODE_NORMAL:
4691                 return REGULATOR_STATUS_NORMAL;
4692         case REGULATOR_MODE_IDLE:
4693                 return REGULATOR_STATUS_IDLE;
4694         case REGULATOR_MODE_STANDBY:
4695                 return REGULATOR_STATUS_STANDBY;
4696         default:
4697                 return REGULATOR_STATUS_UNDEFINED;
4698         }
4699 }
4700 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4701
4702 static struct attribute *regulator_dev_attrs[] = {
4703         &dev_attr_name.attr,
4704         &dev_attr_num_users.attr,
4705         &dev_attr_type.attr,
4706         &dev_attr_microvolts.attr,
4707         &dev_attr_microamps.attr,
4708         &dev_attr_opmode.attr,
4709         &dev_attr_state.attr,
4710         &dev_attr_status.attr,
4711         &dev_attr_bypass.attr,
4712         &dev_attr_requested_microamps.attr,
4713         &dev_attr_min_microvolts.attr,
4714         &dev_attr_max_microvolts.attr,
4715         &dev_attr_min_microamps.attr,
4716         &dev_attr_max_microamps.attr,
4717         &dev_attr_suspend_standby_state.attr,
4718         &dev_attr_suspend_mem_state.attr,
4719         &dev_attr_suspend_disk_state.attr,
4720         &dev_attr_suspend_standby_microvolts.attr,
4721         &dev_attr_suspend_mem_microvolts.attr,
4722         &dev_attr_suspend_disk_microvolts.attr,
4723         &dev_attr_suspend_standby_mode.attr,
4724         &dev_attr_suspend_mem_mode.attr,
4725         &dev_attr_suspend_disk_mode.attr,
4726         NULL
4727 };
4728
4729 /*
4730  * To avoid cluttering sysfs (and memory) with useless state, only
4731  * create attributes that can be meaningfully displayed.
4732  */
4733 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4734                                          struct attribute *attr, int idx)
4735 {
4736         struct device *dev = kobj_to_dev(kobj);
4737         struct regulator_dev *rdev = dev_to_rdev(dev);
4738         const struct regulator_ops *ops = rdev->desc->ops;
4739         umode_t mode = attr->mode;
4740
4741         /* these three are always present */
4742         if (attr == &dev_attr_name.attr ||
4743             attr == &dev_attr_num_users.attr ||
4744             attr == &dev_attr_type.attr)
4745                 return mode;
4746
4747         /* some attributes need specific methods to be displayed */
4748         if (attr == &dev_attr_microvolts.attr) {
4749                 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4750                     (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4751                     (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4752                     (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4753                         return mode;
4754                 return 0;
4755         }
4756
4757         if (attr == &dev_attr_microamps.attr)
4758                 return ops->get_current_limit ? mode : 0;
4759
4760         if (attr == &dev_attr_opmode.attr)
4761                 return ops->get_mode ? mode : 0;
4762
4763         if (attr == &dev_attr_state.attr)
4764                 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4765
4766         if (attr == &dev_attr_status.attr)
4767                 return ops->get_status ? mode : 0;
4768
4769         if (attr == &dev_attr_bypass.attr)
4770                 return ops->get_bypass ? mode : 0;
4771
4772         /* constraints need specific supporting methods */
4773         if (attr == &dev_attr_min_microvolts.attr ||
4774             attr == &dev_attr_max_microvolts.attr)
4775                 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4776
4777         if (attr == &dev_attr_min_microamps.attr ||
4778             attr == &dev_attr_max_microamps.attr)
4779                 return ops->set_current_limit ? mode : 0;
4780
4781         if (attr == &dev_attr_suspend_standby_state.attr ||
4782             attr == &dev_attr_suspend_mem_state.attr ||
4783             attr == &dev_attr_suspend_disk_state.attr)
4784                 return mode;
4785
4786         if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4787             attr == &dev_attr_suspend_mem_microvolts.attr ||
4788             attr == &dev_attr_suspend_disk_microvolts.attr)
4789                 return ops->set_suspend_voltage ? mode : 0;
4790
4791         if (attr == &dev_attr_suspend_standby_mode.attr ||
4792             attr == &dev_attr_suspend_mem_mode.attr ||
4793             attr == &dev_attr_suspend_disk_mode.attr)
4794                 return ops->set_suspend_mode ? mode : 0;
4795
4796         return mode;
4797 }
4798
4799 static const struct attribute_group regulator_dev_group = {
4800         .attrs = regulator_dev_attrs,
4801         .is_visible = regulator_attr_is_visible,
4802 };
4803
4804 static const struct attribute_group *regulator_dev_groups[] = {
4805         &regulator_dev_group,
4806         NULL
4807 };
4808
4809 static void regulator_dev_release(struct device *dev)
4810 {
4811         struct regulator_dev *rdev = dev_get_drvdata(dev);
4812
4813         debugfs_remove_recursive(rdev->debugfs);
4814         kfree(rdev->constraints);
4815         of_node_put(rdev->dev.of_node);
4816         kfree(rdev);
4817 }
4818
4819 static void rdev_init_debugfs(struct regulator_dev *rdev)
4820 {
4821         struct device *parent = rdev->dev.parent;
4822         const char *rname = rdev_get_name(rdev);
4823         char name[NAME_MAX];
4824
4825         /* Avoid duplicate debugfs directory names */
4826         if (parent && rname == rdev->desc->name) {
4827                 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4828                          rname);
4829                 rname = name;
4830         }
4831
4832         rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4833         if (!rdev->debugfs) {
4834                 rdev_warn(rdev, "Failed to create debugfs directory\n");
4835                 return;
4836         }
4837
4838         debugfs_create_u32("use_count", 0444, rdev->debugfs,
4839                            &rdev->use_count);
4840         debugfs_create_u32("open_count", 0444, rdev->debugfs,
4841                            &rdev->open_count);
4842         debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4843                            &rdev->bypass_count);
4844 }
4845
4846 static int regulator_register_resolve_supply(struct device *dev, void *data)
4847 {
4848         struct regulator_dev *rdev = dev_to_rdev(dev);
4849
4850         if (regulator_resolve_supply(rdev))
4851                 rdev_dbg(rdev, "unable to resolve supply\n");
4852
4853         return 0;
4854 }
4855
4856 int regulator_coupler_register(struct regulator_coupler *coupler)
4857 {
4858         mutex_lock(&regulator_list_mutex);
4859         list_add_tail(&coupler->list, &regulator_coupler_list);
4860         mutex_unlock(&regulator_list_mutex);
4861
4862         return 0;
4863 }
4864
4865 static struct regulator_coupler *
4866 regulator_find_coupler(struct regulator_dev *rdev)
4867 {
4868         struct regulator_coupler *coupler;
4869         int err;
4870
4871         /*
4872          * Note that regulators are appended to the list and the generic
4873          * coupler is registered first, hence it will be attached at last
4874          * if nobody cared.
4875          */
4876         list_for_each_entry_reverse(coupler, &regulator_coupler_list, list) {
4877                 err = coupler->attach_regulator(coupler, rdev);
4878                 if (!err) {
4879                         if (!coupler->balance_voltage &&
4880                             rdev->coupling_desc.n_coupled > 2)
4881                                 goto err_unsupported;
4882
4883                         return coupler;
4884                 }
4885
4886                 if (err < 0)
4887                         return ERR_PTR(err);
4888
4889                 if (err == 1)
4890                         continue;
4891
4892                 break;
4893         }
4894
4895         return ERR_PTR(-EINVAL);
4896
4897 err_unsupported:
4898         if (coupler->detach_regulator)
4899                 coupler->detach_regulator(coupler, rdev);
4900
4901         rdev_err(rdev,
4902                 "Voltage balancing for multiple regulator couples is unimplemented\n");
4903
4904         return ERR_PTR(-EPERM);
4905 }
4906
4907 static void regulator_resolve_coupling(struct regulator_dev *rdev)
4908 {
4909         struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4910         struct coupling_desc *c_desc = &rdev->coupling_desc;
4911         int n_coupled = c_desc->n_coupled;
4912         struct regulator_dev *c_rdev;
4913         int i;
4914
4915         for (i = 1; i < n_coupled; i++) {
4916                 /* already resolved */
4917                 if (c_desc->coupled_rdevs[i])
4918                         continue;
4919
4920                 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4921
4922                 if (!c_rdev)
4923                         continue;
4924
4925                 if (c_rdev->coupling_desc.coupler != coupler) {
4926                         rdev_err(rdev, "coupler mismatch with %s\n",
4927                                  rdev_get_name(c_rdev));
4928                         return;
4929                 }
4930
4931                 c_desc->coupled_rdevs[i] = c_rdev;
4932                 c_desc->n_resolved++;
4933
4934                 regulator_resolve_coupling(c_rdev);
4935         }
4936 }
4937
4938 static void regulator_remove_coupling(struct regulator_dev *rdev)
4939 {
4940         struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4941         struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4942         struct regulator_dev *__c_rdev, *c_rdev;
4943         unsigned int __n_coupled, n_coupled;
4944         int i, k;
4945         int err;
4946
4947         n_coupled = c_desc->n_coupled;
4948
4949         for (i = 1; i < n_coupled; i++) {
4950                 c_rdev = c_desc->coupled_rdevs[i];
4951
4952                 if (!c_rdev)
4953                         continue;
4954
4955                 regulator_lock(c_rdev);
4956
4957                 __c_desc = &c_rdev->coupling_desc;
4958                 __n_coupled = __c_desc->n_coupled;
4959
4960                 for (k = 1; k < __n_coupled; k++) {
4961                         __c_rdev = __c_desc->coupled_rdevs[k];
4962
4963                         if (__c_rdev == rdev) {
4964                                 __c_desc->coupled_rdevs[k] = NULL;
4965                                 __c_desc->n_resolved--;
4966                                 break;
4967                         }
4968                 }
4969
4970                 regulator_unlock(c_rdev);
4971
4972                 c_desc->coupled_rdevs[i] = NULL;
4973                 c_desc->n_resolved--;
4974         }
4975
4976         if (coupler && coupler->detach_regulator) {
4977                 err = coupler->detach_regulator(coupler, rdev);
4978                 if (err)
4979                         rdev_err(rdev, "failed to detach from coupler: %d\n",
4980                                  err);
4981         }
4982
4983         kfree(rdev->coupling_desc.coupled_rdevs);
4984         rdev->coupling_desc.coupled_rdevs = NULL;
4985 }
4986
4987 static int regulator_init_coupling(struct regulator_dev *rdev)
4988 {
4989         int err, n_phandles;
4990         size_t alloc_size;
4991
4992         if (!IS_ENABLED(CONFIG_OF))
4993                 n_phandles = 0;
4994         else
4995                 n_phandles = of_get_n_coupled(rdev);
4996
4997         alloc_size = sizeof(*rdev) * (n_phandles + 1);
4998
4999         rdev->coupling_desc.coupled_rdevs = kzalloc(alloc_size, GFP_KERNEL);
5000         if (!rdev->coupling_desc.coupled_rdevs)
5001                 return -ENOMEM;
5002
5003         /*
5004          * Every regulator should always have coupling descriptor filled with
5005          * at least pointer to itself.
5006          */
5007         rdev->coupling_desc.coupled_rdevs[0] = rdev;
5008         rdev->coupling_desc.n_coupled = n_phandles + 1;
5009         rdev->coupling_desc.n_resolved++;
5010
5011         /* regulator isn't coupled */
5012         if (n_phandles == 0)
5013                 return 0;
5014
5015         if (!of_check_coupling_data(rdev))
5016                 return -EPERM;
5017
5018         mutex_lock(&regulator_list_mutex);
5019         rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5020         mutex_unlock(&regulator_list_mutex);
5021
5022         if (IS_ERR(rdev->coupling_desc.coupler)) {
5023                 err = PTR_ERR(rdev->coupling_desc.coupler);
5024                 rdev_err(rdev, "failed to get coupler: %d\n", err);
5025                 return err;
5026         }
5027
5028         return 0;
5029 }
5030
5031 static int generic_coupler_attach(struct regulator_coupler *coupler,
5032                                   struct regulator_dev *rdev)
5033 {
5034         if (rdev->coupling_desc.n_coupled > 2) {
5035                 rdev_err(rdev,
5036                          "Voltage balancing for multiple regulator couples is unimplemented\n");
5037                 return -EPERM;
5038         }
5039
5040         return 0;
5041 }
5042
5043 static struct regulator_coupler generic_regulator_coupler = {
5044         .attach_regulator = generic_coupler_attach,
5045 };
5046
5047 /**
5048  * regulator_register - register regulator
5049  * @regulator_desc: regulator to register
5050  * @cfg: runtime configuration for regulator
5051  *
5052  * Called by regulator drivers to register a regulator.
5053  * Returns a valid pointer to struct regulator_dev on success
5054  * or an ERR_PTR() on error.
5055  */
5056 struct regulator_dev *
5057 regulator_register(const struct regulator_desc *regulator_desc,
5058                    const struct regulator_config *cfg)
5059 {
5060         const struct regulator_init_data *init_data;
5061         struct regulator_config *config = NULL;
5062         static atomic_t regulator_no = ATOMIC_INIT(-1);
5063         struct regulator_dev *rdev;
5064         bool dangling_cfg_gpiod = false;
5065         bool dangling_of_gpiod = false;
5066         struct device *dev;
5067         int ret, i;
5068
5069         if (cfg == NULL)
5070                 return ERR_PTR(-EINVAL);
5071         if (cfg->ena_gpiod)
5072                 dangling_cfg_gpiod = true;
5073         if (regulator_desc == NULL) {
5074                 ret = -EINVAL;
5075                 goto rinse;
5076         }
5077
5078         dev = cfg->dev;
5079         WARN_ON(!dev);
5080
5081         if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5082                 ret = -EINVAL;
5083                 goto rinse;
5084         }
5085
5086         if (regulator_desc->type != REGULATOR_VOLTAGE &&
5087             regulator_desc->type != REGULATOR_CURRENT) {
5088                 ret = -EINVAL;
5089                 goto rinse;
5090         }
5091
5092         /* Only one of each should be implemented */
5093         WARN_ON(regulator_desc->ops->get_voltage &&
5094                 regulator_desc->ops->get_voltage_sel);
5095         WARN_ON(regulator_desc->ops->set_voltage &&
5096                 regulator_desc->ops->set_voltage_sel);
5097
5098         /* If we're using selectors we must implement list_voltage. */
5099         if (regulator_desc->ops->get_voltage_sel &&
5100             !regulator_desc->ops->list_voltage) {
5101                 ret = -EINVAL;
5102                 goto rinse;
5103         }
5104         if (regulator_desc->ops->set_voltage_sel &&
5105             !regulator_desc->ops->list_voltage) {
5106                 ret = -EINVAL;
5107                 goto rinse;
5108         }
5109
5110         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5111         if (rdev == NULL) {
5112                 ret = -ENOMEM;
5113                 goto rinse;
5114         }
5115         device_initialize(&rdev->dev);
5116
5117         /*
5118          * Duplicate the config so the driver could override it after
5119          * parsing init data.
5120          */
5121         config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5122         if (config == NULL) {
5123                 ret = -ENOMEM;
5124                 goto clean;
5125         }
5126
5127         init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5128                                                &rdev->dev.of_node);
5129
5130         /*
5131          * Sometimes not all resources are probed already so we need to take
5132          * that into account. This happens most the time if the ena_gpiod comes
5133          * from a gpio extender or something else.
5134          */
5135         if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5136                 ret = -EPROBE_DEFER;
5137                 goto clean;
5138         }
5139
5140         /*
5141          * We need to keep track of any GPIO descriptor coming from the
5142          * device tree until we have handled it over to the core. If the
5143          * config that was passed in to this function DOES NOT contain
5144          * a descriptor, and the config after this call DOES contain
5145          * a descriptor, we definitely got one from parsing the device
5146          * tree.
5147          */
5148         if (!cfg->ena_gpiod && config->ena_gpiod)
5149                 dangling_of_gpiod = true;
5150         if (!init_data) {
5151                 init_data = config->init_data;
5152                 rdev->dev.of_node = of_node_get(config->of_node);
5153         }
5154
5155         ww_mutex_init(&rdev->mutex, &regulator_ww_class);
5156         rdev->reg_data = config->driver_data;
5157         rdev->owner = regulator_desc->owner;
5158         rdev->desc = regulator_desc;
5159         if (config->regmap)
5160                 rdev->regmap = config->regmap;
5161         else if (dev_get_regmap(dev, NULL))
5162                 rdev->regmap = dev_get_regmap(dev, NULL);
5163         else if (dev->parent)
5164                 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5165         INIT_LIST_HEAD(&rdev->consumer_list);
5166         INIT_LIST_HEAD(&rdev->list);
5167         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5168         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5169
5170         /* preform any regulator specific init */
5171         if (init_data && init_data->regulator_init) {
5172                 ret = init_data->regulator_init(rdev->reg_data);
5173                 if (ret < 0)
5174                         goto clean;
5175         }
5176
5177         if (config->ena_gpiod) {
5178                 ret = regulator_ena_gpio_request(rdev, config);
5179                 if (ret != 0) {
5180                         rdev_err(rdev, "Failed to request enable GPIO: %d\n",
5181                                  ret);
5182                         goto clean;
5183                 }
5184                 /* The regulator core took over the GPIO descriptor */
5185                 dangling_cfg_gpiod = false;
5186                 dangling_of_gpiod = false;
5187         }
5188
5189         /* register with sysfs */
5190         rdev->dev.class = &regulator_class;
5191         rdev->dev.parent = dev;
5192         dev_set_name(&rdev->dev, "regulator.%lu",
5193                     (unsigned long) atomic_inc_return(&regulator_no));
5194         dev_set_drvdata(&rdev->dev, rdev);
5195
5196         /* set regulator constraints */
5197         if (init_data)
5198                 rdev->constraints = kmemdup(&init_data->constraints,
5199                                             sizeof(*rdev->constraints),
5200                                             GFP_KERNEL);
5201         else
5202                 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
5203                                             GFP_KERNEL);
5204         if (!rdev->constraints) {
5205                 ret = -ENOMEM;
5206                 goto wash;
5207         }
5208
5209         if (init_data && init_data->supply_regulator)
5210                 rdev->supply_name = init_data->supply_regulator;
5211         else if (regulator_desc->supply_name)
5212                 rdev->supply_name = regulator_desc->supply_name;
5213
5214         ret = set_machine_constraints(rdev);
5215         if (ret == -EPROBE_DEFER) {
5216                 /* Regulator might be in bypass mode and so needs its supply
5217                  * to set the constraints */
5218                 /* FIXME: this currently triggers a chicken-and-egg problem
5219                  * when creating -SUPPLY symlink in sysfs to a regulator
5220                  * that is just being created */
5221                 ret = regulator_resolve_supply(rdev);
5222                 if (!ret)
5223                         ret = set_machine_constraints(rdev);
5224                 else
5225                         rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
5226                                  ERR_PTR(ret));
5227         }
5228         if (ret < 0)
5229                 goto wash;
5230
5231         ret = regulator_init_coupling(rdev);
5232         if (ret < 0)
5233                 goto wash;
5234
5235         /* add consumers devices */
5236         if (init_data) {
5237                 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5238                         ret = set_consumer_device_supply(rdev,
5239                                 init_data->consumer_supplies[i].dev_name,
5240                                 init_data->consumer_supplies[i].supply);
5241                         if (ret < 0) {
5242                                 dev_err(dev, "Failed to set supply %s\n",
5243                                         init_data->consumer_supplies[i].supply);
5244                                 goto unset_supplies;
5245                         }
5246                 }
5247         }
5248
5249         if (!rdev->desc->ops->get_voltage &&
5250             !rdev->desc->ops->list_voltage &&
5251             !rdev->desc->fixed_uV)
5252                 rdev->is_switch = true;
5253
5254         ret = device_add(&rdev->dev);
5255         if (ret != 0)
5256                 goto unset_supplies;
5257
5258         rdev_init_debugfs(rdev);
5259
5260         /* try to resolve regulators coupling since a new one was registered */
5261         mutex_lock(&regulator_list_mutex);
5262         regulator_resolve_coupling(rdev);
5263         mutex_unlock(&regulator_list_mutex);
5264
5265         /* try to resolve regulators supply since a new one was registered */
5266         class_for_each_device(&regulator_class, NULL, NULL,
5267                               regulator_register_resolve_supply);
5268         kfree(config);
5269         return rdev;
5270
5271 unset_supplies:
5272         mutex_lock(&regulator_list_mutex);
5273         unset_regulator_supplies(rdev);
5274         regulator_remove_coupling(rdev);
5275         mutex_unlock(&regulator_list_mutex);
5276 wash:
5277         regulator_put(rdev->supply);
5278         kfree(rdev->coupling_desc.coupled_rdevs);
5279         mutex_lock(&regulator_list_mutex);
5280         regulator_ena_gpio_free(rdev);
5281         mutex_unlock(&regulator_list_mutex);
5282         put_device(&rdev->dev);
5283         rdev = NULL;
5284 clean:
5285         if (dangling_of_gpiod)
5286                 gpiod_put(config->ena_gpiod);
5287         if (rdev && rdev->dev.of_node)
5288                 of_node_put(rdev->dev.of_node);
5289         kfree(rdev);
5290         kfree(config);
5291 rinse:
5292         if (dangling_cfg_gpiod)
5293                 gpiod_put(cfg->ena_gpiod);
5294         return ERR_PTR(ret);
5295 }
5296 EXPORT_SYMBOL_GPL(regulator_register);
5297
5298 /**
5299  * regulator_unregister - unregister regulator
5300  * @rdev: regulator to unregister
5301  *
5302  * Called by regulator drivers to unregister a regulator.
5303  */
5304 void regulator_unregister(struct regulator_dev *rdev)
5305 {
5306         if (rdev == NULL)
5307                 return;
5308
5309         if (rdev->supply) {
5310                 while (rdev->use_count--)
5311                         regulator_disable(rdev->supply);
5312                 regulator_put(rdev->supply);
5313         }
5314
5315         flush_work(&rdev->disable_work.work);
5316
5317         mutex_lock(&regulator_list_mutex);
5318
5319         WARN_ON(rdev->open_count);
5320         regulator_remove_coupling(rdev);
5321         unset_regulator_supplies(rdev);
5322         list_del(&rdev->list);
5323         regulator_ena_gpio_free(rdev);
5324         device_unregister(&rdev->dev);
5325
5326         mutex_unlock(&regulator_list_mutex);
5327 }
5328 EXPORT_SYMBOL_GPL(regulator_unregister);
5329
5330 #ifdef CONFIG_SUSPEND
5331 /**
5332  * regulator_suspend - prepare regulators for system wide suspend
5333  * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5334  *
5335  * Configure each regulator with it's suspend operating parameters for state.
5336  */
5337 static int regulator_suspend(struct device *dev)
5338 {
5339         struct regulator_dev *rdev = dev_to_rdev(dev);
5340         suspend_state_t state = pm_suspend_target_state;
5341         int ret;
5342
5343         regulator_lock(rdev);
5344         ret = suspend_set_state(rdev, state);
5345         regulator_unlock(rdev);
5346
5347         return ret;
5348 }
5349
5350 static int regulator_resume(struct device *dev)
5351 {
5352         suspend_state_t state = pm_suspend_target_state;
5353         struct regulator_dev *rdev = dev_to_rdev(dev);
5354         struct regulator_state *rstate;
5355         int ret = 0;
5356
5357         rstate = regulator_get_suspend_state(rdev, state);
5358         if (rstate == NULL)
5359                 return 0;
5360
5361         regulator_lock(rdev);
5362
5363         if (rdev->desc->ops->resume &&
5364             (rstate->enabled == ENABLE_IN_SUSPEND ||
5365              rstate->enabled == DISABLE_IN_SUSPEND))
5366                 ret = rdev->desc->ops->resume(rdev);
5367
5368         regulator_unlock(rdev);
5369
5370         return ret;
5371 }
5372 #else /* !CONFIG_SUSPEND */
5373
5374 #define regulator_suspend       NULL
5375 #define regulator_resume        NULL
5376
5377 #endif /* !CONFIG_SUSPEND */
5378
5379 #ifdef CONFIG_PM
5380 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5381         .suspend        = regulator_suspend,
5382         .resume         = regulator_resume,
5383 };
5384 #endif
5385
5386 struct class regulator_class = {
5387         .name = "regulator",
5388         .dev_release = regulator_dev_release,
5389         .dev_groups = regulator_dev_groups,
5390 #ifdef CONFIG_PM
5391         .pm = &regulator_pm_ops,
5392 #endif
5393 };
5394 /**
5395  * regulator_has_full_constraints - the system has fully specified constraints
5396  *
5397  * Calling this function will cause the regulator API to disable all
5398  * regulators which have a zero use count and don't have an always_on
5399  * constraint in a late_initcall.
5400  *
5401  * The intention is that this will become the default behaviour in a
5402  * future kernel release so users are encouraged to use this facility
5403  * now.
5404  */
5405 void regulator_has_full_constraints(void)
5406 {
5407         has_full_constraints = 1;
5408 }
5409 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5410
5411 /**
5412  * rdev_get_drvdata - get rdev regulator driver data
5413  * @rdev: regulator
5414  *
5415  * Get rdev regulator driver private data. This call can be used in the
5416  * regulator driver context.
5417  */
5418 void *rdev_get_drvdata(struct regulator_dev *rdev)
5419 {
5420         return rdev->reg_data;
5421 }
5422 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5423
5424 /**
5425  * regulator_get_drvdata - get regulator driver data
5426  * @regulator: regulator
5427  *
5428  * Get regulator driver private data. This call can be used in the consumer
5429  * driver context when non API regulator specific functions need to be called.
5430  */
5431 void *regulator_get_drvdata(struct regulator *regulator)
5432 {
5433         return regulator->rdev->reg_data;
5434 }
5435 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5436
5437 /**
5438  * regulator_set_drvdata - set regulator driver data
5439  * @regulator: regulator
5440  * @data: data
5441  */
5442 void regulator_set_drvdata(struct regulator *regulator, void *data)
5443 {
5444         regulator->rdev->reg_data = data;
5445 }
5446 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5447
5448 /**
5449  * regulator_get_id - get regulator ID
5450  * @rdev: regulator
5451  */
5452 int rdev_get_id(struct regulator_dev *rdev)
5453 {
5454         return rdev->desc->id;
5455 }
5456 EXPORT_SYMBOL_GPL(rdev_get_id);
5457
5458 struct device *rdev_get_dev(struct regulator_dev *rdev)
5459 {
5460         return &rdev->dev;
5461 }
5462 EXPORT_SYMBOL_GPL(rdev_get_dev);
5463
5464 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5465 {
5466         return rdev->regmap;
5467 }
5468 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5469
5470 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5471 {
5472         return reg_init_data->driver_data;
5473 }
5474 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5475
5476 #ifdef CONFIG_DEBUG_FS
5477 static int supply_map_show(struct seq_file *sf, void *data)
5478 {
5479         struct regulator_map *map;
5480
5481         list_for_each_entry(map, &regulator_map_list, list) {
5482                 seq_printf(sf, "%s -> %s.%s\n",
5483                                 rdev_get_name(map->regulator), map->dev_name,
5484                                 map->supply);
5485         }
5486
5487         return 0;
5488 }
5489 DEFINE_SHOW_ATTRIBUTE(supply_map);
5490
5491 struct summary_data {
5492         struct seq_file *s;
5493         struct regulator_dev *parent;
5494         int level;
5495 };
5496
5497 static void regulator_summary_show_subtree(struct seq_file *s,
5498                                            struct regulator_dev *rdev,
5499                                            int level);
5500
5501 static int regulator_summary_show_children(struct device *dev, void *data)
5502 {
5503         struct regulator_dev *rdev = dev_to_rdev(dev);
5504         struct summary_data *summary_data = data;
5505
5506         if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5507                 regulator_summary_show_subtree(summary_data->s, rdev,
5508                                                summary_data->level + 1);
5509
5510         return 0;
5511 }
5512
5513 static void regulator_summary_show_subtree(struct seq_file *s,
5514                                            struct regulator_dev *rdev,
5515                                            int level)
5516 {
5517         struct regulation_constraints *c;
5518         struct regulator *consumer;
5519         struct summary_data summary_data;
5520         unsigned int opmode;
5521
5522         if (!rdev)
5523                 return;
5524
5525         opmode = _regulator_get_mode_unlocked(rdev);
5526         seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5527                    level * 3 + 1, "",
5528                    30 - level * 3, rdev_get_name(rdev),
5529                    rdev->use_count, rdev->open_count, rdev->bypass_count,
5530                    regulator_opmode_to_str(opmode));
5531
5532         seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5533         seq_printf(s, "%5dmA ",
5534                    _regulator_get_current_limit_unlocked(rdev) / 1000);
5535
5536         c = rdev->constraints;
5537         if (c) {
5538                 switch (rdev->desc->type) {
5539                 case REGULATOR_VOLTAGE:
5540                         seq_printf(s, "%5dmV %5dmV ",
5541                                    c->min_uV / 1000, c->max_uV / 1000);
5542                         break;
5543                 case REGULATOR_CURRENT:
5544                         seq_printf(s, "%5dmA %5dmA ",
5545                                    c->min_uA / 1000, c->max_uA / 1000);
5546                         break;
5547                 }
5548         }
5549
5550         seq_puts(s, "\n");
5551
5552         list_for_each_entry(consumer, &rdev->consumer_list, list) {
5553                 if (consumer->dev && consumer->dev->class == &regulator_class)
5554                         continue;
5555
5556                 seq_printf(s, "%*s%-*s ",
5557                            (level + 1) * 3 + 1, "",
5558                            30 - (level + 1) * 3,
5559                            consumer->dev ? dev_name(consumer->dev) : "deviceless");
5560
5561                 switch (rdev->desc->type) {
5562                 case REGULATOR_VOLTAGE:
5563                         seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5564                                    consumer->enable_count,
5565                                    consumer->uA_load / 1000,
5566                                    consumer->uA_load && !consumer->enable_count ?
5567                                    '*' : ' ',
5568                                    consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5569                                    consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5570                         break;
5571                 case REGULATOR_CURRENT:
5572                         break;
5573                 }
5574
5575                 seq_puts(s, "\n");
5576         }
5577
5578         summary_data.s = s;
5579         summary_data.level = level;
5580         summary_data.parent = rdev;
5581
5582         class_for_each_device(&regulator_class, NULL, &summary_data,
5583                               regulator_summary_show_children);
5584 }
5585
5586 struct summary_lock_data {
5587         struct ww_acquire_ctx *ww_ctx;
5588         struct regulator_dev **new_contended_rdev;
5589         struct regulator_dev **old_contended_rdev;
5590 };
5591
5592 static int regulator_summary_lock_one(struct device *dev, void *data)
5593 {
5594         struct regulator_dev *rdev = dev_to_rdev(dev);
5595         struct summary_lock_data *lock_data = data;
5596         int ret = 0;
5597
5598         if (rdev != *lock_data->old_contended_rdev) {
5599                 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5600
5601                 if (ret == -EDEADLK)
5602                         *lock_data->new_contended_rdev = rdev;
5603                 else
5604                         WARN_ON_ONCE(ret);
5605         } else {
5606                 *lock_data->old_contended_rdev = NULL;
5607         }
5608
5609         return ret;
5610 }
5611
5612 static int regulator_summary_unlock_one(struct device *dev, void *data)
5613 {
5614         struct regulator_dev *rdev = dev_to_rdev(dev);
5615         struct summary_lock_data *lock_data = data;
5616
5617         if (lock_data) {
5618                 if (rdev == *lock_data->new_contended_rdev)
5619                         return -EDEADLK;
5620         }
5621
5622         regulator_unlock(rdev);
5623
5624         return 0;
5625 }
5626
5627 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5628                                       struct regulator_dev **new_contended_rdev,
5629                                       struct regulator_dev **old_contended_rdev)
5630 {
5631         struct summary_lock_data lock_data;
5632         int ret;
5633
5634         lock_data.ww_ctx = ww_ctx;
5635         lock_data.new_contended_rdev = new_contended_rdev;
5636         lock_data.old_contended_rdev = old_contended_rdev;
5637
5638         ret = class_for_each_device(&regulator_class, NULL, &lock_data,
5639                                     regulator_summary_lock_one);
5640         if (ret)
5641                 class_for_each_device(&regulator_class, NULL, &lock_data,
5642                                       regulator_summary_unlock_one);
5643
5644         return ret;
5645 }
5646
5647 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5648 {
5649         struct regulator_dev *new_contended_rdev = NULL;
5650         struct regulator_dev *old_contended_rdev = NULL;
5651         int err;
5652
5653         mutex_lock(&regulator_list_mutex);
5654
5655         ww_acquire_init(ww_ctx, &regulator_ww_class);
5656
5657         do {
5658                 if (new_contended_rdev) {
5659                         ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5660                         old_contended_rdev = new_contended_rdev;
5661                         old_contended_rdev->ref_cnt++;
5662                 }
5663
5664                 err = regulator_summary_lock_all(ww_ctx,
5665                                                  &new_contended_rdev,
5666                                                  &old_contended_rdev);
5667
5668                 if (old_contended_rdev)
5669                         regulator_unlock(old_contended_rdev);
5670
5671         } while (err == -EDEADLK);
5672
5673         ww_acquire_done(ww_ctx);
5674 }
5675
5676 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5677 {
5678         class_for_each_device(&regulator_class, NULL, NULL,
5679                               regulator_summary_unlock_one);
5680         ww_acquire_fini(ww_ctx);
5681
5682         mutex_unlock(&regulator_list_mutex);
5683 }
5684
5685 static int regulator_summary_show_roots(struct device *dev, void *data)
5686 {
5687         struct regulator_dev *rdev = dev_to_rdev(dev);
5688         struct seq_file *s = data;
5689
5690         if (!rdev->supply)
5691                 regulator_summary_show_subtree(s, rdev, 0);
5692
5693         return 0;
5694 }
5695
5696 static int regulator_summary_show(struct seq_file *s, void *data)
5697 {
5698         struct ww_acquire_ctx ww_ctx;
5699
5700         seq_puts(s, " regulator                      use open bypass  opmode voltage current     min     max\n");
5701         seq_puts(s, "---------------------------------------------------------------------------------------\n");
5702
5703         regulator_summary_lock(&ww_ctx);
5704
5705         class_for_each_device(&regulator_class, NULL, s,
5706                               regulator_summary_show_roots);
5707
5708         regulator_summary_unlock(&ww_ctx);
5709
5710         return 0;
5711 }
5712 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5713 #endif /* CONFIG_DEBUG_FS */
5714
5715 static int __init regulator_init(void)
5716 {
5717         int ret;
5718
5719         ret = class_register(&regulator_class);
5720
5721         debugfs_root = debugfs_create_dir("regulator", NULL);
5722         if (!debugfs_root)
5723                 pr_warn("regulator: Failed to create debugfs directory\n");
5724
5725 #ifdef CONFIG_DEBUG_FS
5726         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5727                             &supply_map_fops);
5728
5729         debugfs_create_file("regulator_summary", 0444, debugfs_root,
5730                             NULL, &regulator_summary_fops);
5731 #endif
5732         regulator_dummy_init();
5733
5734         regulator_coupler_register(&generic_regulator_coupler);
5735
5736         return ret;
5737 }
5738
5739 /* init early to allow our consumers to complete system booting */
5740 core_initcall(regulator_init);
5741
5742 static int regulator_late_cleanup(struct device *dev, void *data)
5743 {
5744         struct regulator_dev *rdev = dev_to_rdev(dev);
5745         const struct regulator_ops *ops = rdev->desc->ops;
5746         struct regulation_constraints *c = rdev->constraints;
5747         int enabled, ret;
5748
5749         if (c && c->always_on)
5750                 return 0;
5751
5752         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5753                 return 0;
5754
5755         regulator_lock(rdev);
5756
5757         if (rdev->use_count)
5758                 goto unlock;
5759
5760         /* If we can't read the status assume it's on. */
5761         if (ops->is_enabled)
5762                 enabled = ops->is_enabled(rdev);
5763         else
5764                 enabled = 1;
5765
5766         if (!enabled)
5767                 goto unlock;
5768
5769         if (have_full_constraints()) {
5770                 /* We log since this may kill the system if it goes
5771                  * wrong. */
5772                 rdev_info(rdev, "disabling\n");
5773                 ret = _regulator_do_disable(rdev);
5774                 if (ret != 0)
5775                         rdev_err(rdev, "couldn't disable: %d\n", ret);
5776         } else {
5777                 /* The intention is that in future we will
5778                  * assume that full constraints are provided
5779                  * so warn even if we aren't going to do
5780                  * anything here.
5781                  */
5782                 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5783         }
5784
5785 unlock:
5786         regulator_unlock(rdev);
5787
5788         return 0;
5789 }
5790
5791 static void regulator_init_complete_work_function(struct work_struct *work)
5792 {
5793         /*
5794          * Regulators may had failed to resolve their input supplies
5795          * when were registered, either because the input supply was
5796          * not registered yet or because its parent device was not
5797          * bound yet. So attempt to resolve the input supplies for
5798          * pending regulators before trying to disable unused ones.
5799          */
5800         class_for_each_device(&regulator_class, NULL, NULL,
5801                               regulator_register_resolve_supply);
5802
5803         /* If we have a full configuration then disable any regulators
5804          * we have permission to change the status for and which are
5805          * not in use or always_on.  This is effectively the default
5806          * for DT and ACPI as they have full constraints.
5807          */
5808         class_for_each_device(&regulator_class, NULL, NULL,
5809                               regulator_late_cleanup);
5810 }
5811
5812 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
5813                             regulator_init_complete_work_function);
5814
5815 static int __init regulator_init_complete(void)
5816 {
5817         /*
5818          * Since DT doesn't provide an idiomatic mechanism for
5819          * enabling full constraints and since it's much more natural
5820          * with DT to provide them just assume that a DT enabled
5821          * system has full constraints.
5822          */
5823         if (of_have_populated_dt())
5824                 has_full_constraints = true;
5825
5826         /*
5827          * We punt completion for an arbitrary amount of time since
5828          * systems like distros will load many drivers from userspace
5829          * so consumers might not always be ready yet, this is
5830          * particularly an issue with laptops where this might bounce
5831          * the display off then on.  Ideally we'd get a notification
5832          * from userspace when this happens but we don't so just wait
5833          * a bit and hope we waited long enough.  It'd be better if
5834          * we'd only do this on systems that need it, and a kernel
5835          * command line option might be useful.
5836          */
5837         schedule_delayed_work(&regulator_init_complete_work,
5838                               msecs_to_jiffies(30000));
5839
5840         return 0;
5841 }
5842 late_initcall_sync(regulator_init_complete);