1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright 2009 CompuLab, Ltd.
7 * Author: Mike Rapoport <mike@compulab.co.il>
9 * Based of virtual consumer driver:
10 * Copyright 2008 Wolfson Microelectronics PLC.
11 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
14 #include <linux/err.h>
15 #include <linux/mutex.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/regulator/userspace-consumer.h>
20 #include <linux/slab.h>
22 struct userspace_consumer_data {
29 struct regulator_bulk_data *supplies;
32 static ssize_t reg_show_name(struct device *dev,
33 struct device_attribute *attr, char *buf)
35 struct userspace_consumer_data *data = dev_get_drvdata(dev);
37 return sprintf(buf, "%s\n", data->name);
40 static ssize_t reg_show_state(struct device *dev,
41 struct device_attribute *attr, char *buf)
43 struct userspace_consumer_data *data = dev_get_drvdata(dev);
46 return sprintf(buf, "enabled\n");
48 return sprintf(buf, "disabled\n");
51 static ssize_t reg_set_state(struct device *dev, struct device_attribute *attr,
52 const char *buf, size_t count)
54 struct userspace_consumer_data *data = dev_get_drvdata(dev);
59 * sysfs_streq() doesn't need the \n's, but we add them so the strings
60 * will be shared with show_state(), above.
62 if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1"))
64 else if (sysfs_streq(buf, "disabled\n") || sysfs_streq(buf, "0"))
67 dev_err(dev, "Configuring invalid mode\n");
71 mutex_lock(&data->lock);
72 if (enabled != data->enabled) {
74 ret = regulator_bulk_enable(data->num_supplies,
77 ret = regulator_bulk_disable(data->num_supplies,
81 data->enabled = enabled;
83 dev_err(dev, "Failed to configure state: %d\n", ret);
85 mutex_unlock(&data->lock);
90 static DEVICE_ATTR(name, 0444, reg_show_name, NULL);
91 static DEVICE_ATTR(state, 0644, reg_show_state, reg_set_state);
93 static struct attribute *attributes[] = {
99 static const struct attribute_group attr_group = {
103 static int regulator_userspace_consumer_probe(struct platform_device *pdev)
105 struct regulator_userspace_consumer_data *pdata;
106 struct userspace_consumer_data *drvdata;
109 pdata = dev_get_platdata(&pdev->dev);
113 drvdata = devm_kzalloc(&pdev->dev,
114 sizeof(struct userspace_consumer_data),
119 drvdata->name = pdata->name;
120 drvdata->num_supplies = pdata->num_supplies;
121 drvdata->supplies = pdata->supplies;
123 mutex_init(&drvdata->lock);
125 ret = devm_regulator_bulk_get(&pdev->dev, drvdata->num_supplies,
128 dev_err(&pdev->dev, "Failed to get supplies: %d\n", ret);
132 ret = sysfs_create_group(&pdev->dev.kobj, &attr_group);
136 if (pdata->init_on) {
137 ret = regulator_bulk_enable(drvdata->num_supplies,
141 "Failed to set initial state: %d\n", ret);
146 drvdata->enabled = pdata->init_on;
147 platform_set_drvdata(pdev, drvdata);
152 sysfs_remove_group(&pdev->dev.kobj, &attr_group);
157 static int regulator_userspace_consumer_remove(struct platform_device *pdev)
159 struct userspace_consumer_data *data = platform_get_drvdata(pdev);
161 sysfs_remove_group(&pdev->dev.kobj, &attr_group);
164 regulator_bulk_disable(data->num_supplies, data->supplies);
169 static struct platform_driver regulator_userspace_consumer_driver = {
170 .probe = regulator_userspace_consumer_probe,
171 .remove = regulator_userspace_consumer_remove,
173 .name = "reg-userspace-consumer",
177 module_platform_driver(regulator_userspace_consumer_driver);
179 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
180 MODULE_DESCRIPTION("Userspace consumer for voltage and current regulators");
181 MODULE_LICENSE("GPL");