sunxi: initial 3.14 patchset
[openwrt/svn-archive/archive.git] / target / linux / sunxi / patches-3.14 / 212-regulator-add-axp20x-regulator-support.patch
1 From c3af279a9031b3375d3e5a684619c1adbbe30da9 Mon Sep 17 00:00:00 2001
2 From: Carlo Caione <carlo@caione.org>
3 Date: Sat, 1 Mar 2014 17:45:51 +0100
4 Subject: [PATCH] regulator: AXP20x: Add support for regulators subsystem
5
6 AXP202 and AXP209 come with two synchronous step-down DC-DCs and five
7 LDOs. This patch introduces basic support for those regulators.
8
9 Signed-off-by: Carlo Caione <carlo@caione.org>
10 ---
11 arch/arm/configs/sunxi_defconfig | 1 +
12 drivers/regulator/Kconfig | 7 +
13 drivers/regulator/Makefile | 1 +
14 drivers/regulator/axp20x-regulator.c | 349 +++++++++++++++++++++++++++++++++++
15 4 files changed, 358 insertions(+)
16 create mode 100644 drivers/regulator/axp20x-regulator.c
17
18 diff --git a/arch/arm/configs/sunxi_defconfig b/arch/arm/configs/sunxi_defconfig
19 index 3ed7023..6e305da 100644
20 --- a/arch/arm/configs/sunxi_defconfig
21 +++ b/arch/arm/configs/sunxi_defconfig
22 @@ -72,3 +72,4 @@ CONFIG_NFS_FS=y
23 CONFIG_ROOT_NFS=y
24 CONFIG_NLS=y
25 CONFIG_PRINTK_TIME=y
26 +CONFIG_REGULATOR_AXP20X=y
27 diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
28 index 6a79328..9f3bc48 100644
29 --- a/drivers/regulator/Kconfig
30 +++ b/drivers/regulator/Kconfig
31 @@ -139,6 +139,13 @@ config REGULATOR_AS3722
32 AS3722 PMIC. This will enable support for all the software
33 controllable DCDC/LDO regulators.
34
35 +config REGULATOR_AXP20X
36 + tristate "X-POWERS AXP20X PMIC Regulators"
37 + depends on MFD_AXP20X
38 + help
39 + This driver provides support for the voltage regulators on the
40 + AXP20X PMIC.
41 +
42 config REGULATOR_DA903X
43 tristate "Dialog Semiconductor DA9030/DA9034 regulators"
44 depends on PMIC_DA903X
45 diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile
46 index 979f9dd..1dd084a 100644
47 --- a/drivers/regulator/Makefile
48 +++ b/drivers/regulator/Makefile
49 @@ -20,6 +20,7 @@ obj-$(CONFIG_REGULATOR_ANATOP) += anatop-regulator.o
50 obj-$(CONFIG_REGULATOR_ARIZONA) += arizona-micsupp.o arizona-ldo1.o
51 obj-$(CONFIG_REGULATOR_AS3711) += as3711-regulator.o
52 obj-$(CONFIG_REGULATOR_AS3722) += as3722-regulator.o
53 +obj-$(CONFIG_REGULATOR_AXP20X) += axp20x-regulator.o
54 obj-$(CONFIG_REGULATOR_DA903X) += da903x.o
55 obj-$(CONFIG_REGULATOR_DA9052) += da9052-regulator.o
56 obj-$(CONFIG_REGULATOR_DA9055) += da9055-regulator.o
57 diff --git a/drivers/regulator/axp20x-regulator.c b/drivers/regulator/axp20x-regulator.c
58 new file mode 100644
59 index 0000000..9072f2f
60 --- /dev/null
61 +++ b/drivers/regulator/axp20x-regulator.c
62 @@ -0,0 +1,349 @@
63 +/*
64 + * axp20x regulators driver.
65 + *
66 + * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
67 + *
68 + * This file is subject to the terms and conditions of the GNU General
69 + * Public License. See the file "COPYING" in the main directory of this
70 + * archive for more details.
71 + *
72 + * This program is distributed in the hope that it will be useful,
73 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
74 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75 + * GNU General Public License for more details.
76 + */
77 +
78 +#include <linux/module.h>
79 +#include <linux/init.h>
80 +#include <linux/err.h>
81 +#include <linux/platform_device.h>
82 +#include <linux/of.h>
83 +#include <linux/of_device.h>
84 +#include <linux/regulator/driver.h>
85 +#include <linux/regulator/of_regulator.h>
86 +#include <linux/mfd/axp20x.h>
87 +#include <linux/regmap.h>
88 +
89 +#define AXP20X_IO_ENABLED (0x03)
90 +
91 +#define AXP20X_WORKMODE_DCDC2_MASK BIT(2)
92 +#define AXP20X_WORKMODE_DCDC3_MASK BIT(1)
93 +
94 +#define AXP20X_FREQ_DCDC_MASK (0x0f)
95 +
96 +struct axp20x_regulators {
97 + struct regulator_desc rdesc[AXP20X_REG_ID_MAX];
98 + struct regulator_dev *rdev[AXP20X_REG_ID_MAX];
99 + struct axp20x_dev *axp20x;
100 +};
101 +
102 +#define AXP20X_DESC(_id, _min, _max, _step, _vreg, _vmask, _ereg, _emask) \
103 + [AXP20X_##_id] = { \
104 + .name = #_id, \
105 + .type = REGULATOR_VOLTAGE, \
106 + .id = AXP20X_##_id, \
107 + .n_voltages = (((_max) - (_min)) / (_step) + 1), \
108 + .owner = THIS_MODULE, \
109 + .min_uV = (_min) * 1000, \
110 + .uV_step = (_step) * 1000, \
111 + .vsel_reg = (_vreg), \
112 + .vsel_mask = (_vmask), \
113 + .enable_reg = (_ereg), \
114 + .enable_mask = (_emask), \
115 + .ops = &axp20x_ops, \
116 + }
117 +
118 +#define AXP20X_DESC_IO(_id, _min, _max, _step, _vreg, _vmask, _ereg, _emask) \
119 + [AXP20X_##_id] = { \
120 + .name = #_id, \
121 + .type = REGULATOR_VOLTAGE, \
122 + .id = AXP20X_##_id, \
123 + .n_voltages = (((_max) - (_min)) / (_step) + 1), \
124 + .owner = THIS_MODULE, \
125 + .min_uV = (_min) * 1000, \
126 + .uV_step = (_step) * 1000, \
127 + .vsel_reg = (_vreg), \
128 + .vsel_mask = (_vmask), \
129 + .enable_reg = (_ereg), \
130 + .enable_mask = (_emask), \
131 + .ops = &axp20x_ops_io, \
132 + }
133 +
134 +#define AXP20X_DESC_FIXED(_id, _volt) \
135 + [AXP20X_##_id] = { \
136 + .name = #_id, \
137 + .type = REGULATOR_VOLTAGE, \
138 + .id = AXP20X_##_id, \
139 + .n_voltages = 1, \
140 + .owner = THIS_MODULE, \
141 + .min_uV = (_volt) * 1000, \
142 + .ops = &axp20x_ops, \
143 + }
144 +
145 +#define AXP20X_DESC_TABLE(_id, _table, _vreg, _vmask, _ereg, _emask) \
146 + [AXP20X_##_id] = { \
147 + .name = #_id, \
148 + .type = REGULATOR_VOLTAGE, \
149 + .id = AXP20X_##_id, \
150 + .n_voltages = ARRAY_SIZE(_table), \
151 + .owner = THIS_MODULE, \
152 + .vsel_reg = (_vreg), \
153 + .vsel_mask = (_vmask), \
154 + .enable_reg = (_ereg), \
155 + .enable_mask = (_emask), \
156 + .volt_table = (_table), \
157 + .ops = &axp20x_ops_table, \
158 + }
159 +
160 +static int axp20x_ldo4_data[] = { 1250000, 1300000, 1400000, 1500000, 1600000, 1700000,
161 + 1800000, 1900000, 2000000, 2500000, 2700000, 2800000,
162 + 3000000, 3100000, 3200000, 3300000 };
163 +
164 +static int axp20x_set_suspend_voltage(struct regulator_dev *rdev, int uV)
165 +{
166 + return regulator_set_voltage_sel_regmap(rdev, 0);
167 +}
168 +
169 +static int axp20x_io_enable_regmap(struct regulator_dev *rdev)
170 +{
171 + return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
172 + rdev->desc->enable_mask, AXP20X_IO_ENABLED);
173 +}
174 +
175 +static int axp109_io_is_enabled_regmap(struct regulator_dev *rdev)
176 +{
177 + unsigned int val;
178 + int ret;
179 +
180 + ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
181 + if (ret != 0)
182 + return ret;
183 +
184 + val &= rdev->desc->enable_mask;
185 + return (val == AXP20X_IO_ENABLED);
186 +}
187 +
188 +static struct regulator_ops axp20x_ops_table = {
189 + .set_voltage_sel = regulator_set_voltage_sel_regmap,
190 + .get_voltage_sel = regulator_get_voltage_sel_regmap,
191 + .list_voltage = regulator_list_voltage_table,
192 + .enable = regulator_enable_regmap,
193 + .disable = regulator_disable_regmap,
194 + .is_enabled = regulator_is_enabled_regmap,
195 + .set_suspend_enable = regulator_enable_regmap,
196 + .set_suspend_disable = regulator_disable_regmap,
197 + .set_suspend_voltage = axp20x_set_suspend_voltage,
198 +};
199 +
200 +
201 +static struct regulator_ops axp20x_ops = {
202 + .set_voltage_sel = regulator_set_voltage_sel_regmap,
203 + .get_voltage_sel = regulator_get_voltage_sel_regmap,
204 + .list_voltage = regulator_list_voltage_linear,
205 + .enable = regulator_enable_regmap,
206 + .disable = regulator_disable_regmap,
207 + .is_enabled = regulator_is_enabled_regmap,
208 + .set_suspend_enable = regulator_enable_regmap,
209 + .set_suspend_disable = regulator_disable_regmap,
210 + .set_suspend_voltage = axp20x_set_suspend_voltage,
211 +};
212 +
213 +static struct regulator_ops axp20x_ops_io = {
214 + .set_voltage_sel = regulator_set_voltage_sel_regmap,
215 + .get_voltage_sel = regulator_get_voltage_sel_regmap,
216 + .list_voltage = regulator_list_voltage_linear,
217 + .enable = axp20x_io_enable_regmap,
218 + .disable = regulator_disable_regmap,
219 + .is_enabled = axp109_io_is_enabled_regmap,
220 + .set_suspend_enable = regulator_enable_regmap,
221 + .set_suspend_disable = regulator_disable_regmap,
222 + .set_suspend_voltage = axp20x_set_suspend_voltage,
223 +};
224 +
225 +static struct regulator_desc axp20x_regulators[] = {
226 + AXP20X_DESC(DCDC2, 700, 2275, 25, AXP20X_DCDC2_V_OUT, 0x3f,
227 + AXP20X_PWR_OUT_CTRL, 0x10),
228 + AXP20X_DESC(DCDC3, 700, 3500, 25, AXP20X_DCDC3_V_OUT, 0x7f,
229 + AXP20X_PWR_OUT_CTRL, 0x02),
230 + AXP20X_DESC_FIXED(LDO1, 1300),
231 + AXP20X_DESC(LDO2, 1800, 3300, 100, AXP20X_LDO24_V_OUT, 0xf0,
232 + AXP20X_PWR_OUT_CTRL, 0x04),
233 + AXP20X_DESC(LDO3, 700, 3500, 25, AXP20X_LDO3_V_OUT, 0x7f,
234 + AXP20X_PWR_OUT_CTRL, 0x40),
235 + AXP20X_DESC_TABLE(LDO4, axp20x_ldo4_data, AXP20X_LDO24_V_OUT, 0x0f,
236 + AXP20X_PWR_OUT_CTRL, 0x08),
237 + AXP20X_DESC_IO(LDO5, 1800, 3300, 100, AXP20X_LDO5_V_OUT, 0xf0,
238 + AXP20X_GPIO0_CTRL, 0x07),
239 +};
240 +
241 +#define AXP_MATCH(_name, _id) \
242 + [AXP20X_##_id] = { \
243 + .name = #_name, \
244 + .driver_data = (void *) &axp20x_regulators[AXP20X_##_id], \
245 + }
246 +
247 +static struct of_regulator_match axp20x_matches[] = {
248 + AXP_MATCH(dcdc2, DCDC2),
249 + AXP_MATCH(dcdc3, DCDC3),
250 + AXP_MATCH(ldo1, LDO1),
251 + AXP_MATCH(ldo2, LDO2),
252 + AXP_MATCH(ldo3, LDO3),
253 + AXP_MATCH(ldo4, LDO4),
254 + AXP_MATCH(ldo5, LDO5),
255 +};
256 +
257 +static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
258 +{
259 + struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
260 +
261 + if (dcdcfreq < 750)
262 + dcdcfreq = 750;
263 +
264 + if (dcdcfreq > 1875)
265 + dcdcfreq = 1875;
266 +
267 + dcdcfreq = (dcdcfreq - 750) / 75;
268 +
269 + return regmap_update_bits(axp20x->regmap, AXP20X_DCDC_FREQ,
270 + AXP20X_FREQ_DCDC_MASK, dcdcfreq);
271 +}
272 +
273 +static int axp20x_regulator_parse_dt(struct platform_device *pdev)
274 +{
275 + struct device_node *np, *regulators;
276 + int ret;
277 + u32 dcdcfreq;
278 +
279 + np = of_node_get(pdev->dev.parent->of_node);
280 + if (!np)
281 + return 0;
282 +
283 + regulators = of_find_node_by_name(np, "regulators");
284 + if (!regulators) {
285 + dev_err(&pdev->dev, "regulators node not found\n");
286 + return -EINVAL;
287 + }
288 +
289 + ret = of_regulator_match(&pdev->dev, regulators, axp20x_matches,
290 + ARRAY_SIZE(axp20x_matches));
291 + if (ret < 0) {
292 + dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
293 + ret);
294 + return ret;
295 + }
296 +
297 + dcdcfreq = 0x08;
298 + of_property_read_u32(regulators, "dcdc-freq", &dcdcfreq);
299 + ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
300 + if (ret < 0) {
301 + dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
302 + return ret;
303 + }
304 +
305 + of_node_put(regulators);
306 +
307 + return 0;
308 +}
309 +
310 +static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
311 +{
312 + unsigned int mask = AXP20X_WORKMODE_DCDC2_MASK;
313 +
314 + if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
315 + return -EINVAL;
316 +
317 + if (id == AXP20X_DCDC3)
318 + mask = AXP20X_WORKMODE_DCDC3_MASK;
319 +
320 + return regmap_update_bits(rdev->regmap, AXP20X_DCDC_MODE, mask, workmode);
321 +}
322 +
323 +static int axp20x_regulator_probe(struct platform_device *pdev)
324 +{
325 + struct axp20x_regulators *pmic;
326 + struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
327 + struct regulator_config config = { };
328 + struct regulator_init_data *init_data;
329 + int ret, i;
330 + u32 workmode;
331 +
332 + ret = axp20x_regulator_parse_dt(pdev);
333 + if (ret)
334 + return ret;
335 +
336 + pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
337 + if (!pmic) {
338 + dev_err(&pdev->dev, "Failed to alloc pmic\n");
339 + return -ENOMEM;
340 + }
341 +
342 + pmic->axp20x = axp20x;
343 + memcpy(pmic->rdesc, axp20x_regulators, sizeof(pmic->rdesc));
344 + platform_set_drvdata(pdev, pmic);
345 +
346 + for (i = 0; i < AXP20X_REG_ID_MAX; i++) {
347 + init_data = axp20x_matches[i].init_data;
348 + if (!init_data)
349 + continue;
350 +
351 + config.dev = &pdev->dev;
352 + config.init_data = init_data;
353 + config.driver_data = pmic;
354 + config.regmap = axp20x->regmap;
355 + config.of_node = axp20x_matches[i].of_node;
356 +
357 + pmic->rdev[i] = regulator_register(&pmic->rdesc[i], &config);
358 + if (IS_ERR(pmic->rdev[i])) {
359 + ret = PTR_ERR(pmic->rdev[i]);
360 + dev_err(&pdev->dev, "Failed to register %s\n",
361 + pmic->rdesc[i].name);
362 +
363 + while (--i >= 0)
364 + regulator_unregister(pmic->rdev[i]);
365 +
366 + return ret;
367 + }
368 +
369 + ret = of_property_read_u32(axp20x_matches[i].of_node, "dcdc-workmode",
370 + &workmode);
371 + if (!ret) {
372 + ret = axp20x_set_dcdc_workmode(pmic->rdev[i], i, workmode);
373 + if (ret)
374 + dev_err(&pdev->dev, "Failed to set workmode on %s\n",
375 + pmic->rdesc[i].name);
376 + }
377 + }
378 +
379 + return 0;
380 +}
381 +
382 +static int axp20x_regulator_remove(struct platform_device *pdev)
383 +{
384 + struct axp20x_regulators *pmic = platform_get_drvdata(pdev);
385 + int i;
386 +
387 + for (i = 0; i < AXP20X_REG_ID_MAX; i++)
388 + regulator_unregister(pmic->rdev[i]);
389 +
390 + return 0;
391 +}
392 +
393 +static struct platform_driver axp20x_regulator_driver = {
394 + .probe = axp20x_regulator_probe,
395 + .remove = axp20x_regulator_remove,
396 + .driver = {
397 + .name = "axp20x-regulator",
398 + .owner = THIS_MODULE,
399 + },
400 +};
401 +
402 +static int __init axp20x_regulator_init(void)
403 +{
404 + return platform_driver_register(&axp20x_regulator_driver);
405 +}
406 +
407 +subsys_initcall(axp20x_regulator_init);
408 +
409 +MODULE_LICENSE("GPL v2");
410 +MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
411 +MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");
412 --
413 2.0.3
414