xref: /linux/drivers/media/platform/amlogic/meson-ge2d/ge2d.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2020 BayLibre, SAS
4  * Author: Neil Armstrong <narmstrong@baylibre.com>
5  */
6 
7 #include <linux/clk.h>
8 #include <linux/delay.h>
9 #include <linux/bitfield.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/reset.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/timer.h>
17 #include <linux/regmap.h>
18 
19 #include <linux/platform_device.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/videobuf2-v4l2.h>
26 #include <media/videobuf2-dma-contig.h>
27 
28 #include "ge2d-regs.h"
29 
30 #define GE2D_NAME	"meson-ge2d"
31 
32 #define DEFAULT_WIDTH	128
33 #define DEFAULT_HEIGHT	128
34 #define DEFAULT_STRIDE	512
35 
36 #define MAX_WIDTH	8191
37 #define MAX_HEIGHT	8191
38 
39 /*
40  * Missing features:
41  * - Scaling
42  * - Simple 1/2 vertical scaling
43  * - YUV input support
44  * - Source global alpha
45  * - Colorspace conversion
46  */
47 
48 struct ge2d_fmt {
49 	u32 fourcc;
50 	bool alpha;
51 	bool le;
52 	unsigned int depth;
53 	unsigned int hw_fmt;
54 	unsigned int hw_map;
55 };
56 
57 struct ge2d_frame {
58 	struct vb2_v4l2_buffer *buf;
59 
60 	/* Image Format */
61 	struct v4l2_pix_format pix_fmt;
62 
63 	/* Crop */
64 	struct v4l2_rect crop;
65 
66 	/* Image format */
67 	const struct ge2d_fmt *fmt;
68 };
69 
70 struct ge2d_ctx {
71 	struct v4l2_fh fh;
72 	struct meson_ge2d *ge2d;
73 	struct ge2d_frame in;
74 	struct ge2d_frame out;
75 	struct v4l2_ctrl_handler ctrl_handler;
76 
77 	unsigned long sequence_out, sequence_cap;
78 
79 	/* Control values */
80 	u32 hflip;
81 	u32 vflip;
82 	u32 xy_swap;
83 };
84 
file_to_ge2d_ctx(struct file * filp)85 static inline struct ge2d_ctx *file_to_ge2d_ctx(struct file *filp)
86 {
87 	return container_of(file_to_v4l2_fh(filp), struct ge2d_ctx, fh);
88 }
89 
90 struct meson_ge2d {
91 	struct v4l2_device v4l2_dev;
92 	struct v4l2_m2m_dev *m2m_dev;
93 	struct video_device *vfd;
94 
95 	struct device *dev;
96 	struct regmap *map;
97 	struct clk *clk;
98 
99 	/* vb2 queue lock */
100 	struct mutex mutex;
101 
102 	struct ge2d_ctx *curr;
103 };
104 
105 #define FMT(_fourcc, _alpha, _depth, _map)		\
106 {							\
107 	.fourcc = _fourcc,				\
108 	.alpha = (_alpha),				\
109 	.depth = (_depth),				\
110 	.hw_fmt = GE2D_FORMAT_ ## _depth ## BIT,	\
111 	.hw_map = GE2D_COLOR_MAP_ ## _map,		\
112 }
113 
114 /* TOFIX Handle the YUV input formats */
115 static const struct ge2d_fmt formats[] = {
116 	/*  FOURCC Alpha  HW FMT  HW MAP */
117 	FMT(V4L2_PIX_FMT_XRGB32, false, 32, BGRA8888),
118 	FMT(V4L2_PIX_FMT_RGB32, true, 32, BGRA8888),
119 	FMT(V4L2_PIX_FMT_ARGB32, true, 32, BGRA8888),
120 	FMT(V4L2_PIX_FMT_RGBX32, false, 32, ABGR8888),
121 	FMT(V4L2_PIX_FMT_RGBA32, true, 32, ABGR8888),
122 	FMT(V4L2_PIX_FMT_BGRX32, false, 32, RGBA8888),
123 	FMT(V4L2_PIX_FMT_BGRA32, true, 32, RGBA8888),
124 	FMT(V4L2_PIX_FMT_BGR32, true, 32, ARGB8888),
125 	FMT(V4L2_PIX_FMT_ABGR32, true, 32, ARGB8888),
126 	FMT(V4L2_PIX_FMT_XBGR32, false, 32, ARGB8888),
127 
128 	FMT(V4L2_PIX_FMT_RGB24, false, 24, BGR888),
129 	FMT(V4L2_PIX_FMT_BGR24, false, 24, RGB888),
130 
131 	FMT(V4L2_PIX_FMT_XRGB555X, false, 16, ARGB1555),
132 	FMT(V4L2_PIX_FMT_ARGB555X, true, 16, ARGB1555),
133 	FMT(V4L2_PIX_FMT_RGB565, false, 16, RGB565),
134 	FMT(V4L2_PIX_FMT_RGBX444, false, 16, RGBA4444),
135 	FMT(V4L2_PIX_FMT_RGBA444, true, 16, RGBA4444),
136 	FMT(V4L2_PIX_FMT_XRGB444, false, 16, ARGB4444),
137 	FMT(V4L2_PIX_FMT_ARGB444, true, 16, ARGB4444),
138 };
139 
140 #define NUM_FORMATS ARRAY_SIZE(formats)
141 
find_fmt(struct v4l2_format * f)142 static const struct ge2d_fmt *find_fmt(struct v4l2_format *f)
143 {
144 	unsigned int i;
145 
146 	for (i = 0; i < NUM_FORMATS; i++) {
147 		if (formats[i].fourcc == f->fmt.pix.pixelformat)
148 			return &formats[i];
149 	}
150 
151 	/*
152 	 * TRY_FMT/S_FMT should never return an error when the requested format
153 	 * is not supported. Drivers should always return a valid format,
154 	 * preferably a format that is as widely supported by applications as
155 	 * possible.
156 	 */
157 	return &formats[0];
158 }
159 
get_frame(struct ge2d_ctx * ctx,enum v4l2_buf_type type)160 static struct ge2d_frame *get_frame(struct ge2d_ctx *ctx,
161 				    enum v4l2_buf_type type)
162 {
163 	switch (type) {
164 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
165 		return &ctx->in;
166 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
167 		return &ctx->out;
168 	default:
169 		/* This should never happen, warn and return OUTPUT frame */
170 		dev_warn(ctx->ge2d->dev, "%s: invalid buffer type\n", __func__);
171 		return &ctx->in;
172 	}
173 }
174 
ge2d_hw_start(struct meson_ge2d * ge2d)175 static void ge2d_hw_start(struct meson_ge2d *ge2d)
176 {
177 	struct ge2d_ctx *ctx = ge2d->curr;
178 	u32 reg;
179 
180 	/* Reset */
181 	regmap_update_bits(ge2d->map, GE2D_GEN_CTRL1,
182 			   GE2D_SOFT_RST, GE2D_SOFT_RST);
183 	regmap_update_bits(ge2d->map, GE2D_GEN_CTRL1,
184 			   GE2D_SOFT_RST, 0);
185 
186 	usleep_range(100, 200);
187 
188 	/* Implement CANVAS for non-AXG */
189 	regmap_write(ge2d->map, GE2D_SRC1_BADDR_CTRL,
190 		     (vb2_dma_contig_plane_dma_addr(&ctx->in.buf->vb2_buf, 0) + 7) >> 3);
191 	regmap_write(ge2d->map, GE2D_SRC1_STRIDE_CTRL,
192 		     (ctx->in.pix_fmt.bytesperline + 7) >> 3);
193 	regmap_write(ge2d->map, GE2D_SRC2_BADDR_CTRL,
194 		     (vb2_dma_contig_plane_dma_addr(&ctx->out.buf->vb2_buf, 0) + 7) >> 3);
195 	regmap_write(ge2d->map, GE2D_SRC2_STRIDE_CTRL,
196 		     (ctx->out.pix_fmt.bytesperline + 7) >> 3);
197 	regmap_write(ge2d->map, GE2D_DST1_BADDR_CTRL,
198 		     (vb2_dma_contig_plane_dma_addr(&ctx->out.buf->vb2_buf, 0) + 7) >> 3);
199 	regmap_write(ge2d->map, GE2D_DST1_STRIDE_CTRL,
200 		     (ctx->out.pix_fmt.bytesperline + 7) >> 3);
201 
202 	regmap_write(ge2d->map, GE2D_GEN_CTRL0, 0);
203 	regmap_write(ge2d->map, GE2D_GEN_CTRL1,
204 		     FIELD_PREP(GE2D_INTERRUPT_CTRL, 2) |
205 		     FIELD_PREP(GE2D_SRC2_BURST_SIZE_CTRL, 3) |
206 		     FIELD_PREP(GE2D_SRC1_BURST_SIZE_CTRL, 0x3f));
207 
208 	regmap_write(ge2d->map, GE2D_GEN_CTRL2,
209 		     GE2D_SRC1_LITTLE_ENDIAN |
210 		     GE2D_SRC2_LITTLE_ENDIAN |
211 		     GE2D_DST_LITTLE_ENDIAN |
212 		     FIELD_PREP(GE2D_DST1_COLOR_MAP, ctx->out.fmt->hw_map) |
213 		     FIELD_PREP(GE2D_DST1_FORMAT, ctx->out.fmt->hw_fmt) |
214 		     FIELD_PREP(GE2D_SRC2_COLOR_MAP, ctx->out.fmt->hw_map) |
215 		     FIELD_PREP(GE2D_SRC2_FORMAT, ctx->out.fmt->hw_fmt) |
216 		     FIELD_PREP(GE2D_SRC1_COLOR_MAP, ctx->in.fmt->hw_map) |
217 		     FIELD_PREP(GE2D_SRC1_FORMAT, ctx->in.fmt->hw_fmt));
218 	regmap_write(ge2d->map, GE2D_GEN_CTRL3,
219 		     GE2D_DST1_ENABLE);
220 
221 	regmap_write(ge2d->map, GE2D_SRC1_CLIPY_START_END,
222 		     FIELD_PREP(GE2D_START, ctx->in.crop.top) |
223 		     FIELD_PREP(GE2D_END, ctx->in.crop.top + ctx->in.crop.height - 1));
224 	regmap_write(ge2d->map, GE2D_SRC1_CLIPX_START_END,
225 		     FIELD_PREP(GE2D_START, ctx->in.crop.left) |
226 		     FIELD_PREP(GE2D_END, ctx->in.crop.left + ctx->in.crop.width - 1));
227 	regmap_write(ge2d->map, GE2D_SRC2_CLIPY_START_END,
228 		     FIELD_PREP(GE2D_START, ctx->out.crop.top) |
229 		     FIELD_PREP(GE2D_END, ctx->out.crop.top + ctx->out.crop.height - 1));
230 	regmap_write(ge2d->map, GE2D_SRC2_CLIPX_START_END,
231 		     FIELD_PREP(GE2D_START, ctx->out.crop.left) |
232 		     FIELD_PREP(GE2D_END, ctx->out.crop.left + ctx->out.crop.width - 1));
233 	regmap_write(ge2d->map, GE2D_DST_CLIPY_START_END,
234 		     FIELD_PREP(GE2D_START, ctx->out.crop.top) |
235 		     FIELD_PREP(GE2D_END, ctx->out.crop.top + ctx->out.crop.height - 1));
236 	regmap_write(ge2d->map, GE2D_DST_CLIPX_START_END,
237 		     FIELD_PREP(GE2D_START, ctx->out.crop.left) |
238 		     FIELD_PREP(GE2D_END, ctx->out.crop.left + ctx->out.crop.width - 1));
239 
240 	regmap_write(ge2d->map, GE2D_SRC1_Y_START_END,
241 		     FIELD_PREP(GE2D_END, ctx->in.pix_fmt.height - 1));
242 	regmap_write(ge2d->map, GE2D_SRC1_X_START_END,
243 		     FIELD_PREP(GE2D_END, ctx->in.pix_fmt.width - 1));
244 	regmap_write(ge2d->map, GE2D_SRC2_Y_START_END,
245 		     FIELD_PREP(GE2D_END, ctx->out.pix_fmt.height - 1));
246 	regmap_write(ge2d->map, GE2D_SRC2_X_START_END,
247 		     FIELD_PREP(GE2D_END, ctx->out.pix_fmt.width - 1));
248 	regmap_write(ge2d->map, GE2D_DST_Y_START_END,
249 		     FIELD_PREP(GE2D_END, ctx->out.pix_fmt.height - 1));
250 	regmap_write(ge2d->map, GE2D_DST_X_START_END,
251 		     FIELD_PREP(GE2D_END, ctx->out.pix_fmt.width - 1));
252 
253 	/* Color, no blend, use source color */
254 	reg = GE2D_ALU_DO_COLOR_OPERATION_LOGIC(LOGIC_OPERATION_COPY,
255 						COLOR_FACTOR_SRC_COLOR);
256 
257 	if (ctx->in.fmt->alpha && ctx->out.fmt->alpha)
258 		/* Take source alpha */
259 		reg |= GE2D_ALU_DO_ALPHA_OPERATION_LOGIC(LOGIC_OPERATION_COPY,
260 							 COLOR_FACTOR_SRC_ALPHA);
261 	else if (!ctx->out.fmt->alpha)
262 		/* Set alpha to 0 */
263 		reg |= GE2D_ALU_DO_ALPHA_OPERATION_LOGIC(LOGIC_OPERATION_SET,
264 							 COLOR_FACTOR_ZERO);
265 	else
266 		/* Keep original alpha */
267 		reg |= GE2D_ALU_DO_ALPHA_OPERATION_LOGIC(LOGIC_OPERATION_COPY,
268 							 COLOR_FACTOR_DST_ALPHA);
269 
270 	regmap_write(ge2d->map, GE2D_ALU_OP_CTRL, reg);
271 
272 	/* Start */
273 	regmap_write(ge2d->map, GE2D_CMD_CTRL,
274 		     (ctx->xy_swap ? GE2D_DST_XY_SWAP : 0) |
275 		     (ctx->hflip ? GE2D_SRC1_Y_REV : 0) |
276 		     (ctx->vflip ? GE2D_SRC1_X_REV : 0) |
277 		     GE2D_CBUS_CMD_WR);
278 }
279 
device_run(void * priv)280 static void device_run(void *priv)
281 {
282 	struct ge2d_ctx *ctx = priv;
283 	struct meson_ge2d *ge2d = ctx->ge2d;
284 
285 	ge2d->curr = ctx;
286 
287 	ctx->in.buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
288 	ctx->out.buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
289 
290 	ge2d_hw_start(ge2d);
291 }
292 
ge2d_isr(int irq,void * priv)293 static irqreturn_t ge2d_isr(int irq, void *priv)
294 {
295 	struct meson_ge2d *ge2d = priv;
296 	u32 intr;
297 
298 	regmap_read(ge2d->map, GE2D_STATUS0, &intr);
299 
300 	if (!(intr & GE2D_GE2D_BUSY)) {
301 		struct vb2_v4l2_buffer *src, *dst;
302 		struct ge2d_ctx *ctx = ge2d->curr;
303 
304 		ge2d->curr = NULL;
305 
306 		src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
307 		dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
308 
309 		src->sequence = ctx->sequence_out++;
310 		dst->sequence = ctx->sequence_cap++;
311 
312 		dst->timecode = src->timecode;
313 		dst->vb2_buf.timestamp = src->vb2_buf.timestamp;
314 		dst->flags = src->flags;
315 
316 		v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
317 		v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
318 		v4l2_m2m_job_finish(ge2d->m2m_dev, ctx->fh.m2m_ctx);
319 	}
320 
321 	return IRQ_HANDLED;
322 }
323 
324 static const struct v4l2_m2m_ops ge2d_m2m_ops = {
325 	.device_run = device_run,
326 };
327 
ge2d_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])328 static int ge2d_queue_setup(struct vb2_queue *vq,
329 			    unsigned int *nbuffers, unsigned int *nplanes,
330 			    unsigned int sizes[], struct device *alloc_devs[])
331 {
332 	struct ge2d_ctx *ctx = vb2_get_drv_priv(vq);
333 	struct ge2d_frame *f = get_frame(ctx, vq->type);
334 
335 	if (*nplanes)
336 		return sizes[0] < f->pix_fmt.sizeimage ? -EINVAL : 0;
337 
338 	sizes[0] = f->pix_fmt.sizeimage;
339 	*nplanes = 1;
340 
341 	return 0;
342 }
343 
ge2d_buf_prepare(struct vb2_buffer * vb)344 static int ge2d_buf_prepare(struct vb2_buffer *vb)
345 {
346 	struct ge2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
347 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
348 	struct ge2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
349 
350 	vbuf->field = V4L2_FIELD_NONE;
351 
352 	vb2_set_plane_payload(vb, 0, f->pix_fmt.sizeimage);
353 
354 	return 0;
355 }
356 
ge2d_buf_queue(struct vb2_buffer * vb)357 static void ge2d_buf_queue(struct vb2_buffer *vb)
358 {
359 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
360 	struct ge2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
361 
362 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
363 }
364 
ge2d_start_streaming(struct vb2_queue * vq,unsigned int count)365 static int ge2d_start_streaming(struct vb2_queue *vq, unsigned int count)
366 {
367 	struct ge2d_ctx *ctx = vb2_get_drv_priv(vq);
368 
369 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
370 		ctx->sequence_out = 0;
371 	else
372 		ctx->sequence_cap = 0;
373 
374 	return 0;
375 }
376 
ge2d_stop_streaming(struct vb2_queue * vq)377 static void ge2d_stop_streaming(struct vb2_queue *vq)
378 {
379 	struct ge2d_ctx *ctx = vb2_get_drv_priv(vq);
380 	struct vb2_v4l2_buffer *vbuf;
381 
382 	for (;;) {
383 		if (V4L2_TYPE_IS_OUTPUT(vq->type))
384 			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
385 		else
386 			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
387 		if (!vbuf)
388 			break;
389 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
390 	}
391 }
392 
393 static const struct vb2_ops ge2d_qops = {
394 	.queue_setup = ge2d_queue_setup,
395 	.buf_prepare = ge2d_buf_prepare,
396 	.buf_queue = ge2d_buf_queue,
397 	.start_streaming = ge2d_start_streaming,
398 	.stop_streaming = ge2d_stop_streaming,
399 };
400 
401 static int
queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)402 queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
403 {
404 	struct ge2d_ctx *ctx = priv;
405 	int ret;
406 
407 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
408 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
409 	src_vq->drv_priv = ctx;
410 	src_vq->ops = &ge2d_qops;
411 	src_vq->mem_ops = &vb2_dma_contig_memops;
412 	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
413 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
414 	src_vq->lock = &ctx->ge2d->mutex;
415 	src_vq->dev = ctx->ge2d->v4l2_dev.dev;
416 
417 	ret = vb2_queue_init(src_vq);
418 	if (ret)
419 		return ret;
420 
421 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
422 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
423 	dst_vq->drv_priv = ctx;
424 	dst_vq->ops = &ge2d_qops;
425 	dst_vq->mem_ops = &vb2_dma_contig_memops;
426 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
427 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
428 	dst_vq->lock = &ctx->ge2d->mutex;
429 	dst_vq->dev = ctx->ge2d->v4l2_dev.dev;
430 
431 	return vb2_queue_init(dst_vq);
432 }
433 
434 static int
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)435 vidioc_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
436 {
437 	strscpy(cap->driver, GE2D_NAME, sizeof(cap->driver));
438 	strscpy(cap->card, GE2D_NAME, sizeof(cap->card));
439 	strscpy(cap->bus_info, "platform:" GE2D_NAME, sizeof(cap->bus_info));
440 
441 	return 0;
442 }
443 
vidioc_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)444 static int vidioc_enum_fmt(struct file *file, void *priv, struct v4l2_fmtdesc *f)
445 {
446 	const struct ge2d_fmt *fmt;
447 
448 	if (f->index >= NUM_FORMATS)
449 		return -EINVAL;
450 
451 	fmt = &formats[f->index];
452 	f->pixelformat = fmt->fourcc;
453 
454 	return 0;
455 }
456 
vidioc_g_selection(struct file * file,void * priv,struct v4l2_selection * s)457 static int vidioc_g_selection(struct file *file, void *priv,
458 			      struct v4l2_selection *s)
459 {
460 	struct ge2d_ctx *ctx = file_to_ge2d_ctx(file);
461 	struct ge2d_frame *f;
462 	bool use_frame = false;
463 
464 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
465 	    s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
466 		return -EINVAL;
467 
468 	f = get_frame(ctx, s->type);
469 
470 	switch (s->target) {
471 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
472 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
473 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
474 			return -EINVAL;
475 		break;
476 	case V4L2_SEL_TGT_CROP_DEFAULT:
477 	case V4L2_SEL_TGT_CROP_BOUNDS:
478 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
479 			return -EINVAL;
480 		break;
481 	case V4L2_SEL_TGT_COMPOSE:
482 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
483 			return -EINVAL;
484 		use_frame = true;
485 		break;
486 	case V4L2_SEL_TGT_CROP:
487 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
488 			return -EINVAL;
489 		use_frame = true;
490 		break;
491 	default:
492 		return -EINVAL;
493 	}
494 
495 	if (use_frame) {
496 		s->r = f->crop;
497 	} else {
498 		s->r.left = 0;
499 		s->r.top = 0;
500 		s->r.width = f->pix_fmt.width;
501 		s->r.height = f->pix_fmt.height;
502 	}
503 
504 	return 0;
505 }
506 
vidioc_s_selection(struct file * file,void * priv,struct v4l2_selection * s)507 static int vidioc_s_selection(struct file *file, void *priv,
508 			      struct v4l2_selection *s)
509 {
510 	struct ge2d_ctx *ctx = file_to_ge2d_ctx(file);
511 	struct meson_ge2d *ge2d = ctx->ge2d;
512 	struct ge2d_frame *f;
513 	int ret = 0;
514 
515 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
516 	    s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
517 		return -EINVAL;
518 
519 	f = get_frame(ctx, s->type);
520 
521 	switch (s->target) {
522 	case V4L2_SEL_TGT_COMPOSE:
523 		/*
524 		 * COMPOSE target is only valid for capture buffer type, return
525 		 * error for output buffer type
526 		 */
527 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
528 			return -EINVAL;
529 		break;
530 	case V4L2_SEL_TGT_CROP:
531 		/*
532 		 * CROP target is only valid for output buffer type, return
533 		 * error for capture buffer type
534 		 */
535 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
536 			return -EINVAL;
537 		break;
538 	/*
539 	 * bound and default crop/compose targets are invalid targets to
540 	 * try/set
541 	 */
542 	default:
543 		return -EINVAL;
544 	}
545 
546 	if (s->r.top < 0 || s->r.left < 0) {
547 		v4l2_err(&ge2d->v4l2_dev,
548 			 "doesn't support negative values for top & left.\n");
549 		return -EINVAL;
550 	}
551 
552 	if (s->r.left + s->r.width > f->pix_fmt.width ||
553 	    s->r.top + s->r.height > f->pix_fmt.height) {
554 		v4l2_err(&ge2d->v4l2_dev, "unsupported rectangle value.\n");
555 		return -EINVAL;
556 	}
557 
558 	f->crop = s->r;
559 
560 	return ret;
561 }
562 
vidioc_setup_cap_fmt(struct ge2d_ctx * ctx,struct v4l2_pix_format * f)563 static void vidioc_setup_cap_fmt(struct ge2d_ctx *ctx, struct v4l2_pix_format *f)
564 {
565 	struct ge2d_frame *frm_out = get_frame(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
566 
567 	*f = frm_out->pix_fmt;
568 
569 	if (ctx->xy_swap) {
570 		f->width = frm_out->pix_fmt.height;
571 		f->height = frm_out->pix_fmt.width;
572 	}
573 }
574 
vidioc_try_fmt_cap(struct file * file,void * priv,struct v4l2_format * f)575 static int vidioc_try_fmt_cap(struct file *file, void *priv, struct v4l2_format *f)
576 {
577 	struct ge2d_ctx *ctx = file_to_ge2d_ctx(file);
578 	const struct ge2d_fmt *fmt = find_fmt(f);
579 	struct v4l2_pix_format fmt_cap;
580 
581 	vidioc_setup_cap_fmt(ctx, &fmt_cap);
582 
583 	fmt_cap.pixelformat = fmt->fourcc;
584 
585 	fmt_cap.bytesperline = max(f->fmt.pix.bytesperline,
586 				   ALIGN((fmt_cap.width * fmt->depth) >> 3, 8));
587 
588 	fmt_cap.sizeimage = max(f->fmt.pix.sizeimage,
589 				fmt_cap.height * fmt_cap.bytesperline);
590 
591 	f->fmt.pix = fmt_cap;
592 
593 	return 0;
594 }
595 
vidioc_s_fmt_cap(struct file * file,void * priv,struct v4l2_format * f)596 static int vidioc_s_fmt_cap(struct file *file, void *priv, struct v4l2_format *f)
597 {
598 	struct ge2d_ctx *ctx = file_to_ge2d_ctx(file);
599 	struct meson_ge2d *ge2d = ctx->ge2d;
600 	struct vb2_queue *vq;
601 	struct ge2d_frame *frm;
602 	int ret = 0;
603 
604 	/* Adjust all values accordingly to the hardware capabilities
605 	 * and chosen format.
606 	 */
607 	ret = vidioc_try_fmt_cap(file, priv, f);
608 	if (ret)
609 		return ret;
610 
611 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
612 	if (vb2_is_busy(vq)) {
613 		v4l2_err(&ge2d->v4l2_dev, "queue (%d) bust\n", f->type);
614 		return -EBUSY;
615 	}
616 
617 	frm = get_frame(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
618 
619 	frm->pix_fmt = f->fmt.pix;
620 	frm->fmt = find_fmt(f);
621 	f->fmt.pix.pixelformat = frm->fmt->fourcc;
622 
623 	/* Reset crop settings */
624 	frm->crop.left = 0;
625 	frm->crop.top = 0;
626 	frm->crop.width = frm->pix_fmt.width;
627 	frm->crop.height = frm->pix_fmt.height;
628 
629 	return 0;
630 }
631 
vidioc_g_fmt(struct file * file,void * priv,struct v4l2_format * f)632 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
633 {
634 	struct ge2d_ctx *ctx = file_to_ge2d_ctx(file);
635 	struct ge2d_frame *frm;
636 
637 	frm = get_frame(ctx, f->type);
638 
639 	f->fmt.pix = frm->pix_fmt;
640 	f->fmt.pix.pixelformat = frm->fmt->fourcc;
641 
642 	return 0;
643 }
644 
vidioc_try_fmt_out(struct file * file,void * priv,struct v4l2_format * f)645 static int vidioc_try_fmt_out(struct file *file, void *priv, struct v4l2_format *f)
646 {
647 	const struct ge2d_fmt *fmt = find_fmt(f);
648 
649 	f->fmt.pix.field = V4L2_FIELD_NONE;
650 	f->fmt.pix.pixelformat = fmt->fourcc;
651 
652 	if (f->fmt.pix.width > MAX_WIDTH)
653 		f->fmt.pix.width = MAX_WIDTH;
654 	if (f->fmt.pix.height > MAX_HEIGHT)
655 		f->fmt.pix.height = MAX_HEIGHT;
656 
657 	f->fmt.pix.bytesperline = max(f->fmt.pix.bytesperline,
658 				      ALIGN((f->fmt.pix.width * fmt->depth) >> 3, 8));
659 
660 	f->fmt.pix.sizeimage = max(f->fmt.pix.sizeimage,
661 				   f->fmt.pix.height * f->fmt.pix.bytesperline);
662 
663 	return 0;
664 }
665 
vidioc_s_fmt_out(struct file * file,void * priv,struct v4l2_format * f)666 static int vidioc_s_fmt_out(struct file *file, void *priv, struct v4l2_format *f)
667 {
668 	struct ge2d_ctx *ctx = file_to_ge2d_ctx(file);
669 	struct meson_ge2d *ge2d = ctx->ge2d;
670 	struct vb2_queue *vq;
671 	struct ge2d_frame *frm, *frm_cap;
672 	int ret = 0;
673 
674 	/* Adjust all values accordingly to the hardware capabilities
675 	 * and chosen format.
676 	 */
677 	ret = vidioc_try_fmt_out(file, priv, f);
678 	if (ret)
679 		return ret;
680 
681 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
682 	if (vb2_is_busy(vq)) {
683 		v4l2_err(&ge2d->v4l2_dev, "queue (%d) bust\n", f->type);
684 		return -EBUSY;
685 	}
686 
687 	frm = get_frame(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
688 	frm_cap = get_frame(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
689 
690 	frm->pix_fmt = f->fmt.pix;
691 	frm->fmt = find_fmt(f);
692 	f->fmt.pix.pixelformat = frm->fmt->fourcc;
693 
694 	/* Reset crop settings */
695 	frm->crop.left = 0;
696 	frm->crop.top = 0;
697 	frm->crop.width = frm->pix_fmt.width;
698 	frm->crop.height = frm->pix_fmt.height;
699 
700 	/* Propagate settings to capture */
701 	vidioc_setup_cap_fmt(ctx, &frm_cap->pix_fmt);
702 
703 	return 0;
704 }
705 
706 static const struct v4l2_ioctl_ops ge2d_ioctl_ops = {
707 	.vidioc_querycap = vidioc_querycap,
708 
709 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
710 	.vidioc_g_fmt_vid_cap = vidioc_g_fmt,
711 	.vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
712 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
713 
714 	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
715 	.vidioc_g_fmt_vid_out = vidioc_g_fmt,
716 	.vidioc_try_fmt_vid_out = vidioc_try_fmt_out,
717 	.vidioc_s_fmt_vid_out = vidioc_s_fmt_out,
718 
719 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
720 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
721 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
722 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
723 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
724 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
725 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
726 
727 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
728 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
729 
730 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
731 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
732 
733 	.vidioc_g_selection = vidioc_g_selection,
734 	.vidioc_s_selection = vidioc_s_selection,
735 };
736 
ge2d_s_ctrl(struct v4l2_ctrl * ctrl)737 static int ge2d_s_ctrl(struct v4l2_ctrl *ctrl)
738 {
739 	struct ge2d_ctx *ctx = container_of(ctrl->handler, struct ge2d_ctx,
740 					   ctrl_handler);
741 	struct v4l2_pix_format fmt;
742 	struct vb2_queue *vq;
743 
744 	switch (ctrl->id) {
745 	case V4L2_CID_HFLIP:
746 		ctx->hflip = ctrl->val;
747 		break;
748 	case V4L2_CID_VFLIP:
749 		ctx->vflip = ctrl->val;
750 		break;
751 	case V4L2_CID_ROTATE:
752 		vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
753 		if (vb2_is_busy(vq))
754 			return -EBUSY;
755 
756 		if (ctrl->val == 90) {
757 			ctx->hflip = 0;
758 			ctx->vflip = 1;
759 			ctx->xy_swap = 1;
760 		} else if (ctrl->val == 180) {
761 			ctx->hflip = 1;
762 			ctx->vflip = 1;
763 			ctx->xy_swap = 0;
764 		} else if (ctrl->val == 270) {
765 			ctx->hflip = 1;
766 			ctx->vflip = 0;
767 			ctx->xy_swap = 1;
768 		} else {
769 			ctx->hflip = 0;
770 			ctx->vflip = 0;
771 			ctx->xy_swap = 0;
772 		}
773 
774 		vidioc_setup_cap_fmt(ctx, &fmt);
775 
776 		/*
777 		 * If the rotation parameter changes the OUTPUT frames
778 		 * parameters, take them in account
779 		 */
780 		ctx->out.pix_fmt = fmt;
781 
782 		break;
783 	}
784 
785 	return 0;
786 }
787 
788 static const struct v4l2_ctrl_ops ge2d_ctrl_ops = {
789 	.s_ctrl = ge2d_s_ctrl,
790 };
791 
ge2d_setup_ctrls(struct ge2d_ctx * ctx)792 static int ge2d_setup_ctrls(struct ge2d_ctx *ctx)
793 {
794 	struct meson_ge2d *ge2d = ctx->ge2d;
795 
796 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, 4);
797 
798 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &ge2d_ctrl_ops,
799 			  V4L2_CID_HFLIP, 0, 1, 1, 0);
800 
801 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &ge2d_ctrl_ops,
802 			  V4L2_CID_VFLIP, 0, 1, 1, 0);
803 
804 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &ge2d_ctrl_ops,
805 			  V4L2_CID_ROTATE, 0, 270, 90, 0);
806 
807 	if (ctx->ctrl_handler.error) {
808 		int err = ctx->ctrl_handler.error;
809 
810 		v4l2_err(&ge2d->v4l2_dev, "%s failed\n", __func__);
811 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
812 		return err;
813 	}
814 
815 	return 0;
816 }
817 
818 static const struct ge2d_frame def_frame = {
819 	.pix_fmt = {
820 		.width = DEFAULT_WIDTH,
821 		.height = DEFAULT_HEIGHT,
822 		.bytesperline = DEFAULT_STRIDE,
823 		.sizeimage = DEFAULT_STRIDE * DEFAULT_HEIGHT,
824 		.field = V4L2_FIELD_NONE,
825 	},
826 	.crop.width = DEFAULT_WIDTH,
827 	.crop.height = DEFAULT_HEIGHT,
828 	.fmt = &formats[0],
829 };
830 
ge2d_open(struct file * file)831 static int ge2d_open(struct file *file)
832 {
833 	struct meson_ge2d *ge2d = video_drvdata(file);
834 	struct ge2d_ctx *ctx = NULL;
835 	int ret = 0;
836 
837 	ctx = kzalloc_obj(*ctx);
838 	if (!ctx)
839 		return -ENOMEM;
840 	ctx->ge2d = ge2d;
841 
842 	/* Set default formats */
843 	ctx->in = def_frame;
844 	ctx->out = def_frame;
845 
846 	if (mutex_lock_interruptible(&ge2d->mutex)) {
847 		kfree(ctx);
848 		return -ERESTARTSYS;
849 	}
850 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(ge2d->m2m_dev, ctx, &queue_init);
851 	if (IS_ERR(ctx->fh.m2m_ctx)) {
852 		ret = PTR_ERR(ctx->fh.m2m_ctx);
853 		mutex_unlock(&ge2d->mutex);
854 		kfree(ctx);
855 		return ret;
856 	}
857 	v4l2_fh_init(&ctx->fh, video_devdata(file));
858 	v4l2_fh_add(&ctx->fh, file);
859 
860 	ge2d_setup_ctrls(ctx);
861 
862 	/* Write the default values to the ctx struct */
863 	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
864 
865 	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
866 	mutex_unlock(&ge2d->mutex);
867 
868 	return 0;
869 }
870 
ge2d_release(struct file * file)871 static int ge2d_release(struct file *file)
872 {
873 	struct ge2d_ctx *ctx = file_to_ge2d_ctx(file);
874 	struct meson_ge2d *ge2d = ctx->ge2d;
875 
876 	mutex_lock(&ge2d->mutex);
877 
878 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
879 
880 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
881 	v4l2_fh_del(&ctx->fh, file);
882 	v4l2_fh_exit(&ctx->fh);
883 	kfree(ctx);
884 
885 	mutex_unlock(&ge2d->mutex);
886 
887 	return 0;
888 }
889 
890 static const struct v4l2_file_operations ge2d_fops = {
891 	.owner = THIS_MODULE,
892 	.open = ge2d_open,
893 	.release = ge2d_release,
894 	.poll = v4l2_m2m_fop_poll,
895 	.unlocked_ioctl = video_ioctl2,
896 	.mmap = v4l2_m2m_fop_mmap,
897 };
898 
899 static const struct video_device ge2d_videodev = {
900 	.name = "meson-ge2d",
901 	.fops = &ge2d_fops,
902 	.ioctl_ops = &ge2d_ioctl_ops,
903 	.minor = -1,
904 	.release = video_device_release,
905 	.vfl_dir = VFL_DIR_M2M,
906 	.device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
907 };
908 
909 static const struct regmap_config meson_ge2d_regmap_conf = {
910 	.reg_bits = 8,
911 	.val_bits = 32,
912 	.reg_stride = 4,
913 	.max_register = GE2D_SRC2_STRIDE_CTRL,
914 };
915 
ge2d_probe(struct platform_device * pdev)916 static int ge2d_probe(struct platform_device *pdev)
917 {
918 	struct reset_control *rst;
919 	struct video_device *vfd;
920 	struct meson_ge2d *ge2d;
921 	void __iomem *regs;
922 	int ret = 0;
923 	int irq;
924 
925 	if (!pdev->dev.of_node)
926 		return -ENODEV;
927 
928 	ge2d = devm_kzalloc(&pdev->dev, sizeof(*ge2d), GFP_KERNEL);
929 	if (!ge2d)
930 		return -ENOMEM;
931 
932 	ge2d->dev = &pdev->dev;
933 	mutex_init(&ge2d->mutex);
934 
935 	regs = devm_platform_ioremap_resource(pdev, 0);
936 	if (IS_ERR(regs))
937 		return PTR_ERR(regs);
938 
939 	ge2d->map = devm_regmap_init_mmio(ge2d->dev, regs,
940 					  &meson_ge2d_regmap_conf);
941 	if (IS_ERR(ge2d->map))
942 		return PTR_ERR(ge2d->map);
943 
944 	irq = platform_get_irq(pdev, 0);
945 	ret = devm_request_irq(ge2d->dev, irq, ge2d_isr, 0,
946 			       dev_name(ge2d->dev), ge2d);
947 	if (ret < 0) {
948 		dev_err(ge2d->dev, "failed to request irq\n");
949 		return ret;
950 	}
951 
952 	rst = devm_reset_control_get(ge2d->dev, NULL);
953 	if (IS_ERR(rst)) {
954 		dev_err(ge2d->dev, "failed to get core reset controller\n");
955 		return PTR_ERR(rst);
956 	}
957 
958 	ge2d->clk = devm_clk_get(ge2d->dev, NULL);
959 	if (IS_ERR(ge2d->clk)) {
960 		dev_err(ge2d->dev, "failed to get clock\n");
961 		return PTR_ERR(ge2d->clk);
962 	}
963 
964 	reset_control_assert(rst);
965 	udelay(1);
966 	reset_control_deassert(rst);
967 
968 	ret = clk_prepare_enable(ge2d->clk);
969 	if (ret) {
970 		dev_err(ge2d->dev, "Cannot enable ge2d sclk: %d\n", ret);
971 		return ret;
972 	}
973 
974 	ret = v4l2_device_register(&pdev->dev, &ge2d->v4l2_dev);
975 	if (ret)
976 		goto disable_clks;
977 
978 	vfd = video_device_alloc();
979 	if (!vfd) {
980 		v4l2_err(&ge2d->v4l2_dev, "Failed to allocate video device\n");
981 		ret = -ENOMEM;
982 		goto unreg_v4l2_dev;
983 	}
984 
985 	*vfd = ge2d_videodev;
986 	vfd->lock = &ge2d->mutex;
987 	vfd->v4l2_dev = &ge2d->v4l2_dev;
988 
989 	video_set_drvdata(vfd, ge2d);
990 	ge2d->vfd = vfd;
991 
992 	platform_set_drvdata(pdev, ge2d);
993 	ge2d->m2m_dev = v4l2_m2m_init(&ge2d_m2m_ops);
994 	if (IS_ERR(ge2d->m2m_dev)) {
995 		v4l2_err(&ge2d->v4l2_dev, "Failed to init mem2mem device\n");
996 		ret = PTR_ERR(ge2d->m2m_dev);
997 		goto rel_vdev;
998 	}
999 
1000 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
1001 	if (ret) {
1002 		v4l2_err(&ge2d->v4l2_dev, "Failed to register video device\n");
1003 		goto rel_m2m;
1004 	}
1005 
1006 	v4l2_info(&ge2d->v4l2_dev, "Registered %s as /dev/%s\n",
1007 		  vfd->name, video_device_node_name(vfd));
1008 
1009 	return 0;
1010 
1011 rel_m2m:
1012 	v4l2_m2m_release(ge2d->m2m_dev);
1013 rel_vdev:
1014 	video_device_release(ge2d->vfd);
1015 unreg_v4l2_dev:
1016 	v4l2_device_unregister(&ge2d->v4l2_dev);
1017 disable_clks:
1018 	clk_disable_unprepare(ge2d->clk);
1019 
1020 	return ret;
1021 }
1022 
ge2d_remove(struct platform_device * pdev)1023 static void ge2d_remove(struct platform_device *pdev)
1024 {
1025 	struct meson_ge2d *ge2d = platform_get_drvdata(pdev);
1026 
1027 	video_unregister_device(ge2d->vfd);
1028 	v4l2_m2m_release(ge2d->m2m_dev);
1029 	v4l2_device_unregister(&ge2d->v4l2_dev);
1030 	clk_disable_unprepare(ge2d->clk);
1031 }
1032 
1033 static const struct of_device_id meson_ge2d_match[] = {
1034 	{
1035 		.compatible = "amlogic,axg-ge2d",
1036 	},
1037 	{},
1038 };
1039 
1040 MODULE_DEVICE_TABLE(of, meson_ge2d_match);
1041 
1042 static struct platform_driver ge2d_drv = {
1043 	.probe = ge2d_probe,
1044 	.remove = ge2d_remove,
1045 	.driver = {
1046 		.name = "meson-ge2d",
1047 		.of_match_table = meson_ge2d_match,
1048 	},
1049 };
1050 
1051 module_platform_driver(ge2d_drv);
1052 
1053 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
1054 MODULE_DESCRIPTION("Amlogic 2D Graphic Acceleration Unit");
1055 MODULE_LICENSE("GPL");
1056