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