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