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