]> Pileus Git - ~andy/linux/blob - drivers/media/video/davinci/vpif_display.c
[media] davinci: vpif display: migrate driver to videobuf2
[~andy/linux] / 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/slab.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 #include <media/v4l2-chip-ident.h>
41
42 #include "vpif_display.h"
43 #include "vpif.h"
44
45 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(VPIF_DISPLAY_VERSION);
48
49 #define VPIF_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 static void vpif_calculate_offsets(struct channel_obj *ch);
86 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
87
88 /*
89  * buffer_prepare: This is the callback function called from vb2_qbuf()
90  * function the buffer is prepared and user space virtual address is converted
91  * into physical address
92  */
93 static int vpif_buffer_prepare(struct vb2_buffer *vb)
94 {
95         struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
96         struct vb2_queue *q = vb->vb2_queue;
97         struct common_obj *common;
98         unsigned long addr;
99
100         common = &fh->channel->common[VPIF_VIDEO_INDEX];
101         if (vb->state != VB2_BUF_STATE_ACTIVE &&
102                 vb->state != VB2_BUF_STATE_PREPARED) {
103                 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
104                 if (vb2_plane_vaddr(vb, 0) &&
105                 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
106                         goto buf_align_exit;
107
108                 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
109                 if (q->streaming &&
110                         (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
111                         if (!ISALIGNED(addr + common->ytop_off) ||
112                         !ISALIGNED(addr + common->ybtm_off) ||
113                         !ISALIGNED(addr + common->ctop_off) ||
114                         !ISALIGNED(addr + common->cbtm_off))
115                                 goto buf_align_exit;
116                 }
117         }
118         return 0;
119
120 buf_align_exit:
121         vpif_err("buffer offset not aligned to 8 bytes\n");
122         return -EINVAL;
123 }
124
125 /*
126  * vpif_buffer_queue_setup: This function allocates memory for the buffers
127  */
128 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
129                                 const struct v4l2_format *fmt,
130                                 unsigned int *nbuffers, unsigned int *nplanes,
131                                 unsigned int sizes[], void *alloc_ctxs[])
132 {
133         struct vpif_fh *fh = vb2_get_drv_priv(vq);
134         struct channel_obj *ch = fh->channel;
135         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
136         unsigned long size;
137
138         if (V4L2_MEMORY_MMAP == common->memory) {
139                 size = config_params.channel_bufsize[ch->channel_id];
140                 /*
141                 * Checking if the buffer size exceeds the available buffer
142                 * ycmux_mode = 0 means 1 channel mode HD and
143                 * ycmux_mode = 1 means 2 channels mode SD
144                 */
145                 if (ch->vpifparams.std_info.ycmux_mode == 0) {
146                         if (config_params.video_limit[ch->channel_id])
147                                 while (size * *nbuffers >
148                                         (config_params.video_limit[0]
149                                                 + config_params.video_limit[1]))
150                                         (*nbuffers)--;
151                 } else {
152                         if (config_params.video_limit[ch->channel_id])
153                                 while (size * *nbuffers >
154                                 config_params.video_limit[ch->channel_id])
155                                         (*nbuffers)--;
156                 }
157         } else {
158                 size = common->fmt.fmt.pix.sizeimage;
159         }
160
161         if (*nbuffers < config_params.min_numbuffers)
162                         *nbuffers = config_params.min_numbuffers;
163
164         *nplanes = 1;
165         sizes[0] = size;
166         alloc_ctxs[0] = common->alloc_ctx;
167         return 0;
168 }
169
170 /*
171  * vpif_buffer_queue: This function adds the buffer to DMA queue
172  */
173 static void vpif_buffer_queue(struct vb2_buffer *vb)
174 {
175         struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
176         struct vpif_disp_buffer *buf = container_of(vb,
177                                 struct vpif_disp_buffer, vb);
178         struct channel_obj *ch = fh->channel;
179         struct common_obj *common;
180
181         common = &ch->common[VPIF_VIDEO_INDEX];
182
183         /* add the buffer to the DMA queue */
184         list_add_tail(&buf->list, &common->dma_queue);
185 }
186
187 /*
188  * vpif_buf_cleanup: This function is called from the videobuf2 layer to
189  * free memory allocated to the buffers
190  */
191 static void vpif_buf_cleanup(struct vb2_buffer *vb)
192 {
193         struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
194         struct vpif_disp_buffer *buf = container_of(vb,
195                                         struct vpif_disp_buffer, vb);
196         struct channel_obj *ch = fh->channel;
197         struct common_obj *common;
198         unsigned long flags;
199
200         common = &ch->common[VPIF_VIDEO_INDEX];
201
202         spin_lock_irqsave(&common->irqlock, flags);
203         if (vb->state == VB2_BUF_STATE_ACTIVE)
204                 list_del_init(&buf->list);
205         spin_unlock_irqrestore(&common->irqlock, flags);
206 }
207
208 static void vpif_wait_prepare(struct vb2_queue *vq)
209 {
210         struct vpif_fh *fh = vb2_get_drv_priv(vq);
211         struct channel_obj *ch = fh->channel;
212         struct common_obj *common;
213
214         common = &ch->common[VPIF_VIDEO_INDEX];
215         mutex_unlock(&common->lock);
216 }
217
218 static void vpif_wait_finish(struct vb2_queue *vq)
219 {
220         struct vpif_fh *fh = vb2_get_drv_priv(vq);
221         struct channel_obj *ch = fh->channel;
222         struct common_obj *common;
223
224         common = &ch->common[VPIF_VIDEO_INDEX];
225         mutex_lock(&common->lock);
226 }
227
228 static int vpif_buffer_init(struct vb2_buffer *vb)
229 {
230         struct vpif_disp_buffer *buf = container_of(vb,
231                                         struct vpif_disp_buffer, vb);
232
233         INIT_LIST_HEAD(&buf->list);
234
235         return 0;
236 }
237
238 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
239
240 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
241 {
242         struct vpif_display_config *vpif_config_data =
243                                         vpif_dev->platform_data;
244         struct vpif_fh *fh = vb2_get_drv_priv(vq);
245         struct channel_obj *ch = fh->channel;
246         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
247         struct vpif_params *vpif = &ch->vpifparams;
248         unsigned long addr = 0;
249         int ret;
250
251         /* If buffer queue is empty, return error */
252         if (list_empty(&common->dma_queue)) {
253                 vpif_err("buffer queue is empty\n");
254                 return -EIO;
255         }
256
257         /* Get the next frame from the buffer queue */
258         common->next_frm = common->cur_frm =
259                             list_entry(common->dma_queue.next,
260                                        struct vpif_disp_buffer, list);
261
262         list_del(&common->cur_frm->list);
263         /* Mark state of the current frame to active */
264         common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
265
266         /* Initialize field_id and started member */
267         ch->field_id = 0;
268         common->started = 1;
269         addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
270         /* Calculate the offset for Y and C data  in the buffer */
271         vpif_calculate_offsets(ch);
272
273         if ((ch->vpifparams.std_info.frm_fmt &&
274                 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
275                 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
276                 || (!ch->vpifparams.std_info.frm_fmt
277                 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
278                 vpif_err("conflict in field format and std format\n");
279                 return -EINVAL;
280         }
281
282         /* clock settings */
283         ret =
284             vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
285                                         ch->vpifparams.std_info.hd_sd);
286         if (ret < 0) {
287                 vpif_err("can't set clock\n");
288                 return ret;
289         }
290
291         /* set the parameters and addresses */
292         ret = vpif_set_video_params(vpif, ch->channel_id + 2);
293         if (ret < 0)
294                 return ret;
295
296         common->started = ret;
297         vpif_config_addr(ch, ret);
298         common->set_addr((addr + common->ytop_off),
299                             (addr + common->ybtm_off),
300                             (addr + common->ctop_off),
301                             (addr + common->cbtm_off));
302
303         /* Set interrupt for both the fields in VPIF
304             Register enable channel in VPIF register */
305         if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
306                 channel2_intr_assert();
307                 channel2_intr_enable(1);
308                 enable_channel2(1);
309         }
310
311         if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
312                 || (common->started == 2)) {
313                 channel3_intr_assert();
314                 channel3_intr_enable(1);
315                 enable_channel3(1);
316         }
317         channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
318
319         return 0;
320 }
321
322 /* abort streaming and wait for last buffer */
323 static int vpif_stop_streaming(struct vb2_queue *vq)
324 {
325         struct vpif_fh *fh = vb2_get_drv_priv(vq);
326         struct channel_obj *ch = fh->channel;
327         struct common_obj *common;
328
329         if (!vb2_is_streaming(vq))
330                 return 0;
331
332         common = &ch->common[VPIF_VIDEO_INDEX];
333
334         /* release all active buffers */
335         while (!list_empty(&common->dma_queue)) {
336                 common->next_frm = list_entry(common->dma_queue.next,
337                                                 struct vpif_disp_buffer, list);
338                 list_del(&common->next_frm->list);
339                 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
340         }
341
342         return 0;
343 }
344
345 static struct vb2_ops video_qops = {
346         .queue_setup            = vpif_buffer_queue_setup,
347         .wait_prepare           = vpif_wait_prepare,
348         .wait_finish            = vpif_wait_finish,
349         .buf_init               = vpif_buffer_init,
350         .buf_prepare            = vpif_buffer_prepare,
351         .start_streaming        = vpif_start_streaming,
352         .stop_streaming         = vpif_stop_streaming,
353         .buf_cleanup            = vpif_buf_cleanup,
354         .buf_queue              = vpif_buffer_queue,
355 };
356
357 static void process_progressive_mode(struct common_obj *common)
358 {
359         unsigned long addr = 0;
360
361         /* Get the next buffer from buffer queue */
362         common->next_frm = list_entry(common->dma_queue.next,
363                                 struct vpif_disp_buffer, list);
364         /* Remove that buffer from the buffer queue */
365         list_del(&common->next_frm->list);
366         /* Mark status of the buffer as active */
367         common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
368
369         /* Set top and bottom field addrs in VPIF registers */
370         addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
371         common->set_addr(addr + common->ytop_off,
372                                  addr + common->ybtm_off,
373                                  addr + common->ctop_off,
374                                  addr + common->cbtm_off);
375 }
376
377 static void process_interlaced_mode(int fid, struct common_obj *common)
378 {
379         /* device field id and local field id are in sync */
380         /* If this is even field */
381         if (0 == fid) {
382                 if (common->cur_frm == common->next_frm)
383                         return;
384
385                 /* one frame is displayed If next frame is
386                  *  available, release cur_frm and move on */
387                 /* Copy frame display time */
388                 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
389                 /* Change status of the cur_frm */
390                 vb2_buffer_done(&common->cur_frm->vb,
391                                             VB2_BUF_STATE_DONE);
392                 /* Make cur_frm pointing to next_frm */
393                 common->cur_frm = common->next_frm;
394
395         } else if (1 == fid) {  /* odd field */
396                 if (list_empty(&common->dma_queue)
397                     || (common->cur_frm != common->next_frm)) {
398                         return;
399                 }
400                 /* one field is displayed configure the next
401                  * frame if it is available else hold on current
402                  * frame */
403                 /* Get next from the buffer queue */
404                 process_progressive_mode(common);
405
406         }
407 }
408
409 /*
410  * vpif_channel_isr: It changes status of the displayed buffer, takes next
411  * buffer from the queue and sets its address in VPIF registers
412  */
413 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
414 {
415         struct vpif_device *dev = &vpif_obj;
416         struct channel_obj *ch;
417         struct common_obj *common;
418         enum v4l2_field field;
419         int fid = -1, i;
420         int channel_id = 0;
421
422         channel_id = *(int *)(dev_id);
423         if (!vpif_intr_status(channel_id + 2))
424                 return IRQ_NONE;
425
426         ch = dev->dev[channel_id];
427         field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
428         for (i = 0; i < VPIF_NUMOBJECTS; i++) {
429                 common = &ch->common[i];
430                 /* If streaming is started in this channel */
431                 if (0 == common->started)
432                         continue;
433
434                 if (1 == ch->vpifparams.std_info.frm_fmt) {
435                         if (list_empty(&common->dma_queue))
436                                 continue;
437
438                         /* Progressive mode */
439                         if (!channel_first_int[i][channel_id]) {
440                                 /* Mark status of the cur_frm to
441                                  * done and unlock semaphore on it */
442                                 do_gettimeofday(&common->cur_frm->vb.
443                                                 v4l2_buf.timestamp);
444                                 vb2_buffer_done(&common->cur_frm->vb,
445                                             VB2_BUF_STATE_DONE);
446                                 /* Make cur_frm pointing to next_frm */
447                                 common->cur_frm = common->next_frm;
448                         }
449
450                         channel_first_int[i][channel_id] = 0;
451                         process_progressive_mode(common);
452                 } else {
453                         /* Interlaced mode */
454                         /* If it is first interrupt, ignore it */
455
456                         if (channel_first_int[i][channel_id]) {
457                                 channel_first_int[i][channel_id] = 0;
458                                 continue;
459                         }
460
461                         if (0 == i) {
462                                 ch->field_id ^= 1;
463                                 /* Get field id from VPIF registers */
464                                 fid = vpif_channel_getfid(ch->channel_id + 2);
465                                 /* If fid does not match with stored field id */
466                                 if (fid != ch->field_id) {
467                                         /* Make them in sync */
468                                         if (0 == fid)
469                                                 ch->field_id = fid;
470
471                                         return IRQ_HANDLED;
472                                 }
473                         }
474                         process_interlaced_mode(fid, common);
475                 }
476         }
477
478         return IRQ_HANDLED;
479 }
480
481 static int vpif_update_std_info(struct channel_obj *ch)
482 {
483         struct video_obj *vid_ch = &ch->video;
484         struct vpif_params *vpifparams = &ch->vpifparams;
485         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
486         const struct vpif_channel_config_params *config;
487
488         int i;
489
490         for (i = 0; i < vpif_ch_params_count; i++) {
491                 config = &ch_params[i];
492                 if (config->hd_sd == 0) {
493                         vpif_dbg(2, debug, "SD format\n");
494                         if (config->stdid & vid_ch->stdid) {
495                                 memcpy(std_info, config, sizeof(*config));
496                                 break;
497                         }
498                 } else {
499                         vpif_dbg(2, debug, "HD format\n");
500                         if (config->dv_preset == vid_ch->dv_preset) {
501                                 memcpy(std_info, config, sizeof(*config));
502                                 break;
503                         }
504                 }
505         }
506
507         if (i == vpif_ch_params_count) {
508                 vpif_dbg(1, debug, "Format not found\n");
509                 return -EINVAL;
510         }
511
512         return 0;
513 }
514
515 static int vpif_update_resolution(struct channel_obj *ch)
516 {
517         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
518         struct video_obj *vid_ch = &ch->video;
519         struct vpif_params *vpifparams = &ch->vpifparams;
520         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
521
522         if (!vid_ch->stdid && !vid_ch->dv_preset && !vid_ch->bt_timings.height)
523                 return -EINVAL;
524
525         if (vid_ch->stdid || vid_ch->dv_preset) {
526                 if (vpif_update_std_info(ch))
527                         return -EINVAL;
528         }
529
530         common->fmt.fmt.pix.width = std_info->width;
531         common->fmt.fmt.pix.height = std_info->height;
532         vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
533                         common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
534
535         /* Set height and width paramateres */
536         common->height = std_info->height;
537         common->width = std_info->width;
538
539         return 0;
540 }
541
542 /*
543  * vpif_calculate_offsets: This function calculates buffers offset for Y and C
544  * in the top and bottom field
545  */
546 static void vpif_calculate_offsets(struct channel_obj *ch)
547 {
548         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
549         struct vpif_params *vpifparams = &ch->vpifparams;
550         enum v4l2_field field = common->fmt.fmt.pix.field;
551         struct video_obj *vid_ch = &ch->video;
552         unsigned int hpitch, vpitch, sizeimage;
553
554         if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
555                 if (ch->vpifparams.std_info.frm_fmt)
556                         vid_ch->buf_field = V4L2_FIELD_NONE;
557                 else
558                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
559         } else {
560                 vid_ch->buf_field = common->fmt.fmt.pix.field;
561         }
562
563         sizeimage = common->fmt.fmt.pix.sizeimage;
564
565         hpitch = common->fmt.fmt.pix.bytesperline;
566         vpitch = sizeimage / (hpitch * 2);
567         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
568             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
569                 common->ytop_off = 0;
570                 common->ybtm_off = hpitch;
571                 common->ctop_off = sizeimage / 2;
572                 common->cbtm_off = sizeimage / 2 + hpitch;
573         } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
574                 common->ytop_off = 0;
575                 common->ybtm_off = sizeimage / 4;
576                 common->ctop_off = sizeimage / 2;
577                 common->cbtm_off = common->ctop_off + sizeimage / 4;
578         } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
579                 common->ybtm_off = 0;
580                 common->ytop_off = sizeimage / 4;
581                 common->cbtm_off = sizeimage / 2;
582                 common->ctop_off = common->cbtm_off + sizeimage / 4;
583         }
584
585         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
586             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
587                 vpifparams->video_params.storage_mode = 1;
588         } else {
589                 vpifparams->video_params.storage_mode = 0;
590         }
591
592         if (ch->vpifparams.std_info.frm_fmt == 1) {
593                 vpifparams->video_params.hpitch =
594                     common->fmt.fmt.pix.bytesperline;
595         } else {
596                 if ((field == V4L2_FIELD_ANY) ||
597                         (field == V4L2_FIELD_INTERLACED))
598                         vpifparams->video_params.hpitch =
599                             common->fmt.fmt.pix.bytesperline * 2;
600                 else
601                         vpifparams->video_params.hpitch =
602                             common->fmt.fmt.pix.bytesperline;
603         }
604
605         ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
606 }
607
608 static void vpif_config_format(struct channel_obj *ch)
609 {
610         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
611
612         common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
613         if (config_params.numbuffers[ch->channel_id] == 0)
614                 common->memory = V4L2_MEMORY_USERPTR;
615         else
616                 common->memory = V4L2_MEMORY_MMAP;
617
618         common->fmt.fmt.pix.sizeimage =
619                         config_params.channel_bufsize[ch->channel_id];
620         common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
621         common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
622 }
623
624 static int vpif_check_format(struct channel_obj *ch,
625                              struct v4l2_pix_format *pixfmt)
626 {
627         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
628         enum v4l2_field field = pixfmt->field;
629         u32 sizeimage, hpitch, vpitch;
630
631         if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
632                 goto invalid_fmt_exit;
633
634         if (!(VPIF_VALID_FIELD(field)))
635                 goto invalid_fmt_exit;
636
637         if (pixfmt->bytesperline <= 0)
638                 goto invalid_pitch_exit;
639
640         sizeimage = pixfmt->sizeimage;
641
642         if (vpif_update_resolution(ch))
643                 return -EINVAL;
644
645         hpitch = pixfmt->bytesperline;
646         vpitch = sizeimage / (hpitch * 2);
647
648         /* Check for valid value of pitch */
649         if ((hpitch < ch->vpifparams.std_info.width) ||
650             (vpitch < ch->vpifparams.std_info.height))
651                 goto invalid_pitch_exit;
652
653         /* Check for 8 byte alignment */
654         if (!ISALIGNED(hpitch)) {
655                 vpif_err("invalid pitch alignment\n");
656                 return -EINVAL;
657         }
658         pixfmt->width = common->fmt.fmt.pix.width;
659         pixfmt->height = common->fmt.fmt.pix.height;
660
661         return 0;
662
663 invalid_fmt_exit:
664         vpif_err("invalid field format\n");
665         return -EINVAL;
666
667 invalid_pitch_exit:
668         vpif_err("invalid pitch\n");
669         return -EINVAL;
670 }
671
672 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
673 {
674         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
675
676         if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
677                 common->set_addr = ch3_set_videobuf_addr;
678         } else {
679                 if (2 == muxmode)
680                         common->set_addr = ch2_set_videobuf_addr_yc_nmux;
681                 else
682                         common->set_addr = ch2_set_videobuf_addr;
683         }
684 }
685
686 /*
687  * vpif_mmap: It is used to map kernel space buffers into user spaces
688  */
689 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
690 {
691         struct vpif_fh *fh = filep->private_data;
692         struct channel_obj *ch = fh->channel;
693         struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
694
695         vpif_dbg(2, debug, "vpif_mmap\n");
696
697         return vb2_mmap(&common->buffer_queue, vma);
698 }
699
700 /*
701  * vpif_poll: It is used for select/poll system call
702  */
703 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
704 {
705         struct vpif_fh *fh = filep->private_data;
706         struct channel_obj *ch = fh->channel;
707         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
708
709         if (common->started)
710                 return vb2_poll(&common->buffer_queue, filep, wait);
711
712         return 0;
713 }
714
715 /*
716  * vpif_open: It creates object of file handle structure and stores it in
717  * private_data member of filepointer
718  */
719 static int vpif_open(struct file *filep)
720 {
721         struct video_device *vdev = video_devdata(filep);
722         struct channel_obj *ch = NULL;
723         struct vpif_fh *fh = NULL;
724
725         ch = video_get_drvdata(vdev);
726         /* Allocate memory for the file handle object */
727         fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
728         if (fh == NULL) {
729                 vpif_err("unable to allocate memory for file handle object\n");
730                 return -ENOMEM;
731         }
732
733         /* store pointer to fh in private_data member of filep */
734         filep->private_data = fh;
735         fh->channel = ch;
736         fh->initialized = 0;
737         if (!ch->initialized) {
738                 fh->initialized = 1;
739                 ch->initialized = 1;
740                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
741         }
742
743         /* Increment channel usrs counter */
744         atomic_inc(&ch->usrs);
745         /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
746         fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
747         /* Initialize priority of this instance to default priority */
748         fh->prio = V4L2_PRIORITY_UNSET;
749         v4l2_prio_open(&ch->prio, &fh->prio);
750
751         return 0;
752 }
753
754 /*
755  * vpif_release: This function deletes buffer queue, frees the buffers and
756  * the vpif file handle
757  */
758 static int vpif_release(struct file *filep)
759 {
760         struct vpif_fh *fh = filep->private_data;
761         struct channel_obj *ch = fh->channel;
762         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
763
764         /* if this instance is doing IO */
765         if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
766                 /* Reset io_usrs member of channel object */
767                 common->io_usrs = 0;
768                 /* Disable channel */
769                 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
770                         enable_channel2(0);
771                         channel2_intr_enable(0);
772                 }
773                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
774                     (2 == common->started)) {
775                         enable_channel3(0);
776                         channel3_intr_enable(0);
777                 }
778                 common->started = 0;
779
780                 /* Free buffers allocated */
781                 vb2_queue_release(&common->buffer_queue);
782                 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
783
784                 common->numbuffers =
785                     config_params.numbuffers[ch->channel_id];
786         }
787
788         /* Decrement channel usrs counter */
789         atomic_dec(&ch->usrs);
790         /* If this file handle has initialize encoder device, reset it */
791         if (fh->initialized)
792                 ch->initialized = 0;
793
794         /* Close the priority */
795         v4l2_prio_close(&ch->prio, fh->prio);
796         filep->private_data = NULL;
797         fh->initialized = 0;
798         kfree(fh);
799
800         return 0;
801 }
802
803 /* functions implementing ioctls */
804 /**
805  * vpif_querycap() - QUERYCAP handler
806  * @file: file ptr
807  * @priv: file handle
808  * @cap: ptr to v4l2_capability structure
809  */
810 static int vpif_querycap(struct file *file, void  *priv,
811                                 struct v4l2_capability *cap)
812 {
813         struct vpif_display_config *config = vpif_dev->platform_data;
814
815         cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
816         strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
817         strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
818         strlcpy(cap->card, config->card_name, sizeof(cap->card));
819
820         return 0;
821 }
822
823 static int vpif_enum_fmt_vid_out(struct file *file, void  *priv,
824                                         struct v4l2_fmtdesc *fmt)
825 {
826         if (fmt->index != 0) {
827                 vpif_err("Invalid format index\n");
828                 return -EINVAL;
829         }
830
831         /* Fill in the information about format */
832         fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
833         strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
834         fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
835
836         return 0;
837 }
838
839 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
840                                 struct v4l2_format *fmt)
841 {
842         struct vpif_fh *fh = priv;
843         struct channel_obj *ch = fh->channel;
844         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
845
846         /* Check the validity of the buffer type */
847         if (common->fmt.type != fmt->type)
848                 return -EINVAL;
849
850         if (vpif_update_resolution(ch))
851                 return -EINVAL;
852         *fmt = common->fmt;
853         return 0;
854 }
855
856 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
857                                 struct v4l2_format *fmt)
858 {
859         struct vpif_fh *fh = priv;
860         struct v4l2_pix_format *pixfmt;
861         struct channel_obj *ch = fh->channel;
862         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
863         int ret = 0;
864
865         if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
866             || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
867                 if (!fh->initialized) {
868                         vpif_dbg(1, debug, "Channel Busy\n");
869                         return -EBUSY;
870                 }
871
872                 /* Check for the priority */
873                 ret = v4l2_prio_check(&ch->prio, fh->prio);
874                 if (0 != ret)
875                         return ret;
876                 fh->initialized = 1;
877         }
878
879         if (common->started) {
880                 vpif_dbg(1, debug, "Streaming in progress\n");
881                 return -EBUSY;
882         }
883
884         pixfmt = &fmt->fmt.pix;
885         /* Check for valid field format */
886         ret = vpif_check_format(ch, pixfmt);
887         if (ret)
888                 return ret;
889
890         /* store the pix format in the channel object */
891         common->fmt.fmt.pix = *pixfmt;
892         /* store the format in the channel object */
893         common->fmt = *fmt;
894         return 0;
895 }
896
897 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
898                                 struct v4l2_format *fmt)
899 {
900         struct vpif_fh *fh = priv;
901         struct channel_obj *ch = fh->channel;
902         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
903         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
904         int ret = 0;
905
906         ret = vpif_check_format(ch, pixfmt);
907         if (ret) {
908                 *pixfmt = common->fmt.fmt.pix;
909                 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
910         }
911
912         return ret;
913 }
914
915 static int vpif_reqbufs(struct file *file, void *priv,
916                         struct v4l2_requestbuffers *reqbuf)
917 {
918         struct vpif_fh *fh = priv;
919         struct channel_obj *ch = fh->channel;
920         struct common_obj *common;
921         enum v4l2_field field;
922         struct vb2_queue *q;
923         u8 index = 0;
924
925         /* This file handle has not initialized the channel,
926            It is not allowed to do settings */
927         if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
928             || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
929                 if (!fh->initialized) {
930                         vpif_err("Channel Busy\n");
931                         return -EBUSY;
932                 }
933         }
934
935         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
936                 return -EINVAL;
937
938         index = VPIF_VIDEO_INDEX;
939
940         common = &ch->common[index];
941
942         if (common->fmt.type != reqbuf->type || !vpif_dev)
943                 return -EINVAL;
944         if (0 != common->io_usrs)
945                 return -EBUSY;
946
947         if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
948                 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
949                         field = V4L2_FIELD_INTERLACED;
950                 else
951                         field = common->fmt.fmt.pix.field;
952         } else {
953                 field = V4L2_VBI_INTERLACED;
954         }
955         /* Initialize videobuf2 queue as per the buffer type */
956         common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
957         if (!common->alloc_ctx) {
958                 vpif_err("Failed to get the context\n");
959                 return -EINVAL;
960         }
961         q = &common->buffer_queue;
962         q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
963         q->io_modes = VB2_MMAP | VB2_USERPTR;
964         q->drv_priv = fh;
965         q->ops = &video_qops;
966         q->mem_ops = &vb2_dma_contig_memops;
967         q->buf_struct_size = sizeof(struct vpif_disp_buffer);
968
969         vb2_queue_init(q);
970
971         /* Set io allowed member of file handle to TRUE */
972         fh->io_allowed[index] = 1;
973         /* Increment io usrs member of channel object to 1 */
974         common->io_usrs = 1;
975         /* Store type of memory requested in channel object */
976         common->memory = reqbuf->memory;
977         INIT_LIST_HEAD(&common->dma_queue);
978         /* Allocate buffers */
979         return vb2_reqbufs(&common->buffer_queue, reqbuf);
980 }
981
982 static int vpif_querybuf(struct file *file, void *priv,
983                                 struct v4l2_buffer *tbuf)
984 {
985         struct vpif_fh *fh = priv;
986         struct channel_obj *ch = fh->channel;
987         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
988
989         if (common->fmt.type != tbuf->type)
990                 return -EINVAL;
991
992         return vb2_querybuf(&common->buffer_queue, tbuf);
993 }
994
995 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
996 {
997         struct vpif_fh *fh = NULL;
998         struct channel_obj *ch = NULL;
999         struct common_obj *common = NULL;
1000
1001         if (!buf || !priv)
1002                 return -EINVAL;
1003
1004         fh = priv;
1005         ch = fh->channel;
1006         if (!ch)
1007                 return -EINVAL;
1008
1009         common = &(ch->common[VPIF_VIDEO_INDEX]);
1010         if (common->fmt.type != buf->type)
1011                 return -EINVAL;
1012
1013         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1014                 vpif_err("fh->io_allowed\n");
1015                 return -EACCES;
1016         }
1017
1018         return vb2_qbuf(&common->buffer_queue, buf);
1019 }
1020
1021 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1022 {
1023         struct vpif_fh *fh = priv;
1024         struct channel_obj *ch = fh->channel;
1025         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1026         int ret = 0;
1027
1028         if (!(*std_id & VPIF_V4L2_STD))
1029                 return -EINVAL;
1030
1031         if (common->started) {
1032                 vpif_err("streaming in progress\n");
1033                 return -EBUSY;
1034         }
1035
1036         /* Call encoder subdevice function to set the standard */
1037         ch->video.stdid = *std_id;
1038         ch->video.dv_preset = V4L2_DV_INVALID;
1039         memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1040
1041         /* Get the information about the standard */
1042         if (vpif_update_resolution(ch))
1043                 return -EINVAL;
1044
1045         if ((ch->vpifparams.std_info.width *
1046                 ch->vpifparams.std_info.height * 2) >
1047                 config_params.channel_bufsize[ch->channel_id]) {
1048                 vpif_err("invalid std for this size\n");
1049                 return -EINVAL;
1050         }
1051
1052         common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1053         /* Configure the default format information */
1054         vpif_config_format(ch);
1055
1056         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1057                                                 s_std_output, *std_id);
1058         if (ret < 0) {
1059                 vpif_err("Failed to set output standard\n");
1060                 return ret;
1061         }
1062
1063         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1064                                                         s_std, *std_id);
1065         if (ret < 0)
1066                 vpif_err("Failed to set standard for sub devices\n");
1067         return ret;
1068 }
1069
1070 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1071 {
1072         struct vpif_fh *fh = priv;
1073         struct channel_obj *ch = fh->channel;
1074
1075         *std = ch->video.stdid;
1076         return 0;
1077 }
1078
1079 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1080 {
1081         struct vpif_fh *fh = priv;
1082         struct channel_obj *ch = fh->channel;
1083         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1084
1085         return vb2_dqbuf(&common->buffer_queue, p,
1086                                         (file->f_flags & O_NONBLOCK));
1087 }
1088
1089 static int vpif_streamon(struct file *file, void *priv,
1090                                 enum v4l2_buf_type buftype)
1091 {
1092         struct vpif_fh *fh = priv;
1093         struct channel_obj *ch = fh->channel;
1094         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1095         struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1096         int ret = 0;
1097
1098         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1099                 vpif_err("buffer type not supported\n");
1100                 return -EINVAL;
1101         }
1102
1103         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1104                 vpif_err("fh->io_allowed\n");
1105                 return -EACCES;
1106         }
1107
1108         /* If Streaming is already started, return error */
1109         if (common->started) {
1110                 vpif_err("channel->started\n");
1111                 return -EBUSY;
1112         }
1113
1114         if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1115                 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1116                 ch->vpifparams.std_info.ycmux_mode == 0)
1117                 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1118                 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1119                 vpif_err("other channel is using\n");
1120                 return -EBUSY;
1121         }
1122
1123         ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1124         if (ret < 0)
1125                 return ret;
1126
1127         /* Call vb2_streamon to start streaming in videobuf2 */
1128         ret = vb2_streamon(&common->buffer_queue, buftype);
1129         if (ret < 0) {
1130                 vpif_err("vb2_streamon\n");
1131                 return ret;
1132         }
1133
1134         return ret;
1135 }
1136
1137 static int vpif_streamoff(struct file *file, void *priv,
1138                                 enum v4l2_buf_type buftype)
1139 {
1140         struct vpif_fh *fh = priv;
1141         struct channel_obj *ch = fh->channel;
1142         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1143
1144         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1145                 vpif_err("buffer type not supported\n");
1146                 return -EINVAL;
1147         }
1148
1149         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1150                 vpif_err("fh->io_allowed\n");
1151                 return -EACCES;
1152         }
1153
1154         if (!common->started) {
1155                 vpif_err("channel->started\n");
1156                 return -EINVAL;
1157         }
1158
1159         if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1160                 /* disable channel */
1161                 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1162                         enable_channel2(0);
1163                         channel2_intr_enable(0);
1164                 }
1165                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1166                                         (2 == common->started)) {
1167                         enable_channel3(0);
1168                         channel3_intr_enable(0);
1169                 }
1170         }
1171
1172         common->started = 0;
1173         return vb2_streamoff(&common->buffer_queue, buftype);
1174 }
1175
1176 static int vpif_cropcap(struct file *file, void *priv,
1177                         struct v4l2_cropcap *crop)
1178 {
1179         struct vpif_fh *fh = priv;
1180         struct channel_obj *ch = fh->channel;
1181         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1182         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1183                 return -EINVAL;
1184
1185         crop->bounds.left = crop->bounds.top = 0;
1186         crop->defrect.left = crop->defrect.top = 0;
1187         crop->defrect.height = crop->bounds.height = common->height;
1188         crop->defrect.width = crop->bounds.width = common->width;
1189
1190         return 0;
1191 }
1192
1193 static int vpif_enum_output(struct file *file, void *fh,
1194                                 struct v4l2_output *output)
1195 {
1196
1197         struct vpif_display_config *config = vpif_dev->platform_data;
1198
1199         if (output->index >= config->output_count) {
1200                 vpif_dbg(1, debug, "Invalid output index\n");
1201                 return -EINVAL;
1202         }
1203
1204         strcpy(output->name, config->output[output->index]);
1205         output->type = V4L2_OUTPUT_TYPE_ANALOG;
1206         output->std = VPIF_V4L2_STD;
1207
1208         return 0;
1209 }
1210
1211 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1212 {
1213         struct vpif_fh *fh = priv;
1214         struct channel_obj *ch = fh->channel;
1215         struct video_obj *vid_ch = &ch->video;
1216         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1217         int ret = 0;
1218
1219         if (common->started) {
1220                 vpif_err("Streaming in progress\n");
1221                 return -EBUSY;
1222         }
1223
1224         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1225                                                         s_routing, 0, i, 0);
1226
1227         if (ret < 0)
1228                 vpif_err("Failed to set output standard\n");
1229
1230         vid_ch->output_id = i;
1231         return ret;
1232 }
1233
1234 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1235 {
1236         struct vpif_fh *fh = priv;
1237         struct channel_obj *ch = fh->channel;
1238         struct video_obj *vid_ch = &ch->video;
1239
1240         *i = vid_ch->output_id;
1241
1242         return 0;
1243 }
1244
1245 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1246 {
1247         struct vpif_fh *fh = priv;
1248         struct channel_obj *ch = fh->channel;
1249
1250         *p = v4l2_prio_max(&ch->prio);
1251
1252         return 0;
1253 }
1254
1255 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1256 {
1257         struct vpif_fh *fh = priv;
1258         struct channel_obj *ch = fh->channel;
1259
1260         return v4l2_prio_change(&ch->prio, &fh->prio, p);
1261 }
1262
1263 /**
1264  * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1265  * @file: file ptr
1266  * @priv: file handle
1267  * @preset: input preset
1268  */
1269 static int vpif_enum_dv_presets(struct file *file, void *priv,
1270                 struct v4l2_dv_enum_preset *preset)
1271 {
1272         struct vpif_fh *fh = priv;
1273         struct channel_obj *ch = fh->channel;
1274         struct video_obj *vid_ch = &ch->video;
1275
1276         return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1277                         video, enum_dv_presets, preset);
1278 }
1279
1280 /**
1281  * vpif_s_dv_presets() - S_DV_PRESETS handler
1282  * @file: file ptr
1283  * @priv: file handle
1284  * @preset: input preset
1285  */
1286 static int vpif_s_dv_preset(struct file *file, void *priv,
1287                 struct v4l2_dv_preset *preset)
1288 {
1289         struct vpif_fh *fh = priv;
1290         struct channel_obj *ch = fh->channel;
1291         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1292         struct video_obj *vid_ch = &ch->video;
1293         int ret = 0;
1294
1295         if (common->started) {
1296                 vpif_dbg(1, debug, "streaming in progress\n");
1297                 return -EBUSY;
1298         }
1299
1300         ret = v4l2_prio_check(&ch->prio, fh->prio);
1301         if (ret != 0)
1302                 return ret;
1303
1304         fh->initialized = 1;
1305
1306         /* Call encoder subdevice function to set the standard */
1307         if (mutex_lock_interruptible(&common->lock))
1308                 return -ERESTARTSYS;
1309
1310         ch->video.dv_preset = preset->preset;
1311         ch->video.stdid = V4L2_STD_UNKNOWN;
1312         memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1313
1314         /* Get the information about the standard */
1315         if (vpif_update_resolution(ch)) {
1316                 ret = -EINVAL;
1317         } else {
1318                 /* Configure the default format information */
1319                 vpif_config_format(ch);
1320
1321                 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1322                                 video, s_dv_preset, preset);
1323         }
1324
1325         mutex_unlock(&common->lock);
1326
1327         return ret;
1328 }
1329 /**
1330  * vpif_g_dv_presets() - G_DV_PRESETS handler
1331  * @file: file ptr
1332  * @priv: file handle
1333  * @preset: input preset
1334  */
1335 static int vpif_g_dv_preset(struct file *file, void *priv,
1336                 struct v4l2_dv_preset *preset)
1337 {
1338         struct vpif_fh *fh = priv;
1339         struct channel_obj *ch = fh->channel;
1340
1341         preset->preset = ch->video.dv_preset;
1342
1343         return 0;
1344 }
1345 /**
1346  * vpif_s_dv_timings() - S_DV_TIMINGS handler
1347  * @file: file ptr
1348  * @priv: file handle
1349  * @timings: digital video timings
1350  */
1351 static int vpif_s_dv_timings(struct file *file, void *priv,
1352                 struct v4l2_dv_timings *timings)
1353 {
1354         struct vpif_fh *fh = priv;
1355         struct channel_obj *ch = fh->channel;
1356         struct vpif_params *vpifparams = &ch->vpifparams;
1357         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1358         struct video_obj *vid_ch = &ch->video;
1359         struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1360         int ret;
1361
1362         if (timings->type != V4L2_DV_BT_656_1120) {
1363                 vpif_dbg(2, debug, "Timing type not defined\n");
1364                 return -EINVAL;
1365         }
1366
1367         /* Configure subdevice timings, if any */
1368         ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1369                         video, s_dv_timings, timings);
1370         if (ret == -ENOIOCTLCMD) {
1371                 vpif_dbg(2, debug, "Custom DV timings not supported by "
1372                                 "subdevice\n");
1373                 return -EINVAL;
1374         }
1375         if (ret < 0) {
1376                 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1377                 return ret;
1378         }
1379
1380         if (!(timings->bt.width && timings->bt.height &&
1381                                 (timings->bt.hbackporch ||
1382                                  timings->bt.hfrontporch ||
1383                                  timings->bt.hsync) &&
1384                                 timings->bt.vfrontporch &&
1385                                 (timings->bt.vbackporch ||
1386                                  timings->bt.vsync))) {
1387                 vpif_dbg(2, debug, "Timings for width, height, "
1388                                 "horizontal back porch, horizontal sync, "
1389                                 "horizontal front porch, vertical back porch, "
1390                                 "vertical sync and vertical back porch "
1391                                 "must be defined\n");
1392                 return -EINVAL;
1393         }
1394
1395         *bt = timings->bt;
1396
1397         /* Configure video port timings */
1398
1399         std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1400                 bt->hsync - 8;
1401         std_info->sav2eav = bt->width;
1402
1403         std_info->l1 = 1;
1404         std_info->l3 = bt->vsync + bt->vbackporch + 1;
1405
1406         if (bt->interlaced) {
1407                 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1408                         std_info->vsize = bt->height * 2 +
1409                                 bt->vfrontporch + bt->vsync + bt->vbackporch +
1410                                 bt->il_vfrontporch + bt->il_vsync +
1411                                 bt->il_vbackporch;
1412                         std_info->l5 = std_info->vsize/2 -
1413                                 (bt->vfrontporch - 1);
1414                         std_info->l7 = std_info->vsize/2 + 1;
1415                         std_info->l9 = std_info->l7 + bt->il_vsync +
1416                                 bt->il_vbackporch + 1;
1417                         std_info->l11 = std_info->vsize -
1418                                 (bt->il_vfrontporch - 1);
1419                 } else {
1420                         vpif_dbg(2, debug, "Required timing values for "
1421                                         "interlaced BT format missing\n");
1422                         return -EINVAL;
1423                 }
1424         } else {
1425                 std_info->vsize = bt->height + bt->vfrontporch +
1426                         bt->vsync + bt->vbackporch;
1427                 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1428         }
1429         strncpy(std_info->name, "Custom timings BT656/1120",
1430                         VPIF_MAX_NAME);
1431         std_info->width = bt->width;
1432         std_info->height = bt->height;
1433         std_info->frm_fmt = bt->interlaced ? 0 : 1;
1434         std_info->ycmux_mode = 0;
1435         std_info->capture_format = 0;
1436         std_info->vbi_supported = 0;
1437         std_info->hd_sd = 1;
1438         std_info->stdid = 0;
1439         std_info->dv_preset = V4L2_DV_INVALID;
1440
1441         vid_ch->stdid = 0;
1442         vid_ch->dv_preset = V4L2_DV_INVALID;
1443
1444         return 0;
1445 }
1446
1447 /**
1448  * vpif_g_dv_timings() - G_DV_TIMINGS handler
1449  * @file: file ptr
1450  * @priv: file handle
1451  * @timings: digital video timings
1452  */
1453 static int vpif_g_dv_timings(struct file *file, void *priv,
1454                 struct v4l2_dv_timings *timings)
1455 {
1456         struct vpif_fh *fh = priv;
1457         struct channel_obj *ch = fh->channel;
1458         struct video_obj *vid_ch = &ch->video;
1459         struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1460
1461         timings->bt = *bt;
1462
1463         return 0;
1464 }
1465
1466 /*
1467  * vpif_g_chip_ident() - Identify the chip
1468  * @file: file ptr
1469  * @priv: file handle
1470  * @chip: chip identity
1471  *
1472  * Returns zero or -EINVAL if read operations fails.
1473  */
1474 static int vpif_g_chip_ident(struct file *file, void *priv,
1475                 struct v4l2_dbg_chip_ident *chip)
1476 {
1477         chip->ident = V4L2_IDENT_NONE;
1478         chip->revision = 0;
1479         if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1480                         chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1481                 vpif_dbg(2, debug, "match_type is invalid.\n");
1482                 return -EINVAL;
1483         }
1484
1485         return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1486                         g_chip_ident, chip);
1487 }
1488
1489 #ifdef CONFIG_VIDEO_ADV_DEBUG
1490 /*
1491  * vpif_dbg_g_register() - Read register
1492  * @file: file ptr
1493  * @priv: file handle
1494  * @reg: register to be read
1495  *
1496  * Debugging only
1497  * Returns zero or -EINVAL if read operations fails.
1498  */
1499 static int vpif_dbg_g_register(struct file *file, void *priv,
1500                 struct v4l2_dbg_register *reg){
1501         struct vpif_fh *fh = priv;
1502         struct channel_obj *ch = fh->channel;
1503         struct video_obj *vid_ch = &ch->video;
1504
1505         return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1506                         g_register, reg);
1507 }
1508
1509 /*
1510  * vpif_dbg_s_register() - Write to register
1511  * @file: file ptr
1512  * @priv: file handle
1513  * @reg: register to be modified
1514  *
1515  * Debugging only
1516  * Returns zero or -EINVAL if write operations fails.
1517  */
1518 static int vpif_dbg_s_register(struct file *file, void *priv,
1519                 struct v4l2_dbg_register *reg){
1520         struct vpif_fh *fh = priv;
1521         struct channel_obj *ch = fh->channel;
1522         struct video_obj *vid_ch = &ch->video;
1523
1524         return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1525                         s_register, reg);
1526 }
1527 #endif
1528
1529 /*
1530  * vpif_log_status() - Status information
1531  * @file: file ptr
1532  * @priv: file handle
1533  *
1534  * Returns zero.
1535  */
1536 static int vpif_log_status(struct file *filep, void *priv)
1537 {
1538         /* status for sub devices */
1539         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1540
1541         return 0;
1542 }
1543
1544 /* vpif display ioctl operations */
1545 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1546         .vidioc_querycap                = vpif_querycap,
1547         .vidioc_g_priority              = vpif_g_priority,
1548         .vidioc_s_priority              = vpif_s_priority,
1549         .vidioc_enum_fmt_vid_out        = vpif_enum_fmt_vid_out,
1550         .vidioc_g_fmt_vid_out           = vpif_g_fmt_vid_out,
1551         .vidioc_s_fmt_vid_out           = vpif_s_fmt_vid_out,
1552         .vidioc_try_fmt_vid_out         = vpif_try_fmt_vid_out,
1553         .vidioc_reqbufs                 = vpif_reqbufs,
1554         .vidioc_querybuf                = vpif_querybuf,
1555         .vidioc_qbuf                    = vpif_qbuf,
1556         .vidioc_dqbuf                   = vpif_dqbuf,
1557         .vidioc_streamon                = vpif_streamon,
1558         .vidioc_streamoff               = vpif_streamoff,
1559         .vidioc_s_std                   = vpif_s_std,
1560         .vidioc_g_std                   = vpif_g_std,
1561         .vidioc_enum_output             = vpif_enum_output,
1562         .vidioc_s_output                = vpif_s_output,
1563         .vidioc_g_output                = vpif_g_output,
1564         .vidioc_cropcap                 = vpif_cropcap,
1565         .vidioc_enum_dv_presets         = vpif_enum_dv_presets,
1566         .vidioc_s_dv_preset             = vpif_s_dv_preset,
1567         .vidioc_g_dv_preset             = vpif_g_dv_preset,
1568         .vidioc_s_dv_timings            = vpif_s_dv_timings,
1569         .vidioc_g_dv_timings            = vpif_g_dv_timings,
1570         .vidioc_g_chip_ident            = vpif_g_chip_ident,
1571 #ifdef CONFIG_VIDEO_ADV_DEBUG
1572         .vidioc_g_register              = vpif_dbg_g_register,
1573         .vidioc_s_register              = vpif_dbg_s_register,
1574 #endif
1575         .vidioc_log_status              = vpif_log_status,
1576 };
1577
1578 static const struct v4l2_file_operations vpif_fops = {
1579         .owner          = THIS_MODULE,
1580         .open           = vpif_open,
1581         .release        = vpif_release,
1582         .unlocked_ioctl = video_ioctl2,
1583         .mmap           = vpif_mmap,
1584         .poll           = vpif_poll
1585 };
1586
1587 static struct video_device vpif_video_template = {
1588         .name           = "vpif",
1589         .fops           = &vpif_fops,
1590         .ioctl_ops      = &vpif_ioctl_ops,
1591         .tvnorms        = VPIF_V4L2_STD,
1592         .current_norm   = V4L2_STD_625_50,
1593
1594 };
1595
1596 /*Configure the channels, buffer sizei, request irq */
1597 static int initialize_vpif(void)
1598 {
1599         int free_channel_objects_index;
1600         int free_buffer_channel_index;
1601         int free_buffer_index;
1602         int err = 0, i, j;
1603
1604         /* Default number of buffers should be 3 */
1605         if ((ch2_numbuffers > 0) &&
1606             (ch2_numbuffers < config_params.min_numbuffers))
1607                 ch2_numbuffers = config_params.min_numbuffers;
1608         if ((ch3_numbuffers > 0) &&
1609             (ch3_numbuffers < config_params.min_numbuffers))
1610                 ch3_numbuffers = config_params.min_numbuffers;
1611
1612         /* Set buffer size to min buffers size if invalid buffer size is
1613          * given */
1614         if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1615                 ch2_bufsize =
1616                     config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1617         if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1618                 ch3_bufsize =
1619                     config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1620
1621         config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1622
1623         if (ch2_numbuffers) {
1624                 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1625                                                         ch2_bufsize;
1626         }
1627         config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1628
1629         if (ch3_numbuffers) {
1630                 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1631                                                         ch3_bufsize;
1632         }
1633
1634         /* Allocate memory for six channel objects */
1635         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1636                 vpif_obj.dev[i] =
1637                     kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1638                 /* If memory allocation fails, return error */
1639                 if (!vpif_obj.dev[i]) {
1640                         free_channel_objects_index = i;
1641                         err = -ENOMEM;
1642                         goto vpif_init_free_channel_objects;
1643                 }
1644         }
1645
1646         free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1647         free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1648         free_buffer_index = config_params.numbuffers[i - 1];
1649
1650         return 0;
1651
1652 vpif_init_free_channel_objects:
1653         for (j = 0; j < free_channel_objects_index; j++)
1654                 kfree(vpif_obj.dev[j]);
1655         return err;
1656 }
1657
1658 /*
1659  * vpif_probe: This function creates device entries by register itself to the
1660  * V4L2 driver and initializes fields of each channel objects
1661  */
1662 static __init int vpif_probe(struct platform_device *pdev)
1663 {
1664         struct vpif_subdev_info *subdevdata;
1665         struct vpif_display_config *config;
1666         int i, j = 0, k, q, m, err = 0;
1667         struct i2c_adapter *i2c_adap;
1668         struct common_obj *common;
1669         struct channel_obj *ch;
1670         struct video_device *vfd;
1671         struct resource *res;
1672         int subdev_count;
1673         size_t size;
1674
1675         vpif_dev = &pdev->dev;
1676         err = initialize_vpif();
1677
1678         if (err) {
1679                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1680                 return err;
1681         }
1682
1683         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1684         if (err) {
1685                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1686                 return err;
1687         }
1688
1689         k = 0;
1690         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1691                 for (i = res->start; i <= res->end; i++) {
1692                         if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
1693                                         "VPIF_Display",
1694                                 (void *)(&vpif_obj.dev[k]->channel_id))) {
1695                                 err = -EBUSY;
1696                                 goto vpif_int_err;
1697                         }
1698                 }
1699                 k++;
1700         }
1701
1702         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1703
1704                 /* Get the pointer to the channel object */
1705                 ch = vpif_obj.dev[i];
1706
1707                 /* Allocate memory for video device */
1708                 vfd = video_device_alloc();
1709                 if (vfd == NULL) {
1710                         for (j = 0; j < i; j++) {
1711                                 ch = vpif_obj.dev[j];
1712                                 video_device_release(ch->video_dev);
1713                         }
1714                         err = -ENOMEM;
1715                         goto vpif_int_err;
1716                 }
1717
1718                 /* Initialize field of video device */
1719                 *vfd = vpif_video_template;
1720                 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1721                 vfd->release = video_device_release;
1722                 snprintf(vfd->name, sizeof(vfd->name),
1723                          "VPIF_Display_DRIVER_V%s",
1724                          VPIF_DISPLAY_VERSION);
1725
1726                 /* Set video_dev to the video device */
1727                 ch->video_dev = vfd;
1728         }
1729
1730         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1731         if (res) {
1732                 size = resource_size(res);
1733                 /* The resources are divided into two equal memory and when
1734                  * we have HD output we can add them together
1735                  */
1736                 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1737                         ch = vpif_obj.dev[j];
1738                         ch->channel_id = j;
1739
1740                         /* only enabled if second resource exists */
1741                         config_params.video_limit[ch->channel_id] = 0;
1742                         if (size)
1743                                 config_params.video_limit[ch->channel_id] =
1744                                                                         size/2;
1745                 }
1746         }
1747
1748         for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1749                 ch = vpif_obj.dev[j];
1750                 /* Initialize field of the channel objects */
1751                 atomic_set(&ch->usrs, 0);
1752                 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1753                         ch->common[k].numbuffers = 0;
1754                         common = &ch->common[k];
1755                         common->io_usrs = 0;
1756                         common->started = 0;
1757                         spin_lock_init(&common->irqlock);
1758                         mutex_init(&common->lock);
1759                         common->numbuffers = 0;
1760                         common->set_addr = NULL;
1761                         common->ytop_off = common->ybtm_off = 0;
1762                         common->ctop_off = common->cbtm_off = 0;
1763                         common->cur_frm = common->next_frm = NULL;
1764                         memset(&common->fmt, 0, sizeof(common->fmt));
1765                         common->numbuffers = config_params.numbuffers[k];
1766
1767                 }
1768                 ch->initialized = 0;
1769                 ch->channel_id = j;
1770                 if (j < 2)
1771                         ch->common[VPIF_VIDEO_INDEX].numbuffers =
1772                             config_params.numbuffers[ch->channel_id];
1773                 else
1774                         ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1775
1776                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1777
1778                 /* Initialize prio member of channel object */
1779                 v4l2_prio_init(&ch->prio);
1780                 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1781                                                 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1782                 /* Locking in file operations other than ioctl should be done
1783                    by the driver, not the V4L2 core.
1784                    This driver needs auditing so that this flag can be removed. */
1785                 set_bit(V4L2_FL_LOCK_ALL_FOPS, &ch->video_dev->flags);
1786                 ch->video_dev->lock = &common->lock;
1787
1788                 /* register video device */
1789                 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1790                                 (int)ch, (int)&ch->video_dev);
1791
1792                 err = video_register_device(ch->video_dev,
1793                                           VFL_TYPE_GRABBER, (j ? 3 : 2));
1794                 if (err < 0)
1795                         goto probe_out;
1796
1797                 video_set_drvdata(ch->video_dev, ch);
1798         }
1799
1800         i2c_adap = i2c_get_adapter(1);
1801         config = pdev->dev.platform_data;
1802         subdev_count = config->subdev_count;
1803         subdevdata = config->subdevinfo;
1804         vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1805                                                                 GFP_KERNEL);
1806         if (vpif_obj.sd == NULL) {
1807                 vpif_err("unable to allocate memory for subdevice pointers\n");
1808                 err = -ENOMEM;
1809                 goto probe_out;
1810         }
1811
1812         for (i = 0; i < subdev_count; i++) {
1813                 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1814                                                 i2c_adap,
1815                                                 &subdevdata[i].board_info,
1816                                                 NULL);
1817                 if (!vpif_obj.sd[i]) {
1818                         vpif_err("Error registering v4l2 subdevice\n");
1819                         goto probe_subdev_out;
1820                 }
1821
1822                 if (vpif_obj.sd[i])
1823                         vpif_obj.sd[i]->grp_id = 1 << i;
1824         }
1825
1826         v4l2_info(&vpif_obj.v4l2_dev,
1827                         " VPIF display driver initialized\n");
1828         return 0;
1829
1830 probe_subdev_out:
1831         kfree(vpif_obj.sd);
1832 probe_out:
1833         for (k = 0; k < j; k++) {
1834                 ch = vpif_obj.dev[k];
1835                 video_unregister_device(ch->video_dev);
1836                 video_device_release(ch->video_dev);
1837                 ch->video_dev = NULL;
1838         }
1839 vpif_int_err:
1840         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1841         vpif_err("VPIF IRQ request failed\n");
1842         for (q = k; k >= 0; k--) {
1843                 for (m = i; m >= res->start; m--)
1844                         free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1845                 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1846                 m = res->end;
1847         }
1848
1849         return err;
1850 }
1851
1852 /*
1853  * vpif_remove: It un-register channels from V4L2 driver
1854  */
1855 static int vpif_remove(struct platform_device *device)
1856 {
1857         struct channel_obj *ch;
1858         int i;
1859
1860         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1861
1862         /* un-register device */
1863         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1864                 /* Get the pointer to the channel object */
1865                 ch = vpif_obj.dev[i];
1866                 /* Unregister video device */
1867                 video_unregister_device(ch->video_dev);
1868
1869                 ch->video_dev = NULL;
1870         }
1871
1872         return 0;
1873 }
1874
1875 static __refdata struct platform_driver vpif_driver = {
1876         .driver = {
1877                         .name   = "vpif_display",
1878                         .owner  = THIS_MODULE,
1879         },
1880         .probe  = vpif_probe,
1881         .remove = vpif_remove,
1882 };
1883
1884 static __init int vpif_init(void)
1885 {
1886         return platform_driver_register(&vpif_driver);
1887 }
1888
1889 /*
1890  * vpif_cleanup: This function un-registers device and driver to the kernel,
1891  * frees requested irq handler and de-allocates memory allocated for channel
1892  * objects.
1893  */
1894 static void vpif_cleanup(void)
1895 {
1896         struct platform_device *pdev;
1897         struct resource *res;
1898         int irq_num;
1899         int i = 0;
1900
1901         pdev = container_of(vpif_dev, struct platform_device, dev);
1902
1903         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1904                 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1905                         free_irq(irq_num,
1906                                  (void *)(&vpif_obj.dev[i]->channel_id));
1907                 i++;
1908         }
1909
1910         platform_driver_unregister(&vpif_driver);
1911         kfree(vpif_obj.sd);
1912         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1913                 kfree(vpif_obj.dev[i]);
1914 }
1915
1916 module_init(vpif_init);
1917 module_exit(vpif_cleanup);