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