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
);
272 spin_unlock_irqrestore(&common
->irqlock
, flags
);
278 * vpif_stop_streaming : Stop the DMA engine
279 * @vq: ptr to vb2_queue
281 * This callback stops the DMA engine and any remaining buffers
282 * in the DMA queue are released.
284 static void vpif_stop_streaming(struct vb2_queue
*vq
)
286 struct channel_obj
*ch
= vb2_get_drv_priv(vq
);
287 struct common_obj
*common
;
291 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
293 /* Disable channel as per its device type and channel id */
294 if (VPIF_CHANNEL0_VIDEO
== ch
->channel_id
) {
296 channel0_intr_enable(0);
298 if (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
||
301 channel1_intr_enable(0);
306 ret
= v4l2_subdev_call(ch
->sd
, video
, s_stream
, 0);
307 if (ret
&& ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
308 vpif_dbg(1, debug
, "stream off failed in subdev\n");
310 /* release all active buffers */
311 spin_lock_irqsave(&common
->irqlock
, flags
);
312 if (common
->cur_frm
== common
->next_frm
) {
313 vb2_buffer_done(&common
->cur_frm
->vb
, VB2_BUF_STATE_ERROR
);
315 if (common
->cur_frm
!= NULL
)
316 vb2_buffer_done(&common
->cur_frm
->vb
,
317 VB2_BUF_STATE_ERROR
);
318 if (common
->next_frm
!= NULL
)
319 vb2_buffer_done(&common
->next_frm
->vb
,
320 VB2_BUF_STATE_ERROR
);
323 while (!list_empty(&common
->dma_queue
)) {
324 common
->next_frm
= list_entry(common
->dma_queue
.next
,
325 struct vpif_cap_buffer
, list
);
326 list_del(&common
->next_frm
->list
);
327 vb2_buffer_done(&common
->next_frm
->vb
, VB2_BUF_STATE_ERROR
);
329 spin_unlock_irqrestore(&common
->irqlock
, flags
);
332 static struct vb2_ops video_qops
= {
333 .queue_setup
= vpif_buffer_queue_setup
,
334 .buf_prepare
= vpif_buffer_prepare
,
335 .start_streaming
= vpif_start_streaming
,
336 .stop_streaming
= vpif_stop_streaming
,
337 .buf_queue
= vpif_buffer_queue
,
341 * vpif_process_buffer_complete: process a completed buffer
342 * @common: ptr to common channel object
344 * This function time stamp the buffer and mark it as DONE. It also
345 * wake up any process waiting on the QUEUE and set the next buffer
348 static void vpif_process_buffer_complete(struct common_obj
*common
)
350 v4l2_get_timestamp(&common
->cur_frm
->vb
.v4l2_buf
.timestamp
);
351 vb2_buffer_done(&common
->cur_frm
->vb
,
353 /* Make curFrm pointing to nextFrm */
354 common
->cur_frm
= common
->next_frm
;
358 * vpif_schedule_next_buffer: set next buffer address for capture
359 * @common : ptr to common channel object
361 * This function will get next buffer from the dma queue and
362 * set the buffer address in the vpif register for capture.
363 * the buffer is marked active
365 static void vpif_schedule_next_buffer(struct common_obj
*common
)
367 unsigned long addr
= 0;
369 spin_lock(&common
->irqlock
);
370 common
->next_frm
= list_entry(common
->dma_queue
.next
,
371 struct vpif_cap_buffer
, list
);
372 /* Remove that buffer from the buffer queue */
373 list_del(&common
->next_frm
->list
);
374 spin_unlock(&common
->irqlock
);
375 common
->next_frm
->vb
.state
= VB2_BUF_STATE_ACTIVE
;
376 addr
= vb2_dma_contig_plane_dma_addr(&common
->next_frm
->vb
, 0);
378 /* Set top and bottom field addresses in VPIF registers */
379 common
->set_addr(addr
+ common
->ytop_off
,
380 addr
+ common
->ybtm_off
,
381 addr
+ common
->ctop_off
,
382 addr
+ common
->cbtm_off
);
386 * vpif_channel_isr : ISR handler for vpif capture
388 * @dev_id: dev_id ptr
390 * It changes status of the captured buffer, takes next buffer from the queue
391 * and sets its address in VPIF registers
393 static irqreturn_t
vpif_channel_isr(int irq
, void *dev_id
)
395 struct vpif_device
*dev
= &vpif_obj
;
396 struct common_obj
*common
;
397 struct channel_obj
*ch
;
398 enum v4l2_field field
;
402 channel_id
= *(int *)(dev_id
);
403 if (!vpif_intr_status(channel_id
))
406 ch
= dev
->dev
[channel_id
];
408 field
= ch
->common
[VPIF_VIDEO_INDEX
].fmt
.fmt
.pix
.field
;
410 for (i
= 0; i
< VPIF_NUMBER_OF_OBJECTS
; i
++) {
411 common
= &ch
->common
[i
];
412 /* skip If streaming is not started in this channel */
413 /* Check the field format */
414 if (1 == ch
->vpifparams
.std_info
.frm_fmt
) {
415 /* Progressive mode */
416 spin_lock(&common
->irqlock
);
417 if (list_empty(&common
->dma_queue
)) {
418 spin_unlock(&common
->irqlock
);
421 spin_unlock(&common
->irqlock
);
423 if (!channel_first_int
[i
][channel_id
])
424 vpif_process_buffer_complete(common
);
426 channel_first_int
[i
][channel_id
] = 0;
428 vpif_schedule_next_buffer(common
);
431 channel_first_int
[i
][channel_id
] = 0;
434 * Interlaced mode. If it is first interrupt, ignore
437 if (channel_first_int
[i
][channel_id
]) {
438 channel_first_int
[i
][channel_id
] = 0;
443 /* Get field id from VPIF registers */
444 fid
= vpif_channel_getfid(ch
->channel_id
);
445 if (fid
!= ch
->field_id
) {
447 * If field id does not match stored
448 * field id, make them in sync
455 /* device field id and local field id are in sync */
457 /* this is even field */
458 if (common
->cur_frm
== common
->next_frm
)
461 /* mark the current buffer as done */
462 vpif_process_buffer_complete(common
);
463 } else if (1 == fid
) {
465 spin_lock(&common
->irqlock
);
466 if (list_empty(&common
->dma_queue
) ||
467 (common
->cur_frm
!= common
->next_frm
)) {
468 spin_unlock(&common
->irqlock
);
471 spin_unlock(&common
->irqlock
);
473 vpif_schedule_next_buffer(common
);
481 * vpif_update_std_info() - update standard related info
482 * @ch: ptr to channel object
484 * For a given standard selected by application, update values
485 * in the device data structures
487 static int vpif_update_std_info(struct channel_obj
*ch
)
489 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
490 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
491 const struct vpif_channel_config_params
*config
;
492 struct vpif_channel_config_params
*std_info
= &vpifparams
->std_info
;
493 struct video_obj
*vid_ch
= &ch
->video
;
496 vpif_dbg(2, debug
, "vpif_update_std_info\n");
498 for (index
= 0; index
< vpif_ch_params_count
; index
++) {
499 config
= &vpif_ch_params
[index
];
500 if (config
->hd_sd
== 0) {
501 vpif_dbg(2, debug
, "SD format\n");
502 if (config
->stdid
& vid_ch
->stdid
) {
503 memcpy(std_info
, config
, sizeof(*config
));
507 vpif_dbg(2, debug
, "HD format\n");
508 if (!memcmp(&config
->dv_timings
, &vid_ch
->dv_timings
,
509 sizeof(vid_ch
->dv_timings
))) {
510 memcpy(std_info
, config
, sizeof(*config
));
516 /* standard not found */
517 if (index
== vpif_ch_params_count
)
520 common
->fmt
.fmt
.pix
.width
= std_info
->width
;
521 common
->width
= std_info
->width
;
522 common
->fmt
.fmt
.pix
.height
= std_info
->height
;
523 common
->height
= std_info
->height
;
524 common
->fmt
.fmt
.pix
.bytesperline
= std_info
->width
;
525 vpifparams
->video_params
.hpitch
= std_info
->width
;
526 vpifparams
->video_params
.storage_mode
= std_info
->frm_fmt
;
532 * vpif_calculate_offsets : This function calculates buffers offsets
533 * @ch : ptr to channel object
535 * This function calculates buffer offsets for Y and C in the top and
538 static void vpif_calculate_offsets(struct channel_obj
*ch
)
540 unsigned int hpitch
, vpitch
, sizeimage
;
541 struct video_obj
*vid_ch
= &(ch
->video
);
542 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
543 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
544 enum v4l2_field field
= common
->fmt
.fmt
.pix
.field
;
546 vpif_dbg(2, debug
, "vpif_calculate_offsets\n");
548 if (V4L2_FIELD_ANY
== field
) {
549 if (vpifparams
->std_info
.frm_fmt
)
550 vid_ch
->buf_field
= V4L2_FIELD_NONE
;
552 vid_ch
->buf_field
= V4L2_FIELD_INTERLACED
;
554 vid_ch
->buf_field
= common
->fmt
.fmt
.pix
.field
;
556 sizeimage
= common
->fmt
.fmt
.pix
.sizeimage
;
558 hpitch
= common
->fmt
.fmt
.pix
.bytesperline
;
559 vpitch
= sizeimage
/ (hpitch
* 2);
561 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
562 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
)) {
563 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
564 common
->ytop_off
= 0;
565 common
->ybtm_off
= hpitch
;
566 common
->ctop_off
= sizeimage
/ 2;
567 common
->cbtm_off
= sizeimage
/ 2 + hpitch
;
568 } else if (V4L2_FIELD_SEQ_TB
== vid_ch
->buf_field
) {
569 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
570 common
->ytop_off
= 0;
571 common
->ybtm_off
= sizeimage
/ 4;
572 common
->ctop_off
= sizeimage
/ 2;
573 common
->cbtm_off
= common
->ctop_off
+ sizeimage
/ 4;
574 } else if (V4L2_FIELD_SEQ_BT
== vid_ch
->buf_field
) {
575 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
576 common
->ybtm_off
= 0;
577 common
->ytop_off
= sizeimage
/ 4;
578 common
->cbtm_off
= sizeimage
/ 2;
579 common
->ctop_off
= common
->cbtm_off
+ sizeimage
/ 4;
581 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
582 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
))
583 vpifparams
->video_params
.storage_mode
= 1;
585 vpifparams
->video_params
.storage_mode
= 0;
587 if (1 == vpifparams
->std_info
.frm_fmt
)
588 vpifparams
->video_params
.hpitch
=
589 common
->fmt
.fmt
.pix
.bytesperline
;
591 if ((field
== V4L2_FIELD_ANY
)
592 || (field
== V4L2_FIELD_INTERLACED
))
593 vpifparams
->video_params
.hpitch
=
594 common
->fmt
.fmt
.pix
.bytesperline
* 2;
596 vpifparams
->video_params
.hpitch
=
597 common
->fmt
.fmt
.pix
.bytesperline
;
600 ch
->vpifparams
.video_params
.stdid
= vpifparams
->std_info
.stdid
;
604 * vpif_config_format: configure default frame format in the device
605 * ch : ptr to channel object
607 static void vpif_config_format(struct channel_obj
*ch
)
609 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
611 vpif_dbg(2, debug
, "vpif_config_format\n");
613 common
->fmt
.fmt
.pix
.field
= V4L2_FIELD_ANY
;
614 common
->fmt
.fmt
.pix
.sizeimage
615 = config_params
.channel_bufsize
[ch
->channel_id
];
617 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
)
618 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_SBGGR8
;
620 common
->fmt
.fmt
.pix
.pixelformat
= V4L2_PIX_FMT_YUV422P
;
621 common
->fmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
625 * vpif_get_default_field() - Get default field type based on interface
626 * @vpif_params - ptr to vpif params
628 static inline enum v4l2_field
vpif_get_default_field(
629 struct vpif_interface
*iface
)
631 return (iface
->if_type
== VPIF_IF_RAW_BAYER
) ? V4L2_FIELD_NONE
:
632 V4L2_FIELD_INTERLACED
;
636 * vpif_check_format() - check given pixel format for compatibility
638 * @pixfmt - Given pixel format
639 * @update - update the values as per hardware requirement
641 * Check the application pixel format for S_FMT and update the input
642 * values as per hardware limits for TRY_FMT. The default pixel and
643 * field format is selected based on interface type.
645 static int vpif_check_format(struct channel_obj
*ch
,
646 struct v4l2_pix_format
*pixfmt
,
649 struct common_obj
*common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
650 struct vpif_params
*vpif_params
= &ch
->vpifparams
;
651 enum v4l2_field field
= pixfmt
->field
;
652 u32 sizeimage
, hpitch
, vpitch
;
655 vpif_dbg(2, debug
, "vpif_check_format\n");
657 * first check for the pixel format. If if_type is Raw bayer,
658 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
659 * V4L2_PIX_FMT_YUV422P is supported
661 if (vpif_params
->iface
.if_type
== VPIF_IF_RAW_BAYER
) {
662 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_SBGGR8
) {
664 vpif_dbg(2, debug
, "invalid pix format\n");
667 pixfmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
670 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_YUV422P
) {
672 vpif_dbg(2, debug
, "invalid pixel format\n");
675 pixfmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
679 if (!(VPIF_VALID_FIELD(field
))) {
681 vpif_dbg(2, debug
, "invalid field format\n");
685 * By default use FIELD_NONE for RAW Bayer capture
686 * and FIELD_INTERLACED for other interfaces
688 field
= vpif_get_default_field(&vpif_params
->iface
);
689 } else if (field
== V4L2_FIELD_ANY
)
690 /* unsupported field. Use default */
691 field
= vpif_get_default_field(&vpif_params
->iface
);
693 /* validate the hpitch */
694 hpitch
= pixfmt
->bytesperline
;
695 if (hpitch
< vpif_params
->std_info
.width
) {
697 vpif_dbg(2, debug
, "invalid hpitch\n");
700 hpitch
= vpif_params
->std_info
.width
;
703 sizeimage
= pixfmt
->sizeimage
;
705 vpitch
= sizeimage
/ (hpitch
* 2);
707 /* validate the vpitch */
708 if (vpitch
< vpif_params
->std_info
.height
) {
710 vpif_dbg(2, debug
, "Invalid vpitch\n");
713 vpitch
= vpif_params
->std_info
.height
;
716 /* Check for 8 byte alignment */
717 if (!ALIGN(hpitch
, 8)) {
719 vpif_dbg(2, debug
, "invalid pitch alignment\n");
722 /* adjust to next 8 byte boundary */
723 hpitch
= (((hpitch
+ 7) / 8) * 8);
725 /* if update is set, modify the bytesperline and sizeimage */
727 pixfmt
->bytesperline
= hpitch
;
728 pixfmt
->sizeimage
= hpitch
* vpitch
* 2;
731 * Image width and height is always based on current standard width and
734 pixfmt
->width
= common
->fmt
.fmt
.pix
.width
;
735 pixfmt
->height
= common
->fmt
.fmt
.pix
.height
;
742 * vpif_config_addr() - function to configure buffer address in vpif
744 * @muxmode - channel mux mode
746 static void vpif_config_addr(struct channel_obj
*ch
, int muxmode
)
748 struct common_obj
*common
;
750 vpif_dbg(2, debug
, "vpif_config_addr\n");
752 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
754 if (VPIF_CHANNEL1_VIDEO
== ch
->channel_id
)
755 common
->set_addr
= ch1_set_videobuf_addr
;
756 else if (2 == muxmode
)
757 common
->set_addr
= ch0_set_videobuf_addr_yc_nmux
;
759 common
->set_addr
= ch0_set_videobuf_addr
;
763 * vpif_input_to_subdev() - Maps input to sub device
764 * @vpif_cfg - global config ptr
765 * @chan_cfg - channel config ptr
766 * @input_index - Given input index from application
768 * lookup the sub device information for a given input index.
769 * we report all the inputs to application. inputs table also
770 * has sub device name for the each input
772 static int vpif_input_to_subdev(
773 struct vpif_capture_config
*vpif_cfg
,
774 struct vpif_capture_chan_config
*chan_cfg
,
777 struct vpif_subdev_info
*subdev_info
;
778 const char *subdev_name
;
781 vpif_dbg(2, debug
, "vpif_input_to_subdev\n");
783 subdev_name
= chan_cfg
->inputs
[input_index
].subdev_name
;
784 if (subdev_name
== NULL
)
787 /* loop through the sub device list to get the sub device info */
788 for (i
= 0; i
< vpif_cfg
->subdev_count
; i
++) {
789 subdev_info
= &vpif_cfg
->subdev_info
[i
];
790 if (!strcmp(subdev_info
->name
, subdev_name
))
797 * vpif_set_input() - Select an input
798 * @vpif_cfg - global config ptr
800 * @_index - Given input index from application
802 * Select the given input.
804 static int vpif_set_input(
805 struct vpif_capture_config
*vpif_cfg
,
806 struct channel_obj
*ch
,
809 struct vpif_capture_chan_config
*chan_cfg
=
810 &vpif_cfg
->chan_config
[ch
->channel_id
];
811 struct vpif_subdev_info
*subdev_info
= NULL
;
812 struct v4l2_subdev
*sd
= NULL
;
813 u32 input
= 0, output
= 0;
817 sd_index
= vpif_input_to_subdev(vpif_cfg
, chan_cfg
, index
);
819 sd
= vpif_obj
.sd
[sd_index
];
820 subdev_info
= &vpif_cfg
->subdev_info
[sd_index
];
823 /* first setup input path from sub device to vpif */
824 if (sd
&& vpif_cfg
->setup_input_path
) {
825 ret
= vpif_cfg
->setup_input_path(ch
->channel_id
,
828 vpif_dbg(1, debug
, "couldn't setup input path for the" \
829 " sub device %s, for input index %d\n",
830 subdev_info
->name
, index
);
836 input
= chan_cfg
->inputs
[index
].input_route
;
837 output
= chan_cfg
->inputs
[index
].output_route
;
838 ret
= v4l2_subdev_call(sd
, video
, s_routing
,
840 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
841 vpif_dbg(1, debug
, "Failed to set input\n");
845 ch
->input_idx
= index
;
847 /* copy interface parameters to vpif */
848 ch
->vpifparams
.iface
= chan_cfg
->vpif_if
;
850 /* update tvnorms from the sub device input info */
851 ch
->video_dev
->tvnorms
= chan_cfg
->inputs
[index
].input
.std
;
856 * vpif_querystd() - querystd handler
859 * @std_id: ptr to std id
861 * This function is called to detect standard at the selected input
863 static int vpif_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
865 struct video_device
*vdev
= video_devdata(file
);
866 struct channel_obj
*ch
= video_get_drvdata(vdev
);
869 vpif_dbg(2, debug
, "vpif_querystd\n");
871 /* Call querystd function of decoder device */
872 ret
= v4l2_subdev_call(ch
->sd
, video
, querystd
, std_id
);
874 if (ret
== -ENOIOCTLCMD
|| ret
== -ENODEV
)
877 vpif_dbg(1, debug
, "Failed to query standard for sub devices\n");
885 * vpif_g_std() - get STD handler
888 * @std_id: ptr to std id
890 static int vpif_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
892 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
893 struct video_device
*vdev
= video_devdata(file
);
894 struct channel_obj
*ch
= video_get_drvdata(vdev
);
895 struct vpif_capture_chan_config
*chan_cfg
;
896 struct v4l2_input input
;
898 vpif_dbg(2, debug
, "vpif_g_std\n");
900 if (config
->chan_config
[ch
->channel_id
].inputs
== NULL
)
903 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
904 input
= chan_cfg
->inputs
[ch
->input_idx
].input
;
905 if (input
.capabilities
!= V4L2_IN_CAP_STD
)
908 *std
= ch
->video
.stdid
;
913 * vpif_s_std() - set STD handler
916 * @std_id: ptr to std id
918 static int vpif_s_std(struct file
*file
, void *priv
, v4l2_std_id std_id
)
920 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
921 struct video_device
*vdev
= video_devdata(file
);
922 struct channel_obj
*ch
= video_get_drvdata(vdev
);
923 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
924 struct vpif_capture_chan_config
*chan_cfg
;
925 struct v4l2_input input
;
928 vpif_dbg(2, debug
, "vpif_s_std\n");
930 if (config
->chan_config
[ch
->channel_id
].inputs
== NULL
)
933 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
934 input
= chan_cfg
->inputs
[ch
->input_idx
].input
;
935 if (input
.capabilities
!= V4L2_IN_CAP_STD
)
938 if (vb2_is_busy(&common
->buffer_queue
))
941 /* Call encoder subdevice function to set the standard */
942 ch
->video
.stdid
= std_id
;
943 memset(&ch
->video
.dv_timings
, 0, sizeof(ch
->video
.dv_timings
));
945 /* Get the information about the standard */
946 if (vpif_update_std_info(ch
)) {
947 vpif_err("Error getting the standard info\n");
951 /* Configure the default format information */
952 vpif_config_format(ch
);
954 /* set standard in the sub device */
955 ret
= v4l2_subdev_call(ch
->sd
, video
, s_std
, std_id
);
956 if (ret
&& ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
) {
957 vpif_dbg(1, debug
, "Failed to set standard for sub devices\n");
964 * vpif_enum_input() - ENUMINPUT handler
967 * @input: ptr to input structure
969 static int vpif_enum_input(struct file
*file
, void *priv
,
970 struct v4l2_input
*input
)
973 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
974 struct video_device
*vdev
= video_devdata(file
);
975 struct channel_obj
*ch
= video_get_drvdata(vdev
);
976 struct vpif_capture_chan_config
*chan_cfg
;
978 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
980 if (input
->index
>= chan_cfg
->input_count
) {
981 vpif_dbg(1, debug
, "Invalid input index\n");
985 memcpy(input
, &chan_cfg
->inputs
[input
->index
].input
,
991 * vpif_g_input() - Get INPUT handler
994 * @index: ptr to input index
996 static int vpif_g_input(struct file
*file
, void *priv
, unsigned int *index
)
998 struct video_device
*vdev
= video_devdata(file
);
999 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1001 *index
= ch
->input_idx
;
1006 * vpif_s_input() - Set INPUT handler
1008 * @priv: file handle
1009 * @index: input index
1011 static int vpif_s_input(struct file
*file
, void *priv
, unsigned int index
)
1013 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1014 struct video_device
*vdev
= video_devdata(file
);
1015 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1016 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1017 struct vpif_capture_chan_config
*chan_cfg
;
1019 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1021 if (index
>= chan_cfg
->input_count
)
1024 if (vb2_is_busy(&common
->buffer_queue
))
1027 return vpif_set_input(config
, ch
, index
);
1031 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1033 * @priv: file handle
1034 * @index: input index
1036 static int vpif_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1037 struct v4l2_fmtdesc
*fmt
)
1039 struct video_device
*vdev
= video_devdata(file
);
1040 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1042 if (fmt
->index
!= 0) {
1043 vpif_dbg(1, debug
, "Invalid format index\n");
1047 /* Fill in the information about format */
1048 if (ch
->vpifparams
.iface
.if_type
== VPIF_IF_RAW_BAYER
) {
1049 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1050 strcpy(fmt
->description
, "Raw Mode -Bayer Pattern GrRBGb");
1051 fmt
->pixelformat
= V4L2_PIX_FMT_SBGGR8
;
1053 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1054 strcpy(fmt
->description
, "YCbCr4:2:2 YC Planar");
1055 fmt
->pixelformat
= V4L2_PIX_FMT_YUV422P
;
1061 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1063 * @priv: file handle
1064 * @fmt: ptr to v4l2 format structure
1066 static int vpif_try_fmt_vid_cap(struct file
*file
, void *priv
,
1067 struct v4l2_format
*fmt
)
1069 struct video_device
*vdev
= video_devdata(file
);
1070 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1071 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
1073 return vpif_check_format(ch
, pixfmt
, 1);
1078 * vpif_g_fmt_vid_cap() - Set INPUT handler
1080 * @priv: file handle
1081 * @fmt: ptr to v4l2 format structure
1083 static int vpif_g_fmt_vid_cap(struct file
*file
, void *priv
,
1084 struct v4l2_format
*fmt
)
1086 struct video_device
*vdev
= video_devdata(file
);
1087 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1088 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1090 /* Check the validity of the buffer type */
1091 if (common
->fmt
.type
!= fmt
->type
)
1094 /* Fill in the information about format */
1100 * vpif_s_fmt_vid_cap() - Set FMT handler
1102 * @priv: file handle
1103 * @fmt: ptr to v4l2 format structure
1105 static int vpif_s_fmt_vid_cap(struct file
*file
, void *priv
,
1106 struct v4l2_format
*fmt
)
1108 struct video_device
*vdev
= video_devdata(file
);
1109 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1110 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1111 struct v4l2_pix_format
*pixfmt
;
1114 vpif_dbg(2, debug
, "%s\n", __func__
);
1116 if (vb2_is_busy(&common
->buffer_queue
))
1119 pixfmt
= &fmt
->fmt
.pix
;
1120 /* Check for valid field format */
1121 ret
= vpif_check_format(ch
, pixfmt
, 0);
1125 /* store the format in the channel object */
1131 * vpif_querycap() - QUERYCAP handler
1133 * @priv: file handle
1134 * @cap: ptr to v4l2_capability structure
1136 static int vpif_querycap(struct file
*file
, void *priv
,
1137 struct v4l2_capability
*cap
)
1139 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1141 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1142 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
1143 strlcpy(cap
->driver
, VPIF_DRIVER_NAME
, sizeof(cap
->driver
));
1144 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
1145 dev_name(vpif_dev
));
1146 strlcpy(cap
->card
, config
->card_name
, sizeof(cap
->card
));
1152 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1154 * @priv: file handle
1155 * @timings: input timings
1158 vpif_enum_dv_timings(struct file
*file
, void *priv
,
1159 struct v4l2_enum_dv_timings
*timings
)
1161 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1162 struct video_device
*vdev
= video_devdata(file
);
1163 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1164 struct vpif_capture_chan_config
*chan_cfg
;
1165 struct v4l2_input input
;
1168 if (config
->chan_config
[ch
->channel_id
].inputs
== NULL
)
1171 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1172 input
= chan_cfg
->inputs
[ch
->input_idx
].input
;
1173 if (input
.capabilities
!= V4L2_IN_CAP_DV_TIMINGS
)
1178 ret
= v4l2_subdev_call(ch
->sd
, pad
, enum_dv_timings
, timings
);
1179 if (ret
== -ENOIOCTLCMD
|| ret
== -ENODEV
)
1186 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1188 * @priv: file handle
1189 * @timings: input timings
1192 vpif_query_dv_timings(struct file
*file
, void *priv
,
1193 struct v4l2_dv_timings
*timings
)
1195 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1196 struct video_device
*vdev
= video_devdata(file
);
1197 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1198 struct vpif_capture_chan_config
*chan_cfg
;
1199 struct v4l2_input input
;
1202 if (config
->chan_config
[ch
->channel_id
].inputs
== NULL
)
1205 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1206 input
= chan_cfg
->inputs
[ch
->input_idx
].input
;
1207 if (input
.capabilities
!= V4L2_IN_CAP_DV_TIMINGS
)
1210 ret
= v4l2_subdev_call(ch
->sd
, video
, query_dv_timings
, timings
);
1211 if (ret
== -ENOIOCTLCMD
|| ret
== -ENODEV
)
1218 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1220 * @priv: file handle
1221 * @timings: digital video timings
1223 static int vpif_s_dv_timings(struct file
*file
, void *priv
,
1224 struct v4l2_dv_timings
*timings
)
1226 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1227 struct video_device
*vdev
= video_devdata(file
);
1228 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1229 struct vpif_params
*vpifparams
= &ch
->vpifparams
;
1230 struct vpif_channel_config_params
*std_info
= &vpifparams
->std_info
;
1231 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1232 struct video_obj
*vid_ch
= &ch
->video
;
1233 struct v4l2_bt_timings
*bt
= &vid_ch
->dv_timings
.bt
;
1234 struct vpif_capture_chan_config
*chan_cfg
;
1235 struct v4l2_input input
;
1238 if (config
->chan_config
[ch
->channel_id
].inputs
== NULL
)
1241 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1242 input
= chan_cfg
->inputs
[ch
->input_idx
].input
;
1243 if (input
.capabilities
!= V4L2_IN_CAP_DV_TIMINGS
)
1246 if (timings
->type
!= V4L2_DV_BT_656_1120
) {
1247 vpif_dbg(2, debug
, "Timing type not defined\n");
1251 if (vb2_is_busy(&common
->buffer_queue
))
1254 /* Configure subdevice timings, if any */
1255 ret
= v4l2_subdev_call(ch
->sd
, video
, s_dv_timings
, timings
);
1256 if (ret
== -ENOIOCTLCMD
|| ret
== -ENODEV
)
1259 vpif_dbg(2, debug
, "Error setting custom DV timings\n");
1263 if (!(timings
->bt
.width
&& timings
->bt
.height
&&
1264 (timings
->bt
.hbackporch
||
1265 timings
->bt
.hfrontporch
||
1266 timings
->bt
.hsync
) &&
1267 timings
->bt
.vfrontporch
&&
1268 (timings
->bt
.vbackporch
||
1269 timings
->bt
.vsync
))) {
1270 vpif_dbg(2, debug
, "Timings for width, height, "
1271 "horizontal back porch, horizontal sync, "
1272 "horizontal front porch, vertical back porch, "
1273 "vertical sync and vertical back porch "
1274 "must be defined\n");
1278 vid_ch
->dv_timings
= *timings
;
1280 /* Configure video port timings */
1282 std_info
->eav2sav
= V4L2_DV_BT_BLANKING_WIDTH(bt
) - 8;
1283 std_info
->sav2eav
= bt
->width
;
1286 std_info
->l3
= bt
->vsync
+ bt
->vbackporch
+ 1;
1288 std_info
->vsize
= V4L2_DV_BT_FRAME_HEIGHT(bt
);
1289 if (bt
->interlaced
) {
1290 if (bt
->il_vbackporch
|| bt
->il_vfrontporch
|| bt
->il_vsync
) {
1291 std_info
->l5
= std_info
->vsize
/2 -
1292 (bt
->vfrontporch
- 1);
1293 std_info
->l7
= std_info
->vsize
/2 + 1;
1294 std_info
->l9
= std_info
->l7
+ bt
->il_vsync
+
1295 bt
->il_vbackporch
+ 1;
1296 std_info
->l11
= std_info
->vsize
-
1297 (bt
->il_vfrontporch
- 1);
1299 vpif_dbg(2, debug
, "Required timing values for "
1300 "interlaced BT format missing\n");
1304 std_info
->l5
= std_info
->vsize
- (bt
->vfrontporch
- 1);
1306 strncpy(std_info
->name
, "Custom timings BT656/1120", VPIF_MAX_NAME
);
1307 std_info
->width
= bt
->width
;
1308 std_info
->height
= bt
->height
;
1309 std_info
->frm_fmt
= bt
->interlaced
? 0 : 1;
1310 std_info
->ycmux_mode
= 0;
1311 std_info
->capture_format
= 0;
1312 std_info
->vbi_supported
= 0;
1313 std_info
->hd_sd
= 1;
1314 std_info
->stdid
= 0;
1321 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1323 * @priv: file handle
1324 * @timings: digital video timings
1326 static int vpif_g_dv_timings(struct file
*file
, void *priv
,
1327 struct v4l2_dv_timings
*timings
)
1329 struct vpif_capture_config
*config
= vpif_dev
->platform_data
;
1330 struct video_device
*vdev
= video_devdata(file
);
1331 struct channel_obj
*ch
= video_get_drvdata(vdev
);
1332 struct video_obj
*vid_ch
= &ch
->video
;
1333 struct vpif_capture_chan_config
*chan_cfg
;
1334 struct v4l2_input input
;
1336 if (config
->chan_config
[ch
->channel_id
].inputs
== NULL
)
1339 chan_cfg
= &config
->chan_config
[ch
->channel_id
];
1340 input
= chan_cfg
->inputs
[ch
->input_idx
].input
;
1341 if (input
.capabilities
!= V4L2_IN_CAP_DV_TIMINGS
)
1344 *timings
= vid_ch
->dv_timings
;
1350 * vpif_log_status() - Status information
1352 * @priv: file handle
1356 static int vpif_log_status(struct file
*filep
, void *priv
)
1358 /* status for sub devices */
1359 v4l2_device_call_all(&vpif_obj
.v4l2_dev
, 0, core
, log_status
);
1364 /* vpif capture ioctl operations */
1365 static const struct v4l2_ioctl_ops vpif_ioctl_ops
= {
1366 .vidioc_querycap
= vpif_querycap
,
1367 .vidioc_enum_fmt_vid_cap
= vpif_enum_fmt_vid_cap
,
1368 .vidioc_g_fmt_vid_cap
= vpif_g_fmt_vid_cap
,
1369 .vidioc_s_fmt_vid_cap
= vpif_s_fmt_vid_cap
,
1370 .vidioc_try_fmt_vid_cap
= vpif_try_fmt_vid_cap
,
1372 .vidioc_enum_input
= vpif_enum_input
,
1373 .vidioc_s_input
= vpif_s_input
,
1374 .vidioc_g_input
= vpif_g_input
,
1376 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1377 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1378 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1379 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1380 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1381 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1382 .vidioc_streamon
= vb2_ioctl_streamon
,
1383 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1385 .vidioc_querystd
= vpif_querystd
,
1386 .vidioc_s_std
= vpif_s_std
,
1387 .vidioc_g_std
= vpif_g_std
,
1389 .vidioc_enum_dv_timings
= vpif_enum_dv_timings
,
1390 .vidioc_query_dv_timings
= vpif_query_dv_timings
,
1391 .vidioc_s_dv_timings
= vpif_s_dv_timings
,
1392 .vidioc_g_dv_timings
= vpif_g_dv_timings
,
1394 .vidioc_log_status
= vpif_log_status
,
1397 /* vpif file operations */
1398 static struct v4l2_file_operations vpif_fops
= {
1399 .owner
= THIS_MODULE
,
1400 .open
= v4l2_fh_open
,
1401 .release
= vb2_fop_release
,
1402 .unlocked_ioctl
= video_ioctl2
,
1403 .mmap
= vb2_fop_mmap
,
1404 .poll
= vb2_fop_poll
1408 * initialize_vpif() - Initialize vpif data structures
1410 * Allocate memory for data structures and initialize them
1412 static int initialize_vpif(void)
1415 int free_channel_objects_index
;
1417 /* Default number of buffers should be 3 */
1418 if ((ch0_numbuffers
> 0) &&
1419 (ch0_numbuffers
< config_params
.min_numbuffers
))
1420 ch0_numbuffers
= config_params
.min_numbuffers
;
1421 if ((ch1_numbuffers
> 0) &&
1422 (ch1_numbuffers
< config_params
.min_numbuffers
))
1423 ch1_numbuffers
= config_params
.min_numbuffers
;
1425 /* Set buffer size to min buffers size if it is invalid */
1426 if (ch0_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
])
1428 config_params
.min_bufsize
[VPIF_CHANNEL0_VIDEO
];
1429 if (ch1_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
])
1431 config_params
.min_bufsize
[VPIF_CHANNEL1_VIDEO
];
1433 config_params
.numbuffers
[VPIF_CHANNEL0_VIDEO
] = ch0_numbuffers
;
1434 config_params
.numbuffers
[VPIF_CHANNEL1_VIDEO
] = ch1_numbuffers
;
1435 if (ch0_numbuffers
) {
1436 config_params
.channel_bufsize
[VPIF_CHANNEL0_VIDEO
]
1439 if (ch1_numbuffers
) {
1440 config_params
.channel_bufsize
[VPIF_CHANNEL1_VIDEO
]
1444 /* Allocate memory for six channel objects */
1445 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1447 kzalloc(sizeof(*vpif_obj
.dev
[i
]), GFP_KERNEL
);
1448 /* If memory allocation fails, return error */
1449 if (!vpif_obj
.dev
[i
]) {
1450 free_channel_objects_index
= i
;
1452 goto vpif_init_free_channel_objects
;
1457 vpif_init_free_channel_objects
:
1458 for (j
= 0; j
< free_channel_objects_index
; j
++)
1459 kfree(vpif_obj
.dev
[j
]);
1463 static int vpif_async_bound(struct v4l2_async_notifier
*notifier
,
1464 struct v4l2_subdev
*subdev
,
1465 struct v4l2_async_subdev
*asd
)
1469 for (i
= 0; i
< vpif_obj
.config
->subdev_count
; i
++)
1470 if (!strcmp(vpif_obj
.config
->subdev_info
[i
].name
,
1472 vpif_obj
.sd
[i
] = subdev
;
1479 static int vpif_probe_complete(void)
1481 struct common_obj
*common
;
1482 struct video_device
*vdev
;
1483 struct channel_obj
*ch
;
1484 struct vb2_queue
*q
;
1487 for (j
= 0; j
< VPIF_CAPTURE_MAX_DEVICES
; j
++) {
1488 ch
= vpif_obj
.dev
[j
];
1490 common
= &(ch
->common
[VPIF_VIDEO_INDEX
]);
1491 spin_lock_init(&common
->irqlock
);
1492 mutex_init(&common
->lock
);
1494 /* select input 0 */
1495 err
= vpif_set_input(vpif_obj
.config
, ch
, 0);
1499 /* Initialize vb2 queue */
1500 q
= &common
->buffer_queue
;
1501 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1502 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
1504 q
->ops
= &video_qops
;
1505 q
->mem_ops
= &vb2_dma_contig_memops
;
1506 q
->buf_struct_size
= sizeof(struct vpif_cap_buffer
);
1507 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1508 q
->min_buffers_needed
= 1;
1509 q
->lock
= &common
->lock
;
1511 err
= vb2_queue_init(q
);
1513 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1517 common
->alloc_ctx
= vb2_dma_contig_init_ctx(vpif_dev
);
1518 if (IS_ERR(common
->alloc_ctx
)) {
1519 vpif_err("Failed to get the context\n");
1520 err
= PTR_ERR(common
->alloc_ctx
);
1524 INIT_LIST_HEAD(&common
->dma_queue
);
1526 /* Initialize the video_device structure */
1527 vdev
= ch
->video_dev
;
1528 strlcpy(vdev
->name
, VPIF_DRIVER_NAME
, sizeof(vdev
->name
));
1529 vdev
->release
= video_device_release
;
1530 vdev
->fops
= &vpif_fops
;
1531 vdev
->ioctl_ops
= &vpif_ioctl_ops
;
1532 vdev
->v4l2_dev
= &vpif_obj
.v4l2_dev
;
1533 vdev
->vfl_dir
= VFL_DIR_RX
;
1535 vdev
->lock
= &common
->lock
;
1536 set_bit(V4L2_FL_USE_FH_PRIO
, &vdev
->flags
);
1537 video_set_drvdata(ch
->video_dev
, ch
);
1538 err
= video_register_device(vdev
,
1539 VFL_TYPE_GRABBER
, (j
? 1 : 0));
1544 v4l2_info(&vpif_obj
.v4l2_dev
, "VPIF capture driver initialized\n");
1548 for (k
= 0; k
< j
; k
++) {
1549 /* Get the pointer to the channel object */
1550 ch
= vpif_obj
.dev
[k
];
1551 common
= &ch
->common
[k
];
1552 vb2_dma_contig_cleanup_ctx(common
->alloc_ctx
);
1553 /* Unregister video device */
1554 video_unregister_device(ch
->video_dev
);
1557 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1558 ch
= vpif_obj
.dev
[i
];
1559 /* Note: does nothing if ch->video_dev == NULL */
1560 video_device_release(ch
->video_dev
);
1562 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
1567 static int vpif_async_complete(struct v4l2_async_notifier
*notifier
)
1569 return vpif_probe_complete();
1573 * vpif_probe : This function probes the vpif capture driver
1574 * @pdev: platform device pointer
1576 * This creates device entries by register itself to the V4L2 driver and
1577 * initializes fields of each channel objects
1579 static __init
int vpif_probe(struct platform_device
*pdev
)
1581 struct vpif_subdev_info
*subdevdata
;
1584 struct i2c_adapter
*i2c_adap
;
1585 struct channel_obj
*ch
;
1586 struct video_device
*vfd
;
1587 struct resource
*res
;
1590 vpif_dev
= &pdev
->dev
;
1592 err
= initialize_vpif();
1594 v4l2_err(vpif_dev
->driver
, "Error initializing vpif\n");
1598 err
= v4l2_device_register(vpif_dev
, &vpif_obj
.v4l2_dev
);
1600 v4l2_err(vpif_dev
->driver
, "Error registering v4l2 device\n");
1604 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, res_idx
))) {
1605 err
= devm_request_irq(&pdev
->dev
, res
->start
, vpif_channel_isr
,
1606 IRQF_SHARED
, VPIF_DRIVER_NAME
,
1607 (void *)(&vpif_obj
.dev
[res_idx
]->
1611 goto vpif_unregister
;
1616 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1617 /* Get the pointer to the channel object */
1618 ch
= vpif_obj
.dev
[i
];
1619 /* Allocate memory for video device */
1620 vfd
= video_device_alloc();
1622 for (j
= 0; j
< i
; j
++) {
1623 ch
= vpif_obj
.dev
[j
];
1624 video_device_release(ch
->video_dev
);
1627 goto vpif_unregister
;
1630 /* Set video_dev to the video device */
1631 ch
->video_dev
= vfd
;
1634 vpif_obj
.config
= pdev
->dev
.platform_data
;
1636 subdev_count
= vpif_obj
.config
->subdev_count
;
1637 vpif_obj
.sd
= kzalloc(sizeof(struct v4l2_subdev
*) * subdev_count
,
1639 if (vpif_obj
.sd
== NULL
) {
1640 vpif_err("unable to allocate memory for subdevice pointers\n");
1645 if (!vpif_obj
.config
->asd_sizes
) {
1646 i2c_adap
= i2c_get_adapter(1);
1647 for (i
= 0; i
< subdev_count
; i
++) {
1648 subdevdata
= &vpif_obj
.config
->subdev_info
[i
];
1650 v4l2_i2c_new_subdev_board(&vpif_obj
.v4l2_dev
,
1656 if (!vpif_obj
.sd
[i
]) {
1657 vpif_err("Error registering v4l2 subdevice\n");
1659 goto probe_subdev_out
;
1661 v4l2_info(&vpif_obj
.v4l2_dev
,
1662 "registered sub device %s\n",
1665 vpif_probe_complete();
1667 vpif_obj
.notifier
.subdevs
= vpif_obj
.config
->asd
;
1668 vpif_obj
.notifier
.num_subdevs
= vpif_obj
.config
->asd_sizes
[0];
1669 vpif_obj
.notifier
.bound
= vpif_async_bound
;
1670 vpif_obj
.notifier
.complete
= vpif_async_complete
;
1671 err
= v4l2_async_notifier_register(&vpif_obj
.v4l2_dev
,
1672 &vpif_obj
.notifier
);
1674 vpif_err("Error registering async notifier\n");
1676 goto probe_subdev_out
;
1683 /* free sub devices memory */
1687 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1688 ch
= vpif_obj
.dev
[i
];
1689 /* Note: does nothing if ch->video_dev == NULL */
1690 video_device_release(ch
->video_dev
);
1693 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
1699 * vpif_remove() - driver remove handler
1700 * @device: ptr to platform device structure
1702 * The vidoe device is unregistered
1704 static int vpif_remove(struct platform_device
*device
)
1706 struct common_obj
*common
;
1707 struct channel_obj
*ch
;
1710 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
1713 /* un-register device */
1714 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1715 /* Get the pointer to the channel object */
1716 ch
= vpif_obj
.dev
[i
];
1717 common
= &ch
->common
[i
];
1718 vb2_dma_contig_cleanup_ctx(common
->alloc_ctx
);
1719 /* Unregister video device */
1720 video_unregister_device(ch
->video_dev
);
1721 kfree(vpif_obj
.dev
[i
]);
1726 #ifdef CONFIG_PM_SLEEP
1728 * vpif_suspend: vpif device suspend
1730 static int vpif_suspend(struct device
*dev
)
1733 struct common_obj
*common
;
1734 struct channel_obj
*ch
;
1737 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1738 /* Get the pointer to the channel object */
1739 ch
= vpif_obj
.dev
[i
];
1740 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1742 if (!vb2_is_streaming(&common
->buffer_queue
))
1745 mutex_lock(&common
->lock
);
1746 /* Disable channel */
1747 if (ch
->channel_id
== VPIF_CHANNEL0_VIDEO
) {
1749 channel0_intr_enable(0);
1751 if (ch
->channel_id
== VPIF_CHANNEL1_VIDEO
||
1754 channel1_intr_enable(0);
1756 mutex_unlock(&common
->lock
);
1763 * vpif_resume: vpif device suspend
1765 static int vpif_resume(struct device
*dev
)
1767 struct common_obj
*common
;
1768 struct channel_obj
*ch
;
1771 for (i
= 0; i
< VPIF_CAPTURE_MAX_DEVICES
; i
++) {
1772 /* Get the pointer to the channel object */
1773 ch
= vpif_obj
.dev
[i
];
1774 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1776 if (!vb2_is_streaming(&common
->buffer_queue
))
1779 mutex_lock(&common
->lock
);
1780 /* Enable channel */
1781 if (ch
->channel_id
== VPIF_CHANNEL0_VIDEO
) {
1783 channel0_intr_enable(1);
1785 if (ch
->channel_id
== VPIF_CHANNEL1_VIDEO
||
1788 channel1_intr_enable(1);
1790 mutex_unlock(&common
->lock
);
1797 static SIMPLE_DEV_PM_OPS(vpif_pm_ops
, vpif_suspend
, vpif_resume
);
1799 static __refdata
struct platform_driver vpif_driver
= {
1801 .name
= VPIF_DRIVER_NAME
,
1802 .owner
= THIS_MODULE
,
1805 .probe
= vpif_probe
,
1806 .remove
= vpif_remove
,
1809 module_platform_driver(vpif_driver
);