1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b961c48bSJyri Sarha /*
3b961c48bSJyri Sarha * Copyright (C) 2015 Texas Instruments
4b961c48bSJyri Sarha * Author: Jyri Sarha <jsarha@ti.com>
5b961c48bSJyri Sarha */
6b961c48bSJyri Sarha
7b961c48bSJyri Sarha #include <drm/drm_atomic.h>
8b961c48bSJyri Sarha #include <drm/drm_atomic_helper.h>
9fcb57664SSam Ravnborg #include <drm/drm_fourcc.h>
10720cf96dSVille Syrjälä #include <drm/drm_framebuffer.h>
11b961c48bSJyri Sarha
12b961c48bSJyri Sarha #include "tilcdc_drv.h"
13b961c48bSJyri Sarha
14fba3baa8SNishka Dasgupta static const struct drm_plane_funcs tilcdc_plane_funcs = {
15b961c48bSJyri Sarha .update_plane = drm_atomic_helper_update_plane,
16b961c48bSJyri Sarha .disable_plane = drm_atomic_helper_disable_plane,
17b961c48bSJyri Sarha .destroy = drm_plane_cleanup,
18b961c48bSJyri Sarha .reset = drm_atomic_helper_plane_reset,
19b961c48bSJyri Sarha .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
20b961c48bSJyri Sarha .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
21b961c48bSJyri Sarha };
22b961c48bSJyri Sarha
tilcdc_plane_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)23b961c48bSJyri Sarha static int tilcdc_plane_atomic_check(struct drm_plane *plane,
247c11b99aSMaxime Ripard struct drm_atomic_state *state)
25b961c48bSJyri Sarha {
267c11b99aSMaxime Ripard struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
277c11b99aSMaxime Ripard plane);
28b961c48bSJyri Sarha struct drm_crtc_state *crtc_state;
290b6aaf9dSMaxime Ripard struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
300b6aaf9dSMaxime Ripard plane);
3159f11a43SLaurent Pinchart unsigned int pitch;
32b961c48bSJyri Sarha
33ba5c1649SMaxime Ripard if (!new_state->crtc)
34b961c48bSJyri Sarha return 0;
35b961c48bSJyri Sarha
36ba5c1649SMaxime Ripard if (WARN_ON(!new_state->fb))
37b961c48bSJyri Sarha return -EINVAL;
38b961c48bSJyri Sarha
39ba5c1649SMaxime Ripard if (new_state->crtc_x || new_state->crtc_y) {
40b961c48bSJyri Sarha dev_err(plane->dev->dev, "%s: crtc position must be zero.",
41b961c48bSJyri Sarha __func__);
42b961c48bSJyri Sarha return -EINVAL;
43b961c48bSJyri Sarha }
44b961c48bSJyri Sarha
45dec92020SMaxime Ripard crtc_state = drm_atomic_get_existing_crtc_state(state,
46ba5c1649SMaxime Ripard new_state->crtc);
47b961c48bSJyri Sarha /* we should have a crtc state if the plane is attached to a crtc */
48b961c48bSJyri Sarha if (WARN_ON(!crtc_state))
49b961c48bSJyri Sarha return 0;
50b961c48bSJyri Sarha
51ba5c1649SMaxime Ripard if (crtc_state->mode.hdisplay != new_state->crtc_w ||
52ba5c1649SMaxime Ripard crtc_state->mode.vdisplay != new_state->crtc_h) {
53b961c48bSJyri Sarha dev_err(plane->dev->dev,
54b961c48bSJyri Sarha "%s: Size must match mode (%dx%d == %dx%d)", __func__,
55b961c48bSJyri Sarha crtc_state->mode.hdisplay, crtc_state->mode.vdisplay,
56ba5c1649SMaxime Ripard new_state->crtc_w, new_state->crtc_h);
57b961c48bSJyri Sarha return -EINVAL;
58b961c48bSJyri Sarha }
59b961c48bSJyri Sarha
6059f11a43SLaurent Pinchart pitch = crtc_state->mode.hdisplay *
61ba5c1649SMaxime Ripard new_state->fb->format->cpp[0];
62ba5c1649SMaxime Ripard if (new_state->fb->pitches[0] != pitch) {
63b961c48bSJyri Sarha dev_err(plane->dev->dev,
64b961c48bSJyri Sarha "Invalid pitch: fb and crtc widths must be the same");
65b961c48bSJyri Sarha return -EINVAL;
66b961c48bSJyri Sarha }
67b961c48bSJyri Sarha
68ba5c1649SMaxime Ripard if (old_state->fb && new_state->fb->format != old_state->fb->format) {
69b961c48bSJyri Sarha dev_dbg(plane->dev->dev,
70b961c48bSJyri Sarha "%s(): pixel format change requires mode_change\n",
71b961c48bSJyri Sarha __func__);
72b961c48bSJyri Sarha crtc_state->mode_changed = true;
73b961c48bSJyri Sarha }
74b961c48bSJyri Sarha
75b961c48bSJyri Sarha return 0;
76b961c48bSJyri Sarha }
77b961c48bSJyri Sarha
tilcdc_plane_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)78b961c48bSJyri Sarha static void tilcdc_plane_atomic_update(struct drm_plane *plane,
79977697e2SMaxime Ripard struct drm_atomic_state *state)
80b961c48bSJyri Sarha {
8137418bf1SMaxime Ripard struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
8237418bf1SMaxime Ripard plane);
83b961c48bSJyri Sarha
8441016fe1SMaxime Ripard if (!new_state->crtc)
85b961c48bSJyri Sarha return;
86b961c48bSJyri Sarha
8741016fe1SMaxime Ripard if (WARN_ON(!new_state->fb || !new_state->crtc->state))
88b961c48bSJyri Sarha return;
89b961c48bSJyri Sarha
9041016fe1SMaxime Ripard if (tilcdc_crtc_update_fb(new_state->crtc,
9141016fe1SMaxime Ripard new_state->fb,
9241016fe1SMaxime Ripard new_state->crtc->state->event) == 0) {
9341016fe1SMaxime Ripard new_state->crtc->state->event = NULL;
94cb345decSDaniel Vetter }
95b961c48bSJyri Sarha }
96b961c48bSJyri Sarha
97b961c48bSJyri Sarha static const struct drm_plane_helper_funcs plane_helper_funcs = {
98b961c48bSJyri Sarha .atomic_check = tilcdc_plane_atomic_check,
99b961c48bSJyri Sarha .atomic_update = tilcdc_plane_atomic_update,
100b961c48bSJyri Sarha };
101b961c48bSJyri Sarha
tilcdc_plane_init(struct drm_device * dev,struct drm_plane * plane)102b961c48bSJyri Sarha int tilcdc_plane_init(struct drm_device *dev,
103b961c48bSJyri Sarha struct drm_plane *plane)
104b961c48bSJyri Sarha {
105bcc5a6f5SJyri Sarha struct tilcdc_drm_private *priv = dev->dev_private;
106b961c48bSJyri Sarha int ret;
107b961c48bSJyri Sarha
108*7221941cSThomas Zimmermann ret = drm_universal_plane_init(dev, plane, 1, &tilcdc_plane_funcs,
109bcc5a6f5SJyri Sarha priv->pixelformats,
110bcc5a6f5SJyri Sarha priv->num_pixelformats,
111*7221941cSThomas Zimmermann NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
112b961c48bSJyri Sarha if (ret) {
113b961c48bSJyri Sarha dev_err(dev->dev, "Failed to initialize plane: %d\n", ret);
114b961c48bSJyri Sarha return ret;
115b961c48bSJyri Sarha }
116b961c48bSJyri Sarha
117b961c48bSJyri Sarha drm_plane_helper_add(plane, &plane_helper_funcs);
118b961c48bSJyri Sarha
119b961c48bSJyri Sarha return 0;
120b961c48bSJyri Sarha }
121