4 * Copyright 2008 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * Copyright (c) 2009 Nokia Corporation
9 * Roger Quadros <ext-roger.quadros@nokia.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
16 * This is useful for systems with mixed controllable and
17 * non-controllable regulators, as well as for allowing testing on
18 * systems with no controllable regulators.
21 #include <linux/err.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/fixed.h>
26 #include <linux/gpio.h>
27 #include <linux/delay.h>
29 struct fixed_voltage_data
{
30 struct regulator_desc desc
;
31 struct regulator_dev
*dev
;
34 unsigned startup_delay
;
35 unsigned enable_high
:1;
36 unsigned is_enabled
:1;
39 static int fixed_voltage_is_enabled(struct regulator_dev
*dev
)
41 struct fixed_voltage_data
*data
= rdev_get_drvdata(dev
);
43 return data
->is_enabled
;
46 static int fixed_voltage_enable(struct regulator_dev
*dev
)
48 struct fixed_voltage_data
*data
= rdev_get_drvdata(dev
);
50 if (gpio_is_valid(data
->gpio
)) {
51 gpio_set_value_cansleep(data
->gpio
, data
->enable_high
);
58 static int fixed_voltage_disable(struct regulator_dev
*dev
)
60 struct fixed_voltage_data
*data
= rdev_get_drvdata(dev
);
62 if (gpio_is_valid(data
->gpio
)) {
63 gpio_set_value_cansleep(data
->gpio
, !data
->enable_high
);
70 static int fixed_voltage_enable_time(struct regulator_dev
*dev
)
72 struct fixed_voltage_data
*data
= rdev_get_drvdata(dev
);
74 return data
->startup_delay
;
77 static int fixed_voltage_get_voltage(struct regulator_dev
*dev
)
79 struct fixed_voltage_data
*data
= rdev_get_drvdata(dev
);
81 return data
->microvolts
;
84 static int fixed_voltage_list_voltage(struct regulator_dev
*dev
,
87 struct fixed_voltage_data
*data
= rdev_get_drvdata(dev
);
92 return data
->microvolts
;
95 static struct regulator_ops fixed_voltage_ops
= {
96 .is_enabled
= fixed_voltage_is_enabled
,
97 .enable
= fixed_voltage_enable
,
98 .disable
= fixed_voltage_disable
,
99 .enable_time
= fixed_voltage_enable_time
,
100 .get_voltage
= fixed_voltage_get_voltage
,
101 .list_voltage
= fixed_voltage_list_voltage
,
104 static int regulator_fixed_voltage_probe(struct platform_device
*pdev
)
106 struct fixed_voltage_config
*config
= pdev
->dev
.platform_data
;
107 struct fixed_voltage_data
*drvdata
;
110 drvdata
= kzalloc(sizeof(struct fixed_voltage_data
), GFP_KERNEL
);
111 if (drvdata
== NULL
) {
112 dev_err(&pdev
->dev
, "Failed to allocate device data\n");
117 drvdata
->desc
.name
= kstrdup(config
->supply_name
, GFP_KERNEL
);
118 if (drvdata
->desc
.name
== NULL
) {
119 dev_err(&pdev
->dev
, "Failed to allocate supply name\n");
123 drvdata
->desc
.type
= REGULATOR_VOLTAGE
;
124 drvdata
->desc
.owner
= THIS_MODULE
;
125 drvdata
->desc
.ops
= &fixed_voltage_ops
;
126 drvdata
->desc
.n_voltages
= 1;
128 drvdata
->microvolts
= config
->microvolts
;
129 drvdata
->gpio
= config
->gpio
;
130 drvdata
->startup_delay
= config
->startup_delay
;
132 if (gpio_is_valid(config
->gpio
)) {
133 drvdata
->enable_high
= config
->enable_high
;
135 /* FIXME: Remove below print warning
137 * config->gpio must be set to -EINVAL by platform code if
138 * GPIO control is not required. However, early adopters
139 * not requiring GPIO control may forget to initialize
140 * config->gpio to -EINVAL. This will cause GPIO 0 to be used
143 * This warning will be removed once there are a couple of users
148 "using GPIO 0 for regulator enable control\n");
150 ret
= gpio_request(config
->gpio
, config
->supply_name
);
153 "Could not obtain regulator enable GPIO %d: %d\n",
158 /* set output direction without changing state
161 drvdata
->is_enabled
= config
->enabled_at_boot
;
162 ret
= drvdata
->is_enabled
?
163 config
->enable_high
: !config
->enable_high
;
165 ret
= gpio_direction_output(config
->gpio
, ret
);
168 "Could not configure regulator enable GPIO %d direction: %d\n",
174 /* Regulator without GPIO control is considered
177 drvdata
->is_enabled
= 1;
180 drvdata
->dev
= regulator_register(&drvdata
->desc
, &pdev
->dev
,
181 config
->init_data
, drvdata
);
182 if (IS_ERR(drvdata
->dev
)) {
183 ret
= PTR_ERR(drvdata
->dev
);
184 dev_err(&pdev
->dev
, "Failed to register regulator: %d\n", ret
);
188 platform_set_drvdata(pdev
, drvdata
);
190 dev_dbg(&pdev
->dev
, "%s supplying %duV\n", drvdata
->desc
.name
,
191 drvdata
->microvolts
);
196 if (gpio_is_valid(config
->gpio
))
197 gpio_free(config
->gpio
);
199 kfree(drvdata
->desc
.name
);
205 static int regulator_fixed_voltage_remove(struct platform_device
*pdev
)
207 struct fixed_voltage_data
*drvdata
= platform_get_drvdata(pdev
);
209 regulator_unregister(drvdata
->dev
);
210 if (gpio_is_valid(drvdata
->gpio
))
211 gpio_free(drvdata
->gpio
);
212 kfree(drvdata
->desc
.name
);
218 static struct platform_driver regulator_fixed_voltage_driver
= {
219 .probe
= regulator_fixed_voltage_probe
,
220 .remove
= regulator_fixed_voltage_remove
,
222 .name
= "reg-fixed-voltage",
226 static int __init
regulator_fixed_voltage_init(void)
228 return platform_driver_register(®ulator_fixed_voltage_driver
);
230 subsys_initcall(regulator_fixed_voltage_init
);
232 static void __exit
regulator_fixed_voltage_exit(void)
234 platform_driver_unregister(®ulator_fixed_voltage_driver
);
236 module_exit(regulator_fixed_voltage_exit
);
238 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
239 MODULE_DESCRIPTION("Fixed voltage regulator");
240 MODULE_LICENSE("GPL");
241 MODULE_ALIAS("platform:reg-fixed-voltage");