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