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

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

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