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 
find_fmt(struct v4l2_format * f)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 
get_frame(struct g2d_ctx * ctx,enum v4l2_buf_type type)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 
g2d_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])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 
g2d_buf_prepare(struct vb2_buffer * vb)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 
g2d_buf_queue(struct vb2_buffer * vb)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 
queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)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 
g2d_s_ctrl(struct v4l2_ctrl * ctrl)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 
g2d_setup_ctrls(struct g2d_ctx * ctx)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 
g2d_open(struct file * file)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 
g2d_release(struct file * file)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 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
280 	v4l2_fh_del(&ctx->fh);
281 	v4l2_fh_exit(&ctx->fh);
282 	kfree(ctx);
283 	v4l2_info(&dev->v4l2_dev, "instance closed\n");
284 	return 0;
285 }
286 
287 
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)288 static int vidioc_querycap(struct file *file, void *priv,
289 				struct v4l2_capability *cap)
290 {
291 	strscpy(cap->driver, G2D_NAME, sizeof(cap->driver));
292 	strscpy(cap->card, G2D_NAME, sizeof(cap->card));
293 	cap->bus_info[0] = 0;
294 	return 0;
295 }
296 
vidioc_enum_fmt(struct file * file,void * prv,struct v4l2_fmtdesc * f)297 static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
298 {
299 	if (f->index >= NUM_FORMATS)
300 		return -EINVAL;
301 	f->pixelformat = formats[f->index].fourcc;
302 	return 0;
303 }
304 
vidioc_g_fmt(struct file * file,void * prv,struct v4l2_format * f)305 static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
306 {
307 	struct g2d_ctx *ctx = prv;
308 	struct vb2_queue *vq;
309 	struct g2d_frame *frm;
310 
311 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
312 	if (!vq)
313 		return -EINVAL;
314 	frm = get_frame(ctx, f->type);
315 	if (IS_ERR(frm))
316 		return PTR_ERR(frm);
317 
318 	f->fmt.pix.width		= frm->width;
319 	f->fmt.pix.height		= frm->height;
320 	f->fmt.pix.field		= V4L2_FIELD_NONE;
321 	f->fmt.pix.pixelformat		= frm->fmt->fourcc;
322 	f->fmt.pix.bytesperline		= (frm->width * frm->fmt->depth) >> 3;
323 	f->fmt.pix.sizeimage		= frm->size;
324 	return 0;
325 }
326 
vidioc_try_fmt(struct file * file,void * prv,struct v4l2_format * f)327 static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
328 {
329 	struct g2d_fmt *fmt;
330 	enum v4l2_field *field;
331 
332 	fmt = find_fmt(f);
333 	if (!fmt)
334 		return -EINVAL;
335 
336 	field = &f->fmt.pix.field;
337 	if (*field == V4L2_FIELD_ANY)
338 		*field = V4L2_FIELD_NONE;
339 	else if (*field != V4L2_FIELD_NONE)
340 		return -EINVAL;
341 
342 	if (f->fmt.pix.width > MAX_WIDTH)
343 		f->fmt.pix.width = MAX_WIDTH;
344 	if (f->fmt.pix.height > MAX_HEIGHT)
345 		f->fmt.pix.height = MAX_HEIGHT;
346 
347 	if (f->fmt.pix.width < 1)
348 		f->fmt.pix.width = 1;
349 	if (f->fmt.pix.height < 1)
350 		f->fmt.pix.height = 1;
351 
352 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
353 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
354 	return 0;
355 }
356 
vidioc_s_fmt(struct file * file,void * prv,struct v4l2_format * f)357 static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
358 {
359 	struct g2d_ctx *ctx = prv;
360 	struct g2d_dev *dev = ctx->dev;
361 	struct vb2_queue *vq;
362 	struct g2d_frame *frm;
363 	struct g2d_fmt *fmt;
364 	int ret = 0;
365 
366 	/* Adjust all values accordingly to the hardware capabilities
367 	 * and chosen format. */
368 	ret = vidioc_try_fmt(file, prv, f);
369 	if (ret)
370 		return ret;
371 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
372 	if (vb2_is_busy(vq)) {
373 		v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
374 		return -EBUSY;
375 	}
376 	frm = get_frame(ctx, f->type);
377 	if (IS_ERR(frm))
378 		return PTR_ERR(frm);
379 	fmt = find_fmt(f);
380 	if (!fmt)
381 		return -EINVAL;
382 	frm->width	= f->fmt.pix.width;
383 	frm->height	= f->fmt.pix.height;
384 	frm->size	= f->fmt.pix.sizeimage;
385 	/* Reset crop settings */
386 	frm->o_width	= 0;
387 	frm->o_height	= 0;
388 	frm->c_width	= frm->width;
389 	frm->c_height	= frm->height;
390 	frm->right	= frm->width;
391 	frm->bottom	= frm->height;
392 	frm->fmt	= fmt;
393 	frm->stride	= f->fmt.pix.bytesperline;
394 	return 0;
395 }
396 
vidioc_g_selection(struct file * file,void * prv,struct v4l2_selection * s)397 static int vidioc_g_selection(struct file *file, void *prv,
398 			      struct v4l2_selection *s)
399 {
400 	struct g2d_ctx *ctx = prv;
401 	struct g2d_frame *f;
402 
403 	f = get_frame(ctx, s->type);
404 	if (IS_ERR(f))
405 		return PTR_ERR(f);
406 
407 	switch (s->target) {
408 	case V4L2_SEL_TGT_CROP:
409 	case V4L2_SEL_TGT_CROP_DEFAULT:
410 	case V4L2_SEL_TGT_CROP_BOUNDS:
411 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
412 			return -EINVAL;
413 		break;
414 	case V4L2_SEL_TGT_COMPOSE:
415 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
416 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
417 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
418 			return -EINVAL;
419 		break;
420 	default:
421 		return -EINVAL;
422 	}
423 
424 	switch (s->target) {
425 	case V4L2_SEL_TGT_CROP:
426 	case V4L2_SEL_TGT_COMPOSE:
427 		s->r.left = f->o_height;
428 		s->r.top = f->o_width;
429 		s->r.width = f->c_width;
430 		s->r.height = f->c_height;
431 		break;
432 	case V4L2_SEL_TGT_CROP_DEFAULT:
433 	case V4L2_SEL_TGT_CROP_BOUNDS:
434 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
435 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
436 		s->r.left = 0;
437 		s->r.top = 0;
438 		s->r.width = f->width;
439 		s->r.height = f->height;
440 		break;
441 	default:
442 		return -EINVAL;
443 	}
444 	return 0;
445 }
446 
vidioc_try_selection(struct file * file,void * prv,const struct v4l2_selection * s)447 static int vidioc_try_selection(struct file *file, void *prv,
448 				const struct v4l2_selection *s)
449 {
450 	struct g2d_ctx *ctx = prv;
451 	struct g2d_dev *dev = ctx->dev;
452 	struct g2d_frame *f;
453 
454 	f = get_frame(ctx, s->type);
455 	if (IS_ERR(f))
456 		return PTR_ERR(f);
457 
458 	if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
459 		if (s->target != V4L2_SEL_TGT_COMPOSE)
460 			return -EINVAL;
461 	} else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
462 		if (s->target != V4L2_SEL_TGT_CROP)
463 			return -EINVAL;
464 	}
465 
466 	if (s->r.top < 0 || s->r.left < 0) {
467 		v4l2_err(&dev->v4l2_dev,
468 			"doesn't support negative values for top & left\n");
469 		return -EINVAL;
470 	}
471 
472 	return 0;
473 }
474 
vidioc_s_selection(struct file * file,void * prv,struct v4l2_selection * s)475 static int vidioc_s_selection(struct file *file, void *prv,
476 			      struct v4l2_selection *s)
477 {
478 	struct g2d_ctx *ctx = prv;
479 	struct g2d_frame *f;
480 	int ret;
481 
482 	ret = vidioc_try_selection(file, prv, s);
483 	if (ret)
484 		return ret;
485 	f = get_frame(ctx, s->type);
486 	if (IS_ERR(f))
487 		return PTR_ERR(f);
488 
489 	f->c_width	= s->r.width;
490 	f->c_height	= s->r.height;
491 	f->o_width	= s->r.left;
492 	f->o_height	= s->r.top;
493 	f->bottom	= f->o_height + f->c_height;
494 	f->right	= f->o_width + f->c_width;
495 	return 0;
496 }
497 
device_run(void * prv)498 static void device_run(void *prv)
499 {
500 	struct g2d_ctx *ctx = prv;
501 	struct g2d_dev *dev = ctx->dev;
502 	struct vb2_v4l2_buffer *src, *dst;
503 	unsigned long flags;
504 	u32 cmd = 0;
505 
506 	dev->curr = ctx;
507 
508 	src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
509 	dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
510 
511 	clk_enable(dev->gate);
512 	g2d_reset(dev);
513 
514 	spin_lock_irqsave(&dev->ctrl_lock, flags);
515 
516 	g2d_set_src_size(dev, &ctx->in);
517 	g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
518 
519 	g2d_set_dst_size(dev, &ctx->out);
520 	g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
521 
522 	g2d_set_rop4(dev, ctx->rop);
523 	g2d_set_flip(dev, ctx->flip);
524 
525 	if (ctx->in.c_width != ctx->out.c_width ||
526 		ctx->in.c_height != ctx->out.c_height) {
527 		if (dev->variant->hw_rev == TYPE_G2D_3X)
528 			cmd |= CMD_V3_ENABLE_STRETCH;
529 		else
530 			g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
531 	}
532 
533 	g2d_set_cmd(dev, cmd);
534 	g2d_start(dev);
535 
536 	spin_unlock_irqrestore(&dev->ctrl_lock, flags);
537 }
538 
g2d_isr(int irq,void * prv)539 static irqreturn_t g2d_isr(int irq, void *prv)
540 {
541 	struct g2d_dev *dev = prv;
542 	struct g2d_ctx *ctx = dev->curr;
543 	struct vb2_v4l2_buffer *src, *dst;
544 
545 	g2d_clear_int(dev);
546 	clk_disable(dev->gate);
547 
548 	BUG_ON(ctx == NULL);
549 
550 	src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
551 	dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
552 
553 	BUG_ON(src == NULL);
554 	BUG_ON(dst == NULL);
555 
556 	dst->timecode = src->timecode;
557 	dst->vb2_buf.timestamp = src->vb2_buf.timestamp;
558 	dst->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
559 	dst->flags |=
560 		src->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
561 
562 	v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
563 	v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
564 	v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
565 
566 	dev->curr = NULL;
567 	return IRQ_HANDLED;
568 }
569 
570 static const struct v4l2_file_operations g2d_fops = {
571 	.owner		= THIS_MODULE,
572 	.open		= g2d_open,
573 	.release	= g2d_release,
574 	.poll		= v4l2_m2m_fop_poll,
575 	.unlocked_ioctl	= video_ioctl2,
576 	.mmap		= v4l2_m2m_fop_mmap,
577 };
578 
579 static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
580 	.vidioc_querycap	= vidioc_querycap,
581 
582 	.vidioc_enum_fmt_vid_cap	= vidioc_enum_fmt,
583 	.vidioc_g_fmt_vid_cap		= vidioc_g_fmt,
584 	.vidioc_try_fmt_vid_cap		= vidioc_try_fmt,
585 	.vidioc_s_fmt_vid_cap		= vidioc_s_fmt,
586 
587 	.vidioc_enum_fmt_vid_out	= vidioc_enum_fmt,
588 	.vidioc_g_fmt_vid_out		= vidioc_g_fmt,
589 	.vidioc_try_fmt_vid_out		= vidioc_try_fmt,
590 	.vidioc_s_fmt_vid_out		= vidioc_s_fmt,
591 
592 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
593 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
594 	.vidioc_qbuf			= v4l2_m2m_ioctl_qbuf,
595 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
596 
597 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
598 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
599 
600 	.vidioc_g_selection		= vidioc_g_selection,
601 	.vidioc_s_selection		= vidioc_s_selection,
602 };
603 
604 static const struct video_device g2d_videodev = {
605 	.name		= G2D_NAME,
606 	.fops		= &g2d_fops,
607 	.ioctl_ops	= &g2d_ioctl_ops,
608 	.minor		= -1,
609 	.release	= video_device_release,
610 	.vfl_dir	= VFL_DIR_M2M,
611 };
612 
613 static const struct v4l2_m2m_ops g2d_m2m_ops = {
614 	.device_run	= device_run,
615 };
616 
617 static const struct of_device_id exynos_g2d_match[];
618 
g2d_probe(struct platform_device * pdev)619 static int g2d_probe(struct platform_device *pdev)
620 {
621 	struct g2d_dev *dev;
622 	struct video_device *vfd;
623 	struct resource *res;
624 	const struct of_device_id *of_id;
625 	int ret = 0;
626 
627 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
628 	if (!dev)
629 		return -ENOMEM;
630 
631 	spin_lock_init(&dev->ctrl_lock);
632 	mutex_init(&dev->mutex);
633 	atomic_set(&dev->num_inst, 0);
634 
635 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
636 
637 	dev->regs = devm_ioremap_resource(&pdev->dev, res);
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 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
667 	if (!res) {
668 		dev_err(&pdev->dev, "failed to find IRQ\n");
669 		ret = -ENXIO;
670 		goto unprep_clk_gate;
671 	}
672 
673 	dev->irq = res->start;
674 
675 	ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
676 						0, pdev->name, dev);
677 	if (ret) {
678 		dev_err(&pdev->dev, "failed to install IRQ\n");
679 		goto unprep_clk_gate;
680 	}
681 
682 	vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
683 
684 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
685 	if (ret)
686 		goto unprep_clk_gate;
687 	vfd = video_device_alloc();
688 	if (!vfd) {
689 		v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
690 		ret = -ENOMEM;
691 		goto unreg_v4l2_dev;
692 	}
693 	*vfd = g2d_videodev;
694 	set_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags);
695 	vfd->lock = &dev->mutex;
696 	vfd->v4l2_dev = &dev->v4l2_dev;
697 	vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
698 
699 	platform_set_drvdata(pdev, dev);
700 	dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
701 	if (IS_ERR(dev->m2m_dev)) {
702 		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
703 		ret = PTR_ERR(dev->m2m_dev);
704 		goto rel_vdev;
705 	}
706 
707 	def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
708 
709 	of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
710 	if (!of_id) {
711 		ret = -ENODEV;
712 		goto free_m2m;
713 	}
714 	dev->variant = (struct g2d_variant *)of_id->data;
715 
716 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
717 	if (ret) {
718 		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
719 		goto free_m2m;
720 	}
721 	video_set_drvdata(vfd, dev);
722 	dev->vfd = vfd;
723 	v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
724 		  vfd->num);
725 
726 	return 0;
727 
728 free_m2m:
729 	v4l2_m2m_release(dev->m2m_dev);
730 rel_vdev:
731 	video_device_release(vfd);
732 unreg_v4l2_dev:
733 	v4l2_device_unregister(&dev->v4l2_dev);
734 unprep_clk_gate:
735 	clk_unprepare(dev->gate);
736 put_clk_gate:
737 	clk_put(dev->gate);
738 unprep_clk:
739 	clk_unprepare(dev->clk);
740 put_clk:
741 	clk_put(dev->clk);
742 
743 	return ret;
744 }
745 
g2d_remove(struct platform_device * pdev)746 static int g2d_remove(struct platform_device *pdev)
747 {
748 	struct g2d_dev *dev = platform_get_drvdata(pdev);
749 
750 	v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
751 	v4l2_m2m_release(dev->m2m_dev);
752 	video_unregister_device(dev->vfd);
753 	v4l2_device_unregister(&dev->v4l2_dev);
754 	vb2_dma_contig_clear_max_seg_size(&pdev->dev);
755 	clk_unprepare(dev->gate);
756 	clk_put(dev->gate);
757 	clk_unprepare(dev->clk);
758 	clk_put(dev->clk);
759 	return 0;
760 }
761 
762 static struct g2d_variant g2d_drvdata_v3x = {
763 	.hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
764 };
765 
766 static struct g2d_variant g2d_drvdata_v4x = {
767 	.hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
768 };
769 
770 static const struct of_device_id exynos_g2d_match[] = {
771 	{
772 		.compatible = "samsung,s5pv210-g2d",
773 		.data = &g2d_drvdata_v3x,
774 	}, {
775 		.compatible = "samsung,exynos4212-g2d",
776 		.data = &g2d_drvdata_v4x,
777 	},
778 	{},
779 };
780 MODULE_DEVICE_TABLE(of, exynos_g2d_match);
781 
782 static struct platform_driver g2d_pdrv = {
783 	.probe		= g2d_probe,
784 	.remove		= g2d_remove,
785 	.driver		= {
786 		.name = G2D_NAME,
787 		.of_match_table = exynos_g2d_match,
788 	},
789 };
790 
791 module_platform_driver(g2d_pdrv);
792 
793 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
794 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
795 MODULE_LICENSE("GPL");
796