blob: 83f2a9177c149c3f990ccd6ec76bdac3b8c2825a [file] [log] [blame]
Rob Clarkcd5351f2011-11-12 12:09:40 -06001/*
Rob Clark8bb0daf2013-02-11 12:43:09 -05002 * drivers/gpu/drm/omapdrm/omap_connector.c
Rob Clarkcd5351f2011-11-12 12:09:40 -06003 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Laurent Pinchart69a12262015-03-05 21:38:16 +020020#include <drm/drm_atomic_helper.h>
Laurent Pinchart2d278f52015-03-05 21:31:37 +020021#include <drm/drm_crtc.h>
22#include <drm/drm_crtc_helper.h>
Rob Clarkcd5351f2011-11-12 12:09:40 -060023
Laurent Pinchart2d278f52015-03-05 21:31:37 +020024#include "omap_drv.h"
Rob Clarkcd5351f2011-11-12 12:09:40 -060025
26/*
27 * connector funcs
28 */
29
30#define to_omap_connector(x) container_of(x, struct omap_connector, base)
31
32struct omap_connector {
33 struct drm_connector base;
34 struct omap_dss_device *dssdev;
Rob Clarkf5f94542012-12-04 13:59:12 -060035 struct drm_encoder *encoder;
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030036 bool hdmi_mode;
Rob Clarkcd5351f2011-11-12 12:09:40 -060037};
38
Tomi Valkeinen4f930c02014-06-18 14:19:48 +030039bool omap_connector_get_hdmi_mode(struct drm_connector *connector)
40{
41 struct omap_connector *omap_connector = to_omap_connector(connector);
42
43 return omap_connector->hdmi_mode;
44}
45
Rob Clarkf5f94542012-12-04 13:59:12 -060046void copy_timings_omap_to_drm(struct drm_display_mode *mode,
Rob Clarkcd5351f2011-11-12 12:09:40 -060047 struct omap_video_timings *timings)
48{
Tomi Valkeinend8d789412013-04-10 14:12:14 +030049 mode->clock = timings->pixelclock / 1000;
Rob Clarkcd5351f2011-11-12 12:09:40 -060050
51 mode->hdisplay = timings->x_res;
52 mode->hsync_start = mode->hdisplay + timings->hfp;
53 mode->hsync_end = mode->hsync_start + timings->hsw;
54 mode->htotal = mode->hsync_end + timings->hbp;
55
56 mode->vdisplay = timings->y_res;
57 mode->vsync_start = mode->vdisplay + timings->vfp;
58 mode->vsync_end = mode->vsync_start + timings->vsw;
59 mode->vtotal = mode->vsync_end + timings->vbp;
60
Rob Clark0b0d7b62012-09-05 16:48:53 -050061 mode->flags = 0;
62
63 if (timings->interlace)
64 mode->flags |= DRM_MODE_FLAG_INTERLACE;
Rob Clark94254ed2012-09-07 12:59:45 -050065
66 if (timings->hsync_level == OMAPDSS_SIG_ACTIVE_HIGH)
67 mode->flags |= DRM_MODE_FLAG_PHSYNC;
68 else
69 mode->flags |= DRM_MODE_FLAG_NHSYNC;
70
71 if (timings->vsync_level == OMAPDSS_SIG_ACTIVE_HIGH)
72 mode->flags |= DRM_MODE_FLAG_PVSYNC;
73 else
74 mode->flags |= DRM_MODE_FLAG_NVSYNC;
Rob Clarkcd5351f2011-11-12 12:09:40 -060075}
76
Rob Clarkf5f94542012-12-04 13:59:12 -060077void copy_timings_drm_to_omap(struct omap_video_timings *timings,
Rob Clarkcd5351f2011-11-12 12:09:40 -060078 struct drm_display_mode *mode)
79{
Tomi Valkeinend8d789412013-04-10 14:12:14 +030080 timings->pixelclock = mode->clock * 1000;
Rob Clarkcd5351f2011-11-12 12:09:40 -060081
82 timings->x_res = mode->hdisplay;
83 timings->hfp = mode->hsync_start - mode->hdisplay;
84 timings->hsw = mode->hsync_end - mode->hsync_start;
85 timings->hbp = mode->htotal - mode->hsync_end;
86
87 timings->y_res = mode->vdisplay;
88 timings->vfp = mode->vsync_start - mode->vdisplay;
89 timings->vsw = mode->vsync_end - mode->vsync_start;
90 timings->vbp = mode->vtotal - mode->vsync_end;
Rob Clark0b0d7b62012-09-05 16:48:53 -050091
92 timings->interlace = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
Rob Clark94254ed2012-09-07 12:59:45 -050093
94 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
95 timings->hsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
96 else
97 timings->hsync_level = OMAPDSS_SIG_ACTIVE_LOW;
98
99 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
100 timings->vsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
101 else
102 timings->vsync_level = OMAPDSS_SIG_ACTIVE_LOW;
103
104 timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
105 timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
Tomi Valkeinen7a163602014-10-02 17:58:48 +0000106 timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600107}
108
Rob Clarkf5f94542012-12-04 13:59:12 -0600109static enum drm_connector_status omap_connector_detect(
Rob Clarkcd5351f2011-11-12 12:09:40 -0600110 struct drm_connector *connector, bool force)
111{
112 struct omap_connector *omap_connector = to_omap_connector(connector);
113 struct omap_dss_device *dssdev = omap_connector->dssdev;
114 struct omap_dss_driver *dssdrv = dssdev->driver;
115 enum drm_connector_status ret;
116
117 if (dssdrv->detect) {
YAMANE Toshiakifdbf20b2012-11-14 19:28:54 +0900118 if (dssdrv->detect(dssdev))
Rob Clarkcd5351f2011-11-12 12:09:40 -0600119 ret = connector_status_connected;
YAMANE Toshiakifdbf20b2012-11-14 19:28:54 +0900120 else
Rob Clarkcd5351f2011-11-12 12:09:40 -0600121 ret = connector_status_disconnected;
Archit Tanejabddabbe2013-03-26 19:15:20 +0530122 } else if (dssdev->type == OMAP_DISPLAY_TYPE_DPI ||
123 dssdev->type == OMAP_DISPLAY_TYPE_DBI ||
124 dssdev->type == OMAP_DISPLAY_TYPE_SDI ||
125 dssdev->type == OMAP_DISPLAY_TYPE_DSI) {
126 ret = connector_status_connected;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600127 } else {
128 ret = connector_status_unknown;
129 }
130
131 VERB("%s: %d (force=%d)", omap_connector->dssdev->name, ret, force);
132
133 return ret;
134}
135
136static void omap_connector_destroy(struct drm_connector *connector)
137{
138 struct omap_connector *omap_connector = to_omap_connector(connector);
139 struct omap_dss_device *dssdev = omap_connector->dssdev;
140
Rob Clarkcd5351f2011-11-12 12:09:40 -0600141 DBG("%s", omap_connector->dssdev->name);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100142 drm_connector_unregister(connector);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600143 drm_connector_cleanup(connector);
144 kfree(omap_connector);
145
146 omap_dss_put_device(dssdev);
147}
148
149#define MAX_EDID 512
150
151static int omap_connector_get_modes(struct drm_connector *connector)
152{
153 struct omap_connector *omap_connector = to_omap_connector(connector);
154 struct omap_dss_device *dssdev = omap_connector->dssdev;
155 struct omap_dss_driver *dssdrv = dssdev->driver;
156 struct drm_device *dev = connector->dev;
157 int n = 0;
158
159 DBG("%s", omap_connector->dssdev->name);
160
161 /* if display exposes EDID, then we parse that in the normal way to
162 * build table of supported modes.. otherwise (ie. fixed resolution
163 * LCD panels) we just return a single mode corresponding to the
164 * currently configured timings:
165 */
166 if (dssdrv->read_edid) {
167 void *edid = kzalloc(MAX_EDID, GFP_KERNEL);
168
169 if ((dssdrv->read_edid(dssdev, edid, MAX_EDID) > 0) &&
170 drm_edid_is_valid(edid)) {
171 drm_mode_connector_update_edid_property(
172 connector, edid);
173 n = drm_add_edid_modes(connector, edid);
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300174
175 omap_connector->hdmi_mode =
176 drm_detect_hdmi_monitor(edid);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600177 } else {
178 drm_mode_connector_update_edid_property(
179 connector, NULL);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600180 }
Tomi Valkeinen4f930c02014-06-18 14:19:48 +0300181
Jani Nikula451023d2012-08-15 09:32:39 +0000182 kfree(edid);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600183 } else {
184 struct drm_display_mode *mode = drm_mode_create(dev);
Rob Clark0b0d7b62012-09-05 16:48:53 -0500185 struct omap_video_timings timings = {0};
Rob Clarkcd5351f2011-11-12 12:09:40 -0600186
187 dssdrv->get_timings(dssdev, &timings);
188
189 copy_timings_omap_to_drm(mode, &timings);
190
191 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
192 drm_mode_set_name(mode);
193 drm_mode_probed_add(connector, mode);
194
195 n = 1;
196 }
197
198 return n;
199}
200
201static int omap_connector_mode_valid(struct drm_connector *connector,
202 struct drm_display_mode *mode)
203{
204 struct omap_connector *omap_connector = to_omap_connector(connector);
205 struct omap_dss_device *dssdev = omap_connector->dssdev;
206 struct omap_dss_driver *dssdrv = dssdev->driver;
207 struct omap_video_timings timings = {0};
208 struct drm_device *dev = connector->dev;
209 struct drm_display_mode *new_mode;
Archit Tanejabddabbe2013-03-26 19:15:20 +0530210 int r, ret = MODE_BAD;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600211
212 copy_timings_drm_to_omap(&timings, mode);
213 mode->vrefresh = drm_mode_vrefresh(mode);
214
Archit Tanejabddabbe2013-03-26 19:15:20 +0530215 /*
216 * if the panel driver doesn't have a check_timings, it's most likely
217 * a fixed resolution panel, check if the timings match with the
218 * panel's timings
219 */
220 if (dssdrv->check_timings) {
221 r = dssdrv->check_timings(dssdev, &timings);
222 } else {
223 struct omap_video_timings t = {0};
224
225 dssdrv->get_timings(dssdev, &t);
226
227 if (memcmp(&timings, &t, sizeof(struct omap_video_timings)))
228 r = -EINVAL;
229 else
230 r = 0;
231 }
232
233 if (!r) {
Rob Clarkcd5351f2011-11-12 12:09:40 -0600234 /* check if vrefresh is still valid */
235 new_mode = drm_mode_duplicate(dev, mode);
Tomi Valkeinend8d789412013-04-10 14:12:14 +0300236 new_mode->clock = timings.pixelclock / 1000;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600237 new_mode->vrefresh = 0;
238 if (mode->vrefresh == drm_mode_vrefresh(new_mode))
239 ret = MODE_OK;
240 drm_mode_destroy(dev, new_mode);
241 }
242
243 DBG("connector: mode %s: "
244 "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
245 (ret == MODE_OK) ? "valid" : "invalid",
246 mode->base.id, mode->name, mode->vrefresh, mode->clock,
247 mode->hdisplay, mode->hsync_start,
248 mode->hsync_end, mode->htotal,
249 mode->vdisplay, mode->vsync_start,
250 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
251
252 return ret;
253}
254
255struct drm_encoder *omap_connector_attached_encoder(
256 struct drm_connector *connector)
257{
Rob Clarkcd5351f2011-11-12 12:09:40 -0600258 struct omap_connector *omap_connector = to_omap_connector(connector);
Rob Clarkf5f94542012-12-04 13:59:12 -0600259 return omap_connector->encoder;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600260}
261
262static const struct drm_connector_funcs omap_connector_funcs = {
Laurent Pinchartaea3cab2015-03-05 22:50:51 +0200263 .dpms = drm_atomic_helper_connector_dpms,
Laurent Pinchart69a12262015-03-05 21:38:16 +0200264 .reset = drm_atomic_helper_connector_reset,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600265 .detect = omap_connector_detect,
266 .fill_modes = drm_helper_probe_single_connector_modes,
267 .destroy = omap_connector_destroy,
Laurent Pinchart69a12262015-03-05 21:38:16 +0200268 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
269 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Rob Clarkcd5351f2011-11-12 12:09:40 -0600270};
271
272static const struct drm_connector_helper_funcs omap_connector_helper_funcs = {
273 .get_modes = omap_connector_get_modes,
274 .mode_valid = omap_connector_mode_valid,
275 .best_encoder = omap_connector_attached_encoder,
276};
277
Rob Clarkcd5351f2011-11-12 12:09:40 -0600278/* initialize connector */
279struct drm_connector *omap_connector_init(struct drm_device *dev,
Rob Clarkf5f94542012-12-04 13:59:12 -0600280 int connector_type, struct omap_dss_device *dssdev,
281 struct drm_encoder *encoder)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600282{
283 struct drm_connector *connector = NULL;
284 struct omap_connector *omap_connector;
285
286 DBG("%s", dssdev->name);
287
288 omap_dss_get_device(dssdev);
289
290 omap_connector = kzalloc(sizeof(struct omap_connector), GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800291 if (!omap_connector)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600292 goto fail;
Rob Clarkcd5351f2011-11-12 12:09:40 -0600293
294 omap_connector->dssdev = dssdev;
Rob Clarkf5f94542012-12-04 13:59:12 -0600295 omap_connector->encoder = encoder;
296
Rob Clarkcd5351f2011-11-12 12:09:40 -0600297 connector = &omap_connector->base;
298
299 drm_connector_init(dev, connector, &omap_connector_funcs,
300 connector_type);
301 drm_connector_helper_add(connector, &omap_connector_helper_funcs);
302
303#if 0 /* enable when dss2 supports hotplug */
304 if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_HPD)
305 connector->polled = 0;
306 else
307#endif
308 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
309 DRM_CONNECTOR_POLL_DISCONNECT;
310
311 connector->interlace_allowed = 1;
312 connector->doublescan_allowed = 0;
313
Thomas Wood34ea3d32014-05-29 16:57:41 +0100314 drm_connector_register(connector);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600315
316 return connector;
317
318fail:
YAMANE Toshiakifdbf20b2012-11-14 19:28:54 +0900319 if (connector)
Rob Clarkcd5351f2011-11-12 12:09:40 -0600320 omap_connector_destroy(connector);
Rob Clarkcd5351f2011-11-12 12:09:40 -0600321
322 return NULL;
323}