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