Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* exynos_drm_encoder.c |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * Authors: |
| 5 | * Inki Dae <inki.dae@samsung.com> |
| 6 | * Joonyoung Shim <jy0922.shim@samsung.com> |
| 7 | * Seung-Woo Kim <sw0312.kim@samsung.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 10 | * copy of this software and associated documentation files (the "Software"), |
| 11 | * to deal in the Software without restriction, including without limitation |
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 | * and/or sell copies of the Software, and to permit persons to whom the |
| 14 | * Software is furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice (including the next |
| 17 | * paragraph) shall be included in all copies or substantial portions of the |
| 18 | * Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 24 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 25 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | * OTHER DEALINGS IN THE SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include "drmP.h" |
| 30 | #include "drm_crtc_helper.h" |
| 31 | |
| 32 | #include "exynos_drm_drv.h" |
| 33 | #include "exynos_drm_crtc.h" |
| 34 | #include "exynos_drm_encoder.h" |
| 35 | |
| 36 | #define to_exynos_encoder(x) container_of(x, struct exynos_drm_encoder,\ |
| 37 | drm_encoder) |
| 38 | |
| 39 | /* |
| 40 | * exynos specific encoder structure. |
| 41 | * |
| 42 | * @drm_encoder: encoder object. |
| 43 | * @manager: specific encoder has its own manager to control a hardware |
| 44 | * appropriately and we can access a hardware drawing on this manager. |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 45 | * @dpms: store the encoder dpms value. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 46 | */ |
| 47 | struct exynos_drm_encoder { |
| 48 | struct drm_encoder drm_encoder; |
| 49 | struct exynos_drm_manager *manager; |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 50 | int dpms; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 51 | }; |
| 52 | |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 53 | static void exynos_drm_display_power(struct drm_encoder *encoder, int mode) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 54 | { |
| 55 | struct drm_device *dev = encoder->dev; |
| 56 | struct drm_connector *connector; |
| 57 | struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder); |
Joonyoung Shim | 396464d | 2011-11-14 15:20:49 +0900 | [diff] [blame] | 58 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 59 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 60 | if (connector->encoder == encoder) { |
Inki Dae | 74ccc53 | 2011-10-19 17:23:07 +0900 | [diff] [blame] | 61 | struct exynos_drm_display_ops *display_ops = |
| 62 | manager->display_ops; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 63 | |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 64 | DRM_DEBUG_KMS("connector[%d] dpms[%d]\n", |
| 65 | connector->base.id, mode); |
Inki Dae | 74ccc53 | 2011-10-19 17:23:07 +0900 | [diff] [blame] | 66 | if (display_ops && display_ops->power_on) |
| 67 | display_ops->power_on(manager->dev, mode); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 72 | static void exynos_drm_encoder_dpms(struct drm_encoder *encoder, int mode) |
| 73 | { |
| 74 | struct drm_device *dev = encoder->dev; |
| 75 | struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder); |
| 76 | struct exynos_drm_manager_ops *manager_ops = manager->ops; |
| 77 | struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder); |
| 78 | |
| 79 | DRM_DEBUG_KMS("%s, encoder dpms: %d\n", __FILE__, mode); |
| 80 | |
| 81 | if (exynos_encoder->dpms == mode) { |
| 82 | DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n"); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | mutex_lock(&dev->struct_mutex); |
| 87 | |
| 88 | switch (mode) { |
| 89 | case DRM_MODE_DPMS_ON: |
| 90 | if (manager_ops && manager_ops->apply) |
| 91 | manager_ops->apply(manager->dev); |
| 92 | exynos_drm_display_power(encoder, mode); |
| 93 | exynos_encoder->dpms = mode; |
| 94 | break; |
| 95 | case DRM_MODE_DPMS_STANDBY: |
| 96 | case DRM_MODE_DPMS_SUSPEND: |
| 97 | case DRM_MODE_DPMS_OFF: |
| 98 | exynos_drm_display_power(encoder, mode); |
| 99 | exynos_encoder->dpms = mode; |
| 100 | break; |
| 101 | default: |
| 102 | DRM_ERROR("unspecified mode %d\n", mode); |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | mutex_unlock(&dev->struct_mutex); |
| 107 | } |
| 108 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 109 | static bool |
| 110 | exynos_drm_encoder_mode_fixup(struct drm_encoder *encoder, |
| 111 | struct drm_display_mode *mode, |
| 112 | struct drm_display_mode *adjusted_mode) |
| 113 | { |
| 114 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 115 | |
| 116 | /* drm framework doesn't check NULL. */ |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | static void exynos_drm_encoder_mode_set(struct drm_encoder *encoder, |
| 122 | struct drm_display_mode *mode, |
| 123 | struct drm_display_mode *adjusted_mode) |
| 124 | { |
| 125 | struct drm_device *dev = encoder->dev; |
| 126 | struct drm_connector *connector; |
| 127 | struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder); |
| 128 | struct exynos_drm_manager_ops *manager_ops = manager->ops; |
| 129 | struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops; |
| 130 | struct exynos_drm_overlay *overlay = get_exynos_drm_overlay(dev, |
| 131 | encoder->crtc); |
| 132 | |
| 133 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 134 | |
| 135 | mode = adjusted_mode; |
| 136 | |
| 137 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 138 | if (connector->encoder == encoder) { |
| 139 | if (manager_ops && manager_ops->mode_set) |
| 140 | manager_ops->mode_set(manager->dev, mode); |
| 141 | |
| 142 | if (overlay_ops && overlay_ops->mode_set) |
| 143 | overlay_ops->mode_set(manager->dev, overlay); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void exynos_drm_encoder_prepare(struct drm_encoder *encoder) |
| 149 | { |
| 150 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 151 | |
| 152 | /* drm framework doesn't check NULL. */ |
| 153 | } |
| 154 | |
| 155 | static void exynos_drm_encoder_commit(struct drm_encoder *encoder) |
| 156 | { |
| 157 | struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder); |
| 158 | struct exynos_drm_manager_ops *manager_ops = manager->ops; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 159 | |
| 160 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 161 | |
| 162 | if (manager_ops && manager_ops->commit) |
| 163 | manager_ops->commit(manager->dev); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static struct drm_crtc * |
| 167 | exynos_drm_encoder_get_crtc(struct drm_encoder *encoder) |
| 168 | { |
| 169 | return encoder->crtc; |
| 170 | } |
| 171 | |
| 172 | static struct drm_encoder_helper_funcs exynos_encoder_helper_funcs = { |
| 173 | .dpms = exynos_drm_encoder_dpms, |
| 174 | .mode_fixup = exynos_drm_encoder_mode_fixup, |
| 175 | .mode_set = exynos_drm_encoder_mode_set, |
| 176 | .prepare = exynos_drm_encoder_prepare, |
| 177 | .commit = exynos_drm_encoder_commit, |
| 178 | .get_crtc = exynos_drm_encoder_get_crtc, |
| 179 | }; |
| 180 | |
| 181 | static void exynos_drm_encoder_destroy(struct drm_encoder *encoder) |
| 182 | { |
| 183 | struct exynos_drm_encoder *exynos_encoder = |
| 184 | to_exynos_encoder(encoder); |
| 185 | |
| 186 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 187 | |
| 188 | exynos_encoder->manager->pipe = -1; |
| 189 | |
| 190 | drm_encoder_cleanup(encoder); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 191 | kfree(exynos_encoder); |
| 192 | } |
| 193 | |
| 194 | static struct drm_encoder_funcs exynos_encoder_funcs = { |
| 195 | .destroy = exynos_drm_encoder_destroy, |
| 196 | }; |
| 197 | |
| 198 | struct drm_encoder * |
| 199 | exynos_drm_encoder_create(struct drm_device *dev, |
| 200 | struct exynos_drm_manager *manager, |
| 201 | unsigned int possible_crtcs) |
| 202 | { |
| 203 | struct drm_encoder *encoder; |
| 204 | struct exynos_drm_encoder *exynos_encoder; |
| 205 | |
| 206 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 207 | |
| 208 | if (!manager || !possible_crtcs) |
| 209 | return NULL; |
| 210 | |
| 211 | if (!manager->dev) |
| 212 | return NULL; |
| 213 | |
| 214 | exynos_encoder = kzalloc(sizeof(*exynos_encoder), GFP_KERNEL); |
| 215 | if (!exynos_encoder) { |
| 216 | DRM_ERROR("failed to allocate encoder\n"); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 220 | exynos_encoder->dpms = DRM_MODE_DPMS_OFF; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 221 | exynos_encoder->manager = manager; |
| 222 | encoder = &exynos_encoder->drm_encoder; |
| 223 | encoder->possible_crtcs = possible_crtcs; |
| 224 | |
| 225 | DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs); |
| 226 | |
| 227 | drm_encoder_init(dev, encoder, &exynos_encoder_funcs, |
| 228 | DRM_MODE_ENCODER_TMDS); |
| 229 | |
| 230 | drm_encoder_helper_add(encoder, &exynos_encoder_helper_funcs); |
| 231 | |
| 232 | DRM_DEBUG_KMS("encoder has been created\n"); |
| 233 | |
| 234 | return encoder; |
| 235 | } |
| 236 | |
| 237 | struct exynos_drm_manager *exynos_drm_get_manager(struct drm_encoder *encoder) |
| 238 | { |
| 239 | return to_exynos_encoder(encoder)->manager; |
| 240 | } |
| 241 | |
| 242 | void exynos_drm_fn_encoder(struct drm_crtc *crtc, void *data, |
| 243 | void (*fn)(struct drm_encoder *, void *)) |
| 244 | { |
| 245 | struct drm_device *dev = crtc->dev; |
| 246 | struct drm_encoder *encoder; |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 247 | struct exynos_drm_private *private = dev->dev_private; |
| 248 | struct exynos_drm_manager *manager; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 249 | |
| 250 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 251 | /* |
| 252 | * if crtc is detached from encoder, check pipe, |
| 253 | * otherwise check crtc attached to encoder |
| 254 | */ |
| 255 | if (!encoder->crtc) { |
| 256 | manager = to_exynos_encoder(encoder)->manager; |
| 257 | if (manager->pipe < 0 || |
| 258 | private->crtc[manager->pipe] != crtc) |
| 259 | continue; |
| 260 | } else { |
| 261 | if (encoder->crtc != crtc) |
| 262 | continue; |
| 263 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 264 | |
| 265 | fn(encoder, data); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void exynos_drm_enable_vblank(struct drm_encoder *encoder, void *data) |
| 270 | { |
| 271 | struct exynos_drm_manager *manager = |
| 272 | to_exynos_encoder(encoder)->manager; |
| 273 | struct exynos_drm_manager_ops *manager_ops = manager->ops; |
| 274 | int crtc = *(int *)data; |
| 275 | |
| 276 | if (manager->pipe == -1) |
| 277 | manager->pipe = crtc; |
| 278 | |
| 279 | if (manager_ops->enable_vblank) |
| 280 | manager_ops->enable_vblank(manager->dev); |
| 281 | } |
| 282 | |
| 283 | void exynos_drm_disable_vblank(struct drm_encoder *encoder, void *data) |
| 284 | { |
| 285 | struct exynos_drm_manager *manager = |
| 286 | to_exynos_encoder(encoder)->manager; |
| 287 | struct exynos_drm_manager_ops *manager_ops = manager->ops; |
| 288 | int crtc = *(int *)data; |
| 289 | |
| 290 | if (manager->pipe == -1) |
| 291 | manager->pipe = crtc; |
| 292 | |
| 293 | if (manager_ops->disable_vblank) |
| 294 | manager_ops->disable_vblank(manager->dev); |
| 295 | } |
| 296 | |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 297 | void exynos_drm_encoder_crtc_plane_commit(struct drm_encoder *encoder, |
| 298 | void *data) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 299 | { |
| 300 | struct exynos_drm_manager *manager = |
| 301 | to_exynos_encoder(encoder)->manager; |
| 302 | struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops; |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 303 | int zpos = DEFAULT_ZPOS; |
| 304 | |
| 305 | if (data) |
| 306 | zpos = *(int *)data; |
| 307 | |
| 308 | if (overlay_ops && overlay_ops->commit) |
| 309 | overlay_ops->commit(manager->dev, zpos); |
| 310 | } |
| 311 | |
| 312 | void exynos_drm_encoder_crtc_commit(struct drm_encoder *encoder, void *data) |
| 313 | { |
| 314 | struct exynos_drm_manager *manager = |
| 315 | to_exynos_encoder(encoder)->manager; |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 316 | int crtc = *(int *)data; |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 317 | int zpos = DEFAULT_ZPOS; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 318 | |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 319 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 320 | |
| 321 | /* |
| 322 | * when crtc is detached from encoder, this pipe is used |
| 323 | * to select manager operation |
| 324 | */ |
| 325 | manager->pipe = crtc; |
| 326 | |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 327 | exynos_drm_encoder_crtc_plane_commit(encoder, &zpos); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 328 | } |
| 329 | |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 330 | void exynos_drm_encoder_dpms_from_crtc(struct drm_encoder *encoder, void *data) |
| 331 | { |
| 332 | struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder); |
| 333 | int mode = *(int *)data; |
| 334 | |
| 335 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 336 | |
| 337 | exynos_drm_encoder_dpms(encoder, mode); |
| 338 | |
| 339 | exynos_encoder->dpms = mode; |
| 340 | } |
| 341 | |
| 342 | void exynos_drm_encoder_crtc_dpms(struct drm_encoder *encoder, void *data) |
| 343 | { |
| 344 | struct drm_device *dev = encoder->dev; |
| 345 | struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder); |
| 346 | struct exynos_drm_manager *manager = exynos_encoder->manager; |
| 347 | struct exynos_drm_manager_ops *manager_ops = manager->ops; |
| 348 | struct drm_connector *connector; |
| 349 | int mode = *(int *)data; |
| 350 | |
| 351 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 352 | |
| 353 | if (manager_ops && manager_ops->dpms) |
| 354 | manager_ops->dpms(manager->dev, mode); |
| 355 | |
| 356 | /* |
| 357 | * set current dpms mode to the connector connected to |
| 358 | * current encoder. connector->dpms would be checked |
| 359 | * at drm_helper_connector_dpms() |
| 360 | */ |
| 361 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
| 362 | if (connector->encoder == encoder) |
| 363 | connector->dpms = mode; |
| 364 | |
| 365 | /* |
| 366 | * if this condition is ok then it means that the crtc is already |
| 367 | * detached from encoder and last function for detaching is properly |
| 368 | * done, so clear pipe from manager to prevent repeated call. |
| 369 | */ |
| 370 | if (mode > DRM_MODE_DPMS_ON) { |
| 371 | if (!encoder->crtc) |
| 372 | manager->pipe = -1; |
| 373 | } |
| 374 | } |
| 375 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 376 | void exynos_drm_encoder_crtc_mode_set(struct drm_encoder *encoder, void *data) |
| 377 | { |
| 378 | struct exynos_drm_manager *manager = |
| 379 | to_exynos_encoder(encoder)->manager; |
| 380 | struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops; |
| 381 | struct exynos_drm_overlay *overlay = data; |
| 382 | |
Seung-Woo Kim | b0e0f85 | 2011-11-04 17:31:41 +0900 | [diff] [blame] | 383 | if (overlay_ops && overlay_ops->mode_set) |
| 384 | overlay_ops->mode_set(manager->dev, overlay); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 385 | } |
| 386 | |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 387 | void exynos_drm_encoder_crtc_disable(struct drm_encoder *encoder, void *data) |
| 388 | { |
| 389 | struct exynos_drm_manager *manager = |
| 390 | to_exynos_encoder(encoder)->manager; |
| 391 | struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops; |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 392 | int zpos = DEFAULT_ZPOS; |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 393 | |
| 394 | DRM_DEBUG_KMS("\n"); |
| 395 | |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 396 | if (data) |
| 397 | zpos = *(int *)data; |
| 398 | |
Seung-Woo Kim | b0e0f85 | 2011-11-04 17:31:41 +0900 | [diff] [blame] | 399 | if (overlay_ops && overlay_ops->disable) |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 400 | overlay_ops->disable(manager->dev, zpos); |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 401 | } |
| 402 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 403 | MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>"); |
| 404 | MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); |
| 405 | MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>"); |
| 406 | MODULE_DESCRIPTION("Samsung SoC DRM Encoder Driver"); |
| 407 | MODULE_LICENSE("GPL"); |