blob: 52c5a0e0acd80681e39f5dd2f3d5b90176fc9c61 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Brownc0809092008-04-30 17:05:33 +01002/*
3 * reg-virtual-consumer.c
4 *
5 * Copyright 2008 Wolfson Microelectronics PLC.
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brownc0809092008-04-30 17:05:33 +01008 */
9
10#include <linux/err.h>
11#include <linux/mutex.h>
12#include <linux/platform_device.h>
13#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040015#include <linux/module.h>
Mark Brownc0809092008-04-30 17:05:33 +010016
17struct virtual_consumer_data {
18 struct mutex lock;
19 struct regulator *regulator;
Dmitry Torokhov4cf95662010-02-23 23:37:44 -080020 bool enabled;
Mark Brownc0809092008-04-30 17:05:33 +010021 int min_uV;
22 int max_uV;
23 int min_uA;
24 int max_uA;
25 unsigned int mode;
26};
27
Mark Browna07ac212009-06-17 15:45:06 +010028static void update_voltage_constraints(struct device *dev,
29 struct virtual_consumer_data *data)
Mark Brownc0809092008-04-30 17:05:33 +010030{
31 int ret;
32
33 if (data->min_uV && data->max_uV
34 && data->min_uV <= data->max_uV) {
Mark Browna5d2abc2009-06-17 15:45:07 +010035 dev_dbg(dev, "Requesting %d-%duV\n",
36 data->min_uV, data->max_uV);
Mark Brownc0809092008-04-30 17:05:33 +010037 ret = regulator_set_voltage(data->regulator,
Mark Browna07ac212009-06-17 15:45:06 +010038 data->min_uV, data->max_uV);
Mark Brownc0809092008-04-30 17:05:33 +010039 if (ret != 0) {
Mark Browna07ac212009-06-17 15:45:06 +010040 dev_err(dev,
41 "regulator_set_voltage() failed: %d\n", ret);
Mark Brownc0809092008-04-30 17:05:33 +010042 return;
43 }
44 }
45
46 if (data->min_uV && data->max_uV && !data->enabled) {
Mark Browna5d2abc2009-06-17 15:45:07 +010047 dev_dbg(dev, "Enabling regulator\n");
Mark Brownc0809092008-04-30 17:05:33 +010048 ret = regulator_enable(data->regulator);
49 if (ret == 0)
Dmitry Torokhov4cf95662010-02-23 23:37:44 -080050 data->enabled = true;
Mark Brownc0809092008-04-30 17:05:33 +010051 else
Mark Browna07ac212009-06-17 15:45:06 +010052 dev_err(dev, "regulator_enable() failed: %d\n",
Mark Brownc0809092008-04-30 17:05:33 +010053 ret);
54 }
55
56 if (!(data->min_uV && data->max_uV) && data->enabled) {
Mark Browna5d2abc2009-06-17 15:45:07 +010057 dev_dbg(dev, "Disabling regulator\n");
Mark Brownc0809092008-04-30 17:05:33 +010058 ret = regulator_disable(data->regulator);
59 if (ret == 0)
Dmitry Torokhov4cf95662010-02-23 23:37:44 -080060 data->enabled = false;
Mark Brownc0809092008-04-30 17:05:33 +010061 else
Mark Browna07ac212009-06-17 15:45:06 +010062 dev_err(dev, "regulator_disable() failed: %d\n",
Mark Brownc0809092008-04-30 17:05:33 +010063 ret);
64 }
65}
66
Mark Browna07ac212009-06-17 15:45:06 +010067static void update_current_limit_constraints(struct device *dev,
68 struct virtual_consumer_data *data)
Mark Brownc0809092008-04-30 17:05:33 +010069{
70 int ret;
71
72 if (data->max_uA
73 && data->min_uA <= data->max_uA) {
Mark Browna5d2abc2009-06-17 15:45:07 +010074 dev_dbg(dev, "Requesting %d-%duA\n",
75 data->min_uA, data->max_uA);
Mark Brownc0809092008-04-30 17:05:33 +010076 ret = regulator_set_current_limit(data->regulator,
77 data->min_uA, data->max_uA);
78 if (ret != 0) {
Mark Browna07ac212009-06-17 15:45:06 +010079 dev_err(dev,
80 "regulator_set_current_limit() failed: %d\n",
81 ret);
Mark Brownc0809092008-04-30 17:05:33 +010082 return;
83 }
84 }
85
86 if (data->max_uA && !data->enabled) {
Mark Browna5d2abc2009-06-17 15:45:07 +010087 dev_dbg(dev, "Enabling regulator\n");
Mark Brownc0809092008-04-30 17:05:33 +010088 ret = regulator_enable(data->regulator);
89 if (ret == 0)
Dmitry Torokhov4cf95662010-02-23 23:37:44 -080090 data->enabled = true;
Mark Brownc0809092008-04-30 17:05:33 +010091 else
Mark Browna07ac212009-06-17 15:45:06 +010092 dev_err(dev, "regulator_enable() failed: %d\n",
Mark Brownc0809092008-04-30 17:05:33 +010093 ret);
94 }
95
96 if (!(data->min_uA && data->max_uA) && data->enabled) {
Mark Browna5d2abc2009-06-17 15:45:07 +010097 dev_dbg(dev, "Disabling regulator\n");
Mark Brownc0809092008-04-30 17:05:33 +010098 ret = regulator_disable(data->regulator);
99 if (ret == 0)
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800100 data->enabled = false;
Mark Brownc0809092008-04-30 17:05:33 +0100101 else
Mark Browna07ac212009-06-17 15:45:06 +0100102 dev_err(dev, "regulator_disable() failed: %d\n",
Mark Brownc0809092008-04-30 17:05:33 +0100103 ret);
104 }
105}
106
107static ssize_t show_min_uV(struct device *dev,
108 struct device_attribute *attr, char *buf)
109{
110 struct virtual_consumer_data *data = dev_get_drvdata(dev);
111 return sprintf(buf, "%d\n", data->min_uV);
112}
113
114static ssize_t set_min_uV(struct device *dev, struct device_attribute *attr,
115 const char *buf, size_t count)
116{
117 struct virtual_consumer_data *data = dev_get_drvdata(dev);
118 long val;
119
Axel Lin85960e72012-04-19 09:03:45 +0800120 if (kstrtol(buf, 10, &val) != 0)
Mark Brownc0809092008-04-30 17:05:33 +0100121 return count;
122
123 mutex_lock(&data->lock);
124
125 data->min_uV = val;
Mark Browna07ac212009-06-17 15:45:06 +0100126 update_voltage_constraints(dev, data);
Mark Brownc0809092008-04-30 17:05:33 +0100127
128 mutex_unlock(&data->lock);
129
130 return count;
131}
132
133static ssize_t show_max_uV(struct device *dev,
134 struct device_attribute *attr, char *buf)
135{
136 struct virtual_consumer_data *data = dev_get_drvdata(dev);
137 return sprintf(buf, "%d\n", data->max_uV);
138}
139
140static ssize_t set_max_uV(struct device *dev, struct device_attribute *attr,
141 const char *buf, size_t count)
142{
143 struct virtual_consumer_data *data = dev_get_drvdata(dev);
144 long val;
145
Axel Lin85960e72012-04-19 09:03:45 +0800146 if (kstrtol(buf, 10, &val) != 0)
Mark Brownc0809092008-04-30 17:05:33 +0100147 return count;
148
149 mutex_lock(&data->lock);
150
151 data->max_uV = val;
Mark Browna07ac212009-06-17 15:45:06 +0100152 update_voltage_constraints(dev, data);
Mark Brownc0809092008-04-30 17:05:33 +0100153
154 mutex_unlock(&data->lock);
155
156 return count;
157}
158
159static ssize_t show_min_uA(struct device *dev,
160 struct device_attribute *attr, char *buf)
161{
162 struct virtual_consumer_data *data = dev_get_drvdata(dev);
163 return sprintf(buf, "%d\n", data->min_uA);
164}
165
166static ssize_t set_min_uA(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t count)
168{
169 struct virtual_consumer_data *data = dev_get_drvdata(dev);
170 long val;
171
Axel Lin85960e72012-04-19 09:03:45 +0800172 if (kstrtol(buf, 10, &val) != 0)
Mark Brownc0809092008-04-30 17:05:33 +0100173 return count;
174
175 mutex_lock(&data->lock);
176
177 data->min_uA = val;
Mark Browna07ac212009-06-17 15:45:06 +0100178 update_current_limit_constraints(dev, data);
Mark Brownc0809092008-04-30 17:05:33 +0100179
180 mutex_unlock(&data->lock);
181
182 return count;
183}
184
185static ssize_t show_max_uA(struct device *dev,
186 struct device_attribute *attr, char *buf)
187{
188 struct virtual_consumer_data *data = dev_get_drvdata(dev);
189 return sprintf(buf, "%d\n", data->max_uA);
190}
191
192static ssize_t set_max_uA(struct device *dev, struct device_attribute *attr,
193 const char *buf, size_t count)
194{
195 struct virtual_consumer_data *data = dev_get_drvdata(dev);
196 long val;
197
Axel Lin85960e72012-04-19 09:03:45 +0800198 if (kstrtol(buf, 10, &val) != 0)
Mark Brownc0809092008-04-30 17:05:33 +0100199 return count;
200
201 mutex_lock(&data->lock);
202
203 data->max_uA = val;
Mark Browna07ac212009-06-17 15:45:06 +0100204 update_current_limit_constraints(dev, data);
Mark Brownc0809092008-04-30 17:05:33 +0100205
206 mutex_unlock(&data->lock);
207
208 return count;
209}
210
211static ssize_t show_mode(struct device *dev,
212 struct device_attribute *attr, char *buf)
213{
214 struct virtual_consumer_data *data = dev_get_drvdata(dev);
215
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");
225 default:
226 return sprintf(buf, "unknown\n");
227 }
228}
229
230static ssize_t set_mode(struct device *dev, struct device_attribute *attr,
231 const char *buf, size_t count)
232{
233 struct virtual_consumer_data *data = dev_get_drvdata(dev);
234 unsigned int mode;
235 int ret;
236
Andrew Morton94853972009-01-15 16:13:01 -0800237 /*
238 * sysfs_streq() doesn't need the \n's, but we add them so the strings
239 * will be shared with show_mode(), above.
240 */
Mike Rapoportaa61d552009-04-26 11:49:30 +0300241 if (sysfs_streq(buf, "fast\n"))
Mark Brownc0809092008-04-30 17:05:33 +0100242 mode = REGULATOR_MODE_FAST;
Mike Rapoportaa61d552009-04-26 11:49:30 +0300243 else if (sysfs_streq(buf, "normal\n"))
Mark Brownc0809092008-04-30 17:05:33 +0100244 mode = REGULATOR_MODE_NORMAL;
Mike Rapoportaa61d552009-04-26 11:49:30 +0300245 else if (sysfs_streq(buf, "idle\n"))
Mark Brownc0809092008-04-30 17:05:33 +0100246 mode = REGULATOR_MODE_IDLE;
Mike Rapoportaa61d552009-04-26 11:49:30 +0300247 else if (sysfs_streq(buf, "standby\n"))
Mark Brownc0809092008-04-30 17:05:33 +0100248 mode = REGULATOR_MODE_STANDBY;
249 else {
250 dev_err(dev, "Configuring invalid mode\n");
251 return count;
252 }
253
254 mutex_lock(&data->lock);
255 ret = regulator_set_mode(data->regulator, mode);
256 if (ret == 0)
257 data->mode = mode;
258 else
259 dev_err(dev, "Failed to configure mode: %d\n", ret);
260 mutex_unlock(&data->lock);
261
262 return count;
263}
264
Rusty Russell71e06af2014-05-14 10:33:49 +0930265static DEVICE_ATTR(min_microvolts, 0664, show_min_uV, set_min_uV);
266static DEVICE_ATTR(max_microvolts, 0664, show_max_uV, set_max_uV);
267static DEVICE_ATTR(min_microamps, 0664, show_min_uA, set_min_uA);
268static DEVICE_ATTR(max_microamps, 0664, show_max_uA, set_max_uA);
269static DEVICE_ATTR(mode, 0664, show_mode, set_mode);
Mark Brownc0809092008-04-30 17:05:33 +0100270
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800271static 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,
276 &dev_attr_mode.attr,
277 NULL
Mark Brownc0809092008-04-30 17:05:33 +0100278};
279
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800280static const struct attribute_group regulator_virtual_attr_group = {
281 .attrs = regulator_virtual_attributes,
282};
283
Bill Pembertona5023572012-11-19 13:22:22 -0500284static int regulator_virtual_probe(struct platform_device *pdev)
Mark Brownc0809092008-04-30 17:05:33 +0100285{
Jingoo Handff91d02013-07-30 17:20:47 +0900286 char *reg_id = dev_get_platdata(&pdev->dev);
Mark Brownc0809092008-04-30 17:05:33 +0100287 struct virtual_consumer_data *drvdata;
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800288 int ret;
Mark Brownc0809092008-04-30 17:05:33 +0100289
Axel Lina81edbd2012-04-18 09:35:35 +0800290 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct virtual_consumer_data),
291 GFP_KERNEL);
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800292 if (drvdata == NULL)
Mark Brown72b868762009-07-21 16:00:27 +0100293 return -ENOMEM;
Mark Brownc0809092008-04-30 17:05:33 +0100294
295 mutex_init(&drvdata->lock);
296
Axel Lina81edbd2012-04-18 09:35:35 +0800297 drvdata->regulator = devm_regulator_get(&pdev->dev, reg_id);
Mark Brownc0809092008-04-30 17:05:33 +0100298 if (IS_ERR(drvdata->regulator)) {
299 ret = PTR_ERR(drvdata->regulator);
Mark Brownd61c3d52009-06-15 20:01:01 +0100300 dev_err(&pdev->dev, "Failed to obtain supply '%s': %d\n",
301 reg_id, ret);
Axel Lina81edbd2012-04-18 09:35:35 +0800302 return ret;
Mark Brownc0809092008-04-30 17:05:33 +0100303 }
304
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800305 ret = sysfs_create_group(&pdev->dev.kobj,
306 &regulator_virtual_attr_group);
307 if (ret != 0) {
308 dev_err(&pdev->dev,
309 "Failed to create attribute group: %d\n", ret);
Axel Lina81edbd2012-04-18 09:35:35 +0800310 return ret;
Mark Brownc0809092008-04-30 17:05:33 +0100311 }
312
313 drvdata->mode = regulator_get_mode(drvdata->regulator);
314
315 platform_set_drvdata(pdev, drvdata);
316
317 return 0;
Mark Brownc0809092008-04-30 17:05:33 +0100318}
319
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500320static int regulator_virtual_remove(struct platform_device *pdev)
Mark Brownc0809092008-04-30 17:05:33 +0100321{
322 struct virtual_consumer_data *drvdata = platform_get_drvdata(pdev);
Mark Brownc0809092008-04-30 17:05:33 +0100323
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800324 sysfs_remove_group(&pdev->dev.kobj, &regulator_virtual_attr_group);
325
Mark Brownc0809092008-04-30 17:05:33 +0100326 if (drvdata->enabled)
327 regulator_disable(drvdata->regulator);
Mark Brownc0809092008-04-30 17:05:33 +0100328
329 return 0;
330}
331
332static struct platform_driver regulator_virtual_consumer_driver = {
Dmitry Torokhov4cf95662010-02-23 23:37:44 -0800333 .probe = regulator_virtual_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500334 .remove = regulator_virtual_remove,
Mark Brownc0809092008-04-30 17:05:33 +0100335 .driver = {
336 .name = "reg-virt-consumer",
337 },
338};
339
Axel Lin005d6102011-11-28 11:38:39 +0800340module_platform_driver(regulator_virtual_consumer_driver);
Mark Brownc0809092008-04-30 17:05:33 +0100341
342MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
343MODULE_DESCRIPTION("Virtual regulator consumer");
344MODULE_LICENSE("GPL");
Mark Brown38c53c82009-04-28 11:13:55 +0100345MODULE_ALIAS("platform:reg-virt-consumer");