blob: a1b42b037678b52e561d289829ec9c3f00e5e1a2 [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 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030021
Lad, Prabhakar012eef72013-04-19 05:53:29 -030022#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26
27#include <media/v4l2-chip-ident.h>
28#include <media/v4l2-ioctl.h>
29
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030030#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030031#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030032
33MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030035MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030036
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
41static int debug = 1;
42static u32 ch0_numbuffers = 3;
43static u32 ch1_numbuffers = 3;
44static u32 ch0_bufsize = 1920 * 1080 * 2;
45static u32 ch1_bufsize = 720 * 576 * 2;
46
47module_param(debug, int, 0644);
48module_param(ch0_numbuffers, uint, S_IRUGO);
49module_param(ch1_numbuffers, uint, S_IRUGO);
50module_param(ch0_bufsize, uint, S_IRUGO);
51module_param(ch1_bufsize, uint, S_IRUGO);
52
53MODULE_PARM_DESC(debug, "Debug level 0-1");
54MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
55MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
56MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
57MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
58
59static struct vpif_config_params config_params = {
60 .min_numbuffers = 3,
61 .numbuffers[0] = 3,
62 .numbuffers[1] = 3,
63 .min_bufsize[0] = 720 * 480 * 2,
64 .min_bufsize[1] = 720 * 480 * 2,
65 .channel_bufsize[0] = 1920 * 1080 * 2,
66 .channel_bufsize[1] = 720 * 576 * 2,
67};
68
69/* global variables */
70static struct vpif_device vpif_obj = { {NULL} };
71static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030072static void vpif_calculate_offsets(struct channel_obj *ch);
73static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030074
75/**
76 * buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030077 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030078 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030079 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030080 * function is called. The buffer is prepared and user space virtual address
81 * or user address is converted into physical address
82 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030083static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030084{
85 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030086 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
87 struct vb2_queue *q = vb->vb2_queue;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030088 struct channel_obj *ch = fh->channel;
89 struct common_obj *common;
90 unsigned long addr;
91
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030092 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
93
94 common = &ch->common[VPIF_VIDEO_INDEX];
95
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030096 if (vb->state != VB2_BUF_STATE_ACTIVE &&
97 vb->state != VB2_BUF_STATE_PREPARED) {
98 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
99 if (vb2_plane_vaddr(vb, 0) &&
100 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
101 goto exit;
102 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300103
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300104 if (q->streaming) {
105 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
106 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
107 !IS_ALIGNED((addr + common->ctop_off), 8) ||
108 !IS_ALIGNED((addr + common->cbtm_off), 8))
109 goto exit;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300110 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300111 }
112 return 0;
113exit:
114 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
115 return -EINVAL;
116}
117
118/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300119 * vpif_buffer_queue_setup : Callback function for buffer setup.
120 * @vq: vb2_queue ptr
121 * @fmt: v4l2 format
122 * @nbuffers: ptr to number of buffers requested by application
123 * @nplanes:: contains number of distinct video planes needed to hold a frame
124 * @sizes[]: contains the size (in bytes) of each plane.
125 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300126 *
127 * This callback function is called when reqbuf() is called to adjust
128 * the buffer count and buffer size
129 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300130static int vpif_buffer_queue_setup(struct vb2_queue *vq,
131 const struct v4l2_format *fmt,
132 unsigned int *nbuffers, unsigned int *nplanes,
133 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300134{
135 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300136 struct vpif_fh *fh = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300137 struct channel_obj *ch = fh->channel;
138 struct common_obj *common;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300139 unsigned long size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300140
141 common = &ch->common[VPIF_VIDEO_INDEX];
142
143 vpif_dbg(2, debug, "vpif_buffer_setup\n");
144
145 /* If memory type is not mmap, return */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300146 if (V4L2_MEMORY_MMAP == common->memory) {
147 /* Calculate the size of the buffer */
148 size = config_params.channel_bufsize[ch->channel_id];
149 /*
150 * Checking if the buffer size exceeds the available buffer
151 * ycmux_mode = 0 means 1 channel mode HD and
152 * ycmux_mode = 1 means 2 channels mode SD
153 */
154 if (ch->vpifparams.std_info.ycmux_mode == 0) {
155 if (config_params.video_limit[ch->channel_id])
156 while (size * *nbuffers >
157 (config_params.video_limit[0]
Manjunath Hadli764af392012-04-13 04:49:34 -0300158 + config_params.video_limit[1]))
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300159 (*nbuffers)--;
160 } else {
161 if (config_params.video_limit[ch->channel_id])
162 while (size * *nbuffers >
Manjunath Hadli764af392012-04-13 04:49:34 -0300163 config_params.video_limit[ch->channel_id])
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300164 (*nbuffers)--;
165 }
166
167 } else {
168 size = common->fmt.fmt.pix.sizeimage;
Manjunath Hadli764af392012-04-13 04:49:34 -0300169 }
170
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300171 if (*nbuffers < config_params.min_numbuffers)
172 *nbuffers = config_params.min_numbuffers;
173
174 *nplanes = 1;
175 sizes[0] = size;
176 alloc_ctxs[0] = common->alloc_ctx;
177
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300178 return 0;
179}
180
181/**
182 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300183 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300184 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300185static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300186{
187 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300188 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300189 struct channel_obj *ch = fh->channel;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300190 struct vpif_cap_buffer *buf = container_of(vb,
191 struct vpif_cap_buffer, vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300192 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300193 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300194
195 common = &ch->common[VPIF_VIDEO_INDEX];
196
197 vpif_dbg(2, debug, "vpif_buffer_queue\n");
198
Hans Verkuilaec96832012-11-16 12:03:06 -0300199 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300200 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300201 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300202 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300203}
204
205/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300206 * vpif_buf_cleanup : Callback function to free buffer
207 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300208 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300209 * This function is called from the videobuf2 layer to free memory
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300210 * allocated to the buffers
211 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300212static void vpif_buf_cleanup(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300213{
214 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300215 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
216 struct vpif_cap_buffer *buf = container_of(vb,
217 struct vpif_cap_buffer, vb);
218 struct channel_obj *ch = fh->channel;
219 struct common_obj *common;
220 unsigned long flags;
221
222 common = &ch->common[VPIF_VIDEO_INDEX];
223
224 spin_lock_irqsave(&common->irqlock, flags);
225 if (vb->state == VB2_BUF_STATE_ACTIVE)
226 list_del_init(&buf->list);
227 spin_unlock_irqrestore(&common->irqlock, flags);
228
229}
230
231static void vpif_wait_prepare(struct vb2_queue *vq)
232{
233 struct vpif_fh *fh = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300234 struct channel_obj *ch = fh->channel;
235 struct common_obj *common;
236
237 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300238 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300239}
240
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300241static void vpif_wait_finish(struct vb2_queue *vq)
242{
243 struct vpif_fh *fh = vb2_get_drv_priv(vq);
244 struct channel_obj *ch = fh->channel;
245 struct common_obj *common;
246
247 common = &ch->common[VPIF_VIDEO_INDEX];
248 mutex_lock(&common->lock);
249}
250
251static int vpif_buffer_init(struct vb2_buffer *vb)
252{
253 struct vpif_cap_buffer *buf = container_of(vb,
254 struct vpif_cap_buffer, vb);
255
256 INIT_LIST_HEAD(&buf->list);
257
258 return 0;
259}
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300260
261static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
262 { {1, 1} };
263
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300264static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
265{
266 struct vpif_capture_config *vpif_config_data =
267 vpif_dev->platform_data;
268 struct vpif_fh *fh = vb2_get_drv_priv(vq);
269 struct channel_obj *ch = fh->channel;
270 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
271 struct vpif_params *vpif = &ch->vpifparams;
272 unsigned long addr = 0;
Hans Verkuilaec96832012-11-16 12:03:06 -0300273 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300274 int ret;
275
Hans Verkuilaec96832012-11-16 12:03:06 -0300276 /* If buffer queue is empty, return error */
277 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300278 if (list_empty(&common->dma_queue)) {
Hans Verkuilaec96832012-11-16 12:03:06 -0300279 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300280 vpif_dbg(1, debug, "buffer queue is empty\n");
281 return -EIO;
282 }
283
284 /* Get the next frame from the buffer queue */
285 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
286 struct vpif_cap_buffer, list);
287 /* Remove buffer from the buffer queue */
288 list_del(&common->cur_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300289 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300290 /* Mark state of the current frame to active */
291 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
292 /* Initialize field_id and started member */
293 ch->field_id = 0;
294 common->started = 1;
295 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
296
297 /* Calculate the offset for Y and C data in the buffer */
298 vpif_calculate_offsets(ch);
299
300 if ((vpif->std_info.frm_fmt &&
301 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
302 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
303 (!vpif->std_info.frm_fmt &&
304 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
305 vpif_dbg(1, debug, "conflict in field format and std format\n");
306 return -EINVAL;
307 }
308
309 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300310 if (vpif_config_data->setup_input_channel_mode) {
311 ret = vpif_config_data->
312 setup_input_channel_mode(vpif->std_info.ycmux_mode);
313 if (ret < 0) {
314 vpif_dbg(1, debug, "can't set vpif channel mode\n");
315 return ret;
316 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300317 }
318
319 /* Call vpif_set_params function to set the parameters and addresses */
320 ret = vpif_set_video_params(vpif, ch->channel_id);
321
322 if (ret < 0) {
323 vpif_dbg(1, debug, "can't set video params\n");
324 return ret;
325 }
326
327 common->started = ret;
328 vpif_config_addr(ch, ret);
329
330 common->set_addr(addr + common->ytop_off,
331 addr + common->ybtm_off,
332 addr + common->ctop_off,
333 addr + common->cbtm_off);
334
335 /**
336 * Set interrupt for both the fields in VPIF Register enable channel in
337 * VPIF register
338 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300339 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300340 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
341 channel0_intr_assert();
342 channel0_intr_enable(1);
343 enable_channel0(1);
344 }
345 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
346 (common->started == 2)) {
347 channel1_intr_assert();
348 channel1_intr_enable(1);
349 enable_channel1(1);
350 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300351
352 return 0;
353}
354
355/* abort streaming and wait for last buffer */
356static int vpif_stop_streaming(struct vb2_queue *vq)
357{
358 struct vpif_fh *fh = vb2_get_drv_priv(vq);
359 struct channel_obj *ch = fh->channel;
360 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300361 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300362
363 if (!vb2_is_streaming(vq))
364 return 0;
365
366 common = &ch->common[VPIF_VIDEO_INDEX];
367
368 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300369 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300370 while (!list_empty(&common->dma_queue)) {
371 common->next_frm = list_entry(common->dma_queue.next,
372 struct vpif_cap_buffer, list);
373 list_del(&common->next_frm->list);
374 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
375 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300376 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300377
378 return 0;
379}
380
381static struct vb2_ops video_qops = {
382 .queue_setup = vpif_buffer_queue_setup,
383 .wait_prepare = vpif_wait_prepare,
384 .wait_finish = vpif_wait_finish,
385 .buf_init = vpif_buffer_init,
386 .buf_prepare = vpif_buffer_prepare,
387 .start_streaming = vpif_start_streaming,
388 .stop_streaming = vpif_stop_streaming,
389 .buf_cleanup = vpif_buf_cleanup,
390 .buf_queue = vpif_buffer_queue,
391};
392
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300393/**
394 * vpif_process_buffer_complete: process a completed buffer
395 * @common: ptr to common channel object
396 *
397 * This function time stamp the buffer and mark it as DONE. It also
398 * wake up any process waiting on the QUEUE and set the next buffer
399 * as current
400 */
401static void vpif_process_buffer_complete(struct common_obj *common)
402{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300403 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300404 vb2_buffer_done(&common->cur_frm->vb,
405 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300406 /* Make curFrm pointing to nextFrm */
407 common->cur_frm = common->next_frm;
408}
409
410/**
411 * vpif_schedule_next_buffer: set next buffer address for capture
412 * @common : ptr to common channel object
413 *
414 * This function will get next buffer from the dma queue and
415 * set the buffer address in the vpif register for capture.
416 * the buffer is marked active
417 */
418static void vpif_schedule_next_buffer(struct common_obj *common)
419{
420 unsigned long addr = 0;
421
Hans Verkuilaec96832012-11-16 12:03:06 -0300422 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300423 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300424 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300425 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300426 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300427 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300428 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
429 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300430
431 /* Set top and bottom field addresses in VPIF registers */
432 common->set_addr(addr + common->ytop_off,
433 addr + common->ybtm_off,
434 addr + common->ctop_off,
435 addr + common->cbtm_off);
436}
437
438/**
439 * vpif_channel_isr : ISR handler for vpif capture
440 * @irq: irq number
441 * @dev_id: dev_id ptr
442 *
443 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300444 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300445 */
446static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
447{
448 struct vpif_device *dev = &vpif_obj;
449 struct common_obj *common;
450 struct channel_obj *ch;
451 enum v4l2_field field;
452 int channel_id = 0;
453 int fid = -1, i;
454
455 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300456 if (!vpif_intr_status(channel_id))
457 return IRQ_NONE;
458
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300459 ch = dev->dev[channel_id];
460
461 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
462
463 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
464 common = &ch->common[i];
465 /* skip If streaming is not started in this channel */
466 if (0 == common->started)
467 continue;
468
469 /* Check the field format */
470 if (1 == ch->vpifparams.std_info.frm_fmt) {
471 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300472 spin_lock(&common->irqlock);
473 if (list_empty(&common->dma_queue)) {
474 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300475 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300476 }
477 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300478
479 if (!channel_first_int[i][channel_id])
480 vpif_process_buffer_complete(common);
481
482 channel_first_int[i][channel_id] = 0;
483
484 vpif_schedule_next_buffer(common);
485
486
487 channel_first_int[i][channel_id] = 0;
488 } else {
489 /**
490 * Interlaced mode. If it is first interrupt, ignore
491 * it
492 */
493 if (channel_first_int[i][channel_id]) {
494 channel_first_int[i][channel_id] = 0;
495 continue;
496 }
497 if (0 == i) {
498 ch->field_id ^= 1;
499 /* Get field id from VPIF registers */
500 fid = vpif_channel_getfid(ch->channel_id);
501 if (fid != ch->field_id) {
502 /**
503 * If field id does not match stored
504 * field id, make them in sync
505 */
506 if (0 == fid)
507 ch->field_id = fid;
508 return IRQ_HANDLED;
509 }
510 }
511 /* device field id and local field id are in sync */
512 if (0 == fid) {
513 /* this is even field */
514 if (common->cur_frm == common->next_frm)
515 continue;
516
517 /* mark the current buffer as done */
518 vpif_process_buffer_complete(common);
519 } else if (1 == fid) {
520 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300521 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300522 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300523 (common->cur_frm != common->next_frm)) {
524 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300525 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300526 }
527 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300528
529 vpif_schedule_next_buffer(common);
530 }
531 }
532 }
533 return IRQ_HANDLED;
534}
535
536/**
537 * vpif_update_std_info() - update standard related info
538 * @ch: ptr to channel object
539 *
540 * For a given standard selected by application, update values
541 * in the device data structures
542 */
543static int vpif_update_std_info(struct channel_obj *ch)
544{
545 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
546 struct vpif_params *vpifparams = &ch->vpifparams;
547 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300548 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300549 struct video_obj *vid_ch = &ch->video;
550 int index;
551
552 vpif_dbg(2, debug, "vpif_update_std_info\n");
553
Mats Randgaardaa444402010-12-16 12:17:42 -0300554 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300555 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300556 if (config->hd_sd == 0) {
557 vpif_dbg(2, debug, "SD format\n");
558 if (config->stdid & vid_ch->stdid) {
559 memcpy(std_info, config, sizeof(*config));
560 break;
561 }
562 } else {
563 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300564 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
565 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300566 memcpy(std_info, config, sizeof(*config));
567 break;
568 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300569 }
570 }
571
572 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300573 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300574 return -EINVAL;
575
576 common->fmt.fmt.pix.width = std_info->width;
577 common->width = std_info->width;
578 common->fmt.fmt.pix.height = std_info->height;
579 common->height = std_info->height;
580 common->fmt.fmt.pix.bytesperline = std_info->width;
581 vpifparams->video_params.hpitch = std_info->width;
582 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300583
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300584 return 0;
585}
586
587/**
588 * vpif_calculate_offsets : This function calculates buffers offsets
589 * @ch : ptr to channel object
590 *
591 * This function calculates buffer offsets for Y and C in the top and
592 * bottom field
593 */
594static void vpif_calculate_offsets(struct channel_obj *ch)
595{
596 unsigned int hpitch, vpitch, sizeimage;
597 struct video_obj *vid_ch = &(ch->video);
598 struct vpif_params *vpifparams = &ch->vpifparams;
599 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
600 enum v4l2_field field = common->fmt.fmt.pix.field;
601
602 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
603
604 if (V4L2_FIELD_ANY == field) {
605 if (vpifparams->std_info.frm_fmt)
606 vid_ch->buf_field = V4L2_FIELD_NONE;
607 else
608 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
609 } else
610 vid_ch->buf_field = common->fmt.fmt.pix.field;
611
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300612 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300613
614 hpitch = common->fmt.fmt.pix.bytesperline;
615 vpitch = sizeimage / (hpitch * 2);
616
617 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
618 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
619 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
620 common->ytop_off = 0;
621 common->ybtm_off = hpitch;
622 common->ctop_off = sizeimage / 2;
623 common->cbtm_off = sizeimage / 2 + hpitch;
624 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
625 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
626 common->ytop_off = 0;
627 common->ybtm_off = sizeimage / 4;
628 common->ctop_off = sizeimage / 2;
629 common->cbtm_off = common->ctop_off + sizeimage / 4;
630 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
631 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
632 common->ybtm_off = 0;
633 common->ytop_off = sizeimage / 4;
634 common->cbtm_off = sizeimage / 2;
635 common->ctop_off = common->cbtm_off + sizeimage / 4;
636 }
637 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
638 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
639 vpifparams->video_params.storage_mode = 1;
640 else
641 vpifparams->video_params.storage_mode = 0;
642
643 if (1 == vpifparams->std_info.frm_fmt)
644 vpifparams->video_params.hpitch =
645 common->fmt.fmt.pix.bytesperline;
646 else {
647 if ((field == V4L2_FIELD_ANY)
648 || (field == V4L2_FIELD_INTERLACED))
649 vpifparams->video_params.hpitch =
650 common->fmt.fmt.pix.bytesperline * 2;
651 else
652 vpifparams->video_params.hpitch =
653 common->fmt.fmt.pix.bytesperline;
654 }
655
656 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
657}
658
659/**
660 * vpif_config_format: configure default frame format in the device
661 * ch : ptr to channel object
662 */
663static void vpif_config_format(struct channel_obj *ch)
664{
665 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
666
667 vpif_dbg(2, debug, "vpif_config_format\n");
668
669 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
670 if (config_params.numbuffers[ch->channel_id] == 0)
671 common->memory = V4L2_MEMORY_USERPTR;
672 else
673 common->memory = V4L2_MEMORY_MMAP;
674
675 common->fmt.fmt.pix.sizeimage
676 = config_params.channel_bufsize[ch->channel_id];
677
678 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
679 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
680 else
681 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
682 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
683}
684
685/**
686 * vpif_get_default_field() - Get default field type based on interface
687 * @vpif_params - ptr to vpif params
688 */
689static inline enum v4l2_field vpif_get_default_field(
690 struct vpif_interface *iface)
691{
692 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
693 V4L2_FIELD_INTERLACED;
694}
695
696/**
697 * vpif_check_format() - check given pixel format for compatibility
698 * @ch - channel ptr
699 * @pixfmt - Given pixel format
700 * @update - update the values as per hardware requirement
701 *
702 * Check the application pixel format for S_FMT and update the input
703 * values as per hardware limits for TRY_FMT. The default pixel and
704 * field format is selected based on interface type.
705 */
706static int vpif_check_format(struct channel_obj *ch,
707 struct v4l2_pix_format *pixfmt,
708 int update)
709{
710 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
711 struct vpif_params *vpif_params = &ch->vpifparams;
712 enum v4l2_field field = pixfmt->field;
713 u32 sizeimage, hpitch, vpitch;
714 int ret = -EINVAL;
715
716 vpif_dbg(2, debug, "vpif_check_format\n");
717 /**
718 * first check for the pixel format. If if_type is Raw bayer,
719 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
720 * V4L2_PIX_FMT_YUV422P is supported
721 */
722 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
723 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
724 if (!update) {
725 vpif_dbg(2, debug, "invalid pix format\n");
726 goto exit;
727 }
728 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
729 }
730 } else {
731 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
732 if (!update) {
733 vpif_dbg(2, debug, "invalid pixel format\n");
734 goto exit;
735 }
736 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
737 }
738 }
739
740 if (!(VPIF_VALID_FIELD(field))) {
741 if (!update) {
742 vpif_dbg(2, debug, "invalid field format\n");
743 goto exit;
744 }
745 /**
746 * By default use FIELD_NONE for RAW Bayer capture
747 * and FIELD_INTERLACED for other interfaces
748 */
749 field = vpif_get_default_field(&vpif_params->iface);
750 } else if (field == V4L2_FIELD_ANY)
751 /* unsupported field. Use default */
752 field = vpif_get_default_field(&vpif_params->iface);
753
754 /* validate the hpitch */
755 hpitch = pixfmt->bytesperline;
756 if (hpitch < vpif_params->std_info.width) {
757 if (!update) {
758 vpif_dbg(2, debug, "invalid hpitch\n");
759 goto exit;
760 }
761 hpitch = vpif_params->std_info.width;
762 }
763
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300764 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300765
766 vpitch = sizeimage / (hpitch * 2);
767
768 /* validate the vpitch */
769 if (vpitch < vpif_params->std_info.height) {
770 if (!update) {
771 vpif_dbg(2, debug, "Invalid vpitch\n");
772 goto exit;
773 }
774 vpitch = vpif_params->std_info.height;
775 }
776
777 /* Check for 8 byte alignment */
778 if (!ALIGN(hpitch, 8)) {
779 if (!update) {
780 vpif_dbg(2, debug, "invalid pitch alignment\n");
781 goto exit;
782 }
783 /* adjust to next 8 byte boundary */
784 hpitch = (((hpitch + 7) / 8) * 8);
785 }
786 /* if update is set, modify the bytesperline and sizeimage */
787 if (update) {
788 pixfmt->bytesperline = hpitch;
789 pixfmt->sizeimage = hpitch * vpitch * 2;
790 }
791 /**
792 * Image width and height is always based on current standard width and
793 * height
794 */
795 pixfmt->width = common->fmt.fmt.pix.width;
796 pixfmt->height = common->fmt.fmt.pix.height;
797 return 0;
798exit:
799 return ret;
800}
801
802/**
803 * vpif_config_addr() - function to configure buffer address in vpif
804 * @ch - channel ptr
805 * @muxmode - channel mux mode
806 */
807static void vpif_config_addr(struct channel_obj *ch, int muxmode)
808{
809 struct common_obj *common;
810
811 vpif_dbg(2, debug, "vpif_config_addr\n");
812
813 common = &(ch->common[VPIF_VIDEO_INDEX]);
814
815 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
816 common->set_addr = ch1_set_videobuf_addr;
817 else if (2 == muxmode)
818 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
819 else
820 common->set_addr = ch0_set_videobuf_addr;
821}
822
823/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300824 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300825 * @filep: file pointer
826 * @vma: ptr to vm_area_struct
827 */
828static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
829{
830 /* Get the channel object and file handle object */
831 struct vpif_fh *fh = filep->private_data;
832 struct channel_obj *ch = fh->channel;
833 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300834 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300835
836 vpif_dbg(2, debug, "vpif_mmap\n");
837
Hans Verkuil72246792012-07-31 03:48:31 -0300838 if (mutex_lock_interruptible(&common->lock))
839 return -ERESTARTSYS;
840 ret = vb2_mmap(&common->buffer_queue, vma);
841 mutex_unlock(&common->lock);
842 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300843}
844
845/**
846 * vpif_poll: It is used for select/poll system call
847 * @filep: file pointer
848 * @wait: poll table to wait
849 */
850static unsigned int vpif_poll(struct file *filep, poll_table * wait)
851{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300852 struct vpif_fh *fh = filep->private_data;
853 struct channel_obj *channel = fh->channel;
854 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300855 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300856
857 vpif_dbg(2, debug, "vpif_poll\n");
858
Hans Verkuil72246792012-07-31 03:48:31 -0300859 if (common->started) {
860 mutex_lock(&common->lock);
861 res = vb2_poll(&common->buffer_queue, filep, wait);
862 mutex_unlock(&common->lock);
863 }
864 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300865}
866
867/**
868 * vpif_open : vpif open handler
869 * @filep: file ptr
870 *
871 * It creates object of file handle structure and stores it in private_data
872 * member of filepointer
873 */
874static int vpif_open(struct file *filep)
875{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300876 struct video_device *vdev = video_devdata(filep);
877 struct common_obj *common;
878 struct video_obj *vid_ch;
879 struct channel_obj *ch;
880 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300881
882 vpif_dbg(2, debug, "vpif_open\n");
883
884 ch = video_get_drvdata(vdev);
885
886 vid_ch = &ch->video;
887 common = &ch->common[VPIF_VIDEO_INDEX];
888
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300889 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300890 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300891 if (NULL == fh) {
892 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300893 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300894 }
895
Hans Verkuil72246792012-07-31 03:48:31 -0300896 if (mutex_lock_interruptible(&common->lock)) {
897 kfree(fh);
898 return -ERESTARTSYS;
899 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300900 /* store pointer to fh in private_data member of filep */
901 filep->private_data = fh;
902 fh->channel = ch;
903 fh->initialized = 0;
904 /* If decoder is not initialized. initialize it */
905 if (!ch->initialized) {
906 fh->initialized = 1;
907 ch->initialized = 1;
908 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
909 }
910 /* Increment channel usrs counter */
911 ch->usrs++;
912 /* Set io_allowed member to false */
913 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
914 /* Initialize priority of this instance to default priority */
915 fh->prio = V4L2_PRIORITY_UNSET;
916 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300917 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300918 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300919}
920
921/**
922 * vpif_release : function to clean up file close
923 * @filep: file pointer
924 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300925 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300926 * handle
927 */
928static int vpif_release(struct file *filep)
929{
930 struct vpif_fh *fh = filep->private_data;
931 struct channel_obj *ch = fh->channel;
932 struct common_obj *common;
933
934 vpif_dbg(2, debug, "vpif_release\n");
935
936 common = &ch->common[VPIF_VIDEO_INDEX];
937
Hans Verkuil72246792012-07-31 03:48:31 -0300938 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300939 /* if this instance is doing IO */
940 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
941 /* Reset io_usrs member of channel object */
942 common->io_usrs = 0;
943 /* Disable channel as per its device type and channel id */
944 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
945 enable_channel0(0);
946 channel0_intr_enable(0);
947 }
948 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
949 (2 == common->started)) {
950 enable_channel1(0);
951 channel1_intr_enable(0);
952 }
953 common->started = 0;
954 /* Free buffers allocated */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300955 vb2_queue_release(&common->buffer_queue);
956 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300957 }
958
959 /* Decrement channel usrs counter */
960 ch->usrs--;
961
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300962 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300963 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300964
965 if (fh->initialized)
966 ch->initialized = 0;
967
Hans Verkuil72246792012-07-31 03:48:31 -0300968 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300969 filep->private_data = NULL;
970 kfree(fh);
971 return 0;
972}
973
974/**
975 * vpif_reqbufs() - request buffer handler
976 * @file: file ptr
977 * @priv: file handle
978 * @reqbuf: request buffer structure ptr
979 */
980static int vpif_reqbufs(struct file *file, void *priv,
981 struct v4l2_requestbuffers *reqbuf)
982{
983 struct vpif_fh *fh = priv;
984 struct channel_obj *ch = fh->channel;
985 struct common_obj *common;
986 u8 index = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300987 struct vb2_queue *q;
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -0300988 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300989
990 vpif_dbg(2, debug, "vpif_reqbufs\n");
991
992 /**
993 * This file handle has not initialized the channel,
994 * It is not allowed to do settings
995 */
996 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
997 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
998 if (!fh->initialized) {
999 vpif_dbg(1, debug, "Channel Busy\n");
1000 return -EBUSY;
1001 }
1002 }
1003
Manjunath Hadli764af392012-04-13 04:49:34 -03001004 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001005 return -EINVAL;
1006
1007 index = VPIF_VIDEO_INDEX;
1008
1009 common = &ch->common[index];
1010
Hans Verkuil46656af2011-01-04 06:51:35 -03001011 if (0 != common->io_usrs)
1012 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001013
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001014 /* Initialize videobuf2 queue as per the buffer type */
1015 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
Wei Yongjun94b76a82012-11-15 09:18:17 -03001016 if (IS_ERR(common->alloc_ctx)) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001017 vpif_err("Failed to get the context\n");
Wei Yongjun94b76a82012-11-15 09:18:17 -03001018 return PTR_ERR(common->alloc_ctx);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001019 }
1020 q = &common->buffer_queue;
1021 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1022 q->io_modes = VB2_MMAP | VB2_USERPTR;
1023 q->drv_priv = fh;
1024 q->ops = &video_qops;
1025 q->mem_ops = &vb2_dma_contig_memops;
1026 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
Kamil Debski6aa69f92013-01-25 06:29:57 -03001027 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001028
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -03001029 ret = vb2_queue_init(q);
1030 if (ret) {
1031 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1032 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1033 return ret;
1034 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001035 /* Set io allowed member of file handle to TRUE */
1036 fh->io_allowed[index] = 1;
1037 /* Increment io usrs member of channel object to 1 */
1038 common->io_usrs = 1;
1039 /* Store type of memory requested in channel object */
1040 common->memory = reqbuf->memory;
1041 INIT_LIST_HEAD(&common->dma_queue);
1042
1043 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001044 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001045}
1046
1047/**
1048 * vpif_querybuf() - query buffer handler
1049 * @file: file ptr
1050 * @priv: file handle
1051 * @buf: v4l2 buffer structure ptr
1052 */
1053static int vpif_querybuf(struct file *file, void *priv,
1054 struct v4l2_buffer *buf)
1055{
1056 struct vpif_fh *fh = priv;
1057 struct channel_obj *ch = fh->channel;
1058 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1059
1060 vpif_dbg(2, debug, "vpif_querybuf\n");
1061
1062 if (common->fmt.type != buf->type)
1063 return -EINVAL;
1064
1065 if (common->memory != V4L2_MEMORY_MMAP) {
1066 vpif_dbg(1, debug, "Invalid memory\n");
1067 return -EINVAL;
1068 }
1069
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001070 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001071}
1072
1073/**
1074 * vpif_qbuf() - query buffer handler
1075 * @file: file ptr
1076 * @priv: file handle
1077 * @buf: v4l2 buffer structure ptr
1078 */
1079static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1080{
1081
1082 struct vpif_fh *fh = priv;
1083 struct channel_obj *ch = fh->channel;
1084 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1085 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001086
1087 vpif_dbg(2, debug, "vpif_qbuf\n");
1088
1089 if (common->fmt.type != tbuf.type) {
1090 vpif_err("invalid buffer type\n");
1091 return -EINVAL;
1092 }
1093
1094 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001095 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001096 return -EACCES;
1097 }
1098
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001099 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001100}
1101
1102/**
1103 * vpif_dqbuf() - query buffer handler
1104 * @file: file ptr
1105 * @priv: file handle
1106 * @buf: v4l2 buffer structure ptr
1107 */
1108static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1109{
1110 struct vpif_fh *fh = priv;
1111 struct channel_obj *ch = fh->channel;
1112 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1113
1114 vpif_dbg(2, debug, "vpif_dqbuf\n");
1115
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001116 return vb2_dqbuf(&common->buffer_queue, buf,
1117 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001118}
1119
1120/**
1121 * vpif_streamon() - streamon handler
1122 * @file: file ptr
1123 * @priv: file handle
1124 * @buftype: v4l2 buffer type
1125 */
1126static int vpif_streamon(struct file *file, void *priv,
1127 enum v4l2_buf_type buftype)
1128{
1129
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001130 struct vpif_fh *fh = priv;
1131 struct channel_obj *ch = fh->channel;
1132 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1133 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1134 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001135 int ret = 0;
1136
1137 vpif_dbg(2, debug, "vpif_streamon\n");
1138
1139 vpif = &ch->vpifparams;
1140
1141 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1142 vpif_dbg(1, debug, "buffer type not supported\n");
1143 return -EINVAL;
1144 }
1145
1146 /* If file handle is not allowed IO, return error */
1147 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1148 vpif_dbg(1, debug, "io not allowed\n");
1149 return -EACCES;
1150 }
1151
1152 /* If Streaming is already started, return error */
1153 if (common->started) {
1154 vpif_dbg(1, debug, "channel->started\n");
1155 return -EBUSY;
1156 }
1157
1158 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1159 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1160 vpif->std_info.ycmux_mode == 0) ||
1161 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1162 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1163 vpif_dbg(1, debug, "other channel is being used\n");
1164 return -EBUSY;
1165 }
1166
1167 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1168 if (ret)
1169 return ret;
1170
1171 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001172 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001173
Hans Verkuil178cce12012-09-20 09:06:30 -03001174 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001175 vpif_dbg(1, debug, "stream on failed in subdev\n");
1176 return ret;
1177 }
1178
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001179 /* Call vb2_streamon to start streaming in videobuf2 */
1180 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001181 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001182 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001183 return ret;
1184 }
1185
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001186 return ret;
1187}
1188
1189/**
1190 * vpif_streamoff() - streamoff handler
1191 * @file: file ptr
1192 * @priv: file handle
1193 * @buftype: v4l2 buffer type
1194 */
1195static int vpif_streamoff(struct file *file, void *priv,
1196 enum v4l2_buf_type buftype)
1197{
1198
1199 struct vpif_fh *fh = priv;
1200 struct channel_obj *ch = fh->channel;
1201 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1202 int ret;
1203
1204 vpif_dbg(2, debug, "vpif_streamoff\n");
1205
1206 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1207 vpif_dbg(1, debug, "buffer type not supported\n");
1208 return -EINVAL;
1209 }
1210
1211 /* If io is allowed for this file handle, return error */
1212 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1213 vpif_dbg(1, debug, "io not allowed\n");
1214 return -EACCES;
1215 }
1216
1217 /* If streaming is not started, return error */
1218 if (!common->started) {
1219 vpif_dbg(1, debug, "channel->started\n");
1220 return -EINVAL;
1221 }
1222
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001223 /* disable channel */
1224 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1225 enable_channel0(0);
1226 channel0_intr_enable(0);
1227 } else {
1228 enable_channel1(0);
1229 channel1_intr_enable(0);
1230 }
1231
1232 common->started = 0;
1233
Hans Verkuil178cce12012-09-20 09:06:30 -03001234 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001235
Hans Verkuil178cce12012-09-20 09:06:30 -03001236 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001237 vpif_dbg(1, debug, "stream off failed in subdev\n");
1238
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001239 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001240}
1241
1242/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001243 * vpif_input_to_subdev() - Maps input to sub device
1244 * @vpif_cfg - global config ptr
1245 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001246 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001247 *
1248 * lookup the sub device information for a given input index.
1249 * we report all the inputs to application. inputs table also
1250 * has sub device name for the each input
1251 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001252static int vpif_input_to_subdev(
1253 struct vpif_capture_config *vpif_cfg,
1254 struct vpif_capture_chan_config *chan_cfg,
1255 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001256{
Hans Verkuil178cce12012-09-20 09:06:30 -03001257 struct vpif_subdev_info *subdev_info;
1258 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001259 int i;
1260
Hans Verkuil178cce12012-09-20 09:06:30 -03001261 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001262
Hans Verkuil178cce12012-09-20 09:06:30 -03001263 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1264 if (subdev_name == NULL)
1265 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001266
1267 /* loop through the sub device list to get the sub device info */
1268 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1269 subdev_info = &vpif_cfg->subdev_info[i];
1270 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001271 return i;
1272 }
1273 return -1;
1274}
1275
1276/**
1277 * vpif_set_input() - Select an input
1278 * @vpif_cfg - global config ptr
1279 * @ch - channel
1280 * @_index - Given input index from application
1281 *
1282 * Select the given input.
1283 */
1284static int vpif_set_input(
1285 struct vpif_capture_config *vpif_cfg,
1286 struct channel_obj *ch,
1287 int index)
1288{
1289 struct vpif_capture_chan_config *chan_cfg =
1290 &vpif_cfg->chan_config[ch->channel_id];
1291 struct vpif_subdev_info *subdev_info = NULL;
1292 struct v4l2_subdev *sd = NULL;
1293 u32 input = 0, output = 0;
1294 int sd_index;
1295 int ret;
1296
1297 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1298 if (sd_index >= 0) {
1299 sd = vpif_obj.sd[sd_index];
1300 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001301 }
1302
Hans Verkuil178cce12012-09-20 09:06:30 -03001303 /* first setup input path from sub device to vpif */
1304 if (sd && vpif_cfg->setup_input_path) {
1305 ret = vpif_cfg->setup_input_path(ch->channel_id,
1306 subdev_info->name);
1307 if (ret < 0) {
1308 vpif_dbg(1, debug, "couldn't setup input path for the" \
1309 " sub device %s, for input index %d\n",
1310 subdev_info->name, index);
1311 return ret;
1312 }
1313 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001314
Hans Verkuil178cce12012-09-20 09:06:30 -03001315 if (sd) {
1316 input = chan_cfg->inputs[index].input_route;
1317 output = chan_cfg->inputs[index].output_route;
1318 ret = v4l2_subdev_call(sd, video, s_routing,
1319 input, output, 0);
1320 if (ret < 0 && ret != -ENOIOCTLCMD) {
1321 vpif_dbg(1, debug, "Failed to set input\n");
1322 return ret;
1323 }
1324 }
1325 ch->input_idx = index;
1326 ch->sd = sd;
1327 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001328 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001329
Hans Verkuil178cce12012-09-20 09:06:30 -03001330 /* update tvnorms from the sub device input info */
1331 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1332 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001333}
1334
1335/**
1336 * vpif_querystd() - querystd handler
1337 * @file: file ptr
1338 * @priv: file handle
1339 * @std_id: ptr to std id
1340 *
1341 * This function is called to detect standard at the selected input
1342 */
1343static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1344{
1345 struct vpif_fh *fh = priv;
1346 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001347 int ret = 0;
1348
1349 vpif_dbg(2, debug, "vpif_querystd\n");
1350
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001351 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001352 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001353
Hans Verkuil178cce12012-09-20 09:06:30 -03001354 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1355 return -ENODATA;
1356 if (ret) {
1357 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1358 return ret;
1359 }
1360
1361 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001362}
1363
1364/**
1365 * vpif_g_std() - get STD handler
1366 * @file: file ptr
1367 * @priv: file handle
1368 * @std_id: ptr to std id
1369 */
1370static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1371{
1372 struct vpif_fh *fh = priv;
1373 struct channel_obj *ch = fh->channel;
1374
1375 vpif_dbg(2, debug, "vpif_g_std\n");
1376
1377 *std = ch->video.stdid;
1378 return 0;
1379}
1380
1381/**
1382 * vpif_s_std() - set STD handler
1383 * @file: file ptr
1384 * @priv: file handle
1385 * @std_id: ptr to std id
1386 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001387static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001388{
1389 struct vpif_fh *fh = priv;
1390 struct channel_obj *ch = fh->channel;
1391 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1392 int ret = 0;
1393
1394 vpif_dbg(2, debug, "vpif_s_std\n");
1395
1396 if (common->started) {
1397 vpif_err("streaming in progress\n");
1398 return -EBUSY;
1399 }
1400
1401 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1402 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1403 if (!fh->initialized) {
1404 vpif_dbg(1, debug, "Channel Busy\n");
1405 return -EBUSY;
1406 }
1407 }
1408
Hans Verkuilffb48772010-05-01 08:03:24 -03001409 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001410 if (0 != ret)
1411 return ret;
1412
1413 fh->initialized = 1;
1414
1415 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001416 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001417 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001418
1419 /* Get the information about the standard */
1420 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001421 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001422 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001423 }
1424
1425 /* Configure the default format information */
1426 vpif_config_format(ch);
1427
1428 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001429 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001430 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001431 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001432 return ret;
1433 }
1434 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001435}
1436
1437/**
1438 * vpif_enum_input() - ENUMINPUT handler
1439 * @file: file ptr
1440 * @priv: file handle
1441 * @input: ptr to input structure
1442 */
1443static int vpif_enum_input(struct file *file, void *priv,
1444 struct v4l2_input *input)
1445{
1446
1447 struct vpif_capture_config *config = vpif_dev->platform_data;
1448 struct vpif_capture_chan_config *chan_cfg;
1449 struct vpif_fh *fh = priv;
1450 struct channel_obj *ch = fh->channel;
1451
1452 chan_cfg = &config->chan_config[ch->channel_id];
1453
1454 if (input->index >= chan_cfg->input_count) {
1455 vpif_dbg(1, debug, "Invalid input index\n");
1456 return -EINVAL;
1457 }
1458
1459 memcpy(input, &chan_cfg->inputs[input->index].input,
1460 sizeof(*input));
1461 return 0;
1462}
1463
1464/**
1465 * vpif_g_input() - Get INPUT handler
1466 * @file: file ptr
1467 * @priv: file handle
1468 * @index: ptr to input index
1469 */
1470static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1471{
1472 struct vpif_fh *fh = priv;
1473 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001474
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001475 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001476 return 0;
1477}
1478
1479/**
1480 * vpif_s_input() - Set INPUT handler
1481 * @file: file ptr
1482 * @priv: file handle
1483 * @index: input index
1484 */
1485static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1486{
1487 struct vpif_capture_config *config = vpif_dev->platform_data;
1488 struct vpif_capture_chan_config *chan_cfg;
1489 struct vpif_fh *fh = priv;
1490 struct channel_obj *ch = fh->channel;
1491 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001492 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001493
1494 chan_cfg = &config->chan_config[ch->channel_id];
1495
Hans Verkuil7aaad132012-09-20 09:06:25 -03001496 if (index >= chan_cfg->input_count)
1497 return -EINVAL;
1498
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001499 if (common->started) {
1500 vpif_err("Streaming in progress\n");
1501 return -EBUSY;
1502 }
1503
1504 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1505 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1506 if (!fh->initialized) {
1507 vpif_dbg(1, debug, "Channel Busy\n");
1508 return -EBUSY;
1509 }
1510 }
1511
Hans Verkuilffb48772010-05-01 08:03:24 -03001512 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001513 if (0 != ret)
1514 return ret;
1515
1516 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001517 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001518}
1519
1520/**
1521 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1522 * @file: file ptr
1523 * @priv: file handle
1524 * @index: input index
1525 */
1526static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1527 struct v4l2_fmtdesc *fmt)
1528{
1529 struct vpif_fh *fh = priv;
1530 struct channel_obj *ch = fh->channel;
1531
1532 if (fmt->index != 0) {
1533 vpif_dbg(1, debug, "Invalid format index\n");
1534 return -EINVAL;
1535 }
1536
1537 /* Fill in the information about format */
1538 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1539 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1540 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1541 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1542 } else {
1543 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1544 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1545 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1546 }
1547 return 0;
1548}
1549
1550/**
1551 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1552 * @file: file ptr
1553 * @priv: file handle
1554 * @fmt: ptr to v4l2 format structure
1555 */
1556static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1557 struct v4l2_format *fmt)
1558{
1559 struct vpif_fh *fh = priv;
1560 struct channel_obj *ch = fh->channel;
1561 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1562
1563 return vpif_check_format(ch, pixfmt, 1);
1564}
1565
1566
1567/**
1568 * vpif_g_fmt_vid_cap() - Set INPUT handler
1569 * @file: file ptr
1570 * @priv: file handle
1571 * @fmt: ptr to v4l2 format structure
1572 */
1573static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1574 struct v4l2_format *fmt)
1575{
1576 struct vpif_fh *fh = priv;
1577 struct channel_obj *ch = fh->channel;
1578 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1579
1580 /* Check the validity of the buffer type */
1581 if (common->fmt.type != fmt->type)
1582 return -EINVAL;
1583
1584 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001585 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001586 return 0;
1587}
1588
1589/**
1590 * vpif_s_fmt_vid_cap() - Set FMT handler
1591 * @file: file ptr
1592 * @priv: file handle
1593 * @fmt: ptr to v4l2 format structure
1594 */
1595static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1596 struct v4l2_format *fmt)
1597{
1598 struct vpif_fh *fh = priv;
1599 struct channel_obj *ch = fh->channel;
1600 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1601 struct v4l2_pix_format *pixfmt;
1602 int ret = 0;
1603
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001604 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001605
1606 /* If streaming is started, return error */
1607 if (common->started) {
1608 vpif_dbg(1, debug, "Streaming is started\n");
1609 return -EBUSY;
1610 }
1611
1612 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1613 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1614 if (!fh->initialized) {
1615 vpif_dbg(1, debug, "Channel Busy\n");
1616 return -EBUSY;
1617 }
1618 }
1619
Hans Verkuilffb48772010-05-01 08:03:24 -03001620 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001621 if (0 != ret)
1622 return ret;
1623
1624 fh->initialized = 1;
1625
1626 pixfmt = &fmt->fmt.pix;
1627 /* Check for valid field format */
1628 ret = vpif_check_format(ch, pixfmt, 0);
1629
1630 if (ret)
1631 return ret;
1632 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001633 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001634 return 0;
1635}
1636
1637/**
1638 * vpif_querycap() - QUERYCAP handler
1639 * @file: file ptr
1640 * @priv: file handle
1641 * @cap: ptr to v4l2_capability structure
1642 */
1643static int vpif_querycap(struct file *file, void *priv,
1644 struct v4l2_capability *cap)
1645{
1646 struct vpif_capture_config *config = vpif_dev->platform_data;
1647
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001648 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1649 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1650 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1651 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1652 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001653 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1654
1655 return 0;
1656}
1657
1658/**
1659 * vpif_g_priority() - get priority handler
1660 * @file: file ptr
1661 * @priv: file handle
1662 * @prio: ptr to v4l2_priority structure
1663 */
1664static int vpif_g_priority(struct file *file, void *priv,
1665 enum v4l2_priority *prio)
1666{
1667 struct vpif_fh *fh = priv;
1668 struct channel_obj *ch = fh->channel;
1669
1670 *prio = v4l2_prio_max(&ch->prio);
1671
1672 return 0;
1673}
1674
1675/**
1676 * vpif_s_priority() - set priority handler
1677 * @file: file ptr
1678 * @priv: file handle
1679 * @prio: ptr to v4l2_priority structure
1680 */
1681static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1682{
1683 struct vpif_fh *fh = priv;
1684 struct channel_obj *ch = fh->channel;
1685
1686 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1687}
1688
1689/**
1690 * vpif_cropcap() - cropcap handler
1691 * @file: file ptr
1692 * @priv: file handle
1693 * @crop: ptr to v4l2_cropcap structure
1694 */
1695static int vpif_cropcap(struct file *file, void *priv,
1696 struct v4l2_cropcap *crop)
1697{
1698 struct vpif_fh *fh = priv;
1699 struct channel_obj *ch = fh->channel;
1700 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1701
1702 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1703 return -EINVAL;
1704
1705 crop->bounds.left = 0;
1706 crop->bounds.top = 0;
1707 crop->bounds.height = common->height;
1708 crop->bounds.width = common->width;
1709 crop->defrect = crop->bounds;
1710 return 0;
1711}
1712
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001713/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001714 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001715 * @file: file ptr
1716 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001717 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001718 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001719static int
1720vpif_enum_dv_timings(struct file *file, void *priv,
1721 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001722{
1723 struct vpif_fh *fh = priv;
1724 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001725 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001726
Hans Verkuil178cce12012-09-20 09:06:30 -03001727 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001728 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001729 return -EINVAL;
1730 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001731}
1732
1733/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001734 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001735 * @file: file ptr
1736 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001737 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001738 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001739static int
1740vpif_query_dv_timings(struct file *file, void *priv,
1741 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001742{
1743 struct vpif_fh *fh = priv;
1744 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001745 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001746
Hans Verkuil178cce12012-09-20 09:06:30 -03001747 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001748 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001749 return -ENODATA;
1750 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001751}
1752
Mats Randgaardc027e162010-12-16 12:17:44 -03001753/**
1754 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1755 * @file: file ptr
1756 * @priv: file handle
1757 * @timings: digital video timings
1758 */
1759static int vpif_s_dv_timings(struct file *file, void *priv,
1760 struct v4l2_dv_timings *timings)
1761{
1762 struct vpif_fh *fh = priv;
1763 struct channel_obj *ch = fh->channel;
1764 struct vpif_params *vpifparams = &ch->vpifparams;
1765 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1766 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001767 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001768 int ret;
1769
1770 if (timings->type != V4L2_DV_BT_656_1120) {
1771 vpif_dbg(2, debug, "Timing type not defined\n");
1772 return -EINVAL;
1773 }
1774
1775 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001776 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1777 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1778 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001779 if (ret < 0) {
1780 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1781 return ret;
1782 }
1783
1784 if (!(timings->bt.width && timings->bt.height &&
1785 (timings->bt.hbackporch ||
1786 timings->bt.hfrontporch ||
1787 timings->bt.hsync) &&
1788 timings->bt.vfrontporch &&
1789 (timings->bt.vbackporch ||
1790 timings->bt.vsync))) {
1791 vpif_dbg(2, debug, "Timings for width, height, "
1792 "horizontal back porch, horizontal sync, "
1793 "horizontal front porch, vertical back porch, "
1794 "vertical sync and vertical back porch "
1795 "must be defined\n");
1796 return -EINVAL;
1797 }
1798
Hans Verkuil0598c172012-09-18 07:18:47 -03001799 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001800
1801 /* Configure video port timings */
1802
1803 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1804 bt->hsync - 8;
1805 std_info->sav2eav = bt->width;
1806
1807 std_info->l1 = 1;
1808 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1809
1810 if (bt->interlaced) {
1811 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1812 std_info->vsize = bt->height * 2 +
1813 bt->vfrontporch + bt->vsync + bt->vbackporch +
1814 bt->il_vfrontporch + bt->il_vsync +
1815 bt->il_vbackporch;
1816 std_info->l5 = std_info->vsize/2 -
1817 (bt->vfrontporch - 1);
1818 std_info->l7 = std_info->vsize/2 + 1;
1819 std_info->l9 = std_info->l7 + bt->il_vsync +
1820 bt->il_vbackporch + 1;
1821 std_info->l11 = std_info->vsize -
1822 (bt->il_vfrontporch - 1);
1823 } else {
1824 vpif_dbg(2, debug, "Required timing values for "
1825 "interlaced BT format missing\n");
1826 return -EINVAL;
1827 }
1828 } else {
1829 std_info->vsize = bt->height + bt->vfrontporch +
1830 bt->vsync + bt->vbackporch;
1831 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1832 }
1833 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1834 std_info->width = bt->width;
1835 std_info->height = bt->height;
1836 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1837 std_info->ycmux_mode = 0;
1838 std_info->capture_format = 0;
1839 std_info->vbi_supported = 0;
1840 std_info->hd_sd = 1;
1841 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001842
1843 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001844 return 0;
1845}
1846
1847/**
1848 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1849 * @file: file ptr
1850 * @priv: file handle
1851 * @timings: digital video timings
1852 */
1853static int vpif_g_dv_timings(struct file *file, void *priv,
1854 struct v4l2_dv_timings *timings)
1855{
1856 struct vpif_fh *fh = priv;
1857 struct channel_obj *ch = fh->channel;
1858 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001859
Hans Verkuil0598c172012-09-18 07:18:47 -03001860 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001861
1862 return 0;
1863}
1864
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001865/*
1866 * vpif_g_chip_ident() - Identify the chip
1867 * @file: file ptr
1868 * @priv: file handle
1869 * @chip: chip identity
1870 *
1871 * Returns zero or -EINVAL if read operations fails.
1872 */
1873static int vpif_g_chip_ident(struct file *file, void *priv,
1874 struct v4l2_dbg_chip_ident *chip)
1875{
1876 chip->ident = V4L2_IDENT_NONE;
1877 chip->revision = 0;
1878 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1879 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1880 vpif_dbg(2, debug, "match_type is invalid.\n");
1881 return -EINVAL;
1882 }
1883
1884 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1885 g_chip_ident, chip);
1886}
1887
1888#ifdef CONFIG_VIDEO_ADV_DEBUG
1889/*
1890 * vpif_dbg_g_register() - Read register
1891 * @file: file ptr
1892 * @priv: file handle
1893 * @reg: register to be read
1894 *
1895 * Debugging only
1896 * Returns zero or -EINVAL if read operations fails.
1897 */
1898static int vpif_dbg_g_register(struct file *file, void *priv,
1899 struct v4l2_dbg_register *reg){
1900 struct vpif_fh *fh = priv;
1901 struct channel_obj *ch = fh->channel;
1902
Hans Verkuil178cce12012-09-20 09:06:30 -03001903 return v4l2_subdev_call(ch->sd, core, g_register, reg);
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001904}
1905
1906/*
1907 * vpif_dbg_s_register() - Write to register
1908 * @file: file ptr
1909 * @priv: file handle
1910 * @reg: register to be modified
1911 *
1912 * Debugging only
1913 * Returns zero or -EINVAL if write operations fails.
1914 */
1915static int vpif_dbg_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001916 const struct v4l2_dbg_register *reg)
1917{
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001918 struct vpif_fh *fh = priv;
1919 struct channel_obj *ch = fh->channel;
1920
Hans Verkuil178cce12012-09-20 09:06:30 -03001921 return v4l2_subdev_call(ch->sd, core, s_register, reg);
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001922}
1923#endif
1924
1925/*
1926 * vpif_log_status() - Status information
1927 * @file: file ptr
1928 * @priv: file handle
1929 *
1930 * Returns zero.
1931 */
1932static int vpif_log_status(struct file *filep, void *priv)
1933{
1934 /* status for sub devices */
1935 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1936
1937 return 0;
1938}
1939
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001940/* vpif capture ioctl operations */
1941static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1942 .vidioc_querycap = vpif_querycap,
1943 .vidioc_g_priority = vpif_g_priority,
1944 .vidioc_s_priority = vpif_s_priority,
1945 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1946 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1947 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1948 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1949 .vidioc_enum_input = vpif_enum_input,
1950 .vidioc_s_input = vpif_s_input,
1951 .vidioc_g_input = vpif_g_input,
1952 .vidioc_reqbufs = vpif_reqbufs,
1953 .vidioc_querybuf = vpif_querybuf,
1954 .vidioc_querystd = vpif_querystd,
1955 .vidioc_s_std = vpif_s_std,
1956 .vidioc_g_std = vpif_g_std,
1957 .vidioc_qbuf = vpif_qbuf,
1958 .vidioc_dqbuf = vpif_dqbuf,
1959 .vidioc_streamon = vpif_streamon,
1960 .vidioc_streamoff = vpif_streamoff,
1961 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001962 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1963 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001964 .vidioc_s_dv_timings = vpif_s_dv_timings,
1965 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001966 .vidioc_g_chip_ident = vpif_g_chip_ident,
1967#ifdef CONFIG_VIDEO_ADV_DEBUG
1968 .vidioc_g_register = vpif_dbg_g_register,
1969 .vidioc_s_register = vpif_dbg_s_register,
1970#endif
1971 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001972};
1973
1974/* vpif file operations */
1975static struct v4l2_file_operations vpif_fops = {
1976 .owner = THIS_MODULE,
1977 .open = vpif_open,
1978 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001979 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001980 .mmap = vpif_mmap,
1981 .poll = vpif_poll
1982};
1983
1984/* vpif video template */
1985static struct video_device vpif_video_template = {
1986 .name = "vpif",
1987 .fops = &vpif_fops,
1988 .minor = -1,
1989 .ioctl_ops = &vpif_ioctl_ops,
1990};
1991
1992/**
1993 * initialize_vpif() - Initialize vpif data structures
1994 *
1995 * Allocate memory for data structures and initialize them
1996 */
1997static int initialize_vpif(void)
1998{
1999 int err = 0, i, j;
2000 int free_channel_objects_index;
2001
2002 /* Default number of buffers should be 3 */
2003 if ((ch0_numbuffers > 0) &&
2004 (ch0_numbuffers < config_params.min_numbuffers))
2005 ch0_numbuffers = config_params.min_numbuffers;
2006 if ((ch1_numbuffers > 0) &&
2007 (ch1_numbuffers < config_params.min_numbuffers))
2008 ch1_numbuffers = config_params.min_numbuffers;
2009
2010 /* Set buffer size to min buffers size if it is invalid */
2011 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2012 ch0_bufsize =
2013 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2014 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2015 ch1_bufsize =
2016 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2017
2018 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2019 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2020 if (ch0_numbuffers) {
2021 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2022 = ch0_bufsize;
2023 }
2024 if (ch1_numbuffers) {
2025 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2026 = ch1_bufsize;
2027 }
2028
2029 /* Allocate memory for six channel objects */
2030 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2031 vpif_obj.dev[i] =
2032 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2033 /* If memory allocation fails, return error */
2034 if (!vpif_obj.dev[i]) {
2035 free_channel_objects_index = i;
2036 err = -ENOMEM;
2037 goto vpif_init_free_channel_objects;
2038 }
2039 }
2040 return 0;
2041
2042vpif_init_free_channel_objects:
2043 for (j = 0; j < free_channel_objects_index; j++)
2044 kfree(vpif_obj.dev[j]);
2045 return err;
2046}
2047
2048/**
2049 * vpif_probe : This function probes the vpif capture driver
2050 * @pdev: platform device pointer
2051 *
2052 * This creates device entries by register itself to the V4L2 driver and
2053 * initializes fields of each channel objects
2054 */
2055static __init int vpif_probe(struct platform_device *pdev)
2056{
2057 struct vpif_subdev_info *subdevdata;
2058 struct vpif_capture_config *config;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002059 int i, j, k, err;
2060 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002061 struct i2c_adapter *i2c_adap;
2062 struct channel_obj *ch;
2063 struct common_obj *common;
2064 struct video_device *vfd;
2065 struct resource *res;
2066 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03002067 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002068
2069 vpif_dev = &pdev->dev;
2070
2071 err = initialize_vpif();
2072 if (err) {
2073 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2074 return err;
2075 }
2076
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002077 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2078 if (err) {
2079 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2080 return err;
2081 }
2082
Hans Verkuil5be452c2012-09-20 09:06:29 -03002083 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002084 for (i = res->start; i <= res->end; i++) {
Manjunath Hadli0316b892012-04-13 04:44:31 -03002085 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
Hans Verkuil5be452c2012-09-20 09:06:29 -03002086 "VPIF_Capture", (void *)
2087 (&vpif_obj.dev[res_idx]->channel_id))) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002088 err = -EBUSY;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002089 for (j = 0; j < i; j++)
2090 free_irq(j, (void *)
2091 (&vpif_obj.dev[res_idx]->channel_id));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002092 goto vpif_int_err;
2093 }
2094 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002095 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002096 }
2097
2098 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2099 /* Get the pointer to the channel object */
2100 ch = vpif_obj.dev[i];
2101 /* Allocate memory for video device */
2102 vfd = video_device_alloc();
2103 if (NULL == vfd) {
2104 for (j = 0; j < i; j++) {
2105 ch = vpif_obj.dev[j];
2106 video_device_release(ch->video_dev);
2107 }
2108 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002109 goto vpif_int_err;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002110 }
2111
2112 /* Initialize field of video device */
2113 *vfd = vpif_video_template;
2114 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2115 vfd->release = video_device_release;
2116 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002117 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002118 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002119 /* Set video_dev to the video device */
2120 ch->video_dev = vfd;
2121 }
2122
Manjunath Hadli764af392012-04-13 04:49:34 -03002123 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2124 if (res) {
2125 size = resource_size(res);
2126 /* The resources are divided into two equal memory and when we
2127 * have HD output we can add them together
2128 */
2129 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2130 ch = vpif_obj.dev[j];
2131 ch->channel_id = j;
2132 /* only enabled if second resource exists */
2133 config_params.video_limit[ch->channel_id] = 0;
2134 if (size)
2135 config_params.video_limit[ch->channel_id] =
2136 size/2;
2137 }
2138 }
2139
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002140 i2c_adap = i2c_get_adapter(1);
2141 config = pdev->dev.platform_data;
2142
2143 subdev_count = config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002144 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002145 GFP_KERNEL);
2146 if (vpif_obj.sd == NULL) {
2147 vpif_err("unable to allocate memory for subdevice pointers\n");
2148 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002149 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002150 }
2151
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002152 for (i = 0; i < subdev_count; i++) {
2153 subdevdata = &config->subdev_info[i];
2154 vpif_obj.sd[i] =
2155 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2156 i2c_adap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002157 &subdevdata->board_info,
2158 NULL);
2159
2160 if (!vpif_obj.sd[i]) {
2161 vpif_err("Error registering v4l2 subdevice\n");
2162 goto probe_subdev_out;
2163 }
2164 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2165 subdevdata->name);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002166 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002167
Hans Verkuilb65814e2012-09-20 09:06:26 -03002168 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2169 ch = vpif_obj.dev[j];
2170 ch->channel_id = j;
2171 common = &(ch->common[VPIF_VIDEO_INDEX]);
2172 spin_lock_init(&common->irqlock);
2173 mutex_init(&common->lock);
2174 ch->video_dev->lock = &common->lock;
2175 /* Initialize prio member of channel object */
2176 v4l2_prio_init(&ch->prio);
2177 video_set_drvdata(ch->video_dev, ch);
2178
Hans Verkuil178cce12012-09-20 09:06:30 -03002179 /* select input 0 */
2180 err = vpif_set_input(config, ch, 0);
2181 if (err)
2182 goto probe_out;
2183
Hans Verkuilb65814e2012-09-20 09:06:26 -03002184 err = video_register_device(ch->video_dev,
2185 VFL_TYPE_GRABBER, (j ? 1 : 0));
2186 if (err)
2187 goto probe_out;
2188 }
Manjunath Hadli0a631722012-04-13 04:44:00 -03002189 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002190 return 0;
2191
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002192probe_out:
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002193 for (k = 0; k < j; k++) {
2194 /* Get the pointer to the channel object */
2195 ch = vpif_obj.dev[k];
2196 /* Unregister video device */
2197 video_unregister_device(ch->video_dev);
2198 }
Hans Verkuilb65814e2012-09-20 09:06:26 -03002199probe_subdev_out:
2200 /* free sub devices memory */
2201 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002202
Hans Verkuil5be452c2012-09-20 09:06:29 -03002203vpif_sd_error:
2204 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2205 ch = vpif_obj.dev[i];
2206 /* Note: does nothing if ch->video_dev == NULL */
2207 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002208 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002209vpif_int_err:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002210 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Hans Verkuil5be452c2012-09-20 09:06:29 -03002211 for (i = 0; i < res_idx; i++) {
2212 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
2213 for (j = res->start; j <= res->end; j++)
2214 free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
2215 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002216 return err;
2217}
2218
2219/**
2220 * vpif_remove() - driver remove handler
2221 * @device: ptr to platform device structure
2222 *
2223 * The vidoe device is unregistered
2224 */
2225static int vpif_remove(struct platform_device *device)
2226{
2227 int i;
2228 struct channel_obj *ch;
2229
2230 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2231
2232 /* un-register device */
2233 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2234 /* Get the pointer to the channel object */
2235 ch = vpif_obj.dev[i];
2236 /* Unregister video device */
2237 video_unregister_device(ch->video_dev);
2238 }
2239 return 0;
2240}
2241
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002242#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002243/**
2244 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002245 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002246static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002247{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002248
2249 struct common_obj *common;
2250 struct channel_obj *ch;
2251 int i;
2252
2253 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2254 /* Get the pointer to the channel object */
2255 ch = vpif_obj.dev[i];
2256 common = &ch->common[VPIF_VIDEO_INDEX];
2257 mutex_lock(&common->lock);
2258 if (ch->usrs && common->io_usrs) {
2259 /* Disable channel */
2260 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2261 enable_channel0(0);
2262 channel0_intr_enable(0);
2263 }
2264 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2265 common->started == 2) {
2266 enable_channel1(0);
2267 channel1_intr_enable(0);
2268 }
2269 }
2270 mutex_unlock(&common->lock);
2271 }
2272
2273 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002274}
2275
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002276/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002277 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002278 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002279static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002280{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002281 struct common_obj *common;
2282 struct channel_obj *ch;
2283 int i;
2284
2285 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2286 /* Get the pointer to the channel object */
2287 ch = vpif_obj.dev[i];
2288 common = &ch->common[VPIF_VIDEO_INDEX];
2289 mutex_lock(&common->lock);
2290 if (ch->usrs && common->io_usrs) {
2291 /* Disable channel */
2292 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2293 enable_channel0(1);
2294 channel0_intr_enable(1);
2295 }
2296 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2297 common->started == 2) {
2298 enable_channel1(1);
2299 channel1_intr_enable(1);
2300 }
2301 }
2302 mutex_unlock(&common->lock);
2303 }
2304
2305 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002306}
2307
Alexey Dobriyan47145212009-12-14 18:00:08 -08002308static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002309 .suspend = vpif_suspend,
2310 .resume = vpif_resume,
2311};
2312
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002313#define vpif_pm_ops (&vpif_dev_pm_ops)
2314#else
2315#define vpif_pm_ops NULL
2316#endif
2317
Mats Randgaardffa1b392010-08-30 10:30:36 -03002318static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002319 .driver = {
2320 .name = "vpif_capture",
2321 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002322 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002323 },
2324 .probe = vpif_probe,
2325 .remove = vpif_remove,
2326};
2327
2328/**
2329 * vpif_init: initialize the vpif driver
2330 *
2331 * This function registers device and driver to the kernel, requests irq
2332 * handler and allocates memory
2333 * for channel objects
2334 */
2335static __init int vpif_init(void)
2336{
2337 return platform_driver_register(&vpif_driver);
2338}
2339
2340/**
2341 * vpif_cleanup : This function clean up the vpif capture resources
2342 *
2343 * This will un-registers device and driver to the kernel, frees
2344 * requested irq handler and de-allocates memory allocated for channel
2345 * objects.
2346 */
2347static void vpif_cleanup(void)
2348{
2349 struct platform_device *pdev;
2350 struct resource *res;
2351 int irq_num;
2352 int i = 0;
2353
2354 pdev = container_of(vpif_dev, struct platform_device, dev);
2355 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2356 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2357 free_irq(irq_num,
2358 (void *)(&vpif_obj.dev[i]->channel_id));
2359 i++;
2360 }
2361
2362 platform_driver_unregister(&vpif_driver);
2363
2364 kfree(vpif_obj.sd);
2365 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2366 kfree(vpif_obj.dev[i]);
2367}
2368
2369/* Function for module initialization and cleanup */
2370module_init(vpif_init);
2371module_exit(vpif_cleanup);