1 // SPDX-License-Identifier: GPL-2.0+
6 #include <linux/kernel.h>
7 #include <linux/module.h>
9 #include <linux/property.h>
10 #include <linux/slab.h>
12 #include <drm/drm_atomic_state_helper.h>
13 #include <drm/drm_bridge.h>
14 #include <drm/drm_bridge_connector.h>
15 #include <drm/drm_connector.h>
16 #include <drm/drm_device.h>
17 #include <drm/drm_edid.h>
18 #include <drm/drm_managed.h>
19 #include <drm/drm_modeset_helper_vtables.h>
20 #include <drm/drm_probe_helper.h>
21 #include <drm/display/drm_hdmi_state_helper.h>
26 * The DRM bridge connector helper object provides a DRM connector
27 * implementation that wraps a chain of &struct drm_bridge. The connector
28 * operations are fully implemented based on the operations of the bridges in
29 * the chain, and don't require any intervention from the display controller
32 * To use the helper, display controller drivers create a bridge connector with
33 * a call to drm_bridge_connector_init(). This associates the newly created
34 * connector with the chain of bridges passed to the function and registers it
35 * with the DRM device. At that point the connector becomes fully usable, no
36 * further operation is needed.
38 * The DRM bridge connector operations are implemented based on the operations
39 * provided by the bridges in the chain. Each connector operation is delegated
40 * to the bridge closest to the connector (at the end of the chain) that
41 * provides the relevant functionality.
43 * To make use of this helper, all bridges in the chain shall report bridge
44 * operation flags (&drm_bridge->ops) and bridge output type
45 * (&drm_bridge->type), as well as the DRM_BRIDGE_ATTACH_NO_CONNECTOR attach
46 * flag (none of the bridges shall create a DRM connector directly).
50 * struct drm_bridge_connector - A connector backed by a chain of bridges
52 struct drm_bridge_connector {
54 * @base: The base DRM connector
56 struct drm_connector base;
60 * The encoder at the start of the bridges chain.
62 struct drm_encoder *encoder;
66 * The last bridge in the chain (closest to the connector) that provides
67 * EDID read support, if any (see &DRM_BRIDGE_OP_EDID).
69 struct drm_bridge *bridge_edid;
73 * The last bridge in the chain (closest to the connector) that provides
74 * hot-plug detection notification, if any (see &DRM_BRIDGE_OP_HPD).
76 struct drm_bridge *bridge_hpd;
80 * The last bridge in the chain (closest to the connector) that provides
81 * connector detection, if any (see &DRM_BRIDGE_OP_DETECT).
83 struct drm_bridge *bridge_detect;
87 * The last bridge in the chain (closest to the connector) that provides
88 * connector modes detection, if any (see &DRM_BRIDGE_OP_MODES).
90 struct drm_bridge *bridge_modes;
94 * The bridge in the chain that implements necessary support for the
95 * HDMI connector infrastructure, if any (see &DRM_BRIDGE_OP_HDMI).
97 struct drm_bridge *bridge_hdmi;
100 #define to_drm_bridge_connector(x) \
101 container_of(x, struct drm_bridge_connector, base)
103 /* -----------------------------------------------------------------------------
104 * Bridge Connector Hot-Plug Handling
107 static void drm_bridge_connector_hpd_notify(struct drm_connector *connector,
108 enum drm_connector_status status)
110 struct drm_bridge_connector *bridge_connector =
111 to_drm_bridge_connector(connector);
112 struct drm_bridge *bridge;
114 /* Notify all bridges in the pipeline of hotplug events. */
115 drm_for_each_bridge_in_chain(bridge_connector->encoder, bridge) {
116 if (bridge->funcs->hpd_notify)
117 bridge->funcs->hpd_notify(bridge, status);
121 static void drm_bridge_connector_handle_hpd(struct drm_bridge_connector *drm_bridge_connector,
122 enum drm_connector_status status)
124 struct drm_connector *connector = &drm_bridge_connector->base;
125 struct drm_device *dev = connector->dev;
127 mutex_lock(&dev->mode_config.mutex);
128 connector->status = status;
129 mutex_unlock(&dev->mode_config.mutex);
131 drm_bridge_connector_hpd_notify(connector, status);
133 drm_kms_helper_connector_hotplug_event(connector);
136 static void drm_bridge_connector_hpd_cb(void *cb_data,
137 enum drm_connector_status status)
139 drm_bridge_connector_handle_hpd(cb_data, status);
142 static void drm_bridge_connector_oob_hotplug_event(struct drm_connector *connector,
143 enum drm_connector_status status)
145 struct drm_bridge_connector *bridge_connector =
146 to_drm_bridge_connector(connector);
148 drm_bridge_connector_handle_hpd(bridge_connector, status);
151 static void drm_bridge_connector_enable_hpd(struct drm_connector *connector)
153 struct drm_bridge_connector *bridge_connector =
154 to_drm_bridge_connector(connector);
155 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
158 drm_bridge_hpd_enable(hpd, drm_bridge_connector_hpd_cb,
162 static void drm_bridge_connector_disable_hpd(struct drm_connector *connector)
164 struct drm_bridge_connector *bridge_connector =
165 to_drm_bridge_connector(connector);
166 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
169 drm_bridge_hpd_disable(hpd);
172 /* -----------------------------------------------------------------------------
173 * Bridge Connector Functions
176 static enum drm_connector_status
177 drm_bridge_connector_detect(struct drm_connector *connector, bool force)
179 struct drm_bridge_connector *bridge_connector =
180 to_drm_bridge_connector(connector);
181 struct drm_bridge *detect = bridge_connector->bridge_detect;
182 enum drm_connector_status status;
185 status = detect->funcs->detect(detect);
187 drm_bridge_connector_hpd_notify(connector, status);
189 switch (connector->connector_type) {
190 case DRM_MODE_CONNECTOR_DPI:
191 case DRM_MODE_CONNECTOR_LVDS:
192 case DRM_MODE_CONNECTOR_DSI:
193 case DRM_MODE_CONNECTOR_eDP:
194 status = connector_status_connected;
197 status = connector_status_unknown;
205 static void drm_bridge_connector_debugfs_init(struct drm_connector *connector,
208 struct drm_bridge_connector *bridge_connector =
209 to_drm_bridge_connector(connector);
210 struct drm_encoder *encoder = bridge_connector->encoder;
211 struct drm_bridge *bridge;
213 list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
214 if (bridge->funcs->debugfs_init)
215 bridge->funcs->debugfs_init(bridge, root);
219 static void drm_bridge_connector_reset(struct drm_connector *connector)
221 struct drm_bridge_connector *bridge_connector =
222 to_drm_bridge_connector(connector);
224 drm_atomic_helper_connector_reset(connector);
225 if (bridge_connector->bridge_hdmi)
226 __drm_atomic_helper_connector_hdmi_reset(connector,
230 static const struct drm_connector_funcs drm_bridge_connector_funcs = {
231 .reset = drm_bridge_connector_reset,
232 .detect = drm_bridge_connector_detect,
233 .fill_modes = drm_helper_probe_single_connector_modes,
234 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
235 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
236 .debugfs_init = drm_bridge_connector_debugfs_init,
237 .oob_hotplug_event = drm_bridge_connector_oob_hotplug_event,
240 /* -----------------------------------------------------------------------------
241 * Bridge Connector Helper Functions
244 static int drm_bridge_connector_get_modes_edid(struct drm_connector *connector,
245 struct drm_bridge *bridge)
247 enum drm_connector_status status;
248 const struct drm_edid *drm_edid;
251 status = drm_bridge_connector_detect(connector, false);
252 if (status != connector_status_connected)
255 drm_edid = drm_bridge_edid_read(bridge, connector);
256 if (!drm_edid_valid(drm_edid)) {
257 drm_edid_free(drm_edid);
261 drm_edid_connector_update(connector, drm_edid);
262 n = drm_edid_connector_add_modes(connector);
264 drm_edid_free(drm_edid);
268 drm_edid_connector_update(connector, NULL);
272 static int drm_bridge_connector_get_modes(struct drm_connector *connector)
274 struct drm_bridge_connector *bridge_connector =
275 to_drm_bridge_connector(connector);
276 struct drm_bridge *bridge;
279 * If display exposes EDID, then we parse that in the normal way to
280 * build table of supported modes.
282 bridge = bridge_connector->bridge_edid;
284 return drm_bridge_connector_get_modes_edid(connector, bridge);
287 * Otherwise if the display pipeline reports modes (e.g. with a fixed
288 * resolution panel or an analog TV output), query it.
290 bridge = bridge_connector->bridge_modes;
292 return bridge->funcs->get_modes(bridge, connector);
295 * We can't retrieve modes, which can happen for instance for a DVI or
296 * VGA output with the DDC bus unconnected. The KMS core will add the
302 static const struct drm_connector_helper_funcs drm_bridge_connector_helper_funcs = {
303 .get_modes = drm_bridge_connector_get_modes,
304 /* No need for .mode_valid(), the bridges are checked by the core. */
305 .enable_hpd = drm_bridge_connector_enable_hpd,
306 .disable_hpd = drm_bridge_connector_disable_hpd,
309 static enum drm_mode_status
310 drm_bridge_connector_tmds_char_rate_valid(const struct drm_connector *connector,
311 const struct drm_display_mode *mode,
312 unsigned long long tmds_rate)
314 struct drm_bridge_connector *bridge_connector =
315 to_drm_bridge_connector(connector);
316 struct drm_bridge *bridge;
318 bridge = bridge_connector->bridge_hdmi;
322 if (bridge->funcs->hdmi_tmds_char_rate_valid)
323 return bridge->funcs->hdmi_tmds_char_rate_valid(bridge, mode, tmds_rate);
328 static int drm_bridge_connector_clear_infoframe(struct drm_connector *connector,
329 enum hdmi_infoframe_type type)
331 struct drm_bridge_connector *bridge_connector =
332 to_drm_bridge_connector(connector);
333 struct drm_bridge *bridge;
335 bridge = bridge_connector->bridge_hdmi;
339 return bridge->funcs->hdmi_clear_infoframe(bridge, type);
342 static int drm_bridge_connector_write_infoframe(struct drm_connector *connector,
343 enum hdmi_infoframe_type type,
344 const u8 *buffer, size_t len)
346 struct drm_bridge_connector *bridge_connector =
347 to_drm_bridge_connector(connector);
348 struct drm_bridge *bridge;
350 bridge = bridge_connector->bridge_hdmi;
354 return bridge->funcs->hdmi_write_infoframe(bridge, type, buffer, len);
357 static const struct drm_connector_hdmi_funcs drm_bridge_connector_hdmi_funcs = {
358 .tmds_char_rate_valid = drm_bridge_connector_tmds_char_rate_valid,
359 .clear_infoframe = drm_bridge_connector_clear_infoframe,
360 .write_infoframe = drm_bridge_connector_write_infoframe,
363 /* -----------------------------------------------------------------------------
364 * Bridge Connector Initialisation
368 * drm_bridge_connector_init - Initialise a connector for a chain of bridges
369 * @drm: the DRM device
370 * @encoder: the encoder where the bridge chain starts
372 * Allocate, initialise and register a &drm_bridge_connector with the @drm
373 * device. The connector is associated with a chain of bridges that starts at
374 * the @encoder. All bridges in the chain shall report bridge operation flags
375 * (&drm_bridge->ops) and bridge output type (&drm_bridge->type), and none of
376 * them may create a DRM connector directly.
378 * Returns a pointer to the new connector on success, or a negative error
381 struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
382 struct drm_encoder *encoder)
384 struct drm_bridge_connector *bridge_connector;
385 struct drm_connector *connector;
386 struct i2c_adapter *ddc = NULL;
387 struct drm_bridge *bridge, *panel_bridge = NULL;
388 unsigned int supported_formats = BIT(HDMI_COLORSPACE_RGB);
389 unsigned int max_bpc = 8;
393 bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);
394 if (!bridge_connector)
395 return ERR_PTR(-ENOMEM);
397 bridge_connector->encoder = encoder;
400 * TODO: Handle doublescan_allowed and stereo_allowed.
402 connector = &bridge_connector->base;
403 connector->interlace_allowed = true;
404 connector->ycbcr_420_allowed = true;
407 * Initialise connector status handling. First locate the furthest
408 * bridges in the pipeline that support HPD and output detection. Then
409 * initialise the connector polling mode, using HPD if available and
410 * falling back to polling if supported. If neither HPD nor output
411 * detection are available, we don't support hotplug detection at all.
413 connector_type = DRM_MODE_CONNECTOR_Unknown;
414 drm_for_each_bridge_in_chain(encoder, bridge) {
415 if (!bridge->interlace_allowed)
416 connector->interlace_allowed = false;
417 if (!bridge->ycbcr_420_allowed)
418 connector->ycbcr_420_allowed = false;
420 if (bridge->ops & DRM_BRIDGE_OP_EDID)
421 bridge_connector->bridge_edid = bridge;
422 if (bridge->ops & DRM_BRIDGE_OP_HPD)
423 bridge_connector->bridge_hpd = bridge;
424 if (bridge->ops & DRM_BRIDGE_OP_DETECT)
425 bridge_connector->bridge_detect = bridge;
426 if (bridge->ops & DRM_BRIDGE_OP_MODES)
427 bridge_connector->bridge_modes = bridge;
428 if (bridge->ops & DRM_BRIDGE_OP_HDMI) {
429 if (bridge_connector->bridge_hdmi)
430 return ERR_PTR(-EBUSY);
431 if (!bridge->funcs->hdmi_write_infoframe ||
432 !bridge->funcs->hdmi_clear_infoframe)
433 return ERR_PTR(-EINVAL);
435 bridge_connector->bridge_hdmi = bridge;
437 if (bridge->supported_formats)
438 supported_formats = bridge->supported_formats;
440 max_bpc = bridge->max_bpc;
443 if (!drm_bridge_get_next_bridge(bridge))
444 connector_type = bridge->type;
447 if (!drm_bridge_get_next_bridge(bridge) &&
449 connector->fwnode = fwnode_handle_get(of_fwnode_handle(bridge->of_node));
455 if (drm_bridge_is_panel(bridge))
456 panel_bridge = bridge;
459 if (connector_type == DRM_MODE_CONNECTOR_Unknown)
460 return ERR_PTR(-EINVAL);
462 if (bridge_connector->bridge_hdmi)
463 ret = drmm_connector_hdmi_init(drm, connector,
464 bridge_connector->bridge_hdmi->vendor,
465 bridge_connector->bridge_hdmi->product,
466 &drm_bridge_connector_funcs,
467 &drm_bridge_connector_hdmi_funcs,
472 ret = drmm_connector_init(drm, connector,
473 &drm_bridge_connector_funcs,
474 connector_type, ddc);
478 drm_connector_helper_add(connector, &drm_bridge_connector_helper_funcs);
480 if (bridge_connector->bridge_hpd)
481 connector->polled = DRM_CONNECTOR_POLL_HPD;
482 else if (bridge_connector->bridge_detect)
483 connector->polled = DRM_CONNECTOR_POLL_CONNECT
484 | DRM_CONNECTOR_POLL_DISCONNECT;
487 drm_panel_bridge_set_orientation(connector, panel_bridge);
491 EXPORT_SYMBOL_GPL(drm_bridge_connector_init);