2 * S5P camera interface (video postprocessor) driver
4 * Copyright (c) 2010 Samsung Electronics Co., Ltd
6 * Sylwester Nawrocki, <s.nawrocki@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation, either version 2 of the License,
11 * or (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/version.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/bug.h>
20 #include <linux/interrupt.h>
21 #include <linux/device.h>
22 #include <linux/platform_device.h>
23 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <linux/clk.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/videobuf2-core.h>
29 #include <media/videobuf2-dma-contig.h>
31 #include "fimc-core.h"
33 static char *fimc_clocks[MAX_FIMC_CLOCKS] = {
34 "sclk_fimc", "fimc", "sclk_cam"
37 static struct fimc_fmt fimc_formats[] = {
40 .fourcc = V4L2_PIX_FMT_RGB565X,
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 = "XRGB-8-8-8-8, 32 bpp",
56 .fourcc = V4L2_PIX_FMT_RGB32,
58 .color = S5P_FIMC_RGB888,
61 .flags = FMT_FLAGS_M2M,
63 .name = "YUV 4:2:2 packed, YCbYCr",
64 .fourcc = V4L2_PIX_FMT_YUYV,
66 .color = S5P_FIMC_YCBYCR422,
69 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
70 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
72 .name = "YUV 4:2:2 packed, CbYCrY",
73 .fourcc = V4L2_PIX_FMT_UYVY,
75 .color = S5P_FIMC_CBYCRY422,
78 .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
79 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
81 .name = "YUV 4:2:2 packed, CrYCbY",
82 .fourcc = V4L2_PIX_FMT_VYUY,
84 .color = S5P_FIMC_CRYCBY422,
87 .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
88 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
90 .name = "YUV 4:2:2 packed, YCrYCb",
91 .fourcc = V4L2_PIX_FMT_YVYU,
93 .color = S5P_FIMC_YCRYCB422,
96 .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
97 .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
99 .name = "YUV 4:2:2 planar, Y/Cb/Cr",
100 .fourcc = V4L2_PIX_FMT_YUV422P,
102 .color = S5P_FIMC_YCBYCR422,
105 .flags = FMT_FLAGS_M2M,
107 .name = "YUV 4:2:2 planar, Y/CbCr",
108 .fourcc = V4L2_PIX_FMT_NV16,
110 .color = S5P_FIMC_YCBYCR422,
113 .flags = FMT_FLAGS_M2M,
115 .name = "YUV 4:2:2 planar, Y/CrCb",
116 .fourcc = V4L2_PIX_FMT_NV61,
118 .color = S5P_FIMC_YCRYCB422,
121 .flags = FMT_FLAGS_M2M,
123 .name = "YUV 4:2:0 planar, YCbCr",
124 .fourcc = V4L2_PIX_FMT_YUV420,
126 .color = S5P_FIMC_YCBCR420,
129 .flags = FMT_FLAGS_M2M,
131 .name = "YUV 4:2:0 planar, Y/CbCr",
132 .fourcc = V4L2_PIX_FMT_NV12,
134 .color = S5P_FIMC_YCBCR420,
137 .flags = FMT_FLAGS_M2M,
139 .name = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr",
140 .fourcc = V4L2_PIX_FMT_NV12M,
141 .color = S5P_FIMC_YCBCR420,
145 .flags = FMT_FLAGS_M2M,
147 .name = "YUV 4:2:0 non-contiguous 3-planar, Y/Cb/Cr",
148 .fourcc = V4L2_PIX_FMT_YUV420M,
149 .color = S5P_FIMC_YCBCR420,
150 .depth = { 8, 2, 2 },
153 .flags = FMT_FLAGS_M2M,
155 .name = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr, tiled",
156 .fourcc = V4L2_PIX_FMT_NV12MT,
157 .color = S5P_FIMC_YCBCR420,
161 .flags = FMT_FLAGS_M2M,
165 static struct v4l2_queryctrl fimc_ctrls[] = {
167 .id = V4L2_CID_HFLIP,
168 .type = V4L2_CTRL_TYPE_BOOLEAN,
169 .name = "Horizontal flip",
174 .id = V4L2_CID_VFLIP,
175 .type = V4L2_CTRL_TYPE_BOOLEAN,
176 .name = "Vertical flip",
181 .id = V4L2_CID_ROTATE,
182 .type = V4L2_CTRL_TYPE_INTEGER,
183 .name = "Rotation (CCW)",
192 static struct v4l2_queryctrl *get_ctrl(int id)
196 for (i = 0; i < ARRAY_SIZE(fimc_ctrls); ++i)
197 if (id == fimc_ctrls[i].id)
198 return &fimc_ctrls[i];
202 int fimc_check_scaler_ratio(int sw, int sh, int dw, int dh, int rot)
206 if (rot == 90 || rot == 270) {
214 if ((sw >= SCALER_MAX_HRATIO * tx) || (sh >= SCALER_MAX_VRATIO * ty))
220 static int fimc_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
229 if (src >= tar * tmp) {
230 *shift = sh, *ratio = tmp;
234 *shift = 0, *ratio = 1;
238 int fimc_set_scaler_info(struct fimc_ctx *ctx)
240 struct fimc_scaler *sc = &ctx->scaler;
241 struct fimc_frame *s_frame = &ctx->s_frame;
242 struct fimc_frame *d_frame = &ctx->d_frame;
243 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
247 if (ctx->rotation == 90 || ctx->rotation == 270) {
249 tx = d_frame->height;
252 ty = d_frame->height;
254 if (tx <= 0 || ty <= 0) {
255 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev,
256 "invalid target size: %d x %d", tx, ty);
261 sy = s_frame->height;
262 if (sx <= 0 || sy <= 0) {
263 err("invalid source size: %d x %d", sx, sy);
267 sc->real_height = sy;
269 ret = fimc_get_scaler_factor(sx, tx, &sc->pre_hratio, &sc->hfactor);
273 ret = fimc_get_scaler_factor(sy, ty, &sc->pre_vratio, &sc->vfactor);
277 sc->pre_dst_width = sx / sc->pre_hratio;
278 sc->pre_dst_height = sy / sc->pre_vratio;
280 if (variant->has_mainscaler_ext) {
281 sc->main_hratio = (sx << 14) / (tx << sc->hfactor);
282 sc->main_vratio = (sy << 14) / (ty << sc->vfactor);
284 sc->main_hratio = (sx << 8) / (tx << sc->hfactor);
285 sc->main_vratio = (sy << 8) / (ty << sc->vfactor);
289 sc->scaleup_h = (tx >= sx) ? 1 : 0;
290 sc->scaleup_v = (ty >= sy) ? 1 : 0;
292 /* check to see if input and output size/format differ */
293 if (s_frame->fmt->color == d_frame->fmt->color
294 && s_frame->width == d_frame->width
295 && s_frame->height == d_frame->height)
303 static void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state)
305 struct vb2_buffer *src_vb, *dst_vb;
306 struct fimc_dev *fimc = ctx->fimc_dev;
308 if (!ctx || !ctx->m2m_ctx)
311 src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
312 dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
314 if (src_vb && dst_vb) {
315 v4l2_m2m_buf_done(src_vb, vb_state);
316 v4l2_m2m_buf_done(dst_vb, vb_state);
317 v4l2_m2m_job_finish(fimc->m2m.m2m_dev, ctx->m2m_ctx);
321 /* Complete the transaction which has been scheduled for execution. */
322 static void fimc_m2m_shutdown(struct fimc_ctx *ctx)
324 struct fimc_dev *fimc = ctx->fimc_dev;
327 if (!fimc_m2m_pending(fimc))
330 fimc_ctx_state_lock_set(FIMC_CTX_SHUT, ctx);
332 ret = wait_event_timeout(fimc->irq_queue,
333 !fimc_ctx_state_is_set(FIMC_CTX_SHUT, ctx),
334 FIMC_SHUTDOWN_TIMEOUT);
336 * In case of a timeout the buffers are not released in the interrupt
337 * handler so return them here with the error flag set, if there are
341 fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
344 static int stop_streaming(struct vb2_queue *q)
346 struct fimc_ctx *ctx = q->drv_priv;
348 fimc_m2m_shutdown(ctx);
353 static void fimc_capture_irq_handler(struct fimc_dev *fimc)
355 struct fimc_vid_cap *cap = &fimc->vid_cap;
356 struct fimc_vid_buffer *v_buf;
360 if (!list_empty(&cap->active_buf_q) &&
361 test_bit(ST_CAPT_RUN, &fimc->state)) {
362 ktime_get_real_ts(&ts);
364 v_buf = active_queue_pop(cap);
366 tv = &v_buf->vb.v4l2_buf.timestamp;
367 tv->tv_sec = ts.tv_sec;
368 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
369 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
371 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
374 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
375 wake_up(&fimc->irq_queue);
379 if (!list_empty(&cap->pending_buf_q)) {
381 v_buf = pending_queue_pop(cap);
382 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
383 v_buf->index = cap->buf_index;
385 /* Move the buffer to the capture active queue */
386 active_queue_add(cap, v_buf);
388 dbg("next frame: %d, done frame: %d",
389 fimc_hw_get_frame_index(fimc), v_buf->index);
391 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
395 if (cap->active_buf_cnt == 0) {
396 clear_bit(ST_CAPT_RUN, &fimc->state);
398 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
401 set_bit(ST_CAPT_RUN, &fimc->state);
404 dbg("frame: %d, active_buf_cnt: %d",
405 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
408 static irqreturn_t fimc_isr(int irq, void *priv)
410 struct fimc_dev *fimc = priv;
411 struct fimc_vid_cap *cap = &fimc->vid_cap;
412 struct fimc_ctx *ctx;
414 fimc_hw_clear_irq(fimc);
416 if (test_and_clear_bit(ST_M2M_PEND, &fimc->state)) {
417 ctx = v4l2_m2m_get_curr_priv(fimc->m2m.m2m_dev);
419 fimc_m2m_job_finish(ctx, VB2_BUF_STATE_DONE);
421 spin_lock(&ctx->slock);
422 if (ctx->state & FIMC_CTX_SHUT) {
423 ctx->state &= ~FIMC_CTX_SHUT;
424 wake_up(&fimc->irq_queue);
426 spin_unlock(&ctx->slock);
432 spin_lock(&fimc->slock);
434 if (test_bit(ST_CAPT_PEND, &fimc->state)) {
435 fimc_capture_irq_handler(fimc);
437 if (cap->active_buf_cnt == 1) {
438 fimc_deactivate_capture(fimc);
439 clear_bit(ST_CAPT_STREAM, &fimc->state);
443 spin_unlock(&fimc->slock);
447 /* The color format (colplanes, memplanes) must be already configured. */
448 int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
449 struct fimc_frame *frame, struct fimc_addr *paddr)
454 if (vb == NULL || frame == NULL)
457 pix_size = frame->width * frame->height;
459 dbg("memplanes= %d, colplanes= %d, pix_size= %d",
460 frame->fmt->memplanes, frame->fmt->colplanes, pix_size);
462 paddr->y = vb2_dma_contig_plane_paddr(vb, 0);
464 if (frame->fmt->memplanes == 1) {
465 switch (frame->fmt->colplanes) {
471 /* decompose Y into Y/Cb */
472 paddr->cb = (u32)(paddr->y + pix_size);
476 paddr->cb = (u32)(paddr->y + pix_size);
477 /* decompose Y into Y/Cb/Cr */
478 if (S5P_FIMC_YCBCR420 == frame->fmt->color)
479 paddr->cr = (u32)(paddr->cb
482 paddr->cr = (u32)(paddr->cb
489 if (frame->fmt->memplanes >= 2)
490 paddr->cb = vb2_dma_contig_plane_paddr(vb, 1);
492 if (frame->fmt->memplanes == 3)
493 paddr->cr = vb2_dma_contig_plane_paddr(vb, 2);
496 dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
497 paddr->y, paddr->cb, paddr->cr, ret);
502 /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
503 static void fimc_set_yuv_order(struct fimc_ctx *ctx)
505 /* The one only mode supported in SoC. */
506 ctx->in_order_2p = S5P_FIMC_LSB_CRCB;
507 ctx->out_order_2p = S5P_FIMC_LSB_CRCB;
509 /* Set order for 1 plane input formats. */
510 switch (ctx->s_frame.fmt->color) {
511 case S5P_FIMC_YCRYCB422:
512 ctx->in_order_1p = S5P_MSCTRL_ORDER422_CBYCRY;
514 case S5P_FIMC_CBYCRY422:
515 ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCRYCB;
517 case S5P_FIMC_CRYCBY422:
518 ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCBYCR;
520 case S5P_FIMC_YCBYCR422:
522 ctx->in_order_1p = S5P_MSCTRL_ORDER422_CRYCBY;
525 dbg("ctx->in_order_1p= %d", ctx->in_order_1p);
527 switch (ctx->d_frame.fmt->color) {
528 case S5P_FIMC_YCRYCB422:
529 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CBYCRY;
531 case S5P_FIMC_CBYCRY422:
532 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCRYCB;
534 case S5P_FIMC_CRYCBY422:
535 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCBYCR;
537 case S5P_FIMC_YCBYCR422:
539 ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CRYCBY;
542 dbg("ctx->out_order_1p= %d", ctx->out_order_1p);
545 static void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f)
547 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
550 for (i = 0; i < f->fmt->colplanes; i++)
551 depth += f->fmt->depth[i];
553 f->dma_offset.y_h = f->offs_h;
554 if (!variant->pix_hoff)
555 f->dma_offset.y_h *= (depth >> 3);
557 f->dma_offset.y_v = f->offs_v;
559 f->dma_offset.cb_h = f->offs_h;
560 f->dma_offset.cb_v = f->offs_v;
562 f->dma_offset.cr_h = f->offs_h;
563 f->dma_offset.cr_v = f->offs_v;
565 if (!variant->pix_hoff) {
566 if (f->fmt->colplanes == 3) {
567 f->dma_offset.cb_h >>= 1;
568 f->dma_offset.cr_h >>= 1;
570 if (f->fmt->color == S5P_FIMC_YCBCR420) {
571 f->dma_offset.cb_v >>= 1;
572 f->dma_offset.cr_v >>= 1;
576 dbg("in_offset: color= %d, y_h= %d, y_v= %d",
577 f->fmt->color, f->dma_offset.y_h, f->dma_offset.y_v);
581 * fimc_prepare_config - check dimensions, operation and color mode
582 * and pre-calculate offset and the scaling coefficients.
584 * @ctx: hardware context information
585 * @flags: flags indicating which parameters to check/update
587 * Return: 0 if dimensions are valid or non zero otherwise.
589 int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags)
591 struct fimc_frame *s_frame, *d_frame;
592 struct vb2_buffer *vb = NULL;
595 s_frame = &ctx->s_frame;
596 d_frame = &ctx->d_frame;
598 if (flags & FIMC_PARAMS) {
599 /* Prepare the DMA offset ratios for scaler. */
600 fimc_prepare_dma_offset(ctx, &ctx->s_frame);
601 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
603 if (s_frame->height > (SCALER_MAX_VRATIO * d_frame->height) ||
604 s_frame->width > (SCALER_MAX_HRATIO * d_frame->width)) {
605 err("out of scaler range");
608 fimc_set_yuv_order(ctx);
611 /* Input DMA mode is not allowed when the scaler is disabled. */
612 ctx->scaler.enabled = 1;
614 if (flags & FIMC_SRC_ADDR) {
615 vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
616 ret = fimc_prepare_addr(ctx, vb, s_frame, &s_frame->paddr);
621 if (flags & FIMC_DST_ADDR) {
622 vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
623 ret = fimc_prepare_addr(ctx, vb, d_frame, &d_frame->paddr);
629 static void fimc_dma_run(void *priv)
631 struct fimc_ctx *ctx = priv;
632 struct fimc_dev *fimc;
636 if (WARN(!ctx, "null hardware context\n"))
639 fimc = ctx->fimc_dev;
641 spin_lock_irqsave(&ctx->slock, flags);
642 set_bit(ST_M2M_PEND, &fimc->state);
644 ctx->state |= (FIMC_SRC_ADDR | FIMC_DST_ADDR);
645 ret = fimc_prepare_config(ctx, ctx->state);
649 /* Reconfigure hardware if the context has changed. */
650 if (fimc->m2m.ctx != ctx) {
651 ctx->state |= FIMC_PARAMS;
655 spin_lock(&fimc->slock);
656 fimc_hw_set_input_addr(fimc, &ctx->s_frame.paddr);
658 if (ctx->state & FIMC_PARAMS) {
659 fimc_hw_set_input_path(ctx);
660 fimc_hw_set_in_dma(ctx);
661 ret = fimc_set_scaler_info(ctx);
663 spin_unlock(&fimc->slock);
666 fimc_hw_set_prescaler(ctx);
667 fimc_hw_set_mainscaler(ctx);
668 fimc_hw_set_target_format(ctx);
669 fimc_hw_set_rotation(ctx);
670 fimc_hw_set_effect(ctx);
673 fimc_hw_set_output_path(ctx);
674 if (ctx->state & (FIMC_DST_ADDR | FIMC_PARAMS))
675 fimc_hw_set_output_addr(fimc, &ctx->d_frame.paddr, -1);
677 if (ctx->state & FIMC_PARAMS)
678 fimc_hw_set_out_dma(ctx);
680 fimc_activate_capture(ctx);
682 ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP |
683 FIMC_SRC_FMT | FIMC_DST_FMT);
684 fimc_hw_activate_input_dma(fimc, true);
685 spin_unlock(&fimc->slock);
688 spin_unlock_irqrestore(&ctx->slock, flags);
691 static void fimc_job_abort(void *priv)
693 fimc_m2m_shutdown(priv);
696 static int fimc_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
697 unsigned int *num_planes, unsigned long sizes[],
700 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
701 struct fimc_frame *f;
704 f = ctx_get_frame(ctx, vq->type);
709 * Return number of non-contigous planes (plane buffers)
710 * depending on the configured color format.
713 *num_planes = f->fmt->memplanes;
715 for (i = 0; i < f->fmt->memplanes; i++) {
716 sizes[i] = (f->width * f->height * f->fmt->depth[i]) >> 3;
717 allocators[i] = ctx->fimc_dev->alloc_ctx;
720 if (*num_buffers == 0)
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,
773 static int fimc_m2m_querycap(struct file *file, void *priv,
774 struct v4l2_capability *cap)
776 struct fimc_ctx *ctx = file->private_data;
777 struct fimc_dev *fimc = ctx->fimc_dev;
779 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
780 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
781 cap->bus_info[0] = 0;
782 cap->version = KERNEL_VERSION(1, 0, 0);
783 cap->capabilities = V4L2_CAP_STREAMING |
784 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
785 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
790 int fimc_vidioc_enum_fmt_mplane(struct file *file, void *priv,
791 struct v4l2_fmtdesc *f)
793 struct fimc_fmt *fmt;
795 if (f->index >= ARRAY_SIZE(fimc_formats))
798 fmt = &fimc_formats[f->index];
799 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
800 f->pixelformat = fmt->fourcc;
805 int fimc_vidioc_g_fmt_mplane(struct file *file, void *priv,
806 struct v4l2_format *f)
808 struct fimc_ctx *ctx = priv;
809 struct fimc_frame *frame;
810 struct v4l2_pix_format_mplane *pixm;
813 frame = ctx_get_frame(ctx, f->type);
815 return PTR_ERR(frame);
817 pixm = &f->fmt.pix_mp;
819 pixm->width = frame->width;
820 pixm->height = frame->height;
821 pixm->field = V4L2_FIELD_NONE;
822 pixm->pixelformat = frame->fmt->fourcc;
823 pixm->colorspace = V4L2_COLORSPACE_JPEG;
824 pixm->num_planes = frame->fmt->memplanes;
826 for (i = 0; i < pixm->num_planes; ++i) {
827 int bpl = frame->o_width;
829 if (frame->fmt->colplanes == 1) /* packed formats */
830 bpl = (bpl * frame->fmt->depth[0]) / 8;
832 pixm->plane_fmt[i].bytesperline = bpl;
834 pixm->plane_fmt[i].sizeimage = (frame->o_width *
835 frame->o_height * frame->fmt->depth[i]) / 8;
841 struct fimc_fmt *find_format(struct v4l2_format *f, unsigned int mask)
843 struct fimc_fmt *fmt;
846 for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
847 fmt = &fimc_formats[i];
848 if (fmt->fourcc == f->fmt.pix.pixelformat &&
853 return (i == ARRAY_SIZE(fimc_formats)) ? NULL : fmt;
856 struct fimc_fmt *find_mbus_format(struct v4l2_mbus_framefmt *f,
859 struct fimc_fmt *fmt;
862 for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
863 fmt = &fimc_formats[i];
864 if (fmt->mbus_code == f->code && (fmt->flags & mask))
868 return (i == ARRAY_SIZE(fimc_formats)) ? NULL : fmt;
872 int fimc_vidioc_try_fmt_mplane(struct file *file, void *priv,
873 struct v4l2_format *f)
875 struct fimc_ctx *ctx = priv;
876 struct fimc_dev *fimc = ctx->fimc_dev;
877 struct samsung_fimc_variant *variant = fimc->variant;
878 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
879 struct fimc_fmt *fmt;
880 u32 max_width, mod_x, mod_y, mask;
881 int i, is_output = 0;
884 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
885 if (fimc_ctx_state_is_set(FIMC_CTX_CAP, ctx))
888 } else if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
892 dbg("w: %d, h: %d", pix->width, pix->height);
894 mask = is_output ? FMT_FLAGS_M2M : FMT_FLAGS_M2M | FMT_FLAGS_CAM;
895 fmt = find_format(f, mask);
897 v4l2_err(&fimc->m2m.v4l2_dev, "Fourcc format (0x%X) invalid.\n",
902 if (pix->field == V4L2_FIELD_ANY)
903 pix->field = V4L2_FIELD_NONE;
904 else if (V4L2_FIELD_NONE != pix->field)
908 max_width = variant->pix_limit->scaler_dis_w;
909 mod_x = ffs(variant->min_inp_pixsize) - 1;
911 max_width = variant->pix_limit->out_rot_dis_w;
912 mod_x = ffs(variant->min_out_pixsize) - 1;
915 if (tiled_fmt(fmt)) {
916 mod_x = 6; /* 64 x 32 pixels tile */
919 if (fimc->id == 1 && variant->pix_hoff)
920 mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
925 dbg("mod_x: %d, mod_y: %d, max_w: %d", mod_x, mod_y, max_width);
927 v4l_bound_align_image(&pix->width, 16, max_width, mod_x,
928 &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
930 pix->num_planes = fmt->memplanes;
931 pix->colorspace = V4L2_COLORSPACE_JPEG;
934 for (i = 0; i < pix->num_planes; ++i) {
935 u32 bpl = pix->plane_fmt[i].bytesperline;
936 u32 *sizeimage = &pix->plane_fmt[i].sizeimage;
938 if (fmt->colplanes > 1 && (bpl == 0 || bpl < pix->width))
939 bpl = pix->width; /* Planar */
941 if (fmt->colplanes == 1 && /* Packed */
942 (bpl == 0 || ((bpl * 8) / fmt->depth[i]) < pix->width))
943 bpl = (pix->width * fmt->depth[0]) / 8;
945 if (i == 0) /* Same bytesperline for each plane. */
948 pix->plane_fmt[i].bytesperline = mod_x;
949 *sizeimage = (pix->width * pix->height * fmt->depth[i]) / 8;
955 static int fimc_m2m_s_fmt_mplane(struct file *file, void *priv,
956 struct v4l2_format *f)
958 struct fimc_ctx *ctx = priv;
959 struct fimc_dev *fimc = ctx->fimc_dev;
960 struct vb2_queue *vq;
961 struct fimc_frame *frame;
962 struct v4l2_pix_format_mplane *pix;
965 ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
969 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
971 if (vb2_is_busy(vq)) {
972 v4l2_err(&fimc->m2m.v4l2_dev, "queue (%d) busy\n", f->type);
976 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
977 frame = &ctx->s_frame;
978 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
979 frame = &ctx->d_frame;
981 v4l2_err(&fimc->m2m.v4l2_dev,
982 "Wrong buffer/video queue type (%d)\n", f->type);
986 pix = &f->fmt.pix_mp;
987 frame->fmt = find_format(f, FMT_FLAGS_M2M);
991 for (i = 0; i < frame->fmt->colplanes; i++) {
993 (pix->width * pix->height * frame->fmt->depth[i]) / 8;
996 frame->f_width = pix->plane_fmt[0].bytesperline * 8 /
997 frame->fmt->depth[0];
998 frame->f_height = pix->height;
999 frame->width = pix->width;
1000 frame->height = pix->height;
1001 frame->o_width = pix->width;
1002 frame->o_height = pix->height;
1006 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1007 fimc_ctx_state_lock_set(FIMC_PARAMS | FIMC_DST_FMT, ctx);
1009 fimc_ctx_state_lock_set(FIMC_PARAMS | FIMC_SRC_FMT, ctx);
1011 dbg("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
1016 static int fimc_m2m_reqbufs(struct file *file, void *priv,
1017 struct v4l2_requestbuffers *reqbufs)
1019 struct fimc_ctx *ctx = priv;
1020 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
1023 static int fimc_m2m_querybuf(struct file *file, void *priv,
1024 struct v4l2_buffer *buf)
1026 struct fimc_ctx *ctx = priv;
1027 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
1030 static int fimc_m2m_qbuf(struct file *file, void *priv,
1031 struct v4l2_buffer *buf)
1033 struct fimc_ctx *ctx = priv;
1035 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
1038 static int fimc_m2m_dqbuf(struct file *file, void *priv,
1039 struct v4l2_buffer *buf)
1041 struct fimc_ctx *ctx = priv;
1042 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
1045 static int fimc_m2m_streamon(struct file *file, void *priv,
1046 enum v4l2_buf_type type)
1048 struct fimc_ctx *ctx = priv;
1050 /* The source and target color format need to be set */
1051 if (V4L2_TYPE_IS_OUTPUT(type)) {
1052 if (!fimc_ctx_state_is_set(FIMC_SRC_FMT, ctx))
1054 } else if (!fimc_ctx_state_is_set(FIMC_DST_FMT, ctx)) {
1058 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
1061 static int fimc_m2m_streamoff(struct file *file, void *priv,
1062 enum v4l2_buf_type type)
1064 struct fimc_ctx *ctx = priv;
1065 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
1068 int fimc_vidioc_queryctrl(struct file *file, void *priv,
1069 struct v4l2_queryctrl *qc)
1071 struct fimc_ctx *ctx = priv;
1072 struct v4l2_queryctrl *c;
1075 c = get_ctrl(qc->id);
1081 if (fimc_ctx_state_is_set(FIMC_CTX_CAP, ctx)) {
1082 return v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
1083 core, queryctrl, qc);
1088 int fimc_vidioc_g_ctrl(struct file *file, void *priv,
1089 struct v4l2_control *ctrl)
1091 struct fimc_ctx *ctx = priv;
1092 struct fimc_dev *fimc = ctx->fimc_dev;
1095 case V4L2_CID_HFLIP:
1096 ctrl->value = (FLIP_X_AXIS & ctx->flip) ? 1 : 0;
1098 case V4L2_CID_VFLIP:
1099 ctrl->value = (FLIP_Y_AXIS & ctx->flip) ? 1 : 0;
1101 case V4L2_CID_ROTATE:
1102 ctrl->value = ctx->rotation;
1105 if (fimc_ctx_state_is_set(FIMC_CTX_CAP, ctx)) {
1106 return v4l2_subdev_call(fimc->vid_cap.sd, core,
1109 v4l2_err(&fimc->m2m.v4l2_dev, "Invalid control\n");
1113 dbg("ctrl->value= %d", ctrl->value);
1118 int check_ctrl_val(struct fimc_ctx *ctx, struct v4l2_control *ctrl)
1120 struct v4l2_queryctrl *c;
1121 c = get_ctrl(ctrl->id);
1125 if (ctrl->value < c->minimum || ctrl->value > c->maximum
1126 || (c->step != 0 && ctrl->value % c->step != 0)) {
1127 v4l2_err(&ctx->fimc_dev->m2m.v4l2_dev,
1128 "Invalid control value\n");
1135 int fimc_s_ctrl(struct fimc_ctx *ctx, struct v4l2_control *ctrl)
1137 struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
1138 struct fimc_dev *fimc = ctx->fimc_dev;
1142 case V4L2_CID_HFLIP:
1144 ctx->flip |= FLIP_X_AXIS;
1146 ctx->flip &= ~FLIP_X_AXIS;
1149 case V4L2_CID_VFLIP:
1151 ctx->flip |= FLIP_Y_AXIS;
1153 ctx->flip &= ~FLIP_Y_AXIS;
1156 case V4L2_CID_ROTATE:
1157 if (fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
1158 ret = fimc_check_scaler_ratio(ctx->s_frame.width,
1159 ctx->s_frame.height, ctx->d_frame.width,
1160 ctx->d_frame.height, ctrl->value);
1164 v4l2_err(&fimc->m2m.v4l2_dev, "Out of scaler range\n");
1168 /* Check for the output rotator availability */
1169 if ((ctrl->value == 90 || ctrl->value == 270) &&
1170 (ctx->in_path == FIMC_DMA && !variant->has_out_rot))
1172 ctx->rotation = ctrl->value;
1176 v4l2_err(&fimc->m2m.v4l2_dev, "Invalid control\n");
1180 fimc_ctx_state_lock_set(FIMC_PARAMS, ctx);
1185 static int fimc_m2m_s_ctrl(struct file *file, void *priv,
1186 struct v4l2_control *ctrl)
1188 struct fimc_ctx *ctx = priv;
1191 ret = check_ctrl_val(ctx, ctrl);
1195 ret = fimc_s_ctrl(ctx, ctrl);
1199 static int fimc_m2m_cropcap(struct file *file, void *fh,
1200 struct v4l2_cropcap *cr)
1202 struct fimc_frame *frame;
1203 struct fimc_ctx *ctx = fh;
1205 frame = ctx_get_frame(ctx, cr->type);
1207 return PTR_ERR(frame);
1209 cr->bounds.left = 0;
1211 cr->bounds.width = frame->f_width;
1212 cr->bounds.height = frame->f_height;
1213 cr->defrect = cr->bounds;
1218 static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1220 struct fimc_frame *frame;
1221 struct fimc_ctx *ctx = file->private_data;
1223 frame = ctx_get_frame(ctx, cr->type);
1225 return PTR_ERR(frame);
1227 cr->c.left = frame->offs_h;
1228 cr->c.top = frame->offs_v;
1229 cr->c.width = frame->width;
1230 cr->c.height = frame->height;
1235 int fimc_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
1237 struct fimc_dev *fimc = ctx->fimc_dev;
1238 struct fimc_frame *f;
1239 u32 min_size, halign, depth = 0;
1240 bool is_capture_ctx;
1243 if (cr->c.top < 0 || cr->c.left < 0) {
1244 v4l2_err(&fimc->m2m.v4l2_dev,
1245 "doesn't support negative values for top & left\n");
1249 is_capture_ctx = fimc_ctx_state_is_set(FIMC_CTX_CAP, ctx);
1251 if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1252 f = is_capture_ctx ? &ctx->s_frame : &ctx->d_frame;
1253 else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1259 min_size = (f == &ctx->s_frame) ?
1260 fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
1262 /* Get pixel alignment constraints. */
1263 if (is_capture_ctx) {
1267 if (fimc->id == 1 && fimc->variant->pix_hoff)
1268 halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
1270 halign = ffs(min_size) - 1;
1273 for (i = 0; i < f->fmt->colplanes; i++)
1274 depth += f->fmt->depth[i];
1276 v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
1278 &cr->c.height, min_size, f->o_height,
1279 halign, 64/(ALIGN(depth, 8)));
1281 /* adjust left/top if cropping rectangle is out of bounds */
1282 if (cr->c.left + cr->c.width > f->o_width)
1283 cr->c.left = f->o_width - cr->c.width;
1284 if (cr->c.top + cr->c.height > f->o_height)
1285 cr->c.top = f->o_height - cr->c.height;
1287 cr->c.left = round_down(cr->c.left, min_size);
1288 cr->c.top = round_down(cr->c.top, is_capture_ctx ? 16 : 8);
1290 dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
1291 cr->c.left, cr->c.top, cr->c.width, cr->c.height,
1292 f->f_width, f->f_height);
1297 static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1299 struct fimc_ctx *ctx = file->private_data;
1300 struct fimc_dev *fimc = ctx->fimc_dev;
1301 struct fimc_frame *f;
1304 ret = fimc_try_crop(ctx, cr);
1308 f = (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
1309 &ctx->s_frame : &ctx->d_frame;
1311 /* Check to see if scaling ratio is within supported range */
1312 if (fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
1313 if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1314 ret = fimc_check_scaler_ratio(cr->c.width, cr->c.height,
1316 ctx->d_frame.height,
1319 ret = fimc_check_scaler_ratio(ctx->s_frame.width,
1320 ctx->s_frame.height,
1321 cr->c.width, cr->c.height,
1325 v4l2_err(&fimc->m2m.v4l2_dev, "Out of scaler range\n");
1330 f->offs_h = cr->c.left;
1331 f->offs_v = cr->c.top;
1332 f->width = cr->c.width;
1333 f->height = cr->c.height;
1335 fimc_ctx_state_lock_set(FIMC_PARAMS, ctx);
1340 static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
1341 .vidioc_querycap = fimc_m2m_querycap,
1343 .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
1344 .vidioc_enum_fmt_vid_out_mplane = fimc_vidioc_enum_fmt_mplane,
1346 .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane,
1347 .vidioc_g_fmt_vid_out_mplane = fimc_vidioc_g_fmt_mplane,
1349 .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane,
1350 .vidioc_try_fmt_vid_out_mplane = fimc_vidioc_try_fmt_mplane,
1352 .vidioc_s_fmt_vid_cap_mplane = fimc_m2m_s_fmt_mplane,
1353 .vidioc_s_fmt_vid_out_mplane = fimc_m2m_s_fmt_mplane,
1355 .vidioc_reqbufs = fimc_m2m_reqbufs,
1356 .vidioc_querybuf = fimc_m2m_querybuf,
1358 .vidioc_qbuf = fimc_m2m_qbuf,
1359 .vidioc_dqbuf = fimc_m2m_dqbuf,
1361 .vidioc_streamon = fimc_m2m_streamon,
1362 .vidioc_streamoff = fimc_m2m_streamoff,
1364 .vidioc_queryctrl = fimc_vidioc_queryctrl,
1365 .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
1366 .vidioc_s_ctrl = fimc_m2m_s_ctrl,
1368 .vidioc_g_crop = fimc_m2m_g_crop,
1369 .vidioc_s_crop = fimc_m2m_s_crop,
1370 .vidioc_cropcap = fimc_m2m_cropcap
1374 static int queue_init(void *priv, struct vb2_queue *src_vq,
1375 struct vb2_queue *dst_vq)
1377 struct fimc_ctx *ctx = priv;
1380 memset(src_vq, 0, sizeof(*src_vq));
1381 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1382 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1383 src_vq->drv_priv = ctx;
1384 src_vq->ops = &fimc_qops;
1385 src_vq->mem_ops = &vb2_dma_contig_memops;
1386 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1388 ret = vb2_queue_init(src_vq);
1392 memset(dst_vq, 0, sizeof(*dst_vq));
1393 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1394 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
1395 dst_vq->drv_priv = ctx;
1396 dst_vq->ops = &fimc_qops;
1397 dst_vq->mem_ops = &vb2_dma_contig_memops;
1398 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1400 return vb2_queue_init(dst_vq);
1403 static int fimc_m2m_open(struct file *file)
1405 struct fimc_dev *fimc = video_drvdata(file);
1406 struct fimc_ctx *ctx = NULL;
1408 dbg("pid: %d, state: 0x%lx, refcnt: %d",
1409 task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt);
1412 * Return if the corresponding video capture node
1413 * is already opened.
1415 if (fimc->vid_cap.refcnt > 0)
1419 set_bit(ST_OUTDMA_RUN, &fimc->state);
1421 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1425 file->private_data = ctx;
1426 ctx->fimc_dev = fimc;
1427 /* Default color format */
1428 ctx->s_frame.fmt = &fimc_formats[0];
1429 ctx->d_frame.fmt = &fimc_formats[0];
1430 /* Setup the device context for mem2mem mode. */
1431 ctx->state = FIMC_CTX_M2M;
1433 ctx->in_path = FIMC_DMA;
1434 ctx->out_path = FIMC_DMA;
1435 spin_lock_init(&ctx->slock);
1437 ctx->m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
1438 if (IS_ERR(ctx->m2m_ctx)) {
1439 int err = PTR_ERR(ctx->m2m_ctx);
1447 static int fimc_m2m_release(struct file *file)
1449 struct fimc_ctx *ctx = file->private_data;
1450 struct fimc_dev *fimc = ctx->fimc_dev;
1452 dbg("pid: %d, state: 0x%lx, refcnt= %d",
1453 task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
1455 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1457 if (--fimc->m2m.refcnt <= 0)
1458 clear_bit(ST_OUTDMA_RUN, &fimc->state);
1463 static unsigned int fimc_m2m_poll(struct file *file,
1464 struct poll_table_struct *wait)
1466 struct fimc_ctx *ctx = file->private_data;
1468 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1472 static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
1474 struct fimc_ctx *ctx = file->private_data;
1476 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1479 static const struct v4l2_file_operations fimc_m2m_fops = {
1480 .owner = THIS_MODULE,
1481 .open = fimc_m2m_open,
1482 .release = fimc_m2m_release,
1483 .poll = fimc_m2m_poll,
1484 .unlocked_ioctl = video_ioctl2,
1485 .mmap = fimc_m2m_mmap,
1488 static struct v4l2_m2m_ops m2m_ops = {
1489 .device_run = fimc_dma_run,
1490 .job_abort = fimc_job_abort,
1493 static int fimc_register_m2m_device(struct fimc_dev *fimc)
1495 struct video_device *vfd;
1496 struct platform_device *pdev;
1497 struct v4l2_device *v4l2_dev;
1504 v4l2_dev = &fimc->m2m.v4l2_dev;
1506 /* set name if it is empty */
1507 if (!v4l2_dev->name[0])
1508 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
1509 "%s.m2m", dev_name(&pdev->dev));
1511 ret = v4l2_device_register(&pdev->dev, v4l2_dev);
1515 vfd = video_device_alloc();
1517 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1521 vfd->fops = &fimc_m2m_fops;
1522 vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
1524 vfd->release = video_device_release;
1525 vfd->lock = &fimc->lock;
1527 snprintf(vfd->name, sizeof(vfd->name), "%s:m2m", dev_name(&pdev->dev));
1529 video_set_drvdata(vfd, fimc);
1530 platform_set_drvdata(pdev, fimc);
1532 fimc->m2m.vfd = vfd;
1533 fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
1534 if (IS_ERR(fimc->m2m.m2m_dev)) {
1535 v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
1536 ret = PTR_ERR(fimc->m2m.m2m_dev);
1540 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1543 "%s(): failed to register video device\n", __func__);
1547 "FIMC m2m driver registered as /dev/video%d\n", vfd->num);
1552 v4l2_m2m_release(fimc->m2m.m2m_dev);
1554 video_device_release(fimc->m2m.vfd);
1556 v4l2_device_unregister(v4l2_dev);
1561 static void fimc_unregister_m2m_device(struct fimc_dev *fimc)
1564 v4l2_m2m_release(fimc->m2m.m2m_dev);
1565 video_unregister_device(fimc->m2m.vfd);
1567 v4l2_device_unregister(&fimc->m2m.v4l2_dev);
1571 static void fimc_clk_release(struct fimc_dev *fimc)
1574 for (i = 0; i < fimc->num_clocks; i++) {
1575 if (fimc->clock[i]) {
1576 clk_disable(fimc->clock[i]);
1577 clk_put(fimc->clock[i]);
1582 static int fimc_clk_get(struct fimc_dev *fimc)
1585 for (i = 0; i < fimc->num_clocks; i++) {
1586 fimc->clock[i] = clk_get(&fimc->pdev->dev, fimc_clocks[i]);
1588 if (!IS_ERR_OR_NULL(fimc->clock[i])) {
1589 clk_enable(fimc->clock[i]);
1592 dev_err(&fimc->pdev->dev, "failed to get fimc clock: %s\n",
1599 static int fimc_probe(struct platform_device *pdev)
1601 struct fimc_dev *fimc;
1602 struct resource *res;
1603 struct samsung_fimc_driverdata *drv_data;
1604 struct s5p_platform_fimc *pdata;
1606 int cap_input_index = -1;
1608 dev_dbg(&pdev->dev, "%s():\n", __func__);
1610 drv_data = (struct samsung_fimc_driverdata *)
1611 platform_get_device_id(pdev)->driver_data;
1613 if (pdev->id >= drv_data->num_entities) {
1614 dev_err(&pdev->dev, "Invalid platform device id: %d\n",
1619 fimc = kzalloc(sizeof(struct fimc_dev), GFP_KERNEL);
1623 fimc->id = pdev->id;
1624 fimc->variant = drv_data->variant[fimc->id];
1626 pdata = pdev->dev.platform_data;
1627 fimc->pdata = pdata;
1628 fimc->state = ST_IDLE;
1630 init_waitqueue_head(&fimc->irq_queue);
1631 spin_lock_init(&fimc->slock);
1633 mutex_init(&fimc->lock);
1635 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1637 dev_err(&pdev->dev, "failed to find the registers\n");
1642 fimc->regs_res = request_mem_region(res->start, resource_size(res),
1643 dev_name(&pdev->dev));
1644 if (!fimc->regs_res) {
1645 dev_err(&pdev->dev, "failed to obtain register region\n");
1650 fimc->regs = ioremap(res->start, resource_size(res));
1652 dev_err(&pdev->dev, "failed to map registers\n");
1654 goto err_req_region;
1657 fimc->num_clocks = MAX_FIMC_CLOCKS - 1;
1659 /* Check if a video capture node needs to be registered. */
1660 if (pdata && pdata->num_clients > 0) {
1661 cap_input_index = 0;
1665 ret = fimc_clk_get(fimc);
1667 goto err_regs_unmap;
1668 clk_set_rate(fimc->clock[CLK_BUS], drv_data->lclk_frequency);
1670 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1672 dev_err(&pdev->dev, "failed to get IRQ resource\n");
1676 fimc->irq = res->start;
1678 fimc_hw_reset(fimc);
1680 ret = request_irq(fimc->irq, fimc_isr, 0, pdev->name, fimc);
1682 dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
1686 /* Initialize contiguous memory allocator */
1687 fimc->alloc_ctx = vb2_dma_contig_init_ctx(&fimc->pdev->dev);
1688 if (IS_ERR(fimc->alloc_ctx)) {
1689 ret = PTR_ERR(fimc->alloc_ctx);
1693 ret = fimc_register_m2m_device(fimc);
1697 /* At least one camera sensor is required to register capture node */
1698 if (cap_input_index >= 0) {
1699 ret = fimc_register_capture_device(fimc);
1702 clk_disable(fimc->clock[CLK_CAM]);
1705 * Exclude the additional output DMA address registers by masking
1706 * them out on HW revisions that provide extended capabilites.
1708 if (fimc->variant->out_buf_count > 4)
1709 fimc_hw_set_dma_seq(fimc, 0xF);
1711 dev_dbg(&pdev->dev, "%s(): fimc-%d registered successfully\n",
1712 __func__, fimc->id);
1717 fimc_unregister_m2m_device(fimc);
1719 free_irq(fimc->irq, fimc);
1721 fimc_clk_release(fimc);
1723 iounmap(fimc->regs);
1725 release_resource(fimc->regs_res);
1726 kfree(fimc->regs_res);
1733 static int __devexit fimc_remove(struct platform_device *pdev)
1735 struct fimc_dev *fimc =
1736 (struct fimc_dev *)platform_get_drvdata(pdev);
1738 free_irq(fimc->irq, fimc);
1739 fimc_hw_reset(fimc);
1741 fimc_unregister_m2m_device(fimc);
1742 fimc_unregister_capture_device(fimc);
1744 fimc_clk_release(fimc);
1746 vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
1748 iounmap(fimc->regs);
1749 release_resource(fimc->regs_res);
1750 kfree(fimc->regs_res);
1753 dev_info(&pdev->dev, "%s driver unloaded\n", pdev->name);
1757 /* Image pixel limits, similar across several FIMC HW revisions. */
1758 static struct fimc_pix_limit s5p_pix_limit[4] = {
1760 .scaler_en_w = 3264,
1761 .scaler_dis_w = 8192,
1762 .in_rot_en_h = 1920,
1763 .in_rot_dis_w = 8192,
1764 .out_rot_en_w = 1920,
1765 .out_rot_dis_w = 4224,
1768 .scaler_en_w = 4224,
1769 .scaler_dis_w = 8192,
1770 .in_rot_en_h = 1920,
1771 .in_rot_dis_w = 8192,
1772 .out_rot_en_w = 1920,
1773 .out_rot_dis_w = 4224,
1776 .scaler_en_w = 1920,
1777 .scaler_dis_w = 8192,
1778 .in_rot_en_h = 1280,
1779 .in_rot_dis_w = 8192,
1780 .out_rot_en_w = 1280,
1781 .out_rot_dis_w = 1920,
1784 .scaler_en_w = 1920,
1785 .scaler_dis_w = 8192,
1786 .in_rot_en_h = 1366,
1787 .in_rot_dis_w = 8192,
1788 .out_rot_en_w = 1366,
1789 .out_rot_dis_w = 1920,
1793 static struct samsung_fimc_variant fimc0_variant_s5p = {
1796 .min_inp_pixsize = 16,
1797 .min_out_pixsize = 16,
1798 .hor_offs_align = 8,
1800 .pix_limit = &s5p_pix_limit[0],
1803 static struct samsung_fimc_variant fimc2_variant_s5p = {
1804 .min_inp_pixsize = 16,
1805 .min_out_pixsize = 16,
1806 .hor_offs_align = 8,
1808 .pix_limit = &s5p_pix_limit[1],
1811 static struct samsung_fimc_variant fimc0_variant_s5pv210 = {
1815 .min_inp_pixsize = 16,
1816 .min_out_pixsize = 16,
1817 .hor_offs_align = 8,
1819 .pix_limit = &s5p_pix_limit[1],
1822 static struct samsung_fimc_variant fimc1_variant_s5pv210 = {
1826 .has_mainscaler_ext = 1,
1827 .min_inp_pixsize = 16,
1828 .min_out_pixsize = 16,
1829 .hor_offs_align = 1,
1831 .pix_limit = &s5p_pix_limit[2],
1834 static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
1836 .min_inp_pixsize = 16,
1837 .min_out_pixsize = 16,
1838 .hor_offs_align = 8,
1840 .pix_limit = &s5p_pix_limit[2],
1843 static struct samsung_fimc_variant fimc0_variant_exynos4 = {
1848 .has_mainscaler_ext = 1,
1849 .min_inp_pixsize = 16,
1850 .min_out_pixsize = 16,
1851 .hor_offs_align = 1,
1852 .out_buf_count = 32,
1853 .pix_limit = &s5p_pix_limit[1],
1856 static struct samsung_fimc_variant fimc2_variant_exynos4 = {
1859 .has_mainscaler_ext = 1,
1860 .min_inp_pixsize = 16,
1861 .min_out_pixsize = 16,
1862 .hor_offs_align = 1,
1863 .out_buf_count = 32,
1864 .pix_limit = &s5p_pix_limit[3],
1868 static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
1870 [0] = &fimc0_variant_s5p,
1871 [1] = &fimc0_variant_s5p,
1872 [2] = &fimc2_variant_s5p,
1875 .lclk_frequency = 133000000UL,
1878 /* S5PV210, S5PC110 */
1879 static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
1881 [0] = &fimc0_variant_s5pv210,
1882 [1] = &fimc1_variant_s5pv210,
1883 [2] = &fimc2_variant_s5pv210,
1886 .lclk_frequency = 166000000UL,
1889 /* S5PV310, S5PC210 */
1890 static struct samsung_fimc_driverdata fimc_drvdata_exynos4 = {
1892 [0] = &fimc0_variant_exynos4,
1893 [1] = &fimc0_variant_exynos4,
1894 [2] = &fimc0_variant_exynos4,
1895 [3] = &fimc2_variant_exynos4,
1898 .lclk_frequency = 166000000UL,
1901 static struct platform_device_id fimc_driver_ids[] = {
1904 .driver_data = (unsigned long)&fimc_drvdata_s5p,
1906 .name = "s5pv210-fimc",
1907 .driver_data = (unsigned long)&fimc_drvdata_s5pv210,
1909 .name = "exynos4-fimc",
1910 .driver_data = (unsigned long)&fimc_drvdata_exynos4,
1914 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1916 static struct platform_driver fimc_driver = {
1917 .probe = fimc_probe,
1918 .remove = __devexit_p(fimc_remove),
1919 .id_table = fimc_driver_ids,
1921 .name = MODULE_NAME,
1922 .owner = THIS_MODULE,
1926 static int __init fimc_init(void)
1928 int ret = platform_driver_register(&fimc_driver);
1930 err("platform_driver_register failed: %d\n", ret);
1934 static void __exit fimc_exit(void)
1936 platform_driver_unregister(&fimc_driver);
1939 module_init(fimc_init);
1940 module_exit(fimc_exit);
1942 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1943 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1944 MODULE_LICENSE("GPL");