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