core.c revision a56d66a2f01b172bc00d73b0b0392423c8aaae19
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 * returns 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 = 0;
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	/*
1263	 * If we have return value from dev_lookup fail, we do not expect to
1264	 * succeed, so, quit with appropriate error value
1265	 */
1266	if (ret) {
1267		regulator = ERR_PTR(ret);
1268		goto out;
1269	}
1270
1271	if (board_wants_dummy_regulator) {
1272		rdev = dummy_regulator_rdev;
1273		goto found;
1274	}
1275
1276#ifdef CONFIG_REGULATOR_DUMMY
1277	if (!devname)
1278		devname = "deviceless";
1279
1280	/* If the board didn't flag that it was fully constrained then
1281	 * substitute in a dummy regulator so consumers can continue.
1282	 */
1283	if (!has_full_constraints) {
1284		pr_warn("%s supply %s not found, using dummy regulator\n",
1285			devname, id);
1286		rdev = dummy_regulator_rdev;
1287		goto found;
1288	}
1289#endif
1290
1291	mutex_unlock(&regulator_list_mutex);
1292	return regulator;
1293
1294found:
1295	if (rdev->exclusive) {
1296		regulator = ERR_PTR(-EPERM);
1297		goto out;
1298	}
1299
1300	if (exclusive && rdev->open_count) {
1301		regulator = ERR_PTR(-EBUSY);
1302		goto out;
1303	}
1304
1305	if (!try_module_get(rdev->owner))
1306		goto out;
1307
1308	regulator = create_regulator(rdev, dev, id);
1309	if (regulator == NULL) {
1310		regulator = ERR_PTR(-ENOMEM);
1311		module_put(rdev->owner);
1312		goto out;
1313	}
1314
1315	rdev->open_count++;
1316	if (exclusive) {
1317		rdev->exclusive = 1;
1318
1319		ret = _regulator_is_enabled(rdev);
1320		if (ret > 0)
1321			rdev->use_count = 1;
1322		else
1323			rdev->use_count = 0;
1324	}
1325
1326out:
1327	mutex_unlock(&regulator_list_mutex);
1328
1329	return regulator;
1330}
1331
1332/**
1333 * regulator_get - lookup and obtain a reference to a regulator.
1334 * @dev: device for regulator "consumer"
1335 * @id: Supply name or regulator ID.
1336 *
1337 * Returns a struct regulator corresponding to the regulator producer,
1338 * or IS_ERR() condition containing errno.
1339 *
1340 * Use of supply names configured via regulator_set_device_supply() is
1341 * strongly encouraged.  It is recommended that the supply name used
1342 * should match the name used for the supply and/or the relevant
1343 * device pins in the datasheet.
1344 */
1345struct regulator *regulator_get(struct device *dev, const char *id)
1346{
1347	return _regulator_get(dev, id, 0);
1348}
1349EXPORT_SYMBOL_GPL(regulator_get);
1350
1351static void devm_regulator_release(struct device *dev, void *res)
1352{
1353	regulator_put(*(struct regulator **)res);
1354}
1355
1356/**
1357 * devm_regulator_get - Resource managed regulator_get()
1358 * @dev: device for regulator "consumer"
1359 * @id: Supply name or regulator ID.
1360 *
1361 * Managed regulator_get(). Regulators returned from this function are
1362 * automatically regulator_put() on driver detach. See regulator_get() for more
1363 * information.
1364 */
1365struct regulator *devm_regulator_get(struct device *dev, const char *id)
1366{
1367	struct regulator **ptr, *regulator;
1368
1369	ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1370	if (!ptr)
1371		return ERR_PTR(-ENOMEM);
1372
1373	regulator = regulator_get(dev, id);
1374	if (!IS_ERR(regulator)) {
1375		*ptr = regulator;
1376		devres_add(dev, ptr);
1377	} else {
1378		devres_free(ptr);
1379	}
1380
1381	return regulator;
1382}
1383EXPORT_SYMBOL_GPL(devm_regulator_get);
1384
1385/**
1386 * regulator_get_exclusive - obtain exclusive access to a regulator.
1387 * @dev: device for regulator "consumer"
1388 * @id: Supply name or regulator ID.
1389 *
1390 * Returns a struct regulator corresponding to the regulator producer,
1391 * or IS_ERR() condition containing errno.  Other consumers will be
1392 * unable to obtain this reference is held and the use count for the
1393 * regulator will be initialised to reflect the current state of the
1394 * regulator.
1395 *
1396 * This is intended for use by consumers which cannot tolerate shared
1397 * use of the regulator such as those which need to force the
1398 * regulator off for correct operation of the hardware they are
1399 * controlling.
1400 *
1401 * Use of supply names configured via regulator_set_device_supply() is
1402 * strongly encouraged.  It is recommended that the supply name used
1403 * should match the name used for the supply and/or the relevant
1404 * device pins in the datasheet.
1405 */
1406struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1407{
1408	return _regulator_get(dev, id, 1);
1409}
1410EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1411
1412/* Locks held by regulator_put() */
1413static void _regulator_put(struct regulator *regulator)
1414{
1415	struct regulator_dev *rdev;
1416
1417	if (regulator == NULL || IS_ERR(regulator))
1418		return;
1419
1420	rdev = regulator->rdev;
1421
1422	debugfs_remove_recursive(regulator->debugfs);
1423
1424	/* remove any sysfs entries */
1425	if (regulator->dev)
1426		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1427	kfree(regulator->supply_name);
1428	list_del(&regulator->list);
1429	kfree(regulator);
1430
1431	rdev->open_count--;
1432	rdev->exclusive = 0;
1433
1434	module_put(rdev->owner);
1435}
1436
1437/**
1438 * regulator_put - "free" the regulator source
1439 * @regulator: regulator source
1440 *
1441 * Note: drivers must ensure that all regulator_enable calls made on this
1442 * regulator source are balanced by regulator_disable calls prior to calling
1443 * this function.
1444 */
1445void regulator_put(struct regulator *regulator)
1446{
1447	mutex_lock(&regulator_list_mutex);
1448	_regulator_put(regulator);
1449	mutex_unlock(&regulator_list_mutex);
1450}
1451EXPORT_SYMBOL_GPL(regulator_put);
1452
1453static int devm_regulator_match(struct device *dev, void *res, void *data)
1454{
1455	struct regulator **r = res;
1456	if (!r || !*r) {
1457		WARN_ON(!r || !*r);
1458		return 0;
1459	}
1460	return *r == data;
1461}
1462
1463/**
1464 * devm_regulator_put - Resource managed regulator_put()
1465 * @regulator: regulator to free
1466 *
1467 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1468 * this function will not need to be called and the resource management
1469 * code will ensure that the resource is freed.
1470 */
1471void devm_regulator_put(struct regulator *regulator)
1472{
1473	int rc;
1474
1475	rc = devres_release(regulator->dev, devm_regulator_release,
1476			    devm_regulator_match, regulator);
1477	if (rc != 0)
1478		WARN_ON(rc);
1479}
1480EXPORT_SYMBOL_GPL(devm_regulator_put);
1481
1482/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1483static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1484				const struct regulator_config *config)
1485{
1486	struct regulator_enable_gpio *pin;
1487	int ret;
1488
1489	list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1490		if (pin->gpio == config->ena_gpio) {
1491			rdev_dbg(rdev, "GPIO %d is already used\n",
1492				config->ena_gpio);
1493			goto update_ena_gpio_to_rdev;
1494		}
1495	}
1496
1497	ret = gpio_request_one(config->ena_gpio,
1498				GPIOF_DIR_OUT | config->ena_gpio_flags,
1499				rdev_get_name(rdev));
1500	if (ret)
1501		return ret;
1502
1503	pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1504	if (pin == NULL) {
1505		gpio_free(config->ena_gpio);
1506		return -ENOMEM;
1507	}
1508
1509	pin->gpio = config->ena_gpio;
1510	pin->ena_gpio_invert = config->ena_gpio_invert;
1511	list_add(&pin->list, &regulator_ena_gpio_list);
1512
1513update_ena_gpio_to_rdev:
1514	pin->request_count++;
1515	rdev->ena_pin = pin;
1516	return 0;
1517}
1518
1519static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1520{
1521	struct regulator_enable_gpio *pin, *n;
1522
1523	if (!rdev->ena_pin)
1524		return;
1525
1526	/* Free the GPIO only in case of no use */
1527	list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1528		if (pin->gpio == rdev->ena_pin->gpio) {
1529			if (pin->request_count <= 1) {
1530				pin->request_count = 0;
1531				gpio_free(pin->gpio);
1532				list_del(&pin->list);
1533				kfree(pin);
1534			} else {
1535				pin->request_count--;
1536			}
1537		}
1538	}
1539}
1540
1541/**
1542 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1543 * @rdev: regulator_dev structure
1544 * @enable: enable GPIO at initial use?
1545 *
1546 * GPIO is enabled in case of initial use. (enable_count is 0)
1547 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1548 */
1549static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1550{
1551	struct regulator_enable_gpio *pin = rdev->ena_pin;
1552
1553	if (!pin)
1554		return -EINVAL;
1555
1556	if (enable) {
1557		/* Enable GPIO at initial use */
1558		if (pin->enable_count == 0)
1559			gpio_set_value_cansleep(pin->gpio,
1560						!pin->ena_gpio_invert);
1561
1562		pin->enable_count++;
1563	} else {
1564		if (pin->enable_count > 1) {
1565			pin->enable_count--;
1566			return 0;
1567		}
1568
1569		/* Disable GPIO if not used */
1570		if (pin->enable_count <= 1) {
1571			gpio_set_value_cansleep(pin->gpio,
1572						pin->ena_gpio_invert);
1573			pin->enable_count = 0;
1574		}
1575	}
1576
1577	return 0;
1578}
1579
1580static int _regulator_do_enable(struct regulator_dev *rdev)
1581{
1582	int ret, delay;
1583
1584	/* Query before enabling in case configuration dependent.  */
1585	ret = _regulator_get_enable_time(rdev);
1586	if (ret >= 0) {
1587		delay = ret;
1588	} else {
1589		rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1590		delay = 0;
1591	}
1592
1593	trace_regulator_enable(rdev_get_name(rdev));
1594
1595	if (rdev->ena_pin) {
1596		ret = regulator_ena_gpio_ctrl(rdev, true);
1597		if (ret < 0)
1598			return ret;
1599		rdev->ena_gpio_state = 1;
1600	} else if (rdev->desc->ops->enable) {
1601		ret = rdev->desc->ops->enable(rdev);
1602		if (ret < 0)
1603			return ret;
1604	} else {
1605		return -EINVAL;
1606	}
1607
1608	/* Allow the regulator to ramp; it would be useful to extend
1609	 * this for bulk operations so that the regulators can ramp
1610	 * together.  */
1611	trace_regulator_enable_delay(rdev_get_name(rdev));
1612
1613	if (delay >= 1000) {
1614		mdelay(delay / 1000);
1615		udelay(delay % 1000);
1616	} else if (delay) {
1617		udelay(delay);
1618	}
1619
1620	trace_regulator_enable_complete(rdev_get_name(rdev));
1621
1622	return 0;
1623}
1624
1625/* locks held by regulator_enable() */
1626static int _regulator_enable(struct regulator_dev *rdev)
1627{
1628	int ret;
1629
1630	/* check voltage and requested load before enabling */
1631	if (rdev->constraints &&
1632	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1633		drms_uA_update(rdev);
1634
1635	if (rdev->use_count == 0) {
1636		/* The regulator may on if it's not switchable or left on */
1637		ret = _regulator_is_enabled(rdev);
1638		if (ret == -EINVAL || ret == 0) {
1639			if (!_regulator_can_change_status(rdev))
1640				return -EPERM;
1641
1642			ret = _regulator_do_enable(rdev);
1643			if (ret < 0)
1644				return ret;
1645
1646		} else if (ret < 0) {
1647			rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1648			return ret;
1649		}
1650		/* Fallthrough on positive return values - already enabled */
1651	}
1652
1653	rdev->use_count++;
1654
1655	return 0;
1656}
1657
1658/**
1659 * regulator_enable - enable regulator output
1660 * @regulator: regulator source
1661 *
1662 * Request that the regulator be enabled with the regulator output at
1663 * the predefined voltage or current value.  Calls to regulator_enable()
1664 * must be balanced with calls to regulator_disable().
1665 *
1666 * NOTE: the output value can be set by other drivers, boot loader or may be
1667 * hardwired in the regulator.
1668 */
1669int regulator_enable(struct regulator *regulator)
1670{
1671	struct regulator_dev *rdev = regulator->rdev;
1672	int ret = 0;
1673
1674	if (regulator->always_on)
1675		return 0;
1676
1677	if (rdev->supply) {
1678		ret = regulator_enable(rdev->supply);
1679		if (ret != 0)
1680			return ret;
1681	}
1682
1683	mutex_lock(&rdev->mutex);
1684	ret = _regulator_enable(rdev);
1685	mutex_unlock(&rdev->mutex);
1686
1687	if (ret != 0 && rdev->supply)
1688		regulator_disable(rdev->supply);
1689
1690	return ret;
1691}
1692EXPORT_SYMBOL_GPL(regulator_enable);
1693
1694static int _regulator_do_disable(struct regulator_dev *rdev)
1695{
1696	int ret;
1697
1698	trace_regulator_disable(rdev_get_name(rdev));
1699
1700	if (rdev->ena_pin) {
1701		ret = regulator_ena_gpio_ctrl(rdev, false);
1702		if (ret < 0)
1703			return ret;
1704		rdev->ena_gpio_state = 0;
1705
1706	} else if (rdev->desc->ops->disable) {
1707		ret = rdev->desc->ops->disable(rdev);
1708		if (ret != 0)
1709			return ret;
1710	}
1711
1712	trace_regulator_disable_complete(rdev_get_name(rdev));
1713
1714	_notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1715			     NULL);
1716	return 0;
1717}
1718
1719/* locks held by regulator_disable() */
1720static int _regulator_disable(struct regulator_dev *rdev)
1721{
1722	int ret = 0;
1723
1724	if (WARN(rdev->use_count <= 0,
1725		 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1726		return -EIO;
1727
1728	/* are we the last user and permitted to disable ? */
1729	if (rdev->use_count == 1 &&
1730	    (rdev->constraints && !rdev->constraints->always_on)) {
1731
1732		/* we are last user */
1733		if (_regulator_can_change_status(rdev)) {
1734			ret = _regulator_do_disable(rdev);
1735			if (ret < 0) {
1736				rdev_err(rdev, "failed to disable\n");
1737				return ret;
1738			}
1739		}
1740
1741		rdev->use_count = 0;
1742	} else if (rdev->use_count > 1) {
1743
1744		if (rdev->constraints &&
1745			(rdev->constraints->valid_ops_mask &
1746			REGULATOR_CHANGE_DRMS))
1747			drms_uA_update(rdev);
1748
1749		rdev->use_count--;
1750	}
1751
1752	return ret;
1753}
1754
1755/**
1756 * regulator_disable - disable regulator output
1757 * @regulator: regulator source
1758 *
1759 * Disable the regulator output voltage or current.  Calls to
1760 * regulator_enable() must be balanced with calls to
1761 * regulator_disable().
1762 *
1763 * NOTE: this will only disable the regulator output if no other consumer
1764 * devices have it enabled, the regulator device supports disabling and
1765 * machine constraints permit this operation.
1766 */
1767int regulator_disable(struct regulator *regulator)
1768{
1769	struct regulator_dev *rdev = regulator->rdev;
1770	int ret = 0;
1771
1772	if (regulator->always_on)
1773		return 0;
1774
1775	mutex_lock(&rdev->mutex);
1776	ret = _regulator_disable(rdev);
1777	mutex_unlock(&rdev->mutex);
1778
1779	if (ret == 0 && rdev->supply)
1780		regulator_disable(rdev->supply);
1781
1782	return ret;
1783}
1784EXPORT_SYMBOL_GPL(regulator_disable);
1785
1786/* locks held by regulator_force_disable() */
1787static int _regulator_force_disable(struct regulator_dev *rdev)
1788{
1789	int ret = 0;
1790
1791	/* force disable */
1792	if (rdev->desc->ops->disable) {
1793		/* ah well, who wants to live forever... */
1794		ret = rdev->desc->ops->disable(rdev);
1795		if (ret < 0) {
1796			rdev_err(rdev, "failed to force disable\n");
1797			return ret;
1798		}
1799		/* notify other consumers that power has been forced off */
1800		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1801			REGULATOR_EVENT_DISABLE, NULL);
1802	}
1803
1804	return ret;
1805}
1806
1807/**
1808 * regulator_force_disable - force disable regulator output
1809 * @regulator: regulator source
1810 *
1811 * Forcibly disable the regulator output voltage or current.
1812 * NOTE: this *will* disable the regulator output even if other consumer
1813 * devices have it enabled. This should be used for situations when device
1814 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1815 */
1816int regulator_force_disable(struct regulator *regulator)
1817{
1818	struct regulator_dev *rdev = regulator->rdev;
1819	int ret;
1820
1821	mutex_lock(&rdev->mutex);
1822	regulator->uA_load = 0;
1823	ret = _regulator_force_disable(regulator->rdev);
1824	mutex_unlock(&rdev->mutex);
1825
1826	if (rdev->supply)
1827		while (rdev->open_count--)
1828			regulator_disable(rdev->supply);
1829
1830	return ret;
1831}
1832EXPORT_SYMBOL_GPL(regulator_force_disable);
1833
1834static void regulator_disable_work(struct work_struct *work)
1835{
1836	struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1837						  disable_work.work);
1838	int count, i, ret;
1839
1840	mutex_lock(&rdev->mutex);
1841
1842	BUG_ON(!rdev->deferred_disables);
1843
1844	count = rdev->deferred_disables;
1845	rdev->deferred_disables = 0;
1846
1847	for (i = 0; i < count; i++) {
1848		ret = _regulator_disable(rdev);
1849		if (ret != 0)
1850			rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1851	}
1852
1853	mutex_unlock(&rdev->mutex);
1854
1855	if (rdev->supply) {
1856		for (i = 0; i < count; i++) {
1857			ret = regulator_disable(rdev->supply);
1858			if (ret != 0) {
1859				rdev_err(rdev,
1860					 "Supply disable failed: %d\n", ret);
1861			}
1862		}
1863	}
1864}
1865
1866/**
1867 * regulator_disable_deferred - disable regulator output with delay
1868 * @regulator: regulator source
1869 * @ms: miliseconds until the regulator is disabled
1870 *
1871 * Execute regulator_disable() on the regulator after a delay.  This
1872 * is intended for use with devices that require some time to quiesce.
1873 *
1874 * NOTE: this will only disable the regulator output if no other consumer
1875 * devices have it enabled, the regulator device supports disabling and
1876 * machine constraints permit this operation.
1877 */
1878int regulator_disable_deferred(struct regulator *regulator, int ms)
1879{
1880	struct regulator_dev *rdev = regulator->rdev;
1881	int ret;
1882
1883	if (regulator->always_on)
1884		return 0;
1885
1886	if (!ms)
1887		return regulator_disable(regulator);
1888
1889	mutex_lock(&rdev->mutex);
1890	rdev->deferred_disables++;
1891	mutex_unlock(&rdev->mutex);
1892
1893	ret = schedule_delayed_work(&rdev->disable_work,
1894				    msecs_to_jiffies(ms));
1895	if (ret < 0)
1896		return ret;
1897	else
1898		return 0;
1899}
1900EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1901
1902/**
1903 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1904 *
1905 * @rdev: regulator to operate on
1906 *
1907 * Regulators that use regmap for their register I/O can set the
1908 * enable_reg and enable_mask fields in their descriptor and then use
1909 * this as their is_enabled operation, saving some code.
1910 */
1911int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1912{
1913	unsigned int val;
1914	int ret;
1915
1916	ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1917	if (ret != 0)
1918		return ret;
1919
1920	if (rdev->desc->enable_is_inverted)
1921		return (val & rdev->desc->enable_mask) == 0;
1922	else
1923		return (val & rdev->desc->enable_mask) != 0;
1924}
1925EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1926
1927/**
1928 * regulator_enable_regmap - standard enable() for regmap users
1929 *
1930 * @rdev: regulator to operate on
1931 *
1932 * Regulators that use regmap for their register I/O can set the
1933 * enable_reg and enable_mask fields in their descriptor and then use
1934 * this as their enable() operation, saving some code.
1935 */
1936int regulator_enable_regmap(struct regulator_dev *rdev)
1937{
1938	unsigned int val;
1939
1940	if (rdev->desc->enable_is_inverted)
1941		val = 0;
1942	else
1943		val = rdev->desc->enable_mask;
1944
1945	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1946				  rdev->desc->enable_mask, val);
1947}
1948EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1949
1950/**
1951 * regulator_disable_regmap - standard disable() for regmap users
1952 *
1953 * @rdev: regulator to operate on
1954 *
1955 * Regulators that use regmap for their register I/O can set the
1956 * enable_reg and enable_mask fields in their descriptor and then use
1957 * this as their disable() operation, saving some code.
1958 */
1959int regulator_disable_regmap(struct regulator_dev *rdev)
1960{
1961	unsigned int val;
1962
1963	if (rdev->desc->enable_is_inverted)
1964		val = rdev->desc->enable_mask;
1965	else
1966		val = 0;
1967
1968	return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1969				  rdev->desc->enable_mask, val);
1970}
1971EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1972
1973static int _regulator_is_enabled(struct regulator_dev *rdev)
1974{
1975	/* A GPIO control always takes precedence */
1976	if (rdev->ena_pin)
1977		return rdev->ena_gpio_state;
1978
1979	/* If we don't know then assume that the regulator is always on */
1980	if (!rdev->desc->ops->is_enabled)
1981		return 1;
1982
1983	return rdev->desc->ops->is_enabled(rdev);
1984}
1985
1986/**
1987 * regulator_is_enabled - is the regulator output enabled
1988 * @regulator: regulator source
1989 *
1990 * Returns positive if the regulator driver backing the source/client
1991 * has requested that the device be enabled, zero if it hasn't, else a
1992 * negative errno code.
1993 *
1994 * Note that the device backing this regulator handle can have multiple
1995 * users, so it might be enabled even if regulator_enable() was never
1996 * called for this particular source.
1997 */
1998int regulator_is_enabled(struct regulator *regulator)
1999{
2000	int ret;
2001
2002	if (regulator->always_on)
2003		return 1;
2004
2005	mutex_lock(&regulator->rdev->mutex);
2006	ret = _regulator_is_enabled(regulator->rdev);
2007	mutex_unlock(&regulator->rdev->mutex);
2008
2009	return ret;
2010}
2011EXPORT_SYMBOL_GPL(regulator_is_enabled);
2012
2013/**
2014 * regulator_can_change_voltage - check if regulator can change voltage
2015 * @regulator: regulator source
2016 *
2017 * Returns positive if the regulator driver backing the source/client
2018 * can change its voltage, false otherwise. Usefull for detecting fixed
2019 * or dummy regulators and disabling voltage change logic in the client
2020 * driver.
2021 */
2022int regulator_can_change_voltage(struct regulator *regulator)
2023{
2024	struct regulator_dev	*rdev = regulator->rdev;
2025
2026	if (rdev->constraints &&
2027	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2028		if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2029			return 1;
2030
2031		if (rdev->desc->continuous_voltage_range &&
2032		    rdev->constraints->min_uV && rdev->constraints->max_uV &&
2033		    rdev->constraints->min_uV != rdev->constraints->max_uV)
2034			return 1;
2035	}
2036
2037	return 0;
2038}
2039EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2040
2041/**
2042 * regulator_count_voltages - count regulator_list_voltage() selectors
2043 * @regulator: regulator source
2044 *
2045 * Returns number of selectors, or negative errno.  Selectors are
2046 * numbered starting at zero, and typically correspond to bitfields
2047 * in hardware registers.
2048 */
2049int regulator_count_voltages(struct regulator *regulator)
2050{
2051	struct regulator_dev	*rdev = regulator->rdev;
2052
2053	return rdev->desc->n_voltages ? : -EINVAL;
2054}
2055EXPORT_SYMBOL_GPL(regulator_count_voltages);
2056
2057/**
2058 * regulator_list_voltage_linear - List voltages with simple calculation
2059 *
2060 * @rdev: Regulator device
2061 * @selector: Selector to convert into a voltage
2062 *
2063 * Regulators with a simple linear mapping between voltages and
2064 * selectors can set min_uV and uV_step in the regulator descriptor
2065 * and then use this function as their list_voltage() operation,
2066 */
2067int regulator_list_voltage_linear(struct regulator_dev *rdev,
2068				  unsigned int selector)
2069{
2070	if (selector >= rdev->desc->n_voltages)
2071		return -EINVAL;
2072	if (selector < rdev->desc->linear_min_sel)
2073		return 0;
2074
2075	selector -= rdev->desc->linear_min_sel;
2076
2077	return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2078}
2079EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2080
2081/**
2082 * regulator_list_voltage_linear_range - List voltages for linear ranges
2083 *
2084 * @rdev: Regulator device
2085 * @selector: Selector to convert into a voltage
2086 *
2087 * Regulators with a series of simple linear mappings between voltages
2088 * and selectors can set linear_ranges in the regulator descriptor and
2089 * then use this function as their list_voltage() operation,
2090 */
2091int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
2092					unsigned int selector)
2093{
2094	const struct regulator_linear_range *range;
2095	int i;
2096
2097	if (!rdev->desc->n_linear_ranges) {
2098		BUG_ON(!rdev->desc->n_linear_ranges);
2099		return -EINVAL;
2100	}
2101
2102	for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
2103		range = &rdev->desc->linear_ranges[i];
2104
2105		if (!(selector >= range->min_sel &&
2106		      selector <= range->max_sel))
2107			continue;
2108
2109		selector -= range->min_sel;
2110
2111		return range->min_uV + (range->uV_step * selector);
2112	}
2113
2114	return -EINVAL;
2115}
2116EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range);
2117
2118/**
2119 * regulator_list_voltage_table - List voltages with table based mapping
2120 *
2121 * @rdev: Regulator device
2122 * @selector: Selector to convert into a voltage
2123 *
2124 * Regulators with table based mapping between voltages and
2125 * selectors can set volt_table in the regulator descriptor
2126 * and then use this function as their list_voltage() operation.
2127 */
2128int regulator_list_voltage_table(struct regulator_dev *rdev,
2129				 unsigned int selector)
2130{
2131	if (!rdev->desc->volt_table) {
2132		BUG_ON(!rdev->desc->volt_table);
2133		return -EINVAL;
2134	}
2135
2136	if (selector >= rdev->desc->n_voltages)
2137		return -EINVAL;
2138
2139	return rdev->desc->volt_table[selector];
2140}
2141EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2142
2143/**
2144 * regulator_list_voltage - enumerate supported voltages
2145 * @regulator: regulator source
2146 * @selector: identify voltage to list
2147 * Context: can sleep
2148 *
2149 * Returns a voltage that can be passed to @regulator_set_voltage(),
2150 * zero if this selector code can't be used on this system, or a
2151 * negative errno.
2152 */
2153int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2154{
2155	struct regulator_dev	*rdev = regulator->rdev;
2156	struct regulator_ops	*ops = rdev->desc->ops;
2157	int			ret;
2158
2159	if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2160		return -EINVAL;
2161
2162	mutex_lock(&rdev->mutex);
2163	ret = ops->list_voltage(rdev, selector);
2164	mutex_unlock(&rdev->mutex);
2165
2166	if (ret > 0) {
2167		if (ret < rdev->constraints->min_uV)
2168			ret = 0;
2169		else if (ret > rdev->constraints->max_uV)
2170			ret = 0;
2171	}
2172
2173	return ret;
2174}
2175EXPORT_SYMBOL_GPL(regulator_list_voltage);
2176
2177/**
2178 * regulator_get_linear_step - return the voltage step size between VSEL values
2179 * @regulator: regulator source
2180 *
2181 * Returns the voltage step size between VSEL values for linear
2182 * regulators, or return 0 if the regulator isn't a linear regulator.
2183 */
2184unsigned int regulator_get_linear_step(struct regulator *regulator)
2185{
2186	struct regulator_dev *rdev = regulator->rdev;
2187
2188	return rdev->desc->uV_step;
2189}
2190EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2191
2192/**
2193 * regulator_is_supported_voltage - check if a voltage range can be supported
2194 *
2195 * @regulator: Regulator to check.
2196 * @min_uV: Minimum required voltage in uV.
2197 * @max_uV: Maximum required voltage in uV.
2198 *
2199 * Returns a boolean or a negative error code.
2200 */
2201int regulator_is_supported_voltage(struct regulator *regulator,
2202				   int min_uV, int max_uV)
2203{
2204	struct regulator_dev *rdev = regulator->rdev;
2205	int i, voltages, ret;
2206
2207	/* If we can't change voltage check the current voltage */
2208	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2209		ret = regulator_get_voltage(regulator);
2210		if (ret >= 0)
2211			return (min_uV <= ret && ret <= max_uV);
2212		else
2213			return ret;
2214	}
2215
2216	/* Any voltage within constrains range is fine? */
2217	if (rdev->desc->continuous_voltage_range)
2218		return min_uV >= rdev->constraints->min_uV &&
2219				max_uV <= rdev->constraints->max_uV;
2220
2221	ret = regulator_count_voltages(regulator);
2222	if (ret < 0)
2223		return ret;
2224	voltages = ret;
2225
2226	for (i = 0; i < voltages; i++) {
2227		ret = regulator_list_voltage(regulator, i);
2228
2229		if (ret >= min_uV && ret <= max_uV)
2230			return 1;
2231	}
2232
2233	return 0;
2234}
2235EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2236
2237/**
2238 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2239 *
2240 * @rdev: regulator to operate on
2241 *
2242 * Regulators that use regmap for their register I/O can set the
2243 * vsel_reg and vsel_mask fields in their descriptor and then use this
2244 * as their get_voltage_vsel operation, saving some code.
2245 */
2246int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2247{
2248	unsigned int val;
2249	int ret;
2250
2251	ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2252	if (ret != 0)
2253		return ret;
2254
2255	val &= rdev->desc->vsel_mask;
2256	val >>= ffs(rdev->desc->vsel_mask) - 1;
2257
2258	return val;
2259}
2260EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2261
2262/**
2263 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2264 *
2265 * @rdev: regulator to operate on
2266 * @sel: Selector to set
2267 *
2268 * Regulators that use regmap for their register I/O can set the
2269 * vsel_reg and vsel_mask fields in their descriptor and then use this
2270 * as their set_voltage_vsel operation, saving some code.
2271 */
2272int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2273{
2274	int ret;
2275
2276	sel <<= ffs(rdev->desc->vsel_mask) - 1;
2277
2278	ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2279				  rdev->desc->vsel_mask, sel);
2280	if (ret)
2281		return ret;
2282
2283	if (rdev->desc->apply_bit)
2284		ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2285					 rdev->desc->apply_bit,
2286					 rdev->desc->apply_bit);
2287	return ret;
2288}
2289EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2290
2291/**
2292 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2293 *
2294 * @rdev: Regulator to operate on
2295 * @min_uV: Lower bound for voltage
2296 * @max_uV: Upper bound for voltage
2297 *
2298 * Drivers implementing set_voltage_sel() and list_voltage() can use
2299 * this as their map_voltage() operation.  It will find a suitable
2300 * voltage by calling list_voltage() until it gets something in bounds
2301 * for the requested voltages.
2302 */
2303int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2304				  int min_uV, int max_uV)
2305{
2306	int best_val = INT_MAX;
2307	int selector = 0;
2308	int i, ret;
2309
2310	/* Find the smallest voltage that falls within the specified
2311	 * range.
2312	 */
2313	for (i = 0; i < rdev->desc->n_voltages; i++) {
2314		ret = rdev->desc->ops->list_voltage(rdev, i);
2315		if (ret < 0)
2316			continue;
2317
2318		if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2319			best_val = ret;
2320			selector = i;
2321		}
2322	}
2323
2324	if (best_val != INT_MAX)
2325		return selector;
2326	else
2327		return -EINVAL;
2328}
2329EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2330
2331/**
2332 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2333 *
2334 * @rdev: Regulator to operate on
2335 * @min_uV: Lower bound for voltage
2336 * @max_uV: Upper bound for voltage
2337 *
2338 * Drivers that have ascendant voltage list can use this as their
2339 * map_voltage() operation.
2340 */
2341int regulator_map_voltage_ascend(struct regulator_dev *rdev,
2342				 int min_uV, int max_uV)
2343{
2344	int i, ret;
2345
2346	for (i = 0; i < rdev->desc->n_voltages; i++) {
2347		ret = rdev->desc->ops->list_voltage(rdev, i);
2348		if (ret < 0)
2349			continue;
2350
2351		if (ret > max_uV)
2352			break;
2353
2354		if (ret >= min_uV && ret <= max_uV)
2355			return i;
2356	}
2357
2358	return -EINVAL;
2359}
2360EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
2361
2362/**
2363 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2364 *
2365 * @rdev: Regulator to operate on
2366 * @min_uV: Lower bound for voltage
2367 * @max_uV: Upper bound for voltage
2368 *
2369 * Drivers providing min_uV and uV_step in their regulator_desc can
2370 * use this as their map_voltage() operation.
2371 */
2372int regulator_map_voltage_linear(struct regulator_dev *rdev,
2373				 int min_uV, int max_uV)
2374{
2375	int ret, voltage;
2376
2377	/* Allow uV_step to be 0 for fixed voltage */
2378	if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2379		if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2380			return 0;
2381		else
2382			return -EINVAL;
2383	}
2384
2385	if (!rdev->desc->uV_step) {
2386		BUG_ON(!rdev->desc->uV_step);
2387		return -EINVAL;
2388	}
2389
2390	if (min_uV < rdev->desc->min_uV)
2391		min_uV = rdev->desc->min_uV;
2392
2393	ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2394	if (ret < 0)
2395		return ret;
2396
2397	ret += rdev->desc->linear_min_sel;
2398
2399	/* Map back into a voltage to verify we're still in bounds */
2400	voltage = rdev->desc->ops->list_voltage(rdev, ret);
2401	if (voltage < min_uV || voltage > max_uV)
2402		return -EINVAL;
2403
2404	return ret;
2405}
2406EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2407
2408/**
2409 * regulator_map_voltage_linear - map_voltage() for multiple linear ranges
2410 *
2411 * @rdev: Regulator to operate on
2412 * @min_uV: Lower bound for voltage
2413 * @max_uV: Upper bound for voltage
2414 *
2415 * Drivers providing linear_ranges in their descriptor can use this as
2416 * their map_voltage() callback.
2417 */
2418int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
2419				       int min_uV, int max_uV)
2420{
2421	const struct regulator_linear_range *range;
2422	int ret = -EINVAL;
2423	int voltage, i;
2424
2425	if (!rdev->desc->n_linear_ranges) {
2426		BUG_ON(!rdev->desc->n_linear_ranges);
2427		return -EINVAL;
2428	}
2429
2430	for (i = 0; i < rdev->desc->n_linear_ranges; i++) {
2431		range = &rdev->desc->linear_ranges[i];
2432
2433		if (!(min_uV <= range->max_uV && max_uV >= range->min_uV))
2434			continue;
2435
2436		if (min_uV <= range->min_uV)
2437			min_uV = range->min_uV;
2438
2439		/* range->uV_step == 0 means fixed voltage range */
2440		if (range->uV_step == 0) {
2441			ret = 0;
2442		} else {
2443			ret = DIV_ROUND_UP(min_uV - range->min_uV,
2444					   range->uV_step);
2445			if (ret < 0)
2446				return ret;
2447		}
2448
2449		break;
2450	}
2451
2452	if (i == rdev->desc->n_linear_ranges)
2453		return -EINVAL;
2454
2455	/* Map back into a voltage to verify we're still in bounds */
2456	voltage = rdev->desc->ops->list_voltage(rdev, ret);
2457	if (voltage < min_uV || voltage > max_uV)
2458		return -EINVAL;
2459
2460	return ret;
2461}
2462EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range);
2463
2464static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2465				     int min_uV, int max_uV)
2466{
2467	int ret;
2468	int delay = 0;
2469	int best_val = 0;
2470	unsigned int selector;
2471	int old_selector = -1;
2472
2473	trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2474
2475	min_uV += rdev->constraints->uV_offset;
2476	max_uV += rdev->constraints->uV_offset;
2477
2478	/*
2479	 * If we can't obtain the old selector there is not enough
2480	 * info to call set_voltage_time_sel().
2481	 */
2482	if (_regulator_is_enabled(rdev) &&
2483	    rdev->desc->ops->set_voltage_time_sel &&
2484	    rdev->desc->ops->get_voltage_sel) {
2485		old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2486		if (old_selector < 0)
2487			return old_selector;
2488	}
2489
2490	if (rdev->desc->ops->set_voltage) {
2491		ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2492						   &selector);
2493
2494		if (ret >= 0) {
2495			if (rdev->desc->ops->list_voltage)
2496				best_val = rdev->desc->ops->list_voltage(rdev,
2497									 selector);
2498			else
2499				best_val = _regulator_get_voltage(rdev);
2500		}
2501
2502	} else if (rdev->desc->ops->set_voltage_sel) {
2503		if (rdev->desc->ops->map_voltage) {
2504			ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2505							   max_uV);
2506		} else {
2507			if (rdev->desc->ops->list_voltage ==
2508			    regulator_list_voltage_linear)
2509				ret = regulator_map_voltage_linear(rdev,
2510								min_uV, max_uV);
2511			else
2512				ret = regulator_map_voltage_iterate(rdev,
2513								min_uV, max_uV);
2514		}
2515
2516		if (ret >= 0) {
2517			best_val = rdev->desc->ops->list_voltage(rdev, ret);
2518			if (min_uV <= best_val && max_uV >= best_val) {
2519				selector = ret;
2520				if (old_selector == selector)
2521					ret = 0;
2522				else
2523					ret = rdev->desc->ops->set_voltage_sel(
2524								rdev, ret);
2525			} else {
2526				ret = -EINVAL;
2527			}
2528		}
2529	} else {
2530		ret = -EINVAL;
2531	}
2532
2533	/* Call set_voltage_time_sel if successfully obtained old_selector */
2534	if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2535	    old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
2536
2537		delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2538						old_selector, selector);
2539		if (delay < 0) {
2540			rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2541				  delay);
2542			delay = 0;
2543		}
2544
2545		/* Insert any necessary delays */
2546		if (delay >= 1000) {
2547			mdelay(delay / 1000);
2548			udelay(delay % 1000);
2549		} else if (delay) {
2550			udelay(delay);
2551		}
2552	}
2553
2554	if (ret == 0 && best_val >= 0) {
2555		unsigned long data = best_val;
2556
2557		_notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2558				     (void *)data);
2559	}
2560
2561	trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2562
2563	return ret;
2564}
2565
2566/**
2567 * regulator_set_voltage - set regulator output voltage
2568 * @regulator: regulator source
2569 * @min_uV: Minimum required voltage in uV
2570 * @max_uV: Maximum acceptable voltage in uV
2571 *
2572 * Sets a voltage regulator to the desired output voltage. This can be set
2573 * during any regulator state. IOW, regulator can be disabled or enabled.
2574 *
2575 * If the regulator is enabled then the voltage will change to the new value
2576 * immediately otherwise if the regulator is disabled the regulator will
2577 * output at the new voltage when enabled.
2578 *
2579 * NOTE: If the regulator is shared between several devices then the lowest
2580 * request voltage that meets the system constraints will be used.
2581 * Regulator system constraints must be set for this regulator before
2582 * calling this function otherwise this call will fail.
2583 */
2584int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2585{
2586	struct regulator_dev *rdev = regulator->rdev;
2587	int ret = 0;
2588	int old_min_uV, old_max_uV;
2589
2590	mutex_lock(&rdev->mutex);
2591
2592	/* If we're setting the same range as last time the change
2593	 * should be a noop (some cpufreq implementations use the same
2594	 * voltage for multiple frequencies, for example).
2595	 */
2596	if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2597		goto out;
2598
2599	/* sanity check */
2600	if (!rdev->desc->ops->set_voltage &&
2601	    !rdev->desc->ops->set_voltage_sel) {
2602		ret = -EINVAL;
2603		goto out;
2604	}
2605
2606	/* constraints check */
2607	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2608	if (ret < 0)
2609		goto out;
2610
2611	/* restore original values in case of error */
2612	old_min_uV = regulator->min_uV;
2613	old_max_uV = regulator->max_uV;
2614	regulator->min_uV = min_uV;
2615	regulator->max_uV = max_uV;
2616
2617	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2618	if (ret < 0)
2619		goto out2;
2620
2621	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2622	if (ret < 0)
2623		goto out2;
2624
2625out:
2626	mutex_unlock(&rdev->mutex);
2627	return ret;
2628out2:
2629	regulator->min_uV = old_min_uV;
2630	regulator->max_uV = old_max_uV;
2631	mutex_unlock(&rdev->mutex);
2632	return ret;
2633}
2634EXPORT_SYMBOL_GPL(regulator_set_voltage);
2635
2636/**
2637 * regulator_set_voltage_time - get raise/fall time
2638 * @regulator: regulator source
2639 * @old_uV: starting voltage in microvolts
2640 * @new_uV: target voltage in microvolts
2641 *
2642 * Provided with the starting and ending voltage, this function attempts to
2643 * calculate the time in microseconds required to rise or fall to this new
2644 * voltage.
2645 */
2646int regulator_set_voltage_time(struct regulator *regulator,
2647			       int old_uV, int new_uV)
2648{
2649	struct regulator_dev	*rdev = regulator->rdev;
2650	struct regulator_ops	*ops = rdev->desc->ops;
2651	int old_sel = -1;
2652	int new_sel = -1;
2653	int voltage;
2654	int i;
2655
2656	/* Currently requires operations to do this */
2657	if (!ops->list_voltage || !ops->set_voltage_time_sel
2658	    || !rdev->desc->n_voltages)
2659		return -EINVAL;
2660
2661	for (i = 0; i < rdev->desc->n_voltages; i++) {
2662		/* We only look for exact voltage matches here */
2663		voltage = regulator_list_voltage(regulator, i);
2664		if (voltage < 0)
2665			return -EINVAL;
2666		if (voltage == 0)
2667			continue;
2668		if (voltage == old_uV)
2669			old_sel = i;
2670		if (voltage == new_uV)
2671			new_sel = i;
2672	}
2673
2674	if (old_sel < 0 || new_sel < 0)
2675		return -EINVAL;
2676
2677	return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2678}
2679EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2680
2681/**
2682 * regulator_set_voltage_time_sel - get raise/fall time
2683 * @rdev: regulator source device
2684 * @old_selector: selector for starting voltage
2685 * @new_selector: selector for target voltage
2686 *
2687 * Provided with the starting and target voltage selectors, this function
2688 * returns time in microseconds required to rise or fall to this new voltage
2689 *
2690 * Drivers providing ramp_delay in regulation_constraints can use this as their
2691 * set_voltage_time_sel() operation.
2692 */
2693int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2694				   unsigned int old_selector,
2695				   unsigned int new_selector)
2696{
2697	unsigned int ramp_delay = 0;
2698	int old_volt, new_volt;
2699
2700	if (rdev->constraints->ramp_delay)
2701		ramp_delay = rdev->constraints->ramp_delay;
2702	else if (rdev->desc->ramp_delay)
2703		ramp_delay = rdev->desc->ramp_delay;
2704
2705	if (ramp_delay == 0) {
2706		rdev_warn(rdev, "ramp_delay not set\n");
2707		return 0;
2708	}
2709
2710	/* sanity check */
2711	if (!rdev->desc->ops->list_voltage)
2712		return -EINVAL;
2713
2714	old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2715	new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2716
2717	return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2718}
2719EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2720
2721/**
2722 * regulator_sync_voltage - re-apply last regulator output voltage
2723 * @regulator: regulator source
2724 *
2725 * Re-apply the last configured voltage.  This is intended to be used
2726 * where some external control source the consumer is cooperating with
2727 * has caused the configured voltage to change.
2728 */
2729int regulator_sync_voltage(struct regulator *regulator)
2730{
2731	struct regulator_dev *rdev = regulator->rdev;
2732	int ret, min_uV, max_uV;
2733
2734	mutex_lock(&rdev->mutex);
2735
2736	if (!rdev->desc->ops->set_voltage &&
2737	    !rdev->desc->ops->set_voltage_sel) {
2738		ret = -EINVAL;
2739		goto out;
2740	}
2741
2742	/* This is only going to work if we've had a voltage configured. */
2743	if (!regulator->min_uV && !regulator->max_uV) {
2744		ret = -EINVAL;
2745		goto out;
2746	}
2747
2748	min_uV = regulator->min_uV;
2749	max_uV = regulator->max_uV;
2750
2751	/* This should be a paranoia check... */
2752	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2753	if (ret < 0)
2754		goto out;
2755
2756	ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2757	if (ret < 0)
2758		goto out;
2759
2760	ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2761
2762out:
2763	mutex_unlock(&rdev->mutex);
2764	return ret;
2765}
2766EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2767
2768static int _regulator_get_voltage(struct regulator_dev *rdev)
2769{
2770	int sel, ret;
2771
2772	if (rdev->desc->ops->get_voltage_sel) {
2773		sel = rdev->desc->ops->get_voltage_sel(rdev);
2774		if (sel < 0)
2775			return sel;
2776		ret = rdev->desc->ops->list_voltage(rdev, sel);
2777	} else if (rdev->desc->ops->get_voltage) {
2778		ret = rdev->desc->ops->get_voltage(rdev);
2779	} else if (rdev->desc->ops->list_voltage) {
2780		ret = rdev->desc->ops->list_voltage(rdev, 0);
2781	} else {
2782		return -EINVAL;
2783	}
2784
2785	if (ret < 0)
2786		return ret;
2787	return ret - rdev->constraints->uV_offset;
2788}
2789
2790/**
2791 * regulator_get_voltage - get regulator output voltage
2792 * @regulator: regulator source
2793 *
2794 * This returns the current regulator voltage in uV.
2795 *
2796 * NOTE: If the regulator is disabled it will return the voltage value. This
2797 * function should not be used to determine regulator state.
2798 */
2799int regulator_get_voltage(struct regulator *regulator)
2800{
2801	int ret;
2802
2803	mutex_lock(&regulator->rdev->mutex);
2804
2805	ret = _regulator_get_voltage(regulator->rdev);
2806
2807	mutex_unlock(&regulator->rdev->mutex);
2808
2809	return ret;
2810}
2811EXPORT_SYMBOL_GPL(regulator_get_voltage);
2812
2813/**
2814 * regulator_set_current_limit - set regulator output current limit
2815 * @regulator: regulator source
2816 * @min_uA: Minimum supported current in uA
2817 * @max_uA: Maximum supported current in uA
2818 *
2819 * Sets current sink to the desired output current. This can be set during
2820 * any regulator state. IOW, regulator can be disabled or enabled.
2821 *
2822 * If the regulator is enabled then the current will change to the new value
2823 * immediately otherwise if the regulator is disabled the regulator will
2824 * output at the new current when enabled.
2825 *
2826 * NOTE: Regulator system constraints must be set for this regulator before
2827 * calling this function otherwise this call will fail.
2828 */
2829int regulator_set_current_limit(struct regulator *regulator,
2830			       int min_uA, int max_uA)
2831{
2832	struct regulator_dev *rdev = regulator->rdev;
2833	int ret;
2834
2835	mutex_lock(&rdev->mutex);
2836
2837	/* sanity check */
2838	if (!rdev->desc->ops->set_current_limit) {
2839		ret = -EINVAL;
2840		goto out;
2841	}
2842
2843	/* constraints check */
2844	ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2845	if (ret < 0)
2846		goto out;
2847
2848	ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2849out:
2850	mutex_unlock(&rdev->mutex);
2851	return ret;
2852}
2853EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2854
2855static int _regulator_get_current_limit(struct regulator_dev *rdev)
2856{
2857	int ret;
2858
2859	mutex_lock(&rdev->mutex);
2860
2861	/* sanity check */
2862	if (!rdev->desc->ops->get_current_limit) {
2863		ret = -EINVAL;
2864		goto out;
2865	}
2866
2867	ret = rdev->desc->ops->get_current_limit(rdev);
2868out:
2869	mutex_unlock(&rdev->mutex);
2870	return ret;
2871}
2872
2873/**
2874 * regulator_get_current_limit - get regulator output current
2875 * @regulator: regulator source
2876 *
2877 * This returns the current supplied by the specified current sink in uA.
2878 *
2879 * NOTE: If the regulator is disabled it will return the current value. This
2880 * function should not be used to determine regulator state.
2881 */
2882int regulator_get_current_limit(struct regulator *regulator)
2883{
2884	return _regulator_get_current_limit(regulator->rdev);
2885}
2886EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2887
2888/**
2889 * regulator_set_mode - set regulator operating mode
2890 * @regulator: regulator source
2891 * @mode: operating mode - one of the REGULATOR_MODE constants
2892 *
2893 * Set regulator operating mode to increase regulator efficiency or improve
2894 * regulation performance.
2895 *
2896 * NOTE: Regulator system constraints must be set for this regulator before
2897 * calling this function otherwise this call will fail.
2898 */
2899int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2900{
2901	struct regulator_dev *rdev = regulator->rdev;
2902	int ret;
2903	int regulator_curr_mode;
2904
2905	mutex_lock(&rdev->mutex);
2906
2907	/* sanity check */
2908	if (!rdev->desc->ops->set_mode) {
2909		ret = -EINVAL;
2910		goto out;
2911	}
2912
2913	/* return if the same mode is requested */
2914	if (rdev->desc->ops->get_mode) {
2915		regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2916		if (regulator_curr_mode == mode) {
2917			ret = 0;
2918			goto out;
2919		}
2920	}
2921
2922	/* constraints check */
2923	ret = regulator_mode_constrain(rdev, &mode);
2924	if (ret < 0)
2925		goto out;
2926
2927	ret = rdev->desc->ops->set_mode(rdev, mode);
2928out:
2929	mutex_unlock(&rdev->mutex);
2930	return ret;
2931}
2932EXPORT_SYMBOL_GPL(regulator_set_mode);
2933
2934static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2935{
2936	int ret;
2937
2938	mutex_lock(&rdev->mutex);
2939
2940	/* sanity check */
2941	if (!rdev->desc->ops->get_mode) {
2942		ret = -EINVAL;
2943		goto out;
2944	}
2945
2946	ret = rdev->desc->ops->get_mode(rdev);
2947out:
2948	mutex_unlock(&rdev->mutex);
2949	return ret;
2950}
2951
2952/**
2953 * regulator_get_mode - get regulator operating mode
2954 * @regulator: regulator source
2955 *
2956 * Get the current regulator operating mode.
2957 */
2958unsigned int regulator_get_mode(struct regulator *regulator)
2959{
2960	return _regulator_get_mode(regulator->rdev);
2961}
2962EXPORT_SYMBOL_GPL(regulator_get_mode);
2963
2964/**
2965 * regulator_set_optimum_mode - set regulator optimum operating mode
2966 * @regulator: regulator source
2967 * @uA_load: load current
2968 *
2969 * Notifies the regulator core of a new device load. This is then used by
2970 * DRMS (if enabled by constraints) to set the most efficient regulator
2971 * operating mode for the new regulator loading.
2972 *
2973 * Consumer devices notify their supply regulator of the maximum power
2974 * they will require (can be taken from device datasheet in the power
2975 * consumption tables) when they change operational status and hence power
2976 * state. Examples of operational state changes that can affect power
2977 * consumption are :-
2978 *
2979 *    o Device is opened / closed.
2980 *    o Device I/O is about to begin or has just finished.
2981 *    o Device is idling in between work.
2982 *
2983 * This information is also exported via sysfs to userspace.
2984 *
2985 * DRMS will sum the total requested load on the regulator and change
2986 * to the most efficient operating mode if platform constraints allow.
2987 *
2988 * Returns the new regulator mode or error.
2989 */
2990int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2991{
2992	struct regulator_dev *rdev = regulator->rdev;
2993	struct regulator *consumer;
2994	int ret, output_uV, input_uV = 0, total_uA_load = 0;
2995	unsigned int mode;
2996
2997	if (rdev->supply)
2998		input_uV = regulator_get_voltage(rdev->supply);
2999
3000	mutex_lock(&rdev->mutex);
3001
3002	/*
3003	 * first check to see if we can set modes at all, otherwise just
3004	 * tell the consumer everything is OK.
3005	 */
3006	regulator->uA_load = uA_load;
3007	ret = regulator_check_drms(rdev);
3008	if (ret < 0) {
3009		ret = 0;
3010		goto out;
3011	}
3012
3013	if (!rdev->desc->ops->get_optimum_mode)
3014		goto out;
3015
3016	/*
3017	 * we can actually do this so any errors are indicators of
3018	 * potential real failure.
3019	 */
3020	ret = -EINVAL;
3021
3022	if (!rdev->desc->ops->set_mode)
3023		goto out;
3024
3025	/* get output voltage */
3026	output_uV = _regulator_get_voltage(rdev);
3027	if (output_uV <= 0) {
3028		rdev_err(rdev, "invalid output voltage found\n");
3029		goto out;
3030	}
3031
3032	/* No supply? Use constraint voltage */
3033	if (input_uV <= 0)
3034		input_uV = rdev->constraints->input_uV;
3035	if (input_uV <= 0) {
3036		rdev_err(rdev, "invalid input voltage found\n");
3037		goto out;
3038	}
3039
3040	/* calc total requested load for this regulator */
3041	list_for_each_entry(consumer, &rdev->consumer_list, list)
3042		total_uA_load += consumer->uA_load;
3043
3044	mode = rdev->desc->ops->get_optimum_mode(rdev,
3045						 input_uV, output_uV,
3046						 total_uA_load);
3047	ret = regulator_mode_constrain(rdev, &mode);
3048	if (ret < 0) {
3049		rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
3050			 total_uA_load, input_uV, output_uV);
3051		goto out;
3052	}
3053
3054	ret = rdev->desc->ops->set_mode(rdev, mode);
3055	if (ret < 0) {
3056		rdev_err(rdev, "failed to set optimum mode %x\n", mode);
3057		goto out;
3058	}
3059	ret = mode;
3060out:
3061	mutex_unlock(&rdev->mutex);
3062	return ret;
3063}
3064EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
3065
3066/**
3067 * regulator_set_bypass_regmap - Default set_bypass() using regmap
3068 *
3069 * @rdev: device to operate on.
3070 * @enable: state to set.
3071 */
3072int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
3073{
3074	unsigned int val;
3075
3076	if (enable)
3077		val = rdev->desc->bypass_mask;
3078	else
3079		val = 0;
3080
3081	return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
3082				  rdev->desc->bypass_mask, val);
3083}
3084EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
3085
3086/**
3087 * regulator_get_bypass_regmap - Default get_bypass() using regmap
3088 *
3089 * @rdev: device to operate on.
3090 * @enable: current state.
3091 */
3092int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
3093{
3094	unsigned int val;
3095	int ret;
3096
3097	ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
3098	if (ret != 0)
3099		return ret;
3100
3101	*enable = val & rdev->desc->bypass_mask;
3102
3103	return 0;
3104}
3105EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
3106
3107/**
3108 * regulator_allow_bypass - allow the regulator to go into bypass mode
3109 *
3110 * @regulator: Regulator to configure
3111 * @enable: enable or disable bypass mode
3112 *
3113 * Allow the regulator to go into bypass mode if all other consumers
3114 * for the regulator also enable bypass mode and the machine
3115 * constraints allow this.  Bypass mode means that the regulator is
3116 * simply passing the input directly to the output with no regulation.
3117 */
3118int regulator_allow_bypass(struct regulator *regulator, bool enable)
3119{
3120	struct regulator_dev *rdev = regulator->rdev;
3121	int ret = 0;
3122
3123	if (!rdev->desc->ops->set_bypass)
3124		return 0;
3125
3126	if (rdev->constraints &&
3127	    !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3128		return 0;
3129
3130	mutex_lock(&rdev->mutex);
3131
3132	if (enable && !regulator->bypass) {
3133		rdev->bypass_count++;
3134
3135		if (rdev->bypass_count == rdev->open_count) {
3136			ret = rdev->desc->ops->set_bypass(rdev, enable);
3137			if (ret != 0)
3138				rdev->bypass_count--;
3139		}
3140
3141	} else if (!enable && regulator->bypass) {
3142		rdev->bypass_count--;
3143
3144		if (rdev->bypass_count != rdev->open_count) {
3145			ret = rdev->desc->ops->set_bypass(rdev, enable);
3146			if (ret != 0)
3147				rdev->bypass_count++;
3148		}
3149	}
3150
3151	if (ret == 0)
3152		regulator->bypass = enable;
3153
3154	mutex_unlock(&rdev->mutex);
3155
3156	return ret;
3157}
3158EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3159
3160/**
3161 * regulator_register_notifier - register regulator event notifier
3162 * @regulator: regulator source
3163 * @nb: notifier block
3164 *
3165 * Register notifier block to receive regulator events.
3166 */
3167int regulator_register_notifier(struct regulator *regulator,
3168			      struct notifier_block *nb)
3169{
3170	return blocking_notifier_chain_register(&regulator->rdev->notifier,
3171						nb);
3172}
3173EXPORT_SYMBOL_GPL(regulator_register_notifier);
3174
3175/**
3176 * regulator_unregister_notifier - unregister regulator event notifier
3177 * @regulator: regulator source
3178 * @nb: notifier block
3179 *
3180 * Unregister regulator event notifier block.
3181 */
3182int regulator_unregister_notifier(struct regulator *regulator,
3183				struct notifier_block *nb)
3184{
3185	return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3186						  nb);
3187}
3188EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3189
3190/* notify regulator consumers and downstream regulator consumers.
3191 * Note mutex must be held by caller.
3192 */
3193static void _notifier_call_chain(struct regulator_dev *rdev,
3194				  unsigned long event, void *data)
3195{
3196	/* call rdev chain first */
3197	blocking_notifier_call_chain(&rdev->notifier, event, data);
3198}
3199
3200/**
3201 * regulator_bulk_get - get multiple regulator consumers
3202 *
3203 * @dev:           Device to supply
3204 * @num_consumers: Number of consumers to register
3205 * @consumers:     Configuration of consumers; clients are stored here.
3206 *
3207 * @return 0 on success, an errno on failure.
3208 *
3209 * This helper function allows drivers to get several regulator
3210 * consumers in one operation.  If any of the regulators cannot be
3211 * acquired then any regulators that were allocated will be freed
3212 * before returning to the caller.
3213 */
3214int regulator_bulk_get(struct device *dev, int num_consumers,
3215		       struct regulator_bulk_data *consumers)
3216{
3217	int i;
3218	int ret;
3219
3220	for (i = 0; i < num_consumers; i++)
3221		consumers[i].consumer = NULL;
3222
3223	for (i = 0; i < num_consumers; i++) {
3224		consumers[i].consumer = regulator_get(dev,
3225						      consumers[i].supply);
3226		if (IS_ERR(consumers[i].consumer)) {
3227			ret = PTR_ERR(consumers[i].consumer);
3228			dev_err(dev, "Failed to get supply '%s': %d\n",
3229				consumers[i].supply, ret);
3230			consumers[i].consumer = NULL;
3231			goto err;
3232		}
3233	}
3234
3235	return 0;
3236
3237err:
3238	while (--i >= 0)
3239		regulator_put(consumers[i].consumer);
3240
3241	return ret;
3242}
3243EXPORT_SYMBOL_GPL(regulator_bulk_get);
3244
3245/**
3246 * devm_regulator_bulk_get - managed get multiple regulator consumers
3247 *
3248 * @dev:           Device to supply
3249 * @num_consumers: Number of consumers to register
3250 * @consumers:     Configuration of consumers; clients are stored here.
3251 *
3252 * @return 0 on success, an errno on failure.
3253 *
3254 * This helper function allows drivers to get several regulator
3255 * consumers in one operation with management, the regulators will
3256 * automatically be freed when the device is unbound.  If any of the
3257 * regulators cannot be acquired then any regulators that were
3258 * allocated will be freed before returning to the caller.
3259 */
3260int devm_regulator_bulk_get(struct device *dev, int num_consumers,
3261			    struct regulator_bulk_data *consumers)
3262{
3263	int i;
3264	int ret;
3265
3266	for (i = 0; i < num_consumers; i++)
3267		consumers[i].consumer = NULL;
3268
3269	for (i = 0; i < num_consumers; i++) {
3270		consumers[i].consumer = devm_regulator_get(dev,
3271							   consumers[i].supply);
3272		if (IS_ERR(consumers[i].consumer)) {
3273			ret = PTR_ERR(consumers[i].consumer);
3274			dev_err(dev, "Failed to get supply '%s': %d\n",
3275				consumers[i].supply, ret);
3276			consumers[i].consumer = NULL;
3277			goto err;
3278		}
3279	}
3280
3281	return 0;
3282
3283err:
3284	for (i = 0; i < num_consumers && consumers[i].consumer; i++)
3285		devm_regulator_put(consumers[i].consumer);
3286
3287	return ret;
3288}
3289EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
3290
3291static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3292{
3293	struct regulator_bulk_data *bulk = data;
3294
3295	bulk->ret = regulator_enable(bulk->consumer);
3296}
3297
3298/**
3299 * regulator_bulk_enable - enable multiple regulator consumers
3300 *
3301 * @num_consumers: Number of consumers
3302 * @consumers:     Consumer data; clients are stored here.
3303 * @return         0 on success, an errno on failure
3304 *
3305 * This convenience API allows consumers to enable multiple regulator
3306 * clients in a single API call.  If any consumers cannot be enabled
3307 * then any others that were enabled will be disabled again prior to
3308 * return.
3309 */
3310int regulator_bulk_enable(int num_consumers,
3311			  struct regulator_bulk_data *consumers)
3312{
3313	ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3314	int i;
3315	int ret = 0;
3316
3317	for (i = 0; i < num_consumers; i++) {
3318		if (consumers[i].consumer->always_on)
3319			consumers[i].ret = 0;
3320		else
3321			async_schedule_domain(regulator_bulk_enable_async,
3322					      &consumers[i], &async_domain);
3323	}
3324
3325	async_synchronize_full_domain(&async_domain);
3326
3327	/* If any consumer failed we need to unwind any that succeeded */
3328	for (i = 0; i < num_consumers; i++) {
3329		if (consumers[i].ret != 0) {
3330			ret = consumers[i].ret;
3331			goto err;
3332		}
3333	}
3334
3335	return 0;
3336
3337err:
3338	for (i = 0; i < num_consumers; i++) {
3339		if (consumers[i].ret < 0)
3340			pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3341			       consumers[i].ret);
3342		else
3343			regulator_disable(consumers[i].consumer);
3344	}
3345
3346	return ret;
3347}
3348EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3349
3350/**
3351 * regulator_bulk_disable - disable multiple regulator consumers
3352 *
3353 * @num_consumers: Number of consumers
3354 * @consumers:     Consumer data; clients are stored here.
3355 * @return         0 on success, an errno on failure
3356 *
3357 * This convenience API allows consumers to disable multiple regulator
3358 * clients in a single API call.  If any consumers cannot be disabled
3359 * then any others that were disabled will be enabled again prior to
3360 * return.
3361 */
3362int regulator_bulk_disable(int num_consumers,
3363			   struct regulator_bulk_data *consumers)
3364{
3365	int i;
3366	int ret, r;
3367
3368	for (i = num_consumers - 1; i >= 0; --i) {
3369		ret = regulator_disable(consumers[i].consumer);
3370		if (ret != 0)
3371			goto err;
3372	}
3373
3374	return 0;
3375
3376err:
3377	pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3378	for (++i; i < num_consumers; ++i) {
3379		r = regulator_enable(consumers[i].consumer);
3380		if (r != 0)
3381			pr_err("Failed to reename %s: %d\n",
3382			       consumers[i].supply, r);
3383	}
3384
3385	return ret;
3386}
3387EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3388
3389/**
3390 * regulator_bulk_force_disable - force disable multiple regulator consumers
3391 *
3392 * @num_consumers: Number of consumers
3393 * @consumers:     Consumer data; clients are stored here.
3394 * @return         0 on success, an errno on failure
3395 *
3396 * This convenience API allows consumers to forcibly disable multiple regulator
3397 * clients in a single API call.
3398 * NOTE: This should be used for situations when device damage will
3399 * likely occur if the regulators are not disabled (e.g. over temp).
3400 * Although regulator_force_disable function call for some consumers can
3401 * return error numbers, the function is called for all consumers.
3402 */
3403int regulator_bulk_force_disable(int num_consumers,
3404			   struct regulator_bulk_data *consumers)
3405{
3406	int i;
3407	int ret;
3408
3409	for (i = 0; i < num_consumers; i++)
3410		consumers[i].ret =
3411			    regulator_force_disable(consumers[i].consumer);
3412
3413	for (i = 0; i < num_consumers; i++) {
3414		if (consumers[i].ret != 0) {
3415			ret = consumers[i].ret;
3416			goto out;
3417		}
3418	}
3419
3420	return 0;
3421out:
3422	return ret;
3423}
3424EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3425
3426/**
3427 * regulator_bulk_free - free multiple regulator consumers
3428 *
3429 * @num_consumers: Number of consumers
3430 * @consumers:     Consumer data; clients are stored here.
3431 *
3432 * This convenience API allows consumers to free multiple regulator
3433 * clients in a single API call.
3434 */
3435void regulator_bulk_free(int num_consumers,
3436			 struct regulator_bulk_data *consumers)
3437{
3438	int i;
3439
3440	for (i = 0; i < num_consumers; i++) {
3441		regulator_put(consumers[i].consumer);
3442		consumers[i].consumer = NULL;
3443	}
3444}
3445EXPORT_SYMBOL_GPL(regulator_bulk_free);
3446
3447/**
3448 * regulator_notifier_call_chain - call regulator event notifier
3449 * @rdev: regulator source
3450 * @event: notifier block
3451 * @data: callback-specific data.
3452 *
3453 * Called by regulator drivers to notify clients a regulator event has
3454 * occurred. We also notify regulator clients downstream.
3455 * Note lock must be held by caller.
3456 */
3457int regulator_notifier_call_chain(struct regulator_dev *rdev,
3458				  unsigned long event, void *data)
3459{
3460	_notifier_call_chain(rdev, event, data);
3461	return NOTIFY_DONE;
3462
3463}
3464EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3465
3466/**
3467 * regulator_mode_to_status - convert a regulator mode into a status
3468 *
3469 * @mode: Mode to convert
3470 *
3471 * Convert a regulator mode into a status.
3472 */
3473int regulator_mode_to_status(unsigned int mode)
3474{
3475	switch (mode) {
3476	case REGULATOR_MODE_FAST:
3477		return REGULATOR_STATUS_FAST;
3478	case REGULATOR_MODE_NORMAL:
3479		return REGULATOR_STATUS_NORMAL;
3480	case REGULATOR_MODE_IDLE:
3481		return REGULATOR_STATUS_IDLE;
3482	case REGULATOR_MODE_STANDBY:
3483		return REGULATOR_STATUS_STANDBY;
3484	default:
3485		return REGULATOR_STATUS_UNDEFINED;
3486	}
3487}
3488EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3489
3490/*
3491 * To avoid cluttering sysfs (and memory) with useless state, only
3492 * create attributes that can be meaningfully displayed.
3493 */
3494static int add_regulator_attributes(struct regulator_dev *rdev)
3495{
3496	struct device		*dev = &rdev->dev;
3497	struct regulator_ops	*ops = rdev->desc->ops;
3498	int			status = 0;
3499
3500	/* some attributes need specific methods to be displayed */
3501	if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3502	    (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3503	    (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3504		status = device_create_file(dev, &dev_attr_microvolts);
3505		if (status < 0)
3506			return status;
3507	}
3508	if (ops->get_current_limit) {
3509		status = device_create_file(dev, &dev_attr_microamps);
3510		if (status < 0)
3511			return status;
3512	}
3513	if (ops->get_mode) {
3514		status = device_create_file(dev, &dev_attr_opmode);
3515		if (status < 0)
3516			return status;
3517	}
3518	if (rdev->ena_pin || ops->is_enabled) {
3519		status = device_create_file(dev, &dev_attr_state);
3520		if (status < 0)
3521			return status;
3522	}
3523	if (ops->get_status) {
3524		status = device_create_file(dev, &dev_attr_status);
3525		if (status < 0)
3526			return status;
3527	}
3528	if (ops->get_bypass) {
3529		status = device_create_file(dev, &dev_attr_bypass);
3530		if (status < 0)
3531			return status;
3532	}
3533
3534	/* some attributes are type-specific */
3535	if (rdev->desc->type == REGULATOR_CURRENT) {
3536		status = device_create_file(dev, &dev_attr_requested_microamps);
3537		if (status < 0)
3538			return status;
3539	}
3540
3541	/* all the other attributes exist to support constraints;
3542	 * don't show them if there are no constraints, or if the
3543	 * relevant supporting methods are missing.
3544	 */
3545	if (!rdev->constraints)
3546		return status;
3547
3548	/* constraints need specific supporting methods */
3549	if (ops->set_voltage || ops->set_voltage_sel) {
3550		status = device_create_file(dev, &dev_attr_min_microvolts);
3551		if (status < 0)
3552			return status;
3553		status = device_create_file(dev, &dev_attr_max_microvolts);
3554		if (status < 0)
3555			return status;
3556	}
3557	if (ops->set_current_limit) {
3558		status = device_create_file(dev, &dev_attr_min_microamps);
3559		if (status < 0)
3560			return status;
3561		status = device_create_file(dev, &dev_attr_max_microamps);
3562		if (status < 0)
3563			return status;
3564	}
3565
3566	status = device_create_file(dev, &dev_attr_suspend_standby_state);
3567	if (status < 0)
3568		return status;
3569	status = device_create_file(dev, &dev_attr_suspend_mem_state);
3570	if (status < 0)
3571		return status;
3572	status = device_create_file(dev, &dev_attr_suspend_disk_state);
3573	if (status < 0)
3574		return status;
3575
3576	if (ops->set_suspend_voltage) {
3577		status = device_create_file(dev,
3578				&dev_attr_suspend_standby_microvolts);
3579		if (status < 0)
3580			return status;
3581		status = device_create_file(dev,
3582				&dev_attr_suspend_mem_microvolts);
3583		if (status < 0)
3584			return status;
3585		status = device_create_file(dev,
3586				&dev_attr_suspend_disk_microvolts);
3587		if (status < 0)
3588			return status;
3589	}
3590
3591	if (ops->set_suspend_mode) {
3592		status = device_create_file(dev,
3593				&dev_attr_suspend_standby_mode);
3594		if (status < 0)
3595			return status;
3596		status = device_create_file(dev,
3597				&dev_attr_suspend_mem_mode);
3598		if (status < 0)
3599			return status;
3600		status = device_create_file(dev,
3601				&dev_attr_suspend_disk_mode);
3602		if (status < 0)
3603			return status;
3604	}
3605
3606	return status;
3607}
3608
3609static void rdev_init_debugfs(struct regulator_dev *rdev)
3610{
3611	rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3612	if (!rdev->debugfs) {
3613		rdev_warn(rdev, "Failed to create debugfs directory\n");
3614		return;
3615	}
3616
3617	debugfs_create_u32("use_count", 0444, rdev->debugfs,
3618			   &rdev->use_count);
3619	debugfs_create_u32("open_count", 0444, rdev->debugfs,
3620			   &rdev->open_count);
3621	debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3622			   &rdev->bypass_count);
3623}
3624
3625/**
3626 * regulator_register - register regulator
3627 * @regulator_desc: regulator to register
3628 * @config: runtime configuration for regulator
3629 *
3630 * Called by regulator drivers to register a regulator.
3631 * Returns a valid pointer to struct regulator_dev on success
3632 * or an ERR_PTR() on error.
3633 */
3634struct regulator_dev *
3635regulator_register(const struct regulator_desc *regulator_desc,
3636		   const struct regulator_config *config)
3637{
3638	const struct regulation_constraints *constraints = NULL;
3639	const struct regulator_init_data *init_data;
3640	static atomic_t regulator_no = ATOMIC_INIT(0);
3641	struct regulator_dev *rdev;
3642	struct device *dev;
3643	int ret, i;
3644	const char *supply = NULL;
3645
3646	if (regulator_desc == NULL || config == NULL)
3647		return ERR_PTR(-EINVAL);
3648
3649	dev = config->dev;
3650	WARN_ON(!dev);
3651
3652	if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3653		return ERR_PTR(-EINVAL);
3654
3655	if (regulator_desc->type != REGULATOR_VOLTAGE &&
3656	    regulator_desc->type != REGULATOR_CURRENT)
3657		return ERR_PTR(-EINVAL);
3658
3659	/* Only one of each should be implemented */
3660	WARN_ON(regulator_desc->ops->get_voltage &&
3661		regulator_desc->ops->get_voltage_sel);
3662	WARN_ON(regulator_desc->ops->set_voltage &&
3663		regulator_desc->ops->set_voltage_sel);
3664
3665	/* If we're using selectors we must implement list_voltage. */
3666	if (regulator_desc->ops->get_voltage_sel &&
3667	    !regulator_desc->ops->list_voltage) {
3668		return ERR_PTR(-EINVAL);
3669	}
3670	if (regulator_desc->ops->set_voltage_sel &&
3671	    !regulator_desc->ops->list_voltage) {
3672		return ERR_PTR(-EINVAL);
3673	}
3674
3675	init_data = config->init_data;
3676
3677	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3678	if (rdev == NULL)
3679		return ERR_PTR(-ENOMEM);
3680
3681	mutex_lock(&regulator_list_mutex);
3682
3683	mutex_init(&rdev->mutex);
3684	rdev->reg_data = config->driver_data;
3685	rdev->owner = regulator_desc->owner;
3686	rdev->desc = regulator_desc;
3687	if (config->regmap)
3688		rdev->regmap = config->regmap;
3689	else if (dev_get_regmap(dev, NULL))
3690		rdev->regmap = dev_get_regmap(dev, NULL);
3691	else if (dev->parent)
3692		rdev->regmap = dev_get_regmap(dev->parent, NULL);
3693	INIT_LIST_HEAD(&rdev->consumer_list);
3694	INIT_LIST_HEAD(&rdev->list);
3695	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3696	INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3697
3698	/* preform any regulator specific init */
3699	if (init_data && init_data->regulator_init) {
3700		ret = init_data->regulator_init(rdev->reg_data);
3701		if (ret < 0)
3702			goto clean;
3703	}
3704
3705	/* register with sysfs */
3706	rdev->dev.class = &regulator_class;
3707	rdev->dev.of_node = config->of_node;
3708	rdev->dev.parent = dev;
3709	dev_set_name(&rdev->dev, "regulator.%d",
3710		     atomic_inc_return(&regulator_no) - 1);
3711	ret = device_register(&rdev->dev);
3712	if (ret != 0) {
3713		put_device(&rdev->dev);
3714		goto clean;
3715	}
3716
3717	dev_set_drvdata(&rdev->dev, rdev);
3718
3719	if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3720		ret = regulator_ena_gpio_request(rdev, config);
3721		if (ret != 0) {
3722			rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3723				 config->ena_gpio, ret);
3724			goto wash;
3725		}
3726
3727		if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3728			rdev->ena_gpio_state = 1;
3729
3730		if (config->ena_gpio_invert)
3731			rdev->ena_gpio_state = !rdev->ena_gpio_state;
3732	}
3733
3734	/* set regulator constraints */
3735	if (init_data)
3736		constraints = &init_data->constraints;
3737
3738	ret = set_machine_constraints(rdev, constraints);
3739	if (ret < 0)
3740		goto scrub;
3741
3742	/* add attributes supported by this regulator */
3743	ret = add_regulator_attributes(rdev);
3744	if (ret < 0)
3745		goto scrub;
3746
3747	if (init_data && init_data->supply_regulator)
3748		supply = init_data->supply_regulator;
3749	else if (regulator_desc->supply_name)
3750		supply = regulator_desc->supply_name;
3751
3752	if (supply) {
3753		struct regulator_dev *r;
3754
3755		r = regulator_dev_lookup(dev, supply, &ret);
3756
3757		if (ret == -ENODEV) {
3758			/*
3759			 * No supply was specified for this regulator and
3760			 * there will never be one.
3761			 */
3762			ret = 0;
3763			goto add_dev;
3764		} else if (!r) {
3765			dev_err(dev, "Failed to find supply %s\n", supply);
3766			ret = -EPROBE_DEFER;
3767			goto scrub;
3768		}
3769
3770		ret = set_supply(rdev, r);
3771		if (ret < 0)
3772			goto scrub;
3773
3774		/* Enable supply if rail is enabled */
3775		if (_regulator_is_enabled(rdev)) {
3776			ret = regulator_enable(rdev->supply);
3777			if (ret < 0)
3778				goto scrub;
3779		}
3780	}
3781
3782add_dev:
3783	/* add consumers devices */
3784	if (init_data) {
3785		for (i = 0; i < init_data->num_consumer_supplies; i++) {
3786			ret = set_consumer_device_supply(rdev,
3787				init_data->consumer_supplies[i].dev_name,
3788				init_data->consumer_supplies[i].supply);
3789			if (ret < 0) {
3790				dev_err(dev, "Failed to set supply %s\n",
3791					init_data->consumer_supplies[i].supply);
3792				goto unset_supplies;
3793			}
3794		}
3795	}
3796
3797	list_add(&rdev->list, &regulator_list);
3798
3799	rdev_init_debugfs(rdev);
3800out:
3801	mutex_unlock(&regulator_list_mutex);
3802	return rdev;
3803
3804unset_supplies:
3805	unset_regulator_supplies(rdev);
3806
3807scrub:
3808	if (rdev->supply)
3809		_regulator_put(rdev->supply);
3810	regulator_ena_gpio_free(rdev);
3811	kfree(rdev->constraints);
3812wash:
3813	device_unregister(&rdev->dev);
3814	/* device core frees rdev */
3815	rdev = ERR_PTR(ret);
3816	goto out;
3817
3818clean:
3819	kfree(rdev);
3820	rdev = ERR_PTR(ret);
3821	goto out;
3822}
3823EXPORT_SYMBOL_GPL(regulator_register);
3824
3825/**
3826 * regulator_unregister - unregister regulator
3827 * @rdev: regulator to unregister
3828 *
3829 * Called by regulator drivers to unregister a regulator.
3830 */
3831void regulator_unregister(struct regulator_dev *rdev)
3832{
3833	if (rdev == NULL)
3834		return;
3835
3836	if (rdev->supply)
3837		regulator_put(rdev->supply);
3838	mutex_lock(&regulator_list_mutex);
3839	debugfs_remove_recursive(rdev->debugfs);
3840	flush_work(&rdev->disable_work.work);
3841	WARN_ON(rdev->open_count);
3842	unset_regulator_supplies(rdev);
3843	list_del(&rdev->list);
3844	kfree(rdev->constraints);
3845	regulator_ena_gpio_free(rdev);
3846	device_unregister(&rdev->dev);
3847	mutex_unlock(&regulator_list_mutex);
3848}
3849EXPORT_SYMBOL_GPL(regulator_unregister);
3850
3851/**
3852 * regulator_suspend_prepare - prepare regulators for system wide suspend
3853 * @state: system suspend state
3854 *
3855 * Configure each regulator with it's suspend operating parameters for state.
3856 * This will usually be called by machine suspend code prior to supending.
3857 */
3858int regulator_suspend_prepare(suspend_state_t state)
3859{
3860	struct regulator_dev *rdev;
3861	int ret = 0;
3862
3863	/* ON is handled by regulator active state */
3864	if (state == PM_SUSPEND_ON)
3865		return -EINVAL;
3866
3867	mutex_lock(&regulator_list_mutex);
3868	list_for_each_entry(rdev, &regulator_list, list) {
3869
3870		mutex_lock(&rdev->mutex);
3871		ret = suspend_prepare(rdev, state);
3872		mutex_unlock(&rdev->mutex);
3873
3874		if (ret < 0) {
3875			rdev_err(rdev, "failed to prepare\n");
3876			goto out;
3877		}
3878	}
3879out:
3880	mutex_unlock(&regulator_list_mutex);
3881	return ret;
3882}
3883EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3884
3885/**
3886 * regulator_suspend_finish - resume regulators from system wide suspend
3887 *
3888 * Turn on regulators that might be turned off by regulator_suspend_prepare
3889 * and that should be turned on according to the regulators properties.
3890 */
3891int regulator_suspend_finish(void)
3892{
3893	struct regulator_dev *rdev;
3894	int ret = 0, error;
3895
3896	mutex_lock(&regulator_list_mutex);
3897	list_for_each_entry(rdev, &regulator_list, list) {
3898		struct regulator_ops *ops = rdev->desc->ops;
3899
3900		mutex_lock(&rdev->mutex);
3901		if ((rdev->use_count > 0  || rdev->constraints->always_on) &&
3902				ops->enable) {
3903			error = ops->enable(rdev);
3904			if (error)
3905				ret = error;
3906		} else {
3907			if (!has_full_constraints)
3908				goto unlock;
3909			if (!ops->disable)
3910				goto unlock;
3911			if (!_regulator_is_enabled(rdev))
3912				goto unlock;
3913
3914			error = ops->disable(rdev);
3915			if (error)
3916				ret = error;
3917		}
3918unlock:
3919		mutex_unlock(&rdev->mutex);
3920	}
3921	mutex_unlock(&regulator_list_mutex);
3922	return ret;
3923}
3924EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3925
3926/**
3927 * regulator_has_full_constraints - the system has fully specified constraints
3928 *
3929 * Calling this function will cause the regulator API to disable all
3930 * regulators which have a zero use count and don't have an always_on
3931 * constraint in a late_initcall.
3932 *
3933 * The intention is that this will become the default behaviour in a
3934 * future kernel release so users are encouraged to use this facility
3935 * now.
3936 */
3937void regulator_has_full_constraints(void)
3938{
3939	has_full_constraints = 1;
3940}
3941EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3942
3943/**
3944 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3945 *
3946 * Calling this function will cause the regulator API to provide a
3947 * dummy regulator to consumers if no physical regulator is found,
3948 * allowing most consumers to proceed as though a regulator were
3949 * configured.  This allows systems such as those with software
3950 * controllable regulators for the CPU core only to be brought up more
3951 * readily.
3952 */
3953void regulator_use_dummy_regulator(void)
3954{
3955	board_wants_dummy_regulator = true;
3956}
3957EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3958
3959/**
3960 * rdev_get_drvdata - get rdev regulator driver data
3961 * @rdev: regulator
3962 *
3963 * Get rdev regulator driver private data. This call can be used in the
3964 * regulator driver context.
3965 */
3966void *rdev_get_drvdata(struct regulator_dev *rdev)
3967{
3968	return rdev->reg_data;
3969}
3970EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3971
3972/**
3973 * regulator_get_drvdata - get regulator driver data
3974 * @regulator: regulator
3975 *
3976 * Get regulator driver private data. This call can be used in the consumer
3977 * driver context when non API regulator specific functions need to be called.
3978 */
3979void *regulator_get_drvdata(struct regulator *regulator)
3980{
3981	return regulator->rdev->reg_data;
3982}
3983EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3984
3985/**
3986 * regulator_set_drvdata - set regulator driver data
3987 * @regulator: regulator
3988 * @data: data
3989 */
3990void regulator_set_drvdata(struct regulator *regulator, void *data)
3991{
3992	regulator->rdev->reg_data = data;
3993}
3994EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3995
3996/**
3997 * regulator_get_id - get regulator ID
3998 * @rdev: regulator
3999 */
4000int rdev_get_id(struct regulator_dev *rdev)
4001{
4002	return rdev->desc->id;
4003}
4004EXPORT_SYMBOL_GPL(rdev_get_id);
4005
4006struct device *rdev_get_dev(struct regulator_dev *rdev)
4007{
4008	return &rdev->dev;
4009}
4010EXPORT_SYMBOL_GPL(rdev_get_dev);
4011
4012void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4013{
4014	return reg_init_data->driver_data;
4015}
4016EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4017
4018#ifdef CONFIG_DEBUG_FS
4019static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4020				    size_t count, loff_t *ppos)
4021{
4022	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4023	ssize_t len, ret = 0;
4024	struct regulator_map *map;
4025
4026	if (!buf)
4027		return -ENOMEM;
4028
4029	list_for_each_entry(map, &regulator_map_list, list) {
4030		len = snprintf(buf + ret, PAGE_SIZE - ret,
4031			       "%s -> %s.%s\n",
4032			       rdev_get_name(map->regulator), map->dev_name,
4033			       map->supply);
4034		if (len >= 0)
4035			ret += len;
4036		if (ret > PAGE_SIZE) {
4037			ret = PAGE_SIZE;
4038			break;
4039		}
4040	}
4041
4042	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4043
4044	kfree(buf);
4045
4046	return ret;
4047}
4048#endif
4049
4050static const struct file_operations supply_map_fops = {
4051#ifdef CONFIG_DEBUG_FS
4052	.read = supply_map_read_file,
4053	.llseek = default_llseek,
4054#endif
4055};
4056
4057static int __init regulator_init(void)
4058{
4059	int ret;
4060
4061	ret = class_register(&regulator_class);
4062
4063	debugfs_root = debugfs_create_dir("regulator", NULL);
4064	if (!debugfs_root)
4065		pr_warn("regulator: Failed to create debugfs directory\n");
4066
4067	debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4068			    &supply_map_fops);
4069
4070	regulator_dummy_init();
4071
4072	return ret;
4073}
4074
4075/* init early to allow our consumers to complete system booting */
4076core_initcall(regulator_init);
4077
4078static int __init regulator_init_complete(void)
4079{
4080	struct regulator_dev *rdev;
4081	struct regulator_ops *ops;
4082	struct regulation_constraints *c;
4083	int enabled, ret;
4084
4085	/*
4086	 * Since DT doesn't provide an idiomatic mechanism for
4087	 * enabling full constraints and since it's much more natural
4088	 * with DT to provide them just assume that a DT enabled
4089	 * system has full constraints.
4090	 */
4091	if (of_have_populated_dt())
4092		has_full_constraints = true;
4093
4094	mutex_lock(&regulator_list_mutex);
4095
4096	/* If we have a full configuration then disable any regulators
4097	 * which are not in use or always_on.  This will become the
4098	 * default behaviour in the future.
4099	 */
4100	list_for_each_entry(rdev, &regulator_list, list) {
4101		ops = rdev->desc->ops;
4102		c = rdev->constraints;
4103
4104		if (!ops->disable || (c && c->always_on))
4105			continue;
4106
4107		mutex_lock(&rdev->mutex);
4108
4109		if (rdev->use_count)
4110			goto unlock;
4111
4112		/* If we can't read the status assume it's on. */
4113		if (ops->is_enabled)
4114			enabled = ops->is_enabled(rdev);
4115		else
4116			enabled = 1;
4117
4118		if (!enabled)
4119			goto unlock;
4120
4121		if (has_full_constraints) {
4122			/* We log since this may kill the system if it
4123			 * goes wrong. */
4124			rdev_info(rdev, "disabling\n");
4125			ret = ops->disable(rdev);
4126			if (ret != 0) {
4127				rdev_err(rdev, "couldn't disable: %d\n", ret);
4128			}
4129		} else {
4130			/* The intention is that in future we will
4131			 * assume that full constraints are provided
4132			 * so warn even if we aren't going to do
4133			 * anything here.
4134			 */
4135			rdev_warn(rdev, "incomplete constraints, leaving on\n");
4136		}
4137
4138unlock:
4139		mutex_unlock(&rdev->mutex);
4140	}
4141
4142	mutex_unlock(&regulator_list_mutex);
4143
4144	return 0;
4145}
4146late_initcall(regulator_init_complete);
4147