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