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