Version:  2.0.40 2.2.26 2.4.37 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 3.17 3.18

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

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