Commit f07980d4 authored by Tzung-Bi Shih's avatar Tzung-Bi Shih Committed by Mark Brown

drm/mediatek: fix race condition for HDMI jack status reporting

hdmi_conn_detect and mtk_hdmi_audio_hook_plugged_cb would be called
by different threads.

Imaging the following calling sequence:
           Thread A                            Thread B
--------------------------------------------------------------------
mtk_hdmi_audio_hook_plugged_cb()
mtk_cec_hpd_high() -> disconnected
                                     hdmi_conn_detect()
                                     mtk_cec_hpd_high() -> connected
                                     plugged_cb(connected)
plugged_cb(disconnected)

The latest disconnected is false reported.  Makes mtk_cec_hpd_high
and plugged_cb atomic to fix.

Also uses the same lock to protect read/write of plugged_cb and codec_dev.

Fixes: 5d3c6447 ("drm/mediatek: support HDMI jack status reporting")
Signed-off-by: default avatarTzung-Bi Shih <tzungbi@google.com>
Acked-by: default avatarCK Hu <ck.hu@mediatek.com>
Link: https://lore.kernel.org/r/20200217105513.2.I477092c2f104fd589133436c3ae4590e6fc6323b@changeidSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 2f0b4203
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <linux/io.h> #include <linux/io.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/mfd/syscon.h> #include <linux/mfd/syscon.h>
#include <linux/mutex.h>
#include <linux/of_platform.h> #include <linux/of_platform.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/of_gpio.h> #include <linux/of_gpio.h>
...@@ -171,6 +172,7 @@ struct mtk_hdmi { ...@@ -171,6 +172,7 @@ struct mtk_hdmi {
bool enabled; bool enabled;
hdmi_codec_plugged_cb plugged_cb; hdmi_codec_plugged_cb plugged_cb;
struct device *codec_dev; struct device *codec_dev;
struct mutex update_plugged_status_lock;
}; };
static inline struct mtk_hdmi *hdmi_ctx_from_bridge(struct drm_bridge *b) static inline struct mtk_hdmi *hdmi_ctx_from_bridge(struct drm_bridge *b)
...@@ -1199,10 +1201,13 @@ static void mtk_hdmi_clk_disable_audio(struct mtk_hdmi *hdmi) ...@@ -1199,10 +1201,13 @@ static void mtk_hdmi_clk_disable_audio(struct mtk_hdmi *hdmi)
static enum drm_connector_status static enum drm_connector_status
mtk_hdmi_update_plugged_status(struct mtk_hdmi *hdmi) mtk_hdmi_update_plugged_status(struct mtk_hdmi *hdmi)
{ {
bool connected = mtk_cec_hpd_high(hdmi->cec_dev); bool connected;
mutex_lock(&hdmi->update_plugged_status_lock);
connected = mtk_cec_hpd_high(hdmi->cec_dev);
if (hdmi->plugged_cb && hdmi->codec_dev) if (hdmi->plugged_cb && hdmi->codec_dev)
hdmi->plugged_cb(hdmi->codec_dev, connected); hdmi->plugged_cb(hdmi->codec_dev, connected);
mutex_unlock(&hdmi->update_plugged_status_lock);
return connected ? return connected ?
connector_status_connected : connector_status_disconnected; connector_status_connected : connector_status_disconnected;
...@@ -1669,8 +1674,11 @@ static int mtk_hdmi_audio_hook_plugged_cb(struct device *dev, void *data, ...@@ -1669,8 +1674,11 @@ static int mtk_hdmi_audio_hook_plugged_cb(struct device *dev, void *data,
{ {
struct mtk_hdmi *hdmi = data; struct mtk_hdmi *hdmi = data;
mutex_lock(&hdmi->update_plugged_status_lock);
hdmi->plugged_cb = fn; hdmi->plugged_cb = fn;
hdmi->codec_dev = codec_dev; hdmi->codec_dev = codec_dev;
mutex_unlock(&hdmi->update_plugged_status_lock);
mtk_hdmi_update_plugged_status(hdmi); mtk_hdmi_update_plugged_status(hdmi);
return 0; return 0;
...@@ -1729,6 +1737,7 @@ static int mtk_drm_hdmi_probe(struct platform_device *pdev) ...@@ -1729,6 +1737,7 @@ static int mtk_drm_hdmi_probe(struct platform_device *pdev)
return ret; return ret;
} }
mutex_init(&hdmi->update_plugged_status_lock);
platform_set_drvdata(pdev, hdmi); platform_set_drvdata(pdev, hdmi);
ret = mtk_hdmi_output_init(hdmi); ret = mtk_hdmi_output_init(hdmi);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment