Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[linux-3.10.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 "omap_drv.h"
21
22 #include "drm_crtc.h"
23 #include "drm_crtc_helper.h"
24
25 /*
26  * connector funcs
27  */
28
29 #define to_omap_connector(x) container_of(x, struct omap_connector, base)
30
31 struct omap_connector {
32         struct drm_connector base;
33         struct omap_dss_device *dssdev;
34         struct drm_encoder *encoder;
35 };
36
37 void copy_timings_omap_to_drm(struct drm_display_mode *mode,
38                 struct omap_video_timings *timings)
39 {
40         mode->clock = timings->pixel_clock;
41
42         mode->hdisplay = timings->x_res;
43         mode->hsync_start = mode->hdisplay + timings->hfp;
44         mode->hsync_end = mode->hsync_start + timings->hsw;
45         mode->htotal = mode->hsync_end + timings->hbp;
46
47         mode->vdisplay = timings->y_res;
48         mode->vsync_start = mode->vdisplay + timings->vfp;
49         mode->vsync_end = mode->vsync_start + timings->vsw;
50         mode->vtotal = mode->vsync_end + timings->vbp;
51
52         mode->flags = 0;
53
54         if (timings->interlace)
55                 mode->flags |= DRM_MODE_FLAG_INTERLACE;
56
57         if (timings->hsync_level == OMAPDSS_SIG_ACTIVE_HIGH)
58                 mode->flags |= DRM_MODE_FLAG_PHSYNC;
59         else
60                 mode->flags |= DRM_MODE_FLAG_NHSYNC;
61
62         if (timings->vsync_level == OMAPDSS_SIG_ACTIVE_HIGH)
63                 mode->flags |= DRM_MODE_FLAG_PVSYNC;
64         else
65                 mode->flags |= DRM_MODE_FLAG_NVSYNC;
66 }
67
68 void copy_timings_drm_to_omap(struct omap_video_timings *timings,
69                 struct drm_display_mode *mode)
70 {
71         timings->pixel_clock = mode->clock;
72
73         timings->x_res = mode->hdisplay;
74         timings->hfp = mode->hsync_start - mode->hdisplay;
75         timings->hsw = mode->hsync_end - mode->hsync_start;
76         timings->hbp = mode->htotal - mode->hsync_end;
77
78         timings->y_res = mode->vdisplay;
79         timings->vfp = mode->vsync_start - mode->vdisplay;
80         timings->vsw = mode->vsync_end - mode->vsync_start;
81         timings->vbp = mode->vtotal - mode->vsync_end;
82
83         timings->interlace = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
84
85         if (mode->flags & DRM_MODE_FLAG_PHSYNC)
86                 timings->hsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
87         else
88                 timings->hsync_level = OMAPDSS_SIG_ACTIVE_LOW;
89
90         if (mode->flags & DRM_MODE_FLAG_PVSYNC)
91                 timings->vsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
92         else
93                 timings->vsync_level = OMAPDSS_SIG_ACTIVE_LOW;
94
95         timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
96         timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
97         timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES;
98 }
99
100 static enum drm_connector_status omap_connector_detect(
101                 struct drm_connector *connector, bool force)
102 {
103         struct omap_connector *omap_connector = to_omap_connector(connector);
104         struct omap_dss_device *dssdev = omap_connector->dssdev;
105         struct omap_dss_driver *dssdrv = dssdev->driver;
106         enum drm_connector_status ret;
107
108         if (dssdrv->detect) {
109                 if (dssdrv->detect(dssdev))
110                         ret = connector_status_connected;
111                 else
112                         ret = connector_status_disconnected;
113         } else {
114                 ret = connector_status_unknown;
115         }
116
117         VERB("%s: %d (force=%d)", omap_connector->dssdev->name, ret, force);
118
119         return ret;
120 }
121
122 static void omap_connector_destroy(struct drm_connector *connector)
123 {
124         struct omap_connector *omap_connector = to_omap_connector(connector);
125         struct omap_dss_device *dssdev = omap_connector->dssdev;
126
127         DBG("%s", omap_connector->dssdev->name);
128         drm_sysfs_connector_remove(connector);
129         drm_connector_cleanup(connector);
130         kfree(omap_connector);
131
132         omap_dss_put_device(dssdev);
133 }
134
135 #define MAX_EDID  512
136
137 static int omap_connector_get_modes(struct drm_connector *connector)
138 {
139         struct omap_connector *omap_connector = to_omap_connector(connector);
140         struct omap_dss_device *dssdev = omap_connector->dssdev;
141         struct omap_dss_driver *dssdrv = dssdev->driver;
142         struct drm_device *dev = connector->dev;
143         int n = 0;
144
145         DBG("%s", omap_connector->dssdev->name);
146
147         /* if display exposes EDID, then we parse that in the normal way to
148          * build table of supported modes.. otherwise (ie. fixed resolution
149          * LCD panels) we just return a single mode corresponding to the
150          * currently configured timings:
151          */
152         if (dssdrv->read_edid) {
153                 void *edid = kzalloc(MAX_EDID, GFP_KERNEL);
154
155                 if ((dssdrv->read_edid(dssdev, edid, MAX_EDID) > 0) &&
156                                 drm_edid_is_valid(edid)) {
157                         drm_mode_connector_update_edid_property(
158                                         connector, edid);
159                         n = drm_add_edid_modes(connector, edid);
160                 } else {
161                         drm_mode_connector_update_edid_property(
162                                         connector, NULL);
163                 }
164                 kfree(edid);
165         } else {
166                 struct drm_display_mode *mode = drm_mode_create(dev);
167                 struct omap_video_timings timings = {0};
168
169                 dssdrv->get_timings(dssdev, &timings);
170
171                 copy_timings_omap_to_drm(mode, &timings);
172
173                 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
174                 drm_mode_set_name(mode);
175                 drm_mode_probed_add(connector, mode);
176
177                 n = 1;
178         }
179
180         return n;
181 }
182
183 static int omap_connector_mode_valid(struct drm_connector *connector,
184                                  struct drm_display_mode *mode)
185 {
186         struct omap_connector *omap_connector = to_omap_connector(connector);
187         struct omap_dss_device *dssdev = omap_connector->dssdev;
188         struct omap_dss_driver *dssdrv = dssdev->driver;
189         struct omap_video_timings timings = {0};
190         struct drm_device *dev = connector->dev;
191         struct drm_display_mode *new_mode;
192         int ret = MODE_BAD;
193
194         copy_timings_drm_to_omap(&timings, mode);
195         mode->vrefresh = drm_mode_vrefresh(mode);
196
197         if (!dssdrv->check_timings(dssdev, &timings)) {
198                 /* check if vrefresh is still valid */
199                 new_mode = drm_mode_duplicate(dev, mode);
200                 new_mode->clock = timings.pixel_clock;
201                 new_mode->vrefresh = 0;
202                 if (mode->vrefresh == drm_mode_vrefresh(new_mode))
203                         ret = MODE_OK;
204                 drm_mode_destroy(dev, new_mode);
205         }
206
207         DBG("connector: mode %s: "
208                         "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
209                         (ret == MODE_OK) ? "valid" : "invalid",
210                         mode->base.id, mode->name, mode->vrefresh, mode->clock,
211                         mode->hdisplay, mode->hsync_start,
212                         mode->hsync_end, mode->htotal,
213                         mode->vdisplay, mode->vsync_start,
214                         mode->vsync_end, mode->vtotal, mode->type, mode->flags);
215
216         return ret;
217 }
218
219 struct drm_encoder *omap_connector_attached_encoder(
220                 struct drm_connector *connector)
221 {
222         struct omap_connector *omap_connector = to_omap_connector(connector);
223         return omap_connector->encoder;
224 }
225
226 static const struct drm_connector_funcs omap_connector_funcs = {
227         .dpms = drm_helper_connector_dpms,
228         .detect = omap_connector_detect,
229         .fill_modes = drm_helper_probe_single_connector_modes,
230         .destroy = omap_connector_destroy,
231 };
232
233 static const struct drm_connector_helper_funcs omap_connector_helper_funcs = {
234         .get_modes = omap_connector_get_modes,
235         .mode_valid = omap_connector_mode_valid,
236         .best_encoder = omap_connector_attached_encoder,
237 };
238
239 /* flush an area of the framebuffer (in case of manual update display that
240  * is not automatically flushed)
241  */
242 void omap_connector_flush(struct drm_connector *connector,
243                 int x, int y, int w, int h)
244 {
245         struct omap_connector *omap_connector = to_omap_connector(connector);
246
247         /* TODO: enable when supported in dss */
248         VERB("%s: %d,%d, %dx%d", omap_connector->dssdev->name, x, y, w, h);
249 }
250
251 /* initialize connector */
252 struct drm_connector *omap_connector_init(struct drm_device *dev,
253                 int connector_type, struct omap_dss_device *dssdev,
254                 struct drm_encoder *encoder)
255 {
256         struct drm_connector *connector = NULL;
257         struct omap_connector *omap_connector;
258
259         DBG("%s", dssdev->name);
260
261         omap_dss_get_device(dssdev);
262
263         omap_connector = kzalloc(sizeof(struct omap_connector), GFP_KERNEL);
264         if (!omap_connector)
265                 goto fail;
266
267         omap_connector->dssdev = dssdev;
268         omap_connector->encoder = encoder;
269
270         connector = &omap_connector->base;
271
272         drm_connector_init(dev, connector, &omap_connector_funcs,
273                                 connector_type);
274         drm_connector_helper_add(connector, &omap_connector_helper_funcs);
275
276 #if 0 /* enable when dss2 supports hotplug */
277         if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_HPD)
278                 connector->polled = 0;
279         else
280 #endif
281                 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
282                                 DRM_CONNECTOR_POLL_DISCONNECT;
283
284         connector->interlace_allowed = 1;
285         connector->doublescan_allowed = 0;
286
287         drm_sysfs_connector_add(connector);
288
289         return connector;
290
291 fail:
292         if (connector)
293                 omap_connector_destroy(connector);
294
295         return NULL;
296 }