Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us