]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/power/supply/axp20x_usb_power.c
power: supply: axp20x_usb_power: use polling to detect vbus status change
[mirror_ubuntu-eoan-kernel.git] / drivers / power / supply / axp20x_usb_power.c
1 /*
2 * AXP20x PMIC USB power supply status driver
3 *
4 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
5 * Copyright (C) 2014 Bruno Prémont <bonbons@linux-vserver.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 */
12
13 #include <linux/bitops.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/axp20x.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_device.h>
22 #include <linux/platform_device.h>
23 #include <linux/power_supply.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
26 #include <linux/iio/consumer.h>
27 #include <linux/workqueue.h>
28
29 #define DRVNAME "axp20x-usb-power-supply"
30
31 #define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5)
32 #define AXP20X_PWR_STATUS_VBUS_USED BIT(4)
33
34 #define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
35
36 #define AXP20X_VBUS_VHOLD_uV(b) (4000000 + (((b) >> 3) & 7) * 100000)
37 #define AXP20X_VBUS_VHOLD_MASK GENMASK(5, 3)
38 #define AXP20X_VBUS_VHOLD_OFFSET 3
39 #define AXP20X_VBUS_CLIMIT_MASK 3
40 #define AXP20X_VBUS_CLIMIT_900mA 0
41 #define AXP20X_VBUS_CLIMIT_500mA 1
42 #define AXP20X_VBUS_CLIMIT_100mA 2
43 #define AXP20X_VBUS_CLIMIT_NONE 3
44
45 #define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
46 #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
47
48 #define AXP20X_VBUS_MON_VBUS_VALID BIT(3)
49
50 /*
51 * Note do not raise the debounce time, we must report Vusb high within
52 * 100ms otherwise we get Vbus errors in musb.
53 */
54 #define DEBOUNCE_TIME msecs_to_jiffies(50)
55
56 struct axp20x_usb_power {
57 struct device_node *np;
58 struct regmap *regmap;
59 struct power_supply *supply;
60 enum axp20x_variants axp20x_id;
61 struct iio_channel *vbus_v;
62 struct iio_channel *vbus_i;
63 struct delayed_work vbus_detect;
64 unsigned int old_status;
65 };
66
67 static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
68 {
69 struct axp20x_usb_power *power = devid;
70
71 power_supply_changed(power->supply);
72
73 return IRQ_HANDLED;
74 }
75
76 static void axp20x_usb_power_poll_vbus(struct work_struct *work)
77 {
78 struct axp20x_usb_power *power =
79 container_of(work, struct axp20x_usb_power, vbus_detect.work);
80 unsigned int val;
81 int ret;
82
83 ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &val);
84 if (ret)
85 goto out;
86
87 val &= (AXP20X_PWR_STATUS_VBUS_PRESENT | AXP20X_PWR_STATUS_VBUS_USED);
88 if (val != power->old_status)
89 power_supply_changed(power->supply);
90
91 power->old_status = val;
92
93 out:
94 mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
95 }
96
97 static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power *power)
98 {
99 if (power->axp20x_id >= AXP221_ID)
100 return true;
101
102 return false;
103 }
104
105 static int axp20x_usb_power_get_property(struct power_supply *psy,
106 enum power_supply_property psp, union power_supply_propval *val)
107 {
108 struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
109 unsigned int input, v;
110 int ret;
111
112 switch (psp) {
113 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
114 ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
115 if (ret)
116 return ret;
117
118 val->intval = AXP20X_VBUS_VHOLD_uV(v);
119 return 0;
120 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
121 if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
122 ret = iio_read_channel_processed(power->vbus_v,
123 &val->intval);
124 if (ret)
125 return ret;
126
127 /*
128 * IIO framework gives mV but Power Supply framework
129 * gives uV.
130 */
131 val->intval *= 1000;
132 return 0;
133 }
134
135 ret = axp20x_read_variable_width(power->regmap,
136 AXP20X_VBUS_V_ADC_H, 12);
137 if (ret < 0)
138 return ret;
139
140 val->intval = ret * 1700; /* 1 step = 1.7 mV */
141 return 0;
142 case POWER_SUPPLY_PROP_CURRENT_MAX:
143 ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
144 if (ret)
145 return ret;
146
147 switch (v & AXP20X_VBUS_CLIMIT_MASK) {
148 case AXP20X_VBUS_CLIMIT_100mA:
149 if (power->axp20x_id == AXP221_ID)
150 val->intval = -1; /* No 100mA limit */
151 else
152 val->intval = 100000;
153 break;
154 case AXP20X_VBUS_CLIMIT_500mA:
155 val->intval = 500000;
156 break;
157 case AXP20X_VBUS_CLIMIT_900mA:
158 val->intval = 900000;
159 break;
160 case AXP20X_VBUS_CLIMIT_NONE:
161 val->intval = -1;
162 break;
163 }
164 return 0;
165 case POWER_SUPPLY_PROP_CURRENT_NOW:
166 if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
167 ret = iio_read_channel_processed(power->vbus_i,
168 &val->intval);
169 if (ret)
170 return ret;
171
172 /*
173 * IIO framework gives mA but Power Supply framework
174 * gives uA.
175 */
176 val->intval *= 1000;
177 return 0;
178 }
179
180 ret = axp20x_read_variable_width(power->regmap,
181 AXP20X_VBUS_I_ADC_H, 12);
182 if (ret < 0)
183 return ret;
184
185 val->intval = ret * 375; /* 1 step = 0.375 mA */
186 return 0;
187 default:
188 break;
189 }
190
191 /* All the properties below need the input-status reg value */
192 ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
193 if (ret)
194 return ret;
195
196 switch (psp) {
197 case POWER_SUPPLY_PROP_HEALTH:
198 if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
199 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
200 break;
201 }
202
203 val->intval = POWER_SUPPLY_HEALTH_GOOD;
204
205 if (power->axp20x_id == AXP202_ID) {
206 ret = regmap_read(power->regmap,
207 AXP20X_USB_OTG_STATUS, &v);
208 if (ret)
209 return ret;
210
211 if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
212 val->intval =
213 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
214 }
215 break;
216 case POWER_SUPPLY_PROP_PRESENT:
217 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
218 break;
219 case POWER_SUPPLY_PROP_ONLINE:
220 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
221 break;
222 default:
223 return -EINVAL;
224 }
225
226 return 0;
227 }
228
229 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
230 int intval)
231 {
232 int val;
233
234 switch (intval) {
235 case 4000000:
236 case 4100000:
237 case 4200000:
238 case 4300000:
239 case 4400000:
240 case 4500000:
241 case 4600000:
242 case 4700000:
243 val = (intval - 4000000) / 100000;
244 return regmap_update_bits(power->regmap,
245 AXP20X_VBUS_IPSOUT_MGMT,
246 AXP20X_VBUS_VHOLD_MASK,
247 val << AXP20X_VBUS_VHOLD_OFFSET);
248 default:
249 return -EINVAL;
250 }
251
252 return -EINVAL;
253 }
254
255 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
256 int intval)
257 {
258 int val;
259
260 switch (intval) {
261 case 100000:
262 if (power->axp20x_id == AXP221_ID)
263 return -EINVAL;
264 /* fall through */
265 case 500000:
266 case 900000:
267 val = (900000 - intval) / 400000;
268 return regmap_update_bits(power->regmap,
269 AXP20X_VBUS_IPSOUT_MGMT,
270 AXP20X_VBUS_CLIMIT_MASK, val);
271 default:
272 return -EINVAL;
273 }
274
275 return -EINVAL;
276 }
277
278 static int axp20x_usb_power_set_property(struct power_supply *psy,
279 enum power_supply_property psp,
280 const union power_supply_propval *val)
281 {
282 struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
283
284 switch (psp) {
285 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
286 return axp20x_usb_power_set_voltage_min(power, val->intval);
287
288 case POWER_SUPPLY_PROP_CURRENT_MAX:
289 return axp20x_usb_power_set_current_max(power, val->intval);
290
291 default:
292 return -EINVAL;
293 }
294
295 return -EINVAL;
296 }
297
298 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
299 enum power_supply_property psp)
300 {
301 return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
302 psp == POWER_SUPPLY_PROP_CURRENT_MAX;
303 }
304
305 static enum power_supply_property axp20x_usb_power_properties[] = {
306 POWER_SUPPLY_PROP_HEALTH,
307 POWER_SUPPLY_PROP_PRESENT,
308 POWER_SUPPLY_PROP_ONLINE,
309 POWER_SUPPLY_PROP_VOLTAGE_MIN,
310 POWER_SUPPLY_PROP_VOLTAGE_NOW,
311 POWER_SUPPLY_PROP_CURRENT_MAX,
312 POWER_SUPPLY_PROP_CURRENT_NOW,
313 };
314
315 static enum power_supply_property axp22x_usb_power_properties[] = {
316 POWER_SUPPLY_PROP_HEALTH,
317 POWER_SUPPLY_PROP_PRESENT,
318 POWER_SUPPLY_PROP_ONLINE,
319 POWER_SUPPLY_PROP_VOLTAGE_MIN,
320 POWER_SUPPLY_PROP_CURRENT_MAX,
321 };
322
323 static const struct power_supply_desc axp20x_usb_power_desc = {
324 .name = "axp20x-usb",
325 .type = POWER_SUPPLY_TYPE_USB,
326 .properties = axp20x_usb_power_properties,
327 .num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
328 .property_is_writeable = axp20x_usb_power_prop_writeable,
329 .get_property = axp20x_usb_power_get_property,
330 .set_property = axp20x_usb_power_set_property,
331 };
332
333 static const struct power_supply_desc axp22x_usb_power_desc = {
334 .name = "axp20x-usb",
335 .type = POWER_SUPPLY_TYPE_USB,
336 .properties = axp22x_usb_power_properties,
337 .num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
338 .property_is_writeable = axp20x_usb_power_prop_writeable,
339 .get_property = axp20x_usb_power_get_property,
340 .set_property = axp20x_usb_power_set_property,
341 };
342
343 static int configure_iio_channels(struct platform_device *pdev,
344 struct axp20x_usb_power *power)
345 {
346 power->vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v");
347 if (IS_ERR(power->vbus_v)) {
348 if (PTR_ERR(power->vbus_v) == -ENODEV)
349 return -EPROBE_DEFER;
350 return PTR_ERR(power->vbus_v);
351 }
352
353 power->vbus_i = devm_iio_channel_get(&pdev->dev, "vbus_i");
354 if (IS_ERR(power->vbus_i)) {
355 if (PTR_ERR(power->vbus_i) == -ENODEV)
356 return -EPROBE_DEFER;
357 return PTR_ERR(power->vbus_i);
358 }
359
360 return 0;
361 }
362
363 static int configure_adc_registers(struct axp20x_usb_power *power)
364 {
365 /* Enable vbus voltage and current measurement */
366 return regmap_update_bits(power->regmap, AXP20X_ADC_EN1,
367 AXP20X_ADC_EN1_VBUS_CURR |
368 AXP20X_ADC_EN1_VBUS_VOLT,
369 AXP20X_ADC_EN1_VBUS_CURR |
370 AXP20X_ADC_EN1_VBUS_VOLT);
371 }
372
373 static int axp20x_usb_power_probe(struct platform_device *pdev)
374 {
375 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
376 struct power_supply_config psy_cfg = {};
377 struct axp20x_usb_power *power;
378 static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN",
379 "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL };
380 static const char * const axp22x_irq_names[] = {
381 "VBUS_PLUGIN", "VBUS_REMOVAL", NULL };
382 const char * const *irq_names;
383 const struct power_supply_desc *usb_power_desc;
384 int i, irq, ret;
385
386 if (!of_device_is_available(pdev->dev.of_node))
387 return -ENODEV;
388
389 if (!axp20x) {
390 dev_err(&pdev->dev, "Parent drvdata not set\n");
391 return -EINVAL;
392 }
393
394 power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
395 if (!power)
396 return -ENOMEM;
397
398 platform_set_drvdata(pdev, power);
399 power->axp20x_id = (enum axp20x_variants)of_device_get_match_data(
400 &pdev->dev);
401
402 power->np = pdev->dev.of_node;
403 power->regmap = axp20x->regmap;
404
405 if (power->axp20x_id == AXP202_ID) {
406 /* Enable vbus valid checking */
407 ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON,
408 AXP20X_VBUS_MON_VBUS_VALID,
409 AXP20X_VBUS_MON_VBUS_VALID);
410 if (ret)
411 return ret;
412
413 if (IS_ENABLED(CONFIG_AXP20X_ADC))
414 ret = configure_iio_channels(pdev, power);
415 else
416 ret = configure_adc_registers(power);
417
418 if (ret)
419 return ret;
420
421 usb_power_desc = &axp20x_usb_power_desc;
422 irq_names = axp20x_irq_names;
423 } else if (power->axp20x_id == AXP221_ID ||
424 power->axp20x_id == AXP223_ID) {
425 usb_power_desc = &axp22x_usb_power_desc;
426 irq_names = axp22x_irq_names;
427 } else {
428 dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
429 axp20x->variant);
430 return -EINVAL;
431 }
432
433 psy_cfg.of_node = pdev->dev.of_node;
434 psy_cfg.drv_data = power;
435
436 power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
437 &psy_cfg);
438 if (IS_ERR(power->supply))
439 return PTR_ERR(power->supply);
440
441 /* Request irqs after registering, as irqs may trigger immediately */
442 for (i = 0; irq_names[i]; i++) {
443 irq = platform_get_irq_byname(pdev, irq_names[i]);
444 if (irq < 0) {
445 dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
446 irq_names[i], irq);
447 continue;
448 }
449 irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
450 ret = devm_request_any_context_irq(&pdev->dev, irq,
451 axp20x_usb_power_irq, 0, DRVNAME, power);
452 if (ret < 0)
453 dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
454 irq_names[i], ret);
455 }
456
457 INIT_DELAYED_WORK(&power->vbus_detect, axp20x_usb_power_poll_vbus);
458 if (axp20x_usb_vbus_needs_polling(power))
459 queue_delayed_work(system_wq, &power->vbus_detect, 0);
460
461 return 0;
462 }
463
464 static int axp20x_usb_power_remove(struct platform_device *pdev)
465 {
466 struct axp20x_usb_power *power = platform_get_drvdata(pdev);
467
468 cancel_delayed_work_sync(&power->vbus_detect);
469
470 return 0;
471 }
472
473 static const struct of_device_id axp20x_usb_power_match[] = {
474 {
475 .compatible = "x-powers,axp202-usb-power-supply",
476 .data = (void *)AXP202_ID,
477 }, {
478 .compatible = "x-powers,axp221-usb-power-supply",
479 .data = (void *)AXP221_ID,
480 }, {
481 .compatible = "x-powers,axp223-usb-power-supply",
482 .data = (void *)AXP223_ID,
483 }, { /* sentinel */ }
484 };
485 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
486
487 static struct platform_driver axp20x_usb_power_driver = {
488 .probe = axp20x_usb_power_probe,
489 .remove = axp20x_usb_power_remove,
490 .driver = {
491 .name = DRVNAME,
492 .of_match_table = axp20x_usb_power_match,
493 },
494 };
495
496 module_platform_driver(axp20x_usb_power_driver);
497
498 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
499 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
500 MODULE_LICENSE("GPL");