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