Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 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 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 11 | #include <linux/mfd/mc13783.h> |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 12 | #include <linux/regulator/machine.h> |
| 13 | #include <linux/regulator/driver.h> |
| 14 | #include <linux/platform_device.h> |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/err.h> |
| 18 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 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 | |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 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 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 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 | |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 76 | static struct mc13783_regulator mc13783_regulators[] = { |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 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), |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 103 | struct mc13783_regulator_priv { |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 104 | struct mc13783 *mc13783; |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 105 | struct regulator_dev *regulators[]; |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 108 | static int mc13783_regulator_enable(struct regulator_dev *rdev) |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 109 | { |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 110 | struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 111 | int id = rdev_get_id(rdev); |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 112 | int ret; |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 113 | |
| 114 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); |
| 115 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 116 | mc13783_lock(priv->mc13783); |
| 117 | ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg, |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 118 | mc13783_regulators[id].enable_bit, |
| 119 | mc13783_regulators[id].enable_bit); |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 120 | mc13783_unlock(priv->mc13783); |
| 121 | |
| 122 | return ret; |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 125 | static int mc13783_regulator_disable(struct regulator_dev *rdev) |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 126 | { |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 127 | struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 128 | int id = rdev_get_id(rdev); |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 129 | int ret; |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 130 | |
| 131 | dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id); |
| 132 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 133 | mc13783_lock(priv->mc13783); |
| 134 | ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg, |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 135 | mc13783_regulators[id].enable_bit, 0); |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 136 | mc13783_unlock(priv->mc13783); |
| 137 | |
| 138 | return ret; |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 141 | static int mc13783_regulator_is_enabled(struct regulator_dev *rdev) |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 142 | { |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 143 | struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev); |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 144 | int ret, id = rdev_get_id(rdev); |
| 145 | unsigned int val; |
| 146 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 147 | mc13783_lock(priv->mc13783); |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 148 | ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val); |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 149 | mc13783_unlock(priv->mc13783); |
| 150 | |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 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 = { |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 158 | .enable = mc13783_regulator_enable, |
| 159 | .disable = mc13783_regulator_disable, |
| 160 | .is_enabled = mc13783_regulator_is_enabled, |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | static int __devinit mc13783_regulator_probe(struct platform_device *pdev) |
| 164 | { |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 165 | struct mc13783_regulator_priv *priv; |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 166 | struct mc13783 *mc13783 = dev_get_drvdata(pdev->dev.parent); |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 167 | struct mc13783_regulator_platform_data *pdata = |
| 168 | dev_get_platdata(&pdev->dev); |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 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 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 174 | priv = kzalloc(sizeof(*priv) + |
| 175 | pdata->num_regulators * sizeof(priv->regulators[0]), |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 176 | GFP_KERNEL); |
| 177 | if (!priv) |
| 178 | return -ENOMEM; |
| 179 | |
| 180 | priv->mc13783 = mc13783; |
| 181 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 182 | for (i = 0; i < pdata->num_regulators; i++) { |
| 183 | init_data = &pdata->regulators[i]; |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 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 | { |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 210 | struct mc13783_regulator_priv *priv = platform_get_drvdata(pdev); |
| 211 | struct mc13783_regulator_platform_data *pdata = |
| 212 | dev_get_platdata(&pdev->dev); |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 213 | int i; |
| 214 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 215 | for (i = 0; i < pdata->num_regulators; i++) |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 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), |
Alberto Panizzo | 735eb93 | 2009-12-14 18:53:35 +0100 | [diff] [blame] | 227 | .probe = mc13783_regulator_probe, |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | static int __init mc13783_regulator_init(void) |
| 231 | { |
Alberto Panizzo | 735eb93 | 2009-12-14 18:53:35 +0100 | [diff] [blame] | 232 | return platform_driver_register(&mc13783_regulator_driver); |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 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 | |
Uwe Kleine-König | a10099b | 2009-11-10 09:18:07 +0100 | [diff] [blame] | 242 | MODULE_LICENSE("GPL v2"); |
Sascha Hauer | 295c08b | 2009-08-19 01:43:50 +0200 | [diff] [blame] | 243 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de"); |
| 244 | MODULE_DESCRIPTION("Regulator Driver for Freescale MC13783 PMIC"); |
| 245 | MODULE_ALIAS("platform:mc13783-regulator"); |