Lines Matching full:jpeg

3  * V4L2 driver for the JPEG encoder/decoder from i.MX8QXP/i.MX8QM application
8 * Baseline and extended sequential jpeg decoding is supported.
9 * Progressive jpeg decoding is not supported by the IP.
19 * from the jpeg stream, by parsing the jpeg markers.
32 * but the driver works around this by replacing them in the jpeg stream.
38 * This is inspired by the drivers/media/platform/samsung/s5p-jpeg driver
56 #include <media/v4l2-jpeg.h>
63 #include "mxc-jpeg-hw.h"
64 #include "mxc-jpeg.h"
68 .name = "JPEG",
168 * split by JPEG marker, so it's easier to modify & use
315 /* mxc-jpeg specific */
341 static void print_mxc_buf(struct mxc_jpeg_dev *jpeg, struct vb2_buffer *buf, in print_mxc_buf() argument
358 v4l2_dbg(3, debug, &jpeg->v4l2_dev, in print_mxc_buf()
487 static bool mxc_jpeg_alloc_slot_data(struct mxc_jpeg_dev *jpeg, in mxc_jpeg_alloc_slot_data() argument
494 if (jpeg->slot_data[slot].desc) in mxc_jpeg_alloc_slot_data()
498 desc = dma_alloc_coherent(jpeg->dev, in mxc_jpeg_alloc_slot_data()
500 &jpeg->slot_data[slot].desc_handle, in mxc_jpeg_alloc_slot_data()
504 jpeg->slot_data[slot].desc = desc; in mxc_jpeg_alloc_slot_data()
507 cfg_desc = dma_alloc_coherent(jpeg->dev, in mxc_jpeg_alloc_slot_data()
509 &jpeg->slot_data[slot].cfg_desc_handle, in mxc_jpeg_alloc_slot_data()
513 jpeg->slot_data[slot].cfg_desc = cfg_desc; in mxc_jpeg_alloc_slot_data()
516 cfg_stm = dma_alloc_coherent(jpeg->dev, in mxc_jpeg_alloc_slot_data()
518 &jpeg->slot_data[slot].cfg_stream_handle, in mxc_jpeg_alloc_slot_data()
523 jpeg->slot_data[slot].cfg_stream_vaddr = cfg_stm; in mxc_jpeg_alloc_slot_data()
526 jpeg->slot_data[slot].used = true; in mxc_jpeg_alloc_slot_data()
530 dev_err(jpeg->dev, "Could not allocate descriptors for slot %d", slot); in mxc_jpeg_alloc_slot_data()
535 static void mxc_jpeg_free_slot_data(struct mxc_jpeg_dev *jpeg, in mxc_jpeg_free_slot_data() argument
539 dev_err(jpeg->dev, "Invalid slot %d, nothing to free.", slot); in mxc_jpeg_free_slot_data()
544 dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc), in mxc_jpeg_free_slot_data()
545 jpeg->slot_data[slot].desc, in mxc_jpeg_free_slot_data()
546 jpeg->slot_data[slot].desc_handle); in mxc_jpeg_free_slot_data()
549 dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc), in mxc_jpeg_free_slot_data()
550 jpeg->slot_data[slot].cfg_desc, in mxc_jpeg_free_slot_data()
551 jpeg->slot_data[slot].cfg_desc_handle); in mxc_jpeg_free_slot_data()
554 dma_free_coherent(jpeg->dev, MXC_JPEG_MAX_CFG_STREAM, in mxc_jpeg_free_slot_data()
555 jpeg->slot_data[slot].cfg_stream_vaddr, in mxc_jpeg_free_slot_data()
556 jpeg->slot_data[slot].cfg_stream_handle); in mxc_jpeg_free_slot_data()
558 jpeg->slot_data[slot].used = false; in mxc_jpeg_free_slot_data()
575 struct mxc_jpeg_dev *jpeg = priv; in mxc_jpeg_dec_irq() local
577 void __iomem *reg = jpeg->base_reg; in mxc_jpeg_dec_irq()
578 struct device *dev = jpeg->dev; in mxc_jpeg_dec_irq()
588 spin_lock(&jpeg->hw_lock); in mxc_jpeg_dec_irq()
594 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev); in mxc_jpeg_dec_irq()
628 if (jpeg->mode == MXC_JPEG_ENCODE && in mxc_jpeg_dec_irq()
636 if (jpeg->mode == MXC_JPEG_DECODE && jpeg_src_buf->dht_needed) { in mxc_jpeg_dec_irq()
642 if (jpeg->mode == MXC_JPEG_ENCODE) { in mxc_jpeg_dec_irq()
663 print_mxc_buf(jpeg, &src_buf->vb2_buf, 32); in mxc_jpeg_dec_irq()
665 print_mxc_buf(jpeg, &dst_buf->vb2_buf, 32); in mxc_jpeg_dec_irq()
670 jpeg->slot_data[slot].used = false; /* unused, but don't free */ in mxc_jpeg_dec_irq()
676 spin_unlock(&jpeg->hw_lock); in mxc_jpeg_dec_irq()
677 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); in mxc_jpeg_dec_irq()
680 spin_unlock(&jpeg->hw_lock); in mxc_jpeg_dec_irq()
826 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; in mxc_jpeg_config_dec_desc() local
827 void __iomem *reg = jpeg->base_reg; in mxc_jpeg_config_dec_desc()
829 struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc; in mxc_jpeg_config_dec_desc()
830 struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc; in mxc_jpeg_config_dec_desc()
831 dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle; in mxc_jpeg_config_dec_desc()
832 dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle; in mxc_jpeg_config_dec_desc()
833 dma_addr_t cfg_stream_handle = jpeg->slot_data[slot].cfg_stream_handle; in mxc_jpeg_config_dec_desc()
834 unsigned int *cfg_size = &jpeg->slot_data[slot].cfg_stream_size; in mxc_jpeg_config_dec_desc()
835 void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr; in mxc_jpeg_config_dec_desc()
851 print_descriptor_info(jpeg->dev, desc); in mxc_jpeg_config_dec_desc()
877 print_descriptor_info(jpeg->dev, cfg_desc); in mxc_jpeg_config_dec_desc()
888 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; in mxc_jpeg_config_enc_desc() local
889 void __iomem *reg = jpeg->base_reg; in mxc_jpeg_config_enc_desc()
891 struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc; in mxc_jpeg_config_enc_desc()
892 struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc; in mxc_jpeg_config_enc_desc()
893 dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle; in mxc_jpeg_config_enc_desc()
894 dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle; in mxc_jpeg_config_enc_desc()
895 void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr; in mxc_jpeg_config_enc_desc()
902 jpeg->slot_data[slot].cfg_stream_size = in mxc_jpeg_config_enc_desc()
911 cfg_desc->buf_base0 = jpeg->slot_data[slot].cfg_stream_handle; in mxc_jpeg_config_enc_desc()
930 dev_err(jpeg->dev, "No valid image format detected\n"); in mxc_jpeg_config_enc_desc()
935 dev_dbg(jpeg->dev, "cfg_desc:\n"); in mxc_jpeg_config_enc_desc()
936 print_descriptor_info(jpeg->dev, cfg_desc); in mxc_jpeg_config_enc_desc()
937 dev_dbg(jpeg->dev, "enc desc:\n"); in mxc_jpeg_config_enc_desc()
938 print_descriptor_info(jpeg->dev, desc); in mxc_jpeg_config_enc_desc()
939 print_wrapper_info(jpeg->dev, reg); in mxc_jpeg_config_enc_desc()
940 print_cast_status(jpeg->dev, reg, MXC_JPEG_ENCODE); in mxc_jpeg_config_enc_desc()
959 dev_dbg(dev, "Detected jpeg res=(%dx%d)->(%dx%d), pixfmt=%c%c%c%c\n", in mxc_jpeg_source_change()
969 * detected from the jpeg output stream in mxc_jpeg_source_change()
1010 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; in mxc_jpeg_device_run() local
1011 void __iomem *reg = jpeg->base_reg; in mxc_jpeg_device_run()
1012 struct device *dev = jpeg->dev; in mxc_jpeg_device_run()
1051 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); in mxc_jpeg_device_run()
1058 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); in mxc_jpeg_device_run()
1066 ctx->slot = mxc_get_free_slot(jpeg->slot_data, MXC_MAX_SLOTS); in mxc_jpeg_device_run()
1071 if (!mxc_jpeg_alloc_slot_data(jpeg, ctx->slot)) { in mxc_jpeg_device_run()
1079 if (jpeg->mode == MXC_JPEG_ENCODE) { in mxc_jpeg_device_run()
1087 print_mxc_buf(jpeg, &src_buf->vb2_buf, 0); in mxc_jpeg_device_run()
1216 dev_err(ctx->mxc_jpeg->dev, "Failed to power up jpeg\n"); in mxc_jpeg_start_streaming()
1346 /* if no sizeimage from user, assume worst jpeg compression */ in mxc_jpeg_sizeimage()
1354 /* jpeg stream size must be multiple of 1K */ in mxc_jpeg_sizeimage()
1380 dev_err(dev, "Error parsing JPEG stream markers\n"); in mxc_jpeg_parse()
1391 dev_warn(dev, "Keeping resolution from JPEG: %dx%d", in mxc_jpeg_parse()
1396 "Resolution mismatch: %dx%d (JPEG) versus %dx%d(user)", in mxc_jpeg_parse()
1404 dev_err(dev, "JPEG width or height should be <= 8192: %dx%d\n", in mxc_jpeg_parse()
1410 dev_err(dev, "JPEG width or height should be > 64: %dx%d\n", in mxc_jpeg_parse()
1415 dev_err(dev, "JPEG number of components should be <=%d", in mxc_jpeg_parse()
1423 dev_warn(dev, "JPEG component ids should be 0-3 or 1-4"); in mxc_jpeg_parse()
1669 dev_err(ctx->mxc_jpeg->dev, "failed to setup mxc jpeg controls\n"); in mxc_jpeg_open()
1677 dev_dbg(dev, "Opened JPEG decoder instance %p\n", ctx); in mxc_jpeg_open()
1679 dev_dbg(dev, "Opened JPEG encoder instance %p\n", ctx); in mxc_jpeg_open()
1722 * after jpeg parse on the output buffer) in mxc_jpeg_enum_fmt_vid_cap()
1804 * but since inside JPEG the YUV quantization is full-range, in mxc_jpeg_try_fmt()
1816 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; in mxc_jpeg_try_fmt_vid_cap() local
1817 struct device *dev = jpeg->dev; in mxc_jpeg_try_fmt_vid_cap()
1821 int q_type = (jpeg->mode == MXC_JPEG_DECODE) ? in mxc_jpeg_try_fmt_vid_cap()
1836 f->fmt.pix_mp.pixelformat = (jpeg->mode == MXC_JPEG_DECODE) ? in mxc_jpeg_try_fmt_vid_cap()
1847 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; in mxc_jpeg_try_fmt_vid_out() local
1848 struct device *dev = jpeg->dev; in mxc_jpeg_try_fmt_vid_out()
1852 int q_type = (jpeg->mode == MXC_JPEG_ENCODE) ? in mxc_jpeg_try_fmt_vid_out()
1867 f->fmt.pix_mp.pixelformat = (jpeg->mode == MXC_JPEG_ENCODE) ? in mxc_jpeg_try_fmt_vid_out()
1880 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; in mxc_jpeg_s_fmt() local
1890 v4l2_err(&jpeg->v4l2_dev, "queue busy\n"); in mxc_jpeg_s_fmt()
1978 struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg; in mxc_jpeg_g_fmt_vid() local
1979 struct device *dev = jpeg->dev; in mxc_jpeg_g_fmt_vid()
2066 dev_dbg(dev, "Release JPEG decoder instance on slot %d.", in mxc_jpeg_release()
2069 dev_dbg(dev, "Release JPEG encoder instance on slot %d.", in mxc_jpeg_release()
2095 static void mxc_jpeg_detach_pm_domains(struct mxc_jpeg_dev *jpeg) in mxc_jpeg_detach_pm_domains() argument
2099 for (i = 0; i < jpeg->num_domains; i++) { in mxc_jpeg_detach_pm_domains()
2100 if (jpeg->pd_link[i] && !IS_ERR(jpeg->pd_link[i])) in mxc_jpeg_detach_pm_domains()
2101 device_link_del(jpeg->pd_link[i]); in mxc_jpeg_detach_pm_domains()
2102 if (jpeg->pd_dev[i] && !IS_ERR(jpeg->pd_dev[i])) in mxc_jpeg_detach_pm_domains()
2103 dev_pm_domain_detach(jpeg->pd_dev[i], true); in mxc_jpeg_detach_pm_domains()
2104 jpeg->pd_dev[i] = NULL; in mxc_jpeg_detach_pm_domains()
2105 jpeg->pd_link[i] = NULL; in mxc_jpeg_detach_pm_domains()
2109 static int mxc_jpeg_attach_pm_domains(struct mxc_jpeg_dev *jpeg) in mxc_jpeg_attach_pm_domains() argument
2111 struct device *dev = jpeg->dev; in mxc_jpeg_attach_pm_domains()
2112 struct device_node *np = jpeg->pdev->dev.of_node; in mxc_jpeg_attach_pm_domains()
2116 jpeg->num_domains = of_count_phandle_with_args(np, "power-domains", in mxc_jpeg_attach_pm_domains()
2118 if (jpeg->num_domains < 0) { in mxc_jpeg_attach_pm_domains()
2119 dev_err(dev, "No power domains defined for jpeg node\n"); in mxc_jpeg_attach_pm_domains()
2120 return jpeg->num_domains; in mxc_jpeg_attach_pm_domains()
2123 jpeg->pd_dev = devm_kmalloc_array(dev, jpeg->num_domains, in mxc_jpeg_attach_pm_domains()
2124 sizeof(*jpeg->pd_dev), GFP_KERNEL); in mxc_jpeg_attach_pm_domains()
2125 if (!jpeg->pd_dev) in mxc_jpeg_attach_pm_domains()
2128 jpeg->pd_link = devm_kmalloc_array(dev, jpeg->num_domains, in mxc_jpeg_attach_pm_domains()
2129 sizeof(*jpeg->pd_link), GFP_KERNEL); in mxc_jpeg_attach_pm_domains()
2130 if (!jpeg->pd_link) in mxc_jpeg_attach_pm_domains()
2133 for (i = 0; i < jpeg->num_domains; i++) { in mxc_jpeg_attach_pm_domains()
2134 jpeg->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i); in mxc_jpeg_attach_pm_domains()
2135 if (IS_ERR(jpeg->pd_dev[i])) { in mxc_jpeg_attach_pm_domains()
2136 ret = PTR_ERR(jpeg->pd_dev[i]); in mxc_jpeg_attach_pm_domains()
2140 jpeg->pd_link[i] = device_link_add(dev, jpeg->pd_dev[i], in mxc_jpeg_attach_pm_domains()
2143 if (!jpeg->pd_link[i]) { in mxc_jpeg_attach_pm_domains()
2151 mxc_jpeg_detach_pm_domains(jpeg); in mxc_jpeg_attach_pm_domains()
2157 struct mxc_jpeg_dev *jpeg; in mxc_jpeg_probe() local
2168 jpeg = devm_kzalloc(dev, sizeof(struct mxc_jpeg_dev), GFP_KERNEL); in mxc_jpeg_probe()
2169 if (!jpeg) in mxc_jpeg_probe()
2172 mutex_init(&jpeg->lock); in mxc_jpeg_probe()
2173 spin_lock_init(&jpeg->hw_lock); in mxc_jpeg_probe()
2181 jpeg->base_reg = devm_platform_ioremap_resource(pdev, 0); in mxc_jpeg_probe()
2182 if (IS_ERR(jpeg->base_reg)) in mxc_jpeg_probe()
2183 return PTR_ERR(jpeg->base_reg); in mxc_jpeg_probe()
2192 0, pdev->name, jpeg); in mxc_jpeg_probe()
2200 jpeg->pdev = pdev; in mxc_jpeg_probe()
2201 jpeg->dev = dev; in mxc_jpeg_probe()
2202 jpeg->mode = mode; in mxc_jpeg_probe()
2205 jpeg->clk_ipg = devm_clk_get(dev, "ipg"); in mxc_jpeg_probe()
2206 if (IS_ERR(jpeg->clk_ipg)) { in mxc_jpeg_probe()
2208 ret = PTR_ERR(jpeg->clk_ipg); in mxc_jpeg_probe()
2212 jpeg->clk_per = devm_clk_get(dev, "per"); in mxc_jpeg_probe()
2213 if (IS_ERR(jpeg->clk_per)) { in mxc_jpeg_probe()
2215 ret = PTR_ERR(jpeg->clk_per); in mxc_jpeg_probe()
2219 ret = mxc_jpeg_attach_pm_domains(jpeg); in mxc_jpeg_probe()
2226 ret = v4l2_device_register(dev, &jpeg->v4l2_dev); in mxc_jpeg_probe()
2231 jpeg->m2m_dev = v4l2_m2m_init(&mxc_jpeg_m2m_ops); in mxc_jpeg_probe()
2232 if (IS_ERR(jpeg->m2m_dev)) { in mxc_jpeg_probe()
2234 ret = PTR_ERR(jpeg->m2m_dev); in mxc_jpeg_probe()
2238 jpeg->dec_vdev = video_device_alloc(); in mxc_jpeg_probe()
2239 if (!jpeg->dec_vdev) { in mxc_jpeg_probe()
2245 snprintf(jpeg->dec_vdev->name, in mxc_jpeg_probe()
2246 sizeof(jpeg->dec_vdev->name), in mxc_jpeg_probe()
2249 snprintf(jpeg->dec_vdev->name, in mxc_jpeg_probe()
2250 sizeof(jpeg->dec_vdev->name), in mxc_jpeg_probe()
2253 jpeg->dec_vdev->fops = &mxc_jpeg_fops; in mxc_jpeg_probe()
2254 jpeg->dec_vdev->ioctl_ops = &mxc_jpeg_ioctl_ops; in mxc_jpeg_probe()
2255 jpeg->dec_vdev->minor = -1; in mxc_jpeg_probe()
2256 jpeg->dec_vdev->release = video_device_release; in mxc_jpeg_probe()
2257 jpeg->dec_vdev->lock = &jpeg->lock; /* lock for ioctl serialization */ in mxc_jpeg_probe()
2258 jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev; in mxc_jpeg_probe()
2259 jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M; in mxc_jpeg_probe()
2260 jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING | in mxc_jpeg_probe()
2263 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_DECODER_CMD); in mxc_jpeg_probe()
2264 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_DECODER_CMD); in mxc_jpeg_probe()
2266 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_ENCODER_CMD); in mxc_jpeg_probe()
2267 v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_ENCODER_CMD); in mxc_jpeg_probe()
2269 ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_VIDEO, -1); in mxc_jpeg_probe()
2274 video_set_drvdata(jpeg->dec_vdev, jpeg); in mxc_jpeg_probe()
2276 v4l2_info(&jpeg->v4l2_dev, in mxc_jpeg_probe()
2278 jpeg->dec_vdev->num, VIDEO_MAJOR, in mxc_jpeg_probe()
2279 jpeg->dec_vdev->minor); in mxc_jpeg_probe()
2281 v4l2_info(&jpeg->v4l2_dev, in mxc_jpeg_probe()
2283 jpeg->dec_vdev->num, VIDEO_MAJOR, in mxc_jpeg_probe()
2284 jpeg->dec_vdev->minor); in mxc_jpeg_probe()
2286 platform_set_drvdata(pdev, jpeg); in mxc_jpeg_probe()
2292 video_device_release(jpeg->dec_vdev); in mxc_jpeg_probe()
2295 v4l2_m2m_release(jpeg->m2m_dev); in mxc_jpeg_probe()
2298 v4l2_device_unregister(&jpeg->v4l2_dev); in mxc_jpeg_probe()
2301 mxc_jpeg_detach_pm_domains(jpeg); in mxc_jpeg_probe()
2311 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); in mxc_jpeg_runtime_resume() local
2314 ret = clk_prepare_enable(jpeg->clk_ipg); in mxc_jpeg_runtime_resume()
2320 ret = clk_prepare_enable(jpeg->clk_per); in mxc_jpeg_runtime_resume()
2329 clk_disable_unprepare(jpeg->clk_ipg); in mxc_jpeg_runtime_resume()
2336 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); in mxc_jpeg_runtime_suspend() local
2338 clk_disable_unprepare(jpeg->clk_ipg); in mxc_jpeg_runtime_suspend()
2339 clk_disable_unprepare(jpeg->clk_per); in mxc_jpeg_runtime_suspend()
2348 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); in mxc_jpeg_suspend() local
2350 v4l2_m2m_suspend(jpeg->m2m_dev); in mxc_jpeg_suspend()
2356 struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev); in mxc_jpeg_resume() local
2363 v4l2_m2m_resume(jpeg->m2m_dev); in mxc_jpeg_resume()
2377 struct mxc_jpeg_dev *jpeg = platform_get_drvdata(pdev); in mxc_jpeg_remove() local
2380 mxc_jpeg_free_slot_data(jpeg, slot); in mxc_jpeg_remove()
2383 video_unregister_device(jpeg->dec_vdev); in mxc_jpeg_remove()
2384 v4l2_m2m_release(jpeg->m2m_dev); in mxc_jpeg_remove()
2385 v4l2_device_unregister(&jpeg->v4l2_dev); in mxc_jpeg_remove()
2386 mxc_jpeg_detach_pm_domains(jpeg); in mxc_jpeg_remove()
2397 .name = "mxc-jpeg",
2406 MODULE_DESCRIPTION("V4L2 driver for i.MX8 QXP/QM JPEG encoder/decoder");