[S390] crash_dump: remove duplicate include
[zen-stable.git] / drivers / media / video / s5p-g2d / g2d.c
blobfebaa673d3635c019011ff174c70eee17818de17
1 /*
2 * Samsung S5P G2D - 2D Graphics Accelerator Driver
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Kamil Debski, <k.debski@samsung.com>
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
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>
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>
29 #include "g2d.h"
30 #include "g2d-regs.h"
32 #define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
34 static struct g2d_fmt formats[] = {
36 .name = "XRGB_8888",
37 .fourcc = V4L2_PIX_FMT_RGB32,
38 .depth = 32,
39 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
42 .name = "RGB_565",
43 .fourcc = V4L2_PIX_FMT_RGB565X,
44 .depth = 16,
45 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
48 .name = "XRGB_1555",
49 .fourcc = V4L2_PIX_FMT_RGB555X,
50 .depth = 16,
51 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
54 .name = "XRGB_4444",
55 .fourcc = V4L2_PIX_FMT_RGB444,
56 .depth = 16,
57 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
60 .name = "PACKED_RGB_888",
61 .fourcc = V4L2_PIX_FMT_RGB24,
62 .depth = 24,
63 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
66 #define NUM_FORMATS ARRAY_SIZE(formats)
68 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,
80 struct g2d_fmt *find_fmt(struct v4l2_format *f)
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];
87 return NULL;
91 static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
92 enum v4l2_buf_type type)
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);
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[])
108 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
109 struct g2d_frame *f = get_frame(ctx, vq->type);
111 if (IS_ERR(f))
112 return PTR_ERR(f);
114 sizes[0] = f->size;
115 *nplanes = 1;
116 alloc_ctxs[0] = ctx->dev->alloc_ctx;
118 if (*nbuffers == 0)
119 *nbuffers = 1;
121 return 0;
124 static int g2d_buf_prepare(struct vb2_buffer *vb)
126 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
127 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
129 if (IS_ERR(f))
130 return PTR_ERR(f);
131 vb2_set_plane_payload(vb, 0, f->size);
132 return 0;
135 static void g2d_buf_queue(struct vb2_buffer *vb)
137 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
138 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
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,
148 static int queue_init(void *priv, struct vb2_queue *src_vq,
149 struct vb2_queue *dst_vq)
151 struct g2d_ctx *ctx = priv;
152 int ret;
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);
162 ret = vb2_queue_init(src_vq);
163 if (ret)
164 return ret;
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);
174 return vb2_queue_init(dst_vq);
177 static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
179 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
180 ctrl_handler);
181 switch (ctrl->id) {
182 case V4L2_CID_COLORFX:
183 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
184 ctx->rop = ROP4_INVERT;
185 else
186 ctx->rop = ROP4_COPY;
187 break;
188 default:
189 v4l2_err(&ctx->dev->v4l2_dev, "unknown control\n");
190 return -EINVAL;
192 return 0;
195 static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
196 .s_ctrl = g2d_s_ctrl,
199 int g2d_setup_ctrls(struct g2d_ctx *ctx)
201 struct g2d_dev *dev = ctx->dev;
203 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 1);
204 if (ctx->ctrl_handler.error) {
205 v4l2_err(&dev->v4l2_dev, "v4l2_ctrl_handler_init failed\n");
206 return ctx->ctrl_handler.error;
209 v4l2_ctrl_new_std_menu(
210 &ctx->ctrl_handler,
211 &g2d_ctrl_ops,
212 V4L2_CID_COLORFX,
213 V4L2_COLORFX_NEGATIVE,
214 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
215 V4L2_COLORFX_NONE);
217 if (ctx->ctrl_handler.error) {
218 v4l2_err(&dev->v4l2_dev, "v4l2_ctrl_handler_init failed\n");
219 return ctx->ctrl_handler.error;
222 return 0;
225 static int g2d_open(struct file *file)
227 struct g2d_dev *dev = video_drvdata(file);
228 struct g2d_ctx *ctx = NULL;
229 int ret = 0;
231 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
232 if (!ctx)
233 return -ENOMEM;
234 ctx->dev = dev;
235 /* Set default formats */
236 ctx->in = def_frame;
237 ctx->out = def_frame;
239 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
240 if (IS_ERR(ctx->m2m_ctx)) {
241 ret = PTR_ERR(ctx->m2m_ctx);
242 kfree(ctx);
243 return ret;
245 v4l2_fh_init(&ctx->fh, video_devdata(file));
246 file->private_data = &ctx->fh;
247 v4l2_fh_add(&ctx->fh);
249 g2d_setup_ctrls(ctx);
251 /* Write the default values to the ctx struct */
252 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
254 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
256 v4l2_info(&dev->v4l2_dev, "instance opened\n");
257 return 0;
260 static int g2d_release(struct file *file)
262 struct g2d_dev *dev = video_drvdata(file);
263 struct g2d_ctx *ctx = fh2ctx(file->private_data);
265 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
266 v4l2_fh_del(&ctx->fh);
267 v4l2_fh_exit(&ctx->fh);
268 kfree(ctx);
269 v4l2_info(&dev->v4l2_dev, "instance closed\n");
270 return 0;
274 static int vidioc_querycap(struct file *file, void *priv,
275 struct v4l2_capability *cap)
277 strncpy(cap->driver, G2D_NAME, sizeof(cap->driver) - 1);
278 strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1);
279 cap->bus_info[0] = 0;
280 cap->version = KERNEL_VERSION(1, 0, 0);
281 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
282 | V4L2_CAP_STREAMING;
283 return 0;
286 static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
288 struct g2d_fmt *fmt;
289 if (f->index >= NUM_FORMATS)
290 return -EINVAL;
291 fmt = &formats[f->index];
292 f->pixelformat = fmt->fourcc;
293 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
294 return 0;
297 static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
299 struct g2d_ctx *ctx = prv;
300 struct vb2_queue *vq;
301 struct g2d_frame *frm;
303 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
304 if (!vq)
305 return -EINVAL;
306 frm = get_frame(ctx, f->type);
307 if (IS_ERR(frm))
308 return PTR_ERR(frm);
310 f->fmt.pix.width = frm->width;
311 f->fmt.pix.height = frm->height;
312 f->fmt.pix.field = V4L2_FIELD_NONE;
313 f->fmt.pix.pixelformat = frm->fmt->fourcc;
314 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
315 f->fmt.pix.sizeimage = frm->size;
316 return 0;
319 static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
321 struct g2d_fmt *fmt;
322 enum v4l2_field *field;
324 fmt = find_fmt(f);
325 if (!fmt)
326 return -EINVAL;
328 field = &f->fmt.pix.field;
329 if (*field == V4L2_FIELD_ANY)
330 *field = V4L2_FIELD_NONE;
331 else if (*field != V4L2_FIELD_NONE)
332 return -EINVAL;
334 if (f->fmt.pix.width > MAX_WIDTH)
335 f->fmt.pix.width = MAX_WIDTH;
336 if (f->fmt.pix.height > MAX_HEIGHT)
337 f->fmt.pix.height = MAX_HEIGHT;
339 if (f->fmt.pix.width < 1)
340 f->fmt.pix.width = 1;
341 if (f->fmt.pix.height < 1)
342 f->fmt.pix.height = 1;
344 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
345 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
346 return 0;
349 static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
351 struct g2d_ctx *ctx = prv;
352 struct g2d_dev *dev = ctx->dev;
353 struct vb2_queue *vq;
354 struct g2d_frame *frm;
355 struct g2d_fmt *fmt;
356 int ret = 0;
358 /* Adjust all values accordingly to the hardware capabilities
359 * and chosen format. */
360 ret = vidioc_try_fmt(file, prv, f);
361 if (ret)
362 return ret;
363 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
364 if (vb2_is_busy(vq)) {
365 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
366 return -EBUSY;
368 frm = get_frame(ctx, f->type);
369 if (IS_ERR(frm))
370 return PTR_ERR(frm);
371 fmt = find_fmt(f);
372 if (!fmt)
373 return -EINVAL;
374 frm->width = f->fmt.pix.width;
375 frm->height = f->fmt.pix.height;
376 frm->size = f->fmt.pix.sizeimage;
377 /* Reset crop settings */
378 frm->o_width = 0;
379 frm->o_height = 0;
380 frm->c_width = frm->width;
381 frm->c_height = frm->height;
382 frm->right = frm->width;
383 frm->bottom = frm->height;
384 frm->fmt = fmt;
385 frm->stride = f->fmt.pix.bytesperline;
386 return 0;
389 static unsigned int g2d_poll(struct file *file, struct poll_table_struct *wait)
391 struct g2d_ctx *ctx = fh2ctx(file->private_data);
392 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
395 static int g2d_mmap(struct file *file, struct vm_area_struct *vma)
397 struct g2d_ctx *ctx = fh2ctx(file->private_data);
398 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
401 static int vidioc_reqbufs(struct file *file, void *priv,
402 struct v4l2_requestbuffers *reqbufs)
404 struct g2d_ctx *ctx = priv;
405 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
408 static int vidioc_querybuf(struct file *file, void *priv,
409 struct v4l2_buffer *buf)
411 struct g2d_ctx *ctx = priv;
412 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
415 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
417 struct g2d_ctx *ctx = priv;
418 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
421 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
423 struct g2d_ctx *ctx = priv;
424 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
428 static int vidioc_streamon(struct file *file, void *priv,
429 enum v4l2_buf_type type)
431 struct g2d_ctx *ctx = priv;
432 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
435 static int vidioc_streamoff(struct file *file, void *priv,
436 enum v4l2_buf_type type)
438 struct g2d_ctx *ctx = priv;
439 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
442 static int vidioc_cropcap(struct file *file, void *priv,
443 struct v4l2_cropcap *cr)
445 struct g2d_ctx *ctx = priv;
446 struct g2d_frame *f;
448 f = get_frame(ctx, cr->type);
449 if (IS_ERR(f))
450 return PTR_ERR(f);
452 cr->bounds.left = 0;
453 cr->bounds.top = 0;
454 cr->bounds.width = f->width;
455 cr->bounds.height = f->height;
456 cr->defrect = cr->bounds;
457 return 0;
460 static int vidioc_g_crop(struct file *file, void *prv, struct v4l2_crop *cr)
462 struct g2d_ctx *ctx = prv;
463 struct g2d_frame *f;
465 f = get_frame(ctx, cr->type);
466 if (IS_ERR(f))
467 return PTR_ERR(f);
469 cr->c.left = f->o_height;
470 cr->c.top = f->o_width;
471 cr->c.width = f->c_width;
472 cr->c.height = f->c_height;
473 return 0;
476 static int vidioc_try_crop(struct file *file, void *prv, struct v4l2_crop *cr)
478 struct g2d_ctx *ctx = prv;
479 struct g2d_dev *dev = ctx->dev;
480 struct g2d_frame *f;
482 f = get_frame(ctx, cr->type);
483 if (IS_ERR(f))
484 return PTR_ERR(f);
486 if (cr->c.top < 0 || cr->c.left < 0) {
487 v4l2_err(&dev->v4l2_dev,
488 "doesn't support negative values for top & left\n");
489 return -EINVAL;
492 return 0;
495 static int vidioc_s_crop(struct file *file, void *prv, struct v4l2_crop *cr)
497 struct g2d_ctx *ctx = prv;
498 struct g2d_frame *f;
499 int ret;
501 ret = vidioc_try_crop(file, prv, cr);
502 if (ret)
503 return ret;
504 f = get_frame(ctx, cr->type);
505 if (IS_ERR(f))
506 return PTR_ERR(f);
508 f->c_width = cr->c.width;
509 f->c_height = cr->c.height;
510 f->o_width = cr->c.left;
511 f->o_height = cr->c.top;
512 f->bottom = f->o_height + f->c_height;
513 f->right = f->o_width + f->c_width;
514 return 0;
517 static void g2d_lock(void *prv)
519 struct g2d_ctx *ctx = prv;
520 struct g2d_dev *dev = ctx->dev;
521 mutex_lock(&dev->mutex);
524 static void g2d_unlock(void *prv)
526 struct g2d_ctx *ctx = prv;
527 struct g2d_dev *dev = ctx->dev;
528 mutex_unlock(&dev->mutex);
531 static void job_abort(void *prv)
533 struct g2d_ctx *ctx = prv;
534 struct g2d_dev *dev = ctx->dev;
535 int ret;
537 if (dev->curr == 0) /* No job currently running */
538 return;
540 ret = wait_event_timeout(dev->irq_queue,
541 dev->curr == 0,
542 msecs_to_jiffies(G2D_TIMEOUT));
545 static void device_run(void *prv)
547 struct g2d_ctx *ctx = prv;
548 struct g2d_dev *dev = ctx->dev;
549 struct vb2_buffer *src, *dst;
550 u32 cmd = 0;
552 dev->curr = ctx;
554 src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
555 dst = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
557 clk_enable(dev->gate);
558 g2d_reset(dev);
560 g2d_set_src_size(dev, &ctx->in);
561 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(src, 0));
563 g2d_set_dst_size(dev, &ctx->out);
564 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(dst, 0));
566 g2d_set_rop4(dev, ctx->rop);
567 if (ctx->in.c_width != ctx->out.c_width ||
568 ctx->in.c_height != ctx->out.c_height)
569 cmd |= g2d_cmd_stretch(1);
570 g2d_set_cmd(dev, cmd);
571 g2d_start(dev);
574 static irqreturn_t g2d_isr(int irq, void *prv)
576 struct g2d_dev *dev = prv;
577 struct g2d_ctx *ctx = dev->curr;
578 struct vb2_buffer *src, *dst;
580 g2d_clear_int(dev);
581 clk_disable(dev->gate);
583 BUG_ON(ctx == 0);
585 src = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
586 dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
588 BUG_ON(src == 0);
589 BUG_ON(dst == 0);
591 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
592 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
593 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
595 dev->curr = 0;
596 wake_up(&dev->irq_queue);
597 return IRQ_HANDLED;
600 static const struct v4l2_file_operations g2d_fops = {
601 .owner = THIS_MODULE,
602 .open = g2d_open,
603 .release = g2d_release,
604 .poll = g2d_poll,
605 .unlocked_ioctl = video_ioctl2,
606 .mmap = g2d_mmap,
609 static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
610 .vidioc_querycap = vidioc_querycap,
612 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
613 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
614 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
615 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
617 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
618 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
619 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
620 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
622 .vidioc_reqbufs = vidioc_reqbufs,
623 .vidioc_querybuf = vidioc_querybuf,
625 .vidioc_qbuf = vidioc_qbuf,
626 .vidioc_dqbuf = vidioc_dqbuf,
628 .vidioc_streamon = vidioc_streamon,
629 .vidioc_streamoff = vidioc_streamoff,
631 .vidioc_g_crop = vidioc_g_crop,
632 .vidioc_s_crop = vidioc_s_crop,
633 .vidioc_cropcap = vidioc_cropcap,
636 static struct video_device g2d_videodev = {
637 .name = G2D_NAME,
638 .fops = &g2d_fops,
639 .ioctl_ops = &g2d_ioctl_ops,
640 .minor = -1,
641 .release = video_device_release,
644 static struct v4l2_m2m_ops g2d_m2m_ops = {
645 .device_run = device_run,
646 .job_abort = job_abort,
647 .lock = g2d_lock,
648 .unlock = g2d_unlock,
651 static int g2d_probe(struct platform_device *pdev)
653 struct g2d_dev *dev;
654 struct video_device *vfd;
655 struct resource *res;
656 int ret = 0;
658 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
659 if (!dev)
660 return -ENOMEM;
661 spin_lock_init(&dev->irqlock);
662 mutex_init(&dev->mutex);
663 atomic_set(&dev->num_inst, 0);
664 init_waitqueue_head(&dev->irq_queue);
666 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
667 if (!res) {
668 dev_err(&pdev->dev, "failed to find registers\n");
669 ret = -ENOENT;
670 goto free_dev;
673 dev->res_regs = request_mem_region(res->start, resource_size(res),
674 dev_name(&pdev->dev));
676 if (!dev->res_regs) {
677 dev_err(&pdev->dev, "failed to obtain register region\n");
678 ret = -ENOENT;
679 goto free_dev;
682 dev->regs = ioremap(res->start, resource_size(res));
683 if (!dev->regs) {
684 dev_err(&pdev->dev, "failed to map registers\n");
685 ret = -ENOENT;
686 goto rel_res_regs;
689 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
690 if (IS_ERR_OR_NULL(dev->clk)) {
691 dev_err(&pdev->dev, "failed to get g2d clock\n");
692 ret = -ENXIO;
693 goto unmap_regs;
696 dev->gate = clk_get(&pdev->dev, "fimg2d");
697 if (IS_ERR_OR_NULL(dev->gate)) {
698 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
699 ret = -ENXIO;
700 goto put_clk;
703 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
704 if (!res) {
705 dev_err(&pdev->dev, "failed to find IRQ\n");
706 ret = -ENXIO;
707 goto put_clk_gate;
710 dev->irq = res->start;
712 ret = request_irq(dev->irq, g2d_isr, 0, pdev->name, dev);
713 if (ret) {
714 dev_err(&pdev->dev, "failed to install IRQ\n");
715 goto put_clk_gate;
718 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
719 if (IS_ERR(dev->alloc_ctx)) {
720 ret = PTR_ERR(dev->alloc_ctx);
721 goto rel_irq;
724 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
725 if (ret)
726 goto alloc_ctx_cleanup;
727 vfd = video_device_alloc();
728 if (!vfd) {
729 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
730 ret = -ENOMEM;
731 goto unreg_v4l2_dev;
733 *vfd = g2d_videodev;
734 vfd->lock = &dev->mutex;
735 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
736 if (ret) {
737 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
738 goto rel_vdev;
740 video_set_drvdata(vfd, dev);
741 snprintf(vfd->name, sizeof(vfd->name), "%s", g2d_videodev.name);
742 dev->vfd = vfd;
743 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
744 vfd->num);
745 platform_set_drvdata(pdev, dev);
746 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
747 if (IS_ERR(dev->m2m_dev)) {
748 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
749 ret = PTR_ERR(dev->m2m_dev);
750 goto unreg_video_dev;
753 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
755 return 0;
757 unreg_video_dev:
758 video_unregister_device(dev->vfd);
759 rel_vdev:
760 video_device_release(vfd);
761 unreg_v4l2_dev:
762 v4l2_device_unregister(&dev->v4l2_dev);
763 alloc_ctx_cleanup:
764 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
765 rel_irq:
766 free_irq(dev->irq, dev);
767 put_clk_gate:
768 clk_put(dev->gate);
769 put_clk:
770 clk_put(dev->clk);
771 unmap_regs:
772 iounmap(dev->regs);
773 rel_res_regs:
774 release_resource(dev->res_regs);
775 free_dev:
776 kfree(dev);
777 return ret;
780 static int g2d_remove(struct platform_device *pdev)
782 struct g2d_dev *dev = (struct g2d_dev *)platform_get_drvdata(pdev);
784 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
785 v4l2_m2m_release(dev->m2m_dev);
786 video_unregister_device(dev->vfd);
787 v4l2_device_unregister(&dev->v4l2_dev);
788 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
789 free_irq(dev->irq, dev);
790 clk_put(dev->gate);
791 clk_put(dev->clk);
792 iounmap(dev->regs);
793 release_resource(dev->res_regs);
794 kfree(dev);
795 return 0;
798 static struct platform_driver g2d_pdrv = {
799 .probe = g2d_probe,
800 .remove = g2d_remove,
801 .driver = {
802 .name = G2D_NAME,
803 .owner = THIS_MODULE,
807 module_platform_driver(g2d_pdrv);
809 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
810 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
811 MODULE_LICENSE("GPL");