regulators: Added verbose debug messages to ab8500 regulators
[deliverable/linux.git] / drivers / regulator / ab8500.c
CommitLineData
c789ca20
SI
1/*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * License Terms: GNU General Public License v2
5 *
e1159e6d
BJ
6 * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
7 * Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
c789ca20
SI
8 *
9 * AB8500 peripheral regulators
10 *
e1159e6d
BJ
11 * AB8500 supports the following regulators:
12 * VAUX1/2/3, VINTCORE, VTVOUT, VAUDIO, VAMIC1/2, VDMIC, VANA
c789ca20
SI
13 */
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/err.h>
17#include <linux/platform_device.h>
18#include <linux/mfd/ab8500.h>
47c16975 19#include <linux/mfd/abx500.h>
c789ca20
SI
20#include <linux/regulator/driver.h>
21#include <linux/regulator/machine.h>
22#include <linux/regulator/ab8500.h>
23
24/**
25 * struct ab8500_regulator_info - ab8500 regulator information
e1159e6d 26 * @dev: device pointer
c789ca20 27 * @desc: regulator description
c789ca20
SI
28 * @regulator_dev: regulator device
29 * @max_uV: maximum voltage (for variable voltage supplies)
30 * @min_uV: minimum voltage (for variable voltage supplies)
31 * @fixed_uV: typical voltage (for fixed voltage supplies)
47c16975 32 * @update_bank: bank to control on/off
c789ca20 33 * @update_reg: register to control on/off
e1159e6d
BJ
34 * @update_mask: mask to enable/disable regulator
35 * @update_val_enable: bits to enable the regulator in normal (high power) mode
47c16975 36 * @voltage_bank: bank to control regulator voltage
c789ca20
SI
37 * @voltage_reg: register to control regulator voltage
38 * @voltage_mask: mask to control regulator voltage
e1159e6d 39 * @voltages: supported voltage table
c789ca20
SI
40 * @voltages_len: number of supported voltages for the regulator
41 */
42struct ab8500_regulator_info {
43 struct device *dev;
44 struct regulator_desc desc;
c789ca20
SI
45 struct regulator_dev *regulator;
46 int max_uV;
47 int min_uV;
48 int fixed_uV;
47c16975
MW
49 u8 update_bank;
50 u8 update_reg;
e1159e6d
BJ
51 u8 update_mask;
52 u8 update_val_enable;
47c16975
MW
53 u8 voltage_bank;
54 u8 voltage_reg;
55 u8 voltage_mask;
e1159e6d 56 int const *voltages;
c789ca20
SI
57 int voltages_len;
58};
59
60/* voltage tables for the vauxn/vintcore supplies */
61static const int ldo_vauxn_voltages[] = {
62 1100000,
63 1200000,
64 1300000,
65 1400000,
66 1500000,
67 1800000,
68 1850000,
69 1900000,
70 2500000,
71 2650000,
72 2700000,
73 2750000,
74 2800000,
75 2900000,
76 3000000,
77 3300000,
78};
79
2b75151a
BJ
80static const int ldo_vaux3_voltages[] = {
81 1200000,
82 1500000,
83 1800000,
84 2100000,
85 2500000,
86 2750000,
87 2790000,
88 2910000,
89};
90
c789ca20
SI
91static const int ldo_vintcore_voltages[] = {
92 1200000,
93 1225000,
94 1250000,
95 1275000,
96 1300000,
97 1325000,
98 1350000,
99};
100
101static int ab8500_regulator_enable(struct regulator_dev *rdev)
102{
fc24b426 103 int ret;
c789ca20
SI
104 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
105
fc24b426
BJ
106 if (info == NULL) {
107 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
c789ca20 108 return -EINVAL;
fc24b426 109 }
c789ca20 110
47c16975 111 ret = abx500_mask_and_set_register_interruptible(info->dev,
e1159e6d
BJ
112 info->update_bank, info->update_reg,
113 info->update_mask, info->update_val_enable);
c789ca20
SI
114 if (ret < 0)
115 dev_err(rdev_get_dev(rdev),
116 "couldn't set enable bits for regulator\n");
09aefa12
BJ
117
118 dev_vdbg(rdev_get_dev(rdev),
119 "%s-enable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
120 info->desc.name, info->update_bank, info->update_reg,
121 info->update_mask, info->update_val_enable);
122
c789ca20
SI
123 return ret;
124}
125
126static int ab8500_regulator_disable(struct regulator_dev *rdev)
127{
fc24b426 128 int ret;
c789ca20
SI
129 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
130
fc24b426
BJ
131 if (info == NULL) {
132 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
c789ca20 133 return -EINVAL;
fc24b426 134 }
c789ca20 135
47c16975 136 ret = abx500_mask_and_set_register_interruptible(info->dev,
e1159e6d
BJ
137 info->update_bank, info->update_reg,
138 info->update_mask, 0x0);
c789ca20
SI
139 if (ret < 0)
140 dev_err(rdev_get_dev(rdev),
141 "couldn't set disable bits for regulator\n");
09aefa12
BJ
142
143 dev_vdbg(rdev_get_dev(rdev),
144 "%s-disable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
145 info->desc.name, info->update_bank, info->update_reg,
146 info->update_mask, 0x0);
147
c789ca20
SI
148 return ret;
149}
150
151static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
152{
fc24b426 153 int ret;
c789ca20 154 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
09aefa12 155 u8 regval;
c789ca20 156
fc24b426
BJ
157 if (info == NULL) {
158 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
c789ca20 159 return -EINVAL;
fc24b426 160 }
c789ca20 161
47c16975 162 ret = abx500_get_register_interruptible(info->dev,
09aefa12 163 info->update_bank, info->update_reg, &regval);
c789ca20
SI
164 if (ret < 0) {
165 dev_err(rdev_get_dev(rdev),
166 "couldn't read 0x%x register\n", info->update_reg);
167 return ret;
168 }
169
09aefa12
BJ
170 dev_vdbg(rdev_get_dev(rdev),
171 "%s-is_enabled (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
172 " 0x%x\n",
173 info->desc.name, info->update_bank, info->update_reg,
174 info->update_mask, regval);
175
176 if (regval & info->update_mask)
c789ca20
SI
177 return true;
178 else
179 return false;
180}
181
182static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
183{
c789ca20
SI
184 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
185
fc24b426
BJ
186 if (info == NULL) {
187 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
c789ca20 188 return -EINVAL;
fc24b426 189 }
c789ca20
SI
190
191 /* return the uV for the fixed regulators */
192 if (info->fixed_uV)
193 return info->fixed_uV;
194
49990e6e 195 if (selector >= info->voltages_len)
c789ca20
SI
196 return -EINVAL;
197
e1159e6d 198 return info->voltages[selector];
c789ca20
SI
199}
200
201static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
202{
09aefa12 203 int ret, val;
c789ca20 204 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
09aefa12 205 u8 regval;
c789ca20 206
fc24b426
BJ
207 if (info == NULL) {
208 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
c789ca20 209 return -EINVAL;
fc24b426 210 }
c789ca20 211
09aefa12
BJ
212 ret = abx500_get_register_interruptible(info->dev,
213 info->voltage_bank, info->voltage_reg, &regval);
c789ca20
SI
214 if (ret < 0) {
215 dev_err(rdev_get_dev(rdev),
216 "couldn't read voltage reg for regulator\n");
217 return ret;
218 }
219
09aefa12
BJ
220 dev_vdbg(rdev_get_dev(rdev),
221 "%s-get_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
222 " 0x%x\n",
223 info->desc.name, info->voltage_bank, info->voltage_reg,
224 info->voltage_mask, regval);
225
c789ca20 226 /* vintcore has a different layout */
09aefa12 227 val = regval & info->voltage_mask;
fc24b426 228 if (info->desc.id == AB8500_LDO_INTCORE)
09aefa12 229 ret = info->voltages[val >> 0x3];
c789ca20 230 else
09aefa12 231 ret = info->voltages[val];
c789ca20
SI
232
233 return ret;
234}
235
236static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
237 int min_uV, int max_uV)
238{
239 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
240 int i;
241
242 /* check the supported voltage */
243 for (i = 0; i < info->voltages_len; i++) {
e1159e6d
BJ
244 if ((info->voltages[i] >= min_uV) &&
245 (info->voltages[i] <= max_uV))
c789ca20
SI
246 return i;
247 }
248
249 return -EINVAL;
250}
251
252static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
3a93f2a9
MB
253 int min_uV, int max_uV,
254 unsigned *selector)
c789ca20 255{
fc24b426 256 int ret;
c789ca20 257 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
09aefa12 258 u8 regval;
c789ca20 259
fc24b426
BJ
260 if (info == NULL) {
261 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
c789ca20 262 return -EINVAL;
fc24b426 263 }
c789ca20
SI
264
265 /* get the appropriate voltages within the range */
266 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
267 if (ret < 0) {
268 dev_err(rdev_get_dev(rdev),
269 "couldn't get best voltage for regulator\n");
270 return ret;
271 }
272
3a93f2a9
MB
273 *selector = ret;
274
c789ca20 275 /* set the registers for the request */
09aefa12 276 regval = (u8)ret;
47c16975 277 ret = abx500_mask_and_set_register_interruptible(info->dev,
09aefa12
BJ
278 info->voltage_bank, info->voltage_reg,
279 info->voltage_mask, regval);
c789ca20
SI
280 if (ret < 0)
281 dev_err(rdev_get_dev(rdev),
282 "couldn't set voltage reg for regulator\n");
283
09aefa12
BJ
284 dev_vdbg(rdev_get_dev(rdev),
285 "%s-set_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
286 " 0x%x\n",
287 info->desc.name, info->voltage_bank, info->voltage_reg,
288 info->voltage_mask, regval);
289
c789ca20
SI
290 return ret;
291}
292
293static struct regulator_ops ab8500_regulator_ops = {
294 .enable = ab8500_regulator_enable,
295 .disable = ab8500_regulator_disable,
296 .is_enabled = ab8500_regulator_is_enabled,
297 .get_voltage = ab8500_regulator_get_voltage,
298 .set_voltage = ab8500_regulator_set_voltage,
299 .list_voltage = ab8500_list_voltage,
300};
301
302static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
303{
c789ca20
SI
304 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
305
fc24b426
BJ
306 if (info == NULL) {
307 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
c789ca20 308 return -EINVAL;
fc24b426 309 }
c789ca20
SI
310
311 return info->fixed_uV;
312}
313
314static struct regulator_ops ab8500_ldo_fixed_ops = {
315 .enable = ab8500_regulator_enable,
316 .disable = ab8500_regulator_disable,
317 .is_enabled = ab8500_regulator_is_enabled,
318 .get_voltage = ab8500_fixed_get_voltage,
319 .list_voltage = ab8500_list_voltage,
320};
321
e1159e6d
BJ
322#define AB8500_LDO(_id, _min_mV, _max_mV, \
323 _u_bank, _u_reg, _u_mask, _u_val_enable, \
324 _v_bank, _v_reg, _v_mask, _v_table, _v_table_len) \
325[AB8500_LDO_##_id] = { \
c789ca20 326 .desc = { \
e1159e6d
BJ
327 .name = "LDO-" #_id, \
328 .ops = &ab8500_regulator_ops, \
329 .type = REGULATOR_VOLTAGE, \
330 .id = AB8500_LDO_##_id, \
331 .owner = THIS_MODULE, \
c789ca20 332 }, \
e1159e6d
BJ
333 .min_uV = (_min_mV) * 1000, \
334 .max_uV = (_max_mV) * 1000, \
335 .update_bank = _u_bank, \
336 .update_reg = _u_reg, \
337 .update_mask = _u_mask, \
338 .update_val_enable = _u_val_enable, \
339 .voltage_bank = _v_bank, \
340 .voltage_reg = _v_reg, \
341 .voltage_mask = _v_mask, \
342 .voltages = _v_table, \
343 .voltages_len = _v_table_len, \
344 .fixed_uV = 0, \
c789ca20
SI
345}
346
e1159e6d
BJ
347#define AB8500_FIXED_LDO(_id, _fixed_mV, \
348 _u_bank, _u_reg, _u_mask, _u_val_enable) \
349[AB8500_LDO_##_id] = { \
350 .desc = { \
351 .name = "LDO-" #_id, \
352 .ops = &ab8500_ldo_fixed_ops, \
353 .type = REGULATOR_VOLTAGE, \
354 .id = AB8500_LDO_##_id, \
355 .owner = THIS_MODULE, \
356 }, \
357 .fixed_uV = (_fixed_mV) * 1000, \
358 .update_bank = _u_bank, \
359 .update_reg = _u_reg, \
360 .update_mask = _u_mask, \
361 .update_val_enable = _u_val_enable, \
c789ca20
SI
362}
363
364static struct ab8500_regulator_info ab8500_regulator_info[] = {
365 /*
e1159e6d
BJ
366 * Variable Voltage Regulators
367 * name, min mV, max mV,
368 * update bank, reg, mask, enable val
369 * volt bank, reg, mask, table, table length
c789ca20 370 */
e1159e6d
BJ
371 AB8500_LDO(AUX1, 1100, 3300,
372 0x04, 0x09, 0x03, 0x01, 0x04, 0x1f, 0x0f,
373 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
374 AB8500_LDO(AUX2, 1100, 3300,
375 0x04, 0x09, 0x0c, 0x04, 0x04, 0x20, 0x0f,
376 ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
377 AB8500_LDO(AUX3, 1100, 3300,
378 0x04, 0x0a, 0x03, 0x01, 0x04, 0x21, 0x07,
379 ldo_vaux3_voltages, ARRAY_SIZE(ldo_vaux3_voltages)),
380 AB8500_LDO(INTCORE, 1100, 3300,
381 0x03, 0x80, 0x44, 0x04, 0x03, 0x80, 0x38,
c789ca20
SI
382 ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),
383
384 /*
e1159e6d
BJ
385 * Fixed Voltage Regulators
386 * name, fixed mV,
387 * update bank, reg, mask, enable val
c789ca20 388 */
e1159e6d
BJ
389 AB8500_FIXED_LDO(TVOUT, 2000, 0x03, 0x80, 0x82, 0x02),
390 AB8500_FIXED_LDO(AUDIO, 2000, 0x03, 0x83, 0x02, 0x02),
391 AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03, 0x83, 0x08, 0x08),
392 AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03, 0x83, 0x10, 0x10),
393 AB8500_FIXED_LDO(DMIC, 1800, 0x03, 0x83, 0x04, 0x04),
394 AB8500_FIXED_LDO(ANA, 1200, 0x04, 0x06, 0x0c, 0x04),
c789ca20
SI
395};
396
c789ca20
SI
397static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
398{
399 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
af54decd 400 struct ab8500_platform_data *pdata;
c789ca20
SI
401 int i, err;
402
403 if (!ab8500) {
404 dev_err(&pdev->dev, "null mfd parent\n");
405 return -EINVAL;
406 }
af54decd 407 pdata = dev_get_platdata(ab8500->dev);
fc24b426
BJ
408 if (!pdata) {
409 dev_err(&pdev->dev, "null pdata\n");
410 return -EINVAL;
411 }
c789ca20 412
cb189b07
BJ
413 /* make sure the platform data has the correct size */
414 if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
415 dev_err(&pdev->dev, "platform configuration error\n");
416 return -EINVAL;
417 }
418
c789ca20
SI
419 /* register all regulators */
420 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
421 struct ab8500_regulator_info *info = NULL;
422
423 /* assign per-regulator data */
424 info = &ab8500_regulator_info[i];
425 info->dev = &pdev->dev;
c789ca20 426
2b75151a
BJ
427 /* fix for hardware before ab8500v2.0 */
428 if (abx500_get_chip_id(info->dev) < 0x20) {
429 if (info->desc.id == AB8500_LDO_AUX3) {
430 info->desc.n_voltages =
431 ARRAY_SIZE(ldo_vauxn_voltages);
e1159e6d 432 info->voltages = ldo_vauxn_voltages;
2b75151a
BJ
433 info->voltages_len =
434 ARRAY_SIZE(ldo_vauxn_voltages);
435 info->voltage_mask = 0xf;
436 }
437 }
438
439 /* register regulator with framework */
c789ca20 440 info->regulator = regulator_register(&info->desc, &pdev->dev,
cb189b07 441 &pdata->regulator[i], info);
c789ca20
SI
442 if (IS_ERR(info->regulator)) {
443 err = PTR_ERR(info->regulator);
444 dev_err(&pdev->dev, "failed to register regulator %s\n",
445 info->desc.name);
446 /* when we fail, un-register all earlier regulators */
d4876a3b 447 while (--i >= 0) {
c789ca20
SI
448 info = &ab8500_regulator_info[i];
449 regulator_unregister(info->regulator);
c789ca20
SI
450 }
451 return err;
452 }
09aefa12
BJ
453
454 dev_vdbg(rdev_get_dev(info->regulator),
455 "%s-probed\n", info->desc.name);
c789ca20
SI
456 }
457
458 return 0;
459}
460
461static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
462{
463 int i;
464
465 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
466 struct ab8500_regulator_info *info = NULL;
467 info = &ab8500_regulator_info[i];
09aefa12
BJ
468
469 dev_vdbg(rdev_get_dev(info->regulator),
470 "%s-remove\n", info->desc.name);
471
c789ca20
SI
472 regulator_unregister(info->regulator);
473 }
474
475 return 0;
476}
477
478static struct platform_driver ab8500_regulator_driver = {
479 .probe = ab8500_regulator_probe,
480 .remove = __devexit_p(ab8500_regulator_remove),
481 .driver = {
482 .name = "ab8500-regulator",
483 .owner = THIS_MODULE,
484 },
485};
486
487static int __init ab8500_regulator_init(void)
488{
489 int ret;
490
491 ret = platform_driver_register(&ab8500_regulator_driver);
492 if (ret != 0)
493 pr_err("Failed to register ab8500 regulator: %d\n", ret);
494
495 return ret;
496}
497subsys_initcall(ab8500_regulator_init);
498
499static void __exit ab8500_regulator_exit(void)
500{
501 platform_driver_unregister(&ab8500_regulator_driver);
502}
503module_exit(ab8500_regulator_exit);
504
505MODULE_LICENSE("GPL v2");
506MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
507MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
508MODULE_ALIAS("platform:ab8500-regulator");
This page took 0.071083 seconds and 5 git commands to generate.