Lines Matching +full:stm32 +full:- +full:dma2d
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * STM32 DMA2D - 2D Graphics Accelerator Driver
8 * based on s5p-g2d
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>
31 #include "dma2d.h"
32 #include "dma2d-regs.h"
35 * This V4L2 subdev m2m driver enables Chrom-Art Accelerator unit
36 * of STMicroelectronics STM32 SoC series.
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
44 * - m2m_pfc, Copying a part or the whole of a source image into a part or the
105 return V4L2_TYPE_IS_OUTPUT(type) ? &ctx->cap : &ctx->out; in get_frame()
113 struct dma2d_frame *f = get_frame(ctx, vq->type); in dma2d_queue_setup()
116 return sizes[0] < f->size ? -EINVAL : 0; in dma2d_queue_setup()
118 sizes[0] = f->size; in dma2d_queue_setup()
128 if (vbuf->field == V4L2_FIELD_ANY) in dma2d_buf_out_validate()
129 vbuf->field = V4L2_FIELD_NONE; in dma2d_buf_out_validate()
130 if (vbuf->field != V4L2_FIELD_NONE) in dma2d_buf_out_validate()
131 return -EINVAL; in dma2d_buf_out_validate()
138 struct dma2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); in dma2d_buf_prepare()
139 struct dma2d_frame *f = get_frame(ctx, vb->vb2_queue->type); in dma2d_buf_prepare()
141 if (vb2_plane_size(vb, 0) < f->size) in dma2d_buf_prepare()
142 return -EINVAL; in dma2d_buf_prepare()
144 vb2_set_plane_payload(vb, 0, f->size); in dma2d_buf_prepare()
152 struct dma2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); in dma2d_buf_queue()
154 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); in dma2d_buf_queue()
160 struct dma2d_frame *f = get_frame(ctx, q->type); in dma2d_start_streaming()
162 f->sequence = 0; in dma2d_start_streaming()
172 if (V4L2_TYPE_IS_OUTPUT(q->type)) in dma2d_stop_streaming()
173 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); in dma2d_stop_streaming()
175 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); in dma2d_stop_streaming()
199 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; in queue_init()
200 src_vq->io_modes = VB2_MMAP | VB2_DMABUF; in queue_init()
201 src_vq->drv_priv = ctx; in queue_init()
202 src_vq->ops = &dma2d_qops; in queue_init()
203 src_vq->mem_ops = &vb2_dma_contig_memops; in queue_init()
204 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); in queue_init()
205 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; in queue_init()
206 src_vq->lock = &ctx->dev->mutex; in queue_init()
207 src_vq->dev = ctx->dev->v4l2_dev.dev; in queue_init()
213 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; in queue_init()
214 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; in queue_init()
215 dst_vq->drv_priv = ctx; in queue_init()
216 dst_vq->ops = &dma2d_qops; in queue_init()
217 dst_vq->mem_ops = &vb2_dma_contig_memops; in queue_init()
218 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); in queue_init()
219 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; in queue_init()
220 dst_vq->lock = &ctx->dev->mutex; in queue_init()
221 dst_vq->dev = ctx->dev->v4l2_dev.dev; in queue_init()
229 struct dma2d_ctx *ctx = container_of(ctrl->handler, struct dma2d_ctx, in dma2d_s_ctrl()
233 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags); in dma2d_s_ctrl()
234 switch (ctrl->id) { in dma2d_s_ctrl()
236 if (ctrl->val == V4L2_COLORFX_SET_RGB) in dma2d_s_ctrl()
237 ctx->op_mode = DMA2D_MODE_R2M; in dma2d_s_ctrl()
238 else if (ctrl->val == V4L2_COLORFX_NONE) in dma2d_s_ctrl()
239 ctx->op_mode = DMA2D_MODE_M2M; in dma2d_s_ctrl()
243 frm->a_rgb[2] = (ctrl->val >> 16) & 0xff; in dma2d_s_ctrl()
244 frm->a_rgb[1] = (ctrl->val >> 8) & 0xff; in dma2d_s_ctrl()
245 frm->a_rgb[0] = (ctrl->val >> 0) & 0xff; in dma2d_s_ctrl()
248 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags); in dma2d_s_ctrl()
249 return -EINVAL; in dma2d_s_ctrl()
251 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags); in dma2d_s_ctrl()
262 struct v4l2_ctrl_handler *handler = &ctx->ctrl_handler; in dma2d_setup_ctrls()
284 return -ENOMEM; in dma2d_open()
285 ctx->dev = dev; in dma2d_open()
287 ctx->cap = def_frame; in dma2d_open()
288 ctx->bg = def_frame; in dma2d_open()
289 ctx->out = def_frame; in dma2d_open()
290 ctx->op_mode = DMA2D_MODE_M2M_FPC; in dma2d_open()
291 ctx->colorspace = V4L2_COLORSPACE_REC709; in dma2d_open()
292 if (mutex_lock_interruptible(&dev->mutex)) { in dma2d_open()
294 return -ERESTARTSYS; in dma2d_open()
297 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init); in dma2d_open()
298 if (IS_ERR(ctx->fh.m2m_ctx)) { in dma2d_open()
299 ret = PTR_ERR(ctx->fh.m2m_ctx); in dma2d_open()
300 mutex_unlock(&dev->mutex); in dma2d_open()
305 v4l2_fh_init(&ctx->fh, video_devdata(file)); in dma2d_open()
306 file->private_data = &ctx->fh; in dma2d_open()
307 v4l2_fh_add(&ctx->fh); in dma2d_open()
312 v4l2_ctrl_handler_setup(&ctx->ctrl_handler); in dma2d_open()
314 ctx->fh.ctrl_handler = &ctx->ctrl_handler; in dma2d_open()
315 mutex_unlock(&dev->mutex); in dma2d_open()
323 struct dma2d_ctx *ctx = fh2ctx(file->private_data); in dma2d_release()
325 mutex_lock(&dev->mutex); in dma2d_release()
326 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); in dma2d_release()
327 mutex_unlock(&dev->mutex); in dma2d_release()
328 v4l2_ctrl_handler_free(&ctx->ctrl_handler); in dma2d_release()
329 v4l2_fh_del(&ctx->fh); in dma2d_release()
330 v4l2_fh_exit(&ctx->fh); in dma2d_release()
339 strscpy(cap->driver, DMA2D_NAME, sizeof(cap->driver)); in vidioc_querycap()
340 strscpy(cap->card, DMA2D_NAME, sizeof(cap->card)); in vidioc_querycap()
341 strscpy(cap->bus_info, BUS_INFO, sizeof(cap->bus_info)); in vidioc_querycap()
348 if (f->index >= NUM_FORMATS) in vidioc_enum_fmt()
349 return -EINVAL; in vidioc_enum_fmt()
351 f->pixelformat = formats[f->index].fourcc; in vidioc_enum_fmt()
361 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); in vidioc_g_fmt()
363 return -EINVAL; in vidioc_g_fmt()
365 frm = get_frame(ctx, f->type); in vidioc_g_fmt()
366 f->fmt.pix.width = frm->width; in vidioc_g_fmt()
367 f->fmt.pix.height = frm->height; in vidioc_g_fmt()
368 f->fmt.pix.field = V4L2_FIELD_NONE; in vidioc_g_fmt()
369 f->fmt.pix.pixelformat = frm->fmt->fourcc; in vidioc_g_fmt()
370 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3; in vidioc_g_fmt()
371 f->fmt.pix.sizeimage = frm->size; in vidioc_g_fmt()
372 f->fmt.pix.colorspace = ctx->colorspace; in vidioc_g_fmt()
373 f->fmt.pix.xfer_func = ctx->xfer_func; in vidioc_g_fmt()
374 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; in vidioc_g_fmt()
375 f->fmt.pix.quantization = ctx->quant; in vidioc_g_fmt()
385 u32 fourcc = f->fmt.pix.pixelformat; in vidioc_try_fmt()
389 f->fmt.pix.pixelformat = formats[0].fourcc; in vidioc_try_fmt()
390 fmt = find_fmt(f->fmt.pix.pixelformat); in vidioc_try_fmt()
393 field = &f->fmt.pix.field; in vidioc_try_fmt()
397 return -EINVAL; in vidioc_try_fmt()
399 if (f->fmt.pix.width > MAX_WIDTH) in vidioc_try_fmt()
400 f->fmt.pix.width = MAX_WIDTH; in vidioc_try_fmt()
401 if (f->fmt.pix.height > MAX_HEIGHT) in vidioc_try_fmt()
402 f->fmt.pix.height = MAX_HEIGHT; in vidioc_try_fmt()
404 if (f->fmt.pix.width < 1) in vidioc_try_fmt()
405 f->fmt.pix.width = 1; in vidioc_try_fmt()
406 if (f->fmt.pix.height < 1) in vidioc_try_fmt()
407 f->fmt.pix.height = 1; in vidioc_try_fmt()
409 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && !f->fmt.pix.colorspace) { in vidioc_try_fmt()
410 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; in vidioc_try_fmt()
411 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { in vidioc_try_fmt()
412 f->fmt.pix.colorspace = ctx->colorspace; in vidioc_try_fmt()
413 f->fmt.pix.xfer_func = ctx->xfer_func; in vidioc_try_fmt()
414 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; in vidioc_try_fmt()
415 f->fmt.pix.quantization = ctx->quant; in vidioc_try_fmt()
417 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3; in vidioc_try_fmt()
418 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; in vidioc_try_fmt()
438 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); in vidioc_s_fmt()
440 return -EBUSY; in vidioc_s_fmt()
442 fmt = find_fmt(f->fmt.pix.pixelformat); in vidioc_s_fmt()
444 return -EINVAL; in vidioc_s_fmt()
446 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { in vidioc_s_fmt()
447 ctx->colorspace = f->fmt.pix.colorspace; in vidioc_s_fmt()
448 ctx->xfer_func = f->fmt.pix.xfer_func; in vidioc_s_fmt()
449 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc; in vidioc_s_fmt()
450 ctx->quant = f->fmt.pix.quantization; in vidioc_s_fmt()
453 frm = get_frame(ctx, f->type); in vidioc_s_fmt()
454 frm->width = f->fmt.pix.width; in vidioc_s_fmt()
455 frm->height = f->fmt.pix.height; in vidioc_s_fmt()
456 frm->size = f->fmt.pix.sizeimage; in vidioc_s_fmt()
458 frm->o_width = 0; in vidioc_s_fmt()
459 frm->o_height = 0; in vidioc_s_fmt()
460 frm->c_width = frm->width; in vidioc_s_fmt()
461 frm->c_height = frm->height; in vidioc_s_fmt()
462 frm->right = frm->width; in vidioc_s_fmt()
463 frm->bottom = frm->height; in vidioc_s_fmt()
464 frm->fmt = fmt; in vidioc_s_fmt()
465 frm->line_offset = 0; in vidioc_s_fmt()
473 struct dma2d_dev *dev = ctx->dev; in device_run()
478 spin_lock_irqsave(&dev->ctrl_lock, flags); in device_run()
479 dev->curr = ctx; in device_run()
481 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); in device_run()
482 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); in device_run()
491 src->sequence = frm_out->sequence++; in device_run()
492 dst->sequence = frm_cap->sequence++; in device_run()
495 clk_enable(dev->gate); in device_run()
498 vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0)); in device_run()
502 if (ctx->op_mode != DMA2D_MODE_R2M) { in device_run()
503 if (frm_out->fmt->fourcc == frm_cap->fmt->fourcc) in device_run()
504 ctx->op_mode = DMA2D_MODE_M2M; in device_run()
506 ctx->op_mode = DMA2D_MODE_M2M_FPC; in device_run()
510 vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0)); in device_run()
511 dma2d_config_common(dev, ctx->op_mode, frm_cap->width, frm_cap->height); in device_run()
515 spin_unlock_irqrestore(&dev->ctrl_lock, flags); in device_run()
521 struct dma2d_ctx *ctx = dev->curr; in dma2d_isr()
527 clk_disable(dev->gate); in dma2d_isr()
531 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); in dma2d_isr()
532 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); in dma2d_isr()
539 v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx); in dma2d_isr()
541 dev->curr = NULL; in dma2d_isr()
591 .minor = -1,
608 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); in dma2d_probe()
610 return -ENOMEM; in dma2d_probe()
612 spin_lock_init(&dev->ctrl_lock); in dma2d_probe()
613 mutex_init(&dev->mutex); in dma2d_probe()
614 atomic_set(&dev->num_inst, 0); in dma2d_probe()
616 dev->regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL); in dma2d_probe()
617 if (IS_ERR(dev->regs)) in dma2d_probe()
618 return PTR_ERR(dev->regs); in dma2d_probe()
620 dev->gate = clk_get(&pdev->dev, "dma2d"); in dma2d_probe()
621 if (IS_ERR(dev->gate)) { in dma2d_probe()
622 dev_err(&pdev->dev, "failed to get dma2d clock gate\n"); in dma2d_probe()
623 ret = -ENXIO; in dma2d_probe()
627 ret = clk_prepare(dev->gate); in dma2d_probe()
629 dev_err(&pdev->dev, "failed to prepare dma2d clock gate\n"); in dma2d_probe()
637 dev->irq = ret; in dma2d_probe()
639 ret = devm_request_irq(&pdev->dev, dev->irq, dma2d_isr, in dma2d_probe()
640 0, pdev->name, dev); in dma2d_probe()
642 dev_err(&pdev->dev, "failed to install IRQ\n"); in dma2d_probe()
646 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); in dma2d_probe()
652 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n"); in dma2d_probe()
653 ret = -ENOMEM; in dma2d_probe()
658 vfd->lock = &dev->mutex; in dma2d_probe()
659 vfd->v4l2_dev = &dev->v4l2_dev; in dma2d_probe()
660 vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; in dma2d_probe()
663 dev->m2m_dev = v4l2_m2m_init(&dma2d_m2m_ops); in dma2d_probe()
664 if (IS_ERR(dev->m2m_dev)) { in dma2d_probe()
665 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); in dma2d_probe()
666 ret = PTR_ERR(dev->m2m_dev); in dma2d_probe()
672 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); in dma2d_probe()
677 dev->vfd = vfd; in dma2d_probe()
678 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n", in dma2d_probe()
679 vfd->num); in dma2d_probe()
683 v4l2_m2m_release(dev->m2m_dev); in dma2d_probe()
687 v4l2_device_unregister(&dev->v4l2_dev); in dma2d_probe()
689 clk_unprepare(dev->gate); in dma2d_probe()
691 clk_put(dev->gate); in dma2d_probe()
700 v4l2_info(&dev->v4l2_dev, "Removing " DMA2D_NAME); in dma2d_remove()
701 v4l2_m2m_release(dev->m2m_dev); in dma2d_remove()
702 video_unregister_device(dev->vfd); in dma2d_remove()
703 v4l2_device_unregister(&dev->v4l2_dev); in dma2d_remove()
704 vb2_dma_contig_clear_max_seg_size(&pdev->dev); in dma2d_remove()
705 clk_unprepare(dev->gate); in dma2d_remove()
706 clk_put(dev->gate); in dma2d_remove()
711 .compatible = "st,stm32-dma2d",
730 MODULE_DESCRIPTION("STM32 Chrom-Art Accelerator DMA2D driver");