[PATCH v4 6/6] Input: aw86927 - stop playback over suspend, close and shutdown

From: Qianhui Xu

Date: Mon Nov 23 2026 - 23:51:15 EST


Playback is not stopped across system sleep, device close and shutdown:
an effect that is being played when the system suspends keeps the chip
driving the actuator, a memless effect whose timer fires around close()
restarts playback on a device nobody has open, and nothing holds the chip
in reset on shutdown or unbind.

Park the chip in standby on suspend, refuse new effects while the device
is closed or suspended, and assert reset on shutdown. The work item is
drained from a devres action so it also runs on unbind, where
input_close_device() is skipped for an inhibited device and the memless
effect timer can queue play_work again; that action also covers shutdown.

Suspend parks the chip in standby without asserting reset, and resume
only clears the suspend flag: the chip is left in standby with its SRAM
initialised, which is the state probe leaves it in. Shutdown asserts
reset without a separate stop.

Signed-off-by: Qianhui Xu <xiaqinghuan@xxxxxxxxxxxxxxxx>
---
drivers/input/misc/aw86927.c | 120 ++++++++++++++++++++++++++++++++---
1 file changed, 112 insertions(+), 8 deletions(-)

diff --git a/drivers/input/misc/aw86927.c b/drivers/input/misc/aw86927.c
index 1dbcba7988..9ffea6a2bc 100644
--- a/drivers/input/misc/aw86927.c
+++ b/drivers/input/misc/aw86927.c
@@ -14,6 +14,7 @@
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/module.h>
+#include <linux/pm.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/types.h>
@@ -203,6 +204,8 @@ struct aw86927_data {
struct i2c_client *client;
struct regmap *regmap;
struct gpio_desc *reset_gpio;
+ bool suspended;
+ bool enabled;
u16 level;
};

@@ -365,22 +368,30 @@ static int aw86927_haptics_play(struct input_dev *dev, void *data, struct ff_eff
struct aw86927_data *haptics = input_get_drvdata(dev);
int level;

+ /*
+ * Ignore new effects while the chip is parked in standby (suspended) or
+ * while nobody has the device open: a queued effect would either fail or
+ * restart playback that close() already stopped.
+ */
+ if (READ_ONCE(haptics->suspended) || !READ_ONCE(haptics->enabled))
+ return 0;
+
level = effect->u.rumble.strong_magnitude;
if (!level)
level = effect->u.rumble.weak_magnitude;

/* If level does not change, don't restart playback */
- if (haptics->level == level)
+ if (READ_ONCE(haptics->level) == level)
return 0;

- haptics->level = level;
+ WRITE_ONCE(haptics->level, level);

schedule_work(&haptics->play_work);

return 0;
}

-static int aw86927_play_sine(struct aw86927_data *haptics)
+static int aw86927_play_sine(struct aw86927_data *haptics, u16 level)
{
int err;

@@ -425,7 +436,8 @@ static int aw86927_play_sine(struct aw86927_data *haptics)
if (err)
return err;

- err = regmap_write(haptics->regmap, AW86927_PLAYCFG2_REG, haptics->level * 0x80 / 0xffff);
+ err = regmap_write(haptics->regmap, AW86927_PLAYCFG2_REG,
+ level * 0x80 / 0xffff);
if (err)
return err;

@@ -437,12 +449,33 @@ static int aw86927_play_sine(struct aw86927_data *haptics)
return 0;
}

+static int aw86927_open(struct input_dev *input)
+{
+ struct aw86927_data *haptics = input_get_drvdata(input);
+
+ WRITE_ONCE(haptics->enabled, true);
+
+ return 0;
+}
+
static void aw86927_close(struct input_dev *input)
{
struct aw86927_data *haptics = input_get_drvdata(input);
struct device *dev = &haptics->client->dev;
int err;

+ /*
+ * Stop accepting effects before draining the work: the memless effect
+ * timer runs from a softirq, and clearing the level without blocking
+ * aw86927_haptics_play() would let the next tick see a level change and
+ * queue play_work again. input core calls close() with
+ * input_dev->mutex held, so this cannot interleave with a new open().
+ */
+ scoped_guard(spinlock_irq, &input->event_lock) {
+ WRITE_ONCE(haptics->enabled, false);
+ WRITE_ONCE(haptics->level, 0);
+ }
+
cancel_work_sync(&haptics->play_work);

err = aw86927_stop(haptics);
@@ -455,10 +488,11 @@ static void aw86927_haptics_play_work(struct work_struct *work)
struct aw86927_data *haptics =
container_of(work, struct aw86927_data, play_work);
struct device *dev = &haptics->client->dev;
+ u16 level = READ_ONCE(haptics->level);
int err;

- if (haptics->level)
- err = aw86927_play_sine(haptics);
+ if (level)
+ err = aw86927_play_sine(haptics, level);
else
err = aw86927_stop(haptics);

@@ -800,6 +834,20 @@ static int aw86927_detect(struct aw86927_data *haptics)
return 0;
}

