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