summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/renesas/shmobile/shmob_drm_plane.c
blob: 8f9a728affde8375fb71633426072cb0f17c0d6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// SPDX-License-Identifier: GPL-2.0+
/*
 * shmob_drm_plane.c  --  SH Mobile DRM Planes
 *
 * Copyright (C) 2012 Renesas Electronics Corporation
 *
 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
 */

#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_crtc.h>
#include <drm/drm_fb_dma_helper.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_gem_dma_helper.h>
#include <drm/drm_plane_helper.h>

#include "shmob_drm_drv.h"
#include "shmob_drm_kms.h"
#include "shmob_drm_plane.h"
#include "shmob_drm_regs.h"

struct shmob_drm_plane {
	struct drm_plane base;
	unsigned int index;
};

struct shmob_drm_plane_state {
	struct drm_plane_state base;

	const struct shmob_drm_format_info *format;
	u32 dma[2];
};

static inline struct shmob_drm_plane *to_shmob_plane(struct drm_plane *plane)
{
	return container_of(plane, struct shmob_drm_plane, base);
}

static inline struct shmob_drm_plane_state *to_shmob_plane_state(struct drm_plane_state *state)
{
	return container_of(state, struct shmob_drm_plane_state, base);
}

static void shmob_drm_plane_compute_base(struct shmob_drm_plane_state *sstate)
{
	struct drm_framebuffer *fb = sstate->base.fb;
	unsigned int x = sstate->base.src_x >> 16;
	unsigned int y = sstate->base.src_y >> 16;
	struct drm_gem_dma_object *gem;
	unsigned int bpp;

	bpp = shmob_drm_format_is_yuv(sstate->format) ? 8 : sstate->format->bpp;
	gem = drm_fb_dma_get_gem_obj(fb, 0);
	sstate->dma[0] = gem->dma_addr + fb->offsets[0]
		       + y * fb->pitches[0] + x * bpp / 8;

	if (shmob_drm_format_is_yuv(sstate->format)) {
		bpp = sstate->format->bpp - 8;
		gem = drm_fb_dma_get_gem_obj(fb, 1);
		sstate->dma[1] = gem->dma_addr + fb->offsets[1]
			       + y / (bpp == 4 ? 2 : 1) * fb->pitches[1]
			       + x * (bpp == 16 ? 2 : 1);
	}
}

static void shmob_drm_primary_plane_setup(struct shmob_drm_plane *splane,
					  struct drm_plane_state *state)
{
	struct shmob_drm_plane_state *sstate = to_shmob_plane_state(state);
	struct shmob_drm_device *sdev = to_shmob_device(splane->base.dev);
	struct drm_framebuffer *fb = state->fb;

	/* TODO: Handle YUV colorspaces. Hardcode REC709 for now. */
	lcdc_write(sdev, LDDFR, sstate->format->lddfr | LDDFR_CF1);
	lcdc_write(sdev, LDMLSR, fb->pitches[0]);

	/* Word and long word swap. */
	lcdc_write(sdev, LDDDSR, sstate->format->ldddsr);

	lcdc_write_mirror(sdev, LDSA1R, sstate->dma[0]);
	if (shmob_drm_format_is_yuv(sstate->format))
		lcdc_write_mirror(sdev, LDSA2R, sstate->dma[1]);

	lcdc_write(sdev, LDRCNTR, lcdc_read(sdev, LDRCNTR) ^ LDRCNTR_MRS);
}

static void shmob_drm_overlay_plane_setup(struct shmob_drm_plane *splane,
					  struct drm_plane_state *state)
{
	struct shmob_drm_plane_state *sstate = to_shmob_plane_state(state);
	struct shmob_drm_device *sdev = to_shmob_device(splane->base.dev);
	struct drm_framebuffer *fb = state->fb;
	u32 format;

