blob: b78b622d24c5c1770ad5b73dd46594516d65c8f4 [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>
Dong Aishengbaa64152012-09-05 10:57:15 +080024#include <linux/mfd/syscon.h>
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080025#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>
Dong Aishengbaa64152012-09-05 10:57:15 +080030#include <linux/regmap.h>
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080031#include <linux/regulator/driver.h>
32#include <linux/regulator/of_regulator.h>
33
34struct anatop_regulator {
35 const char *name;
36 u32 control_reg;
Dong Aishengbaa64152012-09-05 10:57:15 +080037 struct regmap *anatop;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080038 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
Dong Aishengbaa64152012-09-05 10:57:15 +080047static int anatop_regmap_set_voltage_sel(struct regulator_dev *reg,
48 unsigned selector)
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080049{
50 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
Axel Lin985884d2012-12-09 08:05:45 +080051 u32 mask;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080052
53 if (!anatop_reg->control_reg)
54 return -ENOTSUPP;
55
Richard Zhaob09530e2012-05-13 09:18:02 +080056 mask = ((1 << anatop_reg->vol_bit_width) - 1) <<
57 anatop_reg->vol_bit_shift;
Axel Lin985884d2012-12-09 08:05:45 +080058 selector <<= anatop_reg->vol_bit_shift;
Dong Aishengbaa64152012-09-05 10:57:15 +080059 regmap_update_bits(anatop_reg->anatop, anatop_reg->control_reg,
Axel Lin985884d2012-12-09 08:05:45 +080060 mask, selector);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080061
62 return 0;
63}
64
Dong Aishengbaa64152012-09-05 10:57:15 +080065static int anatop_regmap_get_voltage_sel(struct regulator_dev *reg)
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080066{
67 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
Axel Lin3e2a9282012-07-17 00:10:42 +080068 u32 val, mask;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080069
70 if (!anatop_reg->control_reg)
71 return -ENOTSUPP;
72
Dong Aishengbaa64152012-09-05 10:57:15 +080073 regmap_read(anatop_reg->anatop, anatop_reg->control_reg, &val);
Axel Lin3e2a9282012-07-17 00:10:42 +080074 mask = ((1 << anatop_reg->vol_bit_width) - 1) <<
Richard Zhaob09530e2012-05-13 09:18:02 +080075 anatop_reg->vol_bit_shift;
Axel Lin3e2a9282012-07-17 00:10:42 +080076 val = (val & mask) >> anatop_reg->vol_bit_shift;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080077
Axel Lin985884d2012-12-09 08:05:45 +080078 return val;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080079}
80
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080081static struct regulator_ops anatop_rops = {
Dong Aishengbaa64152012-09-05 10:57:15 +080082 .set_voltage_sel = anatop_regmap_set_voltage_sel,
83 .get_voltage_sel = anatop_regmap_get_voltage_sel,
Axel Lind01c3a1e2012-06-03 23:02:34 +080084 .list_voltage = regulator_list_voltage_linear,
85 .map_voltage = regulator_map_voltage_linear,
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080086};
87
88static int __devinit anatop_regulator_probe(struct platform_device *pdev)
89{
90 struct device *dev = &pdev->dev;
91 struct device_node *np = dev->of_node;
Dong Aishengbaa64152012-09-05 10:57:15 +080092 struct device_node *anatop_np;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080093 struct regulator_desc *rdesc;
94 struct regulator_dev *rdev;
95 struct anatop_regulator *sreg;
96 struct regulator_init_data *initdata;
Axel Lind914d812012-04-10 22:45:01 +080097 struct regulator_config config = { };
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080098 int ret = 0;
99
100 initdata = of_get_regulator_init_data(dev, np);
101 sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
102 if (!sreg)
103 return -ENOMEM;
104 sreg->initdata = initdata;
105 sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL),
106 GFP_KERNEL);
107 rdesc = &sreg->rdesc;
108 memset(rdesc, 0, sizeof(*rdesc));
109 rdesc->name = sreg->name;
110 rdesc->ops = &anatop_rops;
111 rdesc->type = REGULATOR_VOLTAGE;
112 rdesc->owner = THIS_MODULE;
Dong Aishengbaa64152012-09-05 10:57:15 +0800113
114 anatop_np = of_get_parent(np);
115 if (!anatop_np)
116 return -ENODEV;
117 sreg->anatop = syscon_node_to_regmap(anatop_np);
118 of_node_put(anatop_np);
119 if (IS_ERR(sreg->anatop))
120 return PTR_ERR(sreg->anatop);
121
Ying-Chun Liu (PaulLiu)2f2cc272012-03-27 15:54:01 +0800122 ret = of_property_read_u32(np, "anatop-reg-offset",
123 &sreg->control_reg);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800124 if (ret) {
Ying-Chun Liu (PaulLiu)2f2cc272012-03-27 15:54:01 +0800125 dev_err(dev, "no anatop-reg-offset property set\n");
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800126 goto anatop_probe_end;
127 }
128 ret = of_property_read_u32(np, "anatop-vol-bit-width",
129 &sreg->vol_bit_width);
130 if (ret) {
131 dev_err(dev, "no anatop-vol-bit-width property set\n");
132 goto anatop_probe_end;
133 }
134 ret = of_property_read_u32(np, "anatop-vol-bit-shift",
135 &sreg->vol_bit_shift);
136 if (ret) {
137 dev_err(dev, "no anatop-vol-bit-shift property set\n");
138 goto anatop_probe_end;
139 }
140 ret = of_property_read_u32(np, "anatop-min-bit-val",
141 &sreg->min_bit_val);
142 if (ret) {
143 dev_err(dev, "no anatop-min-bit-val property set\n");
144 goto anatop_probe_end;
145 }
146 ret = of_property_read_u32(np, "anatop-min-voltage",
147 &sreg->min_voltage);
148 if (ret) {
149 dev_err(dev, "no anatop-min-voltage property set\n");
150 goto anatop_probe_end;
151 }
152 ret = of_property_read_u32(np, "anatop-max-voltage",
153 &sreg->max_voltage);
154 if (ret) {
155 dev_err(dev, "no anatop-max-voltage property set\n");
156 goto anatop_probe_end;
157 }
158
Axel Lin985884d2012-12-09 08:05:45 +0800159 rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage) / 25000 + 1
160 + sreg->min_bit_val;
Axel Lin0713e6a2012-05-14 11:06:44 +0800161 rdesc->min_uV = sreg->min_voltage;
162 rdesc->uV_step = 25000;
Axel Lin985884d2012-12-09 08:05:45 +0800163 rdesc->linear_min_sel = sreg->min_bit_val;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800164
Axel Lind914d812012-04-10 22:45:01 +0800165 config.dev = &pdev->dev;
166 config.init_data = initdata;
167 config.driver_data = sreg;
168 config.of_node = pdev->dev.of_node;
169
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800170 /* register regulator */
Axel Lind914d812012-04-10 22:45:01 +0800171 rdev = regulator_register(rdesc, &config);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800172 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
181anatop_probe_end:
182 if (ret)
183 kfree(sreg->name);
184
185 return ret;
186}
187
188static 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
200static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
201 { .compatible = "fsl,anatop-regulator", },
202 { /* end */ }
203};
204
Shawn Guoc0d78c22012-04-02 14:57:01 +0800205static struct platform_driver anatop_regulator_driver = {
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800206 .driver = {
207 .name = "anatop_regulator",
208 .owner = THIS_MODULE,
209 .of_match_table = of_anatop_regulator_match_tbl,
210 },
211 .probe = anatop_regulator_probe,
Axel Lin0c09d312012-05-23 09:08:08 +0800212 .remove = __devexit_p(anatop_regulator_remove),
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800213};
214
215static int __init anatop_regulator_init(void)
216{
Shawn Guoc0d78c22012-04-02 14:57:01 +0800217 return platform_driver_register(&anatop_regulator_driver);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800218}
219postcore_initcall(anatop_regulator_init);
220
221static void __exit anatop_regulator_exit(void)
222{
Shawn Guoc0d78c22012-04-02 14:57:01 +0800223 platform_driver_unregister(&anatop_regulator_driver);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800224}
225module_exit(anatop_regulator_exit);
226
227MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
228 "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
229MODULE_DESCRIPTION("ANATOP Regulator driver");
230MODULE_LICENSE("GPL v2");