]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/regulator/userspace-consumer.c
UBUNTU: [Config] Set CONFIG_PWM_PCA9685=m for amd64 and i386
[mirror_ubuntu-zesty-kernel.git] / drivers / regulator / userspace-consumer.c
CommitLineData
1d98cccf
MR
1/*
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>
65602c32 21#include <linux/module.h>
1d98cccf
MR
22#include <linux/platform_device.h>
23#include <linux/regulator/consumer.h>
24#include <linux/regulator/userspace-consumer.h>
5a0e3ad6 25#include <linux/slab.h>
1d98cccf
MR
26
27struct userspace_consumer_data {
28 const char *name;
29
30 struct mutex lock;
31 bool enabled;
32
33 int num_supplies;
34 struct regulator_bulk_data *supplies;
35};
36
7c314991 37static ssize_t reg_show_name(struct device *dev,
1d98cccf
MR
38 struct device_attribute *attr, char *buf)
39{
40 struct userspace_consumer_data *data = dev_get_drvdata(dev);
41
42 return sprintf(buf, "%s\n", data->name);
43}
44
7c314991 45static ssize_t reg_show_state(struct device *dev,
1d98cccf
MR
46 struct device_attribute *attr, char *buf)
47{
48 struct userspace_consumer_data *data = dev_get_drvdata(dev);
49
50 if (data->enabled)
51 return sprintf(buf, "enabled\n");
52
53 return sprintf(buf, "disabled\n");
54}
55
7c314991 56static ssize_t reg_set_state(struct device *dev, struct device_attribute *attr,
1d98cccf
MR
57 const char *buf, size_t count)
58{
59 struct userspace_consumer_data *data = dev_get_drvdata(dev);
60 bool enabled;
61 int ret;
62
63 /*
64 * sysfs_streq() doesn't need the \n's, but we add them so the strings
65 * will be shared with show_state(), above.
66 */
67 if (sysfs_streq(buf, "enabled\n") || sysfs_streq(buf, "1"))
68 enabled = true;
69 else if (sysfs_streq(buf, "disabled\n") || sysfs_streq(buf, "0"))
70 enabled = false;
71 else {
72 dev_err(dev, "Configuring invalid mode\n");
73 return count;
74 }
75
76 mutex_lock(&data->lock);
77 if (enabled != data->enabled) {
78 if (enabled)
79 ret = regulator_bulk_enable(data->num_supplies,
80 data->supplies);
81 else
82 ret = regulator_bulk_disable(data->num_supplies,
83 data->supplies);
84
85 if (ret == 0)
86 data->enabled = enabled;
87 else
88 dev_err(dev, "Failed to configure state: %d\n", ret);
89 }
90 mutex_unlock(&data->lock);
91
92 return count;
93}
94
7c314991
LG
95static DEVICE_ATTR(name, 0444, reg_show_name, NULL);
96static DEVICE_ATTR(state, 0644, reg_show_state, reg_set_state);
1d98cccf 97
e9d62698
FB
98static struct attribute *attributes[] = {
99 &dev_attr_name.attr,
100 &dev_attr_state.attr,
101 NULL,
102};
103
104static const struct attribute_group attr_group = {
105 .attrs = attributes,
1d98cccf
MR
106};
107
108static int regulator_userspace_consumer_probe(struct platform_device *pdev)
109{
110 struct regulator_userspace_consumer_data *pdata;
111 struct userspace_consumer_data *drvdata;
e9d62698 112 int ret;
1d98cccf 113
dff91d0b 114 pdata = dev_get_platdata(&pdev->dev);
1d98cccf
MR
115 if (!pdata)
116 return -EINVAL;
117
5abe0c40
AL
118 drvdata = devm_kzalloc(&pdev->dev,
119 sizeof(struct userspace_consumer_data),
120 GFP_KERNEL);
1d98cccf
MR
121 if (drvdata == NULL)
122 return -ENOMEM;
123
124 drvdata->name = pdata->name;
125 drvdata->num_supplies = pdata->num_supplies;
126 drvdata->supplies = pdata->supplies;
127
128 mutex_init(&drvdata->lock);
129
5abe0c40
AL
130 ret = devm_regulator_bulk_get(&pdev->dev, drvdata->num_supplies,
131 drvdata->supplies);
1d98cccf
MR
132 if (ret) {
133 dev_err(&pdev->dev, "Failed to get supplies: %d\n", ret);
5abe0c40 134 return ret;
1d98cccf
MR
135 }
136
e9d62698
FB
137 ret = sysfs_create_group(&pdev->dev.kobj, &attr_group);
138 if (ret != 0)
5abe0c40 139 return ret;
1d98cccf 140
e9d62698 141 if (pdata->init_on) {
1d98cccf
MR
142 ret = regulator_bulk_enable(drvdata->num_supplies,
143 drvdata->supplies);
e9d62698
FB
144 if (ret) {
145 dev_err(&pdev->dev,
146 "Failed to set initial state: %d\n", ret);
147 goto err_enable;
148 }
1d98cccf
MR
149 }
150
e9d62698 151 drvdata->enabled = pdata->init_on;
1d98cccf
MR
152 platform_set_drvdata(pdev, drvdata);
153
154 return 0;
155
e9d62698
FB
156err_enable:
157 sysfs_remove_group(&pdev->dev.kobj, &attr_group);
1d98cccf 158
1d98cccf
MR
159 return ret;
160}
161
162static int regulator_userspace_consumer_remove(struct platform_device *pdev)
163{
164 struct userspace_consumer_data *data = platform_get_drvdata(pdev);
1d98cccf 165
e9d62698 166 sysfs_remove_group(&pdev->dev.kobj, &attr_group);
1d98cccf
MR
167
168 if (data->enabled)
169 regulator_bulk_disable(data->num_supplies, data->supplies);
170
1d98cccf
MR
171 return 0;
172}
173
174static struct platform_driver regulator_userspace_consumer_driver = {
175 .probe = regulator_userspace_consumer_probe,
176 .remove = regulator_userspace_consumer_remove,
177 .driver = {
178 .name = "reg-userspace-consumer",
179 },
180};
181
005d610f 182module_platform_driver(regulator_userspace_consumer_driver);
1d98cccf
MR
183
184MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
185MODULE_DESCRIPTION("Userspace consumer for voltage and current regulators");
186MODULE_LICENSE("GPL");