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, VUSB, VAUDIO, VAMIC1/2, VDMIC, VANA
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/mfd/abx500.h>
20 #include <linux/mfd/abx500/ab8500.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regulator/ab8500.h>
26 * struct ab8500_regulator_info - ab8500 regulator information
27 * @dev: device pointer
28 * @desc: regulator description
29 * @regulator_dev: regulator device
30 * @max_uV: maximum voltage (for variable voltage supplies)
31 * @min_uV: minimum voltage (for variable voltage supplies)
32 * @fixed_uV: typical voltage (for fixed voltage supplies)
33 * @update_bank: bank to control on/off
34 * @update_reg: register to control on/off
35 * @update_mask: mask to enable/disable regulator
36 * @update_val_enable: bits to enable the regulator in normal (high power) mode
37 * @voltage_bank: bank to control regulator voltage
38 * @voltage_reg: register to control regulator voltage
39 * @voltage_mask: mask to control regulator voltage
40 * @voltages: supported voltage table
41 * @voltages_len: number of supported voltages for the regulator
42 * @delay: startup/set voltage delay in us
44 struct ab8500_regulator_info {
46 struct regulator_desc desc;
47 struct regulator_dev *regulator;
63 /* voltage tables for the vauxn/vintcore supplies */
64 static const int ldo_vauxn_voltages[] = {
83 static const int ldo_vaux3_voltages[] = {
94 static const int ldo_vintcore_voltages[] = {
104 static int ab8500_regulator_enable(struct regulator_dev *rdev)
107 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
110 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
114 ret = abx500_mask_and_set_register_interruptible(info->dev,
115 info->update_bank, info->update_reg,
116 info->update_mask, info->update_val_enable);
118 dev_err(rdev_get_dev(rdev),
119 "couldn't set enable bits for regulator\n");
121 dev_vdbg(rdev_get_dev(rdev),
122 "%s-enable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
123 info->desc.name, info->update_bank, info->update_reg,
124 info->update_mask, info->update_val_enable);
129 static int ab8500_regulator_disable(struct regulator_dev *rdev)
132 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
135 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
139 ret = abx500_mask_and_set_register_interruptible(info->dev,
140 info->update_bank, info->update_reg,
141 info->update_mask, 0x0);
143 dev_err(rdev_get_dev(rdev),
144 "couldn't set disable bits for regulator\n");
146 dev_vdbg(rdev_get_dev(rdev),
147 "%s-disable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
148 info->desc.name, info->update_bank, info->update_reg,
149 info->update_mask, 0x0);
154 static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
157 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
161 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
165 ret = abx500_get_register_interruptible(info->dev,
166 info->update_bank, info->update_reg, ®val);
168 dev_err(rdev_get_dev(rdev),
169 "couldn't read 0x%x register\n", info->update_reg);
173 dev_vdbg(rdev_get_dev(rdev),
174 "%s-is_enabled (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
176 info->desc.name, info->update_bank, info->update_reg,
177 info->update_mask, regval);
179 if (regval & info->update_mask)
185 static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
187 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
190 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
194 /* return the uV for the fixed regulators */
196 return info->fixed_uV;
198 if (selector >= info->voltages_len)
201 return info->voltages[selector];
204 static int ab8500_regulator_get_voltage_sel(struct regulator_dev *rdev)
207 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
211 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
215 ret = abx500_get_register_interruptible(info->dev,
216 info->voltage_bank, info->voltage_reg, ®val);
218 dev_err(rdev_get_dev(rdev),
219 "couldn't read voltage reg for regulator\n");
223 dev_vdbg(rdev_get_dev(rdev),
224 "%s-get_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
226 info->desc.name, info->voltage_bank, info->voltage_reg,
227 info->voltage_mask, regval);
229 /* vintcore has a different layout */
230 val = regval & info->voltage_mask;
231 if (info->desc.id == AB8500_LDO_INTCORE)
237 static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
238 int min_uV, int max_uV)
240 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
243 /* check the supported voltage */
244 for (i = 0; i < info->voltages_len; i++) {
245 if ((info->voltages[i] >= min_uV) &&
246 (info->voltages[i] <= max_uV))
253 static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
254 int min_uV, int max_uV,
258 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
262 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
266 /* get the appropriate voltages within the range */
267 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
269 dev_err(rdev_get_dev(rdev),
270 "couldn't get best voltage for regulator\n");
276 /* set the registers for the request */
278 ret = abx500_mask_and_set_register_interruptible(info->dev,
279 info->voltage_bank, info->voltage_reg,
280 info->voltage_mask, regval);
282 dev_err(rdev_get_dev(rdev),
283 "couldn't set voltage reg for regulator\n");
285 dev_vdbg(rdev_get_dev(rdev),
286 "%s-set_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
288 info->desc.name, info->voltage_bank, info->voltage_reg,
289 info->voltage_mask, regval);
294 static int ab8500_regulator_enable_time(struct regulator_dev *rdev)
296 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
301 static int ab8500_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
302 unsigned int old_sel,
303 unsigned int new_sel)
305 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
308 /* If the regulator isn't on, it won't take time here */
309 ret = ab8500_regulator_is_enabled(rdev);
317 static struct regulator_ops ab8500_regulator_ops = {
318 .enable = ab8500_regulator_enable,
319 .disable = ab8500_regulator_disable,
320 .is_enabled = ab8500_regulator_is_enabled,
321 .get_voltage_sel = ab8500_regulator_get_voltage_sel,
322 .set_voltage = ab8500_regulator_set_voltage,
323 .list_voltage = ab8500_list_voltage,
324 .enable_time = ab8500_regulator_enable_time,
325 .set_voltage_time_sel = ab8500_regulator_set_voltage_time_sel,
328 static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
330 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
333 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
337 return info->fixed_uV;
340 static struct regulator_ops ab8500_regulator_fixed_ops = {
341 .enable = ab8500_regulator_enable,
342 .disable = ab8500_regulator_disable,
343 .is_enabled = ab8500_regulator_is_enabled,
344 .get_voltage = ab8500_fixed_get_voltage,
345 .list_voltage = ab8500_list_voltage,
346 .enable_time = ab8500_regulator_enable_time,
347 .set_voltage_time_sel = ab8500_regulator_set_voltage_time_sel,
350 static struct ab8500_regulator_info
351 ab8500_regulator_info[AB8500_NUM_REGULATORS] = {
353 * Variable Voltage Regulators
354 * name, min mV, max mV,
355 * update bank, reg, mask, enable val
356 * volt bank, reg, mask, table, table length
358 [AB8500_LDO_AUX1] = {
361 .ops = &ab8500_regulator_ops,
362 .type = REGULATOR_VOLTAGE,
363 .id = AB8500_LDO_AUX1,
364 .owner = THIS_MODULE,
365 .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
372 .update_val_enable = 0x01,
373 .voltage_bank = 0x04,
375 .voltage_mask = 0x0f,
376 .voltages = ldo_vauxn_voltages,
377 .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
379 [AB8500_LDO_AUX2] = {
382 .ops = &ab8500_regulator_ops,
383 .type = REGULATOR_VOLTAGE,
384 .id = AB8500_LDO_AUX2,
385 .owner = THIS_MODULE,
386 .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
393 .update_val_enable = 0x04,
394 .voltage_bank = 0x04,
396 .voltage_mask = 0x0f,
397 .voltages = ldo_vauxn_voltages,
398 .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
400 [AB8500_LDO_AUX3] = {
403 .ops = &ab8500_regulator_ops,
404 .type = REGULATOR_VOLTAGE,
405 .id = AB8500_LDO_AUX3,
406 .owner = THIS_MODULE,
407 .n_voltages = ARRAY_SIZE(ldo_vaux3_voltages),
414 .update_val_enable = 0x01,
415 .voltage_bank = 0x04,
417 .voltage_mask = 0x07,
418 .voltages = ldo_vaux3_voltages,
419 .voltages_len = ARRAY_SIZE(ldo_vaux3_voltages),
421 [AB8500_LDO_INTCORE] = {
423 .name = "LDO-INTCORE",
424 .ops = &ab8500_regulator_ops,
425 .type = REGULATOR_VOLTAGE,
426 .id = AB8500_LDO_INTCORE,
427 .owner = THIS_MODULE,
428 .n_voltages = ARRAY_SIZE(ldo_vintcore_voltages),
435 .update_val_enable = 0x04,
436 .voltage_bank = 0x03,
438 .voltage_mask = 0x38,
439 .voltages = ldo_vintcore_voltages,
440 .voltages_len = ARRAY_SIZE(ldo_vintcore_voltages),
444 * Fixed Voltage Regulators
446 * update bank, reg, mask, enable val
448 [AB8500_LDO_TVOUT] = {
451 .ops = &ab8500_regulator_fixed_ops,
452 .type = REGULATOR_VOLTAGE,
453 .id = AB8500_LDO_TVOUT,
454 .owner = THIS_MODULE,
462 .update_val_enable = 0x02,
467 .ops = &ab8500_regulator_fixed_ops,
468 .type = REGULATOR_VOLTAGE,
469 .id = AB8500_LDO_USB,
470 .owner = THIS_MODULE,
477 .update_val_enable = 0x01,
479 [AB8500_LDO_AUDIO] = {
482 .ops = &ab8500_regulator_fixed_ops,
483 .type = REGULATOR_VOLTAGE,
484 .id = AB8500_LDO_AUDIO,
485 .owner = THIS_MODULE,
492 .update_val_enable = 0x02,
494 [AB8500_LDO_ANAMIC1] = {
496 .name = "LDO-ANAMIC1",
497 .ops = &ab8500_regulator_fixed_ops,
498 .type = REGULATOR_VOLTAGE,
499 .id = AB8500_LDO_ANAMIC1,
500 .owner = THIS_MODULE,
507 .update_val_enable = 0x08,
509 [AB8500_LDO_ANAMIC2] = {
511 .name = "LDO-ANAMIC2",
512 .ops = &ab8500_regulator_fixed_ops,
513 .type = REGULATOR_VOLTAGE,
514 .id = AB8500_LDO_ANAMIC2,
515 .owner = THIS_MODULE,
522 .update_val_enable = 0x10,
524 [AB8500_LDO_DMIC] = {
527 .ops = &ab8500_regulator_fixed_ops,
528 .type = REGULATOR_VOLTAGE,
529 .id = AB8500_LDO_DMIC,
530 .owner = THIS_MODULE,
537 .update_val_enable = 0x04,
542 .ops = &ab8500_regulator_fixed_ops,
543 .type = REGULATOR_VOLTAGE,
544 .id = AB8500_LDO_ANA,
545 .owner = THIS_MODULE,
552 .update_val_enable = 0x04,
558 struct ab8500_reg_init {
564 #define REG_INIT(_id, _bank, _addr, _mask) \
571 static struct ab8500_reg_init ab8500_reg_init[] = {
573 * 0x30, VanaRequestCtrl
574 * 0x0C, VpllRequestCtrl
575 * 0xc0, VextSupply1RequestCtrl
577 REG_INIT(AB8500_REGUREQUESTCTRL2, 0x03, 0x04, 0xfc),
579 * 0x03, VextSupply2RequestCtrl
580 * 0x0c, VextSupply3RequestCtrl
581 * 0x30, Vaux1RequestCtrl
582 * 0xc0, Vaux2RequestCtrl
584 REG_INIT(AB8500_REGUREQUESTCTRL3, 0x03, 0x05, 0xff),
586 * 0x03, Vaux3RequestCtrl
589 REG_INIT(AB8500_REGUREQUESTCTRL4, 0x03, 0x06, 0x07),
591 * 0x08, VanaSysClkReq1HPValid
592 * 0x20, Vaux1SysClkReq1HPValid
593 * 0x40, Vaux2SysClkReq1HPValid
594 * 0x80, Vaux3SysClkReq1HPValid
596 REG_INIT(AB8500_REGUSYSCLKREQ1HPVALID1, 0x03, 0x07, 0xe8),
598 * 0x10, VextSupply1SysClkReq1HPValid
599 * 0x20, VextSupply2SysClkReq1HPValid
600 * 0x40, VextSupply3SysClkReq1HPValid
602 REG_INIT(AB8500_REGUSYSCLKREQ1HPVALID2, 0x03, 0x08, 0x70),
604 * 0x08, VanaHwHPReq1Valid
605 * 0x20, Vaux1HwHPReq1Valid
606 * 0x40, Vaux2HwHPReq1Valid
607 * 0x80, Vaux3HwHPReq1Valid
609 REG_INIT(AB8500_REGUHWHPREQ1VALID1, 0x03, 0x09, 0xe8),
611 * 0x01, VextSupply1HwHPReq1Valid
612 * 0x02, VextSupply2HwHPReq1Valid
613 * 0x04, VextSupply3HwHPReq1Valid
615 REG_INIT(AB8500_REGUHWHPREQ1VALID2, 0x03, 0x0a, 0x07),
617 * 0x08, VanaHwHPReq2Valid
618 * 0x20, Vaux1HwHPReq2Valid
619 * 0x40, Vaux2HwHPReq2Valid
620 * 0x80, Vaux3HwHPReq2Valid
622 REG_INIT(AB8500_REGUHWHPREQ2VALID1, 0x03, 0x0b, 0xe8),
624 * 0x01, VextSupply1HwHPReq2Valid
625 * 0x02, VextSupply2HwHPReq2Valid
626 * 0x04, VextSupply3HwHPReq2Valid
628 REG_INIT(AB8500_REGUHWHPREQ2VALID2, 0x03, 0x0c, 0x07),
630 * 0x20, VanaSwHPReqValid
631 * 0x80, Vaux1SwHPReqValid
633 REG_INIT(AB8500_REGUSWHPREQVALID1, 0x03, 0x0d, 0xa0),
635 * 0x01, Vaux2SwHPReqValid
636 * 0x02, Vaux3SwHPReqValid
637 * 0x04, VextSupply1SwHPReqValid
638 * 0x08, VextSupply2SwHPReqValid
639 * 0x10, VextSupply3SwHPReqValid
641 REG_INIT(AB8500_REGUSWHPREQVALID2, 0x03, 0x0e, 0x1f),
643 * 0x02, SysClkReq2Valid1
645 * 0x80, SysClkReq8Valid1
647 REG_INIT(AB8500_REGUSYSCLKREQVALID1, 0x03, 0x0f, 0xfe),
649 * 0x02, SysClkReq2Valid2
651 * 0x80, SysClkReq8Valid2
653 REG_INIT(AB8500_REGUSYSCLKREQVALID2, 0x03, 0x10, 0xfe),
656 * 0x04, Vintcore12Ena
657 * 0x38, Vintcore12Sel
661 REG_INIT(AB8500_REGUMISC1, 0x03, 0x80, 0xfe),
668 REG_INIT(AB8500_VAUDIOSUPPLY, 0x03, 0x83, 0x1e),
673 REG_INIT(AB8500_REGUCTRL1VAMIC, 0x03, 0x84, 0x03),
678 REG_INIT(AB8500_VPLLVANAREGU, 0x04, 0x06, 0x0f),
681 * 0x02, VrefDDRSleepMode
683 REG_INIT(AB8500_VREFDDR, 0x04, 0x07, 0x03),
685 * 0x03, VextSupply1Regu
686 * 0x0c, VextSupply2Regu
687 * 0x30, VextSupply3Regu
688 * 0x40, ExtSupply2Bypass
689 * 0x80, ExtSupply3Bypass
691 REG_INIT(AB8500_EXTSUPPLYREGU, 0x04, 0x08, 0xff),
696 REG_INIT(AB8500_VAUX12REGU, 0x04, 0x09, 0x0f),
700 REG_INIT(AB8500_VRF1VAUX3REGU, 0x04, 0x0a, 0x03),
704 REG_INIT(AB8500_VSMPS1SEL1, 0x04, 0x13, 0x3f),
708 REG_INIT(AB8500_VAUX1SEL, 0x04, 0x1f, 0x0f),
712 REG_INIT(AB8500_VAUX2SEL, 0x04, 0x20, 0x0f),
716 REG_INIT(AB8500_VRF1VAUX3SEL, 0x04, 0x21, 0x07),
718 * 0x01, VextSupply12LP
720 REG_INIT(AB8500_REGUCTRL2SPARE, 0x04, 0x22, 0x01),
725 * 0x20, Vintcore12Disch
729 REG_INIT(AB8500_REGUCTRLDISCH, 0x04, 0x43, 0xfc),
732 * 0x04, VdmicPullDownEna
735 REG_INIT(AB8500_REGUCTRLDISCH2, 0x04, 0x44, 0x16),
738 static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
740 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
741 struct ab8500_platform_data *pdata;
745 dev_err(&pdev->dev, "null mfd parent\n");
748 pdata = dev_get_platdata(ab8500->dev);
750 dev_err(&pdev->dev, "null pdata\n");
754 /* make sure the platform data has the correct size */
755 if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
756 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
760 /* initialize registers */
761 for (i = 0; i < pdata->num_regulator_reg_init; i++) {
765 id = pdata->regulator_reg_init[i].id;
766 value = pdata->regulator_reg_init[i].value;
768 /* check for configuration errors */
769 if (id >= AB8500_NUM_REGULATOR_REGISTERS) {
771 "Configuration error: id outside range.\n");
774 if (value & ~ab8500_reg_init[id].mask) {
776 "Configuration error: value outside mask.\n");
780 /* initialize register */
781 err = abx500_mask_and_set_register_interruptible(&pdev->dev,
782 ab8500_reg_init[id].bank,
783 ab8500_reg_init[id].addr,
784 ab8500_reg_init[id].mask,
788 "Failed to initialize 0x%02x, 0x%02x.\n",
789 ab8500_reg_init[id].bank,
790 ab8500_reg_init[id].addr);
794 " init: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
795 ab8500_reg_init[id].bank,
796 ab8500_reg_init[id].addr,
797 ab8500_reg_init[id].mask,
801 /* register all regulators */
802 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
803 struct ab8500_regulator_info *info = NULL;
805 /* assign per-regulator data */
806 info = &ab8500_regulator_info[i];
807 info->dev = &pdev->dev;
809 /* fix for hardware before ab8500v2.0 */
810 if (abx500_get_chip_id(info->dev) < 0x20) {
811 if (info->desc.id == AB8500_LDO_AUX3) {
812 info->desc.n_voltages =
813 ARRAY_SIZE(ldo_vauxn_voltages);
814 info->voltages = ldo_vauxn_voltages;
816 ARRAY_SIZE(ldo_vauxn_voltages);
817 info->voltage_mask = 0xf;
821 /* register regulator with framework */
822 info->regulator = regulator_register(&info->desc, &pdev->dev,
823 &pdata->regulator[i], info, NULL);
824 if (IS_ERR(info->regulator)) {
825 err = PTR_ERR(info->regulator);
826 dev_err(&pdev->dev, "failed to register regulator %s\n",
828 /* when we fail, un-register all earlier regulators */
830 info = &ab8500_regulator_info[i];
831 regulator_unregister(info->regulator);
836 dev_vdbg(rdev_get_dev(info->regulator),
837 "%s-probed\n", info->desc.name);
843 static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
847 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
848 struct ab8500_regulator_info *info = NULL;
849 info = &ab8500_regulator_info[i];
851 dev_vdbg(rdev_get_dev(info->regulator),
852 "%s-remove\n", info->desc.name);
854 regulator_unregister(info->regulator);
860 static struct platform_driver ab8500_regulator_driver = {
861 .probe = ab8500_regulator_probe,
862 .remove = __devexit_p(ab8500_regulator_remove),
864 .name = "ab8500-regulator",
865 .owner = THIS_MODULE,
869 static int __init ab8500_regulator_init(void)
873 ret = platform_driver_register(&ab8500_regulator_driver);
875 pr_err("Failed to register ab8500 regulator: %d\n", ret);
879 subsys_initcall(ab8500_regulator_init);
881 static void __exit ab8500_regulator_exit(void)
883 platform_driver_unregister(&ab8500_regulator_driver);
885 module_exit(ab8500_regulator_exit);
887 MODULE_LICENSE("GPL v2");
888 MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
889 MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
890 MODULE_ALIAS("platform:ab8500-regulator");