1 /* NXP PCF50633 PMIC Driver
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Balaji Rao <balajirrao@openmoko.org>
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) \
31 .ops = &pcf50633_regulator_ops, \
32 .type = REGULATOR_VOLTAGE, \
33 .owner = THIS_MODULE, \
36 static const u8 pcf50633_regulator_registers
[PCF50633_NUM_REGULATORS
] = {
37 [PCF50633_REGULATOR_AUTO
] = PCF50633_REG_AUTOOUT
,
38 [PCF50633_REGULATOR_DOWN1
] = PCF50633_REG_DOWN1OUT
,
39 [PCF50633_REGULATOR_DOWN2
] = PCF50633_REG_DOWN2OUT
,
40 [PCF50633_REGULATOR_MEMLDO
] = PCF50633_REG_MEMLDOOUT
,
41 [PCF50633_REGULATOR_LDO1
] = PCF50633_REG_LDO1OUT
,
42 [PCF50633_REGULATOR_LDO2
] = PCF50633_REG_LDO2OUT
,
43 [PCF50633_REGULATOR_LDO3
] = PCF50633_REG_LDO3OUT
,
44 [PCF50633_REGULATOR_LDO4
] = PCF50633_REG_LDO4OUT
,
45 [PCF50633_REGULATOR_LDO5
] = PCF50633_REG_LDO5OUT
,
46 [PCF50633_REGULATOR_LDO6
] = PCF50633_REG_LDO6OUT
,
47 [PCF50633_REGULATOR_HCLDO
] = PCF50633_REG_HCLDOOUT
,
50 /* Bits from voltage value */
51 static u8
auto_voltage_bits(unsigned int millivolts
)
53 if (millivolts
< 1800)
55 if (millivolts
> 3800)
60 return millivolts
/ 25;
63 static u8
down_voltage_bits(unsigned int millivolts
)
67 else if (millivolts
> 3000)
72 return millivolts
/ 25;
75 static u8
ldo_voltage_bits(unsigned int millivolts
)
79 else if (millivolts
> 3600)
83 return millivolts
/ 100;
86 /* Obtain voltage value from bits */
87 static unsigned int auto_voltage_value(u8 bits
)
92 return 625 + (bits
* 25);
96 static unsigned int down_voltage_value(u8 bits
)
98 return 625 + (bits
* 25);
102 static unsigned int ldo_voltage_value(u8 bits
)
106 return 900 + (bits
* 100);
109 static int pcf50633_regulator_set_voltage(struct regulator_dev
*rdev
,
110 int min_uV
, int max_uV
)
112 struct pcf50633
*pcf
;
113 int regulator_id
, millivolts
;
116 pcf
= rdev_get_drvdata(rdev
);
118 regulator_id
= rdev_get_id(rdev
);
119 if (regulator_id
>= PCF50633_NUM_REGULATORS
)
122 millivolts
= min_uV
/ 1000;
124 regnr
= pcf50633_regulator_registers
[regulator_id
];
126 switch (regulator_id
) {
127 case PCF50633_REGULATOR_AUTO
:
128 volt_bits
= auto_voltage_bits(millivolts
);
130 case PCF50633_REGULATOR_DOWN1
:
131 volt_bits
= down_voltage_bits(millivolts
);
133 case PCF50633_REGULATOR_DOWN2
:
134 volt_bits
= down_voltage_bits(millivolts
);
136 case PCF50633_REGULATOR_LDO1
:
137 case PCF50633_REGULATOR_LDO2
:
138 case PCF50633_REGULATOR_LDO3
:
139 case PCF50633_REGULATOR_LDO4
:
140 case PCF50633_REGULATOR_LDO5
:
141 case PCF50633_REGULATOR_LDO6
:
142 case PCF50633_REGULATOR_HCLDO
:
143 volt_bits
= ldo_voltage_bits(millivolts
);
149 return pcf50633_reg_write(pcf
, regnr
, volt_bits
);
152 static int pcf50633_regulator_get_voltage(struct regulator_dev
*rdev
)
154 struct pcf50633
*pcf
;
155 int regulator_id
, millivolts
, volt_bits
;
158 pcf
= rdev_get_drvdata(rdev
);;
160 regulator_id
= rdev_get_id(rdev
);
161 if (regulator_id
>= PCF50633_NUM_REGULATORS
)
164 regnr
= pcf50633_regulator_registers
[regulator_id
];
166 volt_bits
= pcf50633_reg_read(pcf
, regnr
);
170 switch (regulator_id
) {
171 case PCF50633_REGULATOR_AUTO
:
172 millivolts
= auto_voltage_value(volt_bits
);
174 case PCF50633_REGULATOR_DOWN1
:
175 millivolts
= down_voltage_value(volt_bits
);
177 case PCF50633_REGULATOR_DOWN2
:
178 millivolts
= down_voltage_value(volt_bits
);
180 case PCF50633_REGULATOR_LDO1
:
181 case PCF50633_REGULATOR_LDO2
:
182 case PCF50633_REGULATOR_LDO3
:
183 case PCF50633_REGULATOR_LDO4
:
184 case PCF50633_REGULATOR_LDO5
:
185 case PCF50633_REGULATOR_LDO6
:
186 case PCF50633_REGULATOR_HCLDO
:
187 millivolts
= ldo_voltage_value(volt_bits
);
193 return millivolts
* 1000;
196 static int pcf50633_regulator_enable(struct regulator_dev
*rdev
)
198 struct pcf50633
*pcf
= rdev_get_drvdata(rdev
);
202 regulator_id
= rdev_get_id(rdev
);
203 if (regulator_id
>= PCF50633_NUM_REGULATORS
)
206 /* The *ENA register is always one after the *OUT register */
207 regnr
= pcf50633_regulator_registers
[regulator_id
] + 1;
209 return pcf50633_reg_set_bit_mask(pcf
, regnr
, PCF50633_REGULATOR_ON
,
210 PCF50633_REGULATOR_ON
);
213 static int pcf50633_regulator_disable(struct regulator_dev
*rdev
)
215 struct pcf50633
*pcf
= rdev_get_drvdata(rdev
);
219 regulator_id
= rdev_get_id(rdev
);
220 if (regulator_id
>= PCF50633_NUM_REGULATORS
)
223 /* the *ENA register is always one after the *OUT register */
224 regnr
= pcf50633_regulator_registers
[regulator_id
] + 1;
226 return pcf50633_reg_set_bit_mask(pcf
, regnr
,
227 PCF50633_REGULATOR_ON
, 0);
230 static int pcf50633_regulator_is_enabled(struct regulator_dev
*rdev
)
232 struct pcf50633
*pcf
= rdev_get_drvdata(rdev
);
233 int regulator_id
= rdev_get_id(rdev
);
236 regulator_id
= rdev_get_id(rdev
);
237 if (regulator_id
>= PCF50633_NUM_REGULATORS
)
240 /* the *ENA register is always one after the *OUT register */
241 regnr
= pcf50633_regulator_registers
[regulator_id
] + 1;
243 return pcf50633_reg_read(pcf
, regnr
) & PCF50633_REGULATOR_ON
;
246 static struct regulator_ops pcf50633_regulator_ops
= {
247 .set_voltage
= pcf50633_regulator_set_voltage
,
248 .get_voltage
= pcf50633_regulator_get_voltage
,
249 .enable
= pcf50633_regulator_enable
,
250 .disable
= pcf50633_regulator_disable
,
251 .is_enabled
= pcf50633_regulator_is_enabled
,
254 static struct regulator_desc regulators
[] = {
255 [PCF50633_REGULATOR_AUTO
] =
256 PCF50633_REGULATOR("auto", PCF50633_REGULATOR_AUTO
),
257 [PCF50633_REGULATOR_DOWN1
] =
258 PCF50633_REGULATOR("down1", PCF50633_REGULATOR_DOWN1
),
259 [PCF50633_REGULATOR_DOWN2
] =
260 PCF50633_REGULATOR("down2", PCF50633_REGULATOR_DOWN2
),
261 [PCF50633_REGULATOR_LDO1
] =
262 PCF50633_REGULATOR("ldo1", PCF50633_REGULATOR_LDO1
),
263 [PCF50633_REGULATOR_LDO2
] =
264 PCF50633_REGULATOR("ldo2", PCF50633_REGULATOR_LDO2
),
265 [PCF50633_REGULATOR_LDO3
] =
266 PCF50633_REGULATOR("ldo3", PCF50633_REGULATOR_LDO3
),
267 [PCF50633_REGULATOR_LDO4
] =
268 PCF50633_REGULATOR("ldo4", PCF50633_REGULATOR_LDO4
),
269 [PCF50633_REGULATOR_LDO5
] =
270 PCF50633_REGULATOR("ldo5", PCF50633_REGULATOR_LDO5
),
271 [PCF50633_REGULATOR_LDO6
] =
272 PCF50633_REGULATOR("ldo6", PCF50633_REGULATOR_LDO6
),
273 [PCF50633_REGULATOR_HCLDO
] =
274 PCF50633_REGULATOR("hcldo", PCF50633_REGULATOR_HCLDO
),
275 [PCF50633_REGULATOR_MEMLDO
] =
276 PCF50633_REGULATOR("memldo", PCF50633_REGULATOR_MEMLDO
),
279 static int __devinit
pcf50633_regulator_probe(struct platform_device
*pdev
)
281 struct regulator_dev
*rdev
;
282 struct pcf50633
*pcf
;
284 /* Already set by core driver */
285 pcf
= platform_get_drvdata(pdev
);
287 rdev
= regulator_register(®ulators
[pdev
->id
], &pdev
->dev
,
288 pdev
->dev
.platform_data
, pcf
);
290 return PTR_ERR(rdev
);
292 if (pcf
->pdata
->regulator_registered
)
293 pcf
->pdata
->regulator_registered(pcf
, pdev
->id
);
298 static int __devexit
pcf50633_regulator_remove(struct platform_device
*pdev
)
300 struct regulator_dev
*rdev
= platform_get_drvdata(pdev
);
302 regulator_unregister(rdev
);
307 static struct platform_driver pcf50633_regulator_driver
= {
309 .name
= "pcf50633-regltr",
311 .probe
= pcf50633_regulator_probe
,
312 .remove
= __devexit_p(pcf50633_regulator_remove
),
315 static int __init
pcf50633_regulator_init(void)
317 return platform_driver_register(&pcf50633_regulator_driver
);
319 module_init(pcf50633_regulator_init
);
321 static void __exit
pcf50633_regulator_exit(void)
323 platform_driver_unregister(&pcf50633_regulator_driver
);
325 module_exit(pcf50633_regulator_exit
);
327 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
328 MODULE_DESCRIPTION("PCF50633 regulator driver");
329 MODULE_LICENSE("GPL");
330 MODULE_ALIAS("platform:pcf50633-regulator");