1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*
* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define pr_fmt(fmt) "%s: " fmt, __func__
#include <linux/module.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/msm-gpio-regulator.h>
struct gpio_vreg {
struct regulator_desc desc;
struct regulator_dev *rdev;
char *gpio_label;
char *name;
unsigned gpio;
int active_low;
bool gpio_requested;
};
static int gpio_vreg_request_gpio(struct gpio_vreg *vreg)
{
int rc = 0;
/* Request GPIO now if it hasn't been requested before. */
if (!vreg->gpio_requested) {
rc = gpio_request(vreg->gpio, vreg->gpio_label);
if (rc < 0) {
pr_err("failed to request gpio %u (%s), rc=%d\n",
vreg->gpio, vreg->gpio_label, rc);
return rc;
} else {
vreg->gpio_requested = true;
}
rc = gpio_sysfs_set_active_low(vreg->gpio, vreg->active_low);
if (rc < 0)
pr_err("active_low=%d failed for gpio %u, rc=%d\n",
vreg->active_low, vreg->gpio, rc);
}
return rc;
}
static int gpio_vreg_is_enabled(struct regulator_dev *rdev)
{
struct gpio_vreg *vreg = rdev_get_drvdata(rdev);
int rc;
rc = gpio_vreg_request_gpio(vreg);
if (rc < 0)
return rc;
return (gpio_get_value_cansleep(vreg->gpio) ? 1 : 0) ^ vreg->active_low;
}
static int gpio_vreg_enable(struct regulator_dev *rdev)
{
struct gpio_vreg *vreg = rdev_get_drvdata(rdev);
int rc;
rc = gpio_vreg_request_gpio(vreg);
if (rc < 0)
return rc;
return gpio_direction_output(vreg->gpio, !vreg->active_low);
}
static int gpio_vreg_disable(struct regulator_dev *rdev)
{
struct gpio_vreg *vreg = rdev_get_drvdata(rdev);
int rc;
rc = gpio_vreg_request_gpio(vreg);
if (rc < 0)
return rc;
return gpio_direction_output(vreg->gpio, vreg->active_low);
}
static struct regulator_ops gpio_vreg_ops = {
.enable = gpio_vreg_enable,
.disable = gpio_vreg_disable,
.is_enabled = gpio_vreg_is_enabled,
};
static int __devinit gpio_vreg_probe(struct platform_device *pdev)
{
const struct gpio_regulator_platform_data *pdata;
struct gpio_vreg *vreg;
int rc = 0;
pdata = pdev->dev.platform_data;
if (!pdata) {
pr_err("platform data required.\n");
return -EINVAL;
}
if (!pdata->gpio_label) {
pr_err("gpio_label required.\n");
return -EINVAL;
}
if (!pdata->regulator_name) {
pr_err("regulator_name required.\n");
return -EINVAL;
}
vreg = kzalloc(sizeof(struct gpio_vreg), GFP_KERNEL);
if (!vreg) {
pr_err("kzalloc failed.\n");
return -ENOMEM;
}
vreg->name = kstrdup(pdata->regulator_name, GFP_KERNEL);
if (!vreg->name) {
pr_err("kzalloc failed.\n");
rc = -ENOMEM;
goto free_vreg;
}
vreg->gpio_label = kstrdup(pdata->gpio_label, GFP_KERNEL);
if (!vreg->gpio_label) {
pr_err("kzalloc failed.\n");
rc = -ENOMEM;
goto free_name;
}
vreg->gpio = pdata->gpio;
vreg->active_low = (pdata->active_low ? 1 : 0);
vreg->gpio_requested = false;
vreg->desc.name = vreg->name;
vreg->desc.id = pdev->id;
vreg->desc.ops = &gpio_vreg_ops;
vreg->desc.type = REGULATOR_VOLTAGE;
vreg->desc.owner = THIS_MODULE;
vreg->rdev = regulator_register(&vreg->desc, &pdev->dev,
&pdata->init_data, vreg, NULL);
if (IS_ERR(vreg->rdev)) {
rc = PTR_ERR(vreg->rdev);
pr_err("%s: regulator_register failed, rc=%d.\n", vreg->name,
rc);
goto free_gpio_label;
}
platform_set_drvdata(pdev, vreg);
pr_info("id=%d, name=%s, gpio=%u, gpio_label=%s\n", pdev->id,
vreg->name, vreg->gpio, vreg->gpio_label);
return rc;
free_gpio_label:
kfree(vreg->gpio_label);
free_name:
kfree(vreg->name);
free_vreg:
kfree(vreg);
return rc;
}
static int __devexit gpio_vreg_remove(struct platform_device *pdev)
{
struct gpio_vreg *vreg = platform_get_drvdata(pdev);
if (vreg->gpio_requested)
gpio_free(vreg->gpio);
regulator_unregister(vreg->rdev);
kfree(vreg->name);
kfree(vreg->gpio_label);
kfree(vreg);
platform_set_drvdata(pdev, NULL);
return 0;
}
static struct platform_driver gpio_vreg_driver = {
.probe = gpio_vreg_probe,
.remove = __devexit_p(gpio_vreg_remove),
.driver = {
.name = GPIO_REGULATOR_DEV_NAME,
.owner = THIS_MODULE,
},
};
static int __init gpio_vreg_init(void)
{
return platform_driver_register(&gpio_vreg_driver);
}
static void __exit gpio_vreg_exit(void)
{
platform_driver_unregister(&gpio_vreg_driver);
}
postcore_initcall(gpio_vreg_init);
module_exit(gpio_vreg_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("GPIO regulator driver");
MODULE_VERSION("1.0");
MODULE_ALIAS("platform:" GPIO_REGULATOR_DEV_NAME);
|