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