blob: a409ccefb38073ec2c67d74d96ea53079bc6f38a [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
20 */
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
28#include <linux/workqueue.h>
29#include <linux/string.h>
30#include <linux/videodev2.h>
31#include <linux/wait.h>
32#include <linux/time.h>
33#include <linux/i2c.h>
34#include <linux/platform_device.h>
35#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030037#include <media/v4l2-device.h>
38#include <media/v4l2-ioctl.h>
Mats Randgaard7036d6a2010-12-16 12:17:41 -030039#include <media/v4l2-chip-ident.h>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030040
41#include "vpif_capture.h"
42#include "vpif.h"
43
44MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030046MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030047
48#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
49#define vpif_dbg(level, debug, fmt, arg...) \
50 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
51
52static int debug = 1;
53static u32 ch0_numbuffers = 3;
54static u32 ch1_numbuffers = 3;
55static u32 ch0_bufsize = 1920 * 1080 * 2;
56static u32 ch1_bufsize = 720 * 576 * 2;
57
58module_param(debug, int, 0644);
59module_param(ch0_numbuffers, uint, S_IRUGO);
60module_param(ch1_numbuffers, uint, S_IRUGO);
61module_param(ch0_bufsize, uint, S_IRUGO);
62module_param(ch1_bufsize, uint, S_IRUGO);
63
64MODULE_PARM_DESC(debug, "Debug level 0-1");
65MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
66MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
67MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
68MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
69
70static struct vpif_config_params config_params = {
71 .min_numbuffers = 3,
72 .numbuffers[0] = 3,
73 .numbuffers[1] = 3,
74 .min_bufsize[0] = 720 * 480 * 2,
75 .min_bufsize[1] = 720 * 480 * 2,
76 .channel_bufsize[0] = 1920 * 1080 * 2,
77 .channel_bufsize[1] = 720 * 576 * 2,
78};
79
80/* global variables */
81static struct vpif_device vpif_obj = { {NULL} };
82static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030083static void vpif_calculate_offsets(struct channel_obj *ch);
84static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030085
86/**
87 * buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030088 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030089 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030090 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030091 * function is called. The buffer is prepared and user space virtual address
92 * or user address is converted into physical address
93 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030094static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030095{
96 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030097 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
98 struct vb2_queue *q = vb->vb2_queue;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030099 struct channel_obj *ch = fh->channel;
100 struct common_obj *common;
101 unsigned long addr;
102
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300103 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
104
105 common = &ch->common[VPIF_VIDEO_INDEX];
106
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300107 if (vb->state != VB2_BUF_STATE_ACTIVE &&
108 vb->state != VB2_BUF_STATE_PREPARED) {
109 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
110 if (vb2_plane_vaddr(vb, 0) &&
111 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
112 goto exit;
113 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300114
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300115 if (q->streaming) {
116 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
117 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
118 !IS_ALIGNED((addr + common->ctop_off), 8) ||
119 !IS_ALIGNED((addr + common->cbtm_off), 8))
120 goto exit;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300121 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300122 }
123 return 0;
124exit:
125 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
126 return -EINVAL;
127}
128
129/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300130 * vpif_buffer_queue_setup : Callback function for buffer setup.
131 * @vq: vb2_queue ptr
132 * @fmt: v4l2 format
133 * @nbuffers: ptr to number of buffers requested by application
134 * @nplanes:: contains number of distinct video planes needed to hold a frame
135 * @sizes[]: contains the size (in bytes) of each plane.
136 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300137 *
138 * This callback function is called when reqbuf() is called to adjust
139 * the buffer count and buffer size
140 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300141static int vpif_buffer_queue_setup(struct vb2_queue *vq,
142 const struct v4l2_format *fmt,
143 unsigned int *nbuffers, unsigned int *nplanes,
144 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300145{
146 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300147 struct vpif_fh *fh = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300148 struct channel_obj *ch = fh->channel;
149 struct common_obj *common;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300150 unsigned long size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300151
152 common = &ch->common[VPIF_VIDEO_INDEX];
153
154 vpif_dbg(2, debug, "vpif_buffer_setup\n");
155
156 /* If memory type is not mmap, return */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300157 if (V4L2_MEMORY_MMAP == common->memory) {
158 /* Calculate the size of the buffer */
159 size = config_params.channel_bufsize[ch->channel_id];
160 /*
161 * Checking if the buffer size exceeds the available buffer
162 * ycmux_mode = 0 means 1 channel mode HD and
163 * ycmux_mode = 1 means 2 channels mode SD
164 */
165 if (ch->vpifparams.std_info.ycmux_mode == 0) {
166 if (config_params.video_limit[ch->channel_id])
167 while (size * *nbuffers >
168 (config_params.video_limit[0]
Manjunath Hadli764af392012-04-13 04:49:34 -0300169 + config_params.video_limit[1]))
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300170 (*nbuffers)--;
171 } else {
172 if (config_params.video_limit[ch->channel_id])
173 while (size * *nbuffers >
Manjunath Hadli764af392012-04-13 04:49:34 -0300174 config_params.video_limit[ch->channel_id])
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300175 (*nbuffers)--;
176 }
177
178 } else {
179 size = common->fmt.fmt.pix.sizeimage;
Manjunath Hadli764af392012-04-13 04:49:34 -0300180 }
181
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300182 if (*nbuffers < config_params.min_numbuffers)
183 *nbuffers = config_params.min_numbuffers;
184
185 *nplanes = 1;
186 sizes[0] = size;
187 alloc_ctxs[0] = common->alloc_ctx;
188
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300189 return 0;
190}
191
192/**
193 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300194 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300195 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300196static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300197{
198 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300199 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300200 struct channel_obj *ch = fh->channel;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300201 struct vpif_cap_buffer *buf = container_of(vb,
202 struct vpif_cap_buffer, vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300203 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300204 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300205
206 common = &ch->common[VPIF_VIDEO_INDEX];
207
208 vpif_dbg(2, debug, "vpif_buffer_queue\n");
209
Hans Verkuilaec96832012-11-16 12:03:06 -0300210 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300211 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300212 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300213 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300214}
215
216/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300217 * vpif_buf_cleanup : Callback function to free buffer
218 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300219 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300220 * This function is called from the videobuf2 layer to free memory
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300221 * allocated to the buffers
222 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300223static void vpif_buf_cleanup(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300224{
225 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300226 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
227 struct vpif_cap_buffer *buf = container_of(vb,
228 struct vpif_cap_buffer, vb);
229 struct channel_obj *ch = fh->channel;
230 struct common_obj *common;
231 unsigned long flags;
232
233 common = &ch->common[VPIF_VIDEO_INDEX];
234
235 spin_lock_irqsave(&common->irqlock, flags);
236 if (vb->state == VB2_BUF_STATE_ACTIVE)
237 list_del_init(&buf->list);
238 spin_unlock_irqrestore(&common->irqlock, flags);
239
240}
241
242static void vpif_wait_prepare(struct vb2_queue *vq)
243{
244 struct vpif_fh *fh = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300245 struct channel_obj *ch = fh->channel;
246 struct common_obj *common;
247
248 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300249 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300250}
251
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300252static void vpif_wait_finish(struct vb2_queue *vq)
253{
254 struct vpif_fh *fh = vb2_get_drv_priv(vq);
255 struct channel_obj *ch = fh->channel;
256 struct common_obj *common;
257
258 common = &ch->common[VPIF_VIDEO_INDEX];
259 mutex_lock(&common->lock);
260}
261
262static int vpif_buffer_init(struct vb2_buffer *vb)
263{
264 struct vpif_cap_buffer *buf = container_of(vb,
265 struct vpif_cap_buffer, vb);
266
267 INIT_LIST_HEAD(&buf->list);
268
269 return 0;
270}
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300271
272static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
273 { {1, 1} };
274
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300275static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
276{
277 struct vpif_capture_config *vpif_config_data =
278 vpif_dev->platform_data;
279 struct vpif_fh *fh = vb2_get_drv_priv(vq);
280 struct channel_obj *ch = fh->channel;
281 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
282 struct vpif_params *vpif = &ch->vpifparams;
283 unsigned long addr = 0;
Hans Verkuilaec96832012-11-16 12:03:06 -0300284 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300285 int ret;
286
Hans Verkuilaec96832012-11-16 12:03:06 -0300287 /* If buffer queue is empty, return error */
288 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300289 if (list_empty(&common->dma_queue)) {
Hans Verkuilaec96832012-11-16 12:03:06 -0300290 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300291 vpif_dbg(1, debug, "buffer queue is empty\n");
292 return -EIO;
293 }
294
295 /* Get the next frame from the buffer queue */
296 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
297 struct vpif_cap_buffer, list);
298 /* Remove buffer from the buffer queue */
299 list_del(&common->cur_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300300 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300301 /* Mark state of the current frame to active */
302 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
303 /* Initialize field_id and started member */
304 ch->field_id = 0;
305 common->started = 1;
306 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
307
308 /* Calculate the offset for Y and C data in the buffer */
309 vpif_calculate_offsets(ch);
310
311 if ((vpif->std_info.frm_fmt &&
312 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
313 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
314 (!vpif->std_info.frm_fmt &&
315 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
316 vpif_dbg(1, debug, "conflict in field format and std format\n");
317 return -EINVAL;
318 }
319
320 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300321 if (vpif_config_data->setup_input_channel_mode) {
322 ret = vpif_config_data->
323 setup_input_channel_mode(vpif->std_info.ycmux_mode);
324 if (ret < 0) {
325 vpif_dbg(1, debug, "can't set vpif channel mode\n");
326 return ret;
327 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300328 }
329
330 /* Call vpif_set_params function to set the parameters and addresses */
331 ret = vpif_set_video_params(vpif, ch->channel_id);
332
333 if (ret < 0) {
334 vpif_dbg(1, debug, "can't set video params\n");
335 return ret;
336 }
337
338 common->started = ret;
339 vpif_config_addr(ch, ret);
340
341 common->set_addr(addr + common->ytop_off,
342 addr + common->ybtm_off,
343 addr + common->ctop_off,
344 addr + common->cbtm_off);
345
346 /**
347 * Set interrupt for both the fields in VPIF Register enable channel in
348 * VPIF register
349 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300350 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300351 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
352 channel0_intr_assert();
353 channel0_intr_enable(1);
354 enable_channel0(1);
355 }
356 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
357 (common->started == 2)) {
358 channel1_intr_assert();
359 channel1_intr_enable(1);
360 enable_channel1(1);
361 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300362
363 return 0;
364}
365
366/* abort streaming and wait for last buffer */
367static int vpif_stop_streaming(struct vb2_queue *vq)
368{
369 struct vpif_fh *fh = vb2_get_drv_priv(vq);
370 struct channel_obj *ch = fh->channel;
371 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300372 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300373
374 if (!vb2_is_streaming(vq))
375 return 0;
376
377 common = &ch->common[VPIF_VIDEO_INDEX];
378
379 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300380 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300381 while (!list_empty(&common->dma_queue)) {
382 common->next_frm = list_entry(common->dma_queue.next,
383 struct vpif_cap_buffer, list);
384 list_del(&common->next_frm->list);
385 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
386 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300387 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300388
389 return 0;
390}
391
392static struct vb2_ops video_qops = {
393 .queue_setup = vpif_buffer_queue_setup,
394 .wait_prepare = vpif_wait_prepare,
395 .wait_finish = vpif_wait_finish,
396 .buf_init = vpif_buffer_init,
397 .buf_prepare = vpif_buffer_prepare,
398 .start_streaming = vpif_start_streaming,
399 .stop_streaming = vpif_stop_streaming,
400 .buf_cleanup = vpif_buf_cleanup,
401 .buf_queue = vpif_buffer_queue,
402};
403
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300404/**
405 * vpif_process_buffer_complete: process a completed buffer
406 * @common: ptr to common channel object
407 *
408 * This function time stamp the buffer and mark it as DONE. It also
409 * wake up any process waiting on the QUEUE and set the next buffer
410 * as current
411 */
412static void vpif_process_buffer_complete(struct common_obj *common)
413{
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300414 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
415 vb2_buffer_done(&common->cur_frm->vb,
416 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300417 /* Make curFrm pointing to nextFrm */
418 common->cur_frm = common->next_frm;
419}
420
421/**
422 * vpif_schedule_next_buffer: set next buffer address for capture
423 * @common : ptr to common channel object
424 *
425 * This function will get next buffer from the dma queue and
426 * set the buffer address in the vpif register for capture.
427 * the buffer is marked active
428 */
429static void vpif_schedule_next_buffer(struct common_obj *common)
430{
431 unsigned long addr = 0;
432
Hans Verkuilaec96832012-11-16 12:03:06 -0300433 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300434 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300435 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300436 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300437 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300438 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300439 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
440 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300441
442 /* Set top and bottom field addresses in VPIF registers */
443 common->set_addr(addr + common->ytop_off,
444 addr + common->ybtm_off,
445 addr + common->ctop_off,
446 addr + common->cbtm_off);
447}
448
449/**
450 * vpif_channel_isr : ISR handler for vpif capture
451 * @irq: irq number
452 * @dev_id: dev_id ptr
453 *
454 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300455 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300456 */
457static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
458{
459 struct vpif_device *dev = &vpif_obj;
460 struct common_obj *common;
461 struct channel_obj *ch;
462 enum v4l2_field field;
463 int channel_id = 0;
464 int fid = -1, i;
465
466 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300467 if (!vpif_intr_status(channel_id))
468 return IRQ_NONE;
469
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300470 ch = dev->dev[channel_id];
471
472 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
473
474 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
475 common = &ch->common[i];
476 /* skip If streaming is not started in this channel */
477 if (0 == common->started)
478 continue;
479
480 /* Check the field format */
481 if (1 == ch->vpifparams.std_info.frm_fmt) {
482 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300483 spin_lock(&common->irqlock);
484 if (list_empty(&common->dma_queue)) {
485 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300486 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300487 }
488 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300489
490 if (!channel_first_int[i][channel_id])
491 vpif_process_buffer_complete(common);
492
493 channel_first_int[i][channel_id] = 0;
494
495 vpif_schedule_next_buffer(common);
496
497
498 channel_first_int[i][channel_id] = 0;
499 } else {
500 /**
501 * Interlaced mode. If it is first interrupt, ignore
502 * it
503 */
504 if (channel_first_int[i][channel_id]) {
505 channel_first_int[i][channel_id] = 0;
506 continue;
507 }
508 if (0 == i) {
509 ch->field_id ^= 1;
510 /* Get field id from VPIF registers */
511 fid = vpif_channel_getfid(ch->channel_id);
512 if (fid != ch->field_id) {
513 /**
514 * If field id does not match stored
515 * field id, make them in sync
516 */
517 if (0 == fid)
518 ch->field_id = fid;
519 return IRQ_HANDLED;
520 }
521 }
522 /* device field id and local field id are in sync */
523 if (0 == fid) {
524 /* this is even field */
525 if (common->cur_frm == common->next_frm)
526 continue;
527
528 /* mark the current buffer as done */
529 vpif_process_buffer_complete(common);
530 } else if (1 == fid) {
531 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300532 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300533 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300534 (common->cur_frm != common->next_frm)) {
535 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300536 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300537 }
538 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300539
540 vpif_schedule_next_buffer(common);
541 }
542 }
543 }
544 return IRQ_HANDLED;
545}
546
547/**
548 * vpif_update_std_info() - update standard related info
549 * @ch: ptr to channel object
550 *
551 * For a given standard selected by application, update values
552 * in the device data structures
553 */
554static int vpif_update_std_info(struct channel_obj *ch)
555{
556 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
557 struct vpif_params *vpifparams = &ch->vpifparams;
558 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300559 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300560 struct video_obj *vid_ch = &ch->video;
561 int index;
562
563 vpif_dbg(2, debug, "vpif_update_std_info\n");
564
Mats Randgaardaa444402010-12-16 12:17:42 -0300565 for (index = 0; index < vpif_ch_params_count; index++) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300566 config = &ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300567 if (config->hd_sd == 0) {
568 vpif_dbg(2, debug, "SD format\n");
569 if (config->stdid & vid_ch->stdid) {
570 memcpy(std_info, config, sizeof(*config));
571 break;
572 }
573 } else {
574 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300575 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
576 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300577 memcpy(std_info, config, sizeof(*config));
578 break;
579 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300580 }
581 }
582
583 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300584 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300585 return -EINVAL;
586
587 common->fmt.fmt.pix.width = std_info->width;
588 common->width = std_info->width;
589 common->fmt.fmt.pix.height = std_info->height;
590 common->height = std_info->height;
591 common->fmt.fmt.pix.bytesperline = std_info->width;
592 vpifparams->video_params.hpitch = std_info->width;
593 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300594
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300595 return 0;
596}
597
598/**
599 * vpif_calculate_offsets : This function calculates buffers offsets
600 * @ch : ptr to channel object
601 *
602 * This function calculates buffer offsets for Y and C in the top and
603 * bottom field
604 */
605static void vpif_calculate_offsets(struct channel_obj *ch)
606{
607 unsigned int hpitch, vpitch, sizeimage;
608 struct video_obj *vid_ch = &(ch->video);
609 struct vpif_params *vpifparams = &ch->vpifparams;
610 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
611 enum v4l2_field field = common->fmt.fmt.pix.field;
612
613 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
614
615 if (V4L2_FIELD_ANY == field) {
616 if (vpifparams->std_info.frm_fmt)
617 vid_ch->buf_field = V4L2_FIELD_NONE;
618 else
619 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
620 } else
621 vid_ch->buf_field = common->fmt.fmt.pix.field;
622
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300623 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300624
625 hpitch = common->fmt.fmt.pix.bytesperline;
626 vpitch = sizeimage / (hpitch * 2);
627
628 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
629 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
630 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
631 common->ytop_off = 0;
632 common->ybtm_off = hpitch;
633 common->ctop_off = sizeimage / 2;
634 common->cbtm_off = sizeimage / 2 + hpitch;
635 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
636 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
637 common->ytop_off = 0;
638 common->ybtm_off = sizeimage / 4;
639 common->ctop_off = sizeimage / 2;
640 common->cbtm_off = common->ctop_off + sizeimage / 4;
641 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
642 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
643 common->ybtm_off = 0;
644 common->ytop_off = sizeimage / 4;
645 common->cbtm_off = sizeimage / 2;
646 common->ctop_off = common->cbtm_off + sizeimage / 4;
647 }
648 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
649 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
650 vpifparams->video_params.storage_mode = 1;
651 else
652 vpifparams->video_params.storage_mode = 0;
653
654 if (1 == vpifparams->std_info.frm_fmt)
655 vpifparams->video_params.hpitch =
656 common->fmt.fmt.pix.bytesperline;
657 else {
658 if ((field == V4L2_FIELD_ANY)
659 || (field == V4L2_FIELD_INTERLACED))
660 vpifparams->video_params.hpitch =
661 common->fmt.fmt.pix.bytesperline * 2;
662 else
663 vpifparams->video_params.hpitch =
664 common->fmt.fmt.pix.bytesperline;
665 }
666
667 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
668}
669
670/**
671 * vpif_config_format: configure default frame format in the device
672 * ch : ptr to channel object
673 */
674static void vpif_config_format(struct channel_obj *ch)
675{
676 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
677
678 vpif_dbg(2, debug, "vpif_config_format\n");
679
680 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
681 if (config_params.numbuffers[ch->channel_id] == 0)
682 common->memory = V4L2_MEMORY_USERPTR;
683 else
684 common->memory = V4L2_MEMORY_MMAP;
685
686 common->fmt.fmt.pix.sizeimage
687 = config_params.channel_bufsize[ch->channel_id];
688
689 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
690 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
691 else
692 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
693 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
694}
695
696/**
697 * vpif_get_default_field() - Get default field type based on interface
698 * @vpif_params - ptr to vpif params
699 */
700static inline enum v4l2_field vpif_get_default_field(
701 struct vpif_interface *iface)
702{
703 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
704 V4L2_FIELD_INTERLACED;
705}
706
707/**
708 * vpif_check_format() - check given pixel format for compatibility
709 * @ch - channel ptr
710 * @pixfmt - Given pixel format
711 * @update - update the values as per hardware requirement
712 *
713 * Check the application pixel format for S_FMT and update the input
714 * values as per hardware limits for TRY_FMT. The default pixel and
715 * field format is selected based on interface type.
716 */
717static int vpif_check_format(struct channel_obj *ch,
718 struct v4l2_pix_format *pixfmt,
719 int update)
720{
721 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
722 struct vpif_params *vpif_params = &ch->vpifparams;
723 enum v4l2_field field = pixfmt->field;
724 u32 sizeimage, hpitch, vpitch;
725 int ret = -EINVAL;
726
727 vpif_dbg(2, debug, "vpif_check_format\n");
728 /**
729 * first check for the pixel format. If if_type is Raw bayer,
730 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
731 * V4L2_PIX_FMT_YUV422P is supported
732 */
733 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
734 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
735 if (!update) {
736 vpif_dbg(2, debug, "invalid pix format\n");
737 goto exit;
738 }
739 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
740 }
741 } else {
742 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
743 if (!update) {
744 vpif_dbg(2, debug, "invalid pixel format\n");
745 goto exit;
746 }
747 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
748 }
749 }
750
751 if (!(VPIF_VALID_FIELD(field))) {
752 if (!update) {
753 vpif_dbg(2, debug, "invalid field format\n");
754 goto exit;
755 }
756 /**
757 * By default use FIELD_NONE for RAW Bayer capture
758 * and FIELD_INTERLACED for other interfaces
759 */
760 field = vpif_get_default_field(&vpif_params->iface);
761 } else if (field == V4L2_FIELD_ANY)
762 /* unsupported field. Use default */
763 field = vpif_get_default_field(&vpif_params->iface);
764
765 /* validate the hpitch */
766 hpitch = pixfmt->bytesperline;
767 if (hpitch < vpif_params->std_info.width) {
768 if (!update) {
769 vpif_dbg(2, debug, "invalid hpitch\n");
770 goto exit;
771 }
772 hpitch = vpif_params->std_info.width;
773 }
774
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300775 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300776
777 vpitch = sizeimage / (hpitch * 2);
778
779 /* validate the vpitch */
780 if (vpitch < vpif_params->std_info.height) {
781 if (!update) {
782 vpif_dbg(2, debug, "Invalid vpitch\n");
783 goto exit;
784 }
785 vpitch = vpif_params->std_info.height;
786 }
787
788 /* Check for 8 byte alignment */
789 if (!ALIGN(hpitch, 8)) {
790 if (!update) {
791 vpif_dbg(2, debug, "invalid pitch alignment\n");
792 goto exit;
793 }
794 /* adjust to next 8 byte boundary */
795 hpitch = (((hpitch + 7) / 8) * 8);
796 }
797 /* if update is set, modify the bytesperline and sizeimage */
798 if (update) {
799 pixfmt->bytesperline = hpitch;
800 pixfmt->sizeimage = hpitch * vpitch * 2;
801 }
802 /**
803 * Image width and height is always based on current standard width and
804 * height
805 */
806 pixfmt->width = common->fmt.fmt.pix.width;
807 pixfmt->height = common->fmt.fmt.pix.height;
808 return 0;
809exit:
810 return ret;
811}
812
813/**
814 * vpif_config_addr() - function to configure buffer address in vpif
815 * @ch - channel ptr
816 * @muxmode - channel mux mode
817 */
818static void vpif_config_addr(struct channel_obj *ch, int muxmode)
819{
820 struct common_obj *common;
821
822 vpif_dbg(2, debug, "vpif_config_addr\n");
823
824 common = &(ch->common[VPIF_VIDEO_INDEX]);
825
826 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
827 common->set_addr = ch1_set_videobuf_addr;
828 else if (2 == muxmode)
829 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
830 else
831 common->set_addr = ch0_set_videobuf_addr;
832}
833
834/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300835 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300836 * @filep: file pointer
837 * @vma: ptr to vm_area_struct
838 */
839static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
840{
841 /* Get the channel object and file handle object */
842 struct vpif_fh *fh = filep->private_data;
843 struct channel_obj *ch = fh->channel;
844 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300845 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300846
847 vpif_dbg(2, debug, "vpif_mmap\n");
848
Hans Verkuil72246792012-07-31 03:48:31 -0300849 if (mutex_lock_interruptible(&common->lock))
850 return -ERESTARTSYS;
851 ret = vb2_mmap(&common->buffer_queue, vma);
852 mutex_unlock(&common->lock);
853 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300854}
855
856/**
857 * vpif_poll: It is used for select/poll system call
858 * @filep: file pointer
859 * @wait: poll table to wait
860 */
861static unsigned int vpif_poll(struct file *filep, poll_table * wait)
862{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300863 struct vpif_fh *fh = filep->private_data;
864 struct channel_obj *channel = fh->channel;
865 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300866 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300867
868 vpif_dbg(2, debug, "vpif_poll\n");
869
Hans Verkuil72246792012-07-31 03:48:31 -0300870 if (common->started) {
871 mutex_lock(&common->lock);
872 res = vb2_poll(&common->buffer_queue, filep, wait);
873 mutex_unlock(&common->lock);
874 }
875 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300876}
877
878/**
879 * vpif_open : vpif open handler
880 * @filep: file ptr
881 *
882 * It creates object of file handle structure and stores it in private_data
883 * member of filepointer
884 */
885static int vpif_open(struct file *filep)
886{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300887 struct video_device *vdev = video_devdata(filep);
888 struct common_obj *common;
889 struct video_obj *vid_ch;
890 struct channel_obj *ch;
891 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300892
893 vpif_dbg(2, debug, "vpif_open\n");
894
895 ch = video_get_drvdata(vdev);
896
897 vid_ch = &ch->video;
898 common = &ch->common[VPIF_VIDEO_INDEX];
899
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300900 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300901 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300902 if (NULL == fh) {
903 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300904 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300905 }
906
Hans Verkuil72246792012-07-31 03:48:31 -0300907 if (mutex_lock_interruptible(&common->lock)) {
908 kfree(fh);
909 return -ERESTARTSYS;
910 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300911 /* store pointer to fh in private_data member of filep */
912 filep->private_data = fh;
913 fh->channel = ch;
914 fh->initialized = 0;
915 /* If decoder is not initialized. initialize it */
916 if (!ch->initialized) {
917 fh->initialized = 1;
918 ch->initialized = 1;
919 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
920 }
921 /* Increment channel usrs counter */
922 ch->usrs++;
923 /* Set io_allowed member to false */
924 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
925 /* Initialize priority of this instance to default priority */
926 fh->prio = V4L2_PRIORITY_UNSET;
927 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300928 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300929 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300930}
931
932/**
933 * vpif_release : function to clean up file close
934 * @filep: file pointer
935 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300936 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300937 * handle
938 */
939static int vpif_release(struct file *filep)
940{
941 struct vpif_fh *fh = filep->private_data;
942 struct channel_obj *ch = fh->channel;
943 struct common_obj *common;
944
945 vpif_dbg(2, debug, "vpif_release\n");
946
947 common = &ch->common[VPIF_VIDEO_INDEX];
948
Hans Verkuil72246792012-07-31 03:48:31 -0300949 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300950 /* if this instance is doing IO */
951 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
952 /* Reset io_usrs member of channel object */
953 common->io_usrs = 0;
954 /* Disable channel as per its device type and channel id */
955 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
956 enable_channel0(0);
957 channel0_intr_enable(0);
958 }
959 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
960 (2 == common->started)) {
961 enable_channel1(0);
962 channel1_intr_enable(0);
963 }
964 common->started = 0;
965 /* Free buffers allocated */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300966 vb2_queue_release(&common->buffer_queue);
967 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300968 }
969
970 /* Decrement channel usrs counter */
971 ch->usrs--;
972
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300973 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300974 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300975
976 if (fh->initialized)
977 ch->initialized = 0;
978
Hans Verkuil72246792012-07-31 03:48:31 -0300979 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300980 filep->private_data = NULL;
981 kfree(fh);
982 return 0;
983}
984
985/**
986 * vpif_reqbufs() - request buffer handler
987 * @file: file ptr
988 * @priv: file handle
989 * @reqbuf: request buffer structure ptr
990 */
991static int vpif_reqbufs(struct file *file, void *priv,
992 struct v4l2_requestbuffers *reqbuf)
993{
994 struct vpif_fh *fh = priv;
995 struct channel_obj *ch = fh->channel;
996 struct common_obj *common;
997 u8 index = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300998 struct vb2_queue *q;
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -0300999 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001000
1001 vpif_dbg(2, debug, "vpif_reqbufs\n");
1002
1003 /**
1004 * This file handle has not initialized the channel,
1005 * It is not allowed to do settings
1006 */
1007 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
1008 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1009 if (!fh->initialized) {
1010 vpif_dbg(1, debug, "Channel Busy\n");
1011 return -EBUSY;
1012 }
1013 }
1014
Manjunath Hadli764af392012-04-13 04:49:34 -03001015 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001016 return -EINVAL;
1017
1018 index = VPIF_VIDEO_INDEX;
1019
1020 common = &ch->common[index];
1021
Hans Verkuil46656af2011-01-04 06:51:35 -03001022 if (0 != common->io_usrs)
1023 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001024
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001025 /* Initialize videobuf2 queue as per the buffer type */
1026 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
Wei Yongjun94b76a82012-11-15 09:18:17 -03001027 if (IS_ERR(common->alloc_ctx)) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001028 vpif_err("Failed to get the context\n");
Wei Yongjun94b76a82012-11-15 09:18:17 -03001029 return PTR_ERR(common->alloc_ctx);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001030 }
1031 q = &common->buffer_queue;
1032 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1033 q->io_modes = VB2_MMAP | VB2_USERPTR;
1034 q->drv_priv = fh;
1035 q->ops = &video_qops;
1036 q->mem_ops = &vb2_dma_contig_memops;
1037 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1038
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -03001039 ret = vb2_queue_init(q);
1040 if (ret) {
1041 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1042 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1043 return ret;
1044 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001045 /* Set io allowed member of file handle to TRUE */
1046 fh->io_allowed[index] = 1;
1047 /* Increment io usrs member of channel object to 1 */
1048 common->io_usrs = 1;
1049 /* Store type of memory requested in channel object */
1050 common->memory = reqbuf->memory;
1051 INIT_LIST_HEAD(&common->dma_queue);
1052
1053 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001054 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001055}
1056
1057/**
1058 * vpif_querybuf() - query buffer handler
1059 * @file: file ptr
1060 * @priv: file handle
1061 * @buf: v4l2 buffer structure ptr
1062 */
1063static int vpif_querybuf(struct file *file, void *priv,
1064 struct v4l2_buffer *buf)
1065{
1066 struct vpif_fh *fh = priv;
1067 struct channel_obj *ch = fh->channel;
1068 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1069
1070 vpif_dbg(2, debug, "vpif_querybuf\n");
1071
1072 if (common->fmt.type != buf->type)
1073 return -EINVAL;
1074
1075 if (common->memory != V4L2_MEMORY_MMAP) {
1076 vpif_dbg(1, debug, "Invalid memory\n");
1077 return -EINVAL;
1078 }
1079
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001080 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001081}
1082
1083/**
1084 * vpif_qbuf() - query buffer handler
1085 * @file: file ptr
1086 * @priv: file handle
1087 * @buf: v4l2 buffer structure ptr
1088 */
1089static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1090{
1091
1092 struct vpif_fh *fh = priv;
1093 struct channel_obj *ch = fh->channel;
1094 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1095 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001096
1097 vpif_dbg(2, debug, "vpif_qbuf\n");
1098
1099 if (common->fmt.type != tbuf.type) {
1100 vpif_err("invalid buffer type\n");
1101 return -EINVAL;
1102 }
1103
1104 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001105 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001106 return -EACCES;
1107 }
1108
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001109 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001110}
1111
1112/**
1113 * vpif_dqbuf() - query buffer handler
1114 * @file: file ptr
1115 * @priv: file handle
1116 * @buf: v4l2 buffer structure ptr
1117 */
1118static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1119{
1120 struct vpif_fh *fh = priv;
1121 struct channel_obj *ch = fh->channel;
1122 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1123
1124 vpif_dbg(2, debug, "vpif_dqbuf\n");
1125
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001126 return vb2_dqbuf(&common->buffer_queue, buf,
1127 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001128}
1129
1130/**
1131 * vpif_streamon() - streamon handler
1132 * @file: file ptr
1133 * @priv: file handle
1134 * @buftype: v4l2 buffer type
1135 */
1136static int vpif_streamon(struct file *file, void *priv,
1137 enum v4l2_buf_type buftype)
1138{
1139
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001140 struct vpif_fh *fh = priv;
1141 struct channel_obj *ch = fh->channel;
1142 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1143 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1144 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001145 int ret = 0;
1146
1147 vpif_dbg(2, debug, "vpif_streamon\n");
1148
1149 vpif = &ch->vpifparams;
1150
1151 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1152 vpif_dbg(1, debug, "buffer type not supported\n");
1153 return -EINVAL;
1154 }
1155
1156 /* If file handle is not allowed IO, return error */
1157 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1158 vpif_dbg(1, debug, "io not allowed\n");
1159 return -EACCES;
1160 }
1161
1162 /* If Streaming is already started, return error */
1163 if (common->started) {
1164 vpif_dbg(1, debug, "channel->started\n");
1165 return -EBUSY;
1166 }
1167
1168 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1169 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1170 vpif->std_info.ycmux_mode == 0) ||
1171 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1172 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1173 vpif_dbg(1, debug, "other channel is being used\n");
1174 return -EBUSY;
1175 }
1176
1177 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1178 if (ret)
1179 return ret;
1180
1181 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001182 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001183
Hans Verkuil178cce12012-09-20 09:06:30 -03001184 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001185 vpif_dbg(1, debug, "stream on failed in subdev\n");
1186 return ret;
1187 }
1188
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001189 /* Call vb2_streamon to start streaming in videobuf2 */
1190 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001191 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001192 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001193 return ret;
1194 }
1195
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001196 return ret;
1197}
1198
1199/**
1200 * vpif_streamoff() - streamoff handler
1201 * @file: file ptr
1202 * @priv: file handle
1203 * @buftype: v4l2 buffer type
1204 */
1205static int vpif_streamoff(struct file *file, void *priv,
1206 enum v4l2_buf_type buftype)
1207{
1208
1209 struct vpif_fh *fh = priv;
1210 struct channel_obj *ch = fh->channel;
1211 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1212 int ret;
1213
1214 vpif_dbg(2, debug, "vpif_streamoff\n");
1215
1216 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1217 vpif_dbg(1, debug, "buffer type not supported\n");
1218 return -EINVAL;
1219 }
1220
1221 /* If io is allowed for this file handle, return error */
1222 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1223 vpif_dbg(1, debug, "io not allowed\n");
1224 return -EACCES;
1225 }
1226
1227 /* If streaming is not started, return error */
1228 if (!common->started) {
1229 vpif_dbg(1, debug, "channel->started\n");
1230 return -EINVAL;
1231 }
1232
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001233 /* disable channel */
1234 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1235 enable_channel0(0);
1236 channel0_intr_enable(0);
1237 } else {
1238 enable_channel1(0);
1239 channel1_intr_enable(0);
1240 }
1241
1242 common->started = 0;
1243
Hans Verkuil178cce12012-09-20 09:06:30 -03001244 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001245
Hans Verkuil178cce12012-09-20 09:06:30 -03001246 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001247 vpif_dbg(1, debug, "stream off failed in subdev\n");
1248
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001249 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001250}
1251
1252/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001253 * vpif_input_to_subdev() - Maps input to sub device
1254 * @vpif_cfg - global config ptr
1255 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001256 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001257 *
1258 * lookup the sub device information for a given input index.
1259 * we report all the inputs to application. inputs table also
1260 * has sub device name for the each input
1261 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001262static int vpif_input_to_subdev(
1263 struct vpif_capture_config *vpif_cfg,
1264 struct vpif_capture_chan_config *chan_cfg,
1265 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001266{
Hans Verkuil178cce12012-09-20 09:06:30 -03001267 struct vpif_subdev_info *subdev_info;
1268 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001269 int i;
1270
Hans Verkuil178cce12012-09-20 09:06:30 -03001271 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001272
Hans Verkuil178cce12012-09-20 09:06:30 -03001273 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1274 if (subdev_name == NULL)
1275 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001276
1277 /* loop through the sub device list to get the sub device info */
1278 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1279 subdev_info = &vpif_cfg->subdev_info[i];
1280 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001281 return i;
1282 }
1283 return -1;
1284}
1285
1286/**
1287 * vpif_set_input() - Select an input
1288 * @vpif_cfg - global config ptr
1289 * @ch - channel
1290 * @_index - Given input index from application
1291 *
1292 * Select the given input.
1293 */
1294static int vpif_set_input(
1295 struct vpif_capture_config *vpif_cfg,
1296 struct channel_obj *ch,
1297 int index)
1298{
1299 struct vpif_capture_chan_config *chan_cfg =
1300 &vpif_cfg->chan_config[ch->channel_id];
1301 struct vpif_subdev_info *subdev_info = NULL;
1302 struct v4l2_subdev *sd = NULL;
1303 u32 input = 0, output = 0;
1304 int sd_index;
1305 int ret;
1306
1307 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1308 if (sd_index >= 0) {
1309 sd = vpif_obj.sd[sd_index];
1310 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001311 }
1312
Hans Verkuil178cce12012-09-20 09:06:30 -03001313 /* first setup input path from sub device to vpif */
1314 if (sd && vpif_cfg->setup_input_path) {
1315 ret = vpif_cfg->setup_input_path(ch->channel_id,
1316 subdev_info->name);
1317 if (ret < 0) {
1318 vpif_dbg(1, debug, "couldn't setup input path for the" \
1319 " sub device %s, for input index %d\n",
1320 subdev_info->name, index);
1321 return ret;
1322 }
1323 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001324
Hans Verkuil178cce12012-09-20 09:06:30 -03001325 if (sd) {
1326 input = chan_cfg->inputs[index].input_route;
1327 output = chan_cfg->inputs[index].output_route;
1328 ret = v4l2_subdev_call(sd, video, s_routing,
1329 input, output, 0);
1330 if (ret < 0 && ret != -ENOIOCTLCMD) {
1331 vpif_dbg(1, debug, "Failed to set input\n");
1332 return ret;
1333 }
1334 }
1335 ch->input_idx = index;
1336 ch->sd = sd;
1337 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001338 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001339
Hans Verkuil178cce12012-09-20 09:06:30 -03001340 /* update tvnorms from the sub device input info */
1341 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1342 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001343}
1344
1345/**
1346 * vpif_querystd() - querystd handler
1347 * @file: file ptr
1348 * @priv: file handle
1349 * @std_id: ptr to std id
1350 *
1351 * This function is called to detect standard at the selected input
1352 */
1353static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1354{
1355 struct vpif_fh *fh = priv;
1356 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001357 int ret = 0;
1358
1359 vpif_dbg(2, debug, "vpif_querystd\n");
1360
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001361 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001362 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001363
Hans Verkuil178cce12012-09-20 09:06:30 -03001364 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1365 return -ENODATA;
1366 if (ret) {
1367 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1368 return ret;
1369 }
1370
1371 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001372}
1373
1374/**
1375 * vpif_g_std() - get STD handler
1376 * @file: file ptr
1377 * @priv: file handle
1378 * @std_id: ptr to std id
1379 */
1380static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1381{
1382 struct vpif_fh *fh = priv;
1383 struct channel_obj *ch = fh->channel;
1384
1385 vpif_dbg(2, debug, "vpif_g_std\n");
1386
1387 *std = ch->video.stdid;
1388 return 0;
1389}
1390
1391/**
1392 * vpif_s_std() - set STD handler
1393 * @file: file ptr
1394 * @priv: file handle
1395 * @std_id: ptr to std id
1396 */
1397static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1398{
1399 struct vpif_fh *fh = priv;
1400 struct channel_obj *ch = fh->channel;
1401 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1402 int ret = 0;
1403
1404 vpif_dbg(2, debug, "vpif_s_std\n");
1405
1406 if (common->started) {
1407 vpif_err("streaming in progress\n");
1408 return -EBUSY;
1409 }
1410
1411 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1412 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1413 if (!fh->initialized) {
1414 vpif_dbg(1, debug, "Channel Busy\n");
1415 return -EBUSY;
1416 }
1417 }
1418
Hans Verkuilffb48772010-05-01 08:03:24 -03001419 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001420 if (0 != ret)
1421 return ret;
1422
1423 fh->initialized = 1;
1424
1425 /* Call encoder subdevice function to set the standard */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001426 ch->video.stdid = *std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001427 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001428
1429 /* Get the information about the standard */
1430 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001431 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001432 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001433 }
1434
1435 /* Configure the default format information */
1436 vpif_config_format(ch);
1437
1438 /* set standard in the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001439 ret = v4l2_subdev_call(ch->sd, core, s_std, *std_id);
1440 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001441 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001442 return ret;
1443 }
1444 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001445}
1446
1447/**
1448 * vpif_enum_input() - ENUMINPUT handler
1449 * @file: file ptr
1450 * @priv: file handle
1451 * @input: ptr to input structure
1452 */
1453static int vpif_enum_input(struct file *file, void *priv,
1454 struct v4l2_input *input)
1455{
1456
1457 struct vpif_capture_config *config = vpif_dev->platform_data;
1458 struct vpif_capture_chan_config *chan_cfg;
1459 struct vpif_fh *fh = priv;
1460 struct channel_obj *ch = fh->channel;
1461
1462 chan_cfg = &config->chan_config[ch->channel_id];
1463
1464 if (input->index >= chan_cfg->input_count) {
1465 vpif_dbg(1, debug, "Invalid input index\n");
1466 return -EINVAL;
1467 }
1468
1469 memcpy(input, &chan_cfg->inputs[input->index].input,
1470 sizeof(*input));
1471 return 0;
1472}
1473
1474/**
1475 * vpif_g_input() - Get INPUT handler
1476 * @file: file ptr
1477 * @priv: file handle
1478 * @index: ptr to input index
1479 */
1480static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1481{
1482 struct vpif_fh *fh = priv;
1483 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001484
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001485 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001486 return 0;
1487}
1488
1489/**
1490 * vpif_s_input() - Set INPUT handler
1491 * @file: file ptr
1492 * @priv: file handle
1493 * @index: input index
1494 */
1495static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1496{
1497 struct vpif_capture_config *config = vpif_dev->platform_data;
1498 struct vpif_capture_chan_config *chan_cfg;
1499 struct vpif_fh *fh = priv;
1500 struct channel_obj *ch = fh->channel;
1501 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001502 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001503
1504 chan_cfg = &config->chan_config[ch->channel_id];
1505
Hans Verkuil7aaad132012-09-20 09:06:25 -03001506 if (index >= chan_cfg->input_count)
1507 return -EINVAL;
1508
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001509 if (common->started) {
1510 vpif_err("Streaming in progress\n");
1511 return -EBUSY;
1512 }
1513
1514 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1515 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1516 if (!fh->initialized) {
1517 vpif_dbg(1, debug, "Channel Busy\n");
1518 return -EBUSY;
1519 }
1520 }
1521
Hans Verkuilffb48772010-05-01 08:03:24 -03001522 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001523 if (0 != ret)
1524 return ret;
1525
1526 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001527 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001528}
1529
1530/**
1531 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1532 * @file: file ptr
1533 * @priv: file handle
1534 * @index: input index
1535 */
1536static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1537 struct v4l2_fmtdesc *fmt)
1538{
1539 struct vpif_fh *fh = priv;
1540 struct channel_obj *ch = fh->channel;
1541
1542 if (fmt->index != 0) {
1543 vpif_dbg(1, debug, "Invalid format index\n");
1544 return -EINVAL;
1545 }
1546
1547 /* Fill in the information about format */
1548 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1549 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1550 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1551 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1552 } else {
1553 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1554 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1555 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1556 }
1557 return 0;
1558}
1559
1560/**
1561 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1562 * @file: file ptr
1563 * @priv: file handle
1564 * @fmt: ptr to v4l2 format structure
1565 */
1566static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1567 struct v4l2_format *fmt)
1568{
1569 struct vpif_fh *fh = priv;
1570 struct channel_obj *ch = fh->channel;
1571 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1572
1573 return vpif_check_format(ch, pixfmt, 1);
1574}
1575
1576
1577/**
1578 * vpif_g_fmt_vid_cap() - Set INPUT handler
1579 * @file: file ptr
1580 * @priv: file handle
1581 * @fmt: ptr to v4l2 format structure
1582 */
1583static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1584 struct v4l2_format *fmt)
1585{
1586 struct vpif_fh *fh = priv;
1587 struct channel_obj *ch = fh->channel;
1588 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1589
1590 /* Check the validity of the buffer type */
1591 if (common->fmt.type != fmt->type)
1592 return -EINVAL;
1593
1594 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001595 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001596 return 0;
1597}
1598
1599/**
1600 * vpif_s_fmt_vid_cap() - Set FMT handler
1601 * @file: file ptr
1602 * @priv: file handle
1603 * @fmt: ptr to v4l2 format structure
1604 */
1605static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1606 struct v4l2_format *fmt)
1607{
1608 struct vpif_fh *fh = priv;
1609 struct channel_obj *ch = fh->channel;
1610 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1611 struct v4l2_pix_format *pixfmt;
1612 int ret = 0;
1613
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001614 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001615
1616 /* If streaming is started, return error */
1617 if (common->started) {
1618 vpif_dbg(1, debug, "Streaming is started\n");
1619 return -EBUSY;
1620 }
1621
1622 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1623 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1624 if (!fh->initialized) {
1625 vpif_dbg(1, debug, "Channel Busy\n");
1626 return -EBUSY;
1627 }
1628 }
1629
Hans Verkuilffb48772010-05-01 08:03:24 -03001630 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001631 if (0 != ret)
1632 return ret;
1633
1634 fh->initialized = 1;
1635
1636 pixfmt = &fmt->fmt.pix;
1637 /* Check for valid field format */
1638 ret = vpif_check_format(ch, pixfmt, 0);
1639
1640 if (ret)
1641 return ret;
1642 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001643 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001644 return 0;
1645}
1646
1647/**
1648 * vpif_querycap() - QUERYCAP handler
1649 * @file: file ptr
1650 * @priv: file handle
1651 * @cap: ptr to v4l2_capability structure
1652 */
1653static int vpif_querycap(struct file *file, void *priv,
1654 struct v4l2_capability *cap)
1655{
1656 struct vpif_capture_config *config = vpif_dev->platform_data;
1657
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001658 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1659 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1660 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1661 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1662 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001663 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1664
1665 return 0;
1666}
1667
1668/**
1669 * vpif_g_priority() - get priority handler
1670 * @file: file ptr
1671 * @priv: file handle
1672 * @prio: ptr to v4l2_priority structure
1673 */
1674static int vpif_g_priority(struct file *file, void *priv,
1675 enum v4l2_priority *prio)
1676{
1677 struct vpif_fh *fh = priv;
1678 struct channel_obj *ch = fh->channel;
1679
1680 *prio = v4l2_prio_max(&ch->prio);
1681
1682 return 0;
1683}
1684
1685/**
1686 * vpif_s_priority() - set priority handler
1687 * @file: file ptr
1688 * @priv: file handle
1689 * @prio: ptr to v4l2_priority structure
1690 */
1691static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1692{
1693 struct vpif_fh *fh = priv;
1694 struct channel_obj *ch = fh->channel;
1695
1696 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1697}
1698
1699/**
1700 * vpif_cropcap() - cropcap handler
1701 * @file: file ptr
1702 * @priv: file handle
1703 * @crop: ptr to v4l2_cropcap structure
1704 */
1705static int vpif_cropcap(struct file *file, void *priv,
1706 struct v4l2_cropcap *crop)
1707{
1708 struct vpif_fh *fh = priv;
1709 struct channel_obj *ch = fh->channel;
1710 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1711
1712 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1713 return -EINVAL;
1714
1715 crop->bounds.left = 0;
1716 crop->bounds.top = 0;
1717 crop->bounds.height = common->height;
1718 crop->bounds.width = common->width;
1719 crop->defrect = crop->bounds;
1720 return 0;
1721}
1722
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001723/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001724 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001725 * @file: file ptr
1726 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001727 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001728 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001729static int
1730vpif_enum_dv_timings(struct file *file, void *priv,
1731 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001732{
1733 struct vpif_fh *fh = priv;
1734 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001735 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001736
Hans Verkuil178cce12012-09-20 09:06:30 -03001737 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001738 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001739 return -EINVAL;
1740 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001741}
1742
1743/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001744 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001745 * @file: file ptr
1746 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001747 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001748 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001749static int
1750vpif_query_dv_timings(struct file *file, void *priv,
1751 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001752{
1753 struct vpif_fh *fh = priv;
1754 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001755 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001756
Hans Verkuil178cce12012-09-20 09:06:30 -03001757 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001758 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001759 return -ENODATA;
1760 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001761}
1762
Mats Randgaardc027e162010-12-16 12:17:44 -03001763/**
1764 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1765 * @file: file ptr
1766 * @priv: file handle
1767 * @timings: digital video timings
1768 */
1769static int vpif_s_dv_timings(struct file *file, void *priv,
1770 struct v4l2_dv_timings *timings)
1771{
1772 struct vpif_fh *fh = priv;
1773 struct channel_obj *ch = fh->channel;
1774 struct vpif_params *vpifparams = &ch->vpifparams;
1775 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1776 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001777 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001778 int ret;
1779
1780 if (timings->type != V4L2_DV_BT_656_1120) {
1781 vpif_dbg(2, debug, "Timing type not defined\n");
1782 return -EINVAL;
1783 }
1784
1785 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001786 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1787 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1788 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001789 if (ret < 0) {
1790 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1791 return ret;
1792 }
1793
1794 if (!(timings->bt.width && timings->bt.height &&
1795 (timings->bt.hbackporch ||
1796 timings->bt.hfrontporch ||
1797 timings->bt.hsync) &&
1798 timings->bt.vfrontporch &&
1799 (timings->bt.vbackporch ||
1800 timings->bt.vsync))) {
1801 vpif_dbg(2, debug, "Timings for width, height, "
1802 "horizontal back porch, horizontal sync, "
1803 "horizontal front porch, vertical back porch, "
1804 "vertical sync and vertical back porch "
1805 "must be defined\n");
1806 return -EINVAL;
1807 }
1808
Hans Verkuil0598c172012-09-18 07:18:47 -03001809 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001810
1811 /* Configure video port timings */
1812
1813 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1814 bt->hsync - 8;
1815 std_info->sav2eav = bt->width;
1816
1817 std_info->l1 = 1;
1818 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1819
1820 if (bt->interlaced) {
1821 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1822 std_info->vsize = bt->height * 2 +
1823 bt->vfrontporch + bt->vsync + bt->vbackporch +
1824 bt->il_vfrontporch + bt->il_vsync +
1825 bt->il_vbackporch;
1826 std_info->l5 = std_info->vsize/2 -
1827 (bt->vfrontporch - 1);
1828 std_info->l7 = std_info->vsize/2 + 1;
1829 std_info->l9 = std_info->l7 + bt->il_vsync +
1830 bt->il_vbackporch + 1;
1831 std_info->l11 = std_info->vsize -
1832 (bt->il_vfrontporch - 1);
1833 } else {
1834 vpif_dbg(2, debug, "Required timing values for "
1835 "interlaced BT format missing\n");
1836 return -EINVAL;
1837 }
1838 } else {
1839 std_info->vsize = bt->height + bt->vfrontporch +
1840 bt->vsync + bt->vbackporch;
1841 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1842 }
1843 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1844 std_info->width = bt->width;
1845 std_info->height = bt->height;
1846 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1847 std_info->ycmux_mode = 0;
1848 std_info->capture_format = 0;
1849 std_info->vbi_supported = 0;
1850 std_info->hd_sd = 1;
1851 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001852
1853 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001854 return 0;
1855}
1856
1857/**
1858 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1859 * @file: file ptr
1860 * @priv: file handle
1861 * @timings: digital video timings
1862 */
1863static int vpif_g_dv_timings(struct file *file, void *priv,
1864 struct v4l2_dv_timings *timings)
1865{
1866 struct vpif_fh *fh = priv;
1867 struct channel_obj *ch = fh->channel;
1868 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001869
Hans Verkuil0598c172012-09-18 07:18:47 -03001870 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001871
1872 return 0;
1873}
1874
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001875/*
1876 * vpif_g_chip_ident() - Identify the chip
1877 * @file: file ptr
1878 * @priv: file handle
1879 * @chip: chip identity
1880 *
1881 * Returns zero or -EINVAL if read operations fails.
1882 */
1883static int vpif_g_chip_ident(struct file *file, void *priv,
1884 struct v4l2_dbg_chip_ident *chip)
1885{
1886 chip->ident = V4L2_IDENT_NONE;
1887 chip->revision = 0;
1888 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1889 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1890 vpif_dbg(2, debug, "match_type is invalid.\n");
1891 return -EINVAL;
1892 }
1893
1894 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1895 g_chip_ident, chip);
1896}
1897
1898#ifdef CONFIG_VIDEO_ADV_DEBUG
1899/*
1900 * vpif_dbg_g_register() - Read register
1901 * @file: file ptr
1902 * @priv: file handle
1903 * @reg: register to be read
1904 *
1905 * Debugging only
1906 * Returns zero or -EINVAL if read operations fails.
1907 */
1908static int vpif_dbg_g_register(struct file *file, void *priv,
1909 struct v4l2_dbg_register *reg){
1910 struct vpif_fh *fh = priv;
1911 struct channel_obj *ch = fh->channel;
1912
Hans Verkuil178cce12012-09-20 09:06:30 -03001913 return v4l2_subdev_call(ch->sd, core, g_register, reg);
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001914}
1915
1916/*
1917 * vpif_dbg_s_register() - Write to register
1918 * @file: file ptr
1919 * @priv: file handle
1920 * @reg: register to be modified
1921 *
1922 * Debugging only
1923 * Returns zero or -EINVAL if write operations fails.
1924 */
1925static int vpif_dbg_s_register(struct file *file, void *priv,
1926 struct v4l2_dbg_register *reg){
1927 struct vpif_fh *fh = priv;
1928 struct channel_obj *ch = fh->channel;
1929
Hans Verkuil178cce12012-09-20 09:06:30 -03001930 return v4l2_subdev_call(ch->sd, core, s_register, reg);
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001931}
1932#endif
1933
1934/*
1935 * vpif_log_status() - Status information
1936 * @file: file ptr
1937 * @priv: file handle
1938 *
1939 * Returns zero.
1940 */
1941static int vpif_log_status(struct file *filep, void *priv)
1942{
1943 /* status for sub devices */
1944 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1945
1946 return 0;
1947}
1948
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001949/* vpif capture ioctl operations */
1950static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1951 .vidioc_querycap = vpif_querycap,
1952 .vidioc_g_priority = vpif_g_priority,
1953 .vidioc_s_priority = vpif_s_priority,
1954 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1955 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1956 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1957 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1958 .vidioc_enum_input = vpif_enum_input,
1959 .vidioc_s_input = vpif_s_input,
1960 .vidioc_g_input = vpif_g_input,
1961 .vidioc_reqbufs = vpif_reqbufs,
1962 .vidioc_querybuf = vpif_querybuf,
1963 .vidioc_querystd = vpif_querystd,
1964 .vidioc_s_std = vpif_s_std,
1965 .vidioc_g_std = vpif_g_std,
1966 .vidioc_qbuf = vpif_qbuf,
1967 .vidioc_dqbuf = vpif_dqbuf,
1968 .vidioc_streamon = vpif_streamon,
1969 .vidioc_streamoff = vpif_streamoff,
1970 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001971 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1972 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001973 .vidioc_s_dv_timings = vpif_s_dv_timings,
1974 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001975 .vidioc_g_chip_ident = vpif_g_chip_ident,
1976#ifdef CONFIG_VIDEO_ADV_DEBUG
1977 .vidioc_g_register = vpif_dbg_g_register,
1978 .vidioc_s_register = vpif_dbg_s_register,
1979#endif
1980 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001981};
1982
1983/* vpif file operations */
1984static struct v4l2_file_operations vpif_fops = {
1985 .owner = THIS_MODULE,
1986 .open = vpif_open,
1987 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001988 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001989 .mmap = vpif_mmap,
1990 .poll = vpif_poll
1991};
1992
1993/* vpif video template */
1994static struct video_device vpif_video_template = {
1995 .name = "vpif",
1996 .fops = &vpif_fops,
1997 .minor = -1,
1998 .ioctl_ops = &vpif_ioctl_ops,
1999};
2000
2001/**
2002 * initialize_vpif() - Initialize vpif data structures
2003 *
2004 * Allocate memory for data structures and initialize them
2005 */
2006static int initialize_vpif(void)
2007{
2008 int err = 0, i, j;
2009 int free_channel_objects_index;
2010
2011 /* Default number of buffers should be 3 */
2012 if ((ch0_numbuffers > 0) &&
2013 (ch0_numbuffers < config_params.min_numbuffers))
2014 ch0_numbuffers = config_params.min_numbuffers;
2015 if ((ch1_numbuffers > 0) &&
2016 (ch1_numbuffers < config_params.min_numbuffers))
2017 ch1_numbuffers = config_params.min_numbuffers;
2018
2019 /* Set buffer size to min buffers size if it is invalid */
2020 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2021 ch0_bufsize =
2022 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2023 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2024 ch1_bufsize =
2025 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2026
2027 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2028 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2029 if (ch0_numbuffers) {
2030 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2031 = ch0_bufsize;
2032 }
2033 if (ch1_numbuffers) {
2034 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2035 = ch1_bufsize;
2036 }
2037
2038 /* Allocate memory for six channel objects */
2039 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2040 vpif_obj.dev[i] =
2041 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2042 /* If memory allocation fails, return error */
2043 if (!vpif_obj.dev[i]) {
2044 free_channel_objects_index = i;
2045 err = -ENOMEM;
2046 goto vpif_init_free_channel_objects;
2047 }
2048 }
2049 return 0;
2050
2051vpif_init_free_channel_objects:
2052 for (j = 0; j < free_channel_objects_index; j++)
2053 kfree(vpif_obj.dev[j]);
2054 return err;
2055}
2056
2057/**
2058 * vpif_probe : This function probes the vpif capture driver
2059 * @pdev: platform device pointer
2060 *
2061 * This creates device entries by register itself to the V4L2 driver and
2062 * initializes fields of each channel objects
2063 */
2064static __init int vpif_probe(struct platform_device *pdev)
2065{
2066 struct vpif_subdev_info *subdevdata;
2067 struct vpif_capture_config *config;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002068 int i, j, k, err;
2069 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002070 struct i2c_adapter *i2c_adap;
2071 struct channel_obj *ch;
2072 struct common_obj *common;
2073 struct video_device *vfd;
2074 struct resource *res;
2075 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03002076 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002077
2078 vpif_dev = &pdev->dev;
2079
2080 err = initialize_vpif();
2081 if (err) {
2082 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2083 return err;
2084 }
2085
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002086 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2087 if (err) {
2088 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2089 return err;
2090 }
2091
Hans Verkuil5be452c2012-09-20 09:06:29 -03002092 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002093 for (i = res->start; i <= res->end; i++) {
Manjunath Hadli0316b892012-04-13 04:44:31 -03002094 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
Hans Verkuil5be452c2012-09-20 09:06:29 -03002095 "VPIF_Capture", (void *)
2096 (&vpif_obj.dev[res_idx]->channel_id))) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002097 err = -EBUSY;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002098 for (j = 0; j < i; j++)
2099 free_irq(j, (void *)
2100 (&vpif_obj.dev[res_idx]->channel_id));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002101 goto vpif_int_err;
2102 }
2103 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002104 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002105 }
2106
2107 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2108 /* Get the pointer to the channel object */
2109 ch = vpif_obj.dev[i];
2110 /* Allocate memory for video device */
2111 vfd = video_device_alloc();
2112 if (NULL == vfd) {
2113 for (j = 0; j < i; j++) {
2114 ch = vpif_obj.dev[j];
2115 video_device_release(ch->video_dev);
2116 }
2117 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002118 goto vpif_int_err;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002119 }
2120
2121 /* Initialize field of video device */
2122 *vfd = vpif_video_template;
2123 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2124 vfd->release = video_device_release;
2125 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002126 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002127 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002128 /* Set video_dev to the video device */
2129 ch->video_dev = vfd;
2130 }
2131
Manjunath Hadli764af392012-04-13 04:49:34 -03002132 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2133 if (res) {
2134 size = resource_size(res);
2135 /* The resources are divided into two equal memory and when we
2136 * have HD output we can add them together
2137 */
2138 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2139 ch = vpif_obj.dev[j];
2140 ch->channel_id = j;
2141 /* only enabled if second resource exists */
2142 config_params.video_limit[ch->channel_id] = 0;
2143 if (size)
2144 config_params.video_limit[ch->channel_id] =
2145 size/2;
2146 }
2147 }
2148
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002149 i2c_adap = i2c_get_adapter(1);
2150 config = pdev->dev.platform_data;
2151
2152 subdev_count = config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002153 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002154 GFP_KERNEL);
2155 if (vpif_obj.sd == NULL) {
2156 vpif_err("unable to allocate memory for subdevice pointers\n");
2157 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002158 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002159 }
2160
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002161 for (i = 0; i < subdev_count; i++) {
2162 subdevdata = &config->subdev_info[i];
2163 vpif_obj.sd[i] =
2164 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2165 i2c_adap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002166 &subdevdata->board_info,
2167 NULL);
2168
2169 if (!vpif_obj.sd[i]) {
2170 vpif_err("Error registering v4l2 subdevice\n");
2171 goto probe_subdev_out;
2172 }
2173 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2174 subdevdata->name);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002175 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002176
Hans Verkuilb65814e2012-09-20 09:06:26 -03002177 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2178 ch = vpif_obj.dev[j];
2179 ch->channel_id = j;
2180 common = &(ch->common[VPIF_VIDEO_INDEX]);
2181 spin_lock_init(&common->irqlock);
2182 mutex_init(&common->lock);
2183 ch->video_dev->lock = &common->lock;
2184 /* Initialize prio member of channel object */
2185 v4l2_prio_init(&ch->prio);
2186 video_set_drvdata(ch->video_dev, ch);
2187
Hans Verkuil178cce12012-09-20 09:06:30 -03002188 /* select input 0 */
2189 err = vpif_set_input(config, ch, 0);
2190 if (err)
2191 goto probe_out;
2192
Hans Verkuilb65814e2012-09-20 09:06:26 -03002193 err = video_register_device(ch->video_dev,
2194 VFL_TYPE_GRABBER, (j ? 1 : 0));
2195 if (err)
2196 goto probe_out;
2197 }
Manjunath Hadli0a631722012-04-13 04:44:00 -03002198 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002199 return 0;
2200
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002201probe_out:
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002202 for (k = 0; k < j; k++) {
2203 /* Get the pointer to the channel object */
2204 ch = vpif_obj.dev[k];
2205 /* Unregister video device */
2206 video_unregister_device(ch->video_dev);
2207 }
Hans Verkuilb65814e2012-09-20 09:06:26 -03002208probe_subdev_out:
2209 /* free sub devices memory */
2210 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002211
Hans Verkuil5be452c2012-09-20 09:06:29 -03002212vpif_sd_error:
2213 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2214 ch = vpif_obj.dev[i];
2215 /* Note: does nothing if ch->video_dev == NULL */
2216 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002217 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002218vpif_int_err:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002219 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Hans Verkuil5be452c2012-09-20 09:06:29 -03002220 for (i = 0; i < res_idx; i++) {
2221 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
2222 for (j = res->start; j <= res->end; j++)
2223 free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
2224 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002225 return err;
2226}
2227
2228/**
2229 * vpif_remove() - driver remove handler
2230 * @device: ptr to platform device structure
2231 *
2232 * The vidoe device is unregistered
2233 */
2234static int vpif_remove(struct platform_device *device)
2235{
2236 int i;
2237 struct channel_obj *ch;
2238
2239 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2240
2241 /* un-register device */
2242 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2243 /* Get the pointer to the channel object */
2244 ch = vpif_obj.dev[i];
2245 /* Unregister video device */
2246 video_unregister_device(ch->video_dev);
2247 }
2248 return 0;
2249}
2250
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002251#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002252/**
2253 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002254 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002255static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002256{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002257
2258 struct common_obj *common;
2259 struct channel_obj *ch;
2260 int i;
2261
2262 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2263 /* Get the pointer to the channel object */
2264 ch = vpif_obj.dev[i];
2265 common = &ch->common[VPIF_VIDEO_INDEX];
2266 mutex_lock(&common->lock);
2267 if (ch->usrs && common->io_usrs) {
2268 /* Disable channel */
2269 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2270 enable_channel0(0);
2271 channel0_intr_enable(0);
2272 }
2273 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2274 common->started == 2) {
2275 enable_channel1(0);
2276 channel1_intr_enable(0);
2277 }
2278 }
2279 mutex_unlock(&common->lock);
2280 }
2281
2282 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002283}
2284
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002285/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002286 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002287 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002288static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002289{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002290 struct common_obj *common;
2291 struct channel_obj *ch;
2292 int i;
2293
2294 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2295 /* Get the pointer to the channel object */
2296 ch = vpif_obj.dev[i];
2297 common = &ch->common[VPIF_VIDEO_INDEX];
2298 mutex_lock(&common->lock);
2299 if (ch->usrs && common->io_usrs) {
2300 /* Disable channel */
2301 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2302 enable_channel0(1);
2303 channel0_intr_enable(1);
2304 }
2305 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2306 common->started == 2) {
2307 enable_channel1(1);
2308 channel1_intr_enable(1);
2309 }
2310 }
2311 mutex_unlock(&common->lock);
2312 }
2313
2314 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002315}
2316
Alexey Dobriyan47145212009-12-14 18:00:08 -08002317static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002318 .suspend = vpif_suspend,
2319 .resume = vpif_resume,
2320};
2321
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002322#define vpif_pm_ops (&vpif_dev_pm_ops)
2323#else
2324#define vpif_pm_ops NULL
2325#endif
2326
Mats Randgaardffa1b392010-08-30 10:30:36 -03002327static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002328 .driver = {
2329 .name = "vpif_capture",
2330 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002331 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002332 },
2333 .probe = vpif_probe,
2334 .remove = vpif_remove,
2335};
2336
2337/**
2338 * vpif_init: initialize the vpif driver
2339 *
2340 * This function registers device and driver to the kernel, requests irq
2341 * handler and allocates memory
2342 * for channel objects
2343 */
2344static __init int vpif_init(void)
2345{
2346 return platform_driver_register(&vpif_driver);
2347}
2348
2349/**
2350 * vpif_cleanup : This function clean up the vpif capture resources
2351 *
2352 * This will un-registers device and driver to the kernel, frees
2353 * requested irq handler and de-allocates memory allocated for channel
2354 * objects.
2355 */
2356static void vpif_cleanup(void)
2357{
2358 struct platform_device *pdev;
2359 struct resource *res;
2360 int irq_num;
2361 int i = 0;
2362
2363 pdev = container_of(vpif_dev, struct platform_device, dev);
2364 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2365 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2366 free_irq(irq_num,
2367 (void *)(&vpif_obj.dev[i]->channel_id));
2368 i++;
2369 }
2370
2371 platform_driver_unregister(&vpif_driver);
2372
2373 kfree(vpif_obj.sd);
2374 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2375 kfree(vpif_obj.dev[i]);
2376}
2377
2378/* Function for module initialization and cleanup */
2379module_init(vpif_init);
2380module_exit(vpif_cleanup);