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