From 8d9cb36fc74da909c1514a89b319c95b9253055c Mon Sep 17 00:00:00 2001
From: Luke Lu <luke.lu@libre.computer>
Date: Wed, 13 Dec 2023 03:47:44 +0000
Subject: [PATCH 49/58] WIP: drm/meson: poweron/off dw_hdmi only if dw_hdmi
 enabled

dw_hdmi_poweron() assumes that hdmi->curr_conn is valid. Calling
dw_hdmi_poweron() in meson_dw_hdmi_pm_resume() only with the bridge
enabled ensures this, avoiding invalid memory access when resuming
with HDMI port disconnected.

dw_hdmi_poweroff() is called to maintain the internal state of dw_hdmi.
This is only needed when dw_hdmi_poweron() will be called later, i.e.
dw_hdmi is enabled.
---
 drivers/gpu/drm/bridge/synopsys/dw-hdmi.c | 6 ++++++
 drivers/gpu/drm/meson/meson_dw_hdmi.c     | 7 +++++--
 include/drm/bridge/dw_hdmi.h              | 1 +
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c b/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
index 189a9abe2035..c4a60be40603 100644
--- a/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
+++ b/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
@@ -3642,6 +3642,12 @@ void dw_hdmi_resume(struct dw_hdmi *hdmi)
 }
 EXPORT_SYMBOL_GPL(dw_hdmi_resume);
 
+bool dw_hdmi_is_bridge_on(struct dw_hdmi *hdmi)
+{
+	return !hdmi->disabled;
+}
+EXPORT_SYMBOL_GPL(dw_hdmi_is_bridge_on);
+
 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
 MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
 MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
diff --git a/drivers/gpu/drm/meson/meson_dw_hdmi.c b/drivers/gpu/drm/meson/meson_dw_hdmi.c
index eaee6e750550..a91cedbeed3d 100644
--- a/drivers/gpu/drm/meson/meson_dw_hdmi.c
+++ b/drivers/gpu/drm/meson/meson_dw_hdmi.c
@@ -823,7 +823,8 @@ static int __maybe_unused meson_dw_hdmi_pm_suspend(struct device *dev)
 	meson_dw_hdmi->data->top_write(meson_dw_hdmi,
 				       HDMITX_TOP_SW_RESET, 0);
 
-	dw_hdmi_poweroff(meson_dw_hdmi->hdmi);
+	if (dw_hdmi_is_bridge_on(meson_dw_hdmi->hdmi))
+		dw_hdmi_poweroff(meson_dw_hdmi->hdmi);
 
 	return 0;
 }
@@ -837,7 +838,9 @@ static int __maybe_unused meson_dw_hdmi_pm_resume(struct device *dev)
 
 	meson_dw_hdmi_init(meson_dw_hdmi);
 
-	dw_hdmi_poweron(meson_dw_hdmi->hdmi);
+	if (dw_hdmi_is_bridge_on(meson_dw_hdmi->hdmi))
+		dw_hdmi_poweron(meson_dw_hdmi->hdmi);
+
 	dw_hdmi_resume(meson_dw_hdmi->hdmi);
 
 	return 0;
diff --git a/include/drm/bridge/dw_hdmi.h b/include/drm/bridge/dw_hdmi.h
index 5eac26a51245..e57d61009645 100644
--- a/include/drm/bridge/dw_hdmi.h
+++ b/include/drm/bridge/dw_hdmi.h
@@ -174,6 +174,7 @@ struct dw_hdmi *dw_hdmi_bind(struct platform_device *pdev,
 void dw_hdmi_poweron(struct dw_hdmi *hdmi);
 void dw_hdmi_poweroff(struct dw_hdmi *hdmi);
 void dw_hdmi_resume(struct dw_hdmi *hdmi);
+bool dw_hdmi_is_bridge_on(struct dw_hdmi *hdmi);
 
 void dw_hdmi_setup_rx_sense(struct dw_hdmi *hdmi, bool hpd, bool rx_sense);
 
-- 
2.34.1

