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>
20 struct virtual_consumer_data
{
22 struct regulator
*regulator
;
31 static void update_voltage_constraints(struct device
*dev
,
32 struct virtual_consumer_data
*data
)
36 if (data
->min_uV
&& data
->max_uV
37 && data
->min_uV
<= data
->max_uV
) {
38 dev_dbg(dev
, "Requesting %d-%duV\n",
39 data
->min_uV
, data
->max_uV
);
40 ret
= regulator_set_voltage(data
->regulator
,
41 data
->min_uV
, data
->max_uV
);
44 "regulator_set_voltage() failed: %d\n", ret
);
49 if (data
->min_uV
&& data
->max_uV
&& !data
->enabled
) {
50 dev_dbg(dev
, "Enabling regulator\n");
51 ret
= regulator_enable(data
->regulator
);
55 dev_err(dev
, "regulator_enable() failed: %d\n",
59 if (!(data
->min_uV
&& data
->max_uV
) && data
->enabled
) {
60 dev_dbg(dev
, "Disabling regulator\n");
61 ret
= regulator_disable(data
->regulator
);
63 data
->enabled
= false;
65 dev_err(dev
, "regulator_disable() failed: %d\n",
70 static void update_current_limit_constraints(struct device
*dev
,
71 struct virtual_consumer_data
*data
)
76 && data
->min_uA
<= data
->max_uA
) {
77 dev_dbg(dev
, "Requesting %d-%duA\n",
78 data
->min_uA
, data
->max_uA
);
79 ret
= regulator_set_current_limit(data
->regulator
,
80 data
->min_uA
, data
->max_uA
);
83 "regulator_set_current_limit() failed: %d\n",
89 if (data
->max_uA
&& !data
->enabled
) {
90 dev_dbg(dev
, "Enabling regulator\n");
91 ret
= regulator_enable(data
->regulator
);
95 dev_err(dev
, "regulator_enable() failed: %d\n",
99 if (!(data
->min_uA
&& data
->max_uA
) && data
->enabled
) {
100 dev_dbg(dev
, "Disabling regulator\n");
101 ret
= regulator_disable(data
->regulator
);
103 data
->enabled
= false;
105 dev_err(dev
, "regulator_disable() failed: %d\n",
110 static ssize_t
show_min_uV(struct device
*dev
,
111 struct device_attribute
*attr
, char *buf
)
113 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
114 return sprintf(buf
, "%d\n", data
->min_uV
);
117 static ssize_t
set_min_uV(struct device
*dev
, struct device_attribute
*attr
,
118 const char *buf
, size_t count
)
120 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
123 if (strict_strtol(buf
, 10, &val
) != 0)
126 mutex_lock(&data
->lock
);
129 update_voltage_constraints(dev
, data
);
131 mutex_unlock(&data
->lock
);
136 static ssize_t
show_max_uV(struct device
*dev
,
137 struct device_attribute
*attr
, char *buf
)
139 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
140 return sprintf(buf
, "%d\n", data
->max_uV
);
143 static ssize_t
set_max_uV(struct device
*dev
, struct device_attribute
*attr
,
144 const char *buf
, size_t count
)
146 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
149 if (strict_strtol(buf
, 10, &val
) != 0)
152 mutex_lock(&data
->lock
);
155 update_voltage_constraints(dev
, data
);
157 mutex_unlock(&data
->lock
);
162 static ssize_t
show_min_uA(struct device
*dev
,
163 struct device_attribute
*attr
, char *buf
)
165 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
166 return sprintf(buf
, "%d\n", data
->min_uA
);
169 static ssize_t
set_min_uA(struct device
*dev
, struct device_attribute
*attr
,
170 const char *buf
, size_t count
)
172 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
175 if (strict_strtol(buf
, 10, &val
) != 0)
178 mutex_lock(&data
->lock
);
181 update_current_limit_constraints(dev
, data
);
183 mutex_unlock(&data
->lock
);
188 static ssize_t
show_max_uA(struct device
*dev
,
189 struct device_attribute
*attr
, char *buf
)
191 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
192 return sprintf(buf
, "%d\n", data
->max_uA
);
195 static ssize_t
set_max_uA(struct device
*dev
, struct device_attribute
*attr
,
196 const char *buf
, size_t count
)
198 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
201 if (strict_strtol(buf
, 10, &val
) != 0)
204 mutex_lock(&data
->lock
);
207 update_current_limit_constraints(dev
, data
);
209 mutex_unlock(&data
->lock
);
214 static ssize_t
show_mode(struct device
*dev
,
215 struct device_attribute
*attr
, char *buf
)
217 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
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");
229 return sprintf(buf
, "unknown\n");
233 static ssize_t
set_mode(struct device
*dev
, struct device_attribute
*attr
,
234 const char *buf
, size_t count
)
236 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
241 * sysfs_streq() doesn't need the \n's, but we add them so the strings
242 * will be shared with show_mode(), above.
244 if (sysfs_streq(buf
, "fast\n"))
245 mode
= REGULATOR_MODE_FAST
;
246 else if (sysfs_streq(buf
, "normal\n"))
247 mode
= REGULATOR_MODE_NORMAL
;
248 else if (sysfs_streq(buf
, "idle\n"))
249 mode
= REGULATOR_MODE_IDLE
;
250 else if (sysfs_streq(buf
, "standby\n"))
251 mode
= REGULATOR_MODE_STANDBY
;
253 dev_err(dev
, "Configuring invalid mode\n");
257 mutex_lock(&data
->lock
);
258 ret
= regulator_set_mode(data
->regulator
, mode
);
262 dev_err(dev
, "Failed to configure mode: %d\n", ret
);
263 mutex_unlock(&data
->lock
);
268 static DEVICE_ATTR(min_microvolts
, 0666, show_min_uV
, set_min_uV
);
269 static DEVICE_ATTR(max_microvolts
, 0666, show_max_uV
, set_max_uV
);
270 static DEVICE_ATTR(min_microamps
, 0666, show_min_uA
, set_min_uA
);
271 static DEVICE_ATTR(max_microamps
, 0666, show_max_uA
, set_max_uA
);
272 static DEVICE_ATTR(mode
, 0666, show_mode
, set_mode
);
274 static 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
,
283 static const struct attribute_group regulator_virtual_attr_group
= {
284 .attrs
= regulator_virtual_attributes
,
287 static int __devinit
regulator_virtual_probe(struct platform_device
*pdev
)
289 char *reg_id
= pdev
->dev
.platform_data
;
290 struct virtual_consumer_data
*drvdata
;
293 drvdata
= kzalloc(sizeof(struct virtual_consumer_data
), GFP_KERNEL
);
297 mutex_init(&drvdata
->lock
);
299 drvdata
->regulator
= regulator_get(&pdev
->dev
, reg_id
);
300 if (IS_ERR(drvdata
->regulator
)) {
301 ret
= PTR_ERR(drvdata
->regulator
);
302 dev_err(&pdev
->dev
, "Failed to obtain supply '%s': %d\n",
307 ret
= sysfs_create_group(&pdev
->dev
.kobj
,
308 ®ulator_virtual_attr_group
);
311 "Failed to create attribute group: %d\n", ret
);
315 drvdata
->mode
= regulator_get_mode(drvdata
->regulator
);
317 platform_set_drvdata(pdev
, drvdata
);
322 regulator_put(drvdata
->regulator
);
328 static int __devexit
regulator_virtual_remove(struct platform_device
*pdev
)
330 struct virtual_consumer_data
*drvdata
= platform_get_drvdata(pdev
);
332 sysfs_remove_group(&pdev
->dev
.kobj
, ®ulator_virtual_attr_group
);
334 if (drvdata
->enabled
)
335 regulator_disable(drvdata
->regulator
);
336 regulator_put(drvdata
->regulator
);
340 platform_set_drvdata(pdev
, NULL
);
345 static struct platform_driver regulator_virtual_consumer_driver
= {
346 .probe
= regulator_virtual_probe
,
347 .remove
= __devexit_p(regulator_virtual_remove
),
349 .name
= "reg-virt-consumer",
350 .owner
= THIS_MODULE
,
354 static int __init
regulator_virtual_consumer_init(void)
356 return platform_driver_register(®ulator_virtual_consumer_driver
);
358 module_init(regulator_virtual_consumer_init
);
360 static void __exit
regulator_virtual_consumer_exit(void)
362 platform_driver_unregister(®ulator_virtual_consumer_driver
);
364 module_exit(regulator_virtual_consumer_exit
);
366 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
367 MODULE_DESCRIPTION("Virtual regulator consumer");
368 MODULE_LICENSE("GPL");
369 MODULE_ALIAS("platform:reg-virt-consumer");