regulator: tps80031: make regualtor_init_data as pointer type
[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         if (rc == 0)
1498                 regulator_put(regulator);
1499         else
1500                 WARN_ON(rc);
1501 }
1502 EXPORT_SYMBOL_GPL(devm_regulator_put);
1503
1504 static int _regulator_can_change_status(struct regulator_dev *rdev)
1505 {
1506         if (!rdev->constraints)
1507                 return 0;
1508
1509         if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1510                 return 1;
1511         else
1512                 return 0;
1513 }
1514
1515 /* locks held by regulator_enable() */
1516 static int _regulator_enable(struct regulator_dev *rdev)
1517 {
1518         int ret, delay;
1519
1520         /* check voltage and requested load before enabling */
1521         if (rdev->constraints &&
1522             (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1523                 drms_uA_update(rdev);
1524
1525         if (rdev->use_count == 0) {
1526                 /* The regulator may on if it's not switchable or left on */
1527                 ret = _regulator_is_enabled(rdev);
1528                 if (ret == -EINVAL || ret == 0) {
1529                         if (!_regulator_can_change_status(rdev))
1530                                 return -EPERM;
1531
1532                         if (!rdev->desc->ops->enable)
1533                                 return -EINVAL;
1534
1535                         /* Query before enabling in case configuration
1536                          * dependent.  */
1537                         ret = _regulator_get_enable_time(rdev);
1538                         if (ret >= 0) {
1539                                 delay = ret;
1540                         } else {
1541                                 rdev_warn(rdev, "enable_time() failed: %d\n",
1542                                            ret);
1543                                 delay = 0;
1544                         }
1545
1546                         trace_regulator_enable(rdev_get_name(rdev));
1547                         _notifier_call_chain(
1548                                 rdev, REGULATOR_EVENT_PRE_ENABLE, NULL);
1549
1550                         /* Allow the regulator to ramp; it would be useful
1551                          * to extend this for bulk operations so that the
1552                          * regulators can ramp together.  */
1553                         ret = rdev->desc->ops->enable(rdev);
1554                         if (ret < 0)
1555                                 return ret;
1556
1557                         trace_regulator_enable_delay(rdev_get_name(rdev));
1558
1559                         if (delay >= 1000) {
1560                                 mdelay(delay / 1000);
1561                                 udelay(delay % 1000);
1562                         } else if (delay) {
1563                                 udelay(delay);
1564                         }
1565
1566                         _notifier_call_chain(
1567                                 rdev, REGULATOR_EVENT_POST_ENABLE, NULL);
1568                         trace_regulator_enable_complete(rdev_get_name(rdev));
1569
1570                 } else if (ret < 0) {
1571                         rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1572                         return ret;
1573                 }
1574                 /* Fallthrough on positive return values - already enabled */
1575         }
1576
1577         rdev->use_count++;
1578
1579         return 0;
1580 }
1581
1582 /**
1583  * regulator_enable - enable regulator output
1584  * @regulator: regulator source
1585  *
1586  * Request that the regulator be enabled with the regulator output at
1587  * the predefined voltage or current value.  Calls to regulator_enable()
1588  * must be balanced with calls to regulator_disable().
1589  *
1590  * NOTE: the output value can be set by other drivers, boot loader or may be
1591  * hardwired in the regulator.
1592  */
1593 int regulator_enable(struct regulator *regulator)
1594 {
1595         struct regulator_dev *rdev = regulator->rdev;
1596         int ret = 0;
1597
1598         if (rdev->supply) {
1599                 ret = regulator_enable(rdev->supply);
1600                 if (ret != 0)
1601                         return ret;
1602         }
1603
1604         mutex_lock(&rdev->mutex);
1605         ret = _regulator_enable(rdev);
1606         mutex_unlock(&rdev->mutex);
1607
1608         if (ret != 0 && rdev->supply)
1609                 regulator_disable(rdev->supply);
1610
1611         return ret;
1612 }
1613 EXPORT_SYMBOL_GPL(regulator_enable);
1614
1615 /* locks held by regulator_disable() */
1616 static int _regulator_disable(struct regulator_dev *rdev)
1617 {
1618         int ret = 0;
1619
1620         if (WARN(rdev->use_count <= 0,
1621                  "unbalanced disables for %s\n", rdev_get_name(rdev)))
1622                 return -EIO;
1623
1624         /* are we the last user and permitted to disable ? */
1625         if (rdev->use_count == 1 &&
1626             (rdev->constraints && !rdev->constraints->always_on)) {
1627
1628                 /* we are last user */
1629                 if (_regulator_can_change_status(rdev) &&
1630                     rdev->desc->ops->disable) {
1631                         trace_regulator_disable(rdev_get_name(rdev));
1632
1633                         ret = rdev->desc->ops->disable(rdev);
1634                         if (ret < 0) {
1635                                 rdev_err(rdev, "failed to disable\n");
1636                                 return ret;
1637                         }
1638
1639                         trace_regulator_disable_complete(rdev_get_name(rdev));
1640
1641                         _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1642                                              NULL);
1643                 }
1644
1645                 rdev->use_count = 0;
1646         } else if (rdev->use_count > 1) {
1647
1648                 if (rdev->constraints &&
1649                         (rdev->constraints->valid_ops_mask &
1650                         REGULATOR_CHANGE_DRMS))
1651                         drms_uA_update(rdev);
1652
1653                 rdev->use_count--;
1654         }
1655
1656         return ret;
1657 }
1658
1659 /**
1660  * regulator_disable - disable regulator output
1661  * @regulator: regulator source
1662  *
1663  * Disable the regulator output voltage or current.  Calls to
1664  * regulator_enable() must be balanced with calls to
1665  * regulator_disable().
1666  *
1667  * NOTE: this will only disable the regulator output if no other consumer
1668  * devices have it enabled, the regulator device supports disabling and
1669  * machine constraints permit this operation.
1670  */
1671 int regulator_disable(struct regulator *regulator)
1672 {
1673         struct regulator_dev *rdev = regulator->rdev;
1674         int ret = 0;
1675
1676         mutex_lock(&rdev->mutex);
1677         ret = _regulator_disable(rdev);
1678         mutex_unlock(&rdev->mutex);
1679
1680         if (ret == 0 && rdev->supply)
1681                 regulator_disable(rdev->supply);
1682
1683         return ret;
1684 }
1685 EXPORT_SYMBOL_GPL(regulator_disable);
1686
1687 /* locks held by regulator_force_disable() */
1688 static int _regulator_force_disable(struct regulator_dev *rdev)
1689 {
1690         int ret = 0;
1691
1692         /* force disable */
1693         if (rdev->desc->ops->disable) {
1694                 /* ah well, who wants to live forever... */
1695                 ret = rdev->desc->ops->disable(rdev);
1696                 if (ret < 0) {
1697                         rdev_err(rdev, "failed to force disable\n");
1698                         return ret;
1699                 }
1700                 /* notify other consumers that power has been forced off */
1701                 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1702                         REGULATOR_EVENT_DISABLE, NULL);
1703         }
1704
1705         return ret;
1706 }
1707
1708 /**
1709  * regulator_force_disable - force disable regulator output
1710  * @regulator: regulator source
1711  *
1712  * Forcibly disable the regulator output voltage or current.
1713  * NOTE: this *will* disable the regulator output even if other consumer
1714  * devices have it enabled. This should be used for situations when device
1715  * damage will likely occur if the regulator is not disabled (e.g. over temp).
1716  */
1717 int regulator_force_disable(struct regulator *regulator)
1718 {
1719         struct regulator_dev *rdev = regulator->rdev;
1720         int ret;
1721
1722         mutex_lock(&rdev->mutex);
1723         regulator->uA_load = 0;
1724         ret = _regulator_force_disable(regulator->rdev);
1725         mutex_unlock(&rdev->mutex);
1726
1727         if (rdev->supply)
1728                 while (rdev->open_count--)
1729                         regulator_disable(rdev->supply);
1730
1731         return ret;
1732 }
1733 EXPORT_SYMBOL_GPL(regulator_force_disable);
1734
1735 static void regulator_disable_work(struct work_struct *work)
1736 {
1737         struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1738                                                   disable_work.work);
1739         int count, i, ret;
1740
1741         mutex_lock(&rdev->mutex);
1742
1743         BUG_ON(!rdev->deferred_disables);
1744
1745         count = rdev->deferred_disables;
1746         rdev->deferred_disables = 0;
1747
1748         for (i = 0; i < count; i++) {
1749                 ret = _regulator_disable(rdev);
1750                 if (ret != 0)
1751                         rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1752         }
1753
1754         mutex_unlock(&rdev->mutex);
1755
1756         if (rdev->supply) {
1757                 for (i = 0; i < count; i++) {
1758                         ret = regulator_disable(rdev->supply);
1759                         if (ret != 0) {
1760                                 rdev_err(rdev,
1761                                          "Supply disable failed: %d\n", ret);
1762                         }
1763                 }
1764         }
1765 }
1766
1767 /**
1768  * regulator_disable_deferred - disable regulator output with delay
1769  * @regulator: regulator source
1770  * @ms: miliseconds until the regulator is disabled
1771  *
1772  * Execute regulator_disable() on the regulator after a delay.  This
1773  * is intended for use with devices that require some time to quiesce.
1774  *
1775  * NOTE: this will only disable the regulator output if no other consumer
1776  * devices have it enabled, the regulator device supports disabling and
1777  * machine constraints permit this operation.
1778  */
1779 int regulator_disable_deferred(struct regulator *regulator, int ms)
1780 {
1781         struct regulator_dev *rdev = regulator->rdev;
1782         int ret;
1783
1784         mutex_lock(&rdev->mutex);
1785         rdev->deferred_disables++;
1786         mutex_unlock(&rdev->mutex);
1787
1788         ret = schedule_delayed_work(&rdev->disable_work,
1789                                     msecs_to_jiffies(ms));
1790         if (ret < 0)
1791                 return ret;
1792         else
1793                 return 0;
1794 }
1795 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1796
1797 static int _regulator_is_enabled(struct regulator_dev *rdev)
1798 {
1799         /* If we don't know then assume that the regulator is always on */
1800         if (!rdev->desc->ops->is_enabled)
1801                 return 1;
1802
1803         return rdev->desc->ops->is_enabled(rdev);
1804 }
1805
1806 /**
1807  * regulator_is_enabled - is the regulator output enabled
1808  * @regulator: regulator source
1809  *
1810  * Returns positive if the regulator driver backing the source/client
1811  * has requested that the device be enabled, zero if it hasn't, else a
1812  * negative errno code.
1813  *
1814  * Note that the device backing this regulator handle can have multiple
1815  * users, so it might be enabled even if regulator_enable() was never
1816  * called for this particular source.
1817  */
1818 int regulator_is_enabled(struct regulator *regulator)
1819 {
1820         int ret;
1821
1822         mutex_lock(&regulator->rdev->mutex);
1823         ret = _regulator_is_enabled(regulator->rdev);
1824         mutex_unlock(&regulator->rdev->mutex);
1825
1826         return ret;
1827 }
1828 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1829
1830 /**
1831  * regulator_count_voltages - count regulator_list_voltage() selectors
1832  * @regulator: regulator source
1833  *
1834  * Returns number of selectors, or negative errno.  Selectors are
1835  * numbered starting at zero, and typically correspond to bitfields
1836  * in hardware registers.
1837  */
1838 int regulator_count_voltages(struct regulator *regulator)
1839 {
1840         struct regulator_dev    *rdev = regulator->rdev;
1841
1842         return rdev->desc->n_voltages ? : -EINVAL;
1843 }
1844 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1845
1846 /**
1847  * regulator_list_voltage - enumerate supported voltages
1848  * @regulator: regulator source
1849  * @selector: identify voltage to list
1850  * Context: can sleep
1851  *
1852  * Returns a voltage that can be passed to @regulator_set_voltage(),
1853  * zero if this selector code can't be used on this system, or a
1854  * negative errno.
1855  */
1856 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1857 {
1858         struct regulator_dev    *rdev = regulator->rdev;
1859         struct regulator_ops    *ops = rdev->desc->ops;
1860         int                     ret;
1861
1862         if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1863                 return -EINVAL;
1864
1865         mutex_lock(&rdev->mutex);
1866         ret = ops->list_voltage(rdev, selector);
1867         mutex_unlock(&rdev->mutex);
1868
1869         if (ret > 0) {
1870                 if (ret < rdev->constraints->min_uV)
1871                         ret = 0;
1872                 else if (ret > rdev->constraints->max_uV)
1873                         ret = 0;
1874         }
1875
1876         return ret;
1877 }
1878 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1879
1880 /**
1881  * regulator_is_supported_voltage - check if a voltage range can be supported
1882  *
1883  * @regulator: Regulator to check.
1884  * @min_uV: Minimum required voltage in uV.
1885  * @max_uV: Maximum required voltage in uV.
1886  *
1887  * Returns a boolean or a negative error code.
1888  */
1889 int regulator_is_supported_voltage(struct regulator *regulator,
1890                                    int min_uV, int max_uV)
1891 {
1892         int i, voltages, ret;
1893
1894         ret = regulator_count_voltages(regulator);
1895         if (ret < 0)
1896                 return ret;
1897         voltages = ret;
1898
1899         for (i = 0; i < voltages; i++) {
1900                 ret = regulator_list_voltage(regulator, i);
1901
1902                 if (ret >= min_uV && ret <= max_uV)
1903                         return 1;
1904         }
1905
1906         return 0;
1907 }
1908 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
1909
1910 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1911                                      int min_uV, int max_uV)
1912 {
1913         int ret;
1914         int delay = 0;
1915         unsigned int selector;
1916
1917         trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1918
1919         min_uV += rdev->constraints->uV_offset;
1920         max_uV += rdev->constraints->uV_offset;
1921
1922         if (_regulator_is_enabled(rdev))
1923                 _notifier_call_chain(rdev, REGULATOR_EVENT_OUT_PRECHANGE,
1924                                      NULL);
1925
1926         if (rdev->desc->ops->set_voltage) {
1927                 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1928                                                    &selector);
1929
1930                 if (rdev->desc->ops->list_voltage)
1931                         selector = rdev->desc->ops->list_voltage(rdev,
1932                                                                  selector);
1933                 else
1934                         selector = -1;
1935         } else if (rdev->desc->ops->set_voltage_sel) {
1936                 int best_val = INT_MAX;
1937                 int i;
1938
1939                 selector = 0;
1940
1941                 /* Find the smallest voltage that falls within the specified
1942                  * range.
1943                  */
1944                 for (i = 0; i < rdev->desc->n_voltages; i++) {
1945                         ret = rdev->desc->ops->list_voltage(rdev, i);
1946                         if (ret < 0)
1947                                 continue;
1948
1949                         if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1950                                 best_val = ret;
1951                                 selector = i;
1952                         }
1953                 }
1954
1955                 /*
1956                  * If we can't obtain the old selector there is not enough
1957                  * info to call set_voltage_time_sel().
1958                  */
1959                 if (rdev->desc->ops->set_voltage_time_sel &&
1960                     rdev->desc->ops->get_voltage_sel) {
1961                         unsigned int old_selector = 0;
1962
1963                         ret = rdev->desc->ops->get_voltage_sel(rdev);
1964                         if (ret < 0)
1965                                 return ret;
1966                         old_selector = ret;
1967                         ret = rdev->desc->ops->set_voltage_time_sel(rdev,
1968                                                 old_selector, selector);
1969                         if (ret < 0)
1970                                 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
1971                         else
1972                                 delay = ret;
1973                 }
1974
1975                 if (best_val != INT_MAX) {
1976                         ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1977                         selector = best_val;
1978                 } else {
1979                         ret = -EINVAL;
1980                 }
1981         } else {
1982                 ret = -EINVAL;
1983         }
1984
1985         /* Insert any necessary delays */
1986         if (delay >= 1000) {
1987                 mdelay(delay / 1000);
1988                 udelay(delay % 1000);
1989         } else if (delay) {
1990                 udelay(delay);
1991         }
1992
1993         if (ret == 0)
1994                 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1995                                      NULL);
1996
1997         if (_regulator_is_enabled(rdev))
1998                 _notifier_call_chain(rdev, REGULATOR_EVENT_OUT_POSTCHANGE,
1999                                      NULL);
2000
2001         trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
2002
2003         return ret;
2004 }
2005
2006 /**
2007  * regulator_set_voltage - set regulator output voltage
2008  * @regulator: regulator source
2009  * @min_uV: Minimum required voltage in uV
2010  * @max_uV: Maximum acceptable voltage in uV
2011  *
2012  * Sets a voltage regulator to the desired output voltage. This can be set
2013  * during any regulator state. IOW, regulator can be disabled or enabled.
2014  *
2015  * If the regulator is enabled then the voltage will change to the new value
2016  * immediately otherwise if the regulator is disabled the regulator will
2017  * output at the new voltage when enabled.
2018  *
2019  * NOTE: If the regulator is shared between several devices then the lowest
2020  * request voltage that meets the system constraints will be used.
2021  * Regulator system constraints must be set for this regulator before
2022  * calling this function otherwise this call will fail.
2023  */
2024 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2025 {
2026         struct regulator_dev *rdev = regulator->rdev;
2027         int ret = 0;
2028
2029         mutex_lock(&rdev->mutex);
2030
2031         /* If we're setting the same range as last time the change
2032          * should be a noop (some cpufreq implementations use the same
2033          * voltage for multiple frequencies, for example).
2034          */
2035         if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2036                 goto out;
2037
2038         /* sanity check */
2039         if (!rdev->desc->ops->set_voltage &&
2040             !rdev->desc->ops->set_voltage_sel) {
2041                 ret = -EINVAL;
2042                 goto out;
2043         }
2044
2045         /* constraints check */
2046         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2047         if (ret < 0)
2048                 goto out;
2049         regulator->min_uV = min_uV;
2050         regulator->max_uV = max_uV;
2051
2052         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2053         if (ret < 0)
2054                 goto out;
2055
2056         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2057
2058 out:
2059         mutex_unlock(&rdev->mutex);
2060         return ret;
2061 }
2062 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2063
2064 /**
2065  * regulator_set_voltage_time - get raise/fall time
2066  * @regulator: regulator source
2067  * @old_uV: starting voltage in microvolts
2068  * @new_uV: target voltage in microvolts
2069  *
2070  * Provided with the starting and ending voltage, this function attempts to
2071  * calculate the time in microseconds required to rise or fall to this new
2072  * voltage.
2073  */
2074 int regulator_set_voltage_time(struct regulator *regulator,
2075                                int old_uV, int new_uV)
2076 {
2077         struct regulator_dev    *rdev = regulator->rdev;
2078         struct regulator_ops    *ops = rdev->desc->ops;
2079         int old_sel = -1;
2080         int new_sel = -1;
2081         int voltage;
2082         int i;
2083
2084         /* Currently requires operations to do this */
2085         if (!ops->list_voltage || !ops->set_voltage_time_sel
2086             || !rdev->desc->n_voltages)
2087                 return -EINVAL;
2088
2089         for (i = 0; i < rdev->desc->n_voltages; i++) {
2090                 /* We only look for exact voltage matches here */
2091                 voltage = regulator_list_voltage(regulator, i);
2092                 if (voltage < 0)
2093                         return -EINVAL;
2094                 if (voltage == 0)
2095                         continue;
2096                 if (voltage == old_uV)
2097                         old_sel = i;
2098                 if (voltage == new_uV)
2099                         new_sel = i;
2100         }
2101
2102         if (old_sel < 0 || new_sel < 0)
2103                 return -EINVAL;
2104
2105         return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2106 }
2107 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2108
2109 /**
2110  * regulator_sync_voltage - re-apply last regulator output voltage
2111  * @regulator: regulator source
2112  *
2113  * Re-apply the last configured voltage.  This is intended to be used
2114  * where some external control source the consumer is cooperating with
2115  * has caused the configured voltage to change.
2116  */
2117 int regulator_sync_voltage(struct regulator *regulator)
2118 {
2119         struct regulator_dev *rdev = regulator->rdev;
2120         int ret, min_uV, max_uV;
2121
2122         mutex_lock(&rdev->mutex);
2123
2124         if (!rdev->desc->ops->set_voltage &&
2125             !rdev->desc->ops->set_voltage_sel) {
2126                 ret = -EINVAL;
2127                 goto out;
2128         }
2129
2130         /* This is only going to work if we've had a voltage configured. */
2131         if (!regulator->min_uV && !regulator->max_uV) {
2132                 ret = -EINVAL;
2133                 goto out;
2134         }
2135
2136         min_uV = regulator->min_uV;
2137         max_uV = regulator->max_uV;
2138
2139         /* This should be a paranoia check... */
2140         ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2141         if (ret < 0)
2142                 goto out;
2143
2144         ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2145         if (ret < 0)
2146                 goto out;
2147
2148         ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2149
2150 out:
2151         mutex_unlock(&rdev->mutex);
2152         return ret;
2153 }
2154 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2155
2156 static int _regulator_get_voltage(struct regulator_dev *rdev)
2157 {
2158         int sel, ret;
2159
2160         if (rdev->desc->ops->get_voltage_sel) {
2161                 sel = rdev->desc->ops->get_voltage_sel(rdev);
2162                 if (sel < 0)
2163                         return sel;
2164                 ret = rdev->desc->ops->list_voltage(rdev, sel);
2165         } else if (rdev->desc->ops->get_voltage) {
2166                 ret = rdev->desc->ops->get_voltage(rdev);
2167         } else {
2168                 return -EINVAL;
2169         }
2170
2171         if (ret < 0)
2172                 return ret;
2173         return ret - rdev->constraints->uV_offset;
2174 }
2175
2176 /**
2177  * regulator_get_voltage - get regulator output voltage
2178  * @regulator: regulator source
2179  *
2180  * This returns the current regulator voltage in uV.
2181  *
2182  * NOTE: If the regulator is disabled it will return the voltage value. This
2183  * function should not be used to determine regulator state.
2184  */
2185 int regulator_get_voltage(struct regulator *regulator)
2186 {
2187         int ret;
2188
2189         mutex_lock(&regulator->rdev->mutex);
2190
2191         ret = _regulator_get_voltage(regulator->rdev);
2192
2193         mutex_unlock(&regulator->rdev->mutex);
2194
2195         return ret;
2196 }
2197 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2198
2199 /**
2200  * regulator_set_current_limit - set regulator output current limit
2201  * @regulator: regulator source
2202  * @min_uA: Minimuum supported current in uA
2203  * @max_uA: Maximum supported current in uA
2204  *
2205  * Sets current sink to the desired output current. This can be set during
2206  * any regulator state. IOW, regulator can be disabled or enabled.
2207  *
2208  * If the regulator is enabled then the current will change to the new value
2209  * immediately otherwise if the regulator is disabled the regulator will
2210  * output at the new current when enabled.
2211  *
2212  * NOTE: Regulator system constraints must be set for this regulator before
2213  * calling this function otherwise this call will fail.
2214  */
2215 int regulator_set_current_limit(struct regulator *regulator,
2216                                int min_uA, int max_uA)
2217 {
2218         struct regulator_dev *rdev = regulator->rdev;
2219         int ret;
2220
2221         mutex_lock(&rdev->mutex);
2222
2223         /* sanity check */
2224         if (!rdev->desc->ops->set_current_limit) {
2225                 ret = -EINVAL;
2226                 goto out;
2227         }
2228
2229         /* constraints check */
2230         ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2231         if (ret < 0)
2232                 goto out;
2233
2234         ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2235 out:
2236         mutex_unlock(&rdev->mutex);
2237         return ret;
2238 }
2239 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2240
2241 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2242 {
2243         int ret;
2244
2245         mutex_lock(&rdev->mutex);
2246
2247         /* sanity check */
2248         if (!rdev->desc->ops->get_current_limit) {
2249                 ret = -EINVAL;
2250                 goto out;
2251         }
2252
2253         ret = rdev->desc->ops->get_current_limit(rdev);
2254 out:
2255         mutex_unlock(&rdev->mutex);
2256         return ret;
2257 }
2258
2259 /**
2260  * regulator_get_current_limit - get regulator output current
2261  * @regulator: regulator source
2262  *
2263  * This returns the current supplied by the specified current sink in uA.
2264  *
2265  * NOTE: If the regulator is disabled it will return the current value. This
2266  * function should not be used to determine regulator state.
2267  */
2268 int regulator_get_current_limit(struct regulator *regulator)
2269 {
2270         return _regulator_get_current_limit(regulator->rdev);
2271 }
2272 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2273
2274 /**
2275  * regulator_set_mode - set regulator operating mode
2276  * @regulator: regulator source
2277  * @mode: operating mode - one of the REGULATOR_MODE constants
2278  *
2279  * Set regulator operating mode to increase regulator efficiency or improve
2280  * regulation performance.
2281  *
2282  * NOTE: Regulator system constraints must be set for this regulator before
2283  * calling this function otherwise this call will fail.
2284  */
2285 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2286 {
2287         struct regulator_dev *rdev = regulator->rdev;
2288         int ret;
2289         int regulator_curr_mode;
2290
2291         mutex_lock(&rdev->mutex);
2292
2293         /* sanity check */
2294         if (!rdev->desc->ops->set_mode) {
2295                 ret = -EINVAL;
2296                 goto out;
2297         }
2298
2299         /* return if the same mode is requested */
2300         if (rdev->desc->ops->get_mode) {
2301                 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2302                 if (regulator_curr_mode == mode) {
2303                         ret = 0;
2304                         goto out;
2305                 }
2306         }
2307
2308         /* constraints check */
2309         ret = regulator_mode_constrain(rdev, &mode);
2310         if (ret < 0)
2311                 goto out;
2312
2313         ret = rdev->desc->ops->set_mode(rdev, mode);
2314 out:
2315         mutex_unlock(&rdev->mutex);
2316         return ret;
2317 }
2318 EXPORT_SYMBOL_GPL(regulator_set_mode);
2319
2320 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2321 {
2322         int ret;
2323
2324         mutex_lock(&rdev->mutex);
2325
2326         /* sanity check */
2327         if (!rdev->desc->ops->get_mode) {
2328                 ret = -EINVAL;
2329                 goto out;
2330         }
2331
2332         ret = rdev->desc->ops->get_mode(rdev);
2333 out:
2334         mutex_unlock(&rdev->mutex);
2335         return ret;
2336 }
2337
2338 /**
2339  * regulator_get_mode - get regulator operating mode
2340  * @regulator: regulator source
2341  *
2342  * Get the current regulator operating mode.
2343  */
2344 unsigned int regulator_get_mode(struct regulator *regulator)
2345 {
2346         return _regulator_get_mode(regulator->rdev);
2347 }
2348 EXPORT_SYMBOL_GPL(regulator_get_mode);
2349
2350 /**
2351  * regulator_set_optimum_mode - set regulator optimum operating mode
2352  * @regulator: regulator source
2353  * @uA_load: load current
2354  *
2355  * Notifies the regulator core of a new device load. This is then used by
2356  * DRMS (if enabled by constraints) to set the most efficient regulator
2357  * operating mode for the new regulator loading.
2358  *
2359  * Consumer devices notify their supply regulator of the maximum power
2360  * they will require (can be taken from device datasheet in the power
2361  * consumption tables) when they change operational status and hence power
2362  * state. Examples of operational state changes that can affect power
2363  * consumption are :-
2364  *
2365  *    o Device is opened / closed.
2366  *    o Device I/O is about to begin or has just finished.
2367  *    o Device is idling in between work.
2368  *
2369  * This information is also exported via sysfs to userspace.
2370  *
2371  * DRMS will sum the total requested load on the regulator and change
2372  * to the most efficient operating mode if platform constraints allow.
2373  *
2374  * Returns the new regulator mode or error.
2375  */
2376 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2377 {
2378         struct regulator_dev *rdev = regulator->rdev;
2379         struct regulator *consumer;
2380         int ret, output_uV, input_uV, total_uA_load = 0;
2381         unsigned int mode;
2382
2383         mutex_lock(&rdev->mutex);
2384
2385         /*
2386          * first check to see if we can set modes at all, otherwise just
2387          * tell the consumer everything is OK.
2388          */
2389         regulator->uA_load = uA_load;
2390         ret = regulator_check_drms(rdev);
2391         if (ret < 0) {
2392                 ret = 0;
2393                 goto out;
2394         }
2395
2396         if (!rdev->desc->ops->get_optimum_mode)
2397                 goto out;
2398
2399         /*
2400          * we can actually do this so any errors are indicators of
2401          * potential real failure.
2402          */
2403         ret = -EINVAL;
2404
2405         /* get output voltage */
2406         output_uV = _regulator_get_voltage(rdev);
2407         if (output_uV <= 0) {
2408                 rdev_err(rdev, "invalid output voltage found\n");
2409                 goto out;
2410         }
2411
2412         /* get input voltage */
2413         input_uV = 0;
2414         if (rdev->supply)
2415                 input_uV = regulator_get_voltage(rdev->supply);
2416         if (input_uV <= 0)
2417                 input_uV = rdev->constraints->input_uV;
2418         if (input_uV <= 0) {
2419                 rdev_err(rdev, "invalid input voltage found\n");
2420                 goto out;
2421         }
2422
2423         /* calc total requested load for this regulator */
2424         list_for_each_entry(consumer, &rdev->consumer_list, list)
2425                 total_uA_load += consumer->uA_load;
2426
2427         mode = rdev->desc->ops->get_optimum_mode(rdev,
2428                                                  input_uV, output_uV,
2429                                                  total_uA_load);
2430         ret = regulator_mode_constrain(rdev, &mode);
2431         if (ret < 0) {
2432                 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2433                          total_uA_load, input_uV, output_uV);
2434                 goto out;
2435         }
2436
2437         ret = rdev->desc->ops->set_mode(rdev, mode);
2438         if (ret < 0) {
2439                 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2440                 goto out;
2441         }
2442         ret = mode;
2443 out:
2444         mutex_unlock(&rdev->mutex);
2445         return ret;
2446 }
2447 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2448
2449 /**
2450  * regulator_register_notifier - register regulator event notifier
2451  * @regulator: regulator source
2452  * @nb: notifier block
2453  *
2454  * Register notifier block to receive regulator events.
2455  */
2456 int regulator_register_notifier(struct regulator *regulator,
2457                               struct notifier_block *nb)
2458 {
2459         return blocking_notifier_chain_register(&regulator->rdev->notifier,
2460                                                 nb);
2461 }
2462 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2463
2464 /**
2465  * regulator_unregister_notifier - unregister regulator event notifier
2466  * @regulator: regulator source
2467  * @nb: notifier block
2468  *
2469  * Unregister regulator event notifier block.
2470  */
2471 int regulator_unregister_notifier(struct regulator *regulator,
2472                                 struct notifier_block *nb)
2473 {
2474         return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2475                                                   nb);
2476 }
2477 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2478
2479 /* notify regulator consumers and downstream regulator consumers.
2480  * Note mutex must be held by caller.
2481  */
2482 static void _notifier_call_chain(struct regulator_dev *rdev,
2483                                   unsigned long event, void *data)
2484 {
2485         /* call rdev chain first */
2486         blocking_notifier_call_chain(&rdev->notifier, event, NULL);
2487 }
2488
2489 /**
2490  * regulator_bulk_get - get multiple regulator consumers
2491  *
2492  * @dev:           Device to supply
2493  * @num_consumers: Number of consumers to register
2494  * @consumers:     Configuration of consumers; clients are stored here.
2495  *
2496  * @return 0 on success, an errno on failure.
2497  *
2498  * This helper function allows drivers to get several regulator
2499  * consumers in one operation.  If any of the regulators cannot be
2500  * acquired then any regulators that were allocated will be freed
2501  * before returning to the caller.
2502  */
2503 int regulator_bulk_get(struct device *dev, int num_consumers,
2504                        struct regulator_bulk_data *consumers)
2505 {
2506         int i;
2507         int ret;
2508
2509         for (i = 0; i < num_consumers; i++)
2510                 consumers[i].consumer = NULL;
2511
2512         for (i = 0; i < num_consumers; i++) {
2513                 consumers[i].consumer = regulator_get(dev,
2514                                                       consumers[i].supply);
2515                 if (IS_ERR(consumers[i].consumer)) {
2516                         ret = PTR_ERR(consumers[i].consumer);
2517                         dev_err(dev, "Failed to get supply '%s': %d\n",
2518                                 consumers[i].supply, ret);
2519                         consumers[i].consumer = NULL;
2520                         goto err;
2521                 }
2522         }
2523
2524         return 0;
2525
2526 err:
2527         while (--i >= 0)
2528                 regulator_put(consumers[i].consumer);
2529
2530         return ret;
2531 }
2532 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2533
2534 /**
2535  * devm_regulator_bulk_get - managed get multiple regulator consumers
2536  *
2537  * @dev:           Device to supply
2538  * @num_consumers: Number of consumers to register
2539  * @consumers:     Configuration of consumers; clients are stored here.
2540  *
2541  * @return 0 on success, an errno on failure.
2542  *
2543  * This helper function allows drivers to get several regulator
2544  * consumers in one operation with management, the regulators will
2545  * automatically be freed when the device is unbound.  If any of the
2546  * regulators cannot be acquired then any regulators that were
2547  * allocated will be freed before returning to the caller.
2548  */
2549 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2550                             struct regulator_bulk_data *consumers)
2551 {
2552         int i;
2553         int ret;
2554
2555         for (i = 0; i < num_consumers; i++)
2556                 consumers[i].consumer = NULL;
2557
2558         for (i = 0; i < num_consumers; i++) {
2559                 consumers[i].consumer = devm_regulator_get(dev,
2560                                                            consumers[i].supply);
2561                 if (IS_ERR(consumers[i].consumer)) {
2562                         ret = PTR_ERR(consumers[i].consumer);
2563                         dev_err(dev, "Failed to get supply '%s': %d\n",
2564                                 consumers[i].supply, ret);
2565                         consumers[i].consumer = NULL;
2566                         goto err;
2567                 }
2568         }
2569
2570         return 0;
2571
2572 err:
2573         for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2574                 devm_regulator_put(consumers[i].consumer);
2575
2576         return ret;
2577 }
2578 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2579
2580 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2581 {
2582         struct regulator_bulk_data *bulk = data;
2583
2584         bulk->ret = regulator_enable(bulk->consumer);
2585 }
2586
2587 /**
2588  * regulator_bulk_enable - enable multiple regulator consumers
2589  *
2590  * @num_consumers: Number of consumers
2591  * @consumers:     Consumer data; clients are stored here.
2592  * @return         0 on success, an errno on failure
2593  *
2594  * This convenience API allows consumers to enable multiple regulator
2595  * clients in a single API call.  If any consumers cannot be enabled
2596  * then any others that were enabled will be disabled again prior to
2597  * return.
2598  */
2599 int regulator_bulk_enable(int num_consumers,
2600                           struct regulator_bulk_data *consumers)
2601 {
2602         LIST_HEAD(async_domain);
2603         int i;
2604         int ret = 0;
2605
2606         for (i = 0; i < num_consumers; i++)
2607                 async_schedule_domain(regulator_bulk_enable_async,
2608                                       &consumers[i], &async_domain);
2609
2610         async_synchronize_full_domain(&async_domain);
2611
2612         /* If any consumer failed we need to unwind any that succeeded */
2613         for (i = 0; i < num_consumers; i++) {
2614                 if (consumers[i].ret != 0) {
2615                         ret = consumers[i].ret;
2616                         goto err;
2617                 }
2618         }
2619
2620         return 0;
2621
2622 err:
2623         pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2624         while (--i >= 0)
2625                 regulator_disable(consumers[i].consumer);
2626
2627         return ret;
2628 }
2629 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2630
2631 /**
2632  * regulator_bulk_disable - disable multiple regulator consumers
2633  *
2634  * @num_consumers: Number of consumers
2635  * @consumers:     Consumer data; clients are stored here.
2636  * @return         0 on success, an errno on failure
2637  *
2638  * This convenience API allows consumers to disable multiple regulator
2639  * clients in a single API call.  If any consumers cannot be disabled
2640  * then any others that were disabled will be enabled again prior to
2641  * return.
2642  */
2643 int regulator_bulk_disable(int num_consumers,
2644                            struct regulator_bulk_data *consumers)
2645 {
2646         int i;
2647         int ret;
2648
2649         for (i = num_consumers - 1; i >= 0; --i) {
2650                 ret = regulator_disable(consumers[i].consumer);
2651                 if (ret != 0)
2652                         goto err;
2653         }
2654
2655         return 0;
2656
2657 err:
2658         pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2659         for (++i; i < num_consumers; ++i)
2660                 regulator_enable(consumers[i].consumer);
2661
2662         return ret;
2663 }
2664 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2665
2666 /**
2667  * regulator_bulk_force_disable - force disable multiple regulator consumers
2668  *
2669  * @num_consumers: Number of consumers
2670  * @consumers:     Consumer data; clients are stored here.
2671  * @return         0 on success, an errno on failure
2672  *
2673  * This convenience API allows consumers to forcibly disable multiple regulator
2674  * clients in a single API call.
2675  * NOTE: This should be used for situations when device damage will
2676  * likely occur if the regulators are not disabled (e.g. over temp).
2677  * Although regulator_force_disable function call for some consumers can
2678  * return error numbers, the function is called for all consumers.
2679  */
2680 int regulator_bulk_force_disable(int num_consumers,
2681                            struct regulator_bulk_data *consumers)
2682 {
2683         int i;
2684         int ret;
2685
2686         for (i = 0; i < num_consumers; i++)
2687                 consumers[i].ret =
2688                             regulator_force_disable(consumers[i].consumer);
2689
2690         for (i = 0; i < num_consumers; i++) {
2691                 if (consumers[i].ret != 0) {
2692                         ret = consumers[i].ret;
2693                         goto out;
2694                 }
2695         }
2696
2697         return 0;
2698 out:
2699         return ret;
2700 }
2701 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2702
2703 /**
2704  * regulator_bulk_free - free multiple regulator consumers
2705  *
2706  * @num_consumers: Number of consumers
2707  * @consumers:     Consumer data; clients are stored here.
2708  *
2709  * This convenience API allows consumers to free multiple regulator
2710  * clients in a single API call.
2711  */
2712 void regulator_bulk_free(int num_consumers,
2713                          struct regulator_bulk_data *consumers)
2714 {
2715         int i;
2716
2717         for (i = 0; i < num_consumers; i++) {
2718                 regulator_put(consumers[i].consumer);
2719                 consumers[i].consumer = NULL;
2720         }
2721 }
2722 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2723
2724 /**
2725  * regulator_notifier_call_chain - call regulator event notifier
2726  * @rdev: regulator source
2727  * @event: notifier block
2728  * @data: callback-specific data.
2729  *
2730  * Called by regulator drivers to notify clients a regulator event has
2731  * occurred. We also notify regulator clients downstream.
2732  * Note lock must be held by caller.
2733  */
2734 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2735                                   unsigned long event, void *data)
2736 {
2737         _notifier_call_chain(rdev, event, data);
2738         return NOTIFY_DONE;
2739
2740 }
2741 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2742
2743 /**
2744  * regulator_mode_to_status - convert a regulator mode into a status
2745  *
2746  * @mode: Mode to convert
2747  *
2748  * Convert a regulator mode into a status.
2749  */
2750 int regulator_mode_to_status(unsigned int mode)
2751 {
2752         switch (mode) {
2753         case REGULATOR_MODE_FAST:
2754                 return REGULATOR_STATUS_FAST;
2755         case REGULATOR_MODE_NORMAL:
2756                 return REGULATOR_STATUS_NORMAL;
2757         case REGULATOR_MODE_IDLE:
2758                 return REGULATOR_STATUS_IDLE;
2759         case REGULATOR_STATUS_STANDBY:
2760                 return REGULATOR_STATUS_STANDBY;
2761         default:
2762                 return 0;
2763         }
2764 }
2765 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2766
2767 /*
2768  * To avoid cluttering sysfs (and memory) with useless state, only
2769  * create attributes that can be meaningfully displayed.
2770  */
2771 static int add_regulator_attributes(struct regulator_dev *rdev)
2772 {
2773         struct device           *dev = &rdev->dev;
2774         struct regulator_ops    *ops = rdev->desc->ops;
2775         int                     status = 0;
2776
2777         /* some attributes need specific methods to be displayed */
2778         if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2779             (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
2780                 status = device_create_file(dev, &dev_attr_microvolts);
2781                 if (status < 0)
2782                         return status;
2783         }
2784         if (ops->get_current_limit) {
2785                 status = device_create_file(dev, &dev_attr_microamps);
2786                 if (status < 0)
2787                         return status;
2788         }
2789         if (ops->get_mode) {
2790                 status = device_create_file(dev, &dev_attr_opmode);
2791                 if (status < 0)
2792                         return status;
2793         }
2794         if (ops->is_enabled) {
2795                 status = device_create_file(dev, &dev_attr_state);
2796                 if (status < 0)
2797                         return status;
2798         }
2799         if (ops->get_status) {
2800                 status = device_create_file(dev, &dev_attr_status);
2801                 if (status < 0)
2802                         return status;
2803         }
2804
2805         /* some attributes are type-specific */
2806         if (rdev->desc->type == REGULATOR_CURRENT) {
2807                 status = device_create_file(dev, &dev_attr_requested_microamps);
2808                 if (status < 0)
2809                         return status;
2810         }
2811
2812         /* all the other attributes exist to support constraints;
2813          * don't show them if there are no constraints, or if the
2814          * relevant supporting methods are missing.
2815          */
2816         if (!rdev->constraints)
2817                 return status;
2818
2819         /* constraints need specific supporting methods */
2820         if (ops->set_voltage || ops->set_voltage_sel) {
2821                 status = device_create_file(dev, &dev_attr_min_microvolts);
2822                 if (status < 0)
2823                         return status;
2824                 status = device_create_file(dev, &dev_attr_max_microvolts);
2825                 if (status < 0)
2826                         return status;
2827         }
2828         if (ops->set_current_limit) {
2829                 status = device_create_file(dev, &dev_attr_min_microamps);
2830                 if (status < 0)
2831                         return status;
2832                 status = device_create_file(dev, &dev_attr_max_microamps);
2833                 if (status < 0)
2834                         return status;
2835         }
2836
2837         /* suspend mode constraints need multiple supporting methods */
2838         if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2839                 return status;
2840
2841         status = device_create_file(dev, &dev_attr_suspend_standby_state);
2842         if (status < 0)
2843                 return status;
2844         status = device_create_file(dev, &dev_attr_suspend_mem_state);
2845         if (status < 0)
2846                 return status;
2847         status = device_create_file(dev, &dev_attr_suspend_disk_state);
2848         if (status < 0)
2849                 return status;
2850
2851         if (ops->set_suspend_voltage) {
2852                 status = device_create_file(dev,
2853                                 &dev_attr_suspend_standby_microvolts);
2854                 if (status < 0)
2855                         return status;
2856                 status = device_create_file(dev,
2857                                 &dev_attr_suspend_mem_microvolts);
2858                 if (status < 0)
2859                         return status;
2860                 status = device_create_file(dev,
2861                                 &dev_attr_suspend_disk_microvolts);
2862                 if (status < 0)
2863                         return status;
2864         }
2865
2866         if (ops->set_suspend_mode) {
2867                 status = device_create_file(dev,
2868                                 &dev_attr_suspend_standby_mode);
2869                 if (status < 0)
2870                         return status;
2871                 status = device_create_file(dev,
2872                                 &dev_attr_suspend_mem_mode);
2873                 if (status < 0)
2874                         return status;
2875                 status = device_create_file(dev,
2876                                 &dev_attr_suspend_disk_mode);
2877                 if (status < 0)
2878                         return status;
2879         }
2880
2881         return status;
2882 }
2883
2884 static void rdev_init_debugfs(struct regulator_dev *rdev)
2885 {
2886         rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2887         if (!rdev->debugfs) {
2888                 rdev_warn(rdev, "Failed to create debugfs directory\n");
2889                 return;
2890         }
2891
2892         debugfs_create_u32("use_count", 0444, rdev->debugfs,
2893                            &rdev->use_count);
2894         debugfs_create_u32("open_count", 0444, rdev->debugfs,
2895                            &rdev->open_count);
2896 }
2897
2898 /**
2899  * regulator_register - register regulator
2900  * @regulator_desc: regulator to register
2901  * @dev: struct device for the regulator
2902  * @init_data: platform provided init data, passed through by driver
2903  * @driver_data: private regulator data
2904  * @of_node: OpenFirmware node to parse for device tree bindings (may be
2905  *           NULL).
2906  *
2907  * Called by regulator drivers to register a regulator.
2908  * Returns 0 on success.
2909  */
2910 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2911         struct device *dev, const struct regulator_init_data *init_data,
2912         void *driver_data, struct device_node *of_node)
2913 {
2914         const struct regulation_constraints *constraints = NULL;
2915         static atomic_t regulator_no = ATOMIC_INIT(0);
2916         struct regulator_dev *rdev;
2917         int ret, i;
2918         const char *supply = NULL;
2919
2920         if (regulator_desc == NULL)
2921                 return ERR_PTR(-EINVAL);
2922
2923         if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2924                 return ERR_PTR(-EINVAL);
2925
2926         if (regulator_desc->type != REGULATOR_VOLTAGE &&
2927             regulator_desc->type != REGULATOR_CURRENT)
2928                 return ERR_PTR(-EINVAL);
2929
2930         /* Only one of each should be implemented */
2931         WARN_ON(regulator_desc->ops->get_voltage &&
2932                 regulator_desc->ops->get_voltage_sel);
2933         WARN_ON(regulator_desc->ops->set_voltage &&
2934                 regulator_desc->ops->set_voltage_sel);
2935
2936         /* If we're using selectors we must implement list_voltage. */
2937         if (regulator_desc->ops->get_voltage_sel &&
2938             !regulator_desc->ops->list_voltage) {
2939                 return ERR_PTR(-EINVAL);
2940         }
2941         if (regulator_desc->ops->set_voltage_sel &&
2942             !regulator_desc->ops->list_voltage) {
2943                 return ERR_PTR(-EINVAL);
2944         }
2945
2946         rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2947         if (rdev == NULL)
2948                 return ERR_PTR(-ENOMEM);
2949
2950         mutex_lock(&regulator_list_mutex);
2951
2952         mutex_init(&rdev->mutex);
2953         rdev->reg_data = driver_data;
2954         rdev->owner = regulator_desc->owner;
2955         rdev->desc = regulator_desc;
2956         INIT_LIST_HEAD(&rdev->consumer_list);
2957         INIT_LIST_HEAD(&rdev->list);
2958         BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2959         INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
2960
2961         /* preform any regulator specific init */
2962         if (init_data && init_data->regulator_init) {
2963                 ret = init_data->regulator_init(rdev->reg_data);
2964                 if (ret < 0)
2965                         goto clean;
2966         }
2967
2968         /* register with sysfs */
2969         rdev->dev.class = &regulator_class;
2970         rdev->dev.of_node = of_node;
2971         rdev->dev.parent = dev;
2972         dev_set_name(&rdev->dev, "regulator.%d",
2973                      atomic_inc_return(&regulator_no) - 1);
2974         ret = device_register(&rdev->dev);
2975         if (ret != 0) {
2976                 put_device(&rdev->dev);
2977                 goto clean;
2978         }
2979
2980         dev_set_drvdata(&rdev->dev, rdev);
2981
2982         /* set regulator constraints */
2983         if (init_data)
2984                 constraints = &init_data->constraints;
2985
2986         ret = set_machine_constraints(rdev, constraints);
2987         if (ret < 0)
2988                 goto scrub;
2989
2990         /* add attributes supported by this regulator */
2991         ret = add_regulator_attributes(rdev);
2992         if (ret < 0)
2993                 goto scrub;
2994
2995         if (init_data && init_data->supply_regulator)
2996                 supply = init_data->supply_regulator;
2997         else if (regulator_desc->supply_name)
2998                 supply = regulator_desc->supply_name;
2999
3000         if (supply) {
3001                 struct regulator_dev *r;
3002
3003                 r = regulator_dev_lookup(dev, supply);
3004
3005                 if (!r) {
3006                         dev_err(dev, "Failed to find supply %s\n", supply);
3007                         ret = -EPROBE_DEFER;
3008                         goto scrub;
3009                 }
3010
3011                 ret = set_supply(rdev, r);
3012                 if (ret < 0)
3013                         goto scrub;
3014
3015                 /* Enable supply if rail is enabled */
3016                 if (rdev->desc->ops->is_enabled &&
3017                                 rdev->desc->ops->is_enabled(rdev)) {
3018                         ret = regulator_enable(rdev->supply);
3019                         if (ret < 0)
3020                                 goto scrub;
3021                 }
3022         }
3023
3024         /* add consumers devices */
3025         if (init_data) {
3026                 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3027                         ret = set_consumer_device_supply(rdev,
3028                                 init_data->consumer_supplies[i].dev_name,
3029                                 init_data->consumer_supplies[i].supply);
3030                         if (ret < 0) {
3031                                 dev_err(dev, "Failed to set supply %s\n",
3032                                         init_data->consumer_supplies[i].supply);
3033                                 goto unset_supplies;
3034                         }
3035                 }
3036         }
3037
3038         list_add(&rdev->list, &regulator_list);
3039
3040         rdev_init_debugfs(rdev);
3041 out:
3042         mutex_unlock(&regulator_list_mutex);
3043         return rdev;
3044
3045 unset_supplies:
3046         unset_regulator_supplies(rdev);
3047
3048 scrub:
3049         if (rdev->supply)
3050                 regulator_put(rdev->supply);
3051         kfree(rdev->constraints);
3052         device_unregister(&rdev->dev);
3053         /* device core frees rdev */
3054         rdev = ERR_PTR(ret);
3055         goto out;
3056
3057 clean:
3058         kfree(rdev);
3059         rdev = ERR_PTR(ret);
3060         goto out;
3061 }
3062 EXPORT_SYMBOL_GPL(regulator_register);
3063
3064 /**
3065  * regulator_unregister - unregister regulator
3066  * @rdev: regulator to unregister
3067  *
3068  * Called by regulator drivers to unregister a regulator.
3069  */
3070 void regulator_unregister(struct regulator_dev *rdev)
3071 {
3072         if (rdev == NULL)
3073                 return;
3074
3075         if (rdev->supply)
3076                 regulator_put(rdev->supply);
3077         mutex_lock(&regulator_list_mutex);
3078         debugfs_remove_recursive(rdev->debugfs);
3079         flush_work_sync(&rdev->disable_work.work);
3080         WARN_ON(rdev->open_count);
3081         unset_regulator_supplies(rdev);
3082         list_del(&rdev->list);
3083         kfree(rdev->constraints);
3084         device_unregister(&rdev->dev);
3085         mutex_unlock(&regulator_list_mutex);
3086 }
3087 EXPORT_SYMBOL_GPL(regulator_unregister);
3088
3089 /**
3090  * regulator_suspend_prepare - prepare regulators for system wide suspend
3091  * @state: system suspend state
3092  *
3093  * Configure each regulator with it's suspend operating parameters for state.
3094  * This will usually be called by machine suspend code prior to supending.
3095  */
3096 int regulator_suspend_prepare(suspend_state_t state)
3097 {
3098         struct regulator_dev *rdev;
3099         int ret = 0;
3100
3101         /* ON is handled by regulator active state */
3102         if (state == PM_SUSPEND_ON)
3103                 return -EINVAL;
3104
3105         mutex_lock(&regulator_list_mutex);
3106         list_for_each_entry(rdev, &regulator_list, list) {
3107
3108                 mutex_lock(&rdev->mutex);
3109                 ret = suspend_prepare(rdev, state);
3110                 mutex_unlock(&rdev->mutex);
3111
3112                 if (ret < 0) {
3113                         rdev_err(rdev, "failed to prepare\n");
3114                         goto out;
3115                 }
3116         }
3117 out:
3118         mutex_unlock(&regulator_list_mutex);
3119         return ret;
3120 }
3121 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3122
3123 /**
3124  * regulator_suspend_finish - resume regulators from system wide suspend
3125  *
3126  * Turn on regulators that might be turned off by regulator_suspend_prepare
3127  * and that should be turned on according to the regulators properties.
3128  */
3129 int regulator_suspend_finish(void)
3130 {
3131         struct regulator_dev *rdev;
3132         int ret = 0, error;
3133
3134         mutex_lock(&regulator_list_mutex);
3135         list_for_each_entry(rdev, &regulator_list, list) {
3136                 struct regulator_ops *ops = rdev->desc->ops;
3137
3138                 mutex_lock(&rdev->mutex);
3139                 if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
3140                                 ops->enable) {
3141                         error = ops->enable(rdev);
3142                         if (error)
3143                                 ret = error;
3144                 } else {
3145                         if (!has_full_constraints)
3146                                 goto unlock;
3147                         if (!ops->disable)
3148                                 goto unlock;
3149                         if (ops->is_enabled && !ops->is_enabled(rdev))
3150                                 goto unlock;
3151
3152                         error = ops->disable(rdev);
3153                         if (error)
3154                                 ret = error;
3155                 }
3156 unlock:
3157                 mutex_unlock(&rdev->mutex);
3158         }
3159         mutex_unlock(&regulator_list_mutex);
3160         return ret;
3161 }
3162 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3163
3164 /**
3165  * regulator_has_full_constraints - the system has fully specified constraints
3166  *
3167  * Calling this function will cause the regulator API to disable all
3168  * regulators which have a zero use count and don't have an always_on
3169  * constraint in a late_initcall.
3170  *
3171  * The intention is that this will become the default behaviour in a
3172  * future kernel release so users are encouraged to use this facility
3173  * now.
3174  */
3175 void regulator_has_full_constraints(void)
3176 {
3177         has_full_constraints = 1;
3178 }
3179 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3180
3181 /**
3182  * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3183  *
3184  * Calling this function will cause the regulator API to provide a
3185  * dummy regulator to consumers if no physical regulator is found,
3186  * allowing most consumers to proceed as though a regulator were
3187  * configured.  This allows systems such as those with software
3188  * controllable regulators for the CPU core only to be brought up more
3189  * readily.
3190  */
3191 void regulator_use_dummy_regulator(void)
3192 {
3193         board_wants_dummy_regulator = true;
3194 }
3195 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3196
3197 /**
3198  * rdev_get_drvdata - get rdev regulator driver data
3199  * @rdev: regulator
3200  *
3201  * Get rdev regulator driver private data. This call can be used in the
3202  * regulator driver context.
3203  */
3204 void *rdev_get_drvdata(struct regulator_dev *rdev)
3205 {
3206         return rdev->reg_data;
3207 }
3208 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3209
3210 /**
3211  * regulator_get_drvdata - get regulator driver data
3212  * @regulator: regulator
3213  *
3214  * Get regulator driver private data. This call can be used in the consumer
3215  * driver context when non API regulator specific functions need to be called.
3216  */
3217 void *regulator_get_drvdata(struct regulator *regulator)
3218 {
3219         return regulator->rdev->reg_data;
3220 }
3221 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3222
3223 /**
3224  * regulator_set_drvdata - set regulator driver data
3225  * @regulator: regulator
3226  * @data: data
3227  */
3228 void regulator_set_drvdata(struct regulator *regulator, void *data)
3229 {
3230         regulator->rdev->reg_data = data;
3231 }
3232 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3233
3234 /**
3235  * regulator_get_id - get regulator ID
3236  * @rdev: regulator
3237  */
3238 int rdev_get_id(struct regulator_dev *rdev)
3239 {
3240         return rdev->desc->id;
3241 }
3242 EXPORT_SYMBOL_GPL(rdev_get_id);
3243
3244 struct device *rdev_get_dev(struct regulator_dev *rdev)
3245 {
3246         return &rdev->dev;
3247 }
3248 EXPORT_SYMBOL_GPL(rdev_get_dev);
3249
3250 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3251 {
3252         return reg_init_data->driver_data;
3253 }
3254 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3255
3256 #ifdef CONFIG_DEBUG_FS
3257 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3258                                     size_t count, loff_t *ppos)
3259 {
3260         char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3261         ssize_t len, ret = 0;
3262         struct regulator_map *map;
3263
3264         if (!buf)
3265                 return -ENOMEM;
3266
3267         list_for_each_entry(map, &regulator_map_list, list) {
3268                 len = snprintf(buf + ret, PAGE_SIZE - ret,
3269                                "%s -> %s.%s\n",
3270                                rdev_get_name(map->regulator), map->dev_name,
3271                                map->supply);
3272                 if (len >= 0)
3273                         ret += len;
3274                 if (ret > PAGE_SIZE) {
3275                         ret = PAGE_SIZE;
3276                         break;
3277                 }
3278         }
3279
3280         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3281
3282         kfree(buf);
3283
3284         return ret;
3285 }
3286 #endif
3287
3288 static const struct file_operations supply_map_fops = {
3289 #ifdef CONFIG_DEBUG_FS
3290         .read = supply_map_read_file,
3291         .llseek = default_llseek,
3292 #endif
3293 };
3294
3295 static int __init regulator_init(void)
3296 {
3297         int ret;
3298
3299         ret = class_register(&regulator_class);
3300
3301         debugfs_root = debugfs_create_dir("regulator", NULL);
3302         if (!debugfs_root)
3303                 pr_warn("regulator: Failed to create debugfs directory\n");
3304
3305         debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3306                             &supply_map_fops);
3307
3308         regulator_dummy_init();
3309
3310         return ret;
3311 }
3312
3313 /* init early to allow our consumers to complete system booting */
3314 core_initcall(regulator_init);
3315
3316 static int __init regulator_init_complete(void)
3317 {
3318         struct regulator_dev *rdev;
3319         struct regulator_ops *ops;
3320         struct regulation_constraints *c;
3321         int enabled, ret;
3322
3323         mutex_lock(&regulator_list_mutex);
3324
3325         /* If we have a full configuration then disable any regulators
3326          * which are not in use or always_on.  This will become the
3327          * default behaviour in the future.
3328          */
3329         list_for_each_entry(rdev, &regulator_list, list) {
3330                 ops = rdev->desc->ops;
3331                 c = rdev->constraints;
3332
3333                 if (!ops->disable || (c && c->always_on))
3334                         continue;
3335
3336                 mutex_lock(&rdev->mutex);
3337
3338                 if (rdev->use_count)
3339                         goto unlock;
3340
3341                 /* If we can't read the status assume it's on. */
3342                 if (ops->is_enabled)
3343                         enabled = ops->is_enabled(rdev);
3344                 else
3345                         enabled = 1;
3346
3347                 if (!enabled)
3348                         goto unlock;
3349
3350                 if (has_full_constraints) {
3351                         /* We log since this may kill the system if it
3352                          * goes wrong. */
3353                         rdev_info(rdev, "disabling\n");
3354                         ret = ops->disable(rdev);
3355                         if (ret != 0) {
3356                                 rdev_err(rdev, "couldn't disable: %d\n", ret);
3357                         }
3358                 } else {
3359                         /* The intention is that in future we will
3360                          * assume that full constraints are provided
3361                          * so warn even if we aren't going to do
3362                          * anything here.
3363                          */
3364                         rdev_warn(rdev, "incomplete constraints, leaving on\n");
3365                 }
3366
3367 unlock:
3368                 mutex_unlock(&rdev->mutex);
3369         }
3370
3371         mutex_unlock(&regulator_list_mutex);
3372
3373         return 0;
3374 }
3375
3376 #ifdef CONFIG_DEBUG_FS
3377 static int regulator_syncevent(struct file *file, const char __user *user_buf,
3378                                 size_t count, loff_t *ppos)
3379 {
3380         struct regulator_dev *rdev;
3381         char buffer[40];
3382         int buf_size;
3383
3384         memset(buffer, 0, sizeof(buffer));
3385         buf_size = min(count, (sizeof(buffer)-1));
3386
3387         if (copy_from_user(buffer, user_buf, buf_size))
3388                 return -EFAULT;
3389
3390         if (!strnicmp("all", buffer, 3)) {
3391
3392                 mutex_lock(&regulator_list_mutex);
3393
3394                 list_for_each_entry(rdev, &regulator_list, list) {
3395                         mutex_lock(&rdev->mutex);
3396
3397                         if (_regulator_is_enabled(rdev))
3398                                 trace_regulator_enable(rdev_get_name(rdev));
3399                         else
3400                                 trace_regulator_disable(rdev_get_name(rdev));
3401
3402                         trace_regulator_set_voltage(rdev_get_name(rdev),
3403                                 _regulator_get_voltage(rdev),
3404                                 _regulator_get_voltage(rdev));
3405
3406                         mutex_unlock(&rdev->mutex);
3407                 }
3408         }
3409
3410         mutex_unlock(&regulator_list_mutex);
3411
3412         return count;
3413 }
3414
3415 static const struct file_operations regulator_syncevent_fops = {
3416         .write          = regulator_syncevent,
3417 };
3418
3419 static int __init regulator_init_debugfs(void)
3420 {
3421         debugfs_create_file("syncevent_regulators", S_IWUSR, NULL, NULL,
3422                         &regulator_syncevent_fops);
3423
3424         return 0;
3425 }
3426
3427 late_initcall(regulator_init_debugfs);
3428 #endif
3429
3430 late_initcall(regulator_init_complete);