4 * Copyright 2009 CompuLab, Ltd.
6 * Author: Mike Rapoport <mike@compulab.co.il>
8 * Based of virtual consumer driver:
9 * Copyright 2008 Wolfson Microelectronics PLC.
10 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
19 #include <linux/err.h>
20 #include <linux/mutex.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/regulator/userspace-consumer.h>
25 #include <linux/slab.h>
27 struct userspace_consumer_data {
34 struct regulator_bulk_data *supplies;
37 static ssize_t reg_show_name(struct device *dev,
38 struct device_attribute *attr, char *buf)
40 struct userspace_consumer_data *data = dev_get_drvdata(dev);
42 return sprintf(buf, "%s\n", data->name);
45 static ssize_t reg_show_state(struct device *dev,
46 struct device_attribute *attr, char *buf)
48 struct userspace_consumer_data *data = dev_get_drvdata(dev);
51 return sprintf(buf, "enabled\n");
53 return sprintf(buf, "disabled\n");
56 static ssize_t reg_set_state(struct device *dev, struct device_attribute *attr,
57 const char *buf, size_t count)
59 struct userspace_consumer_data *data = dev_get_drvdata(dev);
64 * sysfs_streq() doesn't need the \n's, but we add them so the strings
65 * will be shared with show_state(), above.
67 if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1"))
69 else if (sysfs_streq(buf, "disabled\n") || sysfs_streq(buf, "0"))
72 dev_err(dev, "Configuring invalid mode\n");
76 mutex_lock(&data->lock);
77 if (enabled != data->enabled) {
79 ret = regulator_bulk_enable(data->num_supplies,
82 ret = regulator_bulk_disable(data->num_supplies,
86 data->enabled = enabled;
88 dev_err(dev, "Failed to configure state: %d\n", ret);
90 mutex_unlock(&data->lock);
95 static DEVICE_ATTR(name, 0444, reg_show_name, NULL);
96 static DEVICE_ATTR(state, 0644, reg_show_state, reg_set_state);
98 static struct attribute *attributes[] = {
100 &dev_attr_state.attr,
104 static const struct attribute_group attr_group = {
108 static int regulator_userspace_consumer_probe(struct platform_device *pdev)
110 struct regulator_userspace_consumer_data *pdata;
111 struct userspace_consumer_data *drvdata;
114 pdata = dev_get_platdata(&pdev->dev);
118 drvdata = devm_kzalloc(&pdev->dev,
119 sizeof(struct userspace_consumer_data),
124 drvdata->name = pdata->name;
125 drvdata->num_supplies = pdata->num_supplies;
126 drvdata->supplies = pdata->supplies;
128 mutex_init(&drvdata->lock);
130 ret = devm_regulator_bulk_get(&pdev->dev, drvdata->num_supplies,
133 dev_err(&pdev->dev, "Failed to get supplies: %d\n", ret);
137 ret = sysfs_create_group(&pdev->dev.kobj, &attr_group);
141 if (pdata->init_on) {
142 ret = regulator_bulk_enable(drvdata->num_supplies,
146 "Failed to set initial state: %d\n", ret);
151 drvdata->enabled = pdata->init_on;
152 platform_set_drvdata(pdev, drvdata);
157 sysfs_remove_group(&pdev->dev.kobj, &attr_group);
162 static int regulator_userspace_consumer_remove(struct platform_device *pdev)
164 struct userspace_consumer_data *data = platform_get_drvdata(pdev);
166 sysfs_remove_group(&pdev->dev.kobj, &attr_group);
169 regulator_bulk_disable(data->num_supplies, data->supplies);
174 static struct platform_driver regulator_userspace_consumer_driver = {
175 .probe = regulator_userspace_consumer_probe,
176 .remove = regulator_userspace_consumer_remove,
178 .name = "reg-userspace-consumer",
182 module_platform_driver(regulator_userspace_consumer_driver);
184 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
185 MODULE_DESCRIPTION("Userspace consumer for voltage and current regulators");
186 MODULE_LICENSE("GPL");