1 // SPDX-License-Identifier: GPL-2.0+
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/slab.h>
10 #include <drm/drm_atomic_state_helper.h>
11 #include <drm/drm_bridge.h>
12 #include <drm/drm_bridge_connector.h>
13 #include <drm/drm_connector.h>
14 #include <drm/drm_device.h>
15 #include <drm/drm_edid.h>
16 #include <drm/drm_modeset_helper_vtables.h>
17 #include <drm/drm_probe_helper.h>
22 * The DRM bridge connector helper object provides a DRM connector
23 * implementation that wraps a chain of &struct drm_bridge. The connector
24 * operations are fully implemented based on the operations of the bridges in
25 * the chain, and don't require any intervention from the display controller
28 * To use the helper, display controller drivers create a bridge connector with
29 * a call to drm_bridge_connector_init(). This associates the newly created
30 * connector with the chain of bridges passed to the function and registers it
31 * with the DRM device. At that point the connector becomes fully usable, no
32 * further operation is needed.
34 * The DRM bridge connector operations are implemented based on the operations
35 * provided by the bridges in the chain. Each connector operation is delegated
36 * to the bridge closest to the connector (at the end of the chain) that
37 * provides the relevant functionality.
39 * To make use of this helper, all bridges in the chain shall report bridge
40 * operation flags (&drm_bridge->ops) and bridge output type
41 * (&drm_bridge->type), as well as the DRM_BRIDGE_ATTACH_NO_CONNECTOR attach
42 * flag (none of the bridges shall create a DRM connector directly).
46 * struct drm_bridge_connector - A connector backed by a chain of bridges
48 struct drm_bridge_connector {
50 * @base: The base DRM connector
52 struct drm_connector base;
56 * The encoder at the start of the bridges chain.
58 struct drm_encoder *encoder;
62 * The last bridge in the chain (closest to the connector) that provides
63 * EDID read support, if any (see &DRM_BRIDGE_OP_EDID).
65 struct drm_bridge *bridge_edid;
69 * The last bridge in the chain (closest to the connector) that provides
70 * hot-plug detection notification, if any (see &DRM_BRIDGE_OP_HPD).
72 struct drm_bridge *bridge_hpd;
76 * The last bridge in the chain (closest to the connector) that provides
77 * connector detection, if any (see &DRM_BRIDGE_OP_DETECT).
79 struct drm_bridge *bridge_detect;
83 * The last bridge in the chain (closest to the connector) that provides
84 * connector modes detection, if any (see &DRM_BRIDGE_OP_MODES).
86 struct drm_bridge *bridge_modes;
89 #define to_drm_bridge_connector(x) \
90 container_of(x, struct drm_bridge_connector, base)
92 /* -----------------------------------------------------------------------------
93 * Bridge Connector Hot-Plug Handling
96 static void drm_bridge_connector_hpd_notify(struct drm_connector *connector,
97 enum drm_connector_status status)
99 struct drm_bridge_connector *bridge_connector =
100 to_drm_bridge_connector(connector);
101 struct drm_bridge *bridge;
103 /* Notify all bridges in the pipeline of hotplug events. */
104 drm_for_each_bridge_in_chain(bridge_connector->encoder, bridge) {
105 if (bridge->funcs->hpd_notify)
106 bridge->funcs->hpd_notify(bridge, status);
110 static void drm_bridge_connector_hpd_cb(void *cb_data,
111 enum drm_connector_status status)
113 struct drm_bridge_connector *drm_bridge_connector = cb_data;
114 struct drm_connector *connector = &drm_bridge_connector->base;
115 struct drm_device *dev = connector->dev;
116 enum drm_connector_status old_status;
118 mutex_lock(&dev->mode_config.mutex);
119 old_status = connector->status;
120 connector->status = status;
121 mutex_unlock(&dev->mode_config.mutex);
123 if (old_status == status)
126 drm_bridge_connector_hpd_notify(connector, status);
128 drm_kms_helper_hotplug_event(dev);
132 * drm_bridge_connector_enable_hpd - Enable hot-plug detection for the connector
133 * @connector: The DRM bridge connector
135 * This function enables hot-plug detection for the given bridge connector.
136 * This is typically used by display drivers in their resume handler.
138 void drm_bridge_connector_enable_hpd(struct drm_connector *connector)
140 struct drm_bridge_connector *bridge_connector =
141 to_drm_bridge_connector(connector);
142 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
145 drm_bridge_hpd_enable(hpd, drm_bridge_connector_hpd_cb,
148 EXPORT_SYMBOL_GPL(drm_bridge_connector_enable_hpd);
151 * drm_bridge_connector_disable_hpd - Disable hot-plug detection for the
153 * @connector: The DRM bridge connector
155 * This function disables hot-plug detection for the given bridge connector.
156 * This is typically used by display drivers in their suspend handler.
158 void drm_bridge_connector_disable_hpd(struct drm_connector *connector)
160 struct drm_bridge_connector *bridge_connector =
161 to_drm_bridge_connector(connector);
162 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
165 drm_bridge_hpd_disable(hpd);
167 EXPORT_SYMBOL_GPL(drm_bridge_connector_disable_hpd);
169 /* -----------------------------------------------------------------------------
170 * Bridge Connector Functions
173 static enum drm_connector_status
174 drm_bridge_connector_detect(struct drm_connector *connector, bool force)
176 struct drm_bridge_connector *bridge_connector =
177 to_drm_bridge_connector(connector);
178 struct drm_bridge *detect = bridge_connector->bridge_detect;
179 enum drm_connector_status status;
182 status = detect->funcs->detect(detect);
184 drm_bridge_connector_hpd_notify(connector, status);
186 switch (connector->connector_type) {
187 case DRM_MODE_CONNECTOR_DPI:
188 case DRM_MODE_CONNECTOR_LVDS:
189 case DRM_MODE_CONNECTOR_DSI:
190 status = connector_status_connected;
193 status = connector_status_unknown;
201 static void drm_bridge_connector_destroy(struct drm_connector *connector)
203 struct drm_bridge_connector *bridge_connector =
204 to_drm_bridge_connector(connector);
206 if (bridge_connector->bridge_hpd) {
207 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
209 drm_bridge_hpd_disable(hpd);
212 drm_connector_unregister(connector);
213 drm_connector_cleanup(connector);
215 kfree(bridge_connector);
218 static const struct drm_connector_funcs drm_bridge_connector_funcs = {
219 .reset = drm_atomic_helper_connector_reset,
220 .detect = drm_bridge_connector_detect,
221 .fill_modes = drm_helper_probe_single_connector_modes,
222 .destroy = drm_bridge_connector_destroy,
223 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
224 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
227 /* -----------------------------------------------------------------------------
228 * Bridge Connector Helper Functions
231 static int drm_bridge_connector_get_modes_edid(struct drm_connector *connector,
232 struct drm_bridge *bridge)
234 enum drm_connector_status status;
238 status = drm_bridge_connector_detect(connector, false);
239 if (status != connector_status_connected)
242 edid = bridge->funcs->get_edid(bridge, connector);
243 if (!edid || !drm_edid_is_valid(edid)) {
248 drm_connector_update_edid_property(connector, edid);
249 n = drm_add_edid_modes(connector, edid);
255 drm_connector_update_edid_property(connector, NULL);
259 static int drm_bridge_connector_get_modes(struct drm_connector *connector)
261 struct drm_bridge_connector *bridge_connector =
262 to_drm_bridge_connector(connector);
263 struct drm_bridge *bridge;
266 * If display exposes EDID, then we parse that in the normal way to
267 * build table of supported modes.
269 bridge = bridge_connector->bridge_edid;
271 return drm_bridge_connector_get_modes_edid(connector, bridge);
274 * Otherwise if the display pipeline reports modes (e.g. with a fixed
275 * resolution panel or an analog TV output), query it.
277 bridge = bridge_connector->bridge_modes;
279 return bridge->funcs->get_modes(bridge, connector);
282 * We can't retrieve modes, which can happen for instance for a DVI or
283 * VGA output with the DDC bus unconnected. The KMS core will add the
289 static const struct drm_connector_helper_funcs drm_bridge_connector_helper_funcs = {
290 .get_modes = drm_bridge_connector_get_modes,
291 /* No need for .mode_valid(), the bridges are checked by the core. */
294 /* -----------------------------------------------------------------------------
295 * Bridge Connector Initialisation
299 * drm_bridge_connector_init - Initialise a connector for a chain of bridges
300 * @drm: the DRM device
301 * @encoder: the encoder where the bridge chain starts
303 * Allocate, initialise and register a &drm_bridge_connector with the @drm
304 * device. The connector is associated with a chain of bridges that starts at
305 * the @encoder. All bridges in the chain shall report bridge operation flags
306 * (&drm_bridge->ops) and bridge output type (&drm_bridge->type), and none of
307 * them may create a DRM connector directly.
309 * Returns a pointer to the new connector on success, or a negative error
312 struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
313 struct drm_encoder *encoder)
315 struct drm_bridge_connector *bridge_connector;
316 struct drm_connector *connector;
317 struct i2c_adapter *ddc = NULL;
318 struct drm_bridge *bridge;
321 bridge_connector = kzalloc(sizeof(*bridge_connector), GFP_KERNEL);
322 if (!bridge_connector)
323 return ERR_PTR(-ENOMEM);
325 bridge_connector->encoder = encoder;
328 * TODO: Handle doublescan_allowed, stereo_allowed and
331 connector = &bridge_connector->base;
332 connector->interlace_allowed = true;
335 * Initialise connector status handling. First locate the furthest
336 * bridges in the pipeline that support HPD and output detection. Then
337 * initialise the connector polling mode, using HPD if available and
338 * falling back to polling if supported. If neither HPD nor output
339 * detection are available, we don't support hotplug detection at all.
341 connector_type = DRM_MODE_CONNECTOR_Unknown;
342 drm_for_each_bridge_in_chain(encoder, bridge) {
343 if (!bridge->interlace_allowed)
344 connector->interlace_allowed = false;
346 if (bridge->ops & DRM_BRIDGE_OP_EDID)
347 bridge_connector->bridge_edid = bridge;
348 if (bridge->ops & DRM_BRIDGE_OP_HPD)
349 bridge_connector->bridge_hpd = bridge;
350 if (bridge->ops & DRM_BRIDGE_OP_DETECT)
351 bridge_connector->bridge_detect = bridge;
352 if (bridge->ops & DRM_BRIDGE_OP_MODES)
353 bridge_connector->bridge_modes = bridge;
355 if (!drm_bridge_get_next_bridge(bridge))
356 connector_type = bridge->type;
362 if (connector_type == DRM_MODE_CONNECTOR_Unknown) {
363 kfree(bridge_connector);
364 return ERR_PTR(-EINVAL);
367 drm_connector_init_with_ddc(drm, connector, &drm_bridge_connector_funcs,
368 connector_type, ddc);
369 drm_connector_helper_add(connector, &drm_bridge_connector_helper_funcs);
371 if (bridge_connector->bridge_hpd)
372 connector->polled = DRM_CONNECTOR_POLL_HPD;
373 else if (bridge_connector->bridge_detect)
374 connector->polled = DRM_CONNECTOR_POLL_CONNECT
375 | DRM_CONNECTOR_POLL_DISCONNECT;
379 EXPORT_SYMBOL_GPL(drm_bridge_connector_init);