]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/omapdrm/omap_connector.c
drm/omap: omap_display_timings: Use display_flags for sync edge
[karo-tx-linux.git] / drivers / gpu / drm / omapdrm / omap_connector.c
1 /*
2  * drivers/gpu/drm/omapdrm/omap_connector.c
3  *
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
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_crtc.h>
22 #include <drm/drm_crtc_helper.h>
23
24 #include "omap_drv.h"
25
26 /*
27  * connector funcs
28  */
29
30 #define to_omap_connector(x) container_of(x, struct omap_connector, base)
31
32 struct omap_connector {
33         struct drm_connector base;
34         struct omap_dss_device *dssdev;
35         bool hdmi_mode;
36 };
37
38 bool omap_connector_get_hdmi_mode(struct drm_connector *connector)
39 {
40         struct omap_connector *omap_connector = to_omap_connector(connector);
41
42         return omap_connector->hdmi_mode;
43 }
44
45 void copy_timings_omap_to_drm(struct drm_display_mode *mode,
46                 struct omap_video_timings *timings)
47 {
48         mode->clock = timings->pixelclock / 1000;
49
50         mode->hdisplay = timings->hactive;
51         mode->hsync_start = mode->hdisplay + timings->hfront_porch;
52         mode->hsync_end = mode->hsync_start + timings->hsync_len;
53         mode->htotal = mode->hsync_end + timings->hback_porch;
54
55         mode->vdisplay = timings->vactive;
56         mode->vsync_start = mode->vdisplay + timings->vfront_porch;
57         mode->vsync_end = mode->vsync_start + timings->vsync_len;
58         mode->vtotal = mode->vsync_end + timings->vback_porch;
59
60         mode->flags = 0;
61
62         if (timings->flags & DISPLAY_FLAGS_INTERLACED)
63                 mode->flags |= DRM_MODE_FLAG_INTERLACE;
64
65         if (timings->flags & DISPLAY_FLAGS_DOUBLECLK)
66                 mode->flags |= DRM_MODE_FLAG_DBLCLK;
67
68         if (timings->flags & DISPLAY_FLAGS_HSYNC_HIGH)
69                 mode->flags |= DRM_MODE_FLAG_PHSYNC;
70         else
71                 mode->flags |= DRM_MODE_FLAG_NHSYNC;
72
73         if (timings->flags & DISPLAY_FLAGS_VSYNC_HIGH)
74                 mode->flags |= DRM_MODE_FLAG_PVSYNC;
75         else
76                 mode->flags |= DRM_MODE_FLAG_NVSYNC;
77 }
78
79 void copy_timings_drm_to_omap(struct omap_video_timings *timings,
80                 struct drm_display_mode *mode)
81 {
82         timings->pixelclock = mode->clock * 1000;
83
84         timings->hactive = mode->hdisplay;
85         timings->hfront_porch = mode->hsync_start - mode->hdisplay;
86         timings->hsync_len = mode->hsync_end - mode->hsync_start;
87         timings->hback_porch = mode->htotal - mode->hsync_end;
88
89         timings->vactive = mode->vdisplay;
90         timings->vfront_porch = mode->vsync_start - mode->vdisplay;
91         timings->vsync_len = mode->vsync_end - mode->vsync_start;
92         timings->vback_porch = mode->vtotal - mode->vsync_end;
93
94         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
95                 timings->flags |= DISPLAY_FLAGS_INTERLACED;
96
97         if (mode->flags & DRM_MODE_FLAG_DBLCLK)
98                 timings->flags |= DISPLAY_FLAGS_DOUBLECLK;
99
100         if (mode->flags & DRM_MODE_FLAG_PHSYNC)
101                 timings->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
102         else
103                 timings->flags |= DISPLAY_FLAGS_HSYNC_LOW;
104
105         if (mode->flags & DRM_MODE_FLAG_PVSYNC)
106                 timings->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
107         else
108                 timings->flags |= DISPLAY_FLAGS_VSYNC_LOW;
109
110         timings->flags |= DISPLAY_FLAGS_DE_HIGH |
111                           DISPLAY_FLAGS_PIXDATA_POSEDGE |
112                           DISPLAY_FLAGS_SYNC_NEGEDGE;
113 }
114
115 static enum drm_connector_status omap_connector_detect(
116                 struct drm_connector *connector, bool force)
117 {
118         struct omap_connector *omap_connector = to_omap_connector(connector);
119         struct omap_dss_device *dssdev = omap_connector->dssdev;
120         struct omap_dss_driver *dssdrv = dssdev->driver;
121         enum drm_connector_status ret;
122
123         if (dssdrv->detect) {
124                 if (dssdrv->detect(dssdev))
125                         ret = connector_status_connected;
126                 else
127                         ret = connector_status_disconnected;
128         } else if (dssdev->type == OMAP_DISPLAY_TYPE_DPI ||
129                         dssdev->type == OMAP_DISPLAY_TYPE_DBI ||
130                         dssdev->type == OMAP_DISPLAY_TYPE_SDI ||
131                         dssdev->type == OMAP_DISPLAY_TYPE_DSI) {
132                 ret = connector_status_connected;
133         } else {
134                 ret = connector_status_unknown;
135         }
136
137         VERB("%s: %d (force=%d)", omap_connector->dssdev->name, ret, force);
138
139         return ret;
140 }
141
142 static void omap_connector_destroy(struct drm_connector *connector)
143 {
144         struct omap_connector *omap_connector = to_omap_connector(connector);
145         struct omap_dss_device *dssdev = omap_connector->dssdev;
146
147         DBG("%s", omap_connector->dssdev->name);
148         drm_connector_unregister(connector);
149         drm_connector_cleanup(connector);
150         kfree(omap_connector);
151
152         omap_dss_put_device(dssdev);
153 }
154
155 #define MAX_EDID  512
156
157 static int omap_connector_get_modes(struct drm_connector *connector)
158 {
159         struct omap_connector *omap_connector = to_omap_connector(connector);
160         struct omap_dss_device *dssdev = omap_connector->dssdev;
161         struct omap_dss_driver *dssdrv = dssdev->driver;
162         struct drm_device *dev = connector->dev;
163         int n = 0;
164
165         DBG("%s", omap_connector->dssdev->name);
166
167         /* if display exposes EDID, then we parse that in the normal way to
168          * build table of supported modes.. otherwise (ie. fixed resolution
169          * LCD panels) we just return a single mode corresponding to the
170          * currently configured timings:
171          */
172         if (dssdrv->read_edid) {
173                 void *edid = kzalloc(MAX_EDID, GFP_KERNEL);
174
175                 if ((dssdrv->read_edid(dssdev, edid, MAX_EDID) > 0) &&
176                                 drm_edid_is_valid(edid)) {
177                         drm_mode_connector_update_edid_property(
178                                         connector, edid);
179                         n = drm_add_edid_modes(connector, edid);
180
181                         omap_connector->hdmi_mode =
182                                 drm_detect_hdmi_monitor(edid);
183                 } else {
184                         drm_mode_connector_update_edid_property(
185                                         connector, NULL);
186                 }
187
188                 kfree(edid);
189         } else {
190                 struct drm_display_mode *mode = drm_mode_create(dev);
191                 struct omap_video_timings timings = {0};
192
193                 dssdrv->get_timings(dssdev, &timings);
194
195                 copy_timings_omap_to_drm(mode, &timings);
196
197                 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
198                 drm_mode_set_name(mode);
199                 drm_mode_probed_add(connector, mode);
200
201                 n = 1;
202         }
203
204         return n;
205 }
206
207 static int omap_connector_mode_valid(struct drm_connector *connector,
208                                  struct drm_display_mode *mode)
209 {
210         struct omap_connector *omap_connector = to_omap_connector(connector);
211         struct omap_dss_device *dssdev = omap_connector->dssdev;
212         struct omap_dss_driver *dssdrv = dssdev->driver;
213         struct omap_video_timings timings = {0};
214         struct drm_device *dev = connector->dev;
215         struct drm_display_mode *new_mode;
216         int r, ret = MODE_BAD;
217
218         copy_timings_drm_to_omap(&timings, mode);
219         mode->vrefresh = drm_mode_vrefresh(mode);
220
221         /*
222          * if the panel driver doesn't have a check_timings, it's most likely
223          * a fixed resolution panel, check if the timings match with the
224          * panel's timings
225          */
226         if (dssdrv->check_timings) {
227                 r = dssdrv->check_timings(dssdev, &timings);
228         } else {
229                 struct omap_video_timings t = {0};
230
231                 dssdrv->get_timings(dssdev, &t);
232
233                 if (memcmp(&timings, &t, sizeof(struct omap_video_timings)))
234                         r = -EINVAL;
235                 else
236                         r = 0;
237         }
238
239         if (!r) {
240                 /* check if vrefresh is still valid */
241                 new_mode = drm_mode_duplicate(dev, mode);
242                 new_mode->clock = timings.pixelclock / 1000;
243                 new_mode->vrefresh = 0;
244                 if (mode->vrefresh == drm_mode_vrefresh(new_mode))
245                         ret = MODE_OK;
246                 drm_mode_destroy(dev, new_mode);
247         }
248
249         DBG("connector: mode %s: "
250                         "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
251                         (ret == MODE_OK) ? "valid" : "invalid",
252                         mode->base.id, mode->name, mode->vrefresh, mode->clock,
253                         mode->hdisplay, mode->hsync_start,
254                         mode->hsync_end, mode->htotal,
255                         mode->vdisplay, mode->vsync_start,
256                         mode->vsync_end, mode->vtotal, mode->type, mode->flags);
257
258         return ret;
259 }
260
261 static const struct drm_connector_funcs omap_connector_funcs = {
262         .dpms = drm_atomic_helper_connector_dpms,
263         .reset = drm_atomic_helper_connector_reset,
264         .detect = omap_connector_detect,
265         .fill_modes = drm_helper_probe_single_connector_modes,
266         .destroy = omap_connector_destroy,
267         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
268         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
269 };
270
271 static const struct drm_connector_helper_funcs omap_connector_helper_funcs = {
272         .get_modes = omap_connector_get_modes,
273         .mode_valid = omap_connector_mode_valid,
274 };
275
276 /* initialize connector */
277 struct drm_connector *omap_connector_init(struct drm_device *dev,
278                 int connector_type, struct omap_dss_device *dssdev,
279                 struct drm_encoder *encoder)
280 {
281         struct drm_connector *connector = NULL;
282         struct omap_connector *omap_connector;
283
284         DBG("%s", dssdev->name);
285
286         omap_dss_get_device(dssdev);
287
288         omap_connector = kzalloc(sizeof(struct omap_connector), GFP_KERNEL);
289         if (!omap_connector)
290                 goto fail;
291
292         omap_connector->dssdev = dssdev;
293
294         connector = &omap_connector->base;
295
296         drm_connector_init(dev, connector, &omap_connector_funcs,
297                                 connector_type);
298         drm_connector_helper_add(connector, &omap_connector_helper_funcs);
299
300 #if 0 /* enable when dss2 supports hotplug */
301         if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_HPD)
302                 connector->polled = 0;
303         else
304 #endif
305                 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
306                                 DRM_CONNECTOR_POLL_DISCONNECT;
307
308         connector->interlace_allowed = 1;
309         connector->doublescan_allowed = 0;
310
311         drm_connector_register(connector);
312
313         return connector;
314
315 fail:
316         if (connector)
317                 omap_connector_destroy(connector);
318
319         return NULL;
320 }