Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[linux-3.10.git] / drivers / gpu / drm / exynos / exynos_drm_connector.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3  * Authors:
4  *      Inki Dae <inki.dae@samsung.com>
5  *      Joonyoung Shim <jy0922.shim@samsung.com>
6  *      Seung-Woo Kim <sw0312.kim@samsung.com>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #include <drm/drmP.h>
29 #include <drm/drm_crtc_helper.h>
30
31 #include <drm/exynos_drm.h>
32 #include "exynos_drm_drv.h"
33 #include "exynos_drm_encoder.h"
34
35 #define MAX_EDID 256
36 #define to_exynos_connector(x)  container_of(x, struct exynos_drm_connector,\
37                                 drm_connector)
38
39 struct exynos_drm_connector {
40         struct drm_connector    drm_connector;
41         uint32_t                encoder_id;
42         struct exynos_drm_manager *manager;
43 };
44
45 /* convert exynos_video_timings to drm_display_mode */
46 static inline void
47 convert_to_display_mode(struct drm_display_mode *mode,
48                         struct exynos_drm_panel_info *panel)
49 {
50         struct fb_videomode *timing = &panel->timing;
51         DRM_DEBUG_KMS("%s\n", __FILE__);
52
53         mode->clock = timing->pixclock / 1000;
54         mode->vrefresh = timing->refresh;
55
56         mode->hdisplay = timing->xres;
57         mode->hsync_start = mode->hdisplay + timing->right_margin;
58         mode->hsync_end = mode->hsync_start + timing->hsync_len;
59         mode->htotal = mode->hsync_end + timing->left_margin;
60
61         mode->vdisplay = timing->yres;
62         mode->vsync_start = mode->vdisplay + timing->lower_margin;
63         mode->vsync_end = mode->vsync_start + timing->vsync_len;
64         mode->vtotal = mode->vsync_end + timing->upper_margin;
65         mode->width_mm = panel->width_mm;
66         mode->height_mm = panel->height_mm;
67
68         if (timing->vmode & FB_VMODE_INTERLACED)
69                 mode->flags |= DRM_MODE_FLAG_INTERLACE;
70
71         if (timing->vmode & FB_VMODE_DOUBLE)
72                 mode->flags |= DRM_MODE_FLAG_DBLSCAN;
73 }
74
75 /* convert drm_display_mode to exynos_video_timings */
76 static inline void
77 convert_to_video_timing(struct fb_videomode *timing,
78                         struct drm_display_mode *mode)
79 {
80         DRM_DEBUG_KMS("%s\n", __FILE__);
81
82         memset(timing, 0, sizeof(*timing));
83
84         timing->pixclock = mode->clock * 1000;
85         timing->refresh = drm_mode_vrefresh(mode);
86
87         timing->xres = mode->hdisplay;
88         timing->right_margin = mode->hsync_start - mode->hdisplay;
89         timing->hsync_len = mode->hsync_end - mode->hsync_start;
90         timing->left_margin = mode->htotal - mode->hsync_end;
91
92         timing->yres = mode->vdisplay;
93         timing->lower_margin = mode->vsync_start - mode->vdisplay;
94         timing->vsync_len = mode->vsync_end - mode->vsync_start;
95         timing->upper_margin = mode->vtotal - mode->vsync_end;
96
97         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
98                 timing->vmode = FB_VMODE_INTERLACED;
99         else
100                 timing->vmode = FB_VMODE_NONINTERLACED;
101
102         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
103                 timing->vmode |= FB_VMODE_DOUBLE;
104 }
105
106 static int exynos_drm_connector_get_modes(struct drm_connector *connector)
107 {
108         struct exynos_drm_connector *exynos_connector =
109                                         to_exynos_connector(connector);
110         struct exynos_drm_manager *manager = exynos_connector->manager;
111         struct exynos_drm_display_ops *display_ops = manager->display_ops;
112         unsigned int count;
113
114         DRM_DEBUG_KMS("%s\n", __FILE__);
115
116         if (!display_ops) {
117                 DRM_DEBUG_KMS("display_ops is null.\n");
118                 return 0;
119         }
120
121         /*
122          * if get_edid() exists then get_edid() callback of hdmi side
123          * is called to get edid data through i2c interface else
124          * get timing from the FIMD driver(display controller).
125          *
126          * P.S. in case of lcd panel, count is always 1 if success
127          * because lcd panel has only one mode.
128          */
129         if (display_ops->get_edid) {
130                 int ret;
131                 void *edid;
132
133                 edid = kzalloc(MAX_EDID, GFP_KERNEL);
134                 if (!edid) {
135                         DRM_ERROR("failed to allocate edid\n");
136                         return 0;
137                 }
138
139                 ret = display_ops->get_edid(manager->dev, connector,
140                                                 edid, MAX_EDID);
141                 if (ret < 0) {
142                         DRM_ERROR("failed to get edid data.\n");
143                         kfree(edid);
144                         edid = NULL;
145                         return 0;
146                 }
147
148                 drm_mode_connector_update_edid_property(connector, edid);
149                 count = drm_add_edid_modes(connector, edid);
150                 kfree(edid);
151         } else {
152                 struct drm_display_mode *mode = drm_mode_create(connector->dev);
153                 struct exynos_drm_panel_info *panel;
154
155                 if (display_ops->get_panel)
156                         panel = display_ops->get_panel(manager->dev);
157                 else {
158                         drm_mode_destroy(connector->dev, mode);
159                         return 0;
160                 }
161
162                 convert_to_display_mode(mode, panel);
163                 connector->display_info.width_mm = mode->width_mm;
164                 connector->display_info.height_mm = mode->height_mm;
165
166                 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
167                 drm_mode_set_name(mode);
168                 drm_mode_probed_add(connector, mode);
169
170                 count = 1;
171         }
172
173         return count;
174 }
175
176 static int exynos_drm_connector_mode_valid(struct drm_connector *connector,
177                                             struct drm_display_mode *mode)
178 {
179         struct exynos_drm_connector *exynos_connector =
180                                         to_exynos_connector(connector);
181         struct exynos_drm_manager *manager = exynos_connector->manager;
182         struct exynos_drm_display_ops *display_ops = manager->display_ops;
183         struct fb_videomode timing;
184         int ret = MODE_BAD;
185
186         DRM_DEBUG_KMS("%s\n", __FILE__);
187
188         convert_to_video_timing(&timing, mode);
189
190         if (display_ops && display_ops->check_timing)
191                 if (!display_ops->check_timing(manager->dev, (void *)&timing))
192                         ret = MODE_OK;
193
194         return ret;
195 }
196
197 static struct drm_encoder *exynos_drm_best_encoder(
198                                                 struct drm_connector *connector)
199 {
200         struct drm_device *dev = connector->dev;
201         struct exynos_drm_connector *exynos_connector =
202                                         to_exynos_connector(connector);
203         struct drm_mode_object *obj;
204         struct drm_encoder *encoder;
205
206         DRM_DEBUG_KMS("%s\n", __FILE__);
207
208         obj = drm_mode_object_find(dev, exynos_connector->encoder_id,
209                                    DRM_MODE_OBJECT_ENCODER);
210         if (!obj) {
211                 DRM_DEBUG_KMS("Unknown ENCODER ID %d\n",
212                                 exynos_connector->encoder_id);
213                 return NULL;
214         }
215
216         encoder = obj_to_encoder(obj);
217
218         return encoder;
219 }
220
221 static struct drm_connector_helper_funcs exynos_connector_helper_funcs = {
222         .get_modes      = exynos_drm_connector_get_modes,
223         .mode_valid     = exynos_drm_connector_mode_valid,
224         .best_encoder   = exynos_drm_best_encoder,
225 };
226
227 static int exynos_drm_connector_fill_modes(struct drm_connector *connector,
228                                 unsigned int max_width, unsigned int max_height)
229 {
230         struct exynos_drm_connector *exynos_connector =
231                                         to_exynos_connector(connector);
232         struct exynos_drm_manager *manager = exynos_connector->manager;
233         struct exynos_drm_manager_ops *ops = manager->ops;
234         unsigned int width, height;
235
236         width = max_width;
237         height = max_height;
238
239         /*
240          * if specific driver want to find desired_mode using maxmum
241          * resolution then get max width and height from that driver.
242          */
243         if (ops && ops->get_max_resol)
244                 ops->get_max_resol(manager->dev, &width, &height);
245
246         return drm_helper_probe_single_connector_modes(connector, width,
247                                                         height);
248 }
249
250 /* get detection status of display device. */
251 static enum drm_connector_status
252 exynos_drm_connector_detect(struct drm_connector *connector, bool force)
253 {
254         struct exynos_drm_connector *exynos_connector =
255                                         to_exynos_connector(connector);
256         struct exynos_drm_manager *manager = exynos_connector->manager;
257         struct exynos_drm_display_ops *display_ops =
258                                         manager->display_ops;
259         enum drm_connector_status status = connector_status_disconnected;
260
261         DRM_DEBUG_KMS("%s\n", __FILE__);
262
263         if (display_ops && display_ops->is_connected) {
264                 if (display_ops->is_connected(manager->dev))
265                         status = connector_status_connected;
266                 else
267                         status = connector_status_disconnected;
268         }
269
270         return status;
271 }
272
273 static void exynos_drm_connector_destroy(struct drm_connector *connector)
274 {
275         struct exynos_drm_connector *exynos_connector =
276                 to_exynos_connector(connector);
277
278         DRM_DEBUG_KMS("%s\n", __FILE__);
279
280         drm_sysfs_connector_remove(connector);
281         drm_connector_cleanup(connector);
282         kfree(exynos_connector);
283 }
284
285 static struct drm_connector_funcs exynos_connector_funcs = {
286         .dpms           = drm_helper_connector_dpms,
287         .fill_modes     = exynos_drm_connector_fill_modes,
288         .detect         = exynos_drm_connector_detect,
289         .destroy        = exynos_drm_connector_destroy,
290 };
291
292 struct drm_connector *exynos_drm_connector_create(struct drm_device *dev,
293                                                    struct drm_encoder *encoder)
294 {
295         struct exynos_drm_connector *exynos_connector;
296         struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
297         struct drm_connector *connector;
298         int type;
299         int err;
300
301         DRM_DEBUG_KMS("%s\n", __FILE__);
302
303         exynos_connector = kzalloc(sizeof(*exynos_connector), GFP_KERNEL);
304         if (!exynos_connector) {
305                 DRM_ERROR("failed to allocate connector\n");
306                 return NULL;
307         }
308
309         connector = &exynos_connector->drm_connector;
310
311         switch (manager->display_ops->type) {
312         case EXYNOS_DISPLAY_TYPE_HDMI:
313                 type = DRM_MODE_CONNECTOR_HDMIA;
314                 connector->interlace_allowed = true;
315                 connector->polled = DRM_CONNECTOR_POLL_HPD;
316                 break;
317         case EXYNOS_DISPLAY_TYPE_VIDI:
318                 type = DRM_MODE_CONNECTOR_VIRTUAL;
319                 connector->polled = DRM_CONNECTOR_POLL_HPD;
320                 break;
321         default:
322                 type = DRM_MODE_CONNECTOR_Unknown;
323                 break;
324         }
325
326         drm_connector_init(dev, connector, &exynos_connector_funcs, type);
327         drm_connector_helper_add(connector, &exynos_connector_helper_funcs);
328
329         err = drm_sysfs_connector_add(connector);
330         if (err)
331                 goto err_connector;
332
333         exynos_connector->encoder_id = encoder->base.id;
334         exynos_connector->manager = manager;
335         connector->encoder = encoder;
336
337         err = drm_mode_connector_attach_encoder(connector, encoder);
338         if (err) {
339                 DRM_ERROR("failed to attach a connector to a encoder\n");
340                 goto err_sysfs;
341         }
342
343         DRM_DEBUG_KMS("connector has been created\n");
344
345         return connector;
346
347 err_sysfs:
348         drm_sysfs_connector_remove(connector);
349 err_connector:
350         drm_connector_cleanup(connector);
351         kfree(exynos_connector);
352         return NULL;
353 }