]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/media/platform/davinci/vpif_display.c
iommu/exynos: Fix checkpatch warning
[karo-tx-linux.git] / drivers / media / platform / davinci / vpif_display.c
1 /*
2  * vpif-display - VPIF display driver
3  * Display driver for TI DaVinci VPIF
4  *
5  * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation version 2.
10  *
11  * This program is distributed .as is. WITHOUT ANY WARRANTY of any
12  * kind, whether express or implied; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/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         q->min_buffers_needed = 1;
982
983         ret = vb2_queue_init(q);
984         if (ret) {
985                 vpif_err("vpif_display: vb2_queue_init() failed\n");
986                 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
987                 return ret;
988         }
989         /* Set io allowed member of file handle to TRUE */
990         fh->io_allowed[index] = 1;
991         /* Increment io usrs member of channel object to 1 */
992         common->io_usrs = 1;
993         /* Store type of memory requested in channel object */
994         common->memory = reqbuf->memory;
995         INIT_LIST_HEAD(&common->dma_queue);
996         /* Allocate buffers */
997         return vb2_reqbufs(&common->buffer_queue, reqbuf);
998 }
999
1000 static int vpif_querybuf(struct file *file, void *priv,
1001                                 struct v4l2_buffer *tbuf)
1002 {
1003         struct vpif_fh *fh = priv;
1004         struct channel_obj *ch = fh->channel;
1005         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1006
1007         if (common->fmt.type != tbuf->type)
1008                 return -EINVAL;
1009
1010         return vb2_querybuf(&common->buffer_queue, tbuf);
1011 }
1012
1013 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1014 {
1015         struct vpif_fh *fh = NULL;
1016         struct channel_obj *ch = NULL;
1017         struct common_obj *common = NULL;
1018
1019         if (!buf || !priv)
1020                 return -EINVAL;
1021
1022         fh = priv;
1023         ch = fh->channel;
1024         if (!ch)
1025                 return -EINVAL;
1026
1027         common = &(ch->common[VPIF_VIDEO_INDEX]);
1028         if (common->fmt.type != buf->type)
1029                 return -EINVAL;
1030
1031         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1032                 vpif_err("fh->io_allowed\n");
1033                 return -EACCES;
1034         }
1035
1036         return vb2_qbuf(&common->buffer_queue, buf);
1037 }
1038
1039 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1040 {
1041         struct vpif_fh *fh = priv;
1042         struct channel_obj *ch = fh->channel;
1043         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1044         int ret = 0;
1045
1046         if (!(std_id & VPIF_V4L2_STD))
1047                 return -EINVAL;
1048
1049         if (common->started) {
1050                 vpif_err("streaming in progress\n");
1051                 return -EBUSY;
1052         }
1053
1054         /* Call encoder subdevice function to set the standard */
1055         ch->video.stdid = std_id;
1056         memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1057         /* Get the information about the standard */
1058         if (vpif_update_resolution(ch))
1059                 return -EINVAL;
1060
1061         if ((ch->vpifparams.std_info.width *
1062                 ch->vpifparams.std_info.height * 2) >
1063                 config_params.channel_bufsize[ch->channel_id]) {
1064                 vpif_err("invalid std for this size\n");
1065                 return -EINVAL;
1066         }
1067
1068         common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1069         /* Configure the default format information */
1070         vpif_config_format(ch);
1071
1072         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1073                                                 s_std_output, std_id);
1074         if (ret < 0) {
1075                 vpif_err("Failed to set output standard\n");
1076                 return ret;
1077         }
1078
1079         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1080                                                         s_std, std_id);
1081         if (ret < 0)
1082                 vpif_err("Failed to set standard for sub devices\n");
1083         return ret;
1084 }
1085
1086 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1087 {
1088         struct vpif_fh *fh = priv;
1089         struct channel_obj *ch = fh->channel;
1090
1091         *std = ch->video.stdid;
1092         return 0;
1093 }
1094
1095 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1096 {
1097         struct vpif_fh *fh = priv;
1098         struct channel_obj *ch = fh->channel;
1099         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1100
1101         return vb2_dqbuf(&common->buffer_queue, p,
1102                                         (file->f_flags & O_NONBLOCK));
1103 }
1104
1105 static int vpif_streamon(struct file *file, void *priv,
1106                                 enum v4l2_buf_type buftype)
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         struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1112         int ret = 0;
1113
1114         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1115                 vpif_err("buffer type not supported\n");
1116                 return -EINVAL;
1117         }
1118
1119         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1120                 vpif_err("fh->io_allowed\n");
1121                 return -EACCES;
1122         }
1123
1124         /* If Streaming is already started, return error */
1125         if (common->started) {
1126                 vpif_err("channel->started\n");
1127                 return -EBUSY;
1128         }
1129
1130         if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1131                 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1132                 ch->vpifparams.std_info.ycmux_mode == 0)
1133                 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1134                 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1135                 vpif_err("other channel is using\n");
1136                 return -EBUSY;
1137         }
1138
1139         ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1140         if (ret < 0)
1141                 return ret;
1142
1143         /* Call vb2_streamon to start streaming in videobuf2 */
1144         ret = vb2_streamon(&common->buffer_queue, buftype);
1145         if (ret < 0) {
1146                 vpif_err("vb2_streamon\n");
1147                 return ret;
1148         }
1149
1150         return ret;
1151 }
1152
1153 static int vpif_streamoff(struct file *file, void *priv,
1154                                 enum v4l2_buf_type buftype)
1155 {
1156         struct vpif_fh *fh = priv;
1157         struct channel_obj *ch = fh->channel;
1158         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1159         struct vpif_display_config *vpif_config_data =
1160                                         vpif_dev->platform_data;
1161
1162         if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1163                 vpif_err("buffer type not supported\n");
1164                 return -EINVAL;
1165         }
1166
1167         if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1168                 vpif_err("fh->io_allowed\n");
1169                 return -EACCES;
1170         }
1171
1172         if (!common->started) {
1173                 vpif_err("channel->started\n");
1174                 return -EINVAL;
1175         }
1176
1177         if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1178                 /* disable channel */
1179                 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1180                         if (vpif_config_data->
1181                                 chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
1182                                 channel2_clipping_enable(0);
1183                         enable_channel2(0);
1184                         channel2_intr_enable(0);
1185                 }
1186                 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1187                                         (2 == common->started)) {
1188                         if (vpif_config_data->
1189                                 chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
1190                                 channel3_clipping_enable(0);
1191                         enable_channel3(0);
1192                         channel3_intr_enable(0);
1193                 }
1194         }
1195
1196         common->started = 0;
1197         return vb2_streamoff(&common->buffer_queue, buftype);
1198 }
1199
1200 static int vpif_cropcap(struct file *file, void *priv,
1201                         struct v4l2_cropcap *crop)
1202 {
1203         struct vpif_fh *fh = priv;
1204         struct channel_obj *ch = fh->channel;
1205         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1206         if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1207                 return -EINVAL;
1208
1209         crop->bounds.left = crop->bounds.top = 0;
1210         crop->defrect.left = crop->defrect.top = 0;
1211         crop->defrect.height = crop->bounds.height = common->height;
1212         crop->defrect.width = crop->bounds.width = common->width;
1213
1214         return 0;
1215 }
1216
1217 static int vpif_enum_output(struct file *file, void *fh,
1218                                 struct v4l2_output *output)
1219 {
1220
1221         struct vpif_display_config *config = vpif_dev->platform_data;
1222         struct vpif_display_chan_config *chan_cfg;
1223         struct vpif_fh *vpif_handler = fh;
1224         struct channel_obj *ch = vpif_handler->channel;
1225
1226         chan_cfg = &config->chan_config[ch->channel_id];
1227         if (output->index >= chan_cfg->output_count) {
1228                 vpif_dbg(1, debug, "Invalid output index\n");
1229                 return -EINVAL;
1230         }
1231
1232         *output = chan_cfg->outputs[output->index].output;
1233         return 0;
1234 }
1235
1236 /**
1237  * vpif_output_to_subdev() - Maps output to sub device
1238  * @vpif_cfg - global config ptr
1239  * @chan_cfg - channel config ptr
1240  * @index - Given output index from application
1241  *
1242  * lookup the sub device information for a given output index.
1243  * we report all the output to application. output table also
1244  * has sub device name for the each output
1245  */
1246 static int
1247 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
1248                       struct vpif_display_chan_config *chan_cfg, int index)
1249 {
1250         struct vpif_subdev_info *subdev_info;
1251         const char *subdev_name;
1252         int i;
1253
1254         vpif_dbg(2, debug, "vpif_output_to_subdev\n");
1255
1256         if (chan_cfg->outputs == NULL)
1257                 return -1;
1258
1259         subdev_name = chan_cfg->outputs[index].subdev_name;
1260         if (subdev_name == NULL)
1261                 return -1;
1262
1263         /* loop through the sub device list to get the sub device info */
1264         for (i = 0; i < vpif_cfg->subdev_count; i++) {
1265                 subdev_info = &vpif_cfg->subdevinfo[i];
1266                 if (!strcmp(subdev_info->name, subdev_name))
1267                         return i;
1268         }
1269         return -1;
1270 }
1271
1272 /**
1273  * vpif_set_output() - Select an output
1274  * @vpif_cfg - global config ptr
1275  * @ch - channel
1276  * @index - Given output index from application
1277  *
1278  * Select the given output.
1279  */
1280 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
1281                       struct channel_obj *ch, int index)
1282 {
1283         struct vpif_display_chan_config *chan_cfg =
1284                 &vpif_cfg->chan_config[ch->channel_id];
1285         struct vpif_subdev_info *subdev_info = NULL;
1286         struct v4l2_subdev *sd = NULL;
1287         u32 input = 0, output = 0;
1288         int sd_index;
1289         int ret;
1290
1291         sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
1292         if (sd_index >= 0) {
1293                 sd = vpif_obj.sd[sd_index];
1294                 subdev_info = &vpif_cfg->subdevinfo[sd_index];
1295         }
1296
1297         if (sd) {
1298                 input = chan_cfg->outputs[index].input_route;
1299                 output = chan_cfg->outputs[index].output_route;
1300                 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1301                 if (ret < 0 && ret != -ENOIOCTLCMD) {
1302                         vpif_err("Failed to set output\n");
1303                         return ret;
1304                 }
1305
1306         }
1307         ch->output_idx = index;
1308         ch->sd = sd;
1309         if (chan_cfg->outputs != NULL)
1310                 /* update tvnorms from the sub device output info */
1311                 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
1312         return 0;
1313 }
1314
1315 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1316 {
1317         struct vpif_display_config *config = vpif_dev->platform_data;
1318         struct vpif_display_chan_config *chan_cfg;
1319         struct vpif_fh *fh = priv;
1320         struct channel_obj *ch = fh->channel;
1321         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1322
1323         chan_cfg = &config->chan_config[ch->channel_id];
1324
1325         if (i >= chan_cfg->output_count)
1326                 return -EINVAL;
1327
1328         if (common->started) {
1329                 vpif_err("Streaming in progress\n");
1330                 return -EBUSY;
1331         }
1332
1333         return vpif_set_output(config, ch, i);
1334 }
1335
1336 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1337 {
1338         struct vpif_fh *fh = priv;
1339         struct channel_obj *ch = fh->channel;
1340
1341         *i = ch->output_idx;
1342
1343         return 0;
1344 }
1345
1346 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1347 {
1348         struct vpif_fh *fh = priv;
1349         struct channel_obj *ch = fh->channel;
1350
1351         *p = v4l2_prio_max(&ch->prio);
1352
1353         return 0;
1354 }
1355
1356 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1357 {
1358         struct vpif_fh *fh = priv;
1359         struct channel_obj *ch = fh->channel;
1360
1361         return v4l2_prio_change(&ch->prio, &fh->prio, p);
1362 }
1363
1364 /**
1365  * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1366  * @file: file ptr
1367  * @priv: file handle
1368  * @timings: input timings
1369  */
1370 static int
1371 vpif_enum_dv_timings(struct file *file, void *priv,
1372                      struct v4l2_enum_dv_timings *timings)
1373 {
1374         struct vpif_fh *fh = priv;
1375         struct channel_obj *ch = fh->channel;
1376         int ret;
1377
1378         ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1379         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1380                 return -EINVAL;
1381         return ret;
1382 }
1383
1384 /**
1385  * vpif_s_dv_timings() - S_DV_TIMINGS handler
1386  * @file: file ptr
1387  * @priv: file handle
1388  * @timings: digital video timings
1389  */
1390 static int vpif_s_dv_timings(struct file *file, void *priv,
1391                 struct v4l2_dv_timings *timings)
1392 {
1393         struct vpif_fh *fh = priv;
1394         struct channel_obj *ch = fh->channel;
1395         struct vpif_params *vpifparams = &ch->vpifparams;
1396         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1397         struct video_obj *vid_ch = &ch->video;
1398         struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1399         int ret;
1400
1401         if (timings->type != V4L2_DV_BT_656_1120) {
1402                 vpif_dbg(2, debug, "Timing type not defined\n");
1403                 return -EINVAL;
1404         }
1405
1406         /* Configure subdevice timings, if any */
1407         ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1408         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1409                 ret = 0;
1410         if (ret < 0) {
1411                 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1412                 return ret;
1413         }
1414
1415         if (!(timings->bt.width && timings->bt.height &&
1416                                 (timings->bt.hbackporch ||
1417                                  timings->bt.hfrontporch ||
1418                                  timings->bt.hsync) &&
1419                                 timings->bt.vfrontporch &&
1420                                 (timings->bt.vbackporch ||
1421                                  timings->bt.vsync))) {
1422                 vpif_dbg(2, debug, "Timings for width, height, "
1423                                 "horizontal back porch, horizontal sync, "
1424                                 "horizontal front porch, vertical back porch, "
1425                                 "vertical sync and vertical back porch "
1426                                 "must be defined\n");
1427                 return -EINVAL;
1428         }
1429
1430         vid_ch->dv_timings = *timings;
1431
1432         /* Configure video port timings */
1433
1434         std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1435         std_info->sav2eav = bt->width;
1436
1437         std_info->l1 = 1;
1438         std_info->l3 = bt->vsync + bt->vbackporch + 1;
1439
1440         std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1441         if (bt->interlaced) {
1442                 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1443                         std_info->l5 = std_info->vsize/2 -
1444                                 (bt->vfrontporch - 1);
1445                         std_info->l7 = std_info->vsize/2 + 1;
1446                         std_info->l9 = std_info->l7 + bt->il_vsync +
1447                                 bt->il_vbackporch + 1;
1448                         std_info->l11 = std_info->vsize -
1449                                 (bt->il_vfrontporch - 1);
1450                 } else {
1451                         vpif_dbg(2, debug, "Required timing values for "
1452                                         "interlaced BT format missing\n");
1453                         return -EINVAL;
1454                 }
1455         } else {
1456                 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1457         }
1458         strncpy(std_info->name, "Custom timings BT656/1120",
1459                         VPIF_MAX_NAME);
1460         std_info->width = bt->width;
1461         std_info->height = bt->height;
1462         std_info->frm_fmt = bt->interlaced ? 0 : 1;
1463         std_info->ycmux_mode = 0;
1464         std_info->capture_format = 0;
1465         std_info->vbi_supported = 0;
1466         std_info->hd_sd = 1;
1467         std_info->stdid = 0;
1468         vid_ch->stdid = 0;
1469
1470         return 0;
1471 }
1472
1473 /**
1474  * vpif_g_dv_timings() - G_DV_TIMINGS handler
1475  * @file: file ptr
1476  * @priv: file handle
1477  * @timings: digital video timings
1478  */
1479 static int vpif_g_dv_timings(struct file *file, void *priv,
1480                 struct v4l2_dv_timings *timings)
1481 {
1482         struct vpif_fh *fh = priv;
1483         struct channel_obj *ch = fh->channel;
1484         struct video_obj *vid_ch = &ch->video;
1485
1486         *timings = vid_ch->dv_timings;
1487
1488         return 0;
1489 }
1490
1491 /*
1492  * vpif_log_status() - Status information
1493  * @file: file ptr
1494  * @priv: file handle
1495  *
1496  * Returns zero.
1497  */
1498 static int vpif_log_status(struct file *filep, void *priv)
1499 {
1500         /* status for sub devices */
1501         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1502
1503         return 0;
1504 }
1505
1506 /* vpif display ioctl operations */
1507 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1508         .vidioc_querycap                = vpif_querycap,
1509         .vidioc_g_priority              = vpif_g_priority,
1510         .vidioc_s_priority              = vpif_s_priority,
1511         .vidioc_enum_fmt_vid_out        = vpif_enum_fmt_vid_out,
1512         .vidioc_g_fmt_vid_out           = vpif_g_fmt_vid_out,
1513         .vidioc_s_fmt_vid_out           = vpif_s_fmt_vid_out,
1514         .vidioc_try_fmt_vid_out         = vpif_try_fmt_vid_out,
1515         .vidioc_reqbufs                 = vpif_reqbufs,
1516         .vidioc_querybuf                = vpif_querybuf,
1517         .vidioc_qbuf                    = vpif_qbuf,
1518         .vidioc_dqbuf                   = vpif_dqbuf,
1519         .vidioc_streamon                = vpif_streamon,
1520         .vidioc_streamoff               = vpif_streamoff,
1521         .vidioc_s_std                   = vpif_s_std,
1522         .vidioc_g_std                   = vpif_g_std,
1523         .vidioc_enum_output             = vpif_enum_output,
1524         .vidioc_s_output                = vpif_s_output,
1525         .vidioc_g_output                = vpif_g_output,
1526         .vidioc_cropcap                 = vpif_cropcap,
1527         .vidioc_enum_dv_timings         = vpif_enum_dv_timings,
1528         .vidioc_s_dv_timings            = vpif_s_dv_timings,
1529         .vidioc_g_dv_timings            = vpif_g_dv_timings,
1530         .vidioc_log_status              = vpif_log_status,
1531 };
1532
1533 static const struct v4l2_file_operations vpif_fops = {
1534         .owner          = THIS_MODULE,
1535         .open           = vpif_open,
1536         .release        = vpif_release,
1537         .unlocked_ioctl = video_ioctl2,
1538         .mmap           = vpif_mmap,
1539         .poll           = vpif_poll
1540 };
1541
1542 static struct video_device vpif_video_template = {
1543         .name           = "vpif",
1544         .fops           = &vpif_fops,
1545         .ioctl_ops      = &vpif_ioctl_ops,
1546 };
1547
1548 /*Configure the channels, buffer sizei, request irq */
1549 static int initialize_vpif(void)
1550 {
1551         int free_channel_objects_index;
1552         int free_buffer_channel_index;
1553         int free_buffer_index;
1554         int err = 0, i, j;
1555
1556         /* Default number of buffers should be 3 */
1557         if ((ch2_numbuffers > 0) &&
1558             (ch2_numbuffers < config_params.min_numbuffers))
1559                 ch2_numbuffers = config_params.min_numbuffers;
1560         if ((ch3_numbuffers > 0) &&
1561             (ch3_numbuffers < config_params.min_numbuffers))
1562                 ch3_numbuffers = config_params.min_numbuffers;
1563
1564         /* Set buffer size to min buffers size if invalid buffer size is
1565          * given */
1566         if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1567                 ch2_bufsize =
1568                     config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1569         if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1570                 ch3_bufsize =
1571                     config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1572
1573         config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1574
1575         if (ch2_numbuffers) {
1576                 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1577                                                         ch2_bufsize;
1578         }
1579         config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1580
1581         if (ch3_numbuffers) {
1582                 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1583                                                         ch3_bufsize;
1584         }
1585
1586         /* Allocate memory for six channel objects */
1587         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1588                 vpif_obj.dev[i] =
1589                     kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1590                 /* If memory allocation fails, return error */
1591                 if (!vpif_obj.dev[i]) {
1592                         free_channel_objects_index = i;
1593                         err = -ENOMEM;
1594                         goto vpif_init_free_channel_objects;
1595                 }
1596         }
1597
1598         free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1599         free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1600         free_buffer_index = config_params.numbuffers[i - 1];
1601
1602         return 0;
1603
1604 vpif_init_free_channel_objects:
1605         for (j = 0; j < free_channel_objects_index; j++)
1606                 kfree(vpif_obj.dev[j]);
1607         return err;
1608 }
1609
1610 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1611                             struct v4l2_subdev *subdev,
1612                             struct v4l2_async_subdev *asd)
1613 {
1614         int i;
1615
1616         for (i = 0; i < vpif_obj.config->subdev_count; i++)
1617                 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1618                             subdev->name)) {
1619                         vpif_obj.sd[i] = subdev;
1620                         vpif_obj.sd[i]->grp_id = 1 << i;
1621                         return 0;
1622                 }
1623
1624         return -EINVAL;
1625 }
1626
1627 static int vpif_probe_complete(void)
1628 {
1629         struct common_obj *common;
1630         struct channel_obj *ch;
1631         int j, err, k;
1632
1633         for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1634                 ch = vpif_obj.dev[j];
1635                 /* Initialize field of the channel objects */
1636                 atomic_set(&ch->usrs, 0);
1637                 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1638                         ch->common[k].numbuffers = 0;
1639                         common = &ch->common[k];
1640                         common->io_usrs = 0;
1641                         common->started = 0;
1642                         spin_lock_init(&common->irqlock);
1643                         mutex_init(&common->lock);
1644                         common->numbuffers = 0;
1645                         common->set_addr = NULL;
1646                         common->ytop_off = 0;
1647                         common->ybtm_off = 0;
1648                         common->ctop_off = 0;
1649                         common->cbtm_off = 0;
1650                         common->cur_frm = NULL;
1651                         common->next_frm = NULL;
1652                         memset(&common->fmt, 0, sizeof(common->fmt));
1653                         common->numbuffers = config_params.numbuffers[k];
1654                 }
1655                 ch->initialized = 0;
1656                 if (vpif_obj.config->subdev_count)
1657                         ch->sd = vpif_obj.sd[0];
1658                 ch->channel_id = j;
1659                 if (j < 2)
1660                         ch->common[VPIF_VIDEO_INDEX].numbuffers =
1661                             config_params.numbuffers[ch->channel_id];
1662                 else
1663                         ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1664
1665                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1666
1667                 /* Initialize prio member of channel object */
1668                 v4l2_prio_init(&ch->prio);
1669                 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1670                                                 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1671                 ch->video_dev->lock = &common->lock;
1672                 video_set_drvdata(ch->video_dev, ch);
1673
1674                 /* select output 0 */
1675                 err = vpif_set_output(vpif_obj.config, ch, 0);
1676                 if (err)
1677                         goto probe_out;
1678
1679                 /* register video device */
1680                 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1681                          (int)ch, (int)&ch->video_dev);
1682
1683                 err = video_register_device(ch->video_dev,
1684                                           VFL_TYPE_GRABBER, (j ? 3 : 2));
1685                 if (err < 0)
1686                         goto probe_out;
1687         }
1688
1689         return 0;
1690
1691 probe_out:
1692         for (k = 0; k < j; k++) {
1693                 ch = vpif_obj.dev[k];
1694                 video_unregister_device(ch->video_dev);
1695                 video_device_release(ch->video_dev);
1696                 ch->video_dev = NULL;
1697         }
1698         return err;
1699 }
1700
1701 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1702 {
1703         return vpif_probe_complete();
1704 }
1705
1706 /*
1707  * vpif_probe: This function creates device entries by register itself to the
1708  * V4L2 driver and initializes fields of each channel objects
1709  */
1710 static __init int vpif_probe(struct platform_device *pdev)
1711 {
1712         struct vpif_subdev_info *subdevdata;
1713         int i, j = 0, err = 0;
1714         int res_idx = 0;
1715         struct i2c_adapter *i2c_adap;
1716         struct channel_obj *ch;
1717         struct video_device *vfd;
1718         struct resource *res;
1719         int subdev_count;
1720         size_t size;
1721
1722         vpif_dev = &pdev->dev;
1723         err = initialize_vpif();
1724
1725         if (err) {
1726                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1727                 return err;
1728         }
1729
1730         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1731         if (err) {
1732                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1733                 return err;
1734         }
1735
1736         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1737                 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1738                                         IRQF_SHARED, "VPIF_Display",
1739                                         (void *)(&vpif_obj.dev[res_idx]->
1740                                         channel_id));
1741                 if (err) {
1742                         err = -EINVAL;
1743                         vpif_err("VPIF IRQ request failed\n");
1744                         goto vpif_unregister;
1745                 }
1746                 res_idx++;
1747         }
1748
1749         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1750                 /* Get the pointer to the channel object */
1751                 ch = vpif_obj.dev[i];
1752
1753                 /* Allocate memory for video device */
1754                 vfd = video_device_alloc();
1755                 if (vfd == NULL) {
1756                         for (j = 0; j < i; j++) {
1757                                 ch = vpif_obj.dev[j];
1758                                 video_device_release(ch->video_dev);
1759                         }
1760                         err = -ENOMEM;
1761                         goto vpif_unregister;
1762                 }
1763
1764                 /* Initialize field of video device */
1765                 *vfd = vpif_video_template;
1766                 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1767                 vfd->release = video_device_release;
1768                 vfd->vfl_dir = VFL_DIR_TX;
1769                 snprintf(vfd->name, sizeof(vfd->name),
1770                          "VPIF_Display_DRIVER_V%s",
1771                          VPIF_DISPLAY_VERSION);
1772
1773                 /* Set video_dev to the video device */
1774                 ch->video_dev = vfd;
1775         }
1776
1777         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1778         if (res) {
1779                 size = resource_size(res);
1780                 /* The resources are divided into two equal memory and when
1781                  * we have HD output we can add them together
1782                  */
1783                 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1784                         ch = vpif_obj.dev[j];
1785                         ch->channel_id = j;
1786
1787                         /* only enabled if second resource exists */
1788                         config_params.video_limit[ch->channel_id] = 0;
1789                         if (size)
1790                                 config_params.video_limit[ch->channel_id] =
1791                                                                         size/2;
1792                 }
1793         }
1794         vpif_obj.config = pdev->dev.platform_data;
1795         subdev_count = vpif_obj.config->subdev_count;
1796         subdevdata = vpif_obj.config->subdevinfo;
1797         vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1798                                                                 GFP_KERNEL);
1799         if (vpif_obj.sd == NULL) {
1800                 vpif_err("unable to allocate memory for subdevice pointers\n");
1801                 err = -ENOMEM;
1802                 goto vpif_sd_error;
1803         }
1804
1805         if (!vpif_obj.config->asd_sizes) {
1806                 i2c_adap = i2c_get_adapter(1);
1807                 for (i = 0; i < subdev_count; i++) {
1808                         vpif_obj.sd[i] =
1809                                 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1810                                                           i2c_adap,
1811                                                           &subdevdata[i].
1812                                                           board_info,
1813                                                           NULL);
1814                         if (!vpif_obj.sd[i]) {
1815                                 vpif_err("Error registering v4l2 subdevice\n");
1816                                 err = -ENODEV;
1817                                 goto probe_subdev_out;
1818                         }
1819
1820                         if (vpif_obj.sd[i])
1821                                 vpif_obj.sd[i]->grp_id = 1 << i;
1822                 }
1823                 vpif_probe_complete();
1824         } else {
1825                 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1826                 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1827                 vpif_obj.notifier.bound = vpif_async_bound;
1828                 vpif_obj.notifier.complete = vpif_async_complete;
1829                 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1830                                                    &vpif_obj.notifier);
1831                 if (err) {
1832                         vpif_err("Error registering async notifier\n");
1833                         err = -EINVAL;
1834                         goto probe_subdev_out;
1835                 }
1836         }
1837
1838         return 0;
1839
1840 probe_subdev_out:
1841         kfree(vpif_obj.sd);
1842 vpif_sd_error:
1843         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1844                 ch = vpif_obj.dev[i];
1845                 /* Note: does nothing if ch->video_dev == NULL */
1846                 video_device_release(ch->video_dev);
1847         }
1848 vpif_unregister:
1849         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1850
1851         return err;
1852 }
1853
1854 /*
1855  * vpif_remove: It un-register channels from V4L2 driver
1856  */
1857 static int vpif_remove(struct platform_device *device)
1858 {
1859         struct channel_obj *ch;
1860         int i;
1861
1862         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1863
1864         kfree(vpif_obj.sd);
1865         /* un-register device */
1866         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1867                 /* Get the pointer to the channel object */
1868                 ch = vpif_obj.dev[i];
1869                 /* Unregister video device */
1870                 video_unregister_device(ch->video_dev);
1871
1872                 ch->video_dev = NULL;
1873                 kfree(vpif_obj.dev[i]);
1874         }
1875
1876         return 0;
1877 }
1878
1879 #ifdef CONFIG_PM
1880 static int vpif_suspend(struct device *dev)
1881 {
1882         struct common_obj *common;
1883         struct channel_obj *ch;
1884         int i;
1885
1886         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1887                 /* Get the pointer to the channel object */
1888                 ch = vpif_obj.dev[i];
1889                 common = &ch->common[VPIF_VIDEO_INDEX];
1890                 mutex_lock(&common->lock);
1891                 if (atomic_read(&ch->usrs) && common->io_usrs) {
1892                         /* Disable channel */
1893                         if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1894                                 enable_channel2(0);
1895                                 channel2_intr_enable(0);
1896                         }
1897                         if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1898                                         common->started == 2) {
1899                                 enable_channel3(0);
1900                                 channel3_intr_enable(0);
1901                         }
1902                 }
1903                 mutex_unlock(&common->lock);
1904         }
1905
1906         return 0;
1907 }
1908
1909 static int vpif_resume(struct device *dev)
1910 {
1911
1912         struct common_obj *common;
1913         struct channel_obj *ch;
1914         int i;
1915
1916         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1917                 /* Get the pointer to the channel object */
1918                 ch = vpif_obj.dev[i];
1919                 common = &ch->common[VPIF_VIDEO_INDEX];
1920                 mutex_lock(&common->lock);
1921                 if (atomic_read(&ch->usrs) && common->io_usrs) {
1922                         /* Enable channel */
1923                         if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1924                                 enable_channel2(1);
1925                                 channel2_intr_enable(1);
1926                         }
1927                         if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1928                                         common->started == 2) {
1929                                 enable_channel3(1);
1930                                 channel3_intr_enable(1);
1931                         }
1932                 }
1933                 mutex_unlock(&common->lock);
1934         }
1935
1936         return 0;
1937 }
1938
1939 static const struct dev_pm_ops vpif_pm = {
1940         .suspend        = vpif_suspend,
1941         .resume         = vpif_resume,
1942 };
1943
1944 #define vpif_pm_ops (&vpif_pm)
1945 #else
1946 #define vpif_pm_ops NULL
1947 #endif
1948
1949 static __refdata struct platform_driver vpif_driver = {
1950         .driver = {
1951                         .name   = "vpif_display",
1952                         .owner  = THIS_MODULE,
1953                         .pm     = vpif_pm_ops,
1954         },
1955         .probe  = vpif_probe,
1956         .remove = vpif_remove,
1957 };
1958
1959 module_platform_driver(vpif_driver);