GNU Linux-libre 4.19.245-gnu1
[releases.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
1215                 if (rdev->constraints->always_on)
1216                         rdev->use_count++;
1217         }
1218
1219         print_constraints(rdev);
1220         return 0;
1221 }
1222
1223 /**
1224  * set_supply - set regulator supply regulator
1225  * @rdev: regulator name
1226  * @supply_rdev: supply regulator name
1227  *
1228  * Called by platform initialisation code to set the supply regulator for this
1229  * regulator. This ensures that a regulators supply will also be enabled by the
1230  * core if it's child is enabled.
1231  */
1232 static int set_supply(struct regulator_dev *rdev,
1233                       struct regulator_dev *supply_rdev)
1234 {
1235         int err;
1236
1237         rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1238
1239         if (!try_module_get(supply_rdev->owner))
1240                 return -ENODEV;
1241
1242         rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1243         if (rdev->supply == NULL) {
1244                 err = -ENOMEM;
1245                 return err;
1246         }
1247         supply_rdev->open_count++;
1248
1249         return 0;
1250 }
1251
1252 /**
1253  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1254  * @rdev:         regulator source
1255  * @consumer_dev_name: dev_name() string for device supply applies to
1256  * @supply:       symbolic name for supply
1257  *
1258  * Allows platform initialisation code to map physical regulator
1259  * sources to symbolic names for supplies for use by devices.  Devices
1260  * should use these symbolic names to request regulators, avoiding the
1261  * need to provide board-specific regulator names as platform data.
1262  */
1263 static int set_consumer_device_supply(struct regulator_dev *rdev,
1264                                       const char *consumer_dev_name,
1265                                       const char *supply)
1266 {
1267         struct regulator_map *node, *new_node;
1268         int has_dev;
1269
1270         if (supply == NULL)
1271                 return -EINVAL;
1272
1273         if (consumer_dev_name != NULL)
1274                 has_dev = 1;
1275         else
1276                 has_dev = 0;
1277
1278         new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1279         if (new_node == NULL)
1280                 return -ENOMEM;
1281
1282         new_node->regulator = rdev;
1283         new_node->supply = supply;
1284
1285         if (has_dev) {
1286                 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1287                 if (new_node->dev_name == NULL) {
1288                         kfree(new_node);
1289                         return -ENOMEM;
1290                 }
1291         }
1292
1293         mutex_lock(&regulator_list_mutex);
1294         list_for_each_entry(node, &regulator_map_list, list) {
1295                 if (node->dev_name && consumer_dev_name) {
1296                         if (strcmp(node->dev_name, consumer_dev_name) != 0)
1297                                 continue;
1298                 } else if (node->dev_name || consumer_dev_name) {
1299                         continue;
1300                 }
1301
1302                 if (strcmp(node->supply, supply) != 0)
1303                         continue;
1304
1305                 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1306                          consumer_dev_name,
1307                          dev_name(&node->regulator->dev),
1308                          node->regulator->desc->name,
1309                          supply,
1310                          dev_name(&rdev->dev), rdev_get_name(rdev));
1311                 goto fail;
1312         }
1313
1314         list_add(&new_node->list, &regulator_map_list);
1315         mutex_unlock(&regulator_list_mutex);
1316
1317         return 0;
1318
1319 fail:
1320         mutex_unlock(&regulator_list_mutex);
1321         kfree(new_node->dev_name);
1322         kfree(new_node);
1323         return -EBUSY;
1324 }
1325
1326 static void unset_regulator_supplies(struct regulator_dev *rdev)
1327 {
1328         struct regulator_map *node, *n;
1329
1330         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1331                 if (rdev == node->regulator) {
1332                         list_del(&node->list);
1333                         kfree(node->dev_name);
1334                         kfree(node);
1335                 }
1336         }
1337 }
1338
1339 #ifdef CONFIG_DEBUG_FS
1340 static ssize_t constraint_flags_read_file(struct file *file,
1341                                           char __user *user_buf,
1342                                           size_t count, loff_t *ppos)
1343 {
1344         const struct regulator *regulator = file->private_data;
1345         const struct regulation_constraints *c = regulator->rdev->constraints;
1346         char *buf;
1347         ssize_t ret;
1348
1349         if (!c)
1350                 return 0;
1351
1352         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1353         if (!buf)
1354                 return -ENOMEM;
1355
1356         ret = snprintf(buf, PAGE_SIZE,
1357                         "always_on: %u\n"
1358                         "boot_on: %u\n"
1359                         "apply_uV: %u\n"
1360                         "ramp_disable: %u\n"
1361                         "soft_start: %u\n"
1362                         "pull_down: %u\n"
1363                         "over_current_protection: %u\n",
1364                         c->always_on,
1365                         c->boot_on,
1366                         c->apply_uV,
1367                         c->ramp_disable,
1368                         c->soft_start,
1369                         c->pull_down,
1370                         c->over_current_protection);
1371
1372         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1373         kfree(buf);
1374
1375         return ret;
1376 }
1377
1378 #endif
1379
1380 static const struct file_operations constraint_flags_fops = {
1381 #ifdef CONFIG_DEBUG_FS
1382         .open = simple_open,
1383         .read = constraint_flags_read_file,
1384         .llseek = default_llseek,
1385 #endif
1386 };
1387
1388 #define REG_STR_SIZE    64
1389
1390 static struct regulator *create_regulator(struct regulator_dev *rdev,
1391                                           struct device *dev,
1392                                           const char *supply_name)
1393 {
1394         struct regulator *regulator;
1395         char buf[REG_STR_SIZE];
1396         int err, size;
1397
1398         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1399         if (regulator == NULL)
1400                 return NULL;
1401
1402         regulator_lock(rdev);
1403         regulator->rdev = rdev;
1404         list_add(&regulator->list, &rdev->consumer_list);
1405
1406         if (dev) {
1407                 regulator->dev = dev;
1408
1409                 /* Add a link to the device sysfs entry */
1410                 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1411                                 dev->kobj.name, supply_name);
1412                 if (size >= REG_STR_SIZE)
1413                         goto overflow_err;
1414
1415                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1416                 if (regulator->supply_name == NULL)
1417                         goto overflow_err;
1418
1419                 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1420                                         buf);
1421                 if (err) {
1422                         rdev_dbg(rdev, "could not add device link %s err %d\n",
1423                                   dev->kobj.name, err);
1424                         /* non-fatal */
1425                 }
1426         } else {
1427                 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1428                 if (regulator->supply_name == NULL)
1429                         goto overflow_err;
1430         }
1431
1432         regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1433                                                 rdev->debugfs);
1434         if (!regulator->debugfs) {
1435                 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1436         } else {
1437                 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1438                                    &regulator->uA_load);
1439                 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1440                                    &regulator->voltage[PM_SUSPEND_ON].min_uV);
1441                 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1442                                    &regulator->voltage[PM_SUSPEND_ON].max_uV);
1443                 debugfs_create_file("constraint_flags", 0444,
1444                                     regulator->debugfs, regulator,
1445                                     &constraint_flags_fops);
1446         }
1447
1448         /*
1449          * Check now if the regulator is an always on regulator - if
1450          * it is then we don't need to do nearly so much work for
1451          * enable/disable calls.
1452          */
1453         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1454             _regulator_is_enabled(rdev))
1455                 regulator->always_on = true;
1456
1457         regulator_unlock(rdev);
1458         return regulator;
1459 overflow_err:
1460         list_del(&regulator->list);
1461         kfree(regulator);
1462         regulator_unlock(rdev);
1463         return NULL;
1464 }
1465
1466 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1467 {
1468         if (rdev->constraints && rdev->constraints->enable_time)
1469                 return rdev->constraints->enable_time;
1470         if (!rdev->desc->ops->enable_time)
1471                 return rdev->desc->enable_time;
1472         return rdev->desc->ops->enable_time(rdev);
1473 }
1474
1475 static struct regulator_supply_alias *regulator_find_supply_alias(
1476                 struct device *dev, const char *supply)
1477 {
1478         struct regulator_supply_alias *map;
1479
1480         list_for_each_entry(map, &regulator_supply_alias_list, list)
1481                 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1482                         return map;
1483
1484         return NULL;
1485 }
1486
1487 static void regulator_supply_alias(struct device **dev, const char **supply)
1488 {
1489         struct regulator_supply_alias *map;
1490
1491         map = regulator_find_supply_alias(*dev, *supply);
1492         if (map) {
1493                 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1494                                 *supply, map->alias_supply,
1495                                 dev_name(map->alias_dev));
1496                 *dev = map->alias_dev;
1497                 *supply = map->alias_supply;
1498         }
1499 }
1500
1501 static int regulator_match(struct device *dev, const void *data)
1502 {
1503         struct regulator_dev *r = dev_to_rdev(dev);
1504
1505         return strcmp(rdev_get_name(r), data) == 0;
1506 }
1507
1508 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1509 {
1510         struct device *dev;
1511
1512         dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1513
1514         return dev ? dev_to_rdev(dev) : NULL;
1515 }
1516
1517 /**
1518  * regulator_dev_lookup - lookup a regulator device.
1519  * @dev: device for regulator "consumer".
1520  * @supply: Supply name or regulator ID.
1521  *
1522  * If successful, returns a struct regulator_dev that corresponds to the name
1523  * @supply and with the embedded struct device refcount incremented by one.
1524  * The refcount must be dropped by calling put_device().
1525  * On failure one of the following ERR-PTR-encoded values is returned:
1526  * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1527  * in the future.
1528  */
1529 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1530                                                   const char *supply)
1531 {
1532         struct regulator_dev *r = NULL;
1533         struct device_node *node;
1534         struct regulator_map *map;
1535         const char *devname = NULL;
1536
1537         regulator_supply_alias(&dev, &supply);
1538
1539         /* first do a dt based lookup */
1540         if (dev && dev->of_node) {
1541                 node = of_get_regulator(dev, supply);
1542                 if (node) {
1543                         r = of_find_regulator_by_node(node);
1544                         if (r)
1545                                 return r;
1546
1547                         /*
1548                          * We have a node, but there is no device.
1549                          * assume it has not registered yet.
1550                          */
1551                         return ERR_PTR(-EPROBE_DEFER);
1552                 }
1553         }
1554
1555         /* if not found, try doing it non-dt way */
1556         if (dev)
1557                 devname = dev_name(dev);
1558
1559         mutex_lock(&regulator_list_mutex);
1560         list_for_each_entry(map, &regulator_map_list, list) {
1561                 /* If the mapping has a device set up it must match */
1562                 if (map->dev_name &&
1563                     (!devname || strcmp(map->dev_name, devname)))
1564                         continue;
1565
1566                 if (strcmp(map->supply, supply) == 0 &&
1567                     get_device(&map->regulator->dev)) {
1568                         r = map->regulator;
1569                         break;
1570                 }
1571         }
1572         mutex_unlock(&regulator_list_mutex);
1573
1574         if (r)
1575                 return r;
1576
1577         r = regulator_lookup_by_name(supply);
1578         if (r)
1579                 return r;
1580
1581         return ERR_PTR(-ENODEV);
1582 }
1583
1584 static int regulator_resolve_supply(struct regulator_dev *rdev)
1585 {
1586         struct regulator_dev *r;
1587         struct device *dev = rdev->dev.parent;
1588         int ret = 0;
1589
1590         /* No supply to resovle? */
1591         if (!rdev->supply_name)
1592                 return 0;
1593
1594         /* Supply already resolved? (fast-path without locking contention) */
1595         if (rdev->supply)
1596                 return 0;
1597
1598         r = regulator_dev_lookup(dev, rdev->supply_name);
1599         if (IS_ERR(r)) {
1600                 ret = PTR_ERR(r);
1601
1602                 /* Did the lookup explicitly defer for us? */
1603                 if (ret == -EPROBE_DEFER)
1604                         goto out;
1605
1606                 if (have_full_constraints()) {
1607                         r = dummy_regulator_rdev;
1608                         get_device(&r->dev);
1609                 } else {
1610                         dev_err(dev, "Failed to resolve %s-supply for %s\n",
1611                                 rdev->supply_name, rdev->desc->name);
1612                         ret = -EPROBE_DEFER;
1613                         goto out;
1614                 }
1615         }
1616
1617         if (r == rdev) {
1618                 dev_err(dev, "Supply for %s (%s) resolved to itself\n",
1619                         rdev->desc->name, rdev->supply_name);
1620                 if (!have_full_constraints()) {
1621                         ret = -EINVAL;
1622                         goto out;
1623                 }
1624                 r = dummy_regulator_rdev;
1625                 get_device(&r->dev);
1626         }
1627
1628         /*
1629          * If the supply's parent device is not the same as the
1630          * regulator's parent device, then ensure the parent device
1631          * is bound before we resolve the supply, in case the parent
1632          * device get probe deferred and unregisters the supply.
1633          */
1634         if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1635                 if (!device_is_bound(r->dev.parent)) {
1636                         put_device(&r->dev);
1637                         ret = -EPROBE_DEFER;
1638                         goto out;
1639                 }
1640         }
1641
1642         /* Recursively resolve the supply of the supply */
1643         ret = regulator_resolve_supply(r);
1644         if (ret < 0) {
1645                 put_device(&r->dev);
1646                 goto out;
1647         }
1648
1649         /*
1650          * Recheck rdev->supply with rdev->mutex lock held to avoid a race
1651          * between rdev->supply null check and setting rdev->supply in
1652          * set_supply() from concurrent tasks.
1653          */
1654         regulator_lock(rdev);
1655
1656         /* Supply just resolved by a concurrent task? */
1657         if (rdev->supply) {
1658                 regulator_unlock(rdev);
1659                 put_device(&r->dev);
1660                 goto out;
1661         }
1662
1663         ret = set_supply(rdev, r);
1664         if (ret < 0) {
1665                 regulator_unlock(rdev);
1666                 put_device(&r->dev);
1667                 goto out;
1668         }
1669
1670         regulator_unlock(rdev);
1671
1672         /*
1673          * In set_machine_constraints() we may have turned this regulator on
1674          * but we couldn't propagate to the supply if it hadn't been resolved
1675          * yet.  Do it now.
1676          */
1677         if (rdev->use_count) {
1678                 ret = regulator_enable(rdev->supply);
1679                 if (ret < 0) {
1680                         _regulator_put(rdev->supply);
1681                         rdev->supply = NULL;
1682                         goto out;
1683                 }
1684         }
1685
1686 out:
1687         return ret;
1688 }
1689
1690 /* Internal regulator request function */
1691 struct regulator *_regulator_get(struct device *dev, const char *id,
1692                                  enum regulator_get_type get_type)
1693 {
1694         struct regulator_dev *rdev;
1695         struct regulator *regulator;
1696         const char *devname = dev ? dev_name(dev) : "deviceless";
1697         int ret;
1698
1699         if (get_type >= MAX_GET_TYPE) {
1700                 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1701                 return ERR_PTR(-EINVAL);
1702         }
1703
1704         if (id == NULL) {
1705                 pr_err("get() with no identifier\n");
1706                 return ERR_PTR(-EINVAL);
1707         }
1708
1709         rdev = regulator_dev_lookup(dev, id);
1710         if (IS_ERR(rdev)) {
1711                 ret = PTR_ERR(rdev);
1712
1713                 /*
1714                  * If regulator_dev_lookup() fails with error other
1715                  * than -ENODEV our job here is done, we simply return it.
1716                  */
1717                 if (ret != -ENODEV)
1718                         return ERR_PTR(ret);
1719
1720                 if (!have_full_constraints()) {
1721                         dev_warn(dev,
1722                                  "incomplete constraints, dummy supplies not allowed\n");
1723                         return ERR_PTR(-ENODEV);
1724                 }
1725
1726                 switch (get_type) {
1727                 case NORMAL_GET:
1728                         /*
1729                          * Assume that a regulator is physically present and
1730                          * enabled, even if it isn't hooked up, and just
1731                          * provide a dummy.
1732                          */
1733                         dev_warn(dev,
1734                                  "%s supply %s not found, using dummy regulator\n",
1735                                  devname, id);
1736                         rdev = dummy_regulator_rdev;
1737                         get_device(&rdev->dev);
1738                         break;
1739
1740                 case EXCLUSIVE_GET:
1741                         dev_warn(dev,
1742                                  "dummy supplies not allowed for exclusive requests\n");
1743                         /* fall through */
1744
1745                 default:
1746                         return ERR_PTR(-ENODEV);
1747                 }
1748         }
1749
1750         if (rdev->exclusive) {
1751                 regulator = ERR_PTR(-EPERM);
1752                 put_device(&rdev->dev);
1753                 return regulator;
1754         }
1755
1756         if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1757                 regulator = ERR_PTR(-EBUSY);
1758                 put_device(&rdev->dev);
1759                 return regulator;
1760         }
1761
1762         ret = regulator_resolve_supply(rdev);
1763         if (ret < 0) {
1764                 regulator = ERR_PTR(ret);
1765                 put_device(&rdev->dev);
1766                 return regulator;
1767         }
1768
1769         if (!try_module_get(rdev->owner)) {
1770                 regulator = ERR_PTR(-EPROBE_DEFER);
1771                 put_device(&rdev->dev);
1772                 return regulator;
1773         }
1774
1775         regulator = create_regulator(rdev, dev, id);
1776         if (regulator == NULL) {
1777                 regulator = ERR_PTR(-ENOMEM);
1778                 module_put(rdev->owner);
1779                 put_device(&rdev->dev);
1780                 return regulator;
1781         }
1782
1783         rdev->open_count++;
1784         if (get_type == EXCLUSIVE_GET) {
1785                 rdev->exclusive = 1;
1786
1787                 ret = _regulator_is_enabled(rdev);
1788                 if (ret > 0)
1789                         rdev->use_count = 1;
1790                 else
1791                         rdev->use_count = 0;
1792         }
1793
1794         device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1795
1796         return regulator;
1797 }
1798
1799 /**
1800  * regulator_get - lookup and obtain a reference to a regulator.
1801  * @dev: device for regulator "consumer"
1802  * @id: Supply name or regulator ID.
1803  *
1804  * Returns a struct regulator corresponding to the regulator producer,
1805  * or IS_ERR() condition containing errno.
1806  *
1807  * Use of supply names configured via regulator_set_device_supply() is
1808  * strongly encouraged.  It is recommended that the supply name used
1809  * should match the name used for the supply and/or the relevant
1810  * device pins in the datasheet.
1811  */
1812 struct regulator *regulator_get(struct device *dev, const char *id)
1813 {
1814         return _regulator_get(dev, id, NORMAL_GET);
1815 }
1816 EXPORT_SYMBOL_GPL(regulator_get);
1817
1818 /**
1819  * regulator_get_exclusive - obtain exclusive access to a regulator.
1820  * @dev: device for regulator "consumer"
1821  * @id: Supply name or regulator ID.
1822  *
1823  * Returns a struct regulator corresponding to the regulator producer,
1824  * or IS_ERR() condition containing errno.  Other consumers will be
1825  * unable to obtain this regulator while this reference is held and the
1826  * use count for the regulator will be initialised to reflect the current
1827  * state of the regulator.
1828  *
1829  * This is intended for use by consumers which cannot tolerate shared
1830  * use of the regulator such as those which need to force the
1831  * regulator off for correct operation of the hardware they are
1832  * controlling.
1833  *
1834  * Use of supply names configured via regulator_set_device_supply() is
1835  * strongly encouraged.  It is recommended that the supply name used
1836  * should match the name used for the supply and/or the relevant
1837  * device pins in the datasheet.
1838  */
1839 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1840 {
1841         return _regulator_get(dev, id, EXCLUSIVE_GET);
1842 }
1843 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1844
1845 /**
1846  * regulator_get_optional - obtain optional access to a regulator.
1847  * @dev: device for regulator "consumer"
1848  * @id: Supply name or regulator ID.
1849  *
1850  * Returns a struct regulator corresponding to the regulator producer,
1851  * or IS_ERR() condition containing errno.
1852  *
1853  * This is intended for use by consumers for devices which can have
1854  * some supplies unconnected in normal use, such as some MMC devices.
1855  * It can allow the regulator core to provide stub supplies for other
1856  * supplies requested using normal regulator_get() calls without
1857  * disrupting the operation of drivers that can handle absent
1858  * supplies.
1859  *
1860  * Use of supply names configured via regulator_set_device_supply() is
1861  * strongly encouraged.  It is recommended that the supply name used
1862  * should match the name used for the supply and/or the relevant
1863  * device pins in the datasheet.
1864  */
1865 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1866 {
1867         return _regulator_get(dev, id, OPTIONAL_GET);
1868 }
1869 EXPORT_SYMBOL_GPL(regulator_get_optional);
1870
1871 /* regulator_list_mutex lock held by regulator_put() */
1872 static void _regulator_put(struct regulator *regulator)
1873 {
1874         struct regulator_dev *rdev;
1875
1876         if (IS_ERR_OR_NULL(regulator))
1877                 return;
1878
1879         lockdep_assert_held_once(&regulator_list_mutex);
1880
1881         rdev = regulator->rdev;
1882
1883         debugfs_remove_recursive(regulator->debugfs);
1884
1885         if (regulator->dev) {
1886                 int count = 0;
1887                 struct regulator *r;
1888
1889                 list_for_each_entry(r, &rdev->consumer_list, list)
1890                         if (r->dev == regulator->dev)
1891                                 count++;
1892
1893                 if (count == 1)
1894                         device_link_remove(regulator->dev, &rdev->dev);
1895
1896                 /* remove any sysfs entries */
1897                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1898         }
1899
1900         regulator_lock(rdev);
1901         list_del(&regulator->list);
1902
1903         rdev->open_count--;
1904         rdev->exclusive = 0;
1905         regulator_unlock(rdev);
1906
1907         kfree_const(regulator->supply_name);
1908         kfree(regulator);
1909
1910         module_put(rdev->owner);
1911         put_device(&rdev->dev);
1912 }
1913
1914 /**
1915  * regulator_put - "free" the regulator source
1916  * @regulator: regulator source
1917  *
1918  * Note: drivers must ensure that all regulator_enable calls made on this
1919  * regulator source are balanced by regulator_disable calls prior to calling
1920  * this function.
1921  */
1922 void regulator_put(struct regulator *regulator)
1923 {
1924         mutex_lock(&regulator_list_mutex);
1925         _regulator_put(regulator);
1926         mutex_unlock(&regulator_list_mutex);
1927 }
1928 EXPORT_SYMBOL_GPL(regulator_put);
1929
1930 /**
1931  * regulator_register_supply_alias - Provide device alias for supply lookup
1932  *
1933  * @dev: device that will be given as the regulator "consumer"
1934  * @id: Supply name or regulator ID
1935  * @alias_dev: device that should be used to lookup the supply
1936  * @alias_id: Supply name or regulator ID that should be used to lookup the
1937  * supply
1938  *
1939  * All lookups for id on dev will instead be conducted for alias_id on
1940  * alias_dev.
1941  */
1942 int regulator_register_supply_alias(struct device *dev, const char *id,
1943                                     struct device *alias_dev,
1944                                     const char *alias_id)
1945 {
1946         struct regulator_supply_alias *map;
1947
1948         map = regulator_find_supply_alias(dev, id);
1949         if (map)
1950                 return -EEXIST;
1951
1952         map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1953         if (!map)
1954                 return -ENOMEM;
1955
1956         map->src_dev = dev;
1957         map->src_supply = id;
1958         map->alias_dev = alias_dev;
1959         map->alias_supply = alias_id;
1960
1961         list_add(&map->list, &regulator_supply_alias_list);
1962
1963         pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1964                 id, dev_name(dev), alias_id, dev_name(alias_dev));
1965
1966         return 0;
1967 }
1968 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1969
1970 /**
1971  * regulator_unregister_supply_alias - Remove device alias
1972  *
1973  * @dev: device that will be given as the regulator "consumer"
1974  * @id: Supply name or regulator ID
1975  *
1976  * Remove a lookup alias if one exists for id on dev.
1977  */
1978 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1979 {
1980         struct regulator_supply_alias *map;
1981
1982         map = regulator_find_supply_alias(dev, id);
1983         if (map) {
1984                 list_del(&map->list);
1985                 kfree(map);
1986         }
1987 }
1988 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1989
1990 /**
1991  * regulator_bulk_register_supply_alias - register multiple aliases
1992  *
1993  * @dev: device that will be given as the regulator "consumer"
1994  * @id: List of supply names or regulator IDs
1995  * @alias_dev: device that should be used to lookup the supply
1996  * @alias_id: List of supply names or regulator IDs that should be used to
1997  * lookup the supply
1998  * @num_id: Number of aliases to register
1999  *
2000  * @return 0 on success, an errno on failure.
2001  *
2002  * This helper function allows drivers to register several supply
2003  * aliases in one operation.  If any of the aliases cannot be
2004  * registered any aliases that were registered will be removed
2005  * before returning to the caller.
2006  */
2007 int regulator_bulk_register_supply_alias(struct device *dev,
2008                                          const char *const *id,
2009                                          struct device *alias_dev,
2010                                          const char *const *alias_id,
2011                                          int num_id)
2012 {
2013         int i;
2014         int ret;
2015
2016         for (i = 0; i < num_id; ++i) {
2017                 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2018                                                       alias_id[i]);
2019                 if (ret < 0)
2020                         goto err;
2021         }
2022
2023         return 0;
2024
2025 err:
2026         dev_err(dev,
2027                 "Failed to create supply alias %s,%s -> %s,%s\n",
2028                 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2029
2030         while (--i >= 0)
2031                 regulator_unregister_supply_alias(dev, id[i]);
2032
2033         return ret;
2034 }
2035 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2036
2037 /**
2038  * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2039  *
2040  * @dev: device that will be given as the regulator "consumer"
2041  * @id: List of supply names or regulator IDs
2042  * @num_id: Number of aliases to unregister
2043  *
2044  * This helper function allows drivers to unregister several supply
2045  * aliases in one operation.
2046  */
2047 void regulator_bulk_unregister_supply_alias(struct device *dev,
2048                                             const char *const *id,
2049                                             int num_id)
2050 {
2051         int i;
2052
2053         for (i = 0; i < num_id; ++i)
2054                 regulator_unregister_supply_alias(dev, id[i]);
2055 }
2056 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2057
2058
2059 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2060 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2061                                 const struct regulator_config *config)
2062 {
2063         struct regulator_enable_gpio *pin;
2064         struct gpio_desc *gpiod;
2065         int ret;
2066
2067         if (config->ena_gpiod)
2068                 gpiod = config->ena_gpiod;
2069         else
2070                 gpiod = gpio_to_desc(config->ena_gpio);
2071
2072         list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
2073                 if (pin->gpiod == gpiod) {
2074                         rdev_dbg(rdev, "GPIO %d is already used\n",
2075                                 config->ena_gpio);
2076                         goto update_ena_gpio_to_rdev;
2077                 }
2078         }
2079
2080         if (!config->ena_gpiod) {
2081                 ret = gpio_request_one(config->ena_gpio,
2082                                        GPIOF_DIR_OUT | config->ena_gpio_flags,
2083                                        rdev_get_name(rdev));
2084                 if (ret)
2085                         return ret;
2086         }
2087
2088         pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2089         if (pin == NULL) {
2090                 if (!config->ena_gpiod)
2091                         gpio_free(config->ena_gpio);
2092                 return -ENOMEM;
2093         }
2094
2095         pin->gpiod = gpiod;
2096         pin->ena_gpio_invert = config->ena_gpio_invert;
2097         list_add(&pin->list, &regulator_ena_gpio_list);
2098
2099 update_ena_gpio_to_rdev:
2100         pin->request_count++;
2101         rdev->ena_pin = pin;
2102         return 0;
2103 }
2104
2105 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2106 {
2107         struct regulator_enable_gpio *pin, *n;
2108
2109         if (!rdev->ena_pin)
2110                 return;
2111
2112         /* Free the GPIO only in case of no use */
2113         list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
2114                 if (pin->gpiod == rdev->ena_pin->gpiod) {
2115                         if (pin->request_count <= 1) {
2116                                 pin->request_count = 0;
2117                                 gpiod_put(pin->gpiod);
2118                                 list_del(&pin->list);
2119                                 kfree(pin);
2120                                 rdev->ena_pin = NULL;
2121                                 return;
2122                         } else {
2123                                 pin->request_count--;
2124                         }
2125                 }
2126         }
2127 }
2128
2129 /**
2130  * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2131  * @rdev: regulator_dev structure
2132  * @enable: enable GPIO at initial use?
2133  *
2134  * GPIO is enabled in case of initial use. (enable_count is 0)
2135  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2136  */
2137 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2138 {
2139         struct regulator_enable_gpio *pin = rdev->ena_pin;
2140
2141         if (!pin)
2142                 return -EINVAL;
2143
2144         if (enable) {
2145                 /* Enable GPIO at initial use */
2146                 if (pin->enable_count == 0)
2147                         gpiod_set_value_cansleep(pin->gpiod,
2148                                                  !pin->ena_gpio_invert);
2149
2150                 pin->enable_count++;
2151         } else {
2152                 if (pin->enable_count > 1) {
2153                         pin->enable_count--;
2154                         return 0;
2155                 }
2156
2157                 /* Disable GPIO if not used */
2158                 if (pin->enable_count <= 1) {
2159                         gpiod_set_value_cansleep(pin->gpiod,
2160                                                  pin->ena_gpio_invert);
2161                         pin->enable_count = 0;
2162                 }
2163         }
2164
2165         return 0;
2166 }
2167
2168 /**
2169  * _regulator_enable_delay - a delay helper function
2170  * @delay: time to delay in microseconds
2171  *
2172  * Delay for the requested amount of time as per the guidelines in:
2173  *
2174  *     Documentation/timers/timers-howto.txt
2175  *
2176  * The assumption here is that regulators will never be enabled in
2177  * atomic context and therefore sleeping functions can be used.
2178  */
2179 static void _regulator_enable_delay(unsigned int delay)
2180 {
2181         unsigned int ms = delay / 1000;
2182         unsigned int us = delay % 1000;
2183
2184         if (ms > 0) {
2185                 /*
2186                  * For small enough values, handle super-millisecond
2187                  * delays in the usleep_range() call below.
2188                  */
2189                 if (ms < 20)
2190                         us += ms * 1000;
2191                 else
2192                         msleep(ms);
2193         }
2194
2195         /*
2196          * Give the scheduler some room to coalesce with any other
2197          * wakeup sources. For delays shorter than 10 us, don't even
2198          * bother setting up high-resolution timers and just busy-
2199          * loop.
2200          */
2201         if (us >= 10)
2202                 usleep_range(us, us + 100);
2203         else
2204                 udelay(us);
2205 }
2206
2207 static int _regulator_do_enable(struct regulator_dev *rdev)
2208 {
2209         int ret, delay;
2210
2211         /* Query before enabling in case configuration dependent.  */
2212         ret = _regulator_get_enable_time(rdev);
2213         if (ret >= 0) {
2214                 delay = ret;
2215         } else {
2216                 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2217                 delay = 0;
2218         }
2219
2220         trace_regulator_enable(rdev_get_name(rdev));
2221
2222         if (rdev->desc->off_on_delay) {
2223                 /* if needed, keep a distance of off_on_delay from last time
2224                  * this regulator was disabled.
2225                  */
2226                 unsigned long start_jiffy = jiffies;
2227                 unsigned long intended, max_delay, remaining;
2228
2229                 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2230                 intended = rdev->last_off_jiffy + max_delay;
2231
2232                 if (time_before(start_jiffy, intended)) {
2233                         /* calc remaining jiffies to deal with one-time
2234                          * timer wrapping.
2235                          * in case of multiple timer wrapping, either it can be
2236                          * detected by out-of-range remaining, or it cannot be
2237                          * detected and we gets a panelty of
2238                          * _regulator_enable_delay().
2239                          */
2240                         remaining = intended - start_jiffy;
2241                         if (remaining <= max_delay)
2242                                 _regulator_enable_delay(
2243                                                 jiffies_to_usecs(remaining));
2244                 }
2245         }
2246
2247         if (rdev->ena_pin) {
2248                 if (!rdev->ena_gpio_state) {
2249                         ret = regulator_ena_gpio_ctrl(rdev, true);
2250                         if (ret < 0)
2251                                 return ret;
2252                         rdev->ena_gpio_state = 1;
2253                 }
2254         } else if (rdev->desc->ops->enable) {
2255                 ret = rdev->desc->ops->enable(rdev);
2256                 if (ret < 0)
2257                         return ret;
2258         } else {
2259                 return -EINVAL;
2260         }
2261
2262         /* Allow the regulator to ramp; it would be useful to extend
2263          * this for bulk operations so that the regulators can ramp
2264          * together.  */
2265         trace_regulator_enable_delay(rdev_get_name(rdev));
2266
2267         _regulator_enable_delay(delay);
2268
2269         trace_regulator_enable_complete(rdev_get_name(rdev));
2270
2271         return 0;
2272 }
2273
2274 /* locks held by regulator_enable() */
2275 static int _regulator_enable(struct regulator_dev *rdev)
2276 {
2277         int ret;
2278
2279         lockdep_assert_held_once(&rdev->mutex);
2280
2281         /* check voltage and requested load before enabling */
2282         if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2283                 drms_uA_update(rdev);
2284
2285         if (rdev->use_count == 0) {
2286                 /* The regulator may on if it's not switchable or left on */
2287                 ret = _regulator_is_enabled(rdev);
2288                 if (ret == -EINVAL || ret == 0) {
2289                         if (!regulator_ops_is_valid(rdev,
2290                                         REGULATOR_CHANGE_STATUS))
2291                                 return -EPERM;
2292
2293                         ret = _regulator_do_enable(rdev);
2294                         if (ret < 0)
2295                                 return ret;
2296
2297                         _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2298                                              NULL);
2299                 } else if (ret < 0) {
2300                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2301                         return ret;
2302                 }
2303                 /* Fallthrough on positive return values - already enabled */
2304         }
2305
2306         rdev->use_count++;
2307
2308         return 0;
2309 }
2310
2311 /**
2312  * regulator_enable - enable regulator output
2313  * @regulator: regulator source
2314  *
2315  * Request that the regulator be enabled with the regulator output at
2316  * the predefined voltage or current value.  Calls to regulator_enable()
2317  * must be balanced with calls to regulator_disable().
2318  *
2319  * NOTE: the output value can be set by other drivers, boot loader or may be
2320  * hardwired in the regulator.
2321  */
2322 int regulator_enable(struct regulator *regulator)
2323 {
2324         struct regulator_dev *rdev = regulator->rdev;
2325         int ret = 0;
2326
2327         if (regulator->always_on)
2328                 return 0;
2329
2330         if (rdev->supply) {
2331                 ret = regulator_enable(rdev->supply);
2332                 if (ret != 0)
2333                         return ret;
2334         }
2335
2336         mutex_lock(&rdev->mutex);
2337         ret = _regulator_enable(rdev);
2338         mutex_unlock(&rdev->mutex);
2339
2340         if (ret != 0 && rdev->supply)
2341                 regulator_disable(rdev->supply);
2342
2343         return ret;
2344 }
2345 EXPORT_SYMBOL_GPL(regulator_enable);
2346
2347 static int _regulator_do_disable(struct regulator_dev *rdev)
2348 {
2349         int ret;
2350
2351         trace_regulator_disable(rdev_get_name(rdev));
2352
2353         if (rdev->ena_pin) {
2354                 if (rdev->ena_gpio_state) {
2355                         ret = regulator_ena_gpio_ctrl(rdev, false);
2356                         if (ret < 0)
2357                                 return ret;
2358                         rdev->ena_gpio_state = 0;
2359                 }
2360
2361         } else if (rdev->desc->ops->disable) {
2362                 ret = rdev->desc->ops->disable(rdev);
2363                 if (ret != 0)
2364                         return ret;
2365         }
2366
2367         /* cares about last_off_jiffy only if off_on_delay is required by
2368          * device.
2369          */
2370         if (rdev->desc->off_on_delay)
2371                 rdev->last_off_jiffy = jiffies;
2372
2373         trace_regulator_disable_complete(rdev_get_name(rdev));
2374
2375         return 0;
2376 }
2377
2378 /* locks held by regulator_disable() */
2379 static int _regulator_disable(struct regulator_dev *rdev)
2380 {
2381         int ret = 0;
2382
2383         lockdep_assert_held_once(&rdev->mutex);
2384
2385         if (WARN(rdev->use_count <= 0,
2386                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
2387                 return -EIO;
2388
2389         /* are we the last user and permitted to disable ? */
2390         if (rdev->use_count == 1 &&
2391             (rdev->constraints && !rdev->constraints->always_on)) {
2392
2393                 /* we are last user */
2394                 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2395                         ret = _notifier_call_chain(rdev,
2396                                                    REGULATOR_EVENT_PRE_DISABLE,
2397                                                    NULL);
2398                         if (ret & NOTIFY_STOP_MASK)
2399                                 return -EINVAL;
2400
2401                         ret = _regulator_do_disable(rdev);
2402                         if (ret < 0) {
2403                                 rdev_err(rdev, "failed to disable\n");
2404                                 _notifier_call_chain(rdev,
2405                                                 REGULATOR_EVENT_ABORT_DISABLE,
2406                                                 NULL);
2407                                 return ret;
2408                         }
2409                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2410                                         NULL);
2411                 }
2412
2413                 rdev->use_count = 0;
2414         } else if (rdev->use_count > 1) {
2415                 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2416                         drms_uA_update(rdev);
2417
2418                 rdev->use_count--;
2419         }
2420
2421         return ret;
2422 }
2423
2424 /**
2425  * regulator_disable - disable regulator output
2426  * @regulator: regulator source
2427  *
2428  * Disable the regulator output voltage or current.  Calls to
2429  * regulator_enable() must be balanced with calls to
2430  * regulator_disable().
2431  *
2432  * NOTE: this will only disable the regulator output if no other consumer
2433  * devices have it enabled, the regulator device supports disabling and
2434  * machine constraints permit this operation.
2435  */
2436 int regulator_disable(struct regulator *regulator)
2437 {
2438         struct regulator_dev *rdev = regulator->rdev;
2439         int ret = 0;
2440
2441         if (regulator->always_on)
2442                 return 0;
2443
2444         mutex_lock(&rdev->mutex);
2445         ret = _regulator_disable(rdev);
2446         mutex_unlock(&rdev->mutex);
2447
2448         if (ret == 0 && rdev->supply)
2449                 regulator_disable(rdev->supply);
2450
2451         return ret;
2452 }
2453 EXPORT_SYMBOL_GPL(regulator_disable);
2454
2455 /* locks held by regulator_force_disable() */
2456 static int _regulator_force_disable(struct regulator_dev *rdev)
2457 {
2458         int ret = 0;
2459
2460         lockdep_assert_held_once(&rdev->mutex);
2461
2462         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2463                         REGULATOR_EVENT_PRE_DISABLE, NULL);
2464         if (ret & NOTIFY_STOP_MASK)
2465                 return -EINVAL;
2466
2467         ret = _regulator_do_disable(rdev);
2468         if (ret < 0) {
2469                 rdev_err(rdev, "failed to force disable\n");
2470                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2471                                 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2472                 return ret;
2473         }
2474
2475         _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2476                         REGULATOR_EVENT_DISABLE, NULL);
2477
2478         return 0;
2479 }
2480
2481 /**
2482  * regulator_force_disable - force disable regulator output
2483  * @regulator: regulator source
2484  *
2485  * Forcibly disable the regulator output voltage or current.
2486  * NOTE: this *will* disable the regulator output even if other consumer
2487  * devices have it enabled. This should be used for situations when device
2488  * damage will likely occur if the regulator is not disabled (e.g. over temp).
2489  */
2490 int regulator_force_disable(struct regulator *regulator)
2491 {
2492         struct regulator_dev *rdev = regulator->rdev;
2493         int ret;
2494
2495         mutex_lock(&rdev->mutex);
2496         regulator->uA_load = 0;
2497         ret = _regulator_force_disable(regulator->rdev);
2498         mutex_unlock(&rdev->mutex);
2499
2500         if (rdev->supply)
2501                 while (rdev->open_count--)
2502                         regulator_disable(rdev->supply);
2503
2504         return ret;
2505 }
2506 EXPORT_SYMBOL_GPL(regulator_force_disable);
2507
2508 static void regulator_disable_work(struct work_struct *work)
2509 {
2510         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2511                                                   disable_work.work);
2512         int count, i, ret;
2513
2514         regulator_lock(rdev);
2515
2516         BUG_ON(!rdev->deferred_disables);
2517
2518         count = rdev->deferred_disables;
2519         rdev->deferred_disables = 0;
2520
2521         /*
2522          * Workqueue functions queue the new work instance while the previous
2523          * work instance is being processed. Cancel the queued work instance
2524          * as the work instance under processing does the job of the queued
2525          * work instance.
2526          */
2527         cancel_delayed_work(&rdev->disable_work);
2528
2529         for (i = 0; i < count; i++) {
2530                 ret = _regulator_disable(rdev);
2531                 if (ret != 0)
2532                         rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2533         }
2534
2535         regulator_unlock(rdev);
2536
2537         if (rdev->supply) {
2538                 for (i = 0; i < count; i++) {
2539                         ret = regulator_disable(rdev->supply);
2540                         if (ret != 0) {
2541                                 rdev_err(rdev,
2542                                          "Supply disable failed: %d\n", ret);
2543                         }
2544                 }
2545         }
2546 }
2547
2548 /**
2549  * regulator_disable_deferred - disable regulator output with delay
2550  * @regulator: regulator source
2551  * @ms: miliseconds until the regulator is disabled
2552  *
2553  * Execute regulator_disable() on the regulator after a delay.  This
2554  * is intended for use with devices that require some time to quiesce.
2555  *
2556  * NOTE: this will only disable the regulator output if no other consumer
2557  * devices have it enabled, the regulator device supports disabling and
2558  * machine constraints permit this operation.
2559  */
2560 int regulator_disable_deferred(struct regulator *regulator, int ms)
2561 {
2562         struct regulator_dev *rdev = regulator->rdev;
2563
2564         if (regulator->always_on)
2565                 return 0;
2566
2567         if (!ms)
2568                 return regulator_disable(regulator);
2569
2570         regulator_lock(rdev);
2571         rdev->deferred_disables++;
2572         mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2573                          msecs_to_jiffies(ms));
2574         regulator_unlock(rdev);
2575
2576         return 0;
2577 }
2578 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2579
2580 static int _regulator_is_enabled(struct regulator_dev *rdev)
2581 {
2582         /* A GPIO control always takes precedence */
2583         if (rdev->ena_pin)
2584                 return rdev->ena_gpio_state;
2585
2586         /* If we don't know then assume that the regulator is always on */
2587         if (!rdev->desc->ops->is_enabled)
2588                 return 1;
2589
2590         return rdev->desc->ops->is_enabled(rdev);
2591 }
2592
2593 static int _regulator_list_voltage(struct regulator_dev *rdev,
2594                                    unsigned selector, int lock)
2595 {
2596         const struct regulator_ops *ops = rdev->desc->ops;
2597         int ret;
2598
2599         if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2600                 return rdev->desc->fixed_uV;
2601
2602         if (ops->list_voltage) {
2603                 if (selector >= rdev->desc->n_voltages)
2604                         return -EINVAL;
2605                 if (lock)
2606                         regulator_lock(rdev);
2607                 ret = ops->list_voltage(rdev, selector);
2608                 if (lock)
2609                         regulator_unlock(rdev);
2610         } else if (rdev->is_switch && rdev->supply) {
2611                 ret = _regulator_list_voltage(rdev->supply->rdev,
2612                                               selector, lock);
2613         } else {
2614                 return -EINVAL;
2615         }
2616
2617         if (ret > 0) {
2618                 if (ret < rdev->constraints->min_uV)
2619                         ret = 0;
2620                 else if (ret > rdev->constraints->max_uV)
2621                         ret = 0;
2622         }
2623
2624         return ret;
2625 }
2626
2627 /**
2628  * regulator_is_enabled - is the regulator output enabled
2629  * @regulator: regulator source
2630  *
2631  * Returns positive if the regulator driver backing the source/client
2632  * has requested that the device be enabled, zero if it hasn't, else a
2633  * negative errno code.
2634  *
2635  * Note that the device backing this regulator handle can have multiple
2636  * users, so it might be enabled even if regulator_enable() was never
2637  * called for this particular source.
2638  */
2639 int regulator_is_enabled(struct regulator *regulator)
2640 {
2641         int ret;
2642
2643         if (regulator->always_on)
2644                 return 1;
2645
2646         mutex_lock(&regulator->rdev->mutex);
2647         ret = _regulator_is_enabled(regulator->rdev);
2648         mutex_unlock(&regulator->rdev->mutex);
2649
2650         return ret;
2651 }
2652 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2653
2654 /**
2655  * regulator_count_voltages - count regulator_list_voltage() selectors
2656  * @regulator: regulator source
2657  *
2658  * Returns number of selectors, or negative errno.  Selectors are
2659  * numbered starting at zero, and typically correspond to bitfields
2660  * in hardware registers.
2661  */
2662 int regulator_count_voltages(struct regulator *regulator)
2663 {
2664         struct regulator_dev    *rdev = regulator->rdev;
2665
2666         if (rdev->desc->n_voltages)
2667                 return rdev->desc->n_voltages;
2668
2669         if (!rdev->is_switch || !rdev->supply)
2670                 return -EINVAL;
2671
2672         return regulator_count_voltages(rdev->supply);
2673 }
2674 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2675
2676 /**
2677  * regulator_list_voltage - enumerate supported voltages
2678  * @regulator: regulator source
2679  * @selector: identify voltage to list
2680  * Context: can sleep
2681  *
2682  * Returns a voltage that can be passed to @regulator_set_voltage(),
2683  * zero if this selector code can't be used on this system, or a
2684  * negative errno.
2685  */
2686 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2687 {
2688         return _regulator_list_voltage(regulator->rdev, selector, 1);
2689 }
2690 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2691
2692 /**
2693  * regulator_get_regmap - get the regulator's register map
2694  * @regulator: regulator source
2695  *
2696  * Returns the register map for the given regulator, or an ERR_PTR value
2697  * if the regulator doesn't use regmap.
2698  */
2699 struct regmap *regulator_get_regmap(struct regulator *regulator)
2700 {
2701         struct regmap *map = regulator->rdev->regmap;
2702
2703         return map ? map : ERR_PTR(-EOPNOTSUPP);
2704 }
2705
2706 /**
2707  * regulator_get_hardware_vsel_register - get the HW voltage selector register
2708  * @regulator: regulator source
2709  * @vsel_reg: voltage selector register, output parameter
2710  * @vsel_mask: mask for voltage selector bitfield, output parameter
2711  *
2712  * Returns the hardware register offset and bitmask used for setting the
2713  * regulator voltage. This might be useful when configuring voltage-scaling
2714  * hardware or firmware that can make I2C requests behind the kernel's back,
2715  * for example.
2716  *
2717  * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2718  * and 0 is returned, otherwise a negative errno is returned.
2719  */
2720 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2721                                          unsigned *vsel_reg,
2722                                          unsigned *vsel_mask)
2723 {
2724         struct regulator_dev *rdev = regulator->rdev;
2725         const struct regulator_ops *ops = rdev->desc->ops;
2726
2727         if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2728                 return -EOPNOTSUPP;
2729
2730         *vsel_reg = rdev->desc->vsel_reg;
2731         *vsel_mask = rdev->desc->vsel_mask;
2732
2733          return 0;
2734 }
2735 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2736
2737 /**
2738  * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2739  * @regulator: regulator source
2740  * @selector: identify voltage to list
2741  *
2742  * Converts the selector to a hardware-specific voltage selector that can be
2743  * directly written to the regulator registers. The address of the voltage
2744  * register can be determined by calling @regulator_get_hardware_vsel_register.
2745  *
2746  * On error a negative errno is returned.
2747  */
2748 int regulator_list_hardware_vsel(struct regulator *regulator,
2749                                  unsigned selector)
2750 {
2751         struct regulator_dev *rdev = regulator->rdev;
2752         const struct regulator_ops *ops = rdev->desc->ops;
2753
2754         if (selector >= rdev->desc->n_voltages)
2755                 return -EINVAL;
2756         if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2757                 return -EOPNOTSUPP;
2758
2759         return selector;
2760 }
2761 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2762
2763 /**
2764  * regulator_get_linear_step - return the voltage step size between VSEL values
2765  * @regulator: regulator source
2766  *
2767  * Returns the voltage step size between VSEL values for linear
2768  * regulators, or return 0 if the regulator isn't a linear regulator.
2769  */
2770 unsigned int regulator_get_linear_step(struct regulator *regulator)
2771 {
2772         struct regulator_dev *rdev = regulator->rdev;
2773
2774         return rdev->desc->uV_step;
2775 }
2776 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2777
2778 /**
2779  * regulator_is_supported_voltage - check if a voltage range can be supported
2780  *
2781  * @regulator: Regulator to check.
2782  * @min_uV: Minimum required voltage in uV.
2783  * @max_uV: Maximum required voltage in uV.
2784  *
2785  * Returns a boolean or a negative error code.
2786  */
2787 int regulator_is_supported_voltage(struct regulator *regulator,
2788                                    int min_uV, int max_uV)
2789 {
2790         struct regulator_dev *rdev = regulator->rdev;
2791         int i, voltages, ret;
2792
2793         /* If we can't change voltage check the current voltage */
2794         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2795                 ret = regulator_get_voltage(regulator);
2796                 if (ret >= 0)
2797                         return min_uV <= ret && ret <= max_uV;
2798                 else
2799                         return ret;
2800         }
2801
2802         /* Any voltage within constrains range is fine? */
2803         if (rdev->desc->continuous_voltage_range)
2804                 return min_uV >= rdev->constraints->min_uV &&
2805                                 max_uV <= rdev->constraints->max_uV;
2806
2807         ret = regulator_count_voltages(regulator);
2808         if (ret < 0)
2809                 return ret;
2810         voltages = ret;
2811
2812         for (i = 0; i < voltages; i++) {
2813                 ret = regulator_list_voltage(regulator, i);
2814
2815                 if (ret >= min_uV && ret <= max_uV)
2816                         return 1;
2817         }
2818
2819         return 0;
2820 }
2821 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2822
2823 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2824                                  int max_uV)
2825 {
2826         const struct regulator_desc *desc = rdev->desc;
2827
2828         if (desc->ops->map_voltage)
2829                 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2830
2831         if (desc->ops->list_voltage == regulator_list_voltage_linear)
2832                 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2833
2834         if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2835                 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2836
2837         return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2838 }
2839
2840 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2841                                        int min_uV, int max_uV,
2842                                        unsigned *selector)
2843 {
2844         struct pre_voltage_change_data data;
2845         int ret;
2846
2847         data.old_uV = _regulator_get_voltage(rdev);
2848         data.min_uV = min_uV;
2849         data.max_uV = max_uV;
2850         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2851                                    &data);
2852         if (ret & NOTIFY_STOP_MASK)
2853                 return -EINVAL;
2854
2855         ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2856         if (ret >= 0)
2857                 return ret;
2858
2859         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2860                              (void *)data.old_uV);
2861
2862         return ret;
2863 }
2864
2865 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2866                                            int uV, unsigned selector)
2867 {
2868         struct pre_voltage_change_data data;
2869         int ret;
2870
2871         data.old_uV = _regulator_get_voltage(rdev);
2872         data.min_uV = uV;
2873         data.max_uV = uV;
2874         ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2875                                    &data);
2876         if (ret & NOTIFY_STOP_MASK)
2877                 return -EINVAL;
2878
2879         ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2880         if (ret >= 0)
2881                 return ret;
2882
2883         _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2884                              (void *)data.old_uV);
2885
2886         return ret;
2887 }
2888
2889 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2890                                        int old_uV, int new_uV)
2891 {
2892         unsigned int ramp_delay = 0;
2893
2894         if (rdev->constraints->ramp_delay)
2895                 ramp_delay = rdev->constraints->ramp_delay;
2896         else if (rdev->desc->ramp_delay)
2897                 ramp_delay = rdev->desc->ramp_delay;
2898         else if (rdev->constraints->settling_time)
2899                 return rdev->constraints->settling_time;
2900         else if (rdev->constraints->settling_time_up &&
2901                  (new_uV > old_uV))
2902                 return rdev->constraints->settling_time_up;
2903         else if (rdev->constraints->settling_time_down &&
2904                  (new_uV < old_uV))
2905                 return rdev->constraints->settling_time_down;
2906
2907         if (ramp_delay == 0) {
2908                 rdev_dbg(rdev, "ramp_delay not set\n");
2909                 return 0;
2910         }
2911
2912         return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2913 }
2914
2915 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2916                                      int min_uV, int max_uV)
2917 {
2918         int ret;
2919         int delay = 0;
2920         int best_val = 0;
2921         unsigned int selector;
2922         int old_selector = -1;
2923         const struct regulator_ops *ops = rdev->desc->ops;
2924         int old_uV = _regulator_get_voltage(rdev);
2925
2926         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2927
2928         min_uV += rdev->constraints->uV_offset;
2929         max_uV += rdev->constraints->uV_offset;
2930
2931         /*
2932          * If we can't obtain the old selector there is not enough
2933          * info to call set_voltage_time_sel().
2934          */
2935         if (_regulator_is_enabled(rdev) &&
2936             ops->set_voltage_time_sel && ops->get_voltage_sel) {
2937                 old_selector = ops->get_voltage_sel(rdev);
2938                 if (old_selector < 0)
2939                         return old_selector;
2940         }
2941
2942         if (ops->set_voltage) {
2943                 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2944                                                   &selector);
2945
2946                 if (ret >= 0) {
2947                         if (ops->list_voltage)
2948                                 best_val = ops->list_voltage(rdev,
2949                                                              selector);
2950                         else
2951                                 best_val = _regulator_get_voltage(rdev);
2952                 }
2953
2954         } else if (ops->set_voltage_sel) {
2955                 ret = regulator_map_voltage(rdev, min_uV, max_uV);
2956                 if (ret >= 0) {
2957                         best_val = ops->list_voltage(rdev, ret);
2958                         if (min_uV <= best_val && max_uV >= best_val) {
2959                                 selector = ret;
2960                                 if (old_selector == selector)
2961                                         ret = 0;
2962                                 else
2963                                         ret = _regulator_call_set_voltage_sel(
2964                                                 rdev, best_val, selector);
2965                         } else {
2966                                 ret = -EINVAL;
2967                         }
2968                 }
2969         } else {
2970                 ret = -EINVAL;
2971         }
2972
2973         if (ret)
2974                 goto out;
2975
2976         if (ops->set_voltage_time_sel) {
2977                 /*
2978                  * Call set_voltage_time_sel if successfully obtained
2979                  * old_selector
2980                  */
2981                 if (old_selector >= 0 && old_selector != selector)
2982                         delay = ops->set_voltage_time_sel(rdev, old_selector,
2983                                                           selector);
2984         } else {
2985                 if (old_uV != best_val) {
2986                         if (ops->set_voltage_time)
2987                                 delay = ops->set_voltage_time(rdev, old_uV,
2988                                                               best_val);
2989                         else
2990                                 delay = _regulator_set_voltage_time(rdev,
2991                                                                     old_uV,
2992                                                                     best_val);
2993                 }
2994         }
2995
2996         if (delay < 0) {
2997                 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2998                 delay = 0;
2999         }
3000
3001         /* Insert any necessary delays */
3002         if (delay >= 1000) {
3003                 mdelay(delay / 1000);
3004                 udelay(delay % 1000);
3005         } else if (delay) {
3006                 udelay(delay);
3007         }
3008
3009         if (best_val >= 0) {
3010                 unsigned long data = best_val;
3011
3012                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3013                                      (void *)data);
3014         }
3015
3016 out:
3017         trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3018
3019         return ret;
3020 }
3021
3022 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3023                                   int min_uV, int max_uV, suspend_state_t state)
3024 {
3025         struct regulator_state *rstate;
3026         int uV, sel;
3027
3028         rstate = regulator_get_suspend_state(rdev, state);
3029         if (rstate == NULL)
3030                 return -EINVAL;
3031
3032         if (min_uV < rstate->min_uV)
3033                 min_uV = rstate->min_uV;
3034         if (max_uV > rstate->max_uV)
3035                 max_uV = rstate->max_uV;
3036
3037         sel = regulator_map_voltage(rdev, min_uV, max_uV);
3038         if (sel < 0)
3039                 return sel;
3040
3041         uV = rdev->desc->ops->list_voltage(rdev, sel);
3042         if (uV >= min_uV && uV <= max_uV)
3043                 rstate->uV = uV;
3044
3045         return 0;
3046 }
3047
3048 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3049                                           int min_uV, int max_uV,
3050                                           suspend_state_t state)
3051 {
3052         struct regulator_dev *rdev = regulator->rdev;
3053         struct regulator_voltage *voltage = &regulator->voltage[state];
3054         int ret = 0;
3055         int old_min_uV, old_max_uV;
3056         int current_uV;
3057         int best_supply_uV = 0;
3058         int supply_change_uV = 0;
3059
3060         /* If we're setting the same range as last time the change
3061          * should be a noop (some cpufreq implementations use the same
3062          * voltage for multiple frequencies, for example).
3063          */
3064         if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3065                 goto out;
3066
3067         /* If we're trying to set a range that overlaps the current voltage,
3068          * return successfully even though the regulator does not support
3069          * changing the voltage.
3070          */
3071         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3072                 current_uV = _regulator_get_voltage(rdev);
3073                 if (min_uV <= current_uV && current_uV <= max_uV) {
3074                         voltage->min_uV = min_uV;
3075                         voltage->max_uV = max_uV;
3076                         goto out;
3077                 }
3078         }
3079
3080         /* sanity check */
3081         if (!rdev->desc->ops->set_voltage &&
3082             !rdev->desc->ops->set_voltage_sel) {
3083                 ret = -EINVAL;
3084                 goto out;
3085         }
3086
3087         /* constraints check */
3088         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3089         if (ret < 0)
3090                 goto out;
3091
3092         /* restore original values in case of error */
3093         old_min_uV = voltage->min_uV;
3094         old_max_uV = voltage->max_uV;
3095         voltage->min_uV = min_uV;
3096         voltage->max_uV = max_uV;
3097
3098         ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
3099         if (ret < 0)
3100                 goto out2;
3101
3102         if (rdev->supply &&
3103             regulator_ops_is_valid(rdev->supply->rdev,
3104                                    REGULATOR_CHANGE_VOLTAGE) &&
3105             (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3106                                            rdev->desc->ops->get_voltage_sel))) {
3107                 int current_supply_uV;
3108                 int selector;
3109
3110                 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3111                 if (selector < 0) {
3112                         ret = selector;
3113                         goto out2;
3114                 }
3115
3116                 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3117                 if (best_supply_uV < 0) {
3118                         ret = best_supply_uV;
3119                         goto out2;
3120                 }
3121
3122                 best_supply_uV += rdev->desc->min_dropout_uV;
3123
3124                 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3125                 if (current_supply_uV < 0) {
3126                         ret = current_supply_uV;
3127                         goto out2;
3128                 }
3129
3130                 supply_change_uV = best_supply_uV - current_supply_uV;
3131         }
3132
3133         if (supply_change_uV > 0) {
3134                 ret = regulator_set_voltage_unlocked(rdev->supply,
3135                                 best_supply_uV, INT_MAX, state);
3136                 if (ret) {
3137                         dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3138                                         ret);
3139                         goto out2;
3140                 }
3141         }
3142
3143         if (state == PM_SUSPEND_ON)
3144                 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3145         else
3146                 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3147                                                         max_uV, state);
3148         if (ret < 0)
3149                 goto out2;
3150
3151         if (supply_change_uV < 0) {
3152                 ret = regulator_set_voltage_unlocked(rdev->supply,
3153                                 best_supply_uV, INT_MAX, state);
3154                 if (ret)
3155                         dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3156                                         ret);
3157                 /* No need to fail here */
3158                 ret = 0;
3159         }
3160
3161 out:
3162         return ret;
3163 out2:
3164         voltage->min_uV = old_min_uV;
3165         voltage->max_uV = old_max_uV;
3166
3167         return ret;
3168 }
3169
3170 /**
3171  * regulator_set_voltage - set regulator output voltage
3172  * @regulator: regulator source
3173  * @min_uV: Minimum required voltage in uV
3174  * @max_uV: Maximum acceptable voltage in uV
3175  *
3176  * Sets a voltage regulator to the desired output voltage. This can be set
3177  * during any regulator state. IOW, regulator can be disabled or enabled.
3178  *
3179  * If the regulator is enabled then the voltage will change to the new value
3180  * immediately otherwise if the regulator is disabled the regulator will
3181  * output at the new voltage when enabled.
3182  *
3183  * NOTE: If the regulator is shared between several devices then the lowest
3184  * request voltage that meets the system constraints will be used.
3185  * Regulator system constraints must be set for this regulator before
3186  * calling this function otherwise this call will fail.
3187  */
3188 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3189 {
3190         int ret = 0;
3191
3192         regulator_lock_supply(regulator->rdev);
3193
3194         ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3195                                              PM_SUSPEND_ON);
3196
3197         regulator_unlock_supply(regulator->rdev);
3198
3199         return ret;
3200 }
3201 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3202
3203 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3204                                            suspend_state_t state, bool en)
3205 {
3206         struct regulator_state *rstate;
3207
3208         rstate = regulator_get_suspend_state(rdev, state);
3209         if (rstate == NULL)
3210                 return -EINVAL;
3211
3212         if (!rstate->changeable)
3213                 return -EPERM;
3214
3215         rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3216
3217         return 0;
3218 }
3219
3220 int regulator_suspend_enable(struct regulator_dev *rdev,
3221                                     suspend_state_t state)
3222 {
3223         return regulator_suspend_toggle(rdev, state, true);
3224 }
3225 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3226
3227 int regulator_suspend_disable(struct regulator_dev *rdev,
3228                                      suspend_state_t state)
3229 {
3230         struct regulator *regulator;
3231         struct regulator_voltage *voltage;
3232
3233         /*
3234          * if any consumer wants this regulator device keeping on in
3235          * suspend states, don't set it as disabled.
3236          */
3237         list_for_each_entry(regulator, &rdev->consumer_list, list) {
3238                 voltage = &regulator->voltage[state];
3239                 if (voltage->min_uV || voltage->max_uV)
3240                         return 0;
3241         }
3242
3243         return regulator_suspend_toggle(rdev, state, false);
3244 }
3245 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3246
3247 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3248                                           int min_uV, int max_uV,
3249                                           suspend_state_t state)
3250 {
3251         struct regulator_dev *rdev = regulator->rdev;
3252         struct regulator_state *rstate;
3253
3254         rstate = regulator_get_suspend_state(rdev, state);
3255         if (rstate == NULL)
3256                 return -EINVAL;
3257
3258         if (rstate->min_uV == rstate->max_uV) {
3259                 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3260                 return -EPERM;
3261         }
3262
3263         return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3264 }
3265
3266 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3267                                   int max_uV, suspend_state_t state)
3268 {
3269         int ret = 0;
3270
3271         /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3272         if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3273                 return -EINVAL;
3274
3275         regulator_lock_supply(regulator->rdev);
3276
3277         ret = _regulator_set_suspend_voltage(regulator, min_uV,
3278                                              max_uV, state);
3279
3280         regulator_unlock_supply(regulator->rdev);
3281
3282         return ret;
3283 }
3284 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3285
3286 /**
3287  * regulator_set_voltage_time - get raise/fall time
3288  * @regulator: regulator source
3289  * @old_uV: starting voltage in microvolts
3290  * @new_uV: target voltage in microvolts
3291  *
3292  * Provided with the starting and ending voltage, this function attempts to
3293  * calculate the time in microseconds required to rise or fall to this new
3294  * voltage.
3295  */
3296 int regulator_set_voltage_time(struct regulator *regulator,
3297                                int old_uV, int new_uV)
3298 {
3299         struct regulator_dev *rdev = regulator->rdev;
3300         const struct regulator_ops *ops = rdev->desc->ops;
3301         int old_sel = -1;
3302         int new_sel = -1;
3303         int voltage;
3304         int i;
3305
3306         if (ops->set_voltage_time)
3307                 return ops->set_voltage_time(rdev, old_uV, new_uV);
3308         else if (!ops->set_voltage_time_sel)
3309                 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3310
3311         /* Currently requires operations to do this */
3312         if (!ops->list_voltage || !rdev->desc->n_voltages)
3313                 return -EINVAL;
3314
3315         for (i = 0; i < rdev->desc->n_voltages; i++) {
3316                 /* We only look for exact voltage matches here */
3317                 voltage = regulator_list_voltage(regulator, i);
3318                 if (voltage < 0)
3319                         return -EINVAL;
3320                 if (voltage == 0)
3321                         continue;
3322                 if (voltage == old_uV)
3323                         old_sel = i;
3324                 if (voltage == new_uV)
3325                         new_sel = i;
3326         }
3327
3328         if (old_sel < 0 || new_sel < 0)
3329                 return -EINVAL;
3330
3331         return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3332 }
3333 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3334
3335 /**
3336  * regulator_set_voltage_time_sel - get raise/fall time
3337  * @rdev: regulator source device
3338  * @old_selector: selector for starting voltage
3339  * @new_selector: selector for target voltage
3340  *
3341  * Provided with the starting and target voltage selectors, this function
3342  * returns time in microseconds required to rise or fall to this new voltage
3343  *
3344  * Drivers providing ramp_delay in regulation_constraints can use this as their
3345  * set_voltage_time_sel() operation.
3346  */
3347 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3348                                    unsigned int old_selector,
3349                                    unsigned int new_selector)
3350 {
3351         int old_volt, new_volt;
3352
3353         /* sanity check */
3354         if (!rdev->desc->ops->list_voltage)
3355                 return -EINVAL;
3356
3357         old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3358         new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3359
3360         if (rdev->desc->ops->set_voltage_time)
3361                 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3362                                                          new_volt);
3363         else
3364                 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3365 }
3366 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3367
3368 /**
3369  * regulator_sync_voltage - re-apply last regulator output voltage
3370  * @regulator: regulator source
3371  *
3372  * Re-apply the last configured voltage.  This is intended to be used
3373  * where some external control source the consumer is cooperating with
3374  * has caused the configured voltage to change.
3375  */
3376 int regulator_sync_voltage(struct regulator *regulator)
3377 {
3378         struct regulator_dev *rdev = regulator->rdev;
3379         struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
3380         int ret, min_uV, max_uV;
3381
3382         regulator_lock(rdev);
3383
3384         if (!rdev->desc->ops->set_voltage &&
3385             !rdev->desc->ops->set_voltage_sel) {
3386                 ret = -EINVAL;
3387                 goto out;
3388         }
3389
3390         /* This is only going to work if we've had a voltage configured. */
3391         if (!voltage->min_uV && !voltage->max_uV) {
3392                 ret = -EINVAL;
3393                 goto out;
3394         }
3395
3396         min_uV = voltage->min_uV;
3397         max_uV = voltage->max_uV;
3398
3399         /* This should be a paranoia check... */
3400         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3401         if (ret < 0)
3402                 goto out;
3403
3404         ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3405         if (ret < 0)
3406                 goto out;
3407
3408         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3409
3410 out:
3411         regulator_unlock(rdev);
3412         return ret;
3413 }
3414 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3415
3416 static int _regulator_get_voltage(struct regulator_dev *rdev)
3417 {
3418         int sel, ret;
3419         bool bypassed;
3420
3421         if (rdev->desc->ops->get_bypass) {
3422                 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3423                 if (ret < 0)
3424                         return ret;
3425                 if (bypassed) {
3426                         /* if bypassed the regulator must have a supply */
3427                         if (!rdev->supply) {
3428                                 rdev_err(rdev,
3429                                          "bypassed regulator has no supply!\n");
3430                                 return -EPROBE_DEFER;
3431                         }
3432
3433                         return _regulator_get_voltage(rdev->supply->rdev);
3434                 }
3435         }
3436
3437         if (rdev->desc->ops->get_voltage_sel) {
3438                 sel = rdev->desc->ops->get_voltage_sel(rdev);
3439                 if (sel < 0)
3440                         return sel;
3441                 ret = rdev->desc->ops->list_voltage(rdev, sel);
3442         } else if (rdev->desc->ops->get_voltage) {
3443                 ret = rdev->desc->ops->get_voltage(rdev);
3444         } else if (rdev->desc->ops->list_voltage) {
3445                 ret = rdev->desc->ops->list_voltage(rdev, 0);
3446         } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3447                 ret = rdev->desc->fixed_uV;
3448         } else if (rdev->supply) {
3449                 ret = _regulator_get_voltage(rdev->supply->rdev);
3450         } else if (rdev->supply_name) {
3451                 return -EPROBE_DEFER;
3452         } else {
3453                 return -EINVAL;
3454         }
3455
3456         if (ret < 0)
3457                 return ret;
3458         return ret - rdev->constraints->uV_offset;
3459 }
3460
3461 /**
3462  * regulator_get_voltage - get regulator output voltage
3463  * @regulator: regulator source
3464  *
3465  * This returns the current regulator voltage in uV.
3466  *
3467  * NOTE: If the regulator is disabled it will return the voltage value. This
3468  * function should not be used to determine regulator state.
3469  */
3470 int regulator_get_voltage(struct regulator *regulator)
3471 {
3472         int ret;
3473
3474         regulator_lock_supply(regulator->rdev);
3475
3476         ret = _regulator_get_voltage(regulator->rdev);
3477
3478         regulator_unlock_supply(regulator->rdev);
3479
3480         return ret;
3481 }
3482 EXPORT_SYMBOL_GPL(regulator_get_voltage);
3483
3484 /**
3485  * regulator_set_current_limit - set regulator output current limit
3486  * @regulator: regulator source
3487  * @min_uA: Minimum supported current in uA
3488  * @max_uA: Maximum supported current in uA
3489  *
3490  * Sets current sink to the desired output current. This can be set during
3491  * any regulator state. IOW, regulator can be disabled or enabled.
3492  *
3493  * If the regulator is enabled then the current will change to the new value
3494  * immediately otherwise if the regulator is disabled the regulator will
3495  * output at the new current when enabled.
3496  *
3497  * NOTE: Regulator system constraints must be set for this regulator before
3498  * calling this function otherwise this call will fail.
3499  */
3500 int regulator_set_current_limit(struct regulator *regulator,
3501                                int min_uA, int max_uA)
3502 {
3503         struct regulator_dev *rdev = regulator->rdev;
3504         int ret;
3505
3506         regulator_lock(rdev);
3507
3508         /* sanity check */
3509         if (!rdev->desc->ops->set_current_limit) {
3510                 ret = -EINVAL;
3511                 goto out;
3512         }
3513
3514         /* constraints check */
3515         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3516         if (ret < 0)
3517                 goto out;
3518
3519         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3520 out:
3521         regulator_unlock(rdev);
3522         return ret;
3523 }
3524 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3525
3526 static int _regulator_get_current_limit(struct regulator_dev *rdev)
3527 {
3528         int ret;
3529
3530         regulator_lock(rdev);
3531
3532         /* sanity check */
3533         if (!rdev->desc->ops->get_current_limit) {
3534                 ret = -EINVAL;
3535                 goto out;
3536         }
3537
3538         ret = rdev->desc->ops->get_current_limit(rdev);
3539 out:
3540         regulator_unlock(rdev);
3541         return ret;
3542 }
3543
3544 /**
3545  * regulator_get_current_limit - get regulator output current
3546  * @regulator: regulator source
3547  *
3548  * This returns the current supplied by the specified current sink in uA.
3549  *
3550  * NOTE: If the regulator is disabled it will return the current value. This
3551  * function should not be used to determine regulator state.
3552  */
3553 int regulator_get_current_limit(struct regulator *regulator)
3554 {
3555         return _regulator_get_current_limit(regulator->rdev);
3556 }
3557 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3558
3559 /**
3560  * regulator_set_mode - set regulator operating mode
3561  * @regulator: regulator source
3562  * @mode: operating mode - one of the REGULATOR_MODE constants
3563  *
3564  * Set regulator operating mode to increase regulator efficiency or improve
3565  * regulation performance.
3566  *
3567  * NOTE: Regulator system constraints must be set for this regulator before
3568  * calling this function otherwise this call will fail.
3569  */
3570 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3571 {
3572         struct regulator_dev *rdev = regulator->rdev;
3573         int ret;
3574         int regulator_curr_mode;
3575
3576         regulator_lock(rdev);
3577
3578         /* sanity check */
3579         if (!rdev->desc->ops->set_mode) {
3580                 ret = -EINVAL;
3581                 goto out;
3582         }
3583
3584         /* return if the same mode is requested */
3585         if (rdev->desc->ops->get_mode) {
3586                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3587                 if (regulator_curr_mode == mode) {
3588                         ret = 0;
3589                         goto out;
3590                 }
3591         }
3592
3593         /* constraints check */
3594         ret = regulator_mode_constrain(rdev, &mode);
3595         if (ret < 0)
3596                 goto out;
3597
3598         ret = rdev->desc->ops->set_mode(rdev, mode);
3599 out:
3600         regulator_unlock(rdev);
3601         return ret;
3602 }
3603 EXPORT_SYMBOL_GPL(regulator_set_mode);
3604
3605 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3606 {
3607         int ret;
3608
3609         regulator_lock(rdev);
3610
3611         /* sanity check */
3612         if (!rdev->desc->ops->get_mode) {
3613                 ret = -EINVAL;
3614                 goto out;
3615         }
3616
3617         ret = rdev->desc->ops->get_mode(rdev);
3618 out:
3619         regulator_unlock(rdev);
3620         return ret;
3621 }
3622
3623 /**
3624  * regulator_get_mode - get regulator operating mode
3625  * @regulator: regulator source
3626  *
3627  * Get the current regulator operating mode.
3628  */
3629 unsigned int regulator_get_mode(struct regulator *regulator)
3630 {
3631         return _regulator_get_mode(regulator->rdev);
3632 }
3633 EXPORT_SYMBOL_GPL(regulator_get_mode);
3634
3635 static int _regulator_get_error_flags(struct regulator_dev *rdev,
3636                                         unsigned int *flags)
3637 {
3638         int ret;
3639
3640         regulator_lock(rdev);
3641
3642         /* sanity check */
3643         if (!rdev->desc->ops->get_error_flags) {
3644                 ret = -EINVAL;
3645                 goto out;
3646         }
3647
3648         ret = rdev->desc->ops->get_error_flags(rdev, flags);
3649 out:
3650         regulator_unlock(rdev);
3651         return ret;
3652 }
3653
3654 /**
3655  * regulator_get_error_flags - get regulator error information
3656  * @regulator: regulator source
3657  * @flags: pointer to store error flags
3658  *
3659  * Get the current regulator error information.
3660  */
3661 int regulator_get_error_flags(struct regulator *regulator,
3662                                 unsigned int *flags)
3663 {
3664         return _regulator_get_error_flags(regulator->rdev, flags);
3665 }
3666 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
3667
3668 /**
3669  * regulator_set_load - set regulator load
3670  * @regulator: regulator source
3671  * @uA_load: load current
3672  *
3673  * Notifies the regulator core of a new device load. This is then used by
3674  * DRMS (if enabled by constraints) to set the most efficient regulator
3675  * operating mode for the new regulator loading.
3676  *
3677  * Consumer devices notify their supply regulator of the maximum power
3678  * they will require (can be taken from device datasheet in the power
3679  * consumption tables) when they change operational status and hence power
3680  * state. Examples of operational state changes that can affect power
3681  * consumption are :-
3682  *
3683  *    o Device is opened / closed.
3684  *    o Device I/O is about to begin or has just finished.
3685  *    o Device is idling in between work.
3686  *
3687  * This information is also exported via sysfs to userspace.
3688  *
3689  * DRMS will sum the total requested load on the regulator and change
3690  * to the most efficient operating mode if platform constraints allow.
3691  *
3692  * On error a negative errno is returned.
3693  */
3694 int regulator_set_load(struct regulator *regulator, int uA_load)
3695 {
3696         struct regulator_dev *rdev = regulator->rdev;
3697         int ret;
3698
3699         regulator_lock(rdev);
3700         regulator->uA_load = uA_load;
3701         ret = drms_uA_update(rdev);
3702         regulator_unlock(rdev);
3703
3704         return ret;
3705 }
3706 EXPORT_SYMBOL_GPL(regulator_set_load);
3707
3708 /**
3709  * regulator_allow_bypass - allow the regulator to go into bypass mode
3710  *
3711  * @regulator: Regulator to configure
3712  * @enable: enable or disable bypass mode
3713  *
3714  * Allow the regulator to go into bypass mode if all other consumers
3715  * for the regulator also enable bypass mode and the machine
3716  * constraints allow this.  Bypass mode means that the regulator is
3717  * simply passing the input directly to the output with no regulation.
3718  */
3719 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3720 {
3721         struct regulator_dev *rdev = regulator->rdev;
3722         int ret = 0;
3723
3724         if (!rdev->desc->ops->set_bypass)
3725                 return 0;
3726
3727         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
3728                 return 0;
3729
3730         regulator_lock(rdev);
3731
3732         if (enable && !regulator->bypass) {
3733                 rdev->bypass_count++;
3734
3735                 if (rdev->bypass_count == rdev->open_count) {
3736                         ret = rdev->desc->ops->set_bypass(rdev, enable);
3737                         if (ret != 0)
3738                                 rdev->bypass_count--;
3739                 }
3740
3741         } else if (!enable && regulator->bypass) {
3742                 rdev->bypass_count--;
3743
3744                 if (rdev->bypass_count != rdev->open_count) {
3745                         ret = rdev->desc->ops->set_bypass(rdev, enable);
3746                         if (ret != 0)
3747                                 rdev->bypass_count++;
3748                 }
3749         }
3750
3751         if (ret == 0)
3752                 regulator->bypass = enable;
3753
3754         regulator_unlock(rdev);
3755
3756         return ret;
3757 }
3758 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3759
3760 /**
3761  * regulator_register_notifier - register regulator event notifier
3762  * @regulator: regulator source
3763  * @nb: notifier block
3764  *
3765  * Register notifier block to receive regulator events.
3766  */
3767 int regulator_register_notifier(struct regulator *regulator,
3768                               struct notifier_block *nb)
3769 {
3770         return blocking_notifier_chain_register(&regulator->rdev->notifier,
3771                                                 nb);
3772 }
3773 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3774
3775 /**
3776  * regulator_unregister_notifier - unregister regulator event notifier
3777  * @regulator: regulator source
3778  * @nb: notifier block
3779  *
3780  * Unregister regulator event notifier block.
3781  */
3782 int regulator_unregister_notifier(struct regulator *regulator,
3783                                 struct notifier_block *nb)
3784 {
3785         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3786                                                   nb);
3787 }
3788 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3789
3790 /* notify regulator consumers and downstream regulator consumers.
3791  * Note mutex must be held by caller.
3792  */
3793 static int _notifier_call_chain(struct regulator_dev *rdev,
3794                                   unsigned long event, void *data)
3795 {
3796         /* call rdev chain first */
3797         return blocking_notifier_call_chain(&rdev->notifier, event, data);
3798 }
3799
3800 /**
3801  * regulator_bulk_get - get multiple regulator consumers
3802  *
3803  * @dev:           Device to supply
3804  * @num_consumers: Number of consumers to register
3805  * @consumers:     Configuration of consumers; clients are stored here.
3806  *
3807  * @return 0 on success, an errno on failure.
3808  *
3809  * This helper function allows drivers to get several regulator
3810  * consumers in one operation.  If any of the regulators cannot be
3811  * acquired then any regulators that were allocated will be freed
3812  * before returning to the caller.
3813  */
3814 int regulator_bulk_get(struct device *dev, int num_consumers,
3815                        struct regulator_bulk_data *consumers)
3816 {
3817         int i;
3818         int ret;
3819
3820         for (i = 0; i < num_consumers; i++)
3821                 consumers[i].consumer = NULL;
3822
3823         for (i = 0; i < num_consumers; i++) {
3824                 consumers[i].consumer = regulator_get(dev,
3825                                                       consumers[i].supply);
3826                 if (IS_ERR(consumers[i].consumer)) {
3827                         ret = PTR_ERR(consumers[i].consumer);
3828                         dev_err(dev, "Failed to get supply '%s': %d\n",
3829                                 consumers[i].supply, ret);
3830                         consumers[i].consumer = NULL;
3831                         goto err;
3832                 }
3833         }
3834
3835         return 0;
3836
3837 err:
3838         while (--i >= 0)
3839                 regulator_put(consumers[i].consumer);
3840
3841         return ret;
3842 }
3843 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3844
3845 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3846 {
3847         struct regulator_bulk_data *bulk = data;
3848
3849         bulk->ret = regulator_enable(bulk->consumer);
3850 }
3851
3852 /**
3853  * regulator_bulk_enable - enable multiple regulator consumers
3854  *
3855  * @num_consumers: Number of consumers
3856  * @consumers:     Consumer data; clients are stored here.
3857  * @return         0 on success, an errno on failure
3858  *
3859  * This convenience API allows consumers to enable multiple regulator
3860  * clients in a single API call.  If any consumers cannot be enabled
3861  * then any others that were enabled will be disabled again prior to
3862  * return.
3863  */
3864 int regulator_bulk_enable(int num_consumers,
3865                           struct regulator_bulk_data *consumers)
3866 {
3867         ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3868         int i;
3869         int ret = 0;
3870
3871         for (i = 0; i < num_consumers; i++) {
3872                 if (consumers[i].consumer->always_on)
3873                         consumers[i].ret = 0;
3874                 else
3875                         async_schedule_domain(regulator_bulk_enable_async,
3876                                               &consumers[i], &async_domain);
3877         }
3878
3879         async_synchronize_full_domain(&async_domain);
3880
3881         /* If any consumer failed we need to unwind any that succeeded */
3882         for (i = 0; i < num_consumers; i++) {
3883                 if (consumers[i].ret != 0) {
3884                         ret = consumers[i].ret;
3885                         goto err;
3886                 }
3887         }
3888
3889         return 0;
3890
3891 err:
3892         for (i = 0; i < num_consumers; i++) {
3893                 if (consumers[i].ret < 0)
3894                         pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3895                                consumers[i].ret);
3896                 else
3897                         regulator_disable(consumers[i].consumer);
3898         }
3899
3900         return ret;
3901 }
3902 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3903
3904 /**
3905  * regulator_bulk_disable - disable multiple regulator consumers
3906  *
3907  * @num_consumers: Number of consumers
3908  * @consumers:     Consumer data; clients are stored here.
3909  * @return         0 on success, an errno on failure
3910  *
3911  * This convenience API allows consumers to disable multiple regulator
3912  * clients in a single API call.  If any consumers cannot be disabled
3913  * then any others that were disabled will be enabled again prior to
3914  * return.
3915  */
3916 int regulator_bulk_disable(int num_consumers,
3917                            struct regulator_bulk_data *consumers)
3918 {
3919         int i;
3920         int ret, r;
3921
3922         for (i = num_consumers - 1; i >= 0; --i) {
3923                 ret = regulator_disable(consumers[i].consumer);
3924                 if (ret != 0)
3925                         goto err;
3926         }
3927
3928         return 0;
3929
3930 err:
3931         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3932         for (++i; i < num_consumers; ++i) {
3933                 r = regulator_enable(consumers[i].consumer);
3934                 if (r != 0)
3935                         pr_err("Failed to re-enable %s: %d\n",
3936                                consumers[i].supply, r);
3937         }
3938
3939         return ret;
3940 }
3941 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3942
3943 /**
3944  * regulator_bulk_force_disable - force disable multiple regulator consumers
3945  *
3946  * @num_consumers: Number of consumers
3947  * @consumers:     Consumer data; clients are stored here.
3948  * @return         0 on success, an errno on failure
3949  *
3950  * This convenience API allows consumers to forcibly disable multiple regulator
3951  * clients in a single API call.
3952  * NOTE: This should be used for situations when device damage will
3953  * likely occur if the regulators are not disabled (e.g. over temp).
3954  * Although regulator_force_disable function call for some consumers can
3955  * return error numbers, the function is called for all consumers.
3956  */
3957 int regulator_bulk_force_disable(int num_consumers,
3958                            struct regulator_bulk_data *consumers)
3959 {
3960         int i;
3961         int ret = 0;
3962
3963         for (i = 0; i < num_consumers; i++) {
3964                 consumers[i].ret =
3965                             regulator_force_disable(consumers[i].consumer);
3966
3967                 /* Store first error for reporting */
3968                 if (consumers[i].ret && !ret)
3969                         ret = consumers[i].ret;
3970         }
3971
3972         return ret;
3973 }
3974 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3975
3976 /**
3977  * regulator_bulk_free - free multiple regulator consumers
3978  *
3979  * @num_consumers: Number of consumers
3980  * @consumers:     Consumer data; clients are stored here.
3981  *
3982  * This convenience API allows consumers to free multiple regulator
3983  * clients in a single API call.
3984  */
3985 void regulator_bulk_free(int num_consumers,
3986                          struct regulator_bulk_data *consumers)
3987 {
3988         int i;
3989
3990         for (i = 0; i < num_consumers; i++) {
3991                 regulator_put(consumers[i].consumer);
3992                 consumers[i].consumer = NULL;
3993         }
3994 }
3995 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3996
3997 /**
3998  * regulator_notifier_call_chain - call regulator event notifier
3999  * @rdev: regulator source
4000  * @event: notifier block
4001  * @data: callback-specific data.
4002  *
4003  * Called by regulator drivers to notify clients a regulator event has
4004  * occurred. We also notify regulator clients downstream.
4005  * Note lock must be held by caller.
4006  */
4007 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4008                                   unsigned long event, void *data)
4009 {
4010         lockdep_assert_held_once(&rdev->mutex);
4011
4012         _notifier_call_chain(rdev, event, data);
4013         return NOTIFY_DONE;
4014
4015 }
4016 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4017
4018 /**
4019  * regulator_mode_to_status - convert a regulator mode into a status
4020  *
4021  * @mode: Mode to convert
4022  *
4023  * Convert a regulator mode into a status.
4024  */
4025 int regulator_mode_to_status(unsigned int mode)
4026 {
4027         switch (mode) {
4028         case REGULATOR_MODE_FAST:
4029                 return REGULATOR_STATUS_FAST;
4030         case REGULATOR_MODE_NORMAL:
4031                 return REGULATOR_STATUS_NORMAL;
4032         case REGULATOR_MODE_IDLE:
4033                 return REGULATOR_STATUS_IDLE;
4034         case REGULATOR_MODE_STANDBY:
4035                 return REGULATOR_STATUS_STANDBY;
4036         default:
4037                 return REGULATOR_STATUS_UNDEFINED;
4038         }
4039 }
4040 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4041
4042 static struct attribute *regulator_dev_attrs[] = {
4043         &dev_attr_name.attr,
4044         &dev_attr_num_users.attr,
4045         &dev_attr_type.attr,
4046         &dev_attr_microvolts.attr,
4047         &dev_attr_microamps.attr,
4048         &dev_attr_opmode.attr,
4049         &dev_attr_state.attr,
4050         &dev_attr_status.attr,
4051         &dev_attr_bypass.attr,
4052         &dev_attr_requested_microamps.attr,
4053         &dev_attr_min_microvolts.attr,
4054         &dev_attr_max_microvolts.attr,
4055         &dev_attr_min_microamps.attr,
4056         &dev_attr_max_microamps.attr,
4057         &dev_attr_suspend_standby_state.attr,
4058         &dev_attr_suspend_mem_state.attr,
4059         &dev_attr_suspend_disk_state.attr,
4060         &dev_attr_suspend_standby_microvolts.attr,
4061         &dev_attr_suspend_mem_microvolts.attr,
4062         &dev_attr_suspend_disk_microvolts.attr,
4063         &dev_attr_suspend_standby_mode.attr,
4064         &dev_attr_suspend_mem_mode.attr,
4065         &dev_attr_suspend_disk_mode.attr,
4066         NULL
4067 };
4068
4069 /*
4070  * To avoid cluttering sysfs (and memory) with useless state, only
4071  * create attributes that can be meaningfully displayed.
4072  */
4073 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4074                                          struct attribute *attr, int idx)
4075 {
4076         struct device *dev = kobj_to_dev(kobj);
4077         struct regulator_dev *rdev = dev_to_rdev(dev);
4078         const struct regulator_ops *ops = rdev->desc->ops;
4079         umode_t mode = attr->mode;
4080
4081         /* these three are always present */
4082         if (attr == &dev_attr_name.attr ||
4083             attr == &dev_attr_num_users.attr ||
4084             attr == &dev_attr_type.attr)
4085                 return mode;
4086
4087         /* some attributes need specific methods to be displayed */
4088         if (attr == &dev_attr_microvolts.attr) {
4089                 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4090                     (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4091                     (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4092                     (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4093                         return mode;
4094                 return 0;
4095         }
4096
4097         if (attr == &dev_attr_microamps.attr)
4098                 return ops->get_current_limit ? mode : 0;
4099
4100         if (attr == &dev_attr_opmode.attr)
4101                 return ops->get_mode ? mode : 0;
4102
4103         if (attr == &dev_attr_state.attr)
4104                 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4105
4106         if (attr == &dev_attr_status.attr)
4107                 return ops->get_status ? mode : 0;
4108
4109         if (attr == &dev_attr_bypass.attr)
4110                 return ops->get_bypass ? mode : 0;
4111
4112         /* some attributes are type-specific */
4113         if (attr == &dev_attr_requested_microamps.attr)
4114                 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
4115
4116         /* constraints need specific supporting methods */
4117         if (attr == &dev_attr_min_microvolts.attr ||
4118             attr == &dev_attr_max_microvolts.attr)
4119                 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4120
4121         if (attr == &dev_attr_min_microamps.attr ||
4122             attr == &dev_attr_max_microamps.attr)
4123                 return ops->set_current_limit ? mode : 0;
4124
4125         if (attr == &dev_attr_suspend_standby_state.attr ||
4126             attr == &dev_attr_suspend_mem_state.attr ||
4127             attr == &dev_attr_suspend_disk_state.attr)
4128                 return mode;
4129
4130         if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4131             attr == &dev_attr_suspend_mem_microvolts.attr ||
4132             attr == &dev_attr_suspend_disk_microvolts.attr)
4133                 return ops->set_suspend_voltage ? mode : 0;
4134
4135         if (attr == &dev_attr_suspend_standby_mode.attr ||
4136             attr == &dev_attr_suspend_mem_mode.attr ||
4137             attr == &dev_attr_suspend_disk_mode.attr)
4138                 return ops->set_suspend_mode ? mode : 0;
4139
4140         return mode;
4141 }
4142
4143 static const struct attribute_group regulator_dev_group = {
4144         .attrs = regulator_dev_attrs,
4145         .is_visible = regulator_attr_is_visible,
4146 };
4147
4148 static const struct attribute_group *regulator_dev_groups[] = {
4149         &regulator_dev_group,
4150         NULL
4151 };
4152
4153 static void regulator_dev_release(struct device *dev)
4154 {
4155         struct regulator_dev *rdev = dev_get_drvdata(dev);
4156
4157         kfree(rdev->constraints);
4158         of_node_put(rdev->dev.of_node);
4159         kfree(rdev);
4160 }
4161
4162 static void rdev_init_debugfs(struct regulator_dev *rdev)
4163 {
4164         struct device *parent = rdev->dev.parent;
4165         const char *rname = rdev_get_name(rdev);
4166         char name[NAME_MAX];
4167
4168         /* Avoid duplicate debugfs directory names */
4169         if (parent && rname == rdev->desc->name) {
4170                 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4171                          rname);
4172                 rname = name;
4173         }
4174
4175         rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4176         if (!rdev->debugfs) {
4177                 rdev_warn(rdev, "Failed to create debugfs directory\n");
4178                 return;
4179         }
4180
4181         debugfs_create_u32("use_count", 0444, rdev->debugfs,
4182                            &rdev->use_count);
4183         debugfs_create_u32("open_count", 0444, rdev->debugfs,
4184                            &rdev->open_count);
4185         debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4186                            &rdev->bypass_count);
4187 }
4188
4189 static int regulator_register_resolve_supply(struct device *dev, void *data)
4190 {
4191         struct regulator_dev *rdev = dev_to_rdev(dev);
4192
4193         if (regulator_resolve_supply(rdev))
4194                 rdev_dbg(rdev, "unable to resolve supply\n");
4195
4196         return 0;
4197 }
4198
4199 static int regulator_fill_coupling_array(struct regulator_dev *rdev)
4200 {
4201         struct coupling_desc *c_desc = &rdev->coupling_desc;
4202         int n_coupled = c_desc->n_coupled;
4203         struct regulator_dev *c_rdev;
4204         int i;
4205
4206         for (i = 1; i < n_coupled; i++) {
4207                 /* already resolved */
4208                 if (c_desc->coupled_rdevs[i])
4209                         continue;
4210
4211                 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4212
4213                 if (c_rdev) {
4214                         c_desc->coupled_rdevs[i] = c_rdev;
4215                         c_desc->n_resolved++;
4216                 }
4217         }
4218
4219         if (rdev->coupling_desc.n_resolved < n_coupled)
4220                 return -1;
4221         else
4222                 return 0;
4223 }
4224
4225 static int regulator_register_fill_coupling_array(struct device *dev,
4226                                                   void *data)
4227 {
4228         struct regulator_dev *rdev = dev_to_rdev(dev);
4229
4230         if (!IS_ENABLED(CONFIG_OF))
4231                 return 0;
4232
4233         if (regulator_fill_coupling_array(rdev))
4234                 rdev_dbg(rdev, "unable to resolve coupling\n");
4235
4236         return 0;
4237 }
4238
4239 static int regulator_resolve_coupling(struct regulator_dev *rdev)
4240 {
4241         int n_phandles;
4242
4243         if (!IS_ENABLED(CONFIG_OF))
4244                 n_phandles = 0;
4245         else
4246                 n_phandles = of_get_n_coupled(rdev);
4247
4248         if (n_phandles + 1 > MAX_COUPLED) {
4249                 rdev_err(rdev, "too many regulators coupled\n");
4250                 return -EPERM;
4251         }
4252
4253         /*
4254          * Every regulator should always have coupling descriptor filled with
4255          * at least pointer to itself.
4256          */
4257         rdev->coupling_desc.coupled_rdevs[0] = rdev;
4258         rdev->coupling_desc.n_coupled = n_phandles + 1;
4259         rdev->coupling_desc.n_resolved++;
4260
4261         /* regulator isn't coupled */
4262         if (n_phandles == 0)
4263                 return 0;
4264
4265         /* regulator, which can't change its voltage, can't be coupled */
4266         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
4267                 rdev_err(rdev, "voltage operation not allowed\n");
4268                 return -EPERM;
4269         }
4270
4271         if (rdev->constraints->max_spread <= 0) {
4272                 rdev_err(rdev, "wrong max_spread value\n");
4273                 return -EPERM;
4274         }
4275
4276         if (!of_check_coupling_data(rdev))
4277                 return -EPERM;
4278
4279         /*
4280          * After everything has been checked, try to fill rdevs array
4281          * with pointers to regulators parsed from device tree. If some
4282          * regulators are not registered yet, retry in late init call
4283          */
4284         regulator_fill_coupling_array(rdev);
4285
4286         return 0;
4287 }
4288
4289 /**
4290  * regulator_register - register regulator
4291  * @regulator_desc: regulator to register
4292  * @cfg: runtime configuration for regulator
4293  *
4294  * Called by regulator drivers to register a regulator.
4295  * Returns a valid pointer to struct regulator_dev on success
4296  * or an ERR_PTR() on error.
4297  */
4298 struct regulator_dev *
4299 regulator_register(const struct regulator_desc *regulator_desc,
4300                    const struct regulator_config *cfg)
4301 {
4302         const struct regulator_init_data *init_data;
4303         struct regulator_config *config = NULL;
4304         static atomic_t regulator_no = ATOMIC_INIT(-1);
4305         struct regulator_dev *rdev;
4306         struct device *dev;
4307         int ret, i;
4308
4309         if (regulator_desc == NULL || cfg == NULL)
4310                 return ERR_PTR(-EINVAL);
4311
4312         dev = cfg->dev;
4313         WARN_ON(!dev);
4314
4315         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4316                 return ERR_PTR(-EINVAL);
4317
4318         if (regulator_desc->type != REGULATOR_VOLTAGE &&
4319             regulator_desc->type != REGULATOR_CURRENT)
4320                 return ERR_PTR(-EINVAL);
4321
4322         /* Only one of each should be implemented */
4323         WARN_ON(regulator_desc->ops->get_voltage &&
4324                 regulator_desc->ops->get_voltage_sel);
4325         WARN_ON(regulator_desc->ops->set_voltage &&
4326                 regulator_desc->ops->set_voltage_sel);
4327
4328         /* If we're using selectors we must implement list_voltage. */
4329         if (regulator_desc->ops->get_voltage_sel &&
4330             !regulator_desc->ops->list_voltage) {
4331                 return ERR_PTR(-EINVAL);
4332         }
4333         if (regulator_desc->ops->set_voltage_sel &&
4334             !regulator_desc->ops->list_voltage) {
4335                 return ERR_PTR(-EINVAL);
4336         }
4337
4338         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4339         if (rdev == NULL)
4340                 return ERR_PTR(-ENOMEM);
4341
4342         /*
4343          * Duplicate the config so the driver could override it after
4344          * parsing init data.
4345          */
4346         config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4347         if (config == NULL) {
4348                 kfree(rdev);
4349                 return ERR_PTR(-ENOMEM);
4350         }
4351
4352         init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4353                                                &rdev->dev.of_node);
4354         if (!init_data) {
4355                 init_data = config->init_data;
4356                 rdev->dev.of_node = of_node_get(config->of_node);
4357         }
4358
4359         mutex_init(&rdev->mutex);
4360         rdev->reg_data = config->driver_data;
4361         rdev->owner = regulator_desc->owner;
4362         rdev->desc = regulator_desc;
4363         if (config->regmap)
4364                 rdev->regmap = config->regmap;
4365         else if (dev_get_regmap(dev, NULL))
4366                 rdev->regmap = dev_get_regmap(dev, NULL);
4367         else if (dev->parent)
4368                 rdev->regmap = dev_get_regmap(dev->parent, NULL);
4369         INIT_LIST_HEAD(&rdev->consumer_list);
4370         INIT_LIST_HEAD(&rdev->list);
4371         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4372         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4373
4374         /* preform any regulator specific init */
4375         if (init_data && init_data->regulator_init) {
4376                 ret = init_data->regulator_init(rdev->reg_data);
4377                 if (ret < 0)
4378                         goto clean;
4379         }
4380
4381         if (config->ena_gpiod ||
4382             ((config->ena_gpio || config->ena_gpio_initialized) &&
4383              gpio_is_valid(config->ena_gpio))) {
4384                 mutex_lock(&regulator_list_mutex);
4385                 ret = regulator_ena_gpio_request(rdev, config);
4386                 mutex_unlock(&regulator_list_mutex);
4387                 if (ret != 0) {
4388                         rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4389                                  config->ena_gpio, ret);
4390                         goto clean;
4391                 }
4392         }
4393
4394         /* register with sysfs */
4395         rdev->dev.class = &regulator_class;
4396         rdev->dev.parent = dev;
4397         dev_set_name(&rdev->dev, "regulator.%lu",
4398                     (unsigned long) atomic_inc_return(&regulator_no));
4399
4400         /* set regulator constraints */
4401         if (init_data)
4402                 rdev->constraints = kmemdup(&init_data->constraints,
4403                                             sizeof(*rdev->constraints),
4404                                             GFP_KERNEL);
4405         else
4406                 rdev->constraints = kzalloc(sizeof(*rdev->constraints),
4407                                             GFP_KERNEL);
4408         if (!rdev->constraints) {
4409                 ret = -ENOMEM;
4410                 goto wash;
4411         }
4412
4413         if (init_data && init_data->supply_regulator)
4414                 rdev->supply_name = init_data->supply_regulator;
4415         else if (regulator_desc->supply_name)
4416                 rdev->supply_name = regulator_desc->supply_name;
4417
4418         ret = set_machine_constraints(rdev);
4419         if (ret == -EPROBE_DEFER) {
4420                 /* Regulator might be in bypass mode and so needs its supply
4421                  * to set the constraints */
4422                 /* FIXME: this currently triggers a chicken-and-egg problem
4423                  * when creating -SUPPLY symlink in sysfs to a regulator
4424                  * that is just being created */
4425                 ret = regulator_resolve_supply(rdev);
4426                 if (!ret)
4427                         ret = set_machine_constraints(rdev);
4428                 else
4429                         rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
4430                                  ERR_PTR(ret));
4431         }
4432         if (ret < 0)
4433                 goto wash;
4434
4435         mutex_lock(&regulator_list_mutex);
4436         ret = regulator_resolve_coupling(rdev);
4437         mutex_unlock(&regulator_list_mutex);
4438
4439         if (ret != 0)
4440                 goto wash;
4441
4442         /* add consumers devices */
4443         if (init_data) {
4444                 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4445                         ret = set_consumer_device_supply(rdev,
4446                                 init_data->consumer_supplies[i].dev_name,
4447                                 init_data->consumer_supplies[i].supply);
4448                         if (ret < 0) {
4449                                 dev_err(dev, "Failed to set supply %s\n",
4450                                         init_data->consumer_supplies[i].supply);
4451                                 goto unset_supplies;
4452                         }
4453                 }
4454         }
4455
4456         if (!rdev->desc->ops->get_voltage &&
4457             !rdev->desc->ops->list_voltage &&
4458             !rdev->desc->fixed_uV)
4459                 rdev->is_switch = true;
4460
4461         dev_set_drvdata(&rdev->dev, rdev);
4462         ret = device_register(&rdev->dev);
4463         if (ret != 0) {
4464                 put_device(&rdev->dev);
4465                 goto unset_supplies;
4466         }
4467
4468         rdev_init_debugfs(rdev);
4469
4470         /* try to resolve regulators supply since a new one was registered */
4471         class_for_each_device(&regulator_class, NULL, NULL,
4472                               regulator_register_resolve_supply);
4473         kfree(config);
4474         return rdev;
4475
4476 unset_supplies:
4477         mutex_lock(&regulator_list_mutex);
4478         unset_regulator_supplies(rdev);
4479         mutex_unlock(&regulator_list_mutex);
4480 wash:
4481         kfree(rdev->constraints);
4482         mutex_lock(&regulator_list_mutex);
4483         regulator_ena_gpio_free(rdev);
4484         mutex_unlock(&regulator_list_mutex);
4485 clean:
4486         kfree(rdev);
4487         kfree(config);
4488         return ERR_PTR(ret);
4489 }
4490 EXPORT_SYMBOL_GPL(regulator_register);
4491
4492 /**
4493  * regulator_unregister - unregister regulator
4494  * @rdev: regulator to unregister
4495  *
4496  * Called by regulator drivers to unregister a regulator.
4497  */
4498 void regulator_unregister(struct regulator_dev *rdev)
4499 {
4500         if (rdev == NULL)
4501                 return;
4502
4503         if (rdev->supply) {
4504                 while (rdev->use_count--)
4505                         regulator_disable(rdev->supply);
4506                 regulator_put(rdev->supply);
4507         }
4508         mutex_lock(&regulator_list_mutex);
4509         debugfs_remove_recursive(rdev->debugfs);
4510         flush_work(&rdev->disable_work.work);
4511         WARN_ON(rdev->open_count);
4512         unset_regulator_supplies(rdev);
4513         list_del(&rdev->list);
4514         regulator_ena_gpio_free(rdev);
4515         mutex_unlock(&regulator_list_mutex);
4516         device_unregister(&rdev->dev);
4517 }
4518 EXPORT_SYMBOL_GPL(regulator_unregister);
4519
4520 #ifdef CONFIG_SUSPEND
4521 static int _regulator_suspend(struct device *dev, void *data)
4522 {
4523         struct regulator_dev *rdev = dev_to_rdev(dev);
4524         suspend_state_t *state = data;
4525         int ret;
4526
4527         regulator_lock(rdev);
4528         ret = suspend_set_state(rdev, *state);
4529         regulator_unlock(rdev);
4530
4531         return ret;
4532 }
4533
4534 /**
4535  * regulator_suspend - prepare regulators for system wide suspend
4536  * @state: system suspend state
4537  *
4538  * Configure each regulator with it's suspend operating parameters for state.
4539  */
4540 static int regulator_suspend(struct device *dev)
4541 {
4542         suspend_state_t state = pm_suspend_target_state;
4543
4544         return class_for_each_device(&regulator_class, NULL, &state,
4545                                      _regulator_suspend);
4546 }
4547
4548 static int _regulator_resume(struct device *dev, void *data)
4549 {
4550         int ret = 0;
4551         struct regulator_dev *rdev = dev_to_rdev(dev);
4552         suspend_state_t *state = data;
4553         struct regulator_state *rstate;
4554
4555         rstate = regulator_get_suspend_state(rdev, *state);
4556         if (rstate == NULL)
4557                 return 0;
4558
4559         regulator_lock(rdev);
4560
4561         if (rdev->desc->ops->resume &&
4562             (rstate->enabled == ENABLE_IN_SUSPEND ||
4563              rstate->enabled == DISABLE_IN_SUSPEND))
4564                 ret = rdev->desc->ops->resume(rdev);
4565
4566         regulator_unlock(rdev);
4567
4568         return ret;
4569 }
4570
4571 static int regulator_resume(struct device *dev)
4572 {
4573         suspend_state_t state = pm_suspend_target_state;
4574
4575         return class_for_each_device(&regulator_class, NULL, &state,
4576                                      _regulator_resume);
4577 }
4578
4579 #else /* !CONFIG_SUSPEND */
4580
4581 #define regulator_suspend       NULL
4582 #define regulator_resume        NULL
4583
4584 #endif /* !CONFIG_SUSPEND */
4585
4586 #ifdef CONFIG_PM
4587 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
4588         .suspend        = regulator_suspend,
4589         .resume         = regulator_resume,
4590 };
4591 #endif
4592
4593 struct class regulator_class = {
4594         .name = "regulator",
4595         .dev_release = regulator_dev_release,
4596         .dev_groups = regulator_dev_groups,
4597 #ifdef CONFIG_PM
4598         .pm = &regulator_pm_ops,
4599 #endif
4600 };
4601 /**
4602  * regulator_has_full_constraints - the system has fully specified constraints
4603  *
4604  * Calling this function will cause the regulator API to disable all
4605  * regulators which have a zero use count and don't have an always_on
4606  * constraint in a late_initcall.
4607  *
4608  * The intention is that this will become the default behaviour in a
4609  * future kernel release so users are encouraged to use this facility
4610  * now.
4611  */
4612 void regulator_has_full_constraints(void)
4613 {
4614         has_full_constraints = 1;
4615 }
4616 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4617
4618 /**
4619  * rdev_get_drvdata - get rdev regulator driver data
4620  * @rdev: regulator
4621  *
4622  * Get rdev regulator driver private data. This call can be used in the
4623  * regulator driver context.
4624  */
4625 void *rdev_get_drvdata(struct regulator_dev *rdev)
4626 {
4627         return rdev->reg_data;
4628 }
4629 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4630
4631 /**
4632  * regulator_get_drvdata - get regulator driver data
4633  * @regulator: regulator
4634  *
4635  * Get regulator driver private data. This call can be used in the consumer
4636  * driver context when non API regulator specific functions need to be called.
4637  */
4638 void *regulator_get_drvdata(struct regulator *regulator)
4639 {
4640         return regulator->rdev->reg_data;
4641 }
4642 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4643
4644 /**
4645  * regulator_set_drvdata - set regulator driver data
4646  * @regulator: regulator
4647  * @data: data
4648  */
4649 void regulator_set_drvdata(struct regulator *regulator, void *data)
4650 {
4651         regulator->rdev->reg_data = data;
4652 }
4653 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4654
4655 /**
4656  * regulator_get_id - get regulator ID
4657  * @rdev: regulator
4658  */
4659 int rdev_get_id(struct regulator_dev *rdev)
4660 {
4661         return rdev->desc->id;
4662 }
4663 EXPORT_SYMBOL_GPL(rdev_get_id);
4664
4665 struct device *rdev_get_dev(struct regulator_dev *rdev)
4666 {
4667         return &rdev->dev;
4668 }
4669 EXPORT_SYMBOL_GPL(rdev_get_dev);
4670
4671 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4672 {
4673         return reg_init_data->driver_data;
4674 }
4675 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4676
4677 #ifdef CONFIG_DEBUG_FS
4678 static int supply_map_show(struct seq_file *sf, void *data)
4679 {
4680         struct regulator_map *map;
4681
4682         list_for_each_entry(map, &regulator_map_list, list) {
4683                 seq_printf(sf, "%s -> %s.%s\n",
4684                                 rdev_get_name(map->regulator), map->dev_name,
4685                                 map->supply);
4686         }
4687
4688         return 0;
4689 }
4690
4691 static int supply_map_open(struct inode *inode, struct file *file)
4692 {
4693         return single_open(file, supply_map_show, inode->i_private);
4694 }
4695 #endif
4696
4697 static const struct file_operations supply_map_fops = {
4698 #ifdef CONFIG_DEBUG_FS
4699         .open = supply_map_open,
4700         .read = seq_read,
4701         .llseek = seq_lseek,
4702         .release = single_release,
4703 #endif
4704 };
4705
4706 #ifdef CONFIG_DEBUG_FS
4707 struct summary_data {
4708         struct seq_file *s;
4709         struct regulator_dev *parent;
4710         int level;
4711 };
4712
4713 static void regulator_summary_show_subtree(struct seq_file *s,
4714                                            struct regulator_dev *rdev,
4715                                            int level);
4716
4717 static int regulator_summary_show_children(struct device *dev, void *data)
4718 {
4719         struct regulator_dev *rdev = dev_to_rdev(dev);
4720         struct summary_data *summary_data = data;
4721
4722         if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4723                 regulator_summary_show_subtree(summary_data->s, rdev,
4724                                                summary_data->level + 1);
4725
4726         return 0;
4727 }
4728
4729 static void regulator_summary_show_subtree(struct seq_file *s,
4730                                            struct regulator_dev *rdev,
4731                                            int level)
4732 {
4733         struct regulation_constraints *c;
4734         struct regulator *consumer;
4735         struct summary_data summary_data;
4736
4737         if (!rdev)
4738                 return;
4739
4740         seq_printf(s, "%*s%-*s %3d %4d %6d ",
4741                    level * 3 + 1, "",
4742                    30 - level * 3, rdev_get_name(rdev),
4743                    rdev->use_count, rdev->open_count, rdev->bypass_count);
4744
4745         seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4746         seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4747
4748         c = rdev->constraints;
4749         if (c) {
4750                 switch (rdev->desc->type) {
4751                 case REGULATOR_VOLTAGE:
4752                         seq_printf(s, "%5dmV %5dmV ",
4753                                    c->min_uV / 1000, c->max_uV / 1000);
4754                         break;
4755                 case REGULATOR_CURRENT:
4756                         seq_printf(s, "%5dmA %5dmA ",
4757                                    c->min_uA / 1000, c->max_uA / 1000);
4758                         break;
4759                 }
4760         }
4761
4762         seq_puts(s, "\n");
4763
4764         list_for_each_entry(consumer, &rdev->consumer_list, list) {
4765                 if (consumer->dev && consumer->dev->class == &regulator_class)
4766                         continue;
4767
4768                 seq_printf(s, "%*s%-*s ",
4769                            (level + 1) * 3 + 1, "",
4770                            30 - (level + 1) * 3,
4771                            consumer->dev ? dev_name(consumer->dev) : "deviceless");
4772
4773                 switch (rdev->desc->type) {
4774                 case REGULATOR_VOLTAGE:
4775                         seq_printf(s, "%37dmV %5dmV",
4776                                    consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
4777                                    consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
4778                         break;
4779                 case REGULATOR_CURRENT:
4780                         break;
4781                 }
4782
4783                 seq_puts(s, "\n");
4784         }
4785
4786         summary_data.s = s;
4787         summary_data.level = level;
4788         summary_data.parent = rdev;
4789
4790         class_for_each_device(&regulator_class, NULL, &summary_data,
4791                               regulator_summary_show_children);
4792 }
4793
4794 static int regulator_summary_show_roots(struct device *dev, void *data)
4795 {
4796         struct regulator_dev *rdev = dev_to_rdev(dev);
4797         struct seq_file *s = data;
4798
4799         if (!rdev->supply)
4800                 regulator_summary_show_subtree(s, rdev, 0);
4801
4802         return 0;
4803 }
4804
4805 static int regulator_summary_show(struct seq_file *s, void *data)
4806 {
4807         seq_puts(s, " regulator                      use open bypass voltage current     min     max\n");
4808         seq_puts(s, "-------------------------------------------------------------------------------\n");
4809
4810         class_for_each_device(&regulator_class, NULL, s,
4811                               regulator_summary_show_roots);
4812
4813         return 0;
4814 }
4815
4816 static int regulator_summary_open(struct inode *inode, struct file *file)
4817 {
4818         return single_open(file, regulator_summary_show, inode->i_private);
4819 }
4820 #endif
4821
4822 static const struct file_operations regulator_summary_fops = {
4823 #ifdef CONFIG_DEBUG_FS
4824         .open           = regulator_summary_open,
4825         .read           = seq_read,
4826         .llseek         = seq_lseek,
4827         .release        = single_release,
4828 #endif
4829 };
4830
4831 static int __init regulator_init(void)
4832 {
4833         int ret;
4834
4835         ret = class_register(&regulator_class);
4836
4837         debugfs_root = debugfs_create_dir("regulator", NULL);
4838         if (!debugfs_root)
4839                 pr_warn("regulator: Failed to create debugfs directory\n");
4840
4841         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4842                             &supply_map_fops);
4843
4844         debugfs_create_file("regulator_summary", 0444, debugfs_root,
4845                             NULL, &regulator_summary_fops);
4846
4847         regulator_dummy_init();
4848
4849         return ret;
4850 }
4851
4852 /* init early to allow our consumers to complete system booting */
4853 core_initcall(regulator_init);
4854
4855 static int regulator_late_cleanup(struct device *dev, void *data)
4856 {
4857         struct regulator_dev *rdev = dev_to_rdev(dev);
4858         const struct regulator_ops *ops = rdev->desc->ops;
4859         struct regulation_constraints *c = rdev->constraints;
4860         int enabled, ret;
4861
4862         if (c && c->always_on)
4863                 return 0;
4864
4865         if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
4866                 return 0;
4867
4868         regulator_lock(rdev);
4869
4870         if (rdev->use_count)
4871                 goto unlock;
4872
4873         /* If we can't read the status assume it's on. */
4874         if (ops->is_enabled)
4875                 enabled = ops->is_enabled(rdev);
4876         else
4877                 enabled = 1;
4878
4879         if (!enabled)
4880                 goto unlock;
4881
4882         if (have_full_constraints()) {
4883                 /* We log since this may kill the system if it goes
4884                  * wrong. */
4885                 rdev_info(rdev, "disabling\n");
4886                 ret = _regulator_do_disable(rdev);
4887                 if (ret != 0)
4888                         rdev_err(rdev, "couldn't disable: %d\n", ret);
4889         } else {
4890                 /* The intention is that in future we will
4891                  * assume that full constraints are provided
4892                  * so warn even if we aren't going to do
4893                  * anything here.
4894                  */
4895                 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4896         }
4897
4898 unlock:
4899         regulator_unlock(rdev);
4900
4901         return 0;
4902 }
4903
4904 static void regulator_init_complete_work_function(struct work_struct *work)
4905 {
4906         /*
4907          * Regulators may had failed to resolve their input supplies
4908          * when were registered, either because the input supply was
4909          * not registered yet or because its parent device was not
4910          * bound yet. So attempt to resolve the input supplies for
4911          * pending regulators before trying to disable unused ones.
4912          */
4913         class_for_each_device(&regulator_class, NULL, NULL,
4914                               regulator_register_resolve_supply);
4915
4916         /* If we have a full configuration then disable any regulators
4917          * we have permission to change the status for and which are
4918          * not in use or always_on.  This is effectively the default
4919          * for DT and ACPI as they have full constraints.
4920          */
4921         class_for_each_device(&regulator_class, NULL, NULL,
4922                               regulator_late_cleanup);
4923 }
4924
4925 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
4926                             regulator_init_complete_work_function);
4927
4928 static int __init regulator_init_complete(void)
4929 {
4930         /*
4931          * Since DT doesn't provide an idiomatic mechanism for
4932          * enabling full constraints and since it's much more natural
4933          * with DT to provide them just assume that a DT enabled
4934          * system has full constraints.
4935          */
4936         if (of_have_populated_dt())
4937                 has_full_constraints = true;
4938
4939         /*
4940          * We punt completion for an arbitrary amount of time since
4941          * systems like distros will load many drivers from userspace
4942          * so consumers might not always be ready yet, this is
4943          * particularly an issue with laptops where this might bounce
4944          * the display off then on.  Ideally we'd get a notification
4945          * from userspace when this happens but we don't so just wait
4946          * a bit and hope we waited long enough.  It'd be better if
4947          * we'd only do this on systems that need it, and a kernel
4948          * command line option might be useful.
4949          */
4950         schedule_delayed_work(&regulator_init_complete_work,
4951                               msecs_to_jiffies(30000));
4952
4953         class_for_each_device(&regulator_class, NULL, NULL,
4954                               regulator_register_fill_coupling_array);
4955
4956         return 0;
4957 }
4958 late_initcall_sync(regulator_init_complete);