blob: 756da78bac23109dbd3e7da464243da381354231 [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 */
349static int vpif_stop_streaming(struct vb2_queue *vq)
350{
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))
357 return 0;
358
359 common = &ch->common[VPIF_VIDEO_INDEX];
360
361 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300362 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300363 while (!list_empty(&common->dma_queue)) {
364 common->next_frm = list_entry(common->dma_queue.next,
365 struct vpif_cap_buffer, list);
366 list_del(&common->next_frm->list);
367 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
368 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300369 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300370
371 return 0;
372}
373
374static struct vb2_ops video_qops = {
375 .queue_setup = vpif_buffer_queue_setup,
376 .wait_prepare = vpif_wait_prepare,
377 .wait_finish = vpif_wait_finish,
378 .buf_init = vpif_buffer_init,
379 .buf_prepare = vpif_buffer_prepare,
380 .start_streaming = vpif_start_streaming,
381 .stop_streaming = vpif_stop_streaming,
382 .buf_cleanup = vpif_buf_cleanup,
383 .buf_queue = vpif_buffer_queue,
384};
385
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300386/**
387 * vpif_process_buffer_complete: process a completed buffer
388 * @common: ptr to common channel object
389 *
390 * This function time stamp the buffer and mark it as DONE. It also
391 * wake up any process waiting on the QUEUE and set the next buffer
392 * as current
393 */
394static void vpif_process_buffer_complete(struct common_obj *common)
395{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300396 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300397 vb2_buffer_done(&common->cur_frm->vb,
398 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300399 /* Make curFrm pointing to nextFrm */
400 common->cur_frm = common->next_frm;
401}
402
403/**
404 * vpif_schedule_next_buffer: set next buffer address for capture
405 * @common : ptr to common channel object
406 *
407 * This function will get next buffer from the dma queue and
408 * set the buffer address in the vpif register for capture.
409 * the buffer is marked active
410 */
411static void vpif_schedule_next_buffer(struct common_obj *common)
412{
413 unsigned long addr = 0;
414
Hans Verkuilaec96832012-11-16 12:03:06 -0300415 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300416 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300417 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300418 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300419 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300420 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300421 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
422 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300423
424 /* Set top and bottom field addresses in VPIF registers */
425 common->set_addr(addr + common->ytop_off,
426 addr + common->ybtm_off,
427 addr + common->ctop_off,
428 addr + common->cbtm_off);
429}
430
431/**
432 * vpif_channel_isr : ISR handler for vpif capture
433 * @irq: irq number
434 * @dev_id: dev_id ptr
435 *
436 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300437 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300438 */
439static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
440{
441 struct vpif_device *dev = &vpif_obj;
442 struct common_obj *common;
443 struct channel_obj *ch;
444 enum v4l2_field field;
445 int channel_id = 0;
446 int fid = -1, i;
447
448 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300449 if (!vpif_intr_status(channel_id))
450 return IRQ_NONE;
451
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300452 ch = dev->dev[channel_id];
453
454 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
455
456 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
457 common = &ch->common[i];
458 /* skip If streaming is not started in this channel */
459 if (0 == common->started)
460 continue;
461
462 /* Check the field format */
463 if (1 == ch->vpifparams.std_info.frm_fmt) {
464 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300465 spin_lock(&common->irqlock);
466 if (list_empty(&common->dma_queue)) {
467 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300468 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300469 }
470 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300471
472 if (!channel_first_int[i][channel_id])
473 vpif_process_buffer_complete(common);
474
475 channel_first_int[i][channel_id] = 0;
476
477 vpif_schedule_next_buffer(common);
478
479
480 channel_first_int[i][channel_id] = 0;
481 } else {
482 /**
483 * Interlaced mode. If it is first interrupt, ignore
484 * it
485 */
486 if (channel_first_int[i][channel_id]) {
487 channel_first_int[i][channel_id] = 0;
488 continue;
489 }
490 if (0 == i) {
491 ch->field_id ^= 1;
492 /* Get field id from VPIF registers */
493 fid = vpif_channel_getfid(ch->channel_id);
494 if (fid != ch->field_id) {
495 /**
496 * If field id does not match stored
497 * field id, make them in sync
498 */
499 if (0 == fid)
500 ch->field_id = fid;
501 return IRQ_HANDLED;
502 }
503 }
504 /* device field id and local field id are in sync */
505 if (0 == fid) {
506 /* this is even field */
507 if (common->cur_frm == common->next_frm)
508 continue;
509
510 /* mark the current buffer as done */
511 vpif_process_buffer_complete(common);
512 } else if (1 == fid) {
513 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300514 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300515 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300516 (common->cur_frm != common->next_frm)) {
517 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300518 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300519 }
520 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300521
522 vpif_schedule_next_buffer(common);
523 }
524 }
525 }
526 return IRQ_HANDLED;
527}
528
529/**
530 * vpif_update_std_info() - update standard related info
531 * @ch: ptr to channel object
532 *
533 * For a given standard selected by application, update values
534 * in the device data structures
535 */
536static int vpif_update_std_info(struct channel_obj *ch)
537{
538 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
539 struct vpif_params *vpifparams = &ch->vpifparams;
540 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300541 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300542 struct video_obj *vid_ch = &ch->video;
543 int index;
544
545 vpif_dbg(2, debug, "vpif_update_std_info\n");
546
Mats Randgaardaa444402010-12-16 12:17:42 -0300547 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300548 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300549 if (config->hd_sd == 0) {
550 vpif_dbg(2, debug, "SD format\n");
551 if (config->stdid & vid_ch->stdid) {
552 memcpy(std_info, config, sizeof(*config));
553 break;
554 }
555 } else {
556 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300557 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
558 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300559 memcpy(std_info, config, sizeof(*config));
560 break;
561 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300562 }
563 }
564
565 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300566 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300567 return -EINVAL;
568
569 common->fmt.fmt.pix.width = std_info->width;
570 common->width = std_info->width;
571 common->fmt.fmt.pix.height = std_info->height;
572 common->height = std_info->height;
573 common->fmt.fmt.pix.bytesperline = std_info->width;
574 vpifparams->video_params.hpitch = std_info->width;
575 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300576
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300577 return 0;
578}
579
580/**
581 * vpif_calculate_offsets : This function calculates buffers offsets
582 * @ch : ptr to channel object
583 *
584 * This function calculates buffer offsets for Y and C in the top and
585 * bottom field
586 */
587static void vpif_calculate_offsets(struct channel_obj *ch)
588{
589 unsigned int hpitch, vpitch, sizeimage;
590 struct video_obj *vid_ch = &(ch->video);
591 struct vpif_params *vpifparams = &ch->vpifparams;
592 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
593 enum v4l2_field field = common->fmt.fmt.pix.field;
594
595 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
596
597 if (V4L2_FIELD_ANY == field) {
598 if (vpifparams->std_info.frm_fmt)
599 vid_ch->buf_field = V4L2_FIELD_NONE;
600 else
601 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
602 } else
603 vid_ch->buf_field = common->fmt.fmt.pix.field;
604
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300605 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300606
607 hpitch = common->fmt.fmt.pix.bytesperline;
608 vpitch = sizeimage / (hpitch * 2);
609
610 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
611 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
612 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
613 common->ytop_off = 0;
614 common->ybtm_off = hpitch;
615 common->ctop_off = sizeimage / 2;
616 common->cbtm_off = sizeimage / 2 + hpitch;
617 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
618 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
619 common->ytop_off = 0;
620 common->ybtm_off = sizeimage / 4;
621 common->ctop_off = sizeimage / 2;
622 common->cbtm_off = common->ctop_off + sizeimage / 4;
623 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
624 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
625 common->ybtm_off = 0;
626 common->ytop_off = sizeimage / 4;
627 common->cbtm_off = sizeimage / 2;
628 common->ctop_off = common->cbtm_off + sizeimage / 4;
629 }
630 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
631 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
632 vpifparams->video_params.storage_mode = 1;
633 else
634 vpifparams->video_params.storage_mode = 0;
635
636 if (1 == vpifparams->std_info.frm_fmt)
637 vpifparams->video_params.hpitch =
638 common->fmt.fmt.pix.bytesperline;
639 else {
640 if ((field == V4L2_FIELD_ANY)
641 || (field == V4L2_FIELD_INTERLACED))
642 vpifparams->video_params.hpitch =
643 common->fmt.fmt.pix.bytesperline * 2;
644 else
645 vpifparams->video_params.hpitch =
646 common->fmt.fmt.pix.bytesperline;
647 }
648
649 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
650}
651
652/**
653 * vpif_config_format: configure default frame format in the device
654 * ch : ptr to channel object
655 */
656static void vpif_config_format(struct channel_obj *ch)
657{
658 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
659
660 vpif_dbg(2, debug, "vpif_config_format\n");
661
662 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
663 if (config_params.numbuffers[ch->channel_id] == 0)
664 common->memory = V4L2_MEMORY_USERPTR;
665 else
666 common->memory = V4L2_MEMORY_MMAP;
667
668 common->fmt.fmt.pix.sizeimage
669 = config_params.channel_bufsize[ch->channel_id];
670
671 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
672 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
673 else
674 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
675 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
676}
677
678/**
679 * vpif_get_default_field() - Get default field type based on interface
680 * @vpif_params - ptr to vpif params
681 */
682static inline enum v4l2_field vpif_get_default_field(
683 struct vpif_interface *iface)
684{
685 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
686 V4L2_FIELD_INTERLACED;
687}
688
689/**
690 * vpif_check_format() - check given pixel format for compatibility
691 * @ch - channel ptr
692 * @pixfmt - Given pixel format
693 * @update - update the values as per hardware requirement
694 *
695 * Check the application pixel format for S_FMT and update the input
696 * values as per hardware limits for TRY_FMT. The default pixel and
697 * field format is selected based on interface type.
698 */
699static int vpif_check_format(struct channel_obj *ch,
700 struct v4l2_pix_format *pixfmt,
701 int update)
702{
703 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
704 struct vpif_params *vpif_params = &ch->vpifparams;
705 enum v4l2_field field = pixfmt->field;
706 u32 sizeimage, hpitch, vpitch;
707 int ret = -EINVAL;
708
709 vpif_dbg(2, debug, "vpif_check_format\n");
710 /**
711 * first check for the pixel format. If if_type is Raw bayer,
712 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
713 * V4L2_PIX_FMT_YUV422P is supported
714 */
715 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
716 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
717 if (!update) {
718 vpif_dbg(2, debug, "invalid pix format\n");
719 goto exit;
720 }
721 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
722 }
723 } else {
724 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
725 if (!update) {
726 vpif_dbg(2, debug, "invalid pixel format\n");
727 goto exit;
728 }
729 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
730 }
731 }
732
733 if (!(VPIF_VALID_FIELD(field))) {
734 if (!update) {
735 vpif_dbg(2, debug, "invalid field format\n");
736 goto exit;
737 }
738 /**
739 * By default use FIELD_NONE for RAW Bayer capture
740 * and FIELD_INTERLACED for other interfaces
741 */
742 field = vpif_get_default_field(&vpif_params->iface);
743 } else if (field == V4L2_FIELD_ANY)
744 /* unsupported field. Use default */
745 field = vpif_get_default_field(&vpif_params->iface);
746
747 /* validate the hpitch */
748 hpitch = pixfmt->bytesperline;
749 if (hpitch < vpif_params->std_info.width) {
750 if (!update) {
751 vpif_dbg(2, debug, "invalid hpitch\n");
752 goto exit;
753 }
754 hpitch = vpif_params->std_info.width;
755 }
756
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300757 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300758
759 vpitch = sizeimage / (hpitch * 2);
760
761 /* validate the vpitch */
762 if (vpitch < vpif_params->std_info.height) {
763 if (!update) {
764 vpif_dbg(2, debug, "Invalid vpitch\n");
765 goto exit;
766 }
767 vpitch = vpif_params->std_info.height;
768 }
769
770 /* Check for 8 byte alignment */
771 if (!ALIGN(hpitch, 8)) {
772 if (!update) {
773 vpif_dbg(2, debug, "invalid pitch alignment\n");
774 goto exit;
775 }
776 /* adjust to next 8 byte boundary */
777 hpitch = (((hpitch + 7) / 8) * 8);
778 }
779 /* if update is set, modify the bytesperline and sizeimage */
780 if (update) {
781 pixfmt->bytesperline = hpitch;
782 pixfmt->sizeimage = hpitch * vpitch * 2;
783 }
784 /**
785 * Image width and height is always based on current standard width and
786 * height
787 */
788 pixfmt->width = common->fmt.fmt.pix.width;
789 pixfmt->height = common->fmt.fmt.pix.height;
790 return 0;
791exit:
792 return ret;
793}
794
795/**
796 * vpif_config_addr() - function to configure buffer address in vpif
797 * @ch - channel ptr
798 * @muxmode - channel mux mode
799 */
800static void vpif_config_addr(struct channel_obj *ch, int muxmode)
801{
802 struct common_obj *common;
803
804 vpif_dbg(2, debug, "vpif_config_addr\n");
805
806 common = &(ch->common[VPIF_VIDEO_INDEX]);
807
808 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
809 common->set_addr = ch1_set_videobuf_addr;
810 else if (2 == muxmode)
811 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
812 else
813 common->set_addr = ch0_set_videobuf_addr;
814}
815
816/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300817 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300818 * @filep: file pointer
819 * @vma: ptr to vm_area_struct
820 */
821static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
822{
823 /* Get the channel object and file handle object */
824 struct vpif_fh *fh = filep->private_data;
825 struct channel_obj *ch = fh->channel;
826 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300827 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300828
829 vpif_dbg(2, debug, "vpif_mmap\n");
830
Hans Verkuil72246792012-07-31 03:48:31 -0300831 if (mutex_lock_interruptible(&common->lock))
832 return -ERESTARTSYS;
833 ret = vb2_mmap(&common->buffer_queue, vma);
834 mutex_unlock(&common->lock);
835 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300836}
837
838/**
839 * vpif_poll: It is used for select/poll system call
840 * @filep: file pointer
841 * @wait: poll table to wait
842 */
843static unsigned int vpif_poll(struct file *filep, poll_table * wait)
844{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300845 struct vpif_fh *fh = filep->private_data;
846 struct channel_obj *channel = fh->channel;
847 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300848 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300849
850 vpif_dbg(2, debug, "vpif_poll\n");
851
Hans Verkuil72246792012-07-31 03:48:31 -0300852 if (common->started) {
853 mutex_lock(&common->lock);
854 res = vb2_poll(&common->buffer_queue, filep, wait);
855 mutex_unlock(&common->lock);
856 }
857 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300858}
859
860/**
861 * vpif_open : vpif open handler
862 * @filep: file ptr
863 *
864 * It creates object of file handle structure and stores it in private_data
865 * member of filepointer
866 */
867static int vpif_open(struct file *filep)
868{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300869 struct video_device *vdev = video_devdata(filep);
870 struct common_obj *common;
871 struct video_obj *vid_ch;
872 struct channel_obj *ch;
873 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300874
875 vpif_dbg(2, debug, "vpif_open\n");
876
877 ch = video_get_drvdata(vdev);
878
879 vid_ch = &ch->video;
880 common = &ch->common[VPIF_VIDEO_INDEX];
881
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300882 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300883 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300884 if (NULL == fh) {
885 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300886 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300887 }
888
Hans Verkuil72246792012-07-31 03:48:31 -0300889 if (mutex_lock_interruptible(&common->lock)) {
890 kfree(fh);
891 return -ERESTARTSYS;
892 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300893 /* store pointer to fh in private_data member of filep */
894 filep->private_data = fh;
895 fh->channel = ch;
896 fh->initialized = 0;
897 /* If decoder is not initialized. initialize it */
898 if (!ch->initialized) {
899 fh->initialized = 1;
900 ch->initialized = 1;
901 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
902 }
903 /* Increment channel usrs counter */
904 ch->usrs++;
905 /* Set io_allowed member to false */
906 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
907 /* Initialize priority of this instance to default priority */
908 fh->prio = V4L2_PRIORITY_UNSET;
909 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300910 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300911 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300912}
913
914/**
915 * vpif_release : function to clean up file close
916 * @filep: file pointer
917 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300918 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300919 * handle
920 */
921static int vpif_release(struct file *filep)
922{
923 struct vpif_fh *fh = filep->private_data;
924 struct channel_obj *ch = fh->channel;
925 struct common_obj *common;
926
927 vpif_dbg(2, debug, "vpif_release\n");
928
929 common = &ch->common[VPIF_VIDEO_INDEX];
930
Hans Verkuil72246792012-07-31 03:48:31 -0300931 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300932 /* if this instance is doing IO */
933 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
934 /* Reset io_usrs member of channel object */
935 common->io_usrs = 0;
936 /* Disable channel as per its device type and channel id */
937 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
938 enable_channel0(0);
939 channel0_intr_enable(0);
940 }
941 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
942 (2 == common->started)) {
943 enable_channel1(0);
944 channel1_intr_enable(0);
945 }
946 common->started = 0;
947 /* Free buffers allocated */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300948 vb2_queue_release(&common->buffer_queue);
949 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300950 }
951
952 /* Decrement channel usrs counter */
953 ch->usrs--;
954
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300955 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300956 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300957
958 if (fh->initialized)
959 ch->initialized = 0;
960
Hans Verkuil72246792012-07-31 03:48:31 -0300961 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300962 filep->private_data = NULL;
963 kfree(fh);
964 return 0;
965}
966
967/**
968 * vpif_reqbufs() - request buffer handler
969 * @file: file ptr
970 * @priv: file handle
971 * @reqbuf: request buffer structure ptr
972 */
973static int vpif_reqbufs(struct file *file, void *priv,
974 struct v4l2_requestbuffers *reqbuf)
975{
976 struct vpif_fh *fh = priv;
977 struct channel_obj *ch = fh->channel;
978 struct common_obj *common;
979 u8 index = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300980 struct vb2_queue *q;
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -0300981 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300982
983 vpif_dbg(2, debug, "vpif_reqbufs\n");
984
985 /**
986 * This file handle has not initialized the channel,
987 * It is not allowed to do settings
988 */
989 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
990 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
991 if (!fh->initialized) {
992 vpif_dbg(1, debug, "Channel Busy\n");
993 return -EBUSY;
994 }
995 }
996
Manjunath Hadli764af392012-04-13 04:49:34 -0300997 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300998 return -EINVAL;
999
1000 index = VPIF_VIDEO_INDEX;
1001
1002 common = &ch->common[index];
1003
Hans Verkuil46656af2011-01-04 06:51:35 -03001004 if (0 != common->io_usrs)
1005 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001006
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001007 /* Initialize videobuf2 queue as per the buffer type */
1008 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
Wei Yongjun94b76a82012-11-15 09:18:17 -03001009 if (IS_ERR(common->alloc_ctx)) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001010 vpif_err("Failed to get the context\n");
Wei Yongjun94b76a82012-11-15 09:18:17 -03001011 return PTR_ERR(common->alloc_ctx);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001012 }
1013 q = &common->buffer_queue;
1014 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1015 q->io_modes = VB2_MMAP | VB2_USERPTR;
1016 q->drv_priv = fh;
1017 q->ops = &video_qops;
1018 q->mem_ops = &vb2_dma_contig_memops;
1019 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
Sakari Ailusade48682014-02-25 19:12:19 -03001020 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Hans Verkuilb3379c62014-02-24 13:51:03 -03001021 q->min_buffers_needed = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001022
Lad, Prabhakarb4a711e72012-10-03 10:01:07 -03001023 ret = vb2_queue_init(q);
1024 if (ret) {
1025 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1026 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1027 return ret;
1028 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001029 /* Set io allowed member of file handle to TRUE */
1030 fh->io_allowed[index] = 1;
1031 /* Increment io usrs member of channel object to 1 */
1032 common->io_usrs = 1;
1033 /* Store type of memory requested in channel object */
1034 common->memory = reqbuf->memory;
1035 INIT_LIST_HEAD(&common->dma_queue);
1036
1037 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001038 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001039}
1040
1041/**
1042 * vpif_querybuf() - query buffer handler
1043 * @file: file ptr
1044 * @priv: file handle
1045 * @buf: v4l2 buffer structure ptr
1046 */
1047static int vpif_querybuf(struct file *file, void *priv,
1048 struct v4l2_buffer *buf)
1049{
1050 struct vpif_fh *fh = priv;
1051 struct channel_obj *ch = fh->channel;
1052 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1053
1054 vpif_dbg(2, debug, "vpif_querybuf\n");
1055
1056 if (common->fmt.type != buf->type)
1057 return -EINVAL;
1058
1059 if (common->memory != V4L2_MEMORY_MMAP) {
1060 vpif_dbg(1, debug, "Invalid memory\n");
1061 return -EINVAL;
1062 }
1063
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001064 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001065}
1066
1067/**
1068 * vpif_qbuf() - query buffer handler
1069 * @file: file ptr
1070 * @priv: file handle
1071 * @buf: v4l2 buffer structure ptr
1072 */
1073static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1074{
1075
1076 struct vpif_fh *fh = priv;
1077 struct channel_obj *ch = fh->channel;
1078 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1079 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001080
1081 vpif_dbg(2, debug, "vpif_qbuf\n");
1082
1083 if (common->fmt.type != tbuf.type) {
1084 vpif_err("invalid buffer type\n");
1085 return -EINVAL;
1086 }
1087
1088 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001089 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001090 return -EACCES;
1091 }
1092
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001093 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001094}
1095
1096/**
1097 * vpif_dqbuf() - query buffer handler
1098 * @file: file ptr
1099 * @priv: file handle
1100 * @buf: v4l2 buffer structure ptr
1101 */
1102static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1103{
1104 struct vpif_fh *fh = priv;
1105 struct channel_obj *ch = fh->channel;
1106 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1107
1108 vpif_dbg(2, debug, "vpif_dqbuf\n");
1109
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001110 return vb2_dqbuf(&common->buffer_queue, buf,
1111 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001112}
1113
1114/**
1115 * vpif_streamon() - streamon handler
1116 * @file: file ptr
1117 * @priv: file handle
1118 * @buftype: v4l2 buffer type
1119 */
1120static int vpif_streamon(struct file *file, void *priv,
1121 enum v4l2_buf_type buftype)
1122{
1123
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001124 struct vpif_fh *fh = priv;
1125 struct channel_obj *ch = fh->channel;
1126 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1127 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1128 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001129 int ret = 0;
1130
1131 vpif_dbg(2, debug, "vpif_streamon\n");
1132
1133 vpif = &ch->vpifparams;
1134
1135 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1136 vpif_dbg(1, debug, "buffer type not supported\n");
1137 return -EINVAL;
1138 }
1139
1140 /* If file handle is not allowed IO, return error */
1141 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1142 vpif_dbg(1, debug, "io not allowed\n");
1143 return -EACCES;
1144 }
1145
1146 /* If Streaming is already started, return error */
1147 if (common->started) {
1148 vpif_dbg(1, debug, "channel->started\n");
1149 return -EBUSY;
1150 }
1151
1152 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1153 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1154 vpif->std_info.ycmux_mode == 0) ||
1155 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1156 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1157 vpif_dbg(1, debug, "other channel is being used\n");
1158 return -EBUSY;
1159 }
1160
1161 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1162 if (ret)
1163 return ret;
1164
1165 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001166 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001167
Hans Verkuil178cce12012-09-20 09:06:30 -03001168 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001169 vpif_dbg(1, debug, "stream on failed in subdev\n");
1170 return ret;
1171 }
1172
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001173 /* Call vb2_streamon to start streaming in videobuf2 */
1174 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001175 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001176 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001177 return ret;
1178 }
1179
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001180 return ret;
1181}
1182
1183/**
1184 * vpif_streamoff() - streamoff handler
1185 * @file: file ptr
1186 * @priv: file handle
1187 * @buftype: v4l2 buffer type
1188 */
1189static int vpif_streamoff(struct file *file, void *priv,
1190 enum v4l2_buf_type buftype)
1191{
1192
1193 struct vpif_fh *fh = priv;
1194 struct channel_obj *ch = fh->channel;
1195 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1196 int ret;
1197
1198 vpif_dbg(2, debug, "vpif_streamoff\n");
1199
1200 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1201 vpif_dbg(1, debug, "buffer type not supported\n");
1202 return -EINVAL;
1203 }
1204
1205 /* If io is allowed for this file handle, return error */
1206 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1207 vpif_dbg(1, debug, "io not allowed\n");
1208 return -EACCES;
1209 }
1210
1211 /* If streaming is not started, return error */
1212 if (!common->started) {
1213 vpif_dbg(1, debug, "channel->started\n");
1214 return -EINVAL;
1215 }
1216
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001217 /* disable channel */
1218 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1219 enable_channel0(0);
1220 channel0_intr_enable(0);
1221 } else {
1222 enable_channel1(0);
1223 channel1_intr_enable(0);
1224 }
1225
1226 common->started = 0;
1227
Hans Verkuil178cce12012-09-20 09:06:30 -03001228 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001229
Hans Verkuil178cce12012-09-20 09:06:30 -03001230 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001231 vpif_dbg(1, debug, "stream off failed in subdev\n");
1232
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001233 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001234}
1235
1236/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001237 * vpif_input_to_subdev() - Maps input to sub device
1238 * @vpif_cfg - global config ptr
1239 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001240 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001241 *
1242 * lookup the sub device information for a given input index.
1243 * we report all the inputs to application. inputs table also
1244 * has sub device name for the each input
1245 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001246static int vpif_input_to_subdev(
1247 struct vpif_capture_config *vpif_cfg,
1248 struct vpif_capture_chan_config *chan_cfg,
1249 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001250{
Hans Verkuil178cce12012-09-20 09:06:30 -03001251 struct vpif_subdev_info *subdev_info;
1252 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001253 int i;
1254
Hans Verkuil178cce12012-09-20 09:06:30 -03001255 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001256
Hans Verkuil178cce12012-09-20 09:06:30 -03001257 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1258 if (subdev_name == NULL)
1259 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001260
1261 /* loop through the sub device list to get the sub device info */
1262 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1263 subdev_info = &vpif_cfg->subdev_info[i];
1264 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001265 return i;
1266 }
1267 return -1;
1268}
1269
1270/**
1271 * vpif_set_input() - Select an input
1272 * @vpif_cfg - global config ptr
1273 * @ch - channel
1274 * @_index - Given input index from application
1275 *
1276 * Select the given input.
1277 */
1278static int vpif_set_input(
1279 struct vpif_capture_config *vpif_cfg,
1280 struct channel_obj *ch,
1281 int index)
1282{
1283 struct vpif_capture_chan_config *chan_cfg =
1284 &vpif_cfg->chan_config[ch->channel_id];
1285 struct vpif_subdev_info *subdev_info = NULL;
1286 struct v4l2_subdev *sd = NULL;
1287 u32 input = 0, output = 0;
1288 int sd_index;
1289 int ret;
1290
1291 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1292 if (sd_index >= 0) {
1293 sd = vpif_obj.sd[sd_index];
1294 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001295 }
1296
Hans Verkuil178cce12012-09-20 09:06:30 -03001297 /* first setup input path from sub device to vpif */
1298 if (sd && vpif_cfg->setup_input_path) {
1299 ret = vpif_cfg->setup_input_path(ch->channel_id,
1300 subdev_info->name);
1301 if (ret < 0) {
1302 vpif_dbg(1, debug, "couldn't setup input path for the" \
1303 " sub device %s, for input index %d\n",
1304 subdev_info->name, index);
1305 return ret;
1306 }
1307 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001308
Hans Verkuil178cce12012-09-20 09:06:30 -03001309 if (sd) {
1310 input = chan_cfg->inputs[index].input_route;
1311 output = chan_cfg->inputs[index].output_route;
1312 ret = v4l2_subdev_call(sd, video, s_routing,
1313 input, output, 0);
1314 if (ret < 0 && ret != -ENOIOCTLCMD) {
1315 vpif_dbg(1, debug, "Failed to set input\n");
1316 return ret;
1317 }
1318 }
1319 ch->input_idx = index;
1320 ch->sd = sd;
1321 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001322 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001323
Hans Verkuil178cce12012-09-20 09:06:30 -03001324 /* update tvnorms from the sub device input info */
1325 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1326 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001327}
1328
1329/**
1330 * vpif_querystd() - querystd handler
1331 * @file: file ptr
1332 * @priv: file handle
1333 * @std_id: ptr to std id
1334 *
1335 * This function is called to detect standard at the selected input
1336 */
1337static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1338{
1339 struct vpif_fh *fh = priv;
1340 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001341 int ret = 0;
1342
1343 vpif_dbg(2, debug, "vpif_querystd\n");
1344
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001345 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001346 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001347
Hans Verkuil178cce12012-09-20 09:06:30 -03001348 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1349 return -ENODATA;
1350 if (ret) {
1351 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1352 return ret;
1353 }
1354
1355 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001356}
1357
1358/**
1359 * vpif_g_std() - get STD handler
1360 * @file: file ptr
1361 * @priv: file handle
1362 * @std_id: ptr to std id
1363 */
1364static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1365{
1366 struct vpif_fh *fh = priv;
1367 struct channel_obj *ch = fh->channel;
1368
1369 vpif_dbg(2, debug, "vpif_g_std\n");
1370
1371 *std = ch->video.stdid;
1372 return 0;
1373}
1374
1375/**
1376 * vpif_s_std() - set STD handler
1377 * @file: file ptr
1378 * @priv: file handle
1379 * @std_id: ptr to std id
1380 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001381static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001382{
1383 struct vpif_fh *fh = priv;
1384 struct channel_obj *ch = fh->channel;
1385 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1386 int ret = 0;
1387
1388 vpif_dbg(2, debug, "vpif_s_std\n");
1389
1390 if (common->started) {
1391 vpif_err("streaming in progress\n");
1392 return -EBUSY;
1393 }
1394
1395 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1396 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1397 if (!fh->initialized) {
1398 vpif_dbg(1, debug, "Channel Busy\n");
1399 return -EBUSY;
1400 }
1401 }
1402
Hans Verkuilffb48772010-05-01 08:03:24 -03001403 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001404 if (0 != ret)
1405 return ret;
1406
1407 fh->initialized = 1;
1408
1409 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001410 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001411 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001412
1413 /* Get the information about the standard */
1414 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001415 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001416 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001417 }
1418
1419 /* Configure the default format information */
1420 vpif_config_format(ch);
1421
1422 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001423 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001424 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001425 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001426 return ret;
1427 }
1428 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001429}
1430
1431/**
1432 * vpif_enum_input() - ENUMINPUT handler
1433 * @file: file ptr
1434 * @priv: file handle
1435 * @input: ptr to input structure
1436 */
1437static int vpif_enum_input(struct file *file, void *priv,
1438 struct v4l2_input *input)
1439{
1440
1441 struct vpif_capture_config *config = vpif_dev->platform_data;
1442 struct vpif_capture_chan_config *chan_cfg;
1443 struct vpif_fh *fh = priv;
1444 struct channel_obj *ch = fh->channel;
1445
1446 chan_cfg = &config->chan_config[ch->channel_id];
1447
1448 if (input->index >= chan_cfg->input_count) {
1449 vpif_dbg(1, debug, "Invalid input index\n");
1450 return -EINVAL;
1451 }
1452
1453 memcpy(input, &chan_cfg->inputs[input->index].input,
1454 sizeof(*input));
1455 return 0;
1456}
1457
1458/**
1459 * vpif_g_input() - Get INPUT handler
1460 * @file: file ptr
1461 * @priv: file handle
1462 * @index: ptr to input index
1463 */
1464static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1465{
1466 struct vpif_fh *fh = priv;
1467 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001468
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001469 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001470 return 0;
1471}
1472
1473/**
1474 * vpif_s_input() - Set INPUT handler
1475 * @file: file ptr
1476 * @priv: file handle
1477 * @index: input index
1478 */
1479static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1480{
1481 struct vpif_capture_config *config = vpif_dev->platform_data;
1482 struct vpif_capture_chan_config *chan_cfg;
1483 struct vpif_fh *fh = priv;
1484 struct channel_obj *ch = fh->channel;
1485 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001486 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001487
1488 chan_cfg = &config->chan_config[ch->channel_id];
1489
Hans Verkuil7aaad132012-09-20 09:06:25 -03001490 if (index >= chan_cfg->input_count)
1491 return -EINVAL;
1492
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001493 if (common->started) {
1494 vpif_err("Streaming in progress\n");
1495 return -EBUSY;
1496 }
1497
1498 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1499 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1500 if (!fh->initialized) {
1501 vpif_dbg(1, debug, "Channel Busy\n");
1502 return -EBUSY;
1503 }
1504 }
1505
Hans Verkuilffb48772010-05-01 08:03:24 -03001506 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001507 if (0 != ret)
1508 return ret;
1509
1510 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001511 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001512}
1513
1514/**
1515 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1516 * @file: file ptr
1517 * @priv: file handle
1518 * @index: input index
1519 */
1520static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1521 struct v4l2_fmtdesc *fmt)
1522{
1523 struct vpif_fh *fh = priv;
1524 struct channel_obj *ch = fh->channel;
1525
1526 if (fmt->index != 0) {
1527 vpif_dbg(1, debug, "Invalid format index\n");
1528 return -EINVAL;
1529 }
1530
1531 /* Fill in the information about format */
1532 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1533 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1534 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1535 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1536 } else {
1537 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1538 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1539 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1540 }
1541 return 0;
1542}
1543
1544/**
1545 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1546 * @file: file ptr
1547 * @priv: file handle
1548 * @fmt: ptr to v4l2 format structure
1549 */
1550static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1551 struct v4l2_format *fmt)
1552{
1553 struct vpif_fh *fh = priv;
1554 struct channel_obj *ch = fh->channel;
1555 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1556
1557 return vpif_check_format(ch, pixfmt, 1);
1558}
1559
1560
1561/**
1562 * vpif_g_fmt_vid_cap() - Set INPUT handler
1563 * @file: file ptr
1564 * @priv: file handle
1565 * @fmt: ptr to v4l2 format structure
1566 */
1567static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1568 struct v4l2_format *fmt)
1569{
1570 struct vpif_fh *fh = priv;
1571 struct channel_obj *ch = fh->channel;
1572 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1573
1574 /* Check the validity of the buffer type */
1575 if (common->fmt.type != fmt->type)
1576 return -EINVAL;
1577
1578 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001579 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001580 return 0;
1581}
1582
1583/**
1584 * vpif_s_fmt_vid_cap() - Set FMT handler
1585 * @file: file ptr
1586 * @priv: file handle
1587 * @fmt: ptr to v4l2 format structure
1588 */
1589static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1590 struct v4l2_format *fmt)
1591{
1592 struct vpif_fh *fh = priv;
1593 struct channel_obj *ch = fh->channel;
1594 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1595 struct v4l2_pix_format *pixfmt;
1596 int ret = 0;
1597
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001598 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001599
1600 /* If streaming is started, return error */
1601 if (common->started) {
1602 vpif_dbg(1, debug, "Streaming is started\n");
1603 return -EBUSY;
1604 }
1605
1606 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1607 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1608 if (!fh->initialized) {
1609 vpif_dbg(1, debug, "Channel Busy\n");
1610 return -EBUSY;
1611 }
1612 }
1613
Hans Verkuilffb48772010-05-01 08:03:24 -03001614 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001615 if (0 != ret)
1616 return ret;
1617
1618 fh->initialized = 1;
1619
1620 pixfmt = &fmt->fmt.pix;
1621 /* Check for valid field format */
1622 ret = vpif_check_format(ch, pixfmt, 0);
1623
1624 if (ret)
1625 return ret;
1626 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001627 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001628 return 0;
1629}
1630
1631/**
1632 * vpif_querycap() - QUERYCAP handler
1633 * @file: file ptr
1634 * @priv: file handle
1635 * @cap: ptr to v4l2_capability structure
1636 */
1637static int vpif_querycap(struct file *file, void *priv,
1638 struct v4l2_capability *cap)
1639{
1640 struct vpif_capture_config *config = vpif_dev->platform_data;
1641
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001642 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1643 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1644 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1645 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1646 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001647 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1648
1649 return 0;
1650}
1651
1652/**
1653 * vpif_g_priority() - get priority handler
1654 * @file: file ptr
1655 * @priv: file handle
1656 * @prio: ptr to v4l2_priority structure
1657 */
1658static int vpif_g_priority(struct file *file, void *priv,
1659 enum v4l2_priority *prio)
1660{
1661 struct vpif_fh *fh = priv;
1662 struct channel_obj *ch = fh->channel;
1663
1664 *prio = v4l2_prio_max(&ch->prio);
1665
1666 return 0;
1667}
1668
1669/**
1670 * vpif_s_priority() - set priority handler
1671 * @file: file ptr
1672 * @priv: file handle
1673 * @prio: ptr to v4l2_priority structure
1674 */
1675static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1676{
1677 struct vpif_fh *fh = priv;
1678 struct channel_obj *ch = fh->channel;
1679
1680 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1681}
1682
1683/**
1684 * vpif_cropcap() - cropcap handler
1685 * @file: file ptr
1686 * @priv: file handle
1687 * @crop: ptr to v4l2_cropcap structure
1688 */
1689static int vpif_cropcap(struct file *file, void *priv,
1690 struct v4l2_cropcap *crop)
1691{
1692 struct vpif_fh *fh = priv;
1693 struct channel_obj *ch = fh->channel;
1694 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1695
1696 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1697 return -EINVAL;
1698
1699 crop->bounds.left = 0;
1700 crop->bounds.top = 0;
1701 crop->bounds.height = common->height;
1702 crop->bounds.width = common->width;
1703 crop->defrect = crop->bounds;
1704 return 0;
1705}
1706
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001707/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001708 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001709 * @file: file ptr
1710 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001711 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001712 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001713static int
1714vpif_enum_dv_timings(struct file *file, void *priv,
1715 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001716{
1717 struct vpif_fh *fh = priv;
1718 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001719 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001720
Hans Verkuil178cce12012-09-20 09:06:30 -03001721 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001722 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001723 return -EINVAL;
1724 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001725}
1726
1727/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001728 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001729 * @file: file ptr
1730 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001731 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001732 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001733static int
1734vpif_query_dv_timings(struct file *file, void *priv,
1735 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001736{
1737 struct vpif_fh *fh = priv;
1738 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001739 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001740
Hans Verkuil178cce12012-09-20 09:06:30 -03001741 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001742 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001743 return -ENODATA;
1744 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001745}
1746
Mats Randgaardc027e162010-12-16 12:17:44 -03001747/**
1748 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1749 * @file: file ptr
1750 * @priv: file handle
1751 * @timings: digital video timings
1752 */
1753static int vpif_s_dv_timings(struct file *file, void *priv,
1754 struct v4l2_dv_timings *timings)
1755{
1756 struct vpif_fh *fh = priv;
1757 struct channel_obj *ch = fh->channel;
1758 struct vpif_params *vpifparams = &ch->vpifparams;
1759 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1760 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001761 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001762 int ret;
1763
1764 if (timings->type != V4L2_DV_BT_656_1120) {
1765 vpif_dbg(2, debug, "Timing type not defined\n");
1766 return -EINVAL;
1767 }
1768
1769 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001770 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1771 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1772 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001773 if (ret < 0) {
1774 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1775 return ret;
1776 }
1777
1778 if (!(timings->bt.width && timings->bt.height &&
1779 (timings->bt.hbackporch ||
1780 timings->bt.hfrontporch ||
1781 timings->bt.hsync) &&
1782 timings->bt.vfrontporch &&
1783 (timings->bt.vbackporch ||
1784 timings->bt.vsync))) {
1785 vpif_dbg(2, debug, "Timings for width, height, "
1786 "horizontal back porch, horizontal sync, "
1787 "horizontal front porch, vertical back porch, "
1788 "vertical sync and vertical back porch "
1789 "must be defined\n");
1790 return -EINVAL;
1791 }
1792
Hans Verkuil0598c172012-09-18 07:18:47 -03001793 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001794
1795 /* Configure video port timings */
1796
Hans Verkuile3655262013-07-29 08:41:00 -03001797 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001798 std_info->sav2eav = bt->width;
1799
1800 std_info->l1 = 1;
1801 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1802
Hans Verkuile3655262013-07-29 08:41:00 -03001803 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001804 if (bt->interlaced) {
1805 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001806 std_info->l5 = std_info->vsize/2 -
1807 (bt->vfrontporch - 1);
1808 std_info->l7 = std_info->vsize/2 + 1;
1809 std_info->l9 = std_info->l7 + bt->il_vsync +
1810 bt->il_vbackporch + 1;
1811 std_info->l11 = std_info->vsize -
1812 (bt->il_vfrontporch - 1);
1813 } else {
1814 vpif_dbg(2, debug, "Required timing values for "
1815 "interlaced BT format missing\n");
1816 return -EINVAL;
1817 }
1818 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001819 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1820 }
1821 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1822 std_info->width = bt->width;
1823 std_info->height = bt->height;
1824 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1825 std_info->ycmux_mode = 0;
1826 std_info->capture_format = 0;
1827 std_info->vbi_supported = 0;
1828 std_info->hd_sd = 1;
1829 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001830
1831 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001832 return 0;
1833}
1834
1835/**
1836 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1837 * @file: file ptr
1838 * @priv: file handle
1839 * @timings: digital video timings
1840 */
1841static int vpif_g_dv_timings(struct file *file, void *priv,
1842 struct v4l2_dv_timings *timings)
1843{
1844 struct vpif_fh *fh = priv;
1845 struct channel_obj *ch = fh->channel;
1846 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001847
Hans Verkuil0598c172012-09-18 07:18:47 -03001848 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001849
1850 return 0;
1851}
1852
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001853/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001854 * vpif_log_status() - Status information
1855 * @file: file ptr
1856 * @priv: file handle
1857 *
1858 * Returns zero.
1859 */
1860static int vpif_log_status(struct file *filep, void *priv)
1861{
1862 /* status for sub devices */
1863 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1864
1865 return 0;
1866}
1867
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001868/* vpif capture ioctl operations */
1869static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1870 .vidioc_querycap = vpif_querycap,
1871 .vidioc_g_priority = vpif_g_priority,
1872 .vidioc_s_priority = vpif_s_priority,
1873 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1874 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1875 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1876 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1877 .vidioc_enum_input = vpif_enum_input,
1878 .vidioc_s_input = vpif_s_input,
1879 .vidioc_g_input = vpif_g_input,
1880 .vidioc_reqbufs = vpif_reqbufs,
1881 .vidioc_querybuf = vpif_querybuf,
1882 .vidioc_querystd = vpif_querystd,
1883 .vidioc_s_std = vpif_s_std,
1884 .vidioc_g_std = vpif_g_std,
1885 .vidioc_qbuf = vpif_qbuf,
1886 .vidioc_dqbuf = vpif_dqbuf,
1887 .vidioc_streamon = vpif_streamon,
1888 .vidioc_streamoff = vpif_streamoff,
1889 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001890 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1891 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001892 .vidioc_s_dv_timings = vpif_s_dv_timings,
1893 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001894 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001895};
1896
1897/* vpif file operations */
1898static struct v4l2_file_operations vpif_fops = {
1899 .owner = THIS_MODULE,
1900 .open = vpif_open,
1901 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001902 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001903 .mmap = vpif_mmap,
1904 .poll = vpif_poll
1905};
1906
1907/* vpif video template */
1908static struct video_device vpif_video_template = {
1909 .name = "vpif",
1910 .fops = &vpif_fops,
1911 .minor = -1,
1912 .ioctl_ops = &vpif_ioctl_ops,
1913};
1914
1915/**
1916 * initialize_vpif() - Initialize vpif data structures
1917 *
1918 * Allocate memory for data structures and initialize them
1919 */
1920static int initialize_vpif(void)
1921{
1922 int err = 0, i, j;
1923 int free_channel_objects_index;
1924
1925 /* Default number of buffers should be 3 */
1926 if ((ch0_numbuffers > 0) &&
1927 (ch0_numbuffers < config_params.min_numbuffers))
1928 ch0_numbuffers = config_params.min_numbuffers;
1929 if ((ch1_numbuffers > 0) &&
1930 (ch1_numbuffers < config_params.min_numbuffers))
1931 ch1_numbuffers = config_params.min_numbuffers;
1932
1933 /* Set buffer size to min buffers size if it is invalid */
1934 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1935 ch0_bufsize =
1936 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1937 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1938 ch1_bufsize =
1939 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1940
1941 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1942 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1943 if (ch0_numbuffers) {
1944 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1945 = ch0_bufsize;
1946 }
1947 if (ch1_numbuffers) {
1948 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1949 = ch1_bufsize;
1950 }
1951
1952 /* Allocate memory for six channel objects */
1953 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1954 vpif_obj.dev[i] =
1955 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1956 /* If memory allocation fails, return error */
1957 if (!vpif_obj.dev[i]) {
1958 free_channel_objects_index = i;
1959 err = -ENOMEM;
1960 goto vpif_init_free_channel_objects;
1961 }
1962 }
1963 return 0;
1964
1965vpif_init_free_channel_objects:
1966 for (j = 0; j < free_channel_objects_index; j++)
1967 kfree(vpif_obj.dev[j]);
1968 return err;
1969}
1970
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001971static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1972 struct v4l2_subdev *subdev,
1973 struct v4l2_async_subdev *asd)
1974{
1975 int i;
1976
1977 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1978 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1979 subdev->name)) {
1980 vpif_obj.sd[i] = subdev;
1981 return 0;
1982 }
1983
1984 return -EINVAL;
1985}
1986
1987static int vpif_probe_complete(void)
1988{
1989 struct common_obj *common;
1990 struct channel_obj *ch;
1991 int i, j, err, k;
1992
1993 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1994 ch = vpif_obj.dev[j];
1995 ch->channel_id = j;
1996 common = &(ch->common[VPIF_VIDEO_INDEX]);
1997 spin_lock_init(&common->irqlock);
1998 mutex_init(&common->lock);
1999 ch->video_dev->lock = &common->lock;
2000 /* Initialize prio member of channel object */
2001 v4l2_prio_init(&ch->prio);
2002 video_set_drvdata(ch->video_dev, ch);
2003
2004 /* select input 0 */
2005 err = vpif_set_input(vpif_obj.config, ch, 0);
2006 if (err)
2007 goto probe_out;
2008
2009 err = video_register_device(ch->video_dev,
2010 VFL_TYPE_GRABBER, (j ? 1 : 0));
2011 if (err)
2012 goto probe_out;
2013 }
2014
2015 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2016 return 0;
2017
2018probe_out:
2019 for (k = 0; k < j; k++) {
2020 /* Get the pointer to the channel object */
2021 ch = vpif_obj.dev[k];
2022 /* Unregister video device */
2023 video_unregister_device(ch->video_dev);
2024 }
2025 kfree(vpif_obj.sd);
2026 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2027 ch = vpif_obj.dev[i];
2028 /* Note: does nothing if ch->video_dev == NULL */
2029 video_device_release(ch->video_dev);
2030 }
2031 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2032
2033 return err;
2034}
2035
2036static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2037{
2038 return vpif_probe_complete();
2039}
2040
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002041/**
2042 * vpif_probe : This function probes the vpif capture driver
2043 * @pdev: platform device pointer
2044 *
2045 * This creates device entries by register itself to the V4L2 driver and
2046 * initializes fields of each channel objects
2047 */
2048static __init int vpif_probe(struct platform_device *pdev)
2049{
2050 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002051 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002052 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002053 struct i2c_adapter *i2c_adap;
2054 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002055 struct video_device *vfd;
2056 struct resource *res;
2057 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03002058 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002059
2060 vpif_dev = &pdev->dev;
2061
2062 err = initialize_vpif();
2063 if (err) {
2064 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2065 return err;
2066 }
2067
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002068 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2069 if (err) {
2070 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2071 return err;
2072 }
2073
Hans Verkuil5be452c2012-09-20 09:06:29 -03002074 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03002075 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2076 IRQF_SHARED, "VPIF_Capture",
2077 (void *)(&vpif_obj.dev[res_idx]->
2078 channel_id));
2079 if (err) {
2080 err = -EINVAL;
2081 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002082 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002083 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002084 }
2085
2086 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2087 /* Get the pointer to the channel object */
2088 ch = vpif_obj.dev[i];
2089 /* Allocate memory for video device */
2090 vfd = video_device_alloc();
2091 if (NULL == vfd) {
2092 for (j = 0; j < i; j++) {
2093 ch = vpif_obj.dev[j];
2094 video_device_release(ch->video_dev);
2095 }
2096 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002097 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002098 }
2099
2100 /* Initialize field of video device */
2101 *vfd = vpif_video_template;
2102 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2103 vfd->release = video_device_release;
2104 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002105 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002106 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002107 /* Set video_dev to the video device */
2108 ch->video_dev = vfd;
2109 }
2110
Manjunath Hadli764af392012-04-13 04:49:34 -03002111 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2112 if (res) {
2113 size = resource_size(res);
2114 /* The resources are divided into two equal memory and when we
2115 * have HD output we can add them together
2116 */
2117 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2118 ch = vpif_obj.dev[j];
2119 ch->channel_id = j;
2120 /* only enabled if second resource exists */
2121 config_params.video_limit[ch->channel_id] = 0;
2122 if (size)
2123 config_params.video_limit[ch->channel_id] =
2124 size/2;
2125 }
2126 }
2127
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002128 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002129
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002130 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002131 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002132 GFP_KERNEL);
2133 if (vpif_obj.sd == NULL) {
2134 vpif_err("unable to allocate memory for subdevice pointers\n");
2135 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002136 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002137 }
2138
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002139 if (!vpif_obj.config->asd_sizes) {
2140 i2c_adap = i2c_get_adapter(1);
2141 for (i = 0; i < subdev_count; i++) {
2142 subdevdata = &vpif_obj.config->subdev_info[i];
2143 vpif_obj.sd[i] =
2144 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2145 i2c_adap,
2146 &subdevdata->
2147 board_info,
2148 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002149
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002150 if (!vpif_obj.sd[i]) {
2151 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03002152 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002153 goto probe_subdev_out;
2154 }
2155 v4l2_info(&vpif_obj.v4l2_dev,
2156 "registered sub device %s\n",
2157 subdevdata->name);
2158 }
2159 vpif_probe_complete();
2160 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03002161 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002162 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2163 vpif_obj.notifier.bound = vpif_async_bound;
2164 vpif_obj.notifier.complete = vpif_async_complete;
2165 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2166 &vpif_obj.notifier);
2167 if (err) {
2168 vpif_err("Error registering async notifier\n");
2169 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002170 goto probe_subdev_out;
2171 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002172 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002173
2174 return 0;
2175
Hans Verkuilb65814e2012-09-20 09:06:26 -03002176probe_subdev_out:
2177 /* free sub devices memory */
2178 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002179
Hans Verkuil5be452c2012-09-20 09:06:29 -03002180vpif_sd_error:
2181 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2182 ch = vpif_obj.dev[i];
2183 /* Note: does nothing if ch->video_dev == NULL */
2184 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002185 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002186vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002187 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002188
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002189 return err;
2190}
2191
2192/**
2193 * vpif_remove() - driver remove handler
2194 * @device: ptr to platform device structure
2195 *
2196 * The vidoe device is unregistered
2197 */
2198static int vpif_remove(struct platform_device *device)
2199{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002200 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002201 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002202
2203 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2204
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002205 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002206 /* un-register device */
2207 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2208 /* Get the pointer to the channel object */
2209 ch = vpif_obj.dev[i];
2210 /* Unregister video device */
2211 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002212 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002213 }
2214 return 0;
2215}
2216
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002217#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002218/**
2219 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002220 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002221static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002222{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002223
2224 struct common_obj *common;
2225 struct channel_obj *ch;
2226 int i;
2227
2228 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2229 /* Get the pointer to the channel object */
2230 ch = vpif_obj.dev[i];
2231 common = &ch->common[VPIF_VIDEO_INDEX];
2232 mutex_lock(&common->lock);
2233 if (ch->usrs && common->io_usrs) {
2234 /* Disable channel */
2235 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2236 enable_channel0(0);
2237 channel0_intr_enable(0);
2238 }
2239 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2240 common->started == 2) {
2241 enable_channel1(0);
2242 channel1_intr_enable(0);
2243 }
2244 }
2245 mutex_unlock(&common->lock);
2246 }
2247
2248 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002249}
2250
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002251/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002252 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002253 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002254static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002255{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002256 struct common_obj *common;
2257 struct channel_obj *ch;
2258 int i;
2259
2260 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2261 /* Get the pointer to the channel object */
2262 ch = vpif_obj.dev[i];
2263 common = &ch->common[VPIF_VIDEO_INDEX];
2264 mutex_lock(&common->lock);
2265 if (ch->usrs && common->io_usrs) {
2266 /* Disable channel */
2267 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2268 enable_channel0(1);
2269 channel0_intr_enable(1);
2270 }
2271 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2272 common->started == 2) {
2273 enable_channel1(1);
2274 channel1_intr_enable(1);
2275 }
2276 }
2277 mutex_unlock(&common->lock);
2278 }
2279
2280 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002281}
2282
Alexey Dobriyan47145212009-12-14 18:00:08 -08002283static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002284 .suspend = vpif_suspend,
2285 .resume = vpif_resume,
2286};
2287
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002288#define vpif_pm_ops (&vpif_dev_pm_ops)
2289#else
2290#define vpif_pm_ops NULL
2291#endif
2292
Mats Randgaardffa1b392010-08-30 10:30:36 -03002293static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002294 .driver = {
2295 .name = "vpif_capture",
2296 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002297 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002298 },
2299 .probe = vpif_probe,
2300 .remove = vpif_remove,
2301};
2302
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03002303module_platform_driver(vpif_driver);