[media] vpif_cap/disp: Cleanup, improved comments
[pandora-kernel.git] / drivers / media / video / davinci / vpif_display.c
1 /*
2  * vpif-display - VPIF display driver
3  * Display driver for TI DaVinci VPIF
4  *
5  * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
6  *
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.
10  *
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.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/fs.h>
22 #include <linux/mm.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/string.h>
26 #include <linux/videodev2.h>
27 #include <linux/wait.h>
28 #include <linux/time.h>
29 #include <linux/i2c.h>
30 #include <linux/platform_device.h>
31 #include <linux/io.h>
32 #include <linux/version.h>
33 #include <linux/slab.h>
34
35 #include <asm/irq.h>
36 #include <asm/page.h>
37
38 #include <media/adv7343.h>
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-ioctl.h>
41 #include <media/v4l2-chip-ident.h>
42
43 #include <mach/dm646x.h>
44
45 #include "vpif_display.h"
46 #include "vpif.h"
47
48 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
49 MODULE_LICENSE("GPL");
50
51 #define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
52
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)
56
57 static int debug = 1;
58 static u32 ch2_numbuffers = 3;
59 static u32 ch3_numbuffers = 3;
60 static u32 ch2_bufsize = 1920 * 1080 * 2;
61 static u32 ch3_bufsize = 720 * 576 * 2;
62
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);
68
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)");
74
75 static struct vpif_config_params config_params = {
76         .min_numbuffers         = 3,
77         .numbuffers[0]          = 3,
78         .numbuffers[1]          = 3,
79         .min_bufsize[0]         = 720 * 480 * 2,
80         .min_bufsize[1]         = 720 * 480 * 2,
81         .channel_bufsize[0]     = 1920 * 1080 * 2,
82         .channel_bufsize[1]     = 720 * 576 * 2,
83 };
84
85 static struct vpif_device vpif_obj = { {NULL} };
86 static struct device *vpif_dev;
87
88 /*
89  * vpif_uservirt_to_phys: This function is used to convert user
90  * space virtual address to physical address.
91  */
92 static u32 vpif_uservirt_to_phys(u32 virtp)
93 {
94         struct mm_struct *mm = current->mm;
95         unsigned long physp = 0;
96         struct vm_area_struct *vma;
97
98         vma = find_vma(mm, virtp);
99
100         /* For kernel direct-mapped memory, take the easy way */
101         if (virtp >= PAGE_OFFSET) {
102                 physp = virt_to_phys((void *)virtp);
103         } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
104                 /* this will catch, kernel-allocated, mmaped-to-usermode addr */
105                 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
106         } else {
107                 /* otherwise, use get_user_pages() for general userland pages */
108                 int res, nr_pages = 1;
109                 struct page *pages;
110                 down_read(&current->mm->mmap_sem);
111
112                 res = get_user_pages(current, current->mm,
113                                      virtp, nr_pages, 1, 0, &pages, NULL);
114                 up_read(&current->mm->mmap_sem);
115
116                 if (res == nr_pages) {
117                         physp = __pa(page_address(&pages[0]) +
118                                                         (virtp & ~PAGE_MASK));
119                 } else {
120                         vpif_err("get_user_pages failed\n");
121                         return 0;
122                 }
123         }
124
125         return physp;
126 }
127
128 /*
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
132  */
133 static int vpif_buffer_prepare(struct videobuf_queue *q,
134                                struct videobuf_buffer *vb,
135                                enum v4l2_field field)
136 {
137         struct vpif_fh *fh = q->priv_data;
138         struct common_obj *common;
139         unsigned long addr;
140
141         common = &fh->channel->common[VPIF_VIDEO_INDEX];
142         if (VIDEOBUF_NEEDS_INIT == vb->state) {
143                 vb->width       = common->width;
144                 vb->height      = common->height;
145                 vb->size        = vb->width * vb->height;
146                 vb->field       = field;
147         }
148         vb->state = VIDEOBUF_PREPARED;
149
150         /* if user pointer memory mechanism is used, get the physical
151          * address of the buffer */
152         if (V4L2_MEMORY_USERPTR == common->memory) {
153                 if (!vb->baddr) {
154                         vpif_err("buffer_address is 0\n");
155                         return -EINVAL;
156                 }
157
158                 vb->boff = vpif_uservirt_to_phys(vb->baddr);
159                 if (!ISALIGNED(vb->boff))
160                         goto buf_align_exit;
161         }
162
163         addr = vb->boff;
164         if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
165                 if (!ISALIGNED(addr + common->ytop_off) ||
166                     !ISALIGNED(addr + common->ybtm_off) ||
167                     !ISALIGNED(addr + common->ctop_off) ||
168                     !ISALIGNED(addr + common->cbtm_off))
169                         goto buf_align_exit;
170         }
171         return 0;
172
173 buf_align_exit:
174         vpif_err("buffer offset not aligned to 8 bytes\n");
175         return -EINVAL;
176 }
177
178 /*
179  * vpif_buffer_setup: This function allocates memory for the buffers
180  */
181 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
182                                 unsigned int *size)
183 {
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];
187
188         if (V4L2_MEMORY_MMAP != common->memory)
189                 return 0;
190
191         *size = config_params.channel_bufsize[ch->channel_id];
192         if (*count < config_params.min_numbuffers)
193                 *count = config_params.min_numbuffers;
194
195         return 0;
196 }
197
198 /*
199  * vpif_buffer_queue: This function adds the buffer to DMA queue
200  */
201 static void vpif_buffer_queue(struct videobuf_queue *q,
202                               struct videobuf_buffer *vb)
203 {
204         struct vpif_fh *fh = q->priv_data;
205         struct common_obj *common;
206
207         common = &fh->channel->common[VPIF_VIDEO_INDEX];
208
209         /* add the buffer to the DMA queue */
210         list_add_tail(&vb->queue, &common->dma_queue);
211         vb->state = VIDEOBUF_QUEUED;
212 }
213
214 /*
215  * vpif_buffer_release: This function is called from the videobuf layer to
216  * free memory allocated to the buffers
217  */
218 static void vpif_buffer_release(struct videobuf_queue *q,
219                                 struct videobuf_buffer *vb)
220 {
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;
225
226         common = &ch->common[VPIF_VIDEO_INDEX];
227
228         videobuf_dma_contig_free(q, vb);
229         vb->state = VIDEOBUF_NEEDS_INIT;
230
231         if (V4L2_MEMORY_MMAP != common->memory)
232                 return;
233
234         buf_size = config_params.channel_bufsize[ch->channel_id];
235 }
236
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,
242 };
243 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
244
245 static void process_progressive_mode(struct common_obj *common)
246 {
247         unsigned long addr = 0;
248
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;
256
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);
263 }
264
265 static void process_interlaced_mode(int fid, struct common_obj *common)
266 {
267         /* device field id and local field id are in sync */
268         /* If this is even field */
269         if (0 == fid) {
270                 if (common->cur_frm == common->next_frm)
271                         return;
272
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;
283
284         } else if (1 == fid) {  /* odd field */
285                 if (list_empty(&common->dma_queue)
286                     || (common->cur_frm != common->next_frm)) {
287                         return;
288                 }
289                 /* one field is displayed configure the next
290                  * frame if it is available else hold on current
291                  * frame */
292                 /* Get next from the buffer queue */
293                 process_progressive_mode(common);
294
295         }
296 }
297
298 /*
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
301  */
302 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
303 {
304         struct vpif_device *dev = &vpif_obj;
305         struct channel_obj *ch;
306         struct common_obj *common;
307         enum v4l2_field field;
308         int fid = -1, i;
309         int channel_id = 0;
310
311         channel_id = *(int *)(dev_id);
312         ch = dev->dev[channel_id];
313         field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
314         for (i = 0; i < VPIF_NUMOBJECTS; i++) {
315                 common = &ch->common[i];
316                 /* If streaming is started in this channel */
317                 if (0 == common->started)
318                         continue;
319
320                 if (1 == ch->vpifparams.std_info.frm_fmt) {
321                         if (list_empty(&common->dma_queue))
322                                 continue;
323
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;
333                         }
334
335                         channel_first_int[i][channel_id] = 0;
336                         process_progressive_mode(common);
337                 } else {
338                         /* Interlaced mode */
339                         /* If it is first interrupt, ignore it */
340
341                         if (channel_first_int[i][channel_id]) {
342                                 channel_first_int[i][channel_id] = 0;
343                                 continue;
344                         }
345
346                         if (0 == i) {
347                                 ch->field_id ^= 1;
348                                 /* Get field id from VPIF registers */
349                                 fid = vpif_channel_getfid(ch->channel_id + 2);
350                                 /* If fid does not match with stored field id */
351                                 if (fid != ch->field_id) {
352                                         /* Make them in sync */
353                                         if (0 == fid)
354                                                 ch->field_id = fid;
355
356                                         return IRQ_HANDLED;
357                                 }
358                         }
359                         process_interlaced_mode(fid, common);
360                 }
361         }
362
363         return IRQ_HANDLED;
364 }
365
366 static int vpif_update_std_info(struct channel_obj *ch)
367 {
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;
372
373         int i;
374
375         for (i = 0; i < vpif_ch_params_count; i++) {
376                 config = &ch_params[i];
377                 if (config->hd_sd == 0) {
378                         vpif_dbg(2, debug, "SD format\n");
379                         if (config->stdid & vid_ch->stdid) {
380                                 memcpy(std_info, config, sizeof(*config));
381                                 break;
382                         }
383                 } else {
384                         vpif_dbg(2, debug, "HD format\n");
385                         if (config->dv_preset == vid_ch->dv_preset) {
386                                 memcpy(std_info, config, sizeof(*config));
387                                 break;
388                         }
389                 }
390         }
391
392         if (i == vpif_ch_params_count) {
393                 vpif_dbg(1, debug, "Format not found\n");
394                 return -EINVAL;
395         }
396
397         return 0;
398 }
399
400 static int vpif_update_resolution(struct channel_obj *ch)
401 {
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;
406
407         if (!vid_ch->stdid && !vid_ch->dv_preset && !vid_ch->bt_timings.height)
408                 return -EINVAL;
409
410         if (vid_ch->stdid || vid_ch->dv_preset) {
411                 if (vpif_update_std_info(ch))
412                         return -EINVAL;
413         }
414
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);
419
420         /* Set height and width paramateres */
421         common->height = std_info->height;
422         common->width = std_info->width;
423
424         return 0;
425 }
426
427 /*
428  * vpif_calculate_offsets: This function calculates buffers offset for Y and C
429  * in the top and bottom field
430  */
431 static void vpif_calculate_offsets(struct channel_obj *ch)
432 {
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;
438
439         if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
440                 if (ch->vpifparams.std_info.frm_fmt)
441                         vid_ch->buf_field = V4L2_FIELD_NONE;
442                 else
443                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
444         } else {
445                 vid_ch->buf_field = common->fmt.fmt.pix.field;
446         }
447
448         if (V4L2_MEMORY_USERPTR == common->memory)
449                 sizeimage = common->fmt.fmt.pix.sizeimage;
450         else
451                 sizeimage = config_params.channel_bufsize[ch->channel_id];
452
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;
471         }
472
473         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
474             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
475                 vpifparams->video_params.storage_mode = 1;
476         } else {
477                 vpifparams->video_params.storage_mode = 0;
478         }
479
480         if (ch->vpifparams.std_info.frm_fmt == 1) {
481                 vpifparams->video_params.hpitch =
482                     common->fmt.fmt.pix.bytesperline;
483         } else {
484                 if ((field == V4L2_FIELD_ANY) ||
485                         (field == V4L2_FIELD_INTERLACED))
486                         vpifparams->video_params.hpitch =
487                             common->fmt.fmt.pix.bytesperline * 2;
488                 else
489                         vpifparams->video_params.hpitch =
490                             common->fmt.fmt.pix.bytesperline;
491         }
492
493         ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
494 }
495
496 static void vpif_config_format(struct channel_obj *ch)
497 {
498         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
499
500         common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
501         if (config_params.numbuffers[ch->channel_id] == 0)
502                 common->memory = V4L2_MEMORY_USERPTR;
503         else
504                 common->memory = V4L2_MEMORY_MMAP;
505
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;
510 }
511
512 static int vpif_check_format(struct channel_obj *ch,
513                              struct v4l2_pix_format *pixfmt)
514 {
515         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
516         enum v4l2_field field = pixfmt->field;
517         u32 sizeimage, hpitch, vpitch;
518
519         if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
520                 goto invalid_fmt_exit;
521
522         if (!(VPIF_VALID_FIELD(field)))
523                 goto invalid_fmt_exit;
524
525         if (pixfmt->bytesperline <= 0)
526                 goto invalid_pitch_exit;
527
528         if (V4L2_MEMORY_USERPTR == common->memory)
529                 sizeimage = pixfmt->sizeimage;
530         else
531                 sizeimage = config_params.channel_bufsize[ch->channel_id];
532
533         if (vpif_update_resolution(ch))
534                 return -EINVAL;
535
536         hpitch = pixfmt->bytesperline;
537         vpitch = sizeimage / (hpitch * 2);
538
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;
543
544         /* Check for 8 byte alignment */
545         if (!ISALIGNED(hpitch)) {
546                 vpif_err("invalid pitch alignment\n");
547                 return -EINVAL;
548         }
549         pixfmt->width = common->fmt.fmt.pix.width;
550         pixfmt->height = common->fmt.fmt.pix.height;
551
552         return 0;
553
554 invalid_fmt_exit:
555         vpif_err("invalid field format\n");
556         return -EINVAL;
557
558 invalid_pitch_exit:
559         vpif_err("invalid pitch\n");
560         return -EINVAL;
561 }
562
563 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
564 {
565         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
566
567         if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
568                 common->set_addr = ch3_set_videobuf_addr;
569         } else {
570                 if (2 == muxmode)
571                         common->set_addr = ch2_set_videobuf_addr_yc_nmux;
572                 else
573                         common->set_addr = ch2_set_videobuf_addr;
574         }
575 }
576
577 /*
578  * vpif_mmap: It is used to map kernel space buffers into user spaces
579  */
580 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
581 {
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]);
585
586         vpif_dbg(2, debug, "vpif_mmap\n");
587
588         return videobuf_mmap_mapper(&common->buffer_queue, vma);
589 }
590
591 /*
592  * vpif_poll: It is used for select/poll system call
593  */
594 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
595 {
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];
599
600         if (common->started)
601                 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
602
603         return 0;
604 }
605
606 /*
607  * vpif_open: It creates object of file handle structure and stores it in
608  * private_data member of filepointer
609  */
610 static int vpif_open(struct file *filep)
611 {
612         struct video_device *vdev = video_devdata(filep);
613         struct channel_obj *ch = NULL;
614         struct vpif_fh *fh = NULL;
615
616         ch = video_get_drvdata(vdev);
617         /* Allocate memory for the file handle object */
618         fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
619         if (fh == NULL) {
620                 vpif_err("unable to allocate memory for file handle object\n");
621                 return -ENOMEM;
622         }
623
624         /* store pointer to fh in private_data member of filep */
625         filep->private_data = fh;
626         fh->channel = ch;
627         fh->initialized = 0;
628         if (!ch->initialized) {
629                 fh->initialized = 1;
630                 ch->initialized = 1;
631                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
632         }
633
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);
641
642         return 0;
643 }
644
645 /*
646  * vpif_release: This function deletes buffer queue, frees the buffers and
647  * the vpif file handle
648  */
649 static int vpif_release(struct file *filep)
650 {
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];
654
655         if (mutex_lock_interruptible(&common->lock))
656                 return -ERESTARTSYS;
657
658         /* if this instance is doing IO */
659         if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
660                 /* Reset io_usrs member of channel object */
661                 common->io_usrs = 0;
662                 /* Disable channel */
663                 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
664                         enable_channel2(0);
665                         channel2_intr_enable(0);
666                 }
667                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
668                     (2 == common->started)) {
669                         enable_channel3(0);
670                         channel3_intr_enable(0);
671                 }
672                 common->started = 0;
673                 /* Free buffers allocated */
674                 videobuf_queue_cancel(&common->buffer_queue);
675                 videobuf_mmap_free(&common->buffer_queue);
676                 common->numbuffers =
677                     config_params.numbuffers[ch->channel_id];
678         }
679
680         mutex_unlock(&common->lock);
681
682         /* Decrement channel usrs counter */
683         atomic_dec(&ch->usrs);
684         /* If this file handle has initialize encoder device, reset it */
685         if (fh->initialized)
686                 ch->initialized = 0;
687
688         /* Close the priority */
689         v4l2_prio_close(&ch->prio, fh->prio);
690         filep->private_data = NULL;
691         fh->initialized = 0;
692         kfree(fh);
693
694         return 0;
695 }
696
697 /* functions implementing ioctls */
698 /**
699  * vpif_querycap() - QUERYCAP handler
700  * @file: file ptr
701  * @priv: file handle
702  * @cap: ptr to v4l2_capability structure
703  */
704 static int vpif_querycap(struct file *file, void  *priv,
705                                 struct v4l2_capability *cap)
706 {
707         struct vpif_display_config *config = vpif_dev->platform_data;
708
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));
714
715         return 0;
716 }
717
718 static int vpif_enum_fmt_vid_out(struct file *file, void  *priv,
719                                         struct v4l2_fmtdesc *fmt)
720 {
721         if (fmt->index != 0) {
722                 vpif_err("Invalid format index\n");
723                 return -EINVAL;
724         }
725
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;
730
731         return 0;
732 }
733
734 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
735                                 struct v4l2_format *fmt)
736 {
737         struct vpif_fh *fh = priv;
738         struct channel_obj *ch = fh->channel;
739         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
740         int ret = 0;
741
742         /* Check the validity of the buffer type */
743         if (common->fmt.type != fmt->type)
744                 return -EINVAL;
745
746         /* Fill in the information about format */
747         if (mutex_lock_interruptible(&common->lock))
748                 return -ERESTARTSYS;
749
750         if (vpif_update_resolution(ch))
751                 ret = -EINVAL;
752         else
753                 *fmt = common->fmt;
754
755         mutex_unlock(&common->lock);
756
757         return ret;
758 }
759
760 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
761                                 struct v4l2_format *fmt)
762 {
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];
767         int ret = 0;
768
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");
773                         return -EBUSY;
774                 }
775
776                 /* Check for the priority */
777                 ret = v4l2_prio_check(&ch->prio, fh->prio);
778                 if (0 != ret)
779                         return ret;
780                 fh->initialized = 1;
781         }
782
783         if (common->started) {
784                 vpif_dbg(1, debug, "Streaming in progress\n");
785                 return -EBUSY;
786         }
787
788         pixfmt = &fmt->fmt.pix;
789         /* Check for valid field format */
790         ret = vpif_check_format(ch, pixfmt);
791         if (ret)
792                 return ret;
793
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))
798                 return -ERESTARTSYS;
799
800         common->fmt = *fmt;
801         mutex_unlock(&common->lock);
802
803         return 0;
804 }
805
806 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
807                                 struct v4l2_format *fmt)
808 {
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;
813         int ret = 0;
814
815         ret = vpif_check_format(ch, pixfmt);
816         if (ret) {
817                 *pixfmt = common->fmt.fmt.pix;
818                 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
819         }
820
821         return ret;
822 }
823
824 static int vpif_reqbufs(struct file *file, void *priv,
825                         struct v4l2_requestbuffers *reqbuf)
826 {
827         struct vpif_fh *fh = priv;
828         struct channel_obj *ch = fh->channel;
829         struct common_obj *common;
830         enum v4l2_field field;
831         u8 index = 0;
832         int ret = 0;
833
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");
840                         return -EBUSY;
841                 }
842         }
843
844         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
845                 return -EINVAL;
846
847         index = VPIF_VIDEO_INDEX;
848
849         common = &ch->common[index];
850         if (mutex_lock_interruptible(&common->lock))
851                 return -ERESTARTSYS;
852
853         if (common->fmt.type != reqbuf->type) {
854                 ret = -EINVAL;
855                 goto reqbuf_exit;
856         }
857
858         if (0 != common->io_usrs) {
859                 ret = -EBUSY;
860                 goto reqbuf_exit;
861         }
862
863         if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
864                 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
865                         field = V4L2_FIELD_INTERLACED;
866                 else
867                         field = common->fmt.fmt.pix.field;
868         } else {
869                 field = V4L2_VBI_INTERLACED;
870         }
871
872         /* Initialize videobuf queue as per the buffer type */
873         videobuf_queue_dma_contig_init(&common->buffer_queue,
874                                             &video_qops, NULL,
875                                             &common->irqlock,
876                                             reqbuf->type, field,
877                                             sizeof(struct videobuf_buffer), fh,
878                                             NULL);
879
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 */
883         common->io_usrs = 1;
884         /* Store type of memory requested in channel object */
885         common->memory = reqbuf->memory;
886         INIT_LIST_HEAD(&common->dma_queue);
887
888         /* Allocate buffers */
889         ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
890
891 reqbuf_exit:
892         mutex_unlock(&common->lock);
893         return ret;
894 }
895
896 static int vpif_querybuf(struct file *file, void *priv,
897                                 struct v4l2_buffer *tbuf)
898 {
899         struct vpif_fh *fh = priv;
900         struct channel_obj *ch = fh->channel;
901         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
902
903         if (common->fmt.type != tbuf->type)
904                 return -EINVAL;
905
906         return videobuf_querybuf(&common->buffer_queue, tbuf);
907 }
908
909 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
910 {
911
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;
918         unsigned long flags;
919         int ret = 0;
920
921         if (common->fmt.type != tbuf.type)
922                 return -EINVAL;
923
924         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
925                 vpif_err("fh->io_allowed\n");
926                 return -EACCES;
927         }
928
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);
934
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");
940                 goto qbuf_exit;
941         }
942
943         if ((buf1->state == VIDEOBUF_QUEUED) ||
944             (buf1->state == VIDEOBUF_ACTIVE)) {
945                 vpif_err("invalid state\n");
946                 goto qbuf_exit;
947         }
948
949         switch (buf1->memory) {
950         case V4L2_MEMORY_MMAP:
951                 if (buf1->baddr == 0)
952                         goto qbuf_exit;
953                 break;
954
955         case V4L2_MEMORY_USERPTR:
956                 if (tbuf.length < buf1->bsize)
957                         goto qbuf_exit;
958
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;
963                 }
964                 break;
965
966         default:
967                 goto qbuf_exit;
968         }
969
970         local_irq_save(flags);
971         ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
972                                         common->buffer_queue.field);
973         if (ret < 0) {
974                 local_irq_restore(flags);
975                 goto qbuf_exit;
976         }
977
978         buf1->state = VIDEOBUF_ACTIVE;
979         addr = buf1->boff;
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));
986         }
987
988         local_irq_restore(flags);
989         list_add_tail(&buf1->stream, &common->buffer_queue.stream);
990         mutex_unlock(&common->buffer_queue.vb_lock);
991         return 0;
992
993 qbuf_exit:
994         mutex_unlock(&common->buffer_queue.vb_lock);
995         return -EINVAL;
996 }
997
998 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
999 {
1000         struct vpif_fh *fh = priv;
1001         struct channel_obj *ch = fh->channel;
1002         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1003         int ret = 0;
1004
1005         if (!(*std_id & DM646X_V4L2_STD))
1006                 return -EINVAL;
1007
1008         if (common->started) {
1009                 vpif_err("streaming in progress\n");
1010                 return -EBUSY;
1011         }
1012
1013         /* Call encoder subdevice function to set the standard */
1014         if (mutex_lock_interruptible(&common->lock))
1015                 return -ERESTARTSYS;
1016
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));
1020
1021         /* Get the information about the standard */
1022         if (vpif_update_resolution(ch)) {
1023                 ret = -EINVAL;
1024                 goto s_std_exit;
1025         }
1026
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");
1031                 ret = -EINVAL;
1032                 goto s_std_exit;
1033         }
1034
1035         common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1036         /* Configure the default format information */
1037         vpif_config_format(ch);
1038
1039         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1040                                                 s_std_output, *std_id);
1041         if (ret < 0) {
1042                 vpif_err("Failed to set output standard\n");
1043                 goto s_std_exit;
1044         }
1045
1046         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1047                                                         s_std, *std_id);
1048         if (ret < 0)
1049                 vpif_err("Failed to set standard for sub devices\n");
1050
1051 s_std_exit:
1052         mutex_unlock(&common->lock);
1053         return ret;
1054 }
1055
1056 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1057 {
1058         struct vpif_fh *fh = priv;
1059         struct channel_obj *ch = fh->channel;
1060
1061         *std = ch->video.stdid;
1062         return 0;
1063 }
1064
1065 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1066 {
1067         struct vpif_fh *fh = priv;
1068         struct channel_obj *ch = fh->channel;
1069         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1070
1071         return videobuf_dqbuf(&common->buffer_queue, p,
1072                                         (file->f_flags & O_NONBLOCK));
1073 }
1074
1075 static int vpif_streamon(struct file *file, void *priv,
1076                                 enum v4l2_buf_type buftype)
1077 {
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;
1086         int ret = 0;
1087
1088         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1089                 vpif_err("buffer type not supported\n");
1090                 return -EINVAL;
1091         }
1092
1093         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1094                 vpif_err("fh->io_allowed\n");
1095                 return -EACCES;
1096         }
1097
1098         /* If Streaming is already started, return error */
1099         if (common->started) {
1100                 vpif_err("channel->started\n");
1101                 return -EBUSY;
1102         }
1103
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");
1110                 return -EBUSY;
1111         }
1112
1113         ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1114         if (ret < 0)
1115                 return ret;
1116
1117         /* Call videobuf_streamon to start streaming in videobuf */
1118         ret = videobuf_streamon(&common->buffer_queue);
1119         if (ret < 0) {
1120                 vpif_err("videobuf_streamon\n");
1121                 return ret;
1122         }
1123
1124         if (mutex_lock_interruptible(&common->lock))
1125                 return -ERESTARTSYS;
1126
1127         /* If buffer queue is empty, return error */
1128         if (list_empty(&common->dma_queue)) {
1129                 vpif_err("buffer queue is empty\n");
1130                 ret = -EIO;
1131                 goto streamon_exit;
1132         }
1133
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);
1138
1139         list_del(&common->cur_frm->queue);
1140         /* Mark state of the current frame to active */
1141         common->cur_frm->state = VIDEOBUF_ACTIVE;
1142
1143         /* Initialize field_id and started member */
1144         ch->field_id = 0;
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);
1150
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");
1157                         ret = -EINVAL;
1158                         goto streamon_exit;
1159                 }
1160
1161                 /* clock settings */
1162                 ret =
1163                  vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1164                                                 ch->vpifparams.std_info.hd_sd);
1165                 if (ret < 0) {
1166                         vpif_err("can't set clock\n");
1167                         goto streamon_exit;
1168                 }
1169
1170                 /* set the parameters and addresses */
1171                 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1172                 if (ret < 0)
1173                         goto streamon_exit;
1174
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));
1181
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);
1187                         enable_channel2(1);
1188                 }
1189
1190                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1191                         || (common->started == 2)) {
1192                         channel3_intr_assert();
1193                         channel3_intr_enable(1);
1194                         enable_channel3(1);
1195                 }
1196                 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1197         }
1198
1199 streamon_exit:
1200         mutex_unlock(&common->lock);
1201         return ret;
1202 }
1203
1204 static int vpif_streamoff(struct file *file, void *priv,
1205                                 enum v4l2_buf_type buftype)
1206 {
1207         struct vpif_fh *fh = priv;
1208         struct channel_obj *ch = fh->channel;
1209         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1210
1211         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1212                 vpif_err("buffer type not supported\n");
1213                 return -EINVAL;
1214         }
1215
1216         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1217                 vpif_err("fh->io_allowed\n");
1218                 return -EACCES;
1219         }
1220
1221         if (!common->started) {
1222                 vpif_err("channel->started\n");
1223                 return -EINVAL;
1224         }
1225
1226         if (mutex_lock_interruptible(&common->lock))
1227                 return -ERESTARTSYS;
1228
1229         if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1230                 /* disable channel */
1231                 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1232                         enable_channel2(0);
1233                         channel2_intr_enable(0);
1234                 }
1235                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1236                                         (2 == common->started)) {
1237                         enable_channel3(0);
1238                         channel3_intr_enable(0);
1239                 }
1240         }
1241
1242         common->started = 0;
1243         mutex_unlock(&common->lock);
1244
1245         return videobuf_streamoff(&common->buffer_queue);
1246 }
1247
1248 static int vpif_cropcap(struct file *file, void *priv,
1249                         struct v4l2_cropcap *crop)
1250 {
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)
1255                 return -EINVAL;
1256
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;
1261
1262         return 0;
1263 }
1264
1265 static int vpif_enum_output(struct file *file, void *fh,
1266                                 struct v4l2_output *output)
1267 {
1268
1269         struct vpif_display_config *config = vpif_dev->platform_data;
1270
1271         if (output->index >= config->output_count) {
1272                 vpif_dbg(1, debug, "Invalid output index\n");
1273                 return -EINVAL;
1274         }
1275
1276         strcpy(output->name, config->output[output->index]);
1277         output->type = V4L2_OUTPUT_TYPE_ANALOG;
1278         output->std = DM646X_V4L2_STD;
1279
1280         return 0;
1281 }
1282
1283 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1284 {
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];
1289         int ret = 0;
1290
1291         if (mutex_lock_interruptible(&common->lock))
1292                 return -ERESTARTSYS;
1293
1294         if (common->started) {
1295                 vpif_err("Streaming in progress\n");
1296                 ret = -EBUSY;
1297                 goto s_output_exit;
1298         }
1299
1300         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1301                                                         s_routing, 0, i, 0);
1302
1303         if (ret < 0)
1304                 vpif_err("Failed to set output standard\n");
1305
1306         vid_ch->output_id = i;
1307
1308 s_output_exit:
1309         mutex_unlock(&common->lock);
1310         return ret;
1311 }
1312
1313 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1314 {
1315         struct vpif_fh *fh = priv;
1316         struct channel_obj *ch = fh->channel;
1317         struct video_obj *vid_ch = &ch->video;
1318
1319         *i = vid_ch->output_id;
1320
1321         return 0;
1322 }
1323
1324 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1325 {
1326         struct vpif_fh *fh = priv;
1327         struct channel_obj *ch = fh->channel;
1328
1329         *p = v4l2_prio_max(&ch->prio);
1330
1331         return 0;
1332 }
1333
1334 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1335 {
1336         struct vpif_fh *fh = priv;
1337         struct channel_obj *ch = fh->channel;
1338
1339         return v4l2_prio_change(&ch->prio, &fh->prio, p);
1340 }
1341
1342 /**
1343  * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1344  * @file: file ptr
1345  * @priv: file handle
1346  * @preset: input preset
1347  */
1348 static int vpif_enum_dv_presets(struct file *file, void *priv,
1349                 struct v4l2_dv_enum_preset *preset)
1350 {
1351         struct vpif_fh *fh = priv;
1352         struct channel_obj *ch = fh->channel;
1353         struct video_obj *vid_ch = &ch->video;
1354
1355         return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1356                         video, enum_dv_presets, preset);
1357 }
1358
1359 /**
1360  * vpif_s_dv_presets() - S_DV_PRESETS handler
1361  * @file: file ptr
1362  * @priv: file handle
1363  * @preset: input preset
1364  */
1365 static int vpif_s_dv_preset(struct file *file, void *priv,
1366                 struct v4l2_dv_preset *preset)
1367 {
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;
1372         int ret = 0;
1373
1374         if (common->started) {
1375                 vpif_dbg(1, debug, "streaming in progress\n");
1376                 return -EBUSY;
1377         }
1378
1379         ret = v4l2_prio_check(&ch->prio, fh->prio);
1380         if (ret != 0)
1381                 return ret;
1382
1383         fh->initialized = 1;
1384
1385         /* Call encoder subdevice function to set the standard */
1386         if (mutex_lock_interruptible(&common->lock))
1387                 return -ERESTARTSYS;
1388
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));
1392
1393         /* Get the information about the standard */
1394         if (vpif_update_resolution(ch)) {
1395                 ret = -EINVAL;
1396         } else {
1397                 /* Configure the default format information */
1398                 vpif_config_format(ch);
1399
1400                 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1401                                 video, s_dv_preset, preset);
1402         }
1403
1404         mutex_unlock(&common->lock);
1405
1406         return ret;
1407 }
1408 /**
1409  * vpif_g_dv_presets() - G_DV_PRESETS handler
1410  * @file: file ptr
1411  * @priv: file handle
1412  * @preset: input preset
1413  */
1414 static int vpif_g_dv_preset(struct file *file, void *priv,
1415                 struct v4l2_dv_preset *preset)
1416 {
1417         struct vpif_fh *fh = priv;
1418         struct channel_obj *ch = fh->channel;
1419
1420         preset->preset = ch->video.dv_preset;
1421
1422         return 0;
1423 }
1424 /**
1425  * vpif_s_dv_timings() - S_DV_TIMINGS handler
1426  * @file: file ptr
1427  * @priv: file handle
1428  * @timings: digital video timings
1429  */
1430 static int vpif_s_dv_timings(struct file *file, void *priv,
1431                 struct v4l2_dv_timings *timings)
1432 {
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;
1439         int ret;
1440
1441         if (timings->type != V4L2_DV_BT_656_1120) {
1442                 vpif_dbg(2, debug, "Timing type not defined\n");
1443                 return -EINVAL;
1444         }
1445
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 "
1451                                 "subdevice\n");
1452                 return -EINVAL;
1453         }
1454         if (ret < 0) {
1455                 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1456                 return ret;
1457         }
1458
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");
1471                 return -EINVAL;
1472         }
1473
1474         *bt = timings->bt;
1475
1476         /* Configure video port timings */
1477
1478         std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1479                 bt->hsync - 8;
1480         std_info->sav2eav = bt->width;
1481
1482         std_info->l1 = 1;
1483         std_info->l3 = bt->vsync + bt->vbackporch + 1;
1484
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 +
1490                                 bt->il_vbackporch;
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);
1498                 } else {
1499                         vpif_dbg(2, debug, "Required timing values for "
1500                                         "interlaced BT format missing\n");
1501                         return -EINVAL;
1502                 }
1503         } else {
1504                 std_info->vsize = bt->height + bt->vfrontporch +
1505                         bt->vsync + bt->vbackporch;
1506                 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1507         }
1508         strncpy(std_info->name, "Custom timings BT656/1120",
1509                         VPIF_MAX_NAME);
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;
1519
1520         vid_ch->stdid = 0;
1521         vid_ch->dv_preset = V4L2_DV_INVALID;
1522
1523         return 0;
1524 }
1525
1526 /**
1527  * vpif_g_dv_timings() - G_DV_TIMINGS handler
1528  * @file: file ptr
1529  * @priv: file handle
1530  * @timings: digital video timings
1531  */
1532 static int vpif_g_dv_timings(struct file *file, void *priv,
1533                 struct v4l2_dv_timings *timings)
1534 {
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;
1539
1540         timings->bt = *bt;
1541
1542         return 0;
1543 }
1544
1545 /*
1546  * vpif_g_chip_ident() - Identify the chip
1547  * @file: file ptr
1548  * @priv: file handle
1549  * @chip: chip identity
1550  *
1551  * Returns zero or -EINVAL if read operations fails.
1552  */
1553 static int vpif_g_chip_ident(struct file *file, void *priv,
1554                 struct v4l2_dbg_chip_ident *chip)
1555 {
1556         chip->ident = V4L2_IDENT_NONE;
1557         chip->revision = 0;
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");
1561                 return -EINVAL;
1562         }
1563
1564         return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1565                         g_chip_ident, chip);
1566 }
1567
1568 #ifdef CONFIG_VIDEO_ADV_DEBUG
1569 /*
1570  * vpif_dbg_g_register() - Read register
1571  * @file: file ptr
1572  * @priv: file handle
1573  * @reg: register to be read
1574  *
1575  * Debugging only
1576  * Returns zero or -EINVAL if read operations fails.
1577  */
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;
1583
1584         return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1585                         g_register, reg);
1586 }
1587
1588 /*
1589  * vpif_dbg_s_register() - Write to register
1590  * @file: file ptr
1591  * @priv: file handle
1592  * @reg: register to be modified
1593  *
1594  * Debugging only
1595  * Returns zero or -EINVAL if write operations fails.
1596  */
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;
1602
1603         return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1604                         s_register, reg);
1605 }
1606 #endif
1607
1608 /*
1609  * vpif_log_status() - Status information
1610  * @file: file ptr
1611  * @priv: file handle
1612  *
1613  * Returns zero.
1614  */
1615 static int vpif_log_status(struct file *filep, void *priv)
1616 {
1617         /* status for sub devices */
1618         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1619
1620         return 0;
1621 }
1622
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,
1653 #endif
1654         .vidioc_log_status              = vpif_log_status,
1655 };
1656
1657 static const struct v4l2_file_operations vpif_fops = {
1658         .owner          = THIS_MODULE,
1659         .open           = vpif_open,
1660         .release        = vpif_release,
1661         .ioctl          = video_ioctl2,
1662         .mmap           = vpif_mmap,
1663         .poll           = vpif_poll
1664 };
1665
1666 static struct video_device vpif_video_template = {
1667         .name           = "vpif",
1668         .fops           = &vpif_fops,
1669         .ioctl_ops      = &vpif_ioctl_ops,
1670         .tvnorms        = DM646X_V4L2_STD,
1671         .current_norm   = V4L2_STD_625_50,
1672
1673 };
1674
1675 /*Configure the channels, buffer sizei, request irq */
1676 static int initialize_vpif(void)
1677 {
1678         int free_channel_objects_index;
1679         int free_buffer_channel_index;
1680         int free_buffer_index;
1681         int err = 0, i, j;
1682
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;
1690
1691         /* Set buffer size to min buffers size if invalid buffer size is
1692          * given */
1693         if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1694                 ch2_bufsize =
1695                     config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1696         if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1697                 ch3_bufsize =
1698                     config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1699
1700         config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1701
1702         if (ch2_numbuffers) {
1703                 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1704                                                         ch2_bufsize;
1705         }
1706         config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1707
1708         if (ch3_numbuffers) {
1709                 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1710                                                         ch3_bufsize;
1711         }
1712
1713         /* Allocate memory for six channel objects */
1714         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1715                 vpif_obj.dev[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;
1720                         err = -ENOMEM;
1721                         goto vpif_init_free_channel_objects;
1722                 }
1723         }
1724
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];
1728
1729         return 0;
1730
1731 vpif_init_free_channel_objects:
1732         for (j = 0; j < free_channel_objects_index; j++)
1733                 kfree(vpif_obj.dev[j]);
1734         return err;
1735 }
1736
1737 /*
1738  * vpif_probe: This function creates device entries by register itself to the
1739  * V4L2 driver and initializes fields of each channel objects
1740  */
1741 static __init int vpif_probe(struct platform_device *pdev)
1742 {
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;
1751         int subdev_count;
1752
1753         vpif_dev = &pdev->dev;
1754
1755         err = initialize_vpif();
1756
1757         if (err) {
1758                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1759                 return err;
1760         }
1761
1762         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1763         if (err) {
1764                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1765                 return err;
1766         }
1767
1768         k = 0;
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,
1772                                         "DM646x_Display",
1773                                 (void *)(&vpif_obj.dev[k]->channel_id))) {
1774                                 err = -EBUSY;
1775                                 goto vpif_int_err;
1776                         }
1777                 }
1778                 k++;
1779         }
1780
1781         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1782
1783                 /* Get the pointer to the channel object */
1784                 ch = vpif_obj.dev[i];
1785
1786                 /* Allocate memory for video device */
1787                 vfd = video_device_alloc();
1788                 if (vfd == NULL) {
1789                         for (j = 0; j < i; j++) {
1790                                 ch = vpif_obj.dev[j];
1791                                 video_device_release(ch->video_dev);
1792                         }
1793                         err = -ENOMEM;
1794                         goto vpif_int_err;
1795                 }
1796
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);
1806
1807                 /* Set video_dev to the video device */
1808                 ch->video_dev = vfd;
1809         }
1810
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];
1829
1830                 }
1831                 ch->initialized = 0;
1832                 ch->channel_id = j;
1833                 if (j < 2)
1834                         ch->common[VPIF_VIDEO_INDEX].numbuffers =
1835                             config_params.numbuffers[ch->channel_id];
1836                 else
1837                         ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1838
1839                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1840
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;
1845
1846                 /* register video device */
1847                 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1848                                 (int)ch, (int)&ch->video_dev);
1849
1850                 err = video_register_device(ch->video_dev,
1851                                           VFL_TYPE_GRABBER, (j ? 3 : 2));
1852                 if (err < 0)
1853                         goto probe_out;
1854
1855                 video_set_drvdata(ch->video_dev, ch);
1856         }
1857
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,
1863                                                                 GFP_KERNEL);
1864         if (vpif_obj.sd == NULL) {
1865                 vpif_err("unable to allocate memory for subdevice pointers\n");
1866                 err = -ENOMEM;
1867                 goto probe_out;
1868         }
1869
1870         for (i = 0; i < subdev_count; i++) {
1871                 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1872                                                 i2c_adap,
1873                                                 &subdevdata[i].board_info,
1874                                                 NULL);
1875                 if (!vpif_obj.sd[i]) {
1876                         vpif_err("Error registering v4l2 subdevice\n");
1877                         goto probe_subdev_out;
1878                 }
1879
1880                 if (vpif_obj.sd[i])
1881                         vpif_obj.sd[i]->grp_id = 1 << i;
1882         }
1883
1884         v4l2_info(&vpif_obj.v4l2_dev,
1885                         "DM646x VPIF display driver initialized\n");
1886         return 0;
1887
1888 probe_subdev_out:
1889         kfree(vpif_obj.sd);
1890 probe_out:
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;
1896         }
1897 vpif_int_err:
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);
1904                 m = res->end;
1905         }
1906
1907         return err;
1908 }
1909
1910 /*
1911  * vpif_remove: It un-register channels from V4L2 driver
1912  */
1913 static int vpif_remove(struct platform_device *device)
1914 {
1915         struct channel_obj *ch;
1916         int i;
1917
1918         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1919
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);
1926
1927                 ch->video_dev = NULL;
1928         }
1929
1930         return 0;
1931 }
1932
1933 static __refdata struct platform_driver vpif_driver = {
1934         .driver = {
1935                         .name   = "vpif_display",
1936                         .owner  = THIS_MODULE,
1937         },
1938         .probe  = vpif_probe,
1939         .remove = vpif_remove,
1940 };
1941
1942 static __init int vpif_init(void)
1943 {
1944         return platform_driver_register(&vpif_driver);
1945 }
1946
1947 /*
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
1950  * objects.
1951  */
1952 static void vpif_cleanup(void)
1953 {
1954         struct platform_device *pdev;
1955         struct resource *res;
1956         int irq_num;
1957         int i = 0;
1958
1959         pdev = container_of(vpif_dev, struct platform_device, dev);
1960
1961         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1962                 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1963                         free_irq(irq_num,
1964                                  (void *)(&vpif_obj.dev[i]->channel_id));
1965                 i++;
1966         }
1967
1968         platform_driver_unregister(&vpif_driver);
1969         kfree(vpif_obj.sd);
1970         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1971                 kfree(vpif_obj.dev[i]);
1972 }
1973
1974 module_init(vpif_init);
1975 module_exit(vpif_cleanup);