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
);
43 return ERR_PTR(-ENOMEM
);
45 sd
= v4l2_i2c_new_subdev_board(&vid_cap
->v4l2_dev
, i2c_adap
,
46 isp_info
->board_info
, NULL
);
48 v4l2_err(&vid_cap
->v4l2_dev
, "failed to acquire subdev\n");
52 v4l2_info(&vid_cap
->v4l2_dev
, "subdevice %s registered successfuly\n",
53 isp_info
->board_info
->type
);
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. */
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
);
74 vid_cap
->input_index
= -1;
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
;
93 for (i
= 0; i
< pdata
->num_clients
; ++i
) {
94 isp_info
= &pdata
->isp_info
[i
];
96 if (index
>= 0 && i
!= index
)
99 sd
= fimc_subdev_register(fimc
, isp_info
);
100 if (!IS_ERR_OR_NULL(sd
)) {
102 vid_cap
->input_index
= i
;
108 vid_cap
->input_index
= -1;
110 v4l2_err(&vid_cap
->v4l2_dev
, "fimc%d: sensor attach failed\n",
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
;
121 if (index
>= pdata
->num_clients
)
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
]);
133 ret
= fimc_subdev_attach(fimc
, index
);
137 ret
= fimc_hw_set_camera_polarity(fimc
, isp_info
);
141 ret
= v4l2_subdev_call(fimc
->vid_cap
.sd
, core
, s_power
, 1);
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",
154 static int fimc_stop_capture(struct fimc_dev
*fimc
)
157 struct fimc_vid_cap
*cap
;
158 struct fimc_vid_buffer
*buf
;
160 cap
= &fimc
->vid_cap
;
162 if (!fimc_capture_active(fimc
))
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
);
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
;
208 ret
= v4l2_subdev_call(fimc
->vid_cap
.sd
, video
, s_stream
, 1);
209 if (ret
&& ret
!= -ENOIOCTLCMD
)
212 ret
= fimc_prepare_config(ctx
, ctx
->state
);
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
);
224 err("Scaler setup error");
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
);
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
))
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
)
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
[],
271 struct fimc_ctx
*ctx
= vq
->drv_priv
;
272 struct fimc_fmt
*fmt
= ctx
->d_frame
.fmt
;
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
;
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
;
295 if (!ctx
->d_frame
.fmt
|| vq
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
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
);
307 vb2_set_plane_payload(vb
, i
, size
);
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
;
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 :
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;
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
);
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
))
384 if (++fimc
->vid_cap
.refcnt
== 1) {
385 ret
= fimc_isp_subdev_init(fimc
, 0);
387 fimc
->vid_cap
.refcnt
--;
392 file
->private_data
= fimc
->vid_cap
.ctx
;
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
);
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
;
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
;
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");
475 dbg("w: %d, h: %d, code= %d", fmt
->width
, fmt
->height
, fmt
->code
);
477 frame
->fmt
= find_mbus_format(fmt
, FMT_FLAGS_CAM
);
479 err("fimc source format not found\n");
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
;
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
;
505 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
508 ret
= fimc_vidioc_try_fmt_mplane(file
, priv
, f
);
512 if (vb2_is_busy(&fimc
->vid_cap
.vbq
) || fimc_capture_active(fimc
))
515 frame
= &ctx
->d_frame
;
517 pix
= &f
->fmt
.pix_mp
;
518 frame
->fmt
= find_format(f
, FMT_FLAGS_M2M
| FMT_FLAGS_CAM
);
520 err("fimc target format not found\n");
524 for (i
= 0; i
< frame
->fmt
->colplanes
; 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
;
540 ctx
->state
|= (FIMC_PARAMS
| FIMC_DST_FMT
);
542 ret
= sync_capture_fmt(ctx
);
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
)
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);
563 static int fimc_cap_s_input(struct file
*file
, void *priv
,
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
))
573 if (i
>= pdata
->num_clients
)
577 if (fimc
->vid_cap
.sd
) {
578 int ret
= v4l2_subdev_call(fimc
->vid_cap
.sd
, core
, s_power
, 0);
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
,
594 struct fimc_ctx
*ctx
= priv
;
595 struct fimc_vid_cap
*cap
= &ctx
->fimc_dev
->vid_cap
;
597 *i
= cap
->input_index
;
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
)
610 if (!(ctx
->state
& FIMC_DST_FMT
)) {
611 v4l2_err(&fimc
->vid_cap
.v4l2_dev
, "Format is not set\n");
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
;
635 ret
= vb2_reqbufs(&cap
->vbq
, reqbufs
);
637 cap
->reqbufs_count
= reqbufs
->count
;
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
;
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
);
679 ret
= fimc_s_ctrl(ctx
, ctrl
);
681 ctx
->state
|= FIMC_PARAMS
;
685 ret
= v4l2_subdev_call(ctx
->fimc_dev
->vid_cap
.sd
,
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
)
703 cr
->bounds
.width
= f
->o_width
;
704 cr
->bounds
.height
= f
->o_height
;
705 cr
->defrect
= cr
->bounds
;
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
;
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
;
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
;
733 if (fimc_capture_active(fimc
))
736 ret
= fimc_try_crop(ctx
, cr
);
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 */
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
,
752 v4l2_err(&fimc
->vid_cap
.v4l2_dev
, "Out of the scaler range\n");
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
;
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
;
807 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
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
;
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
);
831 vfd
= video_device_alloc();
833 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
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
;
843 vfd
->release
= video_device_release
;
844 vfd
->lock
= &fimc
->lock
;
845 video_set_drvdata(vfd
, fimc
);
847 vid_cap
= &fimc
->vid_cap
;
849 vid_cap
->active_buf_cnt
= 0;
850 vid_cap
->reqbufs_count
= 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
);
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
);
871 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, -1);
873 v4l2_err(v4l2_dev
, "Failed to register video device\n");
878 "FIMC capture driver registered as /dev/video%d\n",
884 video_device_release(vfd
);
886 v4l2_device_unregister(v4l2_dev
);
889 dev_err(&fimc
->pdev
->dev
, "failed to install\n");
893 void fimc_unregister_capture_device(struct fimc_dev
*fimc
)
895 struct fimc_vid_cap
*capture
= &fimc
->vid_cap
;
898 video_unregister_device(capture
->vfd
);