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
;
35 struct drm_encoder
*encoder
;
39 bool omap_connector_get_hdmi_mode(struct drm_connector
*connector
)
41 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
43 return omap_connector
->hdmi_mode
;
46 void copy_timings_omap_to_drm(struct drm_display_mode
*mode
,
47 struct omap_video_timings
*timings
)
49 mode
->clock
= timings
->pixelclock
/ 1000;
51 mode
->hdisplay
= timings
->x_res
;
52 mode
->hsync_start
= mode
->hdisplay
+ timings
->hfp
;
53 mode
->hsync_end
= mode
->hsync_start
+ timings
->hsw
;
54 mode
->htotal
= mode
->hsync_end
+ timings
->hbp
;
56 mode
->vdisplay
= timings
->y_res
;
57 mode
->vsync_start
= mode
->vdisplay
+ timings
->vfp
;
58 mode
->vsync_end
= mode
->vsync_start
+ timings
->vsw
;
59 mode
->vtotal
= mode
->vsync_end
+ timings
->vbp
;
63 if (timings
->interlace
)
64 mode
->flags
|= DRM_MODE_FLAG_INTERLACE
;
66 if (timings
->hsync_level
== OMAPDSS_SIG_ACTIVE_HIGH
)
67 mode
->flags
|= DRM_MODE_FLAG_PHSYNC
;
69 mode
->flags
|= DRM_MODE_FLAG_NHSYNC
;
71 if (timings
->vsync_level
== OMAPDSS_SIG_ACTIVE_HIGH
)
72 mode
->flags
|= DRM_MODE_FLAG_PVSYNC
;
74 mode
->flags
|= DRM_MODE_FLAG_NVSYNC
;
77 void copy_timings_drm_to_omap(struct omap_video_timings
*timings
,
78 struct drm_display_mode
*mode
)
80 timings
->pixelclock
= mode
->clock
* 1000;
82 timings
->x_res
= mode
->hdisplay
;
83 timings
->hfp
= mode
->hsync_start
- mode
->hdisplay
;
84 timings
->hsw
= mode
->hsync_end
- mode
->hsync_start
;
85 timings
->hbp
= mode
->htotal
- mode
->hsync_end
;
87 timings
->y_res
= mode
->vdisplay
;
88 timings
->vfp
= mode
->vsync_start
- mode
->vdisplay
;
89 timings
->vsw
= mode
->vsync_end
- mode
->vsync_start
;
90 timings
->vbp
= mode
->vtotal
- mode
->vsync_end
;
92 timings
->interlace
= !!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
);
94 if (mode
->flags
& DRM_MODE_FLAG_PHSYNC
)
95 timings
->hsync_level
= OMAPDSS_SIG_ACTIVE_HIGH
;
97 timings
->hsync_level
= OMAPDSS_SIG_ACTIVE_LOW
;
99 if (mode
->flags
& DRM_MODE_FLAG_PVSYNC
)
100 timings
->vsync_level
= OMAPDSS_SIG_ACTIVE_HIGH
;
102 timings
->vsync_level
= OMAPDSS_SIG_ACTIVE_LOW
;
104 timings
->data_pclk_edge
= OMAPDSS_DRIVE_SIG_RISING_EDGE
;
105 timings
->de_level
= OMAPDSS_SIG_ACTIVE_HIGH
;
106 timings
->sync_pclk_edge
= OMAPDSS_DRIVE_SIG_FALLING_EDGE
;
109 static enum drm_connector_status
omap_connector_detect(
110 struct drm_connector
*connector
, bool force
)
112 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
113 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
114 struct omap_dss_driver
*dssdrv
= dssdev
->driver
;
115 enum drm_connector_status ret
;
117 if (dssdrv
->detect
) {
118 if (dssdrv
->detect(dssdev
))
119 ret
= connector_status_connected
;
121 ret
= connector_status_disconnected
;
122 } else if (dssdev
->type
== OMAP_DISPLAY_TYPE_DPI
||
123 dssdev
->type
== OMAP_DISPLAY_TYPE_DBI
||
124 dssdev
->type
== OMAP_DISPLAY_TYPE_SDI
||
125 dssdev
->type
== OMAP_DISPLAY_TYPE_DSI
) {
126 ret
= connector_status_connected
;
128 ret
= connector_status_unknown
;
131 VERB("%s: %d (force=%d)", omap_connector
->dssdev
->name
, ret
, force
);
136 static void omap_connector_destroy(struct drm_connector
*connector
)
138 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
139 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
141 DBG("%s", omap_connector
->dssdev
->name
);
142 drm_connector_unregister(connector
);
143 drm_connector_cleanup(connector
);
144 kfree(omap_connector
);
146 omap_dss_put_device(dssdev
);
151 static int omap_connector_get_modes(struct drm_connector
*connector
)
153 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
154 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
155 struct omap_dss_driver
*dssdrv
= dssdev
->driver
;
156 struct drm_device
*dev
= connector
->dev
;
159 DBG("%s", omap_connector
->dssdev
->name
);
161 /* if display exposes EDID, then we parse that in the normal way to
162 * build table of supported modes.. otherwise (ie. fixed resolution
163 * LCD panels) we just return a single mode corresponding to the
164 * currently configured timings:
166 if (dssdrv
->read_edid
) {
167 void *edid
= kzalloc(MAX_EDID
, GFP_KERNEL
);
169 if ((dssdrv
->read_edid(dssdev
, edid
, MAX_EDID
) > 0) &&
170 drm_edid_is_valid(edid
)) {
171 drm_mode_connector_update_edid_property(
173 n
= drm_add_edid_modes(connector
, edid
);
175 omap_connector
->hdmi_mode
=
176 drm_detect_hdmi_monitor(edid
);
178 drm_mode_connector_update_edid_property(
184 struct drm_display_mode
*mode
= drm_mode_create(dev
);
185 struct omap_video_timings timings
= {0};
187 dssdrv
->get_timings(dssdev
, &timings
);
189 copy_timings_omap_to_drm(mode
, &timings
);
191 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
192 drm_mode_set_name(mode
);
193 drm_mode_probed_add(connector
, mode
);
201 static int omap_connector_mode_valid(struct drm_connector
*connector
,
202 struct drm_display_mode
*mode
)
204 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
205 struct omap_dss_device
*dssdev
= omap_connector
->dssdev
;
206 struct omap_dss_driver
*dssdrv
= dssdev
->driver
;
207 struct omap_video_timings timings
= {0};
208 struct drm_device
*dev
= connector
->dev
;
209 struct drm_display_mode
*new_mode
;
210 int r
, ret
= MODE_BAD
;
212 copy_timings_drm_to_omap(&timings
, mode
);
213 mode
->vrefresh
= drm_mode_vrefresh(mode
);
216 * if the panel driver doesn't have a check_timings, it's most likely
217 * a fixed resolution panel, check if the timings match with the
220 if (dssdrv
->check_timings
) {
221 r
= dssdrv
->check_timings(dssdev
, &timings
);
223 struct omap_video_timings t
= {0};
225 dssdrv
->get_timings(dssdev
, &t
);
227 if (memcmp(&timings
, &t
, sizeof(struct omap_video_timings
)))
234 /* check if vrefresh is still valid */
235 new_mode
= drm_mode_duplicate(dev
, mode
);
236 new_mode
->clock
= timings
.pixelclock
/ 1000;
237 new_mode
->vrefresh
= 0;
238 if (mode
->vrefresh
== drm_mode_vrefresh(new_mode
))
240 drm_mode_destroy(dev
, new_mode
);
243 DBG("connector: mode %s: "
244 "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
245 (ret
== MODE_OK
) ? "valid" : "invalid",
246 mode
->base
.id
, mode
->name
, mode
->vrefresh
, mode
->clock
,
247 mode
->hdisplay
, mode
->hsync_start
,
248 mode
->hsync_end
, mode
->htotal
,
249 mode
->vdisplay
, mode
->vsync_start
,
250 mode
->vsync_end
, mode
->vtotal
, mode
->type
, mode
->flags
);
255 struct drm_encoder
*omap_connector_attached_encoder(
256 struct drm_connector
*connector
)
258 struct omap_connector
*omap_connector
= to_omap_connector(connector
);
259 return omap_connector
->encoder
;
262 static const struct drm_connector_funcs omap_connector_funcs
= {
263 .dpms
= drm_atomic_helper_connector_dpms
,
264 .reset
= drm_atomic_helper_connector_reset
,
265 .detect
= omap_connector_detect
,
266 .fill_modes
= drm_helper_probe_single_connector_modes
,
267 .destroy
= omap_connector_destroy
,
268 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
269 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
272 static const struct drm_connector_helper_funcs omap_connector_helper_funcs
= {
273 .get_modes
= omap_connector_get_modes
,
274 .mode_valid
= omap_connector_mode_valid
,
275 .best_encoder
= omap_connector_attached_encoder
,
278 /* initialize connector */
279 struct drm_connector
*omap_connector_init(struct drm_device
*dev
,
280 int connector_type
, struct omap_dss_device
*dssdev
,
281 struct drm_encoder
*encoder
)
283 struct drm_connector
*connector
= NULL
;
284 struct omap_connector
*omap_connector
;
286 DBG("%s", dssdev
->name
);
288 omap_dss_get_device(dssdev
);
290 omap_connector
= kzalloc(sizeof(struct omap_connector
), GFP_KERNEL
);
294 omap_connector
->dssdev
= dssdev
;
295 omap_connector
->encoder
= encoder
;
297 connector
= &omap_connector
->base
;
299 drm_connector_init(dev
, connector
, &omap_connector_funcs
,
301 drm_connector_helper_add(connector
, &omap_connector_helper_funcs
);
303 #if 0 /* enable when dss2 supports hotplug */
304 if (dssdev
->caps
& OMAP_DSS_DISPLAY_CAP_HPD
)
305 connector
->polled
= 0;
308 connector
->polled
= DRM_CONNECTOR_POLL_CONNECT
|
309 DRM_CONNECTOR_POLL_DISCONNECT
;
311 connector
->interlace_allowed
= 1;
312 connector
->doublescan_allowed
= 0;
314 drm_connector_register(connector
);
320 omap_connector_destroy(connector
);