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