]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/regulator/core.c
Merge remote-tracking branch 'regulator/topic/devm' into regulator-next
[karo-tx-linux.git] / drivers / regulator / core.c
1 /*
2  * core.c  --  Voltage/Current Regulator framework.
3  *
4  * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5  * Copyright 2008 SlimLogic Ltd.
6  *
7  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/of.h>
28 #include <linux/regmap.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/module.h>
34
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/regulator.h>
37
38 #include "dummy.h"
39 #include "internal.h"
40
41 #define rdev_crit(rdev, fmt, ...)                                       \
42         pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43 #define rdev_err(rdev, fmt, ...)                                        \
44         pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45 #define rdev_warn(rdev, fmt, ...)                                       \
46         pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 #define rdev_info(rdev, fmt, ...)                                       \
48         pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 #define rdev_dbg(rdev, fmt, ...)                                        \
50         pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51
52 static DEFINE_MUTEX(regulator_list_mutex);
53 static LIST_HEAD(regulator_list);
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 static bool board_wants_dummy_regulator;
59
60 static struct dentry *debugfs_root;
61
62 /*
63  * struct regulator_map
64  *
65  * Used to provide symbolic supply names to devices.
66  */
67 struct regulator_map {
68         struct list_head list;
69         const char *dev_name;   /* The dev_name() for the consumer */
70         const char *supply;
71         struct regulator_dev *regulator;
72 };
73
74 /*
75  * struct regulator_enable_gpio
76  *
77  * Management for shared enable GPIO pin
78  */
79 struct regulator_enable_gpio {
80         struct list_head list;
81         int gpio;
82         u32 enable_count;       /* a number of enabled shared GPIO */
83         u32 request_count;      /* a number of requested shared GPIO */
84         unsigned int ena_gpio_invert:1;
85 };
86
87 /*
88  * struct regulator_supply_alias
89  *
90  * Used to map lookups for a supply onto an alternative device.
91  */
92 struct regulator_supply_alias {
93         struct list_head list;
94         struct device *src_dev;
95         const char *src_supply;
96         struct device *alias_dev;
97         const char *alias_supply;
98 };
99
100 static int _regulator_is_enabled(struct regulator_dev *rdev);
101 static int _regulator_disable(struct regulator_dev *rdev);
102 static int _regulator_get_voltage(struct regulator_dev *rdev);
103 static int _regulator_get_current_limit(struct regulator_dev *rdev);
104 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
105 static void _notifier_call_chain(struct regulator_dev *rdev,
106                                   unsigned long event, void *data);
107 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
108                                      int min_uV, int max_uV);
109 static struct regulator *create_regulator(struct regulator_dev *rdev,
110                                           struct device *dev,
111                                           const char *supply_name);
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 /**
124  * of_get_regulator - get a regulator device node based on supply name
125  * @dev: Device pointer for the consumer (of regulator) device
126  * @supply: regulator supply name
127  *
128  * Extract the regulator device node corresponding to the supply name.
129  * returns the device node corresponding to the regulator if found, else
130  * returns NULL.
131  */
132 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
133 {
134         struct device_node *regnode = NULL;
135         char prop_name[32]; /* 32 is max size of property name */
136
137         dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
138
139         snprintf(prop_name, 32, "%s-supply", supply);
140         regnode = of_parse_phandle(dev->of_node, prop_name, 0);
141
142         if (!regnode) {
143                 dev_dbg(dev, "Looking up %s property in node %s failed",
144                                 prop_name, dev->of_node->full_name);
145                 return NULL;
146         }
147         return regnode;
148 }
149
150 static int _regulator_can_change_status(struct regulator_dev *rdev)
151 {
152         if (!rdev->constraints)
153                 return 0;
154
155         if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
156                 return 1;
157         else
158                 return 0;
159 }
160
161 /* Platform voltage constraint check */
162 static int regulator_check_voltage(struct regulator_dev *rdev,
163                                    int *min_uV, int *max_uV)
164 {
165         BUG_ON(*min_uV > *max_uV);
166
167         if (!rdev->constraints) {
168                 rdev_err(rdev, "no constraints\n");
169                 return -ENODEV;
170         }
171         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
172                 rdev_err(rdev, "operation not allowed\n");
173                 return -EPERM;
174         }
175
176         if (*max_uV > rdev->constraints->max_uV)
177                 *max_uV = rdev->constraints->max_uV;
178         if (*min_uV < rdev->constraints->min_uV)
179                 *min_uV = rdev->constraints->min_uV;
180
181         if (*min_uV > *max_uV) {
182                 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
183                          *min_uV, *max_uV);
184                 return -EINVAL;
185         }
186
187         return 0;
188 }
189
190 /* Make sure we select a voltage that suits the needs of all
191  * regulator consumers
192  */
193 static int regulator_check_consumers(struct regulator_dev *rdev,
194                                      int *min_uV, int *max_uV)
195 {
196         struct regulator *regulator;
197
198         list_for_each_entry(regulator, &rdev->consumer_list, list) {
199                 /*
200                  * Assume consumers that didn't say anything are OK
201                  * with anything in the constraint range.
202                  */
203                 if (!regulator->min_uV && !regulator->max_uV)
204                         continue;
205
206                 if (*max_uV > regulator->max_uV)
207                         *max_uV = regulator->max_uV;
208                 if (*min_uV < regulator->min_uV)
209                         *min_uV = regulator->min_uV;
210         }
211
212         if (*min_uV > *max_uV) {
213                 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
214                         *min_uV, *max_uV);
215                 return -EINVAL;
216         }
217
218         return 0;
219 }
220
221 /* current constraint check */
222 static int regulator_check_current_limit(struct regulator_dev *rdev,
223                                         int *min_uA, int *max_uA)
224 {
225         BUG_ON(*min_uA > *max_uA);
226
227         if (!rdev->constraints) {
228                 rdev_err(rdev, "no constraints\n");
229                 return -ENODEV;
230         }
231         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
232                 rdev_err(rdev, "operation not allowed\n");
233                 return -EPERM;
234         }
235
236         if (*max_uA > rdev->constraints->max_uA)
237                 *max_uA = rdev->constraints->max_uA;
238         if (*min_uA < rdev->constraints->min_uA)
239                 *min_uA = rdev->constraints->min_uA;
240
241         if (*min_uA > *max_uA) {
242                 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
243                          *min_uA, *max_uA);
244                 return -EINVAL;
245         }
246
247         return 0;
248 }
249
250 /* operating mode constraint check */
251 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
252 {
253         switch (*mode) {
254         case REGULATOR_MODE_FAST:
255         case REGULATOR_MODE_NORMAL:
256         case REGULATOR_MODE_IDLE:
257         case REGULATOR_MODE_STANDBY:
258                 break;
259         default:
260                 rdev_err(rdev, "invalid mode %x specified\n", *mode);
261                 return -EINVAL;
262         }
263
264         if (!rdev->constraints) {
265                 rdev_err(rdev, "no constraints\n");
266                 return -ENODEV;
267         }
268         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
269                 rdev_err(rdev, "operation not allowed\n");
270                 return -EPERM;
271         }
272
273         /* The modes are bitmasks, the most power hungry modes having
274          * the lowest values. If the requested mode isn't supported
275          * try higher modes. */
276         while (*mode) {
277                 if (rdev->constraints->valid_modes_mask & *mode)
278                         return 0;
279                 *mode /= 2;
280         }
281
282         return -EINVAL;
283 }
284
285 /* dynamic regulator mode switching constraint check */
286 static int regulator_check_drms(struct regulator_dev *rdev)
287 {
288         if (!rdev->constraints) {
289                 rdev_err(rdev, "no constraints\n");
290                 return -ENODEV;
291         }
292         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
293                 rdev_err(rdev, "operation not allowed\n");
294                 return -EPERM;
295         }
296         return 0;
297 }
298
299 static ssize_t regulator_uV_show(struct device *dev,
300                                 struct device_attribute *attr, char *buf)
301 {
302         struct regulator_dev *rdev = dev_get_drvdata(dev);
303         ssize_t ret;
304
305         mutex_lock(&rdev->mutex);
306         ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
307         mutex_unlock(&rdev->mutex);
308
309         return ret;
310 }
311 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
312
313 static ssize_t regulator_uA_show(struct device *dev,
314                                 struct device_attribute *attr, char *buf)
315 {
316         struct regulator_dev *rdev = dev_get_drvdata(dev);
317
318         return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
319 }
320 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
321
322 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
323                          char *buf)
324 {
325         struct regulator_dev *rdev = dev_get_drvdata(dev);
326
327         return sprintf(buf, "%s\n", rdev_get_name(rdev));
328 }
329 static DEVICE_ATTR_RO(name);
330
331 static ssize_t regulator_print_opmode(char *buf, int mode)
332 {
333         switch (mode) {
334         case REGULATOR_MODE_FAST:
335                 return sprintf(buf, "fast\n");
336         case REGULATOR_MODE_NORMAL:
337                 return sprintf(buf, "normal\n");
338         case REGULATOR_MODE_IDLE:
339                 return sprintf(buf, "idle\n");
340         case REGULATOR_MODE_STANDBY:
341                 return sprintf(buf, "standby\n");
342         }
343         return sprintf(buf, "unknown\n");
344 }
345
346 static ssize_t regulator_opmode_show(struct device *dev,
347                                     struct device_attribute *attr, char *buf)
348 {
349         struct regulator_dev *rdev = dev_get_drvdata(dev);
350
351         return regulator_print_opmode(buf, _regulator_get_mode(rdev));
352 }
353 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
354
355 static ssize_t regulator_print_state(char *buf, int state)
356 {
357         if (state > 0)
358                 return sprintf(buf, "enabled\n");
359         else if (state == 0)
360                 return sprintf(buf, "disabled\n");
361         else
362                 return sprintf(buf, "unknown\n");
363 }
364
365 static ssize_t regulator_state_show(struct device *dev,
366                                    struct device_attribute *attr, char *buf)
367 {
368         struct regulator_dev *rdev = dev_get_drvdata(dev);
369         ssize_t ret;
370
371         mutex_lock(&rdev->mutex);
372         ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
373         mutex_unlock(&rdev->mutex);
374
375         return ret;
376 }
377 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
378
379 static ssize_t regulator_status_show(struct device *dev,
380                                    struct device_attribute *attr, char *buf)
381 {
382         struct regulator_dev *rdev = dev_get_drvdata(dev);
383         int status;
384         char *label;
385
386         status = rdev->desc->ops->get_status(rdev);
387         if (status < 0)
388                 return status;
389
390         switch (status) {
391         case REGULATOR_STATUS_OFF:
392                 label = "off";
393                 break;
394         case REGULATOR_STATUS_ON:
395                 label = "on";
396                 break;
397         case REGULATOR_STATUS_ERROR:
398                 label = "error";
399                 break;
400         case REGULATOR_STATUS_FAST:
401                 label = "fast";
402                 break;
403         case REGULATOR_STATUS_NORMAL:
404                 label = "normal";
405                 break;
406         case REGULATOR_STATUS_IDLE:
407                 label = "idle";
408                 break;
409         case REGULATOR_STATUS_STANDBY:
410                 label = "standby";
411                 break;
412         case REGULATOR_STATUS_BYPASS:
413                 label = "bypass";
414                 break;
415         case REGULATOR_STATUS_UNDEFINED:
416                 label = "undefined";
417                 break;
418         default:
419                 return -ERANGE;
420         }
421
422         return sprintf(buf, "%s\n", label);
423 }
424 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
425
426 static ssize_t regulator_min_uA_show(struct device *dev,
427                                     struct device_attribute *attr, char *buf)
428 {
429         struct regulator_dev *rdev = dev_get_drvdata(dev);
430
431         if (!rdev->constraints)
432                 return sprintf(buf, "constraint not defined\n");
433
434         return sprintf(buf, "%d\n", rdev->constraints->min_uA);
435 }
436 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
437
438 static ssize_t regulator_max_uA_show(struct device *dev,
439                                     struct device_attribute *attr, char *buf)
440 {
441         struct regulator_dev *rdev = dev_get_drvdata(dev);
442
443         if (!rdev->constraints)
444                 return sprintf(buf, "constraint not defined\n");
445
446         return sprintf(buf, "%d\n", rdev->constraints->max_uA);
447 }
448 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
449
450 static ssize_t regulator_min_uV_show(struct device *dev,
451                                     struct device_attribute *attr, char *buf)
452 {
453         struct regulator_dev *rdev = dev_get_drvdata(dev);
454
455         if (!rdev->constraints)
456                 return sprintf(buf, "constraint not defined\n");
457
458         return sprintf(buf, "%d\n", rdev->constraints->min_uV);
459 }
460 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
461
462 static ssize_t regulator_max_uV_show(struct device *dev,
463                                     struct device_attribute *attr, char *buf)
464 {
465         struct regulator_dev *rdev = dev_get_drvdata(dev);
466
467         if (!rdev->constraints)
468                 return sprintf(buf, "constraint not defined\n");
469
470         return sprintf(buf, "%d\n", rdev->constraints->max_uV);
471 }
472 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
473
474 static ssize_t regulator_total_uA_show(struct device *dev,
475                                       struct device_attribute *attr, char *buf)
476 {
477         struct regulator_dev *rdev = dev_get_drvdata(dev);
478         struct regulator *regulator;
479         int uA = 0;
480
481         mutex_lock(&rdev->mutex);
482         list_for_each_entry(regulator, &rdev->consumer_list, list)
483                 uA += regulator->uA_load;
484         mutex_unlock(&rdev->mutex);
485         return sprintf(buf, "%d\n", uA);
486 }
487 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
488
489 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
490                               char *buf)
491 {
492         struct regulator_dev *rdev = dev_get_drvdata(dev);
493         return sprintf(buf, "%d\n", rdev->use_count);
494 }
495 static DEVICE_ATTR_RO(num_users);
496
497 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
498                          char *buf)
499 {
500         struct regulator_dev *rdev = dev_get_drvdata(dev);
501
502         switch (rdev->desc->type) {
503         case REGULATOR_VOLTAGE:
504                 return sprintf(buf, "voltage\n");
505         case REGULATOR_CURRENT:
506                 return sprintf(buf, "current\n");
507         }
508         return sprintf(buf, "unknown\n");
509 }
510 static DEVICE_ATTR_RO(type);
511
512 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
513                                 struct device_attribute *attr, char *buf)
514 {
515         struct regulator_dev *rdev = dev_get_drvdata(dev);
516
517         return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
518 }
519 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
520                 regulator_suspend_mem_uV_show, NULL);
521
522 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
523                                 struct device_attribute *attr, char *buf)
524 {
525         struct regulator_dev *rdev = dev_get_drvdata(dev);
526
527         return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
528 }
529 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
530                 regulator_suspend_disk_uV_show, NULL);
531
532 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
533                                 struct device_attribute *attr, char *buf)
534 {
535         struct regulator_dev *rdev = dev_get_drvdata(dev);
536
537         return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
538 }
539 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
540                 regulator_suspend_standby_uV_show, NULL);
541
542 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
543                                 struct device_attribute *attr, char *buf)
544 {
545         struct regulator_dev *rdev = dev_get_drvdata(dev);
546
547         return regulator_print_opmode(buf,
548                 rdev->constraints->state_mem.mode);
549 }
550 static DEVICE_ATTR(suspend_mem_mode, 0444,
551                 regulator_suspend_mem_mode_show, NULL);
552
553 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
554                                 struct device_attribute *attr, char *buf)
555 {
556         struct regulator_dev *rdev = dev_get_drvdata(dev);
557
558         return regulator_print_opmode(buf,
559                 rdev->constraints->state_disk.mode);
560 }
561 static DEVICE_ATTR(suspend_disk_mode, 0444,
562                 regulator_suspend_disk_mode_show, NULL);
563
564 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
565                                 struct device_attribute *attr, char *buf)
566 {
567         struct regulator_dev *rdev = dev_get_drvdata(dev);
568
569         return regulator_print_opmode(buf,
570                 rdev->constraints->state_standby.mode);
571 }
572 static DEVICE_ATTR(suspend_standby_mode, 0444,
573                 regulator_suspend_standby_mode_show, NULL);
574
575 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
576                                    struct device_attribute *attr, char *buf)
577 {
578         struct regulator_dev *rdev = dev_get_drvdata(dev);
579
580         return regulator_print_state(buf,
581                         rdev->constraints->state_mem.enabled);
582 }
583 static DEVICE_ATTR(suspend_mem_state, 0444,
584                 regulator_suspend_mem_state_show, NULL);
585
586 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
587                                    struct device_attribute *attr, char *buf)
588 {
589         struct regulator_dev *rdev = dev_get_drvdata(dev);
590
591         return regulator_print_state(buf,
592                         rdev->constraints->state_disk.enabled);
593 }
594 static DEVICE_ATTR(suspend_disk_state, 0444,
595                 regulator_suspend_disk_state_show, NULL);
596
597 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
598                                    struct device_attribute *attr, char *buf)
599 {
600         struct regulator_dev *rdev = dev_get_drvdata(dev);
601
602         return regulator_print_state(buf,
603                         rdev->constraints->state_standby.enabled);
604 }
605 static DEVICE_ATTR(suspend_standby_state, 0444,
606                 regulator_suspend_standby_state_show, NULL);
607
608 static ssize_t regulator_bypass_show(struct device *dev,
609                                      struct device_attribute *attr, char *buf)
610 {
611         struct regulator_dev *rdev = dev_get_drvdata(dev);
612         const char *report;
613         bool bypass;
614         int ret;
615
616         ret = rdev->desc->ops->get_bypass(rdev, &bypass);
617
618         if (ret != 0)
619                 report = "unknown";
620         else if (bypass)
621                 report = "enabled";
622         else
623                 report = "disabled";
624
625         return sprintf(buf, "%s\n", report);
626 }
627 static DEVICE_ATTR(bypass, 0444,
628                    regulator_bypass_show, NULL);
629
630 /*
631  * These are the only attributes are present for all regulators.
632  * Other attributes are a function of regulator functionality.
633  */
634 static struct attribute *regulator_dev_attrs[] = {
635         &dev_attr_name.attr,
636         &dev_attr_num_users.attr,
637         &dev_attr_type.attr,
638         NULL,
639 };
640 ATTRIBUTE_GROUPS(regulator_dev);
641
642 static void regulator_dev_release(struct device *dev)
643 {
644         struct regulator_dev *rdev = dev_get_drvdata(dev);
645         kfree(rdev);
646 }
647
648 static struct class regulator_class = {
649         .name = "regulator",
650         .dev_release = regulator_dev_release,
651         .dev_groups = regulator_dev_groups,
652 };
653
654 /* Calculate the new optimum regulator operating mode based on the new total
655  * consumer load. All locks held by caller */
656 static void drms_uA_update(struct regulator_dev *rdev)
657 {
658         struct regulator *sibling;
659         int current_uA = 0, output_uV, input_uV, err;
660         unsigned int mode;
661
662         err = regulator_check_drms(rdev);
663         if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
664             (!rdev->desc->ops->get_voltage &&
665              !rdev->desc->ops->get_voltage_sel) ||
666             !rdev->desc->ops->set_mode)
667                 return;
668
669         /* get output voltage */
670         output_uV = _regulator_get_voltage(rdev);
671         if (output_uV <= 0)
672                 return;
673
674         /* get input voltage */
675         input_uV = 0;
676         if (rdev->supply)
677                 input_uV = regulator_get_voltage(rdev->supply);
678         if (input_uV <= 0)
679                 input_uV = rdev->constraints->input_uV;
680         if (input_uV <= 0)
681                 return;
682
683         /* calc total requested load */
684         list_for_each_entry(sibling, &rdev->consumer_list, list)
685                 current_uA += sibling->uA_load;
686
687         /* now get the optimum mode for our new total regulator load */
688         mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
689                                                   output_uV, current_uA);
690
691         /* check the new mode is allowed */
692         err = regulator_mode_constrain(rdev, &mode);
693         if (err == 0)
694                 rdev->desc->ops->set_mode(rdev, mode);
695 }
696
697 static int suspend_set_state(struct regulator_dev *rdev,
698         struct regulator_state *rstate)
699 {
700         int ret = 0;
701
702         /* If we have no suspend mode configration don't set anything;
703          * only warn if the driver implements set_suspend_voltage or
704          * set_suspend_mode callback.
705          */
706         if (!rstate->enabled && !rstate->disabled) {
707                 if (rdev->desc->ops->set_suspend_voltage ||
708                     rdev->desc->ops->set_suspend_mode)
709                         rdev_warn(rdev, "No configuration\n");
710                 return 0;
711         }
712
713         if (rstate->enabled && rstate->disabled) {
714                 rdev_err(rdev, "invalid configuration\n");
715                 return -EINVAL;
716         }
717
718         if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
719                 ret = rdev->desc->ops->set_suspend_enable(rdev);
720         else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
721                 ret = rdev->desc->ops->set_suspend_disable(rdev);
722         else /* OK if set_suspend_enable or set_suspend_disable is NULL */
723                 ret = 0;
724
725         if (ret < 0) {
726                 rdev_err(rdev, "failed to enabled/disable\n");
727                 return ret;
728         }
729
730         if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
731                 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
732                 if (ret < 0) {
733                         rdev_err(rdev, "failed to set voltage\n");
734                         return ret;
735                 }
736         }
737
738         if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
739                 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
740                 if (ret < 0) {
741                         rdev_err(rdev, "failed to set mode\n");
742                         return ret;
743                 }
744         }
745         return ret;
746 }
747
748 /* locks held by caller */
749 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
750 {
751         if (!rdev->constraints)
752                 return -EINVAL;
753
754         switch (state) {
755         case PM_SUSPEND_STANDBY:
756                 return suspend_set_state(rdev,
757                         &rdev->constraints->state_standby);
758         case PM_SUSPEND_MEM:
759                 return suspend_set_state(rdev,
760                         &rdev->constraints->state_mem);
761         case PM_SUSPEND_MAX:
762                 return suspend_set_state(rdev,
763                         &rdev->constraints->state_disk);
764         default:
765                 return -EINVAL;
766         }
767 }
768
769 static void print_constraints(struct regulator_dev *rdev)
770 {
771         struct regulation_constraints *constraints = rdev->constraints;
772         char buf[80] = "";
773         int count = 0;
774         int ret;
775
776         if (constraints->min_uV && constraints->max_uV) {
777                 if (constraints->min_uV == constraints->max_uV)
778                         count += sprintf(buf + count, "%d mV ",
779                                          constraints->min_uV / 1000);
780                 else
781                         count += sprintf(buf + count, "%d <--> %d mV ",
782                                          constraints->min_uV / 1000,
783                                          constraints->max_uV / 1000);
784         }
785
786         if (!constraints->min_uV ||
787             constraints->min_uV != constraints->max_uV) {
788                 ret = _regulator_get_voltage(rdev);
789                 if (ret > 0)
790                         count += sprintf(buf + count, "at %d mV ", ret / 1000);
791         }
792
793         if (constraints->uV_offset)
794                 count += sprintf(buf, "%dmV offset ",
795                                  constraints->uV_offset / 1000);
796
797         if (constraints->min_uA && constraints->max_uA) {
798                 if (constraints->min_uA == constraints->max_uA)
799                         count += sprintf(buf + count, "%d mA ",
800                                          constraints->min_uA / 1000);
801                 else
802                         count += sprintf(buf + count, "%d <--> %d mA ",
803                                          constraints->min_uA / 1000,
804                                          constraints->max_uA / 1000);
805         }
806
807         if (!constraints->min_uA ||
808             constraints->min_uA != constraints->max_uA) {
809                 ret = _regulator_get_current_limit(rdev);
810                 if (ret > 0)
811                         count += sprintf(buf + count, "at %d mA ", ret / 1000);
812         }
813
814         if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
815                 count += sprintf(buf + count, "fast ");
816         if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
817                 count += sprintf(buf + count, "normal ");
818         if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
819                 count += sprintf(buf + count, "idle ");
820         if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
821                 count += sprintf(buf + count, "standby");
822
823         if (!count)
824                 sprintf(buf, "no parameters");
825
826         rdev_info(rdev, "%s\n", buf);
827
828         if ((constraints->min_uV != constraints->max_uV) &&
829             !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
830                 rdev_warn(rdev,
831                           "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
832 }
833
834 static int machine_constraints_voltage(struct regulator_dev *rdev,
835         struct regulation_constraints *constraints)
836 {
837         struct regulator_ops *ops = rdev->desc->ops;
838         int ret;
839
840         /* do we need to apply the constraint voltage */
841         if (rdev->constraints->apply_uV &&
842             rdev->constraints->min_uV == rdev->constraints->max_uV) {
843                 ret = _regulator_do_set_voltage(rdev,
844                                                 rdev->constraints->min_uV,
845                                                 rdev->constraints->max_uV);
846                 if (ret < 0) {
847                         rdev_err(rdev, "failed to apply %duV constraint\n",
848                                  rdev->constraints->min_uV);
849                         return ret;
850                 }
851         }
852
853         /* constrain machine-level voltage specs to fit
854          * the actual range supported by this regulator.
855          */
856         if (ops->list_voltage && rdev->desc->n_voltages) {
857                 int     count = rdev->desc->n_voltages;
858                 int     i;
859                 int     min_uV = INT_MAX;
860                 int     max_uV = INT_MIN;
861                 int     cmin = constraints->min_uV;
862                 int     cmax = constraints->max_uV;
863
864                 /* it's safe to autoconfigure fixed-voltage supplies
865                    and the constraints are used by list_voltage. */
866                 if (count == 1 && !cmin) {
867                         cmin = 1;
868                         cmax = INT_MAX;
869                         constraints->min_uV = cmin;
870                         constraints->max_uV = cmax;
871                 }
872
873                 /* voltage constraints are optional */
874                 if ((cmin == 0) && (cmax == 0))
875                         return 0;
876
877                 /* else require explicit machine-level constraints */
878                 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
879                         rdev_err(rdev, "invalid voltage constraints\n");
880                         return -EINVAL;
881                 }
882
883                 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
884                 for (i = 0; i < count; i++) {
885                         int     value;
886
887                         value = ops->list_voltage(rdev, i);
888                         if (value <= 0)
889                                 continue;
890
891                         /* maybe adjust [min_uV..max_uV] */
892                         if (value >= cmin && value < min_uV)
893                                 min_uV = value;
894                         if (value <= cmax && value > max_uV)
895                                 max_uV = value;
896                 }
897
898                 /* final: [min_uV..max_uV] valid iff constraints valid */
899                 if (max_uV < min_uV) {
900                         rdev_err(rdev,
901                                  "unsupportable voltage constraints %u-%uuV\n",
902                                  min_uV, max_uV);
903                         return -EINVAL;
904                 }
905
906                 /* use regulator's subset of machine constraints */
907                 if (constraints->min_uV < min_uV) {
908                         rdev_dbg(rdev, "override min_uV, %d -> %d\n",
909                                  constraints->min_uV, min_uV);
910                         constraints->min_uV = min_uV;
911                 }
912                 if (constraints->max_uV > max_uV) {
913                         rdev_dbg(rdev, "override max_uV, %d -> %d\n",
914                                  constraints->max_uV, max_uV);
915                         constraints->max_uV = max_uV;
916                 }
917         }
918
919         return 0;
920 }
921
922 static int machine_constraints_current(struct regulator_dev *rdev,
923         struct regulation_constraints *constraints)
924 {
925         struct regulator_ops *ops = rdev->desc->ops;
926         int ret;
927
928         if (!constraints->min_uA && !constraints->max_uA)
929                 return 0;
930
931         if (constraints->min_uA > constraints->max_uA) {
932                 rdev_err(rdev, "Invalid current constraints\n");
933                 return -EINVAL;
934         }
935
936         if (!ops->set_current_limit || !ops->get_current_limit) {
937                 rdev_warn(rdev, "Operation of current configuration missing\n");
938                 return 0;
939         }
940
941         /* Set regulator current in constraints range */
942         ret = ops->set_current_limit(rdev, constraints->min_uA,
943                         constraints->max_uA);
944         if (ret < 0) {
945                 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
946                 return ret;
947         }
948
949         return 0;
950 }
951
952 /**
953  * set_machine_constraints - sets regulator constraints
954  * @rdev: regulator source
955  * @constraints: constraints to apply
956  *
957  * Allows platform initialisation code to define and constrain
958  * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
959  * Constraints *must* be set by platform code in order for some
960  * regulator operations to proceed i.e. set_voltage, set_current_limit,
961  * set_mode.
962  */
963 static int set_machine_constraints(struct regulator_dev *rdev,
964         const struct regulation_constraints *constraints)
965 {
966         int ret = 0;
967         struct regulator_ops *ops = rdev->desc->ops;
968
969         if (constraints)
970                 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
971                                             GFP_KERNEL);
972         else
973                 rdev->constraints = kzalloc(sizeof(*constraints),
974                                             GFP_KERNEL);
975         if (!rdev->constraints)
976                 return -ENOMEM;
977
978         ret = machine_constraints_voltage(rdev, rdev->constraints);
979         if (ret != 0)
980                 goto out;
981
982         ret = machine_constraints_current(rdev, rdev->constraints);
983         if (ret != 0)
984                 goto out;
985
986         /* do we need to setup our suspend state */
987         if (rdev->constraints->initial_state) {
988                 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
989                 if (ret < 0) {
990                         rdev_err(rdev, "failed to set suspend state\n");
991                         goto out;
992                 }
993         }
994
995         if (rdev->constraints->initial_mode) {
996                 if (!ops->set_mode) {
997                         rdev_err(rdev, "no set_mode operation\n");
998                         ret = -EINVAL;
999                         goto out;
1000                 }
1001
1002                 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1003                 if (ret < 0) {
1004                         rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1005                         goto out;
1006                 }
1007         }
1008
1009         /* If the constraints say the regulator should be on at this point
1010          * and we have control then make sure it is enabled.
1011          */
1012         if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
1013             ops->enable) {
1014                 ret = ops->enable(rdev);
1015                 if (ret < 0) {
1016                         rdev_err(rdev, "failed to enable\n");
1017                         goto out;
1018                 }
1019         }
1020
1021         if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1022                 && ops->set_ramp_delay) {
1023                 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1024                 if (ret < 0) {
1025                         rdev_err(rdev, "failed to set ramp_delay\n");
1026                         goto out;
1027                 }
1028         }
1029
1030         print_constraints(rdev);
1031         return 0;
1032 out:
1033         kfree(rdev->constraints);
1034         rdev->constraints = NULL;
1035         return ret;
1036 }
1037
1038 /**
1039  * set_supply - set regulator supply regulator
1040  * @rdev: regulator name
1041  * @supply_rdev: supply regulator name
1042  *
1043  * Called by platform initialisation code to set the supply regulator for this
1044  * regulator. This ensures that a regulators supply will also be enabled by the
1045  * core if it's child is enabled.
1046  */
1047 static int set_supply(struct regulator_dev *rdev,
1048                       struct regulator_dev *supply_rdev)
1049 {
1050         int err;
1051
1052         rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1053
1054         rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1055         if (rdev->supply == NULL) {
1056                 err = -ENOMEM;
1057                 return err;
1058         }
1059         supply_rdev->open_count++;
1060
1061         return 0;
1062 }
1063
1064 /**
1065  * set_consumer_device_supply - Bind a regulator to a symbolic supply
1066  * @rdev:         regulator source
1067  * @consumer_dev_name: dev_name() string for device supply applies to
1068  * @supply:       symbolic name for supply
1069  *
1070  * Allows platform initialisation code to map physical regulator
1071  * sources to symbolic names for supplies for use by devices.  Devices
1072  * should use these symbolic names to request regulators, avoiding the
1073  * need to provide board-specific regulator names as platform data.
1074  */
1075 static int set_consumer_device_supply(struct regulator_dev *rdev,
1076                                       const char *consumer_dev_name,
1077                                       const char *supply)
1078 {
1079         struct regulator_map *node;
1080         int has_dev;
1081
1082         if (supply == NULL)
1083                 return -EINVAL;
1084
1085         if (consumer_dev_name != NULL)
1086                 has_dev = 1;
1087         else
1088                 has_dev = 0;
1089
1090         list_for_each_entry(node, &regulator_map_list, list) {
1091                 if (node->dev_name && consumer_dev_name) {
1092                         if (strcmp(node->dev_name, consumer_dev_name) != 0)
1093                                 continue;
1094                 } else if (node->dev_name || consumer_dev_name) {
1095                         continue;
1096                 }
1097
1098                 if (strcmp(node->supply, supply) != 0)
1099                         continue;
1100
1101                 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1102                          consumer_dev_name,
1103                          dev_name(&node->regulator->dev),
1104                          node->regulator->desc->name,
1105                          supply,
1106                          dev_name(&rdev->dev), rdev_get_name(rdev));
1107                 return -EBUSY;
1108         }
1109
1110         node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1111         if (node == NULL)
1112                 return -ENOMEM;
1113
1114         node->regulator = rdev;
1115         node->supply = supply;
1116
1117         if (has_dev) {
1118                 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1119                 if (node->dev_name == NULL) {
1120                         kfree(node);
1121                         return -ENOMEM;
1122                 }
1123         }
1124
1125         list_add(&node->list, &regulator_map_list);
1126         return 0;
1127 }
1128
1129 static void unset_regulator_supplies(struct regulator_dev *rdev)
1130 {
1131         struct regulator_map *node, *n;
1132
1133         list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1134                 if (rdev == node->regulator) {
1135                         list_del(&node->list);
1136                         kfree(node->dev_name);
1137                         kfree(node);
1138                 }
1139         }
1140 }
1141
1142 #define REG_STR_SIZE    64
1143
1144 static struct regulator *create_regulator(struct regulator_dev *rdev,
1145                                           struct device *dev,
1146                                           const char *supply_name)
1147 {
1148         struct regulator *regulator;
1149         char buf[REG_STR_SIZE];
1150         int err, size;
1151
1152         regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1153         if (regulator == NULL)
1154                 return NULL;
1155
1156         mutex_lock(&rdev->mutex);
1157         regulator->rdev = rdev;
1158         list_add(&regulator->list, &rdev->consumer_list);
1159
1160         if (dev) {
1161                 regulator->dev = dev;
1162
1163                 /* Add a link to the device sysfs entry */
1164                 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1165                                  dev->kobj.name, supply_name);
1166                 if (size >= REG_STR_SIZE)
1167                         goto overflow_err;
1168
1169                 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1170                 if (regulator->supply_name == NULL)
1171                         goto overflow_err;
1172
1173                 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1174                                         buf);
1175                 if (err) {
1176                         rdev_warn(rdev, "could not add device link %s err %d\n",
1177                                   dev->kobj.name, err);
1178                         /* non-fatal */
1179                 }
1180         } else {
1181                 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1182                 if (regulator->supply_name == NULL)
1183                         goto overflow_err;
1184         }
1185
1186         regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1187                                                 rdev->debugfs);
1188         if (!regulator->debugfs) {
1189                 rdev_warn(rdev, "Failed to create debugfs directory\n");
1190         } else {
1191                 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1192                                    &regulator->uA_load);
1193                 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1194                                    &regulator->min_uV);
1195                 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1196                                    &regulator->max_uV);
1197         }
1198
1199         /*
1200          * Check now if the regulator is an always on regulator - if
1201          * it is then we don't need to do nearly so much work for
1202          * enable/disable calls.
1203          */
1204         if (!_regulator_can_change_status(rdev) &&
1205             _regulator_is_enabled(rdev))
1206                 regulator->always_on = true;
1207
1208         mutex_unlock(&rdev->mutex);
1209         return regulator;
1210 overflow_err:
1211         list_del(&regulator->list);
1212         kfree(regulator);
1213         mutex_unlock(&rdev->mutex);
1214         return NULL;
1215 }
1216
1217 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1218 {
1219         if (rdev->constraints && rdev->constraints->enable_time)
1220                 return rdev->constraints->enable_time;
1221         if (!rdev->desc->ops->enable_time)
1222                 return rdev->desc->enable_time;
1223         return rdev->desc->ops->enable_time(rdev);
1224 }
1225
1226 static struct regulator_supply_alias *regulator_find_supply_alias(
1227                 struct device *dev, const char *supply)
1228 {
1229         struct regulator_supply_alias *map;
1230
1231         list_for_each_entry(map, &regulator_supply_alias_list, list)
1232                 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1233                         return map;
1234
1235         return NULL;
1236 }
1237
1238 static void regulator_supply_alias(struct device **dev, const char **supply)
1239 {
1240         struct regulator_supply_alias *map;
1241
1242         map = regulator_find_supply_alias(*dev, *supply);
1243         if (map) {
1244                 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1245                                 *supply, map->alias_supply,
1246                                 dev_name(map->alias_dev));
1247                 *dev = map->alias_dev;
1248                 *supply = map->alias_supply;
1249         }
1250 }
1251
1252 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1253                                                   const char *supply,
1254                                                   int *ret)
1255 {
1256         struct regulator_dev *r;
1257         struct device_node *node;
1258         struct regulator_map *map;
1259         const char *devname = NULL;
1260
1261         regulator_supply_alias(&dev, &supply);
1262
1263         /* first do a dt based lookup */
1264         if (dev && dev->of_node) {
1265                 node = of_get_regulator(dev, supply);
1266                 if (node) {
1267                         list_for_each_entry(r, &regulator_list, list)
1268                                 if (r->dev.parent &&
1269                                         node == r->dev.of_node)
1270                                         return r;
1271                 } else {
1272                         /*
1273                          * If we couldn't even get the node then it's
1274                          * not just that the device didn't register
1275                          * yet, there's no node and we'll never
1276                          * succeed.
1277                          */
1278                         *ret = -ENODEV;
1279                 }
1280         }
1281
1282         /* if not found, try doing it non-dt way */
1283         if (dev)
1284                 devname = dev_name(dev);
1285
1286         list_for_each_entry(r, &regulator_list, list)
1287                 if (strcmp(rdev_get_name(r), supply) == 0)
1288                         return r;
1289
1290         list_for_each_entry(map, &regulator_map_list, list) {
1291                 /* If the mapping has a device set up it must match */
1292                 if (map->dev_name &&
1293                     (!devname || strcmp(map->dev_name, devname)))
1294                         continue;
1295
1296                 if (strcmp(map->supply, supply) == 0)
1297                         return map->regulator;
1298         }
1299
1300
1301         return NULL;
1302 }
1303
1304 /* Internal regulator request function */
1305 static struct regulator *_regulator_get(struct device *dev, const char *id,
1306                                         bool exclusive)
1307 {
1308         struct regulator_dev *rdev;
1309         struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1310         const char *devname = NULL;
1311         int ret = 0;
1312
1313         if (id == NULL) {
1314                 pr_err("get() with no identifier\n");
1315                 return ERR_PTR(-EINVAL);
1316         }
1317
1318         if (dev)
1319                 devname = dev_name(dev);
1320
1321         mutex_lock(&regulator_list_mutex);
1322
1323         rdev = regulator_dev_lookup(dev, id, &ret);
1324         if (rdev)
1325                 goto found;
1326
1327         /*
1328          * If we have return value from dev_lookup fail, we do not expect to
1329          * succeed, so, quit with appropriate error value
1330          */
1331         if (ret) {
1332                 regulator = ERR_PTR(ret);
1333                 goto out;
1334         }
1335
1336         if (board_wants_dummy_regulator) {
1337                 rdev = dummy_regulator_rdev;
1338                 goto found;
1339         }
1340
1341 #ifdef CONFIG_REGULATOR_DUMMY
1342         if (!devname)
1343                 devname = "deviceless";
1344
1345         /* If the board didn't flag that it was fully constrained then
1346          * substitute in a dummy regulator so consumers can continue.
1347          */
1348         if (!has_full_constraints) {
1349                 pr_warn("%s supply %s not found, using dummy regulator\n",
1350                         devname, id);
1351                 rdev = dummy_regulator_rdev;
1352                 goto found;
1353         }
1354 #endif
1355
1356         mutex_unlock(&regulator_list_mutex);
1357         return regulator;
1358
1359 found:
1360         if (rdev->exclusive) {
1361                 regulator = ERR_PTR(-EPERM);
1362                 goto out;
1363         }
1364
1365         if (exclusive && rdev->open_count) {
1366                 regulator = ERR_PTR(-EBUSY);
1367                 goto out;
1368         }
1369
1370         if (!try_module_get(rdev->owner))
1371                 goto out;
1372
1373         regulator = create_regulator(rdev, dev, id);
1374         if (regulator == NULL) {
1375                 regulator = ERR_PTR(-ENOMEM);
1376                 module_put(rdev->owner);
1377                 goto out;
1378         }
1379
1380         rdev->open_count++;
1381         if (exclusive) {
1382                 rdev->exclusive = 1;
1383
1384                 ret = _regulator_is_enabled(rdev);
1385                 if (ret > 0)
1386                         rdev->use_count = 1;
1387                 else
1388                         rdev->use_count = 0;
1389         }
1390
1391 out:
1392         mutex_unlock(&regulator_list_mutex);
1393
1394         return regulator;
1395 }
1396
1397 /**
1398  * regulator_get - lookup and obtain a reference to a regulator.
1399  * @dev: device for regulator "consumer"
1400  * @id: Supply name or regulator ID.
1401  *
1402  * Returns a struct regulator corresponding to the regulator producer,
1403  * or IS_ERR() condition containing errno.
1404  *
1405  * Use of supply names configured via regulator_set_device_supply() is
1406  * strongly encouraged.  It is recommended that the supply name used
1407  * should match the name used for the supply and/or the relevant
1408  * device pins in the datasheet.
1409  */
1410 struct regulator *regulator_get(struct device *dev, const char *id)
1411 {
1412         return _regulator_get(dev, id, false);
1413 }
1414 EXPORT_SYMBOL_GPL(regulator_get);
1415
1416 /**
1417  * regulator_get_exclusive - obtain exclusive access to a regulator.
1418  * @dev: device for regulator "consumer"
1419  * @id: Supply name or regulator ID.
1420  *
1421  * Returns a struct regulator corresponding to the regulator producer,
1422  * or IS_ERR() condition containing errno.  Other consumers will be
1423  * unable to obtain this reference is held and the use count for the
1424  * regulator will be initialised to reflect the current state of the
1425  * regulator.
1426  *
1427  * This is intended for use by consumers which cannot tolerate shared
1428  * use of the regulator such as those which need to force the
1429  * regulator off for correct operation of the hardware they are
1430  * controlling.
1431  *
1432  * Use of supply names configured via regulator_set_device_supply() is
1433  * strongly encouraged.  It is recommended that the supply name used
1434  * should match the name used for the supply and/or the relevant
1435  * device pins in the datasheet.
1436  */
1437 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1438 {
1439         return _regulator_get(dev, id, true);
1440 }
1441 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1442
1443 /**
1444  * regulator_get_optional - obtain optional access to a regulator.
1445  * @dev: device for regulator "consumer"
1446  * @id: Supply name or regulator ID.
1447  *
1448  * Returns a struct regulator corresponding to the regulator producer,
1449  * or IS_ERR() condition containing errno.  Other consumers will be
1450  * unable to obtain this reference is held and the use count for the
1451  * regulator will be initialised to reflect the current state of the
1452  * regulator.
1453  *
1454  * This is intended for use by consumers for devices which can have
1455  * some supplies unconnected in normal use, such as some MMC devices.
1456  * It can allow the regulator core to provide stub supplies for other
1457  * supplies requested using normal regulator_get() calls without
1458  * disrupting the operation of drivers that can handle absent
1459  * supplies.
1460  *
1461  * Use of supply names configured via regulator_set_device_supply() is
1462  * strongly encouraged.  It is recommended that the supply name used
1463  * should match the name used for the supply and/or the relevant
1464  * device pins in the datasheet.
1465  */
1466 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1467 {
1468         return _regulator_get(dev, id, 0);
1469 }
1470 EXPORT_SYMBOL_GPL(regulator_get_optional);
1471
1472 /* Locks held by regulator_put() */
1473 static void _regulator_put(struct regulator *regulator)
1474 {
1475         struct regulator_dev *rdev;
1476
1477         if (regulator == NULL || IS_ERR(regulator))
1478                 return;
1479
1480         rdev = regulator->rdev;
1481
1482         debugfs_remove_recursive(regulator->debugfs);
1483
1484         /* remove any sysfs entries */
1485         if (regulator->dev)
1486                 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1487         kfree(regulator->supply_name);
1488         list_del(&regulator->list);
1489         kfree(regulator);
1490
1491         rdev->open_count--;
1492         rdev->exclusive = 0;
1493
1494         module_put(rdev->owner);
1495 }
1496
1497 /**
1498  * regulator_put - "free" the regulator source
1499  * @regulator: regulator source
1500  *
1501  * Note: drivers must ensure that all regulator_enable calls made on this
1502  * regulator source are balanced by regulator_disable calls prior to calling
1503  * this function.
1504  */
1505 void regulator_put(struct regulator *regulator)
1506 {
1507         mutex_lock(&regulator_list_mutex);
1508         _regulator_put(regulator);
1509         mutex_unlock(&regulator_list_mutex);
1510 }
1511 EXPORT_SYMBOL_GPL(regulator_put);
1512
1513 /**
1514  * regulator_register_supply_alias - Provide device alias for supply lookup
1515  *
1516  * @dev: device that will be given as the regulator "consumer"
1517  * @id: Supply name or regulator ID
1518  * @alias_dev: device that should be used to lookup the supply
1519  * @alias_id: Supply name or regulator ID that should be used to lookup the
1520  * supply
1521  *
1522  * All lookups for id on dev will instead be conducted for alias_id on
1523  * alias_dev.
1524  */
1525 int regulator_register_supply_alias(struct device *dev, const char *id,
1526                                     struct device *alias_dev,
1527                                     const char *alias_id)
1528 {
1529         struct regulator_supply_alias *map;
1530
1531         map = regulator_find_supply_alias(dev, id);
1532         if (map)
1533                 return -EEXIST;
1534
1535         map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1536         if (!map)
1537                 return -ENOMEM;
1538
1539         map->src_dev = dev;
1540         map->src_supply = id;
1541         map->alias_dev = alias_dev;
1542         map->alias_supply = alias_id;
1543
1544         list_add(&map->list, &regulator_supply_alias_list);
1545
1546         pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1547                 id, dev_name(dev), alias_id, dev_name(alias_dev));
1548
1549         return 0;
1550 }
1551 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1552
1553 /**
1554  * regulator_unregister_supply_alias - Remove device alias
1555  *
1556  * @dev: device that will be given as the regulator "consumer"
1557  * @id: Supply name or regulator ID
1558  *
1559  * Remove a lookup alias if one exists for id on dev.
1560  */
1561 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1562 {
1563         struct regulator_supply_alias *map;
1564
1565         map = regulator_find_supply_alias(dev, id);
1566         if (map) {
1567                 list_del(&map->list);
1568                 kfree(map);
1569         }
1570 }
1571 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1572
1573 /**
1574  * regulator_bulk_register_supply_alias - register multiple aliases
1575  *
1576  * @dev: device that will be given as the regulator "consumer"
1577  * @id: List of supply names or regulator IDs
1578  * @alias_dev: device that should be used to lookup the supply
1579  * @alias_id: List of supply names or regulator IDs that should be used to
1580  * lookup the supply
1581  * @num_id: Number of aliases to register
1582  *
1583  * @return 0 on success, an errno on failure.
1584  *
1585  * This helper function allows drivers to register several supply
1586  * aliases in one operation.  If any of the aliases cannot be
1587  * registered any aliases that were registered will be removed
1588  * before returning to the caller.
1589  */
1590 int regulator_bulk_register_supply_alias(struct device *dev, const char **id,
1591                                          struct device *alias_dev,
1592                                          const char **alias_id,
1593                                          int num_id)
1594 {
1595         int i;
1596         int ret;
1597
1598         for (i = 0; i < num_id; ++i) {
1599                 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1600                                                       alias_id[i]);
1601                 if (ret < 0)
1602                         goto err;
1603         }
1604
1605         return 0;
1606
1607 err:
1608         dev_err(dev,
1609                 "Failed to create supply alias %s,%s -> %s,%s\n",
1610                 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1611
1612         while (--i >= 0)
1613                 regulator_unregister_supply_alias(dev, id[i]);
1614
1615         return ret;
1616 }
1617 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1618
1619 /**
1620  * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1621  *
1622  * @dev: device that will be given as the regulator "consumer"
1623  * @id: List of supply names or regulator IDs
1624  * @num_id: Number of aliases to unregister
1625  *
1626  * This helper function allows drivers to unregister several supply
1627  * aliases in one operation.
1628  */
1629 void regulator_bulk_unregister_supply_alias(struct device *dev,
1630                                             const char **id,
1631                                             int num_id)
1632 {
1633         int i;
1634
1635         for (i = 0; i < num_id; ++i)
1636                 regulator_unregister_supply_alias(dev, id[i]);
1637 }
1638 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1639
1640
1641 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1642 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1643                                 const struct regulator_config *config)
1644 {
1645         struct regulator_enable_gpio *pin;
1646         int ret;
1647
1648         list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1649                 if (pin->gpio == config->ena_gpio) {
1650                         rdev_dbg(rdev, "GPIO %d is already used\n",
1651                                 config->ena_gpio);
1652                         goto update_ena_gpio_to_rdev;
1653                 }
1654         }
1655
1656         ret = gpio_request_one(config->ena_gpio,
1657                                 GPIOF_DIR_OUT | config->ena_gpio_flags,
1658                                 rdev_get_name(rdev));
1659         if (ret)
1660                 return ret;
1661
1662         pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1663         if (pin == NULL) {
1664                 gpio_free(config->ena_gpio);
1665                 return -ENOMEM;
1666         }
1667
1668         pin->gpio = config->ena_gpio;
1669         pin->ena_gpio_invert = config->ena_gpio_invert;
1670         list_add(&pin->list, &regulator_ena_gpio_list);
1671
1672 update_ena_gpio_to_rdev:
1673         pin->request_count++;
1674         rdev->ena_pin = pin;
1675         return 0;
1676 }
1677
1678 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1679 {
1680         struct regulator_enable_gpio *pin, *n;
1681
1682         if (!rdev->ena_pin)
1683                 return;
1684
1685         /* Free the GPIO only in case of no use */
1686         list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1687                 if (pin->gpio == rdev->ena_pin->gpio) {
1688                         if (pin->request_count <= 1) {
1689                                 pin->request_count = 0;
1690                                 gpio_free(pin->gpio);
1691                                 list_del(&pin->list);
1692                                 kfree(pin);
1693                         } else {
1694                                 pin->request_count--;
1695                         }
1696                 }
1697         }
1698 }
1699
1700 /**
1701  * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1702  * @rdev: regulator_dev structure
1703  * @enable: enable GPIO at initial use?
1704  *
1705  * GPIO is enabled in case of initial use. (enable_count is 0)
1706  * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1707  */
1708 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1709 {
1710         struct regulator_enable_gpio *pin = rdev->ena_pin;
1711
1712         if (!pin)
1713                 return -EINVAL;
1714
1715         if (enable) {
1716                 /* Enable GPIO at initial use */
1717                 if (pin->enable_count == 0)
1718                         gpio_set_value_cansleep(pin->gpio,
1719                                                 !pin->ena_gpio_invert);
1720
1721                 pin->enable_count++;
1722         } else {
1723                 if (pin->enable_count > 1) {
1724                         pin->enable_count--;
1725                         return 0;
1726                 }
1727
1728                 /* Disable GPIO if not used */
1729                 if (pin->enable_count <= 1) {
1730                         gpio_set_value_cansleep(pin->gpio,
1731                                                 pin->ena_gpio_invert);
1732                         pin->enable_count = 0;
1733                 }
1734         }
1735
1736         return 0;
1737 }
1738
1739 static int _regulator_do_enable(struct regulator_dev *rdev)
1740 {
1741         int ret, delay;
1742
1743         /* Query before enabling in case configuration dependent.  */
1744         ret = _regulator_get_enable_time(rdev);
1745         if (ret >= 0) {
1746                 delay = ret;
1747         } else {
1748                 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1749                 delay = 0;
1750         }
1751
1752         trace_regulator_enable(rdev_get_name(rdev));
1753
1754         if (rdev->ena_pin) {
1755                 ret = regulator_ena_gpio_ctrl(rdev, true);
1756                 if (ret < 0)
1757                         return ret;
1758                 rdev->ena_gpio_state = 1;
1759         } else if (rdev->desc->ops->enable) {
1760                 ret = rdev->desc->ops->enable(rdev);
1761                 if (ret < 0)
1762                         return ret;
1763         } else {
1764                 return -EINVAL;
1765         }
1766
1767         /* Allow the regulator to ramp; it would be useful to extend
1768          * this for bulk operations so that the regulators can ramp
1769          * together.  */
1770         trace_regulator_enable_delay(rdev_get_name(rdev));
1771
1772         /*
1773          * Delay for the requested amount of time as per the guidelines in:
1774          *
1775          *     Documentation/timers/timers-howto.txt
1776          *
1777          * The assumption here is that regulators will never be enabled in
1778          * atomic context and therefore sleeping functions can be used.
1779          */
1780         if (delay) {
1781                 unsigned int ms = delay / 1000;
1782                 unsigned int us = delay % 1000;
1783
1784                 if (ms > 0) {
1785                         /*
1786                          * For small enough values, handle super-millisecond
1787                          * delays in the usleep_range() call below.
1788                          */
1789                         if (ms < 20)
1790                                 us += ms * 1000;
1791                         else
1792                                 msleep(ms);
1793                 }
1794
1795                 /*
1796                  * Give the scheduler some room to coalesce with any other
1797                  * wakeup sources. For delays shorter than 10 us, don't even
1798                  * bother setting up high-resolution timers and just busy-
1799                  * loop.
1800                  */
1801                 if (us >= 10)
1802                         usleep_range(us, us + 100);
1803                 else
1804                         udelay(us);
1805         }
1806
1807         trace_regulator_enable_complete(rdev_get_name(rdev));
1808
1809         return 0;
1810 }
1811
1812 /* locks held by regulator_enable() */
1813 static int _regulator_enable(struct regulator_dev *rdev)
1814 {
1815         int ret;
1816
1817         /* check voltage and requested load before enabling */
1818         if (rdev->constraints &&
1819             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1820                 drms_uA_update(rdev);
1821
1822         if (rdev->use_count == 0) {
1823                 /* The regulator may on if it's not switchable or left on */
1824                 ret = _regulator_is_enabled(rdev);
1825                 if (ret == -EINVAL || ret == 0) {
1826                         if (!_regulator_can_change_status(rdev))
1827                                 return -EPERM;
1828
1829                         ret = _regulator_do_enable(rdev);
1830                         if (ret < 0)
1831                                 return ret;
1832
1833                 } else if (ret < 0) {
1834                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1835                         return ret;
1836                 }
1837                 /* Fallthrough on positive return values - already enabled */
1838         }
1839
1840         rdev->use_count++;
1841
1842         return 0;
1843 }
1844
1845 /**
1846  * regulator_enable - enable regulator output
1847  * @regulator: regulator source
1848  *
1849  * Request that the regulator be enabled with the regulator output at
1850  * the predefined voltage or current value.  Calls to regulator_enable()
1851  * must be balanced with calls to regulator_disable().
1852  *
1853  * NOTE: the output value can be set by other drivers, boot loader or may be
1854  * hardwired in the regulator.
1855  */
1856 int regulator_enable(struct regulator *regulator)
1857 {
1858         struct regulator_dev *rdev = regulator->rdev;
1859         int ret = 0;
1860
1861         if (regulator->always_on)
1862                 return 0;
1863
1864         if (rdev->supply) {
1865                 ret = regulator_enable(rdev->supply);
1866                 if (ret != 0)
1867                         return ret;
1868         }
1869
1870         mutex_lock(&rdev->mutex);
1871         ret = _regulator_enable(rdev);
1872         mutex_unlock(&rdev->mutex);
1873
1874         if (ret != 0 && rdev->supply)
1875                 regulator_disable(rdev->supply);
1876
1877         return ret;
1878 }
1879 EXPORT_SYMBOL_GPL(regulator_enable);
1880
1881 static int _regulator_do_disable(struct regulator_dev *rdev)
1882 {
1883         int ret;
1884
1885         trace_regulator_disable(rdev_get_name(rdev));
1886
1887         if (rdev->ena_pin) {
1888                 ret = regulator_ena_gpio_ctrl(rdev, false);
1889                 if (ret < 0)
1890                         return ret;
1891                 rdev->ena_gpio_state = 0;
1892
1893         } else if (rdev->desc->ops->disable) {
1894                 ret = rdev->desc->ops->disable(rdev);
1895                 if (ret != 0)
1896                         return ret;
1897         }
1898
1899         trace_regulator_disable_complete(rdev_get_name(rdev));
1900
1901         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1902                              NULL);
1903         return 0;
1904 }
1905
1906 /* locks held by regulator_disable() */
1907 static int _regulator_disable(struct regulator_dev *rdev)
1908 {
1909         int ret = 0;
1910
1911         if (WARN(rdev->use_count <= 0,
1912                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
1913                 return -EIO;
1914
1915         /* are we the last user and permitted to disable ? */
1916         if (rdev->use_count == 1 &&
1917             (rdev->constraints && !rdev->constraints->always_on)) {
1918
1919                 /* we are last user */
1920                 if (_regulator_can_change_status(rdev)) {
1921                         ret = _regulator_do_disable(rdev);
1922                         if (ret < 0) {
1923                                 rdev_err(rdev, "failed to disable\n");
1924                                 return ret;
1925                         }
1926                 }
1927
1928                 rdev->use_count = 0;
1929         } else if (rdev->use_count > 1) {
1930
1931                 if (rdev->constraints &&
1932                         (rdev->constraints->valid_ops_mask &
1933                         REGULATOR_CHANGE_DRMS))
1934                         drms_uA_update(rdev);
1935
1936                 rdev->use_count--;
1937         }
1938
1939         return ret;
1940 }
1941
1942 /**
1943  * regulator_disable - disable regulator output
1944  * @regulator: regulator source
1945  *
1946  * Disable the regulator output voltage or current.  Calls to
1947  * regulator_enable() must be balanced with calls to
1948  * regulator_disable().
1949  *
1950  * NOTE: this will only disable the regulator output if no other consumer
1951  * devices have it enabled, the regulator device supports disabling and
1952  * machine constraints permit this operation.
1953  */
1954 int regulator_disable(struct regulator *regulator)
1955 {
1956         struct regulator_dev *rdev = regulator->rdev;
1957         int ret = 0;
1958
1959         if (regulator->always_on)
1960                 return 0;
1961
1962         mutex_lock(&rdev->mutex);
1963         ret = _regulator_disable(rdev);
1964         mutex_unlock(&rdev->mutex);
1965
1966         if (ret == 0 && rdev->supply)
1967                 regulator_disable(rdev->supply);
1968
1969         return ret;
1970 }
1971 EXPORT_SYMBOL_GPL(regulator_disable);
1972
1973 /* locks held by regulator_force_disable() */
1974 static int _regulator_force_disable(struct regulator_dev *rdev)
1975 {
1976         int ret = 0;
1977
1978         /* force disable */
1979         if (rdev->desc->ops->disable) {
1980                 /* ah well, who wants to live forever... */
1981                 ret = rdev->desc->ops->disable(rdev);
1982                 if (ret < 0) {
1983                         rdev_err(rdev, "failed to force disable\n");
1984                         return ret;
1985                 }
1986                 /* notify other consumers that power has been forced off */
1987                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1988                         REGULATOR_EVENT_DISABLE, NULL);
1989         }
1990
1991         return ret;
1992 }
1993
1994 /**
1995  * regulator_force_disable - force disable regulator output
1996  * @regulator: regulator source
1997  *
1998  * Forcibly disable the regulator output voltage or current.
1999  * NOTE: this *will* disable the regulator output even if other consumer
2000  * devices have it enabled. This should be used for situations when device
2001  * damage will likely occur if the regulator is not disabled (e.g. over temp).
2002  */
2003 int regulator_force_disable(struct regulator *regulator)
2004 {
2005         struct regulator_dev *rdev = regulator->rdev;
2006         int ret;
2007
2008         mutex_lock(&rdev->mutex);
2009         regulator->uA_load = 0;
2010         ret = _regulator_force_disable(regulator->rdev);
2011         mutex_unlock(&rdev->mutex);
2012
2013         if (rdev->supply)
2014                 while (rdev->open_count--)
2015                         regulator_disable(rdev->supply);
2016
2017         return ret;
2018 }
2019 EXPORT_SYMBOL_GPL(regulator_force_disable);
2020
2021 static void regulator_disable_work(struct work_struct *work)
2022 {
2023         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2024                                                   disable_work.work);
2025         int count, i, ret;
2026
2027         mutex_lock(&rdev->mutex);
2028
2029         BUG_ON(!rdev->deferred_disables);
2030
2031         count = rdev->deferred_disables;
2032         rdev->deferred_disables = 0;
2033
2034         for (i = 0; i < count; i++) {
2035                 ret = _regulator_disable(rdev);
2036                 if (ret != 0)
2037                         rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2038         }
2039
2040         mutex_unlock(&rdev->mutex);
2041
2042         if (rdev->supply) {
2043                 for (i = 0; i < count; i++) {
2044                         ret = regulator_disable(rdev->supply);
2045                         if (ret != 0) {
2046                                 rdev_err(rdev,
2047                                          "Supply disable failed: %d\n", ret);
2048                         }
2049                 }
2050         }
2051 }
2052
2053 /**
2054  * regulator_disable_deferred - disable regulator output with delay
2055  * @regulator: regulator source
2056  * @ms: miliseconds until the regulator is disabled
2057  *
2058  * Execute regulator_disable() on the regulator after a delay.  This
2059  * is intended for use with devices that require some time to quiesce.
2060  *
2061  * NOTE: this will only disable the regulator output if no other consumer
2062  * devices have it enabled, the regulator device supports disabling and
2063  * machine constraints permit this operation.
2064  */
2065 int regulator_disable_deferred(struct regulator *regulator, int ms)
2066 {
2067         struct regulator_dev *rdev = regulator->rdev;
2068         int ret;
2069
2070         if (regulator->always_on)
2071                 return 0;
2072
2073         if (!ms)
2074                 return regulator_disable(regulator);
2075
2076         mutex_lock(&rdev->mutex);
2077         rdev->deferred_disables++;
2078         mutex_unlock(&rdev->mutex);
2079
2080         ret = queue_delayed_work(system_power_efficient_wq,
2081                                  &rdev->disable_work,
2082                                  msecs_to_jiffies(ms));
2083         if (ret < 0)
2084                 return ret;
2085         else
2086                 return 0;
2087 }
2088 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2089
2090 static int _regulator_is_enabled(struct regulator_dev *rdev)
2091 {
2092         /* A GPIO control always takes precedence */
2093         if (rdev->ena_pin)
2094                 return rdev->ena_gpio_state;
2095
2096         /* If we don't know then assume that the regulator is always on */
2097         if (!rdev->desc->ops->is_enabled)
2098                 return 1;
2099
2100         return rdev->desc->ops->is_enabled(rdev);
2101 }
2102
2103 /**
2104  * regulator_is_enabled - is the regulator output enabled
2105  * @regulator: regulator source
2106  *
2107  * Returns positive if the regulator driver backing the source/client
2108  * has requested that the device be enabled, zero if it hasn't, else a
2109  * negative errno code.
2110  *
2111  * Note that the device backing this regulator handle can have multiple
2112  * users, so it might be enabled even if regulator_enable() was never
2113  * called for this particular source.
2114  */
2115 int regulator_is_enabled(struct regulator *regulator)
2116 {
2117         int ret;
2118
2119         if (regulator->always_on)
2120                 return 1;
2121
2122         mutex_lock(&regulator->rdev->mutex);
2123         ret = _regulator_is_enabled(regulator->rdev);
2124         mutex_unlock(&regulator->rdev->mutex);
2125
2126         return ret;
2127 }
2128 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2129
2130 /**
2131  * regulator_can_change_voltage - check if regulator can change voltage
2132  * @regulator: regulator source
2133  *
2134  * Returns positive if the regulator driver backing the source/client
2135  * can change its voltage, false otherwise. Usefull for detecting fixed
2136  * or dummy regulators and disabling voltage change logic in the client
2137  * driver.
2138  */
2139 int regulator_can_change_voltage(struct regulator *regulator)
2140 {
2141         struct regulator_dev    *rdev = regulator->rdev;
2142
2143         if (rdev->constraints &&
2144             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2145                 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2146                         return 1;
2147
2148                 if (rdev->desc->continuous_voltage_range &&
2149                     rdev->constraints->min_uV && rdev->constraints->max_uV &&
2150                     rdev->constraints->min_uV != rdev->constraints->max_uV)
2151                         return 1;
2152         }
2153
2154         return 0;
2155 }
2156 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2157
2158 /**
2159  * regulator_count_voltages - count regulator_list_voltage() selectors
2160  * @regulator: regulator source
2161  *
2162  * Returns number of selectors, or negative errno.  Selectors are
2163  * numbered starting at zero, and typically correspond to bitfields
2164  * in hardware registers.
2165  */
2166 int regulator_count_voltages(struct regulator *regulator)
2167 {
2168         struct regulator_dev    *rdev = regulator->rdev;
2169
2170         return rdev->desc->n_voltages ? : -EINVAL;
2171 }
2172 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2173
2174 /**
2175  * regulator_list_voltage - enumerate supported voltages
2176  * @regulator: regulator source
2177  * @selector: identify voltage to list
2178  * Context: can sleep
2179  *
2180  * Returns a voltage that can be passed to @regulator_set_voltage(),
2181  * zero if this selector code can't be used on this system, or a
2182  * negative errno.
2183  */
2184 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2185 {
2186         struct regulator_dev    *rdev = regulator->rdev;
2187         struct regulator_ops    *ops = rdev->desc->ops;
2188         int                     ret;
2189
2190         if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2191                 return -EINVAL;
2192
2193         mutex_lock(&rdev->mutex);
2194         ret = ops->list_voltage(rdev, selector);
2195         mutex_unlock(&rdev->mutex);
2196
2197         if (ret > 0) {
2198                 if (ret < rdev->constraints->min_uV)
2199                         ret = 0;
2200                 else if (ret > rdev->constraints->max_uV)
2201                         ret = 0;
2202         }
2203
2204         return ret;
2205 }
2206 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2207
2208 /**
2209  * regulator_get_linear_step - return the voltage step size between VSEL values
2210  * @regulator: regulator source
2211  *
2212  * Returns the voltage step size between VSEL values for linear
2213  * regulators, or return 0 if the regulator isn't a linear regulator.
2214  */
2215 unsigned int regulator_get_linear_step(struct regulator *regulator)
2216 {
2217         struct regulator_dev *rdev = regulator->rdev;
2218
2219         return rdev->desc->uV_step;
2220 }
2221 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2222
2223 /**
2224  * regulator_is_supported_voltage - check if a voltage range can be supported
2225  *
2226  * @regulator: Regulator to check.
2227  * @min_uV: Minimum required voltage in uV.
2228  * @max_uV: Maximum required voltage in uV.
2229  *
2230  * Returns a boolean or a negative error code.
2231  */
2232 int regulator_is_supported_voltage(struct regulator *regulator,
2233                                    int min_uV, int max_uV)
2234 {
2235         struct regulator_dev *rdev = regulator->rdev;
2236         int i, voltages, ret;
2237
2238         /* If we can't change voltage check the current voltage */
2239         if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2240                 ret = regulator_get_voltage(regulator);
2241                 if (ret >= 0)
2242                         return (min_uV <= ret && ret <= max_uV);
2243                 else
2244                         return ret;
2245         }
2246
2247         /* Any voltage within constrains range is fine? */
2248         if (rdev->desc->continuous_voltage_range)
2249                 return min_uV >= rdev->constraints->min_uV &&
2250                                 max_uV <= rdev->constraints->max_uV;
2251
2252         ret = regulator_count_voltages(regulator);
2253         if (ret < 0)
2254                 return ret;
2255         voltages = ret;
2256
2257         for (i = 0; i < voltages; i++) {
2258                 ret = regulator_list_voltage(regulator, i);
2259
2260                 if (ret >= min_uV && ret <= max_uV)
2261                         return 1;
2262         }
2263
2264         return 0;
2265 }
2266 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2267
2268 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2269                                      int min_uV, int max_uV)
2270 {
2271         int ret;
2272         int delay = 0;
2273         int best_val = 0;
2274         unsigned int selector;
2275         int old_selector = -1;
2276
2277         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2278
2279         min_uV += rdev->constraints->uV_offset;
2280         max_uV += rdev->constraints->uV_offset;
2281
2282         /*
2283          * If we can't obtain the old selector there is not enough
2284          * info to call set_voltage_time_sel().
2285          */
2286         if (_regulator_is_enabled(rdev) &&
2287             rdev->desc->ops->set_voltage_time_sel &&
2288             rdev->desc->ops->get_voltage_sel) {
2289                 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2290                 if (old_selector < 0)
2291                         return old_selector;
2292         }
2293
2294         if (rdev->desc->ops->set_voltage) {
2295                 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2296                                                    &selector);
2297
2298                 if (ret >= 0) {
2299                         if (rdev->desc->ops->list_voltage)
2300                                 best_val = rdev->desc->ops->list_voltage(rdev,
2301                                                                          selector);
2302                         else
2303                                 best_val = _regulator_get_voltage(rdev);
2304                 }
2305
2306         } else if (rdev->desc->ops->set_voltage_sel) {
2307                 if (rdev->desc->ops->map_voltage) {
2308                         ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2309                                                            max_uV);
2310                 } else {
2311                         if (rdev->desc->ops->list_voltage ==
2312                             regulator_list_voltage_linear)
2313                                 ret = regulator_map_voltage_linear(rdev,
2314                                                                 min_uV, max_uV);
2315                         else
2316                                 ret = regulator_map_voltage_iterate(rdev,
2317                                                                 min_uV, max_uV);
2318                 }
2319
2320                 if (ret >= 0) {
2321                         best_val = rdev->desc->ops->list_voltage(rdev, ret);
2322                         if (min_uV <= best_val && max_uV >= best_val) {
2323                                 selector = ret;
2324                                 if (old_selector == selector)
2325                                         ret = 0;
2326                                 else
2327                                         ret = rdev->desc->ops->set_voltage_sel(
2328                                                                 rdev, ret);
2329                         } else {
2330                                 ret = -EINVAL;
2331                         }
2332                 }
2333         } else {
2334                 ret = -EINVAL;
2335         }
2336
2337         /* Call set_voltage_time_sel if successfully obtained old_selector */
2338         if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2339                 && old_selector != selector) {
2340
2341                 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2342                                                 old_selector, selector);
2343                 if (delay < 0) {
2344                         rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2345                                   delay);
2346                         delay = 0;
2347                 }
2348
2349                 /* Insert any necessary delays */
2350                 if (delay >= 1000) {
2351                         mdelay(delay / 1000);
2352                         udelay(delay % 1000);
2353                 } else if (delay) {
2354                         udelay(delay);
2355                 }
2356         }
2357
2358         if (ret == 0 && best_val >= 0) {
2359                 unsigned long data = best_val;
2360
2361                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2362                                      (void *)data);
2363         }
2364
2365         trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2366
2367         return ret;
2368 }
2369
2370 /**
2371  * regulator_set_voltage - set regulator output voltage
2372  * @regulator: regulator source
2373  * @min_uV: Minimum required voltage in uV
2374  * @max_uV: Maximum acceptable voltage in uV
2375  *
2376  * Sets a voltage regulator to the desired output voltage. This can be set
2377  * during any regulator state. IOW, regulator can be disabled or enabled.
2378  *
2379  * If the regulator is enabled then the voltage will change to the new value
2380  * immediately otherwise if the regulator is disabled the regulator will
2381  * output at the new voltage when enabled.
2382  *
2383  * NOTE: If the regulator is shared between several devices then the lowest
2384  * request voltage that meets the system constraints will be used.
2385  * Regulator system constraints must be set for this regulator before
2386  * calling this function otherwise this call will fail.
2387  */
2388 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2389 {
2390         struct regulator_dev *rdev = regulator->rdev;
2391         int ret = 0;
2392         int old_min_uV, old_max_uV;
2393
2394         mutex_lock(&rdev->mutex);
2395
2396         /* If we're setting the same range as last time the change
2397          * should be a noop (some cpufreq implementations use the same
2398          * voltage for multiple frequencies, for example).
2399          */
2400         if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2401                 goto out;
2402
2403         /* sanity check */
2404         if (!rdev->desc->ops->set_voltage &&
2405             !rdev->desc->ops->set_voltage_sel) {
2406                 ret = -EINVAL;
2407                 goto out;
2408         }
2409
2410         /* constraints check */
2411         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2412         if (ret < 0)
2413                 goto out;
2414         
2415         /* restore original values in case of error */
2416         old_min_uV = regulator->min_uV;
2417         old_max_uV = regulator->max_uV;
2418         regulator->min_uV = min_uV;
2419         regulator->max_uV = max_uV;
2420
2421         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2422         if (ret < 0)
2423                 goto out2;
2424
2425         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2426         if (ret < 0)
2427                 goto out2;
2428         
2429 out:
2430         mutex_unlock(&rdev->mutex);
2431         return ret;
2432 out2:
2433         regulator->min_uV = old_min_uV;
2434         regulator->max_uV = old_max_uV;
2435         mutex_unlock(&rdev->mutex);
2436         return ret;
2437 }
2438 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2439
2440 /**
2441  * regulator_set_voltage_time - get raise/fall time
2442  * @regulator: regulator source
2443  * @old_uV: starting voltage in microvolts
2444  * @new_uV: target voltage in microvolts
2445  *
2446  * Provided with the starting and ending voltage, this function attempts to
2447  * calculate the time in microseconds required to rise or fall to this new
2448  * voltage.
2449  */
2450 int regulator_set_voltage_time(struct regulator *regulator,
2451                                int old_uV, int new_uV)
2452 {
2453         struct regulator_dev    *rdev = regulator->rdev;
2454         struct regulator_ops    *ops = rdev->desc->ops;
2455         int old_sel = -1;
2456         int new_sel = -1;
2457         int voltage;
2458         int i;
2459
2460         /* Currently requires operations to do this */
2461         if (!ops->list_voltage || !ops->set_voltage_time_sel
2462             || !rdev->desc->n_voltages)
2463                 return -EINVAL;
2464
2465         for (i = 0; i < rdev->desc->n_voltages; i++) {
2466                 /* We only look for exact voltage matches here */
2467                 voltage = regulator_list_voltage(regulator, i);
2468                 if (voltage < 0)
2469                         return -EINVAL;
2470                 if (voltage == 0)
2471                         continue;
2472                 if (voltage == old_uV)
2473                         old_sel = i;
2474                 if (voltage == new_uV)
2475                         new_sel = i;
2476         }
2477
2478         if (old_sel < 0 || new_sel < 0)
2479                 return -EINVAL;
2480
2481         return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2482 }
2483 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2484
2485 /**
2486  * regulator_set_voltage_time_sel - get raise/fall time
2487  * @rdev: regulator source device
2488  * @old_selector: selector for starting voltage
2489  * @new_selector: selector for target voltage
2490  *
2491  * Provided with the starting and target voltage selectors, this function
2492  * returns time in microseconds required to rise or fall to this new voltage
2493  *
2494  * Drivers providing ramp_delay in regulation_constraints can use this as their
2495  * set_voltage_time_sel() operation.
2496  */
2497 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2498                                    unsigned int old_selector,
2499                                    unsigned int new_selector)
2500 {
2501         unsigned int ramp_delay = 0;
2502         int old_volt, new_volt;
2503
2504         if (rdev->constraints->ramp_delay)
2505                 ramp_delay = rdev->constraints->ramp_delay;
2506         else if (rdev->desc->ramp_delay)
2507                 ramp_delay = rdev->desc->ramp_delay;
2508
2509         if (ramp_delay == 0) {
2510                 rdev_warn(rdev, "ramp_delay not set\n");
2511                 return 0;
2512         }
2513
2514         /* sanity check */
2515         if (!rdev->desc->ops->list_voltage)
2516                 return -EINVAL;
2517
2518         old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2519         new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2520
2521         return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2522 }
2523 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2524
2525 /**
2526  * regulator_sync_voltage - re-apply last regulator output voltage
2527  * @regulator: regulator source
2528  *
2529  * Re-apply the last configured voltage.  This is intended to be used
2530  * where some external control source the consumer is cooperating with
2531  * has caused the configured voltage to change.
2532  */
2533 int regulator_sync_voltage(struct regulator *regulator)
2534 {
2535         struct regulator_dev *rdev = regulator->rdev;
2536         int ret, min_uV, max_uV;
2537
2538         mutex_lock(&rdev->mutex);
2539
2540         if (!rdev->desc->ops->set_voltage &&
2541             !rdev->desc->ops->set_voltage_sel) {
2542                 ret = -EINVAL;
2543                 goto out;
2544         }
2545
2546         /* This is only going to work if we've had a voltage configured. */
2547         if (!regulator->min_uV && !regulator->max_uV) {
2548                 ret = -EINVAL;
2549                 goto out;
2550         }
2551
2552         min_uV = regulator->min_uV;
2553         max_uV = regulator->max_uV;
2554
2555         /* This should be a paranoia check... */
2556         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2557         if (ret < 0)
2558                 goto out;
2559
2560         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2561         if (ret < 0)
2562                 goto out;
2563
2564         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2565
2566 out:
2567         mutex_unlock(&rdev->mutex);
2568         return ret;
2569 }
2570 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2571
2572 static int _regulator_get_voltage(struct regulator_dev *rdev)
2573 {
2574         int sel, ret;
2575
2576         if (rdev->desc->ops->get_voltage_sel) {
2577                 sel = rdev->desc->ops->get_voltage_sel(rdev);
2578                 if (sel < 0)
2579                         return sel;
2580                 ret = rdev->desc->ops->list_voltage(rdev, sel);
2581         } else if (rdev->desc->ops->get_voltage) {
2582                 ret = rdev->desc->ops->get_voltage(rdev);
2583         } else if (rdev->desc->ops->list_voltage) {
2584                 ret = rdev->desc->ops->list_voltage(rdev, 0);
2585         } else {
2586                 return -EINVAL;
2587         }
2588
2589         if (ret < 0)
2590                 return ret;
2591         return ret - rdev->constraints->uV_offset;
2592 }
2593
2594 /**
2595  * regulator_get_voltage - get regulator output voltage
2596  * @regulator: regulator source
2597  *
2598  * This returns the current regulator voltage in uV.
2599  *
2600  * NOTE: If the regulator is disabled it will return the voltage value. This
2601  * function should not be used to determine regulator state.
2602  */
2603 int regulator_get_voltage(struct regulator *regulator)
2604 {
2605         int ret;
2606
2607         mutex_lock(&regulator->rdev->mutex);
2608
2609         ret = _regulator_get_voltage(regulator->rdev);
2610
2611         mutex_unlock(&regulator->rdev->mutex);
2612
2613         return ret;
2614 }
2615 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2616
2617 /**
2618  * regulator_set_current_limit - set regulator output current limit
2619  * @regulator: regulator source
2620  * @min_uA: Minimum supported current in uA
2621  * @max_uA: Maximum supported current in uA
2622  *
2623  * Sets current sink to the desired output current. This can be set during
2624  * any regulator state. IOW, regulator can be disabled or enabled.
2625  *
2626  * If the regulator is enabled then the current will change to the new value
2627  * immediately otherwise if the regulator is disabled the regulator will
2628  * output at the new current when enabled.
2629  *
2630  * NOTE: Regulator system constraints must be set for this regulator before
2631  * calling this function otherwise this call will fail.
2632  */
2633 int regulator_set_current_limit(struct regulator *regulator,
2634                                int min_uA, int max_uA)
2635 {
2636         struct regulator_dev *rdev = regulator->rdev;
2637         int ret;
2638
2639         mutex_lock(&rdev->mutex);
2640
2641         /* sanity check */
2642         if (!rdev->desc->ops->set_current_limit) {
2643                 ret = -EINVAL;
2644                 goto out;
2645         }
2646
2647         /* constraints check */
2648         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2649         if (ret < 0)
2650                 goto out;
2651
2652         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2653 out:
2654         mutex_unlock(&rdev->mutex);
2655         return ret;
2656 }
2657 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2658
2659 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2660 {
2661         int ret;
2662
2663         mutex_lock(&rdev->mutex);
2664
2665         /* sanity check */
2666         if (!rdev->desc->ops->get_current_limit) {
2667                 ret = -EINVAL;
2668                 goto out;
2669         }
2670
2671         ret = rdev->desc->ops->get_current_limit(rdev);
2672 out:
2673         mutex_unlock(&rdev->mutex);
2674         return ret;
2675 }
2676
2677 /**
2678  * regulator_get_current_limit - get regulator output current
2679  * @regulator: regulator source
2680  *
2681  * This returns the current supplied by the specified current sink in uA.
2682  *
2683  * NOTE: If the regulator is disabled it will return the current value. This
2684  * function should not be used to determine regulator state.
2685  */
2686 int regulator_get_current_limit(struct regulator *regulator)
2687 {
2688         return _regulator_get_current_limit(regulator->rdev);
2689 }
2690 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2691
2692 /**
2693  * regulator_set_mode - set regulator operating mode
2694  * @regulator: regulator source
2695  * @mode: operating mode - one of the REGULATOR_MODE constants
2696  *
2697  * Set regulator operating mode to increase regulator efficiency or improve
2698  * regulation performance.
2699  *
2700  * NOTE: Regulator system constraints must be set for this regulator before
2701  * calling this function otherwise this call will fail.
2702  */
2703 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2704 {
2705         struct regulator_dev *rdev = regulator->rdev;
2706         int ret;
2707         int regulator_curr_mode;
2708
2709         mutex_lock(&rdev->mutex);
2710
2711         /* sanity check */
2712         if (!rdev->desc->ops->set_mode) {
2713                 ret = -EINVAL;
2714                 goto out;
2715         }
2716
2717         /* return if the same mode is requested */
2718         if (rdev->desc->ops->get_mode) {
2719                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2720                 if (regulator_curr_mode == mode) {
2721                         ret = 0;
2722                         goto out;
2723                 }
2724         }
2725
2726         /* constraints check */
2727         ret = regulator_mode_constrain(rdev, &mode);
2728         if (ret < 0)
2729                 goto out;
2730
2731         ret = rdev->desc->ops->set_mode(rdev, mode);
2732 out:
2733         mutex_unlock(&rdev->mutex);
2734         return ret;
2735 }
2736 EXPORT_SYMBOL_GPL(regulator_set_mode);
2737
2738 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2739 {
2740         int ret;
2741
2742         mutex_lock(&rdev->mutex);
2743
2744         /* sanity check */
2745         if (!rdev->desc->ops->get_mode) {
2746                 ret = -EINVAL;
2747                 goto out;
2748         }
2749
2750         ret = rdev->desc->ops->get_mode(rdev);
2751 out:
2752         mutex_unlock(&rdev->mutex);
2753         return ret;
2754 }
2755
2756 /**
2757  * regulator_get_mode - get regulator operating mode
2758  * @regulator: regulator source
2759  *
2760  * Get the current regulator operating mode.
2761  */
2762 unsigned int regulator_get_mode(struct regulator *regulator)
2763 {
2764         return _regulator_get_mode(regulator->rdev);
2765 }
2766 EXPORT_SYMBOL_GPL(regulator_get_mode);
2767
2768 /**
2769  * regulator_set_optimum_mode - set regulator optimum operating mode
2770  * @regulator: regulator source
2771  * @uA_load: load current
2772  *
2773  * Notifies the regulator core of a new device load. This is then used by
2774  * DRMS (if enabled by constraints) to set the most efficient regulator
2775  * operating mode for the new regulator loading.
2776  *
2777  * Consumer devices notify their supply regulator of the maximum power
2778  * they will require (can be taken from device datasheet in the power
2779  * consumption tables) when they change operational status and hence power
2780  * state. Examples of operational state changes that can affect power
2781  * consumption are :-
2782  *
2783  *    o Device is opened / closed.
2784  *    o Device I/O is about to begin or has just finished.
2785  *    o Device is idling in between work.
2786  *
2787  * This information is also exported via sysfs to userspace.
2788  *
2789  * DRMS will sum the total requested load on the regulator and change
2790  * to the most efficient operating mode if platform constraints allow.
2791  *
2792  * Returns the new regulator mode or error.
2793  */
2794 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2795 {
2796         struct regulator_dev *rdev = regulator->rdev;
2797         struct regulator *consumer;
2798         int ret, output_uV, input_uV = 0, total_uA_load = 0;
2799         unsigned int mode;
2800
2801         if (rdev->supply)
2802                 input_uV = regulator_get_voltage(rdev->supply);
2803
2804         mutex_lock(&rdev->mutex);
2805
2806         /*
2807          * first check to see if we can set modes at all, otherwise just
2808          * tell the consumer everything is OK.
2809          */
2810         regulator->uA_load = uA_load;
2811         ret = regulator_check_drms(rdev);
2812         if (ret < 0) {
2813                 ret = 0;
2814                 goto out;
2815         }
2816
2817         if (!rdev->desc->ops->get_optimum_mode)
2818                 goto out;
2819
2820         /*
2821          * we can actually do this so any errors are indicators of
2822          * potential real failure.
2823          */
2824         ret = -EINVAL;
2825
2826         if (!rdev->desc->ops->set_mode)
2827                 goto out;
2828
2829         /* get output voltage */
2830         output_uV = _regulator_get_voltage(rdev);
2831         if (output_uV <= 0) {
2832                 rdev_err(rdev, "invalid output voltage found\n");
2833                 goto out;
2834         }
2835
2836         /* No supply? Use constraint voltage */
2837         if (input_uV <= 0)
2838                 input_uV = rdev->constraints->input_uV;
2839         if (input_uV <= 0) {
2840                 rdev_err(rdev, "invalid input voltage found\n");
2841                 goto out;
2842         }
2843
2844         /* calc total requested load for this regulator */
2845         list_for_each_entry(consumer, &rdev->consumer_list, list)
2846                 total_uA_load += consumer->uA_load;
2847
2848         mode = rdev->desc->ops->get_optimum_mode(rdev,
2849                                                  input_uV, output_uV,
2850                                                  total_uA_load);
2851         ret = regulator_mode_constrain(rdev, &mode);
2852         if (ret < 0) {
2853                 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2854                          total_uA_load, input_uV, output_uV);
2855                 goto out;
2856         }
2857
2858         ret = rdev->desc->ops->set_mode(rdev, mode);
2859         if (ret < 0) {
2860                 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2861                 goto out;
2862         }
2863         ret = mode;
2864 out:
2865         mutex_unlock(&rdev->mutex);
2866         return ret;
2867 }
2868 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2869
2870 /**
2871  * regulator_allow_bypass - allow the regulator to go into bypass mode
2872  *
2873  * @regulator: Regulator to configure
2874  * @enable: enable or disable bypass mode
2875  *
2876  * Allow the regulator to go into bypass mode if all other consumers
2877  * for the regulator also enable bypass mode and the machine
2878  * constraints allow this.  Bypass mode means that the regulator is
2879  * simply passing the input directly to the output with no regulation.
2880  */
2881 int regulator_allow_bypass(struct regulator *regulator, bool enable)
2882 {
2883         struct regulator_dev *rdev = regulator->rdev;
2884         int ret = 0;
2885
2886         if (!rdev->desc->ops->set_bypass)
2887                 return 0;
2888
2889         if (rdev->constraints &&
2890             !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2891                 return 0;
2892
2893         mutex_lock(&rdev->mutex);
2894
2895         if (enable && !regulator->bypass) {
2896                 rdev->bypass_count++;
2897
2898                 if (rdev->bypass_count == rdev->open_count) {
2899                         ret = rdev->desc->ops->set_bypass(rdev, enable);
2900                         if (ret != 0)
2901                                 rdev->bypass_count--;
2902                 }
2903
2904         } else if (!enable && regulator->bypass) {
2905                 rdev->bypass_count--;
2906
2907                 if (rdev->bypass_count != rdev->open_count) {
2908                         ret = rdev->desc->ops->set_bypass(rdev, enable);
2909                         if (ret != 0)
2910                                 rdev->bypass_count++;
2911                 }
2912         }
2913
2914         if (ret == 0)
2915                 regulator->bypass = enable;
2916
2917         mutex_unlock(&rdev->mutex);
2918
2919         return ret;
2920 }
2921 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2922
2923 /**
2924  * regulator_register_notifier - register regulator event notifier
2925  * @regulator: regulator source
2926  * @nb: notifier block
2927  *
2928  * Register notifier block to receive regulator events.
2929  */
2930 int regulator_register_notifier(struct regulator *regulator,
2931                               struct notifier_block *nb)
2932 {
2933         return blocking_notifier_chain_register(&regulator->rdev->notifier,
2934                                                 nb);
2935 }
2936 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2937
2938 /**
2939  * regulator_unregister_notifier - unregister regulator event notifier
2940  * @regulator: regulator source
2941  * @nb: notifier block
2942  *
2943  * Unregister regulator event notifier block.
2944  */
2945 int regulator_unregister_notifier(struct regulator *regulator,
2946                                 struct notifier_block *nb)
2947 {
2948         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2949                                                   nb);
2950 }
2951 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2952
2953 /* notify regulator consumers and downstream regulator consumers.
2954  * Note mutex must be held by caller.
2955  */
2956 static void _notifier_call_chain(struct regulator_dev *rdev,
2957                                   unsigned long event, void *data)
2958 {
2959         /* call rdev chain first */
2960         blocking_notifier_call_chain(&rdev->notifier, event, data);
2961 }
2962
2963 /**
2964  * regulator_bulk_get - get multiple regulator consumers
2965  *
2966  * @dev:           Device to supply
2967  * @num_consumers: Number of consumers to register
2968  * @consumers:     Configuration of consumers; clients are stored here.
2969  *
2970  * @return 0 on success, an errno on failure.
2971  *
2972  * This helper function allows drivers to get several regulator
2973  * consumers in one operation.  If any of the regulators cannot be
2974  * acquired then any regulators that were allocated will be freed
2975  * before returning to the caller.
2976  */
2977 int regulator_bulk_get(struct device *dev, int num_consumers,
2978                        struct regulator_bulk_data *consumers)
2979 {
2980         int i;
2981         int ret;
2982
2983         for (i = 0; i < num_consumers; i++)
2984                 consumers[i].consumer = NULL;
2985
2986         for (i = 0; i < num_consumers; i++) {
2987                 consumers[i].consumer = regulator_get(dev,
2988                                                       consumers[i].supply);
2989                 if (IS_ERR(consumers[i].consumer)) {
2990                         ret = PTR_ERR(consumers[i].consumer);
2991                         dev_err(dev, "Failed to get supply '%s': %d\n",
2992                                 consumers[i].supply, ret);
2993                         consumers[i].consumer = NULL;
2994                         goto err;
2995                 }
2996         }
2997
2998         return 0;
2999
3000 err:
3001         while (--i >= 0)
3002                 regulator_put(consumers[i].consumer);
3003
3004         return ret;
3005 }
3006 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3007
3008 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3009 {
3010         struct regulator_bulk_data *bulk = data;
3011
3012         bulk->ret = regulator_enable(bulk->consumer);
3013 }
3014
3015 /**
3016  * regulator_bulk_enable - enable multiple regulator consumers
3017  *
3018  * @num_consumers: Number of consumers
3019  * @consumers:     Consumer data; clients are stored here.
3020  * @return         0 on success, an errno on failure
3021  *
3022  * This convenience API allows consumers to enable multiple regulator
3023  * clients in a single API call.  If any consumers cannot be enabled
3024  * then any others that were enabled will be disabled again prior to
3025  * return.
3026  */
3027 int regulator_bulk_enable(int num_consumers,
3028                           struct regulator_bulk_data *consumers)
3029 {
3030         ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3031         int i;
3032         int ret = 0;
3033
3034         for (i = 0; i < num_consumers; i++) {
3035                 if (consumers[i].consumer->always_on)
3036                         consumers[i].ret = 0;
3037                 else
3038                         async_schedule_domain(regulator_bulk_enable_async,
3039                                               &consumers[i], &async_domain);
3040         }
3041
3042         async_synchronize_full_domain(&async_domain);
3043
3044         /* If any consumer failed we need to unwind any that succeeded */
3045         for (i = 0; i < num_consumers; i++) {
3046                 if (consumers[i].ret != 0) {
3047                         ret = consumers[i].ret;
3048                         goto err;
3049                 }
3050         }
3051
3052         return 0;
3053
3054 err:
3055         for (i = 0; i < num_consumers; i++) {
3056                 if (consumers[i].ret < 0)
3057                         pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3058                                consumers[i].ret);
3059                 else
3060                         regulator_disable(consumers[i].consumer);
3061         }
3062
3063         return ret;
3064 }
3065 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3066
3067 /**
3068  * regulator_bulk_disable - disable multiple regulator consumers
3069  *
3070  * @num_consumers: Number of consumers
3071  * @consumers:     Consumer data; clients are stored here.
3072  * @return         0 on success, an errno on failure
3073  *
3074  * This convenience API allows consumers to disable multiple regulator
3075  * clients in a single API call.  If any consumers cannot be disabled
3076  * then any others that were disabled will be enabled again prior to
3077  * return.
3078  */
3079 int regulator_bulk_disable(int num_consumers,
3080                            struct regulator_bulk_data *consumers)
3081 {
3082         int i;
3083         int ret, r;
3084
3085         for (i = num_consumers - 1; i >= 0; --i) {
3086                 ret = regulator_disable(consumers[i].consumer);
3087                 if (ret != 0)
3088                         goto err;
3089         }
3090
3091         return 0;
3092
3093 err:
3094         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3095         for (++i; i < num_consumers; ++i) {
3096                 r = regulator_enable(consumers[i].consumer);
3097                 if (r != 0)
3098                         pr_err("Failed to reename %s: %d\n",
3099                                consumers[i].supply, r);
3100         }
3101
3102         return ret;
3103 }
3104 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3105
3106 /**
3107  * regulator_bulk_force_disable - force disable multiple regulator consumers
3108  *
3109  * @num_consumers: Number of consumers
3110  * @consumers:     Consumer data; clients are stored here.
3111  * @return         0 on success, an errno on failure
3112  *
3113  * This convenience API allows consumers to forcibly disable multiple regulator
3114  * clients in a single API call.
3115  * NOTE: This should be used for situations when device damage will
3116  * likely occur if the regulators are not disabled (e.g. over temp).
3117  * Although regulator_force_disable function call for some consumers can
3118  * return error numbers, the function is called for all consumers.
3119  */
3120 int regulator_bulk_force_disable(int num_consumers,
3121                            struct regulator_bulk_data *consumers)
3122 {
3123         int i;
3124         int ret;
3125
3126         for (i = 0; i < num_consumers; i++)
3127                 consumers[i].ret =
3128                             regulator_force_disable(consumers[i].consumer);
3129
3130         for (i = 0; i < num_consumers; i++) {
3131                 if (consumers[i].ret != 0) {
3132                         ret = consumers[i].ret;
3133                         goto out;
3134                 }
3135         }
3136
3137         return 0;
3138 out:
3139         return ret;
3140 }
3141 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3142
3143 /**
3144  * regulator_bulk_free - free multiple regulator consumers
3145  *
3146  * @num_consumers: Number of consumers
3147  * @consumers:     Consumer data; clients are stored here.
3148  *
3149  * This convenience API allows consumers to free multiple regulator
3150  * clients in a single API call.
3151  */
3152 void regulator_bulk_free(int num_consumers,
3153                          struct regulator_bulk_data *consumers)
3154 {
3155         int i;
3156
3157         for (i = 0; i < num_consumers; i++) {
3158                 regulator_put(consumers[i].consumer);
3159                 consumers[i].consumer = NULL;
3160         }
3161 }
3162 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3163
3164 /**
3165  * regulator_notifier_call_chain - call regulator event notifier
3166  * @rdev: regulator source
3167  * @event: notifier block
3168  * @data: callback-specific data.
3169  *
3170  * Called by regulator drivers to notify clients a regulator event has
3171  * occurred. We also notify regulator clients downstream.
3172  * Note lock must be held by caller.
3173  */
3174 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3175                                   unsigned long event, void *data)
3176 {
3177         _notifier_call_chain(rdev, event, data);
3178         return NOTIFY_DONE;
3179
3180 }
3181 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3182
3183 /**
3184  * regulator_mode_to_status - convert a regulator mode into a status
3185  *
3186  * @mode: Mode to convert
3187  *
3188  * Convert a regulator mode into a status.
3189  */
3190 int regulator_mode_to_status(unsigned int mode)
3191 {
3192         switch (mode) {
3193         case REGULATOR_MODE_FAST:
3194                 return REGULATOR_STATUS_FAST;
3195         case REGULATOR_MODE_NORMAL:
3196                 return REGULATOR_STATUS_NORMAL;
3197         case REGULATOR_MODE_IDLE:
3198                 return REGULATOR_STATUS_IDLE;
3199         case REGULATOR_MODE_STANDBY:
3200                 return REGULATOR_STATUS_STANDBY;
3201         default:
3202                 return REGULATOR_STATUS_UNDEFINED;
3203         }
3204 }
3205 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3206
3207 /*
3208  * To avoid cluttering sysfs (and memory) with useless state, only
3209  * create attributes that can be meaningfully displayed.
3210  */
3211 static int add_regulator_attributes(struct regulator_dev *rdev)
3212 {
3213         struct device           *dev = &rdev->dev;
3214         struct regulator_ops    *ops = rdev->desc->ops;
3215         int                     status = 0;
3216
3217         /* some attributes need specific methods to be displayed */
3218         if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3219             (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3220             (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3221                 status = device_create_file(dev, &dev_attr_microvolts);
3222                 if (status < 0)
3223                         return status;
3224         }
3225         if (ops->get_current_limit) {
3226                 status = device_create_file(dev, &dev_attr_microamps);
3227                 if (status < 0)
3228                         return status;
3229         }
3230         if (ops->get_mode) {
3231                 status = device_create_file(dev, &dev_attr_opmode);
3232                 if (status < 0)
3233                         return status;
3234         }
3235         if (rdev->ena_pin || ops->is_enabled) {
3236                 status = device_create_file(dev, &dev_attr_state);
3237                 if (status < 0)
3238                         return status;
3239         }
3240         if (ops->get_status) {
3241                 status = device_create_file(dev, &dev_attr_status);
3242                 if (status < 0)
3243                         return status;
3244         }
3245         if (ops->get_bypass) {
3246                 status = device_create_file(dev, &dev_attr_bypass);
3247                 if (status < 0)
3248                         return status;
3249         }
3250
3251         /* some attributes are type-specific */
3252         if (rdev->desc->type == REGULATOR_CURRENT) {
3253                 status = device_create_file(dev, &dev_attr_requested_microamps);
3254                 if (status < 0)
3255                         return status;
3256         }
3257
3258         /* all the other attributes exist to support constraints;
3259          * don't show them if there are no constraints, or if the
3260          * relevant supporting methods are missing.
3261          */
3262         if (!rdev->constraints)
3263                 return status;
3264
3265         /* constraints need specific supporting methods */
3266         if (ops->set_voltage || ops->set_voltage_sel) {
3267                 status = device_create_file(dev, &dev_attr_min_microvolts);
3268                 if (status < 0)
3269                         return status;
3270                 status = device_create_file(dev, &dev_attr_max_microvolts);
3271                 if (status < 0)
3272                         return status;
3273         }
3274         if (ops->set_current_limit) {
3275                 status = device_create_file(dev, &dev_attr_min_microamps);
3276                 if (status < 0)
3277                         return status;
3278                 status = device_create_file(dev, &dev_attr_max_microamps);
3279                 if (status < 0)
3280                         return status;
3281         }
3282
3283         status = device_create_file(dev, &dev_attr_suspend_standby_state);
3284         if (status < 0)
3285                 return status;
3286         status = device_create_file(dev, &dev_attr_suspend_mem_state);
3287         if (status < 0)
3288                 return status;
3289         status = device_create_file(dev, &dev_attr_suspend_disk_state);
3290         if (status < 0)
3291                 return status;
3292
3293         if (ops->set_suspend_voltage) {
3294                 status = device_create_file(dev,
3295                                 &dev_attr_suspend_standby_microvolts);
3296                 if (status < 0)
3297                         return status;
3298                 status = device_create_file(dev,
3299                                 &dev_attr_suspend_mem_microvolts);
3300                 if (status < 0)
3301                         return status;
3302                 status = device_create_file(dev,
3303                                 &dev_attr_suspend_disk_microvolts);
3304                 if (status < 0)
3305                         return status;
3306         }
3307
3308         if (ops->set_suspend_mode) {
3309                 status = device_create_file(dev,
3310                                 &dev_attr_suspend_standby_mode);
3311                 if (status < 0)
3312                         return status;
3313                 status = device_create_file(dev,
3314                                 &dev_attr_suspend_mem_mode);
3315                 if (status < 0)
3316                         return status;
3317                 status = device_create_file(dev,
3318                                 &dev_attr_suspend_disk_mode);
3319                 if (status < 0)
3320                         return status;
3321         }
3322
3323         return status;
3324 }
3325
3326 static void rdev_init_debugfs(struct regulator_dev *rdev)
3327 {
3328         rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3329         if (!rdev->debugfs) {
3330                 rdev_warn(rdev, "Failed to create debugfs directory\n");
3331                 return;
3332         }
3333
3334         debugfs_create_u32("use_count", 0444, rdev->debugfs,
3335                            &rdev->use_count);
3336         debugfs_create_u32("open_count", 0444, rdev->debugfs,
3337                            &rdev->open_count);
3338         debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3339                            &rdev->bypass_count);
3340 }
3341
3342 /**
3343  * regulator_register - register regulator
3344  * @regulator_desc: regulator to register
3345  * @config: runtime configuration for regulator
3346  *
3347  * Called by regulator drivers to register a regulator.
3348  * Returns a valid pointer to struct regulator_dev on success
3349  * or an ERR_PTR() on error.
3350  */
3351 struct regulator_dev *
3352 regulator_register(const struct regulator_desc *regulator_desc,
3353                    const struct regulator_config *config)
3354 {
3355         const struct regulation_constraints *constraints = NULL;
3356         const struct regulator_init_data *init_data;
3357         static atomic_t regulator_no = ATOMIC_INIT(0);
3358         struct regulator_dev *rdev;
3359         struct device *dev;
3360         int ret, i;
3361         const char *supply = NULL;
3362
3363         if (regulator_desc == NULL || config == NULL)
3364                 return ERR_PTR(-EINVAL);
3365
3366         dev = config->dev;
3367         WARN_ON(!dev);
3368
3369         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3370                 return ERR_PTR(-EINVAL);
3371
3372         if (regulator_desc->type != REGULATOR_VOLTAGE &&
3373             regulator_desc->type != REGULATOR_CURRENT)
3374                 return ERR_PTR(-EINVAL);
3375
3376         /* Only one of each should be implemented */
3377         WARN_ON(regulator_desc->ops->get_voltage &&
3378                 regulator_desc->ops->get_voltage_sel);
3379         WARN_ON(regulator_desc->ops->set_voltage &&
3380                 regulator_desc->ops->set_voltage_sel);
3381
3382         /* If we're using selectors we must implement list_voltage. */
3383         if (regulator_desc->ops->get_voltage_sel &&
3384             !regulator_desc->ops->list_voltage) {
3385                 return ERR_PTR(-EINVAL);
3386         }
3387         if (regulator_desc->ops->set_voltage_sel &&
3388             !regulator_desc->ops->list_voltage) {
3389                 return ERR_PTR(-EINVAL);
3390         }
3391
3392         init_data = config->init_data;
3393
3394         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3395         if (rdev == NULL)
3396                 return ERR_PTR(-ENOMEM);
3397
3398         mutex_lock(&regulator_list_mutex);
3399
3400         mutex_init(&rdev->mutex);
3401         rdev->reg_data = config->driver_data;
3402         rdev->owner = regulator_desc->owner;
3403         rdev->desc = regulator_desc;
3404         if (config->regmap)
3405                 rdev->regmap = config->regmap;
3406         else if (dev_get_regmap(dev, NULL))
3407                 rdev->regmap = dev_get_regmap(dev, NULL);
3408         else if (dev->parent)
3409                 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3410         INIT_LIST_HEAD(&rdev->consumer_list);
3411         INIT_LIST_HEAD(&rdev->list);
3412         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3413         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3414
3415         /* preform any regulator specific init */
3416         if (init_data && init_data->regulator_init) {
3417                 ret = init_data->regulator_init(rdev->reg_data);
3418                 if (ret < 0)
3419                         goto clean;
3420         }
3421
3422         /* register with sysfs */
3423         rdev->dev.class = &regulator_class;
3424         rdev->dev.of_node = config->of_node;
3425         rdev->dev.parent = dev;
3426         dev_set_name(&rdev->dev, "regulator.%d",
3427                      atomic_inc_return(&regulator_no) - 1);
3428         ret = device_register(&rdev->dev);
3429         if (ret != 0) {
3430                 put_device(&rdev->dev);
3431                 goto clean;
3432         }
3433
3434         dev_set_drvdata(&rdev->dev, rdev);
3435
3436         if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3437                 ret = regulator_ena_gpio_request(rdev, config);
3438                 if (ret != 0) {
3439                         rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3440                                  config->ena_gpio, ret);
3441                         goto wash;
3442                 }
3443
3444                 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3445                         rdev->ena_gpio_state = 1;
3446
3447                 if (config->ena_gpio_invert)
3448                         rdev->ena_gpio_state = !rdev->ena_gpio_state;
3449         }
3450
3451         /* set regulator constraints */
3452         if (init_data)
3453                 constraints = &init_data->constraints;
3454
3455         ret = set_machine_constraints(rdev, constraints);
3456         if (ret < 0)
3457                 goto scrub;
3458
3459         /* add attributes supported by this regulator */
3460         ret = add_regulator_attributes(rdev);
3461         if (ret < 0)
3462                 goto scrub;
3463
3464         if (init_data && init_data->supply_regulator)
3465                 supply = init_data->supply_regulator;
3466         else if (regulator_desc->supply_name)
3467                 supply = regulator_desc->supply_name;
3468
3469         if (supply) {
3470                 struct regulator_dev *r;
3471
3472                 r = regulator_dev_lookup(dev, supply, &ret);
3473
3474                 if (ret == -ENODEV) {
3475                         /*
3476                          * No supply was specified for this regulator and
3477                          * there will never be one.
3478                          */
3479                         ret = 0;
3480                         goto add_dev;
3481                 } else if (!r) {
3482                         dev_err(dev, "Failed to find supply %s\n", supply);
3483                         ret = -EPROBE_DEFER;
3484                         goto scrub;
3485                 }
3486
3487                 ret = set_supply(rdev, r);
3488                 if (ret < 0)
3489                         goto scrub;
3490
3491                 /* Enable supply if rail is enabled */
3492                 if (_regulator_is_enabled(rdev)) {
3493                         ret = regulator_enable(rdev->supply);
3494                         if (ret < 0)
3495                                 goto scrub;
3496                 }
3497         }
3498
3499 add_dev:
3500         /* add consumers devices */
3501         if (init_data) {
3502                 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3503                         ret = set_consumer_device_supply(rdev,
3504                                 init_data->consumer_supplies[i].dev_name,
3505                                 init_data->consumer_supplies[i].supply);
3506                         if (ret < 0) {
3507                                 dev_err(dev, "Failed to set supply %s\n",
3508                                         init_data->consumer_supplies[i].supply);
3509                                 goto unset_supplies;
3510                         }
3511                 }
3512         }
3513
3514         list_add(&rdev->list, &regulator_list);
3515
3516         rdev_init_debugfs(rdev);
3517 out:
3518         mutex_unlock(&regulator_list_mutex);
3519         return rdev;
3520
3521 unset_supplies:
3522         unset_regulator_supplies(rdev);
3523
3524 scrub:
3525         if (rdev->supply)
3526                 _regulator_put(rdev->supply);
3527         regulator_ena_gpio_free(rdev);
3528         kfree(rdev->constraints);
3529 wash:
3530         device_unregister(&rdev->dev);
3531         /* device core frees rdev */
3532         rdev = ERR_PTR(ret);
3533         goto out;
3534
3535 clean:
3536         kfree(rdev);
3537         rdev = ERR_PTR(ret);
3538         goto out;
3539 }
3540 EXPORT_SYMBOL_GPL(regulator_register);
3541
3542 /**
3543  * regulator_unregister - unregister regulator
3544  * @rdev: regulator to unregister
3545  *
3546  * Called by regulator drivers to unregister a regulator.
3547  */
3548 void regulator_unregister(struct regulator_dev *rdev)
3549 {
3550         if (rdev == NULL)
3551                 return;
3552
3553         if (rdev->supply) {
3554                 while (rdev->use_count--)
3555                         regulator_disable(rdev->supply);
3556                 regulator_put(rdev->supply);
3557         }
3558         mutex_lock(&regulator_list_mutex);
3559         debugfs_remove_recursive(rdev->debugfs);
3560         flush_work(&rdev->disable_work.work);
3561         WARN_ON(rdev->open_count);
3562         unset_regulator_supplies(rdev);
3563         list_del(&rdev->list);
3564         kfree(rdev->constraints);
3565         regulator_ena_gpio_free(rdev);
3566         device_unregister(&rdev->dev);
3567         mutex_unlock(&regulator_list_mutex);
3568 }
3569 EXPORT_SYMBOL_GPL(regulator_unregister);
3570
3571 /**
3572  * regulator_suspend_prepare - prepare regulators for system wide suspend
3573  * @state: system suspend state
3574  *
3575  * Configure each regulator with it's suspend operating parameters for state.
3576  * This will usually be called by machine suspend code prior to supending.
3577  */
3578 int regulator_suspend_prepare(suspend_state_t state)
3579 {
3580         struct regulator_dev *rdev;
3581         int ret = 0;
3582
3583         /* ON is handled by regulator active state */
3584         if (state == PM_SUSPEND_ON)
3585                 return -EINVAL;
3586
3587         mutex_lock(&regulator_list_mutex);
3588         list_for_each_entry(rdev, &regulator_list, list) {
3589
3590                 mutex_lock(&rdev->mutex);
3591                 ret = suspend_prepare(rdev, state);
3592                 mutex_unlock(&rdev->mutex);
3593
3594                 if (ret < 0) {
3595                         rdev_err(rdev, "failed to prepare\n");
3596                         goto out;
3597                 }
3598         }
3599 out:
3600         mutex_unlock(&regulator_list_mutex);
3601         return ret;
3602 }
3603 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3604
3605 /**
3606  * regulator_suspend_finish - resume regulators from system wide suspend
3607  *
3608  * Turn on regulators that might be turned off by regulator_suspend_prepare
3609  * and that should be turned on according to the regulators properties.
3610  */
3611 int regulator_suspend_finish(void)
3612 {
3613         struct regulator_dev *rdev;
3614         int ret = 0, error;
3615
3616         mutex_lock(&regulator_list_mutex);
3617         list_for_each_entry(rdev, &regulator_list, list) {
3618                 struct regulator_ops *ops = rdev->desc->ops;
3619
3620                 mutex_lock(&rdev->mutex);
3621                 if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
3622                                 ops->enable) {
3623                         error = ops->enable(rdev);
3624                         if (error)
3625                                 ret = error;
3626                 } else {
3627                         if (!has_full_constraints)
3628                                 goto unlock;
3629                         if (!ops->disable)
3630                                 goto unlock;
3631                         if (!_regulator_is_enabled(rdev))
3632                                 goto unlock;
3633
3634                         error = ops->disable(rdev);
3635                         if (error)
3636                                 ret = error;
3637                 }
3638 unlock:
3639                 mutex_unlock(&rdev->mutex);
3640         }
3641         mutex_unlock(&regulator_list_mutex);
3642         return ret;
3643 }
3644 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3645
3646 /**
3647  * regulator_has_full_constraints - the system has fully specified constraints
3648  *
3649  * Calling this function will cause the regulator API to disable all
3650  * regulators which have a zero use count and don't have an always_on
3651  * constraint in a late_initcall.
3652  *
3653  * The intention is that this will become the default behaviour in a
3654  * future kernel release so users are encouraged to use this facility
3655  * now.
3656  */
3657 void regulator_has_full_constraints(void)
3658 {
3659         has_full_constraints = 1;
3660 }
3661 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3662
3663 /**
3664  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3665  *
3666  * Calling this function will cause the regulator API to provide a
3667  * dummy regulator to consumers if no physical regulator is found,
3668  * allowing most consumers to proceed as though a regulator were
3669  * configured.  This allows systems such as those with software
3670  * controllable regulators for the CPU core only to be brought up more
3671  * readily.
3672  */
3673 void regulator_use_dummy_regulator(void)
3674 {
3675         board_wants_dummy_regulator = true;
3676 }
3677 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3678
3679 /**
3680  * rdev_get_drvdata - get rdev regulator driver data
3681  * @rdev: regulator
3682  *
3683  * Get rdev regulator driver private data. This call can be used in the
3684  * regulator driver context.
3685  */
3686 void *rdev_get_drvdata(struct regulator_dev *rdev)
3687 {
3688         return rdev->reg_data;
3689 }
3690 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3691
3692 /**
3693  * regulator_get_drvdata - get regulator driver data
3694  * @regulator: regulator
3695  *
3696  * Get regulator driver private data. This call can be used in the consumer
3697  * driver context when non API regulator specific functions need to be called.
3698  */
3699 void *regulator_get_drvdata(struct regulator *regulator)
3700 {
3701         return regulator->rdev->reg_data;
3702 }
3703 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3704
3705 /**
3706  * regulator_set_drvdata - set regulator driver data
3707  * @regulator: regulator
3708  * @data: data
3709  */
3710 void regulator_set_drvdata(struct regulator *regulator, void *data)
3711 {
3712         regulator->rdev->reg_data = data;
3713 }
3714 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3715
3716 /**
3717  * regulator_get_id - get regulator ID
3718  * @rdev: regulator
3719  */
3720 int rdev_get_id(struct regulator_dev *rdev)
3721 {
3722         return rdev->desc->id;
3723 }
3724 EXPORT_SYMBOL_GPL(rdev_get_id);
3725
3726 struct device *rdev_get_dev(struct regulator_dev *rdev)
3727 {
3728         return &rdev->dev;
3729 }
3730 EXPORT_SYMBOL_GPL(rdev_get_dev);
3731
3732 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3733 {
3734         return reg_init_data->driver_data;
3735 }
3736 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3737
3738 #ifdef CONFIG_DEBUG_FS
3739 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3740                                     size_t count, loff_t *ppos)
3741 {
3742         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3743         ssize_t len, ret = 0;
3744         struct regulator_map *map;
3745
3746         if (!buf)
3747                 return -ENOMEM;
3748
3749         list_for_each_entry(map, &regulator_map_list, list) {
3750                 len = snprintf(buf + ret, PAGE_SIZE - ret,
3751                                "%s -> %s.%s\n",
3752                                rdev_get_name(map->regulator), map->dev_name,
3753                                map->supply);
3754                 if (len >= 0)
3755                         ret += len;
3756                 if (ret > PAGE_SIZE) {
3757                         ret = PAGE_SIZE;
3758                         break;
3759                 }
3760         }
3761
3762         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3763
3764         kfree(buf);
3765
3766         return ret;
3767 }
3768 #endif
3769
3770 static const struct file_operations supply_map_fops = {
3771 #ifdef CONFIG_DEBUG_FS
3772         .read = supply_map_read_file,
3773         .llseek = default_llseek,
3774 #endif
3775 };
3776
3777 static int __init regulator_init(void)
3778 {
3779         int ret;
3780
3781         ret = class_register(&regulator_class);
3782
3783         debugfs_root = debugfs_create_dir("regulator", NULL);
3784         if (!debugfs_root)
3785                 pr_warn("regulator: Failed to create debugfs directory\n");
3786
3787         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3788                             &supply_map_fops);
3789
3790         regulator_dummy_init();
3791
3792         return ret;
3793 }
3794
3795 /* init early to allow our consumers to complete system booting */
3796 core_initcall(regulator_init);
3797
3798 static int __init regulator_init_complete(void)
3799 {
3800         struct regulator_dev *rdev;
3801         struct regulator_ops *ops;
3802         struct regulation_constraints *c;
3803         int enabled, ret;
3804
3805         /*
3806          * Since DT doesn't provide an idiomatic mechanism for
3807          * enabling full constraints and since it's much more natural
3808          * with DT to provide them just assume that a DT enabled
3809          * system has full constraints.
3810          */
3811         if (of_have_populated_dt())
3812                 has_full_constraints = true;
3813
3814         mutex_lock(&regulator_list_mutex);
3815
3816         /* If we have a full configuration then disable any regulators
3817          * which are not in use or always_on.  This will become the
3818          * default behaviour in the future.
3819          */
3820         list_for_each_entry(rdev, &regulator_list, list) {
3821                 ops = rdev->desc->ops;
3822                 c = rdev->constraints;
3823
3824                 if (!ops->disable || (c && c->always_on))
3825                         continue;
3826
3827                 mutex_lock(&rdev->mutex);
3828
3829                 if (rdev->use_count)
3830                         goto unlock;
3831
3832                 /* If we can't read the status assume it's on. */
3833                 if (ops->is_enabled)
3834                         enabled = ops->is_enabled(rdev);
3835                 else
3836                         enabled = 1;
3837
3838                 if (!enabled)
3839                         goto unlock;
3840
3841                 if (has_full_constraints) {
3842                         /* We log since this may kill the system if it
3843                          * goes wrong. */
3844                         rdev_info(rdev, "disabling\n");
3845                         ret = ops->disable(rdev);
3846                         if (ret != 0) {
3847                                 rdev_err(rdev, "couldn't disable: %d\n", ret);
3848                         }
3849                 } else {
3850                         /* The intention is that in future we will
3851                          * assume that full constraints are provided
3852                          * so warn even if we aren't going to do
3853                          * anything here.
3854                          */
3855                         rdev_warn(rdev, "incomplete constraints, leaving on\n");
3856                 }
3857
3858 unlock:
3859                 mutex_unlock(&rdev->mutex);
3860         }
3861
3862         mutex_unlock(&regulator_list_mutex);
3863
3864         return 0;
3865 }
3866 late_initcall(regulator_init_complete);