1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * STM32 DMA2D - 2D Graphics Accelerator Driver
5 * Copyright (c) 2021 Dillon Min
10 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
14 #include <linux/module.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>
23 #include <linux/platform_device.h>
24 #include <media/v4l2-mem2mem.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-event.h>
28 #include <media/videobuf2-v4l2.h>
29 #include <media/videobuf2-dma-contig.h>
32 #include "dma2d-regs.h"
35 * This V4L2 subdev m2m driver enables Chrom-Art Accelerator unit
36 * of STMicroelectronics STM32 SoC series.
38 * Currently support r2m, m2m, m2m_pfc.
40 * - r2m, Filling a part or the whole of a destination image with a specific
42 * - m2m, Copying a part or the whole of a source image into a part or the
43 * whole of a destination.
44 * - m2m_pfc, Copying a part or the whole of a source image into a part or the
45 * whole of a destination image with a pixel format conversion.
48 #define fh2ctx(__fh) container_of(__fh, struct dma2d_ctx, fh)
50 static const struct dma2d_fmt formats[] = {
52 .fourcc = V4L2_PIX_FMT_ARGB32,
53 .cmode = DMA2D_CMODE_ARGB8888,
57 .fourcc = V4L2_PIX_FMT_RGB24,
58 .cmode = DMA2D_CMODE_RGB888,
62 .fourcc = V4L2_PIX_FMT_RGB565,
63 .cmode = DMA2D_CMODE_RGB565,
67 .fourcc = V4L2_PIX_FMT_ARGB555,
68 .cmode = DMA2D_CMODE_ARGB1555,
72 .fourcc = V4L2_PIX_FMT_ARGB444,
73 .cmode = DMA2D_CMODE_ARGB4444,
78 #define NUM_FORMATS ARRAY_SIZE(formats)
80 static const struct dma2d_frame def_frame = {
81 .width = DEFAULT_WIDTH,
82 .height = DEFAULT_HEIGHT,
84 .a_rgb = {0x00, 0x00, 0x00, 0xff},
85 .a_mode = DMA2D_ALPHA_MODE_NO_MODIF,
86 .fmt = (struct dma2d_fmt *)&formats[0],
90 static struct dma2d_fmt *find_fmt(int pixelformat)
94 for (i = 0; i < NUM_FORMATS; i++) {
95 if (formats[i].fourcc == pixelformat)
96 return (struct dma2d_fmt *)&formats[i];
102 static struct dma2d_frame *get_frame(struct dma2d_ctx *ctx,
103 enum v4l2_buf_type type)
105 return V4L2_TYPE_IS_OUTPUT(type) ? &ctx->cap : &ctx->out;
108 static int dma2d_queue_setup(struct vb2_queue *vq,
109 unsigned int *nbuffers, unsigned int *nplanes,
110 unsigned int sizes[], struct device *alloc_devs[])
112 struct dma2d_ctx *ctx = vb2_get_drv_priv(vq);
113 struct dma2d_frame *f = get_frame(ctx, vq->type);
116 return sizes[0] < f->size ? -EINVAL : 0;
124 static int dma2d_buf_out_validate(struct vb2_buffer *vb)
126 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
128 if (vbuf->field == V4L2_FIELD_ANY)
129 vbuf->field = V4L2_FIELD_NONE;
130 if (vbuf->field != V4L2_FIELD_NONE)
136 static int dma2d_buf_prepare(struct vb2_buffer *vb)
138 struct dma2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
139 struct dma2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
141 if (vb2_plane_size(vb, 0) < f->size)
144 vb2_set_plane_payload(vb, 0, f->size);
149 static void dma2d_buf_queue(struct vb2_buffer *vb)
151 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
152 struct dma2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
154 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
157 static int dma2d_start_streaming(struct vb2_queue *q, unsigned int count)
159 struct dma2d_ctx *ctx = vb2_get_drv_priv(q);
160 struct dma2d_frame *f = get_frame(ctx, q->type);
166 static void dma2d_stop_streaming(struct vb2_queue *q)
168 struct dma2d_ctx *ctx = vb2_get_drv_priv(q);
169 struct vb2_v4l2_buffer *vbuf;
172 if (V4L2_TYPE_IS_OUTPUT(q->type))
173 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
175 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
178 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
182 static const struct vb2_ops dma2d_qops = {
183 .queue_setup = dma2d_queue_setup,
184 .buf_out_validate = dma2d_buf_out_validate,
185 .buf_prepare = dma2d_buf_prepare,
186 .buf_queue = dma2d_buf_queue,
187 .start_streaming = dma2d_start_streaming,
188 .stop_streaming = dma2d_stop_streaming,
189 .wait_prepare = vb2_ops_wait_prepare,
190 .wait_finish = vb2_ops_wait_finish,
193 static int queue_init(void *priv, struct vb2_queue *src_vq,
194 struct vb2_queue *dst_vq)
196 struct dma2d_ctx *ctx = priv;
199 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
200 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
201 src_vq->drv_priv = ctx;
202 src_vq->ops = &dma2d_qops;
203 src_vq->mem_ops = &vb2_dma_contig_memops;
204 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
205 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
206 src_vq->lock = &ctx->dev->mutex;
207 src_vq->dev = ctx->dev->v4l2_dev.dev;
209 ret = vb2_queue_init(src_vq);
213 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
214 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
215 dst_vq->drv_priv = ctx;
216 dst_vq->ops = &dma2d_qops;
217 dst_vq->mem_ops = &vb2_dma_contig_memops;
218 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
219 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
220 dst_vq->lock = &ctx->dev->mutex;
221 dst_vq->dev = ctx->dev->v4l2_dev.dev;
223 return vb2_queue_init(dst_vq);
226 static int dma2d_s_ctrl(struct v4l2_ctrl *ctrl)
228 struct dma2d_frame *frm;
229 struct dma2d_ctx *ctx = container_of(ctrl->handler, struct dma2d_ctx,
233 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
235 case V4L2_CID_COLORFX:
236 if (ctrl->val == V4L2_COLORFX_SET_RGB)
237 ctx->op_mode = DMA2D_MODE_R2M;
238 else if (ctrl->val == V4L2_COLORFX_NONE)
239 ctx->op_mode = DMA2D_MODE_M2M;
241 case V4L2_CID_COLORFX_RGB:
242 frm = get_frame(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
243 frm->a_rgb[2] = (ctrl->val >> 16) & 0xff;
244 frm->a_rgb[1] = (ctrl->val >> 8) & 0xff;
245 frm->a_rgb[0] = (ctrl->val >> 0) & 0xff;
248 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
251 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
256 static const struct v4l2_ctrl_ops dma2d_ctrl_ops = {
257 .s_ctrl = dma2d_s_ctrl,
260 static int dma2d_setup_ctrls(struct dma2d_ctx *ctx)
262 struct v4l2_ctrl_handler *handler = &ctx->ctrl_handler;
264 v4l2_ctrl_handler_init(handler, 2);
266 v4l2_ctrl_new_std_menu(handler, &dma2d_ctrl_ops, V4L2_CID_COLORFX,
267 V4L2_COLORFX_SET_RGB, ~0x10001,
270 v4l2_ctrl_new_std(handler, &dma2d_ctrl_ops, V4L2_CID_COLORFX_RGB, 0,
276 static int dma2d_open(struct file *file)
278 struct dma2d_dev *dev = video_drvdata(file);
279 struct dma2d_ctx *ctx = NULL;
282 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
286 /* Set default formats */
287 ctx->cap = def_frame;
289 ctx->out = def_frame;
290 ctx->op_mode = DMA2D_MODE_M2M_FPC;
291 ctx->colorspace = V4L2_COLORSPACE_REC709;
292 if (mutex_lock_interruptible(&dev->mutex)) {
297 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
298 if (IS_ERR(ctx->fh.m2m_ctx)) {
299 ret = PTR_ERR(ctx->fh.m2m_ctx);
300 mutex_unlock(&dev->mutex);
305 v4l2_fh_init(&ctx->fh, video_devdata(file));
306 file->private_data = &ctx->fh;
307 v4l2_fh_add(&ctx->fh);
309 dma2d_setup_ctrls(ctx);
311 /* Write the default values to the ctx struct */
312 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
314 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
315 mutex_unlock(&dev->mutex);
320 static int dma2d_release(struct file *file)
322 struct dma2d_dev *dev = video_drvdata(file);
323 struct dma2d_ctx *ctx = fh2ctx(file->private_data);
325 mutex_lock(&dev->mutex);
326 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
327 mutex_unlock(&dev->mutex);
328 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
329 v4l2_fh_del(&ctx->fh);
330 v4l2_fh_exit(&ctx->fh);
336 static int vidioc_querycap(struct file *file, void *priv,
337 struct v4l2_capability *cap)
339 strscpy(cap->driver, DMA2D_NAME, sizeof(cap->driver));
340 strscpy(cap->card, DMA2D_NAME, sizeof(cap->card));
341 strscpy(cap->bus_info, BUS_INFO, sizeof(cap->bus_info));
346 static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
348 if (f->index >= NUM_FORMATS)
351 f->pixelformat = formats[f->index].fourcc;
355 static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
357 struct dma2d_ctx *ctx = prv;
358 struct vb2_queue *vq;
359 struct dma2d_frame *frm;
361 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
365 frm = get_frame(ctx, f->type);
366 f->fmt.pix.width = frm->width;
367 f->fmt.pix.height = frm->height;
368 f->fmt.pix.field = V4L2_FIELD_NONE;
369 f->fmt.pix.pixelformat = frm->fmt->fourcc;
370 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
371 f->fmt.pix.sizeimage = frm->size;
372 f->fmt.pix.colorspace = ctx->colorspace;
373 f->fmt.pix.xfer_func = ctx->xfer_func;
374 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
375 f->fmt.pix.quantization = ctx->quant;
380 static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
382 struct dma2d_ctx *ctx = prv;
383 struct dma2d_fmt *fmt;
384 enum v4l2_field *field;
385 u32 fourcc = f->fmt.pix.pixelformat;
387 fmt = find_fmt(fourcc);
389 f->fmt.pix.pixelformat = formats[0].fourcc;
390 fmt = find_fmt(f->fmt.pix.pixelformat);
393 field = &f->fmt.pix.field;
394 if (*field == V4L2_FIELD_ANY)
395 *field = V4L2_FIELD_NONE;
396 else if (*field != V4L2_FIELD_NONE)
399 if (f->fmt.pix.width > MAX_WIDTH)
400 f->fmt.pix.width = MAX_WIDTH;
401 if (f->fmt.pix.height > MAX_HEIGHT)
402 f->fmt.pix.height = MAX_HEIGHT;
404 if (f->fmt.pix.width < 1)
405 f->fmt.pix.width = 1;
406 if (f->fmt.pix.height < 1)
407 f->fmt.pix.height = 1;
409 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && !f->fmt.pix.colorspace) {
410 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
411 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
412 f->fmt.pix.colorspace = ctx->colorspace;
413 f->fmt.pix.xfer_func = ctx->xfer_func;
414 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
415 f->fmt.pix.quantization = ctx->quant;
417 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
418 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
423 static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
425 struct dma2d_ctx *ctx = prv;
426 struct vb2_queue *vq;
427 struct dma2d_frame *frm;
428 struct dma2d_fmt *fmt;
431 /* Adjust all values accordingly to the hardware capabilities
434 ret = vidioc_try_fmt(file, prv, f);
438 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
442 fmt = find_fmt(f->fmt.pix.pixelformat);
446 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
447 ctx->colorspace = f->fmt.pix.colorspace;
448 ctx->xfer_func = f->fmt.pix.xfer_func;
449 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
450 ctx->quant = f->fmt.pix.quantization;
453 frm = get_frame(ctx, f->type);
454 frm->width = f->fmt.pix.width;
455 frm->height = f->fmt.pix.height;
456 frm->size = f->fmt.pix.sizeimage;
457 /* Reset crop settings */
460 frm->c_width = frm->width;
461 frm->c_height = frm->height;
462 frm->right = frm->width;
463 frm->bottom = frm->height;
465 frm->line_offset = 0;
470 static void device_run(void *prv)
472 struct dma2d_ctx *ctx = prv;
473 struct dma2d_dev *dev = ctx->dev;
474 struct dma2d_frame *frm_out, *frm_cap;
475 struct vb2_v4l2_buffer *src, *dst;
478 spin_lock_irqsave(&dev->ctrl_lock, flags);
481 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
482 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
486 frm_cap = get_frame(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
487 frm_out = get_frame(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
488 if (!frm_cap || !frm_out)
491 src->sequence = frm_out->sequence++;
492 dst->sequence = frm_cap->sequence++;
493 v4l2_m2m_buf_copy_metadata(src, dst, true);
495 clk_enable(dev->gate);
497 dma2d_config_fg(dev, frm_out,
498 vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
500 /* TODO: add M2M_BLEND handler here */
502 if (ctx->op_mode != DMA2D_MODE_R2M) {
503 if (frm_out->fmt->fourcc == frm_cap->fmt->fourcc)
504 ctx->op_mode = DMA2D_MODE_M2M;
506 ctx->op_mode = DMA2D_MODE_M2M_FPC;
509 dma2d_config_out(dev, frm_cap,
510 vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
511 dma2d_config_common(dev, ctx->op_mode, frm_cap->width, frm_cap->height);
515 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
518 static irqreturn_t dma2d_isr(int irq, void *prv)
520 struct dma2d_dev *dev = prv;
521 struct dma2d_ctx *ctx = dev->curr;
522 struct vb2_v4l2_buffer *src, *dst;
523 u32 s = dma2d_get_int(dev);
525 dma2d_clear_int(dev);
526 if (s & ISR_TCIF || s == 0) {
527 clk_disable(dev->gate);
531 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
532 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
537 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
538 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
539 v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
547 static const struct v4l2_file_operations dma2d_fops = {
548 .owner = THIS_MODULE,
550 .release = dma2d_release,
551 .poll = v4l2_m2m_fop_poll,
552 .unlocked_ioctl = video_ioctl2,
553 .mmap = v4l2_m2m_fop_mmap,
555 .get_unmapped_area = v4l2_m2m_get_unmapped_area,
559 static const struct v4l2_ioctl_ops dma2d_ioctl_ops = {
560 .vidioc_querycap = vidioc_querycap,
562 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
563 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
564 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
565 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
567 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
568 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
569 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
570 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
572 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
573 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
574 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
575 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
576 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
577 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
578 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
580 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
581 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
583 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
584 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
587 static const struct video_device dma2d_videodev = {
590 .ioctl_ops = &dma2d_ioctl_ops,
592 .release = video_device_release,
593 .vfl_dir = VFL_DIR_M2M,
596 static const struct v4l2_m2m_ops dma2d_m2m_ops = {
597 .device_run = device_run,
600 static const struct of_device_id stm32_dma2d_match[];
602 static int dma2d_probe(struct platform_device *pdev)
604 struct dma2d_dev *dev;
605 struct video_device *vfd;
608 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
612 spin_lock_init(&dev->ctrl_lock);
613 mutex_init(&dev->mutex);
614 atomic_set(&dev->num_inst, 0);
616 dev->regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
617 if (IS_ERR(dev->regs))
618 return PTR_ERR(dev->regs);
620 dev->gate = clk_get(&pdev->dev, "dma2d");
621 if (IS_ERR(dev->gate)) {
622 dev_err(&pdev->dev, "failed to get dma2d clock gate\n");
627 ret = clk_prepare(dev->gate);
629 dev_err(&pdev->dev, "failed to prepare dma2d clock gate\n");
633 ret = platform_get_irq(pdev, 0);
635 goto unprep_clk_gate;
639 ret = devm_request_irq(&pdev->dev, dev->irq, dma2d_isr,
642 dev_err(&pdev->dev, "failed to install IRQ\n");
643 goto unprep_clk_gate;
646 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
648 goto unprep_clk_gate;
650 vfd = video_device_alloc();
652 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
657 *vfd = dma2d_videodev;
658 vfd->lock = &dev->mutex;
659 vfd->v4l2_dev = &dev->v4l2_dev;
660 vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
662 platform_set_drvdata(pdev, dev);
663 dev->m2m_dev = v4l2_m2m_init(&dma2d_m2m_ops);
664 if (IS_ERR(dev->m2m_dev)) {
665 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
666 ret = PTR_ERR(dev->m2m_dev);
670 ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
672 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
676 video_set_drvdata(vfd, dev);
678 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
683 v4l2_m2m_release(dev->m2m_dev);
685 video_device_release(vfd);
687 v4l2_device_unregister(&dev->v4l2_dev);
689 clk_unprepare(dev->gate);
696 static void dma2d_remove(struct platform_device *pdev)
698 struct dma2d_dev *dev = platform_get_drvdata(pdev);
700 v4l2_info(&dev->v4l2_dev, "Removing " DMA2D_NAME);
701 v4l2_m2m_release(dev->m2m_dev);
702 video_unregister_device(dev->vfd);
703 v4l2_device_unregister(&dev->v4l2_dev);
704 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
705 clk_unprepare(dev->gate);
709 static const struct of_device_id stm32_dma2d_match[] = {
711 .compatible = "st,stm32-dma2d",
716 MODULE_DEVICE_TABLE(of, stm32_dma2d_match);
718 static struct platform_driver dma2d_pdrv = {
719 .probe = dma2d_probe,
720 .remove_new = dma2d_remove,
723 .of_match_table = stm32_dma2d_match,
727 module_platform_driver(dma2d_pdrv);
730 MODULE_DESCRIPTION("STM32 Chrom-Art Accelerator DMA2D driver");
731 MODULE_LICENSE("GPL");