[media] media: videobuf2: Restructure vb2_buffer
[linux-2.6-block.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  * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation version 2.
11  *
12  * This program is distributed .as is. WITHOUT ANY WARRANTY of any
13  * kind, whether express or implied; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22
23 #include <media/v4l2-ioctl.h>
24
25 #include "vpif.h"
26 #include "vpif_display.h"
27
28 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
29 MODULE_LICENSE("GPL");
30 MODULE_VERSION(VPIF_DISPLAY_VERSION);
31
32 #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
33
34 #define vpif_err(fmt, arg...)   v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
35 #define vpif_dbg(level, debug, fmt, arg...)     \
36                 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
37
38 static int debug = 1;
39
40 module_param(debug, int, 0644);
41
42 MODULE_PARM_DESC(debug, "Debug level 0-1");
43
44 #define VPIF_DRIVER_NAME        "vpif_display"
45
46 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
47 static int ycmux_mode;
48
49 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
50
51 static struct vpif_device vpif_obj = { {NULL} };
52 static struct device *vpif_dev;
53 static void vpif_calculate_offsets(struct channel_obj *ch);
54 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
55
56 static inline
57 struct vpif_disp_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
58 {
59         return container_of(vb, struct vpif_disp_buffer, vb);
60 }
61
62 /**
63  * vpif_buffer_prepare :  callback function for buffer prepare
64  * @vb: ptr to vb2_buffer
65  *
66  * This is the callback function for buffer prepare when vb2_qbuf()
67  * function is called. The buffer is prepared and user space virtual address
68  * or user address is converted into  physical address
69  */
70 static int vpif_buffer_prepare(struct vb2_buffer *vb)
71 {
72         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
73         struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
74         struct common_obj *common;
75
76         common = &ch->common[VPIF_VIDEO_INDEX];
77
78         vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
79         if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
80                 return -EINVAL;
81
82         vbuf->field = common->fmt.fmt.pix.field;
83
84         if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
85                 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
86
87                 if (!ISALIGNED(addr + common->ytop_off) ||
88                         !ISALIGNED(addr + common->ybtm_off) ||
89                         !ISALIGNED(addr + common->ctop_off) ||
90                         !ISALIGNED(addr + common->cbtm_off)) {
91                         vpif_err("buffer offset not aligned to 8 bytes\n");
92                         return -EINVAL;
93                 }
94         }
95
96         return 0;
97 }
98
99 /**
100  * vpif_buffer_queue_setup : Callback function for buffer setup.
101  * @vq: vb2_queue ptr
102  * @fmt: v4l2 format
103  * @nbuffers: ptr to number of buffers requested by application
104  * @nplanes:: contains number of distinct video planes needed to hold a frame
105  * @sizes[]: contains the size (in bytes) of each plane.
106  * @alloc_ctxs: ptr to allocation context
107  *
108  * This callback function is called when reqbuf() is called to adjust
109  * the buffer count and buffer size
110  */
111 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
112                                 const struct v4l2_format *fmt,
113                                 unsigned int *nbuffers, unsigned int *nplanes,
114                                 unsigned int sizes[], void *alloc_ctxs[])
115 {
116         struct channel_obj *ch = vb2_get_drv_priv(vq);
117         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
118
119         if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
120                 return -EINVAL;
121
122         if (vq->num_buffers + *nbuffers < 3)
123                 *nbuffers = 3 - vq->num_buffers;
124
125         *nplanes = 1;
126         sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
127         alloc_ctxs[0] = common->alloc_ctx;
128
129         /* Calculate the offset for Y and C data  in the buffer */
130         vpif_calculate_offsets(ch);
131
132         return 0;
133 }
134
135 /**
136  * vpif_buffer_queue : Callback function to add buffer to DMA queue
137  * @vb: ptr to vb2_buffer
138  *
139  * This callback fucntion queues the buffer to DMA engine
140  */
141 static void vpif_buffer_queue(struct vb2_buffer *vb)
142 {
143         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
144         struct vpif_disp_buffer *buf = to_vpif_buffer(vbuf);
145         struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
146         struct common_obj *common;
147         unsigned long flags;
148
149         common = &ch->common[VPIF_VIDEO_INDEX];
150
151         /* add the buffer to the DMA queue */
152         spin_lock_irqsave(&common->irqlock, flags);
153         list_add_tail(&buf->list, &common->dma_queue);
154         spin_unlock_irqrestore(&common->irqlock, flags);
155 }
156
157 /**
158  * vpif_start_streaming : Starts the DMA engine for streaming
159  * @vb: ptr to vb2_buffer
160  * @count: number of buffers
161  */
162 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
163 {
164         struct vpif_display_config *vpif_config_data =
165                                         vpif_dev->platform_data;
166         struct channel_obj *ch = vb2_get_drv_priv(vq);
167         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
168         struct vpif_params *vpif = &ch->vpifparams;
169         struct vpif_disp_buffer *buf, *tmp;
170         unsigned long addr, flags;
171         int ret;
172
173         spin_lock_irqsave(&common->irqlock, flags);
174
175         /* Initialize field_id */
176         ch->field_id = 0;
177
178         /* clock settings */
179         if (vpif_config_data->set_clock) {
180                 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
181                 ycmux_mode, ch->vpifparams.std_info.hd_sd);
182                 if (ret < 0) {
183                         vpif_err("can't set clock\n");
184                         goto err;
185                 }
186         }
187
188         /* set the parameters and addresses */
189         ret = vpif_set_video_params(vpif, ch->channel_id + 2);
190         if (ret < 0)
191                 goto err;
192
193         ycmux_mode = ret;
194         vpif_config_addr(ch, ret);
195         /* Get the next frame from the buffer queue */
196         common->next_frm = common->cur_frm =
197                             list_entry(common->dma_queue.next,
198                                        struct vpif_disp_buffer, list);
199
200         list_del(&common->cur_frm->list);
201         spin_unlock_irqrestore(&common->irqlock, flags);
202
203         addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
204         common->set_addr((addr + common->ytop_off),
205                             (addr + common->ybtm_off),
206                             (addr + common->ctop_off),
207                             (addr + common->cbtm_off));
208
209         /*
210          * Set interrupt for both the fields in VPIF
211          * Register enable channel in VPIF register
212          */
213         channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
214         if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
215                 channel2_intr_assert();
216                 channel2_intr_enable(1);
217                 enable_channel2(1);
218                 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
219                         channel2_clipping_enable(1);
220         }
221
222         if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
223                 channel3_intr_assert();
224                 channel3_intr_enable(1);
225                 enable_channel3(1);
226                 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
227                         channel3_clipping_enable(1);
228         }
229
230         return 0;
231
232 err:
233         list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
234                 list_del(&buf->list);
235                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
236         }
237         spin_unlock_irqrestore(&common->irqlock, flags);
238
239         return ret;
240 }
241
242 /**
243  * vpif_stop_streaming : Stop the DMA engine
244  * @vq: ptr to vb2_queue
245  *
246  * This callback stops the DMA engine and any remaining buffers
247  * in the DMA queue are released.
248  */
249 static void vpif_stop_streaming(struct vb2_queue *vq)
250 {
251         struct channel_obj *ch = vb2_get_drv_priv(vq);
252         struct common_obj *common;
253         unsigned long flags;
254
255         common = &ch->common[VPIF_VIDEO_INDEX];
256
257         /* Disable channel */
258         if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
259                 enable_channel2(0);
260                 channel2_intr_enable(0);
261         }
262         if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
263                 enable_channel3(0);
264                 channel3_intr_enable(0);
265         }
266
267         /* release all active buffers */
268         spin_lock_irqsave(&common->irqlock, flags);
269         if (common->cur_frm == common->next_frm) {
270                 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
271                                 VB2_BUF_STATE_ERROR);
272         } else {
273                 if (common->cur_frm != NULL)
274                         vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
275                                         VB2_BUF_STATE_ERROR);
276                 if (common->next_frm != NULL)
277                         vb2_buffer_done(&common->next_frm->vb.vb2_buf,
278                                         VB2_BUF_STATE_ERROR);
279         }
280
281         while (!list_empty(&common->dma_queue)) {
282                 common->next_frm = list_entry(common->dma_queue.next,
283                                                 struct vpif_disp_buffer, list);
284                 list_del(&common->next_frm->list);
285                 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
286                                 VB2_BUF_STATE_ERROR);
287         }
288         spin_unlock_irqrestore(&common->irqlock, flags);
289 }
290
291 static struct vb2_ops video_qops = {
292         .queue_setup            = vpif_buffer_queue_setup,
293         .wait_prepare           = vb2_ops_wait_prepare,
294         .wait_finish            = vb2_ops_wait_finish,
295         .buf_prepare            = vpif_buffer_prepare,
296         .start_streaming        = vpif_start_streaming,
297         .stop_streaming         = vpif_stop_streaming,
298         .buf_queue              = vpif_buffer_queue,
299 };
300
301 static void process_progressive_mode(struct common_obj *common)
302 {
303         unsigned long addr = 0;
304
305         spin_lock(&common->irqlock);
306         /* Get the next buffer from buffer queue */
307         common->next_frm = list_entry(common->dma_queue.next,
308                                 struct vpif_disp_buffer, list);
309         /* Remove that buffer from the buffer queue */
310         list_del(&common->next_frm->list);
311         spin_unlock(&common->irqlock);
312
313         /* Set top and bottom field addrs in VPIF registers */
314         addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
315         common->set_addr(addr + common->ytop_off,
316                                  addr + common->ybtm_off,
317                                  addr + common->ctop_off,
318                                  addr + common->cbtm_off);
319 }
320
321 static void process_interlaced_mode(int fid, struct common_obj *common)
322 {
323         /* device field id and local field id are in sync */
324         /* If this is even field */
325         if (0 == fid) {
326                 if (common->cur_frm == common->next_frm)
327                         return;
328
329                 /* one frame is displayed If next frame is
330                  *  available, release cur_frm and move on */
331                 /* Copy frame display time */
332                 v4l2_get_timestamp(&common->cur_frm->vb.timestamp);
333                 /* Change status of the cur_frm */
334                 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
335                                         VB2_BUF_STATE_DONE);
336                 /* Make cur_frm pointing to next_frm */
337                 common->cur_frm = common->next_frm;
338
339         } else if (1 == fid) {  /* odd field */
340                 spin_lock(&common->irqlock);
341                 if (list_empty(&common->dma_queue)
342                     || (common->cur_frm != common->next_frm)) {
343                         spin_unlock(&common->irqlock);
344                         return;
345                 }
346                 spin_unlock(&common->irqlock);
347                 /* one field is displayed configure the next
348                  * frame if it is available else hold on current
349                  * frame */
350                 /* Get next from the buffer queue */
351                 process_progressive_mode(common);
352         }
353 }
354
355 /*
356  * vpif_channel_isr: It changes status of the displayed buffer, takes next
357  * buffer from the queue and sets its address in VPIF registers
358  */
359 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
360 {
361         struct vpif_device *dev = &vpif_obj;
362         struct channel_obj *ch;
363         struct common_obj *common;
364         int fid = -1, i;
365         int channel_id = 0;
366
367         channel_id = *(int *)(dev_id);
368         if (!vpif_intr_status(channel_id + 2))
369                 return IRQ_NONE;
370
371         ch = dev->dev[channel_id];
372         for (i = 0; i < VPIF_NUMOBJECTS; i++) {
373                 common = &ch->common[i];
374                 /* If streaming is started in this channel */
375
376                 if (1 == ch->vpifparams.std_info.frm_fmt) {
377                         spin_lock(&common->irqlock);
378                         if (list_empty(&common->dma_queue)) {
379                                 spin_unlock(&common->irqlock);
380                                 continue;
381                         }
382                         spin_unlock(&common->irqlock);
383
384                         /* Progressive mode */
385                         if (!channel_first_int[i][channel_id]) {
386                                 /* Mark status of the cur_frm to
387                                  * done and unlock semaphore on it */
388                                 v4l2_get_timestamp(
389                                         &common->cur_frm->vb.timestamp);
390                                 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
391                                                 VB2_BUF_STATE_DONE);
392                                 /* Make cur_frm pointing to next_frm */
393                                 common->cur_frm = common->next_frm;
394                         }
395
396                         channel_first_int[i][channel_id] = 0;
397                         process_progressive_mode(common);
398                 } else {
399                         /* Interlaced mode */
400                         /* If it is first interrupt, ignore it */
401
402                         if (channel_first_int[i][channel_id]) {
403                                 channel_first_int[i][channel_id] = 0;
404                                 continue;
405                         }
406
407                         if (0 == i) {
408                                 ch->field_id ^= 1;
409                                 /* Get field id from VPIF registers */
410                                 fid = vpif_channel_getfid(ch->channel_id + 2);
411                                 /* If fid does not match with stored field id */
412                                 if (fid != ch->field_id) {
413                                         /* Make them in sync */
414                                         if (0 == fid)
415                                                 ch->field_id = fid;
416
417                                         return IRQ_HANDLED;
418                                 }
419                         }
420                         process_interlaced_mode(fid, common);
421                 }
422         }
423
424         return IRQ_HANDLED;
425 }
426
427 static int vpif_update_std_info(struct channel_obj *ch)
428 {
429         struct video_obj *vid_ch = &ch->video;
430         struct vpif_params *vpifparams = &ch->vpifparams;
431         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
432         const struct vpif_channel_config_params *config;
433
434         int i;
435
436         for (i = 0; i < vpif_ch_params_count; i++) {
437                 config = &vpif_ch_params[i];
438                 if (config->hd_sd == 0) {
439                         vpif_dbg(2, debug, "SD format\n");
440                         if (config->stdid & vid_ch->stdid) {
441                                 memcpy(std_info, config, sizeof(*config));
442                                 break;
443                         }
444                 }
445         }
446
447         if (i == vpif_ch_params_count) {
448                 vpif_dbg(1, debug, "Format not found\n");
449                 return -EINVAL;
450         }
451
452         return 0;
453 }
454
455 static int vpif_update_resolution(struct channel_obj *ch)
456 {
457         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
458         struct video_obj *vid_ch = &ch->video;
459         struct vpif_params *vpifparams = &ch->vpifparams;
460         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
461
462         if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
463                 return -EINVAL;
464
465         if (vid_ch->stdid) {
466                 if (vpif_update_std_info(ch))
467                         return -EINVAL;
468         }
469
470         common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
471         common->fmt.fmt.pix.width = std_info->width;
472         common->fmt.fmt.pix.height = std_info->height;
473         vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
474                         common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
475
476         /* Set height and width paramateres */
477         common->height = std_info->height;
478         common->width = std_info->width;
479         common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
480
481         if (vid_ch->stdid)
482                 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
483         else
484                 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
485
486         if (ch->vpifparams.std_info.frm_fmt)
487                 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
488         else
489                 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
490
491         return 0;
492 }
493
494 /*
495  * vpif_calculate_offsets: This function calculates buffers offset for Y and C
496  * in the top and bottom field
497  */
498 static void vpif_calculate_offsets(struct channel_obj *ch)
499 {
500         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
501         struct vpif_params *vpifparams = &ch->vpifparams;
502         enum v4l2_field field = common->fmt.fmt.pix.field;
503         struct video_obj *vid_ch = &ch->video;
504         unsigned int hpitch, sizeimage;
505
506         if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
507                 if (ch->vpifparams.std_info.frm_fmt)
508                         vid_ch->buf_field = V4L2_FIELD_NONE;
509                 else
510                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
511         } else {
512                 vid_ch->buf_field = common->fmt.fmt.pix.field;
513         }
514
515         sizeimage = common->fmt.fmt.pix.sizeimage;
516
517         hpitch = common->fmt.fmt.pix.bytesperline;
518         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
519             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
520                 common->ytop_off = 0;
521                 common->ybtm_off = hpitch;
522                 common->ctop_off = sizeimage / 2;
523                 common->cbtm_off = sizeimage / 2 + hpitch;
524         } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
525                 common->ytop_off = 0;
526                 common->ybtm_off = sizeimage / 4;
527                 common->ctop_off = sizeimage / 2;
528                 common->cbtm_off = common->ctop_off + sizeimage / 4;
529         } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
530                 common->ybtm_off = 0;
531                 common->ytop_off = sizeimage / 4;
532                 common->cbtm_off = sizeimage / 2;
533                 common->ctop_off = common->cbtm_off + sizeimage / 4;
534         }
535
536         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
537             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
538                 vpifparams->video_params.storage_mode = 1;
539         } else {
540                 vpifparams->video_params.storage_mode = 0;
541         }
542
543         if (ch->vpifparams.std_info.frm_fmt == 1) {
544                 vpifparams->video_params.hpitch =
545                     common->fmt.fmt.pix.bytesperline;
546         } else {
547                 if ((field == V4L2_FIELD_ANY) ||
548                         (field == V4L2_FIELD_INTERLACED))
549                         vpifparams->video_params.hpitch =
550                             common->fmt.fmt.pix.bytesperline * 2;
551                 else
552                         vpifparams->video_params.hpitch =
553                             common->fmt.fmt.pix.bytesperline;
554         }
555
556         ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
557 }
558
559 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
560 {
561         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
562
563         if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
564                 common->set_addr = ch3_set_videobuf_addr;
565         } else {
566                 if (2 == muxmode)
567                         common->set_addr = ch2_set_videobuf_addr_yc_nmux;
568                 else
569                         common->set_addr = ch2_set_videobuf_addr;
570         }
571 }
572
573 /* functions implementing ioctls */
574 /**
575  * vpif_querycap() - QUERYCAP handler
576  * @file: file ptr
577  * @priv: file handle
578  * @cap: ptr to v4l2_capability structure
579  */
580 static int vpif_querycap(struct file *file, void  *priv,
581                                 struct v4l2_capability *cap)
582 {
583         struct vpif_display_config *config = vpif_dev->platform_data;
584
585         cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
586         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
587         strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
588         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
589                  dev_name(vpif_dev));
590         strlcpy(cap->card, config->card_name, sizeof(cap->card));
591
592         return 0;
593 }
594
595 static int vpif_enum_fmt_vid_out(struct file *file, void  *priv,
596                                         struct v4l2_fmtdesc *fmt)
597 {
598         if (fmt->index != 0)
599                 return -EINVAL;
600
601         /* Fill in the information about format */
602         fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
603         strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
604         fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
605         fmt->flags = 0;
606         return 0;
607 }
608
609 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
610                                 struct v4l2_format *fmt)
611 {
612         struct video_device *vdev = video_devdata(file);
613         struct channel_obj *ch = video_get_drvdata(vdev);
614         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
615
616         /* Check the validity of the buffer type */
617         if (common->fmt.type != fmt->type)
618                 return -EINVAL;
619
620         if (vpif_update_resolution(ch))
621                 return -EINVAL;
622         *fmt = common->fmt;
623         return 0;
624 }
625
626 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
627                                 struct v4l2_format *fmt)
628 {
629         struct video_device *vdev = video_devdata(file);
630         struct channel_obj *ch = video_get_drvdata(vdev);
631         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
632         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
633
634         /*
635          * to supress v4l-compliance warnings silently correct
636          * the pixelformat
637          */
638         if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
639                 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
640
641         if (vpif_update_resolution(ch))
642                 return -EINVAL;
643
644         pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
645         pixfmt->field = common->fmt.fmt.pix.field;
646         pixfmt->bytesperline = common->fmt.fmt.pix.width;
647         pixfmt->width = common->fmt.fmt.pix.width;
648         pixfmt->height = common->fmt.fmt.pix.height;
649         pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
650
651         return 0;
652 }
653
654 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
655                                 struct v4l2_format *fmt)
656 {
657         struct video_device *vdev = video_devdata(file);
658         struct channel_obj *ch = video_get_drvdata(vdev);
659         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
660         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
661         int ret;
662
663         if (vb2_is_busy(&common->buffer_queue))
664                 return -EBUSY;
665
666         ret = vpif_try_fmt_vid_out(file, priv, fmt);
667         if (ret)
668                 return ret;
669
670         /* store the pix format in the channel object */
671         common->fmt.fmt.pix = *pixfmt;
672
673         /* store the format in the channel object */
674         common->fmt = *fmt;
675         return 0;
676 }
677
678 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
679 {
680         struct vpif_display_config *config = vpif_dev->platform_data;
681         struct video_device *vdev = video_devdata(file);
682         struct channel_obj *ch = video_get_drvdata(vdev);
683         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
684         struct vpif_display_chan_config *chan_cfg;
685         struct v4l2_output output;
686         int ret;
687
688         if (config->chan_config[ch->channel_id].outputs == NULL)
689                 return -ENODATA;
690
691         chan_cfg = &config->chan_config[ch->channel_id];
692         output = chan_cfg->outputs[ch->output_idx].output;
693         if (output.capabilities != V4L2_OUT_CAP_STD)
694                 return -ENODATA;
695
696         if (vb2_is_busy(&common->buffer_queue))
697                 return -EBUSY;
698
699
700         if (!(std_id & VPIF_V4L2_STD))
701                 return -EINVAL;
702
703         /* Call encoder subdevice function to set the standard */
704         ch->video.stdid = std_id;
705         memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
706         /* Get the information about the standard */
707         if (vpif_update_resolution(ch))
708                 return -EINVAL;
709
710         common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
711
712         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
713                                                 s_std_output, std_id);
714         if (ret < 0) {
715                 vpif_err("Failed to set output standard\n");
716                 return ret;
717         }
718
719         ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
720                                                         s_std, std_id);
721         if (ret < 0)
722                 vpif_err("Failed to set standard for sub devices\n");
723         return ret;
724 }
725
726 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
727 {
728         struct vpif_display_config *config = vpif_dev->platform_data;
729         struct video_device *vdev = video_devdata(file);
730         struct channel_obj *ch = video_get_drvdata(vdev);
731         struct vpif_display_chan_config *chan_cfg;
732         struct v4l2_output output;
733
734         if (config->chan_config[ch->channel_id].outputs == NULL)
735                 return -ENODATA;
736
737         chan_cfg = &config->chan_config[ch->channel_id];
738         output = chan_cfg->outputs[ch->output_idx].output;
739         if (output.capabilities != V4L2_OUT_CAP_STD)
740                 return -ENODATA;
741
742         *std = ch->video.stdid;
743         return 0;
744 }
745
746 static int vpif_enum_output(struct file *file, void *fh,
747                                 struct v4l2_output *output)
748 {
749
750         struct vpif_display_config *config = vpif_dev->platform_data;
751         struct video_device *vdev = video_devdata(file);
752         struct channel_obj *ch = video_get_drvdata(vdev);
753         struct vpif_display_chan_config *chan_cfg;
754
755         chan_cfg = &config->chan_config[ch->channel_id];
756         if (output->index >= chan_cfg->output_count) {
757                 vpif_dbg(1, debug, "Invalid output index\n");
758                 return -EINVAL;
759         }
760
761         *output = chan_cfg->outputs[output->index].output;
762         return 0;
763 }
764
765 /**
766  * vpif_output_to_subdev() - Maps output to sub device
767  * @vpif_cfg - global config ptr
768  * @chan_cfg - channel config ptr
769  * @index - Given output index from application
770  *
771  * lookup the sub device information for a given output index.
772  * we report all the output to application. output table also
773  * has sub device name for the each output
774  */
775 static int
776 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
777                       struct vpif_display_chan_config *chan_cfg, int index)
778 {
779         struct vpif_subdev_info *subdev_info;
780         const char *subdev_name;
781         int i;
782
783         vpif_dbg(2, debug, "vpif_output_to_subdev\n");
784
785         if (chan_cfg->outputs == NULL)
786                 return -1;
787
788         subdev_name = chan_cfg->outputs[index].subdev_name;
789         if (subdev_name == NULL)
790                 return -1;
791
792         /* loop through the sub device list to get the sub device info */
793         for (i = 0; i < vpif_cfg->subdev_count; i++) {
794                 subdev_info = &vpif_cfg->subdevinfo[i];
795                 if (!strcmp(subdev_info->name, subdev_name))
796                         return i;
797         }
798         return -1;
799 }
800
801 /**
802  * vpif_set_output() - Select an output
803  * @vpif_cfg - global config ptr
804  * @ch - channel
805  * @index - Given output index from application
806  *
807  * Select the given output.
808  */
809 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
810                       struct channel_obj *ch, int index)
811 {
812         struct vpif_display_chan_config *chan_cfg =
813                 &vpif_cfg->chan_config[ch->channel_id];
814         struct v4l2_subdev *sd = NULL;
815         u32 input = 0, output = 0;
816         int sd_index;
817         int ret;
818
819         sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
820         if (sd_index >= 0)
821                 sd = vpif_obj.sd[sd_index];
822
823         if (sd) {
824                 input = chan_cfg->outputs[index].input_route;
825                 output = chan_cfg->outputs[index].output_route;
826                 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
827                 if (ret < 0 && ret != -ENOIOCTLCMD) {
828                         vpif_err("Failed to set output\n");
829                         return ret;
830                 }
831
832         }
833         ch->output_idx = index;
834         ch->sd = sd;
835         if (chan_cfg->outputs != NULL)
836                 /* update tvnorms from the sub device output info */
837                 ch->video_dev.tvnorms = chan_cfg->outputs[index].output.std;
838         return 0;
839 }
840
841 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
842 {
843         struct vpif_display_config *config = vpif_dev->platform_data;
844         struct video_device *vdev = video_devdata(file);
845         struct channel_obj *ch = video_get_drvdata(vdev);
846         struct vpif_display_chan_config *chan_cfg;
847         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
848
849         if (vb2_is_busy(&common->buffer_queue))
850                 return -EBUSY;
851
852         chan_cfg = &config->chan_config[ch->channel_id];
853
854         if (i >= chan_cfg->output_count)
855                 return -EINVAL;
856
857         return vpif_set_output(config, ch, i);
858 }
859
860 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
861 {
862         struct video_device *vdev = video_devdata(file);
863         struct channel_obj *ch = video_get_drvdata(vdev);
864
865         *i = ch->output_idx;
866
867         return 0;
868 }
869
870 /**
871  * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
872  * @file: file ptr
873  * @priv: file handle
874  * @timings: input timings
875  */
876 static int
877 vpif_enum_dv_timings(struct file *file, void *priv,
878                      struct v4l2_enum_dv_timings *timings)
879 {
880         struct vpif_display_config *config = vpif_dev->platform_data;
881         struct video_device *vdev = video_devdata(file);
882         struct channel_obj *ch = video_get_drvdata(vdev);
883         struct vpif_display_chan_config *chan_cfg;
884         struct v4l2_output output;
885         int ret;
886
887         if (config->chan_config[ch->channel_id].outputs == NULL)
888                 return -ENODATA;
889
890         chan_cfg = &config->chan_config[ch->channel_id];
891         output = chan_cfg->outputs[ch->output_idx].output;
892         if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
893                 return -ENODATA;
894
895         timings->pad = 0;
896
897         ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
898         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
899                 return -EINVAL;
900         return ret;
901 }
902
903 /**
904  * vpif_s_dv_timings() - S_DV_TIMINGS handler
905  * @file: file ptr
906  * @priv: file handle
907  * @timings: digital video timings
908  */
909 static int vpif_s_dv_timings(struct file *file, void *priv,
910                 struct v4l2_dv_timings *timings)
911 {
912         struct vpif_display_config *config = vpif_dev->platform_data;
913         struct video_device *vdev = video_devdata(file);
914         struct channel_obj *ch = video_get_drvdata(vdev);
915         struct vpif_params *vpifparams = &ch->vpifparams;
916         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
917         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
918         struct video_obj *vid_ch = &ch->video;
919         struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
920         struct vpif_display_chan_config *chan_cfg;
921         struct v4l2_output output;
922         int ret;
923
924         if (config->chan_config[ch->channel_id].outputs == NULL)
925                 return -ENODATA;
926
927         chan_cfg = &config->chan_config[ch->channel_id];
928         output = chan_cfg->outputs[ch->output_idx].output;
929         if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
930                 return -ENODATA;
931
932         if (vb2_is_busy(&common->buffer_queue))
933                 return -EBUSY;
934
935         if (timings->type != V4L2_DV_BT_656_1120) {
936                 vpif_dbg(2, debug, "Timing type not defined\n");
937                 return -EINVAL;
938         }
939
940         /* Configure subdevice timings, if any */
941         ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
942         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
943                 ret = 0;
944         if (ret < 0) {
945                 vpif_dbg(2, debug, "Error setting custom DV timings\n");
946                 return ret;
947         }
948
949         if (!(timings->bt.width && timings->bt.height &&
950                                 (timings->bt.hbackporch ||
951                                  timings->bt.hfrontporch ||
952                                  timings->bt.hsync) &&
953                                 timings->bt.vfrontporch &&
954                                 (timings->bt.vbackporch ||
955                                  timings->bt.vsync))) {
956                 vpif_dbg(2, debug, "Timings for width, height, "
957                                 "horizontal back porch, horizontal sync, "
958                                 "horizontal front porch, vertical back porch, "
959                                 "vertical sync and vertical back porch "
960                                 "must be defined\n");
961                 return -EINVAL;
962         }
963
964         vid_ch->dv_timings = *timings;
965
966         /* Configure video port timings */
967
968         std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
969         std_info->sav2eav = bt->width;
970
971         std_info->l1 = 1;
972         std_info->l3 = bt->vsync + bt->vbackporch + 1;
973
974         std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
975         if (bt->interlaced) {
976                 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
977                         std_info->l5 = std_info->vsize/2 -
978                                 (bt->vfrontporch - 1);
979                         std_info->l7 = std_info->vsize/2 + 1;
980                         std_info->l9 = std_info->l7 + bt->il_vsync +
981                                 bt->il_vbackporch + 1;
982                         std_info->l11 = std_info->vsize -
983                                 (bt->il_vfrontporch - 1);
984                 } else {
985                         vpif_dbg(2, debug, "Required timing values for "
986                                         "interlaced BT format missing\n");
987                         return -EINVAL;
988                 }
989         } else {
990                 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
991         }
992         strncpy(std_info->name, "Custom timings BT656/1120",
993                         VPIF_MAX_NAME);
994         std_info->width = bt->width;
995         std_info->height = bt->height;
996         std_info->frm_fmt = bt->interlaced ? 0 : 1;
997         std_info->ycmux_mode = 0;
998         std_info->capture_format = 0;
999         std_info->vbi_supported = 0;
1000         std_info->hd_sd = 1;
1001         std_info->stdid = 0;
1002         vid_ch->stdid = 0;
1003
1004         return 0;
1005 }
1006
1007 /**
1008  * vpif_g_dv_timings() - G_DV_TIMINGS handler
1009  * @file: file ptr
1010  * @priv: file handle
1011  * @timings: digital video timings
1012  */
1013 static int vpif_g_dv_timings(struct file *file, void *priv,
1014                 struct v4l2_dv_timings *timings)
1015 {
1016         struct vpif_display_config *config = vpif_dev->platform_data;
1017         struct video_device *vdev = video_devdata(file);
1018         struct channel_obj *ch = video_get_drvdata(vdev);
1019         struct vpif_display_chan_config *chan_cfg;
1020         struct video_obj *vid_ch = &ch->video;
1021         struct v4l2_output output;
1022
1023         if (config->chan_config[ch->channel_id].outputs == NULL)
1024                 goto error;
1025
1026         chan_cfg = &config->chan_config[ch->channel_id];
1027         output = chan_cfg->outputs[ch->output_idx].output;
1028
1029         if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1030                 goto error;
1031
1032         *timings = vid_ch->dv_timings;
1033
1034         return 0;
1035 error:
1036         return -ENODATA;
1037 }
1038
1039 /*
1040  * vpif_log_status() - Status information
1041  * @file: file ptr
1042  * @priv: file handle
1043  *
1044  * Returns zero.
1045  */
1046 static int vpif_log_status(struct file *filep, void *priv)
1047 {
1048         /* status for sub devices */
1049         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1050
1051         return 0;
1052 }
1053
1054 /* vpif display ioctl operations */
1055 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1056         .vidioc_querycap                = vpif_querycap,
1057         .vidioc_enum_fmt_vid_out        = vpif_enum_fmt_vid_out,
1058         .vidioc_g_fmt_vid_out           = vpif_g_fmt_vid_out,
1059         .vidioc_s_fmt_vid_out           = vpif_s_fmt_vid_out,
1060         .vidioc_try_fmt_vid_out         = vpif_try_fmt_vid_out,
1061
1062         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
1063         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
1064         .vidioc_querybuf                = vb2_ioctl_querybuf,
1065         .vidioc_qbuf                    = vb2_ioctl_qbuf,
1066         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
1067         .vidioc_expbuf                  = vb2_ioctl_expbuf,
1068         .vidioc_streamon                = vb2_ioctl_streamon,
1069         .vidioc_streamoff               = vb2_ioctl_streamoff,
1070
1071         .vidioc_s_std                   = vpif_s_std,
1072         .vidioc_g_std                   = vpif_g_std,
1073
1074         .vidioc_enum_output             = vpif_enum_output,
1075         .vidioc_s_output                = vpif_s_output,
1076         .vidioc_g_output                = vpif_g_output,
1077
1078         .vidioc_enum_dv_timings         = vpif_enum_dv_timings,
1079         .vidioc_s_dv_timings            = vpif_s_dv_timings,
1080         .vidioc_g_dv_timings            = vpif_g_dv_timings,
1081
1082         .vidioc_log_status              = vpif_log_status,
1083 };
1084
1085 static const struct v4l2_file_operations vpif_fops = {
1086         .owner          = THIS_MODULE,
1087         .open           = v4l2_fh_open,
1088         .release        = vb2_fop_release,
1089         .unlocked_ioctl = video_ioctl2,
1090         .mmap           = vb2_fop_mmap,
1091         .poll           = vb2_fop_poll
1092 };
1093
1094 /*Configure the channels, buffer sizei, request irq */
1095 static int initialize_vpif(void)
1096 {
1097         int free_channel_objects_index;
1098         int err, i, j;
1099
1100         /* Allocate memory for six channel objects */
1101         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1102                 vpif_obj.dev[i] =
1103                     kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1104                 /* If memory allocation fails, return error */
1105                 if (!vpif_obj.dev[i]) {
1106                         free_channel_objects_index = i;
1107                         err = -ENOMEM;
1108                         goto vpif_init_free_channel_objects;
1109                 }
1110         }
1111
1112         return 0;
1113
1114 vpif_init_free_channel_objects:
1115         for (j = 0; j < free_channel_objects_index; j++)
1116                 kfree(vpif_obj.dev[j]);
1117         return err;
1118 }
1119
1120 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1121                             struct v4l2_subdev *subdev,
1122                             struct v4l2_async_subdev *asd)
1123 {
1124         int i;
1125
1126         for (i = 0; i < vpif_obj.config->subdev_count; i++)
1127                 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1128                             subdev->name)) {
1129                         vpif_obj.sd[i] = subdev;
1130                         vpif_obj.sd[i]->grp_id = 1 << i;
1131                         return 0;
1132                 }
1133
1134         return -EINVAL;
1135 }
1136
1137 static int vpif_probe_complete(void)
1138 {
1139         struct common_obj *common;
1140         struct video_device *vdev;
1141         struct channel_obj *ch;
1142         struct vb2_queue *q;
1143         int j, err, k;
1144
1145         for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1146                 ch = vpif_obj.dev[j];
1147                 /* Initialize field of the channel objects */
1148                 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1149                         common = &ch->common[k];
1150                         spin_lock_init(&common->irqlock);
1151                         mutex_init(&common->lock);
1152                         common->set_addr = NULL;
1153                         common->ytop_off = 0;
1154                         common->ybtm_off = 0;
1155                         common->ctop_off = 0;
1156                         common->cbtm_off = 0;
1157                         common->cur_frm = NULL;
1158                         common->next_frm = NULL;
1159                         memset(&common->fmt, 0, sizeof(common->fmt));
1160                 }
1161                 ch->initialized = 0;
1162                 if (vpif_obj.config->subdev_count)
1163                         ch->sd = vpif_obj.sd[0];
1164                 ch->channel_id = j;
1165
1166                 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1167
1168                 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1169                                                 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1170
1171                 /* select output 0 */
1172                 err = vpif_set_output(vpif_obj.config, ch, 0);
1173                 if (err)
1174                         goto probe_out;
1175
1176                 /* set initial format */
1177                 ch->video.stdid = V4L2_STD_525_60;
1178                 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1179                 vpif_update_resolution(ch);
1180
1181                 /* Initialize vb2 queue */
1182                 q = &common->buffer_queue;
1183                 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1184                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1185                 q->drv_priv = ch;
1186                 q->ops = &video_qops;
1187                 q->mem_ops = &vb2_dma_contig_memops;
1188                 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1189                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1190                 q->min_buffers_needed = 1;
1191                 q->lock = &common->lock;
1192                 err = vb2_queue_init(q);
1193                 if (err) {
1194                         vpif_err("vpif_display: vb2_queue_init() failed\n");
1195                         goto probe_out;
1196                 }
1197
1198                 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1199                 if (IS_ERR(common->alloc_ctx)) {
1200                         vpif_err("Failed to get the context\n");
1201                         err = PTR_ERR(common->alloc_ctx);
1202                         goto probe_out;
1203                 }
1204
1205                 INIT_LIST_HEAD(&common->dma_queue);
1206
1207                 /* register video device */
1208                 vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
1209                          ch, &ch->video_dev);
1210
1211                 /* Initialize the video_device structure */
1212                 vdev = &ch->video_dev;
1213                 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1214                 vdev->release = video_device_release_empty;
1215                 vdev->fops = &vpif_fops;
1216                 vdev->ioctl_ops = &vpif_ioctl_ops;
1217                 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1218                 vdev->vfl_dir = VFL_DIR_TX;
1219                 vdev->queue = q;
1220                 vdev->lock = &common->lock;
1221                 video_set_drvdata(&ch->video_dev, ch);
1222                 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1223                                             (j ? 3 : 2));
1224                 if (err < 0)
1225                         goto probe_out;
1226         }
1227
1228         return 0;
1229
1230 probe_out:
1231         for (k = 0; k < j; k++) {
1232                 ch = vpif_obj.dev[k];
1233                 common = &ch->common[k];
1234                 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1235                 video_unregister_device(&ch->video_dev);
1236         }
1237         return err;
1238 }
1239
1240 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1241 {
1242         return vpif_probe_complete();
1243 }
1244
1245 /*
1246  * vpif_probe: This function creates device entries by register itself to the
1247  * V4L2 driver and initializes fields of each channel objects
1248  */
1249 static __init int vpif_probe(struct platform_device *pdev)
1250 {
1251         struct vpif_subdev_info *subdevdata;
1252         struct i2c_adapter *i2c_adap;
1253         struct resource *res;
1254         int subdev_count;
1255         int res_idx = 0;
1256         int i, err;
1257
1258         vpif_dev = &pdev->dev;
1259         err = initialize_vpif();
1260
1261         if (err) {
1262                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1263                 return err;
1264         }
1265
1266         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1267         if (err) {
1268                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1269                 return err;
1270         }
1271
1272         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1273                 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1274                                         IRQF_SHARED, VPIF_DRIVER_NAME,
1275                                         (void *)(&vpif_obj.dev[res_idx]->
1276                                         channel_id));
1277                 if (err) {
1278                         err = -EINVAL;
1279                         vpif_err("VPIF IRQ request failed\n");
1280                         goto vpif_unregister;
1281                 }
1282                 res_idx++;
1283         }
1284
1285         vpif_obj.config = pdev->dev.platform_data;
1286         subdev_count = vpif_obj.config->subdev_count;
1287         subdevdata = vpif_obj.config->subdevinfo;
1288         vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1289                                                                 GFP_KERNEL);
1290         if (vpif_obj.sd == NULL) {
1291                 vpif_err("unable to allocate memory for subdevice pointers\n");
1292                 err = -ENOMEM;
1293                 goto vpif_unregister;
1294         }
1295
1296         if (!vpif_obj.config->asd_sizes) {
1297                 i2c_adap = i2c_get_adapter(1);
1298                 for (i = 0; i < subdev_count; i++) {
1299                         vpif_obj.sd[i] =
1300                                 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1301                                                           i2c_adap,
1302                                                           &subdevdata[i].
1303                                                           board_info,
1304                                                           NULL);
1305                         if (!vpif_obj.sd[i]) {
1306                                 vpif_err("Error registering v4l2 subdevice\n");
1307                                 err = -ENODEV;
1308                                 goto probe_subdev_out;
1309                         }
1310
1311                         if (vpif_obj.sd[i])
1312                                 vpif_obj.sd[i]->grp_id = 1 << i;
1313                 }
1314                 vpif_probe_complete();
1315         } else {
1316                 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1317                 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1318                 vpif_obj.notifier.bound = vpif_async_bound;
1319                 vpif_obj.notifier.complete = vpif_async_complete;
1320                 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1321                                                    &vpif_obj.notifier);
1322                 if (err) {
1323                         vpif_err("Error registering async notifier\n");
1324                         err = -EINVAL;
1325                         goto probe_subdev_out;
1326                 }
1327         }
1328
1329         return 0;
1330
1331 probe_subdev_out:
1332         kfree(vpif_obj.sd);
1333 vpif_unregister:
1334         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1335
1336         return err;
1337 }
1338
1339 /*
1340  * vpif_remove: It un-register channels from V4L2 driver
1341  */
1342 static int vpif_remove(struct platform_device *device)
1343 {
1344         struct common_obj *common;
1345         struct channel_obj *ch;
1346         int i;
1347
1348         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1349
1350         kfree(vpif_obj.sd);
1351         /* un-register device */
1352         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1353                 /* Get the pointer to the channel object */
1354                 ch = vpif_obj.dev[i];
1355                 common = &ch->common[VPIF_VIDEO_INDEX];
1356                 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1357                 /* Unregister video device */
1358                 video_unregister_device(&ch->video_dev);
1359                 kfree(vpif_obj.dev[i]);
1360         }
1361
1362         return 0;
1363 }
1364
1365 #ifdef CONFIG_PM_SLEEP
1366 static int vpif_suspend(struct device *dev)
1367 {
1368         struct common_obj *common;
1369         struct channel_obj *ch;
1370         int i;
1371
1372         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1373                 /* Get the pointer to the channel object */
1374                 ch = vpif_obj.dev[i];
1375                 common = &ch->common[VPIF_VIDEO_INDEX];
1376
1377                 if (!vb2_start_streaming_called(&common->buffer_queue))
1378                         continue;
1379
1380                 mutex_lock(&common->lock);
1381                 /* Disable channel */
1382                 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1383                         enable_channel2(0);
1384                         channel2_intr_enable(0);
1385                 }
1386                 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1387                         ycmux_mode == 2) {
1388                         enable_channel3(0);
1389                         channel3_intr_enable(0);
1390                 }
1391                 mutex_unlock(&common->lock);
1392         }
1393
1394         return 0;
1395 }
1396
1397 static int vpif_resume(struct device *dev)
1398 {
1399
1400         struct common_obj *common;
1401         struct channel_obj *ch;
1402         int i;
1403
1404         for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1405                 /* Get the pointer to the channel object */
1406                 ch = vpif_obj.dev[i];
1407                 common = &ch->common[VPIF_VIDEO_INDEX];
1408
1409                 if (!vb2_start_streaming_called(&common->buffer_queue))
1410                         continue;
1411
1412                 mutex_lock(&common->lock);
1413                 /* Enable channel */
1414                 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1415                         enable_channel2(1);
1416                         channel2_intr_enable(1);
1417                 }
1418                 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1419                                 ycmux_mode == 2) {
1420                         enable_channel3(1);
1421                         channel3_intr_enable(1);
1422                 }
1423                 mutex_unlock(&common->lock);
1424         }
1425
1426         return 0;
1427 }
1428
1429 #endif
1430
1431 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1432
1433 static __refdata struct platform_driver vpif_driver = {
1434         .driver = {
1435                         .name   = VPIF_DRIVER_NAME,
1436                         .pm     = &vpif_pm_ops,
1437         },
1438         .probe  = vpif_probe,
1439         .remove = vpif_remove,
1440 };
1441
1442 module_platform_driver(vpif_driver);