+static void aw86927_reset(void *data)
+{
+ struct aw86927_data *haptics = data;
+
+ /*
+ * Drain the worker before the regmap can go away: this action also runs
+ * from devres on unbind, where input_close_device() may have been skipped
+ * (inhibited device) and the memless effect timer can have queued the
+ * work again.
+ */
+ disable_work_sync(&haptics->play_work);
+ gpiod_set_value_cansleep(haptics->reset_gpio, 1);
+}
+
static int aw86927_probe(struct i2c_client *client)
{
struct aw86927_data *haptics;
@@ -828,6 +876,17 @@ static int aw86927_probe(struct i2c_client *client)
return dev_err_probe(haptics->dev, PTR_ERR(haptics->reset_gpio),
"Failed to get reset gpio\n");

+ /*
+ * The work item must exist before the devres action below can be run: any
+ * later failure in probe makes devres call aw86927_reset(), which drains
+ * play_work.
+ */
+ INIT_WORK(&haptics->play_work, aw86927_haptics_play_work);
+
+ err = devm_add_action_or_reset(haptics->dev, aw86927_reset, haptics);
+ if (err)
+ return err;
+
/* Hardware reset */
aw86927_hw_reset(haptics);

@@ -865,9 +924,8 @@ static int aw86927_probe(struct i2c_client *client)
if (err)
return dev_err_probe(haptics->dev, err, "Failed to request threaded irq\n");

- INIT_WORK(&haptics->play_work, aw86927_haptics_play_work);
-
haptics->input_dev->name = "aw86927-haptics";
+ haptics->input_dev->open = aw86927_open;
haptics->input_dev->close = aw86927_close;

input_set_drvdata(haptics->input_dev, haptics);
@@ -899,6 +957,50 @@ static int aw86927_probe(struct i2c_client *client)
return 0;
}

+static void aw86927_shutdown(struct i2c_client *client)
+{
+ struct aw86927_data *haptics = i2c_get_clientdata(client);
+
+ aw86927_reset(haptics);
+}
+
+static int aw86927_suspend(struct device *dev)
+{
+ struct aw86927_data *haptics = dev_get_drvdata(dev);
+ int err;
+
+ guard(mutex)(&haptics->input_dev->mutex);
+
+ /* Block events from the memless effect timer before draining the work. */
+ scoped_guard(spinlock_irq, &haptics->input_dev->event_lock)
+ haptics->suspended = true;
+
+ cancel_work_sync(&haptics->play_work);
+ err = aw86927_stop(haptics);
+
+ scoped_guard(spinlock_irq, &haptics->input_dev->event_lock) {
+ if (err)
+ haptics->suspended = false;
+ else
+ WRITE_ONCE(haptics->level, 0);
+ }
+
+ return err;
+}
+
+static int aw86927_resume(struct device *dev)
+{
+ struct aw86927_data *haptics = dev_get_drvdata(dev);
+
+ guard(mutex)(&haptics->input_dev->mutex);
+ guard(spinlock_irq)(&haptics->input_dev->event_lock);
+ haptics->suspended = false;
+
+ return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(aw86927_pm_ops, aw86927_suspend, aw86927_resume);
+
static const struct of_device_id aw86927_of_id[] = {
{ .compatible = "awinic,aw86927" },
{ /* sentinel */ }
@@ -910,8 +1012,10 @@ static struct i2c_driver aw86927_driver = {
.driver = {
.name = "aw86927-haptics",
.of_match_table = aw86927_of_id,
+ .pm = pm_sleep_ptr(&aw86927_pm_ops),
},
.probe = aw86927_probe,
+ .shutdown = aw86927_shutdown,
};

module_i2c_driver(aw86927_driver);
--
2.53.0