blob: f8b295700d7d706e6a19d6f273e34a149d49e897 [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>
Mark Brown4b74ff62008-04-30 16:27:12 +010027
28struct fixed_voltage_data {
29 struct regulator_desc desc;
30 struct regulator_dev *dev;
31 int microvolts;
Roger Quadros86d98842009-08-06 19:37:29 +030032 int gpio;
33 unsigned enable_high:1;
34 unsigned is_enabled:1;
Mark Brown4b74ff62008-04-30 16:27:12 +010035};
36
37static int fixed_voltage_is_enabled(struct regulator_dev *dev)
38{
Roger Quadros86d98842009-08-06 19:37:29 +030039 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
40
41 return data->is_enabled;
Mark Brown4b74ff62008-04-30 16:27:12 +010042}
43
44static int fixed_voltage_enable(struct regulator_dev *dev)
45{
Roger Quadros86d98842009-08-06 19:37:29 +030046 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
47
48 if (gpio_is_valid(data->gpio)) {
49 gpio_set_value_cansleep(data->gpio, data->enable_high);
50 data->is_enabled = 1;
51 }
52
53 return 0;
54}
55
56static int fixed_voltage_disable(struct regulator_dev *dev)
57{
58 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
59
60 if (gpio_is_valid(data->gpio)) {
61 gpio_set_value_cansleep(data->gpio, !data->enable_high);
62 data->is_enabled = 0;
63 }
64
Mark Brown4b74ff62008-04-30 16:27:12 +010065 return 0;
66}
67
68static int fixed_voltage_get_voltage(struct regulator_dev *dev)
69{
70 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
71
72 return data->microvolts;
73}
74
Mark Brown9035cef2009-04-28 11:13:54 +010075static int fixed_voltage_list_voltage(struct regulator_dev *dev,
76 unsigned selector)
77{
78 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
79
80 if (selector != 0)
81 return -EINVAL;
82
83 return data->microvolts;
84}
85
Mark Brown4b74ff62008-04-30 16:27:12 +010086static struct regulator_ops fixed_voltage_ops = {
87 .is_enabled = fixed_voltage_is_enabled,
88 .enable = fixed_voltage_enable,
Roger Quadros86d98842009-08-06 19:37:29 +030089 .disable = fixed_voltage_disable,
Mark Brown4b74ff62008-04-30 16:27:12 +010090 .get_voltage = fixed_voltage_get_voltage,
Mark Brown9035cef2009-04-28 11:13:54 +010091 .list_voltage = fixed_voltage_list_voltage,
Mark Brown4b74ff62008-04-30 16:27:12 +010092};
93
94static int regulator_fixed_voltage_probe(struct platform_device *pdev)
95{
96 struct fixed_voltage_config *config = pdev->dev.platform_data;
97 struct fixed_voltage_data *drvdata;
98 int ret;
99
100 drvdata = kzalloc(sizeof(struct fixed_voltage_data), GFP_KERNEL);
101 if (drvdata == NULL) {
Mark Brownc53ad7f2009-08-03 18:49:53 +0100102 dev_err(&pdev->dev, "Failed to allocate device data\n");
Mark Brown4b74ff62008-04-30 16:27:12 +0100103 ret = -ENOMEM;
104 goto err;
105 }
106
107 drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
108 if (drvdata->desc.name == NULL) {
Mark Brownc53ad7f2009-08-03 18:49:53 +0100109 dev_err(&pdev->dev, "Failed to allocate supply name\n");
Mark Brown4b74ff62008-04-30 16:27:12 +0100110 ret = -ENOMEM;
111 goto err;
112 }
113 drvdata->desc.type = REGULATOR_VOLTAGE;
114 drvdata->desc.owner = THIS_MODULE;
Mark Brown9035cef2009-04-28 11:13:54 +0100115 drvdata->desc.ops = &fixed_voltage_ops;
116 drvdata->desc.n_voltages = 1;
Mark Brown4b74ff62008-04-30 16:27:12 +0100117
118 drvdata->microvolts = config->microvolts;
Roger Quadros86d98842009-08-06 19:37:29 +0300119 drvdata->gpio = config->gpio;
120
121 if (gpio_is_valid(config->gpio)) {
122 drvdata->enable_high = config->enable_high;
123
124 /* FIXME: Remove below print warning
125 *
126 * config->gpio must be set to -EINVAL by platform code if
127 * GPIO control is not required. However, early adopters
128 * not requiring GPIO control may forget to initialize
129 * config->gpio to -EINVAL. This will cause GPIO 0 to be used
130 * for GPIO control.
131 *
132 * This warning will be removed once there are a couple of users
133 * for this driver.
134 */
135 if (!config->gpio)
136 dev_warn(&pdev->dev,
137 "using GPIO 0 for regulator enable control\n");
138
139 ret = gpio_request(config->gpio, config->supply_name);
140 if (ret) {
141 dev_err(&pdev->dev,
142 "Could not obtain regulator enable GPIO %d: %d\n",
143 config->gpio, ret);
144 goto err_name;
145 }
146
147 /* set output direction without changing state
148 * to prevent glitch
149 */
150 drvdata->is_enabled = config->enabled_at_boot;
151 ret = drvdata->is_enabled ?
152 config->enable_high : !config->enable_high;
153
154 ret = gpio_direction_output(config->gpio, ret);
155 if (ret) {
156 dev_err(&pdev->dev,
157 "Could not configure regulator enable GPIO %d direction: %d\n",
158 config->gpio, ret);
159 goto err_gpio;
160 }
161
162 } else {
163 /* Regulator without GPIO control is considered
164 * always enabled
165 */
166 drvdata->is_enabled = 1;
167 }
Mark Brown4b74ff62008-04-30 16:27:12 +0100168
Mark Brownbcf34022009-01-19 13:37:04 +0000169 drvdata->dev = regulator_register(&drvdata->desc, &pdev->dev,
170 config->init_data, drvdata);
Mark Brown4b74ff62008-04-30 16:27:12 +0100171 if (IS_ERR(drvdata->dev)) {
172 ret = PTR_ERR(drvdata->dev);
Mark Brownc53ad7f2009-08-03 18:49:53 +0100173 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
Roger Quadros86d98842009-08-06 19:37:29 +0300174 goto err_gpio;
Mark Brown4b74ff62008-04-30 16:27:12 +0100175 }
176
177 platform_set_drvdata(pdev, drvdata);
178
179 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
180 drvdata->microvolts);
181
182 return 0;
183
Roger Quadros86d98842009-08-06 19:37:29 +0300184err_gpio:
185 if (gpio_is_valid(config->gpio))
186 gpio_free(config->gpio);
Mark Brown4b74ff62008-04-30 16:27:12 +0100187err_name:
188 kfree(drvdata->desc.name);
189err:
190 kfree(drvdata);
191 return ret;
192}
193
194static int regulator_fixed_voltage_remove(struct platform_device *pdev)
195{
196 struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
197
198 regulator_unregister(drvdata->dev);
199 kfree(drvdata->desc.name);
200 kfree(drvdata);
201
Roger Quadros86d98842009-08-06 19:37:29 +0300202 if (gpio_is_valid(drvdata->gpio))
203 gpio_free(drvdata->gpio);
204
Mark Brown4b74ff62008-04-30 16:27:12 +0100205 return 0;
206}
207
208static struct platform_driver regulator_fixed_voltage_driver = {
209 .probe = regulator_fixed_voltage_probe,
210 .remove = regulator_fixed_voltage_remove,
211 .driver = {
212 .name = "reg-fixed-voltage",
213 },
214};
215
216static int __init regulator_fixed_voltage_init(void)
217{
218 return platform_driver_register(&regulator_fixed_voltage_driver);
219}
Mark Brown5a1b22b2009-04-27 18:21:18 +0100220subsys_initcall(regulator_fixed_voltage_init);
Mark Brown4b74ff62008-04-30 16:27:12 +0100221
222static void __exit regulator_fixed_voltage_exit(void)
223{
224 platform_driver_unregister(&regulator_fixed_voltage_driver);
225}
226module_exit(regulator_fixed_voltage_exit);
227
228MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
229MODULE_DESCRIPTION("Fixed voltage regulator");
230MODULE_LICENSE("GPL");
Mark Brown38c53c82009-04-28 11:13:55 +0100231MODULE_ALIAS("platform:reg-fixed-voltage");