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/>.
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_crtc.h>
22 #include <drm/drm_crtc_helper.h>
30 #define to_omap_connector(x) container_of(x, struct omap_connector, base)
32 struct omap_connector
{
33 struct drm_connector base
;
34 struct omap_dss_device
*dssdev
;
38 bool omap_connector_get_hdmi_mode(struct drm_connector
*connector
)
40 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
42 return omap_connector
->hdmi_mode
;
45 void copy_timings_omap_to_drm(struct drm_display_mode
*mode
,
46 struct omap_video_timings
*timings
)
48 mode
->clock
= timings
->pixelclock
/ 1000;
50 mode
->hdisplay
= timings
->hactive
;
51 mode
->hsync_start
= mode
->hdisplay
+ timings
->hfront_porch
;
52 mode
->hsync_end
= mode
->hsync_start
+ timings
->hsync_len
;
53 mode
->htotal
= mode
->hsync_end
+ timings
->hback_porch
;
55 mode
->vdisplay
= timings
->vactive
;
56 mode
->vsync_start
= mode
->vdisplay
+ timings
->vfront_porch
;
57 mode
->vsync_end
= mode
->vsync_start
+ timings
->vsync_len
;
58 mode
->vtotal
= mode
->vsync_end
+ timings
->vback_porch
;
62 if (timings
->flags
& DISPLAY_FLAGS_INTERLACED
)
63 mode
->flags
|= DRM_MODE_FLAG_INTERLACE
;
65 if (timings
->flags
& DISPLAY_FLAGS_DOUBLECLK
)
66 mode
->flags
|= DRM_MODE_FLAG_DBLCLK
;
68 if (timings
->flags
& DISPLAY_FLAGS_HSYNC_HIGH
)
69 mode
->flags
|= DRM_MODE_FLAG_PHSYNC
;
71 mode
->flags
|= DRM_MODE_FLAG_NHSYNC
;
73 if (timings
->flags
& DISPLAY_FLAGS_VSYNC_HIGH
)
74 mode
->flags
|= DRM_MODE_FLAG_PVSYNC
;
76 mode
->flags
|= DRM_MODE_FLAG_NVSYNC
;
79 void copy_timings_drm_to_omap(struct omap_video_timings
*timings
,
80 struct drm_display_mode
*mode
)
82 timings
->pixelclock
= mode
->clock
* 1000;
84 timings
->hactive
= mode
->hdisplay
;
85 timings
->hfront_porch
= mode
->hsync_start
- mode
->hdisplay
;
86 timings
->hsync_len
= mode
->hsync_end
- mode
->hsync_start
;
87 timings
->hback_porch
= mode
->htotal
- mode
->hsync_end
;
89 timings
->vactive
= mode
->vdisplay
;
90 timings
->vfront_porch
= mode
->vsync_start
- mode
->vdisplay
;
91 timings
->vsync_len
= mode
->vsync_end
- mode
->vsync_start
;
92 timings
->vback_porch
= mode
->vtotal
- mode
->vsync_end
;
94 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
95 timings
->flags
|= DISPLAY_FLAGS_INTERLACED
;
97 if (mode
->flags
& DRM_MODE_FLAG_DBLCLK
)
98 timings
->flags
|= DISPLAY_FLAGS_DOUBLECLK
;
100 if (mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
101 timings
->flags
|= DISPLAY_FLAGS_HSYNC_HIGH
;
103 timings
->flags
|= DISPLAY_FLAGS_HSYNC_LOW
;
105 if (mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
106 timings
->flags
|= DISPLAY_FLAGS_VSYNC_HIGH
;
108 timings
->flags
|= DISPLAY_FLAGS_VSYNC_LOW
;
110 timings
->flags
|= DISPLAY_FLAGS_DE_HIGH
| DISPLAY_FLAGS_PIXDATA_POSEDGE
;
111 timings
->sync_pclk_edge
= OMAPDSS_DRIVE_SIG_FALLING_EDGE
;
114 static enum drm_connector_status
omap_connector_detect(
115 struct drm_connector
*connector
, bool force
)
117 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
118 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
119 struct omap_dss_driver
*dssdrv
= dssdev
->driver
;
120 enum drm_connector_status ret
;
122 if (dssdrv
->detect
) {
123 if (dssdrv
->detect(dssdev
))
124 ret
= connector_status_connected
;
126 ret
= connector_status_disconnected
;
127 } else if (dssdev
->type
== OMAP_DISPLAY_TYPE_DPI
||
128 dssdev
->type
== OMAP_DISPLAY_TYPE_DBI
||
129 dssdev
->type
== OMAP_DISPLAY_TYPE_SDI
||
130 dssdev
->type
== OMAP_DISPLAY_TYPE_DSI
) {
131 ret
= connector_status_connected
;
133 ret
= connector_status_unknown
;
136 VERB("%s: %d (force=%d)", omap_connector
->dssdev
->name
, ret
, force
);
141 static void omap_connector_destroy(struct drm_connector
*connector
)
143 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
144 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
146 DBG("%s", omap_connector
->dssdev
->name
);
147 drm_connector_unregister(connector
);
148 drm_connector_cleanup(connector
);
149 kfree(omap_connector
);
151 omap_dss_put_device(dssdev
);
156 static int omap_connector_get_modes(struct drm_connector
*connector
)
158 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
159 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
160 struct omap_dss_driver
*dssdrv
= dssdev
->driver
;
161 struct drm_device
*dev
= connector
->dev
;
164 DBG("%s", omap_connector
->dssdev
->name
);
166 /* if display exposes EDID, then we parse that in the normal way to
167 * build table of supported modes.. otherwise (ie. fixed resolution
168 * LCD panels) we just return a single mode corresponding to the
169 * currently configured timings:
171 if (dssdrv
->read_edid
) {
172 void *edid
= kzalloc(MAX_EDID
, GFP_KERNEL
);
174 if ((dssdrv
->read_edid(dssdev
, edid
, MAX_EDID
) > 0) &&
175 drm_edid_is_valid(edid
)) {
176 drm_mode_connector_update_edid_property(
178 n
= drm_add_edid_modes(connector
, edid
);
180 omap_connector
->hdmi_mode
=
181 drm_detect_hdmi_monitor(edid
);
183 drm_mode_connector_update_edid_property(
189 struct drm_display_mode
*mode
= drm_mode_create(dev
);
190 struct omap_video_timings timings
= {0};
192 dssdrv
->get_timings(dssdev
, &timings
);
194 copy_timings_omap_to_drm(mode
, &timings
);
196 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
197 drm_mode_set_name(mode
);
198 drm_mode_probed_add(connector
, mode
);
206 static int omap_connector_mode_valid(struct drm_connector
*connector
,
207 struct drm_display_mode
*mode
)
209 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
210 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
211 struct omap_dss_driver
*dssdrv
= dssdev
->driver
;
212 struct omap_video_timings timings
= {0};
213 struct drm_device
*dev
= connector
->dev
;
214 struct drm_display_mode
*new_mode
;
215 int r
, ret
= MODE_BAD
;
217 copy_timings_drm_to_omap(&timings
, mode
);
218 mode
->vrefresh
= drm_mode_vrefresh(mode
);
221 * if the panel driver doesn't have a check_timings, it's most likely
222 * a fixed resolution panel, check if the timings match with the
225 if (dssdrv
->check_timings
) {
226 r
= dssdrv
->check_timings(dssdev
, &timings
);
228 struct omap_video_timings t
= {0};
230 dssdrv
->get_timings(dssdev
, &t
);
232 if (memcmp(&timings
, &t
, sizeof(struct omap_video_timings
)))
239 /* check if vrefresh is still valid */
240 new_mode
= drm_mode_duplicate(dev
, mode
);
241 new_mode
->clock
= timings
.pixelclock
/ 1000;
242 new_mode
->vrefresh
= 0;
243 if (mode
->vrefresh
== drm_mode_vrefresh(new_mode
))
245 drm_mode_destroy(dev
, new_mode
);
248 DBG("connector: mode %s: "
249 "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
250 (ret
== MODE_OK
) ? "valid" : "invalid",
251 mode
->base
.id
, mode
->name
, mode
->vrefresh
, mode
->clock
,
252 mode
->hdisplay
, mode
->hsync_start
,
253 mode
->hsync_end
, mode
->htotal
,
254 mode
->vdisplay
, mode
->vsync_start
,
255 mode
->vsync_end
, mode
->vtotal
, mode
->type
, mode
->flags
);
260 static const struct drm_connector_funcs omap_connector_funcs
= {
261 .dpms
= drm_atomic_helper_connector_dpms
,
262 .reset
= drm_atomic_helper_connector_reset
,
263 .detect
= omap_connector_detect
,
264 .fill_modes
= drm_helper_probe_single_connector_modes
,
265 .destroy
= omap_connector_destroy
,
266 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
267 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
270 static const struct drm_connector_helper_funcs omap_connector_helper_funcs
= {
271 .get_modes
= omap_connector_get_modes
,
272 .mode_valid
= omap_connector_mode_valid
,
275 /* initialize connector */
276 struct drm_connector
*omap_connector_init(struct drm_device
*dev
,
277 int connector_type
, struct omap_dss_device
*dssdev
,
278 struct drm_encoder
*encoder
)
280 struct drm_connector
*connector
= NULL
;
281 struct omap_connector
*omap_connector
;
283 DBG("%s", dssdev
->name
);
285 omap_dss_get_device(dssdev
);
287 omap_connector
= kzalloc(sizeof(struct omap_connector
), GFP_KERNEL
);
291 omap_connector
->dssdev
= dssdev
;
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_connector_register(connector
);
316 omap_connector_destroy(connector
);