2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
11 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/string.h>
26 #include <linux/videodev2.h>
27 #include <linux/wait.h>
28 #include <linux/time.h>
29 #include <linux/i2c.h>
30 #include <linux/platform_device.h>
32 #include <linux/slab.h>
37 #include <media/adv7343.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-chip-ident.h>
42 #include "vpif_display.h"
45 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(VPIF_DISPLAY_VERSION);
49 #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
51 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
52 #define vpif_dbg(level, debug, fmt, arg...) \
53 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
56 static u32 ch2_numbuffers = 3;
57 static u32 ch3_numbuffers = 3;
58 static u32 ch2_bufsize = 1920 * 1080 * 2;
59 static u32 ch3_bufsize = 720 * 576 * 2;
61 module_param(debug, int, 0644);
62 module_param(ch2_numbuffers, uint, S_IRUGO);
63 module_param(ch3_numbuffers, uint, S_IRUGO);
64 module_param(ch2_bufsize, uint, S_IRUGO);
65 module_param(ch3_bufsize, uint, S_IRUGO);
67 MODULE_PARM_DESC(debug, "Debug level 0-1");
68 MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
69 MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
70 MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
71 MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
73 static struct vpif_config_params config_params = {
77 .min_bufsize[0] = 720 * 480 * 2,
78 .min_bufsize[1] = 720 * 480 * 2,
79 .channel_bufsize[0] = 1920 * 1080 * 2,
80 .channel_bufsize[1] = 720 * 576 * 2,
83 static struct vpif_device vpif_obj = { {NULL} };
84 static struct device *vpif_dev;
85 static void vpif_calculate_offsets(struct channel_obj *ch);
86 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
89 * buffer_prepare: This is the callback function called from vb2_qbuf()
90 * function the buffer is prepared and user space virtual address is converted
91 * into physical address
93 static int vpif_buffer_prepare(struct vb2_buffer *vb)
95 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
96 struct vb2_queue *q = vb->vb2_queue;
97 struct common_obj *common;
100 common = &fh->channel->common[VPIF_VIDEO_INDEX];
101 if (vb->state != VB2_BUF_STATE_ACTIVE &&
102 vb->state != VB2_BUF_STATE_PREPARED) {
103 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
104 if (vb2_plane_vaddr(vb, 0) &&
105 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
108 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
110 (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
111 if (!ISALIGNED(addr + common->ytop_off) ||
112 !ISALIGNED(addr + common->ybtm_off) ||
113 !ISALIGNED(addr + common->ctop_off) ||
114 !ISALIGNED(addr + common->cbtm_off))
121 vpif_err("buffer offset not aligned to 8 bytes\n");
126 * vpif_buffer_queue_setup: This function allocates memory for the buffers
128 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
129 const struct v4l2_format *fmt,
130 unsigned int *nbuffers, unsigned int *nplanes,
131 unsigned int sizes[], void *alloc_ctxs[])
133 struct vpif_fh *fh = vb2_get_drv_priv(vq);
134 struct channel_obj *ch = fh->channel;
135 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
138 if (V4L2_MEMORY_MMAP == common->memory) {
139 size = config_params.channel_bufsize[ch->channel_id];
141 * Checking if the buffer size exceeds the available buffer
142 * ycmux_mode = 0 means 1 channel mode HD and
143 * ycmux_mode = 1 means 2 channels mode SD
145 if (ch->vpifparams.std_info.ycmux_mode == 0) {
146 if (config_params.video_limit[ch->channel_id])
147 while (size * *nbuffers >
148 (config_params.video_limit[0]
149 + config_params.video_limit[1]))
152 if (config_params.video_limit[ch->channel_id])
153 while (size * *nbuffers >
154 config_params.video_limit[ch->channel_id])
158 size = common->fmt.fmt.pix.sizeimage;
161 if (*nbuffers < config_params.min_numbuffers)
162 *nbuffers = config_params.min_numbuffers;
166 alloc_ctxs[0] = common->alloc_ctx;
171 * vpif_buffer_queue: This function adds the buffer to DMA queue
173 static void vpif_buffer_queue(struct vb2_buffer *vb)
175 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
176 struct vpif_disp_buffer *buf = container_of(vb,
177 struct vpif_disp_buffer, vb);
178 struct channel_obj *ch = fh->channel;
179 struct common_obj *common;
181 common = &ch->common[VPIF_VIDEO_INDEX];
183 /* add the buffer to the DMA queue */
184 list_add_tail(&buf->list, &common->dma_queue);
188 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
189 * free memory allocated to the buffers
191 static void vpif_buf_cleanup(struct vb2_buffer *vb)
193 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
194 struct vpif_disp_buffer *buf = container_of(vb,
195 struct vpif_disp_buffer, vb);
196 struct channel_obj *ch = fh->channel;
197 struct common_obj *common;
200 common = &ch->common[VPIF_VIDEO_INDEX];
202 spin_lock_irqsave(&common->irqlock, flags);
203 if (vb->state == VB2_BUF_STATE_ACTIVE)
204 list_del_init(&buf->list);
205 spin_unlock_irqrestore(&common->irqlock, flags);
208 static void vpif_wait_prepare(struct vb2_queue *vq)
210 struct vpif_fh *fh = vb2_get_drv_priv(vq);
211 struct channel_obj *ch = fh->channel;
212 struct common_obj *common;
214 common = &ch->common[VPIF_VIDEO_INDEX];
215 mutex_unlock(&common->lock);
218 static void vpif_wait_finish(struct vb2_queue *vq)
220 struct vpif_fh *fh = vb2_get_drv_priv(vq);
221 struct channel_obj *ch = fh->channel;
222 struct common_obj *common;
224 common = &ch->common[VPIF_VIDEO_INDEX];
225 mutex_lock(&common->lock);
228 static int vpif_buffer_init(struct vb2_buffer *vb)
230 struct vpif_disp_buffer *buf = container_of(vb,
231 struct vpif_disp_buffer, vb);
233 INIT_LIST_HEAD(&buf->list);
238 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
240 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
242 struct vpif_display_config *vpif_config_data =
243 vpif_dev->platform_data;
244 struct vpif_fh *fh = vb2_get_drv_priv(vq);
245 struct channel_obj *ch = fh->channel;
246 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
247 struct vpif_params *vpif = &ch->vpifparams;
248 unsigned long addr = 0;
251 /* If buffer queue is empty, return error */
252 if (list_empty(&common->dma_queue)) {
253 vpif_err("buffer queue is empty\n");
257 /* Get the next frame from the buffer queue */
258 common->next_frm = common->cur_frm =
259 list_entry(common->dma_queue.next,
260 struct vpif_disp_buffer, list);
262 list_del(&common->cur_frm->list);
263 /* Mark state of the current frame to active */
264 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
266 /* Initialize field_id and started member */
269 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
270 /* Calculate the offset for Y and C data in the buffer */
271 vpif_calculate_offsets(ch);
273 if ((ch->vpifparams.std_info.frm_fmt &&
274 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
275 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
276 || (!ch->vpifparams.std_info.frm_fmt
277 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
278 vpif_err("conflict in field format and std format\n");
284 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
285 ch->vpifparams.std_info.hd_sd);
287 vpif_err("can't set clock\n");
291 /* set the parameters and addresses */
292 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
296 common->started = ret;
297 vpif_config_addr(ch, ret);
298 common->set_addr((addr + common->ytop_off),
299 (addr + common->ybtm_off),
300 (addr + common->ctop_off),
301 (addr + common->cbtm_off));
303 /* Set interrupt for both the fields in VPIF
304 Register enable channel in VPIF register */
305 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
306 channel2_intr_assert();
307 channel2_intr_enable(1);
311 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
312 || (common->started == 2)) {
313 channel3_intr_assert();
314 channel3_intr_enable(1);
317 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
322 /* abort streaming and wait for last buffer */
323 static int vpif_stop_streaming(struct vb2_queue *vq)
325 struct vpif_fh *fh = vb2_get_drv_priv(vq);
326 struct channel_obj *ch = fh->channel;
327 struct common_obj *common;
329 if (!vb2_is_streaming(vq))
332 common = &ch->common[VPIF_VIDEO_INDEX];
334 /* release all active buffers */
335 while (!list_empty(&common->dma_queue)) {
336 common->next_frm = list_entry(common->dma_queue.next,
337 struct vpif_disp_buffer, list);
338 list_del(&common->next_frm->list);
339 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
345 static struct vb2_ops video_qops = {
346 .queue_setup = vpif_buffer_queue_setup,
347 .wait_prepare = vpif_wait_prepare,
348 .wait_finish = vpif_wait_finish,
349 .buf_init = vpif_buffer_init,
350 .buf_prepare = vpif_buffer_prepare,
351 .start_streaming = vpif_start_streaming,
352 .stop_streaming = vpif_stop_streaming,
353 .buf_cleanup = vpif_buf_cleanup,
354 .buf_queue = vpif_buffer_queue,
357 static void process_progressive_mode(struct common_obj *common)
359 unsigned long addr = 0;
361 /* Get the next buffer from buffer queue */
362 common->next_frm = list_entry(common->dma_queue.next,
363 struct vpif_disp_buffer, list);
364 /* Remove that buffer from the buffer queue */
365 list_del(&common->next_frm->list);
366 /* Mark status of the buffer as active */
367 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
369 /* Set top and bottom field addrs in VPIF registers */
370 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
371 common->set_addr(addr + common->ytop_off,
372 addr + common->ybtm_off,
373 addr + common->ctop_off,
374 addr + common->cbtm_off);
377 static void process_interlaced_mode(int fid, struct common_obj *common)
379 /* device field id and local field id are in sync */
380 /* If this is even field */
382 if (common->cur_frm == common->next_frm)
385 /* one frame is displayed If next frame is
386 * available, release cur_frm and move on */
387 /* Copy frame display time */
388 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
389 /* Change status of the cur_frm */
390 vb2_buffer_done(&common->cur_frm->vb,
392 /* Make cur_frm pointing to next_frm */
393 common->cur_frm = common->next_frm;
395 } else if (1 == fid) { /* odd field */
396 if (list_empty(&common->dma_queue)
397 || (common->cur_frm != common->next_frm)) {
400 /* one field is displayed configure the next
401 * frame if it is available else hold on current
403 /* Get next from the buffer queue */
404 process_progressive_mode(common);
410 * vpif_channel_isr: It changes status of the displayed buffer, takes next
411 * buffer from the queue and sets its address in VPIF registers
413 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
415 struct vpif_device *dev = &vpif_obj;
416 struct channel_obj *ch;
417 struct common_obj *common;
418 enum v4l2_field field;
422 channel_id = *(int *)(dev_id);
423 if (!vpif_intr_status(channel_id + 2))
426 ch = dev->dev[channel_id];
427 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
428 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
429 common = &ch->common[i];
430 /* If streaming is started in this channel */
431 if (0 == common->started)
434 if (1 == ch->vpifparams.std_info.frm_fmt) {
435 if (list_empty(&common->dma_queue))
438 /* Progressive mode */
439 if (!channel_first_int[i][channel_id]) {
440 /* Mark status of the cur_frm to
441 * done and unlock semaphore on it */
442 do_gettimeofday(&common->cur_frm->vb.
444 vb2_buffer_done(&common->cur_frm->vb,
446 /* Make cur_frm pointing to next_frm */
447 common->cur_frm = common->next_frm;
450 channel_first_int[i][channel_id] = 0;
451 process_progressive_mode(common);
453 /* Interlaced mode */
454 /* If it is first interrupt, ignore it */
456 if (channel_first_int[i][channel_id]) {
457 channel_first_int[i][channel_id] = 0;
463 /* Get field id from VPIF registers */
464 fid = vpif_channel_getfid(ch->channel_id + 2);
465 /* If fid does not match with stored field id */
466 if (fid != ch->field_id) {
467 /* Make them in sync */
474 process_interlaced_mode(fid, common);
481 static int vpif_update_std_info(struct channel_obj *ch)
483 struct video_obj *vid_ch = &ch->video;
484 struct vpif_params *vpifparams = &ch->vpifparams;
485 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
486 const struct vpif_channel_config_params *config;
490 for (i = 0; i < vpif_ch_params_count; i++) {
491 config = &ch_params[i];
492 if (config->hd_sd == 0) {
493 vpif_dbg(2, debug, "SD format\n");
494 if (config->stdid & vid_ch->stdid) {
495 memcpy(std_info, config, sizeof(*config));
499 vpif_dbg(2, debug, "HD format\n");
500 if (config->dv_preset == vid_ch->dv_preset) {
501 memcpy(std_info, config, sizeof(*config));
507 if (i == vpif_ch_params_count) {
508 vpif_dbg(1, debug, "Format not found\n");
515 static int vpif_update_resolution(struct channel_obj *ch)
517 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
518 struct video_obj *vid_ch = &ch->video;
519 struct vpif_params *vpifparams = &ch->vpifparams;
520 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
522 if (!vid_ch->stdid && !vid_ch->dv_preset && !vid_ch->bt_timings.height)
525 if (vid_ch->stdid || vid_ch->dv_preset) {
526 if (vpif_update_std_info(ch))
530 common->fmt.fmt.pix.width = std_info->width;
531 common->fmt.fmt.pix.height = std_info->height;
532 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
533 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
535 /* Set height and width paramateres */
536 common->height = std_info->height;
537 common->width = std_info->width;
543 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
544 * in the top and bottom field
546 static void vpif_calculate_offsets(struct channel_obj *ch)
548 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
549 struct vpif_params *vpifparams = &ch->vpifparams;
550 enum v4l2_field field = common->fmt.fmt.pix.field;
551 struct video_obj *vid_ch = &ch->video;
552 unsigned int hpitch, vpitch, sizeimage;
554 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
555 if (ch->vpifparams.std_info.frm_fmt)
556 vid_ch->buf_field = V4L2_FIELD_NONE;
558 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
560 vid_ch->buf_field = common->fmt.fmt.pix.field;
563 sizeimage = common->fmt.fmt.pix.sizeimage;
565 hpitch = common->fmt.fmt.pix.bytesperline;
566 vpitch = sizeimage / (hpitch * 2);
567 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
568 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
569 common->ytop_off = 0;
570 common->ybtm_off = hpitch;
571 common->ctop_off = sizeimage / 2;
572 common->cbtm_off = sizeimage / 2 + hpitch;
573 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
574 common->ytop_off = 0;
575 common->ybtm_off = sizeimage / 4;
576 common->ctop_off = sizeimage / 2;
577 common->cbtm_off = common->ctop_off + sizeimage / 4;
578 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
579 common->ybtm_off = 0;
580 common->ytop_off = sizeimage / 4;
581 common->cbtm_off = sizeimage / 2;
582 common->ctop_off = common->cbtm_off + sizeimage / 4;
585 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
586 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
587 vpifparams->video_params.storage_mode = 1;
589 vpifparams->video_params.storage_mode = 0;
592 if (ch->vpifparams.std_info.frm_fmt == 1) {
593 vpifparams->video_params.hpitch =
594 common->fmt.fmt.pix.bytesperline;
596 if ((field == V4L2_FIELD_ANY) ||
597 (field == V4L2_FIELD_INTERLACED))
598 vpifparams->video_params.hpitch =
599 common->fmt.fmt.pix.bytesperline * 2;
601 vpifparams->video_params.hpitch =
602 common->fmt.fmt.pix.bytesperline;
605 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
608 static void vpif_config_format(struct channel_obj *ch)
610 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
612 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
613 if (config_params.numbuffers[ch->channel_id] == 0)
614 common->memory = V4L2_MEMORY_USERPTR;
616 common->memory = V4L2_MEMORY_MMAP;
618 common->fmt.fmt.pix.sizeimage =
619 config_params.channel_bufsize[ch->channel_id];
620 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
621 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
624 static int vpif_check_format(struct channel_obj *ch,
625 struct v4l2_pix_format *pixfmt)
627 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
628 enum v4l2_field field = pixfmt->field;
629 u32 sizeimage, hpitch, vpitch;
631 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
632 goto invalid_fmt_exit;
634 if (!(VPIF_VALID_FIELD(field)))
635 goto invalid_fmt_exit;
637 if (pixfmt->bytesperline <= 0)
638 goto invalid_pitch_exit;
640 sizeimage = pixfmt->sizeimage;
642 if (vpif_update_resolution(ch))
645 hpitch = pixfmt->bytesperline;
646 vpitch = sizeimage / (hpitch * 2);
648 /* Check for valid value of pitch */
649 if ((hpitch < ch->vpifparams.std_info.width) ||
650 (vpitch < ch->vpifparams.std_info.height))
651 goto invalid_pitch_exit;
653 /* Check for 8 byte alignment */
654 if (!ISALIGNED(hpitch)) {
655 vpif_err("invalid pitch alignment\n");
658 pixfmt->width = common->fmt.fmt.pix.width;
659 pixfmt->height = common->fmt.fmt.pix.height;
664 vpif_err("invalid field format\n");
668 vpif_err("invalid pitch\n");
672 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
674 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
676 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
677 common->set_addr = ch3_set_videobuf_addr;
680 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
682 common->set_addr = ch2_set_videobuf_addr;
687 * vpif_mmap: It is used to map kernel space buffers into user spaces
689 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
691 struct vpif_fh *fh = filep->private_data;
692 struct channel_obj *ch = fh->channel;
693 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
695 vpif_dbg(2, debug, "vpif_mmap\n");
697 return vb2_mmap(&common->buffer_queue, vma);
701 * vpif_poll: It is used for select/poll system call
703 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
705 struct vpif_fh *fh = filep->private_data;
706 struct channel_obj *ch = fh->channel;
707 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
710 return vb2_poll(&common->buffer_queue, filep, wait);
716 * vpif_open: It creates object of file handle structure and stores it in
717 * private_data member of filepointer
719 static int vpif_open(struct file *filep)
721 struct video_device *vdev = video_devdata(filep);
722 struct channel_obj *ch = NULL;
723 struct vpif_fh *fh = NULL;
725 ch = video_get_drvdata(vdev);
726 /* Allocate memory for the file handle object */
727 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
729 vpif_err("unable to allocate memory for file handle object\n");
733 /* store pointer to fh in private_data member of filep */
734 filep->private_data = fh;
737 if (!ch->initialized) {
740 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
743 /* Increment channel usrs counter */
744 atomic_inc(&ch->usrs);
745 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
746 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
747 /* Initialize priority of this instance to default priority */
748 fh->prio = V4L2_PRIORITY_UNSET;
749 v4l2_prio_open(&ch->prio, &fh->prio);
755 * vpif_release: This function deletes buffer queue, frees the buffers and
756 * the vpif file handle
758 static int vpif_release(struct file *filep)
760 struct vpif_fh *fh = filep->private_data;
761 struct channel_obj *ch = fh->channel;
762 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
764 /* if this instance is doing IO */
765 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
766 /* Reset io_usrs member of channel object */
768 /* Disable channel */
769 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
771 channel2_intr_enable(0);
773 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
774 (2 == common->started)) {
776 channel3_intr_enable(0);
780 /* Free buffers allocated */
781 vb2_queue_release(&common->buffer_queue);
782 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
785 config_params.numbuffers[ch->channel_id];
788 /* Decrement channel usrs counter */
789 atomic_dec(&ch->usrs);
790 /* If this file handle has initialize encoder device, reset it */
794 /* Close the priority */
795 v4l2_prio_close(&ch->prio, fh->prio);
796 filep->private_data = NULL;
803 /* functions implementing ioctls */
805 * vpif_querycap() - QUERYCAP handler
808 * @cap: ptr to v4l2_capability structure
810 static int vpif_querycap(struct file *file, void *priv,
811 struct v4l2_capability *cap)
813 struct vpif_display_config *config = vpif_dev->platform_data;
815 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
816 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
817 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
818 strlcpy(cap->card, config->card_name, sizeof(cap->card));
823 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
824 struct v4l2_fmtdesc *fmt)
826 if (fmt->index != 0) {
827 vpif_err("Invalid format index\n");
831 /* Fill in the information about format */
832 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
833 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
834 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
839 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
840 struct v4l2_format *fmt)
842 struct vpif_fh *fh = priv;
843 struct channel_obj *ch = fh->channel;
844 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
846 /* Check the validity of the buffer type */
847 if (common->fmt.type != fmt->type)
850 if (vpif_update_resolution(ch))
856 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
857 struct v4l2_format *fmt)
859 struct vpif_fh *fh = priv;
860 struct v4l2_pix_format *pixfmt;
861 struct channel_obj *ch = fh->channel;
862 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
865 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
866 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
867 if (!fh->initialized) {
868 vpif_dbg(1, debug, "Channel Busy\n");
872 /* Check for the priority */
873 ret = v4l2_prio_check(&ch->prio, fh->prio);
879 if (common->started) {
880 vpif_dbg(1, debug, "Streaming in progress\n");
884 pixfmt = &fmt->fmt.pix;
885 /* Check for valid field format */
886 ret = vpif_check_format(ch, pixfmt);
890 /* store the pix format in the channel object */
891 common->fmt.fmt.pix = *pixfmt;
892 /* store the format in the channel object */
897 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
898 struct v4l2_format *fmt)
900 struct vpif_fh *fh = priv;
901 struct channel_obj *ch = fh->channel;
902 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
903 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
906 ret = vpif_check_format(ch, pixfmt);
908 *pixfmt = common->fmt.fmt.pix;
909 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
915 static int vpif_reqbufs(struct file *file, void *priv,
916 struct v4l2_requestbuffers *reqbuf)
918 struct vpif_fh *fh = priv;
919 struct channel_obj *ch = fh->channel;
920 struct common_obj *common;
921 enum v4l2_field field;
925 /* This file handle has not initialized the channel,
926 It is not allowed to do settings */
927 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
928 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
929 if (!fh->initialized) {
930 vpif_err("Channel Busy\n");
935 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
938 index = VPIF_VIDEO_INDEX;
940 common = &ch->common[index];
942 if (common->fmt.type != reqbuf->type || !vpif_dev)
944 if (0 != common->io_usrs)
947 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
948 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
949 field = V4L2_FIELD_INTERLACED;
951 field = common->fmt.fmt.pix.field;
953 field = V4L2_VBI_INTERLACED;
955 /* Initialize videobuf2 queue as per the buffer type */
956 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
957 if (!common->alloc_ctx) {
958 vpif_err("Failed to get the context\n");
961 q = &common->buffer_queue;
962 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
963 q->io_modes = VB2_MMAP | VB2_USERPTR;
965 q->ops = &video_qops;
966 q->mem_ops = &vb2_dma_contig_memops;
967 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
971 /* Set io allowed member of file handle to TRUE */
972 fh->io_allowed[index] = 1;
973 /* Increment io usrs member of channel object to 1 */
975 /* Store type of memory requested in channel object */
976 common->memory = reqbuf->memory;
977 INIT_LIST_HEAD(&common->dma_queue);
978 /* Allocate buffers */
979 return vb2_reqbufs(&common->buffer_queue, reqbuf);
982 static int vpif_querybuf(struct file *file, void *priv,
983 struct v4l2_buffer *tbuf)
985 struct vpif_fh *fh = priv;
986 struct channel_obj *ch = fh->channel;
987 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
989 if (common->fmt.type != tbuf->type)
992 return vb2_querybuf(&common->buffer_queue, tbuf);
995 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
997 struct vpif_fh *fh = NULL;
998 struct channel_obj *ch = NULL;
999 struct common_obj *common = NULL;
1009 common = &(ch->common[VPIF_VIDEO_INDEX]);
1010 if (common->fmt.type != buf->type)
1013 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1014 vpif_err("fh->io_allowed\n");
1018 return vb2_qbuf(&common->buffer_queue, buf);
1021 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1023 struct vpif_fh *fh = priv;
1024 struct channel_obj *ch = fh->channel;
1025 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1028 if (!(*std_id & VPIF_V4L2_STD))
1031 if (common->started) {
1032 vpif_err("streaming in progress\n");
1036 /* Call encoder subdevice function to set the standard */
1037 ch->video.stdid = *std_id;
1038 ch->video.dv_preset = V4L2_DV_INVALID;
1039 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1041 /* Get the information about the standard */
1042 if (vpif_update_resolution(ch))
1045 if ((ch->vpifparams.std_info.width *
1046 ch->vpifparams.std_info.height * 2) >
1047 config_params.channel_bufsize[ch->channel_id]) {
1048 vpif_err("invalid std for this size\n");
1052 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1053 /* Configure the default format information */
1054 vpif_config_format(ch);
1056 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1057 s_std_output, *std_id);
1059 vpif_err("Failed to set output standard\n");
1063 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1066 vpif_err("Failed to set standard for sub devices\n");
1070 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1072 struct vpif_fh *fh = priv;
1073 struct channel_obj *ch = fh->channel;
1075 *std = ch->video.stdid;
1079 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1081 struct vpif_fh *fh = priv;
1082 struct channel_obj *ch = fh->channel;
1083 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1085 return vb2_dqbuf(&common->buffer_queue, p,
1086 (file->f_flags & O_NONBLOCK));
1089 static int vpif_streamon(struct file *file, void *priv,
1090 enum v4l2_buf_type buftype)
1092 struct vpif_fh *fh = priv;
1093 struct channel_obj *ch = fh->channel;
1094 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1095 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1098 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1099 vpif_err("buffer type not supported\n");
1103 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1104 vpif_err("fh->io_allowed\n");
1108 /* If Streaming is already started, return error */
1109 if (common->started) {
1110 vpif_err("channel->started\n");
1114 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1115 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1116 ch->vpifparams.std_info.ycmux_mode == 0)
1117 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1118 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1119 vpif_err("other channel is using\n");
1123 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1127 /* Call vb2_streamon to start streaming in videobuf2 */
1128 ret = vb2_streamon(&common->buffer_queue, buftype);
1130 vpif_err("vb2_streamon\n");
1137 static int vpif_streamoff(struct file *file, void *priv,
1138 enum v4l2_buf_type buftype)
1140 struct vpif_fh *fh = priv;
1141 struct channel_obj *ch = fh->channel;
1142 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1144 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1145 vpif_err("buffer type not supported\n");
1149 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1150 vpif_err("fh->io_allowed\n");
1154 if (!common->started) {
1155 vpif_err("channel->started\n");
1159 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1160 /* disable channel */
1161 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1163 channel2_intr_enable(0);
1165 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1166 (2 == common->started)) {
1168 channel3_intr_enable(0);
1172 common->started = 0;
1173 return vb2_streamoff(&common->buffer_queue, buftype);
1176 static int vpif_cropcap(struct file *file, void *priv,
1177 struct v4l2_cropcap *crop)
1179 struct vpif_fh *fh = priv;
1180 struct channel_obj *ch = fh->channel;
1181 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1182 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1185 crop->bounds.left = crop->bounds.top = 0;
1186 crop->defrect.left = crop->defrect.top = 0;
1187 crop->defrect.height = crop->bounds.height = common->height;
1188 crop->defrect.width = crop->bounds.width = common->width;
1193 static int vpif_enum_output(struct file *file, void *fh,
1194 struct v4l2_output *output)
1197 struct vpif_display_config *config = vpif_dev->platform_data;
1199 if (output->index >= config->output_count) {
1200 vpif_dbg(1, debug, "Invalid output index\n");
1204 strcpy(output->name, config->output[output->index]);
1205 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1206 output->std = VPIF_V4L2_STD;
1211 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1213 struct vpif_fh *fh = priv;
1214 struct channel_obj *ch = fh->channel;
1215 struct video_obj *vid_ch = &ch->video;
1216 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1219 if (common->started) {
1220 vpif_err("Streaming in progress\n");
1224 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1225 s_routing, 0, i, 0);
1228 vpif_err("Failed to set output standard\n");
1230 vid_ch->output_id = i;
1234 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1236 struct vpif_fh *fh = priv;
1237 struct channel_obj *ch = fh->channel;
1238 struct video_obj *vid_ch = &ch->video;
1240 *i = vid_ch->output_id;
1245 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1247 struct vpif_fh *fh = priv;
1248 struct channel_obj *ch = fh->channel;
1250 *p = v4l2_prio_max(&ch->prio);
1255 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1257 struct vpif_fh *fh = priv;
1258 struct channel_obj *ch = fh->channel;
1260 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1264 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1266 * @priv: file handle
1267 * @preset: input preset
1269 static int vpif_enum_dv_presets(struct file *file, void *priv,
1270 struct v4l2_dv_enum_preset *preset)
1272 struct vpif_fh *fh = priv;
1273 struct channel_obj *ch = fh->channel;
1274 struct video_obj *vid_ch = &ch->video;
1276 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1277 video, enum_dv_presets, preset);
1281 * vpif_s_dv_presets() - S_DV_PRESETS handler
1283 * @priv: file handle
1284 * @preset: input preset
1286 static int vpif_s_dv_preset(struct file *file, void *priv,
1287 struct v4l2_dv_preset *preset)
1289 struct vpif_fh *fh = priv;
1290 struct channel_obj *ch = fh->channel;
1291 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1292 struct video_obj *vid_ch = &ch->video;
1295 if (common->started) {
1296 vpif_dbg(1, debug, "streaming in progress\n");
1300 ret = v4l2_prio_check(&ch->prio, fh->prio);
1304 fh->initialized = 1;
1306 /* Call encoder subdevice function to set the standard */
1307 if (mutex_lock_interruptible(&common->lock))
1308 return -ERESTARTSYS;
1310 ch->video.dv_preset = preset->preset;
1311 ch->video.stdid = V4L2_STD_UNKNOWN;
1312 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1314 /* Get the information about the standard */
1315 if (vpif_update_resolution(ch)) {
1318 /* Configure the default format information */
1319 vpif_config_format(ch);
1321 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1322 video, s_dv_preset, preset);
1325 mutex_unlock(&common->lock);
1330 * vpif_g_dv_presets() - G_DV_PRESETS handler
1332 * @priv: file handle
1333 * @preset: input preset
1335 static int vpif_g_dv_preset(struct file *file, void *priv,
1336 struct v4l2_dv_preset *preset)
1338 struct vpif_fh *fh = priv;
1339 struct channel_obj *ch = fh->channel;
1341 preset->preset = ch->video.dv_preset;
1346 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1348 * @priv: file handle
1349 * @timings: digital video timings
1351 static int vpif_s_dv_timings(struct file *file, void *priv,
1352 struct v4l2_dv_timings *timings)
1354 struct vpif_fh *fh = priv;
1355 struct channel_obj *ch = fh->channel;
1356 struct vpif_params *vpifparams = &ch->vpifparams;
1357 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1358 struct video_obj *vid_ch = &ch->video;
1359 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1362 if (timings->type != V4L2_DV_BT_656_1120) {
1363 vpif_dbg(2, debug, "Timing type not defined\n");
1367 /* Configure subdevice timings, if any */
1368 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1369 video, s_dv_timings, timings);
1370 if (ret == -ENOIOCTLCMD) {
1371 vpif_dbg(2, debug, "Custom DV timings not supported by "
1376 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1380 if (!(timings->bt.width && timings->bt.height &&
1381 (timings->bt.hbackporch ||
1382 timings->bt.hfrontporch ||
1383 timings->bt.hsync) &&
1384 timings->bt.vfrontporch &&
1385 (timings->bt.vbackporch ||
1386 timings->bt.vsync))) {
1387 vpif_dbg(2, debug, "Timings for width, height, "
1388 "horizontal back porch, horizontal sync, "
1389 "horizontal front porch, vertical back porch, "
1390 "vertical sync and vertical back porch "
1391 "must be defined\n");
1397 /* Configure video port timings */
1399 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1401 std_info->sav2eav = bt->width;
1404 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1406 if (bt->interlaced) {
1407 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1408 std_info->vsize = bt->height * 2 +
1409 bt->vfrontporch + bt->vsync + bt->vbackporch +
1410 bt->il_vfrontporch + bt->il_vsync +
1412 std_info->l5 = std_info->vsize/2 -
1413 (bt->vfrontporch - 1);
1414 std_info->l7 = std_info->vsize/2 + 1;
1415 std_info->l9 = std_info->l7 + bt->il_vsync +
1416 bt->il_vbackporch + 1;
1417 std_info->l11 = std_info->vsize -
1418 (bt->il_vfrontporch - 1);
1420 vpif_dbg(2, debug, "Required timing values for "
1421 "interlaced BT format missing\n");
1425 std_info->vsize = bt->height + bt->vfrontporch +
1426 bt->vsync + bt->vbackporch;
1427 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1429 strncpy(std_info->name, "Custom timings BT656/1120",
1431 std_info->width = bt->width;
1432 std_info->height = bt->height;
1433 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1434 std_info->ycmux_mode = 0;
1435 std_info->capture_format = 0;
1436 std_info->vbi_supported = 0;
1437 std_info->hd_sd = 1;
1438 std_info->stdid = 0;
1439 std_info->dv_preset = V4L2_DV_INVALID;
1442 vid_ch->dv_preset = V4L2_DV_INVALID;
1448 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1450 * @priv: file handle
1451 * @timings: digital video timings
1453 static int vpif_g_dv_timings(struct file *file, void *priv,
1454 struct v4l2_dv_timings *timings)
1456 struct vpif_fh *fh = priv;
1457 struct channel_obj *ch = fh->channel;
1458 struct video_obj *vid_ch = &ch->video;
1459 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1467 * vpif_g_chip_ident() - Identify the chip
1469 * @priv: file handle
1470 * @chip: chip identity
1472 * Returns zero or -EINVAL if read operations fails.
1474 static int vpif_g_chip_ident(struct file *file, void *priv,
1475 struct v4l2_dbg_chip_ident *chip)
1477 chip->ident = V4L2_IDENT_NONE;
1479 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1480 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1481 vpif_dbg(2, debug, "match_type is invalid.\n");
1485 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1486 g_chip_ident, chip);
1489 #ifdef CONFIG_VIDEO_ADV_DEBUG
1491 * vpif_dbg_g_register() - Read register
1493 * @priv: file handle
1494 * @reg: register to be read
1497 * Returns zero or -EINVAL if read operations fails.
1499 static int vpif_dbg_g_register(struct file *file, void *priv,
1500 struct v4l2_dbg_register *reg){
1501 struct vpif_fh *fh = priv;
1502 struct channel_obj *ch = fh->channel;
1503 struct video_obj *vid_ch = &ch->video;
1505 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1510 * vpif_dbg_s_register() - Write to register
1512 * @priv: file handle
1513 * @reg: register to be modified
1516 * Returns zero or -EINVAL if write operations fails.
1518 static int vpif_dbg_s_register(struct file *file, void *priv,
1519 struct v4l2_dbg_register *reg){
1520 struct vpif_fh *fh = priv;
1521 struct channel_obj *ch = fh->channel;
1522 struct video_obj *vid_ch = &ch->video;
1524 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1530 * vpif_log_status() - Status information
1532 * @priv: file handle
1536 static int vpif_log_status(struct file *filep, void *priv)
1538 /* status for sub devices */
1539 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1544 /* vpif display ioctl operations */
1545 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1546 .vidioc_querycap = vpif_querycap,
1547 .vidioc_g_priority = vpif_g_priority,
1548 .vidioc_s_priority = vpif_s_priority,
1549 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1550 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1551 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1552 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1553 .vidioc_reqbufs = vpif_reqbufs,
1554 .vidioc_querybuf = vpif_querybuf,
1555 .vidioc_qbuf = vpif_qbuf,
1556 .vidioc_dqbuf = vpif_dqbuf,
1557 .vidioc_streamon = vpif_streamon,
1558 .vidioc_streamoff = vpif_streamoff,
1559 .vidioc_s_std = vpif_s_std,
1560 .vidioc_g_std = vpif_g_std,
1561 .vidioc_enum_output = vpif_enum_output,
1562 .vidioc_s_output = vpif_s_output,
1563 .vidioc_g_output = vpif_g_output,
1564 .vidioc_cropcap = vpif_cropcap,
1565 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
1566 .vidioc_s_dv_preset = vpif_s_dv_preset,
1567 .vidioc_g_dv_preset = vpif_g_dv_preset,
1568 .vidioc_s_dv_timings = vpif_s_dv_timings,
1569 .vidioc_g_dv_timings = vpif_g_dv_timings,
1570 .vidioc_g_chip_ident = vpif_g_chip_ident,
1571 #ifdef CONFIG_VIDEO_ADV_DEBUG
1572 .vidioc_g_register = vpif_dbg_g_register,
1573 .vidioc_s_register = vpif_dbg_s_register,
1575 .vidioc_log_status = vpif_log_status,
1578 static const struct v4l2_file_operations vpif_fops = {
1579 .owner = THIS_MODULE,
1581 .release = vpif_release,
1582 .unlocked_ioctl = video_ioctl2,
1587 static struct video_device vpif_video_template = {
1590 .ioctl_ops = &vpif_ioctl_ops,
1591 .tvnorms = VPIF_V4L2_STD,
1592 .current_norm = V4L2_STD_625_50,
1596 /*Configure the channels, buffer sizei, request irq */
1597 static int initialize_vpif(void)
1599 int free_channel_objects_index;
1600 int free_buffer_channel_index;
1601 int free_buffer_index;
1604 /* Default number of buffers should be 3 */
1605 if ((ch2_numbuffers > 0) &&
1606 (ch2_numbuffers < config_params.min_numbuffers))
1607 ch2_numbuffers = config_params.min_numbuffers;
1608 if ((ch3_numbuffers > 0) &&
1609 (ch3_numbuffers < config_params.min_numbuffers))
1610 ch3_numbuffers = config_params.min_numbuffers;
1612 /* Set buffer size to min buffers size if invalid buffer size is
1614 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1616 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1617 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1619 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1621 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1623 if (ch2_numbuffers) {
1624 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1627 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1629 if (ch3_numbuffers) {
1630 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1634 /* Allocate memory for six channel objects */
1635 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1637 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1638 /* If memory allocation fails, return error */
1639 if (!vpif_obj.dev[i]) {
1640 free_channel_objects_index = i;
1642 goto vpif_init_free_channel_objects;
1646 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1647 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1648 free_buffer_index = config_params.numbuffers[i - 1];
1652 vpif_init_free_channel_objects:
1653 for (j = 0; j < free_channel_objects_index; j++)
1654 kfree(vpif_obj.dev[j]);
1659 * vpif_probe: This function creates device entries by register itself to the
1660 * V4L2 driver and initializes fields of each channel objects
1662 static __init int vpif_probe(struct platform_device *pdev)
1664 struct vpif_subdev_info *subdevdata;
1665 struct vpif_display_config *config;
1666 int i, j = 0, k, q, m, err = 0;
1667 struct i2c_adapter *i2c_adap;
1668 struct common_obj *common;
1669 struct channel_obj *ch;
1670 struct video_device *vfd;
1671 struct resource *res;
1675 vpif_dev = &pdev->dev;
1676 err = initialize_vpif();
1679 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1683 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1685 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1690 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1691 for (i = res->start; i <= res->end; i++) {
1692 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
1694 (void *)(&vpif_obj.dev[k]->channel_id))) {
1702 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1704 /* Get the pointer to the channel object */
1705 ch = vpif_obj.dev[i];
1707 /* Allocate memory for video device */
1708 vfd = video_device_alloc();
1710 for (j = 0; j < i; j++) {
1711 ch = vpif_obj.dev[j];
1712 video_device_release(ch->video_dev);
1718 /* Initialize field of video device */
1719 *vfd = vpif_video_template;
1720 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1721 vfd->release = video_device_release;
1722 snprintf(vfd->name, sizeof(vfd->name),
1723 "VPIF_Display_DRIVER_V%s",
1724 VPIF_DISPLAY_VERSION);
1726 /* Set video_dev to the video device */
1727 ch->video_dev = vfd;
1730 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1732 size = resource_size(res);
1733 /* The resources are divided into two equal memory and when
1734 * we have HD output we can add them together
1736 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1737 ch = vpif_obj.dev[j];
1740 /* only enabled if second resource exists */
1741 config_params.video_limit[ch->channel_id] = 0;
1743 config_params.video_limit[ch->channel_id] =
1748 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1749 ch = vpif_obj.dev[j];
1750 /* Initialize field of the channel objects */
1751 atomic_set(&ch->usrs, 0);
1752 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1753 ch->common[k].numbuffers = 0;
1754 common = &ch->common[k];
1755 common->io_usrs = 0;
1756 common->started = 0;
1757 spin_lock_init(&common->irqlock);
1758 mutex_init(&common->lock);
1759 common->numbuffers = 0;
1760 common->set_addr = NULL;
1761 common->ytop_off = common->ybtm_off = 0;
1762 common->ctop_off = common->cbtm_off = 0;
1763 common->cur_frm = common->next_frm = NULL;
1764 memset(&common->fmt, 0, sizeof(common->fmt));
1765 common->numbuffers = config_params.numbuffers[k];
1768 ch->initialized = 0;
1771 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1772 config_params.numbuffers[ch->channel_id];
1774 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1776 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1778 /* Initialize prio member of channel object */
1779 v4l2_prio_init(&ch->prio);
1780 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1781 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1782 /* Locking in file operations other than ioctl should be done
1783 by the driver, not the V4L2 core.
1784 This driver needs auditing so that this flag can be removed. */
1785 set_bit(V4L2_FL_LOCK_ALL_FOPS, &ch->video_dev->flags);
1786 ch->video_dev->lock = &common->lock;
1788 /* register video device */
1789 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1790 (int)ch, (int)&ch->video_dev);
1792 err = video_register_device(ch->video_dev,
1793 VFL_TYPE_GRABBER, (j ? 3 : 2));
1797 video_set_drvdata(ch->video_dev, ch);
1800 i2c_adap = i2c_get_adapter(1);
1801 config = pdev->dev.platform_data;
1802 subdev_count = config->subdev_count;
1803 subdevdata = config->subdevinfo;
1804 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1806 if (vpif_obj.sd == NULL) {
1807 vpif_err("unable to allocate memory for subdevice pointers\n");
1812 for (i = 0; i < subdev_count; i++) {
1813 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1815 &subdevdata[i].board_info,
1817 if (!vpif_obj.sd[i]) {
1818 vpif_err("Error registering v4l2 subdevice\n");
1819 goto probe_subdev_out;
1823 vpif_obj.sd[i]->grp_id = 1 << i;
1826 v4l2_info(&vpif_obj.v4l2_dev,
1827 " VPIF display driver initialized\n");
1833 for (k = 0; k < j; k++) {
1834 ch = vpif_obj.dev[k];
1835 video_unregister_device(ch->video_dev);
1836 video_device_release(ch->video_dev);
1837 ch->video_dev = NULL;
1840 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1841 vpif_err("VPIF IRQ request failed\n");
1842 for (q = k; k >= 0; k--) {
1843 for (m = i; m >= res->start; m--)
1844 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1845 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1853 * vpif_remove: It un-register channels from V4L2 driver
1855 static int vpif_remove(struct platform_device *device)
1857 struct channel_obj *ch;
1860 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1862 /* un-register device */
1863 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1864 /* Get the pointer to the channel object */
1865 ch = vpif_obj.dev[i];
1866 /* Unregister video device */
1867 video_unregister_device(ch->video_dev);
1869 ch->video_dev = NULL;
1875 static __refdata struct platform_driver vpif_driver = {
1877 .name = "vpif_display",
1878 .owner = THIS_MODULE,
1880 .probe = vpif_probe,
1881 .remove = vpif_remove,
1884 static __init int vpif_init(void)
1886 return platform_driver_register(&vpif_driver);
1890 * vpif_cleanup: This function un-registers device and driver to the kernel,
1891 * frees requested irq handler and de-allocates memory allocated for channel
1894 static void vpif_cleanup(void)
1896 struct platform_device *pdev;
1897 struct resource *res;
1901 pdev = container_of(vpif_dev, struct platform_device, dev);
1903 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1904 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1906 (void *)(&vpif_obj.dev[i]->channel_id));
1910 platform_driver_unregister(&vpif_driver);
1912 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1913 kfree(vpif_obj.dev[i]);
1916 module_init(vpif_init);
1917 module_exit(vpif_cleanup);