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