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);
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 s3c_platform_fimc *pdata = fimc->pdata;
90 struct s3c_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, int index)
118 struct s3c_fimc_isp_info *isp_info;
121 ret = fimc_subdev_attach(fimc, index);
125 isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
126 ret = fimc_hw_set_camera_polarity(fimc, isp_info);
128 ret = v4l2_subdev_call(fimc->vid_cap.sd, core,
134 fimc_subdev_unregister(fimc);
135 err("ISP initialization failed: %d", 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;
150 BUG_ON(!fimc || !fimc_vb);
152 ret = fimc_prepare_addr(ctx, fimc_vb, &ctx->d_frame,
157 if (test_bit(ST_CAPT_STREAM, &fimc->state)) {
158 fimc_pending_queue_add(cap, fimc_vb);
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)
173 static int fimc_stop_capture(struct fimc_dev *fimc)
176 struct fimc_vid_cap *cap;
179 cap = &fimc->vid_cap;
181 if (!fimc_capture_active(fimc))
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);
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);
208 static int fimc_capture_open(struct file *file)
210 struct fimc_dev *fimc = video_drvdata(file);
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))
219 if (mutex_lock_interruptible(&fimc->lock))
222 if (++fimc->vid_cap.refcnt == 1) {
223 ret = fimc_isp_subdev_init(fimc, -1);
225 fimc->vid_cap.refcnt--;
230 file->private_data = fimc->vid_cap.ctx;
232 mutex_unlock(&fimc->lock);
236 static int fimc_capture_close(struct file *file)
238 struct fimc_dev *fimc = video_drvdata(file);
240 if (mutex_lock_interruptible(&fimc->lock))
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);
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;
268 if (mutex_lock_interruptible(&fimc->lock))
271 ret = videobuf_poll_stream(file, &cap->vbq, wait);
272 mutex_unlock(&fimc->lock);
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;
284 if (mutex_lock_interruptible(&fimc->lock))
287 ret = videobuf_mmap_mapper(&cap->vbq, vma);
288 mutex_unlock(&fimc->lock);
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;
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;
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");
334 dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
336 frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
338 err("fimc source format not found\n");
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;
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;
363 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
366 ret = fimc_vidioc_try_fmt(file, priv, f);
370 if (mutex_lock_interruptible(&fimc->lock))
373 if (fimc_capture_active(fimc)) {
378 frame = &ctx->d_frame;
381 frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
383 err("fimc target format not found\n");
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;
399 ret = sync_capture_fmt(ctx);
401 ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
404 mutex_unlock(&fimc->lock);
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)
418 isp_info = pldata->isp_info[i->index];
419 if (isp_info == NULL)
422 i->type = V4L2_INPUT_TYPE_CAMERA;
423 strncpy(i->name, isp_info->board_info->type, 32);
427 static int fimc_cap_s_input(struct file *file, void *priv,
430 struct fimc_ctx *ctx = priv;
431 struct fimc_dev *fimc = ctx->fimc_dev;
432 struct s3c_platform_fimc *pdata = fimc->pdata;
435 if (fimc_capture_active(ctx->fimc_dev))
438 if (mutex_lock_interruptible(&fimc->lock))
441 if (i >= FIMC_MAX_CAMIF_CLIENTS || !pdata->isp_info[i]) {
446 if (fimc->vid_cap.sd) {
447 ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
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);
458 mutex_unlock(&fimc->lock);
462 static int fimc_cap_g_input(struct file *file, void *priv,
465 struct fimc_ctx *ctx = priv;
466 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
468 *i = cap->input_index;
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;
480 if (mutex_lock_interruptible(&fimc->lock))
483 if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
486 if (!(ctx->state & FIMC_DST_FMT)) {
487 v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
492 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
493 if (ret && ret != -ENOIOCTLCMD)
496 ret = fimc_prepare_config(ctx, ctx->state);
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);
508 err("Scaler setup error");
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;
526 set_bit(ST_CAPT_PEND, &fimc->state);
527 ret = videobuf_streamon(&fimc->vid_cap.vbq);
530 mutex_unlock(&fimc->lock);
534 static int fimc_cap_streamoff(struct file *file, void *priv,
535 enum v4l2_buf_type type)
537 struct fimc_ctx *ctx = priv;
538 struct fimc_dev *fimc = ctx->fimc_dev;
539 struct fimc_vid_cap *cap = &fimc->vid_cap;
543 spin_lock_irqsave(&fimc->slock, flags);
544 if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) {
545 spin_unlock_irqrestore(&fimc->slock, flags);
546 dbg("state: 0x%lx", fimc->state);
549 spin_unlock_irqrestore(&fimc->slock, flags);
551 if (mutex_lock_interruptible(&fimc->lock))
554 fimc_stop_capture(fimc);
555 ret = videobuf_streamoff(&cap->vbq);
556 mutex_unlock(&fimc->lock);
560 static int fimc_cap_reqbufs(struct file *file, void *priv,
561 struct v4l2_requestbuffers *reqbufs)
563 struct fimc_ctx *ctx = priv;
564 struct fimc_dev *fimc = ctx->fimc_dev;
565 struct fimc_vid_cap *cap = &fimc->vid_cap;
568 if (fimc_capture_active(ctx->fimc_dev))
571 if (mutex_lock_interruptible(&fimc->lock))
574 ret = videobuf_reqbufs(&cap->vbq, reqbufs);
576 cap->reqbufs_count = reqbufs->count;
578 mutex_unlock(&fimc->lock);
582 static int fimc_cap_querybuf(struct file *file, void *priv,
583 struct v4l2_buffer *buf)
585 struct fimc_ctx *ctx = priv;
586 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
588 if (fimc_capture_active(ctx->fimc_dev))
591 return videobuf_querybuf(&cap->vbq, buf);
594 static int fimc_cap_qbuf(struct file *file, void *priv,
595 struct v4l2_buffer *buf)
597 struct fimc_ctx *ctx = priv;
598 struct fimc_dev *fimc = ctx->fimc_dev;
599 struct fimc_vid_cap *cap = &fimc->vid_cap;
602 if (mutex_lock_interruptible(&fimc->lock))
605 ret = videobuf_qbuf(&cap->vbq, buf);
607 mutex_unlock(&fimc->lock);
611 static int fimc_cap_dqbuf(struct file *file, void *priv,
612 struct v4l2_buffer *buf)
614 struct fimc_ctx *ctx = priv;
617 if (mutex_lock_interruptible(&ctx->fimc_dev->lock))
620 ret = videobuf_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
621 file->f_flags & O_NONBLOCK);
623 mutex_unlock(&ctx->fimc_dev->lock);
627 static int fimc_cap_s_ctrl(struct file *file, void *priv,
628 struct v4l2_control *ctrl)
630 struct fimc_ctx *ctx = priv;
633 if (mutex_lock_interruptible(&ctx->fimc_dev->lock))
636 /* Allow any controls but 90/270 rotation while streaming */
637 if (!fimc_capture_active(ctx->fimc_dev) ||
638 ctrl->id != V4L2_CID_ROTATE ||
639 (ctrl->value != 90 && ctrl->value != 270)) {
640 ret = check_ctrl_val(ctx, ctrl);
642 ret = fimc_s_ctrl(ctx, ctrl);
644 ctx->state |= FIMC_PARAMS;
648 ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
651 mutex_unlock(&ctx->fimc_dev->lock);
655 static int fimc_cap_s_crop(struct file *file, void *fh,
656 struct v4l2_crop *cr)
658 struct fimc_frame *f;
659 struct fimc_ctx *ctx = file->private_data;
660 struct fimc_dev *fimc = ctx->fimc_dev;
663 if (fimc_capture_active(fimc))
666 ret = fimc_try_crop(ctx, cr);
670 if (mutex_lock_interruptible(&fimc->lock))
673 if (!(ctx->state & FIMC_DST_FMT)) {
674 v4l2_err(&fimc->vid_cap.v4l2_dev,
675 "Capture color format not set\n");
680 /* Check for the pixel scaling ratio when cropping input image. */
681 ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
683 v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range");
686 f->offs_h = cr->c.left;
687 f->offs_v = cr->c.top;
688 f->width = cr->c.width;
689 f->height = cr->c.height;
693 mutex_unlock(&fimc->lock);
698 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
699 .vidioc_querycap = fimc_vidioc_querycap_capture,
701 .vidioc_enum_fmt_vid_cap = fimc_vidioc_enum_fmt,
702 .vidioc_try_fmt_vid_cap = fimc_vidioc_try_fmt,
703 .vidioc_s_fmt_vid_cap = fimc_cap_s_fmt,
704 .vidioc_g_fmt_vid_cap = fimc_vidioc_g_fmt,
706 .vidioc_reqbufs = fimc_cap_reqbufs,
707 .vidioc_querybuf = fimc_cap_querybuf,
709 .vidioc_qbuf = fimc_cap_qbuf,
710 .vidioc_dqbuf = fimc_cap_dqbuf,
712 .vidioc_streamon = fimc_cap_streamon,
713 .vidioc_streamoff = fimc_cap_streamoff,
715 .vidioc_queryctrl = fimc_vidioc_queryctrl,
716 .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
717 .vidioc_s_ctrl = fimc_cap_s_ctrl,
719 .vidioc_g_crop = fimc_vidioc_g_crop,
720 .vidioc_s_crop = fimc_cap_s_crop,
721 .vidioc_cropcap = fimc_vidioc_cropcap,
723 .vidioc_enum_input = fimc_cap_enum_input,
724 .vidioc_s_input = fimc_cap_s_input,
725 .vidioc_g_input = fimc_cap_g_input,
728 int fimc_register_capture_device(struct fimc_dev *fimc)
730 struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
731 struct video_device *vfd;
732 struct fimc_vid_cap *vid_cap;
733 struct fimc_ctx *ctx;
734 struct v4l2_format f;
737 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
741 ctx->fimc_dev = fimc;
742 ctx->in_path = FIMC_CAMERA;
743 ctx->out_path = FIMC_DMA;
744 ctx->state = FIMC_CTX_CAP;
746 f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
747 ctx->d_frame.fmt = find_format(&f, FMT_FLAGS_M2M);
749 if (!v4l2_dev->name[0])
750 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
751 "%s.capture", dev_name(&fimc->pdev->dev));
753 ret = v4l2_device_register(NULL, v4l2_dev);
757 vfd = video_device_alloc();
759 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
763 snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
764 dev_name(&fimc->pdev->dev));
766 vfd->fops = &fimc_capture_fops;
767 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
769 vfd->release = video_device_release;
770 video_set_drvdata(vfd, fimc);
772 vid_cap = &fimc->vid_cap;
774 vid_cap->active_buf_cnt = 0;
775 vid_cap->reqbufs_count = 0;
777 /* The default color format for image sensor. */
778 vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
780 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
781 INIT_LIST_HEAD(&vid_cap->active_buf_q);
782 spin_lock_init(&ctx->slock);
785 videobuf_queue_dma_contig_init(&vid_cap->vbq, &fimc_qops,
786 vid_cap->v4l2_dev.dev, &fimc->irqlock,
787 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
788 sizeof(struct fimc_vid_buffer), (void *)ctx);
790 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
792 v4l2_err(v4l2_dev, "Failed to register video device\n");
797 "FIMC capture driver registered as /dev/video%d\n",
803 video_device_release(vfd);
805 v4l2_device_unregister(v4l2_dev);
807 dev_err(&fimc->pdev->dev, "failed to install\n");
811 void fimc_unregister_capture_device(struct fimc_dev *fimc)
813 struct fimc_vid_cap *capture = &fimc->vid_cap;
816 video_unregister_device(capture->vfd);