1// SPDX-License-Identifier: GPL-2.0-or-later 2/* exynos_drm_crtc.c 3 * 4 * Copyright (c) 2011 Samsung Electronics Co., Ltd. 5 * Authors: 6 * Inki Dae <inki.dae@samsung.com> 7 * Joonyoung Shim <jy0922.shim@samsung.com> 8 * Seung-Woo Kim <sw0312.kim@samsung.com> 9 */ 10 11#include <drm/drm_atomic.h> 12#include <drm/drm_atomic_helper.h> 13#include <drm/drm_encoder.h> 14#include <drm/drm_probe_helper.h> 15#include <drm/drm_vblank.h> 16 17#include "exynos_drm_crtc.h" 18#include "exynos_drm_drv.h" 19#include "exynos_drm_plane.h" 20 21static void exynos_drm_crtc_atomic_enable(struct drm_crtc *crtc, 22 struct drm_crtc_state *old_state) 23{ 24 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 25 26 if (exynos_crtc->ops->atomic_enable) 27 exynos_crtc->ops->atomic_enable(exynos_crtc); 28 29 drm_crtc_vblank_on(crtc); 30} 31 32static void exynos_drm_crtc_atomic_disable(struct drm_crtc *crtc, 33 struct drm_crtc_state *old_state) 34{ 35 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 36 37 drm_crtc_vblank_off(crtc); 38 39 if (exynos_crtc->ops->atomic_disable) 40 exynos_crtc->ops->atomic_disable(exynos_crtc); 41 42 spin_lock_irq(&crtc->dev->event_lock); 43 if (crtc->state->event && !crtc->state->active) { 44 drm_crtc_send_vblank_event(crtc, crtc->state->event); 45 crtc->state->event = NULL; 46 } 47 spin_unlock_irq(&crtc->dev->event_lock); 48} 49 50static int exynos_crtc_atomic_check(struct drm_crtc *crtc, 51 struct drm_crtc_state *state) 52{ 53 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 54 55 if (!state->enable) 56 return 0; 57 58 if (exynos_crtc->ops->atomic_check) 59 return exynos_crtc->ops->atomic_check(exynos_crtc, state); 60 61 return 0; 62} 63 64static void exynos_crtc_atomic_begin(struct drm_crtc *crtc, 65 struct drm_crtc_state *old_crtc_state) 66{ 67 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 68 69 if (exynos_crtc->ops->atomic_begin) 70 exynos_crtc->ops->atomic_begin(exynos_crtc); 71} 72 73static void exynos_crtc_atomic_flush(struct drm_crtc *crtc, 74 struct drm_crtc_state *old_crtc_state) 75{ 76 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 77 78 if (exynos_crtc->ops->atomic_flush) 79 exynos_crtc->ops->atomic_flush(exynos_crtc); 80} 81 82static enum drm_mode_status exynos_crtc_mode_valid(struct drm_crtc *crtc, 83 const struct drm_display_mode *mode) 84{ 85 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 86 87 if (exynos_crtc->ops->mode_valid) 88 return exynos_crtc->ops->mode_valid(exynos_crtc, mode); 89 90 return MODE_OK; 91} 92 93static bool exynos_crtc_mode_fixup(struct drm_crtc *crtc, 94 const struct drm_display_mode *mode, 95 struct drm_display_mode *adjusted_mode) 96{ 97 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 98 99 if (exynos_crtc->ops->mode_fixup) 100 return exynos_crtc->ops->mode_fixup(exynos_crtc, mode, 101 adjusted_mode); 102 103 return true; 104} 105 106 107static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = { 108 .mode_valid = exynos_crtc_mode_valid, 109 .mode_fixup = exynos_crtc_mode_fixup, 110 .atomic_check = exynos_crtc_atomic_check, 111 .atomic_begin = exynos_crtc_atomic_begin, 112 .atomic_flush = exynos_crtc_atomic_flush, 113 .atomic_enable = exynos_drm_crtc_atomic_enable, 114 .atomic_disable = exynos_drm_crtc_atomic_disable, 115}; 116 117void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc) 118{ 119 struct drm_crtc *crtc = &exynos_crtc->base; 120 struct drm_pending_vblank_event *event = crtc->state->event; 121 unsigned long flags; 122 123 if (!event) 124 return; 125 crtc->state->event = NULL; 126 127 WARN_ON(drm_crtc_vblank_get(crtc) != 0); 128 129 spin_lock_irqsave(&crtc->dev->event_lock, flags); 130 drm_crtc_arm_vblank_event(crtc, event); 131 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 132} 133 134static void exynos_drm_crtc_destroy(struct drm_crtc *crtc) 135{ 136 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 137 138 drm_crtc_cleanup(crtc); 139 kfree(exynos_crtc); 140} 141 142static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc) 143{ 144 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 145 146 if (exynos_crtc->ops->enable_vblank) 147 return exynos_crtc->ops->enable_vblank(exynos_crtc); 148 149 return 0; 150} 151 152static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc) 153{ 154 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 155 156 if (exynos_crtc->ops->disable_vblank) 157 exynos_crtc->ops->disable_vblank(exynos_crtc); 158} 159 160static const struct drm_crtc_funcs exynos_crtc_funcs = { 161 .set_config = drm_atomic_helper_set_config, 162 .page_flip = drm_atomic_helper_page_flip, 163 .destroy = exynos_drm_crtc_destroy, 164 .reset = drm_atomic_helper_crtc_reset, 165 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 166 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 167 .enable_vblank = exynos_drm_crtc_enable_vblank, 168 .disable_vblank = exynos_drm_crtc_disable_vblank, 169}; 170 171struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev, 172 struct drm_plane *plane, 173 enum exynos_drm_output_type type, 174 const struct exynos_drm_crtc_ops *ops, 175 void *ctx) 176{ 177 struct exynos_drm_crtc *exynos_crtc; 178 struct drm_crtc *crtc; 179 int ret; 180 181 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL); 182 if (!exynos_crtc) 183 return ERR_PTR(-ENOMEM); 184 185 exynos_crtc->type = type; 186 exynos_crtc->ops = ops; 187 exynos_crtc->ctx = ctx; 188 189 crtc = &exynos_crtc->base; 190 191 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL, 192 &exynos_crtc_funcs, NULL); 193 if (ret < 0) 194 goto err_crtc; 195 196 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs); 197 198 return exynos_crtc; 199 200err_crtc: 201 plane->funcs->destroy(plane); 202 kfree(exynos_crtc); 203 return ERR_PTR(ret); 204} 205 206struct exynos_drm_crtc *exynos_drm_crtc_get_by_type(struct drm_device *drm_dev, 207 enum exynos_drm_output_type out_type) 208{ 209 struct drm_crtc *crtc; 210 211 drm_for_each_crtc(crtc, drm_dev) 212 if (to_exynos_crtc(crtc)->type == out_type) 213 return to_exynos_crtc(crtc); 214 215 return ERR_PTR(-ENODEV); 216} 217 218int exynos_drm_set_possible_crtcs(struct drm_encoder *encoder, 219 enum exynos_drm_output_type out_type) 220{ 221 struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(encoder->dev, 222 out_type); 223 224 if (IS_ERR(crtc)) 225 return PTR_ERR(crtc); 226 227 encoder->possible_crtcs = drm_crtc_mask(&crtc->base); 228 229 return 0; 230} 231 232void exynos_drm_crtc_te_handler(struct drm_crtc *crtc) 233{ 234 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 235 236 if (exynos_crtc->ops->te_handler) 237 exynos_crtc->ops->te_handler(exynos_crtc); 238} 239