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
6 AXP202 and AXP209 come with two synchronous step-down DC-DCs and five
7 LDOs. This patch introduces basic support for those regulators.
9 Signed-off-by: Carlo Caione <carlo@caione.org>
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
18 --- a/arch/arm/configs/sunxi_defconfig
19 +++ b/arch/arm/configs/sunxi_defconfig
20 @@ -72,3 +72,4 @@ CONFIG_NFS_FS=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.
31 +config REGULATOR_AXP20X
32 + tristate "X-POWERS AXP20X PMIC Regulators"
33 + depends on MFD_AXP20X
35 + This driver provides support for the voltage regulators on the
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
52 +++ b/drivers/regulator/axp20x-regulator.c
55 + * axp20x regulators driver.
57 + * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
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.
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.
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>
80 +#define AXP20X_IO_ENABLED (0x03)
82 +#define AXP20X_WORKMODE_DCDC2_MASK BIT(2)
83 +#define AXP20X_WORKMODE_DCDC3_MASK BIT(1)
85 +#define AXP20X_FREQ_DCDC_MASK (0x0f)
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;
93 +#define AXP20X_DESC(_id, _min, _max, _step, _vreg, _vmask, _ereg, _emask) \
94 + [AXP20X_##_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, \
109 +#define AXP20X_DESC_IO(_id, _min, _max, _step, _vreg, _vmask, _ereg, _emask) \
110 + [AXP20X_##_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, \
125 +#define AXP20X_DESC_FIXED(_id, _volt) \
126 + [AXP20X_##_id] = { \
128 + .type = REGULATOR_VOLTAGE, \
129 + .id = AXP20X_##_id, \
131 + .owner = THIS_MODULE, \
132 + .min_uV = (_volt) * 1000, \
133 + .ops = &axp20x_ops, \
136 +#define AXP20X_DESC_TABLE(_id, _table, _vreg, _vmask, _ereg, _emask) \
137 + [AXP20X_##_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, \
151 +static int axp20x_ldo4_data[] = { 1250000, 1300000, 1400000, 1500000, 1600000, 1700000,
152 + 1800000, 1900000, 2000000, 2500000, 2700000, 2800000,
153 + 3000000, 3100000, 3200000, 3300000 };
155 +static int axp20x_set_suspend_voltage(struct regulator_dev *rdev, int uV)
157 + return regulator_set_voltage_sel_regmap(rdev, 0);
160 +static int axp20x_io_enable_regmap(struct regulator_dev *rdev)
162 + return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
163 + rdev->desc->enable_mask, AXP20X_IO_ENABLED);
166 +static int axp109_io_is_enabled_regmap(struct regulator_dev *rdev)
171 + ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
175 + val &= rdev->desc->enable_mask;
176 + return (val == AXP20X_IO_ENABLED);
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,
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,
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,
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),
232 +#define AXP_MATCH(_name, _id) \
233 + [AXP20X_##_id] = { \
235 + .driver_data = (void *) &axp20x_regulators[AXP20X_##_id], \
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),
248 +static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
250 + struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
252 + if (dcdcfreq < 750)
255 + if (dcdcfreq > 1875)
258 + dcdcfreq = (dcdcfreq - 750) / 75;
260 + return regmap_update_bits(axp20x->regmap, AXP20X_DCDC_FREQ,
261 + AXP20X_FREQ_DCDC_MASK, dcdcfreq);
264 +static int axp20x_regulator_parse_dt(struct platform_device *pdev)
266 + struct device_node *np, *regulators;
270 + np = of_node_get(pdev->dev.parent->of_node);
274 + regulators = of_find_node_by_name(np, "regulators");
276 + dev_err(&pdev->dev, "regulators node not found\n");
280 + ret = of_regulator_match(&pdev->dev, regulators, axp20x_matches,
281 + ARRAY_SIZE(axp20x_matches));
283 + dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
289 + of_property_read_u32(regulators, "dcdc-freq", &dcdcfreq);
290 + ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
292 + dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
296 + of_node_put(regulators);
301 +static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
303 + unsigned int mask = AXP20X_WORKMODE_DCDC2_MASK;
305 + if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
308 + if (id == AXP20X_DCDC3)
309 + mask = AXP20X_WORKMODE_DCDC3_MASK;
311 + return regmap_update_bits(rdev->regmap, AXP20X_DCDC_MODE, mask, workmode);
314 +static int axp20x_regulator_probe(struct platform_device *pdev)
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;
323 + ret = axp20x_regulator_parse_dt(pdev);
327 + pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
329 + dev_err(&pdev->dev, "Failed to alloc pmic\n");
333 + pmic->axp20x = axp20x;
334 + memcpy(pmic->rdesc, axp20x_regulators, sizeof(pmic->rdesc));
335 + platform_set_drvdata(pdev, pmic);
337 + for (i = 0; i < AXP20X_REG_ID_MAX; i++) {
338 + init_data = axp20x_matches[i].init_data;
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;
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);
355 + regulator_unregister(pmic->rdev[i]);
360 + ret = of_property_read_u32(axp20x_matches[i].of_node, "dcdc-workmode",
363 + ret = axp20x_set_dcdc_workmode(pmic->rdev[i], i, workmode);
365 + dev_err(&pdev->dev, "Failed to set workmode on %s\n",
366 + pmic->rdesc[i].name);
373 +static int axp20x_regulator_remove(struct platform_device *pdev)
375 + struct axp20x_regulators *pmic = platform_get_drvdata(pdev);
378 + for (i = 0; i < AXP20X_REG_ID_MAX; i++)
379 + regulator_unregister(pmic->rdev[i]);
384 +static struct platform_driver axp20x_regulator_driver = {
385 + .probe = axp20x_regulator_probe,
386 + .remove = axp20x_regulator_remove,
388 + .name = "axp20x-regulator",
389 + .owner = THIS_MODULE,
393 +static int __init axp20x_regulator_init(void)
395 + return platform_driver_register(&axp20x_regulator_driver);
398 +subsys_initcall(axp20x_regulator_init);
400 +MODULE_LICENSE("GPL v2");
401 +MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
402 +MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");