Skip to content

Commit 09aaaec

Browse files
smaeulsre
authored andcommitted
power: supply: axp20x_usb_power: Add wakeup control
The USB power supply input can be used as a wakeup source. Hook up the VBUS_PLUGIN IRQ to trigger wakeup based on userspace configuration. To do this, we must remember the list of IRQs for the life of the device. To know how much space to allocate for the flexible array member, we switch from using a NULL sentinel to using an array length. Because we now depend on the specific order of the IRQs (we assume VBUS_PLUGIN is first and always present), failing to acquire an IRQ during probe must be a fatal error. To avoid spuriously waking up the system when the USB power supply is not configured as a wakeup source, we must explicitly disable all non- wake IRQs during system suspend. This is because the SoC's NMI input is shared among all IRQs on the AXP PMIC. Due to the use of regmap-irq, the individual IRQs within the PMIC are nested threaded interrupts, and are therefore not automatically disabled during system suspend. The upshot is that if any other device within the MFD (such as the power key) is an enabled wakeup source, all enabled IRQs within the PMIC will cause wakeup. We still need to call enable_irq_wake() when we *do* want wakeup, in case those other wakeup sources on the PMIC are all disabled. Reviewed-by: Chen-Yu Tsai <[email protected]> Signed-off-by: Samuel Holland <[email protected]> Signed-off-by: Sebastian Reichel <[email protected]>
1 parent ecbc8dd commit 09aaaec

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

drivers/power/supply/axp20x_usb_power.c

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/of.h>
1717
#include <linux/of_device.h>
1818
#include <linux/platform_device.h>
19+
#include <linux/pm.h>
1920
#include <linux/power_supply.h>
2021
#include <linux/regmap.h>
2122
#include <linux/slab.h>
@@ -67,6 +68,8 @@ struct axp20x_usb_power {
6768
struct iio_channel *vbus_i;
6869
struct delayed_work vbus_detect;
6970
unsigned int old_status;
71+
unsigned int num_irqs;
72+
unsigned int irqs[];
7073
};
7174

7275
static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
@@ -440,45 +443,85 @@ static const char * const axp20x_irq_names[] = {
440443
"VBUS_REMOVAL",
441444
"VBUS_VALID",
442445
"VBUS_NOT_VALID",
443-
NULL
444446
};
445447

446448
static const char * const axp22x_irq_names[] = {
447449
"VBUS_PLUGIN",
448450
"VBUS_REMOVAL",
449-
NULL
450451
};
451452

452453
struct axp_data {
453454
const struct power_supply_desc *power_desc;
454455
const char * const *irq_names;
456+
unsigned int num_irq_names;
455457
enum axp20x_variants axp20x_id;
456458
};
457459

458460
static const struct axp_data axp202_data = {
459461
.power_desc = &axp20x_usb_power_desc,
460462
.irq_names = axp20x_irq_names,
463+
.num_irq_names = ARRAY_SIZE(axp20x_irq_names),
461464
.axp20x_id = AXP202_ID,
462465
};
463466

464467
static const struct axp_data axp221_data = {
465468
.power_desc = &axp22x_usb_power_desc,
466469
.irq_names = axp22x_irq_names,
470+
.num_irq_names = ARRAY_SIZE(axp22x_irq_names),
467471
.axp20x_id = AXP221_ID,
468472
};
469473

470474
static const struct axp_data axp223_data = {
471475
.power_desc = &axp22x_usb_power_desc,
472476
.irq_names = axp22x_irq_names,
477+
.num_irq_names = ARRAY_SIZE(axp22x_irq_names),
473478
.axp20x_id = AXP223_ID,
474479
};
475480

476481
static const struct axp_data axp813_data = {
477482
.power_desc = &axp22x_usb_power_desc,
478483
.irq_names = axp22x_irq_names,
484+
.num_irq_names = ARRAY_SIZE(axp22x_irq_names),
479485
.axp20x_id = AXP813_ID,
480486
};
481487

