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/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
41 * @delay: startup/set voltage delay in us
43 struct ab8500_regulator_info {
45 struct regulator_desc desc;
46 struct regulator_dev *regulator;
62 /* voltage tables for the vauxn/vintcore supplies */
63 static const int ldo_vauxn_voltages[] = {
82 static const int ldo_vaux3_voltages[] = {
93 static const int ldo_vintcore_voltages[] = {
103 static int ab8500_regulator_enable(struct regulator_dev *rdev)
106 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
109 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
113 ret = abx500_mask_and_set_register_interruptible(info->dev,
114 info->update_bank, info->update_reg,
115 info->update_mask, info->update_val_enable);
117 dev_err(rdev_get_dev(rdev),
118 "couldn't set enable bits for regulator\n");
120 dev_vdbg(rdev_get_dev(rdev),
121 "%s-enable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
122 info->desc.name, info->update_bank, info->update_reg,
123 info->update_mask, info->update_val_enable);
128 static int ab8500_regulator_disable(struct regulator_dev *rdev)
131 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
134 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
138 ret = abx500_mask_and_set_register_interruptible(info->dev,
139 info->update_bank, info->update_reg,
140 info->update_mask, 0x0);
142 dev_err(rdev_get_dev(rdev),
143 "couldn't set disable bits for regulator\n");
145 dev_vdbg(rdev_get_dev(rdev),
146 "%s-disable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
147 info->desc.name, info->update_bank, info->update_reg,
148 info->update_mask, 0x0);
153 static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
156 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
160 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
164 ret = abx500_get_register_interruptible(info->dev,
165 info->update_bank, info->update_reg, ®val);
167 dev_err(rdev_get_dev(rdev),
168 "couldn't read 0x%x register\n", info->update_reg);
172 dev_vdbg(rdev_get_dev(rdev),
173 "%s-is_enabled (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
175 info->desc.name, info->update_bank, info->update_reg,
176 info->update_mask, regval);
178 if (regval & info->update_mask)
184 static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
186 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
189 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
193 /* return the uV for the fixed regulators */
195 return info->fixed_uV;
197 if (selector >= info->voltages_len)
200 return info->voltages[selector];
203 static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
206 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
210 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
214 ret = abx500_get_register_interruptible(info->dev,
215 info->voltage_bank, info->voltage_reg, ®val);
217 dev_err(rdev_get_dev(rdev),
218 "couldn't read voltage reg for regulator\n");
222 dev_vdbg(rdev_get_dev(rdev),
223 "%s-get_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
225 info->desc.name, info->voltage_bank, info->voltage_reg,
226 info->voltage_mask, regval);
228 /* vintcore has a different layout */
229 val = regval & info->voltage_mask;
230 if (info->desc.id == AB8500_LDO_INTCORE)
231 ret = info->voltages[val >> 0x3];
233 ret = info->voltages[val];
238 static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
239 int min_uV, int max_uV)
241 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
244 /* check the supported voltage */
245 for (i = 0; i < info->voltages_len; i++) {
246 if ((info->voltages[i] >= min_uV) &&
247 (info->voltages[i] <= max_uV))
254 static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
255 int min_uV, int max_uV,
259 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
263 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
267 /* get the appropriate voltages within the range */
268 ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
270 dev_err(rdev_get_dev(rdev),
271 "couldn't get best voltage for regulator\n");
277 /* set the registers for the request */
279 ret = abx500_mask_and_set_register_interruptible(info->dev,
280 info->voltage_bank, info->voltage_reg,
281 info->voltage_mask, regval);
283 dev_err(rdev_get_dev(rdev),
284 "couldn't set voltage reg for regulator\n");
286 dev_vdbg(rdev_get_dev(rdev),
287 "%s-set_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
289 info->desc.name, info->voltage_bank, info->voltage_reg,
290 info->voltage_mask, regval);
295 static int ab8500_regulator_enable_time(struct regulator_dev *rdev)
297 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
302 static int ab8500_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
303 unsigned int old_sel,
304 unsigned int new_sel)
306 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
309 /* If the regulator isn't on, it won't take time here */
310 ret = ab8500_regulator_is_enabled(rdev);
318 static struct regulator_ops ab8500_regulator_ops = {
319 .enable = ab8500_regulator_enable,
320 .disable = ab8500_regulator_disable,
321 .is_enabled = ab8500_regulator_is_enabled,
322 .get_voltage = ab8500_regulator_get_voltage,
323 .set_voltage = ab8500_regulator_set_voltage,
324 .list_voltage = ab8500_list_voltage,
325 .enable_time = ab8500_regulator_enable_time,
326 .set_voltage_time_sel = ab8500_regulator_set_voltage_time_sel,
329 static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
331 struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
334 dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
338 return info->fixed_uV;
341 static struct regulator_ops ab8500_regulator_fixed_ops = {
342 .enable = ab8500_regulator_enable,
343 .disable = ab8500_regulator_disable,
344 .is_enabled = ab8500_regulator_is_enabled,
345 .get_voltage = ab8500_fixed_get_voltage,
346 .list_voltage = ab8500_list_voltage,
347 .enable_time = ab8500_regulator_enable_time,
348 .set_voltage_time_sel = ab8500_regulator_set_voltage_time_sel,
351 static struct ab8500_regulator_info
352 ab8500_regulator_info[AB8500_NUM_REGULATORS] = {
354 * Variable Voltage Regulators
355 * name, min mV, max mV,
356 * update bank, reg, mask, enable val
357 * volt bank, reg, mask, table, table length
359 [AB8500_LDO_AUX1] = {
362 .ops = &ab8500_regulator_ops,
363 .type = REGULATOR_VOLTAGE,
364 .id = AB8500_LDO_AUX1,
365 .owner = THIS_MODULE,
366 .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
373 .update_val_enable = 0x01,
374 .voltage_bank = 0x04,
376 .voltage_mask = 0x0f,
377 .voltages = ldo_vauxn_voltages,
378 .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
380 [AB8500_LDO_AUX2] = {
383 .ops = &ab8500_regulator_ops,
384 .type = REGULATOR_VOLTAGE,
385 .id = AB8500_LDO_AUX2,
386 .owner = THIS_MODULE,
387 .n_voltages = ARRAY_SIZE(ldo_vauxn_voltages),
394 .update_val_enable = 0x04,
395 .voltage_bank = 0x04,
397 .voltage_mask = 0x0f,
398 .voltages = ldo_vauxn_voltages,
399 .voltages_len = ARRAY_SIZE(ldo_vauxn_voltages),
401 [AB8500_LDO_AUX3] = {
404 .ops = &ab8500_regulator_ops,
405 .type = REGULATOR_VOLTAGE,
406 .id = AB8500_LDO_AUX3,
407 .owner = THIS_MODULE,
408 .n_voltages = ARRAY_SIZE(ldo_vaux3_voltages),
415 .update_val_enable = 0x01,
416 .voltage_bank = 0x04,
418 .voltage_mask = 0x07,
419 .voltages = ldo_vaux3_voltages,
420 .voltages_len = ARRAY_SIZE(ldo_vaux3_voltages),
422 [AB8500_LDO_INTCORE] = {
424 .name = "LDO-INTCORE",
425 .ops = &ab8500_regulator_ops,
426 .type = REGULATOR_VOLTAGE,
427 .id = AB8500_LDO_INTCORE,
428 .owner = THIS_MODULE,
429 .n_voltages = ARRAY_SIZE(ldo_vintcore_voltages),
436 .update_val_enable = 0x04,
437 .voltage_bank = 0x03,
439 .voltage_mask = 0x38,
440 .voltages = ldo_vintcore_voltages,
441 .voltages_len = ARRAY_SIZE(ldo_vintcore_voltages),
445 * Fixed Voltage Regulators
447 * update bank, reg, mask, enable val
449 [AB8500_LDO_TVOUT] = {
452 .ops = &ab8500_regulator_fixed_ops,
453 .type = REGULATOR_VOLTAGE,
454 .id = AB8500_LDO_TVOUT,
455 .owner = THIS_MODULE,
463 .update_val_enable = 0x02,
468 .ops = &ab8500_regulator_fixed_ops,
469 .type = REGULATOR_VOLTAGE,
470 .id = AB8500_LDO_USB,
471 .owner = THIS_MODULE,
478 .update_val_enable = 0x01,
480 [AB8500_LDO_AUDIO] = {
483 .ops = &ab8500_regulator_fixed_ops,
484 .type = REGULATOR_VOLTAGE,
485 .id = AB8500_LDO_AUDIO,
486 .owner = THIS_MODULE,
493 .update_val_enable = 0x02,
495 [AB8500_LDO_ANAMIC1] = {
497 .name = "LDO-ANAMIC1",
498 .ops = &ab8500_regulator_fixed_ops,
499 .type = REGULATOR_VOLTAGE,
500 .id = AB8500_LDO_ANAMIC1,
501 .owner = THIS_MODULE,
508 .update_val_enable = 0x08,
510 [AB8500_LDO_ANAMIC2] = {
512 .name = "LDO-ANAMIC2",
513 .ops = &ab8500_regulator_fixed_ops,
514 .type = REGULATOR_VOLTAGE,
515 .id = AB8500_LDO_ANAMIC2,
516 .owner = THIS_MODULE,
523 .update_val_enable = 0x10,
525 [AB8500_LDO_DMIC] = {
528 .ops = &ab8500_regulator_fixed_ops,
529 .type = REGULATOR_VOLTAGE,
530 .id = AB8500_LDO_DMIC,
531 .owner = THIS_MODULE,
538 .update_val_enable = 0x04,
543 .ops = &ab8500_regulator_fixed_ops,
544 .type = REGULATOR_VOLTAGE,
545 .id = AB8500_LDO_ANA,
546 .owner = THIS_MODULE,
553 .update_val_enable = 0x04,
559 struct ab8500_reg_init {
565 #define REG_INIT(_id, _bank, _addr, _mask) \
572 static struct ab8500_reg_init ab8500_reg_init[] = {
574 * 0x30, VanaRequestCtrl
575 * 0x0C, VpllRequestCtrl
576 * 0xc0, VextSupply1RequestCtrl
578 REG_INIT(AB8500_REGUREQUESTCTRL2, 0x03, 0x04, 0xfc),
580 * 0x03, VextSupply2RequestCtrl
581 * 0x0c, VextSupply3RequestCtrl
582 * 0x30, Vaux1RequestCtrl
583 * 0xc0, Vaux2RequestCtrl
585 REG_INIT(AB8500_REGUREQUESTCTRL3, 0x03, 0x05, 0xff),
587 * 0x03, Vaux3RequestCtrl
590 REG_INIT(AB8500_REGUREQUESTCTRL4, 0x03, 0x06, 0x07),
592 * 0x08, VanaSysClkReq1HPValid
593 * 0x20, Vaux1SysClkReq1HPValid
594 * 0x40, Vaux2SysClkReq1HPValid
595 * 0x80, Vaux3SysClkReq1HPValid
597 REG_INIT(AB8500_REGUSYSCLKREQ1HPVALID1, 0x03, 0x07, 0xe8),
599 * 0x10, VextSupply1SysClkReq1HPValid
600 * 0x20, VextSupply2SysClkReq1HPValid
601 * 0x40, VextSupply3SysClkReq1HPValid
603 REG_INIT(AB8500_REGUSYSCLKREQ1HPVALID2, 0x03, 0x08, 0x70),
605 * 0x08, VanaHwHPReq1Valid
606 * 0x20, Vaux1HwHPReq1Valid
607 * 0x40, Vaux2HwHPReq1Valid
608 * 0x80, Vaux3HwHPReq1Valid
610 REG_INIT(AB8500_REGUHWHPREQ1VALID1, 0x03, 0x09, 0xe8),
612 * 0x01, VextSupply1HwHPReq1Valid
613 * 0x02, VextSupply2HwHPReq1Valid
614 * 0x04, VextSupply3HwHPReq1Valid
616 REG_INIT(AB8500_REGUHWHPREQ1VALID2, 0x03, 0x0a, 0x07),
618 * 0x08, VanaHwHPReq2Valid
619 * 0x20, Vaux1HwHPReq2Valid
620 * 0x40, Vaux2HwHPReq2Valid
621 * 0x80, Vaux3HwHPReq2Valid
623 REG_INIT(AB8500_REGUHWHPREQ2VALID1, 0x03, 0x0b, 0xe8),
625 * 0x01, VextSupply1HwHPReq2Valid
626 * 0x02, VextSupply2HwHPReq2Valid
627 * 0x04, VextSupply3HwHPReq2Valid
629 REG_INIT(AB8500_REGUHWHPREQ2VALID2, 0x03, 0x0c, 0x07),
631 * 0x20, VanaSwHPReqValid
632 * 0x80, Vaux1SwHPReqValid
634 REG_INIT(AB8500_REGUSWHPREQVALID1, 0x03, 0x0d, 0xa0),
636 * 0x01, Vaux2SwHPReqValid
637 * 0x02, Vaux3SwHPReqValid
638 * 0x04, VextSupply1SwHPReqValid
639 * 0x08, VextSupply2SwHPReqValid
640 * 0x10, VextSupply3SwHPReqValid
642 REG_INIT(AB8500_REGUSWHPREQVALID2, 0x03, 0x0e, 0x1f),
644 * 0x02, SysClkReq2Valid1
646 * 0x80, SysClkReq8Valid1
648 REG_INIT(AB8500_REGUSYSCLKREQVALID1, 0x03, 0x0f, 0xfe),
650 * 0x02, SysClkReq2Valid2
652 * 0x80, SysClkReq8Valid2
654 REG_INIT(AB8500_REGUSYSCLKREQVALID2, 0x03, 0x10, 0xfe),
657 * 0x04, Vintcore12Ena
658 * 0x38, Vintcore12Sel
662 REG_INIT(AB8500_REGUMISC1, 0x03, 0x80, 0xfe),
669 REG_INIT(AB8500_VAUDIOSUPPLY, 0x03, 0x83, 0x1e),
674 REG_INIT(AB8500_REGUCTRL1VAMIC, 0x03, 0x84, 0x03),
679 REG_INIT(AB8500_VPLLVANAREGU, 0x04, 0x06, 0x0f),
682 * 0x02, VrefDDRSleepMode
684 REG_INIT(AB8500_VREFDDR, 0x04, 0x07, 0x03),
686 * 0x03, VextSupply1Regu
687 * 0x0c, VextSupply2Regu
688 * 0x30, VextSupply3Regu
689 * 0x40, ExtSupply2Bypass
690 * 0x80, ExtSupply3Bypass
692 REG_INIT(AB8500_EXTSUPPLYREGU, 0x04, 0x08, 0xff),
697 REG_INIT(AB8500_VAUX12REGU, 0x04, 0x09, 0x0f),
701 REG_INIT(AB8500_VRF1VAUX3REGU, 0x04, 0x0a, 0x03),
705 REG_INIT(AB8500_VSMPS1SEL1, 0x04, 0x13, 0x3f),
709 REG_INIT(AB8500_VAUX1SEL, 0x04, 0x1f, 0x0f),
713 REG_INIT(AB8500_VAUX2SEL, 0x04, 0x20, 0x0f),
717 REG_INIT(AB8500_VRF1VAUX3SEL, 0x04, 0x21, 0x07),
719 * 0x01, VextSupply12LP
721 REG_INIT(AB8500_REGUCTRL2SPARE, 0x04, 0x22, 0x01),
726 * 0x20, Vintcore12Disch
730 REG_INIT(AB8500_REGUCTRLDISCH, 0x04, 0x43, 0xfc),
733 * 0x04, VdmicPullDownEna
736 REG_INIT(AB8500_REGUCTRLDISCH2, 0x04, 0x44, 0x16),
739 static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
741 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
742 struct ab8500_platform_data *pdata;
746 dev_err(&pdev->dev, "null mfd parent\n");
749 pdata = dev_get_platdata(ab8500->dev);
751 dev_err(&pdev->dev, "null pdata\n");
755 /* make sure the platform data has the correct size */
756 if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
757 dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
761 /* initialize registers */
762 for (i = 0; i < pdata->num_regulator_reg_init; i++) {
766 id = pdata->regulator_reg_init[i].id;
767 value = pdata->regulator_reg_init[i].value;
769 /* check for configuration errors */
770 if (id >= AB8500_NUM_REGULATOR_REGISTERS) {
772 "Configuration error: id outside range.\n");
775 if (value & ~ab8500_reg_init[id].mask) {
777 "Configuration error: value outside mask.\n");
781 /* initialize register */
782 err = abx500_mask_and_set_register_interruptible(&pdev->dev,
783 ab8500_reg_init[id].bank,
784 ab8500_reg_init[id].addr,
785 ab8500_reg_init[id].mask,
789 "Failed to initialize 0x%02x, 0x%02x.\n",
790 ab8500_reg_init[id].bank,
791 ab8500_reg_init[id].addr);
795 " init: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
796 ab8500_reg_init[id].bank,
797 ab8500_reg_init[id].addr,
798 ab8500_reg_init[id].mask,
802 /* register all regulators */
803 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
804 struct ab8500_regulator_info *info = NULL;
806 /* assign per-regulator data */
807 info = &ab8500_regulator_info[i];
808 info->dev = &pdev->dev;
810 /* fix for hardware before ab8500v2.0 */
811 if (abx500_get_chip_id(info->dev) < 0x20) {
812 if (info->desc.id == AB8500_LDO_AUX3) {
813 info->desc.n_voltages =
814 ARRAY_SIZE(ldo_vauxn_voltages);
815 info->voltages = ldo_vauxn_voltages;
817 ARRAY_SIZE(ldo_vauxn_voltages);
818 info->voltage_mask = 0xf;
822 /* register regulator with framework */
823 info->regulator = regulator_register(&info->desc, &pdev->dev,
824 &pdata->regulator[i], info);
825 if (IS_ERR(info->regulator)) {
826 err = PTR_ERR(info->regulator);
827 dev_err(&pdev->dev, "failed to register regulator %s\n",
829 /* when we fail, un-register all earlier regulators */
831 info = &ab8500_regulator_info[i];
832 regulator_unregister(info->regulator);
837 dev_vdbg(rdev_get_dev(info->regulator),
838 "%s-probed\n", info->desc.name);
844 static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
848 for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
849 struct ab8500_regulator_info *info = NULL;
850 info = &ab8500_regulator_info[i];
852 dev_vdbg(rdev_get_dev(info->regulator),
853 "%s-remove\n", info->desc.name);
855 regulator_unregister(info->regulator);
861 static struct platform_driver ab8500_regulator_driver = {
862 .probe = ab8500_regulator_probe,
863 .remove = __devexit_p(ab8500_regulator_remove),
865 .name = "ab8500-regulator",
866 .owner = THIS_MODULE,
870 static int __init ab8500_regulator_init(void)
874 ret = platform_driver_register(&ab8500_regulator_driver);
876 pr_err("Failed to register ab8500 regulator: %d\n", ret);
880 subsys_initcall(ab8500_regulator_init);
882 static void __exit ab8500_regulator_exit(void)
884 platform_driver_unregister(&ab8500_regulator_driver);
886 module_exit(ab8500_regulator_exit);
888 MODULE_LICENSE("GPL v2");
889 MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
890 MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
891 MODULE_ALIAS("platform:ab8500-regulator");