V4L/DVB (12176): davinci/vpif_display: Add VPIF display driver
[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
34 #include <asm/irq.h>
35 #include <asm/page.h>
36
37 #include <media/adv7343.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
40
41 #include <mach/dm646x.h>
42
43 #include "vpif_display.h"
44 #include "vpif.h"
45
46 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
47 MODULE_LICENSE("GPL");
48
49 #define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
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)
54
55 static int debug = 1;
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;
60
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);
66
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)");
72
73 static struct vpif_config_params config_params = {
74         .min_numbuffers         = 3,
75         .numbuffers[0]          = 3,
76         .numbuffers[1]          = 3,
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,
81 };
82
83 static struct vpif_device vpif_obj = { {NULL} };
84 static struct device *vpif_dev;
85
86 static const struct vpif_channel_config_params ch_params[] = {
87         {
88                 "NTSC", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
89                 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
90         },
91         {
92                 "PAL", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
93                 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
94         },
95 };
96
97 /*
98  * vpif_uservirt_to_phys: This function is used to convert user
99  * space virtual address to physical address.
100  */
101 static u32 vpif_uservirt_to_phys(u32 virtp)
102 {
103         struct mm_struct *mm = current->mm;
104         unsigned long physp = 0;
105         struct vm_area_struct *vma;
106
107         vma = find_vma(mm, virtp);
108
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);
115         } else {
116                 /* otherwise, use get_user_pages() for general userland pages */
117                 int res, nr_pages = 1;
118                 struct page *pages;
119                 down_read(&current->mm->mmap_sem);
120
121                 res = get_user_pages(current, current->mm,
122                                      virtp, nr_pages, 1, 0, &pages, NULL);
123                 up_read(&current->mm->mmap_sem);
124
125                 if (res == nr_pages) {
126                         physp = __pa(page_address(&pages[0]) +
127                                                         (virtp & ~PAGE_MASK));
128                 } else {
129                         vpif_err("get_user_pages failed\n");
130                         return 0;
131                 }
132         }
133
134         return physp;
135 }
136
137 /*
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
141  */
142 static int vpif_buffer_prepare(struct videobuf_queue *q,
143                                struct videobuf_buffer *vb,
144                                enum v4l2_field field)
145 {
146         struct vpif_fh *fh = q->priv_data;
147         struct common_obj *common;
148         unsigned long addr;
149
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;
155                 vb->field       = field;
156         }
157         vb->state = VIDEOBUF_PREPARED;
158
159         /* if user pointer memory mechanism is used, get the physical
160          * address of the buffer */
161         if (V4L2_MEMORY_USERPTR == common->memory) {
162                 if (!vb->baddr) {
163                         vpif_err("buffer_address is 0\n");
164                         return -EINVAL;
165                 }
166
167                 vb->boff = vpif_uservirt_to_phys(vb->baddr);
168                 if (!ISALIGNED(vb->boff))
169                         goto buf_align_exit;
170         }
171
172         addr = 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))
178                         goto buf_align_exit;
179         }
180         return 0;
181
182 buf_align_exit:
183         vpif_err("buffer offset not aligned to 8 bytes\n");
184         return -EINVAL;
185 }
186
187 /*
188  * vpif_buffer_setup: This function allocates memory for the buffers
189  */
190 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
191                                 unsigned int *size)
192 {
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];
196
197         if (V4L2_MEMORY_MMAP != common->memory)
198                 return 0;
199
200         *size = config_params.channel_bufsize[ch->channel_id];
201         if (*count < config_params.min_numbuffers)
202                 *count = config_params.min_numbuffers;
203
204         return 0;
205 }
206
207 /*
208  * vpif_buffer_queue: This function adds the buffer to DMA queue
209  */
210 static void vpif_buffer_queue(struct videobuf_queue *q,
211                               struct videobuf_buffer *vb)
212 {
213         struct vpif_fh *fh = q->priv_data;
214         struct common_obj *common;
215
216         common = &fh->channel->common[VPIF_VIDEO_INDEX];
217
218         /* add the buffer to the DMA queue */
219         list_add_tail(&vb->queue, &common->dma_queue);
220         vb->state = VIDEOBUF_QUEUED;
221 }
222
223 /*
224  * vpif_buffer_release: This function is called from the videobuf layer to
225  * free memory allocated to the buffers
226  */
227 static void vpif_buffer_release(struct videobuf_queue *q,
228                                 struct videobuf_buffer *vb)
229 {
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;
234
235         common = &ch->common[VPIF_VIDEO_INDEX];
236
237         videobuf_dma_contig_free(q, vb);
238         vb->state = VIDEOBUF_NEEDS_INIT;
239
240         if (V4L2_MEMORY_MMAP != common->memory)
241                 return;
242
243         buf_size = config_params.channel_bufsize[ch->channel_id];
244 }
245
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,
251 };
252 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
253
254 static void process_progressive_mode(struct common_obj *common)
255 {
256         unsigned long addr = 0;
257
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;
265
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);
272 }
273
274 static void process_interlaced_mode(int fid, struct common_obj *common)
275 {
276         /* device field id and local field id are in sync */
277         /* If this is even field */
278         if (0 == fid) {
279                 if (common->cur_frm == common->next_frm)
280                         return;
281
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;
292
293         } else if (1 == fid) {  /* odd field */
294                 if (list_empty(&common->dma_queue)
295                     || (common->cur_frm != common->next_frm)) {
296                         return;
297                 }
298                 /* one field is displayed configure the next
299                  * frame if it is available else hold on current
300                  * frame */
301                 /* Get next from the buffer queue */
302                 process_progressive_mode(common);
303
304         }
305 }
306
307 /*
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
310  */
311 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
312 {
313         struct vpif_device *dev = &vpif_obj;
314         struct channel_obj *ch;
315         struct common_obj *common;
316         enum v4l2_field field;
317         int fid = -1, i;
318         int channel_id = 0;
319
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)
327                         continue;
328
329                 if (1 == ch->vpifparams.std_info.frm_fmt) {
330                         if (list_empty(&common->dma_queue))
331                                 continue;
332
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;
342                         }
343
344                         channel_first_int[i][channel_id] = 0;
345                         process_progressive_mode(common);
346                 } else {
347                         /* Interlaced mode */
348                         /* If it is first interrupt, ignore it */
349
350                         if (channel_first_int[i][channel_id]) {
351                                 channel_first_int[i][channel_id] = 0;
352                                 continue;
353                         }
354
355                         if (0 == i) {
356                                 ch->field_id ^= 1;
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 */
362                                         if (0 == fid)
363                                                 ch->field_id = fid;
364
365                                         return IRQ_HANDLED;
366                                 }
367                         }
368                         process_interlaced_mode(fid, common);
369                 }
370         }
371
372         return IRQ_HANDLED;
373 }
374
375 static int vpif_get_std_info(struct channel_obj *ch)
376 {
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;
382
383         int index;
384
385         std_info->stdid = vid_ch->stdid;
386         if (!std_info)
387                 return -1;
388
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));
393                         break;
394                 }
395         }
396
397         if (index == ARRAY_SIZE(ch_params))
398                 return -1;
399
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);
404
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;
408
409         return 0;
410 }
411
412 /*
413  * vpif_calculate_offsets: This function calculates buffers offset for Y and C
414  * in the top and bottom field
415  */
416 static void vpif_calculate_offsets(struct channel_obj *ch)
417 {
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;
423
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;
427                 else
428                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
429         } else {
430                 vid_ch->buf_field = common->fmt.fmt.pix.field;
431         }
432
433         if (V4L2_MEMORY_USERPTR == common->memory)
434                 sizeimage = common->fmt.fmt.pix.sizeimage;
435         else
436                 sizeimage = config_params.channel_bufsize[ch->channel_id];
437
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;
456         }
457
458         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
459             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
460                 vpifparams->video_params.storage_mode = 1;
461         } else {
462                 vpifparams->video_params.storage_mode = 0;
463         }
464
465         if (ch->vpifparams.std_info.frm_fmt == 1) {
466                 vpifparams->video_params.hpitch =
467                     common->fmt.fmt.pix.bytesperline;
468         } else {
469                 if ((field == V4L2_FIELD_ANY) ||
470                         (field == V4L2_FIELD_INTERLACED))
471                         vpifparams->video_params.hpitch =
472                             common->fmt.fmt.pix.bytesperline * 2;
473                 else
474                         vpifparams->video_params.hpitch =
475                             common->fmt.fmt.pix.bytesperline;
476         }
477
478         ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
479 }
480
481 static void vpif_config_format(struct channel_obj *ch)
482 {
483         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
484
485         common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
486         if (config_params.numbuffers[ch->channel_id] == 0)
487                 common->memory = V4L2_MEMORY_USERPTR;
488         else
489                 common->memory = V4L2_MEMORY_MMAP;
490
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;
495 }
496
497 static int vpif_check_format(struct channel_obj *ch,
498                              struct v4l2_pix_format *pixfmt)
499 {
500         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
501         enum v4l2_field field = pixfmt->field;
502         u32 sizeimage, hpitch, vpitch;
503
504         if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
505                 goto invalid_fmt_exit;
506
507         if (!(VPIF_VALID_FIELD(field)))
508                 goto invalid_fmt_exit;
509
510         if (pixfmt->bytesperline <= 0)
511                 goto invalid_pitch_exit;
512
513         if (V4L2_MEMORY_USERPTR == common->memory)
514                 sizeimage = pixfmt->sizeimage;
515         else
516                 sizeimage = config_params.channel_bufsize[ch->channel_id];
517
518         if (vpif_get_std_info(ch)) {
519                 vpif_err("Error getting the standard info\n");
520                 return -EINVAL;
521         }
522
523         hpitch = pixfmt->bytesperline;
524         vpitch = sizeimage / (hpitch * 2);
525
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;
530
531         /* Check for 8 byte alignment */
532         if (!ISALIGNED(hpitch)) {
533                 vpif_err("invalid pitch alignment\n");
534                 return -EINVAL;
535         }
536         pixfmt->width = common->fmt.fmt.pix.width;
537         pixfmt->height = common->fmt.fmt.pix.height;
538
539         return 0;
540
541 invalid_fmt_exit:
542         vpif_err("invalid field format\n");
543         return -EINVAL;
544
545 invalid_pitch_exit:
546         vpif_err("invalid pitch\n");
547         return -EINVAL;
548 }
549
550 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
551 {
552         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
553
554         if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
555                 common->set_addr = ch3_set_videobuf_addr;
556         } else {
557                 if (2 == muxmode)
558                         common->set_addr = ch2_set_videobuf_addr_yc_nmux;
559                 else
560                         common->set_addr = ch2_set_videobuf_addr;
561         }
562 }
563
564 /*
565  * vpif_mmap: It is used to map kernel space buffers into user spaces
566  */
567 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
568 {
569         struct vpif_fh *fh = filep->private_data;
570         struct common_obj *common = &fh->channel->common[VPIF_VIDEO_INDEX];
571
572         return videobuf_mmap_mapper(&common->buffer_queue, vma);
573 }
574
575 /*
576  * vpif_poll: It is used for select/poll system call
577  */
578 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
579 {
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];
583
584         if (common->started)
585                 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
586
587         return 0;
588 }
589
590 /*
591  * vpif_open: It creates object of file handle structure and stores it in
592  * private_data member of filepointer
593  */
594 static int vpif_open(struct file *filep)
595 {
596         struct video_device *vdev = video_devdata(filep);
597         struct channel_obj *ch = NULL;
598         struct vpif_fh *fh = NULL;
599
600         ch = video_get_drvdata(vdev);
601         /* Allocate memory for the file handle object */
602         fh = kmalloc(sizeof(struct vpif_fh), GFP_KERNEL);
603         if (fh == NULL) {
604                 vpif_err("unable to allocate memory for file handle object\n");
605                 return -ENOMEM;
606         }
607
608         /* store pointer to fh in private_data member of filep */
609         filep->private_data = fh;
610         fh->channel = ch;
611         fh->initialized = 0;
612         if (!ch->initialized) {
613                 fh->initialized = 1;
614                 ch->initialized = 1;
615                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
616         }
617
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);
625
626         return 0;
627 }
628
629 /*
630  * vpif_release: This function deletes buffer queue, frees the buffers and
631  * the vpif file handle
632  */
633 static int vpif_release(struct file *filep)
634 {
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];
638
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 */
643                 common->io_usrs = 0;
644                 /* Disable channel */
645                 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
646                         enable_channel2(0);
647                         channel2_intr_enable(0);
648                 }
649                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
650                     (2 == common->started)) {
651                         enable_channel3(0);
652                         channel3_intr_enable(0);
653                 }
654                 common->started = 0;
655                 /* Free buffers allocated */
656                 videobuf_queue_cancel(&common->buffer_queue);
657                 videobuf_mmap_free(&common->buffer_queue);
658                 common->numbuffers =
659                     config_params.numbuffers[ch->channel_id];
660         }
661
662         mutex_unlock(&common->lock);
663
664         /* Decrement channel usrs counter */
665         atomic_dec(&ch->usrs);
666         /* If this file handle has initialize encoder device, reset it */
667         if (fh->initialized)
668                 ch->initialized = 0;
669
670         /* Close the priority */
671         v4l2_prio_close(&ch->prio, &fh->prio);
672         filep->private_data = NULL;
673         fh->initialized = 0;
674         kfree(fh);
675
676         return 0;
677 }
678
679 /* functions implementing ioctls */
680
681 static int vpif_querycap(struct file *file, void  *priv,
682                                 struct v4l2_capability *cap)
683 {
684         struct vpif_config *config = vpif_dev->platform_data;
685
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));
691
692         return 0;
693 }
694
695 static int vpif_enum_fmt_vid_out(struct file *file, void  *priv,
696                                         struct v4l2_fmtdesc *fmt)
697 {
698         if (fmt->index != 0) {
699                 vpif_err("Invalid format index\n");
700                 return -EINVAL;
701         }
702
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;
707
708         return 0;
709 }
710
711 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
712                                 struct v4l2_format *fmt)
713 {
714         struct vpif_fh *fh = priv;
715         struct channel_obj *ch = fh->channel;
716         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
717
718         /* Check the validity of the buffer type */
719         if (common->fmt.type != fmt->type)
720                 return -EINVAL;
721
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");
726                 return -EINVAL;
727         }
728
729         *fmt = common->fmt;
730         mutex_unlock(&common->lock);
731         return 0;
732 }
733
734 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
735                                 struct v4l2_format *fmt)
736 {
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];
741         int ret = 0;
742
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");
747                         return -EBUSY;
748                 }
749
750                 /* Check for the priority */
751                 ret = v4l2_prio_check(&ch->prio, &fh->prio);
752                 if (0 != ret)
753                         return ret;
754                 fh->initialized = 1;
755         }
756
757         if (common->started) {
758                 vpif_dbg(1, debug, "Streaming in progress\n");
759                 return -EBUSY;
760         }
761
762         pixfmt = &fmt->fmt.pix;
763         /* Check for valid field format */
764         ret = vpif_check_format(ch, pixfmt);
765         if (ret)
766                 return ret;
767
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);
772         common->fmt = *fmt;
773         mutex_unlock(&common->lock);
774
775         return 0;
776 }
777
778 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
779                                 struct v4l2_format *fmt)
780 {
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;
785         int ret = 0;
786
787         ret = vpif_check_format(ch, pixfmt);
788         if (ret) {
789                 *pixfmt = common->fmt.fmt.pix;
790                 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
791         }
792
793         return ret;
794 }
795
796 static int vpif_reqbufs(struct file *file, void *priv,
797                         struct v4l2_requestbuffers *reqbuf)
798 {
799         struct vpif_fh *fh = priv;
800         struct channel_obj *ch = fh->channel;
801         struct common_obj *common;
802         enum v4l2_field field;
803         u8 index = 0;
804         int ret = 0;
805
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");
812                         return -EBUSY;
813                 }
814         }
815
816         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
817                 return -EINVAL;
818
819         index = VPIF_VIDEO_INDEX;
820
821         common = &ch->common[index];
822         mutex_lock_interruptible(&common->lock);
823         if (common->fmt.type != reqbuf->type) {
824                 ret = -EINVAL;
825                 goto reqbuf_exit;
826         }
827
828         if (0 != common->io_usrs) {
829                 ret = -EBUSY;
830                 goto reqbuf_exit;
831         }
832
833         if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
834                 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
835                         field = V4L2_FIELD_INTERLACED;
836                 else
837                         field = common->fmt.fmt.pix.field;
838         } else {
839                 field = V4L2_VBI_INTERLACED;
840         }
841
842         /* Initialize videobuf queue as per the buffer type */
843         videobuf_queue_dma_contig_init(&common->buffer_queue,
844                                             &video_qops, NULL,
845                                             &common->irqlock,
846                                             reqbuf->type, field,
847                                             sizeof(struct videobuf_buffer), fh);
848
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 */
852         common->io_usrs = 1;
853         /* Store type of memory requested in channel object */
854         common->memory = reqbuf->memory;
855         INIT_LIST_HEAD(&common->dma_queue);
856
857         /* Allocate buffers */
858         ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
859
860 reqbuf_exit:
861         mutex_unlock(&common->lock);
862         return ret;
863 }
864
865 static int vpif_querybuf(struct file *file, void *priv,
866                                 struct v4l2_buffer *tbuf)
867 {
868         struct vpif_fh *fh = priv;
869         struct channel_obj *ch = fh->channel;
870         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
871
872         if (common->fmt.type != tbuf->type)
873                 return -EINVAL;
874
875         return videobuf_querybuf(&common->buffer_queue, tbuf);
876 }
877
878 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
879 {
880
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;
887         unsigned long flags;
888         int ret = 0;
889
890         if (common->fmt.type != tbuf.type)
891                 return -EINVAL;
892
893         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
894                 vpif_err("fh->io_allowed\n");
895                 return -EACCES;
896         }
897
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);
903
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");
909                 goto qbuf_exit;
910         }
911
912         if ((buf1->state == VIDEOBUF_QUEUED) ||
913             (buf1->state == VIDEOBUF_ACTIVE)) {
914                 vpif_err("invalid state\n");
915                 goto qbuf_exit;
916         }
917
918         switch (buf1->memory) {
919         case V4L2_MEMORY_MMAP:
920                 if (buf1->baddr == 0)
921                         goto qbuf_exit;
922                 break;
923
924         case V4L2_MEMORY_USERPTR:
925                 if (tbuf.length < buf1->bsize)
926                         goto qbuf_exit;
927
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;
932                 break;
933
934         default:
935                 goto qbuf_exit;
936         }
937
938         local_irq_save(flags);
939         ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
940                                         common->buffer_queue.field);
941         if (ret < 0) {
942                 local_irq_restore(flags);
943                 goto qbuf_exit;
944         }
945
946         buf1->state = VIDEOBUF_ACTIVE;
947         addr = buf1->boff;
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));
954         }
955
956         local_irq_restore(flags);
957         list_add_tail(&buf1->stream, &common->buffer_queue.stream);
958         mutex_unlock(&common->buffer_queue.vb_lock);
959         return 0;
960
961 qbuf_exit:
962         mutex_unlock(&common->buffer_queue.vb_lock);
963         return -EINVAL;
964 }
965
966 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
967 {
968         struct vpif_fh *fh = priv;
969         struct channel_obj *ch = fh->channel;
970         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
971         int ret = 0;
972
973         if (!(*std_id & DM646X_V4L2_STD))
974                 return -EINVAL;
975
976         if (common->started) {
977                 vpif_err("streaming in progress\n");
978                 return -EBUSY;
979         }
980
981         /* Call encoder subdevice function to set the standard */
982         mutex_lock_interruptible(&common->lock);
983
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");
988                 return -EINVAL;
989         }
990
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");
995                 ret = -EINVAL;
996                 goto s_std_exit;
997         }
998
999         common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1000         /* Configure the default format information */
1001         vpif_config_format(ch);
1002
1003         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1004                                                 s_std_output, *std_id);
1005         if (ret < 0) {
1006                 vpif_err("Failed to set output standard\n");
1007                 goto s_std_exit;
1008         }
1009
1010         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1011                                                         s_std, *std_id);
1012         if (ret < 0)
1013                 vpif_err("Failed to set standard for sub devices\n");
1014
1015 s_std_exit:
1016         mutex_unlock(&common->lock);
1017         return ret;
1018 }
1019
1020 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1021 {
1022         struct vpif_fh *fh = priv;
1023         struct channel_obj *ch = fh->channel;
1024
1025         *std = ch->video.stdid;
1026         return 0;
1027 }
1028
1029 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1030 {
1031         struct vpif_fh *fh = priv;
1032         struct channel_obj *ch = fh->channel;
1033         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1034
1035         return videobuf_dqbuf(&common->buffer_queue, p,
1036                                         (file->f_flags & O_NONBLOCK));
1037 }
1038
1039 static int vpif_streamon(struct file *file, void *priv,
1040                                 enum v4l2_buf_type buftype)
1041 {
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;
1050         int ret = 0;
1051
1052         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1053                 vpif_err("buffer type not supported\n");
1054                 return -EINVAL;
1055         }
1056
1057         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1058                 vpif_err("fh->io_allowed\n");
1059                 return -EACCES;
1060         }
1061
1062         /* If Streaming is already started, return error */
1063         if (common->started) {
1064                 vpif_err("channel->started\n");
1065                 return -EBUSY;
1066         }
1067
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");
1074                 return -EBUSY;
1075         }
1076
1077         ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1078         if (ret < 0)
1079                 return ret;
1080
1081         /* Call videobuf_streamon to start streaming  in videobuf */
1082         ret = videobuf_streamon(&common->buffer_queue);
1083         if (ret < 0) {
1084                 vpif_err("videobuf_streamon\n");
1085                 return ret;
1086         }
1087
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");
1092                 ret = -EIO;
1093                 goto streamon_exit;
1094         }
1095
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);
1100
1101         list_del(&common->cur_frm->queue);
1102         /* Mark state of the current frame to active */
1103         common->cur_frm->state = VIDEOBUF_ACTIVE;
1104
1105         /* Initialize field_id and started member */
1106         ch->field_id = 0;
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);
1112
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");
1119                         ret = -EINVAL;
1120                         goto streamon_exit;
1121                 }
1122
1123                 /* clock settings */
1124                 ret =
1125                  vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1126                                                 ch->vpifparams.std_info.hd_sd);
1127                 if (ret < 0) {
1128                         vpif_err("can't set clock\n");
1129                         goto streamon_exit;
1130                 }
1131
1132                 /* set the parameters and addresses */
1133                 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1134                 if (ret < 0)
1135                         goto streamon_exit;
1136
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));
1143
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);
1149                         enable_channel2(1);
1150                 }
1151
1152                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1153                         || (common->started == 2)) {
1154                         channel3_intr_assert();
1155                         channel3_intr_enable(1);
1156                         enable_channel3(1);
1157                 }
1158                 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1159         }
1160
1161 streamon_exit:
1162         mutex_unlock(&common->lock);
1163         return ret;
1164 }
1165
1166 static int vpif_streamoff(struct file *file, void *priv,
1167                                 enum v4l2_buf_type buftype)
1168 {
1169         struct vpif_fh *fh = priv;
1170         struct channel_obj *ch = fh->channel;
1171         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1172
1173         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1174                 vpif_err("buffer type not supported\n");
1175                 return -EINVAL;
1176         }
1177
1178         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1179                 vpif_err("fh->io_allowed\n");
1180                 return -EACCES;
1181         }
1182
1183         if (!common->started) {
1184                 vpif_err("channel->started\n");
1185                 return -EINVAL;
1186         }
1187
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) {
1192                         enable_channel2(0);
1193                         channel2_intr_enable(0);
1194                 }
1195                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1196                                         (2 == common->started)) {
1197                         enable_channel3(0);
1198                         channel3_intr_enable(0);
1199                 }
1200         }
1201
1202         common->started = 0;
1203         mutex_unlock(&common->lock);
1204
1205         return videobuf_streamoff(&common->buffer_queue);
1206 }
1207
1208 static int vpif_cropcap(struct file *file, void *priv,
1209                         struct v4l2_cropcap *crop)
1210 {
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)
1215                 return -EINVAL;
1216
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;
1221
1222         return 0;
1223 }
1224
1225 static int vpif_enum_output(struct file *file, void *fh,
1226                                 struct v4l2_output *output)
1227 {
1228
1229         struct vpif_config *config = vpif_dev->platform_data;
1230
1231         if (output->index >= config->output_count) {
1232                 vpif_dbg(1, debug, "Invalid output index\n");
1233                 return -EINVAL;
1234         }
1235
1236         strcpy(output->name, config->output[output->index]);
1237         output->type = V4L2_OUTPUT_TYPE_ANALOG;
1238         output->std = DM646X_V4L2_STD;
1239
1240         return 0;
1241 }
1242
1243 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1244 {
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];
1249         int ret = 0;
1250
1251         mutex_lock_interruptible(&common->lock);
1252         if (common->started) {
1253                 vpif_err("Streaming in progress\n");
1254                 ret = -EBUSY;
1255                 goto s_output_exit;
1256         }
1257
1258         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1259                                                         s_routing, 0, i, 0);
1260
1261         if (ret < 0)
1262                 vpif_err("Failed to set output standard\n");
1263
1264         vid_ch->output_id = i;
1265
1266 s_output_exit:
1267         mutex_unlock(&common->lock);
1268         return ret;
1269 }
1270
1271 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1272 {
1273         struct vpif_fh *fh = priv;
1274         struct channel_obj *ch = fh->channel;
1275         struct video_obj *vid_ch = &ch->video;
1276
1277         *i = vid_ch->output_id;
1278
1279         return 0;
1280 }
1281
1282 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1283 {
1284         struct vpif_fh *fh = priv;
1285         struct channel_obj *ch = fh->channel;
1286
1287         *p = v4l2_prio_max(&ch->prio);
1288
1289         return 0;
1290 }
1291
1292 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1293 {
1294         struct vpif_fh *fh = priv;
1295         struct channel_obj *ch = fh->channel;
1296
1297         return v4l2_prio_change(&ch->prio, &fh->prio, p);
1298 }
1299
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,
1321 };
1322
1323 static const struct v4l2_file_operations vpif_fops = {
1324         .owner          = THIS_MODULE,
1325         .open           = vpif_open,
1326         .release        = vpif_release,
1327         .ioctl          = video_ioctl2,
1328         .mmap           = vpif_mmap,
1329         .poll           = vpif_poll
1330 };
1331
1332 static struct video_device vpif_video_template = {
1333         .name           = "vpif",
1334         .fops           = &vpif_fops,
1335         .minor          = -1,
1336         .ioctl_ops      = &vpif_ioctl_ops,
1337         .tvnorms        = DM646X_V4L2_STD,
1338         .current_norm   = V4L2_STD_625_50,
1339
1340 };
1341
1342 /*Configure the channels, buffer sizei, request irq */
1343 static int initialize_vpif(void)
1344 {
1345         int free_channel_objects_index;
1346         int free_buffer_channel_index;
1347         int free_buffer_index;
1348         int err = 0, i, j;
1349
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;
1357
1358         /* Set buffer size to min buffers size if invalid buffer size is
1359          * given */
1360         if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1361                 ch2_bufsize =
1362                     config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1363         if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1364                 ch3_bufsize =
1365                     config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1366
1367         config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1368
1369         if (ch2_numbuffers) {
1370                 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1371                                                         ch2_bufsize;
1372         }
1373         config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1374
1375         if (ch3_numbuffers) {
1376                 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1377                                                         ch3_bufsize;
1378         }
1379
1380         /* Allocate memory for six channel objects */
1381         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1382                 vpif_obj.dev[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;
1387                         err = -ENOMEM;
1388                         goto vpif_init_free_channel_objects;
1389                 }
1390         }
1391
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];
1395
1396         return 0;
1397
1398 vpif_init_free_channel_objects:
1399         for (j = 0; j < free_channel_objects_index; j++)
1400                 kfree(vpif_obj.dev[j]);
1401         return err;
1402 }
1403
1404 /*
1405  * vpif_probe: This function creates device entries by register itself to the
1406  * V4L2 driver and initializes fields of each channel objects
1407  */
1408 static __init int vpif_probe(struct platform_device *pdev)
1409 {
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;
1418         int subdev_count;
1419
1420         vpif_dev = &pdev->dev;
1421         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1422         if (!res) {
1423                 v4l2_err(vpif_dev->driver,
1424                                 "Error getting platform resource\n");
1425                 return -ENOENT;
1426         }
1427
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");
1431                 return -ENXIO;
1432         }
1433
1434         vpif_base = ioremap_nocache(res->start, res->end - res->start + 1);
1435         if (!vpif_base) {
1436                 v4l2_err(vpif_dev->driver, "Unable to ioremap VPIF reg\n");
1437                 err = -ENXIO;
1438                 goto resource_exit;
1439         }
1440
1441         vpif_base_addr_init(vpif_base);
1442
1443         initialize_vpif();
1444
1445         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1446         if (err) {
1447                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1448                 return err;
1449         }
1450
1451         k = 0;
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,
1455                                         "DM646x_Display",
1456                                 (void *)(&vpif_obj.dev[k]->channel_id))) {
1457                                 err = -EBUSY;
1458                                 goto vpif_int_err;
1459                         }
1460                 }
1461                 k++;
1462         }
1463
1464         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1465
1466                 /* Get the pointer to the channel object */
1467                 ch = vpif_obj.dev[i];
1468
1469                 /* Allocate memory for video device */
1470                 vfd = video_device_alloc();
1471                 if (vfd == NULL) {
1472                         for (j = 0; j < i; j++) {
1473                                 ch = vpif_obj.dev[j];
1474                                 video_device_release(ch->video_dev);
1475                         }
1476                         err = -ENOMEM;
1477                         goto video_dev_alloc_exit;
1478                 }
1479
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);
1489
1490                 /* Set video_dev to the video device */
1491                 ch->video_dev = vfd;
1492         }
1493
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];
1512
1513                 }
1514                 ch->initialized = 0;
1515                 ch->channel_id = j;
1516                 if (j < 2)
1517                         ch->common[VPIF_VIDEO_INDEX].numbuffers =
1518                             config_params.numbuffers[ch->channel_id];
1519                 else
1520                         ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1521
1522                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1523
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;
1528
1529                 /* register video device */
1530                 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1531                                 (int)ch, (int)&ch->video_dev);
1532
1533                 err = video_register_device(ch->video_dev,
1534                                           VFL_TYPE_GRABBER, (j ? 3 : 2));
1535                 if (err < 0)
1536                         goto probe_out;
1537
1538                 video_set_drvdata(ch->video_dev, ch);
1539         }
1540
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,
1546                                                                 GFP_KERNEL);
1547         if (vpif_obj.sd == NULL) {
1548                 vpif_err("unable to allocate memory for subdevice pointers\n");
1549                 err = -ENOMEM;
1550                 goto probe_out;
1551         }
1552
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,
1556                                                 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;
1561                 }
1562
1563                 if (vpif_obj.sd[i])
1564                         vpif_obj.sd[i]->grp_id = 1 << i;
1565         }
1566
1567         return 0;
1568
1569 probe_subdev_out:
1570         kfree(vpif_obj.sd);
1571 probe_out:
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;
1577         }
1578 vpif_int_err:
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);
1585                 m = res->end;
1586         }
1587 video_dev_alloc_exit:
1588         iounmap(vpif_base);
1589 resource_exit:
1590         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1591         release_mem_region(res->start, res->end - res->start + 1);
1592
1593         return err;
1594 }
1595
1596 /*
1597  * vpif_remove: It un-register channels from V4L2 driver
1598  */
1599 static int vpif_remove(struct platform_device *device)
1600 {
1601         struct channel_obj *ch;
1602         int i;
1603
1604         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1605
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);
1612
1613                 ch->video_dev = NULL;
1614         }
1615
1616         return 0;
1617 }
1618
1619 static struct platform_driver vpif_driver = {
1620         .driver = {
1621                         .name   = "vpif_display",
1622                         .owner  = THIS_MODULE,
1623         },
1624         .probe  = vpif_probe,
1625         .remove = vpif_remove,
1626 };
1627
1628 static __init int vpif_init(void)
1629 {
1630         return platform_driver_register(&vpif_driver);
1631 }
1632
1633 /*
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
1636  * objects.
1637  */
1638 static void vpif_cleanup(void)
1639 {
1640         struct platform_device *pdev;
1641         struct resource *res;
1642         int irq_num;
1643         int i = 0;
1644
1645         pdev = container_of(vpif_dev, struct platform_device, dev);
1646
1647         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1648                 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1649                         free_irq(irq_num,
1650                                  (void *)(&vpif_obj.dev[i]->channel_id));
1651                 i++;
1652         }
1653
1654         iounmap(vpif_base);
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);
1658         kfree(vpif_obj.sd);
1659         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1660                 kfree(vpif_obj.dev[i]);
1661 }
1662
1663 module_init(vpif_init);
1664 module_exit(vpif_cleanup);