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-cma.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 v4l2_device_unregister_subdev(vid_cap->fb_sd);
76 vid_cap->fb_sd = NULL;
78 vid_cap->input_index = -1;
81 static int mipi_csi_register_callback(struct device *dev, void *p)
83 struct v4l2_subdev **sd = p;
86 * FIXME: detect platform device id and handle multiple
89 *sd = dev_get_drvdata(dev);
92 struct platform_device *pdev = v4l2_get_subdevdata(*sd);
94 dbg("pdev->id: %d", pdev->id);
97 return 0; /* non-zero value stops iteration */
100 static struct v4l2_subdev * s5p_mipi_get_subdev(int id)
102 const char *module_name = "s5p-mipi-csis";
103 struct device_driver *drv;
104 struct v4l2_subdev *sd = NULL;
107 drv = driver_find(module_name, &platform_bus_type);
109 request_module(module_name);
110 drv = driver_find(module_name, &platform_bus_type);
113 return ERR_PTR(-ENODEV);
116 * FIXME: detect platform device id and handle multiple
117 * MIPI-CSI devices. Now always a subdev from the last
118 * found device is returned.
120 ret = driver_for_each_device(drv, NULL, &sd,
121 mipi_csi_register_callback);
124 return ret ? NULL : sd;
127 static int s3cfb_register_callback(struct device *dev, void *p)
129 struct v4l2_subdev **sd = p;
132 * FIXME: detect platform device id and handle multiple
135 *sd = dev_get_drvdata(dev);
138 struct platform_device *pdev = v4l2_get_subdevdata(*sd);
140 dbg("pdev->id: %d", pdev->id);
143 return 0; /* non-zero value stops iteration */
146 static struct v4l2_subdev *s3c_fb_get_subdev(int id)
148 const char *module_name = FIMD_MODULE_NAME;
149 struct device_driver *drv;
150 struct v4l2_subdev *sd = NULL;
153 drv = driver_find(module_name, &platform_bus_type);
155 request_module(module_name);
156 drv = driver_find(module_name, &platform_bus_type);
159 return ERR_PTR(-ENODEV);
161 * FIXME: detect platform device id and handle multiple
162 * MIPI-CSI devices. Now always a subdev from the last
163 * found device is returned.
165 ret = driver_for_each_device(drv, NULL, &sd,
166 s3cfb_register_callback);
169 return ret ? NULL : sd;
173 * fimc_subdev_attach - attach v4l2_subdev to camera host interface
175 * @fimc: FIMC device information
176 * @index: index to the array of available subdevices,
177 * -1 for full array search or non negative value
178 * to select specific subdevice
180 static int fimc_subdev_attach(struct fimc_dev *fimc, int index)
182 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
183 struct s5p_platform_fimc *pdata = fimc->pdata;
184 struct s5p_fimc_isp_info *isp_info;
185 struct v4l2_subdev *sd;
188 for (i = 0; i < pdata->num_clients; ++i) {
189 isp_info = pdata->isp_info[i];
190 if (!isp_info || (index >= 0 && i != index))
192 if (isp_info->bus_type == FIMC_MIPI_CSI2) {
193 vid_cap->mipi_sd = s5p_mipi_get_subdev(0);
194 if (IS_ERR_OR_NULL(vid_cap->mipi_sd)) {
195 fimc->vid_cap.mipi_sd = NULL;
196 return PTR_ERR(vid_cap->mipi_sd);
199 else if (isp_info->bus_type == FIMC_LCD_WB) {
200 vid_cap->fb_sd = s3c_fb_get_subdev(0);
201 if (IS_ERR_OR_NULL(vid_cap->fb_sd))
202 return PTR_ERR(vid_cap->fb_sd);
203 vid_cap->ctx->in_path = FIMC_LCD_WB;
204 vid_cap->input_index = i;
209 sd = fimc_subdev_register(fimc, isp_info);
212 vid_cap->input_index = i;
218 vid_cap->input_index = -1;
220 v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n",
224 static int fimc_isp_subdev_init(struct fimc_dev *fimc, unsigned int index)
226 struct s5p_fimc_isp_info *isp_info;
228 if (index >= fimc->pdata->num_clients)
231 isp_info = fimc->pdata->isp_info[index];
235 if (isp_info->clk_frequency) {
236 ret = fimc_clk_setrate(fimc, CLK_CAM, isp_info);
241 ret = clk_enable(fimc->clock[CLK_CAM]);
245 ret = fimc_subdev_attach(fimc, index);
249 ret = fimc_hw_set_camera_polarity(fimc, isp_info);
253 if (fimc->vid_cap.mipi_sd)
254 ret = v4l2_subdev_call(fimc->vid_cap.mipi_sd, core, s_power, 1);
257 ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 1);
259 if (fimc->vid_cap.fb_sd) {
260 unsigned int wb_on = 1;
261 dbg("write-back mode\n");
262 ret = v4l2_subdev_call(fimc->vid_cap.fb_sd, core, ioctl,
263 (unsigned int)NULL, &wb_on);
268 /* enabling power failed so unregister subdev */
269 fimc_subdev_unregister(fimc);
271 v4l2_err(&fimc->vid_cap.v4l2_dev, "ISP initialization failed: %d\n",
278 * At least one buffer on the pending_buf_q queue is required.
279 * Locking: The caller holds fimc->slock spinlock.
281 int fimc_vid_cap_buf_queue(struct fimc_dev *fimc,
282 struct fimc_vid_buffer *fimc_vb)
284 struct fimc_vid_cap *cap = &fimc->vid_cap;
285 struct fimc_ctx *ctx = cap->ctx;
288 BUG_ON(!fimc || !fimc_vb);
290 ret = fimc_prepare_addr(ctx, &fimc_vb->vb, &ctx->d_frame,
295 if (test_bit(ST_CAPT_STREAM, &fimc->state)) {
296 fimc_pending_queue_add(cap, fimc_vb);
298 /* Setup the buffer directly for processing. */
299 int buf_id = (cap->reqbufs_count == 1) ? -1 : cap->buf_index;
300 fimc_hw_set_output_addr(fimc, &fimc_vb->paddr, buf_id);
302 fimc_vb->index = cap->buf_index;
303 active_queue_add(cap, fimc_vb);
305 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
311 static int fimc_stop_capture(struct fimc_dev *fimc)
314 struct fimc_vid_cap *cap;
315 struct fimc_vid_buffer *buf;
317 cap = &fimc->vid_cap;
319 if (!fimc_capture_active(fimc))
322 spin_lock_irqsave(&fimc->slock, flags);
323 set_bit(ST_CAPT_SHUT, &fimc->state);
324 fimc_deactivate_capture(fimc);
325 spin_unlock_irqrestore(&fimc->slock, flags);
327 wait_event_timeout(fimc->irq_queue,
328 !test_bit(ST_CAPT_SHUT, &fimc->state),
329 FIMC_SHUTDOWN_TIMEOUT);
331 v4l2_subdev_call(cap->sd, video, s_stream, 0);
333 if (fimc->vid_cap.mipi_sd)
334 v4l2_subdev_call(fimc->vid_cap.mipi_sd, video, s_stream, 0);
336 spin_lock_irqsave(&fimc->slock, flags);
337 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
338 1 << ST_CAPT_STREAM);
340 fimc->vid_cap.active_buf_cnt = 0;
342 /* Release buffers that were enqueued in the driver by videobuf2. */
343 while (!list_empty(&cap->pending_buf_q)) {
344 buf = pending_queue_pop(cap);
345 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
348 while (!list_empty(&cap->active_buf_q)) {
349 buf = active_queue_pop(cap);
350 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
352 spin_unlock_irqrestore(&fimc->slock, flags);
354 dbg("state: 0x%lx", fimc->state);
358 static int start_streaming(struct vb2_queue *q)
360 struct fimc_ctx *ctx = q->drv_priv;
361 struct fimc_dev *fimc = ctx->fimc_dev;
362 struct s5p_fimc_isp_info *isp_info;
365 if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
366 pm_runtime_get_sync(&fimc->pdev->dev);
368 if (ret && ret != -ENOIOCTLCMD)
371 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
372 if (ret && ret != -ENOIOCTLCMD) {
373 err("sd s_stream error");
377 if (fimc->vid_cap.mipi_sd) {
378 ret = v4l2_subdev_call(fimc->vid_cap.mipi_sd, video, s_stream, 1);
380 err("mipi s_stream error");
385 ret = fimc_prepare_config(ctx, ctx->state);
389 isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
390 fimc_hw_set_camera_type(fimc, isp_info);
392 if (ctx->in_path == FIMC_LCD_WB) {
393 if (isp_info->mux_id == 0)
394 fimc_hwset_sysreg_camblk_fimd0_wb(fimc);
396 fimc_hwset_sysreg_camblk_fimd1_wb(fimc);
398 fimc_hw_set_camera_source(fimc, isp_info);
399 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
401 if (ctx->state & FIMC_PARAMS) {
402 ret = fimc_set_scaler_info(ctx);
404 err("Scaler setup error");
407 fimc_hw_set_input_path(ctx);
408 fimc_hw_set_prescaler(ctx);
409 fimc_hw_set_mainscaler(ctx);
410 fimc_hw_set_target_format(ctx);
411 fimc_hw_set_rotation(ctx);
412 fimc_hw_set_effect(ctx);
415 fimc_hw_set_out_dma(ctx);
417 INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
418 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
419 fimc->vid_cap.active_buf_cnt = 0;
420 fimc->vid_cap.frame_count = 0;
421 fimc->vid_cap.buf_index = fimc_hw_get_frame_index(fimc);
423 set_bit(ST_CAPT_PEND, &fimc->state);
427 static int stop_streaming(struct vb2_queue *q)
429 struct fimc_ctx *ctx = q->drv_priv;
430 struct fimc_dev *fimc = ctx->fimc_dev;
433 if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
434 pm_runtime_get_sync(&fimc->pdev->dev);
436 spin_lock_irqsave(&fimc->slock, flags);
437 if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) {
438 spin_unlock_irqrestore(&fimc->slock, flags);
441 spin_unlock_irqrestore(&fimc->slock, flags);
443 fimc_stop_capture(fimc);
444 #ifdef CONFIG_VIDEOBUF2_SDVMM
445 vb2_sdvmm_suspend(fimc->alloc_ctx);
447 if (!test_and_set_bit(ST_SUSPEND, &fimc->state))
448 pm_runtime_put_sync(&fimc->pdev->dev);
453 static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
455 if (!fr || plane >= fr->fmt->memplanes)
458 dbg("%s: w: %d. h: %d. depth[%d]: %d",
459 __func__, fr->width, fr->height, plane, fr->fmt->depth[plane]);
461 return fr->payload[plane];
464 static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
465 unsigned int *num_planes, unsigned long sizes[],
468 struct fimc_ctx *ctx = vq->drv_priv;
469 struct fimc_fmt *fmt = ctx->d_frame.fmt;
475 *num_planes = fmt->memplanes;
477 dbg("%s, buffer count=%d, plane count=%d",
478 __func__, *num_buffers, *num_planes);
480 for (i = 0; i < fmt->memplanes; i++) {
481 sizes[i] = get_plane_size(&ctx->d_frame, i);
482 dbg("plane: %u, plane_size: %lu", i, sizes[i]);
483 allocators[i] = ctx->fimc_dev->alloc_ctx;
489 static int buffer_init(struct vb2_buffer *vb)
495 static int buffer_prepare(struct vb2_buffer *vb)
497 struct vb2_queue *vq = vb->vb2_queue;
498 struct fimc_ctx *ctx = vq->drv_priv;
499 struct fimc_frame *frame = &ctx->d_frame;
500 struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
503 if (!frame->fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
506 for (i = 0; i < frame->fmt->memplanes; i++) {
507 unsigned long size = get_plane_size(frame, i);
508 dbg("vb2_plane_size(%d) : %ld", i, size);
509 if (vb2_plane_size(vb, i) < size) {
510 v4l2_err(v4l2_dev, "User buffer too small(%ld < %ld)\n",
511 vb2_plane_size(vb, i), size);
515 vb2_set_plane_payload(vb, i, size);
516 if (frame->cacheable)
517 vb2_sdvmm_cache_flush(ctx->fimc_dev->alloc_ctx, vb, i);
523 static void buffer_queue(struct vb2_buffer *vb)
525 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
526 struct fimc_dev *fimc = ctx->fimc_dev;
527 struct fimc_vid_buffer *buf
528 = container_of(vb, struct fimc_vid_buffer, vb);
529 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
532 spin_lock_irqsave(&fimc->slock, flags);
533 if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
534 pm_runtime_get_sync(&fimc->pdev->dev);
536 fimc_vid_cap_buf_queue(fimc, buf);
538 dbg("active_buf_cnt: %d", fimc->vid_cap.active_buf_cnt);
540 if (vid_cap->active_buf_cnt == FIMC_MAX_OUT_BUFS)
541 set_bit(ST_CAPT_STREAM, &fimc->state);
543 if (test_bit(ST_CAPT_LAST_IRQ, &fimc->state) ||
544 !test_bit(ST_CAPT_RUN, &fimc->state)) {
545 #ifdef CONFIG_VIDEOBUF2_SDVMM
546 if (vid_cap->active_buf_cnt == 1)
547 vb2_sdvmm_resume(fimc->alloc_ctx);
549 fimc_activate_capture(ctx);
550 clear_bit(ST_CAPT_LAST_IRQ, &fimc->state);
552 spin_unlock_irqrestore(&fimc->slock, flags);
555 static void fimc_lock(struct vb2_queue *vq)
558 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
559 mutex_lock(&ctx->fimc_dev->lock);
563 static void fimc_unlock(struct vb2_queue *vq)
566 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
567 mutex_unlock(&ctx->fimc_dev->lock);
571 static struct vb2_ops fimc_capture_qops = {
572 .queue_setup = queue_setup,
573 .buf_prepare = buffer_prepare,
574 .buf_queue = buffer_queue,
575 .buf_init = buffer_init,
576 .wait_prepare = fimc_unlock,
577 .wait_finish = fimc_lock,
578 .start_streaming = start_streaming,
579 .stop_streaming = stop_streaming,
582 static int fimc_capture_open(struct file *file)
584 struct fimc_dev *fimc = video_drvdata(file);
587 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
588 if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
589 pm_runtime_get_sync(&fimc->pdev->dev);
592 fimc_hw_set_irq_level(fimc);
594 if (fimc->variant->out_buf_count > 4)
595 fimc_hw_set_dma_seq(fimc, 0xF);
597 /* Return if the corresponding video mem2mem node is already opened. */
598 if (fimc_m2m_active(fimc))
601 if (++fimc->vid_cap.refcnt == 1) {
602 ret = fimc_isp_subdev_init(fimc, 0);
604 fimc->vid_cap.refcnt--;
609 file->private_data = fimc->vid_cap.ctx;
611 if (!test_and_set_bit(ST_SUSPEND, &fimc->state))
612 pm_runtime_put_sync(&fimc->pdev->dev);
616 static int fimc_capture_close(struct file *file)
618 struct fimc_dev *fimc = video_drvdata(file);
620 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
622 if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
623 pm_runtime_get_sync(&fimc->pdev->dev);
625 if (--fimc->vid_cap.refcnt == 0) {
626 fimc_stop_capture(fimc);
627 vb2_queue_release(&fimc->vid_cap.vbq);
629 v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n");
631 if (fimc->vid_cap.sd)
632 v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
634 if (fimc->vid_cap.mipi_sd)
635 v4l2_subdev_call(fimc->vid_cap.mipi_sd, core, s_power, 0);
637 clk_disable(fimc->clock[CLK_CAM]);
638 fimc_subdev_unregister(fimc);
641 if (!test_and_set_bit(ST_SUSPEND, &fimc->state))
642 pm_runtime_put_sync(&fimc->pdev->dev);
647 static unsigned int fimc_capture_poll(struct file *file,
648 struct poll_table_struct *wait)
650 struct fimc_ctx *ctx = file->private_data;
651 struct fimc_dev *fimc = ctx->fimc_dev;
653 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
656 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
658 struct fimc_ctx *ctx = file->private_data;
659 struct fimc_dev *fimc = ctx->fimc_dev;
661 return vb2_mmap(&fimc->vid_cap.vbq, vma);
664 /* video device file operations */
665 static const struct v4l2_file_operations fimc_capture_fops = {
666 .owner = THIS_MODULE,
667 .open = fimc_capture_open,
668 .release = fimc_capture_close,
669 .poll = fimc_capture_poll,
670 .unlocked_ioctl = video_ioctl2,
671 .mmap = fimc_capture_mmap,
674 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
675 struct v4l2_capability *cap)
677 struct fimc_ctx *ctx = file->private_data;
678 struct fimc_dev *fimc = ctx->fimc_dev;
680 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
681 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
682 cap->bus_info[0] = 0;
683 cap->version = KERNEL_VERSION(1, 0, 0);
684 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
685 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
690 /* Synchronize formats of the camera interface input and attached sensor. */
691 static int sync_capture_fmt(struct fimc_ctx *ctx)
693 struct fimc_frame *frame = &ctx->s_frame;
694 struct fimc_dev *fimc = ctx->fimc_dev;
695 struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
698 fmt->width = ctx->d_frame.o_width;
699 fmt->height = ctx->d_frame.o_height;
701 if (fimc->vid_cap.sd)
702 ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
704 if (ret == -ENOIOCTLCMD) {
705 err("s_mbus_fmt failed");
709 if (fimc->vid_cap.mipi_sd) {
710 ret = v4l2_subdev_call(fimc->vid_cap.mipi_sd, video, s_mbus_fmt, fmt);
712 err("s_mbus_fmt failed: %d", ret);
717 dbg("w= %d, h= %d, code= %d", fmt->width, fmt->height, fmt->code);
719 frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
721 err("fimc source format not found\n");
725 frame->f_width = fmt->width;
726 frame->f_height = fmt->height;
727 frame->width = fmt->width;
728 frame->height = fmt->height;
729 frame->o_width = fmt->width;
730 frame->o_height = fmt->height;
734 dbg("frame->width : %d, frame->f_height : %d, frame->width : %d,\
735 frame->height : %d, frame->o_width : %d, frame->o_height :\
736 %d\n", frame->width, frame->f_height, frame->width,\
737 frame->height, frame->o_width, frame->o_height);
741 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
742 struct v4l2_format *f)
744 struct fimc_ctx *ctx = priv;
745 struct fimc_dev *fimc = ctx->fimc_dev;
746 struct fimc_frame *frame;
747 struct v4l2_pix_format_mplane *pix;
751 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
754 ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
758 if (vb2_is_streaming(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
761 frame = &ctx->d_frame;
763 pix = &f->fmt.pix_mp;
764 frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
766 err("fimc target format not found\n");
770 for (i = 0; i < frame->fmt->colplanes; i++)
771 frame->payload[i] = pix->plane_fmt[i].sizeimage;
773 /* Output DMA frame pixel size and offsets. */
774 frame->f_width = pix->plane_fmt[0].bytesperline * 8
775 / frame->fmt->depth[0];
776 frame->f_height = pix->height;
777 frame->width = pix->width;
778 frame->height = pix->height;
779 frame->o_width = pix->width;
780 frame->o_height = pix->height;
784 ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
786 ret = sync_capture_fmt(ctx);
790 static int fimc_cap_enum_input(struct file *file, void *priv,
791 struct v4l2_input *i)
793 struct fimc_ctx *ctx = priv;
794 struct s5p_platform_fimc *pldata = ctx->fimc_dev->pdata;
795 struct s5p_fimc_isp_info *isp_info;
797 if (i->index >= pldata->num_clients)
800 isp_info = pldata->isp_info[i->index];
801 if (isp_info == NULL)
804 i->type = V4L2_INPUT_TYPE_CAMERA;
805 strncpy(i->name, isp_info->board_info->type, 32);
809 static int fimc_cap_s_input(struct file *file, void *priv,
812 struct fimc_ctx *ctx = priv;
813 struct fimc_dev *fimc = ctx->fimc_dev;
814 struct s5p_platform_fimc *pdata = fimc->pdata;
816 if (fimc_capture_active(ctx->fimc_dev))
819 if (i >= pdata->num_clients || !pdata->isp_info[i])
822 if (fimc->vid_cap.mipi_sd) {
823 ret = v4l2_subdev_call(fimc->vid_cap.mipi_sd, core, s_power, 0);
825 err("s_power failed: %d", ret);
828 if (fimc->vid_cap.sd) {
829 ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
831 err("s_power failed: %d", ret);
833 clk_disable(fimc->clock[CLK_CAM]);
835 /* Release the attached sensor subdevice. */
836 fimc_subdev_unregister(fimc);
838 if (test_and_clear_bit(ST_SUSPEND, &fimc->state))
839 pm_runtime_get_sync(&fimc->pdev->dev);
841 ret = fimc_isp_subdev_init(fimc, i);
843 err("subdev init failed");
845 if (!test_and_set_bit(ST_SUSPEND, &fimc->state))
846 pm_runtime_put_sync(&fimc->pdev->dev);
851 static int fimc_cap_g_input(struct file *file, void *priv,
854 struct fimc_ctx *ctx = priv;
855 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
857 *i = cap->input_index;
861 static int fimc_cap_streamon(struct file *file, void *priv,
862 enum v4l2_buf_type type)
864 struct fimc_ctx *ctx = priv;
865 struct fimc_dev *fimc = ctx->fimc_dev;
867 if (fimc_capture_active(fimc) ||
868 (!fimc->vid_cap.sd && !fimc->vid_cap.fb_sd))
871 if (!(ctx->state & FIMC_DST_FMT)) {
872 v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
876 return vb2_streamon(&fimc->vid_cap.vbq, type);
879 static int fimc_cap_streamoff(struct file *file, void *priv,
880 enum v4l2_buf_type type)
882 struct fimc_ctx *ctx = priv;
883 struct fimc_dev *fimc = ctx->fimc_dev;
885 return vb2_streamoff(&fimc->vid_cap.vbq, type);
888 static int fimc_cap_reqbufs(struct file *file, void *priv,
889 struct v4l2_requestbuffers *reqbufs)
891 struct fimc_ctx *ctx = priv;
892 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
893 struct fimc_frame *frame;
896 frame = ctx_get_frame(ctx, reqbufs->type);
897 frame->cacheable = ctx->cacheable;
898 vb2_sdvmm_set_cacheable(ctx->fimc_dev->alloc_ctx, frame->cacheable);
900 ret = vb2_reqbufs(&cap->vbq, reqbufs);
902 cap->reqbufs_count = reqbufs->count;
907 static int fimc_cap_querybuf(struct file *file, void *priv,
908 struct v4l2_buffer *buf)
910 struct fimc_ctx *ctx = priv;
911 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
913 return vb2_querybuf(&cap->vbq, buf);
916 static int fimc_cap_qbuf(struct file *file, void *priv,
917 struct v4l2_buffer *buf)
919 struct fimc_ctx *ctx = priv;
920 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
922 return vb2_qbuf(&cap->vbq, buf);
925 static int fimc_cap_dqbuf(struct file *file, void *priv,
926 struct v4l2_buffer *buf)
928 struct fimc_ctx *ctx = priv;
929 return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
930 file->f_flags & O_NONBLOCK);
933 static int fimc_cap_s_ctrl(struct file *file, void *priv,
934 struct v4l2_control *ctrl)
936 struct fimc_ctx *ctx = priv;
939 /* Allow any controls but 90/270 rotation while streaming */
940 if (!fimc_capture_active(ctx->fimc_dev) ||
941 ctrl->id != V4L2_CID_ROTATE ||
942 (ctrl->value != 90 && ctrl->value != 270)) {
943 ret = check_ctrl_val(ctx, ctrl);
945 ret = fimc_s_ctrl(ctx, ctrl);
947 ctx->state |= FIMC_PARAMS;
950 if (ret == -EINVAL && ctx->fimc_dev->vid_cap.sd)
951 ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
956 static int fimc_cap_cropcap(struct file *file, void *fh,
957 struct v4l2_cropcap *cr)
959 struct fimc_frame *f;
960 struct fimc_ctx *ctx = fh;
962 if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
969 cr->bounds.width = f->o_width;
970 cr->bounds.height = f->o_height;
971 cr->defrect = cr->bounds;
976 static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
978 struct fimc_frame *f;
979 struct fimc_ctx *ctx = file->private_data;
983 cr->c.left = f->offs_h;
984 cr->c.top = f->offs_v;
985 cr->c.width = f->width;
986 cr->c.height = f->height;
991 static int fimc_cap_s_crop(struct file *file, void *fh,
992 struct v4l2_crop *cr)
994 struct fimc_frame *f;
995 struct fimc_ctx *ctx = file->private_data;
996 struct fimc_dev *fimc = ctx->fimc_dev;
999 if (fimc_capture_active(fimc))
1002 ret = fimc_try_crop(ctx, cr);
1006 if (!(ctx->state & FIMC_DST_FMT)) {
1007 v4l2_err(&fimc->vid_cap.v4l2_dev,
1008 "Capture color format not set\n");
1009 return -EINVAL; /* TODO: make sure this is the right value */
1013 /* Check for the pixel scaling ratio when cropping input image. */
1014 ret = fimc_check_scaler_ratio(ctx, cr->c.width, cr->c.height,
1015 ctx->d_frame.width, ctx->d_frame.height,
1018 v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range");
1022 f->offs_h = cr->c.left;
1023 f->offs_v = cr->c.top;
1024 f->width = cr->c.width;
1025 f->height = cr->c.height;
1030 static int fimc_cap_g_parm(struct file *filp, void *priv,
1031 struct v4l2_streamparm *sp)
1033 struct fimc_ctx *ctx = priv;
1034 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
1037 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1038 sp->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1040 ret = v4l2_subdev_call(cap->sd, video, g_parm, sp);
1041 return ret ? -EINVAL : 0;
1044 static int fimc_cap_s_parm(struct file *filp, void *priv,
1045 struct v4l2_streamparm *sp)
1047 struct fimc_ctx *ctx = priv;
1048 struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
1051 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1054 ret = v4l2_subdev_call(cap->sd, video, s_parm, sp);
1055 return ret ? -EINVAL : 0;
1058 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1059 .vidioc_querycap = fimc_vidioc_querycap_capture,
1061 .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
1062 .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane,
1063 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1064 .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane,
1066 .vidioc_reqbufs = fimc_cap_reqbufs,
1067 .vidioc_querybuf = fimc_cap_querybuf,
1069 .vidioc_qbuf = fimc_cap_qbuf,
1070 .vidioc_dqbuf = fimc_cap_dqbuf,
1072 .vidioc_streamon = fimc_cap_streamon,
1073 .vidioc_streamoff = fimc_cap_streamoff,
1075 .vidioc_queryctrl = fimc_vidioc_queryctrl,
1076 .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
1077 .vidioc_s_ctrl = fimc_cap_s_ctrl,
1079 .vidioc_g_crop = fimc_cap_g_crop,
1080 .vidioc_s_crop = fimc_cap_s_crop,
1081 .vidioc_cropcap = fimc_cap_cropcap,
1083 .vidioc_enum_input = fimc_cap_enum_input,
1084 .vidioc_s_input = fimc_cap_s_input,
1085 .vidioc_g_input = fimc_cap_g_input,
1087 .vidioc_g_parm = fimc_cap_g_parm,
1088 .vidioc_s_parm = fimc_cap_s_parm,
1091 /* fimc->lock must be already initialized */
1092 int fimc_register_capture_device(struct fimc_dev *fimc)
1094 struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
1095 struct video_device *vfd;
1096 struct fimc_vid_cap *vid_cap;
1097 struct fimc_ctx *ctx;
1098 struct v4l2_format f;
1099 struct fimc_frame *fr;
1100 struct vb2_queue *q;
1103 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1107 ctx->fimc_dev = fimc;
1108 ctx->in_path = FIMC_CAMERA;
1109 ctx->out_path = FIMC_DMA;
1110 ctx->state = FIMC_CTX_CAP;
1112 /* Default format of the output frames */
1113 f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
1115 fr->fmt = find_format(&f, FMT_FLAGS_M2M);
1116 fr->width = fr->f_width = fr->o_width = 640;
1117 fr->height = fr->f_height = fr->o_height = 480;
1119 if (!v4l2_dev->name[0])
1120 snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
1121 "%s.capture", dev_name(&fimc->pdev->dev));
1123 ret = v4l2_device_register(NULL, v4l2_dev);
1127 vfd = video_device_alloc();
1129 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1133 snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
1134 dev_name(&fimc->pdev->dev));
1136 vfd->fops = &fimc_capture_fops;
1137 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1139 vfd->release = video_device_release;
1141 vfd->lock = &fimc->lock;
1143 video_set_drvdata(vfd, fimc);
1145 vid_cap = &fimc->vid_cap;
1147 vid_cap->active_buf_cnt = 0;
1148 vid_cap->reqbufs_count = 0;
1149 vid_cap->refcnt = 0;
1151 vid_cap->fb_sd = NULL;
1152 vid_cap->mipi_sd = NULL;
1154 /* Default color format for 4BA image sensor */
1155 /* vid_cap->fmt.code = V4L2_MBUS_FMT_UYVY8_2X8; */
1157 /* Default color format for 4EA image sensor */
1158 vid_cap->fmt.code = V4L2_MBUS_FMT_VYUY8_2X8;
1160 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1161 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1162 spin_lock_init(&ctx->slock);
1165 q = &fimc->vid_cap.vbq;
1166 memset(q, 0, sizeof(*q));
1167 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1168 q->io_modes = VB2_MMAP | VB2_USERPTR;
1169 q->drv_priv = fimc->vid_cap.ctx;
1170 q->ops = &fimc_capture_qops;
1171 #ifdef CONFIG_VIDEOBUF2_SDVMM
1172 q->mem_ops = &vb2_sdvmm_memops;
1174 q->mem_ops = &vb2_cma_memops;
1176 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1180 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1182 v4l2_err(v4l2_dev, "Failed to register video device\n");
1187 "FIMC capture driver registered as /dev/video%d\n",
1193 video_device_release(vfd);
1195 v4l2_device_unregister(v4l2_dev);
1197 dev_err(&fimc->pdev->dev, "failed to install\n");
1201 void fimc_unregister_capture_device(struct fimc_dev *fimc)
1203 struct fimc_vid_cap *capture = &fimc->vid_cap;
1206 video_unregister_device(capture->vfd);
1208 kfree(capture->ctx);