ARM: 6387/1: errata: check primary part ID in proc-v7.S
[linux-2.6/x86.git] / drivers / regulator / ab8500.c
blob28c7ae67cec9ea1d573c94b30d8732f7770340b0
1 /*
2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License v2
6 * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
8 * AB8500 peripheral regulators
10 * AB8500 supports the following regulators,
11 * LDOs - VAUDIO, VANAMIC2/2, VDIGMIC, VINTCORE12, VTVOUT,
12 * VAUX1/2/3, VANA
14 * for DB8500 cut 1.0 and previous versions of the silicon, all accesses
15 * to registers are through the DB8500 SPI. In cut 1.1 onwards, these
16 * accesses are through the DB8500 PRCMU I2C
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
23 #include <linux/mfd/ab8500.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/machine.h>
26 #include <linux/regulator/ab8500.h>
28 /**
29 * struct ab8500_regulator_info - ab8500 regulator information
30 * @desc: regulator description
31 * @ab8500: ab8500 parent
32 * @regulator_dev: regulator device
33 * @max_uV: maximum voltage (for variable voltage supplies)
34 * @min_uV: minimum voltage (for variable voltage supplies)
35 * @fixed_uV: typical voltage (for fixed voltage supplies)
36 * @update_reg: register to control on/off
37 * @mask: mask to enable/disable regulator
38 * @enable: bits to enable the regulator in normal(high power) mode
39 * @voltage_reg: register to control regulator voltage
40 * @voltage_mask: mask to control regulator voltage
41 * @supported_voltages: supported voltage table
42 * @voltages_len: number of supported voltages for the regulator
44 struct ab8500_regulator_info {
45 struct device *dev;
46 struct regulator_desc desc;
47 struct ab8500 *ab8500;
48 struct regulator_dev *regulator;
49 int max_uV;
50 int min_uV;
51 int fixed_uV;
52 int update_reg;
53 int mask;
54 int enable;
55 int voltage_reg;
56 int voltage_mask;
57 int const *supported_voltages;
58 int voltages_len;
61 /* voltage tables for the vauxn/vintcore supplies */
62 static const int ldo_vauxn_voltages[] = {
63 1100000,
64 1200000,
65 1300000,
66 1400000,
67 1500000,
68 1800000,
69 1850000,
70 1900000,
71 2500000,
72 2650000,
73 2700000,
74 2750000,
75 2800000,
76 2900000,
77 3000000,
78 3300000,
81 static const int ldo_vintcore_voltages[] = {
82 1200000,
83 1225000,
84 1250000,
85 1275000,
86 1300000,
87 1325000,
88 1350000,
91 static int ab8500_regulator_enable(struct regulator_dev *rdev)
93 int regulator_id, ret;
94 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
96 regulator_id = rdev_get_id(rdev);
97 if (regulator_id >= AB8500_NUM_REGULATORS)
98 return -EINVAL;
100 ret = ab8500_set_bits(info->ab8500, info->update_reg,
101 info->mask, info->enable);
102 if (ret < 0)
103 dev_err(rdev_get_dev(rdev),
104 "couldn't set enable bits for regulator\n");
105 return ret;
108 static int ab8500_regulator_disable(struct regulator_dev *rdev)
110 int regulator_id, ret;
111 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
113 regulator_id = rdev_get_id(rdev);
114 if (regulator_id >= AB8500_NUM_REGULATORS)
115 return -EINVAL;
117 ret = ab8500_set_bits(info->ab8500, info->update_reg,
118 info->mask, 0x0);
119 if (ret < 0)
120 dev_err(rdev_get_dev(rdev),
121 "couldn't set disable bits for regulator\n");
122 return ret;
125 static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
127 int regulator_id, ret;
128 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
130 regulator_id = rdev_get_id(rdev);
131 if (regulator_id >= AB8500_NUM_REGULATORS)
132 return -EINVAL;
134 ret = ab8500_read(info->ab8500, info->update_reg);
135 if (ret < 0) {
136 dev_err(rdev_get_dev(rdev),
137 "couldn't read 0x%x register\n", info->update_reg);
138 return ret;
141 if (ret & info->mask)
142 return true;
143 else
144 return false;
147 static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
149 int regulator_id;
150 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
152 regulator_id = rdev_get_id(rdev);
153 if (regulator_id >= AB8500_NUM_REGULATORS)
154 return -EINVAL;
156 /* return the uV for the fixed regulators */
157 if (info->fixed_uV)
158 return info->fixed_uV;
160 if (selector >= info->voltages_len)
161 return -EINVAL;
163 return info->supported_voltages[selector];
166 static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
168 int regulator_id, ret, val;
169 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
171 regulator_id = rdev_get_id(rdev);
172 if (regulator_id >= AB8500_NUM_REGULATORS)
173 return -EINVAL;
175 ret = ab8500_read(info->ab8500, info->voltage_reg);
176 if (ret < 0) {
177 dev_err(rdev_get_dev(rdev),
178 "couldn't read voltage reg for regulator\n");
179 return ret;
182 /* vintcore has a different layout */
183 val = ret & info->voltage_mask;
184 if (regulator_id == AB8500_LDO_INTCORE)
185 ret = info->supported_voltages[val >> 0x3];
186 else
187 ret = info->supported_voltages[val];
189 return ret;
192 static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
193 int min_uV, int max_uV)
195 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
196 int i;
198 /* check the supported voltage */
199 for (i = 0; i < info->voltages_len; i++) {
200 if ((info->supported_voltages[i] >= min_uV) &&
201 (info->supported_voltages[i] <= max_uV))
202 return i;
205 return -EINVAL;
208 static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
209 int min_uV, int max_uV)
211 int regulator_id, ret;
212 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
214 regulator_id = rdev_get_id(rdev);
215 if (regulator_id >= AB8500_NUM_REGULATORS)
216 return -EINVAL;
218 /* get the appropriate voltages within the range */
219 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
220 if (ret < 0) {
221 dev_err(rdev_get_dev(rdev),
222 "couldn't get best voltage for regulator\n");
223 return ret;
226 /* set the registers for the request */
227 ret = ab8500_set_bits(info->ab8500, info->voltage_reg,
228 info->voltage_mask, ret);
229 if (ret < 0)
230 dev_err(rdev_get_dev(rdev),
231 "couldn't set voltage reg for regulator\n");
233 return ret;
236 static struct regulator_ops ab8500_regulator_ops = {
237 .enable = ab8500_regulator_enable,
238 .disable = ab8500_regulator_disable,
239 .is_enabled = ab8500_regulator_is_enabled,
240 .get_voltage = ab8500_regulator_get_voltage,
241 .set_voltage = ab8500_regulator_set_voltage,
242 .list_voltage = ab8500_list_voltage,
245 static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
247 int regulator_id;
248 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
250 regulator_id = rdev_get_id(rdev);
251 if (regulator_id >= AB8500_NUM_REGULATORS)
252 return -EINVAL;
254 return info->fixed_uV;
257 static struct regulator_ops ab8500_ldo_fixed_ops = {
258 .enable = ab8500_regulator_enable,
259 .disable = ab8500_regulator_disable,
260 .is_enabled = ab8500_regulator_is_enabled,
261 .get_voltage = ab8500_fixed_get_voltage,
262 .list_voltage = ab8500_list_voltage,
265 #define AB8500_LDO(_id, min, max, reg, reg_mask, reg_enable, \
266 volt_reg, volt_mask, voltages, \
267 len_volts) \
269 .desc = { \
270 .name = "LDO-" #_id, \
271 .ops = &ab8500_regulator_ops, \
272 .type = REGULATOR_VOLTAGE, \
273 .id = AB8500_LDO_##_id, \
274 .owner = THIS_MODULE, \
275 }, \
276 .min_uV = (min) * 1000, \
277 .max_uV = (max) * 1000, \
278 .update_reg = reg, \
279 .mask = reg_mask, \
280 .enable = reg_enable, \
281 .voltage_reg = volt_reg, \
282 .voltage_mask = volt_mask, \
283 .supported_voltages = voltages, \
284 .voltages_len = len_volts, \
285 .fixed_uV = 0, \
288 #define AB8500_FIXED_LDO(_id, fixed, reg, reg_mask, \
289 reg_enable) \
291 .desc = { \
292 .name = "LDO-" #_id, \
293 .ops = &ab8500_ldo_fixed_ops, \
294 .type = REGULATOR_VOLTAGE, \
295 .id = AB8500_LDO_##_id, \
296 .owner = THIS_MODULE, \
297 }, \
298 .fixed_uV = fixed * 1000, \
299 .update_reg = reg, \
300 .mask = reg_mask, \
301 .enable = reg_enable, \
304 static struct ab8500_regulator_info ab8500_regulator_info[] = {
306 * Variable Voltage LDOs
307 * name, min uV, max uV, ctrl reg, reg mask, enable mask,
308 * volt ctrl reg, volt ctrl mask, volt table, num supported volts
310 AB8500_LDO(AUX1, 1100, 3300, 0x0409, 0x3, 0x1, 0x041f, 0xf,
311 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
312 AB8500_LDO(AUX2, 1100, 3300, 0x0409, 0xc, 0x4, 0x0420, 0xf,
313 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
314 AB8500_LDO(AUX3, 1100, 3300, 0x040a, 0x3, 0x1, 0x0421, 0xf,
315 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
316 AB8500_LDO(INTCORE, 1100, 3300, 0x0380, 0x4, 0x4, 0x0380, 0x38,
317 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
320 * Fixed Voltage LDOs
321 * name, o/p uV, ctrl reg, enable, disable
323 AB8500_FIXED_LDO(TVOUT, 2000, 0x0380, 0x2, 0x2),
324 AB8500_FIXED_LDO(AUDIO, 2000, 0x0383, 0x2, 0x2),
325 AB8500_FIXED_LDO(ANAMIC1, 2050, 0x0383, 0x4, 0x4),
326 AB8500_FIXED_LDO(ANAMIC2, 2050, 0x0383, 0x8, 0x8),
327 AB8500_FIXED_LDO(DMIC, 1800, 0x0383, 0x10, 0x10),
328 AB8500_FIXED_LDO(ANA, 1200, 0x0383, 0xc, 0x4),
331 static inline struct ab8500_regulator_info *find_regulator_info(int id)
333 struct ab8500_regulator_info *info;
334 int i;
336 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
337 info = &ab8500_regulator_info[i];
338 if (info->desc.id == id)
339 return info;
341 return NULL;
344 static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
346 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
347 struct ab8500_platform_data *pdata;
348 int i, err;
350 if (!ab8500) {
351 dev_err(&pdev->dev, "null mfd parent\n");
352 return -EINVAL;
354 pdata = dev_get_platdata(ab8500->dev);
356 /* register all regulators */
357 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
358 struct ab8500_regulator_info *info = NULL;
360 /* assign per-regulator data */
361 info = &ab8500_regulator_info[i];
362 info->dev = &pdev->dev;
363 info->ab8500 = ab8500;
365 info->regulator = regulator_register(&info->desc, &pdev->dev,
366 pdata->regulator[i], info);
367 if (IS_ERR(info->regulator)) {
368 err = PTR_ERR(info->regulator);
369 dev_err(&pdev->dev, "failed to register regulator %s\n",
370 info->desc.name);
371 /* when we fail, un-register all earlier regulators */
372 while (--i >= 0) {
373 info = &ab8500_regulator_info[i];
374 regulator_unregister(info->regulator);
376 return err;
380 return 0;
383 static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
385 int i;
387 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
388 struct ab8500_regulator_info *info = NULL;
389 info = &ab8500_regulator_info[i];
390 regulator_unregister(info->regulator);
393 return 0;
396 static struct platform_driver ab8500_regulator_driver = {
397 .probe = ab8500_regulator_probe,
398 .remove = __devexit_p(ab8500_regulator_remove),
399 .driver = {
400 .name = "ab8500-regulator",
401 .owner = THIS_MODULE,
405 static int __init ab8500_regulator_init(void)
407 int ret;
409 ret = platform_driver_register(&ab8500_regulator_driver);
410 if (ret != 0)
411 pr_err("Failed to register ab8500 regulator: %d\n", ret);
413 return ret;
415 subsys_initcall(ab8500_regulator_init);
417 static void __exit ab8500_regulator_exit(void)
419 platform_driver_unregister(&ab8500_regulator_driver);
421 module_exit(ab8500_regulator_exit);
423 MODULE_LICENSE("GPL v2");
424 MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
425 MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
426 MODULE_ALIAS("platform:ab8500-regulator");