	/* TODO: Support ROP3 mode */
	format = LDBBSIFR_EN | ((state->alpha >> 8) << LDBBSIFR_LAY_SHIFT) |
		 sstate->format->ldbbsifr;

#define plane_reg_dump(sdev, splane, reg) \
	dev_dbg(sdev->ddev.dev, "%s(%u): %s 0x%08x 0x%08x\n", __func__, \
		splane->index, #reg, \
		lcdc_read(sdev, reg(splane->index)), \
		lcdc_read(sdev, reg(splane->index) + LCDC_SIDE_B_OFFSET))

	plane_reg_dump(sdev, splane, LDBnBSIFR);
	plane_reg_dump(sdev, splane, LDBnBSSZR);
	plane_reg_dump(sdev, splane, LDBnBLOCR);
	plane_reg_dump(sdev, splane, LDBnBSMWR);
	plane_reg_dump(sdev, splane, LDBnBSAYR);
	plane_reg_dump(sdev, splane, LDBnBSACR);

	lcdc_write(sdev, LDBCR, LDBCR_UPC(splane->index));
	dev_dbg(sdev->ddev.dev, "%s(%u): %s 0x%08x\n", __func__, splane->index,
		"LDBCR", lcdc_read(sdev, LDBCR));

	lcdc_write(sdev, LDBnBSIFR(splane->index), format);

	lcdc_write(sdev, LDBnBSSZR(splane->index),
		   (state->crtc_h << LDBBSSZR_BVSS_SHIFT) |
		   (state->crtc_w << LDBBSSZR_BHSS_SHIFT));
	lcdc_write(sdev, LDBnBLOCR(splane->index),
		   (state->crtc_y << LDBBLOCR_CVLC_SHIFT) |
		   (state->crtc_x << LDBBLOCR_CHLC_SHIFT));
	lcdc_write(sdev, LDBnBSMWR(splane->index),
		   fb->pitches[0] << LDBBSMWR_BSMW_SHIFT);

	lcdc_write(sdev, LDBnBSAYR(splane->index), sstate->dma[0]);
	if (shmob_drm_format_is_yuv(sstate->format))
		lcdc_write(sdev, LDBnBSACR(splane->index), sstate->dma[1]);

	lcdc_write(sdev, LDBCR,
		   LDBCR_UPF(splane->index) | LDBCR_UPD(splane->index));
	dev_dbg(sdev->ddev.dev, "%s(%u): %s 0x%08x\n", __func__, splane->index,
		"LDBCR", lcdc_read(sdev, LDBCR));

	plane_reg_dump(sdev, splane, LDBnBSIFR);
	plane_reg_dump(sdev, splane, LDBnBSSZR);
	plane_reg_dump(sdev, splane, LDBnBLOCR);
	plane_reg_dump(sdev, splane, LDBnBSMWR);
	plane_reg_dump(sdev, splane, LDBnBSAYR);
	plane_reg_dump(sdev, splane, LDBnBSACR);
}

static int shmob_drm_plane_atomic_check(struct drm_plane *plane,
					struct drm_atomic_state *state)
{
	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
	struct shmob_drm_plane_state *sstate = to_shmob_plane_state(new_plane_state);
	struct drm_crtc_state *crtc_state;
	bool is_primary = plane->type == DRM_PLANE_TYPE_PRIMARY;
	int ret;

	if (!new_plane_state->crtc) {
		/*
		 * The visible field is not reset by the DRM core but only
		 * updated by drm_atomic_helper_check_plane_state(), set it
		 * manually.
		 */
		new_plane_state->visible = false;
		sstate->format = NULL;
		return 0;
	}

	crtc_state = drm_atomic_get_crtc_state(state, new_plane_state->crtc);
	if (IS_ERR(crtc_state))
		return PTR_ERR(crtc_state);

	ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
						  DRM_PLANE_NO_SCALING,
						  DRM_PLANE_NO_SCALING,
						  !is_primary, true);
	if (ret < 0)
		return ret;

	if (!new_plane_state->visible) {
		sstate->format = NULL;
		return 0;
	}

	sstate->format = shmob_drm_format_info(new_plane_state->fb->format->format);
	if (!sstate->format) {
		dev_dbg(plane->dev->dev,
			"plane_atomic_check: unsupported format %p4cc\n",
			&new_plane_state->fb->format->format);
		return -EINVAL;
	}

	shmob_drm_plane_compute_base(sstate);

	return 0;
}

