2022-09-12 23:15:09

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 00/13] acpi: i2c: Use SharedAndWake and ExclusiveAndWake to enable wake irq


Today, i2c drivers are making the assumption that their IRQs can also
be used as wake IRQs. This isn't always the case and it can lead to
spurious wakes. This has recently started to affect AMD Chromebooks.
With the introduction of
d62bd5ce12d7 ("pinctrl: amd: Implement irq_set_wake"), the AMD GPIO
controller gained the capability to set the wake bit on each GPIO. The
ACPI specification defines two ways to inform the system if a device is
wake capable:
1) The _PRW object defines the GPE that can be used to wake the system.
2) Setting ExclusiveAndWake or SharedAndWake in the _CRS GpioInt.

Currently only the first method is supported. The i2c drivers don't have
any indication that the IRQ is wake capable, so they guess. This causes
spurious interrupts, for example:
* We have an ACPI HID device that has `_PR0` and `_PR3`. It doesn't have
`_PRW` or `ExclusiveAndWake` so that means the device can't wake the
system.
* The IRQ line is active level low for this device and is pulled up by
the power resource defined in `_PR0`/`_PR3`.
* The i2c driver will (incorrectly) arm the GPIO for wake by calling
`enable_irq_wake` as part of its suspend hook.
* ACPI will power down the device since it doesn't have a wake GPE
associated with it.
* When the device is powered down, the IRQ line will drop, and it will
trigger a wake event.

See the following debug log:
[ 42.335804] PM: Suspending system (s2idle)
[ 42.340186] amd_gpio AMD0030:00: RX: Setting wake for pin 89 to enable
[ 42.467736] power-0416 __acpi_power_off : Power resource [PR00] turned off
[ 42.467739] device_pm-0280 device_set_power : Device [H05D] transitioned to D3cold
[ 42.475210] PM: pm_system_irq_wakeup: 11 triggered pinctrl_amd
[ 42.535293] PM: Wakeup unrelated to ACPI SCI
[ 42.535294] PM: resume from suspend-to-idle

In order to fix this, we need to take into account the wake capable bit
defined on the Interrupt/GpioInt. This is accomplished by:
* Migrating some of the i2c drivers over to using the PM subsystem to
manage the wake IRQ.
* Expose the wake_capable bit from the ACPI Interrupt/GpioInt resource
to the i2c core.
* Use the wake_capable bit in the i2c core to call
`dev_pm_set_wake_irq`. This reuses the existing device tree flow.
* Make the i2c drivers stop calling `dev_pm_set_wake_irq` since it's now
handled by the i2c core.
* Make the ACPI device PM system aware of the wake_irq. This is
necessary so the device doesn't incorrectly get powered down when a
wake_irq is enabled.

I've tested this code with various combinations of having _PRW,
ExclusiveAndWake and power resources all defined or not defined, but it
would be great if others could test this out on their hardware.

I'm sure this will surface some devices where the IRQs were not
correctly marked as wake capable. Ideally the firmware can be fixed, but
if not we can work around this in the kernel by providing a board
specific `struct i2c_board_info` with the `I2C_CLIENT_WAKE` flag set.
See `chromeos_laptop.c` for an example of matching DMI properties and
setting the `I2C_CLIENT_WAKE` override.

Thanks,
Raul

Changes in v2:
- Added elants_i2c to series
- Added raydium_ts_i2c to series
- Fixed call site in mlxbf_gige_probe
- Added ability to extract wake bit from Interrupt/IRQ resources
- Look at wake_cabple bit for IRQ/Interrupt resources
- I chose not to keep the legacy code around since systems without DT or ACPI should be rare.

