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