2 * Samsung S5P/EXYNOS4 SoC series camera interface (video postprocessor) driver
4 * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
5 * Contact: Sylwester Nawrocki, <s.nawrocki@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 2 of the License,
10 * or (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/bug.h>
18 #include <linux/interrupt.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/clk.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
30 #include "fimc-core.h"
31 #include "fimc-mdevice.h"
33 static char *fimc_clocks[MAX_FIMC_CLOCKS] = {
37 static struct fimc_fmt fimc_formats[] = {
40 .fourcc = V4L2_PIX_FMT_RGB565,
42 .color = S5P_FIMC_RGB565,
45 .flags = FMT_FLAGS_M2M,
48 .fourcc = V4L2_PIX_FMT_BGR666,
50 .color = S5P_FIMC_RGB666,
53 .flags = FMT_FLAGS_M2M,
55 .name = "ARGB8888, 32 bpp",
56 .fourcc = V4L2_PIX_FMT_RGB32,
58 .color = S5P_FIMC_RGB888,
61 .flags = FMT_FLAGS_M2M | FMT_HAS_ALPHA,
64 .fourcc = V4L2_PIX_FMT_RGB555,
66 .color = S5P_FIMC_RGB555,
69 .flags = FMT_FLAGS_M2M_OUT | FMT_HAS_ALPHA,
72 .fourcc = V4L2_PIX_FMT_RGB444,
74 .color = S5P_FIMC_RGB444,
77 .flags = FMT_FLAGS_M2M_OUT | FMT_HAS_ALPHA,
79 .name = "YUV 4:2:2 packed, YCbYCr",
80 .fourcc = V4L2_PIX_FMT_YUYV,
82 .color = S5P_FIMC_YCBYCR422,
85 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
86 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
88 .name = "YUV 4:2:2 packed, CbYCrY",
89 .fourcc = V4L2_PIX_FMT_UYVY,
91 .color = S5P_FIMC_CBYCRY422,
94 .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
95 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
97 .name = "YUV 4:2:2 packed, CrYCbY",
98 .fourcc = V4L2_PIX_FMT_VYUY,
100 .color = S5P_FIMC_CRYCBY422,
103 .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
104 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
106 .name = "YUV 4:2:2 packed, YCrYCb",
107 .fourcc = V4L2_PIX_FMT_YVYU,
109 .color = S5P_FIMC_YCRYCB422,
112 .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
113 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
115 .name = "YUV 4:2:2 planar, Y/Cb/Cr",
116 .fourcc = V4L2_PIX_FMT_YUV422P,
118 .color = S5P_FIMC_YCBYCR422,
121 .flags = FMT_FLAGS_M2M,
123 .name = "YUV 4:2:2 planar, Y/CbCr",
124 .fourcc = V4L2_PIX_FMT_NV16,
126 .color = S5P_FIMC_YCBYCR422,
129 .flags = FMT_FLAGS_M2M,
131 .name = "YUV 4:2:2 planar, Y/CrCb",
132 .fourcc = V4L2_PIX_FMT_NV61,
134 .color = S5P_FIMC_YCRYCB422,
137 .flags = FMT_FLAGS_M2M,
139 .name = "YUV 4:2:0 planar, YCbCr",
140 .fourcc = V4L2_PIX_FMT_YUV420,
142 .color = S5P_FIMC_YCBCR420,
145 .flags = FMT_FLAGS_M2M,
147 .name = "YUV 4:2:0 planar, Y/CbCr",
148 .fourcc = V4L2_PIX_FMT_NV12,
150 .color = S5P_FIMC_YCBCR420,
153 .flags = FMT_FLAGS_M2M,
155 .name = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr",
156 .fourcc = V4L2_PIX_FMT_NV12M,
157 .color = S5P_FIMC_YCBCR420,
161 .flags = FMT_FLAGS_M2M,
163 .name = "YUV 4:2:0 non-contiguous 3-planar, Y/Cb/Cr",
164 .fourcc = V4L2_PIX_FMT_YUV420M,
165 .color = S5P_FIMC_YCBCR420,
166 .depth = { 8, 2, 2 },
169 .flags = FMT_FLAGS_M2M,
171 .name = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr, tiled",
172 .fourcc = V4L2_PIX_FMT_NV12MT,
173 .color = S5P_FIMC_YCBCR420,
177 .flags = FMT_FLAGS_M2M,
179 .name = "JPEG encoded data",
180 .fourcc = V4L2_PIX_FMT_JPEG,
181 .color = S5P_FIMC_JPEG,
185 .mbus_code = V4L2_MBUS_FMT_JPEG_1X8,
186 .flags = FMT_FLAGS_CAM,
190 static unsigned int get_m2m_fmt_flags(unsigned int stream_type)
192 if (stream_type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
193 return FMT_FLAGS_M2M_IN;
195 return FMT_FLAGS_M2M_OUT;
198 int fimc_check_scaler_ratio(struct fimc_ctx *ctx, int sw, int sh,
199 int dw, int dh, int rotation)
201 if (rotation == 90 || rotation == 270)
204 if (!ctx->scaler.enabled)
205 return (sw == dw && sh == dh) ? 0 : -EINVAL;
207 if ((sw >= SCALER_MAX_HRATIO * dw) || (sh >= SCALER_MAX_VRATIO * dh))
213 static int fimc_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
222 if (src >= tar * tmp) {
223 *shift = sh, *ratio = tmp;
227 *shift = 0, *ratio = 1;
231 int fimc_set_scaler_info(struct fimc_ctx *ctx)
233 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
234 struct device *dev = &ctx->fimc_dev->pdev->dev;
235 struct fimc_scaler *sc = &ctx->scaler;
236 struct fimc_frame *s_frame = &ctx->s_frame;
237 struct fimc_frame *d_frame = &ctx->d_frame;
241 if (ctx->rotation == 90 || ctx->rotation == 270) {
243 tx = d_frame->height;
246 ty = d_frame->height;
248 if (tx <= 0 || ty <= 0) {
249 dev_err(dev, "Invalid target size: %dx%d", tx, ty);
254 sy = s_frame->height;
255 if (sx <= 0 || sy <= 0) {
256 dev_err(dev, "Invalid source size: %dx%d", sx, sy);
260 sc->real_height = sy;
262 ret = fimc_get_scaler_factor(sx, tx, &sc->pre_hratio, &sc->hfactor);
266 ret = fimc_get_scaler_factor(sy, ty, &sc->pre_vratio, &sc->vfactor);
270 sc->pre_dst_width = sx / sc->pre_hratio;
271 sc->pre_dst_height = sy / sc->pre_vratio;
273 if (variant->has_mainscaler_ext) {
274 sc->main_hratio = (sx << 14) / (tx << sc->hfactor);
275 sc->main_vratio = (sy << 14) / (ty << sc->vfactor);
277 sc->main_hratio = (sx << 8) / (tx << sc->hfactor);
278 sc->main_vratio = (sy << 8) / (ty << sc->vfactor);
282 sc->scaleup_h = (tx >= sx) ? 1 : 0;
283 sc->scaleup_v = (ty >= sy) ? 1 : 0;
285 /* check to see if input and output size/format differ */
286 if (s_frame->fmt->color == d_frame->fmt->color
287 && s_frame->width == d_frame->width
288 && s_frame->height == d_frame->height)
296 static void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state)
298 struct vb2_buffer *src_vb, *dst_vb;
300 if (!ctx || !ctx->m2m_ctx)
303 src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
304 dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
306 if (src_vb && dst_vb) {
307 v4l2_m2m_buf_done(src_vb, vb_state);
308 v4l2_m2m_buf_done(dst_vb, vb_state);
309 v4l2_m2m_job_finish(ctx->fimc_dev->m2m.m2m_dev,
314 /* Complete the transaction which has been scheduled for execution. */
315 static int fimc_m2m_shutdown(struct fimc_ctx *ctx)
317 struct fimc_dev *fimc = ctx->fimc_dev;
320 if (!fimc_m2m_pending(fimc))
323 fimc_ctx_state_lock_set(FIMC_CTX_SHUT, ctx);
325 ret = wait_event_timeout(fimc->irq_queue,
326 !fimc_ctx_state_is_set(FIMC_CTX_SHUT, ctx),
327 FIMC_SHUTDOWN_TIMEOUT);
329 return ret == 0 ? -ETIMEDOUT : ret;
332 static int start_streaming(struct vb2_queue *q, unsigned int count)
334 struct fimc_ctx *ctx = q->drv_priv;
337 ret = pm_runtime_get_sync(&ctx->fimc_dev->pdev->dev);
338 return ret > 0 ? 0 : ret;
341 static int stop_streaming(struct vb2_queue *q)
343 struct fimc_ctx *ctx = q->drv_priv;
346 ret = fimc_m2m_shutdown(ctx);
347 if (ret == -ETIMEDOUT)
348 fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
350 pm_runtime_put(&ctx->fimc_dev->pdev->dev);
354 void fimc_capture_irq_handler(struct fimc_dev *fimc, bool final)
356 struct fimc_vid_cap *cap = &fimc->vid_cap;
357 struct fimc_vid_buffer *v_buf;
361 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
362 wake_up(&fimc->irq_queue);
366 if (!list_empty(&cap->active_buf_q) &&
367 test_bit(ST_CAPT_RUN, &fimc->state) && final) {
368 ktime_get_real_ts(&ts);
370 v_buf = fimc_active_queue_pop(cap);
372 tv = &v_buf->vb.v4l2_buf.timestamp;
373 tv->tv_sec = ts.tv_sec;
374 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
375 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
377 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
380 if (!list_empty(&cap->pending_buf_q)) {
382 v_buf = fimc_pending_queue_pop(cap);
383 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
384 v_buf->index = cap->buf_index;
386 /* Move the buffer to the capture active queue */
387 fimc_active_queue_add(cap, v_buf);
389 dbg("next frame: %d, done frame: %d",
390 fimc_hw_get_frame_index(fimc), v_buf->index);
392 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
396 if (cap->active_buf_cnt == 0) {
398 clear_bit(ST_CAPT_RUN, &fimc->state);
400 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
403 set_bit(ST_CAPT_RUN, &fimc->state);
406 fimc_capture_config_update(cap->ctx);
408 dbg("frame: %d, active_buf_cnt: %d",
409 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
412 static irqreturn_t fimc_irq_handler(int irq, void *priv)
414 struct fimc_dev *fimc = priv;
415 struct fimc_vid_cap *cap = &fimc->vid_cap;
416 struct fimc_ctx *ctx;
418 fimc_hw_clear_irq(fimc);
420 spin_lock(&fimc->slock);
422 if (test_and_clear_bit(ST_M2M_PEND, &fimc->state)) {
423 if (test_and_clear_bit(ST_M2M_SUSPENDING, &fimc->state)) {
424 set_bit(ST_M2M_SUSPENDED, &fimc->state);
425 wake_up(&fimc->irq_queue);
428 ctx = v4l2_m2m_get_curr_priv(fimc->m2m.m2m_dev);
430 spin_unlock(&fimc->slock);
431 fimc_m2m_job_finish(ctx, VB2_BUF_STATE_DONE);
433 spin_lock(&ctx->slock);
434 if (ctx->state & FIMC_CTX_SHUT) {
435 ctx->state &= ~FIMC_CTX_SHUT;
436 wake_up(&fimc->irq_queue);
438 spin_unlock(&ctx->slock);
441 } else if (test_bit(ST_CAPT_PEND, &fimc->state)) {
442 fimc_capture_irq_handler(fimc,
443 !test_bit(ST_CAPT_JPEG, &fimc->state));
444 if (cap->active_buf_cnt == 1) {
445 fimc_deactivate_capture(fimc);
446 clear_bit(ST_CAPT_STREAM, &fimc->state);
450 spin_unlock(&fimc->slock);
454 /* The color format (colplanes, memplanes) must be already configured. */
455 int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
456 struct fimc_frame *frame, struct fimc_addr *paddr)
461 if (vb == NULL || frame == NULL)
464 pix_size = frame->width * frame->height;
466 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
467 frame->fmt->memplanes, frame->fmt->colplanes, pix_size);
469 paddr->y = vb2_dma_contig_plane_dma_addr(vb, 0);
471 if (frame->fmt->memplanes == 1) {
472 switch (frame->fmt->colplanes) {
478 /* decompose Y into Y/Cb */
479 paddr->cb = (u32)(paddr->y + pix_size);
483 paddr->cb = (u32)(paddr->y + pix_size);
484 /* decompose Y into Y/Cb/Cr */
485 if (S5P_FIMC_YCBCR420 == frame->fmt->color)
486 paddr->cr = (u32)(paddr->cb
489 paddr->cr = (u32)(paddr->cb
496 if (frame->fmt->memplanes >= 2)
497 paddr->cb = vb2_dma_contig_plane_dma_addr(vb, 1);
499 if (frame->fmt->memplanes == 3)
500 paddr->cr = vb2_dma_contig_plane_dma_addr(vb, 2);
503 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
504 paddr->y, paddr->cb, paddr->cr, ret);
509 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
510 void fimc_set_yuv_order(struct fimc_ctx *ctx)
512 /* The one only mode supported in SoC. */
513 ctx->in_order_2p = S5P_FIMC_LSB_CRCB;
514 ctx->out_order_2p = S5P_FIMC_LSB_CRCB;
516 /* Set order for 1 plane input formats. */
517 switch (ctx->s_frame.fmt->color) {
518 case S5P_FIMC_YCRYCB422:
519 ctx->in_order_1p = S5P_MSCTRL_ORDER422_CBYCRY;
521 case S5P_FIMC_CBYCRY422:
522 ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCRYCB;
524 case S5P_FIMC_CRYCBY422:
525 ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCBYCR;
527 case S5P_FIMC_YCBYCR422:
529 ctx->in_order_1p = S5P_MSCTRL_ORDER422_CRYCBY;
532 dbg("ctx->in_order_1p= %d", ctx->in_order_1p);
534 switch (ctx->d_frame.fmt->color) {
535 case S5P_FIMC_YCRYCB422:
536 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CBYCRY;
538 case S5P_FIMC_CBYCRY422:
539 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCRYCB;
541 case S5P_FIMC_CRYCBY422:
542 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCBYCR;
544 case S5P_FIMC_YCBYCR422:
546 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CRYCBY;
549 dbg("ctx->out_order_1p= %d", ctx->out_order_1p);
552 void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f)
554 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
557 for (i = 0; i < f->fmt->colplanes; i++)
558 depth += f->fmt->depth[i];
560 f->dma_offset.y_h = f->offs_h;
561 if (!variant->pix_hoff)
562 f->dma_offset.y_h *= (depth >> 3);
564 f->dma_offset.y_v = f->offs_v;
566 f->dma_offset.cb_h = f->offs_h;
567 f->dma_offset.cb_v = f->offs_v;
569 f->dma_offset.cr_h = f->offs_h;
570 f->dma_offset.cr_v = f->offs_v;
572 if (!variant->pix_hoff) {
573 if (f->fmt->colplanes == 3) {
574 f->dma_offset.cb_h >>= 1;
575 f->dma_offset.cr_h >>= 1;
577 if (f->fmt->color == S5P_FIMC_YCBCR420) {
578 f->dma_offset.cb_v >>= 1;
579 f->dma_offset.cr_v >>= 1;
583 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
584 f->fmt->color, f->dma_offset.y_h, f->dma_offset.y_v);
588 * fimc_prepare_config - check dimensions, operation and color mode
589 * and pre-calculate offset and the scaling coefficients.
591 * @ctx: hardware context information
592 * @flags: flags indicating which parameters to check/update
594 * Return: 0 if dimensions are valid or non zero otherwise.
596 int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags)
598 struct fimc_frame *s_frame, *d_frame;
599 struct vb2_buffer *vb = NULL;
602 s_frame = &ctx->s_frame;
603 d_frame = &ctx->d_frame;
605 if (flags & FIMC_PARAMS) {
606 /* Prepare the DMA offset ratios for scaler. */
607 fimc_prepare_dma_offset(ctx, &ctx->s_frame);
608 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
610 if (s_frame->height > (SCALER_MAX_VRATIO * d_frame->height) ||
611 s_frame->width > (SCALER_MAX_HRATIO * d_frame->width)) {
612 err("out of scaler range");
615 fimc_set_yuv_order(ctx);
618 if (flags & FIMC_SRC_ADDR) {
619 vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
620 ret = fimc_prepare_addr(ctx, vb, s_frame, &s_frame->paddr);
625 if (flags & FIMC_DST_ADDR) {
626 vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
627 ret = fimc_prepare_addr(ctx, vb, d_frame, &d_frame->paddr);
633 static void fimc_dma_run(void *priv)
635 struct fimc_ctx *ctx = priv;
636 struct fimc_dev *fimc;
640 if (WARN(!ctx, "null hardware context\n"))
643 fimc = ctx->fimc_dev;
644 spin_lock_irqsave(&fimc->slock, flags);
645 set_bit(ST_M2M_PEND, &fimc->state);
647 spin_lock(&ctx->slock);
648 ctx->state |= (FIMC_SRC_ADDR | FIMC_DST_ADDR);
649 ret = fimc_prepare_config(ctx, ctx->state);
653 /* Reconfigure hardware if the context has changed. */
654 if (fimc->m2m.ctx != ctx) {
655 ctx->state |= FIMC_PARAMS;
658 fimc_hw_set_input_addr(fimc, &ctx->s_frame.paddr);
660 if (ctx->state & FIMC_PARAMS) {
661 fimc_hw_set_input_path(ctx);
662 fimc_hw_set_in_dma(ctx);
663 ret = fimc_set_scaler_info(ctx);
665 spin_unlock(&fimc->slock);
668 fimc_hw_set_prescaler(ctx);
669 fimc_hw_set_mainscaler(ctx);
670 fimc_hw_set_target_format(ctx);
671 fimc_hw_set_rotation(ctx);
672 fimc_hw_set_effect(ctx, false);
675 fimc_hw_set_output_path(ctx);
676 if (ctx->state & (FIMC_DST_ADDR | FIMC_PARAMS))
677 fimc_hw_set_output_addr(fimc, &ctx->d_frame.paddr, -1);
679 if (ctx->state & FIMC_PARAMS) {
680 fimc_hw_set_out_dma(ctx);
681 if (fimc->variant->has_alpha)
682 fimc_hw_set_rgb_alpha(ctx);
685 fimc_activate_capture(ctx);
687 ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP |
688 FIMC_SRC_FMT | FIMC_DST_FMT);
689 fimc_hw_activate_input_dma(fimc, true);
691 spin_unlock(&ctx->slock);
692 spin_unlock_irqrestore(&fimc->slock, flags);
695 static void fimc_job_abort(void *priv)
697 fimc_m2m_shutdown(priv);
700 static int fimc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
701 unsigned int *num_buffers, unsigned int *num_planes,
702 unsigned int sizes[], void *allocators[])
704 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
705 struct fimc_frame *f;
708 f = ctx_get_frame(ctx, vq->type);
712 * Return number of non-contigous planes (plane buffers)
713 * depending on the configured color format.
718 *num_planes = f->fmt->memplanes;
719 for (i = 0; i < f->fmt->memplanes; i++) {
720 sizes[i] = (f->f_width * f->f_height * f->fmt->depth[i]) / 8;
721 allocators[i] = ctx->fimc_dev->alloc_ctx;
726 static int fimc_buf_prepare(struct vb2_buffer *vb)
728 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
729 struct fimc_frame *frame;
732 frame = ctx_get_frame(ctx, vb->vb2_queue->type);
734 return PTR_ERR(frame);
736 for (i = 0; i < frame->fmt->memplanes; i++)
737 vb2_set_plane_payload(vb, i, frame->payload[i]);
742 static void fimc_buf_queue(struct vb2_buffer *vb)
744 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
746 dbg("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
749 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
752 static void fimc_lock(struct vb2_queue *vq)
754 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
755 mutex_lock(&ctx->fimc_dev->lock);
758 static void fimc_unlock(struct vb2_queue *vq)
760 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
761 mutex_unlock(&ctx->fimc_dev->lock);
764 static struct vb2_ops fimc_qops = {
765 .queue_setup = fimc_queue_setup,
766 .buf_prepare = fimc_buf_prepare,
767 .buf_queue = fimc_buf_queue,
768 .wait_prepare = fimc_unlock,
769 .wait_finish = fimc_lock,
770 .stop_streaming = stop_streaming,
771 .start_streaming = start_streaming,
775 * V4L2 controls handling
777 #define ctrl_to_ctx(__ctrl) \
778 container_of((__ctrl)->handler, struct fimc_ctx, ctrl_handler)
780 static int __fimc_s_ctrl(struct fimc_ctx *ctx, struct v4l2_ctrl *ctrl)
782 struct fimc_dev *fimc = ctx->fimc_dev;
783 struct samsung_fimc_variant *variant = fimc->variant;
784 unsigned int flags = FIMC_DST_FMT | FIMC_SRC_FMT;
787 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
792 ctx->hflip = ctrl->val;
796 ctx->vflip = ctrl->val;
799 case V4L2_CID_ROTATE:
800 if (fimc_capture_pending(fimc) ||
801 (ctx->state & flags) == flags) {
802 ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
803 ctx->s_frame.height, ctx->d_frame.width,
804 ctx->d_frame.height, ctrl->val);
808 if ((ctrl->val == 90 || ctrl->val == 270) &&
809 !variant->has_out_rot)
812 ctx->rotation = ctrl->val;
815 case V4L2_CID_ALPHA_COMPONENT:
816 ctx->d_frame.alpha = ctrl->val;
819 ctx->state |= FIMC_PARAMS;
820 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
824 static int fimc_s_ctrl(struct v4l2_ctrl *ctrl)
826 struct fimc_ctx *ctx = ctrl_to_ctx(ctrl);
830 spin_lock_irqsave(&ctx->slock, flags);
831 ret = __fimc_s_ctrl(ctx, ctrl);
832 spin_unlock_irqrestore(&ctx->slock, flags);
837 static const struct v4l2_ctrl_ops fimc_ctrl_ops = {
838 .s_ctrl = fimc_s_ctrl,
841 int fimc_ctrls_create(struct fimc_ctx *ctx)
843 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
844 unsigned int max_alpha = fimc_get_alpha_mask(ctx->d_frame.fmt);
848 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 4);
850 ctx->ctrl_rotate = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
851 V4L2_CID_ROTATE, 0, 270, 90, 0);
852 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
853 V4L2_CID_HFLIP, 0, 1, 1, 0);
854 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
855 V4L2_CID_VFLIP, 0, 1, 1, 0);
856 if (variant->has_alpha)
857 ctx->ctrl_alpha = v4l2_ctrl_new_std(&ctx->ctrl_handler,
858 &fimc_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
861 ctx->ctrl_alpha = NULL;
863 ctx->ctrls_rdy = ctx->ctrl_handler.error == 0;
865 return ctx->ctrl_handler.error;
868 void fimc_ctrls_delete(struct fimc_ctx *ctx)
870 if (ctx->ctrls_rdy) {
871 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
872 ctx->ctrls_rdy = false;
873 ctx->ctrl_alpha = NULL;
877 void fimc_ctrls_activate(struct fimc_ctx *ctx, bool active)
879 unsigned int has_alpha = ctx->d_frame.fmt->flags & FMT_HAS_ALPHA;
884 mutex_lock(&ctx->ctrl_handler.lock);
885 v4l2_ctrl_activate(ctx->ctrl_rotate, active);
886 v4l2_ctrl_activate(ctx->ctrl_hflip, active);
887 v4l2_ctrl_activate(ctx->ctrl_vflip, active);
889 v4l2_ctrl_activate(ctx->ctrl_alpha, active && has_alpha);
892 ctx->rotation = ctx->ctrl_rotate->val;
893 ctx->hflip = ctx->ctrl_hflip->val;
894 ctx->vflip = ctx->ctrl_vflip->val;
900 mutex_unlock(&ctx->ctrl_handler.lock);
903 /* Update maximum value of the alpha color control */
904 void fimc_alpha_ctrl_update(struct fimc_ctx *ctx)
906 struct fimc_dev *fimc = ctx->fimc_dev;
907 struct v4l2_ctrl *ctrl = ctx->ctrl_alpha;
909 if (ctrl == NULL || !fimc->variant->has_alpha)
912 v4l2_ctrl_lock(ctrl);
913 ctrl->maximum = fimc_get_alpha_mask(ctx->d_frame.fmt);
915 if (ctrl->cur.val > ctrl->maximum)
916 ctrl->cur.val = ctrl->maximum;
918 v4l2_ctrl_unlock(ctrl);
922 * V4L2 ioctl handlers
924 static int fimc_m2m_querycap(struct file *file, void *fh,
925 struct v4l2_capability *cap)
927 struct fimc_ctx *ctx = fh_to_ctx(fh);
928 struct fimc_dev *fimc = ctx->fimc_dev;
930 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
931 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
932 cap->bus_info[0] = 0;
933 cap->capabilities = V4L2_CAP_STREAMING |
934 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
939 static int fimc_m2m_enum_fmt_mplane(struct file *file, void *priv,
940 struct v4l2_fmtdesc *f)
942 struct fimc_fmt *fmt;
944 fmt = fimc_find_format(NULL, NULL, get_m2m_fmt_flags(f->type),
949 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
950 f->pixelformat = fmt->fourcc;
954 int fimc_fill_format(struct fimc_frame *frame, struct v4l2_format *f)
956 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
959 pixm->width = frame->o_width;
960 pixm->height = frame->o_height;
961 pixm->field = V4L2_FIELD_NONE;
962 pixm->pixelformat = frame->fmt->fourcc;
963 pixm->colorspace = V4L2_COLORSPACE_JPEG;
964 pixm->num_planes = frame->fmt->memplanes;
966 for (i = 0; i < pixm->num_planes; ++i) {
967 int bpl = frame->f_width;
968 if (frame->fmt->colplanes == 1) /* packed formats */
969 bpl = (bpl * frame->fmt->depth[0]) / 8;
970 pixm->plane_fmt[i].bytesperline = bpl;
971 pixm->plane_fmt[i].sizeimage = (frame->o_width *
972 frame->o_height * frame->fmt->depth[i]) / 8;
977 void fimc_fill_frame(struct fimc_frame *frame, struct v4l2_format *f)
979 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
981 frame->f_width = pixm->plane_fmt[0].bytesperline;
982 if (frame->fmt->colplanes == 1)
983 frame->f_width = (frame->f_width * 8) / frame->fmt->depth[0];
984 frame->f_height = pixm->height;
985 frame->width = pixm->width;
986 frame->height = pixm->height;
987 frame->o_width = pixm->width;
988 frame->o_height = pixm->height;
994 * fimc_adjust_mplane_format - adjust bytesperline/sizeimage for each plane
995 * @fmt: fimc pixel format description (input)
996 * @width: requested pixel width
997 * @height: requested pixel height
998 * @pix: multi-plane format to adjust
1000 void fimc_adjust_mplane_format(struct fimc_fmt *fmt, u32 width, u32 height,
1001 struct v4l2_pix_format_mplane *pix)
1003 u32 bytesperline = 0;
1006 pix->colorspace = V4L2_COLORSPACE_JPEG;
1007 pix->field = V4L2_FIELD_NONE;
1008 pix->num_planes = fmt->memplanes;
1009 pix->pixelformat = fmt->fourcc;
1010 pix->height = height;
1013 for (i = 0; i < pix->num_planes; ++i) {
1014 u32 bpl = pix->plane_fmt[i].bytesperline;
1015 u32 *sizeimage = &pix->plane_fmt[i].sizeimage;
1017 if (fmt->colplanes > 1 && (bpl == 0 || bpl < pix->width))
1018 bpl = pix->width; /* Planar */
1020 if (fmt->colplanes == 1 && /* Packed */
1021 (bpl == 0 || ((bpl * 8) / fmt->depth[i]) < pix->width))
1022 bpl = (pix->width * fmt->depth[0]) / 8;
1024 if (i == 0) /* Same bytesperline for each plane. */
1027 pix->plane_fmt[i].bytesperline = bytesperline;
1028 *sizeimage = (pix->width * pix->height * fmt->depth[i]) / 8;
1032 static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
1033 struct v4l2_format *f)
1035 struct fimc_ctx *ctx = fh_to_ctx(fh);
1036 struct fimc_frame *frame = ctx_get_frame(ctx, f->type);
1039 return PTR_ERR(frame);
1041 return fimc_fill_format(frame, f);
1045 * fimc_find_format - lookup fimc color format by fourcc or media bus format
1046 * @pixelformat: fourcc to match, ignored if null
1047 * @mbus_code: media bus code to match, ignored if null
1048 * @mask: the color flags to match
1049 * @index: offset in the fimc_formats array, ignored if negative
1051 struct fimc_fmt *fimc_find_format(u32 *pixelformat, u32 *mbus_code,
1052 unsigned int mask, int index)
1054 struct fimc_fmt *fmt, *def_fmt = NULL;
1058 if (index >= ARRAY_SIZE(fimc_formats))
1061 for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
1062 fmt = &fimc_formats[i];
1063 if (!(fmt->flags & mask))
1065 if (pixelformat && fmt->fourcc == *pixelformat)
1067 if (mbus_code && fmt->mbus_code == *mbus_code)
1076 static int fimc_try_fmt_mplane(struct fimc_ctx *ctx, struct v4l2_format *f)
1078 struct fimc_dev *fimc = ctx->fimc_dev;
1079 struct samsung_fimc_variant *variant = fimc->variant;
1080 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1081 struct fimc_fmt *fmt;
1082 u32 max_w, mod_x, mod_y;
1084 if (!IS_M2M(f->type))
1087 dbg("w: %d, h: %d", pix->width, pix->height);
1089 fmt = fimc_find_format(&pix->pixelformat, NULL,
1090 get_m2m_fmt_flags(f->type), 0);
1091 if (WARN(fmt == NULL, "Pixel format lookup failed"))
1094 if (pix->field == V4L2_FIELD_ANY)
1095 pix->field = V4L2_FIELD_NONE;
1096 else if (pix->field != V4L2_FIELD_NONE)
1099 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1100 max_w = variant->pix_limit->scaler_dis_w;
1101 mod_x = ffs(variant->min_inp_pixsize) - 1;
1103 max_w = variant->pix_limit->out_rot_dis_w;
1104 mod_x = ffs(variant->min_out_pixsize) - 1;
1107 if (tiled_fmt(fmt)) {
1108 mod_x = 6; /* 64 x 32 pixels tile */
1111 if (variant->min_vsize_align == 1)
1112 mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
1114 mod_y = ffs(variant->min_vsize_align) - 1;
1117 v4l_bound_align_image(&pix->width, 16, max_w, mod_x,
1118 &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
1120 fimc_adjust_mplane_format(fmt, pix->width, pix->height, &f->fmt.pix_mp);
1124 static int fimc_m2m_try_fmt_mplane(struct file *file, void *fh,
1125 struct v4l2_format *f)
1127 struct fimc_ctx *ctx = fh_to_ctx(fh);
1129 return fimc_try_fmt_mplane(ctx, f);
1132 static int fimc_m2m_s_fmt_mplane(struct file *file, void *fh,
1133 struct v4l2_format *f)
1135 struct fimc_ctx *ctx = fh_to_ctx(fh);
1136 struct fimc_dev *fimc = ctx->fimc_dev;
1137 struct vb2_queue *vq;
1138 struct fimc_frame *frame;
1139 struct v4l2_pix_format_mplane *pix;
1142 ret = fimc_try_fmt_mplane(ctx, f);
1146 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
1148 if (vb2_is_busy(vq)) {
1149 v4l2_err(fimc->m2m.vfd, "queue (%d) busy\n", f->type);
1153 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1154 frame = &ctx->s_frame;
1156 frame = &ctx->d_frame;
1158 pix = &f->fmt.pix_mp;
1159 frame->fmt = fimc_find_format(&pix->pixelformat, NULL,
1160 get_m2m_fmt_flags(f->type), 0);
1164 /* Update RGB Alpha control state and value range */
1165 fimc_alpha_ctrl_update(ctx);
1167 for (i = 0; i < frame->fmt->colplanes; i++) {
1169 (pix->width * pix->height * frame->fmt->depth[i]) / 8;
1172 fimc_fill_frame(frame, f);
1174 ctx->scaler.enabled = 1;
1176 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1177 fimc_ctx_state_lock_set(FIMC_PARAMS | FIMC_DST_FMT, ctx);
1179 fimc_ctx_state_lock_set(FIMC_PARAMS | FIMC_SRC_FMT, ctx);
1181 dbg("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
1186 static int fimc_m2m_reqbufs(struct file *file, void *fh,
1187 struct v4l2_requestbuffers *reqbufs)
1189 struct fimc_ctx *ctx = fh_to_ctx(fh);
1191 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
1194 static int fimc_m2m_querybuf(struct file *file, void *fh,
1195 struct v4l2_buffer *buf)
1197 struct fimc_ctx *ctx = fh_to_ctx(fh);
1199 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
1202 static int fimc_m2m_qbuf(struct file *file, void *fh,
1203 struct v4l2_buffer *buf)
1205 struct fimc_ctx *ctx = fh_to_ctx(fh);
1207 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
1210 static int fimc_m2m_dqbuf(struct file *file, void *fh,
1211 struct v4l2_buffer *buf)
1213 struct fimc_ctx *ctx = fh_to_ctx(fh);
1215 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
1218 static int fimc_m2m_streamon(struct file *file, void *fh,
1219 enum v4l2_buf_type type)
1221 struct fimc_ctx *ctx = fh_to_ctx(fh);
1223 /* The source and target color format need to be set */
1224 if (V4L2_TYPE_IS_OUTPUT(type)) {
1225 if (!fimc_ctx_state_is_set(FIMC_SRC_FMT, ctx))
1227 } else if (!fimc_ctx_state_is_set(FIMC_DST_FMT, ctx)) {
1231 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
1234 static int fimc_m2m_streamoff(struct file *file, void *fh,
1235 enum v4l2_buf_type type)
1237 struct fimc_ctx *ctx = fh_to_ctx(fh);
1239 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
1242 static int fimc_m2m_cropcap(struct file *file, void *fh,
1243 struct v4l2_cropcap *cr)
1245 struct fimc_ctx *ctx = fh_to_ctx(fh);
1246 struct fimc_frame *frame;
1248 frame = ctx_get_frame(ctx, cr->type);
1250 return PTR_ERR(frame);
1252 cr->bounds.left = 0;
1254 cr->bounds.width = frame->o_width;
1255 cr->bounds.height = frame->o_height;
1256 cr->defrect = cr->bounds;
1261 static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1263 struct fimc_ctx *ctx = fh_to_ctx(fh);
1264 struct fimc_frame *frame;
1266 frame = ctx_get_frame(ctx, cr->type);
1268 return PTR_ERR(frame);
1270 cr->c.left = frame->offs_h;
1271 cr->c.top = frame->offs_v;
1272 cr->c.width = frame->width;
1273 cr->c.height = frame->height;
1278 static int fimc_m2m_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
1280 struct fimc_dev *fimc = ctx->fimc_dev;
1281 struct fimc_frame *f;
1282 u32 min_size, halign, depth = 0;
1285 if (cr->c.top < 0 || cr->c.left < 0) {
1286 v4l2_err(fimc->m2m.vfd,
1287 "doesn't support negative values for top & left\n");
1290 if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1292 else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1297 min_size = (f == &ctx->s_frame) ?
1298 fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
1300 /* Get pixel alignment constraints. */
1301 if (fimc->variant->min_vsize_align == 1)
1302 halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
1304 halign = ffs(fimc->variant->min_vsize_align) - 1;
1306 for (i = 0; i < f->fmt->colplanes; i++)
1307 depth += f->fmt->depth[i];
1309 v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
1311 &cr->c.height, min_size, f->o_height,
1312 halign, 64/(ALIGN(depth, 8)));
1314 /* adjust left/top if cropping rectangle is out of bounds */
1315 if (cr->c.left + cr->c.width > f->o_width)
1316 cr->c.left = f->o_width - cr->c.width;
1317 if (cr->c.top + cr->c.height > f->o_height)
1318 cr->c.top = f->o_height - cr->c.height;
1320 cr->c.left = round_down(cr->c.left, min_size);
1321 cr->c.top = round_down(cr->c.top, fimc->variant->hor_offs_align);
1323 dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
1324 cr->c.left, cr->c.top, cr->c.width, cr->c.height,
1325 f->f_width, f->f_height);
1330 static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1332 struct fimc_ctx *ctx = fh_to_ctx(fh);
1333 struct fimc_dev *fimc = ctx->fimc_dev;
1334 struct fimc_frame *f;
1337 ret = fimc_m2m_try_crop(ctx, cr);
1341 f = (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
1342 &ctx->s_frame : &ctx->d_frame;
1344 /* Check to see if scaling ratio is within supported range */
1345 if (fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
1346 if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1347 ret = fimc_check_scaler_ratio(ctx, cr->c.width,
1348 cr->c.height, ctx->d_frame.width,
1349 ctx->d_frame.height, ctx->rotation);
1351 ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
1352 ctx->s_frame.height, cr->c.width,
1353 cr->c.height, ctx->rotation);
1356 v4l2_err(fimc->m2m.vfd, "Out of scaler range\n");
1361 f->offs_h = cr->c.left;
1362 f->offs_v = cr->c.top;
1363 f->width = cr->c.width;
1364 f->height = cr->c.height;
1366 fimc_ctx_state_lock_set(FIMC_PARAMS, ctx);
1371 static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
1372 .vidioc_querycap = fimc_m2m_querycap,
1374 .vidioc_enum_fmt_vid_cap_mplane = fimc_m2m_enum_fmt_mplane,
1375 .vidioc_enum_fmt_vid_out_mplane = fimc_m2m_enum_fmt_mplane,
1377 .vidioc_g_fmt_vid_cap_mplane = fimc_m2m_g_fmt_mplane,
1378 .vidioc_g_fmt_vid_out_mplane = fimc_m2m_g_fmt_mplane,
1380 .vidioc_try_fmt_vid_cap_mplane = fimc_m2m_try_fmt_mplane,
1381 .vidioc_try_fmt_vid_out_mplane = fimc_m2m_try_fmt_mplane,
1383 .vidioc_s_fmt_vid_cap_mplane = fimc_m2m_s_fmt_mplane,
1384 .vidioc_s_fmt_vid_out_mplane = fimc_m2m_s_fmt_mplane,
1386 .vidioc_reqbufs = fimc_m2m_reqbufs,
1387 .vidioc_querybuf = fimc_m2m_querybuf,
1389 .vidioc_qbuf = fimc_m2m_qbuf,
1390 .vidioc_dqbuf = fimc_m2m_dqbuf,
1392 .vidioc_streamon = fimc_m2m_streamon,
1393 .vidioc_streamoff = fimc_m2m_streamoff,
1395 .vidioc_g_crop = fimc_m2m_g_crop,
1396 .vidioc_s_crop = fimc_m2m_s_crop,
1397 .vidioc_cropcap = fimc_m2m_cropcap
1401 static int queue_init(void *priv, struct vb2_queue *src_vq,
1402 struct vb2_queue *dst_vq)
1404 struct fimc_ctx *ctx = priv;
1407 memset(src_vq, 0, sizeof(*src_vq));
1408 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1409 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1410 src_vq->drv_priv = ctx;
1411 src_vq->ops = &fimc_qops;
1412 src_vq->mem_ops = &vb2_dma_contig_memops;
1413 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1415 ret = vb2_queue_init(src_vq);
1419 memset(dst_vq, 0, sizeof(*dst_vq));
1420 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1421 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1422 dst_vq->drv_priv = ctx;
1423 dst_vq->ops = &fimc_qops;
1424 dst_vq->mem_ops = &vb2_dma_contig_memops;
1425 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1427 return vb2_queue_init(dst_vq);
1430 static int fimc_m2m_open(struct file *file)
1432 struct fimc_dev *fimc = video_drvdata(file);
1433 struct fimc_ctx *ctx;
1436 dbg("pid: %d, state: 0x%lx, refcnt: %d",
1437 task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt);
1440 * Return if the corresponding video capture node
1441 * is already opened.
1443 if (fimc->vid_cap.refcnt > 0)
1446 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1449 v4l2_fh_init(&ctx->fh, fimc->m2m.vfd);
1450 ctx->fimc_dev = fimc;
1452 /* Default color format */
1453 ctx->s_frame.fmt = &fimc_formats[0];
1454 ctx->d_frame.fmt = &fimc_formats[0];
1456 ret = fimc_ctrls_create(ctx);
1460 /* Use separate control handler per file handle */
1461 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
1462 file->private_data = &ctx->fh;
1463 v4l2_fh_add(&ctx->fh);
1465 /* Setup the device context for memory-to-memory mode */
1466 ctx->state = FIMC_CTX_M2M;
1468 ctx->in_path = FIMC_DMA;
1469 ctx->out_path = FIMC_DMA;
1470 spin_lock_init(&ctx->slock);
1472 ctx->m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
1473 if (IS_ERR(ctx->m2m_ctx)) {
1474 ret = PTR_ERR(ctx->m2m_ctx);
1478 if (fimc->m2m.refcnt++ == 0)
1479 set_bit(ST_M2M_RUN, &fimc->state);
1483 fimc_ctrls_delete(ctx);
1485 v4l2_fh_del(&ctx->fh);
1486 v4l2_fh_exit(&ctx->fh);
1491 static int fimc_m2m_release(struct file *file)
1493 struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
1494 struct fimc_dev *fimc = ctx->fimc_dev;
1496 dbg("pid: %d, state: 0x%lx, refcnt= %d",
1497 task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
1499 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1500 fimc_ctrls_delete(ctx);
1501 v4l2_fh_del(&ctx->fh);
1502 v4l2_fh_exit(&ctx->fh);
1504 if (--fimc->m2m.refcnt <= 0)
1505 clear_bit(ST_M2M_RUN, &fimc->state);
1510 static unsigned int fimc_m2m_poll(struct file *file,
1511 struct poll_table_struct *wait)
1513 struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
1515 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1519 static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
1521 struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
1523 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1526 static const struct v4l2_file_operations fimc_m2m_fops = {
1527 .owner = THIS_MODULE,
1528 .open = fimc_m2m_open,
1529 .release = fimc_m2m_release,
1530 .poll = fimc_m2m_poll,
1531 .unlocked_ioctl = video_ioctl2,
1532 .mmap = fimc_m2m_mmap,
1535 static struct v4l2_m2m_ops m2m_ops = {
1536 .device_run = fimc_dma_run,
1537 .job_abort = fimc_job_abort,
1540 int fimc_register_m2m_device(struct fimc_dev *fimc,
1541 struct v4l2_device *v4l2_dev)
1543 struct video_device *vfd;
1544 struct platform_device *pdev;
1551 fimc->v4l2_dev = v4l2_dev;
1553 vfd = video_device_alloc();
1555 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1559 vfd->fops = &fimc_m2m_fops;
1560 vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
1561 vfd->v4l2_dev = v4l2_dev;
1563 vfd->release = video_device_release;
1564 vfd->lock = &fimc->lock;
1566 snprintf(vfd->name, sizeof(vfd->name), "%s.m2m", dev_name(&pdev->dev));
1567 video_set_drvdata(vfd, fimc);
1569 fimc->m2m.vfd = vfd;
1570 fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
1571 if (IS_ERR(fimc->m2m.m2m_dev)) {
1572 v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
1573 ret = PTR_ERR(fimc->m2m.m2m_dev);
1577 ret = media_entity_init(&vfd->entity, 0, NULL, 0);
1581 v4l2_m2m_release(fimc->m2m.m2m_dev);
1583 video_device_release(fimc->m2m.vfd);
1587 void fimc_unregister_m2m_device(struct fimc_dev *fimc)
1592 if (fimc->m2m.m2m_dev)
1593 v4l2_m2m_release(fimc->m2m.m2m_dev);
1594 if (fimc->m2m.vfd) {
1595 media_entity_cleanup(&fimc->m2m.vfd->entity);
1596 /* Can also be called if video device wasn't registered */
1597 video_unregister_device(fimc->m2m.vfd);
1601 static void fimc_clk_put(struct fimc_dev *fimc)
1604 for (i = 0; i < fimc->num_clocks; i++) {
1605 if (IS_ERR_OR_NULL(fimc->clock[i]))
1607 clk_unprepare(fimc->clock[i]);
1608 clk_put(fimc->clock[i]);
1609 fimc->clock[i] = NULL;
1613 static int fimc_clk_get(struct fimc_dev *fimc)
1617 for (i = 0; i < fimc->num_clocks; i++) {
1618 fimc->clock[i] = clk_get(&fimc->pdev->dev, fimc_clocks[i]);
1619 if (IS_ERR(fimc->clock[i]))
1621 ret = clk_prepare(fimc->clock[i]);
1623 clk_put(fimc->clock[i]);
1624 fimc->clock[i] = NULL;
1631 dev_err(&fimc->pdev->dev, "failed to get clock: %s\n",
1636 static int fimc_m2m_suspend(struct fimc_dev *fimc)
1638 unsigned long flags;
1641 spin_lock_irqsave(&fimc->slock, flags);
1642 if (!fimc_m2m_pending(fimc)) {
1643 spin_unlock_irqrestore(&fimc->slock, flags);
1646 clear_bit(ST_M2M_SUSPENDED, &fimc->state);
1647 set_bit(ST_M2M_SUSPENDING, &fimc->state);
1648 spin_unlock_irqrestore(&fimc->slock, flags);
1650 timeout = wait_event_timeout(fimc->irq_queue,
1651 test_bit(ST_M2M_SUSPENDED, &fimc->state),
1652 FIMC_SHUTDOWN_TIMEOUT);
1654 clear_bit(ST_M2M_SUSPENDING, &fimc->state);
1655 return timeout == 0 ? -EAGAIN : 0;
1658 static int fimc_m2m_resume(struct fimc_dev *fimc)
1660 unsigned long flags;
1662 spin_lock_irqsave(&fimc->slock, flags);
1663 /* Clear for full H/W setup in first run after resume */
1664 fimc->m2m.ctx = NULL;
1665 spin_unlock_irqrestore(&fimc->slock, flags);
1667 if (test_and_clear_bit(ST_M2M_SUSPENDED, &fimc->state))
1668 fimc_m2m_job_finish(fimc->m2m.ctx,
1669 VB2_BUF_STATE_ERROR);
1673 static int fimc_probe(struct platform_device *pdev)
1675 struct fimc_dev *fimc;
1676 struct resource *res;
1677 struct samsung_fimc_driverdata *drv_data;
1678 struct s5p_platform_fimc *pdata;
1681 drv_data = (struct samsung_fimc_driverdata *)
1682 platform_get_device_id(pdev)->driver_data;
1684 if (pdev->id >= drv_data->num_entities) {
1685 dev_err(&pdev->dev, "Invalid platform device id: %d\n",
1690 fimc = devm_kzalloc(&pdev->dev, sizeof(*fimc), GFP_KERNEL);
1694 fimc->id = pdev->id;
1696 fimc->variant = drv_data->variant[fimc->id];
1698 pdata = pdev->dev.platform_data;
1699 fimc->pdata = pdata;
1701 init_waitqueue_head(&fimc->irq_queue);
1702 spin_lock_init(&fimc->slock);
1703 mutex_init(&fimc->lock);
1705 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1706 fimc->regs = devm_request_and_ioremap(&pdev->dev, res);
1707 if (fimc->regs == NULL) {
1708 dev_err(&pdev->dev, "Failed to obtain io memory\n");
1712 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1714 dev_err(&pdev->dev, "Failed to get IRQ resource\n");
1717 fimc->irq = res->start;
1719 fimc->num_clocks = MAX_FIMC_CLOCKS;
1720 ret = fimc_clk_get(fimc);
1723 clk_set_rate(fimc->clock[CLK_BUS], drv_data->lclk_frequency);
1724 clk_enable(fimc->clock[CLK_BUS]);
1726 platform_set_drvdata(pdev, fimc);
1728 ret = devm_request_irq(&pdev->dev, fimc->irq, fimc_irq_handler,
1729 0, pdev->name, fimc);
1731 dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
1735 pm_runtime_enable(&pdev->dev);
1736 ret = pm_runtime_get_sync(&pdev->dev);
1739 /* Initialize contiguous memory allocator */
1740 fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1741 if (IS_ERR(fimc->alloc_ctx)) {
1742 ret = PTR_ERR(fimc->alloc_ctx);
1746 dev_dbg(&pdev->dev, "FIMC.%d registered successfully\n", fimc->id);
1748 pm_runtime_put(&pdev->dev);
1752 pm_runtime_put(&pdev->dev);
1758 static int fimc_runtime_resume(struct device *dev)
1760 struct fimc_dev *fimc = dev_get_drvdata(dev);
1762 dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
1764 /* Enable clocks and perform basic initalization */
1765 clk_enable(fimc->clock[CLK_GATE]);
1766 fimc_hw_reset(fimc);
1768 /* Resume the capture or mem-to-mem device */
1769 if (fimc_capture_busy(fimc))
1770 return fimc_capture_resume(fimc);
1772 return fimc_m2m_resume(fimc);
1775 static int fimc_runtime_suspend(struct device *dev)
1777 struct fimc_dev *fimc = dev_get_drvdata(dev);
1780 if (fimc_capture_busy(fimc))
1781 ret = fimc_capture_suspend(fimc);
1783 ret = fimc_m2m_suspend(fimc);
1785 clk_disable(fimc->clock[CLK_GATE]);
1787 dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
1791 #ifdef CONFIG_PM_SLEEP
1792 static int fimc_resume(struct device *dev)
1794 struct fimc_dev *fimc = dev_get_drvdata(dev);
1795 unsigned long flags;
1797 dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
1799 /* Do not resume if the device was idle before system suspend */
1800 spin_lock_irqsave(&fimc->slock, flags);
1801 if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
1802 (!fimc_m2m_active(fimc) && !fimc_capture_busy(fimc))) {
1803 spin_unlock_irqrestore(&fimc->slock, flags);
1806 fimc_hw_reset(fimc);
1807 spin_unlock_irqrestore(&fimc->slock, flags);
1809 if (fimc_capture_busy(fimc))
1810 return fimc_capture_resume(fimc);
1812 return fimc_m2m_resume(fimc);
1815 static int fimc_suspend(struct device *dev)
1817 struct fimc_dev *fimc = dev_get_drvdata(dev);
1819 dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
1821 if (test_and_set_bit(ST_LPM, &fimc->state))
1823 if (fimc_capture_busy(fimc))
1824 return fimc_capture_suspend(fimc);
1826 return fimc_m2m_suspend(fimc);
1828 #endif /* CONFIG_PM_SLEEP */
1830 static int __devexit fimc_remove(struct platform_device *pdev)
1832 struct fimc_dev *fimc = platform_get_drvdata(pdev);
1834 pm_runtime_disable(&pdev->dev);
1835 pm_runtime_set_suspended(&pdev->dev);
1837 vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
1839 clk_disable(fimc->clock[CLK_BUS]);
1842 dev_info(&pdev->dev, "driver unloaded\n");
1846 /* Image pixel limits, similar across several FIMC HW revisions. */
1847 static struct fimc_pix_limit s5p_pix_limit[4] = {
1849 .scaler_en_w = 3264,
1850 .scaler_dis_w = 8192,
1851 .in_rot_en_h = 1920,
1852 .in_rot_dis_w = 8192,
1853 .out_rot_en_w = 1920,
1854 .out_rot_dis_w = 4224,
1857 .scaler_en_w = 4224,
1858 .scaler_dis_w = 8192,
1859 .in_rot_en_h = 1920,
1860 .in_rot_dis_w = 8192,
1861 .out_rot_en_w = 1920,
1862 .out_rot_dis_w = 4224,
1865 .scaler_en_w = 1920,
1866 .scaler_dis_w = 8192,
1867 .in_rot_en_h = 1280,
1868 .in_rot_dis_w = 8192,
1869 .out_rot_en_w = 1280,
1870 .out_rot_dis_w = 1920,
1873 .scaler_en_w = 1920,
1874 .scaler_dis_w = 8192,
1875 .in_rot_en_h = 1366,
1876 .in_rot_dis_w = 8192,
1877 .out_rot_en_w = 1366,
1878 .out_rot_dis_w = 1920,
1882 static struct samsung_fimc_variant fimc0_variant_s5p = {
1886 .min_inp_pixsize = 16,
1887 .min_out_pixsize = 16,
1888 .hor_offs_align = 8,
1889 .min_vsize_align = 16,
1891 .pix_limit = &s5p_pix_limit[0],
1894 static struct samsung_fimc_variant fimc2_variant_s5p = {
1896 .min_inp_pixsize = 16,
1897 .min_out_pixsize = 16,
1898 .hor_offs_align = 8,
1899 .min_vsize_align = 16,
1901 .pix_limit = &s5p_pix_limit[1],
1904 static struct samsung_fimc_variant fimc0_variant_s5pv210 = {
1909 .min_inp_pixsize = 16,
1910 .min_out_pixsize = 16,
1911 .hor_offs_align = 8,
1912 .min_vsize_align = 16,
1914 .pix_limit = &s5p_pix_limit[1],
1917 static struct samsung_fimc_variant fimc1_variant_s5pv210 = {
1922 .has_mainscaler_ext = 1,
1923 .min_inp_pixsize = 16,
1924 .min_out_pixsize = 16,
1925 .hor_offs_align = 1,
1926 .min_vsize_align = 1,
1928 .pix_limit = &s5p_pix_limit[2],
1931 static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
1934 .min_inp_pixsize = 16,
1935 .min_out_pixsize = 16,
1936 .hor_offs_align = 8,
1937 .min_vsize_align = 16,
1939 .pix_limit = &s5p_pix_limit[2],
1942 static struct samsung_fimc_variant fimc0_variant_exynos4 = {
1948 .has_mainscaler_ext = 1,
1950 .min_inp_pixsize = 16,
1951 .min_out_pixsize = 16,
1952 .hor_offs_align = 2,
1953 .min_vsize_align = 1,
1954 .out_buf_count = 32,
1955 .pix_limit = &s5p_pix_limit[1],
1958 static struct samsung_fimc_variant fimc3_variant_exynos4 = {
1962 .has_mainscaler_ext = 1,
1964 .min_inp_pixsize = 16,
1965 .min_out_pixsize = 16,
1966 .hor_offs_align = 2,
1967 .min_vsize_align = 1,
1968 .out_buf_count = 32,
1969 .pix_limit = &s5p_pix_limit[3],
1973 static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
1975 [0] = &fimc0_variant_s5p,
1976 [1] = &fimc0_variant_s5p,
1977 [2] = &fimc2_variant_s5p,
1980 .lclk_frequency = 133000000UL,
1983 /* S5PV210, S5PC110 */
1984 static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
1986 [0] = &fimc0_variant_s5pv210,
1987 [1] = &fimc1_variant_s5pv210,
1988 [2] = &fimc2_variant_s5pv210,
1991 .lclk_frequency = 166000000UL,
1994 /* S5PV310, S5PC210 */
1995 static struct samsung_fimc_driverdata fimc_drvdata_exynos4 = {
1997 [0] = &fimc0_variant_exynos4,
1998 [1] = &fimc0_variant_exynos4,
1999 [2] = &fimc0_variant_exynos4,
2000 [3] = &fimc3_variant_exynos4,
2003 .lclk_frequency = 166000000UL,
2006 static struct platform_device_id fimc_driver_ids[] = {
2009 .driver_data = (unsigned long)&fimc_drvdata_s5p,
2011 .name = "s5pv210-fimc",
2012 .driver_data = (unsigned long)&fimc_drvdata_s5pv210,
2014 .name = "exynos4-fimc",
2015 .driver_data = (unsigned long)&fimc_drvdata_exynos4,
2019 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
2021 static const struct dev_pm_ops fimc_pm_ops = {
2022 SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend, fimc_resume)
2023 SET_RUNTIME_PM_OPS(fimc_runtime_suspend, fimc_runtime_resume, NULL)
2026 static struct platform_driver fimc_driver = {
2027 .probe = fimc_probe,
2028 .remove = __devexit_p(fimc_remove),
2029 .id_table = fimc_driver_ids,
2031 .name = FIMC_MODULE_NAME,
2032 .owner = THIS_MODULE,
2037 int __init fimc_register_driver(void)
2039 return platform_driver_probe(&fimc_driver, fimc_probe);
2042 void __exit fimc_unregister_driver(void)
2044 platform_driver_unregister(&fimc_driver);