2 * Copyright (C) 2009 Texas Instruments Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 #include <media/v4l2-ioctl.h>
30 #include "vpif_capture.h"
32 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION(VPIF_CAPTURE_VERSION);
36 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
37 #define vpif_dbg(level, debug, fmt, arg...) \
38 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
41 static u32 ch0_numbuffers = 3;
42 static u32 ch1_numbuffers = 3;
43 static u32 ch0_bufsize = 1920 * 1080 * 2;
44 static u32 ch1_bufsize = 720 * 576 * 2;
46 module_param(debug, int, 0644);
47 module_param(ch0_numbuffers, uint, S_IRUGO);
48 module_param(ch1_numbuffers, uint, S_IRUGO);
49 module_param(ch0_bufsize, uint, S_IRUGO);
50 module_param(ch1_bufsize, uint, S_IRUGO);
52 MODULE_PARM_DESC(debug, "Debug level 0-1");
53 MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
54 MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
55 MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
56 MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
58 static struct vpif_config_params config_params = {
62 .min_bufsize[0] = 720 * 480 * 2,
63 .min_bufsize[1] = 720 * 480 * 2,
64 .channel_bufsize[0] = 1920 * 1080 * 2,
65 .channel_bufsize[1] = 720 * 576 * 2,
68 /* global variables */
69 static struct vpif_device vpif_obj = { {NULL} };
70 static struct device *vpif_dev;
71 static void vpif_calculate_offsets(struct channel_obj *ch);
72 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
75 * buffer_prepare : callback function for buffer prepare
76 * @vb: ptr to vb2_buffer
78 * This is the callback function for buffer prepare when vb2_qbuf()
79 * function is called. The buffer is prepared and user space virtual address
80 * or user address is converted into physical address
82 static int vpif_buffer_prepare(struct vb2_buffer *vb)
84 /* Get the file handle object and channel object */
85 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
86 struct vb2_queue *q = vb->vb2_queue;
87 struct channel_obj *ch = fh->channel;
88 struct common_obj *common;
91 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
93 common = &ch->common[VPIF_VIDEO_INDEX];
95 if (vb->state != VB2_BUF_STATE_ACTIVE &&
96 vb->state != VB2_BUF_STATE_PREPARED) {
97 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
98 if (vb2_plane_vaddr(vb, 0) &&
99 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
101 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
104 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
105 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
106 !IS_ALIGNED((addr + common->ctop_off), 8) ||
107 !IS_ALIGNED((addr + common->cbtm_off), 8))
113 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
118 * vpif_buffer_queue_setup : Callback function for buffer setup.
121 * @nbuffers: ptr to number of buffers requested by application
122 * @nplanes:: contains number of distinct video planes needed to hold a frame
123 * @sizes[]: contains the size (in bytes) of each plane.
124 * @alloc_ctxs: ptr to allocation context
126 * This callback function is called when reqbuf() is called to adjust
127 * the buffer count and buffer size
129 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
130 const struct v4l2_format *fmt,
131 unsigned int *nbuffers, unsigned int *nplanes,
132 unsigned int sizes[], void *alloc_ctxs[])
134 /* Get the file handle object and channel object */
135 struct vpif_fh *fh = vb2_get_drv_priv(vq);
136 struct channel_obj *ch = fh->channel;
137 struct common_obj *common;
140 common = &ch->common[VPIF_VIDEO_INDEX];
142 vpif_dbg(2, debug, "vpif_buffer_setup\n");
144 /* If memory type is not mmap, return */
145 if (V4L2_MEMORY_MMAP == common->memory) {
146 /* Calculate the size of the buffer */
147 size = config_params.channel_bufsize[ch->channel_id];
149 * Checking if the buffer size exceeds the available buffer
150 * ycmux_mode = 0 means 1 channel mode HD and
151 * ycmux_mode = 1 means 2 channels mode SD
153 if (ch->vpifparams.std_info.ycmux_mode == 0) {
154 if (config_params.video_limit[ch->channel_id])
155 while (size * *nbuffers >
156 (config_params.video_limit[0]
157 + config_params.video_limit[1]))
160 if (config_params.video_limit[ch->channel_id])
161 while (size * *nbuffers >
162 config_params.video_limit[ch->channel_id])
167 size = common->fmt.fmt.pix.sizeimage;
170 if (*nbuffers < config_params.min_numbuffers)
171 *nbuffers = config_params.min_numbuffers;
175 alloc_ctxs[0] = common->alloc_ctx;
181 * vpif_buffer_queue : Callback function to add buffer to DMA queue
182 * @vb: ptr to vb2_buffer
184 static void vpif_buffer_queue(struct vb2_buffer *vb)
186 /* Get the file handle object and channel object */
187 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
188 struct channel_obj *ch = fh->channel;
189 struct vpif_cap_buffer *buf = container_of(vb,
190 struct vpif_cap_buffer, vb);
191 struct common_obj *common;
194 common = &ch->common[VPIF_VIDEO_INDEX];
196 vpif_dbg(2, debug, "vpif_buffer_queue\n");
198 spin_lock_irqsave(&common->irqlock, flags);
199 /* add the buffer to the DMA queue */
200 list_add_tail(&buf->list, &common->dma_queue);
201 spin_unlock_irqrestore(&common->irqlock, flags);
205 * vpif_buf_cleanup : Callback function to free buffer
206 * @vb: ptr to vb2_buffer
208 * This function is called from the videobuf2 layer to free memory
209 * allocated to the buffers
211 static void vpif_buf_cleanup(struct vb2_buffer *vb)
213 /* Get the file handle object and channel object */
214 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
215 struct vpif_cap_buffer *buf = container_of(vb,
216 struct vpif_cap_buffer, vb);
217 struct channel_obj *ch = fh->channel;
218 struct common_obj *common;
221 common = &ch->common[VPIF_VIDEO_INDEX];
223 spin_lock_irqsave(&common->irqlock, flags);
224 if (vb->state == VB2_BUF_STATE_ACTIVE)
225 list_del_init(&buf->list);
226 spin_unlock_irqrestore(&common->irqlock, flags);
230 static void vpif_wait_prepare(struct vb2_queue *vq)
232 struct vpif_fh *fh = vb2_get_drv_priv(vq);
233 struct channel_obj *ch = fh->channel;
234 struct common_obj *common;
236 common = &ch->common[VPIF_VIDEO_INDEX];
237 mutex_unlock(&common->lock);
240 static void vpif_wait_finish(struct vb2_queue *vq)
242 struct vpif_fh *fh = vb2_get_drv_priv(vq);
243 struct channel_obj *ch = fh->channel;
244 struct common_obj *common;
246 common = &ch->common[VPIF_VIDEO_INDEX];
247 mutex_lock(&common->lock);
250 static int vpif_buffer_init(struct vb2_buffer *vb)
252 struct vpif_cap_buffer *buf = container_of(vb,
253 struct vpif_cap_buffer, vb);
255 INIT_LIST_HEAD(&buf->list);
260 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
263 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
265 struct vpif_capture_config *vpif_config_data =
266 vpif_dev->platform_data;
267 struct vpif_fh *fh = vb2_get_drv_priv(vq);
268 struct channel_obj *ch = fh->channel;
269 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
270 struct vpif_params *vpif = &ch->vpifparams;
271 unsigned long addr = 0;
275 /* If buffer queue is empty, return error */
276 spin_lock_irqsave(&common->irqlock, flags);
277 if (list_empty(&common->dma_queue)) {
278 spin_unlock_irqrestore(&common->irqlock, flags);
279 vpif_dbg(1, debug, "buffer queue is empty\n");
283 /* Get the next frame from the buffer queue */
284 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
285 struct vpif_cap_buffer, list);
286 /* Remove buffer from the buffer queue */
287 list_del(&common->cur_frm->list);
288 spin_unlock_irqrestore(&common->irqlock, flags);
289 /* Mark state of the current frame to active */
290 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
291 /* Initialize field_id and started member */
294 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
296 /* Calculate the offset for Y and C data in the buffer */
297 vpif_calculate_offsets(ch);
299 if ((vpif->std_info.frm_fmt &&
300 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
301 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
302 (!vpif->std_info.frm_fmt &&
303 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
304 vpif_dbg(1, debug, "conflict in field format and std format\n");
308 /* configure 1 or 2 channel mode */
309 if (vpif_config_data->setup_input_channel_mode) {
310 ret = vpif_config_data->
311 setup_input_channel_mode(vpif->std_info.ycmux_mode);
313 vpif_dbg(1, debug, "can't set vpif channel mode\n");
318 /* Call vpif_set_params function to set the parameters and addresses */
319 ret = vpif_set_video_params(vpif, ch->channel_id);
322 vpif_dbg(1, debug, "can't set video params\n");
326 common->started = ret;
327 vpif_config_addr(ch, ret);
329 common->set_addr(addr + common->ytop_off,
330 addr + common->ybtm_off,
331 addr + common->ctop_off,
332 addr + common->cbtm_off);
335 * Set interrupt for both the fields in VPIF Register enable channel in
338 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
339 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
340 channel0_intr_assert();
341 channel0_intr_enable(1);
344 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
345 (common->started == 2)) {
346 channel1_intr_assert();
347 channel1_intr_enable(1);
354 /* abort streaming and wait for last buffer */
355 static int vpif_stop_streaming(struct vb2_queue *vq)
357 struct vpif_fh *fh = vb2_get_drv_priv(vq);
358 struct channel_obj *ch = fh->channel;
359 struct common_obj *common;
362 if (!vb2_is_streaming(vq))
365 common = &ch->common[VPIF_VIDEO_INDEX];
367 /* release all active buffers */
368 spin_lock_irqsave(&common->irqlock, flags);
369 while (!list_empty(&common->dma_queue)) {
370 common->next_frm = list_entry(common->dma_queue.next,
371 struct vpif_cap_buffer, list);
372 list_del(&common->next_frm->list);
373 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
375 spin_unlock_irqrestore(&common->irqlock, flags);
380 static struct vb2_ops video_qops = {
381 .queue_setup = vpif_buffer_queue_setup,
382 .wait_prepare = vpif_wait_prepare,
383 .wait_finish = vpif_wait_finish,
384 .buf_init = vpif_buffer_init,
385 .buf_prepare = vpif_buffer_prepare,
386 .start_streaming = vpif_start_streaming,
387 .stop_streaming = vpif_stop_streaming,
388 .buf_cleanup = vpif_buf_cleanup,
389 .buf_queue = vpif_buffer_queue,
393 * vpif_process_buffer_complete: process a completed buffer
394 * @common: ptr to common channel object
396 * This function time stamp the buffer and mark it as DONE. It also
397 * wake up any process waiting on the QUEUE and set the next buffer
400 static void vpif_process_buffer_complete(struct common_obj *common)
402 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
403 vb2_buffer_done(&common->cur_frm->vb,
405 /* Make curFrm pointing to nextFrm */
406 common->cur_frm = common->next_frm;
410 * vpif_schedule_next_buffer: set next buffer address for capture
411 * @common : ptr to common channel object
413 * This function will get next buffer from the dma queue and
414 * set the buffer address in the vpif register for capture.
415 * the buffer is marked active
417 static void vpif_schedule_next_buffer(struct common_obj *common)
419 unsigned long addr = 0;
421 spin_lock(&common->irqlock);
422 common->next_frm = list_entry(common->dma_queue.next,
423 struct vpif_cap_buffer, list);
424 /* Remove that buffer from the buffer queue */
425 list_del(&common->next_frm->list);
426 spin_unlock(&common->irqlock);
427 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
428 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
430 /* Set top and bottom field addresses in VPIF registers */
431 common->set_addr(addr + common->ytop_off,
432 addr + common->ybtm_off,
433 addr + common->ctop_off,
434 addr + common->cbtm_off);
438 * vpif_channel_isr : ISR handler for vpif capture
440 * @dev_id: dev_id ptr
442 * It changes status of the captured buffer, takes next buffer from the queue
443 * and sets its address in VPIF registers
445 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
447 struct vpif_device *dev = &vpif_obj;
448 struct common_obj *common;
449 struct channel_obj *ch;
450 enum v4l2_field field;
454 channel_id = *(int *)(dev_id);
455 if (!vpif_intr_status(channel_id))
458 ch = dev->dev[channel_id];
460 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
462 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
463 common = &ch->common[i];
464 /* skip If streaming is not started in this channel */
465 if (0 == common->started)
468 /* Check the field format */
469 if (1 == ch->vpifparams.std_info.frm_fmt) {
470 /* Progressive mode */
471 spin_lock(&common->irqlock);
472 if (list_empty(&common->dma_queue)) {
473 spin_unlock(&common->irqlock);
476 spin_unlock(&common->irqlock);
478 if (!channel_first_int[i][channel_id])
479 vpif_process_buffer_complete(common);
481 channel_first_int[i][channel_id] = 0;
483 vpif_schedule_next_buffer(common);
486 channel_first_int[i][channel_id] = 0;
489 * Interlaced mode. If it is first interrupt, ignore
492 if (channel_first_int[i][channel_id]) {
493 channel_first_int[i][channel_id] = 0;
498 /* Get field id from VPIF registers */
499 fid = vpif_channel_getfid(ch->channel_id);
500 if (fid != ch->field_id) {
502 * If field id does not match stored
503 * field id, make them in sync
510 /* device field id and local field id are in sync */
512 /* this is even field */
513 if (common->cur_frm == common->next_frm)
516 /* mark the current buffer as done */
517 vpif_process_buffer_complete(common);
518 } else if (1 == fid) {
520 spin_lock(&common->irqlock);
521 if (list_empty(&common->dma_queue) ||
522 (common->cur_frm != common->next_frm)) {
523 spin_unlock(&common->irqlock);
526 spin_unlock(&common->irqlock);
528 vpif_schedule_next_buffer(common);
536 * vpif_update_std_info() - update standard related info
537 * @ch: ptr to channel object
539 * For a given standard selected by application, update values
540 * in the device data structures
542 static int vpif_update_std_info(struct channel_obj *ch)
544 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
545 struct vpif_params *vpifparams = &ch->vpifparams;
546 const struct vpif_channel_config_params *config;
547 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
548 struct video_obj *vid_ch = &ch->video;
551 vpif_dbg(2, debug, "vpif_update_std_info\n");
553 for (index = 0; index < vpif_ch_params_count; index++) {
554 config = &vpif_ch_params[index];
555 if (config->hd_sd == 0) {
556 vpif_dbg(2, debug, "SD format\n");
557 if (config->stdid & vid_ch->stdid) {
558 memcpy(std_info, config, sizeof(*config));
562 vpif_dbg(2, debug, "HD format\n");
563 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
564 sizeof(vid_ch->dv_timings))) {
565 memcpy(std_info, config, sizeof(*config));
571 /* standard not found */
572 if (index == vpif_ch_params_count)
575 common->fmt.fmt.pix.width = std_info->width;
576 common->width = std_info->width;
577 common->fmt.fmt.pix.height = std_info->height;
578 common->height = std_info->height;
579 common->fmt.fmt.pix.bytesperline = std_info->width;
580 vpifparams->video_params.hpitch = std_info->width;
581 vpifparams->video_params.storage_mode = std_info->frm_fmt;
587 * vpif_calculate_offsets : This function calculates buffers offsets
588 * @ch : ptr to channel object
590 * This function calculates buffer offsets for Y and C in the top and
593 static void vpif_calculate_offsets(struct channel_obj *ch)
595 unsigned int hpitch, vpitch, sizeimage;
596 struct video_obj *vid_ch = &(ch->video);
597 struct vpif_params *vpifparams = &ch->vpifparams;
598 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
599 enum v4l2_field field = common->fmt.fmt.pix.field;
601 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
603 if (V4L2_FIELD_ANY == field) {
604 if (vpifparams->std_info.frm_fmt)
605 vid_ch->buf_field = V4L2_FIELD_NONE;
607 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
609 vid_ch->buf_field = common->fmt.fmt.pix.field;
611 sizeimage = common->fmt.fmt.pix.sizeimage;
613 hpitch = common->fmt.fmt.pix.bytesperline;
614 vpitch = sizeimage / (hpitch * 2);
616 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
617 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
618 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
619 common->ytop_off = 0;
620 common->ybtm_off = hpitch;
621 common->ctop_off = sizeimage / 2;
622 common->cbtm_off = sizeimage / 2 + hpitch;
623 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
624 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
625 common->ytop_off = 0;
626 common->ybtm_off = sizeimage / 4;
627 common->ctop_off = sizeimage / 2;
628 common->cbtm_off = common->ctop_off + sizeimage / 4;
629 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
630 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
631 common->ybtm_off = 0;
632 common->ytop_off = sizeimage / 4;
633 common->cbtm_off = sizeimage / 2;
634 common->ctop_off = common->cbtm_off + sizeimage / 4;
636 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
637 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
638 vpifparams->video_params.storage_mode = 1;
640 vpifparams->video_params.storage_mode = 0;
642 if (1 == vpifparams->std_info.frm_fmt)
643 vpifparams->video_params.hpitch =
644 common->fmt.fmt.pix.bytesperline;
646 if ((field == V4L2_FIELD_ANY)
647 || (field == V4L2_FIELD_INTERLACED))
648 vpifparams->video_params.hpitch =
649 common->fmt.fmt.pix.bytesperline * 2;
651 vpifparams->video_params.hpitch =
652 common->fmt.fmt.pix.bytesperline;
655 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
659 * vpif_config_format: configure default frame format in the device
660 * ch : ptr to channel object
662 static void vpif_config_format(struct channel_obj *ch)
664 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
666 vpif_dbg(2, debug, "vpif_config_format\n");
668 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
669 if (config_params.numbuffers[ch->channel_id] == 0)
670 common->memory = V4L2_MEMORY_USERPTR;
672 common->memory = V4L2_MEMORY_MMAP;
674 common->fmt.fmt.pix.sizeimage
675 = config_params.channel_bufsize[ch->channel_id];
677 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
678 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
680 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
681 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
685 * vpif_get_default_field() - Get default field type based on interface
686 * @vpif_params - ptr to vpif params
688 static inline enum v4l2_field vpif_get_default_field(
689 struct vpif_interface *iface)
691 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
692 V4L2_FIELD_INTERLACED;
696 * vpif_check_format() - check given pixel format for compatibility
698 * @pixfmt - Given pixel format
699 * @update - update the values as per hardware requirement
701 * Check the application pixel format for S_FMT and update the input
702 * values as per hardware limits for TRY_FMT. The default pixel and
703 * field format is selected based on interface type.
705 static int vpif_check_format(struct channel_obj *ch,
706 struct v4l2_pix_format *pixfmt,
709 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
710 struct vpif_params *vpif_params = &ch->vpifparams;
711 enum v4l2_field field = pixfmt->field;
712 u32 sizeimage, hpitch, vpitch;
715 vpif_dbg(2, debug, "vpif_check_format\n");
717 * first check for the pixel format. If if_type is Raw bayer,
718 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
719 * V4L2_PIX_FMT_YUV422P is supported
721 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
722 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
724 vpif_dbg(2, debug, "invalid pix format\n");
727 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
730 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
732 vpif_dbg(2, debug, "invalid pixel format\n");
735 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
739 if (!(VPIF_VALID_FIELD(field))) {
741 vpif_dbg(2, debug, "invalid field format\n");
745 * By default use FIELD_NONE for RAW Bayer capture
746 * and FIELD_INTERLACED for other interfaces
748 field = vpif_get_default_field(&vpif_params->iface);
749 } else if (field == V4L2_FIELD_ANY)
750 /* unsupported field. Use default */
751 field = vpif_get_default_field(&vpif_params->iface);
753 /* validate the hpitch */
754 hpitch = pixfmt->bytesperline;
755 if (hpitch < vpif_params->std_info.width) {
757 vpif_dbg(2, debug, "invalid hpitch\n");
760 hpitch = vpif_params->std_info.width;
763 sizeimage = pixfmt->sizeimage;
765 vpitch = sizeimage / (hpitch * 2);
767 /* validate the vpitch */
768 if (vpitch < vpif_params->std_info.height) {
770 vpif_dbg(2, debug, "Invalid vpitch\n");
773 vpitch = vpif_params->std_info.height;
776 /* Check for 8 byte alignment */
777 if (!ALIGN(hpitch, 8)) {
779 vpif_dbg(2, debug, "invalid pitch alignment\n");
782 /* adjust to next 8 byte boundary */
783 hpitch = (((hpitch + 7) / 8) * 8);
785 /* if update is set, modify the bytesperline and sizeimage */
787 pixfmt->bytesperline = hpitch;
788 pixfmt->sizeimage = hpitch * vpitch * 2;
791 * Image width and height is always based on current standard width and
794 pixfmt->width = common->fmt.fmt.pix.width;
795 pixfmt->height = common->fmt.fmt.pix.height;
802 * vpif_config_addr() - function to configure buffer address in vpif
804 * @muxmode - channel mux mode
806 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
808 struct common_obj *common;
810 vpif_dbg(2, debug, "vpif_config_addr\n");
812 common = &(ch->common[VPIF_VIDEO_INDEX]);
814 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
815 common->set_addr = ch1_set_videobuf_addr;
816 else if (2 == muxmode)
817 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
819 common->set_addr = ch0_set_videobuf_addr;
823 * vpif_mmap : It is used to map kernel space buffers into user spaces
824 * @filep: file pointer
825 * @vma: ptr to vm_area_struct
827 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
829 /* Get the channel object and file handle object */
830 struct vpif_fh *fh = filep->private_data;
831 struct channel_obj *ch = fh->channel;
832 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
835 vpif_dbg(2, debug, "vpif_mmap\n");
837 if (mutex_lock_interruptible(&common->lock))
839 ret = vb2_mmap(&common->buffer_queue, vma);
840 mutex_unlock(&common->lock);
845 * vpif_poll: It is used for select/poll system call
846 * @filep: file pointer
847 * @wait: poll table to wait
849 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
851 struct vpif_fh *fh = filep->private_data;
852 struct channel_obj *channel = fh->channel;
853 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
854 unsigned int res = 0;
856 vpif_dbg(2, debug, "vpif_poll\n");
858 if (common->started) {
859 mutex_lock(&common->lock);
860 res = vb2_poll(&common->buffer_queue, filep, wait);
861 mutex_unlock(&common->lock);
867 * vpif_open : vpif open handler
870 * It creates object of file handle structure and stores it in private_data
871 * member of filepointer
873 static int vpif_open(struct file *filep)
875 struct video_device *vdev = video_devdata(filep);
876 struct common_obj *common;
877 struct video_obj *vid_ch;
878 struct channel_obj *ch;
881 vpif_dbg(2, debug, "vpif_open\n");
883 ch = video_get_drvdata(vdev);
886 common = &ch->common[VPIF_VIDEO_INDEX];
888 /* Allocate memory for the file handle object */
889 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
891 vpif_err("unable to allocate memory for file handle object\n");
895 if (mutex_lock_interruptible(&common->lock)) {
899 /* store pointer to fh in private_data member of filep */
900 filep->private_data = fh;
903 /* If decoder is not initialized. initialize it */
904 if (!ch->initialized) {
907 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
909 /* Increment channel usrs counter */
911 /* Set io_allowed member to false */
912 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
913 /* Initialize priority of this instance to default priority */
914 fh->prio = V4L2_PRIORITY_UNSET;
915 v4l2_prio_open(&ch->prio, &fh->prio);
916 mutex_unlock(&common->lock);
921 * vpif_release : function to clean up file close
922 * @filep: file pointer
924 * This function deletes buffer queue, frees the buffers and the vpif file
927 static int vpif_release(struct file *filep)
929 struct vpif_fh *fh = filep->private_data;
930 struct channel_obj *ch = fh->channel;
931 struct common_obj *common;
933 vpif_dbg(2, debug, "vpif_release\n");
935 common = &ch->common[VPIF_VIDEO_INDEX];
937 mutex_lock(&common->lock);
938 /* if this instance is doing IO */
939 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
940 /* Reset io_usrs member of channel object */
942 /* Disable channel as per its device type and channel id */
943 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
945 channel0_intr_enable(0);
947 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
948 (2 == common->started)) {
950 channel1_intr_enable(0);
953 /* Free buffers allocated */
954 vb2_queue_release(&common->buffer_queue);
955 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
958 /* Decrement channel usrs counter */
961 /* Close the priority */
962 v4l2_prio_close(&ch->prio, fh->prio);
967 mutex_unlock(&common->lock);
968 filep->private_data = NULL;
974 * vpif_reqbufs() - request buffer handler
977 * @reqbuf: request buffer structure ptr
979 static int vpif_reqbufs(struct file *file, void *priv,
980 struct v4l2_requestbuffers *reqbuf)
982 struct vpif_fh *fh = priv;
983 struct channel_obj *ch = fh->channel;
984 struct common_obj *common;
989 vpif_dbg(2, debug, "vpif_reqbufs\n");
992 * This file handle has not initialized the channel,
993 * It is not allowed to do settings
995 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
996 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
997 if (!fh->initialized) {
998 vpif_dbg(1, debug, "Channel Busy\n");
1003 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
1006 index = VPIF_VIDEO_INDEX;
1008 common = &ch->common[index];
1010 if (0 != common->io_usrs)
1013 /* Initialize videobuf2 queue as per the buffer type */
1014 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1015 if (IS_ERR(common->alloc_ctx)) {
1016 vpif_err("Failed to get the context\n");
1017 return PTR_ERR(common->alloc_ctx);
1019 q = &common->buffer_queue;
1020 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1021 q->io_modes = VB2_MMAP | VB2_USERPTR;
1023 q->ops = &video_qops;
1024 q->mem_ops = &vb2_dma_contig_memops;
1025 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1026 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1028 ret = vb2_queue_init(q);
1030 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1031 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1034 /* Set io allowed member of file handle to TRUE */
1035 fh->io_allowed[index] = 1;
1036 /* Increment io usrs member of channel object to 1 */
1037 common->io_usrs = 1;
1038 /* Store type of memory requested in channel object */
1039 common->memory = reqbuf->memory;
1040 INIT_LIST_HEAD(&common->dma_queue);
1042 /* Allocate buffers */
1043 return vb2_reqbufs(&common->buffer_queue, reqbuf);
1047 * vpif_querybuf() - query buffer handler
1049 * @priv: file handle
1050 * @buf: v4l2 buffer structure ptr
1052 static int vpif_querybuf(struct file *file, void *priv,
1053 struct v4l2_buffer *buf)
1055 struct vpif_fh *fh = priv;
1056 struct channel_obj *ch = fh->channel;
1057 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1059 vpif_dbg(2, debug, "vpif_querybuf\n");
1061 if (common->fmt.type != buf->type)
1064 if (common->memory != V4L2_MEMORY_MMAP) {
1065 vpif_dbg(1, debug, "Invalid memory\n");
1069 return vb2_querybuf(&common->buffer_queue, buf);
1073 * vpif_qbuf() - query buffer handler
1075 * @priv: file handle
1076 * @buf: v4l2 buffer structure ptr
1078 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1081 struct vpif_fh *fh = priv;
1082 struct channel_obj *ch = fh->channel;
1083 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1084 struct v4l2_buffer tbuf = *buf;
1086 vpif_dbg(2, debug, "vpif_qbuf\n");
1088 if (common->fmt.type != tbuf.type) {
1089 vpif_err("invalid buffer type\n");
1093 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1094 vpif_err("fh io not allowed\n");
1098 return vb2_qbuf(&common->buffer_queue, buf);
1102 * vpif_dqbuf() - query buffer handler
1104 * @priv: file handle
1105 * @buf: v4l2 buffer structure ptr
1107 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1109 struct vpif_fh *fh = priv;
1110 struct channel_obj *ch = fh->channel;
1111 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1113 vpif_dbg(2, debug, "vpif_dqbuf\n");
1115 return vb2_dqbuf(&common->buffer_queue, buf,
1116 (file->f_flags & O_NONBLOCK));
1120 * vpif_streamon() - streamon handler
1122 * @priv: file handle
1123 * @buftype: v4l2 buffer type
1125 static int vpif_streamon(struct file *file, void *priv,
1126 enum v4l2_buf_type buftype)
1129 struct vpif_fh *fh = priv;
1130 struct channel_obj *ch = fh->channel;
1131 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1132 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1133 struct vpif_params *vpif;
1136 vpif_dbg(2, debug, "vpif_streamon\n");
1138 vpif = &ch->vpifparams;
1140 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1141 vpif_dbg(1, debug, "buffer type not supported\n");
1145 /* If file handle is not allowed IO, return error */
1146 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1147 vpif_dbg(1, debug, "io not allowed\n");
1151 /* If Streaming is already started, return error */
1152 if (common->started) {
1153 vpif_dbg(1, debug, "channel->started\n");
1157 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1158 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1159 vpif->std_info.ycmux_mode == 0) ||
1160 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1161 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1162 vpif_dbg(1, debug, "other channel is being used\n");
1166 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1170 /* Enable streamon on the sub device */
1171 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
1173 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1174 vpif_dbg(1, debug, "stream on failed in subdev\n");
1178 /* Call vb2_streamon to start streaming in videobuf2 */
1179 ret = vb2_streamon(&common->buffer_queue, buftype);
1181 vpif_dbg(1, debug, "vb2_streamon\n");
1189 * vpif_streamoff() - streamoff handler
1191 * @priv: file handle
1192 * @buftype: v4l2 buffer type
1194 static int vpif_streamoff(struct file *file, void *priv,
1195 enum v4l2_buf_type buftype)
1198 struct vpif_fh *fh = priv;
1199 struct channel_obj *ch = fh->channel;
1200 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1203 vpif_dbg(2, debug, "vpif_streamoff\n");
1205 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1206 vpif_dbg(1, debug, "buffer type not supported\n");
1210 /* If io is allowed for this file handle, return error */
1211 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1212 vpif_dbg(1, debug, "io not allowed\n");
1216 /* If streaming is not started, return error */
1217 if (!common->started) {
1218 vpif_dbg(1, debug, "channel->started\n");
1222 /* disable channel */
1223 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1225 channel0_intr_enable(0);
1228 channel1_intr_enable(0);
1231 common->started = 0;
1233 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
1235 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1236 vpif_dbg(1, debug, "stream off failed in subdev\n");
1238 return vb2_streamoff(&common->buffer_queue, buftype);
1242 * vpif_input_to_subdev() - Maps input to sub device
1243 * @vpif_cfg - global config ptr
1244 * @chan_cfg - channel config ptr
1245 * @input_index - Given input index from application
1247 * lookup the sub device information for a given input index.
1248 * we report all the inputs to application. inputs table also
1249 * has sub device name for the each input
1251 static int vpif_input_to_subdev(
1252 struct vpif_capture_config *vpif_cfg,
1253 struct vpif_capture_chan_config *chan_cfg,
1256 struct vpif_subdev_info *subdev_info;
1257 const char *subdev_name;
1260 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
1262 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1263 if (subdev_name == NULL)
1266 /* loop through the sub device list to get the sub device info */
1267 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1268 subdev_info = &vpif_cfg->subdev_info[i];
1269 if (!strcmp(subdev_info->name, subdev_name))
1276 * vpif_set_input() - Select an input
1277 * @vpif_cfg - global config ptr
1279 * @_index - Given input index from application
1281 * Select the given input.
1283 static int vpif_set_input(
1284 struct vpif_capture_config *vpif_cfg,
1285 struct channel_obj *ch,
1288 struct vpif_capture_chan_config *chan_cfg =
1289 &vpif_cfg->chan_config[ch->channel_id];
1290 struct vpif_subdev_info *subdev_info = NULL;
1291 struct v4l2_subdev *sd = NULL;
1292 u32 input = 0, output = 0;
1296 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1297 if (sd_index >= 0) {
1298 sd = vpif_obj.sd[sd_index];
1299 subdev_info = &vpif_cfg->subdev_info[sd_index];
1302 /* first setup input path from sub device to vpif */
1303 if (sd && vpif_cfg->setup_input_path) {
1304 ret = vpif_cfg->setup_input_path(ch->channel_id,
1307 vpif_dbg(1, debug, "couldn't setup input path for the" \
1308 " sub device %s, for input index %d\n",
1309 subdev_info->name, index);
1315 input = chan_cfg->inputs[index].input_route;
1316 output = chan_cfg->inputs[index].output_route;
1317 ret = v4l2_subdev_call(sd, video, s_routing,
1319 if (ret < 0 && ret != -ENOIOCTLCMD) {
1320 vpif_dbg(1, debug, "Failed to set input\n");
1324 ch->input_idx = index;
1326 /* copy interface parameters to vpif */
1327 ch->vpifparams.iface = chan_cfg->vpif_if;
1329 /* update tvnorms from the sub device input info */
1330 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1335 * vpif_querystd() - querystd handler
1337 * @priv: file handle
1338 * @std_id: ptr to std id
1340 * This function is called to detect standard at the selected input
1342 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1344 struct vpif_fh *fh = priv;
1345 struct channel_obj *ch = fh->channel;
1348 vpif_dbg(2, debug, "vpif_querystd\n");
1350 /* Call querystd function of decoder device */
1351 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
1353 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1356 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1364 * vpif_g_std() - get STD handler
1366 * @priv: file handle
1367 * @std_id: ptr to std id
1369 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1371 struct vpif_fh *fh = priv;
1372 struct channel_obj *ch = fh->channel;
1374 vpif_dbg(2, debug, "vpif_g_std\n");
1376 *std = ch->video.stdid;
1381 * vpif_s_std() - set STD handler
1383 * @priv: file handle
1384 * @std_id: ptr to std id
1386 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1388 struct vpif_fh *fh = priv;
1389 struct channel_obj *ch = fh->channel;
1390 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1393 vpif_dbg(2, debug, "vpif_s_std\n");
1395 if (common->started) {
1396 vpif_err("streaming in progress\n");
1400 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1401 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1402 if (!fh->initialized) {
1403 vpif_dbg(1, debug, "Channel Busy\n");
1408 ret = v4l2_prio_check(&ch->prio, fh->prio);
1412 fh->initialized = 1;
1414 /* Call encoder subdevice function to set the standard */
1415 ch->video.stdid = std_id;
1416 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1418 /* Get the information about the standard */
1419 if (vpif_update_std_info(ch)) {
1420 vpif_err("Error getting the standard info\n");
1424 /* Configure the default format information */
1425 vpif_config_format(ch);
1427 /* set standard in the sub device */
1428 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
1429 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1430 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1437 * vpif_enum_input() - ENUMINPUT handler
1439 * @priv: file handle
1440 * @input: ptr to input structure
1442 static int vpif_enum_input(struct file *file, void *priv,
1443 struct v4l2_input *input)
1446 struct vpif_capture_config *config = vpif_dev->platform_data;
1447 struct vpif_capture_chan_config *chan_cfg;
1448 struct vpif_fh *fh = priv;
1449 struct channel_obj *ch = fh->channel;
1451 chan_cfg = &config->chan_config[ch->channel_id];
1453 if (input->index >= chan_cfg->input_count) {
1454 vpif_dbg(1, debug, "Invalid input index\n");
1458 memcpy(input, &chan_cfg->inputs[input->index].input,
1464 * vpif_g_input() - Get INPUT handler
1466 * @priv: file handle
1467 * @index: ptr to input index
1469 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1471 struct vpif_fh *fh = priv;
1472 struct channel_obj *ch = fh->channel;
1474 *index = ch->input_idx;
1479 * vpif_s_input() - Set INPUT handler
1481 * @priv: file handle
1482 * @index: input index
1484 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1486 struct vpif_capture_config *config = vpif_dev->platform_data;
1487 struct vpif_capture_chan_config *chan_cfg;
1488 struct vpif_fh *fh = priv;
1489 struct channel_obj *ch = fh->channel;
1490 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1493 chan_cfg = &config->chan_config[ch->channel_id];
1495 if (index >= chan_cfg->input_count)
1498 if (common->started) {
1499 vpif_err("Streaming in progress\n");
1503 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1504 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1505 if (!fh->initialized) {
1506 vpif_dbg(1, debug, "Channel Busy\n");
1511 ret = v4l2_prio_check(&ch->prio, fh->prio);
1515 fh->initialized = 1;
1516 return vpif_set_input(config, ch, index);
1520 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1522 * @priv: file handle
1523 * @index: input index
1525 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1526 struct v4l2_fmtdesc *fmt)
1528 struct vpif_fh *fh = priv;
1529 struct channel_obj *ch = fh->channel;
1531 if (fmt->index != 0) {
1532 vpif_dbg(1, debug, "Invalid format index\n");
1536 /* Fill in the information about format */
1537 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1538 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1539 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1540 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1542 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1543 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1544 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1550 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1552 * @priv: file handle
1553 * @fmt: ptr to v4l2 format structure
1555 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1556 struct v4l2_format *fmt)
1558 struct vpif_fh *fh = priv;
1559 struct channel_obj *ch = fh->channel;
1560 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1562 return vpif_check_format(ch, pixfmt, 1);
1567 * vpif_g_fmt_vid_cap() - Set INPUT handler
1569 * @priv: file handle
1570 * @fmt: ptr to v4l2 format structure
1572 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1573 struct v4l2_format *fmt)
1575 struct vpif_fh *fh = priv;
1576 struct channel_obj *ch = fh->channel;
1577 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1579 /* Check the validity of the buffer type */
1580 if (common->fmt.type != fmt->type)
1583 /* Fill in the information about format */
1589 * vpif_s_fmt_vid_cap() - Set FMT handler
1591 * @priv: file handle
1592 * @fmt: ptr to v4l2 format structure
1594 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1595 struct v4l2_format *fmt)
1597 struct vpif_fh *fh = priv;
1598 struct channel_obj *ch = fh->channel;
1599 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1600 struct v4l2_pix_format *pixfmt;
1603 vpif_dbg(2, debug, "%s\n", __func__);
1605 /* If streaming is started, return error */
1606 if (common->started) {
1607 vpif_dbg(1, debug, "Streaming is started\n");
1611 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1612 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1613 if (!fh->initialized) {
1614 vpif_dbg(1, debug, "Channel Busy\n");
1619 ret = v4l2_prio_check(&ch->prio, fh->prio);
1623 fh->initialized = 1;
1625 pixfmt = &fmt->fmt.pix;
1626 /* Check for valid field format */
1627 ret = vpif_check_format(ch, pixfmt, 0);
1631 /* store the format in the channel object */
1637 * vpif_querycap() - QUERYCAP handler
1639 * @priv: file handle
1640 * @cap: ptr to v4l2_capability structure
1642 static int vpif_querycap(struct file *file, void *priv,
1643 struct v4l2_capability *cap)
1645 struct vpif_capture_config *config = vpif_dev->platform_data;
1647 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1648 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1649 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1650 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1651 dev_name(vpif_dev));
1652 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1658 * vpif_g_priority() - get priority handler
1660 * @priv: file handle
1661 * @prio: ptr to v4l2_priority structure
1663 static int vpif_g_priority(struct file *file, void *priv,
1664 enum v4l2_priority *prio)
1666 struct vpif_fh *fh = priv;
1667 struct channel_obj *ch = fh->channel;
1669 *prio = v4l2_prio_max(&ch->prio);
1675 * vpif_s_priority() - set priority handler
1677 * @priv: file handle
1678 * @prio: ptr to v4l2_priority structure
1680 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1682 struct vpif_fh *fh = priv;
1683 struct channel_obj *ch = fh->channel;
1685 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1689 * vpif_cropcap() - cropcap handler
1691 * @priv: file handle
1692 * @crop: ptr to v4l2_cropcap structure
1694 static int vpif_cropcap(struct file *file, void *priv,
1695 struct v4l2_cropcap *crop)
1697 struct vpif_fh *fh = priv;
1698 struct channel_obj *ch = fh->channel;
1699 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1701 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1704 crop->bounds.left = 0;
1705 crop->bounds.top = 0;
1706 crop->bounds.height = common->height;
1707 crop->bounds.width = common->width;
1708 crop->defrect = crop->bounds;
1713 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1715 * @priv: file handle
1716 * @timings: input timings
1719 vpif_enum_dv_timings(struct file *file, void *priv,
1720 struct v4l2_enum_dv_timings *timings)
1722 struct vpif_fh *fh = priv;
1723 struct channel_obj *ch = fh->channel;
1726 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1727 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1733 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1735 * @priv: file handle
1736 * @timings: input timings
1739 vpif_query_dv_timings(struct file *file, void *priv,
1740 struct v4l2_dv_timings *timings)
1742 struct vpif_fh *fh = priv;
1743 struct channel_obj *ch = fh->channel;
1746 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1747 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1753 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1755 * @priv: file handle
1756 * @timings: digital video timings
1758 static int vpif_s_dv_timings(struct file *file, void *priv,
1759 struct v4l2_dv_timings *timings)
1761 struct vpif_fh *fh = priv;
1762 struct channel_obj *ch = fh->channel;
1763 struct vpif_params *vpifparams = &ch->vpifparams;
1764 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1765 struct video_obj *vid_ch = &ch->video;
1766 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1769 if (timings->type != V4L2_DV_BT_656_1120) {
1770 vpif_dbg(2, debug, "Timing type not defined\n");
1774 /* Configure subdevice timings, if any */
1775 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1776 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1779 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1783 if (!(timings->bt.width && timings->bt.height &&
1784 (timings->bt.hbackporch ||
1785 timings->bt.hfrontporch ||
1786 timings->bt.hsync) &&
1787 timings->bt.vfrontporch &&
1788 (timings->bt.vbackporch ||
1789 timings->bt.vsync))) {
1790 vpif_dbg(2, debug, "Timings for width, height, "
1791 "horizontal back porch, horizontal sync, "
1792 "horizontal front porch, vertical back porch, "
1793 "vertical sync and vertical back porch "
1794 "must be defined\n");
1798 vid_ch->dv_timings = *timings;
1800 /* Configure video port timings */
1802 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1803 std_info->sav2eav = bt->width;
1806 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1808 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1809 if (bt->interlaced) {
1810 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1811 std_info->l5 = std_info->vsize/2 -
1812 (bt->vfrontporch - 1);
1813 std_info->l7 = std_info->vsize/2 + 1;
1814 std_info->l9 = std_info->l7 + bt->il_vsync +
1815 bt->il_vbackporch + 1;
1816 std_info->l11 = std_info->vsize -
1817 (bt->il_vfrontporch - 1);
1819 vpif_dbg(2, debug, "Required timing values for "
1820 "interlaced BT format missing\n");
1824 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1826 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1827 std_info->width = bt->width;
1828 std_info->height = bt->height;
1829 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1830 std_info->ycmux_mode = 0;
1831 std_info->capture_format = 0;
1832 std_info->vbi_supported = 0;
1833 std_info->hd_sd = 1;
1834 std_info->stdid = 0;
1841 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1843 * @priv: file handle
1844 * @timings: digital video timings
1846 static int vpif_g_dv_timings(struct file *file, void *priv,
1847 struct v4l2_dv_timings *timings)
1849 struct vpif_fh *fh = priv;
1850 struct channel_obj *ch = fh->channel;
1851 struct video_obj *vid_ch = &ch->video;
1853 *timings = vid_ch->dv_timings;
1859 * vpif_log_status() - Status information
1861 * @priv: file handle
1865 static int vpif_log_status(struct file *filep, void *priv)
1867 /* status for sub devices */
1868 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1873 /* vpif capture ioctl operations */
1874 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1875 .vidioc_querycap = vpif_querycap,
1876 .vidioc_g_priority = vpif_g_priority,
1877 .vidioc_s_priority = vpif_s_priority,
1878 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1879 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1880 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1881 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1882 .vidioc_enum_input = vpif_enum_input,
1883 .vidioc_s_input = vpif_s_input,
1884 .vidioc_g_input = vpif_g_input,
1885 .vidioc_reqbufs = vpif_reqbufs,
1886 .vidioc_querybuf = vpif_querybuf,
1887 .vidioc_querystd = vpif_querystd,
1888 .vidioc_s_std = vpif_s_std,
1889 .vidioc_g_std = vpif_g_std,
1890 .vidioc_qbuf = vpif_qbuf,
1891 .vidioc_dqbuf = vpif_dqbuf,
1892 .vidioc_streamon = vpif_streamon,
1893 .vidioc_streamoff = vpif_streamoff,
1894 .vidioc_cropcap = vpif_cropcap,
1895 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1896 .vidioc_query_dv_timings = vpif_query_dv_timings,
1897 .vidioc_s_dv_timings = vpif_s_dv_timings,
1898 .vidioc_g_dv_timings = vpif_g_dv_timings,
1899 .vidioc_log_status = vpif_log_status,
1902 /* vpif file operations */
1903 static struct v4l2_file_operations vpif_fops = {
1904 .owner = THIS_MODULE,
1906 .release = vpif_release,
1907 .unlocked_ioctl = video_ioctl2,
1912 /* vpif video template */
1913 static struct video_device vpif_video_template = {
1917 .ioctl_ops = &vpif_ioctl_ops,
1921 * initialize_vpif() - Initialize vpif data structures
1923 * Allocate memory for data structures and initialize them
1925 static int initialize_vpif(void)
1928 int free_channel_objects_index;
1930 /* Default number of buffers should be 3 */
1931 if ((ch0_numbuffers > 0) &&
1932 (ch0_numbuffers < config_params.min_numbuffers))
1933 ch0_numbuffers = config_params.min_numbuffers;
1934 if ((ch1_numbuffers > 0) &&
1935 (ch1_numbuffers < config_params.min_numbuffers))
1936 ch1_numbuffers = config_params.min_numbuffers;
1938 /* Set buffer size to min buffers size if it is invalid */
1939 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1941 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1942 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1944 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1946 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1947 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1948 if (ch0_numbuffers) {
1949 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1952 if (ch1_numbuffers) {
1953 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1957 /* Allocate memory for six channel objects */
1958 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1960 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1961 /* If memory allocation fails, return error */
1962 if (!vpif_obj.dev[i]) {
1963 free_channel_objects_index = i;
1965 goto vpif_init_free_channel_objects;
1970 vpif_init_free_channel_objects:
1971 for (j = 0; j < free_channel_objects_index; j++)
1972 kfree(vpif_obj.dev[j]);
1976 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1977 struct v4l2_subdev *subdev,
1978 struct v4l2_async_subdev *asd)
1982 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1983 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1985 vpif_obj.sd[i] = subdev;
1992 static int vpif_probe_complete(void)
1994 struct common_obj *common;
1995 struct channel_obj *ch;
1998 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1999 ch = vpif_obj.dev[j];
2001 common = &(ch->common[VPIF_VIDEO_INDEX]);
2002 spin_lock_init(&common->irqlock);
2003 mutex_init(&common->lock);
2004 ch->video_dev->lock = &common->lock;
2005 /* Initialize prio member of channel object */
2006 v4l2_prio_init(&ch->prio);
2007 video_set_drvdata(ch->video_dev, ch);
2009 /* select input 0 */
2010 err = vpif_set_input(vpif_obj.config, ch, 0);
2014 err = video_register_device(ch->video_dev,
2015 VFL_TYPE_GRABBER, (j ? 1 : 0));
2020 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2024 for (k = 0; k < j; k++) {
2025 /* Get the pointer to the channel object */
2026 ch = vpif_obj.dev[k];
2027 /* Unregister video device */
2028 video_unregister_device(ch->video_dev);
2031 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2032 ch = vpif_obj.dev[i];
2033 /* Note: does nothing if ch->video_dev == NULL */
2034 video_device_release(ch->video_dev);
2036 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2041 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2043 return vpif_probe_complete();
2047 * vpif_probe : This function probes the vpif capture driver
2048 * @pdev: platform device pointer
2050 * This creates device entries by register itself to the V4L2 driver and
2051 * initializes fields of each channel objects
2053 static __init int vpif_probe(struct platform_device *pdev)
2055 struct vpif_subdev_info *subdevdata;
2058 struct i2c_adapter *i2c_adap;
2059 struct channel_obj *ch;
2060 struct video_device *vfd;
2061 struct resource *res;
2065 vpif_dev = &pdev->dev;
2067 err = initialize_vpif();
2069 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2073 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2075 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2079 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
2080 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2081 IRQF_SHARED, "VPIF_Capture",
2082 (void *)(&vpif_obj.dev[res_idx]->
2086 goto vpif_unregister;
2091 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2092 /* Get the pointer to the channel object */
2093 ch = vpif_obj.dev[i];
2094 /* Allocate memory for video device */
2095 vfd = video_device_alloc();
2097 for (j = 0; j < i; j++) {
2098 ch = vpif_obj.dev[j];
2099 video_device_release(ch->video_dev);
2102 goto vpif_unregister;
2105 /* Initialize field of video device */
2106 *vfd = vpif_video_template;
2107 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2108 vfd->release = video_device_release;
2109 snprintf(vfd->name, sizeof(vfd->name),
2110 "VPIF_Capture_DRIVER_V%s",
2111 VPIF_CAPTURE_VERSION);
2112 /* Set video_dev to the video device */
2113 ch->video_dev = vfd;
2116 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2118 size = resource_size(res);
2119 /* The resources are divided into two equal memory and when we
2120 * have HD output we can add them together
2122 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2123 ch = vpif_obj.dev[j];
2125 /* only enabled if second resource exists */
2126 config_params.video_limit[ch->channel_id] = 0;
2128 config_params.video_limit[ch->channel_id] =
2133 vpif_obj.config = pdev->dev.platform_data;
2135 subdev_count = vpif_obj.config->subdev_count;
2136 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2138 if (vpif_obj.sd == NULL) {
2139 vpif_err("unable to allocate memory for subdevice pointers\n");
2144 if (!vpif_obj.config->asd_sizes) {
2145 i2c_adap = i2c_get_adapter(1);
2146 for (i = 0; i < subdev_count; i++) {
2147 subdevdata = &vpif_obj.config->subdev_info[i];
2149 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2155 if (!vpif_obj.sd[i]) {
2156 vpif_err("Error registering v4l2 subdevice\n");
2158 goto probe_subdev_out;
2160 v4l2_info(&vpif_obj.v4l2_dev,
2161 "registered sub device %s\n",
2164 vpif_probe_complete();
2166 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
2167 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2168 vpif_obj.notifier.bound = vpif_async_bound;
2169 vpif_obj.notifier.complete = vpif_async_complete;
2170 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2171 &vpif_obj.notifier);
2173 vpif_err("Error registering async notifier\n");
2175 goto probe_subdev_out;
2182 /* free sub devices memory */
2186 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2187 ch = vpif_obj.dev[i];
2188 /* Note: does nothing if ch->video_dev == NULL */
2189 video_device_release(ch->video_dev);
2192 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2198 * vpif_remove() - driver remove handler
2199 * @device: ptr to platform device structure
2201 * The vidoe device is unregistered
2203 static int vpif_remove(struct platform_device *device)
2205 struct channel_obj *ch;
2208 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2211 /* un-register device */
2212 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2213 /* Get the pointer to the channel object */
2214 ch = vpif_obj.dev[i];
2215 /* Unregister video device */
2216 video_unregister_device(ch->video_dev);
2217 kfree(vpif_obj.dev[i]);
2224 * vpif_suspend: vpif device suspend
2226 static int vpif_suspend(struct device *dev)
2229 struct common_obj *common;
2230 struct channel_obj *ch;
2233 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2234 /* Get the pointer to the channel object */
2235 ch = vpif_obj.dev[i];
2236 common = &ch->common[VPIF_VIDEO_INDEX];
2237 mutex_lock(&common->lock);
2238 if (ch->usrs && common->io_usrs) {
2239 /* Disable channel */
2240 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2242 channel0_intr_enable(0);
2244 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2245 common->started == 2) {
2247 channel1_intr_enable(0);
2250 mutex_unlock(&common->lock);
2257 * vpif_resume: vpif device suspend
2259 static int vpif_resume(struct device *dev)
2261 struct common_obj *common;
2262 struct channel_obj *ch;
2265 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2266 /* Get the pointer to the channel object */
2267 ch = vpif_obj.dev[i];
2268 common = &ch->common[VPIF_VIDEO_INDEX];
2269 mutex_lock(&common->lock);
2270 if (ch->usrs && common->io_usrs) {
2271 /* Disable channel */
2272 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2274 channel0_intr_enable(1);
2276 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2277 common->started == 2) {
2279 channel1_intr_enable(1);
2282 mutex_unlock(&common->lock);
2288 static const struct dev_pm_ops vpif_dev_pm_ops = {
2289 .suspend = vpif_suspend,
2290 .resume = vpif_resume,
2293 #define vpif_pm_ops (&vpif_dev_pm_ops)
2295 #define vpif_pm_ops NULL
2298 static __refdata struct platform_driver vpif_driver = {
2300 .name = "vpif_capture",
2301 .owner = THIS_MODULE,
2304 .probe = vpif_probe,
2305 .remove = vpif_remove,
2308 module_platform_driver(vpif_driver);