xref: /linux/drivers/media/platform/samsung/s5p-g2d/g2d.c (revision b83deaa741558babf4b8d51d34f6637ccfff1b26)
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 #define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
29 
30 static struct g2d_fmt formats[] = {
31 	{
32 		.fourcc	= V4L2_PIX_FMT_RGB32,
33 		.depth	= 32,
34 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
35 	},
36 	{
37 		.fourcc	= V4L2_PIX_FMT_RGB565X,
38 		.depth	= 16,
39 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
40 	},
41 	{
42 		.fourcc	= V4L2_PIX_FMT_RGB555X,
43 		.depth	= 16,
44 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
45 	},
46 	{
47 		.fourcc	= V4L2_PIX_FMT_RGB444,
48 		.depth	= 16,
49 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
50 	},
51 	{
52 		.fourcc	= V4L2_PIX_FMT_RGB24,
53 		.depth	= 24,
54 		.hw	= COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
55 	},
56 };
57 #define NUM_FORMATS ARRAY_SIZE(formats)
58 
59 static struct g2d_frame def_frame = {
60 	.width		= DEFAULT_WIDTH,
61 	.height		= DEFAULT_HEIGHT,
62 	.c_width	= DEFAULT_WIDTH,
63 	.c_height	= DEFAULT_HEIGHT,
64 	.o_width	= 0,
65 	.o_height	= 0,
66 	.fmt		= &formats[0],
67 	.right		= DEFAULT_WIDTH,
68 	.bottom		= DEFAULT_HEIGHT,
69 };
70 
71 static struct g2d_fmt *find_fmt(struct v4l2_format *f)
72 {
73 	unsigned int i;
74 	for (i = 0; i < NUM_FORMATS; i++) {
75 		if (formats[i].fourcc == f->fmt.pix.pixelformat)
76 			return &formats[i];
77 	}
78 	return NULL;
79 }
80 
81 
82 static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
83 				   enum v4l2_buf_type type)
84 {
85 	switch (type) {
86 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
87 		return &ctx->in;
88 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
89 		return &ctx->out;
90 	default:
91 		return ERR_PTR(-EINVAL);
92 	}
93 }
94 
95 static int g2d_queue_setup(struct vb2_queue *vq,
96 			   unsigned int *nbuffers, unsigned int *nplanes,
97 			   unsigned int sizes[], struct device *alloc_devs[])
98 {
99 	struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
100 	struct g2d_frame *f = get_frame(ctx, vq->type);
101 
102 	if (IS_ERR(f))
103 		return PTR_ERR(f);
104 
105 	sizes[0] = f->size;
106 	*nplanes = 1;
107 
108 	if (*nbuffers == 0)
109 		*nbuffers = 1;
110 
111 	return 0;
112 }
113 
114 static int g2d_buf_prepare(struct vb2_buffer *vb)
115 {
116 	struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
117 	struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
118 
119 	if (IS_ERR(f))
120 		return PTR_ERR(f);
121 	vb2_set_plane_payload(vb, 0, f->size);
122 	return 0;
123 }
124 
125 static void g2d_buf_queue(struct vb2_buffer *vb)
126 {
127 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
128 	struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
129 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
130 }
131 
132 static const struct vb2_ops g2d_qops = {
133 	.queue_setup	= g2d_queue_setup,
134 	.buf_prepare	= g2d_buf_prepare,
135 	.buf_queue	= g2d_buf_queue,
136 	.wait_prepare	= vb2_ops_wait_prepare,
137 	.wait_finish	= vb2_ops_wait_finish,
138 };
139 
140 static int queue_init(void *priv, struct vb2_queue *src_vq,
141 						struct vb2_queue *dst_vq)
142 {
143 	struct g2d_ctx *ctx = priv;
144 	int ret;
145 
146 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
147 	src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
148 	src_vq->drv_priv = ctx;
149 	src_vq->ops = &g2d_qops;
150 	src_vq->mem_ops = &vb2_dma_contig_memops;
151 	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
152 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
153 	src_vq->lock = &ctx->dev->mutex;
154 	src_vq->dev = ctx->dev->v4l2_dev.dev;
155 
156 	ret = vb2_queue_init(src_vq);
157 	if (ret)
158 		return ret;
159 
160 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
161 	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
162 	dst_vq->drv_priv = ctx;
163 	dst_vq->ops = &g2d_qops;
164 	dst_vq->mem_ops = &vb2_dma_contig_memops;
165 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
166 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
167 	dst_vq->lock = &ctx->dev->mutex;
168 	dst_vq->dev = ctx->dev->v4l2_dev.dev;
169 
170 	return vb2_queue_init(dst_vq);
171 }
172 
173 static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
174 {
175 	struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
176 								ctrl_handler);
177 	unsigned long flags;
178 
179 	spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
180 	switch (ctrl->id) {
181 	case V4L2_CID_COLORFX:
182 		if (ctrl->val == V4L2_COLORFX_NEGATIVE)
183 			ctx->rop = ROP4_INVERT;
184 		else
185 			ctx->rop = ROP4_COPY;
186 		break;
187 
188 	case V4L2_CID_HFLIP:
189 		ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
190 		break;
191 
192 	}
193 	spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
194 	return 0;
195 }
196 
197 static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
198 	.s_ctrl		= g2d_s_ctrl,
199 };
200 
201 static int g2d_setup_ctrls(struct g2d_ctx *ctx)
202 {
203 	struct g2d_dev *dev = ctx->dev;
204 
205 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
206 
207 	ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
208 						V4L2_CID_HFLIP, 0, 1, 1, 0);
209 
210 	ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
211 						V4L2_CID_VFLIP, 0, 1, 1, 0);
212 
213 	v4l2_ctrl_new_std_menu(
214 		&ctx->ctrl_handler,
215 		&g2d_ctrl_ops,
216 		V4L2_CID_COLORFX,
217 		V4L2_COLORFX_NEGATIVE,
218 		~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
219 		V4L2_COLORFX_NONE);
220 
221 	if (ctx->ctrl_handler.error) {
222 		int err = ctx->ctrl_handler.error;
223 		v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
224 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
225 		return err;
226 	}
227 
228 	v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
229 
230 	return 0;
231 }
232 
233 static int g2d_open(struct file *file)
234 {
235 	struct g2d_dev *dev = video_drvdata(file);
236 	struct g2d_ctx *ctx = NULL;
237 	int ret = 0;
238 
239 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
240 	if (!ctx)
241 		return -ENOMEM;
242 	ctx->dev = dev;
243 	/* Set default formats */
244 	ctx->in		= def_frame;
245 	ctx->out	= def_frame;
246 
247 	if (mutex_lock_interruptible(&dev->mutex)) {
248 		kfree(ctx);
249 		return -ERESTARTSYS;
250 	}
251 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
252 	if (IS_ERR(ctx->fh.m2m_ctx)) {
253 		ret = PTR_ERR(ctx->fh.m2m_ctx);
254 		mutex_unlock(&dev->mutex);
255 		kfree(ctx);
256 		return ret;
257 	}
258 	v4l2_fh_init(&ctx->fh, video_devdata(file));
259 	file->private_data = &ctx->fh;
260 	v4l2_fh_add(&ctx->fh);
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 = fh2ctx(file->private_data);
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);
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 *prv, 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 *prv, struct v4l2_format *f)
309 {
310 	struct g2d_ctx *ctx = prv;
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 *prv, 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 *prv, struct v4l2_format *f)
361 {
362 	struct g2d_ctx *ctx = prv;
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, prv, 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 *prv,
401 			      struct v4l2_selection *s)
402 {
403 	struct g2d_ctx *ctx = prv;
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 *prv,
451 				const struct v4l2_selection *s)
452 {
453 	struct g2d_ctx *ctx = prv;
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 *prv,
479 			      struct v4l2_selection *s)
480 {
481 	struct g2d_ctx *ctx = prv;
482 	struct g2d_frame *f;
483 	int ret;
484 
485 	ret = vidioc_try_selection(file, prv, 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 int 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 	return 0;
757 }
758 
759 static struct g2d_variant g2d_drvdata_v3x = {
760 	.hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
761 };
762 
763 static struct g2d_variant g2d_drvdata_v4x = {
764 	.hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
765 };
766 
767 static const struct of_device_id exynos_g2d_match[] = {
768 	{
769 		.compatible = "samsung,s5pv210-g2d",
770 		.data = &g2d_drvdata_v3x,
771 	}, {
772 		.compatible = "samsung,exynos4212-g2d",
773 		.data = &g2d_drvdata_v4x,
774 	},
775 	{},
776 };
777 MODULE_DEVICE_TABLE(of, exynos_g2d_match);
778 
779 static struct platform_driver g2d_pdrv = {
780 	.probe		= g2d_probe,
781 	.remove		= g2d_remove,
782 	.driver		= {
783 		.name = G2D_NAME,
784 		.of_match_table = exynos_g2d_match,
785 	},
786 };
787 
788 module_platform_driver(g2d_pdrv);
789 
790 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
791 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
792 MODULE_LICENSE("GPL");
793