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