2 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Inki Dae <inki.dae@samsung.com>
5 * Joonyoung Shim <jy0922.shim@samsung.com>
6 * Seung-Woo Kim <sw0312.kim@samsung.com>
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:
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
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.
29 #include <drm/drm_crtc_helper.h>
31 #include <drm/exynos_drm.h>
32 #include "exynos_drm_drv.h"
33 #include "exynos_drm_encoder.h"
36 #define to_exynos_connector(x) container_of(x, struct exynos_drm_connector,\
39 struct exynos_drm_connector {
40 struct drm_connector drm_connector;
42 struct exynos_drm_manager *manager;
46 /* convert exynos_video_timings to drm_display_mode */
48 convert_to_display_mode(struct drm_display_mode *mode,
49 struct exynos_drm_panel_info *panel)
51 struct fb_videomode *timing = &panel->timing;
52 DRM_DEBUG_KMS("%s\n", __FILE__);
54 mode->clock = timing->pixclock / 1000;
55 mode->vrefresh = timing->refresh;
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;
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;
69 if (timing->vmode & FB_VMODE_INTERLACED)
70 mode->flags |= DRM_MODE_FLAG_INTERLACE;
72 if (timing->vmode & FB_VMODE_DOUBLE)
73 mode->flags |= DRM_MODE_FLAG_DBLSCAN;
76 /* convert drm_display_mode to exynos_video_timings */
78 convert_to_video_timing(struct fb_videomode *timing,
79 struct drm_display_mode *mode)
81 DRM_DEBUG_KMS("%s\n", __FILE__);
83 memset(timing, 0, sizeof(*timing));
85 timing->pixclock = mode->clock * 1000;
86 timing->refresh = drm_mode_vrefresh(mode);
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;
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;
98 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
99 timing->vmode = FB_VMODE_INTERLACED;
101 timing->vmode = FB_VMODE_NONINTERLACED;
103 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
104 timing->vmode |= FB_VMODE_DOUBLE;
107 static int exynos_drm_connector_get_modes(struct drm_connector *connector)
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;
115 DRM_DEBUG_KMS("%s\n", __FILE__);
118 DRM_DEBUG_KMS("display_ops is null.\n");
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).
127 * P.S. in case of lcd panel, count is always 1 if success
128 * because lcd panel has only one mode.
130 if (display_ops->get_edid) {
134 edid = kzalloc(MAX_EDID, GFP_KERNEL);
136 DRM_ERROR("failed to allocate edid\n");
140 ret = display_ops->get_edid(manager->dev, connector,
143 DRM_ERROR("failed to get edid data.\n");
149 drm_mode_connector_update_edid_property(connector, edid);
150 count = drm_add_edid_modes(connector, edid);
153 struct exynos_drm_panel_info *panel;
154 struct drm_display_mode *mode = drm_mode_create(connector->dev);
156 DRM_ERROR("failed to create a new display mode.\n");
160 if (display_ops->get_panel)
161 panel = display_ops->get_panel(manager->dev);
163 drm_mode_destroy(connector->dev, mode);
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;
171 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
172 drm_mode_set_name(mode);
173 drm_mode_probed_add(connector, mode);
181 static int exynos_drm_connector_mode_valid(struct drm_connector *connector,
182 struct drm_display_mode *mode)
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;
191 DRM_DEBUG_KMS("%s\n", __FILE__);
193 convert_to_video_timing(&timing, mode);
195 if (display_ops && display_ops->check_timing)
196 if (!display_ops->check_timing(manager->dev, (void *)&timing))
202 struct drm_encoder *exynos_drm_best_encoder(struct drm_connector *connector)
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;
210 DRM_DEBUG_KMS("%s\n", __FILE__);
212 obj = drm_mode_object_find(dev, exynos_connector->encoder_id,
213 DRM_MODE_OBJECT_ENCODER);
215 DRM_DEBUG_KMS("Unknown ENCODER ID %d\n",
216 exynos_connector->encoder_id);
220 encoder = obj_to_encoder(obj);
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,
231 void exynos_drm_display_power(struct drm_connector *connector, int mode)
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;
238 exynos_connector = to_exynos_connector(connector);
240 if (exynos_connector->dpms == mode) {
241 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
245 if (display_ops && display_ops->power_on)
246 display_ops->power_on(manager->dev, mode);
248 exynos_connector->dpms = mode;
251 static void exynos_drm_connector_dpms(struct drm_connector *connector,
254 DRM_DEBUG_KMS("%s\n", __FILE__);
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.
262 drm_helper_connector_dpms(connector, mode);
264 exynos_drm_display_power(connector, mode);
268 static int exynos_drm_connector_fill_modes(struct drm_connector *connector,
269 unsigned int max_width, unsigned int max_height)
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;
281 * if specific driver want to find desired_mode using maxmum
282 * resolution then get max width and height from that driver.
284 if (ops && ops->get_max_resol)
285 ops->get_max_resol(manager->dev, &width, &height);
287 return drm_helper_probe_single_connector_modes(connector, width,
291 /* get detection status of display device. */
292 static enum drm_connector_status
293 exynos_drm_connector_detect(struct drm_connector *connector, bool force)
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;
302 DRM_DEBUG_KMS("%s\n", __FILE__);
304 if (display_ops && display_ops->is_connected) {
305 if (display_ops->is_connected(manager->dev))
306 status = connector_status_connected;
308 status = connector_status_disconnected;
314 static void exynos_drm_connector_destroy(struct drm_connector *connector)
316 struct exynos_drm_connector *exynos_connector =
317 to_exynos_connector(connector);
319 DRM_DEBUG_KMS("%s\n", __FILE__);
321 drm_sysfs_connector_remove(connector);
322 drm_connector_cleanup(connector);
323 kfree(exynos_connector);
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,
333 struct drm_connector *exynos_drm_connector_create(struct drm_device *dev,
334 struct drm_encoder *encoder)
336 struct exynos_drm_connector *exynos_connector;
337 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
338 struct drm_connector *connector;
342 DRM_DEBUG_KMS("%s\n", __FILE__);
344 exynos_connector = kzalloc(sizeof(*exynos_connector), GFP_KERNEL);
345 if (!exynos_connector) {
346 DRM_ERROR("failed to allocate connector\n");
350 connector = &exynos_connector->drm_connector;
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;
358 case EXYNOS_DISPLAY_TYPE_VIDI:
359 type = DRM_MODE_CONNECTOR_VIRTUAL;
360 connector->polled = DRM_CONNECTOR_POLL_HPD;
363 type = DRM_MODE_CONNECTOR_Unknown;
367 drm_connector_init(dev, connector, &exynos_connector_funcs, type);
368 drm_connector_helper_add(connector, &exynos_connector_helper_funcs);
370 err = drm_sysfs_connector_add(connector);
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;
380 err = drm_mode_connector_attach_encoder(connector, encoder);
382 DRM_ERROR("failed to attach a connector to a encoder\n");
386 DRM_DEBUG_KMS("connector has been created\n");
391 drm_sysfs_connector_remove(connector);
393 drm_connector_cleanup(connector);
394 kfree(exynos_connector);