488+
#ifdef CONFIG_PM_SLEEP
489+
static int axp20x_usb_power_suspend(struct device *dev)
490+
{
491+
struct axp20x_usb_power *power = dev_get_drvdata(dev);
492+
int i = 0;
493+
494+
/*
495+
* Allow wake via VBUS_PLUGIN only.
496+
*
497+
* As nested threaded IRQs are not automatically disabled during
498+
* suspend, we must explicitly disable the remainder of the IRQs.
499+
*/
500+
if (device_may_wakeup(&power->supply->dev))
501+
enable_irq_wake(power->irqs[i++]);
502+
while (i < power->num_irqs)
503+
disable_irq(power->irqs[i++]);
504+
505+
return 0;
506+
}
507+
508+
static int axp20x_usb_power_resume(struct device *dev)
509+
{
510+
struct axp20x_usb_power *power = dev_get_drvdata(dev);
511+
int i = 0;
512+
513+
if (device_may_wakeup(&power->supply->dev))
514+
disable_irq_wake(power->irqs[i++]);
515+
while (i < power->num_irqs)
516+
enable_irq(power->irqs[i++]);
517+
518+
return 0;
519+
}
520+
#endif
521+
522+
static SIMPLE_DEV_PM_OPS(axp20x_usb_power_pm_ops, axp20x_usb_power_suspend,
523+
axp20x_usb_power_resume);
524+
482525
static int configure_iio_channels(struct platform_device *pdev,
483526
struct axp20x_usb_power *power)
484527
{
@@ -525,15 +568,19 @@ static int axp20x_usb_power_probe(struct platform_device *pdev)
525568
return -EINVAL;
526569
}
527570

528-
power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
571+
axp_data = of_device_get_match_data(&pdev->dev);
572+
573+
power = devm_kzalloc(&pdev->dev,
574+
struct_size(power, irqs, axp_data->num_irq_names),
575+
GFP_KERNEL);
529576
if (!power)
530577
return -ENOMEM;
531578

532579
platform_set_drvdata(pdev, power);
533580

534-
axp_data = of_device_get_match_data(&pdev->dev);
535581
power->axp20x_id = axp_data->axp20x_id;
536582
power->regmap = axp20x->regmap;
583+
power->num_irqs = axp_data->num_irq_names;
537584

538585
if (power->axp20x_id == AXP202_ID) {
539586
/* Enable vbus valid checking */
@@ -568,19 +615,22 @@ static int axp20x_usb_power_probe(struct platform_device *pdev)
568615
return PTR_ERR(power->supply);
569616

570617
/* Request irqs after registering, as irqs may trigger immediately */
571-
for (i = 0; axp_data->irq_names[i]; i++) {
618+
for (i = 0; i < axp_data->num_irq_names; i++) {
572619
irq = platform_get_irq_byname(pdev, axp_data->irq_names[i]);
573620
if (irq < 0) {
574-
dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
575-
axp_data->irq_names[i], irq);
576-
continue;
621+
dev_err(&pdev->dev, "No IRQ for %s: %d\n",
622+
axp_data->irq_names[i], irq);
623+
return irq;
624+
}
625+
power->irqs[i] = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
626+
ret = devm_request_any_context_irq(&pdev->dev, power->irqs[i],
627+
axp20x_usb_power_irq, 0,
628+
DRVNAME, power);
629+
if (ret < 0) {
630+
dev_err(&pdev->dev, "Error requesting %s IRQ: %d\n",
631+
axp_data->irq_names[i], ret);
632+
return ret;
577633
}
578-
irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
579-
ret = devm_request_any_context_irq(&pdev->dev, irq,
580-
axp20x_usb_power_irq, 0, DRVNAME, power);
581-
if (ret < 0)
582-
dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
583-
axp_data->irq_names[i], ret);
584634
}
585635

586636
INIT_DELAYED_WORK(&power->vbus_detect, axp20x_usb_power_poll_vbus);
@@ -620,8 +670,9 @@ static struct platform_driver axp20x_usb_power_driver = {
620670
.probe = axp20x_usb_power_probe,
621671
.remove = axp20x_usb_power_remove,
622672
.driver = {
623-
.name = DRVNAME,
624-
.of_match_table = axp20x_usb_power_match,
673+
.name = DRVNAME,
674+
.of_match_table = axp20x_usb_power_match,
675+
.pm = &axp20x_usb_power_pm_ops,
625676
},
626677
};
627678

0 commit comments

Comments
 (0)