Raul E Rangel (13):
HID: i2c-hid: Use PM subsystem to manage wake irq
Input: elan_i2c - Use PM subsystem to manage wake irq
Input: elants_i2c - Use PM subsystem to manage wake irq
Input: raydium_ts_i2c - Use PM subsystem to manage wake irq
gpiolib: acpi: Add wake_capable parameter to acpi_dev_gpio_irq_get_by
ACPI: resources: Add wake_capable parameter to acpi_dev_irq_flags
i2c: acpi: Use ACPI wake capability bit to set wake_irq
ACPI: PM: Take wake IRQ into consideration when entering
suspend-to-idle
HID: i2c-hid: acpi: Stop setting wakeup_capable
HID: i2c-hid: Don't set wake_capable and wake_irq
Input: elan_i2c - Don't set wake_capable and wake_irq
Input: elants_i2c - Don't set wake_capable and wake_irq
Input: raydium_ts_i2c - Don't set wake_capable and wake_irq

drivers/acpi/device_pm.c | 19 +++++++++-
drivers/acpi/irq.c | 11 ++++--
drivers/acpi/resource.c | 24 ++++++++----
drivers/gpio/gpio-pca953x.c | 3 +-
drivers/gpio/gpiolib-acpi.c | 11 +++++-
drivers/gpio/gpiolib-acpi.h | 2 +
drivers/hid/i2c-hid/i2c-hid-acpi.c | 5 ---
drivers/hid/i2c-hid/i2c-hid-core.c | 24 ++----------
drivers/i2c/i2c-core-acpi.c | 37 ++++++++++++++-----
drivers/i2c/i2c-core-base.c | 6 ++-
drivers/i2c/i2c-core.h | 4 +-
drivers/input/mouse/elan_i2c_core.c | 15 +-------
drivers/input/touchscreen/elants_i2c.c | 13 +------
drivers/input/touchscreen/raydium_i2c_ts.c | 7 +---
.../mellanox/mlxbf_gige/mlxbf_gige_main.c | 3 +-
drivers/pnp/pnpacpi/rsparser.c | 9 +++--
include/linux/acpi.h | 17 +++++++--
include/linux/ioport.h | 3 +-
18 files changed, 121 insertions(+), 92 deletions(-)

--
2.37.2.789.g6183377224-goog


2022-09-12 23:15:14

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 12/13] Input: elants_i2c - Don't set wake_capable and wake_irq

The i2c-core will now handle setting the wake_irq and wake capability
for DT and ACPI systems.

Signed-off-by: Raul E Rangel <[email protected]>
---

(no changes since v1)

drivers/input/touchscreen/elants_i2c.c | 16 ----------------
1 file changed, 16 deletions(-)

diff --git a/drivers/input/touchscreen/elants_i2c.c b/drivers/input/touchscreen/elants_i2c.c
index 80e16b533c452a..3500293bb1d8e1 100644
--- a/drivers/input/touchscreen/elants_i2c.c
+++ b/drivers/input/touchscreen/elants_i2c.c
@@ -1575,22 +1575,6 @@ static int elants_i2c_probe(struct i2c_client *client)
return error;
}

- /*
- * Systems using device tree should set up wakeup via DTS,
- * the rest will configure device as wakeup source by default.
- */
- if (!client->dev.of_node)
- device_init_wakeup(&client->dev, true);
-
- /*
- * The wake IRQ should be declared via device tree instead of assuming
- * the IRQ can wake the system. This is here for legacy reasons and
- * will be removed once the i2c-core supports querying ACPI for wake
- * capabilities.
- */
- if (!client->dev.power.wakeirq)
- dev_pm_set_wake_irq(&client->dev, client->irq);
-
error = devm_device_add_group(&client->dev, &elants_attribute_group);
if (error) {
dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
--
2.37.2.789.g6183377224-goog

2022-09-12 23:15:33

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 13/13] Input: raydium_ts_i2c - Don't set wake_capable and wake_irq

The i2c-core will now handle setting the wake_irq and wake capability
for DT and ACPI systems.

Signed-off-by: Raul E Rangel <[email protected]>
---

(no changes since v1)

drivers/input/touchscreen/raydium_i2c_ts.c | 9 ---------
1 file changed, 9 deletions(-)

