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