Lines Matching full:cam
95 #define sensor_call(cam, o, f, args...) \ argument
96 v4l2_subdev_call(cam->sensor, o, f, ##args)
210 static void mcam_buffer_done(struct mcam_camera *cam, int frame, in mcam_buffer_done() argument
213 vbuf->vb2_buf.planes[0].bytesused = cam->pix_format.sizeimage; in mcam_buffer_done()
214 vbuf->sequence = cam->buf_seq[frame]; in mcam_buffer_done()
217 vb2_set_plane_payload(&vbuf->vb2_buf, 0, cam->pix_format.sizeimage); in mcam_buffer_done()
226 #define cam_err(cam, fmt, arg...) \ argument
227 dev_err((cam)->dev, fmt, ##arg);
228 #define cam_warn(cam, fmt, arg...) \ argument
229 dev_warn((cam)->dev, fmt, ##arg);
230 #define cam_dbg(cam, fmt, arg...) \ argument
231 dev_dbg((cam)->dev, fmt, ##arg);
237 static void mcam_reset_buffers(struct mcam_camera *cam) in mcam_reset_buffers() argument
241 cam->next_buf = -1; in mcam_reset_buffers()
242 for (i = 0; i < cam->nbufs; i++) { in mcam_reset_buffers()
243 clear_bit(i, &cam->flags); in mcam_reset_buffers()
244 clear_bit(CF_FRAME_SOF0 + i, &cam->flags); in mcam_reset_buffers()
248 static inline int mcam_needs_config(struct mcam_camera *cam) in mcam_needs_config() argument
250 return test_bit(CF_CONFIG_NEEDED, &cam->flags); in mcam_needs_config()
253 static void mcam_set_config_needed(struct mcam_camera *cam, int needed) in mcam_set_config_needed() argument
256 set_bit(CF_CONFIG_NEEDED, &cam->flags); in mcam_set_config_needed()
258 clear_bit(CF_CONFIG_NEEDED, &cam->flags); in mcam_set_config_needed()
266 static void mcam_ctlr_start(struct mcam_camera *cam) in mcam_ctlr_start() argument
270 mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE); in mcam_ctlr_start()
273 static void mcam_ctlr_stop(struct mcam_camera *cam) in mcam_ctlr_stop() argument
275 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE); in mcam_ctlr_stop()
324 static void mcam_write_yuv_bases(struct mcam_camera *cam, in mcam_write_yuv_bases() argument
327 struct v4l2_pix_format *fmt = &cam->pix_format; in mcam_write_yuv_bases()
346 mcam_reg_write(cam, REG_Y0BAR + frame * 4, y); in mcam_write_yuv_bases()
348 mcam_reg_write(cam, REG_U0BAR + frame * 4, u); in mcam_write_yuv_bases()
349 mcam_reg_write(cam, REG_V0BAR + frame * 4, v); in mcam_write_yuv_bases()
363 static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime) in mcam_alloc_dma_bufs() argument
367 mcam_set_config_needed(cam, 1); in mcam_alloc_dma_bufs()
369 cam->dma_buf_size = dma_buf_size; in mcam_alloc_dma_bufs()
371 cam->dma_buf_size = cam->pix_format.sizeimage; in mcam_alloc_dma_bufs()
375 cam->nbufs = 0; in mcam_alloc_dma_bufs()
377 cam->dma_bufs[i] = dma_alloc_coherent(cam->dev, in mcam_alloc_dma_bufs()
378 cam->dma_buf_size, cam->dma_handles + i, in mcam_alloc_dma_bufs()
380 if (cam->dma_bufs[i] == NULL) { in mcam_alloc_dma_bufs()
381 cam_warn(cam, "Failed to allocate DMA buffer\n"); in mcam_alloc_dma_bufs()
384 (cam->nbufs)++; in mcam_alloc_dma_bufs()
387 switch (cam->nbufs) { in mcam_alloc_dma_bufs()
389 dma_free_coherent(cam->dev, cam->dma_buf_size, in mcam_alloc_dma_bufs()
390 cam->dma_bufs[0], cam->dma_handles[0]); in mcam_alloc_dma_bufs()
391 cam->nbufs = 0; in mcam_alloc_dma_bufs()
394 cam_err(cam, "Insufficient DMA buffers, cannot operate\n"); in mcam_alloc_dma_bufs()
399 cam_warn(cam, "Will limp along with only 2 buffers\n"); in mcam_alloc_dma_bufs()
405 static void mcam_free_dma_bufs(struct mcam_camera *cam) in mcam_free_dma_bufs() argument
409 for (i = 0; i < cam->nbufs; i++) { in mcam_free_dma_bufs()
410 dma_free_coherent(cam->dev, cam->dma_buf_size, in mcam_free_dma_bufs()
411 cam->dma_bufs[i], cam->dma_handles[i]); in mcam_free_dma_bufs()
412 cam->dma_bufs[i] = NULL; in mcam_free_dma_bufs()
414 cam->nbufs = 0; in mcam_free_dma_bufs()
421 static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam) in mcam_ctlr_dma_vmalloc() argument
428 mcam_write_yuv_bases(cam, 0, cam->dma_handles[0]); in mcam_ctlr_dma_vmalloc()
429 mcam_write_yuv_bases(cam, 1, cam->dma_handles[1]); in mcam_ctlr_dma_vmalloc()
430 if (cam->nbufs > 2) { in mcam_ctlr_dma_vmalloc()
431 mcam_write_yuv_bases(cam, 2, cam->dma_handles[2]); in mcam_ctlr_dma_vmalloc()
432 mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS); in mcam_ctlr_dma_vmalloc()
434 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS); in mcam_ctlr_dma_vmalloc()
435 if (cam->chip_id == MCAM_CAFE) in mcam_ctlr_dma_vmalloc()
436 mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */ in mcam_ctlr_dma_vmalloc()
444 struct mcam_camera *cam = from_work(cam, t, s_bh_work); in mcam_frame_work() local
449 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_frame_work()
450 for (i = 0; i < cam->nbufs; i++) { in mcam_frame_work()
451 int bufno = cam->next_buf; in mcam_frame_work()
453 if (cam->state != S_STREAMING || bufno < 0) in mcam_frame_work()
455 if (++(cam->next_buf) >= cam->nbufs) in mcam_frame_work()
456 cam->next_buf = 0; in mcam_frame_work()
457 if (!test_bit(bufno, &cam->flags)) in mcam_frame_work()
459 if (list_empty(&cam->buffers)) { in mcam_frame_work()
460 cam->frame_state.singles++; in mcam_frame_work()
463 cam->frame_state.delivered++; in mcam_frame_work()
464 clear_bit(bufno, &cam->flags); in mcam_frame_work()
465 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, in mcam_frame_work()
471 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_frame_work()
473 cam->dma_bufs[bufno], in mcam_frame_work()
474 cam->pix_format.sizeimage); in mcam_frame_work()
475 mcam_buffer_done(cam, bufno, &buf->vb_buf); in mcam_frame_work()
476 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_frame_work()
478 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_frame_work()
485 static int mcam_check_dma_buffers(struct mcam_camera *cam) in mcam_check_dma_buffers() argument
487 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage) in mcam_check_dma_buffers()
488 mcam_free_dma_bufs(cam); in mcam_check_dma_buffers()
489 if (cam->nbufs == 0) in mcam_check_dma_buffers()
490 return mcam_alloc_dma_bufs(cam, 0); in mcam_check_dma_buffers()
494 static void mcam_vmalloc_done(struct mcam_camera *cam, int frame) in mcam_vmalloc_done() argument
496 queue_work(system_bh_wq, &cam->s_bh_work); in mcam_vmalloc_done()
501 static inline int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime) in mcam_alloc_dma_bufs() argument
506 static inline void mcam_free_dma_bufs(struct mcam_camera *cam) in mcam_free_dma_bufs() argument
511 static inline int mcam_check_dma_buffers(struct mcam_camera *cam) in mcam_check_dma_buffers() argument
535 static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame) in mcam_set_contig_buffer() argument
544 if (list_empty(&cam->buffers)) { in mcam_set_contig_buffer()
545 buf = cam->vb_bufs[frame ^ 0x1]; in mcam_set_contig_buffer()
546 set_bit(CF_SINGLE_BUFFER, &cam->flags); in mcam_set_contig_buffer()
547 cam->frame_state.singles++; in mcam_set_contig_buffer()
552 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, in mcam_set_contig_buffer()
555 clear_bit(CF_SINGLE_BUFFER, &cam->flags); in mcam_set_contig_buffer()
558 cam->vb_bufs[frame] = buf; in mcam_set_contig_buffer()
562 mcam_write_yuv_bases(cam, frame, dma_handle); in mcam_set_contig_buffer()
568 static void mcam_ctlr_dma_contig(struct mcam_camera *cam) in mcam_ctlr_dma_contig() argument
570 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS); in mcam_ctlr_dma_contig()
571 cam->nbufs = 2; in mcam_ctlr_dma_contig()
572 mcam_set_contig_buffer(cam, 0); in mcam_ctlr_dma_contig()
573 mcam_set_contig_buffer(cam, 1); in mcam_ctlr_dma_contig()
579 static void mcam_dma_contig_done(struct mcam_camera *cam, int frame) in mcam_dma_contig_done() argument
581 struct mcam_vb_buffer *buf = cam->vb_bufs[frame]; in mcam_dma_contig_done()
583 if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) { in mcam_dma_contig_done()
584 cam->frame_state.delivered++; in mcam_dma_contig_done()
585 cam->vb_bufs[frame] = NULL; in mcam_dma_contig_done()
586 mcam_buffer_done(cam, frame, &buf->vb_buf); in mcam_dma_contig_done()
588 mcam_set_contig_buffer(cam, frame); in mcam_dma_contig_done()
603 static void mcam_sg_next_buffer(struct mcam_camera *cam) in mcam_sg_next_buffer() argument
608 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue); in mcam_sg_next_buffer()
615 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA); in mcam_sg_next_buffer()
616 mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa); in mcam_sg_next_buffer()
617 mcam_reg_write(cam, REG_DESC_LEN_Y, in mcam_sg_next_buffer()
619 mcam_reg_write(cam, REG_DESC_LEN_U, 0); in mcam_sg_next_buffer()
620 mcam_reg_write(cam, REG_DESC_LEN_V, 0); in mcam_sg_next_buffer()
621 mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA); in mcam_sg_next_buffer()
622 cam->vb_bufs[0] = buf; in mcam_sg_next_buffer()
628 static void mcam_ctlr_dma_sg(struct mcam_camera *cam) in mcam_ctlr_dma_sg() argument
634 if (list_empty(&cam->buffers)) { in mcam_ctlr_dma_sg()
635 set_bit(CF_SG_RESTART, &cam->flags); in mcam_ctlr_dma_sg()
639 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD); in mcam_ctlr_dma_sg()
640 mcam_sg_next_buffer(cam); in mcam_ctlr_dma_sg()
641 cam->nbufs = 3; in mcam_ctlr_dma_sg()
658 static void mcam_dma_sg_done(struct mcam_camera *cam, int frame) in mcam_dma_sg_done() argument
660 struct mcam_vb_buffer *buf = cam->vb_bufs[0]; in mcam_dma_sg_done()
665 if (cam->state != S_STREAMING) in mcam_dma_sg_done()
671 if (!list_empty(&cam->buffers)) { in mcam_dma_sg_done()
672 mcam_sg_next_buffer(cam); in mcam_dma_sg_done()
673 mcam_ctlr_start(cam); in mcam_dma_sg_done()
679 set_bit(CF_SG_RESTART, &cam->flags); in mcam_dma_sg_done()
680 cam->frame_state.singles++; in mcam_dma_sg_done()
681 cam->vb_bufs[0] = NULL; in mcam_dma_sg_done()
686 cam->frame_state.delivered++; in mcam_dma_sg_done()
687 mcam_buffer_done(cam, frame, &buf->vb_buf); in mcam_dma_sg_done()
697 static void mcam_sg_restart(struct mcam_camera *cam) in mcam_sg_restart() argument
699 mcam_ctlr_dma_sg(cam); in mcam_sg_restart()
700 mcam_ctlr_start(cam); in mcam_sg_restart()
701 clear_bit(CF_SG_RESTART, &cam->flags); in mcam_sg_restart()
706 static inline void mcam_sg_restart(struct mcam_camera *cam) in mcam_sg_restart() argument
721 static void mcam_ctlr_image(struct mcam_camera *cam) in mcam_ctlr_image() argument
723 struct v4l2_pix_format *fmt = &cam->pix_format; in mcam_ctlr_image()
726 cam_dbg(cam, "camera: bytesperline = %d; height = %d\n", in mcam_ctlr_image()
748 mcam_reg_write_mask(cam, REG_IMGPITCH, widthuv << 16 | widthy, in mcam_ctlr_image()
750 mcam_reg_write(cam, REG_IMGSIZE, imgsz_h | imgsz_w); in mcam_ctlr_image()
751 mcam_reg_write(cam, REG_IMGOFFSET, 0x0); in mcam_ctlr_image()
759 mcam_reg_write_mask(cam, REG_CTRL0, in mcam_ctlr_image()
763 mcam_reg_write_mask(cam, REG_CTRL0, in mcam_ctlr_image()
767 mcam_reg_write_mask(cam, REG_CTRL0, in mcam_ctlr_image()
771 mcam_reg_write_mask(cam, REG_CTRL0, in mcam_ctlr_image()
775 mcam_reg_write_mask(cam, REG_CTRL0, in mcam_ctlr_image()
779 mcam_reg_write_mask(cam, REG_CTRL0, in mcam_ctlr_image()
783 cam_err(cam, "camera: unknown format: %#x\n", fmt->pixelformat); in mcam_ctlr_image()
790 mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC, C0_SIFM_MASK); in mcam_ctlr_image()
798 static int mcam_ctlr_configure(struct mcam_camera *cam) in mcam_ctlr_configure() argument
802 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_ctlr_configure()
803 clear_bit(CF_SG_RESTART, &cam->flags); in mcam_ctlr_configure()
804 cam->dma_setup(cam); in mcam_ctlr_configure()
805 mcam_ctlr_image(cam); in mcam_ctlr_configure()
806 mcam_set_config_needed(cam, 0); in mcam_ctlr_configure()
807 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_ctlr_configure()
811 static void mcam_ctlr_irq_enable(struct mcam_camera *cam) in mcam_ctlr_irq_enable() argument
817 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); in mcam_ctlr_irq_enable()
818 mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS); in mcam_ctlr_irq_enable()
821 static void mcam_ctlr_irq_disable(struct mcam_camera *cam) in mcam_ctlr_irq_disable() argument
823 mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS); in mcam_ctlr_irq_disable()
830 static void mcam_ctlr_stop_dma(struct mcam_camera *cam) in mcam_ctlr_stop_dma() argument
839 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_ctlr_stop_dma()
840 clear_bit(CF_SG_RESTART, &cam->flags); in mcam_ctlr_stop_dma()
841 mcam_ctlr_stop(cam); in mcam_ctlr_stop_dma()
842 cam->state = S_IDLE; in mcam_ctlr_stop_dma()
843 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_ctlr_stop_dma()
852 if (test_bit(CF_DMA_ACTIVE, &cam->flags)) in mcam_ctlr_stop_dma()
853 cam_err(cam, "Timeout waiting for DMA to end\n"); in mcam_ctlr_stop_dma()
855 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_ctlr_stop_dma()
856 mcam_ctlr_irq_disable(cam); in mcam_ctlr_stop_dma()
857 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_ctlr_stop_dma()
863 static int mcam_ctlr_power_up(struct mcam_camera *cam) in mcam_ctlr_power_up() argument
868 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_ctlr_power_up()
869 if (cam->plat_power_up) { in mcam_ctlr_power_up()
870 ret = cam->plat_power_up(cam); in mcam_ctlr_power_up()
872 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_ctlr_power_up()
876 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN); in mcam_ctlr_power_up()
877 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_ctlr_power_up()
881 static void mcam_ctlr_power_down(struct mcam_camera *cam) in mcam_ctlr_power_down() argument
885 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_ctlr_power_down()
891 mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN); in mcam_ctlr_power_down()
892 if (cam->plat_power_down) in mcam_ctlr_power_down()
893 cam->plat_power_down(cam); in mcam_ctlr_power_down()
894 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_ctlr_power_down()
903 struct mcam_camera *cam = container_of(hw, struct mcam_camera, mclk_hw); in mclk_prepare() local
905 clk_prepare(cam->clk[0]); in mclk_prepare()
911 struct mcam_camera *cam = container_of(hw, struct mcam_camera, mclk_hw); in mclk_unprepare() local
913 clk_unprepare(cam->clk[0]); in mclk_unprepare()
918 struct mcam_camera *cam = container_of(hw, struct mcam_camera, mclk_hw); in mclk_enable() local
927 if (cam->bus_type == V4L2_MBUS_CSI2_DPHY) { in mclk_enable()
928 mclk_src = cam->mclk_src; in mclk_enable()
929 mclk_div = cam->mclk_div; in mclk_enable()
935 ret = pm_runtime_resume_and_get(cam->dev); in mclk_enable()
938 clk_enable(cam->clk[0]); in mclk_enable()
939 mcam_reg_write(cam, REG_CLKCTRL, (mclk_src << 29) | mclk_div); in mclk_enable()
940 mcam_ctlr_power_up(cam); in mclk_enable()
947 struct mcam_camera *cam = container_of(hw, struct mcam_camera, mclk_hw); in mclk_disable() local
949 mcam_ctlr_power_down(cam); in mclk_disable()
950 clk_disable(cam->clk[0]); in mclk_disable()
951 pm_runtime_put(cam->dev); in mclk_disable()
973 static int __mcam_cam_reset(struct mcam_camera *cam) in __mcam_cam_reset() argument
975 return sensor_call(cam, core, reset, 0); in __mcam_cam_reset()
982 static int mcam_cam_init(struct mcam_camera *cam) in mcam_cam_init() argument
986 if (cam->state != S_NOTREADY) in mcam_cam_init()
987 cam_warn(cam, "Cam init with device in funky state %d", in mcam_cam_init()
988 cam->state); in mcam_cam_init()
989 ret = __mcam_cam_reset(cam); in mcam_cam_init()
991 cam->state = S_IDLE; in mcam_cam_init()
999 static int mcam_cam_set_flip(struct mcam_camera *cam) in mcam_cam_set_flip() argument
1006 return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl); in mcam_cam_set_flip()
1010 static int mcam_cam_configure(struct mcam_camera *cam) in mcam_cam_configure() argument
1017 v4l2_fill_mbus_format(&format.format, &cam->pix_format, cam->mbus_code); in mcam_cam_configure()
1018 ret = sensor_call(cam, core, init, 0); in mcam_cam_configure()
1020 ret = sensor_call(cam, pad, set_fmt, NULL, &format); in mcam_cam_configure()
1024 ret += mcam_cam_set_flip(cam); in mcam_cam_configure()
1031 static int mcam_read_setup(struct mcam_camera *cam) in mcam_read_setup() argument
1040 if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 && in mcam_read_setup()
1041 mcam_alloc_dma_bufs(cam, 0)) in mcam_read_setup()
1044 if (mcam_needs_config(cam)) { in mcam_read_setup()
1045 mcam_cam_configure(cam); in mcam_read_setup()
1046 ret = mcam_ctlr_configure(cam); in mcam_read_setup()
1054 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_read_setup()
1055 clear_bit(CF_DMA_ACTIVE, &cam->flags); in mcam_read_setup()
1056 mcam_reset_buffers(cam); in mcam_read_setup()
1057 if (cam->bus_type == V4L2_MBUS_CSI2_DPHY) in mcam_read_setup()
1058 mcam_enable_mipi(cam); in mcam_read_setup()
1060 mcam_disable_mipi(cam); in mcam_read_setup()
1061 mcam_ctlr_irq_enable(cam); in mcam_read_setup()
1062 cam->state = S_STREAMING; in mcam_read_setup()
1063 if (!test_bit(CF_SG_RESTART, &cam->flags)) in mcam_read_setup()
1064 mcam_ctlr_start(cam); in mcam_read_setup()
1065 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_read_setup()
1079 struct mcam_camera *cam = vb2_get_drv_priv(vq); in mcam_vb_queue_setup() local
1080 int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2; in mcam_vb_queue_setup()
1081 unsigned size = cam->pix_format.sizeimage; in mcam_vb_queue_setup()
1098 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue); in mcam_vb_buf_queue() local
1102 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_vb_buf_queue()
1103 start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers); in mcam_vb_buf_queue()
1104 list_add(&mvb->queue, &cam->buffers); in mcam_vb_buf_queue()
1105 if (cam->state == S_STREAMING && test_bit(CF_SG_RESTART, &cam->flags)) in mcam_vb_buf_queue()
1106 mcam_sg_restart(cam); in mcam_vb_buf_queue()
1107 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_vb_buf_queue()
1109 mcam_read_setup(cam); in mcam_vb_buf_queue()
1115 struct mcam_camera *cam = vb2_get_drv_priv(vq); in mcam_vb_requeue_bufs() local
1120 spin_lock_irqsave(&cam->dev_lock, flags); in mcam_vb_requeue_bufs()
1121 list_for_each_entry_safe(buf, node, &cam->buffers, queue) { in mcam_vb_requeue_bufs()
1126 buf = cam->vb_bufs[i]; in mcam_vb_requeue_bufs()
1130 cam->vb_bufs[i] = NULL; in mcam_vb_requeue_bufs()
1133 spin_unlock_irqrestore(&cam->dev_lock, flags); in mcam_vb_requeue_bufs()
1141 struct mcam_camera *cam = vb2_get_drv_priv(vq); in mcam_vb_start_streaming() local
1145 if (cam->state != S_IDLE) { in mcam_vb_start_streaming()
1149 cam->frame_state.frames = 0; in mcam_vb_start_streaming()
1150 cam->frame_state.singles = 0; in mcam_vb_start_streaming()
1151 cam->frame_state.delivered = 0; in mcam_vb_start_streaming()
1152 cam->sequence = 0; in mcam_vb_start_streaming()
1160 if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) { in mcam_vb_start_streaming()
1161 cam->state = S_BUFWAIT; in mcam_vb_start_streaming()
1169 for (frame = 0; frame < cam->nbufs; frame++) in mcam_vb_start_streaming()
1170 clear_bit(CF_FRAME_SOF0 + frame, &cam->flags); in mcam_vb_start_streaming()
1172 ret = mcam_read_setup(cam); in mcam_vb_start_streaming()
1180 struct mcam_camera *cam = vb2_get_drv_priv(vq); in mcam_vb_stop_streaming() local
1182 cam_dbg(cam, "stop_streaming: %d frames, %d singles, %d delivered\n", in mcam_vb_stop_streaming()
1183 cam->frame_state.frames, cam->frame_state.singles, in mcam_vb_stop_streaming()
1184 cam->frame_state.delivered); in mcam_vb_stop_streaming()
1185 if (cam->state == S_BUFWAIT) { in mcam_vb_stop_streaming()
1187 cam->state = S_IDLE; in mcam_vb_stop_streaming()
1190 if (cam->state != S_STREAMING) in mcam_vb_stop_streaming()
1192 mcam_ctlr_stop_dma(cam); in mcam_vb_stop_streaming()
1220 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue); in mcam_vb_sg_buf_init() local
1221 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1; in mcam_vb_sg_buf_init()
1223 mvb->dma_desc = dma_alloc_coherent(cam->dev, in mcam_vb_sg_buf_init()
1227 cam_err(cam, "Unable to get DMA descriptor array\n"); in mcam_vb_sg_buf_init()
1253 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue); in mcam_vb_sg_buf_cleanup() local
1255 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1; in mcam_vb_sg_buf_cleanup()
1257 dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc), in mcam_vb_sg_buf_cleanup()
1276 static int mcam_setup_vb2(struct mcam_camera *cam) in mcam_setup_vb2() argument
1278 struct vb2_queue *vq = &cam->vb_queue; in mcam_setup_vb2()
1282 vq->drv_priv = cam; in mcam_setup_vb2()
1283 vq->lock = &cam->s_mutex; in mcam_setup_vb2()
1287 vq->dev = cam->dev; in mcam_setup_vb2()
1288 INIT_LIST_HEAD(&cam->buffers); in mcam_setup_vb2()
1289 switch (cam->buffer_mode) { in mcam_setup_vb2()
1294 cam->dma_setup = mcam_ctlr_dma_contig; in mcam_setup_vb2()
1295 cam->frame_complete = mcam_dma_contig_done; in mcam_setup_vb2()
1302 cam->dma_setup = mcam_ctlr_dma_sg; in mcam_setup_vb2()
1303 cam->frame_complete = mcam_dma_sg_done; in mcam_setup_vb2()
1308 INIT_WORK(&cam->s_bh_work, mcam_frame_work); in mcam_setup_vb2()
1311 cam->dma_setup = mcam_ctlr_dma_vmalloc; in mcam_setup_vb2()
1312 cam->frame_complete = mcam_vmalloc_done; in mcam_setup_vb2()
1328 struct mcam_camera *cam = video_drvdata(file); in mcam_vidioc_querycap() local
1332 strscpy(cap->bus_info, cam->bus_info, sizeof(cap->bus_info)); in mcam_vidioc_querycap()
1349 struct mcam_camera *cam = video_drvdata(filp); in mcam_vidioc_try_fmt_vid_cap() local
1364 ret = sensor_call(cam, pad, set_fmt, &pad_state, &format); in mcam_vidioc_try_fmt_vid_cap()
1383 struct mcam_camera *cam = video_drvdata(filp); in mcam_vidioc_s_fmt_vid_cap() local
1391 if (cam->state != S_IDLE || vb2_is_busy(&cam->vb_queue)) in mcam_vidioc_s_fmt_vid_cap()
1406 cam->pix_format = fmt->fmt.pix; in mcam_vidioc_s_fmt_vid_cap()
1407 cam->mbus_code = f->mbus_code; in mcam_vidioc_s_fmt_vid_cap()
1412 if (cam->buffer_mode == B_vmalloc) { in mcam_vidioc_s_fmt_vid_cap()
1413 ret = mcam_check_dma_buffers(cam); in mcam_vidioc_s_fmt_vid_cap()
1417 mcam_set_config_needed(cam, 1); in mcam_vidioc_s_fmt_vid_cap()
1430 struct mcam_camera *cam = video_drvdata(filp); in mcam_vidioc_g_fmt_vid_cap() local
1432 f->fmt.pix = cam->pix_format; in mcam_vidioc_g_fmt_vid_cap()
1470 struct mcam_camera *cam = video_drvdata(filp); in mcam_vidioc_g_parm() local
1473 ret = v4l2_g_parm_cap(video_devdata(filp), cam->sensor, a); in mcam_vidioc_g_parm()
1481 struct mcam_camera *cam = video_drvdata(filp); in mcam_vidioc_s_parm() local
1484 ret = v4l2_s_parm_cap(video_devdata(filp), cam->sensor, a); in mcam_vidioc_s_parm()
1492 struct mcam_camera *cam = video_drvdata(filp); in mcam_vidioc_enum_framesizes() local
1504 ret = sensor_call(cam, pad, enum_frame_size, NULL, &fse); in mcam_vidioc_enum_framesizes()
1527 struct mcam_camera *cam = video_drvdata(filp); in mcam_vidioc_enum_frameintervals() local
1541 ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie); in mcam_vidioc_enum_frameintervals()
1553 struct mcam_camera *cam = video_drvdata(file); in mcam_vidioc_g_register() local
1555 if (reg->reg > cam->regs_size - 4) in mcam_vidioc_g_register()
1557 reg->val = mcam_reg_read(cam, reg->reg); in mcam_vidioc_g_register()
1565 struct mcam_camera *cam = video_drvdata(file); in mcam_vidioc_s_register() local
1567 if (reg->reg > cam->regs_size - 4) in mcam_vidioc_s_register()
1569 mcam_reg_write(cam, reg->reg, reg->val); in mcam_vidioc_s_register()
1609 struct mcam_camera *cam = video_drvdata(filp); in mcam_v4l_open() local
1612 mutex_lock(&cam->s_mutex); in mcam_v4l_open()
1617 ret = sensor_call(cam, core, s_power, 1); in mcam_v4l_open()
1620 ret = pm_runtime_resume_and_get(cam->dev); in mcam_v4l_open()
1623 __mcam_cam_reset(cam); in mcam_v4l_open()
1624 mcam_set_config_needed(cam, 1); in mcam_v4l_open()
1627 mutex_unlock(&cam->s_mutex); in mcam_v4l_open()
1636 struct mcam_camera *cam = video_drvdata(filp); in mcam_v4l_release() local
1639 mutex_lock(&cam->s_mutex); in mcam_v4l_release()
1643 mcam_disable_mipi(cam); in mcam_v4l_release()
1644 sensor_call(cam, core, s_power, 0); in mcam_v4l_release()
1645 pm_runtime_put(cam->dev); in mcam_v4l_release()
1646 if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read) in mcam_v4l_release()
1647 mcam_free_dma_bufs(cam); in mcam_v4l_release()
1650 mutex_unlock(&cam->s_mutex); in mcam_v4l_release()
1682 static void mcam_frame_complete(struct mcam_camera *cam, int frame) in mcam_frame_complete() argument
1687 set_bit(frame, &cam->flags); in mcam_frame_complete()
1688 clear_bit(CF_DMA_ACTIVE, &cam->flags); in mcam_frame_complete()
1689 cam->next_buf = frame; in mcam_frame_complete()
1690 cam->buf_seq[frame] = cam->sequence++; in mcam_frame_complete()
1691 cam->frame_state.frames++; in mcam_frame_complete()
1695 if (cam->state != S_STREAMING) in mcam_frame_complete()
1700 cam->frame_complete(cam, frame); in mcam_frame_complete()
1708 int mccic_irq(struct mcam_camera *cam, unsigned int irqs) in mccic_irq() argument
1712 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */ in mccic_irq()
1723 for (frame = 0; frame < cam->nbufs; frame++) in mccic_irq()
1725 test_bit(CF_FRAME_SOF0 + frame, &cam->flags)) { in mccic_irq()
1726 mcam_frame_complete(cam, frame); in mccic_irq()
1728 clear_bit(CF_FRAME_SOF0 + frame, &cam->flags); in mccic_irq()
1729 if (cam->buffer_mode == B_DMA_sg) in mccic_irq()
1737 for (frame = 0; frame < cam->nbufs; frame++) { in mccic_irq()
1739 set_bit(CF_FRAME_SOF0 + frame, &cam->flags); in mccic_irq()
1745 set_bit(CF_DMA_ACTIVE, &cam->flags); in mccic_irq()
1746 if (cam->buffer_mode == B_DMA_sg) in mccic_irq()
1747 mcam_ctlr_stop(cam); in mccic_irq()
1761 struct mcam_camera *cam = notifier_to_mcam(notifier); in mccic_notify_bound() local
1764 mutex_lock(&cam->s_mutex); in mccic_notify_bound()
1765 if (cam->sensor) { in mccic_notify_bound()
1766 cam_err(cam, "sensor already bound\n"); in mccic_notify_bound()
1771 v4l2_set_subdev_hostdata(subdev, cam); in mccic_notify_bound()
1772 cam->sensor = subdev; in mccic_notify_bound()
1774 ret = mcam_cam_init(cam); in mccic_notify_bound()
1776 cam->sensor = NULL; in mccic_notify_bound()
1780 ret = mcam_setup_vb2(cam); in mccic_notify_bound()
1782 cam->sensor = NULL; in mccic_notify_bound()
1786 cam->vdev = mcam_v4l_template; in mccic_notify_bound()
1787 cam->vdev.v4l2_dev = &cam->v4l2_dev; in mccic_notify_bound()
1788 cam->vdev.lock = &cam->s_mutex; in mccic_notify_bound()
1789 cam->vdev.queue = &cam->vb_queue; in mccic_notify_bound()
1790 video_set_drvdata(&cam->vdev, cam); in mccic_notify_bound()
1791 ret = video_register_device(&cam->vdev, VFL_TYPE_VIDEO, -1); in mccic_notify_bound()
1793 cam->sensor = NULL; in mccic_notify_bound()
1797 cam_dbg(cam, "sensor %s bound\n", subdev->name); in mccic_notify_bound()
1799 mutex_unlock(&cam->s_mutex); in mccic_notify_bound()
1806 struct mcam_camera *cam = notifier_to_mcam(notifier); in mccic_notify_unbind() local
1808 mutex_lock(&cam->s_mutex); in mccic_notify_unbind()
1809 if (cam->sensor != subdev) { in mccic_notify_unbind()
1810 cam_err(cam, "sensor %s not bound\n", subdev->name); in mccic_notify_unbind()
1814 video_unregister_device(&cam->vdev); in mccic_notify_unbind()
1815 cam->sensor = NULL; in mccic_notify_unbind()
1816 cam_dbg(cam, "sensor %s unbound\n", subdev->name); in mccic_notify_unbind()
1819 mutex_unlock(&cam->s_mutex); in mccic_notify_unbind()
1824 struct mcam_camera *cam = notifier_to_mcam(notifier); in mccic_notify_complete() local
1830 ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10); in mccic_notify_complete()
1832 cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler; in mccic_notify_complete()
1843 int mccic_register(struct mcam_camera *cam) in mccic_register() argument
1852 cam->buffer_mode = buffer_mode; in mccic_register()
1853 if (cam->buffer_mode == B_DMA_sg && in mccic_register()
1854 cam->chip_id == MCAM_CAFE) { in mccic_register()
1855 printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, attempting vmalloc mode instead\n"); in mccic_register()
1856 cam->buffer_mode = B_vmalloc; in mccic_register()
1859 if (!mcam_buffer_mode_supported(cam->buffer_mode)) { in mccic_register()
1860 printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n", in mccic_register()
1861 cam->buffer_mode); in mccic_register()
1866 mutex_init(&cam->s_mutex); in mccic_register()
1867 cam->state = S_NOTREADY; in mccic_register()
1868 mcam_set_config_needed(cam, 1); in mccic_register()
1869 cam->pix_format = mcam_def_pix_format; in mccic_register()
1870 cam->mbus_code = mcam_def_mbus_code; in mccic_register()
1872 cam->notifier.ops = &mccic_notify_ops; in mccic_register()
1873 ret = v4l2_async_nf_register(&cam->notifier); in mccic_register()
1875 cam_warn(cam, "failed to register a sensor notifier"); in mccic_register()
1887 of_property_read_string(cam->dev->of_node, "clock-output-names", in mccic_register()
1890 cam->mclk_hw.init = &mclk_init; in mccic_register()
1892 cam->mclk = devm_clk_register(cam->dev, &cam->mclk_hw); in mccic_register()
1893 if (IS_ERR(cam->mclk)) { in mccic_register()
1894 ret = PTR_ERR(cam->mclk); in mccic_register()
1895 dev_err(cam->dev, "can't register clock\n"); in mccic_register()
1902 if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) { in mccic_register()
1903 if (mcam_alloc_dma_bufs(cam, 1)) in mccic_register()
1904 cam_warn(cam, "Unable to alloc DMA buffers at load will try again later."); in mccic_register()
1910 v4l2_async_nf_unregister(&cam->notifier); in mccic_register()
1911 v4l2_async_nf_cleanup(&cam->notifier); in mccic_register()
1916 void mccic_shutdown(struct mcam_camera *cam) in mccic_shutdown() argument
1924 if (!list_empty(&cam->vdev.fh_list)) { in mccic_shutdown()
1925 cam_warn(cam, "Removing a device with users!\n"); in mccic_shutdown()
1926 sensor_call(cam, core, s_power, 0); in mccic_shutdown()
1928 if (cam->buffer_mode == B_vmalloc) in mccic_shutdown()
1929 mcam_free_dma_bufs(cam); in mccic_shutdown()
1930 v4l2_ctrl_handler_free(&cam->ctrl_handler); in mccic_shutdown()
1931 v4l2_async_nf_unregister(&cam->notifier); in mccic_shutdown()
1932 v4l2_async_nf_cleanup(&cam->notifier); in mccic_shutdown()
1939 void mccic_suspend(struct mcam_camera *cam) in mccic_suspend() argument
1941 mutex_lock(&cam->s_mutex); in mccic_suspend()
1942 if (!list_empty(&cam->vdev.fh_list)) { in mccic_suspend()
1943 enum mcam_state cstate = cam->state; in mccic_suspend()
1945 mcam_ctlr_stop_dma(cam); in mccic_suspend()
1946 sensor_call(cam, core, s_power, 0); in mccic_suspend()
1947 cam->state = cstate; in mccic_suspend()
1949 mutex_unlock(&cam->s_mutex); in mccic_suspend()
1953 int mccic_resume(struct mcam_camera *cam) in mccic_resume() argument
1957 mutex_lock(&cam->s_mutex); in mccic_resume()
1958 if (!list_empty(&cam->vdev.fh_list)) { in mccic_resume()
1959 ret = sensor_call(cam, core, s_power, 1); in mccic_resume()
1961 mutex_unlock(&cam->s_mutex); in mccic_resume()
1964 __mcam_cam_reset(cam); in mccic_resume()
1966 sensor_call(cam, core, s_power, 0); in mccic_resume()
1968 mutex_unlock(&cam->s_mutex); in mccic_resume()
1970 set_bit(CF_CONFIG_NEEDED, &cam->flags); in mccic_resume()
1971 if (cam->state == S_STREAMING) { in mccic_resume()
1976 if (cam->buffer_mode == B_DMA_sg && cam->vb_bufs[0]) in mccic_resume()
1977 list_add(&cam->vb_bufs[0]->queue, &cam->buffers); in mccic_resume()
1978 ret = mcam_read_setup(cam); in mccic_resume()