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 <media/v4l2-device.h>
38 #include <media/v4l2-ioctl.h>
40 #include "vpif_capture.h"
43 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
44 MODULE_LICENSE("GPL");
46 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
47 #define vpif_dbg(level, debug, fmt, arg...) \
48 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
51 static u32 ch0_numbuffers
= 3;
52 static u32 ch1_numbuffers
= 3;
53 static u32 ch0_bufsize
= 1920 * 1080 * 2;
54 static u32 ch1_bufsize
= 720 * 576 * 2;
56 module_param(debug
, int, 0644);
57 module_param(ch0_numbuffers
, uint
, S_IRUGO
);
58 module_param(ch1_numbuffers
, uint
, S_IRUGO
);
59 module_param(ch0_bufsize
, uint
, S_IRUGO
);
60 module_param(ch1_bufsize
, uint
, S_IRUGO
);
62 MODULE_PARM_DESC(debug
, "Debug level 0-1");
63 MODULE_PARM_DESC(ch2_numbuffers
, "Channel0 buffer count (default:3)");
64 MODULE_PARM_DESC(ch3_numbuffers
, "Channel1 buffer count (default:3)");
65 MODULE_PARM_DESC(ch2_bufsize
, "Channel0 buffer size (default:1920 x 1080 x 2)");
66 MODULE_PARM_DESC(ch3_bufsize
, "Channel1 buffer size (default:720 x 576 x 2)");
68 static struct vpif_config_params config_params
= {
72 .min_bufsize
[0] = 720 * 480 * 2,
73 .min_bufsize
[1] = 720 * 480 * 2,
74 .channel_bufsize
[0] = 1920 * 1080 * 2,
75 .channel_bufsize
[1] = 720 * 576 * 2,
78 /* global variables */
79 static struct vpif_device vpif_obj
= { {NULL
} };
80 static struct device
*vpif_dev
;
83 * ch_params: video standard configuration parameters for vpif
85 static const struct vpif_channel_config_params ch_params
[] = {
87 "NTSC_M", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
88 286, 525, 525, 0, 1, 0, V4L2_STD_525_60
,
91 "PAL_BDGHIK", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
92 336, 624, 625, 0, 1, 0, V4L2_STD_625_50
,
97 * vpif_uservirt_to_phys : translate user/virtual address to phy address
98 * @virtp: user/virtual address
100 * This inline function is used to convert user space virtual address to
103 static inline u32
vpif_uservirt_to_phys(u32 virtp
)
105 unsigned long physp
= 0;
106 struct mm_struct
*mm
= current
->mm
;
107 struct vm_area_struct
*vma
;
109 vma
= find_vma(mm
, virtp
);
111 /* For kernel direct-mapped memory, take the easy way */
112 if (virtp
>= PAGE_OFFSET
)
113 physp
= virt_to_phys((void *)virtp
);
114 else if (vma
&& (vma
->vm_flags
& VM_IO
) && (vma
->vm_pgoff
))
116 * this will catch, kernel-allocated, mmaped-to-usermode
119 physp
= (vma
->vm_pgoff
<< PAGE_SHIFT
) + (virtp
- vma
->vm_start
);
121 /* otherwise, use get_user_pages() for general userland pages */
122 int res
, nr_pages
= 1;
125 down_read(¤t
->mm
->mmap_sem
);
127 res
= get_user_pages(current
, current
->mm
,
128 virtp
, nr_pages
, 1, 0, &pages
, NULL
);
129 up_read(¤t
->mm
->mmap_sem
);
132 physp
= __pa(page_address(&pages
[0]) +
133 (virtp
& ~PAGE_MASK
));
135 vpif_err("get_user_pages failed\n");
143 * buffer_prepare : callback function for buffer prepare
144 * @q : buffer queue ptr
145 * @vb: ptr to video buffer
148 * This is the callback function for buffer prepare when videobuf_qbuf()
149 * function is called. The buffer is prepared and user space virtual address
150 * or user address is converted into physical address
152 static int vpif_buffer_prepare(struct videobuf_queue
*q
,
153 struct videobuf_buffer
*vb
,
154 enum v4l2_field field
)
156 /* Get the file handle object and channel object */
157 struct vpif_fh
*fh
= q
->priv_data
;
158 struct channel_obj
*ch
= fh
->channel
;
159 struct common_obj
*common
;
163 vpif_dbg(2, debug
, "vpif_buffer_prepare\n");
165 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
167 /* If buffer is not initialized, initialize it */
168 if (VIDEOBUF_NEEDS_INIT
== vb
->state
) {
169 vb
->width
= common
->width
;
170 vb
->height
= common
->height
;
171 vb
->size
= vb
->width
* vb
->height
;
174 vb
->state
= VIDEOBUF_PREPARED
;
176 * if user pointer memory mechanism is used, get the physical
177 * address of the buffer
179 if (V4L2_MEMORY_USERPTR
== common
->memory
) {
180 if (0 == vb
->baddr
) {
181 vpif_dbg(1, debug
, "buffer address is 0\n");
185 vb
->boff
= vpif_uservirt_to_phys(vb
->baddr
);
186 if (!IS_ALIGNED(vb
->boff
, 8))
192 if (!IS_ALIGNED((addr
+ common
->ytop_off
), 8) ||
193 !IS_ALIGNED((addr
+ common
->ybtm_off
), 8) ||
194 !IS_ALIGNED((addr
+ common
->ctop_off
), 8) ||
195 !IS_ALIGNED((addr
+ common
->cbtm_off
), 8))
200 vpif_dbg(1, debug
, "buffer_prepare:offset is not aligned to 8 bytes\n");
205 * vpif_buffer_setup : Callback function for buffer setup.
206 * @q: buffer queue ptr
207 * @count: number of buffers
208 * @size: size of the buffer
210 * This callback function is called when reqbuf() is called to adjust
211 * the buffer count and buffer size
213 static int vpif_buffer_setup(struct videobuf_queue
*q
, unsigned int *count
,
216 /* Get the file handle object and channel object */
217 struct vpif_fh
*fh
= q
->priv_data
;
218 struct channel_obj
*ch
= fh
->channel
;
219 struct common_obj
*common
;
221 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
223 vpif_dbg(2, debug
, "vpif_buffer_setup\n");
225 /* If memory type is not mmap, return */
226 if (V4L2_MEMORY_MMAP
!= common
->memory
)
229 /* Calculate the size of the buffer */
230 *size
= config_params
.channel_bufsize
[ch
->channel_id
];
232 if (*count
< config_params
.min_numbuffers
)
233 *count
= config_params
.min_numbuffers
;
238 * vpif_buffer_queue : Callback function to add buffer to DMA queue
239 * @q: ptr to videobuf_queue
240 * @vb: ptr to videobuf_buffer
242 static void vpif_buffer_queue(struct videobuf_queue
*q
,
243 struct videobuf_buffer
*vb
)
245 /* Get the file handle object and channel object */
246 struct vpif_fh
*fh
= q
->priv_data
;
247 struct channel_obj
*ch
= fh
->channel
;
248 struct common_obj
*common
;
250 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
252 vpif_dbg(2, debug
, "vpif_buffer_queue\n");
254 /* add the buffer to the DMA queue */
255 list_add_tail(&vb
->queue
, &common
->dma_queue
);
256 /* Change state of the buffer */
257 vb
->state
= VIDEOBUF_QUEUED
;
261 * vpif_buffer_release : Callback function to free buffer
262 * @q: buffer queue ptr
263 * @vb: ptr to video buffer
265 * This function is called from the videobuf layer to free memory
266 * allocated to the buffers
268 static void vpif_buffer_release(struct videobuf_queue
*q
,
269 struct videobuf_buffer
*vb
)
271 /* Get the file handle object and channel object */
272 struct vpif_fh
*fh
= q
->priv_data
;
273 struct channel_obj
*ch
= fh
->channel
;
274 struct common_obj
*common
;
276 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
278 videobuf_dma_contig_free(q
, vb
);
279 vb
->state
= VIDEOBUF_NEEDS_INIT
;
282 static struct videobuf_queue_ops video_qops
= {
283 .buf_setup
= vpif_buffer_setup
,
284 .buf_prepare
= vpif_buffer_prepare
,
285 .buf_queue
= vpif_buffer_queue
,
286 .buf_release
= vpif_buffer_release
,
289 static u8 channel_first_int
[VPIF_NUMBER_OF_OBJECTS
][2] =
293 * vpif_process_buffer_complete: process a completed buffer
294 * @common: ptr to common channel object
296 * This function time stamp the buffer and mark it as DONE. It also
297 * wake up any process waiting on the QUEUE and set the next buffer
300 static void vpif_process_buffer_complete(struct common_obj
*common
)
302 do_gettimeofday(&common
->cur_frm
->ts
);
303 common
->cur_frm
->state
= VIDEOBUF_DONE
;
304 wake_up_interruptible(&common
->cur_frm
->done
);
305 /* Make curFrm pointing to nextFrm */
306 common
->cur_frm
= common
->next_frm
;
310 * vpif_schedule_next_buffer: set next buffer address for capture
311 * @common : ptr to common channel object
313 * This function will get next buffer from the dma queue and
314 * set the buffer address in the vpif register for capture.
315 * the buffer is marked active
317 static void vpif_schedule_next_buffer(struct common_obj
*common
)
319 unsigned long addr
= 0;
321 common
->next_frm
= list_entry(common
->dma_queue
.next
,
322 struct videobuf_buffer
, queue
);
323 /* Remove that buffer from the buffer queue */
324 list_del(&common
->next_frm
->queue
);
325 common
->next_frm
->state
= VIDEOBUF_ACTIVE
;
326 if (V4L2_MEMORY_USERPTR
== common
->memory
)
327 addr
= common
->next_frm
->boff
;
329 addr
= videobuf_to_dma_contig(common
->next_frm
);
331 /* Set top and bottom field addresses in VPIF registers */
332 common
->set_addr(addr
+ common
->ytop_off
,
333 addr
+ common
->ybtm_off
,
334 addr
+ common
->ctop_off
,
335 addr
+ common
->cbtm_off
);
339 * vpif_channel_isr : ISR handler for vpif capture
341 * @dev_id: dev_id ptr
343 * It changes status of the captured buffer, takes next buffer from the queue
344 * and sets its address in VPIF registers
346 static irqreturn_t
vpif_channel_isr(int irq
, void *dev_id
)
348 struct vpif_device
*dev
= &vpif_obj
;
349 struct common_obj
*common
;
350 struct channel_obj
*ch
;
351 enum v4l2_field field
;
355 channel_id
= *(int *)(dev_id
);
356 ch
= dev
->dev
[channel_id
];
358 field
= ch
->common
[VPIF_VIDEO_INDEX
].fmt
.fmt
.pix
.field
;
360 for (i
= 0; i
< VPIF_NUMBER_OF_OBJECTS
; i
++) {
361 common
= &ch
->common
[i
];
362 /* skip If streaming is not started in this channel */
363 if (0 == common
->started
)
366 /* Check the field format */
367 if (1 == ch
->vpifparams
.std_info
.frm_fmt
) {
368 /* Progressive mode */
369 if (list_empty(&common
->dma_queue
))
372 if (!channel_first_int
[i
][channel_id
])
373 vpif_process_buffer_complete(common
);
375 channel_first_int
[i
][channel_id
] = 0;
377 vpif_schedule_next_buffer(common
);
380 channel_first_int
[i
][channel_id
] = 0;
383 * Interlaced mode. If it is first interrupt, ignore
386 if (channel_first_int
[i
][channel_id
]) {
387 channel_first_int
[i
][channel_id
] = 0;
392 /* Get field id from VPIF registers */
393 fid
= vpif_channel_getfid(ch
->channel_id
);
394 if (fid
!= ch
->field_id
) {
396 * If field id does not match stored
397 * field id, make them in sync
404 /* device field id and local field id are in sync */
406 /* this is even field */
407 if (common
->cur_frm
== common
->next_frm
)
410 /* mark the current buffer as done */
411 vpif_process_buffer_complete(common
);
412 } else if (1 == fid
) {
414 if (list_empty(&common
->dma_queue
) ||
415 (common
->cur_frm
!= common
->next_frm
))
418 vpif_schedule_next_buffer(common
);
426 * vpif_update_std_info() - update standard related info
427 * @ch: ptr to channel object
429 * For a given standard selected by application, update values
430 * in the device data structures
432 static int vpif_update_std_info(struct channel_obj
*ch
)
434 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
435 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
436 const struct vpif_channel_config_params
*config
;
437 struct vpif_channel_config_params
*std_info
;
438 struct video_obj
*vid_ch
= &ch
->video
;
441 vpif_dbg(2, debug
, "vpif_update_std_info\n");
443 std_info
= &vpifparams
->std_info
;
445 for (index
= 0; index
< ARRAY_SIZE(ch_params
); index
++) {
446 config
= &ch_params
[index
];
447 if (config
->stdid
& vid_ch
->stdid
) {
448 memcpy(std_info
, config
, sizeof(*config
));
453 /* standard not found */
454 if (index
== ARRAY_SIZE(ch_params
))
457 common
->fmt
.fmt
.pix
.width
= std_info
->width
;
458 common
->width
= std_info
->width
;
459 common
->fmt
.fmt
.pix
.height
= std_info
->height
;
460 common
->height
= std_info
->height
;
461 common
->fmt
.fmt
.pix
.bytesperline
= std_info
->width
;
462 vpifparams
->video_params
.hpitch
= std_info
->width
;
463 vpifparams
->video_params
.storage_mode
= std_info
->frm_fmt
;
468 * vpif_calculate_offsets : This function calculates buffers offsets
469 * @ch : ptr to channel object
471 * This function calculates buffer offsets for Y and C in the top and
474 static void vpif_calculate_offsets(struct channel_obj
*ch
)
476 unsigned int hpitch
, vpitch
, sizeimage
;
477 struct video_obj
*vid_ch
= &(ch
->video
);
478 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
479 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
480 enum v4l2_field field
= common
->fmt
.fmt
.pix
.field
;
482 vpif_dbg(2, debug
, "vpif_calculate_offsets\n");
484 if (V4L2_FIELD_ANY
== field
) {
485 if (vpifparams
->std_info
.frm_fmt
)
486 vid_ch
->buf_field
= V4L2_FIELD_NONE
;
488 vid_ch
->buf_field
= V4L2_FIELD_INTERLACED
;
490 vid_ch
->buf_field
= common
->fmt
.fmt
.pix
.field
;
492 if (V4L2_MEMORY_USERPTR
== common
->memory
)
493 sizeimage
= common
->fmt
.fmt
.pix
.sizeimage
;
495 sizeimage
= config_params
.channel_bufsize
[ch
->channel_id
];
497 hpitch
= common
->fmt
.fmt
.pix
.bytesperline
;
498 vpitch
= sizeimage
/ (hpitch
* 2);
500 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
501 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
)) {
502 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
503 common
->ytop_off
= 0;
504 common
->ybtm_off
= hpitch
;
505 common
->ctop_off
= sizeimage
/ 2;
506 common
->cbtm_off
= sizeimage
/ 2 + hpitch
;
507 } else if (V4L2_FIELD_SEQ_TB
== vid_ch
->buf_field
) {
508 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
509 common
->ytop_off
= 0;
510 common
->ybtm_off
= sizeimage
/ 4;
511 common
->ctop_off
= sizeimage
/ 2;
512 common
->cbtm_off
= common
->ctop_off
+ sizeimage
/ 4;
513 } else if (V4L2_FIELD_SEQ_BT
== vid_ch
->buf_field
) {
514 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
515 common
->ybtm_off
= 0;
516 common
->ytop_off
= sizeimage
/ 4;
517 common
->cbtm_off
= sizeimage
/ 2;
518 common
->ctop_off
= common
->cbtm_off
+ sizeimage
/ 4;
520 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
521 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
))
522 vpifparams
->video_params
.storage_mode
= 1;
524 vpifparams
->video_params
.storage_mode
= 0;
526 if (1 == vpifparams
->std_info
.frm_fmt
)
527 vpifparams
->video_params
.hpitch
=
528 common
->fmt
.fmt
.pix
.bytesperline
;
530 if ((field
== V4L2_FIELD_ANY
)
531 || (field
== V4L2_FIELD_INTERLACED
))
532 vpifparams
->video_params
.hpitch
=
533 common
->fmt
.fmt
.pix
.bytesperline
* 2;
535 vpifparams
->video_params
.hpitch
=
536 common
->fmt
.fmt
.pix
.bytesperline
;
539 ch
->vpifparams
.video_params
.stdid
= vpifparams
->std_info
.stdid
;
543 * vpif_config_format: configure default frame format in the device
544 * ch : ptr to channel object
546 static void vpif_config_format(struct channel_obj
*ch
)
548 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
550 vpif_dbg(2, debug
, "vpif_config_format\n");
552 common
->fmt
.fmt
.pix
.field
= V4L2_FIELD_ANY
;
553 if (config_params
.numbuffers
[ch
->channel_id
] == 0)
554 common
->memory
= V4L2_MEMORY_USERPTR
;
556 common
->memory
= V4L2_MEMORY_MMAP
;
558 common
->fmt
.fmt
.pix
.sizeimage
559 = config_params
.channel_bufsize
[ch
->channel_id
];
561 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
)
562 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
564 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUV422P
;
565 common
->fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
569 * vpif_get_default_field() - Get default field type based on interface
570 * @vpif_params - ptr to vpif params
572 static inline enum v4l2_field
vpif_get_default_field(
573 struct vpif_interface
*iface
)
575 return (iface
->if_type
== VPIF_IF_RAW_BAYER
) ? V4L2_FIELD_NONE
:
576 V4L2_FIELD_INTERLACED
;
580 * vpif_check_format() - check given pixel format for compatibility
582 * @pixfmt - Given pixel format
583 * @update - update the values as per hardware requirement
585 * Check the application pixel format for S_FMT and update the input
586 * values as per hardware limits for TRY_FMT. The default pixel and
587 * field format is selected based on interface type.
589 static int vpif_check_format(struct channel_obj
*ch
,
590 struct v4l2_pix_format
*pixfmt
,
593 struct common_obj
*common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
594 struct vpif_params
*vpif_params
= &ch
->vpifparams
;
595 enum v4l2_field field
= pixfmt
->field
;
596 u32 sizeimage
, hpitch
, vpitch
;
599 vpif_dbg(2, debug
, "vpif_check_format\n");
601 * first check for the pixel format. If if_type is Raw bayer,
602 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
603 * V4L2_PIX_FMT_YUV422P is supported
605 if (vpif_params
->iface
.if_type
== VPIF_IF_RAW_BAYER
) {
606 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
) {
608 vpif_dbg(2, debug
, "invalid pix format\n");
611 pixfmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
614 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_YUV422P
) {
616 vpif_dbg(2, debug
, "invalid pixel format\n");
619 pixfmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
623 if (!(VPIF_VALID_FIELD(field
))) {
625 vpif_dbg(2, debug
, "invalid field format\n");
629 * By default use FIELD_NONE for RAW Bayer capture
630 * and FIELD_INTERLACED for other interfaces
632 field
= vpif_get_default_field(&vpif_params
->iface
);
633 } else if (field
== V4L2_FIELD_ANY
)
634 /* unsupported field. Use default */
635 field
= vpif_get_default_field(&vpif_params
->iface
);
637 /* validate the hpitch */
638 hpitch
= pixfmt
->bytesperline
;
639 if (hpitch
< vpif_params
->std_info
.width
) {
641 vpif_dbg(2, debug
, "invalid hpitch\n");
644 hpitch
= vpif_params
->std_info
.width
;
647 if (V4L2_MEMORY_USERPTR
== common
->memory
)
648 sizeimage
= pixfmt
->sizeimage
;
650 sizeimage
= config_params
.channel_bufsize
[ch
->channel_id
];
652 vpitch
= sizeimage
/ (hpitch
* 2);
654 /* validate the vpitch */
655 if (vpitch
< vpif_params
->std_info
.height
) {
657 vpif_dbg(2, debug
, "Invalid vpitch\n");
660 vpitch
= vpif_params
->std_info
.height
;
663 /* Check for 8 byte alignment */
664 if (!ALIGN(hpitch
, 8)) {
666 vpif_dbg(2, debug
, "invalid pitch alignment\n");
669 /* adjust to next 8 byte boundary */
670 hpitch
= (((hpitch
+ 7) / 8) * 8);
672 /* if update is set, modify the bytesperline and sizeimage */
674 pixfmt
->bytesperline
= hpitch
;
675 pixfmt
->sizeimage
= hpitch
* vpitch
* 2;
678 * Image width and height is always based on current standard width and
681 pixfmt
->width
= common
->fmt
.fmt
.pix
.width
;
682 pixfmt
->height
= common
->fmt
.fmt
.pix
.height
;
689 * vpif_config_addr() - function to configure buffer address in vpif
691 * @muxmode - channel mux mode
693 static void vpif_config_addr(struct channel_obj
*ch
, int muxmode
)
695 struct common_obj
*common
;
697 vpif_dbg(2, debug
, "vpif_config_addr\n");
699 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
701 if (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)
702 common
->set_addr
= ch1_set_videobuf_addr
;
703 else if (2 == muxmode
)
704 common
->set_addr
= ch0_set_videobuf_addr_yc_nmux
;
706 common
->set_addr
= ch0_set_videobuf_addr
;
710 * vpfe_mmap : It is used to map kernel space buffers into user spaces
711 * @filep: file pointer
712 * @vma: ptr to vm_area_struct
714 static int vpif_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
716 /* Get the channel object and file handle object */
717 struct vpif_fh
*fh
= filep
->private_data
;
718 struct channel_obj
*ch
= fh
->channel
;
719 struct common_obj
*common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
721 vpif_dbg(2, debug
, "vpif_mmap\n");
723 return videobuf_mmap_mapper(&common
->buffer_queue
, vma
);
727 * vpif_poll: It is used for select/poll system call
728 * @filep: file pointer
729 * @wait: poll table to wait
731 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 err
= videobuf_poll_stream(filep
, &common
->buffer_queue
, wait
);
747 * vpif_open : vpif open handler
750 * It creates object of file handle structure and stores it in private_data
751 * member of filepointer
753 static int vpif_open(struct file
*filep
)
755 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
756 struct video_device
*vdev
= video_devdata(filep
);
757 struct common_obj
*common
;
758 struct video_obj
*vid_ch
;
759 struct channel_obj
*ch
;
763 vpif_dbg(2, debug
, "vpif_open\n");
765 ch
= video_get_drvdata(vdev
);
768 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
770 if (mutex_lock_interruptible(&common
->lock
))
773 if (NULL
== ch
->curr_subdev_info
) {
775 * search through the sub device to see a registered
776 * sub device and make it as current sub device
778 for (i
= 0; i
< config
->subdev_count
; i
++) {
779 if (vpif_obj
.sd
[i
]) {
780 /* the sub device is registered */
781 ch
->curr_subdev_info
= &config
->subdev_info
[i
];
782 /* make first input as the current input */
783 vid_ch
->input_idx
= 0;
787 if (i
== config
->subdev_count
) {
788 vpif_err("No sub device registered\n");
794 /* Allocate memory for the file handle object */
795 fh
= kmalloc(sizeof(struct vpif_fh
), GFP_KERNEL
);
797 vpif_err("unable to allocate memory for file handle object\n");
802 /* store pointer to fh in private_data member of filep */
803 filep
->private_data
= fh
;
806 /* If decoder is not initialized. initialize it */
807 if (!ch
->initialized
) {
810 memset(&(ch
->vpifparams
), 0, sizeof(struct vpif_params
));
812 /* Increment channel usrs counter */
814 /* Set io_allowed member to false */
815 fh
->io_allowed
[VPIF_VIDEO_INDEX
] = 0;
816 /* Initialize priority of this instance to default priority */
817 fh
->prio
= V4L2_PRIORITY_UNSET
;
818 v4l2_prio_open(&ch
->prio
, &fh
->prio
);
820 mutex_unlock(&common
->lock
);
825 * vpif_release : function to clean up file close
826 * @filep: file pointer
828 * This function deletes buffer queue, frees the buffers and the vpfe file
831 static int vpif_release(struct file
*filep
)
833 struct vpif_fh
*fh
= filep
->private_data
;
834 struct channel_obj
*ch
= fh
->channel
;
835 struct common_obj
*common
;
837 vpif_dbg(2, debug
, "vpif_release\n");
839 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
841 if (mutex_lock_interruptible(&common
->lock
))
844 /* if this instance is doing IO */
845 if (fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
846 /* Reset io_usrs member of channel object */
848 /* Disable channel as per its device type and channel id */
849 if (VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) {
851 channel0_intr_enable(0);
853 if ((VPIF_CHANNEL1_VIDEO
== ch
->channel_id
) ||
854 (2 == common
->started
)) {
856 channel1_intr_enable(0);
859 /* Free buffers allocated */
860 videobuf_queue_cancel(&common
->buffer_queue
);
861 videobuf_mmap_free(&common
->buffer_queue
);
864 /* Decrement channel usrs counter */
867 /* unlock mutex on channel object */
868 mutex_unlock(&common
->lock
);
870 /* Close the priority */
871 v4l2_prio_close(&ch
->prio
, &fh
->prio
);
876 filep
->private_data
= NULL
;
882 * vpif_reqbufs() - request buffer handler
885 * @reqbuf: request buffer structure ptr
887 static int vpif_reqbufs(struct file
*file
, void *priv
,
888 struct v4l2_requestbuffers
*reqbuf
)
890 struct vpif_fh
*fh
= priv
;
891 struct channel_obj
*ch
= fh
->channel
;
892 struct common_obj
*common
;
896 vpif_dbg(2, debug
, "vpif_reqbufs\n");
899 * This file handle has not initialized the channel,
900 * It is not allowed to do settings
902 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
)
903 || (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
904 if (!fh
->initialized
) {
905 vpif_dbg(1, debug
, "Channel Busy\n");
910 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= reqbuf
->type
)
913 index
= VPIF_VIDEO_INDEX
;
915 common
= &ch
->common
[index
];
917 if (mutex_lock_interruptible(&common
->lock
))
920 if (0 != common
->io_usrs
) {
925 /* Initialize videobuf queue as per the buffer type */
926 videobuf_queue_dma_contig_init(&common
->buffer_queue
,
930 common
->fmt
.fmt
.pix
.field
,
931 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
;
1041 local_irq_save(flags
);
1042 ret
= vpif_buffer_prepare(&common
->buffer_queue
, buf1
,
1043 common
->buffer_queue
.field
);
1045 local_irq_restore(flags
);
1049 buf1
->state
= VIDEOBUF_ACTIVE
;
1051 if (V4L2_MEMORY_USERPTR
== common
->memory
)
1054 addr
= videobuf_to_dma_contig(buf1
);
1056 common
->next_frm
= buf1
;
1057 common
->set_addr(addr
+ common
->ytop_off
,
1058 addr
+ common
->ybtm_off
,
1059 addr
+ common
->ctop_off
,
1060 addr
+ common
->cbtm_off
);
1062 local_irq_restore(flags
);
1063 list_add_tail(&buf1
->stream
, &common
->buffer_queue
.stream
);
1064 mutex_unlock(&common
->buffer_queue
.vb_lock
);
1068 mutex_unlock(&common
->buffer_queue
.vb_lock
);
1073 * vpif_dqbuf() - query buffer handler
1075 * @priv: file handle
1076 * @buf: v4l2 buffer structure ptr
1078 static int vpif_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
1080 struct vpif_fh
*fh
= priv
;
1081 struct channel_obj
*ch
= fh
->channel
;
1082 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1084 vpif_dbg(2, debug
, "vpif_dqbuf\n");
1086 return videobuf_dqbuf(&common
->buffer_queue
, buf
,
1087 file
->f_flags
& O_NONBLOCK
);
1091 * vpif_streamon() - streamon handler
1093 * @priv: file handle
1094 * @buftype: v4l2 buffer type
1096 static int vpif_streamon(struct file
*file
, void *priv
,
1097 enum v4l2_buf_type buftype
)
1100 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1101 struct vpif_fh
*fh
= priv
;
1102 struct channel_obj
*ch
= fh
->channel
;
1103 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1104 struct channel_obj
*oth_ch
= vpif_obj
.dev
[!ch
->channel_id
];
1105 struct vpif_params
*vpif
;
1106 unsigned long addr
= 0;
1109 vpif_dbg(2, debug
, "vpif_streamon\n");
1111 vpif
= &ch
->vpifparams
;
1113 if (buftype
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1114 vpif_dbg(1, debug
, "buffer type not supported\n");
1118 /* If file handle is not allowed IO, return error */
1119 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1120 vpif_dbg(1, debug
, "io not allowed\n");
1124 /* If Streaming is already started, return error */
1125 if (common
->started
) {
1126 vpif_dbg(1, debug
, "channel->started\n");
1130 if ((ch
->channel_id
== VPIF_CHANNEL0_VIDEO
&&
1131 oth_ch
->common
[VPIF_VIDEO_INDEX
].started
&&
1132 vpif
->std_info
.ycmux_mode
== 0) ||
1133 ((ch
->channel_id
== VPIF_CHANNEL1_VIDEO
) &&
1134 (2 == oth_ch
->common
[VPIF_VIDEO_INDEX
].started
))) {
1135 vpif_dbg(1, debug
, "other channel is being used\n");
1139 ret
= vpif_check_format(ch
, &common
->fmt
.fmt
.pix
, 0);
1143 /* Enable streamon on the sub device */
1144 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1147 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
1148 vpif_dbg(1, debug
, "stream on failed in subdev\n");
1152 /* Call videobuf_streamon to start streaming in videobuf */
1153 ret
= videobuf_streamon(&common
->buffer_queue
);
1155 vpif_dbg(1, debug
, "videobuf_streamon\n");
1159 if (mutex_lock_interruptible(&common
->lock
)) {
1161 goto streamoff_exit
;
1164 /* If buffer queue is empty, return error */
1165 if (list_empty(&common
->dma_queue
)) {
1166 vpif_dbg(1, debug
, "buffer queue is empty\n");
1171 /* Get the next frame from the buffer queue */
1172 common
->cur_frm
= list_entry(common
->dma_queue
.next
,
1173 struct videobuf_buffer
, queue
);
1174 common
->next_frm
= common
->cur_frm
;
1176 /* Remove buffer from the buffer queue */
1177 list_del(&common
->cur_frm
->queue
);
1178 /* Mark state of the current frame to active */
1179 common
->cur_frm
->state
= VIDEOBUF_ACTIVE
;
1180 /* Initialize field_id and started member */
1182 common
->started
= 1;
1184 if (V4L2_MEMORY_USERPTR
== common
->memory
)
1185 addr
= common
->cur_frm
->boff
;
1187 addr
= videobuf_to_dma_contig(common
->cur_frm
);
1189 /* Calculate the offset for Y and C data in the buffer */
1190 vpif_calculate_offsets(ch
);
1192 if ((vpif
->std_info
.frm_fmt
&&
1193 ((common
->fmt
.fmt
.pix
.field
!= V4L2_FIELD_NONE
) &&
1194 (common
->fmt
.fmt
.pix
.field
!= V4L2_FIELD_ANY
))) ||
1195 (!vpif
->std_info
.frm_fmt
&&
1196 (common
->fmt
.fmt
.pix
.field
== V4L2_FIELD_NONE
))) {
1197 vpif_dbg(1, debug
, "conflict in field format and std format\n");
1202 /* configure 1 or 2 channel mode */
1203 ret
= config
->setup_input_channel_mode(vpif
->std_info
.ycmux_mode
);
1206 vpif_dbg(1, debug
, "can't set vpif channel mode\n");
1210 /* Call vpif_set_params function to set the parameters and addresses */
1211 ret
= vpif_set_video_params(vpif
, ch
->channel_id
);
1214 vpif_dbg(1, debug
, "can't set video params\n");
1218 common
->started
= ret
;
1219 vpif_config_addr(ch
, ret
);
1221 common
->set_addr(addr
+ common
->ytop_off
,
1222 addr
+ common
->ybtm_off
,
1223 addr
+ common
->ctop_off
,
1224 addr
+ common
->cbtm_off
);
1227 * Set interrupt for both the fields in VPIF Register enable channel in
1230 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
)) {
1231 channel0_intr_assert();
1232 channel0_intr_enable(1);
1235 if ((VPIF_CHANNEL1_VIDEO
== ch
->channel_id
) ||
1236 (common
->started
== 2)) {
1237 channel1_intr_assert();
1238 channel1_intr_enable(1);
1241 channel_first_int
[VPIF_VIDEO_INDEX
][ch
->channel_id
] = 1;
1242 mutex_unlock(&common
->lock
);
1246 mutex_unlock(&common
->lock
);
1248 ret
= videobuf_streamoff(&common
->buffer_queue
);
1253 * vpif_streamoff() - streamoff handler
1255 * @priv: file handle
1256 * @buftype: v4l2 buffer type
1258 static int vpif_streamoff(struct file
*file
, void *priv
,
1259 enum v4l2_buf_type buftype
)
1262 struct vpif_fh
*fh
= priv
;
1263 struct channel_obj
*ch
= fh
->channel
;
1264 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1267 vpif_dbg(2, debug
, "vpif_streamoff\n");
1269 if (buftype
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1270 vpif_dbg(1, debug
, "buffer type not supported\n");
1274 /* If io is allowed for this file handle, return error */
1275 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1276 vpif_dbg(1, debug
, "io not allowed\n");
1280 /* If streaming is not started, return error */
1281 if (!common
->started
) {
1282 vpif_dbg(1, debug
, "channel->started\n");
1286 if (mutex_lock_interruptible(&common
->lock
))
1287 return -ERESTARTSYS
;
1289 /* disable channel */
1290 if (VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) {
1292 channel0_intr_enable(0);
1295 channel1_intr_enable(0);
1298 common
->started
= 0;
1300 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1303 if (ret
&& (ret
!= -ENOIOCTLCMD
))
1304 vpif_dbg(1, debug
, "stream off failed in subdev\n");
1306 mutex_unlock(&common
->lock
);
1308 return videobuf_streamoff(&common
->buffer_queue
);
1312 * vpif_map_sub_device_to_input() - Maps sub device to input
1313 * @ch - ptr to channel
1314 * @config - ptr to capture configuration
1315 * @input_index - Given input index from application
1316 * @sub_device_index - index into sd table
1318 * lookup the sub device information for a given input index.
1319 * we report all the inputs to application. inputs table also
1320 * has sub device name for the each input
1322 static struct vpif_subdev_info
*vpif_map_sub_device_to_input(
1323 struct channel_obj
*ch
,
1324 struct vpif_capture_config
*vpif_cfg
,
1326 int *sub_device_index
)
1328 struct vpif_capture_chan_config
*chan_cfg
;
1329 struct vpif_subdev_info
*subdev_info
= NULL
;
1330 const char *subdev_name
= NULL
;
1333 vpif_dbg(2, debug
, "vpif_map_sub_device_to_input\n");
1335 chan_cfg
= &vpif_cfg
->chan_config
[ch
->channel_id
];
1338 * search through the inputs to find the sub device supporting
1341 for (i
= 0; i
< chan_cfg
->input_count
; i
++) {
1342 /* For each sub device, loop through input */
1343 if (i
== input_index
) {
1344 subdev_name
= chan_cfg
->inputs
[i
].subdev_name
;
1349 /* if reached maximum. return null */
1350 if (i
== chan_cfg
->input_count
|| (NULL
== subdev_name
))
1353 /* loop through the sub device list to get the sub device info */
1354 for (i
= 0; i
< vpif_cfg
->subdev_count
; i
++) {
1355 subdev_info
= &vpif_cfg
->subdev_info
[i
];
1356 if (!strcmp(subdev_info
->name
, subdev_name
))
1360 if (i
== vpif_cfg
->subdev_count
)
1363 /* check if the sub device is registered */
1364 if (NULL
== vpif_obj
.sd
[i
])
1367 *sub_device_index
= i
;
1372 * vpif_querystd() - querystd handler
1374 * @priv: file handle
1375 * @std_id: ptr to std id
1377 * This function is called to detect standard at the selected input
1379 static int vpif_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1381 struct vpif_fh
*fh
= priv
;
1382 struct channel_obj
*ch
= fh
->channel
;
1383 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1386 vpif_dbg(2, debug
, "vpif_querystd\n");
1388 if (mutex_lock_interruptible(&common
->lock
))
1389 return -ERESTARTSYS
;
1391 /* Call querystd function of decoder device */
1392 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1395 vpif_dbg(1, debug
, "Failed to set standard for sub devices\n");
1397 mutex_unlock(&common
->lock
);
1402 * vpif_g_std() - get STD handler
1404 * @priv: file handle
1405 * @std_id: ptr to std id
1407 static int vpif_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
1409 struct vpif_fh
*fh
= priv
;
1410 struct channel_obj
*ch
= fh
->channel
;
1412 vpif_dbg(2, debug
, "vpif_g_std\n");
1414 *std
= ch
->video
.stdid
;
1419 * vpif_s_std() - set STD handler
1421 * @priv: file handle
1422 * @std_id: ptr to std id
1424 static int vpif_s_std(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1426 struct vpif_fh
*fh
= priv
;
1427 struct channel_obj
*ch
= fh
->channel
;
1428 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1431 vpif_dbg(2, debug
, "vpif_s_std\n");
1433 if (common
->started
) {
1434 vpif_err("streaming in progress\n");
1438 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1439 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1440 if (!fh
->initialized
) {
1441 vpif_dbg(1, debug
, "Channel Busy\n");
1446 ret
= v4l2_prio_check(&ch
->prio
, &fh
->prio
);
1450 fh
->initialized
= 1;
1452 /* Call encoder subdevice function to set the standard */
1453 if (mutex_lock_interruptible(&common
->lock
))
1454 return -ERESTARTSYS
;
1456 ch
->video
.stdid
= *std_id
;
1458 /* Get the information about the standard */
1459 if (vpif_update_std_info(ch
)) {
1461 vpif_err("Error getting the standard info\n");
1465 /* Configure the default format information */
1466 vpif_config_format(ch
);
1468 /* set standard in the sub device */
1469 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], core
,
1472 vpif_dbg(1, debug
, "Failed to set standard for sub devices\n");
1475 mutex_unlock(&common
->lock
);
1480 * vpif_enum_input() - ENUMINPUT handler
1482 * @priv: file handle
1483 * @input: ptr to input structure
1485 static int vpif_enum_input(struct file
*file
, void *priv
,
1486 struct v4l2_input
*input
)
1489 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1490 struct vpif_capture_chan_config
*chan_cfg
;
1491 struct vpif_fh
*fh
= priv
;
1492 struct channel_obj
*ch
= fh
->channel
;
1494 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1496 if (input
->index
>= chan_cfg
->input_count
) {
1497 vpif_dbg(1, debug
, "Invalid input index\n");
1501 memcpy(input
, &chan_cfg
->inputs
[input
->index
].input
,
1507 * vpif_g_input() - Get INPUT handler
1509 * @priv: file handle
1510 * @index: ptr to input index
1512 static int vpif_g_input(struct file
*file
, void *priv
, unsigned int *index
)
1514 struct vpif_fh
*fh
= priv
;
1515 struct channel_obj
*ch
= fh
->channel
;
1516 struct video_obj
*vid_ch
= &ch
->video
;
1518 *index
= vid_ch
->input_idx
;
1524 * vpif_s_input() - Set INPUT handler
1526 * @priv: file handle
1527 * @index: input index
1529 static int vpif_s_input(struct file
*file
, void *priv
, unsigned int index
)
1531 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1532 struct vpif_capture_chan_config
*chan_cfg
;
1533 struct vpif_fh
*fh
= priv
;
1534 struct channel_obj
*ch
= fh
->channel
;
1535 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1536 struct video_obj
*vid_ch
= &ch
->video
;
1537 struct vpif_subdev_info
*subdev_info
;
1538 int ret
= 0, sd_index
= 0;
1539 u32 input
= 0, output
= 0;
1541 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1543 if (common
->started
) {
1544 vpif_err("Streaming in progress\n");
1548 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1549 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1550 if (!fh
->initialized
) {
1551 vpif_dbg(1, debug
, "Channel Busy\n");
1556 ret
= v4l2_prio_check(&ch
->prio
, &fh
->prio
);
1560 fh
->initialized
= 1;
1561 subdev_info
= vpif_map_sub_device_to_input(ch
, config
, index
,
1563 if (NULL
== subdev_info
) {
1565 "couldn't lookup sub device for the input index\n");
1569 if (mutex_lock_interruptible(&common
->lock
))
1570 return -ERESTARTSYS
;
1572 /* first setup input path from sub device to vpif */
1573 if (config
->setup_input_path
) {
1574 ret
= config
->setup_input_path(ch
->channel_id
,
1577 vpif_dbg(1, debug
, "couldn't setup input path for the"
1578 " sub device %s, for input index %d\n",
1579 subdev_info
->name
, index
);
1584 if (subdev_info
->can_route
) {
1585 input
= subdev_info
->input
;
1586 output
= subdev_info
->output
;
1587 ret
= v4l2_subdev_call(vpif_obj
.sd
[sd_index
], video
, s_routing
,
1590 vpif_dbg(1, debug
, "Failed to set input\n");
1594 vid_ch
->input_idx
= index
;
1595 ch
->curr_subdev_info
= subdev_info
;
1596 ch
->curr_sd_index
= sd_index
;
1597 /* copy interface parameters to vpif */
1598 ch
->vpifparams
.iface
= subdev_info
->vpif_if
;
1600 /* update tvnorms from the sub device input info */
1601 ch
->video_dev
->tvnorms
= chan_cfg
->inputs
[index
].input
.std
;
1604 mutex_unlock(&common
->lock
);
1609 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1611 * @priv: file handle
1612 * @index: input index
1614 static int vpif_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1615 struct v4l2_fmtdesc
*fmt
)
1617 struct vpif_fh
*fh
= priv
;
1618 struct channel_obj
*ch
= fh
->channel
;
1620 if (fmt
->index
!= 0) {
1621 vpif_dbg(1, debug
, "Invalid format index\n");
1625 /* Fill in the information about format */
1626 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
) {
1627 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1628 strcpy(fmt
->description
, "Raw Mode -Bayer Pattern GrRBGb");
1629 fmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
1631 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1632 strcpy(fmt
->description
, "YCbCr4:2:2 YC Planar");
1633 fmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
1639 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1641 * @priv: file handle
1642 * @fmt: ptr to v4l2 format structure
1644 static int vpif_try_fmt_vid_cap(struct file
*file
, void *priv
,
1645 struct v4l2_format
*fmt
)
1647 struct vpif_fh
*fh
= priv
;
1648 struct channel_obj
*ch
= fh
->channel
;
1649 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
1651 return vpif_check_format(ch
, pixfmt
, 1);
1656 * vpif_g_fmt_vid_cap() - Set INPUT handler
1658 * @priv: file handle
1659 * @fmt: ptr to v4l2 format structure
1661 static int vpif_g_fmt_vid_cap(struct file
*file
, void *priv
,
1662 struct v4l2_format
*fmt
)
1664 struct vpif_fh
*fh
= priv
;
1665 struct channel_obj
*ch
= fh
->channel
;
1666 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1668 /* Check the validity of the buffer type */
1669 if (common
->fmt
.type
!= fmt
->type
)
1672 /* Fill in the information about format */
1673 if (mutex_lock_interruptible(&common
->lock
))
1674 return -ERESTARTSYS
;
1677 mutex_unlock(&common
->lock
);
1682 * vpif_s_fmt_vid_cap() - Set FMT handler
1684 * @priv: file handle
1685 * @fmt: ptr to v4l2 format structure
1687 static int vpif_s_fmt_vid_cap(struct file
*file
, void *priv
,
1688 struct v4l2_format
*fmt
)
1690 struct vpif_fh
*fh
= priv
;
1691 struct channel_obj
*ch
= fh
->channel
;
1692 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1693 struct v4l2_pix_format
*pixfmt
;
1696 vpif_dbg(2, debug
, "VIDIOC_S_FMT\n");
1698 /* If streaming is started, return error */
1699 if (common
->started
) {
1700 vpif_dbg(1, debug
, "Streaming is started\n");
1704 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1705 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1706 if (!fh
->initialized
) {
1707 vpif_dbg(1, debug
, "Channel Busy\n");
1712 ret
= v4l2_prio_check(&ch
->prio
, &fh
->prio
);
1716 fh
->initialized
= 1;
1718 pixfmt
= &fmt
->fmt
.pix
;
1719 /* Check for valid field format */
1720 ret
= vpif_check_format(ch
, pixfmt
, 0);
1724 /* store the format in the channel object */
1725 if (mutex_lock_interruptible(&common
->lock
))
1726 return -ERESTARTSYS
;
1729 mutex_unlock(&common
->lock
);
1735 * vpif_querycap() - QUERYCAP handler
1737 * @priv: file handle
1738 * @cap: ptr to v4l2_capability structure
1740 static int vpif_querycap(struct file
*file
, void *priv
,
1741 struct v4l2_capability
*cap
)
1743 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1745 cap
->version
= VPIF_CAPTURE_VERSION_CODE
;
1746 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1747 strlcpy(cap
->driver
, "vpif capture", sizeof(cap
->driver
));
1748 strlcpy(cap
->bus_info
, "DM646x Platform", sizeof(cap
->bus_info
));
1749 strlcpy(cap
->card
, config
->card_name
, sizeof(cap
->card
));
1755 * vpif_g_priority() - get priority handler
1757 * @priv: file handle
1758 * @prio: ptr to v4l2_priority structure
1760 static int vpif_g_priority(struct file
*file
, void *priv
,
1761 enum v4l2_priority
*prio
)
1763 struct vpif_fh
*fh
= priv
;
1764 struct channel_obj
*ch
= fh
->channel
;
1766 *prio
= v4l2_prio_max(&ch
->prio
);
1772 * vpif_s_priority() - set priority handler
1774 * @priv: file handle
1775 * @prio: ptr to v4l2_priority structure
1777 static int vpif_s_priority(struct file
*file
, void *priv
, enum v4l2_priority p
)
1779 struct vpif_fh
*fh
= priv
;
1780 struct channel_obj
*ch
= fh
->channel
;
1782 return v4l2_prio_change(&ch
->prio
, &fh
->prio
, p
);
1786 * vpif_cropcap() - cropcap handler
1788 * @priv: file handle
1789 * @crop: ptr to v4l2_cropcap structure
1791 static int vpif_cropcap(struct file
*file
, void *priv
,
1792 struct v4l2_cropcap
*crop
)
1794 struct vpif_fh
*fh
= priv
;
1795 struct channel_obj
*ch
= fh
->channel
;
1796 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1798 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= crop
->type
)
1801 crop
->bounds
.left
= 0;
1802 crop
->bounds
.top
= 0;
1803 crop
->bounds
.height
= common
->height
;
1804 crop
->bounds
.width
= common
->width
;
1805 crop
->defrect
= crop
->bounds
;
1809 /* vpif capture ioctl operations */
1810 static const struct v4l2_ioctl_ops vpif_ioctl_ops
= {
1811 .vidioc_querycap
= vpif_querycap
,
1812 .vidioc_g_priority
= vpif_g_priority
,
1813 .vidioc_s_priority
= vpif_s_priority
,
1814 .vidioc_enum_fmt_vid_cap
= vpif_enum_fmt_vid_cap
,
1815 .vidioc_g_fmt_vid_cap
= vpif_g_fmt_vid_cap
,
1816 .vidioc_s_fmt_vid_cap
= vpif_s_fmt_vid_cap
,
1817 .vidioc_try_fmt_vid_cap
= vpif_try_fmt_vid_cap
,
1818 .vidioc_enum_input
= vpif_enum_input
,
1819 .vidioc_s_input
= vpif_s_input
,
1820 .vidioc_g_input
= vpif_g_input
,
1821 .vidioc_reqbufs
= vpif_reqbufs
,
1822 .vidioc_querybuf
= vpif_querybuf
,
1823 .vidioc_querystd
= vpif_querystd
,
1824 .vidioc_s_std
= vpif_s_std
,
1825 .vidioc_g_std
= vpif_g_std
,
1826 .vidioc_qbuf
= vpif_qbuf
,
1827 .vidioc_dqbuf
= vpif_dqbuf
,
1828 .vidioc_streamon
= vpif_streamon
,
1829 .vidioc_streamoff
= vpif_streamoff
,
1830 .vidioc_cropcap
= vpif_cropcap
,
1833 /* vpif file operations */
1834 static struct v4l2_file_operations vpif_fops
= {
1835 .owner
= THIS_MODULE
,
1837 .release
= vpif_release
,
1838 .ioctl
= video_ioctl2
,
1843 /* vpif video template */
1844 static struct video_device vpif_video_template
= {
1848 .ioctl_ops
= &vpif_ioctl_ops
,
1852 * initialize_vpif() - Initialize vpif data structures
1854 * Allocate memory for data structures and initialize them
1856 static int initialize_vpif(void)
1859 int free_channel_objects_index
;
1861 /* Default number of buffers should be 3 */
1862 if ((ch0_numbuffers
> 0) &&
1863 (ch0_numbuffers
< config_params
.min_numbuffers
))
1864 ch0_numbuffers
= config_params
.min_numbuffers
;
1865 if ((ch1_numbuffers
> 0) &&
1866 (ch1_numbuffers
< config_params
.min_numbuffers
))
1867 ch1_numbuffers
= config_params
.min_numbuffers
;
1869 /* Set buffer size to min buffers size if it is invalid */
1870 if (ch0_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
])
1872 config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
];
1873 if (ch1_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
])
1875 config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
];
1877 config_params
.numbuffers
[VPIF_CHANNEL0_VIDEO
] = ch0_numbuffers
;
1878 config_params
.numbuffers
[VPIF_CHANNEL1_VIDEO
] = ch1_numbuffers
;
1879 if (ch0_numbuffers
) {
1880 config_params
.channel_bufsize
[VPIF_CHANNEL0_VIDEO
]
1883 if (ch1_numbuffers
) {
1884 config_params
.channel_bufsize
[VPIF_CHANNEL1_VIDEO
]
1888 /* Allocate memory for six channel objects */
1889 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1891 kzalloc(sizeof(*vpif_obj
.dev
[i
]), GFP_KERNEL
);
1892 /* If memory allocation fails, return error */
1893 if (!vpif_obj
.dev
[i
]) {
1894 free_channel_objects_index
= i
;
1896 goto vpif_init_free_channel_objects
;
1901 vpif_init_free_channel_objects
:
1902 for (j
= 0; j
< free_channel_objects_index
; j
++)
1903 kfree(vpif_obj
.dev
[j
]);
1908 * vpif_probe : This function probes the vpif capture driver
1909 * @pdev: platform device pointer
1911 * This creates device entries by register itself to the V4L2 driver and
1912 * initializes fields of each channel objects
1914 static __init
int vpif_probe(struct platform_device
*pdev
)
1916 struct vpif_subdev_info
*subdevdata
;
1917 struct vpif_capture_config
*config
;
1918 int i
, j
, k
, m
, q
, err
;
1919 struct i2c_adapter
*i2c_adap
;
1920 struct channel_obj
*ch
;
1921 struct common_obj
*common
;
1922 struct video_device
*vfd
;
1923 struct resource
*res
;
1926 vpif_dev
= &pdev
->dev
;
1928 err
= initialize_vpif();
1930 v4l2_err(vpif_dev
->driver
, "Error initializing vpif\n");
1935 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, k
))) {
1936 for (i
= res
->start
; i
<= res
->end
; i
++) {
1937 if (request_irq(i
, vpif_channel_isr
, IRQF_DISABLED
,
1939 (void *)(&vpif_obj
.dev
[k
]->channel_id
))) {
1948 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1949 /* Get the pointer to the channel object */
1950 ch
= vpif_obj
.dev
[i
];
1951 /* Allocate memory for video device */
1952 vfd
= video_device_alloc();
1954 for (j
= 0; j
< i
; j
++) {
1955 ch
= vpif_obj
.dev
[j
];
1956 video_device_release(ch
->video_dev
);
1959 goto vpif_dev_alloc_err
;
1962 /* Initialize field of video device */
1963 *vfd
= vpif_video_template
;
1964 vfd
->v4l2_dev
= &vpif_obj
.v4l2_dev
;
1965 vfd
->release
= video_device_release
;
1966 snprintf(vfd
->name
, sizeof(vfd
->name
),
1967 "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
1968 (VPIF_CAPTURE_VERSION_CODE
>> 16) & 0xff,
1969 (VPIF_CAPTURE_VERSION_CODE
>> 8) & 0xff,
1970 (VPIF_CAPTURE_VERSION_CODE
) & 0xff);
1971 /* Set video_dev to the video device */
1972 ch
->video_dev
= vfd
;
1975 for (j
= 0; j
< VPIF_CAPTURE_MAX_DEVICES
; j
++) {
1976 ch
= vpif_obj
.dev
[j
];
1978 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
1979 spin_lock_init(&common
->irqlock
);
1980 mutex_init(&common
->lock
);
1981 /* Initialize prio member of channel object */
1982 v4l2_prio_init(&ch
->prio
);
1983 err
= video_register_device(ch
->video_dev
,
1984 VFL_TYPE_GRABBER
, (j
? 1 : 0));
1988 video_set_drvdata(ch
->video_dev
, ch
);
1992 i2c_adap
= i2c_get_adapter(1);
1993 config
= pdev
->dev
.platform_data
;
1995 subdev_count
= config
->subdev_count
;
1996 vpif_obj
.sd
= kmalloc(sizeof(struct v4l2_subdev
*) * subdev_count
,
1998 if (vpif_obj
.sd
== NULL
) {
1999 vpif_err("unable to allocate memory for subdevice pointers\n");
2004 err
= v4l2_device_register(vpif_dev
, &vpif_obj
.v4l2_dev
);
2006 v4l2_err(vpif_dev
->driver
, "Error registering v4l2 device\n");
2007 goto probe_subdev_out
;
2010 for (i
= 0; i
< subdev_count
; i
++) {
2011 subdevdata
= &config
->subdev_info
[i
];
2013 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 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
);