Fix some AB3100 regulator issues
[linux-2.6.git] / drivers / regulator / mc13783-regulator.c
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-private.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/platform_device.h>
15 #include <linux/mfd/mc13783.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19
20 struct mc13783_regulator {
21         struct regulator_desc desc;
22         int reg;
23         int enable_bit;
24 };
25
26 static struct regulator_ops mc13783_regulator_ops;
27
28 static struct mc13783_regulator mc13783_regulators[] = {
29         [MC13783_SW_SW3] = {
30                 .desc = {
31                         .name   = "SW_SW3",
32                         .ops    = &mc13783_regulator_ops,
33                         .type   = REGULATOR_VOLTAGE,
34                         .id     = MC13783_SW_SW3,
35                         .owner  = THIS_MODULE,
36                 },
37                 .reg = MC13783_REG_SWITCHERS_5,
38                 .enable_bit = MC13783_SWCTRL_SW3_EN,
39         },
40         [MC13783_SW_PLL] = {
41                 .desc = {
42                         .name   = "SW_PLL",
43                         .ops    = &mc13783_regulator_ops,
44                         .type   = REGULATOR_VOLTAGE,
45                         .id     = MC13783_SW_PLL,
46                         .owner  = THIS_MODULE,
47                 },
48                 .reg = MC13783_REG_SWITCHERS_4,
49                 .enable_bit = MC13783_SWCTRL_PLL_EN,
50         },
51         [MC13783_REGU_VAUDIO] = {
52                 .desc = {
53                         .name   = "REGU_VAUDIO",
54                         .ops    = &mc13783_regulator_ops,
55                         .type   = REGULATOR_VOLTAGE,
56                         .id     = MC13783_REGU_VAUDIO,
57                         .owner  = THIS_MODULE,
58                 },
59                 .reg = MC13783_REG_REGULATOR_MODE_0,
60                 .enable_bit = MC13783_REGCTRL_VAUDIO_EN,
61         },
62         [MC13783_REGU_VIOHI] = {
63                 .desc = {
64                         .name   = "REGU_VIOHI",
65                         .ops    = &mc13783_regulator_ops,
66                         .type   = REGULATOR_VOLTAGE,
67                         .id     = MC13783_REGU_VIOHI,
68                         .owner  = THIS_MODULE,
69                 },
70                 .reg = MC13783_REG_REGULATOR_MODE_0,
71                 .enable_bit = MC13783_REGCTRL_VIOHI_EN,
72         },
73         [MC13783_REGU_VIOLO] = {
74                 .desc = {
75                         .name   = "REGU_VIOLO",
76                         .ops    = &mc13783_regulator_ops,
77                         .type   = REGULATOR_VOLTAGE,
78                         .id     = MC13783_REGU_VIOLO,
79                         .owner  = THIS_MODULE,
80                 },
81                 .reg = MC13783_REG_REGULATOR_MODE_0,
82                 .enable_bit = MC13783_REGCTRL_VIOLO_EN,
83         },
84         [MC13783_REGU_VDIG] = {
85                 .desc = {
86                         .name   = "REGU_VDIG",
87                         .ops    = &mc13783_regulator_ops,
88                         .type   = REGULATOR_VOLTAGE,
89                         .id     = MC13783_REGU_VDIG,
90                         .owner  = THIS_MODULE,
91                 },
92                 .reg = MC13783_REG_REGULATOR_MODE_0,
93                 .enable_bit = MC13783_REGCTRL_VDIG_EN,
94         },
95         [MC13783_REGU_VGEN] = {
96                 .desc = {
97                         .name   = "REGU_VGEN",
98                         .ops    = &mc13783_regulator_ops,
99                         .type   = REGULATOR_VOLTAGE,
100                         .id     = MC13783_REGU_VGEN,
101                         .owner  = THIS_MODULE,
102                 },
103                 .reg = MC13783_REG_REGULATOR_MODE_0,
104                 .enable_bit = MC13783_REGCTRL_VGEN_EN,
105         },
106         [MC13783_REGU_VRFDIG] = {
107                 .desc = {
108                         .name   = "REGU_VRFDIG",
109                         .ops    = &mc13783_regulator_ops,
110                         .type   = REGULATOR_VOLTAGE,
111                         .id     = MC13783_REGU_VRFDIG,
112                         .owner  = THIS_MODULE,
113                 },
114                 .reg = MC13783_REG_REGULATOR_MODE_0,
115                 .enable_bit = MC13783_REGCTRL_VRFDIG_EN,
116         },
117         [MC13783_REGU_VRFREF] = {
118                 .desc = {
119                         .name   = "REGU_VRFREF",
120                         .ops    = &mc13783_regulator_ops,
121                         .type   = REGULATOR_VOLTAGE,
122                         .id     = MC13783_REGU_VRFREF,
123                         .owner  = THIS_MODULE,
124                 },
125                 .reg = MC13783_REG_REGULATOR_MODE_0,
126                 .enable_bit = MC13783_REGCTRL_VRFREF_EN,
127         },
128         [MC13783_REGU_VRFCP] = {
129                 .desc = {
130                         .name   = "REGU_VRFCP",
131                         .ops    = &mc13783_regulator_ops,
132                         .type   = REGULATOR_VOLTAGE,
133                         .id     = MC13783_REGU_VRFCP,
134                         .owner  = THIS_MODULE,
135                 },
136                 .reg = MC13783_REG_REGULATOR_MODE_0,
137                 .enable_bit = MC13783_REGCTRL_VRFCP_EN,
138         },
139         [MC13783_REGU_VSIM] = {
140                 .desc = {
141                         .name   = "REGU_VSIM",
142                         .ops    = &mc13783_regulator_ops,
143                         .type   = REGULATOR_VOLTAGE,
144                         .id     = MC13783_REGU_VSIM,
145                         .owner  = THIS_MODULE,
146                 },
147                 .reg = MC13783_REG_REGULATOR_MODE_1,
148                 .enable_bit = MC13783_REGCTRL_VSIM_EN,
149         },
150         [MC13783_REGU_VESIM] = {
151                 .desc = {
152                         .name   = "REGU_VESIM",
153                         .ops    = &mc13783_regulator_ops,
154                         .type   = REGULATOR_VOLTAGE,
155                         .id     = MC13783_REGU_VESIM,
156                         .owner  = THIS_MODULE,
157                 },
158                 .reg = MC13783_REG_REGULATOR_MODE_1,
159                 .enable_bit = MC13783_REGCTRL_VESIM_EN,
160         },
161         [MC13783_REGU_VCAM] = {
162                 .desc = {
163                         .name   = "REGU_VCAM",
164                         .ops    = &mc13783_regulator_ops,
165                         .type   = REGULATOR_VOLTAGE,
166                         .id     = MC13783_REGU_VCAM,
167                         .owner  = THIS_MODULE,
168                 },
169                 .reg = MC13783_REG_REGULATOR_MODE_1,
170                 .enable_bit = MC13783_REGCTRL_VCAM_EN,
171         },
172         [MC13783_REGU_VRFBG] = {
173                 .desc = {
174                         .name   = "REGU_VRFBG",
175                         .ops    = &mc13783_regulator_ops,
176                         .type   = REGULATOR_VOLTAGE,
177                         .id     = MC13783_REGU_VRFBG,
178                         .owner  = THIS_MODULE,
179                 },
180                 .reg = MC13783_REG_REGULATOR_MODE_1,
181                 .enable_bit = MC13783_REGCTRL_VRFBG_EN,
182         },
183         [MC13783_REGU_VVIB] = {
184                 .desc = {
185                         .name   = "REGU_VVIB",
186                         .ops    = &mc13783_regulator_ops,
187                         .type   = REGULATOR_VOLTAGE,
188                         .id     = MC13783_REGU_VVIB,
189                         .owner  = THIS_MODULE,
190                 },
191                 .reg = MC13783_REG_REGULATOR_MODE_1,
192                 .enable_bit = MC13783_REGCTRL_VVIB_EN,
193         },
194         [MC13783_REGU_VRF1] = {
195                 .desc = {
196                         .name   = "REGU_VRF1",
197                         .ops    = &mc13783_regulator_ops,
198                         .type   = REGULATOR_VOLTAGE,
199                         .id     = MC13783_REGU_VRF1,
200                         .owner  = THIS_MODULE,
201                 },
202                 .reg = MC13783_REG_REGULATOR_MODE_1,
203                 .enable_bit = MC13783_REGCTRL_VRF1_EN,
204         },
205         [MC13783_REGU_VRF2] = {
206                 .desc = {
207                         .name   = "REGU_VRF2",
208                         .ops    = &mc13783_regulator_ops,
209                         .type   = REGULATOR_VOLTAGE,
210                         .id     = MC13783_REGU_VRF2,
211                         .owner  = THIS_MODULE,
212                 },
213                 .reg = MC13783_REG_REGULATOR_MODE_1,
214                 .enable_bit = MC13783_REGCTRL_VRF2_EN,
215         },
216         [MC13783_REGU_VMMC1] = {
217                 .desc = {
218                         .name   = "REGU_VMMC1",
219                         .ops    = &mc13783_regulator_ops,
220                         .type   = REGULATOR_VOLTAGE,
221                         .id     = MC13783_REGU_VMMC1,
222                         .owner  = THIS_MODULE,
223                 },
224                 .reg = MC13783_REG_REGULATOR_MODE_1,
225                 .enable_bit = MC13783_REGCTRL_VMMC1_EN,
226         },
227         [MC13783_REGU_VMMC2] = {
228                 .desc = {
229                         .name   = "REGU_VMMC2",
230                         .ops    = &mc13783_regulator_ops,
231                         .type   = REGULATOR_VOLTAGE,
232                         .id     = MC13783_REGU_VMMC2,
233                         .owner  = THIS_MODULE,
234                 },
235                 .reg = MC13783_REG_REGULATOR_MODE_1,
236                 .enable_bit = MC13783_REGCTRL_VMMC2_EN,
237         },
238         [MC13783_REGU_GPO1] = {
239                 .desc = {
240                         .name   = "REGU_GPO1",
241                         .ops    = &mc13783_regulator_ops,
242                         .type   = REGULATOR_VOLTAGE,
243                         .id     = MC13783_REGU_GPO1,
244                         .owner  = THIS_MODULE,
245                 },
246                 .reg = MC13783_REG_POWER_MISCELLANEOUS,
247                 .enable_bit = MC13783_REGCTRL_GPO1_EN,
248         },
249         [MC13783_REGU_GPO2] = {
250                 .desc = {
251                         .name   = "REGU_GPO2",
252                         .ops    = &mc13783_regulator_ops,
253                         .type   = REGULATOR_VOLTAGE,
254                         .id     = MC13783_REGU_GPO2,
255                         .owner  = THIS_MODULE,
256                 },
257                 .reg = MC13783_REG_POWER_MISCELLANEOUS,
258                 .enable_bit = MC13783_REGCTRL_GPO2_EN,
259         },
260         [MC13783_REGU_GPO3] = {
261                 .desc = {
262                         .name   = "REGU_GPO3",
263                         .ops    = &mc13783_regulator_ops,
264                         .type   = REGULATOR_VOLTAGE,
265                         .id     = MC13783_REGU_GPO3,
266                         .owner  = THIS_MODULE,
267                 },
268                 .reg = MC13783_REG_POWER_MISCELLANEOUS,
269                 .enable_bit = MC13783_REGCTRL_GPO3_EN,
270         },
271         [MC13783_REGU_GPO4] = {
272                 .desc = {
273                         .name   = "REGU_GPO4",
274                         .ops    = &mc13783_regulator_ops,
275                         .type   = REGULATOR_VOLTAGE,
276                         .id     = MC13783_REGU_GPO4,
277                         .owner  = THIS_MODULE,
278                 },
279                 .reg = MC13783_REG_POWER_MISCELLANEOUS,
280                 .enable_bit = MC13783_REGCTRL_GPO4_EN,
281         },
282 };
283
284 struct mc13783_priv {
285         struct regulator_desc desc[ARRAY_SIZE(mc13783_regulators)];
286         struct mc13783 *mc13783;
287         struct regulator_dev *regulators[0];
288 };
289
290 static int mc13783_enable(struct regulator_dev *rdev)
291 {
292         struct mc13783_priv *priv = rdev_get_drvdata(rdev);
293         int id = rdev_get_id(rdev);
294
295         dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
296
297         return mc13783_set_bits(priv->mc13783, mc13783_regulators[id].reg,
298                         mc13783_regulators[id].enable_bit,
299                         mc13783_regulators[id].enable_bit);
300 }
301
302 static int mc13783_disable(struct regulator_dev *rdev)
303 {
304         struct mc13783_priv *priv = rdev_get_drvdata(rdev);
305         int id = rdev_get_id(rdev);
306
307         dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
308
309         return mc13783_set_bits(priv->mc13783, mc13783_regulators[id].reg,
310                         mc13783_regulators[id].enable_bit, 0);
311 }
312
313 static int mc13783_is_enabled(struct regulator_dev *rdev)
314 {
315         struct mc13783_priv *priv = rdev_get_drvdata(rdev);
316         int ret, id = rdev_get_id(rdev);
317         unsigned int val;
318
319         ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val);
320         if (ret)
321                 return ret;
322
323         return (val & mc13783_regulators[id].enable_bit) != 0;
324 }
325
326 static struct regulator_ops mc13783_regulator_ops = {
327         .enable         = mc13783_enable,
328         .disable        = mc13783_disable,
329         .is_enabled     = mc13783_is_enabled,
330 };
331
332 static int __devinit mc13783_regulator_probe(struct platform_device *pdev)
333 {
334         struct mc13783_priv *priv;
335         struct mc13783 *mc13783 = dev_get_drvdata(pdev->dev.parent);
336         struct mc13783_regulator_init_data *init_data;
337         int i, ret;
338
339         dev_dbg(&pdev->dev, "mc13783_regulator_probe id %d\n", pdev->id);
340
341         priv = kzalloc(sizeof(*priv) + mc13783->num_regulators * sizeof(void *),
342                         GFP_KERNEL);
343         if (!priv)
344                 return -ENOMEM;
345
346         priv->mc13783 = mc13783;
347
348         for (i = 0; i < mc13783->num_regulators; i++) {
349                 init_data = &mc13783->regulators[i];
350                 priv->regulators[i] = regulator_register(
351                                 &mc13783_regulators[init_data->id].desc,
352                                 &pdev->dev, init_data->init_data, priv);
353
354                 if (IS_ERR(priv->regulators[i])) {
355                         dev_err(&pdev->dev, "failed to register regulator %s\n",
356                                 mc13783_regulators[i].desc.name);
357                         ret = PTR_ERR(priv->regulators[i]);
358                         goto err;
359                 }
360         }
361
362         platform_set_drvdata(pdev, priv);
363
364         return 0;
365 err:
366         while (--i >= 0)
367                 regulator_unregister(priv->regulators[i]);
368
369         kfree(priv);
370
371         return ret;
372 }
373
374 static int __devexit mc13783_regulator_remove(struct platform_device *pdev)
375 {
376         struct mc13783_priv *priv = platform_get_drvdata(pdev);
377         struct mc13783 *mc13783 = priv->mc13783;
378         int i;
379
380         for (i = 0; i < mc13783->num_regulators; i++)
381                 regulator_unregister(priv->regulators[i]);
382
383         return 0;
384 }
385
386 static struct platform_driver mc13783_regulator_driver = {
387         .driver = {
388                 .name   = "mc13783-regulator",
389                 .owner  = THIS_MODULE,
390         },
391         .remove         = __devexit_p(mc13783_regulator_remove),
392 };
393
394 static int __init mc13783_regulator_init(void)
395 {
396         return platform_driver_probe(&mc13783_regulator_driver,
397                         mc13783_regulator_probe);
398 }
399 subsys_initcall(mc13783_regulator_init);
400
401 static void __exit mc13783_regulator_exit(void)
402 {
403         platform_driver_unregister(&mc13783_regulator_driver);
404 }
405 module_exit(mc13783_regulator_exit);
406
407 MODULE_LICENSE("GPL");
408 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
409 MODULE_DESCRIPTION("Regulator Driver for Freescale MC13783 PMIC");
410 MODULE_ALIAS("platform:mc13783-regulator");