Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1 | /* |
| 2 | * drivers/staging/omapdrm/omap_connector.c |
| 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 | |
| 20 | #include "omap_drv.h" |
| 21 | |
| 22 | #include "drm_crtc.h" |
| 23 | #include "drm_crtc_helper.h" |
| 24 | |
| 25 | /* |
| 26 | * connector funcs |
| 27 | */ |
| 28 | |
| 29 | #define to_omap_connector(x) container_of(x, struct omap_connector, base) |
| 30 | |
| 31 | struct omap_connector { |
| 32 | struct drm_connector base; |
| 33 | struct omap_dss_device *dssdev; |
| 34 | }; |
| 35 | |
| 36 | static inline void copy_timings_omap_to_drm(struct drm_display_mode *mode, |
| 37 | struct omap_video_timings *timings) |
| 38 | { |
| 39 | mode->clock = timings->pixel_clock; |
| 40 | |
| 41 | mode->hdisplay = timings->x_res; |
| 42 | mode->hsync_start = mode->hdisplay + timings->hfp; |
| 43 | mode->hsync_end = mode->hsync_start + timings->hsw; |
| 44 | mode->htotal = mode->hsync_end + timings->hbp; |
| 45 | |
| 46 | mode->vdisplay = timings->y_res; |
| 47 | mode->vsync_start = mode->vdisplay + timings->vfp; |
| 48 | mode->vsync_end = mode->vsync_start + timings->vsw; |
| 49 | mode->vtotal = mode->vsync_end + timings->vbp; |
| 50 | |
Rob Clark | 0b0d7b6 | 2012-09-05 16:48:53 -0500 | [diff] [blame] | 51 | mode->flags = 0; |
| 52 | |
| 53 | if (timings->interlace) |
| 54 | mode->flags |= DRM_MODE_FLAG_INTERLACE; |
Rob Clark | 94254ed | 2012-09-07 12:59:45 -0500 | [diff] [blame] | 55 | |
| 56 | if (timings->hsync_level == OMAPDSS_SIG_ACTIVE_HIGH) |
| 57 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 58 | else |
| 59 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 60 | |
| 61 | if (timings->vsync_level == OMAPDSS_SIG_ACTIVE_HIGH) |
| 62 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
| 63 | else |
| 64 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static inline void copy_timings_drm_to_omap(struct omap_video_timings *timings, |
| 68 | struct drm_display_mode *mode) |
| 69 | { |
| 70 | timings->pixel_clock = mode->clock; |
| 71 | |
| 72 | timings->x_res = mode->hdisplay; |
| 73 | timings->hfp = mode->hsync_start - mode->hdisplay; |
| 74 | timings->hsw = mode->hsync_end - mode->hsync_start; |
| 75 | timings->hbp = mode->htotal - mode->hsync_end; |
| 76 | |
| 77 | timings->y_res = mode->vdisplay; |
| 78 | timings->vfp = mode->vsync_start - mode->vdisplay; |
| 79 | timings->vsw = mode->vsync_end - mode->vsync_start; |
| 80 | timings->vbp = mode->vtotal - mode->vsync_end; |
Rob Clark | 0b0d7b6 | 2012-09-05 16:48:53 -0500 | [diff] [blame] | 81 | |
| 82 | timings->interlace = !!(mode->flags & DRM_MODE_FLAG_INTERLACE); |
Rob Clark | 94254ed | 2012-09-07 12:59:45 -0500 | [diff] [blame] | 83 | |
| 84 | if (mode->flags & DRM_MODE_FLAG_PHSYNC) |
| 85 | timings->hsync_level = OMAPDSS_SIG_ACTIVE_HIGH; |
| 86 | else |
| 87 | timings->hsync_level = OMAPDSS_SIG_ACTIVE_LOW; |
| 88 | |
| 89 | if (mode->flags & DRM_MODE_FLAG_PVSYNC) |
| 90 | timings->vsync_level = OMAPDSS_SIG_ACTIVE_HIGH; |
| 91 | else |
| 92 | timings->vsync_level = OMAPDSS_SIG_ACTIVE_LOW; |
| 93 | |
| 94 | timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE; |
| 95 | timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH; |
| 96 | timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void omap_connector_dpms(struct drm_connector *connector, int mode) |
| 100 | { |
| 101 | struct omap_connector *omap_connector = to_omap_connector(connector); |
| 102 | struct omap_dss_device *dssdev = omap_connector->dssdev; |
| 103 | int old_dpms; |
| 104 | |
| 105 | DBG("%s: %d", dssdev->name, mode); |
| 106 | |
| 107 | old_dpms = connector->dpms; |
| 108 | |
| 109 | /* from off to on, do from crtc to connector */ |
| 110 | if (mode < old_dpms) |
| 111 | drm_helper_connector_dpms(connector, mode); |
| 112 | |
| 113 | if (mode == DRM_MODE_DPMS_ON) { |
| 114 | /* store resume info for suspended displays */ |
| 115 | switch (dssdev->state) { |
| 116 | case OMAP_DSS_DISPLAY_SUSPENDED: |
| 117 | dssdev->activate_after_resume = true; |
| 118 | break; |
| 119 | case OMAP_DSS_DISPLAY_DISABLED: { |
| 120 | int ret = dssdev->driver->enable(dssdev); |
| 121 | if (ret) { |
| 122 | DBG("%s: failed to enable: %d", |
| 123 | dssdev->name, ret); |
| 124 | dssdev->driver->disable(dssdev); |
| 125 | } |
| 126 | break; |
| 127 | } |
| 128 | default: |
| 129 | break; |
| 130 | } |
| 131 | } else { |
| 132 | /* TODO */ |
| 133 | } |
| 134 | |
| 135 | /* from on to off, do from connector to crtc */ |
| 136 | if (mode > old_dpms) |
| 137 | drm_helper_connector_dpms(connector, mode); |
| 138 | } |
| 139 | |
| 140 | enum drm_connector_status omap_connector_detect( |
| 141 | struct drm_connector *connector, bool force) |
| 142 | { |
| 143 | struct omap_connector *omap_connector = to_omap_connector(connector); |
| 144 | struct omap_dss_device *dssdev = omap_connector->dssdev; |
| 145 | struct omap_dss_driver *dssdrv = dssdev->driver; |
| 146 | enum drm_connector_status ret; |
| 147 | |
| 148 | if (dssdrv->detect) { |
| 149 | if (dssdrv->detect(dssdev)) { |
| 150 | ret = connector_status_connected; |
| 151 | } else { |
| 152 | ret = connector_status_disconnected; |
| 153 | } |
| 154 | } else { |
| 155 | ret = connector_status_unknown; |
| 156 | } |
| 157 | |
| 158 | VERB("%s: %d (force=%d)", omap_connector->dssdev->name, ret, force); |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static void omap_connector_destroy(struct drm_connector *connector) |
| 164 | { |
| 165 | struct omap_connector *omap_connector = to_omap_connector(connector); |
| 166 | struct omap_dss_device *dssdev = omap_connector->dssdev; |
| 167 | |
| 168 | dssdev->driver->disable(dssdev); |
| 169 | |
| 170 | DBG("%s", omap_connector->dssdev->name); |
| 171 | drm_sysfs_connector_remove(connector); |
| 172 | drm_connector_cleanup(connector); |
| 173 | kfree(omap_connector); |
| 174 | |
| 175 | omap_dss_put_device(dssdev); |
| 176 | } |
| 177 | |
| 178 | #define MAX_EDID 512 |
| 179 | |
| 180 | static int omap_connector_get_modes(struct drm_connector *connector) |
| 181 | { |
| 182 | struct omap_connector *omap_connector = to_omap_connector(connector); |
| 183 | struct omap_dss_device *dssdev = omap_connector->dssdev; |
| 184 | struct omap_dss_driver *dssdrv = dssdev->driver; |
| 185 | struct drm_device *dev = connector->dev; |
| 186 | int n = 0; |
| 187 | |
| 188 | DBG("%s", omap_connector->dssdev->name); |
| 189 | |
| 190 | /* if display exposes EDID, then we parse that in the normal way to |
| 191 | * build table of supported modes.. otherwise (ie. fixed resolution |
| 192 | * LCD panels) we just return a single mode corresponding to the |
| 193 | * currently configured timings: |
| 194 | */ |
| 195 | if (dssdrv->read_edid) { |
| 196 | void *edid = kzalloc(MAX_EDID, GFP_KERNEL); |
| 197 | |
| 198 | if ((dssdrv->read_edid(dssdev, edid, MAX_EDID) > 0) && |
| 199 | drm_edid_is_valid(edid)) { |
| 200 | drm_mode_connector_update_edid_property( |
| 201 | connector, edid); |
| 202 | n = drm_add_edid_modes(connector, edid); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 203 | } else { |
| 204 | drm_mode_connector_update_edid_property( |
| 205 | connector, NULL); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 206 | } |
Jani Nikula | 451023d | 2012-08-15 09:32:39 +0000 | [diff] [blame] | 207 | kfree(edid); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 208 | } else { |
| 209 | struct drm_display_mode *mode = drm_mode_create(dev); |
Rob Clark | 0b0d7b6 | 2012-09-05 16:48:53 -0500 | [diff] [blame] | 210 | struct omap_video_timings timings = {0}; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 211 | |
| 212 | dssdrv->get_timings(dssdev, &timings); |
| 213 | |
| 214 | copy_timings_omap_to_drm(mode, &timings); |
| 215 | |
| 216 | mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; |
| 217 | drm_mode_set_name(mode); |
| 218 | drm_mode_probed_add(connector, mode); |
| 219 | |
| 220 | n = 1; |
| 221 | } |
| 222 | |
| 223 | return n; |
| 224 | } |
| 225 | |
| 226 | static int omap_connector_mode_valid(struct drm_connector *connector, |
| 227 | struct drm_display_mode *mode) |
| 228 | { |
| 229 | struct omap_connector *omap_connector = to_omap_connector(connector); |
| 230 | struct omap_dss_device *dssdev = omap_connector->dssdev; |
| 231 | struct omap_dss_driver *dssdrv = dssdev->driver; |
| 232 | struct omap_video_timings timings = {0}; |
| 233 | struct drm_device *dev = connector->dev; |
| 234 | struct drm_display_mode *new_mode; |
| 235 | int ret = MODE_BAD; |
| 236 | |
| 237 | copy_timings_drm_to_omap(&timings, mode); |
| 238 | mode->vrefresh = drm_mode_vrefresh(mode); |
| 239 | |
| 240 | if (!dssdrv->check_timings(dssdev, &timings)) { |
| 241 | /* check if vrefresh is still valid */ |
| 242 | new_mode = drm_mode_duplicate(dev, mode); |
| 243 | new_mode->clock = timings.pixel_clock; |
| 244 | new_mode->vrefresh = 0; |
| 245 | if (mode->vrefresh == drm_mode_vrefresh(new_mode)) |
| 246 | ret = MODE_OK; |
| 247 | drm_mode_destroy(dev, new_mode); |
| 248 | } |
| 249 | |
| 250 | DBG("connector: mode %s: " |
| 251 | "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x", |
| 252 | (ret == MODE_OK) ? "valid" : "invalid", |
| 253 | mode->base.id, mode->name, mode->vrefresh, mode->clock, |
| 254 | mode->hdisplay, mode->hsync_start, |
| 255 | mode->hsync_end, mode->htotal, |
| 256 | mode->vdisplay, mode->vsync_start, |
| 257 | mode->vsync_end, mode->vtotal, mode->type, mode->flags); |
| 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | struct drm_encoder *omap_connector_attached_encoder( |
| 263 | struct drm_connector *connector) |
| 264 | { |
| 265 | int i; |
| 266 | struct omap_connector *omap_connector = to_omap_connector(connector); |
| 267 | |
| 268 | for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) { |
| 269 | struct drm_mode_object *obj; |
| 270 | |
| 271 | if (connector->encoder_ids[i] == 0) |
| 272 | break; |
| 273 | |
| 274 | obj = drm_mode_object_find(connector->dev, |
| 275 | connector->encoder_ids[i], |
| 276 | DRM_MODE_OBJECT_ENCODER); |
| 277 | |
| 278 | if (obj) { |
| 279 | struct drm_encoder *encoder = obj_to_encoder(obj); |
| 280 | struct omap_overlay_manager *mgr = |
| 281 | omap_encoder_get_manager(encoder); |
| 282 | DBG("%s: found %s", omap_connector->dssdev->name, |
| 283 | mgr->name); |
| 284 | return encoder; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | DBG("%s: no encoder", omap_connector->dssdev->name); |
| 289 | |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | static const struct drm_connector_funcs omap_connector_funcs = { |
| 294 | .dpms = omap_connector_dpms, |
| 295 | .detect = omap_connector_detect, |
| 296 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 297 | .destroy = omap_connector_destroy, |
| 298 | }; |
| 299 | |
| 300 | static const struct drm_connector_helper_funcs omap_connector_helper_funcs = { |
| 301 | .get_modes = omap_connector_get_modes, |
| 302 | .mode_valid = omap_connector_mode_valid, |
| 303 | .best_encoder = omap_connector_attached_encoder, |
| 304 | }; |
| 305 | |
| 306 | /* called from encoder when mode is set, to propagate settings to the dssdev */ |
| 307 | void omap_connector_mode_set(struct drm_connector *connector, |
| 308 | struct drm_display_mode *mode) |
| 309 | { |
| 310 | struct drm_device *dev = connector->dev; |
| 311 | struct omap_connector *omap_connector = to_omap_connector(connector); |
| 312 | struct omap_dss_device *dssdev = omap_connector->dssdev; |
| 313 | struct omap_dss_driver *dssdrv = dssdev->driver; |
Rob Clark | 0b0d7b6 | 2012-09-05 16:48:53 -0500 | [diff] [blame] | 314 | struct omap_video_timings timings = {0}; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 315 | |
| 316 | copy_timings_drm_to_omap(&timings, mode); |
| 317 | |
| 318 | DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x", |
| 319 | omap_connector->dssdev->name, |
| 320 | mode->base.id, mode->name, mode->vrefresh, mode->clock, |
| 321 | mode->hdisplay, mode->hsync_start, |
| 322 | mode->hsync_end, mode->htotal, |
| 323 | mode->vdisplay, mode->vsync_start, |
| 324 | mode->vsync_end, mode->vtotal, mode->type, mode->flags); |
| 325 | |
| 326 | if (dssdrv->check_timings(dssdev, &timings)) { |
| 327 | dev_err(dev->dev, "could not set timings\n"); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | dssdrv->set_timings(dssdev, &timings); |
| 332 | } |
| 333 | |
| 334 | /* flush an area of the framebuffer (in case of manual update display that |
| 335 | * is not automatically flushed) |
| 336 | */ |
| 337 | void omap_connector_flush(struct drm_connector *connector, |
| 338 | int x, int y, int w, int h) |
| 339 | { |
| 340 | struct omap_connector *omap_connector = to_omap_connector(connector); |
| 341 | |
| 342 | /* TODO: enable when supported in dss */ |
| 343 | VERB("%s: %d,%d, %dx%d", omap_connector->dssdev->name, x, y, w, h); |
| 344 | } |
| 345 | |
| 346 | /* initialize connector */ |
| 347 | struct drm_connector *omap_connector_init(struct drm_device *dev, |
| 348 | int connector_type, struct omap_dss_device *dssdev) |
| 349 | { |
| 350 | struct drm_connector *connector = NULL; |
| 351 | struct omap_connector *omap_connector; |
| 352 | |
| 353 | DBG("%s", dssdev->name); |
| 354 | |
| 355 | omap_dss_get_device(dssdev); |
| 356 | |
| 357 | omap_connector = kzalloc(sizeof(struct omap_connector), GFP_KERNEL); |
| 358 | if (!omap_connector) { |
| 359 | dev_err(dev->dev, "could not allocate connector\n"); |
| 360 | goto fail; |
| 361 | } |
| 362 | |
| 363 | omap_connector->dssdev = dssdev; |
| 364 | connector = &omap_connector->base; |
| 365 | |
| 366 | drm_connector_init(dev, connector, &omap_connector_funcs, |
| 367 | connector_type); |
| 368 | drm_connector_helper_add(connector, &omap_connector_helper_funcs); |
| 369 | |
| 370 | #if 0 /* enable when dss2 supports hotplug */ |
| 371 | if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_HPD) |
| 372 | connector->polled = 0; |
| 373 | else |
| 374 | #endif |
| 375 | connector->polled = DRM_CONNECTOR_POLL_CONNECT | |
| 376 | DRM_CONNECTOR_POLL_DISCONNECT; |
| 377 | |
| 378 | connector->interlace_allowed = 1; |
| 379 | connector->doublescan_allowed = 0; |
| 380 | |
| 381 | drm_sysfs_connector_add(connector); |
| 382 | |
| 383 | return connector; |
| 384 | |
| 385 | fail: |
| 386 | if (connector) { |
| 387 | omap_connector_destroy(connector); |
| 388 | } |
| 389 | |
| 390 | return NULL; |
| 391 | } |