regulator: axp20x: Fill regulators_node and of_match descriptor fields
[deliverable/linux.git] / drivers / regulator / axp20x-regulator.c
CommitLineData
dfe7a1b0
CC
1/*
2 * AXP20x regulators driver.
3 *
4 * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
5 *
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.
9 *
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.
14 */
15
16#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/of.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>
26
27#define AXP20X_IO_ENABLED 0x03
28#define AXP20X_IO_DISABLED 0x07
29
30#define AXP20X_WORKMODE_DCDC2_MASK BIT(2)
31#define AXP20X_WORKMODE_DCDC3_MASK BIT(1)
32
33#define AXP20X_FREQ_DCDC_MASK 0x0f
34
880fe82d
CYT
35#define AXP20X_DESC_IO(_id, _match, _supply, _min, _max, _step, _vreg, _vmask, \
36 _ereg, _emask, _enable_val, _disable_val) \
dfe7a1b0
CC
37 [AXP20X_##_id] = { \
38 .name = #_id, \
39 .supply_name = (_supply), \
880fe82d
CYT
40 .of_match = of_match_ptr(_match), \
41 .regulators_node = of_match_ptr("regulators"), \
dfe7a1b0
CC
42 .type = REGULATOR_VOLTAGE, \
43 .id = AXP20X_##_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), \
54 .ops = &axp20x_ops, \
55 }
56
880fe82d
CYT
57#define AXP20X_DESC(_id, _match, _supply, _min, _max, _step, _vreg, _vmask, \
58 _ereg, _emask) \
dfe7a1b0
CC
59 [AXP20X_##_id] = { \
60 .name = #_id, \
61 .supply_name = (_supply), \
880fe82d
CYT
62 .of_match = of_match_ptr(_match), \
63 .regulators_node = of_match_ptr("regulators"), \
dfe7a1b0
CC
64 .type = REGULATOR_VOLTAGE, \
65 .id = AXP20X_##_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), \
74 .ops = &axp20x_ops, \
75 }
76
880fe82d 77#define AXP20X_DESC_FIXED(_id, _match, _supply, _volt) \
dfe7a1b0
CC
78 [AXP20X_##_id] = { \
79 .name = #_id, \
80 .supply_name = (_supply), \
880fe82d
CYT
81 .of_match = of_match_ptr(_match), \
82 .regulators_node = of_match_ptr("regulators"), \
dfe7a1b0
CC
83 .type = REGULATOR_VOLTAGE, \
84 .id = AXP20X_##_id, \
85 .n_voltages = 1, \
86 .owner = THIS_MODULE, \
87 .min_uV = (_volt) * 1000, \
88 .ops = &axp20x_ops_fixed \
89 }
90
880fe82d
CYT
91#define AXP20X_DESC_TABLE(_id, _match, _supply, _table, _vreg, _vmask, _ereg, \
92 _emask) \
dfe7a1b0
CC
93 [AXP20X_##_id] = { \
94 .name = #_id, \
95 .supply_name = (_supply), \
880fe82d
CYT
96 .of_match = of_match_ptr(_match), \
97 .regulators_node = of_match_ptr("regulators"), \
dfe7a1b0
CC
98 .type = REGULATOR_VOLTAGE, \
99 .id = AXP20X_##_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, \
108 }
109
110static const int axp20x_ldo4_data[] = { 1250000, 1300000, 1400000, 1500000, 1600000,
111 1700000, 1800000, 1900000, 2000000, 2500000,
112 2700000, 2800000, 3000000, 3100000, 3200000,
113 3300000 };
114
115static struct regulator_ops axp20x_ops_fixed = {
116 .list_voltage = regulator_list_voltage_linear,
117};
118
119static 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,
b8870356 123 .map_voltage = regulator_map_voltage_ascend,
dfe7a1b0
CC
124 .enable = regulator_enable_regmap,
125 .disable = regulator_disable_regmap,
126 .is_enabled = regulator_is_enabled_regmap,
127};
128
129static 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,
136};
137
138static const struct regulator_desc axp20x_regulators[] = {
880fe82d
CYT
139 AXP20X_DESC(DCDC2, "dcdc2", "vin2", 700, 2275, 25, AXP20X_DCDC2_V_OUT,
140 0x3f, AXP20X_PWR_OUT_CTRL, 0x10),
141 AXP20X_DESC(DCDC3, "dcdc3", "vin3", 700, 3500, 25, AXP20X_DCDC3_V_OUT,
142 0x7f, AXP20X_PWR_OUT_CTRL, 0x02),
143 AXP20X_DESC_FIXED(LDO1, "ldo1", "acin", 1300),
144 AXP20X_DESC(LDO2, "ldo2", "ldo24in", 1800, 3300, 100,
145 AXP20X_LDO24_V_OUT, 0xf0, AXP20X_PWR_OUT_CTRL, 0x04),
146 AXP20X_DESC(LDO3, "ldo3", "ldo3in", 700, 3500, 25, AXP20X_LDO3_V_OUT,
147 0x7f, AXP20X_PWR_OUT_CTRL, 0x40),
148 AXP20X_DESC_TABLE(LDO4, "ldo4", "ldo24in", axp20x_ldo4_data,
149 AXP20X_LDO24_V_OUT, 0x0f, AXP20X_PWR_OUT_CTRL, 0x08),
150 AXP20X_DESC_IO(LDO5, "ldo5", "ldo5in", 1800, 3300, 100,
151 AXP20X_LDO5_V_OUT, 0xf0, AXP20X_GPIO0_CTRL, 0x07,
152 AXP20X_IO_ENABLED, AXP20X_IO_DISABLED),
dfe7a1b0
CC
153};
154
155#define AXP_MATCH(_name, _id) \
156 [AXP20X_##_id] = { \
157 .name = #_name, \
158 .driver_data = (void *) &axp20x_regulators[AXP20X_##_id], \
159 }
160
161static struct of_regulator_match axp20x_matches[] = {
162 AXP_MATCH(dcdc2, DCDC2),
163 AXP_MATCH(dcdc3, DCDC3),
164 AXP_MATCH(ldo1, LDO1),
165 AXP_MATCH(ldo2, LDO2),
166 AXP_MATCH(ldo3, LDO3),
167 AXP_MATCH(ldo4, LDO4),
168 AXP_MATCH(ldo5, LDO5),
169};
170
171static int axp20x_set_dcdc_freq(struct platform_device *pdev, u32 dcdcfreq)
172{
173 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
174
175 if (dcdcfreq < 750) {
176 dcdcfreq = 750;
177 dev_warn(&pdev->dev, "DCDC frequency too low. Set to 750kHz\n");
178 }
179
180 if (dcdcfreq > 1875) {
181 dcdcfreq = 1875;
182 dev_warn(&pdev->dev, "DCDC frequency too high. Set to 1875kHz\n");
183 }
184
185 dcdcfreq = (dcdcfreq - 750) / 75;
186
187 return regmap_update_bits(axp20x->regmap, AXP20X_DCDC_FREQ,
188 AXP20X_FREQ_DCDC_MASK, dcdcfreq);
189}
190
191static int axp20x_regulator_parse_dt(struct platform_device *pdev)
192{
193 struct device_node *np, *regulators;
194 int ret;
195 u32 dcdcfreq;
196
197 np = of_node_get(pdev->dev.parent->of_node);
198 if (!np)
199 return 0;
200
a6016c52 201 regulators = of_get_child_by_name(np, "regulators");
dfe7a1b0
CC
202 if (!regulators) {
203 dev_warn(&pdev->dev, "regulators node not found\n");
204 } else {
205 ret = of_regulator_match(&pdev->dev, regulators, axp20x_matches,
206 ARRAY_SIZE(axp20x_matches));
207 if (ret < 0) {
208 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n", ret);
209 return ret;
210 }
211
212 dcdcfreq = 1500;
213 of_property_read_u32(regulators, "x-powers,dcdc-freq", &dcdcfreq);
214 ret = axp20x_set_dcdc_freq(pdev, dcdcfreq);
215 if (ret < 0) {
216 dev_err(&pdev->dev, "Error setting dcdc frequency: %d\n", ret);
217 return ret;
218 }
219
220 of_node_put(regulators);
221 }
222
223 return 0;
224}
225
226static int axp20x_set_dcdc_workmode(struct regulator_dev *rdev, int id, u32 workmode)
227{
228 unsigned int mask = AXP20X_WORKMODE_DCDC2_MASK;
229
230 if ((id != AXP20X_DCDC2) && (id != AXP20X_DCDC3))
231 return -EINVAL;
232
233 if (id == AXP20X_DCDC3)
234 mask = AXP20X_WORKMODE_DCDC3_MASK;
235
236 workmode <<= ffs(mask) - 1;
237
238 return regmap_update_bits(rdev->regmap, AXP20X_DCDC_MODE, mask, workmode);
239}
240
241static int axp20x_regulator_probe(struct platform_device *pdev)
242{
243 struct regulator_dev *rdev;
244 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
245 struct regulator_config config = { };
246 struct regulator_init_data *init_data;
247 int ret, i;
248 u32 workmode;
249
250 ret = axp20x_regulator_parse_dt(pdev);
251 if (ret)
252 return ret;
253
254 for (i = 0; i < AXP20X_REG_ID_MAX; i++) {
255 init_data = axp20x_matches[i].init_data;
256
0d90ecf3 257 config.dev = pdev->dev.parent;
dfe7a1b0
CC
258 config.init_data = init_data;
259 config.regmap = axp20x->regmap;
260 config.of_node = axp20x_matches[i].of_node;
261
262 rdev = devm_regulator_register(&pdev->dev, &axp20x_regulators[i],
263 &config);
264 if (IS_ERR(rdev)) {
265 dev_err(&pdev->dev, "Failed to register %s\n",
266 axp20x_regulators[i].name);
267
268 return PTR_ERR(rdev);
269 }
270
271 ret = of_property_read_u32(axp20x_matches[i].of_node, "x-powers,dcdc-workmode",
272 &workmode);
273 if (!ret) {
274 if (axp20x_set_dcdc_workmode(rdev, i, workmode))
275 dev_err(&pdev->dev, "Failed to set workmode on %s\n",
276 axp20x_regulators[i].name);
277 }
278 }
279
280 return 0;
281}
282
283static struct platform_driver axp20x_regulator_driver = {
284 .probe = axp20x_regulator_probe,
285 .driver = {
286 .name = "axp20x-regulator",
dfe7a1b0
CC
287 },
288};
289
290module_platform_driver(axp20x_regulator_driver);
291
292MODULE_LICENSE("GPL v2");
293MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
294MODULE_DESCRIPTION("Regulator Driver for AXP20X PMIC");
This page took 0.080272 seconds and 5 git commands to generate.