1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
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_connector_hotplug_event(connector);
131 static void drm_bridge_connector_enable_hpd(struct drm_connector *connector)
133 struct drm_bridge_connector *bridge_connector =
134 to_drm_bridge_connector(connector);
135 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
138 drm_bridge_hpd_enable(hpd, drm_bridge_connector_hpd_cb,
142 static void drm_bridge_connector_disable_hpd(struct drm_connector *connector)
144 struct drm_bridge_connector *bridge_connector =
145 to_drm_bridge_connector(connector);
146 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
149 drm_bridge_hpd_disable(hpd);
152 /* -----------------------------------------------------------------------------
153 * Bridge Connector Functions
156 static enum drm_connector_status
157 drm_bridge_connector_detect(struct drm_connector *connector, bool force)
159 struct drm_bridge_connector *bridge_connector =
160 to_drm_bridge_connector(connector);
161 struct drm_bridge *detect = bridge_connector->bridge_detect;
162 enum drm_connector_status status;
165 status = detect->funcs->detect(detect);
167 drm_bridge_connector_hpd_notify(connector, status);
169 switch (connector->connector_type) {
170 case DRM_MODE_CONNECTOR_DPI:
171 case DRM_MODE_CONNECTOR_LVDS:
172 case DRM_MODE_CONNECTOR_DSI:
173 case DRM_MODE_CONNECTOR_eDP:
174 status = connector_status_connected;
177 status = connector_status_unknown;
185 static void drm_bridge_connector_destroy(struct drm_connector *connector)
187 struct drm_bridge_connector *bridge_connector =
188 to_drm_bridge_connector(connector);
190 if (bridge_connector->bridge_hpd) {
191 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
193 drm_bridge_hpd_disable(hpd);
196 drm_connector_unregister(connector);
197 drm_connector_cleanup(connector);
199 kfree(bridge_connector);
202 static void drm_bridge_connector_debugfs_init(struct drm_connector *connector,
205 struct drm_bridge_connector *bridge_connector =
206 to_drm_bridge_connector(connector);
207 struct drm_encoder *encoder = bridge_connector->encoder;
208 struct drm_bridge *bridge;
210 list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
211 if (bridge->funcs->debugfs_init)
212 bridge->funcs->debugfs_init(bridge, root);
216 static const struct drm_connector_funcs drm_bridge_connector_funcs = {
217 .reset = drm_atomic_helper_connector_reset,
218 .detect = drm_bridge_connector_detect,
219 .fill_modes = drm_helper_probe_single_connector_modes,
220 .destroy = drm_bridge_connector_destroy,
221 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
222 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
223 .debugfs_init = drm_bridge_connector_debugfs_init,
226 /* -----------------------------------------------------------------------------
227 * Bridge Connector Helper Functions
230 static int drm_bridge_connector_get_modes_edid(struct drm_connector *connector,
231 struct drm_bridge *bridge)
233 enum drm_connector_status status;
237 status = drm_bridge_connector_detect(connector, false);
238 if (status != connector_status_connected)
241 edid = bridge->funcs->get_edid(bridge, connector);
242 if (!drm_edid_is_valid(edid)) {
247 drm_connector_update_edid_property(connector, edid);
248 n = drm_add_edid_modes(connector, edid);
254 drm_connector_update_edid_property(connector, NULL);
258 static int drm_bridge_connector_get_modes(struct drm_connector *connector)
260 struct drm_bridge_connector *bridge_connector =
261 to_drm_bridge_connector(connector);
262 struct drm_bridge *bridge;
265 * If display exposes EDID, then we parse that in the normal way to
266 * build table of supported modes.
268 bridge = bridge_connector->bridge_edid;
270 return drm_bridge_connector_get_modes_edid(connector, bridge);
273 * Otherwise if the display pipeline reports modes (e.g. with a fixed
274 * resolution panel or an analog TV output), query it.
276 bridge = bridge_connector->bridge_modes;
278 return bridge->funcs->get_modes(bridge, connector);
281 * We can't retrieve modes, which can happen for instance for a DVI or
282 * VGA output with the DDC bus unconnected. The KMS core will add the
288 static const struct drm_connector_helper_funcs drm_bridge_connector_helper_funcs = {
289 .get_modes = drm_bridge_connector_get_modes,
290 /* No need for .mode_valid(), the bridges are checked by the core. */
291 .enable_hpd = drm_bridge_connector_enable_hpd,
292 .disable_hpd = drm_bridge_connector_disable_hpd,
295 /* -----------------------------------------------------------------------------
296 * Bridge Connector Initialisation
300 * drm_bridge_connector_init - Initialise a connector for a chain of bridges
301 * @drm: the DRM device
302 * @encoder: the encoder where the bridge chain starts
304 * Allocate, initialise and register a &drm_bridge_connector with the @drm
305 * device. The connector is associated with a chain of bridges that starts at
306 * the @encoder. All bridges in the chain shall report bridge operation flags
307 * (&drm_bridge->ops) and bridge output type (&drm_bridge->type), and none of
308 * them may create a DRM connector directly.
310 * Returns a pointer to the new connector on success, or a negative error
313 struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
314 struct drm_encoder *encoder)
316 struct drm_bridge_connector *bridge_connector;
317 struct drm_connector *connector;
318 struct i2c_adapter *ddc = NULL;
319 struct drm_bridge *bridge, *panel_bridge = NULL;
323 bridge_connector = kzalloc(sizeof(*bridge_connector), GFP_KERNEL);
324 if (!bridge_connector)
325 return ERR_PTR(-ENOMEM);
327 bridge_connector->encoder = encoder;
330 * TODO: Handle doublescan_allowed, stereo_allowed and
333 connector = &bridge_connector->base;
334 connector->interlace_allowed = true;
337 * Initialise connector status handling. First locate the furthest
338 * bridges in the pipeline that support HPD and output detection. Then
339 * initialise the connector polling mode, using HPD if available and
340 * falling back to polling if supported. If neither HPD nor output
341 * detection are available, we don't support hotplug detection at all.
343 connector_type = DRM_MODE_CONNECTOR_Unknown;
344 drm_for_each_bridge_in_chain(encoder, bridge) {
345 if (!bridge->interlace_allowed)
346 connector->interlace_allowed = false;
348 if (bridge->ops & DRM_BRIDGE_OP_EDID)
349 bridge_connector->bridge_edid = bridge;
350 if (bridge->ops & DRM_BRIDGE_OP_HPD)
351 bridge_connector->bridge_hpd = bridge;
352 if (bridge->ops & DRM_BRIDGE_OP_DETECT)
353 bridge_connector->bridge_detect = bridge;
354 if (bridge->ops & DRM_BRIDGE_OP_MODES)
355 bridge_connector->bridge_modes = bridge;
357 if (!drm_bridge_get_next_bridge(bridge))
358 connector_type = bridge->type;
363 if (drm_bridge_is_panel(bridge))
364 panel_bridge = bridge;
367 if (connector_type == DRM_MODE_CONNECTOR_Unknown) {
368 kfree(bridge_connector);
369 return ERR_PTR(-EINVAL);
372 ret = drm_connector_init_with_ddc(drm, connector,
373 &drm_bridge_connector_funcs,
374 connector_type, ddc);
376 kfree(bridge_connector);
380 drm_connector_helper_add(connector, &drm_bridge_connector_helper_funcs);
382 if (bridge_connector->bridge_hpd)
383 connector->polled = DRM_CONNECTOR_POLL_HPD;
384 else if (bridge_connector->bridge_detect)
385 connector->polled = DRM_CONNECTOR_POLL_CONNECT
386 | DRM_CONNECTOR_POLL_DISCONNECT;
389 drm_panel_bridge_set_orientation(connector, panel_bridge);
393 EXPORT_SYMBOL_GPL(drm_bridge_connector_init);