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