sched: Make task->start_time nanoseconds based
[sfrench/cifs-2.6.git] / drivers / media / platform / davinci / vpif_capture.c
1 /*
2  * Copyright (C) 2009 Texas Instruments Inc
3  * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  *
19  * TODO : add support for VBI & HBI data service
20  *        add static buffer allocation
21  */
22
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27
28 #include <media/v4l2-ioctl.h>
29
30 #include "vpif.h"
31 #include "vpif_capture.h"
32
33 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(VPIF_CAPTURE_VERSION);
36
37 #define vpif_err(fmt, arg...)   v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
38 #define vpif_dbg(level, debug, fmt, arg...)     \
39                 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
40
41 static int debug = 1;
42 static u32 ch0_numbuffers = 3;
43 static u32 ch1_numbuffers = 3;
44 static u32 ch0_bufsize = 1920 * 1080 * 2;
45 static u32 ch1_bufsize = 720 * 576 * 2;
46
47 module_param(debug, int, 0644);
48 module_param(ch0_numbuffers, uint, S_IRUGO);
49 module_param(ch1_numbuffers, uint, S_IRUGO);
50 module_param(ch0_bufsize, uint, S_IRUGO);
51 module_param(ch1_bufsize, uint, S_IRUGO);
52
53 MODULE_PARM_DESC(debug, "Debug level 0-1");
54 MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
55 MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
56 MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
57 MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
58
59 static struct vpif_config_params config_params = {
60         .min_numbuffers = 3,
61         .numbuffers[0] = 3,
62         .numbuffers[1] = 3,
63         .min_bufsize[0] = 720 * 480 * 2,
64         .min_bufsize[1] = 720 * 480 * 2,
65         .channel_bufsize[0] = 1920 * 1080 * 2,
66         .channel_bufsize[1] = 720 * 576 * 2,
67 };
68
69 #define VPIF_DRIVER_NAME        "vpif_capture"
70
71 /* global variables */
72 static struct vpif_device vpif_obj = { {NULL} };
73 static struct device *vpif_dev;
74 static void vpif_calculate_offsets(struct channel_obj *ch);
75 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
76
77 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
78
79 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
80 static int ycmux_mode;
81
82 static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
83 {
84         return container_of(vb, struct vpif_cap_buffer, vb);
85 }
86
87 /**
88  * vpif_buffer_prepare :  callback function for buffer prepare
89  * @vb: ptr to vb2_buffer
90  *
91  * This is the callback function for buffer prepare when vb2_qbuf()
92  * function is called. The buffer is prepared and user space virtual address
93  * or user address is converted into  physical address
94  */
95 static int vpif_buffer_prepare(struct vb2_buffer *vb)
96 {
97         struct vb2_queue *q = vb->vb2_queue;
98         struct channel_obj *ch = vb2_get_drv_priv(q);
99         struct common_obj *common;
100         unsigned long addr;
101
102         vpif_dbg(2, debug, "vpif_buffer_prepare\n");
103
104         common = &ch->common[VPIF_VIDEO_INDEX];
105
106         vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
107         if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
108                 return -EINVAL;
109
110         vb->v4l2_buf.field = common->fmt.fmt.pix.field;
111
112         addr = vb2_dma_contig_plane_dma_addr(vb, 0);
113         if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
114                 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
115                 !IS_ALIGNED((addr + common->ctop_off), 8) ||
116                 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
117                 vpif_dbg(1, debug, "offset is not aligned\n");
118                 return -EINVAL;
119         }
120
121         return 0;
122 }
123
124 /**
125  * vpif_buffer_queue_setup : Callback function for buffer setup.
126  * @vq: vb2_queue ptr
127  * @fmt: v4l2 format
128  * @nbuffers: ptr to number of buffers requested by application
129  * @nplanes:: contains number of distinct video planes needed to hold a frame
130  * @sizes[]: contains the size (in bytes) of each plane.
131  * @alloc_ctxs: ptr to allocation context
132  *
133  * This callback function is called when reqbuf() is called to adjust
134  * the buffer count and buffer size
135  */
136 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
137                                 const struct v4l2_format *fmt,
138                                 unsigned int *nbuffers, unsigned int *nplanes,
139                                 unsigned int sizes[], void *alloc_ctxs[])
140 {
141         struct channel_obj *ch = vb2_get_drv_priv(vq);
142         struct common_obj *common;
143
144         common = &ch->common[VPIF_VIDEO_INDEX];
145
146         vpif_dbg(2, debug, "vpif_buffer_setup\n");
147
148         if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
149                 return -EINVAL;
150
151         if (vq->num_buffers + *nbuffers < 3)
152                 *nbuffers = 3 - vq->num_buffers;
153
154         *nplanes = 1;
155         sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
156         alloc_ctxs[0] = common->alloc_ctx;
157
158         /* Calculate the offset for Y and C data in the buffer */
159         vpif_calculate_offsets(ch);
160
161         return 0;
162 }
163
164 /**
165  * vpif_buffer_queue : Callback function to add buffer to DMA queue
166  * @vb: ptr to vb2_buffer
167  */
168 static void vpif_buffer_queue(struct vb2_buffer *vb)
169 {
170         struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
171         struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
172         struct common_obj *common;
173         unsigned long flags;
174
175         common = &ch->common[VPIF_VIDEO_INDEX];
176
177         vpif_dbg(2, debug, "vpif_buffer_queue\n");
178
179         spin_lock_irqsave(&common->irqlock, flags);
180         /* add the buffer to the DMA queue */
181         list_add_tail(&buf->list, &common->dma_queue);
182         spin_unlock_irqrestore(&common->irqlock, flags);
183 }
184
185 /**
186  * vpif_start_streaming : Starts the DMA engine for streaming
187  * @vb: ptr to vb2_buffer
188  * @count: number of buffers
189  */
190 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
191 {
192         struct vpif_capture_config *vpif_config_data =
193                                         vpif_dev->platform_data;
194         struct channel_obj *ch = vb2_get_drv_priv(vq);
195         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
196         struct vpif_params *vpif = &ch->vpifparams;
197         struct vpif_cap_buffer *buf, *tmp;
198         unsigned long addr, flags;
199         int ret;
200
201         spin_lock_irqsave(&common->irqlock, flags);
202
203         /* Initialize field_id */
204         ch->field_id = 0;
205
206         /* configure 1 or 2 channel mode */
207         if (vpif_config_data->setup_input_channel_mode) {
208                 ret = vpif_config_data->
209                         setup_input_channel_mode(vpif->std_info.ycmux_mode);
210                 if (ret < 0) {
211                         vpif_dbg(1, debug, "can't set vpif channel mode\n");
212                         goto err;
213                 }
214         }
215
216         ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
217         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
218                 vpif_dbg(1, debug, "stream on failed in subdev\n");
219                 goto err;
220         }
221
222         /* Call vpif_set_params function to set the parameters and addresses */
223         ret = vpif_set_video_params(vpif, ch->channel_id);
224         if (ret < 0) {
225                 vpif_dbg(1, debug, "can't set video params\n");
226                 goto err;
227         }
228
229         ycmux_mode = ret;
230         vpif_config_addr(ch, ret);
231
232         /* Get the next frame from the buffer queue */
233         common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
234                                     struct vpif_cap_buffer, list);
235         /* Remove buffer from the buffer queue */
236         list_del(&common->cur_frm->list);
237         spin_unlock_irqrestore(&common->irqlock, flags);
238         /* Mark state of the current frame to active */
239         common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
240
241         addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
242
243         common->set_addr(addr + common->ytop_off,
244                          addr + common->ybtm_off,
245                          addr + common->ctop_off,
246                          addr + common->cbtm_off);
247
248         /**
249          * Set interrupt for both the fields in VPIF Register enable channel in
250          * VPIF register
251          */
252         channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
253         if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
254                 channel0_intr_assert();
255                 channel0_intr_enable(1);
256                 enable_channel0(1);
257         }
258         if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
259                 ycmux_mode == 2) {
260                 channel1_intr_assert();
261                 channel1_intr_enable(1);
262                 enable_channel1(1);
263         }
264
265         return 0;
266
267 err:
268         list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
269                 list_del(&buf->list);
270                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
271         }
272
273         return ret;
274 }
275
276 /**
277  * vpif_stop_streaming : Stop the DMA engine
278  * @vq: ptr to vb2_queue
279  *
280  * This callback stops the DMA engine and any remaining buffers
281  * in the DMA queue are released.
282  */
283 static void vpif_stop_streaming(struct vb2_queue *vq)
284 {
285         struct channel_obj *ch = vb2_get_drv_priv(vq);
286         struct common_obj *common;
287         unsigned long flags;
288         int ret;
289
290         common = &ch->common[VPIF_VIDEO_INDEX];
291
292         /* Disable channel as per its device type and channel id */
293         if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
294                 enable_channel0(0);
295                 channel0_intr_enable(0);
296         }
297         if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
298                 ycmux_mode == 2) {
299                 enable_channel1(0);
300                 channel1_intr_enable(0);
301         }
302
303         ycmux_mode = 0;
304
305         ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
306         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
307                 vpif_dbg(1, debug, "stream off failed in subdev\n");
308
309         /* release all active buffers */
310         spin_lock_irqsave(&common->irqlock, flags);
311         if (common->cur_frm == common->next_frm) {
312                 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
313         } else {
314                 if (common->cur_frm != NULL)
315                         vb2_buffer_done(&common->cur_frm->vb,
316                                         VB2_BUF_STATE_ERROR);
317                 if (common->next_frm != NULL)
318                         vb2_buffer_done(&common->next_frm->vb,
319                                         VB2_BUF_STATE_ERROR);
320         }
321
322         while (!list_empty(&common->dma_queue)) {
323                 common->next_frm = list_entry(common->dma_queue.next,
324                                                 struct vpif_cap_buffer, list);
325                 list_del(&common->next_frm->list);
326                 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
327         }
328         spin_unlock_irqrestore(&common->irqlock, flags);
329 }
330
331 static struct vb2_ops video_qops = {
332         .queue_setup            = vpif_buffer_queue_setup,
333         .buf_prepare            = vpif_buffer_prepare,
334         .start_streaming        = vpif_start_streaming,
335         .stop_streaming         = vpif_stop_streaming,
336         .buf_queue              = vpif_buffer_queue,
337 };
338
339 /**
340  * vpif_process_buffer_complete: process a completed buffer
341  * @common: ptr to common channel object
342  *
343  * This function time stamp the buffer and mark it as DONE. It also
344  * wake up any process waiting on the QUEUE and set the next buffer
345  * as current
346  */
347 static void vpif_process_buffer_complete(struct common_obj *common)
348 {
349         v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
350         vb2_buffer_done(&common->cur_frm->vb,
351                                             VB2_BUF_STATE_DONE);
352         /* Make curFrm pointing to nextFrm */
353         common->cur_frm = common->next_frm;
354 }
355
356 /**
357  * vpif_schedule_next_buffer: set next buffer address for capture
358  * @common : ptr to common channel object
359  *
360  * This function will get next buffer from the dma queue and
361  * set the buffer address in the vpif register for capture.
362  * the buffer is marked active
363  */
364 static void vpif_schedule_next_buffer(struct common_obj *common)
365 {
366         unsigned long addr = 0;
367
368         spin_lock(&common->irqlock);
369         common->next_frm = list_entry(common->dma_queue.next,
370                                      struct vpif_cap_buffer, list);
371         /* Remove that buffer from the buffer queue */
372         list_del(&common->next_frm->list);
373         spin_unlock(&common->irqlock);
374         common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
375         addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
376
377         /* Set top and bottom field addresses in VPIF registers */
378         common->set_addr(addr + common->ytop_off,
379                          addr + common->ybtm_off,
380                          addr + common->ctop_off,
381                          addr + common->cbtm_off);
382 }
383
384 /**
385  * vpif_channel_isr : ISR handler for vpif capture
386  * @irq: irq number
387  * @dev_id: dev_id ptr
388  *
389  * It changes status of the captured buffer, takes next buffer from the queue
390  * and sets its address in VPIF registers
391  */
392 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
393 {
394         struct vpif_device *dev = &vpif_obj;
395         struct common_obj *common;
396         struct channel_obj *ch;
397         enum v4l2_field field;
398         int channel_id = 0;
399         int fid = -1, i;
400
401         channel_id = *(int *)(dev_id);
402         if (!vpif_intr_status(channel_id))
403                 return IRQ_NONE;
404
405         ch = dev->dev[channel_id];
406
407         field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
408
409         for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
410                 common = &ch->common[i];
411                 /* skip If streaming is not started in this channel */
412                 /* Check the field format */
413                 if (1 == ch->vpifparams.std_info.frm_fmt) {
414                         /* Progressive mode */
415                         spin_lock(&common->irqlock);
416                         if (list_empty(&common->dma_queue)) {
417                                 spin_unlock(&common->irqlock);
418                                 continue;
419                         }
420                         spin_unlock(&common->irqlock);
421
422                         if (!channel_first_int[i][channel_id])
423                                 vpif_process_buffer_complete(common);
424
425                         channel_first_int[i][channel_id] = 0;
426
427                         vpif_schedule_next_buffer(common);
428
429
430                         channel_first_int[i][channel_id] = 0;
431                 } else {
432                         /**
433                          * Interlaced mode. If it is first interrupt, ignore
434                          * it
435                          */
436                         if (channel_first_int[i][channel_id]) {
437                                 channel_first_int[i][channel_id] = 0;
438                                 continue;
439                         }
440                         if (0 == i) {
441                                 ch->field_id ^= 1;
442                                 /* Get field id from VPIF registers */
443                                 fid = vpif_channel_getfid(ch->channel_id);
444                                 if (fid != ch->field_id) {
445                                         /**
446                                          * If field id does not match stored
447                                          * field id, make them in sync
448                                          */
449                                         if (0 == fid)
450                                                 ch->field_id = fid;
451                                         return IRQ_HANDLED;
452                                 }
453                         }
454                         /* device field id and local field id are in sync */
455                         if (0 == fid) {
456                                 /* this is even field */
457                                 if (common->cur_frm == common->next_frm)
458                                         continue;
459
460                                 /* mark the current buffer as done */
461                                 vpif_process_buffer_complete(common);
462                         } else if (1 == fid) {
463                                 /* odd field */
464                                 spin_lock(&common->irqlock);
465                                 if (list_empty(&common->dma_queue) ||
466                                     (common->cur_frm != common->next_frm)) {
467                                         spin_unlock(&common->irqlock);
468                                         continue;
469                                 }
470                                 spin_unlock(&common->irqlock);
471
472                                 vpif_schedule_next_buffer(common);
473                         }
474                 }
475         }
476         return IRQ_HANDLED;
477 }
478
479 /**
480  * vpif_update_std_info() - update standard related info
481  * @ch: ptr to channel object
482  *
483  * For a given standard selected by application, update values
484  * in the device data structures
485  */
486 static int vpif_update_std_info(struct channel_obj *ch)
487 {
488         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
489         struct vpif_params *vpifparams = &ch->vpifparams;
490         const struct vpif_channel_config_params *config;
491         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
492         struct video_obj *vid_ch = &ch->video;
493         int index;
494
495         vpif_dbg(2, debug, "vpif_update_std_info\n");
496
497         for (index = 0; index < vpif_ch_params_count; index++) {
498                 config = &vpif_ch_params[index];
499                 if (config->hd_sd == 0) {
500                         vpif_dbg(2, debug, "SD format\n");
501                         if (config->stdid & vid_ch->stdid) {
502                                 memcpy(std_info, config, sizeof(*config));
503                                 break;
504                         }
505                 } else {
506                         vpif_dbg(2, debug, "HD format\n");
507                         if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
508                                 sizeof(vid_ch->dv_timings))) {
509                                 memcpy(std_info, config, sizeof(*config));
510                                 break;
511                         }
512                 }
513         }
514
515         /* standard not found */
516         if (index == vpif_ch_params_count)
517                 return -EINVAL;
518
519         common->fmt.fmt.pix.width = std_info->width;
520         common->width = std_info->width;
521         common->fmt.fmt.pix.height = std_info->height;
522         common->height = std_info->height;
523         common->fmt.fmt.pix.bytesperline = std_info->width;
524         vpifparams->video_params.hpitch = std_info->width;
525         vpifparams->video_params.storage_mode = std_info->frm_fmt;
526
527         return 0;
528 }
529
530 /**
531  * vpif_calculate_offsets : This function calculates buffers offsets
532  * @ch : ptr to channel object
533  *
534  * This function calculates buffer offsets for Y and C in the top and
535  * bottom field
536  */
537 static void vpif_calculate_offsets(struct channel_obj *ch)
538 {
539         unsigned int hpitch, vpitch, sizeimage;
540         struct video_obj *vid_ch = &(ch->video);
541         struct vpif_params *vpifparams = &ch->vpifparams;
542         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
543         enum v4l2_field field = common->fmt.fmt.pix.field;
544
545         vpif_dbg(2, debug, "vpif_calculate_offsets\n");
546
547         if (V4L2_FIELD_ANY == field) {
548                 if (vpifparams->std_info.frm_fmt)
549                         vid_ch->buf_field = V4L2_FIELD_NONE;
550                 else
551                         vid_ch->buf_field = V4L2_FIELD_INTERLACED;
552         } else
553                 vid_ch->buf_field = common->fmt.fmt.pix.field;
554
555         sizeimage = common->fmt.fmt.pix.sizeimage;
556
557         hpitch = common->fmt.fmt.pix.bytesperline;
558         vpitch = sizeimage / (hpitch * 2);
559
560         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
561             (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
562                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
563                 common->ytop_off = 0;
564                 common->ybtm_off = hpitch;
565                 common->ctop_off = sizeimage / 2;
566                 common->cbtm_off = sizeimage / 2 + hpitch;
567         } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
568                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
569                 common->ytop_off = 0;
570                 common->ybtm_off = sizeimage / 4;
571                 common->ctop_off = sizeimage / 2;
572                 common->cbtm_off = common->ctop_off + sizeimage / 4;
573         } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
574                 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
575                 common->ybtm_off = 0;
576                 common->ytop_off = sizeimage / 4;
577                 common->cbtm_off = sizeimage / 2;
578                 common->ctop_off = common->cbtm_off + sizeimage / 4;
579         }
580         if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
581             (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
582                 vpifparams->video_params.storage_mode = 1;
583         else
584                 vpifparams->video_params.storage_mode = 0;
585
586         if (1 == vpifparams->std_info.frm_fmt)
587                 vpifparams->video_params.hpitch =
588                     common->fmt.fmt.pix.bytesperline;
589         else {
590                 if ((field == V4L2_FIELD_ANY)
591                     || (field == V4L2_FIELD_INTERLACED))
592                         vpifparams->video_params.hpitch =
593                             common->fmt.fmt.pix.bytesperline * 2;
594                 else
595                         vpifparams->video_params.hpitch =
596                             common->fmt.fmt.pix.bytesperline;
597         }
598
599         ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
600 }
601
602 /**
603  * vpif_config_format: configure default frame format in the device
604  * ch : ptr to channel object
605  */
606 static void vpif_config_format(struct channel_obj *ch)
607 {
608         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
609
610         vpif_dbg(2, debug, "vpif_config_format\n");
611
612         common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
613         common->fmt.fmt.pix.sizeimage
614             = config_params.channel_bufsize[ch->channel_id];
615
616         if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
617                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
618         else
619                 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
620         common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
621 }
622
623 /**
624  * vpif_get_default_field() - Get default field type based on interface
625  * @vpif_params - ptr to vpif params
626  */
627 static inline enum v4l2_field vpif_get_default_field(
628                                 struct vpif_interface *iface)
629 {
630         return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
631                                                 V4L2_FIELD_INTERLACED;
632 }
633
634 /**
635  * vpif_check_format()  - check given pixel format for compatibility
636  * @ch - channel  ptr
637  * @pixfmt - Given pixel format
638  * @update - update the values as per hardware requirement
639  *
640  * Check the application pixel format for S_FMT and update the input
641  * values as per hardware limits for TRY_FMT. The default pixel and
642  * field format is selected based on interface type.
643  */
644 static int vpif_check_format(struct channel_obj *ch,
645                              struct v4l2_pix_format *pixfmt,
646                              int update)
647 {
648         struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
649         struct vpif_params *vpif_params = &ch->vpifparams;
650         enum v4l2_field field = pixfmt->field;
651         u32 sizeimage, hpitch, vpitch;
652         int ret = -EINVAL;
653
654         vpif_dbg(2, debug, "vpif_check_format\n");
655         /**
656          * first check for the pixel format. If if_type is Raw bayer,
657          * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
658          * V4L2_PIX_FMT_YUV422P is supported
659          */
660         if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
661                 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
662                         if (!update) {
663                                 vpif_dbg(2, debug, "invalid pix format\n");
664                                 goto exit;
665                         }
666                         pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
667                 }
668         } else {
669                 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
670                         if (!update) {
671                                 vpif_dbg(2, debug, "invalid pixel format\n");
672                                 goto exit;
673                         }
674                         pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
675                 }
676         }
677
678         if (!(VPIF_VALID_FIELD(field))) {
679                 if (!update) {
680                         vpif_dbg(2, debug, "invalid field format\n");
681                         goto exit;
682                 }
683                 /**
684                  * By default use FIELD_NONE for RAW Bayer capture
685                  * and FIELD_INTERLACED for other interfaces
686                  */
687                 field = vpif_get_default_field(&vpif_params->iface);
688         } else if (field == V4L2_FIELD_ANY)
689                 /* unsupported field. Use default */
690                 field = vpif_get_default_field(&vpif_params->iface);
691
692         /* validate the hpitch */
693         hpitch = pixfmt->bytesperline;
694         if (hpitch < vpif_params->std_info.width) {
695                 if (!update) {
696                         vpif_dbg(2, debug, "invalid hpitch\n");
697                         goto exit;
698                 }
699                 hpitch = vpif_params->std_info.width;
700         }
701
702         sizeimage = pixfmt->sizeimage;
703
704         vpitch = sizeimage / (hpitch * 2);
705
706         /* validate the vpitch */
707         if (vpitch < vpif_params->std_info.height) {
708                 if (!update) {
709                         vpif_dbg(2, debug, "Invalid vpitch\n");
710                         goto exit;
711                 }
712                 vpitch = vpif_params->std_info.height;
713         }
714
715         /* Check for 8 byte alignment */
716         if (!ALIGN(hpitch, 8)) {
717                 if (!update) {
718                         vpif_dbg(2, debug, "invalid pitch alignment\n");
719                         goto exit;
720                 }
721                 /* adjust to next 8 byte boundary */
722                 hpitch = (((hpitch + 7) / 8) * 8);
723         }
724         /* if update is set, modify the bytesperline and sizeimage */
725         if (update) {
726                 pixfmt->bytesperline = hpitch;
727                 pixfmt->sizeimage = hpitch * vpitch * 2;
728         }
729         /**
730          * Image width and height is always based on current standard width and
731          * height
732          */
733         pixfmt->width = common->fmt.fmt.pix.width;
734         pixfmt->height = common->fmt.fmt.pix.height;
735         return 0;
736 exit:
737         return ret;
738 }
739
740 /**
741  * vpif_config_addr() - function to configure buffer address in vpif
742  * @ch - channel ptr
743  * @muxmode - channel mux mode
744  */
745 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
746 {
747         struct common_obj *common;
748
749         vpif_dbg(2, debug, "vpif_config_addr\n");
750
751         common = &(ch->common[VPIF_VIDEO_INDEX]);
752
753         if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
754                 common->set_addr = ch1_set_videobuf_addr;
755         else if (2 == muxmode)
756                 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
757         else
758                 common->set_addr = ch0_set_videobuf_addr;
759 }
760
761 /**
762  * vpif_input_to_subdev() - Maps input to sub device
763  * @vpif_cfg - global config ptr
764  * @chan_cfg - channel config ptr
765  * @input_index - Given input index from application
766  *
767  * lookup the sub device information for a given input index.
768  * we report all the inputs to application. inputs table also
769  * has sub device name for the each input
770  */
771 static int vpif_input_to_subdev(
772                 struct vpif_capture_config *vpif_cfg,
773                 struct vpif_capture_chan_config *chan_cfg,
774                 int input_index)
775 {
776         struct vpif_subdev_info *subdev_info;
777         const char *subdev_name;
778         int i;
779
780         vpif_dbg(2, debug, "vpif_input_to_subdev\n");
781
782         subdev_name = chan_cfg->inputs[input_index].subdev_name;
783         if (subdev_name == NULL)
784                 return -1;
785
786         /* loop through the sub device list to get the sub device info */
787         for (i = 0; i < vpif_cfg->subdev_count; i++) {
788                 subdev_info = &vpif_cfg->subdev_info[i];
789                 if (!strcmp(subdev_info->name, subdev_name))
790                         return i;
791         }
792         return -1;
793 }
794
795 /**
796  * vpif_set_input() - Select an input
797  * @vpif_cfg - global config ptr
798  * @ch - channel
799  * @_index - Given input index from application
800  *
801  * Select the given input.
802  */
803 static int vpif_set_input(
804                 struct vpif_capture_config *vpif_cfg,
805                 struct channel_obj *ch,
806                 int index)
807 {
808         struct vpif_capture_chan_config *chan_cfg =
809                         &vpif_cfg->chan_config[ch->channel_id];
810         struct vpif_subdev_info *subdev_info = NULL;
811         struct v4l2_subdev *sd = NULL;
812         u32 input = 0, output = 0;
813         int sd_index;
814         int ret;
815
816         sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
817         if (sd_index >= 0) {
818                 sd = vpif_obj.sd[sd_index];
819                 subdev_info = &vpif_cfg->subdev_info[sd_index];
820         }
821
822         /* first setup input path from sub device to vpif */
823         if (sd && vpif_cfg->setup_input_path) {
824                 ret = vpif_cfg->setup_input_path(ch->channel_id,
825                                        subdev_info->name);
826                 if (ret < 0) {
827                         vpif_dbg(1, debug, "couldn't setup input path for the" \
828                         " sub device %s, for input index %d\n",
829                         subdev_info->name, index);
830                         return ret;
831                 }
832         }
833
834         if (sd) {
835                 input = chan_cfg->inputs[index].input_route;
836                 output = chan_cfg->inputs[index].output_route;
837                 ret = v4l2_subdev_call(sd, video, s_routing,
838                                 input, output, 0);
839                 if (ret < 0 && ret != -ENOIOCTLCMD) {
840                         vpif_dbg(1, debug, "Failed to set input\n");
841                         return ret;
842                 }
843         }
844         ch->input_idx = index;
845         ch->sd = sd;
846         /* copy interface parameters to vpif */
847         ch->vpifparams.iface = chan_cfg->vpif_if;
848
849         /* update tvnorms from the sub device input info */
850         ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
851         return 0;
852 }
853
854 /**
855  * vpif_querystd() - querystd handler
856  * @file: file ptr
857  * @priv: file handle
858  * @std_id: ptr to std id
859  *
860  * This function is called to detect standard at the selected input
861  */
862 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
863 {
864         struct video_device *vdev = video_devdata(file);
865         struct channel_obj *ch = video_get_drvdata(vdev);
866         int ret = 0;
867
868         vpif_dbg(2, debug, "vpif_querystd\n");
869
870         /* Call querystd function of decoder device */
871         ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
872
873         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
874                 return -ENODATA;
875         if (ret) {
876                 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
877                 return ret;
878         }
879
880         return 0;
881 }
882
883 /**
884  * vpif_g_std() - get STD handler
885  * @file: file ptr
886  * @priv: file handle
887  * @std_id: ptr to std id
888  */
889 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
890 {
891         struct vpif_capture_config *config = vpif_dev->platform_data;
892         struct video_device *vdev = video_devdata(file);
893         struct channel_obj *ch = video_get_drvdata(vdev);
894         struct vpif_capture_chan_config *chan_cfg;
895         struct v4l2_input input;
896
897         vpif_dbg(2, debug, "vpif_g_std\n");
898
899         if (config->chan_config[ch->channel_id].inputs == NULL)
900                 return -ENODATA;
901
902         chan_cfg = &config->chan_config[ch->channel_id];
903         input = chan_cfg->inputs[ch->input_idx].input;
904         if (input.capabilities != V4L2_IN_CAP_STD)
905                 return -ENODATA;
906
907         *std = ch->video.stdid;
908         return 0;
909 }
910
911 /**
912  * vpif_s_std() - set STD handler
913  * @file: file ptr
914  * @priv: file handle
915  * @std_id: ptr to std id
916  */
917 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
918 {
919         struct vpif_capture_config *config = vpif_dev->platform_data;
920         struct video_device *vdev = video_devdata(file);
921         struct channel_obj *ch = video_get_drvdata(vdev);
922         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
923         struct vpif_capture_chan_config *chan_cfg;
924         struct v4l2_input input;
925         int ret;
926
927         vpif_dbg(2, debug, "vpif_s_std\n");
928
929         if (config->chan_config[ch->channel_id].inputs == NULL)
930                 return -ENODATA;
931
932         chan_cfg = &config->chan_config[ch->channel_id];
933         input = chan_cfg->inputs[ch->input_idx].input;
934         if (input.capabilities != V4L2_IN_CAP_STD)
935                 return -ENODATA;
936
937         if (vb2_is_busy(&common->buffer_queue))
938                 return -EBUSY;
939
940         /* Call encoder subdevice function to set the standard */
941         ch->video.stdid = std_id;
942         memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
943
944         /* Get the information about the standard */
945         if (vpif_update_std_info(ch)) {
946                 vpif_err("Error getting the standard info\n");
947                 return -EINVAL;
948         }
949
950         /* Configure the default format information */
951         vpif_config_format(ch);
952
953         /* set standard in the sub device */
954         ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
955         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
956                 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
957                 return ret;
958         }
959         return 0;
960 }
961
962 /**
963  * vpif_enum_input() - ENUMINPUT handler
964  * @file: file ptr
965  * @priv: file handle
966  * @input: ptr to input structure
967  */
968 static int vpif_enum_input(struct file *file, void *priv,
969                                 struct v4l2_input *input)
970 {
971
972         struct vpif_capture_config *config = vpif_dev->platform_data;
973         struct video_device *vdev = video_devdata(file);
974         struct channel_obj *ch = video_get_drvdata(vdev);
975         struct vpif_capture_chan_config *chan_cfg;
976
977         chan_cfg = &config->chan_config[ch->channel_id];
978
979         if (input->index >= chan_cfg->input_count) {
980                 vpif_dbg(1, debug, "Invalid input index\n");
981                 return -EINVAL;
982         }
983
984         memcpy(input, &chan_cfg->inputs[input->index].input,
985                 sizeof(*input));
986         return 0;
987 }
988
989 /**
990  * vpif_g_input() - Get INPUT handler
991  * @file: file ptr
992  * @priv: file handle
993  * @index: ptr to input index
994  */
995 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
996 {
997         struct video_device *vdev = video_devdata(file);
998         struct channel_obj *ch = video_get_drvdata(vdev);
999
1000         *index = ch->input_idx;
1001         return 0;
1002 }
1003
1004 /**
1005  * vpif_s_input() - Set INPUT handler
1006  * @file: file ptr
1007  * @priv: file handle
1008  * @index: input index
1009  */
1010 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1011 {
1012         struct vpif_capture_config *config = vpif_dev->platform_data;
1013         struct video_device *vdev = video_devdata(file);
1014         struct channel_obj *ch = video_get_drvdata(vdev);
1015         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1016         struct vpif_capture_chan_config *chan_cfg;
1017
1018         chan_cfg = &config->chan_config[ch->channel_id];
1019
1020         if (index >= chan_cfg->input_count)
1021                 return -EINVAL;
1022
1023         if (vb2_is_busy(&common->buffer_queue))
1024                 return -EBUSY;
1025
1026         return vpif_set_input(config, ch, index);
1027 }
1028
1029 /**
1030  * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1031  * @file: file ptr
1032  * @priv: file handle
1033  * @index: input index
1034  */
1035 static int vpif_enum_fmt_vid_cap(struct file *file, void  *priv,
1036                                         struct v4l2_fmtdesc *fmt)
1037 {
1038         struct video_device *vdev = video_devdata(file);
1039         struct channel_obj *ch = video_get_drvdata(vdev);
1040
1041         if (fmt->index != 0) {
1042                 vpif_dbg(1, debug, "Invalid format index\n");
1043                 return -EINVAL;
1044         }
1045
1046         /* Fill in the information about format */
1047         if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1048                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1049                 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1050                 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1051         } else {
1052                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1053                 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1054                 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1055         }
1056         return 0;
1057 }
1058
1059 /**
1060  * vpif_try_fmt_vid_cap() - TRY_FMT handler
1061  * @file: file ptr
1062  * @priv: file handle
1063  * @fmt: ptr to v4l2 format structure
1064  */
1065 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1066                                 struct v4l2_format *fmt)
1067 {
1068         struct video_device *vdev = video_devdata(file);
1069         struct channel_obj *ch = video_get_drvdata(vdev);
1070         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1071
1072         return vpif_check_format(ch, pixfmt, 1);
1073 }
1074
1075
1076 /**
1077  * vpif_g_fmt_vid_cap() - Set INPUT handler
1078  * @file: file ptr
1079  * @priv: file handle
1080  * @fmt: ptr to v4l2 format structure
1081  */
1082 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1083                                 struct v4l2_format *fmt)
1084 {
1085         struct video_device *vdev = video_devdata(file);
1086         struct channel_obj *ch = video_get_drvdata(vdev);
1087         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1088
1089         /* Check the validity of the buffer type */
1090         if (common->fmt.type != fmt->type)
1091                 return -EINVAL;
1092
1093         /* Fill in the information about format */
1094         *fmt = common->fmt;
1095         return 0;
1096 }
1097
1098 /**
1099  * vpif_s_fmt_vid_cap() - Set FMT handler
1100  * @file: file ptr
1101  * @priv: file handle
1102  * @fmt: ptr to v4l2 format structure
1103  */
1104 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1105                                 struct v4l2_format *fmt)
1106 {
1107         struct video_device *vdev = video_devdata(file);
1108         struct channel_obj *ch = video_get_drvdata(vdev);
1109         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1110         struct v4l2_pix_format *pixfmt;
1111         int ret = 0;
1112
1113         vpif_dbg(2, debug, "%s\n", __func__);
1114
1115         if (vb2_is_busy(&common->buffer_queue))
1116                 return -EBUSY;
1117
1118         pixfmt = &fmt->fmt.pix;
1119         /* Check for valid field format */
1120         ret = vpif_check_format(ch, pixfmt, 0);
1121
1122         if (ret)
1123                 return ret;
1124         /* store the format in the channel object */
1125         common->fmt = *fmt;
1126         return 0;
1127 }
1128
1129 /**
1130  * vpif_querycap() - QUERYCAP handler
1131  * @file: file ptr
1132  * @priv: file handle
1133  * @cap: ptr to v4l2_capability structure
1134  */
1135 static int vpif_querycap(struct file *file, void  *priv,
1136                                 struct v4l2_capability *cap)
1137 {
1138         struct vpif_capture_config *config = vpif_dev->platform_data;
1139
1140         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1141         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1142         strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
1143         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1144                  dev_name(vpif_dev));
1145         strlcpy(cap->card, config->card_name, sizeof(cap->card));
1146
1147         return 0;
1148 }
1149
1150 /**
1151  * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1152  * @file: file ptr
1153  * @priv: file handle
1154  * @timings: input timings
1155  */
1156 static int
1157 vpif_enum_dv_timings(struct file *file, void *priv,
1158                      struct v4l2_enum_dv_timings *timings)
1159 {
1160         struct vpif_capture_config *config = vpif_dev->platform_data;
1161         struct video_device *vdev = video_devdata(file);
1162         struct channel_obj *ch = video_get_drvdata(vdev);
1163         struct vpif_capture_chan_config *chan_cfg;
1164         struct v4l2_input input;
1165         int ret;
1166
1167         if (config->chan_config[ch->channel_id].inputs == NULL)
1168                 return -ENODATA;
1169
1170         chan_cfg = &config->chan_config[ch->channel_id];
1171         input = chan_cfg->inputs[ch->input_idx].input;
1172         if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1173                 return -ENODATA;
1174
1175         timings->pad = 0;
1176
1177         ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
1178         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1179                 return -EINVAL;
1180
1181         return ret;
1182 }
1183
1184 /**
1185  * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1186  * @file: file ptr
1187  * @priv: file handle
1188  * @timings: input timings
1189  */
1190 static int
1191 vpif_query_dv_timings(struct file *file, void *priv,
1192                       struct v4l2_dv_timings *timings)
1193 {
1194         struct vpif_capture_config *config = vpif_dev->platform_data;
1195         struct video_device *vdev = video_devdata(file);
1196         struct channel_obj *ch = video_get_drvdata(vdev);
1197         struct vpif_capture_chan_config *chan_cfg;
1198         struct v4l2_input input;
1199         int ret;
1200
1201         if (config->chan_config[ch->channel_id].inputs == NULL)
1202                 return -ENODATA;
1203
1204         chan_cfg = &config->chan_config[ch->channel_id];
1205         input = chan_cfg->inputs[ch->input_idx].input;
1206         if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1207                 return -ENODATA;
1208
1209         ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1210         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1211                 return -ENODATA;
1212
1213         return ret;
1214 }
1215
1216 /**
1217  * vpif_s_dv_timings() - S_DV_TIMINGS handler
1218  * @file: file ptr
1219  * @priv: file handle
1220  * @timings: digital video timings
1221  */
1222 static int vpif_s_dv_timings(struct file *file, void *priv,
1223                 struct v4l2_dv_timings *timings)
1224 {
1225         struct vpif_capture_config *config = vpif_dev->platform_data;
1226         struct video_device *vdev = video_devdata(file);
1227         struct channel_obj *ch = video_get_drvdata(vdev);
1228         struct vpif_params *vpifparams = &ch->vpifparams;
1229         struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1230         struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1231         struct video_obj *vid_ch = &ch->video;
1232         struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1233         struct vpif_capture_chan_config *chan_cfg;
1234         struct v4l2_input input;
1235         int ret;
1236
1237         if (config->chan_config[ch->channel_id].inputs == NULL)
1238                 return -ENODATA;
1239
1240         chan_cfg = &config->chan_config[ch->channel_id];
1241         input = chan_cfg->inputs[ch->input_idx].input;
1242         if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1243                 return -ENODATA;
1244
1245         if (timings->type != V4L2_DV_BT_656_1120) {
1246                 vpif_dbg(2, debug, "Timing type not defined\n");
1247                 return -EINVAL;
1248         }
1249
1250         if (vb2_is_busy(&common->buffer_queue))
1251                 return -EBUSY;
1252
1253         /* Configure subdevice timings, if any */
1254         ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1255         if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1256                 ret = 0;
1257         if (ret < 0) {
1258                 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1259                 return ret;
1260         }
1261
1262         if (!(timings->bt.width && timings->bt.height &&
1263                                 (timings->bt.hbackporch ||
1264                                  timings->bt.hfrontporch ||
1265                                  timings->bt.hsync) &&
1266                                 timings->bt.vfrontporch &&
1267                                 (timings->bt.vbackporch ||
1268                                  timings->bt.vsync))) {
1269                 vpif_dbg(2, debug, "Timings for width, height, "
1270                                 "horizontal back porch, horizontal sync, "
1271                                 "horizontal front porch, vertical back porch, "
1272                                 "vertical sync and vertical back porch "
1273                                 "must be defined\n");
1274                 return -EINVAL;
1275         }
1276
1277         vid_ch->dv_timings = *timings;
1278
1279         /* Configure video port timings */
1280
1281         std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1282         std_info->sav2eav = bt->width;
1283
1284         std_info->l1 = 1;
1285         std_info->l3 = bt->vsync + bt->vbackporch + 1;
1286
1287         std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1288         if (bt->interlaced) {
1289                 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1290                         std_info->l5 = std_info->vsize/2 -
1291                                 (bt->vfrontporch - 1);
1292                         std_info->l7 = std_info->vsize/2 + 1;
1293                         std_info->l9 = std_info->l7 + bt->il_vsync +
1294                                 bt->il_vbackporch + 1;
1295                         std_info->l11 = std_info->vsize -
1296                                 (bt->il_vfrontporch - 1);
1297                 } else {
1298                         vpif_dbg(2, debug, "Required timing values for "
1299                                         "interlaced BT format missing\n");
1300                         return -EINVAL;
1301                 }
1302         } else {
1303                 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1304         }
1305         strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1306         std_info->width = bt->width;
1307         std_info->height = bt->height;
1308         std_info->frm_fmt = bt->interlaced ? 0 : 1;
1309         std_info->ycmux_mode = 0;
1310         std_info->capture_format = 0;
1311         std_info->vbi_supported = 0;
1312         std_info->hd_sd = 1;
1313         std_info->stdid = 0;
1314
1315         vid_ch->stdid = 0;
1316         return 0;
1317 }
1318
1319 /**
1320  * vpif_g_dv_timings() - G_DV_TIMINGS handler
1321  * @file: file ptr
1322  * @priv: file handle
1323  * @timings: digital video timings
1324  */
1325 static int vpif_g_dv_timings(struct file *file, void *priv,
1326                 struct v4l2_dv_timings *timings)
1327 {
1328         struct vpif_capture_config *config = vpif_dev->platform_data;
1329         struct video_device *vdev = video_devdata(file);
1330         struct channel_obj *ch = video_get_drvdata(vdev);
1331         struct video_obj *vid_ch = &ch->video;
1332         struct vpif_capture_chan_config *chan_cfg;
1333         struct v4l2_input input;
1334
1335         if (config->chan_config[ch->channel_id].inputs == NULL)
1336                 return -ENODATA;
1337
1338         chan_cfg = &config->chan_config[ch->channel_id];
1339         input = chan_cfg->inputs[ch->input_idx].input;
1340         if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1341                 return -ENODATA;
1342
1343         *timings = vid_ch->dv_timings;
1344
1345         return 0;
1346 }
1347
1348 /*
1349  * vpif_log_status() - Status information
1350  * @file: file ptr
1351  * @priv: file handle
1352  *
1353  * Returns zero.
1354  */
1355 static int vpif_log_status(struct file *filep, void *priv)
1356 {
1357         /* status for sub devices */
1358         v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1359
1360         return 0;
1361 }
1362
1363 /* vpif capture ioctl operations */
1364 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1365         .vidioc_querycap                = vpif_querycap,
1366         .vidioc_enum_fmt_vid_cap        = vpif_enum_fmt_vid_cap,
1367         .vidioc_g_fmt_vid_cap           = vpif_g_fmt_vid_cap,
1368         .vidioc_s_fmt_vid_cap           = vpif_s_fmt_vid_cap,
1369         .vidioc_try_fmt_vid_cap         = vpif_try_fmt_vid_cap,
1370
1371         .vidioc_enum_input              = vpif_enum_input,
1372         .vidioc_s_input                 = vpif_s_input,
1373         .vidioc_g_input                 = vpif_g_input,
1374
1375         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
1376         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
1377         .vidioc_querybuf                = vb2_ioctl_querybuf,
1378         .vidioc_qbuf                    = vb2_ioctl_qbuf,
1379         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
1380         .vidioc_expbuf                  = vb2_ioctl_expbuf,
1381         .vidioc_streamon                = vb2_ioctl_streamon,
1382         .vidioc_streamoff               = vb2_ioctl_streamoff,
1383
1384         .vidioc_querystd                = vpif_querystd,
1385         .vidioc_s_std                   = vpif_s_std,
1386         .vidioc_g_std                   = vpif_g_std,
1387
1388         .vidioc_enum_dv_timings         = vpif_enum_dv_timings,
1389         .vidioc_query_dv_timings        = vpif_query_dv_timings,
1390         .vidioc_s_dv_timings            = vpif_s_dv_timings,
1391         .vidioc_g_dv_timings            = vpif_g_dv_timings,
1392
1393         .vidioc_log_status              = vpif_log_status,
1394 };
1395
1396 /* vpif file operations */
1397 static struct v4l2_file_operations vpif_fops = {
1398         .owner = THIS_MODULE,
1399         .open = v4l2_fh_open,
1400         .release = vb2_fop_release,
1401         .unlocked_ioctl = video_ioctl2,
1402         .mmap = vb2_fop_mmap,
1403         .poll = vb2_fop_poll
1404 };
1405
1406 /**
1407  * initialize_vpif() - Initialize vpif data structures
1408  *
1409  * Allocate memory for data structures and initialize them
1410  */
1411 static int initialize_vpif(void)
1412 {
1413         int err = 0, i, j;
1414         int free_channel_objects_index;
1415
1416         /* Default number of buffers should be 3 */
1417         if ((ch0_numbuffers > 0) &&
1418             (ch0_numbuffers < config_params.min_numbuffers))
1419                 ch0_numbuffers = config_params.min_numbuffers;
1420         if ((ch1_numbuffers > 0) &&
1421             (ch1_numbuffers < config_params.min_numbuffers))
1422                 ch1_numbuffers = config_params.min_numbuffers;
1423
1424         /* Set buffer size to min buffers size if it is invalid */
1425         if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1426                 ch0_bufsize =
1427                     config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1428         if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1429                 ch1_bufsize =
1430                     config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1431
1432         config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1433         config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1434         if (ch0_numbuffers) {
1435                 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1436                     = ch0_bufsize;
1437         }
1438         if (ch1_numbuffers) {
1439                 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1440                     = ch1_bufsize;
1441         }
1442
1443         /* Allocate memory for six channel objects */
1444         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1445                 vpif_obj.dev[i] =
1446                     kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1447                 /* If memory allocation fails, return error */
1448                 if (!vpif_obj.dev[i]) {
1449                         free_channel_objects_index = i;
1450                         err = -ENOMEM;
1451                         goto vpif_init_free_channel_objects;
1452                 }
1453         }
1454         return 0;
1455
1456 vpif_init_free_channel_objects:
1457         for (j = 0; j < free_channel_objects_index; j++)
1458                 kfree(vpif_obj.dev[j]);
1459         return err;
1460 }
1461
1462 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1463                             struct v4l2_subdev *subdev,
1464                             struct v4l2_async_subdev *asd)
1465 {
1466         int i;
1467
1468         for (i = 0; i < vpif_obj.config->subdev_count; i++)
1469                 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1470                             subdev->name)) {
1471                         vpif_obj.sd[i] = subdev;
1472                         return 0;
1473                 }
1474
1475         return -EINVAL;
1476 }
1477
1478 static int vpif_probe_complete(void)
1479 {
1480         struct common_obj *common;
1481         struct video_device *vdev;
1482         struct channel_obj *ch;
1483         struct vb2_queue *q;
1484         int i, j, err, k;
1485
1486         for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1487                 ch = vpif_obj.dev[j];
1488                 ch->channel_id = j;
1489                 common = &(ch->common[VPIF_VIDEO_INDEX]);
1490                 spin_lock_init(&common->irqlock);
1491                 mutex_init(&common->lock);
1492
1493                 /* select input 0 */
1494                 err = vpif_set_input(vpif_obj.config, ch, 0);
1495                 if (err)
1496                         goto probe_out;
1497
1498                 /* Initialize vb2 queue */
1499                 q = &common->buffer_queue;
1500                 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1501                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1502                 q->drv_priv = ch;
1503                 q->ops = &video_qops;
1504                 q->mem_ops = &vb2_dma_contig_memops;
1505                 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1506                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1507                 q->min_buffers_needed = 1;
1508                 q->lock = &common->lock;
1509
1510                 err = vb2_queue_init(q);
1511                 if (err) {
1512                         vpif_err("vpif_capture: vb2_queue_init() failed\n");
1513                         goto probe_out;
1514                 }
1515
1516                 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1517                 if (IS_ERR(common->alloc_ctx)) {
1518                         vpif_err("Failed to get the context\n");
1519                         err = PTR_ERR(common->alloc_ctx);
1520                         goto probe_out;
1521                 }
1522
1523                 INIT_LIST_HEAD(&common->dma_queue);
1524
1525                 /* Initialize the video_device structure */
1526                 vdev = ch->video_dev;
1527                 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1528                 vdev->release = video_device_release;
1529                 vdev->fops = &vpif_fops;
1530                 vdev->ioctl_ops = &vpif_ioctl_ops;
1531                 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1532                 vdev->vfl_dir = VFL_DIR_RX;
1533                 vdev->queue = q;
1534                 vdev->lock = &common->lock;
1535                 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1536                 video_set_drvdata(ch->video_dev, ch);
1537                 err = video_register_device(vdev,
1538                                             VFL_TYPE_GRABBER, (j ? 1 : 0));
1539                 if (err)
1540                         goto probe_out;
1541         }
1542
1543         v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1544         return 0;
1545
1546 probe_out:
1547         for (k = 0; k < j; k++) {
1548                 /* Get the pointer to the channel object */
1549                 ch = vpif_obj.dev[k];
1550                 common = &ch->common[k];
1551                 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1552                 /* Unregister video device */
1553                 video_unregister_device(ch->video_dev);
1554         }
1555         kfree(vpif_obj.sd);
1556         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1557                 ch = vpif_obj.dev[i];
1558                 /* Note: does nothing if ch->video_dev == NULL */
1559                 video_device_release(ch->video_dev);
1560         }
1561         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1562
1563         return err;
1564 }
1565
1566 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1567 {
1568         return vpif_probe_complete();
1569 }
1570
1571 /**
1572  * vpif_probe : This function probes the vpif capture driver
1573  * @pdev: platform device pointer
1574  *
1575  * This creates device entries by register itself to the V4L2 driver and
1576  * initializes fields of each channel objects
1577  */
1578 static __init int vpif_probe(struct platform_device *pdev)
1579 {
1580         struct vpif_subdev_info *subdevdata;
1581         int i, j, err;
1582         int res_idx = 0;
1583         struct i2c_adapter *i2c_adap;
1584         struct channel_obj *ch;
1585         struct video_device *vfd;
1586         struct resource *res;
1587         int subdev_count;
1588
1589         vpif_dev = &pdev->dev;
1590
1591         err = initialize_vpif();
1592         if (err) {
1593                 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1594                 return err;
1595         }
1596
1597         err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1598         if (err) {
1599                 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1600                 return err;
1601         }
1602
1603         while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1604                 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1605                                         IRQF_SHARED, VPIF_DRIVER_NAME,
1606                                         (void *)(&vpif_obj.dev[res_idx]->
1607                                         channel_id));
1608                 if (err) {
1609                         err = -EINVAL;
1610                         goto vpif_unregister;
1611                 }
1612                 res_idx++;
1613         }
1614
1615         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1616                 /* Get the pointer to the channel object */
1617                 ch = vpif_obj.dev[i];
1618                 /* Allocate memory for video device */
1619                 vfd = video_device_alloc();
1620                 if (NULL == vfd) {
1621                         for (j = 0; j < i; j++) {
1622                                 ch = vpif_obj.dev[j];
1623                                 video_device_release(ch->video_dev);
1624                         }
1625                         err = -ENOMEM;
1626                         goto vpif_unregister;
1627                 }
1628
1629                 /* Set video_dev to the video device */
1630                 ch->video_dev = vfd;
1631         }
1632
1633         vpif_obj.config = pdev->dev.platform_data;
1634
1635         subdev_count = vpif_obj.config->subdev_count;
1636         vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1637                                 GFP_KERNEL);
1638         if (vpif_obj.sd == NULL) {
1639                 vpif_err("unable to allocate memory for subdevice pointers\n");
1640                 err = -ENOMEM;
1641                 goto vpif_sd_error;
1642         }
1643
1644         if (!vpif_obj.config->asd_sizes) {
1645                 i2c_adap = i2c_get_adapter(1);
1646                 for (i = 0; i < subdev_count; i++) {
1647                         subdevdata = &vpif_obj.config->subdev_info[i];
1648                         vpif_obj.sd[i] =
1649                                 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1650                                                           i2c_adap,
1651                                                           &subdevdata->
1652                                                           board_info,
1653                                                           NULL);
1654
1655                         if (!vpif_obj.sd[i]) {
1656                                 vpif_err("Error registering v4l2 subdevice\n");
1657                                 err = -ENODEV;
1658                                 goto probe_subdev_out;
1659                         }
1660                         v4l2_info(&vpif_obj.v4l2_dev,
1661                                   "registered sub device %s\n",
1662                                    subdevdata->name);
1663                 }
1664                 vpif_probe_complete();
1665         } else {
1666                 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1667                 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1668                 vpif_obj.notifier.bound = vpif_async_bound;
1669                 vpif_obj.notifier.complete = vpif_async_complete;
1670                 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1671                                                    &vpif_obj.notifier);
1672                 if (err) {
1673                         vpif_err("Error registering async notifier\n");
1674                         err = -EINVAL;
1675                         goto probe_subdev_out;
1676                 }
1677         }
1678
1679         return 0;
1680
1681 probe_subdev_out:
1682         /* free sub devices memory */
1683         kfree(vpif_obj.sd);
1684
1685 vpif_sd_error:
1686         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1687                 ch = vpif_obj.dev[i];
1688                 /* Note: does nothing if ch->video_dev == NULL */
1689                 video_device_release(ch->video_dev);
1690         }
1691 vpif_unregister:
1692         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1693
1694         return err;
1695 }
1696
1697 /**
1698  * vpif_remove() - driver remove handler
1699  * @device: ptr to platform device structure
1700  *
1701  * The vidoe device is unregistered
1702  */
1703 static int vpif_remove(struct platform_device *device)
1704 {
1705         struct common_obj *common;
1706         struct channel_obj *ch;
1707         int i;
1708
1709         v4l2_device_unregister(&vpif_obj.v4l2_dev);
1710
1711         kfree(vpif_obj.sd);
1712         /* un-register device */
1713         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1714                 /* Get the pointer to the channel object */
1715                 ch = vpif_obj.dev[i];
1716                 common = &ch->common[i];
1717                 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1718                 /* Unregister video device */
1719                 video_unregister_device(ch->video_dev);
1720                 kfree(vpif_obj.dev[i]);
1721         }
1722         return 0;
1723 }
1724
1725 #ifdef CONFIG_PM_SLEEP
1726 /**
1727  * vpif_suspend: vpif device suspend
1728  */
1729 static int vpif_suspend(struct device *dev)
1730 {
1731
1732         struct common_obj *common;
1733         struct channel_obj *ch;
1734         int i;
1735
1736         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1737                 /* Get the pointer to the channel object */
1738                 ch = vpif_obj.dev[i];
1739                 common = &ch->common[VPIF_VIDEO_INDEX];
1740
1741                 if (!vb2_is_streaming(&common->buffer_queue))
1742                         continue;
1743
1744                 mutex_lock(&common->lock);
1745                 /* Disable channel */
1746                 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1747                         enable_channel0(0);
1748                         channel0_intr_enable(0);
1749                 }
1750                 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1751                         ycmux_mode == 2) {
1752                         enable_channel1(0);
1753                         channel1_intr_enable(0);
1754                 }
1755                 mutex_unlock(&common->lock);
1756         }
1757
1758         return 0;
1759 }
1760
1761 /*
1762  * vpif_resume: vpif device suspend
1763  */
1764 static int vpif_resume(struct device *dev)
1765 {
1766         struct common_obj *common;
1767         struct channel_obj *ch;
1768         int i;
1769
1770         for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1771                 /* Get the pointer to the channel object */
1772                 ch = vpif_obj.dev[i];
1773                 common = &ch->common[VPIF_VIDEO_INDEX];
1774
1775                 if (!vb2_is_streaming(&common->buffer_queue))
1776                         continue;
1777
1778                 mutex_lock(&common->lock);
1779                 /* Enable channel */
1780                 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1781                         enable_channel0(1);
1782                         channel0_intr_enable(1);
1783                 }
1784                 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1785                         ycmux_mode == 2) {
1786                         enable_channel1(1);
1787                         channel1_intr_enable(1);
1788                 }
1789                 mutex_unlock(&common->lock);
1790         }
1791
1792         return 0;
1793 }
1794 #endif
1795
1796 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1797
1798 static __refdata struct platform_driver vpif_driver = {
1799         .driver = {
1800                 .name   = VPIF_DRIVER_NAME,
1801                 .owner  = THIS_MODULE,
1802                 .pm     = &vpif_pm_ops,
1803         },
1804         .probe = vpif_probe,
1805         .remove = vpif_remove,
1806 };
1807
1808 module_platform_driver(vpif_driver);