static void shmob_drm_plane_atomic_update(struct drm_plane *plane,
					  struct drm_atomic_state *state)
{
	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
	struct shmob_drm_plane *splane = to_shmob_plane(plane);

	if (!new_plane_state->visible)
		return;

	if (plane->type == DRM_PLANE_TYPE_PRIMARY)
		shmob_drm_primary_plane_setup(splane, new_plane_state);
	else
		shmob_drm_overlay_plane_setup(splane, new_plane_state);
}

static void shmob_drm_plane_atomic_disable(struct drm_plane *plane,
					   struct drm_atomic_state *state)
{
	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
	struct shmob_drm_device *sdev = to_shmob_device(plane->dev);
	struct shmob_drm_plane *splane = to_shmob_plane(plane);

	if (!old_state->crtc)
		return;

	if (plane->type != DRM_PLANE_TYPE_OVERLAY)
		return;

	lcdc_write(sdev, LDBCR, LDBCR_UPC(splane->index));
	lcdc_write(sdev, LDBnBSIFR(splane->index), 0);
	lcdc_write(sdev, LDBCR,
			 LDBCR_UPF(splane->index) | LDBCR_UPD(splane->index));
}

static struct drm_plane_state *
shmob_drm_plane_atomic_duplicate_state(struct drm_plane *plane)
{
	struct shmob_drm_plane_state *state;
	struct shmob_drm_plane_state *copy;

	if (WARN_ON(!plane->state))
		return NULL;

	state = to_shmob_plane_state(plane->state);
	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
	if (copy == NULL)
		return NULL;

	__drm_atomic_helper_plane_duplicate_state(plane, &copy->base);

	return &copy->base;
}

static void shmob_drm_plane_atomic_destroy_state(struct drm_plane *plane,
						 struct drm_plane_state *state)
{
	__drm_atomic_helper_plane_destroy_state(state);
	kfree(to_shmob_plane_state(state));
}

static void shmob_drm_plane_reset(struct drm_plane *plane)
{
	struct shmob_drm_plane_state *state;

	if (plane->state) {
		shmob_drm_plane_atomic_destroy_state(plane, plane->state);
		plane->state = NULL;
	}

	state = kzalloc(sizeof(*state), GFP_KERNEL);
	if (state == NULL)
		return;

	__drm_atomic_helper_plane_reset(plane, &state->base);
}

static const struct drm_plane_helper_funcs shmob_drm_plane_helper_funcs = {
	.atomic_check = shmob_drm_plane_atomic_check,
	.atomic_update = shmob_drm_plane_atomic_update,
	.atomic_disable = shmob_drm_plane_atomic_disable,
};

static const struct drm_plane_funcs shmob_drm_plane_funcs = {
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
	.reset = shmob_drm_plane_reset,
	.atomic_duplicate_state = shmob_drm_plane_atomic_duplicate_state,
	.atomic_destroy_state = shmob_drm_plane_atomic_destroy_state,
};

static const uint32_t formats[] = {
	DRM_FORMAT_RGB565,
	DRM_FORMAT_RGB888,
	DRM_FORMAT_ARGB8888,
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_NV12,
	DRM_FORMAT_NV21,
	DRM_FORMAT_NV16,
	DRM_FORMAT_NV61,
	DRM_FORMAT_NV24,
	DRM_FORMAT_NV42,
};

struct drm_plane *shmob_drm_plane_create(struct shmob_drm_device *sdev,
					 enum drm_plane_type type,
					 unsigned int index)
{
	struct shmob_drm_plane *splane;

	splane = drmm_universal_plane_alloc(&sdev->ddev,
					    struct shmob_drm_plane, base, 1,
					    &shmob_drm_plane_funcs, formats,
					    ARRAY_SIZE(formats),  NULL, type,
					    NULL);
	if (IS_ERR(splane))
		return ERR_CAST(splane);

	splane->index = index;

	drm_plane_helper_add(&splane->base, &shmob_drm_plane_helper_funcs);

	return &splane->base;
}