]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/regulator/core.c
Merge remote-tracking branch 'regulator/topic/fixed' 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 if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2586                 ret = rdev->desc->fixed_uV;
2587         } else {
2588                 return -EINVAL;
2589         }
2590
2591         if (ret < 0)
2592                 return ret;
2593         return ret - rdev->constraints->uV_offset;
2594 }
2595
2596 /**
2597  * regulator_get_voltage - get regulator output voltage
2598  * @regulator: regulator source
2599  *
2600  * This returns the current regulator voltage in uV.
2601  *
2602  * NOTE: If the regulator is disabled it will return the voltage value. This
2603  * function should not be used to determine regulator state.
2604  */
2605 int regulator_get_voltage(struct regulator *regulator)
2606 {
2607         int ret;
2608
2609         mutex_lock(&regulator->rdev->mutex);
2610
2611         ret = _regulator_get_voltage(regulator->rdev);
2612
2613         mutex_unlock(&regulator->rdev->mutex);
2614
2615         return ret;
2616 }
2617 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2618
2619 /**
2620  * regulator_set_current_limit - set regulator output current limit
2621  * @regulator: regulator source
2622  * @min_uA: Minimum supported current in uA
2623  * @max_uA: Maximum supported current in uA
2624  *
2625  * Sets current sink to the desired output current. This can be set during
2626  * any regulator state. IOW, regulator can be disabled or enabled.
2627  *
2628  * If the regulator is enabled then the current will change to the new value
2629  * immediately otherwise if the regulator is disabled the regulator will
2630  * output at the new current when enabled.
2631  *
2632  * NOTE: Regulator system constraints must be set for this regulator before
2633  * calling this function otherwise this call will fail.
2634  */
2635 int regulator_set_current_limit(struct regulator *regulator,
2636                                int min_uA, int max_uA)
2637 {
2638         struct regulator_dev *rdev = regulator->rdev;
2639         int ret;
2640
2641         mutex_lock(&rdev->mutex);
2642
2643         /* sanity check */
2644         if (!rdev->desc->ops->set_current_limit) {
2645                 ret = -EINVAL;
2646                 goto out;
2647         }
2648
2649         /* constraints check */
2650         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2651         if (ret < 0)
2652                 goto out;
2653
2654         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2655 out:
2656         mutex_unlock(&rdev->mutex);
2657         return ret;
2658 }
2659 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2660
2661 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2662 {
2663         int ret;
2664
2665         mutex_lock(&rdev->mutex);
2666
2667         /* sanity check */
2668         if (!rdev->desc->ops->get_current_limit) {
2669                 ret = -EINVAL;
2670                 goto out;
2671         }
2672
2673         ret = rdev->desc->ops->get_current_limit(rdev);
2674 out:
2675         mutex_unlock(&rdev->mutex);
2676         return ret;
2677 }
2678
2679 /**
2680  * regulator_get_current_limit - get regulator output current
2681  * @regulator: regulator source
2682  *
2683  * This returns the current supplied by the specified current sink in uA.
2684  *
2685  * NOTE: If the regulator is disabled it will return the current value. This
2686  * function should not be used to determine regulator state.
2687  */
2688 int regulator_get_current_limit(struct regulator *regulator)
2689 {
2690         return _regulator_get_current_limit(regulator->rdev);
2691 }
2692 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2693
2694 /**
2695  * regulator_set_mode - set regulator operating mode
2696  * @regulator: regulator source
2697  * @mode: operating mode - one of the REGULATOR_MODE constants
2698  *
2699  * Set regulator operating mode to increase regulator efficiency or improve
2700  * regulation performance.
2701  *
2702  * NOTE: Regulator system constraints must be set for this regulator before
2703  * calling this function otherwise this call will fail.
2704  */
2705 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2706 {
2707         struct regulator_dev *rdev = regulator->rdev;
2708         int ret;
2709         int regulator_curr_mode;
2710
2711         mutex_lock(&rdev->mutex);
2712
2713         /* sanity check */
2714         if (!rdev->desc->ops->set_mode) {
2715                 ret = -EINVAL;
2716                 goto out;
2717         }
2718
2719         /* return if the same mode is requested */
2720         if (rdev->desc->ops->get_mode) {
2721                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2722                 if (regulator_curr_mode == mode) {
2723                         ret = 0;
2724                         goto out;
2725                 }
2726         }
2727
2728         /* constraints check */
2729         ret = regulator_mode_constrain(rdev, &mode);
2730         if (ret < 0)
2731                 goto out;
2732
2733         ret = rdev->desc->ops->set_mode(rdev, mode);
2734 out:
2735         mutex_unlock(&rdev->mutex);
2736         return ret;
2737 }
2738 EXPORT_SYMBOL_GPL(regulator_set_mode);
2739
2740 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2741 {
2742         int ret;
2743
2744         mutex_lock(&rdev->mutex);
2745
2746         /* sanity check */
2747         if (!rdev->desc->ops->get_mode) {
2748                 ret = -EINVAL;
2749                 goto out;
2750         }
2751
2752         ret = rdev->desc->ops->get_mode(rdev);
2753 out:
2754         mutex_unlock(&rdev->mutex);
2755         return ret;
2756 }
2757
2758 /**
2759  * regulator_get_mode - get regulator operating mode
2760  * @regulator: regulator source
2761  *
2762  * Get the current regulator operating mode.
2763  */
2764 unsigned int regulator_get_mode(struct regulator *regulator)
2765 {
2766         return _regulator_get_mode(regulator->rdev);
2767 }
2768 EXPORT_SYMBOL_GPL(regulator_get_mode);
2769
2770 /**
2771  * regulator_set_optimum_mode - set regulator optimum operating mode
2772  * @regulator: regulator source
2773  * @uA_load: load current
2774  *
2775  * Notifies the regulator core of a new device load. This is then used by
2776  * DRMS (if enabled by constraints) to set the most efficient regulator
2777  * operating mode for the new regulator loading.
2778  *
2779  * Consumer devices notify their supply regulator of the maximum power
2780  * they will require (can be taken from device datasheet in the power
2781  * consumption tables) when they change operational status and hence power
2782  * state. Examples of operational state changes that can affect power
2783  * consumption are :-
2784  *
2785  *    o Device is opened / closed.
2786  *    o Device I/O is about to begin or has just finished.
2787  *    o Device is idling in between work.
2788  *
2789  * This information is also exported via sysfs to userspace.
2790  *
2791  * DRMS will sum the total requested load on the regulator and change
2792  * to the most efficient operating mode if platform constraints allow.
2793  *
2794  * Returns the new regulator mode or error.
2795  */
2796 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2797 {
2798         struct regulator_dev *rdev = regulator->rdev;
2799         struct regulator *consumer;
2800         int ret, output_uV, input_uV = 0, total_uA_load = 0;
2801         unsigned int mode;
2802
2803         if (rdev->supply)
2804                 input_uV = regulator_get_voltage(rdev->supply);
2805
2806         mutex_lock(&rdev->mutex);
2807
2808         /*
2809          * first check to see if we can set modes at all, otherwise just
2810          * tell the consumer everything is OK.
2811          */
2812         regulator->uA_load = uA_load;
2813         ret = regulator_check_drms(rdev);
2814         if (ret < 0) {
2815                 ret = 0;
2816                 goto out;
2817         }
2818
2819         if (!rdev->desc->ops->get_optimum_mode)
2820                 goto out;
2821
2822         /*
2823          * we can actually do this so any errors are indicators of
2824          * potential real failure.
2825          */
2826         ret = -EINVAL;
2827
2828         if (!rdev->desc->ops->set_mode)
2829                 goto out;
2830
2831         /* get output voltage */
2832         output_uV = _regulator_get_voltage(rdev);
2833         if (output_uV <= 0) {
2834                 rdev_err(rdev, "invalid output voltage found\n");
2835                 goto out;
2836         }
2837
2838         /* No supply? Use constraint voltage */
2839         if (input_uV <= 0)
2840                 input_uV = rdev->constraints->input_uV;
2841         if (input_uV <= 0) {
2842                 rdev_err(rdev, "invalid input voltage found\n");
2843                 goto out;
2844         }
2845
2846         /* calc total requested load for this regulator */
2847         list_for_each_entry(consumer, &rdev->consumer_list, list)
2848                 total_uA_load += consumer->uA_load;
2849
2850         mode = rdev->desc->ops->get_optimum_mode(rdev,
2851                                                  input_uV, output_uV,
2852                                                  total_uA_load);
2853         ret = regulator_mode_constrain(rdev, &mode);
2854         if (ret < 0) {
2855                 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2856                          total_uA_load, input_uV, output_uV);
2857                 goto out;
2858         }
2859
2860         ret = rdev->desc->ops->set_mode(rdev, mode);
2861         if (ret < 0) {
2862                 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2863                 goto out;
2864         }
2865         ret = mode;
2866 out:
2867         mutex_unlock(&rdev->mutex);
2868         return ret;
2869 }
2870 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2871
2872 /**
2873  * regulator_allow_bypass - allow the regulator to go into bypass mode
2874  *
2875  * @regulator: Regulator to configure
2876  * @enable: enable or disable bypass mode
2877  *
2878  * Allow the regulator to go into bypass mode if all other consumers
2879  * for the regulator also enable bypass mode and the machine
2880  * constraints allow this.  Bypass mode means that the regulator is
2881  * simply passing the input directly to the output with no regulation.
2882  */
2883 int regulator_allow_bypass(struct regulator *regulator, bool enable)
2884 {
2885         struct regulator_dev *rdev = regulator->rdev;
2886         int ret = 0;
2887
2888         if (!rdev->desc->ops->set_bypass)
2889                 return 0;
2890
2891         if (rdev->constraints &&
2892             !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2893                 return 0;
2894
2895         mutex_lock(&rdev->mutex);
2896
2897         if (enable && !regulator->bypass) {
2898                 rdev->bypass_count++;
2899
2900                 if (rdev->bypass_count == rdev->open_count) {
2901                         ret = rdev->desc->ops->set_bypass(rdev, enable);
2902                         if (ret != 0)
2903                                 rdev->bypass_count--;
2904                 }
2905
2906         } else if (!enable && regulator->bypass) {
2907                 rdev->bypass_count--;
2908
2909                 if (rdev->bypass_count != rdev->open_count) {
2910                         ret = rdev->desc->ops->set_bypass(rdev, enable);
2911                         if (ret != 0)
2912                                 rdev->bypass_count++;
2913                 }
2914         }
2915
2916         if (ret == 0)
2917                 regulator->bypass = enable;
2918
2919         mutex_unlock(&rdev->mutex);
2920
2921         return ret;
2922 }
2923 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2924
2925 /**
2926  * regulator_register_notifier - register regulator event notifier
2927  * @regulator: regulator source
2928  * @nb: notifier block
2929  *
2930  * Register notifier block to receive regulator events.
2931  */
2932 int regulator_register_notifier(struct regulator *regulator,
2933                               struct notifier_block *nb)
2934 {
2935         return blocking_notifier_chain_register(&regulator->rdev->notifier,
2936                                                 nb);
2937 }
2938 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2939
2940 /**
2941  * regulator_unregister_notifier - unregister regulator event notifier
2942  * @regulator: regulator source
2943  * @nb: notifier block
2944  *
2945  * Unregister regulator event notifier block.
2946  */
2947 int regulator_unregister_notifier(struct regulator *regulator,
2948                                 struct notifier_block *nb)
2949 {
2950         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2951                                                   nb);
2952 }
2953 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2954
2955 /* notify regulator consumers and downstream regulator consumers.
2956  * Note mutex must be held by caller.
2957  */
2958 static void _notifier_call_chain(struct regulator_dev *rdev,
2959                                   unsigned long event, void *data)
2960 {
2961         /* call rdev chain first */
2962         blocking_notifier_call_chain(&rdev->notifier, event, data);
2963 }
2964
2965 /**
2966  * regulator_bulk_get - get multiple regulator consumers
2967  *
2968  * @dev:           Device to supply
2969  * @num_consumers: Number of consumers to register
2970  * @consumers:     Configuration of consumers; clients are stored here.
2971  *
2972  * @return 0 on success, an errno on failure.
2973  *
2974  * This helper function allows drivers to get several regulator
2975  * consumers in one operation.  If any of the regulators cannot be
2976  * acquired then any regulators that were allocated will be freed
2977  * before returning to the caller.
2978  */
2979 int regulator_bulk_get(struct device *dev, int num_consumers,
2980                        struct regulator_bulk_data *consumers)
2981 {
2982         int i;
2983         int ret;
2984
2985         for (i = 0; i < num_consumers; i++)
2986                 consumers[i].consumer = NULL;
2987
2988         for (i = 0; i < num_consumers; i++) {
2989                 consumers[i].consumer = regulator_get(dev,
2990                                                       consumers[i].supply);
2991                 if (IS_ERR(consumers[i].consumer)) {
2992                         ret = PTR_ERR(consumers[i].consumer);
2993                         dev_err(dev, "Failed to get supply '%s': %d\n",
2994                                 consumers[i].supply, ret);
2995                         consumers[i].consumer = NULL;
2996                         goto err;
2997                 }
2998         }
2999
3000         return 0;
3001
3002 err:
3003         while (--i >= 0)
3004                 regulator_put(consumers[i].consumer);
3005
3006         return ret;
3007 }
3008 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3009
3010 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3011 {
3012         struct regulator_bulk_data *bulk = data;
3013
3014         bulk->ret = regulator_enable(bulk->consumer);
3015 }
3016
3017 /**
3018  * regulator_bulk_enable - enable multiple regulator consumers
3019  *
3020  * @num_consumers: Number of consumers
3021  * @consumers:     Consumer data; clients are stored here.
3022  * @return         0 on success, an errno on failure
3023  *
3024  * This convenience API allows consumers to enable multiple regulator
3025  * clients in a single API call.  If any consumers cannot be enabled
3026  * then any others that were enabled will be disabled again prior to
3027  * return.
3028  */
3029 int regulator_bulk_enable(int num_consumers,
3030                           struct regulator_bulk_data *consumers)
3031 {
3032         ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3033         int i;
3034         int ret = 0;
3035
3036         for (i = 0; i < num_consumers; i++) {
3037                 if (consumers[i].consumer->always_on)
3038                         consumers[i].ret = 0;
3039                 else
3040                         async_schedule_domain(regulator_bulk_enable_async,
3041                                               &consumers[i], &async_domain);
3042         }
3043
3044         async_synchronize_full_domain(&async_domain);
3045
3046         /* If any consumer failed we need to unwind any that succeeded */
3047         for (i = 0; i < num_consumers; i++) {
3048                 if (consumers[i].ret != 0) {
3049                         ret = consumers[i].ret;
3050                         goto err;
3051                 }
3052         }
3053
3054         return 0;
3055
3056 err:
3057         for (i = 0; i < num_consumers; i++) {
3058                 if (consumers[i].ret < 0)
3059                         pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3060                                consumers[i].ret);
3061                 else
3062                         regulator_disable(consumers[i].consumer);
3063         }
3064
3065         return ret;
3066 }
3067 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3068
3069 /**
3070  * regulator_bulk_disable - disable multiple regulator consumers
3071  *
3072  * @num_consumers: Number of consumers
3073  * @consumers:     Consumer data; clients are stored here.
3074  * @return         0 on success, an errno on failure
3075  *
3076  * This convenience API allows consumers to disable multiple regulator
3077  * clients in a single API call.  If any consumers cannot be disabled
3078  * then any others that were disabled will be enabled again prior to
3079  * return.
3080  */
3081 int regulator_bulk_disable(int num_consumers,
3082                            struct regulator_bulk_data *consumers)
3083 {
3084         int i;
3085         int ret, r;
3086
3087         for (i = num_consumers - 1; i >= 0; --i) {
3088                 ret = regulator_disable(consumers[i].consumer);
3089                 if (ret != 0)
3090                         goto err;
3091         }
3092
3093         return 0;
3094
3095 err:
3096         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3097         for (++i; i < num_consumers; ++i) {
3098                 r = regulator_enable(consumers[i].consumer);
3099                 if (r != 0)
3100                         pr_err("Failed to reename %s: %d\n",
3101                                consumers[i].supply, r);
3102         }
3103
3104         return ret;
3105 }
3106 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3107
3108 /**
3109  * regulator_bulk_force_disable - force disable multiple regulator consumers
3110  *
3111  * @num_consumers: Number of consumers
3112  * @consumers:     Consumer data; clients are stored here.
3113  * @return         0 on success, an errno on failure
3114  *
3115  * This convenience API allows consumers to forcibly disable multiple regulator
3116  * clients in a single API call.
3117  * NOTE: This should be used for situations when device damage will
3118  * likely occur if the regulators are not disabled (e.g. over temp).
3119  * Although regulator_force_disable function call for some consumers can
3120  * return error numbers, the function is called for all consumers.
3121  */
3122 int regulator_bulk_force_disable(int num_consumers,
3123                            struct regulator_bulk_data *consumers)
3124 {
3125         int i;
3126         int ret;
3127
3128         for (i = 0; i < num_consumers; i++)
3129                 consumers[i].ret =
3130                             regulator_force_disable(consumers[i].consumer);
3131
3132         for (i = 0; i < num_consumers; i++) {
3133                 if (consumers[i].ret != 0) {
3134                         ret = consumers[i].ret;
3135                         goto out;
3136                 }
3137         }
3138
3139         return 0;
3140 out:
3141         return ret;
3142 }
3143 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3144
3145 /**
3146  * regulator_bulk_free - free multiple regulator consumers
3147  *
3148  * @num_consumers: Number of consumers
3149  * @consumers:     Consumer data; clients are stored here.
3150  *
3151  * This convenience API allows consumers to free multiple regulator
3152  * clients in a single API call.
3153  */
3154 void regulator_bulk_free(int num_consumers,
3155                          struct regulator_bulk_data *consumers)
3156 {
3157         int i;
3158
3159         for (i = 0; i < num_consumers; i++) {
3160                 regulator_put(consumers[i].consumer);
3161                 consumers[i].consumer = NULL;
3162         }
3163 }
3164 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3165
3166 /**
3167  * regulator_notifier_call_chain - call regulator event notifier
3168  * @rdev: regulator source
3169  * @event: notifier block
3170  * @data: callback-specific data.
3171  *
3172  * Called by regulator drivers to notify clients a regulator event has
3173  * occurred. We also notify regulator clients downstream.
3174  * Note lock must be held by caller.
3175  */
3176 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3177                                   unsigned long event, void *data)
3178 {
3179         _notifier_call_chain(rdev, event, data);
3180         return NOTIFY_DONE;
3181
3182 }
3183 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3184
3185 /**
3186  * regulator_mode_to_status - convert a regulator mode into a status
3187  *
3188  * @mode: Mode to convert
3189  *
3190  * Convert a regulator mode into a status.
3191  */
3192 int regulator_mode_to_status(unsigned int mode)
3193 {
3194         switch (mode) {
3195         case REGULATOR_MODE_FAST:
3196                 return REGULATOR_STATUS_FAST;
3197         case REGULATOR_MODE_NORMAL:
3198                 return REGULATOR_STATUS_NORMAL;
3199         case REGULATOR_MODE_IDLE:
3200                 return REGULATOR_STATUS_IDLE;
3201         case REGULATOR_MODE_STANDBY:
3202                 return REGULATOR_STATUS_STANDBY;
3203         default:
3204                 return REGULATOR_STATUS_UNDEFINED;
3205         }
3206 }
3207 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3208
3209 /*
3210  * To avoid cluttering sysfs (and memory) with useless state, only
3211  * create attributes that can be meaningfully displayed.
3212  */
3213 static int add_regulator_attributes(struct regulator_dev *rdev)
3214 {
3215         struct device           *dev = &rdev->dev;
3216         struct regulator_ops    *ops = rdev->desc->ops;
3217         int                     status = 0;
3218
3219         /* some attributes need specific methods to be displayed */
3220         if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3221             (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3222             (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3223                 (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1))) {
3224                 status = device_create_file(dev, &dev_attr_microvolts);
3225                 if (status < 0)
3226                         return status;
3227         }
3228         if (ops->get_current_limit) {
3229                 status = device_create_file(dev, &dev_attr_microamps);
3230                 if (status < 0)
3231                         return status;
3232         }
3233         if (ops->get_mode) {
3234                 status = device_create_file(dev, &dev_attr_opmode);
3235                 if (status < 0)
3236                         return status;
3237         }
3238         if (rdev->ena_pin || ops->is_enabled) {
3239                 status = device_create_file(dev, &dev_attr_state);
3240                 if (status < 0)
3241                         return status;
3242         }
3243         if (ops->get_status) {
3244                 status = device_create_file(dev, &dev_attr_status);
3245                 if (status < 0)
3246                         return status;
3247         }
3248         if (ops->get_bypass) {
3249                 status = device_create_file(dev, &dev_attr_bypass);
3250                 if (status < 0)
3251                         return status;
3252         }
3253
3254         /* some attributes are type-specific */
3255         if (rdev->desc->type == REGULATOR_CURRENT) {
3256                 status = device_create_file(dev, &dev_attr_requested_microamps);
3257                 if (status < 0)
3258                         return status;
3259         }
3260
3261         /* all the other attributes exist to support constraints;
3262          * don't show them if there are no constraints, or if the
3263          * relevant supporting methods are missing.
3264          */
3265         if (!rdev->constraints)
3266                 return status;
3267
3268         /* constraints need specific supporting methods */
3269         if (ops->set_voltage || ops->set_voltage_sel) {
3270                 status = device_create_file(dev, &dev_attr_min_microvolts);
3271                 if (status < 0)
3272                         return status;
3273                 status = device_create_file(dev, &dev_attr_max_microvolts);
3274                 if (status < 0)
3275                         return status;
3276         }
3277         if (ops->set_current_limit) {
3278                 status = device_create_file(dev, &dev_attr_min_microamps);
3279                 if (status < 0)
3280                         return status;
3281                 status = device_create_file(dev, &dev_attr_max_microamps);
3282                 if (status < 0)
3283                         return status;
3284         }
3285
3286         status = device_create_file(dev, &dev_attr_suspend_standby_state);
3287         if (status < 0)
3288                 return status;
3289         status = device_create_file(dev, &dev_attr_suspend_mem_state);
3290         if (status < 0)
3291                 return status;
3292         status = device_create_file(dev, &dev_attr_suspend_disk_state);
3293         if (status < 0)
3294                 return status;
3295
3296         if (ops->set_suspend_voltage) {
3297                 status = device_create_file(dev,
3298                                 &dev_attr_suspend_standby_microvolts);
3299                 if (status < 0)
3300                         return status;
3301                 status = device_create_file(dev,
3302                                 &dev_attr_suspend_mem_microvolts);
3303                 if (status < 0)
3304                         return status;
3305                 status = device_create_file(dev,
3306                                 &dev_attr_suspend_disk_microvolts);
3307                 if (status < 0)
3308                         return status;
3309         }
3310
3311         if (ops->set_suspend_mode) {
3312                 status = device_create_file(dev,
3313                                 &dev_attr_suspend_standby_mode);
3314                 if (status < 0)
3315                         return status;
3316                 status = device_create_file(dev,
3317                                 &dev_attr_suspend_mem_mode);
3318                 if (status < 0)
3319                         return status;
3320                 status = device_create_file(dev,
3321                                 &dev_attr_suspend_disk_mode);
3322                 if (status < 0)
3323                         return status;
3324         }
3325
3326         return status;
3327 }
3328
3329 static void rdev_init_debugfs(struct regulator_dev *rdev)
3330 {
3331         rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3332         if (!rdev->debugfs) {
3333                 rdev_warn(rdev, "Failed to create debugfs directory\n");
3334                 return;
3335         }
3336
3337         debugfs_create_u32("use_count", 0444, rdev->debugfs,
3338                            &rdev->use_count);
3339         debugfs_create_u32("open_count", 0444, rdev->debugfs,
3340                            &rdev->open_count);
3341         debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3342                            &rdev->bypass_count);
3343 }
3344
3345 /**
3346  * regulator_register - register regulator
3347  * @regulator_desc: regulator to register
3348  * @config: runtime configuration for regulator
3349  *
3350  * Called by regulator drivers to register a regulator.
3351  * Returns a valid pointer to struct regulator_dev on success
3352  * or an ERR_PTR() on error.
3353  */
3354 struct regulator_dev *
3355 regulator_register(const struct regulator_desc *regulator_desc,
3356                    const struct regulator_config *config)
3357 {
3358         const struct regulation_constraints *constraints = NULL;
3359         const struct regulator_init_data *init_data;
3360         static atomic_t regulator_no = ATOMIC_INIT(0);
3361         struct regulator_dev *rdev;
3362         struct device *dev;
3363         int ret, i;
3364         const char *supply = NULL;
3365
3366         if (regulator_desc == NULL || config == NULL)
3367                 return ERR_PTR(-EINVAL);
3368
3369         dev = config->dev;
3370         WARN_ON(!dev);
3371
3372         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3373                 return ERR_PTR(-EINVAL);
3374
3375         if (regulator_desc->type != REGULATOR_VOLTAGE &&
3376             regulator_desc->type != REGULATOR_CURRENT)
3377                 return ERR_PTR(-EINVAL);
3378
3379         /* Only one of each should be implemented */
3380         WARN_ON(regulator_desc->ops->get_voltage &&
3381                 regulator_desc->ops->get_voltage_sel);
3382         WARN_ON(regulator_desc->ops->set_voltage &&
3383                 regulator_desc->ops->set_voltage_sel);
3384
3385         /* If we're using selectors we must implement list_voltage. */
3386         if (regulator_desc->ops->get_voltage_sel &&
3387             !regulator_desc->ops->list_voltage) {
3388                 return ERR_PTR(-EINVAL);
3389         }
3390         if (regulator_desc->ops->set_voltage_sel &&
3391             !regulator_desc->ops->list_voltage) {
3392                 return ERR_PTR(-EINVAL);
3393         }
3394
3395         init_data = config->init_data;
3396
3397         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3398         if (rdev == NULL)
3399                 return ERR_PTR(-ENOMEM);
3400
3401         mutex_lock(&regulator_list_mutex);
3402
3403         mutex_init(&rdev->mutex);
3404         rdev->reg_data = config->driver_data;
3405         rdev->owner = regulator_desc->owner;
3406         rdev->desc = regulator_desc;
3407         if (config->regmap)
3408                 rdev->regmap = config->regmap;
3409         else if (dev_get_regmap(dev, NULL))
3410                 rdev->regmap = dev_get_regmap(dev, NULL);
3411         else if (dev->parent)
3412                 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3413         INIT_LIST_HEAD(&rdev->consumer_list);
3414         INIT_LIST_HEAD(&rdev->list);
3415         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3416         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3417
3418         /* preform any regulator specific init */
3419         if (init_data && init_data->regulator_init) {
3420                 ret = init_data->regulator_init(rdev->reg_data);
3421                 if (ret < 0)
3422                         goto clean;
3423         }
3424
3425         /* register with sysfs */
3426         rdev->dev.class = &regulator_class;
3427         rdev->dev.of_node = config->of_node;
3428         rdev->dev.parent = dev;
3429         dev_set_name(&rdev->dev, "regulator.%d",
3430                      atomic_inc_return(&regulator_no) - 1);
3431         ret = device_register(&rdev->dev);
3432         if (ret != 0) {
3433                 put_device(&rdev->dev);
3434                 goto clean;
3435         }
3436
3437         dev_set_drvdata(&rdev->dev, rdev);
3438
3439         if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3440                 ret = regulator_ena_gpio_request(rdev, config);
3441                 if (ret != 0) {
3442                         rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3443                                  config->ena_gpio, ret);
3444                         goto wash;
3445                 }
3446
3447                 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3448                         rdev->ena_gpio_state = 1;
3449
3450                 if (config->ena_gpio_invert)
3451                         rdev->ena_gpio_state = !rdev->ena_gpio_state;
3452         }
3453
3454         /* set regulator constraints */
3455         if (init_data)
3456                 constraints = &init_data->constraints;
3457
3458         ret = set_machine_constraints(rdev, constraints);
3459         if (ret < 0)
3460                 goto scrub;
3461
3462         /* add attributes supported by this regulator */
3463         ret = add_regulator_attributes(rdev);
3464         if (ret < 0)
3465                 goto scrub;
3466
3467         if (init_data && init_data->supply_regulator)
3468                 supply = init_data->supply_regulator;
3469         else if (regulator_desc->supply_name)
3470                 supply = regulator_desc->supply_name;
3471
3472         if (supply) {
3473                 struct regulator_dev *r;
3474
3475                 r = regulator_dev_lookup(dev, supply, &ret);
3476
3477                 if (ret == -ENODEV) {
3478                         /*
3479                          * No supply was specified for this regulator and
3480                          * there will never be one.
3481                          */
3482                         ret = 0;
3483                         goto add_dev;
3484                 } else if (!r) {
3485                         dev_err(dev, "Failed to find supply %s\n", supply);
3486                         ret = -EPROBE_DEFER;
3487                         goto scrub;
3488                 }
3489
3490                 ret = set_supply(rdev, r);
3491                 if (ret < 0)
3492                         goto scrub;
3493
3494                 /* Enable supply if rail is enabled */
3495                 if (_regulator_is_enabled(rdev)) {
3496                         ret = regulator_enable(rdev->supply);
3497                         if (ret < 0)
3498                                 goto scrub;
3499                 }
3500         }
3501
3502 add_dev:
3503         /* add consumers devices */
3504         if (init_data) {
3505                 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3506                         ret = set_consumer_device_supply(rdev,
3507                                 init_data->consumer_supplies[i].dev_name,
3508                                 init_data->consumer_supplies[i].supply);
3509                         if (ret < 0) {
3510                                 dev_err(dev, "Failed to set supply %s\n",
3511                                         init_data->consumer_supplies[i].supply);
3512                                 goto unset_supplies;
3513                         }
3514                 }
3515         }
3516
3517         list_add(&rdev->list, &regulator_list);
3518
3519         rdev_init_debugfs(rdev);
3520 out:
3521         mutex_unlock(&regulator_list_mutex);
3522         return rdev;
3523
3524 unset_supplies:
3525         unset_regulator_supplies(rdev);
3526
3527 scrub:
3528         if (rdev->supply)
3529                 _regulator_put(rdev->supply);
3530         regulator_ena_gpio_free(rdev);
3531         kfree(rdev->constraints);
3532 wash:
3533         device_unregister(&rdev->dev);
3534         /* device core frees rdev */
3535         rdev = ERR_PTR(ret);
3536         goto out;
3537
3538 clean:
3539         kfree(rdev);
3540         rdev = ERR_PTR(ret);
3541         goto out;
3542 }
3543 EXPORT_SYMBOL_GPL(regulator_register);
3544
3545 /**
3546  * regulator_unregister - unregister regulator
3547  * @rdev: regulator to unregister
3548  *
3549  * Called by regulator drivers to unregister a regulator.
3550  */
3551 void regulator_unregister(struct regulator_dev *rdev)
3552 {
3553         if (rdev == NULL)
3554                 return;
3555
3556         if (rdev->supply) {
3557                 while (rdev->use_count--)
3558                         regulator_disable(rdev->supply);
3559                 regulator_put(rdev->supply);
3560         }
3561         mutex_lock(&regulator_list_mutex);
3562         debugfs_remove_recursive(rdev->debugfs);
3563         flush_work(&rdev->disable_work.work);
3564         WARN_ON(rdev->open_count);
3565         unset_regulator_supplies(rdev);
3566         list_del(&rdev->list);
3567         kfree(rdev->constraints);
3568         regulator_ena_gpio_free(rdev);
3569         device_unregister(&rdev->dev);
3570         mutex_unlock(&regulator_list_mutex);
3571 }
3572 EXPORT_SYMBOL_GPL(regulator_unregister);
3573
3574 /**
3575  * regulator_suspend_prepare - prepare regulators for system wide suspend
3576  * @state: system suspend state
3577  *
3578  * Configure each regulator with it's suspend operating parameters for state.
3579  * This will usually be called by machine suspend code prior to supending.
3580  */
3581 int regulator_suspend_prepare(suspend_state_t state)
3582 {
3583         struct regulator_dev *rdev;
3584         int ret = 0;
3585
3586         /* ON is handled by regulator active state */
3587         if (state == PM_SUSPEND_ON)
3588                 return -EINVAL;
3589
3590         mutex_lock(&regulator_list_mutex);
3591         list_for_each_entry(rdev, &regulator_list, list) {
3592
3593                 mutex_lock(&rdev->mutex);
3594                 ret = suspend_prepare(rdev, state);
3595                 mutex_unlock(&rdev->mutex);
3596
3597                 if (ret < 0) {
3598                         rdev_err(rdev, "failed to prepare\n");
3599                         goto out;
3600                 }
3601         }
3602 out:
3603         mutex_unlock(&regulator_list_mutex);
3604         return ret;
3605 }
3606 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3607
3608 /**
3609  * regulator_suspend_finish - resume regulators from system wide suspend
3610  *
3611  * Turn on regulators that might be turned off by regulator_suspend_prepare
3612  * and that should be turned on according to the regulators properties.
3613  */
3614 int regulator_suspend_finish(void)
3615 {
3616         struct regulator_dev *rdev;
3617         int ret = 0, error;
3618
3619         mutex_lock(&regulator_list_mutex);
3620         list_for_each_entry(rdev, &regulator_list, list) {
3621                 struct regulator_ops *ops = rdev->desc->ops;
3622
3623                 mutex_lock(&rdev->mutex);
3624                 if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
3625                                 ops->enable) {
3626                         error = ops->enable(rdev);
3627                         if (error)
3628                                 ret = error;
3629                 } else {
3630                         if (!has_full_constraints)
3631                                 goto unlock;
3632                         if (!ops->disable)
3633                                 goto unlock;
3634                         if (!_regulator_is_enabled(rdev))
3635                                 goto unlock;
3636
3637                         error = ops->disable(rdev);
3638                         if (error)
3639                                 ret = error;
3640                 }
3641 unlock:
3642                 mutex_unlock(&rdev->mutex);
3643         }
3644         mutex_unlock(&regulator_list_mutex);
3645         return ret;
3646 }
3647 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3648
3649 /**
3650  * regulator_has_full_constraints - the system has fully specified constraints
3651  *
3652  * Calling this function will cause the regulator API to disable all
3653  * regulators which have a zero use count and don't have an always_on
3654  * constraint in a late_initcall.
3655  *
3656  * The intention is that this will become the default behaviour in a
3657  * future kernel release so users are encouraged to use this facility
3658  * now.
3659  */
3660 void regulator_has_full_constraints(void)
3661 {
3662         has_full_constraints = 1;
3663 }
3664 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3665
3666 /**
3667  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3668  *
3669  * Calling this function will cause the regulator API to provide a
3670  * dummy regulator to consumers if no physical regulator is found,
3671  * allowing most consumers to proceed as though a regulator were
3672  * configured.  This allows systems such as those with software
3673  * controllable regulators for the CPU core only to be brought up more
3674  * readily.
3675  */
3676 void regulator_use_dummy_regulator(void)
3677 {
3678         board_wants_dummy_regulator = true;
3679 }
3680 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3681
3682 /**
3683  * rdev_get_drvdata - get rdev regulator driver data
3684  * @rdev: regulator
3685  *
3686  * Get rdev regulator driver private data. This call can be used in the
3687  * regulator driver context.
3688  */
3689 void *rdev_get_drvdata(struct regulator_dev *rdev)
3690 {
3691         return rdev->reg_data;
3692 }
3693 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3694
3695 /**
3696  * regulator_get_drvdata - get regulator driver data
3697  * @regulator: regulator
3698  *
3699  * Get regulator driver private data. This call can be used in the consumer
3700  * driver context when non API regulator specific functions need to be called.
3701  */
3702 void *regulator_get_drvdata(struct regulator *regulator)
3703 {
3704         return regulator->rdev->reg_data;
3705 }
3706 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3707
3708 /**
3709  * regulator_set_drvdata - set regulator driver data
3710  * @regulator: regulator
3711  * @data: data
3712  */
3713 void regulator_set_drvdata(struct regulator *regulator, void *data)
3714 {
3715         regulator->rdev->reg_data = data;
3716 }
3717 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3718
3719 /**
3720  * regulator_get_id - get regulator ID
3721  * @rdev: regulator
3722  */
3723 int rdev_get_id(struct regulator_dev *rdev)
3724 {
3725         return rdev->desc->id;
3726 }
3727 EXPORT_SYMBOL_GPL(rdev_get_id);
3728
3729 struct device *rdev_get_dev(struct regulator_dev *rdev)
3730 {
3731         return &rdev->dev;
3732 }
3733 EXPORT_SYMBOL_GPL(rdev_get_dev);
3734
3735 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3736 {
3737         return reg_init_data->driver_data;
3738 }
3739 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3740
3741 #ifdef CONFIG_DEBUG_FS
3742 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3743                                     size_t count, loff_t *ppos)
3744 {
3745         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3746         ssize_t len, ret = 0;
3747         struct regulator_map *map;
3748
3749         if (!buf)
3750                 return -ENOMEM;
3751
3752         list_for_each_entry(map, &regulator_map_list, list) {
3753                 len = snprintf(buf + ret, PAGE_SIZE - ret,
3754                                "%s -> %s.%s\n",
3755                                rdev_get_name(map->regulator), map->dev_name,
3756                                map->supply);
3757                 if (len >= 0)
3758                         ret += len;
3759                 if (ret > PAGE_SIZE) {
3760                         ret = PAGE_SIZE;
3761                         break;
3762                 }
3763         }
3764
3765         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3766
3767         kfree(buf);
3768
3769         return ret;
3770 }
3771 #endif
3772
3773 static const struct file_operations supply_map_fops = {
3774 #ifdef CONFIG_DEBUG_FS
3775         .read = supply_map_read_file,
3776         .llseek = default_llseek,
3777 #endif
3778 };
3779
3780 static int __init regulator_init(void)
3781 {
3782         int ret;
3783
3784         ret = class_register(&regulator_class);
3785
3786         debugfs_root = debugfs_create_dir("regulator", NULL);
3787         if (!debugfs_root)
3788                 pr_warn("regulator: Failed to create debugfs directory\n");
3789
3790         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3791                             &supply_map_fops);
3792
3793         regulator_dummy_init();
3794
3795         return ret;
3796 }
3797
3798 /* init early to allow our consumers to complete system booting */
3799 core_initcall(regulator_init);
3800
3801 static int __init regulator_init_complete(void)
3802 {
3803         struct regulator_dev *rdev;
3804         struct regulator_ops *ops;
3805         struct regulation_constraints *c;
3806         int enabled, ret;
3807
3808         /*
3809          * Since DT doesn't provide an idiomatic mechanism for
3810          * enabling full constraints and since it's much more natural
3811          * with DT to provide them just assume that a DT enabled
3812          * system has full constraints.
3813          */
3814         if (of_have_populated_dt())
3815                 has_full_constraints = true;
3816
3817         mutex_lock(&regulator_list_mutex);
3818
3819         /* If we have a full configuration then disable any regulators
3820          * which are not in use or always_on.  This will become the
3821          * default behaviour in the future.
3822          */
3823         list_for_each_entry(rdev, &regulator_list, list) {
3824                 ops = rdev->desc->ops;
3825                 c = rdev->constraints;
3826
3827                 if (!ops->disable || (c && c->always_on))
3828                         continue;
3829
3830                 mutex_lock(&rdev->mutex);
3831
3832                 if (rdev->use_count)
3833                         goto unlock;
3834
3835                 /* If we can't read the status assume it's on. */
3836                 if (ops->is_enabled)
3837                         enabled = ops->is_enabled(rdev);
3838                 else
3839                         enabled = 1;
3840
3841                 if (!enabled)
3842                         goto unlock;
3843
3844                 if (has_full_constraints) {
3845                         /* We log since this may kill the system if it
3846                          * goes wrong. */
3847                         rdev_info(rdev, "disabling\n");
3848                         ret = ops->disable(rdev);
3849                         if (ret != 0) {
3850                                 rdev_err(rdev, "couldn't disable: %d\n", ret);
3851                         }
3852                 } else {
3853                         /* The intention is that in future we will
3854                          * assume that full constraints are provided
3855                          * so warn even if we aren't going to do
3856                          * anything here.
3857                          */
3858                         rdev_warn(rdev, "incomplete constraints, leaving on\n");
3859                 }
3860
3861 unlock:
3862                 mutex_unlock(&rdev->mutex);
3863         }
3864
3865         mutex_unlock(&regulator_list_mutex);
3866
3867         return 0;
3868 }
3869 late_initcall(regulator_init_complete);