[media] move soc_camera i2c drivers into its own dir
[linux-2.6-block.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         if (mutex_lock_interruptible(&dev->mutex)) {
252                 kfree(ctx);
253                 return -ERESTARTSYS;
254         }
255         ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
256         if (IS_ERR(ctx->m2m_ctx)) {
257                 ret = PTR_ERR(ctx->m2m_ctx);
258                 mutex_unlock(&dev->mutex);
259                 kfree(ctx);
260                 return ret;
261         }
262         v4l2_fh_init(&ctx->fh, video_devdata(file));
263         file->private_data = &ctx->fh;
264         v4l2_fh_add(&ctx->fh);
265
266         g2d_setup_ctrls(ctx);
267
268         /* Write the default values to the ctx struct */
269         v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
270
271         ctx->fh.ctrl_handler = &ctx->ctrl_handler;
272         mutex_unlock(&dev->mutex);
273
274         v4l2_info(&dev->v4l2_dev, "instance opened\n");
275         return 0;
276 }
277
278 static int g2d_release(struct file *file)
279 {
280         struct g2d_dev *dev = video_drvdata(file);
281         struct g2d_ctx *ctx = fh2ctx(file->private_data);
282
283         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
284         v4l2_fh_del(&ctx->fh);
285         v4l2_fh_exit(&ctx->fh);
286         kfree(ctx);
287         v4l2_info(&dev->v4l2_dev, "instance closed\n");
288         return 0;
289 }
290
291
292 static int vidioc_querycap(struct file *file, void *priv,
293                                 struct v4l2_capability *cap)
294 {
295         strncpy(cap->driver, G2D_NAME, sizeof(cap->driver) - 1);
296         strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1);
297         cap->bus_info[0] = 0;
298         cap->version = KERNEL_VERSION(1, 0, 0);
299         /*
300          * This is only a mem-to-mem video device. The capture and output
301          * device capability flags are left only for backward compatibility
302          * and are scheduled for removal.
303          */
304         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
305                             V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
306         return 0;
307 }
308
309 static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
310 {
311         struct g2d_fmt *fmt;
312         if (f->index >= NUM_FORMATS)
313                 return -EINVAL;
314         fmt = &formats[f->index];
315         f->pixelformat = fmt->fourcc;
316         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
317         return 0;
318 }
319
320 static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
321 {
322         struct g2d_ctx *ctx = prv;
323         struct vb2_queue *vq;
324         struct g2d_frame *frm;
325
326         vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
327         if (!vq)
328                 return -EINVAL;
329         frm = get_frame(ctx, f->type);
330         if (IS_ERR(frm))
331                 return PTR_ERR(frm);
332
333         f->fmt.pix.width                = frm->width;
334         f->fmt.pix.height               = frm->height;
335         f->fmt.pix.field                = V4L2_FIELD_NONE;
336         f->fmt.pix.pixelformat          = frm->fmt->fourcc;
337         f->fmt.pix.bytesperline         = (frm->width * frm->fmt->depth) >> 3;
338         f->fmt.pix.sizeimage            = frm->size;
339         return 0;
340 }
341
342 static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
343 {
344         struct g2d_fmt *fmt;
345         enum v4l2_field *field;
346
347         fmt = find_fmt(f);
348         if (!fmt)
349                 return -EINVAL;
350
351         field = &f->fmt.pix.field;
352         if (*field == V4L2_FIELD_ANY)
353                 *field = V4L2_FIELD_NONE;
354         else if (*field != V4L2_FIELD_NONE)
355                 return -EINVAL;
356
357         if (f->fmt.pix.width > MAX_WIDTH)
358                 f->fmt.pix.width = MAX_WIDTH;
359         if (f->fmt.pix.height > MAX_HEIGHT)
360                 f->fmt.pix.height = MAX_HEIGHT;
361
362         if (f->fmt.pix.width < 1)
363                 f->fmt.pix.width = 1;
364         if (f->fmt.pix.height < 1)
365                 f->fmt.pix.height = 1;
366
367         f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
368         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
369         return 0;
370 }
371
372 static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
373 {
374         struct g2d_ctx *ctx = prv;
375         struct g2d_dev *dev = ctx->dev;
376         struct vb2_queue *vq;
377         struct g2d_frame *frm;
378         struct g2d_fmt *fmt;
379         int ret = 0;
380
381         /* Adjust all values accordingly to the hardware capabilities
382          * and chosen format. */
383         ret = vidioc_try_fmt(file, prv, f);
384         if (ret)
385                 return ret;
386         vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
387         if (vb2_is_busy(vq)) {
388                 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
389                 return -EBUSY;
390         }
391         frm = get_frame(ctx, f->type);
392         if (IS_ERR(frm))
393                 return PTR_ERR(frm);
394         fmt = find_fmt(f);
395         if (!fmt)
396                 return -EINVAL;
397         frm->width      = f->fmt.pix.width;
398         frm->height     = f->fmt.pix.height;
399         frm->size       = f->fmt.pix.sizeimage;
400         /* Reset crop settings */
401         frm->o_width    = 0;
402         frm->o_height   = 0;
403         frm->c_width    = frm->width;
404         frm->c_height   = frm->height;
405         frm->right      = frm->width;
406         frm->bottom     = frm->height;
407         frm->fmt        = fmt;
408         frm->stride     = f->fmt.pix.bytesperline;
409         return 0;
410 }
411
412 static unsigned int g2d_poll(struct file *file, struct poll_table_struct *wait)
413 {
414         struct g2d_ctx *ctx = fh2ctx(file->private_data);
415         struct g2d_dev *dev = ctx->dev;
416         unsigned int res;
417
418         mutex_lock(&dev->mutex);
419         res = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
420         mutex_unlock(&dev->mutex);
421         return res;
422 }
423
424 static int g2d_mmap(struct file *file, struct vm_area_struct *vma)
425 {
426         struct g2d_ctx *ctx = fh2ctx(file->private_data);
427         struct g2d_dev *dev = ctx->dev;
428         int ret;
429
430         if (mutex_lock_interruptible(&dev->mutex))
431                 return -ERESTARTSYS;
432         ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
433         mutex_unlock(&dev->mutex);
434         return ret;
435 }
436
437 static int vidioc_reqbufs(struct file *file, void *priv,
438                         struct v4l2_requestbuffers *reqbufs)
439 {
440         struct g2d_ctx *ctx = priv;
441         return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
442 }
443
444 static int vidioc_querybuf(struct file *file, void *priv,
445                         struct v4l2_buffer *buf)
446 {
447         struct g2d_ctx *ctx = priv;
448         return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
449 }
450
451 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
452 {
453         struct g2d_ctx *ctx = priv;
454         return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
455 }
456
457 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
458 {
459         struct g2d_ctx *ctx = priv;
460         return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
461 }
462
463
464 static int vidioc_streamon(struct file *file, void *priv,
465                                         enum v4l2_buf_type type)
466 {
467         struct g2d_ctx *ctx = priv;
468         return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
469 }
470
471 static int vidioc_streamoff(struct file *file, void *priv,
472                                         enum v4l2_buf_type type)
473 {
474         struct g2d_ctx *ctx = priv;
475         return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
476 }
477
478 static int vidioc_cropcap(struct file *file, void *priv,
479                                         struct v4l2_cropcap *cr)
480 {
481         struct g2d_ctx *ctx = priv;
482         struct g2d_frame *f;
483
484         f = get_frame(ctx, cr->type);
485         if (IS_ERR(f))
486                 return PTR_ERR(f);
487
488         cr->bounds.left         = 0;
489         cr->bounds.top          = 0;
490         cr->bounds.width        = f->width;
491         cr->bounds.height       = f->height;
492         cr->defrect             = cr->bounds;
493         return 0;
494 }
495
496 static int vidioc_g_crop(struct file *file, void *prv, struct v4l2_crop *cr)
497 {
498         struct g2d_ctx *ctx = prv;
499         struct g2d_frame *f;
500
501         f = get_frame(ctx, cr->type);
502         if (IS_ERR(f))
503                 return PTR_ERR(f);
504
505         cr->c.left      = f->o_height;
506         cr->c.top       = f->o_width;
507         cr->c.width     = f->c_width;
508         cr->c.height    = f->c_height;
509         return 0;
510 }
511
512 static int vidioc_try_crop(struct file *file, void *prv, struct v4l2_crop *cr)
513 {
514         struct g2d_ctx *ctx = prv;
515         struct g2d_dev *dev = ctx->dev;
516         struct g2d_frame *f;
517
518         f = get_frame(ctx, cr->type);
519         if (IS_ERR(f))
520                 return PTR_ERR(f);
521
522         if (cr->c.top < 0 || cr->c.left < 0) {
523                 v4l2_err(&dev->v4l2_dev,
524                         "doesn't support negative values for top & left\n");
525                 return -EINVAL;
526         }
527
528         return 0;
529 }
530
531 static int vidioc_s_crop(struct file *file, void *prv, struct v4l2_crop *cr)
532 {
533         struct g2d_ctx *ctx = prv;
534         struct g2d_frame *f;
535         int ret;
536
537         ret = vidioc_try_crop(file, prv, cr);
538         if (ret)
539                 return ret;
540         f = get_frame(ctx, cr->type);
541         if (IS_ERR(f))
542                 return PTR_ERR(f);
543
544         f->c_width      = cr->c.width;
545         f->c_height     = cr->c.height;
546         f->o_width      = cr->c.left;
547         f->o_height     = cr->c.top;
548         f->bottom       = f->o_height + f->c_height;
549         f->right        = f->o_width + f->c_width;
550         return 0;
551 }
552
553 static void g2d_lock(void *prv)
554 {
555         struct g2d_ctx *ctx = prv;
556         struct g2d_dev *dev = ctx->dev;
557         mutex_lock(&dev->mutex);
558 }
559
560 static void g2d_unlock(void *prv)
561 {
562         struct g2d_ctx *ctx = prv;
563         struct g2d_dev *dev = ctx->dev;
564         mutex_unlock(&dev->mutex);
565 }
566
567 static void job_abort(void *prv)
568 {
569         struct g2d_ctx *ctx = prv;
570         struct g2d_dev *dev = ctx->dev;
571         int ret;
572
573         if (dev->curr == NULL) /* No job currently running */
574                 return;
575
576         ret = wait_event_timeout(dev->irq_queue,
577                 dev->curr == NULL,
578                 msecs_to_jiffies(G2D_TIMEOUT));
579 }
580
581 static void device_run(void *prv)
582 {
583         struct g2d_ctx *ctx = prv;
584         struct g2d_dev *dev = ctx->dev;
585         struct vb2_buffer *src, *dst;
586         unsigned long flags;
587         u32 cmd = 0;
588
589         dev->curr = ctx;
590
591         src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
592         dst = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
593
594         clk_enable(dev->gate);
595         g2d_reset(dev);
596
597         spin_lock_irqsave(&dev->ctrl_lock, flags);
598
599         g2d_set_src_size(dev, &ctx->in);
600         g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(src, 0));
601
602         g2d_set_dst_size(dev, &ctx->out);
603         g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(dst, 0));
604
605         g2d_set_rop4(dev, ctx->rop);
606         g2d_set_flip(dev, ctx->flip);
607
608         if (ctx->in.c_width != ctx->out.c_width ||
609                 ctx->in.c_height != ctx->out.c_height)
610                 cmd |= g2d_cmd_stretch(1);
611         g2d_set_cmd(dev, cmd);
612         g2d_start(dev);
613
614         spin_unlock_irqrestore(&dev->ctrl_lock, flags);
615 }
616
617 static irqreturn_t g2d_isr(int irq, void *prv)
618 {
619         struct g2d_dev *dev = prv;
620         struct g2d_ctx *ctx = dev->curr;
621         struct vb2_buffer *src, *dst;
622
623         g2d_clear_int(dev);
624         clk_disable(dev->gate);
625
626         BUG_ON(ctx == NULL);
627
628         src = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
629         dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
630
631         BUG_ON(src == NULL);
632         BUG_ON(dst == NULL);
633
634         v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
635         v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
636         v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
637
638         dev->curr = NULL;
639         wake_up(&dev->irq_queue);
640         return IRQ_HANDLED;
641 }
642
643 static const struct v4l2_file_operations g2d_fops = {
644         .owner          = THIS_MODULE,
645         .open           = g2d_open,
646         .release        = g2d_release,
647         .poll           = g2d_poll,
648         .unlocked_ioctl = video_ioctl2,
649         .mmap           = g2d_mmap,
650 };
651
652 static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
653         .vidioc_querycap        = vidioc_querycap,
654
655         .vidioc_enum_fmt_vid_cap        = vidioc_enum_fmt,
656         .vidioc_g_fmt_vid_cap           = vidioc_g_fmt,
657         .vidioc_try_fmt_vid_cap         = vidioc_try_fmt,
658         .vidioc_s_fmt_vid_cap           = vidioc_s_fmt,
659
660         .vidioc_enum_fmt_vid_out        = vidioc_enum_fmt,
661         .vidioc_g_fmt_vid_out           = vidioc_g_fmt,
662         .vidioc_try_fmt_vid_out         = vidioc_try_fmt,
663         .vidioc_s_fmt_vid_out           = vidioc_s_fmt,
664
665         .vidioc_reqbufs                 = vidioc_reqbufs,
666         .vidioc_querybuf                = vidioc_querybuf,
667
668         .vidioc_qbuf                    = vidioc_qbuf,
669         .vidioc_dqbuf                   = vidioc_dqbuf,
670
671         .vidioc_streamon                = vidioc_streamon,
672         .vidioc_streamoff               = vidioc_streamoff,
673
674         .vidioc_g_crop                  = vidioc_g_crop,
675         .vidioc_s_crop                  = vidioc_s_crop,
676         .vidioc_cropcap                 = vidioc_cropcap,
677 };
678
679 static struct video_device g2d_videodev = {
680         .name           = G2D_NAME,
681         .fops           = &g2d_fops,
682         .ioctl_ops      = &g2d_ioctl_ops,
683         .minor          = -1,
684         .release        = video_device_release,
685 };
686
687 static struct v4l2_m2m_ops g2d_m2m_ops = {
688         .device_run     = device_run,
689         .job_abort      = job_abort,
690         .lock           = g2d_lock,
691         .unlock         = g2d_unlock,
692 };
693
694 static int g2d_probe(struct platform_device *pdev)
695 {
696         struct g2d_dev *dev;
697         struct video_device *vfd;
698         struct resource *res;
699         int ret = 0;
700
701         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
702         if (!dev)
703                 return -ENOMEM;
704
705         spin_lock_init(&dev->ctrl_lock);
706         mutex_init(&dev->mutex);
707         atomic_set(&dev->num_inst, 0);
708         init_waitqueue_head(&dev->irq_queue);
709
710         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
711
712         dev->regs = devm_request_and_ioremap(&pdev->dev, res);
713         if (dev->regs == NULL) {
714                         dev_err(&pdev->dev, "Failed to obtain io memory\n");
715                         return -ENOENT;
716         }
717
718         dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
719         if (IS_ERR_OR_NULL(dev->clk)) {
720                 dev_err(&pdev->dev, "failed to get g2d clock\n");
721                 return -ENXIO;
722         }
723
724         ret = clk_prepare(dev->clk);
725         if (ret) {
726                 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
727                 goto put_clk;
728         }
729
730         dev->gate = clk_get(&pdev->dev, "fimg2d");
731         if (IS_ERR_OR_NULL(dev->gate)) {
732                 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
733                 ret = -ENXIO;
734                 goto unprep_clk;
735         }
736
737         ret = clk_prepare(dev->gate);
738         if (ret) {
739                 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
740                 goto put_clk_gate;
741         }
742
743         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
744         if (!res) {
745                 dev_err(&pdev->dev, "failed to find IRQ\n");
746                 ret = -ENXIO;
747                 goto unprep_clk_gate;
748         }
749
750         dev->irq = res->start;
751
752         ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
753                                                 0, pdev->name, dev);
754         if (ret) {
755                 dev_err(&pdev->dev, "failed to install IRQ\n");
756                 goto put_clk_gate;
757         }
758
759         dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
760         if (IS_ERR(dev->alloc_ctx)) {
761                 ret = PTR_ERR(dev->alloc_ctx);
762                 goto unprep_clk_gate;
763         }
764
765         ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
766         if (ret)
767                 goto alloc_ctx_cleanup;
768         vfd = video_device_alloc();
769         if (!vfd) {
770                 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
771                 ret = -ENOMEM;
772                 goto unreg_v4l2_dev;
773         }
774         *vfd = g2d_videodev;
775         vfd->lock = &dev->mutex;
776         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
777         if (ret) {
778                 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
779                 goto rel_vdev;
780         }
781         video_set_drvdata(vfd, dev);
782         snprintf(vfd->name, sizeof(vfd->name), "%s", g2d_videodev.name);
783         dev->vfd = vfd;
784         v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
785                                                                 vfd->num);
786         platform_set_drvdata(pdev, dev);
787         dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
788         if (IS_ERR(dev->m2m_dev)) {
789                 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
790                 ret = PTR_ERR(dev->m2m_dev);
791                 goto unreg_video_dev;
792         }
793
794         def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
795
796         return 0;
797
798 unreg_video_dev:
799         video_unregister_device(dev->vfd);
800 rel_vdev:
801         video_device_release(vfd);
802 unreg_v4l2_dev:
803         v4l2_device_unregister(&dev->v4l2_dev);
804 alloc_ctx_cleanup:
805         vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
806 unprep_clk_gate:
807         clk_unprepare(dev->gate);
808 put_clk_gate:
809         clk_put(dev->gate);
810 unprep_clk:
811         clk_unprepare(dev->clk);
812 put_clk:
813         clk_put(dev->clk);
814
815         return ret;
816 }
817
818 static int g2d_remove(struct platform_device *pdev)
819 {
820         struct g2d_dev *dev = (struct g2d_dev *)platform_get_drvdata(pdev);
821
822         v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
823         v4l2_m2m_release(dev->m2m_dev);
824         video_unregister_device(dev->vfd);
825         v4l2_device_unregister(&dev->v4l2_dev);
826         vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
827         clk_unprepare(dev->gate);
828         clk_put(dev->gate);
829         clk_unprepare(dev->clk);
830         clk_put(dev->clk);
831         return 0;
832 }
833
834 static struct platform_driver g2d_pdrv = {
835         .probe          = g2d_probe,
836         .remove         = g2d_remove,
837         .driver         = {
838                 .name = G2D_NAME,
839                 .owner = THIS_MODULE,
840         },
841 };
842
843 module_platform_driver(g2d_pdrv);
844
845 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
846 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
847 MODULE_LICENSE("GPL");