diff --git a/drivers/input/touchscreen/raydium_i2c_ts.c b/drivers/input/touchscreen/raydium_i2c_ts.c
index 66c5b577b791d4..88d187dc5d325f 100644
--- a/drivers/input/touchscreen/raydium_i2c_ts.c
+++ b/drivers/input/touchscreen/raydium_i2c_ts.c
@@ -1185,15 +1185,6 @@ static int raydium_i2c_probe(struct i2c_client *client,
return error;
}

- /*
- * The wake IRQ should be declared via device tree instead of assuming
- * the IRQ can wake the system. This is here for legacy reasons and
- * will be removed once the i2c-core supports querying ACPI for wake
- * capabilities.
- */
- if (!client->dev.power.wakeirq)
- dev_pm_set_wake_irq(&client->dev, client->irq);
-
error = devm_device_add_group(&client->dev,
&raydium_i2c_attribute_group);
if (error) {
--
2.37.2.789.g6183377224-goog

2022-09-12 23:30:38

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 01/13] HID: i2c-hid: Use PM subsystem to manage wake irq

The I2C hid driver is currently manually managing the wake
IRQ. This change removes the explicit enable_irq_wake/disable_irq_wake
and instead relies on the PM subsystem. This is done by calling
dev_pm_set_wake_irq.

i2c_device_probe already calls dev_pm_set_wake_irq when using device
tree, and i2c_device_remove also already calls dev_pm_clear_wake_irq.
There could be some device tree systems that have incorrectly declared
`wake` capabilities, so this change will set the wake irq if one is
missing. This matches the previous behavior.

I tested this on an ACPI system that has a HID touchscreen and verified
the IRQ was armed for wake on suspend.

Signed-off-by: Raul E Rangel <[email protected]>
---

Changes in v2:
- Set the wake_irq when not configured by the i2c-core. This is
different than v1, where the wake_irq was only set for non DT systems.

drivers/hid/i2c-hid/i2c-hid-core.c | 33 +++++++++++-------------------
1 file changed, 12 insertions(+), 21 deletions(-)

