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
;
83 static void vpif_calculate_offsets(struct channel_obj
*ch
);
84 static void vpif_config_addr(struct channel_obj
*ch
, int muxmode
);
87 * buffer_prepare : callback function for buffer prepare
88 * @vb: ptr to vb2_buffer
90 * This is the callback function for buffer prepare when vb2_qbuf()
91 * function is called. The buffer is prepared and user space virtual address
92 * or user address is converted into physical address
94 static int vpif_buffer_prepare(struct vb2_buffer
*vb
)
96 /* Get the file handle object and channel object */
97 struct vpif_fh
*fh
= vb2_get_drv_priv(vb
->vb2_queue
);
98 struct vb2_queue
*q
= vb
->vb2_queue
;
99 struct channel_obj
*ch
= fh
->channel
;
100 struct common_obj
*common
;
103 vpif_dbg(2, debug
, "vpif_buffer_prepare\n");
105 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
107 if (vb
->state
!= VB2_BUF_STATE_ACTIVE
&&
108 vb
->state
!= VB2_BUF_STATE_PREPARED
) {
109 vb2_set_plane_payload(vb
, 0, common
->fmt
.fmt
.pix
.sizeimage
);
110 if (vb2_plane_vaddr(vb
, 0) &&
111 vb2_get_plane_payload(vb
, 0) > vb2_plane_size(vb
, 0))
113 addr
= vb2_dma_contig_plane_dma_addr(vb
, 0);
116 if (!IS_ALIGNED((addr
+ common
->ytop_off
), 8) ||
117 !IS_ALIGNED((addr
+ common
->ybtm_off
), 8) ||
118 !IS_ALIGNED((addr
+ common
->ctop_off
), 8) ||
119 !IS_ALIGNED((addr
+ common
->cbtm_off
), 8))
125 vpif_dbg(1, debug
, "buffer_prepare:offset is not aligned to 8 bytes\n");
130 * vpif_buffer_queue_setup : Callback function for buffer setup.
133 * @nbuffers: ptr to number of buffers requested by application
134 * @nplanes:: contains number of distinct video planes needed to hold a frame
135 * @sizes[]: contains the size (in bytes) of each plane.
136 * @alloc_ctxs: ptr to allocation context
138 * This callback function is called when reqbuf() is called to adjust
139 * the buffer count and buffer size
141 static int vpif_buffer_queue_setup(struct vb2_queue
*vq
,
142 const struct v4l2_format
*fmt
,
143 unsigned int *nbuffers
, unsigned int *nplanes
,
144 unsigned int sizes
[], void *alloc_ctxs
[])
146 /* Get the file handle object and channel object */
147 struct vpif_fh
*fh
= vb2_get_drv_priv(vq
);
148 struct channel_obj
*ch
= fh
->channel
;
149 struct common_obj
*common
;
152 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
154 vpif_dbg(2, debug
, "vpif_buffer_setup\n");
156 /* If memory type is not mmap, return */
157 if (V4L2_MEMORY_MMAP
== common
->memory
) {
158 /* Calculate the size of the buffer */
159 size
= config_params
.channel_bufsize
[ch
->channel_id
];
161 * Checking if the buffer size exceeds the available buffer
162 * ycmux_mode = 0 means 1 channel mode HD and
163 * ycmux_mode = 1 means 2 channels mode SD
165 if (ch
->vpifparams
.std_info
.ycmux_mode
== 0) {
166 if (config_params
.video_limit
[ch
->channel_id
])
167 while (size
* *nbuffers
>
168 (config_params
.video_limit
[0]
169 + config_params
.video_limit
[1]))
172 if (config_params
.video_limit
[ch
->channel_id
])
173 while (size
* *nbuffers
>
174 config_params
.video_limit
[ch
->channel_id
])
179 size
= common
->fmt
.fmt
.pix
.sizeimage
;
182 if (*nbuffers
< config_params
.min_numbuffers
)
183 *nbuffers
= config_params
.min_numbuffers
;
187 alloc_ctxs
[0] = common
->alloc_ctx
;
193 * vpif_buffer_queue : Callback function to add buffer to DMA queue
194 * @vb: ptr to vb2_buffer
196 static void vpif_buffer_queue(struct vb2_buffer
*vb
)
198 /* Get the file handle object and channel object */
199 struct vpif_fh
*fh
= vb2_get_drv_priv(vb
->vb2_queue
);
200 struct channel_obj
*ch
= fh
->channel
;
201 struct vpif_cap_buffer
*buf
= container_of(vb
,
202 struct vpif_cap_buffer
, vb
);
203 struct common_obj
*common
;
205 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
207 vpif_dbg(2, debug
, "vpif_buffer_queue\n");
209 /* add the buffer to the DMA queue */
210 list_add_tail(&buf
->list
, &common
->dma_queue
);
214 * vpif_buf_cleanup : Callback function to free buffer
215 * @vb: ptr to vb2_buffer
217 * This function is called from the videobuf2 layer to free memory
218 * allocated to the buffers
220 static void vpif_buf_cleanup(struct vb2_buffer
*vb
)
222 /* Get the file handle object and channel object */
223 struct vpif_fh
*fh
= vb2_get_drv_priv(vb
->vb2_queue
);
224 struct vpif_cap_buffer
*buf
= container_of(vb
,
225 struct vpif_cap_buffer
, vb
);
226 struct channel_obj
*ch
= fh
->channel
;
227 struct common_obj
*common
;
230 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
232 spin_lock_irqsave(&common
->irqlock
, flags
);
233 if (vb
->state
== VB2_BUF_STATE_ACTIVE
)
234 list_del_init(&buf
->list
);
235 spin_unlock_irqrestore(&common
->irqlock
, flags
);
239 static void vpif_wait_prepare(struct vb2_queue
*vq
)
241 struct vpif_fh
*fh
= vb2_get_drv_priv(vq
);
242 struct channel_obj
*ch
= fh
->channel
;
243 struct common_obj
*common
;
245 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
246 mutex_unlock(&common
->lock
);
249 static void vpif_wait_finish(struct vb2_queue
*vq
)
251 struct vpif_fh
*fh
= vb2_get_drv_priv(vq
);
252 struct channel_obj
*ch
= fh
->channel
;
253 struct common_obj
*common
;
255 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
256 mutex_lock(&common
->lock
);
259 static int vpif_buffer_init(struct vb2_buffer
*vb
)
261 struct vpif_cap_buffer
*buf
= container_of(vb
,
262 struct vpif_cap_buffer
, vb
);
264 INIT_LIST_HEAD(&buf
->list
);
269 static u8 channel_first_int
[VPIF_NUMBER_OF_OBJECTS
][2] =
272 static int vpif_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
274 struct vpif_capture_config
*vpif_config_data
=
275 vpif_dev
->platform_data
;
276 struct vpif_fh
*fh
= vb2_get_drv_priv(vq
);
277 struct channel_obj
*ch
= fh
->channel
;
278 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
279 struct vpif_params
*vpif
= &ch
->vpifparams
;
280 unsigned long addr
= 0;
283 /* If buffer queue is empty, return error */
284 if (list_empty(&common
->dma_queue
)) {
285 vpif_dbg(1, debug
, "buffer queue is empty\n");
289 /* Get the next frame from the buffer queue */
290 common
->cur_frm
= common
->next_frm
= list_entry(common
->dma_queue
.next
,
291 struct vpif_cap_buffer
, list
);
292 /* Remove buffer from the buffer queue */
293 list_del(&common
->cur_frm
->list
);
294 /* Mark state of the current frame to active */
295 common
->cur_frm
->vb
.state
= VB2_BUF_STATE_ACTIVE
;
296 /* Initialize field_id and started member */
299 addr
= vb2_dma_contig_plane_dma_addr(&common
->cur_frm
->vb
, 0);
301 /* Calculate the offset for Y and C data in the buffer */
302 vpif_calculate_offsets(ch
);
304 if ((vpif
->std_info
.frm_fmt
&&
305 ((common
->fmt
.fmt
.pix
.field
!= V4L2_FIELD_NONE
) &&
306 (common
->fmt
.fmt
.pix
.field
!= V4L2_FIELD_ANY
))) ||
307 (!vpif
->std_info
.frm_fmt
&&
308 (common
->fmt
.fmt
.pix
.field
== V4L2_FIELD_NONE
))) {
309 vpif_dbg(1, debug
, "conflict in field format and std format\n");
313 /* configure 1 or 2 channel mode */
314 ret
= vpif_config_data
->setup_input_channel_mode
315 (vpif
->std_info
.ycmux_mode
);
318 vpif_dbg(1, debug
, "can't set vpif channel mode\n");
322 /* Call vpif_set_params function to set the parameters and addresses */
323 ret
= vpif_set_video_params(vpif
, ch
->channel_id
);
326 vpif_dbg(1, debug
, "can't set video params\n");
330 common
->started
= ret
;
331 vpif_config_addr(ch
, ret
);
333 common
->set_addr(addr
+ common
->ytop_off
,
334 addr
+ common
->ybtm_off
,
335 addr
+ common
->ctop_off
,
336 addr
+ common
->cbtm_off
);
339 * Set interrupt for both the fields in VPIF Register enable channel in
342 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
)) {
343 channel0_intr_assert();
344 channel0_intr_enable(1);
347 if ((VPIF_CHANNEL1_VIDEO
== ch
->channel_id
) ||
348 (common
->started
== 2)) {
349 channel1_intr_assert();
350 channel1_intr_enable(1);
353 channel_first_int
[VPIF_VIDEO_INDEX
][ch
->channel_id
] = 1;
358 /* abort streaming and wait for last buffer */
359 static int vpif_stop_streaming(struct vb2_queue
*vq
)
361 struct vpif_fh
*fh
= vb2_get_drv_priv(vq
);
362 struct channel_obj
*ch
= fh
->channel
;
363 struct common_obj
*common
;
365 if (!vb2_is_streaming(vq
))
368 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
370 /* release all active buffers */
371 while (!list_empty(&common
->dma_queue
)) {
372 common
->next_frm
= list_entry(common
->dma_queue
.next
,
373 struct vpif_cap_buffer
, list
);
374 list_del(&common
->next_frm
->list
);
375 vb2_buffer_done(&common
->next_frm
->vb
, VB2_BUF_STATE_ERROR
);
381 static struct vb2_ops video_qops
= {
382 .queue_setup
= vpif_buffer_queue_setup
,
383 .wait_prepare
= vpif_wait_prepare
,
384 .wait_finish
= vpif_wait_finish
,
385 .buf_init
= vpif_buffer_init
,
386 .buf_prepare
= vpif_buffer_prepare
,
387 .start_streaming
= vpif_start_streaming
,
388 .stop_streaming
= vpif_stop_streaming
,
389 .buf_cleanup
= vpif_buf_cleanup
,
390 .buf_queue
= vpif_buffer_queue
,
394 * vpif_process_buffer_complete: process a completed buffer
395 * @common: ptr to common channel object
397 * This function time stamp the buffer and mark it as DONE. It also
398 * wake up any process waiting on the QUEUE and set the next buffer
401 static void vpif_process_buffer_complete(struct common_obj
*common
)
403 do_gettimeofday(&common
->cur_frm
->vb
.v4l2_buf
.timestamp
);
404 vb2_buffer_done(&common
->cur_frm
->vb
,
406 /* Make curFrm pointing to nextFrm */
407 common
->cur_frm
= common
->next_frm
;
411 * vpif_schedule_next_buffer: set next buffer address for capture
412 * @common : ptr to common channel object
414 * This function will get next buffer from the dma queue and
415 * set the buffer address in the vpif register for capture.
416 * the buffer is marked active
418 static void vpif_schedule_next_buffer(struct common_obj
*common
)
420 unsigned long addr
= 0;
422 common
->next_frm
= list_entry(common
->dma_queue
.next
,
423 struct vpif_cap_buffer
, list
);
424 /* Remove that buffer from the buffer queue */
425 list_del(&common
->next_frm
->list
);
426 common
->next_frm
->vb
.state
= VB2_BUF_STATE_ACTIVE
;
427 addr
= vb2_dma_contig_plane_dma_addr(&common
->next_frm
->vb
, 0);
429 /* Set top and bottom field addresses in VPIF registers */
430 common
->set_addr(addr
+ common
->ytop_off
,
431 addr
+ common
->ybtm_off
,
432 addr
+ common
->ctop_off
,
433 addr
+ common
->cbtm_off
);
437 * vpif_channel_isr : ISR handler for vpif capture
439 * @dev_id: dev_id ptr
441 * It changes status of the captured buffer, takes next buffer from the queue
442 * and sets its address in VPIF registers
444 static irqreturn_t
vpif_channel_isr(int irq
, void *dev_id
)
446 struct vpif_device
*dev
= &vpif_obj
;
447 struct common_obj
*common
;
448 struct channel_obj
*ch
;
449 enum v4l2_field field
;
453 channel_id
= *(int *)(dev_id
);
454 if (!vpif_intr_status(channel_id
))
457 ch
= dev
->dev
[channel_id
];
459 field
= ch
->common
[VPIF_VIDEO_INDEX
].fmt
.fmt
.pix
.field
;
461 for (i
= 0; i
< VPIF_NUMBER_OF_OBJECTS
; i
++) {
462 common
= &ch
->common
[i
];
463 /* skip If streaming is not started in this channel */
464 if (0 == common
->started
)
467 /* Check the field format */
468 if (1 == ch
->vpifparams
.std_info
.frm_fmt
) {
469 /* Progressive mode */
470 if (list_empty(&common
->dma_queue
))
473 if (!channel_first_int
[i
][channel_id
])
474 vpif_process_buffer_complete(common
);
476 channel_first_int
[i
][channel_id
] = 0;
478 vpif_schedule_next_buffer(common
);
481 channel_first_int
[i
][channel_id
] = 0;
484 * Interlaced mode. If it is first interrupt, ignore
487 if (channel_first_int
[i
][channel_id
]) {
488 channel_first_int
[i
][channel_id
] = 0;
493 /* Get field id from VPIF registers */
494 fid
= vpif_channel_getfid(ch
->channel_id
);
495 if (fid
!= ch
->field_id
) {
497 * If field id does not match stored
498 * field id, make them in sync
505 /* device field id and local field id are in sync */
507 /* this is even field */
508 if (common
->cur_frm
== common
->next_frm
)
511 /* mark the current buffer as done */
512 vpif_process_buffer_complete(common
);
513 } else if (1 == fid
) {
515 if (list_empty(&common
->dma_queue
) ||
516 (common
->cur_frm
!= common
->next_frm
))
519 vpif_schedule_next_buffer(common
);
527 * vpif_update_std_info() - update standard related info
528 * @ch: ptr to channel object
530 * For a given standard selected by application, update values
531 * in the device data structures
533 static int vpif_update_std_info(struct channel_obj
*ch
)
535 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
536 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
537 const struct vpif_channel_config_params
*config
;
538 struct vpif_channel_config_params
*std_info
= &vpifparams
->std_info
;
539 struct video_obj
*vid_ch
= &ch
->video
;
542 vpif_dbg(2, debug
, "vpif_update_std_info\n");
544 for (index
= 0; index
< vpif_ch_params_count
; index
++) {
545 config
= &ch_params
[index
];
546 if (config
->hd_sd
== 0) {
547 vpif_dbg(2, debug
, "SD format\n");
548 if (config
->stdid
& vid_ch
->stdid
) {
549 memcpy(std_info
, config
, sizeof(*config
));
553 vpif_dbg(2, debug
, "HD format\n");
554 if (config
->dv_preset
== vid_ch
->dv_preset
) {
555 memcpy(std_info
, config
, sizeof(*config
));
561 /* standard not found */
562 if (index
== vpif_ch_params_count
)
565 common
->fmt
.fmt
.pix
.width
= std_info
->width
;
566 common
->width
= std_info
->width
;
567 common
->fmt
.fmt
.pix
.height
= std_info
->height
;
568 common
->height
= std_info
->height
;
569 common
->fmt
.fmt
.pix
.bytesperline
= std_info
->width
;
570 vpifparams
->video_params
.hpitch
= std_info
->width
;
571 vpifparams
->video_params
.storage_mode
= std_info
->frm_fmt
;
577 * vpif_calculate_offsets : This function calculates buffers offsets
578 * @ch : ptr to channel object
580 * This function calculates buffer offsets for Y and C in the top and
583 static void vpif_calculate_offsets(struct channel_obj
*ch
)
585 unsigned int hpitch
, vpitch
, sizeimage
;
586 struct video_obj
*vid_ch
= &(ch
->video
);
587 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
588 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
589 enum v4l2_field field
= common
->fmt
.fmt
.pix
.field
;
591 vpif_dbg(2, debug
, "vpif_calculate_offsets\n");
593 if (V4L2_FIELD_ANY
== field
) {
594 if (vpifparams
->std_info
.frm_fmt
)
595 vid_ch
->buf_field
= V4L2_FIELD_NONE
;
597 vid_ch
->buf_field
= V4L2_FIELD_INTERLACED
;
599 vid_ch
->buf_field
= common
->fmt
.fmt
.pix
.field
;
601 sizeimage
= common
->fmt
.fmt
.pix
.sizeimage
;
603 hpitch
= common
->fmt
.fmt
.pix
.bytesperline
;
604 vpitch
= sizeimage
/ (hpitch
* 2);
606 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
607 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
)) {
608 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
609 common
->ytop_off
= 0;
610 common
->ybtm_off
= hpitch
;
611 common
->ctop_off
= sizeimage
/ 2;
612 common
->cbtm_off
= sizeimage
/ 2 + hpitch
;
613 } else if (V4L2_FIELD_SEQ_TB
== vid_ch
->buf_field
) {
614 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
615 common
->ytop_off
= 0;
616 common
->ybtm_off
= sizeimage
/ 4;
617 common
->ctop_off
= sizeimage
/ 2;
618 common
->cbtm_off
= common
->ctop_off
+ sizeimage
/ 4;
619 } else if (V4L2_FIELD_SEQ_BT
== vid_ch
->buf_field
) {
620 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
621 common
->ybtm_off
= 0;
622 common
->ytop_off
= sizeimage
/ 4;
623 common
->cbtm_off
= sizeimage
/ 2;
624 common
->ctop_off
= common
->cbtm_off
+ sizeimage
/ 4;
626 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
627 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
))
628 vpifparams
->video_params
.storage_mode
= 1;
630 vpifparams
->video_params
.storage_mode
= 0;
632 if (1 == vpifparams
->std_info
.frm_fmt
)
633 vpifparams
->video_params
.hpitch
=
634 common
->fmt
.fmt
.pix
.bytesperline
;
636 if ((field
== V4L2_FIELD_ANY
)
637 || (field
== V4L2_FIELD_INTERLACED
))
638 vpifparams
->video_params
.hpitch
=
639 common
->fmt
.fmt
.pix
.bytesperline
* 2;
641 vpifparams
->video_params
.hpitch
=
642 common
->fmt
.fmt
.pix
.bytesperline
;
645 ch
->vpifparams
.video_params
.stdid
= vpifparams
->std_info
.stdid
;
649 * vpif_config_format: configure default frame format in the device
650 * ch : ptr to channel object
652 static void vpif_config_format(struct channel_obj
*ch
)
654 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
656 vpif_dbg(2, debug
, "vpif_config_format\n");
658 common
->fmt
.fmt
.pix
.field
= V4L2_FIELD_ANY
;
659 if (config_params
.numbuffers
[ch
->channel_id
] == 0)
660 common
->memory
= V4L2_MEMORY_USERPTR
;
662 common
->memory
= V4L2_MEMORY_MMAP
;
664 common
->fmt
.fmt
.pix
.sizeimage
665 = config_params
.channel_bufsize
[ch
->channel_id
];
667 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
)
668 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
670 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUV422P
;
671 common
->fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
675 * vpif_get_default_field() - Get default field type based on interface
676 * @vpif_params - ptr to vpif params
678 static inline enum v4l2_field
vpif_get_default_field(
679 struct vpif_interface
*iface
)
681 return (iface
->if_type
== VPIF_IF_RAW_BAYER
) ? V4L2_FIELD_NONE
:
682 V4L2_FIELD_INTERLACED
;
686 * vpif_check_format() - check given pixel format for compatibility
688 * @pixfmt - Given pixel format
689 * @update - update the values as per hardware requirement
691 * Check the application pixel format for S_FMT and update the input
692 * values as per hardware limits for TRY_FMT. The default pixel and
693 * field format is selected based on interface type.
695 static int vpif_check_format(struct channel_obj
*ch
,
696 struct v4l2_pix_format
*pixfmt
,
699 struct common_obj
*common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
700 struct vpif_params
*vpif_params
= &ch
->vpifparams
;
701 enum v4l2_field field
= pixfmt
->field
;
702 u32 sizeimage
, hpitch
, vpitch
;
705 vpif_dbg(2, debug
, "vpif_check_format\n");
707 * first check for the pixel format. If if_type is Raw bayer,
708 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
709 * V4L2_PIX_FMT_YUV422P is supported
711 if (vpif_params
->iface
.if_type
== VPIF_IF_RAW_BAYER
) {
712 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
) {
714 vpif_dbg(2, debug
, "invalid pix format\n");
717 pixfmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
720 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_YUV422P
) {
722 vpif_dbg(2, debug
, "invalid pixel format\n");
725 pixfmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
729 if (!(VPIF_VALID_FIELD(field
))) {
731 vpif_dbg(2, debug
, "invalid field format\n");
735 * By default use FIELD_NONE for RAW Bayer capture
736 * and FIELD_INTERLACED for other interfaces
738 field
= vpif_get_default_field(&vpif_params
->iface
);
739 } else if (field
== V4L2_FIELD_ANY
)
740 /* unsupported field. Use default */
741 field
= vpif_get_default_field(&vpif_params
->iface
);
743 /* validate the hpitch */
744 hpitch
= pixfmt
->bytesperline
;
745 if (hpitch
< vpif_params
->std_info
.width
) {
747 vpif_dbg(2, debug
, "invalid hpitch\n");
750 hpitch
= vpif_params
->std_info
.width
;
753 sizeimage
= pixfmt
->sizeimage
;
755 vpitch
= sizeimage
/ (hpitch
* 2);
757 /* validate the vpitch */
758 if (vpitch
< vpif_params
->std_info
.height
) {
760 vpif_dbg(2, debug
, "Invalid vpitch\n");
763 vpitch
= vpif_params
->std_info
.height
;
766 /* Check for 8 byte alignment */
767 if (!ALIGN(hpitch
, 8)) {
769 vpif_dbg(2, debug
, "invalid pitch alignment\n");
772 /* adjust to next 8 byte boundary */
773 hpitch
= (((hpitch
+ 7) / 8) * 8);
775 /* if update is set, modify the bytesperline and sizeimage */
777 pixfmt
->bytesperline
= hpitch
;
778 pixfmt
->sizeimage
= hpitch
* vpitch
* 2;
781 * Image width and height is always based on current standard width and
784 pixfmt
->width
= common
->fmt
.fmt
.pix
.width
;
785 pixfmt
->height
= common
->fmt
.fmt
.pix
.height
;
792 * vpif_config_addr() - function to configure buffer address in vpif
794 * @muxmode - channel mux mode
796 static void vpif_config_addr(struct channel_obj
*ch
, int muxmode
)
798 struct common_obj
*common
;
800 vpif_dbg(2, debug
, "vpif_config_addr\n");
802 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
804 if (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)
805 common
->set_addr
= ch1_set_videobuf_addr
;
806 else if (2 == muxmode
)
807 common
->set_addr
= ch0_set_videobuf_addr_yc_nmux
;
809 common
->set_addr
= ch0_set_videobuf_addr
;
813 * vpif_mmap : It is used to map kernel space buffers into user spaces
814 * @filep: file pointer
815 * @vma: ptr to vm_area_struct
817 static int vpif_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
819 /* Get the channel object and file handle object */
820 struct vpif_fh
*fh
= filep
->private_data
;
821 struct channel_obj
*ch
= fh
->channel
;
822 struct common_obj
*common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
824 vpif_dbg(2, debug
, "vpif_mmap\n");
826 return vb2_mmap(&common
->buffer_queue
, vma
);
830 * vpif_poll: It is used for select/poll system call
831 * @filep: file pointer
832 * @wait: poll table to wait
834 static unsigned int vpif_poll(struct file
*filep
, poll_table
* wait
)
836 struct vpif_fh
*fh
= filep
->private_data
;
837 struct channel_obj
*channel
= fh
->channel
;
838 struct common_obj
*common
= &(channel
->common
[VPIF_VIDEO_INDEX
]);
840 vpif_dbg(2, debug
, "vpif_poll\n");
843 return vb2_poll(&common
->buffer_queue
, filep
, wait
);
848 * vpif_open : vpif open handler
851 * It creates object of file handle structure and stores it in private_data
852 * member of filepointer
854 static int vpif_open(struct file
*filep
)
856 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
857 struct video_device
*vdev
= video_devdata(filep
);
858 struct common_obj
*common
;
859 struct video_obj
*vid_ch
;
860 struct channel_obj
*ch
;
864 vpif_dbg(2, debug
, "vpif_open\n");
866 ch
= video_get_drvdata(vdev
);
869 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
871 if (NULL
== ch
->curr_subdev_info
) {
873 * search through the sub device to see a registered
874 * sub device and make it as current sub device
876 for (i
= 0; i
< config
->subdev_count
; i
++) {
877 if (vpif_obj
.sd
[i
]) {
878 /* the sub device is registered */
879 ch
->curr_subdev_info
= &config
->subdev_info
[i
];
880 /* make first input as the current input */
881 vid_ch
->input_idx
= 0;
885 if (i
== config
->subdev_count
) {
886 vpif_err("No sub device registered\n");
891 /* Allocate memory for the file handle object */
892 fh
= kzalloc(sizeof(struct vpif_fh
), GFP_KERNEL
);
894 vpif_err("unable to allocate memory for file handle object\n");
898 /* store pointer to fh in private_data member of filep */
899 filep
->private_data
= fh
;
902 /* If decoder is not initialized. initialize it */
903 if (!ch
->initialized
) {
906 memset(&(ch
->vpifparams
), 0, sizeof(struct vpif_params
));
908 /* Increment channel usrs counter */
910 /* Set io_allowed member to false */
911 fh
->io_allowed
[VPIF_VIDEO_INDEX
] = 0;
912 /* Initialize priority of this instance to default priority */
913 fh
->prio
= V4L2_PRIORITY_UNSET
;
914 v4l2_prio_open(&ch
->prio
, &fh
->prio
);
919 * vpif_release : function to clean up file close
920 * @filep: file pointer
922 * This function deletes buffer queue, frees the buffers and the vpif file
925 static int vpif_release(struct file
*filep
)
927 struct vpif_fh
*fh
= filep
->private_data
;
928 struct channel_obj
*ch
= fh
->channel
;
929 struct common_obj
*common
;
931 vpif_dbg(2, debug
, "vpif_release\n");
933 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
935 /* if this instance is doing IO */
936 if (fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
937 /* Reset io_usrs member of channel object */
939 /* Disable channel as per its device type and channel id */
940 if (VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) {
942 channel0_intr_enable(0);
944 if ((VPIF_CHANNEL1_VIDEO
== ch
->channel_id
) ||
945 (2 == common
->started
)) {
947 channel1_intr_enable(0);
950 /* Free buffers allocated */
951 vb2_queue_release(&common
->buffer_queue
);
952 vb2_dma_contig_cleanup_ctx(common
->alloc_ctx
);
955 /* Decrement channel usrs counter */
958 /* Close the priority */
959 v4l2_prio_close(&ch
->prio
, fh
->prio
);
964 filep
->private_data
= NULL
;
970 * vpif_reqbufs() - request buffer handler
973 * @reqbuf: request buffer structure ptr
975 static int vpif_reqbufs(struct file
*file
, void *priv
,
976 struct v4l2_requestbuffers
*reqbuf
)
978 struct vpif_fh
*fh
= priv
;
979 struct channel_obj
*ch
= fh
->channel
;
980 struct common_obj
*common
;
984 vpif_dbg(2, debug
, "vpif_reqbufs\n");
987 * This file handle has not initialized the channel,
988 * It is not allowed to do settings
990 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
)
991 || (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
992 if (!fh
->initialized
) {
993 vpif_dbg(1, debug
, "Channel Busy\n");
998 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= reqbuf
->type
|| !vpif_dev
)
1001 index
= VPIF_VIDEO_INDEX
;
1003 common
= &ch
->common
[index
];
1005 if (0 != common
->io_usrs
)
1008 /* Initialize videobuf2 queue as per the buffer type */
1009 common
->alloc_ctx
= vb2_dma_contig_init_ctx(vpif_dev
);
1010 if (!common
->alloc_ctx
) {
1011 vpif_err("Failed to get the context\n");
1014 q
= &common
->buffer_queue
;
1015 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1016 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1018 q
->ops
= &video_qops
;
1019 q
->mem_ops
= &vb2_dma_contig_memops
;
1020 q
->buf_struct_size
= sizeof(struct vpif_cap_buffer
);
1024 /* Set io allowed member of file handle to TRUE */
1025 fh
->io_allowed
[index
] = 1;
1026 /* Increment io usrs member of channel object to 1 */
1027 common
->io_usrs
= 1;
1028 /* Store type of memory requested in channel object */
1029 common
->memory
= reqbuf
->memory
;
1030 INIT_LIST_HEAD(&common
->dma_queue
);
1032 /* Allocate buffers */
1033 return vb2_reqbufs(&common
->buffer_queue
, reqbuf
);
1037 * vpif_querybuf() - query buffer handler
1039 * @priv: file handle
1040 * @buf: v4l2 buffer structure ptr
1042 static int vpif_querybuf(struct file
*file
, void *priv
,
1043 struct v4l2_buffer
*buf
)
1045 struct vpif_fh
*fh
= priv
;
1046 struct channel_obj
*ch
= fh
->channel
;
1047 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1049 vpif_dbg(2, debug
, "vpif_querybuf\n");
1051 if (common
->fmt
.type
!= buf
->type
)
1054 if (common
->memory
!= V4L2_MEMORY_MMAP
) {
1055 vpif_dbg(1, debug
, "Invalid memory\n");
1059 return vb2_querybuf(&common
->buffer_queue
, buf
);
1063 * vpif_qbuf() - query buffer handler
1065 * @priv: file handle
1066 * @buf: v4l2 buffer structure ptr
1068 static int vpif_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
1071 struct vpif_fh
*fh
= priv
;
1072 struct channel_obj
*ch
= fh
->channel
;
1073 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1074 struct v4l2_buffer tbuf
= *buf
;
1076 vpif_dbg(2, debug
, "vpif_qbuf\n");
1078 if (common
->fmt
.type
!= tbuf
.type
) {
1079 vpif_err("invalid buffer type\n");
1083 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1084 vpif_err("fh io not allowed\n");
1088 return vb2_qbuf(&common
->buffer_queue
, buf
);
1092 * vpif_dqbuf() - query buffer handler
1094 * @priv: file handle
1095 * @buf: v4l2 buffer structure ptr
1097 static int vpif_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
1099 struct vpif_fh
*fh
= priv
;
1100 struct channel_obj
*ch
= fh
->channel
;
1101 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1103 vpif_dbg(2, debug
, "vpif_dqbuf\n");
1105 return vb2_dqbuf(&common
->buffer_queue
, buf
,
1106 (file
->f_flags
& O_NONBLOCK
));
1110 * vpif_streamon() - streamon handler
1112 * @priv: file handle
1113 * @buftype: v4l2 buffer type
1115 static int vpif_streamon(struct file
*file
, void *priv
,
1116 enum v4l2_buf_type buftype
)
1119 struct vpif_fh
*fh
= priv
;
1120 struct channel_obj
*ch
= fh
->channel
;
1121 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1122 struct channel_obj
*oth_ch
= vpif_obj
.dev
[!ch
->channel_id
];
1123 struct vpif_params
*vpif
;
1126 vpif_dbg(2, debug
, "vpif_streamon\n");
1128 vpif
= &ch
->vpifparams
;
1130 if (buftype
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1131 vpif_dbg(1, debug
, "buffer type not supported\n");
1135 /* If file handle is not allowed IO, return error */
1136 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1137 vpif_dbg(1, debug
, "io not allowed\n");
1141 /* If Streaming is already started, return error */
1142 if (common
->started
) {
1143 vpif_dbg(1, debug
, "channel->started\n");
1147 if ((ch
->channel_id
== VPIF_CHANNEL0_VIDEO
&&
1148 oth_ch
->common
[VPIF_VIDEO_INDEX
].started
&&
1149 vpif
->std_info
.ycmux_mode
== 0) ||
1150 ((ch
->channel_id
== VPIF_CHANNEL1_VIDEO
) &&
1151 (2 == oth_ch
->common
[VPIF_VIDEO_INDEX
].started
))) {
1152 vpif_dbg(1, debug
, "other channel is being used\n");
1156 ret
= vpif_check_format(ch
, &common
->fmt
.fmt
.pix
, 0);
1160 /* Enable streamon on the sub device */
1161 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1164 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
1165 vpif_dbg(1, debug
, "stream on failed in subdev\n");
1169 /* Call vb2_streamon to start streaming in videobuf2 */
1170 ret
= vb2_streamon(&common
->buffer_queue
, buftype
);
1172 vpif_dbg(1, debug
, "vb2_streamon\n");
1180 * vpif_streamoff() - streamoff handler
1182 * @priv: file handle
1183 * @buftype: v4l2 buffer type
1185 static int vpif_streamoff(struct file
*file
, void *priv
,
1186 enum v4l2_buf_type buftype
)
1189 struct vpif_fh
*fh
= priv
;
1190 struct channel_obj
*ch
= fh
->channel
;
1191 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1194 vpif_dbg(2, debug
, "vpif_streamoff\n");
1196 if (buftype
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1197 vpif_dbg(1, debug
, "buffer type not supported\n");
1201 /* If io is allowed for this file handle, return error */
1202 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1203 vpif_dbg(1, debug
, "io not allowed\n");
1207 /* If streaming is not started, return error */
1208 if (!common
->started
) {
1209 vpif_dbg(1, debug
, "channel->started\n");
1213 /* disable channel */
1214 if (VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) {
1216 channel0_intr_enable(0);
1219 channel1_intr_enable(0);
1222 common
->started
= 0;
1224 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1227 if (ret
&& (ret
!= -ENOIOCTLCMD
))
1228 vpif_dbg(1, debug
, "stream off failed in subdev\n");
1230 return vb2_streamoff(&common
->buffer_queue
, buftype
);
1234 * vpif_map_sub_device_to_input() - Maps sub device to input
1235 * @ch - ptr to channel
1236 * @config - ptr to capture configuration
1237 * @input_index - Given input index from application
1238 * @sub_device_index - index into sd table
1240 * lookup the sub device information for a given input index.
1241 * we report all the inputs to application. inputs table also
1242 * has sub device name for the each input
1244 static struct vpif_subdev_info
*vpif_map_sub_device_to_input(
1245 struct channel_obj
*ch
,
1246 struct vpif_capture_config
*vpif_cfg
,
1248 int *sub_device_index
)
1250 struct vpif_capture_chan_config
*chan_cfg
;
1251 struct vpif_subdev_info
*subdev_info
= NULL
;
1252 const char *subdev_name
= NULL
;
1255 vpif_dbg(2, debug
, "vpif_map_sub_device_to_input\n");
1257 chan_cfg
= &vpif_cfg
->chan_config
[ch
->channel_id
];
1260 * search through the inputs to find the sub device supporting
1263 for (i
= 0; i
< chan_cfg
->input_count
; i
++) {
1264 /* For each sub device, loop through input */
1265 if (i
== input_index
) {
1266 subdev_name
= chan_cfg
->inputs
[i
].subdev_name
;
1271 /* if reached maximum. return null */
1272 if (i
== chan_cfg
->input_count
|| (NULL
== subdev_name
))
1275 /* loop through the sub device list to get the sub device info */
1276 for (i
= 0; i
< vpif_cfg
->subdev_count
; i
++) {
1277 subdev_info
= &vpif_cfg
->subdev_info
[i
];
1278 if (!strcmp(subdev_info
->name
, subdev_name
))
1282 if (i
== vpif_cfg
->subdev_count
)
1285 /* check if the sub device is registered */
1286 if (NULL
== vpif_obj
.sd
[i
])
1289 *sub_device_index
= i
;
1294 * vpif_querystd() - querystd handler
1296 * @priv: file handle
1297 * @std_id: ptr to std id
1299 * This function is called to detect standard at the selected input
1301 static int vpif_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1303 struct vpif_fh
*fh
= priv
;
1304 struct channel_obj
*ch
= fh
->channel
;
1307 vpif_dbg(2, debug
, "vpif_querystd\n");
1309 /* Call querystd function of decoder device */
1310 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], video
,
1313 vpif_dbg(1, debug
, "Failed to set standard for sub devices\n");
1319 * vpif_g_std() - get STD handler
1321 * @priv: file handle
1322 * @std_id: ptr to std id
1324 static int vpif_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
1326 struct vpif_fh
*fh
= priv
;
1327 struct channel_obj
*ch
= fh
->channel
;
1329 vpif_dbg(2, debug
, "vpif_g_std\n");
1331 *std
= ch
->video
.stdid
;
1336 * vpif_s_std() - set STD handler
1338 * @priv: file handle
1339 * @std_id: ptr to std id
1341 static int vpif_s_std(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
1343 struct vpif_fh
*fh
= priv
;
1344 struct channel_obj
*ch
= fh
->channel
;
1345 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1348 vpif_dbg(2, debug
, "vpif_s_std\n");
1350 if (common
->started
) {
1351 vpif_err("streaming in progress\n");
1355 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1356 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1357 if (!fh
->initialized
) {
1358 vpif_dbg(1, debug
, "Channel Busy\n");
1363 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1367 fh
->initialized
= 1;
1369 /* Call encoder subdevice function to set the standard */
1370 ch
->video
.stdid
= *std_id
;
1371 ch
->video
.dv_preset
= V4L2_DV_INVALID
;
1372 memset(&ch
->video
.bt_timings
, 0, sizeof(ch
->video
.bt_timings
));
1374 /* Get the information about the standard */
1375 if (vpif_update_std_info(ch
)) {
1376 vpif_err("Error getting the standard info\n");
1380 /* Configure the default format information */
1381 vpif_config_format(ch
);
1383 /* set standard in the sub device */
1384 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], core
,
1387 vpif_dbg(1, debug
, "Failed to set standard for sub devices\n");
1392 * vpif_enum_input() - ENUMINPUT handler
1394 * @priv: file handle
1395 * @input: ptr to input structure
1397 static int vpif_enum_input(struct file
*file
, void *priv
,
1398 struct v4l2_input
*input
)
1401 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1402 struct vpif_capture_chan_config
*chan_cfg
;
1403 struct vpif_fh
*fh
= priv
;
1404 struct channel_obj
*ch
= fh
->channel
;
1406 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1408 if (input
->index
>= chan_cfg
->input_count
) {
1409 vpif_dbg(1, debug
, "Invalid input index\n");
1413 memcpy(input
, &chan_cfg
->inputs
[input
->index
].input
,
1419 * vpif_g_input() - Get INPUT handler
1421 * @priv: file handle
1422 * @index: ptr to input index
1424 static int vpif_g_input(struct file
*file
, void *priv
, unsigned int *index
)
1426 struct vpif_fh
*fh
= priv
;
1427 struct channel_obj
*ch
= fh
->channel
;
1428 struct video_obj
*vid_ch
= &ch
->video
;
1430 *index
= vid_ch
->input_idx
;
1436 * vpif_s_input() - Set INPUT handler
1438 * @priv: file handle
1439 * @index: input index
1441 static int vpif_s_input(struct file
*file
, void *priv
, unsigned int index
)
1443 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1444 struct vpif_capture_chan_config
*chan_cfg
;
1445 struct vpif_fh
*fh
= priv
;
1446 struct channel_obj
*ch
= fh
->channel
;
1447 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1448 struct video_obj
*vid_ch
= &ch
->video
;
1449 struct vpif_subdev_info
*subdev_info
;
1450 int ret
= 0, sd_index
= 0;
1451 u32 input
= 0, output
= 0;
1453 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1455 if (common
->started
) {
1456 vpif_err("Streaming in progress\n");
1460 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1461 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1462 if (!fh
->initialized
) {
1463 vpif_dbg(1, debug
, "Channel Busy\n");
1468 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1472 fh
->initialized
= 1;
1473 subdev_info
= vpif_map_sub_device_to_input(ch
, config
, index
,
1475 if (NULL
== subdev_info
) {
1477 "couldn't lookup sub device for the input index\n");
1481 /* first setup input path from sub device to vpif */
1482 if (config
->setup_input_path
) {
1483 ret
= config
->setup_input_path(ch
->channel_id
,
1486 vpif_dbg(1, debug
, "couldn't setup input path for the"
1487 " sub device %s, for input index %d\n",
1488 subdev_info
->name
, index
);
1493 if (subdev_info
->can_route
) {
1494 input
= subdev_info
->input
;
1495 output
= subdev_info
->output
;
1496 ret
= v4l2_subdev_call(vpif_obj
.sd
[sd_index
], video
, s_routing
,
1499 vpif_dbg(1, debug
, "Failed to set input\n");
1503 vid_ch
->input_idx
= index
;
1504 ch
->curr_subdev_info
= subdev_info
;
1505 ch
->curr_sd_index
= sd_index
;
1506 /* copy interface parameters to vpif */
1507 ch
->vpifparams
.iface
= subdev_info
->vpif_if
;
1509 /* update tvnorms from the sub device input info */
1510 ch
->video_dev
->tvnorms
= chan_cfg
->inputs
[index
].input
.std
;
1515 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1517 * @priv: file handle
1518 * @index: input index
1520 static int vpif_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1521 struct v4l2_fmtdesc
*fmt
)
1523 struct vpif_fh
*fh
= priv
;
1524 struct channel_obj
*ch
= fh
->channel
;
1526 if (fmt
->index
!= 0) {
1527 vpif_dbg(1, debug
, "Invalid format index\n");
1531 /* Fill in the information about format */
1532 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
) {
1533 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1534 strcpy(fmt
->description
, "Raw Mode -Bayer Pattern GrRBGb");
1535 fmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
1537 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1538 strcpy(fmt
->description
, "YCbCr4:2:2 YC Planar");
1539 fmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
1545 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1547 * @priv: file handle
1548 * @fmt: ptr to v4l2 format structure
1550 static int vpif_try_fmt_vid_cap(struct file
*file
, void *priv
,
1551 struct v4l2_format
*fmt
)
1553 struct vpif_fh
*fh
= priv
;
1554 struct channel_obj
*ch
= fh
->channel
;
1555 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
1557 return vpif_check_format(ch
, pixfmt
, 1);
1562 * vpif_g_fmt_vid_cap() - Set INPUT handler
1564 * @priv: file handle
1565 * @fmt: ptr to v4l2 format structure
1567 static int vpif_g_fmt_vid_cap(struct file
*file
, void *priv
,
1568 struct v4l2_format
*fmt
)
1570 struct vpif_fh
*fh
= priv
;
1571 struct channel_obj
*ch
= fh
->channel
;
1572 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1574 /* Check the validity of the buffer type */
1575 if (common
->fmt
.type
!= fmt
->type
)
1578 /* Fill in the information about format */
1584 * vpif_s_fmt_vid_cap() - Set FMT handler
1586 * @priv: file handle
1587 * @fmt: ptr to v4l2 format structure
1589 static int vpif_s_fmt_vid_cap(struct file
*file
, void *priv
,
1590 struct v4l2_format
*fmt
)
1592 struct vpif_fh
*fh
= priv
;
1593 struct channel_obj
*ch
= fh
->channel
;
1594 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1595 struct v4l2_pix_format
*pixfmt
;
1598 vpif_dbg(2, debug
, "%s\n", __func__
);
1600 /* If streaming is started, return error */
1601 if (common
->started
) {
1602 vpif_dbg(1, debug
, "Streaming is started\n");
1606 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1607 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1608 if (!fh
->initialized
) {
1609 vpif_dbg(1, debug
, "Channel Busy\n");
1614 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1618 fh
->initialized
= 1;
1620 pixfmt
= &fmt
->fmt
.pix
;
1621 /* Check for valid field format */
1622 ret
= vpif_check_format(ch
, pixfmt
, 0);
1626 /* store the format in the channel object */
1632 * vpif_querycap() - QUERYCAP handler
1634 * @priv: file handle
1635 * @cap: ptr to v4l2_capability structure
1637 static int vpif_querycap(struct file
*file
, void *priv
,
1638 struct v4l2_capability
*cap
)
1640 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1642 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1643 strlcpy(cap
->driver
, "vpif capture", sizeof(cap
->driver
));
1644 strlcpy(cap
->bus_info
, "VPIF Platform", sizeof(cap
->bus_info
));
1645 strlcpy(cap
->card
, config
->card_name
, sizeof(cap
->card
));
1651 * vpif_g_priority() - get priority handler
1653 * @priv: file handle
1654 * @prio: ptr to v4l2_priority structure
1656 static int vpif_g_priority(struct file
*file
, void *priv
,
1657 enum v4l2_priority
*prio
)
1659 struct vpif_fh
*fh
= priv
;
1660 struct channel_obj
*ch
= fh
->channel
;
1662 *prio
= v4l2_prio_max(&ch
->prio
);
1668 * vpif_s_priority() - set priority handler
1670 * @priv: file handle
1671 * @prio: ptr to v4l2_priority structure
1673 static int vpif_s_priority(struct file
*file
, void *priv
, enum v4l2_priority p
)
1675 struct vpif_fh
*fh
= priv
;
1676 struct channel_obj
*ch
= fh
->channel
;
1678 return v4l2_prio_change(&ch
->prio
, &fh
->prio
, p
);
1682 * vpif_cropcap() - cropcap handler
1684 * @priv: file handle
1685 * @crop: ptr to v4l2_cropcap structure
1687 static int vpif_cropcap(struct file
*file
, void *priv
,
1688 struct v4l2_cropcap
*crop
)
1690 struct vpif_fh
*fh
= priv
;
1691 struct channel_obj
*ch
= fh
->channel
;
1692 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1694 if (V4L2_BUF_TYPE_VIDEO_CAPTURE
!= crop
->type
)
1697 crop
->bounds
.left
= 0;
1698 crop
->bounds
.top
= 0;
1699 crop
->bounds
.height
= common
->height
;
1700 crop
->bounds
.width
= common
->width
;
1701 crop
->defrect
= crop
->bounds
;
1706 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1708 * @priv: file handle
1709 * @preset: input preset
1711 static int vpif_enum_dv_presets(struct file
*file
, void *priv
,
1712 struct v4l2_dv_enum_preset
*preset
)
1714 struct vpif_fh
*fh
= priv
;
1715 struct channel_obj
*ch
= fh
->channel
;
1717 return v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
],
1718 video
, enum_dv_presets
, preset
);
1722 * vpif_query_dv_presets() - QUERY_DV_PRESET handler
1724 * @priv: file handle
1725 * @preset: input preset
1727 static int vpif_query_dv_preset(struct file
*file
, void *priv
,
1728 struct v4l2_dv_preset
*preset
)
1730 struct vpif_fh
*fh
= priv
;
1731 struct channel_obj
*ch
= fh
->channel
;
1733 return v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
],
1734 video
, query_dv_preset
, preset
);
1737 * vpif_s_dv_presets() - S_DV_PRESETS handler
1739 * @priv: file handle
1740 * @preset: input preset
1742 static int vpif_s_dv_preset(struct file
*file
, void *priv
,
1743 struct v4l2_dv_preset
*preset
)
1745 struct vpif_fh
*fh
= priv
;
1746 struct channel_obj
*ch
= fh
->channel
;
1747 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1750 if (common
->started
) {
1751 vpif_dbg(1, debug
, "streaming in progress\n");
1755 if ((VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) ||
1756 (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)) {
1757 if (!fh
->initialized
) {
1758 vpif_dbg(1, debug
, "Channel Busy\n");
1763 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
1767 fh
->initialized
= 1;
1769 /* Call encoder subdevice function to set the standard */
1770 if (mutex_lock_interruptible(&common
->lock
))
1771 return -ERESTARTSYS
;
1773 ch
->video
.dv_preset
= preset
->preset
;
1774 ch
->video
.stdid
= V4L2_STD_UNKNOWN
;
1775 memset(&ch
->video
.bt_timings
, 0, sizeof(ch
->video
.bt_timings
));
1777 /* Get the information about the standard */
1778 if (vpif_update_std_info(ch
)) {
1779 vpif_dbg(1, debug
, "Error getting the standard info\n");
1782 /* Configure the default format information */
1783 vpif_config_format(ch
);
1785 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
],
1786 video
, s_dv_preset
, preset
);
1789 mutex_unlock(&common
->lock
);
1794 * vpif_g_dv_presets() - G_DV_PRESETS handler
1796 * @priv: file handle
1797 * @preset: input preset
1799 static int vpif_g_dv_preset(struct file
*file
, void *priv
,
1800 struct v4l2_dv_preset
*preset
)
1802 struct vpif_fh
*fh
= priv
;
1803 struct channel_obj
*ch
= fh
->channel
;
1805 preset
->preset
= ch
->video
.dv_preset
;
1811 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1813 * @priv: file handle
1814 * @timings: digital video timings
1816 static int vpif_s_dv_timings(struct file
*file
, void *priv
,
1817 struct v4l2_dv_timings
*timings
)
1819 struct vpif_fh
*fh
= priv
;
1820 struct channel_obj
*ch
= fh
->channel
;
1821 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
1822 struct vpif_channel_config_params
*std_info
= &vpifparams
->std_info
;
1823 struct video_obj
*vid_ch
= &ch
->video
;
1824 struct v4l2_bt_timings
*bt
= &vid_ch
->bt_timings
;
1827 if (timings
->type
!= V4L2_DV_BT_656_1120
) {
1828 vpif_dbg(2, debug
, "Timing type not defined\n");
1832 /* Configure subdevice timings, if any */
1833 ret
= v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
],
1834 video
, s_dv_timings
, timings
);
1835 if (ret
== -ENOIOCTLCMD
) {
1836 vpif_dbg(2, debug
, "Custom DV timings not supported by "
1841 vpif_dbg(2, debug
, "Error setting custom DV timings\n");
1845 if (!(timings
->bt
.width
&& timings
->bt
.height
&&
1846 (timings
->bt
.hbackporch
||
1847 timings
->bt
.hfrontporch
||
1848 timings
->bt
.hsync
) &&
1849 timings
->bt
.vfrontporch
&&
1850 (timings
->bt
.vbackporch
||
1851 timings
->bt
.vsync
))) {
1852 vpif_dbg(2, debug
, "Timings for width, height, "
1853 "horizontal back porch, horizontal sync, "
1854 "horizontal front porch, vertical back porch, "
1855 "vertical sync and vertical back porch "
1856 "must be defined\n");
1862 /* Configure video port timings */
1864 std_info
->eav2sav
= bt
->hbackporch
+ bt
->hfrontporch
+
1866 std_info
->sav2eav
= bt
->width
;
1869 std_info
->l3
= bt
->vsync
+ bt
->vbackporch
+ 1;
1871 if (bt
->interlaced
) {
1872 if (bt
->il_vbackporch
|| bt
->il_vfrontporch
|| bt
->il_vsync
) {
1873 std_info
->vsize
= bt
->height
* 2 +
1874 bt
->vfrontporch
+ bt
->vsync
+ bt
->vbackporch
+
1875 bt
->il_vfrontporch
+ bt
->il_vsync
+
1877 std_info
->l5
= std_info
->vsize
/2 -
1878 (bt
->vfrontporch
- 1);
1879 std_info
->l7
= std_info
->vsize
/2 + 1;
1880 std_info
->l9
= std_info
->l7
+ bt
->il_vsync
+
1881 bt
->il_vbackporch
+ 1;
1882 std_info
->l11
= std_info
->vsize
-
1883 (bt
->il_vfrontporch
- 1);
1885 vpif_dbg(2, debug
, "Required timing values for "
1886 "interlaced BT format missing\n");
1890 std_info
->vsize
= bt
->height
+ bt
->vfrontporch
+
1891 bt
->vsync
+ bt
->vbackporch
;
1892 std_info
->l5
= std_info
->vsize
- (bt
->vfrontporch
- 1);
1894 strncpy(std_info
->name
, "Custom timings BT656/1120", VPIF_MAX_NAME
);
1895 std_info
->width
= bt
->width
;
1896 std_info
->height
= bt
->height
;
1897 std_info
->frm_fmt
= bt
->interlaced
? 0 : 1;
1898 std_info
->ycmux_mode
= 0;
1899 std_info
->capture_format
= 0;
1900 std_info
->vbi_supported
= 0;
1901 std_info
->hd_sd
= 1;
1902 std_info
->stdid
= 0;
1903 std_info
->dv_preset
= V4L2_DV_INVALID
;
1906 vid_ch
->dv_preset
= V4L2_DV_INVALID
;
1911 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1913 * @priv: file handle
1914 * @timings: digital video timings
1916 static int vpif_g_dv_timings(struct file
*file
, void *priv
,
1917 struct v4l2_dv_timings
*timings
)
1919 struct vpif_fh
*fh
= priv
;
1920 struct channel_obj
*ch
= fh
->channel
;
1921 struct video_obj
*vid_ch
= &ch
->video
;
1922 struct v4l2_bt_timings
*bt
= &vid_ch
->bt_timings
;
1930 * vpif_g_chip_ident() - Identify the chip
1932 * @priv: file handle
1933 * @chip: chip identity
1935 * Returns zero or -EINVAL if read operations fails.
1937 static int vpif_g_chip_ident(struct file
*file
, void *priv
,
1938 struct v4l2_dbg_chip_ident
*chip
)
1940 chip
->ident
= V4L2_IDENT_NONE
;
1942 if (chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_DRIVER
&&
1943 chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
) {
1944 vpif_dbg(2, debug
, "match_type is invalid.\n");
1948 return v4l2_device_call_until_err(&vpif_obj
.v4l2_dev
, 0, core
,
1949 g_chip_ident
, chip
);
1952 #ifdef CONFIG_VIDEO_ADV_DEBUG
1954 * vpif_dbg_g_register() - Read register
1956 * @priv: file handle
1957 * @reg: register to be read
1960 * Returns zero or -EINVAL if read operations fails.
1962 static int vpif_dbg_g_register(struct file
*file
, void *priv
,
1963 struct v4l2_dbg_register
*reg
){
1964 struct vpif_fh
*fh
= priv
;
1965 struct channel_obj
*ch
= fh
->channel
;
1967 return v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], core
,
1972 * vpif_dbg_s_register() - Write to register
1974 * @priv: file handle
1975 * @reg: register to be modified
1978 * Returns zero or -EINVAL if write operations fails.
1980 static int vpif_dbg_s_register(struct file
*file
, void *priv
,
1981 struct v4l2_dbg_register
*reg
){
1982 struct vpif_fh
*fh
= priv
;
1983 struct channel_obj
*ch
= fh
->channel
;
1985 return v4l2_subdev_call(vpif_obj
.sd
[ch
->curr_sd_index
], core
,
1991 * vpif_log_status() - Status information
1993 * @priv: file handle
1997 static int vpif_log_status(struct file
*filep
, void *priv
)
1999 /* status for sub devices */
2000 v4l2_device_call_all(&vpif_obj
.v4l2_dev
, 0, core
, log_status
);
2005 /* vpif capture ioctl operations */
2006 static const struct v4l2_ioctl_ops vpif_ioctl_ops
= {
2007 .vidioc_querycap
= vpif_querycap
,
2008 .vidioc_g_priority
= vpif_g_priority
,
2009 .vidioc_s_priority
= vpif_s_priority
,
2010 .vidioc_enum_fmt_vid_cap
= vpif_enum_fmt_vid_cap
,
2011 .vidioc_g_fmt_vid_cap
= vpif_g_fmt_vid_cap
,
2012 .vidioc_s_fmt_vid_cap
= vpif_s_fmt_vid_cap
,
2013 .vidioc_try_fmt_vid_cap
= vpif_try_fmt_vid_cap
,
2014 .vidioc_enum_input
= vpif_enum_input
,
2015 .vidioc_s_input
= vpif_s_input
,
2016 .vidioc_g_input
= vpif_g_input
,
2017 .vidioc_reqbufs
= vpif_reqbufs
,
2018 .vidioc_querybuf
= vpif_querybuf
,
2019 .vidioc_querystd
= vpif_querystd
,
2020 .vidioc_s_std
= vpif_s_std
,
2021 .vidioc_g_std
= vpif_g_std
,
2022 .vidioc_qbuf
= vpif_qbuf
,
2023 .vidioc_dqbuf
= vpif_dqbuf
,
2024 .vidioc_streamon
= vpif_streamon
,
2025 .vidioc_streamoff
= vpif_streamoff
,
2026 .vidioc_cropcap
= vpif_cropcap
,
2027 .vidioc_enum_dv_presets
= vpif_enum_dv_presets
,
2028 .vidioc_s_dv_preset
= vpif_s_dv_preset
,
2029 .vidioc_g_dv_preset
= vpif_g_dv_preset
,
2030 .vidioc_query_dv_preset
= vpif_query_dv_preset
,
2031 .vidioc_s_dv_timings
= vpif_s_dv_timings
,
2032 .vidioc_g_dv_timings
= vpif_g_dv_timings
,
2033 .vidioc_g_chip_ident
= vpif_g_chip_ident
,
2034 #ifdef CONFIG_VIDEO_ADV_DEBUG
2035 .vidioc_g_register
= vpif_dbg_g_register
,
2036 .vidioc_s_register
= vpif_dbg_s_register
,
2038 .vidioc_log_status
= vpif_log_status
,
2041 /* vpif file operations */
2042 static struct v4l2_file_operations vpif_fops
= {
2043 .owner
= THIS_MODULE
,
2045 .release
= vpif_release
,
2046 .unlocked_ioctl
= video_ioctl2
,
2051 /* vpif video template */
2052 static struct video_device vpif_video_template
= {
2056 .ioctl_ops
= &vpif_ioctl_ops
,
2060 * initialize_vpif() - Initialize vpif data structures
2062 * Allocate memory for data structures and initialize them
2064 static int initialize_vpif(void)
2067 int free_channel_objects_index
;
2069 /* Default number of buffers should be 3 */
2070 if ((ch0_numbuffers
> 0) &&
2071 (ch0_numbuffers
< config_params
.min_numbuffers
))
2072 ch0_numbuffers
= config_params
.min_numbuffers
;
2073 if ((ch1_numbuffers
> 0) &&
2074 (ch1_numbuffers
< config_params
.min_numbuffers
))
2075 ch1_numbuffers
= config_params
.min_numbuffers
;
2077 /* Set buffer size to min buffers size if it is invalid */
2078 if (ch0_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
])
2080 config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
];
2081 if (ch1_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
])
2083 config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
];
2085 config_params
.numbuffers
[VPIF_CHANNEL0_VIDEO
] = ch0_numbuffers
;
2086 config_params
.numbuffers
[VPIF_CHANNEL1_VIDEO
] = ch1_numbuffers
;
2087 if (ch0_numbuffers
) {
2088 config_params
.channel_bufsize
[VPIF_CHANNEL0_VIDEO
]
2091 if (ch1_numbuffers
) {
2092 config_params
.channel_bufsize
[VPIF_CHANNEL1_VIDEO
]
2096 /* Allocate memory for six channel objects */
2097 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
2099 kzalloc(sizeof(*vpif_obj
.dev
[i
]), GFP_KERNEL
);
2100 /* If memory allocation fails, return error */
2101 if (!vpif_obj
.dev
[i
]) {
2102 free_channel_objects_index
= i
;
2104 goto vpif_init_free_channel_objects
;
2109 vpif_init_free_channel_objects
:
2110 for (j
= 0; j
< free_channel_objects_index
; j
++)
2111 kfree(vpif_obj
.dev
[j
]);
2116 * vpif_probe : This function probes the vpif capture driver
2117 * @pdev: platform device pointer
2119 * This creates device entries by register itself to the V4L2 driver and
2120 * initializes fields of each channel objects
2122 static __init
int vpif_probe(struct platform_device
*pdev
)
2124 struct vpif_subdev_info
*subdevdata
;
2125 struct vpif_capture_config
*config
;
2126 int i
, j
, k
, m
, q
, err
;
2127 struct i2c_adapter
*i2c_adap
;
2128 struct channel_obj
*ch
;
2129 struct common_obj
*common
;
2130 struct video_device
*vfd
;
2131 struct resource
*res
;
2135 vpif_dev
= &pdev
->dev
;
2137 err
= initialize_vpif();
2139 v4l2_err(vpif_dev
->driver
, "Error initializing vpif\n");
2143 err
= v4l2_device_register(vpif_dev
, &vpif_obj
.v4l2_dev
);
2145 v4l2_err(vpif_dev
->driver
, "Error registering v4l2 device\n");
2150 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, k
))) {
2151 for (i
= res
->start
; i
<= res
->end
; i
++) {
2152 if (request_irq(i
, vpif_channel_isr
, IRQF_SHARED
,
2154 (void *)(&vpif_obj
.dev
[k
]->channel_id
))) {
2163 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
2164 /* Get the pointer to the channel object */
2165 ch
= vpif_obj
.dev
[i
];
2166 /* Allocate memory for video device */
2167 vfd
= video_device_alloc();
2169 for (j
= 0; j
< i
; j
++) {
2170 ch
= vpif_obj
.dev
[j
];
2171 video_device_release(ch
->video_dev
);
2174 goto vpif_dev_alloc_err
;
2177 /* Initialize field of video device */
2178 *vfd
= vpif_video_template
;
2179 vfd
->v4l2_dev
= &vpif_obj
.v4l2_dev
;
2180 vfd
->release
= video_device_release
;
2181 snprintf(vfd
->name
, sizeof(vfd
->name
),
2182 "VPIF_Capture_DRIVER_V%s",
2183 VPIF_CAPTURE_VERSION
);
2184 /* Set video_dev to the video device */
2185 ch
->video_dev
= vfd
;
2188 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2190 size
= resource_size(res
);
2191 /* The resources are divided into two equal memory and when we
2192 * have HD output we can add them together
2194 for (j
= 0; j
< VPIF_CAPTURE_MAX_DEVICES
; j
++) {
2195 ch
= vpif_obj
.dev
[j
];
2197 /* only enabled if second resource exists */
2198 config_params
.video_limit
[ch
->channel_id
] = 0;
2200 config_params
.video_limit
[ch
->channel_id
] =
2205 for (j
= 0; j
< VPIF_CAPTURE_MAX_DEVICES
; j
++) {
2206 ch
= vpif_obj
.dev
[j
];
2208 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
2209 spin_lock_init(&common
->irqlock
);
2210 mutex_init(&common
->lock
);
2211 /* Locking in file operations other than ioctl should be done
2212 by the driver, not the V4L2 core.
2213 This driver needs auditing so that this flag can be removed. */
2214 set_bit(V4L2_FL_LOCK_ALL_FOPS
, &ch
->video_dev
->flags
);
2215 ch
->video_dev
->lock
= &common
->lock
;
2216 /* Initialize prio member of channel object */
2217 v4l2_prio_init(&ch
->prio
);
2218 err
= video_register_device(ch
->video_dev
,
2219 VFL_TYPE_GRABBER
, (j
? 1 : 0));
2223 video_set_drvdata(ch
->video_dev
, ch
);
2227 i2c_adap
= i2c_get_adapter(1);
2228 config
= pdev
->dev
.platform_data
;
2230 subdev_count
= config
->subdev_count
;
2231 vpif_obj
.sd
= kzalloc(sizeof(struct v4l2_subdev
*) * subdev_count
,
2233 if (vpif_obj
.sd
== NULL
) {
2234 vpif_err("unable to allocate memory for subdevice pointers\n");
2239 for (i
= 0; i
< subdev_count
; i
++) {
2240 subdevdata
= &config
->subdev_info
[i
];
2242 v4l2_i2c_new_subdev_board(&vpif_obj
.v4l2_dev
,
2244 &subdevdata
->board_info
,
2247 if (!vpif_obj
.sd
[i
]) {
2248 vpif_err("Error registering v4l2 subdevice\n");
2249 goto probe_subdev_out
;
2251 v4l2_info(&vpif_obj
.v4l2_dev
, "registered sub device %s\n",
2255 vpif_obj
.sd
[i
]->grp_id
= 1 << i
;
2258 v4l2_info(&vpif_obj
.v4l2_dev
, "VPIF capture driver initialized\n");
2262 /* free sub devices memory */
2265 j
= VPIF_CAPTURE_MAX_DEVICES
;
2267 for (k
= 0; k
< j
; k
++) {
2268 /* Get the pointer to the channel object */
2269 ch
= vpif_obj
.dev
[k
];
2270 /* Unregister video device */
2271 video_unregister_device(ch
->video_dev
);
2275 k
= VPIF_CAPTURE_MAX_DEVICES
-1;
2276 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, k
);
2280 for (q
= k
; q
>= 0; q
--) {
2281 for (m
= i
; m
>= (int)res
->start
; m
--)
2282 free_irq(m
, (void *)(&vpif_obj
.dev
[q
]->channel_id
));
2284 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, q
-1);
2288 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
2293 * vpif_remove() - driver remove handler
2294 * @device: ptr to platform device structure
2296 * The vidoe device is unregistered
2298 static int vpif_remove(struct platform_device
*device
)
2301 struct channel_obj
*ch
;
2303 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
2305 /* un-register device */
2306 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
2307 /* Get the pointer to the channel object */
2308 ch
= vpif_obj
.dev
[i
];
2309 /* Unregister video device */
2310 video_unregister_device(ch
->video_dev
);
2317 * vpif_suspend: vpif device suspend
2319 static int vpif_suspend(struct device
*dev
)
2322 struct common_obj
*common
;
2323 struct channel_obj
*ch
;
2326 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
2327 /* Get the pointer to the channel object */
2328 ch
= vpif_obj
.dev
[i
];
2329 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
2330 mutex_lock(&common
->lock
);
2331 if (ch
->usrs
&& common
->io_usrs
) {
2332 /* Disable channel */
2333 if (ch
->channel_id
== VPIF_CHANNEL0_VIDEO
) {
2335 channel0_intr_enable(0);
2337 if (ch
->channel_id
== VPIF_CHANNEL1_VIDEO
||
2338 common
->started
== 2) {
2340 channel1_intr_enable(0);
2343 mutex_unlock(&common
->lock
);
2350 * vpif_resume: vpif device suspend
2352 static int vpif_resume(struct device
*dev
)
2354 struct common_obj
*common
;
2355 struct channel_obj
*ch
;
2358 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
2359 /* Get the pointer to the channel object */
2360 ch
= vpif_obj
.dev
[i
];
2361 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
2362 mutex_lock(&common
->lock
);
2363 if (ch
->usrs
&& common
->io_usrs
) {
2364 /* Disable channel */
2365 if (ch
->channel_id
== VPIF_CHANNEL0_VIDEO
) {
2367 channel0_intr_enable(1);
2369 if (ch
->channel_id
== VPIF_CHANNEL1_VIDEO
||
2370 common
->started
== 2) {
2372 channel1_intr_enable(1);
2375 mutex_unlock(&common
->lock
);
2381 static const struct dev_pm_ops vpif_dev_pm_ops
= {
2382 .suspend
= vpif_suspend
,
2383 .resume
= vpif_resume
,
2386 #define vpif_pm_ops (&vpif_dev_pm_ops)
2388 #define vpif_pm_ops NULL
2391 static __refdata
struct platform_driver vpif_driver
= {
2393 .name
= "vpif_capture",
2394 .owner
= THIS_MODULE
,
2397 .probe
= vpif_probe
,
2398 .remove
= vpif_remove
,
2402 * vpif_init: initialize the vpif driver
2404 * This function registers device and driver to the kernel, requests irq
2405 * handler and allocates memory
2406 * for channel objects
2408 static __init
int vpif_init(void)
2410 return platform_driver_register(&vpif_driver
);
2414 * vpif_cleanup : This function clean up the vpif capture resources
2416 * This will un-registers device and driver to the kernel, frees
2417 * requested irq handler and de-allocates memory allocated for channel
2420 static void vpif_cleanup(void)
2422 struct platform_device
*pdev
;
2423 struct resource
*res
;
2427 pdev
= container_of(vpif_dev
, struct platform_device
, dev
);
2428 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
))) {
2429 for (irq_num
= res
->start
; irq_num
<= res
->end
; irq_num
++)
2431 (void *)(&vpif_obj
.dev
[i
]->channel_id
));
2435 platform_driver_unregister(&vpif_driver
);
2438 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++)
2439 kfree(vpif_obj
.dev
[i
]);
2442 /* Function for module initialization and cleanup */
2443 module_init(vpif_init
);
2444 module_exit(vpif_cleanup
);