2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
11 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/string.h>
26 #include <linux/videodev2.h>
27 #include <linux/wait.h>
28 #include <linux/time.h>
29 #include <linux/i2c.h>
30 #include <linux/platform_device.h>
32 #include <linux/version.h>
33 #include <linux/slab.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"
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)
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 = {
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);
107 /* otherwise, use get_user_pages() for general userland pages */
108 int res, nr_pages = 1;
110 down_read(¤t->mm->mmap_sem);
112 res = get_user_pages(current, current->mm,
113 virtp, nr_pages, 1, 0, &pages, NULL);
114 up_read(¤t->mm->mmap_sem);
116 if (res == nr_pages) {
117 physp = __pa(page_address(&pages[0]) +
118 (virtp & ~PAGE_MASK));
120 vpif_err("get_user_pages failed\n");
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;
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;
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) {
154 vpif_err("buffer_address is 0\n");
158 vb->boff = vpif_uservirt_to_phys(vb->baddr);
159 if (!ISALIGNED(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))
174 vpif_err("buffer offset not aligned to 8 bytes\n");
179 * vpif_buffer_setup: This function allocates memory for the buffers
181 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
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)
191 *size = config_params.channel_bufsize[ch->channel_id];
192 if (*count < config_params.min_numbuffers)
193 *count = config_params.min_numbuffers;
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)
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 */
270 if (common->cur_frm == common->next_frm)
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)) {
289 /* one field is displayed configure the next
290 * frame if it is available else hold on current
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;
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)
320 if (1 == ch->vpifparams.std_info.frm_fmt) {
321 if (list_empty(&common->dma_queue))
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);
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;
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 */
359 process_interlaced_mode(fid, common);
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;
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));
384 vpif_dbg(2, debug, "HD format\n");
385 if (config->dv_preset == vid_ch->dv_preset) {
386 memcpy(std_info, config, sizeof(*config));
392 if (i == vpif_ch_params_count) {
393 vpif_dbg(1, debug, "Format not found\n");
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)
410 if (vid_ch->stdid || vid_ch->dv_preset) {
411 if (vpif_update_std_info(ch))
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;
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;
443 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
445 vid_ch->buf_field = common->fmt.fmt.pix.field;
448 if (V4L2_MEMORY_USERPTR == common->memory)
449 sizeimage = common->fmt.fmt.pix.sizeimage;
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;
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;
484 if ((field == V4L2_FIELD_ANY) ||
485 (field == V4L2_FIELD_INTERLACED))
486 vpifparams->video_params.hpitch =
487 common->fmt.fmt.pix.bytesperline * 2;
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;
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;
531 sizeimage = config_params.channel_bufsize[ch->channel_id];
533 if (vpif_update_resolution(ch))
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");
549 pixfmt->width = common->fmt.fmt.pix.width;
550 pixfmt->height = common->fmt.fmt.pix.height;
555 vpif_err("invalid field format\n");
559 vpif_err("invalid pitch\n");
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;
571 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
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];
601 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
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);
620 vpif_err("unable to allocate memory for file handle object\n");
624 /* store pointer to fh in private_data member of filep */
625 filep->private_data = fh;
628 if (!ch->initialized) {
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);
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 (mutex_lock_interruptible(&common->lock))
658 /* if this instance is doing IO */
659 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
660 /* Reset io_usrs member of channel object */
662 /* Disable channel */
663 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
665 channel2_intr_enable(0);
667 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
668 (2 == common->started)) {
670 channel3_intr_enable(0);
673 /* Free buffers allocated */
674 videobuf_queue_cancel(&common->buffer_queue);
675 videobuf_mmap_free(&common->buffer_queue);
677 config_params.numbuffers[ch->channel_id];
680 mutex_unlock(&common->lock);
682 /* Decrement channel usrs counter */
683 atomic_dec(&ch->usrs);
684 /* If this file handle has initialize encoder device, reset it */
688 /* Close the priority */
689 v4l2_prio_close(&ch->prio, fh->prio);
690 filep->private_data = NULL;
697 /* functions implementing ioctls */
699 * vpif_querycap() - QUERYCAP handler
702 * @cap: ptr to v4l2_capability structure
704 static int vpif_querycap(struct file *file, void *priv,
705 struct v4l2_capability *cap)
707 struct vpif_display_config *config = vpif_dev->platform_data;
709 cap->version = VPIF_DISPLAY_VERSION_CODE;
710 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
711 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
712 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
713 strlcpy(cap->card, config->card_name, sizeof(cap->card));
718 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
719 struct v4l2_fmtdesc *fmt)
721 if (fmt->index != 0) {
722 vpif_err("Invalid format index\n");
726 /* Fill in the information about format */
727 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
728 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
729 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
734 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
735 struct v4l2_format *fmt)
737 struct vpif_fh *fh = priv;
738 struct channel_obj *ch = fh->channel;
739 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
742 /* Check the validity of the buffer type */
743 if (common->fmt.type != fmt->type)
746 /* Fill in the information about format */
747 if (mutex_lock_interruptible(&common->lock))
750 if (vpif_update_resolution(ch))
755 mutex_unlock(&common->lock);
760 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
761 struct v4l2_format *fmt)
763 struct vpif_fh *fh = priv;
764 struct v4l2_pix_format *pixfmt;
765 struct channel_obj *ch = fh->channel;
766 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
769 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
770 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
771 if (!fh->initialized) {
772 vpif_dbg(1, debug, "Channel Busy\n");
776 /* Check for the priority */
777 ret = v4l2_prio_check(&ch->prio, fh->prio);
783 if (common->started) {
784 vpif_dbg(1, debug, "Streaming in progress\n");
788 pixfmt = &fmt->fmt.pix;
789 /* Check for valid field format */
790 ret = vpif_check_format(ch, pixfmt);
794 /* store the pix format in the channel object */
795 common->fmt.fmt.pix = *pixfmt;
796 /* store the format in the channel object */
797 if (mutex_lock_interruptible(&common->lock))
801 mutex_unlock(&common->lock);
806 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
807 struct v4l2_format *fmt)
809 struct vpif_fh *fh = priv;
810 struct channel_obj *ch = fh->channel;
811 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
812 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
815 ret = vpif_check_format(ch, pixfmt);
817 *pixfmt = common->fmt.fmt.pix;
818 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
824 static int vpif_reqbufs(struct file *file, void *priv,
825 struct v4l2_requestbuffers *reqbuf)
827 struct vpif_fh *fh = priv;
828 struct channel_obj *ch = fh->channel;
829 struct common_obj *common;
830 enum v4l2_field field;
834 /* This file handle has not initialized the channel,
835 It is not allowed to do settings */
836 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
837 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
838 if (!fh->initialized) {
839 vpif_err("Channel Busy\n");
844 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
847 index = VPIF_VIDEO_INDEX;
849 common = &ch->common[index];
850 if (mutex_lock_interruptible(&common->lock))
853 if (common->fmt.type != reqbuf->type) {
858 if (0 != common->io_usrs) {
863 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
864 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
865 field = V4L2_FIELD_INTERLACED;
867 field = common->fmt.fmt.pix.field;
869 field = V4L2_VBI_INTERLACED;
872 /* Initialize videobuf queue as per the buffer type */
873 videobuf_queue_dma_contig_init(&common->buffer_queue,
877 sizeof(struct videobuf_buffer), fh,
880 /* Set io allowed member of file handle to TRUE */
881 fh->io_allowed[index] = 1;
882 /* Increment io usrs member of channel object to 1 */
884 /* Store type of memory requested in channel object */
885 common->memory = reqbuf->memory;
886 INIT_LIST_HEAD(&common->dma_queue);
888 /* Allocate buffers */
889 ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
892 mutex_unlock(&common->lock);
896 static int vpif_querybuf(struct file *file, void *priv,
897 struct v4l2_buffer *tbuf)
899 struct vpif_fh *fh = priv;
900 struct channel_obj *ch = fh->channel;
901 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
903 if (common->fmt.type != tbuf->type)
906 return videobuf_querybuf(&common->buffer_queue, tbuf);
909 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
912 struct vpif_fh *fh = priv;
913 struct channel_obj *ch = fh->channel;
914 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
915 struct v4l2_buffer tbuf = *buf;
916 struct videobuf_buffer *buf1;
917 unsigned long addr = 0;
921 if (common->fmt.type != tbuf.type)
924 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
925 vpif_err("fh->io_allowed\n");
929 if (!(list_empty(&common->dma_queue)) ||
930 (common->cur_frm != common->next_frm) ||
931 !(common->started) ||
932 (common->started && (0 == ch->field_id)))
933 return videobuf_qbuf(&common->buffer_queue, buf);
935 /* bufferqueue is empty store buffer address in VPIF registers */
936 mutex_lock(&common->buffer_queue.vb_lock);
937 buf1 = common->buffer_queue.bufs[tbuf.index];
938 if (buf1->memory != tbuf.memory) {
939 vpif_err("invalid buffer type\n");
943 if ((buf1->state == VIDEOBUF_QUEUED) ||
944 (buf1->state == VIDEOBUF_ACTIVE)) {
945 vpif_err("invalid state\n");
949 switch (buf1->memory) {
950 case V4L2_MEMORY_MMAP:
951 if (buf1->baddr == 0)
955 case V4L2_MEMORY_USERPTR:
956 if (tbuf.length < buf1->bsize)
959 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
960 && (buf1->baddr != tbuf.m.userptr)) {
961 vpif_buffer_release(&common->buffer_queue, buf1);
962 buf1->baddr = tbuf.m.userptr;
970 local_irq_save(flags);
971 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
972 common->buffer_queue.field);
974 local_irq_restore(flags);
978 buf1->state = VIDEOBUF_ACTIVE;
980 common->next_frm = buf1;
981 if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
982 common->set_addr((addr + common->ytop_off),
983 (addr + common->ybtm_off),
984 (addr + common->ctop_off),
985 (addr + common->cbtm_off));
988 local_irq_restore(flags);
989 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
990 mutex_unlock(&common->buffer_queue.vb_lock);
994 mutex_unlock(&common->buffer_queue.vb_lock);
998 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1000 struct vpif_fh *fh = priv;
1001 struct channel_obj *ch = fh->channel;
1002 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1005 if (!(*std_id & DM646X_V4L2_STD))
1008 if (common->started) {
1009 vpif_err("streaming in progress\n");
1013 /* Call encoder subdevice function to set the standard */
1014 if (mutex_lock_interruptible(&common->lock))
1015 return -ERESTARTSYS;
1017 ch->video.stdid = *std_id;
1018 ch->video.dv_preset = V4L2_DV_INVALID;
1019 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1021 /* Get the information about the standard */
1022 if (vpif_update_resolution(ch)) {
1027 if ((ch->vpifparams.std_info.width *
1028 ch->vpifparams.std_info.height * 2) >
1029 config_params.channel_bufsize[ch->channel_id]) {
1030 vpif_err("invalid std for this size\n");
1035 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1036 /* Configure the default format information */
1037 vpif_config_format(ch);
1039 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1040 s_std_output, *std_id);
1042 vpif_err("Failed to set output standard\n");
1046 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1049 vpif_err("Failed to set standard for sub devices\n");
1052 mutex_unlock(&common->lock);
1056 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1058 struct vpif_fh *fh = priv;
1059 struct channel_obj *ch = fh->channel;
1061 *std = ch->video.stdid;
1065 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1067 struct vpif_fh *fh = priv;
1068 struct channel_obj *ch = fh->channel;
1069 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1071 return videobuf_dqbuf(&common->buffer_queue, p,
1072 (file->f_flags & O_NONBLOCK));
1075 static int vpif_streamon(struct file *file, void *priv,
1076 enum v4l2_buf_type buftype)
1078 struct vpif_fh *fh = priv;
1079 struct channel_obj *ch = fh->channel;
1080 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1081 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1082 struct vpif_params *vpif = &ch->vpifparams;
1083 struct vpif_display_config *vpif_config_data =
1084 vpif_dev->platform_data;
1085 unsigned long addr = 0;
1088 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1089 vpif_err("buffer type not supported\n");
1093 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1094 vpif_err("fh->io_allowed\n");
1098 /* If Streaming is already started, return error */
1099 if (common->started) {
1100 vpif_err("channel->started\n");
1104 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1105 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1106 ch->vpifparams.std_info.ycmux_mode == 0)
1107 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1108 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1109 vpif_err("other channel is using\n");
1113 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1117 /* Call videobuf_streamon to start streaming in videobuf */
1118 ret = videobuf_streamon(&common->buffer_queue);
1120 vpif_err("videobuf_streamon\n");
1124 if (mutex_lock_interruptible(&common->lock))
1125 return -ERESTARTSYS;
1127 /* If buffer queue is empty, return error */
1128 if (list_empty(&common->dma_queue)) {
1129 vpif_err("buffer queue is empty\n");
1134 /* Get the next frame from the buffer queue */
1135 common->next_frm = common->cur_frm =
1136 list_entry(common->dma_queue.next,
1137 struct videobuf_buffer, queue);
1139 list_del(&common->cur_frm->queue);
1140 /* Mark state of the current frame to active */
1141 common->cur_frm->state = VIDEOBUF_ACTIVE;
1143 /* Initialize field_id and started member */
1145 common->started = 1;
1146 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1147 addr = common->cur_frm->boff;
1148 /* Calculate the offset for Y and C data in the buffer */
1149 vpif_calculate_offsets(ch);
1151 if ((ch->vpifparams.std_info.frm_fmt &&
1152 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1153 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1154 || (!ch->vpifparams.std_info.frm_fmt
1155 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1156 vpif_err("conflict in field format and std format\n");
1161 /* clock settings */
1163 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1164 ch->vpifparams.std_info.hd_sd);
1166 vpif_err("can't set clock\n");
1170 /* set the parameters and addresses */
1171 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1175 common->started = ret;
1176 vpif_config_addr(ch, ret);
1177 common->set_addr((addr + common->ytop_off),
1178 (addr + common->ybtm_off),
1179 (addr + common->ctop_off),
1180 (addr + common->cbtm_off));
1182 /* Set interrupt for both the fields in VPIF
1183 Register enable channel in VPIF register */
1184 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1185 channel2_intr_assert();
1186 channel2_intr_enable(1);
1190 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1191 || (common->started == 2)) {
1192 channel3_intr_assert();
1193 channel3_intr_enable(1);
1196 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1200 mutex_unlock(&common->lock);
1204 static int vpif_streamoff(struct file *file, void *priv,
1205 enum v4l2_buf_type buftype)
1207 struct vpif_fh *fh = priv;
1208 struct channel_obj *ch = fh->channel;
1209 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1211 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1212 vpif_err("buffer type not supported\n");
1216 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1217 vpif_err("fh->io_allowed\n");
1221 if (!common->started) {
1222 vpif_err("channel->started\n");
1226 if (mutex_lock_interruptible(&common->lock))
1227 return -ERESTARTSYS;
1229 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1230 /* disable channel */
1231 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1233 channel2_intr_enable(0);
1235 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1236 (2 == common->started)) {
1238 channel3_intr_enable(0);
1242 common->started = 0;
1243 mutex_unlock(&common->lock);
1245 return videobuf_streamoff(&common->buffer_queue);
1248 static int vpif_cropcap(struct file *file, void *priv,
1249 struct v4l2_cropcap *crop)
1251 struct vpif_fh *fh = priv;
1252 struct channel_obj *ch = fh->channel;
1253 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1254 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1257 crop->bounds.left = crop->bounds.top = 0;
1258 crop->defrect.left = crop->defrect.top = 0;
1259 crop->defrect.height = crop->bounds.height = common->height;
1260 crop->defrect.width = crop->bounds.width = common->width;
1265 static int vpif_enum_output(struct file *file, void *fh,
1266 struct v4l2_output *output)
1269 struct vpif_display_config *config = vpif_dev->platform_data;
1271 if (output->index >= config->output_count) {
1272 vpif_dbg(1, debug, "Invalid output index\n");
1276 strcpy(output->name, config->output[output->index]);
1277 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1278 output->std = DM646X_V4L2_STD;
1283 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1285 struct vpif_fh *fh = priv;
1286 struct channel_obj *ch = fh->channel;
1287 struct video_obj *vid_ch = &ch->video;
1288 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1291 if (mutex_lock_interruptible(&common->lock))
1292 return -ERESTARTSYS;
1294 if (common->started) {
1295 vpif_err("Streaming in progress\n");
1300 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1301 s_routing, 0, i, 0);
1304 vpif_err("Failed to set output standard\n");
1306 vid_ch->output_id = i;
1309 mutex_unlock(&common->lock);
1313 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1315 struct vpif_fh *fh = priv;
1316 struct channel_obj *ch = fh->channel;
1317 struct video_obj *vid_ch = &ch->video;
1319 *i = vid_ch->output_id;
1324 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1326 struct vpif_fh *fh = priv;
1327 struct channel_obj *ch = fh->channel;
1329 *p = v4l2_prio_max(&ch->prio);
1334 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1336 struct vpif_fh *fh = priv;
1337 struct channel_obj *ch = fh->channel;
1339 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1343 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1345 * @priv: file handle
1346 * @preset: input preset
1348 static int vpif_enum_dv_presets(struct file *file, void *priv,
1349 struct v4l2_dv_enum_preset *preset)
1351 struct vpif_fh *fh = priv;
1352 struct channel_obj *ch = fh->channel;
1353 struct video_obj *vid_ch = &ch->video;
1355 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1356 video, enum_dv_presets, preset);
1360 * vpif_s_dv_presets() - S_DV_PRESETS handler
1362 * @priv: file handle
1363 * @preset: input preset
1365 static int vpif_s_dv_preset(struct file *file, void *priv,
1366 struct v4l2_dv_preset *preset)
1368 struct vpif_fh *fh = priv;
1369 struct channel_obj *ch = fh->channel;
1370 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1371 struct video_obj *vid_ch = &ch->video;
1374 if (common->started) {
1375 vpif_dbg(1, debug, "streaming in progress\n");
1379 ret = v4l2_prio_check(&ch->prio, fh->prio);
1383 fh->initialized = 1;
1385 /* Call encoder subdevice function to set the standard */
1386 if (mutex_lock_interruptible(&common->lock))
1387 return -ERESTARTSYS;
1389 ch->video.dv_preset = preset->preset;
1390 ch->video.stdid = V4L2_STD_UNKNOWN;
1391 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1393 /* Get the information about the standard */
1394 if (vpif_update_resolution(ch)) {
1397 /* Configure the default format information */
1398 vpif_config_format(ch);
1400 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1401 video, s_dv_preset, preset);
1404 mutex_unlock(&common->lock);
1409 * vpif_g_dv_presets() - G_DV_PRESETS handler
1411 * @priv: file handle
1412 * @preset: input preset
1414 static int vpif_g_dv_preset(struct file *file, void *priv,
1415 struct v4l2_dv_preset *preset)
1417 struct vpif_fh *fh = priv;
1418 struct channel_obj *ch = fh->channel;
1420 preset->preset = ch->video.dv_preset;
1425 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1427 * @priv: file handle
1428 * @timings: digital video timings
1430 static int vpif_s_dv_timings(struct file *file, void *priv,
1431 struct v4l2_dv_timings *timings)
1433 struct vpif_fh *fh = priv;
1434 struct channel_obj *ch = fh->channel;
1435 struct vpif_params *vpifparams = &ch->vpifparams;
1436 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1437 struct video_obj *vid_ch = &ch->video;
1438 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1441 if (timings->type != V4L2_DV_BT_656_1120) {
1442 vpif_dbg(2, debug, "Timing type not defined\n");
1446 /* Configure subdevice timings, if any */
1447 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1448 video, s_dv_timings, timings);
1449 if (ret == -ENOIOCTLCMD) {
1450 vpif_dbg(2, debug, "Custom DV timings not supported by "
1455 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1459 if (!(timings->bt.width && timings->bt.height &&
1460 (timings->bt.hbackporch ||
1461 timings->bt.hfrontporch ||
1462 timings->bt.hsync) &&
1463 timings->bt.vfrontporch &&
1464 (timings->bt.vbackporch ||
1465 timings->bt.vsync))) {
1466 vpif_dbg(2, debug, "Timings for width, height, "
1467 "horizontal back porch, horizontal sync, "
1468 "horizontal front porch, vertical back porch, "
1469 "vertical sync and vertical back porch "
1470 "must be defined\n");
1476 /* Configure video port timings */
1478 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1480 std_info->sav2eav = bt->width;
1483 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1485 if (bt->interlaced) {
1486 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1487 std_info->vsize = bt->height * 2 +
1488 bt->vfrontporch + bt->vsync + bt->vbackporch +
1489 bt->il_vfrontporch + bt->il_vsync +
1491 std_info->l5 = std_info->vsize/2 -
1492 (bt->vfrontporch - 1);
1493 std_info->l7 = std_info->vsize/2 + 1;
1494 std_info->l9 = std_info->l7 + bt->il_vsync +
1495 bt->il_vbackporch + 1;
1496 std_info->l11 = std_info->vsize -
1497 (bt->il_vfrontporch - 1);
1499 vpif_dbg(2, debug, "Required timing values for "
1500 "interlaced BT format missing\n");
1504 std_info->vsize = bt->height + bt->vfrontporch +
1505 bt->vsync + bt->vbackporch;
1506 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1508 strncpy(std_info->name, "Custom timings BT656/1120",
1510 std_info->width = bt->width;
1511 std_info->height = bt->height;
1512 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1513 std_info->ycmux_mode = 0;
1514 std_info->capture_format = 0;
1515 std_info->vbi_supported = 0;
1516 std_info->hd_sd = 1;
1517 std_info->stdid = 0;
1518 std_info->dv_preset = V4L2_DV_INVALID;
1521 vid_ch->dv_preset = V4L2_DV_INVALID;
1527 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1529 * @priv: file handle
1530 * @timings: digital video timings
1532 static int vpif_g_dv_timings(struct file *file, void *priv,
1533 struct v4l2_dv_timings *timings)
1535 struct vpif_fh *fh = priv;
1536 struct channel_obj *ch = fh->channel;
1537 struct video_obj *vid_ch = &ch->video;
1538 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1546 * vpif_g_chip_ident() - Identify the chip
1548 * @priv: file handle
1549 * @chip: chip identity
1551 * Returns zero or -EINVAL if read operations fails.
1553 static int vpif_g_chip_ident(struct file *file, void *priv,
1554 struct v4l2_dbg_chip_ident *chip)
1556 chip->ident = V4L2_IDENT_NONE;
1558 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1559 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1560 vpif_dbg(2, debug, "match_type is invalid.\n");
1564 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1565 g_chip_ident, chip);
1568 #ifdef CONFIG_VIDEO_ADV_DEBUG
1570 * vpif_dbg_g_register() - Read register
1572 * @priv: file handle
1573 * @reg: register to be read
1576 * Returns zero or -EINVAL if read operations fails.
1578 static int vpif_dbg_g_register(struct file *file, void *priv,
1579 struct v4l2_dbg_register *reg){
1580 struct vpif_fh *fh = priv;
1581 struct channel_obj *ch = fh->channel;
1582 struct video_obj *vid_ch = &ch->video;
1584 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1589 * vpif_dbg_s_register() - Write to register
1591 * @priv: file handle
1592 * @reg: register to be modified
1595 * Returns zero or -EINVAL if write operations fails.
1597 static int vpif_dbg_s_register(struct file *file, void *priv,
1598 struct v4l2_dbg_register *reg){
1599 struct vpif_fh *fh = priv;
1600 struct channel_obj *ch = fh->channel;
1601 struct video_obj *vid_ch = &ch->video;
1603 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1609 * vpif_log_status() - Status information
1611 * @priv: file handle
1615 static int vpif_log_status(struct file *filep, void *priv)
1617 /* status for sub devices */
1618 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1623 /* vpif display ioctl operations */
1624 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1625 .vidioc_querycap = vpif_querycap,
1626 .vidioc_g_priority = vpif_g_priority,
1627 .vidioc_s_priority = vpif_s_priority,
1628 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1629 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1630 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1631 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1632 .vidioc_reqbufs = vpif_reqbufs,
1633 .vidioc_querybuf = vpif_querybuf,
1634 .vidioc_qbuf = vpif_qbuf,
1635 .vidioc_dqbuf = vpif_dqbuf,
1636 .vidioc_streamon = vpif_streamon,
1637 .vidioc_streamoff = vpif_streamoff,
1638 .vidioc_s_std = vpif_s_std,
1639 .vidioc_g_std = vpif_g_std,
1640 .vidioc_enum_output = vpif_enum_output,
1641 .vidioc_s_output = vpif_s_output,
1642 .vidioc_g_output = vpif_g_output,
1643 .vidioc_cropcap = vpif_cropcap,
1644 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
1645 .vidioc_s_dv_preset = vpif_s_dv_preset,
1646 .vidioc_g_dv_preset = vpif_g_dv_preset,
1647 .vidioc_s_dv_timings = vpif_s_dv_timings,
1648 .vidioc_g_dv_timings = vpif_g_dv_timings,
1649 .vidioc_g_chip_ident = vpif_g_chip_ident,
1650 #ifdef CONFIG_VIDEO_ADV_DEBUG
1651 .vidioc_g_register = vpif_dbg_g_register,
1652 .vidioc_s_register = vpif_dbg_s_register,
1654 .vidioc_log_status = vpif_log_status,
1657 static const struct v4l2_file_operations vpif_fops = {
1658 .owner = THIS_MODULE,
1660 .release = vpif_release,
1661 .ioctl = video_ioctl2,
1666 static struct video_device vpif_video_template = {
1669 .ioctl_ops = &vpif_ioctl_ops,
1670 .tvnorms = DM646X_V4L2_STD,
1671 .current_norm = V4L2_STD_625_50,
1675 /*Configure the channels, buffer sizei, request irq */
1676 static int initialize_vpif(void)
1678 int free_channel_objects_index;
1679 int free_buffer_channel_index;
1680 int free_buffer_index;
1683 /* Default number of buffers should be 3 */
1684 if ((ch2_numbuffers > 0) &&
1685 (ch2_numbuffers < config_params.min_numbuffers))
1686 ch2_numbuffers = config_params.min_numbuffers;
1687 if ((ch3_numbuffers > 0) &&
1688 (ch3_numbuffers < config_params.min_numbuffers))
1689 ch3_numbuffers = config_params.min_numbuffers;
1691 /* Set buffer size to min buffers size if invalid buffer size is
1693 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1695 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1696 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1698 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1700 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1702 if (ch2_numbuffers) {
1703 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1706 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1708 if (ch3_numbuffers) {
1709 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1713 /* Allocate memory for six channel objects */
1714 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1716 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1717 /* If memory allocation fails, return error */
1718 if (!vpif_obj.dev[i]) {
1719 free_channel_objects_index = i;
1721 goto vpif_init_free_channel_objects;
1725 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1726 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1727 free_buffer_index = config_params.numbuffers[i - 1];
1731 vpif_init_free_channel_objects:
1732 for (j = 0; j < free_channel_objects_index; j++)
1733 kfree(vpif_obj.dev[j]);
1738 * vpif_probe: This function creates device entries by register itself to the
1739 * V4L2 driver and initializes fields of each channel objects
1741 static __init int vpif_probe(struct platform_device *pdev)
1743 struct vpif_subdev_info *subdevdata;
1744 struct vpif_display_config *config;
1745 int i, j = 0, k, q, m, err = 0;
1746 struct i2c_adapter *i2c_adap;
1747 struct common_obj *common;
1748 struct channel_obj *ch;
1749 struct video_device *vfd;
1750 struct resource *res;
1753 vpif_dev = &pdev->dev;
1755 err = initialize_vpif();
1758 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1762 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1764 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1769 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1770 for (i = res->start; i <= res->end; i++) {
1771 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1773 (void *)(&vpif_obj.dev[k]->channel_id))) {
1781 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1783 /* Get the pointer to the channel object */
1784 ch = vpif_obj.dev[i];
1786 /* Allocate memory for video device */
1787 vfd = video_device_alloc();
1789 for (j = 0; j < i; j++) {
1790 ch = vpif_obj.dev[j];
1791 video_device_release(ch->video_dev);
1797 /* Initialize field of video device */
1798 *vfd = vpif_video_template;
1799 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1800 vfd->release = video_device_release;
1801 snprintf(vfd->name, sizeof(vfd->name),
1802 "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
1803 (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
1804 (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
1805 (VPIF_DISPLAY_VERSION_CODE) & 0xff);
1807 /* Set video_dev to the video device */
1808 ch->video_dev = vfd;
1811 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1812 ch = vpif_obj.dev[j];
1813 /* Initialize field of the channel objects */
1814 atomic_set(&ch->usrs, 0);
1815 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1816 ch->common[k].numbuffers = 0;
1817 common = &ch->common[k];
1818 common->io_usrs = 0;
1819 common->started = 0;
1820 spin_lock_init(&common->irqlock);
1821 mutex_init(&common->lock);
1822 common->numbuffers = 0;
1823 common->set_addr = NULL;
1824 common->ytop_off = common->ybtm_off = 0;
1825 common->ctop_off = common->cbtm_off = 0;
1826 common->cur_frm = common->next_frm = NULL;
1827 memset(&common->fmt, 0, sizeof(common->fmt));
1828 common->numbuffers = config_params.numbuffers[k];
1831 ch->initialized = 0;
1834 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1835 config_params.numbuffers[ch->channel_id];
1837 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1839 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1841 /* Initialize prio member of channel object */
1842 v4l2_prio_init(&ch->prio);
1843 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1844 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1846 /* register video device */
1847 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1848 (int)ch, (int)&ch->video_dev);
1850 err = video_register_device(ch->video_dev,
1851 VFL_TYPE_GRABBER, (j ? 3 : 2));
1855 video_set_drvdata(ch->video_dev, ch);
1858 i2c_adap = i2c_get_adapter(1);
1859 config = pdev->dev.platform_data;
1860 subdev_count = config->subdev_count;
1861 subdevdata = config->subdevinfo;
1862 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1864 if (vpif_obj.sd == NULL) {
1865 vpif_err("unable to allocate memory for subdevice pointers\n");
1870 for (i = 0; i < subdev_count; i++) {
1871 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1873 &subdevdata[i].board_info,
1875 if (!vpif_obj.sd[i]) {
1876 vpif_err("Error registering v4l2 subdevice\n");
1877 goto probe_subdev_out;
1881 vpif_obj.sd[i]->grp_id = 1 << i;
1884 v4l2_info(&vpif_obj.v4l2_dev,
1885 "DM646x VPIF display driver initialized\n");
1891 for (k = 0; k < j; k++) {
1892 ch = vpif_obj.dev[k];
1893 video_unregister_device(ch->video_dev);
1894 video_device_release(ch->video_dev);
1895 ch->video_dev = NULL;
1898 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1899 vpif_err("VPIF IRQ request failed\n");
1900 for (q = k; k >= 0; k--) {
1901 for (m = i; m >= res->start; m--)
1902 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1903 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1911 * vpif_remove: It un-register channels from V4L2 driver
1913 static int vpif_remove(struct platform_device *device)
1915 struct channel_obj *ch;
1918 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1920 /* un-register device */
1921 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1922 /* Get the pointer to the channel object */
1923 ch = vpif_obj.dev[i];
1924 /* Unregister video device */
1925 video_unregister_device(ch->video_dev);
1927 ch->video_dev = NULL;
1933 static __refdata struct platform_driver vpif_driver = {
1935 .name = "vpif_display",
1936 .owner = THIS_MODULE,
1938 .probe = vpif_probe,
1939 .remove = vpif_remove,
1942 static __init int vpif_init(void)
1944 return platform_driver_register(&vpif_driver);
1948 * vpif_cleanup: This function un-registers device and driver to the kernel,
1949 * frees requested irq handler and de-allocates memory allocated for channel
1952 static void vpif_cleanup(void)
1954 struct platform_device *pdev;
1955 struct resource *res;
1959 pdev = container_of(vpif_dev, struct platform_device, dev);
1961 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1962 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1964 (void *)(&vpif_obj.dev[i]->channel_id));
1968 platform_driver_unregister(&vpif_driver);
1970 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1971 kfree(vpif_obj.dev[i]);
1974 module_init(vpif_init);
1975 module_exit(vpif_cleanup);