Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / staging / omapdrm / omap_connector.c
1 /*
2  * drivers/staging/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 };
35
36 static inline void copy_timings_omap_to_drm(struct drm_display_mode *mode,
37                 struct omap_video_timings *timings)
38 {
39         mode->clock = timings->pixel_clock;
40
41         mode->hdisplay = timings->x_res;
42         mode->hsync_start = mode->hdisplay + timings->hfp;
43         mode->hsync_end = mode->hsync_start + timings->hsw;
44         mode->htotal = mode->hsync_end + timings->hbp;
45
46         mode->vdisplay = timings->y_res;
47         mode->vsync_start = mode->vdisplay + timings->vfp;
48         mode->vsync_end = mode->vsync_start + timings->vsw;
49         mode->vtotal = mode->vsync_end + timings->vbp;
50
51         /* note: whether or not it is interlaced, +/- h/vsync, etc,
52          * which should be set in the mode flags, is not exposed in
53          * the omap_video_timings struct.. but hdmi driver tracks
54          * those separately so all we have to have to set the mode
55          * is the way to recover these timings values, and the
56          * omap_dss_driver would do the rest.
57          */
58 }
59
60 static inline void copy_timings_drm_to_omap(struct omap_video_timings *timings,
61                 struct drm_display_mode *mode)
62 {
63         timings->pixel_clock = mode->clock;
64
65         timings->x_res = mode->hdisplay;
66         timings->hfp = mode->hsync_start - mode->hdisplay;
67         timings->hsw = mode->hsync_end - mode->hsync_start;
68         timings->hbp = mode->htotal - mode->hsync_end;
69
70         timings->y_res = mode->vdisplay;
71         timings->vfp = mode->vsync_start - mode->vdisplay;
72         timings->vsw = mode->vsync_end - mode->vsync_start;
73         timings->vbp = mode->vtotal - mode->vsync_end;
74 }
75
76 static void omap_connector_dpms(struct drm_connector *connector, int mode)
77 {
78         struct omap_connector *omap_connector = to_omap_connector(connector);
79         struct omap_dss_device *dssdev = omap_connector->dssdev;
80         int old_dpms;
81
82         DBG("%s: %d", dssdev->name, mode);
83
84         old_dpms = connector->dpms;
85
86         /* from off to on, do from crtc to connector */
87         if (mode < old_dpms)
88                 drm_helper_connector_dpms(connector, mode);
89
90         if (mode == DRM_MODE_DPMS_ON) {
91                 /* store resume info for suspended displays */
92                 switch (dssdev->state) {
93                 case OMAP_DSS_DISPLAY_SUSPENDED:
94                         dssdev->activate_after_resume = true;
95                         break;
96                 case OMAP_DSS_DISPLAY_DISABLED: {
97                         int ret = dssdev->driver->enable(dssdev);
98                         if (ret) {
99                                 DBG("%s: failed to enable: %d",
100                                                 dssdev->name, ret);
101                                 dssdev->driver->disable(dssdev);
102                         }
103                         break;
104                 }
105                 default:
106                         break;
107                 }
108         } else {
109                 /* TODO */
110         }
111
112         /* from on to off, do from connector to crtc */
113         if (mode > old_dpms)
114                 drm_helper_connector_dpms(connector, mode);
115 }
116
117 enum drm_connector_status omap_connector_detect(
118                 struct drm_connector *connector, bool force)
119 {
120         struct omap_connector *omap_connector = to_omap_connector(connector);
121         struct omap_dss_device *dssdev = omap_connector->dssdev;
122         struct omap_dss_driver *dssdrv = dssdev->driver;
123         enum drm_connector_status ret;
124
125         if (dssdrv->detect) {
126                 if (dssdrv->detect(dssdev)) {
127                         ret = connector_status_connected;
128                 } else {
129                         ret = connector_status_disconnected;
130                 }
131         } else {
132                 ret = connector_status_unknown;
133         }
134
135         VERB("%s: %d (force=%d)", omap_connector->dssdev->name, ret, force);
136
137         return ret;
138 }
139
140 static void omap_connector_destroy(struct drm_connector *connector)
141 {
142         struct omap_connector *omap_connector = to_omap_connector(connector);
143         struct omap_dss_device *dssdev = omap_connector->dssdev;
144
145         dssdev->driver->disable(dssdev);
146
147         DBG("%s", omap_connector->dssdev->name);
148         drm_sysfs_connector_remove(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                         kfree(connector->display_info.raw_edid);
181                         connector->display_info.raw_edid = edid;
182                 } else {
183                         drm_mode_connector_update_edid_property(
184                                         connector, NULL);
185                         connector->display_info.raw_edid = NULL;
186                         kfree(edid);
187                 }
188         } else {
189                 struct drm_display_mode *mode = drm_mode_create(dev);
190                 struct omap_video_timings timings;
191
192                 dssdrv->get_timings(dssdev, &timings);
193
194                 copy_timings_omap_to_drm(mode, &timings);
195
196                 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
197                 drm_mode_set_name(mode);
198                 drm_mode_probed_add(connector, mode);
199
200                 n = 1;
201         }
202
203         return n;
204 }
205
206 static int omap_connector_mode_valid(struct drm_connector *connector,
207                                  struct drm_display_mode *mode)
208 {
209         struct omap_connector *omap_connector = to_omap_connector(connector);
210         struct omap_dss_device *dssdev = omap_connector->dssdev;
211         struct omap_dss_driver *dssdrv = dssdev->driver;
212         struct omap_video_timings timings = {0};
213         struct drm_device *dev = connector->dev;
214         struct drm_display_mode *new_mode;
215         int ret = MODE_BAD;
216
217         copy_timings_drm_to_omap(&timings, mode);
218         mode->vrefresh = drm_mode_vrefresh(mode);
219
220         if (!dssdrv->check_timings(dssdev, &timings)) {
221                 /* check if vrefresh is still valid */
222                 new_mode = drm_mode_duplicate(dev, mode);
223                 new_mode->clock = timings.pixel_clock;
224                 new_mode->vrefresh = 0;
225                 if (mode->vrefresh == drm_mode_vrefresh(new_mode))
226                         ret = MODE_OK;
227                 drm_mode_destroy(dev, new_mode);
228         }
229
230         DBG("connector: mode %s: "
231                         "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
232                         (ret == MODE_OK) ? "valid" : "invalid",
233                         mode->base.id, mode->name, mode->vrefresh, mode->clock,
234                         mode->hdisplay, mode->hsync_start,
235                         mode->hsync_end, mode->htotal,
236                         mode->vdisplay, mode->vsync_start,
237                         mode->vsync_end, mode->vtotal, mode->type, mode->flags);
238
239         return ret;
240 }
241
242 struct drm_encoder *omap_connector_attached_encoder(
243                 struct drm_connector *connector)
244 {
245         int i;
246         struct omap_connector *omap_connector = to_omap_connector(connector);
247
248         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
249                 struct drm_mode_object *obj;
250
251                 if (connector->encoder_ids[i] == 0)
252                         break;
253
254                 obj = drm_mode_object_find(connector->dev,
255                                 connector->encoder_ids[i],
256                                 DRM_MODE_OBJECT_ENCODER);
257
258                 if (obj) {
259                         struct drm_encoder *encoder = obj_to_encoder(obj);
260                         struct omap_overlay_manager *mgr =
261                                         omap_encoder_get_manager(encoder);
262                         DBG("%s: found %s", omap_connector->dssdev->name,
263                                         mgr->name);
264                         return encoder;
265                 }
266         }
267
268         DBG("%s: no encoder", omap_connector->dssdev->name);
269
270         return NULL;
271 }
272
273 static const struct drm_connector_funcs omap_connector_funcs = {
274         .dpms = omap_connector_dpms,
275         .detect = omap_connector_detect,
276         .fill_modes = drm_helper_probe_single_connector_modes,
277         .destroy = omap_connector_destroy,
278 };
279
280 static const struct drm_connector_helper_funcs omap_connector_helper_funcs = {
281         .get_modes = omap_connector_get_modes,
282         .mode_valid = omap_connector_mode_valid,
283         .best_encoder = omap_connector_attached_encoder,
284 };
285
286 /* called from encoder when mode is set, to propagate settings to the dssdev */
287 void omap_connector_mode_set(struct drm_connector *connector,
288                 struct drm_display_mode *mode)
289 {
290         struct drm_device *dev = connector->dev;
291         struct omap_connector *omap_connector = to_omap_connector(connector);
292         struct omap_dss_device *dssdev = omap_connector->dssdev;
293         struct omap_dss_driver *dssdrv = dssdev->driver;
294         struct omap_video_timings timings;
295
296         copy_timings_drm_to_omap(&timings, mode);
297
298         DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
299                         omap_connector->dssdev->name,
300                         mode->base.id, mode->name, mode->vrefresh, mode->clock,
301                         mode->hdisplay, mode->hsync_start,
302                         mode->hsync_end, mode->htotal,
303                         mode->vdisplay, mode->vsync_start,
304                         mode->vsync_end, mode->vtotal, mode->type, mode->flags);
305
306         if (dssdrv->check_timings(dssdev, &timings)) {
307                 dev_err(dev->dev, "could not set timings\n");
308                 return;
309         }
310
311         dssdrv->set_timings(dssdev, &timings);
312 }
313
314 /* flush an area of the framebuffer (in case of manual update display that
315  * is not automatically flushed)
316  */
317 void omap_connector_flush(struct drm_connector *connector,
318                 int x, int y, int w, int h)
319 {
320         struct omap_connector *omap_connector = to_omap_connector(connector);
321
322         /* TODO: enable when supported in dss */
323         VERB("%s: %d,%d, %dx%d", omap_connector->dssdev->name, x, y, w, h);
324 }
325
326 /* initialize connector */
327 struct drm_connector *omap_connector_init(struct drm_device *dev,
328                 int connector_type, struct omap_dss_device *dssdev)
329 {
330         struct drm_connector *connector = NULL;
331         struct omap_connector *omap_connector;
332
333         DBG("%s", dssdev->name);
334
335         omap_dss_get_device(dssdev);
336
337         omap_connector = kzalloc(sizeof(struct omap_connector), GFP_KERNEL);
338         if (!omap_connector) {
339                 dev_err(dev->dev, "could not allocate connector\n");
340                 goto fail;
341         }
342
343         omap_connector->dssdev = dssdev;
344         connector = &omap_connector->base;
345
346         drm_connector_init(dev, connector, &omap_connector_funcs,
347                                 connector_type);
348         drm_connector_helper_add(connector, &omap_connector_helper_funcs);
349
350 #if 0 /* enable when dss2 supports hotplug */
351         if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_HPD)
352                 connector->polled = 0;
353         else
354 #endif
355                 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
356                                 DRM_CONNECTOR_POLL_DISCONNECT;
357
358         connector->interlace_allowed = 1;
359         connector->doublescan_allowed = 0;
360
361         drm_sysfs_connector_add(connector);
362
363         return connector;
364
365 fail:
366         if (connector) {
367                 omap_connector_destroy(connector);
368         }
369
370         return NULL;
371 }