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/slab.h>
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-chip-ident.h>
41 #include "vpif_capture.h"
44 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(VPIF_CAPTURE_VERSION
);
48 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
49 #define vpif_dbg(level, debug, fmt, arg...) \
50 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
53 static u32 ch0_numbuffers
= 3;
54 static u32 ch1_numbuffers
= 3;
55 static u32 ch0_bufsize
= 1920 * 1080 * 2;
56 static u32 ch1_bufsize
= 720 * 576 * 2;
58 module_param(debug
, int, 0644);
59 module_param(ch0_numbuffers
, uint
, S_IRUGO
);
60 module_param(ch1_numbuffers
, uint
, S_IRUGO
);
61 module_param(ch0_bufsize
, uint
, S_IRUGO
);
62 module_param(ch1_bufsize
, uint
, S_IRUGO
);
64 MODULE_PARM_DESC(debug
, "Debug level 0-1");
65 MODULE_PARM_DESC(ch2_numbuffers
, "Channel0 buffer count (default:3)");
66 MODULE_PARM_DESC(ch3_numbuffers
, "Channel1 buffer count (default:3)");
67 MODULE_PARM_DESC(ch2_bufsize
, "Channel0 buffer size (default:1920 x 1080 x 2)");
68 MODULE_PARM_DESC(ch3_bufsize
, "Channel1 buffer size (default:720 x 576 x 2)");
70 static struct vpif_config_params config_params
= {
74 .min_bufsize
[0] = 720 * 480 * 2,
75 .min_bufsize
[1] = 720 * 480 * 2,
76 .channel_bufsize
[0] = 1920 * 1080 * 2,
77 .channel_bufsize
[1] = 720 * 576 * 2,
80 /* global variables */
81 static struct vpif_device vpif_obj
= { {NULL
} };
82 static struct device
*vpif_dev
;
85 * vpif_uservirt_to_phys : translate user/virtual address to phy address
86 * @virtp: user/virtual address
88 * This inline function is used to convert user space virtual address to
91 static inline u32
vpif_uservirt_to_phys(u32 virtp
)
93 unsigned long physp
= 0;
94 struct mm_struct
*mm
= current
->mm
;
95 struct vm_area_struct
*vma
;
97 vma
= find_vma(mm
, virtp
);
99 /* For kernel direct-mapped memory, take the easy way */
100 if (virtp
>= PAGE_OFFSET
)
101 physp
= virt_to_phys((void *)virtp
);
102 else if (vma
&& (vma
->vm_flags
& VM_IO
) && (vma
->vm_pgoff
))
104 * this will catch, kernel-allocated, mmaped-to-usermode
107 physp
= (vma
->vm_pgoff
<< PAGE_SHIFT
) + (virtp
- vma
->vm_start
);
109 /* otherwise, use get_user_pages() for general userland pages */
110 int res
, nr_pages
= 1;
113 down_read(¤t
->mm
->mmap_sem
);
115 res
= get_user_pages(current
, current
->mm
,
116 virtp
, nr_pages
, 1, 0, &pages
, NULL
);
117 up_read(¤t
->mm
->mmap_sem
);
120 physp
= __pa(page_address(&pages
[0]) +
121 (virtp
& ~PAGE_MASK
));
123 vpif_err("get_user_pages failed\n");
131 * buffer_prepare : callback function for buffer prepare
132 * @q : buffer queue ptr
133 * @vb: ptr to video buffer
136 * This is the callback function for buffer prepare when videobuf_qbuf()
137 * function is called. The buffer is prepared and user space virtual address
138 * or user address is converted into physical address
140 static int vpif_buffer_prepare(struct videobuf_queue
*q
,
141 struct videobuf_buffer
*vb
,
142 enum v4l2_field field
)
144 /* Get the file handle object and channel object */
145 struct vpif_fh
*fh
= q
->priv_data
;
146 struct channel_obj
*ch
= fh
->channel
;
147 struct common_obj
*common
;
151 vpif_dbg(2, debug
, "vpif_buffer_prepare\n");
153 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
155 /* If buffer is not initialized, initialize it */
156 if (VIDEOBUF_NEEDS_INIT
== vb
->state
) {
157 vb
->width
= common
->width
;
158 vb
->height
= common
->height
;
159 vb
->size
= vb
->width
* vb
->height
;
162 vb
->state
= VIDEOBUF_PREPARED
;
164 * if user pointer memory mechanism is used, get the physical
165 * address of the buffer
167 if (V4L2_MEMORY_USERPTR
== common
->memory
) {
168 if (0 == vb
->baddr
) {
169 vpif_dbg(1, debug
, "buffer address is 0\n");
173 vb
->boff
= vpif_uservirt_to_phys(vb
->baddr
);
174 if (!IS_ALIGNED(vb
->boff
, 8))
180 if (!IS_ALIGNED((addr
+ common
->ytop_off
), 8) ||
181 !IS_ALIGNED((addr
+ common
->ybtm_off
), 8) ||
182 !IS_ALIGNED((addr
+ common
->ctop_off
), 8) ||
183 !IS_ALIGNED((addr
+ common
->cbtm_off
), 8))
188 vpif_dbg(1, debug
, "buffer_prepare:offset is not aligned to 8 bytes\n");
193 * vpif_buffer_setup : Callback function for buffer setup.
194 * @q: buffer queue ptr
195 * @count: number of buffers
196 * @size: size of the buffer
198 * This callback function is called when reqbuf() is called to adjust
199 * the buffer count and buffer size
201 static int vpif_buffer_setup(struct videobuf_queue
*q
, unsigned int *count
,
204 /* Get the file handle object and channel object */
205 struct vpif_fh
*fh
= q
->priv_data
;
206 struct channel_obj
*ch
= fh
->channel
;
207 struct common_obj
*common
;
209 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
211 vpif_dbg(2, debug
, "vpif_buffer_setup\n");
213 /* If memory type is not mmap, return */
214 if (V4L2_MEMORY_MMAP
!= common
->memory
)
217 /* Calculate the size of the buffer */
218 *size
= config_params
.channel_bufsize
[ch
->channel_id
];
220 if (*count
< config_params
.min_numbuffers
)
221 *count
= config_params
.min_numbuffers
;
226 * vpif_buffer_queue : Callback function to add buffer to DMA queue
227 * @q: ptr to videobuf_queue
228 * @vb: ptr to videobuf_buffer
230 static void vpif_buffer_queue(struct videobuf_queue
*q
,
231 struct videobuf_buffer
*vb
)
233 /* Get the file handle object and channel object */
234 struct vpif_fh
*fh
= q
->priv_data
;
235 struct channel_obj
*ch
= fh
->channel
;
236 struct common_obj
*common
;
238 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
240 vpif_dbg(2, debug
, "vpif_buffer_queue\n");
242 /* add the buffer to the DMA queue */
243 list_add_tail(&vb
->queue
, &common
->dma_queue
);
244 /* Change state of the buffer */
245 vb
->state
= VIDEOBUF_QUEUED
;
249 * vpif_buffer_release : Callback function to free buffer
250 * @q: buffer queue ptr
251 * @vb: ptr to video buffer
253 * This function is called from the videobuf layer to free memory
254 * allocated to the buffers
256 static void vpif_buffer_release(struct videobuf_queue
*q
,
257 struct videobuf_buffer
*vb
)
259 /* Get the file handle object and channel object */
260 struct vpif_fh
*fh
= q
->priv_data
;
261 struct channel_obj
*ch
= fh
->channel
;
262 struct common_obj
*common
;
264 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
266 videobuf_dma_contig_free(q
, vb
);
267 vb
->state
= VIDEOBUF_NEEDS_INIT
;
270 static struct videobuf_queue_ops video_qops
= {
271 .buf_setup
= vpif_buffer_setup
,
272 .buf_prepare
= vpif_buffer_prepare
,
273 .buf_queue
= vpif_buffer_queue
,
274 .buf_release
= vpif_buffer_release
,
277 static u8 channel_first_int
[VPIF_NUMBER_OF_OBJECTS
][2] =
281 * vpif_process_buffer_complete: process a completed buffer
282 * @common: ptr to common channel object
284 * This function time stamp the buffer and mark it as DONE. It also
285 * wake up any process waiting on the QUEUE and set the next buffer
288 static void vpif_process_buffer_complete(struct common_obj
*common
)
290 do_gettimeofday(&common
->cur_frm
->ts
);
291 common
->cur_frm
->state
= VIDEOBUF_DONE
;
292 wake_up_interruptible(&common
->cur_frm
->done
);
293 /* Make curFrm pointing to nextFrm */
294 common
->cur_frm
= common
->next_frm
;
298 * vpif_schedule_next_buffer: set next buffer address for capture
299 * @common : ptr to common channel object
301 * This function will get next buffer from the dma queue and
302 * set the buffer address in the vpif register for capture.
303 * the buffer is marked active
305 static void vpif_schedule_next_buffer(struct common_obj
*common
)
307 unsigned long addr
= 0;
309 common
->next_frm
= list_entry(common
->dma_queue
.next
,
310 struct videobuf_buffer
, queue
);
311 /* Remove that buffer from the buffer queue */
312 list_del(&common
->next_frm
->queue
);
313 common
->next_frm
->state
= VIDEOBUF_ACTIVE
;
314 if (V4L2_MEMORY_USERPTR
== common
->memory
)
315 addr
= common
->next_frm
->boff
;
317 addr
= videobuf_to_dma_contig(common
->next_frm
);
319 /* Set top and bottom field addresses in VPIF registers */
320 common
->set_addr(addr
+ common
->ytop_off
,
321 addr
+ common
->ybtm_off
,
322 addr
+ common
->ctop_off
,
323 addr
+ common
->cbtm_off
);
327 * vpif_channel_isr : ISR handler for vpif capture
329 * @dev_id: dev_id ptr
331 * It changes status of the captured buffer, takes next buffer from the queue
332 * and sets its address in VPIF registers
334 static irqreturn_t
vpif_channel_isr(int irq
, void *dev_id
)
336 struct vpif_device
*dev
= &vpif_obj
;
337 struct common_obj
*common
;
338 struct channel_obj
*ch
;
339 enum v4l2_field field
;
343 channel_id
= *(int *)(dev_id
);
344 if (!vpif_intr_status(channel_id
))
347 ch
= dev
->dev
[channel_id
];
349 field
= ch
->common
[VPIF_VIDEO_INDEX
].fmt
.fmt
.pix
.field
;
351 for (i
= 0; i
< VPIF_NUMBER_OF_OBJECTS
; i
++) {
352 common
= &ch
->common
[i
];
353 /* skip If streaming is not started in this channel */
354 if (0 == common
->started
)
357 /* Check the field format */
358 if (1 == ch
->vpifparams
.std_info
.frm_fmt
) {
359 /* Progressive mode */
360 if (list_empty(&common
->dma_queue
))
363 if (!channel_first_int
[i
][channel_id
])
364 vpif_process_buffer_complete(common
);
366 channel_first_int
[i
][channel_id
] = 0;
368 vpif_schedule_next_buffer(common
);
371 channel_first_int
[i
][channel_id
] = 0;
374 * Interlaced mode. If it is first interrupt, ignore
377 if (channel_first_int
[i
][channel_id
]) {
378 channel_first_int
[i
][channel_id
] = 0;
383 /* Get field id from VPIF registers */
384 fid
= vpif_channel_getfid(ch
->channel_id
);
385 if (fid
!= ch
->field_id
) {
387 * If field id does not match stored
388 * field id, make them in sync
395 /* device field id and local field id are in sync */
397 /* this is even field */
398 if (common
->cur_frm
== common
->next_frm
)
401 /* mark the current buffer as done */
402 vpif_process_buffer_complete(common
);
403 } else if (1 == fid
) {
405 if (list_empty(&common
->dma_queue
) ||
406 (common
->cur_frm
!= common
->next_frm
))
409 vpif_schedule_next_buffer(common
);
417 * vpif_update_std_info() - update standard related info
418 * @ch: ptr to channel object
420 * For a given standard selected by application, update values
421 * in the device data structures
423 static int vpif_update_std_info(struct channel_obj
*ch
)
425 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
426 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
427 const struct vpif_channel_config_params
*config
;
428 struct vpif_channel_config_params
*std_info
= &vpifparams
->std_info
;
429 struct video_obj
*vid_ch
= &ch
->video
;
432 vpif_dbg(2, debug
, "vpif_update_std_info\n");
434 for (index
= 0; index
< vpif_ch_params_count
; index
++) {
435 config
= &ch_params
[index
];
436 if (config
->hd_sd
== 0) {
437 vpif_dbg(2, debug
, "SD format\n");
438 if (config
->stdid
& vid_ch
->stdid
) {
439 memcpy(std_info
, config
, sizeof(*config
));
443 vpif_dbg(2, debug
, "HD format\n");
444 if (config
->dv_preset
== vid_ch
->dv_preset
) {
445 memcpy(std_info
, config
, sizeof(*config
));
451 /* standard not found */
452 if (index
== vpif_ch_params_count
)
455 common
->fmt
.fmt
.pix
.width
= std_info
->width
;
456 common
->width
= std_info
->width
;
457 common
->fmt
.fmt
.pix
.height
= std_info
->height
;
458 common
->height
= std_info
->height
;
459 common
->fmt
.fmt
.pix
.bytesperline
= std_info
->width
;
460 vpifparams
->video_params
.hpitch
= std_info
->width
;
461 vpifparams
->video_params
.storage_mode
= std_info
->frm_fmt
;
467 * vpif_calculate_offsets : This function calculates buffers offsets
468 * @ch : ptr to channel object
470 * This function calculates buffer offsets for Y and C in the top and
473 static void vpif_calculate_offsets(struct channel_obj
*ch
)
475 unsigned int hpitch
, vpitch
, sizeimage
;
476 struct video_obj
*vid_ch
= &(ch
->video
);
477 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
478 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
479 enum v4l2_field field
= common
->fmt
.fmt
.pix
.field
;
481 vpif_dbg(2, debug
, "vpif_calculate_offsets\n");
483 if (V4L2_FIELD_ANY
== field
) {
484 if (vpifparams
->std_info
.frm_fmt
)
485 vid_ch
->buf_field
= V4L2_FIELD_NONE
;
487 vid_ch
->buf_field
= V4L2_FIELD_INTERLACED
;
489 vid_ch
->buf_field
= common
->fmt
.fmt
.pix
.field
;
491 if (V4L2_MEMORY_USERPTR
== common
->memory
)
492 sizeimage
= common
->fmt
.fmt
.pix
.sizeimage
;
494 sizeimage
= config_params
.channel_bufsize
[ch
->channel_id
];
496 hpitch
= common
->fmt
.fmt
.pix
.bytesperline
;
497 vpitch
= sizeimage
/ (hpitch
* 2);
499 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
500 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
)) {
501 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
502 common
->ytop_off
= 0;
503 common
->ybtm_off
= hpitch
;
504 common
->ctop_off
= sizeimage
/ 2;
505 common
->cbtm_off
= sizeimage
/ 2 + hpitch
;
506 } else if (V4L2_FIELD_SEQ_TB
== vid_ch
->buf_field
) {
507 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
508 common
->ytop_off
= 0;
509 common
->ybtm_off
= sizeimage
/ 4;
510 common
->ctop_off
= sizeimage
/ 2;
511 common
->cbtm_off
= common
->ctop_off
+ sizeimage
/ 4;
512 } else if (V4L2_FIELD_SEQ_BT
== vid_ch
->buf_field
) {
513 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
514 common
->ybtm_off
= 0;
515 common
->ytop_off
= sizeimage
/ 4;
516 common
->cbtm_off
= sizeimage
/ 2;
517 common
->ctop_off
= common
->cbtm_off
+ sizeimage
/ 4;
519 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
520 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
))
521 vpifparams
->video_params
.storage_mode
= 1;
523 vpifparams
->video_params
.storage_mode
= 0;
525 if (1 == vpifparams
->std_info
.frm_fmt
)
526 vpifparams
->video_params
.hpitch
=
527 common
->fmt
.fmt
.pix
.bytesperline
;
529 if ((field
== V4L2_FIELD_ANY
)
530 || (field
== V4L2_FIELD_INTERLACED
))
531 vpifparams
->video_params
.hpitch
=
532 common
->fmt
.fmt
.pix
.bytesperline
* 2;
534 vpifparams
->video_params
.hpitch
=
535 common
->fmt
.fmt
.pix
.bytesperline
;
538 ch
->vpifparams
.video_params
.stdid
= vpifparams
->std_info
.stdid
;
542 * vpif_config_format: configure default frame format in the device
543 * ch : ptr to channel object
545 static void vpif_config_format(struct channel_obj
*ch
)
547 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
549 vpif_dbg(2, debug
, "vpif_config_format\n");
551 common
->fmt
.fmt
.pix
.field
= V4L2_FIELD_ANY
;
552 if (config_params
.numbuffers
[ch
->channel_id
] == 0)
553 common
->memory
= V4L2_MEMORY_USERPTR
;
555 common
->memory
= V4L2_MEMORY_MMAP
;
557 common
->fmt
.fmt
.pix
.sizeimage
558 = config_params
.channel_bufsize
[ch
->channel_id
];
560 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
)
561 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
563 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUV422P
;
564 common
->fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
568 * vpif_get_default_field() - Get default field type based on interface
569 * @vpif_params - ptr to vpif params
571 static inline enum v4l2_field
vpif_get_default_field(
572 struct vpif_interface
*iface
)
574 return (iface
->if_type
== VPIF_IF_RAW_BAYER
) ? V4L2_FIELD_NONE
:
575 V4L2_FIELD_INTERLACED
;
579 * vpif_check_format() - check given pixel format for compatibility
581 * @pixfmt - Given pixel format
582 * @update - update the values as per hardware requirement
584 * Check the application pixel format for S_FMT and update the input
585 * values as per hardware limits for TRY_FMT. The default pixel and
586 * field format is selected based on interface type.
588 static int vpif_check_format(struct channel_obj
*ch
,
589 struct v4l2_pix_format
*pixfmt
,
592 struct common_obj
*common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
593 struct vpif_params
*vpif_params
= &ch
->vpifparams
;
594 enum v4l2_field field
= pixfmt
->field
;
595 u32 sizeimage
, hpitch
, vpitch
;
598 vpif_dbg(2, debug
, "vpif_check_format\n");
600 * first check for the pixel format. If if_type is Raw bayer,
601 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
602 * V4L2_PIX_FMT_YUV422P is supported
604 if (vpif_params
->iface
.if_type
== VPIF_IF_RAW_BAYER
) {
605 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
) {
607 vpif_dbg(2, debug
, "invalid pix format\n");
610 pixfmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
613 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_YUV422P
) {
615 vpif_dbg(2, debug
, "invalid pixel format\n");
618 pixfmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
622 if (!(VPIF_VALID_FIELD(field
))) {
624 vpif_dbg(2, debug
, "invalid field format\n");
628 * By default use FIELD_NONE for RAW Bayer capture
629 * and FIELD_INTERLACED for other interfaces
631 field
= vpif_get_default_field(&vpif_params
->iface
);
632 } else if (field
== V4L2_FIELD_ANY
)
633 /* unsupported field. Use default */
634 field
= vpif_get_default_field(&vpif_params
->iface
);
636 /* validate the hpitch */
637 hpitch
= pixfmt
->bytesperline
;
638 if (hpitch
< vpif_params
->std_info
.width
) {
640 vpif_dbg(2, debug
, "invalid hpitch\n");
643 hpitch
= vpif_params
->std_info
.width
;
646 if (V4L2_MEMORY_USERPTR
== common
->memory
)
647 sizeimage
= pixfmt
->sizeimage
;
649 sizeimage
= config_params
.channel_bufsize
[ch
->channel_id
];
651 vpitch
= sizeimage
/ (hpitch
* 2);
653 /* validate the vpitch */
654 if (vpitch
< vpif_params
->std_info
.height
) {
656 vpif_dbg(2, debug
, "Invalid vpitch\n");
659 vpitch
= vpif_params
->std_info
.height
;
662 /* Check for 8 byte alignment */
663 if (!ALIGN(hpitch
, 8)) {
665 vpif_dbg(2, debug
, "invalid pitch alignment\n");
668 /* adjust to next 8 byte boundary */
669 hpitch
= (((hpitch
+ 7) / 8) * 8);
671 /* if update is set, modify the bytesperline and sizeimage */
673 pixfmt
->bytesperline
= hpitch
;
674 pixfmt
->sizeimage
= hpitch
* vpitch
* 2;
677 * Image width and height is always based on current standard width and
680 pixfmt
->width
= common
->fmt
.fmt
.pix
.width
;
681 pixfmt
->height
= common
->fmt
.fmt
.pix
.height
;
688 * vpif_config_addr() - function to configure buffer address in vpif
690 * @muxmode - channel mux mode
692 static void vpif_config_addr(struct channel_obj
*ch
, int muxmode
)
694 struct common_obj
*common
;
696 vpif_dbg(2, debug
, "vpif_config_addr\n");
698 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
700 if (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)
701 common
->set_addr
= ch1_set_videobuf_addr
;
702 else if (2 == muxmode
)
703 common
->set_addr
= ch0_set_videobuf_addr_yc_nmux
;
705 common
->set_addr
= ch0_set_videobuf_addr
;
709 * vpfe_mmap : It is used to map kernel space buffers into user spaces
710 * @filep: file pointer
711 * @vma: ptr to vm_area_struct
713 static int vpif_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
715 /* Get the channel object and file handle object */
716 struct vpif_fh
*fh
= filep
->private_data
;
717 struct channel_obj
*ch
= fh
->channel
;
718 struct common_obj
*common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
720 vpif_dbg(2, debug
, "vpif_mmap\n");
722 return videobuf_mmap_mapper(&common
->buffer_queue
, vma
);
726 * vpif_poll: It is used for select/poll system call
727 * @filep: file pointer
728 * @wait: poll table to wait
730 static unsigned int vpif_poll(struct file
*filep
, poll_table
* wait
)
732 struct vpif_fh
*fh
= filep
->private_data
;
733 struct channel_obj
*channel
= fh
->channel
;
734 struct common_obj
*common
= &(channel
->common
[VPIF_VIDEO_INDEX
]);
736 vpif_dbg(2, debug
, "vpif_poll\n");
739 return videobuf_poll_stream(filep
, &common
->buffer_queue
, wait
);
744 * vpif_open : vpif open handler
747 * It creates object of file handle structure and stores it in private_data
748 * member of filepointer
750 static int vpif_open(struct file
*filep
)
752 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
753 struct video_device
*vdev
= video_devdata(filep
);
754 struct common_obj
*common
;
755 struct video_obj
*vid_ch
;
756 struct channel_obj
*ch
;
760 vpif_dbg(2, debug
, "vpif_open\n");
762 ch
= video_get_drvdata(vdev
);
765 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
767 if (NULL
== ch
->curr_subdev_info
) {
769 * search through the sub device to see a registered
770 * sub device and make it as current sub device
772 for (i
= 0; i
< config
->subdev_count
; i
++) {
773 if (vpif_obj
.sd
[i
]) {
774 /* the sub device is registered */
775 ch
->curr_subdev_info
= &config
->subdev_info
[i
];
776 /* make first input as the current input */
777 vid_ch
->input_idx
= 0;
781 if (i
== config
->subdev_count
) {
782 vpif_err("No sub device registered\n");
787 /* Allocate memory for the file handle object */
788 fh
= kzalloc(sizeof(struct vpif_fh
), GFP_KERNEL
);
790 vpif_err("unable to allocate memory for file handle object\n");
794 /* store pointer to fh in private_data member of filep */
795 filep
->private_data
= fh
;
798 /* If decoder is not initialized. initialize it */
799 if (!ch
->initialized
) {
802 memset(&(ch
->vpifparams
), 0, sizeof(struct vpif_params
));
804 /* Increment channel usrs counter */
806 /* Set io_allowed member to false */
807 fh
->io_allowed
[VPIF_VIDEO_INDEX
] = 0;
808 /* Initialize priority of this instance to default priority */
809 fh
->prio
= V4L2_PRIORITY_UNSET
;
810 v4l2_prio_open(&ch
->prio
, &fh
->prio
);
815 * vpif_release : function to clean up file close
816 * @filep: file pointer
818 * This function deletes buffer queue, frees the buffers and the vpfe file
821 static int vpif_release(struct file
*filep
)
823 struct vpif_fh
*fh
= filep
->private_data
;
824 struct channel_obj
*ch
= fh
->channel
;
825 struct common_obj
*common
;
827 vpif_dbg(2, debug
, "vpif_release\n");
829 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
831 /* if this instance is doing IO */
832 if (fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
833 /* Reset io_usrs member of channel object */
835 /* Disable channel as per its device type and channel id */
836 if (VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) {
838 channel0_intr_enable(0);
840 if ((VPIF_CHANNEL1_VIDEO
== ch
->channel_id
) ||
841 (2 == common
->started
)) {
843 channel1_intr_enable(0);
846 /* Free buffers allocated */
847 videobuf_queue_cancel(&common
->buffer_queue
);
848 videobuf_mmap_free(&common
->buffer_queue
);
851 /* Decrement channel usrs counter */
854 /* Close the priority */
855 v4l2_prio_close(&ch
->prio
, fh
->prio
);
860 filep
->private_data
= NULL
;
866 * vpif_reqbufs() - request buffer handler
869 * @reqbuf: request buffer structure ptr
871 static int vpif_reqbufs(struct file
*file
, void *priv
,
872 struct v4l2_requestbuffers
*reqbuf
)
874 struct vpif_fh
*fh
= priv
;
875 struct channel_obj
*ch
= fh
->channel
;
876 struct common_obj
*common
;
879 vpif_dbg(2, debug
, "vpif_reqbufs\n");
882 * This file handle has not initialized the channel,
883 * It is not allowed to do settings
885 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
)
886 || (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
887 if (!fh
->initialized
) {
888 vpif_dbg(1, debug
, "Channel Busy\n");
893 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= reqbuf
->type
)
896 index
= VPIF_VIDEO_INDEX
;
898 common
= &ch
->common
[index
];
900 if (0 != common
->io_usrs
)
903 /* Initialize videobuf queue as per the buffer type */
904 videobuf_queue_dma_contig_init(&common
->buffer_queue
,
908 common
->fmt
.fmt
.pix
.field
,
909 sizeof(struct videobuf_buffer
), fh
,
912 /* Set io allowed member of file handle to TRUE */
913 fh
->io_allowed
[index
] = 1;
914 /* Increment io usrs member of channel object to 1 */
916 /* Store type of memory requested in channel object */
917 common
->memory
= reqbuf
->memory
;
918 INIT_LIST_HEAD(&common
->dma_queue
);
920 /* Allocate buffers */
921 return videobuf_reqbufs(&common
->buffer_queue
, reqbuf
);
925 * vpif_querybuf() - query buffer handler
928 * @buf: v4l2 buffer structure ptr
930 static int vpif_querybuf(struct file
*file
, void *priv
,
931 struct v4l2_buffer
*buf
)
933 struct vpif_fh
*fh
= priv
;
934 struct channel_obj
*ch
= fh
->channel
;
935 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
937 vpif_dbg(2, debug
, "vpif_querybuf\n");
939 if (common
->fmt
.type
!= buf
->type
)
942 if (common
->memory
!= V4L2_MEMORY_MMAP
) {
943 vpif_dbg(1, debug
, "Invalid memory\n");
947 return videobuf_querybuf(&common
->buffer_queue
, buf
);
951 * vpif_qbuf() - query buffer handler
954 * @buf: v4l2 buffer structure ptr
956 static int vpif_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
959 struct vpif_fh
*fh
= priv
;
960 struct channel_obj
*ch
= fh
->channel
;
961 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
962 struct v4l2_buffer tbuf
= *buf
;
963 struct videobuf_buffer
*buf1
;
964 unsigned long addr
= 0;
968 vpif_dbg(2, debug
, "vpif_qbuf\n");
970 if (common
->fmt
.type
!= tbuf
.type
) {
971 vpif_err("invalid buffer type\n");
975 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
976 vpif_err("fh io not allowed \n");
980 if (!(list_empty(&common
->dma_queue
)) ||
981 (common
->cur_frm
!= common
->next_frm
) ||
983 (common
->started
&& (0 == ch
->field_id
)))
984 return videobuf_qbuf(&common
->buffer_queue
, buf
);
986 /* bufferqueue is empty store buffer address in VPIF registers */
987 mutex_lock(&common
->buffer_queue
.vb_lock
);
988 buf1
= common
->buffer_queue
.bufs
[tbuf
.index
];
990 if ((buf1
->state
== VIDEOBUF_QUEUED
) ||
991 (buf1
->state
== VIDEOBUF_ACTIVE
)) {
992 vpif_err("invalid state\n");
996 switch (buf1
->memory
) {
997 case V4L2_MEMORY_MMAP
:
998 if (buf1
->baddr
== 0)
1002 case V4L2_MEMORY_USERPTR
:
1003 if (tbuf
.length
< buf1
->bsize
)
1006 if ((VIDEOBUF_NEEDS_INIT
!= buf1
->state
)
1007 && (buf1
->baddr
!= tbuf
.m
.userptr
)) {
1008 vpif_buffer_release(&common
->buffer_queue
, buf1
);
1009 buf1
->baddr
= tbuf
.m
.userptr
;
1017 local_irq_save(flags
);
1018 ret
= vpif_buffer_prepare(&common
->buffer_queue
, buf1
,
1019 common
->buffer_queue
.field
);
1021 local_irq_restore(flags
);
1025 buf1
->state
= VIDEOBUF_ACTIVE
;
1027 if (V4L2_MEMORY_USERPTR
== common
->memory
)
1030 addr
= videobuf_to_dma_contig(buf1
);
1032 common
->next_frm
= buf1
;
1033 common
->set_addr(addr
+ common
->ytop_off
,
1034 addr
+ common
->ybtm_off
,
1035 addr
+ common
->ctop_off
,
1036 addr
+ common
->cbtm_off
);
1038 local_irq_restore(flags
);
1039 list_add_tail(&buf1
->stream
, &common
->buffer_queue
.stream
);
1040 mutex_unlock(&common
->buffer_queue
.vb_lock
);
1044 mutex_unlock(&common
->buffer_queue
.vb_lock
);
1049 * vpif_dqbuf() - query buffer handler
1051 * @priv: file handle
1052 * @buf: v4l2 buffer structure ptr
1054 static int vpif_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
1056 struct vpif_fh
*fh
= priv
;
1057 struct channel_obj
*ch
= fh
->channel
;
1058 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1060 vpif_dbg(2, debug
, "vpif_dqbuf\n");
1062 return videobuf_dqbuf(&common
->buffer_queue
, buf
,
1063 file
->f_flags
& O_NONBLOCK
);
1067 * vpif_streamon() - streamon handler
1069 * @priv: file handle
1070 * @buftype: v4l2 buffer type
1072 static int vpif_streamon(struct file
*file
, void *priv
,
1073 enum v4l2_buf_type buftype
)
1076 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1077 struct vpif_fh
*fh
= priv
;
1078 struct channel_obj
*ch
= fh
->channel
;
1079 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1080 struct channel_obj
*oth_ch
= vpif_obj
.dev
[!ch
->channel_id
];
1081 struct vpif_params
*vpif
;
1082 unsigned long addr
= 0;
1085 vpif_dbg(2, debug
, "vpif_streamon\n");
1087 vpif
= &ch
->vpifparams
;
1089 if (buftype
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1090 vpif_dbg(1, debug
, "buffer type not supported\n");
1094 /* If file handle is not allowed IO, return error */
1095 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1096 vpif_dbg(1, debug
, "io not allowed\n");
1100 /* If Streaming is already started, return error */
1101 if (common
->started
) {
1102 vpif_dbg(1, debug
, "channel->started\n");
1106 if ((ch
->channel_id
== VPIF_CHANNEL0_VIDEO
&&
1107 oth_ch
->common
[VPIF_VIDEO_INDEX
].started
&&
1108 vpif
->std_info
.ycmux_mode
== 0) ||
1109 ((ch
->channel_id
== VPIF_CHANNEL1_VIDEO
) &&
1110 (2 == oth_ch
->common
[VPIF_VIDEO_INDEX
].started
))) {
1111 vpif_dbg(1, debug
, "other channel is being used\n");
1115 ret
= vpif_check_format(ch
, &common
->fmt
.fmt
.pix
, 0);
1119 /* Enable streamon on the sub device */
1120 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1123 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
1124 vpif_dbg(1, debug
, "stream on failed in subdev\n");
1128 /* Call videobuf_streamon to start streaming in videobuf */
1129 ret
= videobuf_streamon(&common
->buffer_queue
);
1131 vpif_dbg(1, debug
, "videobuf_streamon\n");
1135 /* If buffer queue is empty, return error */
1136 if (list_empty(&common
->dma_queue
)) {
1137 vpif_dbg(1, debug
, "buffer queue is empty\n");
1142 /* Get the next frame from the buffer queue */
1143 common
->cur_frm
= list_entry(common
->dma_queue
.next
,
1144 struct videobuf_buffer
, queue
);
1145 common
->next_frm
= common
->cur_frm
;
1147 /* Remove buffer from the buffer queue */
1148 list_del(&common
->cur_frm
->queue
);
1149 /* Mark state of the current frame to active */
1150 common
->cur_frm
->state
= VIDEOBUF_ACTIVE
;
1151 /* Initialize field_id and started member */
1153 common
->started
= 1;
1155 if (V4L2_MEMORY_USERPTR
== common
->memory
)
1156 addr
= common
->cur_frm
->boff
;
1158 addr
= videobuf_to_dma_contig(common
->cur_frm
);
1160 /* Calculate the offset for Y and C data in the buffer */
1161 vpif_calculate_offsets(ch
);
1163 if ((vpif
->std_info
.frm_fmt
&&
1164 ((common
->fmt
.fmt
.pix
.field
!= V4L2_FIELD_NONE
) &&
1165 (common
->fmt
.fmt
.pix
.field
!= V4L2_FIELD_ANY
))) ||
1166 (!vpif
->std_info
.frm_fmt
&&
1167 (common
->fmt
.fmt
.pix
.field
== V4L2_FIELD_NONE
))) {
1168 vpif_dbg(1, debug
, "conflict in field format and std format\n");
1173 /* configure 1 or 2 channel mode */
1174 ret
= config
->setup_input_channel_mode(vpif
->std_info
.ycmux_mode
);
1177 vpif_dbg(1, debug
, "can't set vpif channel mode\n");
1181 /* Call vpif_set_params function to set the parameters and addresses */
1182 ret
= vpif_set_video_params(vpif
, ch
->channel_id
);
1185 vpif_dbg(1, debug
, "can't set video params\n");
1189 common
->started
= ret
;
1190 vpif_config_addr(ch
, ret
);
1192 common
->set_addr(addr
+ common
->ytop_off
,
1193 addr
+ common
->ybtm_off
,
1194 addr
+ common
->ctop_off
,
1195 addr
+ common
->cbtm_off
);
1198 * Set interrupt for both the fields in VPIF Register enable channel in
1201 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
)) {
1202 channel0_intr_assert();
1203 channel0_intr_enable(1);
1206 if ((VPIF_CHANNEL1_VIDEO
== ch
->channel_id
) ||
1207 (common
->started
== 2)) {
1208 channel1_intr_assert();
1209 channel1_intr_enable(1);
1212 channel_first_int
[VPIF_VIDEO_INDEX
][ch
->channel_id
] = 1;
1216 videobuf_streamoff(&common
->buffer_queue
);
1221 * vpif_streamoff() - streamoff handler
1223 * @priv: file handle
1224 * @buftype: v4l2 buffer type
1226 static int vpif_streamoff(struct file
*file
, void *priv
,
1227 enum v4l2_buf_type buftype
)
1230 struct vpif_fh
*fh
= priv
;
1231 struct channel_obj
*ch
= fh
->channel
;
1232 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1235 vpif_dbg(2, debug
, "vpif_streamoff\n");
1237 if (buftype
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1238 vpif_dbg(1, debug
, "buffer type not supported\n");
1242 /* If io is allowed for this file handle, return error */
1243 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1244 vpif_dbg(1, debug
, "io not allowed\n");
1248 /* If streaming is not started, return error */
1249 if (!common
->started
) {
1250 vpif_dbg(1, debug
, "channel->started\n");
1254 /* disable channel */
1255 if (VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) {
1257 channel0_intr_enable(0);
1260 channel1_intr_enable(0);
1263 common
->started
= 0;
1265 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1268 if (ret
&& (ret
!= -ENOIOCTLCMD
))
1269 vpif_dbg(1, debug
, "stream off failed in subdev\n");
1271 return videobuf_streamoff(&common
->buffer_queue
);
1275 * vpif_map_sub_device_to_input() - Maps sub device to input
1276 * @ch - ptr to channel
1277 * @config - ptr to capture configuration
1278 * @input_index - Given input index from application
1279 * @sub_device_index - index into sd table
1281 * lookup the sub device information for a given input index.
1282 * we report all the inputs to application. inputs table also
1283 * has sub device name for the each input
1285 static struct vpif_subdev_info
*vpif_map_sub_device_to_input(
1286 struct channel_obj
*ch
,
1287 struct vpif_capture_config
*vpif_cfg
,
1289 int *sub_device_index
)
1291 struct vpif_capture_chan_config
*chan_cfg
;
1292 struct vpif_subdev_info
*subdev_info
= NULL
;
1293 const char *subdev_name
= NULL
;
1296 vpif_dbg(2, debug
, "vpif_map_sub_device_to_input\n");
1298 chan_cfg
= &vpif_cfg
->chan_config
[ch
->channel_id
];
1301 * search through the inputs to find the sub device supporting
1304 for (i
= 0; i
< chan_cfg
->input_count
; i
++) {
1305 /* For each sub device, loop through input */
1306 if (i
== input_index
) {
1307 subdev_name
= chan_cfg
->inputs
[i
].subdev_name
;
1312 /* if reached maximum. return null */
1313 if (i
== chan_cfg
->input_count
|| (NULL
== subdev_name
))
1316 /* loop through the sub device list to get the sub device info */
1317 for (i
= 0; i
< vpif_cfg
->subdev_count
; i
++) {
1318 subdev_info
= &vpif_cfg
->subdev_info
[i
];
1319 if (!strcmp(subdev_info
->name
, subdev_name
))
1323 if (i
== vpif_cfg
->subdev_count
)
1326 /* check if the sub device is registered */
1327 if (NULL
== vpif_obj
.sd
[i
])
1330 *sub_device_index
= i
;
1335 * vpif_querystd() - querystd handler
1337 * @priv: file handle
1338 * @std_id: ptr to std id
1340 * This function is called to detect standard at the selected input
1342 static int vpif_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1344 struct vpif_fh
*fh
= priv
;
1345 struct channel_obj
*ch
= fh
->channel
;
1348 vpif_dbg(2, debug
, "vpif_querystd\n");
1350 /* Call querystd function of decoder device */
1351 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1354 vpif_dbg(1, debug
, "Failed to set standard for sub devices\n");
1360 * vpif_g_std() - get STD handler
1362 * @priv: file handle
1363 * @std_id: ptr to std id
1365 static int vpif_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
1367 struct vpif_fh
*fh
= priv
;
1368 struct channel_obj
*ch
= fh
->channel
;
1370 vpif_dbg(2, debug
, "vpif_g_std\n");
1372 *std
= ch
->video
.stdid
;
1377 * vpif_s_std() - set STD handler
1379 * @priv: file handle
1380 * @std_id: ptr to std id
1382 static int vpif_s_std(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1384 struct vpif_fh
*fh
= priv
;
1385 struct channel_obj
*ch
= fh
->channel
;
1386 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1389 vpif_dbg(2, debug
, "vpif_s_std\n");
1391 if (common
->started
) {
1392 vpif_err("streaming in progress\n");
1396 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1397 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1398 if (!fh
->initialized
) {
1399 vpif_dbg(1, debug
, "Channel Busy\n");
1404 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1408 fh
->initialized
= 1;
1410 /* Call encoder subdevice function to set the standard */
1411 ch
->video
.stdid
= *std_id
;
1412 ch
->video
.dv_preset
= V4L2_DV_INVALID
;
1413 memset(&ch
->video
.bt_timings
, 0, sizeof(ch
->video
.bt_timings
));
1415 /* Get the information about the standard */
1416 if (vpif_update_std_info(ch
)) {
1417 vpif_err("Error getting the standard info\n");
1421 /* Configure the default format information */
1422 vpif_config_format(ch
);
1424 /* set standard in the sub device */
1425 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], core
,
1428 vpif_dbg(1, debug
, "Failed to set standard for sub devices\n");
1433 * vpif_enum_input() - ENUMINPUT handler
1435 * @priv: file handle
1436 * @input: ptr to input structure
1438 static int vpif_enum_input(struct file
*file
, void *priv
,
1439 struct v4l2_input
*input
)
1442 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1443 struct vpif_capture_chan_config
*chan_cfg
;
1444 struct vpif_fh
*fh
= priv
;
1445 struct channel_obj
*ch
= fh
->channel
;
1447 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1449 if (input
->index
>= chan_cfg
->input_count
) {
1450 vpif_dbg(1, debug
, "Invalid input index\n");
1454 memcpy(input
, &chan_cfg
->inputs
[input
->index
].input
,
1460 * vpif_g_input() - Get INPUT handler
1462 * @priv: file handle
1463 * @index: ptr to input index
1465 static int vpif_g_input(struct file
*file
, void *priv
, unsigned int *index
)
1467 struct vpif_fh
*fh
= priv
;
1468 struct channel_obj
*ch
= fh
->channel
;
1469 struct video_obj
*vid_ch
= &ch
->video
;
1471 *index
= vid_ch
->input_idx
;
1477 * vpif_s_input() - Set INPUT handler
1479 * @priv: file handle
1480 * @index: input index
1482 static int vpif_s_input(struct file
*file
, void *priv
, unsigned int index
)
1484 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1485 struct vpif_capture_chan_config
*chan_cfg
;
1486 struct vpif_fh
*fh
= priv
;
1487 struct channel_obj
*ch
= fh
->channel
;
1488 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1489 struct video_obj
*vid_ch
= &ch
->video
;
1490 struct vpif_subdev_info
*subdev_info
;
1491 int ret
= 0, sd_index
= 0;
1492 u32 input
= 0, output
= 0;
1494 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1496 if (common
->started
) {
1497 vpif_err("Streaming in progress\n");
1501 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1502 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1503 if (!fh
->initialized
) {
1504 vpif_dbg(1, debug
, "Channel Busy\n");
1509 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1513 fh
->initialized
= 1;
1514 subdev_info
= vpif_map_sub_device_to_input(ch
, config
, index
,
1516 if (NULL
== subdev_info
) {
1518 "couldn't lookup sub device for the input index\n");
1522 /* first setup input path from sub device to vpif */
1523 if (config
->setup_input_path
) {
1524 ret
= config
->setup_input_path(ch
->channel_id
,
1527 vpif_dbg(1, debug
, "couldn't setup input path for the"
1528 " sub device %s, for input index %d\n",
1529 subdev_info
->name
, index
);
1534 if (subdev_info
->can_route
) {
1535 input
= subdev_info
->input
;
1536 output
= subdev_info
->output
;
1537 ret
= v4l2_subdev_call(vpif_obj
.sd
[sd_index
], video
, s_routing
,
1540 vpif_dbg(1, debug
, "Failed to set input\n");
1544 vid_ch
->input_idx
= index
;
1545 ch
->curr_subdev_info
= subdev_info
;
1546 ch
->curr_sd_index
= sd_index
;
1547 /* copy interface parameters to vpif */
1548 ch
->vpifparams
.iface
= subdev_info
->vpif_if
;
1550 /* update tvnorms from the sub device input info */
1551 ch
->video_dev
->tvnorms
= chan_cfg
->inputs
[index
].input
.std
;
1556 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1558 * @priv: file handle
1559 * @index: input index
1561 static int vpif_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1562 struct v4l2_fmtdesc
*fmt
)
1564 struct vpif_fh
*fh
= priv
;
1565 struct channel_obj
*ch
= fh
->channel
;
1567 if (fmt
->index
!= 0) {
1568 vpif_dbg(1, debug
, "Invalid format index\n");
1572 /* Fill in the information about format */
1573 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
) {
1574 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1575 strcpy(fmt
->description
, "Raw Mode -Bayer Pattern GrRBGb");
1576 fmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
1578 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1579 strcpy(fmt
->description
, "YCbCr4:2:2 YC Planar");
1580 fmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
1586 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1588 * @priv: file handle
1589 * @fmt: ptr to v4l2 format structure
1591 static int vpif_try_fmt_vid_cap(struct file
*file
, void *priv
,
1592 struct v4l2_format
*fmt
)
1594 struct vpif_fh
*fh
= priv
;
1595 struct channel_obj
*ch
= fh
->channel
;
1596 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
1598 return vpif_check_format(ch
, pixfmt
, 1);
1603 * vpif_g_fmt_vid_cap() - Set INPUT handler
1605 * @priv: file handle
1606 * @fmt: ptr to v4l2 format structure
1608 static int vpif_g_fmt_vid_cap(struct file
*file
, void *priv
,
1609 struct v4l2_format
*fmt
)
1611 struct vpif_fh
*fh
= priv
;
1612 struct channel_obj
*ch
= fh
->channel
;
1613 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1615 /* Check the validity of the buffer type */
1616 if (common
->fmt
.type
!= fmt
->type
)
1619 /* Fill in the information about format */
1625 * vpif_s_fmt_vid_cap() - Set FMT handler
1627 * @priv: file handle
1628 * @fmt: ptr to v4l2 format structure
1630 static int vpif_s_fmt_vid_cap(struct file
*file
, void *priv
,
1631 struct v4l2_format
*fmt
)
1633 struct vpif_fh
*fh
= priv
;
1634 struct channel_obj
*ch
= fh
->channel
;
1635 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1636 struct v4l2_pix_format
*pixfmt
;
1639 vpif_dbg(2, debug
, "%s\n", __func__
);
1641 /* If streaming is started, return error */
1642 if (common
->started
) {
1643 vpif_dbg(1, debug
, "Streaming is started\n");
1647 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1648 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1649 if (!fh
->initialized
) {
1650 vpif_dbg(1, debug
, "Channel Busy\n");
1655 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1659 fh
->initialized
= 1;
1661 pixfmt
= &fmt
->fmt
.pix
;
1662 /* Check for valid field format */
1663 ret
= vpif_check_format(ch
, pixfmt
, 0);
1667 /* store the format in the channel object */
1673 * vpif_querycap() - QUERYCAP handler
1675 * @priv: file handle
1676 * @cap: ptr to v4l2_capability structure
1678 static int vpif_querycap(struct file
*file
, void *priv
,
1679 struct v4l2_capability
*cap
)
1681 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1683 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1684 strlcpy(cap
->driver
, "vpif capture", sizeof(cap
->driver
));
1685 strlcpy(cap
->bus_info
, "VPIF Platform", sizeof(cap
->bus_info
));
1686 strlcpy(cap
->card
, config
->card_name
, sizeof(cap
->card
));
1692 * vpif_g_priority() - get priority handler
1694 * @priv: file handle
1695 * @prio: ptr to v4l2_priority structure
1697 static int vpif_g_priority(struct file
*file
, void *priv
,
1698 enum v4l2_priority
*prio
)
1700 struct vpif_fh
*fh
= priv
;
1701 struct channel_obj
*ch
= fh
->channel
;
1703 *prio
= v4l2_prio_max(&ch
->prio
);
1709 * vpif_s_priority() - set priority handler
1711 * @priv: file handle
1712 * @prio: ptr to v4l2_priority structure
1714 static int vpif_s_priority(struct file
*file
, void *priv
, enum v4l2_priority p
)
1716 struct vpif_fh
*fh
= priv
;
1717 struct channel_obj
*ch
= fh
->channel
;
1719 return v4l2_prio_change(&ch
->prio
, &fh
->prio
, p
);
1723 * vpif_cropcap() - cropcap handler
1725 * @priv: file handle
1726 * @crop: ptr to v4l2_cropcap structure
1728 static int vpif_cropcap(struct file
*file
, void *priv
,
1729 struct v4l2_cropcap
*crop
)
1731 struct vpif_fh
*fh
= priv
;
1732 struct channel_obj
*ch
= fh
->channel
;
1733 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1735 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= crop
->type
)
1738 crop
->bounds
.left
= 0;
1739 crop
->bounds
.top
= 0;
1740 crop
->bounds
.height
= common
->height
;
1741 crop
->bounds
.width
= common
->width
;
1742 crop
->defrect
= crop
->bounds
;
1747 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1749 * @priv: file handle
1750 * @preset: input preset
1752 static int vpif_enum_dv_presets(struct file
*file
, void *priv
,
1753 struct v4l2_dv_enum_preset
*preset
)
1755 struct vpif_fh
*fh
= priv
;
1756 struct channel_obj
*ch
= fh
->channel
;
1758 return v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
],
1759 video
, enum_dv_presets
, preset
);
1763 * vpif_query_dv_presets() - QUERY_DV_PRESET handler
1765 * @priv: file handle
1766 * @preset: input preset
1768 static int vpif_query_dv_preset(struct file
*file
, void *priv
,
1769 struct v4l2_dv_preset
*preset
)
1771 struct vpif_fh
*fh
= priv
;
1772 struct channel_obj
*ch
= fh
->channel
;
1774 return v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
],
1775 video
, query_dv_preset
, preset
);
1778 * vpif_s_dv_presets() - S_DV_PRESETS handler
1780 * @priv: file handle
1781 * @preset: input preset
1783 static int vpif_s_dv_preset(struct file
*file
, void *priv
,
1784 struct v4l2_dv_preset
*preset
)
1786 struct vpif_fh
*fh
= priv
;
1787 struct channel_obj
*ch
= fh
->channel
;
1788 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1791 if (common
->started
) {
1792 vpif_dbg(1, debug
, "streaming in progress\n");
1796 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1797 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1798 if (!fh
->initialized
) {
1799 vpif_dbg(1, debug
, "Channel Busy\n");
1804 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1808 fh
->initialized
= 1;
1810 /* Call encoder subdevice function to set the standard */
1811 if (mutex_lock_interruptible(&common
->lock
))
1812 return -ERESTARTSYS
;
1814 ch
->video
.dv_preset
= preset
->preset
;
1815 ch
->video
.stdid
= V4L2_STD_UNKNOWN
;
1816 memset(&ch
->video
.bt_timings
, 0, sizeof(ch
->video
.bt_timings
));
1818 /* Get the information about the standard */
1819 if (vpif_update_std_info(ch
)) {
1820 vpif_dbg(1, debug
, "Error getting the standard info\n");
1823 /* Configure the default format information */
1824 vpif_config_format(ch
);
1826 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
],
1827 video
, s_dv_preset
, preset
);
1830 mutex_unlock(&common
->lock
);
1835 * vpif_g_dv_presets() - G_DV_PRESETS handler
1837 * @priv: file handle
1838 * @preset: input preset
1840 static int vpif_g_dv_preset(struct file
*file
, void *priv
,
1841 struct v4l2_dv_preset
*preset
)
1843 struct vpif_fh
*fh
= priv
;
1844 struct channel_obj
*ch
= fh
->channel
;
1846 preset
->preset
= ch
->video
.dv_preset
;
1852 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1854 * @priv: file handle
1855 * @timings: digital video timings
1857 static int vpif_s_dv_timings(struct file
*file
, void *priv
,
1858 struct v4l2_dv_timings
*timings
)
1860 struct vpif_fh
*fh
= priv
;
1861 struct channel_obj
*ch
= fh
->channel
;
1862 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
1863 struct vpif_channel_config_params
*std_info
= &vpifparams
->std_info
;
1864 struct video_obj
*vid_ch
= &ch
->video
;
1865 struct v4l2_bt_timings
*bt
= &vid_ch
->bt_timings
;
1868 if (timings
->type
!= V4L2_DV_BT_656_1120
) {
1869 vpif_dbg(2, debug
, "Timing type not defined\n");
1873 /* Configure subdevice timings, if any */
1874 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
],
1875 video
, s_dv_timings
, timings
);
1876 if (ret
== -ENOIOCTLCMD
) {
1877 vpif_dbg(2, debug
, "Custom DV timings not supported by "
1882 vpif_dbg(2, debug
, "Error setting custom DV timings\n");
1886 if (!(timings
->bt
.width
&& timings
->bt
.height
&&
1887 (timings
->bt
.hbackporch
||
1888 timings
->bt
.hfrontporch
||
1889 timings
->bt
.hsync
) &&
1890 timings
->bt
.vfrontporch
&&
1891 (timings
->bt
.vbackporch
||
1892 timings
->bt
.vsync
))) {
1893 vpif_dbg(2, debug
, "Timings for width, height, "
1894 "horizontal back porch, horizontal sync, "
1895 "horizontal front porch, vertical back porch, "
1896 "vertical sync and vertical back porch "
1897 "must be defined\n");
1903 /* Configure video port timings */
1905 std_info
->eav2sav
= bt
->hbackporch
+ bt
->hfrontporch
+
1907 std_info
->sav2eav
= bt
->width
;
1910 std_info
->l3
= bt
->vsync
+ bt
->vbackporch
+ 1;
1912 if (bt
->interlaced
) {
1913 if (bt
->il_vbackporch
|| bt
->il_vfrontporch
|| bt
->il_vsync
) {
1914 std_info
->vsize
= bt
->height
* 2 +
1915 bt
->vfrontporch
+ bt
->vsync
+ bt
->vbackporch
+
1916 bt
->il_vfrontporch
+ bt
->il_vsync
+
1918 std_info
->l5
= std_info
->vsize
/2 -
1919 (bt
->vfrontporch
- 1);
1920 std_info
->l7
= std_info
->vsize
/2 + 1;
1921 std_info
->l9
= std_info
->l7
+ bt
->il_vsync
+
1922 bt
->il_vbackporch
+ 1;
1923 std_info
->l11
= std_info
->vsize
-
1924 (bt
->il_vfrontporch
- 1);
1926 vpif_dbg(2, debug
, "Required timing values for "
1927 "interlaced BT format missing\n");
1931 std_info
->vsize
= bt
->height
+ bt
->vfrontporch
+
1932 bt
->vsync
+ bt
->vbackporch
;
1933 std_info
->l5
= std_info
->vsize
- (bt
->vfrontporch
- 1);
1935 strncpy(std_info
->name
, "Custom timings BT656/1120", VPIF_MAX_NAME
);
1936 std_info
->width
= bt
->width
;
1937 std_info
->height
= bt
->height
;
1938 std_info
->frm_fmt
= bt
->interlaced
? 0 : 1;
1939 std_info
->ycmux_mode
= 0;
1940 std_info
->capture_format
= 0;
1941 std_info
->vbi_supported
= 0;
1942 std_info
->hd_sd
= 1;
1943 std_info
->stdid
= 0;
1944 std_info
->dv_preset
= V4L2_DV_INVALID
;
1947 vid_ch
->dv_preset
= V4L2_DV_INVALID
;
1952 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1954 * @priv: file handle
1955 * @timings: digital video timings
1957 static int vpif_g_dv_timings(struct file
*file
, void *priv
,
1958 struct v4l2_dv_timings
*timings
)
1960 struct vpif_fh
*fh
= priv
;
1961 struct channel_obj
*ch
= fh
->channel
;
1962 struct video_obj
*vid_ch
= &ch
->video
;
1963 struct v4l2_bt_timings
*bt
= &vid_ch
->bt_timings
;
1971 * vpif_g_chip_ident() - Identify the chip
1973 * @priv: file handle
1974 * @chip: chip identity
1976 * Returns zero or -EINVAL if read operations fails.
1978 static int vpif_g_chip_ident(struct file
*file
, void *priv
,
1979 struct v4l2_dbg_chip_ident
*chip
)
1981 chip
->ident
= V4L2_IDENT_NONE
;
1983 if (chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_DRIVER
&&
1984 chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
) {
1985 vpif_dbg(2, debug
, "match_type is invalid.\n");
1989 return v4l2_device_call_until_err(&vpif_obj
.v4l2_dev
, 0, core
,
1990 g_chip_ident
, chip
);
1993 #ifdef CONFIG_VIDEO_ADV_DEBUG
1995 * vpif_dbg_g_register() - Read register
1997 * @priv: file handle
1998 * @reg: register to be read
2001 * Returns zero or -EINVAL if read operations fails.
2003 static int vpif_dbg_g_register(struct file
*file
, void *priv
,
2004 struct v4l2_dbg_register
*reg
){
2005 struct vpif_fh
*fh
= priv
;
2006 struct channel_obj
*ch
= fh
->channel
;
2008 return v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], core
,
2013 * vpif_dbg_s_register() - Write to register
2015 * @priv: file handle
2016 * @reg: register to be modified
2019 * Returns zero or -EINVAL if write operations fails.
2021 static int vpif_dbg_s_register(struct file
*file
, void *priv
,
2022 struct v4l2_dbg_register
*reg
){
2023 struct vpif_fh
*fh
= priv
;
2024 struct channel_obj
*ch
= fh
->channel
;
2026 return v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], core
,
2032 * vpif_log_status() - Status information
2034 * @priv: file handle
2038 static int vpif_log_status(struct file
*filep
, void *priv
)
2040 /* status for sub devices */
2041 v4l2_device_call_all(&vpif_obj
.v4l2_dev
, 0, core
, log_status
);
2046 /* vpif capture ioctl operations */
2047 static const struct v4l2_ioctl_ops vpif_ioctl_ops
= {
2048 .vidioc_querycap
= vpif_querycap
,
2049 .vidioc_g_priority
= vpif_g_priority
,
2050 .vidioc_s_priority
= vpif_s_priority
,
2051 .vidioc_enum_fmt_vid_cap
= vpif_enum_fmt_vid_cap
,
2052 .vidioc_g_fmt_vid_cap
= vpif_g_fmt_vid_cap
,
2053 .vidioc_s_fmt_vid_cap
= vpif_s_fmt_vid_cap
,
2054 .vidioc_try_fmt_vid_cap
= vpif_try_fmt_vid_cap
,
2055 .vidioc_enum_input
= vpif_enum_input
,
2056 .vidioc_s_input
= vpif_s_input
,
2057 .vidioc_g_input
= vpif_g_input
,
2058 .vidioc_reqbufs
= vpif_reqbufs
,
2059 .vidioc_querybuf
= vpif_querybuf
,
2060 .vidioc_querystd
= vpif_querystd
,
2061 .vidioc_s_std
= vpif_s_std
,
2062 .vidioc_g_std
= vpif_g_std
,
2063 .vidioc_qbuf
= vpif_qbuf
,
2064 .vidioc_dqbuf
= vpif_dqbuf
,
2065 .vidioc_streamon
= vpif_streamon
,
2066 .vidioc_streamoff
= vpif_streamoff
,
2067 .vidioc_cropcap
= vpif_cropcap
,
2068 .vidioc_enum_dv_presets
= vpif_enum_dv_presets
,
2069 .vidioc_s_dv_preset
= vpif_s_dv_preset
,
2070 .vidioc_g_dv_preset
= vpif_g_dv_preset
,
2071 .vidioc_query_dv_preset
= vpif_query_dv_preset
,
2072 .vidioc_s_dv_timings
= vpif_s_dv_timings
,
2073 .vidioc_g_dv_timings
= vpif_g_dv_timings
,
2074 .vidioc_g_chip_ident
= vpif_g_chip_ident
,
2075 #ifdef CONFIG_VIDEO_ADV_DEBUG
2076 .vidioc_g_register
= vpif_dbg_g_register
,
2077 .vidioc_s_register
= vpif_dbg_s_register
,
2079 .vidioc_log_status
= vpif_log_status
,
2082 /* vpif file operations */
2083 static struct v4l2_file_operations vpif_fops
= {
2084 .owner
= THIS_MODULE
,
2086 .release
= vpif_release
,
2087 .unlocked_ioctl
= video_ioctl2
,
2092 /* vpif video template */
2093 static struct video_device vpif_video_template
= {
2097 .ioctl_ops
= &vpif_ioctl_ops
,
2101 * initialize_vpif() - Initialize vpif data structures
2103 * Allocate memory for data structures and initialize them
2105 static int initialize_vpif(void)
2108 int free_channel_objects_index
;
2110 /* Default number of buffers should be 3 */
2111 if ((ch0_numbuffers
> 0) &&
2112 (ch0_numbuffers
< config_params
.min_numbuffers
))
2113 ch0_numbuffers
= config_params
.min_numbuffers
;
2114 if ((ch1_numbuffers
> 0) &&
2115 (ch1_numbuffers
< config_params
.min_numbuffers
))
2116 ch1_numbuffers
= config_params
.min_numbuffers
;
2118 /* Set buffer size to min buffers size if it is invalid */
2119 if (ch0_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
])
2121 config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
];
2122 if (ch1_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
])
2124 config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
];
2126 config_params
.numbuffers
[VPIF_CHANNEL0_VIDEO
] = ch0_numbuffers
;
2127 config_params
.numbuffers
[VPIF_CHANNEL1_VIDEO
] = ch1_numbuffers
;
2128 if (ch0_numbuffers
) {
2129 config_params
.channel_bufsize
[VPIF_CHANNEL0_VIDEO
]
2132 if (ch1_numbuffers
) {
2133 config_params
.channel_bufsize
[VPIF_CHANNEL1_VIDEO
]
2137 /* Allocate memory for six channel objects */
2138 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
2140 kzalloc(sizeof(*vpif_obj
.dev
[i
]), GFP_KERNEL
);
2141 /* If memory allocation fails, return error */
2142 if (!vpif_obj
.dev
[i
]) {
2143 free_channel_objects_index
= i
;
2145 goto vpif_init_free_channel_objects
;
2150 vpif_init_free_channel_objects
:
2151 for (j
= 0; j
< free_channel_objects_index
; j
++)
2152 kfree(vpif_obj
.dev
[j
]);
2157 * vpif_probe : This function probes the vpif capture driver
2158 * @pdev: platform device pointer
2160 * This creates device entries by register itself to the V4L2 driver and
2161 * initializes fields of each channel objects
2163 static __init
int vpif_probe(struct platform_device
*pdev
)
2165 struct vpif_subdev_info
*subdevdata
;
2166 struct vpif_capture_config
*config
;
2167 int i
, j
, k
, m
, q
, err
;
2168 struct i2c_adapter
*i2c_adap
;
2169 struct channel_obj
*ch
;
2170 struct common_obj
*common
;
2171 struct video_device
*vfd
;
2172 struct resource
*res
;
2175 vpif_dev
= &pdev
->dev
;
2177 err
= initialize_vpif();
2179 v4l2_err(vpif_dev
->driver
, "Error initializing vpif\n");
2183 err
= v4l2_device_register(vpif_dev
, &vpif_obj
.v4l2_dev
);
2185 v4l2_err(vpif_dev
->driver
, "Error registering v4l2 device\n");
2190 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, k
))) {
2191 for (i
= res
->start
; i
<= res
->end
; i
++) {
2192 if (request_irq(i
, vpif_channel_isr
, IRQF_SHARED
,
2194 (void *)(&vpif_obj
.dev
[k
]->channel_id
))) {
2203 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
2204 /* Get the pointer to the channel object */
2205 ch
= vpif_obj
.dev
[i
];
2206 /* Allocate memory for video device */
2207 vfd
= video_device_alloc();
2209 for (j
= 0; j
< i
; j
++) {
2210 ch
= vpif_obj
.dev
[j
];
2211 video_device_release(ch
->video_dev
);
2214 goto vpif_dev_alloc_err
;
2217 /* Initialize field of video device */
2218 *vfd
= vpif_video_template
;
2219 vfd
->v4l2_dev
= &vpif_obj
.v4l2_dev
;
2220 vfd
->release
= video_device_release
;
2221 snprintf(vfd
->name
, sizeof(vfd
->name
),
2222 "VPIF_Capture_DRIVER_V%s",
2223 VPIF_CAPTURE_VERSION
);
2224 /* Set video_dev to the video device */
2225 ch
->video_dev
= vfd
;
2228 for (j
= 0; j
< VPIF_CAPTURE_MAX_DEVICES
; j
++) {
2229 ch
= vpif_obj
.dev
[j
];
2231 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
2232 spin_lock_init(&common
->irqlock
);
2233 mutex_init(&common
->lock
);
2234 /* Locking in file operations other than ioctl should be done
2235 by the driver, not the V4L2 core.
2236 This driver needs auditing so that this flag can be removed. */
2237 set_bit(V4L2_FL_LOCK_ALL_FOPS
, &ch
->video_dev
->flags
);
2238 ch
->video_dev
->lock
= &common
->lock
;
2239 /* Initialize prio member of channel object */
2240 v4l2_prio_init(&ch
->prio
);
2241 err
= video_register_device(ch
->video_dev
,
2242 VFL_TYPE_GRABBER
, (j
? 1 : 0));
2246 video_set_drvdata(ch
->video_dev
, ch
);
2250 i2c_adap
= i2c_get_adapter(1);
2251 config
= pdev
->dev
.platform_data
;
2253 subdev_count
= config
->subdev_count
;
2254 vpif_obj
.sd
= kzalloc(sizeof(struct v4l2_subdev
*) * subdev_count
,
2256 if (vpif_obj
.sd
== NULL
) {
2257 vpif_err("unable to allocate memory for subdevice pointers\n");
2262 for (i
= 0; i
< subdev_count
; i
++) {
2263 subdevdata
= &config
->subdev_info
[i
];
2265 v4l2_i2c_new_subdev_board(&vpif_obj
.v4l2_dev
,
2267 &subdevdata
->board_info
,
2270 if (!vpif_obj
.sd
[i
]) {
2271 vpif_err("Error registering v4l2 subdevice\n");
2272 goto probe_subdev_out
;
2274 v4l2_info(&vpif_obj
.v4l2_dev
, "registered sub device %s\n",
2278 vpif_obj
.sd
[i
]->grp_id
= 1 << i
;
2281 v4l2_info(&vpif_obj
.v4l2_dev
, "VPIF capture driver initialized\n");
2285 /* free sub devices memory */
2288 j
= VPIF_CAPTURE_MAX_DEVICES
;
2290 for (k
= 0; k
< j
; k
++) {
2291 /* Get the pointer to the channel object */
2292 ch
= vpif_obj
.dev
[k
];
2293 /* Unregister video device */
2294 video_unregister_device(ch
->video_dev
);
2298 k
= VPIF_CAPTURE_MAX_DEVICES
-1;
2299 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, k
);
2303 for (q
= k
; q
>= 0; q
--) {
2304 for (m
= i
; m
>= (int)res
->start
; m
--)
2305 free_irq(m
, (void *)(&vpif_obj
.dev
[q
]->channel_id
));
2307 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, q
-1);
2311 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
2316 * vpif_remove() - driver remove handler
2317 * @device: ptr to platform device structure
2319 * The vidoe device is unregistered
2321 static int vpif_remove(struct platform_device
*device
)
2324 struct channel_obj
*ch
;
2326 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
2328 /* un-register device */
2329 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
2330 /* Get the pointer to the channel object */
2331 ch
= vpif_obj
.dev
[i
];
2332 /* Unregister video device */
2333 video_unregister_device(ch
->video_dev
);
2339 * vpif_suspend: vpif device suspend
2341 * TODO: Add suspend code here
2344 vpif_suspend(struct device
*dev
)
2350 * vpif_resume: vpif device suspend
2352 * TODO: Add resume code here
2355 vpif_resume(struct device
*dev
)
2360 static const struct dev_pm_ops vpif_dev_pm_ops
= {
2361 .suspend
= vpif_suspend
,
2362 .resume
= vpif_resume
,
2365 static __refdata
struct platform_driver vpif_driver
= {
2367 .name
= "vpif_capture",
2368 .owner
= THIS_MODULE
,
2369 .pm
= &vpif_dev_pm_ops
,
2371 .probe
= vpif_probe
,
2372 .remove
= vpif_remove
,
2376 * vpif_init: initialize the vpif driver
2378 * This function registers device and driver to the kernel, requests irq
2379 * handler and allocates memory
2380 * for channel objects
2382 static __init
int vpif_init(void)
2384 return platform_driver_register(&vpif_driver
);
2388 * vpif_cleanup : This function clean up the vpif capture resources
2390 * This will un-registers device and driver to the kernel, frees
2391 * requested irq handler and de-allocates memory allocated for channel
2394 static void vpif_cleanup(void)
2396 struct platform_device
*pdev
;
2397 struct resource
*res
;
2401 pdev
= container_of(vpif_dev
, struct platform_device
, dev
);
2402 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
))) {
2403 for (irq_num
= res
->start
; irq_num
<= res
->end
; irq_num
++)
2405 (void *)(&vpif_obj
.dev
[i
]->channel_id
));
2409 platform_driver_unregister(&vpif_driver
);
2412 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++)
2413 kfree(vpif_obj
.dev
[i
]);
2416 /* Function for module initialization and cleanup */
2417 module_init(vpif_init
);
2418 module_exit(vpif_cleanup
);