[PATCH v4 14/32] drm/bridge: ti-sn65dsi86: protect device resources on unplug

From: Luca Ceresoli

Date: Wed Nov 25 2026 - 12:53:04 EST


To support hot-unplug of this bridge we need to protect access to device
resources in case device removal happens concurrently to other code.

Some care is needed for the case when drm_bridge_unplug() happens before
ti_sn_bridge_atomic_post_disable(), so the latter cannot release supplier
resources taken by ti_sn_bridge_atomic_pre_enable(): runtime PM (and in
turn regulators) and refclk.

To ensure those resources are released exactly once on device removal:

* move the code to release them to a dedicated function
* register that function when the resources are taken in
ti_sn_bridge_atomic_pre_enable()
* if ti_sn_bridge_atomic_post_disable() happens before drm_bridge_unplug()
(typical non-hot-unplug case):
* ti_sn_bridge_atomic_post_disable() can enter the critical section
(drm_bridge_enter() returns 0) -> it releases and executes the
devres action
* if ti_sn_bridge_atomic_post_disable() happens after drm_bridge_unplug()
(typical hot-unplug case):
* ti_sn_bridge_remove() -> drm_bridge_unplug() defuses
ti_sn_bridge_atomic_post_disable() so it cannot execute the devres
action
* the devres action is executed just after ti_sn_bridge_remove()

Signed-off-by: Luca Ceresoli <luca.ceresoli@xxxxxxxxxxx>

---

Changes in v4:
- protect from register access in ti_sn_bridge_detect() and
ti_sn_bridge_edid_read() too

New patch in v3

This commit basically replicates the same change done on the ti-sn65dsi83
driver, now upstream:
https://lore.kernel.org/all/20251112-drm-bridge-atomic-vs-remove-v3-2-85db717ce094@xxxxxxxxxxx/
---
drivers/gpu/drm/bridge/ti-sn65dsi86.c | 74 +++++++++++++++++++++++++++++++----
1 file changed, 67 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/bridge/ti-sn65dsi86.c b/drivers/gpu/drm/bridge/ti-sn65dsi86.c
index 48b83df9aed6..38318113a534 100644
--- a/drivers/gpu/drm/bridge/ti-sn65dsi86.c
+++ b/drivers/gpu/drm/bridge/ti-sn65dsi86.c
@@ -827,9 +827,15 @@ static void ti_sn_bridge_atomic_disable(struct drm_bridge *bridge,
struct drm_atomic_commit *state)
{
struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+ int idx;
+
+ if (!drm_bridge_enter(bridge, &idx))
+ return;

/* disable video stream */
regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE, 0);
+
+ drm_bridge_exit(idx);
}

static void ti_sn_bridge_set_dsi_rate(struct ti_sn65dsi86 *pdata,
@@ -1099,11 +1105,16 @@ static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,
int ret = -EINVAL;
int max_dp_lanes;
unsigned int bpp;
+ int idx;
+
+ if (!drm_bridge_enter(bridge, &idx))
+ return;

connector = drm_atomic_get_new_connector_for_encoder(state,
bridge->encoder);
if (!connector) {
dev_err_ratelimited(pdata->dev, "Could not get the connector\n");
+ drm_bridge_exit(idx);
return;
}

@@ -1167,6 +1178,7 @@ static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,
}
if (ret) {
DRM_DEV_ERROR(pdata->dev, "%s (%d)\n", last_err_str, ret);
+ drm_bridge_exit(idx);
return;
}

@@ -1176,26 +1188,57 @@ static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,
/* enable video stream */
regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE,
VSTREAM_ENABLE);
+
+ drm_bridge_exit(idx);
+}
+
+/*
+ * Release resources taken by ti_sn_bridge_atomic_pre_enable().
+ *
+ * Invoked by ti_sn_bridge_atomic_post_disable() normally, or by devres
+ * after ti_sn_bridge_remove() in case this happens befora
+ * atomic_post_disable.
+ */
+static void ti_sn_bridge_release_resources(void *data)
+{
+ struct ti_sn65dsi86 *pdata = (struct ti_sn65dsi86 *)data;
+
+ if (!pdata->refclk)
+ ti_sn65dsi86_disable_comms(pdata);
+
+ pm_runtime_put_sync(pdata->dev);
}

static void ti_sn_bridge_atomic_pre_enable(struct drm_bridge *bridge,
struct drm_atomic_commit *state)
{
struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+ int idx;
+
+ if (!drm_bridge_enter(bridge, &idx))
+ return;

pm_runtime_get_sync(pdata->dev);

if (!pdata->refclk)
ti_sn65dsi86_enable_comms(pdata, state);

+ devm_add_action(pdata->dev, ti_sn_bridge_release_resources, pdata);
+
/* td7: min 100 us after enable before DSI data */
usleep_range(100, 110);
+
+ drm_bridge_exit(idx);
}

static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,
struct drm_atomic_commit *state)
{
struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+ int idx;
+
+ if (!drm_bridge_enter(bridge, &idx))
+ return;

/* semi auto link training mode OFF */
regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0);
@@ -1204,17 +1247,21 @@ static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,
/* disable DP PLL */
regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);

- if (!pdata->refclk)
- ti_sn65dsi86_disable_comms(pdata);
+ devm_release_action(pdata->dev, ti_sn_bridge_release_resources, pdata);

- pm_runtime_put_sync(pdata->dev);
+ drm_bridge_exit(idx);
}

static enum drm_connector_status
ti_sn_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
{
struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+ enum drm_connector_status status = connector_status_disconnected;
int val = 0;
+ int idx;
+
+ if (!drm_bridge_enter(bridge, &idx))
+ return status;

/*
* Runtime reference is grabbed in ti_sn_bridge_hpd_enable()
@@ -1225,16 +1272,29 @@ ti_sn_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)

regmap_read(pdata->regmap, SN_HPD_DISABLE_REG, &val);

- return val & HPD_DEBOUNCED_STATE ? connector_status_connected
- : connector_status_disconnected;
+ status = val & HPD_DEBOUNCED_STATE ? connector_status_connected
+ : connector_status_disconnected;
+
+ drm_bridge_exit(idx);
+
+ return status;
}

static const struct drm_edid *ti_sn_bridge_edid_read(struct drm_bridge *bridge,
struct drm_connector *connector)
{
struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);
+ const struct drm_edid *edid;
+ int idx;

- return drm_edid_read_ddc(connector, &pdata->aux.ddc);
+ if (!drm_bridge_enter(bridge, &idx))
+ return NULL;
+
+ edid = drm_edid_read_ddc(connector, &pdata->aux.ddc);
+
+ drm_bridge_exit(idx);
+
+ return edid;
}

static void ti_sn65dsi86_debugfs_init(struct drm_bridge *bridge, struct dentry *root)
@@ -1475,7 +1535,7 @@ static void ti_sn_bridge_remove(struct auxiliary_device *adev)
if (!pdata)
return;

- drm_bridge_remove(&pdata->bridge);
+ drm_bridge_unplug(&pdata->bridge);

of_node_put(pdata->host_node);
}

--
2.56.1