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-dma-contig.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 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 s5p_platform_fimc
*pdata
= fimc
->pdata
;
90 struct s5p_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
, unsigned int index
)
118 struct s5p_fimc_isp_info
*isp_info
;
121 if (index
>= FIMC_MAX_CAMIF_CLIENTS
)
124 isp_info
= fimc
->pdata
->isp_info
[index
];
128 if (isp_info
->clk_frequency
)
129 clk_set_rate(fimc
->clock
[CLK_CAM
], isp_info
->clk_frequency
);
131 ret
= clk_enable(fimc
->clock
[CLK_CAM
]);
135 ret
= fimc_subdev_attach(fimc
, index
);
139 ret
= fimc_hw_set_camera_polarity(fimc
, isp_info
);
143 ret
= v4l2_subdev_call(fimc
->vid_cap
.sd
, core
, s_power
, 1);
147 /* enabling power failed so unregister subdev */
148 fimc_subdev_unregister(fimc
);
150 v4l2_err(&fimc
->vid_cap
.v4l2_dev
, "ISP initialization failed: %d\n",
157 * At least one buffer on the pending_buf_q queue is required.
158 * Locking: The caller holds fimc->slock spinlock.
160 int fimc_vid_cap_buf_queue(struct fimc_dev
*fimc
,
161 struct fimc_vid_buffer
*fimc_vb
)
163 struct fimc_vid_cap
*cap
= &fimc
->vid_cap
;
164 struct fimc_ctx
*ctx
= cap
->ctx
;
167 BUG_ON(!fimc
|| !fimc_vb
);
169 ret
= fimc_prepare_addr(ctx
, &fimc_vb
->vb
, &ctx
->d_frame
,
174 if (test_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
175 fimc_pending_queue_add(cap
, fimc_vb
);
177 /* Setup the buffer directly for processing. */
178 int buf_id
= (cap
->reqbufs_count
== 1) ? -1 : cap
->buf_index
;
179 fimc_hw_set_output_addr(fimc
, &fimc_vb
->paddr
, buf_id
);
181 fimc_vb
->index
= cap
->buf_index
;
182 active_queue_add(cap
, fimc_vb
);
184 if (++cap
->buf_index
>= FIMC_MAX_OUT_BUFS
)
190 static int fimc_stop_capture(struct fimc_dev
*fimc
)
193 struct fimc_vid_cap
*cap
;
194 struct fimc_vid_buffer
*buf
;
196 cap
= &fimc
->vid_cap
;
198 if (!fimc_capture_active(fimc
))
201 spin_lock_irqsave(&fimc
->slock
, flags
);
202 set_bit(ST_CAPT_SHUT
, &fimc
->state
);
203 fimc_deactivate_capture(fimc
);
204 spin_unlock_irqrestore(&fimc
->slock
, flags
);
206 wait_event_timeout(fimc
->irq_queue
,
207 test_bit(ST_CAPT_SHUT
, &fimc
->state
),
208 FIMC_SHUTDOWN_TIMEOUT
);
210 v4l2_subdev_call(cap
->sd
, video
, s_stream
, 0);
212 spin_lock_irqsave(&fimc
->slock
, flags
);
213 fimc
->state
&= ~(1 << ST_CAPT_RUN
| 1 << ST_CAPT_PEND
|
214 1 << ST_CAPT_STREAM
);
216 fimc
->vid_cap
.active_buf_cnt
= 0;
218 /* Release buffers that were enqueued in the driver by videobuf2. */
219 while (!list_empty(&cap
->pending_buf_q
)) {
220 buf
= pending_queue_pop(cap
);
221 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
224 while (!list_empty(&cap
->active_buf_q
)) {
225 buf
= active_queue_pop(cap
);
226 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
229 spin_unlock_irqrestore(&fimc
->slock
, flags
);
231 dbg("state: 0x%lx", fimc
->state
);
235 static int start_streaming(struct vb2_queue
*q
)
237 struct fimc_ctx
*ctx
= q
->drv_priv
;
238 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
239 struct s5p_fimc_isp_info
*isp_info
;
240 struct samsung_fimc_variant
*variant
= ctx
->fimc_dev
->variant
;
243 ret
= v4l2_subdev_call(fimc
->vid_cap
.sd
, video
, s_stream
, 1);
244 if (ret
&& ret
!= -ENOIOCTLCMD
)
247 ret
= fimc_prepare_config(ctx
, ctx
->state
);
251 isp_info
= fimc
->pdata
->isp_info
[fimc
->vid_cap
.input_index
];
252 fimc_hw_set_camera_type(fimc
, isp_info
);
253 fimc_hw_set_camera_source(fimc
, isp_info
);
254 fimc_hw_set_camera_offset(fimc
, &ctx
->s_frame
);
256 if (ctx
->state
& FIMC_PARAMS
) {
257 ret
= fimc_set_scaler_info(ctx
);
259 err("Scaler setup error");
262 fimc_hw_set_input_path(ctx
);
263 fimc_hw_set_prescaler(ctx
);
264 if (variant
->has_mainscaler_ext
)
265 fimc_hw_set_mainscaler_ext(ctx
);
267 fimc_hw_set_mainscaler(ctx
);
268 fimc_hw_set_target_format(ctx
);
269 fimc_hw_set_rotation(ctx
);
270 fimc_hw_set_effect(ctx
);
273 fimc_hw_set_output_path(ctx
);
274 fimc_hw_set_out_dma(ctx
);
276 INIT_LIST_HEAD(&fimc
->vid_cap
.pending_buf_q
);
277 INIT_LIST_HEAD(&fimc
->vid_cap
.active_buf_q
);
278 fimc
->vid_cap
.active_buf_cnt
= 0;
279 fimc
->vid_cap
.frame_count
= 0;
280 fimc
->vid_cap
.buf_index
= fimc_hw_get_frame_index(fimc
);
282 set_bit(ST_CAPT_PEND
, &fimc
->state
);
287 static int stop_streaming(struct vb2_queue
*q
)
289 struct fimc_ctx
*ctx
= q
->drv_priv
;
290 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
293 spin_lock_irqsave(&fimc
->slock
, flags
);
294 if (!fimc_capture_running(fimc
) && !fimc_capture_pending(fimc
)) {
295 spin_unlock_irqrestore(&fimc
->slock
, flags
);
298 spin_unlock_irqrestore(&fimc
->slock
, flags
);
300 return fimc_stop_capture(fimc
);
303 static unsigned int get_plane_size(struct fimc_frame
*fr
, unsigned int plane
)
305 if (!fr
|| plane
>= fr
->fmt
->memplanes
)
308 dbg("%s: w: %d. h: %d. depth[%d]: %d",
309 __func__
, fr
->width
, fr
->height
, plane
, fr
->fmt
->depth
[plane
]);
311 return fr
->f_width
* fr
->f_height
* fr
->fmt
->depth
[plane
] / 8;
315 static int queue_setup(struct vb2_queue
*vq
, unsigned int *num_buffers
,
316 unsigned int *num_planes
, unsigned long sizes
[],
319 struct fimc_ctx
*ctx
= vq
->drv_priv
;
320 struct fimc_fmt
*fmt
= ctx
->d_frame
.fmt
;
326 *num_planes
= fmt
->memplanes
;
328 dbg("%s, buffer count=%d, plane count=%d",
329 __func__
, *num_buffers
, *num_planes
);
331 for (i
= 0; i
< fmt
->memplanes
; i
++) {
332 sizes
[i
] = get_plane_size(&ctx
->d_frame
, i
);
333 dbg("plane: %u, plane_size: %lu", i
, sizes
[i
]);
334 allocators
[i
] = ctx
->fimc_dev
->alloc_ctx
;
340 static int buffer_init(struct vb2_buffer
*vb
)
346 static int buffer_prepare(struct vb2_buffer
*vb
)
348 struct vb2_queue
*vq
= vb
->vb2_queue
;
349 struct fimc_ctx
*ctx
= vq
->drv_priv
;
350 struct v4l2_device
*v4l2_dev
= &ctx
->fimc_dev
->m2m
.v4l2_dev
;
353 if (!ctx
->d_frame
.fmt
|| vq
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
356 for (i
= 0; i
< ctx
->d_frame
.fmt
->memplanes
; i
++) {
357 unsigned long size
= get_plane_size(&ctx
->d_frame
, i
);
359 if (vb2_plane_size(vb
, i
) < size
) {
360 v4l2_err(v4l2_dev
, "User buffer too small (%ld < %ld)\n",
361 vb2_plane_size(vb
, i
), size
);
365 vb2_set_plane_payload(vb
, i
, size
);
371 static void buffer_queue(struct vb2_buffer
*vb
)
373 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vb
->vb2_queue
);
374 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
375 struct fimc_vid_buffer
*buf
376 = container_of(vb
, struct fimc_vid_buffer
, vb
);
377 struct fimc_vid_cap
*vid_cap
= &fimc
->vid_cap
;
380 spin_lock_irqsave(&fimc
->slock
, flags
);
381 fimc_vid_cap_buf_queue(fimc
, buf
);
383 dbg("active_buf_cnt: %d", fimc
->vid_cap
.active_buf_cnt
);
385 if (vid_cap
->active_buf_cnt
>= vid_cap
->reqbufs_count
||
386 vid_cap
->active_buf_cnt
>= FIMC_MAX_OUT_BUFS
) {
387 if (!test_and_set_bit(ST_CAPT_STREAM
, &fimc
->state
)) {
388 fimc_activate_capture(ctx
);
392 spin_unlock_irqrestore(&fimc
->slock
, flags
);
395 static void fimc_lock(struct vb2_queue
*vq
)
397 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
398 mutex_lock(&ctx
->fimc_dev
->lock
);
401 static void fimc_unlock(struct vb2_queue
*vq
)
403 struct fimc_ctx
*ctx
= vb2_get_drv_priv(vq
);
404 mutex_unlock(&ctx
->fimc_dev
->lock
);
407 static struct vb2_ops fimc_capture_qops
= {
408 .queue_setup
= queue_setup
,
409 .buf_prepare
= buffer_prepare
,
410 .buf_queue
= buffer_queue
,
411 .buf_init
= buffer_init
,
412 .wait_prepare
= fimc_unlock
,
413 .wait_finish
= fimc_lock
,
414 .start_streaming
= start_streaming
,
415 .stop_streaming
= stop_streaming
,
418 static int fimc_capture_open(struct file
*file
)
420 struct fimc_dev
*fimc
= video_drvdata(file
);
423 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
425 /* Return if the corresponding video mem2mem node is already opened. */
426 if (fimc_m2m_active(fimc
))
429 if (++fimc
->vid_cap
.refcnt
== 1) {
430 ret
= fimc_isp_subdev_init(fimc
, 0);
432 fimc
->vid_cap
.refcnt
--;
437 file
->private_data
= fimc
->vid_cap
.ctx
;
442 static int fimc_capture_close(struct file
*file
)
444 struct fimc_dev
*fimc
= video_drvdata(file
);
446 dbg("pid: %d, state: 0x%lx", task_pid_nr(current
), fimc
->state
);
448 if (--fimc
->vid_cap
.refcnt
== 0) {
449 fimc_stop_capture(fimc
);
450 vb2_queue_release(&fimc
->vid_cap
.vbq
);
452 v4l2_err(&fimc
->vid_cap
.v4l2_dev
, "releasing ISP\n");
454 v4l2_subdev_call(fimc
->vid_cap
.sd
, core
, s_power
, 0);
455 clk_disable(fimc
->clock
[CLK_CAM
]);
456 fimc_subdev_unregister(fimc
);
462 static unsigned int fimc_capture_poll(struct file
*file
,
463 struct poll_table_struct
*wait
)
465 struct fimc_ctx
*ctx
= file
->private_data
;
466 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
468 return vb2_poll(&fimc
->vid_cap
.vbq
, file
, wait
);
471 static int fimc_capture_mmap(struct file
*file
, struct vm_area_struct
*vma
)
473 struct fimc_ctx
*ctx
= file
->private_data
;
474 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
476 return vb2_mmap(&fimc
->vid_cap
.vbq
, vma
);
479 /* video device file operations */
480 static const struct v4l2_file_operations fimc_capture_fops
= {
481 .owner
= THIS_MODULE
,
482 .open
= fimc_capture_open
,
483 .release
= fimc_capture_close
,
484 .poll
= fimc_capture_poll
,
485 .unlocked_ioctl
= video_ioctl2
,
486 .mmap
= fimc_capture_mmap
,
489 static int fimc_vidioc_querycap_capture(struct file
*file
, void *priv
,
490 struct v4l2_capability
*cap
)
492 struct fimc_ctx
*ctx
= file
->private_data
;
493 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
495 strncpy(cap
->driver
, fimc
->pdev
->name
, sizeof(cap
->driver
) - 1);
496 strncpy(cap
->card
, fimc
->pdev
->name
, sizeof(cap
->card
) - 1);
497 cap
->bus_info
[0] = 0;
498 cap
->version
= KERNEL_VERSION(1, 0, 0);
499 cap
->capabilities
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_CAPTURE
|
500 V4L2_CAP_VIDEO_CAPTURE_MPLANE
;
505 /* Synchronize formats of the camera interface input and attached sensor. */
506 static int sync_capture_fmt(struct fimc_ctx
*ctx
)
508 struct fimc_frame
*frame
= &ctx
->s_frame
;
509 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
510 struct v4l2_mbus_framefmt
*fmt
= &fimc
->vid_cap
.fmt
;
513 fmt
->width
= ctx
->d_frame
.o_width
;
514 fmt
->height
= ctx
->d_frame
.o_height
;
516 ret
= v4l2_subdev_call(fimc
->vid_cap
.sd
, video
, s_mbus_fmt
, fmt
);
517 if (ret
== -ENOIOCTLCMD
) {
518 err("s_mbus_fmt failed");
521 dbg("w: %d, h: %d, code= %d", fmt
->width
, fmt
->height
, fmt
->code
);
523 frame
->fmt
= find_mbus_format(fmt
, FMT_FLAGS_CAM
);
525 err("fimc source format not found\n");
529 frame
->f_width
= fmt
->width
;
530 frame
->f_height
= fmt
->height
;
531 frame
->width
= fmt
->width
;
532 frame
->height
= fmt
->height
;
533 frame
->o_width
= fmt
->width
;
534 frame
->o_height
= fmt
->height
;
541 static int fimc_cap_s_fmt_mplane(struct file
*file
, void *priv
,
542 struct v4l2_format
*f
)
544 struct fimc_ctx
*ctx
= priv
;
545 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
546 struct fimc_frame
*frame
;
547 struct v4l2_pix_format_mplane
*pix
;
551 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
554 ret
= fimc_vidioc_try_fmt_mplane(file
, priv
, f
);
558 if (vb2_is_streaming(&fimc
->vid_cap
.vbq
) || fimc_capture_active(fimc
))
561 frame
= &ctx
->d_frame
;
563 pix
= &f
->fmt
.pix_mp
;
564 frame
->fmt
= find_format(f
, FMT_FLAGS_M2M
| FMT_FLAGS_CAM
);
566 err("fimc target format not found\n");
570 for (i
= 0; i
< frame
->fmt
->colplanes
; i
++)
571 frame
->payload
[i
] = pix
->plane_fmt
[i
].bytesperline
* pix
->height
;
573 /* Output DMA frame pixel size and offsets. */
574 frame
->f_width
= pix
->plane_fmt
[0].bytesperline
* 8
575 / frame
->fmt
->depth
[0];
576 frame
->f_height
= pix
->height
;
577 frame
->width
= pix
->width
;
578 frame
->height
= pix
->height
;
579 frame
->o_width
= pix
->width
;
580 frame
->o_height
= pix
->height
;
584 ctx
->state
|= (FIMC_PARAMS
| FIMC_DST_FMT
);
586 ret
= sync_capture_fmt(ctx
);
590 static int fimc_cap_enum_input(struct file
*file
, void *priv
,
591 struct v4l2_input
*i
)
593 struct fimc_ctx
*ctx
= priv
;
594 struct s5p_platform_fimc
*pldata
= ctx
->fimc_dev
->pdata
;
595 struct s5p_fimc_isp_info
*isp_info
;
597 if (i
->index
>= FIMC_MAX_CAMIF_CLIENTS
)
600 isp_info
= pldata
->isp_info
[i
->index
];
601 if (isp_info
== NULL
)
604 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
605 strncpy(i
->name
, isp_info
->board_info
->type
, 32);
609 static int fimc_cap_s_input(struct file
*file
, void *priv
,
612 struct fimc_ctx
*ctx
= priv
;
613 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
614 struct s5p_platform_fimc
*pdata
= fimc
->pdata
;
616 if (fimc_capture_active(ctx
->fimc_dev
))
619 if (i
>= FIMC_MAX_CAMIF_CLIENTS
|| !pdata
->isp_info
[i
])
623 if (fimc
->vid_cap
.sd
) {
624 int ret
= v4l2_subdev_call(fimc
->vid_cap
.sd
, core
, s_power
, 0);
626 err("s_power failed: %d", ret
);
628 clk_disable(fimc
->clock
[CLK_CAM
]);
631 /* Release the attached sensor subdevice. */
632 fimc_subdev_unregister(fimc
);
634 return fimc_isp_subdev_init(fimc
, i
);
637 static int fimc_cap_g_input(struct file
*file
, void *priv
,
640 struct fimc_ctx
*ctx
= priv
;
641 struct fimc_vid_cap
*cap
= &ctx
->fimc_dev
->vid_cap
;
643 *i
= cap
->input_index
;
647 static int fimc_cap_streamon(struct file
*file
, void *priv
,
648 enum v4l2_buf_type type
)
650 struct fimc_ctx
*ctx
= priv
;
651 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
653 if (fimc_capture_active(fimc
) || !fimc
->vid_cap
.sd
)
656 if (!(ctx
->state
& FIMC_DST_FMT
)) {
657 v4l2_err(&fimc
->vid_cap
.v4l2_dev
, "Format is not set\n");
661 return vb2_streamon(&fimc
->vid_cap
.vbq
, type
);
664 static int fimc_cap_streamoff(struct file
*file
, void *priv
,
665 enum v4l2_buf_type type
)
667 struct fimc_ctx
*ctx
= priv
;
668 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
670 return vb2_streamoff(&fimc
->vid_cap
.vbq
, type
);
673 static int fimc_cap_reqbufs(struct file
*file
, void *priv
,
674 struct v4l2_requestbuffers
*reqbufs
)
676 struct fimc_ctx
*ctx
= priv
;
677 struct fimc_vid_cap
*cap
= &ctx
->fimc_dev
->vid_cap
;
681 ret
= vb2_reqbufs(&cap
->vbq
, reqbufs
);
683 cap
->reqbufs_count
= reqbufs
->count
;
688 static int fimc_cap_querybuf(struct file
*file
, void *priv
,
689 struct v4l2_buffer
*buf
)
691 struct fimc_ctx
*ctx
= priv
;
692 struct fimc_vid_cap
*cap
= &ctx
->fimc_dev
->vid_cap
;
694 return vb2_querybuf(&cap
->vbq
, buf
);
697 static int fimc_cap_qbuf(struct file
*file
, void *priv
,
698 struct v4l2_buffer
*buf
)
700 struct fimc_ctx
*ctx
= priv
;
701 struct fimc_vid_cap
*cap
= &ctx
->fimc_dev
->vid_cap
;
702 return vb2_qbuf(&cap
->vbq
, buf
);
705 static int fimc_cap_dqbuf(struct file
*file
, void *priv
,
706 struct v4l2_buffer
*buf
)
708 struct fimc_ctx
*ctx
= priv
;
709 return vb2_dqbuf(&ctx
->fimc_dev
->vid_cap
.vbq
, buf
,
710 file
->f_flags
& O_NONBLOCK
);
713 static int fimc_cap_s_ctrl(struct file
*file
, void *priv
,
714 struct v4l2_control
*ctrl
)
716 struct fimc_ctx
*ctx
= priv
;
719 /* Allow any controls but 90/270 rotation while streaming */
720 if (!fimc_capture_active(ctx
->fimc_dev
) ||
721 ctrl
->id
!= V4L2_CID_ROTATE
||
722 (ctrl
->value
!= 90 && ctrl
->value
!= 270)) {
723 ret
= check_ctrl_val(ctx
, ctrl
);
725 ret
= fimc_s_ctrl(ctx
, ctrl
);
727 ctx
->state
|= FIMC_PARAMS
;
731 ret
= v4l2_subdev_call(ctx
->fimc_dev
->vid_cap
.sd
,
736 static int fimc_cap_cropcap(struct file
*file
, void *fh
,
737 struct v4l2_cropcap
*cr
)
739 struct fimc_frame
*f
;
740 struct fimc_ctx
*ctx
= fh
;
742 if (cr
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
749 cr
->bounds
.width
= f
->o_width
;
750 cr
->bounds
.height
= f
->o_height
;
751 cr
->defrect
= cr
->bounds
;
756 static int fimc_cap_g_crop(struct file
*file
, void *fh
, struct v4l2_crop
*cr
)
758 struct fimc_frame
*f
;
759 struct fimc_ctx
*ctx
= file
->private_data
;
763 cr
->c
.left
= f
->offs_h
;
764 cr
->c
.top
= f
->offs_v
;
765 cr
->c
.width
= f
->width
;
766 cr
->c
.height
= f
->height
;
771 static int fimc_cap_s_crop(struct file
*file
, void *fh
,
772 struct v4l2_crop
*cr
)
774 struct fimc_frame
*f
;
775 struct fimc_ctx
*ctx
= file
->private_data
;
776 struct fimc_dev
*fimc
= ctx
->fimc_dev
;
779 if (fimc_capture_active(fimc
))
782 ret
= fimc_try_crop(ctx
, cr
);
786 if (!(ctx
->state
& FIMC_DST_FMT
)) {
787 v4l2_err(&fimc
->vid_cap
.v4l2_dev
,
788 "Capture color format not set\n");
789 return -EINVAL
; /* TODO: make sure this is the right value */
793 /* Check for the pixel scaling ratio when cropping input image. */
794 ret
= fimc_check_scaler_ratio(&cr
->c
, &ctx
->d_frame
);
796 v4l2_err(&fimc
->vid_cap
.v4l2_dev
, "Out of the scaler range");
800 f
->offs_h
= cr
->c
.left
;
801 f
->offs_v
= cr
->c
.top
;
802 f
->width
= cr
->c
.width
;
803 f
->height
= cr
->c
.height
;
809 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops
= {
810 .vidioc_querycap
= fimc_vidioc_querycap_capture
,
812 .vidioc_enum_fmt_vid_cap_mplane
= fimc_vidioc_enum_fmt_mplane
,
813 .vidioc_try_fmt_vid_cap_mplane
= fimc_vidioc_try_fmt_mplane
,
814 .vidioc_s_fmt_vid_cap_mplane
= fimc_cap_s_fmt_mplane
,
815 .vidioc_g_fmt_vid_cap_mplane
= fimc_vidioc_g_fmt_mplane
,
817 .vidioc_reqbufs
= fimc_cap_reqbufs
,
818 .vidioc_querybuf
= fimc_cap_querybuf
,
820 .vidioc_qbuf
= fimc_cap_qbuf
,
821 .vidioc_dqbuf
= fimc_cap_dqbuf
,
823 .vidioc_streamon
= fimc_cap_streamon
,
824 .vidioc_streamoff
= fimc_cap_streamoff
,
826 .vidioc_queryctrl
= fimc_vidioc_queryctrl
,
827 .vidioc_g_ctrl
= fimc_vidioc_g_ctrl
,
828 .vidioc_s_ctrl
= fimc_cap_s_ctrl
,
830 .vidioc_g_crop
= fimc_cap_g_crop
,
831 .vidioc_s_crop
= fimc_cap_s_crop
,
832 .vidioc_cropcap
= fimc_cap_cropcap
,
834 .vidioc_enum_input
= fimc_cap_enum_input
,
835 .vidioc_s_input
= fimc_cap_s_input
,
836 .vidioc_g_input
= fimc_cap_g_input
,
839 /* fimc->lock must be already initialized */
840 int fimc_register_capture_device(struct fimc_dev
*fimc
)
842 struct v4l2_device
*v4l2_dev
= &fimc
->vid_cap
.v4l2_dev
;
843 struct video_device
*vfd
;
844 struct fimc_vid_cap
*vid_cap
;
845 struct fimc_ctx
*ctx
;
846 struct v4l2_format f
;
847 struct fimc_frame
*fr
;
851 ctx
= kzalloc(sizeof *ctx
, GFP_KERNEL
);
855 ctx
->fimc_dev
= fimc
;
856 ctx
->in_path
= FIMC_CAMERA
;
857 ctx
->out_path
= FIMC_DMA
;
858 ctx
->state
= FIMC_CTX_CAP
;
860 /* Default format of the output frames */
861 f
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_RGB32
;
863 fr
->fmt
= find_format(&f
, FMT_FLAGS_M2M
);
864 fr
->width
= fr
->f_width
= fr
->o_width
= 640;
865 fr
->height
= fr
->f_height
= fr
->o_height
= 480;
867 if (!v4l2_dev
->name
[0])
868 snprintf(v4l2_dev
->name
, sizeof(v4l2_dev
->name
),
869 "%s.capture", dev_name(&fimc
->pdev
->dev
));
871 ret
= v4l2_device_register(NULL
, v4l2_dev
);
875 vfd
= video_device_alloc();
877 v4l2_err(v4l2_dev
, "Failed to allocate video device\n");
881 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s:cap",
882 dev_name(&fimc
->pdev
->dev
));
884 vfd
->fops
= &fimc_capture_fops
;
885 vfd
->ioctl_ops
= &fimc_capture_ioctl_ops
;
887 vfd
->release
= video_device_release
;
888 vfd
->lock
= &fimc
->lock
;
889 video_set_drvdata(vfd
, fimc
);
891 vid_cap
= &fimc
->vid_cap
;
893 vid_cap
->active_buf_cnt
= 0;
894 vid_cap
->reqbufs_count
= 0;
896 /* Default color format for image sensor */
897 vid_cap
->fmt
.code
= V4L2_MBUS_FMT_YUYV8_2X8
;
899 INIT_LIST_HEAD(&vid_cap
->pending_buf_q
);
900 INIT_LIST_HEAD(&vid_cap
->active_buf_q
);
901 spin_lock_init(&ctx
->slock
);
904 q
= &fimc
->vid_cap
.vbq
;
905 memset(q
, 0, sizeof(*q
));
906 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
907 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
908 q
->drv_priv
= fimc
->vid_cap
.ctx
;
909 q
->ops
= &fimc_capture_qops
;
910 q
->mem_ops
= &vb2_dma_contig_memops
;
911 q
->buf_struct_size
= sizeof(struct fimc_vid_buffer
);
915 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, -1);
917 v4l2_err(v4l2_dev
, "Failed to register video device\n");
922 "FIMC capture driver registered as /dev/video%d\n",
928 video_device_release(vfd
);
930 v4l2_device_unregister(v4l2_dev
);
932 dev_err(&fimc
->pdev
->dev
, "failed to install\n");
936 void fimc_unregister_capture_device(struct fimc_dev
*fimc
)
938 struct fimc_vid_cap
*capture
= &fimc
->vid_cap
;
941 video_unregister_device(capture
->vfd
);