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