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/platform_device.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/userspace-consumer.h>
25 struct userspace_consumer_data {
32 struct regulator_bulk_data *supplies;
35 static ssize_t reg_show_name(struct device *dev,
36 struct device_attribute *attr, char *buf)
38 struct userspace_consumer_data *data = dev_get_drvdata(dev);
40 return sprintf(buf, "%s\n", data->name);
43 static ssize_t reg_show_state(struct device *dev,
44 struct device_attribute *attr, char *buf)
46 struct userspace_consumer_data *data = dev_get_drvdata(dev);
49 return sprintf(buf, "enabled\n");
51 return sprintf(buf, "disabled\n");
54 static ssize_t reg_set_state(struct device *dev, struct device_attribute *attr,
55 const char *buf, size_t count)
57 struct userspace_consumer_data *data = dev_get_drvdata(dev);
62 * sysfs_streq() doesn't need the \n's, but we add them so the strings
63 * will be shared with show_state(), above.
65 if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1"))
67 else if (sysfs_streq(buf, "disabled\n") || sysfs_streq(buf, "0"))
70 dev_err(dev, "Configuring invalid mode\n");
74 mutex_lock(&data->lock);
75 if (enabled != data->enabled) {
77 ret = regulator_bulk_enable(data->num_supplies,
80 ret = regulator_bulk_disable(data->num_supplies,
84 data->enabled = enabled;
86 dev_err(dev, "Failed to configure state: %d\n", ret);
88 mutex_unlock(&data->lock);
93 static DEVICE_ATTR(name, 0444, reg_show_name, NULL);
94 static DEVICE_ATTR(state, 0644, reg_show_state, reg_set_state);
96 static struct device_attribute *attributes[] = {
101 static int regulator_userspace_consumer_probe(struct platform_device *pdev)
103 struct regulator_userspace_consumer_data *pdata;
104 struct userspace_consumer_data *drvdata;
107 pdata = pdev->dev.platform_data;
111 drvdata = kzalloc(sizeof(struct userspace_consumer_data), GFP_KERNEL);
115 drvdata->name = pdata->name;
116 drvdata->num_supplies = pdata->num_supplies;
117 drvdata->supplies = pdata->supplies;
119 mutex_init(&drvdata->lock);
121 ret = regulator_bulk_get(&pdev->dev, drvdata->num_supplies,
124 dev_err(&pdev->dev, "Failed to get supplies: %d\n", ret);
125 goto err_alloc_supplies;
128 for (i = 0; i < ARRAY_SIZE(attributes); i++) {
129 ret = device_create_file(&pdev->dev, attributes[i]);
131 goto err_create_attrs;
135 ret = regulator_bulk_enable(drvdata->num_supplies,
138 drvdata->enabled = pdata->init_on;
141 dev_err(&pdev->dev, "Failed to set initial state: %d\n", ret);
142 goto err_create_attrs;
145 platform_set_drvdata(pdev, drvdata);
150 for (i = 0; i < ARRAY_SIZE(attributes); i++)
151 device_remove_file(&pdev->dev, attributes[i]);
153 regulator_bulk_free(drvdata->num_supplies, drvdata->supplies);
160 static int regulator_userspace_consumer_remove(struct platform_device *pdev)
162 struct userspace_consumer_data *data = platform_get_drvdata(pdev);
165 for (i = 0; i < ARRAY_SIZE(attributes); i++)
166 device_remove_file(&pdev->dev, attributes[i]);
169 regulator_bulk_disable(data->num_supplies, data->supplies);
171 regulator_bulk_free(data->num_supplies, data->supplies);
177 static struct platform_driver regulator_userspace_consumer_driver = {
178 .probe = regulator_userspace_consumer_probe,
179 .remove = regulator_userspace_consumer_remove,
181 .name = "reg-userspace-consumer",
186 static int __init regulator_userspace_consumer_init(void)
188 return platform_driver_register(®ulator_userspace_consumer_driver);
190 module_init(regulator_userspace_consumer_init);
192 static void __exit regulator_userspace_consumer_exit(void)
194 platform_driver_unregister(®ulator_userspace_consumer_driver);
196 module_exit(regulator_userspace_consumer_exit);
198 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
199 MODULE_DESCRIPTION("Userspace consumer for voltage and current regulators");
200 MODULE_LICENSE("GPL");