blob: 906b2658c39d0ca5ad1d3dd3bacac150268834bd [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>
23#include <linux/platform_device.h>
24#include <linux/regulator/driver.h>
25#include <linux/regulator/fixed.h>
Roger Quadros86d98842009-08-06 19:37:29 +030026#include <linux/gpio.h>
Adrian Huntereda79a32010-01-12 12:25:13 +020027#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Rajendra Nayak1dc52402011-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;
Adrian Huntereda79a32010-01-12 12:25:13 +020039 unsigned startup_delay;
Dmitry Torokhov8ab33432010-02-23 23:37:55 -080040 bool enable_high;
41 bool is_enabled;
Mark Brown4b74ff62008-04-30 16:27:12 +010042};
43
Rajendra Nayak1dc52402011-11-18 16:47:18 +053044
45/**
46 * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
47 * @dev: device requesting for fixed_voltage_config
48 *
49 * Populates fixed_voltage_config structure by extracting data from device
50 * tree node, returns a pointer to the populated structure of NULL if memory
51 * alloc fails.
52 */
53struct fixed_voltage_config *of_get_fixed_voltage_config(struct device *dev)
54{
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)
63 return NULL;
64
65 config->init_data = of_get_regulator_init_data(dev);
66 init_data = config->init_data;
67
68 config->supply_name = init_data->constraints.name;
69 if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
70 config->microvolts = init_data->constraints.min_uV;
71 } else {
72 dev_err(dev,
73 "Fixed regulator specified with variable voltages\n");
74 return NULL;
75 }
76
77 if (init_data->constraints.boot_on)
78 config->enabled_at_boot = true;
79
80 config->gpio = of_get_named_gpio(np, "gpio", 0);
81 delay = of_get_property(np, "startup-delay-us", NULL);
82 if (delay)
83 config->startup_delay = be32_to_cpu(*delay);
84
85 if (of_find_property(np, "enable-active-high", NULL))
86 config->enable_high = true;
87
88 return config;
89}
90
Mark Brown4b74ff62008-04-30 16:27:12 +010091static int fixed_voltage_is_enabled(struct regulator_dev *dev)
92{
Roger Quadros86d98842009-08-06 19:37:29 +030093 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
94
95 return data->is_enabled;
Mark Brown4b74ff62008-04-30 16:27:12 +010096}
97
98static int fixed_voltage_enable(struct regulator_dev *dev)
99{
Roger Quadros86d98842009-08-06 19:37:29 +0300100 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
101
102 if (gpio_is_valid(data->gpio)) {
103 gpio_set_value_cansleep(data->gpio, data->enable_high);
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800104 data->is_enabled = true;
Roger Quadros86d98842009-08-06 19:37:29 +0300105 }
106
107 return 0;
108}
109
110static int fixed_voltage_disable(struct regulator_dev *dev)
111{
112 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
113
114 if (gpio_is_valid(data->gpio)) {
115 gpio_set_value_cansleep(data->gpio, !data->enable_high);
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800116 data->is_enabled = false;
Roger Quadros86d98842009-08-06 19:37:29 +0300117 }
118
Mark Brown4b74ff62008-04-30 16:27:12 +0100119 return 0;
120}
121
Mark Brown17133dc2010-01-28 10:21:05 +0000122static int fixed_voltage_enable_time(struct regulator_dev *dev)
123{
124 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
125
126 return data->startup_delay;
127}
128
Mark Brown4b74ff62008-04-30 16:27:12 +0100129static int fixed_voltage_get_voltage(struct regulator_dev *dev)
130{
131 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
132
133 return data->microvolts;
134}
135
Mark Brown9035cef2009-04-28 11:13:54 +0100136static int fixed_voltage_list_voltage(struct regulator_dev *dev,
137 unsigned selector)
138{
139 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
140
141 if (selector != 0)
142 return -EINVAL;
143
144 return data->microvolts;
145}
146
Mark Brown4b74ff62008-04-30 16:27:12 +0100147static struct regulator_ops fixed_voltage_ops = {
148 .is_enabled = fixed_voltage_is_enabled,
149 .enable = fixed_voltage_enable,
Roger Quadros86d98842009-08-06 19:37:29 +0300150 .disable = fixed_voltage_disable,
Mark Brown17133dc2010-01-28 10:21:05 +0000151 .enable_time = fixed_voltage_enable_time,
Mark Brown4b74ff62008-04-30 16:27:12 +0100152 .get_voltage = fixed_voltage_get_voltage,
Mark Brown9035cef2009-04-28 11:13:54 +0100153 .list_voltage = fixed_voltage_list_voltage,
Mark Brown4b74ff62008-04-30 16:27:12 +0100154};
155
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800156static int __devinit reg_fixed_voltage_probe(struct platform_device *pdev)
Mark Brown4b74ff62008-04-30 16:27:12 +0100157{
158 struct fixed_voltage_config *config = pdev->dev.platform_data;
159 struct fixed_voltage_data *drvdata;
160 int ret;
161
Rajendra Nayak1dc52402011-11-18 16:47:18 +0530162 if (pdev->dev.of_node)
163 config = of_get_fixed_voltage_config(&pdev->dev);
164
Mark Brown4b74ff62008-04-30 16:27:12 +0100165 drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL);
166 if (drvdata == NULL) {
Mark Brownc53ad7f2009-08-03 18:49:53 +0100167 dev_err(&pdev->dev, "Failed to allocate device data\n");
Mark Brown4b74ff62008-04-30 16:27:12 +0100168 ret = -ENOMEM;
169 goto err;
170 }
171
172 drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
173 if (drvdata->desc.name == NULL) {
Mark Brownc53ad7f2009-08-03 18:49:53 +0100174 dev_err(&pdev->dev, "Failed to allocate supply name\n");
Mark Brown4b74ff62008-04-30 16:27:12 +0100175 ret = -ENOMEM;
176 goto err;
177 }
178 drvdata->desc.type = REGULATOR_VOLTAGE;
179 drvdata->desc.owner = THIS_MODULE;
Mark Brown9035cef2009-04-28 11:13:54 +0100180 drvdata->desc.ops = &fixed_voltage_ops;
181 drvdata->desc.n_voltages = 1;
Mark Brown4b74ff62008-04-30 16:27:12 +0100182
183 drvdata->microvolts = config->microvolts;
Roger Quadros86d98842009-08-06 19:37:29 +0300184 drvdata->gpio = config->gpio;
Adrian Huntereda79a32010-01-12 12:25:13 +0200185 drvdata->startup_delay = config->startup_delay;
Roger Quadros86d98842009-08-06 19:37:29 +0300186
187 if (gpio_is_valid(config->gpio)) {
188 drvdata->enable_high = config->enable_high;
189
190 /* FIXME: Remove below print warning
191 *
192 * config->gpio must be set to -EINVAL by platform code if
193 * GPIO control is not required. However, early adopters
194 * not requiring GPIO control may forget to initialize
195 * config->gpio to -EINVAL. This will cause GPIO 0 to be used
196 * for GPIO control.
197 *
198 * This warning will be removed once there are a couple of users
199 * for this driver.
200 */
201 if (!config->gpio)
202 dev_warn(&pdev->dev,
203 "using GPIO 0 for regulator enable control\n");
204
205 ret = gpio_request(config->gpio, config->supply_name);
206 if (ret) {
207 dev_err(&pdev->dev,
208 "Could not obtain regulator enable GPIO %d: %d\n",
209 config->gpio, ret);
210 goto err_name;
211 }
212
213 /* set output direction without changing state
214 * to prevent glitch
215 */
216 drvdata->is_enabled = config->enabled_at_boot;
217 ret = drvdata->is_enabled ?
218 config->enable_high : !config->enable_high;
219
220 ret = gpio_direction_output(config->gpio, ret);
221 if (ret) {
222 dev_err(&pdev->dev,
223 "Could not configure regulator enable GPIO %d direction: %d\n",
224 config->gpio, ret);
225 goto err_gpio;
226 }
227
228 } else {
229 /* Regulator without GPIO control is considered
230 * always enabled
231 */
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800232 drvdata->is_enabled = true;
Roger Quadros86d98842009-08-06 19:37:29 +0300233 }
Mark Brown4b74ff62008-04-30 16:27:12 +0100234
Mark Brownbcf34022009-01-19 13:37:04 +0000235 drvdata->dev = regulator_register(&drvdata->desc, &pdev->dev,
Rajendra Nayak11eafc62011-11-18 16:47:19 +0530236 config->init_data, drvdata, NULL);
Mark Brown4b74ff62008-04-30 16:27:12 +0100237 if (IS_ERR(drvdata->dev)) {
238 ret = PTR_ERR(drvdata->dev);
Mark Brownc53ad7f2009-08-03 18:49:53 +0100239 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
Roger Quadros86d98842009-08-06 19:37:29 +0300240 goto err_gpio;
Mark Brown4b74ff62008-04-30 16:27:12 +0100241 }
242
243 platform_set_drvdata(pdev, drvdata);
244
245 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
246 drvdata->microvolts);
247
248 return 0;
249
Roger Quadros86d98842009-08-06 19:37:29 +0300250err_gpio:
251 if (gpio_is_valid(config->gpio))
252 gpio_free(config->gpio);
Mark Brown4b74ff62008-04-30 16:27:12 +0100253err_name:
254 kfree(drvdata->desc.name);
255err:
256 kfree(drvdata);
257 return ret;
258}
259
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800260static int __devexit reg_fixed_voltage_remove(struct platform_device *pdev)
Mark Brown4b74ff62008-04-30 16:27:12 +0100261{
262 struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
263
264 regulator_unregister(drvdata->dev);
Roger Quadros86d98842009-08-06 19:37:29 +0300265 if (gpio_is_valid(drvdata->gpio))
266 gpio_free(drvdata->gpio);
Dan Carpenter80099c72009-11-16 11:05:03 +0200267 kfree(drvdata->desc.name);
268 kfree(drvdata);
Roger Quadros86d98842009-08-06 19:37:29 +0300269
Mark Brown4b74ff62008-04-30 16:27:12 +0100270 return 0;
271}
272
Rajendra Nayak1dc52402011-11-18 16:47:18 +0530273#if defined(CONFIG_OF)
274static const struct of_device_id fixed_of_match[] __devinitconst = {
275 { .compatible = "regulator-fixed", },
276 {},
277};
278MODULE_DEVICE_TABLE(of, fixed_of_match);
279#else
280#define fixed_of_match NULL
281#endif
282
Mark Brown4b74ff62008-04-30 16:27:12 +0100283static struct platform_driver regulator_fixed_voltage_driver = {
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800284 .probe = reg_fixed_voltage_probe,
285 .remove = __devexit_p(reg_fixed_voltage_remove),
Mark Brown4b74ff62008-04-30 16:27:12 +0100286 .driver = {
287 .name = "reg-fixed-voltage",
Dmitry Torokhov8ab33432010-02-23 23:37:55 -0800288 .owner = THIS_MODULE,
Rajendra Nayak1dc52402011-11-18 16:47:18 +0530289 .of_match_table = fixed_of_match,
Mark Brown4b74ff62008-04-30 16:27:12 +0100290 },
291};
292
293static int __init regulator_fixed_voltage_init(void)
294{
295 return platform_driver_register(&regulator_fixed_voltage_driver);
296}
Mark Brown5a1b22b2009-04-27 18:21:18 +0100297subsys_initcall(regulator_fixed_voltage_init);
Mark Brown4b74ff62008-04-30 16:27:12 +0100298
299static void __exit regulator_fixed_voltage_exit(void)
300{
301 platform_driver_unregister(&regulator_fixed_voltage_driver);
302}
303module_exit(regulator_fixed_voltage_exit);
304
305MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
306MODULE_DESCRIPTION("Fixed voltage regulator");
307MODULE_LICENSE("GPL");
Mark Brown38c53c82009-04-28 11:13:55 +0100308MODULE_ALIAS("platform:reg-fixed-voltage");