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 spin_lock_irqsave(&common->irqlock, flags);
277 /* Get the next frame from the buffer queue */
278 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
279 struct vpif_cap_buffer, list);
280 /* Remove buffer from the buffer queue */
281 list_del(&common->cur_frm->list);
282 spin_unlock_irqrestore(&common->irqlock, flags);
283 /* Mark state of the current frame to active */
284 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
285 /* Initialize field_id and started member */
288 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
290 /* Calculate the offset for Y and C data in the buffer */
291 vpif_calculate_offsets(ch);
293 if ((vpif->std_info.frm_fmt &&
294 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
295 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
296 (!vpif->std_info.frm_fmt &&
297 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
298 vpif_dbg(1, debug, "conflict in field format and std format\n");
302 /* configure 1 or 2 channel mode */
303 if (vpif_config_data->setup_input_channel_mode) {
304 ret = vpif_config_data->
305 setup_input_channel_mode(vpif->std_info.ycmux_mode);
307 vpif_dbg(1, debug, "can't set vpif channel mode\n");
312 /* Call vpif_set_params function to set the parameters and addresses */
313 ret = vpif_set_video_params(vpif, ch->channel_id);
316 vpif_dbg(1, debug, "can't set video params\n");
320 common->started = ret;
321 vpif_config_addr(ch, ret);
323 common->set_addr(addr + common->ytop_off,
324 addr + common->ybtm_off,
325 addr + common->ctop_off,
326 addr + common->cbtm_off);
329 * Set interrupt for both the fields in VPIF Register enable channel in
332 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
333 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
334 channel0_intr_assert();
335 channel0_intr_enable(1);
338 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
339 (common->started == 2)) {
340 channel1_intr_assert();
341 channel1_intr_enable(1);
348 /* abort streaming and wait for last buffer */
349 static int vpif_stop_streaming(struct vb2_queue *vq)
351 struct vpif_fh *fh = vb2_get_drv_priv(vq);
352 struct channel_obj *ch = fh->channel;
353 struct common_obj *common;
356 if (!vb2_is_streaming(vq))
359 common = &ch->common[VPIF_VIDEO_INDEX];
361 /* release all active buffers */
362 spin_lock_irqsave(&common->irqlock, flags);
363 while (!list_empty(&common->dma_queue)) {
364 common->next_frm = list_entry(common->dma_queue.next,
365 struct vpif_cap_buffer, list);
366 list_del(&common->next_frm->list);
367 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
369 spin_unlock_irqrestore(&common->irqlock, flags);
374 static struct vb2_ops video_qops = {
375 .queue_setup = vpif_buffer_queue_setup,
376 .wait_prepare = vpif_wait_prepare,
377 .wait_finish = vpif_wait_finish,
378 .buf_init = vpif_buffer_init,
379 .buf_prepare = vpif_buffer_prepare,
380 .start_streaming = vpif_start_streaming,
381 .stop_streaming = vpif_stop_streaming,
382 .buf_cleanup = vpif_buf_cleanup,
383 .buf_queue = vpif_buffer_queue,
387 * vpif_process_buffer_complete: process a completed buffer
388 * @common: ptr to common channel object
390 * This function time stamp the buffer and mark it as DONE. It also
391 * wake up any process waiting on the QUEUE and set the next buffer
394 static void vpif_process_buffer_complete(struct common_obj *common)
396 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
397 vb2_buffer_done(&common->cur_frm->vb,
399 /* Make curFrm pointing to nextFrm */
400 common->cur_frm = common->next_frm;
404 * vpif_schedule_next_buffer: set next buffer address for capture
405 * @common : ptr to common channel object
407 * This function will get next buffer from the dma queue and
408 * set the buffer address in the vpif register for capture.
409 * the buffer is marked active
411 static void vpif_schedule_next_buffer(struct common_obj *common)
413 unsigned long addr = 0;
415 spin_lock(&common->irqlock);
416 common->next_frm = list_entry(common->dma_queue.next,
417 struct vpif_cap_buffer, list);
418 /* Remove that buffer from the buffer queue */
419 list_del(&common->next_frm->list);
420 spin_unlock(&common->irqlock);
421 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
422 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
424 /* Set top and bottom field addresses in VPIF registers */
425 common->set_addr(addr + common->ytop_off,
426 addr + common->ybtm_off,
427 addr + common->ctop_off,
428 addr + common->cbtm_off);
432 * vpif_channel_isr : ISR handler for vpif capture
434 * @dev_id: dev_id ptr
436 * It changes status of the captured buffer, takes next buffer from the queue
437 * and sets its address in VPIF registers
439 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
441 struct vpif_device *dev = &vpif_obj;
442 struct common_obj *common;
443 struct channel_obj *ch;
444 enum v4l2_field field;
448 channel_id = *(int *)(dev_id);
449 if (!vpif_intr_status(channel_id))
452 ch = dev->dev[channel_id];
454 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
456 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
457 common = &ch->common[i];
458 /* skip If streaming is not started in this channel */
459 if (0 == common->started)
462 /* Check the field format */
463 if (1 == ch->vpifparams.std_info.frm_fmt) {
464 /* Progressive mode */
465 spin_lock(&common->irqlock);
466 if (list_empty(&common->dma_queue)) {
467 spin_unlock(&common->irqlock);
470 spin_unlock(&common->irqlock);
472 if (!channel_first_int[i][channel_id])
473 vpif_process_buffer_complete(common);
475 channel_first_int[i][channel_id] = 0;
477 vpif_schedule_next_buffer(common);
480 channel_first_int[i][channel_id] = 0;
483 * Interlaced mode. If it is first interrupt, ignore
486 if (channel_first_int[i][channel_id]) {
487 channel_first_int[i][channel_id] = 0;
492 /* Get field id from VPIF registers */
493 fid = vpif_channel_getfid(ch->channel_id);
494 if (fid != ch->field_id) {
496 * If field id does not match stored
497 * field id, make them in sync
504 /* device field id and local field id are in sync */
506 /* this is even field */
507 if (common->cur_frm == common->next_frm)
510 /* mark the current buffer as done */
511 vpif_process_buffer_complete(common);
512 } else if (1 == fid) {
514 spin_lock(&common->irqlock);
515 if (list_empty(&common->dma_queue) ||
516 (common->cur_frm != common->next_frm)) {
517 spin_unlock(&common->irqlock);
520 spin_unlock(&common->irqlock);
522 vpif_schedule_next_buffer(common);
530 * vpif_update_std_info() - update standard related info
531 * @ch: ptr to channel object
533 * For a given standard selected by application, update values
534 * in the device data structures
536 static int vpif_update_std_info(struct channel_obj *ch)
538 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
539 struct vpif_params *vpifparams = &ch->vpifparams;
540 const struct vpif_channel_config_params *config;
541 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
542 struct video_obj *vid_ch = &ch->video;
545 vpif_dbg(2, debug, "vpif_update_std_info\n");
547 for (index = 0; index < vpif_ch_params_count; index++) {
548 config = &vpif_ch_params[index];
549 if (config->hd_sd == 0) {
550 vpif_dbg(2, debug, "SD format\n");
551 if (config->stdid & vid_ch->stdid) {
552 memcpy(std_info, config, sizeof(*config));
556 vpif_dbg(2, debug, "HD format\n");
557 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
558 sizeof(vid_ch->dv_timings))) {
559 memcpy(std_info, config, sizeof(*config));
565 /* standard not found */
566 if (index == vpif_ch_params_count)
569 common->fmt.fmt.pix.width = std_info->width;
570 common->width = std_info->width;
571 common->fmt.fmt.pix.height = std_info->height;
572 common->height = std_info->height;
573 common->fmt.fmt.pix.bytesperline = std_info->width;
574 vpifparams->video_params.hpitch = std_info->width;
575 vpifparams->video_params.storage_mode = std_info->frm_fmt;
581 * vpif_calculate_offsets : This function calculates buffers offsets
582 * @ch : ptr to channel object
584 * This function calculates buffer offsets for Y and C in the top and
587 static void vpif_calculate_offsets(struct channel_obj *ch)
589 unsigned int hpitch, vpitch, sizeimage;
590 struct video_obj *vid_ch = &(ch->video);
591 struct vpif_params *vpifparams = &ch->vpifparams;
592 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
593 enum v4l2_field field = common->fmt.fmt.pix.field;
595 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
597 if (V4L2_FIELD_ANY == field) {
598 if (vpifparams->std_info.frm_fmt)
599 vid_ch->buf_field = V4L2_FIELD_NONE;
601 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
603 vid_ch->buf_field = common->fmt.fmt.pix.field;
605 sizeimage = common->fmt.fmt.pix.sizeimage;
607 hpitch = common->fmt.fmt.pix.bytesperline;
608 vpitch = sizeimage / (hpitch * 2);
610 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
611 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
612 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
613 common->ytop_off = 0;
614 common->ybtm_off = hpitch;
615 common->ctop_off = sizeimage / 2;
616 common->cbtm_off = sizeimage / 2 + hpitch;
617 } else if (V4L2_FIELD_SEQ_TB == 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 = sizeimage / 4;
621 common->ctop_off = sizeimage / 2;
622 common->cbtm_off = common->ctop_off + sizeimage / 4;
623 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
624 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
625 common->ybtm_off = 0;
626 common->ytop_off = sizeimage / 4;
627 common->cbtm_off = sizeimage / 2;
628 common->ctop_off = common->cbtm_off + sizeimage / 4;
630 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
631 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
632 vpifparams->video_params.storage_mode = 1;
634 vpifparams->video_params.storage_mode = 0;
636 if (1 == vpifparams->std_info.frm_fmt)
637 vpifparams->video_params.hpitch =
638 common->fmt.fmt.pix.bytesperline;
640 if ((field == V4L2_FIELD_ANY)
641 || (field == V4L2_FIELD_INTERLACED))
642 vpifparams->video_params.hpitch =
643 common->fmt.fmt.pix.bytesperline * 2;
645 vpifparams->video_params.hpitch =
646 common->fmt.fmt.pix.bytesperline;
649 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
653 * vpif_config_format: configure default frame format in the device
654 * ch : ptr to channel object
656 static void vpif_config_format(struct channel_obj *ch)
658 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
660 vpif_dbg(2, debug, "vpif_config_format\n");
662 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
663 if (config_params.numbuffers[ch->channel_id] == 0)
664 common->memory = V4L2_MEMORY_USERPTR;
666 common->memory = V4L2_MEMORY_MMAP;
668 common->fmt.fmt.pix.sizeimage
669 = config_params.channel_bufsize[ch->channel_id];
671 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
672 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
674 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
675 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
679 * vpif_get_default_field() - Get default field type based on interface
680 * @vpif_params - ptr to vpif params
682 static inline enum v4l2_field vpif_get_default_field(
683 struct vpif_interface *iface)
685 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
686 V4L2_FIELD_INTERLACED;
690 * vpif_check_format() - check given pixel format for compatibility
692 * @pixfmt - Given pixel format
693 * @update - update the values as per hardware requirement
695 * Check the application pixel format for S_FMT and update the input
696 * values as per hardware limits for TRY_FMT. The default pixel and
697 * field format is selected based on interface type.
699 static int vpif_check_format(struct channel_obj *ch,
700 struct v4l2_pix_format *pixfmt,
703 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
704 struct vpif_params *vpif_params = &ch->vpifparams;
705 enum v4l2_field field = pixfmt->field;
706 u32 sizeimage, hpitch, vpitch;
709 vpif_dbg(2, debug, "vpif_check_format\n");
711 * first check for the pixel format. If if_type is Raw bayer,
712 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
713 * V4L2_PIX_FMT_YUV422P is supported
715 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
716 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
718 vpif_dbg(2, debug, "invalid pix format\n");
721 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
724 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
726 vpif_dbg(2, debug, "invalid pixel format\n");
729 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
733 if (!(VPIF_VALID_FIELD(field))) {
735 vpif_dbg(2, debug, "invalid field format\n");
739 * By default use FIELD_NONE for RAW Bayer capture
740 * and FIELD_INTERLACED for other interfaces
742 field = vpif_get_default_field(&vpif_params->iface);
743 } else if (field == V4L2_FIELD_ANY)
744 /* unsupported field. Use default */
745 field = vpif_get_default_field(&vpif_params->iface);
747 /* validate the hpitch */
748 hpitch = pixfmt->bytesperline;
749 if (hpitch < vpif_params->std_info.width) {
751 vpif_dbg(2, debug, "invalid hpitch\n");
754 hpitch = vpif_params->std_info.width;
757 sizeimage = pixfmt->sizeimage;
759 vpitch = sizeimage / (hpitch * 2);
761 /* validate the vpitch */
762 if (vpitch < vpif_params->std_info.height) {
764 vpif_dbg(2, debug, "Invalid vpitch\n");
767 vpitch = vpif_params->std_info.height;
770 /* Check for 8 byte alignment */
771 if (!ALIGN(hpitch, 8)) {
773 vpif_dbg(2, debug, "invalid pitch alignment\n");
776 /* adjust to next 8 byte boundary */
777 hpitch = (((hpitch + 7) / 8) * 8);
779 /* if update is set, modify the bytesperline and sizeimage */
781 pixfmt->bytesperline = hpitch;
782 pixfmt->sizeimage = hpitch * vpitch * 2;
785 * Image width and height is always based on current standard width and
788 pixfmt->width = common->fmt.fmt.pix.width;
789 pixfmt->height = common->fmt.fmt.pix.height;
796 * vpif_config_addr() - function to configure buffer address in vpif
798 * @muxmode - channel mux mode
800 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
802 struct common_obj *common;
804 vpif_dbg(2, debug, "vpif_config_addr\n");
806 common = &(ch->common[VPIF_VIDEO_INDEX]);
808 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
809 common->set_addr = ch1_set_videobuf_addr;
810 else if (2 == muxmode)
811 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
813 common->set_addr = ch0_set_videobuf_addr;
817 * vpif_mmap : It is used to map kernel space buffers into user spaces
818 * @filep: file pointer
819 * @vma: ptr to vm_area_struct
821 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
823 /* Get the channel object and file handle object */
824 struct vpif_fh *fh = filep->private_data;
825 struct channel_obj *ch = fh->channel;
826 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
829 vpif_dbg(2, debug, "vpif_mmap\n");
831 if (mutex_lock_interruptible(&common->lock))
833 ret = vb2_mmap(&common->buffer_queue, vma);
834 mutex_unlock(&common->lock);
839 * vpif_poll: It is used for select/poll system call
840 * @filep: file pointer
841 * @wait: poll table to wait
843 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
845 struct vpif_fh *fh = filep->private_data;
846 struct channel_obj *channel = fh->channel;
847 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
848 unsigned int res = 0;
850 vpif_dbg(2, debug, "vpif_poll\n");
852 if (common->started) {
853 mutex_lock(&common->lock);
854 res = vb2_poll(&common->buffer_queue, filep, wait);
855 mutex_unlock(&common->lock);
861 * vpif_open : vpif open handler
864 * It creates object of file handle structure and stores it in private_data
865 * member of filepointer
867 static int vpif_open(struct file *filep)
869 struct video_device *vdev = video_devdata(filep);
870 struct common_obj *common;
871 struct video_obj *vid_ch;
872 struct channel_obj *ch;
875 vpif_dbg(2, debug, "vpif_open\n");
877 ch = video_get_drvdata(vdev);
880 common = &ch->common[VPIF_VIDEO_INDEX];
882 /* Allocate memory for the file handle object */
883 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
885 vpif_err("unable to allocate memory for file handle object\n");
889 if (mutex_lock_interruptible(&common->lock)) {
893 /* store pointer to fh in private_data member of filep */
894 filep->private_data = fh;
897 /* If decoder is not initialized. initialize it */
898 if (!ch->initialized) {
901 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
903 /* Increment channel usrs counter */
905 /* Set io_allowed member to false */
906 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
907 /* Initialize priority of this instance to default priority */
908 fh->prio = V4L2_PRIORITY_UNSET;
909 v4l2_prio_open(&ch->prio, &fh->prio);
910 mutex_unlock(&common->lock);
915 * vpif_release : function to clean up file close
916 * @filep: file pointer
918 * This function deletes buffer queue, frees the buffers and the vpif file
921 static int vpif_release(struct file *filep)
923 struct vpif_fh *fh = filep->private_data;
924 struct channel_obj *ch = fh->channel;
925 struct common_obj *common;
927 vpif_dbg(2, debug, "vpif_release\n");
929 common = &ch->common[VPIF_VIDEO_INDEX];
931 mutex_lock(&common->lock);
932 /* if this instance is doing IO */
933 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
934 /* Reset io_usrs member of channel object */
936 /* Disable channel as per its device type and channel id */
937 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
939 channel0_intr_enable(0);
941 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
942 (2 == common->started)) {
944 channel1_intr_enable(0);
947 /* Free buffers allocated */
948 vb2_queue_release(&common->buffer_queue);
949 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
952 /* Decrement channel usrs counter */
955 /* Close the priority */
956 v4l2_prio_close(&ch->prio, fh->prio);
961 mutex_unlock(&common->lock);
962 filep->private_data = NULL;
968 * vpif_reqbufs() - request buffer handler
971 * @reqbuf: request buffer structure ptr
973 static int vpif_reqbufs(struct file *file, void *priv,
974 struct v4l2_requestbuffers *reqbuf)
976 struct vpif_fh *fh = priv;
977 struct channel_obj *ch = fh->channel;
978 struct common_obj *common;
983 vpif_dbg(2, debug, "vpif_reqbufs\n");
986 * This file handle has not initialized the channel,
987 * It is not allowed to do settings
989 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
990 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
991 if (!fh->initialized) {
992 vpif_dbg(1, debug, "Channel Busy\n");
997 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
1000 index = VPIF_VIDEO_INDEX;
1002 common = &ch->common[index];
1004 if (0 != common->io_usrs)
1007 /* Initialize videobuf2 queue as per the buffer type */
1008 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1009 if (IS_ERR(common->alloc_ctx)) {
1010 vpif_err("Failed to get the context\n");
1011 return PTR_ERR(common->alloc_ctx);
1013 q = &common->buffer_queue;
1014 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1015 q->io_modes = VB2_MMAP | VB2_USERPTR;
1017 q->ops = &video_qops;
1018 q->mem_ops = &vb2_dma_contig_memops;
1019 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1020 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1022 ret = vb2_queue_init(q);
1024 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1025 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1028 /* Set io allowed member of file handle to TRUE */
1029 fh->io_allowed[index] = 1;
1030 /* Increment io usrs member of channel object to 1 */
1031 common->io_usrs = 1;
1032 /* Store type of memory requested in channel object */
1033 common->memory = reqbuf->memory;
1034 INIT_LIST_HEAD(&common->dma_queue);
1036 /* Allocate buffers */
1037 return vb2_reqbufs(&common->buffer_queue, reqbuf);
1041 * vpif_querybuf() - query buffer handler
1043 * @priv: file handle
1044 * @buf: v4l2 buffer structure ptr
1046 static int vpif_querybuf(struct file *file, void *priv,
1047 struct v4l2_buffer *buf)
1049 struct vpif_fh *fh = priv;
1050 struct channel_obj *ch = fh->channel;
1051 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1053 vpif_dbg(2, debug, "vpif_querybuf\n");
1055 if (common->fmt.type != buf->type)
1058 if (common->memory != V4L2_MEMORY_MMAP) {
1059 vpif_dbg(1, debug, "Invalid memory\n");
1063 return vb2_querybuf(&common->buffer_queue, buf);
1067 * vpif_qbuf() - query buffer handler
1069 * @priv: file handle
1070 * @buf: v4l2 buffer structure ptr
1072 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1075 struct vpif_fh *fh = priv;
1076 struct channel_obj *ch = fh->channel;
1077 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1078 struct v4l2_buffer tbuf = *buf;
1080 vpif_dbg(2, debug, "vpif_qbuf\n");
1082 if (common->fmt.type != tbuf.type) {
1083 vpif_err("invalid buffer type\n");
1087 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1088 vpif_err("fh io not allowed\n");
1092 return vb2_qbuf(&common->buffer_queue, buf);
1096 * vpif_dqbuf() - query buffer handler
1098 * @priv: file handle
1099 * @buf: v4l2 buffer structure ptr
1101 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1103 struct vpif_fh *fh = priv;
1104 struct channel_obj *ch = fh->channel;
1105 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1107 vpif_dbg(2, debug, "vpif_dqbuf\n");
1109 return vb2_dqbuf(&common->buffer_queue, buf,
1110 (file->f_flags & O_NONBLOCK));
1114 * vpif_streamon() - streamon handler
1116 * @priv: file handle
1117 * @buftype: v4l2 buffer type
1119 static int vpif_streamon(struct file *file, void *priv,
1120 enum v4l2_buf_type buftype)
1123 struct vpif_fh *fh = priv;
1124 struct channel_obj *ch = fh->channel;
1125 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1126 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1127 struct vpif_params *vpif;
1130 vpif_dbg(2, debug, "vpif_streamon\n");
1132 vpif = &ch->vpifparams;
1134 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1135 vpif_dbg(1, debug, "buffer type not supported\n");
1139 /* If file handle is not allowed IO, return error */
1140 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1141 vpif_dbg(1, debug, "io not allowed\n");
1145 /* If Streaming is already started, return error */
1146 if (common->started) {
1147 vpif_dbg(1, debug, "channel->started\n");
1151 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1152 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1153 vpif->std_info.ycmux_mode == 0) ||
1154 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1155 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1156 vpif_dbg(1, debug, "other channel is being used\n");
1160 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1164 /* Enable streamon on the sub device */
1165 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
1167 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1168 vpif_dbg(1, debug, "stream on failed in subdev\n");
1172 /* Call vb2_streamon to start streaming in videobuf2 */
1173 ret = vb2_streamon(&common->buffer_queue, buftype);
1175 vpif_dbg(1, debug, "vb2_streamon\n");
1183 * vpif_streamoff() - streamoff handler
1185 * @priv: file handle
1186 * @buftype: v4l2 buffer type
1188 static int vpif_streamoff(struct file *file, void *priv,
1189 enum v4l2_buf_type buftype)
1192 struct vpif_fh *fh = priv;
1193 struct channel_obj *ch = fh->channel;
1194 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1197 vpif_dbg(2, debug, "vpif_streamoff\n");
1199 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1200 vpif_dbg(1, debug, "buffer type not supported\n");
1204 /* If io is allowed for this file handle, return error */
1205 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1206 vpif_dbg(1, debug, "io not allowed\n");
1210 /* If streaming is not started, return error */
1211 if (!common->started) {
1212 vpif_dbg(1, debug, "channel->started\n");
1216 /* disable channel */
1217 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1219 channel0_intr_enable(0);
1222 channel1_intr_enable(0);
1225 common->started = 0;
1227 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
1229 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1230 vpif_dbg(1, debug, "stream off failed in subdev\n");
1232 return vb2_streamoff(&common->buffer_queue, buftype);
1236 * vpif_input_to_subdev() - Maps input to sub device
1237 * @vpif_cfg - global config ptr
1238 * @chan_cfg - channel config ptr
1239 * @input_index - Given input index from application
1241 * lookup the sub device information for a given input index.
1242 * we report all the inputs to application. inputs table also
1243 * has sub device name for the each input
1245 static int vpif_input_to_subdev(
1246 struct vpif_capture_config *vpif_cfg,
1247 struct vpif_capture_chan_config *chan_cfg,
1250 struct vpif_subdev_info *subdev_info;
1251 const char *subdev_name;
1254 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
1256 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1257 if (subdev_name == NULL)
1260 /* loop through the sub device list to get the sub device info */
1261 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1262 subdev_info = &vpif_cfg->subdev_info[i];
1263 if (!strcmp(subdev_info->name, subdev_name))
1270 * vpif_set_input() - Select an input
1271 * @vpif_cfg - global config ptr
1273 * @_index - Given input index from application
1275 * Select the given input.
1277 static int vpif_set_input(
1278 struct vpif_capture_config *vpif_cfg,
1279 struct channel_obj *ch,
1282 struct vpif_capture_chan_config *chan_cfg =
1283 &vpif_cfg->chan_config[ch->channel_id];
1284 struct vpif_subdev_info *subdev_info = NULL;
1285 struct v4l2_subdev *sd = NULL;
1286 u32 input = 0, output = 0;
1290 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1291 if (sd_index >= 0) {
1292 sd = vpif_obj.sd[sd_index];
1293 subdev_info = &vpif_cfg->subdev_info[sd_index];
1296 /* first setup input path from sub device to vpif */
1297 if (sd && vpif_cfg->setup_input_path) {
1298 ret = vpif_cfg->setup_input_path(ch->channel_id,
1301 vpif_dbg(1, debug, "couldn't setup input path for the" \
1302 " sub device %s, for input index %d\n",
1303 subdev_info->name, index);
1309 input = chan_cfg->inputs[index].input_route;
1310 output = chan_cfg->inputs[index].output_route;
1311 ret = v4l2_subdev_call(sd, video, s_routing,
1313 if (ret < 0 && ret != -ENOIOCTLCMD) {
1314 vpif_dbg(1, debug, "Failed to set input\n");
1318 ch->input_idx = index;
1320 /* copy interface parameters to vpif */
1321 ch->vpifparams.iface = chan_cfg->vpif_if;
1323 /* update tvnorms from the sub device input info */
1324 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1329 * vpif_querystd() - querystd handler
1331 * @priv: file handle
1332 * @std_id: ptr to std id
1334 * This function is called to detect standard at the selected input
1336 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1338 struct vpif_fh *fh = priv;
1339 struct channel_obj *ch = fh->channel;
1342 vpif_dbg(2, debug, "vpif_querystd\n");
1344 /* Call querystd function of decoder device */
1345 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
1347 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1350 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1358 * vpif_g_std() - get STD handler
1360 * @priv: file handle
1361 * @std_id: ptr to std id
1363 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1365 struct vpif_fh *fh = priv;
1366 struct channel_obj *ch = fh->channel;
1368 vpif_dbg(2, debug, "vpif_g_std\n");
1370 *std = ch->video.stdid;
1375 * vpif_s_std() - set STD handler
1377 * @priv: file handle
1378 * @std_id: ptr to std id
1380 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1382 struct vpif_fh *fh = priv;
1383 struct channel_obj *ch = fh->channel;
1384 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1387 vpif_dbg(2, debug, "vpif_s_std\n");
1389 if (common->started) {
1390 vpif_err("streaming in progress\n");
1394 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1395 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1396 if (!fh->initialized) {
1397 vpif_dbg(1, debug, "Channel Busy\n");
1402 ret = v4l2_prio_check(&ch->prio, fh->prio);
1406 fh->initialized = 1;
1408 /* Call encoder subdevice function to set the standard */
1409 ch->video.stdid = std_id;
1410 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1412 /* Get the information about the standard */
1413 if (vpif_update_std_info(ch)) {
1414 vpif_err("Error getting the standard info\n");
1418 /* Configure the default format information */
1419 vpif_config_format(ch);
1421 /* set standard in the sub device */
1422 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
1423 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1424 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1431 * vpif_enum_input() - ENUMINPUT handler
1433 * @priv: file handle
1434 * @input: ptr to input structure
1436 static int vpif_enum_input(struct file *file, void *priv,
1437 struct v4l2_input *input)
1440 struct vpif_capture_config *config = vpif_dev->platform_data;
1441 struct vpif_capture_chan_config *chan_cfg;
1442 struct vpif_fh *fh = priv;
1443 struct channel_obj *ch = fh->channel;
1445 chan_cfg = &config->chan_config[ch->channel_id];
1447 if (input->index >= chan_cfg->input_count) {
1448 vpif_dbg(1, debug, "Invalid input index\n");
1452 memcpy(input, &chan_cfg->inputs[input->index].input,
1458 * vpif_g_input() - Get INPUT handler
1460 * @priv: file handle
1461 * @index: ptr to input index
1463 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1465 struct vpif_fh *fh = priv;
1466 struct channel_obj *ch = fh->channel;
1468 *index = ch->input_idx;
1473 * vpif_s_input() - Set INPUT handler
1475 * @priv: file handle
1476 * @index: input index
1478 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1480 struct vpif_capture_config *config = vpif_dev->platform_data;
1481 struct vpif_capture_chan_config *chan_cfg;
1482 struct vpif_fh *fh = priv;
1483 struct channel_obj *ch = fh->channel;
1484 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1487 chan_cfg = &config->chan_config[ch->channel_id];
1489 if (index >= chan_cfg->input_count)
1492 if (common->started) {
1493 vpif_err("Streaming in progress\n");
1497 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1498 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1499 if (!fh->initialized) {
1500 vpif_dbg(1, debug, "Channel Busy\n");
1505 ret = v4l2_prio_check(&ch->prio, fh->prio);
1509 fh->initialized = 1;
1510 return vpif_set_input(config, ch, index);
1514 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1516 * @priv: file handle
1517 * @index: input index
1519 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1520 struct v4l2_fmtdesc *fmt)
1522 struct vpif_fh *fh = priv;
1523 struct channel_obj *ch = fh->channel;
1525 if (fmt->index != 0) {
1526 vpif_dbg(1, debug, "Invalid format index\n");
1530 /* Fill in the information about format */
1531 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1532 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1533 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1534 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1536 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1537 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1538 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1544 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1546 * @priv: file handle
1547 * @fmt: ptr to v4l2 format structure
1549 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1550 struct v4l2_format *fmt)
1552 struct vpif_fh *fh = priv;
1553 struct channel_obj *ch = fh->channel;
1554 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1556 return vpif_check_format(ch, pixfmt, 1);
1561 * vpif_g_fmt_vid_cap() - Set INPUT handler
1563 * @priv: file handle
1564 * @fmt: ptr to v4l2 format structure
1566 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1567 struct v4l2_format *fmt)
1569 struct vpif_fh *fh = priv;
1570 struct channel_obj *ch = fh->channel;
1571 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1573 /* Check the validity of the buffer type */
1574 if (common->fmt.type != fmt->type)
1577 /* Fill in the information about format */
1583 * vpif_s_fmt_vid_cap() - Set FMT handler
1585 * @priv: file handle
1586 * @fmt: ptr to v4l2 format structure
1588 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1589 struct v4l2_format *fmt)
1591 struct vpif_fh *fh = priv;
1592 struct channel_obj *ch = fh->channel;
1593 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1594 struct v4l2_pix_format *pixfmt;
1597 vpif_dbg(2, debug, "%s\n", __func__);
1599 /* If streaming is started, return error */
1600 if (common->started) {
1601 vpif_dbg(1, debug, "Streaming is started\n");
1605 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1606 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1607 if (!fh->initialized) {
1608 vpif_dbg(1, debug, "Channel Busy\n");
1613 ret = v4l2_prio_check(&ch->prio, fh->prio);
1617 fh->initialized = 1;
1619 pixfmt = &fmt->fmt.pix;
1620 /* Check for valid field format */
1621 ret = vpif_check_format(ch, pixfmt, 0);
1625 /* store the format in the channel object */
1631 * vpif_querycap() - QUERYCAP handler
1633 * @priv: file handle
1634 * @cap: ptr to v4l2_capability structure
1636 static int vpif_querycap(struct file *file, void *priv,
1637 struct v4l2_capability *cap)
1639 struct vpif_capture_config *config = vpif_dev->platform_data;
1641 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1642 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1643 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1644 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1645 dev_name(vpif_dev));
1646 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1652 * vpif_g_priority() - get priority handler
1654 * @priv: file handle
1655 * @prio: ptr to v4l2_priority structure
1657 static int vpif_g_priority(struct file *file, void *priv,
1658 enum v4l2_priority *prio)
1660 struct vpif_fh *fh = priv;
1661 struct channel_obj *ch = fh->channel;
1663 *prio = v4l2_prio_max(&ch->prio);
1669 * vpif_s_priority() - set priority handler
1671 * @priv: file handle
1672 * @prio: ptr to v4l2_priority structure
1674 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1676 struct vpif_fh *fh = priv;
1677 struct channel_obj *ch = fh->channel;
1679 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1683 * vpif_cropcap() - cropcap handler
1685 * @priv: file handle
1686 * @crop: ptr to v4l2_cropcap structure
1688 static int vpif_cropcap(struct file *file, void *priv,
1689 struct v4l2_cropcap *crop)
1691 struct vpif_fh *fh = priv;
1692 struct channel_obj *ch = fh->channel;
1693 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1695 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1698 crop->bounds.left = 0;
1699 crop->bounds.top = 0;
1700 crop->bounds.height = common->height;
1701 crop->bounds.width = common->width;
1702 crop->defrect = crop->bounds;
1707 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1709 * @priv: file handle
1710 * @timings: input timings
1713 vpif_enum_dv_timings(struct file *file, void *priv,
1714 struct v4l2_enum_dv_timings *timings)
1716 struct vpif_fh *fh = priv;
1717 struct channel_obj *ch = fh->channel;
1720 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1721 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1727 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1729 * @priv: file handle
1730 * @timings: input timings
1733 vpif_query_dv_timings(struct file *file, void *priv,
1734 struct v4l2_dv_timings *timings)
1736 struct vpif_fh *fh = priv;
1737 struct channel_obj *ch = fh->channel;
1740 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1741 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1747 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1749 * @priv: file handle
1750 * @timings: digital video timings
1752 static int vpif_s_dv_timings(struct file *file, void *priv,
1753 struct v4l2_dv_timings *timings)
1755 struct vpif_fh *fh = priv;
1756 struct channel_obj *ch = fh->channel;
1757 struct vpif_params *vpifparams = &ch->vpifparams;
1758 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1759 struct video_obj *vid_ch = &ch->video;
1760 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1763 if (timings->type != V4L2_DV_BT_656_1120) {
1764 vpif_dbg(2, debug, "Timing type not defined\n");
1768 /* Configure subdevice timings, if any */
1769 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1770 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1773 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1777 if (!(timings->bt.width && timings->bt.height &&
1778 (timings->bt.hbackporch ||
1779 timings->bt.hfrontporch ||
1780 timings->bt.hsync) &&
1781 timings->bt.vfrontporch &&
1782 (timings->bt.vbackporch ||
1783 timings->bt.vsync))) {
1784 vpif_dbg(2, debug, "Timings for width, height, "
1785 "horizontal back porch, horizontal sync, "
1786 "horizontal front porch, vertical back porch, "
1787 "vertical sync and vertical back porch "
1788 "must be defined\n");
1792 vid_ch->dv_timings = *timings;
1794 /* Configure video port timings */
1796 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1797 std_info->sav2eav = bt->width;
1800 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1802 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1803 if (bt->interlaced) {
1804 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1805 std_info->l5 = std_info->vsize/2 -
1806 (bt->vfrontporch - 1);
1807 std_info->l7 = std_info->vsize/2 + 1;
1808 std_info->l9 = std_info->l7 + bt->il_vsync +
1809 bt->il_vbackporch + 1;
1810 std_info->l11 = std_info->vsize -
1811 (bt->il_vfrontporch - 1);
1813 vpif_dbg(2, debug, "Required timing values for "
1814 "interlaced BT format missing\n");
1818 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1820 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1821 std_info->width = bt->width;
1822 std_info->height = bt->height;
1823 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1824 std_info->ycmux_mode = 0;
1825 std_info->capture_format = 0;
1826 std_info->vbi_supported = 0;
1827 std_info->hd_sd = 1;
1828 std_info->stdid = 0;
1835 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1837 * @priv: file handle
1838 * @timings: digital video timings
1840 static int vpif_g_dv_timings(struct file *file, void *priv,
1841 struct v4l2_dv_timings *timings)
1843 struct vpif_fh *fh = priv;
1844 struct channel_obj *ch = fh->channel;
1845 struct video_obj *vid_ch = &ch->video;
1847 *timings = vid_ch->dv_timings;
1853 * vpif_log_status() - Status information
1855 * @priv: file handle
1859 static int vpif_log_status(struct file *filep, void *priv)
1861 /* status for sub devices */
1862 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1867 /* vpif capture ioctl operations */
1868 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1869 .vidioc_querycap = vpif_querycap,
1870 .vidioc_g_priority = vpif_g_priority,
1871 .vidioc_s_priority = vpif_s_priority,
1872 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1873 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1874 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1875 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1876 .vidioc_enum_input = vpif_enum_input,
1877 .vidioc_s_input = vpif_s_input,
1878 .vidioc_g_input = vpif_g_input,
1879 .vidioc_reqbufs = vpif_reqbufs,
1880 .vidioc_querybuf = vpif_querybuf,
1881 .vidioc_querystd = vpif_querystd,
1882 .vidioc_s_std = vpif_s_std,
1883 .vidioc_g_std = vpif_g_std,
1884 .vidioc_qbuf = vpif_qbuf,
1885 .vidioc_dqbuf = vpif_dqbuf,
1886 .vidioc_streamon = vpif_streamon,
1887 .vidioc_streamoff = vpif_streamoff,
1888 .vidioc_cropcap = vpif_cropcap,
1889 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1890 .vidioc_query_dv_timings = vpif_query_dv_timings,
1891 .vidioc_s_dv_timings = vpif_s_dv_timings,
1892 .vidioc_g_dv_timings = vpif_g_dv_timings,
1893 .vidioc_log_status = vpif_log_status,
1896 /* vpif file operations */
1897 static struct v4l2_file_operations vpif_fops = {
1898 .owner = THIS_MODULE,
1900 .release = vpif_release,
1901 .unlocked_ioctl = video_ioctl2,
1906 /* vpif video template */
1907 static struct video_device vpif_video_template = {
1911 .ioctl_ops = &vpif_ioctl_ops,
1915 * initialize_vpif() - Initialize vpif data structures
1917 * Allocate memory for data structures and initialize them
1919 static int initialize_vpif(void)
1922 int free_channel_objects_index;
1924 /* Default number of buffers should be 3 */
1925 if ((ch0_numbuffers > 0) &&
1926 (ch0_numbuffers < config_params.min_numbuffers))
1927 ch0_numbuffers = config_params.min_numbuffers;
1928 if ((ch1_numbuffers > 0) &&
1929 (ch1_numbuffers < config_params.min_numbuffers))
1930 ch1_numbuffers = config_params.min_numbuffers;
1932 /* Set buffer size to min buffers size if it is invalid */
1933 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1935 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1936 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1938 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1940 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1941 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1942 if (ch0_numbuffers) {
1943 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1946 if (ch1_numbuffers) {
1947 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1951 /* Allocate memory for six channel objects */
1952 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1954 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1955 /* If memory allocation fails, return error */
1956 if (!vpif_obj.dev[i]) {
1957 free_channel_objects_index = i;
1959 goto vpif_init_free_channel_objects;
1964 vpif_init_free_channel_objects:
1965 for (j = 0; j < free_channel_objects_index; j++)
1966 kfree(vpif_obj.dev[j]);
1970 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1971 struct v4l2_subdev *subdev,
1972 struct v4l2_async_subdev *asd)
1976 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1977 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1979 vpif_obj.sd[i] = subdev;
1986 static int vpif_probe_complete(void)
1988 struct common_obj *common;
1989 struct channel_obj *ch;
1992 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1993 ch = vpif_obj.dev[j];
1995 common = &(ch->common[VPIF_VIDEO_INDEX]);
1996 spin_lock_init(&common->irqlock);
1997 mutex_init(&common->lock);
1998 ch->video_dev->lock = &common->lock;
1999 /* Initialize prio member of channel object */
2000 v4l2_prio_init(&ch->prio);
2001 video_set_drvdata(ch->video_dev, ch);
2003 /* select input 0 */
2004 err = vpif_set_input(vpif_obj.config, ch, 0);
2008 err = video_register_device(ch->video_dev,
2009 VFL_TYPE_GRABBER, (j ? 1 : 0));
2014 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2018 for (k = 0; k < j; k++) {
2019 /* Get the pointer to the channel object */
2020 ch = vpif_obj.dev[k];
2021 /* Unregister video device */
2022 video_unregister_device(ch->video_dev);
2025 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2026 ch = vpif_obj.dev[i];
2027 /* Note: does nothing if ch->video_dev == NULL */
2028 video_device_release(ch->video_dev);
2030 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2035 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2037 return vpif_probe_complete();
2041 * vpif_probe : This function probes the vpif capture driver
2042 * @pdev: platform device pointer
2044 * This creates device entries by register itself to the V4L2 driver and
2045 * initializes fields of each channel objects
2047 static __init int vpif_probe(struct platform_device *pdev)
2049 struct vpif_subdev_info *subdevdata;
2052 struct i2c_adapter *i2c_adap;
2053 struct channel_obj *ch;
2054 struct video_device *vfd;
2055 struct resource *res;
2059 vpif_dev = &pdev->dev;
2061 err = initialize_vpif();
2063 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2067 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2069 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2073 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
2074 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2075 IRQF_SHARED, "VPIF_Capture",
2076 (void *)(&vpif_obj.dev[res_idx]->
2080 goto vpif_unregister;
2085 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2086 /* Get the pointer to the channel object */
2087 ch = vpif_obj.dev[i];
2088 /* Allocate memory for video device */
2089 vfd = video_device_alloc();
2091 for (j = 0; j < i; j++) {
2092 ch = vpif_obj.dev[j];
2093 video_device_release(ch->video_dev);
2096 goto vpif_unregister;
2099 /* Initialize field of video device */
2100 *vfd = vpif_video_template;
2101 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2102 vfd->release = video_device_release;
2103 snprintf(vfd->name, sizeof(vfd->name),
2104 "VPIF_Capture_DRIVER_V%s",
2105 VPIF_CAPTURE_VERSION);
2106 /* Set video_dev to the video device */
2107 ch->video_dev = vfd;
2110 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2112 size = resource_size(res);
2113 /* The resources are divided into two equal memory and when we
2114 * have HD output we can add them together
2116 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2117 ch = vpif_obj.dev[j];
2119 /* only enabled if second resource exists */
2120 config_params.video_limit[ch->channel_id] = 0;
2122 config_params.video_limit[ch->channel_id] =
2127 vpif_obj.config = pdev->dev.platform_data;
2129 subdev_count = vpif_obj.config->subdev_count;
2130 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2132 if (vpif_obj.sd == NULL) {
2133 vpif_err("unable to allocate memory for subdevice pointers\n");
2138 if (!vpif_obj.config->asd_sizes) {
2139 i2c_adap = i2c_get_adapter(1);
2140 for (i = 0; i < subdev_count; i++) {
2141 subdevdata = &vpif_obj.config->subdev_info[i];
2143 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2149 if (!vpif_obj.sd[i]) {
2150 vpif_err("Error registering v4l2 subdevice\n");
2152 goto probe_subdev_out;
2154 v4l2_info(&vpif_obj.v4l2_dev,
2155 "registered sub device %s\n",
2158 vpif_probe_complete();
2160 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
2161 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2162 vpif_obj.notifier.bound = vpif_async_bound;
2163 vpif_obj.notifier.complete = vpif_async_complete;
2164 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2165 &vpif_obj.notifier);
2167 vpif_err("Error registering async notifier\n");
2169 goto probe_subdev_out;
2176 /* free sub devices memory */
2180 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2181 ch = vpif_obj.dev[i];
2182 /* Note: does nothing if ch->video_dev == NULL */
2183 video_device_release(ch->video_dev);
2186 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2192 * vpif_remove() - driver remove handler
2193 * @device: ptr to platform device structure
2195 * The vidoe device is unregistered
2197 static int vpif_remove(struct platform_device *device)
2199 struct channel_obj *ch;
2202 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2205 /* un-register device */
2206 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2207 /* Get the pointer to the channel object */
2208 ch = vpif_obj.dev[i];
2209 /* Unregister video device */
2210 video_unregister_device(ch->video_dev);
2211 kfree(vpif_obj.dev[i]);
2218 * vpif_suspend: vpif device suspend
2220 static int vpif_suspend(struct device *dev)
2223 struct common_obj *common;
2224 struct channel_obj *ch;
2227 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2228 /* Get the pointer to the channel object */
2229 ch = vpif_obj.dev[i];
2230 common = &ch->common[VPIF_VIDEO_INDEX];
2231 mutex_lock(&common->lock);
2232 if (ch->usrs && common->io_usrs) {
2233 /* Disable channel */
2234 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2236 channel0_intr_enable(0);
2238 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2239 common->started == 2) {
2241 channel1_intr_enable(0);
2244 mutex_unlock(&common->lock);
2251 * vpif_resume: vpif device suspend
2253 static int vpif_resume(struct device *dev)
2255 struct common_obj *common;
2256 struct channel_obj *ch;
2259 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2260 /* Get the pointer to the channel object */
2261 ch = vpif_obj.dev[i];
2262 common = &ch->common[VPIF_VIDEO_INDEX];
2263 mutex_lock(&common->lock);
2264 if (ch->usrs && common->io_usrs) {
2265 /* Disable channel */
2266 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2268 channel0_intr_enable(1);
2270 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2271 common->started == 2) {
2273 channel1_intr_enable(1);
2276 mutex_unlock(&common->lock);
2282 static const struct dev_pm_ops vpif_dev_pm_ops = {
2283 .suspend = vpif_suspend,
2284 .resume = vpif_resume,
2287 #define vpif_pm_ops (&vpif_dev_pm_ops)
2289 #define vpif_pm_ops NULL
2292 static __refdata struct platform_driver vpif_driver = {
2294 .name = "vpif_capture",
2295 .owner = THIS_MODULE,
2298 .probe = vpif_probe,
2299 .remove = vpif_remove,
2302 module_platform_driver(vpif_driver);