4 * Copyright 2011 Heiko Stuebner <heiko@sntech.de>
8 * Copyright 2008 Wolfson Microelectronics PLC.
10 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * Copyright (c) 2009 Nokia Corporation
13 * Roger Quadros <ext-roger.quadros@nokia.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of the
18 * License, or (at your option) any later version.
20 * This is useful for systems with mixed controllable and
21 * non-controllable regulators, as well as for allowing testing on
22 * systems with no controllable regulators.
25 #include <linux/err.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/regulator/gpio-regulator.h>
32 #include <linux/gpio.h>
33 #include <linux/delay.h>
34 #include <linux/slab.h>
36 struct gpio_regulator_data
{
37 struct regulator_desc desc
;
38 struct regulator_dev
*dev
;
43 unsigned startup_delay
;
48 struct gpio_regulator_state
*states
;
54 static int gpio_regulator_is_enabled(struct regulator_dev
*dev
)
56 struct gpio_regulator_data
*data
= rdev_get_drvdata(dev
);
58 return data
->is_enabled
;
61 static int gpio_regulator_enable(struct regulator_dev
*dev
)
63 struct gpio_regulator_data
*data
= rdev_get_drvdata(dev
);
65 if (gpio_is_valid(data
->enable_gpio
)) {
66 gpio_set_value_cansleep(data
->enable_gpio
, data
->enable_high
);
67 data
->is_enabled
= true;
73 static int gpio_regulator_disable(struct regulator_dev
*dev
)
75 struct gpio_regulator_data
*data
= rdev_get_drvdata(dev
);
77 if (gpio_is_valid(data
->enable_gpio
)) {
78 gpio_set_value_cansleep(data
->enable_gpio
, !data
->enable_high
);
79 data
->is_enabled
= false;
85 static int gpio_regulator_enable_time(struct regulator_dev
*dev
)
87 struct gpio_regulator_data
*data
= rdev_get_drvdata(dev
);
89 return data
->startup_delay
;
92 static int gpio_regulator_get_value(struct regulator_dev
*dev
)
94 struct gpio_regulator_data
*data
= rdev_get_drvdata(dev
);
97 for (ptr
= 0; ptr
< data
->nr_states
; ptr
++)
98 if (data
->states
[ptr
].gpios
== data
->state
)
99 return data
->states
[ptr
].value
;
104 static int gpio_regulator_set_value(struct regulator_dev
*dev
,
107 struct gpio_regulator_data
*data
= rdev_get_drvdata(dev
);
108 int ptr
, target
, state
;
111 for (ptr
= 0; ptr
< data
->nr_states
; ptr
++)
112 if (data
->states
[ptr
].value
>= min
&&
113 data
->states
[ptr
].value
<= max
)
114 target
= data
->states
[ptr
].gpios
;
119 for (ptr
= 0; ptr
< data
->nr_gpios
; ptr
++) {
120 state
= (target
& (1 << ptr
)) >> ptr
;
121 gpio_set_value(data
->gpios
[ptr
].gpio
, state
);
123 data
->state
= target
;
128 static int gpio_regulator_set_voltage(struct regulator_dev
*dev
,
129 int min_uV
, int max_uV
,
132 return gpio_regulator_set_value(dev
, min_uV
, max_uV
);
135 static int gpio_regulator_list_voltage(struct regulator_dev
*dev
,
138 struct gpio_regulator_data
*data
= rdev_get_drvdata(dev
);
140 if (selector
>= data
->nr_states
)
143 return data
->states
[selector
].value
;
146 static int gpio_regulator_set_current_limit(struct regulator_dev
*dev
,
147 int min_uA
, int max_uA
)
149 return gpio_regulator_set_value(dev
, min_uA
, max_uA
);
152 static struct regulator_ops gpio_regulator_voltage_ops
= {
153 .is_enabled
= gpio_regulator_is_enabled
,
154 .enable
= gpio_regulator_enable
,
155 .disable
= gpio_regulator_disable
,
156 .enable_time
= gpio_regulator_enable_time
,
157 .get_voltage
= gpio_regulator_get_value
,
158 .set_voltage
= gpio_regulator_set_voltage
,
159 .list_voltage
= gpio_regulator_list_voltage
,
162 static struct regulator_ops gpio_regulator_current_ops
= {
163 .is_enabled
= gpio_regulator_is_enabled
,
164 .enable
= gpio_regulator_enable
,
165 .disable
= gpio_regulator_disable
,
166 .enable_time
= gpio_regulator_enable_time
,
167 .get_current_limit
= gpio_regulator_get_value
,
168 .set_current_limit
= gpio_regulator_set_current_limit
,
171 static int __devinit
gpio_regulator_probe(struct platform_device
*pdev
)
173 struct gpio_regulator_config
*config
= pdev
->dev
.platform_data
;
174 struct gpio_regulator_data
*drvdata
;
177 drvdata
= kzalloc(sizeof(struct gpio_regulator_data
), GFP_KERNEL
);
178 if (drvdata
== NULL
) {
179 dev_err(&pdev
->dev
, "Failed to allocate device data\n");
183 drvdata
->desc
.name
= kstrdup(config
->supply_name
, GFP_KERNEL
);
184 if (drvdata
->desc
.name
== NULL
) {
185 dev_err(&pdev
->dev
, "Failed to allocate supply name\n");
190 drvdata
->gpios
= kmemdup(config
->gpios
,
191 config
->nr_gpios
* sizeof(struct gpio
),
193 if (drvdata
->gpios
== NULL
) {
194 dev_err(&pdev
->dev
, "Failed to allocate gpio data\n");
199 drvdata
->states
= kmemdup(config
->states
,
201 sizeof(struct gpio_regulator_state
),
203 if (drvdata
->states
== NULL
) {
204 dev_err(&pdev
->dev
, "Failed to allocate state data\n");
208 drvdata
->nr_states
= config
->nr_states
;
210 drvdata
->desc
.owner
= THIS_MODULE
;
212 /* handle regulator type*/
213 switch (config
->type
) {
214 case REGULATOR_VOLTAGE
:
215 drvdata
->desc
.type
= REGULATOR_VOLTAGE
;
216 drvdata
->desc
.ops
= &gpio_regulator_voltage_ops
;
217 drvdata
->desc
.n_voltages
= config
->nr_states
;
219 case REGULATOR_CURRENT
:
220 drvdata
->desc
.type
= REGULATOR_CURRENT
;
221 drvdata
->desc
.ops
= &gpio_regulator_current_ops
;
224 dev_err(&pdev
->dev
, "No regulator type set\n");
230 drvdata
->enable_gpio
= config
->enable_gpio
;
231 drvdata
->startup_delay
= config
->startup_delay
;
233 if (gpio_is_valid(config
->enable_gpio
)) {
234 drvdata
->enable_high
= config
->enable_high
;
236 ret
= gpio_request(config
->enable_gpio
, config
->supply_name
);
239 "Could not obtain regulator enable GPIO %d: %d\n",
240 config
->enable_gpio
, ret
);
244 /* set output direction without changing state
247 if (config
->enabled_at_boot
) {
248 drvdata
->is_enabled
= true;
249 ret
= gpio_direction_output(config
->enable_gpio
,
250 config
->enable_high
);
252 drvdata
->is_enabled
= false;
253 ret
= gpio_direction_output(config
->enable_gpio
,
254 !config
->enable_high
);
259 "Could not configure regulator enable GPIO %d direction: %d\n",
260 config
->enable_gpio
, ret
);
264 /* Regulator without GPIO control is considered
267 drvdata
->is_enabled
= true;
270 drvdata
->nr_gpios
= config
->nr_gpios
;
271 ret
= gpio_request_array(drvdata
->gpios
, drvdata
->nr_gpios
);
274 "Could not obtain regulator setting GPIOs: %d\n", ret
);
278 /* build initial state from gpio init data. */
280 for (ptr
= 0; ptr
< drvdata
->nr_gpios
; ptr
++) {
281 if (config
->gpios
[ptr
].flags
& GPIOF_OUT_INIT_HIGH
)
284 drvdata
->state
= state
;
286 drvdata
->dev
= regulator_register(&drvdata
->desc
, &pdev
->dev
,
287 config
->init_data
, drvdata
);
288 if (IS_ERR(drvdata
->dev
)) {
289 ret
= PTR_ERR(drvdata
->dev
);
290 dev_err(&pdev
->dev
, "Failed to register regulator: %d\n", ret
);
294 platform_set_drvdata(pdev
, drvdata
);
299 gpio_free_array(drvdata
->gpios
, drvdata
->nr_gpios
);
301 if (gpio_is_valid(config
->enable_gpio
))
302 gpio_free(config
->enable_gpio
);
304 kfree(drvdata
->states
);
306 kfree(drvdata
->gpios
);
308 kfree(drvdata
->desc
.name
);
314 static int __devexit
gpio_regulator_remove(struct platform_device
*pdev
)
316 struct gpio_regulator_data
*drvdata
= platform_get_drvdata(pdev
);
318 regulator_unregister(drvdata
->dev
);
320 gpio_free_array(drvdata
->gpios
, drvdata
->nr_gpios
);
322 kfree(drvdata
->states
);
323 kfree(drvdata
->gpios
);
325 if (gpio_is_valid(drvdata
->enable_gpio
))
326 gpio_free(drvdata
->enable_gpio
);
328 kfree(drvdata
->desc
.name
);
334 static struct platform_driver gpio_regulator_driver
= {
335 .probe
= gpio_regulator_probe
,
336 .remove
= __devexit_p(gpio_regulator_remove
),
338 .name
= "gpio-regulator",
339 .owner
= THIS_MODULE
,
343 static int __init
gpio_regulator_init(void)
345 return platform_driver_register(&gpio_regulator_driver
);
347 subsys_initcall(gpio_regulator_init
);
349 static void __exit
gpio_regulator_exit(void)
351 platform_driver_unregister(&gpio_regulator_driver
);
353 module_exit(gpio_regulator_exit
);
355 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
356 MODULE_DESCRIPTION("gpio voltage regulator");
357 MODULE_LICENSE("GPL");
358 MODULE_ALIAS("platform:gpio-regulator");