Loading...
1/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
6 *
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#define pr_fmt(fmt) "%s: " fmt, __func__
17
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/debugfs.h>
21#include <linux/device.h>
22#include <linux/slab.h>
23#include <linux/async.h>
24#include <linux/err.h>
25#include <linux/mutex.h>
26#include <linux/suspend.h>
27#include <linux/delay.h>
28#include <linux/regulator/consumer.h>
29#include <linux/regulator/driver.h>
30#include <linux/regulator/machine.h>
31
32#define CREATE_TRACE_POINTS
33#include <trace/events/regulator.h>
34
35#include "dummy.h"
36
37#define rdev_crit(rdev, fmt, ...) \
38 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
39#define rdev_err(rdev, fmt, ...) \
40 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
41#define rdev_warn(rdev, fmt, ...) \
42 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43#define rdev_info(rdev, fmt, ...) \
44 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45#define rdev_dbg(rdev, fmt, ...) \
46 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47
48static DEFINE_MUTEX(regulator_list_mutex);
49static LIST_HEAD(regulator_list);
50static LIST_HEAD(regulator_map_list);
51static bool has_full_constraints;
52static bool board_wants_dummy_regulator;
53
54#ifdef CONFIG_DEBUG_FS
55static struct dentry *debugfs_root;
56#endif
57
58/*
59 * struct regulator_map
60 *
61 * Used to provide symbolic supply names to devices.
62 */
63struct regulator_map {
64 struct list_head list;
65 const char *dev_name; /* The dev_name() for the consumer */
66 const char *supply;
67 struct regulator_dev *regulator;
68};
69
70/*
71 * struct regulator
72 *
73 * One for each consumer device.
74 */
75struct regulator {
76 struct device *dev;
77 struct list_head list;
78 int uA_load;
79 int min_uV;
80 int max_uV;
81 char *supply_name;
82 struct device_attribute dev_attr;
83 struct regulator_dev *rdev;
84#ifdef CONFIG_DEBUG_FS
85 struct dentry *debugfs;
86#endif
87};
88
89static int _regulator_is_enabled(struct regulator_dev *rdev);
90static int _regulator_disable(struct regulator_dev *rdev);
91static int _regulator_get_voltage(struct regulator_dev *rdev);
92static int _regulator_get_current_limit(struct regulator_dev *rdev);
93static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
94static void _notifier_call_chain(struct regulator_dev *rdev,
95 unsigned long event, void *data);
96static int _regulator_do_set_voltage(struct regulator_dev *rdev,
97 int min_uV, int max_uV);
98static struct regulator *create_regulator(struct regulator_dev *rdev,
99 struct device *dev,
100 const char *supply_name);
101
102static const char *rdev_get_name(struct regulator_dev *rdev)
103{
104 if (rdev->constraints && rdev->constraints->name)
105 return rdev->constraints->name;
106 else if (rdev->desc->name)
107 return rdev->desc->name;
108 else
109 return "";
110}
111
112/* gets the regulator for a given consumer device */
113static struct regulator *get_device_regulator(struct device *dev)
114{
115 struct regulator *regulator = NULL;
116 struct regulator_dev *rdev;
117
118 mutex_lock(®ulator_list_mutex);
119 list_for_each_entry(rdev, ®ulator_list, list) {
120 mutex_lock(&rdev->mutex);
121 list_for_each_entry(regulator, &rdev->consumer_list, list) {
122 if (regulator->dev == dev) {
123 mutex_unlock(&rdev->mutex);
124 mutex_unlock(®ulator_list_mutex);
125 return regulator;
126 }
127 }
128 mutex_unlock(&rdev->mutex);
129 }
130 mutex_unlock(®ulator_list_mutex);
131 return NULL;
132}
133
134/* Platform voltage constraint check */
135static int regulator_check_voltage(struct regulator_dev *rdev,
136 int *min_uV, int *max_uV)
137{
138 BUG_ON(*min_uV > *max_uV);
139
140 if (!rdev->constraints) {
141 rdev_err(rdev, "no constraints\n");
142 return -ENODEV;
143 }
144 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
145 rdev_err(rdev, "operation not allowed\n");
146 return -EPERM;
147 }
148
149 if (*max_uV > rdev->constraints->max_uV)
150 *max_uV = rdev->constraints->max_uV;
151 if (*min_uV < rdev->constraints->min_uV)
152 *min_uV = rdev->constraints->min_uV;
153
154 if (*min_uV > *max_uV) {
155 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
156 *min_uV, *max_uV);
157 return -EINVAL;
158 }
159
160 return 0;
161}
162
163/* Make sure we select a voltage that suits the needs of all
164 * regulator consumers
165 */
166static int regulator_check_consumers(struct regulator_dev *rdev,
167 int *min_uV, int *max_uV)
168{
169 struct regulator *regulator;
170
171 list_for_each_entry(regulator, &rdev->consumer_list, list) {
172 /*
173 * Assume consumers that didn't say anything are OK
174 * with anything in the constraint range.
175 */
176 if (!regulator->min_uV && !regulator->max_uV)
177 continue;
178
179 if (*max_uV > regulator->max_uV)
180 *max_uV = regulator->max_uV;
181 if (*min_uV < regulator->min_uV)
182 *min_uV = regulator->min_uV;
183 }
184
185 if (*min_uV > *max_uV)
186 return -EINVAL;
187
188 return 0;
189}
190
191/* current constraint check */
192static int regulator_check_current_limit(struct regulator_dev *rdev,
193 int *min_uA, int *max_uA)
194{
195 BUG_ON(*min_uA > *max_uA);
196
197 if (!rdev->constraints) {
198 rdev_err(rdev, "no constraints\n");
199 return -ENODEV;
200 }
201 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
202 rdev_err(rdev, "operation not allowed\n");
203 return -EPERM;
204 }
205
206 if (*max_uA > rdev->constraints->max_uA)
207 *max_uA = rdev->constraints->max_uA;
208 if (*min_uA < rdev->constraints->min_uA)
209 *min_uA = rdev->constraints->min_uA;
210
211 if (*min_uA > *max_uA) {
212 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
213 *min_uA, *max_uA);
214 return -EINVAL;
215 }
216
217 return 0;
218}
219
220/* operating mode constraint check */
221static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
222{
223 switch (*mode) {
224 case REGULATOR_MODE_FAST:
225 case REGULATOR_MODE_NORMAL:
226 case REGULATOR_MODE_IDLE:
227 case REGULATOR_MODE_STANDBY:
228 break;
229 default:
230 rdev_err(rdev, "invalid mode %x specified\n", *mode);
231 return -EINVAL;
232 }
233
234 if (!rdev->constraints) {
235 rdev_err(rdev, "no constraints\n");
236 return -ENODEV;
237 }
238 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
239 rdev_err(rdev, "operation not allowed\n");
240 return -EPERM;
241 }
242
243 /* The modes are bitmasks, the most power hungry modes having
244 * the lowest values. If the requested mode isn't supported
245 * try higher modes. */
246 while (*mode) {
247 if (rdev->constraints->valid_modes_mask & *mode)
248 return 0;
249 *mode /= 2;
250 }
251
252 return -EINVAL;
253}
254
255/* dynamic regulator mode switching constraint check */
256static int regulator_check_drms(struct regulator_dev *rdev)
257{
258 if (!rdev->constraints) {
259 rdev_err(rdev, "no constraints\n");
260 return -ENODEV;
261 }
262 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
263 rdev_err(rdev, "operation not allowed\n");
264 return -EPERM;
265 }
266 return 0;
267}
268
269static ssize_t device_requested_uA_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
271{
272 struct regulator *regulator;
273
274 regulator = get_device_regulator(dev);
275 if (regulator == NULL)
276 return 0;
277
278 return sprintf(buf, "%d\n", regulator->uA_load);
279}
280
281static ssize_t regulator_uV_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
283{
284 struct regulator_dev *rdev = dev_get_drvdata(dev);
285 ssize_t ret;
286
287 mutex_lock(&rdev->mutex);
288 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
289 mutex_unlock(&rdev->mutex);
290
291 return ret;
292}
293static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
294
295static ssize_t regulator_uA_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct regulator_dev *rdev = dev_get_drvdata(dev);
299
300 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
301}
302static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
303
304static ssize_t regulator_name_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
306{
307 struct regulator_dev *rdev = dev_get_drvdata(dev);
308
309 return sprintf(buf, "%s\n", rdev_get_name(rdev));
310}
311
312static ssize_t regulator_print_opmode(char *buf, int mode)
313{
314 switch (mode) {
315 case REGULATOR_MODE_FAST:
316 return sprintf(buf, "fast\n");
317 case REGULATOR_MODE_NORMAL:
318 return sprintf(buf, "normal\n");
319 case REGULATOR_MODE_IDLE:
320 return sprintf(buf, "idle\n");
321 case REGULATOR_MODE_STANDBY:
322 return sprintf(buf, "standby\n");
323 }
324 return sprintf(buf, "unknown\n");
325}
326
327static ssize_t regulator_opmode_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
329{
330 struct regulator_dev *rdev = dev_get_drvdata(dev);
331
332 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
333}
334static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
335
336static ssize_t regulator_print_state(char *buf, int state)
337{
338 if (state > 0)
339 return sprintf(buf, "enabled\n");
340 else if (state == 0)
341 return sprintf(buf, "disabled\n");
342 else
343 return sprintf(buf, "unknown\n");
344}
345
346static ssize_t regulator_state_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
349 struct regulator_dev *rdev = dev_get_drvdata(dev);
350 ssize_t ret;
351
352 mutex_lock(&rdev->mutex);
353 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
354 mutex_unlock(&rdev->mutex);
355
356 return ret;
357}
358static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
359
360static ssize_t regulator_status_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
362{
363 struct regulator_dev *rdev = dev_get_drvdata(dev);
364 int status;
365 char *label;
366
367 status = rdev->desc->ops->get_status(rdev);
368 if (status < 0)
369 return status;
370
371 switch (status) {
372 case REGULATOR_STATUS_OFF:
373 label = "off";
374 break;
375 case REGULATOR_STATUS_ON:
376 label = "on";
377 break;
378 case REGULATOR_STATUS_ERROR:
379 label = "error";
380 break;
381 case REGULATOR_STATUS_FAST:
382 label = "fast";
383 break;
384 case REGULATOR_STATUS_NORMAL:
385 label = "normal";
386 break;
387 case REGULATOR_STATUS_IDLE:
388 label = "idle";
389 break;
390 case REGULATOR_STATUS_STANDBY:
391 label = "standby";
392 break;
393 default:
394 return -ERANGE;
395 }
396
397 return sprintf(buf, "%s\n", label);
398}
399static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
400
401static ssize_t regulator_min_uA_show(struct device *dev,
402 struct device_attribute *attr, char *buf)
403{
404 struct regulator_dev *rdev = dev_get_drvdata(dev);
405
406 if (!rdev->constraints)
407 return sprintf(buf, "constraint not defined\n");
408
409 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
410}
411static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
412
413static ssize_t regulator_max_uA_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
416 struct regulator_dev *rdev = dev_get_drvdata(dev);
417
418 if (!rdev->constraints)
419 return sprintf(buf, "constraint not defined\n");
420
421 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
422}
423static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
424
425static ssize_t regulator_min_uV_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
428 struct regulator_dev *rdev = dev_get_drvdata(dev);
429
430 if (!rdev->constraints)
431 return sprintf(buf, "constraint not defined\n");
432
433 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
434}
435static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
436
437static ssize_t regulator_max_uV_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
439{
440 struct regulator_dev *rdev = dev_get_drvdata(dev);
441
442 if (!rdev->constraints)
443 return sprintf(buf, "constraint not defined\n");
444
445 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
446}
447static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
448
449static ssize_t regulator_total_uA_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
452 struct regulator_dev *rdev = dev_get_drvdata(dev);
453 struct regulator *regulator;
454 int uA = 0;
455
456 mutex_lock(&rdev->mutex);
457 list_for_each_entry(regulator, &rdev->consumer_list, list)
458 uA += regulator->uA_load;
459 mutex_unlock(&rdev->mutex);
460 return sprintf(buf, "%d\n", uA);
461}
462static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
463
464static ssize_t regulator_num_users_show(struct device *dev,
465 struct device_attribute *attr, char *buf)
466{
467 struct regulator_dev *rdev = dev_get_drvdata(dev);
468 return sprintf(buf, "%d\n", rdev->use_count);
469}
470
471static ssize_t regulator_type_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
473{
474 struct regulator_dev *rdev = dev_get_drvdata(dev);
475
476 switch (rdev->desc->type) {
477 case REGULATOR_VOLTAGE:
478 return sprintf(buf, "voltage\n");
479 case REGULATOR_CURRENT:
480 return sprintf(buf, "current\n");
481 }
482 return sprintf(buf, "unknown\n");
483}
484
485static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
487{
488 struct regulator_dev *rdev = dev_get_drvdata(dev);
489
490 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
491}
492static DEVICE_ATTR(suspend_mem_microvolts, 0444,
493 regulator_suspend_mem_uV_show, NULL);
494
495static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
497{
498 struct regulator_dev *rdev = dev_get_drvdata(dev);
499
500 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
501}
502static DEVICE_ATTR(suspend_disk_microvolts, 0444,
503 regulator_suspend_disk_uV_show, NULL);
504
505static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
508 struct regulator_dev *rdev = dev_get_drvdata(dev);
509
510 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
511}
512static DEVICE_ATTR(suspend_standby_microvolts, 0444,
513 regulator_suspend_standby_uV_show, NULL);
514
515static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
517{
518 struct regulator_dev *rdev = dev_get_drvdata(dev);
519
520 return regulator_print_opmode(buf,
521 rdev->constraints->state_mem.mode);
522}
523static DEVICE_ATTR(suspend_mem_mode, 0444,
524 regulator_suspend_mem_mode_show, NULL);
525
526static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
528{
529 struct regulator_dev *rdev = dev_get_drvdata(dev);
530
531 return regulator_print_opmode(buf,
532 rdev->constraints->state_disk.mode);
533}
534static DEVICE_ATTR(suspend_disk_mode, 0444,
535 regulator_suspend_disk_mode_show, NULL);
536
537static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
539{
540 struct regulator_dev *rdev = dev_get_drvdata(dev);
541
542 return regulator_print_opmode(buf,
543 rdev->constraints->state_standby.mode);
544}
545static DEVICE_ATTR(suspend_standby_mode, 0444,
546 regulator_suspend_standby_mode_show, NULL);
547
548static ssize_t regulator_suspend_mem_state_show(struct device *dev,
549 struct device_attribute *attr, char *buf)
550{
551 struct regulator_dev *rdev = dev_get_drvdata(dev);
552
553 return regulator_print_state(buf,
554 rdev->constraints->state_mem.enabled);
555}
556static DEVICE_ATTR(suspend_mem_state, 0444,
557 regulator_suspend_mem_state_show, NULL);
558
559static ssize_t regulator_suspend_disk_state_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
561{
562 struct regulator_dev *rdev = dev_get_drvdata(dev);
563
564 return regulator_print_state(buf,
565 rdev->constraints->state_disk.enabled);
566}
567static DEVICE_ATTR(suspend_disk_state, 0444,
568 regulator_suspend_disk_state_show, NULL);
569
570static ssize_t regulator_suspend_standby_state_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
573 struct regulator_dev *rdev = dev_get_drvdata(dev);
574
575 return regulator_print_state(buf,
576 rdev->constraints->state_standby.enabled);
577}
578static DEVICE_ATTR(suspend_standby_state, 0444,
579 regulator_suspend_standby_state_show, NULL);
580
581
582/*
583 * These are the only attributes are present for all regulators.
584 * Other attributes are a function of regulator functionality.
585 */
586static struct device_attribute regulator_dev_attrs[] = {
587 __ATTR(name, 0444, regulator_name_show, NULL),
588 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
589 __ATTR(type, 0444, regulator_type_show, NULL),
590 __ATTR_NULL,
591};
592
593static void regulator_dev_release(struct device *dev)
594{
595 struct regulator_dev *rdev = dev_get_drvdata(dev);
596 kfree(rdev);
597}
598
599static struct class regulator_class = {
600 .name = "regulator",
601 .dev_release = regulator_dev_release,
602 .dev_attrs = regulator_dev_attrs,
603};
604
605/* Calculate the new optimum regulator operating mode based on the new total
606 * consumer load. All locks held by caller */
607static void drms_uA_update(struct regulator_dev *rdev)
608{
609 struct regulator *sibling;
610 int current_uA = 0, output_uV, input_uV, err;
611 unsigned int mode;
612
613 err = regulator_check_drms(rdev);
614 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
615 (!rdev->desc->ops->get_voltage &&
616 !rdev->desc->ops->get_voltage_sel) ||
617 !rdev->desc->ops->set_mode)
618 return;
619
620 /* get output voltage */
621 output_uV = _regulator_get_voltage(rdev);
622 if (output_uV <= 0)
623 return;
624
625 /* get input voltage */
626 input_uV = 0;
627 if (rdev->supply)
628 input_uV = _regulator_get_voltage(rdev);
629 if (input_uV <= 0)
630 input_uV = rdev->constraints->input_uV;
631 if (input_uV <= 0)
632 return;
633
634 /* calc total requested load */
635 list_for_each_entry(sibling, &rdev->consumer_list, list)
636 current_uA += sibling->uA_load;
637
638 /* now get the optimum mode for our new total regulator load */
639 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
640 output_uV, current_uA);
641
642 /* check the new mode is allowed */
643 err = regulator_mode_constrain(rdev, &mode);
644 if (err == 0)
645 rdev->desc->ops->set_mode(rdev, mode);
646}
647
648static int suspend_set_state(struct regulator_dev *rdev,
649 struct regulator_state *rstate)
650{
651 int ret = 0;
652 bool can_set_state;
653
654 can_set_state = rdev->desc->ops->set_suspend_enable &&
655 rdev->desc->ops->set_suspend_disable;
656
657 /* If we have no suspend mode configration don't set anything;
658 * only warn if the driver actually makes the suspend mode
659 * configurable.
660 */
661 if (!rstate->enabled && !rstate->disabled) {
662 if (can_set_state)
663 rdev_warn(rdev, "No configuration\n");
664 return 0;
665 }
666
667 if (rstate->enabled && rstate->disabled) {
668 rdev_err(rdev, "invalid configuration\n");
669 return -EINVAL;
670 }
671
672 if (!can_set_state) {
673 rdev_err(rdev, "no way to set suspend state\n");
674 return -EINVAL;
675 }
676
677 if (rstate->enabled)
678 ret = rdev->desc->ops->set_suspend_enable(rdev);
679 else
680 ret = rdev->desc->ops->set_suspend_disable(rdev);
681 if (ret < 0) {
682 rdev_err(rdev, "failed to enabled/disable\n");
683 return ret;
684 }
685
686 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
687 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
688 if (ret < 0) {
689 rdev_err(rdev, "failed to set voltage\n");
690 return ret;
691 }
692 }
693
694 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
695 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
696 if (ret < 0) {
697 rdev_err(rdev, "failed to set mode\n");
698 return ret;
699 }
700 }
701 return ret;
702}
703
704/* locks held by caller */
705static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
706{
707 if (!rdev->constraints)
708 return -EINVAL;
709
710 switch (state) {
711 case PM_SUSPEND_STANDBY:
712 return suspend_set_state(rdev,
713 &rdev->constraints->state_standby);
714 case PM_SUSPEND_MEM:
715 return suspend_set_state(rdev,
716 &rdev->constraints->state_mem);
717 case PM_SUSPEND_MAX:
718 return suspend_set_state(rdev,
719 &rdev->constraints->state_disk);
720 default:
721 return -EINVAL;
722 }
723}
724
725static void print_constraints(struct regulator_dev *rdev)
726{
727 struct regulation_constraints *constraints = rdev->constraints;
728 char buf[80] = "";
729 int count = 0;
730 int ret;
731
732 if (constraints->min_uV && constraints->max_uV) {
733 if (constraints->min_uV == constraints->max_uV)
734 count += sprintf(buf + count, "%d mV ",
735 constraints->min_uV / 1000);
736 else
737 count += sprintf(buf + count, "%d <--> %d mV ",
738 constraints->min_uV / 1000,
739 constraints->max_uV / 1000);
740 }
741
742 if (!constraints->min_uV ||
743 constraints->min_uV != constraints->max_uV) {
744 ret = _regulator_get_voltage(rdev);
745 if (ret > 0)
746 count += sprintf(buf + count, "at %d mV ", ret / 1000);
747 }
748
749 if (constraints->uV_offset)
750 count += sprintf(buf, "%dmV offset ",
751 constraints->uV_offset / 1000);
752
753 if (constraints->min_uA && constraints->max_uA) {
754 if (constraints->min_uA == constraints->max_uA)
755 count += sprintf(buf + count, "%d mA ",
756 constraints->min_uA / 1000);
757 else
758 count += sprintf(buf + count, "%d <--> %d mA ",
759 constraints->min_uA / 1000,
760 constraints->max_uA / 1000);
761 }
762
763 if (!constraints->min_uA ||
764 constraints->min_uA != constraints->max_uA) {
765 ret = _regulator_get_current_limit(rdev);
766 if (ret > 0)
767 count += sprintf(buf + count, "at %d mA ", ret / 1000);
768 }
769
770 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
771 count += sprintf(buf + count, "fast ");
772 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
773 count += sprintf(buf + count, "normal ");
774 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
775 count += sprintf(buf + count, "idle ");
776 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
777 count += sprintf(buf + count, "standby");
778
779 rdev_info(rdev, "%s\n", buf);
780}
781
782static int machine_constraints_voltage(struct regulator_dev *rdev,
783 struct regulation_constraints *constraints)
784{
785 struct regulator_ops *ops = rdev->desc->ops;
786 int ret;
787
788 /* do we need to apply the constraint voltage */
789 if (rdev->constraints->apply_uV &&
790 rdev->constraints->min_uV == rdev->constraints->max_uV) {
791 ret = _regulator_do_set_voltage(rdev,
792 rdev->constraints->min_uV,
793 rdev->constraints->max_uV);
794 if (ret < 0) {
795 rdev_err(rdev, "failed to apply %duV constraint\n",
796 rdev->constraints->min_uV);
797 return ret;
798 }
799 }
800
801 /* constrain machine-level voltage specs to fit
802 * the actual range supported by this regulator.
803 */
804 if (ops->list_voltage && rdev->desc->n_voltages) {
805 int count = rdev->desc->n_voltages;
806 int i;
807 int min_uV = INT_MAX;
808 int max_uV = INT_MIN;
809 int cmin = constraints->min_uV;
810 int cmax = constraints->max_uV;
811
812 /* it's safe to autoconfigure fixed-voltage supplies
813 and the constraints are used by list_voltage. */
814 if (count == 1 && !cmin) {
815 cmin = 1;
816 cmax = INT_MAX;
817 constraints->min_uV = cmin;
818 constraints->max_uV = cmax;
819 }
820
821 /* voltage constraints are optional */
822 if ((cmin == 0) && (cmax == 0))
823 return 0;
824
825 /* else require explicit machine-level constraints */
826 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
827 rdev_err(rdev, "invalid voltage constraints\n");
828 return -EINVAL;
829 }
830
831 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
832 for (i = 0; i < count; i++) {
833 int value;
834
835 value = ops->list_voltage(rdev, i);
836 if (value <= 0)
837 continue;
838
839 /* maybe adjust [min_uV..max_uV] */
840 if (value >= cmin && value < min_uV)
841 min_uV = value;
842 if (value <= cmax && value > max_uV)
843 max_uV = value;
844 }
845
846 /* final: [min_uV..max_uV] valid iff constraints valid */
847 if (max_uV < min_uV) {
848 rdev_err(rdev, "unsupportable voltage constraints\n");
849 return -EINVAL;
850 }
851
852 /* use regulator's subset of machine constraints */
853 if (constraints->min_uV < min_uV) {
854 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
855 constraints->min_uV, min_uV);
856 constraints->min_uV = min_uV;
857 }
858 if (constraints->max_uV > max_uV) {
859 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
860 constraints->max_uV, max_uV);
861 constraints->max_uV = max_uV;
862 }
863 }
864
865 return 0;
866}
867
868/**
869 * set_machine_constraints - sets regulator constraints
870 * @rdev: regulator source
871 * @constraints: constraints to apply
872 *
873 * Allows platform initialisation code to define and constrain
874 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
875 * Constraints *must* be set by platform code in order for some
876 * regulator operations to proceed i.e. set_voltage, set_current_limit,
877 * set_mode.
878 */
879static int set_machine_constraints(struct regulator_dev *rdev,
880 const struct regulation_constraints *constraints)
881{
882 int ret = 0;
883 struct regulator_ops *ops = rdev->desc->ops;
884
885 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
886 GFP_KERNEL);
887 if (!rdev->constraints)
888 return -ENOMEM;
889
890 ret = machine_constraints_voltage(rdev, rdev->constraints);
891 if (ret != 0)
892 goto out;
893
894 /* do we need to setup our suspend state */
895 if (constraints->initial_state) {
896 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
897 if (ret < 0) {
898 rdev_err(rdev, "failed to set suspend state\n");
899 goto out;
900 }
901 }
902
903 if (constraints->initial_mode) {
904 if (!ops->set_mode) {
905 rdev_err(rdev, "no set_mode operation\n");
906 ret = -EINVAL;
907 goto out;
908 }
909
910 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
911 if (ret < 0) {
912 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
913 goto out;
914 }
915 }
916
917 /* If the constraints say the regulator should be on at this point
918 * and we have control then make sure it is enabled.
919 */
920 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
921 ops->enable) {
922 ret = ops->enable(rdev);
923 if (ret < 0) {
924 rdev_err(rdev, "failed to enable\n");
925 goto out;
926 }
927 }
928
929 print_constraints(rdev);
930 return 0;
931out:
932 kfree(rdev->constraints);
933 rdev->constraints = NULL;
934 return ret;
935}
936
937/**
938 * set_supply - set regulator supply regulator
939 * @rdev: regulator name
940 * @supply_rdev: supply regulator name
941 *
942 * Called by platform initialisation code to set the supply regulator for this
943 * regulator. This ensures that a regulators supply will also be enabled by the
944 * core if it's child is enabled.
945 */
946static int set_supply(struct regulator_dev *rdev,
947 struct regulator_dev *supply_rdev)
948{
949 int err;
950
951 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
952
953 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
954 if (IS_ERR(rdev->supply)) {
955 err = PTR_ERR(rdev->supply);
956 rdev->supply = NULL;
957 return err;
958 }
959
960 return 0;
961}
962
963/**
964 * set_consumer_device_supply - Bind a regulator to a symbolic supply
965 * @rdev: regulator source
966 * @consumer_dev: device the supply applies to
967 * @consumer_dev_name: dev_name() string for device supply applies to
968 * @supply: symbolic name for supply
969 *
970 * Allows platform initialisation code to map physical regulator
971 * sources to symbolic names for supplies for use by devices. Devices
972 * should use these symbolic names to request regulators, avoiding the
973 * need to provide board-specific regulator names as platform data.
974 *
975 * Only one of consumer_dev and consumer_dev_name may be specified.
976 */
977static int set_consumer_device_supply(struct regulator_dev *rdev,
978 struct device *consumer_dev, const char *consumer_dev_name,
979 const char *supply)
980{
981 struct regulator_map *node;
982 int has_dev;
983
984 if (consumer_dev && consumer_dev_name)
985 return -EINVAL;
986
987 if (!consumer_dev_name && consumer_dev)
988 consumer_dev_name = dev_name(consumer_dev);
989
990 if (supply == NULL)
991 return -EINVAL;
992
993 if (consumer_dev_name != NULL)
994 has_dev = 1;
995 else
996 has_dev = 0;
997
998 list_for_each_entry(node, ®ulator_map_list, list) {
999 if (node->dev_name && consumer_dev_name) {
1000 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1001 continue;
1002 } else if (node->dev_name || consumer_dev_name) {
1003 continue;
1004 }
1005
1006 if (strcmp(node->supply, supply) != 0)
1007 continue;
1008
1009 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
1010 dev_name(&node->regulator->dev),
1011 node->regulator->desc->name,
1012 supply,
1013 dev_name(&rdev->dev), rdev_get_name(rdev));
1014 return -EBUSY;
1015 }
1016
1017 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1018 if (node == NULL)
1019 return -ENOMEM;
1020
1021 node->regulator = rdev;
1022 node->supply = supply;
1023
1024 if (has_dev) {
1025 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1026 if (node->dev_name == NULL) {
1027 kfree(node);
1028 return -ENOMEM;
1029 }
1030 }
1031
1032 list_add(&node->list, ®ulator_map_list);
1033 return 0;
1034}
1035
1036static void unset_regulator_supplies(struct regulator_dev *rdev)
1037{
1038 struct regulator_map *node, *n;
1039
1040 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1041 if (rdev == node->regulator) {
1042 list_del(&node->list);
1043 kfree(node->dev_name);
1044 kfree(node);
1045 }
1046 }
1047}
1048
1049#define REG_STR_SIZE 64
1050
1051static struct regulator *create_regulator(struct regulator_dev *rdev,
1052 struct device *dev,
1053 const char *supply_name)
1054{
1055 struct regulator *regulator;
1056 char buf[REG_STR_SIZE];
1057 int err, size;
1058
1059 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1060 if (regulator == NULL)
1061 return NULL;
1062
1063 mutex_lock(&rdev->mutex);
1064 regulator->rdev = rdev;
1065 list_add(®ulator->list, &rdev->consumer_list);
1066
1067 if (dev) {
1068 /* create a 'requested_microamps_name' sysfs entry */
1069 size = scnprintf(buf, REG_STR_SIZE,
1070 "microamps_requested_%s-%s",
1071 dev_name(dev), supply_name);
1072 if (size >= REG_STR_SIZE)
1073 goto overflow_err;
1074
1075 regulator->dev = dev;
1076 sysfs_attr_init(®ulator->dev_attr.attr);
1077 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1078 if (regulator->dev_attr.attr.name == NULL)
1079 goto attr_name_err;
1080
1081 regulator->dev_attr.attr.mode = 0444;
1082 regulator->dev_attr.show = device_requested_uA_show;
1083 err = device_create_file(dev, ®ulator->dev_attr);
1084 if (err < 0) {
1085 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1086 goto attr_name_err;
1087 }
1088
1089 /* also add a link to the device sysfs entry */
1090 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1091 dev->kobj.name, supply_name);
1092 if (size >= REG_STR_SIZE)
1093 goto attr_err;
1094
1095 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1096 if (regulator->supply_name == NULL)
1097 goto attr_err;
1098
1099 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1100 buf);
1101 if (err) {
1102 rdev_warn(rdev, "could not add device link %s err %d\n",
1103 dev->kobj.name, err);
1104 goto link_name_err;
1105 }
1106 } else {
1107 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1108 if (regulator->supply_name == NULL)
1109 goto attr_err;
1110 }
1111
1112#ifdef CONFIG_DEBUG_FS
1113 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1114 rdev->debugfs);
1115 if (IS_ERR_OR_NULL(regulator->debugfs)) {
1116 rdev_warn(rdev, "Failed to create debugfs directory\n");
1117 regulator->debugfs = NULL;
1118 } else {
1119 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1120 ®ulator->uA_load);
1121 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1122 ®ulator->min_uV);
1123 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1124 ®ulator->max_uV);
1125 }
1126#endif
1127
1128 mutex_unlock(&rdev->mutex);
1129 return regulator;
1130link_name_err:
1131 kfree(regulator->supply_name);
1132attr_err:
1133 device_remove_file(regulator->dev, ®ulator->dev_attr);
1134attr_name_err:
1135 kfree(regulator->dev_attr.attr.name);
1136overflow_err:
1137 list_del(®ulator->list);
1138 kfree(regulator);
1139 mutex_unlock(&rdev->mutex);
1140 return NULL;
1141}
1142
1143static int _regulator_get_enable_time(struct regulator_dev *rdev)
1144{
1145 if (!rdev->desc->ops->enable_time)
1146 return 0;
1147 return rdev->desc->ops->enable_time(rdev);
1148}
1149
1150/* Internal regulator request function */
1151static struct regulator *_regulator_get(struct device *dev, const char *id,
1152 int exclusive)
1153{
1154 struct regulator_dev *rdev;
1155 struct regulator_map *map;
1156 struct regulator *regulator = ERR_PTR(-ENODEV);
1157 const char *devname = NULL;
1158 int ret;
1159
1160 if (id == NULL) {
1161 pr_err("get() with no identifier\n");
1162 return regulator;
1163 }
1164
1165 if (dev)
1166 devname = dev_name(dev);
1167
1168 mutex_lock(®ulator_list_mutex);
1169
1170 list_for_each_entry(map, ®ulator_map_list, list) {
1171 /* If the mapping has a device set up it must match */
1172 if (map->dev_name &&
1173 (!devname || strcmp(map->dev_name, devname)))
1174 continue;
1175
1176 if (strcmp(map->supply, id) == 0) {
1177 rdev = map->regulator;
1178 goto found;
1179 }
1180 }
1181
1182 if (board_wants_dummy_regulator) {
1183 rdev = dummy_regulator_rdev;
1184 goto found;
1185 }
1186
1187#ifdef CONFIG_REGULATOR_DUMMY
1188 if (!devname)
1189 devname = "deviceless";
1190
1191 /* If the board didn't flag that it was fully constrained then
1192 * substitute in a dummy regulator so consumers can continue.
1193 */
1194 if (!has_full_constraints) {
1195 pr_warn("%s supply %s not found, using dummy regulator\n",
1196 devname, id);
1197 rdev = dummy_regulator_rdev;
1198 goto found;
1199 }
1200#endif
1201
1202 mutex_unlock(®ulator_list_mutex);
1203 return regulator;
1204
1205found:
1206 if (rdev->exclusive) {
1207 regulator = ERR_PTR(-EPERM);
1208 goto out;
1209 }
1210
1211 if (exclusive && rdev->open_count) {
1212 regulator = ERR_PTR(-EBUSY);
1213 goto out;
1214 }
1215
1216 if (!try_module_get(rdev->owner))
1217 goto out;
1218
1219 regulator = create_regulator(rdev, dev, id);
1220 if (regulator == NULL) {
1221 regulator = ERR_PTR(-ENOMEM);
1222 module_put(rdev->owner);
1223 }
1224
1225 rdev->open_count++;
1226 if (exclusive) {
1227 rdev->exclusive = 1;
1228
1229 ret = _regulator_is_enabled(rdev);
1230 if (ret > 0)
1231 rdev->use_count = 1;
1232 else
1233 rdev->use_count = 0;
1234 }
1235
1236out:
1237 mutex_unlock(®ulator_list_mutex);
1238
1239 return regulator;
1240}
1241
1242/**
1243 * regulator_get - lookup and obtain a reference to a regulator.
1244 * @dev: device for regulator "consumer"
1245 * @id: Supply name or regulator ID.
1246 *
1247 * Returns a struct regulator corresponding to the regulator producer,
1248 * or IS_ERR() condition containing errno.
1249 *
1250 * Use of supply names configured via regulator_set_device_supply() is
1251 * strongly encouraged. It is recommended that the supply name used
1252 * should match the name used for the supply and/or the relevant
1253 * device pins in the datasheet.
1254 */
1255struct regulator *regulator_get(struct device *dev, const char *id)
1256{
1257 return _regulator_get(dev, id, 0);
1258}
1259EXPORT_SYMBOL_GPL(regulator_get);
1260
1261/**
1262 * regulator_get_exclusive - obtain exclusive access to a regulator.
1263 * @dev: device for regulator "consumer"
1264 * @id: Supply name or regulator ID.
1265 *
1266 * Returns a struct regulator corresponding to the regulator producer,
1267 * or IS_ERR() condition containing errno. Other consumers will be
1268 * unable to obtain this reference is held and the use count for the
1269 * regulator will be initialised to reflect the current state of the
1270 * regulator.
1271 *
1272 * This is intended for use by consumers which cannot tolerate shared
1273 * use of the regulator such as those which need to force the
1274 * regulator off for correct operation of the hardware they are
1275 * controlling.
1276 *
1277 * Use of supply names configured via regulator_set_device_supply() is
1278 * strongly encouraged. It is recommended that the supply name used
1279 * should match the name used for the supply and/or the relevant
1280 * device pins in the datasheet.
1281 */
1282struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1283{
1284 return _regulator_get(dev, id, 1);
1285}
1286EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1287
1288/**
1289 * regulator_put - "free" the regulator source
1290 * @regulator: regulator source
1291 *
1292 * Note: drivers must ensure that all regulator_enable calls made on this
1293 * regulator source are balanced by regulator_disable calls prior to calling
1294 * this function.
1295 */
1296void regulator_put(struct regulator *regulator)
1297{
1298 struct regulator_dev *rdev;
1299
1300 if (regulator == NULL || IS_ERR(regulator))
1301 return;
1302
1303 mutex_lock(®ulator_list_mutex);
1304 rdev = regulator->rdev;
1305
1306#ifdef CONFIG_DEBUG_FS
1307 debugfs_remove_recursive(regulator->debugfs);
1308#endif
1309
1310 /* remove any sysfs entries */
1311 if (regulator->dev) {
1312 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1313 device_remove_file(regulator->dev, ®ulator->dev_attr);
1314 kfree(regulator->dev_attr.attr.name);
1315 }
1316 kfree(regulator->supply_name);
1317 list_del(®ulator->list);
1318 kfree(regulator);
1319
1320 rdev->open_count--;
1321 rdev->exclusive = 0;
1322
1323 module_put(rdev->owner);
1324 mutex_unlock(®ulator_list_mutex);
1325}
1326EXPORT_SYMBOL_GPL(regulator_put);
1327
1328static int _regulator_can_change_status(struct regulator_dev *rdev)
1329{
1330 if (!rdev->constraints)
1331 return 0;
1332
1333 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1334 return 1;
1335 else
1336 return 0;
1337}
1338
1339/* locks held by regulator_enable() */
1340static int _regulator_enable(struct regulator_dev *rdev)
1341{
1342 int ret, delay;
1343
1344 /* check voltage and requested load before enabling */
1345 if (rdev->constraints &&
1346 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1347 drms_uA_update(rdev);
1348
1349 if (rdev->use_count == 0) {
1350 /* The regulator may on if it's not switchable or left on */
1351 ret = _regulator_is_enabled(rdev);
1352 if (ret == -EINVAL || ret == 0) {
1353 if (!_regulator_can_change_status(rdev))
1354 return -EPERM;
1355
1356 if (!rdev->desc->ops->enable)
1357 return -EINVAL;
1358
1359 /* Query before enabling in case configuration
1360 * dependent. */
1361 ret = _regulator_get_enable_time(rdev);
1362 if (ret >= 0) {
1363 delay = ret;
1364 } else {
1365 rdev_warn(rdev, "enable_time() failed: %d\n",
1366 ret);
1367 delay = 0;
1368 }
1369
1370 trace_regulator_enable(rdev_get_name(rdev));
1371
1372 /* Allow the regulator to ramp; it would be useful
1373 * to extend this for bulk operations so that the
1374 * regulators can ramp together. */
1375 ret = rdev->desc->ops->enable(rdev);
1376 if (ret < 0)
1377 return ret;
1378
1379 trace_regulator_enable_delay(rdev_get_name(rdev));
1380
1381 if (delay >= 1000) {
1382 mdelay(delay / 1000);
1383 udelay(delay % 1000);
1384 } else if (delay) {
1385 udelay(delay);
1386 }
1387
1388 trace_regulator_enable_complete(rdev_get_name(rdev));
1389
1390 } else if (ret < 0) {
1391 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1392 return ret;
1393 }
1394 /* Fallthrough on positive return values - already enabled */
1395 }
1396
1397 rdev->use_count++;
1398
1399 return 0;
1400}
1401
1402/**
1403 * regulator_enable - enable regulator output
1404 * @regulator: regulator source
1405 *
1406 * Request that the regulator be enabled with the regulator output at
1407 * the predefined voltage or current value. Calls to regulator_enable()
1408 * must be balanced with calls to regulator_disable().
1409 *
1410 * NOTE: the output value can be set by other drivers, boot loader or may be
1411 * hardwired in the regulator.
1412 */
1413int regulator_enable(struct regulator *regulator)
1414{
1415 struct regulator_dev *rdev = regulator->rdev;
1416 int ret = 0;
1417
1418 if (rdev->supply) {
1419 ret = regulator_enable(rdev->supply);
1420 if (ret != 0)
1421 return ret;
1422 }
1423
1424 mutex_lock(&rdev->mutex);
1425 ret = _regulator_enable(rdev);
1426 mutex_unlock(&rdev->mutex);
1427
1428 if (ret != 0)
1429 regulator_disable(rdev->supply);
1430
1431 return ret;
1432}
1433EXPORT_SYMBOL_GPL(regulator_enable);
1434
1435/* locks held by regulator_disable() */
1436static int _regulator_disable(struct regulator_dev *rdev)
1437{
1438 int ret = 0;
1439
1440 if (WARN(rdev->use_count <= 0,
1441 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1442 return -EIO;
1443
1444 /* are we the last user and permitted to disable ? */
1445 if (rdev->use_count == 1 &&
1446 (rdev->constraints && !rdev->constraints->always_on)) {
1447
1448 /* we are last user */
1449 if (_regulator_can_change_status(rdev) &&
1450 rdev->desc->ops->disable) {
1451 trace_regulator_disable(rdev_get_name(rdev));
1452
1453 ret = rdev->desc->ops->disable(rdev);
1454 if (ret < 0) {
1455 rdev_err(rdev, "failed to disable\n");
1456 return ret;
1457 }
1458
1459 trace_regulator_disable_complete(rdev_get_name(rdev));
1460
1461 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1462 NULL);
1463 }
1464
1465 rdev->use_count = 0;
1466 } else if (rdev->use_count > 1) {
1467
1468 if (rdev->constraints &&
1469 (rdev->constraints->valid_ops_mask &
1470 REGULATOR_CHANGE_DRMS))
1471 drms_uA_update(rdev);
1472
1473 rdev->use_count--;
1474 }
1475
1476 return ret;
1477}
1478
1479/**
1480 * regulator_disable - disable regulator output
1481 * @regulator: regulator source
1482 *
1483 * Disable the regulator output voltage or current. Calls to
1484 * regulator_enable() must be balanced with calls to
1485 * regulator_disable().
1486 *
1487 * NOTE: this will only disable the regulator output if no other consumer
1488 * devices have it enabled, the regulator device supports disabling and
1489 * machine constraints permit this operation.
1490 */
1491int regulator_disable(struct regulator *regulator)
1492{
1493 struct regulator_dev *rdev = regulator->rdev;
1494 int ret = 0;
1495
1496 mutex_lock(&rdev->mutex);
1497 ret = _regulator_disable(rdev);
1498 mutex_unlock(&rdev->mutex);
1499
1500 if (ret == 0 && rdev->supply)
1501 regulator_disable(rdev->supply);
1502
1503 return ret;
1504}
1505EXPORT_SYMBOL_GPL(regulator_disable);
1506
1507/* locks held by regulator_force_disable() */
1508static int _regulator_force_disable(struct regulator_dev *rdev)
1509{
1510 int ret = 0;
1511
1512 /* force disable */
1513 if (rdev->desc->ops->disable) {
1514 /* ah well, who wants to live forever... */
1515 ret = rdev->desc->ops->disable(rdev);
1516 if (ret < 0) {
1517 rdev_err(rdev, "failed to force disable\n");
1518 return ret;
1519 }
1520 /* notify other consumers that power has been forced off */
1521 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1522 REGULATOR_EVENT_DISABLE, NULL);
1523 }
1524
1525 return ret;
1526}
1527
1528/**
1529 * regulator_force_disable - force disable regulator output
1530 * @regulator: regulator source
1531 *
1532 * Forcibly disable the regulator output voltage or current.
1533 * NOTE: this *will* disable the regulator output even if other consumer
1534 * devices have it enabled. This should be used for situations when device
1535 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1536 */
1537int regulator_force_disable(struct regulator *regulator)
1538{
1539 struct regulator_dev *rdev = regulator->rdev;
1540 int ret;
1541
1542 mutex_lock(&rdev->mutex);
1543 regulator->uA_load = 0;
1544 ret = _regulator_force_disable(regulator->rdev);
1545 mutex_unlock(&rdev->mutex);
1546
1547 if (rdev->supply)
1548 while (rdev->open_count--)
1549 regulator_disable(rdev->supply);
1550
1551 return ret;
1552}
1553EXPORT_SYMBOL_GPL(regulator_force_disable);
1554
1555static int _regulator_is_enabled(struct regulator_dev *rdev)
1556{
1557 /* If we don't know then assume that the regulator is always on */
1558 if (!rdev->desc->ops->is_enabled)
1559 return 1;
1560
1561 return rdev->desc->ops->is_enabled(rdev);
1562}
1563
1564/**
1565 * regulator_is_enabled - is the regulator output enabled
1566 * @regulator: regulator source
1567 *
1568 * Returns positive if the regulator driver backing the source/client
1569 * has requested that the device be enabled, zero if it hasn't, else a
1570 * negative errno code.
1571 *
1572 * Note that the device backing this regulator handle can have multiple
1573 * users, so it might be enabled even if regulator_enable() was never
1574 * called for this particular source.
1575 */
1576int regulator_is_enabled(struct regulator *regulator)
1577{
1578 int ret;
1579
1580 mutex_lock(®ulator->rdev->mutex);
1581 ret = _regulator_is_enabled(regulator->rdev);
1582 mutex_unlock(®ulator->rdev->mutex);
1583
1584 return ret;
1585}
1586EXPORT_SYMBOL_GPL(regulator_is_enabled);
1587
1588/**
1589 * regulator_count_voltages - count regulator_list_voltage() selectors
1590 * @regulator: regulator source
1591 *
1592 * Returns number of selectors, or negative errno. Selectors are
1593 * numbered starting at zero, and typically correspond to bitfields
1594 * in hardware registers.
1595 */
1596int regulator_count_voltages(struct regulator *regulator)
1597{
1598 struct regulator_dev *rdev = regulator->rdev;
1599
1600 return rdev->desc->n_voltages ? : -EINVAL;
1601}
1602EXPORT_SYMBOL_GPL(regulator_count_voltages);
1603
1604/**
1605 * regulator_list_voltage - enumerate supported voltages
1606 * @regulator: regulator source
1607 * @selector: identify voltage to list
1608 * Context: can sleep
1609 *
1610 * Returns a voltage that can be passed to @regulator_set_voltage(),
1611 * zero if this selector code can't be used on this system, or a
1612 * negative errno.
1613 */
1614int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1615{
1616 struct regulator_dev *rdev = regulator->rdev;
1617 struct regulator_ops *ops = rdev->desc->ops;
1618 int ret;
1619
1620 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1621 return -EINVAL;
1622
1623 mutex_lock(&rdev->mutex);
1624 ret = ops->list_voltage(rdev, selector);
1625 mutex_unlock(&rdev->mutex);
1626
1627 if (ret > 0) {
1628 if (ret < rdev->constraints->min_uV)
1629 ret = 0;
1630 else if (ret > rdev->constraints->max_uV)
1631 ret = 0;
1632 }
1633
1634 return ret;
1635}
1636EXPORT_SYMBOL_GPL(regulator_list_voltage);
1637
1638/**
1639 * regulator_is_supported_voltage - check if a voltage range can be supported
1640 *
1641 * @regulator: Regulator to check.
1642 * @min_uV: Minimum required voltage in uV.
1643 * @max_uV: Maximum required voltage in uV.
1644 *
1645 * Returns a boolean or a negative error code.
1646 */
1647int regulator_is_supported_voltage(struct regulator *regulator,
1648 int min_uV, int max_uV)
1649{
1650 int i, voltages, ret;
1651
1652 ret = regulator_count_voltages(regulator);
1653 if (ret < 0)
1654 return ret;
1655 voltages = ret;
1656
1657 for (i = 0; i < voltages; i++) {
1658 ret = regulator_list_voltage(regulator, i);
1659
1660 if (ret >= min_uV && ret <= max_uV)
1661 return 1;
1662 }
1663
1664 return 0;
1665}
1666
1667static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1668 int min_uV, int max_uV)
1669{
1670 int ret;
1671 int delay = 0;
1672 unsigned int selector;
1673
1674 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1675
1676 min_uV += rdev->constraints->uV_offset;
1677 max_uV += rdev->constraints->uV_offset;
1678
1679 if (rdev->desc->ops->set_voltage) {
1680 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1681 &selector);
1682
1683 if (rdev->desc->ops->list_voltage)
1684 selector = rdev->desc->ops->list_voltage(rdev,
1685 selector);
1686 else
1687 selector = -1;
1688 } else if (rdev->desc->ops->set_voltage_sel) {
1689 int best_val = INT_MAX;
1690 int i;
1691
1692 selector = 0;
1693
1694 /* Find the smallest voltage that falls within the specified
1695 * range.
1696 */
1697 for (i = 0; i < rdev->desc->n_voltages; i++) {
1698 ret = rdev->desc->ops->list_voltage(rdev, i);
1699 if (ret < 0)
1700 continue;
1701
1702 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1703 best_val = ret;
1704 selector = i;
1705 }
1706 }
1707
1708 /*
1709 * If we can't obtain the old selector there is not enough
1710 * info to call set_voltage_time_sel().
1711 */
1712 if (rdev->desc->ops->set_voltage_time_sel &&
1713 rdev->desc->ops->get_voltage_sel) {
1714 unsigned int old_selector = 0;
1715
1716 ret = rdev->desc->ops->get_voltage_sel(rdev);
1717 if (ret < 0)
1718 return ret;
1719 old_selector = ret;
1720 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1721 old_selector, selector);
1722 }
1723
1724 if (best_val != INT_MAX) {
1725 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1726 selector = best_val;
1727 } else {
1728 ret = -EINVAL;
1729 }
1730 } else {
1731 ret = -EINVAL;
1732 }
1733
1734 /* Insert any necessary delays */
1735 if (delay >= 1000) {
1736 mdelay(delay / 1000);
1737 udelay(delay % 1000);
1738 } else if (delay) {
1739 udelay(delay);
1740 }
1741
1742 if (ret == 0)
1743 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1744 NULL);
1745
1746 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1747
1748 return ret;
1749}
1750
1751/**
1752 * regulator_set_voltage - set regulator output voltage
1753 * @regulator: regulator source
1754 * @min_uV: Minimum required voltage in uV
1755 * @max_uV: Maximum acceptable voltage in uV
1756 *
1757 * Sets a voltage regulator to the desired output voltage. This can be set
1758 * during any regulator state. IOW, regulator can be disabled or enabled.
1759 *
1760 * If the regulator is enabled then the voltage will change to the new value
1761 * immediately otherwise if the regulator is disabled the regulator will
1762 * output at the new voltage when enabled.
1763 *
1764 * NOTE: If the regulator is shared between several devices then the lowest
1765 * request voltage that meets the system constraints will be used.
1766 * Regulator system constraints must be set for this regulator before
1767 * calling this function otherwise this call will fail.
1768 */
1769int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1770{
1771 struct regulator_dev *rdev = regulator->rdev;
1772 int ret = 0;
1773
1774 mutex_lock(&rdev->mutex);
1775
1776 /* If we're setting the same range as last time the change
1777 * should be a noop (some cpufreq implementations use the same
1778 * voltage for multiple frequencies, for example).
1779 */
1780 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1781 goto out;
1782
1783 /* sanity check */
1784 if (!rdev->desc->ops->set_voltage &&
1785 !rdev->desc->ops->set_voltage_sel) {
1786 ret = -EINVAL;
1787 goto out;
1788 }
1789
1790 /* constraints check */
1791 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1792 if (ret < 0)
1793 goto out;
1794 regulator->min_uV = min_uV;
1795 regulator->max_uV = max_uV;
1796
1797 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1798 if (ret < 0)
1799 goto out;
1800
1801 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1802
1803out:
1804 mutex_unlock(&rdev->mutex);
1805 return ret;
1806}
1807EXPORT_SYMBOL_GPL(regulator_set_voltage);
1808
1809/**
1810 * regulator_set_voltage_time - get raise/fall time
1811 * @regulator: regulator source
1812 * @old_uV: starting voltage in microvolts
1813 * @new_uV: target voltage in microvolts
1814 *
1815 * Provided with the starting and ending voltage, this function attempts to
1816 * calculate the time in microseconds required to rise or fall to this new
1817 * voltage.
1818 */
1819int regulator_set_voltage_time(struct regulator *regulator,
1820 int old_uV, int new_uV)
1821{
1822 struct regulator_dev *rdev = regulator->rdev;
1823 struct regulator_ops *ops = rdev->desc->ops;
1824 int old_sel = -1;
1825 int new_sel = -1;
1826 int voltage;
1827 int i;
1828
1829 /* Currently requires operations to do this */
1830 if (!ops->list_voltage || !ops->set_voltage_time_sel
1831 || !rdev->desc->n_voltages)
1832 return -EINVAL;
1833
1834 for (i = 0; i < rdev->desc->n_voltages; i++) {
1835 /* We only look for exact voltage matches here */
1836 voltage = regulator_list_voltage(regulator, i);
1837 if (voltage < 0)
1838 return -EINVAL;
1839 if (voltage == 0)
1840 continue;
1841 if (voltage == old_uV)
1842 old_sel = i;
1843 if (voltage == new_uV)
1844 new_sel = i;
1845 }
1846
1847 if (old_sel < 0 || new_sel < 0)
1848 return -EINVAL;
1849
1850 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1851}
1852EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1853
1854/**
1855 * regulator_sync_voltage - re-apply last regulator output voltage
1856 * @regulator: regulator source
1857 *
1858 * Re-apply the last configured voltage. This is intended to be used
1859 * where some external control source the consumer is cooperating with
1860 * has caused the configured voltage to change.
1861 */
1862int regulator_sync_voltage(struct regulator *regulator)
1863{
1864 struct regulator_dev *rdev = regulator->rdev;
1865 int ret, min_uV, max_uV;
1866
1867 mutex_lock(&rdev->mutex);
1868
1869 if (!rdev->desc->ops->set_voltage &&
1870 !rdev->desc->ops->set_voltage_sel) {
1871 ret = -EINVAL;
1872 goto out;
1873 }
1874
1875 /* This is only going to work if we've had a voltage configured. */
1876 if (!regulator->min_uV && !regulator->max_uV) {
1877 ret = -EINVAL;
1878 goto out;
1879 }
1880
1881 min_uV = regulator->min_uV;
1882 max_uV = regulator->max_uV;
1883
1884 /* This should be a paranoia check... */
1885 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1886 if (ret < 0)
1887 goto out;
1888
1889 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1890 if (ret < 0)
1891 goto out;
1892
1893 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1894
1895out:
1896 mutex_unlock(&rdev->mutex);
1897 return ret;
1898}
1899EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1900
1901static int _regulator_get_voltage(struct regulator_dev *rdev)
1902{
1903 int sel, ret;
1904
1905 if (rdev->desc->ops->get_voltage_sel) {
1906 sel = rdev->desc->ops->get_voltage_sel(rdev);
1907 if (sel < 0)
1908 return sel;
1909 ret = rdev->desc->ops->list_voltage(rdev, sel);
1910 } else if (rdev->desc->ops->get_voltage) {
1911 ret = rdev->desc->ops->get_voltage(rdev);
1912 } else {
1913 return -EINVAL;
1914 }
1915
1916 if (ret < 0)
1917 return ret;
1918 return ret - rdev->constraints->uV_offset;
1919}
1920
1921/**
1922 * regulator_get_voltage - get regulator output voltage
1923 * @regulator: regulator source
1924 *
1925 * This returns the current regulator voltage in uV.
1926 *
1927 * NOTE: If the regulator is disabled it will return the voltage value. This
1928 * function should not be used to determine regulator state.
1929 */
1930int regulator_get_voltage(struct regulator *regulator)
1931{
1932 int ret;
1933
1934 mutex_lock(®ulator->rdev->mutex);
1935
1936 ret = _regulator_get_voltage(regulator->rdev);
1937
1938 mutex_unlock(®ulator->rdev->mutex);
1939
1940 return ret;
1941}
1942EXPORT_SYMBOL_GPL(regulator_get_voltage);
1943
1944/**
1945 * regulator_set_current_limit - set regulator output current limit
1946 * @regulator: regulator source
1947 * @min_uA: Minimuum supported current in uA
1948 * @max_uA: Maximum supported current in uA
1949 *
1950 * Sets current sink to the desired output current. This can be set during
1951 * any regulator state. IOW, regulator can be disabled or enabled.
1952 *
1953 * If the regulator is enabled then the current will change to the new value
1954 * immediately otherwise if the regulator is disabled the regulator will
1955 * output at the new current when enabled.
1956 *
1957 * NOTE: Regulator system constraints must be set for this regulator before
1958 * calling this function otherwise this call will fail.
1959 */
1960int regulator_set_current_limit(struct regulator *regulator,
1961 int min_uA, int max_uA)
1962{
1963 struct regulator_dev *rdev = regulator->rdev;
1964 int ret;
1965
1966 mutex_lock(&rdev->mutex);
1967
1968 /* sanity check */
1969 if (!rdev->desc->ops->set_current_limit) {
1970 ret = -EINVAL;
1971 goto out;
1972 }
1973
1974 /* constraints check */
1975 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1976 if (ret < 0)
1977 goto out;
1978
1979 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1980out:
1981 mutex_unlock(&rdev->mutex);
1982 return ret;
1983}
1984EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1985
1986static int _regulator_get_current_limit(struct regulator_dev *rdev)
1987{
1988 int ret;
1989
1990 mutex_lock(&rdev->mutex);
1991
1992 /* sanity check */
1993 if (!rdev->desc->ops->get_current_limit) {
1994 ret = -EINVAL;
1995 goto out;
1996 }
1997
1998 ret = rdev->desc->ops->get_current_limit(rdev);
1999out:
2000 mutex_unlock(&rdev->mutex);
2001 return ret;
2002}
2003
2004/**
2005 * regulator_get_current_limit - get regulator output current
2006 * @regulator: regulator source
2007 *
2008 * This returns the current supplied by the specified current sink in uA.
2009 *
2010 * NOTE: If the regulator is disabled it will return the current value. This
2011 * function should not be used to determine regulator state.
2012 */
2013int regulator_get_current_limit(struct regulator *regulator)
2014{
2015 return _regulator_get_current_limit(regulator->rdev);
2016}
2017EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2018
2019/**
2020 * regulator_set_mode - set regulator operating mode
2021 * @regulator: regulator source
2022 * @mode: operating mode - one of the REGULATOR_MODE constants
2023 *
2024 * Set regulator operating mode to increase regulator efficiency or improve
2025 * regulation performance.
2026 *
2027 * NOTE: Regulator system constraints must be set for this regulator before
2028 * calling this function otherwise this call will fail.
2029 */
2030int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2031{
2032 struct regulator_dev *rdev = regulator->rdev;
2033 int ret;
2034 int regulator_curr_mode;
2035
2036 mutex_lock(&rdev->mutex);
2037
2038 /* sanity check */
2039 if (!rdev->desc->ops->set_mode) {
2040 ret = -EINVAL;
2041 goto out;
2042 }
2043
2044 /* return if the same mode is requested */
2045 if (rdev->desc->ops->get_mode) {
2046 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2047 if (regulator_curr_mode == mode) {
2048 ret = 0;
2049 goto out;
2050 }
2051 }
2052
2053 /* constraints check */
2054 ret = regulator_mode_constrain(rdev, &mode);
2055 if (ret < 0)
2056 goto out;
2057
2058 ret = rdev->desc->ops->set_mode(rdev, mode);
2059out:
2060 mutex_unlock(&rdev->mutex);
2061 return ret;
2062}
2063EXPORT_SYMBOL_GPL(regulator_set_mode);
2064
2065static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2066{
2067 int ret;
2068
2069 mutex_lock(&rdev->mutex);
2070
2071 /* sanity check */
2072 if (!rdev->desc->ops->get_mode) {
2073 ret = -EINVAL;
2074 goto out;
2075 }
2076
2077 ret = rdev->desc->ops->get_mode(rdev);
2078out:
2079 mutex_unlock(&rdev->mutex);
2080 return ret;
2081}
2082
2083/**
2084 * regulator_get_mode - get regulator operating mode
2085 * @regulator: regulator source
2086 *
2087 * Get the current regulator operating mode.
2088 */
2089unsigned int regulator_get_mode(struct regulator *regulator)
2090{
2091 return _regulator_get_mode(regulator->rdev);
2092}
2093EXPORT_SYMBOL_GPL(regulator_get_mode);
2094
2095/**
2096 * regulator_set_optimum_mode - set regulator optimum operating mode
2097 * @regulator: regulator source
2098 * @uA_load: load current
2099 *
2100 * Notifies the regulator core of a new device load. This is then used by
2101 * DRMS (if enabled by constraints) to set the most efficient regulator
2102 * operating mode for the new regulator loading.
2103 *
2104 * Consumer devices notify their supply regulator of the maximum power
2105 * they will require (can be taken from device datasheet in the power
2106 * consumption tables) when they change operational status and hence power
2107 * state. Examples of operational state changes that can affect power
2108 * consumption are :-
2109 *
2110 * o Device is opened / closed.
2111 * o Device I/O is about to begin or has just finished.
2112 * o Device is idling in between work.
2113 *
2114 * This information is also exported via sysfs to userspace.
2115 *
2116 * DRMS will sum the total requested load on the regulator and change
2117 * to the most efficient operating mode if platform constraints allow.
2118 *
2119 * Returns the new regulator mode or error.
2120 */
2121int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2122{
2123 struct regulator_dev *rdev = regulator->rdev;
2124 struct regulator *consumer;
2125 int ret, output_uV, input_uV, total_uA_load = 0;
2126 unsigned int mode;
2127
2128 mutex_lock(&rdev->mutex);
2129
2130 /*
2131 * first check to see if we can set modes at all, otherwise just
2132 * tell the consumer everything is OK.
2133 */
2134 regulator->uA_load = uA_load;
2135 ret = regulator_check_drms(rdev);
2136 if (ret < 0) {
2137 ret = 0;
2138 goto out;
2139 }
2140
2141 if (!rdev->desc->ops->get_optimum_mode)
2142 goto out;
2143
2144 /*
2145 * we can actually do this so any errors are indicators of
2146 * potential real failure.
2147 */
2148 ret = -EINVAL;
2149
2150 /* get output voltage */
2151 output_uV = _regulator_get_voltage(rdev);
2152 if (output_uV <= 0) {
2153 rdev_err(rdev, "invalid output voltage found\n");
2154 goto out;
2155 }
2156
2157 /* get input voltage */
2158 input_uV = 0;
2159 if (rdev->supply)
2160 input_uV = regulator_get_voltage(rdev->supply);
2161 if (input_uV <= 0)
2162 input_uV = rdev->constraints->input_uV;
2163 if (input_uV <= 0) {
2164 rdev_err(rdev, "invalid input voltage found\n");
2165 goto out;
2166 }
2167
2168 /* calc total requested load for this regulator */
2169 list_for_each_entry(consumer, &rdev->consumer_list, list)
2170 total_uA_load += consumer->uA_load;
2171
2172 mode = rdev->desc->ops->get_optimum_mode(rdev,
2173 input_uV, output_uV,
2174 total_uA_load);
2175 ret = regulator_mode_constrain(rdev, &mode);
2176 if (ret < 0) {
2177 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2178 total_uA_load, input_uV, output_uV);
2179 goto out;
2180 }
2181
2182 ret = rdev->desc->ops->set_mode(rdev, mode);
2183 if (ret < 0) {
2184 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2185 goto out;
2186 }
2187 ret = mode;
2188out:
2189 mutex_unlock(&rdev->mutex);
2190 return ret;
2191}
2192EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2193
2194/**
2195 * regulator_register_notifier - register regulator event notifier
2196 * @regulator: regulator source
2197 * @nb: notifier block
2198 *
2199 * Register notifier block to receive regulator events.
2200 */
2201int regulator_register_notifier(struct regulator *regulator,
2202 struct notifier_block *nb)
2203{
2204 return blocking_notifier_chain_register(®ulator->rdev->notifier,
2205 nb);
2206}
2207EXPORT_SYMBOL_GPL(regulator_register_notifier);
2208
2209/**
2210 * regulator_unregister_notifier - unregister regulator event notifier
2211 * @regulator: regulator source
2212 * @nb: notifier block
2213 *
2214 * Unregister regulator event notifier block.
2215 */
2216int regulator_unregister_notifier(struct regulator *regulator,
2217 struct notifier_block *nb)
2218{
2219 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
2220 nb);
2221}
2222EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2223
2224/* notify regulator consumers and downstream regulator consumers.
2225 * Note mutex must be held by caller.
2226 */
2227static void _notifier_call_chain(struct regulator_dev *rdev,
2228 unsigned long event, void *data)
2229{
2230 /* call rdev chain first */
2231 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
2232}
2233
2234/**
2235 * regulator_bulk_get - get multiple regulator consumers
2236 *
2237 * @dev: Device to supply
2238 * @num_consumers: Number of consumers to register
2239 * @consumers: Configuration of consumers; clients are stored here.
2240 *
2241 * @return 0 on success, an errno on failure.
2242 *
2243 * This helper function allows drivers to get several regulator
2244 * consumers in one operation. If any of the regulators cannot be
2245 * acquired then any regulators that were allocated will be freed
2246 * before returning to the caller.
2247 */
2248int regulator_bulk_get(struct device *dev, int num_consumers,
2249 struct regulator_bulk_data *consumers)
2250{
2251 int i;
2252 int ret;
2253
2254 for (i = 0; i < num_consumers; i++)
2255 consumers[i].consumer = NULL;
2256
2257 for (i = 0; i < num_consumers; i++) {
2258 consumers[i].consumer = regulator_get(dev,
2259 consumers[i].supply);
2260 if (IS_ERR(consumers[i].consumer)) {
2261 ret = PTR_ERR(consumers[i].consumer);
2262 dev_err(dev, "Failed to get supply '%s': %d\n",
2263 consumers[i].supply, ret);
2264 consumers[i].consumer = NULL;
2265 goto err;
2266 }
2267 }
2268
2269 return 0;
2270
2271err:
2272 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2273 regulator_put(consumers[i].consumer);
2274
2275 return ret;
2276}
2277EXPORT_SYMBOL_GPL(regulator_bulk_get);
2278
2279static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2280{
2281 struct regulator_bulk_data *bulk = data;
2282
2283 bulk->ret = regulator_enable(bulk->consumer);
2284}
2285
2286/**
2287 * regulator_bulk_enable - enable multiple regulator consumers
2288 *
2289 * @num_consumers: Number of consumers
2290 * @consumers: Consumer data; clients are stored here.
2291 * @return 0 on success, an errno on failure
2292 *
2293 * This convenience API allows consumers to enable multiple regulator
2294 * clients in a single API call. If any consumers cannot be enabled
2295 * then any others that were enabled will be disabled again prior to
2296 * return.
2297 */
2298int regulator_bulk_enable(int num_consumers,
2299 struct regulator_bulk_data *consumers)
2300{
2301 LIST_HEAD(async_domain);
2302 int i;
2303 int ret = 0;
2304
2305 for (i = 0; i < num_consumers; i++)
2306 async_schedule_domain(regulator_bulk_enable_async,
2307 &consumers[i], &async_domain);
2308
2309 async_synchronize_full_domain(&async_domain);
2310
2311 /* If any consumer failed we need to unwind any that succeeded */
2312 for (i = 0; i < num_consumers; i++) {
2313 if (consumers[i].ret != 0) {
2314 ret = consumers[i].ret;
2315 goto err;
2316 }
2317 }
2318
2319 return 0;
2320
2321err:
2322 for (i = 0; i < num_consumers; i++)
2323 if (consumers[i].ret == 0)
2324 regulator_disable(consumers[i].consumer);
2325 else
2326 pr_err("Failed to enable %s: %d\n",
2327 consumers[i].supply, consumers[i].ret);
2328
2329 return ret;
2330}
2331EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2332
2333/**
2334 * regulator_bulk_disable - disable multiple regulator consumers
2335 *
2336 * @num_consumers: Number of consumers
2337 * @consumers: Consumer data; clients are stored here.
2338 * @return 0 on success, an errno on failure
2339 *
2340 * This convenience API allows consumers to disable multiple regulator
2341 * clients in a single API call. If any consumers cannot be enabled
2342 * then any others that were disabled will be disabled again prior to
2343 * return.
2344 */
2345int regulator_bulk_disable(int num_consumers,
2346 struct regulator_bulk_data *consumers)
2347{
2348 int i;
2349 int ret;
2350
2351 for (i = 0; i < num_consumers; i++) {
2352 ret = regulator_disable(consumers[i].consumer);
2353 if (ret != 0)
2354 goto err;
2355 }
2356
2357 return 0;
2358
2359err:
2360 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2361 for (--i; i >= 0; --i)
2362 regulator_enable(consumers[i].consumer);
2363
2364 return ret;
2365}
2366EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2367
2368/**
2369 * regulator_bulk_free - free multiple regulator consumers
2370 *
2371 * @num_consumers: Number of consumers
2372 * @consumers: Consumer data; clients are stored here.
2373 *
2374 * This convenience API allows consumers to free multiple regulator
2375 * clients in a single API call.
2376 */
2377void regulator_bulk_free(int num_consumers,
2378 struct regulator_bulk_data *consumers)
2379{
2380 int i;
2381
2382 for (i = 0; i < num_consumers; i++) {
2383 regulator_put(consumers[i].consumer);
2384 consumers[i].consumer = NULL;
2385 }
2386}
2387EXPORT_SYMBOL_GPL(regulator_bulk_free);
2388
2389/**
2390 * regulator_notifier_call_chain - call regulator event notifier
2391 * @rdev: regulator source
2392 * @event: notifier block
2393 * @data: callback-specific data.
2394 *
2395 * Called by regulator drivers to notify clients a regulator event has
2396 * occurred. We also notify regulator clients downstream.
2397 * Note lock must be held by caller.
2398 */
2399int regulator_notifier_call_chain(struct regulator_dev *rdev,
2400 unsigned long event, void *data)
2401{
2402 _notifier_call_chain(rdev, event, data);
2403 return NOTIFY_DONE;
2404
2405}
2406EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2407
2408/**
2409 * regulator_mode_to_status - convert a regulator mode into a status
2410 *
2411 * @mode: Mode to convert
2412 *
2413 * Convert a regulator mode into a status.
2414 */
2415int regulator_mode_to_status(unsigned int mode)
2416{
2417 switch (mode) {
2418 case REGULATOR_MODE_FAST:
2419 return REGULATOR_STATUS_FAST;
2420 case REGULATOR_MODE_NORMAL:
2421 return REGULATOR_STATUS_NORMAL;
2422 case REGULATOR_MODE_IDLE:
2423 return REGULATOR_STATUS_IDLE;
2424 case REGULATOR_STATUS_STANDBY:
2425 return REGULATOR_STATUS_STANDBY;
2426 default:
2427 return 0;
2428 }
2429}
2430EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2431
2432/*
2433 * To avoid cluttering sysfs (and memory) with useless state, only
2434 * create attributes that can be meaningfully displayed.
2435 */
2436static int add_regulator_attributes(struct regulator_dev *rdev)
2437{
2438 struct device *dev = &rdev->dev;
2439 struct regulator_ops *ops = rdev->desc->ops;
2440 int status = 0;
2441
2442 /* some attributes need specific methods to be displayed */
2443 if (ops->get_voltage || ops->get_voltage_sel) {
2444 status = device_create_file(dev, &dev_attr_microvolts);
2445 if (status < 0)
2446 return status;
2447 }
2448 if (ops->get_current_limit) {
2449 status = device_create_file(dev, &dev_attr_microamps);
2450 if (status < 0)
2451 return status;
2452 }
2453 if (ops->get_mode) {
2454 status = device_create_file(dev, &dev_attr_opmode);
2455 if (status < 0)
2456 return status;
2457 }
2458 if (ops->is_enabled) {
2459 status = device_create_file(dev, &dev_attr_state);
2460 if (status < 0)
2461 return status;
2462 }
2463 if (ops->get_status) {
2464 status = device_create_file(dev, &dev_attr_status);
2465 if (status < 0)
2466 return status;
2467 }
2468
2469 /* some attributes are type-specific */
2470 if (rdev->desc->type == REGULATOR_CURRENT) {
2471 status = device_create_file(dev, &dev_attr_requested_microamps);
2472 if (status < 0)
2473 return status;
2474 }
2475
2476 /* all the other attributes exist to support constraints;
2477 * don't show them if there are no constraints, or if the
2478 * relevant supporting methods are missing.
2479 */
2480 if (!rdev->constraints)
2481 return status;
2482
2483 /* constraints need specific supporting methods */
2484 if (ops->set_voltage || ops->set_voltage_sel) {
2485 status = device_create_file(dev, &dev_attr_min_microvolts);
2486 if (status < 0)
2487 return status;
2488 status = device_create_file(dev, &dev_attr_max_microvolts);
2489 if (status < 0)
2490 return status;
2491 }
2492 if (ops->set_current_limit) {
2493 status = device_create_file(dev, &dev_attr_min_microamps);
2494 if (status < 0)
2495 return status;
2496 status = device_create_file(dev, &dev_attr_max_microamps);
2497 if (status < 0)
2498 return status;
2499 }
2500
2501 /* suspend mode constraints need multiple supporting methods */
2502 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2503 return status;
2504
2505 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2506 if (status < 0)
2507 return status;
2508 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2509 if (status < 0)
2510 return status;
2511 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2512 if (status < 0)
2513 return status;
2514
2515 if (ops->set_suspend_voltage) {
2516 status = device_create_file(dev,
2517 &dev_attr_suspend_standby_microvolts);
2518 if (status < 0)
2519 return status;
2520 status = device_create_file(dev,
2521 &dev_attr_suspend_mem_microvolts);
2522 if (status < 0)
2523 return status;
2524 status = device_create_file(dev,
2525 &dev_attr_suspend_disk_microvolts);
2526 if (status < 0)
2527 return status;
2528 }
2529
2530 if (ops->set_suspend_mode) {
2531 status = device_create_file(dev,
2532 &dev_attr_suspend_standby_mode);
2533 if (status < 0)
2534 return status;
2535 status = device_create_file(dev,
2536 &dev_attr_suspend_mem_mode);
2537 if (status < 0)
2538 return status;
2539 status = device_create_file(dev,
2540 &dev_attr_suspend_disk_mode);
2541 if (status < 0)
2542 return status;
2543 }
2544
2545 return status;
2546}
2547
2548static void rdev_init_debugfs(struct regulator_dev *rdev)
2549{
2550#ifdef CONFIG_DEBUG_FS
2551 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2552 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2553 rdev_warn(rdev, "Failed to create debugfs directory\n");
2554 rdev->debugfs = NULL;
2555 return;
2556 }
2557
2558 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2559 &rdev->use_count);
2560 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2561 &rdev->open_count);
2562#endif
2563}
2564
2565/**
2566 * regulator_register - register regulator
2567 * @regulator_desc: regulator to register
2568 * @dev: struct device for the regulator
2569 * @init_data: platform provided init data, passed through by driver
2570 * @driver_data: private regulator data
2571 *
2572 * Called by regulator drivers to register a regulator.
2573 * Returns 0 on success.
2574 */
2575struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2576 struct device *dev, const struct regulator_init_data *init_data,
2577 void *driver_data)
2578{
2579 static atomic_t regulator_no = ATOMIC_INIT(0);
2580 struct regulator_dev *rdev;
2581 int ret, i;
2582
2583 if (regulator_desc == NULL)
2584 return ERR_PTR(-EINVAL);
2585
2586 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2587 return ERR_PTR(-EINVAL);
2588
2589 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2590 regulator_desc->type != REGULATOR_CURRENT)
2591 return ERR_PTR(-EINVAL);
2592
2593 if (!init_data)
2594 return ERR_PTR(-EINVAL);
2595
2596 /* Only one of each should be implemented */
2597 WARN_ON(regulator_desc->ops->get_voltage &&
2598 regulator_desc->ops->get_voltage_sel);
2599 WARN_ON(regulator_desc->ops->set_voltage &&
2600 regulator_desc->ops->set_voltage_sel);
2601
2602 /* If we're using selectors we must implement list_voltage. */
2603 if (regulator_desc->ops->get_voltage_sel &&
2604 !regulator_desc->ops->list_voltage) {
2605 return ERR_PTR(-EINVAL);
2606 }
2607 if (regulator_desc->ops->set_voltage_sel &&
2608 !regulator_desc->ops->list_voltage) {
2609 return ERR_PTR(-EINVAL);
2610 }
2611
2612 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2613 if (rdev == NULL)
2614 return ERR_PTR(-ENOMEM);
2615
2616 mutex_lock(®ulator_list_mutex);
2617
2618 mutex_init(&rdev->mutex);
2619 rdev->reg_data = driver_data;
2620 rdev->owner = regulator_desc->owner;
2621 rdev->desc = regulator_desc;
2622 INIT_LIST_HEAD(&rdev->consumer_list);
2623 INIT_LIST_HEAD(&rdev->list);
2624 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2625
2626 /* preform any regulator specific init */
2627 if (init_data->regulator_init) {
2628 ret = init_data->regulator_init(rdev->reg_data);
2629 if (ret < 0)
2630 goto clean;
2631 }
2632
2633 /* register with sysfs */
2634 rdev->dev.class = ®ulator_class;
2635 rdev->dev.parent = dev;
2636 dev_set_name(&rdev->dev, "regulator.%d",
2637 atomic_inc_return(®ulator_no) - 1);
2638 ret = device_register(&rdev->dev);
2639 if (ret != 0) {
2640 put_device(&rdev->dev);
2641 goto clean;
2642 }
2643
2644 dev_set_drvdata(&rdev->dev, rdev);
2645
2646 /* set regulator constraints */
2647 ret = set_machine_constraints(rdev, &init_data->constraints);
2648 if (ret < 0)
2649 goto scrub;
2650
2651 /* add attributes supported by this regulator */
2652 ret = add_regulator_attributes(rdev);
2653 if (ret < 0)
2654 goto scrub;
2655
2656 if (init_data->supply_regulator) {
2657 struct regulator_dev *r;
2658 int found = 0;
2659
2660 list_for_each_entry(r, ®ulator_list, list) {
2661 if (strcmp(rdev_get_name(r),
2662 init_data->supply_regulator) == 0) {
2663 found = 1;
2664 break;
2665 }
2666 }
2667
2668 if (!found) {
2669 dev_err(dev, "Failed to find supply %s\n",
2670 init_data->supply_regulator);
2671 ret = -ENODEV;
2672 goto scrub;
2673 }
2674
2675 ret = set_supply(rdev, r);
2676 if (ret < 0)
2677 goto scrub;
2678 }
2679
2680 /* add consumers devices */
2681 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2682 ret = set_consumer_device_supply(rdev,
2683 init_data->consumer_supplies[i].dev,
2684 init_data->consumer_supplies[i].dev_name,
2685 init_data->consumer_supplies[i].supply);
2686 if (ret < 0) {
2687 dev_err(dev, "Failed to set supply %s\n",
2688 init_data->consumer_supplies[i].supply);
2689 goto unset_supplies;
2690 }
2691 }
2692
2693 list_add(&rdev->list, ®ulator_list);
2694
2695 rdev_init_debugfs(rdev);
2696out:
2697 mutex_unlock(®ulator_list_mutex);
2698 return rdev;
2699
2700unset_supplies:
2701 unset_regulator_supplies(rdev);
2702
2703scrub:
2704 kfree(rdev->constraints);
2705 device_unregister(&rdev->dev);
2706 /* device core frees rdev */
2707 rdev = ERR_PTR(ret);
2708 goto out;
2709
2710clean:
2711 kfree(rdev);
2712 rdev = ERR_PTR(ret);
2713 goto out;
2714}
2715EXPORT_SYMBOL_GPL(regulator_register);
2716
2717/**
2718 * regulator_unregister - unregister regulator
2719 * @rdev: regulator to unregister
2720 *
2721 * Called by regulator drivers to unregister a regulator.
2722 */
2723void regulator_unregister(struct regulator_dev *rdev)
2724{
2725 if (rdev == NULL)
2726 return;
2727
2728 mutex_lock(®ulator_list_mutex);
2729#ifdef CONFIG_DEBUG_FS
2730 debugfs_remove_recursive(rdev->debugfs);
2731#endif
2732 WARN_ON(rdev->open_count);
2733 unset_regulator_supplies(rdev);
2734 list_del(&rdev->list);
2735 if (rdev->supply)
2736 regulator_put(rdev->supply);
2737 device_unregister(&rdev->dev);
2738 kfree(rdev->constraints);
2739 mutex_unlock(®ulator_list_mutex);
2740}
2741EXPORT_SYMBOL_GPL(regulator_unregister);
2742
2743/**
2744 * regulator_suspend_prepare - prepare regulators for system wide suspend
2745 * @state: system suspend state
2746 *
2747 * Configure each regulator with it's suspend operating parameters for state.
2748 * This will usually be called by machine suspend code prior to supending.
2749 */
2750int regulator_suspend_prepare(suspend_state_t state)
2751{
2752 struct regulator_dev *rdev;
2753 int ret = 0;
2754
2755 /* ON is handled by regulator active state */
2756 if (state == PM_SUSPEND_ON)
2757 return -EINVAL;
2758
2759 mutex_lock(®ulator_list_mutex);
2760 list_for_each_entry(rdev, ®ulator_list, list) {
2761
2762 mutex_lock(&rdev->mutex);
2763 ret = suspend_prepare(rdev, state);
2764 mutex_unlock(&rdev->mutex);
2765
2766 if (ret < 0) {
2767 rdev_err(rdev, "failed to prepare\n");
2768 goto out;
2769 }
2770 }
2771out:
2772 mutex_unlock(®ulator_list_mutex);
2773 return ret;
2774}
2775EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2776
2777/**
2778 * regulator_suspend_finish - resume regulators from system wide suspend
2779 *
2780 * Turn on regulators that might be turned off by regulator_suspend_prepare
2781 * and that should be turned on according to the regulators properties.
2782 */
2783int regulator_suspend_finish(void)
2784{
2785 struct regulator_dev *rdev;
2786 int ret = 0, error;
2787
2788 mutex_lock(®ulator_list_mutex);
2789 list_for_each_entry(rdev, ®ulator_list, list) {
2790 struct regulator_ops *ops = rdev->desc->ops;
2791
2792 mutex_lock(&rdev->mutex);
2793 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2794 ops->enable) {
2795 error = ops->enable(rdev);
2796 if (error)
2797 ret = error;
2798 } else {
2799 if (!has_full_constraints)
2800 goto unlock;
2801 if (!ops->disable)
2802 goto unlock;
2803 if (ops->is_enabled && !ops->is_enabled(rdev))
2804 goto unlock;
2805
2806 error = ops->disable(rdev);
2807 if (error)
2808 ret = error;
2809 }
2810unlock:
2811 mutex_unlock(&rdev->mutex);
2812 }
2813 mutex_unlock(®ulator_list_mutex);
2814 return ret;
2815}
2816EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2817
2818/**
2819 * regulator_has_full_constraints - the system has fully specified constraints
2820 *
2821 * Calling this function will cause the regulator API to disable all
2822 * regulators which have a zero use count and don't have an always_on
2823 * constraint in a late_initcall.
2824 *
2825 * The intention is that this will become the default behaviour in a
2826 * future kernel release so users are encouraged to use this facility
2827 * now.
2828 */
2829void regulator_has_full_constraints(void)
2830{
2831 has_full_constraints = 1;
2832}
2833EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2834
2835/**
2836 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2837 *
2838 * Calling this function will cause the regulator API to provide a
2839 * dummy regulator to consumers if no physical regulator is found,
2840 * allowing most consumers to proceed as though a regulator were
2841 * configured. This allows systems such as those with software
2842 * controllable regulators for the CPU core only to be brought up more
2843 * readily.
2844 */
2845void regulator_use_dummy_regulator(void)
2846{
2847 board_wants_dummy_regulator = true;
2848}
2849EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2850
2851/**
2852 * rdev_get_drvdata - get rdev regulator driver data
2853 * @rdev: regulator
2854 *
2855 * Get rdev regulator driver private data. This call can be used in the
2856 * regulator driver context.
2857 */
2858void *rdev_get_drvdata(struct regulator_dev *rdev)
2859{
2860 return rdev->reg_data;
2861}
2862EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2863
2864/**
2865 * regulator_get_drvdata - get regulator driver data
2866 * @regulator: regulator
2867 *
2868 * Get regulator driver private data. This call can be used in the consumer
2869 * driver context when non API regulator specific functions need to be called.
2870 */
2871void *regulator_get_drvdata(struct regulator *regulator)
2872{
2873 return regulator->rdev->reg_data;
2874}
2875EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2876
2877/**
2878 * regulator_set_drvdata - set regulator driver data
2879 * @regulator: regulator
2880 * @data: data
2881 */
2882void regulator_set_drvdata(struct regulator *regulator, void *data)
2883{
2884 regulator->rdev->reg_data = data;
2885}
2886EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2887
2888/**
2889 * regulator_get_id - get regulator ID
2890 * @rdev: regulator
2891 */
2892int rdev_get_id(struct regulator_dev *rdev)
2893{
2894 return rdev->desc->id;
2895}
2896EXPORT_SYMBOL_GPL(rdev_get_id);
2897
2898struct device *rdev_get_dev(struct regulator_dev *rdev)
2899{
2900 return &rdev->dev;
2901}
2902EXPORT_SYMBOL_GPL(rdev_get_dev);
2903
2904void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2905{
2906 return reg_init_data->driver_data;
2907}
2908EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2909
2910static int __init regulator_init(void)
2911{
2912 int ret;
2913
2914 ret = class_register(®ulator_class);
2915
2916#ifdef CONFIG_DEBUG_FS
2917 debugfs_root = debugfs_create_dir("regulator", NULL);
2918 if (IS_ERR(debugfs_root) || !debugfs_root) {
2919 pr_warn("regulator: Failed to create debugfs directory\n");
2920 debugfs_root = NULL;
2921 }
2922#endif
2923
2924 regulator_dummy_init();
2925
2926 return ret;
2927}
2928
2929/* init early to allow our consumers to complete system booting */
2930core_initcall(regulator_init);
2931
2932static int __init regulator_init_complete(void)
2933{
2934 struct regulator_dev *rdev;
2935 struct regulator_ops *ops;
2936 struct regulation_constraints *c;
2937 int enabled, ret;
2938
2939 mutex_lock(®ulator_list_mutex);
2940
2941 /* If we have a full configuration then disable any regulators
2942 * which are not in use or always_on. This will become the
2943 * default behaviour in the future.
2944 */
2945 list_for_each_entry(rdev, ®ulator_list, list) {
2946 ops = rdev->desc->ops;
2947 c = rdev->constraints;
2948
2949 if (!ops->disable || (c && c->always_on))
2950 continue;
2951
2952 mutex_lock(&rdev->mutex);
2953
2954 if (rdev->use_count)
2955 goto unlock;
2956
2957 /* If we can't read the status assume it's on. */
2958 if (ops->is_enabled)
2959 enabled = ops->is_enabled(rdev);
2960 else
2961 enabled = 1;
2962
2963 if (!enabled)
2964 goto unlock;
2965
2966 if (has_full_constraints) {
2967 /* We log since this may kill the system if it
2968 * goes wrong. */
2969 rdev_info(rdev, "disabling\n");
2970 ret = ops->disable(rdev);
2971 if (ret != 0) {
2972 rdev_err(rdev, "couldn't disable: %d\n", ret);
2973 }
2974 } else {
2975 /* The intention is that in future we will
2976 * assume that full constraints are provided
2977 * so warn even if we aren't going to do
2978 * anything here.
2979 */
2980 rdev_warn(rdev, "incomplete constraints, leaving on\n");
2981 }
2982
2983unlock:
2984 mutex_unlock(&rdev->mutex);
2985 }
2986
2987 mutex_unlock(®ulator_list_mutex);
2988
2989 return 0;
2990}
2991late_initcall(regulator_init_complete);
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/gpio.h>
27#include <linux/gpio/consumer.h>
28#include <linux/of.h>
29#include <linux/regmap.h>
30#include <linux/regulator/of_regulator.h>
31#include <linux/regulator/consumer.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
34#include <linux/module.h>
35
36#define CREATE_TRACE_POINTS
37#include <trace/events/regulator.h>
38
39#include "dummy.h"
40#include "internal.h"
41
42#define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50#define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52
53static DEFINE_MUTEX(regulator_list_mutex);
54static LIST_HEAD(regulator_map_list);
55static LIST_HEAD(regulator_ena_gpio_list);
56static LIST_HEAD(regulator_supply_alias_list);
57static bool has_full_constraints;
58
59static struct dentry *debugfs_root;
60
61/*
62 * struct regulator_map
63 *
64 * Used to provide symbolic supply names to devices.
65 */
66struct regulator_map {
67 struct list_head list;
68 const char *dev_name; /* The dev_name() for the consumer */
69 const char *supply;
70 struct regulator_dev *regulator;
71};
72
73/*
74 * struct regulator_enable_gpio
75 *
76 * Management for shared enable GPIO pin
77 */
78struct regulator_enable_gpio {
79 struct list_head list;
80 struct gpio_desc *gpiod;
81 u32 enable_count; /* a number of enabled shared GPIO */
82 u32 request_count; /* a number of requested shared GPIO */
83 unsigned int ena_gpio_invert:1;
84};
85
86/*
87 * struct regulator_supply_alias
88 *
89 * Used to map lookups for a supply onto an alternative device.
90 */
91struct regulator_supply_alias {
92 struct list_head list;
93 struct device *src_dev;
94 const char *src_supply;
95 struct device *alias_dev;
96 const char *alias_supply;
97};
98
99static int _regulator_is_enabled(struct regulator_dev *rdev);
100static int _regulator_disable(struct regulator_dev *rdev);
101static int _regulator_get_voltage(struct regulator_dev *rdev);
102static int _regulator_get_current_limit(struct regulator_dev *rdev);
103static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
104static int _notifier_call_chain(struct regulator_dev *rdev,
105 unsigned long event, void *data);
106static int _regulator_do_set_voltage(struct regulator_dev *rdev,
107 int min_uV, int max_uV);
108static struct regulator *create_regulator(struct regulator_dev *rdev,
109 struct device *dev,
110 const char *supply_name);
111static void _regulator_put(struct regulator *regulator);
112
113static const char *rdev_get_name(struct regulator_dev *rdev)
114{
115 if (rdev->constraints && rdev->constraints->name)
116 return rdev->constraints->name;
117 else if (rdev->desc->name)
118 return rdev->desc->name;
119 else
120 return "";
121}
122
123static bool have_full_constraints(void)
124{
125 return has_full_constraints || of_have_populated_dt();
126}
127
128static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
129{
130 if (!rdev->constraints) {
131 rdev_err(rdev, "no constraints\n");
132 return false;
133 }
134
135 if (rdev->constraints->valid_ops_mask & ops)
136 return true;
137
138 return false;
139}
140
141static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
142{
143 if (rdev && rdev->supply)
144 return rdev->supply->rdev;
145
146 return NULL;
147}
148
149/**
150 * regulator_lock_supply - lock a regulator and its supplies
151 * @rdev: regulator source
152 */
153static void regulator_lock_supply(struct regulator_dev *rdev)
154{
155 int i;
156
157 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
158 mutex_lock_nested(&rdev->mutex, i);
159}
160
161/**
162 * regulator_unlock_supply - unlock a regulator and its supplies
163 * @rdev: regulator source
164 */
165static void regulator_unlock_supply(struct regulator_dev *rdev)
166{
167 struct regulator *supply;
168
169 while (1) {
170 mutex_unlock(&rdev->mutex);
171 supply = rdev->supply;
172
173 if (!rdev->supply)
174 return;
175
176 rdev = supply->rdev;
177 }
178}
179
180/**
181 * of_get_regulator - get a regulator device node based on supply name
182 * @dev: Device pointer for the consumer (of regulator) device
183 * @supply: regulator supply name
184 *
185 * Extract the regulator device node corresponding to the supply name.
186 * returns the device node corresponding to the regulator if found, else
187 * returns NULL.
188 */
189static struct device_node *of_get_regulator(struct device *dev, const char *supply)
190{
191 struct device_node *regnode = NULL;
192 char prop_name[32]; /* 32 is max size of property name */
193
194 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
195
196 snprintf(prop_name, 32, "%s-supply", supply);
197 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
198
199 if (!regnode) {
200 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
201 prop_name, dev->of_node);
202 return NULL;
203 }
204 return regnode;
205}
206
207/* Platform voltage constraint check */
208static int regulator_check_voltage(struct regulator_dev *rdev,
209 int *min_uV, int *max_uV)
210{
211 BUG_ON(*min_uV > *max_uV);
212
213 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
214 rdev_err(rdev, "voltage operation not allowed\n");
215 return -EPERM;
216 }
217
218 if (*max_uV > rdev->constraints->max_uV)
219 *max_uV = rdev->constraints->max_uV;
220 if (*min_uV < rdev->constraints->min_uV)
221 *min_uV = rdev->constraints->min_uV;
222
223 if (*min_uV > *max_uV) {
224 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
225 *min_uV, *max_uV);
226 return -EINVAL;
227 }
228
229 return 0;
230}
231
232/* return 0 if the state is valid */
233static int regulator_check_states(suspend_state_t state)
234{
235 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
236}
237
238/* Make sure we select a voltage that suits the needs of all
239 * regulator consumers
240 */
241static int regulator_check_consumers(struct regulator_dev *rdev,
242 int *min_uV, int *max_uV,
243 suspend_state_t state)
244{
245 struct regulator *regulator;
246 struct regulator_voltage *voltage;
247
248 list_for_each_entry(regulator, &rdev->consumer_list, list) {
249 voltage = ®ulator->voltage[state];
250 /*
251 * Assume consumers that didn't say anything are OK
252 * with anything in the constraint range.
253 */
254 if (!voltage->min_uV && !voltage->max_uV)
255 continue;
256
257 if (*max_uV > voltage->max_uV)
258 *max_uV = voltage->max_uV;
259 if (*min_uV < voltage->min_uV)
260 *min_uV = voltage->min_uV;
261 }
262
263 if (*min_uV > *max_uV) {
264 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
265 *min_uV, *max_uV);
266 return -EINVAL;
267 }
268
269 return 0;
270}
271
272/* current constraint check */
273static int regulator_check_current_limit(struct regulator_dev *rdev,
274 int *min_uA, int *max_uA)
275{
276 BUG_ON(*min_uA > *max_uA);
277
278 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
279 rdev_err(rdev, "current operation not allowed\n");
280 return -EPERM;
281 }
282
283 if (*max_uA > rdev->constraints->max_uA)
284 *max_uA = rdev->constraints->max_uA;
285 if (*min_uA < rdev->constraints->min_uA)
286 *min_uA = rdev->constraints->min_uA;
287
288 if (*min_uA > *max_uA) {
289 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
290 *min_uA, *max_uA);
291 return -EINVAL;
292 }
293
294 return 0;
295}
296
297/* operating mode constraint check */
298static int regulator_mode_constrain(struct regulator_dev *rdev,
299 unsigned int *mode)
300{
301 switch (*mode) {
302 case REGULATOR_MODE_FAST:
303 case REGULATOR_MODE_NORMAL:
304 case REGULATOR_MODE_IDLE:
305 case REGULATOR_MODE_STANDBY:
306 break;
307 default:
308 rdev_err(rdev, "invalid mode %x specified\n", *mode);
309 return -EINVAL;
310 }
311
312 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
313 rdev_err(rdev, "mode operation not allowed\n");
314 return -EPERM;
315 }
316
317 /* The modes are bitmasks, the most power hungry modes having
318 * the lowest values. If the requested mode isn't supported
319 * try higher modes. */
320 while (*mode) {
321 if (rdev->constraints->valid_modes_mask & *mode)
322 return 0;
323 *mode /= 2;
324 }
325
326 return -EINVAL;
327}
328
329static inline struct regulator_state *
330regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
331{
332 if (rdev->constraints == NULL)
333 return NULL;
334
335 switch (state) {
336 case PM_SUSPEND_STANDBY:
337 return &rdev->constraints->state_standby;
338 case PM_SUSPEND_MEM:
339 return &rdev->constraints->state_mem;
340 case PM_SUSPEND_MAX:
341 return &rdev->constraints->state_disk;
342 default:
343 return NULL;
344 }
345}
346
347static ssize_t regulator_uV_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
349{
350 struct regulator_dev *rdev = dev_get_drvdata(dev);
351 ssize_t ret;
352
353 mutex_lock(&rdev->mutex);
354 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
355 mutex_unlock(&rdev->mutex);
356
357 return ret;
358}
359static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
360
361static ssize_t regulator_uA_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
364 struct regulator_dev *rdev = dev_get_drvdata(dev);
365
366 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
367}
368static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
369
370static ssize_t name_show(struct device *dev, struct device_attribute *attr,
371 char *buf)
372{
373 struct regulator_dev *rdev = dev_get_drvdata(dev);
374
375 return sprintf(buf, "%s\n", rdev_get_name(rdev));
376}
377static DEVICE_ATTR_RO(name);
378
379static ssize_t regulator_print_opmode(char *buf, int mode)
380{
381 switch (mode) {
382 case REGULATOR_MODE_FAST:
383 return sprintf(buf, "fast\n");
384 case REGULATOR_MODE_NORMAL:
385 return sprintf(buf, "normal\n");
386 case REGULATOR_MODE_IDLE:
387 return sprintf(buf, "idle\n");
388 case REGULATOR_MODE_STANDBY:
389 return sprintf(buf, "standby\n");
390 }
391 return sprintf(buf, "unknown\n");
392}
393
394static ssize_t regulator_opmode_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
396{
397 struct regulator_dev *rdev = dev_get_drvdata(dev);
398
399 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
400}
401static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
402
403static ssize_t regulator_print_state(char *buf, int state)
404{
405 if (state > 0)
406 return sprintf(buf, "enabled\n");
407 else if (state == 0)
408 return sprintf(buf, "disabled\n");
409 else
410 return sprintf(buf, "unknown\n");
411}
412
413static ssize_t regulator_state_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
416 struct regulator_dev *rdev = dev_get_drvdata(dev);
417 ssize_t ret;
418
419 mutex_lock(&rdev->mutex);
420 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
421 mutex_unlock(&rdev->mutex);
422
423 return ret;
424}
425static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
426
427static ssize_t regulator_status_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
429{
430 struct regulator_dev *rdev = dev_get_drvdata(dev);
431 int status;
432 char *label;
433
434 status = rdev->desc->ops->get_status(rdev);
435 if (status < 0)
436 return status;
437
438 switch (status) {
439 case REGULATOR_STATUS_OFF:
440 label = "off";
441 break;
442 case REGULATOR_STATUS_ON:
443 label = "on";
444 break;
445 case REGULATOR_STATUS_ERROR:
446 label = "error";
447 break;
448 case REGULATOR_STATUS_FAST:
449 label = "fast";
450 break;
451 case REGULATOR_STATUS_NORMAL:
452 label = "normal";
453 break;
454 case REGULATOR_STATUS_IDLE:
455 label = "idle";
456 break;
457 case REGULATOR_STATUS_STANDBY:
458 label = "standby";
459 break;
460 case REGULATOR_STATUS_BYPASS:
461 label = "bypass";
462 break;
463 case REGULATOR_STATUS_UNDEFINED:
464 label = "undefined";
465 break;
466 default:
467 return -ERANGE;
468 }
469
470 return sprintf(buf, "%s\n", label);
471}
472static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
473
474static ssize_t regulator_min_uA_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
476{
477 struct regulator_dev *rdev = dev_get_drvdata(dev);
478
479 if (!rdev->constraints)
480 return sprintf(buf, "constraint not defined\n");
481
482 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
483}
484static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
485
486static ssize_t regulator_max_uA_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
488{
489 struct regulator_dev *rdev = dev_get_drvdata(dev);
490
491 if (!rdev->constraints)
492 return sprintf(buf, "constraint not defined\n");
493
494 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
495}
496static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
497
498static ssize_t regulator_min_uV_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
501 struct regulator_dev *rdev = dev_get_drvdata(dev);
502
503 if (!rdev->constraints)
504 return sprintf(buf, "constraint not defined\n");
505
506 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
507}
508static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
509
510static ssize_t regulator_max_uV_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
512{
513 struct regulator_dev *rdev = dev_get_drvdata(dev);
514
515 if (!rdev->constraints)
516 return sprintf(buf, "constraint not defined\n");
517
518 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
519}
520static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
521
522static ssize_t regulator_total_uA_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
525 struct regulator_dev *rdev = dev_get_drvdata(dev);
526 struct regulator *regulator;
527 int uA = 0;
528
529 mutex_lock(&rdev->mutex);
530 list_for_each_entry(regulator, &rdev->consumer_list, list)
531 uA += regulator->uA_load;
532 mutex_unlock(&rdev->mutex);
533 return sprintf(buf, "%d\n", uA);
534}
535static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
536
537static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
538 char *buf)
539{
540 struct regulator_dev *rdev = dev_get_drvdata(dev);
541 return sprintf(buf, "%d\n", rdev->use_count);
542}
543static DEVICE_ATTR_RO(num_users);
544
545static ssize_t type_show(struct device *dev, struct device_attribute *attr,
546 char *buf)
547{
548 struct regulator_dev *rdev = dev_get_drvdata(dev);
549
550 switch (rdev->desc->type) {
551 case REGULATOR_VOLTAGE:
552 return sprintf(buf, "voltage\n");
553 case REGULATOR_CURRENT:
554 return sprintf(buf, "current\n");
555 }
556 return sprintf(buf, "unknown\n");
557}
558static DEVICE_ATTR_RO(type);
559
560static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 struct regulator_dev *rdev = dev_get_drvdata(dev);
564
565 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
566}
567static DEVICE_ATTR(suspend_mem_microvolts, 0444,
568 regulator_suspend_mem_uV_show, NULL);
569
570static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
573 struct regulator_dev *rdev = dev_get_drvdata(dev);
574
575 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
576}
577static DEVICE_ATTR(suspend_disk_microvolts, 0444,
578 regulator_suspend_disk_uV_show, NULL);
579
580static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
581 struct device_attribute *attr, char *buf)
582{
583 struct regulator_dev *rdev = dev_get_drvdata(dev);
584
585 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
586}
587static DEVICE_ATTR(suspend_standby_microvolts, 0444,
588 regulator_suspend_standby_uV_show, NULL);
589
590static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
591 struct device_attribute *attr, char *buf)
592{
593 struct regulator_dev *rdev = dev_get_drvdata(dev);
594
595 return regulator_print_opmode(buf,
596 rdev->constraints->state_mem.mode);
597}
598static DEVICE_ATTR(suspend_mem_mode, 0444,
599 regulator_suspend_mem_mode_show, NULL);
600
601static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
603{
604 struct regulator_dev *rdev = dev_get_drvdata(dev);
605
606 return regulator_print_opmode(buf,
607 rdev->constraints->state_disk.mode);
608}
609static DEVICE_ATTR(suspend_disk_mode, 0444,
610 regulator_suspend_disk_mode_show, NULL);
611
612static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
615 struct regulator_dev *rdev = dev_get_drvdata(dev);
616
617 return regulator_print_opmode(buf,
618 rdev->constraints->state_standby.mode);
619}
620static DEVICE_ATTR(suspend_standby_mode, 0444,
621 regulator_suspend_standby_mode_show, NULL);
622
623static ssize_t regulator_suspend_mem_state_show(struct device *dev,
624 struct device_attribute *attr, char *buf)
625{
626 struct regulator_dev *rdev = dev_get_drvdata(dev);
627
628 return regulator_print_state(buf,
629 rdev->constraints->state_mem.enabled);
630}
631static DEVICE_ATTR(suspend_mem_state, 0444,
632 regulator_suspend_mem_state_show, NULL);
633
634static ssize_t regulator_suspend_disk_state_show(struct device *dev,
635 struct device_attribute *attr, char *buf)
636{
637 struct regulator_dev *rdev = dev_get_drvdata(dev);
638
639 return regulator_print_state(buf,
640 rdev->constraints->state_disk.enabled);
641}
642static DEVICE_ATTR(suspend_disk_state, 0444,
643 regulator_suspend_disk_state_show, NULL);
644
645static ssize_t regulator_suspend_standby_state_show(struct device *dev,
646 struct device_attribute *attr, char *buf)
647{
648 struct regulator_dev *rdev = dev_get_drvdata(dev);
649
650 return regulator_print_state(buf,
651 rdev->constraints->state_standby.enabled);
652}
653static DEVICE_ATTR(suspend_standby_state, 0444,
654 regulator_suspend_standby_state_show, NULL);
655
656static ssize_t regulator_bypass_show(struct device *dev,
657 struct device_attribute *attr, char *buf)
658{
659 struct regulator_dev *rdev = dev_get_drvdata(dev);
660 const char *report;
661 bool bypass;
662 int ret;
663
664 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
665
666 if (ret != 0)
667 report = "unknown";
668 else if (bypass)
669 report = "enabled";
670 else
671 report = "disabled";
672
673 return sprintf(buf, "%s\n", report);
674}
675static DEVICE_ATTR(bypass, 0444,
676 regulator_bypass_show, NULL);
677
678/* Calculate the new optimum regulator operating mode based on the new total
679 * consumer load. All locks held by caller */
680static int drms_uA_update(struct regulator_dev *rdev)
681{
682 struct regulator *sibling;
683 int current_uA = 0, output_uV, input_uV, err;
684 unsigned int mode;
685
686 lockdep_assert_held_once(&rdev->mutex);
687
688 /*
689 * first check to see if we can set modes at all, otherwise just
690 * tell the consumer everything is OK.
691 */
692 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
693 return 0;
694
695 if (!rdev->desc->ops->get_optimum_mode &&
696 !rdev->desc->ops->set_load)
697 return 0;
698
699 if (!rdev->desc->ops->set_mode &&
700 !rdev->desc->ops->set_load)
701 return -EINVAL;
702
703 /* calc total requested load */
704 list_for_each_entry(sibling, &rdev->consumer_list, list)
705 current_uA += sibling->uA_load;
706
707 current_uA += rdev->constraints->system_load;
708
709 if (rdev->desc->ops->set_load) {
710 /* set the optimum mode for our new total regulator load */
711 err = rdev->desc->ops->set_load(rdev, current_uA);
712 if (err < 0)
713 rdev_err(rdev, "failed to set load %d\n", current_uA);
714 } else {
715 /* get output voltage */
716 output_uV = _regulator_get_voltage(rdev);
717 if (output_uV <= 0) {
718 rdev_err(rdev, "invalid output voltage found\n");
719 return -EINVAL;
720 }
721
722 /* get input voltage */
723 input_uV = 0;
724 if (rdev->supply)
725 input_uV = regulator_get_voltage(rdev->supply);
726 if (input_uV <= 0)
727 input_uV = rdev->constraints->input_uV;
728 if (input_uV <= 0) {
729 rdev_err(rdev, "invalid input voltage found\n");
730 return -EINVAL;
731 }
732
733 /* now get the optimum mode for our new total regulator load */
734 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
735 output_uV, current_uA);
736
737 /* check the new mode is allowed */
738 err = regulator_mode_constrain(rdev, &mode);
739 if (err < 0) {
740 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
741 current_uA, input_uV, output_uV);
742 return err;
743 }
744
745 err = rdev->desc->ops->set_mode(rdev, mode);
746 if (err < 0)
747 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
748 }
749
750 return err;
751}
752
753static int suspend_set_state(struct regulator_dev *rdev,
754 suspend_state_t state)
755{
756 int ret = 0;
757 struct regulator_state *rstate;
758
759 rstate = regulator_get_suspend_state(rdev, state);
760 if (rstate == NULL)
761 return 0;
762
763 /* If we have no suspend mode configration don't set anything;
764 * only warn if the driver implements set_suspend_voltage or
765 * set_suspend_mode callback.
766 */
767 if (rstate->enabled != ENABLE_IN_SUSPEND &&
768 rstate->enabled != DISABLE_IN_SUSPEND) {
769 if (rdev->desc->ops->set_suspend_voltage ||
770 rdev->desc->ops->set_suspend_mode)
771 rdev_warn(rdev, "No configuration\n");
772 return 0;
773 }
774
775 if (rstate->enabled == ENABLE_IN_SUSPEND &&
776 rdev->desc->ops->set_suspend_enable)
777 ret = rdev->desc->ops->set_suspend_enable(rdev);
778 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
779 rdev->desc->ops->set_suspend_disable)
780 ret = rdev->desc->ops->set_suspend_disable(rdev);
781 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
782 ret = 0;
783
784 if (ret < 0) {
785 rdev_err(rdev, "failed to enabled/disable\n");
786 return ret;
787 }
788
789 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
790 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
791 if (ret < 0) {
792 rdev_err(rdev, "failed to set voltage\n");
793 return ret;
794 }
795 }
796
797 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
798 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
799 if (ret < 0) {
800 rdev_err(rdev, "failed to set mode\n");
801 return ret;
802 }
803 }
804
805 return ret;
806}
807
808static void print_constraints(struct regulator_dev *rdev)
809{
810 struct regulation_constraints *constraints = rdev->constraints;
811 char buf[160] = "";
812 size_t len = sizeof(buf) - 1;
813 int count = 0;
814 int ret;
815
816 if (constraints->min_uV && constraints->max_uV) {
817 if (constraints->min_uV == constraints->max_uV)
818 count += scnprintf(buf + count, len - count, "%d mV ",
819 constraints->min_uV / 1000);
820 else
821 count += scnprintf(buf + count, len - count,
822 "%d <--> %d mV ",
823 constraints->min_uV / 1000,
824 constraints->max_uV / 1000);
825 }
826
827 if (!constraints->min_uV ||
828 constraints->min_uV != constraints->max_uV) {
829 ret = _regulator_get_voltage(rdev);
830 if (ret > 0)
831 count += scnprintf(buf + count, len - count,
832 "at %d mV ", ret / 1000);
833 }
834
835 if (constraints->uV_offset)
836 count += scnprintf(buf + count, len - count, "%dmV offset ",
837 constraints->uV_offset / 1000);
838
839 if (constraints->min_uA && constraints->max_uA) {
840 if (constraints->min_uA == constraints->max_uA)
841 count += scnprintf(buf + count, len - count, "%d mA ",
842 constraints->min_uA / 1000);
843 else
844 count += scnprintf(buf + count, len - count,
845 "%d <--> %d mA ",
846 constraints->min_uA / 1000,
847 constraints->max_uA / 1000);
848 }
849
850 if (!constraints->min_uA ||
851 constraints->min_uA != constraints->max_uA) {
852 ret = _regulator_get_current_limit(rdev);
853 if (ret > 0)
854 count += scnprintf(buf + count, len - count,
855 "at %d mA ", ret / 1000);
856 }
857
858 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
859 count += scnprintf(buf + count, len - count, "fast ");
860 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
861 count += scnprintf(buf + count, len - count, "normal ");
862 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
863 count += scnprintf(buf + count, len - count, "idle ");
864 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
865 count += scnprintf(buf + count, len - count, "standby");
866
867 if (!count)
868 scnprintf(buf, len, "no parameters");
869
870 rdev_dbg(rdev, "%s\n", buf);
871
872 if ((constraints->min_uV != constraints->max_uV) &&
873 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
874 rdev_warn(rdev,
875 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
876}
877
878static int machine_constraints_voltage(struct regulator_dev *rdev,
879 struct regulation_constraints *constraints)
880{
881 const struct regulator_ops *ops = rdev->desc->ops;
882 int ret;
883
884 /* do we need to apply the constraint voltage */
885 if (rdev->constraints->apply_uV &&
886 rdev->constraints->min_uV && rdev->constraints->max_uV) {
887 int target_min, target_max;
888 int current_uV = _regulator_get_voltage(rdev);
889 if (current_uV < 0) {
890 rdev_err(rdev,
891 "failed to get the current voltage(%d)\n",
892 current_uV);
893 return current_uV;
894 }
895
896 /*
897 * If we're below the minimum voltage move up to the
898 * minimum voltage, if we're above the maximum voltage
899 * then move down to the maximum.
900 */
901 target_min = current_uV;
902 target_max = current_uV;
903
904 if (current_uV < rdev->constraints->min_uV) {
905 target_min = rdev->constraints->min_uV;
906 target_max = rdev->constraints->min_uV;
907 }
908
909 if (current_uV > rdev->constraints->max_uV) {
910 target_min = rdev->constraints->max_uV;
911 target_max = rdev->constraints->max_uV;
912 }
913
914 if (target_min != current_uV || target_max != current_uV) {
915 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
916 current_uV, target_min, target_max);
917 ret = _regulator_do_set_voltage(
918 rdev, target_min, target_max);
919 if (ret < 0) {
920 rdev_err(rdev,
921 "failed to apply %d-%duV constraint(%d)\n",
922 target_min, target_max, ret);
923 return ret;
924 }
925 }
926 }
927
928 /* constrain machine-level voltage specs to fit
929 * the actual range supported by this regulator.
930 */
931 if (ops->list_voltage && rdev->desc->n_voltages) {
932 int count = rdev->desc->n_voltages;
933 int i;
934 int min_uV = INT_MAX;
935 int max_uV = INT_MIN;
936 int cmin = constraints->min_uV;
937 int cmax = constraints->max_uV;
938
939 /* it's safe to autoconfigure fixed-voltage supplies
940 and the constraints are used by list_voltage. */
941 if (count == 1 && !cmin) {
942 cmin = 1;
943 cmax = INT_MAX;
944 constraints->min_uV = cmin;
945 constraints->max_uV = cmax;
946 }
947
948 /* voltage constraints are optional */
949 if ((cmin == 0) && (cmax == 0))
950 return 0;
951
952 /* else require explicit machine-level constraints */
953 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
954 rdev_err(rdev, "invalid voltage constraints\n");
955 return -EINVAL;
956 }
957
958 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
959 for (i = 0; i < count; i++) {
960 int value;
961
962 value = ops->list_voltage(rdev, i);
963 if (value <= 0)
964 continue;
965
966 /* maybe adjust [min_uV..max_uV] */
967 if (value >= cmin && value < min_uV)
968 min_uV = value;
969 if (value <= cmax && value > max_uV)
970 max_uV = value;
971 }
972
973 /* final: [min_uV..max_uV] valid iff constraints valid */
974 if (max_uV < min_uV) {
975 rdev_err(rdev,
976 "unsupportable voltage constraints %u-%uuV\n",
977 min_uV, max_uV);
978 return -EINVAL;
979 }
980
981 /* use regulator's subset of machine constraints */
982 if (constraints->min_uV < min_uV) {
983 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
984 constraints->min_uV, min_uV);
985 constraints->min_uV = min_uV;
986 }
987 if (constraints->max_uV > max_uV) {
988 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
989 constraints->max_uV, max_uV);
990 constraints->max_uV = max_uV;
991 }
992 }
993
994 return 0;
995}
996
997static int machine_constraints_current(struct regulator_dev *rdev,
998 struct regulation_constraints *constraints)
999{
1000 const struct regulator_ops *ops = rdev->desc->ops;
1001 int ret;
1002
1003 if (!constraints->min_uA && !constraints->max_uA)
1004 return 0;
1005
1006 if (constraints->min_uA > constraints->max_uA) {
1007 rdev_err(rdev, "Invalid current constraints\n");
1008 return -EINVAL;
1009 }
1010
1011 if (!ops->set_current_limit || !ops->get_current_limit) {
1012 rdev_warn(rdev, "Operation of current configuration missing\n");
1013 return 0;
1014 }
1015
1016 /* Set regulator current in constraints range */
1017 ret = ops->set_current_limit(rdev, constraints->min_uA,
1018 constraints->max_uA);
1019 if (ret < 0) {
1020 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1021 return ret;
1022 }
1023
1024 return 0;
1025}
1026
1027static int _regulator_do_enable(struct regulator_dev *rdev);
1028
1029/**
1030 * set_machine_constraints - sets regulator constraints
1031 * @rdev: regulator source
1032 * @constraints: constraints to apply
1033 *
1034 * Allows platform initialisation code to define and constrain
1035 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1036 * Constraints *must* be set by platform code in order for some
1037 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1038 * set_mode.
1039 */
1040static int set_machine_constraints(struct regulator_dev *rdev,
1041 const struct regulation_constraints *constraints)
1042{
1043 int ret = 0;
1044 const struct regulator_ops *ops = rdev->desc->ops;
1045
1046 if (constraints)
1047 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1048 GFP_KERNEL);
1049 else
1050 rdev->constraints = kzalloc(sizeof(*constraints),
1051 GFP_KERNEL);
1052 if (!rdev->constraints)
1053 return -ENOMEM;
1054
1055 ret = machine_constraints_voltage(rdev, rdev->constraints);
1056 if (ret != 0)
1057 return ret;
1058
1059 ret = machine_constraints_current(rdev, rdev->constraints);
1060 if (ret != 0)
1061 return ret;
1062
1063 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1064 ret = ops->set_input_current_limit(rdev,
1065 rdev->constraints->ilim_uA);
1066 if (ret < 0) {
1067 rdev_err(rdev, "failed to set input limit\n");
1068 return ret;
1069 }
1070 }
1071
1072 /* do we need to setup our suspend state */
1073 if (rdev->constraints->initial_state) {
1074 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1075 if (ret < 0) {
1076 rdev_err(rdev, "failed to set suspend state\n");
1077 return ret;
1078 }
1079 }
1080
1081 if (rdev->constraints->initial_mode) {
1082 if (!ops->set_mode) {
1083 rdev_err(rdev, "no set_mode operation\n");
1084 return -EINVAL;
1085 }
1086
1087 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1088 if (ret < 0) {
1089 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1090 return ret;
1091 }
1092 }
1093
1094 /* If the constraints say the regulator should be on at this point
1095 * and we have control then make sure it is enabled.
1096 */
1097 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1098 ret = _regulator_do_enable(rdev);
1099 if (ret < 0 && ret != -EINVAL) {
1100 rdev_err(rdev, "failed to enable\n");
1101 return ret;
1102 }
1103 }
1104
1105 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1106 && ops->set_ramp_delay) {
1107 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1108 if (ret < 0) {
1109 rdev_err(rdev, "failed to set ramp_delay\n");
1110 return ret;
1111 }
1112 }
1113
1114 if (rdev->constraints->pull_down && ops->set_pull_down) {
1115 ret = ops->set_pull_down(rdev);
1116 if (ret < 0) {
1117 rdev_err(rdev, "failed to set pull down\n");
1118 return ret;
1119 }
1120 }
1121
1122 if (rdev->constraints->soft_start && ops->set_soft_start) {
1123 ret = ops->set_soft_start(rdev);
1124 if (ret < 0) {
1125 rdev_err(rdev, "failed to set soft start\n");
1126 return ret;
1127 }
1128 }
1129
1130 if (rdev->constraints->over_current_protection
1131 && ops->set_over_current_protection) {
1132 ret = ops->set_over_current_protection(rdev);
1133 if (ret < 0) {
1134 rdev_err(rdev, "failed to set over current protection\n");
1135 return ret;
1136 }
1137 }
1138
1139 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1140 bool ad_state = (rdev->constraints->active_discharge ==
1141 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1142
1143 ret = ops->set_active_discharge(rdev, ad_state);
1144 if (ret < 0) {
1145 rdev_err(rdev, "failed to set active discharge\n");
1146 return ret;
1147 }
1148 }
1149
1150 print_constraints(rdev);
1151 return 0;
1152}
1153
1154/**
1155 * set_supply - set regulator supply regulator
1156 * @rdev: regulator name
1157 * @supply_rdev: supply regulator name
1158 *
1159 * Called by platform initialisation code to set the supply regulator for this
1160 * regulator. This ensures that a regulators supply will also be enabled by the
1161 * core if it's child is enabled.
1162 */
1163static int set_supply(struct regulator_dev *rdev,
1164 struct regulator_dev *supply_rdev)
1165{
1166 int err;
1167
1168 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1169
1170 if (!try_module_get(supply_rdev->owner))
1171 return -ENODEV;
1172
1173 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1174 if (rdev->supply == NULL) {
1175 err = -ENOMEM;
1176 return err;
1177 }
1178 supply_rdev->open_count++;
1179
1180 return 0;
1181}
1182
1183/**
1184 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1185 * @rdev: regulator source
1186 * @consumer_dev_name: dev_name() string for device supply applies to
1187 * @supply: symbolic name for supply
1188 *
1189 * Allows platform initialisation code to map physical regulator
1190 * sources to symbolic names for supplies for use by devices. Devices
1191 * should use these symbolic names to request regulators, avoiding the
1192 * need to provide board-specific regulator names as platform data.
1193 */
1194static int set_consumer_device_supply(struct regulator_dev *rdev,
1195 const char *consumer_dev_name,
1196 const char *supply)
1197{
1198 struct regulator_map *node;
1199 int has_dev;
1200
1201 if (supply == NULL)
1202 return -EINVAL;
1203
1204 if (consumer_dev_name != NULL)
1205 has_dev = 1;
1206 else
1207 has_dev = 0;
1208
1209 list_for_each_entry(node, ®ulator_map_list, list) {
1210 if (node->dev_name && consumer_dev_name) {
1211 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1212 continue;
1213 } else if (node->dev_name || consumer_dev_name) {
1214 continue;
1215 }
1216
1217 if (strcmp(node->supply, supply) != 0)
1218 continue;
1219
1220 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1221 consumer_dev_name,
1222 dev_name(&node->regulator->dev),
1223 node->regulator->desc->name,
1224 supply,
1225 dev_name(&rdev->dev), rdev_get_name(rdev));
1226 return -EBUSY;
1227 }
1228
1229 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1230 if (node == NULL)
1231 return -ENOMEM;
1232
1233 node->regulator = rdev;
1234 node->supply = supply;
1235
1236 if (has_dev) {
1237 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1238 if (node->dev_name == NULL) {
1239 kfree(node);
1240 return -ENOMEM;
1241 }
1242 }
1243
1244 list_add(&node->list, ®ulator_map_list);
1245 return 0;
1246}
1247
1248static void unset_regulator_supplies(struct regulator_dev *rdev)
1249{
1250 struct regulator_map *node, *n;
1251
1252 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1253 if (rdev == node->regulator) {
1254 list_del(&node->list);
1255 kfree(node->dev_name);
1256 kfree(node);
1257 }
1258 }
1259}
1260
1261#ifdef CONFIG_DEBUG_FS
1262static ssize_t constraint_flags_read_file(struct file *file,
1263 char __user *user_buf,
1264 size_t count, loff_t *ppos)
1265{
1266 const struct regulator *regulator = file->private_data;
1267 const struct regulation_constraints *c = regulator->rdev->constraints;
1268 char *buf;
1269 ssize_t ret;
1270
1271 if (!c)
1272 return 0;
1273
1274 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1275 if (!buf)
1276 return -ENOMEM;
1277
1278 ret = snprintf(buf, PAGE_SIZE,
1279 "always_on: %u\n"
1280 "boot_on: %u\n"
1281 "apply_uV: %u\n"
1282 "ramp_disable: %u\n"
1283 "soft_start: %u\n"
1284 "pull_down: %u\n"
1285 "over_current_protection: %u\n",
1286 c->always_on,
1287 c->boot_on,
1288 c->apply_uV,
1289 c->ramp_disable,
1290 c->soft_start,
1291 c->pull_down,
1292 c->over_current_protection);
1293
1294 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1295 kfree(buf);
1296
1297 return ret;
1298}
1299
1300#endif
1301
1302static const struct file_operations constraint_flags_fops = {
1303#ifdef CONFIG_DEBUG_FS
1304 .open = simple_open,
1305 .read = constraint_flags_read_file,
1306 .llseek = default_llseek,
1307#endif
1308};
1309
1310#define REG_STR_SIZE 64
1311
1312static struct regulator *create_regulator(struct regulator_dev *rdev,
1313 struct device *dev,
1314 const char *supply_name)
1315{
1316 struct regulator *regulator;
1317 char buf[REG_STR_SIZE];
1318 int err, size;
1319
1320 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1321 if (regulator == NULL)
1322 return NULL;
1323
1324 mutex_lock(&rdev->mutex);
1325 regulator->rdev = rdev;
1326 list_add(®ulator->list, &rdev->consumer_list);
1327
1328 if (dev) {
1329 regulator->dev = dev;
1330
1331 /* Add a link to the device sysfs entry */
1332 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1333 dev->kobj.name, supply_name);
1334 if (size >= REG_STR_SIZE)
1335 goto overflow_err;
1336
1337 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1338 if (regulator->supply_name == NULL)
1339 goto overflow_err;
1340
1341 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1342 buf);
1343 if (err) {
1344 rdev_dbg(rdev, "could not add device link %s err %d\n",
1345 dev->kobj.name, err);
1346 /* non-fatal */
1347 }
1348 } else {
1349 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1350 if (regulator->supply_name == NULL)
1351 goto overflow_err;
1352 }
1353
1354 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1355 rdev->debugfs);
1356 if (!regulator->debugfs) {
1357 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1358 } else {
1359 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1360 ®ulator->uA_load);
1361 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1362 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1363 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1364 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1365 debugfs_create_file("constraint_flags", 0444,
1366 regulator->debugfs, regulator,
1367 &constraint_flags_fops);
1368 }
1369
1370 /*
1371 * Check now if the regulator is an always on regulator - if
1372 * it is then we don't need to do nearly so much work for
1373 * enable/disable calls.
1374 */
1375 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1376 _regulator_is_enabled(rdev))
1377 regulator->always_on = true;
1378
1379 mutex_unlock(&rdev->mutex);
1380 return regulator;
1381overflow_err:
1382 list_del(®ulator->list);
1383 kfree(regulator);
1384 mutex_unlock(&rdev->mutex);
1385 return NULL;
1386}
1387
1388static int _regulator_get_enable_time(struct regulator_dev *rdev)
1389{
1390 if (rdev->constraints && rdev->constraints->enable_time)
1391 return rdev->constraints->enable_time;
1392 if (!rdev->desc->ops->enable_time)
1393 return rdev->desc->enable_time;
1394 return rdev->desc->ops->enable_time(rdev);
1395}
1396
1397static struct regulator_supply_alias *regulator_find_supply_alias(
1398 struct device *dev, const char *supply)
1399{
1400 struct regulator_supply_alias *map;
1401
1402 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1403 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1404 return map;
1405
1406 return NULL;
1407}
1408
1409static void regulator_supply_alias(struct device **dev, const char **supply)
1410{
1411 struct regulator_supply_alias *map;
1412
1413 map = regulator_find_supply_alias(*dev, *supply);
1414 if (map) {
1415 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1416 *supply, map->alias_supply,
1417 dev_name(map->alias_dev));
1418 *dev = map->alias_dev;
1419 *supply = map->alias_supply;
1420 }
1421}
1422
1423static int regulator_match(struct device *dev, const void *data)
1424{
1425 struct regulator_dev *r = dev_to_rdev(dev);
1426
1427 return strcmp(rdev_get_name(r), data) == 0;
1428}
1429
1430static struct regulator_dev *regulator_lookup_by_name(const char *name)
1431{
1432 struct device *dev;
1433
1434 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1435
1436 return dev ? dev_to_rdev(dev) : NULL;
1437}
1438
1439/**
1440 * regulator_dev_lookup - lookup a regulator device.
1441 * @dev: device for regulator "consumer".
1442 * @supply: Supply name or regulator ID.
1443 *
1444 * If successful, returns a struct regulator_dev that corresponds to the name
1445 * @supply and with the embedded struct device refcount incremented by one.
1446 * The refcount must be dropped by calling put_device().
1447 * On failure one of the following ERR-PTR-encoded values is returned:
1448 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1449 * in the future.
1450 */
1451static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1452 const char *supply)
1453{
1454 struct regulator_dev *r = NULL;
1455 struct device_node *node;
1456 struct regulator_map *map;
1457 const char *devname = NULL;
1458
1459 regulator_supply_alias(&dev, &supply);
1460
1461 /* first do a dt based lookup */
1462 if (dev && dev->of_node) {
1463 node = of_get_regulator(dev, supply);
1464 if (node) {
1465 r = of_find_regulator_by_node(node);
1466 if (r)
1467 return r;
1468
1469 /*
1470 * We have a node, but there is no device.
1471 * assume it has not registered yet.
1472 */
1473 return ERR_PTR(-EPROBE_DEFER);
1474 }
1475 }
1476
1477 /* if not found, try doing it non-dt way */
1478 if (dev)
1479 devname = dev_name(dev);
1480
1481 mutex_lock(®ulator_list_mutex);
1482 list_for_each_entry(map, ®ulator_map_list, list) {
1483 /* If the mapping has a device set up it must match */
1484 if (map->dev_name &&
1485 (!devname || strcmp(map->dev_name, devname)))
1486 continue;
1487
1488 if (strcmp(map->supply, supply) == 0 &&
1489 get_device(&map->regulator->dev)) {
1490 r = map->regulator;
1491 break;
1492 }
1493 }
1494 mutex_unlock(®ulator_list_mutex);
1495
1496 if (r)
1497 return r;
1498
1499 r = regulator_lookup_by_name(supply);
1500 if (r)
1501 return r;
1502
1503 return ERR_PTR(-ENODEV);
1504}
1505
1506static int regulator_resolve_supply(struct regulator_dev *rdev)
1507{
1508 struct regulator_dev *r;
1509 struct device *dev = rdev->dev.parent;
1510 int ret;
1511
1512 /* No supply to resovle? */
1513 if (!rdev->supply_name)
1514 return 0;
1515
1516 /* Supply already resolved? */
1517 if (rdev->supply)
1518 return 0;
1519
1520 r = regulator_dev_lookup(dev, rdev->supply_name);
1521 if (IS_ERR(r)) {
1522 ret = PTR_ERR(r);
1523
1524 /* Did the lookup explicitly defer for us? */
1525 if (ret == -EPROBE_DEFER)
1526 return ret;
1527
1528 if (have_full_constraints()) {
1529 r = dummy_regulator_rdev;
1530 get_device(&r->dev);
1531 } else {
1532 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1533 rdev->supply_name, rdev->desc->name);
1534 return -EPROBE_DEFER;
1535 }
1536 }
1537
1538 /*
1539 * If the supply's parent device is not the same as the
1540 * regulator's parent device, then ensure the parent device
1541 * is bound before we resolve the supply, in case the parent
1542 * device get probe deferred and unregisters the supply.
1543 */
1544 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1545 if (!device_is_bound(r->dev.parent)) {
1546 put_device(&r->dev);
1547 return -EPROBE_DEFER;
1548 }
1549 }
1550
1551 /* Recursively resolve the supply of the supply */
1552 ret = regulator_resolve_supply(r);
1553 if (ret < 0) {
1554 put_device(&r->dev);
1555 return ret;
1556 }
1557
1558 ret = set_supply(rdev, r);
1559 if (ret < 0) {
1560 put_device(&r->dev);
1561 return ret;
1562 }
1563
1564 /* Cascade always-on state to supply */
1565 if (_regulator_is_enabled(rdev)) {
1566 ret = regulator_enable(rdev->supply);
1567 if (ret < 0) {
1568 _regulator_put(rdev->supply);
1569 rdev->supply = NULL;
1570 return ret;
1571 }
1572 }
1573
1574 return 0;
1575}
1576
1577/* Internal regulator request function */
1578struct regulator *_regulator_get(struct device *dev, const char *id,
1579 enum regulator_get_type get_type)
1580{
1581 struct regulator_dev *rdev;
1582 struct regulator *regulator;
1583 const char *devname = dev ? dev_name(dev) : "deviceless";
1584 int ret;
1585
1586 if (get_type >= MAX_GET_TYPE) {
1587 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1588 return ERR_PTR(-EINVAL);
1589 }
1590
1591 if (id == NULL) {
1592 pr_err("get() with no identifier\n");
1593 return ERR_PTR(-EINVAL);
1594 }
1595
1596 rdev = regulator_dev_lookup(dev, id);
1597 if (IS_ERR(rdev)) {
1598 ret = PTR_ERR(rdev);
1599
1600 /*
1601 * If regulator_dev_lookup() fails with error other
1602 * than -ENODEV our job here is done, we simply return it.
1603 */
1604 if (ret != -ENODEV)
1605 return ERR_PTR(ret);
1606
1607 if (!have_full_constraints()) {
1608 dev_warn(dev,
1609 "incomplete constraints, dummy supplies not allowed\n");
1610 return ERR_PTR(-ENODEV);
1611 }
1612
1613 switch (get_type) {
1614 case NORMAL_GET:
1615 /*
1616 * Assume that a regulator is physically present and
1617 * enabled, even if it isn't hooked up, and just
1618 * provide a dummy.
1619 */
1620 dev_warn(dev,
1621 "%s supply %s not found, using dummy regulator\n",
1622 devname, id);
1623 rdev = dummy_regulator_rdev;
1624 get_device(&rdev->dev);
1625 break;
1626
1627 case EXCLUSIVE_GET:
1628 dev_warn(dev,
1629 "dummy supplies not allowed for exclusive requests\n");
1630 /* fall through */
1631
1632 default:
1633 return ERR_PTR(-ENODEV);
1634 }
1635 }
1636
1637 if (rdev->exclusive) {
1638 regulator = ERR_PTR(-EPERM);
1639 put_device(&rdev->dev);
1640 return regulator;
1641 }
1642
1643 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1644 regulator = ERR_PTR(-EBUSY);
1645 put_device(&rdev->dev);
1646 return regulator;
1647 }
1648
1649 ret = regulator_resolve_supply(rdev);
1650 if (ret < 0) {
1651 regulator = ERR_PTR(ret);
1652 put_device(&rdev->dev);
1653 return regulator;
1654 }
1655
1656 if (!try_module_get(rdev->owner)) {
1657 regulator = ERR_PTR(-EPROBE_DEFER);
1658 put_device(&rdev->dev);
1659 return regulator;
1660 }
1661
1662 regulator = create_regulator(rdev, dev, id);
1663 if (regulator == NULL) {
1664 regulator = ERR_PTR(-ENOMEM);
1665 put_device(&rdev->dev);
1666 module_put(rdev->owner);
1667 return regulator;
1668 }
1669
1670 rdev->open_count++;
1671 if (get_type == EXCLUSIVE_GET) {
1672 rdev->exclusive = 1;
1673
1674 ret = _regulator_is_enabled(rdev);
1675 if (ret > 0)
1676 rdev->use_count = 1;
1677 else
1678 rdev->use_count = 0;
1679 }
1680
1681 return regulator;
1682}
1683
1684/**
1685 * regulator_get - lookup and obtain a reference to a regulator.
1686 * @dev: device for regulator "consumer"
1687 * @id: Supply name or regulator ID.
1688 *
1689 * Returns a struct regulator corresponding to the regulator producer,
1690 * or IS_ERR() condition containing errno.
1691 *
1692 * Use of supply names configured via regulator_set_device_supply() is
1693 * strongly encouraged. It is recommended that the supply name used
1694 * should match the name used for the supply and/or the relevant
1695 * device pins in the datasheet.
1696 */
1697struct regulator *regulator_get(struct device *dev, const char *id)
1698{
1699 return _regulator_get(dev, id, NORMAL_GET);
1700}
1701EXPORT_SYMBOL_GPL(regulator_get);
1702
1703/**
1704 * regulator_get_exclusive - obtain exclusive access to a regulator.
1705 * @dev: device for regulator "consumer"
1706 * @id: Supply name or regulator ID.
1707 *
1708 * Returns a struct regulator corresponding to the regulator producer,
1709 * or IS_ERR() condition containing errno. Other consumers will be
1710 * unable to obtain this regulator while this reference is held and the
1711 * use count for the regulator will be initialised to reflect the current
1712 * state of the regulator.
1713 *
1714 * This is intended for use by consumers which cannot tolerate shared
1715 * use of the regulator such as those which need to force the
1716 * regulator off for correct operation of the hardware they are
1717 * controlling.
1718 *
1719 * Use of supply names configured via regulator_set_device_supply() is
1720 * strongly encouraged. It is recommended that the supply name used
1721 * should match the name used for the supply and/or the relevant
1722 * device pins in the datasheet.
1723 */
1724struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1725{
1726 return _regulator_get(dev, id, EXCLUSIVE_GET);
1727}
1728EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1729
1730/**
1731 * regulator_get_optional - obtain optional access to a regulator.
1732 * @dev: device for regulator "consumer"
1733 * @id: Supply name or regulator ID.
1734 *
1735 * Returns a struct regulator corresponding to the regulator producer,
1736 * or IS_ERR() condition containing errno.
1737 *
1738 * This is intended for use by consumers for devices which can have
1739 * some supplies unconnected in normal use, such as some MMC devices.
1740 * It can allow the regulator core to provide stub supplies for other
1741 * supplies requested using normal regulator_get() calls without
1742 * disrupting the operation of drivers that can handle absent
1743 * supplies.
1744 *
1745 * Use of supply names configured via regulator_set_device_supply() is
1746 * strongly encouraged. It is recommended that the supply name used
1747 * should match the name used for the supply and/or the relevant
1748 * device pins in the datasheet.
1749 */
1750struct regulator *regulator_get_optional(struct device *dev, const char *id)
1751{
1752 return _regulator_get(dev, id, OPTIONAL_GET);
1753}
1754EXPORT_SYMBOL_GPL(regulator_get_optional);
1755
1756/* regulator_list_mutex lock held by regulator_put() */
1757static void _regulator_put(struct regulator *regulator)
1758{
1759 struct regulator_dev *rdev;
1760
1761 if (IS_ERR_OR_NULL(regulator))
1762 return;
1763
1764 lockdep_assert_held_once(®ulator_list_mutex);
1765
1766 rdev = regulator->rdev;
1767
1768 debugfs_remove_recursive(regulator->debugfs);
1769
1770 /* remove any sysfs entries */
1771 if (regulator->dev)
1772 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1773 mutex_lock(&rdev->mutex);
1774 list_del(®ulator->list);
1775
1776 rdev->open_count--;
1777 rdev->exclusive = 0;
1778 put_device(&rdev->dev);
1779 mutex_unlock(&rdev->mutex);
1780
1781 kfree_const(regulator->supply_name);
1782 kfree(regulator);
1783
1784 module_put(rdev->owner);
1785}
1786
1787/**
1788 * regulator_put - "free" the regulator source
1789 * @regulator: regulator source
1790 *
1791 * Note: drivers must ensure that all regulator_enable calls made on this
1792 * regulator source are balanced by regulator_disable calls prior to calling
1793 * this function.
1794 */
1795void regulator_put(struct regulator *regulator)
1796{
1797 mutex_lock(®ulator_list_mutex);
1798 _regulator_put(regulator);
1799 mutex_unlock(®ulator_list_mutex);
1800}
1801EXPORT_SYMBOL_GPL(regulator_put);
1802
1803/**
1804 * regulator_register_supply_alias - Provide device alias for supply lookup
1805 *
1806 * @dev: device that will be given as the regulator "consumer"
1807 * @id: Supply name or regulator ID
1808 * @alias_dev: device that should be used to lookup the supply
1809 * @alias_id: Supply name or regulator ID that should be used to lookup the
1810 * supply
1811 *
1812 * All lookups for id on dev will instead be conducted for alias_id on
1813 * alias_dev.
1814 */
1815int regulator_register_supply_alias(struct device *dev, const char *id,
1816 struct device *alias_dev,
1817 const char *alias_id)
1818{
1819 struct regulator_supply_alias *map;
1820
1821 map = regulator_find_supply_alias(dev, id);
1822 if (map)
1823 return -EEXIST;
1824
1825 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1826 if (!map)
1827 return -ENOMEM;
1828
1829 map->src_dev = dev;
1830 map->src_supply = id;
1831 map->alias_dev = alias_dev;
1832 map->alias_supply = alias_id;
1833
1834 list_add(&map->list, ®ulator_supply_alias_list);
1835
1836 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1837 id, dev_name(dev), alias_id, dev_name(alias_dev));
1838
1839 return 0;
1840}
1841EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1842
1843/**
1844 * regulator_unregister_supply_alias - Remove device alias
1845 *
1846 * @dev: device that will be given as the regulator "consumer"
1847 * @id: Supply name or regulator ID
1848 *
1849 * Remove a lookup alias if one exists for id on dev.
1850 */
1851void regulator_unregister_supply_alias(struct device *dev, const char *id)
1852{
1853 struct regulator_supply_alias *map;
1854
1855 map = regulator_find_supply_alias(dev, id);
1856 if (map) {
1857 list_del(&map->list);
1858 kfree(map);
1859 }
1860}
1861EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1862
1863/**
1864 * regulator_bulk_register_supply_alias - register multiple aliases
1865 *
1866 * @dev: device that will be given as the regulator "consumer"
1867 * @id: List of supply names or regulator IDs
1868 * @alias_dev: device that should be used to lookup the supply
1869 * @alias_id: List of supply names or regulator IDs that should be used to
1870 * lookup the supply
1871 * @num_id: Number of aliases to register
1872 *
1873 * @return 0 on success, an errno on failure.
1874 *
1875 * This helper function allows drivers to register several supply
1876 * aliases in one operation. If any of the aliases cannot be
1877 * registered any aliases that were registered will be removed
1878 * before returning to the caller.
1879 */
1880int regulator_bulk_register_supply_alias(struct device *dev,
1881 const char *const *id,
1882 struct device *alias_dev,
1883 const char *const *alias_id,
1884 int num_id)
1885{
1886 int i;
1887 int ret;
1888
1889 for (i = 0; i < num_id; ++i) {
1890 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1891 alias_id[i]);
1892 if (ret < 0)
1893 goto err;
1894 }
1895
1896 return 0;
1897
1898err:
1899 dev_err(dev,
1900 "Failed to create supply alias %s,%s -> %s,%s\n",
1901 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1902
1903 while (--i >= 0)
1904 regulator_unregister_supply_alias(dev, id[i]);
1905
1906 return ret;
1907}
1908EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1909
1910/**
1911 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1912 *
1913 * @dev: device that will be given as the regulator "consumer"
1914 * @id: List of supply names or regulator IDs
1915 * @num_id: Number of aliases to unregister
1916 *
1917 * This helper function allows drivers to unregister several supply
1918 * aliases in one operation.
1919 */
1920void regulator_bulk_unregister_supply_alias(struct device *dev,
1921 const char *const *id,
1922 int num_id)
1923{
1924 int i;
1925
1926 for (i = 0; i < num_id; ++i)
1927 regulator_unregister_supply_alias(dev, id[i]);
1928}
1929EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1930
1931
1932/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1933static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1934 const struct regulator_config *config)
1935{
1936 struct regulator_enable_gpio *pin;
1937 struct gpio_desc *gpiod;
1938 int ret;
1939
1940 if (config->ena_gpiod)
1941 gpiod = config->ena_gpiod;
1942 else
1943 gpiod = gpio_to_desc(config->ena_gpio);
1944
1945 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1946 if (pin->gpiod == gpiod) {
1947 rdev_dbg(rdev, "GPIO %d is already used\n",
1948 config->ena_gpio);
1949 goto update_ena_gpio_to_rdev;
1950 }
1951 }
1952
1953 if (!config->ena_gpiod) {
1954 ret = gpio_request_one(config->ena_gpio,
1955 GPIOF_DIR_OUT | config->ena_gpio_flags,
1956 rdev_get_name(rdev));
1957 if (ret)
1958 return ret;
1959 }
1960
1961 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1962 if (pin == NULL) {
1963 if (!config->ena_gpiod)
1964 gpio_free(config->ena_gpio);
1965 return -ENOMEM;
1966 }
1967
1968 pin->gpiod = gpiod;
1969 pin->ena_gpio_invert = config->ena_gpio_invert;
1970 list_add(&pin->list, ®ulator_ena_gpio_list);
1971
1972update_ena_gpio_to_rdev:
1973 pin->request_count++;
1974 rdev->ena_pin = pin;
1975 return 0;
1976}
1977
1978static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1979{
1980 struct regulator_enable_gpio *pin, *n;
1981
1982 if (!rdev->ena_pin)
1983 return;
1984
1985 /* Free the GPIO only in case of no use */
1986 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1987 if (pin->gpiod == rdev->ena_pin->gpiod) {
1988 if (pin->request_count <= 1) {
1989 pin->request_count = 0;
1990 gpiod_put(pin->gpiod);
1991 list_del(&pin->list);
1992 kfree(pin);
1993 rdev->ena_pin = NULL;
1994 return;
1995 } else {
1996 pin->request_count--;
1997 }
1998 }
1999 }
2000}
2001
2002/**
2003 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2004 * @rdev: regulator_dev structure
2005 * @enable: enable GPIO at initial use?
2006 *
2007 * GPIO is enabled in case of initial use. (enable_count is 0)
2008 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2009 */
2010static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2011{
2012 struct regulator_enable_gpio *pin = rdev->ena_pin;
2013
2014 if (!pin)
2015 return -EINVAL;
2016
2017 if (enable) {
2018 /* Enable GPIO at initial use */
2019 if (pin->enable_count == 0)
2020 gpiod_set_value_cansleep(pin->gpiod,
2021 !pin->ena_gpio_invert);
2022
2023 pin->enable_count++;
2024 } else {
2025 if (pin->enable_count > 1) {
2026 pin->enable_count--;
2027 return 0;
2028 }
2029
2030 /* Disable GPIO if not used */
2031 if (pin->enable_count <= 1) {
2032 gpiod_set_value_cansleep(pin->gpiod,
2033 pin->ena_gpio_invert);
2034 pin->enable_count = 0;
2035 }
2036 }
2037
2038 return 0;
2039}
2040
2041/**
2042 * _regulator_enable_delay - a delay helper function
2043 * @delay: time to delay in microseconds
2044 *
2045 * Delay for the requested amount of time as per the guidelines in:
2046 *
2047 * Documentation/timers/timers-howto.txt
2048 *
2049 * The assumption here is that regulators will never be enabled in
2050 * atomic context and therefore sleeping functions can be used.
2051 */
2052static void _regulator_enable_delay(unsigned int delay)
2053{
2054 unsigned int ms = delay / 1000;
2055 unsigned int us = delay % 1000;
2056
2057 if (ms > 0) {
2058 /*
2059 * For small enough values, handle super-millisecond
2060 * delays in the usleep_range() call below.
2061 */
2062 if (ms < 20)
2063 us += ms * 1000;
2064 else
2065 msleep(ms);
2066 }
2067
2068 /*
2069 * Give the scheduler some room to coalesce with any other
2070 * wakeup sources. For delays shorter than 10 us, don't even
2071 * bother setting up high-resolution timers and just busy-
2072 * loop.
2073 */
2074 if (us >= 10)
2075 usleep_range(us, us + 100);
2076 else
2077 udelay(us);
2078}
2079
2080static int _regulator_do_enable(struct regulator_dev *rdev)
2081{
2082 int ret, delay;
2083
2084 /* Query before enabling in case configuration dependent. */
2085 ret = _regulator_get_enable_time(rdev);
2086 if (ret >= 0) {
2087 delay = ret;
2088 } else {
2089 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2090 delay = 0;
2091 }
2092
2093 trace_regulator_enable(rdev_get_name(rdev));
2094
2095 if (rdev->desc->off_on_delay) {
2096 /* if needed, keep a distance of off_on_delay from last time
2097 * this regulator was disabled.
2098 */
2099 unsigned long start_jiffy = jiffies;
2100 unsigned long intended, max_delay, remaining;
2101
2102 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2103 intended = rdev->last_off_jiffy + max_delay;
2104
2105 if (time_before(start_jiffy, intended)) {
2106 /* calc remaining jiffies to deal with one-time
2107 * timer wrapping.
2108 * in case of multiple timer wrapping, either it can be
2109 * detected by out-of-range remaining, or it cannot be
2110 * detected and we gets a panelty of
2111 * _regulator_enable_delay().
2112 */
2113 remaining = intended - start_jiffy;
2114 if (remaining <= max_delay)
2115 _regulator_enable_delay(
2116 jiffies_to_usecs(remaining));
2117 }
2118 }
2119
2120 if (rdev->ena_pin) {
2121 if (!rdev->ena_gpio_state) {
2122 ret = regulator_ena_gpio_ctrl(rdev, true);
2123 if (ret < 0)
2124 return ret;
2125 rdev->ena_gpio_state = 1;
2126 }
2127 } else if (rdev->desc->ops->enable) {
2128 ret = rdev->desc->ops->enable(rdev);
2129 if (ret < 0)
2130 return ret;
2131 } else {
2132 return -EINVAL;
2133 }
2134
2135 /* Allow the regulator to ramp; it would be useful to extend
2136 * this for bulk operations so that the regulators can ramp
2137 * together. */
2138 trace_regulator_enable_delay(rdev_get_name(rdev));
2139
2140 _regulator_enable_delay(delay);
2141
2142 trace_regulator_enable_complete(rdev_get_name(rdev));
2143
2144 return 0;
2145}
2146
2147/* locks held by regulator_enable() */
2148static int _regulator_enable(struct regulator_dev *rdev)
2149{
2150 int ret;
2151
2152 lockdep_assert_held_once(&rdev->mutex);
2153
2154 /* check voltage and requested load before enabling */
2155 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2156 drms_uA_update(rdev);
2157
2158 if (rdev->use_count == 0) {
2159 /* The regulator may on if it's not switchable or left on */
2160 ret = _regulator_is_enabled(rdev);
2161 if (ret == -EINVAL || ret == 0) {
2162 if (!regulator_ops_is_valid(rdev,
2163 REGULATOR_CHANGE_STATUS))
2164 return -EPERM;
2165
2166 ret = _regulator_do_enable(rdev);
2167 if (ret < 0)
2168 return ret;
2169
2170 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2171 NULL);
2172 } else if (ret < 0) {
2173 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2174 return ret;
2175 }
2176 /* Fallthrough on positive return values - already enabled */
2177 }
2178
2179 rdev->use_count++;
2180
2181 return 0;
2182}
2183
2184/**
2185 * regulator_enable - enable regulator output
2186 * @regulator: regulator source
2187 *
2188 * Request that the regulator be enabled with the regulator output at
2189 * the predefined voltage or current value. Calls to regulator_enable()
2190 * must be balanced with calls to regulator_disable().
2191 *
2192 * NOTE: the output value can be set by other drivers, boot loader or may be
2193 * hardwired in the regulator.
2194 */
2195int regulator_enable(struct regulator *regulator)
2196{
2197 struct regulator_dev *rdev = regulator->rdev;
2198 int ret = 0;
2199
2200 if (regulator->always_on)
2201 return 0;
2202
2203 if (rdev->supply) {
2204 ret = regulator_enable(rdev->supply);
2205 if (ret != 0)
2206 return ret;
2207 }
2208
2209 mutex_lock(&rdev->mutex);
2210 ret = _regulator_enable(rdev);
2211 mutex_unlock(&rdev->mutex);
2212
2213 if (ret != 0 && rdev->supply)
2214 regulator_disable(rdev->supply);
2215
2216 return ret;
2217}
2218EXPORT_SYMBOL_GPL(regulator_enable);
2219
2220static int _regulator_do_disable(struct regulator_dev *rdev)
2221{
2222 int ret;
2223
2224 trace_regulator_disable(rdev_get_name(rdev));
2225
2226 if (rdev->ena_pin) {
2227 if (rdev->ena_gpio_state) {
2228 ret = regulator_ena_gpio_ctrl(rdev, false);
2229 if (ret < 0)
2230 return ret;
2231 rdev->ena_gpio_state = 0;
2232 }
2233
2234 } else if (rdev->desc->ops->disable) {
2235 ret = rdev->desc->ops->disable(rdev);
2236 if (ret != 0)
2237 return ret;
2238 }
2239
2240 /* cares about last_off_jiffy only if off_on_delay is required by
2241 * device.
2242 */
2243 if (rdev->desc->off_on_delay)
2244 rdev->last_off_jiffy = jiffies;
2245
2246 trace_regulator_disable_complete(rdev_get_name(rdev));
2247
2248 return 0;
2249}
2250
2251/* locks held by regulator_disable() */
2252static int _regulator_disable(struct regulator_dev *rdev)
2253{
2254 int ret = 0;
2255
2256 lockdep_assert_held_once(&rdev->mutex);
2257
2258 if (WARN(rdev->use_count <= 0,
2259 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2260 return -EIO;
2261
2262 /* are we the last user and permitted to disable ? */
2263 if (rdev->use_count == 1 &&
2264 (rdev->constraints && !rdev->constraints->always_on)) {
2265
2266 /* we are last user */
2267 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2268 ret = _notifier_call_chain(rdev,
2269 REGULATOR_EVENT_PRE_DISABLE,
2270 NULL);
2271 if (ret & NOTIFY_STOP_MASK)
2272 return -EINVAL;
2273
2274 ret = _regulator_do_disable(rdev);
2275 if (ret < 0) {
2276 rdev_err(rdev, "failed to disable\n");
2277 _notifier_call_chain(rdev,
2278 REGULATOR_EVENT_ABORT_DISABLE,
2279 NULL);
2280 return ret;
2281 }
2282 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2283 NULL);
2284 }
2285
2286 rdev->use_count = 0;
2287 } else if (rdev->use_count > 1) {
2288 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2289 drms_uA_update(rdev);
2290
2291 rdev->use_count--;
2292 }
2293
2294 return ret;
2295}
2296
2297/**
2298 * regulator_disable - disable regulator output
2299 * @regulator: regulator source
2300 *
2301 * Disable the regulator output voltage or current. Calls to
2302 * regulator_enable() must be balanced with calls to
2303 * regulator_disable().
2304 *
2305 * NOTE: this will only disable the regulator output if no other consumer
2306 * devices have it enabled, the regulator device supports disabling and
2307 * machine constraints permit this operation.
2308 */
2309int regulator_disable(struct regulator *regulator)
2310{
2311 struct regulator_dev *rdev = regulator->rdev;
2312 int ret = 0;
2313
2314 if (regulator->always_on)
2315 return 0;
2316
2317 mutex_lock(&rdev->mutex);
2318 ret = _regulator_disable(rdev);
2319 mutex_unlock(&rdev->mutex);
2320
2321 if (ret == 0 && rdev->supply)
2322 regulator_disable(rdev->supply);
2323
2324 return ret;
2325}
2326EXPORT_SYMBOL_GPL(regulator_disable);
2327
2328/* locks held by regulator_force_disable() */
2329static int _regulator_force_disable(struct regulator_dev *rdev)
2330{
2331 int ret = 0;
2332
2333 lockdep_assert_held_once(&rdev->mutex);
2334
2335 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2336 REGULATOR_EVENT_PRE_DISABLE, NULL);
2337 if (ret & NOTIFY_STOP_MASK)
2338 return -EINVAL;
2339
2340 ret = _regulator_do_disable(rdev);
2341 if (ret < 0) {
2342 rdev_err(rdev, "failed to force disable\n");
2343 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2344 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2345 return ret;
2346 }
2347
2348 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2349 REGULATOR_EVENT_DISABLE, NULL);
2350
2351 return 0;
2352}
2353
2354/**
2355 * regulator_force_disable - force disable regulator output
2356 * @regulator: regulator source
2357 *
2358 * Forcibly disable the regulator output voltage or current.
2359 * NOTE: this *will* disable the regulator output even if other consumer
2360 * devices have it enabled. This should be used for situations when device
2361 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2362 */
2363int regulator_force_disable(struct regulator *regulator)
2364{
2365 struct regulator_dev *rdev = regulator->rdev;
2366 int ret;
2367
2368 mutex_lock(&rdev->mutex);
2369 regulator->uA_load = 0;
2370 ret = _regulator_force_disable(regulator->rdev);
2371 mutex_unlock(&rdev->mutex);
2372
2373 if (rdev->supply)
2374 while (rdev->open_count--)
2375 regulator_disable(rdev->supply);
2376
2377 return ret;
2378}
2379EXPORT_SYMBOL_GPL(regulator_force_disable);
2380
2381static void regulator_disable_work(struct work_struct *work)
2382{
2383 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2384 disable_work.work);
2385 int count, i, ret;
2386
2387 mutex_lock(&rdev->mutex);
2388
2389 BUG_ON(!rdev->deferred_disables);
2390
2391 count = rdev->deferred_disables;
2392 rdev->deferred_disables = 0;
2393
2394 /*
2395 * Workqueue functions queue the new work instance while the previous
2396 * work instance is being processed. Cancel the queued work instance
2397 * as the work instance under processing does the job of the queued
2398 * work instance.
2399 */
2400 cancel_delayed_work(&rdev->disable_work);
2401
2402 for (i = 0; i < count; i++) {
2403 ret = _regulator_disable(rdev);
2404 if (ret != 0)
2405 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2406 }
2407
2408 mutex_unlock(&rdev->mutex);
2409
2410 if (rdev->supply) {
2411 for (i = 0; i < count; i++) {
2412 ret = regulator_disable(rdev->supply);
2413 if (ret != 0) {
2414 rdev_err(rdev,
2415 "Supply disable failed: %d\n", ret);
2416 }
2417 }
2418 }
2419}
2420
2421/**
2422 * regulator_disable_deferred - disable regulator output with delay
2423 * @regulator: regulator source
2424 * @ms: miliseconds until the regulator is disabled
2425 *
2426 * Execute regulator_disable() on the regulator after a delay. This
2427 * is intended for use with devices that require some time to quiesce.
2428 *
2429 * NOTE: this will only disable the regulator output if no other consumer
2430 * devices have it enabled, the regulator device supports disabling and
2431 * machine constraints permit this operation.
2432 */
2433int regulator_disable_deferred(struct regulator *regulator, int ms)
2434{
2435 struct regulator_dev *rdev = regulator->rdev;
2436
2437 if (regulator->always_on)
2438 return 0;
2439
2440 if (!ms)
2441 return regulator_disable(regulator);
2442
2443 mutex_lock(&rdev->mutex);
2444 rdev->deferred_disables++;
2445 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2446 msecs_to_jiffies(ms));
2447 mutex_unlock(&rdev->mutex);
2448
2449 return 0;
2450}
2451EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2452
2453static int _regulator_is_enabled(struct regulator_dev *rdev)
2454{
2455 /* A GPIO control always takes precedence */
2456 if (rdev->ena_pin)
2457 return rdev->ena_gpio_state;
2458
2459 /* If we don't know then assume that the regulator is always on */
2460 if (!rdev->desc->ops->is_enabled)
2461 return 1;
2462
2463 return rdev->desc->ops->is_enabled(rdev);
2464}
2465
2466static int _regulator_list_voltage(struct regulator_dev *rdev,
2467 unsigned selector, int lock)
2468{
2469 const struct regulator_ops *ops = rdev->desc->ops;
2470 int ret;
2471
2472 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2473 return rdev->desc->fixed_uV;
2474
2475 if (ops->list_voltage) {
2476 if (selector >= rdev->desc->n_voltages)
2477 return -EINVAL;
2478 if (lock)
2479 mutex_lock(&rdev->mutex);
2480 ret = ops->list_voltage(rdev, selector);
2481 if (lock)
2482 mutex_unlock(&rdev->mutex);
2483 } else if (rdev->is_switch && rdev->supply) {
2484 ret = _regulator_list_voltage(rdev->supply->rdev,
2485 selector, lock);
2486 } else {
2487 return -EINVAL;
2488 }
2489
2490 if (ret > 0) {
2491 if (ret < rdev->constraints->min_uV)
2492 ret = 0;
2493 else if (ret > rdev->constraints->max_uV)
2494 ret = 0;
2495 }
2496
2497 return ret;
2498}
2499
2500/**
2501 * regulator_is_enabled - is the regulator output enabled
2502 * @regulator: regulator source
2503 *
2504 * Returns positive if the regulator driver backing the source/client
2505 * has requested that the device be enabled, zero if it hasn't, else a
2506 * negative errno code.
2507 *
2508 * Note that the device backing this regulator handle can have multiple
2509 * users, so it might be enabled even if regulator_enable() was never
2510 * called for this particular source.
2511 */
2512int regulator_is_enabled(struct regulator *regulator)
2513{
2514 int ret;
2515
2516 if (regulator->always_on)
2517 return 1;
2518
2519 mutex_lock(®ulator->rdev->mutex);
2520 ret = _regulator_is_enabled(regulator->rdev);
2521 mutex_unlock(®ulator->rdev->mutex);
2522
2523 return ret;
2524}
2525EXPORT_SYMBOL_GPL(regulator_is_enabled);
2526
2527/**
2528 * regulator_count_voltages - count regulator_list_voltage() selectors
2529 * @regulator: regulator source
2530 *
2531 * Returns number of selectors, or negative errno. Selectors are
2532 * numbered starting at zero, and typically correspond to bitfields
2533 * in hardware registers.
2534 */
2535int regulator_count_voltages(struct regulator *regulator)
2536{
2537 struct regulator_dev *rdev = regulator->rdev;
2538
2539 if (rdev->desc->n_voltages)
2540 return rdev->desc->n_voltages;
2541
2542 if (!rdev->is_switch || !rdev->supply)
2543 return -EINVAL;
2544
2545 return regulator_count_voltages(rdev->supply);
2546}
2547EXPORT_SYMBOL_GPL(regulator_count_voltages);
2548
2549/**
2550 * regulator_list_voltage - enumerate supported voltages
2551 * @regulator: regulator source
2552 * @selector: identify voltage to list
2553 * Context: can sleep
2554 *
2555 * Returns a voltage that can be passed to @regulator_set_voltage(),
2556 * zero if this selector code can't be used on this system, or a
2557 * negative errno.
2558 */
2559int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2560{
2561 return _regulator_list_voltage(regulator->rdev, selector, 1);
2562}
2563EXPORT_SYMBOL_GPL(regulator_list_voltage);
2564
2565/**
2566 * regulator_get_regmap - get the regulator's register map
2567 * @regulator: regulator source
2568 *
2569 * Returns the register map for the given regulator, or an ERR_PTR value
2570 * if the regulator doesn't use regmap.
2571 */
2572struct regmap *regulator_get_regmap(struct regulator *regulator)
2573{
2574 struct regmap *map = regulator->rdev->regmap;
2575
2576 return map ? map : ERR_PTR(-EOPNOTSUPP);
2577}
2578
2579/**
2580 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2581 * @regulator: regulator source
2582 * @vsel_reg: voltage selector register, output parameter
2583 * @vsel_mask: mask for voltage selector bitfield, output parameter
2584 *
2585 * Returns the hardware register offset and bitmask used for setting the
2586 * regulator voltage. This might be useful when configuring voltage-scaling
2587 * hardware or firmware that can make I2C requests behind the kernel's back,
2588 * for example.
2589 *
2590 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2591 * and 0 is returned, otherwise a negative errno is returned.
2592 */
2593int regulator_get_hardware_vsel_register(struct regulator *regulator,
2594 unsigned *vsel_reg,
2595 unsigned *vsel_mask)
2596{
2597 struct regulator_dev *rdev = regulator->rdev;
2598 const struct regulator_ops *ops = rdev->desc->ops;
2599
2600 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2601 return -EOPNOTSUPP;
2602
2603 *vsel_reg = rdev->desc->vsel_reg;
2604 *vsel_mask = rdev->desc->vsel_mask;
2605
2606 return 0;
2607}
2608EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2609
2610/**
2611 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2612 * @regulator: regulator source
2613 * @selector: identify voltage to list
2614 *
2615 * Converts the selector to a hardware-specific voltage selector that can be
2616 * directly written to the regulator registers. The address of the voltage
2617 * register can be determined by calling @regulator_get_hardware_vsel_register.
2618 *
2619 * On error a negative errno is returned.
2620 */
2621int regulator_list_hardware_vsel(struct regulator *regulator,
2622 unsigned selector)
2623{
2624 struct regulator_dev *rdev = regulator->rdev;
2625 const struct regulator_ops *ops = rdev->desc->ops;
2626
2627 if (selector >= rdev->desc->n_voltages)
2628 return -EINVAL;
2629 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2630 return -EOPNOTSUPP;
2631
2632 return selector;
2633}
2634EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2635
2636/**
2637 * regulator_get_linear_step - return the voltage step size between VSEL values
2638 * @regulator: regulator source
2639 *
2640 * Returns the voltage step size between VSEL values for linear
2641 * regulators, or return 0 if the regulator isn't a linear regulator.
2642 */
2643unsigned int regulator_get_linear_step(struct regulator *regulator)
2644{
2645 struct regulator_dev *rdev = regulator->rdev;
2646
2647 return rdev->desc->uV_step;
2648}
2649EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2650
2651/**
2652 * regulator_is_supported_voltage - check if a voltage range can be supported
2653 *
2654 * @regulator: Regulator to check.
2655 * @min_uV: Minimum required voltage in uV.
2656 * @max_uV: Maximum required voltage in uV.
2657 *
2658 * Returns a boolean or a negative error code.
2659 */
2660int regulator_is_supported_voltage(struct regulator *regulator,
2661 int min_uV, int max_uV)
2662{
2663 struct regulator_dev *rdev = regulator->rdev;
2664 int i, voltages, ret;
2665
2666 /* If we can't change voltage check the current voltage */
2667 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2668 ret = regulator_get_voltage(regulator);
2669 if (ret >= 0)
2670 return min_uV <= ret && ret <= max_uV;
2671 else
2672 return ret;
2673 }
2674
2675 /* Any voltage within constrains range is fine? */
2676 if (rdev->desc->continuous_voltage_range)
2677 return min_uV >= rdev->constraints->min_uV &&
2678 max_uV <= rdev->constraints->max_uV;
2679
2680 ret = regulator_count_voltages(regulator);
2681 if (ret < 0)
2682 return ret;
2683 voltages = ret;
2684
2685 for (i = 0; i < voltages; i++) {
2686 ret = regulator_list_voltage(regulator, i);
2687
2688 if (ret >= min_uV && ret <= max_uV)
2689 return 1;
2690 }
2691
2692 return 0;
2693}
2694EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2695
2696static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2697 int max_uV)
2698{
2699 const struct regulator_desc *desc = rdev->desc;
2700
2701 if (desc->ops->map_voltage)
2702 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2703
2704 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2705 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2706
2707 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2708 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2709
2710 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2711}
2712
2713static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2714 int min_uV, int max_uV,
2715 unsigned *selector)
2716{
2717 struct pre_voltage_change_data data;
2718 int ret;
2719
2720 data.old_uV = _regulator_get_voltage(rdev);
2721 data.min_uV = min_uV;
2722 data.max_uV = max_uV;
2723 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2724 &data);
2725 if (ret & NOTIFY_STOP_MASK)
2726 return -EINVAL;
2727
2728 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2729 if (ret >= 0)
2730 return ret;
2731
2732 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2733 (void *)data.old_uV);
2734
2735 return ret;
2736}
2737
2738static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2739 int uV, unsigned selector)
2740{
2741 struct pre_voltage_change_data data;
2742 int ret;
2743
2744 data.old_uV = _regulator_get_voltage(rdev);
2745 data.min_uV = uV;
2746 data.max_uV = uV;
2747 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2748 &data);
2749 if (ret & NOTIFY_STOP_MASK)
2750 return -EINVAL;
2751
2752 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2753 if (ret >= 0)
2754 return ret;
2755
2756 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2757 (void *)data.old_uV);
2758
2759 return ret;
2760}
2761
2762static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2763 int old_uV, int new_uV)
2764{
2765 unsigned int ramp_delay = 0;
2766
2767 if (rdev->constraints->ramp_delay)
2768 ramp_delay = rdev->constraints->ramp_delay;
2769 else if (rdev->desc->ramp_delay)
2770 ramp_delay = rdev->desc->ramp_delay;
2771 else if (rdev->constraints->settling_time)
2772 return rdev->constraints->settling_time;
2773 else if (rdev->constraints->settling_time_up &&
2774 (new_uV > old_uV))
2775 return rdev->constraints->settling_time_up;
2776 else if (rdev->constraints->settling_time_down &&
2777 (new_uV < old_uV))
2778 return rdev->constraints->settling_time_down;
2779
2780 if (ramp_delay == 0) {
2781 rdev_dbg(rdev, "ramp_delay not set\n");
2782 return 0;
2783 }
2784
2785 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2786}
2787
2788static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2789 int min_uV, int max_uV)
2790{
2791 int ret;
2792 int delay = 0;
2793 int best_val = 0;
2794 unsigned int selector;
2795 int old_selector = -1;
2796 const struct regulator_ops *ops = rdev->desc->ops;
2797 int old_uV = _regulator_get_voltage(rdev);
2798
2799 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2800
2801 min_uV += rdev->constraints->uV_offset;
2802 max_uV += rdev->constraints->uV_offset;
2803
2804 /*
2805 * If we can't obtain the old selector there is not enough
2806 * info to call set_voltage_time_sel().
2807 */
2808 if (_regulator_is_enabled(rdev) &&
2809 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2810 old_selector = ops->get_voltage_sel(rdev);
2811 if (old_selector < 0)
2812 return old_selector;
2813 }
2814
2815 if (ops->set_voltage) {
2816 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2817 &selector);
2818
2819 if (ret >= 0) {
2820 if (ops->list_voltage)
2821 best_val = ops->list_voltage(rdev,
2822 selector);
2823 else
2824 best_val = _regulator_get_voltage(rdev);
2825 }
2826
2827 } else if (ops->set_voltage_sel) {
2828 ret = regulator_map_voltage(rdev, min_uV, max_uV);
2829 if (ret >= 0) {
2830 best_val = ops->list_voltage(rdev, ret);
2831 if (min_uV <= best_val && max_uV >= best_val) {
2832 selector = ret;
2833 if (old_selector == selector)
2834 ret = 0;
2835 else
2836 ret = _regulator_call_set_voltage_sel(
2837 rdev, best_val, selector);
2838 } else {
2839 ret = -EINVAL;
2840 }
2841 }
2842 } else {
2843 ret = -EINVAL;
2844 }
2845
2846 if (ret)
2847 goto out;
2848
2849 if (ops->set_voltage_time_sel) {
2850 /*
2851 * Call set_voltage_time_sel if successfully obtained
2852 * old_selector
2853 */
2854 if (old_selector >= 0 && old_selector != selector)
2855 delay = ops->set_voltage_time_sel(rdev, old_selector,
2856 selector);
2857 } else {
2858 if (old_uV != best_val) {
2859 if (ops->set_voltage_time)
2860 delay = ops->set_voltage_time(rdev, old_uV,
2861 best_val);
2862 else
2863 delay = _regulator_set_voltage_time(rdev,
2864 old_uV,
2865 best_val);
2866 }
2867 }
2868
2869 if (delay < 0) {
2870 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2871 delay = 0;
2872 }
2873
2874 /* Insert any necessary delays */
2875 if (delay >= 1000) {
2876 mdelay(delay / 1000);
2877 udelay(delay % 1000);
2878 } else if (delay) {
2879 udelay(delay);
2880 }
2881
2882 if (best_val >= 0) {
2883 unsigned long data = best_val;
2884
2885 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2886 (void *)data);
2887 }
2888
2889out:
2890 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2891
2892 return ret;
2893}
2894
2895static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
2896 int min_uV, int max_uV, suspend_state_t state)
2897{
2898 struct regulator_state *rstate;
2899 int uV, sel;
2900
2901 rstate = regulator_get_suspend_state(rdev, state);
2902 if (rstate == NULL)
2903 return -EINVAL;
2904
2905 if (min_uV < rstate->min_uV)
2906 min_uV = rstate->min_uV;
2907 if (max_uV > rstate->max_uV)
2908 max_uV = rstate->max_uV;
2909
2910 sel = regulator_map_voltage(rdev, min_uV, max_uV);
2911 if (sel < 0)
2912 return sel;
2913
2914 uV = rdev->desc->ops->list_voltage(rdev, sel);
2915 if (uV >= min_uV && uV <= max_uV)
2916 rstate->uV = uV;
2917
2918 return 0;
2919}
2920
2921static int regulator_set_voltage_unlocked(struct regulator *regulator,
2922 int min_uV, int max_uV,
2923 suspend_state_t state)
2924{
2925 struct regulator_dev *rdev = regulator->rdev;
2926 struct regulator_voltage *voltage = ®ulator->voltage[state];
2927 int ret = 0;
2928 int old_min_uV, old_max_uV;
2929 int current_uV;
2930 int best_supply_uV = 0;
2931 int supply_change_uV = 0;
2932
2933 /* If we're setting the same range as last time the change
2934 * should be a noop (some cpufreq implementations use the same
2935 * voltage for multiple frequencies, for example).
2936 */
2937 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
2938 goto out;
2939
2940 /* If we're trying to set a range that overlaps the current voltage,
2941 * return successfully even though the regulator does not support
2942 * changing the voltage.
2943 */
2944 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2945 current_uV = _regulator_get_voltage(rdev);
2946 if (min_uV <= current_uV && current_uV <= max_uV) {
2947 voltage->min_uV = min_uV;
2948 voltage->max_uV = max_uV;
2949 goto out;
2950 }
2951 }
2952
2953 /* sanity check */
2954 if (!rdev->desc->ops->set_voltage &&
2955 !rdev->desc->ops->set_voltage_sel) {
2956 ret = -EINVAL;
2957 goto out;
2958 }
2959
2960 /* constraints check */
2961 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2962 if (ret < 0)
2963 goto out;
2964
2965 /* restore original values in case of error */
2966 old_min_uV = voltage->min_uV;
2967 old_max_uV = voltage->max_uV;
2968 voltage->min_uV = min_uV;
2969 voltage->max_uV = max_uV;
2970
2971 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
2972 if (ret < 0)
2973 goto out2;
2974
2975 if (rdev->supply &&
2976 regulator_ops_is_valid(rdev->supply->rdev,
2977 REGULATOR_CHANGE_VOLTAGE) &&
2978 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
2979 rdev->desc->ops->get_voltage_sel))) {
2980 int current_supply_uV;
2981 int selector;
2982
2983 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2984 if (selector < 0) {
2985 ret = selector;
2986 goto out2;
2987 }
2988
2989 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
2990 if (best_supply_uV < 0) {
2991 ret = best_supply_uV;
2992 goto out2;
2993 }
2994
2995 best_supply_uV += rdev->desc->min_dropout_uV;
2996
2997 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
2998 if (current_supply_uV < 0) {
2999 ret = current_supply_uV;
3000 goto out2;
3001 }
3002
3003 supply_change_uV = best_supply_uV - current_supply_uV;
3004 }
3005
3006 if (supply_change_uV > 0) {
3007 ret = regulator_set_voltage_unlocked(rdev->supply,
3008 best_supply_uV, INT_MAX, state);
3009 if (ret) {
3010 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3011 ret);
3012 goto out2;
3013 }
3014 }
3015
3016 if (state == PM_SUSPEND_ON)
3017 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3018 else
3019 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3020 max_uV, state);
3021 if (ret < 0)
3022 goto out2;
3023
3024 if (supply_change_uV < 0) {
3025 ret = regulator_set_voltage_unlocked(rdev->supply,
3026 best_supply_uV, INT_MAX, state);
3027 if (ret)
3028 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3029 ret);
3030 /* No need to fail here */
3031 ret = 0;
3032 }
3033
3034out:
3035 return ret;
3036out2:
3037 voltage->min_uV = old_min_uV;
3038 voltage->max_uV = old_max_uV;
3039
3040 return ret;
3041}
3042
3043/**
3044 * regulator_set_voltage - set regulator output voltage
3045 * @regulator: regulator source
3046 * @min_uV: Minimum required voltage in uV
3047 * @max_uV: Maximum acceptable voltage in uV
3048 *
3049 * Sets a voltage regulator to the desired output voltage. This can be set
3050 * during any regulator state. IOW, regulator can be disabled or enabled.
3051 *
3052 * If the regulator is enabled then the voltage will change to the new value
3053 * immediately otherwise if the regulator is disabled the regulator will
3054 * output at the new voltage when enabled.
3055 *
3056 * NOTE: If the regulator is shared between several devices then the lowest
3057 * request voltage that meets the system constraints will be used.
3058 * Regulator system constraints must be set for this regulator before
3059 * calling this function otherwise this call will fail.
3060 */
3061int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3062{
3063 int ret = 0;
3064
3065 regulator_lock_supply(regulator->rdev);
3066
3067 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3068 PM_SUSPEND_ON);
3069
3070 regulator_unlock_supply(regulator->rdev);
3071
3072 return ret;
3073}
3074EXPORT_SYMBOL_GPL(regulator_set_voltage);
3075
3076static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3077 suspend_state_t state, bool en)
3078{
3079 struct regulator_state *rstate;
3080
3081 rstate = regulator_get_suspend_state(rdev, state);
3082 if (rstate == NULL)
3083 return -EINVAL;
3084
3085 if (!rstate->changeable)
3086 return -EPERM;
3087
3088 rstate->enabled = en;
3089
3090 return 0;
3091}
3092
3093int regulator_suspend_enable(struct regulator_dev *rdev,
3094 suspend_state_t state)
3095{
3096 return regulator_suspend_toggle(rdev, state, true);
3097}
3098EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3099
3100int regulator_suspend_disable(struct regulator_dev *rdev,
3101 suspend_state_t state)
3102{
3103 struct regulator *regulator;
3104 struct regulator_voltage *voltage;
3105
3106 /*
3107 * if any consumer wants this regulator device keeping on in
3108 * suspend states, don't set it as disabled.
3109 */
3110 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3111 voltage = ®ulator->voltage[state];
3112 if (voltage->min_uV || voltage->max_uV)
3113 return 0;
3114 }
3115
3116 return regulator_suspend_toggle(rdev, state, false);
3117}
3118EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3119
3120static int _regulator_set_suspend_voltage(struct regulator *regulator,
3121 int min_uV, int max_uV,
3122 suspend_state_t state)
3123{
3124 struct regulator_dev *rdev = regulator->rdev;
3125 struct regulator_state *rstate;
3126
3127 rstate = regulator_get_suspend_state(rdev, state);
3128 if (rstate == NULL)
3129 return -EINVAL;
3130
3131 if (rstate->min_uV == rstate->max_uV) {
3132 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3133 return -EPERM;
3134 }
3135
3136 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3137}
3138
3139int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3140 int max_uV, suspend_state_t state)
3141{
3142 int ret = 0;
3143
3144 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3145 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3146 return -EINVAL;
3147
3148 regulator_lock_supply(regulator->rdev);
3149
3150 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3151 max_uV, state);
3152
3153 regulator_unlock_supply(regulator->rdev);
3154
3155 return ret;
3156}
3157EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3158
3159/**
3160 * regulator_set_voltage_time - get raise/fall time
3161 * @regulator: regulator source
3162 * @old_uV: starting voltage in microvolts
3163 * @new_uV: target voltage in microvolts
3164 *
3165 * Provided with the starting and ending voltage, this function attempts to
3166 * calculate the time in microseconds required to rise or fall to this new
3167 * voltage.
3168 */
3169int regulator_set_voltage_time(struct regulator *regulator,
3170 int old_uV, int new_uV)
3171{
3172 struct regulator_dev *rdev = regulator->rdev;
3173 const struct regulator_ops *ops = rdev->desc->ops;
3174 int old_sel = -1;
3175 int new_sel = -1;
3176 int voltage;
3177 int i;
3178
3179 if (ops->set_voltage_time)
3180 return ops->set_voltage_time(rdev, old_uV, new_uV);
3181 else if (!ops->set_voltage_time_sel)
3182 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3183
3184 /* Currently requires operations to do this */
3185 if (!ops->list_voltage || !rdev->desc->n_voltages)
3186 return -EINVAL;
3187
3188 for (i = 0; i < rdev->desc->n_voltages; i++) {
3189 /* We only look for exact voltage matches here */
3190 voltage = regulator_list_voltage(regulator, i);
3191 if (voltage < 0)
3192 return -EINVAL;
3193 if (voltage == 0)
3194 continue;
3195 if (voltage == old_uV)
3196 old_sel = i;
3197 if (voltage == new_uV)
3198 new_sel = i;
3199 }
3200
3201 if (old_sel < 0 || new_sel < 0)
3202 return -EINVAL;
3203
3204 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3205}
3206EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3207
3208/**
3209 * regulator_set_voltage_time_sel - get raise/fall time
3210 * @rdev: regulator source device
3211 * @old_selector: selector for starting voltage
3212 * @new_selector: selector for target voltage
3213 *
3214 * Provided with the starting and target voltage selectors, this function
3215 * returns time in microseconds required to rise or fall to this new voltage
3216 *
3217 * Drivers providing ramp_delay in regulation_constraints can use this as their
3218 * set_voltage_time_sel() operation.
3219 */
3220int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3221 unsigned int old_selector,
3222 unsigned int new_selector)
3223{
3224 int old_volt, new_volt;
3225
3226 /* sanity check */
3227 if (!rdev->desc->ops->list_voltage)
3228 return -EINVAL;
3229
3230 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3231 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3232
3233 if (rdev->desc->ops->set_voltage_time)
3234 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3235 new_volt);
3236 else
3237 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3238}
3239EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3240
3241/**
3242 * regulator_sync_voltage - re-apply last regulator output voltage
3243 * @regulator: regulator source
3244 *
3245 * Re-apply the last configured voltage. This is intended to be used
3246 * where some external control source the consumer is cooperating with
3247 * has caused the configured voltage to change.
3248 */
3249int regulator_sync_voltage(struct regulator *regulator)
3250{
3251 struct regulator_dev *rdev = regulator->rdev;
3252 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
3253 int ret, min_uV, max_uV;
3254
3255 mutex_lock(&rdev->mutex);
3256
3257 if (!rdev->desc->ops->set_voltage &&
3258 !rdev->desc->ops->set_voltage_sel) {
3259 ret = -EINVAL;
3260 goto out;
3261 }
3262
3263 /* This is only going to work if we've had a voltage configured. */
3264 if (!voltage->min_uV && !voltage->max_uV) {
3265 ret = -EINVAL;
3266 goto out;
3267 }
3268
3269 min_uV = voltage->min_uV;
3270 max_uV = voltage->max_uV;
3271
3272 /* This should be a paranoia check... */
3273 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3274 if (ret < 0)
3275 goto out;
3276
3277 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3278 if (ret < 0)
3279 goto out;
3280
3281 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3282
3283out:
3284 mutex_unlock(&rdev->mutex);
3285 return ret;
3286}
3287EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3288
3289static int _regulator_get_voltage(struct regulator_dev *rdev)
3290{
3291 int sel, ret;
3292 bool bypassed;
3293
3294 if (rdev->desc->ops->get_bypass) {
3295 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3296 if (ret < 0)
3297 return ret;
3298 if (bypassed) {
3299 /* if bypassed the regulator must have a supply */
3300 if (!rdev->supply) {
3301 rdev_err(rdev,
3302 "bypassed regulator has no supply!\n");
3303 return -EPROBE_DEFER;
3304 }
3305
3306 return _regulator_get_voltage(rdev->supply->rdev);
3307 }
3308 }
3309
3310 if (rdev->desc->ops->get_voltage_sel) {
3311 sel = rdev->desc->ops->get_voltage_sel(rdev);
3312 if (sel < 0)
3313 return sel;
3314 ret = rdev->desc->ops->list_voltage(rdev, sel);
3315 } else if (rdev->desc->ops->get_voltage) {
3316 ret = rdev->desc->ops->get_voltage(rdev);
3317 } else if (rdev->desc->ops->list_voltage) {
3318 ret = rdev->desc->ops->list_voltage(rdev, 0);
3319 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3320 ret = rdev->desc->fixed_uV;
3321 } else if (rdev->supply) {
3322 ret = _regulator_get_voltage(rdev->supply->rdev);
3323 } else {
3324 return -EINVAL;
3325 }
3326
3327 if (ret < 0)
3328 return ret;
3329 return ret - rdev->constraints->uV_offset;
3330}
3331
3332/**
3333 * regulator_get_voltage - get regulator output voltage
3334 * @regulator: regulator source
3335 *
3336 * This returns the current regulator voltage in uV.
3337 *
3338 * NOTE: If the regulator is disabled it will return the voltage value. This
3339 * function should not be used to determine regulator state.
3340 */
3341int regulator_get_voltage(struct regulator *regulator)
3342{
3343 int ret;
3344
3345 regulator_lock_supply(regulator->rdev);
3346
3347 ret = _regulator_get_voltage(regulator->rdev);
3348
3349 regulator_unlock_supply(regulator->rdev);
3350
3351 return ret;
3352}
3353EXPORT_SYMBOL_GPL(regulator_get_voltage);
3354
3355/**
3356 * regulator_set_current_limit - set regulator output current limit
3357 * @regulator: regulator source
3358 * @min_uA: Minimum supported current in uA
3359 * @max_uA: Maximum supported current in uA
3360 *
3361 * Sets current sink to the desired output current. This can be set during
3362 * any regulator state. IOW, regulator can be disabled or enabled.
3363 *
3364 * If the regulator is enabled then the current will change to the new value
3365 * immediately otherwise if the regulator is disabled the regulator will
3366 * output at the new current when enabled.
3367 *
3368 * NOTE: Regulator system constraints must be set for this regulator before
3369 * calling this function otherwise this call will fail.
3370 */
3371int regulator_set_current_limit(struct regulator *regulator,
3372 int min_uA, int max_uA)
3373{
3374 struct regulator_dev *rdev = regulator->rdev;
3375 int ret;
3376
3377 mutex_lock(&rdev->mutex);
3378
3379 /* sanity check */
3380 if (!rdev->desc->ops->set_current_limit) {
3381 ret = -EINVAL;
3382 goto out;
3383 }
3384
3385 /* constraints check */
3386 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3387 if (ret < 0)
3388 goto out;
3389
3390 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3391out:
3392 mutex_unlock(&rdev->mutex);
3393 return ret;
3394}
3395EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3396
3397static int _regulator_get_current_limit(struct regulator_dev *rdev)
3398{
3399 int ret;
3400
3401 mutex_lock(&rdev->mutex);
3402
3403 /* sanity check */
3404 if (!rdev->desc->ops->get_current_limit) {
3405 ret = -EINVAL;
3406 goto out;
3407 }
3408
3409 ret = rdev->desc->ops->get_current_limit(rdev);
3410out:
3411 mutex_unlock(&rdev->mutex);
3412 return ret;
3413}
3414
3415/**
3416 * regulator_get_current_limit - get regulator output current
3417 * @regulator: regulator source
3418 *
3419 * This returns the current supplied by the specified current sink in uA.
3420 *
3421 * NOTE: If the regulator is disabled it will return the current value. This
3422 * function should not be used to determine regulator state.
3423 */
3424int regulator_get_current_limit(struct regulator *regulator)
3425{
3426 return _regulator_get_current_limit(regulator->rdev);
3427}
3428EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3429
3430/**
3431 * regulator_set_mode - set regulator operating mode
3432 * @regulator: regulator source
3433 * @mode: operating mode - one of the REGULATOR_MODE constants
3434 *
3435 * Set regulator operating mode to increase regulator efficiency or improve
3436 * regulation performance.
3437 *
3438 * NOTE: Regulator system constraints must be set for this regulator before
3439 * calling this function otherwise this call will fail.
3440 */
3441int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3442{
3443 struct regulator_dev *rdev = regulator->rdev;
3444 int ret;
3445 int regulator_curr_mode;
3446
3447 mutex_lock(&rdev->mutex);
3448
3449 /* sanity check */
3450 if (!rdev->desc->ops->set_mode) {
3451 ret = -EINVAL;
3452 goto out;
3453 }
3454
3455 /* return if the same mode is requested */
3456 if (rdev->desc->ops->get_mode) {
3457 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3458 if (regulator_curr_mode == mode) {
3459 ret = 0;
3460 goto out;
3461 }
3462 }
3463
3464 /* constraints check */
3465 ret = regulator_mode_constrain(rdev, &mode);
3466 if (ret < 0)
3467 goto out;
3468
3469 ret = rdev->desc->ops->set_mode(rdev, mode);
3470out:
3471 mutex_unlock(&rdev->mutex);
3472 return ret;
3473}
3474EXPORT_SYMBOL_GPL(regulator_set_mode);
3475
3476static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3477{
3478 int ret;
3479
3480 mutex_lock(&rdev->mutex);
3481
3482 /* sanity check */
3483 if (!rdev->desc->ops->get_mode) {
3484 ret = -EINVAL;
3485 goto out;
3486 }
3487
3488 ret = rdev->desc->ops->get_mode(rdev);
3489out:
3490 mutex_unlock(&rdev->mutex);
3491 return ret;
3492}
3493
3494/**
3495 * regulator_get_mode - get regulator operating mode
3496 * @regulator: regulator source
3497 *
3498 * Get the current regulator operating mode.
3499 */
3500unsigned int regulator_get_mode(struct regulator *regulator)
3501{
3502 return _regulator_get_mode(regulator->rdev);
3503}
3504EXPORT_SYMBOL_GPL(regulator_get_mode);
3505
3506static int _regulator_get_error_flags(struct regulator_dev *rdev,
3507 unsigned int *flags)
3508{
3509 int ret;
3510
3511 mutex_lock(&rdev->mutex);
3512
3513 /* sanity check */
3514 if (!rdev->desc->ops->get_error_flags) {
3515 ret = -EINVAL;
3516 goto out;
3517 }
3518
3519 ret = rdev->desc->ops->get_error_flags(rdev, flags);
3520out:
3521 mutex_unlock(&rdev->mutex);
3522 return ret;
3523}
3524
3525/**
3526 * regulator_get_error_flags - get regulator error information
3527 * @regulator: regulator source
3528 * @flags: pointer to store error flags
3529 *
3530 * Get the current regulator error information.
3531 */
3532int regulator_get_error_flags(struct regulator *regulator,
3533 unsigned int *flags)
3534{
3535 return _regulator_get_error_flags(regulator->rdev, flags);
3536}
3537EXPORT_SYMBOL_GPL(regulator_get_error_flags);
3538
3539/**
3540 * regulator_set_load - set regulator load
3541 * @regulator: regulator source
3542 * @uA_load: load current
3543 *
3544 * Notifies the regulator core of a new device load. This is then used by
3545 * DRMS (if enabled by constraints) to set the most efficient regulator
3546 * operating mode for the new regulator loading.
3547 *
3548 * Consumer devices notify their supply regulator of the maximum power
3549 * they will require (can be taken from device datasheet in the power
3550 * consumption tables) when they change operational status and hence power
3551 * state. Examples of operational state changes that can affect power
3552 * consumption are :-
3553 *
3554 * o Device is opened / closed.
3555 * o Device I/O is about to begin or has just finished.
3556 * o Device is idling in between work.
3557 *
3558 * This information is also exported via sysfs to userspace.
3559 *
3560 * DRMS will sum the total requested load on the regulator and change
3561 * to the most efficient operating mode if platform constraints allow.
3562 *
3563 * On error a negative errno is returned.
3564 */
3565int regulator_set_load(struct regulator *regulator, int uA_load)
3566{
3567 struct regulator_dev *rdev = regulator->rdev;
3568 int ret;
3569
3570 mutex_lock(&rdev->mutex);
3571 regulator->uA_load = uA_load;
3572 ret = drms_uA_update(rdev);
3573 mutex_unlock(&rdev->mutex);
3574
3575 return ret;
3576}
3577EXPORT_SYMBOL_GPL(regulator_set_load);
3578
3579/**
3580 * regulator_allow_bypass - allow the regulator to go into bypass mode
3581 *
3582 * @regulator: Regulator to configure
3583 * @enable: enable or disable bypass mode
3584 *
3585 * Allow the regulator to go into bypass mode if all other consumers
3586 * for the regulator also enable bypass mode and the machine
3587 * constraints allow this. Bypass mode means that the regulator is
3588 * simply passing the input directly to the output with no regulation.
3589 */
3590int regulator_allow_bypass(struct regulator *regulator, bool enable)
3591{
3592 struct regulator_dev *rdev = regulator->rdev;
3593 int ret = 0;
3594
3595 if (!rdev->desc->ops->set_bypass)
3596 return 0;
3597
3598 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
3599 return 0;
3600
3601 mutex_lock(&rdev->mutex);
3602
3603 if (enable && !regulator->bypass) {
3604 rdev->bypass_count++;
3605
3606 if (rdev->bypass_count == rdev->open_count) {
3607 ret = rdev->desc->ops->set_bypass(rdev, enable);
3608 if (ret != 0)
3609 rdev->bypass_count--;
3610 }
3611
3612 } else if (!enable && regulator->bypass) {
3613 rdev->bypass_count--;
3614
3615 if (rdev->bypass_count != rdev->open_count) {
3616 ret = rdev->desc->ops->set_bypass(rdev, enable);
3617 if (ret != 0)
3618 rdev->bypass_count++;
3619 }
3620 }
3621
3622 if (ret == 0)
3623 regulator->bypass = enable;
3624
3625 mutex_unlock(&rdev->mutex);
3626
3627 return ret;
3628}
3629EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3630
3631/**
3632 * regulator_register_notifier - register regulator event notifier
3633 * @regulator: regulator source
3634 * @nb: notifier block
3635 *
3636 * Register notifier block to receive regulator events.
3637 */
3638int regulator_register_notifier(struct regulator *regulator,
3639 struct notifier_block *nb)
3640{
3641 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3642 nb);
3643}
3644EXPORT_SYMBOL_GPL(regulator_register_notifier);
3645
3646/**
3647 * regulator_unregister_notifier - unregister regulator event notifier
3648 * @regulator: regulator source
3649 * @nb: notifier block
3650 *
3651 * Unregister regulator event notifier block.
3652 */
3653int regulator_unregister_notifier(struct regulator *regulator,
3654 struct notifier_block *nb)
3655{
3656 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3657 nb);
3658}
3659EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3660
3661/* notify regulator consumers and downstream regulator consumers.
3662 * Note mutex must be held by caller.
3663 */
3664static int _notifier_call_chain(struct regulator_dev *rdev,
3665 unsigned long event, void *data)
3666{
3667 /* call rdev chain first */
3668 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3669}
3670
3671/**
3672 * regulator_bulk_get - get multiple regulator consumers
3673 *
3674 * @dev: Device to supply
3675 * @num_consumers: Number of consumers to register
3676 * @consumers: Configuration of consumers; clients are stored here.
3677 *
3678 * @return 0 on success, an errno on failure.
3679 *
3680 * This helper function allows drivers to get several regulator
3681 * consumers in one operation. If any of the regulators cannot be
3682 * acquired then any regulators that were allocated will be freed
3683 * before returning to the caller.
3684 */
3685int regulator_bulk_get(struct device *dev, int num_consumers,
3686 struct regulator_bulk_data *consumers)
3687{
3688 int i;
3689 int ret;
3690
3691 for (i = 0; i < num_consumers; i++)
3692 consumers[i].consumer = NULL;
3693
3694 for (i = 0; i < num_consumers; i++) {
3695 consumers[i].consumer = regulator_get(dev,
3696 consumers[i].supply);
3697 if (IS_ERR(consumers[i].consumer)) {
3698 ret = PTR_ERR(consumers[i].consumer);
3699 dev_err(dev, "Failed to get supply '%s': %d\n",
3700 consumers[i].supply, ret);
3701 consumers[i].consumer = NULL;
3702 goto err;
3703 }
3704 }
3705
3706 return 0;
3707
3708err:
3709 while (--i >= 0)
3710 regulator_put(consumers[i].consumer);
3711
3712 return ret;
3713}
3714EXPORT_SYMBOL_GPL(regulator_bulk_get);
3715
3716static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3717{
3718 struct regulator_bulk_data *bulk = data;
3719
3720 bulk->ret = regulator_enable(bulk->consumer);
3721}
3722
3723/**
3724 * regulator_bulk_enable - enable multiple regulator consumers
3725 *
3726 * @num_consumers: Number of consumers
3727 * @consumers: Consumer data; clients are stored here.
3728 * @return 0 on success, an errno on failure
3729 *
3730 * This convenience API allows consumers to enable multiple regulator
3731 * clients in a single API call. If any consumers cannot be enabled
3732 * then any others that were enabled will be disabled again prior to
3733 * return.
3734 */
3735int regulator_bulk_enable(int num_consumers,
3736 struct regulator_bulk_data *consumers)
3737{
3738 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3739 int i;
3740 int ret = 0;
3741
3742 for (i = 0; i < num_consumers; i++) {
3743 if (consumers[i].consumer->always_on)
3744 consumers[i].ret = 0;
3745 else
3746 async_schedule_domain(regulator_bulk_enable_async,
3747 &consumers[i], &async_domain);
3748 }
3749
3750 async_synchronize_full_domain(&async_domain);
3751
3752 /* If any consumer failed we need to unwind any that succeeded */
3753 for (i = 0; i < num_consumers; i++) {
3754 if (consumers[i].ret != 0) {
3755 ret = consumers[i].ret;
3756 goto err;
3757 }
3758 }
3759
3760 return 0;
3761
3762err:
3763 for (i = 0; i < num_consumers; i++) {
3764 if (consumers[i].ret < 0)
3765 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3766 consumers[i].ret);
3767 else
3768 regulator_disable(consumers[i].consumer);
3769 }
3770
3771 return ret;
3772}
3773EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3774
3775/**
3776 * regulator_bulk_disable - disable multiple regulator consumers
3777 *
3778 * @num_consumers: Number of consumers
3779 * @consumers: Consumer data; clients are stored here.
3780 * @return 0 on success, an errno on failure
3781 *
3782 * This convenience API allows consumers to disable multiple regulator
3783 * clients in a single API call. If any consumers cannot be disabled
3784 * then any others that were disabled will be enabled again prior to
3785 * return.
3786 */
3787int regulator_bulk_disable(int num_consumers,
3788 struct regulator_bulk_data *consumers)
3789{
3790 int i;
3791 int ret, r;
3792
3793 for (i = num_consumers - 1; i >= 0; --i) {
3794 ret = regulator_disable(consumers[i].consumer);
3795 if (ret != 0)
3796 goto err;
3797 }
3798
3799 return 0;
3800
3801err:
3802 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3803 for (++i; i < num_consumers; ++i) {
3804 r = regulator_enable(consumers[i].consumer);
3805 if (r != 0)
3806 pr_err("Failed to re-enable %s: %d\n",
3807 consumers[i].supply, r);
3808 }
3809
3810 return ret;
3811}
3812EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3813
3814/**
3815 * regulator_bulk_force_disable - force disable multiple regulator consumers
3816 *
3817 * @num_consumers: Number of consumers
3818 * @consumers: Consumer data; clients are stored here.
3819 * @return 0 on success, an errno on failure
3820 *
3821 * This convenience API allows consumers to forcibly disable multiple regulator
3822 * clients in a single API call.
3823 * NOTE: This should be used for situations when device damage will
3824 * likely occur if the regulators are not disabled (e.g. over temp).
3825 * Although regulator_force_disable function call for some consumers can
3826 * return error numbers, the function is called for all consumers.
3827 */
3828int regulator_bulk_force_disable(int num_consumers,
3829 struct regulator_bulk_data *consumers)
3830{
3831 int i;
3832 int ret = 0;
3833
3834 for (i = 0; i < num_consumers; i++) {
3835 consumers[i].ret =
3836 regulator_force_disable(consumers[i].consumer);
3837
3838 /* Store first error for reporting */
3839 if (consumers[i].ret && !ret)
3840 ret = consumers[i].ret;
3841 }
3842
3843 return ret;
3844}
3845EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3846
3847/**
3848 * regulator_bulk_free - free multiple regulator consumers
3849 *
3850 * @num_consumers: Number of consumers
3851 * @consumers: Consumer data; clients are stored here.
3852 *
3853 * This convenience API allows consumers to free multiple regulator
3854 * clients in a single API call.
3855 */
3856void regulator_bulk_free(int num_consumers,
3857 struct regulator_bulk_data *consumers)
3858{
3859 int i;
3860
3861 for (i = 0; i < num_consumers; i++) {
3862 regulator_put(consumers[i].consumer);
3863 consumers[i].consumer = NULL;
3864 }
3865}
3866EXPORT_SYMBOL_GPL(regulator_bulk_free);
3867
3868/**
3869 * regulator_notifier_call_chain - call regulator event notifier
3870 * @rdev: regulator source
3871 * @event: notifier block
3872 * @data: callback-specific data.
3873 *
3874 * Called by regulator drivers to notify clients a regulator event has
3875 * occurred. We also notify regulator clients downstream.
3876 * Note lock must be held by caller.
3877 */
3878int regulator_notifier_call_chain(struct regulator_dev *rdev,
3879 unsigned long event, void *data)
3880{
3881 lockdep_assert_held_once(&rdev->mutex);
3882
3883 _notifier_call_chain(rdev, event, data);
3884 return NOTIFY_DONE;
3885
3886}
3887EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3888
3889/**
3890 * regulator_mode_to_status - convert a regulator mode into a status
3891 *
3892 * @mode: Mode to convert
3893 *
3894 * Convert a regulator mode into a status.
3895 */
3896int regulator_mode_to_status(unsigned int mode)
3897{
3898 switch (mode) {
3899 case REGULATOR_MODE_FAST:
3900 return REGULATOR_STATUS_FAST;
3901 case REGULATOR_MODE_NORMAL:
3902 return REGULATOR_STATUS_NORMAL;
3903 case REGULATOR_MODE_IDLE:
3904 return REGULATOR_STATUS_IDLE;
3905 case REGULATOR_MODE_STANDBY:
3906 return REGULATOR_STATUS_STANDBY;
3907 default:
3908 return REGULATOR_STATUS_UNDEFINED;
3909 }
3910}
3911EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3912
3913static struct attribute *regulator_dev_attrs[] = {
3914 &dev_attr_name.attr,
3915 &dev_attr_num_users.attr,
3916 &dev_attr_type.attr,
3917 &dev_attr_microvolts.attr,
3918 &dev_attr_microamps.attr,
3919 &dev_attr_opmode.attr,
3920 &dev_attr_state.attr,
3921 &dev_attr_status.attr,
3922 &dev_attr_bypass.attr,
3923 &dev_attr_requested_microamps.attr,
3924 &dev_attr_min_microvolts.attr,
3925 &dev_attr_max_microvolts.attr,
3926 &dev_attr_min_microamps.attr,
3927 &dev_attr_max_microamps.attr,
3928 &dev_attr_suspend_standby_state.attr,
3929 &dev_attr_suspend_mem_state.attr,
3930 &dev_attr_suspend_disk_state.attr,
3931 &dev_attr_suspend_standby_microvolts.attr,
3932 &dev_attr_suspend_mem_microvolts.attr,
3933 &dev_attr_suspend_disk_microvolts.attr,
3934 &dev_attr_suspend_standby_mode.attr,
3935 &dev_attr_suspend_mem_mode.attr,
3936 &dev_attr_suspend_disk_mode.attr,
3937 NULL
3938};
3939
3940/*
3941 * To avoid cluttering sysfs (and memory) with useless state, only
3942 * create attributes that can be meaningfully displayed.
3943 */
3944static umode_t regulator_attr_is_visible(struct kobject *kobj,
3945 struct attribute *attr, int idx)
3946{
3947 struct device *dev = kobj_to_dev(kobj);
3948 struct regulator_dev *rdev = dev_to_rdev(dev);
3949 const struct regulator_ops *ops = rdev->desc->ops;
3950 umode_t mode = attr->mode;
3951
3952 /* these three are always present */
3953 if (attr == &dev_attr_name.attr ||
3954 attr == &dev_attr_num_users.attr ||
3955 attr == &dev_attr_type.attr)
3956 return mode;
3957
3958 /* some attributes need specific methods to be displayed */
3959 if (attr == &dev_attr_microvolts.attr) {
3960 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3961 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3962 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3963 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3964 return mode;
3965 return 0;
3966 }
3967
3968 if (attr == &dev_attr_microamps.attr)
3969 return ops->get_current_limit ? mode : 0;
3970
3971 if (attr == &dev_attr_opmode.attr)
3972 return ops->get_mode ? mode : 0;
3973
3974 if (attr == &dev_attr_state.attr)
3975 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3976
3977 if (attr == &dev_attr_status.attr)
3978 return ops->get_status ? mode : 0;
3979
3980 if (attr == &dev_attr_bypass.attr)
3981 return ops->get_bypass ? mode : 0;
3982
3983 /* some attributes are type-specific */
3984 if (attr == &dev_attr_requested_microamps.attr)
3985 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
3986
3987 /* constraints need specific supporting methods */
3988 if (attr == &dev_attr_min_microvolts.attr ||
3989 attr == &dev_attr_max_microvolts.attr)
3990 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
3991
3992 if (attr == &dev_attr_min_microamps.attr ||
3993 attr == &dev_attr_max_microamps.attr)
3994 return ops->set_current_limit ? mode : 0;
3995
3996 if (attr == &dev_attr_suspend_standby_state.attr ||
3997 attr == &dev_attr_suspend_mem_state.attr ||
3998 attr == &dev_attr_suspend_disk_state.attr)
3999 return mode;
4000
4001 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4002 attr == &dev_attr_suspend_mem_microvolts.attr ||
4003 attr == &dev_attr_suspend_disk_microvolts.attr)
4004 return ops->set_suspend_voltage ? mode : 0;
4005
4006 if (attr == &dev_attr_suspend_standby_mode.attr ||
4007 attr == &dev_attr_suspend_mem_mode.attr ||
4008 attr == &dev_attr_suspend_disk_mode.attr)
4009 return ops->set_suspend_mode ? mode : 0;
4010
4011 return mode;
4012}
4013
4014static const struct attribute_group regulator_dev_group = {
4015 .attrs = regulator_dev_attrs,
4016 .is_visible = regulator_attr_is_visible,
4017};
4018
4019static const struct attribute_group *regulator_dev_groups[] = {
4020 ®ulator_dev_group,
4021 NULL
4022};
4023
4024static void regulator_dev_release(struct device *dev)
4025{
4026 struct regulator_dev *rdev = dev_get_drvdata(dev);
4027
4028 kfree(rdev->constraints);
4029 of_node_put(rdev->dev.of_node);
4030 kfree(rdev);
4031}
4032
4033static void rdev_init_debugfs(struct regulator_dev *rdev)
4034{
4035 struct device *parent = rdev->dev.parent;
4036 const char *rname = rdev_get_name(rdev);
4037 char name[NAME_MAX];
4038
4039 /* Avoid duplicate debugfs directory names */
4040 if (parent && rname == rdev->desc->name) {
4041 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4042 rname);
4043 rname = name;
4044 }
4045
4046 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4047 if (!rdev->debugfs) {
4048 rdev_warn(rdev, "Failed to create debugfs directory\n");
4049 return;
4050 }
4051
4052 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4053 &rdev->use_count);
4054 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4055 &rdev->open_count);
4056 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4057 &rdev->bypass_count);
4058}
4059
4060static int regulator_register_resolve_supply(struct device *dev, void *data)
4061{
4062 struct regulator_dev *rdev = dev_to_rdev(dev);
4063
4064 if (regulator_resolve_supply(rdev))
4065 rdev_dbg(rdev, "unable to resolve supply\n");
4066
4067 return 0;
4068}
4069
4070/**
4071 * regulator_register - register regulator
4072 * @regulator_desc: regulator to register
4073 * @cfg: runtime configuration for regulator
4074 *
4075 * Called by regulator drivers to register a regulator.
4076 * Returns a valid pointer to struct regulator_dev on success
4077 * or an ERR_PTR() on error.
4078 */
4079struct regulator_dev *
4080regulator_register(const struct regulator_desc *regulator_desc,
4081 const struct regulator_config *cfg)
4082{
4083 const struct regulation_constraints *constraints = NULL;
4084 const struct regulator_init_data *init_data;
4085 struct regulator_config *config = NULL;
4086 static atomic_t regulator_no = ATOMIC_INIT(-1);
4087 struct regulator_dev *rdev;
4088 struct device *dev;
4089 int ret, i;
4090
4091 if (regulator_desc == NULL || cfg == NULL)
4092 return ERR_PTR(-EINVAL);
4093
4094 dev = cfg->dev;
4095 WARN_ON(!dev);
4096
4097 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4098 return ERR_PTR(-EINVAL);
4099
4100 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4101 regulator_desc->type != REGULATOR_CURRENT)
4102 return ERR_PTR(-EINVAL);
4103
4104 /* Only one of each should be implemented */
4105 WARN_ON(regulator_desc->ops->get_voltage &&
4106 regulator_desc->ops->get_voltage_sel);
4107 WARN_ON(regulator_desc->ops->set_voltage &&
4108 regulator_desc->ops->set_voltage_sel);
4109
4110 /* If we're using selectors we must implement list_voltage. */
4111 if (regulator_desc->ops->get_voltage_sel &&
4112 !regulator_desc->ops->list_voltage) {
4113 return ERR_PTR(-EINVAL);
4114 }
4115 if (regulator_desc->ops->set_voltage_sel &&
4116 !regulator_desc->ops->list_voltage) {
4117 return ERR_PTR(-EINVAL);
4118 }
4119
4120 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4121 if (rdev == NULL)
4122 return ERR_PTR(-ENOMEM);
4123
4124 /*
4125 * Duplicate the config so the driver could override it after
4126 * parsing init data.
4127 */
4128 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4129 if (config == NULL) {
4130 kfree(rdev);
4131 return ERR_PTR(-ENOMEM);
4132 }
4133
4134 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4135 &rdev->dev.of_node);
4136 if (!init_data) {
4137 init_data = config->init_data;
4138 rdev->dev.of_node = of_node_get(config->of_node);
4139 }
4140
4141 mutex_init(&rdev->mutex);
4142 rdev->reg_data = config->driver_data;
4143 rdev->owner = regulator_desc->owner;
4144 rdev->desc = regulator_desc;
4145 if (config->regmap)
4146 rdev->regmap = config->regmap;
4147 else if (dev_get_regmap(dev, NULL))
4148 rdev->regmap = dev_get_regmap(dev, NULL);
4149 else if (dev->parent)
4150 rdev->regmap = dev_get_regmap(dev->parent, NULL);
4151 INIT_LIST_HEAD(&rdev->consumer_list);
4152 INIT_LIST_HEAD(&rdev->list);
4153 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4154 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4155
4156 /* preform any regulator specific init */
4157 if (init_data && init_data->regulator_init) {
4158 ret = init_data->regulator_init(rdev->reg_data);
4159 if (ret < 0)
4160 goto clean;
4161 }
4162
4163 if (config->ena_gpiod ||
4164 ((config->ena_gpio || config->ena_gpio_initialized) &&
4165 gpio_is_valid(config->ena_gpio))) {
4166 mutex_lock(®ulator_list_mutex);
4167 ret = regulator_ena_gpio_request(rdev, config);
4168 mutex_unlock(®ulator_list_mutex);
4169 if (ret != 0) {
4170 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4171 config->ena_gpio, ret);
4172 goto clean;
4173 }
4174 }
4175
4176 /* register with sysfs */
4177 rdev->dev.class = ®ulator_class;
4178 rdev->dev.parent = dev;
4179 dev_set_name(&rdev->dev, "regulator.%lu",
4180 (unsigned long) atomic_inc_return(®ulator_no));
4181
4182 /* set regulator constraints */
4183 if (init_data)
4184 constraints = &init_data->constraints;
4185
4186 if (init_data && init_data->supply_regulator)
4187 rdev->supply_name = init_data->supply_regulator;
4188 else if (regulator_desc->supply_name)
4189 rdev->supply_name = regulator_desc->supply_name;
4190
4191 /*
4192 * Attempt to resolve the regulator supply, if specified,
4193 * but don't return an error if we fail because we will try
4194 * to resolve it again later as more regulators are added.
4195 */
4196 if (regulator_resolve_supply(rdev))
4197 rdev_dbg(rdev, "unable to resolve supply\n");
4198
4199 ret = set_machine_constraints(rdev, constraints);
4200 if (ret < 0)
4201 goto wash;
4202
4203 /* add consumers devices */
4204 if (init_data) {
4205 mutex_lock(®ulator_list_mutex);
4206 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4207 ret = set_consumer_device_supply(rdev,
4208 init_data->consumer_supplies[i].dev_name,
4209 init_data->consumer_supplies[i].supply);
4210 if (ret < 0) {
4211 mutex_unlock(®ulator_list_mutex);
4212 dev_err(dev, "Failed to set supply %s\n",
4213 init_data->consumer_supplies[i].supply);
4214 goto unset_supplies;
4215 }
4216 }
4217 mutex_unlock(®ulator_list_mutex);
4218 }
4219
4220 if (!rdev->desc->ops->get_voltage &&
4221 !rdev->desc->ops->list_voltage &&
4222 !rdev->desc->fixed_uV)
4223 rdev->is_switch = true;
4224
4225 ret = device_register(&rdev->dev);
4226 if (ret != 0) {
4227 put_device(&rdev->dev);
4228 goto unset_supplies;
4229 }
4230
4231 dev_set_drvdata(&rdev->dev, rdev);
4232 rdev_init_debugfs(rdev);
4233
4234 /* try to resolve regulators supply since a new one was registered */
4235 class_for_each_device(®ulator_class, NULL, NULL,
4236 regulator_register_resolve_supply);
4237 kfree(config);
4238 return rdev;
4239
4240unset_supplies:
4241 mutex_lock(®ulator_list_mutex);
4242 unset_regulator_supplies(rdev);
4243 mutex_unlock(®ulator_list_mutex);
4244wash:
4245 kfree(rdev->constraints);
4246 mutex_lock(®ulator_list_mutex);
4247 regulator_ena_gpio_free(rdev);
4248 mutex_unlock(®ulator_list_mutex);
4249clean:
4250 kfree(rdev);
4251 kfree(config);
4252 return ERR_PTR(ret);
4253}
4254EXPORT_SYMBOL_GPL(regulator_register);
4255
4256/**
4257 * regulator_unregister - unregister regulator
4258 * @rdev: regulator to unregister
4259 *
4260 * Called by regulator drivers to unregister a regulator.
4261 */
4262void regulator_unregister(struct regulator_dev *rdev)
4263{
4264 if (rdev == NULL)
4265 return;
4266
4267 if (rdev->supply) {
4268 while (rdev->use_count--)
4269 regulator_disable(rdev->supply);
4270 regulator_put(rdev->supply);
4271 }
4272 mutex_lock(®ulator_list_mutex);
4273 debugfs_remove_recursive(rdev->debugfs);
4274 flush_work(&rdev->disable_work.work);
4275 WARN_ON(rdev->open_count);
4276 unset_regulator_supplies(rdev);
4277 list_del(&rdev->list);
4278 regulator_ena_gpio_free(rdev);
4279 mutex_unlock(®ulator_list_mutex);
4280 device_unregister(&rdev->dev);
4281}
4282EXPORT_SYMBOL_GPL(regulator_unregister);
4283
4284#ifdef CONFIG_SUSPEND
4285static int _regulator_suspend_late(struct device *dev, void *data)
4286{
4287 struct regulator_dev *rdev = dev_to_rdev(dev);
4288 suspend_state_t *state = data;
4289 int ret;
4290
4291 mutex_lock(&rdev->mutex);
4292 ret = suspend_set_state(rdev, *state);
4293 mutex_unlock(&rdev->mutex);
4294
4295 return ret;
4296}
4297
4298/**
4299 * regulator_suspend_late - prepare regulators for system wide suspend
4300 * @state: system suspend state
4301 *
4302 * Configure each regulator with it's suspend operating parameters for state.
4303 */
4304static int regulator_suspend_late(struct device *dev)
4305{
4306 suspend_state_t state = pm_suspend_target_state;
4307
4308 return class_for_each_device(®ulator_class, NULL, &state,
4309 _regulator_suspend_late);
4310}
4311
4312static int _regulator_resume_early(struct device *dev, void *data)
4313{
4314 int ret = 0;
4315 struct regulator_dev *rdev = dev_to_rdev(dev);
4316 suspend_state_t *state = data;
4317 struct regulator_state *rstate;
4318
4319 rstate = regulator_get_suspend_state(rdev, *state);
4320 if (rstate == NULL)
4321 return 0;
4322
4323 mutex_lock(&rdev->mutex);
4324
4325 if (rdev->desc->ops->resume_early &&
4326 (rstate->enabled == ENABLE_IN_SUSPEND ||
4327 rstate->enabled == DISABLE_IN_SUSPEND))
4328 ret = rdev->desc->ops->resume_early(rdev);
4329
4330 mutex_unlock(&rdev->mutex);
4331
4332 return ret;
4333}
4334
4335static int regulator_resume_early(struct device *dev)
4336{
4337 suspend_state_t state = pm_suspend_target_state;
4338
4339 return class_for_each_device(®ulator_class, NULL, &state,
4340 _regulator_resume_early);
4341}
4342
4343#else /* !CONFIG_SUSPEND */
4344
4345#define regulator_suspend_late NULL
4346#define regulator_resume_early NULL
4347
4348#endif /* !CONFIG_SUSPEND */
4349
4350#ifdef CONFIG_PM
4351static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
4352 .suspend_late = regulator_suspend_late,
4353 .resume_early = regulator_resume_early,
4354};
4355#endif
4356
4357struct class regulator_class = {
4358 .name = "regulator",
4359 .dev_release = regulator_dev_release,
4360 .dev_groups = regulator_dev_groups,
4361#ifdef CONFIG_PM
4362 .pm = ®ulator_pm_ops,
4363#endif
4364};
4365/**
4366 * regulator_has_full_constraints - the system has fully specified constraints
4367 *
4368 * Calling this function will cause the regulator API to disable all
4369 * regulators which have a zero use count and don't have an always_on
4370 * constraint in a late_initcall.
4371 *
4372 * The intention is that this will become the default behaviour in a
4373 * future kernel release so users are encouraged to use this facility
4374 * now.
4375 */
4376void regulator_has_full_constraints(void)
4377{
4378 has_full_constraints = 1;
4379}
4380EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4381
4382/**
4383 * rdev_get_drvdata - get rdev regulator driver data
4384 * @rdev: regulator
4385 *
4386 * Get rdev regulator driver private data. This call can be used in the
4387 * regulator driver context.
4388 */
4389void *rdev_get_drvdata(struct regulator_dev *rdev)
4390{
4391 return rdev->reg_data;
4392}
4393EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4394
4395/**
4396 * regulator_get_drvdata - get regulator driver data
4397 * @regulator: regulator
4398 *
4399 * Get regulator driver private data. This call can be used in the consumer
4400 * driver context when non API regulator specific functions need to be called.
4401 */
4402void *regulator_get_drvdata(struct regulator *regulator)
4403{
4404 return regulator->rdev->reg_data;
4405}
4406EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4407
4408/**
4409 * regulator_set_drvdata - set regulator driver data
4410 * @regulator: regulator
4411 * @data: data
4412 */
4413void regulator_set_drvdata(struct regulator *regulator, void *data)
4414{
4415 regulator->rdev->reg_data = data;
4416}
4417EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4418
4419/**
4420 * regulator_get_id - get regulator ID
4421 * @rdev: regulator
4422 */
4423int rdev_get_id(struct regulator_dev *rdev)
4424{
4425 return rdev->desc->id;
4426}
4427EXPORT_SYMBOL_GPL(rdev_get_id);
4428
4429struct device *rdev_get_dev(struct regulator_dev *rdev)
4430{
4431 return &rdev->dev;
4432}
4433EXPORT_SYMBOL_GPL(rdev_get_dev);
4434
4435void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4436{
4437 return reg_init_data->driver_data;
4438}
4439EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4440
4441#ifdef CONFIG_DEBUG_FS
4442static int supply_map_show(struct seq_file *sf, void *data)
4443{
4444 struct regulator_map *map;
4445
4446 list_for_each_entry(map, ®ulator_map_list, list) {
4447 seq_printf(sf, "%s -> %s.%s\n",
4448 rdev_get_name(map->regulator), map->dev_name,
4449 map->supply);
4450 }
4451
4452 return 0;
4453}
4454
4455static int supply_map_open(struct inode *inode, struct file *file)
4456{
4457 return single_open(file, supply_map_show, inode->i_private);
4458}
4459#endif
4460
4461static const struct file_operations supply_map_fops = {
4462#ifdef CONFIG_DEBUG_FS
4463 .open = supply_map_open,
4464 .read = seq_read,
4465 .llseek = seq_lseek,
4466 .release = single_release,
4467#endif
4468};
4469
4470#ifdef CONFIG_DEBUG_FS
4471struct summary_data {
4472 struct seq_file *s;
4473 struct regulator_dev *parent;
4474 int level;
4475};
4476
4477static void regulator_summary_show_subtree(struct seq_file *s,
4478 struct regulator_dev *rdev,
4479 int level);
4480
4481static int regulator_summary_show_children(struct device *dev, void *data)
4482{
4483 struct regulator_dev *rdev = dev_to_rdev(dev);
4484 struct summary_data *summary_data = data;
4485
4486 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4487 regulator_summary_show_subtree(summary_data->s, rdev,
4488 summary_data->level + 1);
4489
4490 return 0;
4491}
4492
4493static void regulator_summary_show_subtree(struct seq_file *s,
4494 struct regulator_dev *rdev,
4495 int level)
4496{
4497 struct regulation_constraints *c;
4498 struct regulator *consumer;
4499 struct summary_data summary_data;
4500
4501 if (!rdev)
4502 return;
4503
4504 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4505 level * 3 + 1, "",
4506 30 - level * 3, rdev_get_name(rdev),
4507 rdev->use_count, rdev->open_count, rdev->bypass_count);
4508
4509 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4510 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4511
4512 c = rdev->constraints;
4513 if (c) {
4514 switch (rdev->desc->type) {
4515 case REGULATOR_VOLTAGE:
4516 seq_printf(s, "%5dmV %5dmV ",
4517 c->min_uV / 1000, c->max_uV / 1000);
4518 break;
4519 case REGULATOR_CURRENT:
4520 seq_printf(s, "%5dmA %5dmA ",
4521 c->min_uA / 1000, c->max_uA / 1000);
4522 break;
4523 }
4524 }
4525
4526 seq_puts(s, "\n");
4527
4528 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4529 if (consumer->dev && consumer->dev->class == ®ulator_class)
4530 continue;
4531
4532 seq_printf(s, "%*s%-*s ",
4533 (level + 1) * 3 + 1, "",
4534 30 - (level + 1) * 3,
4535 consumer->dev ? dev_name(consumer->dev) : "deviceless");
4536
4537 switch (rdev->desc->type) {
4538 case REGULATOR_VOLTAGE:
4539 seq_printf(s, "%37dmV %5dmV",
4540 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
4541 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
4542 break;
4543 case REGULATOR_CURRENT:
4544 break;
4545 }
4546
4547 seq_puts(s, "\n");
4548 }
4549
4550 summary_data.s = s;
4551 summary_data.level = level;
4552 summary_data.parent = rdev;
4553
4554 class_for_each_device(®ulator_class, NULL, &summary_data,
4555 regulator_summary_show_children);
4556}
4557
4558static int regulator_summary_show_roots(struct device *dev, void *data)
4559{
4560 struct regulator_dev *rdev = dev_to_rdev(dev);
4561 struct seq_file *s = data;
4562
4563 if (!rdev->supply)
4564 regulator_summary_show_subtree(s, rdev, 0);
4565
4566 return 0;
4567}
4568
4569static int regulator_summary_show(struct seq_file *s, void *data)
4570{
4571 seq_puts(s, " regulator use open bypass voltage current min max\n");
4572 seq_puts(s, "-------------------------------------------------------------------------------\n");
4573
4574 class_for_each_device(®ulator_class, NULL, s,
4575 regulator_summary_show_roots);
4576
4577 return 0;
4578}
4579
4580static int regulator_summary_open(struct inode *inode, struct file *file)
4581{
4582 return single_open(file, regulator_summary_show, inode->i_private);
4583}
4584#endif
4585
4586static const struct file_operations regulator_summary_fops = {
4587#ifdef CONFIG_DEBUG_FS
4588 .open = regulator_summary_open,
4589 .read = seq_read,
4590 .llseek = seq_lseek,
4591 .release = single_release,
4592#endif
4593};
4594
4595static int __init regulator_init(void)
4596{
4597 int ret;
4598
4599 ret = class_register(®ulator_class);
4600
4601 debugfs_root = debugfs_create_dir("regulator", NULL);
4602 if (!debugfs_root)
4603 pr_warn("regulator: Failed to create debugfs directory\n");
4604
4605 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4606 &supply_map_fops);
4607
4608 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4609 NULL, ®ulator_summary_fops);
4610
4611 regulator_dummy_init();
4612
4613 return ret;
4614}
4615
4616/* init early to allow our consumers to complete system booting */
4617core_initcall(regulator_init);
4618
4619static int __init regulator_late_cleanup(struct device *dev, void *data)
4620{
4621 struct regulator_dev *rdev = dev_to_rdev(dev);
4622 const struct regulator_ops *ops = rdev->desc->ops;
4623 struct regulation_constraints *c = rdev->constraints;
4624 int enabled, ret;
4625
4626 if (c && c->always_on)
4627 return 0;
4628
4629 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
4630 return 0;
4631
4632 mutex_lock(&rdev->mutex);
4633
4634 if (rdev->use_count)
4635 goto unlock;
4636
4637 /* If we can't read the status assume it's on. */
4638 if (ops->is_enabled)
4639 enabled = ops->is_enabled(rdev);
4640 else
4641 enabled = 1;
4642
4643 if (!enabled)
4644 goto unlock;
4645
4646 if (have_full_constraints()) {
4647 /* We log since this may kill the system if it goes
4648 * wrong. */
4649 rdev_info(rdev, "disabling\n");
4650 ret = _regulator_do_disable(rdev);
4651 if (ret != 0)
4652 rdev_err(rdev, "couldn't disable: %d\n", ret);
4653 } else {
4654 /* The intention is that in future we will
4655 * assume that full constraints are provided
4656 * so warn even if we aren't going to do
4657 * anything here.
4658 */
4659 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4660 }
4661
4662unlock:
4663 mutex_unlock(&rdev->mutex);
4664
4665 return 0;
4666}
4667
4668static int __init regulator_init_complete(void)
4669{
4670 /*
4671 * Since DT doesn't provide an idiomatic mechanism for
4672 * enabling full constraints and since it's much more natural
4673 * with DT to provide them just assume that a DT enabled
4674 * system has full constraints.
4675 */
4676 if (of_have_populated_dt())
4677 has_full_constraints = true;
4678
4679 /*
4680 * Regulators may had failed to resolve their input supplies
4681 * when were registered, either because the input supply was
4682 * not registered yet or because its parent device was not
4683 * bound yet. So attempt to resolve the input supplies for
4684 * pending regulators before trying to disable unused ones.
4685 */
4686 class_for_each_device(®ulator_class, NULL, NULL,
4687 regulator_register_resolve_supply);
4688
4689 /* If we have a full configuration then disable any regulators
4690 * we have permission to change the status for and which are
4691 * not in use or always_on. This is effectively the default
4692 * for DT and ACPI as they have full constraints.
4693 */
4694 class_for_each_device(®ulator_class, NULL, NULL,
4695 regulator_late_cleanup);
4696
4697 return 0;
4698}
4699late_initcall_sync(regulator_init_complete);