diff options
Diffstat (limited to 'drivers/regulator/mc13783-regulator.c')
| -rw-r--r-- | drivers/regulator/mc13783-regulator.c | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/drivers/regulator/mc13783-regulator.c b/drivers/regulator/mc13783-regulator.c new file mode 100644 index 000000000000..39c495300045 --- /dev/null +++ b/drivers/regulator/mc13783-regulator.c | |||
| @@ -0,0 +1,245 @@ | |||
| 1 | /* | ||
| 2 | * Regulator Driver for Freescale MC13783 PMIC | ||
| 3 | * | ||
| 4 | * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License version 2 as | ||
| 8 | * published by the Free Software Foundation. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <linux/mfd/mc13783.h> | ||
| 12 | #include <linux/regulator/machine.h> | ||
| 13 | #include <linux/regulator/driver.h> | ||
| 14 | #include <linux/platform_device.h> | ||
| 15 | #include <linux/kernel.h> | ||
| 16 | #include <linux/init.h> | ||
| 17 | #include <linux/err.h> | ||
| 18 | |||
| 19 | #define MC13783_REG_SWITCHERS4 28 | ||
| 20 | #define MC13783_REG_SWITCHERS4_PLLEN (1 << 18) | ||
| 21 | |||
| 22 | #define MC13783_REG_SWITCHERS5 29 | ||
| 23 | #define MC13783_REG_SWITCHERS5_SW3EN (1 << 20) | ||
| 24 | |||
| 25 | #define MC13783_REG_REGULATORMODE0 32 | ||
| 26 | #define MC13783_REG_REGULATORMODE0_VAUDIOEN (1 << 0) | ||
| 27 | #define MC13783_REG_REGULATORMODE0_VIOHIEN (1 << 3) | ||
| 28 | #define MC13783_REG_REGULATORMODE0_VIOLOEN (1 << 6) | ||
| 29 | #define MC13783_REG_REGULATORMODE0_VDIGEN (1 << 9) | ||
| 30 | #define MC13783_REG_REGULATORMODE0_VGENEN (1 << 12) | ||
| 31 | #define MC13783_REG_REGULATORMODE0_VRFDIGEN (1 << 15) | ||
| 32 | #define MC13783_REG_REGULATORMODE0_VRFREFEN (1 << 18) | ||
| 33 | #define MC13783_REG_REGULATORMODE0_VRFCPEN (1 << 21) | ||
| 34 | |||
| 35 | #define MC13783_REG_REGULATORMODE1 33 | ||
| 36 | #define MC13783_REG_REGULATORMODE1_VSIMEN (1 << 0) | ||
| 37 | #define MC13783_REG_REGULATORMODE1_VESIMEN (1 << 3) | ||
| 38 | #define MC13783_REG_REGULATORMODE1_VCAMEN (1 << 6) | ||
| 39 | #define MC13783_REG_REGULATORMODE1_VRFBGEN (1 << 9) | ||
| 40 | #define MC13783_REG_REGULATORMODE1_VVIBEN (1 << 11) | ||
| 41 | #define MC13783_REG_REGULATORMODE1_VRF1EN (1 << 12) | ||
| 42 | #define MC13783_REG_REGULATORMODE1_VRF2EN (1 << 15) | ||
| 43 | #define MC13783_REG_REGULATORMODE1_VMMC1EN (1 << 18) | ||
| 44 | #define MC13783_REG_REGULATORMODE1_VMMC2EN (1 << 21) | ||
| 45 | |||
| 46 | #define MC13783_REG_POWERMISC 34 | ||
| 47 | #define MC13783_REG_POWERMISC_GPO1EN (1 << 6) | ||
| 48 | #define MC13783_REG_POWERMISC_GPO2EN (1 << 8) | ||
| 49 | #define MC13783_REG_POWERMISC_GPO3EN (1 << 10) | ||
| 50 | #define MC13783_REG_POWERMISC_GPO4EN (1 << 12) | ||
| 51 | |||
| 52 | struct mc13783_regulator { | ||
| 53 | struct regulator_desc desc; | ||
| 54 | int reg; | ||
| 55 | int enable_bit; | ||
| 56 | }; | ||
| 57 | |||
| 58 | static struct regulator_ops mc13783_regulator_ops; | ||
| 59 | |||
| 60 | #define MC13783_DEFINE(prefix, _name, _reg) \ | ||
| 61 | [MC13783_ ## prefix ## _ ## _name] = { \ | ||
| 62 | .desc = { \ | ||
| 63 | .name = #prefix "_" #_name, \ | ||
| 64 | .ops = &mc13783_regulator_ops, \ | ||
| 65 | .type = REGULATOR_VOLTAGE, \ | ||
| 66 | .id = MC13783_ ## prefix ## _ ## _name, \ | ||
| 67 | .owner = THIS_MODULE, \ | ||
| 68 | }, \ | ||
| 69 | .reg = MC13783_REG_ ## _reg, \ | ||
| 70 | .enable_bit = MC13783_REG_ ## _reg ## _ ## _name ## EN, \ | ||
| 71 | } | ||
| 72 | |||
| 73 | #define MC13783_DEFINE_SW(_name, _reg) MC13783_DEFINE(SW, _name, _reg) | ||
| 74 | #define MC13783_DEFINE_REGU(_name, _reg) MC13783_DEFINE(REGU, _name, _reg) | ||
| 75 | |||
| 76 | static struct mc13783_regulator mc13783_regulators[] = { | ||
| 77 | MC13783_DEFINE_SW(SW3, SWITCHERS5), | ||
| 78 | MC13783_DEFINE_SW(PLL, SWITCHERS4), | ||
| 79 | |||
| 80 | MC13783_DEFINE_REGU(VAUDIO, REGULATORMODE0), | ||
| 81 | MC13783_DEFINE_REGU(VIOHI, REGULATORMODE0), | ||
| 82 | MC13783_DEFINE_REGU(VIOLO, REGULATORMODE0), | ||
| 83 | MC13783_DEFINE_REGU(VDIG, REGULATORMODE0), | ||
| 84 | MC13783_DEFINE_REGU(VGEN, REGULATORMODE0), | ||
| 85 | MC13783_DEFINE_REGU(VRFDIG, REGULATORMODE0), | ||
| 86 | MC13783_DEFINE_REGU(VRFREF, REGULATORMODE0), | ||
| 87 | MC13783_DEFINE_REGU(VRFCP, REGULATORMODE0), | ||
| 88 | MC13783_DEFINE_REGU(VSIM, REGULATORMODE1), | ||
| 89 | MC13783_DEFINE_REGU(VESIM, REGULATORMODE1), | ||
| 90 | MC13783_DEFINE_REGU(VCAM, REGULATORMODE1), | ||
| 91 | MC13783_DEFINE_REGU(VRFBG, REGULATORMODE1), | ||
| 92 | MC13783_DEFINE_REGU(VVIB, REGULATORMODE1), | ||
| 93 | MC13783_DEFINE_REGU(VRF1, REGULATORMODE1), | ||
| 94 | MC13783_DEFINE_REGU(VRF2, REGULATORMODE1), | ||
| 95 | MC13783_DEFINE_REGU(VMMC1, REGULATORMODE1), | ||
| 96 | MC13783_DEFINE_REGU(VMMC2, REGULATORMODE1), | ||
| 97 | MC13783_DEFINE_REGU(GPO1, POWERMISC), | ||
| 98 | MC13783_DEFINE_REGU(GPO2, POWERMISC), | ||
| 99 | MC13783_DEFINE_REGU(GPO3, POWERMISC), | ||
| 100 | MC13783_DEFINE_REGU(GPO4, POWERMISC), | ||
| 101 | }; | ||
| 102 | |||
| 103 | struct mc13783_regulator_priv { | ||
| 104 | struct mc13783 *mc13783; | ||
| 105 | struct regulator_dev *regulators[]; | ||
| 106 | }; | ||
| 107 | |||
| 108 | static int mc13783_regulator_enable(struct regulator_dev *rdev) | ||
| 109 | { | ||
| 110 | struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); | ||
| 111 | int id = rdev_get_id(rdev); | ||
| 112 | int ret; | ||
| 113 | |||
| 114 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); | ||
| 115 | |||
| 116 | mc13783_lock(priv->mc13783); | ||
| 117 | ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg, | ||
| 118 | mc13783_regulators[id].enable_bit, | ||
| 119 | mc13783_regulators[id].enable_bit); | ||
| 120 | mc13783_unlock(priv->mc13783); | ||
| 121 | |||
| 122 | return ret; | ||
| 123 | } | ||
| 124 | |||
| 125 | static int mc13783_regulator_disable(struct regulator_dev *rdev) | ||
| 126 | { | ||
| 127 | struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); | ||
| 128 | int id = rdev_get_id(rdev); | ||
| 129 | int ret; | ||
| 130 | |||
| 131 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); | ||
| 132 | |||
| 133 | mc13783_lock(priv->mc13783); | ||
| 134 | ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg, | ||
| 135 | mc13783_regulators[id].enable_bit, 0); | ||
| 136 | mc13783_unlock(priv->mc13783); | ||
| 137 | |||
| 138 | return ret; | ||
| 139 | } | ||
| 140 | |||
| 141 | static int mc13783_regulator_is_enabled(struct regulator_dev *rdev) | ||
| 142 | { | ||
| 143 | struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); | ||
| 144 | int ret, id = rdev_get_id(rdev); | ||
| 145 | unsigned int val; | ||
| 146 | |||
| 147 | mc13783_lock(priv->mc13783); | ||
| 148 | ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val); | ||
| 149 | mc13783_unlock(priv->mc13783); | ||
| 150 | |||
| 151 | if (ret) | ||
| 152 | return ret; | ||
| 153 | |||
| 154 | return (val & mc13783_regulators[id].enable_bit) != 0; | ||
| 155 | } | ||
| 156 | |||
| 157 | static struct regulator_ops mc13783_regulator_ops = { | ||
| 158 | .enable = mc13783_regulator_enable, | ||
| 159 | .disable = mc13783_regulator_disable, | ||
| 160 | .is_enabled = mc13783_regulator_is_enabled, | ||
| 161 | }; | ||
| 162 | |||
| 163 | static int __devinit mc13783_regulator_probe(struct platform_device *pdev) | ||
| 164 | { | ||
| 165 | struct mc13783_regulator_priv *priv; | ||
| 166 | struct mc13783 *mc13783 = dev_get_drvdata(pdev->dev.parent); | ||
| 167 | struct mc13783_regulator_platform_data *pdata = | ||
| 168 | dev_get_platdata(&pdev->dev); | ||
| 169 | struct mc13783_regulator_init_data *init_data; | ||
| 170 | int i, ret; | ||
| 171 | |||
| 172 | dev_dbg(&pdev->dev, "mc13783_regulator_probe id %d\n", pdev->id); | ||
| 173 | |||
| 174 | priv = kzalloc(sizeof(*priv) + | ||
| 175 | pdata->num_regulators * sizeof(priv->regulators[0]), | ||
| 176 | GFP_KERNEL); | ||
| 177 | if (!priv) | ||
| 178 | return -ENOMEM; | ||
| 179 | |||
| 180 | priv->mc13783 = mc13783; | ||
| 181 | |||
| 182 | for (i = 0; i < pdata->num_regulators; i++) { | ||
| 183 | init_data = &pdata->regulators[i]; | ||
| 184 | priv->regulators[i] = regulator_register( | ||
| 185 | &mc13783_regulators[init_data->id].desc, | ||
| 186 | &pdev->dev, init_data->init_data, priv); | ||
| 187 | |||
| 188 | if (IS_ERR(priv->regulators[i])) { | ||
| 189 | dev_err(&pdev->dev, "failed to register regulator %s\n", | ||
| 190 | mc13783_regulators[i].desc.name); | ||
| 191 | ret = PTR_ERR(priv->regulators[i]); | ||
| 192 | goto err; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | platform_set_drvdata(pdev, priv); | ||
| 197 | |||
| 198 | return 0; | ||
| 199 | err: | ||
| 200 | while (--i >= 0) | ||
| 201 | regulator_unregister(priv->regulators[i]); | ||
| 202 | |||
| 203 | kfree(priv); | ||
| 204 | |||
| 205 | return ret; | ||
| 206 | } | ||
| 207 | |||
| 208 | static int __devexit mc13783_regulator_remove(struct platform_device *pdev) | ||
| 209 | { | ||
| 210 | struct mc13783_regulator_priv *priv = platform_get_drvdata(pdev); | ||
| 211 | struct mc13783_regulator_platform_data *pdata = | ||
| 212 | dev_get_platdata(&pdev->dev); | ||
| 213 | int i; | ||
| 214 | |||
| 215 | for (i = 0; i < pdata->num_regulators; i++) | ||
| 216 | regulator_unregister(priv->regulators[i]); | ||
| 217 | |||
| 218 | return 0; | ||
| 219 | } | ||
| 220 | |||
| 221 | static struct platform_driver mc13783_regulator_driver = { | ||
| 222 | .driver = { | ||
| 223 | .name = "mc13783-regulator", | ||
| 224 | .owner = THIS_MODULE, | ||
| 225 | }, | ||
| 226 | .remove = __devexit_p(mc13783_regulator_remove), | ||
| 227 | .probe = mc13783_regulator_probe, | ||
| 228 | }; | ||
| 229 | |||
| 230 | static int __init mc13783_regulator_init(void) | ||
| 231 | { | ||
| 232 | return platform_driver_register(&mc13783_regulator_driver); | ||
| 233 | } | ||
| 234 | subsys_initcall(mc13783_regulator_init); | ||
| 235 | |||
| 236 | static void __exit mc13783_regulator_exit(void) | ||
| 237 | { | ||
| 238 | platform_driver_unregister(&mc13783_regulator_driver); | ||
| 239 | } | ||
| 240 | module_exit(mc13783_regulator_exit); | ||
| 241 | |||
| 242 | MODULE_LICENSE("GPL v2"); | ||
| 243 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de"); | ||
| 244 | MODULE_DESCRIPTION("Regulator Driver for Freescale MC13783 PMIC"); | ||
| 245 | MODULE_ALIAS("platform:mc13783-regulator"); | ||
