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