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/ab8500.h>
20 #include <linux/mfd/abx500.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(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)
232 ret = info->voltages[val >> 0x3];
234 ret = info->voltages[val];
239 static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
240 int min_uV, int max_uV)
242 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
245 /* check the supported voltage */
246 for (i = 0; i < info->voltages_len; i++) {
247 if ((info->voltages[i] >= min_uV) &&
248 (info->voltages[i] <= max_uV))
255 static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
256 int min_uV, int max_uV,
260 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
264 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
268 /* get the appropriate voltages within the range */
269 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
271 dev_err(rdev_get_dev(rdev),
272 "couldn't get best voltage for regulator\n");
278 /* set the registers for the request */
280 ret = abx500_mask_and_set_register_interruptible(info->dev,
281 info->voltage_bank, info->voltage_reg,
282 info->voltage_mask, regval);
284 dev_err(rdev_get_dev(rdev),
285 "couldn't set voltage reg for regulator\n");
287 dev_vdbg(rdev_get_dev(rdev),
288 "%s-set_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
290 info->desc.name, info->voltage_bank, info->voltage_reg,
291 info->voltage_mask, regval);
296 static int ab8500_regulator_enable_time(struct regulator_dev *rdev)
298 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
303 static int ab8500_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
304 unsigned int old_sel,
305 unsigned int new_sel)
307 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
310 /* If the regulator isn't on, it won't take time here */
311 ret = ab8500_regulator_is_enabled(rdev);
319 static struct regulator_ops ab8500_regulator_ops = {
320 .enable = ab8500_regulator_enable,
321 .disable = ab8500_regulator_disable,
322 .is_enabled = ab8500_regulator_is_enabled,
323 .get_voltage = ab8500_regulator_get_voltage,
324 .set_voltage = ab8500_regulator_set_voltage,
325 .list_voltage = ab8500_list_voltage,
326 .enable_time = ab8500_regulator_enable_time,
327 .set_voltage_time_sel = ab8500_regulator_set_voltage_time_sel,
330 static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
332 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
335 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
339 return info->fixed_uV;
342 static struct regulator_ops ab8500_regulator_fixed_ops = {
343 .enable = ab8500_regulator_enable,
344 .disable = ab8500_regulator_disable,
345 .is_enabled = ab8500_regulator_is_enabled,
346 .get_voltage = ab8500_fixed_get_voltage,
347 .list_voltage = ab8500_list_voltage,
348 .enable_time = ab8500_regulator_enable_time,
349 .set_voltage_time_sel = ab8500_regulator_set_voltage_time_sel,
352 static struct ab8500_regulator_info
353 ab8500_regulator_info[AB8500_NUM_REGULATORS] = {
355 * Variable Voltage Regulators
356 * name, min mV, max mV,
357 * update bank, reg, mask, enable val
358 * volt bank, reg, mask, table, table length
360 [AB8500_LDO_AUX1] = {
363 .ops = &ab8500_regulator_ops,
364 .type = REGULATOR_VOLTAGE,
365 .id = AB8500_LDO_AUX1,
366 .owner = THIS_MODULE,
367 .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
374 .update_val_enable = 0x01,
375 .voltage_bank = 0x04,
377 .voltage_mask = 0x0f,
378 .voltages = ldo_vauxn_voltages,
379 .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
381 [AB8500_LDO_AUX2] = {
384 .ops = &ab8500_regulator_ops,
385 .type = REGULATOR_VOLTAGE,
386 .id = AB8500_LDO_AUX2,
387 .owner = THIS_MODULE,
388 .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
395 .update_val_enable = 0x04,
396 .voltage_bank = 0x04,
398 .voltage_mask = 0x0f,
399 .voltages = ldo_vauxn_voltages,
400 .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
402 [AB8500_LDO_AUX3] = {
405 .ops = &ab8500_regulator_ops,
406 .type = REGULATOR_VOLTAGE,
407 .id = AB8500_LDO_AUX3,
408 .owner = THIS_MODULE,
409 .n_voltages = ARRAY_SIZE(ldo_vaux3_voltages),
416 .update_val_enable = 0x01,
417 .voltage_bank = 0x04,
419 .voltage_mask = 0x07,
420 .voltages = ldo_vaux3_voltages,
421 .voltages_len = ARRAY_SIZE(ldo_vaux3_voltages),
423 [AB8500_LDO_INTCORE] = {
425 .name = "LDO-INTCORE",
426 .ops = &ab8500_regulator_ops,
427 .type = REGULATOR_VOLTAGE,
428 .id = AB8500_LDO_INTCORE,
429 .owner = THIS_MODULE,
430 .n_voltages = ARRAY_SIZE(ldo_vintcore_voltages),
437 .update_val_enable = 0x04,
438 .voltage_bank = 0x03,
440 .voltage_mask = 0x38,
441 .voltages = ldo_vintcore_voltages,
442 .voltages_len = ARRAY_SIZE(ldo_vintcore_voltages),
446 * Fixed Voltage Regulators
448 * update bank, reg, mask, enable val
450 [AB8500_LDO_TVOUT] = {
453 .ops = &ab8500_regulator_fixed_ops,
454 .type = REGULATOR_VOLTAGE,
455 .id = AB8500_LDO_TVOUT,
456 .owner = THIS_MODULE,
464 .update_val_enable = 0x02,
469 .ops = &ab8500_regulator_fixed_ops,
470 .type = REGULATOR_VOLTAGE,
471 .id = AB8500_LDO_USB,
472 .owner = THIS_MODULE,
479 .update_val_enable = 0x01,
481 [AB8500_LDO_AUDIO] = {
484 .ops = &ab8500_regulator_fixed_ops,
485 .type = REGULATOR_VOLTAGE,
486 .id = AB8500_LDO_AUDIO,
487 .owner = THIS_MODULE,
494 .update_val_enable = 0x02,
496 [AB8500_LDO_ANAMIC1] = {
498 .name = "LDO-ANAMIC1",
499 .ops = &ab8500_regulator_fixed_ops,
500 .type = REGULATOR_VOLTAGE,
501 .id = AB8500_LDO_ANAMIC1,
502 .owner = THIS_MODULE,
509 .update_val_enable = 0x08,
511 [AB8500_LDO_ANAMIC2] = {
513 .name = "LDO-ANAMIC2",
514 .ops = &ab8500_regulator_fixed_ops,
515 .type = REGULATOR_VOLTAGE,
516 .id = AB8500_LDO_ANAMIC2,
517 .owner = THIS_MODULE,
524 .update_val_enable = 0x10,
526 [AB8500_LDO_DMIC] = {
529 .ops = &ab8500_regulator_fixed_ops,
530 .type = REGULATOR_VOLTAGE,
531 .id = AB8500_LDO_DMIC,
532 .owner = THIS_MODULE,
539 .update_val_enable = 0x04,
544 .ops = &ab8500_regulator_fixed_ops,
545 .type = REGULATOR_VOLTAGE,
546 .id = AB8500_LDO_ANA,
547 .owner = THIS_MODULE,
554 .update_val_enable = 0x04,
560 struct ab8500_reg_init {
566 #define REG_INIT(_id, _bank, _addr, _mask) \
573 static struct ab8500_reg_init ab8500_reg_init[] = {
575 * 0x30, VanaRequestCtrl
576 * 0x0C, VpllRequestCtrl
577 * 0xc0, VextSupply1RequestCtrl
579 REG_INIT(AB8500_REGUREQUESTCTRL2, 0x03, 0x04, 0xfc),
581 * 0x03, VextSupply2RequestCtrl
582 * 0x0c, VextSupply3RequestCtrl
583 * 0x30, Vaux1RequestCtrl
584 * 0xc0, Vaux2RequestCtrl
586 REG_INIT(AB8500_REGUREQUESTCTRL3, 0x03, 0x05, 0xff),
588 * 0x03, Vaux3RequestCtrl
591 REG_INIT(AB8500_REGUREQUESTCTRL4, 0x03, 0x06, 0x07),
593 * 0x08, VanaSysClkReq1HPValid
594 * 0x20, Vaux1SysClkReq1HPValid
595 * 0x40, Vaux2SysClkReq1HPValid
596 * 0x80, Vaux3SysClkReq1HPValid
598 REG_INIT(AB8500_REGUSYSCLKREQ1HPVALID1, 0x03, 0x07, 0xe8),
600 * 0x10, VextSupply1SysClkReq1HPValid
601 * 0x20, VextSupply2SysClkReq1HPValid
602 * 0x40, VextSupply3SysClkReq1HPValid
604 REG_INIT(AB8500_REGUSYSCLKREQ1HPVALID2, 0x03, 0x08, 0x70),
606 * 0x08, VanaHwHPReq1Valid
607 * 0x20, Vaux1HwHPReq1Valid
608 * 0x40, Vaux2HwHPReq1Valid
609 * 0x80, Vaux3HwHPReq1Valid
611 REG_INIT(AB8500_REGUHWHPREQ1VALID1, 0x03, 0x09, 0xe8),
613 * 0x01, VextSupply1HwHPReq1Valid
614 * 0x02, VextSupply2HwHPReq1Valid
615 * 0x04, VextSupply3HwHPReq1Valid
617 REG_INIT(AB8500_REGUHWHPREQ1VALID2, 0x03, 0x0a, 0x07),
619 * 0x08, VanaHwHPReq2Valid
620 * 0x20, Vaux1HwHPReq2Valid
621 * 0x40, Vaux2HwHPReq2Valid
622 * 0x80, Vaux3HwHPReq2Valid
624 REG_INIT(AB8500_REGUHWHPREQ2VALID1, 0x03, 0x0b, 0xe8),
626 * 0x01, VextSupply1HwHPReq2Valid
627 * 0x02, VextSupply2HwHPReq2Valid
628 * 0x04, VextSupply3HwHPReq2Valid
630 REG_INIT(AB8500_REGUHWHPREQ2VALID2, 0x03, 0x0c, 0x07),
632 * 0x20, VanaSwHPReqValid
633 * 0x80, Vaux1SwHPReqValid
635 REG_INIT(AB8500_REGUSWHPREQVALID1, 0x03, 0x0d, 0xa0),
637 * 0x01, Vaux2SwHPReqValid
638 * 0x02, Vaux3SwHPReqValid
639 * 0x04, VextSupply1SwHPReqValid
640 * 0x08, VextSupply2SwHPReqValid
641 * 0x10, VextSupply3SwHPReqValid
643 REG_INIT(AB8500_REGUSWHPREQVALID2, 0x03, 0x0e, 0x1f),
645 * 0x02, SysClkReq2Valid1
647 * 0x80, SysClkReq8Valid1
649 REG_INIT(AB8500_REGUSYSCLKREQVALID1, 0x03, 0x0f, 0xfe),
651 * 0x02, SysClkReq2Valid2
653 * 0x80, SysClkReq8Valid2
655 REG_INIT(AB8500_REGUSYSCLKREQVALID2, 0x03, 0x10, 0xfe),
658 * 0x04, Vintcore12Ena
659 * 0x38, Vintcore12Sel
663 REG_INIT(AB8500_REGUMISC1, 0x03, 0x80, 0xfe),
670 REG_INIT(AB8500_VAUDIOSUPPLY, 0x03, 0x83, 0x1e),
675 REG_INIT(AB8500_REGUCTRL1VAMIC, 0x03, 0x84, 0x03),
680 REG_INIT(AB8500_VPLLVANAREGU, 0x04, 0x06, 0x0f),
683 * 0x02, VrefDDRSleepMode
685 REG_INIT(AB8500_VREFDDR, 0x04, 0x07, 0x03),
687 * 0x03, VextSupply1Regu
688 * 0x0c, VextSupply2Regu
689 * 0x30, VextSupply3Regu
690 * 0x40, ExtSupply2Bypass
691 * 0x80, ExtSupply3Bypass
693 REG_INIT(AB8500_EXTSUPPLYREGU, 0x04, 0x08, 0xff),
698 REG_INIT(AB8500_VAUX12REGU, 0x04, 0x09, 0x0f),
702 REG_INIT(AB8500_VRF1VAUX3REGU, 0x04, 0x0a, 0x03),
706 REG_INIT(AB8500_VSMPS1SEL1, 0x04, 0x13, 0x3f),
710 REG_INIT(AB8500_VAUX1SEL, 0x04, 0x1f, 0x0f),
714 REG_INIT(AB8500_VAUX2SEL, 0x04, 0x20, 0x0f),
718 REG_INIT(AB8500_VRF1VAUX3SEL, 0x04, 0x21, 0x07),
720 * 0x01, VextSupply12LP
722 REG_INIT(AB8500_REGUCTRL2SPARE, 0x04, 0x22, 0x01),
727 * 0x20, Vintcore12Disch
731 REG_INIT(AB8500_REGUCTRLDISCH, 0x04, 0x43, 0xfc),
734 * 0x04, VdmicPullDownEna
737 REG_INIT(AB8500_REGUCTRLDISCH2, 0x04, 0x44, 0x16),
740 static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
742 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
743 struct ab8500_platform_data *pdata;
747 dev_err(&pdev->dev, "null mfd parent\n");
750 pdata = dev_get_platdata(ab8500->dev);
752 dev_err(&pdev->dev, "null pdata\n");
756 /* make sure the platform data has the correct size */
757 if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
758 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
762 /* initialize registers */
763 for (i = 0; i < pdata->num_regulator_reg_init; i++) {
767 id = pdata->regulator_reg_init[i].id;
768 value = pdata->regulator_reg_init[i].value;
770 /* check for configuration errors */
771 if (id >= AB8500_NUM_REGULATOR_REGISTERS) {
773 "Configuration error: id outside range.\n");
776 if (value & ~ab8500_reg_init[id].mask) {
778 "Configuration error: value outside mask.\n");
782 /* initialize register */
783 err = abx500_mask_and_set_register_interruptible(&pdev->dev,
784 ab8500_reg_init[id].bank,
785 ab8500_reg_init[id].addr,
786 ab8500_reg_init[id].mask,
790 "Failed to initialize 0x%02x, 0x%02x.\n",
791 ab8500_reg_init[id].bank,
792 ab8500_reg_init[id].addr);
796 " init: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
797 ab8500_reg_init[id].bank,
798 ab8500_reg_init[id].addr,
799 ab8500_reg_init[id].mask,
803 /* register all regulators */
804 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
805 struct ab8500_regulator_info *info = NULL;
807 /* assign per-regulator data */
808 info = &ab8500_regulator_info[i];
809 info->dev = &pdev->dev;
811 /* fix for hardware before ab8500v2.0 */
812 if (abx500_get_chip_id(info->dev) < 0x20) {
813 if (info->desc.id == AB8500_LDO_AUX3) {
814 info->desc.n_voltages =
815 ARRAY_SIZE(ldo_vauxn_voltages);
816 info->voltages = ldo_vauxn_voltages;
818 ARRAY_SIZE(ldo_vauxn_voltages);
819 info->voltage_mask = 0xf;
823 /* register regulator with framework */
824 info->regulator = regulator_register(&info->desc, &pdev->dev,
825 &pdata->regulator[i], info);
826 if (IS_ERR(info->regulator)) {
827 err = PTR_ERR(info->regulator);
828 dev_err(&pdev->dev, "failed to register regulator %s\n",
830 /* when we fail, un-register all earlier regulators */
832 info = &ab8500_regulator_info[i];
833 regulator_unregister(info->regulator);
838 dev_vdbg(rdev_get_dev(info->regulator),
839 "%s-probed\n", info->desc.name);
845 static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
849 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
850 struct ab8500_regulator_info *info = NULL;
851 info = &ab8500_regulator_info[i];
853 dev_vdbg(rdev_get_dev(info->regulator),
854 "%s-remove\n", info->desc.name);
856 regulator_unregister(info->regulator);
862 static struct platform_driver ab8500_regulator_driver = {
863 .probe = ab8500_regulator_probe,
864 .remove = __devexit_p(ab8500_regulator_remove),
866 .name = "ab8500-regulator",
867 .owner = THIS_MODULE,
871 static int __init ab8500_regulator_init(void)
875 ret = platform_driver_register(&ab8500_regulator_driver);
877 pr_err("Failed to register ab8500 regulator: %d\n", ret);
881 subsys_initcall(ab8500_regulator_init);
883 static void __exit ab8500_regulator_exit(void)
885 platform_driver_unregister(&ab8500_regulator_driver);
887 module_exit(ab8500_regulator_exit);
889 MODULE_LICENSE("GPL v2");
890 MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
891 MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
892 MODULE_ALIAS("platform:ab8500-regulator");