xref: /linux/drivers/media/platform/samsung/s5p-g2d/g2d.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Samsung S5P G2D - 2D Graphics Accelerator Driver
4  *
5  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
6  * Kamil Debski, <k.debski@samsung.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/fs.h>
11 #include <linux/timer.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/clk.h>
15 #include <linux/interrupt.h>
16 #include <linux/of.h>
17 
18 #include <linux/platform_device.h>
19 #include <media/v4l2-mem2mem.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-v4l2.h>
23 #include <media/videobuf2-dma-contig.h>
24 
25 #include "g2d.h"
26 #include "g2d-regs.h"
27 
28 static inline struct g2d_ctx *file2ctx(struct file *filp)
29 {
30 	return container_of(file_to_v4l2_fh(filp), struct g2d_ctx, fh);
31 }
32 
33 static struct g2d_fmt formats[] = {
34 	{
35 		.fourcc	= V4L2_PIX_FMT_RGB32,
36 		.depth	= 32,
37 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
38 	},
39 	{
40 		.fourcc	= V4L2_PIX_FMT_RGB565X,
41 		.depth	= 16,
42 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
43 	},
44 	{
45 		.fourcc	= V4L2_PIX_FMT_RGB555X,
46 		.depth	= 16,
47 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
48 	},
49 	{
50 		.fourcc	= V4L2_PIX_FMT_RGB444,
51 		.depth	= 16,
52 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
53 	},
54 	{
55 		.fourcc	= V4L2_PIX_FMT_RGB24,
56 		.depth	= 24,
57 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
58 	},
59 };
60 #define NUM_FORMATS ARRAY_SIZE(formats)
61 
62 static struct g2d_frame def_frame = {
63 	.width		= DEFAULT_WIDTH,
64 	.height		= DEFAULT_HEIGHT,
65 	.c_width	= DEFAULT_WIDTH,
66 	.c_height	= DEFAULT_HEIGHT,
67 	.o_width	= 0,
68 	.o_height	= 0,
69 	.fmt		= &formats[0],
70 	.right		= DEFAULT_WIDTH,
71 	.bottom		= DEFAULT_HEIGHT,
72 };
73 
74 static struct g2d_fmt *find_fmt(struct v4l2_format *f)
75 {
76 	unsigned int i;
77 	for (i = 0; i < NUM_FORMATS; i++) {
78 		if (formats[i].fourcc == f->fmt.pix.pixelformat)
79 			return &formats[i];
80 	}
81 	return NULL;
82 }
83 
84 
85 static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
86 				   enum v4l2_buf_type type)
87 {
88 	switch (type) {
89 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
90 		return &ctx->in;
91 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
92 		return &ctx->out;
93 	default:
94 		return ERR_PTR(-EINVAL);
95 	}
96 }
97 
98 static int g2d_queue_setup(struct vb2_queue *vq,
99 			   unsigned int *nbuffers, unsigned int *nplanes,
100 			   unsigned int sizes[], struct device *alloc_devs[])
101 {
102 	struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
103 	struct g2d_frame *f = get_frame(ctx, vq->type);
104 
105 	if (IS_ERR(f))
106 		return PTR_ERR(f);
107 
108 	sizes[0] = f->size;
109 	*nplanes = 1;
110 
111 	if (*nbuffers == 0)
112 		*nbuffers = 1;
113 
114 	return 0;
115 }
116 
117 static int g2d_buf_prepare(struct vb2_buffer *vb)
118 {
119 	struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
120 	struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
121 
122 	if (IS_ERR(f))
123 		return PTR_ERR(f);
124 	vb2_set_plane_payload(vb, 0, f->size);
125 	return 0;
126 }
127 
128 static void g2d_buf_queue(struct vb2_buffer *vb)
129 {
130 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
131 	struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
132 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
133 }
134 
135 static const struct vb2_ops g2d_qops = {
136 	.queue_setup	= g2d_queue_setup,
137 	.buf_prepare	= g2d_buf_prepare,
138 	.buf_queue	= g2d_buf_queue,
139 };
140 
141 static int queue_init(void *priv, struct vb2_queue *src_vq,
142 						struct vb2_queue *dst_vq)
143 {
144 	struct g2d_ctx *ctx = priv;
145 	int ret;
146 
147 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
148 	src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
149 	src_vq->drv_priv = ctx;
150 	src_vq->ops = &g2d_qops;
151 	src_vq->mem_ops = &vb2_dma_contig_memops;
152 	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
153 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
154 	src_vq->lock = &ctx->dev->mutex;
155 	src_vq->dev = ctx->dev->v4l2_dev.dev;
156 
157 	ret = vb2_queue_init(src_vq);
158 	if (ret)
159 		return ret;
160 
161 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
162 	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
163 	dst_vq->drv_priv = ctx;
164 	dst_vq->ops = &g2d_qops;
165 	dst_vq->mem_ops = &vb2_dma_contig_memops;
166 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
167 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
168 	dst_vq->lock = &ctx->dev->mutex;
169 	dst_vq->dev = ctx->dev->v4l2_dev.dev;
170 
171 	return vb2_queue_init(dst_vq);
172 }
173 
174 static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
175 {
176 	struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
177 								ctrl_handler);
178 	unsigned long flags;
179 
180 	spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
181 	switch (ctrl->id) {
182 	case V4L2_CID_COLORFX:
183 		if (ctrl->val == V4L2_COLORFX_NEGATIVE)
184 			ctx->rop = ROP4_INVERT;
185 		else
186 			ctx->rop = ROP4_COPY;
187 		break;
188 
189 	case V4L2_CID_HFLIP:
190 		ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
191 		break;
192 
193 	}
194 	spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
195 	return 0;
196 }
197 
198 static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
199 	.s_ctrl		= g2d_s_ctrl,
200 };
201 
202 static int g2d_setup_ctrls(struct g2d_ctx *ctx)
203 {
204 	struct g2d_dev *dev = ctx->dev;
205 
206 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
207 
208 	ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
209 						V4L2_CID_HFLIP, 0, 1, 1, 0);
210 
211 	ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
212 						V4L2_CID_VFLIP, 0, 1, 1, 0);
213 
214 	v4l2_ctrl_new_std_menu(
215 		&ctx->ctrl_handler,
216 		&g2d_ctrl_ops,
217 		V4L2_CID_COLORFX,
218 		V4L2_COLORFX_NEGATIVE,
219 		~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
220 		V4L2_COLORFX_NONE);
221 
222 	if (ctx->ctrl_handler.error) {
223 		int err = ctx->ctrl_handler.error;
224 		v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
225 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
226 		return err;
227 	}
228 
229 	v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
230 
231 	return 0;
232 }
233 
234 static int g2d_open(struct file *file)
235 {
236 	struct g2d_dev *dev = video_drvdata(file);
237 	struct g2d_ctx *ctx = NULL;
238 	int ret = 0;
239 
240 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
241 	if (!ctx)
242 		return -ENOMEM;
243 	ctx->dev = dev;
244 	/* Set default formats */
245 	ctx->in		= def_frame;
246 	ctx->out	= def_frame;
247 
248 	if (mutex_lock_interruptible(&dev->mutex)) {
249 		kfree(ctx);
250 		return -ERESTARTSYS;
251 	}
252 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
253 	if (IS_ERR(ctx->fh.m2m_ctx)) {
254 		ret = PTR_ERR(ctx->fh.m2m_ctx);
255 		mutex_unlock(&dev->mutex);
256 		kfree(ctx);
257 		return ret;
258 	}
259 	v4l2_fh_init(&ctx->fh, video_devdata(file));
260 	v4l2_fh_add(&ctx->fh, file);
261 
262 	g2d_setup_ctrls(ctx);
263 
264 	/* Write the default values to the ctx struct */
265 	v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
266 
267 	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
268 	mutex_unlock(&dev->mutex);
269 
270 	v4l2_info(&dev->v4l2_dev, "instance opened\n");
271 	return 0;
272 }
273 
274 static int g2d_release(struct file *file)
275 {
276 	struct g2d_dev *dev = video_drvdata(file);
277 	struct g2d_ctx *ctx = file2ctx(file);
278 
279 	mutex_lock(&dev->mutex);
280 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
281 	mutex_unlock(&dev->mutex);
282 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
283 	v4l2_fh_del(&ctx->fh, file);
284 	v4l2_fh_exit(&ctx->fh);
285 	kfree(ctx);
286 	v4l2_info(&dev->v4l2_dev, "instance closed\n");
287 	return 0;
288 }
289 
290 
291 static int vidioc_querycap(struct file *file, void *priv,
292 				struct v4l2_capability *cap)
293 {
294 	strscpy(cap->driver, G2D_NAME, sizeof(cap->driver));
295 	strscpy(cap->card, G2D_NAME, sizeof(cap->card));
296 	cap->bus_info[0] = 0;
297 	return 0;
298 }
299 
300 static int vidioc_enum_fmt(struct file *file, void *priv, struct v4l2_fmtdesc *f)
301 {
302 	if (f->index >= NUM_FORMATS)
303 		return -EINVAL;
304 	f->pixelformat = formats[f->index].fourcc;
305 	return 0;
306 }
307 
308 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
309 {
310 	struct g2d_ctx *ctx = file2ctx(file);
311 	struct vb2_queue *vq;
312 	struct g2d_frame *frm;
313 
314 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
315 	if (!vq)
316 		return -EINVAL;
317 	frm = get_frame(ctx, f->type);
318 	if (IS_ERR(frm))
319 		return PTR_ERR(frm);
320 
321 	f->fmt.pix.width		= frm->width;
322 	f->fmt.pix.height		= frm->height;
323 	f->fmt.pix.field		= V4L2_FIELD_NONE;
324 	f->fmt.pix.pixelformat		= frm->fmt->fourcc;
325 	f->fmt.pix.bytesperline		= (frm->width * frm->fmt->depth) >> 3;
326 	f->fmt.pix.sizeimage		= frm->size;
327 	return 0;
328 }
329 
330 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
331 {
332 	struct g2d_fmt *fmt;
333 	enum v4l2_field *field;
334 
335 	fmt = find_fmt(f);
336 	if (!fmt)
337 		return -EINVAL;
338 
339 	field = &f->fmt.pix.field;
340 	if (*field == V4L2_FIELD_ANY)
341 		*field = V4L2_FIELD_NONE;
342 	else if (*field != V4L2_FIELD_NONE)
343 		return -EINVAL;
344 
345 	if (f->fmt.pix.width > MAX_WIDTH)
346 		f->fmt.pix.width = MAX_WIDTH;
347 	if (f->fmt.pix.height > MAX_HEIGHT)
348 		f->fmt.pix.height = MAX_HEIGHT;
349 
350 	if (f->fmt.pix.width < 1)
351 		f->fmt.pix.width = 1;
352 	if (f->fmt.pix.height < 1)
353 		f->fmt.pix.height = 1;
354 
355 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
356 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
357 	return 0;
358 }
359 
360 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
361 {
362 	struct g2d_ctx *ctx = file2ctx(file);
363 	struct g2d_dev *dev = ctx->dev;
364 	struct vb2_queue *vq;
365 	struct g2d_frame *frm;
366 	struct g2d_fmt *fmt;
367 	int ret = 0;
368 
369 	/* Adjust all values accordingly to the hardware capabilities
370 	 * and chosen format. */
371 	ret = vidioc_try_fmt(file, priv, f);
372 	if (ret)
373 		return ret;
374 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
375 	if (vb2_is_busy(vq)) {
376 		v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
377 		return -EBUSY;
378 	}
379 	frm = get_frame(ctx, f->type);
380 	if (IS_ERR(frm))
381 		return PTR_ERR(frm);
382 	fmt = find_fmt(f);
383 	if (!fmt)
384 		return -EINVAL;
385 	frm->width	= f->fmt.pix.width;
386 	frm->height	= f->fmt.pix.height;
387 	frm->size	= f->fmt.pix.sizeimage;
388 	/* Reset crop settings */
389 	frm->o_width	= 0;
390 	frm->o_height	= 0;
391 	frm->c_width	= frm->width;
392 	frm->c_height	= frm->height;
393 	frm->right	= frm->width;
394 	frm->bottom	= frm->height;
395 	frm->fmt	= fmt;
396 	frm->stride	= f->fmt.pix.bytesperline;
397 	return 0;
398 }
399 
400 static int vidioc_g_selection(struct file *file, void *priv,
401 			      struct v4l2_selection *s)
402 {
403 	struct g2d_ctx *ctx = file2ctx(file);
404 	struct g2d_frame *f;
405 
406 	f = get_frame(ctx, s->type);
407 	if (IS_ERR(f))
408 		return PTR_ERR(f);
409 
410 	switch (s->target) {
411 	case V4L2_SEL_TGT_CROP:
412 	case V4L2_SEL_TGT_CROP_DEFAULT:
413 	case V4L2_SEL_TGT_CROP_BOUNDS:
414 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
415 			return -EINVAL;
416 		break;
417 	case V4L2_SEL_TGT_COMPOSE:
418 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
419 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
420 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
421 			return -EINVAL;
422 		break;
423 	default:
424 		return -EINVAL;
425 	}
426 
427 	switch (s->target) {
428 	case V4L2_SEL_TGT_CROP:
429 	case V4L2_SEL_TGT_COMPOSE:
430 		s->r.left = f->o_height;
431 		s->r.top = f->o_width;
432 		s->r.width = f->c_width;
433 		s->r.height = f->c_height;
434 		break;
435 	case V4L2_SEL_TGT_CROP_DEFAULT:
436 	case V4L2_SEL_TGT_CROP_BOUNDS:
437 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
438 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
439 		s->r.left = 0;
440 		s->r.top = 0;
441 		s->r.width = f->width;
442 		s->r.height = f->height;
443 		break;
444 	default:
445 		return -EINVAL;
446 	}
447 	return 0;
448 }
449 
450 static int vidioc_try_selection(struct file *file, void *priv,
451 				const struct v4l2_selection *s)
452 {
453 	struct g2d_ctx *ctx = file2ctx(file);
454 	struct g2d_dev *dev = ctx->dev;
455 	struct g2d_frame *f;
456 
457 	f = get_frame(ctx, s->type);
458 	if (IS_ERR(f))
459 		return PTR_ERR(f);
460 
461 	if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
462 		if (s->target != V4L2_SEL_TGT_COMPOSE)
463 			return -EINVAL;
464 	} else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
465 		if (s->target != V4L2_SEL_TGT_CROP)
466 			return -EINVAL;
467 	}
468 
469 	if (s->r.top < 0 || s->r.left < 0) {
470 		v4l2_err(&dev->v4l2_dev,
471 			"doesn't support negative values for top & left\n");
472 		return -EINVAL;
473 	}
474 
475 	return 0;
476 }
477 
478 static int vidioc_s_selection(struct file *file, void *priv,
479 			      struct v4l2_selection *s)
480 {
481 	struct g2d_ctx *ctx = file2ctx(file);
482 	struct g2d_frame *f;
483 	int ret;
484 
485 	ret = vidioc_try_selection(file, priv, s);
486 	if (ret)
487 		return ret;
488 	f = get_frame(ctx, s->type);
489 	if (IS_ERR(f))
490 		return PTR_ERR(f);
491 
492 	f->c_width	= s->r.width;
493 	f->c_height	= s->r.height;
494 	f->o_width	= s->r.left;
495 	f->o_height	= s->r.top;
496 	f->bottom	= f->o_height + f->c_height;
497 	f->right	= f->o_width + f->c_width;
498 	return 0;
499 }
500 
501 static void device_run(void *prv)
502 {
503 	struct g2d_ctx *ctx = prv;
504 	struct g2d_dev *dev = ctx->dev;
505 	struct vb2_v4l2_buffer *src, *dst;
506 	unsigned long flags;
507 	u32 cmd = 0;
508 
509 	dev->curr = ctx;
510 
511 	src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
512 	dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
513 
514 	clk_enable(dev->gate);
515 	g2d_reset(dev);
516 
517 	spin_lock_irqsave(&dev->ctrl_lock, flags);
518 
519 	g2d_set_src_size(dev, &ctx->in);
520 	g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
521 
522 	g2d_set_dst_size(dev, &ctx->out);
523 	g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
524 
525 	g2d_set_rop4(dev, ctx->rop);
526 	g2d_set_flip(dev, ctx->flip);
527 
528 	if (ctx->in.c_width != ctx->out.c_width ||
529 		ctx->in.c_height != ctx->out.c_height) {
530 		if (dev->variant->hw_rev == TYPE_G2D_3X)
531 			cmd |= CMD_V3_ENABLE_STRETCH;
532 		else
533 			g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
534 	}
535 
536 	g2d_set_cmd(dev, cmd);
537 	g2d_start(dev);
538 
539 	spin_unlock_irqrestore(&dev->ctrl_lock, flags);
540 }
541 
542 static irqreturn_t g2d_isr(int irq, void *prv)
543 {
544 	struct g2d_dev *dev = prv;
545 	struct g2d_ctx *ctx = dev->curr;
546 	struct vb2_v4l2_buffer *src, *dst;
547 
548 	g2d_clear_int(dev);
549 	clk_disable(dev->gate);
550 
551 	BUG_ON(ctx == NULL);
552 
553 	src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
554 	dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
555 
556 	BUG_ON(src == NULL);
557 	BUG_ON(dst == NULL);
558 
559 	dst->timecode = src->timecode;
560 	dst->vb2_buf.timestamp = src->vb2_buf.timestamp;
561 	dst->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
562 	dst->flags |=
563 		src->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
564 
565 	v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
566 	v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
567 	v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
568 
569 	dev->curr = NULL;
570 	return IRQ_HANDLED;
571 }
572 
573 static const struct v4l2_file_operations g2d_fops = {
574 	.owner		= THIS_MODULE,
575 	.open		= g2d_open,
576 	.release	= g2d_release,
577 	.poll		= v4l2_m2m_fop_poll,
578 	.unlocked_ioctl	= video_ioctl2,
579 	.mmap		= v4l2_m2m_fop_mmap,
580 };
581 
582 static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
583 	.vidioc_querycap	= vidioc_querycap,
584 
585 	.vidioc_enum_fmt_vid_cap	= vidioc_enum_fmt,
586 	.vidioc_g_fmt_vid_cap		= vidioc_g_fmt,
587 	.vidioc_try_fmt_vid_cap		= vidioc_try_fmt,
588 	.vidioc_s_fmt_vid_cap		= vidioc_s_fmt,
589 
590 	.vidioc_enum_fmt_vid_out	= vidioc_enum_fmt,
591 	.vidioc_g_fmt_vid_out		= vidioc_g_fmt,
592 	.vidioc_try_fmt_vid_out		= vidioc_try_fmt,
593 	.vidioc_s_fmt_vid_out		= vidioc_s_fmt,
594 
595 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
596 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
597 	.vidioc_qbuf			= v4l2_m2m_ioctl_qbuf,
598 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
599 
600 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
601 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
602 
603 	.vidioc_g_selection		= vidioc_g_selection,
604 	.vidioc_s_selection		= vidioc_s_selection,
605 };
606 
607 static const struct video_device g2d_videodev = {
608 	.name		= G2D_NAME,
609 	.fops		= &g2d_fops,
610 	.ioctl_ops	= &g2d_ioctl_ops,
611 	.minor		= -1,
612 	.release	= video_device_release,
613 	.vfl_dir	= VFL_DIR_M2M,
614 };
615 
616 static const struct v4l2_m2m_ops g2d_m2m_ops = {
617 	.device_run	= device_run,
618 };
619 
620 static const struct of_device_id exynos_g2d_match[];
621 
622 static int g2d_probe(struct platform_device *pdev)
623 {
624 	struct g2d_dev *dev;
625 	struct video_device *vfd;
626 	const struct of_device_id *of_id;
627 	int ret = 0;
628 
629 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
630 	if (!dev)
631 		return -ENOMEM;
632 
633 	spin_lock_init(&dev->ctrl_lock);
634 	mutex_init(&dev->mutex);
635 	atomic_set(&dev->num_inst, 0);
636 
637 	dev->regs = devm_platform_ioremap_resource(pdev, 0);
638 	if (IS_ERR(dev->regs))
639 		return PTR_ERR(dev->regs);
640 
641 	dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
642 	if (IS_ERR(dev->clk)) {
643 		dev_err(&pdev->dev, "failed to get g2d clock\n");
644 		return -ENXIO;
645 	}
646 
647 	ret = clk_prepare(dev->clk);
648 	if (ret) {
649 		dev_err(&pdev->dev, "failed to prepare g2d clock\n");
650 		goto put_clk;
651 	}
652 
653 	dev->gate = clk_get(&pdev->dev, "fimg2d");
654 	if (IS_ERR(dev->gate)) {
655 		dev_err(&pdev->dev, "failed to get g2d clock gate\n");
656 		ret = -ENXIO;
657 		goto unprep_clk;
658 	}
659 
660 	ret = clk_prepare(dev->gate);
661 	if (ret) {
662 		dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
663 		goto put_clk_gate;
664 	}
665 
666 	ret = platform_get_irq(pdev, 0);
667 	if (ret < 0)
668 		goto unprep_clk_gate;
669 
670 	dev->irq = ret;
671 
672 	ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
673 						0, pdev->name, dev);
674 	if (ret) {
675 		dev_err(&pdev->dev, "failed to install IRQ\n");
676 		goto unprep_clk_gate;
677 	}
678 
679 	vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
680 
681 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
682 	if (ret)
683 		goto unprep_clk_gate;
684 	vfd = video_device_alloc();
685 	if (!vfd) {
686 		v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
687 		ret = -ENOMEM;
688 		goto unreg_v4l2_dev;
689 	}
690 	*vfd = g2d_videodev;
691 	set_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags);
692 	vfd->lock = &dev->mutex;
693 	vfd->v4l2_dev = &dev->v4l2_dev;
694 	vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
695 
696 	platform_set_drvdata(pdev, dev);
697 	dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
698 	if (IS_ERR(dev->m2m_dev)) {
699 		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
700 		ret = PTR_ERR(dev->m2m_dev);
701 		goto rel_vdev;
702 	}
703 
704 	def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
705 
706 	of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
707 	if (!of_id) {
708 		ret = -ENODEV;
709 		goto free_m2m;
710 	}
711 	dev->variant = (struct g2d_variant *)of_id->data;
712 
713 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
714 	if (ret) {
715 		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
716 		goto free_m2m;
717 	}
718 	video_set_drvdata(vfd, dev);
719 	dev->vfd = vfd;
720 	v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
721 		  vfd->num);
722 
723 	return 0;
724 
725 free_m2m:
726 	v4l2_m2m_release(dev->m2m_dev);
727 rel_vdev:
728 	video_device_release(vfd);
729 unreg_v4l2_dev:
730 	v4l2_device_unregister(&dev->v4l2_dev);
731 unprep_clk_gate:
732 	clk_unprepare(dev->gate);
733 put_clk_gate:
734 	clk_put(dev->gate);
735 unprep_clk:
736 	clk_unprepare(dev->clk);
737 put_clk:
738 	clk_put(dev->clk);
739 
740 	return ret;
741 }
742 
743 static void g2d_remove(struct platform_device *pdev)
744 {
745 	struct g2d_dev *dev = platform_get_drvdata(pdev);
746 
747 	v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
748 	v4l2_m2m_release(dev->m2m_dev);
749 	video_unregister_device(dev->vfd);
750 	v4l2_device_unregister(&dev->v4l2_dev);
751 	vb2_dma_contig_clear_max_seg_size(&pdev->dev);
752 	clk_unprepare(dev->gate);
753 	clk_put(dev->gate);
754 	clk_unprepare(dev->clk);
755 	clk_put(dev->clk);
756 }
757 
758 static struct g2d_variant g2d_drvdata_v3x = {
759 	.hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
760 };
761 
762 static struct g2d_variant g2d_drvdata_v4x = {
763 	.hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
764 };
765 
766 static const struct of_device_id exynos_g2d_match[] = {
767 	{
768 		.compatible = "samsung,s5pv210-g2d",
769 		.data = &g2d_drvdata_v3x,
770 	}, {
771 		.compatible = "samsung,exynos4212-g2d",
772 		.data = &g2d_drvdata_v4x,
773 	},
774 	{},
775 };
776 MODULE_DEVICE_TABLE(of, exynos_g2d_match);
777 
778 static struct platform_driver g2d_pdrv = {
779 	.probe		= g2d_probe,
780 	.remove		= g2d_remove,
781 	.driver		= {
782 		.name = G2D_NAME,
783 		.of_match_table = exynos_g2d_match,
784 	},
785 };
786 
787 module_platform_driver(g2d_pdrv);
788 
789 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
790 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
791 MODULE_LICENSE("GPL");
792