[PATCH v2] Input: lm8323 - drain PWM work and finish with outputs disabled

From: Hongyan Xu

Date: Sun Nov 15 2026 - 06:49:36 EST


A PWM brightness callback can leave private work pending when the
managed LED and chip state are released. Merely cancelling that
work also discards the final LED_OFF requested by LED unregister.
Flushing alone is insufficient if an earlier fade is still
running: the worker waits for an IRQ to requeue it, and the
managed IRQ is released before the LEDs during teardown.

Install cleanup before registering each LED. Once LED unregister
and IRQ teardown have stopped the producers, cancel its work and
use the documented single-END shutdown sequence: put END with
RESET set in unused script slot 59, stop the channel and restart
at that slot. Do not overwrite a running brightness script. The
engine then disables its output autonomously without another IRQ
or a worker accessing freed driver data. A current long RAMP can
delay completion, as documented by the device specification.

Found by the author's in-house static analysis tool.

Fixes: a48b2d4a0091 ("Input: introduce lm8323 keypad driver")
Cc: stable@xxxxxxxxxxxxxxx
Link: https://lore.kernel.org/all/20260728065518.489-1-getshell@xxxxxxxxxx/
Link: https://www.ti.com/lit/ds/symlink/lm8323.pdf
Signed-off-by: Hongyan Xu <getshell@xxxxxxxxxx>
Signed-off-by: Zehao Gao <gaozehao@xxxxxxxxx>
---

Discovery: found by the author's in-house static analysis tool.

drivers/input/keyboard/lm8323.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)

diff --git a/drivers/input/keyboard/lm8323.c b/drivers/input/keyboard/lm8323.c
index 4b87d39..e13bcde 100644
--- a/drivers/input/keyboard/lm8323.c
+++ b/drivers/input/keyboard/lm8323.c
@@ -548,6 +548,21 @@ static struct attribute *lm8323_pwm_attrs[] = {
};
ATTRIBUTE_GROUPS(lm8323_pwm);

+#define LM8323_PWM_SHUTDOWN_ADDR 59
+
+static void lm8323_pwm_shutdown(void *data)
+{
+ struct lm8323_pwm *pwm = data;
+
+ cancel_work_sync(&pwm->work);
+
+ /* The normal brightness scripts use only slots 0 through 3. */
+ lm8323_write_pwm_one(pwm, LM8323_PWM_SHUTDOWN_ADDR, PWM_END(1));
+ lm8323_write(pwm->chip, 2, LM8323_CMD_STOP_PWM, pwm->id);
+ lm8323_write(pwm->chip, 2, LM8323_CMD_START_PWM,
+ (LM8323_PWM_SHUTDOWN_ADDR << 2) | pwm->id);
+}
+
static int init_pwm(struct lm8323_chip *lm, int id, struct device *dev,
const char *name)
{
@@ -569,6 +584,10 @@ static int init_pwm(struct lm8323_chip *lm, int id, struct device *dev,
pwm->chip = lm;

if (name) {
+ err = devm_add_action_or_reset(dev, lm8323_pwm_shutdown, pwm);
+ if (err)
+ return err;
+
pwm->cdev.name = name;
pwm->cdev.brightness_set = lm8323_pwm_set_brightness;
pwm->cdev.groups = lm8323_pwm_groups;
--
2.50.1.windows.1