blob: 39c4953000458d2ce7cba8cc9958a6e247a729b0 [file] [log] [blame]
Sascha Hauer295c08b2009-08-19 01:43:50 +02001/*
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öniga10099b2009-11-10 09:18:07 +010011#include <linux/mfd/mc13783.h>
Sascha Hauer295c08b2009-08-19 01:43:50 +020012#include <linux/regulator/machine.h>
13#include <linux/regulator/driver.h>
14#include <linux/platform_device.h>
Sascha Hauer295c08b2009-08-19 01:43:50 +020015#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/err.h>
18
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +010019#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 Hauer295c08b2009-08-19 01:43:50 +020052struct mc13783_regulator {
53 struct regulator_desc desc;
54 int reg;
55 int enable_bit;
56};
57
58static struct regulator_ops mc13783_regulator_ops;
59
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +010060#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 Hauer295c08b2009-08-19 01:43:50 +020076static struct mc13783_regulator mc13783_regulators[] = {
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +010077 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 Hauer295c08b2009-08-19 01:43:50 +0200101};
102
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100103struct mc13783_regulator_priv {
Sascha Hauer295c08b2009-08-19 01:43:50 +0200104 struct mc13783 *mc13783;
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100105 struct regulator_dev *regulators[];
Sascha Hauer295c08b2009-08-19 01:43:50 +0200106};
107
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100108static int mc13783_regulator_enable(struct regulator_dev *rdev)
Sascha Hauer295c08b2009-08-19 01:43:50 +0200109{
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100110 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
Sascha Hauer295c08b2009-08-19 01:43:50 +0200111 int id = rdev_get_id(rdev);
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100112 int ret;
Sascha Hauer295c08b2009-08-19 01:43:50 +0200113
114 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
115
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100116 mc13783_lock(priv->mc13783);
117 ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg,
Sascha Hauer295c08b2009-08-19 01:43:50 +0200118 mc13783_regulators[id].enable_bit,
119 mc13783_regulators[id].enable_bit);
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100120 mc13783_unlock(priv->mc13783);
121
122 return ret;
Sascha Hauer295c08b2009-08-19 01:43:50 +0200123}
124
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100125static int mc13783_regulator_disable(struct regulator_dev *rdev)
Sascha Hauer295c08b2009-08-19 01:43:50 +0200126{
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100127 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
Sascha Hauer295c08b2009-08-19 01:43:50 +0200128 int id = rdev_get_id(rdev);
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100129 int ret;
Sascha Hauer295c08b2009-08-19 01:43:50 +0200130
131 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
132
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100133 mc13783_lock(priv->mc13783);
134 ret = mc13783_reg_rmw(priv->mc13783, mc13783_regulators[id].reg,
Sascha Hauer295c08b2009-08-19 01:43:50 +0200135 mc13783_regulators[id].enable_bit, 0);
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100136 mc13783_unlock(priv->mc13783);
137
138 return ret;
Sascha Hauer295c08b2009-08-19 01:43:50 +0200139}
140
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100141static int mc13783_regulator_is_enabled(struct regulator_dev *rdev)
Sascha Hauer295c08b2009-08-19 01:43:50 +0200142{
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100143 struct mc13783_regulator_priv *priv = rdev_get_drvdata(rdev);
Sascha Hauer295c08b2009-08-19 01:43:50 +0200144 int ret, id = rdev_get_id(rdev);
145 unsigned int val;
146
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100147 mc13783_lock(priv->mc13783);
Sascha Hauer295c08b2009-08-19 01:43:50 +0200148 ret = mc13783_reg_read(priv->mc13783, mc13783_regulators[id].reg, &val);
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100149 mc13783_unlock(priv->mc13783);
150
Sascha Hauer295c08b2009-08-19 01:43:50 +0200151 if (ret)
152 return ret;
153
154 return (val & mc13783_regulators[id].enable_bit) != 0;
155}
156
157static struct regulator_ops mc13783_regulator_ops = {
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100158 .enable = mc13783_regulator_enable,
159 .disable = mc13783_regulator_disable,
160 .is_enabled = mc13783_regulator_is_enabled,
Sascha Hauer295c08b2009-08-19 01:43:50 +0200161};
162
163static int __devinit mc13783_regulator_probe(struct platform_device *pdev)
164{
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100165 struct mc13783_regulator_priv *priv;
Sascha Hauer295c08b2009-08-19 01:43:50 +0200166 struct mc13783 *mc13783 = dev_get_drvdata(pdev->dev.parent);
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100167 struct mc13783_regulator_platform_data *pdata =
168 dev_get_platdata(&pdev->dev);
Sascha Hauer295c08b2009-08-19 01:43:50 +0200169 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öniga10099b2009-11-10 09:18:07 +0100174 priv = kzalloc(sizeof(*priv) +
175 pdata->num_regulators * sizeof(priv->regulators[0]),
Sascha Hauer295c08b2009-08-19 01:43:50 +0200176 GFP_KERNEL);
177 if (!priv)
178 return -ENOMEM;
179
180 priv->mc13783 = mc13783;
181
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100182 for (i = 0; i < pdata->num_regulators; i++) {
183 init_data = &pdata->regulators[i];
Sascha Hauer295c08b2009-08-19 01:43:50 +0200184 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;
199err:
200 while (--i >= 0)
201 regulator_unregister(priv->regulators[i]);
202
203 kfree(priv);
204
205 return ret;
206}
207
208static int __devexit mc13783_regulator_remove(struct platform_device *pdev)
209{
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100210 struct mc13783_regulator_priv *priv = platform_get_drvdata(pdev);
211 struct mc13783_regulator_platform_data *pdata =
212 dev_get_platdata(&pdev->dev);
Sascha Hauer295c08b2009-08-19 01:43:50 +0200213 int i;
214
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100215 for (i = 0; i < pdata->num_regulators; i++)
Sascha Hauer295c08b2009-08-19 01:43:50 +0200216 regulator_unregister(priv->regulators[i]);
217
218 return 0;
219}
220
221static 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 Panizzo735eb932009-12-14 18:53:35 +0100227 .probe = mc13783_regulator_probe,
Sascha Hauer295c08b2009-08-19 01:43:50 +0200228};
229
230static int __init mc13783_regulator_init(void)
231{
Alberto Panizzo735eb932009-12-14 18:53:35 +0100232 return platform_driver_register(&mc13783_regulator_driver);
Sascha Hauer295c08b2009-08-19 01:43:50 +0200233}
234subsys_initcall(mc13783_regulator_init);
235
236static void __exit mc13783_regulator_exit(void)
237{
238 platform_driver_unregister(&mc13783_regulator_driver);
239}
240module_exit(mc13783_regulator_exit);
241
Uwe Kleine-Königa10099b2009-11-10 09:18:07 +0100242MODULE_LICENSE("GPL v2");
Sascha Hauer295c08b2009-08-19 01:43:50 +0200243MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
244MODULE_DESCRIPTION("Regulator Driver for Freescale MC13783 PMIC");
245MODULE_ALIAS("platform:mc13783-regulator");