blob: d09a27a23c5d74361677680c3ed8422ce73af06a [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
Lad, Prabhakar012eef72013-04-19 05:53:29 -030027#include <media/v4l2-ioctl.h>
28
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030029#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030030#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030031
32MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
33MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030034MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030035
36#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
37#define vpif_dbg(level, debug, fmt, arg...) \
38 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
39
40static int debug = 1;
41static u32 ch0_numbuffers = 3;
42static u32 ch1_numbuffers = 3;
43static u32 ch0_bufsize = 1920 * 1080 * 2;
44static u32 ch1_bufsize = 720 * 576 * 2;
45
46module_param(debug, int, 0644);
47module_param(ch0_numbuffers, uint, S_IRUGO);
48module_param(ch1_numbuffers, uint, S_IRUGO);
49module_param(ch0_bufsize, uint, S_IRUGO);
50module_param(ch1_bufsize, uint, S_IRUGO);
51
52MODULE_PARM_DESC(debug, "Debug level 0-1");
53MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
54MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
55MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
56MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
57
58static struct vpif_config_params config_params = {
59 .min_numbuffers = 3,
60 .numbuffers[0] = 3,
61 .numbuffers[1] = 3,
62 .min_bufsize[0] = 720 * 480 * 2,
63 .min_bufsize[1] = 720 * 480 * 2,
64 .channel_bufsize[0] = 1920 * 1080 * 2,
65 .channel_bufsize[1] = 720 * 576 * 2,
66};
67
68/* global variables */
69static struct vpif_device vpif_obj = { {NULL} };
70static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030071static void vpif_calculate_offsets(struct channel_obj *ch);
72static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030073
74/**
75 * buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030076 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030077 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030078 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030079 * function is called. The buffer is prepared and user space virtual address
80 * or user address is converted into physical address
81 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030082static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030083{
84 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030085 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
86 struct vb2_queue *q = vb->vb2_queue;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030087 struct channel_obj *ch = fh->channel;
88 struct common_obj *common;
89 unsigned long addr;
90
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030091 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
92
93 common = &ch->common[VPIF_VIDEO_INDEX];
94
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030095 if (vb->state != VB2_BUF_STATE_ACTIVE &&
96 vb->state != VB2_BUF_STATE_PREPARED) {
97 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
98 if (vb2_plane_vaddr(vb, 0) &&
99 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
100 goto exit;
101 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300102
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300103 if (q->streaming) {
104 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
105 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
106 !IS_ALIGNED((addr + common->ctop_off), 8) ||
107 !IS_ALIGNED((addr + common->cbtm_off), 8))
108 goto exit;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300109 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300110 }
111 return 0;
112exit:
113 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
114 return -EINVAL;
115}
116
117/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300118 * vpif_buffer_queue_setup : Callback function for buffer setup.
119 * @vq: vb2_queue ptr
120 * @fmt: v4l2 format
121 * @nbuffers: ptr to number of buffers requested by application
122 * @nplanes:: contains number of distinct video planes needed to hold a frame
123 * @sizes[]: contains the size (in bytes) of each plane.
124 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300125 *
126 * This callback function is called when reqbuf() is called to adjust
127 * the buffer count and buffer size
128 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300129static int vpif_buffer_queue_setup(struct vb2_queue *vq,
130 const struct v4l2_format *fmt,
131 unsigned int *nbuffers, unsigned int *nplanes,
132 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300133{
134 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300135 struct vpif_fh *fh = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300136 struct channel_obj *ch = fh->channel;
137 struct common_obj *common;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300138 unsigned long size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300139
140 common = &ch->common[VPIF_VIDEO_INDEX];
141
142 vpif_dbg(2, debug, "vpif_buffer_setup\n");
143
144 /* If memory type is not mmap, return */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300145 if (V4L2_MEMORY_MMAP == common->memory) {
146 /* Calculate the size of the buffer */
147 size = config_params.channel_bufsize[ch->channel_id];
148 /*
149 * Checking if the buffer size exceeds the available buffer
150 * ycmux_mode = 0 means 1 channel mode HD and
151 * ycmux_mode = 1 means 2 channels mode SD
152 */
153 if (ch->vpifparams.std_info.ycmux_mode == 0) {
154 if (config_params.video_limit[ch->channel_id])
155 while (size * *nbuffers >
156 (config_params.video_limit[0]
Manjunath Hadli764af392012-04-13 04:49:34 -0300157 + config_params.video_limit[1]))
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300158 (*nbuffers)--;
159 } else {
160 if (config_params.video_limit[ch->channel_id])
161 while (size * *nbuffers >
Manjunath Hadli764af392012-04-13 04:49:34 -0300162 config_params.video_limit[ch->channel_id])
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300163 (*nbuffers)--;
164 }
165
166 } else {
167 size = common->fmt.fmt.pix.sizeimage;
Manjunath Hadli764af392012-04-13 04:49:34 -0300168 }
169
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300170 if (*nbuffers < config_params.min_numbuffers)
171 *nbuffers = config_params.min_numbuffers;
172
173 *nplanes = 1;
174 sizes[0] = size;
175 alloc_ctxs[0] = common->alloc_ctx;
176
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300177 return 0;
178}
179
180/**
181 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300182 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300183 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300184static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300185{
186 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300187 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300188 struct channel_obj *ch = fh->channel;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300189 struct vpif_cap_buffer *buf = container_of(vb,
190 struct vpif_cap_buffer, vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300191 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300192 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300193
194 common = &ch->common[VPIF_VIDEO_INDEX];
195
196 vpif_dbg(2, debug, "vpif_buffer_queue\n");
197
Hans Verkuilaec96832012-11-16 12:03:06 -0300198 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300199 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300200 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300201 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300202}
203
204/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300205 * vpif_buf_cleanup : Callback function to free buffer
206 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300207 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300208 * This function is called from the videobuf2 layer to free memory
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300209 * allocated to the buffers
210 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300211static void vpif_buf_cleanup(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300212{
213 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300214 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
215 struct vpif_cap_buffer *buf = container_of(vb,
216 struct vpif_cap_buffer, vb);
217 struct channel_obj *ch = fh->channel;
218 struct common_obj *common;
219 unsigned long flags;
220
221 common = &ch->common[VPIF_VIDEO_INDEX];
222
223 spin_lock_irqsave(&common->irqlock, flags);
224 if (vb->state == VB2_BUF_STATE_ACTIVE)
225 list_del_init(&buf->list);
226 spin_unlock_irqrestore(&common->irqlock, flags);
227
228}
229
230static void vpif_wait_prepare(struct vb2_queue *vq)
231{
232 struct vpif_fh *fh = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300233 struct channel_obj *ch = fh->channel;
234 struct common_obj *common;
235
236 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300237 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300238}
239
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300240static void vpif_wait_finish(struct vb2_queue *vq)
241{
242 struct vpif_fh *fh = vb2_get_drv_priv(vq);
243 struct channel_obj *ch = fh->channel;
244 struct common_obj *common;
245
246 common = &ch->common[VPIF_VIDEO_INDEX];
247 mutex_lock(&common->lock);
248}
249
250static int vpif_buffer_init(struct vb2_buffer *vb)
251{
252 struct vpif_cap_buffer *buf = container_of(vb,
253 struct vpif_cap_buffer, vb);
254
255 INIT_LIST_HEAD(&buf->list);
256
257 return 0;
258}
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300259
260static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
261 { {1, 1} };
262
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300263static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
264{
265 struct vpif_capture_config *vpif_config_data =
266 vpif_dev->platform_data;
267 struct vpif_fh *fh = vb2_get_drv_priv(vq);
268 struct channel_obj *ch = fh->channel;
269 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
270 struct vpif_params *vpif = &ch->vpifparams;
271 unsigned long addr = 0;
Hans Verkuilaec96832012-11-16 12:03:06 -0300272 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300273 int ret;
274
Hans Verkuilaec96832012-11-16 12:03:06 -0300275 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300276
277 /* Get the next frame from the buffer queue */
278 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
279 struct vpif_cap_buffer, list);
280 /* Remove buffer from the buffer queue */
281 list_del(&common->cur_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300282 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300283 /* Mark state of the current frame to active */
284 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
285 /* Initialize field_id and started member */
286 ch->field_id = 0;
287 common->started = 1;
288 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
289
290 /* Calculate the offset for Y and C data in the buffer */
291 vpif_calculate_offsets(ch);
292
293 if ((vpif->std_info.frm_fmt &&
294 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
295 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
296 (!vpif->std_info.frm_fmt &&
297 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
298 vpif_dbg(1, debug, "conflict in field format and std format\n");
299 return -EINVAL;
300 }
301
302 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300303 if (vpif_config_data->setup_input_channel_mode) {
304 ret = vpif_config_data->
305 setup_input_channel_mode(vpif->std_info.ycmux_mode);
306 if (ret < 0) {
307 vpif_dbg(1, debug, "can't set vpif channel mode\n");
308 return ret;
309 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300310 }
311
312 /* Call vpif_set_params function to set the parameters and addresses */
313 ret = vpif_set_video_params(vpif, ch->channel_id);
314
315 if (ret < 0) {
316 vpif_dbg(1, debug, "can't set video params\n");
317 return ret;
318 }
319
320 common->started = ret;
321 vpif_config_addr(ch, ret);
322
323 common->set_addr(addr + common->ytop_off,
324 addr + common->ybtm_off,
325 addr + common->ctop_off,
326 addr + common->cbtm_off);
327
328 /**
329 * Set interrupt for both the fields in VPIF Register enable channel in
330 * VPIF register
331 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300332 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300333 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
334 channel0_intr_assert();
335 channel0_intr_enable(1);
336 enable_channel0(1);
337 }
338 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
339 (common->started == 2)) {
340 channel1_intr_assert();
341 channel1_intr_enable(1);
342 enable_channel1(1);
343 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300344
345 return 0;
346}
347
348/* abort streaming and wait for last buffer */
Hans Verkuile37559b2014-04-17 02:47:21 -0300349static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300350{
351 struct vpif_fh *fh = vb2_get_drv_priv(vq);
352 struct channel_obj *ch = fh->channel;
353 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300354 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300355
356 if (!vb2_is_streaming(vq))
Hans Verkuile37559b2014-04-17 02:47:21 -0300357 return;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300358
359 common = &ch->common[VPIF_VIDEO_INDEX];
360
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300361 /* Disable channel as per its device type and channel id */
362 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
363 enable_channel0(0);
364 channel0_intr_enable(0);
365 }
366 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
367 (2 == common->started)) {
368 enable_channel1(0);
369 channel1_intr_enable(0);
370 }
371 common->started = 0;
372
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300373 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300374 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300375 if (common->cur_frm == common->next_frm) {
376 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
377 } else {
378 if (common->cur_frm != NULL)
379 vb2_buffer_done(&common->cur_frm->vb,
380 VB2_BUF_STATE_ERROR);
381 if (common->next_frm != NULL)
382 vb2_buffer_done(&common->next_frm->vb,
383 VB2_BUF_STATE_ERROR);
384 }
385
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300386 while (!list_empty(&common->dma_queue)) {
387 common->next_frm = list_entry(common->dma_queue.next,
388 struct vpif_cap_buffer, list);
389 list_del(&common->next_frm->list);
390 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
391 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300392 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300393}
394
395static struct vb2_ops video_qops = {
396 .queue_setup = vpif_buffer_queue_setup,
397 .wait_prepare = vpif_wait_prepare,
398 .wait_finish = vpif_wait_finish,
399 .buf_init = vpif_buffer_init,
400 .buf_prepare = vpif_buffer_prepare,
401 .start_streaming = vpif_start_streaming,
402 .stop_streaming = vpif_stop_streaming,
403 .buf_cleanup = vpif_buf_cleanup,
404 .buf_queue = vpif_buffer_queue,
405};
406
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300407/**
408 * vpif_process_buffer_complete: process a completed buffer
409 * @common: ptr to common channel object
410 *
411 * This function time stamp the buffer and mark it as DONE. It also
412 * wake up any process waiting on the QUEUE and set the next buffer
413 * as current
414 */
415static void vpif_process_buffer_complete(struct common_obj *common)
416{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300417 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300418 vb2_buffer_done(&common->cur_frm->vb,
419 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300420 /* Make curFrm pointing to nextFrm */
421 common->cur_frm = common->next_frm;
422}
423
424/**
425 * vpif_schedule_next_buffer: set next buffer address for capture
426 * @common : ptr to common channel object
427 *
428 * This function will get next buffer from the dma queue and
429 * set the buffer address in the vpif register for capture.
430 * the buffer is marked active
431 */
432static void vpif_schedule_next_buffer(struct common_obj *common)
433{
434 unsigned long addr = 0;
435
Hans Verkuilaec96832012-11-16 12:03:06 -0300436 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300437 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300438 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300439 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300440 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300441 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300442 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
443 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300444
445 /* Set top and bottom field addresses in VPIF registers */
446 common->set_addr(addr + common->ytop_off,
447 addr + common->ybtm_off,
448 addr + common->ctop_off,
449 addr + common->cbtm_off);
450}
451
452/**
453 * vpif_channel_isr : ISR handler for vpif capture
454 * @irq: irq number
455 * @dev_id: dev_id ptr
456 *
457 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300458 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300459 */
460static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
461{
462 struct vpif_device *dev = &vpif_obj;
463 struct common_obj *common;
464 struct channel_obj *ch;
465 enum v4l2_field field;
466 int channel_id = 0;
467 int fid = -1, i;
468
469 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300470 if (!vpif_intr_status(channel_id))
471 return IRQ_NONE;
472
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300473 ch = dev->dev[channel_id];
474
475 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
476
477 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
478 common = &ch->common[i];
479 /* skip If streaming is not started in this channel */
480 if (0 == common->started)
481 continue;
482
483 /* Check the field format */
484 if (1 == ch->vpifparams.std_info.frm_fmt) {
485 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300486 spin_lock(&common->irqlock);
487 if (list_empty(&common->dma_queue)) {
488 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300489 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300490 }
491 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300492
493 if (!channel_first_int[i][channel_id])
494 vpif_process_buffer_complete(common);
495
496 channel_first_int[i][channel_id] = 0;
497
498 vpif_schedule_next_buffer(common);
499
500
501 channel_first_int[i][channel_id] = 0;
502 } else {
503 /**
504 * Interlaced mode. If it is first interrupt, ignore
505 * it
506 */
507 if (channel_first_int[i][channel_id]) {
508 channel_first_int[i][channel_id] = 0;
509 continue;
510 }
511 if (0 == i) {
512 ch->field_id ^= 1;
513 /* Get field id from VPIF registers */
514 fid = vpif_channel_getfid(ch->channel_id);
515 if (fid != ch->field_id) {
516 /**
517 * If field id does not match stored
518 * field id, make them in sync
519 */
520 if (0 == fid)
521 ch->field_id = fid;
522 return IRQ_HANDLED;
523 }
524 }
525 /* device field id and local field id are in sync */
526 if (0 == fid) {
527 /* this is even field */
528 if (common->cur_frm == common->next_frm)
529 continue;
530
531 /* mark the current buffer as done */
532 vpif_process_buffer_complete(common);
533 } else if (1 == fid) {
534 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300535 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300536 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300537 (common->cur_frm != common->next_frm)) {
538 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300539 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300540 }
541 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300542
543 vpif_schedule_next_buffer(common);
544 }
545 }
546 }
547 return IRQ_HANDLED;
548}
549
550/**
551 * vpif_update_std_info() - update standard related info
552 * @ch: ptr to channel object
553 *
554 * For a given standard selected by application, update values
555 * in the device data structures
556 */
557static int vpif_update_std_info(struct channel_obj *ch)
558{
559 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
560 struct vpif_params *vpifparams = &ch->vpifparams;
561 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300562 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300563 struct video_obj *vid_ch = &ch->video;
564 int index;
565
566 vpif_dbg(2, debug, "vpif_update_std_info\n");
567
Mats Randgaardaa444402010-12-16 12:17:42 -0300568 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300569 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300570 if (config->hd_sd == 0) {
571 vpif_dbg(2, debug, "SD format\n");
572 if (config->stdid & vid_ch->stdid) {
573 memcpy(std_info, config, sizeof(*config));
574 break;
575 }
576 } else {
577 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300578 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
579 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300580 memcpy(std_info, config, sizeof(*config));
581 break;
582 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300583 }
584 }
585
586 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300587 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300588 return -EINVAL;
589
590 common->fmt.fmt.pix.width = std_info->width;
591 common->width = std_info->width;
592 common->fmt.fmt.pix.height = std_info->height;
593 common->height = std_info->height;
594 common->fmt.fmt.pix.bytesperline = std_info->width;
595 vpifparams->video_params.hpitch = std_info->width;
596 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300597
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300598 return 0;
599}
600
601/**
602 * vpif_calculate_offsets : This function calculates buffers offsets
603 * @ch : ptr to channel object
604 *
605 * This function calculates buffer offsets for Y and C in the top and
606 * bottom field
607 */
608static void vpif_calculate_offsets(struct channel_obj *ch)
609{
610 unsigned int hpitch, vpitch, sizeimage;
611 struct video_obj *vid_ch = &(ch->video);
612 struct vpif_params *vpifparams = &ch->vpifparams;
613 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
614 enum v4l2_field field = common->fmt.fmt.pix.field;
615
616 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
617
618 if (V4L2_FIELD_ANY == field) {
619 if (vpifparams->std_info.frm_fmt)
620 vid_ch->buf_field = V4L2_FIELD_NONE;
621 else
622 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
623 } else
624 vid_ch->buf_field = common->fmt.fmt.pix.field;
625
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300626 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300627
628 hpitch = common->fmt.fmt.pix.bytesperline;
629 vpitch = sizeimage / (hpitch * 2);
630
631 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
632 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
633 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
634 common->ytop_off = 0;
635 common->ybtm_off = hpitch;
636 common->ctop_off = sizeimage / 2;
637 common->cbtm_off = sizeimage / 2 + hpitch;
638 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
639 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
640 common->ytop_off = 0;
641 common->ybtm_off = sizeimage / 4;
642 common->ctop_off = sizeimage / 2;
643 common->cbtm_off = common->ctop_off + sizeimage / 4;
644 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
645 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
646 common->ybtm_off = 0;
647 common->ytop_off = sizeimage / 4;
648 common->cbtm_off = sizeimage / 2;
649 common->ctop_off = common->cbtm_off + sizeimage / 4;
650 }
651 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
652 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
653 vpifparams->video_params.storage_mode = 1;
654 else
655 vpifparams->video_params.storage_mode = 0;
656
657 if (1 == vpifparams->std_info.frm_fmt)
658 vpifparams->video_params.hpitch =
659 common->fmt.fmt.pix.bytesperline;
660 else {
661 if ((field == V4L2_FIELD_ANY)
662 || (field == V4L2_FIELD_INTERLACED))
663 vpifparams->video_params.hpitch =
664 common->fmt.fmt.pix.bytesperline * 2;
665 else
666 vpifparams->video_params.hpitch =
667 common->fmt.fmt.pix.bytesperline;
668 }
669
670 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
671}
672
673/**
674 * vpif_config_format: configure default frame format in the device
675 * ch : ptr to channel object
676 */
677static void vpif_config_format(struct channel_obj *ch)
678{
679 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
680
681 vpif_dbg(2, debug, "vpif_config_format\n");
682
683 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
684 if (config_params.numbuffers[ch->channel_id] == 0)
685 common->memory = V4L2_MEMORY_USERPTR;
686 else
687 common->memory = V4L2_MEMORY_MMAP;
688
689 common->fmt.fmt.pix.sizeimage
690 = config_params.channel_bufsize[ch->channel_id];
691
692 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
693 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
694 else
695 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
696 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
697}
698
699/**
700 * vpif_get_default_field() - Get default field type based on interface
701 * @vpif_params - ptr to vpif params
702 */
703static inline enum v4l2_field vpif_get_default_field(
704 struct vpif_interface *iface)
705{
706 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
707 V4L2_FIELD_INTERLACED;
708}
709
710/**
711 * vpif_check_format() - check given pixel format for compatibility
712 * @ch - channel ptr
713 * @pixfmt - Given pixel format
714 * @update - update the values as per hardware requirement
715 *
716 * Check the application pixel format for S_FMT and update the input
717 * values as per hardware limits for TRY_FMT. The default pixel and
718 * field format is selected based on interface type.
719 */
720static int vpif_check_format(struct channel_obj *ch,
721 struct v4l2_pix_format *pixfmt,
722 int update)
723{
724 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
725 struct vpif_params *vpif_params = &ch->vpifparams;
726 enum v4l2_field field = pixfmt->field;
727 u32 sizeimage, hpitch, vpitch;
728 int ret = -EINVAL;
729
730 vpif_dbg(2, debug, "vpif_check_format\n");
731 /**
732 * first check for the pixel format. If if_type is Raw bayer,
733 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
734 * V4L2_PIX_FMT_YUV422P is supported
735 */
736 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
737 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
738 if (!update) {
739 vpif_dbg(2, debug, "invalid pix format\n");
740 goto exit;
741 }
742 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
743 }
744 } else {
745 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
746 if (!update) {
747 vpif_dbg(2, debug, "invalid pixel format\n");
748 goto exit;
749 }
750 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
751 }
752 }
753
754 if (!(VPIF_VALID_FIELD(field))) {
755 if (!update) {
756 vpif_dbg(2, debug, "invalid field format\n");
757 goto exit;
758 }
759 /**
760 * By default use FIELD_NONE for RAW Bayer capture
761 * and FIELD_INTERLACED for other interfaces
762 */
763 field = vpif_get_default_field(&vpif_params->iface);
764 } else if (field == V4L2_FIELD_ANY)
765 /* unsupported field. Use default */
766 field = vpif_get_default_field(&vpif_params->iface);
767
768 /* validate the hpitch */
769 hpitch = pixfmt->bytesperline;
770 if (hpitch < vpif_params->std_info.width) {
771 if (!update) {
772 vpif_dbg(2, debug, "invalid hpitch\n");
773 goto exit;
774 }
775 hpitch = vpif_params->std_info.width;
776 }
777
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300778 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300779
780 vpitch = sizeimage / (hpitch * 2);
781
782 /* validate the vpitch */
783 if (vpitch < vpif_params->std_info.height) {
784 if (!update) {
785 vpif_dbg(2, debug, "Invalid vpitch\n");
786 goto exit;
787 }
788 vpitch = vpif_params->std_info.height;
789 }
790
791 /* Check for 8 byte alignment */
792 if (!ALIGN(hpitch, 8)) {
793 if (!update) {
794 vpif_dbg(2, debug, "invalid pitch alignment\n");
795 goto exit;
796 }
797 /* adjust to next 8 byte boundary */
798 hpitch = (((hpitch + 7) / 8) * 8);
799 }
800 /* if update is set, modify the bytesperline and sizeimage */
801 if (update) {
802 pixfmt->bytesperline = hpitch;
803 pixfmt->sizeimage = hpitch * vpitch * 2;
804 }
805 /**
806 * Image width and height is always based on current standard width and
807 * height
808 */
809 pixfmt->width = common->fmt.fmt.pix.width;
810 pixfmt->height = common->fmt.fmt.pix.height;
811 return 0;
812exit:
813 return ret;
814}
815
816/**
817 * vpif_config_addr() - function to configure buffer address in vpif
818 * @ch - channel ptr
819 * @muxmode - channel mux mode
820 */
821static void vpif_config_addr(struct channel_obj *ch, int muxmode)
822{
823 struct common_obj *common;
824
825 vpif_dbg(2, debug, "vpif_config_addr\n");
826
827 common = &(ch->common[VPIF_VIDEO_INDEX]);
828
829 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
830 common->set_addr = ch1_set_videobuf_addr;
831 else if (2 == muxmode)
832 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
833 else
834 common->set_addr = ch0_set_videobuf_addr;
835}
836
837/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300838 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300839 * @filep: file pointer
840 * @vma: ptr to vm_area_struct
841 */
842static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
843{
844 /* Get the channel object and file handle object */
845 struct vpif_fh *fh = filep->private_data;
846 struct channel_obj *ch = fh->channel;
847 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300848 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300849
850 vpif_dbg(2, debug, "vpif_mmap\n");
851
Hans Verkuil72246792012-07-31 03:48:31 -0300852 if (mutex_lock_interruptible(&common->lock))
853 return -ERESTARTSYS;
854 ret = vb2_mmap(&common->buffer_queue, vma);
855 mutex_unlock(&common->lock);
856 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300857}
858
859/**
860 * vpif_poll: It is used for select/poll system call
861 * @filep: file pointer
862 * @wait: poll table to wait
863 */
864static unsigned int vpif_poll(struct file *filep, poll_table * wait)
865{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300866 struct vpif_fh *fh = filep->private_data;
867 struct channel_obj *channel = fh->channel;
868 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300869 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300870
871 vpif_dbg(2, debug, "vpif_poll\n");
872
Hans Verkuil72246792012-07-31 03:48:31 -0300873 if (common->started) {
874 mutex_lock(&common->lock);
875 res = vb2_poll(&common->buffer_queue, filep, wait);
876 mutex_unlock(&common->lock);
877 }
878 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300879}
880
881/**
882 * vpif_open : vpif open handler
883 * @filep: file ptr
884 *
885 * It creates object of file handle structure and stores it in private_data
886 * member of filepointer
887 */
888static int vpif_open(struct file *filep)
889{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300890 struct video_device *vdev = video_devdata(filep);
891 struct common_obj *common;
892 struct video_obj *vid_ch;
893 struct channel_obj *ch;
894 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300895
896 vpif_dbg(2, debug, "vpif_open\n");
897
898 ch = video_get_drvdata(vdev);
899
900 vid_ch = &ch->video;
901 common = &ch->common[VPIF_VIDEO_INDEX];
902
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300903 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300904 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300905 if (NULL == fh) {
906 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300907 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300908 }
909
Hans Verkuil72246792012-07-31 03:48:31 -0300910 if (mutex_lock_interruptible(&common->lock)) {
911 kfree(fh);
912 return -ERESTARTSYS;
913 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300914 /* store pointer to fh in private_data member of filep */
915 filep->private_data = fh;
916 fh->channel = ch;
917 fh->initialized = 0;
918 /* If decoder is not initialized. initialize it */
919 if (!ch->initialized) {
920 fh->initialized = 1;
921 ch->initialized = 1;
922 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
923 }
924 /* Increment channel usrs counter */
925 ch->usrs++;
926 /* Set io_allowed member to false */
927 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
928 /* Initialize priority of this instance to default priority */
929 fh->prio = V4L2_PRIORITY_UNSET;
930 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300931 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300932 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300933}
934
935/**
936 * vpif_release : function to clean up file close
937 * @filep: file pointer
938 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300939 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300940 * handle
941 */
942static int vpif_release(struct file *filep)
943{
944 struct vpif_fh *fh = filep->private_data;
945 struct channel_obj *ch = fh->channel;
946 struct common_obj *common;
947
948 vpif_dbg(2, debug, "vpif_release\n");
949
950 common = &ch->common[VPIF_VIDEO_INDEX];
951
Hans Verkuil72246792012-07-31 03:48:31 -0300952 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300953 /* if this instance is doing IO */
954 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
955 /* Reset io_usrs member of channel object */
956 common->io_usrs = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300957 /* Free buffers allocated */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300958 vb2_queue_release(&common->buffer_queue);
959 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300960 }
961
962 /* Decrement channel usrs counter */
963 ch->usrs--;
964
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300965 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300966 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300967
968 if (fh->initialized)
969 ch->initialized = 0;
970
Hans Verkuil72246792012-07-31 03:48:31 -0300971 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300972 filep->private_data = NULL;
973 kfree(fh);
974 return 0;
975}
976
977/**
978 * vpif_reqbufs() - request buffer handler
979 * @file: file ptr
980 * @priv: file handle
981 * @reqbuf: request buffer structure ptr
982 */
983static int vpif_reqbufs(struct file *file, void *priv,
984 struct v4l2_requestbuffers *reqbuf)
985{
986 struct vpif_fh *fh = priv;
987 struct channel_obj *ch = fh->channel;
988 struct common_obj *common;
989 u8 index = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300990 struct vb2_queue *q;
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -0300991 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300992
993 vpif_dbg(2, debug, "vpif_reqbufs\n");
994
995 /**
996 * This file handle has not initialized the channel,
997 * It is not allowed to do settings
998 */
999 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
1000 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1001 if (!fh->initialized) {
1002 vpif_dbg(1, debug, "Channel Busy\n");
1003 return -EBUSY;
1004 }
1005 }
1006
Manjunath Hadli764af392012-04-13 04:49:34 -03001007 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001008 return -EINVAL;
1009
1010 index = VPIF_VIDEO_INDEX;
1011
1012 common = &ch->common[index];
1013
Hans Verkuil46656af2011-01-04 06:51:35 -03001014 if (0 != common->io_usrs)
1015 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001016
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001017 /* Initialize videobuf2 queue as per the buffer type */
1018 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
Wei Yongjun94b76a82012-11-15 09:18:17 -03001019 if (IS_ERR(common->alloc_ctx)) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001020 vpif_err("Failed to get the context\n");
Wei Yongjun94b76a82012-11-15 09:18:17 -03001021 return PTR_ERR(common->alloc_ctx);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001022 }
1023 q = &common->buffer_queue;
1024 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1025 q->io_modes = VB2_MMAP | VB2_USERPTR;
1026 q->drv_priv = fh;
1027 q->ops = &video_qops;
1028 q->mem_ops = &vb2_dma_contig_memops;
1029 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
Sakari Ailusade48682014-02-25 19:12:19 -03001030 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Hans Verkuilb3379c62014-02-24 13:51:03 -03001031 q->min_buffers_needed = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001032
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -03001033 ret = vb2_queue_init(q);
1034 if (ret) {
1035 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1036 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1037 return ret;
1038 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001039 /* Set io allowed member of file handle to TRUE */
1040 fh->io_allowed[index] = 1;
1041 /* Increment io usrs member of channel object to 1 */
1042 common->io_usrs = 1;
1043 /* Store type of memory requested in channel object */
1044 common->memory = reqbuf->memory;
1045 INIT_LIST_HEAD(&common->dma_queue);
1046
1047 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001048 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001049}
1050
1051/**
1052 * vpif_querybuf() - query buffer handler
1053 * @file: file ptr
1054 * @priv: file handle
1055 * @buf: v4l2 buffer structure ptr
1056 */
1057static int vpif_querybuf(struct file *file, void *priv,
1058 struct v4l2_buffer *buf)
1059{
1060 struct vpif_fh *fh = priv;
1061 struct channel_obj *ch = fh->channel;
1062 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1063
1064 vpif_dbg(2, debug, "vpif_querybuf\n");
1065
1066 if (common->fmt.type != buf->type)
1067 return -EINVAL;
1068
1069 if (common->memory != V4L2_MEMORY_MMAP) {
1070 vpif_dbg(1, debug, "Invalid memory\n");
1071 return -EINVAL;
1072 }
1073
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001074 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001075}
1076
1077/**
1078 * vpif_qbuf() - query buffer handler
1079 * @file: file ptr
1080 * @priv: file handle
1081 * @buf: v4l2 buffer structure ptr
1082 */
1083static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1084{
1085
1086 struct vpif_fh *fh = priv;
1087 struct channel_obj *ch = fh->channel;
1088 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1089 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001090
1091 vpif_dbg(2, debug, "vpif_qbuf\n");
1092
1093 if (common->fmt.type != tbuf.type) {
1094 vpif_err("invalid buffer type\n");
1095 return -EINVAL;
1096 }
1097
1098 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001099 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001100 return -EACCES;
1101 }
1102
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001103 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001104}
1105
1106/**
1107 * vpif_dqbuf() - query buffer handler
1108 * @file: file ptr
1109 * @priv: file handle
1110 * @buf: v4l2 buffer structure ptr
1111 */
1112static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1113{
1114 struct vpif_fh *fh = priv;
1115 struct channel_obj *ch = fh->channel;
1116 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1117
1118 vpif_dbg(2, debug, "vpif_dqbuf\n");
1119
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001120 return vb2_dqbuf(&common->buffer_queue, buf,
1121 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001122}
1123
1124/**
1125 * vpif_streamon() - streamon handler
1126 * @file: file ptr
1127 * @priv: file handle
1128 * @buftype: v4l2 buffer type
1129 */
1130static int vpif_streamon(struct file *file, void *priv,
1131 enum v4l2_buf_type buftype)
1132{
1133
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001134 struct vpif_fh *fh = priv;
1135 struct channel_obj *ch = fh->channel;
1136 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1137 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1138 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001139 int ret = 0;
1140
1141 vpif_dbg(2, debug, "vpif_streamon\n");
1142
1143 vpif = &ch->vpifparams;
1144
1145 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1146 vpif_dbg(1, debug, "buffer type not supported\n");
1147 return -EINVAL;
1148 }
1149
1150 /* If file handle is not allowed IO, return error */
1151 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1152 vpif_dbg(1, debug, "io not allowed\n");
1153 return -EACCES;
1154 }
1155
1156 /* If Streaming is already started, return error */
1157 if (common->started) {
1158 vpif_dbg(1, debug, "channel->started\n");
1159 return -EBUSY;
1160 }
1161
1162 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1163 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1164 vpif->std_info.ycmux_mode == 0) ||
1165 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1166 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1167 vpif_dbg(1, debug, "other channel is being used\n");
1168 return -EBUSY;
1169 }
1170
1171 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1172 if (ret)
1173 return ret;
1174
1175 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001176 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001177
Hans Verkuil178cce12012-09-20 09:06:30 -03001178 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001179 vpif_dbg(1, debug, "stream on failed in subdev\n");
1180 return ret;
1181 }
1182
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001183 /* Call vb2_streamon to start streaming in videobuf2 */
1184 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001185 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001186 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001187 return ret;
1188 }
1189
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001190 return ret;
1191}
1192
1193/**
1194 * vpif_streamoff() - streamoff handler
1195 * @file: file ptr
1196 * @priv: file handle
1197 * @buftype: v4l2 buffer type
1198 */
1199static int vpif_streamoff(struct file *file, void *priv,
1200 enum v4l2_buf_type buftype)
1201{
1202
1203 struct vpif_fh *fh = priv;
1204 struct channel_obj *ch = fh->channel;
1205 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1206 int ret;
1207
1208 vpif_dbg(2, debug, "vpif_streamoff\n");
1209
1210 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1211 vpif_dbg(1, debug, "buffer type not supported\n");
1212 return -EINVAL;
1213 }
1214
1215 /* If io is allowed for this file handle, return error */
1216 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1217 vpif_dbg(1, debug, "io not allowed\n");
1218 return -EACCES;
1219 }
1220
1221 /* If streaming is not started, return error */
1222 if (!common->started) {
1223 vpif_dbg(1, debug, "channel->started\n");
1224 return -EINVAL;
1225 }
1226
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001227 /* disable channel */
1228 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1229 enable_channel0(0);
1230 channel0_intr_enable(0);
1231 } else {
1232 enable_channel1(0);
1233 channel1_intr_enable(0);
1234 }
1235
1236 common->started = 0;
1237
Hans Verkuil178cce12012-09-20 09:06:30 -03001238 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001239
Hans Verkuil178cce12012-09-20 09:06:30 -03001240 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001241 vpif_dbg(1, debug, "stream off failed in subdev\n");
1242
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001243 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001244}
1245
1246/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001247 * vpif_input_to_subdev() - Maps input to sub device
1248 * @vpif_cfg - global config ptr
1249 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001250 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001251 *
1252 * lookup the sub device information for a given input index.
1253 * we report all the inputs to application. inputs table also
1254 * has sub device name for the each input
1255 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001256static int vpif_input_to_subdev(
1257 struct vpif_capture_config *vpif_cfg,
1258 struct vpif_capture_chan_config *chan_cfg,
1259 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001260{
Hans Verkuil178cce12012-09-20 09:06:30 -03001261 struct vpif_subdev_info *subdev_info;
1262 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001263 int i;
1264
Hans Verkuil178cce12012-09-20 09:06:30 -03001265 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001266
Hans Verkuil178cce12012-09-20 09:06:30 -03001267 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1268 if (subdev_name == NULL)
1269 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001270
1271 /* loop through the sub device list to get the sub device info */
1272 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1273 subdev_info = &vpif_cfg->subdev_info[i];
1274 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001275 return i;
1276 }
1277 return -1;
1278}
1279
1280/**
1281 * vpif_set_input() - Select an input
1282 * @vpif_cfg - global config ptr
1283 * @ch - channel
1284 * @_index - Given input index from application
1285 *
1286 * Select the given input.
1287 */
1288static int vpif_set_input(
1289 struct vpif_capture_config *vpif_cfg,
1290 struct channel_obj *ch,
1291 int index)
1292{
1293 struct vpif_capture_chan_config *chan_cfg =
1294 &vpif_cfg->chan_config[ch->channel_id];
1295 struct vpif_subdev_info *subdev_info = NULL;
1296 struct v4l2_subdev *sd = NULL;
1297 u32 input = 0, output = 0;
1298 int sd_index;
1299 int ret;
1300
1301 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1302 if (sd_index >= 0) {
1303 sd = vpif_obj.sd[sd_index];
1304 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001305 }
1306
Hans Verkuil178cce12012-09-20 09:06:30 -03001307 /* first setup input path from sub device to vpif */
1308 if (sd && vpif_cfg->setup_input_path) {
1309 ret = vpif_cfg->setup_input_path(ch->channel_id,
1310 subdev_info->name);
1311 if (ret < 0) {
1312 vpif_dbg(1, debug, "couldn't setup input path for the" \
1313 " sub device %s, for input index %d\n",
1314 subdev_info->name, index);
1315 return ret;
1316 }
1317 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001318
Hans Verkuil178cce12012-09-20 09:06:30 -03001319 if (sd) {
1320 input = chan_cfg->inputs[index].input_route;
1321 output = chan_cfg->inputs[index].output_route;
1322 ret = v4l2_subdev_call(sd, video, s_routing,
1323 input, output, 0);
1324 if (ret < 0 && ret != -ENOIOCTLCMD) {
1325 vpif_dbg(1, debug, "Failed to set input\n");
1326 return ret;
1327 }
1328 }
1329 ch->input_idx = index;
1330 ch->sd = sd;
1331 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001332 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001333
Hans Verkuil178cce12012-09-20 09:06:30 -03001334 /* update tvnorms from the sub device input info */
1335 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1336 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001337}
1338
1339/**
1340 * vpif_querystd() - querystd handler
1341 * @file: file ptr
1342 * @priv: file handle
1343 * @std_id: ptr to std id
1344 *
1345 * This function is called to detect standard at the selected input
1346 */
1347static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1348{
1349 struct vpif_fh *fh = priv;
1350 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001351 int ret = 0;
1352
1353 vpif_dbg(2, debug, "vpif_querystd\n");
1354
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001355 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001356 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001357
Hans Verkuil178cce12012-09-20 09:06:30 -03001358 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1359 return -ENODATA;
1360 if (ret) {
1361 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1362 return ret;
1363 }
1364
1365 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001366}
1367
1368/**
1369 * vpif_g_std() - get STD handler
1370 * @file: file ptr
1371 * @priv: file handle
1372 * @std_id: ptr to std id
1373 */
1374static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1375{
1376 struct vpif_fh *fh = priv;
1377 struct channel_obj *ch = fh->channel;
1378
1379 vpif_dbg(2, debug, "vpif_g_std\n");
1380
1381 *std = ch->video.stdid;
1382 return 0;
1383}
1384
1385/**
1386 * vpif_s_std() - set STD handler
1387 * @file: file ptr
1388 * @priv: file handle
1389 * @std_id: ptr to std id
1390 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001391static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001392{
1393 struct vpif_fh *fh = priv;
1394 struct channel_obj *ch = fh->channel;
1395 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1396 int ret = 0;
1397
1398 vpif_dbg(2, debug, "vpif_s_std\n");
1399
1400 if (common->started) {
1401 vpif_err("streaming in progress\n");
1402 return -EBUSY;
1403 }
1404
1405 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1406 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1407 if (!fh->initialized) {
1408 vpif_dbg(1, debug, "Channel Busy\n");
1409 return -EBUSY;
1410 }
1411 }
1412
Hans Verkuilffb48772010-05-01 08:03:24 -03001413 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001414 if (0 != ret)
1415 return ret;
1416
1417 fh->initialized = 1;
1418
1419 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001420 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001421 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001422
1423 /* Get the information about the standard */
1424 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001425 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001426 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001427 }
1428
1429 /* Configure the default format information */
1430 vpif_config_format(ch);
1431
1432 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001433 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001434 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001435 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001436 return ret;
1437 }
1438 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001439}
1440
1441/**
1442 * vpif_enum_input() - ENUMINPUT handler
1443 * @file: file ptr
1444 * @priv: file handle
1445 * @input: ptr to input structure
1446 */
1447static int vpif_enum_input(struct file *file, void *priv,
1448 struct v4l2_input *input)
1449{
1450
1451 struct vpif_capture_config *config = vpif_dev->platform_data;
1452 struct vpif_capture_chan_config *chan_cfg;
1453 struct vpif_fh *fh = priv;
1454 struct channel_obj *ch = fh->channel;
1455
1456 chan_cfg = &config->chan_config[ch->channel_id];
1457
1458 if (input->index >= chan_cfg->input_count) {
1459 vpif_dbg(1, debug, "Invalid input index\n");
1460 return -EINVAL;
1461 }
1462
1463 memcpy(input, &chan_cfg->inputs[input->index].input,
1464 sizeof(*input));
1465 return 0;
1466}
1467
1468/**
1469 * vpif_g_input() - Get INPUT handler
1470 * @file: file ptr
1471 * @priv: file handle
1472 * @index: ptr to input index
1473 */
1474static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1475{
1476 struct vpif_fh *fh = priv;
1477 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001478
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001479 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001480 return 0;
1481}
1482
1483/**
1484 * vpif_s_input() - Set INPUT handler
1485 * @file: file ptr
1486 * @priv: file handle
1487 * @index: input index
1488 */
1489static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1490{
1491 struct vpif_capture_config *config = vpif_dev->platform_data;
1492 struct vpif_capture_chan_config *chan_cfg;
1493 struct vpif_fh *fh = priv;
1494 struct channel_obj *ch = fh->channel;
1495 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001496 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001497
1498 chan_cfg = &config->chan_config[ch->channel_id];
1499
Hans Verkuil7aaad132012-09-20 09:06:25 -03001500 if (index >= chan_cfg->input_count)
1501 return -EINVAL;
1502
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001503 if (common->started) {
1504 vpif_err("Streaming in progress\n");
1505 return -EBUSY;
1506 }
1507
1508 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1509 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1510 if (!fh->initialized) {
1511 vpif_dbg(1, debug, "Channel Busy\n");
1512 return -EBUSY;
1513 }
1514 }
1515
Hans Verkuilffb48772010-05-01 08:03:24 -03001516 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001517 if (0 != ret)
1518 return ret;
1519
1520 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001521 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001522}
1523
1524/**
1525 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1526 * @file: file ptr
1527 * @priv: file handle
1528 * @index: input index
1529 */
1530static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1531 struct v4l2_fmtdesc *fmt)
1532{
1533 struct vpif_fh *fh = priv;
1534 struct channel_obj *ch = fh->channel;
1535
1536 if (fmt->index != 0) {
1537 vpif_dbg(1, debug, "Invalid format index\n");
1538 return -EINVAL;
1539 }
1540
1541 /* Fill in the information about format */
1542 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1543 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1544 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1545 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1546 } else {
1547 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1548 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1549 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1550 }
1551 return 0;
1552}
1553
1554/**
1555 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1556 * @file: file ptr
1557 * @priv: file handle
1558 * @fmt: ptr to v4l2 format structure
1559 */
1560static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1561 struct v4l2_format *fmt)
1562{
1563 struct vpif_fh *fh = priv;
1564 struct channel_obj *ch = fh->channel;
1565 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1566
1567 return vpif_check_format(ch, pixfmt, 1);
1568}
1569
1570
1571/**
1572 * vpif_g_fmt_vid_cap() - Set INPUT handler
1573 * @file: file ptr
1574 * @priv: file handle
1575 * @fmt: ptr to v4l2 format structure
1576 */
1577static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1578 struct v4l2_format *fmt)
1579{
1580 struct vpif_fh *fh = priv;
1581 struct channel_obj *ch = fh->channel;
1582 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1583
1584 /* Check the validity of the buffer type */
1585 if (common->fmt.type != fmt->type)
1586 return -EINVAL;
1587
1588 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001589 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001590 return 0;
1591}
1592
1593/**
1594 * vpif_s_fmt_vid_cap() - Set FMT handler
1595 * @file: file ptr
1596 * @priv: file handle
1597 * @fmt: ptr to v4l2 format structure
1598 */
1599static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1600 struct v4l2_format *fmt)
1601{
1602 struct vpif_fh *fh = priv;
1603 struct channel_obj *ch = fh->channel;
1604 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1605 struct v4l2_pix_format *pixfmt;
1606 int ret = 0;
1607
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001608 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001609
1610 /* If streaming is started, return error */
1611 if (common->started) {
1612 vpif_dbg(1, debug, "Streaming is started\n");
1613 return -EBUSY;
1614 }
1615
1616 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1617 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1618 if (!fh->initialized) {
1619 vpif_dbg(1, debug, "Channel Busy\n");
1620 return -EBUSY;
1621 }
1622 }
1623
Hans Verkuilffb48772010-05-01 08:03:24 -03001624 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001625 if (0 != ret)
1626 return ret;
1627
1628 fh->initialized = 1;
1629
1630 pixfmt = &fmt->fmt.pix;
1631 /* Check for valid field format */
1632 ret = vpif_check_format(ch, pixfmt, 0);
1633
1634 if (ret)
1635 return ret;
1636 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001637 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001638 return 0;
1639}
1640
1641/**
1642 * vpif_querycap() - QUERYCAP handler
1643 * @file: file ptr
1644 * @priv: file handle
1645 * @cap: ptr to v4l2_capability structure
1646 */
1647static int vpif_querycap(struct file *file, void *priv,
1648 struct v4l2_capability *cap)
1649{
1650 struct vpif_capture_config *config = vpif_dev->platform_data;
1651
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001652 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1653 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1654 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1655 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1656 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001657 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1658
1659 return 0;
1660}
1661
1662/**
1663 * vpif_g_priority() - get priority handler
1664 * @file: file ptr
1665 * @priv: file handle
1666 * @prio: ptr to v4l2_priority structure
1667 */
1668static int vpif_g_priority(struct file *file, void *priv,
1669 enum v4l2_priority *prio)
1670{
1671 struct vpif_fh *fh = priv;
1672 struct channel_obj *ch = fh->channel;
1673
1674 *prio = v4l2_prio_max(&ch->prio);
1675
1676 return 0;
1677}
1678
1679/**
1680 * vpif_s_priority() - set priority handler
1681 * @file: file ptr
1682 * @priv: file handle
1683 * @prio: ptr to v4l2_priority structure
1684 */
1685static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1686{
1687 struct vpif_fh *fh = priv;
1688 struct channel_obj *ch = fh->channel;
1689
1690 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1691}
1692
1693/**
1694 * vpif_cropcap() - cropcap handler
1695 * @file: file ptr
1696 * @priv: file handle
1697 * @crop: ptr to v4l2_cropcap structure
1698 */
1699static int vpif_cropcap(struct file *file, void *priv,
1700 struct v4l2_cropcap *crop)
1701{
1702 struct vpif_fh *fh = priv;
1703 struct channel_obj *ch = fh->channel;
1704 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1705
1706 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1707 return -EINVAL;
1708
1709 crop->bounds.left = 0;
1710 crop->bounds.top = 0;
1711 crop->bounds.height = common->height;
1712 crop->bounds.width = common->width;
1713 crop->defrect = crop->bounds;
1714 return 0;
1715}
1716
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001717/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001718 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001719 * @file: file ptr
1720 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001721 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001722 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001723static int
1724vpif_enum_dv_timings(struct file *file, void *priv,
1725 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001726{
1727 struct vpif_fh *fh = priv;
1728 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001729 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001730
Hans Verkuil178cce12012-09-20 09:06:30 -03001731 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001732 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001733 return -EINVAL;
1734 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001735}
1736
1737/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001738 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001739 * @file: file ptr
1740 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001741 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001742 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001743static int
1744vpif_query_dv_timings(struct file *file, void *priv,
1745 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001746{
1747 struct vpif_fh *fh = priv;
1748 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001749 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001750
Hans Verkuil178cce12012-09-20 09:06:30 -03001751 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001752 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001753 return -ENODATA;
1754 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001755}
1756
Mats Randgaardc027e162010-12-16 12:17:44 -03001757/**
1758 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1759 * @file: file ptr
1760 * @priv: file handle
1761 * @timings: digital video timings
1762 */
1763static int vpif_s_dv_timings(struct file *file, void *priv,
1764 struct v4l2_dv_timings *timings)
1765{
1766 struct vpif_fh *fh = priv;
1767 struct channel_obj *ch = fh->channel;
1768 struct vpif_params *vpifparams = &ch->vpifparams;
1769 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1770 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001771 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001772 int ret;
1773
1774 if (timings->type != V4L2_DV_BT_656_1120) {
1775 vpif_dbg(2, debug, "Timing type not defined\n");
1776 return -EINVAL;
1777 }
1778
1779 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001780 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1781 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1782 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001783 if (ret < 0) {
1784 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1785 return ret;
1786 }
1787
1788 if (!(timings->bt.width && timings->bt.height &&
1789 (timings->bt.hbackporch ||
1790 timings->bt.hfrontporch ||
1791 timings->bt.hsync) &&
1792 timings->bt.vfrontporch &&
1793 (timings->bt.vbackporch ||
1794 timings->bt.vsync))) {
1795 vpif_dbg(2, debug, "Timings for width, height, "
1796 "horizontal back porch, horizontal sync, "
1797 "horizontal front porch, vertical back porch, "
1798 "vertical sync and vertical back porch "
1799 "must be defined\n");
1800 return -EINVAL;
1801 }
1802
Hans Verkuil0598c172012-09-18 07:18:47 -03001803 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001804
1805 /* Configure video port timings */
1806
Hans Verkuile3655262013-07-29 08:41:00 -03001807 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001808 std_info->sav2eav = bt->width;
1809
1810 std_info->l1 = 1;
1811 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1812
Hans Verkuile3655262013-07-29 08:41:00 -03001813 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001814 if (bt->interlaced) {
1815 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001816 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 {
Mats Randgaardc027e162010-12-16 12:17:44 -03001829 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1830 }
1831 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1832 std_info->width = bt->width;
1833 std_info->height = bt->height;
1834 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1835 std_info->ycmux_mode = 0;
1836 std_info->capture_format = 0;
1837 std_info->vbi_supported = 0;
1838 std_info->hd_sd = 1;
1839 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001840
1841 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001842 return 0;
1843}
1844
1845/**
1846 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1847 * @file: file ptr
1848 * @priv: file handle
1849 * @timings: digital video timings
1850 */
1851static int vpif_g_dv_timings(struct file *file, void *priv,
1852 struct v4l2_dv_timings *timings)
1853{
1854 struct vpif_fh *fh = priv;
1855 struct channel_obj *ch = fh->channel;
1856 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001857
Hans Verkuil0598c172012-09-18 07:18:47 -03001858 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001859
1860 return 0;
1861}
1862
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001863/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001864 * vpif_log_status() - Status information
1865 * @file: file ptr
1866 * @priv: file handle
1867 *
1868 * Returns zero.
1869 */
1870static int vpif_log_status(struct file *filep, void *priv)
1871{
1872 /* status for sub devices */
1873 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1874
1875 return 0;
1876}
1877
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001878/* vpif capture ioctl operations */
1879static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1880 .vidioc_querycap = vpif_querycap,
1881 .vidioc_g_priority = vpif_g_priority,
1882 .vidioc_s_priority = vpif_s_priority,
1883 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1884 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1885 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1886 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1887 .vidioc_enum_input = vpif_enum_input,
1888 .vidioc_s_input = vpif_s_input,
1889 .vidioc_g_input = vpif_g_input,
1890 .vidioc_reqbufs = vpif_reqbufs,
1891 .vidioc_querybuf = vpif_querybuf,
1892 .vidioc_querystd = vpif_querystd,
1893 .vidioc_s_std = vpif_s_std,
1894 .vidioc_g_std = vpif_g_std,
1895 .vidioc_qbuf = vpif_qbuf,
1896 .vidioc_dqbuf = vpif_dqbuf,
1897 .vidioc_streamon = vpif_streamon,
1898 .vidioc_streamoff = vpif_streamoff,
1899 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001900 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1901 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001902 .vidioc_s_dv_timings = vpif_s_dv_timings,
1903 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001904 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001905};
1906
1907/* vpif file operations */
1908static struct v4l2_file_operations vpif_fops = {
1909 .owner = THIS_MODULE,
1910 .open = vpif_open,
1911 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001912 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001913 .mmap = vpif_mmap,
1914 .poll = vpif_poll
1915};
1916
1917/* vpif video template */
1918static struct video_device vpif_video_template = {
1919 .name = "vpif",
1920 .fops = &vpif_fops,
1921 .minor = -1,
1922 .ioctl_ops = &vpif_ioctl_ops,
1923};
1924
1925/**
1926 * initialize_vpif() - Initialize vpif data structures
1927 *
1928 * Allocate memory for data structures and initialize them
1929 */
1930static int initialize_vpif(void)
1931{
1932 int err = 0, i, j;
1933 int free_channel_objects_index;
1934
1935 /* Default number of buffers should be 3 */
1936 if ((ch0_numbuffers > 0) &&
1937 (ch0_numbuffers < config_params.min_numbuffers))
1938 ch0_numbuffers = config_params.min_numbuffers;
1939 if ((ch1_numbuffers > 0) &&
1940 (ch1_numbuffers < config_params.min_numbuffers))
1941 ch1_numbuffers = config_params.min_numbuffers;
1942
1943 /* Set buffer size to min buffers size if it is invalid */
1944 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1945 ch0_bufsize =
1946 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1947 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1948 ch1_bufsize =
1949 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1950
1951 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1952 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1953 if (ch0_numbuffers) {
1954 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1955 = ch0_bufsize;
1956 }
1957 if (ch1_numbuffers) {
1958 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1959 = ch1_bufsize;
1960 }
1961
1962 /* Allocate memory for six channel objects */
1963 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1964 vpif_obj.dev[i] =
1965 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1966 /* If memory allocation fails, return error */
1967 if (!vpif_obj.dev[i]) {
1968 free_channel_objects_index = i;
1969 err = -ENOMEM;
1970 goto vpif_init_free_channel_objects;
1971 }
1972 }
1973 return 0;
1974
1975vpif_init_free_channel_objects:
1976 for (j = 0; j < free_channel_objects_index; j++)
1977 kfree(vpif_obj.dev[j]);
1978 return err;
1979}
1980
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001981static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1982 struct v4l2_subdev *subdev,
1983 struct v4l2_async_subdev *asd)
1984{
1985 int i;
1986
1987 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1988 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1989 subdev->name)) {
1990 vpif_obj.sd[i] = subdev;
1991 return 0;
1992 }
1993
1994 return -EINVAL;
1995}
1996
1997static int vpif_probe_complete(void)
1998{
1999 struct common_obj *common;
2000 struct channel_obj *ch;
2001 int i, j, err, k;
2002
2003 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2004 ch = vpif_obj.dev[j];
2005 ch->channel_id = j;
2006 common = &(ch->common[VPIF_VIDEO_INDEX]);
2007 spin_lock_init(&common->irqlock);
2008 mutex_init(&common->lock);
2009 ch->video_dev->lock = &common->lock;
2010 /* Initialize prio member of channel object */
2011 v4l2_prio_init(&ch->prio);
2012 video_set_drvdata(ch->video_dev, ch);
2013
2014 /* select input 0 */
2015 err = vpif_set_input(vpif_obj.config, ch, 0);
2016 if (err)
2017 goto probe_out;
2018
2019 err = video_register_device(ch->video_dev,
2020 VFL_TYPE_GRABBER, (j ? 1 : 0));
2021 if (err)
2022 goto probe_out;
2023 }
2024
2025 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2026 return 0;
2027
2028probe_out:
2029 for (k = 0; k < j; k++) {
2030 /* Get the pointer to the channel object */
2031 ch = vpif_obj.dev[k];
2032 /* Unregister video device */
2033 video_unregister_device(ch->video_dev);
2034 }
2035 kfree(vpif_obj.sd);
2036 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2037 ch = vpif_obj.dev[i];
2038 /* Note: does nothing if ch->video_dev == NULL */
2039 video_device_release(ch->video_dev);
2040 }
2041 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2042
2043 return err;
2044}
2045
2046static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2047{
2048 return vpif_probe_complete();
2049}
2050
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002051/**
2052 * vpif_probe : This function probes the vpif capture driver
2053 * @pdev: platform device pointer
2054 *
2055 * This creates device entries by register itself to the V4L2 driver and
2056 * initializes fields of each channel objects
2057 */
2058static __init int vpif_probe(struct platform_device *pdev)
2059{
2060 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002061 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002062 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002063 struct i2c_adapter *i2c_adap;
2064 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002065 struct video_device *vfd;
2066 struct resource *res;
2067 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03002068 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002069
2070 vpif_dev = &pdev->dev;
2071
2072 err = initialize_vpif();
2073 if (err) {
2074 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2075 return err;
2076 }
2077
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002078 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2079 if (err) {
2080 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2081 return err;
2082 }
2083
Hans Verkuil5be452c2012-09-20 09:06:29 -03002084 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03002085 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2086 IRQF_SHARED, "VPIF_Capture",
2087 (void *)(&vpif_obj.dev[res_idx]->
2088 channel_id));
2089 if (err) {
2090 err = -EINVAL;
2091 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002092 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002093 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002094 }
2095
2096 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2097 /* Get the pointer to the channel object */
2098 ch = vpif_obj.dev[i];
2099 /* Allocate memory for video device */
2100 vfd = video_device_alloc();
2101 if (NULL == vfd) {
2102 for (j = 0; j < i; j++) {
2103 ch = vpif_obj.dev[j];
2104 video_device_release(ch->video_dev);
2105 }
2106 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002107 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002108 }
2109
2110 /* Initialize field of video device */
2111 *vfd = vpif_video_template;
2112 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2113 vfd->release = video_device_release;
2114 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002115 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002116 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002117 /* Set video_dev to the video device */
2118 ch->video_dev = vfd;
2119 }
2120
Manjunath Hadli764af392012-04-13 04:49:34 -03002121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2122 if (res) {
2123 size = resource_size(res);
2124 /* The resources are divided into two equal memory and when we
2125 * have HD output we can add them together
2126 */
2127 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2128 ch = vpif_obj.dev[j];
2129 ch->channel_id = j;
2130 /* only enabled if second resource exists */
2131 config_params.video_limit[ch->channel_id] = 0;
2132 if (size)
2133 config_params.video_limit[ch->channel_id] =
2134 size/2;
2135 }
2136 }
2137
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002138 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002139
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002140 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002141 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002142 GFP_KERNEL);
2143 if (vpif_obj.sd == NULL) {
2144 vpif_err("unable to allocate memory for subdevice pointers\n");
2145 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002146 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002147 }
2148
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002149 if (!vpif_obj.config->asd_sizes) {
2150 i2c_adap = i2c_get_adapter(1);
2151 for (i = 0; i < subdev_count; i++) {
2152 subdevdata = &vpif_obj.config->subdev_info[i];
2153 vpif_obj.sd[i] =
2154 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2155 i2c_adap,
2156 &subdevdata->
2157 board_info,
2158 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002159
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002160 if (!vpif_obj.sd[i]) {
2161 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03002162 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002163 goto probe_subdev_out;
2164 }
2165 v4l2_info(&vpif_obj.v4l2_dev,
2166 "registered sub device %s\n",
2167 subdevdata->name);
2168 }
2169 vpif_probe_complete();
2170 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03002171 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002172 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2173 vpif_obj.notifier.bound = vpif_async_bound;
2174 vpif_obj.notifier.complete = vpif_async_complete;
2175 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2176 &vpif_obj.notifier);
2177 if (err) {
2178 vpif_err("Error registering async notifier\n");
2179 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002180 goto probe_subdev_out;
2181 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002182 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002183
2184 return 0;
2185
Hans Verkuilb65814e2012-09-20 09:06:26 -03002186probe_subdev_out:
2187 /* free sub devices memory */
2188 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002189
Hans Verkuil5be452c2012-09-20 09:06:29 -03002190vpif_sd_error:
2191 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2192 ch = vpif_obj.dev[i];
2193 /* Note: does nothing if ch->video_dev == NULL */
2194 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002195 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002196vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002197 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002198
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002199 return err;
2200}
2201
2202/**
2203 * vpif_remove() - driver remove handler
2204 * @device: ptr to platform device structure
2205 *
2206 * The vidoe device is unregistered
2207 */
2208static int vpif_remove(struct platform_device *device)
2209{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002210 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002211 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002212
2213 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2214
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002215 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002216 /* un-register device */
2217 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2218 /* Get the pointer to the channel object */
2219 ch = vpif_obj.dev[i];
2220 /* Unregister video device */
2221 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002222 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002223 }
2224 return 0;
2225}
2226
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002227#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002228/**
2229 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002230 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002231static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002232{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002233
2234 struct common_obj *common;
2235 struct channel_obj *ch;
2236 int i;
2237
2238 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2239 /* Get the pointer to the channel object */
2240 ch = vpif_obj.dev[i];
2241 common = &ch->common[VPIF_VIDEO_INDEX];
2242 mutex_lock(&common->lock);
2243 if (ch->usrs && common->io_usrs) {
2244 /* Disable channel */
2245 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2246 enable_channel0(0);
2247 channel0_intr_enable(0);
2248 }
2249 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2250 common->started == 2) {
2251 enable_channel1(0);
2252 channel1_intr_enable(0);
2253 }
2254 }
2255 mutex_unlock(&common->lock);
2256 }
2257
2258 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002259}
2260
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002261/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002262 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002263 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002264static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002265{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002266 struct common_obj *common;
2267 struct channel_obj *ch;
2268 int i;
2269
2270 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2271 /* Get the pointer to the channel object */
2272 ch = vpif_obj.dev[i];
2273 common = &ch->common[VPIF_VIDEO_INDEX];
2274 mutex_lock(&common->lock);
2275 if (ch->usrs && common->io_usrs) {
2276 /* Disable channel */
2277 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2278 enable_channel0(1);
2279 channel0_intr_enable(1);
2280 }
2281 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2282 common->started == 2) {
2283 enable_channel1(1);
2284 channel1_intr_enable(1);
2285 }
2286 }
2287 mutex_unlock(&common->lock);
2288 }
2289
2290 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002291}
2292
Alexey Dobriyan47145212009-12-14 18:00:08 -08002293static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002294 .suspend = vpif_suspend,
2295 .resume = vpif_resume,
2296};
2297
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002298#define vpif_pm_ops (&vpif_dev_pm_ops)
2299#else
2300#define vpif_pm_ops NULL
2301#endif
2302
Mats Randgaardffa1b392010-08-30 10:30:36 -03002303static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002304 .driver = {
2305 .name = "vpif_capture",
2306 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002307 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002308 },
2309 .probe = vpif_probe,
2310 .remove = vpif_remove,
2311};
2312
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03002313module_platform_driver(vpif_driver);