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