@@ -166,6 +166,24 @@ static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,
return ret;
}
+/*
+ * This is a mirror of pwm_lpss_apply() without pm_runtime reference handling
+ * for restoring the PWM state on resume.
+ */
+static int pwm_lpss_restore_state(struct pwm_lpss_chip *lpwm,
+ struct pwm_device *pwm,
+ const struct pwm_state *state)
+{
+ int ret = 0;
+
+ if (state->enabled)
+ ret = pwm_lpss_prepare_enable(lpwm, pwm, state, !pwm_is_enabled(pwm));
+ else if (pwm_is_enabled(pwm))
+ pwm_lpss_write(pwm, pwm_lpss_read(pwm) & ~PWM_ENABLE);
+
+ return ret;
+}
+
static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
struct pwm_state *state)
{
@@ -278,10 +296,35 @@ EXPORT_SYMBOL_GPL(pwm_lpss_suspend);
int pwm_lpss_resume(struct device *dev)
{
struct pwm_lpss_chip *lpwm = dev_get_drvdata(dev);
- int i;
+ struct pwm_state saved_state;
+ struct pwm_device *pwm;
+ int i, ret;
+ u32 ctrl;
- for (i = 0; i < lpwm->info->npwm; i++)
- writel(lpwm->saved_ctrl[i], lpwm->regs + i * PWM_SIZE + PWM);
+ for (i = 0; i < lpwm->info->npwm; i++) {
+ pwm = &lpwm->chip.pwms[i];
+
+ ctrl = pwm_lpss_read(pwm);
+ /* If we did not reach S0i3/S3 the controller keeps its state */
+ if (ctrl == lpwm->saved_ctrl[i])
+ continue;
+
+ /*
+ * We cannot just blindly restore the old value here. Since we
+ * are changing the settings we must set SW_UPDATE and if the
+ * PWM was enabled before we must write the new settings +
+ * PWM_SW_UPDATE before setting PWM_ENABLE. We must also wait
+ * for PWM_SW_UPDATE to become 0 again and depending on the
+ * model we must do this either before or after the setting of
+ * PWM_ENABLE.
+ */
+ saved_state = pwm->state;
+ /* Update enabled to its actual setting after resume */
+ pwm->state.enabled = !!(ctrl & PWM_ENABLE);
+ ret = pwm_lpss_restore_state(lpwm, pwm, &saved_state);
+ if (ret)
+ dev_err(dev, "Error restoring state on resume\n");
+ }
return 0;
}