WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / platform / s5p-g2d / g2d.c
blob15bcb7f6e113c264b755035d5833773f8267ab4e
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Samsung S5P G2D - 2D Graphics Accelerator Driver
5 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
6 * Kamil Debski, <k.debski@samsung.com>
7 */
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>
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>
25 #include "g2d.h"
26 #include "g2d-regs.h"
28 #define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
30 static struct g2d_fmt formats[] = {
32 .fourcc = V4L2_PIX_FMT_RGB32,
33 .depth = 32,
34 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
37 .fourcc = V4L2_PIX_FMT_RGB565X,
38 .depth = 16,
39 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
42 .fourcc = V4L2_PIX_FMT_RGB555X,
43 .depth = 16,
44 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
47 .fourcc = V4L2_PIX_FMT_RGB444,
48 .depth = 16,
49 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
52 .fourcc = V4L2_PIX_FMT_RGB24,
53 .depth = 24,
54 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
57 #define NUM_FORMATS ARRAY_SIZE(formats)
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,
71 static struct g2d_fmt *find_fmt(struct v4l2_format *f)
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];
78 return NULL;
82 static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
83 enum v4l2_buf_type type)
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);
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[])
99 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
100 struct g2d_frame *f = get_frame(ctx, vq->type);
102 if (IS_ERR(f))
103 return PTR_ERR(f);
105 sizes[0] = f->size;
106 *nplanes = 1;
108 if (*nbuffers == 0)
109 *nbuffers = 1;
111 return 0;
114 static int g2d_buf_prepare(struct vb2_buffer *vb)
116 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
117 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
119 if (IS_ERR(f))
120 return PTR_ERR(f);
121 vb2_set_plane_payload(vb, 0, f->size);
122 return 0;
125 static void g2d_buf_queue(struct vb2_buffer *vb)
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);
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,
140 static int queue_init(void *priv, struct vb2_queue *src_vq,
141 struct vb2_queue *dst_vq)
143 struct g2d_ctx *ctx = priv;
144 int ret;
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;
156 ret = vb2_queue_init(src_vq);
157 if (ret)
158 return ret;
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;
170 return vb2_queue_init(dst_vq);
173 static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
175 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
176 ctrl_handler);
177 unsigned long flags;
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;
188 case V4L2_CID_HFLIP:
189 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
190 break;
193 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
194 return 0;
197 static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
198 .s_ctrl = g2d_s_ctrl,
201 static int g2d_setup_ctrls(struct g2d_ctx *ctx)
203 struct g2d_dev *dev = ctx->dev;
205 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
207 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
208 V4L2_CID_HFLIP, 0, 1, 1, 0);
210 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
211 V4L2_CID_VFLIP, 0, 1, 1, 0);
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);
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;
228 v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
230 return 0;
233 static int g2d_open(struct file *file)
235 struct g2d_dev *dev = video_drvdata(file);
236 struct g2d_ctx *ctx = NULL;
237 int ret = 0;
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;
247 if (mutex_lock_interruptible(&dev->mutex)) {
248 kfree(ctx);
249 return -ERESTARTSYS;
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;
258 v4l2_fh_init(&ctx->fh, video_devdata(file));
259 file->private_data = &ctx->fh;
260 v4l2_fh_add(&ctx->fh);
262 g2d_setup_ctrls(ctx);
264 /* Write the default values to the ctx struct */
265 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
267 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
268 mutex_unlock(&dev->mutex);
270 v4l2_info(&dev->v4l2_dev, "instance opened\n");
271 return 0;
274 static int g2d_release(struct file *file)
276 struct g2d_dev *dev = video_drvdata(file);
277 struct g2d_ctx *ctx = fh2ctx(file->private_data);
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;
288 static int vidioc_querycap(struct file *file, void *priv,
289 struct v4l2_capability *cap)
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;
297 static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
299 if (f->index >= NUM_FORMATS)
300 return -EINVAL;
301 f->pixelformat = formats[f->index].fourcc;
302 return 0;
305 static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
307 struct g2d_ctx *ctx = prv;
308 struct vb2_queue *vq;
309 struct g2d_frame *frm;
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);
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;
327 static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
329 struct g2d_fmt *fmt;
330 enum v4l2_field *field;
332 fmt = find_fmt(f);
333 if (!fmt)
334 return -EINVAL;
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;
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;
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;
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;
357 static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
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;
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;
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;
397 static int vidioc_g_selection(struct file *file, void *prv,
398 struct v4l2_selection *s)
400 struct g2d_ctx *ctx = prv;
401 struct g2d_frame *f;
403 f = get_frame(ctx, s->type);
404 if (IS_ERR(f))
405 return PTR_ERR(f);
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;
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;
444 return 0;
447 static int vidioc_try_selection(struct file *file, void *prv,
448 const struct v4l2_selection *s)
450 struct g2d_ctx *ctx = prv;
451 struct g2d_dev *dev = ctx->dev;
452 struct g2d_frame *f;
454 f = get_frame(ctx, s->type);
455 if (IS_ERR(f))
456 return PTR_ERR(f);
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;
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;
472 return 0;
475 static int vidioc_s_selection(struct file *file, void *prv,
476 struct v4l2_selection *s)
478 struct g2d_ctx *ctx = prv;
479 struct g2d_frame *f;
480 int ret;
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);
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;
498 static void device_run(void *prv)
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;
506 dev->curr = ctx;
508 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
509 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
511 clk_enable(dev->gate);
512 g2d_reset(dev);
514 spin_lock_irqsave(&dev->ctrl_lock, flags);
516 g2d_set_src_size(dev, &ctx->in);
517 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
519 g2d_set_dst_size(dev, &ctx->out);
520 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
522 g2d_set_rop4(dev, ctx->rop);
523 g2d_set_flip(dev, ctx->flip);
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);
533 g2d_set_cmd(dev, cmd);
534 g2d_start(dev);
536 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
539 static irqreturn_t g2d_isr(int irq, void *prv)
541 struct g2d_dev *dev = prv;
542 struct g2d_ctx *ctx = dev->curr;
543 struct vb2_v4l2_buffer *src, *dst;
545 g2d_clear_int(dev);
546 clk_disable(dev->gate);
548 BUG_ON(ctx == NULL);
550 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
551 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
553 BUG_ON(src == NULL);
554 BUG_ON(dst == NULL);
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;
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);
566 dev->curr = NULL;
567 return IRQ_HANDLED;
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,
579 static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
580 .vidioc_querycap = vidioc_querycap,
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,
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,
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,
597 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
598 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
600 .vidioc_g_selection = vidioc_g_selection,
601 .vidioc_s_selection = vidioc_s_selection,
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,
613 static const struct v4l2_m2m_ops g2d_m2m_ops = {
614 .device_run = device_run,
617 static const struct of_device_id exynos_g2d_match[];
619 static int g2d_probe(struct platform_device *pdev)
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;
627 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
628 if (!dev)
629 return -ENOMEM;
631 spin_lock_init(&dev->ctrl_lock);
632 mutex_init(&dev->mutex);
633 atomic_set(&dev->num_inst, 0);
635 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
637 dev->regs = devm_ioremap_resource(&pdev->dev, res);
638 if (IS_ERR(dev->regs))
639 return PTR_ERR(dev->regs);
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;
647 ret = clk_prepare(dev->clk);
648 if (ret) {
649 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
650 goto put_clk;
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;
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;
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;
673 dev->irq = res->start;
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;
682 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
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;
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;
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;
707 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
709 of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
710 if (!of_id) {
711 ret = -ENODEV;
712 goto free_m2m;
714 dev->variant = (struct g2d_variant *)of_id->data;
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;
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);
726 return 0;
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);
743 return ret;
746 static int g2d_remove(struct platform_device *pdev)
748 struct g2d_dev *dev = platform_get_drvdata(pdev);
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;
762 static struct g2d_variant g2d_drvdata_v3x = {
763 .hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
766 static struct g2d_variant g2d_drvdata_v4x = {
767 .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
770 static const struct of_device_id exynos_g2d_match[] = {
772 .compatible = "samsung,s5pv210-g2d",
773 .data = &g2d_drvdata_v3x,
774 }, {
775 .compatible = "samsung,exynos4212-g2d",
776 .data = &g2d_drvdata_v4x,
780 MODULE_DEVICE_TABLE(of, exynos_g2d_match);
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,
791 module_platform_driver(g2d_pdrv);
793 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
794 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
795 MODULE_LICENSE("GPL");