2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
11 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/string.h>
26 #include <linux/videodev2.h>
27 #include <linux/wait.h>
28 #include <linux/time.h>
29 #include <linux/i2c.h>
30 #include <linux/platform_device.h>
32 #include <linux/slab.h>
37 #include <media/adv7343.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-chip-ident.h>
42 #include "vpif_display.h"
45 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(VPIF_DISPLAY_VERSION);
49 #define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
51 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
52 #define vpif_dbg(level, debug, fmt, arg...) \
53 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
56 static u32 ch2_numbuffers = 3;
57 static u32 ch3_numbuffers = 3;
58 static u32 ch2_bufsize = 1920 * 1080 * 2;
59 static u32 ch3_bufsize = 720 * 576 * 2;
61 module_param(debug, int, 0644);
62 module_param(ch2_numbuffers, uint, S_IRUGO);
63 module_param(ch3_numbuffers, uint, S_IRUGO);
64 module_param(ch2_bufsize, uint, S_IRUGO);
65 module_param(ch3_bufsize, uint, S_IRUGO);
67 MODULE_PARM_DESC(debug, "Debug level 0-1");
68 MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
69 MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
70 MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
71 MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
73 static struct vpif_config_params config_params = {
77 .min_bufsize[0] = 720 * 480 * 2,
78 .min_bufsize[1] = 720 * 480 * 2,
79 .channel_bufsize[0] = 1920 * 1080 * 2,
80 .channel_bufsize[1] = 720 * 576 * 2,
83 static struct vpif_device vpif_obj = { {NULL} };
84 static struct device *vpif_dev;
87 * vpif_uservirt_to_phys: This function is used to convert user
88 * space virtual address to physical address.
90 static u32 vpif_uservirt_to_phys(u32 virtp)
92 struct mm_struct *mm = current->mm;
93 unsigned long physp = 0;
94 struct vm_area_struct *vma;
96 vma = find_vma(mm, virtp);
98 /* For kernel direct-mapped memory, take the easy way */
99 if (virtp >= PAGE_OFFSET) {
100 physp = virt_to_phys((void *)virtp);
101 } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
102 /* this will catch, kernel-allocated, mmaped-to-usermode addr */
103 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
105 /* otherwise, use get_user_pages() for general userland pages */
106 int res, nr_pages = 1;
108 down_read(¤t->mm->mmap_sem);
110 res = get_user_pages(current, current->mm,
111 virtp, nr_pages, 1, 0, &pages, NULL);
112 up_read(¤t->mm->mmap_sem);
114 if (res == nr_pages) {
115 physp = __pa(page_address(&pages[0]) +
116 (virtp & ~PAGE_MASK));
118 vpif_err("get_user_pages failed\n");
127 * buffer_prepare: This is the callback function called from videobuf_qbuf()
128 * function the buffer is prepared and user space virtual address is converted
129 * into physical address
131 static int vpif_buffer_prepare(struct videobuf_queue *q,
132 struct videobuf_buffer *vb,
133 enum v4l2_field field)
135 struct vpif_fh *fh = q->priv_data;
136 struct common_obj *common;
139 common = &fh->channel->common[VPIF_VIDEO_INDEX];
140 if (VIDEOBUF_NEEDS_INIT == vb->state) {
141 vb->width = common->width;
142 vb->height = common->height;
143 vb->size = vb->width * vb->height;
146 vb->state = VIDEOBUF_PREPARED;
148 /* if user pointer memory mechanism is used, get the physical
149 * address of the buffer */
150 if (V4L2_MEMORY_USERPTR == common->memory) {
152 vpif_err("buffer_address is 0\n");
156 vb->boff = vpif_uservirt_to_phys(vb->baddr);
157 if (!ISALIGNED(vb->boff))
162 if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
163 if (!ISALIGNED(addr + common->ytop_off) ||
164 !ISALIGNED(addr + common->ybtm_off) ||
165 !ISALIGNED(addr + common->ctop_off) ||
166 !ISALIGNED(addr + common->cbtm_off))
172 vpif_err("buffer offset not aligned to 8 bytes\n");
177 * vpif_buffer_setup: This function allocates memory for the buffers
179 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
182 struct vpif_fh *fh = q->priv_data;
183 struct channel_obj *ch = fh->channel;
184 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
186 if (V4L2_MEMORY_MMAP != common->memory)
189 *size = config_params.channel_bufsize[ch->channel_id];
190 if (*count < config_params.min_numbuffers)
191 *count = config_params.min_numbuffers;
197 * vpif_buffer_queue: This function adds the buffer to DMA queue
199 static void vpif_buffer_queue(struct videobuf_queue *q,
200 struct videobuf_buffer *vb)
202 struct vpif_fh *fh = q->priv_data;
203 struct common_obj *common;
205 common = &fh->channel->common[VPIF_VIDEO_INDEX];
207 /* add the buffer to the DMA queue */
208 list_add_tail(&vb->queue, &common->dma_queue);
209 vb->state = VIDEOBUF_QUEUED;
213 * vpif_buffer_release: This function is called from the videobuf layer to
214 * free memory allocated to the buffers
216 static void vpif_buffer_release(struct videobuf_queue *q,
217 struct videobuf_buffer *vb)
219 struct vpif_fh *fh = q->priv_data;
220 struct channel_obj *ch = fh->channel;
221 struct common_obj *common;
222 unsigned int buf_size = 0;
224 common = &ch->common[VPIF_VIDEO_INDEX];
226 videobuf_dma_contig_free(q, vb);
227 vb->state = VIDEOBUF_NEEDS_INIT;
229 if (V4L2_MEMORY_MMAP != common->memory)
232 buf_size = config_params.channel_bufsize[ch->channel_id];
235 static struct videobuf_queue_ops video_qops = {
236 .buf_setup = vpif_buffer_setup,
237 .buf_prepare = vpif_buffer_prepare,
238 .buf_queue = vpif_buffer_queue,
239 .buf_release = vpif_buffer_release,
241 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
243 static void process_progressive_mode(struct common_obj *common)
245 unsigned long addr = 0;
247 /* Get the next buffer from buffer queue */
248 common->next_frm = list_entry(common->dma_queue.next,
249 struct videobuf_buffer, queue);
250 /* Remove that buffer from the buffer queue */
251 list_del(&common->next_frm->queue);
252 /* Mark status of the buffer as active */
253 common->next_frm->state = VIDEOBUF_ACTIVE;
255 /* Set top and bottom field addrs in VPIF registers */
256 addr = videobuf_to_dma_contig(common->next_frm);
257 common->set_addr(addr + common->ytop_off,
258 addr + common->ybtm_off,
259 addr + common->ctop_off,
260 addr + common->cbtm_off);
263 static void process_interlaced_mode(int fid, struct common_obj *common)
265 /* device field id and local field id are in sync */
266 /* If this is even field */
268 if (common->cur_frm == common->next_frm)
271 /* one frame is displayed If next frame is
272 * available, release cur_frm and move on */
273 /* Copy frame display time */
274 do_gettimeofday(&common->cur_frm->ts);
275 /* Change status of the cur_frm */
276 common->cur_frm->state = VIDEOBUF_DONE;
277 /* unlock semaphore on cur_frm */
278 wake_up_interruptible(&common->cur_frm->done);
279 /* Make cur_frm pointing to next_frm */
280 common->cur_frm = common->next_frm;
282 } else if (1 == fid) { /* odd field */
283 if (list_empty(&common->dma_queue)
284 || (common->cur_frm != common->next_frm)) {
287 /* one field is displayed configure the next
288 * frame if it is available else hold on current
290 /* Get next from the buffer queue */
291 process_progressive_mode(common);
297 * vpif_channel_isr: It changes status of the displayed buffer, takes next
298 * buffer from the queue and sets its address in VPIF registers
300 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
302 struct vpif_device *dev = &vpif_obj;
303 struct channel_obj *ch;
304 struct common_obj *common;
305 enum v4l2_field field;
309 channel_id = *(int *)(dev_id);
310 ch = dev->dev[channel_id];
311 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
312 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
313 common = &ch->common[i];
314 /* If streaming is started in this channel */
315 if (0 == common->started)
318 if (1 == ch->vpifparams.std_info.frm_fmt) {
319 if (list_empty(&common->dma_queue))
322 /* Progressive mode */
323 if (!channel_first_int[i][channel_id]) {
324 /* Mark status of the cur_frm to
325 * done and unlock semaphore on it */
326 do_gettimeofday(&common->cur_frm->ts);
327 common->cur_frm->state = VIDEOBUF_DONE;
328 wake_up_interruptible(&common->cur_frm->done);
329 /* Make cur_frm pointing to next_frm */
330 common->cur_frm = common->next_frm;
333 channel_first_int[i][channel_id] = 0;
334 process_progressive_mode(common);
336 /* Interlaced mode */
337 /* If it is first interrupt, ignore it */
339 if (channel_first_int[i][channel_id]) {
340 channel_first_int[i][channel_id] = 0;
346 /* Get field id from VPIF registers */
347 fid = vpif_channel_getfid(ch->channel_id + 2);
348 /* If fid does not match with stored field id */
349 if (fid != ch->field_id) {
350 /* Make them in sync */
357 process_interlaced_mode(fid, common);
364 static int vpif_update_std_info(struct channel_obj *ch)
366 struct video_obj *vid_ch = &ch->video;
367 struct vpif_params *vpifparams = &ch->vpifparams;
368 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
369 const struct vpif_channel_config_params *config;
373 for (i = 0; i < vpif_ch_params_count; i++) {
374 config = &ch_params[i];
375 if (config->hd_sd == 0) {
376 vpif_dbg(2, debug, "SD format\n");
377 if (config->stdid & vid_ch->stdid) {
378 memcpy(std_info, config, sizeof(*config));
382 vpif_dbg(2, debug, "HD format\n");
383 if (config->dv_preset == vid_ch->dv_preset) {
384 memcpy(std_info, config, sizeof(*config));
390 if (i == vpif_ch_params_count) {
391 vpif_dbg(1, debug, "Format not found\n");
398 static int vpif_update_resolution(struct channel_obj *ch)
400 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
401 struct video_obj *vid_ch = &ch->video;
402 struct vpif_params *vpifparams = &ch->vpifparams;
403 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
405 if (!vid_ch->stdid && !vid_ch->dv_preset && !vid_ch->bt_timings.height)
408 if (vid_ch->stdid || vid_ch->dv_preset) {
409 if (vpif_update_std_info(ch))
413 common->fmt.fmt.pix.width = std_info->width;
414 common->fmt.fmt.pix.height = std_info->height;
415 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
416 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
418 /* Set height and width paramateres */
419 common->height = std_info->height;
420 common->width = std_info->width;
426 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
427 * in the top and bottom field
429 static void vpif_calculate_offsets(struct channel_obj *ch)
431 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
432 struct vpif_params *vpifparams = &ch->vpifparams;
433 enum v4l2_field field = common->fmt.fmt.pix.field;
434 struct video_obj *vid_ch = &ch->video;
435 unsigned int hpitch, vpitch, sizeimage;
437 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
438 if (ch->vpifparams.std_info.frm_fmt)
439 vid_ch->buf_field = V4L2_FIELD_NONE;
441 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
443 vid_ch->buf_field = common->fmt.fmt.pix.field;
446 if (V4L2_MEMORY_USERPTR == common->memory)
447 sizeimage = common->fmt.fmt.pix.sizeimage;
449 sizeimage = config_params.channel_bufsize[ch->channel_id];
451 hpitch = common->fmt.fmt.pix.bytesperline;
452 vpitch = sizeimage / (hpitch * 2);
453 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
454 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
455 common->ytop_off = 0;
456 common->ybtm_off = hpitch;
457 common->ctop_off = sizeimage / 2;
458 common->cbtm_off = sizeimage / 2 + hpitch;
459 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
460 common->ytop_off = 0;
461 common->ybtm_off = sizeimage / 4;
462 common->ctop_off = sizeimage / 2;
463 common->cbtm_off = common->ctop_off + sizeimage / 4;
464 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
465 common->ybtm_off = 0;
466 common->ytop_off = sizeimage / 4;
467 common->cbtm_off = sizeimage / 2;
468 common->ctop_off = common->cbtm_off + sizeimage / 4;
471 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
472 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
473 vpifparams->video_params.storage_mode = 1;
475 vpifparams->video_params.storage_mode = 0;
478 if (ch->vpifparams.std_info.frm_fmt == 1) {
479 vpifparams->video_params.hpitch =
480 common->fmt.fmt.pix.bytesperline;
482 if ((field == V4L2_FIELD_ANY) ||
483 (field == V4L2_FIELD_INTERLACED))
484 vpifparams->video_params.hpitch =
485 common->fmt.fmt.pix.bytesperline * 2;
487 vpifparams->video_params.hpitch =
488 common->fmt.fmt.pix.bytesperline;
491 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
494 static void vpif_config_format(struct channel_obj *ch)
496 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
498 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
499 if (config_params.numbuffers[ch->channel_id] == 0)
500 common->memory = V4L2_MEMORY_USERPTR;
502 common->memory = V4L2_MEMORY_MMAP;
504 common->fmt.fmt.pix.sizeimage =
505 config_params.channel_bufsize[ch->channel_id];
506 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
507 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
510 static int vpif_check_format(struct channel_obj *ch,
511 struct v4l2_pix_format *pixfmt)
513 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
514 enum v4l2_field field = pixfmt->field;
515 u32 sizeimage, hpitch, vpitch;
517 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
518 goto invalid_fmt_exit;
520 if (!(VPIF_VALID_FIELD(field)))
521 goto invalid_fmt_exit;
523 if (pixfmt->bytesperline <= 0)
524 goto invalid_pitch_exit;
526 if (V4L2_MEMORY_USERPTR == common->memory)
527 sizeimage = pixfmt->sizeimage;
529 sizeimage = config_params.channel_bufsize[ch->channel_id];
531 if (vpif_update_resolution(ch))
534 hpitch = pixfmt->bytesperline;
535 vpitch = sizeimage / (hpitch * 2);
537 /* Check for valid value of pitch */
538 if ((hpitch < ch->vpifparams.std_info.width) ||
539 (vpitch < ch->vpifparams.std_info.height))
540 goto invalid_pitch_exit;
542 /* Check for 8 byte alignment */
543 if (!ISALIGNED(hpitch)) {
544 vpif_err("invalid pitch alignment\n");
547 pixfmt->width = common->fmt.fmt.pix.width;
548 pixfmt->height = common->fmt.fmt.pix.height;
553 vpif_err("invalid field format\n");
557 vpif_err("invalid pitch\n");
561 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
563 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
565 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
566 common->set_addr = ch3_set_videobuf_addr;
569 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
571 common->set_addr = ch2_set_videobuf_addr;
576 * vpif_mmap: It is used to map kernel space buffers into user spaces
578 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
580 struct vpif_fh *fh = filep->private_data;
581 struct channel_obj *ch = fh->channel;
582 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
584 vpif_dbg(2, debug, "vpif_mmap\n");
586 return videobuf_mmap_mapper(&common->buffer_queue, vma);
590 * vpif_poll: It is used for select/poll system call
592 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
594 struct vpif_fh *fh = filep->private_data;
595 struct channel_obj *ch = fh->channel;
596 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
599 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
605 * vpif_open: It creates object of file handle structure and stores it in
606 * private_data member of filepointer
608 static int vpif_open(struct file *filep)
610 struct video_device *vdev = video_devdata(filep);
611 struct channel_obj *ch = NULL;
612 struct vpif_fh *fh = NULL;
614 ch = video_get_drvdata(vdev);
615 /* Allocate memory for the file handle object */
616 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
618 vpif_err("unable to allocate memory for file handle object\n");
622 /* store pointer to fh in private_data member of filep */
623 filep->private_data = fh;
626 if (!ch->initialized) {
629 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
632 /* Increment channel usrs counter */
633 atomic_inc(&ch->usrs);
634 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
635 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
636 /* Initialize priority of this instance to default priority */
637 fh->prio = V4L2_PRIORITY_UNSET;
638 v4l2_prio_open(&ch->prio, &fh->prio);
644 * vpif_release: This function deletes buffer queue, frees the buffers and
645 * the vpif file handle
647 static int vpif_release(struct file *filep)
649 struct vpif_fh *fh = filep->private_data;
650 struct channel_obj *ch = fh->channel;
651 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
653 /* if this instance is doing IO */
654 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
655 /* Reset io_usrs member of channel object */
657 /* Disable channel */
658 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
660 channel2_intr_enable(0);
662 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
663 (2 == common->started)) {
665 channel3_intr_enable(0);
668 /* Free buffers allocated */
669 videobuf_queue_cancel(&common->buffer_queue);
670 videobuf_mmap_free(&common->buffer_queue);
672 config_params.numbuffers[ch->channel_id];
675 /* Decrement channel usrs counter */
676 atomic_dec(&ch->usrs);
677 /* If this file handle has initialize encoder device, reset it */
681 /* Close the priority */
682 v4l2_prio_close(&ch->prio, fh->prio);
683 filep->private_data = NULL;
690 /* functions implementing ioctls */
692 * vpif_querycap() - QUERYCAP handler
695 * @cap: ptr to v4l2_capability structure
697 static int vpif_querycap(struct file *file, void *priv,
698 struct v4l2_capability *cap)
700 struct vpif_display_config *config = vpif_dev->platform_data;
702 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
703 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
704 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
705 strlcpy(cap->card, config->card_name, sizeof(cap->card));
710 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
711 struct v4l2_fmtdesc *fmt)
713 if (fmt->index != 0) {
714 vpif_err("Invalid format index\n");
718 /* Fill in the information about format */
719 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
720 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
721 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
726 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
727 struct v4l2_format *fmt)
729 struct vpif_fh *fh = priv;
730 struct channel_obj *ch = fh->channel;
731 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
733 /* Check the validity of the buffer type */
734 if (common->fmt.type != fmt->type)
737 if (vpif_update_resolution(ch))
743 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
744 struct v4l2_format *fmt)
746 struct vpif_fh *fh = priv;
747 struct v4l2_pix_format *pixfmt;
748 struct channel_obj *ch = fh->channel;
749 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
752 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
753 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
754 if (!fh->initialized) {
755 vpif_dbg(1, debug, "Channel Busy\n");
759 /* Check for the priority */
760 ret = v4l2_prio_check(&ch->prio, fh->prio);
766 if (common->started) {
767 vpif_dbg(1, debug, "Streaming in progress\n");
771 pixfmt = &fmt->fmt.pix;
772 /* Check for valid field format */
773 ret = vpif_check_format(ch, pixfmt);
777 /* store the pix format in the channel object */
778 common->fmt.fmt.pix = *pixfmt;
779 /* store the format in the channel object */
784 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
785 struct v4l2_format *fmt)
787 struct vpif_fh *fh = priv;
788 struct channel_obj *ch = fh->channel;
789 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
790 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
793 ret = vpif_check_format(ch, pixfmt);
795 *pixfmt = common->fmt.fmt.pix;
796 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
802 static int vpif_reqbufs(struct file *file, void *priv,
803 struct v4l2_requestbuffers *reqbuf)
805 struct vpif_fh *fh = priv;
806 struct channel_obj *ch = fh->channel;
807 struct common_obj *common;
808 enum v4l2_field field;
811 /* This file handle has not initialized the channel,
812 It is not allowed to do settings */
813 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
814 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
815 if (!fh->initialized) {
816 vpif_err("Channel Busy\n");
821 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
824 index = VPIF_VIDEO_INDEX;
826 common = &ch->common[index];
828 if (common->fmt.type != reqbuf->type)
831 if (0 != common->io_usrs)
834 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
835 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
836 field = V4L2_FIELD_INTERLACED;
838 field = common->fmt.fmt.pix.field;
840 field = V4L2_VBI_INTERLACED;
843 /* Initialize videobuf queue as per the buffer type */
844 videobuf_queue_dma_contig_init(&common->buffer_queue,
848 sizeof(struct videobuf_buffer), fh,
851 /* Set io allowed member of file handle to TRUE */
852 fh->io_allowed[index] = 1;
853 /* Increment io usrs member of channel object to 1 */
855 /* Store type of memory requested in channel object */
856 common->memory = reqbuf->memory;
857 INIT_LIST_HEAD(&common->dma_queue);
859 /* Allocate buffers */
860 return videobuf_reqbufs(&common->buffer_queue, reqbuf);
863 static int vpif_querybuf(struct file *file, void *priv,
864 struct v4l2_buffer *tbuf)
866 struct vpif_fh *fh = priv;
867 struct channel_obj *ch = fh->channel;
868 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
870 if (common->fmt.type != tbuf->type)
873 return videobuf_querybuf(&common->buffer_queue, tbuf);
876 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
879 struct vpif_fh *fh = priv;
880 struct channel_obj *ch = fh->channel;
881 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
882 struct v4l2_buffer tbuf = *buf;
883 struct videobuf_buffer *buf1;
884 unsigned long addr = 0;
888 if (common->fmt.type != tbuf.type)
891 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
892 vpif_err("fh->io_allowed\n");
896 if (!(list_empty(&common->dma_queue)) ||
897 (common->cur_frm != common->next_frm) ||
898 !(common->started) ||
899 (common->started && (0 == ch->field_id)))
900 return videobuf_qbuf(&common->buffer_queue, buf);
902 /* bufferqueue is empty store buffer address in VPIF registers */
903 mutex_lock(&common->buffer_queue.vb_lock);
904 buf1 = common->buffer_queue.bufs[tbuf.index];
905 if (buf1->memory != tbuf.memory) {
906 vpif_err("invalid buffer type\n");
910 if ((buf1->state == VIDEOBUF_QUEUED) ||
911 (buf1->state == VIDEOBUF_ACTIVE)) {
912 vpif_err("invalid state\n");
916 switch (buf1->memory) {
917 case V4L2_MEMORY_MMAP:
918 if (buf1->baddr == 0)
922 case V4L2_MEMORY_USERPTR:
923 if (tbuf.length < buf1->bsize)
926 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
927 && (buf1->baddr != tbuf.m.userptr)) {
928 vpif_buffer_release(&common->buffer_queue, buf1);
929 buf1->baddr = tbuf.m.userptr;
937 local_irq_save(flags);
938 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
939 common->buffer_queue.field);
941 local_irq_restore(flags);
945 buf1->state = VIDEOBUF_ACTIVE;
947 common->next_frm = buf1;
948 if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
949 common->set_addr((addr + common->ytop_off),
950 (addr + common->ybtm_off),
951 (addr + common->ctop_off),
952 (addr + common->cbtm_off));
955 local_irq_restore(flags);
956 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
957 mutex_unlock(&common->buffer_queue.vb_lock);
961 mutex_unlock(&common->buffer_queue.vb_lock);
965 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
967 struct vpif_fh *fh = priv;
968 struct channel_obj *ch = fh->channel;
969 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
972 if (!(*std_id & DM646X_V4L2_STD))
975 if (common->started) {
976 vpif_err("streaming in progress\n");
980 /* Call encoder subdevice function to set the standard */
981 ch->video.stdid = *std_id;
982 ch->video.dv_preset = V4L2_DV_INVALID;
983 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
985 /* Get the information about the standard */
986 if (vpif_update_resolution(ch))
989 if ((ch->vpifparams.std_info.width *
990 ch->vpifparams.std_info.height * 2) >
991 config_params.channel_bufsize[ch->channel_id]) {
992 vpif_err("invalid std for this size\n");
996 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
997 /* Configure the default format information */
998 vpif_config_format(ch);
1000 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1001 s_std_output, *std_id);
1003 vpif_err("Failed to set output standard\n");
1007 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1010 vpif_err("Failed to set standard for sub devices\n");
1014 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1016 struct vpif_fh *fh = priv;
1017 struct channel_obj *ch = fh->channel;
1019 *std = ch->video.stdid;
1023 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1025 struct vpif_fh *fh = priv;
1026 struct channel_obj *ch = fh->channel;
1027 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1029 return videobuf_dqbuf(&common->buffer_queue, p,
1030 (file->f_flags & O_NONBLOCK));
1033 static int vpif_streamon(struct file *file, void *priv,
1034 enum v4l2_buf_type buftype)
1036 struct vpif_fh *fh = priv;
1037 struct channel_obj *ch = fh->channel;
1038 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1039 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1040 struct vpif_params *vpif = &ch->vpifparams;
1041 struct vpif_display_config *vpif_config_data =
1042 vpif_dev->platform_data;
1043 unsigned long addr = 0;
1046 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1047 vpif_err("buffer type not supported\n");
1051 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1052 vpif_err("fh->io_allowed\n");
1056 /* If Streaming is already started, return error */
1057 if (common->started) {
1058 vpif_err("channel->started\n");
1062 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1063 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1064 ch->vpifparams.std_info.ycmux_mode == 0)
1065 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1066 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1067 vpif_err("other channel is using\n");
1071 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1075 /* Call videobuf_streamon to start streaming in videobuf */
1076 ret = videobuf_streamon(&common->buffer_queue);
1078 vpif_err("videobuf_streamon\n");
1082 /* If buffer queue is empty, return error */
1083 if (list_empty(&common->dma_queue)) {
1084 vpif_err("buffer queue is empty\n");
1088 /* Get the next frame from the buffer queue */
1089 common->next_frm = common->cur_frm =
1090 list_entry(common->dma_queue.next,
1091 struct videobuf_buffer, queue);
1093 list_del(&common->cur_frm->queue);
1094 /* Mark state of the current frame to active */
1095 common->cur_frm->state = VIDEOBUF_ACTIVE;
1097 /* Initialize field_id and started member */
1099 common->started = 1;
1100 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1101 addr = common->cur_frm->boff;
1102 /* Calculate the offset for Y and C data in the buffer */
1103 vpif_calculate_offsets(ch);
1105 if ((ch->vpifparams.std_info.frm_fmt &&
1106 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1107 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1108 || (!ch->vpifparams.std_info.frm_fmt
1109 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1110 vpif_err("conflict in field format and std format\n");
1114 /* clock settings */
1116 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1117 ch->vpifparams.std_info.hd_sd);
1119 vpif_err("can't set clock\n");
1123 /* set the parameters and addresses */
1124 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1128 common->started = ret;
1129 vpif_config_addr(ch, ret);
1130 common->set_addr((addr + common->ytop_off),
1131 (addr + common->ybtm_off),
1132 (addr + common->ctop_off),
1133 (addr + common->cbtm_off));
1135 /* Set interrupt for both the fields in VPIF
1136 Register enable channel in VPIF register */
1137 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1138 channel2_intr_assert();
1139 channel2_intr_enable(1);
1143 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1144 || (common->started == 2)) {
1145 channel3_intr_assert();
1146 channel3_intr_enable(1);
1149 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1154 static int vpif_streamoff(struct file *file, void *priv,
1155 enum v4l2_buf_type buftype)
1157 struct vpif_fh *fh = priv;
1158 struct channel_obj *ch = fh->channel;
1159 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1161 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1162 vpif_err("buffer type not supported\n");
1166 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1167 vpif_err("fh->io_allowed\n");
1171 if (!common->started) {
1172 vpif_err("channel->started\n");
1176 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1177 /* disable channel */
1178 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1180 channel2_intr_enable(0);
1182 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1183 (2 == common->started)) {
1185 channel3_intr_enable(0);
1189 common->started = 0;
1190 return videobuf_streamoff(&common->buffer_queue);
1193 static int vpif_cropcap(struct file *file, void *priv,
1194 struct v4l2_cropcap *crop)
1196 struct vpif_fh *fh = priv;
1197 struct channel_obj *ch = fh->channel;
1198 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1199 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1202 crop->bounds.left = crop->bounds.top = 0;
1203 crop->defrect.left = crop->defrect.top = 0;
1204 crop->defrect.height = crop->bounds.height = common->height;
1205 crop->defrect.width = crop->bounds.width = common->width;
1210 static int vpif_enum_output(struct file *file, void *fh,
1211 struct v4l2_output *output)
1214 struct vpif_display_config *config = vpif_dev->platform_data;
1216 if (output->index >= config->output_count) {
1217 vpif_dbg(1, debug, "Invalid output index\n");
1221 strcpy(output->name, config->output[output->index]);
1222 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1223 output->std = DM646X_V4L2_STD;
1228 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1230 struct vpif_fh *fh = priv;
1231 struct channel_obj *ch = fh->channel;
1232 struct video_obj *vid_ch = &ch->video;
1233 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1236 if (common->started) {
1237 vpif_err("Streaming in progress\n");
1241 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1242 s_routing, 0, i, 0);
1245 vpif_err("Failed to set output standard\n");
1247 vid_ch->output_id = i;
1251 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1253 struct vpif_fh *fh = priv;
1254 struct channel_obj *ch = fh->channel;
1255 struct video_obj *vid_ch = &ch->video;
1257 *i = vid_ch->output_id;
1262 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1264 struct vpif_fh *fh = priv;
1265 struct channel_obj *ch = fh->channel;
1267 *p = v4l2_prio_max(&ch->prio);
1272 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1274 struct vpif_fh *fh = priv;
1275 struct channel_obj *ch = fh->channel;
1277 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1281 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1283 * @priv: file handle
1284 * @preset: input preset
1286 static int vpif_enum_dv_presets(struct file *file, void *priv,
1287 struct v4l2_dv_enum_preset *preset)
1289 struct vpif_fh *fh = priv;
1290 struct channel_obj *ch = fh->channel;
1291 struct video_obj *vid_ch = &ch->video;
1293 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1294 video, enum_dv_presets, preset);
1298 * vpif_s_dv_presets() - S_DV_PRESETS handler
1300 * @priv: file handle
1301 * @preset: input preset
1303 static int vpif_s_dv_preset(struct file *file, void *priv,
1304 struct v4l2_dv_preset *preset)
1306 struct vpif_fh *fh = priv;
1307 struct channel_obj *ch = fh->channel;
1308 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1309 struct video_obj *vid_ch = &ch->video;
1312 if (common->started) {
1313 vpif_dbg(1, debug, "streaming in progress\n");
1317 ret = v4l2_prio_check(&ch->prio, fh->prio);
1321 fh->initialized = 1;
1323 /* Call encoder subdevice function to set the standard */
1324 if (mutex_lock_interruptible(&common->lock))
1325 return -ERESTARTSYS;
1327 ch->video.dv_preset = preset->preset;
1328 ch->video.stdid = V4L2_STD_UNKNOWN;
1329 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1331 /* Get the information about the standard */
1332 if (vpif_update_resolution(ch)) {
1335 /* Configure the default format information */
1336 vpif_config_format(ch);
1338 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1339 video, s_dv_preset, preset);
1342 mutex_unlock(&common->lock);
1347 * vpif_g_dv_presets() - G_DV_PRESETS handler
1349 * @priv: file handle
1350 * @preset: input preset
1352 static int vpif_g_dv_preset(struct file *file, void *priv,
1353 struct v4l2_dv_preset *preset)
1355 struct vpif_fh *fh = priv;
1356 struct channel_obj *ch = fh->channel;
1358 preset->preset = ch->video.dv_preset;
1363 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1365 * @priv: file handle
1366 * @timings: digital video timings
1368 static int vpif_s_dv_timings(struct file *file, void *priv,
1369 struct v4l2_dv_timings *timings)
1371 struct vpif_fh *fh = priv;
1372 struct channel_obj *ch = fh->channel;
1373 struct vpif_params *vpifparams = &ch->vpifparams;
1374 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1375 struct video_obj *vid_ch = &ch->video;
1376 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1379 if (timings->type != V4L2_DV_BT_656_1120) {
1380 vpif_dbg(2, debug, "Timing type not defined\n");
1384 /* Configure subdevice timings, if any */
1385 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1386 video, s_dv_timings, timings);
1387 if (ret == -ENOIOCTLCMD) {
1388 vpif_dbg(2, debug, "Custom DV timings not supported by "
1393 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1397 if (!(timings->bt.width && timings->bt.height &&
1398 (timings->bt.hbackporch ||
1399 timings->bt.hfrontporch ||
1400 timings->bt.hsync) &&
1401 timings->bt.vfrontporch &&
1402 (timings->bt.vbackporch ||
1403 timings->bt.vsync))) {
1404 vpif_dbg(2, debug, "Timings for width, height, "
1405 "horizontal back porch, horizontal sync, "
1406 "horizontal front porch, vertical back porch, "
1407 "vertical sync and vertical back porch "
1408 "must be defined\n");
1414 /* Configure video port timings */
1416 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1418 std_info->sav2eav = bt->width;
1421 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1423 if (bt->interlaced) {
1424 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1425 std_info->vsize = bt->height * 2 +
1426 bt->vfrontporch + bt->vsync + bt->vbackporch +
1427 bt->il_vfrontporch + bt->il_vsync +
1429 std_info->l5 = std_info->vsize/2 -
1430 (bt->vfrontporch - 1);
1431 std_info->l7 = std_info->vsize/2 + 1;
1432 std_info->l9 = std_info->l7 + bt->il_vsync +
1433 bt->il_vbackporch + 1;
1434 std_info->l11 = std_info->vsize -
1435 (bt->il_vfrontporch - 1);
1437 vpif_dbg(2, debug, "Required timing values for "
1438 "interlaced BT format missing\n");
1442 std_info->vsize = bt->height + bt->vfrontporch +
1443 bt->vsync + bt->vbackporch;
1444 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1446 strncpy(std_info->name, "Custom timings BT656/1120",
1448 std_info->width = bt->width;
1449 std_info->height = bt->height;
1450 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1451 std_info->ycmux_mode = 0;
1452 std_info->capture_format = 0;
1453 std_info->vbi_supported = 0;
1454 std_info->hd_sd = 1;
1455 std_info->stdid = 0;
1456 std_info->dv_preset = V4L2_DV_INVALID;
1459 vid_ch->dv_preset = V4L2_DV_INVALID;
1465 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1467 * @priv: file handle
1468 * @timings: digital video timings
1470 static int vpif_g_dv_timings(struct file *file, void *priv,
1471 struct v4l2_dv_timings *timings)
1473 struct vpif_fh *fh = priv;
1474 struct channel_obj *ch = fh->channel;
1475 struct video_obj *vid_ch = &ch->video;
1476 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1484 * vpif_g_chip_ident() - Identify the chip
1486 * @priv: file handle
1487 * @chip: chip identity
1489 * Returns zero or -EINVAL if read operations fails.
1491 static int vpif_g_chip_ident(struct file *file, void *priv,
1492 struct v4l2_dbg_chip_ident *chip)
1494 chip->ident = V4L2_IDENT_NONE;
1496 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1497 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1498 vpif_dbg(2, debug, "match_type is invalid.\n");
1502 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1503 g_chip_ident, chip);
1506 #ifdef CONFIG_VIDEO_ADV_DEBUG
1508 * vpif_dbg_g_register() - Read register
1510 * @priv: file handle
1511 * @reg: register to be read
1514 * Returns zero or -EINVAL if read operations fails.
1516 static int vpif_dbg_g_register(struct file *file, void *priv,
1517 struct v4l2_dbg_register *reg){
1518 struct vpif_fh *fh = priv;
1519 struct channel_obj *ch = fh->channel;
1520 struct video_obj *vid_ch = &ch->video;
1522 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1527 * vpif_dbg_s_register() - Write to register
1529 * @priv: file handle
1530 * @reg: register to be modified
1533 * Returns zero or -EINVAL if write operations fails.
1535 static int vpif_dbg_s_register(struct file *file, void *priv,
1536 struct v4l2_dbg_register *reg){
1537 struct vpif_fh *fh = priv;
1538 struct channel_obj *ch = fh->channel;
1539 struct video_obj *vid_ch = &ch->video;
1541 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1547 * vpif_log_status() - Status information
1549 * @priv: file handle
1553 static int vpif_log_status(struct file *filep, void *priv)
1555 /* status for sub devices */
1556 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1561 /* vpif display ioctl operations */
1562 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1563 .vidioc_querycap = vpif_querycap,
1564 .vidioc_g_priority = vpif_g_priority,
1565 .vidioc_s_priority = vpif_s_priority,
1566 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1567 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1568 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1569 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1570 .vidioc_reqbufs = vpif_reqbufs,
1571 .vidioc_querybuf = vpif_querybuf,
1572 .vidioc_qbuf = vpif_qbuf,
1573 .vidioc_dqbuf = vpif_dqbuf,
1574 .vidioc_streamon = vpif_streamon,
1575 .vidioc_streamoff = vpif_streamoff,
1576 .vidioc_s_std = vpif_s_std,
1577 .vidioc_g_std = vpif_g_std,
1578 .vidioc_enum_output = vpif_enum_output,
1579 .vidioc_s_output = vpif_s_output,
1580 .vidioc_g_output = vpif_g_output,
1581 .vidioc_cropcap = vpif_cropcap,
1582 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
1583 .vidioc_s_dv_preset = vpif_s_dv_preset,
1584 .vidioc_g_dv_preset = vpif_g_dv_preset,
1585 .vidioc_s_dv_timings = vpif_s_dv_timings,
1586 .vidioc_g_dv_timings = vpif_g_dv_timings,
1587 .vidioc_g_chip_ident = vpif_g_chip_ident,
1588 #ifdef CONFIG_VIDEO_ADV_DEBUG
1589 .vidioc_g_register = vpif_dbg_g_register,
1590 .vidioc_s_register = vpif_dbg_s_register,
1592 .vidioc_log_status = vpif_log_status,
1595 static const struct v4l2_file_operations vpif_fops = {
1596 .owner = THIS_MODULE,
1598 .release = vpif_release,
1599 .unlocked_ioctl = video_ioctl2,
1604 static struct video_device vpif_video_template = {
1607 .ioctl_ops = &vpif_ioctl_ops,
1608 .tvnorms = DM646X_V4L2_STD,
1609 .current_norm = V4L2_STD_625_50,
1613 /*Configure the channels, buffer sizei, request irq */
1614 static int initialize_vpif(void)
1616 int free_channel_objects_index;
1617 int free_buffer_channel_index;
1618 int free_buffer_index;
1621 /* Default number of buffers should be 3 */
1622 if ((ch2_numbuffers > 0) &&
1623 (ch2_numbuffers < config_params.min_numbuffers))
1624 ch2_numbuffers = config_params.min_numbuffers;
1625 if ((ch3_numbuffers > 0) &&
1626 (ch3_numbuffers < config_params.min_numbuffers))
1627 ch3_numbuffers = config_params.min_numbuffers;
1629 /* Set buffer size to min buffers size if invalid buffer size is
1631 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1633 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1634 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1636 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1638 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1640 if (ch2_numbuffers) {
1641 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1644 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1646 if (ch3_numbuffers) {
1647 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1651 /* Allocate memory for six channel objects */
1652 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1654 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1655 /* If memory allocation fails, return error */
1656 if (!vpif_obj.dev[i]) {
1657 free_channel_objects_index = i;
1659 goto vpif_init_free_channel_objects;
1663 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1664 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1665 free_buffer_index = config_params.numbuffers[i - 1];
1669 vpif_init_free_channel_objects:
1670 for (j = 0; j < free_channel_objects_index; j++)
1671 kfree(vpif_obj.dev[j]);
1676 * vpif_probe: This function creates device entries by register itself to the
1677 * V4L2 driver and initializes fields of each channel objects
1679 static __init int vpif_probe(struct platform_device *pdev)
1681 struct vpif_subdev_info *subdevdata;
1682 struct vpif_display_config *config;
1683 int i, j = 0, k, q, m, err = 0;
1684 struct i2c_adapter *i2c_adap;
1685 struct common_obj *common;
1686 struct channel_obj *ch;
1687 struct video_device *vfd;
1688 struct resource *res;
1691 vpif_dev = &pdev->dev;
1693 err = initialize_vpif();
1696 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1700 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1702 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1707 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1708 for (i = res->start; i <= res->end; i++) {
1709 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1711 (void *)(&vpif_obj.dev[k]->channel_id))) {
1719 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1721 /* Get the pointer to the channel object */
1722 ch = vpif_obj.dev[i];
1724 /* Allocate memory for video device */
1725 vfd = video_device_alloc();
1727 for (j = 0; j < i; j++) {
1728 ch = vpif_obj.dev[j];
1729 video_device_release(ch->video_dev);
1735 /* Initialize field of video device */
1736 *vfd = vpif_video_template;
1737 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1738 vfd->release = video_device_release;
1739 snprintf(vfd->name, sizeof(vfd->name),
1740 "DM646x_VPIFDisplay_DRIVER_V%s",
1741 VPIF_DISPLAY_VERSION);
1743 /* Set video_dev to the video device */
1744 ch->video_dev = vfd;
1747 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1748 ch = vpif_obj.dev[j];
1749 /* Initialize field of the channel objects */
1750 atomic_set(&ch->usrs, 0);
1751 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1752 ch->common[k].numbuffers = 0;
1753 common = &ch->common[k];
1754 common->io_usrs = 0;
1755 common->started = 0;
1756 spin_lock_init(&common->irqlock);
1757 mutex_init(&common->lock);
1758 common->numbuffers = 0;
1759 common->set_addr = NULL;
1760 common->ytop_off = common->ybtm_off = 0;
1761 common->ctop_off = common->cbtm_off = 0;
1762 common->cur_frm = common->next_frm = NULL;
1763 memset(&common->fmt, 0, sizeof(common->fmt));
1764 common->numbuffers = config_params.numbuffers[k];
1767 ch->initialized = 0;
1770 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1771 config_params.numbuffers[ch->channel_id];
1773 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1775 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1777 /* Initialize prio member of channel object */
1778 v4l2_prio_init(&ch->prio);
1779 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1780 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1781 ch->video_dev->lock = &common->lock;
1783 /* register video device */
1784 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1785 (int)ch, (int)&ch->video_dev);
1787 err = video_register_device(ch->video_dev,
1788 VFL_TYPE_GRABBER, (j ? 3 : 2));
1792 video_set_drvdata(ch->video_dev, ch);
1795 i2c_adap = i2c_get_adapter(1);
1796 config = pdev->dev.platform_data;
1797 subdev_count = config->subdev_count;
1798 subdevdata = config->subdevinfo;
1799 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1801 if (vpif_obj.sd == NULL) {
1802 vpif_err("unable to allocate memory for subdevice pointers\n");
1807 for (i = 0; i < subdev_count; i++) {
1808 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1810 &subdevdata[i].board_info,
1812 if (!vpif_obj.sd[i]) {
1813 vpif_err("Error registering v4l2 subdevice\n");
1814 goto probe_subdev_out;
1818 vpif_obj.sd[i]->grp_id = 1 << i;
1821 v4l2_info(&vpif_obj.v4l2_dev,
1822 "DM646x VPIF display driver initialized\n");
1828 for (k = 0; k < j; k++) {
1829 ch = vpif_obj.dev[k];
1830 video_unregister_device(ch->video_dev);
1831 video_device_release(ch->video_dev);
1832 ch->video_dev = NULL;
1835 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1836 vpif_err("VPIF IRQ request failed\n");
1837 for (q = k; k >= 0; k--) {
1838 for (m = i; m >= res->start; m--)
1839 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1840 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1848 * vpif_remove: It un-register channels from V4L2 driver
1850 static int vpif_remove(struct platform_device *device)
1852 struct channel_obj *ch;
1855 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1857 /* un-register device */
1858 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1859 /* Get the pointer to the channel object */
1860 ch = vpif_obj.dev[i];
1861 /* Unregister video device */
1862 video_unregister_device(ch->video_dev);
1864 ch->video_dev = NULL;
1870 static __refdata struct platform_driver vpif_driver = {
1872 .name = "vpif_display",
1873 .owner = THIS_MODULE,
1875 .probe = vpif_probe,
1876 .remove = vpif_remove,
1879 static __init int vpif_init(void)
1881 return platform_driver_register(&vpif_driver);
1885 * vpif_cleanup: This function un-registers device and driver to the kernel,
1886 * frees requested irq handler and de-allocates memory allocated for channel
1889 static void vpif_cleanup(void)
1891 struct platform_device *pdev;
1892 struct resource *res;
1896 pdev = container_of(vpif_dev, struct platform_device, dev);
1898 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1899 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1901 (void *)(&vpif_obj.dev[i]->channel_id));
1905 platform_driver_unregister(&vpif_driver);
1907 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1908 kfree(vpif_obj.dev[i]);
1911 module_init(vpif_init);
1912 module_exit(vpif_cleanup);