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