core.c revision 412aec610559bdb602a0a21ce149ba8ffbb6f983
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/device.h>
19#include <linux/err.h>
20#include <linux/mutex.h>
21#include <linux/suspend.h>
22#include <linux/regulator/consumer.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/machine.h>
25
26#define REGULATOR_VERSION "0.5"
27
28static DEFINE_MUTEX(regulator_list_mutex);
29static LIST_HEAD(regulator_list);
30static LIST_HEAD(regulator_map_list);
31
32/**
33 * struct regulator_dev
34 *
35 * Voltage / Current regulator class device. One for each regulator.
36 */
37struct regulator_dev {
38	struct regulator_desc *desc;
39	int use_count;
40
41	/* lists we belong to */
42	struct list_head list; /* list of all regulators */
43	struct list_head slist; /* list of supplied regulators */
44
45	/* lists we own */
46	struct list_head consumer_list; /* consumers we supply */
47	struct list_head supply_list; /* regulators we supply */
48
49	struct blocking_notifier_head notifier;
50	struct mutex mutex; /* consumer lock */
51	struct module *owner;
52	struct device dev;
53	struct regulation_constraints *constraints;
54	struct regulator_dev *supply;	/* for tree */
55
56	void *reg_data;		/* regulator_dev data */
57};
58
59/**
60 * struct regulator_map
61 *
62 * Used to provide symbolic supply names to devices.
63 */
64struct regulator_map {
65	struct list_head list;
66	struct device *dev;
67	const char *supply;
68	struct regulator_dev *regulator;
69};
70
71/*
72 * struct regulator
73 *
74 * One for each consumer device.
75 */
76struct regulator {
77	struct device *dev;
78	struct list_head list;
79	int uA_load;
80	int min_uV;
81	int max_uV;
82	int enabled; /* count of client enables */
83	char *supply_name;
84	struct device_attribute dev_attr;
85	struct regulator_dev *rdev;
86};
87
88static int _regulator_is_enabled(struct regulator_dev *rdev);
89static int _regulator_disable(struct regulator_dev *rdev);
90static int _regulator_get_voltage(struct regulator_dev *rdev);
91static int _regulator_get_current_limit(struct regulator_dev *rdev);
92static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
93static void _notifier_call_chain(struct regulator_dev *rdev,
94				  unsigned long event, void *data);
95
96/* gets the regulator for a given consumer device */
97static struct regulator *get_device_regulator(struct device *dev)
98{
99	struct regulator *regulator = NULL;
100	struct regulator_dev *rdev;
101
102	mutex_lock(&regulator_list_mutex);
103	list_for_each_entry(rdev, &regulator_list, list) {
104		mutex_lock(&rdev->mutex);
105		list_for_each_entry(regulator, &rdev->consumer_list, list) {
106			if (regulator->dev == dev) {
107				mutex_unlock(&rdev->mutex);
108				mutex_unlock(&regulator_list_mutex);
109				return regulator;
110			}
111		}
112		mutex_unlock(&rdev->mutex);
113	}
114	mutex_unlock(&regulator_list_mutex);
115	return NULL;
116}
117
118/* Platform voltage constraint check */
119static int regulator_check_voltage(struct regulator_dev *rdev,
120				   int *min_uV, int *max_uV)
121{
122	BUG_ON(*min_uV > *max_uV);
123
124	if (!rdev->constraints) {
125		printk(KERN_ERR "%s: no constraints for %s\n", __func__,
126		       rdev->desc->name);
127		return -ENODEV;
128	}
129	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
130		printk(KERN_ERR "%s: operation not allowed for %s\n",
131		       __func__, rdev->desc->name);
132		return -EPERM;
133	}
134
135	if (*max_uV > rdev->constraints->max_uV)
136		*max_uV = rdev->constraints->max_uV;
137	if (*min_uV < rdev->constraints->min_uV)
138		*min_uV = rdev->constraints->min_uV;
139
140	if (*min_uV > *max_uV)
141		return -EINVAL;
142
143	return 0;
144}
145
146/* current constraint check */
147static int regulator_check_current_limit(struct regulator_dev *rdev,
148					int *min_uA, int *max_uA)
149{
150	BUG_ON(*min_uA > *max_uA);
151
152	if (!rdev->constraints) {
153		printk(KERN_ERR "%s: no constraints for %s\n", __func__,
154		       rdev->desc->name);
155		return -ENODEV;
156	}
157	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
158		printk(KERN_ERR "%s: operation not allowed for %s\n",
159		       __func__, rdev->desc->name);
160		return -EPERM;
161	}
162
163	if (*max_uA > rdev->constraints->max_uA)
164		*max_uA = rdev->constraints->max_uA;
165	if (*min_uA < rdev->constraints->min_uA)
166		*min_uA = rdev->constraints->min_uA;
167
168	if (*min_uA > *max_uA)
169		return -EINVAL;
170
171	return 0;
172}
173
174/* operating mode constraint check */
175static int regulator_check_mode(struct regulator_dev *rdev, int mode)
176{
177	if (!rdev->constraints) {
178		printk(KERN_ERR "%s: no constraints for %s\n", __func__,
179		       rdev->desc->name);
180		return -ENODEV;
181	}
182	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
183		printk(KERN_ERR "%s: operation not allowed for %s\n",
184		       __func__, rdev->desc->name);
185		return -EPERM;
186	}
187	if (!(rdev->constraints->valid_modes_mask & mode)) {
188		printk(KERN_ERR "%s: invalid mode %x for %s\n",
189		       __func__, mode, rdev->desc->name);
190		return -EINVAL;
191	}
192	return 0;
193}
194
195/* dynamic regulator mode switching constraint check */
196static int regulator_check_drms(struct regulator_dev *rdev)
197{
198	if (!rdev->constraints) {
199		printk(KERN_ERR "%s: no constraints for %s\n", __func__,
200		       rdev->desc->name);
201		return -ENODEV;
202	}
203	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
204		printk(KERN_ERR "%s: operation not allowed for %s\n",
205		       __func__, rdev->desc->name);
206		return -EPERM;
207	}
208	return 0;
209}
210
211static ssize_t device_requested_uA_show(struct device *dev,
212			     struct device_attribute *attr, char *buf)
213{
214	struct regulator *regulator;
215
216	regulator = get_device_regulator(dev);
217	if (regulator == NULL)
218		return 0;
219
220	return sprintf(buf, "%d\n", regulator->uA_load);
221}
222
223static ssize_t regulator_uV_show(struct device *dev,
224				struct device_attribute *attr, char *buf)
225{
226	struct regulator_dev *rdev = dev_get_drvdata(dev);
227	ssize_t ret;
228
229	mutex_lock(&rdev->mutex);
230	ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
231	mutex_unlock(&rdev->mutex);
232
233	return ret;
234}
235
236static ssize_t regulator_uA_show(struct device *dev,
237				struct device_attribute *attr, char *buf)
238{
239	struct regulator_dev *rdev = dev_get_drvdata(dev);
240
241	return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
242}
243
244static ssize_t regulator_name_show(struct device *dev,
245			     struct device_attribute *attr, char *buf)
246{
247	struct regulator_dev *rdev = dev_get_drvdata(dev);
248	const char *name;
249
250	if (rdev->constraints->name)
251		name = rdev->constraints->name;
252	else if (rdev->desc->name)
253		name = rdev->desc->name;
254	else
255		name = "";
256
257	return sprintf(buf, "%s\n", name);
258}
259
260static ssize_t regulator_opmode_show(struct device *dev,
261				    struct device_attribute *attr, char *buf)
262{
263	struct regulator_dev *rdev = dev_get_drvdata(dev);
264	int mode = _regulator_get_mode(rdev);
265
266	switch (mode) {
267	case REGULATOR_MODE_FAST:
268		return sprintf(buf, "fast\n");
269	case REGULATOR_MODE_NORMAL:
270		return sprintf(buf, "normal\n");
271	case REGULATOR_MODE_IDLE:
272		return sprintf(buf, "idle\n");
273	case REGULATOR_MODE_STANDBY:
274		return sprintf(buf, "standby\n");
275	}
276	return sprintf(buf, "unknown\n");
277}
278
279static ssize_t regulator_state_show(struct device *dev,
280				   struct device_attribute *attr, char *buf)
281{
282	struct regulator_dev *rdev = dev_get_drvdata(dev);
283	int state = _regulator_is_enabled(rdev);
284
285	if (state > 0)
286		return sprintf(buf, "enabled\n");
287	else if (state == 0)
288		return sprintf(buf, "disabled\n");
289	else
290		return sprintf(buf, "unknown\n");
291}
292
293static ssize_t regulator_min_uA_show(struct device *dev,
294				    struct device_attribute *attr, char *buf)
295{
296	struct regulator_dev *rdev = dev_get_drvdata(dev);
297
298	if (!rdev->constraints)
299		return sprintf(buf, "constraint not defined\n");
300
301	return sprintf(buf, "%d\n", rdev->constraints->min_uA);
302}
303
304static ssize_t regulator_max_uA_show(struct device *dev,
305				    struct device_attribute *attr, char *buf)
306{
307	struct regulator_dev *rdev = dev_get_drvdata(dev);
308
309	if (!rdev->constraints)
310		return sprintf(buf, "constraint not defined\n");
311
312	return sprintf(buf, "%d\n", rdev->constraints->max_uA);
313}
314
315static ssize_t regulator_min_uV_show(struct device *dev,
316				    struct device_attribute *attr, char *buf)
317{
318	struct regulator_dev *rdev = dev_get_drvdata(dev);
319
320	if (!rdev->constraints)
321		return sprintf(buf, "constraint not defined\n");
322
323	return sprintf(buf, "%d\n", rdev->constraints->min_uV);
324}
325
326static ssize_t regulator_max_uV_show(struct device *dev,
327				    struct device_attribute *attr, char *buf)
328{
329	struct regulator_dev *rdev = dev_get_drvdata(dev);
330
331	if (!rdev->constraints)
332		return sprintf(buf, "constraint not defined\n");
333
334	return sprintf(buf, "%d\n", rdev->constraints->max_uV);
335}
336
337static ssize_t regulator_total_uA_show(struct device *dev,
338				      struct device_attribute *attr, char *buf)
339{
340	struct regulator_dev *rdev = dev_get_drvdata(dev);
341	struct regulator *regulator;
342	int uA = 0;
343
344	mutex_lock(&rdev->mutex);
345	list_for_each_entry(regulator, &rdev->consumer_list, list)
346	    uA += regulator->uA_load;
347	mutex_unlock(&rdev->mutex);
348	return sprintf(buf, "%d\n", uA);
349}
350
351static ssize_t regulator_num_users_show(struct device *dev,
352				      struct device_attribute *attr, char *buf)
353{
354	struct regulator_dev *rdev = dev_get_drvdata(dev);
355	return sprintf(buf, "%d\n", rdev->use_count);
356}
357
358static ssize_t regulator_type_show(struct device *dev,
359				  struct device_attribute *attr, char *buf)
360{
361	struct regulator_dev *rdev = dev_get_drvdata(dev);
362
363	switch (rdev->desc->type) {
364	case REGULATOR_VOLTAGE:
365		return sprintf(buf, "voltage\n");
366	case REGULATOR_CURRENT:
367		return sprintf(buf, "current\n");
368	}
369	return sprintf(buf, "unknown\n");
370}
371
372static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
373				struct device_attribute *attr, char *buf)
374{
375	struct regulator_dev *rdev = dev_get_drvdata(dev);
376
377	if (!rdev->constraints)
378		return sprintf(buf, "not defined\n");
379	return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
380}
381
382static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
383				struct device_attribute *attr, char *buf)
384{
385	struct regulator_dev *rdev = dev_get_drvdata(dev);
386
387	if (!rdev->constraints)
388		return sprintf(buf, "not defined\n");
389	return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
390}
391
392static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
393				struct device_attribute *attr, char *buf)
394{
395	struct regulator_dev *rdev = dev_get_drvdata(dev);
396
397	if (!rdev->constraints)
398		return sprintf(buf, "not defined\n");
399	return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
400}
401
402static ssize_t suspend_opmode_show(struct regulator_dev *rdev,
403	unsigned int mode, char *buf)
404{
405	switch (mode) {
406	case REGULATOR_MODE_FAST:
407		return sprintf(buf, "fast\n");
408	case REGULATOR_MODE_NORMAL:
409		return sprintf(buf, "normal\n");
410	case REGULATOR_MODE_IDLE:
411		return sprintf(buf, "idle\n");
412	case REGULATOR_MODE_STANDBY:
413		return sprintf(buf, "standby\n");
414	}
415	return sprintf(buf, "unknown\n");
416}
417
418static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
419				struct device_attribute *attr, char *buf)
420{
421	struct regulator_dev *rdev = dev_get_drvdata(dev);
422
423	if (!rdev->constraints)
424		return sprintf(buf, "not defined\n");
425	return suspend_opmode_show(rdev,
426		rdev->constraints->state_mem.mode, buf);
427}
428
429static ssize_t regulator_suspend_disk_mode_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, "not defined\n");
436	return suspend_opmode_show(rdev,
437		rdev->constraints->state_disk.mode, buf);
438}
439
440static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
441				struct device_attribute *attr, char *buf)
442{
443	struct regulator_dev *rdev = dev_get_drvdata(dev);
444
445	if (!rdev->constraints)
446		return sprintf(buf, "not defined\n");
447	return suspend_opmode_show(rdev,
448		rdev->constraints->state_standby.mode, buf);
449}
450
451static ssize_t regulator_suspend_mem_state_show(struct device *dev,
452				   struct device_attribute *attr, char *buf)
453{
454	struct regulator_dev *rdev = dev_get_drvdata(dev);
455
456	if (!rdev->constraints)
457		return sprintf(buf, "not defined\n");
458
459	if (rdev->constraints->state_mem.enabled)
460		return sprintf(buf, "enabled\n");
461	else
462		return sprintf(buf, "disabled\n");
463}
464
465static ssize_t regulator_suspend_disk_state_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, "not defined\n");
472
473	if (rdev->constraints->state_disk.enabled)
474		return sprintf(buf, "enabled\n");
475	else
476		return sprintf(buf, "disabled\n");
477}
478
479static ssize_t regulator_suspend_standby_state_show(struct device *dev,
480				   struct device_attribute *attr, char *buf)
481{
482	struct regulator_dev *rdev = dev_get_drvdata(dev);
483
484	if (!rdev->constraints)
485		return sprintf(buf, "not defined\n");
486
487	if (rdev->constraints->state_standby.enabled)
488		return sprintf(buf, "enabled\n");
489	else
490		return sprintf(buf, "disabled\n");
491}
492
493static struct device_attribute regulator_dev_attrs[] = {
494	__ATTR(name, 0444, regulator_name_show, NULL),
495	__ATTR(microvolts, 0444, regulator_uV_show, NULL),
496	__ATTR(microamps, 0444, regulator_uA_show, NULL),
497	__ATTR(opmode, 0444, regulator_opmode_show, NULL),
498	__ATTR(state, 0444, regulator_state_show, NULL),
499	__ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL),
500	__ATTR(min_microamps, 0444, regulator_min_uA_show, NULL),
501	__ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL),
502	__ATTR(max_microamps, 0444, regulator_max_uA_show, NULL),
503	__ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL),
504	__ATTR(num_users, 0444, regulator_num_users_show, NULL),
505	__ATTR(type, 0444, regulator_type_show, NULL),
506	__ATTR(suspend_mem_microvolts, 0444,
507		regulator_suspend_mem_uV_show, NULL),
508	__ATTR(suspend_disk_microvolts, 0444,
509		regulator_suspend_disk_uV_show, NULL),
510	__ATTR(suspend_standby_microvolts, 0444,
511		regulator_suspend_standby_uV_show, NULL),
512	__ATTR(suspend_mem_mode, 0444,
513		regulator_suspend_mem_mode_show, NULL),
514	__ATTR(suspend_disk_mode, 0444,
515		regulator_suspend_disk_mode_show, NULL),
516	__ATTR(suspend_standby_mode, 0444,
517		regulator_suspend_standby_mode_show, NULL),
518	__ATTR(suspend_mem_state, 0444,
519		regulator_suspend_mem_state_show, NULL),
520	__ATTR(suspend_disk_state, 0444,
521		regulator_suspend_disk_state_show, NULL),
522	__ATTR(suspend_standby_state, 0444,
523		regulator_suspend_standby_state_show, NULL),
524	__ATTR_NULL,
525};
526
527static void regulator_dev_release(struct device *dev)
528{
529	struct regulator_dev *rdev = dev_get_drvdata(dev);
530	kfree(rdev);
531}
532
533static struct class regulator_class = {
534	.name = "regulator",
535	.dev_release = regulator_dev_release,
536	.dev_attrs = regulator_dev_attrs,
537};
538
539/* Calculate the new optimum regulator operating mode based on the new total
540 * consumer load. All locks held by caller */
541static void drms_uA_update(struct regulator_dev *rdev)
542{
543	struct regulator *sibling;
544	int current_uA = 0, output_uV, input_uV, err;
545	unsigned int mode;
546
547	err = regulator_check_drms(rdev);
548	if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
549	    !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
550	return;
551
552	/* get output voltage */
553	output_uV = rdev->desc->ops->get_voltage(rdev);
554	if (output_uV <= 0)
555		return;
556
557	/* get input voltage */
558	if (rdev->supply && rdev->supply->desc->ops->get_voltage)
559		input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
560	else
561		input_uV = rdev->constraints->input_uV;
562	if (input_uV <= 0)
563		return;
564
565	/* calc total requested load */
566	list_for_each_entry(sibling, &rdev->consumer_list, list)
567	    current_uA += sibling->uA_load;
568
569	/* now get the optimum mode for our new total regulator load */
570	mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
571						  output_uV, current_uA);
572
573	/* check the new mode is allowed */
574	err = regulator_check_mode(rdev, mode);
575	if (err == 0)
576		rdev->desc->ops->set_mode(rdev, mode);
577}
578
579static int suspend_set_state(struct regulator_dev *rdev,
580	struct regulator_state *rstate)
581{
582	int ret = 0;
583
584	/* enable & disable are mandatory for suspend control */
585	if (!rdev->desc->ops->set_suspend_enable ||
586		!rdev->desc->ops->set_suspend_disable) {
587		printk(KERN_ERR "%s: no way to set suspend state\n",
588			__func__);
589		return -EINVAL;
590	}
591
592	if (rstate->enabled)
593		ret = rdev->desc->ops->set_suspend_enable(rdev);
594	else
595		ret = rdev->desc->ops->set_suspend_disable(rdev);
596	if (ret < 0) {
597		printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
598		return ret;
599	}
600
601	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
602		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
603		if (ret < 0) {
604			printk(KERN_ERR "%s: failed to set voltage\n",
605				__func__);
606			return ret;
607		}
608	}
609
610	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
611		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
612		if (ret < 0) {
613			printk(KERN_ERR "%s: failed to set mode\n", __func__);
614			return ret;
615		}
616	}
617	return ret;
618}
619
620/* locks held by caller */
621static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
622{
623	if (!rdev->constraints)
624		return -EINVAL;
625
626	switch (state) {
627	case PM_SUSPEND_STANDBY:
628		return suspend_set_state(rdev,
629			&rdev->constraints->state_standby);
630	case PM_SUSPEND_MEM:
631		return suspend_set_state(rdev,
632			&rdev->constraints->state_mem);
633	case PM_SUSPEND_MAX:
634		return suspend_set_state(rdev,
635			&rdev->constraints->state_disk);
636	default:
637		return -EINVAL;
638	}
639}
640
641static void print_constraints(struct regulator_dev *rdev)
642{
643	struct regulation_constraints *constraints = rdev->constraints;
644	char buf[80];
645	int count;
646
647	if (rdev->desc->type == REGULATOR_VOLTAGE) {
648		if (constraints->min_uV == constraints->max_uV)
649			count = sprintf(buf, "%d mV ",
650					constraints->min_uV / 1000);
651		else
652			count = sprintf(buf, "%d <--> %d mV ",
653					constraints->min_uV / 1000,
654					constraints->max_uV / 1000);
655	} else {
656		if (constraints->min_uA == constraints->max_uA)
657			count = sprintf(buf, "%d mA ",
658					constraints->min_uA / 1000);
659		else
660			count = sprintf(buf, "%d <--> %d mA ",
661					constraints->min_uA / 1000,
662					constraints->max_uA / 1000);
663	}
664	if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
665		count += sprintf(buf + count, "fast ");
666	if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
667		count += sprintf(buf + count, "normal ");
668	if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
669		count += sprintf(buf + count, "idle ");
670	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
671		count += sprintf(buf + count, "standby");
672
673	printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
674}
675
676/**
677 * set_machine_constraints - sets regulator constraints
678 * @regulator: regulator source
679 *
680 * Allows platform initialisation code to define and constrain
681 * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
682 * Constraints *must* be set by platform code in order for some
683 * regulator operations to proceed i.e. set_voltage, set_current_limit,
684 * set_mode.
685 */
686static int set_machine_constraints(struct regulator_dev *rdev,
687	struct regulation_constraints *constraints)
688{
689	int ret = 0;
690	const char *name;
691	struct regulator_ops *ops = rdev->desc->ops;
692
693	if (constraints->name)
694		name = constraints->name;
695	else if (rdev->desc->name)
696		name = rdev->desc->name;
697	else
698		name = "regulator";
699
700	rdev->constraints = constraints;
701
702	/* do we need to apply the constraint voltage */
703	if (rdev->constraints->apply_uV &&
704		rdev->constraints->min_uV == rdev->constraints->max_uV &&
705		ops->set_voltage) {
706		ret = ops->set_voltage(rdev,
707			rdev->constraints->min_uV, rdev->constraints->max_uV);
708			if (ret < 0) {
709				printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
710				       __func__,
711				       rdev->constraints->min_uV, name);
712				rdev->constraints = NULL;
713				goto out;
714			}
715	}
716
717	/* are we enabled at boot time by firmware / bootloader */
718	if (rdev->constraints->boot_on)
719		rdev->use_count = 1;
720
721	/* do we need to setup our suspend state */
722	if (constraints->initial_state) {
723		ret = suspend_prepare(rdev, constraints->initial_state);
724		if (ret < 0) {
725			printk(KERN_ERR "%s: failed to set suspend state for %s\n",
726			       __func__, name);
727			rdev->constraints = NULL;
728			goto out;
729		}
730	}
731
732	/* if always_on is set then turn the regulator on if it's not
733	 * already on. */
734	if (constraints->always_on && ops->enable &&
735	    ((ops->is_enabled && !ops->is_enabled(rdev)) ||
736	     (!ops->is_enabled && !constraints->boot_on))) {
737		ret = ops->enable(rdev);
738		if (ret < 0) {
739			printk(KERN_ERR "%s: failed to enable %s\n",
740			       __func__, name);
741			rdev->constraints = NULL;
742			goto out;
743		}
744	}
745
746	print_constraints(rdev);
747out:
748	return ret;
749}
750
751/**
752 * set_supply - set regulator supply regulator
753 * @regulator: regulator name
754 * @supply: supply regulator name
755 *
756 * Called by platform initialisation code to set the supply regulator for this
757 * regulator. This ensures that a regulators supply will also be enabled by the
758 * core if it's child is enabled.
759 */
760static int set_supply(struct regulator_dev *rdev,
761	struct regulator_dev *supply_rdev)
762{
763	int err;
764
765	err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
766				"supply");
767	if (err) {
768		printk(KERN_ERR
769		       "%s: could not add device link %s err %d\n",
770		       __func__, supply_rdev->dev.kobj.name, err);
771		       goto out;
772	}
773	rdev->supply = supply_rdev;
774	list_add(&rdev->slist, &supply_rdev->supply_list);
775out:
776	return err;
777}
778
779/**
780 * set_consumer_device_supply: Bind a regulator to a symbolic supply
781 * @regulator: regulator source
782 * @dev:       device the supply applies to
783 * @supply:    symbolic name for supply
784 *
785 * Allows platform initialisation code to map physical regulator
786 * sources to symbolic names for supplies for use by devices.  Devices
787 * should use these symbolic names to request regulators, avoiding the
788 * need to provide board-specific regulator names as platform data.
789 */
790static int set_consumer_device_supply(struct regulator_dev *rdev,
791	struct device *consumer_dev, const char *supply)
792{
793	struct regulator_map *node;
794
795	if (supply == NULL)
796		return -EINVAL;
797
798	node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
799	if (node == NULL)
800		return -ENOMEM;
801
802	node->regulator = rdev;
803	node->dev = consumer_dev;
804	node->supply = supply;
805
806	list_add(&node->list, &regulator_map_list);
807	return 0;
808}
809
810static void unset_consumer_device_supply(struct regulator_dev *rdev,
811	struct device *consumer_dev)
812{
813	struct regulator_map *node, *n;
814
815	list_for_each_entry_safe(node, n, &regulator_map_list, list) {
816		if (rdev == node->regulator &&
817			consumer_dev == node->dev) {
818			list_del(&node->list);
819			kfree(node);
820			return;
821		}
822	}
823}
824
825#define REG_STR_SIZE	32
826
827static struct regulator *create_regulator(struct regulator_dev *rdev,
828					  struct device *dev,
829					  const char *supply_name)
830{
831	struct regulator *regulator;
832	char buf[REG_STR_SIZE];
833	int err, size;
834
835	regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
836	if (regulator == NULL)
837		return NULL;
838
839	mutex_lock(&rdev->mutex);
840	regulator->rdev = rdev;
841	list_add(&regulator->list, &rdev->consumer_list);
842
843	if (dev) {
844		/* create a 'requested_microamps_name' sysfs entry */
845		size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
846			supply_name);
847		if (size >= REG_STR_SIZE)
848			goto overflow_err;
849
850		regulator->dev = dev;
851		regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
852		if (regulator->dev_attr.attr.name == NULL)
853			goto attr_name_err;
854
855		regulator->dev_attr.attr.owner = THIS_MODULE;
856		regulator->dev_attr.attr.mode = 0444;
857		regulator->dev_attr.show = device_requested_uA_show;
858		err = device_create_file(dev, &regulator->dev_attr);
859		if (err < 0) {
860			printk(KERN_WARNING "%s: could not add regulator_dev"
861				" load sysfs\n", __func__);
862			goto attr_name_err;
863		}
864
865		/* also add a link to the device sysfs entry */
866		size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
867				 dev->kobj.name, supply_name);
868		if (size >= REG_STR_SIZE)
869			goto attr_err;
870
871		regulator->supply_name = kstrdup(buf, GFP_KERNEL);
872		if (regulator->supply_name == NULL)
873			goto attr_err;
874
875		err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
876					buf);
877		if (err) {
878			printk(KERN_WARNING
879			       "%s: could not add device link %s err %d\n",
880			       __func__, dev->kobj.name, err);
881			device_remove_file(dev, &regulator->dev_attr);
882			goto link_name_err;
883		}
884	}
885	mutex_unlock(&rdev->mutex);
886	return regulator;
887link_name_err:
888	kfree(regulator->supply_name);
889attr_err:
890	device_remove_file(regulator->dev, &regulator->dev_attr);
891attr_name_err:
892	kfree(regulator->dev_attr.attr.name);
893overflow_err:
894	list_del(&regulator->list);
895	kfree(regulator);
896	mutex_unlock(&rdev->mutex);
897	return NULL;
898}
899
900/**
901 * regulator_get - lookup and obtain a reference to a regulator.
902 * @dev: device for regulator "consumer"
903 * @id: Supply name or regulator ID.
904 *
905 * Returns a struct regulator corresponding to the regulator producer,
906 * or IS_ERR() condition containing errno.  Use of supply names
907 * configured via regulator_set_device_supply() is strongly
908 * encouraged.
909 */
910struct regulator *regulator_get(struct device *dev, const char *id)
911{
912	struct regulator_dev *rdev;
913	struct regulator_map *map;
914	struct regulator *regulator = ERR_PTR(-ENODEV);
915
916	if (id == NULL) {
917		printk(KERN_ERR "regulator: get() with no identifier\n");
918		return regulator;
919	}
920
921	mutex_lock(&regulator_list_mutex);
922
923	list_for_each_entry(map, &regulator_map_list, list) {
924		if (dev == map->dev &&
925		    strcmp(map->supply, id) == 0) {
926			rdev = map->regulator;
927			goto found;
928		}
929	}
930	printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
931	       id);
932	mutex_unlock(&regulator_list_mutex);
933	return regulator;
934
935found:
936	if (!try_module_get(rdev->owner))
937		goto out;
938
939	regulator = create_regulator(rdev, dev, id);
940	if (regulator == NULL) {
941		regulator = ERR_PTR(-ENOMEM);
942		module_put(rdev->owner);
943	}
944
945out:
946	mutex_unlock(&regulator_list_mutex);
947	return regulator;
948}
949EXPORT_SYMBOL_GPL(regulator_get);
950
951/**
952 * regulator_put - "free" the regulator source
953 * @regulator: regulator source
954 *
955 * Note: drivers must ensure that all regulator_enable calls made on this
956 * regulator source are balanced by regulator_disable calls prior to calling
957 * this function.
958 */
959void regulator_put(struct regulator *regulator)
960{
961	struct regulator_dev *rdev;
962
963	if (regulator == NULL || IS_ERR(regulator))
964		return;
965
966	mutex_lock(&regulator_list_mutex);
967	rdev = regulator->rdev;
968
969	if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
970			       regulator->supply_name))
971		_regulator_disable(rdev);
972
973	/* remove any sysfs entries */
974	if (regulator->dev) {
975		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
976		kfree(regulator->supply_name);
977		device_remove_file(regulator->dev, &regulator->dev_attr);
978		kfree(regulator->dev_attr.attr.name);
979	}
980	list_del(&regulator->list);
981	kfree(regulator);
982
983	module_put(rdev->owner);
984	mutex_unlock(&regulator_list_mutex);
985}
986EXPORT_SYMBOL_GPL(regulator_put);
987
988/* locks held by regulator_enable() */
989static int _regulator_enable(struct regulator_dev *rdev)
990{
991	int ret = -EINVAL;
992
993	if (!rdev->constraints) {
994		printk(KERN_ERR "%s: %s has no constraints\n",
995		       __func__, rdev->desc->name);
996		return ret;
997	}
998
999	/* do we need to enable the supply regulator first */
1000	if (rdev->supply) {
1001		ret = _regulator_enable(rdev->supply);
1002		if (ret < 0) {
1003			printk(KERN_ERR "%s: failed to enable %s: %d\n",
1004			       __func__, rdev->desc->name, ret);
1005			return ret;
1006		}
1007	}
1008
1009	/* check voltage and requested load before enabling */
1010	if (rdev->desc->ops->enable) {
1011
1012		if (rdev->constraints &&
1013			(rdev->constraints->valid_ops_mask &
1014			REGULATOR_CHANGE_DRMS))
1015			drms_uA_update(rdev);
1016
1017		ret = rdev->desc->ops->enable(rdev);
1018		if (ret < 0) {
1019			printk(KERN_ERR "%s: failed to enable %s: %d\n",
1020			       __func__, rdev->desc->name, ret);
1021			return ret;
1022		}
1023		rdev->use_count++;
1024		return ret;
1025	}
1026
1027	return ret;
1028}
1029
1030/**
1031 * regulator_enable - enable regulator output
1032 * @regulator: regulator source
1033 *
1034 * Enable the regulator output at the predefined voltage or current value.
1035 * NOTE: the output value can be set by other drivers, boot loader or may be
1036 * hardwired in the regulator.
1037 * NOTE: calls to regulator_enable() must be balanced with calls to
1038 * regulator_disable().
1039 */
1040int regulator_enable(struct regulator *regulator)
1041{
1042	struct regulator_dev *rdev = regulator->rdev;
1043	int ret = 0;
1044
1045	mutex_lock(&rdev->mutex);
1046	if (regulator->enabled == 0)
1047		ret = _regulator_enable(rdev);
1048	else if (regulator->enabled < 0)
1049		ret = -EIO;
1050	if (ret == 0)
1051		regulator->enabled++;
1052	mutex_unlock(&rdev->mutex);
1053	return ret;
1054}
1055EXPORT_SYMBOL_GPL(regulator_enable);
1056
1057/* locks held by regulator_disable() */
1058static int _regulator_disable(struct regulator_dev *rdev)
1059{
1060	int ret = 0;
1061
1062	/* are we the last user and permitted to disable ? */
1063	if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1064
1065		/* we are last user */
1066		if (rdev->desc->ops->disable) {
1067			ret = rdev->desc->ops->disable(rdev);
1068			if (ret < 0) {
1069				printk(KERN_ERR "%s: failed to disable %s\n",
1070				       __func__, rdev->desc->name);
1071				return ret;
1072			}
1073		}
1074
1075		/* decrease our supplies ref count and disable if required */
1076		if (rdev->supply)
1077			_regulator_disable(rdev->supply);
1078
1079		rdev->use_count = 0;
1080	} else if (rdev->use_count > 1) {
1081
1082		if (rdev->constraints &&
1083			(rdev->constraints->valid_ops_mask &
1084			REGULATOR_CHANGE_DRMS))
1085			drms_uA_update(rdev);
1086
1087		rdev->use_count--;
1088	}
1089	return ret;
1090}
1091
1092/**
1093 * regulator_disable - disable regulator output
1094 * @regulator: regulator source
1095 *
1096 * Disable the regulator output voltage or current.
1097 * NOTE: this will only disable the regulator output if no other consumer
1098 * devices have it enabled.
1099 * NOTE: calls to regulator_enable() must be balanced with calls to
1100 * regulator_disable().
1101 */
1102int regulator_disable(struct regulator *regulator)
1103{
1104	struct regulator_dev *rdev = regulator->rdev;
1105	int ret = 0;
1106
1107	mutex_lock(&rdev->mutex);
1108	if (regulator->enabled == 1) {
1109		ret = _regulator_disable(rdev);
1110		if (ret == 0)
1111			regulator->uA_load = 0;
1112	} else if (WARN(regulator->enabled <= 0,
1113			"unbalanced disables for supply %s\n",
1114			regulator->supply_name))
1115		ret = -EIO;
1116	if (ret == 0)
1117		regulator->enabled--;
1118	mutex_unlock(&rdev->mutex);
1119	return ret;
1120}
1121EXPORT_SYMBOL_GPL(regulator_disable);
1122
1123/* locks held by regulator_force_disable() */
1124static int _regulator_force_disable(struct regulator_dev *rdev)
1125{
1126	int ret = 0;
1127
1128	/* force disable */
1129	if (rdev->desc->ops->disable) {
1130		/* ah well, who wants to live forever... */
1131		ret = rdev->desc->ops->disable(rdev);
1132		if (ret < 0) {
1133			printk(KERN_ERR "%s: failed to force disable %s\n",
1134			       __func__, rdev->desc->name);
1135			return ret;
1136		}
1137		/* notify other consumers that power has been forced off */
1138		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1139			NULL);
1140	}
1141
1142	/* decrease our supplies ref count and disable if required */
1143	if (rdev->supply)
1144		_regulator_disable(rdev->supply);
1145
1146	rdev->use_count = 0;
1147	return ret;
1148}
1149
1150/**
1151 * regulator_force_disable - force disable regulator output
1152 * @regulator: regulator source
1153 *
1154 * Forcibly disable the regulator output voltage or current.
1155 * NOTE: this *will* disable the regulator output even if other consumer
1156 * devices have it enabled. This should be used for situations when device
1157 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1158 */
1159int regulator_force_disable(struct regulator *regulator)
1160{
1161	int ret;
1162
1163	mutex_lock(&regulator->rdev->mutex);
1164	regulator->enabled = 0;
1165	regulator->uA_load = 0;
1166	ret = _regulator_force_disable(regulator->rdev);
1167	mutex_unlock(&regulator->rdev->mutex);
1168	return ret;
1169}
1170EXPORT_SYMBOL_GPL(regulator_force_disable);
1171
1172static int _regulator_is_enabled(struct regulator_dev *rdev)
1173{
1174	int ret;
1175
1176	mutex_lock(&rdev->mutex);
1177
1178	/* sanity check */
1179	if (!rdev->desc->ops->is_enabled) {
1180		ret = -EINVAL;
1181		goto out;
1182	}
1183
1184	ret = rdev->desc->ops->is_enabled(rdev);
1185out:
1186	mutex_unlock(&rdev->mutex);
1187	return ret;
1188}
1189
1190/**
1191 * regulator_is_enabled - is the regulator output enabled
1192 * @regulator: regulator source
1193 *
1194 * Returns positive if the regulator driver backing the source/client
1195 * has requested that the device be enabled, zero if it hasn't, else a
1196 * negative errno code.
1197 *
1198 * Note that the device backing this regulator handle can have multiple
1199 * users, so it might be enabled even if regulator_enable() was never
1200 * called for this particular source.
1201 */
1202int regulator_is_enabled(struct regulator *regulator)
1203{
1204	return _regulator_is_enabled(regulator->rdev);
1205}
1206EXPORT_SYMBOL_GPL(regulator_is_enabled);
1207
1208/**
1209 * regulator_set_voltage - set regulator output voltage
1210 * @regulator: regulator source
1211 * @min_uV: Minimum required voltage in uV
1212 * @max_uV: Maximum acceptable voltage in uV
1213 *
1214 * Sets a voltage regulator to the desired output voltage. This can be set
1215 * during any regulator state. IOW, regulator can be disabled or enabled.
1216 *
1217 * If the regulator is enabled then the voltage will change to the new value
1218 * immediately otherwise if the regulator is disabled the regulator will
1219 * output at the new voltage when enabled.
1220 *
1221 * NOTE: If the regulator is shared between several devices then the lowest
1222 * request voltage that meets the system constraints will be used.
1223 * NOTE: Regulator system constraints must be set for this regulator before
1224 * calling this function otherwise this call will fail.
1225 */
1226int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1227{
1228	struct regulator_dev *rdev = regulator->rdev;
1229	int ret;
1230
1231	mutex_lock(&rdev->mutex);
1232
1233	/* sanity check */
1234	if (!rdev->desc->ops->set_voltage) {
1235		ret = -EINVAL;
1236		goto out;
1237	}
1238
1239	/* constraints check */
1240	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1241	if (ret < 0)
1242		goto out;
1243	regulator->min_uV = min_uV;
1244	regulator->max_uV = max_uV;
1245	ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1246
1247out:
1248	mutex_unlock(&rdev->mutex);
1249	return ret;
1250}
1251EXPORT_SYMBOL_GPL(regulator_set_voltage);
1252
1253static int _regulator_get_voltage(struct regulator_dev *rdev)
1254{
1255	/* sanity check */
1256	if (rdev->desc->ops->get_voltage)
1257		return rdev->desc->ops->get_voltage(rdev);
1258	else
1259		return -EINVAL;
1260}
1261
1262/**
1263 * regulator_get_voltage - get regulator output voltage
1264 * @regulator: regulator source
1265 *
1266 * This returns the current regulator voltage in uV.
1267 *
1268 * NOTE: If the regulator is disabled it will return the voltage value. This
1269 * function should not be used to determine regulator state.
1270 */
1271int regulator_get_voltage(struct regulator *regulator)
1272{
1273	int ret;
1274
1275	mutex_lock(&regulator->rdev->mutex);
1276
1277	ret = _regulator_get_voltage(regulator->rdev);
1278
1279	mutex_unlock(&regulator->rdev->mutex);
1280
1281	return ret;
1282}
1283EXPORT_SYMBOL_GPL(regulator_get_voltage);
1284
1285/**
1286 * regulator_set_current_limit - set regulator output current limit
1287 * @regulator: regulator source
1288 * @min_uA: Minimuum supported current in uA
1289 * @max_uA: Maximum supported current in uA
1290 *
1291 * Sets current sink to the desired output current. This can be set during
1292 * any regulator state. IOW, regulator can be disabled or enabled.
1293 *
1294 * If the regulator is enabled then the current will change to the new value
1295 * immediately otherwise if the regulator is disabled the regulator will
1296 * output at the new current when enabled.
1297 *
1298 * NOTE: Regulator system constraints must be set for this regulator before
1299 * calling this function otherwise this call will fail.
1300 */
1301int regulator_set_current_limit(struct regulator *regulator,
1302			       int min_uA, int max_uA)
1303{
1304	struct regulator_dev *rdev = regulator->rdev;
1305	int ret;
1306
1307	mutex_lock(&rdev->mutex);
1308
1309	/* sanity check */
1310	if (!rdev->desc->ops->set_current_limit) {
1311		ret = -EINVAL;
1312		goto out;
1313	}
1314
1315	/* constraints check */
1316	ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1317	if (ret < 0)
1318		goto out;
1319
1320	ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1321out:
1322	mutex_unlock(&rdev->mutex);
1323	return ret;
1324}
1325EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1326
1327static int _regulator_get_current_limit(struct regulator_dev *rdev)
1328{
1329	int ret;
1330
1331	mutex_lock(&rdev->mutex);
1332
1333	/* sanity check */
1334	if (!rdev->desc->ops->get_current_limit) {
1335		ret = -EINVAL;
1336		goto out;
1337	}
1338
1339	ret = rdev->desc->ops->get_current_limit(rdev);
1340out:
1341	mutex_unlock(&rdev->mutex);
1342	return ret;
1343}
1344
1345/**
1346 * regulator_get_current_limit - get regulator output current
1347 * @regulator: regulator source
1348 *
1349 * This returns the current supplied by the specified current sink in uA.
1350 *
1351 * NOTE: If the regulator is disabled it will return the current value. This
1352 * function should not be used to determine regulator state.
1353 */
1354int regulator_get_current_limit(struct regulator *regulator)
1355{
1356	return _regulator_get_current_limit(regulator->rdev);
1357}
1358EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1359
1360/**
1361 * regulator_set_mode - set regulator operating mode
1362 * @regulator: regulator source
1363 * @mode: operating mode - one of the REGULATOR_MODE constants
1364 *
1365 * Set regulator operating mode to increase regulator efficiency or improve
1366 * regulation performance.
1367 *
1368 * NOTE: Regulator system constraints must be set for this regulator before
1369 * calling this function otherwise this call will fail.
1370 */
1371int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1372{
1373	struct regulator_dev *rdev = regulator->rdev;
1374	int ret;
1375
1376	mutex_lock(&rdev->mutex);
1377
1378	/* sanity check */
1379	if (!rdev->desc->ops->set_mode) {
1380		ret = -EINVAL;
1381		goto out;
1382	}
1383
1384	/* constraints check */
1385	ret = regulator_check_mode(rdev, mode);
1386	if (ret < 0)
1387		goto out;
1388
1389	ret = rdev->desc->ops->set_mode(rdev, mode);
1390out:
1391	mutex_unlock(&rdev->mutex);
1392	return ret;
1393}
1394EXPORT_SYMBOL_GPL(regulator_set_mode);
1395
1396static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1397{
1398	int ret;
1399
1400	mutex_lock(&rdev->mutex);
1401
1402	/* sanity check */
1403	if (!rdev->desc->ops->get_mode) {
1404		ret = -EINVAL;
1405		goto out;
1406	}
1407
1408	ret = rdev->desc->ops->get_mode(rdev);
1409out:
1410	mutex_unlock(&rdev->mutex);
1411	return ret;
1412}
1413
1414/**
1415 * regulator_get_mode - get regulator operating mode
1416 * @regulator: regulator source
1417 *
1418 * Get the current regulator operating mode.
1419 */
1420unsigned int regulator_get_mode(struct regulator *regulator)
1421{
1422	return _regulator_get_mode(regulator->rdev);
1423}
1424EXPORT_SYMBOL_GPL(regulator_get_mode);
1425
1426/**
1427 * regulator_set_optimum_mode - set regulator optimum operating mode
1428 * @regulator: regulator source
1429 * @uA_load: load current
1430 *
1431 * Notifies the regulator core of a new device load. This is then used by
1432 * DRMS (if enabled by constraints) to set the most efficient regulator
1433 * operating mode for the new regulator loading.
1434 *
1435 * Consumer devices notify their supply regulator of the maximum power
1436 * they will require (can be taken from device datasheet in the power
1437 * consumption tables) when they change operational status and hence power
1438 * state. Examples of operational state changes that can affect power
1439 * consumption are :-
1440 *
1441 *    o Device is opened / closed.
1442 *    o Device I/O is about to begin or has just finished.
1443 *    o Device is idling in between work.
1444 *
1445 * This information is also exported via sysfs to userspace.
1446 *
1447 * DRMS will sum the total requested load on the regulator and change
1448 * to the most efficient operating mode if platform constraints allow.
1449 *
1450 * Returns the new regulator mode or error.
1451 */
1452int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1453{
1454	struct regulator_dev *rdev = regulator->rdev;
1455	struct regulator *consumer;
1456	int ret, output_uV, input_uV, total_uA_load = 0;
1457	unsigned int mode;
1458
1459	mutex_lock(&rdev->mutex);
1460
1461	regulator->uA_load = uA_load;
1462	ret = regulator_check_drms(rdev);
1463	if (ret < 0)
1464		goto out;
1465	ret = -EINVAL;
1466
1467	/* sanity check */
1468	if (!rdev->desc->ops->get_optimum_mode)
1469		goto out;
1470
1471	/* get output voltage */
1472	output_uV = rdev->desc->ops->get_voltage(rdev);
1473	if (output_uV <= 0) {
1474		printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1475			__func__, rdev->desc->name);
1476		goto out;
1477	}
1478
1479	/* get input voltage */
1480	if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1481		input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1482	else
1483		input_uV = rdev->constraints->input_uV;
1484	if (input_uV <= 0) {
1485		printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1486			__func__, rdev->desc->name);
1487		goto out;
1488	}
1489
1490	/* calc total requested load for this regulator */
1491	list_for_each_entry(consumer, &rdev->consumer_list, list)
1492	    total_uA_load += consumer->uA_load;
1493
1494	mode = rdev->desc->ops->get_optimum_mode(rdev,
1495						 input_uV, output_uV,
1496						 total_uA_load);
1497	if (ret <= 0) {
1498		printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1499			" %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1500			total_uA_load, input_uV, output_uV);
1501		goto out;
1502	}
1503
1504	ret = rdev->desc->ops->set_mode(rdev, mode);
1505	if (ret <= 0) {
1506		printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1507			__func__, mode, rdev->desc->name);
1508		goto out;
1509	}
1510	ret = mode;
1511out:
1512	mutex_unlock(&rdev->mutex);
1513	return ret;
1514}
1515EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1516
1517/**
1518 * regulator_register_notifier - register regulator event notifier
1519 * @regulator: regulator source
1520 * @notifier_block: notifier block
1521 *
1522 * Register notifier block to receive regulator events.
1523 */
1524int regulator_register_notifier(struct regulator *regulator,
1525			      struct notifier_block *nb)
1526{
1527	return blocking_notifier_chain_register(&regulator->rdev->notifier,
1528						nb);
1529}
1530EXPORT_SYMBOL_GPL(regulator_register_notifier);
1531
1532/**
1533 * regulator_unregister_notifier - unregister regulator event notifier
1534 * @regulator: regulator source
1535 * @notifier_block: notifier block
1536 *
1537 * Unregister regulator event notifier block.
1538 */
1539int regulator_unregister_notifier(struct regulator *regulator,
1540				struct notifier_block *nb)
1541{
1542	return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1543						  nb);
1544}
1545EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1546
1547/* notify regulator consumers and downstream regulator consumers */
1548static void _notifier_call_chain(struct regulator_dev *rdev,
1549				  unsigned long event, void *data)
1550{
1551	struct regulator_dev *_rdev;
1552
1553	/* call rdev chain first */
1554	mutex_lock(&rdev->mutex);
1555	blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1556	mutex_unlock(&rdev->mutex);
1557
1558	/* now notify regulator we supply */
1559	list_for_each_entry(_rdev, &rdev->supply_list, slist)
1560		_notifier_call_chain(_rdev, event, data);
1561}
1562
1563/**
1564 * regulator_bulk_get - get multiple regulator consumers
1565 *
1566 * @dev:           Device to supply
1567 * @num_consumers: Number of consumers to register
1568 * @consumers:     Configuration of consumers; clients are stored here.
1569 *
1570 * @return 0 on success, an errno on failure.
1571 *
1572 * This helper function allows drivers to get several regulator
1573 * consumers in one operation.  If any of the regulators cannot be
1574 * acquired then any regulators that were allocated will be freed
1575 * before returning to the caller.
1576 */
1577int regulator_bulk_get(struct device *dev, int num_consumers,
1578		       struct regulator_bulk_data *consumers)
1579{
1580	int i;
1581	int ret;
1582
1583	for (i = 0; i < num_consumers; i++)
1584		consumers[i].consumer = NULL;
1585
1586	for (i = 0; i < num_consumers; i++) {
1587		consumers[i].consumer = regulator_get(dev,
1588						      consumers[i].supply);
1589		if (IS_ERR(consumers[i].consumer)) {
1590			dev_err(dev, "Failed to get supply '%s'\n",
1591				consumers[i].supply);
1592			ret = PTR_ERR(consumers[i].consumer);
1593			consumers[i].consumer = NULL;
1594			goto err;
1595		}
1596	}
1597
1598	return 0;
1599
1600err:
1601	for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1602		regulator_put(consumers[i].consumer);
1603
1604	return ret;
1605}
1606EXPORT_SYMBOL_GPL(regulator_bulk_get);
1607
1608/**
1609 * regulator_bulk_enable - enable multiple regulator consumers
1610 *
1611 * @num_consumers: Number of consumers
1612 * @consumers:     Consumer data; clients are stored here.
1613 * @return         0 on success, an errno on failure
1614 *
1615 * This convenience API allows consumers to enable multiple regulator
1616 * clients in a single API call.  If any consumers cannot be enabled
1617 * then any others that were enabled will be disabled again prior to
1618 * return.
1619 */
1620int regulator_bulk_enable(int num_consumers,
1621			  struct regulator_bulk_data *consumers)
1622{
1623	int i;
1624	int ret;
1625
1626	for (i = 0; i < num_consumers; i++) {
1627		ret = regulator_enable(consumers[i].consumer);
1628		if (ret != 0)
1629			goto err;
1630	}
1631
1632	return 0;
1633
1634err:
1635	printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1636	for (i = 0; i < num_consumers; i++)
1637		regulator_disable(consumers[i].consumer);
1638
1639	return ret;
1640}
1641EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1642
1643/**
1644 * regulator_bulk_disable - disable multiple regulator consumers
1645 *
1646 * @num_consumers: Number of consumers
1647 * @consumers:     Consumer data; clients are stored here.
1648 * @return         0 on success, an errno on failure
1649 *
1650 * This convenience API allows consumers to disable multiple regulator
1651 * clients in a single API call.  If any consumers cannot be enabled
1652 * then any others that were disabled will be disabled again prior to
1653 * return.
1654 */
1655int regulator_bulk_disable(int num_consumers,
1656			   struct regulator_bulk_data *consumers)
1657{
1658	int i;
1659	int ret;
1660
1661	for (i = 0; i < num_consumers; i++) {
1662		ret = regulator_disable(consumers[i].consumer);
1663		if (ret != 0)
1664			goto err;
1665	}
1666
1667	return 0;
1668
1669err:
1670	printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1671	for (i = 0; i < num_consumers; i++)
1672		regulator_enable(consumers[i].consumer);
1673
1674	return ret;
1675}
1676EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1677
1678/**
1679 * regulator_bulk_free - free multiple regulator consumers
1680 *
1681 * @num_consumers: Number of consumers
1682 * @consumers:     Consumer data; clients are stored here.
1683 *
1684 * This convenience API allows consumers to free multiple regulator
1685 * clients in a single API call.
1686 */
1687void regulator_bulk_free(int num_consumers,
1688			 struct regulator_bulk_data *consumers)
1689{
1690	int i;
1691
1692	for (i = 0; i < num_consumers; i++) {
1693		regulator_put(consumers[i].consumer);
1694		consumers[i].consumer = NULL;
1695	}
1696}
1697EXPORT_SYMBOL_GPL(regulator_bulk_free);
1698
1699/**
1700 * regulator_notifier_call_chain - call regulator event notifier
1701 * @regulator: regulator source
1702 * @event: notifier block
1703 * @data:
1704 *
1705 * Called by regulator drivers to notify clients a regulator event has
1706 * occurred. We also notify regulator clients downstream.
1707 */
1708int regulator_notifier_call_chain(struct regulator_dev *rdev,
1709				  unsigned long event, void *data)
1710{
1711	_notifier_call_chain(rdev, event, data);
1712	return NOTIFY_DONE;
1713
1714}
1715EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1716
1717/**
1718 * regulator_register - register regulator
1719 * @regulator: regulator source
1720 * @reg_data: private regulator data
1721 *
1722 * Called by regulator drivers to register a regulator.
1723 * Returns 0 on success.
1724 */
1725struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1726	struct device *dev, void *driver_data)
1727{
1728	static atomic_t regulator_no = ATOMIC_INIT(0);
1729	struct regulator_dev *rdev;
1730	struct regulator_init_data *init_data = dev->platform_data;
1731	int ret, i;
1732
1733	if (regulator_desc == NULL)
1734		return ERR_PTR(-EINVAL);
1735
1736	if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1737		return ERR_PTR(-EINVAL);
1738
1739	if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1740	    !regulator_desc->type == REGULATOR_CURRENT)
1741		return ERR_PTR(-EINVAL);
1742
1743	if (!init_data)
1744		return ERR_PTR(-EINVAL);
1745
1746	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1747	if (rdev == NULL)
1748		return ERR_PTR(-ENOMEM);
1749
1750	mutex_lock(&regulator_list_mutex);
1751
1752	mutex_init(&rdev->mutex);
1753	rdev->reg_data = driver_data;
1754	rdev->owner = regulator_desc->owner;
1755	rdev->desc = regulator_desc;
1756	INIT_LIST_HEAD(&rdev->consumer_list);
1757	INIT_LIST_HEAD(&rdev->supply_list);
1758	INIT_LIST_HEAD(&rdev->list);
1759	INIT_LIST_HEAD(&rdev->slist);
1760	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1761
1762	/* preform any regulator specific init */
1763	if (init_data->regulator_init) {
1764		ret = init_data->regulator_init(rdev->reg_data);
1765		if (ret < 0) {
1766			kfree(rdev);
1767			rdev = ERR_PTR(ret);
1768			goto out;
1769		}
1770	}
1771
1772	/* set regulator constraints */
1773	ret = set_machine_constraints(rdev, &init_data->constraints);
1774	if (ret < 0) {
1775		kfree(rdev);
1776		rdev = ERR_PTR(ret);
1777		goto out;
1778	}
1779
1780	/* register with sysfs */
1781	rdev->dev.class = &regulator_class;
1782	rdev->dev.parent = dev;
1783	dev_set_name(&rdev->dev, "regulator.%d",
1784		     atomic_inc_return(&regulator_no) - 1);
1785	ret = device_register(&rdev->dev);
1786	if (ret != 0) {
1787		kfree(rdev);
1788		rdev = ERR_PTR(ret);
1789		goto out;
1790	}
1791
1792	dev_set_drvdata(&rdev->dev, rdev);
1793
1794	/* set supply regulator if it exists */
1795	if (init_data->supply_regulator_dev) {
1796		ret = set_supply(rdev,
1797			dev_get_drvdata(init_data->supply_regulator_dev));
1798		if (ret < 0) {
1799			device_unregister(&rdev->dev);
1800			kfree(rdev);
1801			rdev = ERR_PTR(ret);
1802			goto out;
1803		}
1804	}
1805
1806	/* add consumers devices */
1807	for (i = 0; i < init_data->num_consumer_supplies; i++) {
1808		ret = set_consumer_device_supply(rdev,
1809			init_data->consumer_supplies[i].dev,
1810			init_data->consumer_supplies[i].supply);
1811		if (ret < 0) {
1812			for (--i; i >= 0; i--)
1813				unset_consumer_device_supply(rdev,
1814					init_data->consumer_supplies[i].dev);
1815			device_unregister(&rdev->dev);
1816			kfree(rdev);
1817			rdev = ERR_PTR(ret);
1818			goto out;
1819		}
1820	}
1821
1822	list_add(&rdev->list, &regulator_list);
1823out:
1824	mutex_unlock(&regulator_list_mutex);
1825	return rdev;
1826}
1827EXPORT_SYMBOL_GPL(regulator_register);
1828
1829/**
1830 * regulator_unregister - unregister regulator
1831 * @regulator: regulator source
1832 *
1833 * Called by regulator drivers to unregister a regulator.
1834 */
1835void regulator_unregister(struct regulator_dev *rdev)
1836{
1837	if (rdev == NULL)
1838		return;
1839
1840	mutex_lock(&regulator_list_mutex);
1841	list_del(&rdev->list);
1842	if (rdev->supply)
1843		sysfs_remove_link(&rdev->dev.kobj, "supply");
1844	device_unregister(&rdev->dev);
1845	mutex_unlock(&regulator_list_mutex);
1846}
1847EXPORT_SYMBOL_GPL(regulator_unregister);
1848
1849/**
1850 * regulator_suspend_prepare: prepare regulators for system wide suspend
1851 * @state: system suspend state
1852 *
1853 * Configure each regulator with it's suspend operating parameters for state.
1854 * This will usually be called by machine suspend code prior to supending.
1855 */
1856int regulator_suspend_prepare(suspend_state_t state)
1857{
1858	struct regulator_dev *rdev;
1859	int ret = 0;
1860
1861	/* ON is handled by regulator active state */
1862	if (state == PM_SUSPEND_ON)
1863		return -EINVAL;
1864
1865	mutex_lock(&regulator_list_mutex);
1866	list_for_each_entry(rdev, &regulator_list, list) {
1867
1868		mutex_lock(&rdev->mutex);
1869		ret = suspend_prepare(rdev, state);
1870		mutex_unlock(&rdev->mutex);
1871
1872		if (ret < 0) {
1873			printk(KERN_ERR "%s: failed to prepare %s\n",
1874				__func__, rdev->desc->name);
1875			goto out;
1876		}
1877	}
1878out:
1879	mutex_unlock(&regulator_list_mutex);
1880	return ret;
1881}
1882EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1883
1884/**
1885 * rdev_get_drvdata - get rdev regulator driver data
1886 * @regulator: regulator
1887 *
1888 * Get rdev regulator driver private data. This call can be used in the
1889 * regulator driver context.
1890 */
1891void *rdev_get_drvdata(struct regulator_dev *rdev)
1892{
1893	return rdev->reg_data;
1894}
1895EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1896
1897/**
1898 * regulator_get_drvdata - get regulator driver data
1899 * @regulator: regulator
1900 *
1901 * Get regulator driver private data. This call can be used in the consumer
1902 * driver context when non API regulator specific functions need to be called.
1903 */
1904void *regulator_get_drvdata(struct regulator *regulator)
1905{
1906	return regulator->rdev->reg_data;
1907}
1908EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1909
1910/**
1911 * regulator_set_drvdata - set regulator driver data
1912 * @regulator: regulator
1913 * @data: data
1914 */
1915void regulator_set_drvdata(struct regulator *regulator, void *data)
1916{
1917	regulator->rdev->reg_data = data;
1918}
1919EXPORT_SYMBOL_GPL(regulator_set_drvdata);
1920
1921/**
1922 * regulator_get_id - get regulator ID
1923 * @regulator: regulator
1924 */
1925int rdev_get_id(struct regulator_dev *rdev)
1926{
1927	return rdev->desc->id;
1928}
1929EXPORT_SYMBOL_GPL(rdev_get_id);
1930
1931struct device *rdev_get_dev(struct regulator_dev *rdev)
1932{
1933	return &rdev->dev;
1934}
1935EXPORT_SYMBOL_GPL(rdev_get_dev);
1936
1937void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
1938{
1939	return reg_init_data->driver_data;
1940}
1941EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
1942
1943static int __init regulator_init(void)
1944{
1945	printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
1946	return class_register(&regulator_class);
1947}
1948
1949/* init early to allow our consumers to complete system booting */
1950core_initcall(regulator_init);
1951