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