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>
37 #include <media/adv7343.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
41 #include <mach/dm646x.h>
43 #include "vpif_display.h"
46 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
47 MODULE_LICENSE("GPL");
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;
86 static const struct vpif_channel_config_params ch_params[] = {
88 "NTSC", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
89 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
92 "PAL", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
93 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
98 * vpif_uservirt_to_phys: This function is used to convert user
99 * space virtual address to physical address.
101 static u32 vpif_uservirt_to_phys(u32 virtp)
103 struct mm_struct *mm = current->mm;
104 unsigned long physp = 0;
105 struct vm_area_struct *vma;
107 vma = find_vma(mm, virtp);
109 /* For kernel direct-mapped memory, take the easy way */
110 if (virtp >= PAGE_OFFSET) {
111 physp = virt_to_phys((void *)virtp);
112 } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
113 /* this will catch, kernel-allocated, mmaped-to-usermode addr */
114 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
116 /* otherwise, use get_user_pages() for general userland pages */
117 int res, nr_pages = 1;
119 down_read(¤t->mm->mmap_sem);
121 res = get_user_pages(current, current->mm,
122 virtp, nr_pages, 1, 0, &pages, NULL);
123 up_read(¤t->mm->mmap_sem);
125 if (res == nr_pages) {
126 physp = __pa(page_address(&pages[0]) +
127 (virtp & ~PAGE_MASK));
129 vpif_err("get_user_pages failed\n");
138 * buffer_prepare: This is the callback function called from videobuf_qbuf()
139 * function the buffer is prepared and user space virtual address is converted
140 * into physical address
142 static int vpif_buffer_prepare(struct videobuf_queue *q,
143 struct videobuf_buffer *vb,
144 enum v4l2_field field)
146 struct vpif_fh *fh = q->priv_data;
147 struct common_obj *common;
150 common = &fh->channel->common[VPIF_VIDEO_INDEX];
151 if (VIDEOBUF_NEEDS_INIT == vb->state) {
152 vb->width = common->width;
153 vb->height = common->height;
154 vb->size = vb->width * vb->height;
157 vb->state = VIDEOBUF_PREPARED;
159 /* if user pointer memory mechanism is used, get the physical
160 * address of the buffer */
161 if (V4L2_MEMORY_USERPTR == common->memory) {
163 vpif_err("buffer_address is 0\n");
167 vb->boff = vpif_uservirt_to_phys(vb->baddr);
168 if (!ISALIGNED(vb->boff))
173 if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
174 if (!ISALIGNED(addr + common->ytop_off) ||
175 !ISALIGNED(addr + common->ybtm_off) ||
176 !ISALIGNED(addr + common->ctop_off) ||
177 !ISALIGNED(addr + common->cbtm_off))
183 vpif_err("buffer offset not aligned to 8 bytes\n");
188 * vpif_buffer_setup: This function allocates memory for the buffers
190 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
193 struct vpif_fh *fh = q->priv_data;
194 struct channel_obj *ch = fh->channel;
195 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
197 if (V4L2_MEMORY_MMAP != common->memory)
200 *size = config_params.channel_bufsize[ch->channel_id];
201 if (*count < config_params.min_numbuffers)
202 *count = config_params.min_numbuffers;
208 * vpif_buffer_queue: This function adds the buffer to DMA queue
210 static void vpif_buffer_queue(struct videobuf_queue *q,
211 struct videobuf_buffer *vb)
213 struct vpif_fh *fh = q->priv_data;
214 struct common_obj *common;
216 common = &fh->channel->common[VPIF_VIDEO_INDEX];
218 /* add the buffer to the DMA queue */
219 list_add_tail(&vb->queue, &common->dma_queue);
220 vb->state = VIDEOBUF_QUEUED;
224 * vpif_buffer_release: This function is called from the videobuf layer to
225 * free memory allocated to the buffers
227 static void vpif_buffer_release(struct videobuf_queue *q,
228 struct videobuf_buffer *vb)
230 struct vpif_fh *fh = q->priv_data;
231 struct channel_obj *ch = fh->channel;
232 struct common_obj *common;
233 unsigned int buf_size = 0;
235 common = &ch->common[VPIF_VIDEO_INDEX];
237 videobuf_dma_contig_free(q, vb);
238 vb->state = VIDEOBUF_NEEDS_INIT;
240 if (V4L2_MEMORY_MMAP != common->memory)
243 buf_size = config_params.channel_bufsize[ch->channel_id];
246 static struct videobuf_queue_ops video_qops = {
247 .buf_setup = vpif_buffer_setup,
248 .buf_prepare = vpif_buffer_prepare,
249 .buf_queue = vpif_buffer_queue,
250 .buf_release = vpif_buffer_release,
252 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
254 static void process_progressive_mode(struct common_obj *common)
256 unsigned long addr = 0;
258 /* Get the next buffer from buffer queue */
259 common->next_frm = list_entry(common->dma_queue.next,
260 struct videobuf_buffer, queue);
261 /* Remove that buffer from the buffer queue */
262 list_del(&common->next_frm->queue);
263 /* Mark status of the buffer as active */
264 common->next_frm->state = VIDEOBUF_ACTIVE;
266 /* Set top and bottom field addrs in VPIF registers */
267 addr = videobuf_to_dma_contig(common->next_frm);
268 common->set_addr(addr + common->ytop_off,
269 addr + common->ybtm_off,
270 addr + common->ctop_off,
271 addr + common->cbtm_off);
274 static void process_interlaced_mode(int fid, struct common_obj *common)
276 /* device field id and local field id are in sync */
277 /* If this is even field */
279 if (common->cur_frm == common->next_frm)
282 /* one frame is displayed If next frame is
283 * available, release cur_frm and move on */
284 /* Copy frame display time */
285 do_gettimeofday(&common->cur_frm->ts);
286 /* Change status of the cur_frm */
287 common->cur_frm->state = VIDEOBUF_DONE;
288 /* unlock semaphore on cur_frm */
289 wake_up_interruptible(&common->cur_frm->done);
290 /* Make cur_frm pointing to next_frm */
291 common->cur_frm = common->next_frm;
293 } else if (1 == fid) { /* odd field */
294 if (list_empty(&common->dma_queue)
295 || (common->cur_frm != common->next_frm)) {
298 /* one field is displayed configure the next
299 * frame if it is available else hold on current
301 /* Get next from the buffer queue */
302 process_progressive_mode(common);
308 * vpif_channel_isr: It changes status of the displayed buffer, takes next
309 * buffer from the queue and sets its address in VPIF registers
311 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
313 struct vpif_device *dev = &vpif_obj;
314 struct channel_obj *ch;
315 struct common_obj *common;
316 enum v4l2_field field;
320 channel_id = *(int *)(dev_id);
321 ch = dev->dev[channel_id];
322 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
323 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
324 common = &ch->common[i];
325 /* If streaming is started in this channel */
326 if (0 == common->started)
329 if (1 == ch->vpifparams.std_info.frm_fmt) {
330 if (list_empty(&common->dma_queue))
333 /* Progressive mode */
334 if (!channel_first_int[i][channel_id]) {
335 /* Mark status of the cur_frm to
336 * done and unlock semaphore on it */
337 do_gettimeofday(&common->cur_frm->ts);
338 common->cur_frm->state = VIDEOBUF_DONE;
339 wake_up_interruptible(&common->cur_frm->done);
340 /* Make cur_frm pointing to next_frm */
341 common->cur_frm = common->next_frm;
344 channel_first_int[i][channel_id] = 0;
345 process_progressive_mode(common);
347 /* Interlaced mode */
348 /* If it is first interrupt, ignore it */
350 if (channel_first_int[i][channel_id]) {
351 channel_first_int[i][channel_id] = 0;
357 /* Get field id from VPIF registers */
358 fid = vpif_channel_getfid(ch->channel_id + 2);
359 /* If fid does not match with stored field id */
360 if (fid != ch->field_id) {
361 /* Make them in sync */
368 process_interlaced_mode(fid, common);
375 static int vpif_get_std_info(struct channel_obj *ch)
377 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
378 struct video_obj *vid_ch = &ch->video;
379 struct vpif_params *vpifparams = &ch->vpifparams;
380 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
381 const struct vpif_channel_config_params *config;
385 std_info->stdid = vid_ch->stdid;
389 for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
390 config = &ch_params[index];
391 if (config->stdid & std_info->stdid) {
392 memcpy(std_info, config, sizeof(*config));
397 if (index == ARRAY_SIZE(ch_params))
400 common->fmt.fmt.pix.width = std_info->width;
401 common->fmt.fmt.pix.height = std_info->height;
402 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
403 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
405 /* Set height and width paramateres */
406 ch->common[VPIF_VIDEO_INDEX].height = std_info->height;
407 ch->common[VPIF_VIDEO_INDEX].width = std_info->width;
413 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
414 * in the top and bottom field
416 static void vpif_calculate_offsets(struct channel_obj *ch)
418 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
419 struct vpif_params *vpifparams = &ch->vpifparams;
420 enum v4l2_field field = common->fmt.fmt.pix.field;
421 struct video_obj *vid_ch = &ch->video;
422 unsigned int hpitch, vpitch, sizeimage;
424 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
425 if (ch->vpifparams.std_info.frm_fmt)
426 vid_ch->buf_field = V4L2_FIELD_NONE;
428 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
430 vid_ch->buf_field = common->fmt.fmt.pix.field;
433 if (V4L2_MEMORY_USERPTR == common->memory)
434 sizeimage = common->fmt.fmt.pix.sizeimage;
436 sizeimage = config_params.channel_bufsize[ch->channel_id];
438 hpitch = common->fmt.fmt.pix.bytesperline;
439 vpitch = sizeimage / (hpitch * 2);
440 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
441 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
442 common->ytop_off = 0;
443 common->ybtm_off = hpitch;
444 common->ctop_off = sizeimage / 2;
445 common->cbtm_off = sizeimage / 2 + hpitch;
446 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
447 common->ytop_off = 0;
448 common->ybtm_off = sizeimage / 4;
449 common->ctop_off = sizeimage / 2;
450 common->cbtm_off = common->ctop_off + sizeimage / 4;
451 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
452 common->ybtm_off = 0;
453 common->ytop_off = sizeimage / 4;
454 common->cbtm_off = sizeimage / 2;
455 common->ctop_off = common->cbtm_off + sizeimage / 4;
458 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
459 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
460 vpifparams->video_params.storage_mode = 1;
462 vpifparams->video_params.storage_mode = 0;
465 if (ch->vpifparams.std_info.frm_fmt == 1) {
466 vpifparams->video_params.hpitch =
467 common->fmt.fmt.pix.bytesperline;
469 if ((field == V4L2_FIELD_ANY) ||
470 (field == V4L2_FIELD_INTERLACED))
471 vpifparams->video_params.hpitch =
472 common->fmt.fmt.pix.bytesperline * 2;
474 vpifparams->video_params.hpitch =
475 common->fmt.fmt.pix.bytesperline;
478 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
481 static void vpif_config_format(struct channel_obj *ch)
483 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
485 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
486 if (config_params.numbuffers[ch->channel_id] == 0)
487 common->memory = V4L2_MEMORY_USERPTR;
489 common->memory = V4L2_MEMORY_MMAP;
491 common->fmt.fmt.pix.sizeimage =
492 config_params.channel_bufsize[ch->channel_id];
493 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
494 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
497 static int vpif_check_format(struct channel_obj *ch,
498 struct v4l2_pix_format *pixfmt)
500 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
501 enum v4l2_field field = pixfmt->field;
502 u32 sizeimage, hpitch, vpitch;
504 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
505 goto invalid_fmt_exit;
507 if (!(VPIF_VALID_FIELD(field)))
508 goto invalid_fmt_exit;
510 if (pixfmt->bytesperline <= 0)
511 goto invalid_pitch_exit;
513 if (V4L2_MEMORY_USERPTR == common->memory)
514 sizeimage = pixfmt->sizeimage;
516 sizeimage = config_params.channel_bufsize[ch->channel_id];
518 if (vpif_get_std_info(ch)) {
519 vpif_err("Error getting the standard info\n");
523 hpitch = pixfmt->bytesperline;
524 vpitch = sizeimage / (hpitch * 2);
526 /* Check for valid value of pitch */
527 if ((hpitch < ch->vpifparams.std_info.width) ||
528 (vpitch < ch->vpifparams.std_info.height))
529 goto invalid_pitch_exit;
531 /* Check for 8 byte alignment */
532 if (!ISALIGNED(hpitch)) {
533 vpif_err("invalid pitch alignment\n");
536 pixfmt->width = common->fmt.fmt.pix.width;
537 pixfmt->height = common->fmt.fmt.pix.height;
542 vpif_err("invalid field format\n");
546 vpif_err("invalid pitch\n");
550 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
552 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
554 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
555 common->set_addr = ch3_set_videobuf_addr;
558 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
560 common->set_addr = ch2_set_videobuf_addr;
565 * vpif_mmap: It is used to map kernel space buffers into user spaces
567 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
569 struct vpif_fh *fh = filep->private_data;
570 struct common_obj *common = &fh->channel->common[VPIF_VIDEO_INDEX];
572 return videobuf_mmap_mapper(&common->buffer_queue, vma);
576 * vpif_poll: It is used for select/poll system call
578 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
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];
585 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
591 * vpif_open: It creates object of file handle structure and stores it in
592 * private_data member of filepointer
594 static int vpif_open(struct file *filep)
596 struct video_device *vdev = video_devdata(filep);
597 struct channel_obj *ch = NULL;
598 struct vpif_fh *fh = NULL;
600 ch = video_get_drvdata(vdev);
601 /* Allocate memory for the file handle object */
602 fh = kmalloc(sizeof(struct vpif_fh), GFP_KERNEL);
604 vpif_err("unable to allocate memory for file handle object\n");
608 /* store pointer to fh in private_data member of filep */
609 filep->private_data = fh;
612 if (!ch->initialized) {
615 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
618 /* Increment channel usrs counter */
619 atomic_inc(&ch->usrs);
620 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
621 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
622 /* Initialize priority of this instance to default priority */
623 fh->prio = V4L2_PRIORITY_UNSET;
624 v4l2_prio_open(&ch->prio, &fh->prio);
630 * vpif_release: This function deletes buffer queue, frees the buffers and
631 * the vpif file handle
633 static int vpif_release(struct file *filep)
635 struct vpif_fh *fh = filep->private_data;
636 struct channel_obj *ch = fh->channel;
637 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
639 mutex_lock_interruptible(&common->lock);
640 /* if this instance is doing IO */
641 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
642 /* Reset io_usrs member of channel object */
644 /* Disable channel */
645 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
647 channel2_intr_enable(0);
649 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
650 (2 == common->started)) {
652 channel3_intr_enable(0);
655 /* Free buffers allocated */
656 videobuf_queue_cancel(&common->buffer_queue);
657 videobuf_mmap_free(&common->buffer_queue);
659 config_params.numbuffers[ch->channel_id];
662 mutex_unlock(&common->lock);
664 /* Decrement channel usrs counter */
665 atomic_dec(&ch->usrs);
666 /* If this file handle has initialize encoder device, reset it */
670 /* Close the priority */
671 v4l2_prio_close(&ch->prio, &fh->prio);
672 filep->private_data = NULL;
679 /* functions implementing ioctls */
681 static int vpif_querycap(struct file *file, void *priv,
682 struct v4l2_capability *cap)
684 struct vpif_config *config = vpif_dev->platform_data;
686 cap->version = VPIF_DISPLAY_VERSION_CODE;
687 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
688 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
689 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
690 strlcpy(cap->card, config->card_name, sizeof(cap->card));
695 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
696 struct v4l2_fmtdesc *fmt)
698 if (fmt->index != 0) {
699 vpif_err("Invalid format index\n");
703 /* Fill in the information about format */
704 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
705 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
706 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
711 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
712 struct v4l2_format *fmt)
714 struct vpif_fh *fh = priv;
715 struct channel_obj *ch = fh->channel;
716 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
718 /* Check the validity of the buffer type */
719 if (common->fmt.type != fmt->type)
722 /* Fill in the information about format */
723 mutex_lock_interruptible(&common->lock);
724 if (vpif_get_std_info(ch)) {
725 vpif_err("Error getting the standard info\n");
730 mutex_unlock(&common->lock);
734 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
735 struct v4l2_format *fmt)
737 struct vpif_fh *fh = priv;
738 struct v4l2_pix_format *pixfmt;
739 struct channel_obj *ch = fh->channel;
740 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
743 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
744 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
745 if (!fh->initialized) {
746 vpif_dbg(1, debug, "Channel Busy\n");
750 /* Check for the priority */
751 ret = v4l2_prio_check(&ch->prio, &fh->prio);
757 if (common->started) {
758 vpif_dbg(1, debug, "Streaming in progress\n");
762 pixfmt = &fmt->fmt.pix;
763 /* Check for valid field format */
764 ret = vpif_check_format(ch, pixfmt);
768 /* store the pix format in the channel object */
769 common->fmt.fmt.pix = *pixfmt;
770 /* store the format in the channel object */
771 mutex_lock_interruptible(&common->lock);
773 mutex_unlock(&common->lock);
778 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
779 struct v4l2_format *fmt)
781 struct vpif_fh *fh = priv;
782 struct channel_obj *ch = fh->channel;
783 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
784 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
787 ret = vpif_check_format(ch, pixfmt);
789 *pixfmt = common->fmt.fmt.pix;
790 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
796 static int vpif_reqbufs(struct file *file, void *priv,
797 struct v4l2_requestbuffers *reqbuf)
799 struct vpif_fh *fh = priv;
800 struct channel_obj *ch = fh->channel;
801 struct common_obj *common;
802 enum v4l2_field field;
806 /* This file handle has not initialized the channel,
807 It is not allowed to do settings */
808 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
809 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
810 if (!fh->initialized) {
811 vpif_err("Channel Busy\n");
816 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
819 index = VPIF_VIDEO_INDEX;
821 common = &ch->common[index];
822 mutex_lock_interruptible(&common->lock);
823 if (common->fmt.type != reqbuf->type) {
828 if (0 != common->io_usrs) {
833 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
834 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
835 field = V4L2_FIELD_INTERLACED;
837 field = common->fmt.fmt.pix.field;
839 field = V4L2_VBI_INTERLACED;
842 /* Initialize videobuf queue as per the buffer type */
843 videobuf_queue_dma_contig_init(&common->buffer_queue,
847 sizeof(struct videobuf_buffer), fh);
849 /* Set io allowed member of file handle to TRUE */
850 fh->io_allowed[index] = 1;
851 /* Increment io usrs member of channel object to 1 */
853 /* Store type of memory requested in channel object */
854 common->memory = reqbuf->memory;
855 INIT_LIST_HEAD(&common->dma_queue);
857 /* Allocate buffers */
858 ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
861 mutex_unlock(&common->lock);
865 static int vpif_querybuf(struct file *file, void *priv,
866 struct v4l2_buffer *tbuf)
868 struct vpif_fh *fh = priv;
869 struct channel_obj *ch = fh->channel;
870 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
872 if (common->fmt.type != tbuf->type)
875 return videobuf_querybuf(&common->buffer_queue, tbuf);
878 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
881 struct vpif_fh *fh = priv;
882 struct channel_obj *ch = fh->channel;
883 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
884 struct v4l2_buffer tbuf = *buf;
885 struct videobuf_buffer *buf1;
886 unsigned long addr = 0;
890 if (common->fmt.type != tbuf.type)
893 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
894 vpif_err("fh->io_allowed\n");
898 if (!(list_empty(&common->dma_queue)) ||
899 (common->cur_frm != common->next_frm) ||
900 !(common->started) ||
901 (common->started && (0 == ch->field_id)))
902 return videobuf_qbuf(&common->buffer_queue, buf);
904 /* bufferqueue is empty store buffer address in VPIF registers */
905 mutex_lock(&common->buffer_queue.vb_lock);
906 buf1 = common->buffer_queue.bufs[tbuf.index];
907 if (buf1->memory != tbuf.memory) {
908 vpif_err("invalid buffer type\n");
912 if ((buf1->state == VIDEOBUF_QUEUED) ||
913 (buf1->state == VIDEOBUF_ACTIVE)) {
914 vpif_err("invalid state\n");
918 switch (buf1->memory) {
919 case V4L2_MEMORY_MMAP:
920 if (buf1->baddr == 0)
924 case V4L2_MEMORY_USERPTR:
925 if (tbuf.length < buf1->bsize)
928 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
929 && (buf1->baddr != tbuf.m.userptr))
930 vpif_buffer_release(&common->buffer_queue, buf1);
931 buf1->baddr = tbuf.m.userptr;
938 local_irq_save(flags);
939 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
940 common->buffer_queue.field);
942 local_irq_restore(flags);
946 buf1->state = VIDEOBUF_ACTIVE;
948 common->next_frm = buf1;
949 if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
950 common->set_addr((addr + common->ytop_off),
951 (addr + common->ybtm_off),
952 (addr + common->ctop_off),
953 (addr + common->cbtm_off));
956 local_irq_restore(flags);
957 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
958 mutex_unlock(&common->buffer_queue.vb_lock);
962 mutex_unlock(&common->buffer_queue.vb_lock);
966 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
968 struct vpif_fh *fh = priv;
969 struct channel_obj *ch = fh->channel;
970 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
973 if (!(*std_id & DM646X_V4L2_STD))
976 if (common->started) {
977 vpif_err("streaming in progress\n");
981 /* Call encoder subdevice function to set the standard */
982 mutex_lock_interruptible(&common->lock);
984 ch->video.stdid = *std_id;
985 /* Get the information about the standard */
986 if (vpif_get_std_info(ch)) {
987 vpif_err("Error getting the standard info\n");
991 if ((ch->vpifparams.std_info.width *
992 ch->vpifparams.std_info.height * 2) >
993 config_params.channel_bufsize[ch->channel_id]) {
994 vpif_err("invalid std for this size\n");
999 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1000 /* Configure the default format information */
1001 vpif_config_format(ch);
1003 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1004 s_std_output, *std_id);
1006 vpif_err("Failed to set output standard\n");
1010 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1013 vpif_err("Failed to set standard for sub devices\n");
1016 mutex_unlock(&common->lock);
1020 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1022 struct vpif_fh *fh = priv;
1023 struct channel_obj *ch = fh->channel;
1025 *std = ch->video.stdid;
1029 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1031 struct vpif_fh *fh = priv;
1032 struct channel_obj *ch = fh->channel;
1033 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1035 return videobuf_dqbuf(&common->buffer_queue, p,
1036 (file->f_flags & O_NONBLOCK));
1039 static int vpif_streamon(struct file *file, void *priv,
1040 enum v4l2_buf_type buftype)
1042 struct vpif_fh *fh = priv;
1043 struct channel_obj *ch = fh->channel;
1044 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1045 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1046 struct vpif_params *vpif = &ch->vpifparams;
1047 struct vpif_config *vpif_config_data =
1048 vpif_dev->platform_data;
1049 unsigned long addr = 0;
1052 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1053 vpif_err("buffer type not supported\n");
1057 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1058 vpif_err("fh->io_allowed\n");
1062 /* If Streaming is already started, return error */
1063 if (common->started) {
1064 vpif_err("channel->started\n");
1068 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1069 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1070 ch->vpifparams.std_info.ycmux_mode == 0)
1071 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1072 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1073 vpif_err("other channel is using\n");
1077 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1081 /* Call videobuf_streamon to start streaming in videobuf */
1082 ret = videobuf_streamon(&common->buffer_queue);
1084 vpif_err("videobuf_streamon\n");
1088 mutex_lock_interruptible(&common->lock);
1089 /* If buffer queue is empty, return error */
1090 if (list_empty(&common->dma_queue)) {
1091 vpif_err("buffer queue is empty\n");
1096 /* Get the next frame from the buffer queue */
1097 common->next_frm = common->cur_frm =
1098 list_entry(common->dma_queue.next,
1099 struct videobuf_buffer, queue);
1101 list_del(&common->cur_frm->queue);
1102 /* Mark state of the current frame to active */
1103 common->cur_frm->state = VIDEOBUF_ACTIVE;
1105 /* Initialize field_id and started member */
1107 common->started = 1;
1108 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1109 addr = common->cur_frm->boff;
1110 /* Calculate the offset for Y and C data in the buffer */
1111 vpif_calculate_offsets(ch);
1113 if ((ch->vpifparams.std_info.frm_fmt &&
1114 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1115 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1116 || (!ch->vpifparams.std_info.frm_fmt
1117 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1118 vpif_err("conflict in field format and std format\n");
1123 /* clock settings */
1125 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1126 ch->vpifparams.std_info.hd_sd);
1128 vpif_err("can't set clock\n");
1132 /* set the parameters and addresses */
1133 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1137 common->started = ret;
1138 vpif_config_addr(ch, ret);
1139 common->set_addr((addr + common->ytop_off),
1140 (addr + common->ybtm_off),
1141 (addr + common->ctop_off),
1142 (addr + common->cbtm_off));
1144 /* Set interrupt for both the fields in VPIF
1145 Register enable channel in VPIF register */
1146 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1147 channel2_intr_assert();
1148 channel2_intr_enable(1);
1152 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1153 || (common->started == 2)) {
1154 channel3_intr_assert();
1155 channel3_intr_enable(1);
1158 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1162 mutex_unlock(&common->lock);
1166 static int vpif_streamoff(struct file *file, void *priv,
1167 enum v4l2_buf_type buftype)
1169 struct vpif_fh *fh = priv;
1170 struct channel_obj *ch = fh->channel;
1171 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1173 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1174 vpif_err("buffer type not supported\n");
1178 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1179 vpif_err("fh->io_allowed\n");
1183 if (!common->started) {
1184 vpif_err("channel->started\n");
1188 mutex_lock_interruptible(&common->lock);
1189 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1190 /* disable channel */
1191 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1193 channel2_intr_enable(0);
1195 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1196 (2 == common->started)) {
1198 channel3_intr_enable(0);
1202 common->started = 0;
1203 mutex_unlock(&common->lock);
1205 return videobuf_streamoff(&common->buffer_queue);
1208 static int vpif_cropcap(struct file *file, void *priv,
1209 struct v4l2_cropcap *crop)
1211 struct vpif_fh *fh = priv;
1212 struct channel_obj *ch = fh->channel;
1213 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1214 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1217 crop->bounds.left = crop->bounds.top = 0;
1218 crop->defrect.left = crop->defrect.top = 0;
1219 crop->defrect.height = crop->bounds.height = common->height;
1220 crop->defrect.width = crop->bounds.width = common->width;
1225 static int vpif_enum_output(struct file *file, void *fh,
1226 struct v4l2_output *output)
1229 struct vpif_config *config = vpif_dev->platform_data;
1231 if (output->index >= config->output_count) {
1232 vpif_dbg(1, debug, "Invalid output index\n");
1236 strcpy(output->name, config->output[output->index]);
1237 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1238 output->std = DM646X_V4L2_STD;
1243 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1245 struct vpif_fh *fh = priv;
1246 struct channel_obj *ch = fh->channel;
1247 struct video_obj *vid_ch = &ch->video;
1248 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1251 mutex_lock_interruptible(&common->lock);
1252 if (common->started) {
1253 vpif_err("Streaming in progress\n");
1258 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1259 s_routing, 0, i, 0);
1262 vpif_err("Failed to set output standard\n");
1264 vid_ch->output_id = i;
1267 mutex_unlock(&common->lock);
1271 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1273 struct vpif_fh *fh = priv;
1274 struct channel_obj *ch = fh->channel;
1275 struct video_obj *vid_ch = &ch->video;
1277 *i = vid_ch->output_id;
1282 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1284 struct vpif_fh *fh = priv;
1285 struct channel_obj *ch = fh->channel;
1287 *p = v4l2_prio_max(&ch->prio);
1292 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1294 struct vpif_fh *fh = priv;
1295 struct channel_obj *ch = fh->channel;
1297 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1300 /* vpif display ioctl operations */
1301 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1302 .vidioc_querycap = vpif_querycap,
1303 .vidioc_g_priority = vpif_g_priority,
1304 .vidioc_s_priority = vpif_s_priority,
1305 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1306 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1307 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1308 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1309 .vidioc_reqbufs = vpif_reqbufs,
1310 .vidioc_querybuf = vpif_querybuf,
1311 .vidioc_qbuf = vpif_qbuf,
1312 .vidioc_dqbuf = vpif_dqbuf,
1313 .vidioc_streamon = vpif_streamon,
1314 .vidioc_streamoff = vpif_streamoff,
1315 .vidioc_s_std = vpif_s_std,
1316 .vidioc_g_std = vpif_g_std,
1317 .vidioc_enum_output = vpif_enum_output,
1318 .vidioc_s_output = vpif_s_output,
1319 .vidioc_g_output = vpif_g_output,
1320 .vidioc_cropcap = vpif_cropcap,
1323 static const struct v4l2_file_operations vpif_fops = {
1324 .owner = THIS_MODULE,
1326 .release = vpif_release,
1327 .ioctl = video_ioctl2,
1332 static struct video_device vpif_video_template = {
1336 .ioctl_ops = &vpif_ioctl_ops,
1337 .tvnorms = DM646X_V4L2_STD,
1338 .current_norm = V4L2_STD_625_50,
1342 /*Configure the channels, buffer sizei, request irq */
1343 static int initialize_vpif(void)
1345 int free_channel_objects_index;
1346 int free_buffer_channel_index;
1347 int free_buffer_index;
1350 /* Default number of buffers should be 3 */
1351 if ((ch2_numbuffers > 0) &&
1352 (ch2_numbuffers < config_params.min_numbuffers))
1353 ch2_numbuffers = config_params.min_numbuffers;
1354 if ((ch3_numbuffers > 0) &&
1355 (ch3_numbuffers < config_params.min_numbuffers))
1356 ch3_numbuffers = config_params.min_numbuffers;
1358 /* Set buffer size to min buffers size if invalid buffer size is
1360 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1362 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1363 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1365 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1367 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1369 if (ch2_numbuffers) {
1370 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1373 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1375 if (ch3_numbuffers) {
1376 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1380 /* Allocate memory for six channel objects */
1381 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1383 kmalloc(sizeof(struct channel_obj), GFP_KERNEL);
1384 /* If memory allocation fails, return error */
1385 if (!vpif_obj.dev[i]) {
1386 free_channel_objects_index = i;
1388 goto vpif_init_free_channel_objects;
1392 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1393 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1394 free_buffer_index = config_params.numbuffers[i - 1];
1398 vpif_init_free_channel_objects:
1399 for (j = 0; j < free_channel_objects_index; j++)
1400 kfree(vpif_obj.dev[j]);
1405 * vpif_probe: This function creates device entries by register itself to the
1406 * V4L2 driver and initializes fields of each channel objects
1408 static __init int vpif_probe(struct platform_device *pdev)
1410 const struct subdev_info *subdevdata;
1411 int i, j = 0, k, q, m, err = 0;
1412 struct i2c_adapter *i2c_adap;
1413 struct vpif_config *config;
1414 struct common_obj *common;
1415 struct channel_obj *ch;
1416 struct video_device *vfd;
1417 struct resource *res;
1420 vpif_dev = &pdev->dev;
1421 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1423 v4l2_err(vpif_dev->driver,
1424 "Error getting platform resource\n");
1428 if (!request_mem_region(res->start, res->end - res->start + 1,
1429 vpif_dev->driver->name)) {
1430 v4l2_err(vpif_dev->driver, "VPIF: failed request_mem_region\n");
1434 vpif_base = ioremap_nocache(res->start, res->end - res->start + 1);
1436 v4l2_err(vpif_dev->driver, "Unable to ioremap VPIF reg\n");
1441 vpif_base_addr_init(vpif_base);
1445 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1447 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1452 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1453 for (i = res->start; i <= res->end; i++) {
1454 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1456 (void *)(&vpif_obj.dev[k]->channel_id))) {
1464 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1466 /* Get the pointer to the channel object */
1467 ch = vpif_obj.dev[i];
1469 /* Allocate memory for video device */
1470 vfd = video_device_alloc();
1472 for (j = 0; j < i; j++) {
1473 ch = vpif_obj.dev[j];
1474 video_device_release(ch->video_dev);
1477 goto video_dev_alloc_exit;
1480 /* Initialize field of video device */
1481 *vfd = vpif_video_template;
1482 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1483 vfd->release = video_device_release;
1484 snprintf(vfd->name, sizeof(vfd->name),
1485 "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
1486 (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
1487 (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
1488 (VPIF_DISPLAY_VERSION_CODE) & 0xff);
1490 /* Set video_dev to the video device */
1491 ch->video_dev = vfd;
1494 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1495 ch = vpif_obj.dev[j];
1496 /* Initialize field of the channel objects */
1497 atomic_set(&ch->usrs, 0);
1498 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1499 ch->common[k].numbuffers = 0;
1500 common = &ch->common[k];
1501 common->io_usrs = 0;
1502 common->started = 0;
1503 spin_lock_init(&common->irqlock);
1504 mutex_init(&common->lock);
1505 common->numbuffers = 0;
1506 common->set_addr = NULL;
1507 common->ytop_off = common->ybtm_off = 0;
1508 common->ctop_off = common->cbtm_off = 0;
1509 common->cur_frm = common->next_frm = NULL;
1510 memset(&common->fmt, 0, sizeof(common->fmt));
1511 common->numbuffers = config_params.numbuffers[k];
1514 ch->initialized = 0;
1517 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1518 config_params.numbuffers[ch->channel_id];
1520 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1522 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1524 /* Initialize prio member of channel object */
1525 v4l2_prio_init(&ch->prio);
1526 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1527 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1529 /* register video device */
1530 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1531 (int)ch, (int)&ch->video_dev);
1533 err = video_register_device(ch->video_dev,
1534 VFL_TYPE_GRABBER, (j ? 3 : 2));
1538 video_set_drvdata(ch->video_dev, ch);
1541 i2c_adap = i2c_get_adapter(1);
1542 config = pdev->dev.platform_data;
1543 subdev_count = config->subdev_count;
1544 subdevdata = config->subdevinfo;
1545 vpif_obj.sd = kmalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1547 if (vpif_obj.sd == NULL) {
1548 vpif_err("unable to allocate memory for subdevice pointers\n");
1553 for (i = 0; i < subdev_count; i++) {
1554 vpif_obj.sd[i] = v4l2_i2c_new_probed_subdev(&vpif_obj.v4l2_dev,
1555 i2c_adap, subdevdata[i].name,
1557 &subdevdata[i].addr);
1558 if (!vpif_obj.sd[i]) {
1559 vpif_err("Error registering v4l2 subdevice\n");
1560 goto probe_subdev_out;
1564 vpif_obj.sd[i]->grp_id = 1 << i;
1572 for (k = 0; k < j; k++) {
1573 ch = vpif_obj.dev[k];
1574 video_unregister_device(ch->video_dev);
1575 video_device_release(ch->video_dev);
1576 ch->video_dev = NULL;
1579 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1580 vpif_err("VPIF IRQ request failed\n");
1581 for (q = k; k >= 0; k--) {
1582 for (m = i; m >= res->start; m--)
1583 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1584 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1587 video_dev_alloc_exit:
1590 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1591 release_mem_region(res->start, res->end - res->start + 1);
1597 * vpif_remove: It un-register channels from V4L2 driver
1599 static int vpif_remove(struct platform_device *device)
1601 struct channel_obj *ch;
1604 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1606 /* un-register device */
1607 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1608 /* Get the pointer to the channel object */
1609 ch = vpif_obj.dev[i];
1610 /* Unregister video device */
1611 video_unregister_device(ch->video_dev);
1613 ch->video_dev = NULL;
1619 static struct platform_driver vpif_driver = {
1621 .name = "vpif_display",
1622 .owner = THIS_MODULE,
1624 .probe = vpif_probe,
1625 .remove = vpif_remove,
1628 static __init int vpif_init(void)
1630 return platform_driver_register(&vpif_driver);
1634 * vpif_cleanup: This function un-registers device and driver to the kernel,
1635 * frees requested irq handler and de-allocates memory allocated for channel
1638 static void vpif_cleanup(void)
1640 struct platform_device *pdev;
1641 struct resource *res;
1645 pdev = container_of(vpif_dev, struct platform_device, dev);
1647 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1648 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1650 (void *)(&vpif_obj.dev[i]->channel_id));
1655 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1656 release_mem_region(res->start, res->end - res->start + 1);
1657 platform_driver_unregister(&vpif_driver);
1659 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1660 kfree(vpif_obj.dev[i]);
1663 module_init(vpif_init);
1664 module_exit(vpif_cleanup);