blob: d0410a40b5d21c7bdd12677d6dbebd08d737c5f2 [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>
Adrian Huntereda79a32010-01-12 12:25:13 +020028#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Rajendra Nayakcef49102011-11-18 16:47:18 +053030#include <linux/of.h>
31#include <linux/of_gpio.h>
32#include <linux/regulator/of_regulator.h>
33#include <linux/regulator/machine.h>
Mark Brown4b74ff62008-04-30 16:27:12 +010034
35struct fixed_voltage_data {
36 struct regulator_desc desc;
37 struct regulator_dev *dev;
38 int microvolts;
Roger Quadros86d98842009-08-06 19:37:29 +030039 int gpio;
Adrian Huntereda79a32010-01-12 12:25:13 +020040 unsigned startup_delay;
Dmitry Torokhov8ab33432010-02-23 23:37:55 -080041 bool enable_high;
42 bool is_enabled;
Mark Brown4b74ff62008-04-30 16:27:12 +010043};
44
Rajendra Nayakcef49102011-11-18 16:47:18 +053045
46/**
47 * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
48 * @dev: device requesting for fixed_voltage_config
49 *
50 * Populates fixed_voltage_config structure by extracting data from device
51 * tree node, returns a pointer to the populated structure of NULL if memory
52 * alloc fails.
53 */
Axel Linbc913962011-11-27 10:35:08 +080054static struct fixed_voltage_config *
55of_get_fixed_voltage_config(struct device *dev)
Rajendra Nayakcef49102011-11-18 16:47:18 +053056{
57 struct fixed_voltage_config *config;
58 struct device_node *np = dev->of_node;
59 const __be32 *delay;
60 struct regulator_init_data *init_data;
61
62 config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
63 GFP_KERNEL);
64 if (!config)
Stephen Warren2965b732012-06-29 10:33:15 -060065 return ERR_PTR(-ENOMEM);
Rajendra Nayakcef49102011-11-18 16:47:18 +053066
Shawn Guod9a861c2011-12-01 17:21:06 +080067 config->init_data = of_get_regulator_init_data(dev, dev->of_node);
Axel Lin4b864af2011-11-26 20:19:19 +080068 if (!config->init_data)
Stephen Warren2965b732012-06-29 10:33:15 -060069 return ERR_PTR(-EINVAL);
Axel Lin4b864af2011-11-26 20:19:19 +080070
Rajendra Nayakcef49102011-11-18 16:47:18 +053071 init_data = config->init_data;
Richard Zhao0c437c42012-01-04 11:07:29 +080072 init_data->constraints.apply_uV = 0;
Rajendra Nayakcef49102011-11-18 16:47:18 +053073
74 config->supply_name = init_data->constraints.name;
75 if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
76 config->microvolts = init_data->constraints.min_uV;
77 } else {
78 dev_err(dev,
79 "Fixed regulator specified with variable voltages\n");
Stephen Warren2965b732012-06-29 10:33:15 -060080 return ERR_PTR(-EINVAL);
Rajendra Nayakcef49102011-11-18 16:47:18 +053081 }
82
83 if (init_data->constraints.boot_on)
84 config->enabled_at_boot = true;
85
86 config->gpio = of_get_named_gpio(np, "gpio", 0);
Stephen Warren2965b732012-06-29 10:33:15 -060087 /*
88 * of_get_named_gpio() currently returns ENODEV rather than
89 * EPROBE_DEFER. This code attempts to be compatible with both
90 * for now; the ENODEV check can be removed once the API is fixed.
91 * of_get_named_gpio() doesn't differentiate between a missing
92 * property (which would be fine here, since the GPIO is optional)
93 * and some other error. Patches have been posted for both issues.
94 * Once they are check in, we should replace this with:
95 * if (config->gpio < 0 && config->gpio != -ENOENT)
96 */
97 if ((config->gpio == -ENODEV) || (config->gpio == -EPROBE_DEFER))
98 return ERR_PTR(-EPROBE_DEFER);
99
Rajendra Nayakcef49102011-11-18 16:47:18 +0530100 delay = of_get_property(np, "startup-delay-us", NULL);
101 if (delay)
102 config->startup_delay = be32_to_cpu(*delay);
103
104 if (of_find_property(np, "enable-active-high", NULL))
105 config->enable_high = true;
106
David Collinsb6d30f12012-12-20 11:40:17 -0800107 if (of_find_property(np, "parent-supply", NULL))
108 init_data->supply_regulator = "parent";
109
Rajendra Nayakcef49102011-11-18 16:47:18 +0530110 return config;
111}
112
Mark Brown4b74ff62008-04-30 16:27:12 +0100113static int fixed_voltage_is_enabled(struct regulator_dev *dev)
114{
Roger Quadros86d98842009-08-06 19:37:29 +0300115 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
116
117 return data->is_enabled;
Mark Brown4b74ff62008-04-30 16:27:12 +0100118}
119
120static int fixed_voltage_enable(struct regulator_dev *dev)
121{
Roger Quadros86d98842009-08-06 19:37:29 +0300122 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
123
124 if (gpio_is_valid(data->gpio)) {
125 gpio_set_value_cansleep(data->gpio, data->enable_high);
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800126 data->is_enabled = true;
Roger Quadros86d98842009-08-06 19:37:29 +0300127 }
128
129 return 0;
130}
131
132static int fixed_voltage_disable(struct regulator_dev *dev)
133{
134 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
135
136 if (gpio_is_valid(data->gpio)) {
137 gpio_set_value_cansleep(data->gpio, !data->enable_high);
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800138 data->is_enabled = false;
Roger Quadros86d98842009-08-06 19:37:29 +0300139 }
140
Mark Brown4b74ff62008-04-30 16:27:12 +0100141 return 0;
142}
143
Mark Brown17133dc2010-01-28 10:21:05 +0000144static int fixed_voltage_enable_time(struct regulator_dev *dev)
145{
146 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
147
148 return data->startup_delay;
149}
150
Mark Brown4b74ff62008-04-30 16:27:12 +0100151static int fixed_voltage_get_voltage(struct regulator_dev *dev)
152{
153 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
154
Mark Brownaebe4952011-11-02 11:38:45 +0000155 if (data->microvolts)
156 return data->microvolts;
157 else
158 return -EINVAL;
Mark Brown4b74ff62008-04-30 16:27:12 +0100159}
160
Mark Brown9035cef2009-04-28 11:13:54 +0100161static int fixed_voltage_list_voltage(struct regulator_dev *dev,
162 unsigned selector)
163{
164 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
165
166 if (selector != 0)
167 return -EINVAL;
168
169 return data->microvolts;
170}
171
Mark Brown4b74ff62008-04-30 16:27:12 +0100172static struct regulator_ops fixed_voltage_ops = {
173 .is_enabled = fixed_voltage_is_enabled,
174 .enable = fixed_voltage_enable,
Roger Quadros86d98842009-08-06 19:37:29 +0300175 .disable = fixed_voltage_disable,
Mark Brown17133dc2010-01-28 10:21:05 +0000176 .enable_time = fixed_voltage_enable_time,
Mark Brown4b74ff62008-04-30 16:27:12 +0100177 .get_voltage = fixed_voltage_get_voltage,
Mark Brown9035cef2009-04-28 11:13:54 +0100178 .list_voltage = fixed_voltage_list_voltage,
Mark Brown4b74ff62008-04-30 16:27:12 +0100179};
180
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800181static int __devinit reg_fixed_voltage_probe(struct platform_device *pdev)
Mark Brown4b74ff62008-04-30 16:27:12 +0100182{
Axel Lin22d881c2011-11-27 20:07:57 +0800183 struct fixed_voltage_config *config;
Mark Brown4b74ff62008-04-30 16:27:12 +0100184 struct fixed_voltage_data *drvdata;
185 int ret;
186
Stephen Warren2965b732012-06-29 10:33:15 -0600187 if (pdev->dev.of_node) {
Rajendra Nayakcef49102011-11-18 16:47:18 +0530188 config = of_get_fixed_voltage_config(&pdev->dev);
Stephen Warren2965b732012-06-29 10:33:15 -0600189 if (IS_ERR(config))
190 return PTR_ERR(config);
191 } else {
Axel Lin22d881c2011-11-27 20:07:57 +0800192 config = pdev->dev.platform_data;
Stephen Warren2965b732012-06-29 10:33:15 -0600193 }
Axel Lin22d881c2011-11-27 20:07:57 +0800194
195 if (!config)
196 return -ENOMEM;
Rajendra Nayakcef49102011-11-18 16:47:18 +0530197
Mark Brown4b74ff62008-04-30 16:27:12 +0100198 drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL);
199 if (drvdata == NULL) {
Mark Brownc53ad7f2009-08-03 18:49:53 +0100200 dev_err(&pdev->dev, "Failed to allocate device data\n");
Mark Brown4b74ff62008-04-30 16:27:12 +0100201 ret = -ENOMEM;
202 goto err;
203 }
204
205 drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
206 if (drvdata->desc.name == NULL) {
Mark Brownc53ad7f2009-08-03 18:49:53 +0100207 dev_err(&pdev->dev, "Failed to allocate supply name\n");
Mark Brown4b74ff62008-04-30 16:27:12 +0100208 ret = -ENOMEM;
209 goto err;
210 }
211 drvdata->desc.type = REGULATOR_VOLTAGE;
212 drvdata->desc.owner = THIS_MODULE;
Mark Brown9035cef2009-04-28 11:13:54 +0100213 drvdata->desc.ops = &fixed_voltage_ops;
Sascha Hauer1c37f8a2012-03-03 12:40:01 +0100214
215 if (config->microvolts)
216 drvdata->desc.n_voltages = 1;
Mark Brown4b74ff62008-04-30 16:27:12 +0100217
218 drvdata->microvolts = config->microvolts;
Roger Quadros86d98842009-08-06 19:37:29 +0300219 drvdata->gpio = config->gpio;
Adrian Huntereda79a32010-01-12 12:25:13 +0200220 drvdata->startup_delay = config->startup_delay;
Roger Quadros86d98842009-08-06 19:37:29 +0300221
222 if (gpio_is_valid(config->gpio)) {
223 drvdata->enable_high = config->enable_high;
224
225 /* FIXME: Remove below print warning
226 *
227 * config->gpio must be set to -EINVAL by platform code if
228 * GPIO control is not required. However, early adopters
229 * not requiring GPIO control may forget to initialize
230 * config->gpio to -EINVAL. This will cause GPIO 0 to be used
231 * for GPIO control.
232 *
233 * This warning will be removed once there are a couple of users
234 * for this driver.
235 */
236 if (!config->gpio)
237 dev_warn(&pdev->dev,
238 "using GPIO 0 for regulator enable control\n");
239
240 ret = gpio_request(config->gpio, config->supply_name);
241 if (ret) {
242 dev_err(&pdev->dev,
243 "Could not obtain regulator enable GPIO %d: %d\n",
244 config->gpio, ret);
245 goto err_name;
246 }
247
248 /* set output direction without changing state
249 * to prevent glitch
250 */
251 drvdata->is_enabled = config->enabled_at_boot;
252 ret = drvdata->is_enabled ?
253 config->enable_high : !config->enable_high;
254
255 ret = gpio_direction_output(config->gpio, ret);
256 if (ret) {
257 dev_err(&pdev->dev,
258 "Could not configure regulator enable GPIO %d direction: %d\n",
259 config->gpio, ret);
260 goto err_gpio;
261 }
262
263 } else {
264 /* Regulator without GPIO control is considered
265 * always enabled
266 */
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800267 drvdata->is_enabled = true;
Roger Quadros86d98842009-08-06 19:37:29 +0300268 }
Mark Brown4b74ff62008-04-30 16:27:12 +0100269
Mark Brownbcf34022009-01-19 13:37:04 +0000270 drvdata->dev = regulator_register(&drvdata->desc, &pdev->dev,
Richard Zhao1bb50b22011-12-31 20:51:16 +0800271 config->init_data, drvdata,
272 pdev->dev.of_node);
Mark Brown4b74ff62008-04-30 16:27:12 +0100273 if (IS_ERR(drvdata->dev)) {
274 ret = PTR_ERR(drvdata->dev);
Mark Brownc53ad7f2009-08-03 18:49:53 +0100275 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
Roger Quadros86d98842009-08-06 19:37:29 +0300276 goto err_gpio;
Mark Brown4b74ff62008-04-30 16:27:12 +0100277 }
278
279 platform_set_drvdata(pdev, drvdata);
280
281 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
282 drvdata->microvolts);
283
284 return 0;
285
Roger Quadros86d98842009-08-06 19:37:29 +0300286err_gpio:
287 if (gpio_is_valid(config->gpio))
288 gpio_free(config->gpio);
Mark Brown4b74ff62008-04-30 16:27:12 +0100289err_name:
290 kfree(drvdata->desc.name);
291err:
292 kfree(drvdata);
293 return ret;
294}
295
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800296static int __devexit reg_fixed_voltage_remove(struct platform_device *pdev)
Mark Brown4b74ff62008-04-30 16:27:12 +0100297{
298 struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
299
300 regulator_unregister(drvdata->dev);
Roger Quadros86d98842009-08-06 19:37:29 +0300301 if (gpio_is_valid(drvdata->gpio))
302 gpio_free(drvdata->gpio);
Dan Carpenter80099c72009-11-16 11:05:03 +0200303 kfree(drvdata->desc.name);
304 kfree(drvdata);
Roger Quadros86d98842009-08-06 19:37:29 +0300305
Mark Brown4b74ff62008-04-30 16:27:12 +0100306 return 0;
307}
308
Rajendra Nayakcef49102011-11-18 16:47:18 +0530309#if defined(CONFIG_OF)
310static const struct of_device_id fixed_of_match[] __devinitconst = {
311 { .compatible = "regulator-fixed", },
312 {},
313};
314MODULE_DEVICE_TABLE(of, fixed_of_match);
315#else
316#define fixed_of_match NULL
317#endif
318
Mark Brown4b74ff62008-04-30 16:27:12 +0100319static struct platform_driver regulator_fixed_voltage_driver = {
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800320 .probe = reg_fixed_voltage_probe,
321 .remove = __devexit_p(reg_fixed_voltage_remove),
Mark Brown4b74ff62008-04-30 16:27:12 +0100322 .driver = {
323 .name = "reg-fixed-voltage",
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800324 .owner = THIS_MODULE,
Rajendra Nayakcef49102011-11-18 16:47:18 +0530325 .of_match_table = fixed_of_match,
Mark Brown4b74ff62008-04-30 16:27:12 +0100326 },
327};
328
329static int __init regulator_fixed_voltage_init(void)
330{
331 return platform_driver_register(&regulator_fixed_voltage_driver);
332}
Mark Brown5a1b22b2009-04-27 18:21:18 +0100333subsys_initcall(regulator_fixed_voltage_init);
Mark Brown4b74ff62008-04-30 16:27:12 +0100334
335static void __exit regulator_fixed_voltage_exit(void)
336{
337 platform_driver_unregister(&regulator_fixed_voltage_driver);
338}
339module_exit(regulator_fixed_voltage_exit);
340
341MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
342MODULE_DESCRIPTION("Fixed voltage regulator");
343MODULE_LICENSE("GPL");
Mark Brown38c53c82009-04-28 11:13:55 +0100344MODULE_ALIAS("platform:reg-fixed-voltage");