1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
4 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
7 #include <drm/drm_atomic.h>
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_crtc.h>
10 #include <drm/drm_gem_dma_helper.h>
11 #include <drm/drm_print.h>
12 #include <drm/drm_vblank.h>
14 #include "tidss_crtc.h"
15 #include "tidss_dispc.h"
16 #include "tidss_drv.h"
17 #include "tidss_irq.h"
18 #include "tidss_plane.h"
20 /* Page flip and frame done IRQs */
22 static void tidss_crtc_finish_page_flip(struct tidss_crtc *tcrtc)
24 struct drm_device *ddev = tcrtc->crtc.dev;
25 struct tidss_device *tidss = to_tidss(ddev);
26 struct drm_pending_vblank_event *event;
30 spin_lock_irqsave(&ddev->event_lock, flags);
33 * New settings are taken into use at VFP, and GO bit is cleared at
34 * the same time. This happens before the vertical blank interrupt.
35 * So there is a small change that the driver sets GO bit after VFP, but
36 * before vblank, and we have to check for that case here.
38 busy = dispc_vp_go_busy(tidss->dispc, tcrtc->hw_videoport);
40 spin_unlock_irqrestore(&ddev->event_lock, flags);
48 spin_unlock_irqrestore(&ddev->event_lock, flags);
52 drm_crtc_send_vblank_event(&tcrtc->crtc, event);
54 spin_unlock_irqrestore(&ddev->event_lock, flags);
56 drm_crtc_vblank_put(&tcrtc->crtc);
59 void tidss_crtc_vblank_irq(struct drm_crtc *crtc)
61 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
63 drm_crtc_handle_vblank(crtc);
65 tidss_crtc_finish_page_flip(tcrtc);
68 void tidss_crtc_framedone_irq(struct drm_crtc *crtc)
70 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
72 complete(&tcrtc->framedone_completion);
75 void tidss_crtc_error_irq(struct drm_crtc *crtc, u64 irqstatus)
77 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
79 dev_err_ratelimited(crtc->dev->dev, "CRTC%u SYNC LOST: (irq %llx)\n",
80 tcrtc->hw_videoport, irqstatus);
83 /* drm_crtc_helper_funcs */
85 static int tidss_crtc_atomic_check(struct drm_crtc *crtc,
86 struct drm_atomic_state *state)
88 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
90 struct drm_device *ddev = crtc->dev;
91 struct tidss_device *tidss = to_tidss(ddev);
92 struct dispc_device *dispc = tidss->dispc;
93 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
94 u32 hw_videoport = tcrtc->hw_videoport;
95 struct drm_display_mode *mode;
96 enum drm_mode_status ok;
98 if (!crtc_state->enable)
101 mode = &crtc_state->adjusted_mode;
103 ok = dispc_vp_mode_valid(dispc, hw_videoport, mode);
105 drm_dbg(ddev, "%s: bad mode: %ux%u pclk %u kHz\n",
106 __func__, mode->hdisplay, mode->vdisplay, mode->clock);
110 if (drm_atomic_crtc_needs_modeset(crtc_state))
111 drm_mode_set_crtcinfo(mode, 0);
113 return dispc_vp_bus_check(dispc, hw_videoport, crtc_state);
117 * This needs all affected planes to be present in the atomic
118 * state. The untouched planes are added to the state in
119 * tidss_atomic_check().
121 static void tidss_crtc_position_planes(struct tidss_device *tidss,
122 struct drm_crtc *crtc,
123 struct drm_crtc_state *old_state,
126 struct drm_atomic_state *ostate = old_state->state;
127 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
128 struct drm_crtc_state *cstate = crtc->state;
131 if (!newmodeset && !cstate->zpos_changed &&
132 !to_tidss_crtc_state(cstate)->plane_pos_changed)
135 for (layer = 0; layer < tidss->feat->num_vids ; layer++) {
136 struct drm_plane_state *pstate;
137 struct drm_plane *plane;
138 bool layer_active = false;
141 for_each_new_plane_in_state(ostate, plane, pstate, i) {
142 if (pstate->crtc != crtc || !pstate->visible)
145 if (pstate->normalized_zpos == layer) {
152 struct tidss_plane *tplane = to_tidss_plane(plane);
154 dispc_ovr_set_plane(tidss->dispc, tplane->hw_plane_id,
156 pstate->crtc_x, pstate->crtc_y,
159 dispc_ovr_enable_layer(tidss->dispc, tcrtc->hw_videoport, layer,
164 static void tidss_crtc_atomic_flush(struct drm_crtc *crtc,
165 struct drm_atomic_state *state)
167 struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
169 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
170 struct drm_device *ddev = crtc->dev;
171 struct tidss_device *tidss = to_tidss(ddev);
174 drm_dbg(ddev, "%s: %s is %sactive, %s modeset, event %p\n",
175 __func__, crtc->name, crtc->state->active ? "" : "not ",
176 drm_atomic_crtc_needs_modeset(crtc->state) ? "needs" : "doesn't need",
180 * Flush CRTC changes with go bit only if new modeset is not
181 * coming, so CRTC is enabled trough out the commit.
183 if (drm_atomic_crtc_needs_modeset(crtc->state))
186 /* If the GO bit is stuck we better quit here. */
187 if (WARN_ON(dispc_vp_go_busy(tidss->dispc, tcrtc->hw_videoport)))
190 /* We should have event if CRTC is enabled through out this commit. */
191 if (WARN_ON(!crtc->state->event))
194 /* Write vp properties to HW if needed. */
195 dispc_vp_setup(tidss->dispc, tcrtc->hw_videoport, crtc->state, false);
197 /* Update plane positions if needed. */
198 tidss_crtc_position_planes(tidss, crtc, old_crtc_state, false);
200 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
202 spin_lock_irqsave(&ddev->event_lock, flags);
203 dispc_vp_go(tidss->dispc, tcrtc->hw_videoport);
205 WARN_ON(tcrtc->event);
207 tcrtc->event = crtc->state->event;
208 crtc->state->event = NULL;
210 spin_unlock_irqrestore(&ddev->event_lock, flags);
213 static void tidss_crtc_atomic_enable(struct drm_crtc *crtc,
214 struct drm_atomic_state *state)
216 struct drm_crtc_state *old_state = drm_atomic_get_old_crtc_state(state,
218 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
219 struct drm_device *ddev = crtc->dev;
220 struct tidss_device *tidss = to_tidss(ddev);
221 const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
225 dev_dbg(ddev->dev, "%s, event %p\n", __func__, crtc->state->event);
227 tidss_runtime_get(tidss);
229 r = dispc_vp_set_clk_rate(tidss->dispc, tcrtc->hw_videoport,
230 mode->crtc_clock * 1000);
234 r = dispc_vp_enable_clk(tidss->dispc, tcrtc->hw_videoport);
238 dispc_vp_setup(tidss->dispc, tcrtc->hw_videoport, crtc->state, true);
239 tidss_crtc_position_planes(tidss, crtc, old_state, true);
241 /* Turn vertical blanking interrupt reporting on. */
242 drm_crtc_vblank_on(crtc);
244 dispc_vp_prepare(tidss->dispc, tcrtc->hw_videoport, crtc->state);
246 dispc_vp_enable(tidss->dispc, tcrtc->hw_videoport);
248 spin_lock_irqsave(&ddev->event_lock, flags);
250 if (crtc->state->event) {
251 drm_crtc_send_vblank_event(crtc, crtc->state->event);
252 crtc->state->event = NULL;
255 spin_unlock_irqrestore(&ddev->event_lock, flags);
258 static void tidss_crtc_atomic_disable(struct drm_crtc *crtc,
259 struct drm_atomic_state *state)
261 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
262 struct drm_device *ddev = crtc->dev;
263 struct tidss_device *tidss = to_tidss(ddev);
266 dev_dbg(ddev->dev, "%s, event %p\n", __func__, crtc->state->event);
268 reinit_completion(&tcrtc->framedone_completion);
271 * If a layer is left enabled when the videoport is disabled, and the
272 * vid pipeline that was used for the layer is taken into use on
273 * another videoport, the DSS will report sync lost issues. Disable all
274 * the layers here as a work-around.
276 for (u32 layer = 0; layer < tidss->feat->num_vids; layer++)
277 dispc_ovr_enable_layer(tidss->dispc, tcrtc->hw_videoport, layer,
280 dispc_vp_disable(tidss->dispc, tcrtc->hw_videoport);
282 if (!wait_for_completion_timeout(&tcrtc->framedone_completion,
283 msecs_to_jiffies(500)))
284 dev_err(tidss->dev, "Timeout waiting for framedone on crtc %d",
285 tcrtc->hw_videoport);
287 dispc_vp_unprepare(tidss->dispc, tcrtc->hw_videoport);
289 spin_lock_irqsave(&ddev->event_lock, flags);
290 if (crtc->state->event) {
291 drm_crtc_send_vblank_event(crtc, crtc->state->event);
292 crtc->state->event = NULL;
294 spin_unlock_irqrestore(&ddev->event_lock, flags);
296 drm_crtc_vblank_off(crtc);
298 dispc_vp_disable_clk(tidss->dispc, tcrtc->hw_videoport);
300 tidss_runtime_put(tidss);
304 enum drm_mode_status tidss_crtc_mode_valid(struct drm_crtc *crtc,
305 const struct drm_display_mode *mode)
307 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
308 struct drm_device *ddev = crtc->dev;
309 struct tidss_device *tidss = to_tidss(ddev);
311 return dispc_vp_mode_valid(tidss->dispc, tcrtc->hw_videoport, mode);
314 static const struct drm_crtc_helper_funcs tidss_crtc_helper_funcs = {
315 .atomic_check = tidss_crtc_atomic_check,
316 .atomic_flush = tidss_crtc_atomic_flush,
317 .atomic_enable = tidss_crtc_atomic_enable,
318 .atomic_disable = tidss_crtc_atomic_disable,
320 .mode_valid = tidss_crtc_mode_valid,
325 static int tidss_crtc_enable_vblank(struct drm_crtc *crtc)
327 struct drm_device *ddev = crtc->dev;
328 struct tidss_device *tidss = to_tidss(ddev);
330 tidss_runtime_get(tidss);
332 tidss_irq_enable_vblank(crtc);
337 static void tidss_crtc_disable_vblank(struct drm_crtc *crtc)
339 struct drm_device *ddev = crtc->dev;
340 struct tidss_device *tidss = to_tidss(ddev);
342 tidss_irq_disable_vblank(crtc);
344 tidss_runtime_put(tidss);
347 static void tidss_crtc_destroy_state(struct drm_crtc *crtc,
348 struct drm_crtc_state *state)
350 struct tidss_crtc_state *tstate = to_tidss_crtc_state(state);
352 __drm_atomic_helper_crtc_destroy_state(&tstate->base);
356 static void tidss_crtc_reset(struct drm_crtc *crtc)
358 struct tidss_crtc_state *tstate;
361 tidss_crtc_destroy_state(crtc, crtc->state);
363 tstate = kzalloc(sizeof(*tstate), GFP_KERNEL);
369 __drm_atomic_helper_crtc_reset(crtc, &tstate->base);
372 static struct drm_crtc_state *tidss_crtc_duplicate_state(struct drm_crtc *crtc)
374 struct tidss_crtc_state *state, *current_state;
376 if (WARN_ON(!crtc->state))
379 current_state = to_tidss_crtc_state(crtc->state);
381 state = kmalloc(sizeof(*state), GFP_KERNEL);
385 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
387 state->plane_pos_changed = false;
389 state->bus_format = current_state->bus_format;
390 state->bus_flags = current_state->bus_flags;
395 static void tidss_crtc_destroy(struct drm_crtc *crtc)
397 struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
399 drm_crtc_cleanup(crtc);
403 static const struct drm_crtc_funcs tidss_crtc_funcs = {
404 .reset = tidss_crtc_reset,
405 .destroy = tidss_crtc_destroy,
406 .set_config = drm_atomic_helper_set_config,
407 .page_flip = drm_atomic_helper_page_flip,
408 .atomic_duplicate_state = tidss_crtc_duplicate_state,
409 .atomic_destroy_state = tidss_crtc_destroy_state,
410 .enable_vblank = tidss_crtc_enable_vblank,
411 .disable_vblank = tidss_crtc_disable_vblank,
414 struct tidss_crtc *tidss_crtc_create(struct tidss_device *tidss,
416 struct drm_plane *primary)
418 struct tidss_crtc *tcrtc;
419 struct drm_crtc *crtc;
420 unsigned int gamma_lut_size = 0;
421 bool has_ctm = tidss->feat->vp_feat.color.has_ctm;
424 tcrtc = kzalloc(sizeof(*tcrtc), GFP_KERNEL);
426 return ERR_PTR(-ENOMEM);
428 tcrtc->hw_videoport = hw_videoport;
429 init_completion(&tcrtc->framedone_completion);
433 ret = drm_crtc_init_with_planes(&tidss->ddev, crtc, primary,
434 NULL, &tidss_crtc_funcs, NULL);
440 drm_crtc_helper_add(crtc, &tidss_crtc_helper_funcs);
443 * The dispc gamma functions adapt to what ever size we ask
444 * from it no matter what HW supports. X-server assumes 256
445 * element gamma tables so lets use that.
447 if (tidss->feat->vp_feat.color.gamma_size)
448 gamma_lut_size = 256;
450 drm_crtc_enable_color_mgmt(crtc, 0, has_ctm, gamma_lut_size);
452 drm_mode_crtc_set_gamma_size(crtc, gamma_lut_size);