2 * Copyright (C) 2009 Texas Instruments Inc
3 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * TODO : add support for VBI & HBI data service
20 * add static buffer allocation
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
28 #include <media/v4l2-ioctl.h>
31 #include "vpif_capture.h"
33 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(VPIF_CAPTURE_VERSION);
37 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
38 #define vpif_dbg(level, debug, fmt, arg...) \
39 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
42 static u32 ch0_numbuffers = 3;
43 static u32 ch1_numbuffers = 3;
44 static u32 ch0_bufsize = 1920 * 1080 * 2;
45 static u32 ch1_bufsize = 720 * 576 * 2;
47 module_param(debug, int, 0644);
48 module_param(ch0_numbuffers, uint, S_IRUGO);
49 module_param(ch1_numbuffers, uint, S_IRUGO);
50 module_param(ch0_bufsize, uint, S_IRUGO);
51 module_param(ch1_bufsize, uint, S_IRUGO);
53 MODULE_PARM_DESC(debug, "Debug level 0-1");
54 MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
55 MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
56 MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
57 MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
59 static struct vpif_config_params config_params = {
63 .min_bufsize[0] = 720 * 480 * 2,
64 .min_bufsize[1] = 720 * 480 * 2,
65 .channel_bufsize[0] = 1920 * 1080 * 2,
66 .channel_bufsize[1] = 720 * 576 * 2,
69 #define VPIF_DRIVER_NAME "vpif_capture"
71 /* global variables */
72 static struct vpif_device vpif_obj = { {NULL} };
73 static struct device *vpif_dev;
74 static void vpif_calculate_offsets(struct channel_obj *ch);
75 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
77 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
79 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
80 static int ycmux_mode;
82 static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
84 return container_of(vb, struct vpif_cap_buffer, vb);
88 * vpif_buffer_prepare : callback function for buffer prepare
89 * @vb: ptr to vb2_buffer
91 * This is the callback function for buffer prepare when vb2_qbuf()
92 * function is called. The buffer is prepared and user space virtual address
93 * or user address is converted into physical address
95 static int vpif_buffer_prepare(struct vb2_buffer *vb)
97 struct vb2_queue *q = vb->vb2_queue;
98 struct channel_obj *ch = vb2_get_drv_priv(q);
99 struct common_obj *common;
102 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
104 common = &ch->common[VPIF_VIDEO_INDEX];
106 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
107 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
110 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
112 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
113 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
114 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
115 !IS_ALIGNED((addr + common->ctop_off), 8) ||
116 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
117 vpif_dbg(1, debug, "offset is not aligned\n");
125 * vpif_buffer_queue_setup : Callback function for buffer setup.
128 * @nbuffers: ptr to number of buffers requested by application
129 * @nplanes:: contains number of distinct video planes needed to hold a frame
130 * @sizes[]: contains the size (in bytes) of each plane.
131 * @alloc_ctxs: ptr to allocation context
133 * This callback function is called when reqbuf() is called to adjust
134 * the buffer count and buffer size
136 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
137 const struct v4l2_format *fmt,
138 unsigned int *nbuffers, unsigned int *nplanes,
139 unsigned int sizes[], void *alloc_ctxs[])
141 struct channel_obj *ch = vb2_get_drv_priv(vq);
142 struct common_obj *common;
144 common = &ch->common[VPIF_VIDEO_INDEX];
146 vpif_dbg(2, debug, "vpif_buffer_setup\n");
148 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
151 if (vq->num_buffers + *nbuffers < 3)
152 *nbuffers = 3 - vq->num_buffers;
155 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
156 alloc_ctxs[0] = common->alloc_ctx;
158 /* Calculate the offset for Y and C data in the buffer */
159 vpif_calculate_offsets(ch);
165 * vpif_buffer_queue : Callback function to add buffer to DMA queue
166 * @vb: ptr to vb2_buffer
168 static void vpif_buffer_queue(struct vb2_buffer *vb)
170 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
171 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
172 struct common_obj *common;
175 common = &ch->common[VPIF_VIDEO_INDEX];
177 vpif_dbg(2, debug, "vpif_buffer_queue\n");
179 spin_lock_irqsave(&common->irqlock, flags);
180 /* add the buffer to the DMA queue */
181 list_add_tail(&buf->list, &common->dma_queue);
182 spin_unlock_irqrestore(&common->irqlock, flags);
186 * vpif_start_streaming : Starts the DMA engine for streaming
187 * @vb: ptr to vb2_buffer
188 * @count: number of buffers
190 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
192 struct vpif_capture_config *vpif_config_data =
193 vpif_dev->platform_data;
194 struct channel_obj *ch = vb2_get_drv_priv(vq);
195 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
196 struct vpif_params *vpif = &ch->vpifparams;
197 struct vpif_cap_buffer *buf, *tmp;
198 unsigned long addr, flags;
201 spin_lock_irqsave(&common->irqlock, flags);
203 /* Initialize field_id */
206 /* configure 1 or 2 channel mode */
207 if (vpif_config_data->setup_input_channel_mode) {
208 ret = vpif_config_data->
209 setup_input_channel_mode(vpif->std_info.ycmux_mode);
211 vpif_dbg(1, debug, "can't set vpif channel mode\n");
216 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
217 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
218 vpif_dbg(1, debug, "stream on failed in subdev\n");
222 /* Call vpif_set_params function to set the parameters and addresses */
223 ret = vpif_set_video_params(vpif, ch->channel_id);
225 vpif_dbg(1, debug, "can't set video params\n");
230 vpif_config_addr(ch, ret);
232 /* Get the next frame from the buffer queue */
233 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
234 struct vpif_cap_buffer, list);
235 /* Remove buffer from the buffer queue */
236 list_del(&common->cur_frm->list);
237 spin_unlock_irqrestore(&common->irqlock, flags);
238 /* Mark state of the current frame to active */
239 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
241 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
243 common->set_addr(addr + common->ytop_off,
244 addr + common->ybtm_off,
245 addr + common->ctop_off,
246 addr + common->cbtm_off);
249 * Set interrupt for both the fields in VPIF Register enable channel in
252 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
253 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
254 channel0_intr_assert();
255 channel0_intr_enable(1);
258 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
260 channel1_intr_assert();
261 channel1_intr_enable(1);
268 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
269 list_del(&buf->list);
270 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
277 * vpif_stop_streaming : Stop the DMA engine
278 * @vq: ptr to vb2_queue
280 * This callback stops the DMA engine and any remaining buffers
281 * in the DMA queue are released.
283 static void vpif_stop_streaming(struct vb2_queue *vq)
285 struct channel_obj *ch = vb2_get_drv_priv(vq);
286 struct common_obj *common;
290 common = &ch->common[VPIF_VIDEO_INDEX];
292 /* Disable channel as per its device type and channel id */
293 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
295 channel0_intr_enable(0);
297 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
300 channel1_intr_enable(0);
305 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
306 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
307 vpif_dbg(1, debug, "stream off failed in subdev\n");
309 /* release all active buffers */
310 spin_lock_irqsave(&common->irqlock, flags);
311 if (common->cur_frm == common->next_frm) {
312 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
314 if (common->cur_frm != NULL)
315 vb2_buffer_done(&common->cur_frm->vb,
316 VB2_BUF_STATE_ERROR);
317 if (common->next_frm != NULL)
318 vb2_buffer_done(&common->next_frm->vb,
319 VB2_BUF_STATE_ERROR);
322 while (!list_empty(&common->dma_queue)) {
323 common->next_frm = list_entry(common->dma_queue.next,
324 struct vpif_cap_buffer, list);
325 list_del(&common->next_frm->list);
326 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
328 spin_unlock_irqrestore(&common->irqlock, flags);
331 static struct vb2_ops video_qops = {
332 .queue_setup = vpif_buffer_queue_setup,
333 .buf_prepare = vpif_buffer_prepare,
334 .start_streaming = vpif_start_streaming,
335 .stop_streaming = vpif_stop_streaming,
336 .buf_queue = vpif_buffer_queue,
340 * vpif_process_buffer_complete: process a completed buffer
341 * @common: ptr to common channel object
343 * This function time stamp the buffer and mark it as DONE. It also
344 * wake up any process waiting on the QUEUE and set the next buffer
347 static void vpif_process_buffer_complete(struct common_obj *common)
349 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
350 vb2_buffer_done(&common->cur_frm->vb,
352 /* Make curFrm pointing to nextFrm */
353 common->cur_frm = common->next_frm;
357 * vpif_schedule_next_buffer: set next buffer address for capture
358 * @common : ptr to common channel object
360 * This function will get next buffer from the dma queue and
361 * set the buffer address in the vpif register for capture.
362 * the buffer is marked active
364 static void vpif_schedule_next_buffer(struct common_obj *common)
366 unsigned long addr = 0;
368 spin_lock(&common->irqlock);
369 common->next_frm = list_entry(common->dma_queue.next,
370 struct vpif_cap_buffer, list);
371 /* Remove that buffer from the buffer queue */
372 list_del(&common->next_frm->list);
373 spin_unlock(&common->irqlock);
374 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
375 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
377 /* Set top and bottom field addresses in VPIF registers */
378 common->set_addr(addr + common->ytop_off,
379 addr + common->ybtm_off,
380 addr + common->ctop_off,
381 addr + common->cbtm_off);
385 * vpif_channel_isr : ISR handler for vpif capture
387 * @dev_id: dev_id ptr
389 * It changes status of the captured buffer, takes next buffer from the queue
390 * and sets its address in VPIF registers
392 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
394 struct vpif_device *dev = &vpif_obj;
395 struct common_obj *common;
396 struct channel_obj *ch;
397 enum v4l2_field field;
401 channel_id = *(int *)(dev_id);
402 if (!vpif_intr_status(channel_id))
405 ch = dev->dev[channel_id];
407 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
409 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
410 common = &ch->common[i];
411 /* skip If streaming is not started in this channel */
412 /* Check the field format */
413 if (1 == ch->vpifparams.std_info.frm_fmt) {
414 /* Progressive mode */
415 spin_lock(&common->irqlock);
416 if (list_empty(&common->dma_queue)) {
417 spin_unlock(&common->irqlock);
420 spin_unlock(&common->irqlock);
422 if (!channel_first_int[i][channel_id])
423 vpif_process_buffer_complete(common);
425 channel_first_int[i][channel_id] = 0;
427 vpif_schedule_next_buffer(common);
430 channel_first_int[i][channel_id] = 0;
433 * Interlaced mode. If it is first interrupt, ignore
436 if (channel_first_int[i][channel_id]) {
437 channel_first_int[i][channel_id] = 0;
442 /* Get field id from VPIF registers */
443 fid = vpif_channel_getfid(ch->channel_id);
444 if (fid != ch->field_id) {
446 * If field id does not match stored
447 * field id, make them in sync
454 /* device field id and local field id are in sync */
456 /* this is even field */
457 if (common->cur_frm == common->next_frm)
460 /* mark the current buffer as done */
461 vpif_process_buffer_complete(common);
462 } else if (1 == fid) {
464 spin_lock(&common->irqlock);
465 if (list_empty(&common->dma_queue) ||
466 (common->cur_frm != common->next_frm)) {
467 spin_unlock(&common->irqlock);
470 spin_unlock(&common->irqlock);
472 vpif_schedule_next_buffer(common);
480 * vpif_update_std_info() - update standard related info
481 * @ch: ptr to channel object
483 * For a given standard selected by application, update values
484 * in the device data structures
486 static int vpif_update_std_info(struct channel_obj *ch)
488 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
489 struct vpif_params *vpifparams = &ch->vpifparams;
490 const struct vpif_channel_config_params *config;
491 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
492 struct video_obj *vid_ch = &ch->video;
495 vpif_dbg(2, debug, "vpif_update_std_info\n");
497 for (index = 0; index < vpif_ch_params_count; index++) {
498 config = &vpif_ch_params[index];
499 if (config->hd_sd == 0) {
500 vpif_dbg(2, debug, "SD format\n");
501 if (config->stdid & vid_ch->stdid) {
502 memcpy(std_info, config, sizeof(*config));
506 vpif_dbg(2, debug, "HD format\n");
507 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
508 sizeof(vid_ch->dv_timings))) {
509 memcpy(std_info, config, sizeof(*config));
515 /* standard not found */
516 if (index == vpif_ch_params_count)
519 common->fmt.fmt.pix.width = std_info->width;
520 common->width = std_info->width;
521 common->fmt.fmt.pix.height = std_info->height;
522 common->height = std_info->height;
523 common->fmt.fmt.pix.bytesperline = std_info->width;
524 vpifparams->video_params.hpitch = std_info->width;
525 vpifparams->video_params.storage_mode = std_info->frm_fmt;
531 * vpif_calculate_offsets : This function calculates buffers offsets
532 * @ch : ptr to channel object
534 * This function calculates buffer offsets for Y and C in the top and
537 static void vpif_calculate_offsets(struct channel_obj *ch)
539 unsigned int hpitch, vpitch, sizeimage;
540 struct video_obj *vid_ch = &(ch->video);
541 struct vpif_params *vpifparams = &ch->vpifparams;
542 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
543 enum v4l2_field field = common->fmt.fmt.pix.field;
545 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
547 if (V4L2_FIELD_ANY == field) {
548 if (vpifparams->std_info.frm_fmt)
549 vid_ch->buf_field = V4L2_FIELD_NONE;
551 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
553 vid_ch->buf_field = common->fmt.fmt.pix.field;
555 sizeimage = common->fmt.fmt.pix.sizeimage;
557 hpitch = common->fmt.fmt.pix.bytesperline;
558 vpitch = sizeimage / (hpitch * 2);
560 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
561 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
562 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
563 common->ytop_off = 0;
564 common->ybtm_off = hpitch;
565 common->ctop_off = sizeimage / 2;
566 common->cbtm_off = sizeimage / 2 + hpitch;
567 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
568 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
569 common->ytop_off = 0;
570 common->ybtm_off = sizeimage / 4;
571 common->ctop_off = sizeimage / 2;
572 common->cbtm_off = common->ctop_off + sizeimage / 4;
573 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
574 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
575 common->ybtm_off = 0;
576 common->ytop_off = sizeimage / 4;
577 common->cbtm_off = sizeimage / 2;
578 common->ctop_off = common->cbtm_off + sizeimage / 4;
580 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
581 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
582 vpifparams->video_params.storage_mode = 1;
584 vpifparams->video_params.storage_mode = 0;
586 if (1 == vpifparams->std_info.frm_fmt)
587 vpifparams->video_params.hpitch =
588 common->fmt.fmt.pix.bytesperline;
590 if ((field == V4L2_FIELD_ANY)
591 || (field == V4L2_FIELD_INTERLACED))
592 vpifparams->video_params.hpitch =
593 common->fmt.fmt.pix.bytesperline * 2;
595 vpifparams->video_params.hpitch =
596 common->fmt.fmt.pix.bytesperline;
599 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
603 * vpif_config_format: configure default frame format in the device
604 * ch : ptr to channel object
606 static void vpif_config_format(struct channel_obj *ch)
608 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
610 vpif_dbg(2, debug, "vpif_config_format\n");
612 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
613 common->fmt.fmt.pix.sizeimage
614 = config_params.channel_bufsize[ch->channel_id];
616 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
617 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
619 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
620 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
624 * vpif_get_default_field() - Get default field type based on interface
625 * @vpif_params - ptr to vpif params
627 static inline enum v4l2_field vpif_get_default_field(
628 struct vpif_interface *iface)
630 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
631 V4L2_FIELD_INTERLACED;
635 * vpif_check_format() - check given pixel format for compatibility
637 * @pixfmt - Given pixel format
638 * @update - update the values as per hardware requirement
640 * Check the application pixel format for S_FMT and update the input
641 * values as per hardware limits for TRY_FMT. The default pixel and
642 * field format is selected based on interface type.
644 static int vpif_check_format(struct channel_obj *ch,
645 struct v4l2_pix_format *pixfmt,
648 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
649 struct vpif_params *vpif_params = &ch->vpifparams;
650 enum v4l2_field field = pixfmt->field;
651 u32 sizeimage, hpitch, vpitch;
654 vpif_dbg(2, debug, "vpif_check_format\n");
656 * first check for the pixel format. If if_type is Raw bayer,
657 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
658 * V4L2_PIX_FMT_YUV422P is supported
660 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
661 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
663 vpif_dbg(2, debug, "invalid pix format\n");
666 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
669 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
671 vpif_dbg(2, debug, "invalid pixel format\n");
674 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
678 if (!(VPIF_VALID_FIELD(field))) {
680 vpif_dbg(2, debug, "invalid field format\n");
684 * By default use FIELD_NONE for RAW Bayer capture
685 * and FIELD_INTERLACED for other interfaces
687 field = vpif_get_default_field(&vpif_params->iface);
688 } else if (field == V4L2_FIELD_ANY)
689 /* unsupported field. Use default */
690 field = vpif_get_default_field(&vpif_params->iface);
692 /* validate the hpitch */
693 hpitch = pixfmt->bytesperline;
694 if (hpitch < vpif_params->std_info.width) {
696 vpif_dbg(2, debug, "invalid hpitch\n");
699 hpitch = vpif_params->std_info.width;
702 sizeimage = pixfmt->sizeimage;
704 vpitch = sizeimage / (hpitch * 2);
706 /* validate the vpitch */
707 if (vpitch < vpif_params->std_info.height) {
709 vpif_dbg(2, debug, "Invalid vpitch\n");
712 vpitch = vpif_params->std_info.height;
715 /* Check for 8 byte alignment */
716 if (!ALIGN(hpitch, 8)) {
718 vpif_dbg(2, debug, "invalid pitch alignment\n");
721 /* adjust to next 8 byte boundary */
722 hpitch = (((hpitch + 7) / 8) * 8);
724 /* if update is set, modify the bytesperline and sizeimage */
726 pixfmt->bytesperline = hpitch;
727 pixfmt->sizeimage = hpitch * vpitch * 2;
730 * Image width and height is always based on current standard width and
733 pixfmt->width = common->fmt.fmt.pix.width;
734 pixfmt->height = common->fmt.fmt.pix.height;
741 * vpif_config_addr() - function to configure buffer address in vpif
743 * @muxmode - channel mux mode
745 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
747 struct common_obj *common;
749 vpif_dbg(2, debug, "vpif_config_addr\n");
751 common = &(ch->common[VPIF_VIDEO_INDEX]);
753 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
754 common->set_addr = ch1_set_videobuf_addr;
755 else if (2 == muxmode)
756 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
758 common->set_addr = ch0_set_videobuf_addr;
762 * vpif_input_to_subdev() - Maps input to sub device
763 * @vpif_cfg - global config ptr
764 * @chan_cfg - channel config ptr
765 * @input_index - Given input index from application
767 * lookup the sub device information for a given input index.
768 * we report all the inputs to application. inputs table also
769 * has sub device name for the each input
771 static int vpif_input_to_subdev(
772 struct vpif_capture_config *vpif_cfg,
773 struct vpif_capture_chan_config *chan_cfg,
776 struct vpif_subdev_info *subdev_info;
777 const char *subdev_name;
780 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
782 subdev_name = chan_cfg->inputs[input_index].subdev_name;
783 if (subdev_name == NULL)
786 /* loop through the sub device list to get the sub device info */
787 for (i = 0; i < vpif_cfg->subdev_count; i++) {
788 subdev_info = &vpif_cfg->subdev_info[i];
789 if (!strcmp(subdev_info->name, subdev_name))
796 * vpif_set_input() - Select an input
797 * @vpif_cfg - global config ptr
799 * @_index - Given input index from application
801 * Select the given input.
803 static int vpif_set_input(
804 struct vpif_capture_config *vpif_cfg,
805 struct channel_obj *ch,
808 struct vpif_capture_chan_config *chan_cfg =
809 &vpif_cfg->chan_config[ch->channel_id];
810 struct vpif_subdev_info *subdev_info = NULL;
811 struct v4l2_subdev *sd = NULL;
812 u32 input = 0, output = 0;
816 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
818 sd = vpif_obj.sd[sd_index];
819 subdev_info = &vpif_cfg->subdev_info[sd_index];
822 /* first setup input path from sub device to vpif */
823 if (sd && vpif_cfg->setup_input_path) {
824 ret = vpif_cfg->setup_input_path(ch->channel_id,
827 vpif_dbg(1, debug, "couldn't setup input path for the" \
828 " sub device %s, for input index %d\n",
829 subdev_info->name, index);
835 input = chan_cfg->inputs[index].input_route;
836 output = chan_cfg->inputs[index].output_route;
837 ret = v4l2_subdev_call(sd, video, s_routing,
839 if (ret < 0 && ret != -ENOIOCTLCMD) {
840 vpif_dbg(1, debug, "Failed to set input\n");
844 ch->input_idx = index;
846 /* copy interface parameters to vpif */
847 ch->vpifparams.iface = chan_cfg->vpif_if;
849 /* update tvnorms from the sub device input info */
850 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
855 * vpif_querystd() - querystd handler
858 * @std_id: ptr to std id
860 * This function is called to detect standard at the selected input
862 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
864 struct video_device *vdev = video_devdata(file);
865 struct channel_obj *ch = video_get_drvdata(vdev);
868 vpif_dbg(2, debug, "vpif_querystd\n");
870 /* Call querystd function of decoder device */
871 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
873 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
876 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
884 * vpif_g_std() - get STD handler
887 * @std_id: ptr to std id
889 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
891 struct vpif_capture_config *config = vpif_dev->platform_data;
892 struct video_device *vdev = video_devdata(file);
893 struct channel_obj *ch = video_get_drvdata(vdev);
894 struct vpif_capture_chan_config *chan_cfg;
895 struct v4l2_input input;
897 vpif_dbg(2, debug, "vpif_g_std\n");
899 if (config->chan_config[ch->channel_id].inputs == NULL)
902 chan_cfg = &config->chan_config[ch->channel_id];
903 input = chan_cfg->inputs[ch->input_idx].input;
904 if (input.capabilities != V4L2_IN_CAP_STD)
907 *std = ch->video.stdid;
912 * vpif_s_std() - set STD handler
915 * @std_id: ptr to std id
917 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
919 struct vpif_capture_config *config = vpif_dev->platform_data;
920 struct video_device *vdev = video_devdata(file);
921 struct channel_obj *ch = video_get_drvdata(vdev);
922 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
923 struct vpif_capture_chan_config *chan_cfg;
924 struct v4l2_input input;
927 vpif_dbg(2, debug, "vpif_s_std\n");
929 if (config->chan_config[ch->channel_id].inputs == NULL)
932 chan_cfg = &config->chan_config[ch->channel_id];
933 input = chan_cfg->inputs[ch->input_idx].input;
934 if (input.capabilities != V4L2_IN_CAP_STD)
937 if (vb2_is_busy(&common->buffer_queue))
940 /* Call encoder subdevice function to set the standard */
941 ch->video.stdid = std_id;
942 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
944 /* Get the information about the standard */
945 if (vpif_update_std_info(ch)) {
946 vpif_err("Error getting the standard info\n");
950 /* Configure the default format information */
951 vpif_config_format(ch);
953 /* set standard in the sub device */
954 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
955 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
956 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
963 * vpif_enum_input() - ENUMINPUT handler
966 * @input: ptr to input structure
968 static int vpif_enum_input(struct file *file, void *priv,
969 struct v4l2_input *input)
972 struct vpif_capture_config *config = vpif_dev->platform_data;
973 struct video_device *vdev = video_devdata(file);
974 struct channel_obj *ch = video_get_drvdata(vdev);
975 struct vpif_capture_chan_config *chan_cfg;
977 chan_cfg = &config->chan_config[ch->channel_id];
979 if (input->index >= chan_cfg->input_count) {
980 vpif_dbg(1, debug, "Invalid input index\n");
984 memcpy(input, &chan_cfg->inputs[input->index].input,
990 * vpif_g_input() - Get INPUT handler
993 * @index: ptr to input index
995 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
997 struct video_device *vdev = video_devdata(file);
998 struct channel_obj *ch = video_get_drvdata(vdev);
1000 *index = ch->input_idx;
1005 * vpif_s_input() - Set INPUT handler
1007 * @priv: file handle
1008 * @index: input index
1010 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1012 struct vpif_capture_config *config = vpif_dev->platform_data;
1013 struct video_device *vdev = video_devdata(file);
1014 struct channel_obj *ch = video_get_drvdata(vdev);
1015 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1016 struct vpif_capture_chan_config *chan_cfg;
1018 chan_cfg = &config->chan_config[ch->channel_id];
1020 if (index >= chan_cfg->input_count)
1023 if (vb2_is_busy(&common->buffer_queue))
1026 return vpif_set_input(config, ch, index);
1030 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1032 * @priv: file handle
1033 * @index: input index
1035 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1036 struct v4l2_fmtdesc *fmt)
1038 struct video_device *vdev = video_devdata(file);
1039 struct channel_obj *ch = video_get_drvdata(vdev);
1041 if (fmt->index != 0) {
1042 vpif_dbg(1, debug, "Invalid format index\n");
1046 /* Fill in the information about format */
1047 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1048 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1049 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1050 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1052 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1053 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1054 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1060 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1062 * @priv: file handle
1063 * @fmt: ptr to v4l2 format structure
1065 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1066 struct v4l2_format *fmt)
1068 struct video_device *vdev = video_devdata(file);
1069 struct channel_obj *ch = video_get_drvdata(vdev);
1070 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1072 return vpif_check_format(ch, pixfmt, 1);
1077 * vpif_g_fmt_vid_cap() - Set INPUT handler
1079 * @priv: file handle
1080 * @fmt: ptr to v4l2 format structure
1082 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1083 struct v4l2_format *fmt)
1085 struct video_device *vdev = video_devdata(file);
1086 struct channel_obj *ch = video_get_drvdata(vdev);
1087 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1089 /* Check the validity of the buffer type */
1090 if (common->fmt.type != fmt->type)
1093 /* Fill in the information about format */
1099 * vpif_s_fmt_vid_cap() - Set FMT handler
1101 * @priv: file handle
1102 * @fmt: ptr to v4l2 format structure
1104 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1105 struct v4l2_format *fmt)
1107 struct video_device *vdev = video_devdata(file);
1108 struct channel_obj *ch = video_get_drvdata(vdev);
1109 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1110 struct v4l2_pix_format *pixfmt;
1113 vpif_dbg(2, debug, "%s\n", __func__);
1115 if (vb2_is_busy(&common->buffer_queue))
1118 pixfmt = &fmt->fmt.pix;
1119 /* Check for valid field format */
1120 ret = vpif_check_format(ch, pixfmt, 0);
1124 /* store the format in the channel object */
1130 * vpif_querycap() - QUERYCAP handler
1132 * @priv: file handle
1133 * @cap: ptr to v4l2_capability structure
1135 static int vpif_querycap(struct file *file, void *priv,
1136 struct v4l2_capability *cap)
1138 struct vpif_capture_config *config = vpif_dev->platform_data;
1140 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1141 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1142 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
1143 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1144 dev_name(vpif_dev));
1145 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1151 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1153 * @priv: file handle
1154 * @timings: input timings
1157 vpif_enum_dv_timings(struct file *file, void *priv,
1158 struct v4l2_enum_dv_timings *timings)
1160 struct vpif_capture_config *config = vpif_dev->platform_data;
1161 struct video_device *vdev = video_devdata(file);
1162 struct channel_obj *ch = video_get_drvdata(vdev);
1163 struct vpif_capture_chan_config *chan_cfg;
1164 struct v4l2_input input;
1167 if (config->chan_config[ch->channel_id].inputs == NULL)
1170 chan_cfg = &config->chan_config[ch->channel_id];
1171 input = chan_cfg->inputs[ch->input_idx].input;
1172 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1177 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
1178 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1185 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1187 * @priv: file handle
1188 * @timings: input timings
1191 vpif_query_dv_timings(struct file *file, void *priv,
1192 struct v4l2_dv_timings *timings)
1194 struct vpif_capture_config *config = vpif_dev->platform_data;
1195 struct video_device *vdev = video_devdata(file);
1196 struct channel_obj *ch = video_get_drvdata(vdev);
1197 struct vpif_capture_chan_config *chan_cfg;
1198 struct v4l2_input input;
1201 if (config->chan_config[ch->channel_id].inputs == NULL)
1204 chan_cfg = &config->chan_config[ch->channel_id];
1205 input = chan_cfg->inputs[ch->input_idx].input;
1206 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1209 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1210 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1217 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1219 * @priv: file handle
1220 * @timings: digital video timings
1222 static int vpif_s_dv_timings(struct file *file, void *priv,
1223 struct v4l2_dv_timings *timings)
1225 struct vpif_capture_config *config = vpif_dev->platform_data;
1226 struct video_device *vdev = video_devdata(file);
1227 struct channel_obj *ch = video_get_drvdata(vdev);
1228 struct vpif_params *vpifparams = &ch->vpifparams;
1229 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1230 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1231 struct video_obj *vid_ch = &ch->video;
1232 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1233 struct vpif_capture_chan_config *chan_cfg;
1234 struct v4l2_input input;
1237 if (config->chan_config[ch->channel_id].inputs == NULL)
1240 chan_cfg = &config->chan_config[ch->channel_id];
1241 input = chan_cfg->inputs[ch->input_idx].input;
1242 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1245 if (timings->type != V4L2_DV_BT_656_1120) {
1246 vpif_dbg(2, debug, "Timing type not defined\n");
1250 if (vb2_is_busy(&common->buffer_queue))
1253 /* Configure subdevice timings, if any */
1254 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1255 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1258 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1262 if (!(timings->bt.width && timings->bt.height &&
1263 (timings->bt.hbackporch ||
1264 timings->bt.hfrontporch ||
1265 timings->bt.hsync) &&
1266 timings->bt.vfrontporch &&
1267 (timings->bt.vbackporch ||
1268 timings->bt.vsync))) {
1269 vpif_dbg(2, debug, "Timings for width, height, "
1270 "horizontal back porch, horizontal sync, "
1271 "horizontal front porch, vertical back porch, "
1272 "vertical sync and vertical back porch "
1273 "must be defined\n");
1277 vid_ch->dv_timings = *timings;
1279 /* Configure video port timings */
1281 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1282 std_info->sav2eav = bt->width;
1285 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1287 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1288 if (bt->interlaced) {
1289 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1290 std_info->l5 = std_info->vsize/2 -
1291 (bt->vfrontporch - 1);
1292 std_info->l7 = std_info->vsize/2 + 1;
1293 std_info->l9 = std_info->l7 + bt->il_vsync +
1294 bt->il_vbackporch + 1;
1295 std_info->l11 = std_info->vsize -
1296 (bt->il_vfrontporch - 1);
1298 vpif_dbg(2, debug, "Required timing values for "
1299 "interlaced BT format missing\n");
1303 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1305 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1306 std_info->width = bt->width;
1307 std_info->height = bt->height;
1308 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1309 std_info->ycmux_mode = 0;
1310 std_info->capture_format = 0;
1311 std_info->vbi_supported = 0;
1312 std_info->hd_sd = 1;
1313 std_info->stdid = 0;
1320 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1322 * @priv: file handle
1323 * @timings: digital video timings
1325 static int vpif_g_dv_timings(struct file *file, void *priv,
1326 struct v4l2_dv_timings *timings)
1328 struct vpif_capture_config *config = vpif_dev->platform_data;
1329 struct video_device *vdev = video_devdata(file);
1330 struct channel_obj *ch = video_get_drvdata(vdev);
1331 struct video_obj *vid_ch = &ch->video;
1332 struct vpif_capture_chan_config *chan_cfg;
1333 struct v4l2_input input;
1335 if (config->chan_config[ch->channel_id].inputs == NULL)
1338 chan_cfg = &config->chan_config[ch->channel_id];
1339 input = chan_cfg->inputs[ch->input_idx].input;
1340 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1343 *timings = vid_ch->dv_timings;
1349 * vpif_log_status() - Status information
1351 * @priv: file handle
1355 static int vpif_log_status(struct file *filep, void *priv)
1357 /* status for sub devices */
1358 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1363 /* vpif capture ioctl operations */
1364 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1365 .vidioc_querycap = vpif_querycap,
1366 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1367 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1368 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1369 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1371 .vidioc_enum_input = vpif_enum_input,
1372 .vidioc_s_input = vpif_s_input,
1373 .vidioc_g_input = vpif_g_input,
1375 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1376 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1377 .vidioc_querybuf = vb2_ioctl_querybuf,
1378 .vidioc_qbuf = vb2_ioctl_qbuf,
1379 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1380 .vidioc_expbuf = vb2_ioctl_expbuf,
1381 .vidioc_streamon = vb2_ioctl_streamon,
1382 .vidioc_streamoff = vb2_ioctl_streamoff,
1384 .vidioc_querystd = vpif_querystd,
1385 .vidioc_s_std = vpif_s_std,
1386 .vidioc_g_std = vpif_g_std,
1388 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1389 .vidioc_query_dv_timings = vpif_query_dv_timings,
1390 .vidioc_s_dv_timings = vpif_s_dv_timings,
1391 .vidioc_g_dv_timings = vpif_g_dv_timings,
1393 .vidioc_log_status = vpif_log_status,
1396 /* vpif file operations */
1397 static struct v4l2_file_operations vpif_fops = {
1398 .owner = THIS_MODULE,
1399 .open = v4l2_fh_open,
1400 .release = vb2_fop_release,
1401 .unlocked_ioctl = video_ioctl2,
1402 .mmap = vb2_fop_mmap,
1403 .poll = vb2_fop_poll
1407 * initialize_vpif() - Initialize vpif data structures
1409 * Allocate memory for data structures and initialize them
1411 static int initialize_vpif(void)
1414 int free_channel_objects_index;
1416 /* Default number of buffers should be 3 */
1417 if ((ch0_numbuffers > 0) &&
1418 (ch0_numbuffers < config_params.min_numbuffers))
1419 ch0_numbuffers = config_params.min_numbuffers;
1420 if ((ch1_numbuffers > 0) &&
1421 (ch1_numbuffers < config_params.min_numbuffers))
1422 ch1_numbuffers = config_params.min_numbuffers;
1424 /* Set buffer size to min buffers size if it is invalid */
1425 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1427 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1428 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1430 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1432 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1433 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1434 if (ch0_numbuffers) {
1435 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1438 if (ch1_numbuffers) {
1439 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1443 /* Allocate memory for six channel objects */
1444 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1446 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1447 /* If memory allocation fails, return error */
1448 if (!vpif_obj.dev[i]) {
1449 free_channel_objects_index = i;
1451 goto vpif_init_free_channel_objects;
1456 vpif_init_free_channel_objects:
1457 for (j = 0; j < free_channel_objects_index; j++)
1458 kfree(vpif_obj.dev[j]);
1462 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1463 struct v4l2_subdev *subdev,
1464 struct v4l2_async_subdev *asd)
1468 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1469 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1471 vpif_obj.sd[i] = subdev;
1478 static int vpif_probe_complete(void)
1480 struct common_obj *common;
1481 struct video_device *vdev;
1482 struct channel_obj *ch;
1483 struct vb2_queue *q;
1486 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1487 ch = vpif_obj.dev[j];
1489 common = &(ch->common[VPIF_VIDEO_INDEX]);
1490 spin_lock_init(&common->irqlock);
1491 mutex_init(&common->lock);
1493 /* select input 0 */
1494 err = vpif_set_input(vpif_obj.config, ch, 0);
1498 /* Initialize vb2 queue */
1499 q = &common->buffer_queue;
1500 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1501 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1503 q->ops = &video_qops;
1504 q->mem_ops = &vb2_dma_contig_memops;
1505 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1506 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1507 q->min_buffers_needed = 1;
1508 q->lock = &common->lock;
1510 err = vb2_queue_init(q);
1512 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1516 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1517 if (IS_ERR(common->alloc_ctx)) {
1518 vpif_err("Failed to get the context\n");
1519 err = PTR_ERR(common->alloc_ctx);
1523 INIT_LIST_HEAD(&common->dma_queue);
1525 /* Initialize the video_device structure */
1526 vdev = ch->video_dev;
1527 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1528 vdev->release = video_device_release;
1529 vdev->fops = &vpif_fops;
1530 vdev->ioctl_ops = &vpif_ioctl_ops;
1531 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1532 vdev->vfl_dir = VFL_DIR_RX;
1534 vdev->lock = &common->lock;
1535 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1536 video_set_drvdata(ch->video_dev, ch);
1537 err = video_register_device(vdev,
1538 VFL_TYPE_GRABBER, (j ? 1 : 0));
1543 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1547 for (k = 0; k < j; k++) {
1548 /* Get the pointer to the channel object */
1549 ch = vpif_obj.dev[k];
1550 common = &ch->common[k];
1551 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1552 /* Unregister video device */
1553 video_unregister_device(ch->video_dev);
1556 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1557 ch = vpif_obj.dev[i];
1558 /* Note: does nothing if ch->video_dev == NULL */
1559 video_device_release(ch->video_dev);
1561 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1566 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1568 return vpif_probe_complete();
1572 * vpif_probe : This function probes the vpif capture driver
1573 * @pdev: platform device pointer
1575 * This creates device entries by register itself to the V4L2 driver and
1576 * initializes fields of each channel objects
1578 static __init int vpif_probe(struct platform_device *pdev)
1580 struct vpif_subdev_info *subdevdata;
1583 struct i2c_adapter *i2c_adap;
1584 struct channel_obj *ch;
1585 struct video_device *vfd;
1586 struct resource *res;
1589 vpif_dev = &pdev->dev;
1591 err = initialize_vpif();
1593 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1597 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1599 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1603 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1604 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1605 IRQF_SHARED, VPIF_DRIVER_NAME,
1606 (void *)(&vpif_obj.dev[res_idx]->
1610 goto vpif_unregister;
1615 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1616 /* Get the pointer to the channel object */
1617 ch = vpif_obj.dev[i];
1618 /* Allocate memory for video device */
1619 vfd = video_device_alloc();
1621 for (j = 0; j < i; j++) {
1622 ch = vpif_obj.dev[j];
1623 video_device_release(ch->video_dev);
1626 goto vpif_unregister;
1629 /* Set video_dev to the video device */
1630 ch->video_dev = vfd;
1633 vpif_obj.config = pdev->dev.platform_data;
1635 subdev_count = vpif_obj.config->subdev_count;
1636 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1638 if (vpif_obj.sd == NULL) {
1639 vpif_err("unable to allocate memory for subdevice pointers\n");
1644 if (!vpif_obj.config->asd_sizes) {
1645 i2c_adap = i2c_get_adapter(1);
1646 for (i = 0; i < subdev_count; i++) {
1647 subdevdata = &vpif_obj.config->subdev_info[i];
1649 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1655 if (!vpif_obj.sd[i]) {
1656 vpif_err("Error registering v4l2 subdevice\n");
1658 goto probe_subdev_out;
1660 v4l2_info(&vpif_obj.v4l2_dev,
1661 "registered sub device %s\n",
1664 vpif_probe_complete();
1666 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1667 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1668 vpif_obj.notifier.bound = vpif_async_bound;
1669 vpif_obj.notifier.complete = vpif_async_complete;
1670 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1671 &vpif_obj.notifier);
1673 vpif_err("Error registering async notifier\n");
1675 goto probe_subdev_out;
1682 /* free sub devices memory */
1686 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1687 ch = vpif_obj.dev[i];
1688 /* Note: does nothing if ch->video_dev == NULL */
1689 video_device_release(ch->video_dev);
1692 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1698 * vpif_remove() - driver remove handler
1699 * @device: ptr to platform device structure
1701 * The vidoe device is unregistered
1703 static int vpif_remove(struct platform_device *device)
1705 struct common_obj *common;
1706 struct channel_obj *ch;
1709 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1712 /* un-register device */
1713 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1714 /* Get the pointer to the channel object */
1715 ch = vpif_obj.dev[i];
1716 common = &ch->common[i];
1717 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1718 /* Unregister video device */
1719 video_unregister_device(ch->video_dev);
1720 kfree(vpif_obj.dev[i]);
1725 #ifdef CONFIG_PM_SLEEP
1727 * vpif_suspend: vpif device suspend
1729 static int vpif_suspend(struct device *dev)
1732 struct common_obj *common;
1733 struct channel_obj *ch;
1736 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1737 /* Get the pointer to the channel object */
1738 ch = vpif_obj.dev[i];
1739 common = &ch->common[VPIF_VIDEO_INDEX];
1741 if (!vb2_is_streaming(&common->buffer_queue))
1744 mutex_lock(&common->lock);
1745 /* Disable channel */
1746 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1748 channel0_intr_enable(0);
1750 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1753 channel1_intr_enable(0);
1755 mutex_unlock(&common->lock);
1762 * vpif_resume: vpif device suspend
1764 static int vpif_resume(struct device *dev)
1766 struct common_obj *common;
1767 struct channel_obj *ch;
1770 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1771 /* Get the pointer to the channel object */
1772 ch = vpif_obj.dev[i];
1773 common = &ch->common[VPIF_VIDEO_INDEX];
1775 if (!vb2_is_streaming(&common->buffer_queue))
1778 mutex_lock(&common->lock);
1779 /* Enable channel */
1780 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1782 channel0_intr_enable(1);
1784 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1787 channel1_intr_enable(1);
1789 mutex_unlock(&common->lock);
1796 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1798 static __refdata struct platform_driver vpif_driver = {
1800 .name = VPIF_DRIVER_NAME,
1801 .owner = THIS_MODULE,
1804 .probe = vpif_probe,
1805 .remove = vpif_remove,
1808 module_platform_driver(vpif_driver);