1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Samsung S5P G2D - 2D Graphics Accelerator Driver
5 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
6 * Kamil Debski, <k.debski@samsung.com>
9 #include <linux/module.h>
11 #include <linux/timer.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/clk.h>
15 #include <linux/interrupt.h>
18 #include <linux/platform_device.h>
19 #include <media/v4l2-mem2mem.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-v4l2.h>
23 #include <media/videobuf2-dma-contig.h>
28 #define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
30 static struct g2d_fmt formats[] = {
32 .fourcc = V4L2_PIX_FMT_RGB32,
34 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
37 .fourcc = V4L2_PIX_FMT_RGB565X,
39 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
42 .fourcc = V4L2_PIX_FMT_RGB555X,
44 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
47 .fourcc = V4L2_PIX_FMT_RGB444,
49 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
52 .fourcc = V4L2_PIX_FMT_RGB24,
54 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
57 #define NUM_FORMATS ARRAY_SIZE(formats)
59 static struct g2d_frame def_frame = {
60 .width = DEFAULT_WIDTH,
61 .height = DEFAULT_HEIGHT,
62 .c_width = DEFAULT_WIDTH,
63 .c_height = DEFAULT_HEIGHT,
67 .right = DEFAULT_WIDTH,
68 .bottom = DEFAULT_HEIGHT,
71 static struct g2d_fmt *find_fmt(struct v4l2_format *f)
74 for (i = 0; i < NUM_FORMATS; i++) {
75 if (formats[i].fourcc == f->fmt.pix.pixelformat)
82 static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
83 enum v4l2_buf_type type)
86 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
88 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
91 return ERR_PTR(-EINVAL);
95 static int g2d_queue_setup(struct vb2_queue *vq,
96 unsigned int *nbuffers, unsigned int *nplanes,
97 unsigned int sizes[], struct device *alloc_devs[])
99 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
100 struct g2d_frame *f = get_frame(ctx, vq->type);
114 static int g2d_buf_prepare(struct vb2_buffer *vb)
116 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
117 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
121 vb2_set_plane_payload(vb, 0, f->size);
125 static void g2d_buf_queue(struct vb2_buffer *vb)
127 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
128 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
129 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
132 static const struct vb2_ops g2d_qops = {
133 .queue_setup = g2d_queue_setup,
134 .buf_prepare = g2d_buf_prepare,
135 .buf_queue = g2d_buf_queue,
136 .wait_prepare = vb2_ops_wait_prepare,
137 .wait_finish = vb2_ops_wait_finish,
140 static int queue_init(void *priv, struct vb2_queue *src_vq,
141 struct vb2_queue *dst_vq)
143 struct g2d_ctx *ctx = priv;
146 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
147 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
148 src_vq->drv_priv = ctx;
149 src_vq->ops = &g2d_qops;
150 src_vq->mem_ops = &vb2_dma_contig_memops;
151 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
152 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
153 src_vq->lock = &ctx->dev->mutex;
154 src_vq->dev = ctx->dev->v4l2_dev.dev;
156 ret = vb2_queue_init(src_vq);
160 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
161 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
162 dst_vq->drv_priv = ctx;
163 dst_vq->ops = &g2d_qops;
164 dst_vq->mem_ops = &vb2_dma_contig_memops;
165 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
166 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
167 dst_vq->lock = &ctx->dev->mutex;
168 dst_vq->dev = ctx->dev->v4l2_dev.dev;
170 return vb2_queue_init(dst_vq);
173 static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
175 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
179 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
181 case V4L2_CID_COLORFX:
182 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
183 ctx->rop = ROP4_INVERT;
185 ctx->rop = ROP4_COPY;
189 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
193 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
197 static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
198 .s_ctrl = g2d_s_ctrl,
201 static int g2d_setup_ctrls(struct g2d_ctx *ctx)
203 struct g2d_dev *dev = ctx->dev;
205 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
207 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
208 V4L2_CID_HFLIP, 0, 1, 1, 0);
210 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
211 V4L2_CID_VFLIP, 0, 1, 1, 0);
213 v4l2_ctrl_new_std_menu(
217 V4L2_COLORFX_NEGATIVE,
218 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
221 if (ctx->ctrl_handler.error) {
222 int err = ctx->ctrl_handler.error;
223 v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
224 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
228 v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
233 static int g2d_open(struct file *file)
235 struct g2d_dev *dev = video_drvdata(file);
236 struct g2d_ctx *ctx = NULL;
239 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
243 /* Set default formats */
245 ctx->out = def_frame;
247 if (mutex_lock_interruptible(&dev->mutex)) {
251 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
252 if (IS_ERR(ctx->fh.m2m_ctx)) {
253 ret = PTR_ERR(ctx->fh.m2m_ctx);
254 mutex_unlock(&dev->mutex);
258 v4l2_fh_init(&ctx->fh, video_devdata(file));
259 file->private_data = &ctx->fh;
260 v4l2_fh_add(&ctx->fh);
262 g2d_setup_ctrls(ctx);
264 /* Write the default values to the ctx struct */
265 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
267 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
268 mutex_unlock(&dev->mutex);
270 v4l2_info(&dev->v4l2_dev, "instance opened\n");
274 static int g2d_release(struct file *file)
276 struct g2d_dev *dev = video_drvdata(file);
277 struct g2d_ctx *ctx = fh2ctx(file->private_data);
279 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
280 v4l2_fh_del(&ctx->fh);
281 v4l2_fh_exit(&ctx->fh);
283 v4l2_info(&dev->v4l2_dev, "instance closed\n");
288 static int vidioc_querycap(struct file *file, void *priv,
289 struct v4l2_capability *cap)
291 strscpy(cap->driver, G2D_NAME, sizeof(cap->driver));
292 strscpy(cap->card, G2D_NAME, sizeof(cap->card));
293 cap->bus_info[0] = 0;
297 static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
299 if (f->index >= NUM_FORMATS)
301 f->pixelformat = formats[f->index].fourcc;
305 static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
307 struct g2d_ctx *ctx = prv;
308 struct vb2_queue *vq;
309 struct g2d_frame *frm;
311 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
314 frm = get_frame(ctx, f->type);
318 f->fmt.pix.width = frm->width;
319 f->fmt.pix.height = frm->height;
320 f->fmt.pix.field = V4L2_FIELD_NONE;
321 f->fmt.pix.pixelformat = frm->fmt->fourcc;
322 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
323 f->fmt.pix.sizeimage = frm->size;
327 static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
330 enum v4l2_field *field;
336 field = &f->fmt.pix.field;
337 if (*field == V4L2_FIELD_ANY)
338 *field = V4L2_FIELD_NONE;
339 else if (*field != V4L2_FIELD_NONE)
342 if (f->fmt.pix.width > MAX_WIDTH)
343 f->fmt.pix.width = MAX_WIDTH;
344 if (f->fmt.pix.height > MAX_HEIGHT)
345 f->fmt.pix.height = MAX_HEIGHT;
347 if (f->fmt.pix.width < 1)
348 f->fmt.pix.width = 1;
349 if (f->fmt.pix.height < 1)
350 f->fmt.pix.height = 1;
352 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
353 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
357 static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
359 struct g2d_ctx *ctx = prv;
360 struct g2d_dev *dev = ctx->dev;
361 struct vb2_queue *vq;
362 struct g2d_frame *frm;
366 /* Adjust all values accordingly to the hardware capabilities
367 * and chosen format. */
368 ret = vidioc_try_fmt(file, prv, f);
371 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
372 if (vb2_is_busy(vq)) {
373 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
376 frm = get_frame(ctx, f->type);
382 frm->width = f->fmt.pix.width;
383 frm->height = f->fmt.pix.height;
384 frm->size = f->fmt.pix.sizeimage;
385 /* Reset crop settings */
388 frm->c_width = frm->width;
389 frm->c_height = frm->height;
390 frm->right = frm->width;
391 frm->bottom = frm->height;
393 frm->stride = f->fmt.pix.bytesperline;
397 static int vidioc_g_selection(struct file *file, void *prv,
398 struct v4l2_selection *s)
400 struct g2d_ctx *ctx = prv;
403 f = get_frame(ctx, s->type);
408 case V4L2_SEL_TGT_CROP:
409 case V4L2_SEL_TGT_CROP_DEFAULT:
410 case V4L2_SEL_TGT_CROP_BOUNDS:
411 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
414 case V4L2_SEL_TGT_COMPOSE:
415 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
416 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
417 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
425 case V4L2_SEL_TGT_CROP:
426 case V4L2_SEL_TGT_COMPOSE:
427 s->r.left = f->o_height;
428 s->r.top = f->o_width;
429 s->r.width = f->c_width;
430 s->r.height = f->c_height;
432 case V4L2_SEL_TGT_CROP_DEFAULT:
433 case V4L2_SEL_TGT_CROP_BOUNDS:
434 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
435 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
438 s->r.width = f->width;
439 s->r.height = f->height;
447 static int vidioc_try_selection(struct file *file, void *prv,
448 const struct v4l2_selection *s)
450 struct g2d_ctx *ctx = prv;
451 struct g2d_dev *dev = ctx->dev;
454 f = get_frame(ctx, s->type);
458 if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
459 if (s->target != V4L2_SEL_TGT_COMPOSE)
461 } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
462 if (s->target != V4L2_SEL_TGT_CROP)
466 if (s->r.top < 0 || s->r.left < 0) {
467 v4l2_err(&dev->v4l2_dev,
468 "doesn't support negative values for top & left\n");
475 static int vidioc_s_selection(struct file *file, void *prv,
476 struct v4l2_selection *s)
478 struct g2d_ctx *ctx = prv;
482 ret = vidioc_try_selection(file, prv, s);
485 f = get_frame(ctx, s->type);
489 f->c_width = s->r.width;
490 f->c_height = s->r.height;
491 f->o_width = s->r.left;
492 f->o_height = s->r.top;
493 f->bottom = f->o_height + f->c_height;
494 f->right = f->o_width + f->c_width;
498 static void device_run(void *prv)
500 struct g2d_ctx *ctx = prv;
501 struct g2d_dev *dev = ctx->dev;
502 struct vb2_v4l2_buffer *src, *dst;
508 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
509 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
511 clk_enable(dev->gate);
514 spin_lock_irqsave(&dev->ctrl_lock, flags);
516 g2d_set_src_size(dev, &ctx->in);
517 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
519 g2d_set_dst_size(dev, &ctx->out);
520 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
522 g2d_set_rop4(dev, ctx->rop);
523 g2d_set_flip(dev, ctx->flip);
525 if (ctx->in.c_width != ctx->out.c_width ||
526 ctx->in.c_height != ctx->out.c_height) {
527 if (dev->variant->hw_rev == TYPE_G2D_3X)
528 cmd |= CMD_V3_ENABLE_STRETCH;
530 g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
533 g2d_set_cmd(dev, cmd);
536 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
539 static irqreturn_t g2d_isr(int irq, void *prv)
541 struct g2d_dev *dev = prv;
542 struct g2d_ctx *ctx = dev->curr;
543 struct vb2_v4l2_buffer *src, *dst;
546 clk_disable(dev->gate);
550 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
551 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
556 dst->timecode = src->timecode;
557 dst->vb2_buf.timestamp = src->vb2_buf.timestamp;
558 dst->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
560 src->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
562 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
563 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
564 v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
570 static const struct v4l2_file_operations g2d_fops = {
571 .owner = THIS_MODULE,
573 .release = g2d_release,
574 .poll = v4l2_m2m_fop_poll,
575 .unlocked_ioctl = video_ioctl2,
576 .mmap = v4l2_m2m_fop_mmap,
579 static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
580 .vidioc_querycap = vidioc_querycap,
582 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
583 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
584 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
585 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
587 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
588 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
589 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
590 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
592 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
593 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
594 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
595 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
597 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
598 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
600 .vidioc_g_selection = vidioc_g_selection,
601 .vidioc_s_selection = vidioc_s_selection,
604 static const struct video_device g2d_videodev = {
607 .ioctl_ops = &g2d_ioctl_ops,
609 .release = video_device_release,
610 .vfl_dir = VFL_DIR_M2M,
613 static const struct v4l2_m2m_ops g2d_m2m_ops = {
614 .device_run = device_run,
617 static const struct of_device_id exynos_g2d_match[];
619 static int g2d_probe(struct platform_device *pdev)
622 struct video_device *vfd;
623 struct resource *res;
624 const struct of_device_id *of_id;
627 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
631 spin_lock_init(&dev->ctrl_lock);
632 mutex_init(&dev->mutex);
633 atomic_set(&dev->num_inst, 0);
635 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
637 dev->regs = devm_ioremap_resource(&pdev->dev, res);
638 if (IS_ERR(dev->regs))
639 return PTR_ERR(dev->regs);
641 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
642 if (IS_ERR(dev->clk)) {
643 dev_err(&pdev->dev, "failed to get g2d clock\n");
647 ret = clk_prepare(dev->clk);
649 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
653 dev->gate = clk_get(&pdev->dev, "fimg2d");
654 if (IS_ERR(dev->gate)) {
655 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
660 ret = clk_prepare(dev->gate);
662 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
666 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
668 dev_err(&pdev->dev, "failed to find IRQ\n");
670 goto unprep_clk_gate;
673 dev->irq = res->start;
675 ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
678 dev_err(&pdev->dev, "failed to install IRQ\n");
679 goto unprep_clk_gate;
682 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
684 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
686 goto unprep_clk_gate;
687 vfd = video_device_alloc();
689 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
694 set_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags);
695 vfd->lock = &dev->mutex;
696 vfd->v4l2_dev = &dev->v4l2_dev;
697 vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
698 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
700 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
703 video_set_drvdata(vfd, dev);
705 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
707 platform_set_drvdata(pdev, dev);
708 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
709 if (IS_ERR(dev->m2m_dev)) {
710 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
711 ret = PTR_ERR(dev->m2m_dev);
712 goto unreg_video_dev;
715 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
717 of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
720 goto unreg_video_dev;
722 dev->variant = (struct g2d_variant *)of_id->data;
727 video_unregister_device(dev->vfd);
729 video_device_release(vfd);
731 v4l2_device_unregister(&dev->v4l2_dev);
733 clk_unprepare(dev->gate);
737 clk_unprepare(dev->clk);
744 static int g2d_remove(struct platform_device *pdev)
746 struct g2d_dev *dev = platform_get_drvdata(pdev);
748 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
749 v4l2_m2m_release(dev->m2m_dev);
750 video_unregister_device(dev->vfd);
751 v4l2_device_unregister(&dev->v4l2_dev);
752 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
753 clk_unprepare(dev->gate);
755 clk_unprepare(dev->clk);
760 static struct g2d_variant g2d_drvdata_v3x = {
761 .hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
764 static struct g2d_variant g2d_drvdata_v4x = {
765 .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
768 static const struct of_device_id exynos_g2d_match[] = {
770 .compatible = "samsung,s5pv210-g2d",
771 .data = &g2d_drvdata_v3x,
773 .compatible = "samsung,exynos4212-g2d",
774 .data = &g2d_drvdata_v4x,
778 MODULE_DEVICE_TABLE(of, exynos_g2d_match);
780 static struct platform_driver g2d_pdrv = {
782 .remove = g2d_remove,
785 .of_match_table = exynos_g2d_match,
789 module_platform_driver(g2d_pdrv);
791 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
792 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
793 MODULE_LICENSE("GPL");