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>
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>
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>
32 #define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
34 static struct g2d_fmt formats[] = {
37 .fourcc = V4L2_PIX_FMT_RGB32,
39 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
43 .fourcc = V4L2_PIX_FMT_RGB565X,
45 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
49 .fourcc = V4L2_PIX_FMT_RGB555X,
51 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
55 .fourcc = V4L2_PIX_FMT_RGB444,
57 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
60 .name = "PACKED_RGB_888",
61 .fourcc = V4L2_PIX_FMT_RGB24,
63 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
66 #define NUM_FORMATS ARRAY_SIZE(formats)
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,
76 .right = DEFAULT_WIDTH,
77 .bottom = DEFAULT_HEIGHT,
80 static struct g2d_fmt *find_fmt(struct v4l2_format *f)
83 for (i = 0; i < NUM_FORMATS; i++) {
84 if (formats[i].fourcc == f->fmt.pix.pixelformat)
91 static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
92 enum v4l2_buf_type type)
95 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
97 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
100 return ERR_PTR(-EINVAL);
104 static int g2d_queue_setup(struct vb2_queue *vq,
105 unsigned int *nbuffers, unsigned int *nplanes,
106 unsigned int sizes[], struct device *alloc_devs[])
108 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
109 struct g2d_frame *f = get_frame(ctx, vq->type);
123 static int g2d_buf_prepare(struct vb2_buffer *vb)
125 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
126 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
130 vb2_set_plane_payload(vb, 0, f->size);
134 static void g2d_buf_queue(struct vb2_buffer *vb)
136 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
137 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
138 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
141 static const struct vb2_ops g2d_qops = {
142 .queue_setup = g2d_queue_setup,
143 .buf_prepare = g2d_buf_prepare,
144 .buf_queue = g2d_buf_queue,
147 static int queue_init(void *priv, struct vb2_queue *src_vq,
148 struct vb2_queue *dst_vq)
150 struct g2d_ctx *ctx = priv;
153 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
154 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
155 src_vq->drv_priv = ctx;
156 src_vq->ops = &g2d_qops;
157 src_vq->mem_ops = &vb2_dma_contig_memops;
158 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
159 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
160 src_vq->lock = &ctx->dev->mutex;
161 src_vq->dev = ctx->dev->v4l2_dev.dev;
163 ret = vb2_queue_init(src_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 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
174 dst_vq->lock = &ctx->dev->mutex;
175 dst_vq->dev = ctx->dev->v4l2_dev.dev;
177 return vb2_queue_init(dst_vq);
180 static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
182 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
186 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
188 case V4L2_CID_COLORFX:
189 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
190 ctx->rop = ROP4_INVERT;
192 ctx->rop = ROP4_COPY;
196 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
200 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
204 static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
205 .s_ctrl = g2d_s_ctrl,
208 static int g2d_setup_ctrls(struct g2d_ctx *ctx)
210 struct g2d_dev *dev = ctx->dev;
212 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
214 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
215 V4L2_CID_HFLIP, 0, 1, 1, 0);
217 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
218 V4L2_CID_VFLIP, 0, 1, 1, 0);
220 v4l2_ctrl_new_std_menu(
224 V4L2_COLORFX_NEGATIVE,
225 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
228 if (ctx->ctrl_handler.error) {
229 int err = ctx->ctrl_handler.error;
230 v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
231 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
235 v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
240 static int g2d_open(struct file *file)
242 struct g2d_dev *dev = video_drvdata(file);
243 struct g2d_ctx *ctx = NULL;
246 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
250 /* Set default formats */
252 ctx->out = def_frame;
254 if (mutex_lock_interruptible(&dev->mutex)) {
258 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
259 if (IS_ERR(ctx->fh.m2m_ctx)) {
260 ret = PTR_ERR(ctx->fh.m2m_ctx);
261 mutex_unlock(&dev->mutex);
265 v4l2_fh_init(&ctx->fh, video_devdata(file));
266 file->private_data = &ctx->fh;
267 v4l2_fh_add(&ctx->fh);
269 g2d_setup_ctrls(ctx);
271 /* Write the default values to the ctx struct */
272 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
274 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
275 mutex_unlock(&dev->mutex);
277 v4l2_info(&dev->v4l2_dev, "instance opened\n");
281 static int g2d_release(struct file *file)
283 struct g2d_dev *dev = video_drvdata(file);
284 struct g2d_ctx *ctx = fh2ctx(file->private_data);
286 mutex_lock(&dev->mutex);
287 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
288 mutex_unlock(&dev->mutex);
289 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
290 v4l2_fh_del(&ctx->fh);
291 v4l2_fh_exit(&ctx->fh);
293 v4l2_info(&dev->v4l2_dev, "instance closed\n");
298 static int vidioc_querycap(struct file *file, void *priv,
299 struct v4l2_capability *cap)
301 strncpy(cap->driver, G2D_NAME, sizeof(cap->driver) - 1);
302 strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1);
303 cap->bus_info[0] = 0;
304 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
305 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
309 static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
312 if (f->index >= NUM_FORMATS)
314 fmt = &formats[f->index];
315 f->pixelformat = fmt->fourcc;
316 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
320 static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
322 struct g2d_ctx *ctx = prv;
323 struct vb2_queue *vq;
324 struct g2d_frame *frm;
326 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
329 frm = get_frame(ctx, f->type);
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;
342 static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
345 enum v4l2_field *field;
351 field = &f->fmt.pix.field;
352 if (*field == V4L2_FIELD_ANY)
353 *field = V4L2_FIELD_NONE;
354 else if (*field != V4L2_FIELD_NONE)
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;
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;
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;
372 static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
374 struct g2d_ctx *ctx = prv;
375 struct g2d_dev *dev = ctx->dev;
376 struct vb2_queue *vq;
377 struct g2d_frame *frm;
381 /* Adjust all values accordingly to the hardware capabilities
382 * and chosen format. */
383 ret = vidioc_try_fmt(file, prv, f);
386 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
387 if (vb2_is_busy(vq)) {
388 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
391 frm = get_frame(ctx, f->type);
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 */
403 frm->c_width = frm->width;
404 frm->c_height = frm->height;
405 frm->right = frm->width;
406 frm->bottom = frm->height;
408 frm->stride = f->fmt.pix.bytesperline;
412 static int vidioc_cropcap(struct file *file, void *priv,
413 struct v4l2_cropcap *cr)
415 struct g2d_ctx *ctx = priv;
418 f = get_frame(ctx, cr->type);
424 cr->bounds.width = f->width;
425 cr->bounds.height = f->height;
426 cr->defrect = cr->bounds;
430 static int vidioc_g_crop(struct file *file, void *prv, struct v4l2_crop *cr)
432 struct g2d_ctx *ctx = prv;
435 f = get_frame(ctx, cr->type);
439 cr->c.left = f->o_height;
440 cr->c.top = f->o_width;
441 cr->c.width = f->c_width;
442 cr->c.height = f->c_height;
446 static int vidioc_try_crop(struct file *file, void *prv, const struct v4l2_crop *cr)
448 struct g2d_ctx *ctx = prv;
449 struct g2d_dev *dev = ctx->dev;
452 f = get_frame(ctx, cr->type);
456 if (cr->c.top < 0 || cr->c.left < 0) {
457 v4l2_err(&dev->v4l2_dev,
458 "doesn't support negative values for top & left\n");
465 static int vidioc_s_crop(struct file *file, void *prv, const struct v4l2_crop *cr)
467 struct g2d_ctx *ctx = prv;
471 ret = vidioc_try_crop(file, prv, cr);
474 f = get_frame(ctx, cr->type);
478 f->c_width = cr->c.width;
479 f->c_height = cr->c.height;
480 f->o_width = cr->c.left;
481 f->o_height = cr->c.top;
482 f->bottom = f->o_height + f->c_height;
483 f->right = f->o_width + f->c_width;
487 static void job_abort(void *prv)
489 struct g2d_ctx *ctx = prv;
490 struct g2d_dev *dev = ctx->dev;
492 if (dev->curr == NULL) /* No job currently running */
495 wait_event_timeout(dev->irq_queue,
497 msecs_to_jiffies(G2D_TIMEOUT));
500 static void device_run(void *prv)
502 struct g2d_ctx *ctx = prv;
503 struct g2d_dev *dev = ctx->dev;
504 struct vb2_v4l2_buffer *src, *dst;
510 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
511 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
513 clk_enable(dev->gate);
516 spin_lock_irqsave(&dev->ctrl_lock, flags);
518 g2d_set_src_size(dev, &ctx->in);
519 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
521 g2d_set_dst_size(dev, &ctx->out);
522 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
524 g2d_set_rop4(dev, ctx->rop);
525 g2d_set_flip(dev, ctx->flip);
527 if (ctx->in.c_width != ctx->out.c_width ||
528 ctx->in.c_height != ctx->out.c_height) {
529 if (dev->variant->hw_rev == TYPE_G2D_3X)
530 cmd |= CMD_V3_ENABLE_STRETCH;
532 g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
535 g2d_set_cmd(dev, cmd);
538 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
541 static irqreturn_t g2d_isr(int irq, void *prv)
543 struct g2d_dev *dev = prv;
544 struct g2d_ctx *ctx = dev->curr;
545 struct vb2_v4l2_buffer *src, *dst;
548 clk_disable(dev->gate);
552 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
553 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
558 dst->timecode = src->timecode;
559 dst->vb2_buf.timestamp = src->vb2_buf.timestamp;
560 dst->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
562 src->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
564 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
565 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
566 v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
569 wake_up(&dev->irq_queue);
573 static const struct v4l2_file_operations g2d_fops = {
574 .owner = THIS_MODULE,
576 .release = g2d_release,
577 .poll = v4l2_m2m_fop_poll,
578 .unlocked_ioctl = video_ioctl2,
579 .mmap = v4l2_m2m_fop_mmap,
582 static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
583 .vidioc_querycap = vidioc_querycap,
585 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
586 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
587 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
588 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
590 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
591 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
592 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
593 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
595 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
596 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
597 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
598 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
600 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
601 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
603 .vidioc_g_crop = vidioc_g_crop,
604 .vidioc_s_crop = vidioc_s_crop,
605 .vidioc_cropcap = vidioc_cropcap,
608 static struct video_device g2d_videodev = {
611 .ioctl_ops = &g2d_ioctl_ops,
613 .release = video_device_release,
614 .vfl_dir = VFL_DIR_M2M,
617 static struct v4l2_m2m_ops g2d_m2m_ops = {
618 .device_run = device_run,
619 .job_abort = job_abort,
622 static const struct of_device_id exynos_g2d_match[];
624 static int g2d_probe(struct platform_device *pdev)
627 struct video_device *vfd;
628 struct resource *res;
629 const struct of_device_id *of_id;
632 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
636 spin_lock_init(&dev->ctrl_lock);
637 mutex_init(&dev->mutex);
638 atomic_set(&dev->num_inst, 0);
639 init_waitqueue_head(&dev->irq_queue);
641 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
643 dev->regs = devm_ioremap_resource(&pdev->dev, res);
644 if (IS_ERR(dev->regs))
645 return PTR_ERR(dev->regs);
647 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
648 if (IS_ERR(dev->clk)) {
649 dev_err(&pdev->dev, "failed to get g2d clock\n");
653 ret = clk_prepare(dev->clk);
655 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
659 dev->gate = clk_get(&pdev->dev, "fimg2d");
660 if (IS_ERR(dev->gate)) {
661 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
666 ret = clk_prepare(dev->gate);
668 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
672 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
674 dev_err(&pdev->dev, "failed to find IRQ\n");
676 goto unprep_clk_gate;
679 dev->irq = res->start;
681 ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
684 dev_err(&pdev->dev, "failed to install IRQ\n");
688 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
690 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
692 goto unprep_clk_gate;
693 vfd = video_device_alloc();
695 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
700 vfd->lock = &dev->mutex;
701 vfd->v4l2_dev = &dev->v4l2_dev;
702 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
704 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
707 video_set_drvdata(vfd, dev);
708 snprintf(vfd->name, sizeof(vfd->name), "%s", g2d_videodev.name);
710 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
712 platform_set_drvdata(pdev, dev);
713 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
714 if (IS_ERR(dev->m2m_dev)) {
715 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
716 ret = PTR_ERR(dev->m2m_dev);
717 goto unreg_video_dev;
720 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
722 of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
725 goto unreg_video_dev;
727 dev->variant = (struct g2d_variant *)of_id->data;
732 video_unregister_device(dev->vfd);
734 video_device_release(vfd);
736 v4l2_device_unregister(&dev->v4l2_dev);
738 clk_unprepare(dev->gate);
742 clk_unprepare(dev->clk);
749 static int g2d_remove(struct platform_device *pdev)
751 struct g2d_dev *dev = platform_get_drvdata(pdev);
753 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
754 v4l2_m2m_release(dev->m2m_dev);
755 video_unregister_device(dev->vfd);
756 v4l2_device_unregister(&dev->v4l2_dev);
757 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
758 clk_unprepare(dev->gate);
760 clk_unprepare(dev->clk);
765 static struct g2d_variant g2d_drvdata_v3x = {
766 .hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
769 static struct g2d_variant g2d_drvdata_v4x = {
770 .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
773 static const struct of_device_id exynos_g2d_match[] = {
775 .compatible = "samsung,s5pv210-g2d",
776 .data = &g2d_drvdata_v3x,
778 .compatible = "samsung,exynos4212-g2d",
779 .data = &g2d_drvdata_v4x,
783 MODULE_DEVICE_TABLE(of, exynos_g2d_match);
785 static struct platform_driver g2d_pdrv = {
787 .remove = g2d_remove,
790 .of_match_table = exynos_g2d_match,
794 module_platform_driver(g2d_pdrv);
796 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
797 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
798 MODULE_LICENSE("GPL");