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)
43 module_param(debug, int, 0644);
45 MODULE_PARM_DESC(debug, "Debug level 0-1");
47 #define VPIF_DRIVER_NAME "vpif_capture"
49 /* global variables */
50 static struct vpif_device vpif_obj = { {NULL} };
51 static struct device *vpif_dev;
52 static void vpif_calculate_offsets(struct channel_obj *ch);
53 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
55 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
57 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
58 static int ycmux_mode;
61 struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
63 return container_of(vb, struct vpif_cap_buffer, vb);
67 * vpif_buffer_prepare : callback function for buffer prepare
68 * @vb: ptr to vb2_buffer
70 * This is the callback function for buffer prepare when vb2_qbuf()
71 * function is called. The buffer is prepared and user space virtual address
72 * or user address is converted into physical address
74 static int vpif_buffer_prepare(struct vb2_buffer *vb)
76 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
77 struct vb2_queue *q = vb->vb2_queue;
78 struct channel_obj *ch = vb2_get_drv_priv(q);
79 struct common_obj *common;
82 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
84 common = &ch->common[VPIF_VIDEO_INDEX];
86 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
87 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
90 vbuf->field = common->fmt.fmt.pix.field;
92 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
93 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
94 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
95 !IS_ALIGNED((addr + common->ctop_off), 8) ||
96 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
97 vpif_dbg(1, debug, "offset is not aligned\n");
105 * vpif_buffer_queue_setup : Callback function for buffer setup.
108 * @nbuffers: ptr to number of buffers requested by application
109 * @nplanes:: contains number of distinct video planes needed to hold a frame
110 * @sizes[]: contains the size (in bytes) of each plane.
111 * @alloc_ctxs: ptr to allocation context
113 * This callback function is called when reqbuf() is called to adjust
114 * the buffer count and buffer size
116 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
118 unsigned int *nbuffers, unsigned int *nplanes,
119 unsigned int sizes[], void *alloc_ctxs[])
121 const struct v4l2_format *fmt = parg;
122 struct channel_obj *ch = vb2_get_drv_priv(vq);
123 struct common_obj *common;
125 common = &ch->common[VPIF_VIDEO_INDEX];
127 vpif_dbg(2, debug, "vpif_buffer_setup\n");
129 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
132 if (vq->num_buffers + *nbuffers < 3)
133 *nbuffers = 3 - vq->num_buffers;
136 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
137 alloc_ctxs[0] = common->alloc_ctx;
139 /* Calculate the offset for Y and C data in the buffer */
140 vpif_calculate_offsets(ch);
146 * vpif_buffer_queue : Callback function to add buffer to DMA queue
147 * @vb: ptr to vb2_buffer
149 static void vpif_buffer_queue(struct vb2_buffer *vb)
151 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
152 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
153 struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
154 struct common_obj *common;
157 common = &ch->common[VPIF_VIDEO_INDEX];
159 vpif_dbg(2, debug, "vpif_buffer_queue\n");
161 spin_lock_irqsave(&common->irqlock, flags);
162 /* add the buffer to the DMA queue */
163 list_add_tail(&buf->list, &common->dma_queue);
164 spin_unlock_irqrestore(&common->irqlock, flags);
168 * vpif_start_streaming : Starts the DMA engine for streaming
169 * @vb: ptr to vb2_buffer
170 * @count: number of buffers
172 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
174 struct vpif_capture_config *vpif_config_data =
175 vpif_dev->platform_data;
176 struct channel_obj *ch = vb2_get_drv_priv(vq);
177 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
178 struct vpif_params *vpif = &ch->vpifparams;
179 struct vpif_cap_buffer *buf, *tmp;
180 unsigned long addr, flags;
183 spin_lock_irqsave(&common->irqlock, flags);
185 /* Initialize field_id */
188 /* configure 1 or 2 channel mode */
189 if (vpif_config_data->setup_input_channel_mode) {
190 ret = vpif_config_data->
191 setup_input_channel_mode(vpif->std_info.ycmux_mode);
193 vpif_dbg(1, debug, "can't set vpif channel mode\n");
198 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
199 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
200 vpif_dbg(1, debug, "stream on failed in subdev\n");
204 /* Call vpif_set_params function to set the parameters and addresses */
205 ret = vpif_set_video_params(vpif, ch->channel_id);
207 vpif_dbg(1, debug, "can't set video params\n");
212 vpif_config_addr(ch, ret);
214 /* Get the next frame from the buffer queue */
215 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
216 struct vpif_cap_buffer, list);
217 /* Remove buffer from the buffer queue */
218 list_del(&common->cur_frm->list);
219 spin_unlock_irqrestore(&common->irqlock, flags);
221 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
223 common->set_addr(addr + common->ytop_off,
224 addr + common->ybtm_off,
225 addr + common->ctop_off,
226 addr + common->cbtm_off);
229 * Set interrupt for both the fields in VPIF Register enable channel in
232 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
233 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
234 channel0_intr_assert();
235 channel0_intr_enable(1);
238 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
240 channel1_intr_assert();
241 channel1_intr_enable(1);
248 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
249 list_del(&buf->list);
250 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
252 spin_unlock_irqrestore(&common->irqlock, flags);
258 * vpif_stop_streaming : Stop the DMA engine
259 * @vq: ptr to vb2_queue
261 * This callback stops the DMA engine and any remaining buffers
262 * in the DMA queue are released.
264 static void vpif_stop_streaming(struct vb2_queue *vq)
266 struct channel_obj *ch = vb2_get_drv_priv(vq);
267 struct common_obj *common;
271 common = &ch->common[VPIF_VIDEO_INDEX];
273 /* Disable channel as per its device type and channel id */
274 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
276 channel0_intr_enable(0);
278 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
281 channel1_intr_enable(0);
286 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
287 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
288 vpif_dbg(1, debug, "stream off failed in subdev\n");
290 /* release all active buffers */
291 spin_lock_irqsave(&common->irqlock, flags);
292 if (common->cur_frm == common->next_frm) {
293 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
294 VB2_BUF_STATE_ERROR);
296 if (common->cur_frm != NULL)
297 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
298 VB2_BUF_STATE_ERROR);
299 if (common->next_frm != NULL)
300 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
301 VB2_BUF_STATE_ERROR);
304 while (!list_empty(&common->dma_queue)) {
305 common->next_frm = list_entry(common->dma_queue.next,
306 struct vpif_cap_buffer, list);
307 list_del(&common->next_frm->list);
308 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
309 VB2_BUF_STATE_ERROR);
311 spin_unlock_irqrestore(&common->irqlock, flags);
314 static struct vb2_ops video_qops = {
315 .queue_setup = vpif_buffer_queue_setup,
316 .buf_prepare = vpif_buffer_prepare,
317 .start_streaming = vpif_start_streaming,
318 .stop_streaming = vpif_stop_streaming,
319 .buf_queue = vpif_buffer_queue,
320 .wait_prepare = vb2_ops_wait_prepare,
321 .wait_finish = vb2_ops_wait_finish,
325 * vpif_process_buffer_complete: process a completed buffer
326 * @common: ptr to common channel object
328 * This function time stamp the buffer and mark it as DONE. It also
329 * wake up any process waiting on the QUEUE and set the next buffer
332 static void vpif_process_buffer_complete(struct common_obj *common)
334 v4l2_get_timestamp(&common->cur_frm->vb.timestamp);
335 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
336 /* Make curFrm pointing to nextFrm */
337 common->cur_frm = common->next_frm;
341 * vpif_schedule_next_buffer: set next buffer address for capture
342 * @common : ptr to common channel object
344 * This function will get next buffer from the dma queue and
345 * set the buffer address in the vpif register for capture.
346 * the buffer is marked active
348 static void vpif_schedule_next_buffer(struct common_obj *common)
350 unsigned long addr = 0;
352 spin_lock(&common->irqlock);
353 common->next_frm = list_entry(common->dma_queue.next,
354 struct vpif_cap_buffer, list);
355 /* Remove that buffer from the buffer queue */
356 list_del(&common->next_frm->list);
357 spin_unlock(&common->irqlock);
358 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
360 /* Set top and bottom field addresses in VPIF registers */
361 common->set_addr(addr + common->ytop_off,
362 addr + common->ybtm_off,
363 addr + common->ctop_off,
364 addr + common->cbtm_off);
368 * vpif_channel_isr : ISR handler for vpif capture
370 * @dev_id: dev_id ptr
372 * It changes status of the captured buffer, takes next buffer from the queue
373 * and sets its address in VPIF registers
375 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
377 struct vpif_device *dev = &vpif_obj;
378 struct common_obj *common;
379 struct channel_obj *ch;
383 channel_id = *(int *)(dev_id);
384 if (!vpif_intr_status(channel_id))
387 ch = dev->dev[channel_id];
389 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
390 common = &ch->common[i];
391 /* skip If streaming is not started in this channel */
392 /* Check the field format */
393 if (1 == ch->vpifparams.std_info.frm_fmt) {
394 /* Progressive mode */
395 spin_lock(&common->irqlock);
396 if (list_empty(&common->dma_queue)) {
397 spin_unlock(&common->irqlock);
400 spin_unlock(&common->irqlock);
402 if (!channel_first_int[i][channel_id])
403 vpif_process_buffer_complete(common);
405 channel_first_int[i][channel_id] = 0;
407 vpif_schedule_next_buffer(common);
410 channel_first_int[i][channel_id] = 0;
413 * Interlaced mode. If it is first interrupt, ignore
416 if (channel_first_int[i][channel_id]) {
417 channel_first_int[i][channel_id] = 0;
422 /* Get field id from VPIF registers */
423 fid = vpif_channel_getfid(ch->channel_id);
424 if (fid != ch->field_id) {
426 * If field id does not match stored
427 * field id, make them in sync
434 /* device field id and local field id are in sync */
436 /* this is even field */
437 if (common->cur_frm == common->next_frm)
440 /* mark the current buffer as done */
441 vpif_process_buffer_complete(common);
442 } else if (1 == fid) {
444 spin_lock(&common->irqlock);
445 if (list_empty(&common->dma_queue) ||
446 (common->cur_frm != common->next_frm)) {
447 spin_unlock(&common->irqlock);
450 spin_unlock(&common->irqlock);
452 vpif_schedule_next_buffer(common);
460 * vpif_update_std_info() - update standard related info
461 * @ch: ptr to channel object
463 * For a given standard selected by application, update values
464 * in the device data structures
466 static int vpif_update_std_info(struct channel_obj *ch)
468 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
469 struct vpif_params *vpifparams = &ch->vpifparams;
470 const struct vpif_channel_config_params *config;
471 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
472 struct video_obj *vid_ch = &ch->video;
475 vpif_dbg(2, debug, "vpif_update_std_info\n");
477 for (index = 0; index < vpif_ch_params_count; index++) {
478 config = &vpif_ch_params[index];
479 if (config->hd_sd == 0) {
480 vpif_dbg(2, debug, "SD format\n");
481 if (config->stdid & vid_ch->stdid) {
482 memcpy(std_info, config, sizeof(*config));
486 vpif_dbg(2, debug, "HD format\n");
487 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
488 sizeof(vid_ch->dv_timings))) {
489 memcpy(std_info, config, sizeof(*config));
495 /* standard not found */
496 if (index == vpif_ch_params_count)
499 common->fmt.fmt.pix.width = std_info->width;
500 common->width = std_info->width;
501 common->fmt.fmt.pix.height = std_info->height;
502 common->height = std_info->height;
503 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
504 common->fmt.fmt.pix.bytesperline = std_info->width;
505 vpifparams->video_params.hpitch = std_info->width;
506 vpifparams->video_params.storage_mode = std_info->frm_fmt;
509 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
511 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
513 if (ch->vpifparams.std_info.frm_fmt)
514 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
516 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
518 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
519 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
521 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
523 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
529 * vpif_calculate_offsets : This function calculates buffers offsets
530 * @ch : ptr to channel object
532 * This function calculates buffer offsets for Y and C in the top and
535 static void vpif_calculate_offsets(struct channel_obj *ch)
537 unsigned int hpitch, sizeimage;
538 struct video_obj *vid_ch = &(ch->video);
539 struct vpif_params *vpifparams = &ch->vpifparams;
540 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
541 enum v4l2_field field = common->fmt.fmt.pix.field;
543 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
545 if (V4L2_FIELD_ANY == field) {
546 if (vpifparams->std_info.frm_fmt)
547 vid_ch->buf_field = V4L2_FIELD_NONE;
549 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
551 vid_ch->buf_field = common->fmt.fmt.pix.field;
553 sizeimage = common->fmt.fmt.pix.sizeimage;
555 hpitch = common->fmt.fmt.pix.bytesperline;
557 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
558 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
559 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
560 common->ytop_off = 0;
561 common->ybtm_off = hpitch;
562 common->ctop_off = sizeimage / 2;
563 common->cbtm_off = sizeimage / 2 + hpitch;
564 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
565 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
566 common->ytop_off = 0;
567 common->ybtm_off = sizeimage / 4;
568 common->ctop_off = sizeimage / 2;
569 common->cbtm_off = common->ctop_off + sizeimage / 4;
570 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
571 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
572 common->ybtm_off = 0;
573 common->ytop_off = sizeimage / 4;
574 common->cbtm_off = sizeimage / 2;
575 common->ctop_off = common->cbtm_off + sizeimage / 4;
577 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
578 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
579 vpifparams->video_params.storage_mode = 1;
581 vpifparams->video_params.storage_mode = 0;
583 if (1 == vpifparams->std_info.frm_fmt)
584 vpifparams->video_params.hpitch =
585 common->fmt.fmt.pix.bytesperline;
587 if ((field == V4L2_FIELD_ANY)
588 || (field == V4L2_FIELD_INTERLACED))
589 vpifparams->video_params.hpitch =
590 common->fmt.fmt.pix.bytesperline * 2;
592 vpifparams->video_params.hpitch =
593 common->fmt.fmt.pix.bytesperline;
596 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
600 * vpif_get_default_field() - Get default field type based on interface
601 * @vpif_params - ptr to vpif params
603 static inline enum v4l2_field vpif_get_default_field(
604 struct vpif_interface *iface)
606 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
607 V4L2_FIELD_INTERLACED;
611 * vpif_config_addr() - function to configure buffer address in vpif
613 * @muxmode - channel mux mode
615 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
617 struct common_obj *common;
619 vpif_dbg(2, debug, "vpif_config_addr\n");
621 common = &(ch->common[VPIF_VIDEO_INDEX]);
623 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
624 common->set_addr = ch1_set_videobuf_addr;
625 else if (2 == muxmode)
626 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
628 common->set_addr = ch0_set_videobuf_addr;
632 * vpif_input_to_subdev() - Maps input to sub device
633 * @vpif_cfg - global config ptr
634 * @chan_cfg - channel config ptr
635 * @input_index - Given input index from application
637 * lookup the sub device information for a given input index.
638 * we report all the inputs to application. inputs table also
639 * has sub device name for the each input
641 static int vpif_input_to_subdev(
642 struct vpif_capture_config *vpif_cfg,
643 struct vpif_capture_chan_config *chan_cfg,
646 struct vpif_subdev_info *subdev_info;
647 const char *subdev_name;
650 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
652 subdev_name = chan_cfg->inputs[input_index].subdev_name;
653 if (subdev_name == NULL)
656 /* loop through the sub device list to get the sub device info */
657 for (i = 0; i < vpif_cfg->subdev_count; i++) {
658 subdev_info = &vpif_cfg->subdev_info[i];
659 if (!strcmp(subdev_info->name, subdev_name))
666 * vpif_set_input() - Select an input
667 * @vpif_cfg - global config ptr
669 * @_index - Given input index from application
671 * Select the given input.
673 static int vpif_set_input(
674 struct vpif_capture_config *vpif_cfg,
675 struct channel_obj *ch,
678 struct vpif_capture_chan_config *chan_cfg =
679 &vpif_cfg->chan_config[ch->channel_id];
680 struct vpif_subdev_info *subdev_info = NULL;
681 struct v4l2_subdev *sd = NULL;
682 u32 input = 0, output = 0;
686 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
688 sd = vpif_obj.sd[sd_index];
689 subdev_info = &vpif_cfg->subdev_info[sd_index];
692 /* first setup input path from sub device to vpif */
693 if (sd && vpif_cfg->setup_input_path) {
694 ret = vpif_cfg->setup_input_path(ch->channel_id,
697 vpif_dbg(1, debug, "couldn't setup input path for the" \
698 " sub device %s, for input index %d\n",
699 subdev_info->name, index);
705 input = chan_cfg->inputs[index].input_route;
706 output = chan_cfg->inputs[index].output_route;
707 ret = v4l2_subdev_call(sd, video, s_routing,
709 if (ret < 0 && ret != -ENOIOCTLCMD) {
710 vpif_dbg(1, debug, "Failed to set input\n");
714 ch->input_idx = index;
716 /* copy interface parameters to vpif */
717 ch->vpifparams.iface = chan_cfg->vpif_if;
719 /* update tvnorms from the sub device input info */
720 ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
725 * vpif_querystd() - querystd handler
728 * @std_id: ptr to std id
730 * This function is called to detect standard at the selected input
732 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
734 struct video_device *vdev = video_devdata(file);
735 struct channel_obj *ch = video_get_drvdata(vdev);
738 vpif_dbg(2, debug, "vpif_querystd\n");
740 /* Call querystd function of decoder device */
741 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
743 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
746 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
754 * vpif_g_std() - get STD handler
757 * @std_id: ptr to std id
759 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
761 struct vpif_capture_config *config = vpif_dev->platform_data;
762 struct video_device *vdev = video_devdata(file);
763 struct channel_obj *ch = video_get_drvdata(vdev);
764 struct vpif_capture_chan_config *chan_cfg;
765 struct v4l2_input input;
767 vpif_dbg(2, debug, "vpif_g_std\n");
769 if (config->chan_config[ch->channel_id].inputs == NULL)
772 chan_cfg = &config->chan_config[ch->channel_id];
773 input = chan_cfg->inputs[ch->input_idx].input;
774 if (input.capabilities != V4L2_IN_CAP_STD)
777 *std = ch->video.stdid;
782 * vpif_s_std() - set STD handler
785 * @std_id: ptr to std id
787 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
789 struct vpif_capture_config *config = vpif_dev->platform_data;
790 struct video_device *vdev = video_devdata(file);
791 struct channel_obj *ch = video_get_drvdata(vdev);
792 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
793 struct vpif_capture_chan_config *chan_cfg;
794 struct v4l2_input input;
797 vpif_dbg(2, debug, "vpif_s_std\n");
799 if (config->chan_config[ch->channel_id].inputs == NULL)
802 chan_cfg = &config->chan_config[ch->channel_id];
803 input = chan_cfg->inputs[ch->input_idx].input;
804 if (input.capabilities != V4L2_IN_CAP_STD)
807 if (vb2_is_busy(&common->buffer_queue))
810 /* Call encoder subdevice function to set the standard */
811 ch->video.stdid = std_id;
812 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
814 /* Get the information about the standard */
815 if (vpif_update_std_info(ch)) {
816 vpif_err("Error getting the standard info\n");
820 /* set standard in the sub device */
821 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
822 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
823 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
830 * vpif_enum_input() - ENUMINPUT handler
833 * @input: ptr to input structure
835 static int vpif_enum_input(struct file *file, void *priv,
836 struct v4l2_input *input)
839 struct vpif_capture_config *config = vpif_dev->platform_data;
840 struct video_device *vdev = video_devdata(file);
841 struct channel_obj *ch = video_get_drvdata(vdev);
842 struct vpif_capture_chan_config *chan_cfg;
844 chan_cfg = &config->chan_config[ch->channel_id];
846 if (input->index >= chan_cfg->input_count)
849 memcpy(input, &chan_cfg->inputs[input->index].input,
855 * vpif_g_input() - Get INPUT handler
858 * @index: ptr to input index
860 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
862 struct video_device *vdev = video_devdata(file);
863 struct channel_obj *ch = video_get_drvdata(vdev);
865 *index = ch->input_idx;
870 * vpif_s_input() - Set INPUT handler
873 * @index: input index
875 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
877 struct vpif_capture_config *config = vpif_dev->platform_data;
878 struct video_device *vdev = video_devdata(file);
879 struct channel_obj *ch = video_get_drvdata(vdev);
880 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
881 struct vpif_capture_chan_config *chan_cfg;
883 chan_cfg = &config->chan_config[ch->channel_id];
885 if (index >= chan_cfg->input_count)
888 if (vb2_is_busy(&common->buffer_queue))
891 return vpif_set_input(config, ch, index);
895 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
898 * @index: input index
900 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
901 struct v4l2_fmtdesc *fmt)
903 struct video_device *vdev = video_devdata(file);
904 struct channel_obj *ch = video_get_drvdata(vdev);
906 if (fmt->index != 0) {
907 vpif_dbg(1, debug, "Invalid format index\n");
911 /* Fill in the information about format */
912 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
913 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
914 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
915 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
917 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
918 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
919 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
925 * vpif_try_fmt_vid_cap() - TRY_FMT handler
928 * @fmt: ptr to v4l2 format structure
930 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
931 struct v4l2_format *fmt)
933 struct video_device *vdev = video_devdata(file);
934 struct channel_obj *ch = video_get_drvdata(vdev);
935 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
936 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
937 struct vpif_params *vpif_params = &ch->vpifparams;
940 * to supress v4l-compliance warnings silently correct
943 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
944 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8)
945 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
947 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
948 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
951 common->fmt.fmt.pix.pixelformat = pixfmt->pixelformat;
953 vpif_update_std_info(ch);
955 pixfmt->field = common->fmt.fmt.pix.field;
956 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
957 pixfmt->bytesperline = common->fmt.fmt.pix.width;
958 pixfmt->width = common->fmt.fmt.pix.width;
959 pixfmt->height = common->fmt.fmt.pix.height;
960 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
968 * vpif_g_fmt_vid_cap() - Set INPUT handler
971 * @fmt: ptr to v4l2 format structure
973 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
974 struct v4l2_format *fmt)
976 struct video_device *vdev = video_devdata(file);
977 struct channel_obj *ch = video_get_drvdata(vdev);
978 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
980 /* Check the validity of the buffer type */
981 if (common->fmt.type != fmt->type)
984 /* Fill in the information about format */
990 * vpif_s_fmt_vid_cap() - Set FMT handler
993 * @fmt: ptr to v4l2 format structure
995 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
996 struct v4l2_format *fmt)
998 struct video_device *vdev = video_devdata(file);
999 struct channel_obj *ch = video_get_drvdata(vdev);
1000 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1003 vpif_dbg(2, debug, "%s\n", __func__);
1005 if (vb2_is_busy(&common->buffer_queue))
1008 ret = vpif_try_fmt_vid_cap(file, priv, fmt);
1012 /* store the format in the channel object */
1018 * vpif_querycap() - QUERYCAP handler
1020 * @priv: file handle
1021 * @cap: ptr to v4l2_capability structure
1023 static int vpif_querycap(struct file *file, void *priv,
1024 struct v4l2_capability *cap)
1026 struct vpif_capture_config *config = vpif_dev->platform_data;
1028 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1029 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1030 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
1031 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1032 dev_name(vpif_dev));
1033 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1039 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1041 * @priv: file handle
1042 * @timings: input timings
1045 vpif_enum_dv_timings(struct file *file, void *priv,
1046 struct v4l2_enum_dv_timings *timings)
1048 struct vpif_capture_config *config = vpif_dev->platform_data;
1049 struct video_device *vdev = video_devdata(file);
1050 struct channel_obj *ch = video_get_drvdata(vdev);
1051 struct vpif_capture_chan_config *chan_cfg;
1052 struct v4l2_input input;
1055 if (config->chan_config[ch->channel_id].inputs == NULL)
1058 chan_cfg = &config->chan_config[ch->channel_id];
1059 input = chan_cfg->inputs[ch->input_idx].input;
1060 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1065 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
1066 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1073 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1075 * @priv: file handle
1076 * @timings: input timings
1079 vpif_query_dv_timings(struct file *file, void *priv,
1080 struct v4l2_dv_timings *timings)
1082 struct vpif_capture_config *config = vpif_dev->platform_data;
1083 struct video_device *vdev = video_devdata(file);
1084 struct channel_obj *ch = video_get_drvdata(vdev);
1085 struct vpif_capture_chan_config *chan_cfg;
1086 struct v4l2_input input;
1089 if (config->chan_config[ch->channel_id].inputs == NULL)
1092 chan_cfg = &config->chan_config[ch->channel_id];
1093 input = chan_cfg->inputs[ch->input_idx].input;
1094 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1097 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1098 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1105 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1107 * @priv: file handle
1108 * @timings: digital video timings
1110 static int vpif_s_dv_timings(struct file *file, void *priv,
1111 struct v4l2_dv_timings *timings)
1113 struct vpif_capture_config *config = vpif_dev->platform_data;
1114 struct video_device *vdev = video_devdata(file);
1115 struct channel_obj *ch = video_get_drvdata(vdev);
1116 struct vpif_params *vpifparams = &ch->vpifparams;
1117 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1118 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1119 struct video_obj *vid_ch = &ch->video;
1120 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1121 struct vpif_capture_chan_config *chan_cfg;
1122 struct v4l2_input input;
1125 if (config->chan_config[ch->channel_id].inputs == NULL)
1128 chan_cfg = &config->chan_config[ch->channel_id];
1129 input = chan_cfg->inputs[ch->input_idx].input;
1130 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1133 if (timings->type != V4L2_DV_BT_656_1120) {
1134 vpif_dbg(2, debug, "Timing type not defined\n");
1138 if (vb2_is_busy(&common->buffer_queue))
1141 /* Configure subdevice timings, if any */
1142 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1143 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1146 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1150 if (!(timings->bt.width && timings->bt.height &&
1151 (timings->bt.hbackporch ||
1152 timings->bt.hfrontporch ||
1153 timings->bt.hsync) &&
1154 timings->bt.vfrontporch &&
1155 (timings->bt.vbackporch ||
1156 timings->bt.vsync))) {
1157 vpif_dbg(2, debug, "Timings for width, height, "
1158 "horizontal back porch, horizontal sync, "
1159 "horizontal front porch, vertical back porch, "
1160 "vertical sync and vertical back porch "
1161 "must be defined\n");
1165 vid_ch->dv_timings = *timings;
1167 /* Configure video port timings */
1169 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1170 std_info->sav2eav = bt->width;
1173 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1175 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1176 if (bt->interlaced) {
1177 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1178 std_info->l5 = std_info->vsize/2 -
1179 (bt->vfrontporch - 1);
1180 std_info->l7 = std_info->vsize/2 + 1;
1181 std_info->l9 = std_info->l7 + bt->il_vsync +
1182 bt->il_vbackporch + 1;
1183 std_info->l11 = std_info->vsize -
1184 (bt->il_vfrontporch - 1);
1186 vpif_dbg(2, debug, "Required timing values for "
1187 "interlaced BT format missing\n");
1191 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1193 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1194 std_info->width = bt->width;
1195 std_info->height = bt->height;
1196 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1197 std_info->ycmux_mode = 0;
1198 std_info->capture_format = 0;
1199 std_info->vbi_supported = 0;
1200 std_info->hd_sd = 1;
1201 std_info->stdid = 0;
1208 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1210 * @priv: file handle
1211 * @timings: digital video timings
1213 static int vpif_g_dv_timings(struct file *file, void *priv,
1214 struct v4l2_dv_timings *timings)
1216 struct vpif_capture_config *config = vpif_dev->platform_data;
1217 struct video_device *vdev = video_devdata(file);
1218 struct channel_obj *ch = video_get_drvdata(vdev);
1219 struct video_obj *vid_ch = &ch->video;
1220 struct vpif_capture_chan_config *chan_cfg;
1221 struct v4l2_input input;
1223 if (config->chan_config[ch->channel_id].inputs == NULL)
1226 chan_cfg = &config->chan_config[ch->channel_id];
1227 input = chan_cfg->inputs[ch->input_idx].input;
1228 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1231 *timings = vid_ch->dv_timings;
1237 * vpif_log_status() - Status information
1239 * @priv: file handle
1243 static int vpif_log_status(struct file *filep, void *priv)
1245 /* status for sub devices */
1246 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1251 /* vpif capture ioctl operations */
1252 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1253 .vidioc_querycap = vpif_querycap,
1254 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1255 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1256 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1257 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1259 .vidioc_enum_input = vpif_enum_input,
1260 .vidioc_s_input = vpif_s_input,
1261 .vidioc_g_input = vpif_g_input,
1263 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1264 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1265 .vidioc_querybuf = vb2_ioctl_querybuf,
1266 .vidioc_qbuf = vb2_ioctl_qbuf,
1267 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1268 .vidioc_expbuf = vb2_ioctl_expbuf,
1269 .vidioc_streamon = vb2_ioctl_streamon,
1270 .vidioc_streamoff = vb2_ioctl_streamoff,
1272 .vidioc_querystd = vpif_querystd,
1273 .vidioc_s_std = vpif_s_std,
1274 .vidioc_g_std = vpif_g_std,
1276 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1277 .vidioc_query_dv_timings = vpif_query_dv_timings,
1278 .vidioc_s_dv_timings = vpif_s_dv_timings,
1279 .vidioc_g_dv_timings = vpif_g_dv_timings,
1281 .vidioc_log_status = vpif_log_status,
1284 /* vpif file operations */
1285 static struct v4l2_file_operations vpif_fops = {
1286 .owner = THIS_MODULE,
1287 .open = v4l2_fh_open,
1288 .release = vb2_fop_release,
1289 .unlocked_ioctl = video_ioctl2,
1290 .mmap = vb2_fop_mmap,
1291 .poll = vb2_fop_poll
1295 * initialize_vpif() - Initialize vpif data structures
1297 * Allocate memory for data structures and initialize them
1299 static int initialize_vpif(void)
1302 int free_channel_objects_index;
1304 /* Allocate memory for six channel objects */
1305 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1307 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1308 /* If memory allocation fails, return error */
1309 if (!vpif_obj.dev[i]) {
1310 free_channel_objects_index = i;
1312 goto vpif_init_free_channel_objects;
1317 vpif_init_free_channel_objects:
1318 for (j = 0; j < free_channel_objects_index; j++)
1319 kfree(vpif_obj.dev[j]);
1323 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1324 struct v4l2_subdev *subdev,
1325 struct v4l2_async_subdev *asd)
1329 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1330 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1332 vpif_obj.sd[i] = subdev;
1339 static int vpif_probe_complete(void)
1341 struct common_obj *common;
1342 struct video_device *vdev;
1343 struct channel_obj *ch;
1344 struct vb2_queue *q;
1347 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1348 ch = vpif_obj.dev[j];
1350 common = &(ch->common[VPIF_VIDEO_INDEX]);
1351 spin_lock_init(&common->irqlock);
1352 mutex_init(&common->lock);
1354 /* select input 0 */
1355 err = vpif_set_input(vpif_obj.config, ch, 0);
1359 /* set initial format */
1360 ch->video.stdid = V4L2_STD_525_60;
1361 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1362 vpif_update_std_info(ch);
1364 /* Initialize vb2 queue */
1365 q = &common->buffer_queue;
1366 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1367 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1369 q->ops = &video_qops;
1370 q->mem_ops = &vb2_dma_contig_memops;
1371 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1372 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1373 q->min_buffers_needed = 1;
1374 q->lock = &common->lock;
1376 err = vb2_queue_init(q);
1378 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1382 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1383 if (IS_ERR(common->alloc_ctx)) {
1384 vpif_err("Failed to get the context\n");
1385 err = PTR_ERR(common->alloc_ctx);
1389 INIT_LIST_HEAD(&common->dma_queue);
1391 /* Initialize the video_device structure */
1392 vdev = &ch->video_dev;
1393 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1394 vdev->release = video_device_release_empty;
1395 vdev->fops = &vpif_fops;
1396 vdev->ioctl_ops = &vpif_ioctl_ops;
1397 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1398 vdev->vfl_dir = VFL_DIR_RX;
1400 vdev->lock = &common->lock;
1401 video_set_drvdata(&ch->video_dev, ch);
1402 err = video_register_device(vdev,
1403 VFL_TYPE_GRABBER, (j ? 1 : 0));
1408 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1412 for (k = 0; k < j; k++) {
1413 /* Get the pointer to the channel object */
1414 ch = vpif_obj.dev[k];
1415 common = &ch->common[k];
1416 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1417 /* Unregister video device */
1418 video_unregister_device(&ch->video_dev);
1421 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1426 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1428 return vpif_probe_complete();
1432 * vpif_probe : This function probes the vpif capture driver
1433 * @pdev: platform device pointer
1435 * This creates device entries by register itself to the V4L2 driver and
1436 * initializes fields of each channel objects
1438 static __init int vpif_probe(struct platform_device *pdev)
1440 struct vpif_subdev_info *subdevdata;
1441 struct i2c_adapter *i2c_adap;
1442 struct resource *res;
1447 vpif_dev = &pdev->dev;
1449 err = initialize_vpif();
1451 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1455 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1457 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1461 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1462 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1463 IRQF_SHARED, VPIF_DRIVER_NAME,
1464 (void *)(&vpif_obj.dev[res_idx]->
1468 goto vpif_unregister;
1473 vpif_obj.config = pdev->dev.platform_data;
1475 subdev_count = vpif_obj.config->subdev_count;
1476 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1478 if (vpif_obj.sd == NULL) {
1479 vpif_err("unable to allocate memory for subdevice pointers\n");
1481 goto vpif_unregister;
1484 if (!vpif_obj.config->asd_sizes) {
1485 i2c_adap = i2c_get_adapter(1);
1486 for (i = 0; i < subdev_count; i++) {
1487 subdevdata = &vpif_obj.config->subdev_info[i];
1489 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1495 if (!vpif_obj.sd[i]) {
1496 vpif_err("Error registering v4l2 subdevice\n");
1498 goto probe_subdev_out;
1500 v4l2_info(&vpif_obj.v4l2_dev,
1501 "registered sub device %s\n",
1504 vpif_probe_complete();
1506 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1507 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1508 vpif_obj.notifier.bound = vpif_async_bound;
1509 vpif_obj.notifier.complete = vpif_async_complete;
1510 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1511 &vpif_obj.notifier);
1513 vpif_err("Error registering async notifier\n");
1515 goto probe_subdev_out;
1522 /* free sub devices memory */
1525 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1531 * vpif_remove() - driver remove handler
1532 * @device: ptr to platform device structure
1534 * The vidoe device is unregistered
1536 static int vpif_remove(struct platform_device *device)
1538 struct common_obj *common;
1539 struct channel_obj *ch;
1542 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1545 /* un-register device */
1546 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1547 /* Get the pointer to the channel object */
1548 ch = vpif_obj.dev[i];
1549 common = &ch->common[VPIF_VIDEO_INDEX];
1550 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1551 /* Unregister video device */
1552 video_unregister_device(&ch->video_dev);
1553 kfree(vpif_obj.dev[i]);
1558 #ifdef CONFIG_PM_SLEEP
1560 * vpif_suspend: vpif device suspend
1562 static int vpif_suspend(struct device *dev)
1565 struct common_obj *common;
1566 struct channel_obj *ch;
1569 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1570 /* Get the pointer to the channel object */
1571 ch = vpif_obj.dev[i];
1572 common = &ch->common[VPIF_VIDEO_INDEX];
1574 if (!vb2_start_streaming_called(&common->buffer_queue))
1577 mutex_lock(&common->lock);
1578 /* Disable channel */
1579 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1581 channel0_intr_enable(0);
1583 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1586 channel1_intr_enable(0);
1588 mutex_unlock(&common->lock);
1595 * vpif_resume: vpif device suspend
1597 static int vpif_resume(struct device *dev)
1599 struct common_obj *common;
1600 struct channel_obj *ch;
1603 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1604 /* Get the pointer to the channel object */
1605 ch = vpif_obj.dev[i];
1606 common = &ch->common[VPIF_VIDEO_INDEX];
1608 if (!vb2_start_streaming_called(&common->buffer_queue))
1611 mutex_lock(&common->lock);
1612 /* Enable channel */
1613 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1615 channel0_intr_enable(1);
1617 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1620 channel1_intr_enable(1);
1622 mutex_unlock(&common->lock);
1629 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1631 static __refdata struct platform_driver vpif_driver = {
1633 .name = VPIF_DRIVER_NAME,
1636 .probe = vpif_probe,
1637 .remove = vpif_remove,
1640 module_platform_driver(vpif_driver);