Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / video / davinci / vpif_display.c
blob286f029100447552550d81c5b6c1d6b716fcf8e7
1 /*
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>
21 #include <linux/fs.h>
22 #include <linux/mm.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>
31 #include <linux/io.h>
32 #include <linux/slab.h>
34 #include <asm/irq.h>
35 #include <asm/page.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 <mach/dm646x.h>
44 #include "vpif_display.h"
45 #include "vpif.h"
47 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
48 MODULE_LICENSE("GPL");
49 MODULE_VERSION(VPIF_DISPLAY_VERSION);
51 #define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
53 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
54 #define vpif_dbg(level, debug, fmt, arg...) \
55 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
57 static int debug = 1;
58 static u32 ch2_numbuffers = 3;
59 static u32 ch3_numbuffers = 3;
60 static u32 ch2_bufsize = 1920 * 1080 * 2;
61 static u32 ch3_bufsize = 720 * 576 * 2;
63 module_param(debug, int, 0644);
64 module_param(ch2_numbuffers, uint, S_IRUGO);
65 module_param(ch3_numbuffers, uint, S_IRUGO);
66 module_param(ch2_bufsize, uint, S_IRUGO);
67 module_param(ch3_bufsize, uint, S_IRUGO);
69 MODULE_PARM_DESC(debug, "Debug level 0-1");
70 MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
71 MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
72 MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
73 MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
75 static struct vpif_config_params config_params = {
76 .min_numbuffers = 3,
77 .numbuffers[0] = 3,
78 .numbuffers[1] = 3,
79 .min_bufsize[0] = 720 * 480 * 2,
80 .min_bufsize[1] = 720 * 480 * 2,
81 .channel_bufsize[0] = 1920 * 1080 * 2,
82 .channel_bufsize[1] = 720 * 576 * 2,
85 static struct vpif_device vpif_obj = { {NULL} };
86 static struct device *vpif_dev;
89 * vpif_uservirt_to_phys: This function is used to convert user
90 * space virtual address to physical address.
92 static u32 vpif_uservirt_to_phys(u32 virtp)
94 struct mm_struct *mm = current->mm;
95 unsigned long physp = 0;
96 struct vm_area_struct *vma;
98 vma = find_vma(mm, virtp);
100 /* For kernel direct-mapped memory, take the easy way */
101 if (virtp >= PAGE_OFFSET) {
102 physp = virt_to_phys((void *)virtp);
103 } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
104 /* this will catch, kernel-allocated, mmaped-to-usermode addr */
105 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
106 } else {
107 /* otherwise, use get_user_pages() for general userland pages */
108 int res, nr_pages = 1;
109 struct page *pages;
110 down_read(&current->mm->mmap_sem);
112 res = get_user_pages(current, current->mm,
113 virtp, nr_pages, 1, 0, &pages, NULL);
114 up_read(&current->mm->mmap_sem);
116 if (res == nr_pages) {
117 physp = __pa(page_address(&pages[0]) +
118 (virtp & ~PAGE_MASK));
119 } else {
120 vpif_err("get_user_pages failed\n");
121 return 0;
125 return physp;
129 * buffer_prepare: This is the callback function called from videobuf_qbuf()
130 * function the buffer is prepared and user space virtual address is converted
131 * into physical address
133 static int vpif_buffer_prepare(struct videobuf_queue *q,
134 struct videobuf_buffer *vb,
135 enum v4l2_field field)
137 struct vpif_fh *fh = q->priv_data;
138 struct common_obj *common;
139 unsigned long addr;
141 common = &fh->channel->common[VPIF_VIDEO_INDEX];
142 if (VIDEOBUF_NEEDS_INIT == vb->state) {
143 vb->width = common->width;
144 vb->height = common->height;
145 vb->size = vb->width * vb->height;
146 vb->field = field;
148 vb->state = VIDEOBUF_PREPARED;
150 /* if user pointer memory mechanism is used, get the physical
151 * address of the buffer */
152 if (V4L2_MEMORY_USERPTR == common->memory) {
153 if (!vb->baddr) {
154 vpif_err("buffer_address is 0\n");
155 return -EINVAL;
158 vb->boff = vpif_uservirt_to_phys(vb->baddr);
159 if (!ISALIGNED(vb->boff))
160 goto buf_align_exit;
163 addr = vb->boff;
164 if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
165 if (!ISALIGNED(addr + common->ytop_off) ||
166 !ISALIGNED(addr + common->ybtm_off) ||
167 !ISALIGNED(addr + common->ctop_off) ||
168 !ISALIGNED(addr + common->cbtm_off))
169 goto buf_align_exit;
171 return 0;
173 buf_align_exit:
174 vpif_err("buffer offset not aligned to 8 bytes\n");
175 return -EINVAL;
179 * vpif_buffer_setup: This function allocates memory for the buffers
181 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
182 unsigned int *size)
184 struct vpif_fh *fh = q->priv_data;
185 struct channel_obj *ch = fh->channel;
186 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
188 if (V4L2_MEMORY_MMAP != common->memory)
189 return 0;
191 *size = config_params.channel_bufsize[ch->channel_id];
192 if (*count < config_params.min_numbuffers)
193 *count = config_params.min_numbuffers;
195 return 0;
199 * vpif_buffer_queue: This function adds the buffer to DMA queue
201 static void vpif_buffer_queue(struct videobuf_queue *q,
202 struct videobuf_buffer *vb)
204 struct vpif_fh *fh = q->priv_data;
205 struct common_obj *common;
207 common = &fh->channel->common[VPIF_VIDEO_INDEX];
209 /* add the buffer to the DMA queue */
210 list_add_tail(&vb->queue, &common->dma_queue);
211 vb->state = VIDEOBUF_QUEUED;
215 * vpif_buffer_release: This function is called from the videobuf layer to
216 * free memory allocated to the buffers
218 static void vpif_buffer_release(struct videobuf_queue *q,
219 struct videobuf_buffer *vb)
221 struct vpif_fh *fh = q->priv_data;
222 struct channel_obj *ch = fh->channel;
223 struct common_obj *common;
224 unsigned int buf_size = 0;
226 common = &ch->common[VPIF_VIDEO_INDEX];
228 videobuf_dma_contig_free(q, vb);
229 vb->state = VIDEOBUF_NEEDS_INIT;
231 if (V4L2_MEMORY_MMAP != common->memory)
232 return;
234 buf_size = config_params.channel_bufsize[ch->channel_id];
237 static struct videobuf_queue_ops video_qops = {
238 .buf_setup = vpif_buffer_setup,
239 .buf_prepare = vpif_buffer_prepare,
240 .buf_queue = vpif_buffer_queue,
241 .buf_release = vpif_buffer_release,
243 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
245 static void process_progressive_mode(struct common_obj *common)
247 unsigned long addr = 0;
249 /* Get the next buffer from buffer queue */
250 common->next_frm = list_entry(common->dma_queue.next,
251 struct videobuf_buffer, queue);
252 /* Remove that buffer from the buffer queue */
253 list_del(&common->next_frm->queue);
254 /* Mark status of the buffer as active */
255 common->next_frm->state = VIDEOBUF_ACTIVE;
257 /* Set top and bottom field addrs in VPIF registers */
258 addr = videobuf_to_dma_contig(common->next_frm);
259 common->set_addr(addr + common->ytop_off,
260 addr + common->ybtm_off,
261 addr + common->ctop_off,
262 addr + common->cbtm_off);
265 static void process_interlaced_mode(int fid, struct common_obj *common)
267 /* device field id and local field id are in sync */
268 /* If this is even field */
269 if (0 == fid) {
270 if (common->cur_frm == common->next_frm)
271 return;
273 /* one frame is displayed If next frame is
274 * available, release cur_frm and move on */
275 /* Copy frame display time */
276 do_gettimeofday(&common->cur_frm->ts);
277 /* Change status of the cur_frm */
278 common->cur_frm->state = VIDEOBUF_DONE;
279 /* unlock semaphore on cur_frm */
280 wake_up_interruptible(&common->cur_frm->done);
281 /* Make cur_frm pointing to next_frm */
282 common->cur_frm = common->next_frm;
284 } else if (1 == fid) { /* odd field */
285 if (list_empty(&common->dma_queue)
286 || (common->cur_frm != common->next_frm)) {
287 return;
289 /* one field is displayed configure the next
290 * frame if it is available else hold on current
291 * frame */
292 /* Get next from the buffer queue */
293 process_progressive_mode(common);
299 * vpif_channel_isr: It changes status of the displayed buffer, takes next
300 * buffer from the queue and sets its address in VPIF registers
302 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
304 struct vpif_device *dev = &vpif_obj;
305 struct channel_obj *ch;
306 struct common_obj *common;
307 enum v4l2_field field;
308 int fid = -1, i;
309 int channel_id = 0;
311 channel_id = *(int *)(dev_id);
312 ch = dev->dev[channel_id];
313 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
314 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
315 common = &ch->common[i];
316 /* If streaming is started in this channel */
317 if (0 == common->started)
318 continue;
320 if (1 == ch->vpifparams.std_info.frm_fmt) {
321 if (list_empty(&common->dma_queue))
322 continue;
324 /* Progressive mode */
325 if (!channel_first_int[i][channel_id]) {
326 /* Mark status of the cur_frm to
327 * done and unlock semaphore on it */
328 do_gettimeofday(&common->cur_frm->ts);
329 common->cur_frm->state = VIDEOBUF_DONE;
330 wake_up_interruptible(&common->cur_frm->done);
331 /* Make cur_frm pointing to next_frm */
332 common->cur_frm = common->next_frm;
335 channel_first_int[i][channel_id] = 0;
336 process_progressive_mode(common);
337 } else {
338 /* Interlaced mode */
339 /* If it is first interrupt, ignore it */
341 if (channel_first_int[i][channel_id]) {
342 channel_first_int[i][channel_id] = 0;
343 continue;
346 if (0 == i) {
347 ch->field_id ^= 1;
348 /* Get field id from VPIF registers */
349 fid = vpif_channel_getfid(ch->channel_id + 2);
350 /* If fid does not match with stored field id */
351 if (fid != ch->field_id) {
352 /* Make them in sync */
353 if (0 == fid)
354 ch->field_id = fid;
356 return IRQ_HANDLED;
359 process_interlaced_mode(fid, common);
363 return IRQ_HANDLED;
366 static int vpif_update_std_info(struct channel_obj *ch)
368 struct video_obj *vid_ch = &ch->video;
369 struct vpif_params *vpifparams = &ch->vpifparams;
370 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
371 const struct vpif_channel_config_params *config;
373 int i;
375 for (i = 0; i < vpif_ch_params_count; i++) {
376 config = &ch_params[i];
377 if (config->hd_sd == 0) {
378 vpif_dbg(2, debug, "SD format\n");
379 if (config->stdid & vid_ch->stdid) {
380 memcpy(std_info, config, sizeof(*config));
381 break;
383 } else {
384 vpif_dbg(2, debug, "HD format\n");
385 if (config->dv_preset == vid_ch->dv_preset) {
386 memcpy(std_info, config, sizeof(*config));
387 break;
392 if (i == vpif_ch_params_count) {
393 vpif_dbg(1, debug, "Format not found\n");
394 return -EINVAL;
397 return 0;
400 static int vpif_update_resolution(struct channel_obj *ch)
402 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
403 struct video_obj *vid_ch = &ch->video;
404 struct vpif_params *vpifparams = &ch->vpifparams;
405 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
407 if (!vid_ch->stdid && !vid_ch->dv_preset && !vid_ch->bt_timings.height)
408 return -EINVAL;
410 if (vid_ch->stdid || vid_ch->dv_preset) {
411 if (vpif_update_std_info(ch))
412 return -EINVAL;
415 common->fmt.fmt.pix.width = std_info->width;
416 common->fmt.fmt.pix.height = std_info->height;
417 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
418 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
420 /* Set height and width paramateres */
421 common->height = std_info->height;
422 common->width = std_info->width;
424 return 0;
428 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
429 * in the top and bottom field
431 static void vpif_calculate_offsets(struct channel_obj *ch)
433 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
434 struct vpif_params *vpifparams = &ch->vpifparams;
435 enum v4l2_field field = common->fmt.fmt.pix.field;
436 struct video_obj *vid_ch = &ch->video;
437 unsigned int hpitch, vpitch, sizeimage;
439 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
440 if (ch->vpifparams.std_info.frm_fmt)
441 vid_ch->buf_field = V4L2_FIELD_NONE;
442 else
443 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
444 } else {
445 vid_ch->buf_field = common->fmt.fmt.pix.field;
448 if (V4L2_MEMORY_USERPTR == common->memory)
449 sizeimage = common->fmt.fmt.pix.sizeimage;
450 else
451 sizeimage = config_params.channel_bufsize[ch->channel_id];
453 hpitch = common->fmt.fmt.pix.bytesperline;
454 vpitch = sizeimage / (hpitch * 2);
455 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
456 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
457 common->ytop_off = 0;
458 common->ybtm_off = hpitch;
459 common->ctop_off = sizeimage / 2;
460 common->cbtm_off = sizeimage / 2 + hpitch;
461 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
462 common->ytop_off = 0;
463 common->ybtm_off = sizeimage / 4;
464 common->ctop_off = sizeimage / 2;
465 common->cbtm_off = common->ctop_off + sizeimage / 4;
466 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
467 common->ybtm_off = 0;
468 common->ytop_off = sizeimage / 4;
469 common->cbtm_off = sizeimage / 2;
470 common->ctop_off = common->cbtm_off + sizeimage / 4;
473 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
474 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
475 vpifparams->video_params.storage_mode = 1;
476 } else {
477 vpifparams->video_params.storage_mode = 0;
480 if (ch->vpifparams.std_info.frm_fmt == 1) {
481 vpifparams->video_params.hpitch =
482 common->fmt.fmt.pix.bytesperline;
483 } else {
484 if ((field == V4L2_FIELD_ANY) ||
485 (field == V4L2_FIELD_INTERLACED))
486 vpifparams->video_params.hpitch =
487 common->fmt.fmt.pix.bytesperline * 2;
488 else
489 vpifparams->video_params.hpitch =
490 common->fmt.fmt.pix.bytesperline;
493 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
496 static void vpif_config_format(struct channel_obj *ch)
498 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
500 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
501 if (config_params.numbuffers[ch->channel_id] == 0)
502 common->memory = V4L2_MEMORY_USERPTR;
503 else
504 common->memory = V4L2_MEMORY_MMAP;
506 common->fmt.fmt.pix.sizeimage =
507 config_params.channel_bufsize[ch->channel_id];
508 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
509 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
512 static int vpif_check_format(struct channel_obj *ch,
513 struct v4l2_pix_format *pixfmt)
515 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
516 enum v4l2_field field = pixfmt->field;
517 u32 sizeimage, hpitch, vpitch;
519 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
520 goto invalid_fmt_exit;
522 if (!(VPIF_VALID_FIELD(field)))
523 goto invalid_fmt_exit;
525 if (pixfmt->bytesperline <= 0)
526 goto invalid_pitch_exit;
528 if (V4L2_MEMORY_USERPTR == common->memory)
529 sizeimage = pixfmt->sizeimage;
530 else
531 sizeimage = config_params.channel_bufsize[ch->channel_id];
533 if (vpif_update_resolution(ch))
534 return -EINVAL;
536 hpitch = pixfmt->bytesperline;
537 vpitch = sizeimage / (hpitch * 2);
539 /* Check for valid value of pitch */
540 if ((hpitch < ch->vpifparams.std_info.width) ||
541 (vpitch < ch->vpifparams.std_info.height))
542 goto invalid_pitch_exit;
544 /* Check for 8 byte alignment */
545 if (!ISALIGNED(hpitch)) {
546 vpif_err("invalid pitch alignment\n");
547 return -EINVAL;
549 pixfmt->width = common->fmt.fmt.pix.width;
550 pixfmt->height = common->fmt.fmt.pix.height;
552 return 0;
554 invalid_fmt_exit:
555 vpif_err("invalid field format\n");
556 return -EINVAL;
558 invalid_pitch_exit:
559 vpif_err("invalid pitch\n");
560 return -EINVAL;
563 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
565 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
567 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
568 common->set_addr = ch3_set_videobuf_addr;
569 } else {
570 if (2 == muxmode)
571 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
572 else
573 common->set_addr = ch2_set_videobuf_addr;
578 * vpif_mmap: It is used to map kernel space buffers into user spaces
580 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
582 struct vpif_fh *fh = filep->private_data;
583 struct channel_obj *ch = fh->channel;
584 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
586 vpif_dbg(2, debug, "vpif_mmap\n");
588 return videobuf_mmap_mapper(&common->buffer_queue, vma);
592 * vpif_poll: It is used for select/poll system call
594 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
596 struct vpif_fh *fh = filep->private_data;
597 struct channel_obj *ch = fh->channel;
598 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
600 if (common->started)
601 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
603 return 0;
607 * vpif_open: It creates object of file handle structure and stores it in
608 * private_data member of filepointer
610 static int vpif_open(struct file *filep)
612 struct video_device *vdev = video_devdata(filep);
613 struct channel_obj *ch = NULL;
614 struct vpif_fh *fh = NULL;
616 ch = video_get_drvdata(vdev);
617 /* Allocate memory for the file handle object */
618 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
619 if (fh == NULL) {
620 vpif_err("unable to allocate memory for file handle object\n");
621 return -ENOMEM;
624 /* store pointer to fh in private_data member of filep */
625 filep->private_data = fh;
626 fh->channel = ch;
627 fh->initialized = 0;
628 if (!ch->initialized) {
629 fh->initialized = 1;
630 ch->initialized = 1;
631 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
634 /* Increment channel usrs counter */
635 atomic_inc(&ch->usrs);
636 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
637 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
638 /* Initialize priority of this instance to default priority */
639 fh->prio = V4L2_PRIORITY_UNSET;
640 v4l2_prio_open(&ch->prio, &fh->prio);
642 return 0;
646 * vpif_release: This function deletes buffer queue, frees the buffers and
647 * the vpif file handle
649 static int vpif_release(struct file *filep)
651 struct vpif_fh *fh = filep->private_data;
652 struct channel_obj *ch = fh->channel;
653 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
655 /* if this instance is doing IO */
656 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
657 /* Reset io_usrs member of channel object */
658 common->io_usrs = 0;
659 /* Disable channel */
660 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
661 enable_channel2(0);
662 channel2_intr_enable(0);
664 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
665 (2 == common->started)) {
666 enable_channel3(0);
667 channel3_intr_enable(0);
669 common->started = 0;
670 /* Free buffers allocated */
671 videobuf_queue_cancel(&common->buffer_queue);
672 videobuf_mmap_free(&common->buffer_queue);
673 common->numbuffers =
674 config_params.numbuffers[ch->channel_id];
677 /* Decrement channel usrs counter */
678 atomic_dec(&ch->usrs);
679 /* If this file handle has initialize encoder device, reset it */
680 if (fh->initialized)
681 ch->initialized = 0;
683 /* Close the priority */
684 v4l2_prio_close(&ch->prio, fh->prio);
685 filep->private_data = NULL;
686 fh->initialized = 0;
687 kfree(fh);
689 return 0;
692 /* functions implementing ioctls */
694 * vpif_querycap() - QUERYCAP handler
695 * @file: file ptr
696 * @priv: file handle
697 * @cap: ptr to v4l2_capability structure
699 static int vpif_querycap(struct file *file, void *priv,
700 struct v4l2_capability *cap)
702 struct vpif_display_config *config = vpif_dev->platform_data;
704 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
705 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
706 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
707 strlcpy(cap->card, config->card_name, sizeof(cap->card));
709 return 0;
712 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
713 struct v4l2_fmtdesc *fmt)
715 if (fmt->index != 0) {
716 vpif_err("Invalid format index\n");
717 return -EINVAL;
720 /* Fill in the information about format */
721 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
722 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
723 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
725 return 0;
728 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
729 struct v4l2_format *fmt)
731 struct vpif_fh *fh = priv;
732 struct channel_obj *ch = fh->channel;
733 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
735 /* Check the validity of the buffer type */
736 if (common->fmt.type != fmt->type)
737 return -EINVAL;
739 if (vpif_update_resolution(ch))
740 return -EINVAL;
741 *fmt = common->fmt;
742 return 0;
745 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
746 struct v4l2_format *fmt)
748 struct vpif_fh *fh = priv;
749 struct v4l2_pix_format *pixfmt;
750 struct channel_obj *ch = fh->channel;
751 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
752 int ret = 0;
754 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
755 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
756 if (!fh->initialized) {
757 vpif_dbg(1, debug, "Channel Busy\n");
758 return -EBUSY;
761 /* Check for the priority */
762 ret = v4l2_prio_check(&ch->prio, fh->prio);
763 if (0 != ret)
764 return ret;
765 fh->initialized = 1;
768 if (common->started) {
769 vpif_dbg(1, debug, "Streaming in progress\n");
770 return -EBUSY;
773 pixfmt = &fmt->fmt.pix;
774 /* Check for valid field format */
775 ret = vpif_check_format(ch, pixfmt);
776 if (ret)
777 return ret;
779 /* store the pix format in the channel object */
780 common->fmt.fmt.pix = *pixfmt;
781 /* store the format in the channel object */
782 common->fmt = *fmt;
783 return 0;
786 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
787 struct v4l2_format *fmt)
789 struct vpif_fh *fh = priv;
790 struct channel_obj *ch = fh->channel;
791 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
792 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
793 int ret = 0;
795 ret = vpif_check_format(ch, pixfmt);
796 if (ret) {
797 *pixfmt = common->fmt.fmt.pix;
798 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
801 return ret;
804 static int vpif_reqbufs(struct file *file, void *priv,
805 struct v4l2_requestbuffers *reqbuf)
807 struct vpif_fh *fh = priv;
808 struct channel_obj *ch = fh->channel;
809 struct common_obj *common;
810 enum v4l2_field field;
811 u8 index = 0;
813 /* This file handle has not initialized the channel,
814 It is not allowed to do settings */
815 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
816 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
817 if (!fh->initialized) {
818 vpif_err("Channel Busy\n");
819 return -EBUSY;
823 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
824 return -EINVAL;
826 index = VPIF_VIDEO_INDEX;
828 common = &ch->common[index];
830 if (common->fmt.type != reqbuf->type)
831 return -EINVAL;
833 if (0 != common->io_usrs)
834 return -EBUSY;
836 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
837 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
838 field = V4L2_FIELD_INTERLACED;
839 else
840 field = common->fmt.fmt.pix.field;
841 } else {
842 field = V4L2_VBI_INTERLACED;
845 /* Initialize videobuf queue as per the buffer type */
846 videobuf_queue_dma_contig_init(&common->buffer_queue,
847 &video_qops, NULL,
848 &common->irqlock,
849 reqbuf->type, field,
850 sizeof(struct videobuf_buffer), fh,
851 &common->lock);
853 /* Set io allowed member of file handle to TRUE */
854 fh->io_allowed[index] = 1;
855 /* Increment io usrs member of channel object to 1 */
856 common->io_usrs = 1;
857 /* Store type of memory requested in channel object */
858 common->memory = reqbuf->memory;
859 INIT_LIST_HEAD(&common->dma_queue);
861 /* Allocate buffers */
862 return videobuf_reqbufs(&common->buffer_queue, reqbuf);
865 static int vpif_querybuf(struct file *file, void *priv,
866 struct v4l2_buffer *tbuf)
868 struct vpif_fh *fh = priv;
869 struct channel_obj *ch = fh->channel;
870 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
872 if (common->fmt.type != tbuf->type)
873 return -EINVAL;
875 return videobuf_querybuf(&common->buffer_queue, tbuf);
878 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
881 struct vpif_fh *fh = priv;
882 struct channel_obj *ch = fh->channel;
883 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
884 struct v4l2_buffer tbuf = *buf;
885 struct videobuf_buffer *buf1;
886 unsigned long addr = 0;
887 unsigned long flags;
888 int ret = 0;
890 if (common->fmt.type != tbuf.type)
891 return -EINVAL;
893 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
894 vpif_err("fh->io_allowed\n");
895 return -EACCES;
898 if (!(list_empty(&common->dma_queue)) ||
899 (common->cur_frm != common->next_frm) ||
900 !(common->started) ||
901 (common->started && (0 == ch->field_id)))
902 return videobuf_qbuf(&common->buffer_queue, buf);
904 /* bufferqueue is empty store buffer address in VPIF registers */
905 mutex_lock(&common->buffer_queue.vb_lock);
906 buf1 = common->buffer_queue.bufs[tbuf.index];
907 if (buf1->memory != tbuf.memory) {
908 vpif_err("invalid buffer type\n");
909 goto qbuf_exit;
912 if ((buf1->state == VIDEOBUF_QUEUED) ||
913 (buf1->state == VIDEOBUF_ACTIVE)) {
914 vpif_err("invalid state\n");
915 goto qbuf_exit;
918 switch (buf1->memory) {
919 case V4L2_MEMORY_MMAP:
920 if (buf1->baddr == 0)
921 goto qbuf_exit;
922 break;
924 case V4L2_MEMORY_USERPTR:
925 if (tbuf.length < buf1->bsize)
926 goto qbuf_exit;
928 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
929 && (buf1->baddr != tbuf.m.userptr)) {
930 vpif_buffer_release(&common->buffer_queue, buf1);
931 buf1->baddr = tbuf.m.userptr;
933 break;
935 default:
936 goto qbuf_exit;
939 local_irq_save(flags);
940 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
941 common->buffer_queue.field);
942 if (ret < 0) {
943 local_irq_restore(flags);
944 goto qbuf_exit;
947 buf1->state = VIDEOBUF_ACTIVE;
948 addr = buf1->boff;
949 common->next_frm = buf1;
950 if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
951 common->set_addr((addr + common->ytop_off),
952 (addr + common->ybtm_off),
953 (addr + common->ctop_off),
954 (addr + common->cbtm_off));
957 local_irq_restore(flags);
958 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
959 mutex_unlock(&common->buffer_queue.vb_lock);
960 return 0;
962 qbuf_exit:
963 mutex_unlock(&common->buffer_queue.vb_lock);
964 return -EINVAL;
967 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
969 struct vpif_fh *fh = priv;
970 struct channel_obj *ch = fh->channel;
971 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
972 int ret = 0;
974 if (!(*std_id & DM646X_V4L2_STD))
975 return -EINVAL;
977 if (common->started) {
978 vpif_err("streaming in progress\n");
979 return -EBUSY;
982 /* Call encoder subdevice function to set the standard */
983 ch->video.stdid = *std_id;
984 ch->video.dv_preset = V4L2_DV_INVALID;
985 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
987 /* Get the information about the standard */
988 if (vpif_update_resolution(ch))
989 return -EINVAL;
991 if ((ch->vpifparams.std_info.width *
992 ch->vpifparams.std_info.height * 2) >
993 config_params.channel_bufsize[ch->channel_id]) {
994 vpif_err("invalid std for this size\n");
995 return -EINVAL;
998 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
999 /* Configure the default format information */
1000 vpif_config_format(ch);
1002 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1003 s_std_output, *std_id);
1004 if (ret < 0) {
1005 vpif_err("Failed to set output standard\n");
1006 return ret;
1009 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1010 s_std, *std_id);
1011 if (ret < 0)
1012 vpif_err("Failed to set standard for sub devices\n");
1013 return ret;
1016 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1018 struct vpif_fh *fh = priv;
1019 struct channel_obj *ch = fh->channel;
1021 *std = ch->video.stdid;
1022 return 0;
1025 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1027 struct vpif_fh *fh = priv;
1028 struct channel_obj *ch = fh->channel;
1029 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1031 return videobuf_dqbuf(&common->buffer_queue, p,
1032 (file->f_flags & O_NONBLOCK));
1035 static int vpif_streamon(struct file *file, void *priv,
1036 enum v4l2_buf_type buftype)
1038 struct vpif_fh *fh = priv;
1039 struct channel_obj *ch = fh->channel;
1040 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1041 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1042 struct vpif_params *vpif = &ch->vpifparams;
1043 struct vpif_display_config *vpif_config_data =
1044 vpif_dev->platform_data;
1045 unsigned long addr = 0;
1046 int ret = 0;
1048 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1049 vpif_err("buffer type not supported\n");
1050 return -EINVAL;
1053 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1054 vpif_err("fh->io_allowed\n");
1055 return -EACCES;
1058 /* If Streaming is already started, return error */
1059 if (common->started) {
1060 vpif_err("channel->started\n");
1061 return -EBUSY;
1064 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1065 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1066 ch->vpifparams.std_info.ycmux_mode == 0)
1067 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1068 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1069 vpif_err("other channel is using\n");
1070 return -EBUSY;
1073 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1074 if (ret < 0)
1075 return ret;
1077 /* Call videobuf_streamon to start streaming in videobuf */
1078 ret = videobuf_streamon(&common->buffer_queue);
1079 if (ret < 0) {
1080 vpif_err("videobuf_streamon\n");
1081 return ret;
1084 /* If buffer queue is empty, return error */
1085 if (list_empty(&common->dma_queue)) {
1086 vpif_err("buffer queue is empty\n");
1087 return -EIO;
1090 /* Get the next frame from the buffer queue */
1091 common->next_frm = common->cur_frm =
1092 list_entry(common->dma_queue.next,
1093 struct videobuf_buffer, queue);
1095 list_del(&common->cur_frm->queue);
1096 /* Mark state of the current frame to active */
1097 common->cur_frm->state = VIDEOBUF_ACTIVE;
1099 /* Initialize field_id and started member */
1100 ch->field_id = 0;
1101 common->started = 1;
1102 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1103 addr = common->cur_frm->boff;
1104 /* Calculate the offset for Y and C data in the buffer */
1105 vpif_calculate_offsets(ch);
1107 if ((ch->vpifparams.std_info.frm_fmt &&
1108 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1109 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1110 || (!ch->vpifparams.std_info.frm_fmt
1111 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1112 vpif_err("conflict in field format and std format\n");
1113 return -EINVAL;
1116 /* clock settings */
1117 ret =
1118 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1119 ch->vpifparams.std_info.hd_sd);
1120 if (ret < 0) {
1121 vpif_err("can't set clock\n");
1122 return ret;
1125 /* set the parameters and addresses */
1126 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1127 if (ret < 0)
1128 return ret;
1130 common->started = ret;
1131 vpif_config_addr(ch, ret);
1132 common->set_addr((addr + common->ytop_off),
1133 (addr + common->ybtm_off),
1134 (addr + common->ctop_off),
1135 (addr + common->cbtm_off));
1137 /* Set interrupt for both the fields in VPIF
1138 Register enable channel in VPIF register */
1139 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1140 channel2_intr_assert();
1141 channel2_intr_enable(1);
1142 enable_channel2(1);
1145 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1146 || (common->started == 2)) {
1147 channel3_intr_assert();
1148 channel3_intr_enable(1);
1149 enable_channel3(1);
1151 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1153 return ret;
1156 static int vpif_streamoff(struct file *file, void *priv,
1157 enum v4l2_buf_type buftype)
1159 struct vpif_fh *fh = priv;
1160 struct channel_obj *ch = fh->channel;
1161 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1163 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1164 vpif_err("buffer type not supported\n");
1165 return -EINVAL;
1168 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1169 vpif_err("fh->io_allowed\n");
1170 return -EACCES;
1173 if (!common->started) {
1174 vpif_err("channel->started\n");
1175 return -EINVAL;
1178 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1179 /* disable channel */
1180 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1181 enable_channel2(0);
1182 channel2_intr_enable(0);
1184 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1185 (2 == common->started)) {
1186 enable_channel3(0);
1187 channel3_intr_enable(0);
1191 common->started = 0;
1192 return videobuf_streamoff(&common->buffer_queue);
1195 static int vpif_cropcap(struct file *file, void *priv,
1196 struct v4l2_cropcap *crop)
1198 struct vpif_fh *fh = priv;
1199 struct channel_obj *ch = fh->channel;
1200 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1201 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1202 return -EINVAL;
1204 crop->bounds.left = crop->bounds.top = 0;
1205 crop->defrect.left = crop->defrect.top = 0;
1206 crop->defrect.height = crop->bounds.height = common->height;
1207 crop->defrect.width = crop->bounds.width = common->width;
1209 return 0;
1212 static int vpif_enum_output(struct file *file, void *fh,
1213 struct v4l2_output *output)
1216 struct vpif_display_config *config = vpif_dev->platform_data;
1218 if (output->index >= config->output_count) {
1219 vpif_dbg(1, debug, "Invalid output index\n");
1220 return -EINVAL;
1223 strcpy(output->name, config->output[output->index]);
1224 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1225 output->std = DM646X_V4L2_STD;
1227 return 0;
1230 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1232 struct vpif_fh *fh = priv;
1233 struct channel_obj *ch = fh->channel;
1234 struct video_obj *vid_ch = &ch->video;
1235 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1236 int ret = 0;
1238 if (common->started) {
1239 vpif_err("Streaming in progress\n");
1240 return -EBUSY;
1243 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1244 s_routing, 0, i, 0);
1246 if (ret < 0)
1247 vpif_err("Failed to set output standard\n");
1249 vid_ch->output_id = i;
1250 return ret;
1253 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1255 struct vpif_fh *fh = priv;
1256 struct channel_obj *ch = fh->channel;
1257 struct video_obj *vid_ch = &ch->video;
1259 *i = vid_ch->output_id;
1261 return 0;
1264 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1266 struct vpif_fh *fh = priv;
1267 struct channel_obj *ch = fh->channel;
1269 *p = v4l2_prio_max(&ch->prio);
1271 return 0;
1274 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1276 struct vpif_fh *fh = priv;
1277 struct channel_obj *ch = fh->channel;
1279 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1283 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1284 * @file: file ptr
1285 * @priv: file handle
1286 * @preset: input preset
1288 static int vpif_enum_dv_presets(struct file *file, void *priv,
1289 struct v4l2_dv_enum_preset *preset)
1291 struct vpif_fh *fh = priv;
1292 struct channel_obj *ch = fh->channel;
1293 struct video_obj *vid_ch = &ch->video;
1295 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1296 video, enum_dv_presets, preset);
1300 * vpif_s_dv_presets() - S_DV_PRESETS handler
1301 * @file: file ptr
1302 * @priv: file handle
1303 * @preset: input preset
1305 static int vpif_s_dv_preset(struct file *file, void *priv,
1306 struct v4l2_dv_preset *preset)
1308 struct vpif_fh *fh = priv;
1309 struct channel_obj *ch = fh->channel;
1310 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1311 struct video_obj *vid_ch = &ch->video;
1312 int ret = 0;
1314 if (common->started) {
1315 vpif_dbg(1, debug, "streaming in progress\n");
1316 return -EBUSY;
1319 ret = v4l2_prio_check(&ch->prio, fh->prio);
1320 if (ret != 0)
1321 return ret;
1323 fh->initialized = 1;
1325 /* Call encoder subdevice function to set the standard */
1326 if (mutex_lock_interruptible(&common->lock))
1327 return -ERESTARTSYS;
1329 ch->video.dv_preset = preset->preset;
1330 ch->video.stdid = V4L2_STD_UNKNOWN;
1331 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1333 /* Get the information about the standard */
1334 if (vpif_update_resolution(ch)) {
1335 ret = -EINVAL;
1336 } else {
1337 /* Configure the default format information */
1338 vpif_config_format(ch);
1340 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1341 video, s_dv_preset, preset);
1344 mutex_unlock(&common->lock);
1346 return ret;
1349 * vpif_g_dv_presets() - G_DV_PRESETS handler
1350 * @file: file ptr
1351 * @priv: file handle
1352 * @preset: input preset
1354 static int vpif_g_dv_preset(struct file *file, void *priv,
1355 struct v4l2_dv_preset *preset)
1357 struct vpif_fh *fh = priv;
1358 struct channel_obj *ch = fh->channel;
1360 preset->preset = ch->video.dv_preset;
1362 return 0;
1365 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1366 * @file: file ptr
1367 * @priv: file handle
1368 * @timings: digital video timings
1370 static int vpif_s_dv_timings(struct file *file, void *priv,
1371 struct v4l2_dv_timings *timings)
1373 struct vpif_fh *fh = priv;
1374 struct channel_obj *ch = fh->channel;
1375 struct vpif_params *vpifparams = &ch->vpifparams;
1376 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1377 struct video_obj *vid_ch = &ch->video;
1378 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1379 int ret;
1381 if (timings->type != V4L2_DV_BT_656_1120) {
1382 vpif_dbg(2, debug, "Timing type not defined\n");
1383 return -EINVAL;
1386 /* Configure subdevice timings, if any */
1387 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1388 video, s_dv_timings, timings);
1389 if (ret == -ENOIOCTLCMD) {
1390 vpif_dbg(2, debug, "Custom DV timings not supported by "
1391 "subdevice\n");
1392 return -EINVAL;
1394 if (ret < 0) {
1395 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1396 return ret;
1399 if (!(timings->bt.width && timings->bt.height &&
1400 (timings->bt.hbackporch ||
1401 timings->bt.hfrontporch ||
1402 timings->bt.hsync) &&
1403 timings->bt.vfrontporch &&
1404 (timings->bt.vbackporch ||
1405 timings->bt.vsync))) {
1406 vpif_dbg(2, debug, "Timings for width, height, "
1407 "horizontal back porch, horizontal sync, "
1408 "horizontal front porch, vertical back porch, "
1409 "vertical sync and vertical back porch "
1410 "must be defined\n");
1411 return -EINVAL;
1414 *bt = timings->bt;
1416 /* Configure video port timings */
1418 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1419 bt->hsync - 8;
1420 std_info->sav2eav = bt->width;
1422 std_info->l1 = 1;
1423 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1425 if (bt->interlaced) {
1426 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1427 std_info->vsize = bt->height * 2 +
1428 bt->vfrontporch + bt->vsync + bt->vbackporch +
1429 bt->il_vfrontporch + bt->il_vsync +
1430 bt->il_vbackporch;
1431 std_info->l5 = std_info->vsize/2 -
1432 (bt->vfrontporch - 1);
1433 std_info->l7 = std_info->vsize/2 + 1;
1434 std_info->l9 = std_info->l7 + bt->il_vsync +
1435 bt->il_vbackporch + 1;
1436 std_info->l11 = std_info->vsize -
1437 (bt->il_vfrontporch - 1);
1438 } else {
1439 vpif_dbg(2, debug, "Required timing values for "
1440 "interlaced BT format missing\n");
1441 return -EINVAL;
1443 } else {
1444 std_info->vsize = bt->height + bt->vfrontporch +
1445 bt->vsync + bt->vbackporch;
1446 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1448 strncpy(std_info->name, "Custom timings BT656/1120",
1449 VPIF_MAX_NAME);
1450 std_info->width = bt->width;
1451 std_info->height = bt->height;
1452 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1453 std_info->ycmux_mode = 0;
1454 std_info->capture_format = 0;
1455 std_info->vbi_supported = 0;
1456 std_info->hd_sd = 1;
1457 std_info->stdid = 0;
1458 std_info->dv_preset = V4L2_DV_INVALID;
1460 vid_ch->stdid = 0;
1461 vid_ch->dv_preset = V4L2_DV_INVALID;
1463 return 0;
1467 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1468 * @file: file ptr
1469 * @priv: file handle
1470 * @timings: digital video timings
1472 static int vpif_g_dv_timings(struct file *file, void *priv,
1473 struct v4l2_dv_timings *timings)
1475 struct vpif_fh *fh = priv;
1476 struct channel_obj *ch = fh->channel;
1477 struct video_obj *vid_ch = &ch->video;
1478 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1480 timings->bt = *bt;
1482 return 0;
1486 * vpif_g_chip_ident() - Identify the chip
1487 * @file: file ptr
1488 * @priv: file handle
1489 * @chip: chip identity
1491 * Returns zero or -EINVAL if read operations fails.
1493 static int vpif_g_chip_ident(struct file *file, void *priv,
1494 struct v4l2_dbg_chip_ident *chip)
1496 chip->ident = V4L2_IDENT_NONE;
1497 chip->revision = 0;
1498 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1499 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1500 vpif_dbg(2, debug, "match_type is invalid.\n");
1501 return -EINVAL;
1504 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1505 g_chip_ident, chip);
1508 #ifdef CONFIG_VIDEO_ADV_DEBUG
1510 * vpif_dbg_g_register() - Read register
1511 * @file: file ptr
1512 * @priv: file handle
1513 * @reg: register to be read
1515 * Debugging only
1516 * Returns zero or -EINVAL if read operations fails.
1518 static int vpif_dbg_g_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,
1525 g_register, reg);
1529 * vpif_dbg_s_register() - Write to register
1530 * @file: file ptr
1531 * @priv: file handle
1532 * @reg: register to be modified
1534 * Debugging only
1535 * Returns zero or -EINVAL if write operations fails.
1537 static int vpif_dbg_s_register(struct file *file, void *priv,
1538 struct v4l2_dbg_register *reg){
1539 struct vpif_fh *fh = priv;
1540 struct channel_obj *ch = fh->channel;
1541 struct video_obj *vid_ch = &ch->video;
1543 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1544 s_register, reg);
1546 #endif
1549 * vpif_log_status() - Status information
1550 * @file: file ptr
1551 * @priv: file handle
1553 * Returns zero.
1555 static int vpif_log_status(struct file *filep, void *priv)
1557 /* status for sub devices */
1558 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1560 return 0;
1563 /* vpif display ioctl operations */
1564 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1565 .vidioc_querycap = vpif_querycap,
1566 .vidioc_g_priority = vpif_g_priority,
1567 .vidioc_s_priority = vpif_s_priority,
1568 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1569 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1570 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1571 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1572 .vidioc_reqbufs = vpif_reqbufs,
1573 .vidioc_querybuf = vpif_querybuf,
1574 .vidioc_qbuf = vpif_qbuf,
1575 .vidioc_dqbuf = vpif_dqbuf,
1576 .vidioc_streamon = vpif_streamon,
1577 .vidioc_streamoff = vpif_streamoff,
1578 .vidioc_s_std = vpif_s_std,
1579 .vidioc_g_std = vpif_g_std,
1580 .vidioc_enum_output = vpif_enum_output,
1581 .vidioc_s_output = vpif_s_output,
1582 .vidioc_g_output = vpif_g_output,
1583 .vidioc_cropcap = vpif_cropcap,
1584 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
1585 .vidioc_s_dv_preset = vpif_s_dv_preset,
1586 .vidioc_g_dv_preset = vpif_g_dv_preset,
1587 .vidioc_s_dv_timings = vpif_s_dv_timings,
1588 .vidioc_g_dv_timings = vpif_g_dv_timings,
1589 .vidioc_g_chip_ident = vpif_g_chip_ident,
1590 #ifdef CONFIG_VIDEO_ADV_DEBUG
1591 .vidioc_g_register = vpif_dbg_g_register,
1592 .vidioc_s_register = vpif_dbg_s_register,
1593 #endif
1594 .vidioc_log_status = vpif_log_status,
1597 static const struct v4l2_file_operations vpif_fops = {
1598 .owner = THIS_MODULE,
1599 .open = vpif_open,
1600 .release = vpif_release,
1601 .unlocked_ioctl = video_ioctl2,
1602 .mmap = vpif_mmap,
1603 .poll = vpif_poll
1606 static struct video_device vpif_video_template = {
1607 .name = "vpif",
1608 .fops = &vpif_fops,
1609 .ioctl_ops = &vpif_ioctl_ops,
1610 .tvnorms = DM646X_V4L2_STD,
1611 .current_norm = V4L2_STD_625_50,
1615 /*Configure the channels, buffer sizei, request irq */
1616 static int initialize_vpif(void)
1618 int free_channel_objects_index;
1619 int free_buffer_channel_index;
1620 int free_buffer_index;
1621 int err = 0, i, j;
1623 /* Default number of buffers should be 3 */
1624 if ((ch2_numbuffers > 0) &&
1625 (ch2_numbuffers < config_params.min_numbuffers))
1626 ch2_numbuffers = config_params.min_numbuffers;
1627 if ((ch3_numbuffers > 0) &&
1628 (ch3_numbuffers < config_params.min_numbuffers))
1629 ch3_numbuffers = config_params.min_numbuffers;
1631 /* Set buffer size to min buffers size if invalid buffer size is
1632 * given */
1633 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1634 ch2_bufsize =
1635 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1636 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1637 ch3_bufsize =
1638 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1640 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1642 if (ch2_numbuffers) {
1643 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1644 ch2_bufsize;
1646 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1648 if (ch3_numbuffers) {
1649 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1650 ch3_bufsize;
1653 /* Allocate memory for six channel objects */
1654 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1655 vpif_obj.dev[i] =
1656 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1657 /* If memory allocation fails, return error */
1658 if (!vpif_obj.dev[i]) {
1659 free_channel_objects_index = i;
1660 err = -ENOMEM;
1661 goto vpif_init_free_channel_objects;
1665 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1666 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1667 free_buffer_index = config_params.numbuffers[i - 1];
1669 return 0;
1671 vpif_init_free_channel_objects:
1672 for (j = 0; j < free_channel_objects_index; j++)
1673 kfree(vpif_obj.dev[j]);
1674 return err;
1678 * vpif_probe: This function creates device entries by register itself to the
1679 * V4L2 driver and initializes fields of each channel objects
1681 static __init int vpif_probe(struct platform_device *pdev)
1683 struct vpif_subdev_info *subdevdata;
1684 struct vpif_display_config *config;
1685 int i, j = 0, k, q, m, err = 0;
1686 struct i2c_adapter *i2c_adap;
1687 struct common_obj *common;
1688 struct channel_obj *ch;
1689 struct video_device *vfd;
1690 struct resource *res;
1691 int subdev_count;
1693 vpif_dev = &pdev->dev;
1695 err = initialize_vpif();
1697 if (err) {
1698 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1699 return err;
1702 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1703 if (err) {
1704 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1705 return err;
1708 k = 0;
1709 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1710 for (i = res->start; i <= res->end; i++) {
1711 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1712 "DM646x_Display",
1713 (void *)(&vpif_obj.dev[k]->channel_id))) {
1714 err = -EBUSY;
1715 goto vpif_int_err;
1718 k++;
1721 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1723 /* Get the pointer to the channel object */
1724 ch = vpif_obj.dev[i];
1726 /* Allocate memory for video device */
1727 vfd = video_device_alloc();
1728 if (vfd == NULL) {
1729 for (j = 0; j < i; j++) {
1730 ch = vpif_obj.dev[j];
1731 video_device_release(ch->video_dev);
1733 err = -ENOMEM;
1734 goto vpif_int_err;
1737 /* Initialize field of video device */
1738 *vfd = vpif_video_template;
1739 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1740 vfd->release = video_device_release;
1741 snprintf(vfd->name, sizeof(vfd->name),
1742 "DM646x_VPIFDisplay_DRIVER_V%s",
1743 VPIF_DISPLAY_VERSION);
1745 /* Set video_dev to the video device */
1746 ch->video_dev = vfd;
1749 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1750 ch = vpif_obj.dev[j];
1751 /* Initialize field of the channel objects */
1752 atomic_set(&ch->usrs, 0);
1753 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1754 ch->common[k].numbuffers = 0;
1755 common = &ch->common[k];
1756 common->io_usrs = 0;
1757 common->started = 0;
1758 spin_lock_init(&common->irqlock);
1759 mutex_init(&common->lock);
1760 common->numbuffers = 0;
1761 common->set_addr = NULL;
1762 common->ytop_off = common->ybtm_off = 0;
1763 common->ctop_off = common->cbtm_off = 0;
1764 common->cur_frm = common->next_frm = NULL;
1765 memset(&common->fmt, 0, sizeof(common->fmt));
1766 common->numbuffers = config_params.numbuffers[k];
1769 ch->initialized = 0;
1770 ch->channel_id = j;
1771 if (j < 2)
1772 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1773 config_params.numbuffers[ch->channel_id];
1774 else
1775 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1777 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1779 /* Initialize prio member of channel object */
1780 v4l2_prio_init(&ch->prio);
1781 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1782 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1783 ch->video_dev->lock = &common->lock;
1785 /* register video device */
1786 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1787 (int)ch, (int)&ch->video_dev);
1789 err = video_register_device(ch->video_dev,
1790 VFL_TYPE_GRABBER, (j ? 3 : 2));
1791 if (err < 0)
1792 goto probe_out;
1794 video_set_drvdata(ch->video_dev, ch);
1797 i2c_adap = i2c_get_adapter(1);
1798 config = pdev->dev.platform_data;
1799 subdev_count = config->subdev_count;
1800 subdevdata = config->subdevinfo;
1801 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1802 GFP_KERNEL);
1803 if (vpif_obj.sd == NULL) {
1804 vpif_err("unable to allocate memory for subdevice pointers\n");
1805 err = -ENOMEM;
1806 goto probe_out;
1809 for (i = 0; i < subdev_count; i++) {
1810 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1811 i2c_adap,
1812 &subdevdata[i].board_info,
1813 NULL);
1814 if (!vpif_obj.sd[i]) {
1815 vpif_err("Error registering v4l2 subdevice\n");
1816 goto probe_subdev_out;
1819 if (vpif_obj.sd[i])
1820 vpif_obj.sd[i]->grp_id = 1 << i;
1823 v4l2_info(&vpif_obj.v4l2_dev,
1824 "DM646x VPIF display driver initialized\n");
1825 return 0;
1827 probe_subdev_out:
1828 kfree(vpif_obj.sd);
1829 probe_out:
1830 for (k = 0; k < j; k++) {
1831 ch = vpif_obj.dev[k];
1832 video_unregister_device(ch->video_dev);
1833 video_device_release(ch->video_dev);
1834 ch->video_dev = NULL;
1836 vpif_int_err:
1837 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1838 vpif_err("VPIF IRQ request failed\n");
1839 for (q = k; k >= 0; k--) {
1840 for (m = i; m >= res->start; m--)
1841 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1842 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1843 m = res->end;
1846 return err;
1850 * vpif_remove: It un-register channels from V4L2 driver
1852 static int vpif_remove(struct platform_device *device)
1854 struct channel_obj *ch;
1855 int i;
1857 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1859 /* un-register device */
1860 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1861 /* Get the pointer to the channel object */
1862 ch = vpif_obj.dev[i];
1863 /* Unregister video device */
1864 video_unregister_device(ch->video_dev);
1866 ch->video_dev = NULL;
1869 return 0;
1872 static __refdata struct platform_driver vpif_driver = {
1873 .driver = {
1874 .name = "vpif_display",
1875 .owner = THIS_MODULE,
1877 .probe = vpif_probe,
1878 .remove = vpif_remove,
1881 static __init int vpif_init(void)
1883 return platform_driver_register(&vpif_driver);
1887 * vpif_cleanup: This function un-registers device and driver to the kernel,
1888 * frees requested irq handler and de-allocates memory allocated for channel
1889 * objects.
1891 static void vpif_cleanup(void)
1893 struct platform_device *pdev;
1894 struct resource *res;
1895 int irq_num;
1896 int i = 0;
1898 pdev = container_of(vpif_dev, struct platform_device, dev);
1900 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1901 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1902 free_irq(irq_num,
1903 (void *)(&vpif_obj.dev[i]->channel_id));
1904 i++;
1907 platform_driver_unregister(&vpif_driver);
1908 kfree(vpif_obj.sd);
1909 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1910 kfree(vpif_obj.dev[i]);
1913 module_init(vpif_init);
1914 module_exit(vpif_cleanup);