Commit | Line | Data |
---|---|---|
cd5351f4 | 1 | /* |
8bb0daff | 2 | * drivers/gpu/drm/omapdrm/omap_connector.c |
cd5351f4 RC |
3 | * |
4 | * Copyright (C) 2011 Texas Instruments | |
5 | * Author: Rob Clark <rob@ti.com> | |
6 | * | |
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. | |
10 | * | |
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 | |
14 | * more details. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
69a12263 | 20 | #include <drm/drm_atomic_helper.h> |
2d278f54 LP |
21 | #include <drm/drm_crtc.h> |
22 | #include <drm/drm_crtc_helper.h> | |
cd5351f4 | 23 | |
2d278f54 | 24 | #include "omap_drv.h" |
cd5351f4 RC |
25 | |
26 | /* | |
27 | * connector funcs | |
28 | */ | |
29 | ||
30 | #define to_omap_connector(x) container_of(x, struct omap_connector, base) | |
31 | ||
32 | struct omap_connector { | |
33 | struct drm_connector base; | |
34 | struct omap_dss_device *dssdev; | |
f5f9454c | 35 | struct drm_encoder *encoder; |
4f930c0f | 36 | bool hdmi_mode; |
cd5351f4 RC |
37 | }; |
38 | ||
4f930c0f TV |
39 | bool omap_connector_get_hdmi_mode(struct drm_connector *connector) |
40 | { | |
41 | struct omap_connector *omap_connector = to_omap_connector(connector); | |
42 | ||
43 | return omap_connector->hdmi_mode; | |
44 | } | |
45 | ||
f5f9454c | 46 | void copy_timings_omap_to_drm(struct drm_display_mode *mode, |
cd5351f4 RC |
47 | struct omap_video_timings *timings) |
48 | { | |
d8d78941 | 49 | mode->clock = timings->pixelclock / 1000; |
cd5351f4 RC |
50 | |
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; | |
55 | ||
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; | |
60 | ||
0b0d7b62 RC |
61 | mode->flags = 0; |
62 | ||
63 | if (timings->interlace) | |
64 | mode->flags |= DRM_MODE_FLAG_INTERLACE; | |
94254edc RC |
65 | |
66 | if (timings->hsync_level == OMAPDSS_SIG_ACTIVE_HIGH) | |
67 | mode->flags |= DRM_MODE_FLAG_PHSYNC; | |
68 | else | |
69 | mode->flags |= DRM_MODE_FLAG_NHSYNC; | |
70 | ||
71 | if (timings->vsync_level == OMAPDSS_SIG_ACTIVE_HIGH) | |
72 | mode->flags |= DRM_MODE_FLAG_PVSYNC; | |
73 | else | |
74 | mode->flags |= DRM_MODE_FLAG_NVSYNC; | |
cd5351f4 RC |
75 | } |
76 | ||
f5f9454c | 77 | void copy_timings_drm_to_omap(struct omap_video_timings *timings, |
cd5351f4 RC |
78 | struct drm_display_mode *mode) |
79 | { | |
d8d78941 | 80 | timings->pixelclock = mode->clock * 1000; |
cd5351f4 RC |
81 | |
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; | |
86 | ||
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; | |
0b0d7b62 RC |
91 | |
92 | timings->interlace = !!(mode->flags & DRM_MODE_FLAG_INTERLACE); | |
94254edc RC |
93 | |
94 | if (mode->flags & DRM_MODE_FLAG_PHSYNC) | |
95 | timings->hsync_level = OMAPDSS_SIG_ACTIVE_HIGH; | |
96 | else | |
97 | timings->hsync_level = OMAPDSS_SIG_ACTIVE_LOW; | |
98 | ||
99 | if (mode->flags & DRM_MODE_FLAG_PVSYNC) | |
100 | timings->vsync_level = OMAPDSS_SIG_ACTIVE_HIGH; | |
101 | else | |
102 | timings->vsync_level = OMAPDSS_SIG_ACTIVE_LOW; | |
103 | ||
104 | timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE; | |
105 | timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH; | |
7a16360d | 106 | timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE; |
cd5351f4 RC |
107 | } |
108 | ||
f5f9454c | 109 | static enum drm_connector_status omap_connector_detect( |
cd5351f4 RC |
110 | struct drm_connector *connector, bool force) |
111 | { | |
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; | |
116 | ||
117 | if (dssdrv->detect) { | |
fdbf20b3 | 118 | if (dssdrv->detect(dssdev)) |
cd5351f4 | 119 | ret = connector_status_connected; |
fdbf20b3 | 120 | else |
cd5351f4 | 121 | ret = connector_status_disconnected; |
bddabbe1 AT |
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; | |
cd5351f4 RC |
127 | } else { |
128 | ret = connector_status_unknown; | |
129 | } | |
130 | ||
131 | VERB("%s: %d (force=%d)", omap_connector->dssdev->name, ret, force); | |
132 | ||
133 | return ret; | |
134 | } | |
135 | ||
136 | static void omap_connector_destroy(struct drm_connector *connector) | |
137 | { | |
138 | struct omap_connector *omap_connector = to_omap_connector(connector); | |
139 | struct omap_dss_device *dssdev = omap_connector->dssdev; | |
140 | ||
cd5351f4 | 141 | DBG("%s", omap_connector->dssdev->name); |
34ea3d38 | 142 | drm_connector_unregister(connector); |
cd5351f4 RC |
143 | drm_connector_cleanup(connector); |
144 | kfree(omap_connector); | |
145 | ||
146 | omap_dss_put_device(dssdev); | |
147 | } | |
148 | ||
149 | #define MAX_EDID 512 | |
150 | ||
151 | static int omap_connector_get_modes(struct drm_connector *connector) | |
152 | { | |
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; | |
157 | int n = 0; | |
158 | ||
159 | DBG("%s", omap_connector->dssdev->name); | |
160 | ||
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: | |
165 | */ | |
166 | if (dssdrv->read_edid) { | |
167 | void *edid = kzalloc(MAX_EDID, GFP_KERNEL); | |
168 | ||
169 | if ((dssdrv->read_edid(dssdev, edid, MAX_EDID) > 0) && | |
170 | drm_edid_is_valid(edid)) { | |
171 | drm_mode_connector_update_edid_property( | |
172 | connector, edid); | |
173 | n = drm_add_edid_modes(connector, edid); | |
4f930c0f TV |
174 | |
175 | omap_connector->hdmi_mode = | |
176 | drm_detect_hdmi_monitor(edid); | |
cd5351f4 RC |
177 | } else { |
178 | drm_mode_connector_update_edid_property( | |
179 | connector, NULL); | |
cd5351f4 | 180 | } |
4f930c0f | 181 | |
451023dc | 182 | kfree(edid); |
cd5351f4 RC |
183 | } else { |
184 | struct drm_display_mode *mode = drm_mode_create(dev); | |
0b0d7b62 | 185 | struct omap_video_timings timings = {0}; |
cd5351f4 RC |
186 | |
187 | dssdrv->get_timings(dssdev, &timings); | |
188 | ||
189 | copy_timings_omap_to_drm(mode, &timings); | |
190 | ||
191 | mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; | |
192 | drm_mode_set_name(mode); | |
193 | drm_mode_probed_add(connector, mode); | |
194 | ||
195 | n = 1; | |
196 | } | |
197 | ||
198 | return n; | |
199 | } | |
200 | ||
201 | static int omap_connector_mode_valid(struct drm_connector *connector, | |
202 | struct drm_display_mode *mode) | |
203 | { | |
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; | |
bddabbe1 | 210 | int r, ret = MODE_BAD; |
cd5351f4 RC |
211 | |
212 | copy_timings_drm_to_omap(&timings, mode); | |
213 | mode->vrefresh = drm_mode_vrefresh(mode); | |
214 | ||
bddabbe1 AT |
215 | /* |
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 | |
218 | * panel's timings | |
219 | */ | |
220 | if (dssdrv->check_timings) { | |
221 | r = dssdrv->check_timings(dssdev, &timings); | |
222 | } else { | |
223 | struct omap_video_timings t = {0}; | |
224 | ||
225 | dssdrv->get_timings(dssdev, &t); | |
226 | ||
227 | if (memcmp(&timings, &t, sizeof(struct omap_video_timings))) | |
228 | r = -EINVAL; | |
229 | else | |
230 | r = 0; | |
231 | } | |
232 | ||
233 | if (!r) { | |
cd5351f4 RC |
234 | /* check if vrefresh is still valid */ |
235 | new_mode = drm_mode_duplicate(dev, mode); | |
d8d78941 | 236 | new_mode->clock = timings.pixelclock / 1000; |
cd5351f4 RC |
237 | new_mode->vrefresh = 0; |
238 | if (mode->vrefresh == drm_mode_vrefresh(new_mode)) | |
239 | ret = MODE_OK; | |
240 | drm_mode_destroy(dev, new_mode); | |
241 | } | |
242 | ||
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); | |
251 | ||
252 | return ret; | |
253 | } | |
254 | ||
255 | struct drm_encoder *omap_connector_attached_encoder( | |
256 | struct drm_connector *connector) | |
257 | { | |
cd5351f4 | 258 | struct omap_connector *omap_connector = to_omap_connector(connector); |
f5f9454c | 259 | return omap_connector->encoder; |
cd5351f4 RC |
260 | } |
261 | ||
262 | static const struct drm_connector_funcs omap_connector_funcs = { | |
aea3cab1 | 263 | .dpms = drm_atomic_helper_connector_dpms, |
69a12263 | 264 | .reset = drm_atomic_helper_connector_reset, |
cd5351f4 RC |
265 | .detect = omap_connector_detect, |
266 | .fill_modes = drm_helper_probe_single_connector_modes, | |
267 | .destroy = omap_connector_destroy, | |
69a12263 LP |
268 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
269 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, | |
cd5351f4 RC |
270 | }; |
271 | ||
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, | |
276 | }; | |
277 | ||
cd5351f4 RC |
278 | /* initialize connector */ |
279 | struct drm_connector *omap_connector_init(struct drm_device *dev, | |
f5f9454c RC |
280 | int connector_type, struct omap_dss_device *dssdev, |
281 | struct drm_encoder *encoder) | |
cd5351f4 RC |
282 | { |
283 | struct drm_connector *connector = NULL; | |
284 | struct omap_connector *omap_connector; | |
285 | ||
286 | DBG("%s", dssdev->name); | |
287 | ||
288 | omap_dss_get_device(dssdev); | |
289 | ||
290 | omap_connector = kzalloc(sizeof(struct omap_connector), GFP_KERNEL); | |
78110bb8 | 291 | if (!omap_connector) |
cd5351f4 | 292 | goto fail; |
cd5351f4 RC |
293 | |
294 | omap_connector->dssdev = dssdev; | |
f5f9454c RC |
295 | omap_connector->encoder = encoder; |
296 | ||
cd5351f4 RC |
297 | connector = &omap_connector->base; |
298 | ||
299 | drm_connector_init(dev, connector, &omap_connector_funcs, | |
300 | connector_type); | |
301 | drm_connector_helper_add(connector, &omap_connector_helper_funcs); | |
302 | ||
303 | #if 0 /* enable when dss2 supports hotplug */ | |
304 | if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_HPD) | |
305 | connector->polled = 0; | |
306 | else | |
307 | #endif | |
308 | connector->polled = DRM_CONNECTOR_POLL_CONNECT | | |
309 | DRM_CONNECTOR_POLL_DISCONNECT; | |
310 | ||
311 | connector->interlace_allowed = 1; | |
312 | connector->doublescan_allowed = 0; | |
313 | ||
34ea3d38 | 314 | drm_connector_register(connector); |
cd5351f4 RC |
315 | |
316 | return connector; | |
317 | ||
318 | fail: | |
fdbf20b3 | 319 | if (connector) |
cd5351f4 | 320 | omap_connector_destroy(connector); |
cd5351f4 RC |
321 | |
322 | return NULL; | |
323 | } |