blob: d04cf21132459ca1e8e891342a04c10f08d84aaf [file] [log] [blame]
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +08001/*
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
33struct 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
46static 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);
Richard Zhaob09530e2012-05-13 09:18:02 +080050 u32 val, sel, mask;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080051 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);
Richard Zhaob09530e2012-05-13 09:18:02 +080074 mask = ((1 << anatop_reg->vol_bit_width) - 1) <<
75 anatop_reg->vol_bit_shift;
76 val <<= anatop_reg->vol_bit_shift;
77 anatop_write_reg(anatop_reg->mfd, anatop_reg->control_reg, val, mask);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080078
79 return 0;
80}
81
82static int anatop_get_voltage_sel(struct regulator_dev *reg)
83{
84 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
85 u32 val;
86
87 if (!anatop_reg->control_reg)
88 return -ENOTSUPP;
89
Richard Zhaob09530e2012-05-13 09:18:02 +080090 val = anatop_read_reg(anatop_reg->mfd, anatop_reg->control_reg);
91 val = (val & ((1 << anatop_reg->vol_bit_width) - 1)) >>
92 anatop_reg->vol_bit_shift;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080093
94 return val - anatop_reg->min_bit_val;
95}
96
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080097static struct regulator_ops anatop_rops = {
98 .set_voltage = anatop_set_voltage,
99 .get_voltage_sel = anatop_get_voltage_sel,
Axel Lin0713e6a2012-05-14 11:06:44 +0800100 .list_voltage = regulator_list_voltage_linear,
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800101};
102
103static int __devinit anatop_regulator_probe(struct platform_device *pdev)
104{
105 struct device *dev = &pdev->dev;
106 struct device_node *np = dev->of_node;
107 struct regulator_desc *rdesc;
108 struct regulator_dev *rdev;
109 struct anatop_regulator *sreg;
110 struct regulator_init_data *initdata;
111 struct anatop *anatopmfd = dev_get_drvdata(pdev->dev.parent);
Axel Lind914d812012-04-10 22:45:01 +0800112 struct regulator_config config = { };
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800113 int ret = 0;
114
115 initdata = of_get_regulator_init_data(dev, np);
116 sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
117 if (!sreg)
118 return -ENOMEM;
119 sreg->initdata = initdata;
120 sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL),
121 GFP_KERNEL);
122 rdesc = &sreg->rdesc;
123 memset(rdesc, 0, sizeof(*rdesc));
124 rdesc->name = sreg->name;
125 rdesc->ops = &anatop_rops;
126 rdesc->type = REGULATOR_VOLTAGE;
127 rdesc->owner = THIS_MODULE;
128 sreg->mfd = anatopmfd;
Ying-Chun Liu (PaulLiu)2f2cc272012-03-27 15:54:01 +0800129 ret = of_property_read_u32(np, "anatop-reg-offset",
130 &sreg->control_reg);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800131 if (ret) {
Ying-Chun Liu (PaulLiu)2f2cc272012-03-27 15:54:01 +0800132 dev_err(dev, "no anatop-reg-offset property set\n");
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800133 goto anatop_probe_end;
134 }
135 ret = of_property_read_u32(np, "anatop-vol-bit-width",
136 &sreg->vol_bit_width);
137 if (ret) {
138 dev_err(dev, "no anatop-vol-bit-width property set\n");
139 goto anatop_probe_end;
140 }
141 ret = of_property_read_u32(np, "anatop-vol-bit-shift",
142 &sreg->vol_bit_shift);
143 if (ret) {
144 dev_err(dev, "no anatop-vol-bit-shift property set\n");
145 goto anatop_probe_end;
146 }
147 ret = of_property_read_u32(np, "anatop-min-bit-val",
148 &sreg->min_bit_val);
149 if (ret) {
150 dev_err(dev, "no anatop-min-bit-val property set\n");
151 goto anatop_probe_end;
152 }
153 ret = of_property_read_u32(np, "anatop-min-voltage",
154 &sreg->min_voltage);
155 if (ret) {
156 dev_err(dev, "no anatop-min-voltage property set\n");
157 goto anatop_probe_end;
158 }
159 ret = of_property_read_u32(np, "anatop-max-voltage",
160 &sreg->max_voltage);
161 if (ret) {
162 dev_err(dev, "no anatop-max-voltage property set\n");
163 goto anatop_probe_end;
164 }
165
166 rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage)
167 / 25000 + 1;
Axel Lin0713e6a2012-05-14 11:06:44 +0800168 rdesc->min_uV = sreg->min_voltage;
169 rdesc->uV_step = 25000;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800170
Axel Lind914d812012-04-10 22:45:01 +0800171 config.dev = &pdev->dev;
172 config.init_data = initdata;
173 config.driver_data = sreg;
174 config.of_node = pdev->dev.of_node;
175
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800176 /* register regulator */
Axel Lind914d812012-04-10 22:45:01 +0800177 rdev = regulator_register(rdesc, &config);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800178 if (IS_ERR(rdev)) {
179 dev_err(dev, "failed to register %s\n",
180 rdesc->name);
181 ret = PTR_ERR(rdev);
182 goto anatop_probe_end;
183 }
184
185 platform_set_drvdata(pdev, rdev);
186
187anatop_probe_end:
188 if (ret)
189 kfree(sreg->name);
190
191 return ret;
192}
193
194static int __devexit anatop_regulator_remove(struct platform_device *pdev)
195{
196 struct regulator_dev *rdev = platform_get_drvdata(pdev);
197 struct anatop_regulator *sreg = rdev_get_drvdata(rdev);
198 const char *name = sreg->name;
199
200 regulator_unregister(rdev);
201 kfree(name);
202
203 return 0;
204}
205
206static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
207 { .compatible = "fsl,anatop-regulator", },
208 { /* end */ }
209};
210
Shawn Guoc0d78c22012-04-02 14:57:01 +0800211static struct platform_driver anatop_regulator_driver = {
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800212 .driver = {
213 .name = "anatop_regulator",
214 .owner = THIS_MODULE,
215 .of_match_table = of_anatop_regulator_match_tbl,
216 },
217 .probe = anatop_regulator_probe,
218 .remove = anatop_regulator_remove,
219};
220
221static int __init anatop_regulator_init(void)
222{
Shawn Guoc0d78c22012-04-02 14:57:01 +0800223 return platform_driver_register(&anatop_regulator_driver);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800224}
225postcore_initcall(anatop_regulator_init);
226
227static void __exit anatop_regulator_exit(void)
228{
Shawn Guoc0d78c22012-04-02 14:57:01 +0800229 platform_driver_unregister(&anatop_regulator_driver);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800230}
231module_exit(anatop_regulator_exit);
232
233MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
234 "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
235MODULE_DESCRIPTION("ANATOP Regulator driver");
236MODULE_LICENSE("GPL v2");