blob: ce0fe72a428e811418aa8190c7f534ce1009d5a4 [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
Axel Lind01c3a12012-06-03 23:02:34 +080046static int anatop_set_voltage_sel(struct regulator_dev *reg, unsigned selector)
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080047{
48 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
Axel Lind01c3a12012-06-03 23:02:34 +080049 u32 val, mask;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080050
51 if (!anatop_reg->control_reg)
52 return -ENOTSUPP;
53
Axel Lind01c3a12012-06-03 23:02:34 +080054 val = anatop_reg->min_bit_val + selector;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080055 dev_dbg(&reg->dev, "%s: calculated val %d\n", __func__, val);
Richard Zhaob09530e2012-05-13 09:18:02 +080056 mask = ((1 << anatop_reg->vol_bit_width) - 1) <<
57 anatop_reg->vol_bit_shift;
58 val <<= anatop_reg->vol_bit_shift;
59 anatop_write_reg(anatop_reg->mfd, anatop_reg->control_reg, val, mask);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080060
61 return 0;
62}
63
64static int anatop_get_voltage_sel(struct regulator_dev *reg)
65{
66 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
Axel Lin3e2a9282012-07-17 00:10:42 +080067 u32 val, mask;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080068
69 if (!anatop_reg->control_reg)
70 return -ENOTSUPP;
71
Richard Zhaob09530e2012-05-13 09:18:02 +080072 val = anatop_read_reg(anatop_reg->mfd, anatop_reg->control_reg);
Axel Lin3e2a9282012-07-17 00:10:42 +080073 mask = ((1 << anatop_reg->vol_bit_width) - 1) <<
Richard Zhaob09530e2012-05-13 09:18:02 +080074 anatop_reg->vol_bit_shift;
Axel Lin3e2a9282012-07-17 00:10:42 +080075 val = (val & mask) >> anatop_reg->vol_bit_shift;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080076
77 return val - anatop_reg->min_bit_val;
78}
79
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080080static struct regulator_ops anatop_rops = {
Axel Lind01c3a12012-06-03 23:02:34 +080081 .set_voltage_sel = anatop_set_voltage_sel,
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080082 .get_voltage_sel = anatop_get_voltage_sel,
Axel Lind01c3a12012-06-03 23:02:34 +080083 .list_voltage = regulator_list_voltage_linear,
84 .map_voltage = regulator_map_voltage_linear,
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080085};
86
87static int __devinit anatop_regulator_probe(struct platform_device *pdev)
88{
89 struct device *dev = &pdev->dev;
90 struct device_node *np = dev->of_node;
91 struct regulator_desc *rdesc;
92 struct regulator_dev *rdev;
93 struct anatop_regulator *sreg;
94 struct regulator_init_data *initdata;
95 struct anatop *anatopmfd = dev_get_drvdata(pdev->dev.parent);
Axel Lind914d812012-04-10 22:45:01 +080096 struct regulator_config config = { };
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080097 int ret = 0;
98
99 initdata = of_get_regulator_init_data(dev, np);
100 sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
101 if (!sreg)
102 return -ENOMEM;
103 sreg->initdata = initdata;
104 sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL),
105 GFP_KERNEL);
106 rdesc = &sreg->rdesc;
107 memset(rdesc, 0, sizeof(*rdesc));
108 rdesc->name = sreg->name;
109 rdesc->ops = &anatop_rops;
110 rdesc->type = REGULATOR_VOLTAGE;
111 rdesc->owner = THIS_MODULE;
112 sreg->mfd = anatopmfd;
Ying-Chun Liu (PaulLiu)2f2cc272012-03-27 15:54:01 +0800113 ret = of_property_read_u32(np, "anatop-reg-offset",
114 &sreg->control_reg);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800115 if (ret) {
Ying-Chun Liu (PaulLiu)2f2cc272012-03-27 15:54:01 +0800116 dev_err(dev, "no anatop-reg-offset property set\n");
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800117 goto anatop_probe_end;
118 }
119 ret = of_property_read_u32(np, "anatop-vol-bit-width",
120 &sreg->vol_bit_width);
121 if (ret) {
122 dev_err(dev, "no anatop-vol-bit-width property set\n");
123 goto anatop_probe_end;
124 }
125 ret = of_property_read_u32(np, "anatop-vol-bit-shift",
126 &sreg->vol_bit_shift);
127 if (ret) {
128 dev_err(dev, "no anatop-vol-bit-shift property set\n");
129 goto anatop_probe_end;
130 }
131 ret = of_property_read_u32(np, "anatop-min-bit-val",
132 &sreg->min_bit_val);
133 if (ret) {
134 dev_err(dev, "no anatop-min-bit-val property set\n");
135 goto anatop_probe_end;
136 }
137 ret = of_property_read_u32(np, "anatop-min-voltage",
138 &sreg->min_voltage);
139 if (ret) {
140 dev_err(dev, "no anatop-min-voltage property set\n");
141 goto anatop_probe_end;
142 }
143 ret = of_property_read_u32(np, "anatop-max-voltage",
144 &sreg->max_voltage);
145 if (ret) {
146 dev_err(dev, "no anatop-max-voltage property set\n");
147 goto anatop_probe_end;
148 }
149
150 rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage)
151 / 25000 + 1;
Axel Lin0713e6a2012-05-14 11:06:44 +0800152 rdesc->min_uV = sreg->min_voltage;
153 rdesc->uV_step = 25000;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800154
Axel Lind914d812012-04-10 22:45:01 +0800155 config.dev = &pdev->dev;
156 config.init_data = initdata;
157 config.driver_data = sreg;
158 config.of_node = pdev->dev.of_node;
159
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800160 /* register regulator */
Axel Lind914d812012-04-10 22:45:01 +0800161 rdev = regulator_register(rdesc, &config);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800162 if (IS_ERR(rdev)) {
163 dev_err(dev, "failed to register %s\n",
164 rdesc->name);
165 ret = PTR_ERR(rdev);
166 goto anatop_probe_end;
167 }
168
169 platform_set_drvdata(pdev, rdev);
170
171anatop_probe_end:
172 if (ret)
173 kfree(sreg->name);
174
175 return ret;
176}
177
178static int __devexit anatop_regulator_remove(struct platform_device *pdev)
179{
180 struct regulator_dev *rdev = platform_get_drvdata(pdev);
181 struct anatop_regulator *sreg = rdev_get_drvdata(rdev);
182 const char *name = sreg->name;
183
184 regulator_unregister(rdev);
185 kfree(name);
186
187 return 0;
188}
189
190static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
191 { .compatible = "fsl,anatop-regulator", },
192 { /* end */ }
193};
194
Shawn Guoc0d78c22012-04-02 14:57:01 +0800195static struct platform_driver anatop_regulator_driver = {
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800196 .driver = {
197 .name = "anatop_regulator",
198 .owner = THIS_MODULE,
199 .of_match_table = of_anatop_regulator_match_tbl,
200 },
201 .probe = anatop_regulator_probe,
Axel Lin0c09d312012-05-23 09:08:08 +0800202 .remove = __devexit_p(anatop_regulator_remove),
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800203};
204
205static int __init anatop_regulator_init(void)
206{
Shawn Guoc0d78c22012-04-02 14:57:01 +0800207 return platform_driver_register(&anatop_regulator_driver);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800208}
209postcore_initcall(anatop_regulator_init);
210
211static void __exit anatop_regulator_exit(void)
212{
Shawn Guoc0d78c22012-04-02 14:57:01 +0800213 platform_driver_unregister(&anatop_regulator_driver);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800214}
215module_exit(anatop_regulator_exit);
216
217MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
218 "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
219MODULE_DESCRIPTION("ANATOP Regulator driver");
220MODULE_LICENSE("GPL v2");