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