1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
4 * Author: Rob Clark <rob@ti.com>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_crtc.h>
9 #include <drm/drm_panel.h>
10 #include <drm/drm_probe_helper.h>
18 #define to_omap_connector(x) container_of(x, struct omap_connector, base)
20 struct omap_connector {
21 struct drm_connector base;
22 struct omap_dss_device *output;
23 struct omap_dss_device *hpd;
27 static void omap_connector_hpd_notify(struct drm_connector *connector,
28 enum drm_connector_status status)
30 struct omap_connector *omap_connector = to_omap_connector(connector);
31 struct omap_dss_device *dssdev;
33 if (status != connector_status_disconnected)
37 * Notify all devics in the pipeline of disconnection. This is required
38 * to let the HDMI encoders reset their internal state related to
39 * connection status, such as the CEC address.
41 for (dssdev = omap_connector->output; dssdev; dssdev = dssdev->next) {
42 if (dssdev->ops && dssdev->ops->hdmi.lost_hotplug)
43 dssdev->ops->hdmi.lost_hotplug(dssdev);
47 static void omap_connector_hpd_cb(void *cb_data,
48 enum drm_connector_status status)
50 struct omap_connector *omap_connector = cb_data;
51 struct drm_connector *connector = &omap_connector->base;
52 struct drm_device *dev = connector->dev;
53 enum drm_connector_status old_status;
55 mutex_lock(&dev->mode_config.mutex);
56 old_status = connector->status;
57 connector->status = status;
58 mutex_unlock(&dev->mode_config.mutex);
60 if (old_status == status)
63 omap_connector_hpd_notify(connector, status);
65 drm_kms_helper_hotplug_event(dev);
68 void omap_connector_enable_hpd(struct drm_connector *connector)
70 struct omap_connector *omap_connector = to_omap_connector(connector);
71 struct omap_dss_device *hpd = omap_connector->hpd;
74 hpd->ops->register_hpd_cb(hpd, omap_connector_hpd_cb,
78 void omap_connector_disable_hpd(struct drm_connector *connector)
80 struct omap_connector *omap_connector = to_omap_connector(connector);
81 struct omap_dss_device *hpd = omap_connector->hpd;
84 hpd->ops->unregister_hpd_cb(hpd);
87 bool omap_connector_get_hdmi_mode(struct drm_connector *connector)
89 struct omap_connector *omap_connector = to_omap_connector(connector);
91 return omap_connector->hdmi_mode;
94 static struct omap_dss_device *
95 omap_connector_find_device(struct drm_connector *connector,
96 enum omap_dss_device_ops_flag op)
98 struct omap_connector *omap_connector = to_omap_connector(connector);
99 struct omap_dss_device *dssdev = NULL;
100 struct omap_dss_device *d;
102 for (d = omap_connector->output; d; d = d->next) {
103 if (d->ops_flags & op)
110 static enum drm_connector_status omap_connector_detect(
111 struct drm_connector *connector, bool force)
113 struct omap_dss_device *dssdev;
114 enum drm_connector_status status;
116 dssdev = omap_connector_find_device(connector,
117 OMAP_DSS_DEVICE_OP_DETECT);
120 status = dssdev->ops->detect(dssdev)
121 ? connector_status_connected
122 : connector_status_disconnected;
124 omap_connector_hpd_notify(connector, status);
126 switch (connector->connector_type) {
127 case DRM_MODE_CONNECTOR_DPI:
128 case DRM_MODE_CONNECTOR_LVDS:
129 case DRM_MODE_CONNECTOR_DSI:
130 status = connector_status_connected;
133 status = connector_status_unknown;
138 VERB("%s: %d (force=%d)", connector->name, status, force);
143 static void omap_connector_destroy(struct drm_connector *connector)
145 struct omap_connector *omap_connector = to_omap_connector(connector);
147 DBG("%s", connector->name);
149 if (omap_connector->hpd) {
150 struct omap_dss_device *hpd = omap_connector->hpd;
152 hpd->ops->unregister_hpd_cb(hpd);
153 omapdss_device_put(hpd);
154 omap_connector->hpd = NULL;
157 drm_connector_unregister(connector);
158 drm_connector_cleanup(connector);
160 omapdss_device_put(omap_connector->output);
162 kfree(omap_connector);
167 static int omap_connector_get_modes_edid(struct drm_connector *connector,
168 struct omap_dss_device *dssdev)
170 struct omap_connector *omap_connector = to_omap_connector(connector);
171 enum drm_connector_status status;
175 status = omap_connector_detect(connector, false);
176 if (status != connector_status_connected)
179 edid = kzalloc(MAX_EDID, GFP_KERNEL);
183 if (dssdev->ops->read_edid(dssdev, edid, MAX_EDID) <= 0 ||
184 !drm_edid_is_valid(edid)) {
189 drm_connector_update_edid_property(connector, edid);
190 n = drm_add_edid_modes(connector, edid);
192 omap_connector->hdmi_mode = drm_detect_hdmi_monitor(edid);
198 drm_connector_update_edid_property(connector, NULL);
202 static int omap_connector_get_modes(struct drm_connector *connector)
204 struct omap_connector *omap_connector = to_omap_connector(connector);
205 struct omap_dss_device *dssdev;
207 DBG("%s", connector->name);
210 * If display exposes EDID, then we parse that in the normal way to
211 * build table of supported modes.
213 dssdev = omap_connector_find_device(connector,
214 OMAP_DSS_DEVICE_OP_EDID);
216 return omap_connector_get_modes_edid(connector, dssdev);
219 * Otherwise if the display pipeline reports modes (e.g. with a fixed
220 * resolution panel or an analog TV output), query it.
222 dssdev = omap_connector_find_device(connector,
223 OMAP_DSS_DEVICE_OP_MODES);
225 return dssdev->ops->get_modes(dssdev, connector);
228 * Otherwise if the display pipeline uses a drm_panel, we delegate the
229 * operation to the panel API.
231 if (omap_connector->output->panel)
232 return drm_panel_get_modes(omap_connector->output->panel);
235 * We can't retrieve modes, which can happen for instance for a DVI or
236 * VGA output with the DDC bus unconnected. The KMS core will add the
242 enum drm_mode_status omap_connector_mode_fixup(struct omap_dss_device *dssdev,
243 const struct drm_display_mode *mode,
244 struct drm_display_mode *adjusted_mode)
248 drm_mode_copy(adjusted_mode, mode);
250 for (; dssdev; dssdev = dssdev->next) {
251 if (!dssdev->ops->check_timings)
254 ret = dssdev->ops->check_timings(dssdev, adjusted_mode);
262 static enum drm_mode_status omap_connector_mode_valid(struct drm_connector *connector,
263 struct drm_display_mode *mode)
265 struct omap_connector *omap_connector = to_omap_connector(connector);
266 struct drm_display_mode new_mode = { { 0 } };
267 enum drm_mode_status status;
269 status = omap_connector_mode_fixup(omap_connector->output, mode,
271 if (status != MODE_OK)
274 /* Check if vrefresh is still valid. */
275 if (drm_mode_vrefresh(mode) != drm_mode_vrefresh(&new_mode))
276 status = MODE_NOCLOCK;
279 DBG("connector: mode %s: " DRM_MODE_FMT,
280 (status == MODE_OK) ? "valid" : "invalid",
286 static const struct drm_connector_funcs omap_connector_funcs = {
287 .reset = drm_atomic_helper_connector_reset,
288 .detect = omap_connector_detect,
289 .fill_modes = drm_helper_probe_single_connector_modes,
290 .destroy = omap_connector_destroy,
291 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
292 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
295 static const struct drm_connector_helper_funcs omap_connector_helper_funcs = {
296 .get_modes = omap_connector_get_modes,
297 .mode_valid = omap_connector_mode_valid,
300 static int omap_connector_get_type(struct omap_dss_device *output)
302 struct omap_dss_device *display;
303 enum omap_display_type type;
305 display = omapdss_display_get(output);
306 type = display->type;
307 omapdss_device_put(display);
310 case OMAP_DISPLAY_TYPE_HDMI:
311 return DRM_MODE_CONNECTOR_HDMIA;
312 case OMAP_DISPLAY_TYPE_DVI:
313 return DRM_MODE_CONNECTOR_DVID;
314 case OMAP_DISPLAY_TYPE_DSI:
315 return DRM_MODE_CONNECTOR_DSI;
316 case OMAP_DISPLAY_TYPE_DPI:
317 case OMAP_DISPLAY_TYPE_DBI:
318 return DRM_MODE_CONNECTOR_DPI;
319 case OMAP_DISPLAY_TYPE_VENC:
320 /* TODO: This could also be composite */
321 return DRM_MODE_CONNECTOR_SVIDEO;
322 case OMAP_DISPLAY_TYPE_SDI:
323 return DRM_MODE_CONNECTOR_LVDS;
325 return DRM_MODE_CONNECTOR_Unknown;
329 /* initialize connector */
330 struct drm_connector *omap_connector_init(struct drm_device *dev,
331 struct omap_dss_device *output,
332 struct drm_encoder *encoder)
334 struct drm_connector *connector = NULL;
335 struct omap_connector *omap_connector;
336 struct omap_dss_device *dssdev;
338 DBG("%s", output->name);
340 omap_connector = kzalloc(sizeof(*omap_connector), GFP_KERNEL);
344 omap_connector->output = omapdss_device_get(output);
346 connector = &omap_connector->base;
347 connector->interlace_allowed = 1;
348 connector->doublescan_allowed = 0;
350 drm_connector_init(dev, connector, &omap_connector_funcs,
351 omap_connector_get_type(output));
352 drm_connector_helper_add(connector, &omap_connector_helper_funcs);
355 * Initialize connector status handling. First try to find a device that
356 * supports hot-plug reporting. If it fails, fall back to a device that
357 * support polling. If that fails too, we don't support hot-plug
360 dssdev = omap_connector_find_device(connector, OMAP_DSS_DEVICE_OP_HPD);
362 omap_connector->hpd = omapdss_device_get(dssdev);
363 connector->polled = DRM_CONNECTOR_POLL_HPD;
365 dssdev = omap_connector_find_device(connector,
366 OMAP_DSS_DEVICE_OP_DETECT);
368 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
369 DRM_CONNECTOR_POLL_DISCONNECT;
376 omap_connector_destroy(connector);