2 * drivers/gpu/drm/omapdrm/omap_connector.c
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob@ti.com>
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.
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
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/>.
23 #include "drm_crtc_helper.h"
29 #define to_omap_connector(x) container_of(x, struct omap_connector, base)
31 struct omap_connector
{
32 struct drm_connector base
;
33 struct omap_dss_device
*dssdev
;
34 struct drm_encoder
*encoder
;
37 void copy_timings_omap_to_drm(struct drm_display_mode
*mode
,
38 struct omap_video_timings
*timings
)
40 mode
->clock
= timings
->pixel_clock
;
42 mode
->hdisplay
= timings
->x_res
;
43 mode
->hsync_start
= mode
->hdisplay
+ timings
->hfp
;
44 mode
->hsync_end
= mode
->hsync_start
+ timings
->hsw
;
45 mode
->htotal
= mode
->hsync_end
+ timings
->hbp
;
47 mode
->vdisplay
= timings
->y_res
;
48 mode
->vsync_start
= mode
->vdisplay
+ timings
->vfp
;
49 mode
->vsync_end
= mode
->vsync_start
+ timings
->vsw
;
50 mode
->vtotal
= mode
->vsync_end
+ timings
->vbp
;
54 if (timings
->interlace
)
55 mode
->flags
|= DRM_MODE_FLAG_INTERLACE
;
57 if (timings
->hsync_level
== OMAPDSS_SIG_ACTIVE_HIGH
)
58 mode
->flags
|= DRM_MODE_FLAG_PHSYNC
;
60 mode
->flags
|= DRM_MODE_FLAG_NHSYNC
;
62 if (timings
->vsync_level
== OMAPDSS_SIG_ACTIVE_HIGH
)
63 mode
->flags
|= DRM_MODE_FLAG_PVSYNC
;
65 mode
->flags
|= DRM_MODE_FLAG_NVSYNC
;
68 void copy_timings_drm_to_omap(struct omap_video_timings
*timings
,
69 struct drm_display_mode
*mode
)
71 timings
->pixel_clock
= mode
->clock
;
73 timings
->x_res
= mode
->hdisplay
;
74 timings
->hfp
= mode
->hsync_start
- mode
->hdisplay
;
75 timings
->hsw
= mode
->hsync_end
- mode
->hsync_start
;
76 timings
->hbp
= mode
->htotal
- mode
->hsync_end
;
78 timings
->y_res
= mode
->vdisplay
;
79 timings
->vfp
= mode
->vsync_start
- mode
->vdisplay
;
80 timings
->vsw
= mode
->vsync_end
- mode
->vsync_start
;
81 timings
->vbp
= mode
->vtotal
- mode
->vsync_end
;
83 timings
->interlace
= !!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
);
85 if (mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
86 timings
->hsync_level
= OMAPDSS_SIG_ACTIVE_HIGH
;
88 timings
->hsync_level
= OMAPDSS_SIG_ACTIVE_LOW
;
90 if (mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
91 timings
->vsync_level
= OMAPDSS_SIG_ACTIVE_HIGH
;
93 timings
->vsync_level
= OMAPDSS_SIG_ACTIVE_LOW
;
95 timings
->data_pclk_edge
= OMAPDSS_DRIVE_SIG_RISING_EDGE
;
96 timings
->de_level
= OMAPDSS_SIG_ACTIVE_HIGH
;
97 timings
->sync_pclk_edge
= OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES
;
100 static enum drm_connector_status
omap_connector_detect(
101 struct drm_connector
*connector
, bool force
)
103 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
104 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
105 struct omap_dss_driver
*dssdrv
= dssdev
->driver
;
106 enum drm_connector_status ret
;
108 if (dssdrv
->detect
) {
109 if (dssdrv
->detect(dssdev
))
110 ret
= connector_status_connected
;
112 ret
= connector_status_disconnected
;
113 } else if (dssdev
->type
== OMAP_DISPLAY_TYPE_DPI
||
114 dssdev
->type
== OMAP_DISPLAY_TYPE_DBI
||
115 dssdev
->type
== OMAP_DISPLAY_TYPE_SDI
||
116 dssdev
->type
== OMAP_DISPLAY_TYPE_DSI
) {
117 ret
= connector_status_connected
;
119 ret
= connector_status_unknown
;
122 VERB("%s: %d (force=%d)", omap_connector
->dssdev
->name
, ret
, force
);
127 static void omap_connector_destroy(struct drm_connector
*connector
)
129 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
130 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
132 DBG("%s", omap_connector
->dssdev
->name
);
133 drm_sysfs_connector_remove(connector
);
134 drm_connector_cleanup(connector
);
135 kfree(omap_connector
);
137 omap_dss_put_device(dssdev
);
142 static int omap_connector_get_modes(struct drm_connector
*connector
)
144 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
145 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
146 struct omap_dss_driver
*dssdrv
= dssdev
->driver
;
147 struct drm_device
*dev
= connector
->dev
;
150 DBG("%s", omap_connector
->dssdev
->name
);
152 /* if display exposes EDID, then we parse that in the normal way to
153 * build table of supported modes.. otherwise (ie. fixed resolution
154 * LCD panels) we just return a single mode corresponding to the
155 * currently configured timings:
157 if (dssdrv
->read_edid
) {
158 void *edid
= kzalloc(MAX_EDID
, GFP_KERNEL
);
160 if ((dssdrv
->read_edid(dssdev
, edid
, MAX_EDID
) > 0) &&
161 drm_edid_is_valid(edid
)) {
162 drm_mode_connector_update_edid_property(
164 n
= drm_add_edid_modes(connector
, edid
);
166 drm_mode_connector_update_edid_property(
171 struct drm_display_mode
*mode
= drm_mode_create(dev
);
172 struct omap_video_timings timings
= {0};
174 dssdrv
->get_timings(dssdev
, &timings
);
176 copy_timings_omap_to_drm(mode
, &timings
);
178 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
179 drm_mode_set_name(mode
);
180 drm_mode_probed_add(connector
, mode
);
188 static int omap_connector_mode_valid(struct drm_connector
*connector
,
189 struct drm_display_mode
*mode
)
191 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
192 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
193 struct omap_dss_driver
*dssdrv
= dssdev
->driver
;
194 struct omap_video_timings timings
= {0};
195 struct drm_device
*dev
= connector
->dev
;
196 struct drm_display_mode
*new_mode
;
197 int r
, ret
= MODE_BAD
;
199 copy_timings_drm_to_omap(&timings
, mode
);
200 mode
->vrefresh
= drm_mode_vrefresh(mode
);
203 * if the panel driver doesn't have a check_timings, it's most likely
204 * a fixed resolution panel, check if the timings match with the
207 if (dssdrv
->check_timings
) {
208 r
= dssdrv
->check_timings(dssdev
, &timings
);
210 struct omap_video_timings t
= {0};
212 dssdrv
->get_timings(dssdev
, &t
);
214 if (memcmp(&timings
, &t
, sizeof(struct omap_video_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
))
227 drm_mode_destroy(dev
, new_mode
);
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
);
242 struct drm_encoder
*omap_connector_attached_encoder(
243 struct drm_connector
*connector
)
245 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
246 return omap_connector
->encoder
;
249 static const struct drm_connector_funcs omap_connector_funcs
= {
250 .dpms
= drm_helper_connector_dpms
,
251 .detect
= omap_connector_detect
,
252 .fill_modes
= drm_helper_probe_single_connector_modes
,
253 .destroy
= omap_connector_destroy
,
256 static const struct drm_connector_helper_funcs omap_connector_helper_funcs
= {
257 .get_modes
= omap_connector_get_modes
,
258 .mode_valid
= omap_connector_mode_valid
,
259 .best_encoder
= omap_connector_attached_encoder
,
262 /* flush an area of the framebuffer (in case of manual update display that
263 * is not automatically flushed)
265 void omap_connector_flush(struct drm_connector
*connector
,
266 int x
, int y
, int w
, int h
)
268 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
270 /* TODO: enable when supported in dss */
271 VERB("%s: %d,%d, %dx%d", omap_connector
->dssdev
->name
, x
, y
, w
, h
);
274 /* initialize connector */
275 struct drm_connector
*omap_connector_init(struct drm_device
*dev
,
276 int connector_type
, struct omap_dss_device
*dssdev
,
277 struct drm_encoder
*encoder
)
279 struct drm_connector
*connector
= NULL
;
280 struct omap_connector
*omap_connector
;
282 DBG("%s", dssdev
->name
);
284 omap_dss_get_device(dssdev
);
286 omap_connector
= kzalloc(sizeof(struct omap_connector
), GFP_KERNEL
);
290 omap_connector
->dssdev
= dssdev
;
291 omap_connector
->encoder
= encoder
;
293 connector
= &omap_connector
->base
;
295 drm_connector_init(dev
, connector
, &omap_connector_funcs
,
297 drm_connector_helper_add(connector
, &omap_connector_helper_funcs
);
299 #if 0 /* enable when dss2 supports hotplug */
300 if (dssdev
->caps
& OMAP_DSS_DISPLAY_CAP_HPD
)
301 connector
->polled
= 0;
304 connector
->polled
= DRM_CONNECTOR_POLL_CONNECT
|
305 DRM_CONNECTOR_POLL_DISCONNECT
;
307 connector
->interlace_allowed
= 1;
308 connector
->doublescan_allowed
= 0;
310 drm_sysfs_connector_add(connector
);
316 omap_connector_destroy(connector
);