Add linux-next specific files for 20110801
[linux-2.6/next.git] / drivers / regulator / virtual.c
blob858c1f861ba596d134c253d74ba7d06d330ea0ff
1 /*
2 * reg-virtual-consumer.c
4 * Copyright 2008 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
14 #include <linux/err.h>
15 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
21 struct virtual_consumer_data {
22 struct mutex lock;
23 struct regulator *regulator;
24 bool enabled;
25 int min_uV;
26 int max_uV;
27 int min_uA;
28 int max_uA;
29 unsigned int mode;
32 static void update_voltage_constraints(struct device *dev,
33 struct virtual_consumer_data *data)
35 int ret;
37 if (data->min_uV && data->max_uV
38 && data->min_uV <= data->max_uV) {
39 dev_dbg(dev, "Requesting %d-%duV\n",
40 data->min_uV, data->max_uV);
41 ret = regulator_set_voltage(data->regulator,
42 data->min_uV, data->max_uV);
43 if (ret != 0) {
44 dev_err(dev,
45 "regulator_set_voltage() failed: %d\n", ret);
46 return;
50 if (data->min_uV && data->max_uV && !data->enabled) {
51 dev_dbg(dev, "Enabling regulator\n");
52 ret = regulator_enable(data->regulator);
53 if (ret == 0)
54 data->enabled = true;
55 else
56 dev_err(dev, "regulator_enable() failed: %d\n",
57 ret);
60 if (!(data->min_uV && data->max_uV) && data->enabled) {
61 dev_dbg(dev, "Disabling regulator\n");
62 ret = regulator_disable(data->regulator);
63 if (ret == 0)
64 data->enabled = false;
65 else
66 dev_err(dev, "regulator_disable() failed: %d\n",
67 ret);
71 static void update_current_limit_constraints(struct device *dev,
72 struct virtual_consumer_data *data)
74 int ret;
76 if (data->max_uA
77 && data->min_uA <= data->max_uA) {
78 dev_dbg(dev, "Requesting %d-%duA\n",
79 data->min_uA, data->max_uA);
80 ret = regulator_set_current_limit(data->regulator,
81 data->min_uA, data->max_uA);
82 if (ret != 0) {
83 dev_err(dev,
84 "regulator_set_current_limit() failed: %d\n",
85 ret);
86 return;
90 if (data->max_uA && !data->enabled) {
91 dev_dbg(dev, "Enabling regulator\n");
92 ret = regulator_enable(data->regulator);
93 if (ret == 0)
94 data->enabled = true;
95 else
96 dev_err(dev, "regulator_enable() failed: %d\n",
97 ret);
100 if (!(data->min_uA && data->max_uA) && data->enabled) {
101 dev_dbg(dev, "Disabling regulator\n");
102 ret = regulator_disable(data->regulator);
103 if (ret == 0)
104 data->enabled = false;
105 else
106 dev_err(dev, "regulator_disable() failed: %d\n",
107 ret);
111 static ssize_t show_min_uV(struct device *dev,
112 struct device_attribute *attr, char *buf)
114 struct virtual_consumer_data *data = dev_get_drvdata(dev);
115 return sprintf(buf, "%d\n", data->min_uV);
118 static ssize_t set_min_uV(struct device *dev, struct device_attribute *attr,
119 const char *buf, size_t count)
121 struct virtual_consumer_data *data = dev_get_drvdata(dev);
122 long val;
124 if (strict_strtol(buf, 10, &val) != 0)
125 return count;
127 mutex_lock(&data->lock);
129 data->min_uV = val;
130 update_voltage_constraints(dev, data);
132 mutex_unlock(&data->lock);
134 return count;
137 static ssize_t show_max_uV(struct device *dev,
138 struct device_attribute *attr, char *buf)
140 struct virtual_consumer_data *data = dev_get_drvdata(dev);
141 return sprintf(buf, "%d\n", data->max_uV);
144 static ssize_t set_max_uV(struct device *dev, struct device_attribute *attr,
145 const char *buf, size_t count)
147 struct virtual_consumer_data *data = dev_get_drvdata(dev);
148 long val;
150 if (strict_strtol(buf, 10, &val) != 0)
151 return count;
153 mutex_lock(&data->lock);
155 data->max_uV = val;
156 update_voltage_constraints(dev, data);
158 mutex_unlock(&data->lock);
160 return count;
163 static ssize_t show_min_uA(struct device *dev,
164 struct device_attribute *attr, char *buf)
166 struct virtual_consumer_data *data = dev_get_drvdata(dev);
167 return sprintf(buf, "%d\n", data->min_uA);
170 static ssize_t set_min_uA(struct device *dev, struct device_attribute *attr,
171 const char *buf, size_t count)
173 struct virtual_consumer_data *data = dev_get_drvdata(dev);
174 long val;
176 if (strict_strtol(buf, 10, &val) != 0)
177 return count;
179 mutex_lock(&data->lock);
181 data->min_uA = val;
182 update_current_limit_constraints(dev, data);
184 mutex_unlock(&data->lock);
186 return count;
189 static ssize_t show_max_uA(struct device *dev,
190 struct device_attribute *attr, char *buf)
192 struct virtual_consumer_data *data = dev_get_drvdata(dev);
193 return sprintf(buf, "%d\n", data->max_uA);
196 static ssize_t set_max_uA(struct device *dev, struct device_attribute *attr,
197 const char *buf, size_t count)
199 struct virtual_consumer_data *data = dev_get_drvdata(dev);
200 long val;
202 if (strict_strtol(buf, 10, &val) != 0)
203 return count;
205 mutex_lock(&data->lock);
207 data->max_uA = val;
208 update_current_limit_constraints(dev, data);
210 mutex_unlock(&data->lock);
212 return count;
215 static ssize_t show_mode(struct device *dev,
216 struct device_attribute *attr, char *buf)
218 struct virtual_consumer_data *data = dev_get_drvdata(dev);
220 switch (data->mode) {
221 case REGULATOR_MODE_FAST:
222 return sprintf(buf, "fast\n");
223 case REGULATOR_MODE_NORMAL:
224 return sprintf(buf, "normal\n");
225 case REGULATOR_MODE_IDLE:
226 return sprintf(buf, "idle\n");
227 case REGULATOR_MODE_STANDBY:
228 return sprintf(buf, "standby\n");
229 default:
230 return sprintf(buf, "unknown\n");
234 static ssize_t set_mode(struct device *dev, struct device_attribute *attr,
235 const char *buf, size_t count)
237 struct virtual_consumer_data *data = dev_get_drvdata(dev);
238 unsigned int mode;
239 int ret;
242 * sysfs_streq() doesn't need the \n's, but we add them so the strings
243 * will be shared with show_mode(), above.
245 if (sysfs_streq(buf, "fast\n"))
246 mode = REGULATOR_MODE_FAST;
247 else if (sysfs_streq(buf, "normal\n"))
248 mode = REGULATOR_MODE_NORMAL;
249 else if (sysfs_streq(buf, "idle\n"))
250 mode = REGULATOR_MODE_IDLE;
251 else if (sysfs_streq(buf, "standby\n"))
252 mode = REGULATOR_MODE_STANDBY;
253 else {
254 dev_err(dev, "Configuring invalid mode\n");
255 return count;
258 mutex_lock(&data->lock);
259 ret = regulator_set_mode(data->regulator, mode);
260 if (ret == 0)
261 data->mode = mode;
262 else
263 dev_err(dev, "Failed to configure mode: %d\n", ret);
264 mutex_unlock(&data->lock);
266 return count;
269 static DEVICE_ATTR(min_microvolts, 0666, show_min_uV, set_min_uV);
270 static DEVICE_ATTR(max_microvolts, 0666, show_max_uV, set_max_uV);
271 static DEVICE_ATTR(min_microamps, 0666, show_min_uA, set_min_uA);
272 static DEVICE_ATTR(max_microamps, 0666, show_max_uA, set_max_uA);
273 static DEVICE_ATTR(mode, 0666, show_mode, set_mode);
275 static struct attribute *regulator_virtual_attributes[] = {
276 &dev_attr_min_microvolts.attr,
277 &dev_attr_max_microvolts.attr,
278 &dev_attr_min_microamps.attr,
279 &dev_attr_max_microamps.attr,
280 &dev_attr_mode.attr,
281 NULL
284 static const struct attribute_group regulator_virtual_attr_group = {
285 .attrs = regulator_virtual_attributes,
288 static int __devinit regulator_virtual_probe(struct platform_device *pdev)
290 char *reg_id = pdev->dev.platform_data;
291 struct virtual_consumer_data *drvdata;
292 int ret;
294 drvdata = kzalloc(sizeof(struct virtual_consumer_data), GFP_KERNEL);
295 if (drvdata == NULL)
296 return -ENOMEM;
298 mutex_init(&drvdata->lock);
300 drvdata->regulator = regulator_get(&pdev->dev, reg_id);
301 if (IS_ERR(drvdata->regulator)) {
302 ret = PTR_ERR(drvdata->regulator);
303 dev_err(&pdev->dev, "Failed to obtain supply '%s': %d\n",
304 reg_id, ret);
305 goto err;
308 ret = sysfs_create_group(&pdev->dev.kobj,
309 &regulator_virtual_attr_group);
310 if (ret != 0) {
311 dev_err(&pdev->dev,
312 "Failed to create attribute group: %d\n", ret);
313 goto err_regulator;
316 drvdata->mode = regulator_get_mode(drvdata->regulator);
318 platform_set_drvdata(pdev, drvdata);
320 return 0;
322 err_regulator:
323 regulator_put(drvdata->regulator);
324 err:
325 kfree(drvdata);
326 return ret;
329 static int __devexit regulator_virtual_remove(struct platform_device *pdev)
331 struct virtual_consumer_data *drvdata = platform_get_drvdata(pdev);
333 sysfs_remove_group(&pdev->dev.kobj, &regulator_virtual_attr_group);
335 if (drvdata->enabled)
336 regulator_disable(drvdata->regulator);
337 regulator_put(drvdata->regulator);
339 kfree(drvdata);
341 platform_set_drvdata(pdev, NULL);
343 return 0;
346 static struct platform_driver regulator_virtual_consumer_driver = {
347 .probe = regulator_virtual_probe,
348 .remove = __devexit_p(regulator_virtual_remove),
349 .driver = {
350 .name = "reg-virt-consumer",
351 .owner = THIS_MODULE,
355 static int __init regulator_virtual_consumer_init(void)
357 return platform_driver_register(&regulator_virtual_consumer_driver);
359 module_init(regulator_virtual_consumer_init);
361 static void __exit regulator_virtual_consumer_exit(void)
363 platform_driver_unregister(&regulator_virtual_consumer_driver);
365 module_exit(regulator_virtual_consumer_exit);
367 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
368 MODULE_DESCRIPTION("Virtual regulator consumer");
369 MODULE_LICENSE("GPL");
370 MODULE_ALIAS("platform:reg-virt-consumer");