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 0 : static void drm_bridge_connector_enable_hpd(struct drm_connector *connector)
132 : {
133 0 : struct drm_bridge_connector *bridge_connector =
134 0 : to_drm_bridge_connector(connector);
135 0 : struct drm_bridge *hpd = bridge_connector->bridge_hpd;
136 :
137 0 : if (hpd)
138 0 : drm_bridge_hpd_enable(hpd, drm_bridge_connector_hpd_cb,
139 : bridge_connector);
140 0 : }
141 :
142 0 : static void drm_bridge_connector_disable_hpd(struct drm_connector *connector)
143 : {
144 0 : struct drm_bridge_connector *bridge_connector =
145 0 : to_drm_bridge_connector(connector);
146 0 : struct drm_bridge *hpd = bridge_connector->bridge_hpd;
147 :
148 0 : if (hpd)
149 0 : drm_bridge_hpd_disable(hpd);
150 0 : }
151 :
152 : /* -----------------------------------------------------------------------------
153 : * Bridge Connector Functions
154 : */
155 :
156 : static enum drm_connector_status
157 0 : drm_bridge_connector_detect(struct drm_connector *connector, bool force)
158 : {
159 0 : struct drm_bridge_connector *bridge_connector =
160 0 : to_drm_bridge_connector(connector);
161 0 : struct drm_bridge *detect = bridge_connector->bridge_detect;
162 : enum drm_connector_status status;
163 :
164 0 : if (detect) {
165 0 : status = detect->funcs->detect(detect);
166 :
167 : drm_bridge_connector_hpd_notify(connector, status);
168 : } else {
169 0 : 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;
175 : break;
176 : default:
177 0 : status = connector_status_unknown;
178 0 : break;
179 : }
180 : }
181 :
182 0 : return status;
183 : }
184 :
185 0 : static void drm_bridge_connector_destroy(struct drm_connector *connector)
186 : {
187 0 : struct drm_bridge_connector *bridge_connector =
188 0 : to_drm_bridge_connector(connector);
189 :
190 0 : if (bridge_connector->bridge_hpd) {
191 0 : struct drm_bridge *hpd = bridge_connector->bridge_hpd;
192 :
193 0 : drm_bridge_hpd_disable(hpd);
194 : }
195 :
196 0 : drm_connector_unregister(connector);
197 0 : drm_connector_cleanup(connector);
198 :
199 0 : kfree(bridge_connector);
200 0 : }
201 :
202 0 : static void drm_bridge_connector_debugfs_init(struct drm_connector *connector,
203 : struct dentry *root)
204 : {
205 0 : struct drm_bridge_connector *bridge_connector =
206 0 : to_drm_bridge_connector(connector);
207 0 : struct drm_encoder *encoder = bridge_connector->encoder;
208 : struct drm_bridge *bridge;
209 :
210 0 : list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
211 0 : if (bridge->funcs->debugfs_init)
212 0 : bridge->funcs->debugfs_init(bridge, root);
213 : }
214 0 : }
215 :
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,
224 : };
225 :
226 : /* -----------------------------------------------------------------------------
227 : * Bridge Connector Helper Functions
228 : */
229 :
230 0 : static int drm_bridge_connector_get_modes_edid(struct drm_connector *connector,
231 : struct drm_bridge *bridge)
232 : {
233 : enum drm_connector_status status;
234 : struct edid *edid;
235 : int n;
236 :
237 0 : status = drm_bridge_connector_detect(connector, false);
238 0 : if (status != connector_status_connected)
239 : goto no_edid;
240 :
241 0 : edid = bridge->funcs->get_edid(bridge, connector);
242 0 : if (!drm_edid_is_valid(edid)) {
243 0 : kfree(edid);
244 0 : goto no_edid;
245 : }
246 :
247 0 : drm_connector_update_edid_property(connector, edid);
248 0 : n = drm_add_edid_modes(connector, edid);
249 :
250 0 : kfree(edid);
251 0 : return n;
252 :
253 : no_edid:
254 0 : drm_connector_update_edid_property(connector, NULL);
255 0 : return 0;
256 : }
257 :
258 0 : static int drm_bridge_connector_get_modes(struct drm_connector *connector)
259 : {
260 0 : struct drm_bridge_connector *bridge_connector =
261 0 : to_drm_bridge_connector(connector);
262 : struct drm_bridge *bridge;
263 :
264 : /*
265 : * If display exposes EDID, then we parse that in the normal way to
266 : * build table of supported modes.
267 : */
268 0 : bridge = bridge_connector->bridge_edid;
269 0 : if (bridge)
270 0 : return drm_bridge_connector_get_modes_edid(connector, bridge);
271 :
272 : /*
273 : * Otherwise if the display pipeline reports modes (e.g. with a fixed
274 : * resolution panel or an analog TV output), query it.
275 : */
276 0 : bridge = bridge_connector->bridge_modes;
277 0 : if (bridge)
278 0 : return bridge->funcs->get_modes(bridge, connector);
279 :
280 : /*
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
283 : * default modes.
284 : */
285 : return 0;
286 : }
287 :
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,
293 : };
294 :
295 : /* -----------------------------------------------------------------------------
296 : * Bridge Connector Initialisation
297 : */
298 :
299 : /**
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
303 : *
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.
309 : *
310 : * Returns a pointer to the new connector on success, or a negative error
311 : * pointer otherwise.
312 : */
313 0 : struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
314 : struct drm_encoder *encoder)
315 : {
316 : struct drm_bridge_connector *bridge_connector;
317 : struct drm_connector *connector;
318 0 : struct i2c_adapter *ddc = NULL;
319 0 : struct drm_bridge *bridge, *panel_bridge = NULL;
320 : int connector_type;
321 :
322 0 : bridge_connector = kzalloc(sizeof(*bridge_connector), GFP_KERNEL);
323 0 : if (!bridge_connector)
324 : return ERR_PTR(-ENOMEM);
325 :
326 0 : bridge_connector->encoder = encoder;
327 :
328 : /*
329 : * TODO: Handle doublescan_allowed, stereo_allowed and
330 : * ycbcr_420_allowed.
331 : */
332 0 : connector = &bridge_connector->base;
333 0 : connector->interlace_allowed = true;
334 :
335 : /*
336 : * Initialise connector status handling. First locate the furthest
337 : * bridges in the pipeline that support HPD and output detection. Then
338 : * initialise the connector polling mode, using HPD if available and
339 : * falling back to polling if supported. If neither HPD nor output
340 : * detection are available, we don't support hotplug detection at all.
341 : */
342 0 : connector_type = DRM_MODE_CONNECTOR_Unknown;
343 0 : drm_for_each_bridge_in_chain(encoder, bridge) {
344 0 : if (!bridge->interlace_allowed)
345 0 : connector->interlace_allowed = false;
346 :
347 0 : if (bridge->ops & DRM_BRIDGE_OP_EDID)
348 0 : bridge_connector->bridge_edid = bridge;
349 0 : if (bridge->ops & DRM_BRIDGE_OP_HPD)
350 0 : bridge_connector->bridge_hpd = bridge;
351 0 : if (bridge->ops & DRM_BRIDGE_OP_DETECT)
352 0 : bridge_connector->bridge_detect = bridge;
353 0 : if (bridge->ops & DRM_BRIDGE_OP_MODES)
354 0 : bridge_connector->bridge_modes = bridge;
355 :
356 0 : if (!drm_bridge_get_next_bridge(bridge))
357 0 : connector_type = bridge->type;
358 :
359 0 : if (bridge->ddc)
360 0 : ddc = bridge->ddc;
361 :
362 0 : if (drm_bridge_is_panel(bridge))
363 0 : panel_bridge = bridge;
364 : }
365 :
366 0 : if (connector_type == DRM_MODE_CONNECTOR_Unknown) {
367 0 : kfree(bridge_connector);
368 0 : return ERR_PTR(-EINVAL);
369 : }
370 :
371 0 : drm_connector_init_with_ddc(drm, connector, &drm_bridge_connector_funcs,
372 : connector_type, ddc);
373 0 : drm_connector_helper_add(connector, &drm_bridge_connector_helper_funcs);
374 :
375 0 : if (bridge_connector->bridge_hpd)
376 0 : connector->polled = DRM_CONNECTOR_POLL_HPD;
377 0 : else if (bridge_connector->bridge_detect)
378 0 : connector->polled = DRM_CONNECTOR_POLL_CONNECT
379 : | DRM_CONNECTOR_POLL_DISCONNECT;
380 :
381 0 : if (panel_bridge)
382 0 : drm_panel_bridge_set_orientation(connector, panel_bridge);
383 :
384 : return connector;
385 : }
386 : EXPORT_SYMBOL_GPL(drm_bridge_connector_init);
|