net: add skb_dst_force() in sock_queue_err_skb()
[linux/fpc-iii.git] / drivers / media / video / davinci / vpif_display.c
blobcdf659abdc2ab73ba172d85fc6a6aa75db4390c1
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/version.h>
33 #include <linux/slab.h>
35 #include <asm/irq.h>
36 #include <asm/page.h>
38 #include <media/adv7343.h>
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-ioctl.h>
41 #include <media/v4l2-chip-ident.h>
43 #include <mach/dm646x.h>
45 #include "vpif_display.h"
46 #include "vpif.h"
48 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
49 MODULE_LICENSE("GPL");
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->version = VPIF_DISPLAY_VERSION_CODE;
705 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
706 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
707 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
708 strlcpy(cap->card, config->card_name, sizeof(cap->card));
710 return 0;
713 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
714 struct v4l2_fmtdesc *fmt)
716 if (fmt->index != 0) {
717 vpif_err("Invalid format index\n");
718 return -EINVAL;
721 /* Fill in the information about format */
722 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
723 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
724 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
726 return 0;
729 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
730 struct v4l2_format *fmt)
732 struct vpif_fh *fh = priv;
733 struct channel_obj *ch = fh->channel;
734 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
736 /* Check the validity of the buffer type */
737 if (common->fmt.type != fmt->type)
738 return -EINVAL;
740 if (vpif_update_resolution(ch))
741 return -EINVAL;
742 *fmt = common->fmt;
743 return 0;
746 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
747 struct v4l2_format *fmt)
749 struct vpif_fh *fh = priv;
750 struct v4l2_pix_format *pixfmt;
751 struct channel_obj *ch = fh->channel;
752 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
753 int ret = 0;
755 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
756 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
757 if (!fh->initialized) {
758 vpif_dbg(1, debug, "Channel Busy\n");
759 return -EBUSY;
762 /* Check for the priority */
763 ret = v4l2_prio_check(&ch->prio, fh->prio);
764 if (0 != ret)
765 return ret;
766 fh->initialized = 1;
769 if (common->started) {
770 vpif_dbg(1, debug, "Streaming in progress\n");
771 return -EBUSY;
774 pixfmt = &fmt->fmt.pix;
775 /* Check for valid field format */
776 ret = vpif_check_format(ch, pixfmt);
777 if (ret)
778 return ret;
780 /* store the pix format in the channel object */
781 common->fmt.fmt.pix = *pixfmt;
782 /* store the format in the channel object */
783 common->fmt = *fmt;
784 return 0;
787 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
788 struct v4l2_format *fmt)
790 struct vpif_fh *fh = priv;
791 struct channel_obj *ch = fh->channel;
792 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
793 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
794 int ret = 0;
796 ret = vpif_check_format(ch, pixfmt);
797 if (ret) {
798 *pixfmt = common->fmt.fmt.pix;
799 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
802 return ret;
805 static int vpif_reqbufs(struct file *file, void *priv,
806 struct v4l2_requestbuffers *reqbuf)
808 struct vpif_fh *fh = priv;
809 struct channel_obj *ch = fh->channel;
810 struct common_obj *common;
811 enum v4l2_field field;
812 u8 index = 0;
814 /* This file handle has not initialized the channel,
815 It is not allowed to do settings */
816 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
817 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
818 if (!fh->initialized) {
819 vpif_err("Channel Busy\n");
820 return -EBUSY;
824 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
825 return -EINVAL;
827 index = VPIF_VIDEO_INDEX;
829 common = &ch->common[index];
831 if (common->fmt.type != reqbuf->type)
832 return -EINVAL;
834 if (0 != common->io_usrs)
835 return -EBUSY;
837 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
838 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
839 field = V4L2_FIELD_INTERLACED;
840 else
841 field = common->fmt.fmt.pix.field;
842 } else {
843 field = V4L2_VBI_INTERLACED;
846 /* Initialize videobuf queue as per the buffer type */
847 videobuf_queue_dma_contig_init(&common->buffer_queue,
848 &video_qops, NULL,
849 &common->irqlock,
850 reqbuf->type, field,
851 sizeof(struct videobuf_buffer), fh,
852 &common->lock);
854 /* Set io allowed member of file handle to TRUE */
855 fh->io_allowed[index] = 1;
856 /* Increment io usrs member of channel object to 1 */
857 common->io_usrs = 1;
858 /* Store type of memory requested in channel object */
859 common->memory = reqbuf->memory;
860 INIT_LIST_HEAD(&common->dma_queue);
862 /* Allocate buffers */
863 return videobuf_reqbufs(&common->buffer_queue, reqbuf);
866 static int vpif_querybuf(struct file *file, void *priv,
867 struct v4l2_buffer *tbuf)
869 struct vpif_fh *fh = priv;
870 struct channel_obj *ch = fh->channel;
871 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
873 if (common->fmt.type != tbuf->type)
874 return -EINVAL;
876 return videobuf_querybuf(&common->buffer_queue, tbuf);
879 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
882 struct vpif_fh *fh = priv;
883 struct channel_obj *ch = fh->channel;
884 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
885 struct v4l2_buffer tbuf = *buf;
886 struct videobuf_buffer *buf1;
887 unsigned long addr = 0;
888 unsigned long flags;
889 int ret = 0;
891 if (common->fmt.type != tbuf.type)
892 return -EINVAL;
894 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
895 vpif_err("fh->io_allowed\n");
896 return -EACCES;
899 if (!(list_empty(&common->dma_queue)) ||
900 (common->cur_frm != common->next_frm) ||
901 !(common->started) ||
902 (common->started && (0 == ch->field_id)))
903 return videobuf_qbuf(&common->buffer_queue, buf);
905 /* bufferqueue is empty store buffer address in VPIF registers */
906 mutex_lock(&common->buffer_queue.vb_lock);
907 buf1 = common->buffer_queue.bufs[tbuf.index];
908 if (buf1->memory != tbuf.memory) {
909 vpif_err("invalid buffer type\n");
910 goto qbuf_exit;
913 if ((buf1->state == VIDEOBUF_QUEUED) ||
914 (buf1->state == VIDEOBUF_ACTIVE)) {
915 vpif_err("invalid state\n");
916 goto qbuf_exit;
919 switch (buf1->memory) {
920 case V4L2_MEMORY_MMAP:
921 if (buf1->baddr == 0)
922 goto qbuf_exit;
923 break;
925 case V4L2_MEMORY_USERPTR:
926 if (tbuf.length < buf1->bsize)
927 goto qbuf_exit;
929 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
930 && (buf1->baddr != tbuf.m.userptr)) {
931 vpif_buffer_release(&common->buffer_queue, buf1);
932 buf1->baddr = tbuf.m.userptr;
934 break;
936 default:
937 goto qbuf_exit;
940 local_irq_save(flags);
941 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
942 common->buffer_queue.field);
943 if (ret < 0) {
944 local_irq_restore(flags);
945 goto qbuf_exit;
948 buf1->state = VIDEOBUF_ACTIVE;
949 addr = buf1->boff;
950 common->next_frm = buf1;
951 if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
952 common->set_addr((addr + common->ytop_off),
953 (addr + common->ybtm_off),
954 (addr + common->ctop_off),
955 (addr + common->cbtm_off));
958 local_irq_restore(flags);
959 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
960 mutex_unlock(&common->buffer_queue.vb_lock);
961 return 0;
963 qbuf_exit:
964 mutex_unlock(&common->buffer_queue.vb_lock);
965 return -EINVAL;
968 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
970 struct vpif_fh *fh = priv;
971 struct channel_obj *ch = fh->channel;
972 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
973 int ret = 0;
975 if (!(*std_id & DM646X_V4L2_STD))
976 return -EINVAL;
978 if (common->started) {
979 vpif_err("streaming in progress\n");
980 return -EBUSY;
983 /* Call encoder subdevice function to set the standard */
984 ch->video.stdid = *std_id;
985 ch->video.dv_preset = V4L2_DV_INVALID;
986 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
988 /* Get the information about the standard */
989 if (vpif_update_resolution(ch))
990 return -EINVAL;
992 if ((ch->vpifparams.std_info.width *
993 ch->vpifparams.std_info.height * 2) >
994 config_params.channel_bufsize[ch->channel_id]) {
995 vpif_err("invalid std for this size\n");
996 return -EINVAL;
999 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1000 /* Configure the default format information */
1001 vpif_config_format(ch);
1003 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1004 s_std_output, *std_id);
1005 if (ret < 0) {
1006 vpif_err("Failed to set output standard\n");
1007 return ret;
1010 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1011 s_std, *std_id);
1012 if (ret < 0)
1013 vpif_err("Failed to set standard for sub devices\n");
1014 return ret;
1017 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1019 struct vpif_fh *fh = priv;
1020 struct channel_obj *ch = fh->channel;
1022 *std = ch->video.stdid;
1023 return 0;
1026 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1028 struct vpif_fh *fh = priv;
1029 struct channel_obj *ch = fh->channel;
1030 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1032 return videobuf_dqbuf(&common->buffer_queue, p,
1033 (file->f_flags & O_NONBLOCK));
1036 static int vpif_streamon(struct file *file, void *priv,
1037 enum v4l2_buf_type buftype)
1039 struct vpif_fh *fh = priv;
1040 struct channel_obj *ch = fh->channel;
1041 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1042 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1043 struct vpif_params *vpif = &ch->vpifparams;
1044 struct vpif_display_config *vpif_config_data =
1045 vpif_dev->platform_data;
1046 unsigned long addr = 0;
1047 int ret = 0;
1049 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1050 vpif_err("buffer type not supported\n");
1051 return -EINVAL;
1054 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1055 vpif_err("fh->io_allowed\n");
1056 return -EACCES;
1059 /* If Streaming is already started, return error */
1060 if (common->started) {
1061 vpif_err("channel->started\n");
1062 return -EBUSY;
1065 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1066 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1067 ch->vpifparams.std_info.ycmux_mode == 0)
1068 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1069 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1070 vpif_err("other channel is using\n");
1071 return -EBUSY;
1074 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1075 if (ret < 0)
1076 return ret;
1078 /* Call videobuf_streamon to start streaming in videobuf */
1079 ret = videobuf_streamon(&common->buffer_queue);
1080 if (ret < 0) {
1081 vpif_err("videobuf_streamon\n");
1082 return ret;
1085 /* If buffer queue is empty, return error */
1086 if (list_empty(&common->dma_queue)) {
1087 vpif_err("buffer queue is empty\n");
1088 return -EIO;
1091 /* Get the next frame from the buffer queue */
1092 common->next_frm = common->cur_frm =
1093 list_entry(common->dma_queue.next,
1094 struct videobuf_buffer, queue);
1096 list_del(&common->cur_frm->queue);
1097 /* Mark state of the current frame to active */
1098 common->cur_frm->state = VIDEOBUF_ACTIVE;
1100 /* Initialize field_id and started member */
1101 ch->field_id = 0;
1102 common->started = 1;
1103 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1104 addr = common->cur_frm->boff;
1105 /* Calculate the offset for Y and C data in the buffer */
1106 vpif_calculate_offsets(ch);
1108 if ((ch->vpifparams.std_info.frm_fmt &&
1109 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1110 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1111 || (!ch->vpifparams.std_info.frm_fmt
1112 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1113 vpif_err("conflict in field format and std format\n");
1114 return -EINVAL;
1117 /* clock settings */
1118 ret =
1119 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1120 ch->vpifparams.std_info.hd_sd);
1121 if (ret < 0) {
1122 vpif_err("can't set clock\n");
1123 return ret;
1126 /* set the parameters and addresses */
1127 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1128 if (ret < 0)
1129 return ret;
1131 common->started = ret;
1132 vpif_config_addr(ch, ret);
1133 common->set_addr((addr + common->ytop_off),
1134 (addr + common->ybtm_off),
1135 (addr + common->ctop_off),
1136 (addr + common->cbtm_off));
1138 /* Set interrupt for both the fields in VPIF
1139 Register enable channel in VPIF register */
1140 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1141 channel2_intr_assert();
1142 channel2_intr_enable(1);
1143 enable_channel2(1);
1146 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1147 || (common->started == 2)) {
1148 channel3_intr_assert();
1149 channel3_intr_enable(1);
1150 enable_channel3(1);
1152 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1154 return ret;
1157 static int vpif_streamoff(struct file *file, void *priv,
1158 enum v4l2_buf_type buftype)
1160 struct vpif_fh *fh = priv;
1161 struct channel_obj *ch = fh->channel;
1162 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1164 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1165 vpif_err("buffer type not supported\n");
1166 return -EINVAL;
1169 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1170 vpif_err("fh->io_allowed\n");
1171 return -EACCES;
1174 if (!common->started) {
1175 vpif_err("channel->started\n");
1176 return -EINVAL;
1179 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1180 /* disable channel */
1181 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1182 enable_channel2(0);
1183 channel2_intr_enable(0);
1185 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1186 (2 == common->started)) {
1187 enable_channel3(0);
1188 channel3_intr_enable(0);
1192 common->started = 0;
1193 return videobuf_streamoff(&common->buffer_queue);
1196 static int vpif_cropcap(struct file *file, void *priv,
1197 struct v4l2_cropcap *crop)
1199 struct vpif_fh *fh = priv;
1200 struct channel_obj *ch = fh->channel;
1201 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1202 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1203 return -EINVAL;
1205 crop->bounds.left = crop->bounds.top = 0;
1206 crop->defrect.left = crop->defrect.top = 0;
1207 crop->defrect.height = crop->bounds.height = common->height;
1208 crop->defrect.width = crop->bounds.width = common->width;
1210 return 0;
1213 static int vpif_enum_output(struct file *file, void *fh,
1214 struct v4l2_output *output)
1217 struct vpif_display_config *config = vpif_dev->platform_data;
1219 if (output->index >= config->output_count) {
1220 vpif_dbg(1, debug, "Invalid output index\n");
1221 return -EINVAL;
1224 strcpy(output->name, config->output[output->index]);
1225 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1226 output->std = DM646X_V4L2_STD;
1228 return 0;
1231 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1233 struct vpif_fh *fh = priv;
1234 struct channel_obj *ch = fh->channel;
1235 struct video_obj *vid_ch = &ch->video;
1236 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1237 int ret = 0;
1239 if (common->started) {
1240 vpif_err("Streaming in progress\n");
1241 return -EBUSY;
1244 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1245 s_routing, 0, i, 0);
1247 if (ret < 0)
1248 vpif_err("Failed to set output standard\n");
1250 vid_ch->output_id = i;
1251 return ret;
1254 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1256 struct vpif_fh *fh = priv;
1257 struct channel_obj *ch = fh->channel;
1258 struct video_obj *vid_ch = &ch->video;
1260 *i = vid_ch->output_id;
1262 return 0;
1265 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1267 struct vpif_fh *fh = priv;
1268 struct channel_obj *ch = fh->channel;
1270 *p = v4l2_prio_max(&ch->prio);
1272 return 0;
1275 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1277 struct vpif_fh *fh = priv;
1278 struct channel_obj *ch = fh->channel;
1280 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1284 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1285 * @file: file ptr
1286 * @priv: file handle
1287 * @preset: input preset
1289 static int vpif_enum_dv_presets(struct file *file, void *priv,
1290 struct v4l2_dv_enum_preset *preset)
1292 struct vpif_fh *fh = priv;
1293 struct channel_obj *ch = fh->channel;
1294 struct video_obj *vid_ch = &ch->video;
1296 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1297 video, enum_dv_presets, preset);
1301 * vpif_s_dv_presets() - S_DV_PRESETS handler
1302 * @file: file ptr
1303 * @priv: file handle
1304 * @preset: input preset
1306 static int vpif_s_dv_preset(struct file *file, void *priv,
1307 struct v4l2_dv_preset *preset)
1309 struct vpif_fh *fh = priv;
1310 struct channel_obj *ch = fh->channel;
1311 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1312 struct video_obj *vid_ch = &ch->video;
1313 int ret = 0;
1315 if (common->started) {
1316 vpif_dbg(1, debug, "streaming in progress\n");
1317 return -EBUSY;
1320 ret = v4l2_prio_check(&ch->prio, fh->prio);
1321 if (ret != 0)
1322 return ret;
1324 fh->initialized = 1;
1326 /* Call encoder subdevice function to set the standard */
1327 if (mutex_lock_interruptible(&common->lock))
1328 return -ERESTARTSYS;
1330 ch->video.dv_preset = preset->preset;
1331 ch->video.stdid = V4L2_STD_UNKNOWN;
1332 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1334 /* Get the information about the standard */
1335 if (vpif_update_resolution(ch)) {
1336 ret = -EINVAL;
1337 } else {
1338 /* Configure the default format information */
1339 vpif_config_format(ch);
1341 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1342 video, s_dv_preset, preset);
1345 mutex_unlock(&common->lock);
1347 return ret;
1350 * vpif_g_dv_presets() - G_DV_PRESETS handler
1351 * @file: file ptr
1352 * @priv: file handle
1353 * @preset: input preset
1355 static int vpif_g_dv_preset(struct file *file, void *priv,
1356 struct v4l2_dv_preset *preset)
1358 struct vpif_fh *fh = priv;
1359 struct channel_obj *ch = fh->channel;
1361 preset->preset = ch->video.dv_preset;
1363 return 0;
1366 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1367 * @file: file ptr
1368 * @priv: file handle
1369 * @timings: digital video timings
1371 static int vpif_s_dv_timings(struct file *file, void *priv,
1372 struct v4l2_dv_timings *timings)
1374 struct vpif_fh *fh = priv;
1375 struct channel_obj *ch = fh->channel;
1376 struct vpif_params *vpifparams = &ch->vpifparams;
1377 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1378 struct video_obj *vid_ch = &ch->video;
1379 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1380 int ret;
1382 if (timings->type != V4L2_DV_BT_656_1120) {
1383 vpif_dbg(2, debug, "Timing type not defined\n");
1384 return -EINVAL;
1387 /* Configure subdevice timings, if any */
1388 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1389 video, s_dv_timings, timings);
1390 if (ret == -ENOIOCTLCMD) {
1391 vpif_dbg(2, debug, "Custom DV timings not supported by "
1392 "subdevice\n");
1393 return -EINVAL;
1395 if (ret < 0) {
1396 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1397 return ret;
1400 if (!(timings->bt.width && timings->bt.height &&
1401 (timings->bt.hbackporch ||
1402 timings->bt.hfrontporch ||
1403 timings->bt.hsync) &&
1404 timings->bt.vfrontporch &&
1405 (timings->bt.vbackporch ||
1406 timings->bt.vsync))) {
1407 vpif_dbg(2, debug, "Timings for width, height, "
1408 "horizontal back porch, horizontal sync, "
1409 "horizontal front porch, vertical back porch, "
1410 "vertical sync and vertical back porch "
1411 "must be defined\n");
1412 return -EINVAL;
1415 *bt = timings->bt;
1417 /* Configure video port timings */
1419 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1420 bt->hsync - 8;
1421 std_info->sav2eav = bt->width;
1423 std_info->l1 = 1;
1424 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1426 if (bt->interlaced) {
1427 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1428 std_info->vsize = bt->height * 2 +
1429 bt->vfrontporch + bt->vsync + bt->vbackporch +
1430 bt->il_vfrontporch + bt->il_vsync +
1431 bt->il_vbackporch;
1432 std_info->l5 = std_info->vsize/2 -
1433 (bt->vfrontporch - 1);
1434 std_info->l7 = std_info->vsize/2 + 1;
1435 std_info->l9 = std_info->l7 + bt->il_vsync +
1436 bt->il_vbackporch + 1;
1437 std_info->l11 = std_info->vsize -
1438 (bt->il_vfrontporch - 1);
1439 } else {
1440 vpif_dbg(2, debug, "Required timing values for "
1441 "interlaced BT format missing\n");
1442 return -EINVAL;
1444 } else {
1445 std_info->vsize = bt->height + bt->vfrontporch +
1446 bt->vsync + bt->vbackporch;
1447 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1449 strncpy(std_info->name, "Custom timings BT656/1120",
1450 VPIF_MAX_NAME);
1451 std_info->width = bt->width;
1452 std_info->height = bt->height;
1453 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1454 std_info->ycmux_mode = 0;
1455 std_info->capture_format = 0;
1456 std_info->vbi_supported = 0;
1457 std_info->hd_sd = 1;
1458 std_info->stdid = 0;
1459 std_info->dv_preset = V4L2_DV_INVALID;
1461 vid_ch->stdid = 0;
1462 vid_ch->dv_preset = V4L2_DV_INVALID;
1464 return 0;
1468 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1469 * @file: file ptr
1470 * @priv: file handle
1471 * @timings: digital video timings
1473 static int vpif_g_dv_timings(struct file *file, void *priv,
1474 struct v4l2_dv_timings *timings)
1476 struct vpif_fh *fh = priv;
1477 struct channel_obj *ch = fh->channel;
1478 struct video_obj *vid_ch = &ch->video;
1479 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1481 timings->bt = *bt;
1483 return 0;
1487 * vpif_g_chip_ident() - Identify the chip
1488 * @file: file ptr
1489 * @priv: file handle
1490 * @chip: chip identity
1492 * Returns zero or -EINVAL if read operations fails.
1494 static int vpif_g_chip_ident(struct file *file, void *priv,
1495 struct v4l2_dbg_chip_ident *chip)
1497 chip->ident = V4L2_IDENT_NONE;
1498 chip->revision = 0;
1499 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1500 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1501 vpif_dbg(2, debug, "match_type is invalid.\n");
1502 return -EINVAL;
1505 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1506 g_chip_ident, chip);
1509 #ifdef CONFIG_VIDEO_ADV_DEBUG
1511 * vpif_dbg_g_register() - Read register
1512 * @file: file ptr
1513 * @priv: file handle
1514 * @reg: register to be read
1516 * Debugging only
1517 * Returns zero or -EINVAL if read operations fails.
1519 static int vpif_dbg_g_register(struct file *file, void *priv,
1520 struct v4l2_dbg_register *reg){
1521 struct vpif_fh *fh = priv;
1522 struct channel_obj *ch = fh->channel;
1523 struct video_obj *vid_ch = &ch->video;
1525 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1526 g_register, reg);
1530 * vpif_dbg_s_register() - Write to register
1531 * @file: file ptr
1532 * @priv: file handle
1533 * @reg: register to be modified
1535 * Debugging only
1536 * Returns zero or -EINVAL if write operations fails.
1538 static int vpif_dbg_s_register(struct file *file, void *priv,
1539 struct v4l2_dbg_register *reg){
1540 struct vpif_fh *fh = priv;
1541 struct channel_obj *ch = fh->channel;
1542 struct video_obj *vid_ch = &ch->video;
1544 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1545 s_register, reg);
1547 #endif
1550 * vpif_log_status() - Status information
1551 * @file: file ptr
1552 * @priv: file handle
1554 * Returns zero.
1556 static int vpif_log_status(struct file *filep, void *priv)
1558 /* status for sub devices */
1559 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1561 return 0;
1564 /* vpif display ioctl operations */
1565 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1566 .vidioc_querycap = vpif_querycap,
1567 .vidioc_g_priority = vpif_g_priority,
1568 .vidioc_s_priority = vpif_s_priority,
1569 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1570 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1571 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1572 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1573 .vidioc_reqbufs = vpif_reqbufs,
1574 .vidioc_querybuf = vpif_querybuf,
1575 .vidioc_qbuf = vpif_qbuf,
1576 .vidioc_dqbuf = vpif_dqbuf,
1577 .vidioc_streamon = vpif_streamon,
1578 .vidioc_streamoff = vpif_streamoff,
1579 .vidioc_s_std = vpif_s_std,
1580 .vidioc_g_std = vpif_g_std,
1581 .vidioc_enum_output = vpif_enum_output,
1582 .vidioc_s_output = vpif_s_output,
1583 .vidioc_g_output = vpif_g_output,
1584 .vidioc_cropcap = vpif_cropcap,
1585 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
1586 .vidioc_s_dv_preset = vpif_s_dv_preset,
1587 .vidioc_g_dv_preset = vpif_g_dv_preset,
1588 .vidioc_s_dv_timings = vpif_s_dv_timings,
1589 .vidioc_g_dv_timings = vpif_g_dv_timings,
1590 .vidioc_g_chip_ident = vpif_g_chip_ident,
1591 #ifdef CONFIG_VIDEO_ADV_DEBUG
1592 .vidioc_g_register = vpif_dbg_g_register,
1593 .vidioc_s_register = vpif_dbg_s_register,
1594 #endif
1595 .vidioc_log_status = vpif_log_status,
1598 static const struct v4l2_file_operations vpif_fops = {
1599 .owner = THIS_MODULE,
1600 .open = vpif_open,
1601 .release = vpif_release,
1602 .unlocked_ioctl = video_ioctl2,
1603 .mmap = vpif_mmap,
1604 .poll = vpif_poll
1607 static struct video_device vpif_video_template = {
1608 .name = "vpif",
1609 .fops = &vpif_fops,
1610 .ioctl_ops = &vpif_ioctl_ops,
1611 .tvnorms = DM646X_V4L2_STD,
1612 .current_norm = V4L2_STD_625_50,
1616 /*Configure the channels, buffer sizei, request irq */
1617 static int initialize_vpif(void)
1619 int free_channel_objects_index;
1620 int free_buffer_channel_index;
1621 int free_buffer_index;
1622 int err = 0, i, j;
1624 /* Default number of buffers should be 3 */
1625 if ((ch2_numbuffers > 0) &&
1626 (ch2_numbuffers < config_params.min_numbuffers))
1627 ch2_numbuffers = config_params.min_numbuffers;
1628 if ((ch3_numbuffers > 0) &&
1629 (ch3_numbuffers < config_params.min_numbuffers))
1630 ch3_numbuffers = config_params.min_numbuffers;
1632 /* Set buffer size to min buffers size if invalid buffer size is
1633 * given */
1634 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1635 ch2_bufsize =
1636 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1637 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1638 ch3_bufsize =
1639 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1641 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1643 if (ch2_numbuffers) {
1644 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1645 ch2_bufsize;
1647 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1649 if (ch3_numbuffers) {
1650 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1651 ch3_bufsize;
1654 /* Allocate memory for six channel objects */
1655 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1656 vpif_obj.dev[i] =
1657 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1658 /* If memory allocation fails, return error */
1659 if (!vpif_obj.dev[i]) {
1660 free_channel_objects_index = i;
1661 err = -ENOMEM;
1662 goto vpif_init_free_channel_objects;
1666 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1667 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1668 free_buffer_index = config_params.numbuffers[i - 1];
1670 return 0;
1672 vpif_init_free_channel_objects:
1673 for (j = 0; j < free_channel_objects_index; j++)
1674 kfree(vpif_obj.dev[j]);
1675 return err;
1679 * vpif_probe: This function creates device entries by register itself to the
1680 * V4L2 driver and initializes fields of each channel objects
1682 static __init int vpif_probe(struct platform_device *pdev)
1684 struct vpif_subdev_info *subdevdata;
1685 struct vpif_display_config *config;
1686 int i, j = 0, k, q, m, err = 0;
1687 struct i2c_adapter *i2c_adap;
1688 struct common_obj *common;
1689 struct channel_obj *ch;
1690 struct video_device *vfd;
1691 struct resource *res;
1692 int subdev_count;
1694 vpif_dev = &pdev->dev;
1696 err = initialize_vpif();
1698 if (err) {
1699 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1700 return err;
1703 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1704 if (err) {
1705 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1706 return err;
1709 k = 0;
1710 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1711 for (i = res->start; i <= res->end; i++) {
1712 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1713 "DM646x_Display",
1714 (void *)(&vpif_obj.dev[k]->channel_id))) {
1715 err = -EBUSY;
1716 goto vpif_int_err;
1719 k++;
1722 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1724 /* Get the pointer to the channel object */
1725 ch = vpif_obj.dev[i];
1727 /* Allocate memory for video device */
1728 vfd = video_device_alloc();
1729 if (vfd == NULL) {
1730 for (j = 0; j < i; j++) {
1731 ch = vpif_obj.dev[j];
1732 video_device_release(ch->video_dev);
1734 err = -ENOMEM;
1735 goto vpif_int_err;
1738 /* Initialize field of video device */
1739 *vfd = vpif_video_template;
1740 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1741 vfd->release = video_device_release;
1742 snprintf(vfd->name, sizeof(vfd->name),
1743 "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
1744 (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
1745 (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
1746 (VPIF_DISPLAY_VERSION_CODE) & 0xff);
1748 /* Set video_dev to the video device */
1749 ch->video_dev = vfd;
1752 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1753 ch = vpif_obj.dev[j];
1754 /* Initialize field of the channel objects */
1755 atomic_set(&ch->usrs, 0);
1756 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1757 ch->common[k].numbuffers = 0;
1758 common = &ch->common[k];
1759 common->io_usrs = 0;
1760 common->started = 0;
1761 spin_lock_init(&common->irqlock);
1762 mutex_init(&common->lock);
1763 common->numbuffers = 0;
1764 common->set_addr = NULL;
1765 common->ytop_off = common->ybtm_off = 0;
1766 common->ctop_off = common->cbtm_off = 0;
1767 common->cur_frm = common->next_frm = NULL;
1768 memset(&common->fmt, 0, sizeof(common->fmt));
1769 common->numbuffers = config_params.numbuffers[k];
1772 ch->initialized = 0;
1773 ch->channel_id = j;
1774 if (j < 2)
1775 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1776 config_params.numbuffers[ch->channel_id];
1777 else
1778 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1780 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1782 /* Initialize prio member of channel object */
1783 v4l2_prio_init(&ch->prio);
1784 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1785 V4L2_BUF_TYPE_VIDEO_OUTPUT;
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));
1794 if (err < 0)
1795 goto probe_out;
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,
1805 GFP_KERNEL);
1806 if (vpif_obj.sd == NULL) {
1807 vpif_err("unable to allocate memory for subdevice pointers\n");
1808 err = -ENOMEM;
1809 goto probe_out;
1812 for (i = 0; i < subdev_count; i++) {
1813 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1814 i2c_adap,
1815 &subdevdata[i].board_info,
1816 NULL);
1817 if (!vpif_obj.sd[i]) {
1818 vpif_err("Error registering v4l2 subdevice\n");
1819 goto probe_subdev_out;
1822 if (vpif_obj.sd[i])
1823 vpif_obj.sd[i]->grp_id = 1 << i;
1826 v4l2_info(&vpif_obj.v4l2_dev,
1827 "DM646x VPIF display driver initialized\n");
1828 return 0;
1830 probe_subdev_out:
1831 kfree(vpif_obj.sd);
1832 probe_out:
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;
1839 vpif_int_err:
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);
1846 m = res->end;
1849 return err;
1853 * vpif_remove: It un-register channels from V4L2 driver
1855 static int vpif_remove(struct platform_device *device)
1857 struct channel_obj *ch;
1858 int i;
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;
1872 return 0;
1875 static __refdata struct platform_driver vpif_driver = {
1876 .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
1892 * objects.
1894 static void vpif_cleanup(void)
1896 struct platform_device *pdev;
1897 struct resource *res;
1898 int irq_num;
1899 int i = 0;
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++)
1905 free_irq(irq_num,
1906 (void *)(&vpif_obj.dev[i]->channel_id));
1907 i++;
1910 platform_driver_unregister(&vpif_driver);
1911 kfree(vpif_obj.sd);
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);