OSDN Git Service

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