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