blob: d5635d6cbdff9d2d48b96ca7a86c3e8517bff75b [file] [log] [blame]
Jyri Sarhab961c48b2016-04-07 14:52:02 +03001/*
2 * Copyright (C) 2015 Texas Instruments
3 * Author: Jyri Sarha <jsarha@ti.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <drm/drmP.h>
19
20#include <drm/drm_atomic.h>
21#include <drm/drm_plane_helper.h>
22#include <drm/drm_atomic_helper.h>
23#include <uapi/drm/drm_fourcc.h>
24
25#include "tilcdc_drv.h"
26
27static const u32 tilcdc_formats[] = { DRM_FORMAT_RGB565,
28 DRM_FORMAT_RGB888,
29 DRM_FORMAT_XRGB8888 };
30
31static struct drm_plane_funcs tilcdc_plane_funcs = {
32 .update_plane = drm_atomic_helper_update_plane,
33 .disable_plane = drm_atomic_helper_disable_plane,
34 .destroy = drm_plane_cleanup,
35 .set_property = drm_atomic_helper_plane_set_property,
36 .reset = drm_atomic_helper_plane_reset,
37 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
38 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
39};
40
41static int tilcdc_plane_atomic_check(struct drm_plane *plane,
42 struct drm_plane_state *state)
43{
44 struct drm_crtc_state *crtc_state;
45 struct drm_plane_state *old_state = plane->state;
46 unsigned int depth, bpp;
47
48 if (!state->crtc)
49 return 0;
50
51 if (WARN_ON(!state->fb))
52 return -EINVAL;
53
54 if (state->crtc_x || state->crtc_y) {
55 dev_err(plane->dev->dev, "%s: crtc position must be zero.",
56 __func__);
57 return -EINVAL;
58 }
59
60 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
61 state->crtc);
62 /* we should have a crtc state if the plane is attached to a crtc */
63 if (WARN_ON(!crtc_state))
64 return 0;
65
66 if (crtc_state->mode.hdisplay != state->crtc_w ||
67 crtc_state->mode.vdisplay != state->crtc_h) {
68 dev_err(plane->dev->dev,
69 "%s: Size must match mode (%dx%d == %dx%d)", __func__,
70 crtc_state->mode.hdisplay, crtc_state->mode.vdisplay,
71 state->crtc_w, state->crtc_h);
72 return -EINVAL;
73 }
74
75 drm_fb_get_bpp_depth(state->fb->pixel_format, &depth, &bpp);
76 if (state->fb->pitches[0] != crtc_state->mode.hdisplay * bpp / 8) {
77 dev_err(plane->dev->dev,
78 "Invalid pitch: fb and crtc widths must be the same");
79 return -EINVAL;
80 }
81
82 if (state->fb && old_state->fb &&
83 state->fb->pixel_format != old_state->fb->pixel_format) {
84 dev_dbg(plane->dev->dev,
85 "%s(): pixel format change requires mode_change\n",
86 __func__);
87 crtc_state->mode_changed = true;
88 }
89
90 return 0;
91}
92
93static void tilcdc_plane_atomic_update(struct drm_plane *plane,
94 struct drm_plane_state *old_state)
95{
96 struct drm_plane_state *state = plane->state;
97
98 if (!state->crtc)
99 return;
100
101 if (WARN_ON(!state->fb || !state->crtc->state))
102 return;
103
104 tilcdc_crtc_page_flip(state->crtc,
105 state->fb,
106 state->crtc->state->event,
107 0);
108}
109
110static const struct drm_plane_helper_funcs plane_helper_funcs = {
111 .atomic_check = tilcdc_plane_atomic_check,
112 .atomic_update = tilcdc_plane_atomic_update,
113};
114
115int tilcdc_plane_init(struct drm_device *dev,
116 struct drm_plane *plane)
117{
118 int ret;
119
120 ret = drm_plane_init(dev, plane, 1,
121 &tilcdc_plane_funcs,
122 tilcdc_formats,
123 ARRAY_SIZE(tilcdc_formats),
124 true);
125 if (ret) {
126 dev_err(dev->dev, "Failed to initialize plane: %d\n", ret);
127 return ret;
128 }
129
130 drm_plane_helper_add(plane, &plane_helper_funcs);
131
132 return 0;
133}