blob: 9d5ba93575975f22e8d00a620613cc1848ffb8fe [file] [log] [blame]
Mike Rapoport1d98ccc2009-04-26 16:49:39 +03001/*
2 * userspace-consumer.c
3 *
4 * Copyright 2009 CompuLab, Ltd.
5 *
6 * Author: Mike Rapoport <mike@compulab.co.il>
7 *
8 * Based of virtual consumer driver:
9 * Copyright 2008 Wolfson Microelectronics PLC.
10 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
11 *
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.
16 *
17 */
18
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Mike Rapoport1d98ccc2009-04-26 16:49:39 +030025
26struct userspace_consumer_data {
27 const char *name;
28
29 struct mutex lock;
30 bool enabled;
31
32 int num_supplies;
33 struct regulator_bulk_data *supplies;
34};
35
Liam Girdwood7c314992009-04-28 12:01:16 +010036static ssize_t reg_show_name(struct device *dev,
Mike Rapoport1d98ccc2009-04-26 16:49:39 +030037 struct device_attribute *attr, char *buf)
38{
39 struct userspace_consumer_data *data = dev_get_drvdata(dev);
40
41 return sprintf(buf, "%s\n", data->name);
42}
43
Liam Girdwood7c314992009-04-28 12:01:16 +010044static ssize_t reg_show_state(struct device *dev,
Mike Rapoport1d98ccc2009-04-26 16:49:39 +030045 struct device_attribute *attr, char *buf)
46{
47 struct userspace_consumer_data *data = dev_get_drvdata(dev);
48
49 if (data->enabled)
50 return sprintf(buf, "enabled\n");
51
52 return sprintf(buf, "disabled\n");
53}
54
Liam Girdwood7c314992009-04-28 12:01:16 +010055static ssize_t reg_set_state(struct device *dev, struct device_attribute *attr,
Mike Rapoport1d98ccc2009-04-26 16:49:39 +030056 const char *buf, size_t count)
57{
58 struct userspace_consumer_data *data = dev_get_drvdata(dev);
59 bool enabled;
60 int ret;
61
62 /*
63 * sysfs_streq() doesn't need the \n's, but we add them so the strings
64 * will be shared with show_state(), above.
65 */
66 if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1"))
67 enabled = true;
68 else if (sysfs_streq(buf, "disabled\n") || sysfs_streq(buf, "0"))
69 enabled = false;
70 else {
71 dev_err(dev, "Configuring invalid mode\n");
72 return count;
73 }
74
75 mutex_lock(&data->lock);
76 if (enabled != data->enabled) {
77 if (enabled)
78 ret = regulator_bulk_enable(data->num_supplies,
79 data->supplies);
80 else
81 ret = regulator_bulk_disable(data->num_supplies,
82 data->supplies);
83
84 if (ret == 0)
85 data->enabled = enabled;
86 else
87 dev_err(dev, "Failed to configure state: %d\n", ret);
88 }
89 mutex_unlock(&data->lock);
90
91 return count;
92}
93
Liam Girdwood7c314992009-04-28 12:01:16 +010094static DEVICE_ATTR(name, 0444, reg_show_name, NULL);
95static DEVICE_ATTR(state, 0644, reg_show_state, reg_set_state);
Mike Rapoport1d98ccc2009-04-26 16:49:39 +030096
Felipe Balbie9d62692009-08-10 09:05:13 +030097static struct attribute *attributes[] = {
98 &dev_attr_name.attr,
99 &dev_attr_state.attr,
100 NULL,
101};
102
103static const struct attribute_group attr_group = {
104 .attrs = attributes,
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300105};
106
107static int regulator_userspace_consumer_probe(struct platform_device *pdev)
108{
109 struct regulator_userspace_consumer_data *pdata;
110 struct userspace_consumer_data *drvdata;
Felipe Balbie9d62692009-08-10 09:05:13 +0300111 int ret;
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300112
113 pdata = pdev->dev.platform_data;
114 if (!pdata)
115 return -EINVAL;
116
117 drvdata = kzalloc(sizeof(struct userspace_consumer_data), GFP_KERNEL);
118 if (drvdata == NULL)
119 return -ENOMEM;
120
121 drvdata->name = pdata->name;
122 drvdata->num_supplies = pdata->num_supplies;
123 drvdata->supplies = pdata->supplies;
124
125 mutex_init(&drvdata->lock);
126
127 ret = regulator_bulk_get(&pdev->dev, drvdata->num_supplies,
128 drvdata->supplies);
129 if (ret) {
130 dev_err(&pdev->dev, "Failed to get supplies: %d\n", ret);
131 goto err_alloc_supplies;
132 }
133
Felipe Balbie9d62692009-08-10 09:05:13 +0300134 ret = sysfs_create_group(&pdev->dev.kobj, &attr_group);
135 if (ret != 0)
136 goto err_create_attrs;
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300137
Felipe Balbie9d62692009-08-10 09:05:13 +0300138 if (pdata->init_on) {
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300139 ret = regulator_bulk_enable(drvdata->num_supplies,
140 drvdata->supplies);
Felipe Balbie9d62692009-08-10 09:05:13 +0300141 if (ret) {
142 dev_err(&pdev->dev,
143 "Failed to set initial state: %d\n", ret);
144 goto err_enable;
145 }
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300146 }
147
Felipe Balbie9d62692009-08-10 09:05:13 +0300148 drvdata->enabled = pdata->init_on;
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300149 platform_set_drvdata(pdev, drvdata);
150
151 return 0;
152
Felipe Balbie9d62692009-08-10 09:05:13 +0300153err_enable:
154 sysfs_remove_group(&pdev->dev.kobj, &attr_group);
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300155
Felipe Balbie9d62692009-08-10 09:05:13 +0300156err_create_attrs:
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300157 regulator_bulk_free(drvdata->num_supplies, drvdata->supplies);
158
159err_alloc_supplies:
160 kfree(drvdata);
161 return ret;
162}
163
164static int regulator_userspace_consumer_remove(struct platform_device *pdev)
165{
166 struct userspace_consumer_data *data = platform_get_drvdata(pdev);
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300167
Felipe Balbie9d62692009-08-10 09:05:13 +0300168 sysfs_remove_group(&pdev->dev.kobj, &attr_group);
Mike Rapoport1d98ccc2009-04-26 16:49:39 +0300169
170 if (data->enabled)
171 regulator_bulk_disable(data->num_supplies, data->supplies);
172
173 regulator_bulk_free(data->num_supplies, data->supplies);
174 kfree(data);
175
176 return 0;
177}
178
179static struct platform_driver regulator_userspace_consumer_driver = {
180 .probe = regulator_userspace_consumer_probe,
181 .remove = regulator_userspace_consumer_remove,
182 .driver = {
183 .name = "reg-userspace-consumer",
184 },
185};
186
187
188static int __init regulator_userspace_consumer_init(void)
189{
190 return platform_driver_register(&regulator_userspace_consumer_driver);
191}
192module_init(regulator_userspace_consumer_init);
193
194static void __exit regulator_userspace_consumer_exit(void)
195{
196 platform_driver_unregister(&regulator_userspace_consumer_driver);
197}
198module_exit(regulator_userspace_consumer_exit);
199
200MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
201MODULE_DESCRIPTION("Userspace consumer for voltage and current regulators");
202MODULE_LICENSE("GPL");