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