]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/regulator/anatop-regulator.c
ARM: dts: Enable 88pm860x pmic
[karo-tx-linux.git] / drivers / regulator / anatop-regulator.c
1 /*
2  * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
3  */
4
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 as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <linux/slab.h>
22 #include <linux/device.h>
23 #include <linux/module.h>
24 #include <linux/mfd/syscon.h>
25 #include <linux/err.h>
26 #include <linux/io.h>
27 #include <linux/platform_device.h>
28 #include <linux/of.h>
29 #include <linux/of_address.h>
30 #include <linux/regmap.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/of_regulator.h>
33
34 struct anatop_regulator {
35         const char *name;
36         u32 control_reg;
37         struct regmap *anatop;
38         int vol_bit_shift;
39         int vol_bit_width;
40         int min_bit_val;
41         int min_voltage;
42         int max_voltage;
43         struct regulator_desc rdesc;
44         struct regulator_init_data *initdata;
45 };
46
47 static int anatop_regmap_set_voltage_sel(struct regulator_dev *reg,
48                                         unsigned selector)
49 {
50         struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
51         u32 val, mask;
52
53         if (!anatop_reg->control_reg)
54                 return -ENOTSUPP;
55
56         val = anatop_reg->min_bit_val + selector;
57         dev_dbg(&reg->dev, "%s: calculated val %d\n", __func__, val);
58         mask = ((1 << anatop_reg->vol_bit_width) - 1) <<
59                 anatop_reg->vol_bit_shift;
60         val <<= anatop_reg->vol_bit_shift;
61         regmap_update_bits(anatop_reg->anatop, anatop_reg->control_reg,
62                                 mask, val);
63
64         return 0;
65 }
66
67 static int anatop_regmap_get_voltage_sel(struct regulator_dev *reg)
68 {
69         struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
70         u32 val;
71
72         if (!anatop_reg->control_reg)
73                 return -ENOTSUPP;
74
75         regmap_read(anatop_reg->anatop, anatop_reg->control_reg, &val);
76         val = (val & ((1 << anatop_reg->vol_bit_width) - 1)) >>
77                 anatop_reg->vol_bit_shift;
78
79         return val - anatop_reg->min_bit_val;
80 }
81
82 static struct regulator_ops anatop_rops = {
83         .set_voltage_sel = anatop_regmap_set_voltage_sel,
84         .get_voltage_sel = anatop_regmap_get_voltage_sel,
85         .list_voltage = regulator_list_voltage_linear,
86         .map_voltage = regulator_map_voltage_linear,
87 };
88
89 static int __devinit anatop_regulator_probe(struct platform_device *pdev)
90 {
91         struct device *dev = &pdev->dev;
92         struct device_node *np = dev->of_node;
93         struct device_node *anatop_np;
94         struct regulator_desc *rdesc;
95         struct regulator_dev *rdev;
96         struct anatop_regulator *sreg;
97         struct regulator_init_data *initdata;
98         struct regulator_config config = { };
99         int ret = 0;
100
101         initdata = of_get_regulator_init_data(dev, np);
102         sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
103         if (!sreg)
104                 return -ENOMEM;
105         sreg->initdata = initdata;
106         sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL),
107                              GFP_KERNEL);
108         rdesc = &sreg->rdesc;
109         memset(rdesc, 0, sizeof(*rdesc));
110         rdesc->name = sreg->name;
111         rdesc->ops = &anatop_rops;
112         rdesc->type = REGULATOR_VOLTAGE;
113         rdesc->owner = THIS_MODULE;
114
115         anatop_np = of_get_parent(np);
116         if (!anatop_np)
117                 return -ENODEV;
118         sreg->anatop = syscon_node_to_regmap(anatop_np);
119         of_node_put(anatop_np);
120         if (IS_ERR(sreg->anatop))
121                 return PTR_ERR(sreg->anatop);
122
123         ret = of_property_read_u32(np, "anatop-reg-offset",
124                                    &sreg->control_reg);
125         if (ret) {
126                 dev_err(dev, "no anatop-reg-offset property set\n");
127                 goto anatop_probe_end;
128         }
129         ret = of_property_read_u32(np, "anatop-vol-bit-width",
130                                    &sreg->vol_bit_width);
131         if (ret) {
132                 dev_err(dev, "no anatop-vol-bit-width property set\n");
133                 goto anatop_probe_end;
134         }
135         ret = of_property_read_u32(np, "anatop-vol-bit-shift",
136                                    &sreg->vol_bit_shift);
137         if (ret) {
138                 dev_err(dev, "no anatop-vol-bit-shift property set\n");
139                 goto anatop_probe_end;
140         }
141         ret = of_property_read_u32(np, "anatop-min-bit-val",
142                                    &sreg->min_bit_val);
143         if (ret) {
144                 dev_err(dev, "no anatop-min-bit-val property set\n");
145                 goto anatop_probe_end;
146         }
147         ret = of_property_read_u32(np, "anatop-min-voltage",
148                                    &sreg->min_voltage);
149         if (ret) {
150                 dev_err(dev, "no anatop-min-voltage property set\n");
151                 goto anatop_probe_end;
152         }
153         ret = of_property_read_u32(np, "anatop-max-voltage",
154                                    &sreg->max_voltage);
155         if (ret) {
156                 dev_err(dev, "no anatop-max-voltage property set\n");
157                 goto anatop_probe_end;
158         }
159
160         rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage)
161                 / 25000 + 1;
162         rdesc->min_uV = sreg->min_voltage;
163         rdesc->uV_step = 25000;
164
165         config.dev = &pdev->dev;
166         config.init_data = initdata;
167         config.driver_data = sreg;
168         config.of_node = pdev->dev.of_node;
169
170         /* register regulator */
171         rdev = regulator_register(rdesc, &config);
172         if (IS_ERR(rdev)) {
173                 dev_err(dev, "failed to register %s\n",
174                         rdesc->name);
175                 ret = PTR_ERR(rdev);
176                 goto anatop_probe_end;
177         }
178
179         platform_set_drvdata(pdev, rdev);
180
181 anatop_probe_end:
182         if (ret)
183                 kfree(sreg->name);
184
185         return ret;
186 }
187
188 static int __devexit anatop_regulator_remove(struct platform_device *pdev)
189 {
190         struct regulator_dev *rdev = platform_get_drvdata(pdev);
191         struct anatop_regulator *sreg = rdev_get_drvdata(rdev);
192         const char *name = sreg->name;
193
194         regulator_unregister(rdev);
195         kfree(name);
196
197         return 0;
198 }
199
200 static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
201         { .compatible = "fsl,anatop-regulator", },
202         { /* end */ }
203 };
204
205 static struct platform_driver anatop_regulator_driver = {
206         .driver = {
207                 .name   = "anatop_regulator",
208                 .owner  = THIS_MODULE,
209                 .of_match_table = of_anatop_regulator_match_tbl,
210         },
211         .probe  = anatop_regulator_probe,
212         .remove = __devexit_p(anatop_regulator_remove),
213 };
214
215 static int __init anatop_regulator_init(void)
216 {
217         return platform_driver_register(&anatop_regulator_driver);
218 }
219 postcore_initcall(anatop_regulator_init);
220
221 static void __exit anatop_regulator_exit(void)
222 {
223         platform_driver_unregister(&anatop_regulator_driver);
224 }
225 module_exit(anatop_regulator_exit);
226
227 MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
228               "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
229 MODULE_DESCRIPTION("ANATOP Regulator driver");
230 MODULE_LICENSE("GPL v2");