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