2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License v2
6 * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
7 * Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
9 * AB8500 peripheral regulators
11 * AB8500 supports the following regulators:
12 * VAUX1/2/3, VINTCORE, VTVOUT, VAUDIO, VAMIC1/2, VDMIC, VANA
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>
19 #include <linux/mfd/abx500.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/ab8500.h>
25 * struct ab8500_regulator_info - ab8500 regulator information
26 * @dev: device pointer
27 * @desc: regulator description
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)
32 * @update_bank: bank to control on/off
33 * @update_reg: register to control on/off
34 * @update_mask: mask to enable/disable regulator
35 * @update_val_enable: bits to enable the regulator in normal (high power) mode
36 * @voltage_bank: bank to control regulator voltage
37 * @voltage_reg: register to control regulator voltage
38 * @voltage_mask: mask to control regulator voltage
39 * @voltages: supported voltage table
40 * @voltages_len: number of supported voltages for the regulator
42 struct ab8500_regulator_info {
44 struct regulator_desc desc;
45 struct regulator_dev *regulator;
60 /* voltage tables for the vauxn/vintcore supplies */
61 static const int ldo_vauxn_voltages[] = {
80 static const int ldo_vaux3_voltages[] = {
91 static const int ldo_vintcore_voltages[] = {
101 static int ab8500_regulator_enable(struct regulator_dev *rdev)
104 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
107 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
111 ret = abx500_mask_and_set_register_interruptible(info->dev,
112 info->update_bank, info->update_reg,
113 info->update_mask, info->update_val_enable);
115 dev_err(rdev_get_dev(rdev),
116 "couldn't set enable bits for regulator\n");
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);
126 static int ab8500_regulator_disable(struct regulator_dev *rdev)
129 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
132 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
136 ret = abx500_mask_and_set_register_interruptible(info->dev,
137 info->update_bank, info->update_reg,
138 info->update_mask, 0x0);
140 dev_err(rdev_get_dev(rdev),
141 "couldn't set disable bits for regulator\n");
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);
151 static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
154 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
158 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
162 ret = abx500_get_register_interruptible(info->dev,
163 info->update_bank, info->update_reg, ®val);
165 dev_err(rdev_get_dev(rdev),
166 "couldn't read 0x%x register\n", info->update_reg);
170 dev_vdbg(rdev_get_dev(rdev),
171 "%s-is_enabled (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
173 info->desc.name, info->update_bank, info->update_reg,
174 info->update_mask, regval);
176 if (regval & info->update_mask)
182 static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
184 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
187 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
191 /* return the uV for the fixed regulators */
193 return info->fixed_uV;
195 if (selector >= info->voltages_len)
198 return info->voltages[selector];
201 static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
204 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
208 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
212 ret = abx500_get_register_interruptible(info->dev,
213 info->voltage_bank, info->voltage_reg, ®val);
215 dev_err(rdev_get_dev(rdev),
216 "couldn't read voltage reg for regulator\n");
220 dev_vdbg(rdev_get_dev(rdev),
221 "%s-get_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
223 info->desc.name, info->voltage_bank, info->voltage_reg,
224 info->voltage_mask, regval);
226 /* vintcore has a different layout */
227 val = regval & info->voltage_mask;
228 if (info->desc.id == AB8500_LDO_INTCORE)
229 ret = info->voltages[val >> 0x3];
231 ret = info->voltages[val];
236 static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
237 int min_uV, int max_uV)
239 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
242 /* check the supported voltage */
243 for (i = 0; i < info->voltages_len; i++) {
244 if ((info->voltages[i] >= min_uV) &&
245 (info->voltages[i] <= max_uV))
252 static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
253 int min_uV, int max_uV,
257 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
261 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
265 /* get the appropriate voltages within the range */
266 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
268 dev_err(rdev_get_dev(rdev),
269 "couldn't get best voltage for regulator\n");
275 /* set the registers for the request */
277 ret = abx500_mask_and_set_register_interruptible(info->dev,
278 info->voltage_bank, info->voltage_reg,
279 info->voltage_mask, regval);
281 dev_err(rdev_get_dev(rdev),
282 "couldn't set voltage reg for regulator\n");
284 dev_vdbg(rdev_get_dev(rdev),
285 "%s-set_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
287 info->desc.name, info->voltage_bank, info->voltage_reg,
288 info->voltage_mask, regval);
293 static 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,
302 static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
304 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
307 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
311 return info->fixed_uV;
314 static struct regulator_ops ab8500_regulator_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,
322 static struct ab8500_regulator_info
323 ab8500_regulator_info[AB8500_NUM_REGULATORS] = {
325 * Variable Voltage Regulators
326 * name, min mV, max mV,
327 * update bank, reg, mask, enable val
328 * volt bank, reg, mask, table, table length
330 [AB8500_LDO_AUX1] = {
333 .ops = &ab8500_regulator_ops,
334 .type = REGULATOR_VOLTAGE,
335 .id = AB8500_LDO_AUX1,
336 .owner = THIS_MODULE,
337 .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
344 .update_val_enable = 0x01,
345 .voltage_bank = 0x04,
347 .voltage_mask = 0x0f,
348 .voltages = ldo_vauxn_voltages,
349 .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
351 [AB8500_LDO_AUX2] = {
354 .ops = &ab8500_regulator_ops,
355 .type = REGULATOR_VOLTAGE,
356 .id = AB8500_LDO_AUX2,
357 .owner = THIS_MODULE,
358 .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
365 .update_val_enable = 0x04,
366 .voltage_bank = 0x04,
368 .voltage_mask = 0x0f,
369 .voltages = ldo_vauxn_voltages,
370 .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
372 [AB8500_LDO_AUX3] = {
375 .ops = &ab8500_regulator_ops,
376 .type = REGULATOR_VOLTAGE,
377 .id = AB8500_LDO_AUX3,
378 .owner = THIS_MODULE,
379 .n_voltages = ARRAY_SIZE(ldo_vaux3_voltages),
386 .update_val_enable = 0x01,
387 .voltage_bank = 0x04,
389 .voltage_mask = 0x07,
390 .voltages = ldo_vaux3_voltages,
391 .voltages_len = ARRAY_SIZE(ldo_vaux3_voltages),
393 [AB8500_LDO_INTCORE] = {
395 .name = "LDO-INTCORE",
396 .ops = &ab8500_regulator_ops,
397 .type = REGULATOR_VOLTAGE,
398 .id = AB8500_LDO_INTCORE,
399 .owner = THIS_MODULE,
400 .n_voltages = ARRAY_SIZE(ldo_vintcore_voltages),
407 .update_val_enable = 0x04,
408 .voltage_bank = 0x03,
410 .voltage_mask = 0x38,
411 .voltages = ldo_vintcore_voltages,
412 .voltages_len = ARRAY_SIZE(ldo_vintcore_voltages),
416 * Fixed Voltage Regulators
418 * update bank, reg, mask, enable val
420 [AB8500_LDO_TVOUT] = {
423 .ops = &ab8500_regulator_fixed_ops,
424 .type = REGULATOR_VOLTAGE,
425 .id = AB8500_LDO_TVOUT,
426 .owner = THIS_MODULE,
433 .update_val_enable = 0x02,
435 [AB8500_LDO_AUDIO] = {
438 .ops = &ab8500_regulator_fixed_ops,
439 .type = REGULATOR_VOLTAGE,
440 .id = AB8500_LDO_AUDIO,
441 .owner = THIS_MODULE,
448 .update_val_enable = 0x02,
450 [AB8500_LDO_ANAMIC1] = {
452 .name = "LDO-ANAMIC1",
453 .ops = &ab8500_regulator_fixed_ops,
454 .type = REGULATOR_VOLTAGE,
455 .id = AB8500_LDO_ANAMIC1,
456 .owner = THIS_MODULE,
463 .update_val_enable = 0x08,
465 [AB8500_LDO_ANAMIC2] = {
467 .name = "LDO-ANAMIC2",
468 .ops = &ab8500_regulator_fixed_ops,
469 .type = REGULATOR_VOLTAGE,
470 .id = AB8500_LDO_ANAMIC2,
471 .owner = THIS_MODULE,
478 .update_val_enable = 0x10,
480 [AB8500_LDO_DMIC] = {
483 .ops = &ab8500_regulator_fixed_ops,
484 .type = REGULATOR_VOLTAGE,
485 .id = AB8500_LDO_DMIC,
486 .owner = THIS_MODULE,
493 .update_val_enable = 0x04,
498 .ops = &ab8500_regulator_fixed_ops,
499 .type = REGULATOR_VOLTAGE,
500 .id = AB8500_LDO_ANA,
501 .owner = THIS_MODULE,
508 .update_val_enable = 0x04,
514 static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
516 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
517 struct ab8500_platform_data *pdata;
521 dev_err(&pdev->dev, "null mfd parent\n");
524 pdata = dev_get_platdata(ab8500->dev);
526 dev_err(&pdev->dev, "null pdata\n");
530 /* make sure the platform data has the correct size */
531 if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
532 dev_err(&pdev->dev, "platform configuration error\n");
536 /* register all regulators */
537 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
538 struct ab8500_regulator_info *info = NULL;
540 /* assign per-regulator data */
541 info = &ab8500_regulator_info[i];
542 info->dev = &pdev->dev;
544 /* fix for hardware before ab8500v2.0 */
545 if (abx500_get_chip_id(info->dev) < 0x20) {
546 if (info->desc.id == AB8500_LDO_AUX3) {
547 info->desc.n_voltages =
548 ARRAY_SIZE(ldo_vauxn_voltages);
549 info->voltages = ldo_vauxn_voltages;
551 ARRAY_SIZE(ldo_vauxn_voltages);
552 info->voltage_mask = 0xf;
556 /* register regulator with framework */
557 info->regulator = regulator_register(&info->desc, &pdev->dev,
558 &pdata->regulator[i], info);
559 if (IS_ERR(info->regulator)) {
560 err = PTR_ERR(info->regulator);
561 dev_err(&pdev->dev, "failed to register regulator %s\n",
563 /* when we fail, un-register all earlier regulators */
565 info = &ab8500_regulator_info[i];
566 regulator_unregister(info->regulator);
571 dev_vdbg(rdev_get_dev(info->regulator),
572 "%s-probed\n", info->desc.name);
578 static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
582 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
583 struct ab8500_regulator_info *info = NULL;
584 info = &ab8500_regulator_info[i];
586 dev_vdbg(rdev_get_dev(info->regulator),
587 "%s-remove\n", info->desc.name);
589 regulator_unregister(info->regulator);
595 static struct platform_driver ab8500_regulator_driver = {
596 .probe = ab8500_regulator_probe,
597 .remove = __devexit_p(ab8500_regulator_remove),
599 .name = "ab8500-regulator",
600 .owner = THIS_MODULE,
604 static int __init ab8500_regulator_init(void)
608 ret = platform_driver_register(&ab8500_regulator_driver);
610 pr_err("Failed to register ab8500 regulator: %d\n", ret);
614 subsys_initcall(ab8500_regulator_init);
616 static void __exit ab8500_regulator_exit(void)
618 platform_driver_unregister(&ab8500_regulator_driver);
620 module_exit(ab8500_regulator_exit);
622 MODULE_LICENSE("GPL v2");
623 MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
624 MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
625 MODULE_ALIAS("platform:ab8500-regulator");