Merge branch 'v6v7' into devel
[linux/fpc-iii.git] / drivers / media / video / s5p-fimc / fimc-capture.c
blob2f500809f53d9b418dd2d3117ce44ef7706d0287
1 /*
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/videobuf-core.h>
31 #include <media/videobuf-dma-contig.h>
33 #include "fimc-core.h"
35 static struct v4l2_subdev *fimc_subdev_register(struct fimc_dev *fimc,
36 struct s3c_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);
43 if (!i2c_adap)
44 return ERR_PTR(-ENOMEM);
46 sd = v4l2_i2c_new_subdev_board(&vid_cap->v4l2_dev, i2c_adap,
47 isp_info->board_info, NULL);
48 if (!sd) {
49 v4l2_err(&vid_cap->v4l2_dev, "failed to acquire subdev\n");
50 return NULL;
53 v4l2_info(&vid_cap->v4l2_dev, "subdevice %s registered successfuly\n",
54 isp_info->board_info->type);
56 return sd;
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. */
67 if (vid_cap->sd) {
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);
72 vid_cap->sd = NULL;
75 vid_cap->input_index = -1;
78 /**
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 s3c_platform_fimc *pdata = fimc->pdata;
90 struct s3c_fimc_isp_info *isp_info;
91 struct v4l2_subdev *sd;
92 int i;
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))
98 continue;
100 sd = fimc_subdev_register(fimc, isp_info);
101 if (sd) {
102 vid_cap->sd = sd;
103 vid_cap->input_index = i;
105 return 0;
109 vid_cap->input_index = -1;
110 vid_cap->sd = NULL;
111 v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n",
112 fimc->id);
113 return -ENODEV;
116 static int fimc_isp_subdev_init(struct fimc_dev *fimc, int index)
118 struct s3c_fimc_isp_info *isp_info;
119 int ret;
121 ret = fimc_subdev_attach(fimc, index);
122 if (ret)
123 return ret;
125 isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
126 ret = fimc_hw_set_camera_polarity(fimc, isp_info);
127 if (!ret) {
128 ret = v4l2_subdev_call(fimc->vid_cap.sd, core,
129 s_power, 1);
130 if (!ret)
131 return ret;
134 fimc_subdev_unregister(fimc);
135 err("ISP initialization failed: %d", ret);
136 return ret;
140 * At least one buffer on the pending_buf_q queue is required.
141 * Locking: The caller holds fimc->slock spinlock.
143 int fimc_vid_cap_buf_queue(struct fimc_dev *fimc,
144 struct fimc_vid_buffer *fimc_vb)
146 struct fimc_vid_cap *cap = &fimc->vid_cap;
147 struct fimc_ctx *ctx = cap->ctx;
148 int ret = 0;
150 BUG_ON(!fimc || !fimc_vb);
152 ret = fimc_prepare_addr(ctx, fimc_vb, &ctx->d_frame,
153 &fimc_vb->paddr);
154 if (ret)
155 return ret;
157 if (test_bit(ST_CAPT_STREAM, &fimc->state)) {
158 fimc_pending_queue_add(cap, fimc_vb);
159 } else {
160 /* Setup the buffer directly for processing. */
161 int buf_id = (cap->reqbufs_count == 1) ? -1 : cap->buf_index;
162 fimc_hw_set_output_addr(fimc, &fimc_vb->paddr, buf_id);
164 fimc_vb->index = cap->buf_index;
165 active_queue_add(cap, fimc_vb);
167 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
168 cap->buf_index = 0;
170 return ret;
173 static int fimc_stop_capture(struct fimc_dev *fimc)
175 unsigned long flags;
176 struct fimc_vid_cap *cap;
177 int ret;
179 cap = &fimc->vid_cap;
181 if (!fimc_capture_active(fimc))
182 return 0;
184 spin_lock_irqsave(&fimc->slock, flags);
185 set_bit(ST_CAPT_SHUT, &fimc->state);
186 fimc_deactivate_capture(fimc);
187 spin_unlock_irqrestore(&fimc->slock, flags);
189 wait_event_timeout(fimc->irq_queue,
190 test_bit(ST_CAPT_SHUT, &fimc->state),
191 FIMC_SHUTDOWN_TIMEOUT);
193 ret = v4l2_subdev_call(cap->sd, video, s_stream, 0);
194 if (ret)
195 v4l2_err(&fimc->vid_cap.v4l2_dev, "s_stream(0) failed\n");
197 spin_lock_irqsave(&fimc->slock, flags);
198 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
199 1 << ST_CAPT_STREAM);
201 fimc->vid_cap.active_buf_cnt = 0;
202 spin_unlock_irqrestore(&fimc->slock, flags);
204 dbg("state: 0x%lx", fimc->state);
205 return 0;
208 static int fimc_capture_open(struct file *file)
210 struct fimc_dev *fimc = video_drvdata(file);
211 int ret = 0;
213 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
215 /* Return if the corresponding video mem2mem node is already opened. */
216 if (fimc_m2m_active(fimc))
217 return -EBUSY;
219 if (mutex_lock_interruptible(&fimc->lock))
220 return -ERESTARTSYS;
222 if (++fimc->vid_cap.refcnt == 1) {
223 ret = fimc_isp_subdev_init(fimc, -1);
224 if (ret) {
225 fimc->vid_cap.refcnt--;
226 ret = -EIO;
230 file->private_data = fimc->vid_cap.ctx;
232 mutex_unlock(&fimc->lock);
233 return ret;
236 static int fimc_capture_close(struct file *file)
238 struct fimc_dev *fimc = video_drvdata(file);
240 if (mutex_lock_interruptible(&fimc->lock))
241 return -ERESTARTSYS;
243 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
245 if (--fimc->vid_cap.refcnt == 0) {
246 fimc_stop_capture(fimc);
248 videobuf_stop(&fimc->vid_cap.vbq);
249 videobuf_mmap_free(&fimc->vid_cap.vbq);
251 v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n");
252 v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
253 fimc_subdev_unregister(fimc);
256 mutex_unlock(&fimc->lock);
257 return 0;
260 static unsigned int fimc_capture_poll(struct file *file,
261 struct poll_table_struct *wait)
263 struct fimc_ctx *ctx = file->private_data;
264 struct fimc_dev *fimc = ctx->fimc_dev;
265 struct fimc_vid_cap *cap = &fimc->vid_cap;
266 int ret;
268 if (mutex_lock_interruptible(&fimc->lock))
269 return POLLERR;
271 ret = videobuf_poll_stream(file, &cap->vbq, wait);
272 mutex_unlock(&fimc->lock);
274 return ret;
277 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
279 struct fimc_ctx *ctx = file->private_data;
280 struct fimc_dev *fimc = ctx->fimc_dev;
281 struct fimc_vid_cap *cap = &fimc->vid_cap;
282 int ret;
284 if (mutex_lock_interruptible(&fimc->lock))
285 return -ERESTARTSYS;
287 ret = videobuf_mmap_mapper(&cap->vbq, vma);
288 mutex_unlock(&fimc->lock);
290 return ret;
293 /* video device file operations */
294 static const struct v4l2_file_operations fimc_capture_fops = {
295 .owner = THIS_MODULE,
296 .open = fimc_capture_open,
297 .release = fimc_capture_close,
298 .poll = fimc_capture_poll,
299 .unlocked_ioctl = video_ioctl2,
300 .mmap = fimc_capture_mmap,
303 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
304 struct v4l2_capability *cap)
306 struct fimc_ctx *ctx = file->private_data;
307 struct fimc_dev *fimc = ctx->fimc_dev;
309 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
310 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
311 cap->bus_info[0] = 0;
312 cap->version = KERNEL_VERSION(1, 0, 0);
313 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
315 return 0;
318 /* Synchronize formats of the camera interface input and attached sensor. */
319 static int sync_capture_fmt(struct fimc_ctx *ctx)
321 struct fimc_frame *frame = &ctx->s_frame;
322 struct fimc_dev *fimc = ctx->fimc_dev;
323 struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
324 int ret;
326 fmt->width = ctx->d_frame.o_width;
327 fmt->height = ctx->d_frame.o_height;
329 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
330 if (ret == -ENOIOCTLCMD) {
331 err("s_mbus_fmt failed");
332 return ret;
334 dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
336 frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
337 if (!frame->fmt) {
338 err("fimc source format not found\n");
339 return -EINVAL;
342 frame->f_width = fmt->width;
343 frame->f_height = fmt->height;
344 frame->width = fmt->width;
345 frame->height = fmt->height;
346 frame->o_width = fmt->width;
347 frame->o_height = fmt->height;
348 frame->offs_h = 0;
349 frame->offs_v = 0;
351 return 0;
354 static int fimc_cap_s_fmt(struct file *file, void *priv,
355 struct v4l2_format *f)
357 struct fimc_ctx *ctx = priv;
358 struct fimc_dev *fimc = ctx->fimc_dev;
359 struct fimc_frame *frame;
360 struct v4l2_pix_format *pix;
361 int ret;
363 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
364 return -EINVAL;
366 ret = fimc_vidioc_try_fmt(file, priv, f);
367 if (ret)
368 return ret;
370 if (mutex_lock_interruptible(&fimc->lock))
371 return -ERESTARTSYS;
373 if (fimc_capture_active(fimc)) {
374 ret = -EBUSY;
375 goto sf_unlock;
378 frame = &ctx->d_frame;
380 pix = &f->fmt.pix;
381 frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
382 if (!frame->fmt) {
383 err("fimc target format not found\n");
384 ret = -EINVAL;
385 goto sf_unlock;
388 /* Output DMA frame pixel size and offsets. */
389 frame->f_width = pix->bytesperline * 8 / frame->fmt->depth;
390 frame->f_height = pix->height;
391 frame->width = pix->width;
392 frame->height = pix->height;
393 frame->o_width = pix->width;
394 frame->o_height = pix->height;
395 frame->size = (pix->width * pix->height * frame->fmt->depth) >> 3;
396 frame->offs_h = 0;
397 frame->offs_v = 0;
399 ret = sync_capture_fmt(ctx);
401 ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
403 sf_unlock:
404 mutex_unlock(&fimc->lock);
405 return ret;
408 static int fimc_cap_enum_input(struct file *file, void *priv,
409 struct v4l2_input *i)
411 struct fimc_ctx *ctx = priv;
412 struct s3c_platform_fimc *pldata = ctx->fimc_dev->pdata;
413 struct s3c_fimc_isp_info *isp_info;
415 if (i->index >= FIMC_MAX_CAMIF_CLIENTS)
416 return -EINVAL;
418 isp_info = pldata->isp_info[i->index];
419 if (isp_info == NULL)
420 return -EINVAL;
422 i->type = V4L2_INPUT_TYPE_CAMERA;
423 strncpy(i->name, isp_info->board_info->type, 32);
424 return 0;
427 static int fimc_cap_s_input(struct file *file, void *priv,
428 unsigned int i)
430 struct fimc_ctx *ctx = priv;
431 struct fimc_dev *fimc = ctx->fimc_dev;
432 struct s3c_platform_fimc *pdata = fimc->pdata;
433 int ret;
435 if (fimc_capture_active(ctx->fimc_dev))
436 return -EBUSY;
438 if (mutex_lock_interruptible(&fimc->lock))
439 return -ERESTARTSYS;
441 if (i >= FIMC_MAX_CAMIF_CLIENTS || !pdata->isp_info[i]) {
442 ret = -EINVAL;
443 goto si_unlock;
446 if (fimc->vid_cap.sd) {
447 ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
448 if (ret)
449 err("s_power failed: %d", ret);
452 /* Release the attached sensor subdevice. */
453 fimc_subdev_unregister(fimc);
455 ret = fimc_isp_subdev_init(fimc, i);
457 si_unlock:
458 mutex_unlock(&fimc->lock);
459 return ret;
462 static int fimc_cap_g_input(struct file *file, void *priv,
463 unsigned int *i)
465 struct fimc_ctx *ctx = priv;
466 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
468 *i = cap->input_index;
469 return 0;
472 static int fimc_cap_streamon(struct file *file, void *priv,
473 enum v4l2_buf_type type)
475 struct s3c_fimc_isp_info *isp_info;
476 struct fimc_ctx *ctx = priv;
477 struct fimc_dev *fimc = ctx->fimc_dev;
478 int ret = -EBUSY;
480 if (mutex_lock_interruptible(&fimc->lock))
481 return -ERESTARTSYS;
483 if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
484 goto s_unlock;
486 if (!(ctx->state & FIMC_DST_FMT)) {
487 v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
488 ret = -EINVAL;
489 goto s_unlock;
492 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
493 if (ret && ret != -ENOIOCTLCMD)
494 goto s_unlock;
496 ret = fimc_prepare_config(ctx, ctx->state);
497 if (ret)
498 goto s_unlock;
500 isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
501 fimc_hw_set_camera_type(fimc, isp_info);
502 fimc_hw_set_camera_source(fimc, isp_info);
503 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
505 if (ctx->state & FIMC_PARAMS) {
506 ret = fimc_set_scaler_info(ctx);
507 if (ret) {
508 err("Scaler setup error");
509 goto s_unlock;
511 fimc_hw_set_input_path(ctx);
512 fimc_hw_set_scaler(ctx);
513 fimc_hw_set_target_format(ctx);
514 fimc_hw_set_rotation(ctx);
515 fimc_hw_set_effect(ctx);
518 fimc_hw_set_output_path(ctx);
519 fimc_hw_set_out_dma(ctx);
521 INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
522 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
523 fimc->vid_cap.active_buf_cnt = 0;
524 fimc->vid_cap.frame_count = 0;
525 fimc->vid_cap.buf_index = fimc_hw_get_frame_index(fimc);
527 set_bit(ST_CAPT_PEND, &fimc->state);
528 ret = videobuf_streamon(&fimc->vid_cap.vbq);
530 s_unlock:
531 mutex_unlock(&fimc->lock);
532 return ret;
535 static int fimc_cap_streamoff(struct file *file, void *priv,
536 enum v4l2_buf_type type)
538 struct fimc_ctx *ctx = priv;
539 struct fimc_dev *fimc = ctx->fimc_dev;
540 struct fimc_vid_cap *cap = &fimc->vid_cap;
541 unsigned long flags;
542 int ret;
544 spin_lock_irqsave(&fimc->slock, flags);
545 if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) {
546 spin_unlock_irqrestore(&fimc->slock, flags);
547 dbg("state: 0x%lx", fimc->state);
548 return -EINVAL;
550 spin_unlock_irqrestore(&fimc->slock, flags);
552 if (mutex_lock_interruptible(&fimc->lock))
553 return -ERESTARTSYS;
555 fimc_stop_capture(fimc);
556 ret = videobuf_streamoff(&cap->vbq);
557 mutex_unlock(&fimc->lock);
558 return ret;
561 static int fimc_cap_reqbufs(struct file *file, void *priv,
562 struct v4l2_requestbuffers *reqbufs)
564 struct fimc_ctx *ctx = priv;
565 struct fimc_dev *fimc = ctx->fimc_dev;
566 struct fimc_vid_cap *cap = &fimc->vid_cap;
567 int ret;
569 if (fimc_capture_active(ctx->fimc_dev))
570 return -EBUSY;
572 if (mutex_lock_interruptible(&fimc->lock))
573 return -ERESTARTSYS;
575 ret = videobuf_reqbufs(&cap->vbq, reqbufs);
576 if (!ret)
577 cap->reqbufs_count = reqbufs->count;
579 mutex_unlock(&fimc->lock);
580 return ret;
583 static int fimc_cap_querybuf(struct file *file, void *priv,
584 struct v4l2_buffer *buf)
586 struct fimc_ctx *ctx = priv;
587 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
589 if (fimc_capture_active(ctx->fimc_dev))
590 return -EBUSY;
592 return videobuf_querybuf(&cap->vbq, buf);
595 static int fimc_cap_qbuf(struct file *file, void *priv,
596 struct v4l2_buffer *buf)
598 struct fimc_ctx *ctx = priv;
599 struct fimc_dev *fimc = ctx->fimc_dev;
600 struct fimc_vid_cap *cap = &fimc->vid_cap;
601 int ret;
603 if (mutex_lock_interruptible(&fimc->lock))
604 return -ERESTARTSYS;
606 ret = videobuf_qbuf(&cap->vbq, buf);
608 mutex_unlock(&fimc->lock);
609 return ret;
612 static int fimc_cap_dqbuf(struct file *file, void *priv,
613 struct v4l2_buffer *buf)
615 struct fimc_ctx *ctx = priv;
616 int ret;
618 if (mutex_lock_interruptible(&ctx->fimc_dev->lock))
619 return -ERESTARTSYS;
621 ret = videobuf_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
622 file->f_flags & O_NONBLOCK);
624 mutex_unlock(&ctx->fimc_dev->lock);
625 return ret;
628 static int fimc_cap_s_ctrl(struct file *file, void *priv,
629 struct v4l2_control *ctrl)
631 struct fimc_ctx *ctx = priv;
632 int ret = -EINVAL;
634 if (mutex_lock_interruptible(&ctx->fimc_dev->lock))
635 return -ERESTARTSYS;
637 /* Allow any controls but 90/270 rotation while streaming */
638 if (!fimc_capture_active(ctx->fimc_dev) ||
639 ctrl->id != V4L2_CID_ROTATE ||
640 (ctrl->value != 90 && ctrl->value != 270)) {
641 ret = check_ctrl_val(ctx, ctrl);
642 if (!ret) {
643 ret = fimc_s_ctrl(ctx, ctrl);
644 if (!ret)
645 ctx->state |= FIMC_PARAMS;
648 if (ret == -EINVAL)
649 ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
650 core, s_ctrl, ctrl);
652 mutex_unlock(&ctx->fimc_dev->lock);
653 return ret;
656 static int fimc_cap_cropcap(struct file *file, void *fh,
657 struct v4l2_cropcap *cr)
659 struct fimc_frame *f;
660 struct fimc_ctx *ctx = fh;
661 struct fimc_dev *fimc = ctx->fimc_dev;
663 if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
664 return -EINVAL;
666 if (mutex_lock_interruptible(&fimc->lock))
667 return -ERESTARTSYS;
669 f = &ctx->s_frame;
670 cr->bounds.left = 0;
671 cr->bounds.top = 0;
672 cr->bounds.width = f->o_width;
673 cr->bounds.height = f->o_height;
674 cr->defrect = cr->bounds;
676 mutex_unlock(&fimc->lock);
677 return 0;
680 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
682 struct fimc_frame *f;
683 struct fimc_ctx *ctx = file->private_data;
684 struct fimc_dev *fimc = ctx->fimc_dev;
687 if (mutex_lock_interruptible(&fimc->lock))
688 return -ERESTARTSYS;
690 f = &ctx->s_frame;
691 cr->c.left = f->offs_h;
692 cr->c.top = f->offs_v;
693 cr->c.width = f->width;
694 cr->c.height = f->height;
696 mutex_unlock(&fimc->lock);
697 return 0;
700 static int fimc_cap_s_crop(struct file *file, void *fh,
701 struct v4l2_crop *cr)
703 struct fimc_frame *f;
704 struct fimc_ctx *ctx = file->private_data;
705 struct fimc_dev *fimc = ctx->fimc_dev;
706 int ret = -EINVAL;
708 if (fimc_capture_active(fimc))
709 return -EBUSY;
711 ret = fimc_try_crop(ctx, cr);
712 if (ret)
713 return ret;
715 if (mutex_lock_interruptible(&fimc->lock))
716 return -ERESTARTSYS;
718 if (!(ctx->state & FIMC_DST_FMT)) {
719 v4l2_err(&fimc->vid_cap.v4l2_dev,
720 "Capture color format not set\n");
721 goto sc_unlock;
724 f = &ctx->s_frame;
725 /* Check for the pixel scaling ratio when cropping input image. */
726 ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
727 if (ret) {
728 v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range");
729 } else {
730 ret = 0;
731 f->offs_h = cr->c.left;
732 f->offs_v = cr->c.top;
733 f->width = cr->c.width;
734 f->height = cr->c.height;
737 sc_unlock:
738 mutex_unlock(&fimc->lock);
739 return ret;
743 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
744 .vidioc_querycap = fimc_vidioc_querycap_capture,
746 .vidioc_enum_fmt_vid_cap = fimc_vidioc_enum_fmt,
747 .vidioc_try_fmt_vid_cap = fimc_vidioc_try_fmt,
748 .vidioc_s_fmt_vid_cap = fimc_cap_s_fmt,
749 .vidioc_g_fmt_vid_cap = fimc_vidioc_g_fmt,
751 .vidioc_reqbufs = fimc_cap_reqbufs,
752 .vidioc_querybuf = fimc_cap_querybuf,
754 .vidioc_qbuf = fimc_cap_qbuf,
755 .vidioc_dqbuf = fimc_cap_dqbuf,
757 .vidioc_streamon = fimc_cap_streamon,
758 .vidioc_streamoff = fimc_cap_streamoff,
760 .vidioc_queryctrl = fimc_vidioc_queryctrl,
761 .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
762 .vidioc_s_ctrl = fimc_cap_s_ctrl,
764 .vidioc_g_crop = fimc_cap_g_crop,
765 .vidioc_s_crop = fimc_cap_s_crop,
766 .vidioc_cropcap = fimc_cap_cropcap,
768 .vidioc_enum_input = fimc_cap_enum_input,
769 .vidioc_s_input = fimc_cap_s_input,
770 .vidioc_g_input = fimc_cap_g_input,
773 int fimc_register_capture_device(struct fimc_dev *fimc)
775 struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
776 struct video_device *vfd;
777 struct fimc_vid_cap *vid_cap;
778 struct fimc_ctx *ctx;
779 struct v4l2_format f;
780 int ret;
782 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
783 if (!ctx)
784 return -ENOMEM;
786 ctx->fimc_dev = fimc;
787 ctx->in_path = FIMC_CAMERA;
788 ctx->out_path = FIMC_DMA;
789 ctx->state = FIMC_CTX_CAP;
791 f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
792 ctx->d_frame.fmt = find_format(&f, FMT_FLAGS_M2M);
794 if (!v4l2_dev->name[0])
795 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
796 "%s.capture", dev_name(&fimc->pdev->dev));
798 ret = v4l2_device_register(NULL, v4l2_dev);
799 if (ret)
800 goto err_info;
802 vfd = video_device_alloc();
803 if (!vfd) {
804 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
805 goto err_v4l2_reg;
808 snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
809 dev_name(&fimc->pdev->dev));
811 vfd->fops = &fimc_capture_fops;
812 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
813 vfd->minor = -1;
814 vfd->release = video_device_release;
815 video_set_drvdata(vfd, fimc);
817 vid_cap = &fimc->vid_cap;
818 vid_cap->vfd = vfd;
819 vid_cap->active_buf_cnt = 0;
820 vid_cap->reqbufs_count = 0;
821 vid_cap->refcnt = 0;
822 /* The default color format for image sensor. */
823 vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
825 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
826 INIT_LIST_HEAD(&vid_cap->active_buf_q);
827 spin_lock_init(&ctx->slock);
828 vid_cap->ctx = ctx;
830 videobuf_queue_dma_contig_init(&vid_cap->vbq, &fimc_qops,
831 vid_cap->v4l2_dev.dev, &fimc->irqlock,
832 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
833 sizeof(struct fimc_vid_buffer), (void *)ctx, NULL);
835 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
836 if (ret) {
837 v4l2_err(v4l2_dev, "Failed to register video device\n");
838 goto err_vd_reg;
841 v4l2_info(v4l2_dev,
842 "FIMC capture driver registered as /dev/video%d\n",
843 vfd->num);
845 return 0;
847 err_vd_reg:
848 video_device_release(vfd);
849 err_v4l2_reg:
850 v4l2_device_unregister(v4l2_dev);
851 err_info:
852 dev_err(&fimc->pdev->dev, "failed to install\n");
853 return ret;
856 void fimc_unregister_capture_device(struct fimc_dev *fimc)
858 struct fimc_vid_cap *capture = &fimc->vid_cap;
860 if (capture->vfd)
861 video_unregister_device(capture->vfd);
863 kfree(capture->ctx);