From c8ae813c02fca7c7c43317e894a6516e09012bd8 Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Mon, 15 Aug 2022 02:23:37 +0200
Subject: ASoC: rockchip: Fix doubling of playback speed after system sleep

There is some issue with CRU on RK3399 that can be reproduced by:

- playing some audio and stopping the playback (so that runtime PM suspends I2S)
- putting system to deep sleep and resuming it (using mainline TF-A)
- playing some audio (audio plays at 2x the normal speed)

If the audio is kept playing during system sleep cycle, the issue
does not manifest. Relevant registers in CRU are identical in bug/
no-bug scenarios, so this patch is just touching the CRU registers
without actually changing anything in the end. Touching the registers
fixes the playback speed, though.

Inspired by: https://github.com/djselbeck/linux/commit/cb4be5dec3fa18c4b344c11fed3fc57aa3bea424

Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 sound/soc/rockchip/rockchip_i2s.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/sound/soc/rockchip/rockchip_i2s.c b/sound/soc/rockchip/rockchip_i2s.c
index 4315da4a47c1..b6602eaea194 100644
--- a/sound/soc/rockchip/rockchip_i2s.c
+++ b/sound/soc/rockchip/rockchip_i2s.c
@@ -114,6 +114,13 @@ static int i2s_runtime_resume(struct device *dev)
 	if (ret)
 		clk_disable_unprepare(i2s->mclk);
 
+	if (ret == 0) {
+		unsigned long rate = clk_get_rate(i2s->mclk);
+
+		clk_set_rate(i2s->mclk, rate - 1);
+		clk_set_rate(i2s->mclk, rate);
+	}
+
 	return ret;
 }
 
-- 
2.35.3

