1 /*
2  * Copyright (c) 2015 MediaTek Inc.
3  * Author: CK Hu <ck.hu@mediatek.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #include <drm/drmP.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_plane_helper.h>
19 
20 #include "mtk_drm_crtc.h"
21 #include "mtk_drm_ddp_comp.h"
22 #include "mtk_drm_drv.h"
23 #include "mtk_drm_fb.h"
24 #include "mtk_drm_gem.h"
25 #include "mtk_drm_plane.h"
26 
27 static const u32 formats[] = {
28 	DRM_FORMAT_XRGB8888,
29 	DRM_FORMAT_ARGB8888,
30 	DRM_FORMAT_RGB565,
31 	DRM_FORMAT_UYVY,
32 	DRM_FORMAT_YUYV,
33 };
34 
35 static void mtk_plane_reset(struct drm_plane *plane)
36 {
37 	struct mtk_plane_state *state;
38 
39 	if (plane->state) {
40 		__drm_atomic_helper_plane_destroy_state(plane->state);
41 
42 		state = to_mtk_plane_state(plane->state);
43 		memset(state, 0, sizeof(*state));
44 	} else {
45 		state = kzalloc(sizeof(*state), GFP_KERNEL);
46 		if (!state)
47 			return;
48 		plane->state = &state->base;
49 	}
50 
51 	state->base.plane = plane;
52 	state->pending.format = DRM_FORMAT_RGB565;
53 }
54 
55 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
56 {
57 	struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
58 	struct mtk_plane_state *state;
59 
60 	state = kzalloc(sizeof(*state), GFP_KERNEL);
61 	if (!state)
62 		return NULL;
63 
64 	__drm_atomic_helper_plane_duplicate_state(plane, &state->base);
65 
66 	WARN_ON(state->base.plane != plane);
67 
68 	state->pending = old_state->pending;
69 
70 	return &state->base;
71 }
72 
73 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
74 					struct drm_plane_state *state)
75 {
76 	__drm_atomic_helper_plane_destroy_state(state);
77 	kfree(to_mtk_plane_state(state));
78 }
79 
80 static const struct drm_plane_funcs mtk_plane_funcs = {
81 	.update_plane = drm_atomic_helper_update_plane,
82 	.disable_plane = drm_atomic_helper_disable_plane,
83 	.destroy = drm_plane_cleanup,
84 	.reset = mtk_plane_reset,
85 	.atomic_duplicate_state = mtk_plane_duplicate_state,
86 	.atomic_destroy_state = mtk_drm_plane_destroy_state,
87 };
88 
89 static int mtk_plane_atomic_check(struct drm_plane *plane,
90 				  struct drm_plane_state *state)
91 {
92 	struct drm_framebuffer *fb = state->fb;
93 	struct drm_crtc_state *crtc_state;
94 	struct drm_rect clip = { 0, };
95 
96 	if (!fb)
97 		return 0;
98 
99 	if (!mtk_fb_get_gem_obj(fb)) {
100 		DRM_DEBUG_KMS("buffer is null\n");
101 		return -EFAULT;
102 	}
103 
104 	if (!state->crtc)
105 		return 0;
106 
107 	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
108 	if (IS_ERR(crtc_state))
109 		return PTR_ERR(crtc_state);
110 
111 	if (crtc_state->enable)
112 		drm_mode_get_hv_timing(&crtc_state->mode,
113 				       &clip.x2, &clip.y2);
114 
115 	return drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
116 						   DRM_PLANE_HELPER_NO_SCALING,
117 						   DRM_PLANE_HELPER_NO_SCALING,
118 						   true, true);
119 }
120 
121 static void mtk_plane_atomic_update(struct drm_plane *plane,
122 				    struct drm_plane_state *old_state)
123 {
124 	struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
125 	struct drm_crtc *crtc = plane->state->crtc;
126 	struct drm_framebuffer *fb = plane->state->fb;
127 	struct drm_gem_object *gem;
128 	struct mtk_drm_gem_obj *mtk_gem;
129 	unsigned int pitch, format;
130 	dma_addr_t addr;
131 
132 	if (!crtc || WARN_ON(!fb))
133 		return;
134 
135 	gem = mtk_fb_get_gem_obj(fb);
136 	mtk_gem = to_mtk_gem_obj(gem);
137 	addr = mtk_gem->dma_addr;
138 	pitch = fb->pitches[0];
139 	format = fb->format->format;
140 
141 	addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
142 	addr += (plane->state->src.y1 >> 16) * pitch;
143 
144 	state->pending.enable = true;
145 	state->pending.pitch = pitch;
146 	state->pending.format = format;
147 	state->pending.addr = addr;
148 	state->pending.x = plane->state->dst.x1;
149 	state->pending.y = plane->state->dst.y1;
150 	state->pending.width = drm_rect_width(&plane->state->dst);
151 	state->pending.height = drm_rect_height(&plane->state->dst);
152 	wmb(); /* Make sure the above parameters are set before update */
153 	state->pending.dirty = true;
154 }
155 
156 static void mtk_plane_atomic_disable(struct drm_plane *plane,
157 				     struct drm_plane_state *old_state)
158 {
159 	struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
160 
161 	state->pending.enable = false;
162 	wmb(); /* Make sure the above parameter is set before update */
163 	state->pending.dirty = true;
164 }
165 
166 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
167 	.atomic_check = mtk_plane_atomic_check,
168 	.atomic_update = mtk_plane_atomic_update,
169 	.atomic_disable = mtk_plane_atomic_disable,
170 };
171 
172 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
173 		   unsigned long possible_crtcs, enum drm_plane_type type)
174 {
175 	int err;
176 
177 	err = drm_universal_plane_init(dev, plane, possible_crtcs,
178 				       &mtk_plane_funcs, formats,
179 				       ARRAY_SIZE(formats), NULL, type, NULL);
180 	if (err) {
181 		DRM_ERROR("failed to initialize plane\n");
182 		return err;
183 	}
184 
185 	drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
186 
187 	return 0;
188 }
189