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