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