Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / video / s5p-fimc / fimc-capture.c
blob0d730e55605d02423a5f34aea359f1e5202416d0
1 /*
2 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
4 * Copyright (C) 2010 - 2011 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/types.h>
15 #include <linux/errno.h>
16 #include <linux/bug.h>
17 #include <linux/interrupt.h>
18 #include <linux/device.h>
19 #include <linux/platform_device.h>
20 #include <linux/list.h>
21 #include <linux/slab.h>
22 #include <linux/clk.h>
23 #include <linux/i2c.h>
25 #include <linux/videodev2.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/v4l2-mem2mem.h>
29 #include <media/videobuf2-core.h>
30 #include <media/videobuf2-dma-contig.h>
32 #include "fimc-core.h"
34 static struct v4l2_subdev *fimc_subdev_register(struct fimc_dev *fimc,
35 struct s5p_fimc_isp_info *isp_info)
37 struct i2c_adapter *i2c_adap;
38 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
39 struct v4l2_subdev *sd = NULL;
41 i2c_adap = i2c_get_adapter(isp_info->i2c_bus_num);
42 if (!i2c_adap)
43 return ERR_PTR(-ENOMEM);
45 sd = v4l2_i2c_new_subdev_board(&vid_cap->v4l2_dev, i2c_adap,
46 isp_info->board_info, NULL);
47 if (!sd) {
48 v4l2_err(&vid_cap->v4l2_dev, "failed to acquire subdev\n");
49 return NULL;
52 v4l2_info(&vid_cap->v4l2_dev, "subdevice %s registered successfuly\n",
53 isp_info->board_info->type);
55 return sd;
58 static void fimc_subdev_unregister(struct fimc_dev *fimc)
60 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
61 struct i2c_client *client;
63 if (vid_cap->input_index < 0)
64 return; /* Subdevice already released or not registered. */
66 if (vid_cap->sd) {
67 v4l2_device_unregister_subdev(vid_cap->sd);
68 client = v4l2_get_subdevdata(vid_cap->sd);
69 i2c_unregister_device(client);
70 i2c_put_adapter(client->adapter);
71 vid_cap->sd = NULL;
74 vid_cap->input_index = -1;
77 /**
78 * fimc_subdev_attach - attach v4l2_subdev to camera host interface
80 * @fimc: FIMC device information
81 * @index: index to the array of available subdevices,
82 * -1 for full array search or non negative value
83 * to select specific subdevice
85 static int fimc_subdev_attach(struct fimc_dev *fimc, int index)
87 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
88 struct s5p_platform_fimc *pdata = fimc->pdata;
89 struct s5p_fimc_isp_info *isp_info;
90 struct v4l2_subdev *sd;
91 int i;
93 for (i = 0; i < pdata->num_clients; ++i) {
94 isp_info = &pdata->isp_info[i];
96 if (index >= 0 && i != index)
97 continue;
99 sd = fimc_subdev_register(fimc, isp_info);
100 if (!IS_ERR_OR_NULL(sd)) {
101 vid_cap->sd = sd;
102 vid_cap->input_index = i;
104 return 0;
108 vid_cap->input_index = -1;
109 vid_cap->sd = NULL;
110 v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n",
111 fimc->id);
112 return -ENODEV;
115 static int fimc_isp_subdev_init(struct fimc_dev *fimc, unsigned int index)
117 struct s5p_fimc_isp_info *isp_info;
118 struct s5p_platform_fimc *pdata = fimc->pdata;
119 int ret;
121 if (index >= pdata->num_clients)
122 return -EINVAL;
124 isp_info = &pdata->isp_info[index];
126 if (isp_info->clk_frequency)
127 clk_set_rate(fimc->clock[CLK_CAM], isp_info->clk_frequency);
129 ret = clk_enable(fimc->clock[CLK_CAM]);
130 if (ret)
131 return ret;
133 ret = fimc_subdev_attach(fimc, index);
134 if (ret)
135 return ret;
137 ret = fimc_hw_set_camera_polarity(fimc, isp_info);
138 if (ret)
139 return ret;
141 ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 1);
142 if (!ret)
143 return ret;
145 /* enabling power failed so unregister subdev */
146 fimc_subdev_unregister(fimc);
148 v4l2_err(&fimc->vid_cap.v4l2_dev, "ISP initialization failed: %d\n",
149 ret);
151 return ret;
154 static int fimc_stop_capture(struct fimc_dev *fimc)
156 unsigned long flags;
157 struct fimc_vid_cap *cap;
158 struct fimc_vid_buffer *buf;
160 cap = &fimc->vid_cap;
162 if (!fimc_capture_active(fimc))
163 return 0;
165 spin_lock_irqsave(&fimc->slock, flags);
166 set_bit(ST_CAPT_SHUT, &fimc->state);
167 fimc_deactivate_capture(fimc);
168 spin_unlock_irqrestore(&fimc->slock, flags);
170 wait_event_timeout(fimc->irq_queue,
171 !test_bit(ST_CAPT_SHUT, &fimc->state),
172 FIMC_SHUTDOWN_TIMEOUT);
174 v4l2_subdev_call(cap->sd, video, s_stream, 0);
176 spin_lock_irqsave(&fimc->slock, flags);
177 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
178 1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM);
180 fimc->vid_cap.active_buf_cnt = 0;
182 /* Release buffers that were enqueued in the driver by videobuf2. */
183 while (!list_empty(&cap->pending_buf_q)) {
184 buf = pending_queue_pop(cap);
185 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
188 while (!list_empty(&cap->active_buf_q)) {
189 buf = active_queue_pop(cap);
190 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
193 spin_unlock_irqrestore(&fimc->slock, flags);
195 dbg("state: 0x%lx", fimc->state);
196 return 0;
199 static int start_streaming(struct vb2_queue *q)
201 struct fimc_ctx *ctx = q->drv_priv;
202 struct fimc_dev *fimc = ctx->fimc_dev;
203 struct s5p_fimc_isp_info *isp_info;
204 int ret;
206 fimc_hw_reset(fimc);
208 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
209 if (ret && ret != -ENOIOCTLCMD)
210 return ret;
212 ret = fimc_prepare_config(ctx, ctx->state);
213 if (ret)
214 return ret;
216 isp_info = &fimc->pdata->isp_info[fimc->vid_cap.input_index];
217 fimc_hw_set_camera_type(fimc, isp_info);
218 fimc_hw_set_camera_source(fimc, isp_info);
219 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
221 if (ctx->state & FIMC_PARAMS) {
222 ret = fimc_set_scaler_info(ctx);
223 if (ret) {
224 err("Scaler setup error");
225 return ret;
227 fimc_hw_set_input_path(ctx);
228 fimc_hw_set_prescaler(ctx);
229 fimc_hw_set_mainscaler(ctx);
230 fimc_hw_set_target_format(ctx);
231 fimc_hw_set_rotation(ctx);
232 fimc_hw_set_effect(ctx);
235 fimc_hw_set_output_path(ctx);
236 fimc_hw_set_out_dma(ctx);
238 INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
239 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
240 fimc->vid_cap.active_buf_cnt = 0;
241 fimc->vid_cap.frame_count = 0;
242 fimc->vid_cap.buf_index = 0;
244 set_bit(ST_CAPT_PEND, &fimc->state);
246 return 0;
249 static int stop_streaming(struct vb2_queue *q)
251 struct fimc_ctx *ctx = q->drv_priv;
252 struct fimc_dev *fimc = ctx->fimc_dev;
254 if (!fimc_capture_active(fimc))
255 return -EINVAL;
257 return fimc_stop_capture(fimc);
260 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
262 if (!fr || plane >= fr->fmt->memplanes)
263 return 0;
264 return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
267 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
268 unsigned int *num_planes, unsigned long sizes[],
269 void *allocators[])
271 struct fimc_ctx *ctx = vq->drv_priv;
272 struct fimc_fmt *fmt = ctx->d_frame.fmt;
273 int i;
275 if (!fmt)
276 return -EINVAL;
278 *num_planes = fmt->memplanes;
280 for (i = 0; i < fmt->memplanes; i++) {
281 sizes[i] = get_plane_size(&ctx->d_frame, i);
282 allocators[i] = ctx->fimc_dev->alloc_ctx;
285 return 0;
288 static int buffer_prepare(struct vb2_buffer *vb)
290 struct vb2_queue *vq = vb->vb2_queue;
291 struct fimc_ctx *ctx = vq->drv_priv;
292 struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
293 int i;
295 if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
296 return -EINVAL;
298 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
299 unsigned long size = get_plane_size(&ctx->d_frame, i);
301 if (vb2_plane_size(vb, i) < size) {
302 v4l2_err(v4l2_dev, "User buffer too small (%ld < %ld)\n",
303 vb2_plane_size(vb, i), size);
304 return -EINVAL;
307 vb2_set_plane_payload(vb, i, size);
310 return 0;
313 static void buffer_queue(struct vb2_buffer *vb)
315 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
316 struct fimc_dev *fimc = ctx->fimc_dev;
317 struct fimc_vid_buffer *buf
318 = container_of(vb, struct fimc_vid_buffer, vb);
319 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
320 unsigned long flags;
321 int min_bufs;
323 spin_lock_irqsave(&fimc->slock, flags);
324 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
326 if (!test_bit(ST_CAPT_STREAM, &fimc->state)
327 && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
328 /* Setup the buffer directly for processing. */
329 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
330 vid_cap->buf_index;
332 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
333 buf->index = vid_cap->buf_index;
334 active_queue_add(vid_cap, buf);
336 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
337 vid_cap->buf_index = 0;
338 } else {
339 fimc_pending_queue_add(vid_cap, buf);
342 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
344 if (vid_cap->active_buf_cnt >= min_bufs &&
345 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state))
346 fimc_activate_capture(ctx);
348 spin_unlock_irqrestore(&fimc->slock, flags);
351 static void fimc_lock(struct vb2_queue *vq)
353 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
354 mutex_lock(&ctx->fimc_dev->lock);
357 static void fimc_unlock(struct vb2_queue *vq)
359 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
360 mutex_unlock(&ctx->fimc_dev->lock);
363 static struct vb2_ops fimc_capture_qops = {
364 .queue_setup = queue_setup,
365 .buf_prepare = buffer_prepare,
366 .buf_queue = buffer_queue,
367 .wait_prepare = fimc_unlock,
368 .wait_finish = fimc_lock,
369 .start_streaming = start_streaming,
370 .stop_streaming = stop_streaming,
373 static int fimc_capture_open(struct file *file)
375 struct fimc_dev *fimc = video_drvdata(file);
376 int ret = 0;
378 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
380 /* Return if the corresponding video mem2mem node is already opened. */
381 if (fimc_m2m_active(fimc))
382 return -EBUSY;
384 if (++fimc->vid_cap.refcnt == 1) {
385 ret = fimc_isp_subdev_init(fimc, 0);
386 if (ret) {
387 fimc->vid_cap.refcnt--;
388 return -EIO;
392 file->private_data = fimc->vid_cap.ctx;
394 return 0;
397 static int fimc_capture_close(struct file *file)
399 struct fimc_dev *fimc = video_drvdata(file);
401 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
403 if (--fimc->vid_cap.refcnt == 0) {
404 fimc_stop_capture(fimc);
405 vb2_queue_release(&fimc->vid_cap.vbq);
407 v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n");
409 v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
410 clk_disable(fimc->clock[CLK_CAM]);
411 fimc_subdev_unregister(fimc);
414 return 0;
417 static unsigned int fimc_capture_poll(struct file *file,
418 struct poll_table_struct *wait)
420 struct fimc_ctx *ctx = file->private_data;
421 struct fimc_dev *fimc = ctx->fimc_dev;
423 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
426 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
428 struct fimc_ctx *ctx = file->private_data;
429 struct fimc_dev *fimc = ctx->fimc_dev;
431 return vb2_mmap(&fimc->vid_cap.vbq, vma);
434 /* video device file operations */
435 static const struct v4l2_file_operations fimc_capture_fops = {
436 .owner = THIS_MODULE,
437 .open = fimc_capture_open,
438 .release = fimc_capture_close,
439 .poll = fimc_capture_poll,
440 .unlocked_ioctl = video_ioctl2,
441 .mmap = fimc_capture_mmap,
444 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
445 struct v4l2_capability *cap)
447 struct fimc_ctx *ctx = file->private_data;
448 struct fimc_dev *fimc = ctx->fimc_dev;
450 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
451 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
452 cap->bus_info[0] = 0;
453 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
454 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
456 return 0;
459 /* Synchronize formats of the camera interface input and attached sensor. */
460 static int sync_capture_fmt(struct fimc_ctx *ctx)
462 struct fimc_frame *frame = &ctx->s_frame;
463 struct fimc_dev *fimc = ctx->fimc_dev;
464 struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
465 int ret;
467 fmt->width = ctx->d_frame.o_width;
468 fmt->height = ctx->d_frame.o_height;
470 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
471 if (ret == -ENOIOCTLCMD) {
472 err("s_mbus_fmt failed");
473 return ret;
475 dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
477 frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
478 if (!frame->fmt) {
479 err("fimc source format not found\n");
480 return -EINVAL;
483 frame->f_width = fmt->width;
484 frame->f_height = fmt->height;
485 frame->width = fmt->width;
486 frame->height = fmt->height;
487 frame->o_width = fmt->width;
488 frame->o_height = fmt->height;
489 frame->offs_h = 0;
490 frame->offs_v = 0;
492 return 0;
495 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
496 struct v4l2_format *f)
498 struct fimc_ctx *ctx = priv;
499 struct fimc_dev *fimc = ctx->fimc_dev;
500 struct fimc_frame *frame;
501 struct v4l2_pix_format_mplane *pix;
502 int ret;
503 int i;
505 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
506 return -EINVAL;
508 ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
509 if (ret)
510 return ret;
512 if (vb2_is_busy(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
513 return -EBUSY;
515 frame = &ctx->d_frame;
517 pix = &f->fmt.pix_mp;
518 frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
519 if (!frame->fmt) {
520 err("fimc target format not found\n");
521 return -EINVAL;
524 for (i = 0; i < frame->fmt->colplanes; i++) {
525 frame->payload[i] =
526 (pix->width * pix->height * frame->fmt->depth[i]) >> 3;
529 /* Output DMA frame pixel size and offsets. */
530 frame->f_width = pix->plane_fmt[0].bytesperline * 8
531 / frame->fmt->depth[0];
532 frame->f_height = pix->height;
533 frame->width = pix->width;
534 frame->height = pix->height;
535 frame->o_width = pix->width;
536 frame->o_height = pix->height;
537 frame->offs_h = 0;
538 frame->offs_v = 0;
540 ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
542 ret = sync_capture_fmt(ctx);
543 return ret;
546 static int fimc_cap_enum_input(struct file *file, void *priv,
547 struct v4l2_input *i)
549 struct fimc_ctx *ctx = priv;
550 struct s5p_platform_fimc *pldata = ctx->fimc_dev->pdata;
551 struct s5p_fimc_isp_info *isp_info;
553 if (i->index >= pldata->num_clients)
554 return -EINVAL;
556 isp_info = &pldata->isp_info[i->index];
558 i->type = V4L2_INPUT_TYPE_CAMERA;
559 strncpy(i->name, isp_info->board_info->type, 32);
560 return 0;
563 static int fimc_cap_s_input(struct file *file, void *priv,
564 unsigned int i)
566 struct fimc_ctx *ctx = priv;
567 struct fimc_dev *fimc = ctx->fimc_dev;
568 struct s5p_platform_fimc *pdata = fimc->pdata;
570 if (fimc_capture_active(ctx->fimc_dev))
571 return -EBUSY;
573 if (i >= pdata->num_clients)
574 return -EINVAL;
577 if (fimc->vid_cap.sd) {
578 int ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
579 if (ret)
580 err("s_power failed: %d", ret);
582 clk_disable(fimc->clock[CLK_CAM]);
585 /* Release the attached sensor subdevice. */
586 fimc_subdev_unregister(fimc);
588 return fimc_isp_subdev_init(fimc, i);
591 static int fimc_cap_g_input(struct file *file, void *priv,
592 unsigned int *i)
594 struct fimc_ctx *ctx = priv;
595 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
597 *i = cap->input_index;
598 return 0;
601 static int fimc_cap_streamon(struct file *file, void *priv,
602 enum v4l2_buf_type type)
604 struct fimc_ctx *ctx = priv;
605 struct fimc_dev *fimc = ctx->fimc_dev;
607 if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
608 return -EBUSY;
610 if (!(ctx->state & FIMC_DST_FMT)) {
611 v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
612 return -EINVAL;
615 return vb2_streamon(&fimc->vid_cap.vbq, type);
618 static int fimc_cap_streamoff(struct file *file, void *priv,
619 enum v4l2_buf_type type)
621 struct fimc_ctx *ctx = priv;
622 struct fimc_dev *fimc = ctx->fimc_dev;
624 return vb2_streamoff(&fimc->vid_cap.vbq, type);
627 static int fimc_cap_reqbufs(struct file *file, void *priv,
628 struct v4l2_requestbuffers *reqbufs)
630 struct fimc_ctx *ctx = priv;
631 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
632 int ret;
635 ret = vb2_reqbufs(&cap->vbq, reqbufs);
636 if (!ret)
637 cap->reqbufs_count = reqbufs->count;
639 return ret;
642 static int fimc_cap_querybuf(struct file *file, void *priv,
643 struct v4l2_buffer *buf)
645 struct fimc_ctx *ctx = priv;
646 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
648 return vb2_querybuf(&cap->vbq, buf);
651 static int fimc_cap_qbuf(struct file *file, void *priv,
652 struct v4l2_buffer *buf)
654 struct fimc_ctx *ctx = priv;
655 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
656 return vb2_qbuf(&cap->vbq, buf);
659 static int fimc_cap_dqbuf(struct file *file, void *priv,
660 struct v4l2_buffer *buf)
662 struct fimc_ctx *ctx = priv;
663 return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
664 file->f_flags & O_NONBLOCK);
667 static int fimc_cap_s_ctrl(struct file *file, void *priv,
668 struct v4l2_control *ctrl)
670 struct fimc_ctx *ctx = priv;
671 int ret = -EINVAL;
673 /* Allow any controls but 90/270 rotation while streaming */
674 if (!fimc_capture_active(ctx->fimc_dev) ||
675 ctrl->id != V4L2_CID_ROTATE ||
676 (ctrl->value != 90 && ctrl->value != 270)) {
677 ret = check_ctrl_val(ctx, ctrl);
678 if (!ret) {
679 ret = fimc_s_ctrl(ctx, ctrl);
680 if (!ret)
681 ctx->state |= FIMC_PARAMS;
684 if (ret == -EINVAL)
685 ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
686 core, s_ctrl, ctrl);
687 return ret;
690 static int fimc_cap_cropcap(struct file *file, void *fh,
691 struct v4l2_cropcap *cr)
693 struct fimc_frame *f;
694 struct fimc_ctx *ctx = fh;
696 if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
697 return -EINVAL;
699 f = &ctx->s_frame;
701 cr->bounds.left = 0;
702 cr->bounds.top = 0;
703 cr->bounds.width = f->o_width;
704 cr->bounds.height = f->o_height;
705 cr->defrect = cr->bounds;
707 return 0;
710 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
712 struct fimc_frame *f;
713 struct fimc_ctx *ctx = file->private_data;
715 f = &ctx->s_frame;
717 cr->c.left = f->offs_h;
718 cr->c.top = f->offs_v;
719 cr->c.width = f->width;
720 cr->c.height = f->height;
722 return 0;
725 static int fimc_cap_s_crop(struct file *file, void *fh,
726 struct v4l2_crop *cr)
728 struct fimc_frame *f;
729 struct fimc_ctx *ctx = file->private_data;
730 struct fimc_dev *fimc = ctx->fimc_dev;
731 int ret = -EINVAL;
733 if (fimc_capture_active(fimc))
734 return -EBUSY;
736 ret = fimc_try_crop(ctx, cr);
737 if (ret)
738 return ret;
740 if (!(ctx->state & FIMC_DST_FMT)) {
741 v4l2_err(&fimc->vid_cap.v4l2_dev,
742 "Capture color format not set\n");
743 return -EINVAL; /* TODO: make sure this is the right value */
746 f = &ctx->s_frame;
747 /* Check for the pixel scaling ratio when cropping input image. */
748 ret = fimc_check_scaler_ratio(cr->c.width, cr->c.height,
749 ctx->d_frame.width, ctx->d_frame.height,
750 ctx->rotation);
751 if (ret) {
752 v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range\n");
753 return ret;
756 f->offs_h = cr->c.left;
757 f->offs_v = cr->c.top;
758 f->width = cr->c.width;
759 f->height = cr->c.height;
761 return 0;
765 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
766 .vidioc_querycap = fimc_vidioc_querycap_capture,
768 .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
769 .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane,
770 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
771 .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane,
773 .vidioc_reqbufs = fimc_cap_reqbufs,
774 .vidioc_querybuf = fimc_cap_querybuf,
776 .vidioc_qbuf = fimc_cap_qbuf,
777 .vidioc_dqbuf = fimc_cap_dqbuf,
779 .vidioc_streamon = fimc_cap_streamon,
780 .vidioc_streamoff = fimc_cap_streamoff,
782 .vidioc_queryctrl = fimc_vidioc_queryctrl,
783 .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
784 .vidioc_s_ctrl = fimc_cap_s_ctrl,
786 .vidioc_g_crop = fimc_cap_g_crop,
787 .vidioc_s_crop = fimc_cap_s_crop,
788 .vidioc_cropcap = fimc_cap_cropcap,
790 .vidioc_enum_input = fimc_cap_enum_input,
791 .vidioc_s_input = fimc_cap_s_input,
792 .vidioc_g_input = fimc_cap_g_input,
795 /* fimc->lock must be already initialized */
796 int fimc_register_capture_device(struct fimc_dev *fimc)
798 struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
799 struct video_device *vfd;
800 struct fimc_vid_cap *vid_cap;
801 struct fimc_ctx *ctx;
802 struct v4l2_format f;
803 struct fimc_frame *fr;
804 struct vb2_queue *q;
805 int ret;
807 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
808 if (!ctx)
809 return -ENOMEM;
811 ctx->fimc_dev = fimc;
812 ctx->in_path = FIMC_CAMERA;
813 ctx->out_path = FIMC_DMA;
814 ctx->state = FIMC_CTX_CAP;
816 /* Default format of the output frames */
817 f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
818 fr = &ctx->d_frame;
819 fr->fmt = find_format(&f, FMT_FLAGS_M2M);
820 fr->width = fr->f_width = fr->o_width = 640;
821 fr->height = fr->f_height = fr->o_height = 480;
823 if (!v4l2_dev->name[0])
824 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
825 "%s.capture", dev_name(&fimc->pdev->dev));
827 ret = v4l2_device_register(NULL, v4l2_dev);
828 if (ret)
829 goto err_info;
831 vfd = video_device_alloc();
832 if (!vfd) {
833 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
834 goto err_v4l2_reg;
837 snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
838 dev_name(&fimc->pdev->dev));
840 vfd->fops = &fimc_capture_fops;
841 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
842 vfd->minor = -1;
843 vfd->release = video_device_release;
844 vfd->lock = &fimc->lock;
845 video_set_drvdata(vfd, fimc);
847 vid_cap = &fimc->vid_cap;
848 vid_cap->vfd = vfd;
849 vid_cap->active_buf_cnt = 0;
850 vid_cap->reqbufs_count = 0;
851 vid_cap->refcnt = 0;
852 /* Default color format for image sensor */
853 vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
855 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
856 INIT_LIST_HEAD(&vid_cap->active_buf_q);
857 spin_lock_init(&ctx->slock);
858 vid_cap->ctx = ctx;
860 q = &fimc->vid_cap.vbq;
861 memset(q, 0, sizeof(*q));
862 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
863 q->io_modes = VB2_MMAP | VB2_USERPTR;
864 q->drv_priv = fimc->vid_cap.ctx;
865 q->ops = &fimc_capture_qops;
866 q->mem_ops = &vb2_dma_contig_memops;
867 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
869 vb2_queue_init(q);
871 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
872 if (ret) {
873 v4l2_err(v4l2_dev, "Failed to register video device\n");
874 goto err_vd_reg;
877 v4l2_info(v4l2_dev,
878 "FIMC capture driver registered as /dev/video%d\n",
879 vfd->num);
881 return 0;
883 err_vd_reg:
884 video_device_release(vfd);
885 err_v4l2_reg:
886 v4l2_device_unregister(v4l2_dev);
887 err_info:
888 kfree(ctx);
889 dev_err(&fimc->pdev->dev, "failed to install\n");
890 return ret;
893 void fimc_unregister_capture_device(struct fimc_dev *fimc)
895 struct fimc_vid_cap *capture = &fimc->vid_cap;
897 if (capture->vfd)
898 video_unregister_device(capture->vfd);
900 kfree(capture->ctx);