[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / regulator / pcf50633-regulator.c
blob0803ffe6236d03526c66df8fbf6e9bdba0150b7f
1 /* NXP PCF50633 PMIC Driver
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Balaji Rao <balajirrao@openmoko.org>
5 * All rights reserved.
7 * Broken down from monstrous PCF50633 driver mainly by
8 * Harald Welte and Andy Green and Werner Almesberger
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
24 #include <linux/mfd/pcf50633/core.h>
25 #include <linux/mfd/pcf50633/pmic.h>
27 #define PCF50633_REGULATOR(_name, _id, _n) \
28 { \
29 .name = _name, \
30 .id = _id, \
31 .ops = &pcf50633_regulator_ops, \
32 .n_voltages = _n, \
33 .type = REGULATOR_VOLTAGE, \
34 .owner = THIS_MODULE, \
37 static const u8 pcf50633_regulator_registers[PCF50633_NUM_REGULATORS] = {
38 [PCF50633_REGULATOR_AUTO] = PCF50633_REG_AUTOOUT,
39 [PCF50633_REGULATOR_DOWN1] = PCF50633_REG_DOWN1OUT,
40 [PCF50633_REGULATOR_DOWN2] = PCF50633_REG_DOWN2OUT,
41 [PCF50633_REGULATOR_MEMLDO] = PCF50633_REG_MEMLDOOUT,
42 [PCF50633_REGULATOR_LDO1] = PCF50633_REG_LDO1OUT,
43 [PCF50633_REGULATOR_LDO2] = PCF50633_REG_LDO2OUT,
44 [PCF50633_REGULATOR_LDO3] = PCF50633_REG_LDO3OUT,
45 [PCF50633_REGULATOR_LDO4] = PCF50633_REG_LDO4OUT,
46 [PCF50633_REGULATOR_LDO5] = PCF50633_REG_LDO5OUT,
47 [PCF50633_REGULATOR_LDO6] = PCF50633_REG_LDO6OUT,
48 [PCF50633_REGULATOR_HCLDO] = PCF50633_REG_HCLDOOUT,
51 /* Bits from voltage value */
52 static u8 auto_voltage_bits(unsigned int millivolts)
54 if (millivolts < 1800)
55 return 0;
56 if (millivolts > 3800)
57 return 0xff;
59 millivolts -= 625;
61 return millivolts / 25;
64 static u8 down_voltage_bits(unsigned int millivolts)
66 if (millivolts < 625)
67 return 0;
68 else if (millivolts > 3000)
69 return 0xff;
71 millivolts -= 625;
73 return millivolts / 25;
76 static u8 ldo_voltage_bits(unsigned int millivolts)
78 if (millivolts < 900)
79 return 0;
80 else if (millivolts > 3600)
81 return 0x1f;
83 millivolts -= 900;
84 return millivolts / 100;
87 /* Obtain voltage value from bits */
88 static unsigned int auto_voltage_value(u8 bits)
90 if (bits < 0x2f)
91 return 0;
93 return 625 + (bits * 25);
97 static unsigned int down_voltage_value(u8 bits)
99 return 625 + (bits * 25);
103 static unsigned int ldo_voltage_value(u8 bits)
105 bits &= 0x1f;
107 return 900 + (bits * 100);
110 static int pcf50633_regulator_set_voltage(struct regulator_dev *rdev,
111 int min_uV, int max_uV)
113 struct pcf50633 *pcf;
114 int regulator_id, millivolts;
115 u8 volt_bits, regnr;
117 pcf = rdev_get_drvdata(rdev);
119 regulator_id = rdev_get_id(rdev);
120 if (regulator_id >= PCF50633_NUM_REGULATORS)
121 return -EINVAL;
123 millivolts = min_uV / 1000;
125 regnr = pcf50633_regulator_registers[regulator_id];
127 switch (regulator_id) {
128 case PCF50633_REGULATOR_AUTO:
129 volt_bits = auto_voltage_bits(millivolts);
130 break;
131 case PCF50633_REGULATOR_DOWN1:
132 volt_bits = down_voltage_bits(millivolts);
133 break;
134 case PCF50633_REGULATOR_DOWN2:
135 volt_bits = down_voltage_bits(millivolts);
136 break;
137 case PCF50633_REGULATOR_LDO1:
138 case PCF50633_REGULATOR_LDO2:
139 case PCF50633_REGULATOR_LDO3:
140 case PCF50633_REGULATOR_LDO4:
141 case PCF50633_REGULATOR_LDO5:
142 case PCF50633_REGULATOR_LDO6:
143 case PCF50633_REGULATOR_HCLDO:
144 volt_bits = ldo_voltage_bits(millivolts);
145 break;
146 default:
147 return -EINVAL;
150 return pcf50633_reg_write(pcf, regnr, volt_bits);
153 static int pcf50633_regulator_voltage_value(enum pcf50633_regulator_id id,
154 u8 bits)
156 int millivolts;
158 switch (id) {
159 case PCF50633_REGULATOR_AUTO:
160 millivolts = auto_voltage_value(bits);
161 break;
162 case PCF50633_REGULATOR_DOWN1:
163 millivolts = down_voltage_value(bits);
164 break;
165 case PCF50633_REGULATOR_DOWN2:
166 millivolts = down_voltage_value(bits);
167 break;
168 case PCF50633_REGULATOR_LDO1:
169 case PCF50633_REGULATOR_LDO2:
170 case PCF50633_REGULATOR_LDO3:
171 case PCF50633_REGULATOR_LDO4:
172 case PCF50633_REGULATOR_LDO5:
173 case PCF50633_REGULATOR_LDO6:
174 case PCF50633_REGULATOR_HCLDO:
175 millivolts = ldo_voltage_value(bits);
176 break;
177 default:
178 return -EINVAL;
181 return millivolts * 1000;
184 static int pcf50633_regulator_get_voltage(struct regulator_dev *rdev)
186 struct pcf50633 *pcf;
187 int regulator_id;
188 u8 volt_bits, regnr;
190 pcf = rdev_get_drvdata(rdev);
192 regulator_id = rdev_get_id(rdev);
193 if (regulator_id >= PCF50633_NUM_REGULATORS)
194 return -EINVAL;
196 regnr = pcf50633_regulator_registers[regulator_id];
198 volt_bits = pcf50633_reg_read(pcf, regnr);
200 return pcf50633_regulator_voltage_value(regulator_id, volt_bits);
203 static int pcf50633_regulator_list_voltage(struct regulator_dev *rdev,
204 unsigned int index)
206 struct pcf50633 *pcf;
207 int regulator_id;
209 pcf = rdev_get_drvdata(rdev);
211 regulator_id = rdev_get_id(rdev);
213 switch (regulator_id) {
214 case PCF50633_REGULATOR_AUTO:
215 index += 0x2f;
216 break;
217 case PCF50633_REGULATOR_HCLDO:
218 index += 0x01;
219 break;
220 default:
221 break;
224 return pcf50633_regulator_voltage_value(regulator_id, index);
227 static int pcf50633_regulator_enable(struct regulator_dev *rdev)
229 struct pcf50633 *pcf = rdev_get_drvdata(rdev);
230 int regulator_id;
231 u8 regnr;
233 regulator_id = rdev_get_id(rdev);
234 if (regulator_id >= PCF50633_NUM_REGULATORS)
235 return -EINVAL;
237 /* The *ENA register is always one after the *OUT register */
238 regnr = pcf50633_regulator_registers[regulator_id] + 1;
240 return pcf50633_reg_set_bit_mask(pcf, regnr, PCF50633_REGULATOR_ON,
241 PCF50633_REGULATOR_ON);
244 static int pcf50633_regulator_disable(struct regulator_dev *rdev)
246 struct pcf50633 *pcf = rdev_get_drvdata(rdev);
247 int regulator_id;
248 u8 regnr;
250 regulator_id = rdev_get_id(rdev);
251 if (regulator_id >= PCF50633_NUM_REGULATORS)
252 return -EINVAL;
254 /* the *ENA register is always one after the *OUT register */
255 regnr = pcf50633_regulator_registers[regulator_id] + 1;
257 return pcf50633_reg_set_bit_mask(pcf, regnr,
258 PCF50633_REGULATOR_ON, 0);
261 static int pcf50633_regulator_is_enabled(struct regulator_dev *rdev)
263 struct pcf50633 *pcf = rdev_get_drvdata(rdev);
264 int regulator_id = rdev_get_id(rdev);
265 u8 regnr;
267 regulator_id = rdev_get_id(rdev);
268 if (regulator_id >= PCF50633_NUM_REGULATORS)
269 return -EINVAL;
271 /* the *ENA register is always one after the *OUT register */
272 regnr = pcf50633_regulator_registers[regulator_id] + 1;
274 return pcf50633_reg_read(pcf, regnr) & PCF50633_REGULATOR_ON;
277 static struct regulator_ops pcf50633_regulator_ops = {
278 .set_voltage = pcf50633_regulator_set_voltage,
279 .get_voltage = pcf50633_regulator_get_voltage,
280 .list_voltage = pcf50633_regulator_list_voltage,
281 .enable = pcf50633_regulator_enable,
282 .disable = pcf50633_regulator_disable,
283 .is_enabled = pcf50633_regulator_is_enabled,
286 static struct regulator_desc regulators[] = {
287 [PCF50633_REGULATOR_AUTO] =
288 PCF50633_REGULATOR("auto", PCF50633_REGULATOR_AUTO, 80),
289 [PCF50633_REGULATOR_DOWN1] =
290 PCF50633_REGULATOR("down1", PCF50633_REGULATOR_DOWN1, 95),
291 [PCF50633_REGULATOR_DOWN2] =
292 PCF50633_REGULATOR("down2", PCF50633_REGULATOR_DOWN2, 95),
293 [PCF50633_REGULATOR_LDO1] =
294 PCF50633_REGULATOR("ldo1", PCF50633_REGULATOR_LDO1, 27),
295 [PCF50633_REGULATOR_LDO2] =
296 PCF50633_REGULATOR("ldo2", PCF50633_REGULATOR_LDO2, 27),
297 [PCF50633_REGULATOR_LDO3] =
298 PCF50633_REGULATOR("ldo3", PCF50633_REGULATOR_LDO3, 27),
299 [PCF50633_REGULATOR_LDO4] =
300 PCF50633_REGULATOR("ldo4", PCF50633_REGULATOR_LDO4, 27),
301 [PCF50633_REGULATOR_LDO5] =
302 PCF50633_REGULATOR("ldo5", PCF50633_REGULATOR_LDO5, 27),
303 [PCF50633_REGULATOR_LDO6] =
304 PCF50633_REGULATOR("ldo6", PCF50633_REGULATOR_LDO6, 27),
305 [PCF50633_REGULATOR_HCLDO] =
306 PCF50633_REGULATOR("hcldo", PCF50633_REGULATOR_HCLDO, 26),
307 [PCF50633_REGULATOR_MEMLDO] =
308 PCF50633_REGULATOR("memldo", PCF50633_REGULATOR_MEMLDO, 0),
311 static int __devinit pcf50633_regulator_probe(struct platform_device *pdev)
313 struct regulator_dev *rdev;
314 struct pcf50633 *pcf;
316 /* Already set by core driver */
317 pcf = platform_get_drvdata(pdev);
319 rdev = regulator_register(&regulators[pdev->id], &pdev->dev,
320 pdev->dev.platform_data, pcf);
321 if (IS_ERR(rdev))
322 return PTR_ERR(rdev);
324 if (pcf->pdata->regulator_registered)
325 pcf->pdata->regulator_registered(pcf, pdev->id);
327 return 0;
330 static int __devexit pcf50633_regulator_remove(struct platform_device *pdev)
332 struct regulator_dev *rdev = platform_get_drvdata(pdev);
334 regulator_unregister(rdev);
336 return 0;
339 static struct platform_driver pcf50633_regulator_driver = {
340 .driver = {
341 .name = "pcf50633-regltr",
343 .probe = pcf50633_regulator_probe,
344 .remove = __devexit_p(pcf50633_regulator_remove),
347 static int __init pcf50633_regulator_init(void)
349 return platform_driver_register(&pcf50633_regulator_driver);
351 subsys_initcall(pcf50633_regulator_init);
353 static void __exit pcf50633_regulator_exit(void)
355 platform_driver_unregister(&pcf50633_regulator_driver);
357 module_exit(pcf50633_regulator_exit);
359 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
360 MODULE_DESCRIPTION("PCF50633 regulator driver");
361 MODULE_LICENSE("GPL");
362 MODULE_ALIAS("platform:pcf50633-regulator");