[media] vpif_cap/disp: Add support for DV presets
[linux-2.6.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_get_std_info(struct channel_obj *ch)
367 {
368         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
369         struct video_obj *vid_ch = &ch->video;
370         struct vpif_params *vpifparams = &ch->vpifparams;
371         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
372         const struct vpif_channel_config_params *config;
373
374         int index;
375
376         if (!vid_ch->stdid && !vid_ch->dv_preset)
377                 return -EINVAL;
378
379         for (index = 0; index < vpif_ch_params_count; index++) {
380                 config = &ch_params[index];
381                 if (config->hd_sd == 0) {
382                         vpif_dbg(2, debug, "SD format\n");
383                         if (config->stdid & vid_ch->stdid) {
384                                 memcpy(std_info, config, sizeof(*config));
385                                 break;
386                         }
387                 } else {
388                         vpif_dbg(2, debug, "HD format\n");
389                         if (config->dv_preset == vid_ch->dv_preset) {
390                                 memcpy(std_info, config, sizeof(*config));
391                                 break;
392                         }
393                 }
394         }
395
396         if (index == vpif_ch_params_count)
397                 return -EINVAL;
398
399         common->fmt.fmt.pix.width = std_info->width;
400         common->fmt.fmt.pix.height = std_info->height;
401         vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
402                         common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
403
404         /* Set height and width paramateres */
405         ch->common[VPIF_VIDEO_INDEX].height = std_info->height;
406         ch->common[VPIF_VIDEO_INDEX].width = std_info->width;
407
408         return 0;
409 }
410
411 /*
412  * vpif_calculate_offsets: This function calculates buffers offset for Y and C
413  * in the top and bottom field
414  */
415 static void vpif_calculate_offsets(struct channel_obj *ch)
416 {
417         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
418         struct vpif_params *vpifparams = &ch->vpifparams;
419         enum v4l2_field field = common->fmt.fmt.pix.field;
420         struct video_obj *vid_ch = &ch->video;
421         unsigned int hpitch, vpitch, sizeimage;
422
423         if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
424                 if (ch->vpifparams.std_info.frm_fmt)
425                         vid_ch->buf_field = V4L2_FIELD_NONE;
426                 else
427                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
428         } else {
429                 vid_ch->buf_field = common->fmt.fmt.pix.field;
430         }
431
432         if (V4L2_MEMORY_USERPTR == common->memory)
433                 sizeimage = common->fmt.fmt.pix.sizeimage;
434         else
435                 sizeimage = config_params.channel_bufsize[ch->channel_id];
436
437         hpitch = common->fmt.fmt.pix.bytesperline;
438         vpitch = sizeimage / (hpitch * 2);
439         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
440             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
441                 common->ytop_off = 0;
442                 common->ybtm_off = hpitch;
443                 common->ctop_off = sizeimage / 2;
444                 common->cbtm_off = sizeimage / 2 + hpitch;
445         } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
446                 common->ytop_off = 0;
447                 common->ybtm_off = sizeimage / 4;
448                 common->ctop_off = sizeimage / 2;
449                 common->cbtm_off = common->ctop_off + sizeimage / 4;
450         } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
451                 common->ybtm_off = 0;
452                 common->ytop_off = sizeimage / 4;
453                 common->cbtm_off = sizeimage / 2;
454                 common->ctop_off = common->cbtm_off + sizeimage / 4;
455         }
456
457         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
458             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
459                 vpifparams->video_params.storage_mode = 1;
460         } else {
461                 vpifparams->video_params.storage_mode = 0;
462         }
463
464         if (ch->vpifparams.std_info.frm_fmt == 1) {
465                 vpifparams->video_params.hpitch =
466                     common->fmt.fmt.pix.bytesperline;
467         } else {
468                 if ((field == V4L2_FIELD_ANY) ||
469                         (field == V4L2_FIELD_INTERLACED))
470                         vpifparams->video_params.hpitch =
471                             common->fmt.fmt.pix.bytesperline * 2;
472                 else
473                         vpifparams->video_params.hpitch =
474                             common->fmt.fmt.pix.bytesperline;
475         }
476
477         ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
478 }
479
480 static void vpif_config_format(struct channel_obj *ch)
481 {
482         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
483
484         common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
485         if (config_params.numbuffers[ch->channel_id] == 0)
486                 common->memory = V4L2_MEMORY_USERPTR;
487         else
488                 common->memory = V4L2_MEMORY_MMAP;
489
490         common->fmt.fmt.pix.sizeimage =
491                         config_params.channel_bufsize[ch->channel_id];
492         common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
493         common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
494 }
495
496 static int vpif_check_format(struct channel_obj *ch,
497                              struct v4l2_pix_format *pixfmt)
498 {
499         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
500         enum v4l2_field field = pixfmt->field;
501         u32 sizeimage, hpitch, vpitch;
502
503         if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
504                 goto invalid_fmt_exit;
505
506         if (!(VPIF_VALID_FIELD(field)))
507                 goto invalid_fmt_exit;
508
509         if (pixfmt->bytesperline <= 0)
510                 goto invalid_pitch_exit;
511
512         if (V4L2_MEMORY_USERPTR == common->memory)
513                 sizeimage = pixfmt->sizeimage;
514         else
515                 sizeimage = config_params.channel_bufsize[ch->channel_id];
516
517         if (vpif_get_std_info(ch)) {
518                 vpif_err("Error getting the standard info\n");
519                 return -EINVAL;
520         }
521
522         hpitch = pixfmt->bytesperline;
523         vpitch = sizeimage / (hpitch * 2);
524
525         /* Check for valid value of pitch */
526         if ((hpitch < ch->vpifparams.std_info.width) ||
527             (vpitch < ch->vpifparams.std_info.height))
528                 goto invalid_pitch_exit;
529
530         /* Check for 8 byte alignment */
531         if (!ISALIGNED(hpitch)) {
532                 vpif_err("invalid pitch alignment\n");
533                 return -EINVAL;
534         }
535         pixfmt->width = common->fmt.fmt.pix.width;
536         pixfmt->height = common->fmt.fmt.pix.height;
537
538         return 0;
539
540 invalid_fmt_exit:
541         vpif_err("invalid field format\n");
542         return -EINVAL;
543
544 invalid_pitch_exit:
545         vpif_err("invalid pitch\n");
546         return -EINVAL;
547 }
548
549 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
550 {
551         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
552
553         if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
554                 common->set_addr = ch3_set_videobuf_addr;
555         } else {
556                 if (2 == muxmode)
557                         common->set_addr = ch2_set_videobuf_addr_yc_nmux;
558                 else
559                         common->set_addr = ch2_set_videobuf_addr;
560         }
561 }
562
563 /*
564  * vpif_mmap: It is used to map kernel space buffers into user spaces
565  */
566 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
567 {
568         struct vpif_fh *fh = filep->private_data;
569         struct common_obj *common = &fh->channel->common[VPIF_VIDEO_INDEX];
570
571         return videobuf_mmap_mapper(&common->buffer_queue, vma);
572 }
573
574 /*
575  * vpif_poll: It is used for select/poll system call
576  */
577 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
578 {
579         struct vpif_fh *fh = filep->private_data;
580         struct channel_obj *ch = fh->channel;
581         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
582
583         if (common->started)
584                 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
585
586         return 0;
587 }
588
589 /*
590  * vpif_open: It creates object of file handle structure and stores it in
591  * private_data member of filepointer
592  */
593 static int vpif_open(struct file *filep)
594 {
595         struct video_device *vdev = video_devdata(filep);
596         struct channel_obj *ch = NULL;
597         struct vpif_fh *fh = NULL;
598
599         ch = video_get_drvdata(vdev);
600         /* Allocate memory for the file handle object */
601         fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
602         if (fh == NULL) {
603                 vpif_err("unable to allocate memory for file handle object\n");
604                 return -ENOMEM;
605         }
606
607         /* store pointer to fh in private_data member of filep */
608         filep->private_data = fh;
609         fh->channel = ch;
610         fh->initialized = 0;
611         if (!ch->initialized) {
612                 fh->initialized = 1;
613                 ch->initialized = 1;
614                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
615         }
616
617         /* Increment channel usrs counter */
618         atomic_inc(&ch->usrs);
619         /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
620         fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
621         /* Initialize priority of this instance to default priority */
622         fh->prio = V4L2_PRIORITY_UNSET;
623         v4l2_prio_open(&ch->prio, &fh->prio);
624
625         return 0;
626 }
627
628 /*
629  * vpif_release: This function deletes buffer queue, frees the buffers and
630  * the vpif file handle
631  */
632 static int vpif_release(struct file *filep)
633 {
634         struct vpif_fh *fh = filep->private_data;
635         struct channel_obj *ch = fh->channel;
636         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
637
638         if (mutex_lock_interruptible(&common->lock))
639                 return -ERESTARTSYS;
640
641         /* if this instance is doing IO */
642         if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
643                 /* Reset io_usrs member of channel object */
644                 common->io_usrs = 0;
645                 /* Disable channel */
646                 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
647                         enable_channel2(0);
648                         channel2_intr_enable(0);
649                 }
650                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
651                     (2 == common->started)) {
652                         enable_channel3(0);
653                         channel3_intr_enable(0);
654                 }
655                 common->started = 0;
656                 /* Free buffers allocated */
657                 videobuf_queue_cancel(&common->buffer_queue);
658                 videobuf_mmap_free(&common->buffer_queue);
659                 common->numbuffers =
660                     config_params.numbuffers[ch->channel_id];
661         }
662
663         mutex_unlock(&common->lock);
664
665         /* Decrement channel usrs counter */
666         atomic_dec(&ch->usrs);
667         /* If this file handle has initialize encoder device, reset it */
668         if (fh->initialized)
669                 ch->initialized = 0;
670
671         /* Close the priority */
672         v4l2_prio_close(&ch->prio, fh->prio);
673         filep->private_data = NULL;
674         fh->initialized = 0;
675         kfree(fh);
676
677         return 0;
678 }
679
680 /* functions implementing ioctls */
681
682 static int vpif_querycap(struct file *file, void  *priv,
683                                 struct v4l2_capability *cap)
684 {
685         struct vpif_display_config *config = vpif_dev->platform_data;
686
687         cap->version = VPIF_DISPLAY_VERSION_CODE;
688         cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
689         strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
690         strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
691         strlcpy(cap->card, config->card_name, sizeof(cap->card));
692
693         return 0;
694 }
695
696 static int vpif_enum_fmt_vid_out(struct file *file, void  *priv,
697                                         struct v4l2_fmtdesc *fmt)
698 {
699         if (fmt->index != 0) {
700                 vpif_err("Invalid format index\n");
701                 return -EINVAL;
702         }
703
704         /* Fill in the information about format */
705         fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
706         strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
707         fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
708
709         return 0;
710 }
711
712 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
713                                 struct v4l2_format *fmt)
714 {
715         struct vpif_fh *fh = priv;
716         struct channel_obj *ch = fh->channel;
717         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
718
719         /* Check the validity of the buffer type */
720         if (common->fmt.type != fmt->type)
721                 return -EINVAL;
722
723         /* Fill in the information about format */
724         if (mutex_lock_interruptible(&common->lock))
725                 return -ERESTARTSYS;
726
727         if (vpif_get_std_info(ch)) {
728                 vpif_err("Error getting the standard info\n");
729                 return -EINVAL;
730         }
731
732         *fmt = common->fmt;
733         mutex_unlock(&common->lock);
734         return 0;
735 }
736
737 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
738                                 struct v4l2_format *fmt)
739 {
740         struct vpif_fh *fh = priv;
741         struct v4l2_pix_format *pixfmt;
742         struct channel_obj *ch = fh->channel;
743         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
744         int ret = 0;
745
746         if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
747             || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
748                 if (!fh->initialized) {
749                         vpif_dbg(1, debug, "Channel Busy\n");
750                         return -EBUSY;
751                 }
752
753                 /* Check for the priority */
754                 ret = v4l2_prio_check(&ch->prio, fh->prio);
755                 if (0 != ret)
756                         return ret;
757                 fh->initialized = 1;
758         }
759
760         if (common->started) {
761                 vpif_dbg(1, debug, "Streaming in progress\n");
762                 return -EBUSY;
763         }
764
765         pixfmt = &fmt->fmt.pix;
766         /* Check for valid field format */
767         ret = vpif_check_format(ch, pixfmt);
768         if (ret)
769                 return ret;
770
771         /* store the pix format in the channel object */
772         common->fmt.fmt.pix = *pixfmt;
773         /* store the format in the channel object */
774         if (mutex_lock_interruptible(&common->lock))
775                 return -ERESTARTSYS;
776
777         common->fmt = *fmt;
778         mutex_unlock(&common->lock);
779
780         return 0;
781 }
782
783 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
784                                 struct v4l2_format *fmt)
785 {
786         struct vpif_fh *fh = priv;
787         struct channel_obj *ch = fh->channel;
788         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
789         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
790         int ret = 0;
791
792         ret = vpif_check_format(ch, pixfmt);
793         if (ret) {
794                 *pixfmt = common->fmt.fmt.pix;
795                 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
796         }
797
798         return ret;
799 }
800
801 static int vpif_reqbufs(struct file *file, void *priv,
802                         struct v4l2_requestbuffers *reqbuf)
803 {
804         struct vpif_fh *fh = priv;
805         struct channel_obj *ch = fh->channel;
806         struct common_obj *common;
807         enum v4l2_field field;
808         u8 index = 0;
809         int ret = 0;
810
811         /* This file handle has not initialized the channel,
812            It is not allowed to do settings */
813         if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
814             || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
815                 if (!fh->initialized) {
816                         vpif_err("Channel Busy\n");
817                         return -EBUSY;
818                 }
819         }
820
821         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
822                 return -EINVAL;
823
824         index = VPIF_VIDEO_INDEX;
825
826         common = &ch->common[index];
827         if (mutex_lock_interruptible(&common->lock))
828                 return -ERESTARTSYS;
829
830         if (common->fmt.type != reqbuf->type) {
831                 ret = -EINVAL;
832                 goto reqbuf_exit;
833         }
834
835         if (0 != common->io_usrs) {
836                 ret = -EBUSY;
837                 goto reqbuf_exit;
838         }
839
840         if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
841                 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
842                         field = V4L2_FIELD_INTERLACED;
843                 else
844                         field = common->fmt.fmt.pix.field;
845         } else {
846                 field = V4L2_VBI_INTERLACED;
847         }
848
849         /* Initialize videobuf queue as per the buffer type */
850         videobuf_queue_dma_contig_init(&common->buffer_queue,
851                                             &video_qops, NULL,
852                                             &common->irqlock,
853                                             reqbuf->type, field,
854                                             sizeof(struct videobuf_buffer), fh,
855                                             NULL);
856
857         /* Set io allowed member of file handle to TRUE */
858         fh->io_allowed[index] = 1;
859         /* Increment io usrs member of channel object to 1 */
860         common->io_usrs = 1;
861         /* Store type of memory requested in channel object */
862         common->memory = reqbuf->memory;
863         INIT_LIST_HEAD(&common->dma_queue);
864
865         /* Allocate buffers */
866         ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
867
868 reqbuf_exit:
869         mutex_unlock(&common->lock);
870         return ret;
871 }
872
873 static int vpif_querybuf(struct file *file, void *priv,
874                                 struct v4l2_buffer *tbuf)
875 {
876         struct vpif_fh *fh = priv;
877         struct channel_obj *ch = fh->channel;
878         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
879
880         if (common->fmt.type != tbuf->type)
881                 return -EINVAL;
882
883         return videobuf_querybuf(&common->buffer_queue, tbuf);
884 }
885
886 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
887 {
888
889         struct vpif_fh *fh = priv;
890         struct channel_obj *ch = fh->channel;
891         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
892         struct v4l2_buffer tbuf = *buf;
893         struct videobuf_buffer *buf1;
894         unsigned long addr = 0;
895         unsigned long flags;
896         int ret = 0;
897
898         if (common->fmt.type != tbuf.type)
899                 return -EINVAL;
900
901         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
902                 vpif_err("fh->io_allowed\n");
903                 return -EACCES;
904         }
905
906         if (!(list_empty(&common->dma_queue)) ||
907             (common->cur_frm != common->next_frm) ||
908             !(common->started) ||
909             (common->started && (0 == ch->field_id)))
910                 return videobuf_qbuf(&common->buffer_queue, buf);
911
912         /* bufferqueue is empty store buffer address in VPIF registers */
913         mutex_lock(&common->buffer_queue.vb_lock);
914         buf1 = common->buffer_queue.bufs[tbuf.index];
915         if (buf1->memory != tbuf.memory) {
916                 vpif_err("invalid buffer type\n");
917                 goto qbuf_exit;
918         }
919
920         if ((buf1->state == VIDEOBUF_QUEUED) ||
921             (buf1->state == VIDEOBUF_ACTIVE)) {
922                 vpif_err("invalid state\n");
923                 goto qbuf_exit;
924         }
925
926         switch (buf1->memory) {
927         case V4L2_MEMORY_MMAP:
928                 if (buf1->baddr == 0)
929                         goto qbuf_exit;
930                 break;
931
932         case V4L2_MEMORY_USERPTR:
933                 if (tbuf.length < buf1->bsize)
934                         goto qbuf_exit;
935
936                 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
937                             && (buf1->baddr != tbuf.m.userptr)) {
938                         vpif_buffer_release(&common->buffer_queue, buf1);
939                         buf1->baddr = tbuf.m.userptr;
940                 }
941                 break;
942
943         default:
944                 goto qbuf_exit;
945         }
946
947         local_irq_save(flags);
948         ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
949                                         common->buffer_queue.field);
950         if (ret < 0) {
951                 local_irq_restore(flags);
952                 goto qbuf_exit;
953         }
954
955         buf1->state = VIDEOBUF_ACTIVE;
956         addr = buf1->boff;
957         common->next_frm = buf1;
958         if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
959                 common->set_addr((addr + common->ytop_off),
960                                  (addr + common->ybtm_off),
961                                  (addr + common->ctop_off),
962                                  (addr + common->cbtm_off));
963         }
964
965         local_irq_restore(flags);
966         list_add_tail(&buf1->stream, &common->buffer_queue.stream);
967         mutex_unlock(&common->buffer_queue.vb_lock);
968         return 0;
969
970 qbuf_exit:
971         mutex_unlock(&common->buffer_queue.vb_lock);
972         return -EINVAL;
973 }
974
975 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
976 {
977         struct vpif_fh *fh = priv;
978         struct channel_obj *ch = fh->channel;
979         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
980         int ret = 0;
981
982         if (!(*std_id & DM646X_V4L2_STD))
983                 return -EINVAL;
984
985         if (common->started) {
986                 vpif_err("streaming in progress\n");
987                 return -EBUSY;
988         }
989
990         /* Call encoder subdevice function to set the standard */
991         if (mutex_lock_interruptible(&common->lock))
992                 return -ERESTARTSYS;
993
994         ch->video.stdid = *std_id;
995         /* Get the information about the standard */
996         if (vpif_get_std_info(ch)) {
997                 vpif_err("Error getting the standard info\n");
998                 return -EINVAL;
999         }
1000
1001         if ((ch->vpifparams.std_info.width *
1002                 ch->vpifparams.std_info.height * 2) >
1003                 config_params.channel_bufsize[ch->channel_id]) {
1004                 vpif_err("invalid std for this size\n");
1005                 ret = -EINVAL;
1006                 goto s_std_exit;
1007         }
1008
1009         common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1010         /* Configure the default format information */
1011         vpif_config_format(ch);
1012
1013         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1014                                                 s_std_output, *std_id);
1015         if (ret < 0) {
1016                 vpif_err("Failed to set output standard\n");
1017                 goto s_std_exit;
1018         }
1019
1020         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1021                                                         s_std, *std_id);
1022         if (ret < 0)
1023                 vpif_err("Failed to set standard for sub devices\n");
1024
1025 s_std_exit:
1026         mutex_unlock(&common->lock);
1027         return ret;
1028 }
1029
1030 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1031 {
1032         struct vpif_fh *fh = priv;
1033         struct channel_obj *ch = fh->channel;
1034
1035         *std = ch->video.stdid;
1036         return 0;
1037 }
1038
1039 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1040 {
1041         struct vpif_fh *fh = priv;
1042         struct channel_obj *ch = fh->channel;
1043         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1044
1045         return videobuf_dqbuf(&common->buffer_queue, p,
1046                                         (file->f_flags & O_NONBLOCK));
1047 }
1048
1049 static int vpif_streamon(struct file *file, void *priv,
1050                                 enum v4l2_buf_type buftype)
1051 {
1052         struct vpif_fh *fh = priv;
1053         struct channel_obj *ch = fh->channel;
1054         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1055         struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1056         struct vpif_params *vpif = &ch->vpifparams;
1057         struct vpif_display_config *vpif_config_data =
1058                                         vpif_dev->platform_data;
1059         unsigned long addr = 0;
1060         int ret = 0;
1061
1062         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1063                 vpif_err("buffer type not supported\n");
1064                 return -EINVAL;
1065         }
1066
1067         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1068                 vpif_err("fh->io_allowed\n");
1069                 return -EACCES;
1070         }
1071
1072         /* If Streaming is already started, return error */
1073         if (common->started) {
1074                 vpif_err("channel->started\n");
1075                 return -EBUSY;
1076         }
1077
1078         if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1079                 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1080                 ch->vpifparams.std_info.ycmux_mode == 0)
1081                 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1082                 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1083                 vpif_err("other channel is using\n");
1084                 return -EBUSY;
1085         }
1086
1087         ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1088         if (ret < 0)
1089                 return ret;
1090
1091         /* Call videobuf_streamon to start streaming  in videobuf */
1092         ret = videobuf_streamon(&common->buffer_queue);
1093         if (ret < 0) {
1094                 vpif_err("videobuf_streamon\n");
1095                 return ret;
1096         }
1097
1098         if (mutex_lock_interruptible(&common->lock))
1099                 return -ERESTARTSYS;
1100
1101         /* If buffer queue is empty, return error */
1102         if (list_empty(&common->dma_queue)) {
1103                 vpif_err("buffer queue is empty\n");
1104                 ret = -EIO;
1105                 goto streamon_exit;
1106         }
1107
1108         /* Get the next frame from the buffer queue */
1109         common->next_frm = common->cur_frm =
1110                             list_entry(common->dma_queue.next,
1111                                        struct videobuf_buffer, queue);
1112
1113         list_del(&common->cur_frm->queue);
1114         /* Mark state of the current frame to active */
1115         common->cur_frm->state = VIDEOBUF_ACTIVE;
1116
1117         /* Initialize field_id and started member */
1118         ch->field_id = 0;
1119         common->started = 1;
1120         if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1121                 addr = common->cur_frm->boff;
1122                 /* Calculate the offset for Y and C data  in the buffer */
1123                 vpif_calculate_offsets(ch);
1124
1125                 if ((ch->vpifparams.std_info.frm_fmt &&
1126                         ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1127                         && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1128                         || (!ch->vpifparams.std_info.frm_fmt
1129                         && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1130                         vpif_err("conflict in field format and std format\n");
1131                         ret = -EINVAL;
1132                         goto streamon_exit;
1133                 }
1134
1135                 /* clock settings */
1136                 ret =
1137                  vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1138                                                 ch->vpifparams.std_info.hd_sd);
1139                 if (ret < 0) {
1140                         vpif_err("can't set clock\n");
1141                         goto streamon_exit;
1142                 }
1143
1144                 /* set the parameters and addresses */
1145                 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1146                 if (ret < 0)
1147                         goto streamon_exit;
1148
1149                 common->started = ret;
1150                 vpif_config_addr(ch, ret);
1151                 common->set_addr((addr + common->ytop_off),
1152                                  (addr + common->ybtm_off),
1153                                  (addr + common->ctop_off),
1154                                  (addr + common->cbtm_off));
1155
1156                 /* Set interrupt for both the fields in VPIF
1157                    Register enable channel in VPIF register */
1158                 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1159                         channel2_intr_assert();
1160                         channel2_intr_enable(1);
1161                         enable_channel2(1);
1162                 }
1163
1164                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1165                         || (common->started == 2)) {
1166                         channel3_intr_assert();
1167                         channel3_intr_enable(1);
1168                         enable_channel3(1);
1169                 }
1170                 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1171         }
1172
1173 streamon_exit:
1174         mutex_unlock(&common->lock);
1175         return ret;
1176 }
1177
1178 static int vpif_streamoff(struct file *file, void *priv,
1179                                 enum v4l2_buf_type buftype)
1180 {
1181         struct vpif_fh *fh = priv;
1182         struct channel_obj *ch = fh->channel;
1183         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1184
1185         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1186                 vpif_err("buffer type not supported\n");
1187                 return -EINVAL;
1188         }
1189
1190         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1191                 vpif_err("fh->io_allowed\n");
1192                 return -EACCES;
1193         }
1194
1195         if (!common->started) {
1196                 vpif_err("channel->started\n");
1197                 return -EINVAL;
1198         }
1199
1200         if (mutex_lock_interruptible(&common->lock))
1201                 return -ERESTARTSYS;
1202
1203         if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1204                 /* disable channel */
1205                 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1206                         enable_channel2(0);
1207                         channel2_intr_enable(0);
1208                 }
1209                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1210                                         (2 == common->started)) {
1211                         enable_channel3(0);
1212                         channel3_intr_enable(0);
1213                 }
1214         }
1215
1216         common->started = 0;
1217         mutex_unlock(&common->lock);
1218
1219         return videobuf_streamoff(&common->buffer_queue);
1220 }
1221
1222 static int vpif_cropcap(struct file *file, void *priv,
1223                         struct v4l2_cropcap *crop)
1224 {
1225         struct vpif_fh *fh = priv;
1226         struct channel_obj *ch = fh->channel;
1227         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1228         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1229                 return -EINVAL;
1230
1231         crop->bounds.left = crop->bounds.top = 0;
1232         crop->defrect.left = crop->defrect.top = 0;
1233         crop->defrect.height = crop->bounds.height = common->height;
1234         crop->defrect.width = crop->bounds.width = common->width;
1235
1236         return 0;
1237 }
1238
1239 static int vpif_enum_output(struct file *file, void *fh,
1240                                 struct v4l2_output *output)
1241 {
1242
1243         struct vpif_display_config *config = vpif_dev->platform_data;
1244
1245         if (output->index >= config->output_count) {
1246                 vpif_dbg(1, debug, "Invalid output index\n");
1247                 return -EINVAL;
1248         }
1249
1250         strcpy(output->name, config->output[output->index]);
1251         output->type = V4L2_OUTPUT_TYPE_ANALOG;
1252         output->std = DM646X_V4L2_STD;
1253
1254         return 0;
1255 }
1256
1257 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1258 {
1259         struct vpif_fh *fh = priv;
1260         struct channel_obj *ch = fh->channel;
1261         struct video_obj *vid_ch = &ch->video;
1262         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1263         int ret = 0;
1264
1265         if (mutex_lock_interruptible(&common->lock))
1266                 return -ERESTARTSYS;
1267
1268         if (common->started) {
1269                 vpif_err("Streaming in progress\n");
1270                 ret = -EBUSY;
1271                 goto s_output_exit;
1272         }
1273
1274         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1275                                                         s_routing, 0, i, 0);
1276
1277         if (ret < 0)
1278                 vpif_err("Failed to set output standard\n");
1279
1280         vid_ch->output_id = i;
1281
1282 s_output_exit:
1283         mutex_unlock(&common->lock);
1284         return ret;
1285 }
1286
1287 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1288 {
1289         struct vpif_fh *fh = priv;
1290         struct channel_obj *ch = fh->channel;
1291         struct video_obj *vid_ch = &ch->video;
1292
1293         *i = vid_ch->output_id;
1294
1295         return 0;
1296 }
1297
1298 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1299 {
1300         struct vpif_fh *fh = priv;
1301         struct channel_obj *ch = fh->channel;
1302
1303         *p = v4l2_prio_max(&ch->prio);
1304
1305         return 0;
1306 }
1307
1308 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1309 {
1310         struct vpif_fh *fh = priv;
1311         struct channel_obj *ch = fh->channel;
1312
1313         return v4l2_prio_change(&ch->prio, &fh->prio, p);
1314 }
1315
1316 /**
1317  * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1318  * @file: file ptr
1319  * @priv: file handle
1320  * @preset: input preset
1321  */
1322 static int vpif_enum_dv_presets(struct file *file, void *priv,
1323                 struct v4l2_dv_enum_preset *preset)
1324 {
1325         struct vpif_fh *fh = priv;
1326         struct channel_obj *ch = fh->channel;
1327         struct video_obj *vid_ch = &ch->video;
1328
1329         return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1330                         video, enum_dv_presets, preset);
1331 }
1332
1333 /**
1334  * vpif_s_dv_presets() - S_DV_PRESETS handler
1335  * @file: file ptr
1336  * @priv: file handle
1337  * @preset: input preset
1338  */
1339 static int vpif_s_dv_preset(struct file *file, void *priv,
1340                 struct v4l2_dv_preset *preset)
1341 {
1342         struct vpif_fh *fh = priv;
1343         struct channel_obj *ch = fh->channel;
1344         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1345         struct video_obj *vid_ch = &ch->video;
1346         int ret = 0;
1347
1348         if (common->started) {
1349                 vpif_dbg(1, debug, "streaming in progress\n");
1350                 return -EBUSY;
1351         }
1352
1353         ret = v4l2_prio_check(&ch->prio, fh->prio);
1354         if (ret != 0)
1355                 return ret;
1356
1357         fh->initialized = 1;
1358
1359         /* Call encoder subdevice function to set the standard */
1360         if (mutex_lock_interruptible(&common->lock))
1361                 return -ERESTARTSYS;
1362
1363         ch->video.dv_preset = preset->preset;
1364         ch->video.stdid = V4L2_STD_UNKNOWN;
1365
1366         /* Get the information about the standard */
1367         if (vpif_get_std_info(ch)) {
1368                 ret = -EINVAL;
1369                 vpif_dbg(1, debug, "Error getting the standard info\n");
1370         } else {
1371                 /* Configure the default format information */
1372                 vpif_config_format(ch);
1373
1374                 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1375                                 video, s_dv_preset, preset);
1376         }
1377
1378         mutex_unlock(&common->lock);
1379
1380         return ret;
1381 }
1382 /**
1383  * vpif_g_dv_presets() - G_DV_PRESETS handler
1384  * @file: file ptr
1385  * @priv: file handle
1386  * @preset: input preset
1387  */
1388 static int vpif_g_dv_preset(struct file *file, void *priv,
1389                 struct v4l2_dv_preset *preset)
1390 {
1391         struct vpif_fh *fh = priv;
1392         struct channel_obj *ch = fh->channel;
1393
1394         preset->preset = ch->video.dv_preset;
1395
1396         return 0;
1397 }
1398
1399 /*
1400  * vpif_g_chip_ident() - Identify the chip
1401  * @file: file ptr
1402  * @priv: file handle
1403  * @chip: chip identity
1404  *
1405  * Returns zero or -EINVAL if read operations fails.
1406  */
1407 static int vpif_g_chip_ident(struct file *file, void *priv,
1408                 struct v4l2_dbg_chip_ident *chip)
1409 {
1410         chip->ident = V4L2_IDENT_NONE;
1411         chip->revision = 0;
1412         if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1413                         chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1414                 vpif_dbg(2, debug, "match_type is invalid.\n");
1415                 return -EINVAL;
1416         }
1417
1418         return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1419                         g_chip_ident, chip);
1420 }
1421
1422 #ifdef CONFIG_VIDEO_ADV_DEBUG
1423 /*
1424  * vpif_dbg_g_register() - Read register
1425  * @file: file ptr
1426  * @priv: file handle
1427  * @reg: register to be read
1428  *
1429  * Debugging only
1430  * Returns zero or -EINVAL if read operations fails.
1431  */
1432 static int vpif_dbg_g_register(struct file *file, void *priv,
1433                 struct v4l2_dbg_register *reg){
1434         struct vpif_fh *fh = priv;
1435         struct channel_obj *ch = fh->channel;
1436         struct video_obj *vid_ch = &ch->video;
1437
1438         return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1439                         g_register, reg);
1440 }
1441
1442 /*
1443  * vpif_dbg_s_register() - Write to register
1444  * @file: file ptr
1445  * @priv: file handle
1446  * @reg: register to be modified
1447  *
1448  * Debugging only
1449  * Returns zero or -EINVAL if write operations fails.
1450  */
1451 static int vpif_dbg_s_register(struct file *file, void *priv,
1452                 struct v4l2_dbg_register *reg){
1453         struct vpif_fh *fh = priv;
1454         struct channel_obj *ch = fh->channel;
1455         struct video_obj *vid_ch = &ch->video;
1456
1457         return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1458                         s_register, reg);
1459 }
1460 #endif
1461
1462 /*
1463  * vpif_log_status() - Status information
1464  * @file: file ptr
1465  * @priv: file handle
1466  *
1467  * Returns zero.
1468  */
1469 static int vpif_log_status(struct file *filep, void *priv)
1470 {
1471         /* status for sub devices */
1472         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1473
1474         return 0;
1475 }
1476
1477 /* vpif display ioctl operations */
1478 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1479         .vidioc_querycap                = vpif_querycap,
1480         .vidioc_g_priority              = vpif_g_priority,
1481         .vidioc_s_priority              = vpif_s_priority,
1482         .vidioc_enum_fmt_vid_out        = vpif_enum_fmt_vid_out,
1483         .vidioc_g_fmt_vid_out           = vpif_g_fmt_vid_out,
1484         .vidioc_s_fmt_vid_out           = vpif_s_fmt_vid_out,
1485         .vidioc_try_fmt_vid_out         = vpif_try_fmt_vid_out,
1486         .vidioc_reqbufs                 = vpif_reqbufs,
1487         .vidioc_querybuf                = vpif_querybuf,
1488         .vidioc_qbuf                    = vpif_qbuf,
1489         .vidioc_dqbuf                   = vpif_dqbuf,
1490         .vidioc_streamon                = vpif_streamon,
1491         .vidioc_streamoff               = vpif_streamoff,
1492         .vidioc_s_std                   = vpif_s_std,
1493         .vidioc_g_std                   = vpif_g_std,
1494         .vidioc_enum_output             = vpif_enum_output,
1495         .vidioc_s_output                = vpif_s_output,
1496         .vidioc_g_output                = vpif_g_output,
1497         .vidioc_cropcap                 = vpif_cropcap,
1498         .vidioc_enum_dv_presets         = vpif_enum_dv_presets,
1499         .vidioc_s_dv_preset             = vpif_s_dv_preset,
1500         .vidioc_g_dv_preset             = vpif_g_dv_preset,
1501         .vidioc_g_chip_ident            = vpif_g_chip_ident,
1502 #ifdef CONFIG_VIDEO_ADV_DEBUG
1503         .vidioc_g_register              = vpif_dbg_g_register,
1504         .vidioc_s_register              = vpif_dbg_s_register,
1505 #endif
1506         .vidioc_log_status              = vpif_log_status,
1507 };
1508
1509 static const struct v4l2_file_operations vpif_fops = {
1510         .owner          = THIS_MODULE,
1511         .open           = vpif_open,
1512         .release        = vpif_release,
1513         .ioctl          = video_ioctl2,
1514         .mmap           = vpif_mmap,
1515         .poll           = vpif_poll
1516 };
1517
1518 static struct video_device vpif_video_template = {
1519         .name           = "vpif",
1520         .fops           = &vpif_fops,
1521         .ioctl_ops      = &vpif_ioctl_ops,
1522         .tvnorms        = DM646X_V4L2_STD,
1523         .current_norm   = V4L2_STD_625_50,
1524
1525 };
1526
1527 /*Configure the channels, buffer sizei, request irq */
1528 static int initialize_vpif(void)
1529 {
1530         int free_channel_objects_index;
1531         int free_buffer_channel_index;
1532         int free_buffer_index;
1533         int err = 0, i, j;
1534
1535         /* Default number of buffers should be 3 */
1536         if ((ch2_numbuffers > 0) &&
1537             (ch2_numbuffers < config_params.min_numbuffers))
1538                 ch2_numbuffers = config_params.min_numbuffers;
1539         if ((ch3_numbuffers > 0) &&
1540             (ch3_numbuffers < config_params.min_numbuffers))
1541                 ch3_numbuffers = config_params.min_numbuffers;
1542
1543         /* Set buffer size to min buffers size if invalid buffer size is
1544          * given */
1545         if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1546                 ch2_bufsize =
1547                     config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1548         if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1549                 ch3_bufsize =
1550                     config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1551
1552         config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1553
1554         if (ch2_numbuffers) {
1555                 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1556                                                         ch2_bufsize;
1557         }
1558         config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1559
1560         if (ch3_numbuffers) {
1561                 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1562                                                         ch3_bufsize;
1563         }
1564
1565         /* Allocate memory for six channel objects */
1566         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1567                 vpif_obj.dev[i] =
1568                     kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1569                 /* If memory allocation fails, return error */
1570                 if (!vpif_obj.dev[i]) {
1571                         free_channel_objects_index = i;
1572                         err = -ENOMEM;
1573                         goto vpif_init_free_channel_objects;
1574                 }
1575         }
1576
1577         free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1578         free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1579         free_buffer_index = config_params.numbuffers[i - 1];
1580
1581         return 0;
1582
1583 vpif_init_free_channel_objects:
1584         for (j = 0; j < free_channel_objects_index; j++)
1585                 kfree(vpif_obj.dev[j]);
1586         return err;
1587 }
1588
1589 /*
1590  * vpif_probe: This function creates device entries by register itself to the
1591  * V4L2 driver and initializes fields of each channel objects
1592  */
1593 static __init int vpif_probe(struct platform_device *pdev)
1594 {
1595         struct vpif_subdev_info *subdevdata;
1596         struct vpif_display_config *config;
1597         int i, j = 0, k, q, m, err = 0;
1598         struct i2c_adapter *i2c_adap;
1599         struct common_obj *common;
1600         struct channel_obj *ch;
1601         struct video_device *vfd;
1602         struct resource *res;
1603         int subdev_count;
1604
1605         vpif_dev = &pdev->dev;
1606
1607         err = initialize_vpif();
1608
1609         if (err) {
1610                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1611                 return err;
1612         }
1613
1614         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1615         if (err) {
1616                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1617                 return err;
1618         }
1619
1620         k = 0;
1621         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1622                 for (i = res->start; i <= res->end; i++) {
1623                         if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1624                                         "DM646x_Display",
1625                                 (void *)(&vpif_obj.dev[k]->channel_id))) {
1626                                 err = -EBUSY;
1627                                 goto vpif_int_err;
1628                         }
1629                 }
1630                 k++;
1631         }
1632
1633         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1634
1635                 /* Get the pointer to the channel object */
1636                 ch = vpif_obj.dev[i];
1637
1638                 /* Allocate memory for video device */
1639                 vfd = video_device_alloc();
1640                 if (vfd == NULL) {
1641                         for (j = 0; j < i; j++) {
1642                                 ch = vpif_obj.dev[j];
1643                                 video_device_release(ch->video_dev);
1644                         }
1645                         err = -ENOMEM;
1646                         goto vpif_int_err;
1647                 }
1648
1649                 /* Initialize field of video device */
1650                 *vfd = vpif_video_template;
1651                 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1652                 vfd->release = video_device_release;
1653                 snprintf(vfd->name, sizeof(vfd->name),
1654                          "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
1655                          (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
1656                          (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
1657                          (VPIF_DISPLAY_VERSION_CODE) & 0xff);
1658
1659                 /* Set video_dev to the video device */
1660                 ch->video_dev = vfd;
1661         }
1662
1663         for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1664                 ch = vpif_obj.dev[j];
1665                 /* Initialize field of the channel objects */
1666                 atomic_set(&ch->usrs, 0);
1667                 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1668                         ch->common[k].numbuffers = 0;
1669                         common = &ch->common[k];
1670                         common->io_usrs = 0;
1671                         common->started = 0;
1672                         spin_lock_init(&common->irqlock);
1673                         mutex_init(&common->lock);
1674                         common->numbuffers = 0;
1675                         common->set_addr = NULL;
1676                         common->ytop_off = common->ybtm_off = 0;
1677                         common->ctop_off = common->cbtm_off = 0;
1678                         common->cur_frm = common->next_frm = NULL;
1679                         memset(&common->fmt, 0, sizeof(common->fmt));
1680                         common->numbuffers = config_params.numbuffers[k];
1681
1682                 }
1683                 ch->initialized = 0;
1684                 ch->channel_id = j;
1685                 if (j < 2)
1686                         ch->common[VPIF_VIDEO_INDEX].numbuffers =
1687                             config_params.numbuffers[ch->channel_id];
1688                 else
1689                         ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1690
1691                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1692
1693                 /* Initialize prio member of channel object */
1694                 v4l2_prio_init(&ch->prio);
1695                 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1696                                                 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1697
1698                 /* register video device */
1699                 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1700                                 (int)ch, (int)&ch->video_dev);
1701
1702                 err = video_register_device(ch->video_dev,
1703                                           VFL_TYPE_GRABBER, (j ? 3 : 2));
1704                 if (err < 0)
1705                         goto probe_out;
1706
1707                 video_set_drvdata(ch->video_dev, ch);
1708         }
1709
1710         i2c_adap = i2c_get_adapter(1);
1711         config = pdev->dev.platform_data;
1712         subdev_count = config->subdev_count;
1713         subdevdata = config->subdevinfo;
1714         vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1715                                                                 GFP_KERNEL);
1716         if (vpif_obj.sd == NULL) {
1717                 vpif_err("unable to allocate memory for subdevice pointers\n");
1718                 err = -ENOMEM;
1719                 goto probe_out;
1720         }
1721
1722         for (i = 0; i < subdev_count; i++) {
1723                 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1724                                                 i2c_adap,
1725                                                 &subdevdata[i].board_info,
1726                                                 NULL);
1727                 if (!vpif_obj.sd[i]) {
1728                         vpif_err("Error registering v4l2 subdevice\n");
1729                         goto probe_subdev_out;
1730                 }
1731
1732                 if (vpif_obj.sd[i])
1733                         vpif_obj.sd[i]->grp_id = 1 << i;
1734         }
1735
1736         return 0;
1737
1738 probe_subdev_out:
1739         kfree(vpif_obj.sd);
1740 probe_out:
1741         for (k = 0; k < j; k++) {
1742                 ch = vpif_obj.dev[k];
1743                 video_unregister_device(ch->video_dev);
1744                 video_device_release(ch->video_dev);
1745                 ch->video_dev = NULL;
1746         }
1747 vpif_int_err:
1748         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1749         vpif_err("VPIF IRQ request failed\n");
1750         for (q = k; k >= 0; k--) {
1751                 for (m = i; m >= res->start; m--)
1752                         free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1753                 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1754                 m = res->end;
1755         }
1756
1757         return err;
1758 }
1759
1760 /*
1761  * vpif_remove: It un-register channels from V4L2 driver
1762  */
1763 static int vpif_remove(struct platform_device *device)
1764 {
1765         struct channel_obj *ch;
1766         int i;
1767
1768         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1769
1770         /* un-register device */
1771         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1772                 /* Get the pointer to the channel object */
1773                 ch = vpif_obj.dev[i];
1774                 /* Unregister video device */
1775                 video_unregister_device(ch->video_dev);
1776
1777                 ch->video_dev = NULL;
1778         }
1779
1780         return 0;
1781 }
1782
1783 static __refdata struct platform_driver vpif_driver = {
1784         .driver = {
1785                         .name   = "vpif_display",
1786                         .owner  = THIS_MODULE,
1787         },
1788         .probe  = vpif_probe,
1789         .remove = vpif_remove,
1790 };
1791
1792 static __init int vpif_init(void)
1793 {
1794         return platform_driver_register(&vpif_driver);
1795 }
1796
1797 /*
1798  * vpif_cleanup: This function un-registers device and driver to the kernel,
1799  * frees requested irq handler and de-allocates memory allocated for channel
1800  * objects.
1801  */
1802 static void vpif_cleanup(void)
1803 {
1804         struct platform_device *pdev;
1805         struct resource *res;
1806         int irq_num;
1807         int i = 0;
1808
1809         pdev = container_of(vpif_dev, struct platform_device, dev);
1810
1811         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1812                 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1813                         free_irq(irq_num,
1814                                  (void *)(&vpif_obj.dev[i]->channel_id));
1815                 i++;
1816         }
1817
1818         platform_driver_unregister(&vpif_driver);
1819         kfree(vpif_obj.sd);
1820         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1821                 kfree(vpif_obj.dev[i]);
1822 }
1823
1824 module_init(vpif_init);
1825 module_exit(vpif_cleanup);