netfilter: remove unnecessary goto statement for error recovery
[cascardo/linux.git] / drivers / media / video / 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/version.h>
16 #include <linux/timer.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/clk.h>
20 #include <linux/interrupt.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-core.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, const struct v4l2_format *fmt,
105                            unsigned int *nbuffers, unsigned int *nplanes,
106                            unsigned int sizes[], void *alloc_ctxs[])
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         alloc_ctxs[0] = ctx->dev->alloc_ctx;
117
118         if (*nbuffers == 0)
119                 *nbuffers = 1;
120
121         return 0;
122 }
123
124 static int g2d_buf_prepare(struct vb2_buffer *vb)
125 {
126         struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
127         struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
128
129         if (IS_ERR(f))
130                 return PTR_ERR(f);
131         vb2_set_plane_payload(vb, 0, f->size);
132         return 0;
133 }
134
135 static void g2d_buf_queue(struct vb2_buffer *vb)
136 {
137         struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
138         v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
139 }
140
141
142 static struct vb2_ops g2d_qops = {
143         .queue_setup    = g2d_queue_setup,
144         .buf_prepare    = g2d_buf_prepare,
145         .buf_queue      = g2d_buf_queue,
146 };
147
148 static int queue_init(void *priv, struct vb2_queue *src_vq,
149                                                 struct vb2_queue *dst_vq)
150 {
151         struct g2d_ctx *ctx = priv;
152         int ret;
153
154         memset(src_vq, 0, sizeof(*src_vq));
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
162         ret = vb2_queue_init(src_vq);
163         if (ret)
164                 return ret;
165
166         memset(dst_vq, 0, sizeof(*dst_vq));
167         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
168         dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
169         dst_vq->drv_priv = ctx;
170         dst_vq->ops = &g2d_qops;
171         dst_vq->mem_ops = &vb2_dma_contig_memops;
172         dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
173
174         return vb2_queue_init(dst_vq);
175 }
176
177 static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
178 {
179         struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
180                                                                 ctrl_handler);
181         unsigned long flags;
182
183         spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
184         switch (ctrl->id) {
185         case V4L2_CID_COLORFX:
186                 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
187                         ctx->rop = ROP4_INVERT;
188                 else
189                         ctx->rop = ROP4_COPY;
190                 break;
191
192         case V4L2_CID_HFLIP:
193                 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
194                 break;
195
196         }
197         spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
198         return 0;
199 }
200
201 static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
202         .s_ctrl         = g2d_s_ctrl,
203 };
204
205 static int g2d_setup_ctrls(struct g2d_ctx *ctx)
206 {
207         struct g2d_dev *dev = ctx->dev;
208
209         v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
210
211         ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
212                                                 V4L2_CID_HFLIP, 0, 1, 1, 0);
213
214         ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
215                                                 V4L2_CID_VFLIP, 0, 1, 1, 0);
216
217         v4l2_ctrl_new_std_menu(
218                 &ctx->ctrl_handler,
219                 &g2d_ctrl_ops,
220                 V4L2_CID_COLORFX,
221                 V4L2_COLORFX_NEGATIVE,
222                 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
223                 V4L2_COLORFX_NONE);
224
225         if (ctx->ctrl_handler.error) {
226                 int err = ctx->ctrl_handler.error;
227                 v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
228                 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
229                 return err;
230         }
231
232         v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
233
234         return 0;
235 }
236
237 static int g2d_open(struct file *file)
238 {
239         struct g2d_dev *dev = video_drvdata(file);
240         struct g2d_ctx *ctx = NULL;
241         int ret = 0;
242
243         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
244         if (!ctx)
245                 return -ENOMEM;
246         ctx->dev = dev;
247         /* Set default formats */
248         ctx->in         = def_frame;
249         ctx->out        = def_frame;
250
251         ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
252         if (IS_ERR(ctx->m2m_ctx)) {
253                 ret = PTR_ERR(ctx->m2m_ctx);
254                 kfree(ctx);
255                 return ret;
256         }
257         v4l2_fh_init(&ctx->fh, video_devdata(file));
258         file->private_data = &ctx->fh;
259         v4l2_fh_add(&ctx->fh);
260
261         g2d_setup_ctrls(ctx);
262
263         /* Write the default values to the ctx struct */
264         v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
265
266         ctx->fh.ctrl_handler = &ctx->ctrl_handler;
267
268         v4l2_info(&dev->v4l2_dev, "instance opened\n");
269         return 0;
270 }
271
272 static int g2d_release(struct file *file)
273 {
274         struct g2d_dev *dev = video_drvdata(file);
275         struct g2d_ctx *ctx = fh2ctx(file->private_data);
276
277         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
278         v4l2_fh_del(&ctx->fh);
279         v4l2_fh_exit(&ctx->fh);
280         kfree(ctx);
281         v4l2_info(&dev->v4l2_dev, "instance closed\n");
282         return 0;
283 }
284
285
286 static int vidioc_querycap(struct file *file, void *priv,
287                                 struct v4l2_capability *cap)
288 {
289         strncpy(cap->driver, G2D_NAME, sizeof(cap->driver) - 1);
290         strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1);
291         cap->bus_info[0] = 0;
292         cap->version = KERNEL_VERSION(1, 0, 0);
293         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
294                                                         | V4L2_CAP_STREAMING;
295         return 0;
296 }
297
298 static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
299 {
300         struct g2d_fmt *fmt;
301         if (f->index >= NUM_FORMATS)
302                 return -EINVAL;
303         fmt = &formats[f->index];
304         f->pixelformat = fmt->fourcc;
305         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
306         return 0;
307 }
308
309 static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
310 {
311         struct g2d_ctx *ctx = prv;
312         struct vb2_queue *vq;
313         struct g2d_frame *frm;
314
315         vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
316         if (!vq)
317                 return -EINVAL;
318         frm = get_frame(ctx, f->type);
319         if (IS_ERR(frm))
320                 return PTR_ERR(frm);
321
322         f->fmt.pix.width                = frm->width;
323         f->fmt.pix.height               = frm->height;
324         f->fmt.pix.field                = V4L2_FIELD_NONE;
325         f->fmt.pix.pixelformat          = frm->fmt->fourcc;
326         f->fmt.pix.bytesperline         = (frm->width * frm->fmt->depth) >> 3;
327         f->fmt.pix.sizeimage            = frm->size;
328         return 0;
329 }
330
331 static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
332 {
333         struct g2d_fmt *fmt;
334         enum v4l2_field *field;
335
336         fmt = find_fmt(f);
337         if (!fmt)
338                 return -EINVAL;
339
340         field = &f->fmt.pix.field;
341         if (*field == V4L2_FIELD_ANY)
342                 *field = V4L2_FIELD_NONE;
343         else if (*field != V4L2_FIELD_NONE)
344                 return -EINVAL;
345
346         if (f->fmt.pix.width > MAX_WIDTH)
347                 f->fmt.pix.width = MAX_WIDTH;
348         if (f->fmt.pix.height > MAX_HEIGHT)
349                 f->fmt.pix.height = MAX_HEIGHT;
350
351         if (f->fmt.pix.width < 1)
352                 f->fmt.pix.width = 1;
353         if (f->fmt.pix.height < 1)
354                 f->fmt.pix.height = 1;
355
356         f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
357         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
358         return 0;
359 }
360
361 static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
362 {
363         struct g2d_ctx *ctx = prv;
364         struct g2d_dev *dev = ctx->dev;
365         struct vb2_queue *vq;
366         struct g2d_frame *frm;
367         struct g2d_fmt *fmt;
368         int ret = 0;
369
370         /* Adjust all values accordingly to the hardware capabilities
371          * and chosen format. */
372         ret = vidioc_try_fmt(file, prv, f);
373         if (ret)
374                 return ret;
375         vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
376         if (vb2_is_busy(vq)) {
377                 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
378                 return -EBUSY;
379         }
380         frm = get_frame(ctx, f->type);
381         if (IS_ERR(frm))
382                 return PTR_ERR(frm);
383         fmt = find_fmt(f);
384         if (!fmt)
385                 return -EINVAL;
386         frm->width      = f->fmt.pix.width;
387         frm->height     = f->fmt.pix.height;
388         frm->size       = f->fmt.pix.sizeimage;
389         /* Reset crop settings */
390         frm->o_width    = 0;
391         frm->o_height   = 0;
392         frm->c_width    = frm->width;
393         frm->c_height   = frm->height;
394         frm->right      = frm->width;
395         frm->bottom     = frm->height;
396         frm->fmt        = fmt;
397         frm->stride     = f->fmt.pix.bytesperline;
398         return 0;
399 }
400
401 static unsigned int g2d_poll(struct file *file, struct poll_table_struct *wait)
402 {
403         struct g2d_ctx *ctx = fh2ctx(file->private_data);
404         return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
405 }
406
407 static int g2d_mmap(struct file *file, struct vm_area_struct *vma)
408 {
409         struct g2d_ctx *ctx = fh2ctx(file->private_data);
410         return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
411 }
412
413 static int vidioc_reqbufs(struct file *file, void *priv,
414                         struct v4l2_requestbuffers *reqbufs)
415 {
416         struct g2d_ctx *ctx = priv;
417         return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
418 }
419
420 static int vidioc_querybuf(struct file *file, void *priv,
421                         struct v4l2_buffer *buf)
422 {
423         struct g2d_ctx *ctx = priv;
424         return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
425 }
426
427 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
428 {
429         struct g2d_ctx *ctx = priv;
430         return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
431 }
432
433 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
434 {
435         struct g2d_ctx *ctx = priv;
436         return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
437 }
438
439
440 static int vidioc_streamon(struct file *file, void *priv,
441                                         enum v4l2_buf_type type)
442 {
443         struct g2d_ctx *ctx = priv;
444         return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
445 }
446
447 static int vidioc_streamoff(struct file *file, void *priv,
448                                         enum v4l2_buf_type type)
449 {
450         struct g2d_ctx *ctx = priv;
451         return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
452 }
453
454 static int vidioc_cropcap(struct file *file, void *priv,
455                                         struct v4l2_cropcap *cr)
456 {
457         struct g2d_ctx *ctx = priv;
458         struct g2d_frame *f;
459
460         f = get_frame(ctx, cr->type);
461         if (IS_ERR(f))
462                 return PTR_ERR(f);
463
464         cr->bounds.left         = 0;
465         cr->bounds.top          = 0;
466         cr->bounds.width        = f->width;
467         cr->bounds.height       = f->height;
468         cr->defrect             = cr->bounds;
469         return 0;
470 }
471
472 static int vidioc_g_crop(struct file *file, void *prv, struct v4l2_crop *cr)
473 {
474         struct g2d_ctx *ctx = prv;
475         struct g2d_frame *f;
476
477         f = get_frame(ctx, cr->type);
478         if (IS_ERR(f))
479                 return PTR_ERR(f);
480
481         cr->c.left      = f->o_height;
482         cr->c.top       = f->o_width;
483         cr->c.width     = f->c_width;
484         cr->c.height    = f->c_height;
485         return 0;
486 }
487
488 static int vidioc_try_crop(struct file *file, void *prv, struct v4l2_crop *cr)
489 {
490         struct g2d_ctx *ctx = prv;
491         struct g2d_dev *dev = ctx->dev;
492         struct g2d_frame *f;
493
494         f = get_frame(ctx, cr->type);
495         if (IS_ERR(f))
496                 return PTR_ERR(f);
497
498         if (cr->c.top < 0 || cr->c.left < 0) {
499                 v4l2_err(&dev->v4l2_dev,
500                         "doesn't support negative values for top & left\n");
501                 return -EINVAL;
502         }
503
504         return 0;
505 }
506
507 static int vidioc_s_crop(struct file *file, void *prv, struct v4l2_crop *cr)
508 {
509         struct g2d_ctx *ctx = prv;
510         struct g2d_frame *f;
511         int ret;
512
513         ret = vidioc_try_crop(file, prv, cr);
514         if (ret)
515                 return ret;
516         f = get_frame(ctx, cr->type);
517         if (IS_ERR(f))
518                 return PTR_ERR(f);
519
520         f->c_width      = cr->c.width;
521         f->c_height     = cr->c.height;
522         f->o_width      = cr->c.left;
523         f->o_height     = cr->c.top;
524         f->bottom       = f->o_height + f->c_height;
525         f->right        = f->o_width + f->c_width;
526         return 0;
527 }
528
529 static void g2d_lock(void *prv)
530 {
531         struct g2d_ctx *ctx = prv;
532         struct g2d_dev *dev = ctx->dev;
533         mutex_lock(&dev->mutex);
534 }
535
536 static void g2d_unlock(void *prv)
537 {
538         struct g2d_ctx *ctx = prv;
539         struct g2d_dev *dev = ctx->dev;
540         mutex_unlock(&dev->mutex);
541 }
542
543 static void job_abort(void *prv)
544 {
545         struct g2d_ctx *ctx = prv;
546         struct g2d_dev *dev = ctx->dev;
547         int ret;
548
549         if (dev->curr == NULL) /* No job currently running */
550                 return;
551
552         ret = wait_event_timeout(dev->irq_queue,
553                 dev->curr == NULL,
554                 msecs_to_jiffies(G2D_TIMEOUT));
555 }
556
557 static void device_run(void *prv)
558 {
559         struct g2d_ctx *ctx = prv;
560         struct g2d_dev *dev = ctx->dev;
561         struct vb2_buffer *src, *dst;
562         unsigned long flags;
563         u32 cmd = 0;
564
565         dev->curr = ctx;
566
567         src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
568         dst = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
569
570         clk_enable(dev->gate);
571         g2d_reset(dev);
572
573         spin_lock_irqsave(&dev->ctrl_lock, flags);
574
575         g2d_set_src_size(dev, &ctx->in);
576         g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(src, 0));
577
578         g2d_set_dst_size(dev, &ctx->out);
579         g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(dst, 0));
580
581         g2d_set_rop4(dev, ctx->rop);
582         g2d_set_flip(dev, ctx->flip);
583
584         if (ctx->in.c_width != ctx->out.c_width ||
585                 ctx->in.c_height != ctx->out.c_height)
586                 cmd |= g2d_cmd_stretch(1);
587         g2d_set_cmd(dev, cmd);
588         g2d_start(dev);
589
590         spin_unlock_irqrestore(&dev->ctrl_lock, flags);
591 }
592
593 static irqreturn_t g2d_isr(int irq, void *prv)
594 {
595         struct g2d_dev *dev = prv;
596         struct g2d_ctx *ctx = dev->curr;
597         struct vb2_buffer *src, *dst;
598
599         g2d_clear_int(dev);
600         clk_disable(dev->gate);
601
602         BUG_ON(ctx == NULL);
603
604         src = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
605         dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
606
607         BUG_ON(src == NULL);
608         BUG_ON(dst == NULL);
609
610         v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
611         v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
612         v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
613
614         dev->curr = NULL;
615         wake_up(&dev->irq_queue);
616         return IRQ_HANDLED;
617 }
618
619 static const struct v4l2_file_operations g2d_fops = {
620         .owner          = THIS_MODULE,
621         .open           = g2d_open,
622         .release        = g2d_release,
623         .poll           = g2d_poll,
624         .unlocked_ioctl = video_ioctl2,
625         .mmap           = g2d_mmap,
626 };
627
628 static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
629         .vidioc_querycap        = vidioc_querycap,
630
631         .vidioc_enum_fmt_vid_cap        = vidioc_enum_fmt,
632         .vidioc_g_fmt_vid_cap           = vidioc_g_fmt,
633         .vidioc_try_fmt_vid_cap         = vidioc_try_fmt,
634         .vidioc_s_fmt_vid_cap           = vidioc_s_fmt,
635
636         .vidioc_enum_fmt_vid_out        = vidioc_enum_fmt,
637         .vidioc_g_fmt_vid_out           = vidioc_g_fmt,
638         .vidioc_try_fmt_vid_out         = vidioc_try_fmt,
639         .vidioc_s_fmt_vid_out           = vidioc_s_fmt,
640
641         .vidioc_reqbufs                 = vidioc_reqbufs,
642         .vidioc_querybuf                = vidioc_querybuf,
643
644         .vidioc_qbuf                    = vidioc_qbuf,
645         .vidioc_dqbuf                   = vidioc_dqbuf,
646
647         .vidioc_streamon                = vidioc_streamon,
648         .vidioc_streamoff               = vidioc_streamoff,
649
650         .vidioc_g_crop                  = vidioc_g_crop,
651         .vidioc_s_crop                  = vidioc_s_crop,
652         .vidioc_cropcap                 = vidioc_cropcap,
653 };
654
655 static struct video_device g2d_videodev = {
656         .name           = G2D_NAME,
657         .fops           = &g2d_fops,
658         .ioctl_ops      = &g2d_ioctl_ops,
659         .minor          = -1,
660         .release        = video_device_release,
661 };
662
663 static struct v4l2_m2m_ops g2d_m2m_ops = {
664         .device_run     = device_run,
665         .job_abort      = job_abort,
666         .lock           = g2d_lock,
667         .unlock         = g2d_unlock,
668 };
669
670 static int g2d_probe(struct platform_device *pdev)
671 {
672         struct g2d_dev *dev;
673         struct video_device *vfd;
674         struct resource *res;
675         int ret = 0;
676
677         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
678         if (!dev)
679                 return -ENOMEM;
680
681         spin_lock_init(&dev->ctrl_lock);
682         mutex_init(&dev->mutex);
683         atomic_set(&dev->num_inst, 0);
684         init_waitqueue_head(&dev->irq_queue);
685
686         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
687
688         dev->regs = devm_request_and_ioremap(&pdev->dev, res);
689         if (dev->regs == NULL) {
690                         dev_err(&pdev->dev, "Failed to obtain io memory\n");
691                         return -ENOENT;
692         }
693
694         dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
695         if (IS_ERR_OR_NULL(dev->clk)) {
696                 dev_err(&pdev->dev, "failed to get g2d clock\n");
697                 return -ENXIO;
698         }
699
700         ret = clk_prepare(dev->clk);
701         if (ret) {
702                 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
703                 goto put_clk;
704         }
705
706         dev->gate = clk_get(&pdev->dev, "fimg2d");
707         if (IS_ERR_OR_NULL(dev->gate)) {
708                 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
709                 ret = -ENXIO;
710                 goto unprep_clk;
711         }
712
713         ret = clk_prepare(dev->gate);
714         if (ret) {
715                 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
716                 goto put_clk_gate;
717         }
718
719         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
720         if (!res) {
721                 dev_err(&pdev->dev, "failed to find IRQ\n");
722                 ret = -ENXIO;
723                 goto unprep_clk_gate;
724         }
725
726         dev->irq = res->start;
727
728         ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
729                                                 0, pdev->name, dev);
730         if (ret) {
731                 dev_err(&pdev->dev, "failed to install IRQ\n");
732                 goto put_clk_gate;
733         }
734
735         dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
736         if (IS_ERR(dev->alloc_ctx)) {
737                 ret = PTR_ERR(dev->alloc_ctx);
738                 goto unprep_clk_gate;
739         }
740
741         ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
742         if (ret)
743                 goto alloc_ctx_cleanup;
744         vfd = video_device_alloc();
745         if (!vfd) {
746                 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
747                 ret = -ENOMEM;
748                 goto unreg_v4l2_dev;
749         }
750         *vfd = g2d_videodev;
751         /* Locking in file operations other than ioctl should be done
752            by the driver, not the V4L2 core.
753            This driver needs auditing so that this flag can be removed. */
754         set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
755         vfd->lock = &dev->mutex;
756         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
757         if (ret) {
758                 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
759                 goto rel_vdev;
760         }
761         video_set_drvdata(vfd, dev);
762         snprintf(vfd->name, sizeof(vfd->name), "%s", g2d_videodev.name);
763         dev->vfd = vfd;
764         v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
765                                                                 vfd->num);
766         platform_set_drvdata(pdev, dev);
767         dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
768         if (IS_ERR(dev->m2m_dev)) {
769                 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
770                 ret = PTR_ERR(dev->m2m_dev);
771                 goto unreg_video_dev;
772         }
773
774         def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
775
776         return 0;
777
778 unreg_video_dev:
779         video_unregister_device(dev->vfd);
780 rel_vdev:
781         video_device_release(vfd);
782 unreg_v4l2_dev:
783         v4l2_device_unregister(&dev->v4l2_dev);
784 alloc_ctx_cleanup:
785         vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
786 unprep_clk_gate:
787         clk_unprepare(dev->gate);
788 put_clk_gate:
789         clk_put(dev->gate);
790 unprep_clk:
791         clk_unprepare(dev->clk);
792 put_clk:
793         clk_put(dev->clk);
794
795         return ret;
796 }
797
798 static int g2d_remove(struct platform_device *pdev)
799 {
800         struct g2d_dev *dev = (struct g2d_dev *)platform_get_drvdata(pdev);
801
802         v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
803         v4l2_m2m_release(dev->m2m_dev);
804         video_unregister_device(dev->vfd);
805         v4l2_device_unregister(&dev->v4l2_dev);
806         vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
807         clk_unprepare(dev->gate);
808         clk_put(dev->gate);
809         clk_unprepare(dev->clk);
810         clk_put(dev->clk);
811         return 0;
812 }
813
814 static struct platform_driver g2d_pdrv = {
815         .probe          = g2d_probe,
816         .remove         = g2d_remove,
817         .driver         = {
818                 .name = G2D_NAME,
819                 .owner = THIS_MODULE,
820         },
821 };
822
823 module_platform_driver(g2d_pdrv);
824
825 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
826 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
827 MODULE_LICENSE("GPL");