regulator: Convert fixed voltage regulator to use enable_time()
[firewire-audio.git] / drivers / regulator / fixed.c
bloba3d3bfc24b1f027b29fdffb377c5e75d757b60df
1 /*
2 * fixed.c
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;
32 int microvolts;
33 int gpio;
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);
52 data->is_enabled = 1;
55 return 0;
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);
64 data->is_enabled = 0;
67 return 0;
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,
85 unsigned selector)
87 struct fixed_voltage_data *data = rdev_get_drvdata(dev);
89 if (selector != 0)
90 return -EINVAL;
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;
108 int ret;
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");
113 ret = -ENOMEM;
114 goto err;
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");
120 ret = -ENOMEM;
121 goto err;
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
141 * for GPIO control.
143 * This warning will be removed once there are a couple of users
144 * for this driver.
146 if (!config->gpio)
147 dev_warn(&pdev->dev,
148 "using GPIO 0 for regulator enable control\n");
150 ret = gpio_request(config->gpio, config->supply_name);
151 if (ret) {
152 dev_err(&pdev->dev,
153 "Could not obtain regulator enable GPIO %d: %d\n",
154 config->gpio, ret);
155 goto err_name;
158 /* set output direction without changing state
159 * to prevent glitch
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);
166 if (ret) {
167 dev_err(&pdev->dev,
168 "Could not configure regulator enable GPIO %d direction: %d\n",
169 config->gpio, ret);
170 goto err_gpio;
173 } else {
174 /* Regulator without GPIO control is considered
175 * always enabled
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);
185 goto err_gpio;
188 platform_set_drvdata(pdev, drvdata);
190 dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
191 drvdata->microvolts);
193 return 0;
195 err_gpio:
196 if (gpio_is_valid(config->gpio))
197 gpio_free(config->gpio);
198 err_name:
199 kfree(drvdata->desc.name);
200 err:
201 kfree(drvdata);
202 return ret;
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);
213 kfree(drvdata);
215 return 0;
218 static struct platform_driver regulator_fixed_voltage_driver = {
219 .probe = regulator_fixed_voltage_probe,
220 .remove = regulator_fixed_voltage_remove,
221 .driver = {
222 .name = "reg-fixed-voltage",
226 static int __init regulator_fixed_voltage_init(void)
228 return platform_driver_register(&regulator_fixed_voltage_driver);
230 subsys_initcall(regulator_fixed_voltage_init);
232 static void __exit regulator_fixed_voltage_exit(void)
234 platform_driver_unregister(&regulator_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");