diff --git a/drivers/hid/i2c-hid/i2c-hid-core.c b/drivers/hid/i2c-hid/i2c-hid-core.c
index baa169fadd6632..57214549460043 100644
--- a/drivers/hid/i2c-hid/i2c-hid-core.c
+++ b/drivers/hid/i2c-hid/i2c-hid-core.c
@@ -26,6 +26,7 @@
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/pm.h>
+#include <linux/pm_wakeirq.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/err.h>
@@ -116,7 +117,6 @@ struct i2c_hid {

wait_queue_head_t wait; /* For waiting the interrupt */

- bool irq_wake_enabled;
struct mutex reset_lock;

struct i2chid_ops *ops;
@@ -1036,6 +1036,15 @@ int i2c_hid_core_probe(struct i2c_client *client, struct i2chid_ops *ops,
if (ret < 0)
goto err_powered;

+ /*
+ * The wake IRQ should be declared via device tree instead of assuming
+ * the IRQ can wake the system. This is here for legacy reasons and
+ * will be removed once the i2c-core supports querying ACPI for wake
+ * capabilities.
+ */
+ if (!dev->power.wakeirq)
+ dev_pm_set_wake_irq(&client->dev, client->irq);
+
hid = hid_allocate_device();
if (IS_ERR(hid)) {
ret = PTR_ERR(hid);
@@ -1119,7 +1128,6 @@ static int i2c_hid_core_suspend(struct device *dev)
struct i2c_hid *ihid = i2c_get_clientdata(client);
struct hid_device *hid = ihid->hid;
int ret;
- int wake_status;

ret = hid_driver_suspend(hid, PMSG_SUSPEND);
if (ret < 0)
@@ -1130,16 +1138,8 @@ static int i2c_hid_core_suspend(struct device *dev)

disable_irq(client->irq);

- if (device_may_wakeup(&client->dev)) {
- wake_status = enable_irq_wake(client->irq);
- if (!wake_status)
- ihid->irq_wake_enabled = true;
- else
- hid_warn(hid, "Failed to enable irq wake: %d\n",
- wake_status);
- } else {
+ if (!device_may_wakeup(&client->dev))
i2c_hid_core_power_down(ihid);
- }

return 0;
}
@@ -1150,18 +1150,9 @@ static int i2c_hid_core_resume(struct device *dev)
struct i2c_client *client = to_i2c_client(dev);
struct i2c_hid *ihid = i2c_get_clientdata(client);
struct hid_device *hid = ihid->hid;
- int wake_status;

- if (!device_may_wakeup(&client->dev)) {
+ if (!device_may_wakeup(&client->dev))
i2c_hid_core_power_up(ihid);
- } else if (ihid->irq_wake_enabled) {
- wake_status = disable_irq_wake(client->irq);
- if (!wake_status)
- ihid->irq_wake_enabled = false;
- else
- hid_warn(hid, "Failed to disable irq wake: %d\n",
- wake_status);
- }

enable_irq(client->irq);

--
2.37.2.789.g6183377224-goog

2022-09-12 23:39:31

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 04/13] Input: raydium_ts_i2c - Use PM subsystem to manage wake irq

The raydium I2C touchscreen driver is currently manually managing the
wake IRQ. This change removes the explicit enable_irq_wake /
disable_irq_wake and instead relies on the PM subsystem. This is done by
calling dev_pm_set_wake_irq.

i2c_device_probe already calls dev_pm_set_wake_irq when using device
tree, and i2c_device_remove also already calls dev_pm_clear_wake_irq.
There could be some device tree systems that have incorrectly declared
`wake` capabilities, so this change will set the wake irq if one is
missing. This matches the previous behavior.

Signed-off-by: Raul E Rangel <[email protected]>
---

Changes in v2:
- Added raydium_ts_i2c to series

drivers/input/touchscreen/raydium_i2c_ts.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/input/touchscreen/raydium_i2c_ts.c b/drivers/input/touchscreen/raydium_i2c_ts.c
index 3a4952935366f9..66c5b577b791d4 100644
--- a/drivers/input/touchscreen/raydium_i2c_ts.c
+++ b/drivers/input/touchscreen/raydium_i2c_ts.c
@@ -21,6 +21,7 @@
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.h>
+#include <linux/pm_wakeirq.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <asm/unaligned.h>
@@ -134,8 +135,6 @@ struct raydium_data {
u8 pkg_size;

enum raydium_boot_mode boot_mode;
-
- bool wake_irq_enabled;
};

/*
@@ -1186,6 +1185,15 @@ static int raydium_i2c_probe(struct i2c_client *client,
return error;
}

+ /*
+ * The wake IRQ should be declared via device tree instead of assuming
+ * the IRQ can wake the system. This is here for legacy reasons and
+ * will be removed once the i2c-core supports querying ACPI for wake
+ * capabilities.
+ */
+ if (!client->dev.power.wakeirq)
+ dev_pm_set_wake_irq(&client->dev, client->irq);
+
error = devm_device_add_group(&client->dev,
&raydium_i2c_attribute_group);
if (error) {
@@ -1222,8 +1230,6 @@ static int __maybe_unused raydium_i2c_suspend(struct device *dev)

if (device_may_wakeup(dev)) {
raydium_enter_sleep(client);
-
- ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
} else {
raydium_i2c_power_off(ts);
}
@@ -1237,8 +1243,6 @@ static int __maybe_unused raydium_i2c_resume(struct device *dev)
struct raydium_data *ts = i2c_get_clientdata(client);

if (device_may_wakeup(dev)) {
- if (ts->wake_irq_enabled)
- disable_irq_wake(client->irq);
raydium_i2c_sw_reset(client);
} else {
raydium_i2c_power_on(ts);
--
2.37.2.789.g6183377224-goog

2022-09-12 23:40:02

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 09/13] HID: i2c-hid: acpi: Stop setting wakeup_capable

This is now handled by the i2c-core driver.

Signed-off-by: Raul E Rangel <[email protected]>
---

(no changes since v1)

drivers/hid/i2c-hid/i2c-hid-acpi.c | 5 -----
1 file changed, 5 deletions(-)

diff --git a/drivers/hid/i2c-hid/i2c-hid-acpi.c b/drivers/hid/i2c-hid/i2c-hid-acpi.c
index b96ae15e0ad917..375c77c3db74d9 100644
--- a/drivers/hid/i2c-hid/i2c-hid-acpi.c
+++ b/drivers/hid/i2c-hid/i2c-hid-acpi.c
@@ -105,11 +105,6 @@ static int i2c_hid_acpi_probe(struct i2c_client *client)

acpi_device_fix_up_power(adev);

- if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0) {
- device_set_wakeup_capable(dev, true);
- device_set_wakeup_enable(dev, false);
- }
-
return i2c_hid_core_probe(client, &ihid_acpi->ops,
hid_descriptor_address, 0);
}
--
2.37.2.789.g6183377224-goog

