Lines Matching +full:stm32 +full:- +full:dcmi
1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for STM32 Digital Camera Memory Interface
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-dev.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-event.h>
34 #include <media/v4l2-fwnode.h>
35 #include <media/v4l2-image-sizes.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-rect.h>
38 #include <media/videobuf2-dma-contig.h>
40 #define DRV_NAME "stm32-dcmi"
42 /* Registers offset for DCMI */
199 static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf);
201 static void dcmi_buffer_done(struct stm32_dcmi *dcmi, in dcmi_buffer_done() argument
211 list_del_init(&buf->list); in dcmi_buffer_done()
213 vbuf = &buf->vb; in dcmi_buffer_done()
215 vbuf->sequence = dcmi->sequence++; in dcmi_buffer_done()
216 vbuf->field = V4L2_FIELD_NONE; in dcmi_buffer_done()
217 vbuf->vb2_buf.timestamp = ktime_get_ns(); in dcmi_buffer_done()
218 vb2_set_plane_payload(&vbuf->vb2_buf, 0, bytesused); in dcmi_buffer_done()
219 vb2_buffer_done(&vbuf->vb2_buf, in dcmi_buffer_done()
221 dev_dbg(dcmi->dev, "buffer[%d] done seq=%d, bytesused=%zu\n", in dcmi_buffer_done()
222 vbuf->vb2_buf.index, vbuf->sequence, bytesused); in dcmi_buffer_done()
224 dcmi->buffers_count++; in dcmi_buffer_done()
225 dcmi->active = NULL; in dcmi_buffer_done()
228 static int dcmi_restart_capture(struct stm32_dcmi *dcmi) in dcmi_restart_capture() argument
232 spin_lock_irq(&dcmi->irqlock); in dcmi_restart_capture()
234 if (dcmi->state != RUNNING) { in dcmi_restart_capture()
235 spin_unlock_irq(&dcmi->irqlock); in dcmi_restart_capture()
236 return -EINVAL; in dcmi_restart_capture()
240 if (list_empty(&dcmi->buffers)) { in dcmi_restart_capture()
241 dev_dbg(dcmi->dev, "Capture restart is deferred to next buffer queueing\n"); in dcmi_restart_capture()
242 dcmi->state = WAIT_FOR_BUFFER; in dcmi_restart_capture()
243 spin_unlock_irq(&dcmi->irqlock); in dcmi_restart_capture()
246 buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list); in dcmi_restart_capture()
247 dcmi->active = buf; in dcmi_restart_capture()
249 spin_unlock_irq(&dcmi->irqlock); in dcmi_restart_capture()
251 return dcmi_start_capture(dcmi, buf); in dcmi_restart_capture()
256 struct stm32_dcmi *dcmi = (struct stm32_dcmi *)param; in dcmi_dma_callback() local
259 struct dcmi_buf *buf = dcmi->active; in dcmi_dma_callback()
261 spin_lock_irq(&dcmi->irqlock); in dcmi_dma_callback()
264 status = dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state); in dcmi_dma_callback()
268 dev_dbg(dcmi->dev, "%s: Received DMA_IN_PROGRESS\n", __func__); in dcmi_dma_callback()
271 dev_err(dcmi->dev, "%s: Received DMA_PAUSED\n", __func__); in dcmi_dma_callback()
274 dev_err(dcmi->dev, "%s: Received DMA_ERROR\n", __func__); in dcmi_dma_callback()
277 dcmi_buffer_done(dcmi, buf, 0, -EIO); in dcmi_dma_callback()
280 dev_dbg(dcmi->dev, "%s: Received DMA_COMPLETE\n", __func__); in dcmi_dma_callback()
283 dcmi_buffer_done(dcmi, buf, buf->size, 0); in dcmi_dma_callback()
285 spin_unlock_irq(&dcmi->irqlock); in dcmi_dma_callback()
288 if (dcmi_restart_capture(dcmi)) in dcmi_dma_callback()
289 dev_err(dcmi->dev, "%s: Cannot restart capture on DMA complete\n", in dcmi_dma_callback()
293 dev_err(dcmi->dev, "%s: Received unknown status\n", __func__); in dcmi_dma_callback()
297 spin_unlock_irq(&dcmi->irqlock); in dcmi_dma_callback()
300 static int dcmi_start_dma(struct stm32_dcmi *dcmi, in dcmi_start_dma() argument
309 config.src_addr = (dma_addr_t)dcmi->res->start + DCMI_DR; in dcmi_start_dma()
315 ret = dmaengine_slave_config(dcmi->dma_chan, &config); in dcmi_start_dma()
317 dev_err(dcmi->dev, "%s: DMA channel config failed (%d)\n", in dcmi_start_dma()
327 mutex_lock(&dcmi->dma_lock); in dcmi_start_dma()
330 desc = dmaengine_prep_slave_sg(dcmi->dma_chan, buf->sgt.sgl, buf->sgt.nents, in dcmi_start_dma()
334 dev_err(dcmi->dev, "%s: DMA dmaengine_prep_slave_sg failed\n", __func__); in dcmi_start_dma()
335 mutex_unlock(&dcmi->dma_lock); in dcmi_start_dma()
336 return -EINVAL; in dcmi_start_dma()
340 desc->callback = dcmi_dma_callback; in dcmi_start_dma()
341 desc->callback_param = dcmi; in dcmi_start_dma()
344 dcmi->dma_cookie = dmaengine_submit(desc); in dcmi_start_dma()
345 if (dma_submit_error(dcmi->dma_cookie)) { in dcmi_start_dma()
346 dev_err(dcmi->dev, "%s: DMA submission failed\n", __func__); in dcmi_start_dma()
347 mutex_unlock(&dcmi->dma_lock); in dcmi_start_dma()
348 return -ENXIO; in dcmi_start_dma()
351 mutex_unlock(&dcmi->dma_lock); in dcmi_start_dma()
353 dma_async_issue_pending(dcmi->dma_chan); in dcmi_start_dma()
358 static int dcmi_start_capture(struct stm32_dcmi *dcmi, struct dcmi_buf *buf) in dcmi_start_capture() argument
363 return -EINVAL; in dcmi_start_capture()
365 ret = dcmi_start_dma(dcmi, buf); in dcmi_start_capture()
367 dcmi->errors_count++; in dcmi_start_capture()
372 reg_set(dcmi->regs, DCMI_CR, CR_CAPTURE); in dcmi_start_capture()
377 static void dcmi_set_crop(struct stm32_dcmi *dcmi) in dcmi_set_crop() argument
382 size = ((dcmi->crop.height - 1) << 16) | in dcmi_set_crop()
383 ((dcmi->crop.width << 1) - 1); in dcmi_set_crop()
384 reg_write(dcmi->regs, DCMI_CWSIZE, size); in dcmi_set_crop()
387 start = ((dcmi->crop.top) << 16) | in dcmi_set_crop()
388 ((dcmi->crop.left << 1)); in dcmi_set_crop()
389 reg_write(dcmi->regs, DCMI_CWSTRT, start); in dcmi_set_crop()
391 dev_dbg(dcmi->dev, "Cropping to %ux%u@%u:%u\n", in dcmi_set_crop()
392 dcmi->crop.width, dcmi->crop.height, in dcmi_set_crop()
393 dcmi->crop.left, dcmi->crop.top); in dcmi_set_crop()
396 reg_set(dcmi->regs, DCMI_CR, CR_CROP); in dcmi_set_crop()
399 static void dcmi_process_jpeg(struct stm32_dcmi *dcmi) in dcmi_process_jpeg() argument
403 struct dcmi_buf *buf = dcmi->active; in dcmi_process_jpeg()
419 dmaengine_synchronize(dcmi->dma_chan); in dcmi_process_jpeg()
422 status = dmaengine_tx_status(dcmi->dma_chan, dcmi->dma_cookie, &state); in dcmi_process_jpeg()
423 if (status != DMA_ERROR && state.residue < buf->size) { in dcmi_process_jpeg()
425 dcmi_buffer_done(dcmi, buf, buf->size - state.residue, 0); in dcmi_process_jpeg()
427 dcmi->errors_count++; in dcmi_process_jpeg()
428 dev_err(dcmi->dev, "%s: Cannot get JPEG size from DMA\n", in dcmi_process_jpeg()
431 dcmi_buffer_done(dcmi, buf, 0, -EIO); in dcmi_process_jpeg()
435 dmaengine_terminate_sync(dcmi->dma_chan); in dcmi_process_jpeg()
438 if (dcmi_restart_capture(dcmi)) in dcmi_process_jpeg()
439 dev_err(dcmi->dev, "%s: Cannot restart capture on JPEG received\n", in dcmi_process_jpeg()
445 struct stm32_dcmi *dcmi = arg; in dcmi_irq_thread() local
447 spin_lock_irq(&dcmi->irqlock); in dcmi_irq_thread()
449 if (dcmi->misr & IT_OVR) { in dcmi_irq_thread()
450 dcmi->overrun_count++; in dcmi_irq_thread()
451 if (dcmi->overrun_count > OVERRUN_ERROR_THRESHOLD) in dcmi_irq_thread()
452 dcmi->errors_count++; in dcmi_irq_thread()
454 if (dcmi->misr & IT_ERR) in dcmi_irq_thread()
455 dcmi->errors_count++; in dcmi_irq_thread()
457 if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG && in dcmi_irq_thread()
458 dcmi->misr & IT_FRAME) { in dcmi_irq_thread()
460 spin_unlock_irq(&dcmi->irqlock); in dcmi_irq_thread()
461 dcmi_process_jpeg(dcmi); in dcmi_irq_thread()
465 spin_unlock_irq(&dcmi->irqlock); in dcmi_irq_thread()
471 struct stm32_dcmi *dcmi = arg; in dcmi_irq_callback() local
474 spin_lock_irqsave(&dcmi->irqlock, flags); in dcmi_irq_callback()
476 dcmi->misr = reg_read(dcmi->regs, DCMI_MIS); in dcmi_irq_callback()
479 reg_set(dcmi->regs, DCMI_ICR, IT_FRAME | IT_OVR | IT_ERR); in dcmi_irq_callback()
481 spin_unlock_irqrestore(&dcmi->irqlock, flags); in dcmi_irq_callback()
492 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq); in dcmi_queue_setup() local
495 size = dcmi->fmt.fmt.pix.sizeimage; in dcmi_queue_setup()
499 return sizes[0] < size ? -EINVAL : 0; in dcmi_queue_setup()
504 dev_dbg(dcmi->dev, "Setup queue, count=%d, size=%d\n", in dcmi_queue_setup()
515 INIT_LIST_HEAD(&buf->list); in dcmi_buf_init()
522 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue); in dcmi_buf_prepare() local
531 size = dcmi->fmt.fmt.pix.sizeimage; in dcmi_buf_prepare()
534 dev_err(dcmi->dev, "%s data will not fit into plane (%lu < %lu)\n", in dcmi_buf_prepare()
536 return -EINVAL; in dcmi_buf_prepare()
541 if (!buf->prepared) { in dcmi_buf_prepare()
543 buf->size = vb2_plane_size(&buf->vb.vb2_buf, 0); in dcmi_buf_prepare()
544 if (buf->size > dcmi->dma_max_burst) in dcmi_buf_prepare()
545 num_sgs = DIV_ROUND_UP(buf->size, dcmi->dma_max_burst); in dcmi_buf_prepare()
547 ret = sg_alloc_table(&buf->sgt, num_sgs, GFP_ATOMIC); in dcmi_buf_prepare()
549 dev_err(dcmi->dev, "sg table alloc failed\n"); in dcmi_buf_prepare()
553 dma_buf = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0); in dcmi_buf_prepare()
555 dev_dbg(dcmi->dev, "buffer[%d] phy=%pad size=%zu\n", in dcmi_buf_prepare()
556 vb->index, &dma_buf, buf->size); in dcmi_buf_prepare()
558 for_each_sg(buf->sgt.sgl, sg, num_sgs, i) { in dcmi_buf_prepare()
559 size_t bytes = min_t(size_t, size, dcmi->dma_max_burst); in dcmi_buf_prepare()
564 size -= bytes; in dcmi_buf_prepare()
567 buf->prepared = true; in dcmi_buf_prepare()
569 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->size); in dcmi_buf_prepare()
577 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vb->vb2_queue); in dcmi_buf_queue() local
581 spin_lock_irq(&dcmi->irqlock); in dcmi_buf_queue()
584 list_add_tail(&buf->list, &dcmi->buffers); in dcmi_buf_queue()
586 if (dcmi->state == WAIT_FOR_BUFFER) { in dcmi_buf_queue()
587 dcmi->state = RUNNING; in dcmi_buf_queue()
588 dcmi->active = buf; in dcmi_buf_queue()
590 dev_dbg(dcmi->dev, "Starting capture on buffer[%d] queued\n", in dcmi_buf_queue()
591 buf->vb.vb2_buf.index); in dcmi_buf_queue()
593 spin_unlock_irq(&dcmi->irqlock); in dcmi_buf_queue()
594 if (dcmi_start_capture(dcmi, buf)) in dcmi_buf_queue()
595 dev_err(dcmi->dev, "%s: Cannot restart capture on overflow or error\n", in dcmi_buf_queue()
600 spin_unlock_irq(&dcmi->irqlock); in dcmi_buf_queue()
603 static struct media_entity *dcmi_find_source(struct stm32_dcmi *dcmi) in dcmi_find_source() argument
605 struct media_entity *entity = &dcmi->vdev->entity; in dcmi_find_source()
610 pad = &entity->pads[0]; in dcmi_find_source()
611 if (!(pad->flags & MEDIA_PAD_FL_SINK)) in dcmi_find_source()
615 if (!pad || !is_media_entity_v4l2_subdev(pad->entity)) in dcmi_find_source()
618 entity = pad->entity; in dcmi_find_source()
624 static int dcmi_pipeline_s_fmt(struct stm32_dcmi *dcmi, in dcmi_pipeline_s_fmt() argument
627 struct media_entity *entity = &dcmi->source->entity; in dcmi_pipeline_s_fmt()
644 for (i = 0; i < entity->num_pads; i++) { in dcmi_pipeline_s_fmt()
645 pad = &entity->pads[i]; in dcmi_pipeline_s_fmt()
646 if (pad->flags & MEDIA_PAD_FL_SOURCE) { in dcmi_pipeline_s_fmt()
661 dev_dbg(dcmi->dev, "\"%s\":%d pad format set to 0x%x %ux%u\n", in dcmi_pipeline_s_fmt()
662 subdev->name, pad->index, format->format.code, in dcmi_pipeline_s_fmt()
663 format->format.width, format->format.height); in dcmi_pipeline_s_fmt()
665 fmt.pad = pad->index; in dcmi_pipeline_s_fmt()
668 dev_err(dcmi->dev, "%s: Failed to set format 0x%x %ux%u on \"%s\":%d pad (%d)\n", in dcmi_pipeline_s_fmt()
669 __func__, format->format.code, in dcmi_pipeline_s_fmt()
670 format->format.width, format->format.height, in dcmi_pipeline_s_fmt()
671 subdev->name, pad->index, ret); in dcmi_pipeline_s_fmt()
675 if (fmt.format.code != format->format.code || in dcmi_pipeline_s_fmt()
676 fmt.format.width != format->format.width || in dcmi_pipeline_s_fmt()
677 fmt.format.height != format->format.height) { in dcmi_pipeline_s_fmt()
678 dev_dbg(dcmi->dev, "\"%s\":%d pad format has been changed to 0x%x %ux%u\n", in dcmi_pipeline_s_fmt()
679 subdev->name, pad->index, fmt.format.code, in dcmi_pipeline_s_fmt()
685 if (!sink_pad || !is_media_entity_v4l2_subdev(sink_pad->entity)) in dcmi_pipeline_s_fmt()
688 entity = sink_pad->entity; in dcmi_pipeline_s_fmt()
697 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq); in dcmi_start_streaming() local
702 ret = pm_runtime_resume_and_get(dcmi->dev); in dcmi_start_streaming()
704 dev_err(dcmi->dev, "%s: Failed to start streaming, cannot get sync (%d)\n", in dcmi_start_streaming()
709 ret = video_device_pipeline_start(dcmi->vdev, &dcmi->pipeline); in dcmi_start_streaming()
711 dev_err(dcmi->dev, "%s: Failed to start streaming, media pipeline start error (%d)\n", in dcmi_start_streaming()
716 ret = v4l2_subdev_call(dcmi->s_subdev, video, s_stream, 1); in dcmi_start_streaming()
718 dev_err(dcmi->dev, "%s: Failed to start source subdev, error (%d)\n", in dcmi_start_streaming()
723 spin_lock_irq(&dcmi->irqlock); in dcmi_start_streaming()
726 switch (dcmi->bus.bus_width) { in dcmi_start_streaming()
742 if (dcmi->bus.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) in dcmi_start_streaming()
746 if (dcmi->bus.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) in dcmi_start_streaming()
750 if (dcmi->bus.flags & V4L2_MBUS_PCLK_SAMPLE_RISING) in dcmi_start_streaming()
758 * With DCMI this means LSC=SAV=0x80 & LEC=EAV=0x9d. in dcmi_start_streaming()
760 if (dcmi->bus_type == V4L2_MBUS_BT656) { in dcmi_start_streaming()
764 reg_write(dcmi->regs, DCMI_ESUR, 0xffffffff);/* FEC:LEC:LSC:FSC */ in dcmi_start_streaming()
767 reg_write(dcmi->regs, DCMI_ESCR, 0xff9d80ff);/* FEC:LEC:LSC:FSC */ in dcmi_start_streaming()
770 reg_write(dcmi->regs, DCMI_CR, val); in dcmi_start_streaming()
773 if (dcmi->do_crop) in dcmi_start_streaming()
774 dcmi_set_crop(dcmi); in dcmi_start_streaming()
777 if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG) in dcmi_start_streaming()
778 reg_set(dcmi->regs, DCMI_CR, CR_CM);/* Snapshot mode */ in dcmi_start_streaming()
780 /* Enable dcmi */ in dcmi_start_streaming()
781 reg_set(dcmi->regs, DCMI_CR, CR_ENABLE); in dcmi_start_streaming()
783 dcmi->sequence = 0; in dcmi_start_streaming()
784 dcmi->errors_count = 0; in dcmi_start_streaming()
785 dcmi->overrun_count = 0; in dcmi_start_streaming()
786 dcmi->buffers_count = 0; in dcmi_start_streaming()
792 if (list_empty(&dcmi->buffers)) { in dcmi_start_streaming()
793 dev_dbg(dcmi->dev, "Start streaming is deferred to next buffer queueing\n"); in dcmi_start_streaming()
794 dcmi->state = WAIT_FOR_BUFFER; in dcmi_start_streaming()
795 spin_unlock_irq(&dcmi->irqlock); in dcmi_start_streaming()
799 buf = list_entry(dcmi->buffers.next, struct dcmi_buf, list); in dcmi_start_streaming()
800 dcmi->active = buf; in dcmi_start_streaming()
802 dcmi->state = RUNNING; in dcmi_start_streaming()
804 dev_dbg(dcmi->dev, "Start streaming, starting capture\n"); in dcmi_start_streaming()
806 spin_unlock_irq(&dcmi->irqlock); in dcmi_start_streaming()
807 ret = dcmi_start_capture(dcmi, buf); in dcmi_start_streaming()
809 dev_err(dcmi->dev, "%s: Start streaming failed, cannot start capture\n", in dcmi_start_streaming()
815 if (dcmi->sd_format->fourcc == V4L2_PIX_FMT_JPEG) in dcmi_start_streaming()
816 reg_set(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR); in dcmi_start_streaming()
818 reg_set(dcmi->regs, DCMI_IER, IT_OVR | IT_ERR); in dcmi_start_streaming()
823 v4l2_subdev_call(dcmi->s_subdev, video, s_stream, 0); in dcmi_start_streaming()
826 video_device_pipeline_stop(dcmi->vdev); in dcmi_start_streaming()
829 pm_runtime_put(dcmi->dev); in dcmi_start_streaming()
831 spin_lock_irq(&dcmi->irqlock); in dcmi_start_streaming()
836 list_for_each_entry_safe(buf, node, &dcmi->buffers, list) { in dcmi_start_streaming()
837 list_del_init(&buf->list); in dcmi_start_streaming()
838 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED); in dcmi_start_streaming()
840 dcmi->active = NULL; in dcmi_start_streaming()
841 spin_unlock_irq(&dcmi->irqlock); in dcmi_start_streaming()
848 struct stm32_dcmi *dcmi = vb2_get_drv_priv(vq); in dcmi_stop_streaming() local
852 ret = v4l2_subdev_call(dcmi->s_subdev, video, s_stream, 0); in dcmi_stop_streaming()
854 dev_err(dcmi->dev, "%s: Failed to stop source subdev, error (%d)\n", in dcmi_stop_streaming()
857 video_device_pipeline_stop(dcmi->vdev); in dcmi_stop_streaming()
859 spin_lock_irq(&dcmi->irqlock); in dcmi_stop_streaming()
862 reg_clear(dcmi->regs, DCMI_IER, IT_FRAME | IT_OVR | IT_ERR); in dcmi_stop_streaming()
864 /* Disable DCMI */ in dcmi_stop_streaming()
865 reg_clear(dcmi->regs, DCMI_CR, CR_ENABLE); in dcmi_stop_streaming()
868 list_for_each_entry_safe(buf, node, &dcmi->buffers, list) { in dcmi_stop_streaming()
869 list_del_init(&buf->list); in dcmi_stop_streaming()
870 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); in dcmi_stop_streaming()
873 dcmi->active = NULL; in dcmi_stop_streaming()
874 dcmi->state = STOPPED; in dcmi_stop_streaming()
876 spin_unlock_irq(&dcmi->irqlock); in dcmi_stop_streaming()
879 mutex_lock(&dcmi->dma_lock); in dcmi_stop_streaming()
880 dmaengine_terminate_sync(dcmi->dma_chan); in dcmi_stop_streaming()
881 mutex_unlock(&dcmi->dma_lock); in dcmi_stop_streaming()
883 pm_runtime_put(dcmi->dev); in dcmi_stop_streaming()
885 if (dcmi->errors_count) in dcmi_stop_streaming()
886 dev_warn(dcmi->dev, "Some errors found while streaming: errors=%d (overrun=%d), buffers=%d\n", in dcmi_stop_streaming()
887 dcmi->errors_count, dcmi->overrun_count, in dcmi_stop_streaming()
888 dcmi->buffers_count); in dcmi_stop_streaming()
889 dev_dbg(dcmi->dev, "Stop streaming, errors=%d (overrun=%d), buffers=%d\n", in dcmi_stop_streaming()
890 dcmi->errors_count, dcmi->overrun_count, in dcmi_stop_streaming()
891 dcmi->buffers_count); in dcmi_stop_streaming()
908 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_g_fmt_vid_cap() local
910 *fmt = dcmi->fmt; in dcmi_g_fmt_vid_cap()
915 static const struct dcmi_format *find_format_by_fourcc(struct stm32_dcmi *dcmi, in find_format_by_fourcc() argument
918 unsigned int num_formats = dcmi->num_of_sd_formats; in find_format_by_fourcc()
923 fmt = dcmi->sd_formats[i]; in find_format_by_fourcc()
924 if (fmt->fourcc == fourcc) in find_format_by_fourcc()
931 static void __find_outer_frame_size(struct stm32_dcmi *dcmi, in __find_outer_frame_size() argument
939 for (i = 0; i < dcmi->num_of_sd_framesizes; i++) { in __find_outer_frame_size()
940 struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i]; in __find_outer_frame_size()
941 int w_err = (fsize->width - pix->width); in __find_outer_frame_size()
942 int h_err = (fsize->height - pix->height); in __find_outer_frame_size()
951 match = &dcmi->sd_framesizes[0]; in __find_outer_frame_size()
956 static int dcmi_try_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f, in dcmi_try_fmt() argument
962 struct v4l2_pix_format *pix = &f->fmt.pix; in dcmi_try_fmt()
969 sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat); in dcmi_try_fmt()
971 if (!dcmi->num_of_sd_formats) in dcmi_try_fmt()
972 return -ENODATA; in dcmi_try_fmt()
974 sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1]; in dcmi_try_fmt()
975 pix->pixelformat = sd_fmt->fourcc; in dcmi_try_fmt()
979 pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH); in dcmi_try_fmt()
980 pix->height = clamp(pix->height, MIN_HEIGHT, MAX_HEIGHT); in dcmi_try_fmt()
983 do_crop = dcmi->do_crop && (pix->pixelformat != V4L2_PIX_FMT_JPEG); in dcmi_try_fmt()
985 if (do_crop && dcmi->num_of_sd_framesizes) { in dcmi_try_fmt()
991 __find_outer_frame_size(dcmi, pix, &outer_sd_fsize); in dcmi_try_fmt()
992 pix->width = outer_sd_fsize.width; in dcmi_try_fmt()
993 pix->height = outer_sd_fsize.height; in dcmi_try_fmt()
996 v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code); in dcmi_try_fmt()
997 ret = v4l2_subdev_call_state_try(dcmi->source, pad, set_fmt, &format); in dcmi_try_fmt()
1005 sd_fsize.width = pix->width; in dcmi_try_fmt()
1006 sd_fsize.height = pix->height; in dcmi_try_fmt()
1009 struct v4l2_rect c = dcmi->crop; in dcmi_try_fmt()
1018 max_rect.width = pix->width; in dcmi_try_fmt()
1019 max_rect.height = pix->height; in dcmi_try_fmt()
1021 c.top = clamp_t(s32, c.top, 0, pix->height - c.height); in dcmi_try_fmt()
1022 c.left = clamp_t(s32, c.left, 0, pix->width - c.width); in dcmi_try_fmt()
1023 dcmi->crop = c; in dcmi_try_fmt()
1026 pix->width = dcmi->crop.width; in dcmi_try_fmt()
1027 pix->height = dcmi->crop.height; in dcmi_try_fmt()
1030 pix->field = V4L2_FIELD_NONE; in dcmi_try_fmt()
1031 pix->bytesperline = pix->width * sd_fmt->bpp; in dcmi_try_fmt()
1032 pix->sizeimage = pix->bytesperline * pix->height; in dcmi_try_fmt()
1042 static int dcmi_set_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f) in dcmi_set_fmt() argument
1050 struct v4l2_pix_format *pix = &f->fmt.pix; in dcmi_set_fmt()
1059 ret = dcmi_try_fmt(dcmi, f, &sd_format, &sd_framesize); in dcmi_set_fmt()
1064 if (pix->pixelformat == V4L2_PIX_FMT_JPEG && in dcmi_set_fmt()
1065 dcmi->bus_type != V4L2_MBUS_BT656) in dcmi_set_fmt()
1066 dcmi->do_crop = false; in dcmi_set_fmt()
1070 sd_format->mbus_code); in dcmi_set_fmt()
1071 mf->width = sd_framesize.width; in dcmi_set_fmt()
1072 mf->height = sd_framesize.height; in dcmi_set_fmt()
1074 ret = dcmi_pipeline_s_fmt(dcmi, &format); in dcmi_set_fmt()
1078 dev_dbg(dcmi->dev, "Sensor format set to 0x%x %ux%u\n", in dcmi_set_fmt()
1079 mf->code, mf->width, mf->height); in dcmi_set_fmt()
1080 dev_dbg(dcmi->dev, "Buffer format set to %4.4s %ux%u\n", in dcmi_set_fmt()
1081 (char *)&pix->pixelformat, in dcmi_set_fmt()
1082 pix->width, pix->height); in dcmi_set_fmt()
1084 dcmi->fmt = *f; in dcmi_set_fmt()
1085 dcmi->sd_format = sd_format; in dcmi_set_fmt()
1086 dcmi->sd_framesize = sd_framesize; in dcmi_set_fmt()
1094 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_s_fmt_vid_cap() local
1096 if (vb2_is_streaming(&dcmi->queue)) in dcmi_s_fmt_vid_cap()
1097 return -EBUSY; in dcmi_s_fmt_vid_cap()
1099 return dcmi_set_fmt(dcmi, f); in dcmi_s_fmt_vid_cap()
1105 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_try_fmt_vid_cap() local
1107 return dcmi_try_fmt(dcmi, f, NULL, NULL); in dcmi_try_fmt_vid_cap()
1113 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_enum_fmt_vid_cap() local
1115 if (f->index >= dcmi->num_of_sd_formats) in dcmi_enum_fmt_vid_cap()
1116 return -EINVAL; in dcmi_enum_fmt_vid_cap()
1118 f->pixelformat = dcmi->sd_formats[f->index]->fourcc; in dcmi_enum_fmt_vid_cap()
1122 static int dcmi_get_sensor_format(struct stm32_dcmi *dcmi, in dcmi_get_sensor_format() argument
1130 ret = v4l2_subdev_call(dcmi->source, pad, get_fmt, NULL, &fmt); in dcmi_get_sensor_format()
1139 static int dcmi_set_sensor_format(struct stm32_dcmi *dcmi, in dcmi_set_sensor_format() argument
1148 sd_fmt = find_format_by_fourcc(dcmi, pix->pixelformat); in dcmi_set_sensor_format()
1150 if (!dcmi->num_of_sd_formats) in dcmi_set_sensor_format()
1151 return -ENODATA; in dcmi_set_sensor_format()
1153 sd_fmt = dcmi->sd_formats[dcmi->num_of_sd_formats - 1]; in dcmi_set_sensor_format()
1154 pix->pixelformat = sd_fmt->fourcc; in dcmi_set_sensor_format()
1157 v4l2_fill_mbus_format(&format.format, pix, sd_fmt->mbus_code); in dcmi_set_sensor_format()
1158 ret = v4l2_subdev_call_state_try(dcmi->source, pad, set_fmt, &format); in dcmi_set_sensor_format()
1165 static int dcmi_get_sensor_bounds(struct stm32_dcmi *dcmi, in dcmi_get_sensor_bounds() argument
1180 ret = v4l2_subdev_call(dcmi->source, pad, get_selection, in dcmi_get_sensor_bounds()
1184 if (ret != -ENOIOCTLCMD) in dcmi_get_sensor_bounds()
1195 for (i = 0; i < dcmi->num_of_sd_framesizes; i++) { in dcmi_get_sensor_bounds()
1196 struct dcmi_framesize *fsize = &dcmi->sd_framesizes[i]; in dcmi_get_sensor_bounds()
1197 unsigned int pixsize = fsize->width * fsize->height; in dcmi_get_sensor_bounds()
1201 max_width = fsize->width; in dcmi_get_sensor_bounds()
1202 max_height = fsize->height; in dcmi_get_sensor_bounds()
1206 r->top = 0; in dcmi_get_sensor_bounds()
1207 r->left = 0; in dcmi_get_sensor_bounds()
1208 r->width = max_width; in dcmi_get_sensor_bounds()
1209 r->height = max_height; in dcmi_get_sensor_bounds()
1217 ret = dcmi_get_sensor_format(dcmi, &pix); in dcmi_get_sensor_bounds()
1221 r->top = 0; in dcmi_get_sensor_bounds()
1222 r->left = 0; in dcmi_get_sensor_bounds()
1223 r->width = pix.width; in dcmi_get_sensor_bounds()
1224 r->height = pix.height; in dcmi_get_sensor_bounds()
1232 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_g_selection() local
1234 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) in dcmi_g_selection()
1235 return -EINVAL; in dcmi_g_selection()
1237 switch (s->target) { in dcmi_g_selection()
1240 s->r = dcmi->sd_bounds; in dcmi_g_selection()
1243 if (dcmi->do_crop) { in dcmi_g_selection()
1244 s->r = dcmi->crop; in dcmi_g_selection()
1246 s->r.top = 0; in dcmi_g_selection()
1247 s->r.left = 0; in dcmi_g_selection()
1248 s->r.width = dcmi->fmt.fmt.pix.width; in dcmi_g_selection()
1249 s->r.height = dcmi->fmt.fmt.pix.height; in dcmi_g_selection()
1253 return -EINVAL; in dcmi_g_selection()
1262 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_s_selection() local
1263 struct v4l2_rect r = s->r; in dcmi_s_selection()
1267 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || in dcmi_s_selection()
1268 s->target != V4L2_SEL_TGT_CROP) in dcmi_s_selection()
1269 return -EINVAL; in dcmi_s_selection()
1272 pix.pixelformat = dcmi->fmt.fmt.pix.pixelformat; in dcmi_s_selection()
1273 pix.width = dcmi->sd_bounds.width; in dcmi_s_selection()
1274 pix.height = dcmi->sd_bounds.height; in dcmi_s_selection()
1275 dcmi_set_sensor_format(dcmi, &pix); in dcmi_s_selection()
1287 r.top = clamp_t(s32, r.top, 0, pix.height - r.height); in dcmi_s_selection()
1288 r.left = clamp_t(s32, r.left, 0, pix.width - r.width); in dcmi_s_selection()
1290 if (!(r.top == dcmi->sd_bounds.top && in dcmi_s_selection()
1291 r.left == dcmi->sd_bounds.left && in dcmi_s_selection()
1292 r.width == dcmi->sd_bounds.width && in dcmi_s_selection()
1293 r.height == dcmi->sd_bounds.height)) { in dcmi_s_selection()
1295 dcmi->do_crop = true; in dcmi_s_selection()
1296 dcmi->crop = r; in dcmi_s_selection()
1297 dev_dbg(dcmi->dev, "s_selection: crop %ux%u@(%u,%u) from %ux%u\n", in dcmi_s_selection()
1302 dcmi->do_crop = false; in dcmi_s_selection()
1303 dev_dbg(dcmi->dev, "s_selection: crop is disabled\n"); in dcmi_s_selection()
1306 s->r = r; in dcmi_s_selection()
1313 strscpy(cap->driver, DRV_NAME, sizeof(cap->driver)); in dcmi_querycap()
1314 strscpy(cap->card, "STM32 Camera Memory Interface", in dcmi_querycap()
1315 sizeof(cap->card)); in dcmi_querycap()
1316 strscpy(cap->bus_info, "platform:dcmi", sizeof(cap->bus_info)); in dcmi_querycap()
1323 if (i->index != 0) in dcmi_enum_input()
1324 return -EINVAL; in dcmi_enum_input()
1326 i->type = V4L2_INPUT_TYPE_CAMERA; in dcmi_enum_input()
1327 strscpy(i->name, "Camera", sizeof(i->name)); in dcmi_enum_input()
1340 return -EINVAL; in dcmi_s_input()
1347 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_enum_framesizes() local
1350 .index = fsize->index, in dcmi_enum_framesizes()
1355 sd_fmt = find_format_by_fourcc(dcmi, fsize->pixel_format); in dcmi_enum_framesizes()
1357 return -EINVAL; in dcmi_enum_framesizes()
1359 fse.code = sd_fmt->mbus_code; in dcmi_enum_framesizes()
1361 ret = v4l2_subdev_call(dcmi->source, pad, enum_frame_size, in dcmi_enum_framesizes()
1366 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; in dcmi_enum_framesizes()
1367 fsize->discrete.width = fse.max_width; in dcmi_enum_framesizes()
1368 fsize->discrete.height = fse.max_height; in dcmi_enum_framesizes()
1376 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_g_parm() local
1378 return v4l2_g_parm_cap(video_devdata(file), dcmi->source, p); in dcmi_g_parm()
1384 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_s_parm() local
1386 return v4l2_s_parm_cap(video_devdata(file), dcmi->source, p); in dcmi_s_parm()
1392 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_enum_frameintervals() local
1395 .index = fival->index, in dcmi_enum_frameintervals()
1396 .width = fival->width, in dcmi_enum_frameintervals()
1397 .height = fival->height, in dcmi_enum_frameintervals()
1402 sd_fmt = find_format_by_fourcc(dcmi, fival->pixel_format); in dcmi_enum_frameintervals()
1404 return -EINVAL; in dcmi_enum_frameintervals()
1406 fie.code = sd_fmt->mbus_code; in dcmi_enum_frameintervals()
1408 ret = v4l2_subdev_call(dcmi->source, pad, in dcmi_enum_frameintervals()
1413 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; in dcmi_enum_frameintervals()
1414 fival->discrete = fie.interval; in dcmi_enum_frameintervals()
1420 { .compatible = "st,stm32-dcmi"},
1427 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_open() local
1428 struct v4l2_subdev *sd = dcmi->source; in dcmi_open()
1431 if (mutex_lock_interruptible(&dcmi->lock)) in dcmi_open()
1432 return -ERESTARTSYS; in dcmi_open()
1442 if (ret < 0 && ret != -ENOIOCTLCMD) in dcmi_open()
1445 ret = dcmi_set_fmt(dcmi, &dcmi->fmt); in dcmi_open()
1452 mutex_unlock(&dcmi->lock); in dcmi_open()
1458 struct stm32_dcmi *dcmi = video_drvdata(file); in dcmi_release() local
1459 struct v4l2_subdev *sd = dcmi->source; in dcmi_release()
1463 mutex_lock(&dcmi->lock); in dcmi_release()
1472 mutex_unlock(&dcmi->lock); in dcmi_release()
1525 static int dcmi_set_default_fmt(struct stm32_dcmi *dcmi) in dcmi_set_default_fmt() argument
1533 .pixelformat = dcmi->sd_formats[0]->fourcc, in dcmi_set_default_fmt()
1538 ret = dcmi_try_fmt(dcmi, &f, NULL, NULL); in dcmi_set_default_fmt()
1541 dcmi->sd_format = dcmi->sd_formats[0]; in dcmi_set_default_fmt()
1542 dcmi->fmt = f; in dcmi_set_default_fmt()
1642 static int dcmi_formats_init(struct stm32_dcmi *dcmi) in dcmi_formats_init() argument
1646 struct v4l2_subdev *subdev = dcmi->source; in dcmi_formats_init()
1659 dcmi->bus_type == V4L2_MBUS_BT656) in dcmi_formats_init()
1664 if (sd_fmts[j]->fourcc == in dcmi_formats_init()
1667 dev_dbg(dcmi->dev, "Skipping fourcc/code: %4.4s/0x%x\n", in dcmi_formats_init()
1668 (char *)&sd_fmts[j]->fourcc, in dcmi_formats_init()
1675 dev_dbg(dcmi->dev, "Supported fourcc/code: %4.4s/0x%x\n", in dcmi_formats_init()
1676 (char *)&sd_fmts[num_fmts - 1]->fourcc, in dcmi_formats_init()
1677 sd_fmts[num_fmts - 1]->mbus_code); in dcmi_formats_init()
1684 return -ENXIO; in dcmi_formats_init()
1686 dcmi->num_of_sd_formats = num_fmts; in dcmi_formats_init()
1687 dcmi->sd_formats = devm_kcalloc(dcmi->dev, in dcmi_formats_init()
1690 if (!dcmi->sd_formats) { in dcmi_formats_init()
1691 dev_err(dcmi->dev, "Could not allocate memory\n"); in dcmi_formats_init()
1692 return -ENOMEM; in dcmi_formats_init()
1695 memcpy(dcmi->sd_formats, sd_fmts, in dcmi_formats_init()
1697 dcmi->sd_format = dcmi->sd_formats[0]; in dcmi_formats_init()
1702 static int dcmi_framesizes_init(struct stm32_dcmi *dcmi) in dcmi_framesizes_init() argument
1705 struct v4l2_subdev *subdev = dcmi->source; in dcmi_framesizes_init()
1708 .code = dcmi->sd_format->mbus_code, in dcmi_framesizes_init()
1722 dcmi->num_of_sd_framesizes = num_fsize; in dcmi_framesizes_init()
1723 dcmi->sd_framesizes = devm_kcalloc(dcmi->dev, num_fsize, in dcmi_framesizes_init()
1726 if (!dcmi->sd_framesizes) { in dcmi_framesizes_init()
1727 dev_err(dcmi->dev, "Could not allocate memory\n"); in dcmi_framesizes_init()
1728 return -ENOMEM; in dcmi_framesizes_init()
1732 dev_dbg(dcmi->dev, "Sensor supports %u frame sizes:\n", num_fsize); in dcmi_framesizes_init()
1733 for (i = 0; i < dcmi->num_of_sd_framesizes; i++) { in dcmi_framesizes_init()
1739 dcmi->sd_framesizes[fse.index].width = fse.max_width; in dcmi_framesizes_init()
1740 dcmi->sd_framesizes[fse.index].height = fse.max_height; in dcmi_framesizes_init()
1741 dev_dbg(dcmi->dev, "%ux%u\n", fse.max_width, fse.max_height); in dcmi_framesizes_init()
1749 struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier); in dcmi_graph_notify_complete() local
1757 dcmi->source = media_entity_to_v4l2_subdev(dcmi_find_source(dcmi)); in dcmi_graph_notify_complete()
1758 if (!dcmi->source) { in dcmi_graph_notify_complete()
1759 dev_err(dcmi->dev, "Source subdevice not found\n"); in dcmi_graph_notify_complete()
1760 return -ENODEV; in dcmi_graph_notify_complete()
1763 dcmi->vdev->ctrl_handler = dcmi->source->ctrl_handler; in dcmi_graph_notify_complete()
1765 ret = dcmi_formats_init(dcmi); in dcmi_graph_notify_complete()
1767 dev_err(dcmi->dev, "No supported mediabus format found\n"); in dcmi_graph_notify_complete()
1771 ret = dcmi_framesizes_init(dcmi); in dcmi_graph_notify_complete()
1773 dev_err(dcmi->dev, "Could not initialize framesizes\n"); in dcmi_graph_notify_complete()
1777 ret = dcmi_get_sensor_bounds(dcmi, &dcmi->sd_bounds); in dcmi_graph_notify_complete()
1779 dev_err(dcmi->dev, "Could not get sensor bounds\n"); in dcmi_graph_notify_complete()
1783 ret = dcmi_set_default_fmt(dcmi); in dcmi_graph_notify_complete()
1785 dev_err(dcmi->dev, "Could not set default format\n"); in dcmi_graph_notify_complete()
1789 ret = devm_request_threaded_irq(dcmi->dev, dcmi->irq, dcmi_irq_callback, in dcmi_graph_notify_complete()
1791 dev_name(dcmi->dev), dcmi); in dcmi_graph_notify_complete()
1793 dev_err(dcmi->dev, "Unable to request irq %d\n", dcmi->irq); in dcmi_graph_notify_complete()
1804 struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier); in dcmi_graph_notify_unbind() local
1806 dev_dbg(dcmi->dev, "Removing %s\n", video_device_node_name(dcmi->vdev)); in dcmi_graph_notify_unbind()
1809 video_unregister_device(dcmi->vdev); in dcmi_graph_notify_unbind()
1816 struct stm32_dcmi *dcmi = notifier_to_dcmi(notifier); in dcmi_graph_notify_bound() local
1820 dev_dbg(dcmi->dev, "Subdev \"%s\" bound\n", subdev->name); in dcmi_graph_notify_bound()
1823 * Link this sub-device to DCMI, it could be in dcmi_graph_notify_bound()
1826 src_pad = media_entity_get_fwnode_pad(&subdev->entity, in dcmi_graph_notify_bound()
1827 subdev->fwnode, in dcmi_graph_notify_bound()
1830 ret = media_create_pad_link(&subdev->entity, src_pad, in dcmi_graph_notify_bound()
1831 &dcmi->vdev->entity, 0, in dcmi_graph_notify_bound()
1835 dev_err(dcmi->dev, "Failed to create media pad link with subdev \"%s\"\n", in dcmi_graph_notify_bound()
1836 subdev->name); in dcmi_graph_notify_bound()
1838 dev_dbg(dcmi->dev, "DCMI is now linked to \"%s\"\n", in dcmi_graph_notify_bound()
1839 subdev->name); in dcmi_graph_notify_bound()
1841 dcmi->s_subdev = subdev; in dcmi_graph_notify_bound()
1852 static int dcmi_graph_init(struct stm32_dcmi *dcmi) in dcmi_graph_init() argument
1858 ep = of_graph_get_endpoint_by_regs(dcmi->dev->of_node, 0, -1); in dcmi_graph_init()
1860 dev_err(dcmi->dev, "Failed to get next endpoint\n"); in dcmi_graph_init()
1861 return -EINVAL; in dcmi_graph_init()
1864 v4l2_async_nf_init(&dcmi->notifier, &dcmi->v4l2_dev); in dcmi_graph_init()
1866 asd = v4l2_async_nf_add_fwnode_remote(&dcmi->notifier, in dcmi_graph_init()
1873 dev_err(dcmi->dev, "Failed to add subdev notifier\n"); in dcmi_graph_init()
1877 dcmi->notifier.ops = &dcmi_graph_notify_ops; in dcmi_graph_init()
1879 ret = v4l2_async_nf_register(&dcmi->notifier); in dcmi_graph_init()
1881 dev_err(dcmi->dev, "Failed to register notifier\n"); in dcmi_graph_init()
1882 v4l2_async_nf_cleanup(&dcmi->notifier); in dcmi_graph_init()
1891 struct device_node *np = pdev->dev.of_node; in dcmi_probe()
1893 struct stm32_dcmi *dcmi; in dcmi_probe() local
1900 dcmi = devm_kzalloc(&pdev->dev, sizeof(struct stm32_dcmi), GFP_KERNEL); in dcmi_probe()
1901 if (!dcmi) in dcmi_probe()
1902 return -ENOMEM; in dcmi_probe()
1904 dcmi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); in dcmi_probe()
1905 if (IS_ERR(dcmi->rstc)) in dcmi_probe()
1906 return dev_err_probe(&pdev->dev, PTR_ERR(dcmi->rstc), in dcmi_probe()
1910 np = of_graph_get_endpoint_by_regs(np, 0, -1); in dcmi_probe()
1912 dev_err(&pdev->dev, "Could not find the endpoint\n"); in dcmi_probe()
1913 return -ENODEV; in dcmi_probe()
1919 dev_err(&pdev->dev, "Could not parse the endpoint\n"); in dcmi_probe()
1924 dev_err(&pdev->dev, "CSI bus not supported\n"); in dcmi_probe()
1925 return -ENODEV; in dcmi_probe()
1930 dev_err(&pdev->dev, "BT656 bus conflicts with %u bits bus width (8 bits required)\n", in dcmi_probe()
1932 return -ENODEV; in dcmi_probe()
1935 dcmi->bus.flags = ep.bus.parallel.flags; in dcmi_probe()
1936 dcmi->bus.bus_width = ep.bus.parallel.bus_width; in dcmi_probe()
1937 dcmi->bus.data_shift = ep.bus.parallel.data_shift; in dcmi_probe()
1938 dcmi->bus_type = ep.bus_type; in dcmi_probe()
1940 dcmi->irq = platform_get_irq(pdev, 0); in dcmi_probe()
1941 if (dcmi->irq < 0) in dcmi_probe()
1942 return dcmi->irq; in dcmi_probe()
1944 dcmi->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &dcmi->res); in dcmi_probe()
1945 if (IS_ERR(dcmi->regs)) in dcmi_probe()
1946 return PTR_ERR(dcmi->regs); in dcmi_probe()
1948 mclk = devm_clk_get(&pdev->dev, "mclk"); in dcmi_probe()
1950 return dev_err_probe(&pdev->dev, PTR_ERR(mclk), in dcmi_probe()
1953 chan = dma_request_chan(&pdev->dev, "tx"); in dcmi_probe()
1955 return dev_err_probe(&pdev->dev, PTR_ERR(chan), in dcmi_probe()
1958 dcmi->dma_max_burst = UINT_MAX; in dcmi_probe()
1961 dcmi->dma_max_burst = caps.max_sg_burst * DMA_SLAVE_BUSWIDTH_4_BYTES; in dcmi_probe()
1963 spin_lock_init(&dcmi->irqlock); in dcmi_probe()
1964 mutex_init(&dcmi->lock); in dcmi_probe()
1965 mutex_init(&dcmi->dma_lock); in dcmi_probe()
1966 init_completion(&dcmi->complete); in dcmi_probe()
1967 INIT_LIST_HEAD(&dcmi->buffers); in dcmi_probe()
1969 dcmi->dev = &pdev->dev; in dcmi_probe()
1970 dcmi->mclk = mclk; in dcmi_probe()
1971 dcmi->state = STOPPED; in dcmi_probe()
1972 dcmi->dma_chan = chan; in dcmi_probe()
1974 q = &dcmi->queue; in dcmi_probe()
1976 dcmi->v4l2_dev.mdev = &dcmi->mdev; in dcmi_probe()
1979 strscpy(dcmi->mdev.model, DRV_NAME, sizeof(dcmi->mdev.model)); in dcmi_probe()
1980 dcmi->mdev.dev = &pdev->dev; in dcmi_probe()
1981 media_device_init(&dcmi->mdev); in dcmi_probe()
1983 /* Initialize the top-level structure */ in dcmi_probe()
1984 ret = v4l2_device_register(&pdev->dev, &dcmi->v4l2_dev); in dcmi_probe()
1988 dcmi->vdev = video_device_alloc(); in dcmi_probe()
1989 if (!dcmi->vdev) { in dcmi_probe()
1990 ret = -ENOMEM; in dcmi_probe()
1995 dcmi->vdev->fops = &dcmi_fops; in dcmi_probe()
1996 dcmi->vdev->v4l2_dev = &dcmi->v4l2_dev; in dcmi_probe()
1997 dcmi->vdev->queue = &dcmi->queue; in dcmi_probe()
1998 strscpy(dcmi->vdev->name, KBUILD_MODNAME, sizeof(dcmi->vdev->name)); in dcmi_probe()
1999 dcmi->vdev->release = video_device_release; in dcmi_probe()
2000 dcmi->vdev->ioctl_ops = &dcmi_ioctl_ops; in dcmi_probe()
2001 dcmi->vdev->lock = &dcmi->lock; in dcmi_probe()
2002 dcmi->vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | in dcmi_probe()
2004 video_set_drvdata(dcmi->vdev, dcmi); in dcmi_probe()
2007 dcmi->vid_cap_pad.flags = MEDIA_PAD_FL_SINK; in dcmi_probe()
2008 ret = media_entity_pads_init(&dcmi->vdev->entity, in dcmi_probe()
2009 1, &dcmi->vid_cap_pad); in dcmi_probe()
2011 dev_err(dcmi->dev, "Failed to init media entity pad\n"); in dcmi_probe()
2014 dcmi->vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT; in dcmi_probe()
2016 ret = video_register_device(dcmi->vdev, VFL_TYPE_VIDEO, -1); in dcmi_probe()
2018 dev_err(dcmi->dev, "Failed to register video device\n"); in dcmi_probe()
2022 dev_dbg(dcmi->dev, "Device registered as %s\n", in dcmi_probe()
2023 video_device_node_name(dcmi->vdev)); in dcmi_probe()
2026 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; in dcmi_probe()
2027 q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF; in dcmi_probe()
2028 q->lock = &dcmi->lock; in dcmi_probe()
2029 q->drv_priv = dcmi; in dcmi_probe()
2030 q->buf_struct_size = sizeof(struct dcmi_buf); in dcmi_probe()
2031 q->ops = &dcmi_video_qops; in dcmi_probe()
2032 q->mem_ops = &vb2_dma_contig_memops; in dcmi_probe()
2033 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; in dcmi_probe()
2034 q->min_queued_buffers = 2; in dcmi_probe()
2035 q->allow_cache_hints = 1; in dcmi_probe()
2036 q->dev = &pdev->dev; in dcmi_probe()
2040 dev_err(&pdev->dev, "Failed to initialize vb2 queue\n"); in dcmi_probe()
2044 ret = dcmi_graph_init(dcmi); in dcmi_probe()
2049 ret = reset_control_assert(dcmi->rstc); in dcmi_probe()
2051 dev_err(&pdev->dev, "Failed to assert the reset line\n"); in dcmi_probe()
2057 ret = reset_control_deassert(dcmi->rstc); in dcmi_probe()
2059 dev_err(&pdev->dev, "Failed to deassert the reset line\n"); in dcmi_probe()
2063 dev_info(&pdev->dev, "Probe done\n"); in dcmi_probe()
2065 platform_set_drvdata(pdev, dcmi); in dcmi_probe()
2067 pm_runtime_enable(&pdev->dev); in dcmi_probe()
2072 v4l2_async_nf_cleanup(&dcmi->notifier); in dcmi_probe()
2074 media_entity_cleanup(&dcmi->vdev->entity); in dcmi_probe()
2076 video_device_release(dcmi->vdev); in dcmi_probe()
2078 v4l2_device_unregister(&dcmi->v4l2_dev); in dcmi_probe()
2080 media_device_cleanup(&dcmi->mdev); in dcmi_probe()
2081 dma_release_channel(dcmi->dma_chan); in dcmi_probe()
2088 struct stm32_dcmi *dcmi = platform_get_drvdata(pdev); in dcmi_remove() local
2090 pm_runtime_disable(&pdev->dev); in dcmi_remove()
2092 v4l2_async_nf_unregister(&dcmi->notifier); in dcmi_remove()
2093 v4l2_async_nf_cleanup(&dcmi->notifier); in dcmi_remove()
2094 media_entity_cleanup(&dcmi->vdev->entity); in dcmi_remove()
2095 v4l2_device_unregister(&dcmi->v4l2_dev); in dcmi_remove()
2096 media_device_cleanup(&dcmi->mdev); in dcmi_remove()
2098 dma_release_channel(dcmi->dma_chan); in dcmi_remove()
2103 struct stm32_dcmi *dcmi = dev_get_drvdata(dev); in dcmi_runtime_suspend() local
2105 clk_disable_unprepare(dcmi->mclk); in dcmi_runtime_suspend()
2112 struct stm32_dcmi *dcmi = dev_get_drvdata(dev); in dcmi_runtime_resume() local
2115 ret = clk_prepare_enable(dcmi->mclk); in dcmi_runtime_resume()
2164 MODULE_DESCRIPTION("STMicroelectronics STM32 Digital Camera Memory Interface driver");