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>
19 struct virtual_consumer_data
{
21 struct regulator
*regulator
;
30 static void update_voltage_constraints(struct device
*dev
,
31 struct virtual_consumer_data
*data
)
35 if (data
->min_uV
&& data
->max_uV
36 && data
->min_uV
<= data
->max_uV
) {
37 dev_dbg(dev
, "Requesting %d-%duV\n",
38 data
->min_uV
, data
->max_uV
);
39 ret
= regulator_set_voltage(data
->regulator
,
40 data
->min_uV
, data
->max_uV
);
43 "regulator_set_voltage() failed: %d\n", ret
);
48 if (data
->min_uV
&& data
->max_uV
&& !data
->enabled
) {
49 dev_dbg(dev
, "Enabling regulator\n");
50 ret
= regulator_enable(data
->regulator
);
54 dev_err(dev
, "regulator_enable() failed: %d\n",
58 if (!(data
->min_uV
&& data
->max_uV
) && data
->enabled
) {
59 dev_dbg(dev
, "Disabling regulator\n");
60 ret
= regulator_disable(data
->regulator
);
64 dev_err(dev
, "regulator_disable() failed: %d\n",
69 static void update_current_limit_constraints(struct device
*dev
,
70 struct virtual_consumer_data
*data
)
75 && data
->min_uA
<= data
->max_uA
) {
76 dev_dbg(dev
, "Requesting %d-%duA\n",
77 data
->min_uA
, data
->max_uA
);
78 ret
= regulator_set_current_limit(data
->regulator
,
79 data
->min_uA
, data
->max_uA
);
82 "regulator_set_current_limit() failed: %d\n",
88 if (data
->max_uA
&& !data
->enabled
) {
89 dev_dbg(dev
, "Enabling regulator\n");
90 ret
= regulator_enable(data
->regulator
);
94 dev_err(dev
, "regulator_enable() failed: %d\n",
98 if (!(data
->min_uA
&& data
->max_uA
) && data
->enabled
) {
99 dev_dbg(dev
, "Disabling regulator\n");
100 ret
= regulator_disable(data
->regulator
);
104 dev_err(dev
, "regulator_disable() failed: %d\n",
109 static ssize_t
show_min_uV(struct device
*dev
,
110 struct device_attribute
*attr
, char *buf
)
112 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
113 return sprintf(buf
, "%d\n", data
->min_uV
);
116 static ssize_t
set_min_uV(struct device
*dev
, struct device_attribute
*attr
,
117 const char *buf
, size_t count
)
119 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
122 if (strict_strtol(buf
, 10, &val
) != 0)
125 mutex_lock(&data
->lock
);
128 update_voltage_constraints(dev
, data
);
130 mutex_unlock(&data
->lock
);
135 static ssize_t
show_max_uV(struct device
*dev
,
136 struct device_attribute
*attr
, char *buf
)
138 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
139 return sprintf(buf
, "%d\n", data
->max_uV
);
142 static ssize_t
set_max_uV(struct device
*dev
, struct device_attribute
*attr
,
143 const char *buf
, size_t count
)
145 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
148 if (strict_strtol(buf
, 10, &val
) != 0)
151 mutex_lock(&data
->lock
);
154 update_voltage_constraints(dev
, data
);
156 mutex_unlock(&data
->lock
);
161 static ssize_t
show_min_uA(struct device
*dev
,
162 struct device_attribute
*attr
, char *buf
)
164 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
165 return sprintf(buf
, "%d\n", data
->min_uA
);
168 static ssize_t
set_min_uA(struct device
*dev
, struct device_attribute
*attr
,
169 const char *buf
, size_t count
)
171 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
174 if (strict_strtol(buf
, 10, &val
) != 0)
177 mutex_lock(&data
->lock
);
180 update_current_limit_constraints(dev
, data
);
182 mutex_unlock(&data
->lock
);
187 static ssize_t
show_max_uA(struct device
*dev
,
188 struct device_attribute
*attr
, char *buf
)
190 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
191 return sprintf(buf
, "%d\n", data
->max_uA
);
194 static ssize_t
set_max_uA(struct device
*dev
, struct device_attribute
*attr
,
195 const char *buf
, size_t count
)
197 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
200 if (strict_strtol(buf
, 10, &val
) != 0)
203 mutex_lock(&data
->lock
);
206 update_current_limit_constraints(dev
, data
);
208 mutex_unlock(&data
->lock
);
213 static ssize_t
show_mode(struct device
*dev
,
214 struct device_attribute
*attr
, char *buf
)
216 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
218 switch (data
->mode
) {
219 case REGULATOR_MODE_FAST
:
220 return sprintf(buf
, "fast\n");
221 case REGULATOR_MODE_NORMAL
:
222 return sprintf(buf
, "normal\n");
223 case REGULATOR_MODE_IDLE
:
224 return sprintf(buf
, "idle\n");
225 case REGULATOR_MODE_STANDBY
:
226 return sprintf(buf
, "standby\n");
228 return sprintf(buf
, "unknown\n");
232 static ssize_t
set_mode(struct device
*dev
, struct device_attribute
*attr
,
233 const char *buf
, size_t count
)
235 struct virtual_consumer_data
*data
= dev_get_drvdata(dev
);
240 * sysfs_streq() doesn't need the \n's, but we add them so the strings
241 * will be shared with show_mode(), above.
243 if (sysfs_streq(buf
, "fast\n"))
244 mode
= REGULATOR_MODE_FAST
;
245 else if (sysfs_streq(buf
, "normal\n"))
246 mode
= REGULATOR_MODE_NORMAL
;
247 else if (sysfs_streq(buf
, "idle\n"))
248 mode
= REGULATOR_MODE_IDLE
;
249 else if (sysfs_streq(buf
, "standby\n"))
250 mode
= REGULATOR_MODE_STANDBY
;
252 dev_err(dev
, "Configuring invalid mode\n");
256 mutex_lock(&data
->lock
);
257 ret
= regulator_set_mode(data
->regulator
, mode
);
261 dev_err(dev
, "Failed to configure mode: %d\n", ret
);
262 mutex_unlock(&data
->lock
);
267 static DEVICE_ATTR(min_microvolts
, 0666, show_min_uV
, set_min_uV
);
268 static DEVICE_ATTR(max_microvolts
, 0666, show_max_uV
, set_max_uV
);
269 static DEVICE_ATTR(min_microamps
, 0666, show_min_uA
, set_min_uA
);
270 static DEVICE_ATTR(max_microamps
, 0666, show_max_uA
, set_max_uA
);
271 static DEVICE_ATTR(mode
, 0666, show_mode
, set_mode
);
273 static struct device_attribute
*attributes
[] = {
274 &dev_attr_min_microvolts
,
275 &dev_attr_max_microvolts
,
276 &dev_attr_min_microamps
,
277 &dev_attr_max_microamps
,
281 static int regulator_virtual_consumer_probe(struct platform_device
*pdev
)
283 char *reg_id
= pdev
->dev
.platform_data
;
284 struct virtual_consumer_data
*drvdata
;
287 drvdata
= kzalloc(sizeof(struct virtual_consumer_data
), GFP_KERNEL
);
288 if (drvdata
== NULL
) {
292 mutex_init(&drvdata
->lock
);
294 drvdata
->regulator
= regulator_get(&pdev
->dev
, reg_id
);
295 if (IS_ERR(drvdata
->regulator
)) {
296 ret
= PTR_ERR(drvdata
->regulator
);
297 dev_err(&pdev
->dev
, "Failed to obtain supply '%s': %d\n",
302 for (i
= 0; i
< ARRAY_SIZE(attributes
); i
++) {
303 ret
= device_create_file(&pdev
->dev
, attributes
[i
]);
305 dev_err(&pdev
->dev
, "Failed to create attr %d: %d\n",
311 drvdata
->mode
= regulator_get_mode(drvdata
->regulator
);
313 platform_set_drvdata(pdev
, drvdata
);
318 regulator_put(drvdata
->regulator
);
320 for (i
= 0; i
< ARRAY_SIZE(attributes
); i
++)
321 device_remove_file(&pdev
->dev
, attributes
[i
]);
326 static int regulator_virtual_consumer_remove(struct platform_device
*pdev
)
328 struct virtual_consumer_data
*drvdata
= platform_get_drvdata(pdev
);
331 for (i
= 0; i
< ARRAY_SIZE(attributes
); i
++)
332 device_remove_file(&pdev
->dev
, attributes
[i
]);
333 if (drvdata
->enabled
)
334 regulator_disable(drvdata
->regulator
);
335 regulator_put(drvdata
->regulator
);
342 static struct platform_driver regulator_virtual_consumer_driver
= {
343 .probe
= regulator_virtual_consumer_probe
,
344 .remove
= regulator_virtual_consumer_remove
,
346 .name
= "reg-virt-consumer",
351 static int __init
regulator_virtual_consumer_init(void)
353 return platform_driver_register(®ulator_virtual_consumer_driver
);
355 module_init(regulator_virtual_consumer_init
);
357 static void __exit
regulator_virtual_consumer_exit(void)
359 platform_driver_unregister(®ulator_virtual_consumer_driver
);
361 module_exit(regulator_virtual_consumer_exit
);
363 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
364 MODULE_DESCRIPTION("Virtual regulator consumer");
365 MODULE_LICENSE("GPL");
366 MODULE_ALIAS("platform:reg-virt-consumer");