blob: 8dbbd4bffb84b4908090bbf4e36b8f1be66b5a8f [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
20 */
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/mm.h>
27#include <linux/interrupt.h>
28#include <linux/workqueue.h>
29#include <linux/string.h>
30#include <linux/videodev2.h>
31#include <linux/wait.h>
32#include <linux/time.h>
33#include <linux/i2c.h>
34#include <linux/platform_device.h>
35#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030037#include <media/v4l2-device.h>
38#include <media/v4l2-ioctl.h>
Mats Randgaard7036d6a2010-12-16 12:17:41 -030039#include <media/v4l2-chip-ident.h>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030040
41#include "vpif_capture.h"
42#include "vpif.h"
43
44MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030046MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030047
48#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
49#define vpif_dbg(level, debug, fmt, arg...) \
50 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
51
52static int debug = 1;
53static u32 ch0_numbuffers = 3;
54static u32 ch1_numbuffers = 3;
55static u32 ch0_bufsize = 1920 * 1080 * 2;
56static u32 ch1_bufsize = 720 * 576 * 2;
57
58module_param(debug, int, 0644);
59module_param(ch0_numbuffers, uint, S_IRUGO);
60module_param(ch1_numbuffers, uint, S_IRUGO);
61module_param(ch0_bufsize, uint, S_IRUGO);
62module_param(ch1_bufsize, uint, S_IRUGO);
63
64MODULE_PARM_DESC(debug, "Debug level 0-1");
65MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
66MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
67MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
68MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
69
70static struct vpif_config_params config_params = {
71 .min_numbuffers = 3,
72 .numbuffers[0] = 3,
73 .numbuffers[1] = 3,
74 .min_bufsize[0] = 720 * 480 * 2,
75 .min_bufsize[1] = 720 * 480 * 2,
76 .channel_bufsize[0] = 1920 * 1080 * 2,
77 .channel_bufsize[1] = 720 * 576 * 2,
78};
79
80/* global variables */
81static struct vpif_device vpif_obj = { {NULL} };
82static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030083static void vpif_calculate_offsets(struct channel_obj *ch);
84static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030085
86/**
87 * buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030088 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030089 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030090 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030091 * function is called. The buffer is prepared and user space virtual address
92 * or user address is converted into physical address
93 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030094static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030095{
96 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030097 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
98 struct vb2_queue *q = vb->vb2_queue;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030099 struct channel_obj *ch = fh->channel;
100 struct common_obj *common;
101 unsigned long addr;
102
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300103 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
104
105 common = &ch->common[VPIF_VIDEO_INDEX];
106
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300107 if (vb->state != VB2_BUF_STATE_ACTIVE &&
108 vb->state != VB2_BUF_STATE_PREPARED) {
109 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
110 if (vb2_plane_vaddr(vb, 0) &&
111 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
112 goto exit;
113 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300114
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300115 if (q->streaming) {
116 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
117 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
118 !IS_ALIGNED((addr + common->ctop_off), 8) ||
119 !IS_ALIGNED((addr + common->cbtm_off), 8))
120 goto exit;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300121 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300122 }
123 return 0;
124exit:
125 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
126 return -EINVAL;
127}
128
129/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300130 * vpif_buffer_queue_setup : Callback function for buffer setup.
131 * @vq: vb2_queue ptr
132 * @fmt: v4l2 format
133 * @nbuffers: ptr to number of buffers requested by application
134 * @nplanes:: contains number of distinct video planes needed to hold a frame
135 * @sizes[]: contains the size (in bytes) of each plane.
136 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300137 *
138 * This callback function is called when reqbuf() is called to adjust
139 * the buffer count and buffer size
140 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300141static int vpif_buffer_queue_setup(struct vb2_queue *vq,
142 const struct v4l2_format *fmt,
143 unsigned int *nbuffers, unsigned int *nplanes,
144 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300145{
146 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300147 struct vpif_fh *fh = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300148 struct channel_obj *ch = fh->channel;
149 struct common_obj *common;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300150 unsigned long size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300151
152 common = &ch->common[VPIF_VIDEO_INDEX];
153
154 vpif_dbg(2, debug, "vpif_buffer_setup\n");
155
156 /* If memory type is not mmap, return */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300157 if (V4L2_MEMORY_MMAP == common->memory) {
158 /* Calculate the size of the buffer */
159 size = config_params.channel_bufsize[ch->channel_id];
160 /*
161 * Checking if the buffer size exceeds the available buffer
162 * ycmux_mode = 0 means 1 channel mode HD and
163 * ycmux_mode = 1 means 2 channels mode SD
164 */
165 if (ch->vpifparams.std_info.ycmux_mode == 0) {
166 if (config_params.video_limit[ch->channel_id])
167 while (size * *nbuffers >
168 (config_params.video_limit[0]
Manjunath Hadli764af392012-04-13 04:49:34 -0300169 + config_params.video_limit[1]))
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300170 (*nbuffers)--;
171 } else {
172 if (config_params.video_limit[ch->channel_id])
173 while (size * *nbuffers >
Manjunath Hadli764af392012-04-13 04:49:34 -0300174 config_params.video_limit[ch->channel_id])
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300175 (*nbuffers)--;
176 }
177
178 } else {
179 size = common->fmt.fmt.pix.sizeimage;
Manjunath Hadli764af392012-04-13 04:49:34 -0300180 }
181
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300182 if (*nbuffers < config_params.min_numbuffers)
183 *nbuffers = config_params.min_numbuffers;
184
185 *nplanes = 1;
186 sizes[0] = size;
187 alloc_ctxs[0] = common->alloc_ctx;
188
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300189 return 0;
190}
191
192/**
193 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300194 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300195 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300196static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300197{
198 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300199 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300200 struct channel_obj *ch = fh->channel;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300201 struct vpif_cap_buffer *buf = container_of(vb,
202 struct vpif_cap_buffer, vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300203 struct common_obj *common;
204
205 common = &ch->common[VPIF_VIDEO_INDEX];
206
207 vpif_dbg(2, debug, "vpif_buffer_queue\n");
208
209 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300210 list_add_tail(&buf->list, &common->dma_queue);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300211}
212
213/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300214 * vpif_buf_cleanup : Callback function to free buffer
215 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300216 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300217 * This function is called from the videobuf2 layer to free memory
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300218 * allocated to the buffers
219 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300220static void vpif_buf_cleanup(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300221{
222 /* Get the file handle object and channel object */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300223 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
224 struct vpif_cap_buffer *buf = container_of(vb,
225 struct vpif_cap_buffer, vb);
226 struct channel_obj *ch = fh->channel;
227 struct common_obj *common;
228 unsigned long flags;
229
230 common = &ch->common[VPIF_VIDEO_INDEX];
231
232 spin_lock_irqsave(&common->irqlock, flags);
233 if (vb->state == VB2_BUF_STATE_ACTIVE)
234 list_del_init(&buf->list);
235 spin_unlock_irqrestore(&common->irqlock, flags);
236
237}
238
239static void vpif_wait_prepare(struct vb2_queue *vq)
240{
241 struct vpif_fh *fh = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300242 struct channel_obj *ch = fh->channel;
243 struct common_obj *common;
244
245 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300246 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300247}
248
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300249static void vpif_wait_finish(struct vb2_queue *vq)
250{
251 struct vpif_fh *fh = vb2_get_drv_priv(vq);
252 struct channel_obj *ch = fh->channel;
253 struct common_obj *common;
254
255 common = &ch->common[VPIF_VIDEO_INDEX];
256 mutex_lock(&common->lock);
257}
258
259static int vpif_buffer_init(struct vb2_buffer *vb)
260{
261 struct vpif_cap_buffer *buf = container_of(vb,
262 struct vpif_cap_buffer, vb);
263
264 INIT_LIST_HEAD(&buf->list);
265
266 return 0;
267}
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300268
269static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
270 { {1, 1} };
271
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300272static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
273{
274 struct vpif_capture_config *vpif_config_data =
275 vpif_dev->platform_data;
276 struct vpif_fh *fh = vb2_get_drv_priv(vq);
277 struct channel_obj *ch = fh->channel;
278 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
279 struct vpif_params *vpif = &ch->vpifparams;
280 unsigned long addr = 0;
281 int ret;
282
283 /* If buffer queue is empty, return error */
284 if (list_empty(&common->dma_queue)) {
285 vpif_dbg(1, debug, "buffer queue is empty\n");
286 return -EIO;
287 }
288
289 /* Get the next frame from the buffer queue */
290 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
291 struct vpif_cap_buffer, list);
292 /* Remove buffer from the buffer queue */
293 list_del(&common->cur_frm->list);
294 /* Mark state of the current frame to active */
295 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
296 /* Initialize field_id and started member */
297 ch->field_id = 0;
298 common->started = 1;
299 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
300
301 /* Calculate the offset for Y and C data in the buffer */
302 vpif_calculate_offsets(ch);
303
304 if ((vpif->std_info.frm_fmt &&
305 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
306 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
307 (!vpif->std_info.frm_fmt &&
308 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
309 vpif_dbg(1, debug, "conflict in field format and std format\n");
310 return -EINVAL;
311 }
312
313 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300314 if (vpif_config_data->setup_input_channel_mode) {
315 ret = vpif_config_data->
316 setup_input_channel_mode(vpif->std_info.ycmux_mode);
317 if (ret < 0) {
318 vpif_dbg(1, debug, "can't set vpif channel mode\n");
319 return ret;
320 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300321 }
322
323 /* Call vpif_set_params function to set the parameters and addresses */
324 ret = vpif_set_video_params(vpif, ch->channel_id);
325
326 if (ret < 0) {
327 vpif_dbg(1, debug, "can't set video params\n");
328 return ret;
329 }
330
331 common->started = ret;
332 vpif_config_addr(ch, ret);
333
334 common->set_addr(addr + common->ytop_off,
335 addr + common->ybtm_off,
336 addr + common->ctop_off,
337 addr + common->cbtm_off);
338
339 /**
340 * Set interrupt for both the fields in VPIF Register enable channel in
341 * VPIF register
342 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300343 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300344 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
345 channel0_intr_assert();
346 channel0_intr_enable(1);
347 enable_channel0(1);
348 }
349 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
350 (common->started == 2)) {
351 channel1_intr_assert();
352 channel1_intr_enable(1);
353 enable_channel1(1);
354 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300355
356 return 0;
357}
358
359/* abort streaming and wait for last buffer */
360static int vpif_stop_streaming(struct vb2_queue *vq)
361{
362 struct vpif_fh *fh = vb2_get_drv_priv(vq);
363 struct channel_obj *ch = fh->channel;
364 struct common_obj *common;
365
366 if (!vb2_is_streaming(vq))
367 return 0;
368
369 common = &ch->common[VPIF_VIDEO_INDEX];
370
371 /* release all active buffers */
372 while (!list_empty(&common->dma_queue)) {
373 common->next_frm = list_entry(common->dma_queue.next,
374 struct vpif_cap_buffer, list);
375 list_del(&common->next_frm->list);
376 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
377 }
378
379 return 0;
380}
381
382static struct vb2_ops video_qops = {
383 .queue_setup = vpif_buffer_queue_setup,
384 .wait_prepare = vpif_wait_prepare,
385 .wait_finish = vpif_wait_finish,
386 .buf_init = vpif_buffer_init,
387 .buf_prepare = vpif_buffer_prepare,
388 .start_streaming = vpif_start_streaming,
389 .stop_streaming = vpif_stop_streaming,
390 .buf_cleanup = vpif_buf_cleanup,
391 .buf_queue = vpif_buffer_queue,
392};
393
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300394/**
395 * vpif_process_buffer_complete: process a completed buffer
396 * @common: ptr to common channel object
397 *
398 * This function time stamp the buffer and mark it as DONE. It also
399 * wake up any process waiting on the QUEUE and set the next buffer
400 * as current
401 */
402static void vpif_process_buffer_complete(struct common_obj *common)
403{
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300404 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
405 vb2_buffer_done(&common->cur_frm->vb,
406 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300407 /* Make curFrm pointing to nextFrm */
408 common->cur_frm = common->next_frm;
409}
410
411/**
412 * vpif_schedule_next_buffer: set next buffer address for capture
413 * @common : ptr to common channel object
414 *
415 * This function will get next buffer from the dma queue and
416 * set the buffer address in the vpif register for capture.
417 * the buffer is marked active
418 */
419static void vpif_schedule_next_buffer(struct common_obj *common)
420{
421 unsigned long addr = 0;
422
423 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300424 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300425 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300426 list_del(&common->next_frm->list);
427 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
428 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300429
430 /* Set top and bottom field addresses in VPIF registers */
431 common->set_addr(addr + common->ytop_off,
432 addr + common->ybtm_off,
433 addr + common->ctop_off,
434 addr + common->cbtm_off);
435}
436
437/**
438 * vpif_channel_isr : ISR handler for vpif capture
439 * @irq: irq number
440 * @dev_id: dev_id ptr
441 *
442 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300443 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300444 */
445static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
446{
447 struct vpif_device *dev = &vpif_obj;
448 struct common_obj *common;
449 struct channel_obj *ch;
450 enum v4l2_field field;
451 int channel_id = 0;
452 int fid = -1, i;
453
454 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300455 if (!vpif_intr_status(channel_id))
456 return IRQ_NONE;
457
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300458 ch = dev->dev[channel_id];
459
460 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
461
462 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
463 common = &ch->common[i];
464 /* skip If streaming is not started in this channel */
465 if (0 == common->started)
466 continue;
467
468 /* Check the field format */
469 if (1 == ch->vpifparams.std_info.frm_fmt) {
470 /* Progressive mode */
471 if (list_empty(&common->dma_queue))
472 continue;
473
474 if (!channel_first_int[i][channel_id])
475 vpif_process_buffer_complete(common);
476
477 channel_first_int[i][channel_id] = 0;
478
479 vpif_schedule_next_buffer(common);
480
481
482 channel_first_int[i][channel_id] = 0;
483 } else {
484 /**
485 * Interlaced mode. If it is first interrupt, ignore
486 * it
487 */
488 if (channel_first_int[i][channel_id]) {
489 channel_first_int[i][channel_id] = 0;
490 continue;
491 }
492 if (0 == i) {
493 ch->field_id ^= 1;
494 /* Get field id from VPIF registers */
495 fid = vpif_channel_getfid(ch->channel_id);
496 if (fid != ch->field_id) {
497 /**
498 * If field id does not match stored
499 * field id, make them in sync
500 */
501 if (0 == fid)
502 ch->field_id = fid;
503 return IRQ_HANDLED;
504 }
505 }
506 /* device field id and local field id are in sync */
507 if (0 == fid) {
508 /* this is even field */
509 if (common->cur_frm == common->next_frm)
510 continue;
511
512 /* mark the current buffer as done */
513 vpif_process_buffer_complete(common);
514 } else if (1 == fid) {
515 /* odd field */
516 if (list_empty(&common->dma_queue) ||
517 (common->cur_frm != common->next_frm))
518 continue;
519
520 vpif_schedule_next_buffer(common);
521 }
522 }
523 }
524 return IRQ_HANDLED;
525}
526
527/**
528 * vpif_update_std_info() - update standard related info
529 * @ch: ptr to channel object
530 *
531 * For a given standard selected by application, update values
532 * in the device data structures
533 */
534static int vpif_update_std_info(struct channel_obj *ch)
535{
536 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
537 struct vpif_params *vpifparams = &ch->vpifparams;
538 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300539 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300540 struct video_obj *vid_ch = &ch->video;
541 int index;
542
543 vpif_dbg(2, debug, "vpif_update_std_info\n");
544
Mats Randgaardaa444402010-12-16 12:17:42 -0300545 for (index = 0; index < vpif_ch_params_count; index++) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300546 config = &ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300547 if (config->hd_sd == 0) {
548 vpif_dbg(2, debug, "SD format\n");
549 if (config->stdid & vid_ch->stdid) {
550 memcpy(std_info, config, sizeof(*config));
551 break;
552 }
553 } else {
554 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300555 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
556 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300557 memcpy(std_info, config, sizeof(*config));
558 break;
559 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300560 }
561 }
562
563 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300564 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300565 return -EINVAL;
566
567 common->fmt.fmt.pix.width = std_info->width;
568 common->width = std_info->width;
569 common->fmt.fmt.pix.height = std_info->height;
570 common->height = std_info->height;
571 common->fmt.fmt.pix.bytesperline = std_info->width;
572 vpifparams->video_params.hpitch = std_info->width;
573 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300574
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300575 return 0;
576}
577
578/**
579 * vpif_calculate_offsets : This function calculates buffers offsets
580 * @ch : ptr to channel object
581 *
582 * This function calculates buffer offsets for Y and C in the top and
583 * bottom field
584 */
585static void vpif_calculate_offsets(struct channel_obj *ch)
586{
587 unsigned int hpitch, vpitch, sizeimage;
588 struct video_obj *vid_ch = &(ch->video);
589 struct vpif_params *vpifparams = &ch->vpifparams;
590 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
591 enum v4l2_field field = common->fmt.fmt.pix.field;
592
593 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
594
595 if (V4L2_FIELD_ANY == field) {
596 if (vpifparams->std_info.frm_fmt)
597 vid_ch->buf_field = V4L2_FIELD_NONE;
598 else
599 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
600 } else
601 vid_ch->buf_field = common->fmt.fmt.pix.field;
602
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300603 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300604
605 hpitch = common->fmt.fmt.pix.bytesperline;
606 vpitch = sizeimage / (hpitch * 2);
607
608 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
609 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
610 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
611 common->ytop_off = 0;
612 common->ybtm_off = hpitch;
613 common->ctop_off = sizeimage / 2;
614 common->cbtm_off = sizeimage / 2 + hpitch;
615 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
616 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
617 common->ytop_off = 0;
618 common->ybtm_off = sizeimage / 4;
619 common->ctop_off = sizeimage / 2;
620 common->cbtm_off = common->ctop_off + sizeimage / 4;
621 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
622 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
623 common->ybtm_off = 0;
624 common->ytop_off = sizeimage / 4;
625 common->cbtm_off = sizeimage / 2;
626 common->ctop_off = common->cbtm_off + sizeimage / 4;
627 }
628 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
629 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
630 vpifparams->video_params.storage_mode = 1;
631 else
632 vpifparams->video_params.storage_mode = 0;
633
634 if (1 == vpifparams->std_info.frm_fmt)
635 vpifparams->video_params.hpitch =
636 common->fmt.fmt.pix.bytesperline;
637 else {
638 if ((field == V4L2_FIELD_ANY)
639 || (field == V4L2_FIELD_INTERLACED))
640 vpifparams->video_params.hpitch =
641 common->fmt.fmt.pix.bytesperline * 2;
642 else
643 vpifparams->video_params.hpitch =
644 common->fmt.fmt.pix.bytesperline;
645 }
646
647 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
648}
649
650/**
651 * vpif_config_format: configure default frame format in the device
652 * ch : ptr to channel object
653 */
654static void vpif_config_format(struct channel_obj *ch)
655{
656 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
657
658 vpif_dbg(2, debug, "vpif_config_format\n");
659
660 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
661 if (config_params.numbuffers[ch->channel_id] == 0)
662 common->memory = V4L2_MEMORY_USERPTR;
663 else
664 common->memory = V4L2_MEMORY_MMAP;
665
666 common->fmt.fmt.pix.sizeimage
667 = config_params.channel_bufsize[ch->channel_id];
668
669 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
670 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
671 else
672 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
673 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
674}
675
676/**
677 * vpif_get_default_field() - Get default field type based on interface
678 * @vpif_params - ptr to vpif params
679 */
680static inline enum v4l2_field vpif_get_default_field(
681 struct vpif_interface *iface)
682{
683 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
684 V4L2_FIELD_INTERLACED;
685}
686
687/**
688 * vpif_check_format() - check given pixel format for compatibility
689 * @ch - channel ptr
690 * @pixfmt - Given pixel format
691 * @update - update the values as per hardware requirement
692 *
693 * Check the application pixel format for S_FMT and update the input
694 * values as per hardware limits for TRY_FMT. The default pixel and
695 * field format is selected based on interface type.
696 */
697static int vpif_check_format(struct channel_obj *ch,
698 struct v4l2_pix_format *pixfmt,
699 int update)
700{
701 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
702 struct vpif_params *vpif_params = &ch->vpifparams;
703 enum v4l2_field field = pixfmt->field;
704 u32 sizeimage, hpitch, vpitch;
705 int ret = -EINVAL;
706
707 vpif_dbg(2, debug, "vpif_check_format\n");
708 /**
709 * first check for the pixel format. If if_type is Raw bayer,
710 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
711 * V4L2_PIX_FMT_YUV422P is supported
712 */
713 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
714 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
715 if (!update) {
716 vpif_dbg(2, debug, "invalid pix format\n");
717 goto exit;
718 }
719 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
720 }
721 } else {
722 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
723 if (!update) {
724 vpif_dbg(2, debug, "invalid pixel format\n");
725 goto exit;
726 }
727 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
728 }
729 }
730
731 if (!(VPIF_VALID_FIELD(field))) {
732 if (!update) {
733 vpif_dbg(2, debug, "invalid field format\n");
734 goto exit;
735 }
736 /**
737 * By default use FIELD_NONE for RAW Bayer capture
738 * and FIELD_INTERLACED for other interfaces
739 */
740 field = vpif_get_default_field(&vpif_params->iface);
741 } else if (field == V4L2_FIELD_ANY)
742 /* unsupported field. Use default */
743 field = vpif_get_default_field(&vpif_params->iface);
744
745 /* validate the hpitch */
746 hpitch = pixfmt->bytesperline;
747 if (hpitch < vpif_params->std_info.width) {
748 if (!update) {
749 vpif_dbg(2, debug, "invalid hpitch\n");
750 goto exit;
751 }
752 hpitch = vpif_params->std_info.width;
753 }
754
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300755 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300756
757 vpitch = sizeimage / (hpitch * 2);
758
759 /* validate the vpitch */
760 if (vpitch < vpif_params->std_info.height) {
761 if (!update) {
762 vpif_dbg(2, debug, "Invalid vpitch\n");
763 goto exit;
764 }
765 vpitch = vpif_params->std_info.height;
766 }
767
768 /* Check for 8 byte alignment */
769 if (!ALIGN(hpitch, 8)) {
770 if (!update) {
771 vpif_dbg(2, debug, "invalid pitch alignment\n");
772 goto exit;
773 }
774 /* adjust to next 8 byte boundary */
775 hpitch = (((hpitch + 7) / 8) * 8);
776 }
777 /* if update is set, modify the bytesperline and sizeimage */
778 if (update) {
779 pixfmt->bytesperline = hpitch;
780 pixfmt->sizeimage = hpitch * vpitch * 2;
781 }
782 /**
783 * Image width and height is always based on current standard width and
784 * height
785 */
786 pixfmt->width = common->fmt.fmt.pix.width;
787 pixfmt->height = common->fmt.fmt.pix.height;
788 return 0;
789exit:
790 return ret;
791}
792
793/**
794 * vpif_config_addr() - function to configure buffer address in vpif
795 * @ch - channel ptr
796 * @muxmode - channel mux mode
797 */
798static void vpif_config_addr(struct channel_obj *ch, int muxmode)
799{
800 struct common_obj *common;
801
802 vpif_dbg(2, debug, "vpif_config_addr\n");
803
804 common = &(ch->common[VPIF_VIDEO_INDEX]);
805
806 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
807 common->set_addr = ch1_set_videobuf_addr;
808 else if (2 == muxmode)
809 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
810 else
811 common->set_addr = ch0_set_videobuf_addr;
812}
813
814/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300815 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300816 * @filep: file pointer
817 * @vma: ptr to vm_area_struct
818 */
819static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
820{
821 /* Get the channel object and file handle object */
822 struct vpif_fh *fh = filep->private_data;
823 struct channel_obj *ch = fh->channel;
824 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300825 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300826
827 vpif_dbg(2, debug, "vpif_mmap\n");
828
Hans Verkuil72246792012-07-31 03:48:31 -0300829 if (mutex_lock_interruptible(&common->lock))
830 return -ERESTARTSYS;
831 ret = vb2_mmap(&common->buffer_queue, vma);
832 mutex_unlock(&common->lock);
833 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300834}
835
836/**
837 * vpif_poll: It is used for select/poll system call
838 * @filep: file pointer
839 * @wait: poll table to wait
840 */
841static unsigned int vpif_poll(struct file *filep, poll_table * wait)
842{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300843 struct vpif_fh *fh = filep->private_data;
844 struct channel_obj *channel = fh->channel;
845 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300846 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300847
848 vpif_dbg(2, debug, "vpif_poll\n");
849
Hans Verkuil72246792012-07-31 03:48:31 -0300850 if (common->started) {
851 mutex_lock(&common->lock);
852 res = vb2_poll(&common->buffer_queue, filep, wait);
853 mutex_unlock(&common->lock);
854 }
855 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300856}
857
858/**
859 * vpif_open : vpif open handler
860 * @filep: file ptr
861 *
862 * It creates object of file handle structure and stores it in private_data
863 * member of filepointer
864 */
865static int vpif_open(struct file *filep)
866{
867 struct vpif_capture_config *config = vpif_dev->platform_data;
868 struct video_device *vdev = video_devdata(filep);
869 struct common_obj *common;
870 struct video_obj *vid_ch;
871 struct channel_obj *ch;
872 struct vpif_fh *fh;
Hans Verkuil46656af2011-01-04 06:51:35 -0300873 int i;
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 if (NULL == ch->curr_subdev_info) {
883 /**
884 * search through the sub device to see a registered
885 * sub device and make it as current sub device
886 */
887 for (i = 0; i < config->subdev_count; i++) {
888 if (vpif_obj.sd[i]) {
889 /* the sub device is registered */
890 ch->curr_subdev_info = &config->subdev_info[i];
891 /* make first input as the current input */
892 vid_ch->input_idx = 0;
893 break;
894 }
895 }
896 if (i == config->subdev_count) {
897 vpif_err("No sub device registered\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300898 return -ENOENT;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300899 }
900 }
901
902 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300903 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300904 if (NULL == fh) {
905 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300906 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300907 }
908
Hans Verkuil72246792012-07-31 03:48:31 -0300909 if (mutex_lock_interruptible(&common->lock)) {
910 kfree(fh);
911 return -ERESTARTSYS;
912 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300913 /* store pointer to fh in private_data member of filep */
914 filep->private_data = fh;
915 fh->channel = ch;
916 fh->initialized = 0;
917 /* If decoder is not initialized. initialize it */
918 if (!ch->initialized) {
919 fh->initialized = 1;
920 ch->initialized = 1;
921 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
922 }
923 /* Increment channel usrs counter */
924 ch->usrs++;
925 /* Set io_allowed member to false */
926 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
927 /* Initialize priority of this instance to default priority */
928 fh->prio = V4L2_PRIORITY_UNSET;
929 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300930 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300931 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300932}
933
934/**
935 * vpif_release : function to clean up file close
936 * @filep: file pointer
937 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300938 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300939 * handle
940 */
941static int vpif_release(struct file *filep)
942{
943 struct vpif_fh *fh = filep->private_data;
944 struct channel_obj *ch = fh->channel;
945 struct common_obj *common;
946
947 vpif_dbg(2, debug, "vpif_release\n");
948
949 common = &ch->common[VPIF_VIDEO_INDEX];
950
Hans Verkuil72246792012-07-31 03:48:31 -0300951 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300952 /* if this instance is doing IO */
953 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
954 /* Reset io_usrs member of channel object */
955 common->io_usrs = 0;
956 /* Disable channel as per its device type and channel id */
957 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
958 enable_channel0(0);
959 channel0_intr_enable(0);
960 }
961 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
962 (2 == common->started)) {
963 enable_channel1(0);
964 channel1_intr_enable(0);
965 }
966 common->started = 0;
967 /* Free buffers allocated */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300968 vb2_queue_release(&common->buffer_queue);
969 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300970 }
971
972 /* Decrement channel usrs counter */
973 ch->usrs--;
974
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300975 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300976 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300977
978 if (fh->initialized)
979 ch->initialized = 0;
980
Hans Verkuil72246792012-07-31 03:48:31 -0300981 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300982 filep->private_data = NULL;
983 kfree(fh);
984 return 0;
985}
986
987/**
988 * vpif_reqbufs() - request buffer handler
989 * @file: file ptr
990 * @priv: file handle
991 * @reqbuf: request buffer structure ptr
992 */
993static int vpif_reqbufs(struct file *file, void *priv,
994 struct v4l2_requestbuffers *reqbuf)
995{
996 struct vpif_fh *fh = priv;
997 struct channel_obj *ch = fh->channel;
998 struct common_obj *common;
999 u8 index = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001000 struct vb2_queue *q;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001001
1002 vpif_dbg(2, debug, "vpif_reqbufs\n");
1003
1004 /**
1005 * This file handle has not initialized the channel,
1006 * It is not allowed to do settings
1007 */
1008 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
1009 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1010 if (!fh->initialized) {
1011 vpif_dbg(1, debug, "Channel Busy\n");
1012 return -EBUSY;
1013 }
1014 }
1015
Manjunath Hadli764af392012-04-13 04:49:34 -03001016 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001017 return -EINVAL;
1018
1019 index = VPIF_VIDEO_INDEX;
1020
1021 common = &ch->common[index];
1022
Hans Verkuil46656af2011-01-04 06:51:35 -03001023 if (0 != common->io_usrs)
1024 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001025
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001026 /* Initialize videobuf2 queue as per the buffer type */
1027 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1028 if (!common->alloc_ctx) {
1029 vpif_err("Failed to get the context\n");
1030 return -EINVAL;
1031 }
1032 q = &common->buffer_queue;
1033 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1034 q->io_modes = VB2_MMAP | VB2_USERPTR;
1035 q->drv_priv = fh;
1036 q->ops = &video_qops;
1037 q->mem_ops = &vb2_dma_contig_memops;
1038 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1039
1040 vb2_queue_init(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001041
1042 /* Set io allowed member of file handle to TRUE */
1043 fh->io_allowed[index] = 1;
1044 /* Increment io usrs member of channel object to 1 */
1045 common->io_usrs = 1;
1046 /* Store type of memory requested in channel object */
1047 common->memory = reqbuf->memory;
1048 INIT_LIST_HEAD(&common->dma_queue);
1049
1050 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001051 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001052}
1053
1054/**
1055 * vpif_querybuf() - query buffer handler
1056 * @file: file ptr
1057 * @priv: file handle
1058 * @buf: v4l2 buffer structure ptr
1059 */
1060static int vpif_querybuf(struct file *file, void *priv,
1061 struct v4l2_buffer *buf)
1062{
1063 struct vpif_fh *fh = priv;
1064 struct channel_obj *ch = fh->channel;
1065 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1066
1067 vpif_dbg(2, debug, "vpif_querybuf\n");
1068
1069 if (common->fmt.type != buf->type)
1070 return -EINVAL;
1071
1072 if (common->memory != V4L2_MEMORY_MMAP) {
1073 vpif_dbg(1, debug, "Invalid memory\n");
1074 return -EINVAL;
1075 }
1076
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001077 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001078}
1079
1080/**
1081 * vpif_qbuf() - query buffer handler
1082 * @file: file ptr
1083 * @priv: file handle
1084 * @buf: v4l2 buffer structure ptr
1085 */
1086static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1087{
1088
1089 struct vpif_fh *fh = priv;
1090 struct channel_obj *ch = fh->channel;
1091 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1092 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001093
1094 vpif_dbg(2, debug, "vpif_qbuf\n");
1095
1096 if (common->fmt.type != tbuf.type) {
1097 vpif_err("invalid buffer type\n");
1098 return -EINVAL;
1099 }
1100
1101 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001102 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001103 return -EACCES;
1104 }
1105
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001106 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001107}
1108
1109/**
1110 * vpif_dqbuf() - query buffer handler
1111 * @file: file ptr
1112 * @priv: file handle
1113 * @buf: v4l2 buffer structure ptr
1114 */
1115static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1116{
1117 struct vpif_fh *fh = priv;
1118 struct channel_obj *ch = fh->channel;
1119 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1120
1121 vpif_dbg(2, debug, "vpif_dqbuf\n");
1122
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001123 return vb2_dqbuf(&common->buffer_queue, buf,
1124 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001125}
1126
1127/**
1128 * vpif_streamon() - streamon handler
1129 * @file: file ptr
1130 * @priv: file handle
1131 * @buftype: v4l2 buffer type
1132 */
1133static int vpif_streamon(struct file *file, void *priv,
1134 enum v4l2_buf_type buftype)
1135{
1136
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001137 struct vpif_fh *fh = priv;
1138 struct channel_obj *ch = fh->channel;
1139 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1140 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1141 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001142 int ret = 0;
1143
1144 vpif_dbg(2, debug, "vpif_streamon\n");
1145
1146 vpif = &ch->vpifparams;
1147
1148 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1149 vpif_dbg(1, debug, "buffer type not supported\n");
1150 return -EINVAL;
1151 }
1152
1153 /* If file handle is not allowed IO, return error */
1154 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1155 vpif_dbg(1, debug, "io not allowed\n");
1156 return -EACCES;
1157 }
1158
1159 /* If Streaming is already started, return error */
1160 if (common->started) {
1161 vpif_dbg(1, debug, "channel->started\n");
1162 return -EBUSY;
1163 }
1164
1165 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1166 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1167 vpif->std_info.ycmux_mode == 0) ||
1168 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1169 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1170 vpif_dbg(1, debug, "other channel is being used\n");
1171 return -EBUSY;
1172 }
1173
1174 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1175 if (ret)
1176 return ret;
1177
1178 /* Enable streamon on the sub device */
1179 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1180 s_stream, 1);
1181
1182 if (ret && (ret != -ENOIOCTLCMD)) {
1183 vpif_dbg(1, debug, "stream on failed in subdev\n");
1184 return ret;
1185 }
1186
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001187 /* Call vb2_streamon to start streaming in videobuf2 */
1188 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001189 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001190 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001191 return ret;
1192 }
1193
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001194 return ret;
1195}
1196
1197/**
1198 * vpif_streamoff() - streamoff handler
1199 * @file: file ptr
1200 * @priv: file handle
1201 * @buftype: v4l2 buffer type
1202 */
1203static int vpif_streamoff(struct file *file, void *priv,
1204 enum v4l2_buf_type buftype)
1205{
1206
1207 struct vpif_fh *fh = priv;
1208 struct channel_obj *ch = fh->channel;
1209 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1210 int ret;
1211
1212 vpif_dbg(2, debug, "vpif_streamoff\n");
1213
1214 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1215 vpif_dbg(1, debug, "buffer type not supported\n");
1216 return -EINVAL;
1217 }
1218
1219 /* If io is allowed for this file handle, return error */
1220 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1221 vpif_dbg(1, debug, "io not allowed\n");
1222 return -EACCES;
1223 }
1224
1225 /* If streaming is not started, return error */
1226 if (!common->started) {
1227 vpif_dbg(1, debug, "channel->started\n");
1228 return -EINVAL;
1229 }
1230
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001231 /* disable channel */
1232 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1233 enable_channel0(0);
1234 channel0_intr_enable(0);
1235 } else {
1236 enable_channel1(0);
1237 channel1_intr_enable(0);
1238 }
1239
1240 common->started = 0;
1241
1242 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1243 s_stream, 0);
1244
1245 if (ret && (ret != -ENOIOCTLCMD))
1246 vpif_dbg(1, debug, "stream off failed in subdev\n");
1247
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001248 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001249}
1250
1251/**
1252 * vpif_map_sub_device_to_input() - Maps sub device to input
1253 * @ch - ptr to channel
1254 * @config - ptr to capture configuration
1255 * @input_index - Given input index from application
1256 * @sub_device_index - index into sd table
1257 *
1258 * lookup the sub device information for a given input index.
1259 * we report all the inputs to application. inputs table also
1260 * has sub device name for the each input
1261 */
1262static struct vpif_subdev_info *vpif_map_sub_device_to_input(
1263 struct channel_obj *ch,
1264 struct vpif_capture_config *vpif_cfg,
1265 int input_index,
1266 int *sub_device_index)
1267{
1268 struct vpif_capture_chan_config *chan_cfg;
1269 struct vpif_subdev_info *subdev_info = NULL;
1270 const char *subdev_name = NULL;
1271 int i;
1272
1273 vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
1274
1275 chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
1276
1277 /**
1278 * search through the inputs to find the sub device supporting
1279 * the input
1280 */
1281 for (i = 0; i < chan_cfg->input_count; i++) {
1282 /* For each sub device, loop through input */
1283 if (i == input_index) {
1284 subdev_name = chan_cfg->inputs[i].subdev_name;
1285 break;
1286 }
1287 }
1288
1289 /* if reached maximum. return null */
1290 if (i == chan_cfg->input_count || (NULL == subdev_name))
1291 return subdev_info;
1292
1293 /* loop through the sub device list to get the sub device info */
1294 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1295 subdev_info = &vpif_cfg->subdev_info[i];
1296 if (!strcmp(subdev_info->name, subdev_name))
1297 break;
1298 }
1299
1300 if (i == vpif_cfg->subdev_count)
1301 return subdev_info;
1302
1303 /* check if the sub device is registered */
1304 if (NULL == vpif_obj.sd[i])
1305 return NULL;
1306
1307 *sub_device_index = i;
1308 return subdev_info;
1309}
1310
1311/**
1312 * vpif_querystd() - querystd handler
1313 * @file: file ptr
1314 * @priv: file handle
1315 * @std_id: ptr to std id
1316 *
1317 * This function is called to detect standard at the selected input
1318 */
1319static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1320{
1321 struct vpif_fh *fh = priv;
1322 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001323 int ret = 0;
1324
1325 vpif_dbg(2, debug, "vpif_querystd\n");
1326
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001327 /* Call querystd function of decoder device */
1328 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1329 querystd, std_id);
1330 if (ret < 0)
1331 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1332
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001333 return ret;
1334}
1335
1336/**
1337 * vpif_g_std() - get STD handler
1338 * @file: file ptr
1339 * @priv: file handle
1340 * @std_id: ptr to std id
1341 */
1342static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1343{
1344 struct vpif_fh *fh = priv;
1345 struct channel_obj *ch = fh->channel;
1346
1347 vpif_dbg(2, debug, "vpif_g_std\n");
1348
1349 *std = ch->video.stdid;
1350 return 0;
1351}
1352
1353/**
1354 * vpif_s_std() - set STD handler
1355 * @file: file ptr
1356 * @priv: file handle
1357 * @std_id: ptr to std id
1358 */
1359static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1360{
1361 struct vpif_fh *fh = priv;
1362 struct channel_obj *ch = fh->channel;
1363 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1364 int ret = 0;
1365
1366 vpif_dbg(2, debug, "vpif_s_std\n");
1367
1368 if (common->started) {
1369 vpif_err("streaming in progress\n");
1370 return -EBUSY;
1371 }
1372
1373 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1374 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1375 if (!fh->initialized) {
1376 vpif_dbg(1, debug, "Channel Busy\n");
1377 return -EBUSY;
1378 }
1379 }
1380
Hans Verkuilffb48772010-05-01 08:03:24 -03001381 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001382 if (0 != ret)
1383 return ret;
1384
1385 fh->initialized = 1;
1386
1387 /* Call encoder subdevice function to set the standard */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001388 ch->video.stdid = *std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001389 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001390
1391 /* Get the information about the standard */
1392 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001393 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001394 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001395 }
1396
1397 /* Configure the default format information */
1398 vpif_config_format(ch);
1399
1400 /* set standard in the sub device */
1401 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1402 s_std, *std_id);
1403 if (ret < 0)
1404 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001405 return ret;
1406}
1407
1408/**
1409 * vpif_enum_input() - ENUMINPUT handler
1410 * @file: file ptr
1411 * @priv: file handle
1412 * @input: ptr to input structure
1413 */
1414static int vpif_enum_input(struct file *file, void *priv,
1415 struct v4l2_input *input)
1416{
1417
1418 struct vpif_capture_config *config = vpif_dev->platform_data;
1419 struct vpif_capture_chan_config *chan_cfg;
1420 struct vpif_fh *fh = priv;
1421 struct channel_obj *ch = fh->channel;
1422
1423 chan_cfg = &config->chan_config[ch->channel_id];
1424
1425 if (input->index >= chan_cfg->input_count) {
1426 vpif_dbg(1, debug, "Invalid input index\n");
1427 return -EINVAL;
1428 }
1429
1430 memcpy(input, &chan_cfg->inputs[input->index].input,
1431 sizeof(*input));
1432 return 0;
1433}
1434
1435/**
1436 * vpif_g_input() - Get INPUT handler
1437 * @file: file ptr
1438 * @priv: file handle
1439 * @index: ptr to input index
1440 */
1441static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1442{
1443 struct vpif_fh *fh = priv;
1444 struct channel_obj *ch = fh->channel;
1445 struct video_obj *vid_ch = &ch->video;
1446
1447 *index = vid_ch->input_idx;
1448
1449 return 0;
1450}
1451
1452/**
1453 * vpif_s_input() - Set INPUT handler
1454 * @file: file ptr
1455 * @priv: file handle
1456 * @index: input index
1457 */
1458static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1459{
1460 struct vpif_capture_config *config = vpif_dev->platform_data;
1461 struct vpif_capture_chan_config *chan_cfg;
1462 struct vpif_fh *fh = priv;
1463 struct channel_obj *ch = fh->channel;
1464 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1465 struct video_obj *vid_ch = &ch->video;
1466 struct vpif_subdev_info *subdev_info;
1467 int ret = 0, sd_index = 0;
1468 u32 input = 0, output = 0;
1469
1470 chan_cfg = &config->chan_config[ch->channel_id];
1471
1472 if (common->started) {
1473 vpif_err("Streaming in progress\n");
1474 return -EBUSY;
1475 }
1476
1477 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1478 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1479 if (!fh->initialized) {
1480 vpif_dbg(1, debug, "Channel Busy\n");
1481 return -EBUSY;
1482 }
1483 }
1484
Hans Verkuilffb48772010-05-01 08:03:24 -03001485 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001486 if (0 != ret)
1487 return ret;
1488
1489 fh->initialized = 1;
1490 subdev_info = vpif_map_sub_device_to_input(ch, config, index,
1491 &sd_index);
1492 if (NULL == subdev_info) {
1493 vpif_dbg(1, debug,
1494 "couldn't lookup sub device for the input index\n");
1495 return -EINVAL;
1496 }
1497
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001498 /* first setup input path from sub device to vpif */
1499 if (config->setup_input_path) {
1500 ret = config->setup_input_path(ch->channel_id,
1501 subdev_info->name);
1502 if (ret < 0) {
1503 vpif_dbg(1, debug, "couldn't setup input path for the"
1504 " sub device %s, for input index %d\n",
1505 subdev_info->name, index);
Hans Verkuil46656af2011-01-04 06:51:35 -03001506 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001507 }
1508 }
1509
1510 if (subdev_info->can_route) {
1511 input = subdev_info->input;
1512 output = subdev_info->output;
1513 ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
1514 input, output, 0);
1515 if (ret < 0) {
1516 vpif_dbg(1, debug, "Failed to set input\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001517 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001518 }
1519 }
1520 vid_ch->input_idx = index;
1521 ch->curr_subdev_info = subdev_info;
1522 ch->curr_sd_index = sd_index;
1523 /* copy interface parameters to vpif */
1524 ch->vpifparams.iface = subdev_info->vpif_if;
1525
1526 /* update tvnorms from the sub device input info */
1527 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001528 return ret;
1529}
1530
1531/**
1532 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1533 * @file: file ptr
1534 * @priv: file handle
1535 * @index: input index
1536 */
1537static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1538 struct v4l2_fmtdesc *fmt)
1539{
1540 struct vpif_fh *fh = priv;
1541 struct channel_obj *ch = fh->channel;
1542
1543 if (fmt->index != 0) {
1544 vpif_dbg(1, debug, "Invalid format index\n");
1545 return -EINVAL;
1546 }
1547
1548 /* Fill in the information about format */
1549 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1550 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1551 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1552 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1553 } else {
1554 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1555 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1556 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1557 }
1558 return 0;
1559}
1560
1561/**
1562 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1563 * @file: file ptr
1564 * @priv: file handle
1565 * @fmt: ptr to v4l2 format structure
1566 */
1567static int vpif_try_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 v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1573
1574 return vpif_check_format(ch, pixfmt, 1);
1575}
1576
1577
1578/**
1579 * vpif_g_fmt_vid_cap() - Set INPUT handler
1580 * @file: file ptr
1581 * @priv: file handle
1582 * @fmt: ptr to v4l2 format structure
1583 */
1584static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1585 struct v4l2_format *fmt)
1586{
1587 struct vpif_fh *fh = priv;
1588 struct channel_obj *ch = fh->channel;
1589 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1590
1591 /* Check the validity of the buffer type */
1592 if (common->fmt.type != fmt->type)
1593 return -EINVAL;
1594
1595 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001596 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001597 return 0;
1598}
1599
1600/**
1601 * vpif_s_fmt_vid_cap() - Set FMT handler
1602 * @file: file ptr
1603 * @priv: file handle
1604 * @fmt: ptr to v4l2 format structure
1605 */
1606static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1607 struct v4l2_format *fmt)
1608{
1609 struct vpif_fh *fh = priv;
1610 struct channel_obj *ch = fh->channel;
1611 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1612 struct v4l2_pix_format *pixfmt;
1613 int ret = 0;
1614
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001615 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001616
1617 /* If streaming is started, return error */
1618 if (common->started) {
1619 vpif_dbg(1, debug, "Streaming is started\n");
1620 return -EBUSY;
1621 }
1622
1623 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1624 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1625 if (!fh->initialized) {
1626 vpif_dbg(1, debug, "Channel Busy\n");
1627 return -EBUSY;
1628 }
1629 }
1630
Hans Verkuilffb48772010-05-01 08:03:24 -03001631 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001632 if (0 != ret)
1633 return ret;
1634
1635 fh->initialized = 1;
1636
1637 pixfmt = &fmt->fmt.pix;
1638 /* Check for valid field format */
1639 ret = vpif_check_format(ch, pixfmt, 0);
1640
1641 if (ret)
1642 return ret;
1643 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001644 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001645 return 0;
1646}
1647
1648/**
1649 * vpif_querycap() - QUERYCAP handler
1650 * @file: file ptr
1651 * @priv: file handle
1652 * @cap: ptr to v4l2_capability structure
1653 */
1654static int vpif_querycap(struct file *file, void *priv,
1655 struct v4l2_capability *cap)
1656{
1657 struct vpif_capture_config *config = vpif_dev->platform_data;
1658
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001659 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1660 strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
Manjunath Hadli0a631722012-04-13 04:44:00 -03001661 strlcpy(cap->bus_info, "VPIF Platform", sizeof(cap->bus_info));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001662 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1663
1664 return 0;
1665}
1666
1667/**
1668 * vpif_g_priority() - get priority handler
1669 * @file: file ptr
1670 * @priv: file handle
1671 * @prio: ptr to v4l2_priority structure
1672 */
1673static int vpif_g_priority(struct file *file, void *priv,
1674 enum v4l2_priority *prio)
1675{
1676 struct vpif_fh *fh = priv;
1677 struct channel_obj *ch = fh->channel;
1678
1679 *prio = v4l2_prio_max(&ch->prio);
1680
1681 return 0;
1682}
1683
1684/**
1685 * vpif_s_priority() - set priority handler
1686 * @file: file ptr
1687 * @priv: file handle
1688 * @prio: ptr to v4l2_priority structure
1689 */
1690static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1691{
1692 struct vpif_fh *fh = priv;
1693 struct channel_obj *ch = fh->channel;
1694
1695 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1696}
1697
1698/**
1699 * vpif_cropcap() - cropcap handler
1700 * @file: file ptr
1701 * @priv: file handle
1702 * @crop: ptr to v4l2_cropcap structure
1703 */
1704static int vpif_cropcap(struct file *file, void *priv,
1705 struct v4l2_cropcap *crop)
1706{
1707 struct vpif_fh *fh = priv;
1708 struct channel_obj *ch = fh->channel;
1709 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1710
1711 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1712 return -EINVAL;
1713
1714 crop->bounds.left = 0;
1715 crop->bounds.top = 0;
1716 crop->bounds.height = common->height;
1717 crop->bounds.width = common->width;
1718 crop->defrect = crop->bounds;
1719 return 0;
1720}
1721
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001722/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001723 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001724 * @file: file ptr
1725 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001726 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001727 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001728static int
1729vpif_enum_dv_timings(struct file *file, void *priv,
1730 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001731{
1732 struct vpif_fh *fh = priv;
1733 struct channel_obj *ch = fh->channel;
1734
1735 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
Hans Verkuil0598c172012-09-18 07:18:47 -03001736 video, enum_dv_timings, timings);
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001737}
1738
1739/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001740 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001741 * @file: file ptr
1742 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001743 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001744 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001745static int
1746vpif_query_dv_timings(struct file *file, void *priv,
1747 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001748{
1749 struct vpif_fh *fh = priv;
1750 struct channel_obj *ch = fh->channel;
1751
1752 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
Hans Verkuil0598c172012-09-18 07:18:47 -03001753 video, query_dv_timings, timings);
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001754}
1755
Mats Randgaardc027e162010-12-16 12:17:44 -03001756/**
1757 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1758 * @file: file ptr
1759 * @priv: file handle
1760 * @timings: digital video timings
1761 */
1762static int vpif_s_dv_timings(struct file *file, void *priv,
1763 struct v4l2_dv_timings *timings)
1764{
1765 struct vpif_fh *fh = priv;
1766 struct channel_obj *ch = fh->channel;
1767 struct vpif_params *vpifparams = &ch->vpifparams;
1768 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1769 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001770 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001771 int ret;
1772
1773 if (timings->type != V4L2_DV_BT_656_1120) {
1774 vpif_dbg(2, debug, "Timing type not defined\n");
1775 return -EINVAL;
1776 }
1777
1778 /* Configure subdevice timings, if any */
1779 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1780 video, s_dv_timings, timings);
1781 if (ret == -ENOIOCTLCMD) {
1782 vpif_dbg(2, debug, "Custom DV timings not supported by "
1783 "subdevice\n");
1784 return -EINVAL;
1785 }
1786 if (ret < 0) {
1787 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1788 return ret;
1789 }
1790
1791 if (!(timings->bt.width && timings->bt.height &&
1792 (timings->bt.hbackporch ||
1793 timings->bt.hfrontporch ||
1794 timings->bt.hsync) &&
1795 timings->bt.vfrontporch &&
1796 (timings->bt.vbackporch ||
1797 timings->bt.vsync))) {
1798 vpif_dbg(2, debug, "Timings for width, height, "
1799 "horizontal back porch, horizontal sync, "
1800 "horizontal front porch, vertical back porch, "
1801 "vertical sync and vertical back porch "
1802 "must be defined\n");
1803 return -EINVAL;
1804 }
1805
Hans Verkuil0598c172012-09-18 07:18:47 -03001806 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001807
1808 /* Configure video port timings */
1809
1810 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1811 bt->hsync - 8;
1812 std_info->sav2eav = bt->width;
1813
1814 std_info->l1 = 1;
1815 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1816
1817 if (bt->interlaced) {
1818 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1819 std_info->vsize = bt->height * 2 +
1820 bt->vfrontporch + bt->vsync + bt->vbackporch +
1821 bt->il_vfrontporch + bt->il_vsync +
1822 bt->il_vbackporch;
1823 std_info->l5 = std_info->vsize/2 -
1824 (bt->vfrontporch - 1);
1825 std_info->l7 = std_info->vsize/2 + 1;
1826 std_info->l9 = std_info->l7 + bt->il_vsync +
1827 bt->il_vbackporch + 1;
1828 std_info->l11 = std_info->vsize -
1829 (bt->il_vfrontporch - 1);
1830 } else {
1831 vpif_dbg(2, debug, "Required timing values for "
1832 "interlaced BT format missing\n");
1833 return -EINVAL;
1834 }
1835 } else {
1836 std_info->vsize = bt->height + bt->vfrontporch +
1837 bt->vsync + bt->vbackporch;
1838 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1839 }
1840 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1841 std_info->width = bt->width;
1842 std_info->height = bt->height;
1843 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1844 std_info->ycmux_mode = 0;
1845 std_info->capture_format = 0;
1846 std_info->vbi_supported = 0;
1847 std_info->hd_sd = 1;
1848 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001849
1850 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001851 return 0;
1852}
1853
1854/**
1855 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1856 * @file: file ptr
1857 * @priv: file handle
1858 * @timings: digital video timings
1859 */
1860static int vpif_g_dv_timings(struct file *file, void *priv,
1861 struct v4l2_dv_timings *timings)
1862{
1863 struct vpif_fh *fh = priv;
1864 struct channel_obj *ch = fh->channel;
1865 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001866
Hans Verkuil0598c172012-09-18 07:18:47 -03001867 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001868
1869 return 0;
1870}
1871
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001872/*
1873 * vpif_g_chip_ident() - Identify the chip
1874 * @file: file ptr
1875 * @priv: file handle
1876 * @chip: chip identity
1877 *
1878 * Returns zero or -EINVAL if read operations fails.
1879 */
1880static int vpif_g_chip_ident(struct file *file, void *priv,
1881 struct v4l2_dbg_chip_ident *chip)
1882{
1883 chip->ident = V4L2_IDENT_NONE;
1884 chip->revision = 0;
1885 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1886 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1887 vpif_dbg(2, debug, "match_type is invalid.\n");
1888 return -EINVAL;
1889 }
1890
1891 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1892 g_chip_ident, chip);
1893}
1894
1895#ifdef CONFIG_VIDEO_ADV_DEBUG
1896/*
1897 * vpif_dbg_g_register() - Read register
1898 * @file: file ptr
1899 * @priv: file handle
1900 * @reg: register to be read
1901 *
1902 * Debugging only
1903 * Returns zero or -EINVAL if read operations fails.
1904 */
1905static int vpif_dbg_g_register(struct file *file, void *priv,
1906 struct v4l2_dbg_register *reg){
1907 struct vpif_fh *fh = priv;
1908 struct channel_obj *ch = fh->channel;
1909
1910 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1911 g_register, reg);
1912}
1913
1914/*
1915 * vpif_dbg_s_register() - Write to register
1916 * @file: file ptr
1917 * @priv: file handle
1918 * @reg: register to be modified
1919 *
1920 * Debugging only
1921 * Returns zero or -EINVAL if write operations fails.
1922 */
1923static int vpif_dbg_s_register(struct file *file, void *priv,
1924 struct v4l2_dbg_register *reg){
1925 struct vpif_fh *fh = priv;
1926 struct channel_obj *ch = fh->channel;
1927
1928 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1929 s_register, reg);
1930}
1931#endif
1932
1933/*
1934 * vpif_log_status() - Status information
1935 * @file: file ptr
1936 * @priv: file handle
1937 *
1938 * Returns zero.
1939 */
1940static int vpif_log_status(struct file *filep, void *priv)
1941{
1942 /* status for sub devices */
1943 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1944
1945 return 0;
1946}
1947
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001948/* vpif capture ioctl operations */
1949static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1950 .vidioc_querycap = vpif_querycap,
1951 .vidioc_g_priority = vpif_g_priority,
1952 .vidioc_s_priority = vpif_s_priority,
1953 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1954 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1955 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1956 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1957 .vidioc_enum_input = vpif_enum_input,
1958 .vidioc_s_input = vpif_s_input,
1959 .vidioc_g_input = vpif_g_input,
1960 .vidioc_reqbufs = vpif_reqbufs,
1961 .vidioc_querybuf = vpif_querybuf,
1962 .vidioc_querystd = vpif_querystd,
1963 .vidioc_s_std = vpif_s_std,
1964 .vidioc_g_std = vpif_g_std,
1965 .vidioc_qbuf = vpif_qbuf,
1966 .vidioc_dqbuf = vpif_dqbuf,
1967 .vidioc_streamon = vpif_streamon,
1968 .vidioc_streamoff = vpif_streamoff,
1969 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001970 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1971 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001972 .vidioc_s_dv_timings = vpif_s_dv_timings,
1973 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001974 .vidioc_g_chip_ident = vpif_g_chip_ident,
1975#ifdef CONFIG_VIDEO_ADV_DEBUG
1976 .vidioc_g_register = vpif_dbg_g_register,
1977 .vidioc_s_register = vpif_dbg_s_register,
1978#endif
1979 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001980};
1981
1982/* vpif file operations */
1983static struct v4l2_file_operations vpif_fops = {
1984 .owner = THIS_MODULE,
1985 .open = vpif_open,
1986 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001987 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001988 .mmap = vpif_mmap,
1989 .poll = vpif_poll
1990};
1991
1992/* vpif video template */
1993static struct video_device vpif_video_template = {
1994 .name = "vpif",
1995 .fops = &vpif_fops,
1996 .minor = -1,
1997 .ioctl_ops = &vpif_ioctl_ops,
1998};
1999
2000/**
2001 * initialize_vpif() - Initialize vpif data structures
2002 *
2003 * Allocate memory for data structures and initialize them
2004 */
2005static int initialize_vpif(void)
2006{
2007 int err = 0, i, j;
2008 int free_channel_objects_index;
2009
2010 /* Default number of buffers should be 3 */
2011 if ((ch0_numbuffers > 0) &&
2012 (ch0_numbuffers < config_params.min_numbuffers))
2013 ch0_numbuffers = config_params.min_numbuffers;
2014 if ((ch1_numbuffers > 0) &&
2015 (ch1_numbuffers < config_params.min_numbuffers))
2016 ch1_numbuffers = config_params.min_numbuffers;
2017
2018 /* Set buffer size to min buffers size if it is invalid */
2019 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2020 ch0_bufsize =
2021 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2022 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2023 ch1_bufsize =
2024 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2025
2026 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2027 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2028 if (ch0_numbuffers) {
2029 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2030 = ch0_bufsize;
2031 }
2032 if (ch1_numbuffers) {
2033 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2034 = ch1_bufsize;
2035 }
2036
2037 /* Allocate memory for six channel objects */
2038 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2039 vpif_obj.dev[i] =
2040 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2041 /* If memory allocation fails, return error */
2042 if (!vpif_obj.dev[i]) {
2043 free_channel_objects_index = i;
2044 err = -ENOMEM;
2045 goto vpif_init_free_channel_objects;
2046 }
2047 }
2048 return 0;
2049
2050vpif_init_free_channel_objects:
2051 for (j = 0; j < free_channel_objects_index; j++)
2052 kfree(vpif_obj.dev[j]);
2053 return err;
2054}
2055
2056/**
2057 * vpif_probe : This function probes the vpif capture driver
2058 * @pdev: platform device pointer
2059 *
2060 * This creates device entries by register itself to the V4L2 driver and
2061 * initializes fields of each channel objects
2062 */
2063static __init int vpif_probe(struct platform_device *pdev)
2064{
2065 struct vpif_subdev_info *subdevdata;
2066 struct vpif_capture_config *config;
2067 int i, j, k, m, q, err;
2068 struct i2c_adapter *i2c_adap;
2069 struct channel_obj *ch;
2070 struct common_obj *common;
2071 struct video_device *vfd;
2072 struct resource *res;
2073 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03002074 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002075
2076 vpif_dev = &pdev->dev;
2077
2078 err = initialize_vpif();
2079 if (err) {
2080 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2081 return err;
2082 }
2083
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002084 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2085 if (err) {
2086 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2087 return err;
2088 }
2089
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002090 k = 0;
2091 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
2092 for (i = res->start; i <= res->end; i++) {
Manjunath Hadli0316b892012-04-13 04:44:31 -03002093 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
Manjunath Hadli0a631722012-04-13 04:44:00 -03002094 "VPIF_Capture",
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002095 (void *)(&vpif_obj.dev[k]->channel_id))) {
2096 err = -EBUSY;
2097 i--;
2098 goto vpif_int_err;
2099 }
2100 }
2101 k++;
2102 }
2103
2104 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2105 /* Get the pointer to the channel object */
2106 ch = vpif_obj.dev[i];
2107 /* Allocate memory for video device */
2108 vfd = video_device_alloc();
2109 if (NULL == vfd) {
2110 for (j = 0; j < i; j++) {
2111 ch = vpif_obj.dev[j];
2112 video_device_release(ch->video_dev);
2113 }
2114 err = -ENOMEM;
2115 goto vpif_dev_alloc_err;
2116 }
2117
2118 /* Initialize field of video device */
2119 *vfd = vpif_video_template;
2120 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2121 vfd->release = video_device_release;
2122 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002123 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002124 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002125 /* Set video_dev to the video device */
2126 ch->video_dev = vfd;
2127 }
2128
Manjunath Hadli764af392012-04-13 04:49:34 -03002129 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2130 if (res) {
2131 size = resource_size(res);
2132 /* The resources are divided into two equal memory and when we
2133 * have HD output we can add them together
2134 */
2135 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2136 ch = vpif_obj.dev[j];
2137 ch->channel_id = j;
2138 /* only enabled if second resource exists */
2139 config_params.video_limit[ch->channel_id] = 0;
2140 if (size)
2141 config_params.video_limit[ch->channel_id] =
2142 size/2;
2143 }
2144 }
2145
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002146 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2147 ch = vpif_obj.dev[j];
2148 ch->channel_id = j;
2149 common = &(ch->common[VPIF_VIDEO_INDEX]);
2150 spin_lock_init(&common->irqlock);
2151 mutex_init(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -03002152 ch->video_dev->lock = &common->lock;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002153 /* Initialize prio member of channel object */
2154 v4l2_prio_init(&ch->prio);
2155 err = video_register_device(ch->video_dev,
2156 VFL_TYPE_GRABBER, (j ? 1 : 0));
2157 if (err)
2158 goto probe_out;
2159
2160 video_set_drvdata(ch->video_dev, ch);
2161
2162 }
2163
2164 i2c_adap = i2c_get_adapter(1);
2165 config = pdev->dev.platform_data;
2166
2167 subdev_count = config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002168 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002169 GFP_KERNEL);
2170 if (vpif_obj.sd == NULL) {
2171 vpif_err("unable to allocate memory for subdevice pointers\n");
2172 err = -ENOMEM;
2173 goto probe_out;
2174 }
2175
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002176 for (i = 0; i < subdev_count; i++) {
2177 subdevdata = &config->subdev_info[i];
2178 vpif_obj.sd[i] =
2179 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2180 i2c_adap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002181 &subdevdata->board_info,
2182 NULL);
2183
2184 if (!vpif_obj.sd[i]) {
2185 vpif_err("Error registering v4l2 subdevice\n");
2186 goto probe_subdev_out;
2187 }
2188 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2189 subdevdata->name);
2190
2191 if (vpif_obj.sd[i])
2192 vpif_obj.sd[i]->grp_id = 1 << i;
2193 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002194
Manjunath Hadli0a631722012-04-13 04:44:00 -03002195 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002196 return 0;
2197
2198probe_subdev_out:
2199 /* free sub devices memory */
2200 kfree(vpif_obj.sd);
2201
2202 j = VPIF_CAPTURE_MAX_DEVICES;
2203probe_out:
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002204 for (k = 0; k < j; k++) {
2205 /* Get the pointer to the channel object */
2206 ch = vpif_obj.dev[k];
2207 /* Unregister video device */
2208 video_unregister_device(ch->video_dev);
2209 }
2210
2211vpif_dev_alloc_err:
2212 k = VPIF_CAPTURE_MAX_DEVICES-1;
2213 res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2214 i = res->end;
2215
2216vpif_int_err:
2217 for (q = k; q >= 0; q--) {
2218 for (m = i; m >= (int)res->start; m--)
2219 free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2220
2221 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2222 if (res)
2223 i = res->end;
2224 }
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002225 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002226 return err;
2227}
2228
2229/**
2230 * vpif_remove() - driver remove handler
2231 * @device: ptr to platform device structure
2232 *
2233 * The vidoe device is unregistered
2234 */
2235static int vpif_remove(struct platform_device *device)
2236{
2237 int i;
2238 struct channel_obj *ch;
2239
2240 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2241
2242 /* un-register device */
2243 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2244 /* Get the pointer to the channel object */
2245 ch = vpif_obj.dev[i];
2246 /* Unregister video device */
2247 video_unregister_device(ch->video_dev);
2248 }
2249 return 0;
2250}
2251
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002252#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002253/**
2254 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002255 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002256static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002257{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002258
2259 struct common_obj *common;
2260 struct channel_obj *ch;
2261 int i;
2262
2263 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2264 /* Get the pointer to the channel object */
2265 ch = vpif_obj.dev[i];
2266 common = &ch->common[VPIF_VIDEO_INDEX];
2267 mutex_lock(&common->lock);
2268 if (ch->usrs && common->io_usrs) {
2269 /* Disable channel */
2270 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2271 enable_channel0(0);
2272 channel0_intr_enable(0);
2273 }
2274 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2275 common->started == 2) {
2276 enable_channel1(0);
2277 channel1_intr_enable(0);
2278 }
2279 }
2280 mutex_unlock(&common->lock);
2281 }
2282
2283 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002284}
2285
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002286/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002287 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002288 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002289static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002290{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002291 struct common_obj *common;
2292 struct channel_obj *ch;
2293 int i;
2294
2295 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2296 /* Get the pointer to the channel object */
2297 ch = vpif_obj.dev[i];
2298 common = &ch->common[VPIF_VIDEO_INDEX];
2299 mutex_lock(&common->lock);
2300 if (ch->usrs && common->io_usrs) {
2301 /* Disable channel */
2302 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2303 enable_channel0(1);
2304 channel0_intr_enable(1);
2305 }
2306 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2307 common->started == 2) {
2308 enable_channel1(1);
2309 channel1_intr_enable(1);
2310 }
2311 }
2312 mutex_unlock(&common->lock);
2313 }
2314
2315 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002316}
2317
Alexey Dobriyan47145212009-12-14 18:00:08 -08002318static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002319 .suspend = vpif_suspend,
2320 .resume = vpif_resume,
2321};
2322
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002323#define vpif_pm_ops (&vpif_dev_pm_ops)
2324#else
2325#define vpif_pm_ops NULL
2326#endif
2327
Mats Randgaardffa1b392010-08-30 10:30:36 -03002328static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002329 .driver = {
2330 .name = "vpif_capture",
2331 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002332 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002333 },
2334 .probe = vpif_probe,
2335 .remove = vpif_remove,
2336};
2337
2338/**
2339 * vpif_init: initialize the vpif driver
2340 *
2341 * This function registers device and driver to the kernel, requests irq
2342 * handler and allocates memory
2343 * for channel objects
2344 */
2345static __init int vpif_init(void)
2346{
2347 return platform_driver_register(&vpif_driver);
2348}
2349
2350/**
2351 * vpif_cleanup : This function clean up the vpif capture resources
2352 *
2353 * This will un-registers device and driver to the kernel, frees
2354 * requested irq handler and de-allocates memory allocated for channel
2355 * objects.
2356 */
2357static void vpif_cleanup(void)
2358{
2359 struct platform_device *pdev;
2360 struct resource *res;
2361 int irq_num;
2362 int i = 0;
2363
2364 pdev = container_of(vpif_dev, struct platform_device, dev);
2365 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2366 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2367 free_irq(irq_num,
2368 (void *)(&vpif_obj.dev[i]->channel_id));
2369 i++;
2370 }
2371
2372 platform_driver_unregister(&vpif_driver);
2373
2374 kfree(vpif_obj.sd);
2375 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2376 kfree(vpif_obj.dev[i]);
2377}
2378
2379/* Function for module initialization and cleanup */
2380module_init(vpif_init);
2381module_exit(vpif_cleanup);