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