Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[~shefty/rdma-dev.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 "drmP.h"
29 #include "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
151                 kfree(connector->display_info.raw_edid);
152                 connector->display_info.raw_edid = edid;
153         } else {
154                 struct drm_display_mode *mode = drm_mode_create(connector->dev);
155                 struct exynos_drm_panel_info *panel;
156
157                 if (display_ops->get_panel)
158                         panel = display_ops->get_panel(manager->dev);
159                 else {
160                         drm_mode_destroy(connector->dev, mode);
161                         return 0;
162                 }
163
164                 convert_to_display_mode(mode, panel);
165                 connector->display_info.width_mm = mode->width_mm;
166                 connector->display_info.height_mm = mode->height_mm;
167
168                 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
169                 drm_mode_set_name(mode);
170                 drm_mode_probed_add(connector, mode);
171
172                 count = 1;
173         }
174
175         return count;
176 }
177
178 static int exynos_drm_connector_mode_valid(struct drm_connector *connector,
179                                             struct drm_display_mode *mode)
180 {
181         struct exynos_drm_connector *exynos_connector =
182                                         to_exynos_connector(connector);
183         struct exynos_drm_manager *manager = exynos_connector->manager;
184         struct exynos_drm_display_ops *display_ops = manager->display_ops;
185         struct fb_videomode timing;
186         int ret = MODE_BAD;
187
188         DRM_DEBUG_KMS("%s\n", __FILE__);
189
190         convert_to_video_timing(&timing, mode);
191
192         if (display_ops && display_ops->check_timing)
193                 if (!display_ops->check_timing(manager->dev, (void *)&timing))
194                         ret = MODE_OK;
195
196         return ret;
197 }
198
199 struct drm_encoder *exynos_drm_best_encoder(struct drm_connector *connector)
200 {
201         struct drm_device *dev = connector->dev;
202         struct exynos_drm_connector *exynos_connector =
203                                         to_exynos_connector(connector);
204         struct drm_mode_object *obj;
205         struct drm_encoder *encoder;
206
207         DRM_DEBUG_KMS("%s\n", __FILE__);
208
209         obj = drm_mode_object_find(dev, exynos_connector->encoder_id,
210                                    DRM_MODE_OBJECT_ENCODER);
211         if (!obj) {
212                 DRM_DEBUG_KMS("Unknown ENCODER ID %d\n",
213                                 exynos_connector->encoder_id);
214                 return NULL;
215         }
216
217         encoder = obj_to_encoder(obj);
218
219         return encoder;
220 }
221
222 static struct drm_connector_helper_funcs exynos_connector_helper_funcs = {
223         .get_modes      = exynos_drm_connector_get_modes,
224         .mode_valid     = exynos_drm_connector_mode_valid,
225         .best_encoder   = exynos_drm_best_encoder,
226 };
227
228 static int exynos_drm_connector_fill_modes(struct drm_connector *connector,
229                                 unsigned int max_width, unsigned int max_height)
230 {
231         struct exynos_drm_connector *exynos_connector =
232                                         to_exynos_connector(connector);
233         struct exynos_drm_manager *manager = exynos_connector->manager;
234         struct exynos_drm_manager_ops *ops = manager->ops;
235         unsigned int width, height;
236
237         width = max_width;
238         height = max_height;
239
240         /*
241          * if specific driver want to find desired_mode using maxmum
242          * resolution then get max width and height from that driver.
243          */
244         if (ops && ops->get_max_resol)
245                 ops->get_max_resol(manager->dev, &width, &height);
246
247         return drm_helper_probe_single_connector_modes(connector, width,
248                                                         height);
249 }
250
251 /* get detection status of display device. */
252 static enum drm_connector_status
253 exynos_drm_connector_detect(struct drm_connector *connector, bool force)
254 {
255         struct exynos_drm_connector *exynos_connector =
256                                         to_exynos_connector(connector);
257         struct exynos_drm_manager *manager = exynos_connector->manager;
258         struct exynos_drm_display_ops *display_ops =
259                                         manager->display_ops;
260         enum drm_connector_status status = connector_status_disconnected;
261
262         DRM_DEBUG_KMS("%s\n", __FILE__);
263
264         if (display_ops && display_ops->is_connected) {
265                 if (display_ops->is_connected(manager->dev))
266                         status = connector_status_connected;
267                 else
268                         status = connector_status_disconnected;
269         }
270
271         return status;
272 }
273
274 static void exynos_drm_connector_destroy(struct drm_connector *connector)
275 {
276         struct exynos_drm_connector *exynos_connector =
277                 to_exynos_connector(connector);
278
279         DRM_DEBUG_KMS("%s\n", __FILE__);
280
281         drm_sysfs_connector_remove(connector);
282         drm_connector_cleanup(connector);
283         kfree(exynos_connector);
284 }
285
286 static struct drm_connector_funcs exynos_connector_funcs = {
287         .dpms           = drm_helper_connector_dpms,
288         .fill_modes     = exynos_drm_connector_fill_modes,
289         .detect         = exynos_drm_connector_detect,
290         .destroy        = exynos_drm_connector_destroy,
291 };
292
293 struct drm_connector *exynos_drm_connector_create(struct drm_device *dev,
294                                                    struct drm_encoder *encoder)
295 {
296         struct exynos_drm_connector *exynos_connector;
297         struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
298         struct drm_connector *connector;
299         int type;
300         int err;
301
302         DRM_DEBUG_KMS("%s\n", __FILE__);
303
304         exynos_connector = kzalloc(sizeof(*exynos_connector), GFP_KERNEL);
305         if (!exynos_connector) {
306                 DRM_ERROR("failed to allocate connector\n");
307                 return NULL;
308         }
309
310         connector = &exynos_connector->drm_connector;
311
312         switch (manager->display_ops->type) {
313         case EXYNOS_DISPLAY_TYPE_HDMI:
314                 type = DRM_MODE_CONNECTOR_HDMIA;
315                 connector->interlace_allowed = true;
316                 connector->polled = DRM_CONNECTOR_POLL_HPD;
317                 break;
318         case EXYNOS_DISPLAY_TYPE_VIDI:
319                 type = DRM_MODE_CONNECTOR_VIRTUAL;
320                 connector->polled = DRM_CONNECTOR_POLL_HPD;
321                 break;
322         default:
323                 type = DRM_MODE_CONNECTOR_Unknown;
324                 break;
325         }
326
327         drm_connector_init(dev, connector, &exynos_connector_funcs, type);
328         drm_connector_helper_add(connector, &exynos_connector_helper_funcs);
329
330         err = drm_sysfs_connector_add(connector);
331         if (err)
332                 goto err_connector;
333
334         exynos_connector->encoder_id = encoder->base.id;
335         exynos_connector->manager = manager;
336         connector->encoder = encoder;
337
338         err = drm_mode_connector_attach_encoder(connector, encoder);
339         if (err) {
340                 DRM_ERROR("failed to attach a connector to a encoder\n");
341                 goto err_sysfs;
342         }
343
344         DRM_DEBUG_KMS("connector has been created\n");
345
346         return connector;
347
348 err_sysfs:
349         drm_sysfs_connector_remove(connector);
350 err_connector:
351         drm_connector_cleanup(connector);
352         kfree(exynos_connector);
353         return NULL;
354 }