Merge tag 'driver-core-3.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.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/err.h>
25 #include <linux/io.h>
26 #include <linux/platform_device.h>
27 #include <linux/of.h>
28 #include <linux/of_address.h>
29 #include <linux/mfd/anatop.h>
30 #include <linux/regulator/driver.h>
31 #include <linux/regulator/of_regulator.h>
32
33 struct anatop_regulator {
34         const char *name;
35         u32 control_reg;
36         struct anatop *mfd;
37         int vol_bit_shift;
38         int vol_bit_width;
39         int min_bit_val;
40         int min_voltage;
41         int max_voltage;
42         struct regulator_desc rdesc;
43         struct regulator_init_data *initdata;
44 };
45
46 static int anatop_set_voltage(struct regulator_dev *reg, int min_uV,
47                                   int max_uV, unsigned *selector)
48 {
49         struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
50         u32 val, sel;
51         int uv;
52
53         uv = min_uV;
54         dev_dbg(&reg->dev, "%s: uv %d, min %d, max %d\n", __func__,
55                 uv, anatop_reg->min_voltage,
56                 anatop_reg->max_voltage);
57
58         if (uv < anatop_reg->min_voltage) {
59                 if (max_uV > anatop_reg->min_voltage)
60                         uv = anatop_reg->min_voltage;
61                 else
62                         return -EINVAL;
63         }
64
65         if (!anatop_reg->control_reg)
66                 return -ENOTSUPP;
67
68         sel = DIV_ROUND_UP(uv - anatop_reg->min_voltage, 25000);
69         if (sel * 25000 + anatop_reg->min_voltage > anatop_reg->max_voltage)
70                 return -EINVAL;
71         val = anatop_reg->min_bit_val + sel;
72         *selector = sel;
73         dev_dbg(&reg->dev, "%s: calculated val %d\n", __func__, val);
74         anatop_set_bits(anatop_reg->mfd,
75                         anatop_reg->control_reg,
76                         anatop_reg->vol_bit_shift,
77                         anatop_reg->vol_bit_width,
78                         val);
79
80         return 0;
81 }
82
83 static int anatop_get_voltage_sel(struct regulator_dev *reg)
84 {
85         struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
86         u32 val;
87
88         if (!anatop_reg->control_reg)
89                 return -ENOTSUPP;
90
91         val = anatop_get_bits(anatop_reg->mfd,
92                               anatop_reg->control_reg,
93                               anatop_reg->vol_bit_shift,
94                               anatop_reg->vol_bit_width);
95
96         return val - anatop_reg->min_bit_val;
97 }
98
99 static int anatop_list_voltage(struct regulator_dev *reg, unsigned selector)
100 {
101         struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
102         int uv;
103
104         uv = anatop_reg->min_voltage + selector * 25000;
105         dev_dbg(&reg->dev, "vddio = %d, selector = %u\n", uv, selector);
106
107         return uv;
108 }
109
110 static struct regulator_ops anatop_rops = {
111         .set_voltage     = anatop_set_voltage,
112         .get_voltage_sel = anatop_get_voltage_sel,
113         .list_voltage    = anatop_list_voltage,
114 };
115
116 static int __devinit anatop_regulator_probe(struct platform_device *pdev)
117 {
118         struct device *dev = &pdev->dev;
119         struct device_node *np = dev->of_node;
120         struct regulator_desc *rdesc;
121         struct regulator_dev *rdev;
122         struct anatop_regulator *sreg;
123         struct regulator_init_data *initdata;
124         struct anatop *anatopmfd = dev_get_drvdata(pdev->dev.parent);
125         struct regulator_config config = { };
126         int ret = 0;
127
128         initdata = of_get_regulator_init_data(dev, np);
129         sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
130         if (!sreg)
131                 return -ENOMEM;
132         sreg->initdata = initdata;
133         sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL),
134                              GFP_KERNEL);
135         rdesc = &sreg->rdesc;
136         memset(rdesc, 0, sizeof(*rdesc));
137         rdesc->name = sreg->name;
138         rdesc->ops = &anatop_rops;
139         rdesc->type = REGULATOR_VOLTAGE;
140         rdesc->owner = THIS_MODULE;
141         sreg->mfd = anatopmfd;
142         ret = of_property_read_u32(np, "anatop-reg-offset",
143                                    &sreg->control_reg);
144         if (ret) {
145                 dev_err(dev, "no anatop-reg-offset property set\n");
146                 goto anatop_probe_end;
147         }
148         ret = of_property_read_u32(np, "anatop-vol-bit-width",
149                                    &sreg->vol_bit_width);
150         if (ret) {
151                 dev_err(dev, "no anatop-vol-bit-width property set\n");
152                 goto anatop_probe_end;
153         }
154         ret = of_property_read_u32(np, "anatop-vol-bit-shift",
155                                    &sreg->vol_bit_shift);
156         if (ret) {
157                 dev_err(dev, "no anatop-vol-bit-shift property set\n");
158                 goto anatop_probe_end;
159         }
160         ret = of_property_read_u32(np, "anatop-min-bit-val",
161                                    &sreg->min_bit_val);
162         if (ret) {
163                 dev_err(dev, "no anatop-min-bit-val property set\n");
164                 goto anatop_probe_end;
165         }
166         ret = of_property_read_u32(np, "anatop-min-voltage",
167                                    &sreg->min_voltage);
168         if (ret) {
169                 dev_err(dev, "no anatop-min-voltage property set\n");
170                 goto anatop_probe_end;
171         }
172         ret = of_property_read_u32(np, "anatop-max-voltage",
173                                    &sreg->max_voltage);
174         if (ret) {
175                 dev_err(dev, "no anatop-max-voltage property set\n");
176                 goto anatop_probe_end;
177         }
178
179         rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage)
180                 / 25000 + 1;
181
182         config.dev = &pdev->dev;
183         config.init_data = initdata;
184         config.driver_data = sreg;
185         config.of_node = pdev->dev.of_node;
186
187         /* register regulator */
188         rdev = regulator_register(rdesc, &config);
189         if (IS_ERR(rdev)) {
190                 dev_err(dev, "failed to register %s\n",
191                         rdesc->name);
192                 ret = PTR_ERR(rdev);
193                 goto anatop_probe_end;
194         }
195
196         platform_set_drvdata(pdev, rdev);
197
198 anatop_probe_end:
199         if (ret)
200                 kfree(sreg->name);
201
202         return ret;
203 }
204
205 static int __devexit anatop_regulator_remove(struct platform_device *pdev)
206 {
207         struct regulator_dev *rdev = platform_get_drvdata(pdev);
208         struct anatop_regulator *sreg = rdev_get_drvdata(rdev);
209         const char *name = sreg->name;
210
211         regulator_unregister(rdev);
212         kfree(name);
213
214         return 0;
215 }
216
217 static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
218         { .compatible = "fsl,anatop-regulator", },
219         { /* end */ }
220 };
221
222 static struct platform_driver anatop_regulator_driver = {
223         .driver = {
224                 .name   = "anatop_regulator",
225                 .owner  = THIS_MODULE,
226                 .of_match_table = of_anatop_regulator_match_tbl,
227         },
228         .probe  = anatop_regulator_probe,
229         .remove = anatop_regulator_remove,
230 };
231
232 static int __init anatop_regulator_init(void)
233 {
234         return platform_driver_register(&anatop_regulator_driver);
235 }
236 postcore_initcall(anatop_regulator_init);
237
238 static void __exit anatop_regulator_exit(void)
239 {
240         platform_driver_unregister(&anatop_regulator_driver);
241 }
242 module_exit(anatop_regulator_exit);
243
244 MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
245               "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
246 MODULE_DESCRIPTION("ANATOP Regulator driver");
247 MODULE_LICENSE("GPL v2");