1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * reg-virtual-consumer.c
5 * Copyright 2008 Wolfson Microelectronics PLC.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10 #include <linux/err.h>
11 #include <linux/mutex.h>
12 #include <linux/platform_device.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
17 struct virtual_consumer_data {
19 struct regulator *regulator;
28 static void update_voltage_constraints(struct device *dev,
29 struct virtual_consumer_data *data)
33 if (data->min_uV && data->max_uV
34 && data->min_uV <= data->max_uV) {
35 dev_dbg(dev, "Requesting %d-%duV\n",
36 data->min_uV, data->max_uV);
37 ret = regulator_set_voltage(data->regulator,
38 data->min_uV, data->max_uV);
41 "regulator_set_voltage() failed: %d\n", ret);
46 if (data->min_uV && data->max_uV && !data->enabled) {
47 dev_dbg(dev, "Enabling regulator\n");
48 ret = regulator_enable(data->regulator);
52 dev_err(dev, "regulator_enable() failed: %d\n",
56 if (!(data->min_uV && data->max_uV) && data->enabled) {
57 dev_dbg(dev, "Disabling regulator\n");
58 ret = regulator_disable(data->regulator);
60 data->enabled = false;
62 dev_err(dev, "regulator_disable() failed: %d\n",
67 static void update_current_limit_constraints(struct device *dev,
68 struct virtual_consumer_data *data)
73 && data->min_uA <= data->max_uA) {
74 dev_dbg(dev, "Requesting %d-%duA\n",
75 data->min_uA, data->max_uA);
76 ret = regulator_set_current_limit(data->regulator,
77 data->min_uA, data->max_uA);
80 "regulator_set_current_limit() failed: %d\n",
86 if (data->max_uA && !data->enabled) {
87 dev_dbg(dev, "Enabling regulator\n");
88 ret = regulator_enable(data->regulator);
92 dev_err(dev, "regulator_enable() failed: %d\n",
96 if (!(data->min_uA && data->max_uA) && data->enabled) {
97 dev_dbg(dev, "Disabling regulator\n");
98 ret = regulator_disable(data->regulator);
100 data->enabled = false;
102 dev_err(dev, "regulator_disable() failed: %d\n",
107 static ssize_t show_min_uV(struct device *dev,
108 struct device_attribute *attr, char *buf)
110 struct virtual_consumer_data *data = dev_get_drvdata(dev);
111 return sprintf(buf, "%d\n", data->min_uV);
114 static ssize_t set_min_uV(struct device *dev, struct device_attribute *attr,
115 const char *buf, size_t count)
117 struct virtual_consumer_data *data = dev_get_drvdata(dev);
120 if (kstrtol(buf, 10, &val) != 0)
123 mutex_lock(&data->lock);
126 update_voltage_constraints(dev, data);
128 mutex_unlock(&data->lock);
133 static ssize_t show_max_uV(struct device *dev,
134 struct device_attribute *attr, char *buf)
136 struct virtual_consumer_data *data = dev_get_drvdata(dev);
137 return sprintf(buf, "%d\n", data->max_uV);
140 static ssize_t set_max_uV(struct device *dev, struct device_attribute *attr,
141 const char *buf, size_t count)
143 struct virtual_consumer_data *data = dev_get_drvdata(dev);
146 if (kstrtol(buf, 10, &val) != 0)
149 mutex_lock(&data->lock);
152 update_voltage_constraints(dev, data);
154 mutex_unlock(&data->lock);
159 static ssize_t show_min_uA(struct device *dev,
160 struct device_attribute *attr, char *buf)
162 struct virtual_consumer_data *data = dev_get_drvdata(dev);
163 return sprintf(buf, "%d\n", data->min_uA);
166 static ssize_t set_min_uA(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t count)
169 struct virtual_consumer_data *data = dev_get_drvdata(dev);
172 if (kstrtol(buf, 10, &val) != 0)
175 mutex_lock(&data->lock);
178 update_current_limit_constraints(dev, data);
180 mutex_unlock(&data->lock);
185 static ssize_t show_max_uA(struct device *dev,
186 struct device_attribute *attr, char *buf)
188 struct virtual_consumer_data *data = dev_get_drvdata(dev);
189 return sprintf(buf, "%d\n", data->max_uA);
192 static ssize_t set_max_uA(struct device *dev, struct device_attribute *attr,
193 const char *buf, size_t count)
195 struct virtual_consumer_data *data = dev_get_drvdata(dev);
198 if (kstrtol(buf, 10, &val) != 0)
201 mutex_lock(&data->lock);
204 update_current_limit_constraints(dev, data);
206 mutex_unlock(&data->lock);
211 static ssize_t show_mode(struct device *dev,
212 struct device_attribute *attr, char *buf)
214 struct virtual_consumer_data *data = dev_get_drvdata(dev);
216 switch (data->mode) {
217 case REGULATOR_MODE_FAST:
218 return sprintf(buf, "fast\n");
219 case REGULATOR_MODE_NORMAL:
220 return sprintf(buf, "normal\n");
221 case REGULATOR_MODE_IDLE:
222 return sprintf(buf, "idle\n");
223 case REGULATOR_MODE_STANDBY:
224 return sprintf(buf, "standby\n");
226 return sprintf(buf, "unknown\n");
230 static ssize_t set_mode(struct device *dev, struct device_attribute *attr,
231 const char *buf, size_t count)
233 struct virtual_consumer_data *data = dev_get_drvdata(dev);
238 * sysfs_streq() doesn't need the \n's, but we add them so the strings
239 * will be shared with show_mode(), above.
241 if (sysfs_streq(buf, "fast\n"))
242 mode = REGULATOR_MODE_FAST;
243 else if (sysfs_streq(buf, "normal\n"))
244 mode = REGULATOR_MODE_NORMAL;
245 else if (sysfs_streq(buf, "idle\n"))
246 mode = REGULATOR_MODE_IDLE;
247 else if (sysfs_streq(buf, "standby\n"))
248 mode = REGULATOR_MODE_STANDBY;
250 dev_err(dev, "Configuring invalid mode\n");
254 mutex_lock(&data->lock);
255 ret = regulator_set_mode(data->regulator, mode);
259 dev_err(dev, "Failed to configure mode: %d\n", ret);
260 mutex_unlock(&data->lock);
265 static DEVICE_ATTR(min_microvolts, 0664, show_min_uV, set_min_uV);
266 static DEVICE_ATTR(max_microvolts, 0664, show_max_uV, set_max_uV);
267 static DEVICE_ATTR(min_microamps, 0664, show_min_uA, set_min_uA);
268 static DEVICE_ATTR(max_microamps, 0664, show_max_uA, set_max_uA);
269 static DEVICE_ATTR(mode, 0664, show_mode, set_mode);
271 static struct attribute *regulator_virtual_attributes[] = {
272 &dev_attr_min_microvolts.attr,
273 &dev_attr_max_microvolts.attr,
274 &dev_attr_min_microamps.attr,
275 &dev_attr_max_microamps.attr,
280 static const struct attribute_group regulator_virtual_attr_group = {
281 .attrs = regulator_virtual_attributes,
284 static int regulator_virtual_probe(struct platform_device *pdev)
286 char *reg_id = dev_get_platdata(&pdev->dev);
287 struct virtual_consumer_data *drvdata;
290 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct virtual_consumer_data),
295 mutex_init(&drvdata->lock);
297 drvdata->regulator = devm_regulator_get(&pdev->dev, reg_id);
298 if (IS_ERR(drvdata->regulator)) {
299 ret = PTR_ERR(drvdata->regulator);
300 dev_err(&pdev->dev, "Failed to obtain supply '%s': %d\n",
305 ret = sysfs_create_group(&pdev->dev.kobj,
306 ®ulator_virtual_attr_group);
309 "Failed to create attribute group: %d\n", ret);
313 drvdata->mode = regulator_get_mode(drvdata->regulator);
315 platform_set_drvdata(pdev, drvdata);
320 static int regulator_virtual_remove(struct platform_device *pdev)
322 struct virtual_consumer_data *drvdata = platform_get_drvdata(pdev);
324 sysfs_remove_group(&pdev->dev.kobj, ®ulator_virtual_attr_group);
326 if (drvdata->enabled)
327 regulator_disable(drvdata->regulator);
332 static struct platform_driver regulator_virtual_consumer_driver = {
333 .probe = regulator_virtual_probe,
334 .remove = regulator_virtual_remove,
336 .name = "reg-virt-consumer",
340 module_platform_driver(regulator_virtual_consumer_driver);
342 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
343 MODULE_DESCRIPTION("Virtual regulator consumer");
344 MODULE_LICENSE("GPL");
345 MODULE_ALIAS("platform:reg-virt-consumer");