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[] = {
33 .fourcc = V4L2_PIX_FMT_RGB32,
35 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
39 .fourcc = V4L2_PIX_FMT_RGB565X,
41 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
45 .fourcc = V4L2_PIX_FMT_RGB555X,
47 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
51 .fourcc = V4L2_PIX_FMT_RGB444,
53 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
56 .name = "PACKED_RGB_888",
57 .fourcc = V4L2_PIX_FMT_RGB24,
59 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
62 #define NUM_FORMATS ARRAY_SIZE(formats)
64 static struct g2d_frame def_frame = {
65 .width = DEFAULT_WIDTH,
66 .height = DEFAULT_HEIGHT,
67 .c_width = DEFAULT_WIDTH,
68 .c_height = DEFAULT_HEIGHT,
72 .right = DEFAULT_WIDTH,
73 .bottom = DEFAULT_HEIGHT,
76 static struct g2d_fmt *find_fmt(struct v4l2_format *f)
79 for (i = 0; i < NUM_FORMATS; i++) {
80 if (formats[i].fourcc == f->fmt.pix.pixelformat)
87 static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
88 enum v4l2_buf_type type)
91 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
93 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
96 return ERR_PTR(-EINVAL);
100 static int g2d_queue_setup(struct vb2_queue *vq,
101 unsigned int *nbuffers, unsigned int *nplanes,
102 unsigned int sizes[], struct device *alloc_devs[])
104 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
105 struct g2d_frame *f = get_frame(ctx, vq->type);
119 static int g2d_buf_prepare(struct vb2_buffer *vb)
121 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
122 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
126 vb2_set_plane_payload(vb, 0, f->size);
130 static void g2d_buf_queue(struct vb2_buffer *vb)
132 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
133 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
134 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
137 static const struct vb2_ops g2d_qops = {
138 .queue_setup = g2d_queue_setup,
139 .buf_prepare = g2d_buf_prepare,
140 .buf_queue = g2d_buf_queue,
141 .wait_prepare = vb2_ops_wait_prepare,
142 .wait_finish = vb2_ops_wait_finish,
145 static int queue_init(void *priv, struct vb2_queue *src_vq,
146 struct vb2_queue *dst_vq)
148 struct g2d_ctx *ctx = priv;
151 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
152 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
153 src_vq->drv_priv = ctx;
154 src_vq->ops = &g2d_qops;
155 src_vq->mem_ops = &vb2_dma_contig_memops;
156 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
157 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
158 src_vq->lock = &ctx->dev->mutex;
159 src_vq->dev = ctx->dev->v4l2_dev.dev;
161 ret = vb2_queue_init(src_vq);
165 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
166 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
167 dst_vq->drv_priv = ctx;
168 dst_vq->ops = &g2d_qops;
169 dst_vq->mem_ops = &vb2_dma_contig_memops;
170 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
171 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
172 dst_vq->lock = &ctx->dev->mutex;
173 dst_vq->dev = ctx->dev->v4l2_dev.dev;
175 return vb2_queue_init(dst_vq);
178 static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
180 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
184 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
186 case V4L2_CID_COLORFX:
187 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
188 ctx->rop = ROP4_INVERT;
190 ctx->rop = ROP4_COPY;
194 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
198 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
202 static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
203 .s_ctrl = g2d_s_ctrl,
206 static int g2d_setup_ctrls(struct g2d_ctx *ctx)
208 struct g2d_dev *dev = ctx->dev;
210 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
212 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
213 V4L2_CID_HFLIP, 0, 1, 1, 0);
215 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
216 V4L2_CID_VFLIP, 0, 1, 1, 0);
218 v4l2_ctrl_new_std_menu(
222 V4L2_COLORFX_NEGATIVE,
223 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
226 if (ctx->ctrl_handler.error) {
227 int err = ctx->ctrl_handler.error;
228 v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
229 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
233 v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
238 static int g2d_open(struct file *file)
240 struct g2d_dev *dev = video_drvdata(file);
241 struct g2d_ctx *ctx = NULL;
244 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
248 /* Set default formats */
250 ctx->out = def_frame;
252 if (mutex_lock_interruptible(&dev->mutex)) {
256 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
257 if (IS_ERR(ctx->fh.m2m_ctx)) {
258 ret = PTR_ERR(ctx->fh.m2m_ctx);
259 mutex_unlock(&dev->mutex);
263 v4l2_fh_init(&ctx->fh, video_devdata(file));
264 file->private_data = &ctx->fh;
265 v4l2_fh_add(&ctx->fh);
267 g2d_setup_ctrls(ctx);
269 /* Write the default values to the ctx struct */
270 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
272 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
273 mutex_unlock(&dev->mutex);
275 v4l2_info(&dev->v4l2_dev, "instance opened\n");
279 static int g2d_release(struct file *file)
281 struct g2d_dev *dev = video_drvdata(file);
282 struct g2d_ctx *ctx = fh2ctx(file->private_data);
284 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
285 v4l2_fh_del(&ctx->fh);
286 v4l2_fh_exit(&ctx->fh);
288 v4l2_info(&dev->v4l2_dev, "instance closed\n");
293 static int vidioc_querycap(struct file *file, void *priv,
294 struct v4l2_capability *cap)
296 strscpy(cap->driver, G2D_NAME, sizeof(cap->driver));
297 strscpy(cap->card, G2D_NAME, sizeof(cap->card));
298 cap->bus_info[0] = 0;
299 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
300 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
304 static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
307 if (f->index >= NUM_FORMATS)
309 fmt = &formats[f->index];
310 f->pixelformat = fmt->fourcc;
311 strscpy(f->description, fmt->name, sizeof(f->description));
315 static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
317 struct g2d_ctx *ctx = prv;
318 struct vb2_queue *vq;
319 struct g2d_frame *frm;
321 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
324 frm = get_frame(ctx, f->type);
328 f->fmt.pix.width = frm->width;
329 f->fmt.pix.height = frm->height;
330 f->fmt.pix.field = V4L2_FIELD_NONE;
331 f->fmt.pix.pixelformat = frm->fmt->fourcc;
332 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
333 f->fmt.pix.sizeimage = frm->size;
337 static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
340 enum v4l2_field *field;
346 field = &f->fmt.pix.field;
347 if (*field == V4L2_FIELD_ANY)
348 *field = V4L2_FIELD_NONE;
349 else if (*field != V4L2_FIELD_NONE)
352 if (f->fmt.pix.width > MAX_WIDTH)
353 f->fmt.pix.width = MAX_WIDTH;
354 if (f->fmt.pix.height > MAX_HEIGHT)
355 f->fmt.pix.height = MAX_HEIGHT;
357 if (f->fmt.pix.width < 1)
358 f->fmt.pix.width = 1;
359 if (f->fmt.pix.height < 1)
360 f->fmt.pix.height = 1;
362 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
363 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
367 static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
369 struct g2d_ctx *ctx = prv;
370 struct g2d_dev *dev = ctx->dev;
371 struct vb2_queue *vq;
372 struct g2d_frame *frm;
376 /* Adjust all values accordingly to the hardware capabilities
377 * and chosen format. */
378 ret = vidioc_try_fmt(file, prv, f);
381 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
382 if (vb2_is_busy(vq)) {
383 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
386 frm = get_frame(ctx, f->type);
392 frm->width = f->fmt.pix.width;
393 frm->height = f->fmt.pix.height;
394 frm->size = f->fmt.pix.sizeimage;
395 /* Reset crop settings */
398 frm->c_width = frm->width;
399 frm->c_height = frm->height;
400 frm->right = frm->width;
401 frm->bottom = frm->height;
403 frm->stride = f->fmt.pix.bytesperline;
407 static int vidioc_g_selection(struct file *file, void *prv,
408 struct v4l2_selection *s)
410 struct g2d_ctx *ctx = prv;
413 f = get_frame(ctx, s->type);
418 case V4L2_SEL_TGT_CROP:
419 case V4L2_SEL_TGT_CROP_DEFAULT:
420 case V4L2_SEL_TGT_CROP_BOUNDS:
421 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
424 case V4L2_SEL_TGT_COMPOSE:
425 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
426 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
427 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
435 case V4L2_SEL_TGT_CROP:
436 case V4L2_SEL_TGT_COMPOSE:
437 s->r.left = f->o_height;
438 s->r.top = f->o_width;
439 s->r.width = f->c_width;
440 s->r.height = f->c_height;
442 case V4L2_SEL_TGT_CROP_DEFAULT:
443 case V4L2_SEL_TGT_CROP_BOUNDS:
444 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
445 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
448 s->r.width = f->width;
449 s->r.height = f->height;
457 static int vidioc_try_selection(struct file *file, void *prv,
458 const struct v4l2_selection *s)
460 struct g2d_ctx *ctx = prv;
461 struct g2d_dev *dev = ctx->dev;
464 f = get_frame(ctx, s->type);
468 if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
469 if (s->target != V4L2_SEL_TGT_COMPOSE)
471 } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
472 if (s->target != V4L2_SEL_TGT_CROP)
476 if (s->r.top < 0 || s->r.left < 0) {
477 v4l2_err(&dev->v4l2_dev,
478 "doesn't support negative values for top & left\n");
485 static int vidioc_s_selection(struct file *file, void *prv,
486 struct v4l2_selection *s)
488 struct g2d_ctx *ctx = prv;
492 ret = vidioc_try_selection(file, prv, s);
495 f = get_frame(ctx, s->type);
499 f->c_width = s->r.width;
500 f->c_height = s->r.height;
501 f->o_width = s->r.left;
502 f->o_height = s->r.top;
503 f->bottom = f->o_height + f->c_height;
504 f->right = f->o_width + f->c_width;
508 static void device_run(void *prv)
510 struct g2d_ctx *ctx = prv;
511 struct g2d_dev *dev = ctx->dev;
512 struct vb2_v4l2_buffer *src, *dst;
518 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
519 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
521 clk_enable(dev->gate);
524 spin_lock_irqsave(&dev->ctrl_lock, flags);
526 g2d_set_src_size(dev, &ctx->in);
527 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
529 g2d_set_dst_size(dev, &ctx->out);
530 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
532 g2d_set_rop4(dev, ctx->rop);
533 g2d_set_flip(dev, ctx->flip);
535 if (ctx->in.c_width != ctx->out.c_width ||
536 ctx->in.c_height != ctx->out.c_height) {
537 if (dev->variant->hw_rev == TYPE_G2D_3X)
538 cmd |= CMD_V3_ENABLE_STRETCH;
540 g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
543 g2d_set_cmd(dev, cmd);
546 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
549 static irqreturn_t g2d_isr(int irq, void *prv)
551 struct g2d_dev *dev = prv;
552 struct g2d_ctx *ctx = dev->curr;
553 struct vb2_v4l2_buffer *src, *dst;
556 clk_disable(dev->gate);
560 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
561 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
566 dst->timecode = src->timecode;
567 dst->vb2_buf.timestamp = src->vb2_buf.timestamp;
568 dst->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
570 src->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
572 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
573 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
574 v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
580 static const struct v4l2_file_operations g2d_fops = {
581 .owner = THIS_MODULE,
583 .release = g2d_release,
584 .poll = v4l2_m2m_fop_poll,
585 .unlocked_ioctl = video_ioctl2,
586 .mmap = v4l2_m2m_fop_mmap,
589 static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
590 .vidioc_querycap = vidioc_querycap,
592 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
593 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
594 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
595 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
597 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
598 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
599 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
600 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
602 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
603 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
604 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
605 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
607 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
608 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
610 .vidioc_g_selection = vidioc_g_selection,
611 .vidioc_s_selection = vidioc_s_selection,
614 static const struct video_device g2d_videodev = {
617 .ioctl_ops = &g2d_ioctl_ops,
619 .release = video_device_release,
620 .vfl_dir = VFL_DIR_M2M,
623 static const struct v4l2_m2m_ops g2d_m2m_ops = {
624 .device_run = device_run,
627 static const struct of_device_id exynos_g2d_match[];
629 static int g2d_probe(struct platform_device *pdev)
632 struct video_device *vfd;
633 struct resource *res;
634 const struct of_device_id *of_id;
637 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
641 spin_lock_init(&dev->ctrl_lock);
642 mutex_init(&dev->mutex);
643 atomic_set(&dev->num_inst, 0);
645 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
647 dev->regs = devm_ioremap_resource(&pdev->dev, res);
648 if (IS_ERR(dev->regs))
649 return PTR_ERR(dev->regs);
651 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
652 if (IS_ERR(dev->clk)) {
653 dev_err(&pdev->dev, "failed to get g2d clock\n");
657 ret = clk_prepare(dev->clk);
659 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
663 dev->gate = clk_get(&pdev->dev, "fimg2d");
664 if (IS_ERR(dev->gate)) {
665 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
670 ret = clk_prepare(dev->gate);
672 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
676 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
678 dev_err(&pdev->dev, "failed to find IRQ\n");
680 goto unprep_clk_gate;
683 dev->irq = res->start;
685 ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
688 dev_err(&pdev->dev, "failed to install IRQ\n");
689 goto unprep_clk_gate;
692 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
694 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
696 goto unprep_clk_gate;
697 vfd = video_device_alloc();
699 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
704 set_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags);
705 vfd->lock = &dev->mutex;
706 vfd->v4l2_dev = &dev->v4l2_dev;
707 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
709 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
712 video_set_drvdata(vfd, dev);
714 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
716 platform_set_drvdata(pdev, dev);
717 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
718 if (IS_ERR(dev->m2m_dev)) {
719 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
720 ret = PTR_ERR(dev->m2m_dev);
721 goto unreg_video_dev;
724 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
726 of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
729 goto unreg_video_dev;
731 dev->variant = (struct g2d_variant *)of_id->data;
736 video_unregister_device(dev->vfd);
738 video_device_release(vfd);
740 v4l2_device_unregister(&dev->v4l2_dev);
742 clk_unprepare(dev->gate);
746 clk_unprepare(dev->clk);
753 static int g2d_remove(struct platform_device *pdev)
755 struct g2d_dev *dev = platform_get_drvdata(pdev);
757 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
758 v4l2_m2m_release(dev->m2m_dev);
759 video_unregister_device(dev->vfd);
760 v4l2_device_unregister(&dev->v4l2_dev);
761 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
762 clk_unprepare(dev->gate);
764 clk_unprepare(dev->clk);
769 static struct g2d_variant g2d_drvdata_v3x = {
770 .hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
773 static struct g2d_variant g2d_drvdata_v4x = {
774 .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
777 static const struct of_device_id exynos_g2d_match[] = {
779 .compatible = "samsung,s5pv210-g2d",
780 .data = &g2d_drvdata_v3x,
782 .compatible = "samsung,exynos4212-g2d",
783 .data = &g2d_drvdata_v4x,
787 MODULE_DEVICE_TABLE(of, exynos_g2d_match);
789 static struct platform_driver g2d_pdrv = {
791 .remove = g2d_remove,
794 .of_match_table = exynos_g2d_match,
798 module_platform_driver(g2d_pdrv);
800 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
801 MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
802 MODULE_LICENSE("GPL");