dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / power / supply / axp20x_usb_power.c
blob4fde24b5f35a1bb2aa939b8b17a3a744e63344da
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * AXP20x PMIC USB power supply status driver
5 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2014 Bruno Prémont <bonbons@linux-vserver.org>
7 */
9 #include <linux/bitops.h>
10 #include <linux/device.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/mfd/axp20x.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm.h>
20 #include <linux/power_supply.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/iio/consumer.h>
24 #include <linux/workqueue.h>
26 #define DRVNAME "axp20x-usb-power-supply"
28 #define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5)
29 #define AXP20X_PWR_STATUS_VBUS_USED BIT(4)
31 #define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
33 #define AXP20X_VBUS_PATH_SEL BIT(7)
34 #define AXP20X_VBUS_PATH_SEL_OFFSET 7
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
45 #define AXP813_VBUS_CLIMIT_900mA 0
46 #define AXP813_VBUS_CLIMIT_1500mA 1
47 #define AXP813_VBUS_CLIMIT_2000mA 2
48 #define AXP813_VBUS_CLIMIT_2500mA 3
50 #define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
51 #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
53 #define AXP20X_VBUS_MON_VBUS_VALID BIT(3)
55 #define AXP813_BC_EN BIT(0)
58 * Note do not raise the debounce time, we must report Vusb high within
59 * 100ms otherwise we get Vbus errors in musb.
61 #define DEBOUNCE_TIME msecs_to_jiffies(50)
63 struct axp20x_usb_power {
64 struct regmap *regmap;
65 struct power_supply *supply;
66 enum axp20x_variants axp20x_id;
67 struct iio_channel *vbus_v;
68 struct iio_channel *vbus_i;
69 struct delayed_work vbus_detect;
70 unsigned int old_status;
71 unsigned int online;
72 unsigned int num_irqs;
73 unsigned int irqs[];
76 static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power *power)
79 * Polling is only necessary while VBUS is offline. While online, a
80 * present->absent transition implies an online->offline transition
81 * and will triger the VBUS_REMOVAL IRQ.
83 if (power->axp20x_id >= AXP221_ID && !power->online)
84 return true;
86 return false;
89 static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
91 struct axp20x_usb_power *power = devid;
93 power_supply_changed(power->supply);
95 mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
97 return IRQ_HANDLED;
100 static void axp20x_usb_power_poll_vbus(struct work_struct *work)
102 struct axp20x_usb_power *power =
103 container_of(work, struct axp20x_usb_power, vbus_detect.work);
104 unsigned int val;
105 int ret;
107 ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &val);
108 if (ret)
109 goto out;
111 val &= (AXP20X_PWR_STATUS_VBUS_PRESENT | AXP20X_PWR_STATUS_VBUS_USED);
112 if (val != power->old_status)
113 power_supply_changed(power->supply);
115 power->old_status = val;
116 power->online = val & AXP20X_PWR_STATUS_VBUS_USED;
118 out:
119 if (axp20x_usb_vbus_needs_polling(power))
120 mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
123 static int axp20x_get_current_max(struct axp20x_usb_power *power, int *val)
125 unsigned int v;
126 int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
128 if (ret)
129 return ret;
131 switch (v & AXP20X_VBUS_CLIMIT_MASK) {
132 case AXP20X_VBUS_CLIMIT_100mA:
133 if (power->axp20x_id == AXP221_ID)
134 *val = -1; /* No 100mA limit */
135 else
136 *val = 100000;
137 break;
138 case AXP20X_VBUS_CLIMIT_500mA:
139 *val = 500000;
140 break;
141 case AXP20X_VBUS_CLIMIT_900mA:
142 *val = 900000;
143 break;
144 case AXP20X_VBUS_CLIMIT_NONE:
145 *val = -1;
146 break;
149 return 0;
152 static int axp813_get_current_max(struct axp20x_usb_power *power, int *val)
154 unsigned int v;
155 int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
157 if (ret)
158 return ret;
160 switch (v & AXP20X_VBUS_CLIMIT_MASK) {
161 case AXP813_VBUS_CLIMIT_900mA:
162 *val = 900000;
163 break;
164 case AXP813_VBUS_CLIMIT_1500mA:
165 *val = 1500000;
166 break;
167 case AXP813_VBUS_CLIMIT_2000mA:
168 *val = 2000000;
169 break;
170 case AXP813_VBUS_CLIMIT_2500mA:
171 *val = 2500000;
172 break;
174 return 0;
177 static int axp20x_usb_power_get_property(struct power_supply *psy,
178 enum power_supply_property psp, union power_supply_propval *val)
180 struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
181 unsigned int input, v;
182 int ret;
184 switch (psp) {
185 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
186 ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
187 if (ret)
188 return ret;
190 val->intval = AXP20X_VBUS_VHOLD_uV(v);
191 return 0;
192 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
193 if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
194 ret = iio_read_channel_processed(power->vbus_v,
195 &val->intval);
196 if (ret)
197 return ret;
200 * IIO framework gives mV but Power Supply framework
201 * gives uV.
203 val->intval *= 1000;
204 return 0;
207 ret = axp20x_read_variable_width(power->regmap,
208 AXP20X_VBUS_V_ADC_H, 12);
209 if (ret < 0)
210 return ret;
212 val->intval = ret * 1700; /* 1 step = 1.7 mV */
213 return 0;
214 case POWER_SUPPLY_PROP_CURRENT_MAX:
215 if (power->axp20x_id == AXP813_ID)
216 return axp813_get_current_max(power, &val->intval);
217 return axp20x_get_current_max(power, &val->intval);
218 case POWER_SUPPLY_PROP_CURRENT_NOW:
219 if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
220 ret = iio_read_channel_processed(power->vbus_i,
221 &val->intval);
222 if (ret)
223 return ret;
226 * IIO framework gives mA but Power Supply framework
227 * gives uA.
229 val->intval *= 1000;
230 return 0;
233 ret = axp20x_read_variable_width(power->regmap,
234 AXP20X_VBUS_I_ADC_H, 12);
235 if (ret < 0)
236 return ret;
238 val->intval = ret * 375; /* 1 step = 0.375 mA */
239 return 0;
240 default:
241 break;
244 /* All the properties below need the input-status reg value */
245 ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
246 if (ret)
247 return ret;
249 switch (psp) {
250 case POWER_SUPPLY_PROP_HEALTH:
251 if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
252 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
253 break;
256 val->intval = POWER_SUPPLY_HEALTH_GOOD;
258 if (power->axp20x_id == AXP202_ID) {
259 ret = regmap_read(power->regmap,
260 AXP20X_USB_OTG_STATUS, &v);
261 if (ret)
262 return ret;
264 if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
265 val->intval =
266 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
268 break;
269 case POWER_SUPPLY_PROP_PRESENT:
270 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
271 break;
272 case POWER_SUPPLY_PROP_ONLINE:
273 val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
274 break;
275 default:
276 return -EINVAL;
279 return 0;
282 static int axp813_usb_power_set_online(struct axp20x_usb_power *power,
283 int intval)
285 int val = !intval << AXP20X_VBUS_PATH_SEL_OFFSET;
287 return regmap_update_bits(power->regmap,
288 AXP20X_VBUS_IPSOUT_MGMT,
289 AXP20X_VBUS_PATH_SEL, val);
292 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
293 int intval)
295 int val;
297 switch (intval) {
298 case 4000000:
299 case 4100000:
300 case 4200000:
301 case 4300000:
302 case 4400000:
303 case 4500000:
304 case 4600000:
305 case 4700000:
306 val = (intval - 4000000) / 100000;
307 return regmap_update_bits(power->regmap,
308 AXP20X_VBUS_IPSOUT_MGMT,
309 AXP20X_VBUS_VHOLD_MASK,
310 val << AXP20X_VBUS_VHOLD_OFFSET);
311 default:
312 return -EINVAL;
315 return -EINVAL;
318 static int axp813_usb_power_set_current_max(struct axp20x_usb_power *power,
319 int intval)
321 int val;
323 switch (intval) {
324 case 900000:
325 return regmap_update_bits(power->regmap,
326 AXP20X_VBUS_IPSOUT_MGMT,
327 AXP20X_VBUS_CLIMIT_MASK,
328 AXP813_VBUS_CLIMIT_900mA);
329 case 1500000:
330 case 2000000:
331 case 2500000:
332 val = (intval - 1000000) / 500000;
333 return regmap_update_bits(power->regmap,
334 AXP20X_VBUS_IPSOUT_MGMT,
335 AXP20X_VBUS_CLIMIT_MASK, val);
336 default:
337 return -EINVAL;
340 return -EINVAL;
343 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
344 int intval)
346 int val;
348 switch (intval) {
349 case 100000:
350 if (power->axp20x_id == AXP221_ID)
351 return -EINVAL;
352 /* fall through */
353 case 500000:
354 case 900000:
355 val = (900000 - intval) / 400000;
356 return regmap_update_bits(power->regmap,
357 AXP20X_VBUS_IPSOUT_MGMT,
358 AXP20X_VBUS_CLIMIT_MASK, val);
359 default:
360 return -EINVAL;
363 return -EINVAL;
366 static int axp20x_usb_power_set_property(struct power_supply *psy,
367 enum power_supply_property psp,
368 const union power_supply_propval *val)
370 struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
372 switch (psp) {
373 case POWER_SUPPLY_PROP_ONLINE:
374 if (power->axp20x_id != AXP813_ID)
375 return -EINVAL;
376 return axp813_usb_power_set_online(power, val->intval);
378 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
379 return axp20x_usb_power_set_voltage_min(power, val->intval);
381 case POWER_SUPPLY_PROP_CURRENT_MAX:
382 if (power->axp20x_id == AXP813_ID)
383 return axp813_usb_power_set_current_max(power,
384 val->intval);
385 return axp20x_usb_power_set_current_max(power, val->intval);
387 default:
388 return -EINVAL;
391 return -EINVAL;
394 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
395 enum power_supply_property psp)
397 struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
400 * The VBUS path select flag works differently on on AXP288 and newer:
401 * - On AXP20x and AXP22x, the flag enables VBUS (ignoring N_VBUSEN).
402 * - On AXP288 and AXP8xx, the flag disables VBUS (ignoring N_VBUSEN).
403 * We only expose the control on variants where it can be used to force
404 * the VBUS input offline.
406 if (psp == POWER_SUPPLY_PROP_ONLINE)
407 return power->axp20x_id == AXP813_ID;
409 return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
410 psp == POWER_SUPPLY_PROP_CURRENT_MAX;
413 static enum power_supply_property axp20x_usb_power_properties[] = {
414 POWER_SUPPLY_PROP_HEALTH,
415 POWER_SUPPLY_PROP_PRESENT,
416 POWER_SUPPLY_PROP_ONLINE,
417 POWER_SUPPLY_PROP_VOLTAGE_MIN,
418 POWER_SUPPLY_PROP_VOLTAGE_NOW,
419 POWER_SUPPLY_PROP_CURRENT_MAX,
420 POWER_SUPPLY_PROP_CURRENT_NOW,
423 static enum power_supply_property axp22x_usb_power_properties[] = {
424 POWER_SUPPLY_PROP_HEALTH,
425 POWER_SUPPLY_PROP_PRESENT,
426 POWER_SUPPLY_PROP_ONLINE,
427 POWER_SUPPLY_PROP_VOLTAGE_MIN,
428 POWER_SUPPLY_PROP_CURRENT_MAX,
431 static const struct power_supply_desc axp20x_usb_power_desc = {
432 .name = "axp20x-usb",
433 .type = POWER_SUPPLY_TYPE_USB,
434 .properties = axp20x_usb_power_properties,
435 .num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
436 .property_is_writeable = axp20x_usb_power_prop_writeable,
437 .get_property = axp20x_usb_power_get_property,
438 .set_property = axp20x_usb_power_set_property,
441 static const struct power_supply_desc axp22x_usb_power_desc = {
442 .name = "axp20x-usb",
443 .type = POWER_SUPPLY_TYPE_USB,
444 .properties = axp22x_usb_power_properties,
445 .num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
446 .property_is_writeable = axp20x_usb_power_prop_writeable,
447 .get_property = axp20x_usb_power_get_property,
448 .set_property = axp20x_usb_power_set_property,
451 static const char * const axp20x_irq_names[] = {
452 "VBUS_PLUGIN",
453 "VBUS_REMOVAL",
454 "VBUS_VALID",
455 "VBUS_NOT_VALID",
458 static const char * const axp22x_irq_names[] = {
459 "VBUS_PLUGIN",
460 "VBUS_REMOVAL",
463 struct axp_data {
464 const struct power_supply_desc *power_desc;
465 const char * const *irq_names;
466 unsigned int num_irq_names;
467 enum axp20x_variants axp20x_id;
470 static const struct axp_data axp202_data = {
471 .power_desc = &axp20x_usb_power_desc,
472 .irq_names = axp20x_irq_names,
473 .num_irq_names = ARRAY_SIZE(axp20x_irq_names),
474 .axp20x_id = AXP202_ID,
477 static const struct axp_data axp221_data = {
478 .power_desc = &axp22x_usb_power_desc,
479 .irq_names = axp22x_irq_names,
480 .num_irq_names = ARRAY_SIZE(axp22x_irq_names),
481 .axp20x_id = AXP221_ID,
484 static const struct axp_data axp223_data = {
485 .power_desc = &axp22x_usb_power_desc,
486 .irq_names = axp22x_irq_names,
487 .num_irq_names = ARRAY_SIZE(axp22x_irq_names),
488 .axp20x_id = AXP223_ID,
491 static const struct axp_data axp813_data = {
492 .power_desc = &axp22x_usb_power_desc,
493 .irq_names = axp22x_irq_names,
494 .num_irq_names = ARRAY_SIZE(axp22x_irq_names),
495 .axp20x_id = AXP813_ID,
498 #ifdef CONFIG_PM_SLEEP
499 static int axp20x_usb_power_suspend(struct device *dev)
501 struct axp20x_usb_power *power = dev_get_drvdata(dev);
502 int i = 0;
505 * Allow wake via VBUS_PLUGIN only.
507 * As nested threaded IRQs are not automatically disabled during
508 * suspend, we must explicitly disable the remainder of the IRQs.
510 if (device_may_wakeup(&power->supply->dev))
511 enable_irq_wake(power->irqs[i++]);
512 while (i < power->num_irqs)
513 disable_irq(power->irqs[i++]);
515 return 0;
518 static int axp20x_usb_power_resume(struct device *dev)
520 struct axp20x_usb_power *power = dev_get_drvdata(dev);
521 int i = 0;
523 if (device_may_wakeup(&power->supply->dev))
524 disable_irq_wake(power->irqs[i++]);
525 while (i < power->num_irqs)
526 enable_irq(power->irqs[i++]);
528 mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
530 return 0;
532 #endif
534 static SIMPLE_DEV_PM_OPS(axp20x_usb_power_pm_ops, axp20x_usb_power_suspend,
535 axp20x_usb_power_resume);
537 static int configure_iio_channels(struct platform_device *pdev,
538 struct axp20x_usb_power *power)
540 power->vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v");
541 if (IS_ERR(power->vbus_v)) {
542 if (PTR_ERR(power->vbus_v) == -ENODEV)
543 return -EPROBE_DEFER;
544 return PTR_ERR(power->vbus_v);
547 power->vbus_i = devm_iio_channel_get(&pdev->dev, "vbus_i");
548 if (IS_ERR(power->vbus_i)) {
549 if (PTR_ERR(power->vbus_i) == -ENODEV)
550 return -EPROBE_DEFER;
551 return PTR_ERR(power->vbus_i);
554 return 0;
557 static int configure_adc_registers(struct axp20x_usb_power *power)
559 /* Enable vbus voltage and current measurement */
560 return regmap_update_bits(power->regmap, AXP20X_ADC_EN1,
561 AXP20X_ADC_EN1_VBUS_CURR |
562 AXP20X_ADC_EN1_VBUS_VOLT,
563 AXP20X_ADC_EN1_VBUS_CURR |
564 AXP20X_ADC_EN1_VBUS_VOLT);
567 static int axp20x_usb_power_probe(struct platform_device *pdev)
569 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
570 struct power_supply_config psy_cfg = {};
571 struct axp20x_usb_power *power;
572 const struct axp_data *axp_data;
573 int i, irq, ret;
575 if (!of_device_is_available(pdev->dev.of_node))
576 return -ENODEV;
578 if (!axp20x) {
579 dev_err(&pdev->dev, "Parent drvdata not set\n");
580 return -EINVAL;
583 axp_data = of_device_get_match_data(&pdev->dev);
585 power = devm_kzalloc(&pdev->dev,
586 struct_size(power, irqs, axp_data->num_irq_names),
587 GFP_KERNEL);
588 if (!power)
589 return -ENOMEM;
591 platform_set_drvdata(pdev, power);
593 power->axp20x_id = axp_data->axp20x_id;
594 power->regmap = axp20x->regmap;
595 power->num_irqs = axp_data->num_irq_names;
597 if (power->axp20x_id == AXP202_ID) {
598 /* Enable vbus valid checking */
599 ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON,
600 AXP20X_VBUS_MON_VBUS_VALID,
601 AXP20X_VBUS_MON_VBUS_VALID);
602 if (ret)
603 return ret;
605 if (IS_ENABLED(CONFIG_AXP20X_ADC))
606 ret = configure_iio_channels(pdev, power);
607 else
608 ret = configure_adc_registers(power);
610 if (ret)
611 return ret;
614 if (power->axp20x_id == AXP813_ID) {
615 /* Enable USB Battery Charging specification detection */
616 regmap_update_bits(axp20x->regmap, AXP288_BC_GLOBAL,
617 AXP813_BC_EN, AXP813_BC_EN);
620 psy_cfg.of_node = pdev->dev.of_node;
621 psy_cfg.drv_data = power;
623 power->supply = devm_power_supply_register(&pdev->dev,
624 axp_data->power_desc,
625 &psy_cfg);
626 if (IS_ERR(power->supply))
627 return PTR_ERR(power->supply);
629 /* Request irqs after registering, as irqs may trigger immediately */
630 for (i = 0; i < axp_data->num_irq_names; i++) {
631 irq = platform_get_irq_byname(pdev, axp_data->irq_names[i]);
632 if (irq < 0) {
633 dev_err(&pdev->dev, "No IRQ for %s: %d\n",
634 axp_data->irq_names[i], irq);
635 return irq;
637 power->irqs[i] = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
638 ret = devm_request_any_context_irq(&pdev->dev, power->irqs[i],
639 axp20x_usb_power_irq, 0,
640 DRVNAME, power);
641 if (ret < 0) {
642 dev_err(&pdev->dev, "Error requesting %s IRQ: %d\n",
643 axp_data->irq_names[i], ret);
644 return ret;
648 INIT_DELAYED_WORK(&power->vbus_detect, axp20x_usb_power_poll_vbus);
649 if (axp20x_usb_vbus_needs_polling(power))
650 queue_delayed_work(system_wq, &power->vbus_detect, 0);
652 return 0;
655 static int axp20x_usb_power_remove(struct platform_device *pdev)
657 struct axp20x_usb_power *power = platform_get_drvdata(pdev);
659 cancel_delayed_work_sync(&power->vbus_detect);
661 return 0;
664 static const struct of_device_id axp20x_usb_power_match[] = {
666 .compatible = "x-powers,axp202-usb-power-supply",
667 .data = &axp202_data,
668 }, {
669 .compatible = "x-powers,axp221-usb-power-supply",
670 .data = &axp221_data,
671 }, {
672 .compatible = "x-powers,axp223-usb-power-supply",
673 .data = &axp223_data,
674 }, {
675 .compatible = "x-powers,axp813-usb-power-supply",
676 .data = &axp813_data,
677 }, { /* sentinel */ }
679 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
681 static struct platform_driver axp20x_usb_power_driver = {
682 .probe = axp20x_usb_power_probe,
683 .remove = axp20x_usb_power_remove,
684 .driver = {
685 .name = DRVNAME,
686 .of_match_table = axp20x_usb_power_match,
687 .pm = &axp20x_usb_power_pm_ops,
691 module_platform_driver(axp20x_usb_power_driver);
693 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
694 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
695 MODULE_LICENSE("GPL");