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/of.h>
9 #include <linux/property.h>
10 #include <linux/slab.h>
11
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>
22
23 /**
24 * DOC: overview
25 *
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
30 * driver at runtime.
31 *
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.
37 *
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.
42 *
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).
47 */
48
49 /**
50 * struct drm_bridge_connector - A connector backed by a chain of bridges
51 */
52 struct drm_bridge_connector {
53 /**
54 * @base: The base DRM connector
55 */
56 struct drm_connector base;
57 /**
58 * @encoder:
59 *
60 * The encoder at the start of the bridges chain.
61 */
62 struct drm_encoder *encoder;
63 /**
64 * @bridge_edid:
65 *
66 * The last bridge in the chain (closest to the connector) that provides
67 * EDID read support, if any (see &DRM_BRIDGE_OP_EDID).
68 */
69 struct drm_bridge *bridge_edid;
70 /**
71 * @bridge_hpd:
72 *
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).
75 */
76 struct drm_bridge *bridge_hpd;
77 /**
78 * @bridge_detect:
79 *
80 * The last bridge in the chain (closest to the connector) that provides
81 * connector detection, if any (see &DRM_BRIDGE_OP_DETECT).
82 */
83 struct drm_bridge *bridge_detect;
84 /**
85 * @bridge_modes:
86 *
87 * The last bridge in the chain (closest to the connector) that provides
88 * connector modes detection, if any (see &DRM_BRIDGE_OP_MODES).
89 */
90 struct drm_bridge *bridge_modes;
91 /**
92 * @bridge_hdmi:
93 *
94 * The bridge in the chain that implements necessary support for the
95 * HDMI connector infrastructure, if any (see &DRM_BRIDGE_OP_HDMI).
96 */
97 struct drm_bridge *bridge_hdmi;
98 };
99
100 #define to_drm_bridge_connector(x) \
101 container_of(x, struct drm_bridge_connector, base)
102
103 /* -----------------------------------------------------------------------------
104 * Bridge Connector Hot-Plug Handling
105 */
106
drm_bridge_connector_hpd_notify(struct drm_connector * connector,enum drm_connector_status status)107 static void drm_bridge_connector_hpd_notify(struct drm_connector *connector,
108 enum drm_connector_status status)
109 {
110 struct drm_bridge_connector *bridge_connector =
111 to_drm_bridge_connector(connector);
112 struct drm_bridge *bridge;
113
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);
118 }
119 }
120
drm_bridge_connector_handle_hpd(struct drm_bridge_connector * drm_bridge_connector,enum drm_connector_status status)121 static void drm_bridge_connector_handle_hpd(struct drm_bridge_connector *drm_bridge_connector,
122 enum drm_connector_status status)
123 {
124 struct drm_connector *connector = &drm_bridge_connector->base;
125 struct drm_device *dev = connector->dev;
126
127 mutex_lock(&dev->mode_config.mutex);
128 connector->status = status;
129 mutex_unlock(&dev->mode_config.mutex);
130
131 drm_bridge_connector_hpd_notify(connector, status);
132
133 drm_kms_helper_connector_hotplug_event(connector);
134 }
135
drm_bridge_connector_hpd_cb(void * cb_data,enum drm_connector_status status)136 static void drm_bridge_connector_hpd_cb(void *cb_data,
137 enum drm_connector_status status)
138 {
139 drm_bridge_connector_handle_hpd(cb_data, status);
140 }
141
drm_bridge_connector_oob_hotplug_event(struct drm_connector * connector,enum drm_connector_status status)142 static void drm_bridge_connector_oob_hotplug_event(struct drm_connector *connector,
143 enum drm_connector_status status)
144 {
145 struct drm_bridge_connector *bridge_connector =
146 to_drm_bridge_connector(connector);
147
148 drm_bridge_connector_handle_hpd(bridge_connector, status);
149 }
150
drm_bridge_connector_enable_hpd(struct drm_connector * connector)151 static void drm_bridge_connector_enable_hpd(struct drm_connector *connector)
152 {
153 struct drm_bridge_connector *bridge_connector =
154 to_drm_bridge_connector(connector);
155 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
156
157 if (hpd)
158 drm_bridge_hpd_enable(hpd, drm_bridge_connector_hpd_cb,
159 bridge_connector);
160 }
161
drm_bridge_connector_disable_hpd(struct drm_connector * connector)162 static void drm_bridge_connector_disable_hpd(struct drm_connector *connector)
163 {
164 struct drm_bridge_connector *bridge_connector =
165 to_drm_bridge_connector(connector);
166 struct drm_bridge *hpd = bridge_connector->bridge_hpd;
167
168 if (hpd)
169 drm_bridge_hpd_disable(hpd);
170 }
171
172 /* -----------------------------------------------------------------------------
173 * Bridge Connector Functions
174 */
175
176 static enum drm_connector_status
drm_bridge_connector_detect(struct drm_connector * connector,bool force)177 drm_bridge_connector_detect(struct drm_connector *connector, bool force)
178 {
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;
183
184 if (detect) {
185 status = detect->funcs->detect(detect);
186
187 drm_bridge_connector_hpd_notify(connector, status);
188 } else {
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;
195 break;
196 default:
197 status = connector_status_unknown;
198 break;
199 }
200 }
201
202 return status;
203 }
204
drm_bridge_connector_debugfs_init(struct drm_connector * connector,struct dentry * root)205 static void drm_bridge_connector_debugfs_init(struct drm_connector *connector,
206 struct dentry *root)
207 {
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;
212
213 list_for_each_entry(bridge, &encoder->bridge_chain, chain_node) {
214 if (bridge->funcs->debugfs_init)
215 bridge->funcs->debugfs_init(bridge, root);
216 }
217 }
218
drm_bridge_connector_reset(struct drm_connector * connector)219 static void drm_bridge_connector_reset(struct drm_connector *connector)
220 {
221 struct drm_bridge_connector *bridge_connector =
222 to_drm_bridge_connector(connector);
223
224 drm_atomic_helper_connector_reset(connector);
225 if (bridge_connector->bridge_hdmi)
226 __drm_atomic_helper_connector_hdmi_reset(connector,
227 connector->state);
228 }
229
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,
238 };
239
240 /* -----------------------------------------------------------------------------
241 * Bridge Connector Helper Functions
242 */
243
drm_bridge_connector_get_modes_edid(struct drm_connector * connector,struct drm_bridge * bridge)244 static int drm_bridge_connector_get_modes_edid(struct drm_connector *connector,
245 struct drm_bridge *bridge)
246 {
247 enum drm_connector_status status;
248 const struct drm_edid *drm_edid;
249 int n;
250
251 status = drm_bridge_connector_detect(connector, false);
252 if (status != connector_status_connected)
253 goto no_edid;
254
255 drm_edid = drm_bridge_edid_read(bridge, connector);
256 if (!drm_edid_valid(drm_edid)) {
257 drm_edid_free(drm_edid);
258 goto no_edid;
259 }
260
261 drm_edid_connector_update(connector, drm_edid);
262 n = drm_edid_connector_add_modes(connector);
263
264 drm_edid_free(drm_edid);
265 return n;
266
267 no_edid:
268 drm_edid_connector_update(connector, NULL);
269 return 0;
270 }
271
drm_bridge_connector_get_modes(struct drm_connector * connector)272 static int drm_bridge_connector_get_modes(struct drm_connector *connector)
273 {
274 struct drm_bridge_connector *bridge_connector =
275 to_drm_bridge_connector(connector);
276 struct drm_bridge *bridge;
277
278 /*
279 * If display exposes EDID, then we parse that in the normal way to
280 * build table of supported modes.
281 */
282 bridge = bridge_connector->bridge_edid;
283 if (bridge)
284 return drm_bridge_connector_get_modes_edid(connector, bridge);
285
286 /*
287 * Otherwise if the display pipeline reports modes (e.g. with a fixed
288 * resolution panel or an analog TV output), query it.
289 */
290 bridge = bridge_connector->bridge_modes;
291 if (bridge)
292 return bridge->funcs->get_modes(bridge, connector);
293
294 /*
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
297 * default modes.
298 */
299 return 0;
300 }
301
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,
307 };
308
309 static enum drm_mode_status
drm_bridge_connector_tmds_char_rate_valid(const struct drm_connector * connector,const struct drm_display_mode * mode,unsigned long long tmds_rate)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)
313 {
314 struct drm_bridge_connector *bridge_connector =
315 to_drm_bridge_connector(connector);
316 struct drm_bridge *bridge;
317
318 bridge = bridge_connector->bridge_hdmi;
319 if (!bridge)
320 return MODE_ERROR;
321
322 if (bridge->funcs->hdmi_tmds_char_rate_valid)
323 return bridge->funcs->hdmi_tmds_char_rate_valid(bridge, mode, tmds_rate);
324 else
325 return MODE_OK;
326 }
327
drm_bridge_connector_clear_infoframe(struct drm_connector * connector,enum hdmi_infoframe_type type)328 static int drm_bridge_connector_clear_infoframe(struct drm_connector *connector,
329 enum hdmi_infoframe_type type)
330 {
331 struct drm_bridge_connector *bridge_connector =
332 to_drm_bridge_connector(connector);
333 struct drm_bridge *bridge;
334
335 bridge = bridge_connector->bridge_hdmi;
336 if (!bridge)
337 return -EINVAL;
338
339 return bridge->funcs->hdmi_clear_infoframe(bridge, type);
340 }
341
drm_bridge_connector_write_infoframe(struct drm_connector * connector,enum hdmi_infoframe_type type,const u8 * buffer,size_t len)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)
345 {
346 struct drm_bridge_connector *bridge_connector =
347 to_drm_bridge_connector(connector);
348 struct drm_bridge *bridge;
349
350 bridge = bridge_connector->bridge_hdmi;
351 if (!bridge)
352 return -EINVAL;
353
354 return bridge->funcs->hdmi_write_infoframe(bridge, type, buffer, len);
355 }
356
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,
361 };
362
363 /* -----------------------------------------------------------------------------
364 * Bridge Connector Initialisation
365 */
366
367 /**
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
371 *
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.
377 *
378 * Returns a pointer to the new connector on success, or a negative error
379 * pointer otherwise.
380 */
drm_bridge_connector_init(struct drm_device * drm,struct drm_encoder * encoder)381 struct drm_connector *drm_bridge_connector_init(struct drm_device *drm,
382 struct drm_encoder *encoder)
383 {
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;
390 int connector_type;
391 int ret;
392
393 bridge_connector = drmm_kzalloc(drm, sizeof(*bridge_connector), GFP_KERNEL);
394 if (!bridge_connector)
395 return ERR_PTR(-ENOMEM);
396
397 bridge_connector->encoder = encoder;
398
399 /*
400 * TODO: Handle doublescan_allowed and stereo_allowed.
401 */
402 connector = &bridge_connector->base;
403 connector->interlace_allowed = true;
404 connector->ycbcr_420_allowed = true;
405
406 /*
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.
412 */
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;
419
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);
434
435 bridge_connector->bridge_hdmi = bridge;
436
437 if (bridge->supported_formats)
438 supported_formats = bridge->supported_formats;
439 if (bridge->max_bpc)
440 max_bpc = bridge->max_bpc;
441 }
442
443 if (!drm_bridge_get_next_bridge(bridge))
444 connector_type = bridge->type;
445
446 #ifdef CONFIG_OF
447 if (!drm_bridge_get_next_bridge(bridge) &&
448 bridge->of_node)
449 connector->fwnode = fwnode_handle_get(of_fwnode_handle(bridge->of_node));
450 #endif
451
452 if (bridge->ddc)
453 ddc = bridge->ddc;
454
455 if (drm_bridge_is_panel(bridge))
456 panel_bridge = bridge;
457 }
458
459 if (connector_type == DRM_MODE_CONNECTOR_Unknown)
460 return ERR_PTR(-EINVAL);
461
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,
468 connector_type, ddc,
469 supported_formats,
470 max_bpc);
471 else
472 ret = drmm_connector_init(drm, connector,
473 &drm_bridge_connector_funcs,
474 connector_type, ddc);
475 if (ret)
476 return ERR_PTR(ret);
477
478 drm_connector_helper_add(connector, &drm_bridge_connector_helper_funcs);
479
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;
485
486 if (panel_bridge)
487 drm_panel_bridge_set_orientation(connector, panel_bridge);
488
489 return connector;
490 }
491 EXPORT_SYMBOL_GPL(drm_bridge_connector_init);
492