core.c revision 5da84fd99bb1ab1c7cd39d0cf7c08bb63931a59a
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#define pr_fmt(fmt) "%s: " fmt, __func__
17
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/slab.h>
22#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
25#include <linux/delay.h>
26#include <linux/regulator/consumer.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/machine.h>
29
30#define CREATE_TRACE_POINTS
31#include <trace/events/regulator.h>
32
33#include "dummy.h"
34
35#define rdev_err(rdev, fmt, ...)					\
36	pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
37#define rdev_warn(rdev, fmt, ...)					\
38	pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
39#define rdev_info(rdev, fmt, ...)					\
40	pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
41#define rdev_dbg(rdev, fmt, ...)					\
42	pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43
44static DEFINE_MUTEX(regulator_list_mutex);
45static LIST_HEAD(regulator_list);
46static LIST_HEAD(regulator_map_list);
47static int has_full_constraints;
48static bool board_wants_dummy_regulator;
49
50/*
51 * struct regulator_map
52 *
53 * Used to provide symbolic supply names to devices.
54 */
55struct regulator_map {
56	struct list_head list;
57	const char *dev_name;   /* The dev_name() for the consumer */
58	const char *supply;
59	struct regulator_dev *regulator;
60};
61
62/*
63 * struct regulator
64 *
65 * One for each consumer device.
66 */
67struct regulator {
68	struct device *dev;
69	struct list_head list;
70	int uA_load;
71	int min_uV;
72	int max_uV;
73	char *supply_name;
74	struct device_attribute dev_attr;
75	struct regulator_dev *rdev;
76};
77
78static int _regulator_is_enabled(struct regulator_dev *rdev);
79static int _regulator_disable(struct regulator_dev *rdev,
80		struct regulator_dev **supply_rdev_ptr);
81static int _regulator_get_voltage(struct regulator_dev *rdev);
82static int _regulator_get_current_limit(struct regulator_dev *rdev);
83static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
84static void _notifier_call_chain(struct regulator_dev *rdev,
85				  unsigned long event, void *data);
86
87static const char *rdev_get_name(struct regulator_dev *rdev)
88{
89	if (rdev->constraints && rdev->constraints->name)
90		return rdev->constraints->name;
91	else if (rdev->desc->name)
92		return rdev->desc->name;
93	else
94		return "";
95}
96
97/* gets the regulator for a given consumer device */
98static struct regulator *get_device_regulator(struct device *dev)
99{
100	struct regulator *regulator = NULL;
101	struct regulator_dev *rdev;
102
103	mutex_lock(&regulator_list_mutex);
104	list_for_each_entry(rdev, &regulator_list, list) {
105		mutex_lock(&rdev->mutex);
106		list_for_each_entry(regulator, &rdev->consumer_list, list) {
107			if (regulator->dev == dev) {
108				mutex_unlock(&rdev->mutex);
109				mutex_unlock(&regulator_list_mutex);
110				return regulator;
111			}
112		}
113		mutex_unlock(&rdev->mutex);
114	}
115	mutex_unlock(&regulator_list_mutex);
116	return NULL;
117}
118
119/* Platform voltage constraint check */
120static int regulator_check_voltage(struct regulator_dev *rdev,
121				   int *min_uV, int *max_uV)
122{
123	BUG_ON(*min_uV > *max_uV);
124
125	if (!rdev->constraints) {
126		rdev_err(rdev, "no constraints\n");
127		return -ENODEV;
128	}
129	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
130		rdev_err(rdev, "operation not allowed\n");
131		return -EPERM;
132	}
133
134	if (*max_uV > rdev->constraints->max_uV)
135		*max_uV = rdev->constraints->max_uV;
136	if (*min_uV < rdev->constraints->min_uV)
137		*min_uV = rdev->constraints->min_uV;
138
139	if (*min_uV > *max_uV)
140		return -EINVAL;
141
142	return 0;
143}
144
145/* current constraint check */
146static int regulator_check_current_limit(struct regulator_dev *rdev,
147					int *min_uA, int *max_uA)
148{
149	BUG_ON(*min_uA > *max_uA);
150
151	if (!rdev->constraints) {
152		rdev_err(rdev, "no constraints\n");
153		return -ENODEV;
154	}
155	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
156		rdev_err(rdev, "operation not allowed\n");
157		return -EPERM;
158	}
159
160	if (*max_uA > rdev->constraints->max_uA)
161		*max_uA = rdev->constraints->max_uA;
162	if (*min_uA < rdev->constraints->min_uA)
163		*min_uA = rdev->constraints->min_uA;
164
165	if (*min_uA > *max_uA)
166		return -EINVAL;
167
168	return 0;
169}
170
171/* operating mode constraint check */
172static int regulator_check_mode(struct regulator_dev *rdev, int mode)
173{
174	switch (mode) {
175	case REGULATOR_MODE_FAST:
176	case REGULATOR_MODE_NORMAL:
177	case REGULATOR_MODE_IDLE:
178	case REGULATOR_MODE_STANDBY:
179		break;
180	default:
181		return -EINVAL;
182	}
183
184	if (!rdev->constraints) {
185		rdev_err(rdev, "no constraints\n");
186		return -ENODEV;
187	}
188	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
189		rdev_err(rdev, "operation not allowed\n");
190		return -EPERM;
191	}
192	if (!(rdev->constraints->valid_modes_mask & mode)) {
193		rdev_err(rdev, "invalid mode %x\n", mode);
194		return -EINVAL;
195	}
196	return 0;
197}
198
199/* dynamic regulator mode switching constraint check */
200static int regulator_check_drms(struct regulator_dev *rdev)
201{
202	if (!rdev->constraints) {
203		rdev_err(rdev, "no constraints\n");
204		return -ENODEV;
205	}
206	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
207		rdev_err(rdev, "operation not allowed\n");
208		return -EPERM;
209	}
210	return 0;
211}
212
213static ssize_t device_requested_uA_show(struct device *dev,
214			     struct device_attribute *attr, char *buf)
215{
216	struct regulator *regulator;
217
218	regulator = get_device_regulator(dev);
219	if (regulator == NULL)
220		return 0;
221
222	return sprintf(buf, "%d\n", regulator->uA_load);
223}
224
225static ssize_t regulator_uV_show(struct device *dev,
226				struct device_attribute *attr, char *buf)
227{
228	struct regulator_dev *rdev = dev_get_drvdata(dev);
229	ssize_t ret;
230
231	mutex_lock(&rdev->mutex);
232	ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
233	mutex_unlock(&rdev->mutex);
234
235	return ret;
236}
237static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
238
239static ssize_t regulator_uA_show(struct device *dev,
240				struct device_attribute *attr, char *buf)
241{
242	struct regulator_dev *rdev = dev_get_drvdata(dev);
243
244	return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
245}
246static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
247
248static ssize_t regulator_name_show(struct device *dev,
249			     struct device_attribute *attr, char *buf)
250{
251	struct regulator_dev *rdev = dev_get_drvdata(dev);
252
253	return sprintf(buf, "%s\n", rdev_get_name(rdev));
254}
255
256static ssize_t regulator_print_opmode(char *buf, int mode)
257{
258	switch (mode) {
259	case REGULATOR_MODE_FAST:
260		return sprintf(buf, "fast\n");
261	case REGULATOR_MODE_NORMAL:
262		return sprintf(buf, "normal\n");
263	case REGULATOR_MODE_IDLE:
264		return sprintf(buf, "idle\n");
265	case REGULATOR_MODE_STANDBY:
266		return sprintf(buf, "standby\n");
267	}
268	return sprintf(buf, "unknown\n");
269}
270
271static ssize_t regulator_opmode_show(struct device *dev,
272				    struct device_attribute *attr, char *buf)
273{
274	struct regulator_dev *rdev = dev_get_drvdata(dev);
275
276	return regulator_print_opmode(buf, _regulator_get_mode(rdev));
277}
278static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
279
280static ssize_t regulator_print_state(char *buf, int state)
281{
282	if (state > 0)
283		return sprintf(buf, "enabled\n");
284	else if (state == 0)
285		return sprintf(buf, "disabled\n");
286	else
287		return sprintf(buf, "unknown\n");
288}
289
290static ssize_t regulator_state_show(struct device *dev,
291				   struct device_attribute *attr, char *buf)
292{
293	struct regulator_dev *rdev = dev_get_drvdata(dev);
294	ssize_t ret;
295
296	mutex_lock(&rdev->mutex);
297	ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
298	mutex_unlock(&rdev->mutex);
299
300	return ret;
301}
302static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
303
304static ssize_t regulator_status_show(struct device *dev,
305				   struct device_attribute *attr, char *buf)
306{
307	struct regulator_dev *rdev = dev_get_drvdata(dev);
308	int status;
309	char *label;
310
311	status = rdev->desc->ops->get_status(rdev);
312	if (status < 0)
313		return status;
314
315	switch (status) {
316	case REGULATOR_STATUS_OFF:
317		label = "off";
318		break;
319	case REGULATOR_STATUS_ON:
320		label = "on";
321		break;
322	case REGULATOR_STATUS_ERROR:
323		label = "error";
324		break;
325	case REGULATOR_STATUS_FAST:
326		label = "fast";
327		break;
328	case REGULATOR_STATUS_NORMAL:
329		label = "normal";
330		break;
331	case REGULATOR_STATUS_IDLE:
332		label = "idle";
333		break;
334	case REGULATOR_STATUS_STANDBY:
335		label = "standby";
336		break;
337	default:
338		return -ERANGE;
339	}
340
341	return sprintf(buf, "%s\n", label);
342}
343static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
344
345static ssize_t regulator_min_uA_show(struct device *dev,
346				    struct device_attribute *attr, char *buf)
347{
348	struct regulator_dev *rdev = dev_get_drvdata(dev);
349
350	if (!rdev->constraints)
351		return sprintf(buf, "constraint not defined\n");
352
353	return sprintf(buf, "%d\n", rdev->constraints->min_uA);
354}
355static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
356
357static ssize_t regulator_max_uA_show(struct device *dev,
358				    struct device_attribute *attr, char *buf)
359{
360	struct regulator_dev *rdev = dev_get_drvdata(dev);
361
362	if (!rdev->constraints)
363		return sprintf(buf, "constraint not defined\n");
364
365	return sprintf(buf, "%d\n", rdev->constraints->max_uA);
366}
367static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
368
369static ssize_t regulator_min_uV_show(struct device *dev,
370				    struct device_attribute *attr, char *buf)
371{
372	struct regulator_dev *rdev = dev_get_drvdata(dev);
373
374	if (!rdev->constraints)
375		return sprintf(buf, "constraint not defined\n");
376
377	return sprintf(buf, "%d\n", rdev->constraints->min_uV);
378}
379static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
380
381static ssize_t regulator_max_uV_show(struct device *dev,
382				    struct device_attribute *attr, char *buf)
383{
384	struct regulator_dev *rdev = dev_get_drvdata(dev);
385
386	if (!rdev->constraints)
387		return sprintf(buf, "constraint not defined\n");
388
389	return sprintf(buf, "%d\n", rdev->constraints->max_uV);
390}
391static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
392
393static ssize_t regulator_total_uA_show(struct device *dev,
394				      struct device_attribute *attr, char *buf)
395{
396	struct regulator_dev *rdev = dev_get_drvdata(dev);
397	struct regulator *regulator;
398	int uA = 0;
399
400	mutex_lock(&rdev->mutex);
401	list_for_each_entry(regulator, &rdev->consumer_list, list)
402		uA += regulator->uA_load;
403	mutex_unlock(&rdev->mutex);
404	return sprintf(buf, "%d\n", uA);
405}
406static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
407
408static ssize_t regulator_num_users_show(struct device *dev,
409				      struct device_attribute *attr, char *buf)
410{
411	struct regulator_dev *rdev = dev_get_drvdata(dev);
412	return sprintf(buf, "%d\n", rdev->use_count);
413}
414
415static ssize_t regulator_type_show(struct device *dev,
416				  struct device_attribute *attr, char *buf)
417{
418	struct regulator_dev *rdev = dev_get_drvdata(dev);
419
420	switch (rdev->desc->type) {
421	case REGULATOR_VOLTAGE:
422		return sprintf(buf, "voltage\n");
423	case REGULATOR_CURRENT:
424		return sprintf(buf, "current\n");
425	}
426	return sprintf(buf, "unknown\n");
427}
428
429static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
430				struct device_attribute *attr, char *buf)
431{
432	struct regulator_dev *rdev = dev_get_drvdata(dev);
433
434	return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
435}
436static DEVICE_ATTR(suspend_mem_microvolts, 0444,
437		regulator_suspend_mem_uV_show, NULL);
438
439static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
440				struct device_attribute *attr, char *buf)
441{
442	struct regulator_dev *rdev = dev_get_drvdata(dev);
443
444	return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
445}
446static DEVICE_ATTR(suspend_disk_microvolts, 0444,
447		regulator_suspend_disk_uV_show, NULL);
448
449static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
450				struct device_attribute *attr, char *buf)
451{
452	struct regulator_dev *rdev = dev_get_drvdata(dev);
453
454	return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
455}
456static DEVICE_ATTR(suspend_standby_microvolts, 0444,
457		regulator_suspend_standby_uV_show, NULL);
458
459static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
460				struct device_attribute *attr, char *buf)
461{
462	struct regulator_dev *rdev = dev_get_drvdata(dev);
463
464	return regulator_print_opmode(buf,
465		rdev->constraints->state_mem.mode);
466}
467static DEVICE_ATTR(suspend_mem_mode, 0444,
468		regulator_suspend_mem_mode_show, NULL);
469
470static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
471				struct device_attribute *attr, char *buf)
472{
473	struct regulator_dev *rdev = dev_get_drvdata(dev);
474
475	return regulator_print_opmode(buf,
476		rdev->constraints->state_disk.mode);
477}
478static DEVICE_ATTR(suspend_disk_mode, 0444,
479		regulator_suspend_disk_mode_show, NULL);
480
481static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
482				struct device_attribute *attr, char *buf)
483{
484	struct regulator_dev *rdev = dev_get_drvdata(dev);
485
486	return regulator_print_opmode(buf,
487		rdev->constraints->state_standby.mode);
488}
489static DEVICE_ATTR(suspend_standby_mode, 0444,
490		regulator_suspend_standby_mode_show, NULL);
491
492static ssize_t regulator_suspend_mem_state_show(struct device *dev,
493				   struct device_attribute *attr, char *buf)
494{
495	struct regulator_dev *rdev = dev_get_drvdata(dev);
496
497	return regulator_print_state(buf,
498			rdev->constraints->state_mem.enabled);
499}
500static DEVICE_ATTR(suspend_mem_state, 0444,
501		regulator_suspend_mem_state_show, NULL);
502
503static ssize_t regulator_suspend_disk_state_show(struct device *dev,
504				   struct device_attribute *attr, char *buf)
505{
506	struct regulator_dev *rdev = dev_get_drvdata(dev);
507
508	return regulator_print_state(buf,
509			rdev->constraints->state_disk.enabled);
510}
511static DEVICE_ATTR(suspend_disk_state, 0444,
512		regulator_suspend_disk_state_show, NULL);
513
514static ssize_t regulator_suspend_standby_state_show(struct device *dev,
515				   struct device_attribute *attr, char *buf)
516{
517	struct regulator_dev *rdev = dev_get_drvdata(dev);
518
519	return regulator_print_state(buf,
520			rdev->constraints->state_standby.enabled);
521}
522static DEVICE_ATTR(suspend_standby_state, 0444,
523		regulator_suspend_standby_state_show, NULL);
524
525
526/*
527 * These are the only attributes are present for all regulators.
528 * Other attributes are a function of regulator functionality.
529 */
530static struct device_attribute regulator_dev_attrs[] = {
531	__ATTR(name, 0444, regulator_name_show, NULL),
532	__ATTR(num_users, 0444, regulator_num_users_show, NULL),
533	__ATTR(type, 0444, regulator_type_show, NULL),
534	__ATTR_NULL,
535};
536
537static void regulator_dev_release(struct device *dev)
538{
539	struct regulator_dev *rdev = dev_get_drvdata(dev);
540	kfree(rdev);
541}
542
543static struct class regulator_class = {
544	.name = "regulator",
545	.dev_release = regulator_dev_release,
546	.dev_attrs = regulator_dev_attrs,
547};
548
549/* Calculate the new optimum regulator operating mode based on the new total
550 * consumer load. All locks held by caller */
551static void drms_uA_update(struct regulator_dev *rdev)
552{
553	struct regulator *sibling;
554	int current_uA = 0, output_uV, input_uV, err;
555	unsigned int mode;
556
557	err = regulator_check_drms(rdev);
558	if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
559	    !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
560		return;
561
562	/* get output voltage */
563	output_uV = rdev->desc->ops->get_voltage(rdev);
564	if (output_uV <= 0)
565		return;
566
567	/* get input voltage */
568	if (rdev->supply && rdev->supply->desc->ops->get_voltage)
569		input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
570	else
571		input_uV = rdev->constraints->input_uV;
572	if (input_uV <= 0)
573		return;
574
575	/* calc total requested load */
576	list_for_each_entry(sibling, &rdev->consumer_list, list)
577		current_uA += sibling->uA_load;
578
579	/* now get the optimum mode for our new total regulator load */
580	mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
581						  output_uV, current_uA);
582
583	/* check the new mode is allowed */
584	err = regulator_check_mode(rdev, mode);
585	if (err == 0)
586		rdev->desc->ops->set_mode(rdev, mode);
587}
588
589static int suspend_set_state(struct regulator_dev *rdev,
590	struct regulator_state *rstate)
591{
592	int ret = 0;
593	bool can_set_state;
594
595	can_set_state = rdev->desc->ops->set_suspend_enable &&
596		rdev->desc->ops->set_suspend_disable;
597
598	/* If we have no suspend mode configration don't set anything;
599	 * only warn if the driver actually makes the suspend mode
600	 * configurable.
601	 */
602	if (!rstate->enabled && !rstate->disabled) {
603		if (can_set_state)
604			rdev_warn(rdev, "No configuration\n");
605		return 0;
606	}
607
608	if (rstate->enabled && rstate->disabled) {
609		rdev_err(rdev, "invalid configuration\n");
610		return -EINVAL;
611	}
612
613	if (!can_set_state) {
614		rdev_err(rdev, "no way to set suspend state\n");
615		return -EINVAL;
616	}
617
618	if (rstate->enabled)
619		ret = rdev->desc->ops->set_suspend_enable(rdev);
620	else
621		ret = rdev->desc->ops->set_suspend_disable(rdev);
622	if (ret < 0) {
623		rdev_err(rdev, "failed to enabled/disable\n");
624		return ret;
625	}
626
627	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
628		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
629		if (ret < 0) {
630			rdev_err(rdev, "failed to set voltage\n");
631			return ret;
632		}
633	}
634
635	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
636		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
637		if (ret < 0) {
638			rdev_err(rdev, "failed to set mode\n");
639			return ret;
640		}
641	}
642	return ret;
643}
644
645/* locks held by caller */
646static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
647{
648	if (!rdev->constraints)
649		return -EINVAL;
650
651	switch (state) {
652	case PM_SUSPEND_STANDBY:
653		return suspend_set_state(rdev,
654			&rdev->constraints->state_standby);
655	case PM_SUSPEND_MEM:
656		return suspend_set_state(rdev,
657			&rdev->constraints->state_mem);
658	case PM_SUSPEND_MAX:
659		return suspend_set_state(rdev,
660			&rdev->constraints->state_disk);
661	default:
662		return -EINVAL;
663	}
664}
665
666static void print_constraints(struct regulator_dev *rdev)
667{
668	struct regulation_constraints *constraints = rdev->constraints;
669	char buf[80] = "";
670	int count = 0;
671	int ret;
672
673	if (constraints->min_uV && constraints->max_uV) {
674		if (constraints->min_uV == constraints->max_uV)
675			count += sprintf(buf + count, "%d mV ",
676					 constraints->min_uV / 1000);
677		else
678			count += sprintf(buf + count, "%d <--> %d mV ",
679					 constraints->min_uV / 1000,
680					 constraints->max_uV / 1000);
681	}
682
683	if (!constraints->min_uV ||
684	    constraints->min_uV != constraints->max_uV) {
685		ret = _regulator_get_voltage(rdev);
686		if (ret > 0)
687			count += sprintf(buf + count, "at %d mV ", ret / 1000);
688	}
689
690	if (constraints->min_uA && constraints->max_uA) {
691		if (constraints->min_uA == constraints->max_uA)
692			count += sprintf(buf + count, "%d mA ",
693					 constraints->min_uA / 1000);
694		else
695			count += sprintf(buf + count, "%d <--> %d mA ",
696					 constraints->min_uA / 1000,
697					 constraints->max_uA / 1000);
698	}
699
700	if (!constraints->min_uA ||
701	    constraints->min_uA != constraints->max_uA) {
702		ret = _regulator_get_current_limit(rdev);
703		if (ret > 0)
704			count += sprintf(buf + count, "at %d mA ", ret / 1000);
705	}
706
707	if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
708		count += sprintf(buf + count, "fast ");
709	if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
710		count += sprintf(buf + count, "normal ");
711	if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
712		count += sprintf(buf + count, "idle ");
713	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
714		count += sprintf(buf + count, "standby");
715
716	rdev_info(rdev, "regulator: %s\n", buf);
717}
718
719static int machine_constraints_voltage(struct regulator_dev *rdev,
720	struct regulation_constraints *constraints)
721{
722	struct regulator_ops *ops = rdev->desc->ops;
723	int ret;
724	unsigned selector;
725
726	/* do we need to apply the constraint voltage */
727	if (rdev->constraints->apply_uV &&
728		rdev->constraints->min_uV == rdev->constraints->max_uV &&
729		ops->set_voltage) {
730		ret = ops->set_voltage(rdev,
731				       rdev->constraints->min_uV,
732				       rdev->constraints->max_uV,
733				       &selector);
734			if (ret < 0) {
735				rdev_err(rdev, "failed to apply %duV constraint\n",
736					 rdev->constraints->min_uV);
737				rdev->constraints = NULL;
738				return ret;
739			}
740	}
741
742	/* constrain machine-level voltage specs to fit
743	 * the actual range supported by this regulator.
744	 */
745	if (ops->list_voltage && rdev->desc->n_voltages) {
746		int	count = rdev->desc->n_voltages;
747		int	i;
748		int	min_uV = INT_MAX;
749		int	max_uV = INT_MIN;
750		int	cmin = constraints->min_uV;
751		int	cmax = constraints->max_uV;
752
753		/* it's safe to autoconfigure fixed-voltage supplies
754		   and the constraints are used by list_voltage. */
755		if (count == 1 && !cmin) {
756			cmin = 1;
757			cmax = INT_MAX;
758			constraints->min_uV = cmin;
759			constraints->max_uV = cmax;
760		}
761
762		/* voltage constraints are optional */
763		if ((cmin == 0) && (cmax == 0))
764			return 0;
765
766		/* else require explicit machine-level constraints */
767		if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
768			rdev_err(rdev, "invalid voltage constraints\n");
769			return -EINVAL;
770		}
771
772		/* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
773		for (i = 0; i < count; i++) {
774			int	value;
775
776			value = ops->list_voltage(rdev, i);
777			if (value <= 0)
778				continue;
779
780			/* maybe adjust [min_uV..max_uV] */
781			if (value >= cmin && value < min_uV)
782				min_uV = value;
783			if (value <= cmax && value > max_uV)
784				max_uV = value;
785		}
786
787		/* final: [min_uV..max_uV] valid iff constraints valid */
788		if (max_uV < min_uV) {
789			rdev_err(rdev, "unsupportable voltage constraints\n");
790			return -EINVAL;
791		}
792
793		/* use regulator's subset of machine constraints */
794		if (constraints->min_uV < min_uV) {
795			rdev_dbg(rdev, "override min_uV, %d -> %d\n",
796				 constraints->min_uV, min_uV);
797			constraints->min_uV = min_uV;
798		}
799		if (constraints->max_uV > max_uV) {
800			rdev_dbg(rdev, "override max_uV, %d -> %d\n",
801				 constraints->max_uV, max_uV);
802			constraints->max_uV = max_uV;
803		}
804	}
805
806	return 0;
807}
808
809/**
810 * set_machine_constraints - sets regulator constraints
811 * @rdev: regulator source
812 * @constraints: constraints to apply
813 *
814 * Allows platform initialisation code to define and constrain
815 * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
816 * Constraints *must* be set by platform code in order for some
817 * regulator operations to proceed i.e. set_voltage, set_current_limit,
818 * set_mode.
819 */
820static int set_machine_constraints(struct regulator_dev *rdev,
821	const struct regulation_constraints *constraints)
822{
823	int ret = 0;
824	struct regulator_ops *ops = rdev->desc->ops;
825
826	rdev->constraints = kmemdup(constraints, sizeof(*constraints),
827				    GFP_KERNEL);
828	if (!rdev->constraints)
829		return -ENOMEM;
830
831	ret = machine_constraints_voltage(rdev, rdev->constraints);
832	if (ret != 0)
833		goto out;
834
835	/* do we need to setup our suspend state */
836	if (constraints->initial_state) {
837		ret = suspend_prepare(rdev, rdev->constraints->initial_state);
838		if (ret < 0) {
839			rdev_err(rdev, "failed to set suspend state\n");
840			rdev->constraints = NULL;
841			goto out;
842		}
843	}
844
845	if (constraints->initial_mode) {
846		if (!ops->set_mode) {
847			rdev_err(rdev, "no set_mode operation\n");
848			ret = -EINVAL;
849			goto out;
850		}
851
852		ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
853		if (ret < 0) {
854			rdev_err(rdev, "failed to set initial mode: %d\n", ret);
855			goto out;
856		}
857	}
858
859	/* If the constraints say the regulator should be on at this point
860	 * and we have control then make sure it is enabled.
861	 */
862	if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
863	    ops->enable) {
864		ret = ops->enable(rdev);
865		if (ret < 0) {
866			rdev_err(rdev, "failed to enable\n");
867			rdev->constraints = NULL;
868			goto out;
869		}
870	}
871
872	print_constraints(rdev);
873out:
874	return ret;
875}
876
877/**
878 * set_supply - set regulator supply regulator
879 * @rdev: regulator name
880 * @supply_rdev: supply regulator name
881 *
882 * Called by platform initialisation code to set the supply regulator for this
883 * regulator. This ensures that a regulators supply will also be enabled by the
884 * core if it's child is enabled.
885 */
886static int set_supply(struct regulator_dev *rdev,
887	struct regulator_dev *supply_rdev)
888{
889	int err;
890
891	err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
892				"supply");
893	if (err) {
894		rdev_err(rdev, "could not add device link %s err %d\n",
895			 supply_rdev->dev.kobj.name, err);
896		       goto out;
897	}
898	rdev->supply = supply_rdev;
899	list_add(&rdev->slist, &supply_rdev->supply_list);
900out:
901	return err;
902}
903
904/**
905 * set_consumer_device_supply - Bind a regulator to a symbolic supply
906 * @rdev:         regulator source
907 * @consumer_dev: device the supply applies to
908 * @consumer_dev_name: dev_name() string for device supply applies to
909 * @supply:       symbolic name for supply
910 *
911 * Allows platform initialisation code to map physical regulator
912 * sources to symbolic names for supplies for use by devices.  Devices
913 * should use these symbolic names to request regulators, avoiding the
914 * need to provide board-specific regulator names as platform data.
915 *
916 * Only one of consumer_dev and consumer_dev_name may be specified.
917 */
918static int set_consumer_device_supply(struct regulator_dev *rdev,
919	struct device *consumer_dev, const char *consumer_dev_name,
920	const char *supply)
921{
922	struct regulator_map *node;
923	int has_dev;
924
925	if (consumer_dev && consumer_dev_name)
926		return -EINVAL;
927
928	if (!consumer_dev_name && consumer_dev)
929		consumer_dev_name = dev_name(consumer_dev);
930
931	if (supply == NULL)
932		return -EINVAL;
933
934	if (consumer_dev_name != NULL)
935		has_dev = 1;
936	else
937		has_dev = 0;
938
939	list_for_each_entry(node, &regulator_map_list, list) {
940		if (node->dev_name && consumer_dev_name) {
941			if (strcmp(node->dev_name, consumer_dev_name) != 0)
942				continue;
943		} else if (node->dev_name || consumer_dev_name) {
944			continue;
945		}
946
947		if (strcmp(node->supply, supply) != 0)
948			continue;
949
950		dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
951			dev_name(&node->regulator->dev),
952			node->regulator->desc->name,
953			supply,
954			dev_name(&rdev->dev), rdev_get_name(rdev));
955		return -EBUSY;
956	}
957
958	node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
959	if (node == NULL)
960		return -ENOMEM;
961
962	node->regulator = rdev;
963	node->supply = supply;
964
965	if (has_dev) {
966		node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
967		if (node->dev_name == NULL) {
968			kfree(node);
969			return -ENOMEM;
970		}
971	}
972
973	list_add(&node->list, &regulator_map_list);
974	return 0;
975}
976
977static void unset_regulator_supplies(struct regulator_dev *rdev)
978{
979	struct regulator_map *node, *n;
980
981	list_for_each_entry_safe(node, n, &regulator_map_list, list) {
982		if (rdev == node->regulator) {
983			list_del(&node->list);
984			kfree(node->dev_name);
985			kfree(node);
986		}
987	}
988}
989
990#define REG_STR_SIZE	32
991
992static struct regulator *create_regulator(struct regulator_dev *rdev,
993					  struct device *dev,
994					  const char *supply_name)
995{
996	struct regulator *regulator;
997	char buf[REG_STR_SIZE];
998	int err, size;
999
1000	regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1001	if (regulator == NULL)
1002		return NULL;
1003
1004	mutex_lock(&rdev->mutex);
1005	regulator->rdev = rdev;
1006	list_add(&regulator->list, &rdev->consumer_list);
1007
1008	if (dev) {
1009		/* create a 'requested_microamps_name' sysfs entry */
1010		size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1011			supply_name);
1012		if (size >= REG_STR_SIZE)
1013			goto overflow_err;
1014
1015		regulator->dev = dev;
1016		sysfs_attr_init(&regulator->dev_attr.attr);
1017		regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1018		if (regulator->dev_attr.attr.name == NULL)
1019			goto attr_name_err;
1020
1021		regulator->dev_attr.attr.mode = 0444;
1022		regulator->dev_attr.show = device_requested_uA_show;
1023		err = device_create_file(dev, &regulator->dev_attr);
1024		if (err < 0) {
1025			rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1026			goto attr_name_err;
1027		}
1028
1029		/* also add a link to the device sysfs entry */
1030		size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1031				 dev->kobj.name, supply_name);
1032		if (size >= REG_STR_SIZE)
1033			goto attr_err;
1034
1035		regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1036		if (regulator->supply_name == NULL)
1037			goto attr_err;
1038
1039		err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1040					buf);
1041		if (err) {
1042			rdev_warn(rdev, "could not add device link %s err %d\n",
1043				  dev->kobj.name, err);
1044			goto link_name_err;
1045		}
1046	}
1047	mutex_unlock(&rdev->mutex);
1048	return regulator;
1049link_name_err:
1050	kfree(regulator->supply_name);
1051attr_err:
1052	device_remove_file(regulator->dev, &regulator->dev_attr);
1053attr_name_err:
1054	kfree(regulator->dev_attr.attr.name);
1055overflow_err:
1056	list_del(&regulator->list);
1057	kfree(regulator);
1058	mutex_unlock(&rdev->mutex);
1059	return NULL;
1060}
1061
1062static int _regulator_get_enable_time(struct regulator_dev *rdev)
1063{
1064	if (!rdev->desc->ops->enable_time)
1065		return 0;
1066	return rdev->desc->ops->enable_time(rdev);
1067}
1068
1069/* Internal regulator request function */
1070static struct regulator *_regulator_get(struct device *dev, const char *id,
1071					int exclusive)
1072{
1073	struct regulator_dev *rdev;
1074	struct regulator_map *map;
1075	struct regulator *regulator = ERR_PTR(-ENODEV);
1076	const char *devname = NULL;
1077	int ret;
1078
1079	if (id == NULL) {
1080		pr_err("get() with no identifier\n");
1081		return regulator;
1082	}
1083
1084	if (dev)
1085		devname = dev_name(dev);
1086
1087	mutex_lock(&regulator_list_mutex);
1088
1089	list_for_each_entry(map, &regulator_map_list, list) {
1090		/* If the mapping has a device set up it must match */
1091		if (map->dev_name &&
1092		    (!devname || strcmp(map->dev_name, devname)))
1093			continue;
1094
1095		if (strcmp(map->supply, id) == 0) {
1096			rdev = map->regulator;
1097			goto found;
1098		}
1099	}
1100
1101	if (board_wants_dummy_regulator) {
1102		rdev = dummy_regulator_rdev;
1103		goto found;
1104	}
1105
1106#ifdef CONFIG_REGULATOR_DUMMY
1107	if (!devname)
1108		devname = "deviceless";
1109
1110	/* If the board didn't flag that it was fully constrained then
1111	 * substitute in a dummy regulator so consumers can continue.
1112	 */
1113	if (!has_full_constraints) {
1114		pr_warn("%s supply %s not found, using dummy regulator\n",
1115			devname, id);
1116		rdev = dummy_regulator_rdev;
1117		goto found;
1118	}
1119#endif
1120
1121	mutex_unlock(&regulator_list_mutex);
1122	return regulator;
1123
1124found:
1125	if (rdev->exclusive) {
1126		regulator = ERR_PTR(-EPERM);
1127		goto out;
1128	}
1129
1130	if (exclusive && rdev->open_count) {
1131		regulator = ERR_PTR(-EBUSY);
1132		goto out;
1133	}
1134
1135	if (!try_module_get(rdev->owner))
1136		goto out;
1137
1138	regulator = create_regulator(rdev, dev, id);
1139	if (regulator == NULL) {
1140		regulator = ERR_PTR(-ENOMEM);
1141		module_put(rdev->owner);
1142	}
1143
1144	rdev->open_count++;
1145	if (exclusive) {
1146		rdev->exclusive = 1;
1147
1148		ret = _regulator_is_enabled(rdev);
1149		if (ret > 0)
1150			rdev->use_count = 1;
1151		else
1152			rdev->use_count = 0;
1153	}
1154
1155out:
1156	mutex_unlock(&regulator_list_mutex);
1157
1158	return regulator;
1159}
1160
1161/**
1162 * regulator_get - lookup and obtain a reference to a regulator.
1163 * @dev: device for regulator "consumer"
1164 * @id: Supply name or regulator ID.
1165 *
1166 * Returns a struct regulator corresponding to the regulator producer,
1167 * or IS_ERR() condition containing errno.
1168 *
1169 * Use of supply names configured via regulator_set_device_supply() is
1170 * strongly encouraged.  It is recommended that the supply name used
1171 * should match the name used for the supply and/or the relevant
1172 * device pins in the datasheet.
1173 */
1174struct regulator *regulator_get(struct device *dev, const char *id)
1175{
1176	return _regulator_get(dev, id, 0);
1177}
1178EXPORT_SYMBOL_GPL(regulator_get);
1179
1180/**
1181 * regulator_get_exclusive - obtain exclusive access to a regulator.
1182 * @dev: device for regulator "consumer"
1183 * @id: Supply name or regulator ID.
1184 *
1185 * Returns a struct regulator corresponding to the regulator producer,
1186 * or IS_ERR() condition containing errno.  Other consumers will be
1187 * unable to obtain this reference is held and the use count for the
1188 * regulator will be initialised to reflect the current state of the
1189 * regulator.
1190 *
1191 * This is intended for use by consumers which cannot tolerate shared
1192 * use of the regulator such as those which need to force the
1193 * regulator off for correct operation of the hardware they are
1194 * controlling.
1195 *
1196 * Use of supply names configured via regulator_set_device_supply() is
1197 * strongly encouraged.  It is recommended that the supply name used
1198 * should match the name used for the supply and/or the relevant
1199 * device pins in the datasheet.
1200 */
1201struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1202{
1203	return _regulator_get(dev, id, 1);
1204}
1205EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1206
1207/**
1208 * regulator_put - "free" the regulator source
1209 * @regulator: regulator source
1210 *
1211 * Note: drivers must ensure that all regulator_enable calls made on this
1212 * regulator source are balanced by regulator_disable calls prior to calling
1213 * this function.
1214 */
1215void regulator_put(struct regulator *regulator)
1216{
1217	struct regulator_dev *rdev;
1218
1219	if (regulator == NULL || IS_ERR(regulator))
1220		return;
1221
1222	mutex_lock(&regulator_list_mutex);
1223	rdev = regulator->rdev;
1224
1225	/* remove any sysfs entries */
1226	if (regulator->dev) {
1227		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1228		kfree(regulator->supply_name);
1229		device_remove_file(regulator->dev, &regulator->dev_attr);
1230		kfree(regulator->dev_attr.attr.name);
1231	}
1232	list_del(&regulator->list);
1233	kfree(regulator);
1234
1235	rdev->open_count--;
1236	rdev->exclusive = 0;
1237
1238	module_put(rdev->owner);
1239	mutex_unlock(&regulator_list_mutex);
1240}
1241EXPORT_SYMBOL_GPL(regulator_put);
1242
1243static int _regulator_can_change_status(struct regulator_dev *rdev)
1244{
1245	if (!rdev->constraints)
1246		return 0;
1247
1248	if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1249		return 1;
1250	else
1251		return 0;
1252}
1253
1254/* locks held by regulator_enable() */
1255static int _regulator_enable(struct regulator_dev *rdev)
1256{
1257	int ret, delay;
1258
1259	if (rdev->use_count == 0) {
1260		/* do we need to enable the supply regulator first */
1261		if (rdev->supply) {
1262			mutex_lock(&rdev->supply->mutex);
1263			ret = _regulator_enable(rdev->supply);
1264			mutex_unlock(&rdev->supply->mutex);
1265			if (ret < 0) {
1266				rdev_err(rdev, "failed to enable: %d\n", ret);
1267				return ret;
1268			}
1269		}
1270	}
1271
1272	/* check voltage and requested load before enabling */
1273	if (rdev->constraints &&
1274	    (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1275		drms_uA_update(rdev);
1276
1277	if (rdev->use_count == 0) {
1278		/* The regulator may on if it's not switchable or left on */
1279		ret = _regulator_is_enabled(rdev);
1280		if (ret == -EINVAL || ret == 0) {
1281			if (!_regulator_can_change_status(rdev))
1282				return -EPERM;
1283
1284			if (!rdev->desc->ops->enable)
1285				return -EINVAL;
1286
1287			/* Query before enabling in case configuration
1288			 * dependant.  */
1289			ret = _regulator_get_enable_time(rdev);
1290			if (ret >= 0) {
1291				delay = ret;
1292			} else {
1293				rdev_warn(rdev, "enable_time() failed: %d\n",
1294					   ret);
1295				delay = 0;
1296			}
1297
1298			trace_regulator_enable(rdev_get_name(rdev));
1299
1300			/* Allow the regulator to ramp; it would be useful
1301			 * to extend this for bulk operations so that the
1302			 * regulators can ramp together.  */
1303			ret = rdev->desc->ops->enable(rdev);
1304			if (ret < 0)
1305				return ret;
1306
1307			trace_regulator_enable_delay(rdev_get_name(rdev));
1308
1309			if (delay >= 1000) {
1310				mdelay(delay / 1000);
1311				udelay(delay % 1000);
1312			} else if (delay) {
1313				udelay(delay);
1314			}
1315
1316			trace_regulator_enable_complete(rdev_get_name(rdev));
1317
1318		} else if (ret < 0) {
1319			rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1320			return ret;
1321		}
1322		/* Fallthrough on positive return values - already enabled */
1323	}
1324
1325	rdev->use_count++;
1326
1327	return 0;
1328}
1329
1330/**
1331 * regulator_enable - enable regulator output
1332 * @regulator: regulator source
1333 *
1334 * Request that the regulator be enabled with the regulator output at
1335 * the predefined voltage or current value.  Calls to regulator_enable()
1336 * must be balanced with calls to regulator_disable().
1337 *
1338 * NOTE: the output value can be set by other drivers, boot loader or may be
1339 * hardwired in the regulator.
1340 */
1341int regulator_enable(struct regulator *regulator)
1342{
1343	struct regulator_dev *rdev = regulator->rdev;
1344	int ret = 0;
1345
1346	mutex_lock(&rdev->mutex);
1347	ret = _regulator_enable(rdev);
1348	mutex_unlock(&rdev->mutex);
1349	return ret;
1350}
1351EXPORT_SYMBOL_GPL(regulator_enable);
1352
1353/* locks held by regulator_disable() */
1354static int _regulator_disable(struct regulator_dev *rdev,
1355		struct regulator_dev **supply_rdev_ptr)
1356{
1357	int ret = 0;
1358	*supply_rdev_ptr = NULL;
1359
1360	if (WARN(rdev->use_count <= 0,
1361			"unbalanced disables for %s\n",
1362			rdev_get_name(rdev)))
1363		return -EIO;
1364
1365	/* are we the last user and permitted to disable ? */
1366	if (rdev->use_count == 1 &&
1367	    (rdev->constraints && !rdev->constraints->always_on)) {
1368
1369		/* we are last user */
1370		if (_regulator_can_change_status(rdev) &&
1371		    rdev->desc->ops->disable) {
1372			trace_regulator_disable(rdev_get_name(rdev));
1373
1374			ret = rdev->desc->ops->disable(rdev);
1375			if (ret < 0) {
1376				rdev_err(rdev, "failed to disable\n");
1377				return ret;
1378			}
1379
1380			trace_regulator_disable_complete(rdev_get_name(rdev));
1381
1382			_notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1383					     NULL);
1384		}
1385
1386		/* decrease our supplies ref count and disable if required */
1387		*supply_rdev_ptr = rdev->supply;
1388
1389		rdev->use_count = 0;
1390	} else if (rdev->use_count > 1) {
1391
1392		if (rdev->constraints &&
1393			(rdev->constraints->valid_ops_mask &
1394			REGULATOR_CHANGE_DRMS))
1395			drms_uA_update(rdev);
1396
1397		rdev->use_count--;
1398	}
1399	return ret;
1400}
1401
1402/**
1403 * regulator_disable - disable regulator output
1404 * @regulator: regulator source
1405 *
1406 * Disable the regulator output voltage or current.  Calls to
1407 * regulator_enable() must be balanced with calls to
1408 * regulator_disable().
1409 *
1410 * NOTE: this will only disable the regulator output if no other consumer
1411 * devices have it enabled, the regulator device supports disabling and
1412 * machine constraints permit this operation.
1413 */
1414int regulator_disable(struct regulator *regulator)
1415{
1416	struct regulator_dev *rdev = regulator->rdev;
1417	struct regulator_dev *supply_rdev = NULL;
1418	int ret = 0;
1419
1420	mutex_lock(&rdev->mutex);
1421	ret = _regulator_disable(rdev, &supply_rdev);
1422	mutex_unlock(&rdev->mutex);
1423
1424	/* decrease our supplies ref count and disable if required */
1425	while (supply_rdev != NULL) {
1426		rdev = supply_rdev;
1427
1428		mutex_lock(&rdev->mutex);
1429		_regulator_disable(rdev, &supply_rdev);
1430		mutex_unlock(&rdev->mutex);
1431	}
1432
1433	return ret;
1434}
1435EXPORT_SYMBOL_GPL(regulator_disable);
1436
1437/* locks held by regulator_force_disable() */
1438static int _regulator_force_disable(struct regulator_dev *rdev,
1439		struct regulator_dev **supply_rdev_ptr)
1440{
1441	int ret = 0;
1442
1443	/* force disable */
1444	if (rdev->desc->ops->disable) {
1445		/* ah well, who wants to live forever... */
1446		ret = rdev->desc->ops->disable(rdev);
1447		if (ret < 0) {
1448			rdev_err(rdev, "failed to force disable\n");
1449			return ret;
1450		}
1451		/* notify other consumers that power has been forced off */
1452		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1453			REGULATOR_EVENT_DISABLE, NULL);
1454	}
1455
1456	/* decrease our supplies ref count and disable if required */
1457	*supply_rdev_ptr = rdev->supply;
1458
1459	rdev->use_count = 0;
1460	return ret;
1461}
1462
1463/**
1464 * regulator_force_disable - force disable regulator output
1465 * @regulator: regulator source
1466 *
1467 * Forcibly disable the regulator output voltage or current.
1468 * NOTE: this *will* disable the regulator output even if other consumer
1469 * devices have it enabled. This should be used for situations when device
1470 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1471 */
1472int regulator_force_disable(struct regulator *regulator)
1473{
1474	struct regulator_dev *supply_rdev = NULL;
1475	int ret;
1476
1477	mutex_lock(&regulator->rdev->mutex);
1478	regulator->uA_load = 0;
1479	ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
1480	mutex_unlock(&regulator->rdev->mutex);
1481
1482	if (supply_rdev)
1483		regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1484
1485	return ret;
1486}
1487EXPORT_SYMBOL_GPL(regulator_force_disable);
1488
1489static int _regulator_is_enabled(struct regulator_dev *rdev)
1490{
1491	/* If we don't know then assume that the regulator is always on */
1492	if (!rdev->desc->ops->is_enabled)
1493		return 1;
1494
1495	return rdev->desc->ops->is_enabled(rdev);
1496}
1497
1498/**
1499 * regulator_is_enabled - is the regulator output enabled
1500 * @regulator: regulator source
1501 *
1502 * Returns positive if the regulator driver backing the source/client
1503 * has requested that the device be enabled, zero if it hasn't, else a
1504 * negative errno code.
1505 *
1506 * Note that the device backing this regulator handle can have multiple
1507 * users, so it might be enabled even if regulator_enable() was never
1508 * called for this particular source.
1509 */
1510int regulator_is_enabled(struct regulator *regulator)
1511{
1512	int ret;
1513
1514	mutex_lock(&regulator->rdev->mutex);
1515	ret = _regulator_is_enabled(regulator->rdev);
1516	mutex_unlock(&regulator->rdev->mutex);
1517
1518	return ret;
1519}
1520EXPORT_SYMBOL_GPL(regulator_is_enabled);
1521
1522/**
1523 * regulator_count_voltages - count regulator_list_voltage() selectors
1524 * @regulator: regulator source
1525 *
1526 * Returns number of selectors, or negative errno.  Selectors are
1527 * numbered starting at zero, and typically correspond to bitfields
1528 * in hardware registers.
1529 */
1530int regulator_count_voltages(struct regulator *regulator)
1531{
1532	struct regulator_dev	*rdev = regulator->rdev;
1533
1534	return rdev->desc->n_voltages ? : -EINVAL;
1535}
1536EXPORT_SYMBOL_GPL(regulator_count_voltages);
1537
1538/**
1539 * regulator_list_voltage - enumerate supported voltages
1540 * @regulator: regulator source
1541 * @selector: identify voltage to list
1542 * Context: can sleep
1543 *
1544 * Returns a voltage that can be passed to @regulator_set_voltage(),
1545 * zero if this selector code can't be used on this system, or a
1546 * negative errno.
1547 */
1548int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1549{
1550	struct regulator_dev	*rdev = regulator->rdev;
1551	struct regulator_ops	*ops = rdev->desc->ops;
1552	int			ret;
1553
1554	if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1555		return -EINVAL;
1556
1557	mutex_lock(&rdev->mutex);
1558	ret = ops->list_voltage(rdev, selector);
1559	mutex_unlock(&rdev->mutex);
1560
1561	if (ret > 0) {
1562		if (ret < rdev->constraints->min_uV)
1563			ret = 0;
1564		else if (ret > rdev->constraints->max_uV)
1565			ret = 0;
1566	}
1567
1568	return ret;
1569}
1570EXPORT_SYMBOL_GPL(regulator_list_voltage);
1571
1572/**
1573 * regulator_is_supported_voltage - check if a voltage range can be supported
1574 *
1575 * @regulator: Regulator to check.
1576 * @min_uV: Minimum required voltage in uV.
1577 * @max_uV: Maximum required voltage in uV.
1578 *
1579 * Returns a boolean or a negative error code.
1580 */
1581int regulator_is_supported_voltage(struct regulator *regulator,
1582				   int min_uV, int max_uV)
1583{
1584	int i, voltages, ret;
1585
1586	ret = regulator_count_voltages(regulator);
1587	if (ret < 0)
1588		return ret;
1589	voltages = ret;
1590
1591	for (i = 0; i < voltages; i++) {
1592		ret = regulator_list_voltage(regulator, i);
1593
1594		if (ret >= min_uV && ret <= max_uV)
1595			return 1;
1596	}
1597
1598	return 0;
1599}
1600
1601/**
1602 * regulator_set_voltage - set regulator output voltage
1603 * @regulator: regulator source
1604 * @min_uV: Minimum required voltage in uV
1605 * @max_uV: Maximum acceptable voltage in uV
1606 *
1607 * Sets a voltage regulator to the desired output voltage. This can be set
1608 * during any regulator state. IOW, regulator can be disabled or enabled.
1609 *
1610 * If the regulator is enabled then the voltage will change to the new value
1611 * immediately otherwise if the regulator is disabled the regulator will
1612 * output at the new voltage when enabled.
1613 *
1614 * NOTE: If the regulator is shared between several devices then the lowest
1615 * request voltage that meets the system constraints will be used.
1616 * Regulator system constraints must be set for this regulator before
1617 * calling this function otherwise this call will fail.
1618 */
1619int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1620{
1621	struct regulator_dev *rdev = regulator->rdev;
1622	int ret;
1623	unsigned selector;
1624
1625	mutex_lock(&rdev->mutex);
1626
1627	/* sanity check */
1628	if (!rdev->desc->ops->set_voltage) {
1629		ret = -EINVAL;
1630		goto out;
1631	}
1632
1633	/* constraints check */
1634	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1635	if (ret < 0)
1636		goto out;
1637	regulator->min_uV = min_uV;
1638	regulator->max_uV = max_uV;
1639
1640	trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1641
1642	ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, &selector);
1643
1644	if (rdev->desc->ops->list_voltage)
1645		selector = rdev->desc->ops->list_voltage(rdev, selector);
1646	else
1647		selector = -1;
1648
1649	trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1650
1651out:
1652	_notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1653	mutex_unlock(&rdev->mutex);
1654	return ret;
1655}
1656EXPORT_SYMBOL_GPL(regulator_set_voltage);
1657
1658static int _regulator_get_voltage(struct regulator_dev *rdev)
1659{
1660	/* sanity check */
1661	if (rdev->desc->ops->get_voltage)
1662		return rdev->desc->ops->get_voltage(rdev);
1663	else
1664		return -EINVAL;
1665}
1666
1667/**
1668 * regulator_get_voltage - get regulator output voltage
1669 * @regulator: regulator source
1670 *
1671 * This returns the current regulator voltage in uV.
1672 *
1673 * NOTE: If the regulator is disabled it will return the voltage value. This
1674 * function should not be used to determine regulator state.
1675 */
1676int regulator_get_voltage(struct regulator *regulator)
1677{
1678	int ret;
1679
1680	mutex_lock(&regulator->rdev->mutex);
1681
1682	ret = _regulator_get_voltage(regulator->rdev);
1683
1684	mutex_unlock(&regulator->rdev->mutex);
1685
1686	return ret;
1687}
1688EXPORT_SYMBOL_GPL(regulator_get_voltage);
1689
1690/**
1691 * regulator_set_current_limit - set regulator output current limit
1692 * @regulator: regulator source
1693 * @min_uA: Minimuum supported current in uA
1694 * @max_uA: Maximum supported current in uA
1695 *
1696 * Sets current sink to the desired output current. This can be set during
1697 * any regulator state. IOW, regulator can be disabled or enabled.
1698 *
1699 * If the regulator is enabled then the current will change to the new value
1700 * immediately otherwise if the regulator is disabled the regulator will
1701 * output at the new current when enabled.
1702 *
1703 * NOTE: Regulator system constraints must be set for this regulator before
1704 * calling this function otherwise this call will fail.
1705 */
1706int regulator_set_current_limit(struct regulator *regulator,
1707			       int min_uA, int max_uA)
1708{
1709	struct regulator_dev *rdev = regulator->rdev;
1710	int ret;
1711
1712	mutex_lock(&rdev->mutex);
1713
1714	/* sanity check */
1715	if (!rdev->desc->ops->set_current_limit) {
1716		ret = -EINVAL;
1717		goto out;
1718	}
1719
1720	/* constraints check */
1721	ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1722	if (ret < 0)
1723		goto out;
1724
1725	ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1726out:
1727	mutex_unlock(&rdev->mutex);
1728	return ret;
1729}
1730EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1731
1732static int _regulator_get_current_limit(struct regulator_dev *rdev)
1733{
1734	int ret;
1735
1736	mutex_lock(&rdev->mutex);
1737
1738	/* sanity check */
1739	if (!rdev->desc->ops->get_current_limit) {
1740		ret = -EINVAL;
1741		goto out;
1742	}
1743
1744	ret = rdev->desc->ops->get_current_limit(rdev);
1745out:
1746	mutex_unlock(&rdev->mutex);
1747	return ret;
1748}
1749
1750/**
1751 * regulator_get_current_limit - get regulator output current
1752 * @regulator: regulator source
1753 *
1754 * This returns the current supplied by the specified current sink in uA.
1755 *
1756 * NOTE: If the regulator is disabled it will return the current value. This
1757 * function should not be used to determine regulator state.
1758 */
1759int regulator_get_current_limit(struct regulator *regulator)
1760{
1761	return _regulator_get_current_limit(regulator->rdev);
1762}
1763EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1764
1765/**
1766 * regulator_set_mode - set regulator operating mode
1767 * @regulator: regulator source
1768 * @mode: operating mode - one of the REGULATOR_MODE constants
1769 *
1770 * Set regulator operating mode to increase regulator efficiency or improve
1771 * regulation performance.
1772 *
1773 * NOTE: Regulator system constraints must be set for this regulator before
1774 * calling this function otherwise this call will fail.
1775 */
1776int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1777{
1778	struct regulator_dev *rdev = regulator->rdev;
1779	int ret;
1780	int regulator_curr_mode;
1781
1782	mutex_lock(&rdev->mutex);
1783
1784	/* sanity check */
1785	if (!rdev->desc->ops->set_mode) {
1786		ret = -EINVAL;
1787		goto out;
1788	}
1789
1790	/* return if the same mode is requested */
1791	if (rdev->desc->ops->get_mode) {
1792		regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1793		if (regulator_curr_mode == mode) {
1794			ret = 0;
1795			goto out;
1796		}
1797	}
1798
1799	/* constraints check */
1800	ret = regulator_check_mode(rdev, mode);
1801	if (ret < 0)
1802		goto out;
1803
1804	ret = rdev->desc->ops->set_mode(rdev, mode);
1805out:
1806	mutex_unlock(&rdev->mutex);
1807	return ret;
1808}
1809EXPORT_SYMBOL_GPL(regulator_set_mode);
1810
1811static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1812{
1813	int ret;
1814
1815	mutex_lock(&rdev->mutex);
1816
1817	/* sanity check */
1818	if (!rdev->desc->ops->get_mode) {
1819		ret = -EINVAL;
1820		goto out;
1821	}
1822
1823	ret = rdev->desc->ops->get_mode(rdev);
1824out:
1825	mutex_unlock(&rdev->mutex);
1826	return ret;
1827}
1828
1829/**
1830 * regulator_get_mode - get regulator operating mode
1831 * @regulator: regulator source
1832 *
1833 * Get the current regulator operating mode.
1834 */
1835unsigned int regulator_get_mode(struct regulator *regulator)
1836{
1837	return _regulator_get_mode(regulator->rdev);
1838}
1839EXPORT_SYMBOL_GPL(regulator_get_mode);
1840
1841/**
1842 * regulator_set_optimum_mode - set regulator optimum operating mode
1843 * @regulator: regulator source
1844 * @uA_load: load current
1845 *
1846 * Notifies the regulator core of a new device load. This is then used by
1847 * DRMS (if enabled by constraints) to set the most efficient regulator
1848 * operating mode for the new regulator loading.
1849 *
1850 * Consumer devices notify their supply regulator of the maximum power
1851 * they will require (can be taken from device datasheet in the power
1852 * consumption tables) when they change operational status and hence power
1853 * state. Examples of operational state changes that can affect power
1854 * consumption are :-
1855 *
1856 *    o Device is opened / closed.
1857 *    o Device I/O is about to begin or has just finished.
1858 *    o Device is idling in between work.
1859 *
1860 * This information is also exported via sysfs to userspace.
1861 *
1862 * DRMS will sum the total requested load on the regulator and change
1863 * to the most efficient operating mode if platform constraints allow.
1864 *
1865 * Returns the new regulator mode or error.
1866 */
1867int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1868{
1869	struct regulator_dev *rdev = regulator->rdev;
1870	struct regulator *consumer;
1871	int ret, output_uV, input_uV, total_uA_load = 0;
1872	unsigned int mode;
1873
1874	mutex_lock(&rdev->mutex);
1875
1876	regulator->uA_load = uA_load;
1877	ret = regulator_check_drms(rdev);
1878	if (ret < 0)
1879		goto out;
1880	ret = -EINVAL;
1881
1882	/* sanity check */
1883	if (!rdev->desc->ops->get_optimum_mode)
1884		goto out;
1885
1886	/* get output voltage */
1887	output_uV = rdev->desc->ops->get_voltage(rdev);
1888	if (output_uV <= 0) {
1889		rdev_err(rdev, "invalid output voltage found\n");
1890		goto out;
1891	}
1892
1893	/* get input voltage */
1894	if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1895		input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1896	else
1897		input_uV = rdev->constraints->input_uV;
1898	if (input_uV <= 0) {
1899		rdev_err(rdev, "invalid input voltage found\n");
1900		goto out;
1901	}
1902
1903	/* calc total requested load for this regulator */
1904	list_for_each_entry(consumer, &rdev->consumer_list, list)
1905		total_uA_load += consumer->uA_load;
1906
1907	mode = rdev->desc->ops->get_optimum_mode(rdev,
1908						 input_uV, output_uV,
1909						 total_uA_load);
1910	ret = regulator_check_mode(rdev, mode);
1911	if (ret < 0) {
1912		rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
1913			 total_uA_load, input_uV, output_uV);
1914		goto out;
1915	}
1916
1917	ret = rdev->desc->ops->set_mode(rdev, mode);
1918	if (ret < 0) {
1919		rdev_err(rdev, "failed to set optimum mode %x\n", mode);
1920		goto out;
1921	}
1922	ret = mode;
1923out:
1924	mutex_unlock(&rdev->mutex);
1925	return ret;
1926}
1927EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1928
1929/**
1930 * regulator_register_notifier - register regulator event notifier
1931 * @regulator: regulator source
1932 * @nb: notifier block
1933 *
1934 * Register notifier block to receive regulator events.
1935 */
1936int regulator_register_notifier(struct regulator *regulator,
1937			      struct notifier_block *nb)
1938{
1939	return blocking_notifier_chain_register(&regulator->rdev->notifier,
1940						nb);
1941}
1942EXPORT_SYMBOL_GPL(regulator_register_notifier);
1943
1944/**
1945 * regulator_unregister_notifier - unregister regulator event notifier
1946 * @regulator: regulator source
1947 * @nb: notifier block
1948 *
1949 * Unregister regulator event notifier block.
1950 */
1951int regulator_unregister_notifier(struct regulator *regulator,
1952				struct notifier_block *nb)
1953{
1954	return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1955						  nb);
1956}
1957EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1958
1959/* notify regulator consumers and downstream regulator consumers.
1960 * Note mutex must be held by caller.
1961 */
1962static void _notifier_call_chain(struct regulator_dev *rdev,
1963				  unsigned long event, void *data)
1964{
1965	struct regulator_dev *_rdev;
1966
1967	/* call rdev chain first */
1968	blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1969
1970	/* now notify regulator we supply */
1971	list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1972		mutex_lock(&_rdev->mutex);
1973		_notifier_call_chain(_rdev, event, data);
1974		mutex_unlock(&_rdev->mutex);
1975	}
1976}
1977
1978/**
1979 * regulator_bulk_get - get multiple regulator consumers
1980 *
1981 * @dev:           Device to supply
1982 * @num_consumers: Number of consumers to register
1983 * @consumers:     Configuration of consumers; clients are stored here.
1984 *
1985 * @return 0 on success, an errno on failure.
1986 *
1987 * This helper function allows drivers to get several regulator
1988 * consumers in one operation.  If any of the regulators cannot be
1989 * acquired then any regulators that were allocated will be freed
1990 * before returning to the caller.
1991 */
1992int regulator_bulk_get(struct device *dev, int num_consumers,
1993		       struct regulator_bulk_data *consumers)
1994{
1995	int i;
1996	int ret;
1997
1998	for (i = 0; i < num_consumers; i++)
1999		consumers[i].consumer = NULL;
2000
2001	for (i = 0; i < num_consumers; i++) {
2002		consumers[i].consumer = regulator_get(dev,
2003						      consumers[i].supply);
2004		if (IS_ERR(consumers[i].consumer)) {
2005			ret = PTR_ERR(consumers[i].consumer);
2006			dev_err(dev, "Failed to get supply '%s': %d\n",
2007				consumers[i].supply, ret);
2008			consumers[i].consumer = NULL;
2009			goto err;
2010		}
2011	}
2012
2013	return 0;
2014
2015err:
2016	for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2017		regulator_put(consumers[i].consumer);
2018
2019	return ret;
2020}
2021EXPORT_SYMBOL_GPL(regulator_bulk_get);
2022
2023/**
2024 * regulator_bulk_enable - enable multiple regulator consumers
2025 *
2026 * @num_consumers: Number of consumers
2027 * @consumers:     Consumer data; clients are stored here.
2028 * @return         0 on success, an errno on failure
2029 *
2030 * This convenience API allows consumers to enable multiple regulator
2031 * clients in a single API call.  If any consumers cannot be enabled
2032 * then any others that were enabled will be disabled again prior to
2033 * return.
2034 */
2035int regulator_bulk_enable(int num_consumers,
2036			  struct regulator_bulk_data *consumers)
2037{
2038	int i;
2039	int ret;
2040
2041	for (i = 0; i < num_consumers; i++) {
2042		ret = regulator_enable(consumers[i].consumer);
2043		if (ret != 0)
2044			goto err;
2045	}
2046
2047	return 0;
2048
2049err:
2050	pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2051	for (--i; i >= 0; --i)
2052		regulator_disable(consumers[i].consumer);
2053
2054	return ret;
2055}
2056EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2057
2058/**
2059 * regulator_bulk_disable - disable multiple regulator consumers
2060 *
2061 * @num_consumers: Number of consumers
2062 * @consumers:     Consumer data; clients are stored here.
2063 * @return         0 on success, an errno on failure
2064 *
2065 * This convenience API allows consumers to disable multiple regulator
2066 * clients in a single API call.  If any consumers cannot be enabled
2067 * then any others that were disabled will be disabled again prior to
2068 * return.
2069 */
2070int regulator_bulk_disable(int num_consumers,
2071			   struct regulator_bulk_data *consumers)
2072{
2073	int i;
2074	int ret;
2075
2076	for (i = 0; i < num_consumers; i++) {
2077		ret = regulator_disable(consumers[i].consumer);
2078		if (ret != 0)
2079			goto err;
2080	}
2081
2082	return 0;
2083
2084err:
2085	pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2086	for (--i; i >= 0; --i)
2087		regulator_enable(consumers[i].consumer);
2088
2089	return ret;
2090}
2091EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2092
2093/**
2094 * regulator_bulk_free - free multiple regulator consumers
2095 *
2096 * @num_consumers: Number of consumers
2097 * @consumers:     Consumer data; clients are stored here.
2098 *
2099 * This convenience API allows consumers to free multiple regulator
2100 * clients in a single API call.
2101 */
2102void regulator_bulk_free(int num_consumers,
2103			 struct regulator_bulk_data *consumers)
2104{
2105	int i;
2106
2107	for (i = 0; i < num_consumers; i++) {
2108		regulator_put(consumers[i].consumer);
2109		consumers[i].consumer = NULL;
2110	}
2111}
2112EXPORT_SYMBOL_GPL(regulator_bulk_free);
2113
2114/**
2115 * regulator_notifier_call_chain - call regulator event notifier
2116 * @rdev: regulator source
2117 * @event: notifier block
2118 * @data: callback-specific data.
2119 *
2120 * Called by regulator drivers to notify clients a regulator event has
2121 * occurred. We also notify regulator clients downstream.
2122 * Note lock must be held by caller.
2123 */
2124int regulator_notifier_call_chain(struct regulator_dev *rdev,
2125				  unsigned long event, void *data)
2126{
2127	_notifier_call_chain(rdev, event, data);
2128	return NOTIFY_DONE;
2129
2130}
2131EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2132
2133/**
2134 * regulator_mode_to_status - convert a regulator mode into a status
2135 *
2136 * @mode: Mode to convert
2137 *
2138 * Convert a regulator mode into a status.
2139 */
2140int regulator_mode_to_status(unsigned int mode)
2141{
2142	switch (mode) {
2143	case REGULATOR_MODE_FAST:
2144		return REGULATOR_STATUS_FAST;
2145	case REGULATOR_MODE_NORMAL:
2146		return REGULATOR_STATUS_NORMAL;
2147	case REGULATOR_MODE_IDLE:
2148		return REGULATOR_STATUS_IDLE;
2149	case REGULATOR_STATUS_STANDBY:
2150		return REGULATOR_STATUS_STANDBY;
2151	default:
2152		return 0;
2153	}
2154}
2155EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2156
2157/*
2158 * To avoid cluttering sysfs (and memory) with useless state, only
2159 * create attributes that can be meaningfully displayed.
2160 */
2161static int add_regulator_attributes(struct regulator_dev *rdev)
2162{
2163	struct device		*dev = &rdev->dev;
2164	struct regulator_ops	*ops = rdev->desc->ops;
2165	int			status = 0;
2166
2167	/* some attributes need specific methods to be displayed */
2168	if (ops->get_voltage) {
2169		status = device_create_file(dev, &dev_attr_microvolts);
2170		if (status < 0)
2171			return status;
2172	}
2173	if (ops->get_current_limit) {
2174		status = device_create_file(dev, &dev_attr_microamps);
2175		if (status < 0)
2176			return status;
2177	}
2178	if (ops->get_mode) {
2179		status = device_create_file(dev, &dev_attr_opmode);
2180		if (status < 0)
2181			return status;
2182	}
2183	if (ops->is_enabled) {
2184		status = device_create_file(dev, &dev_attr_state);
2185		if (status < 0)
2186			return status;
2187	}
2188	if (ops->get_status) {
2189		status = device_create_file(dev, &dev_attr_status);
2190		if (status < 0)
2191			return status;
2192	}
2193
2194	/* some attributes are type-specific */
2195	if (rdev->desc->type == REGULATOR_CURRENT) {
2196		status = device_create_file(dev, &dev_attr_requested_microamps);
2197		if (status < 0)
2198			return status;
2199	}
2200
2201	/* all the other attributes exist to support constraints;
2202	 * don't show them if there are no constraints, or if the
2203	 * relevant supporting methods are missing.
2204	 */
2205	if (!rdev->constraints)
2206		return status;
2207
2208	/* constraints need specific supporting methods */
2209	if (ops->set_voltage) {
2210		status = device_create_file(dev, &dev_attr_min_microvolts);
2211		if (status < 0)
2212			return status;
2213		status = device_create_file(dev, &dev_attr_max_microvolts);
2214		if (status < 0)
2215			return status;
2216	}
2217	if (ops->set_current_limit) {
2218		status = device_create_file(dev, &dev_attr_min_microamps);
2219		if (status < 0)
2220			return status;
2221		status = device_create_file(dev, &dev_attr_max_microamps);
2222		if (status < 0)
2223			return status;
2224	}
2225
2226	/* suspend mode constraints need multiple supporting methods */
2227	if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2228		return status;
2229
2230	status = device_create_file(dev, &dev_attr_suspend_standby_state);
2231	if (status < 0)
2232		return status;
2233	status = device_create_file(dev, &dev_attr_suspend_mem_state);
2234	if (status < 0)
2235		return status;
2236	status = device_create_file(dev, &dev_attr_suspend_disk_state);
2237	if (status < 0)
2238		return status;
2239
2240	if (ops->set_suspend_voltage) {
2241		status = device_create_file(dev,
2242				&dev_attr_suspend_standby_microvolts);
2243		if (status < 0)
2244			return status;
2245		status = device_create_file(dev,
2246				&dev_attr_suspend_mem_microvolts);
2247		if (status < 0)
2248			return status;
2249		status = device_create_file(dev,
2250				&dev_attr_suspend_disk_microvolts);
2251		if (status < 0)
2252			return status;
2253	}
2254
2255	if (ops->set_suspend_mode) {
2256		status = device_create_file(dev,
2257				&dev_attr_suspend_standby_mode);
2258		if (status < 0)
2259			return status;
2260		status = device_create_file(dev,
2261				&dev_attr_suspend_mem_mode);
2262		if (status < 0)
2263			return status;
2264		status = device_create_file(dev,
2265				&dev_attr_suspend_disk_mode);
2266		if (status < 0)
2267			return status;
2268	}
2269
2270	return status;
2271}
2272
2273/**
2274 * regulator_register - register regulator
2275 * @regulator_desc: regulator to register
2276 * @dev: struct device for the regulator
2277 * @init_data: platform provided init data, passed through by driver
2278 * @driver_data: private regulator data
2279 *
2280 * Called by regulator drivers to register a regulator.
2281 * Returns 0 on success.
2282 */
2283struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2284	struct device *dev, const struct regulator_init_data *init_data,
2285	void *driver_data)
2286{
2287	static atomic_t regulator_no = ATOMIC_INIT(0);
2288	struct regulator_dev *rdev;
2289	int ret, i;
2290
2291	if (regulator_desc == NULL)
2292		return ERR_PTR(-EINVAL);
2293
2294	if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2295		return ERR_PTR(-EINVAL);
2296
2297	if (regulator_desc->type != REGULATOR_VOLTAGE &&
2298	    regulator_desc->type != REGULATOR_CURRENT)
2299		return ERR_PTR(-EINVAL);
2300
2301	if (!init_data)
2302		return ERR_PTR(-EINVAL);
2303
2304	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2305	if (rdev == NULL)
2306		return ERR_PTR(-ENOMEM);
2307
2308	mutex_lock(&regulator_list_mutex);
2309
2310	mutex_init(&rdev->mutex);
2311	rdev->reg_data = driver_data;
2312	rdev->owner = regulator_desc->owner;
2313	rdev->desc = regulator_desc;
2314	INIT_LIST_HEAD(&rdev->consumer_list);
2315	INIT_LIST_HEAD(&rdev->supply_list);
2316	INIT_LIST_HEAD(&rdev->list);
2317	INIT_LIST_HEAD(&rdev->slist);
2318	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2319
2320	/* preform any regulator specific init */
2321	if (init_data->regulator_init) {
2322		ret = init_data->regulator_init(rdev->reg_data);
2323		if (ret < 0)
2324			goto clean;
2325	}
2326
2327	/* register with sysfs */
2328	rdev->dev.class = &regulator_class;
2329	rdev->dev.parent = dev;
2330	dev_set_name(&rdev->dev, "regulator.%d",
2331		     atomic_inc_return(&regulator_no) - 1);
2332	ret = device_register(&rdev->dev);
2333	if (ret != 0) {
2334		put_device(&rdev->dev);
2335		goto clean;
2336	}
2337
2338	dev_set_drvdata(&rdev->dev, rdev);
2339
2340	/* set regulator constraints */
2341	ret = set_machine_constraints(rdev, &init_data->constraints);
2342	if (ret < 0)
2343		goto scrub;
2344
2345	/* add attributes supported by this regulator */
2346	ret = add_regulator_attributes(rdev);
2347	if (ret < 0)
2348		goto scrub;
2349
2350	/* set supply regulator if it exists */
2351	if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2352		dev_err(dev,
2353			"Supply regulator specified by both name and dev\n");
2354		ret = -EINVAL;
2355		goto scrub;
2356	}
2357
2358	if (init_data->supply_regulator) {
2359		struct regulator_dev *r;
2360		int found = 0;
2361
2362		list_for_each_entry(r, &regulator_list, list) {
2363			if (strcmp(rdev_get_name(r),
2364				   init_data->supply_regulator) == 0) {
2365				found = 1;
2366				break;
2367			}
2368		}
2369
2370		if (!found) {
2371			dev_err(dev, "Failed to find supply %s\n",
2372				init_data->supply_regulator);
2373			ret = -ENODEV;
2374			goto scrub;
2375		}
2376
2377		ret = set_supply(rdev, r);
2378		if (ret < 0)
2379			goto scrub;
2380	}
2381
2382	if (init_data->supply_regulator_dev) {
2383		dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
2384		ret = set_supply(rdev,
2385			dev_get_drvdata(init_data->supply_regulator_dev));
2386		if (ret < 0)
2387			goto scrub;
2388	}
2389
2390	/* add consumers devices */
2391	for (i = 0; i < init_data->num_consumer_supplies; i++) {
2392		ret = set_consumer_device_supply(rdev,
2393			init_data->consumer_supplies[i].dev,
2394			init_data->consumer_supplies[i].dev_name,
2395			init_data->consumer_supplies[i].supply);
2396		if (ret < 0)
2397			goto unset_supplies;
2398	}
2399
2400	list_add(&rdev->list, &regulator_list);
2401out:
2402	mutex_unlock(&regulator_list_mutex);
2403	return rdev;
2404
2405unset_supplies:
2406	unset_regulator_supplies(rdev);
2407
2408scrub:
2409	device_unregister(&rdev->dev);
2410	/* device core frees rdev */
2411	rdev = ERR_PTR(ret);
2412	goto out;
2413
2414clean:
2415	kfree(rdev);
2416	rdev = ERR_PTR(ret);
2417	goto out;
2418}
2419EXPORT_SYMBOL_GPL(regulator_register);
2420
2421/**
2422 * regulator_unregister - unregister regulator
2423 * @rdev: regulator to unregister
2424 *
2425 * Called by regulator drivers to unregister a regulator.
2426 */
2427void regulator_unregister(struct regulator_dev *rdev)
2428{
2429	if (rdev == NULL)
2430		return;
2431
2432	mutex_lock(&regulator_list_mutex);
2433	WARN_ON(rdev->open_count);
2434	unset_regulator_supplies(rdev);
2435	list_del(&rdev->list);
2436	if (rdev->supply)
2437		sysfs_remove_link(&rdev->dev.kobj, "supply");
2438	device_unregister(&rdev->dev);
2439	kfree(rdev->constraints);
2440	mutex_unlock(&regulator_list_mutex);
2441}
2442EXPORT_SYMBOL_GPL(regulator_unregister);
2443
2444/**
2445 * regulator_suspend_prepare - prepare regulators for system wide suspend
2446 * @state: system suspend state
2447 *
2448 * Configure each regulator with it's suspend operating parameters for state.
2449 * This will usually be called by machine suspend code prior to supending.
2450 */
2451int regulator_suspend_prepare(suspend_state_t state)
2452{
2453	struct regulator_dev *rdev;
2454	int ret = 0;
2455
2456	/* ON is handled by regulator active state */
2457	if (state == PM_SUSPEND_ON)
2458		return -EINVAL;
2459
2460	mutex_lock(&regulator_list_mutex);
2461	list_for_each_entry(rdev, &regulator_list, list) {
2462
2463		mutex_lock(&rdev->mutex);
2464		ret = suspend_prepare(rdev, state);
2465		mutex_unlock(&rdev->mutex);
2466
2467		if (ret < 0) {
2468			rdev_err(rdev, "failed to prepare\n");
2469			goto out;
2470		}
2471	}
2472out:
2473	mutex_unlock(&regulator_list_mutex);
2474	return ret;
2475}
2476EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2477
2478/**
2479 * regulator_has_full_constraints - the system has fully specified constraints
2480 *
2481 * Calling this function will cause the regulator API to disable all
2482 * regulators which have a zero use count and don't have an always_on
2483 * constraint in a late_initcall.
2484 *
2485 * The intention is that this will become the default behaviour in a
2486 * future kernel release so users are encouraged to use this facility
2487 * now.
2488 */
2489void regulator_has_full_constraints(void)
2490{
2491	has_full_constraints = 1;
2492}
2493EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2494
2495/**
2496 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2497 *
2498 * Calling this function will cause the regulator API to provide a
2499 * dummy regulator to consumers if no physical regulator is found,
2500 * allowing most consumers to proceed as though a regulator were
2501 * configured.  This allows systems such as those with software
2502 * controllable regulators for the CPU core only to be brought up more
2503 * readily.
2504 */
2505void regulator_use_dummy_regulator(void)
2506{
2507	board_wants_dummy_regulator = true;
2508}
2509EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2510
2511/**
2512 * rdev_get_drvdata - get rdev regulator driver data
2513 * @rdev: regulator
2514 *
2515 * Get rdev regulator driver private data. This call can be used in the
2516 * regulator driver context.
2517 */
2518void *rdev_get_drvdata(struct regulator_dev *rdev)
2519{
2520	return rdev->reg_data;
2521}
2522EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2523
2524/**
2525 * regulator_get_drvdata - get regulator driver data
2526 * @regulator: regulator
2527 *
2528 * Get regulator driver private data. This call can be used in the consumer
2529 * driver context when non API regulator specific functions need to be called.
2530 */
2531void *regulator_get_drvdata(struct regulator *regulator)
2532{
2533	return regulator->rdev->reg_data;
2534}
2535EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2536
2537/**
2538 * regulator_set_drvdata - set regulator driver data
2539 * @regulator: regulator
2540 * @data: data
2541 */
2542void regulator_set_drvdata(struct regulator *regulator, void *data)
2543{
2544	regulator->rdev->reg_data = data;
2545}
2546EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2547
2548/**
2549 * regulator_get_id - get regulator ID
2550 * @rdev: regulator
2551 */
2552int rdev_get_id(struct regulator_dev *rdev)
2553{
2554	return rdev->desc->id;
2555}
2556EXPORT_SYMBOL_GPL(rdev_get_id);
2557
2558struct device *rdev_get_dev(struct regulator_dev *rdev)
2559{
2560	return &rdev->dev;
2561}
2562EXPORT_SYMBOL_GPL(rdev_get_dev);
2563
2564void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2565{
2566	return reg_init_data->driver_data;
2567}
2568EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2569
2570static int __init regulator_init(void)
2571{
2572	int ret;
2573
2574	ret = class_register(&regulator_class);
2575
2576	regulator_dummy_init();
2577
2578	return ret;
2579}
2580
2581/* init early to allow our consumers to complete system booting */
2582core_initcall(regulator_init);
2583
2584static int __init regulator_init_complete(void)
2585{
2586	struct regulator_dev *rdev;
2587	struct regulator_ops *ops;
2588	struct regulation_constraints *c;
2589	int enabled, ret;
2590
2591	mutex_lock(&regulator_list_mutex);
2592
2593	/* If we have a full configuration then disable any regulators
2594	 * which are not in use or always_on.  This will become the
2595	 * default behaviour in the future.
2596	 */
2597	list_for_each_entry(rdev, &regulator_list, list) {
2598		ops = rdev->desc->ops;
2599		c = rdev->constraints;
2600
2601		if (!ops->disable || (c && c->always_on))
2602			continue;
2603
2604		mutex_lock(&rdev->mutex);
2605
2606		if (rdev->use_count)
2607			goto unlock;
2608
2609		/* If we can't read the status assume it's on. */
2610		if (ops->is_enabled)
2611			enabled = ops->is_enabled(rdev);
2612		else
2613			enabled = 1;
2614
2615		if (!enabled)
2616			goto unlock;
2617
2618		if (has_full_constraints) {
2619			/* We log since this may kill the system if it
2620			 * goes wrong. */
2621			rdev_info(rdev, "disabling\n");
2622			ret = ops->disable(rdev);
2623			if (ret != 0) {
2624				rdev_err(rdev, "couldn't disable: %d\n", ret);
2625			}
2626		} else {
2627			/* The intention is that in future we will
2628			 * assume that full constraints are provided
2629			 * so warn even if we aren't going to do
2630			 * anything here.
2631			 */
2632			rdev_warn(rdev, "incomplete constraints, leaving on\n");
2633		}
2634
2635unlock:
2636		mutex_unlock(&rdev->mutex);
2637	}
2638
2639	mutex_unlock(&regulator_list_mutex);
2640
2641	return 0;
2642}
2643late_initcall(regulator_init_complete);
2644