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 /* Disable channel as per its device type and channel id */
362 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
364 channel0_intr_enable(0);
366 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
367 (2 == common->started)) {
369 channel1_intr_enable(0);
373 /* release all active buffers */
374 spin_lock_irqsave(&common->irqlock, flags);
375 if (common->cur_frm == common->next_frm) {
376 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
378 if (common->cur_frm != NULL)
379 vb2_buffer_done(&common->cur_frm->vb,
380 VB2_BUF_STATE_ERROR);
381 if (common->next_frm != NULL)
382 vb2_buffer_done(&common->next_frm->vb,
383 VB2_BUF_STATE_ERROR);
386 while (!list_empty(&common->dma_queue)) {
387 common->next_frm = list_entry(common->dma_queue.next,
388 struct vpif_cap_buffer, list);
389 list_del(&common->next_frm->list);
390 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
392 spin_unlock_irqrestore(&common->irqlock, flags);
397 static struct vb2_ops video_qops = {
398 .queue_setup = vpif_buffer_queue_setup,
399 .wait_prepare = vpif_wait_prepare,
400 .wait_finish = vpif_wait_finish,
401 .buf_init = vpif_buffer_init,
402 .buf_prepare = vpif_buffer_prepare,
403 .start_streaming = vpif_start_streaming,
404 .stop_streaming = vpif_stop_streaming,
405 .buf_cleanup = vpif_buf_cleanup,
406 .buf_queue = vpif_buffer_queue,
410 * vpif_process_buffer_complete: process a completed buffer
411 * @common: ptr to common channel object
413 * This function time stamp the buffer and mark it as DONE. It also
414 * wake up any process waiting on the QUEUE and set the next buffer
417 static void vpif_process_buffer_complete(struct common_obj *common)
419 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
420 vb2_buffer_done(&common->cur_frm->vb,
422 /* Make curFrm pointing to nextFrm */
423 common->cur_frm = common->next_frm;
427 * vpif_schedule_next_buffer: set next buffer address for capture
428 * @common : ptr to common channel object
430 * This function will get next buffer from the dma queue and
431 * set the buffer address in the vpif register for capture.
432 * the buffer is marked active
434 static void vpif_schedule_next_buffer(struct common_obj *common)
436 unsigned long addr = 0;
438 spin_lock(&common->irqlock);
439 common->next_frm = list_entry(common->dma_queue.next,
440 struct vpif_cap_buffer, list);
441 /* Remove that buffer from the buffer queue */
442 list_del(&common->next_frm->list);
443 spin_unlock(&common->irqlock);
444 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
445 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
447 /* Set top and bottom field addresses in VPIF registers */
448 common->set_addr(addr + common->ytop_off,
449 addr + common->ybtm_off,
450 addr + common->ctop_off,
451 addr + common->cbtm_off);
455 * vpif_channel_isr : ISR handler for vpif capture
457 * @dev_id: dev_id ptr
459 * It changes status of the captured buffer, takes next buffer from the queue
460 * and sets its address in VPIF registers
462 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
464 struct vpif_device *dev = &vpif_obj;
465 struct common_obj *common;
466 struct channel_obj *ch;
467 enum v4l2_field field;
471 channel_id = *(int *)(dev_id);
472 if (!vpif_intr_status(channel_id))
475 ch = dev->dev[channel_id];
477 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
479 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
480 common = &ch->common[i];
481 /* skip If streaming is not started in this channel */
482 if (0 == common->started)
485 /* Check the field format */
486 if (1 == ch->vpifparams.std_info.frm_fmt) {
487 /* Progressive mode */
488 spin_lock(&common->irqlock);
489 if (list_empty(&common->dma_queue)) {
490 spin_unlock(&common->irqlock);
493 spin_unlock(&common->irqlock);
495 if (!channel_first_int[i][channel_id])
496 vpif_process_buffer_complete(common);
498 channel_first_int[i][channel_id] = 0;
500 vpif_schedule_next_buffer(common);
503 channel_first_int[i][channel_id] = 0;
506 * Interlaced mode. If it is first interrupt, ignore
509 if (channel_first_int[i][channel_id]) {
510 channel_first_int[i][channel_id] = 0;
515 /* Get field id from VPIF registers */
516 fid = vpif_channel_getfid(ch->channel_id);
517 if (fid != ch->field_id) {
519 * If field id does not match stored
520 * field id, make them in sync
527 /* device field id and local field id are in sync */
529 /* this is even field */
530 if (common->cur_frm == common->next_frm)
533 /* mark the current buffer as done */
534 vpif_process_buffer_complete(common);
535 } else if (1 == fid) {
537 spin_lock(&common->irqlock);
538 if (list_empty(&common->dma_queue) ||
539 (common->cur_frm != common->next_frm)) {
540 spin_unlock(&common->irqlock);
543 spin_unlock(&common->irqlock);
545 vpif_schedule_next_buffer(common);
553 * vpif_update_std_info() - update standard related info
554 * @ch: ptr to channel object
556 * For a given standard selected by application, update values
557 * in the device data structures
559 static int vpif_update_std_info(struct channel_obj *ch)
561 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
562 struct vpif_params *vpifparams = &ch->vpifparams;
563 const struct vpif_channel_config_params *config;
564 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
565 struct video_obj *vid_ch = &ch->video;
568 vpif_dbg(2, debug, "vpif_update_std_info\n");
570 for (index = 0; index < vpif_ch_params_count; index++) {
571 config = &vpif_ch_params[index];
572 if (config->hd_sd == 0) {
573 vpif_dbg(2, debug, "SD format\n");
574 if (config->stdid & vid_ch->stdid) {
575 memcpy(std_info, config, sizeof(*config));
579 vpif_dbg(2, debug, "HD format\n");
580 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
581 sizeof(vid_ch->dv_timings))) {
582 memcpy(std_info, config, sizeof(*config));
588 /* standard not found */
589 if (index == vpif_ch_params_count)
592 common->fmt.fmt.pix.width = std_info->width;
593 common->width = std_info->width;
594 common->fmt.fmt.pix.height = std_info->height;
595 common->height = std_info->height;
596 common->fmt.fmt.pix.bytesperline = std_info->width;
597 vpifparams->video_params.hpitch = std_info->width;
598 vpifparams->video_params.storage_mode = std_info->frm_fmt;
604 * vpif_calculate_offsets : This function calculates buffers offsets
605 * @ch : ptr to channel object
607 * This function calculates buffer offsets for Y and C in the top and
610 static void vpif_calculate_offsets(struct channel_obj *ch)
612 unsigned int hpitch, vpitch, sizeimage;
613 struct video_obj *vid_ch = &(ch->video);
614 struct vpif_params *vpifparams = &ch->vpifparams;
615 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
616 enum v4l2_field field = common->fmt.fmt.pix.field;
618 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
620 if (V4L2_FIELD_ANY == field) {
621 if (vpifparams->std_info.frm_fmt)
622 vid_ch->buf_field = V4L2_FIELD_NONE;
624 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
626 vid_ch->buf_field = common->fmt.fmt.pix.field;
628 sizeimage = common->fmt.fmt.pix.sizeimage;
630 hpitch = common->fmt.fmt.pix.bytesperline;
631 vpitch = sizeimage / (hpitch * 2);
633 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
634 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
635 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
636 common->ytop_off = 0;
637 common->ybtm_off = hpitch;
638 common->ctop_off = sizeimage / 2;
639 common->cbtm_off = sizeimage / 2 + hpitch;
640 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
641 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
642 common->ytop_off = 0;
643 common->ybtm_off = sizeimage / 4;
644 common->ctop_off = sizeimage / 2;
645 common->cbtm_off = common->ctop_off + sizeimage / 4;
646 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
647 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
648 common->ybtm_off = 0;
649 common->ytop_off = sizeimage / 4;
650 common->cbtm_off = sizeimage / 2;
651 common->ctop_off = common->cbtm_off + sizeimage / 4;
653 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
654 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
655 vpifparams->video_params.storage_mode = 1;
657 vpifparams->video_params.storage_mode = 0;
659 if (1 == vpifparams->std_info.frm_fmt)
660 vpifparams->video_params.hpitch =
661 common->fmt.fmt.pix.bytesperline;
663 if ((field == V4L2_FIELD_ANY)
664 || (field == V4L2_FIELD_INTERLACED))
665 vpifparams->video_params.hpitch =
666 common->fmt.fmt.pix.bytesperline * 2;
668 vpifparams->video_params.hpitch =
669 common->fmt.fmt.pix.bytesperline;
672 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
676 * vpif_config_format: configure default frame format in the device
677 * ch : ptr to channel object
679 static void vpif_config_format(struct channel_obj *ch)
681 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
683 vpif_dbg(2, debug, "vpif_config_format\n");
685 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
686 if (config_params.numbuffers[ch->channel_id] == 0)
687 common->memory = V4L2_MEMORY_USERPTR;
689 common->memory = V4L2_MEMORY_MMAP;
691 common->fmt.fmt.pix.sizeimage
692 = config_params.channel_bufsize[ch->channel_id];
694 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
695 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
697 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
698 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
702 * vpif_get_default_field() - Get default field type based on interface
703 * @vpif_params - ptr to vpif params
705 static inline enum v4l2_field vpif_get_default_field(
706 struct vpif_interface *iface)
708 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
709 V4L2_FIELD_INTERLACED;
713 * vpif_check_format() - check given pixel format for compatibility
715 * @pixfmt - Given pixel format
716 * @update - update the values as per hardware requirement
718 * Check the application pixel format for S_FMT and update the input
719 * values as per hardware limits for TRY_FMT. The default pixel and
720 * field format is selected based on interface type.
722 static int vpif_check_format(struct channel_obj *ch,
723 struct v4l2_pix_format *pixfmt,
726 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
727 struct vpif_params *vpif_params = &ch->vpifparams;
728 enum v4l2_field field = pixfmt->field;
729 u32 sizeimage, hpitch, vpitch;
732 vpif_dbg(2, debug, "vpif_check_format\n");
734 * first check for the pixel format. If if_type is Raw bayer,
735 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
736 * V4L2_PIX_FMT_YUV422P is supported
738 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
739 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
741 vpif_dbg(2, debug, "invalid pix format\n");
744 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
747 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
749 vpif_dbg(2, debug, "invalid pixel format\n");
752 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
756 if (!(VPIF_VALID_FIELD(field))) {
758 vpif_dbg(2, debug, "invalid field format\n");
762 * By default use FIELD_NONE for RAW Bayer capture
763 * and FIELD_INTERLACED for other interfaces
765 field = vpif_get_default_field(&vpif_params->iface);
766 } else if (field == V4L2_FIELD_ANY)
767 /* unsupported field. Use default */
768 field = vpif_get_default_field(&vpif_params->iface);
770 /* validate the hpitch */
771 hpitch = pixfmt->bytesperline;
772 if (hpitch < vpif_params->std_info.width) {
774 vpif_dbg(2, debug, "invalid hpitch\n");
777 hpitch = vpif_params->std_info.width;
780 sizeimage = pixfmt->sizeimage;
782 vpitch = sizeimage / (hpitch * 2);
784 /* validate the vpitch */
785 if (vpitch < vpif_params->std_info.height) {
787 vpif_dbg(2, debug, "Invalid vpitch\n");
790 vpitch = vpif_params->std_info.height;
793 /* Check for 8 byte alignment */
794 if (!ALIGN(hpitch, 8)) {
796 vpif_dbg(2, debug, "invalid pitch alignment\n");
799 /* adjust to next 8 byte boundary */
800 hpitch = (((hpitch + 7) / 8) * 8);
802 /* if update is set, modify the bytesperline and sizeimage */
804 pixfmt->bytesperline = hpitch;
805 pixfmt->sizeimage = hpitch * vpitch * 2;
808 * Image width and height is always based on current standard width and
811 pixfmt->width = common->fmt.fmt.pix.width;
812 pixfmt->height = common->fmt.fmt.pix.height;
819 * vpif_config_addr() - function to configure buffer address in vpif
821 * @muxmode - channel mux mode
823 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
825 struct common_obj *common;
827 vpif_dbg(2, debug, "vpif_config_addr\n");
829 common = &(ch->common[VPIF_VIDEO_INDEX]);
831 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
832 common->set_addr = ch1_set_videobuf_addr;
833 else if (2 == muxmode)
834 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
836 common->set_addr = ch0_set_videobuf_addr;
840 * vpif_mmap : It is used to map kernel space buffers into user spaces
841 * @filep: file pointer
842 * @vma: ptr to vm_area_struct
844 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
846 /* Get the channel object and file handle object */
847 struct vpif_fh *fh = filep->private_data;
848 struct channel_obj *ch = fh->channel;
849 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
852 vpif_dbg(2, debug, "vpif_mmap\n");
854 if (mutex_lock_interruptible(&common->lock))
856 ret = vb2_mmap(&common->buffer_queue, vma);
857 mutex_unlock(&common->lock);
862 * vpif_poll: It is used for select/poll system call
863 * @filep: file pointer
864 * @wait: poll table to wait
866 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
868 struct vpif_fh *fh = filep->private_data;
869 struct channel_obj *channel = fh->channel;
870 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
871 unsigned int res = 0;
873 vpif_dbg(2, debug, "vpif_poll\n");
875 if (common->started) {
876 mutex_lock(&common->lock);
877 res = vb2_poll(&common->buffer_queue, filep, wait);
878 mutex_unlock(&common->lock);
884 * vpif_open : vpif open handler
887 * It creates object of file handle structure and stores it in private_data
888 * member of filepointer
890 static int vpif_open(struct file *filep)
892 struct video_device *vdev = video_devdata(filep);
893 struct common_obj *common;
894 struct video_obj *vid_ch;
895 struct channel_obj *ch;
898 vpif_dbg(2, debug, "vpif_open\n");
900 ch = video_get_drvdata(vdev);
903 common = &ch->common[VPIF_VIDEO_INDEX];
905 /* Allocate memory for the file handle object */
906 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
908 vpif_err("unable to allocate memory for file handle object\n");
912 if (mutex_lock_interruptible(&common->lock)) {
916 /* store pointer to fh in private_data member of filep */
917 filep->private_data = fh;
920 /* If decoder is not initialized. initialize it */
921 if (!ch->initialized) {
924 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
926 /* Increment channel usrs counter */
928 /* Set io_allowed member to false */
929 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
930 /* Initialize priority of this instance to default priority */
931 fh->prio = V4L2_PRIORITY_UNSET;
932 v4l2_prio_open(&ch->prio, &fh->prio);
933 mutex_unlock(&common->lock);
938 * vpif_release : function to clean up file close
939 * @filep: file pointer
941 * This function deletes buffer queue, frees the buffers and the vpif file
944 static int vpif_release(struct file *filep)
946 struct vpif_fh *fh = filep->private_data;
947 struct channel_obj *ch = fh->channel;
948 struct common_obj *common;
950 vpif_dbg(2, debug, "vpif_release\n");
952 common = &ch->common[VPIF_VIDEO_INDEX];
954 mutex_lock(&common->lock);
955 /* if this instance is doing IO */
956 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
957 /* Reset io_usrs member of channel object */
959 /* Free buffers allocated */
960 vb2_queue_release(&common->buffer_queue);
961 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
964 /* Decrement channel usrs counter */
967 /* Close the priority */
968 v4l2_prio_close(&ch->prio, fh->prio);
973 mutex_unlock(&common->lock);
974 filep->private_data = NULL;
980 * vpif_reqbufs() - request buffer handler
983 * @reqbuf: request buffer structure ptr
985 static int vpif_reqbufs(struct file *file, void *priv,
986 struct v4l2_requestbuffers *reqbuf)
988 struct vpif_fh *fh = priv;
989 struct channel_obj *ch = fh->channel;
990 struct common_obj *common;
995 vpif_dbg(2, debug, "vpif_reqbufs\n");
998 * This file handle has not initialized the channel,
999 * It is not allowed to do settings
1001 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
1002 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1003 if (!fh->initialized) {
1004 vpif_dbg(1, debug, "Channel Busy\n");
1009 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
1012 index = VPIF_VIDEO_INDEX;
1014 common = &ch->common[index];
1016 if (0 != common->io_usrs)
1019 /* Initialize videobuf2 queue as per the buffer type */
1020 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1021 if (IS_ERR(common->alloc_ctx)) {
1022 vpif_err("Failed to get the context\n");
1023 return PTR_ERR(common->alloc_ctx);
1025 q = &common->buffer_queue;
1026 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1027 q->io_modes = VB2_MMAP | VB2_USERPTR;
1029 q->ops = &video_qops;
1030 q->mem_ops = &vb2_dma_contig_memops;
1031 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1032 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1033 q->min_buffers_needed = 1;
1035 ret = vb2_queue_init(q);
1037 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1038 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1041 /* Set io allowed member of file handle to TRUE */
1042 fh->io_allowed[index] = 1;
1043 /* Increment io usrs member of channel object to 1 */
1044 common->io_usrs = 1;
1045 /* Store type of memory requested in channel object */
1046 common->memory = reqbuf->memory;
1047 INIT_LIST_HEAD(&common->dma_queue);
1049 /* Allocate buffers */
1050 return vb2_reqbufs(&common->buffer_queue, reqbuf);
1054 * vpif_querybuf() - query buffer handler
1056 * @priv: file handle
1057 * @buf: v4l2 buffer structure ptr
1059 static int vpif_querybuf(struct file *file, void *priv,
1060 struct v4l2_buffer *buf)
1062 struct vpif_fh *fh = priv;
1063 struct channel_obj *ch = fh->channel;
1064 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1066 vpif_dbg(2, debug, "vpif_querybuf\n");
1068 if (common->fmt.type != buf->type)
1071 if (common->memory != V4L2_MEMORY_MMAP) {
1072 vpif_dbg(1, debug, "Invalid memory\n");
1076 return vb2_querybuf(&common->buffer_queue, buf);
1080 * vpif_qbuf() - query buffer handler
1082 * @priv: file handle
1083 * @buf: v4l2 buffer structure ptr
1085 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1088 struct vpif_fh *fh = priv;
1089 struct channel_obj *ch = fh->channel;
1090 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1091 struct v4l2_buffer tbuf = *buf;
1093 vpif_dbg(2, debug, "vpif_qbuf\n");
1095 if (common->fmt.type != tbuf.type) {
1096 vpif_err("invalid buffer type\n");
1100 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1101 vpif_err("fh io not allowed\n");
1105 return vb2_qbuf(&common->buffer_queue, buf);
1109 * vpif_dqbuf() - query buffer handler
1111 * @priv: file handle
1112 * @buf: v4l2 buffer structure ptr
1114 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1116 struct vpif_fh *fh = priv;
1117 struct channel_obj *ch = fh->channel;
1118 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1120 vpif_dbg(2, debug, "vpif_dqbuf\n");
1122 return vb2_dqbuf(&common->buffer_queue, buf,
1123 (file->f_flags & O_NONBLOCK));
1127 * vpif_streamon() - streamon handler
1129 * @priv: file handle
1130 * @buftype: v4l2 buffer type
1132 static int vpif_streamon(struct file *file, void *priv,
1133 enum v4l2_buf_type buftype)
1136 struct vpif_fh *fh = priv;
1137 struct channel_obj *ch = fh->channel;
1138 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1139 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1140 struct vpif_params *vpif;
1143 vpif_dbg(2, debug, "vpif_streamon\n");
1145 vpif = &ch->vpifparams;
1147 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1148 vpif_dbg(1, debug, "buffer type not supported\n");
1152 /* If file handle is not allowed IO, return error */
1153 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1154 vpif_dbg(1, debug, "io not allowed\n");
1158 /* If Streaming is already started, return error */
1159 if (common->started) {
1160 vpif_dbg(1, debug, "channel->started\n");
1164 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1165 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1166 vpif->std_info.ycmux_mode == 0) ||
1167 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1168 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1169 vpif_dbg(1, debug, "other channel is being used\n");
1173 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1177 /* Enable streamon on the sub device */
1178 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
1180 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1181 vpif_dbg(1, debug, "stream on failed in subdev\n");
1185 /* Call vb2_streamon to start streaming in videobuf2 */
1186 ret = vb2_streamon(&common->buffer_queue, buftype);
1188 vpif_dbg(1, debug, "vb2_streamon\n");
1196 * vpif_streamoff() - streamoff handler
1198 * @priv: file handle
1199 * @buftype: v4l2 buffer type
1201 static int vpif_streamoff(struct file *file, void *priv,
1202 enum v4l2_buf_type buftype)
1205 struct vpif_fh *fh = priv;
1206 struct channel_obj *ch = fh->channel;
1207 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1210 vpif_dbg(2, debug, "vpif_streamoff\n");
1212 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1213 vpif_dbg(1, debug, "buffer type not supported\n");
1217 /* If io is allowed for this file handle, return error */
1218 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1219 vpif_dbg(1, debug, "io not allowed\n");
1223 /* If streaming is not started, return error */
1224 if (!common->started) {
1225 vpif_dbg(1, debug, "channel->started\n");
1229 /* disable channel */
1230 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1232 channel0_intr_enable(0);
1235 channel1_intr_enable(0);
1238 common->started = 0;
1240 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
1242 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1243 vpif_dbg(1, debug, "stream off failed in subdev\n");
1245 return vb2_streamoff(&common->buffer_queue, buftype);
1249 * vpif_input_to_subdev() - Maps input to sub device
1250 * @vpif_cfg - global config ptr
1251 * @chan_cfg - channel config ptr
1252 * @input_index - Given input index from application
1254 * lookup the sub device information for a given input index.
1255 * we report all the inputs to application. inputs table also
1256 * has sub device name for the each input
1258 static int vpif_input_to_subdev(
1259 struct vpif_capture_config *vpif_cfg,
1260 struct vpif_capture_chan_config *chan_cfg,
1263 struct vpif_subdev_info *subdev_info;
1264 const char *subdev_name;
1267 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
1269 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1270 if (subdev_name == NULL)
1273 /* loop through the sub device list to get the sub device info */
1274 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1275 subdev_info = &vpif_cfg->subdev_info[i];
1276 if (!strcmp(subdev_info->name, subdev_name))
1283 * vpif_set_input() - Select an input
1284 * @vpif_cfg - global config ptr
1286 * @_index - Given input index from application
1288 * Select the given input.
1290 static int vpif_set_input(
1291 struct vpif_capture_config *vpif_cfg,
1292 struct channel_obj *ch,
1295 struct vpif_capture_chan_config *chan_cfg =
1296 &vpif_cfg->chan_config[ch->channel_id];
1297 struct vpif_subdev_info *subdev_info = NULL;
1298 struct v4l2_subdev *sd = NULL;
1299 u32 input = 0, output = 0;
1303 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1304 if (sd_index >= 0) {
1305 sd = vpif_obj.sd[sd_index];
1306 subdev_info = &vpif_cfg->subdev_info[sd_index];
1309 /* first setup input path from sub device to vpif */
1310 if (sd && vpif_cfg->setup_input_path) {
1311 ret = vpif_cfg->setup_input_path(ch->channel_id,
1314 vpif_dbg(1, debug, "couldn't setup input path for the" \
1315 " sub device %s, for input index %d\n",
1316 subdev_info->name, index);
1322 input = chan_cfg->inputs[index].input_route;
1323 output = chan_cfg->inputs[index].output_route;
1324 ret = v4l2_subdev_call(sd, video, s_routing,
1326 if (ret < 0 && ret != -ENOIOCTLCMD) {
1327 vpif_dbg(1, debug, "Failed to set input\n");
1331 ch->input_idx = index;
1333 /* copy interface parameters to vpif */
1334 ch->vpifparams.iface = chan_cfg->vpif_if;
1336 /* update tvnorms from the sub device input info */
1337 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1342 * vpif_querystd() - querystd handler
1344 * @priv: file handle
1345 * @std_id: ptr to std id
1347 * This function is called to detect standard at the selected input
1349 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1351 struct vpif_fh *fh = priv;
1352 struct channel_obj *ch = fh->channel;
1355 vpif_dbg(2, debug, "vpif_querystd\n");
1357 /* Call querystd function of decoder device */
1358 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
1360 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1363 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1371 * vpif_g_std() - get STD handler
1373 * @priv: file handle
1374 * @std_id: ptr to std id
1376 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1378 struct vpif_fh *fh = priv;
1379 struct channel_obj *ch = fh->channel;
1381 vpif_dbg(2, debug, "vpif_g_std\n");
1383 *std = ch->video.stdid;
1388 * vpif_s_std() - set STD handler
1390 * @priv: file handle
1391 * @std_id: ptr to std id
1393 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1395 struct vpif_fh *fh = priv;
1396 struct channel_obj *ch = fh->channel;
1397 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1400 vpif_dbg(2, debug, "vpif_s_std\n");
1402 if (common->started) {
1403 vpif_err("streaming in progress\n");
1407 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1408 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1409 if (!fh->initialized) {
1410 vpif_dbg(1, debug, "Channel Busy\n");
1415 ret = v4l2_prio_check(&ch->prio, fh->prio);
1419 fh->initialized = 1;
1421 /* Call encoder subdevice function to set the standard */
1422 ch->video.stdid = std_id;
1423 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1425 /* Get the information about the standard */
1426 if (vpif_update_std_info(ch)) {
1427 vpif_err("Error getting the standard info\n");
1431 /* Configure the default format information */
1432 vpif_config_format(ch);
1434 /* set standard in the sub device */
1435 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
1436 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1437 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1444 * vpif_enum_input() - ENUMINPUT handler
1446 * @priv: file handle
1447 * @input: ptr to input structure
1449 static int vpif_enum_input(struct file *file, void *priv,
1450 struct v4l2_input *input)
1453 struct vpif_capture_config *config = vpif_dev->platform_data;
1454 struct vpif_capture_chan_config *chan_cfg;
1455 struct vpif_fh *fh = priv;
1456 struct channel_obj *ch = fh->channel;
1458 chan_cfg = &config->chan_config[ch->channel_id];
1460 if (input->index >= chan_cfg->input_count) {
1461 vpif_dbg(1, debug, "Invalid input index\n");
1465 memcpy(input, &chan_cfg->inputs[input->index].input,
1471 * vpif_g_input() - Get INPUT handler
1473 * @priv: file handle
1474 * @index: ptr to input index
1476 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1478 struct vpif_fh *fh = priv;
1479 struct channel_obj *ch = fh->channel;
1481 *index = ch->input_idx;
1486 * vpif_s_input() - Set INPUT handler
1488 * @priv: file handle
1489 * @index: input index
1491 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1493 struct vpif_capture_config *config = vpif_dev->platform_data;
1494 struct vpif_capture_chan_config *chan_cfg;
1495 struct vpif_fh *fh = priv;
1496 struct channel_obj *ch = fh->channel;
1497 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1500 chan_cfg = &config->chan_config[ch->channel_id];
1502 if (index >= chan_cfg->input_count)
1505 if (common->started) {
1506 vpif_err("Streaming in progress\n");
1510 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1511 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1512 if (!fh->initialized) {
1513 vpif_dbg(1, debug, "Channel Busy\n");
1518 ret = v4l2_prio_check(&ch->prio, fh->prio);
1522 fh->initialized = 1;
1523 return vpif_set_input(config, ch, index);
1527 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1529 * @priv: file handle
1530 * @index: input index
1532 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1533 struct v4l2_fmtdesc *fmt)
1535 struct vpif_fh *fh = priv;
1536 struct channel_obj *ch = fh->channel;
1538 if (fmt->index != 0) {
1539 vpif_dbg(1, debug, "Invalid format index\n");
1543 /* Fill in the information about format */
1544 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1545 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1546 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1547 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1549 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1550 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1551 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1557 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1559 * @priv: file handle
1560 * @fmt: ptr to v4l2 format structure
1562 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1563 struct v4l2_format *fmt)
1565 struct vpif_fh *fh = priv;
1566 struct channel_obj *ch = fh->channel;
1567 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1569 return vpif_check_format(ch, pixfmt, 1);
1574 * vpif_g_fmt_vid_cap() - Set INPUT handler
1576 * @priv: file handle
1577 * @fmt: ptr to v4l2 format structure
1579 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1580 struct v4l2_format *fmt)
1582 struct vpif_fh *fh = priv;
1583 struct channel_obj *ch = fh->channel;
1584 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1586 /* Check the validity of the buffer type */
1587 if (common->fmt.type != fmt->type)
1590 /* Fill in the information about format */
1596 * vpif_s_fmt_vid_cap() - Set FMT handler
1598 * @priv: file handle
1599 * @fmt: ptr to v4l2 format structure
1601 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1602 struct v4l2_format *fmt)
1604 struct vpif_fh *fh = priv;
1605 struct channel_obj *ch = fh->channel;
1606 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1607 struct v4l2_pix_format *pixfmt;
1610 vpif_dbg(2, debug, "%s\n", __func__);
1612 /* If streaming is started, return error */
1613 if (common->started) {
1614 vpif_dbg(1, debug, "Streaming is started\n");
1618 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1619 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1620 if (!fh->initialized) {
1621 vpif_dbg(1, debug, "Channel Busy\n");
1626 ret = v4l2_prio_check(&ch->prio, fh->prio);
1630 fh->initialized = 1;
1632 pixfmt = &fmt->fmt.pix;
1633 /* Check for valid field format */
1634 ret = vpif_check_format(ch, pixfmt, 0);
1638 /* store the format in the channel object */
1644 * vpif_querycap() - QUERYCAP handler
1646 * @priv: file handle
1647 * @cap: ptr to v4l2_capability structure
1649 static int vpif_querycap(struct file *file, void *priv,
1650 struct v4l2_capability *cap)
1652 struct vpif_capture_config *config = vpif_dev->platform_data;
1654 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1655 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1656 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1657 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1658 dev_name(vpif_dev));
1659 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1665 * vpif_g_priority() - get priority handler
1667 * @priv: file handle
1668 * @prio: ptr to v4l2_priority structure
1670 static int vpif_g_priority(struct file *file, void *priv,
1671 enum v4l2_priority *prio)
1673 struct vpif_fh *fh = priv;
1674 struct channel_obj *ch = fh->channel;
1676 *prio = v4l2_prio_max(&ch->prio);
1682 * vpif_s_priority() - set priority handler
1684 * @priv: file handle
1685 * @prio: ptr to v4l2_priority structure
1687 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1689 struct vpif_fh *fh = priv;
1690 struct channel_obj *ch = fh->channel;
1692 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1696 * vpif_cropcap() - cropcap handler
1698 * @priv: file handle
1699 * @crop: ptr to v4l2_cropcap structure
1701 static int vpif_cropcap(struct file *file, void *priv,
1702 struct v4l2_cropcap *crop)
1704 struct vpif_fh *fh = priv;
1705 struct channel_obj *ch = fh->channel;
1706 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1708 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1711 crop->bounds.left = 0;
1712 crop->bounds.top = 0;
1713 crop->bounds.height = common->height;
1714 crop->bounds.width = common->width;
1715 crop->defrect = crop->bounds;
1720 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1722 * @priv: file handle
1723 * @timings: input timings
1726 vpif_enum_dv_timings(struct file *file, void *priv,
1727 struct v4l2_enum_dv_timings *timings)
1729 struct vpif_fh *fh = priv;
1730 struct channel_obj *ch = fh->channel;
1733 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1734 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1740 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1742 * @priv: file handle
1743 * @timings: input timings
1746 vpif_query_dv_timings(struct file *file, void *priv,
1747 struct v4l2_dv_timings *timings)
1749 struct vpif_fh *fh = priv;
1750 struct channel_obj *ch = fh->channel;
1753 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1754 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1760 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1762 * @priv: file handle
1763 * @timings: digital video timings
1765 static int vpif_s_dv_timings(struct file *file, void *priv,
1766 struct v4l2_dv_timings *timings)
1768 struct vpif_fh *fh = priv;
1769 struct channel_obj *ch = fh->channel;
1770 struct vpif_params *vpifparams = &ch->vpifparams;
1771 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1772 struct video_obj *vid_ch = &ch->video;
1773 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1776 if (timings->type != V4L2_DV_BT_656_1120) {
1777 vpif_dbg(2, debug, "Timing type not defined\n");
1781 /* Configure subdevice timings, if any */
1782 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1783 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1786 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1790 if (!(timings->bt.width && timings->bt.height &&
1791 (timings->bt.hbackporch ||
1792 timings->bt.hfrontporch ||
1793 timings->bt.hsync) &&
1794 timings->bt.vfrontporch &&
1795 (timings->bt.vbackporch ||
1796 timings->bt.vsync))) {
1797 vpif_dbg(2, debug, "Timings for width, height, "
1798 "horizontal back porch, horizontal sync, "
1799 "horizontal front porch, vertical back porch, "
1800 "vertical sync and vertical back porch "
1801 "must be defined\n");
1805 vid_ch->dv_timings = *timings;
1807 /* Configure video port timings */
1809 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1810 std_info->sav2eav = bt->width;
1813 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1815 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1816 if (bt->interlaced) {
1817 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1818 std_info->l5 = std_info->vsize/2 -
1819 (bt->vfrontporch - 1);
1820 std_info->l7 = std_info->vsize/2 + 1;
1821 std_info->l9 = std_info->l7 + bt->il_vsync +
1822 bt->il_vbackporch + 1;
1823 std_info->l11 = std_info->vsize -
1824 (bt->il_vfrontporch - 1);
1826 vpif_dbg(2, debug, "Required timing values for "
1827 "interlaced BT format missing\n");
1831 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1833 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1834 std_info->width = bt->width;
1835 std_info->height = bt->height;
1836 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1837 std_info->ycmux_mode = 0;
1838 std_info->capture_format = 0;
1839 std_info->vbi_supported = 0;
1840 std_info->hd_sd = 1;
1841 std_info->stdid = 0;
1848 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1850 * @priv: file handle
1851 * @timings: digital video timings
1853 static int vpif_g_dv_timings(struct file *file, void *priv,
1854 struct v4l2_dv_timings *timings)
1856 struct vpif_fh *fh = priv;
1857 struct channel_obj *ch = fh->channel;
1858 struct video_obj *vid_ch = &ch->video;
1860 *timings = vid_ch->dv_timings;
1866 * vpif_log_status() - Status information
1868 * @priv: file handle
1872 static int vpif_log_status(struct file *filep, void *priv)
1874 /* status for sub devices */
1875 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1880 /* vpif capture ioctl operations */
1881 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1882 .vidioc_querycap = vpif_querycap,
1883 .vidioc_g_priority = vpif_g_priority,
1884 .vidioc_s_priority = vpif_s_priority,
1885 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1886 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1887 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1888 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1889 .vidioc_enum_input = vpif_enum_input,
1890 .vidioc_s_input = vpif_s_input,
1891 .vidioc_g_input = vpif_g_input,
1892 .vidioc_reqbufs = vpif_reqbufs,
1893 .vidioc_querybuf = vpif_querybuf,
1894 .vidioc_querystd = vpif_querystd,
1895 .vidioc_s_std = vpif_s_std,
1896 .vidioc_g_std = vpif_g_std,
1897 .vidioc_qbuf = vpif_qbuf,
1898 .vidioc_dqbuf = vpif_dqbuf,
1899 .vidioc_streamon = vpif_streamon,
1900 .vidioc_streamoff = vpif_streamoff,
1901 .vidioc_cropcap = vpif_cropcap,
1902 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1903 .vidioc_query_dv_timings = vpif_query_dv_timings,
1904 .vidioc_s_dv_timings = vpif_s_dv_timings,
1905 .vidioc_g_dv_timings = vpif_g_dv_timings,
1906 .vidioc_log_status = vpif_log_status,
1909 /* vpif file operations */
1910 static struct v4l2_file_operations vpif_fops = {
1911 .owner = THIS_MODULE,
1913 .release = vpif_release,
1914 .unlocked_ioctl = video_ioctl2,
1919 /* vpif video template */
1920 static struct video_device vpif_video_template = {
1924 .ioctl_ops = &vpif_ioctl_ops,
1928 * initialize_vpif() - Initialize vpif data structures
1930 * Allocate memory for data structures and initialize them
1932 static int initialize_vpif(void)
1935 int free_channel_objects_index;
1937 /* Default number of buffers should be 3 */
1938 if ((ch0_numbuffers > 0) &&
1939 (ch0_numbuffers < config_params.min_numbuffers))
1940 ch0_numbuffers = config_params.min_numbuffers;
1941 if ((ch1_numbuffers > 0) &&
1942 (ch1_numbuffers < config_params.min_numbuffers))
1943 ch1_numbuffers = config_params.min_numbuffers;
1945 /* Set buffer size to min buffers size if it is invalid */
1946 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1948 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1949 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1951 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1953 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1954 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1955 if (ch0_numbuffers) {
1956 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1959 if (ch1_numbuffers) {
1960 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1964 /* Allocate memory for six channel objects */
1965 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1967 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1968 /* If memory allocation fails, return error */
1969 if (!vpif_obj.dev[i]) {
1970 free_channel_objects_index = i;
1972 goto vpif_init_free_channel_objects;
1977 vpif_init_free_channel_objects:
1978 for (j = 0; j < free_channel_objects_index; j++)
1979 kfree(vpif_obj.dev[j]);
1983 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1984 struct v4l2_subdev *subdev,
1985 struct v4l2_async_subdev *asd)
1989 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1990 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1992 vpif_obj.sd[i] = subdev;
1999 static int vpif_probe_complete(void)
2001 struct common_obj *common;
2002 struct channel_obj *ch;
2005 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2006 ch = vpif_obj.dev[j];
2008 common = &(ch->common[VPIF_VIDEO_INDEX]);
2009 spin_lock_init(&common->irqlock);
2010 mutex_init(&common->lock);
2011 ch->video_dev->lock = &common->lock;
2012 /* Initialize prio member of channel object */
2013 v4l2_prio_init(&ch->prio);
2014 video_set_drvdata(ch->video_dev, ch);
2016 /* select input 0 */
2017 err = vpif_set_input(vpif_obj.config, ch, 0);
2021 err = video_register_device(ch->video_dev,
2022 VFL_TYPE_GRABBER, (j ? 1 : 0));
2027 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2031 for (k = 0; k < j; k++) {
2032 /* Get the pointer to the channel object */
2033 ch = vpif_obj.dev[k];
2034 /* Unregister video device */
2035 video_unregister_device(ch->video_dev);
2038 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2039 ch = vpif_obj.dev[i];
2040 /* Note: does nothing if ch->video_dev == NULL */
2041 video_device_release(ch->video_dev);
2043 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2048 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2050 return vpif_probe_complete();
2054 * vpif_probe : This function probes the vpif capture driver
2055 * @pdev: platform device pointer
2057 * This creates device entries by register itself to the V4L2 driver and
2058 * initializes fields of each channel objects
2060 static __init int vpif_probe(struct platform_device *pdev)
2062 struct vpif_subdev_info *subdevdata;
2065 struct i2c_adapter *i2c_adap;
2066 struct channel_obj *ch;
2067 struct video_device *vfd;
2068 struct resource *res;
2072 vpif_dev = &pdev->dev;
2074 err = initialize_vpif();
2076 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2080 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2082 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2086 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
2087 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2088 IRQF_SHARED, "VPIF_Capture",
2089 (void *)(&vpif_obj.dev[res_idx]->
2093 goto vpif_unregister;
2098 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2099 /* Get the pointer to the channel object */
2100 ch = vpif_obj.dev[i];
2101 /* Allocate memory for video device */
2102 vfd = video_device_alloc();
2104 for (j = 0; j < i; j++) {
2105 ch = vpif_obj.dev[j];
2106 video_device_release(ch->video_dev);
2109 goto vpif_unregister;
2112 /* Initialize field of video device */
2113 *vfd = vpif_video_template;
2114 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2115 vfd->release = video_device_release;
2116 snprintf(vfd->name, sizeof(vfd->name),
2117 "VPIF_Capture_DRIVER_V%s",
2118 VPIF_CAPTURE_VERSION);
2119 /* Set video_dev to the video device */
2120 ch->video_dev = vfd;
2123 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2125 size = resource_size(res);
2126 /* The resources are divided into two equal memory and when we
2127 * have HD output we can add them together
2129 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2130 ch = vpif_obj.dev[j];
2132 /* only enabled if second resource exists */
2133 config_params.video_limit[ch->channel_id] = 0;
2135 config_params.video_limit[ch->channel_id] =
2140 vpif_obj.config = pdev->dev.platform_data;
2142 subdev_count = vpif_obj.config->subdev_count;
2143 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2145 if (vpif_obj.sd == NULL) {
2146 vpif_err("unable to allocate memory for subdevice pointers\n");
2151 if (!vpif_obj.config->asd_sizes) {
2152 i2c_adap = i2c_get_adapter(1);
2153 for (i = 0; i < subdev_count; i++) {
2154 subdevdata = &vpif_obj.config->subdev_info[i];
2156 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2162 if (!vpif_obj.sd[i]) {
2163 vpif_err("Error registering v4l2 subdevice\n");
2165 goto probe_subdev_out;
2167 v4l2_info(&vpif_obj.v4l2_dev,
2168 "registered sub device %s\n",
2171 vpif_probe_complete();
2173 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
2174 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2175 vpif_obj.notifier.bound = vpif_async_bound;
2176 vpif_obj.notifier.complete = vpif_async_complete;
2177 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2178 &vpif_obj.notifier);
2180 vpif_err("Error registering async notifier\n");
2182 goto probe_subdev_out;
2189 /* free sub devices memory */
2193 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2194 ch = vpif_obj.dev[i];
2195 /* Note: does nothing if ch->video_dev == NULL */
2196 video_device_release(ch->video_dev);
2199 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2205 * vpif_remove() - driver remove handler
2206 * @device: ptr to platform device structure
2208 * The vidoe device is unregistered
2210 static int vpif_remove(struct platform_device *device)
2212 struct channel_obj *ch;
2215 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2218 /* un-register device */
2219 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2220 /* Get the pointer to the channel object */
2221 ch = vpif_obj.dev[i];
2222 /* Unregister video device */
2223 video_unregister_device(ch->video_dev);
2224 kfree(vpif_obj.dev[i]);
2231 * vpif_suspend: vpif device suspend
2233 static int vpif_suspend(struct device *dev)
2236 struct common_obj *common;
2237 struct channel_obj *ch;
2240 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2241 /* Get the pointer to the channel object */
2242 ch = vpif_obj.dev[i];
2243 common = &ch->common[VPIF_VIDEO_INDEX];
2244 mutex_lock(&common->lock);
2245 if (ch->usrs && common->io_usrs) {
2246 /* Disable channel */
2247 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2249 channel0_intr_enable(0);
2251 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2252 common->started == 2) {
2254 channel1_intr_enable(0);
2257 mutex_unlock(&common->lock);
2264 * vpif_resume: vpif device suspend
2266 static int vpif_resume(struct device *dev)
2268 struct common_obj *common;
2269 struct channel_obj *ch;
2272 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2273 /* Get the pointer to the channel object */
2274 ch = vpif_obj.dev[i];
2275 common = &ch->common[VPIF_VIDEO_INDEX];
2276 mutex_lock(&common->lock);
2277 if (ch->usrs && common->io_usrs) {
2278 /* Disable channel */
2279 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2281 channel0_intr_enable(1);
2283 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2284 common->started == 2) {
2286 channel1_intr_enable(1);
2289 mutex_unlock(&common->lock);
2295 static const struct dev_pm_ops vpif_dev_pm_ops = {
2296 .suspend = vpif_suspend,
2297 .resume = vpif_resume,
2300 #define vpif_pm_ops (&vpif_dev_pm_ops)
2302 #define vpif_pm_ops NULL
2305 static __refdata struct platform_driver vpif_driver = {
2307 .name = "vpif_capture",
2308 .owner = THIS_MODULE,
2311 .probe = vpif_probe,
2312 .remove = vpif_remove,
2315 module_platform_driver(vpif_driver);