2022-09-12 23:40:50

by Raul Rangel

[permalink] [raw]
Subject: [PATCH v2 03/13] Input: elants_i2c - Use PM subsystem to manage wake irq

The Elan I2C touchscreen driver is currently manually managing the wake
IRQ. This change removes the explicit enable_irq_wake/disable_irq_wake
and instead relies on the PM subsystem. This is done by calling
dev_pm_set_wake_irq.

i2c_device_probe already calls dev_pm_set_wake_irq when using device
tree, and i2c_device_remove also already calls dev_pm_clear_wake_irq.
There could be some device tree systems that have incorrectly declared
`wake` capabilities, so this change will set the wake irq if one is
missing. This matches the previous behavior.

Signed-off-by: Raul E Rangel <[email protected]>
---

Changes in v2:
- Added elants_i2c to series

drivers/input/touchscreen/elants_i2c.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/input/touchscreen/elants_i2c.c b/drivers/input/touchscreen/elants_i2c.c
index a56f042adf9d82..80e16b533c452a 100644
--- a/drivers/input/touchscreen/elants_i2c.c
+++ b/drivers/input/touchscreen/elants_i2c.c
@@ -36,6 +36,7 @@
#include <linux/input/touchscreen.h>
#include <linux/acpi.h>
#include <linux/of.h>
+#include <linux/pm_wakeirq.h>
#include <linux/gpio/consumer.h>
#include <linux/regulator/consumer.h>
#include <linux/uuid.h>
@@ -180,7 +181,6 @@ struct elants_data {
u8 cmd_resp[HEADER_SIZE];
struct completion cmd_done;

- bool wake_irq_enabled;
bool keep_power_in_suspend;

/* Must be last to be used for DMA operations */
@@ -1582,6 +1582,15 @@ static int elants_i2c_probe(struct i2c_client *client)
if (!client->dev.of_node)
device_init_wakeup(&client->dev, true);

+ /*
+ * The wake IRQ should be declared via device tree instead of assuming
+ * the IRQ can wake the system. This is here for legacy reasons and
+ * will be removed once the i2c-core supports querying ACPI for wake
+ * capabilities.
+ */
+ if (!client->dev.power.wakeirq)
+ dev_pm_set_wake_irq(&client->dev, client->irq);
+
error = devm_device_add_group(&client->dev, &elants_attribute_group);
if (error) {
dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
@@ -1626,7 +1635,7 @@ static int __maybe_unused elants_i2c_suspend(struct device *dev)
* The device will automatically enter idle mode
* that has reduced power consumption.
*/
- ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0);
+ return 0;
} else if (ts->keep_power_in_suspend) {
for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
error = elants_i2c_send(client, set_sleep_cmd,
@@ -1655,8 +1664,6 @@ static int __maybe_unused elants_i2c_resume(struct device *dev)
int error;

if (device_may_wakeup(dev)) {
- if (ts->wake_irq_enabled)
- disable_irq_wake(client->irq);
elants_i2c_sw_reset(client);
} else if (ts->keep_power_in_suspend) {
for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) {
--
2.37.2.789.g6183377224-goog