blob: 5adc35aa6cc70b67a604b08ca626aec5e887b3e1 [file] [log] [blame]
Mark Brown4b74ff62008-04-30 16:27:12 +01001/*
2 * fixed.c
3 *
4 * Copyright 2008 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
Roger Quadros86d98842009-08-06 19:37:29 +03008 * Copyright (c) 2009 Nokia Corporation
9 * Roger Quadros <ext-roger.quadros@nokia.com>
10 *
Mark Brown4b74ff62008-04-30 16:27:12 +010011 * 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.
15 *
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.
19 */
20
21#include <linux/err.h>
22#include <linux/mutex.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040023#include <linux/module.h>
Mark Brown4b74ff62008-04-30 16:27:12 +010024#include <linux/platform_device.h>
25#include <linux/regulator/driver.h>
26#include <linux/regulator/fixed.h>
Roger Quadros86d98842009-08-06 19:37:29 +030027#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Rajendra Nayakcef49102011-11-18 16:47:18 +053029#include <linux/of.h>
30#include <linux/of_gpio.h>
31#include <linux/regulator/of_regulator.h>
32#include <linux/regulator/machine.h>
Mark Brown4b74ff62008-04-30 16:27:12 +010033
34struct fixed_voltage_data {
35 struct regulator_desc desc;
36 struct regulator_dev *dev;
37 int microvolts;
Roger Quadros86d98842009-08-06 19:37:29 +030038 int gpio;
Dmitry Torokhov8ab33432010-02-23 23:37:55 -080039 bool enable_high;
40 bool is_enabled;
Mark Brown4b74ff62008-04-30 16:27:12 +010041};
42
Rajendra Nayakcef49102011-11-18 16:47:18 +053043
44/**
45 * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
46 * @dev: device requesting for fixed_voltage_config
47 *
48 * Populates fixed_voltage_config structure by extracting data from device
49 * tree node, returns a pointer to the populated structure of NULL if memory
50 * alloc fails.
51 */
Axel Linbc913962011-11-27 10:35:08 +080052static struct fixed_voltage_config *
53of_get_fixed_voltage_config(struct device *dev)
Rajendra Nayakcef49102011-11-18 16:47:18 +053054{
55 struct fixed_voltage_config *config;
56 struct device_node *np = dev->of_node;
57 const __be32 *delay;
58 struct regulator_init_data *init_data;
59
60 config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
61 GFP_KERNEL);
62 if (!config)
Stephen Warrenf1418222012-06-29 10:33:15 -060063 return ERR_PTR(-ENOMEM);
Rajendra Nayakcef49102011-11-18 16:47:18 +053064
Shawn Guod9a861c2011-12-01 17:21:06 +080065 config->init_data = of_get_regulator_init_data(dev, dev->of_node);
Axel Lin4b864af2011-11-26 20:19:19 +080066 if (!config->init_data)
Stephen Warrenf1418222012-06-29 10:33:15 -060067 return ERR_PTR(-EINVAL);
Axel Lin4b864af2011-11-26 20:19:19 +080068
Rajendra Nayakcef49102011-11-18 16:47:18 +053069 init_data = config->init_data;
Richard Zhao0c437c42012-01-04 11:07:29 +080070 init_data->constraints.apply_uV = 0;
Rajendra Nayakcef49102011-11-18 16:47:18 +053071
72 config->supply_name = init_data->constraints.name;
73 if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
74 config->microvolts = init_data->constraints.min_uV;
75 } else {
76 dev_err(dev,
77 "Fixed regulator specified with variable voltages\n");
Stephen Warrenf1418222012-06-29 10:33:15 -060078 return ERR_PTR(-EINVAL);
Rajendra Nayakcef49102011-11-18 16:47:18 +053079 }
80
81 if (init_data->constraints.boot_on)
82 config->enabled_at_boot = true;
83
84 config->gpio = of_get_named_gpio(np, "gpio", 0);
Stephen Warrenf1418222012-06-29 10:33:15 -060085 /*
86 * of_get_named_gpio() currently returns ENODEV rather than
87 * EPROBE_DEFER. This code attempts to be compatible with both
88 * for now; the ENODEV check can be removed once the API is fixed.
89 * of_get_named_gpio() doesn't differentiate between a missing
90 * property (which would be fine here, since the GPIO is optional)
91 * and some other error. Patches have been posted for both issues.
92 * Once they are check in, we should replace this with:
93 * if (config->gpio < 0 && config->gpio != -ENOENT)
94 */
95 if ((config->gpio == -ENODEV) || (config->gpio == -EPROBE_DEFER))
96 return ERR_PTR(-EPROBE_DEFER);
97
Rajendra Nayakcef49102011-11-18 16:47:18 +053098 delay = of_get_property(np, "startup-delay-us", NULL);
99 if (delay)
100 config->startup_delay = be32_to_cpu(*delay);
101
102 if (of_find_property(np, "enable-active-high", NULL))
103 config->enable_high = true;
104
Laxman Dewangan9a50dba2012-05-07 15:58:19 +0530105 if (of_find_property(np, "gpio-open-drain", NULL))
106 config->gpio_is_open_drain = true;
107
Rajendra Nayakcef49102011-11-18 16:47:18 +0530108 return config;
109}
110
Mark Brown4b74ff62008-04-30 16:27:12 +0100111static int fixed_voltage_is_enabled(struct regulator_dev *dev)
112{
Roger Quadros86d98842009-08-06 19:37:29 +0300113 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
114
115 return data->is_enabled;
Mark Brown4b74ff62008-04-30 16:27:12 +0100116}
117
118static int fixed_voltage_enable(struct regulator_dev *dev)
119{
Roger Quadros86d98842009-08-06 19:37:29 +0300120 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
121
Mark Brown9d442062012-03-21 20:04:44 +0000122 gpio_set_value_cansleep(data->gpio, data->enable_high);
123 data->is_enabled = true;
Roger Quadros86d98842009-08-06 19:37:29 +0300124
125 return 0;
126}
127
128static int fixed_voltage_disable(struct regulator_dev *dev)
129{
130 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
131
Mark Brown9d442062012-03-21 20:04:44 +0000132 gpio_set_value_cansleep(data->gpio, !data->enable_high);
133 data->is_enabled = false;
Roger Quadros86d98842009-08-06 19:37:29 +0300134
Mark Brown4b74ff62008-04-30 16:27:12 +0100135 return 0;
136}
137
138static int fixed_voltage_get_voltage(struct regulator_dev *dev)
139{
140 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
141
Mark Brownaebe4952011-11-02 11:38:45 +0000142 if (data->microvolts)
143 return data->microvolts;
144 else
145 return -EINVAL;
Mark Brown4b74ff62008-04-30 16:27:12 +0100146}
147
Mark Brown9035cef2009-04-28 11:13:54 +0100148static int fixed_voltage_list_voltage(struct regulator_dev *dev,
149 unsigned selector)
150{
151 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
152
153 if (selector != 0)
154 return -EINVAL;
155
156 return data->microvolts;
157}
158
Mark Brown9d442062012-03-21 20:04:44 +0000159static struct regulator_ops fixed_voltage_gpio_ops = {
Mark Brown4b74ff62008-04-30 16:27:12 +0100160 .is_enabled = fixed_voltage_is_enabled,
161 .enable = fixed_voltage_enable,
Roger Quadros86d98842009-08-06 19:37:29 +0300162 .disable = fixed_voltage_disable,
Mark Brown4b74ff62008-04-30 16:27:12 +0100163 .get_voltage = fixed_voltage_get_voltage,
Mark Brown9035cef2009-04-28 11:13:54 +0100164 .list_voltage = fixed_voltage_list_voltage,
Mark Brown4b74ff62008-04-30 16:27:12 +0100165};
166
Mark Brown9d442062012-03-21 20:04:44 +0000167static struct regulator_ops fixed_voltage_ops = {
168 .get_voltage = fixed_voltage_get_voltage,
169 .list_voltage = fixed_voltage_list_voltage,
170};
171
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800172static int __devinit reg_fixed_voltage_probe(struct platform_device *pdev)
Mark Brown4b74ff62008-04-30 16:27:12 +0100173{
Axel Lin22d881c2011-11-27 20:07:57 +0800174 struct fixed_voltage_config *config;
Mark Brown4b74ff62008-04-30 16:27:12 +0100175 struct fixed_voltage_data *drvdata;
Mark Brownc172708d2012-04-04 00:50:22 +0100176 struct regulator_config cfg = { };
Mark Brown4b74ff62008-04-30 16:27:12 +0100177 int ret;
178
Stephen Warrenf1418222012-06-29 10:33:15 -0600179 if (pdev->dev.of_node) {
Rajendra Nayakcef49102011-11-18 16:47:18 +0530180 config = of_get_fixed_voltage_config(&pdev->dev);
Stephen Warrenf1418222012-06-29 10:33:15 -0600181 if (IS_ERR(config))
182 return PTR_ERR(config);
183 } else {
Axel Lin22d881c2011-11-27 20:07:57 +0800184 config = pdev->dev.platform_data;
Stephen Warrenf1418222012-06-29 10:33:15 -0600185 }
Axel Lin22d881c2011-11-27 20:07:57 +0800186
187 if (!config)
188 return -ENOMEM;
Rajendra Nayakcef49102011-11-18 16:47:18 +0530189
Mark Brownc45bb352012-03-21 18:13:29 +0000190 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
191 GFP_KERNEL);
Mark Brown4b74ff62008-04-30 16:27:12 +0100192 if (drvdata == NULL) {
Mark Brownc53ad7f2009-08-03 18:49:53 +0100193 dev_err(&pdev->dev, "Failed to allocate device data\n");
Mark Brown4b74ff62008-04-30 16:27:12 +0100194 ret = -ENOMEM;
195 goto err;
196 }
197
198 drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
199 if (drvdata->desc.name == NULL) {
Mark Brownc53ad7f2009-08-03 18:49:53 +0100200 dev_err(&pdev->dev, "Failed to allocate supply name\n");
Mark Brown4b74ff62008-04-30 16:27:12 +0100201 ret = -ENOMEM;
202 goto err;
203 }
204 drvdata->desc.type = REGULATOR_VOLTAGE;
205 drvdata->desc.owner = THIS_MODULE;
Sascha Hauer1c37f8a2012-03-03 12:40:01 +0100206
Mark Brown3d0f2672012-06-27 14:27:22 +0100207 drvdata->desc.enable_time = config->startup_delay;
208
Sascha Hauer1c37f8a2012-03-03 12:40:01 +0100209 if (config->microvolts)
210 drvdata->desc.n_voltages = 1;
Mark Brown4b74ff62008-04-30 16:27:12 +0100211
212 drvdata->microvolts = config->microvolts;
Roger Quadros86d98842009-08-06 19:37:29 +0300213 drvdata->gpio = config->gpio;
214
215 if (gpio_is_valid(config->gpio)) {
Laxman Dewangana4d9f172012-03-07 15:58:33 +0530216 int gpio_flag;
Roger Quadros86d98842009-08-06 19:37:29 +0300217 drvdata->enable_high = config->enable_high;
218
219 /* FIXME: Remove below print warning
220 *
221 * config->gpio must be set to -EINVAL by platform code if
222 * GPIO control is not required. However, early adopters
223 * not requiring GPIO control may forget to initialize
224 * config->gpio to -EINVAL. This will cause GPIO 0 to be used
225 * for GPIO control.
226 *
227 * This warning will be removed once there are a couple of users
228 * for this driver.
229 */
230 if (!config->gpio)
231 dev_warn(&pdev->dev,
232 "using GPIO 0 for regulator enable control\n");
233
Laxman Dewangana4d9f172012-03-07 15:58:33 +0530234 /*
235 * set output direction without changing state
Roger Quadros86d98842009-08-06 19:37:29 +0300236 * to prevent glitch
237 */
238 drvdata->is_enabled = config->enabled_at_boot;
239 ret = drvdata->is_enabled ?
240 config->enable_high : !config->enable_high;
Laxman Dewangana4d9f172012-03-07 15:58:33 +0530241 gpio_flag = ret ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
Roger Quadros86d98842009-08-06 19:37:29 +0300242
Laxman Dewangana4d9f172012-03-07 15:58:33 +0530243 if (config->gpio_is_open_drain)
244 gpio_flag |= GPIOF_OPEN_DRAIN;
245
246 ret = gpio_request_one(config->gpio, gpio_flag,
247 config->supply_name);
Roger Quadros86d98842009-08-06 19:37:29 +0300248 if (ret) {
249 dev_err(&pdev->dev,
Laxman Dewangana4d9f172012-03-07 15:58:33 +0530250 "Could not obtain regulator enable GPIO %d: %d\n",
Roger Quadros86d98842009-08-06 19:37:29 +0300251 config->gpio, ret);
Laxman Dewangana4d9f172012-03-07 15:58:33 +0530252 goto err_name;
Roger Quadros86d98842009-08-06 19:37:29 +0300253 }
254
Mark Brown9d442062012-03-21 20:04:44 +0000255 drvdata->desc.ops = &fixed_voltage_gpio_ops;
256
Roger Quadros86d98842009-08-06 19:37:29 +0300257 } else {
Mark Brown9d442062012-03-21 20:04:44 +0000258 drvdata->desc.ops = &fixed_voltage_ops;
Roger Quadros86d98842009-08-06 19:37:29 +0300259 }
Mark Brown4b74ff62008-04-30 16:27:12 +0100260
Mark Brownc172708d2012-04-04 00:50:22 +0100261 cfg.dev = &pdev->dev;
262 cfg.init_data = config->init_data;
263 cfg.driver_data = drvdata;
264 cfg.of_node = pdev->dev.of_node;
265
266 drvdata->dev = regulator_register(&drvdata->desc, &cfg);
Mark Brown4b74ff62008-04-30 16:27:12 +0100267 if (IS_ERR(drvdata->dev)) {
268 ret = PTR_ERR(drvdata->dev);
Mark Brownc53ad7f2009-08-03 18:49:53 +0100269 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
Roger Quadros86d98842009-08-06 19:37:29 +0300270 goto err_gpio;
Mark Brown4b74ff62008-04-30 16:27:12 +0100271 }
272
273 platform_set_drvdata(pdev, drvdata);
274
275 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
276 drvdata->microvolts);
277
278 return 0;
279
Roger Quadros86d98842009-08-06 19:37:29 +0300280err_gpio:
281 if (gpio_is_valid(config->gpio))
282 gpio_free(config->gpio);
Mark Brown4b74ff62008-04-30 16:27:12 +0100283err_name:
284 kfree(drvdata->desc.name);
285err:
Mark Brown4b74ff62008-04-30 16:27:12 +0100286 return ret;
287}
288
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800289static int __devexit reg_fixed_voltage_remove(struct platform_device *pdev)
Mark Brown4b74ff62008-04-30 16:27:12 +0100290{
291 struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
292
293 regulator_unregister(drvdata->dev);
Roger Quadros86d98842009-08-06 19:37:29 +0300294 if (gpio_is_valid(drvdata->gpio))
295 gpio_free(drvdata->gpio);
Dan Carpenter80099c72009-11-16 11:05:03 +0200296 kfree(drvdata->desc.name);
Roger Quadros86d98842009-08-06 19:37:29 +0300297
Mark Brown4b74ff62008-04-30 16:27:12 +0100298 return 0;
299}
300
Rajendra Nayakcef49102011-11-18 16:47:18 +0530301#if defined(CONFIG_OF)
302static const struct of_device_id fixed_of_match[] __devinitconst = {
303 { .compatible = "regulator-fixed", },
304 {},
305};
306MODULE_DEVICE_TABLE(of, fixed_of_match);
Rajendra Nayakcef49102011-11-18 16:47:18 +0530307#endif
308
Mark Brown4b74ff62008-04-30 16:27:12 +0100309static struct platform_driver regulator_fixed_voltage_driver = {
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800310 .probe = reg_fixed_voltage_probe,
311 .remove = __devexit_p(reg_fixed_voltage_remove),
Mark Brown4b74ff62008-04-30 16:27:12 +0100312 .driver = {
313 .name = "reg-fixed-voltage",
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800314 .owner = THIS_MODULE,
Axel Linabcfaf22012-06-01 12:16:25 +0800315 .of_match_table = of_match_ptr(fixed_of_match),
Mark Brown4b74ff62008-04-30 16:27:12 +0100316 },
317};
318
319static int __init regulator_fixed_voltage_init(void)
320{
321 return platform_driver_register(&regulator_fixed_voltage_driver);
322}
Mark Brown5a1b22b2009-04-27 18:21:18 +0100323subsys_initcall(regulator_fixed_voltage_init);
Mark Brown4b74ff62008-04-30 16:27:12 +0100324
325static void __exit regulator_fixed_voltage_exit(void)
326{
327 platform_driver_unregister(&regulator_fixed_voltage_driver);
328}
329module_exit(regulator_fixed_voltage_exit);
330
331MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
332MODULE_DESCRIPTION("Fixed voltage regulator");
333MODULE_LICENSE("GPL");
Mark Brown38c53c82009-04-28 11:13:55 +0100334MODULE_ALIAS("platform:reg-fixed-voltage");