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
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/workqueue.h>
29 #include <linux/string.h>
30 #include <linux/videodev2.h>
31 #include <linux/wait.h>
32 #include <linux/time.h>
33 #include <linux/i2c.h>
34 #include <linux/platform_device.h>
36 #include <linux/version.h>
37 #include <linux/slab.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
41 #include "vpif_capture.h"
44 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45 MODULE_LICENSE("GPL");
47 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
48 #define vpif_dbg(level, debug, fmt, arg...) \
49 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
52 static u32 ch0_numbuffers
= 3;
53 static u32 ch1_numbuffers
= 3;
54 static u32 ch0_bufsize
= 1920 * 1080 * 2;
55 static u32 ch1_bufsize
= 720 * 576 * 2;
57 module_param(debug
, int, 0644);
58 module_param(ch0_numbuffers
, uint
, S_IRUGO
);
59 module_param(ch1_numbuffers
, uint
, S_IRUGO
);
60 module_param(ch0_bufsize
, uint
, S_IRUGO
);
61 module_param(ch1_bufsize
, uint
, S_IRUGO
);
63 MODULE_PARM_DESC(debug
, "Debug level 0-1");
64 MODULE_PARM_DESC(ch2_numbuffers
, "Channel0 buffer count (default:3)");
65 MODULE_PARM_DESC(ch3_numbuffers
, "Channel1 buffer count (default:3)");
66 MODULE_PARM_DESC(ch2_bufsize
, "Channel0 buffer size (default:1920 x 1080 x 2)");
67 MODULE_PARM_DESC(ch3_bufsize
, "Channel1 buffer size (default:720 x 576 x 2)");
69 static struct vpif_config_params config_params
= {
73 .min_bufsize
[0] = 720 * 480 * 2,
74 .min_bufsize
[1] = 720 * 480 * 2,
75 .channel_bufsize
[0] = 1920 * 1080 * 2,
76 .channel_bufsize
[1] = 720 * 576 * 2,
79 /* global variables */
80 static struct vpif_device vpif_obj
= { {NULL
} };
81 static struct device
*vpif_dev
;
84 * ch_params: video standard configuration parameters for vpif
86 static const struct vpif_channel_config_params ch_params
[] = {
88 "NTSC_M", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
89 286, 525, 525, 0, 1, 0, V4L2_STD_525_60
,
92 "PAL_BDGHIK", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
93 336, 624, 625, 0, 1, 0, V4L2_STD_625_50
,
98 * vpif_uservirt_to_phys : translate user/virtual address to phy address
99 * @virtp: user/virtual address
101 * This inline function is used to convert user space virtual address to
104 static inline u32
vpif_uservirt_to_phys(u32 virtp
)
106 unsigned long physp
= 0;
107 struct mm_struct
*mm
= current
->mm
;
108 struct vm_area_struct
*vma
;
110 vma
= find_vma(mm
, virtp
);
112 /* For kernel direct-mapped memory, take the easy way */
113 if (virtp
>= PAGE_OFFSET
)
114 physp
= virt_to_phys((void *)virtp
);
115 else if (vma
&& (vma
->vm_flags
& VM_IO
) && (vma
->vm_pgoff
))
117 * this will catch, kernel-allocated, mmaped-to-usermode
120 physp
= (vma
->vm_pgoff
<< PAGE_SHIFT
) + (virtp
- vma
->vm_start
);
122 /* otherwise, use get_user_pages() for general userland pages */
123 int res
, nr_pages
= 1;
126 down_read(¤t
->mm
->mmap_sem
);
128 res
= get_user_pages(current
, current
->mm
,
129 virtp
, nr_pages
, 1, 0, &pages
, NULL
);
130 up_read(¤t
->mm
->mmap_sem
);
133 physp
= __pa(page_address(&pages
[0]) +
134 (virtp
& ~PAGE_MASK
));
136 vpif_err("get_user_pages failed\n");
144 * buffer_prepare : callback function for buffer prepare
145 * @q : buffer queue ptr
146 * @vb: ptr to video buffer
149 * This is the callback function for buffer prepare when videobuf_qbuf()
150 * function is called. The buffer is prepared and user space virtual address
151 * or user address is converted into physical address
153 static int vpif_buffer_prepare(struct videobuf_queue
*q
,
154 struct videobuf_buffer
*vb
,
155 enum v4l2_field field
)
157 /* Get the file handle object and channel object */
158 struct vpif_fh
*fh
= q
->priv_data
;
159 struct channel_obj
*ch
= fh
->channel
;
160 struct common_obj
*common
;
164 vpif_dbg(2, debug
, "vpif_buffer_prepare\n");
166 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
168 /* If buffer is not initialized, initialize it */
169 if (VIDEOBUF_NEEDS_INIT
== vb
->state
) {
170 vb
->width
= common
->width
;
171 vb
->height
= common
->height
;
172 vb
->size
= vb
->width
* vb
->height
;
175 vb
->state
= VIDEOBUF_PREPARED
;
177 * if user pointer memory mechanism is used, get the physical
178 * address of the buffer
180 if (V4L2_MEMORY_USERPTR
== common
->memory
) {
181 if (0 == vb
->baddr
) {
182 vpif_dbg(1, debug
, "buffer address is 0\n");
186 vb
->boff
= vpif_uservirt_to_phys(vb
->baddr
);
187 if (!IS_ALIGNED(vb
->boff
, 8))
193 if (!IS_ALIGNED((addr
+ common
->ytop_off
), 8) ||
194 !IS_ALIGNED((addr
+ common
->ybtm_off
), 8) ||
195 !IS_ALIGNED((addr
+ common
->ctop_off
), 8) ||
196 !IS_ALIGNED((addr
+ common
->cbtm_off
), 8))
201 vpif_dbg(1, debug
, "buffer_prepare:offset is not aligned to 8 bytes\n");
206 * vpif_buffer_setup : Callback function for buffer setup.
207 * @q: buffer queue ptr
208 * @count: number of buffers
209 * @size: size of the buffer
211 * This callback function is called when reqbuf() is called to adjust
212 * the buffer count and buffer size
214 static int vpif_buffer_setup(struct videobuf_queue
*q
, unsigned int *count
,
217 /* Get the file handle object and channel object */
218 struct vpif_fh
*fh
= q
->priv_data
;
219 struct channel_obj
*ch
= fh
->channel
;
220 struct common_obj
*common
;
222 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
224 vpif_dbg(2, debug
, "vpif_buffer_setup\n");
226 /* If memory type is not mmap, return */
227 if (V4L2_MEMORY_MMAP
!= common
->memory
)
230 /* Calculate the size of the buffer */
231 *size
= config_params
.channel_bufsize
[ch
->channel_id
];
233 if (*count
< config_params
.min_numbuffers
)
234 *count
= config_params
.min_numbuffers
;
239 * vpif_buffer_queue : Callback function to add buffer to DMA queue
240 * @q: ptr to videobuf_queue
241 * @vb: ptr to videobuf_buffer
243 static void vpif_buffer_queue(struct videobuf_queue
*q
,
244 struct videobuf_buffer
*vb
)
246 /* Get the file handle object and channel object */
247 struct vpif_fh
*fh
= q
->priv_data
;
248 struct channel_obj
*ch
= fh
->channel
;
249 struct common_obj
*common
;
251 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
253 vpif_dbg(2, debug
, "vpif_buffer_queue\n");
255 /* add the buffer to the DMA queue */
256 list_add_tail(&vb
->queue
, &common
->dma_queue
);
257 /* Change state of the buffer */
258 vb
->state
= VIDEOBUF_QUEUED
;
262 * vpif_buffer_release : Callback function to free buffer
263 * @q: buffer queue ptr
264 * @vb: ptr to video buffer
266 * This function is called from the videobuf layer to free memory
267 * allocated to the buffers
269 static void vpif_buffer_release(struct videobuf_queue
*q
,
270 struct videobuf_buffer
*vb
)
272 /* Get the file handle object and channel object */
273 struct vpif_fh
*fh
= q
->priv_data
;
274 struct channel_obj
*ch
= fh
->channel
;
275 struct common_obj
*common
;
277 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
279 videobuf_dma_contig_free(q
, vb
);
280 vb
->state
= VIDEOBUF_NEEDS_INIT
;
283 static struct videobuf_queue_ops video_qops
= {
284 .buf_setup
= vpif_buffer_setup
,
285 .buf_prepare
= vpif_buffer_prepare
,
286 .buf_queue
= vpif_buffer_queue
,
287 .buf_release
= vpif_buffer_release
,
290 static u8 channel_first_int
[VPIF_NUMBER_OF_OBJECTS
][2] =
294 * vpif_process_buffer_complete: process a completed buffer
295 * @common: ptr to common channel object
297 * This function time stamp the buffer and mark it as DONE. It also
298 * wake up any process waiting on the QUEUE and set the next buffer
301 static void vpif_process_buffer_complete(struct common_obj
*common
)
303 do_gettimeofday(&common
->cur_frm
->ts
);
304 common
->cur_frm
->state
= VIDEOBUF_DONE
;
305 wake_up_interruptible(&common
->cur_frm
->done
);
306 /* Make curFrm pointing to nextFrm */
307 common
->cur_frm
= common
->next_frm
;
311 * vpif_schedule_next_buffer: set next buffer address for capture
312 * @common : ptr to common channel object
314 * This function will get next buffer from the dma queue and
315 * set the buffer address in the vpif register for capture.
316 * the buffer is marked active
318 static void vpif_schedule_next_buffer(struct common_obj
*common
)
320 unsigned long addr
= 0;
322 common
->next_frm
= list_entry(common
->dma_queue
.next
,
323 struct videobuf_buffer
, queue
);
324 /* Remove that buffer from the buffer queue */
325 list_del(&common
->next_frm
->queue
);
326 common
->next_frm
->state
= VIDEOBUF_ACTIVE
;
327 if (V4L2_MEMORY_USERPTR
== common
->memory
)
328 addr
= common
->next_frm
->boff
;
330 addr
= videobuf_to_dma_contig(common
->next_frm
);
332 /* Set top and bottom field addresses in VPIF registers */
333 common
->set_addr(addr
+ common
->ytop_off
,
334 addr
+ common
->ybtm_off
,
335 addr
+ common
->ctop_off
,
336 addr
+ common
->cbtm_off
);
340 * vpif_channel_isr : ISR handler for vpif capture
342 * @dev_id: dev_id ptr
344 * It changes status of the captured buffer, takes next buffer from the queue
345 * and sets its address in VPIF registers
347 static irqreturn_t
vpif_channel_isr(int irq
, void *dev_id
)
349 struct vpif_device
*dev
= &vpif_obj
;
350 struct common_obj
*common
;
351 struct channel_obj
*ch
;
352 enum v4l2_field field
;
356 channel_id
= *(int *)(dev_id
);
357 ch
= dev
->dev
[channel_id
];
359 field
= ch
->common
[VPIF_VIDEO_INDEX
].fmt
.fmt
.pix
.field
;
361 for (i
= 0; i
< VPIF_NUMBER_OF_OBJECTS
; i
++) {
362 common
= &ch
->common
[i
];
363 /* skip If streaming is not started in this channel */
364 if (0 == common
->started
)
367 /* Check the field format */
368 if (1 == ch
->vpifparams
.std_info
.frm_fmt
) {
369 /* Progressive mode */
370 if (list_empty(&common
->dma_queue
))
373 if (!channel_first_int
[i
][channel_id
])
374 vpif_process_buffer_complete(common
);
376 channel_first_int
[i
][channel_id
] = 0;
378 vpif_schedule_next_buffer(common
);
381 channel_first_int
[i
][channel_id
] = 0;
384 * Interlaced mode. If it is first interrupt, ignore
387 if (channel_first_int
[i
][channel_id
]) {
388 channel_first_int
[i
][channel_id
] = 0;
393 /* Get field id from VPIF registers */
394 fid
= vpif_channel_getfid(ch
->channel_id
);
395 if (fid
!= ch
->field_id
) {
397 * If field id does not match stored
398 * field id, make them in sync
405 /* device field id and local field id are in sync */
407 /* this is even field */
408 if (common
->cur_frm
== common
->next_frm
)
411 /* mark the current buffer as done */
412 vpif_process_buffer_complete(common
);
413 } else if (1 == fid
) {
415 if (list_empty(&common
->dma_queue
) ||
416 (common
->cur_frm
!= common
->next_frm
))
419 vpif_schedule_next_buffer(common
);
427 * vpif_update_std_info() - update standard related info
428 * @ch: ptr to channel object
430 * For a given standard selected by application, update values
431 * in the device data structures
433 static int vpif_update_std_info(struct channel_obj
*ch
)
435 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
436 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
437 const struct vpif_channel_config_params
*config
;
438 struct vpif_channel_config_params
*std_info
;
439 struct video_obj
*vid_ch
= &ch
->video
;
442 vpif_dbg(2, debug
, "vpif_update_std_info\n");
444 std_info
= &vpifparams
->std_info
;
446 for (index
= 0; index
< ARRAY_SIZE(ch_params
); index
++) {
447 config
= &ch_params
[index
];
448 if (config
->stdid
& vid_ch
->stdid
) {
449 memcpy(std_info
, config
, sizeof(*config
));
454 /* standard not found */
455 if (index
== ARRAY_SIZE(ch_params
))
458 common
->fmt
.fmt
.pix
.width
= std_info
->width
;
459 common
->width
= std_info
->width
;
460 common
->fmt
.fmt
.pix
.height
= std_info
->height
;
461 common
->height
= std_info
->height
;
462 common
->fmt
.fmt
.pix
.bytesperline
= std_info
->width
;
463 vpifparams
->video_params
.hpitch
= std_info
->width
;
464 vpifparams
->video_params
.storage_mode
= std_info
->frm_fmt
;
469 * vpif_calculate_offsets : This function calculates buffers offsets
470 * @ch : ptr to channel object
472 * This function calculates buffer offsets for Y and C in the top and
475 static void vpif_calculate_offsets(struct channel_obj
*ch
)
477 unsigned int hpitch
, vpitch
, sizeimage
;
478 struct video_obj
*vid_ch
= &(ch
->video
);
479 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
480 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
481 enum v4l2_field field
= common
->fmt
.fmt
.pix
.field
;
483 vpif_dbg(2, debug
, "vpif_calculate_offsets\n");
485 if (V4L2_FIELD_ANY
== field
) {
486 if (vpifparams
->std_info
.frm_fmt
)
487 vid_ch
->buf_field
= V4L2_FIELD_NONE
;
489 vid_ch
->buf_field
= V4L2_FIELD_INTERLACED
;
491 vid_ch
->buf_field
= common
->fmt
.fmt
.pix
.field
;
493 if (V4L2_MEMORY_USERPTR
== common
->memory
)
494 sizeimage
= common
->fmt
.fmt
.pix
.sizeimage
;
496 sizeimage
= config_params
.channel_bufsize
[ch
->channel_id
];
498 hpitch
= common
->fmt
.fmt
.pix
.bytesperline
;
499 vpitch
= sizeimage
/ (hpitch
* 2);
501 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
502 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
)) {
503 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
504 common
->ytop_off
= 0;
505 common
->ybtm_off
= hpitch
;
506 common
->ctop_off
= sizeimage
/ 2;
507 common
->cbtm_off
= sizeimage
/ 2 + hpitch
;
508 } else if (V4L2_FIELD_SEQ_TB
== vid_ch
->buf_field
) {
509 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
510 common
->ytop_off
= 0;
511 common
->ybtm_off
= sizeimage
/ 4;
512 common
->ctop_off
= sizeimage
/ 2;
513 common
->cbtm_off
= common
->ctop_off
+ sizeimage
/ 4;
514 } else if (V4L2_FIELD_SEQ_BT
== vid_ch
->buf_field
) {
515 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
516 common
->ybtm_off
= 0;
517 common
->ytop_off
= sizeimage
/ 4;
518 common
->cbtm_off
= sizeimage
/ 2;
519 common
->ctop_off
= common
->cbtm_off
+ sizeimage
/ 4;
521 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
522 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
))
523 vpifparams
->video_params
.storage_mode
= 1;
525 vpifparams
->video_params
.storage_mode
= 0;
527 if (1 == vpifparams
->std_info
.frm_fmt
)
528 vpifparams
->video_params
.hpitch
=
529 common
->fmt
.fmt
.pix
.bytesperline
;
531 if ((field
== V4L2_FIELD_ANY
)
532 || (field
== V4L2_FIELD_INTERLACED
))
533 vpifparams
->video_params
.hpitch
=
534 common
->fmt
.fmt
.pix
.bytesperline
* 2;
536 vpifparams
->video_params
.hpitch
=
537 common
->fmt
.fmt
.pix
.bytesperline
;
540 ch
->vpifparams
.video_params
.stdid
= vpifparams
->std_info
.stdid
;
544 * vpif_config_format: configure default frame format in the device
545 * ch : ptr to channel object
547 static void vpif_config_format(struct channel_obj
*ch
)
549 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
551 vpif_dbg(2, debug
, "vpif_config_format\n");
553 common
->fmt
.fmt
.pix
.field
= V4L2_FIELD_ANY
;
554 if (config_params
.numbuffers
[ch
->channel_id
] == 0)
555 common
->memory
= V4L2_MEMORY_USERPTR
;
557 common
->memory
= V4L2_MEMORY_MMAP
;
559 common
->fmt
.fmt
.pix
.sizeimage
560 = config_params
.channel_bufsize
[ch
->channel_id
];
562 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
)
563 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
565 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUV422P
;
566 common
->fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
570 * vpif_get_default_field() - Get default field type based on interface
571 * @vpif_params - ptr to vpif params
573 static inline enum v4l2_field
vpif_get_default_field(
574 struct vpif_interface
*iface
)
576 return (iface
->if_type
== VPIF_IF_RAW_BAYER
) ? V4L2_FIELD_NONE
:
577 V4L2_FIELD_INTERLACED
;
581 * vpif_check_format() - check given pixel format for compatibility
583 * @pixfmt - Given pixel format
584 * @update - update the values as per hardware requirement
586 * Check the application pixel format for S_FMT and update the input
587 * values as per hardware limits for TRY_FMT. The default pixel and
588 * field format is selected based on interface type.
590 static int vpif_check_format(struct channel_obj
*ch
,
591 struct v4l2_pix_format
*pixfmt
,
594 struct common_obj
*common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
595 struct vpif_params
*vpif_params
= &ch
->vpifparams
;
596 enum v4l2_field field
= pixfmt
->field
;
597 u32 sizeimage
, hpitch
, vpitch
;
600 vpif_dbg(2, debug
, "vpif_check_format\n");
602 * first check for the pixel format. If if_type is Raw bayer,
603 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
604 * V4L2_PIX_FMT_YUV422P is supported
606 if (vpif_params
->iface
.if_type
== VPIF_IF_RAW_BAYER
) {
607 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
) {
609 vpif_dbg(2, debug
, "invalid pix format\n");
612 pixfmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
615 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_YUV422P
) {
617 vpif_dbg(2, debug
, "invalid pixel format\n");
620 pixfmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
624 if (!(VPIF_VALID_FIELD(field
))) {
626 vpif_dbg(2, debug
, "invalid field format\n");
630 * By default use FIELD_NONE for RAW Bayer capture
631 * and FIELD_INTERLACED for other interfaces
633 field
= vpif_get_default_field(&vpif_params
->iface
);
634 } else if (field
== V4L2_FIELD_ANY
)
635 /* unsupported field. Use default */
636 field
= vpif_get_default_field(&vpif_params
->iface
);
638 /* validate the hpitch */
639 hpitch
= pixfmt
->bytesperline
;
640 if (hpitch
< vpif_params
->std_info
.width
) {
642 vpif_dbg(2, debug
, "invalid hpitch\n");
645 hpitch
= vpif_params
->std_info
.width
;
648 if (V4L2_MEMORY_USERPTR
== common
->memory
)
649 sizeimage
= pixfmt
->sizeimage
;
651 sizeimage
= config_params
.channel_bufsize
[ch
->channel_id
];
653 vpitch
= sizeimage
/ (hpitch
* 2);
655 /* validate the vpitch */
656 if (vpitch
< vpif_params
->std_info
.height
) {
658 vpif_dbg(2, debug
, "Invalid vpitch\n");
661 vpitch
= vpif_params
->std_info
.height
;
664 /* Check for 8 byte alignment */
665 if (!ALIGN(hpitch
, 8)) {
667 vpif_dbg(2, debug
, "invalid pitch alignment\n");
670 /* adjust to next 8 byte boundary */
671 hpitch
= (((hpitch
+ 7) / 8) * 8);
673 /* if update is set, modify the bytesperline and sizeimage */
675 pixfmt
->bytesperline
= hpitch
;
676 pixfmt
->sizeimage
= hpitch
* vpitch
* 2;
679 * Image width and height is always based on current standard width and
682 pixfmt
->width
= common
->fmt
.fmt
.pix
.width
;
683 pixfmt
->height
= common
->fmt
.fmt
.pix
.height
;
690 * vpif_config_addr() - function to configure buffer address in vpif
692 * @muxmode - channel mux mode
694 static void vpif_config_addr(struct channel_obj
*ch
, int muxmode
)
696 struct common_obj
*common
;
698 vpif_dbg(2, debug
, "vpif_config_addr\n");
700 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
702 if (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)
703 common
->set_addr
= ch1_set_videobuf_addr
;
704 else if (2 == muxmode
)
705 common
->set_addr
= ch0_set_videobuf_addr_yc_nmux
;
707 common
->set_addr
= ch0_set_videobuf_addr
;
711 * vpfe_mmap : It is used to map kernel space buffers into user spaces
712 * @filep: file pointer
713 * @vma: ptr to vm_area_struct
715 static int vpif_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
717 /* Get the channel object and file handle object */
718 struct vpif_fh
*fh
= filep
->private_data
;
719 struct channel_obj
*ch
= fh
->channel
;
720 struct common_obj
*common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
722 vpif_dbg(2, debug
, "vpif_mmap\n");
724 return videobuf_mmap_mapper(&common
->buffer_queue
, vma
);
728 * vpif_poll: It is used for select/poll system call
729 * @filep: file pointer
730 * @wait: poll table to wait
732 static unsigned int vpif_poll(struct file
*filep
, poll_table
* wait
)
734 struct vpif_fh
*fh
= filep
->private_data
;
735 struct channel_obj
*channel
= fh
->channel
;
736 struct common_obj
*common
= &(channel
->common
[VPIF_VIDEO_INDEX
]);
738 vpif_dbg(2, debug
, "vpif_poll\n");
741 return videobuf_poll_stream(filep
, &common
->buffer_queue
, wait
);
746 * vpif_open : vpif open handler
749 * It creates object of file handle structure and stores it in private_data
750 * member of filepointer
752 static int vpif_open(struct file
*filep
)
754 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
755 struct video_device
*vdev
= video_devdata(filep
);
756 struct common_obj
*common
;
757 struct video_obj
*vid_ch
;
758 struct channel_obj
*ch
;
762 vpif_dbg(2, debug
, "vpif_open\n");
764 ch
= video_get_drvdata(vdev
);
767 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
769 if (mutex_lock_interruptible(&common
->lock
))
772 if (NULL
== ch
->curr_subdev_info
) {
774 * search through the sub device to see a registered
775 * sub device and make it as current sub device
777 for (i
= 0; i
< config
->subdev_count
; i
++) {
778 if (vpif_obj
.sd
[i
]) {
779 /* the sub device is registered */
780 ch
->curr_subdev_info
= &config
->subdev_info
[i
];
781 /* make first input as the current input */
782 vid_ch
->input_idx
= 0;
786 if (i
== config
->subdev_count
) {
787 vpif_err("No sub device registered\n");
793 /* Allocate memory for the file handle object */
794 fh
= kzalloc(sizeof(struct vpif_fh
), GFP_KERNEL
);
796 vpif_err("unable to allocate memory for file handle object\n");
801 /* store pointer to fh in private_data member of filep */
802 filep
->private_data
= fh
;
805 /* If decoder is not initialized. initialize it */
806 if (!ch
->initialized
) {
809 memset(&(ch
->vpifparams
), 0, sizeof(struct vpif_params
));
811 /* Increment channel usrs counter */
813 /* Set io_allowed member to false */
814 fh
->io_allowed
[VPIF_VIDEO_INDEX
] = 0;
815 /* Initialize priority of this instance to default priority */
816 fh
->prio
= V4L2_PRIORITY_UNSET
;
817 v4l2_prio_open(&ch
->prio
, &fh
->prio
);
819 mutex_unlock(&common
->lock
);
824 * vpif_release : function to clean up file close
825 * @filep: file pointer
827 * This function deletes buffer queue, frees the buffers and the vpfe file
830 static int vpif_release(struct file
*filep
)
832 struct vpif_fh
*fh
= filep
->private_data
;
833 struct channel_obj
*ch
= fh
->channel
;
834 struct common_obj
*common
;
836 vpif_dbg(2, debug
, "vpif_release\n");
838 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
840 if (mutex_lock_interruptible(&common
->lock
))
843 /* if this instance is doing IO */
844 if (fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
845 /* Reset io_usrs member of channel object */
847 /* Disable channel as per its device type and channel id */
848 if (VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) {
850 channel0_intr_enable(0);
852 if ((VPIF_CHANNEL1_VIDEO
== ch
->channel_id
) ||
853 (2 == common
->started
)) {
855 channel1_intr_enable(0);
858 /* Free buffers allocated */
859 videobuf_queue_cancel(&common
->buffer_queue
);
860 videobuf_mmap_free(&common
->buffer_queue
);
863 /* Decrement channel usrs counter */
866 /* unlock mutex on channel object */
867 mutex_unlock(&common
->lock
);
869 /* Close the priority */
870 v4l2_prio_close(&ch
->prio
, fh
->prio
);
875 filep
->private_data
= NULL
;
881 * vpif_reqbufs() - request buffer handler
884 * @reqbuf: request buffer structure ptr
886 static int vpif_reqbufs(struct file
*file
, void *priv
,
887 struct v4l2_requestbuffers
*reqbuf
)
889 struct vpif_fh
*fh
= priv
;
890 struct channel_obj
*ch
= fh
->channel
;
891 struct common_obj
*common
;
895 vpif_dbg(2, debug
, "vpif_reqbufs\n");
898 * This file handle has not initialized the channel,
899 * It is not allowed to do settings
901 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
)
902 || (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
903 if (!fh
->initialized
) {
904 vpif_dbg(1, debug
, "Channel Busy\n");
909 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= reqbuf
->type
)
912 index
= VPIF_VIDEO_INDEX
;
914 common
= &ch
->common
[index
];
916 if (mutex_lock_interruptible(&common
->lock
))
919 if (0 != common
->io_usrs
) {
924 /* Initialize videobuf queue as per the buffer type */
925 videobuf_queue_dma_contig_init(&common
->buffer_queue
,
929 common
->fmt
.fmt
.pix
.field
,
930 sizeof(struct videobuf_buffer
), fh
,
933 /* Set io allowed member of file handle to TRUE */
934 fh
->io_allowed
[index
] = 1;
935 /* Increment io usrs member of channel object to 1 */
937 /* Store type of memory requested in channel object */
938 common
->memory
= reqbuf
->memory
;
939 INIT_LIST_HEAD(&common
->dma_queue
);
941 /* Allocate buffers */
942 ret
= videobuf_reqbufs(&common
->buffer_queue
, reqbuf
);
945 mutex_unlock(&common
->lock
);
950 * vpif_querybuf() - query buffer handler
953 * @buf: v4l2 buffer structure ptr
955 static int vpif_querybuf(struct file
*file
, void *priv
,
956 struct v4l2_buffer
*buf
)
958 struct vpif_fh
*fh
= priv
;
959 struct channel_obj
*ch
= fh
->channel
;
960 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
962 vpif_dbg(2, debug
, "vpif_querybuf\n");
964 if (common
->fmt
.type
!= buf
->type
)
967 if (common
->memory
!= V4L2_MEMORY_MMAP
) {
968 vpif_dbg(1, debug
, "Invalid memory\n");
972 return videobuf_querybuf(&common
->buffer_queue
, buf
);
976 * vpif_qbuf() - query buffer handler
979 * @buf: v4l2 buffer structure ptr
981 static int vpif_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
984 struct vpif_fh
*fh
= priv
;
985 struct channel_obj
*ch
= fh
->channel
;
986 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
987 struct v4l2_buffer tbuf
= *buf
;
988 struct videobuf_buffer
*buf1
;
989 unsigned long addr
= 0;
993 vpif_dbg(2, debug
, "vpif_qbuf\n");
995 if (common
->fmt
.type
!= tbuf
.type
) {
996 vpif_err("invalid buffer type\n");
1000 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1001 vpif_err("fh io not allowed \n");
1005 if (!(list_empty(&common
->dma_queue
)) ||
1006 (common
->cur_frm
!= common
->next_frm
) ||
1008 (common
->started
&& (0 == ch
->field_id
)))
1009 return videobuf_qbuf(&common
->buffer_queue
, buf
);
1011 /* bufferqueue is empty store buffer address in VPIF registers */
1012 mutex_lock(&common
->buffer_queue
.vb_lock
);
1013 buf1
= common
->buffer_queue
.bufs
[tbuf
.index
];
1015 if ((buf1
->state
== VIDEOBUF_QUEUED
) ||
1016 (buf1
->state
== VIDEOBUF_ACTIVE
)) {
1017 vpif_err("invalid state\n");
1021 switch (buf1
->memory
) {
1022 case V4L2_MEMORY_MMAP
:
1023 if (buf1
->baddr
== 0)
1027 case V4L2_MEMORY_USERPTR
:
1028 if (tbuf
.length
< buf1
->bsize
)
1031 if ((VIDEOBUF_NEEDS_INIT
!= buf1
->state
)
1032 && (buf1
->baddr
!= tbuf
.m
.userptr
)) {
1033 vpif_buffer_release(&common
->buffer_queue
, buf1
);
1034 buf1
->baddr
= tbuf
.m
.userptr
;
1042 local_irq_save(flags
);
1043 ret
= vpif_buffer_prepare(&common
->buffer_queue
, buf1
,
1044 common
->buffer_queue
.field
);
1046 local_irq_restore(flags
);
1050 buf1
->state
= VIDEOBUF_ACTIVE
;
1052 if (V4L2_MEMORY_USERPTR
== common
->memory
)
1055 addr
= videobuf_to_dma_contig(buf1
);
1057 common
->next_frm
= buf1
;
1058 common
->set_addr(addr
+ common
->ytop_off
,
1059 addr
+ common
->ybtm_off
,
1060 addr
+ common
->ctop_off
,
1061 addr
+ common
->cbtm_off
);
1063 local_irq_restore(flags
);
1064 list_add_tail(&buf1
->stream
, &common
->buffer_queue
.stream
);
1065 mutex_unlock(&common
->buffer_queue
.vb_lock
);
1069 mutex_unlock(&common
->buffer_queue
.vb_lock
);
1074 * vpif_dqbuf() - query buffer handler
1076 * @priv: file handle
1077 * @buf: v4l2 buffer structure ptr
1079 static int vpif_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
1081 struct vpif_fh
*fh
= priv
;
1082 struct channel_obj
*ch
= fh
->channel
;
1083 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1085 vpif_dbg(2, debug
, "vpif_dqbuf\n");
1087 return videobuf_dqbuf(&common
->buffer_queue
, buf
,
1088 file
->f_flags
& O_NONBLOCK
);
1092 * vpif_streamon() - streamon handler
1094 * @priv: file handle
1095 * @buftype: v4l2 buffer type
1097 static int vpif_streamon(struct file
*file
, void *priv
,
1098 enum v4l2_buf_type buftype
)
1101 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1102 struct vpif_fh
*fh
= priv
;
1103 struct channel_obj
*ch
= fh
->channel
;
1104 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1105 struct channel_obj
*oth_ch
= vpif_obj
.dev
[!ch
->channel_id
];
1106 struct vpif_params
*vpif
;
1107 unsigned long addr
= 0;
1110 vpif_dbg(2, debug
, "vpif_streamon\n");
1112 vpif
= &ch
->vpifparams
;
1114 if (buftype
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1115 vpif_dbg(1, debug
, "buffer type not supported\n");
1119 /* If file handle is not allowed IO, return error */
1120 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1121 vpif_dbg(1, debug
, "io not allowed\n");
1125 /* If Streaming is already started, return error */
1126 if (common
->started
) {
1127 vpif_dbg(1, debug
, "channel->started\n");
1131 if ((ch
->channel_id
== VPIF_CHANNEL0_VIDEO
&&
1132 oth_ch
->common
[VPIF_VIDEO_INDEX
].started
&&
1133 vpif
->std_info
.ycmux_mode
== 0) ||
1134 ((ch
->channel_id
== VPIF_CHANNEL1_VIDEO
) &&
1135 (2 == oth_ch
->common
[VPIF_VIDEO_INDEX
].started
))) {
1136 vpif_dbg(1, debug
, "other channel is being used\n");
1140 ret
= vpif_check_format(ch
, &common
->fmt
.fmt
.pix
, 0);
1144 /* Enable streamon on the sub device */
1145 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1148 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
1149 vpif_dbg(1, debug
, "stream on failed in subdev\n");
1153 /* Call videobuf_streamon to start streaming in videobuf */
1154 ret
= videobuf_streamon(&common
->buffer_queue
);
1156 vpif_dbg(1, debug
, "videobuf_streamon\n");
1160 if (mutex_lock_interruptible(&common
->lock
)) {
1162 goto streamoff_exit
;
1165 /* If buffer queue is empty, return error */
1166 if (list_empty(&common
->dma_queue
)) {
1167 vpif_dbg(1, debug
, "buffer queue is empty\n");
1172 /* Get the next frame from the buffer queue */
1173 common
->cur_frm
= list_entry(common
->dma_queue
.next
,
1174 struct videobuf_buffer
, queue
);
1175 common
->next_frm
= common
->cur_frm
;
1177 /* Remove buffer from the buffer queue */
1178 list_del(&common
->cur_frm
->queue
);
1179 /* Mark state of the current frame to active */
1180 common
->cur_frm
->state
= VIDEOBUF_ACTIVE
;
1181 /* Initialize field_id and started member */
1183 common
->started
= 1;
1185 if (V4L2_MEMORY_USERPTR
== common
->memory
)
1186 addr
= common
->cur_frm
->boff
;
1188 addr
= videobuf_to_dma_contig(common
->cur_frm
);
1190 /* Calculate the offset for Y and C data in the buffer */
1191 vpif_calculate_offsets(ch
);
1193 if ((vpif
->std_info
.frm_fmt
&&
1194 ((common
->fmt
.fmt
.pix
.field
!= V4L2_FIELD_NONE
) &&
1195 (common
->fmt
.fmt
.pix
.field
!= V4L2_FIELD_ANY
))) ||
1196 (!vpif
->std_info
.frm_fmt
&&
1197 (common
->fmt
.fmt
.pix
.field
== V4L2_FIELD_NONE
))) {
1198 vpif_dbg(1, debug
, "conflict in field format and std format\n");
1203 /* configure 1 or 2 channel mode */
1204 ret
= config
->setup_input_channel_mode(vpif
->std_info
.ycmux_mode
);
1207 vpif_dbg(1, debug
, "can't set vpif channel mode\n");
1211 /* Call vpif_set_params function to set the parameters and addresses */
1212 ret
= vpif_set_video_params(vpif
, ch
->channel_id
);
1215 vpif_dbg(1, debug
, "can't set video params\n");
1219 common
->started
= ret
;
1220 vpif_config_addr(ch
, ret
);
1222 common
->set_addr(addr
+ common
->ytop_off
,
1223 addr
+ common
->ybtm_off
,
1224 addr
+ common
->ctop_off
,
1225 addr
+ common
->cbtm_off
);
1228 * Set interrupt for both the fields in VPIF Register enable channel in
1231 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
)) {
1232 channel0_intr_assert();
1233 channel0_intr_enable(1);
1236 if ((VPIF_CHANNEL1_VIDEO
== ch
->channel_id
) ||
1237 (common
->started
== 2)) {
1238 channel1_intr_assert();
1239 channel1_intr_enable(1);
1242 channel_first_int
[VPIF_VIDEO_INDEX
][ch
->channel_id
] = 1;
1243 mutex_unlock(&common
->lock
);
1247 mutex_unlock(&common
->lock
);
1249 ret
= videobuf_streamoff(&common
->buffer_queue
);
1254 * vpif_streamoff() - streamoff handler
1256 * @priv: file handle
1257 * @buftype: v4l2 buffer type
1259 static int vpif_streamoff(struct file
*file
, void *priv
,
1260 enum v4l2_buf_type buftype
)
1263 struct vpif_fh
*fh
= priv
;
1264 struct channel_obj
*ch
= fh
->channel
;
1265 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1268 vpif_dbg(2, debug
, "vpif_streamoff\n");
1270 if (buftype
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1271 vpif_dbg(1, debug
, "buffer type not supported\n");
1275 /* If io is allowed for this file handle, return error */
1276 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1277 vpif_dbg(1, debug
, "io not allowed\n");
1281 /* If streaming is not started, return error */
1282 if (!common
->started
) {
1283 vpif_dbg(1, debug
, "channel->started\n");
1287 if (mutex_lock_interruptible(&common
->lock
))
1288 return -ERESTARTSYS
;
1290 /* disable channel */
1291 if (VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) {
1293 channel0_intr_enable(0);
1296 channel1_intr_enable(0);
1299 common
->started
= 0;
1301 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1304 if (ret
&& (ret
!= -ENOIOCTLCMD
))
1305 vpif_dbg(1, debug
, "stream off failed in subdev\n");
1307 mutex_unlock(&common
->lock
);
1309 return videobuf_streamoff(&common
->buffer_queue
);
1313 * vpif_map_sub_device_to_input() - Maps sub device to input
1314 * @ch - ptr to channel
1315 * @config - ptr to capture configuration
1316 * @input_index - Given input index from application
1317 * @sub_device_index - index into sd table
1319 * lookup the sub device information for a given input index.
1320 * we report all the inputs to application. inputs table also
1321 * has sub device name for the each input
1323 static struct vpif_subdev_info
*vpif_map_sub_device_to_input(
1324 struct channel_obj
*ch
,
1325 struct vpif_capture_config
*vpif_cfg
,
1327 int *sub_device_index
)
1329 struct vpif_capture_chan_config
*chan_cfg
;
1330 struct vpif_subdev_info
*subdev_info
= NULL
;
1331 const char *subdev_name
= NULL
;
1334 vpif_dbg(2, debug
, "vpif_map_sub_device_to_input\n");
1336 chan_cfg
= &vpif_cfg
->chan_config
[ch
->channel_id
];
1339 * search through the inputs to find the sub device supporting
1342 for (i
= 0; i
< chan_cfg
->input_count
; i
++) {
1343 /* For each sub device, loop through input */
1344 if (i
== input_index
) {
1345 subdev_name
= chan_cfg
->inputs
[i
].subdev_name
;
1350 /* if reached maximum. return null */
1351 if (i
== chan_cfg
->input_count
|| (NULL
== subdev_name
))
1354 /* loop through the sub device list to get the sub device info */
1355 for (i
= 0; i
< vpif_cfg
->subdev_count
; i
++) {
1356 subdev_info
= &vpif_cfg
->subdev_info
[i
];
1357 if (!strcmp(subdev_info
->name
, subdev_name
))
1361 if (i
== vpif_cfg
->subdev_count
)
1364 /* check if the sub device is registered */
1365 if (NULL
== vpif_obj
.sd
[i
])
1368 *sub_device_index
= i
;
1373 * vpif_querystd() - querystd handler
1375 * @priv: file handle
1376 * @std_id: ptr to std id
1378 * This function is called to detect standard at the selected input
1380 static int vpif_querystd(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_querystd\n");
1389 if (mutex_lock_interruptible(&common
->lock
))
1390 return -ERESTARTSYS
;
1392 /* Call querystd function of decoder device */
1393 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1396 vpif_dbg(1, debug
, "Failed to set standard for sub devices\n");
1398 mutex_unlock(&common
->lock
);
1403 * vpif_g_std() - get STD handler
1405 * @priv: file handle
1406 * @std_id: ptr to std id
1408 static int vpif_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
1410 struct vpif_fh
*fh
= priv
;
1411 struct channel_obj
*ch
= fh
->channel
;
1413 vpif_dbg(2, debug
, "vpif_g_std\n");
1415 *std
= ch
->video
.stdid
;
1420 * vpif_s_std() - set STD handler
1422 * @priv: file handle
1423 * @std_id: ptr to std id
1425 static int vpif_s_std(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1427 struct vpif_fh
*fh
= priv
;
1428 struct channel_obj
*ch
= fh
->channel
;
1429 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1432 vpif_dbg(2, debug
, "vpif_s_std\n");
1434 if (common
->started
) {
1435 vpif_err("streaming in progress\n");
1439 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1440 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1441 if (!fh
->initialized
) {
1442 vpif_dbg(1, debug
, "Channel Busy\n");
1447 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1451 fh
->initialized
= 1;
1453 /* Call encoder subdevice function to set the standard */
1454 if (mutex_lock_interruptible(&common
->lock
))
1455 return -ERESTARTSYS
;
1457 ch
->video
.stdid
= *std_id
;
1459 /* Get the information about the standard */
1460 if (vpif_update_std_info(ch
)) {
1462 vpif_err("Error getting the standard info\n");
1466 /* Configure the default format information */
1467 vpif_config_format(ch
);
1469 /* set standard in the sub device */
1470 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], core
,
1473 vpif_dbg(1, debug
, "Failed to set standard for sub devices\n");
1476 mutex_unlock(&common
->lock
);
1481 * vpif_enum_input() - ENUMINPUT handler
1483 * @priv: file handle
1484 * @input: ptr to input structure
1486 static int vpif_enum_input(struct file
*file
, void *priv
,
1487 struct v4l2_input
*input
)
1490 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1491 struct vpif_capture_chan_config
*chan_cfg
;
1492 struct vpif_fh
*fh
= priv
;
1493 struct channel_obj
*ch
= fh
->channel
;
1495 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1497 if (input
->index
>= chan_cfg
->input_count
) {
1498 vpif_dbg(1, debug
, "Invalid input index\n");
1502 memcpy(input
, &chan_cfg
->inputs
[input
->index
].input
,
1508 * vpif_g_input() - Get INPUT handler
1510 * @priv: file handle
1511 * @index: ptr to input index
1513 static int vpif_g_input(struct file
*file
, void *priv
, unsigned int *index
)
1515 struct vpif_fh
*fh
= priv
;
1516 struct channel_obj
*ch
= fh
->channel
;
1517 struct video_obj
*vid_ch
= &ch
->video
;
1519 *index
= vid_ch
->input_idx
;
1525 * vpif_s_input() - Set INPUT handler
1527 * @priv: file handle
1528 * @index: input index
1530 static int vpif_s_input(struct file
*file
, void *priv
, unsigned int index
)
1532 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1533 struct vpif_capture_chan_config
*chan_cfg
;
1534 struct vpif_fh
*fh
= priv
;
1535 struct channel_obj
*ch
= fh
->channel
;
1536 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1537 struct video_obj
*vid_ch
= &ch
->video
;
1538 struct vpif_subdev_info
*subdev_info
;
1539 int ret
= 0, sd_index
= 0;
1540 u32 input
= 0, output
= 0;
1542 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1544 if (common
->started
) {
1545 vpif_err("Streaming in progress\n");
1549 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1550 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1551 if (!fh
->initialized
) {
1552 vpif_dbg(1, debug
, "Channel Busy\n");
1557 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1561 fh
->initialized
= 1;
1562 subdev_info
= vpif_map_sub_device_to_input(ch
, config
, index
,
1564 if (NULL
== subdev_info
) {
1566 "couldn't lookup sub device for the input index\n");
1570 if (mutex_lock_interruptible(&common
->lock
))
1571 return -ERESTARTSYS
;
1573 /* first setup input path from sub device to vpif */
1574 if (config
->setup_input_path
) {
1575 ret
= config
->setup_input_path(ch
->channel_id
,
1578 vpif_dbg(1, debug
, "couldn't setup input path for the"
1579 " sub device %s, for input index %d\n",
1580 subdev_info
->name
, index
);
1585 if (subdev_info
->can_route
) {
1586 input
= subdev_info
->input
;
1587 output
= subdev_info
->output
;
1588 ret
= v4l2_subdev_call(vpif_obj
.sd
[sd_index
], video
, s_routing
,
1591 vpif_dbg(1, debug
, "Failed to set input\n");
1595 vid_ch
->input_idx
= index
;
1596 ch
->curr_subdev_info
= subdev_info
;
1597 ch
->curr_sd_index
= sd_index
;
1598 /* copy interface parameters to vpif */
1599 ch
->vpifparams
.iface
= subdev_info
->vpif_if
;
1601 /* update tvnorms from the sub device input info */
1602 ch
->video_dev
->tvnorms
= chan_cfg
->inputs
[index
].input
.std
;
1605 mutex_unlock(&common
->lock
);
1610 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1612 * @priv: file handle
1613 * @index: input index
1615 static int vpif_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1616 struct v4l2_fmtdesc
*fmt
)
1618 struct vpif_fh
*fh
= priv
;
1619 struct channel_obj
*ch
= fh
->channel
;
1621 if (fmt
->index
!= 0) {
1622 vpif_dbg(1, debug
, "Invalid format index\n");
1626 /* Fill in the information about format */
1627 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
) {
1628 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1629 strcpy(fmt
->description
, "Raw Mode -Bayer Pattern GrRBGb");
1630 fmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
1632 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1633 strcpy(fmt
->description
, "YCbCr4:2:2 YC Planar");
1634 fmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
1640 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1642 * @priv: file handle
1643 * @fmt: ptr to v4l2 format structure
1645 static int vpif_try_fmt_vid_cap(struct file
*file
, void *priv
,
1646 struct v4l2_format
*fmt
)
1648 struct vpif_fh
*fh
= priv
;
1649 struct channel_obj
*ch
= fh
->channel
;
1650 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
1652 return vpif_check_format(ch
, pixfmt
, 1);
1657 * vpif_g_fmt_vid_cap() - Set INPUT handler
1659 * @priv: file handle
1660 * @fmt: ptr to v4l2 format structure
1662 static int vpif_g_fmt_vid_cap(struct file
*file
, void *priv
,
1663 struct v4l2_format
*fmt
)
1665 struct vpif_fh
*fh
= priv
;
1666 struct channel_obj
*ch
= fh
->channel
;
1667 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1669 /* Check the validity of the buffer type */
1670 if (common
->fmt
.type
!= fmt
->type
)
1673 /* Fill in the information about format */
1674 if (mutex_lock_interruptible(&common
->lock
))
1675 return -ERESTARTSYS
;
1678 mutex_unlock(&common
->lock
);
1683 * vpif_s_fmt_vid_cap() - Set FMT handler
1685 * @priv: file handle
1686 * @fmt: ptr to v4l2 format structure
1688 static int vpif_s_fmt_vid_cap(struct file
*file
, void *priv
,
1689 struct v4l2_format
*fmt
)
1691 struct vpif_fh
*fh
= priv
;
1692 struct channel_obj
*ch
= fh
->channel
;
1693 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1694 struct v4l2_pix_format
*pixfmt
;
1697 vpif_dbg(2, debug
, "VIDIOC_S_FMT\n");
1699 /* If streaming is started, return error */
1700 if (common
->started
) {
1701 vpif_dbg(1, debug
, "Streaming is started\n");
1705 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1706 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1707 if (!fh
->initialized
) {
1708 vpif_dbg(1, debug
, "Channel Busy\n");
1713 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1717 fh
->initialized
= 1;
1719 pixfmt
= &fmt
->fmt
.pix
;
1720 /* Check for valid field format */
1721 ret
= vpif_check_format(ch
, pixfmt
, 0);
1725 /* store the format in the channel object */
1726 if (mutex_lock_interruptible(&common
->lock
))
1727 return -ERESTARTSYS
;
1730 mutex_unlock(&common
->lock
);
1736 * vpif_querycap() - QUERYCAP handler
1738 * @priv: file handle
1739 * @cap: ptr to v4l2_capability structure
1741 static int vpif_querycap(struct file
*file
, void *priv
,
1742 struct v4l2_capability
*cap
)
1744 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1746 cap
->version
= VPIF_CAPTURE_VERSION_CODE
;
1747 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1748 strlcpy(cap
->driver
, "vpif capture", sizeof(cap
->driver
));
1749 strlcpy(cap
->bus_info
, "DM646x Platform", sizeof(cap
->bus_info
));
1750 strlcpy(cap
->card
, config
->card_name
, sizeof(cap
->card
));
1756 * vpif_g_priority() - get priority handler
1758 * @priv: file handle
1759 * @prio: ptr to v4l2_priority structure
1761 static int vpif_g_priority(struct file
*file
, void *priv
,
1762 enum v4l2_priority
*prio
)
1764 struct vpif_fh
*fh
= priv
;
1765 struct channel_obj
*ch
= fh
->channel
;
1767 *prio
= v4l2_prio_max(&ch
->prio
);
1773 * vpif_s_priority() - set priority handler
1775 * @priv: file handle
1776 * @prio: ptr to v4l2_priority structure
1778 static int vpif_s_priority(struct file
*file
, void *priv
, enum v4l2_priority p
)
1780 struct vpif_fh
*fh
= priv
;
1781 struct channel_obj
*ch
= fh
->channel
;
1783 return v4l2_prio_change(&ch
->prio
, &fh
->prio
, p
);
1787 * vpif_cropcap() - cropcap handler
1789 * @priv: file handle
1790 * @crop: ptr to v4l2_cropcap structure
1792 static int vpif_cropcap(struct file
*file
, void *priv
,
1793 struct v4l2_cropcap
*crop
)
1795 struct vpif_fh
*fh
= priv
;
1796 struct channel_obj
*ch
= fh
->channel
;
1797 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1799 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= crop
->type
)
1802 crop
->bounds
.left
= 0;
1803 crop
->bounds
.top
= 0;
1804 crop
->bounds
.height
= common
->height
;
1805 crop
->bounds
.width
= common
->width
;
1806 crop
->defrect
= crop
->bounds
;
1810 /* vpif capture ioctl operations */
1811 static const struct v4l2_ioctl_ops vpif_ioctl_ops
= {
1812 .vidioc_querycap
= vpif_querycap
,
1813 .vidioc_g_priority
= vpif_g_priority
,
1814 .vidioc_s_priority
= vpif_s_priority
,
1815 .vidioc_enum_fmt_vid_cap
= vpif_enum_fmt_vid_cap
,
1816 .vidioc_g_fmt_vid_cap
= vpif_g_fmt_vid_cap
,
1817 .vidioc_s_fmt_vid_cap
= vpif_s_fmt_vid_cap
,
1818 .vidioc_try_fmt_vid_cap
= vpif_try_fmt_vid_cap
,
1819 .vidioc_enum_input
= vpif_enum_input
,
1820 .vidioc_s_input
= vpif_s_input
,
1821 .vidioc_g_input
= vpif_g_input
,
1822 .vidioc_reqbufs
= vpif_reqbufs
,
1823 .vidioc_querybuf
= vpif_querybuf
,
1824 .vidioc_querystd
= vpif_querystd
,
1825 .vidioc_s_std
= vpif_s_std
,
1826 .vidioc_g_std
= vpif_g_std
,
1827 .vidioc_qbuf
= vpif_qbuf
,
1828 .vidioc_dqbuf
= vpif_dqbuf
,
1829 .vidioc_streamon
= vpif_streamon
,
1830 .vidioc_streamoff
= vpif_streamoff
,
1831 .vidioc_cropcap
= vpif_cropcap
,
1834 /* vpif file operations */
1835 static struct v4l2_file_operations vpif_fops
= {
1836 .owner
= THIS_MODULE
,
1838 .release
= vpif_release
,
1839 .ioctl
= video_ioctl2
,
1844 /* vpif video template */
1845 static struct video_device vpif_video_template
= {
1849 .ioctl_ops
= &vpif_ioctl_ops
,
1853 * initialize_vpif() - Initialize vpif data structures
1855 * Allocate memory for data structures and initialize them
1857 static int initialize_vpif(void)
1860 int free_channel_objects_index
;
1862 /* Default number of buffers should be 3 */
1863 if ((ch0_numbuffers
> 0) &&
1864 (ch0_numbuffers
< config_params
.min_numbuffers
))
1865 ch0_numbuffers
= config_params
.min_numbuffers
;
1866 if ((ch1_numbuffers
> 0) &&
1867 (ch1_numbuffers
< config_params
.min_numbuffers
))
1868 ch1_numbuffers
= config_params
.min_numbuffers
;
1870 /* Set buffer size to min buffers size if it is invalid */
1871 if (ch0_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
])
1873 config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
];
1874 if (ch1_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
])
1876 config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
];
1878 config_params
.numbuffers
[VPIF_CHANNEL0_VIDEO
] = ch0_numbuffers
;
1879 config_params
.numbuffers
[VPIF_CHANNEL1_VIDEO
] = ch1_numbuffers
;
1880 if (ch0_numbuffers
) {
1881 config_params
.channel_bufsize
[VPIF_CHANNEL0_VIDEO
]
1884 if (ch1_numbuffers
) {
1885 config_params
.channel_bufsize
[VPIF_CHANNEL1_VIDEO
]
1889 /* Allocate memory for six channel objects */
1890 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1892 kzalloc(sizeof(*vpif_obj
.dev
[i
]), GFP_KERNEL
);
1893 /* If memory allocation fails, return error */
1894 if (!vpif_obj
.dev
[i
]) {
1895 free_channel_objects_index
= i
;
1897 goto vpif_init_free_channel_objects
;
1902 vpif_init_free_channel_objects
:
1903 for (j
= 0; j
< free_channel_objects_index
; j
++)
1904 kfree(vpif_obj
.dev
[j
]);
1909 * vpif_probe : This function probes the vpif capture driver
1910 * @pdev: platform device pointer
1912 * This creates device entries by register itself to the V4L2 driver and
1913 * initializes fields of each channel objects
1915 static __init
int vpif_probe(struct platform_device
*pdev
)
1917 struct vpif_subdev_info
*subdevdata
;
1918 struct vpif_capture_config
*config
;
1919 int i
, j
, k
, m
, q
, err
;
1920 struct i2c_adapter
*i2c_adap
;
1921 struct channel_obj
*ch
;
1922 struct common_obj
*common
;
1923 struct video_device
*vfd
;
1924 struct resource
*res
;
1927 vpif_dev
= &pdev
->dev
;
1929 err
= initialize_vpif();
1931 v4l2_err(vpif_dev
->driver
, "Error initializing vpif\n");
1936 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, k
))) {
1937 for (i
= res
->start
; i
<= res
->end
; i
++) {
1938 if (request_irq(i
, vpif_channel_isr
, IRQF_DISABLED
,
1940 (void *)(&vpif_obj
.dev
[k
]->channel_id
))) {
1949 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1950 /* Get the pointer to the channel object */
1951 ch
= vpif_obj
.dev
[i
];
1952 /* Allocate memory for video device */
1953 vfd
= video_device_alloc();
1955 for (j
= 0; j
< i
; j
++) {
1956 ch
= vpif_obj
.dev
[j
];
1957 video_device_release(ch
->video_dev
);
1960 goto vpif_dev_alloc_err
;
1963 /* Initialize field of video device */
1964 *vfd
= vpif_video_template
;
1965 vfd
->v4l2_dev
= &vpif_obj
.v4l2_dev
;
1966 vfd
->release
= video_device_release
;
1967 snprintf(vfd
->name
, sizeof(vfd
->name
),
1968 "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
1969 (VPIF_CAPTURE_VERSION_CODE
>> 16) & 0xff,
1970 (VPIF_CAPTURE_VERSION_CODE
>> 8) & 0xff,
1971 (VPIF_CAPTURE_VERSION_CODE
) & 0xff);
1972 /* Set video_dev to the video device */
1973 ch
->video_dev
= vfd
;
1976 for (j
= 0; j
< VPIF_CAPTURE_MAX_DEVICES
; j
++) {
1977 ch
= vpif_obj
.dev
[j
];
1979 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
1980 spin_lock_init(&common
->irqlock
);
1981 mutex_init(&common
->lock
);
1982 /* Initialize prio member of channel object */
1983 v4l2_prio_init(&ch
->prio
);
1984 err
= video_register_device(ch
->video_dev
,
1985 VFL_TYPE_GRABBER
, (j
? 1 : 0));
1989 video_set_drvdata(ch
->video_dev
, ch
);
1993 i2c_adap
= i2c_get_adapter(1);
1994 config
= pdev
->dev
.platform_data
;
1996 subdev_count
= config
->subdev_count
;
1997 vpif_obj
.sd
= kzalloc(sizeof(struct v4l2_subdev
*) * subdev_count
,
1999 if (vpif_obj
.sd
== NULL
) {
2000 vpif_err("unable to allocate memory for subdevice pointers\n");
2005 err
= v4l2_device_register(vpif_dev
, &vpif_obj
.v4l2_dev
);
2007 v4l2_err(vpif_dev
->driver
, "Error registering v4l2 device\n");
2008 goto probe_subdev_out
;
2011 for (i
= 0; i
< subdev_count
; i
++) {
2012 subdevdata
= &config
->subdev_info
[i
];
2014 v4l2_i2c_new_subdev_board(&vpif_obj
.v4l2_dev
,
2016 &subdevdata
->board_info
,
2019 if (!vpif_obj
.sd
[i
]) {
2020 vpif_err("Error registering v4l2 subdevice\n");
2021 goto probe_subdev_out
;
2023 v4l2_info(&vpif_obj
.v4l2_dev
, "registered sub device %s\n",
2027 vpif_obj
.sd
[i
]->grp_id
= 1 << i
;
2029 v4l2_info(&vpif_obj
.v4l2_dev
, "DM646x VPIF Capture driver"
2035 /* free sub devices memory */
2038 j
= VPIF_CAPTURE_MAX_DEVICES
;
2040 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
2041 for (k
= 0; k
< j
; k
++) {
2042 /* Get the pointer to the channel object */
2043 ch
= vpif_obj
.dev
[k
];
2044 /* Unregister video device */
2045 video_unregister_device(ch
->video_dev
);
2049 k
= VPIF_CAPTURE_MAX_DEVICES
-1;
2050 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, k
);
2054 for (q
= k
; q
>= 0; q
--) {
2055 for (m
= i
; m
>= (int)res
->start
; m
--)
2056 free_irq(m
, (void *)(&vpif_obj
.dev
[q
]->channel_id
));
2058 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, q
-1);
2066 * vpif_remove() - driver remove handler
2067 * @device: ptr to platform device structure
2069 * The vidoe device is unregistered
2071 static int vpif_remove(struct platform_device
*device
)
2074 struct channel_obj
*ch
;
2076 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
2078 /* un-register device */
2079 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
2080 /* Get the pointer to the channel object */
2081 ch
= vpif_obj
.dev
[i
];
2082 /* Unregister video device */
2083 video_unregister_device(ch
->video_dev
);
2089 * vpif_suspend: vpif device suspend
2091 * TODO: Add suspend code here
2094 vpif_suspend(struct device
*dev
)
2100 * vpif_resume: vpif device suspend
2102 * TODO: Add resume code here
2105 vpif_resume(struct device
*dev
)
2110 static const struct dev_pm_ops vpif_dev_pm_ops
= {
2111 .suspend
= vpif_suspend
,
2112 .resume
= vpif_resume
,
2115 static __refdata
struct platform_driver vpif_driver
= {
2117 .name
= "vpif_capture",
2118 .owner
= THIS_MODULE
,
2119 .pm
= &vpif_dev_pm_ops
,
2121 .probe
= vpif_probe
,
2122 .remove
= vpif_remove
,
2126 * vpif_init: initialize the vpif driver
2128 * This function registers device and driver to the kernel, requests irq
2129 * handler and allocates memory
2130 * for channel objects
2132 static __init
int vpif_init(void)
2134 return platform_driver_register(&vpif_driver
);
2138 * vpif_cleanup : This function clean up the vpif capture resources
2140 * This will un-registers device and driver to the kernel, frees
2141 * requested irq handler and de-allocates memory allocated for channel
2144 static void vpif_cleanup(void)
2146 struct platform_device
*pdev
;
2147 struct resource
*res
;
2151 pdev
= container_of(vpif_dev
, struct platform_device
, dev
);
2152 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
))) {
2153 for (irq_num
= res
->start
; irq_num
<= res
->end
; irq_num
++)
2155 (void *)(&vpif_obj
.dev
[i
]->channel_id
));
2159 platform_driver_unregister(&vpif_driver
);
2162 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++)
2163 kfree(vpif_obj
.dev
[i
]);
2166 /* Function for module initialization and cleanup */
2167 module_init(vpif_init
);
2168 module_exit(vpif_cleanup
);