2 * Samsung S5P SoC series camera interface (camera capture) driver
4 * Copyright (c) 2010 Samsung Electronics Co., Ltd
5 * Author: 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 version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/version.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/list.h>
22 #include <linux/slab.h>
23 #include <linux/clk.h>
24 #include <linux/i2c.h>
26 #include <linux/videodev2.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-mem2mem.h>
30 #include <media/videobuf2-core.h>
31 #include <media/videobuf2-dma-contig.h>
33 #include "fimc-core.h"
35 static struct v4l2_subdev *fimc_subdev_register(struct fimc_dev *fimc,
36 struct s5p_fimc_isp_info *isp_info)
38 struct i2c_adapter *i2c_adap;
39 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
40 struct v4l2_subdev *sd = NULL;
42 i2c_adap = i2c_get_adapter(isp_info->i2c_bus_num);
44 return ERR_PTR(-ENOMEM);
46 sd = v4l2_i2c_new_subdev_board(&vid_cap->v4l2_dev, i2c_adap,
47 isp_info->board_info, NULL);
49 v4l2_err(&vid_cap->v4l2_dev, "failed to acquire subdev\n");
53 v4l2_info(&vid_cap->v4l2_dev, "subdevice %s registered successfuly\n",
54 isp_info->board_info->type);
59 static void fimc_subdev_unregister(struct fimc_dev *fimc)
61 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
62 struct i2c_client *client;
64 if (vid_cap->input_index < 0)
65 return; /* Subdevice already released or not registered. */
68 v4l2_device_unregister_subdev(vid_cap->sd);
69 client = v4l2_get_subdevdata(vid_cap->sd);
70 i2c_unregister_device(client);
71 i2c_put_adapter(client->adapter);
75 vid_cap->input_index = -1;
79 * fimc_subdev_attach - attach v4l2_subdev to camera host interface
81 * @fimc: FIMC device information
82 * @index: index to the array of available subdevices,
83 * -1 for full array search or non negative value
84 * to select specific subdevice
86 static int fimc_subdev_attach(struct fimc_dev *fimc, int index)
88 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
89 struct s5p_platform_fimc *pdata = fimc->pdata;
90 struct s5p_fimc_isp_info *isp_info;
91 struct v4l2_subdev *sd;
94 for (i = 0; i < FIMC_MAX_CAMIF_CLIENTS; ++i) {
95 isp_info = pdata->isp_info[i];
97 if (!isp_info || (index >= 0 && i != index))
100 sd = fimc_subdev_register(fimc, isp_info);
103 vid_cap->input_index = i;
109 vid_cap->input_index = -1;
111 v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n",
116 static int fimc_isp_subdev_init(struct fimc_dev *fimc, unsigned int index)
118 struct s5p_fimc_isp_info *isp_info;
121 if (index >= FIMC_MAX_CAMIF_CLIENTS)
124 isp_info = fimc->pdata->isp_info[index];
128 if (isp_info->clk_frequency)
129 clk_set_rate(fimc->clock[CLK_CAM], isp_info->clk_frequency);
131 ret = clk_enable(fimc->clock[CLK_CAM]);
135 ret = fimc_subdev_attach(fimc, index);
139 ret = fimc_hw_set_camera_polarity(fimc, isp_info);
143 ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 1);
147 /* enabling power failed so unregister subdev */
148 fimc_subdev_unregister(fimc);
150 v4l2_err(&fimc->vid_cap.v4l2_dev, "ISP initialization failed: %d\n",
157 * At least one buffer on the pending_buf_q queue is required.
158 * Locking: The caller holds fimc->slock spinlock.
160 int fimc_vid_cap_buf_queue(struct fimc_dev *fimc,
161 struct fimc_vid_buffer *fimc_vb)
163 struct fimc_vid_cap *cap = &fimc->vid_cap;
164 struct fimc_ctx *ctx = cap->ctx;
167 BUG_ON(!fimc || !fimc_vb);
169 ret = fimc_prepare_addr(ctx, &fimc_vb->vb, &ctx->d_frame,
174 if (test_bit(ST_CAPT_STREAM, &fimc->state)) {
175 fimc_pending_queue_add(cap, fimc_vb);
177 /* Setup the buffer directly for processing. */
178 int buf_id = (cap->reqbufs_count == 1) ? -1 : cap->buf_index;
179 fimc_hw_set_output_addr(fimc, &fimc_vb->paddr, buf_id);
181 fimc_vb->index = cap->buf_index;
182 active_queue_add(cap, fimc_vb);
184 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
190 static int fimc_stop_capture(struct fimc_dev *fimc)
193 struct fimc_vid_cap *cap;
194 struct fimc_vid_buffer *buf;
196 cap = &fimc->vid_cap;
198 if (!fimc_capture_active(fimc))
201 spin_lock_irqsave(&fimc->slock, flags);
202 set_bit(ST_CAPT_SHUT, &fimc->state);
203 fimc_deactivate_capture(fimc);
204 spin_unlock_irqrestore(&fimc->slock, flags);
206 wait_event_timeout(fimc->irq_queue,
207 test_bit(ST_CAPT_SHUT, &fimc->state),
208 FIMC_SHUTDOWN_TIMEOUT);
210 v4l2_subdev_call(cap->sd, video, s_stream, 0);
212 spin_lock_irqsave(&fimc->slock, flags);
213 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
214 1 << ST_CAPT_STREAM);
216 fimc->vid_cap.active_buf_cnt = 0;
218 /* Release buffers that were enqueued in the driver by videobuf2. */
219 while (!list_empty(&cap->pending_buf_q)) {
220 buf = pending_queue_pop(cap);
221 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
224 while (!list_empty(&cap->active_buf_q)) {
225 buf = active_queue_pop(cap);
226 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
229 spin_unlock_irqrestore(&fimc->slock, flags);
231 dbg("state: 0x%lx", fimc->state);
235 static int start_streaming(struct vb2_queue *q)
237 struct fimc_ctx *ctx = q->drv_priv;
238 struct fimc_dev *fimc = ctx->fimc_dev;
239 struct s5p_fimc_isp_info *isp_info;
242 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
243 if (ret && ret != -ENOIOCTLCMD)
246 ret = fimc_prepare_config(ctx, ctx->state);
250 isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
251 fimc_hw_set_camera_type(fimc, isp_info);
252 fimc_hw_set_camera_source(fimc, isp_info);
253 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
255 if (ctx->state & FIMC_PARAMS) {
256 ret = fimc_set_scaler_info(ctx);
258 err("Scaler setup error");
261 fimc_hw_set_input_path(ctx);
262 fimc_hw_set_scaler(ctx);
263 fimc_hw_set_target_format(ctx);
264 fimc_hw_set_rotation(ctx);
265 fimc_hw_set_effect(ctx);
268 fimc_hw_set_output_path(ctx);
269 fimc_hw_set_out_dma(ctx);
271 INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
272 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
273 fimc->vid_cap.active_buf_cnt = 0;
274 fimc->vid_cap.frame_count = 0;
275 fimc->vid_cap.buf_index = fimc_hw_get_frame_index(fimc);
277 set_bit(ST_CAPT_PEND, &fimc->state);
282 static int stop_streaming(struct vb2_queue *q)
284 struct fimc_ctx *ctx = q->drv_priv;
285 struct fimc_dev *fimc = ctx->fimc_dev;
288 spin_lock_irqsave(&fimc->slock, flags);
289 if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) {
290 spin_unlock_irqrestore(&fimc->slock, flags);
293 spin_unlock_irqrestore(&fimc->slock, flags);
295 return fimc_stop_capture(fimc);
298 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
300 if (!fr || plane >= fr->fmt->memplanes)
303 dbg("%s: w: %d. h: %d. depth[%d]: %d",
304 __func__, fr->width, fr->height, plane, fr->fmt->depth[plane]);
306 return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
310 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
311 unsigned int *num_planes, unsigned long sizes[],
314 struct fimc_ctx *ctx = vq->drv_priv;
315 struct fimc_fmt *fmt = ctx->d_frame.fmt;
321 *num_planes = fmt->memplanes;
323 dbg("%s, buffer count=%d, plane count=%d",
324 __func__, *num_buffers, *num_planes);
326 for (i = 0; i < fmt->memplanes; i++) {
327 sizes[i] = get_plane_size(&ctx->d_frame, i);
328 dbg("plane: %u, plane_size: %lu", i, sizes[i]);
329 allocators[i] = ctx->fimc_dev->alloc_ctx;
335 static int buffer_init(struct vb2_buffer *vb)
341 static int buffer_prepare(struct vb2_buffer *vb)
343 struct vb2_queue *vq = vb->vb2_queue;
344 struct fimc_ctx *ctx = vq->drv_priv;
345 struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
348 if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
351 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
352 unsigned long size = get_plane_size(&ctx->d_frame, i);
354 if (vb2_plane_size(vb, i) < size) {
355 v4l2_err(v4l2_dev, "User buffer too small (%ld < %ld)\n",
356 vb2_plane_size(vb, i), size);
360 vb2_set_plane_payload(vb, i, size);
366 static void buffer_queue(struct vb2_buffer *vb)
368 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
369 struct fimc_dev *fimc = ctx->fimc_dev;
370 struct fimc_vid_buffer *buf
371 = container_of(vb, struct fimc_vid_buffer, vb);
372 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
375 spin_lock_irqsave(&fimc->slock, flags);
376 fimc_vid_cap_buf_queue(fimc, buf);
378 dbg("active_buf_cnt: %d", fimc->vid_cap.active_buf_cnt);
380 if (vid_cap->active_buf_cnt >= vid_cap->reqbufs_count ||
381 vid_cap->active_buf_cnt >= FIMC_MAX_OUT_BUFS) {
382 if (!test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
383 fimc_activate_capture(ctx);
387 spin_unlock_irqrestore(&fimc->slock, flags);
390 static void fimc_lock(struct vb2_queue *vq)
392 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
393 mutex_lock(&ctx->fimc_dev->lock);
396 static void fimc_unlock(struct vb2_queue *vq)
398 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
399 mutex_unlock(&ctx->fimc_dev->lock);
402 static struct vb2_ops fimc_capture_qops = {
403 .queue_setup = queue_setup,
404 .buf_prepare = buffer_prepare,
405 .buf_queue = buffer_queue,
406 .buf_init = buffer_init,
407 .wait_prepare = fimc_unlock,
408 .wait_finish = fimc_lock,
409 .start_streaming = start_streaming,
410 .stop_streaming = stop_streaming,
413 static int fimc_capture_open(struct file *file)
415 struct fimc_dev *fimc = video_drvdata(file);
418 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
420 /* Return if the corresponding video mem2mem node is already opened. */
421 if (fimc_m2m_active(fimc))
424 if (++fimc->vid_cap.refcnt == 1) {
425 ret = fimc_isp_subdev_init(fimc, 0);
427 fimc->vid_cap.refcnt--;
432 file->private_data = fimc->vid_cap.ctx;
437 static int fimc_capture_close(struct file *file)
439 struct fimc_dev *fimc = video_drvdata(file);
441 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
443 if (--fimc->vid_cap.refcnt == 0) {
444 fimc_stop_capture(fimc);
445 vb2_queue_release(&fimc->vid_cap.vbq);
447 v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n");
449 v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
450 clk_disable(fimc->clock[CLK_CAM]);
451 fimc_subdev_unregister(fimc);
457 static unsigned int fimc_capture_poll(struct file *file,
458 struct poll_table_struct *wait)
460 struct fimc_ctx *ctx = file->private_data;
461 struct fimc_dev *fimc = ctx->fimc_dev;
463 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
466 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
468 struct fimc_ctx *ctx = file->private_data;
469 struct fimc_dev *fimc = ctx->fimc_dev;
471 return vb2_mmap(&fimc->vid_cap.vbq, vma);
474 /* video device file operations */
475 static const struct v4l2_file_operations fimc_capture_fops = {
476 .owner = THIS_MODULE,
477 .open = fimc_capture_open,
478 .release = fimc_capture_close,
479 .poll = fimc_capture_poll,
480 .unlocked_ioctl = video_ioctl2,
481 .mmap = fimc_capture_mmap,
484 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
485 struct v4l2_capability *cap)
487 struct fimc_ctx *ctx = file->private_data;
488 struct fimc_dev *fimc = ctx->fimc_dev;
490 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
491 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
492 cap->bus_info[0] = 0;
493 cap->version = KERNEL_VERSION(1, 0, 0);
494 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
495 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
500 /* Synchronize formats of the camera interface input and attached sensor. */
501 static int sync_capture_fmt(struct fimc_ctx *ctx)
503 struct fimc_frame *frame = &ctx->s_frame;
504 struct fimc_dev *fimc = ctx->fimc_dev;
505 struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
508 fmt->width = ctx->d_frame.o_width;
509 fmt->height = ctx->d_frame.o_height;
511 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
512 if (ret == -ENOIOCTLCMD) {
513 err("s_mbus_fmt failed");
516 dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
518 frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
520 err("fimc source format not found\n");
524 frame->f_width = fmt->width;
525 frame->f_height = fmt->height;
526 frame->width = fmt->width;
527 frame->height = fmt->height;
528 frame->o_width = fmt->width;
529 frame->o_height = fmt->height;
536 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
537 struct v4l2_format *f)
539 struct fimc_ctx *ctx = priv;
540 struct fimc_dev *fimc = ctx->fimc_dev;
541 struct fimc_frame *frame;
542 struct v4l2_pix_format_mplane *pix;
546 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
549 ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
553 if (vb2_is_streaming(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
556 frame = &ctx->d_frame;
558 pix = &f->fmt.pix_mp;
559 frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
561 err("fimc target format not found\n");
565 for (i = 0; i < frame->fmt->colplanes; i++)
566 frame->payload[i] = pix->plane_fmt[i].bytesperline * pix->height;
568 /* Output DMA frame pixel size and offsets. */
569 frame->f_width = pix->plane_fmt[0].bytesperline * 8
570 / frame->fmt->depth[0];
571 frame->f_height = pix->height;
572 frame->width = pix->width;
573 frame->height = pix->height;
574 frame->o_width = pix->width;
575 frame->o_height = pix->height;
579 ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
581 ret = sync_capture_fmt(ctx);
585 static int fimc_cap_enum_input(struct file *file, void *priv,
586 struct v4l2_input *i)
588 struct fimc_ctx *ctx = priv;
589 struct s5p_platform_fimc *pldata = ctx->fimc_dev->pdata;
590 struct s5p_fimc_isp_info *isp_info;
592 if (i->index >= FIMC_MAX_CAMIF_CLIENTS)
595 isp_info = pldata->isp_info[i->index];
596 if (isp_info == NULL)
599 i->type = V4L2_INPUT_TYPE_CAMERA;
600 strncpy(i->name, isp_info->board_info->type, 32);
604 static int fimc_cap_s_input(struct file *file, void *priv,
607 struct fimc_ctx *ctx = priv;
608 struct fimc_dev *fimc = ctx->fimc_dev;
609 struct s5p_platform_fimc *pdata = fimc->pdata;
611 if (fimc_capture_active(ctx->fimc_dev))
614 if (i >= FIMC_MAX_CAMIF_CLIENTS || !pdata->isp_info[i])
618 if (fimc->vid_cap.sd) {
619 int ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
621 err("s_power failed: %d", ret);
623 clk_disable(fimc->clock[CLK_CAM]);
626 /* Release the attached sensor subdevice. */
627 fimc_subdev_unregister(fimc);
629 return fimc_isp_subdev_init(fimc, i);
632 static int fimc_cap_g_input(struct file *file, void *priv,
635 struct fimc_ctx *ctx = priv;
636 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
638 *i = cap->input_index;
642 static int fimc_cap_streamon(struct file *file, void *priv,
643 enum v4l2_buf_type type)
645 struct fimc_ctx *ctx = priv;
646 struct fimc_dev *fimc = ctx->fimc_dev;
648 if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
651 if (!(ctx->state & FIMC_DST_FMT)) {
652 v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
656 return vb2_streamon(&fimc->vid_cap.vbq, type);
659 static int fimc_cap_streamoff(struct file *file, void *priv,
660 enum v4l2_buf_type type)
662 struct fimc_ctx *ctx = priv;
663 struct fimc_dev *fimc = ctx->fimc_dev;
665 return vb2_streamoff(&fimc->vid_cap.vbq, type);
668 static int fimc_cap_reqbufs(struct file *file, void *priv,
669 struct v4l2_requestbuffers *reqbufs)
671 struct fimc_ctx *ctx = priv;
672 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
676 ret = vb2_reqbufs(&cap->vbq, reqbufs);
678 cap->reqbufs_count = reqbufs->count;
683 static int fimc_cap_querybuf(struct file *file, void *priv,
684 struct v4l2_buffer *buf)
686 struct fimc_ctx *ctx = priv;
687 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
689 return vb2_querybuf(&cap->vbq, buf);
692 static int fimc_cap_qbuf(struct file *file, void *priv,
693 struct v4l2_buffer *buf)
695 struct fimc_ctx *ctx = priv;
696 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
697 return vb2_qbuf(&cap->vbq, buf);
700 static int fimc_cap_dqbuf(struct file *file, void *priv,
701 struct v4l2_buffer *buf)
703 struct fimc_ctx *ctx = priv;
704 return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
705 file->f_flags & O_NONBLOCK);
708 static int fimc_cap_s_ctrl(struct file *file, void *priv,
709 struct v4l2_control *ctrl)
711 struct fimc_ctx *ctx = priv;
714 /* Allow any controls but 90/270 rotation while streaming */
715 if (!fimc_capture_active(ctx->fimc_dev) ||
716 ctrl->id != V4L2_CID_ROTATE ||
717 (ctrl->value != 90 && ctrl->value != 270)) {
718 ret = check_ctrl_val(ctx, ctrl);
720 ret = fimc_s_ctrl(ctx, ctrl);
722 ctx->state |= FIMC_PARAMS;
726 ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
731 static int fimc_cap_cropcap(struct file *file, void *fh,
732 struct v4l2_cropcap *cr)
734 struct fimc_frame *f;
735 struct fimc_ctx *ctx = fh;
737 if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
744 cr->bounds.width = f->o_width;
745 cr->bounds.height = f->o_height;
746 cr->defrect = cr->bounds;
751 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
753 struct fimc_frame *f;
754 struct fimc_ctx *ctx = file->private_data;
758 cr->c.left = f->offs_h;
759 cr->c.top = f->offs_v;
760 cr->c.width = f->width;
761 cr->c.height = f->height;
766 static int fimc_cap_s_crop(struct file *file, void *fh,
767 struct v4l2_crop *cr)
769 struct fimc_frame *f;
770 struct fimc_ctx *ctx = file->private_data;
771 struct fimc_dev *fimc = ctx->fimc_dev;
774 if (fimc_capture_active(fimc))
777 ret = fimc_try_crop(ctx, cr);
781 if (!(ctx->state & FIMC_DST_FMT)) {
782 v4l2_err(&fimc->vid_cap.v4l2_dev,
783 "Capture color format not set\n");
784 return -EINVAL; /* TODO: make sure this is the right value */
788 /* Check for the pixel scaling ratio when cropping input image. */
789 ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
791 v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range");
795 f->offs_h = cr->c.left;
796 f->offs_v = cr->c.top;
797 f->width = cr->c.width;
798 f->height = cr->c.height;
804 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
805 .vidioc_querycap = fimc_vidioc_querycap_capture,
807 .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
808 .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane,
809 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
810 .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane,
812 .vidioc_reqbufs = fimc_cap_reqbufs,
813 .vidioc_querybuf = fimc_cap_querybuf,
815 .vidioc_qbuf = fimc_cap_qbuf,
816 .vidioc_dqbuf = fimc_cap_dqbuf,
818 .vidioc_streamon = fimc_cap_streamon,
819 .vidioc_streamoff = fimc_cap_streamoff,
821 .vidioc_queryctrl = fimc_vidioc_queryctrl,
822 .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
823 .vidioc_s_ctrl = fimc_cap_s_ctrl,
825 .vidioc_g_crop = fimc_cap_g_crop,
826 .vidioc_s_crop = fimc_cap_s_crop,
827 .vidioc_cropcap = fimc_cap_cropcap,
829 .vidioc_enum_input = fimc_cap_enum_input,
830 .vidioc_s_input = fimc_cap_s_input,
831 .vidioc_g_input = fimc_cap_g_input,
834 /* fimc->lock must be already initialized */
835 int fimc_register_capture_device(struct fimc_dev *fimc)
837 struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
838 struct video_device *vfd;
839 struct fimc_vid_cap *vid_cap;
840 struct fimc_ctx *ctx;
841 struct v4l2_format f;
842 struct fimc_frame *fr;
846 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
850 ctx->fimc_dev = fimc;
851 ctx->in_path = FIMC_CAMERA;
852 ctx->out_path = FIMC_DMA;
853 ctx->state = FIMC_CTX_CAP;
855 /* Default format of the output frames */
856 f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
858 fr->fmt = find_format(&f, FMT_FLAGS_M2M);
859 fr->width = fr->f_width = fr->o_width = 640;
860 fr->height = fr->f_height = fr->o_height = 480;
862 if (!v4l2_dev->name[0])
863 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
864 "%s.capture", dev_name(&fimc->pdev->dev));
866 ret = v4l2_device_register(NULL, v4l2_dev);
870 vfd = video_device_alloc();
872 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
876 snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
877 dev_name(&fimc->pdev->dev));
879 vfd->fops = &fimc_capture_fops;
880 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
882 vfd->release = video_device_release;
883 vfd->lock = &fimc->lock;
884 video_set_drvdata(vfd, fimc);
886 vid_cap = &fimc->vid_cap;
888 vid_cap->active_buf_cnt = 0;
889 vid_cap->reqbufs_count = 0;
891 /* Default color format for image sensor */
892 vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
894 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
895 INIT_LIST_HEAD(&vid_cap->active_buf_q);
896 spin_lock_init(&ctx->slock);
899 q = &fimc->vid_cap.vbq;
900 memset(q, 0, sizeof(*q));
901 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
902 q->io_modes = VB2_MMAP | VB2_USERPTR;
903 q->drv_priv = fimc->vid_cap.ctx;
904 q->ops = &fimc_capture_qops;
905 q->mem_ops = &vb2_dma_contig_memops;
906 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
910 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
912 v4l2_err(v4l2_dev, "Failed to register video device\n");
917 "FIMC capture driver registered as /dev/video%d\n",
923 video_device_release(vfd);
925 v4l2_device_unregister(v4l2_dev);
927 dev_err(&fimc->pdev->dev, "failed to install\n");
931 void fimc_unregister_capture_device(struct fimc_dev *fimc)
933 struct fimc_vid_cap *capture = &fimc->vid_cap;
936 video_unregister_device(capture->vfd);