[PATCH] pwm: meson: Restore the registers after hibernation

From: Igor Velkov via B4 Relay

Date: Thu Nov 19 2026 - 22:48:01 EST


From: Igor Velkov <iav@xxxxxx>

When a hibernation image is restored after the system was powered off,
the PWM registers are reset. The channel clocks (mux, divider and gate)
are fields of REG_MISC_AB, and the clock framework still counts the gate
as enabled and the rate as set, so none of these fields is written
again. pwm-fan reconfigures its channel on resume, but that rewrites
only the counters and the enable bits, not the clock fields, and the
PWM core skips an unchanged state altogether. On ODROID-HC4 the fan
stays stopped after the restore.

Save REG_PWM_A, REG_PWM_B and REG_MISC_AB on suspend and write them back
on resume. pwm_AO_cd, which drives the VDDCPU regulator on this board,
is restored the same way.

Tested on ODROID-HC4: after two restores following a power cut, the fan
follows both manual pwm1 settings and the thermal control.

Signed-off-by: Igor Velkov <iav@xxxxxx>
---
Tested on a v7.3.5 based kernel, before the waveform API conversion, which did not change the clock handling relevant here; build-tested on pwm/for-next.
---
drivers/pwm/pwm-meson.c | 43 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)

diff --git a/drivers/pwm/pwm-meson.c b/drivers/pwm/pwm-meson.c
index 633b384358c0..c8fec42ae414 100644
--- a/drivers/pwm/pwm-meson.c
+++ b/drivers/pwm/pwm-meson.c
@@ -119,6 +119,8 @@ struct meson_pwm {
* that is shared between the two PWMs.
*/
spinlock_t lock;
+ u32 saved_pwm[MESON_NUM_PWMS];
+ u32 saved_misc;
};

static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
@@ -732,13 +734,54 @@ static int meson_pwm_probe(struct platform_device *pdev)
return dev_err_probe(&pdev->dev, err,
"failed to register PWM chip\n");

+ platform_set_drvdata(pdev, chip);
+
+ return 0;
+}
+
+/*
+ * After a power loss the registers are reset, but the PWM core and the clock
+ * framework keep their state and skip unchanged writes.
+ */
+static int meson_pwm_suspend(struct device *dev)
+{
+ struct pwm_chip *chip = dev_get_drvdata(dev);
+ struct meson_pwm *meson = to_meson_pwm(chip);
+ unsigned int i;
+
+ for (i = 0; i < MESON_NUM_PWMS; i++)
+ meson->saved_pwm[i] = readl(meson->base +
+ meson_pwm_per_channel_data[i].reg_offset);
+ meson->saved_misc = readl(meson->base + REG_MISC_AB);
+
return 0;
}

+static int meson_pwm_resume(struct device *dev)
+{
+ struct pwm_chip *chip = dev_get_drvdata(dev);
+ struct meson_pwm *meson = to_meson_pwm(chip);
+ unsigned long flags;
+ unsigned int i;
+
+ spin_lock_irqsave(&meson->lock, flags);
+ for (i = 0; i < MESON_NUM_PWMS; i++)
+ writel(meson->saved_pwm[i], meson->base +
+ meson_pwm_per_channel_data[i].reg_offset);
+ writel(meson->saved_misc, meson->base + REG_MISC_AB);
+ spin_unlock_irqrestore(&meson->lock, flags);
+
+ return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(meson_pwm_pm_ops, meson_pwm_suspend,
+ meson_pwm_resume);
+
static struct platform_driver meson_pwm_driver = {
.driver = {
.name = "meson-pwm",
.of_match_table = meson_pwm_matches,
+ .pm = pm_sleep_ptr(&meson_pwm_pm_ops),
},
.probe = meson_pwm_probe,
};

---
base-commit: 3f9bd3ccc80af2684cfc0901757f85a78358cbab
change-id: 20261120-pwm-meson-hib-a8d9eb52de2a

Best regards,
--
Igor Velkov <iav@xxxxxx>