core.c revision 4fca9545d17b99cdb2774716b034c62a70151bcd
1414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/*
2414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * core.c  --  Voltage/Current Regulator framework.
3414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
4414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * Copyright 2008 SlimLogic Ltd.
6414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
7a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
9414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *  This program is free software; you can redistribute  it and/or modify it
10414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *  under  the terms of  the GNU General  Public License as published by the
11414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *  Free Software Foundation;  either version 2 of the  License, or (at your
12414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *  option) any later version.
13414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
14414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
15414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
16414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#include <linux/kernel.h>
17414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#include <linux/init.h>
18414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#include <linux/device.h>
19414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#include <linux/err.h>
20414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#include <linux/mutex.h>
21414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#include <linux/suspend.h>
22414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#include <linux/regulator/consumer.h>
23414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#include <linux/regulator/driver.h>
24414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#include <linux/regulator/machine.h>
25414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
26414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#define REGULATOR_VERSION "0.5"
27414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
28414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic DEFINE_MUTEX(regulator_list_mutex);
29414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic LIST_HEAD(regulator_list);
30414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic LIST_HEAD(regulator_map_list);
31414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
32414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
33414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * struct regulator_dev
34414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
35414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Voltage / Current regulator class device. One for each regulator.
36414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
37414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstruct regulator_dev {
38414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_desc *desc;
39414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int use_count;
40414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
41414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* lists we belong to */
42414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct list_head list; /* list of all regulators */
43414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct list_head slist; /* list of supplied regulators */
44414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
45414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* lists we own */
46414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct list_head consumer_list; /* consumers we supply */
47414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct list_head supply_list; /* regulators we supply */
48414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
49414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct blocking_notifier_head notifier;
50414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct mutex mutex; /* consumer lock */
51414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct module *owner;
52414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct device dev;
53414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulation_constraints *constraints;
54414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *supply;	/* for tree */
55414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
56414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	void *reg_data;		/* regulator_dev data */
57414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood};
58414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
59414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
60414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * struct regulator_map
61414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
62414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Used to provide symbolic supply names to devices.
63414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
64414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstruct regulator_map {
65414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct list_head list;
66414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct device *dev;
67414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	const char *supply;
68a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *regulator;
69414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood};
70414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
71414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/*
72414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * struct regulator
73414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
74414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * One for each consumer device.
75414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
76414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstruct regulator {
77414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct device *dev;
78414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct list_head list;
79414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int uA_load;
80414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int min_uV;
81414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int max_uV;
82412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	int enabled; /* count of client enables */
83414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	char *supply_name;
84414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct device_attribute dev_attr;
85414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev;
86414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood};
87414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
88414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_is_enabled(struct regulator_dev *rdev);
89414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_disable(struct regulator_dev *rdev);
90414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_get_voltage(struct regulator_dev *rdev);
91414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_get_current_limit(struct regulator_dev *rdev);
92414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic unsigned int _regulator_get_mode(struct regulator_dev *rdev);
93414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic void _notifier_call_chain(struct regulator_dev *rdev,
94414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				  unsigned long event, void *data);
95414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
96414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* gets the regulator for a given consumer device */
97414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic struct regulator *get_device_regulator(struct device *dev)
98414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
99414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator *regulator = NULL;
100414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev;
101414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
102414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&regulator_list_mutex);
103414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_for_each_entry(rdev, &regulator_list, list) {
104414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		mutex_lock(&rdev->mutex);
105414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		list_for_each_entry(regulator, &rdev->consumer_list, list) {
106414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			if (regulator->dev == dev) {
107414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				mutex_unlock(&rdev->mutex);
108414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				mutex_unlock(&regulator_list_mutex);
109414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				return regulator;
110414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			}
111414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
112414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		mutex_unlock(&rdev->mutex);
113414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
114414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&regulator_list_mutex);
115414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return NULL;
116414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
117414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
118414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* Platform voltage constraint check */
119414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int regulator_check_voltage(struct regulator_dev *rdev,
120414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				   int *min_uV, int *max_uV)
121414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
122414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	BUG_ON(*min_uV > *max_uV);
123414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
124414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints) {
125414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: no constraints for %s\n", __func__,
126414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       rdev->desc->name);
127414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -ENODEV;
128414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
129414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
130414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: operation not allowed for %s\n",
131414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       __func__, rdev->desc->name);
132414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EPERM;
133414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
134414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
135414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (*max_uV > rdev->constraints->max_uV)
136414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		*max_uV = rdev->constraints->max_uV;
137414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (*min_uV < rdev->constraints->min_uV)
138414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		*min_uV = rdev->constraints->min_uV;
139414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
140414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (*min_uV > *max_uV)
141414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EINVAL;
142414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
143414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return 0;
144414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
145414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
146414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* current constraint check */
147414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int regulator_check_current_limit(struct regulator_dev *rdev,
148414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					int *min_uA, int *max_uA)
149414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
150414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	BUG_ON(*min_uA > *max_uA);
151414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
152414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints) {
153414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: no constraints for %s\n", __func__,
154414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       rdev->desc->name);
155414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -ENODEV;
156414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
157414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
158414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: operation not allowed for %s\n",
159414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       __func__, rdev->desc->name);
160414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EPERM;
161414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
162414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
163414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (*max_uA > rdev->constraints->max_uA)
164414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		*max_uA = rdev->constraints->max_uA;
165414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (*min_uA < rdev->constraints->min_uA)
166414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		*min_uA = rdev->constraints->min_uA;
167414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
168414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (*min_uA > *max_uA)
169414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EINVAL;
170414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
171414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return 0;
172414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
173414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
174414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* operating mode constraint check */
175414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int regulator_check_mode(struct regulator_dev *rdev, int mode)
176414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
177e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	switch (mode) {
178e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	case REGULATOR_MODE_FAST:
179e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	case REGULATOR_MODE_NORMAL:
180e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	case REGULATOR_MODE_IDLE:
181e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	case REGULATOR_MODE_STANDBY:
182e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell		break;
183e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	default:
184e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell		return -EINVAL;
185e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	}
186e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell
187414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints) {
188414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: no constraints for %s\n", __func__,
189414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       rdev->desc->name);
190414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -ENODEV;
191414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
192414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
193414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: operation not allowed for %s\n",
194414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       __func__, rdev->desc->name);
195414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EPERM;
196414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
197414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!(rdev->constraints->valid_modes_mask & mode)) {
198414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: invalid mode %x for %s\n",
199414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       __func__, mode, rdev->desc->name);
200414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EINVAL;
201414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
202414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return 0;
203414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
204414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
205414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* dynamic regulator mode switching constraint check */
206414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int regulator_check_drms(struct regulator_dev *rdev)
207414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
208414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints) {
209414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: no constraints for %s\n", __func__,
210414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       rdev->desc->name);
211414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -ENODEV;
212414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
213414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
214414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: operation not allowed for %s\n",
215414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       __func__, rdev->desc->name);
216414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EPERM;
217414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
218414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return 0;
219414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
220414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
221414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t device_requested_uA_show(struct device *dev,
222414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			     struct device_attribute *attr, char *buf)
223414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
224414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator *regulator;
225414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
226414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator = get_device_regulator(dev);
227414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (regulator == NULL)
228414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return 0;
229414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
230414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", regulator->uA_load);
231414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
232414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
233414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_uV_show(struct device *dev,
234414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				struct device_attribute *attr, char *buf)
235414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
236a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
237414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ssize_t ret;
238414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
239414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
240414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
241414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
242414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
243414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
244414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
245414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
246414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_uA_show(struct device *dev,
247414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				struct device_attribute *attr, char *buf)
248414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
249a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
250414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
251414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
252414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
253414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
254bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brownstatic ssize_t regulator_name_show(struct device *dev,
255bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown			     struct device_attribute *attr, char *buf)
256bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown{
257bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown	struct regulator_dev *rdev = dev_get_drvdata(dev);
258bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown	const char *name;
259bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown
260bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown	if (rdev->constraints->name)
261bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown		name = rdev->constraints->name;
262bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown	else if (rdev->desc->name)
263bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown		name = rdev->desc->name;
264bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown	else
265bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown		name = "";
266bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown
267bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown	return sprintf(buf, "%s\n", name);
268bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown}
269bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown
2704fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownellstatic ssize_t regulator_print_opmode(char *buf, int mode)
271414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
272414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	switch (mode) {
273414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	case REGULATOR_MODE_FAST:
274414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "fast\n");
275414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	case REGULATOR_MODE_NORMAL:
276414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "normal\n");
277414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	case REGULATOR_MODE_IDLE:
278414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "idle\n");
279414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	case REGULATOR_MODE_STANDBY:
280414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "standby\n");
281414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
282414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "unknown\n");
283414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
284414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
2854fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownellstatic ssize_t regulator_opmode_show(struct device *dev,
2864fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell				    struct device_attribute *attr, char *buf)
287414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
288a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
289414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
2904fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	return regulator_print_opmode(buf, _regulator_get_mode(rdev));
2914fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell}
2924fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell
2934fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownellstatic ssize_t regulator_print_state(char *buf, int state)
2944fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell{
295414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (state > 0)
296414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "enabled\n");
297414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	else if (state == 0)
298414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "disabled\n");
299414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	else
300414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "unknown\n");
301414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
302414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
3034fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownellstatic ssize_t regulator_state_show(struct device *dev,
3044fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell				   struct device_attribute *attr, char *buf)
3054fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell{
3064fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	struct regulator_dev *rdev = dev_get_drvdata(dev);
3074fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell
3084fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	return regulator_print_state(buf, _regulator_is_enabled(rdev));
3094fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell}
3104fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell
311414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_min_uA_show(struct device *dev,
312414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				    struct device_attribute *attr, char *buf)
313414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
314a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
315414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
316414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
317414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "constraint not defined\n");
318414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
319414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", rdev->constraints->min_uA);
320414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
321414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
322414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_max_uA_show(struct device *dev,
323414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				    struct device_attribute *attr, char *buf)
324414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
325a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
326414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
327414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
328414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "constraint not defined\n");
329414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
330414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", rdev->constraints->max_uA);
331414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
332414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
333414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_min_uV_show(struct device *dev,
334414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				    struct device_attribute *attr, char *buf)
335414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
336a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
337414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
338414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
339414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "constraint not defined\n");
340414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
341414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", rdev->constraints->min_uV);
342414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
343414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
344414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_max_uV_show(struct device *dev,
345414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				    struct device_attribute *attr, char *buf)
346414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
347a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
348414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
349414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
350414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "constraint not defined\n");
351414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
352414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", rdev->constraints->max_uV);
353414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
354414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
355414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_total_uA_show(struct device *dev,
356414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				      struct device_attribute *attr, char *buf)
357414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
358a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
359414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator *regulator;
360414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int uA = 0;
361414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
362414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
363414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_for_each_entry(regulator, &rdev->consumer_list, list)
364414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	    uA += regulator->uA_load;
365414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
366414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", uA);
367414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
368414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
369414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_num_users_show(struct device *dev,
370414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				      struct device_attribute *attr, char *buf)
371414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
372a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
373414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", rdev->use_count);
374414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
375414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
376414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_type_show(struct device *dev,
377414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				  struct device_attribute *attr, char *buf)
378414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
379a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
380414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
381414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	switch (rdev->desc->type) {
382414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	case REGULATOR_VOLTAGE:
383414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "voltage\n");
384414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	case REGULATOR_CURRENT:
385414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "current\n");
386414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
387414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "unknown\n");
388414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
389414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
390414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_suspend_mem_uV_show(struct device *dev,
391414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				struct device_attribute *attr, char *buf)
392414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
393a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
394414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
395414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
396414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "not defined\n");
397414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
398414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
399414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
400414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_suspend_disk_uV_show(struct device *dev,
401414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				struct device_attribute *attr, char *buf)
402414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
403a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
404414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
405414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
406414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "not defined\n");
407414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
408414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
409414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
410414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_suspend_standby_uV_show(struct device *dev,
411414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				struct device_attribute *attr, char *buf)
412414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
413a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
414414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
415414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
416414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "not defined\n");
417414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
418414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
419414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
420414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_suspend_mem_mode_show(struct device *dev,
421414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				struct device_attribute *attr, char *buf)
422414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
423a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
424414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
425414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
426414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "not defined\n");
4274fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	return regulator_print_opmode(buf,
4284fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell		rdev->constraints->state_mem.mode);
429414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
430414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
431414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_suspend_disk_mode_show(struct device *dev,
432414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				struct device_attribute *attr, char *buf)
433414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
434a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
435414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
436414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
437414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "not defined\n");
4384fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	return regulator_print_opmode(buf,
4394fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell		rdev->constraints->state_disk.mode);
440414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
441414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
442414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_suspend_standby_mode_show(struct device *dev,
443414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				struct device_attribute *attr, char *buf)
444414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
445a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
446414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
447414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
448414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "not defined\n");
4494fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	return regulator_print_opmode(buf,
4504fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell		rdev->constraints->state_standby.mode);
451414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
452414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
453414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_suspend_mem_state_show(struct device *dev,
454414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				   struct device_attribute *attr, char *buf)
455414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
456a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
457414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
458414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
459414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "not defined\n");
460414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
4614fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	return regulator_print_state(buf,
4624fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell			rdev->constraints->state_mem.enabled);
463414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
464414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
465414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_suspend_disk_state_show(struct device *dev,
466414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				   struct device_attribute *attr, char *buf)
467414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
468a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
469414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
470414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
471414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "not defined\n");
472414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
4734fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	return regulator_print_state(buf,
4744fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell			rdev->constraints->state_disk.enabled);
475414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
476414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
477414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic ssize_t regulator_suspend_standby_state_show(struct device *dev,
478414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				   struct device_attribute *attr, char *buf)
479414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
480a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
481414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
482414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
483414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return sprintf(buf, "not defined\n");
484414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
4854fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	return regulator_print_state(buf,
4864fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell			rdev->constraints->state_standby.enabled);
487414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
488bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown
489414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic struct device_attribute regulator_dev_attrs[] = {
490bc558a60b58f638ee0188affb627d4894a97b1c7Mark Brown	__ATTR(name, 0444, regulator_name_show, NULL),
491414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(microvolts, 0444, regulator_uV_show, NULL),
492414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(microamps, 0444, regulator_uA_show, NULL),
493414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(opmode, 0444, regulator_opmode_show, NULL),
494414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(state, 0444, regulator_state_show, NULL),
495414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL),
496414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(min_microamps, 0444, regulator_min_uA_show, NULL),
497414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL),
498414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(max_microamps, 0444, regulator_max_uA_show, NULL),
499414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL),
500414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(num_users, 0444, regulator_num_users_show, NULL),
501414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(type, 0444, regulator_type_show, NULL),
502414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(suspend_mem_microvolts, 0444,
503414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_suspend_mem_uV_show, NULL),
504414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(suspend_disk_microvolts, 0444,
505414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_suspend_disk_uV_show, NULL),
506414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(suspend_standby_microvolts, 0444,
507414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_suspend_standby_uV_show, NULL),
508414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(suspend_mem_mode, 0444,
509414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_suspend_mem_mode_show, NULL),
510414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(suspend_disk_mode, 0444,
511414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_suspend_disk_mode_show, NULL),
512414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(suspend_standby_mode, 0444,
513414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_suspend_standby_mode_show, NULL),
514414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(suspend_mem_state, 0444,
515414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_suspend_mem_state_show, NULL),
516414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(suspend_disk_state, 0444,
517414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_suspend_disk_state_show, NULL),
518414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR(suspend_standby_state, 0444,
519414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_suspend_standby_state_show, NULL),
520414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	__ATTR_NULL,
521414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood};
522414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
523414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic void regulator_dev_release(struct device *dev)
524414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
525a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *rdev = dev_get_drvdata(dev);
526414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	kfree(rdev);
527414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
528414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
529414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic struct class regulator_class = {
530414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	.name = "regulator",
531414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	.dev_release = regulator_dev_release,
532414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	.dev_attrs = regulator_dev_attrs,
533414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood};
534414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
535414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* Calculate the new optimum regulator operating mode based on the new total
536414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * consumer load. All locks held by caller */
537414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic void drms_uA_update(struct regulator_dev *rdev)
538414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
539414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator *sibling;
540414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int current_uA = 0, output_uV, input_uV, err;
541414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	unsigned int mode;
542414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
543414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	err = regulator_check_drms(rdev);
544414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
545414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	    !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
546414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return;
547414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
548414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* get output voltage */
549414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	output_uV = rdev->desc->ops->get_voltage(rdev);
550414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (output_uV <= 0)
551414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return;
552414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
553414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* get input voltage */
554414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->supply && rdev->supply->desc->ops->get_voltage)
555414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
556414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	else
557414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		input_uV = rdev->constraints->input_uV;
558414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (input_uV <= 0)
559414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return;
560414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
561414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* calc total requested load */
562414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_for_each_entry(sibling, &rdev->consumer_list, list)
563414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	    current_uA += sibling->uA_load;
564414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
565414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* now get the optimum mode for our new total regulator load */
566414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
567414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood						  output_uV, current_uA);
568414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
569414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* check the new mode is allowed */
570414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	err = regulator_check_mode(rdev, mode);
571414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (err == 0)
572414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		rdev->desc->ops->set_mode(rdev, mode);
573414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
574414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
575414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int suspend_set_state(struct regulator_dev *rdev,
576414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_state *rstate)
577414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
578414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret = 0;
579414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
580414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* enable & disable are mandatory for suspend control */
581414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->desc->ops->set_suspend_enable ||
582a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		!rdev->desc->ops->set_suspend_disable) {
583a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		printk(KERN_ERR "%s: no way to set suspend state\n",
584a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			__func__);
585414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EINVAL;
586a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	}
587414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
588414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rstate->enabled)
589414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = rdev->desc->ops->set_suspend_enable(rdev);
590414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	else
591414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = rdev->desc->ops->set_suspend_disable(rdev);
592414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (ret < 0) {
593414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
594414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return ret;
595414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
596414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
597414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
598414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
599414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (ret < 0) {
600414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			printk(KERN_ERR "%s: failed to set voltage\n",
601414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				__func__);
602414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			return ret;
603414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
604414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
605414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
606414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
607414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
608414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (ret < 0) {
609414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			printk(KERN_ERR "%s: failed to set mode\n", __func__);
610414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			return ret;
611414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
612414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
613414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
614414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
615414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
616414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* locks held by caller */
617414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
618414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
619414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints)
620414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EINVAL;
621414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
622414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	switch (state) {
623414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	case PM_SUSPEND_STANDBY:
624414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return suspend_set_state(rdev,
625414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			&rdev->constraints->state_standby);
626414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	case PM_SUSPEND_MEM:
627414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return suspend_set_state(rdev,
628414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			&rdev->constraints->state_mem);
629414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	case PM_SUSPEND_MAX:
630414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return suspend_set_state(rdev,
631414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			&rdev->constraints->state_disk);
632414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	default:
633414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EINVAL;
634414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
635414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
636414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
637414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic void print_constraints(struct regulator_dev *rdev)
638414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
639414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulation_constraints *constraints = rdev->constraints;
640414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	char buf[80];
641414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int count;
642414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
643414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->desc->type == REGULATOR_VOLTAGE) {
644414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (constraints->min_uV == constraints->max_uV)
645414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			count = sprintf(buf, "%d mV ",
646414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					constraints->min_uV / 1000);
647414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		else
648414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			count = sprintf(buf, "%d <--> %d mV ",
649414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					constraints->min_uV / 1000,
650414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					constraints->max_uV / 1000);
651414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	} else {
652414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (constraints->min_uA == constraints->max_uA)
653414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			count = sprintf(buf, "%d mA ",
654414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					constraints->min_uA / 1000);
655414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		else
656414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			count = sprintf(buf, "%d <--> %d mA ",
657414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					constraints->min_uA / 1000,
658414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					constraints->max_uA / 1000);
659414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
660414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
661414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		count += sprintf(buf + count, "fast ");
662414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
663414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		count += sprintf(buf + count, "normal ");
664414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
665414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		count += sprintf(buf + count, "idle ");
666414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
667414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		count += sprintf(buf + count, "standby");
668414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
669414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
670414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
671414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
672a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood/**
673a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * set_machine_constraints - sets regulator constraints
674a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * @regulator: regulator source
675a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood *
676a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * Allows platform initialisation code to define and constrain
677a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE:
678a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * Constraints *must* be set by platform code in order for some
679a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * regulator operations to proceed i.e. set_voltage, set_current_limit,
680a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * set_mode.
681a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood */
682a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodstatic int set_machine_constraints(struct regulator_dev *rdev,
683a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulation_constraints *constraints)
684a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood{
685a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	int ret = 0;
686e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown	const char *name;
687e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown	struct regulator_ops *ops = rdev->desc->ops;
688e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown
689e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown	if (constraints->name)
690e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown		name = constraints->name;
691e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown	else if (rdev->desc->name)
692e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown		name = rdev->desc->name;
693e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown	else
694e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown		name = "regulator";
695a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
696a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	rdev->constraints = constraints;
697a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
698a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	/* do we need to apply the constraint voltage */
699a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	if (rdev->constraints->apply_uV &&
700a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		rdev->constraints->min_uV == rdev->constraints->max_uV &&
701e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown		ops->set_voltage) {
702e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown		ret = ops->set_voltage(rdev,
703a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			rdev->constraints->min_uV, rdev->constraints->max_uV);
704a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			if (ret < 0) {
705e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown				printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
706e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown				       __func__,
707e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown				       rdev->constraints->min_uV, name);
708a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood				rdev->constraints = NULL;
709a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood				goto out;
710a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			}
711a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	}
712a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
713a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	/* are we enabled at boot time by firmware / bootloader */
714a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	if (rdev->constraints->boot_on)
715a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		rdev->use_count = 1;
716a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
717a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	/* do we need to setup our suspend state */
718e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown	if (constraints->initial_state) {
719a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		ret = suspend_prepare(rdev, constraints->initial_state);
720e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown		if (ret < 0) {
721e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown			printk(KERN_ERR "%s: failed to set suspend state for %s\n",
722e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown			       __func__, name);
723e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown			rdev->constraints = NULL;
724e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown			goto out;
725e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown		}
726e06f5b4fea243b152c79fe5d9552a852069de483Mark Brown	}
727a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
728e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown	/* if always_on is set then turn the regulator on if it's not
729e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown	 * already on. */
730e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown	if (constraints->always_on && ops->enable &&
731e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown	    ((ops->is_enabled && !ops->is_enabled(rdev)) ||
732e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown	     (!ops->is_enabled && !constraints->boot_on))) {
733e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown		ret = ops->enable(rdev);
734e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown		if (ret < 0) {
735e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown			printk(KERN_ERR "%s: failed to enable %s\n",
736e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown			       __func__, name);
737e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown			rdev->constraints = NULL;
738e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown			goto out;
739e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown		}
740e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown	}
741e5fda26c7ea9430d7d953364f900bafdce2be67bMark Brown
742a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	print_constraints(rdev);
743a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodout:
744a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	return ret;
745a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood}
746a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
747a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood/**
748a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * set_supply - set regulator supply regulator
749a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * @regulator: regulator name
750a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * @supply: supply regulator name
751a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood *
752a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * Called by platform initialisation code to set the supply regulator for this
753a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * regulator. This ensures that a regulators supply will also be enabled by the
754a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * core if it's child is enabled.
755a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood */
756a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodstatic int set_supply(struct regulator_dev *rdev,
757a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_dev *supply_rdev)
758a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood{
759a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	int err;
760a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
761a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
762a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood				"supply");
763a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	if (err) {
764a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		printk(KERN_ERR
765a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		       "%s: could not add device link %s err %d\n",
766a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		       __func__, supply_rdev->dev.kobj.name, err);
767a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		       goto out;
768a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	}
769a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	rdev->supply = supply_rdev;
770a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	list_add(&rdev->slist, &supply_rdev->supply_list);
771a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodout:
772a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	return err;
773a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood}
774a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
775a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood/**
776a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * set_consumer_device_supply: Bind a regulator to a symbolic supply
777a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * @regulator: regulator source
778a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * @dev:       device the supply applies to
779a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * @supply:    symbolic name for supply
780a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood *
781a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * Allows platform initialisation code to map physical regulator
782a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * sources to symbolic names for supplies for use by devices.  Devices
783a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * should use these symbolic names to request regulators, avoiding the
784a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood * need to provide board-specific regulator names as platform data.
785a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood */
786a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodstatic int set_consumer_device_supply(struct regulator_dev *rdev,
787a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct device *consumer_dev, const char *supply)
788a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood{
789a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_map *node;
790a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
791a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	if (supply == NULL)
792a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		return -EINVAL;
793a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
794a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
795a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	if (node == NULL)
796a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		return -ENOMEM;
797a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
798a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	node->regulator = rdev;
799a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	node->dev = consumer_dev;
800a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	node->supply = supply;
801a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
802a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	list_add(&node->list, &regulator_map_list);
803a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	return 0;
804a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood}
805a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
806a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodstatic void unset_consumer_device_supply(struct regulator_dev *rdev,
807a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct device *consumer_dev)
808a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood{
809a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_map *node, *n;
810a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
811a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	list_for_each_entry_safe(node, n, &regulator_map_list, list) {
812a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		if (rdev == node->regulator &&
813a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			consumer_dev == node->dev) {
814a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			list_del(&node->list);
815a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			kfree(node);
816a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			return;
817a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		}
818a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	}
819a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood}
820a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
821414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood#define REG_STR_SIZE	32
822414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
823414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic struct regulator *create_regulator(struct regulator_dev *rdev,
824414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					  struct device *dev,
825414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					  const char *supply_name)
826414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
827414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator *regulator;
828414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	char buf[REG_STR_SIZE];
829414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int err, size;
830414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
831414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
832414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (regulator == NULL)
833414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return NULL;
834414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
835414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
836414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator->rdev = rdev;
837414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_add(&regulator->list, &rdev->consumer_list);
838414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
839414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (dev) {
840414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		/* create a 'requested_microamps_name' sysfs entry */
841414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
842414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			supply_name);
843414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (size >= REG_STR_SIZE)
844414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto overflow_err;
845414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
846414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator->dev = dev;
847414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
848414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (regulator->dev_attr.attr.name == NULL)
849414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto attr_name_err;
850414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
851414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator->dev_attr.attr.owner = THIS_MODULE;
852414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator->dev_attr.attr.mode = 0444;
853414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator->dev_attr.show = device_requested_uA_show;
854414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		err = device_create_file(dev, &regulator->dev_attr);
855414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (err < 0) {
856414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			printk(KERN_WARNING "%s: could not add regulator_dev"
857414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				" load sysfs\n", __func__);
858414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto attr_name_err;
859414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
860414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
861414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		/* also add a link to the device sysfs entry */
862414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
863414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				 dev->kobj.name, supply_name);
864414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (size >= REG_STR_SIZE)
865414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto attr_err;
866414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
867414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator->supply_name = kstrdup(buf, GFP_KERNEL);
868414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (regulator->supply_name == NULL)
869414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto attr_err;
870414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
871414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
872414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood					buf);
873414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (err) {
874414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			printk(KERN_WARNING
875414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			       "%s: could not add device link %s err %d\n",
876414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			       __func__, dev->kobj.name, err);
877414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			device_remove_file(dev, &regulator->dev_attr);
878414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto link_name_err;
879414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
880414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
881414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
882414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return regulator;
883414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodlink_name_err:
884414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	kfree(regulator->supply_name);
885414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodattr_err:
886414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	device_remove_file(regulator->dev, &regulator->dev_attr);
887414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodattr_name_err:
888414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	kfree(regulator->dev_attr.attr.name);
889414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodoverflow_err:
890414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_del(&regulator->list);
891414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	kfree(regulator);
892414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
893414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return NULL;
894414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
895414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
896414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
897414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_get - lookup and obtain a reference to a regulator.
898414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @dev: device for regulator "consumer"
899414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @id: Supply name or regulator ID.
900414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
901414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Returns a struct regulator corresponding to the regulator producer,
902414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * or IS_ERR() condition containing errno.  Use of supply names
903414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * configured via regulator_set_device_supply() is strongly
904414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * encouraged.
905414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
906414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstruct regulator *regulator_get(struct device *dev, const char *id)
907414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
908414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev;
909414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_map *map;
910414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator *regulator = ERR_PTR(-ENODEV);
911414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
912414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (id == NULL) {
913414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "regulator: get() with no identifier\n");
914414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return regulator;
915414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
916414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
917414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&regulator_list_mutex);
918414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
919414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_for_each_entry(map, &regulator_map_list, list) {
920414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (dev == map->dev &&
921414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		    strcmp(map->supply, id) == 0) {
922a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			rdev = map->regulator;
923414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto found;
924a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		}
925414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
926414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
927414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	       id);
928414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&regulator_list_mutex);
929414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return regulator;
930414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
931414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodfound:
932a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	if (!try_module_get(rdev->owner))
933a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		goto out;
934a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
935414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator = create_regulator(rdev, dev, id);
936414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (regulator == NULL) {
937414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator = ERR_PTR(-ENOMEM);
938414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		module_put(rdev->owner);
939414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
940414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
941a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodout:
942414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&regulator_list_mutex);
943414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return regulator;
944414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
945414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_get);
946414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
947414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
948414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_put - "free" the regulator source
949414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
950414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
951414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Note: drivers must ensure that all regulator_enable calls made on this
952414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator source are balanced by regulator_disable calls prior to calling
953414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * this function.
954414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
955414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodvoid regulator_put(struct regulator *regulator)
956414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
957414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev;
958414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
959414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (regulator == NULL || IS_ERR(regulator))
960414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return;
961414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
962414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&regulator_list_mutex);
963414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	rdev = regulator->rdev;
964414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
965412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
966412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell			       regulator->supply_name))
967412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell		_regulator_disable(rdev);
968412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell
969414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* remove any sysfs entries */
970414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (regulator->dev) {
971414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
972414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		kfree(regulator->supply_name);
973414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		device_remove_file(regulator->dev, &regulator->dev_attr);
974414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		kfree(regulator->dev_attr.attr.name);
975414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
976414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_del(&regulator->list);
977414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	kfree(regulator);
978414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
979414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	module_put(rdev->owner);
980414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&regulator_list_mutex);
981414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
982414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_put);
983414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
984414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* locks held by regulator_enable() */
985414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_enable(struct regulator_dev *rdev)
986414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
987414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret = -EINVAL;
988414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
989414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->constraints) {
990414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: %s has no constraints\n",
991414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       __func__, rdev->desc->name);
992414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return ret;
993414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
994414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
995414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* do we need to enable the supply regulator first */
996414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->supply) {
997414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = _regulator_enable(rdev->supply);
998414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (ret < 0) {
999414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			printk(KERN_ERR "%s: failed to enable %s: %d\n",
1000414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			       __func__, rdev->desc->name, ret);
1001414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			return ret;
1002414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
1003414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1004414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1005414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* check voltage and requested load before enabling */
1006414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->desc->ops->enable) {
1007414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1008414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (rdev->constraints &&
1009414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			(rdev->constraints->valid_ops_mask &
1010414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			REGULATOR_CHANGE_DRMS))
1011414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			drms_uA_update(rdev);
1012414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1013414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = rdev->desc->ops->enable(rdev);
1014414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (ret < 0) {
1015414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			printk(KERN_ERR "%s: failed to enable %s: %d\n",
1016414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			       __func__, rdev->desc->name, ret);
1017414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			return ret;
1018414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
1019414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		rdev->use_count++;
1020414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return ret;
1021414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1022414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1023414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1024414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1025414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1026414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1027414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_enable - enable regulator output
1028414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1029414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1030414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Enable the regulator output at the predefined voltage or current value.
1031414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: the output value can be set by other drivers, boot loader or may be
1032414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * hardwired in the regulator.
1033414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: calls to regulator_enable() must be balanced with calls to
1034414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_disable().
1035414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1036414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_enable(struct regulator *regulator)
1037414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1038412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	struct regulator_dev *rdev = regulator->rdev;
1039412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	int ret = 0;
1040414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1041412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	mutex_lock(&rdev->mutex);
1042412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	if (regulator->enabled == 0)
1043412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell		ret = _regulator_enable(rdev);
1044412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	else if (regulator->enabled < 0)
1045412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell		ret = -EIO;
1046412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	if (ret == 0)
1047412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell		regulator->enabled++;
1048412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	mutex_unlock(&rdev->mutex);
1049414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1050414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1051414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_enable);
1052414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1053414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* locks held by regulator_disable() */
1054414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_disable(struct regulator_dev *rdev)
1055414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1056414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret = 0;
1057414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1058414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* are we the last user and permitted to disable ? */
1059414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1060414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1061414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		/* we are last user */
1062414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (rdev->desc->ops->disable) {
1063414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			ret = rdev->desc->ops->disable(rdev);
1064414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			if (ret < 0) {
1065414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				printk(KERN_ERR "%s: failed to disable %s\n",
1066414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				       __func__, rdev->desc->name);
1067414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				return ret;
1068414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			}
1069414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
1070414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1071414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		/* decrease our supplies ref count and disable if required */
1072414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (rdev->supply)
1073414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			_regulator_disable(rdev->supply);
1074414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1075414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		rdev->use_count = 0;
1076414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	} else if (rdev->use_count > 1) {
1077414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1078414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (rdev->constraints &&
1079414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			(rdev->constraints->valid_ops_mask &
1080414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			REGULATOR_CHANGE_DRMS))
1081414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			drms_uA_update(rdev);
1082414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1083414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		rdev->use_count--;
1084414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1085414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1086414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1087414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1088414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1089414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_disable - disable regulator output
1090414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1091414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1092414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Disable the regulator output voltage or current.
1093414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: this will only disable the regulator output if no other consumer
1094414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * devices have it enabled.
1095414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: calls to regulator_enable() must be balanced with calls to
1096414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_disable().
1097414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1098414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_disable(struct regulator *regulator)
1099414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1100412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	struct regulator_dev *rdev = regulator->rdev;
1101412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	int ret = 0;
1102414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1103412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	mutex_lock(&rdev->mutex);
1104412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	if (regulator->enabled == 1) {
1105412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell		ret = _regulator_disable(rdev);
1106412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell		if (ret == 0)
1107412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell			regulator->uA_load = 0;
1108412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	} else if (WARN(regulator->enabled <= 0,
1109412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell			"unbalanced disables for supply %s\n",
1110412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell			regulator->supply_name))
1111412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell		ret = -EIO;
1112412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	if (ret == 0)
1113412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell		regulator->enabled--;
1114412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell	mutex_unlock(&rdev->mutex);
1115414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1116414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1117414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_disable);
1118414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1119414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* locks held by regulator_force_disable() */
1120414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_force_disable(struct regulator_dev *rdev)
1121414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1122414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret = 0;
1123414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1124414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* force disable */
1125414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->desc->ops->disable) {
1126414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		/* ah well, who wants to live forever... */
1127414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = rdev->desc->ops->disable(rdev);
1128414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (ret < 0) {
1129414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			printk(KERN_ERR "%s: failed to force disable %s\n",
1130414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			       __func__, rdev->desc->name);
1131414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			return ret;
1132414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
1133414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		/* notify other consumers that power has been forced off */
1134414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		_notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1135414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			NULL);
1136414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1137414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1138414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* decrease our supplies ref count and disable if required */
1139414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->supply)
1140414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		_regulator_disable(rdev->supply);
1141414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1142414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	rdev->use_count = 0;
1143414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1144414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1145414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1146414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1147414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_force_disable - force disable regulator output
1148414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1149414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1150414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Forcibly disable the regulator output voltage or current.
1151414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: this *will* disable the regulator output even if other consumer
1152414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * devices have it enabled. This should be used for situations when device
1153414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * damage will likely occur if the regulator is not disabled (e.g. over temp).
1154414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1155414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_force_disable(struct regulator *regulator)
1156414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1157414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1158414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1159414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&regulator->rdev->mutex);
1160414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator->enabled = 0;
1161414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator->uA_load = 0;
1162414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = _regulator_force_disable(regulator->rdev);
1163414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&regulator->rdev->mutex);
1164414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1165414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1166414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_force_disable);
1167414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1168414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_is_enabled(struct regulator_dev *rdev)
1169414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1170414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1171414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1172414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
1173414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1174414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* sanity check */
1175414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->desc->ops->is_enabled) {
1176414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = -EINVAL;
1177414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1178414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1179414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1180414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = rdev->desc->ops->is_enabled(rdev);
1181414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodout:
1182414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
1183414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1184414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1185414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1186414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1187414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_is_enabled - is the regulator output enabled
1188414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1189414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1190412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell * Returns positive if the regulator driver backing the source/client
1191412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell * has requested that the device be enabled, zero if it hasn't, else a
1192412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell * negative errno code.
1193412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell *
1194412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell * Note that the device backing this regulator handle can have multiple
1195412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell * users, so it might be enabled even if regulator_enable() was never
1196412aec610559bdb602a0a21ce149ba8ffbb6f983David Brownell * called for this particular source.
1197414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1198414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_is_enabled(struct regulator *regulator)
1199414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1200414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return _regulator_is_enabled(regulator->rdev);
1201414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1202414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_is_enabled);
1203414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1204414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1205414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_set_voltage - set regulator output voltage
1206414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1207414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @min_uV: Minimum required voltage in uV
1208414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @max_uV: Maximum acceptable voltage in uV
1209414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1210414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Sets a voltage regulator to the desired output voltage. This can be set
1211414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * during any regulator state. IOW, regulator can be disabled or enabled.
1212414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1213414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * If the regulator is enabled then the voltage will change to the new value
1214414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * immediately otherwise if the regulator is disabled the regulator will
1215414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * output at the new voltage when enabled.
1216414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1217414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: If the regulator is shared between several devices then the lowest
1218414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * request voltage that meets the system constraints will be used.
1219414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: Regulator system constraints must be set for this regulator before
1220414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * calling this function otherwise this call will fail.
1221414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1222414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1223414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1224414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev = regulator->rdev;
1225414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1226414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1227414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
1228414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1229414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* sanity check */
1230414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->desc->ops->set_voltage) {
1231414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = -EINVAL;
1232414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1233414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1234414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1235414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* constraints check */
1236414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1237414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (ret < 0)
1238414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1239414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator->min_uV = min_uV;
1240414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator->max_uV = max_uV;
1241414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1242414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1243414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodout:
1244414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
1245414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1246414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1247414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_set_voltage);
1248414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1249414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_get_voltage(struct regulator_dev *rdev)
1250414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1251414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* sanity check */
1252414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->desc->ops->get_voltage)
1253414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return rdev->desc->ops->get_voltage(rdev);
1254414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	else
1255414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EINVAL;
1256414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1257414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1258414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1259414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_get_voltage - get regulator output voltage
1260414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1261414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1262414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * This returns the current regulator voltage in uV.
1263414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1264414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: If the regulator is disabled it will return the voltage value. This
1265414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * function should not be used to determine regulator state.
1266414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1267414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_get_voltage(struct regulator *regulator)
1268414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1269414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1270414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1271414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&regulator->rdev->mutex);
1272414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1273414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = _regulator_get_voltage(regulator->rdev);
1274414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1275414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&regulator->rdev->mutex);
1276414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1277414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1278414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1279414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_get_voltage);
1280414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1281414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1282414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_set_current_limit - set regulator output current limit
1283414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1284414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @min_uA: Minimuum supported current in uA
1285414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @max_uA: Maximum supported current in uA
1286414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1287414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Sets current sink to the desired output current. This can be set during
1288414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * any regulator state. IOW, regulator can be disabled or enabled.
1289414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1290414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * If the regulator is enabled then the current will change to the new value
1291414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * immediately otherwise if the regulator is disabled the regulator will
1292414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * output at the new current when enabled.
1293414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1294414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: Regulator system constraints must be set for this regulator before
1295414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * calling this function otherwise this call will fail.
1296414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1297414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_set_current_limit(struct regulator *regulator,
1298414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			       int min_uA, int max_uA)
1299414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1300414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev = regulator->rdev;
1301414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1302414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1303414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
1304414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1305414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* sanity check */
1306414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->desc->ops->set_current_limit) {
1307414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = -EINVAL;
1308414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1309414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1310414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1311414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* constraints check */
1312414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1313414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (ret < 0)
1314414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1315414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1316414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1317414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodout:
1318414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
1319414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1320414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1321414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_set_current_limit);
1322414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1323414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int _regulator_get_current_limit(struct regulator_dev *rdev)
1324414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1325414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1326414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1327414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
1328414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1329414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* sanity check */
1330414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->desc->ops->get_current_limit) {
1331414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = -EINVAL;
1332414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1333414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1334414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1335414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = rdev->desc->ops->get_current_limit(rdev);
1336414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodout:
1337414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
1338414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1339414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1340414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1341414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1342414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_get_current_limit - get regulator output current
1343414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1344414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1345414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * This returns the current supplied by the specified current sink in uA.
1346414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1347414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: If the regulator is disabled it will return the current value. This
1348414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * function should not be used to determine regulator state.
1349414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1350414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_get_current_limit(struct regulator *regulator)
1351414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1352414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return _regulator_get_current_limit(regulator->rdev);
1353414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1354414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_get_current_limit);
1355414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1356414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1357414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_set_mode - set regulator operating mode
1358414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1359414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @mode: operating mode - one of the REGULATOR_MODE constants
1360414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1361414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Set regulator operating mode to increase regulator efficiency or improve
1362414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulation performance.
1363414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1364414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * NOTE: Regulator system constraints must be set for this regulator before
1365414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * calling this function otherwise this call will fail.
1366414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1367414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_set_mode(struct regulator *regulator, unsigned int mode)
1368414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1369414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev = regulator->rdev;
1370414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1371414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1372414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
1373414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1374414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* sanity check */
1375414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->desc->ops->set_mode) {
1376414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = -EINVAL;
1377414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1378414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1379414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1380414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* constraints check */
1381414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = regulator_check_mode(rdev, mode);
1382414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (ret < 0)
1383414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1384414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1385414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = rdev->desc->ops->set_mode(rdev, mode);
1386414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodout:
1387414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
1388414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1389414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1390414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_set_mode);
1391414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1392414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1393414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1394414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1395414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1396414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
1397414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1398414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* sanity check */
1399414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->desc->ops->get_mode) {
1400414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = -EINVAL;
1401414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1402414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1403414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1404414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = rdev->desc->ops->get_mode(rdev);
1405414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodout:
1406414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
1407414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1408414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1409414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1410414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1411414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_get_mode - get regulator operating mode
1412414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1413414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1414414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Get the current regulator operating mode.
1415414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1416414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodunsigned int regulator_get_mode(struct regulator *regulator)
1417414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1418414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return _regulator_get_mode(regulator->rdev);
1419414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1420414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_get_mode);
1421414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1422414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1423414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_set_optimum_mode - set regulator optimum operating mode
1424414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1425414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @uA_load: load current
1426414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1427414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Notifies the regulator core of a new device load. This is then used by
1428414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * DRMS (if enabled by constraints) to set the most efficient regulator
1429414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * operating mode for the new regulator loading.
1430414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1431414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Consumer devices notify their supply regulator of the maximum power
1432414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * they will require (can be taken from device datasheet in the power
1433414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * consumption tables) when they change operational status and hence power
1434414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * state. Examples of operational state changes that can affect power
1435414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * consumption are :-
1436414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1437414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *    o Device is opened / closed.
1438414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *    o Device I/O is about to begin or has just finished.
1439414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *    o Device is idling in between work.
1440414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1441414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * This information is also exported via sysfs to userspace.
1442414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1443414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * DRMS will sum the total requested load on the regulator and change
1444414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * to the most efficient operating mode if platform constraints allow.
1445414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1446414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Returns the new regulator mode or error.
1447414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1448414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1449414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1450414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev = regulator->rdev;
1451414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator *consumer;
1452414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret, output_uV, input_uV, total_uA_load = 0;
1453414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	unsigned int mode;
1454414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1455414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
1456414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1457414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator->uA_load = uA_load;
1458414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = regulator_check_drms(rdev);
1459414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (ret < 0)
1460414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1461414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = -EINVAL;
1462414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1463414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* sanity check */
1464414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!rdev->desc->ops->get_optimum_mode)
1465414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1466414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1467414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* get output voltage */
1468414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	output_uV = rdev->desc->ops->get_voltage(rdev);
1469414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (output_uV <= 0) {
1470414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1471414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			__func__, rdev->desc->name);
1472414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1473414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1474414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1475414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* get input voltage */
1476414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1477414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1478414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	else
1479414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		input_uV = rdev->constraints->input_uV;
1480414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (input_uV <= 0) {
1481414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1482414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			__func__, rdev->desc->name);
1483414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1484414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1485414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1486414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* calc total requested load for this regulator */
1487414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_for_each_entry(consumer, &rdev->consumer_list, list)
1488414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	    total_uA_load += consumer->uA_load;
1489414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1490414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mode = rdev->desc->ops->get_optimum_mode(rdev,
1491414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood						 input_uV, output_uV,
1492414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood						 total_uA_load);
1493e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	ret = regulator_check_mode(rdev, mode);
1494e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	if (ret < 0) {
1495414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1496414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			" %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1497414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			total_uA_load, input_uV, output_uV);
1498414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1499414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1500414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1501414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = rdev->desc->ops->set_mode(rdev, mode);
1502e573520b171095c106ffbbbf4f9cbed6d9bff576David Brownell	if (ret < 0) {
1503414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1504414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			__func__, mode, rdev->desc->name);
1505414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		goto out;
1506414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1507414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	ret = mode;
1508414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodout:
1509414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
1510414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1511414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1512414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1513414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1514414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1515414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_register_notifier - register regulator event notifier
1516414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1517414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @notifier_block: notifier block
1518414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1519414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Register notifier block to receive regulator events.
1520414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1521414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_register_notifier(struct regulator *regulator,
1522414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			      struct notifier_block *nb)
1523414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1524414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return blocking_notifier_chain_register(&regulator->rdev->notifier,
1525414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood						nb);
1526414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1527414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_register_notifier);
1528414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1529414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1530414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_unregister_notifier - unregister regulator event notifier
1531414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1532414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @notifier_block: notifier block
1533414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1534414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Unregister regulator event notifier block.
1535414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1536414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_unregister_notifier(struct regulator *regulator,
1537414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				struct notifier_block *nb)
1538414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1539414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1540414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood						  nb);
1541414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1542414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1543414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1544414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* notify regulator consumers and downstream regulator consumers */
1545414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic void _notifier_call_chain(struct regulator_dev *rdev,
1546414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				  unsigned long event, void *data)
1547414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1548414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *_rdev;
1549414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1550414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* call rdev chain first */
1551414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&rdev->mutex);
1552414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1553414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&rdev->mutex);
1554414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1555414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* now notify regulator we supply */
1556414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_for_each_entry(_rdev, &rdev->supply_list, slist)
1557414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		_notifier_call_chain(_rdev, event, data);
1558414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1559414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1560414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1561414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_bulk_get - get multiple regulator consumers
1562414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1563414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @dev:           Device to supply
1564414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @num_consumers: Number of consumers to register
1565414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @consumers:     Configuration of consumers; clients are stored here.
1566414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1567414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @return 0 on success, an errno on failure.
1568414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1569414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * This helper function allows drivers to get several regulator
1570414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * consumers in one operation.  If any of the regulators cannot be
1571414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * acquired then any regulators that were allocated will be freed
1572414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * before returning to the caller.
1573414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1574414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_bulk_get(struct device *dev, int num_consumers,
1575414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		       struct regulator_bulk_data *consumers)
1576414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1577414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int i;
1578414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1579414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1580414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	for (i = 0; i < num_consumers; i++)
1581414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		consumers[i].consumer = NULL;
1582414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1583414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	for (i = 0; i < num_consumers; i++) {
1584414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		consumers[i].consumer = regulator_get(dev,
1585414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood						      consumers[i].supply);
1586414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (IS_ERR(consumers[i].consumer)) {
1587414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			dev_err(dev, "Failed to get supply '%s'\n",
1588414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				consumers[i].supply);
1589414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			ret = PTR_ERR(consumers[i].consumer);
1590414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			consumers[i].consumer = NULL;
1591414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto err;
1592414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
1593414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1594414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1595414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return 0;
1596414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1597414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwooderr:
1598414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1599414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_put(consumers[i].consumer);
1600414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1601414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1602414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1603414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_bulk_get);
1604414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1605414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1606414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_bulk_enable - enable multiple regulator consumers
1607414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1608414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @num_consumers: Number of consumers
1609414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @consumers:     Consumer data; clients are stored here.
1610414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @return         0 on success, an errno on failure
1611414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1612414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * This convenience API allows consumers to enable multiple regulator
1613414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * clients in a single API call.  If any consumers cannot be enabled
1614414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * then any others that were enabled will be disabled again prior to
1615414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * return.
1616414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1617414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_bulk_enable(int num_consumers,
1618414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			  struct regulator_bulk_data *consumers)
1619414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1620414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int i;
1621414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1622414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1623414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	for (i = 0; i < num_consumers; i++) {
1624414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = regulator_enable(consumers[i].consumer);
1625414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (ret != 0)
1626414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto err;
1627414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1628414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1629414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return 0;
1630414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1631414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwooderr:
1632414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1633414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	for (i = 0; i < num_consumers; i++)
1634414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_disable(consumers[i].consumer);
1635414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1636414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1637414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1638414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_bulk_enable);
1639414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1640414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1641414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_bulk_disable - disable multiple regulator consumers
1642414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1643414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @num_consumers: Number of consumers
1644414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @consumers:     Consumer data; clients are stored here.
1645414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @return         0 on success, an errno on failure
1646414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1647414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * This convenience API allows consumers to disable multiple regulator
1648414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * clients in a single API call.  If any consumers cannot be enabled
1649414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * then any others that were disabled will be disabled again prior to
1650414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * return.
1651414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1652414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_bulk_disable(int num_consumers,
1653414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			   struct regulator_bulk_data *consumers)
1654414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1655414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int i;
1656414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret;
1657414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1658414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	for (i = 0; i < num_consumers; i++) {
1659414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = regulator_disable(consumers[i].consumer);
1660414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (ret != 0)
1661414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto err;
1662414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1663414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1664414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return 0;
1665414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1666414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwooderr:
1667414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1668414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	for (i = 0; i < num_consumers; i++)
1669414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_enable(consumers[i].consumer);
1670414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1671414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1672414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1673414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_bulk_disable);
1674414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1675414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1676414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_bulk_free - free multiple regulator consumers
1677414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1678414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @num_consumers: Number of consumers
1679414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @consumers:     Consumer data; clients are stored here.
1680414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1681414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * This convenience API allows consumers to free multiple regulator
1682414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * clients in a single API call.
1683414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1684414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodvoid regulator_bulk_free(int num_consumers,
1685414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			 struct regulator_bulk_data *consumers)
1686414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1687414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int i;
1688414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1689414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	for (i = 0; i < num_consumers; i++) {
1690414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		regulator_put(consumers[i].consumer);
1691414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		consumers[i].consumer = NULL;
1692414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1693414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1694414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_bulk_free);
1695414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1696414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1697414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_notifier_call_chain - call regulator event notifier
1698414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1699414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @event: notifier block
1700414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @data:
1701414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1702414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Called by regulator drivers to notify clients a regulator event has
1703414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * occurred. We also notify regulator clients downstream.
1704414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1705414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_notifier_call_chain(struct regulator_dev *rdev,
1706414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				  unsigned long event, void *data)
1707414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1708414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	_notifier_call_chain(rdev, event, data);
1709414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return NOTIFY_DONE;
1710414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1711414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1712414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1713414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1714414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1715414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_register - register regulator
1716414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1717414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @reg_data: private regulator data
1718414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1719414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Called by regulator drivers to register a regulator.
1720414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Returns 0 on success.
1721414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1722414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstruct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1723a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct device *dev, void *driver_data)
1724414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1725414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	static atomic_t regulator_no = ATOMIC_INIT(0);
1726414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev;
1727a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	struct regulator_init_data *init_data = dev->platform_data;
1728a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	int ret, i;
1729414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1730414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (regulator_desc == NULL)
1731414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return ERR_PTR(-EINVAL);
1732414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1733414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1734414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return ERR_PTR(-EINVAL);
1735414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1736414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1737414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	    !regulator_desc->type == REGULATOR_CURRENT)
1738414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return ERR_PTR(-EINVAL);
1739414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
174046fabe1edd44a8893d88d7982f88d01ccf77f0bbMark Brown	if (!init_data)
174146fabe1edd44a8893d88d7982f88d01ccf77f0bbMark Brown		return ERR_PTR(-EINVAL);
174246fabe1edd44a8893d88d7982f88d01ccf77f0bbMark Brown
1743414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1744414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev == NULL)
1745414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return ERR_PTR(-ENOMEM);
1746414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1747414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&regulator_list_mutex);
1748414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1749414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_init(&rdev->mutex);
1750a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	rdev->reg_data = driver_data;
1751414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	rdev->owner = regulator_desc->owner;
1752414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	rdev->desc = regulator_desc;
1753414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	INIT_LIST_HEAD(&rdev->consumer_list);
1754414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	INIT_LIST_HEAD(&rdev->supply_list);
1755414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	INIT_LIST_HEAD(&rdev->list);
1756414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	INIT_LIST_HEAD(&rdev->slist);
1757414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1758414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1759a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	/* preform any regulator specific init */
1760a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	if (init_data->regulator_init) {
1761a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		ret = init_data->regulator_init(rdev->reg_data);
17624fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell		if (ret < 0)
17634fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell			goto clean;
1764a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	}
1765a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
1766a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	/* set regulator constraints */
1767a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	ret = set_machine_constraints(rdev, &init_data->constraints);
17684fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	if (ret < 0)
17694fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell		goto clean;
1770a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
1771a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	/* register with sysfs */
1772414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	rdev->dev.class = &regulator_class;
1773a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	rdev->dev.parent = dev;
1774812460a927c1d0dc1fbdbec9aa07de1b04043d83Kay Sievers	dev_set_name(&rdev->dev, "regulator.%d",
1775812460a927c1d0dc1fbdbec9aa07de1b04043d83Kay Sievers		     atomic_inc_return(&regulator_no) - 1);
1776a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	ret = device_register(&rdev->dev);
17774fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	if (ret != 0)
17784fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell		goto clean;
1779a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
1780a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	dev_set_drvdata(&rdev->dev, rdev);
1781a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
1782a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	/* set supply regulator if it exists */
1783a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	if (init_data->supply_regulator_dev) {
1784a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		ret = set_supply(rdev,
1785a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			dev_get_drvdata(init_data->supply_regulator_dev));
17864fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell		if (ret < 0)
17874fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell			goto scrub;
1788a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	}
1789a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
1790a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	/* add consumers devices */
1791a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	for (i = 0; i < init_data->num_consumer_supplies; i++) {
1792a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		ret = set_consumer_device_supply(rdev,
1793a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			init_data->consumer_supplies[i].dev,
1794a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			init_data->consumer_supplies[i].supply);
1795a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		if (ret < 0) {
1796a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood			for (--i; i >= 0; i--)
1797a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood				unset_consumer_device_supply(rdev,
1798a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood					init_data->consumer_supplies[i].dev);
17994fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell			goto scrub;
1800a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood		}
1801414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1802a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
1803a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	list_add(&rdev->list, &regulator_list);
1804a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodout:
1805414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&regulator_list_mutex);
1806414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return rdev;
18074fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell
18084fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownellscrub:
18094fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	device_unregister(&rdev->dev);
18104fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownellclean:
18114fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	kfree(rdev);
18124fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	rdev = ERR_PTR(ret);
18134fca9545d17b99cdb2774716b034c62a70151bcdDavid Brownell	goto out;
1814414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1815414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_register);
1816414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1817414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1818414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_unregister - unregister regulator
1819414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator source
1820414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1821414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Called by regulator drivers to unregister a regulator.
1822414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1823414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodvoid regulator_unregister(struct regulator_dev *rdev)
1824414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1825414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev == NULL)
1826414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return;
1827414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1828414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&regulator_list_mutex);
1829414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_del(&rdev->list);
1830414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (rdev->supply)
1831414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		sysfs_remove_link(&rdev->dev.kobj, "supply");
1832414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	device_unregister(&rdev->dev);
1833414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&regulator_list_mutex);
1834414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1835414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_unregister);
1836414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1837414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1838414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_suspend_prepare: prepare regulators for system wide suspend
1839414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @state: system suspend state
1840414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1841414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Configure each regulator with it's suspend operating parameters for state.
1842414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * This will usually be called by machine suspend code prior to supending.
1843414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1844414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint regulator_suspend_prepare(suspend_state_t state)
1845414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1846414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	struct regulator_dev *rdev;
1847414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	int ret = 0;
1848414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1849414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	/* ON is handled by regulator active state */
1850414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	if (state == PM_SUSPEND_ON)
1851414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		return -EINVAL;
1852414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1853414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_lock(&regulator_list_mutex);
1854414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	list_for_each_entry(rdev, &regulator_list, list) {
1855414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1856414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		mutex_lock(&rdev->mutex);
1857414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		ret = suspend_prepare(rdev, state);
1858414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		mutex_unlock(&rdev->mutex);
1859414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1860414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		if (ret < 0) {
1861414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			printk(KERN_ERR "%s: failed to prepare %s\n",
1862414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood				__func__, rdev->desc->name);
1863414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood			goto out;
1864414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood		}
1865414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	}
1866414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodout:
1867414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	mutex_unlock(&regulator_list_mutex);
1868414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return ret;
1869414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1870414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1871414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1872414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1873414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * rdev_get_drvdata - get rdev regulator driver data
1874414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator
1875414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1876414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Get rdev regulator driver private data. This call can be used in the
1877414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator driver context.
1878414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1879414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodvoid *rdev_get_drvdata(struct regulator_dev *rdev)
1880414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1881414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return rdev->reg_data;
1882414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1883414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(rdev_get_drvdata);
1884414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1885414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1886414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_get_drvdata - get regulator driver data
1887414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator
1888414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood *
1889414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * Get regulator driver private data. This call can be used in the consumer
1890414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * driver context when non API regulator specific functions need to be called.
1891414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1892414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodvoid *regulator_get_drvdata(struct regulator *regulator)
1893414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1894414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return regulator->rdev->reg_data;
1895414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1896414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_get_drvdata);
1897414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1898414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1899414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_set_drvdata - set regulator driver data
1900414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator
1901414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @data: data
1902414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1903414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodvoid regulator_set_drvdata(struct regulator *regulator, void *data)
1904414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1905414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	regulator->rdev->reg_data = data;
1906414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1907414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_set_drvdata);
1908414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1909414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/**
1910414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * regulator_get_id - get regulator ID
1911414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood * @regulator: regulator
1912414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood */
1913414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodint rdev_get_id(struct regulator_dev *rdev)
1914414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1915414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return rdev->desc->id;
1916414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1917414c70cb91c445ec813b61e16fe4882807e40240Liam GirdwoodEXPORT_SYMBOL_GPL(rdev_get_id);
1918414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1919a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodstruct device *rdev_get_dev(struct regulator_dev *rdev)
1920a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood{
1921a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	return &rdev->dev;
1922a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood}
1923a5766f11cfd3a0c03450d99c8fe548c2940be884Liam GirdwoodEXPORT_SYMBOL_GPL(rdev_get_dev);
1924a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
1925a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwoodvoid *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
1926a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood{
1927a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood	return reg_init_data->driver_data;
1928a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood}
1929a5766f11cfd3a0c03450d99c8fe548c2940be884Liam GirdwoodEXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
1930a5766f11cfd3a0c03450d99c8fe548c2940be884Liam Girdwood
1931414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodstatic int __init regulator_init(void)
1932414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood{
1933414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
1934414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood	return class_register(&regulator_class);
1935414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood}
1936414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood
1937414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwood/* init early to allow our consumers to complete system booting */
1938414c70cb91c445ec813b61e16fe4882807e40240Liam Girdwoodcore_initcall(regulator_init);
1939