Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0+
2 : /*
3 : * Copyright (C) 2019 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
4 : */
5 :
6 : #include <linux/kernel.h>
7 : #include <linux/module.h>
8 : #include <linux/slab.h>
9 :
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>
18 :
19 : /**
20 : * DOC: overview
21 : *
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
26 : * driver at runtime.
27 : *
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.
33 : *
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.
38 : *
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).
43 : */
44 :
45 : /**
46 : * struct drm_bridge_connector - A connector backed by a chain of bridges
47 : */
48 : struct drm_bridge_connector {
49 : /**
50 : * @base: The base DRM connector
51 : */
52 : struct drm_connector base;
53 : /**
54 : * @encoder:
55 : *
56 : * The encoder at the start of the bridges chain.
57 : */
58 : struct drm_encoder *encoder;
59 : /**
60 : * @bridge_edid:
61 : *
62 : * The last bridge in the chain (closest to the connector) that provides
63 : * EDID read support, if any (see &DRM_BRIDGE_OP_EDID).
64 : */
65 : struct drm_bridge *bridge_edid;
66 : /**
67 : * @bridge_hpd:
68 : *
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).
71 : */
72 : struct drm_bridge *bridge_hpd;
73 : /**
74 : * @bridge_detect:
75 : *
76 : * The last bridge in the chain (closest to the connector) that provides
77 : * connector detection, if any (see &DRM_BRIDGE_OP_DETECT).
78 : */
79 : struct drm_bridge *bridge_detect;
80 : /**
81 : * @bridge_modes:
82 : *
83 : * The last bridge in the chain (closest to the connector) that provides
84 : * connector modes detection, if any (see &DRM_BRIDGE_OP_MODES).
85 : */
86 : struct drm_bridge *bridge_modes;
87 : };
88 :
89 : #define to_drm_bridge_connector(x) \
90 : container_of(x, struct drm_bridge_connector, base)
91 :
92 : /* -----------------------------------------------------------------------------
93 : * Bridge Connector Hot-Plug Handling
94 : */
95 :
96 : static void drm_bridge_connector_hpd_notify(struct drm_connector *connector,
97 : enum drm_connector_status status)
98 : {
99 0 : struct drm_bridge_connector *bridge_connector =
100 0 : to_drm_bridge_connector(connector);
101 : struct drm_bridge *bridge;
102 :
103 : /* Notify all bridges in the pipeline of hotplug events. */
104 0 : drm_for_each_bridge_in_chain(bridge_connector->encoder, bridge) {
105 0 : if (bridge->funcs->hpd_notify)
106 0 : bridge->funcs->hpd_notify(bridge, status);
107 : }
108 : }
109 :
110 0 : static void drm_bridge_connector_hpd_cb(void *cb_data,
111 : enum drm_connector_status status)
112 : {
113 0 : struct drm_bridge_connector *drm_bridge_connector = cb_data;
114 0 : struct drm_connector *connector = &drm_bridge_connector->base;
115 0 : struct drm_device *dev = connector->dev;
116 : enum drm_connector_status old_status;
117 :
118 0 : mutex_lock(&dev->mode_config.mutex);
119 0 : old_status = connector->status;
120 0 : connector->status = status;
121 0 : mutex_unlock(&dev->mode_config.mutex);
122 :
123 0 : if (old_status == status)
124 : return;
125 :
126 0 : drm_bridge_connector_hpd_notify(connector, status);
127 :
128 0 : drm_kms_helper_hotplug_event(dev);
129 : }
130 :
131 : /**
132 : * drm_bridge_connector_enable_hpd - Enable hot-plug detection for the connector
133 : * @connector: The DRM bridge connector
134 : *
135 : * This function enables hot-plug detection for the given bridge connector.
136 : * This is typically used by display drivers in their resume handler.
137 : */
138 0 : void drm_bridge_connector_enable_hpd(struct drm_connector *connector)
139 : {
140 0 : struct drm_bridge_connector *bridge_connector =
141 0 : to_drm_bridge_connector(connector);
142 0 : struct drm_bridge *hpd = bridge_connector->bridge_hpd;
143 :
144 0 : if (hpd)
145 0 : drm_bridge_hpd_enable(hpd, drm_bridge_connector_hpd_cb,
146 : bridge_connector);
147 0 : }
148 : EXPORT_SYMBOL_GPL(drm_bridge_connector_enable_hpd);
149 :
150 : /**
151 : * drm_bridge_connector_disable_hpd - Disable hot-plug detection for the
152 : * connector
153 : * @connector: The DRM bridge connector
154 : *
155 : * This function disables hot-plug detection for the given bridge connector.
156 : * This is typically used by display drivers in their suspend handler.
157 : */
158 0 : void drm_bridge_connector_disable_hpd(struct drm_connector *connector)
159 : {
160 0 : struct drm_bridge_connector *bridge_connector =
161 0 : to_drm_bridge_connector(connector);
162 0 : struct drm_bridge *hpd = bridge_connector->bridge_hpd;
163 :
164 0 : if (hpd)
165 0 : drm_bridge_hpd_disable(hpd);
166 0 : }
167 : EXPORT_SYMBOL_GPL(drm_bridge_connector_disable_hpd);
168 :
169 : /* -----------------------------------------------------------------------------
170 : * Bridge Connector Functions
171 : */
172 :
173 : static enum drm_connector_status
174 0 : drm_bridge_connector_detect(struct drm_connector *connector, bool force)
175 : {
176 0 : struct drm_bridge_connector *bridge_connector =
177 0 : to_drm_bridge_connector(connector);
178 0 : struct drm_bridge *detect = bridge_connector->bridge_detect;
179 : enum drm_connector_status status;
180 :
181 0 : if (detect) {
182 0 : status = detect->funcs->detect(detect);
183 :
184 : drm_bridge_connector_hpd_notify(connector, status);
185 : } else {
186 0 : switch (connector->connector_type) {
187 : case DRM_MODE_CONNECTOR_DPI:
188 : case DRM_MODE_CONNECTOR_LVDS:
189 : case DRM_MODE_CONNECTOR_DSI:
190 : case DRM_MODE_CONNECTOR_eDP:
191 : status = connector_status_connected;
192 : break;
193 : default:
194 0 : status = connector_status_unknown;
195 0 : break;
196 : }
197 : }
198 :
199 0 : return status;
200 : }
201 :
202 0 : static void drm_bridge_connector_destroy(struct drm_connector *connector)
203 : {
204 0 : struct drm_bridge_connector *bridge_connector =
205 0 : to_drm_bridge_connector(connector);
206 :
207 0 : if (bridge_connector->bridge_hpd) {
208 0 : struct drm_bridge *hpd = bridge_connector->bridge_hpd;
209 :
210 0 : drm_bridge_hpd_disable(hpd);
211 : }
212 :
213 0 : drm_connector_unregister(connector);
214 0 : drm_connector_cleanup(connector);
215 :
216 0 : kfree(bridge_connector);
217 0 : }
218 :
219 0 : static void drm_bridge_connector_debugfs_init(struct drm_connector *connector,
220 : struct dentry *root)
221 : {
222 0 : struct drm_bridge_connector *bridge_connector =
223 0 : to_drm_bridge_connector(connector);
224 0 : struct drm_encoder *encoder = bridge_connector->encoder;
225 : struct drm_bridge *bridge;
226 :
227 0 : list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
228 0 : if (bridge->funcs->debugfs_init)
229 0 : bridge->funcs->debugfs_init(bridge, root);
230 : }
231 0 : }
232 :
233 : static const struct drm_connector_funcs drm_bridge_connector_funcs = {
234 : .reset = drm_atomic_helper_connector_reset,
235 : .detect = drm_bridge_connector_detect,
236 : .fill_modes = drm_helper_probe_single_connector_modes,
237 : .destroy = drm_bridge_connector_destroy,
238 : .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
239 : .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
240 : .debugfs_init = drm_bridge_connector_debugfs_init,
241 : };
242 :
243 : /* -----------------------------------------------------------------------------
244 : * Bridge Connector Helper Functions
245 : */
246 :
247 0 : static int drm_bridge_connector_get_modes_edid(struct drm_connector *connector,
248 : struct drm_bridge *bridge)
249 : {
250 : enum drm_connector_status status;
251 : struct edid *edid;
252 : int n;
253 :
254 0 : status = drm_bridge_connector_detect(connector, false);
255 0 : if (status != connector_status_connected)
256 : goto no_edid;
257 :
258 0 : edid = bridge->funcs->get_edid(bridge, connector);
259 0 : if (!drm_edid_is_valid(edid)) {
260 0 : kfree(edid);
261 0 : goto no_edid;
262 : }
263 :
264 0 : drm_connector_update_edid_property(connector, edid);
265 0 : n = drm_add_edid_modes(connector, edid);
266 :
267 0 : kfree(edid);
268 0 : return n;
269 :
270 : no_edid:
271 0 : drm_connector_update_edid_property(connector, NULL);
272 0 : return 0;
273 : }
274 :
275 0 : static int drm_bridge_connector_get_modes(struct drm_connector *connector)
276 : {
277 0 : struct drm_bridge_connector *bridge_connector =
278 0 : to_drm_bridge_connector(connector);
279 : struct drm_bridge *bridge;
280 :
281 : /*
282 : * If display exposes EDID, then we parse that in the normal way to
283 : * build table of supported modes.
284 : */
285 0 : bridge = bridge_connector->bridge_edid;
286 0 : if (bridge)
287 0 : return drm_bridge_connector_get_modes_edid(connector, bridge);
288 :
289 : /*
290 : * Otherwise if the display pipeline reports modes (e.g. with a fixed
291 : * resolution panel or an analog TV output), query it.
292 : */
293 0 : bridge = bridge_connector->bridge_modes;
294 0 : if (bridge)
295 0 : return bridge->funcs->get_modes(bridge, connector);
296 :
297 : /*
298 : * We can't retrieve modes, which can happen for instance for a DVI or
299 : * VGA output with the DDC bus unconnected. The KMS core will add the
300 : * default modes.
301 : */
302 : return 0;
303 : }
304 :
305 : static const struct drm_connector_helper_funcs drm_bridge_connector_helper_funcs = {
306 : .get_modes = drm_bridge_connector_get_modes,
307 : /* No need for .mode_valid(), the bridges are checked by the core. */
308 : };
309 :
310 : /* -----------------------------------------------------------------------------
311 : * Bridge Connector Initialisation
312 : */
313 :
314 : /**
315 : * drm_bridge_connector_init - Initialise a connector for a chain of bridges
316 : * @drm: the DRM device
317 : * @encoder: the encoder where the bridge chain starts
318 : *
319 : * Allocate, initialise and register a &drm_bridge_connector with the @drm
320 : * device. The connector is associated with a chain of bridges that starts at
321 : * the @encoder. All bridges in the chain shall report bridge operation flags
322 : * (&drm_bridge->ops) and bridge output type (&drm_bridge->type), and none of
323 : * them may create a DRM connector directly.
324 : *
325 : * Returns a pointer to the new connector on success, or a negative error
326 : * pointer otherwise.
327 : */
328 0 : struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
329 : struct drm_encoder *encoder)
330 : {
331 : struct drm_bridge_connector *bridge_connector;
332 : struct drm_connector *connector;
333 0 : struct i2c_adapter *ddc = NULL;
334 : struct drm_bridge *bridge;
335 : int connector_type;
336 :
337 0 : bridge_connector = kzalloc(sizeof(*bridge_connector), GFP_KERNEL);
338 0 : if (!bridge_connector)
339 : return ERR_PTR(-ENOMEM);
340 :
341 0 : bridge_connector->encoder = encoder;
342 :
343 : /*
344 : * TODO: Handle doublescan_allowed, stereo_allowed and
345 : * ycbcr_420_allowed.
346 : */
347 0 : connector = &bridge_connector->base;
348 0 : connector->interlace_allowed = true;
349 :
350 : /*
351 : * Initialise connector status handling. First locate the furthest
352 : * bridges in the pipeline that support HPD and output detection. Then
353 : * initialise the connector polling mode, using HPD if available and
354 : * falling back to polling if supported. If neither HPD nor output
355 : * detection are available, we don't support hotplug detection at all.
356 : */
357 0 : connector_type = DRM_MODE_CONNECTOR_Unknown;
358 0 : drm_for_each_bridge_in_chain(encoder, bridge) {
359 0 : if (!bridge->interlace_allowed)
360 0 : connector->interlace_allowed = false;
361 :
362 0 : if (bridge->ops & DRM_BRIDGE_OP_EDID)
363 0 : bridge_connector->bridge_edid = bridge;
364 0 : if (bridge->ops & DRM_BRIDGE_OP_HPD)
365 0 : bridge_connector->bridge_hpd = bridge;
366 0 : if (bridge->ops & DRM_BRIDGE_OP_DETECT)
367 0 : bridge_connector->bridge_detect = bridge;
368 0 : if (bridge->ops & DRM_BRIDGE_OP_MODES)
369 0 : bridge_connector->bridge_modes = bridge;
370 :
371 0 : if (!drm_bridge_get_next_bridge(bridge))
372 0 : connector_type = bridge->type;
373 :
374 0 : if (bridge->ddc)
375 0 : ddc = bridge->ddc;
376 : }
377 :
378 0 : if (connector_type == DRM_MODE_CONNECTOR_Unknown) {
379 0 : kfree(bridge_connector);
380 0 : return ERR_PTR(-EINVAL);
381 : }
382 :
383 0 : drm_connector_init_with_ddc(drm, connector, &drm_bridge_connector_funcs,
384 : connector_type, ddc);
385 0 : drm_connector_helper_add(connector, &drm_bridge_connector_helper_funcs);
386 :
387 0 : if (bridge_connector->bridge_hpd) {
388 0 : connector->polled = DRM_CONNECTOR_POLL_HPD;
389 : drm_bridge_connector_enable_hpd(connector);
390 : }
391 0 : else if (bridge_connector->bridge_detect)
392 0 : connector->polled = DRM_CONNECTOR_POLL_CONNECT
393 : | DRM_CONNECTOR_POLL_DISCONNECT;
394 :
395 : return connector;
396 : }
397 : EXPORT_SYMBOL_GPL(drm_bridge_connector_init);
|