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