0f68a28726739dd6b795ad066edb645799cfc6d2
[platform/adaptation/renesas_rcar/renesas_kernel.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         uint32_t                dpms;
44 };
45
46 /* convert exynos_video_timings to drm_display_mode */
47 static inline void
48 convert_to_display_mode(struct drm_display_mode *mode,
49                         struct exynos_drm_panel_info *panel)
50 {
51         struct fb_videomode *timing = &panel->timing;
52         DRM_DEBUG_KMS("%s\n", __FILE__);
53
54         mode->clock = timing->pixclock / 1000;
55         mode->vrefresh = timing->refresh;
56
57         mode->hdisplay = timing->xres;
58         mode->hsync_start = mode->hdisplay + timing->right_margin;
59         mode->hsync_end = mode->hsync_start + timing->hsync_len;
60         mode->htotal = mode->hsync_end + timing->left_margin;
61
62         mode->vdisplay = timing->yres;
63         mode->vsync_start = mode->vdisplay + timing->lower_margin;
64         mode->vsync_end = mode->vsync_start + timing->vsync_len;
65         mode->vtotal = mode->vsync_end + timing->upper_margin;
66         mode->width_mm = panel->width_mm;
67         mode->height_mm = panel->height_mm;
68
69         if (timing->vmode & FB_VMODE_INTERLACED)
70                 mode->flags |= DRM_MODE_FLAG_INTERLACE;
71
72         if (timing->vmode & FB_VMODE_DOUBLE)
73                 mode->flags |= DRM_MODE_FLAG_DBLSCAN;
74 }
75
76 /* convert drm_display_mode to exynos_video_timings */
77 static inline void
78 convert_to_video_timing(struct fb_videomode *timing,
79                         struct drm_display_mode *mode)
80 {
81         DRM_DEBUG_KMS("%s\n", __FILE__);
82
83         memset(timing, 0, sizeof(*timing));
84
85         timing->pixclock = mode->clock * 1000;
86         timing->refresh = drm_mode_vrefresh(mode);
87
88         timing->xres = mode->hdisplay;
89         timing->right_margin = mode->hsync_start - mode->hdisplay;
90         timing->hsync_len = mode->hsync_end - mode->hsync_start;
91         timing->left_margin = mode->htotal - mode->hsync_end;
92
93         timing->yres = mode->vdisplay;
94         timing->lower_margin = mode->vsync_start - mode->vdisplay;
95         timing->vsync_len = mode->vsync_end - mode->vsync_start;
96         timing->upper_margin = mode->vtotal - mode->vsync_end;
97
98         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
99                 timing->vmode = FB_VMODE_INTERLACED;
100         else
101                 timing->vmode = FB_VMODE_NONINTERLACED;
102
103         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
104                 timing->vmode |= FB_VMODE_DOUBLE;
105 }
106
107 static int exynos_drm_connector_get_modes(struct drm_connector *connector)
108 {
109         struct exynos_drm_connector *exynos_connector =
110                                         to_exynos_connector(connector);
111         struct exynos_drm_manager *manager = exynos_connector->manager;
112         struct exynos_drm_display_ops *display_ops = manager->display_ops;
113         unsigned int count;
114
115         DRM_DEBUG_KMS("%s\n", __FILE__);
116
117         if (!display_ops) {
118                 DRM_DEBUG_KMS("display_ops is null.\n");
119                 return 0;
120         }
121
122         /*
123          * if get_edid() exists then get_edid() callback of hdmi side
124          * is called to get edid data through i2c interface else
125          * get timing from the FIMD driver(display controller).
126          *
127          * P.S. in case of lcd panel, count is always 1 if success
128          * because lcd panel has only one mode.
129          */
130         if (display_ops->get_edid) {
131                 int ret;
132                 void *edid;
133
134                 edid = kzalloc(MAX_EDID, GFP_KERNEL);
135                 if (!edid) {
136                         DRM_ERROR("failed to allocate edid\n");
137                         return 0;
138                 }
139
140                 ret = display_ops->get_edid(manager->dev, connector,
141                                                 edid, MAX_EDID);
142                 if (ret < 0) {
143                         DRM_ERROR("failed to get edid data.\n");
144                         kfree(edid);
145                         edid = NULL;
146                         return 0;
147                 }
148
149                 drm_mode_connector_update_edid_property(connector, edid);
150                 count = drm_add_edid_modes(connector, edid);
151                 kfree(edid);
152         } else {
153                 struct exynos_drm_panel_info *panel;
154                 struct drm_display_mode *mode = drm_mode_create(connector->dev);
155                 if (!mode) {
156                         DRM_ERROR("failed to create a new display mode.\n");
157                         return 0;
158                 }
159
160                 if (display_ops->get_panel)
161                         panel = display_ops->get_panel(manager->dev);
162                 else {
163                         drm_mode_destroy(connector->dev, mode);
164                         return 0;
165                 }
166
167                 convert_to_display_mode(mode, panel);
168                 connector->display_info.width_mm = mode->width_mm;
169                 connector->display_info.height_mm = mode->height_mm;
170
171                 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
172                 drm_mode_set_name(mode);
173                 drm_mode_probed_add(connector, mode);
174
175                 count = 1;
176         }
177
178         return count;
179 }
180
181 static int exynos_drm_connector_mode_valid(struct drm_connector *connector,
182                                             struct drm_display_mode *mode)
183 {
184         struct exynos_drm_connector *exynos_connector =
185                                         to_exynos_connector(connector);
186         struct exynos_drm_manager *manager = exynos_connector->manager;
187         struct exynos_drm_display_ops *display_ops = manager->display_ops;
188         struct fb_videomode timing;
189         int ret = MODE_BAD;
190
191         DRM_DEBUG_KMS("%s\n", __FILE__);
192
193         convert_to_video_timing(&timing, mode);
194
195         if (display_ops && display_ops->check_timing)
196                 if (!display_ops->check_timing(manager->dev, (void *)&timing))
197                         ret = MODE_OK;
198
199         return ret;
200 }
201
202 struct drm_encoder *exynos_drm_best_encoder(struct drm_connector *connector)
203 {
204         struct drm_device *dev = connector->dev;
205         struct exynos_drm_connector *exynos_connector =
206                                         to_exynos_connector(connector);
207         struct drm_mode_object *obj;
208         struct drm_encoder *encoder;
209
210         DRM_DEBUG_KMS("%s\n", __FILE__);
211
212         obj = drm_mode_object_find(dev, exynos_connector->encoder_id,
213                                    DRM_MODE_OBJECT_ENCODER);
214         if (!obj) {
215                 DRM_DEBUG_KMS("Unknown ENCODER ID %d\n",
216                                 exynos_connector->encoder_id);
217                 return NULL;
218         }
219
220         encoder = obj_to_encoder(obj);
221
222         return encoder;
223 }
224
225 static struct drm_connector_helper_funcs exynos_connector_helper_funcs = {
226         .get_modes      = exynos_drm_connector_get_modes,
227         .mode_valid     = exynos_drm_connector_mode_valid,
228         .best_encoder   = exynos_drm_best_encoder,
229 };
230
231 void exynos_drm_display_power(struct drm_connector *connector, int mode)
232 {
233         struct drm_encoder *encoder = exynos_drm_best_encoder(connector);
234         struct exynos_drm_connector *exynos_connector;
235         struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
236         struct exynos_drm_display_ops *display_ops = manager->display_ops;
237
238         exynos_connector = to_exynos_connector(connector);
239
240         if (exynos_connector->dpms == mode) {
241                 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
242                 return;
243         }
244
245         if (display_ops && display_ops->power_on)
246                 display_ops->power_on(manager->dev, mode);
247
248         exynos_connector->dpms = mode;
249 }
250
251 static void exynos_drm_connector_dpms(struct drm_connector *connector,
252                                         int mode)
253 {
254         DRM_DEBUG_KMS("%s\n", __FILE__);
255
256         /*
257          * in case that drm_crtc_helper_set_mode() is called,
258          * encoder/crtc->funcs->dpms() will be just returned
259          * because they already were DRM_MODE_DPMS_ON so only
260          * exynos_drm_display_power() will be called.
261          */
262         drm_helper_connector_dpms(connector, mode);
263
264         exynos_drm_display_power(connector, mode);
265
266 }
267
268 static int exynos_drm_connector_fill_modes(struct drm_connector *connector,
269                                 unsigned int max_width, unsigned int max_height)
270 {
271         struct exynos_drm_connector *exynos_connector =
272                                         to_exynos_connector(connector);
273         struct exynos_drm_manager *manager = exynos_connector->manager;
274         struct exynos_drm_manager_ops *ops = manager->ops;
275         unsigned int width, height;
276
277         width = max_width;
278         height = max_height;
279
280         /*
281          * if specific driver want to find desired_mode using maxmum
282          * resolution then get max width and height from that driver.
283          */
284         if (ops && ops->get_max_resol)
285                 ops->get_max_resol(manager->dev, &width, &height);
286
287         return drm_helper_probe_single_connector_modes(connector, width,
288                                                         height);
289 }
290
291 /* get detection status of display device. */
292 static enum drm_connector_status
293 exynos_drm_connector_detect(struct drm_connector *connector, bool force)
294 {
295         struct exynos_drm_connector *exynos_connector =
296                                         to_exynos_connector(connector);
297         struct exynos_drm_manager *manager = exynos_connector->manager;
298         struct exynos_drm_display_ops *display_ops =
299                                         manager->display_ops;
300         enum drm_connector_status status = connector_status_disconnected;
301
302         DRM_DEBUG_KMS("%s\n", __FILE__);
303
304         if (display_ops && display_ops->is_connected) {
305                 if (display_ops->is_connected(manager->dev))
306                         status = connector_status_connected;
307                 else
308                         status = connector_status_disconnected;
309         }
310
311         return status;
312 }
313
314 static void exynos_drm_connector_destroy(struct drm_connector *connector)
315 {
316         struct exynos_drm_connector *exynos_connector =
317                 to_exynos_connector(connector);
318
319         DRM_DEBUG_KMS("%s\n", __FILE__);
320
321         drm_sysfs_connector_remove(connector);
322         drm_connector_cleanup(connector);
323         kfree(exynos_connector);
324 }
325
326 static struct drm_connector_funcs exynos_connector_funcs = {
327         .dpms           = exynos_drm_connector_dpms,
328         .fill_modes     = exynos_drm_connector_fill_modes,
329         .detect         = exynos_drm_connector_detect,
330         .destroy        = exynos_drm_connector_destroy,
331 };
332
333 struct drm_connector *exynos_drm_connector_create(struct drm_device *dev,
334                                                    struct drm_encoder *encoder)
335 {
336         struct exynos_drm_connector *exynos_connector;
337         struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
338         struct drm_connector *connector;
339         int type;
340         int err;
341
342         DRM_DEBUG_KMS("%s\n", __FILE__);
343
344         exynos_connector = kzalloc(sizeof(*exynos_connector), GFP_KERNEL);
345         if (!exynos_connector) {
346                 DRM_ERROR("failed to allocate connector\n");
347                 return NULL;
348         }
349
350         connector = &exynos_connector->drm_connector;
351
352         switch (manager->display_ops->type) {
353         case EXYNOS_DISPLAY_TYPE_HDMI:
354                 type = DRM_MODE_CONNECTOR_HDMIA;
355                 connector->interlace_allowed = true;
356                 connector->polled = DRM_CONNECTOR_POLL_HPD;
357                 break;
358         case EXYNOS_DISPLAY_TYPE_VIDI:
359                 type = DRM_MODE_CONNECTOR_VIRTUAL;
360                 connector->polled = DRM_CONNECTOR_POLL_HPD;
361                 break;
362         default:
363                 type = DRM_MODE_CONNECTOR_Unknown;
364                 break;
365         }
366
367         drm_connector_init(dev, connector, &exynos_connector_funcs, type);
368         drm_connector_helper_add(connector, &exynos_connector_helper_funcs);
369
370         err = drm_sysfs_connector_add(connector);
371         if (err)
372                 goto err_connector;
373
374         exynos_connector->encoder_id = encoder->base.id;
375         exynos_connector->manager = manager;
376         exynos_connector->dpms = DRM_MODE_DPMS_OFF;
377         connector->dpms = DRM_MODE_DPMS_OFF;
378         connector->encoder = encoder;
379
380         err = drm_mode_connector_attach_encoder(connector, encoder);
381         if (err) {
382                 DRM_ERROR("failed to attach a connector to a encoder\n");
383                 goto err_sysfs;
384         }
385
386         DRM_DEBUG_KMS("connector has been created\n");
387
388         return connector;
389
390 err_sysfs:
391         drm_sysfs_connector_remove(connector);
392 err_connector:
393         drm_connector_cleanup(connector);
394         kfree(exynos_connector);
395         return NULL;
396 }