2 * AXP20x regulators driver.
4 * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
6 * This file is subject to the terms and conditions of the GNU General
7 * Public License. See the file "COPYING" in the main directory of this
8 * archive for more details.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/mfd/axp20x.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/of_regulator.h>
27 #define AXP20X_IO_ENABLED 0x03
28 #define AXP20X_IO_DISABLED 0x07
30 #define AXP20X_WORKMODE_DCDC2_MASK BIT(2)
31 #define AXP20X_WORKMODE_DCDC3_MASK BIT(1)
33 #define AXP20X_FREQ_DCDC_MASK 0x0f
35 #define AXP_DESC_IO(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
36 _vmask, _ereg, _emask, _enable_val, _disable_val) \
37 [_family##_##_id] = { \
39 .supply_name = (_supply), \
40 .of_match = of_match_ptr(_match), \
41 .regulators_node = of_match_ptr("regulators"), \
42 .type = REGULATOR_VOLTAGE, \
43 .id = _family##_##_id, \
44 .n_voltages = (((_max) - (_min)) / (_step) + 1), \
45 .owner = THIS_MODULE, \
46 .min_uV = (_min) * 1000, \
47 .uV_step = (_step) * 1000, \
48 .vsel_reg = (_vreg), \
49 .vsel_mask = (_vmask), \
50 .enable_reg = (_ereg), \
51 .enable_mask = (_emask), \
52 .enable_val = (_enable_val), \
53 .disable_val = (_disable_val), \
57 #define AXP_DESC(_family, _id, _match, _supply, _min, _max, _step, _vreg, \
58 _vmask, _ereg, _emask) \
59 [_family##_##_id] = { \
61 .supply_name = (_supply), \
62 .of_match = of_match_ptr(_match), \
63 .regulators_node = of_match_ptr("regulators"), \
64 .type = REGULATOR_VOLTAGE, \
65 .id = _family##_##_id, \
66 .n_voltages = (((_max) - (_min)) / (_step) + 1), \
67 .owner = THIS_MODULE, \
68 .min_uV = (_min) * 1000, \
69 .uV_step = (_step) * 1000, \
70 .vsel_reg = (_vreg), \
71 .vsel_mask = (_vmask), \
72 .enable_reg = (_ereg), \
73 .enable_mask = (_emask), \
77 #define AXP_DESC_FIXED(_family, _id, _match, _supply, _volt) \
78 [_family##_##_id] = { \
80 .supply_name = (_supply), \
81 .of_match = of_match_ptr(_match), \
82 .regulators_node = of_match_ptr("regulators"), \
83 .type = REGULATOR_VOLTAGE, \
84 .id = _family##_##_id, \
86 .owner = THIS_MODULE, \
87 .min_uV = (_volt) * 1000, \
88 .ops = &axp20x_ops_fixed \
91 #define AXP_DESC_TABLE(_family, _id, _match, _supply, _table, _vreg, _vmask, \
93 [_family##_##_id] = { \
95 .supply_name = (_supply), \
96 .of_match = of_match_ptr(_match), \
97 .regulators_node = of_match_ptr("regulators"), \
98 .type = REGULATOR_VOLTAGE, \
99 .id = _family##_##_id, \
100 .n_voltages = ARRAY_SIZE(_table), \
101 .owner = THIS_MODULE, \
102 .vsel_reg = (_vreg), \
103 .vsel_mask = (_vmask), \
104 .enable_reg = (_ereg), \
105 .enable_mask = (_emask), \
106 .volt_table = (_table), \
107 .ops = &axp20x_ops_table, \
110 static const int axp20x_ldo4_data[] = { 1250000, 1300000, 1400000, 1500000, 1600000,
111 1700000, 1800000, 1900000, 2000000, 2500000,
112 2700000, 2800000, 3000000, 3100000, 3200000,
115 static struct regulator_ops axp20x_ops_fixed = {
116 .list_voltage = regulator_list_voltage_linear,
119 static struct regulator_ops axp20x_ops_table = {
120 .set_voltage_sel = regulator_set_voltage_sel_regmap,
121 .get_voltage_sel = regulator_get_voltage_sel_regmap,
122 .list_voltage = regulator_list_voltage_table,
123 .map_voltage = regulator_map_voltage_ascend,
124 .enable = regulator_enable_regmap,
125 .disable = regulator_disable_regmap,
126 .is_enabled = regulator_is_enabled_regmap,
129 static struct regulator_ops axp20x_ops = {
130 .set_voltage_sel = regulator_set_voltage_sel_regmap,
131 .get_voltage_sel = regulator_get_voltage_sel_regmap,
132 .list_voltage = regulator_list_voltage_linear,
133 .enable = regulator_enable_regmap,
134 .disable = regulator_disable_regmap,
135 .is_enabled = regulator_is_enabled_regmap,
138 static const struct regulator_desc axp20x_regulators[] = {
139 AXP_DESC(AXP20X, DCDC2, "dcdc2", "vin2", 700, 2275, 25,
140 AXP20X_DCDC2_V_OUT, 0x3f, AXP20X_PWR_OUT_CTRL, 0x10),
141 AXP_DESC(AXP20X, DCDC3, "dcdc3", "vin3", 700, 3500, 25,
142 AXP20X_DCDC3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x02),
143 AXP_DESC_FIXED(AXP20X, LDO1, "ldo1", "acin", 1300),
144 AXP_DESC(AXP20X, LDO2, "ldo2", "ldo24in", 1800, 3300, 100,
145 AXP20X_LDO24_V_OUT, 0xf0, AXP20X_PWR_OUT_CTRL, 0x04),
146 AXP_DESC(AXP20X, LDO3, "ldo3", "ldo3in", 700, 3500, 25,
147 AXP20X_LDO3_V_OUT, 0x7f, AXP20X_PWR_OUT_CTRL, 0x40),
148 AXP_DESC_TABLE(AXP20X, LDO4, "ldo4", "ldo24in", axp20x_ldo4_data,
149 AXP20X_LDO24_V_OUT, 0x0f, AXP20X_PWR_OUT_CTRL, 0x08),
150 AXP_DESC_IO(AXP20X, LDO5, "ldo5", "ldo5in", 1800, 3300, 100,
151 AXP20X_LDO5_V_OUT, 0xf0, AXP20X_GPIO0_CTRL, 0x07,
152 AXP20X_IO_ENABLED, AXP20X_IO_DISABLED),
155 static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
157 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
158 u32 min, max, def, step;
160 switch (axp20x->variant) {
170 "Setting DCDC frequency for unsupported AXP variant\n");
177 if (dcdcfreq < min) {
179 dev_warn(&pdev->dev, "DCDC frequency too low. Set to %ukHz\n",
183 if (dcdcfreq > max) {
185 dev_warn(&pdev->dev, "DCDC frequency too high. Set to %ukHz\n",
189 dcdcfreq = (dcdcfreq - min) / step;
191 return regmap_update_bits(axp20x->regmap, AXP20X_DCDC_FREQ,
192 AXP20X_FREQ_DCDC_MASK, dcdcfreq);
195 static int axp20x_regulator_parse_dt(struct platform_device *pdev)
197 struct device_node *np, *regulators;
201 np = of_node_get(pdev->dev.parent->of_node);
205 regulators = of_get_child_by_name(np, "regulators");
207 dev_warn(&pdev->dev, "regulators node not found\n");
209 of_property_read_u32(regulators, "x-powers,dcdc-freq", &dcdcfreq);
210 ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
212 dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
216 of_node_put(regulators);
222 static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
224 struct axp20x_dev *axp20x = rdev_get_drvdata(rdev);
227 switch (axp20x->variant) {
230 if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
233 mask = AXP20X_WORKMODE_DCDC2_MASK;
234 if (id == AXP20X_DCDC3)
235 mask = AXP20X_WORKMODE_DCDC3_MASK;
237 workmode <<= ffs(mask) - 1;
241 /* should not happen */
246 return regmap_update_bits(rdev->regmap, AXP20X_DCDC_MODE, mask, workmode);
249 static int axp20x_regulator_probe(struct platform_device *pdev)
251 struct regulator_dev *rdev;
252 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
253 const struct regulator_desc *regulators;
254 struct regulator_config config = {
255 .dev = pdev->dev.parent,
256 .regmap = axp20x->regmap,
257 .driver_data = axp20x,
259 int ret, i, nregulators;
262 switch (axp20x->variant) {
265 regulators = axp20x_regulators;
266 nregulators = AXP20X_REG_ID_MAX;
269 dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
274 /* This only sets the dcdc freq. Ignore any errors */
275 axp20x_regulator_parse_dt(pdev);
277 for (i = 0; i < nregulators; i++) {
278 rdev = devm_regulator_register(&pdev->dev, ®ulators[i],
281 dev_err(&pdev->dev, "Failed to register %s\n",
284 return PTR_ERR(rdev);
287 ret = of_property_read_u32(rdev->dev.of_node,
288 "x-powers,dcdc-workmode",
291 if (axp20x_set_dcdc_workmode(rdev, i, workmode))
292 dev_err(&pdev->dev, "Failed to set workmode on %s\n",
300 static struct platform_driver axp20x_regulator_driver = {
301 .probe = axp20x_regulator_probe,
303 .name = "axp20x-regulator",
307 module_platform_driver(axp20x_regulator_driver);
309 MODULE_LICENSE("GPL v2");
310 MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
311 MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");