This source file includes following definitions.
- axp20x_ac_power_irq
- axp20x_ac_power_get_property
- axp813_ac_power_set_property
- axp813_ac_power_prop_writeable
- axp20x_ac_power_probe
1
2
3
4
5
6
7
8
9 #include <linux/device.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/mfd/axp20x.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/power_supply.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <linux/iio/consumer.h>
22
23 #define AXP20X_PWR_STATUS_ACIN_PRESENT BIT(7)
24 #define AXP20X_PWR_STATUS_ACIN_AVAIL BIT(6)
25
26 #define AXP813_ACIN_PATH_SEL BIT(7)
27
28 #define AXP813_VHOLD_MASK GENMASK(5, 3)
29 #define AXP813_VHOLD_UV_TO_BIT(x) ((((x) / 100000) - 40) << 3)
30 #define AXP813_VHOLD_REG_TO_UV(x) \
31 (((((x) & AXP813_VHOLD_MASK) >> 3) + 40) * 100000)
32
33 #define AXP813_CURR_LIMIT_MASK GENMASK(2, 0)
34 #define AXP813_CURR_LIMIT_UA_TO_BIT(x) (((x) / 500000) - 3)
35 #define AXP813_CURR_LIMIT_REG_TO_UA(x) \
36 ((((x) & AXP813_CURR_LIMIT_MASK) + 3) * 500000)
37
38 #define DRVNAME "axp20x-ac-power-supply"
39
40 struct axp20x_ac_power {
41 struct regmap *regmap;
42 struct power_supply *supply;
43 struct iio_channel *acin_v;
44 struct iio_channel *acin_i;
45 bool has_acin_path_sel;
46 };
47
48 static irqreturn_t axp20x_ac_power_irq(int irq, void *devid)
49 {
50 struct axp20x_ac_power *power = devid;
51
52 power_supply_changed(power->supply);
53
54 return IRQ_HANDLED;
55 }
56
57 static int axp20x_ac_power_get_property(struct power_supply *psy,
58 enum power_supply_property psp,
59 union power_supply_propval *val)
60 {
61 struct axp20x_ac_power *power = power_supply_get_drvdata(psy);
62 int ret, reg;
63
64 switch (psp) {
65 case POWER_SUPPLY_PROP_HEALTH:
66 ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, ®);
67 if (ret)
68 return ret;
69
70 if (reg & AXP20X_PWR_STATUS_ACIN_PRESENT) {
71 val->intval = POWER_SUPPLY_HEALTH_GOOD;
72 return 0;
73 }
74
75 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
76 return 0;
77
78 case POWER_SUPPLY_PROP_PRESENT:
79 ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, ®);
80 if (ret)
81 return ret;
82
83 val->intval = !!(reg & AXP20X_PWR_STATUS_ACIN_PRESENT);
84 return 0;
85
86 case POWER_SUPPLY_PROP_ONLINE:
87 ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, ®);
88 if (ret)
89 return ret;
90
91 val->intval = !!(reg & AXP20X_PWR_STATUS_ACIN_AVAIL);
92
93
94 if (val->intval && power->has_acin_path_sel) {
95 ret = regmap_read(power->regmap, AXP813_ACIN_PATH_CTRL,
96 ®);
97 if (ret)
98 return ret;
99
100 val->intval = !!(reg & AXP813_ACIN_PATH_SEL);
101 }
102
103 return 0;
104
105 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
106 ret = iio_read_channel_processed(power->acin_v, &val->intval);
107 if (ret)
108 return ret;
109
110
111 val->intval *= 1000;
112
113 return 0;
114
115 case POWER_SUPPLY_PROP_CURRENT_NOW:
116 ret = iio_read_channel_processed(power->acin_i, &val->intval);
117 if (ret)
118 return ret;
119
120
121 val->intval *= 1000;
122
123 return 0;
124
125 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
126 ret = regmap_read(power->regmap, AXP813_ACIN_PATH_CTRL, ®);
127 if (ret)
128 return ret;
129
130 val->intval = AXP813_VHOLD_REG_TO_UV(reg);
131
132 return 0;
133
134 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
135 ret = regmap_read(power->regmap, AXP813_ACIN_PATH_CTRL, ®);
136 if (ret)
137 return ret;
138
139 val->intval = AXP813_CURR_LIMIT_REG_TO_UA(reg);
140
141 if (val->intval > 4000000)
142 val->intval = 4000000;
143
144 return 0;
145
146 default:
147 return -EINVAL;
148 }
149
150 return -EINVAL;
151 }
152
153 static int axp813_ac_power_set_property(struct power_supply *psy,
154 enum power_supply_property psp,
155 const union power_supply_propval *val)
156 {
157 struct axp20x_ac_power *power = power_supply_get_drvdata(psy);
158
159 switch (psp) {
160 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
161 if (val->intval < 4000000 || val->intval > 4700000)
162 return -EINVAL;
163
164 return regmap_update_bits(power->regmap, AXP813_ACIN_PATH_CTRL,
165 AXP813_VHOLD_MASK,
166 AXP813_VHOLD_UV_TO_BIT(val->intval));
167
168 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
169 if (val->intval < 1500000 || val->intval > 4000000)
170 return -EINVAL;
171
172 return regmap_update_bits(power->regmap, AXP813_ACIN_PATH_CTRL,
173 AXP813_CURR_LIMIT_MASK,
174 AXP813_CURR_LIMIT_UA_TO_BIT(val->intval));
175
176 default:
177 return -EINVAL;
178 }
179
180 return -EINVAL;
181 }
182
183 static int axp813_ac_power_prop_writeable(struct power_supply *psy,
184 enum power_supply_property psp)
185 {
186 return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
187 psp == POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
188 }
189
190 static enum power_supply_property axp20x_ac_power_properties[] = {
191 POWER_SUPPLY_PROP_HEALTH,
192 POWER_SUPPLY_PROP_PRESENT,
193 POWER_SUPPLY_PROP_ONLINE,
194 POWER_SUPPLY_PROP_VOLTAGE_NOW,
195 POWER_SUPPLY_PROP_CURRENT_NOW,
196 };
197
198 static enum power_supply_property axp22x_ac_power_properties[] = {
199 POWER_SUPPLY_PROP_HEALTH,
200 POWER_SUPPLY_PROP_PRESENT,
201 POWER_SUPPLY_PROP_ONLINE,
202 };
203
204 static enum power_supply_property axp813_ac_power_properties[] = {
205 POWER_SUPPLY_PROP_HEALTH,
206 POWER_SUPPLY_PROP_PRESENT,
207 POWER_SUPPLY_PROP_ONLINE,
208 POWER_SUPPLY_PROP_VOLTAGE_MIN,
209 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
210 };
211
212 static const struct power_supply_desc axp20x_ac_power_desc = {
213 .name = "axp20x-ac",
214 .type = POWER_SUPPLY_TYPE_MAINS,
215 .properties = axp20x_ac_power_properties,
216 .num_properties = ARRAY_SIZE(axp20x_ac_power_properties),
217 .get_property = axp20x_ac_power_get_property,
218 };
219
220 static const struct power_supply_desc axp22x_ac_power_desc = {
221 .name = "axp22x-ac",
222 .type = POWER_SUPPLY_TYPE_MAINS,
223 .properties = axp22x_ac_power_properties,
224 .num_properties = ARRAY_SIZE(axp22x_ac_power_properties),
225 .get_property = axp20x_ac_power_get_property,
226 };
227
228 static const struct power_supply_desc axp813_ac_power_desc = {
229 .name = "axp813-ac",
230 .type = POWER_SUPPLY_TYPE_MAINS,
231 .properties = axp813_ac_power_properties,
232 .num_properties = ARRAY_SIZE(axp813_ac_power_properties),
233 .property_is_writeable = axp813_ac_power_prop_writeable,
234 .get_property = axp20x_ac_power_get_property,
235 .set_property = axp813_ac_power_set_property,
236 };
237
238 struct axp_data {
239 const struct power_supply_desc *power_desc;
240 bool acin_adc;
241 bool acin_path_sel;
242 };
243
244 static const struct axp_data axp20x_data = {
245 .power_desc = &axp20x_ac_power_desc,
246 .acin_adc = true,
247 .acin_path_sel = false,
248 };
249
250 static const struct axp_data axp22x_data = {
251 .power_desc = &axp22x_ac_power_desc,
252 .acin_adc = false,
253 .acin_path_sel = false,
254 };
255
256 static const struct axp_data axp813_data = {
257 .power_desc = &axp813_ac_power_desc,
258 .acin_adc = false,
259 .acin_path_sel = true,
260 };
261
262 static int axp20x_ac_power_probe(struct platform_device *pdev)
263 {
264 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
265 struct power_supply_config psy_cfg = {};
266 struct axp20x_ac_power *power;
267 const struct axp_data *axp_data;
268 static const char * const irq_names[] = { "ACIN_PLUGIN", "ACIN_REMOVAL",
269 NULL };
270 int i, irq, ret;
271
272 if (!of_device_is_available(pdev->dev.of_node))
273 return -ENODEV;
274
275 if (!axp20x) {
276 dev_err(&pdev->dev, "Parent drvdata not set\n");
277 return -EINVAL;
278 }
279
280 power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
281 if (!power)
282 return -ENOMEM;
283
284 axp_data = of_device_get_match_data(&pdev->dev);
285
286 if (axp_data->acin_adc) {
287 power->acin_v = devm_iio_channel_get(&pdev->dev, "acin_v");
288 if (IS_ERR(power->acin_v)) {
289 if (PTR_ERR(power->acin_v) == -ENODEV)
290 return -EPROBE_DEFER;
291 return PTR_ERR(power->acin_v);
292 }
293
294 power->acin_i = devm_iio_channel_get(&pdev->dev, "acin_i");
295 if (IS_ERR(power->acin_i)) {
296 if (PTR_ERR(power->acin_i) == -ENODEV)
297 return -EPROBE_DEFER;
298 return PTR_ERR(power->acin_i);
299 }
300 }
301
302 power->regmap = dev_get_regmap(pdev->dev.parent, NULL);
303 power->has_acin_path_sel = axp_data->acin_path_sel;
304
305 platform_set_drvdata(pdev, power);
306
307 psy_cfg.of_node = pdev->dev.of_node;
308 psy_cfg.drv_data = power;
309
310 power->supply = devm_power_supply_register(&pdev->dev,
311 axp_data->power_desc,
312 &psy_cfg);
313 if (IS_ERR(power->supply))
314 return PTR_ERR(power->supply);
315
316
317 for (i = 0; irq_names[i]; i++) {
318 irq = platform_get_irq_byname(pdev, irq_names[i]);
319 if (irq < 0) {
320 dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
321 irq_names[i], irq);
322 continue;
323 }
324 irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
325 ret = devm_request_any_context_irq(&pdev->dev, irq,
326 axp20x_ac_power_irq, 0,
327 DRVNAME, power);
328 if (ret < 0)
329 dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
330 irq_names[i], ret);
331 }
332
333 return 0;
334 }
335
336 static const struct of_device_id axp20x_ac_power_match[] = {
337 {
338 .compatible = "x-powers,axp202-ac-power-supply",
339 .data = &axp20x_data,
340 }, {
341 .compatible = "x-powers,axp221-ac-power-supply",
342 .data = &axp22x_data,
343 }, {
344 .compatible = "x-powers,axp813-ac-power-supply",
345 .data = &axp813_data,
346 }, { }
347 };
348 MODULE_DEVICE_TABLE(of, axp20x_ac_power_match);
349
350 static struct platform_driver axp20x_ac_power_driver = {
351 .probe = axp20x_ac_power_probe,
352 .driver = {
353 .name = DRVNAME,
354 .of_match_table = axp20x_ac_power_match,
355 },
356 };
357
358 module_platform_driver(axp20x_ac_power_driver);
359
360 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
361 MODULE_DESCRIPTION("AXP20X and AXP22X PMICs' AC power supply driver");
362 MODULE_LICENSE("GPL");