blob: 69e550f57638cbde4ebde30990318cb21816ad0a [file] [log] [blame]
Mark Brownc0809092008-04-30 17:05:33 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Mark Brownc0809092008-04-30 17:05:33 +010019
20struct virtual_consumer_data {
21 struct mutex lock;
22 struct regulator *regulator;
Dmitry Torokhov4cf95662010-02-23 23:37:44 -080023 bool enabled;
Mark Brownc0809092008-04-30 17:05:33 +010024 int min_uV;
25 int max_uV;
26 int min_uA;
27 int max_uA;
28 unsigned int mode;
29};
30
Mark Browna07ac212009-06-17 15:45:06 +010031static void update_voltage_constraints(struct device *dev,
32 struct virtual_consumer_data *data)
Mark Brownc0809092008-04-30 17:05:33 +010033{
34 int ret;
35
36 if (data->min_uV && data->max_uV
37 && data->min_uV <= data->max_uV) {
Mark Browna5d2abc2009-06-17 15:45:07 +010038 dev_dbg(dev, "Requesting %d-%duV\n",
39 data->min_uV, data->max_uV);
Mark Brownc0809092008-04-30 17:05:33 +010040 ret = regulator_set_voltage(data->regulator,
Mark Browna07ac212009-06-17 15:45:06 +010041 data->min_uV, data->max_uV);
Mark Brownc0809092008-04-30 17:05:33 +010042 if (ret != 0) {
Mark Browna07ac212009-06-17 15:45:06 +010043 dev_err(dev,
44 "regulator_set_voltage() failed: %d\n", ret);
Mark Brownc0809092008-04-30 17:05:33 +010045 return;
46 }
47 }
48
49 if (data->min_uV && data->max_uV && !data->enabled) {
Mark Browna5d2abc2009-06-17 15:45:07 +010050 dev_dbg(dev, "Enabling regulator\n");
Mark Brownc0809092008-04-30 17:05:33 +010051 ret = regulator_enable(data->regulator);
52 if (ret == 0)
Dmitry Torokhov4cf95662010-02-23 23:37:44 -080053 data->enabled = true;
Mark Brownc0809092008-04-30 17:05:33 +010054 else
Mark Browna07ac212009-06-17 15:45:06 +010055 dev_err(dev, "regulator_enable() failed: %d\n",
Mark Brownc0809092008-04-30 17:05:33 +010056 ret);
57 }
58
59 if (!(data->min_uV && data->max_uV) && data->enabled) {
Mark Browna5d2abc2009-06-17 15:45:07 +010060 dev_dbg(dev, "Disabling regulator\n");
Mark Brownc0809092008-04-30 17:05:33 +010061 ret = regulator_disable(data->regulator);
62 if (ret == 0)
Dmitry Torokhov4cf95662010-02-23 23:37:44 -080063 data->enabled = false;
Mark Brownc0809092008-04-30 17:05:33 +010064 else
Mark Browna07ac212009-06-17 15:45:06 +010065 dev_err(dev, "regulator_disable() failed: %d\n",
Mark Brownc0809092008-04-30 17:05:33 +010066 ret);
67 }
68}
69
Mark Browna07ac212009-06-17 15:45:06 +010070static void update_current_limit_constraints(struct device *dev,
71 struct virtual_consumer_data *data)
Mark Brownc0809092008-04-30 17:05:33 +010072{
73 int ret;
74
75 if (data->max_uA
76 && data->min_uA <= data->max_uA) {
Mark Browna5d2abc2009-06-17 15:45:07 +010077 dev_dbg(dev, "Requesting %d-%duA\n",
78 data->min_uA, data->max_uA);
Mark Brownc0809092008-04-30 17:05:33 +010079 ret = regulator_set_current_limit(data->regulator,
80 data->min_uA, data->max_uA);
81 if (ret != 0) {
Mark Browna07ac212009-06-17 15:45:06 +010082 dev_err(dev,
83 "regulator_set_current_limit() failed: %d\n",
84 ret);
Mark Brownc0809092008-04-30 17:05:33 +010085 return;
86 }
87 }
88
89 if (data->max_uA && !data->enabled) {
Mark Browna5d2abc2009-06-17 15:45:07 +010090 dev_dbg(dev, "Enabling regulator\n");
Mark Brownc0809092008-04-30 17:05:33 +010091 ret = regulator_enable(data->regulator);
92 if (ret == 0)
Dmitry Torokhov4cf95662010-02-23 23:37:44 -080093 data->enabled = true;
Mark Brownc0809092008-04-30 17:05:33 +010094 else
Mark Browna07ac212009-06-17 15:45:06 +010095 dev_err(dev, "regulator_enable() failed: %d\n",
Mark Brownc0809092008-04-30 17:05:33 +010096 ret);
97 }
98
99 if (!(data->min_uA && data->max_uA) && data->enabled) {
Mark Browna5d2abc2009-06-17 15:45:07 +0100100 dev_dbg(dev, "Disabling regulator\n");
Mark Brownc0809092008-04-30 17:05:33 +0100101 ret = regulator_disable(data->regulator);
102 if (ret == 0)
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800103 data->enabled = false;
Mark Brownc0809092008-04-30 17:05:33 +0100104 else
Mark Browna07ac212009-06-17 15:45:06 +0100105 dev_err(dev, "regulator_disable() failed: %d\n",
Mark Brownc0809092008-04-30 17:05:33 +0100106 ret);
107 }
108}
109
110static ssize_t show_min_uV(struct device *dev,
111 struct device_attribute *attr, char *buf)
112{
113 struct virtual_consumer_data *data = dev_get_drvdata(dev);
114 return sprintf(buf, "%d\n", data->min_uV);
115}
116
117static ssize_t set_min_uV(struct device *dev, struct device_attribute *attr,
118 const char *buf, size_t count)
119{
120 struct virtual_consumer_data *data = dev_get_drvdata(dev);
121 long val;
122
123 if (strict_strtol(buf, 10, &val) != 0)
124 return count;
125
126 mutex_lock(&data->lock);
127
128 data->min_uV = val;
Mark Browna07ac212009-06-17 15:45:06 +0100129 update_voltage_constraints(dev, data);
Mark Brownc0809092008-04-30 17:05:33 +0100130
131 mutex_unlock(&data->lock);
132
133 return count;
134}
135
136static ssize_t show_max_uV(struct device *dev,
137 struct device_attribute *attr, char *buf)
138{
139 struct virtual_consumer_data *data = dev_get_drvdata(dev);
140 return sprintf(buf, "%d\n", data->max_uV);
141}
142
143static ssize_t set_max_uV(struct device *dev, struct device_attribute *attr,
144 const char *buf, size_t count)
145{
146 struct virtual_consumer_data *data = dev_get_drvdata(dev);
147 long val;
148
149 if (strict_strtol(buf, 10, &val) != 0)
150 return count;
151
152 mutex_lock(&data->lock);
153
154 data->max_uV = val;
Mark Browna07ac212009-06-17 15:45:06 +0100155 update_voltage_constraints(dev, data);
Mark Brownc0809092008-04-30 17:05:33 +0100156
157 mutex_unlock(&data->lock);
158
159 return count;
160}
161
162static ssize_t show_min_uA(struct device *dev,
163 struct device_attribute *attr, char *buf)
164{
165 struct virtual_consumer_data *data = dev_get_drvdata(dev);
166 return sprintf(buf, "%d\n", data->min_uA);
167}
168
169static ssize_t set_min_uA(struct device *dev, struct device_attribute *attr,
170 const char *buf, size_t count)
171{
172 struct virtual_consumer_data *data = dev_get_drvdata(dev);
173 long val;
174
175 if (strict_strtol(buf, 10, &val) != 0)
176 return count;
177
178 mutex_lock(&data->lock);
179
180 data->min_uA = val;
Mark Browna07ac212009-06-17 15:45:06 +0100181 update_current_limit_constraints(dev, data);
Mark Brownc0809092008-04-30 17:05:33 +0100182
183 mutex_unlock(&data->lock);
184
185 return count;
186}
187
188static ssize_t show_max_uA(struct device *dev,
189 struct device_attribute *attr, char *buf)
190{
191 struct virtual_consumer_data *data = dev_get_drvdata(dev);
192 return sprintf(buf, "%d\n", data->max_uA);
193}
194
195static ssize_t set_max_uA(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
197{
198 struct virtual_consumer_data *data = dev_get_drvdata(dev);
199 long val;
200
201 if (strict_strtol(buf, 10, &val) != 0)
202 return count;
203
204 mutex_lock(&data->lock);
205
206 data->max_uA = val;
Mark Browna07ac212009-06-17 15:45:06 +0100207 update_current_limit_constraints(dev, data);
Mark Brownc0809092008-04-30 17:05:33 +0100208
209 mutex_unlock(&data->lock);
210
211 return count;
212}
213
214static ssize_t show_mode(struct device *dev,
215 struct device_attribute *attr, char *buf)
216{
217 struct virtual_consumer_data *data = dev_get_drvdata(dev);
218
219 switch (data->mode) {
220 case REGULATOR_MODE_FAST:
221 return sprintf(buf, "fast\n");
222 case REGULATOR_MODE_NORMAL:
223 return sprintf(buf, "normal\n");
224 case REGULATOR_MODE_IDLE:
225 return sprintf(buf, "idle\n");
226 case REGULATOR_MODE_STANDBY:
227 return sprintf(buf, "standby\n");
228 default:
229 return sprintf(buf, "unknown\n");
230 }
231}
232
233static ssize_t set_mode(struct device *dev, struct device_attribute *attr,
234 const char *buf, size_t count)
235{
236 struct virtual_consumer_data *data = dev_get_drvdata(dev);
237 unsigned int mode;
238 int ret;
239
Andrew Morton94853972009-01-15 16:13:01 -0800240 /*
241 * sysfs_streq() doesn't need the \n's, but we add them so the strings
242 * will be shared with show_mode(), above.
243 */
Mike Rapoportaa61d552009-04-26 11:49:30 +0300244 if (sysfs_streq(buf, "fast\n"))
Mark Brownc0809092008-04-30 17:05:33 +0100245 mode = REGULATOR_MODE_FAST;
Mike Rapoportaa61d552009-04-26 11:49:30 +0300246 else if (sysfs_streq(buf, "normal\n"))
Mark Brownc0809092008-04-30 17:05:33 +0100247 mode = REGULATOR_MODE_NORMAL;
Mike Rapoportaa61d552009-04-26 11:49:30 +0300248 else if (sysfs_streq(buf, "idle\n"))
Mark Brownc0809092008-04-30 17:05:33 +0100249 mode = REGULATOR_MODE_IDLE;
Mike Rapoportaa61d552009-04-26 11:49:30 +0300250 else if (sysfs_streq(buf, "standby\n"))
Mark Brownc0809092008-04-30 17:05:33 +0100251 mode = REGULATOR_MODE_STANDBY;
252 else {
253 dev_err(dev, "Configuring invalid mode\n");
254 return count;
255 }
256
257 mutex_lock(&data->lock);
258 ret = regulator_set_mode(data->regulator, mode);
259 if (ret == 0)
260 data->mode = mode;
261 else
262 dev_err(dev, "Failed to configure mode: %d\n", ret);
263 mutex_unlock(&data->lock);
264
265 return count;
266}
267
268static DEVICE_ATTR(min_microvolts, 0666, show_min_uV, set_min_uV);
269static DEVICE_ATTR(max_microvolts, 0666, show_max_uV, set_max_uV);
270static DEVICE_ATTR(min_microamps, 0666, show_min_uA, set_min_uA);
271static DEVICE_ATTR(max_microamps, 0666, show_max_uA, set_max_uA);
272static DEVICE_ATTR(mode, 0666, show_mode, set_mode);
273
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800274static struct attribute *regulator_virtual_attributes[] = {
275 &dev_attr_min_microvolts.attr,
276 &dev_attr_max_microvolts.attr,
277 &dev_attr_min_microamps.attr,
278 &dev_attr_max_microamps.attr,
279 &dev_attr_mode.attr,
280 NULL
Mark Brownc0809092008-04-30 17:05:33 +0100281};
282
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800283static const struct attribute_group regulator_virtual_attr_group = {
284 .attrs = regulator_virtual_attributes,
285};
286
287static int __devinit regulator_virtual_probe(struct platform_device *pdev)
Mark Brownc0809092008-04-30 17:05:33 +0100288{
289 char *reg_id = pdev->dev.platform_data;
290 struct virtual_consumer_data *drvdata;
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800291 int ret;
Mark Brownc0809092008-04-30 17:05:33 +0100292
293 drvdata = kzalloc(sizeof(struct virtual_consumer_data), GFP_KERNEL);
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800294 if (drvdata == NULL)
Mark Brown72b868762009-07-21 16:00:27 +0100295 return -ENOMEM;
Mark Brownc0809092008-04-30 17:05:33 +0100296
297 mutex_init(&drvdata->lock);
298
299 drvdata->regulator = regulator_get(&pdev->dev, reg_id);
300 if (IS_ERR(drvdata->regulator)) {
301 ret = PTR_ERR(drvdata->regulator);
Mark Brownd61c3d52009-06-15 20:01:01 +0100302 dev_err(&pdev->dev, "Failed to obtain supply '%s': %d\n",
303 reg_id, ret);
Mark Brownc0809092008-04-30 17:05:33 +0100304 goto err;
305 }
306
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800307 ret = sysfs_create_group(&pdev->dev.kobj,
308 &regulator_virtual_attr_group);
309 if (ret != 0) {
310 dev_err(&pdev->dev,
311 "Failed to create attribute group: %d\n", ret);
312 goto err_regulator;
Mark Brownc0809092008-04-30 17:05:33 +0100313 }
314
315 drvdata->mode = regulator_get_mode(drvdata->regulator);
316
317 platform_set_drvdata(pdev, drvdata);
318
319 return 0;
320
Mark Brown72b868762009-07-21 16:00:27 +0100321err_regulator:
322 regulator_put(drvdata->regulator);
Mark Brownc0809092008-04-30 17:05:33 +0100323err:
Mark Brownc0809092008-04-30 17:05:33 +0100324 kfree(drvdata);
325 return ret;
326}
327
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800328static int __devexit regulator_virtual_remove(struct platform_device *pdev)
Mark Brownc0809092008-04-30 17:05:33 +0100329{
330 struct virtual_consumer_data *drvdata = platform_get_drvdata(pdev);
Mark Brownc0809092008-04-30 17:05:33 +0100331
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800332 sysfs_remove_group(&pdev->dev.kobj, &regulator_virtual_attr_group);
333
Mark Brownc0809092008-04-30 17:05:33 +0100334 if (drvdata->enabled)
335 regulator_disable(drvdata->regulator);
336 regulator_put(drvdata->regulator);
337
338 kfree(drvdata);
339
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800340 platform_set_drvdata(pdev, NULL);
341
Mark Brownc0809092008-04-30 17:05:33 +0100342 return 0;
343}
344
345static struct platform_driver regulator_virtual_consumer_driver = {
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800346 .probe = regulator_virtual_probe,
347 .remove = __devexit_p(regulator_virtual_remove),
Mark Brownc0809092008-04-30 17:05:33 +0100348 .driver = {
349 .name = "reg-virt-consumer",
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800350 .owner = THIS_MODULE,
Mark Brownc0809092008-04-30 17:05:33 +0100351 },
352};
353
Mark Brownc0809092008-04-30 17:05:33 +0100354static int __init regulator_virtual_consumer_init(void)
355{
356 return platform_driver_register(&regulator_virtual_consumer_driver);
357}
358module_init(regulator_virtual_consumer_init);
359
360static void __exit regulator_virtual_consumer_exit(void)
361{
362 platform_driver_unregister(&regulator_virtual_consumer_driver);
363}
364module_exit(regulator_virtual_consumer_exit);
365
366MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
367MODULE_DESCRIPTION("Virtual regulator consumer");
368MODULE_LICENSE("GPL");
Mark Brown38c53c82009-04-28 11:13:55 +0100369MODULE_ALIAS("platform:reg-virt-consumer");