blob: f8e65909dc37fd4945a3b68b524d08d396563832 [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>
36#include <linux/version.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030038#include <media/v4l2-device.h>
39#include <media/v4l2-ioctl.h>
Mats Randgaard7036d6a2010-12-16 12:17:41 -030040#include <media/v4l2-chip-ident.h>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030041
42#include "vpif_capture.h"
43#include "vpif.h"
44
45MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
46MODULE_LICENSE("GPL");
47
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;
83
84/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030085 * vpif_uservirt_to_phys : translate user/virtual address to phy address
86 * @virtp: user/virtual address
87 *
88 * This inline function is used to convert user space virtual address to
89 * physical address.
90 */
91static inline u32 vpif_uservirt_to_phys(u32 virtp)
92{
93 unsigned long physp = 0;
94 struct mm_struct *mm = current->mm;
95 struct vm_area_struct *vma;
96
97 vma = find_vma(mm, virtp);
98
99 /* For kernel direct-mapped memory, take the easy way */
100 if (virtp >= PAGE_OFFSET)
101 physp = virt_to_phys((void *)virtp);
102 else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
103 /**
104 * this will catch, kernel-allocated, mmaped-to-usermode
105 * addresses
106 */
107 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
108 else {
109 /* otherwise, use get_user_pages() for general userland pages */
110 int res, nr_pages = 1;
111 struct page *pages;
112
113 down_read(&current->mm->mmap_sem);
114
115 res = get_user_pages(current, current->mm,
116 virtp, nr_pages, 1, 0, &pages, NULL);
117 up_read(&current->mm->mmap_sem);
118
119 if (res == nr_pages)
120 physp = __pa(page_address(&pages[0]) +
121 (virtp & ~PAGE_MASK));
122 else {
123 vpif_err("get_user_pages failed\n");
124 return 0;
125 }
126 }
127 return physp;
128}
129
130/**
131 * buffer_prepare : callback function for buffer prepare
132 * @q : buffer queue ptr
133 * @vb: ptr to video buffer
134 * @field: field info
135 *
136 * This is the callback function for buffer prepare when videobuf_qbuf()
137 * function is called. The buffer is prepared and user space virtual address
138 * or user address is converted into physical address
139 */
140static int vpif_buffer_prepare(struct videobuf_queue *q,
141 struct videobuf_buffer *vb,
142 enum v4l2_field field)
143{
144 /* Get the file handle object and channel object */
145 struct vpif_fh *fh = q->priv_data;
146 struct channel_obj *ch = fh->channel;
147 struct common_obj *common;
148 unsigned long addr;
149
150
151 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
152
153 common = &ch->common[VPIF_VIDEO_INDEX];
154
155 /* If buffer is not initialized, initialize it */
156 if (VIDEOBUF_NEEDS_INIT == vb->state) {
157 vb->width = common->width;
158 vb->height = common->height;
159 vb->size = vb->width * vb->height;
160 vb->field = field;
161 }
162 vb->state = VIDEOBUF_PREPARED;
163 /**
164 * if user pointer memory mechanism is used, get the physical
165 * address of the buffer
166 */
167 if (V4L2_MEMORY_USERPTR == common->memory) {
168 if (0 == vb->baddr) {
169 vpif_dbg(1, debug, "buffer address is 0\n");
170 return -EINVAL;
171
172 }
173 vb->boff = vpif_uservirt_to_phys(vb->baddr);
174 if (!IS_ALIGNED(vb->boff, 8))
175 goto exit;
176 }
177
178 addr = vb->boff;
179 if (q->streaming) {
180 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
181 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
182 !IS_ALIGNED((addr + common->ctop_off), 8) ||
183 !IS_ALIGNED((addr + common->cbtm_off), 8))
184 goto exit;
185 }
186 return 0;
187exit:
188 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
189 return -EINVAL;
190}
191
192/**
193 * vpif_buffer_setup : Callback function for buffer setup.
194 * @q: buffer queue ptr
195 * @count: number of buffers
196 * @size: size of the buffer
197 *
198 * This callback function is called when reqbuf() is called to adjust
199 * the buffer count and buffer size
200 */
201static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
202 unsigned int *size)
203{
204 /* Get the file handle object and channel object */
205 struct vpif_fh *fh = q->priv_data;
206 struct channel_obj *ch = fh->channel;
207 struct common_obj *common;
208
209 common = &ch->common[VPIF_VIDEO_INDEX];
210
211 vpif_dbg(2, debug, "vpif_buffer_setup\n");
212
213 /* If memory type is not mmap, return */
214 if (V4L2_MEMORY_MMAP != common->memory)
215 return 0;
216
217 /* Calculate the size of the buffer */
218 *size = config_params.channel_bufsize[ch->channel_id];
219
220 if (*count < config_params.min_numbuffers)
221 *count = config_params.min_numbuffers;
222 return 0;
223}
224
225/**
226 * vpif_buffer_queue : Callback function to add buffer to DMA queue
227 * @q: ptr to videobuf_queue
228 * @vb: ptr to videobuf_buffer
229 */
230static void vpif_buffer_queue(struct videobuf_queue *q,
231 struct videobuf_buffer *vb)
232{
233 /* Get the file handle object and channel object */
234 struct vpif_fh *fh = q->priv_data;
235 struct channel_obj *ch = fh->channel;
236 struct common_obj *common;
237
238 common = &ch->common[VPIF_VIDEO_INDEX];
239
240 vpif_dbg(2, debug, "vpif_buffer_queue\n");
241
242 /* add the buffer to the DMA queue */
243 list_add_tail(&vb->queue, &common->dma_queue);
244 /* Change state of the buffer */
245 vb->state = VIDEOBUF_QUEUED;
246}
247
248/**
249 * vpif_buffer_release : Callback function to free buffer
250 * @q: buffer queue ptr
251 * @vb: ptr to video buffer
252 *
253 * This function is called from the videobuf layer to free memory
254 * allocated to the buffers
255 */
256static void vpif_buffer_release(struct videobuf_queue *q,
257 struct videobuf_buffer *vb)
258{
259 /* Get the file handle object and channel object */
260 struct vpif_fh *fh = q->priv_data;
261 struct channel_obj *ch = fh->channel;
262 struct common_obj *common;
263
264 common = &ch->common[VPIF_VIDEO_INDEX];
265
266 videobuf_dma_contig_free(q, vb);
267 vb->state = VIDEOBUF_NEEDS_INIT;
268}
269
270static struct videobuf_queue_ops video_qops = {
271 .buf_setup = vpif_buffer_setup,
272 .buf_prepare = vpif_buffer_prepare,
273 .buf_queue = vpif_buffer_queue,
274 .buf_release = vpif_buffer_release,
275};
276
277static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
278 { {1, 1} };
279
280/**
281 * vpif_process_buffer_complete: process a completed buffer
282 * @common: ptr to common channel object
283 *
284 * This function time stamp the buffer and mark it as DONE. It also
285 * wake up any process waiting on the QUEUE and set the next buffer
286 * as current
287 */
288static void vpif_process_buffer_complete(struct common_obj *common)
289{
290 do_gettimeofday(&common->cur_frm->ts);
291 common->cur_frm->state = VIDEOBUF_DONE;
292 wake_up_interruptible(&common->cur_frm->done);
293 /* Make curFrm pointing to nextFrm */
294 common->cur_frm = common->next_frm;
295}
296
297/**
298 * vpif_schedule_next_buffer: set next buffer address for capture
299 * @common : ptr to common channel object
300 *
301 * This function will get next buffer from the dma queue and
302 * set the buffer address in the vpif register for capture.
303 * the buffer is marked active
304 */
305static void vpif_schedule_next_buffer(struct common_obj *common)
306{
307 unsigned long addr = 0;
308
309 common->next_frm = list_entry(common->dma_queue.next,
310 struct videobuf_buffer, queue);
311 /* Remove that buffer from the buffer queue */
312 list_del(&common->next_frm->queue);
313 common->next_frm->state = VIDEOBUF_ACTIVE;
314 if (V4L2_MEMORY_USERPTR == common->memory)
315 addr = common->next_frm->boff;
316 else
317 addr = videobuf_to_dma_contig(common->next_frm);
318
319 /* Set top and bottom field addresses in VPIF registers */
320 common->set_addr(addr + common->ytop_off,
321 addr + common->ybtm_off,
322 addr + common->ctop_off,
323 addr + common->cbtm_off);
324}
325
326/**
327 * vpif_channel_isr : ISR handler for vpif capture
328 * @irq: irq number
329 * @dev_id: dev_id ptr
330 *
331 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300332 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300333 */
334static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
335{
336 struct vpif_device *dev = &vpif_obj;
337 struct common_obj *common;
338 struct channel_obj *ch;
339 enum v4l2_field field;
340 int channel_id = 0;
341 int fid = -1, i;
342
343 channel_id = *(int *)(dev_id);
344 ch = dev->dev[channel_id];
345
346 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
347
348 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
349 common = &ch->common[i];
350 /* skip If streaming is not started in this channel */
351 if (0 == common->started)
352 continue;
353
354 /* Check the field format */
355 if (1 == ch->vpifparams.std_info.frm_fmt) {
356 /* Progressive mode */
357 if (list_empty(&common->dma_queue))
358 continue;
359
360 if (!channel_first_int[i][channel_id])
361 vpif_process_buffer_complete(common);
362
363 channel_first_int[i][channel_id] = 0;
364
365 vpif_schedule_next_buffer(common);
366
367
368 channel_first_int[i][channel_id] = 0;
369 } else {
370 /**
371 * Interlaced mode. If it is first interrupt, ignore
372 * it
373 */
374 if (channel_first_int[i][channel_id]) {
375 channel_first_int[i][channel_id] = 0;
376 continue;
377 }
378 if (0 == i) {
379 ch->field_id ^= 1;
380 /* Get field id from VPIF registers */
381 fid = vpif_channel_getfid(ch->channel_id);
382 if (fid != ch->field_id) {
383 /**
384 * If field id does not match stored
385 * field id, make them in sync
386 */
387 if (0 == fid)
388 ch->field_id = fid;
389 return IRQ_HANDLED;
390 }
391 }
392 /* device field id and local field id are in sync */
393 if (0 == fid) {
394 /* this is even field */
395 if (common->cur_frm == common->next_frm)
396 continue;
397
398 /* mark the current buffer as done */
399 vpif_process_buffer_complete(common);
400 } else if (1 == fid) {
401 /* odd field */
402 if (list_empty(&common->dma_queue) ||
403 (common->cur_frm != common->next_frm))
404 continue;
405
406 vpif_schedule_next_buffer(common);
407 }
408 }
409 }
410 return IRQ_HANDLED;
411}
412
413/**
414 * vpif_update_std_info() - update standard related info
415 * @ch: ptr to channel object
416 *
417 * For a given standard selected by application, update values
418 * in the device data structures
419 */
420static int vpif_update_std_info(struct channel_obj *ch)
421{
422 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
423 struct vpif_params *vpifparams = &ch->vpifparams;
424 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300425 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300426 struct video_obj *vid_ch = &ch->video;
427 int index;
428
429 vpif_dbg(2, debug, "vpif_update_std_info\n");
430
Mats Randgaardaa444402010-12-16 12:17:42 -0300431 for (index = 0; index < vpif_ch_params_count; index++) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300432 config = &ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300433 if (config->hd_sd == 0) {
434 vpif_dbg(2, debug, "SD format\n");
435 if (config->stdid & vid_ch->stdid) {
436 memcpy(std_info, config, sizeof(*config));
437 break;
438 }
439 } else {
440 vpif_dbg(2, debug, "HD format\n");
441 if (config->dv_preset == vid_ch->dv_preset) {
442 memcpy(std_info, config, sizeof(*config));
443 break;
444 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300445 }
446 }
447
448 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300449 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300450 return -EINVAL;
451
452 common->fmt.fmt.pix.width = std_info->width;
453 common->width = std_info->width;
454 common->fmt.fmt.pix.height = std_info->height;
455 common->height = std_info->height;
456 common->fmt.fmt.pix.bytesperline = std_info->width;
457 vpifparams->video_params.hpitch = std_info->width;
458 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300459
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300460 return 0;
461}
462
463/**
464 * vpif_calculate_offsets : This function calculates buffers offsets
465 * @ch : ptr to channel object
466 *
467 * This function calculates buffer offsets for Y and C in the top and
468 * bottom field
469 */
470static void vpif_calculate_offsets(struct channel_obj *ch)
471{
472 unsigned int hpitch, vpitch, sizeimage;
473 struct video_obj *vid_ch = &(ch->video);
474 struct vpif_params *vpifparams = &ch->vpifparams;
475 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
476 enum v4l2_field field = common->fmt.fmt.pix.field;
477
478 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
479
480 if (V4L2_FIELD_ANY == field) {
481 if (vpifparams->std_info.frm_fmt)
482 vid_ch->buf_field = V4L2_FIELD_NONE;
483 else
484 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
485 } else
486 vid_ch->buf_field = common->fmt.fmt.pix.field;
487
488 if (V4L2_MEMORY_USERPTR == common->memory)
489 sizeimage = common->fmt.fmt.pix.sizeimage;
490 else
491 sizeimage = config_params.channel_bufsize[ch->channel_id];
492
493 hpitch = common->fmt.fmt.pix.bytesperline;
494 vpitch = sizeimage / (hpitch * 2);
495
496 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
497 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
498 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
499 common->ytop_off = 0;
500 common->ybtm_off = hpitch;
501 common->ctop_off = sizeimage / 2;
502 common->cbtm_off = sizeimage / 2 + hpitch;
503 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
504 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
505 common->ytop_off = 0;
506 common->ybtm_off = sizeimage / 4;
507 common->ctop_off = sizeimage / 2;
508 common->cbtm_off = common->ctop_off + sizeimage / 4;
509 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
510 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
511 common->ybtm_off = 0;
512 common->ytop_off = sizeimage / 4;
513 common->cbtm_off = sizeimage / 2;
514 common->ctop_off = common->cbtm_off + sizeimage / 4;
515 }
516 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
517 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
518 vpifparams->video_params.storage_mode = 1;
519 else
520 vpifparams->video_params.storage_mode = 0;
521
522 if (1 == vpifparams->std_info.frm_fmt)
523 vpifparams->video_params.hpitch =
524 common->fmt.fmt.pix.bytesperline;
525 else {
526 if ((field == V4L2_FIELD_ANY)
527 || (field == V4L2_FIELD_INTERLACED))
528 vpifparams->video_params.hpitch =
529 common->fmt.fmt.pix.bytesperline * 2;
530 else
531 vpifparams->video_params.hpitch =
532 common->fmt.fmt.pix.bytesperline;
533 }
534
535 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
536}
537
538/**
539 * vpif_config_format: configure default frame format in the device
540 * ch : ptr to channel object
541 */
542static void vpif_config_format(struct channel_obj *ch)
543{
544 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
545
546 vpif_dbg(2, debug, "vpif_config_format\n");
547
548 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
549 if (config_params.numbuffers[ch->channel_id] == 0)
550 common->memory = V4L2_MEMORY_USERPTR;
551 else
552 common->memory = V4L2_MEMORY_MMAP;
553
554 common->fmt.fmt.pix.sizeimage
555 = config_params.channel_bufsize[ch->channel_id];
556
557 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
558 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
559 else
560 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
561 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
562}
563
564/**
565 * vpif_get_default_field() - Get default field type based on interface
566 * @vpif_params - ptr to vpif params
567 */
568static inline enum v4l2_field vpif_get_default_field(
569 struct vpif_interface *iface)
570{
571 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
572 V4L2_FIELD_INTERLACED;
573}
574
575/**
576 * vpif_check_format() - check given pixel format for compatibility
577 * @ch - channel ptr
578 * @pixfmt - Given pixel format
579 * @update - update the values as per hardware requirement
580 *
581 * Check the application pixel format for S_FMT and update the input
582 * values as per hardware limits for TRY_FMT. The default pixel and
583 * field format is selected based on interface type.
584 */
585static int vpif_check_format(struct channel_obj *ch,
586 struct v4l2_pix_format *pixfmt,
587 int update)
588{
589 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
590 struct vpif_params *vpif_params = &ch->vpifparams;
591 enum v4l2_field field = pixfmt->field;
592 u32 sizeimage, hpitch, vpitch;
593 int ret = -EINVAL;
594
595 vpif_dbg(2, debug, "vpif_check_format\n");
596 /**
597 * first check for the pixel format. If if_type is Raw bayer,
598 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
599 * V4L2_PIX_FMT_YUV422P is supported
600 */
601 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
602 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
603 if (!update) {
604 vpif_dbg(2, debug, "invalid pix format\n");
605 goto exit;
606 }
607 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
608 }
609 } else {
610 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
611 if (!update) {
612 vpif_dbg(2, debug, "invalid pixel format\n");
613 goto exit;
614 }
615 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
616 }
617 }
618
619 if (!(VPIF_VALID_FIELD(field))) {
620 if (!update) {
621 vpif_dbg(2, debug, "invalid field format\n");
622 goto exit;
623 }
624 /**
625 * By default use FIELD_NONE for RAW Bayer capture
626 * and FIELD_INTERLACED for other interfaces
627 */
628 field = vpif_get_default_field(&vpif_params->iface);
629 } else if (field == V4L2_FIELD_ANY)
630 /* unsupported field. Use default */
631 field = vpif_get_default_field(&vpif_params->iface);
632
633 /* validate the hpitch */
634 hpitch = pixfmt->bytesperline;
635 if (hpitch < vpif_params->std_info.width) {
636 if (!update) {
637 vpif_dbg(2, debug, "invalid hpitch\n");
638 goto exit;
639 }
640 hpitch = vpif_params->std_info.width;
641 }
642
643 if (V4L2_MEMORY_USERPTR == common->memory)
644 sizeimage = pixfmt->sizeimage;
645 else
646 sizeimage = config_params.channel_bufsize[ch->channel_id];
647
648 vpitch = sizeimage / (hpitch * 2);
649
650 /* validate the vpitch */
651 if (vpitch < vpif_params->std_info.height) {
652 if (!update) {
653 vpif_dbg(2, debug, "Invalid vpitch\n");
654 goto exit;
655 }
656 vpitch = vpif_params->std_info.height;
657 }
658
659 /* Check for 8 byte alignment */
660 if (!ALIGN(hpitch, 8)) {
661 if (!update) {
662 vpif_dbg(2, debug, "invalid pitch alignment\n");
663 goto exit;
664 }
665 /* adjust to next 8 byte boundary */
666 hpitch = (((hpitch + 7) / 8) * 8);
667 }
668 /* if update is set, modify the bytesperline and sizeimage */
669 if (update) {
670 pixfmt->bytesperline = hpitch;
671 pixfmt->sizeimage = hpitch * vpitch * 2;
672 }
673 /**
674 * Image width and height is always based on current standard width and
675 * height
676 */
677 pixfmt->width = common->fmt.fmt.pix.width;
678 pixfmt->height = common->fmt.fmt.pix.height;
679 return 0;
680exit:
681 return ret;
682}
683
684/**
685 * vpif_config_addr() - function to configure buffer address in vpif
686 * @ch - channel ptr
687 * @muxmode - channel mux mode
688 */
689static void vpif_config_addr(struct channel_obj *ch, int muxmode)
690{
691 struct common_obj *common;
692
693 vpif_dbg(2, debug, "vpif_config_addr\n");
694
695 common = &(ch->common[VPIF_VIDEO_INDEX]);
696
697 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
698 common->set_addr = ch1_set_videobuf_addr;
699 else if (2 == muxmode)
700 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
701 else
702 common->set_addr = ch0_set_videobuf_addr;
703}
704
705/**
706 * vpfe_mmap : It is used to map kernel space buffers into user spaces
707 * @filep: file pointer
708 * @vma: ptr to vm_area_struct
709 */
710static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
711{
712 /* Get the channel object and file handle object */
713 struct vpif_fh *fh = filep->private_data;
714 struct channel_obj *ch = fh->channel;
715 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
716
717 vpif_dbg(2, debug, "vpif_mmap\n");
718
719 return videobuf_mmap_mapper(&common->buffer_queue, vma);
720}
721
722/**
723 * vpif_poll: It is used for select/poll system call
724 * @filep: file pointer
725 * @wait: poll table to wait
726 */
727static unsigned int vpif_poll(struct file *filep, poll_table * wait)
728{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300729 struct vpif_fh *fh = filep->private_data;
730 struct channel_obj *channel = fh->channel;
731 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
732
733 vpif_dbg(2, debug, "vpif_poll\n");
734
735 if (common->started)
Mats Randgaard11382f32010-09-07 11:28:23 -0300736 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300737 return 0;
738}
739
740/**
741 * vpif_open : vpif open handler
742 * @filep: file ptr
743 *
744 * It creates object of file handle structure and stores it in private_data
745 * member of filepointer
746 */
747static int vpif_open(struct file *filep)
748{
749 struct vpif_capture_config *config = vpif_dev->platform_data;
750 struct video_device *vdev = video_devdata(filep);
751 struct common_obj *common;
752 struct video_obj *vid_ch;
753 struct channel_obj *ch;
754 struct vpif_fh *fh;
755 int i, ret = 0;
756
757 vpif_dbg(2, debug, "vpif_open\n");
758
759 ch = video_get_drvdata(vdev);
760
761 vid_ch = &ch->video;
762 common = &ch->common[VPIF_VIDEO_INDEX];
763
764 if (mutex_lock_interruptible(&common->lock))
765 return -ERESTARTSYS;
766
767 if (NULL == ch->curr_subdev_info) {
768 /**
769 * search through the sub device to see a registered
770 * sub device and make it as current sub device
771 */
772 for (i = 0; i < config->subdev_count; i++) {
773 if (vpif_obj.sd[i]) {
774 /* the sub device is registered */
775 ch->curr_subdev_info = &config->subdev_info[i];
776 /* make first input as the current input */
777 vid_ch->input_idx = 0;
778 break;
779 }
780 }
781 if (i == config->subdev_count) {
782 vpif_err("No sub device registered\n");
783 ret = -ENOENT;
784 goto exit;
785 }
786 }
787
788 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300789 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300790 if (NULL == fh) {
791 vpif_err("unable to allocate memory for file handle object\n");
792 ret = -ENOMEM;
793 goto exit;
794 }
795
796 /* store pointer to fh in private_data member of filep */
797 filep->private_data = fh;
798 fh->channel = ch;
799 fh->initialized = 0;
800 /* If decoder is not initialized. initialize it */
801 if (!ch->initialized) {
802 fh->initialized = 1;
803 ch->initialized = 1;
804 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
805 }
806 /* Increment channel usrs counter */
807 ch->usrs++;
808 /* Set io_allowed member to false */
809 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
810 /* Initialize priority of this instance to default priority */
811 fh->prio = V4L2_PRIORITY_UNSET;
812 v4l2_prio_open(&ch->prio, &fh->prio);
813exit:
814 mutex_unlock(&common->lock);
815 return ret;
816}
817
818/**
819 * vpif_release : function to clean up file close
820 * @filep: file pointer
821 *
822 * This function deletes buffer queue, frees the buffers and the vpfe file
823 * handle
824 */
825static int vpif_release(struct file *filep)
826{
827 struct vpif_fh *fh = filep->private_data;
828 struct channel_obj *ch = fh->channel;
829 struct common_obj *common;
830
831 vpif_dbg(2, debug, "vpif_release\n");
832
833 common = &ch->common[VPIF_VIDEO_INDEX];
834
835 if (mutex_lock_interruptible(&common->lock))
836 return -ERESTARTSYS;
837
838 /* if this instance is doing IO */
839 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
840 /* Reset io_usrs member of channel object */
841 common->io_usrs = 0;
842 /* Disable channel as per its device type and channel id */
843 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
844 enable_channel0(0);
845 channel0_intr_enable(0);
846 }
847 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
848 (2 == common->started)) {
849 enable_channel1(0);
850 channel1_intr_enable(0);
851 }
852 common->started = 0;
853 /* Free buffers allocated */
854 videobuf_queue_cancel(&common->buffer_queue);
855 videobuf_mmap_free(&common->buffer_queue);
856 }
857
858 /* Decrement channel usrs counter */
859 ch->usrs--;
860
861 /* unlock mutex on channel object */
862 mutex_unlock(&common->lock);
863
864 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300865 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300866
867 if (fh->initialized)
868 ch->initialized = 0;
869
870 filep->private_data = NULL;
871 kfree(fh);
872 return 0;
873}
874
875/**
876 * vpif_reqbufs() - request buffer handler
877 * @file: file ptr
878 * @priv: file handle
879 * @reqbuf: request buffer structure ptr
880 */
881static int vpif_reqbufs(struct file *file, void *priv,
882 struct v4l2_requestbuffers *reqbuf)
883{
884 struct vpif_fh *fh = priv;
885 struct channel_obj *ch = fh->channel;
886 struct common_obj *common;
887 u8 index = 0;
888 int ret = 0;
889
890 vpif_dbg(2, debug, "vpif_reqbufs\n");
891
892 /**
893 * This file handle has not initialized the channel,
894 * It is not allowed to do settings
895 */
896 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
897 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
898 if (!fh->initialized) {
899 vpif_dbg(1, debug, "Channel Busy\n");
900 return -EBUSY;
901 }
902 }
903
904 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
905 return -EINVAL;
906
907 index = VPIF_VIDEO_INDEX;
908
909 common = &ch->common[index];
910
911 if (mutex_lock_interruptible(&common->lock))
912 return -ERESTARTSYS;
913
914 if (0 != common->io_usrs) {
915 ret = -EBUSY;
916 goto reqbuf_exit;
917 }
918
919 /* Initialize videobuf queue as per the buffer type */
920 videobuf_queue_dma_contig_init(&common->buffer_queue,
921 &video_qops, NULL,
922 &common->irqlock,
923 reqbuf->type,
924 common->fmt.fmt.pix.field,
Hans Verkuile3cfd442010-09-30 09:18:52 -0300925 sizeof(struct videobuf_buffer), fh,
926 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300927
928 /* Set io allowed member of file handle to TRUE */
929 fh->io_allowed[index] = 1;
930 /* Increment io usrs member of channel object to 1 */
931 common->io_usrs = 1;
932 /* Store type of memory requested in channel object */
933 common->memory = reqbuf->memory;
934 INIT_LIST_HEAD(&common->dma_queue);
935
936 /* Allocate buffers */
937 ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
938
939reqbuf_exit:
940 mutex_unlock(&common->lock);
941 return ret;
942}
943
944/**
945 * vpif_querybuf() - query buffer handler
946 * @file: file ptr
947 * @priv: file handle
948 * @buf: v4l2 buffer structure ptr
949 */
950static int vpif_querybuf(struct file *file, void *priv,
951 struct v4l2_buffer *buf)
952{
953 struct vpif_fh *fh = priv;
954 struct channel_obj *ch = fh->channel;
955 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
956
957 vpif_dbg(2, debug, "vpif_querybuf\n");
958
959 if (common->fmt.type != buf->type)
960 return -EINVAL;
961
962 if (common->memory != V4L2_MEMORY_MMAP) {
963 vpif_dbg(1, debug, "Invalid memory\n");
964 return -EINVAL;
965 }
966
967 return videobuf_querybuf(&common->buffer_queue, buf);
968}
969
970/**
971 * vpif_qbuf() - query buffer handler
972 * @file: file ptr
973 * @priv: file handle
974 * @buf: v4l2 buffer structure ptr
975 */
976static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
977{
978
979 struct vpif_fh *fh = priv;
980 struct channel_obj *ch = fh->channel;
981 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
982 struct v4l2_buffer tbuf = *buf;
983 struct videobuf_buffer *buf1;
984 unsigned long addr = 0;
985 unsigned long flags;
986 int ret = 0;
987
988 vpif_dbg(2, debug, "vpif_qbuf\n");
989
990 if (common->fmt.type != tbuf.type) {
991 vpif_err("invalid buffer type\n");
992 return -EINVAL;
993 }
994
995 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
996 vpif_err("fh io not allowed \n");
997 return -EACCES;
998 }
999
1000 if (!(list_empty(&common->dma_queue)) ||
1001 (common->cur_frm != common->next_frm) ||
1002 !common->started ||
1003 (common->started && (0 == ch->field_id)))
1004 return videobuf_qbuf(&common->buffer_queue, buf);
1005
1006 /* bufferqueue is empty store buffer address in VPIF registers */
1007 mutex_lock(&common->buffer_queue.vb_lock);
1008 buf1 = common->buffer_queue.bufs[tbuf.index];
1009
1010 if ((buf1->state == VIDEOBUF_QUEUED) ||
1011 (buf1->state == VIDEOBUF_ACTIVE)) {
1012 vpif_err("invalid state\n");
1013 goto qbuf_exit;
1014 }
1015
1016 switch (buf1->memory) {
1017 case V4L2_MEMORY_MMAP:
1018 if (buf1->baddr == 0)
1019 goto qbuf_exit;
1020 break;
1021
1022 case V4L2_MEMORY_USERPTR:
1023 if (tbuf.length < buf1->bsize)
1024 goto qbuf_exit;
1025
1026 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
Julia Lawall473d8022010-08-05 17:22:44 -03001027 && (buf1->baddr != tbuf.m.userptr)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001028 vpif_buffer_release(&common->buffer_queue, buf1);
1029 buf1->baddr = tbuf.m.userptr;
Julia Lawall473d8022010-08-05 17:22:44 -03001030 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001031 break;
1032
1033 default:
1034 goto qbuf_exit;
1035 }
1036
1037 local_irq_save(flags);
1038 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
1039 common->buffer_queue.field);
1040 if (ret < 0) {
1041 local_irq_restore(flags);
1042 goto qbuf_exit;
1043 }
1044
1045 buf1->state = VIDEOBUF_ACTIVE;
1046
1047 if (V4L2_MEMORY_USERPTR == common->memory)
1048 addr = buf1->boff;
1049 else
1050 addr = videobuf_to_dma_contig(buf1);
1051
1052 common->next_frm = buf1;
1053 common->set_addr(addr + common->ytop_off,
1054 addr + common->ybtm_off,
1055 addr + common->ctop_off,
1056 addr + common->cbtm_off);
1057
1058 local_irq_restore(flags);
1059 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
1060 mutex_unlock(&common->buffer_queue.vb_lock);
1061 return 0;
1062
1063qbuf_exit:
1064 mutex_unlock(&common->buffer_queue.vb_lock);
1065 return -EINVAL;
1066}
1067
1068/**
1069 * vpif_dqbuf() - query buffer handler
1070 * @file: file ptr
1071 * @priv: file handle
1072 * @buf: v4l2 buffer structure ptr
1073 */
1074static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
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
1080 vpif_dbg(2, debug, "vpif_dqbuf\n");
1081
1082 return videobuf_dqbuf(&common->buffer_queue, buf,
1083 file->f_flags & O_NONBLOCK);
1084}
1085
1086/**
1087 * vpif_streamon() - streamon handler
1088 * @file: file ptr
1089 * @priv: file handle
1090 * @buftype: v4l2 buffer type
1091 */
1092static int vpif_streamon(struct file *file, void *priv,
1093 enum v4l2_buf_type buftype)
1094{
1095
1096 struct vpif_capture_config *config = vpif_dev->platform_data;
1097 struct vpif_fh *fh = priv;
1098 struct channel_obj *ch = fh->channel;
1099 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1100 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1101 struct vpif_params *vpif;
1102 unsigned long addr = 0;
1103 int ret = 0;
1104
1105 vpif_dbg(2, debug, "vpif_streamon\n");
1106
1107 vpif = &ch->vpifparams;
1108
1109 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1110 vpif_dbg(1, debug, "buffer type not supported\n");
1111 return -EINVAL;
1112 }
1113
1114 /* If file handle is not allowed IO, return error */
1115 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1116 vpif_dbg(1, debug, "io not allowed\n");
1117 return -EACCES;
1118 }
1119
1120 /* If Streaming is already started, return error */
1121 if (common->started) {
1122 vpif_dbg(1, debug, "channel->started\n");
1123 return -EBUSY;
1124 }
1125
1126 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1127 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1128 vpif->std_info.ycmux_mode == 0) ||
1129 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1130 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1131 vpif_dbg(1, debug, "other channel is being used\n");
1132 return -EBUSY;
1133 }
1134
1135 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1136 if (ret)
1137 return ret;
1138
1139 /* Enable streamon on the sub device */
1140 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1141 s_stream, 1);
1142
1143 if (ret && (ret != -ENOIOCTLCMD)) {
1144 vpif_dbg(1, debug, "stream on failed in subdev\n");
1145 return ret;
1146 }
1147
1148 /* Call videobuf_streamon to start streaming in videobuf */
1149 ret = videobuf_streamon(&common->buffer_queue);
1150 if (ret) {
1151 vpif_dbg(1, debug, "videobuf_streamon\n");
1152 return ret;
1153 }
1154
1155 if (mutex_lock_interruptible(&common->lock)) {
1156 ret = -ERESTARTSYS;
1157 goto streamoff_exit;
1158 }
1159
1160 /* If buffer queue is empty, return error */
1161 if (list_empty(&common->dma_queue)) {
1162 vpif_dbg(1, debug, "buffer queue is empty\n");
1163 ret = -EIO;
1164 goto exit;
1165 }
1166
1167 /* Get the next frame from the buffer queue */
1168 common->cur_frm = list_entry(common->dma_queue.next,
1169 struct videobuf_buffer, queue);
1170 common->next_frm = common->cur_frm;
1171
1172 /* Remove buffer from the buffer queue */
1173 list_del(&common->cur_frm->queue);
1174 /* Mark state of the current frame to active */
1175 common->cur_frm->state = VIDEOBUF_ACTIVE;
1176 /* Initialize field_id and started member */
1177 ch->field_id = 0;
1178 common->started = 1;
1179
1180 if (V4L2_MEMORY_USERPTR == common->memory)
1181 addr = common->cur_frm->boff;
1182 else
1183 addr = videobuf_to_dma_contig(common->cur_frm);
1184
1185 /* Calculate the offset for Y and C data in the buffer */
1186 vpif_calculate_offsets(ch);
1187
1188 if ((vpif->std_info.frm_fmt &&
1189 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
1190 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
1191 (!vpif->std_info.frm_fmt &&
1192 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1193 vpif_dbg(1, debug, "conflict in field format and std format\n");
1194 ret = -EINVAL;
1195 goto exit;
1196 }
1197
1198 /* configure 1 or 2 channel mode */
1199 ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
1200
1201 if (ret < 0) {
1202 vpif_dbg(1, debug, "can't set vpif channel mode\n");
1203 goto exit;
1204 }
1205
1206 /* Call vpif_set_params function to set the parameters and addresses */
1207 ret = vpif_set_video_params(vpif, ch->channel_id);
1208
1209 if (ret < 0) {
1210 vpif_dbg(1, debug, "can't set video params\n");
1211 goto exit;
1212 }
1213
1214 common->started = ret;
1215 vpif_config_addr(ch, ret);
1216
1217 common->set_addr(addr + common->ytop_off,
1218 addr + common->ybtm_off,
1219 addr + common->ctop_off,
1220 addr + common->cbtm_off);
1221
1222 /**
1223 * Set interrupt for both the fields in VPIF Register enable channel in
1224 * VPIF register
1225 */
1226 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
1227 channel0_intr_assert();
1228 channel0_intr_enable(1);
1229 enable_channel0(1);
1230 }
1231 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
1232 (common->started == 2)) {
1233 channel1_intr_assert();
1234 channel1_intr_enable(1);
1235 enable_channel1(1);
1236 }
1237 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1238 mutex_unlock(&common->lock);
1239 return ret;
1240
1241exit:
1242 mutex_unlock(&common->lock);
1243streamoff_exit:
1244 ret = videobuf_streamoff(&common->buffer_queue);
1245 return ret;
1246}
1247
1248/**
1249 * vpif_streamoff() - streamoff handler
1250 * @file: file ptr
1251 * @priv: file handle
1252 * @buftype: v4l2 buffer type
1253 */
1254static int vpif_streamoff(struct file *file, void *priv,
1255 enum v4l2_buf_type buftype)
1256{
1257
1258 struct vpif_fh *fh = priv;
1259 struct channel_obj *ch = fh->channel;
1260 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1261 int ret;
1262
1263 vpif_dbg(2, debug, "vpif_streamoff\n");
1264
1265 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1266 vpif_dbg(1, debug, "buffer type not supported\n");
1267 return -EINVAL;
1268 }
1269
1270 /* If io is allowed for this file handle, return error */
1271 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1272 vpif_dbg(1, debug, "io not allowed\n");
1273 return -EACCES;
1274 }
1275
1276 /* If streaming is not started, return error */
1277 if (!common->started) {
1278 vpif_dbg(1, debug, "channel->started\n");
1279 return -EINVAL;
1280 }
1281
1282 if (mutex_lock_interruptible(&common->lock))
1283 return -ERESTARTSYS;
1284
1285 /* disable channel */
1286 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1287 enable_channel0(0);
1288 channel0_intr_enable(0);
1289 } else {
1290 enable_channel1(0);
1291 channel1_intr_enable(0);
1292 }
1293
1294 common->started = 0;
1295
1296 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1297 s_stream, 0);
1298
1299 if (ret && (ret != -ENOIOCTLCMD))
1300 vpif_dbg(1, debug, "stream off failed in subdev\n");
1301
1302 mutex_unlock(&common->lock);
1303
1304 return videobuf_streamoff(&common->buffer_queue);
1305}
1306
1307/**
1308 * vpif_map_sub_device_to_input() - Maps sub device to input
1309 * @ch - ptr to channel
1310 * @config - ptr to capture configuration
1311 * @input_index - Given input index from application
1312 * @sub_device_index - index into sd table
1313 *
1314 * lookup the sub device information for a given input index.
1315 * we report all the inputs to application. inputs table also
1316 * has sub device name for the each input
1317 */
1318static struct vpif_subdev_info *vpif_map_sub_device_to_input(
1319 struct channel_obj *ch,
1320 struct vpif_capture_config *vpif_cfg,
1321 int input_index,
1322 int *sub_device_index)
1323{
1324 struct vpif_capture_chan_config *chan_cfg;
1325 struct vpif_subdev_info *subdev_info = NULL;
1326 const char *subdev_name = NULL;
1327 int i;
1328
1329 vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
1330
1331 chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
1332
1333 /**
1334 * search through the inputs to find the sub device supporting
1335 * the input
1336 */
1337 for (i = 0; i < chan_cfg->input_count; i++) {
1338 /* For each sub device, loop through input */
1339 if (i == input_index) {
1340 subdev_name = chan_cfg->inputs[i].subdev_name;
1341 break;
1342 }
1343 }
1344
1345 /* if reached maximum. return null */
1346 if (i == chan_cfg->input_count || (NULL == subdev_name))
1347 return subdev_info;
1348
1349 /* loop through the sub device list to get the sub device info */
1350 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1351 subdev_info = &vpif_cfg->subdev_info[i];
1352 if (!strcmp(subdev_info->name, subdev_name))
1353 break;
1354 }
1355
1356 if (i == vpif_cfg->subdev_count)
1357 return subdev_info;
1358
1359 /* check if the sub device is registered */
1360 if (NULL == vpif_obj.sd[i])
1361 return NULL;
1362
1363 *sub_device_index = i;
1364 return subdev_info;
1365}
1366
1367/**
1368 * vpif_querystd() - querystd handler
1369 * @file: file ptr
1370 * @priv: file handle
1371 * @std_id: ptr to std id
1372 *
1373 * This function is called to detect standard at the selected input
1374 */
1375static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1376{
1377 struct vpif_fh *fh = priv;
1378 struct channel_obj *ch = fh->channel;
1379 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1380 int ret = 0;
1381
1382 vpif_dbg(2, debug, "vpif_querystd\n");
1383
1384 if (mutex_lock_interruptible(&common->lock))
1385 return -ERESTARTSYS;
1386
1387 /* Call querystd function of decoder device */
1388 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1389 querystd, std_id);
1390 if (ret < 0)
1391 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1392
1393 mutex_unlock(&common->lock);
1394 return ret;
1395}
1396
1397/**
1398 * vpif_g_std() - get STD handler
1399 * @file: file ptr
1400 * @priv: file handle
1401 * @std_id: ptr to std id
1402 */
1403static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1404{
1405 struct vpif_fh *fh = priv;
1406 struct channel_obj *ch = fh->channel;
1407
1408 vpif_dbg(2, debug, "vpif_g_std\n");
1409
1410 *std = ch->video.stdid;
1411 return 0;
1412}
1413
1414/**
1415 * vpif_s_std() - set STD handler
1416 * @file: file ptr
1417 * @priv: file handle
1418 * @std_id: ptr to std id
1419 */
1420static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1421{
1422 struct vpif_fh *fh = priv;
1423 struct channel_obj *ch = fh->channel;
1424 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1425 int ret = 0;
1426
1427 vpif_dbg(2, debug, "vpif_s_std\n");
1428
1429 if (common->started) {
1430 vpif_err("streaming in progress\n");
1431 return -EBUSY;
1432 }
1433
1434 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1435 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1436 if (!fh->initialized) {
1437 vpif_dbg(1, debug, "Channel Busy\n");
1438 return -EBUSY;
1439 }
1440 }
1441
Hans Verkuilffb48772010-05-01 08:03:24 -03001442 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001443 if (0 != ret)
1444 return ret;
1445
1446 fh->initialized = 1;
1447
1448 /* Call encoder subdevice function to set the standard */
1449 if (mutex_lock_interruptible(&common->lock))
1450 return -ERESTARTSYS;
1451
1452 ch->video.stdid = *std_id;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001453 ch->video.dv_preset = V4L2_DV_INVALID;
Mats Randgaardc027e162010-12-16 12:17:44 -03001454 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001455
1456 /* Get the information about the standard */
1457 if (vpif_update_std_info(ch)) {
1458 ret = -EINVAL;
1459 vpif_err("Error getting the standard info\n");
1460 goto s_std_exit;
1461 }
1462
1463 /* Configure the default format information */
1464 vpif_config_format(ch);
1465
1466 /* set standard in the sub device */
1467 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1468 s_std, *std_id);
1469 if (ret < 0)
1470 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1471
1472s_std_exit:
1473 mutex_unlock(&common->lock);
1474 return ret;
1475}
1476
1477/**
1478 * vpif_enum_input() - ENUMINPUT handler
1479 * @file: file ptr
1480 * @priv: file handle
1481 * @input: ptr to input structure
1482 */
1483static int vpif_enum_input(struct file *file, void *priv,
1484 struct v4l2_input *input)
1485{
1486
1487 struct vpif_capture_config *config = vpif_dev->platform_data;
1488 struct vpif_capture_chan_config *chan_cfg;
1489 struct vpif_fh *fh = priv;
1490 struct channel_obj *ch = fh->channel;
1491
1492 chan_cfg = &config->chan_config[ch->channel_id];
1493
1494 if (input->index >= chan_cfg->input_count) {
1495 vpif_dbg(1, debug, "Invalid input index\n");
1496 return -EINVAL;
1497 }
1498
1499 memcpy(input, &chan_cfg->inputs[input->index].input,
1500 sizeof(*input));
1501 return 0;
1502}
1503
1504/**
1505 * vpif_g_input() - Get INPUT handler
1506 * @file: file ptr
1507 * @priv: file handle
1508 * @index: ptr to input index
1509 */
1510static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1511{
1512 struct vpif_fh *fh = priv;
1513 struct channel_obj *ch = fh->channel;
1514 struct video_obj *vid_ch = &ch->video;
1515
1516 *index = vid_ch->input_idx;
1517
1518 return 0;
1519}
1520
1521/**
1522 * vpif_s_input() - Set INPUT handler
1523 * @file: file ptr
1524 * @priv: file handle
1525 * @index: input index
1526 */
1527static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1528{
1529 struct vpif_capture_config *config = vpif_dev->platform_data;
1530 struct vpif_capture_chan_config *chan_cfg;
1531 struct vpif_fh *fh = priv;
1532 struct channel_obj *ch = fh->channel;
1533 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1534 struct video_obj *vid_ch = &ch->video;
1535 struct vpif_subdev_info *subdev_info;
1536 int ret = 0, sd_index = 0;
1537 u32 input = 0, output = 0;
1538
1539 chan_cfg = &config->chan_config[ch->channel_id];
1540
1541 if (common->started) {
1542 vpif_err("Streaming in progress\n");
1543 return -EBUSY;
1544 }
1545
1546 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1547 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1548 if (!fh->initialized) {
1549 vpif_dbg(1, debug, "Channel Busy\n");
1550 return -EBUSY;
1551 }
1552 }
1553
Hans Verkuilffb48772010-05-01 08:03:24 -03001554 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001555 if (0 != ret)
1556 return ret;
1557
1558 fh->initialized = 1;
1559 subdev_info = vpif_map_sub_device_to_input(ch, config, index,
1560 &sd_index);
1561 if (NULL == subdev_info) {
1562 vpif_dbg(1, debug,
1563 "couldn't lookup sub device for the input index\n");
1564 return -EINVAL;
1565 }
1566
1567 if (mutex_lock_interruptible(&common->lock))
1568 return -ERESTARTSYS;
1569
1570 /* first setup input path from sub device to vpif */
1571 if (config->setup_input_path) {
1572 ret = config->setup_input_path(ch->channel_id,
1573 subdev_info->name);
1574 if (ret < 0) {
1575 vpif_dbg(1, debug, "couldn't setup input path for the"
1576 " sub device %s, for input index %d\n",
1577 subdev_info->name, index);
1578 goto exit;
1579 }
1580 }
1581
1582 if (subdev_info->can_route) {
1583 input = subdev_info->input;
1584 output = subdev_info->output;
1585 ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
1586 input, output, 0);
1587 if (ret < 0) {
1588 vpif_dbg(1, debug, "Failed to set input\n");
1589 goto exit;
1590 }
1591 }
1592 vid_ch->input_idx = index;
1593 ch->curr_subdev_info = subdev_info;
1594 ch->curr_sd_index = sd_index;
1595 /* copy interface parameters to vpif */
1596 ch->vpifparams.iface = subdev_info->vpif_if;
1597
1598 /* update tvnorms from the sub device input info */
1599 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1600
1601exit:
1602 mutex_unlock(&common->lock);
1603 return ret;
1604}
1605
1606/**
1607 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1608 * @file: file ptr
1609 * @priv: file handle
1610 * @index: input index
1611 */
1612static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1613 struct v4l2_fmtdesc *fmt)
1614{
1615 struct vpif_fh *fh = priv;
1616 struct channel_obj *ch = fh->channel;
1617
1618 if (fmt->index != 0) {
1619 vpif_dbg(1, debug, "Invalid format index\n");
1620 return -EINVAL;
1621 }
1622
1623 /* Fill in the information about format */
1624 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1625 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1626 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1627 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1628 } else {
1629 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1630 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1631 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1632 }
1633 return 0;
1634}
1635
1636/**
1637 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1638 * @file: file ptr
1639 * @priv: file handle
1640 * @fmt: ptr to v4l2 format structure
1641 */
1642static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1643 struct v4l2_format *fmt)
1644{
1645 struct vpif_fh *fh = priv;
1646 struct channel_obj *ch = fh->channel;
1647 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1648
1649 return vpif_check_format(ch, pixfmt, 1);
1650}
1651
1652
1653/**
1654 * vpif_g_fmt_vid_cap() - Set INPUT handler
1655 * @file: file ptr
1656 * @priv: file handle
1657 * @fmt: ptr to v4l2 format structure
1658 */
1659static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1660 struct v4l2_format *fmt)
1661{
1662 struct vpif_fh *fh = priv;
1663 struct channel_obj *ch = fh->channel;
1664 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1665
1666 /* Check the validity of the buffer type */
1667 if (common->fmt.type != fmt->type)
1668 return -EINVAL;
1669
1670 /* Fill in the information about format */
1671 if (mutex_lock_interruptible(&common->lock))
1672 return -ERESTARTSYS;
1673
1674 *fmt = common->fmt;
1675 mutex_unlock(&common->lock);
1676 return 0;
1677}
1678
1679/**
1680 * vpif_s_fmt_vid_cap() - Set FMT handler
1681 * @file: file ptr
1682 * @priv: file handle
1683 * @fmt: ptr to v4l2 format structure
1684 */
1685static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1686 struct v4l2_format *fmt)
1687{
1688 struct vpif_fh *fh = priv;
1689 struct channel_obj *ch = fh->channel;
1690 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1691 struct v4l2_pix_format *pixfmt;
1692 int ret = 0;
1693
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001694 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001695
1696 /* If streaming is started, return error */
1697 if (common->started) {
1698 vpif_dbg(1, debug, "Streaming is started\n");
1699 return -EBUSY;
1700 }
1701
1702 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1703 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1704 if (!fh->initialized) {
1705 vpif_dbg(1, debug, "Channel Busy\n");
1706 return -EBUSY;
1707 }
1708 }
1709
Hans Verkuilffb48772010-05-01 08:03:24 -03001710 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001711 if (0 != ret)
1712 return ret;
1713
1714 fh->initialized = 1;
1715
1716 pixfmt = &fmt->fmt.pix;
1717 /* Check for valid field format */
1718 ret = vpif_check_format(ch, pixfmt, 0);
1719
1720 if (ret)
1721 return ret;
1722 /* store the format in the channel object */
1723 if (mutex_lock_interruptible(&common->lock))
1724 return -ERESTARTSYS;
1725
1726 common->fmt = *fmt;
1727 mutex_unlock(&common->lock);
1728
1729 return 0;
1730}
1731
1732/**
1733 * vpif_querycap() - QUERYCAP handler
1734 * @file: file ptr
1735 * @priv: file handle
1736 * @cap: ptr to v4l2_capability structure
1737 */
1738static int vpif_querycap(struct file *file, void *priv,
1739 struct v4l2_capability *cap)
1740{
1741 struct vpif_capture_config *config = vpif_dev->platform_data;
1742
1743 cap->version = VPIF_CAPTURE_VERSION_CODE;
1744 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1745 strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
1746 strlcpy(cap->bus_info, "DM646x Platform", sizeof(cap->bus_info));
1747 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1748
1749 return 0;
1750}
1751
1752/**
1753 * vpif_g_priority() - get priority handler
1754 * @file: file ptr
1755 * @priv: file handle
1756 * @prio: ptr to v4l2_priority structure
1757 */
1758static int vpif_g_priority(struct file *file, void *priv,
1759 enum v4l2_priority *prio)
1760{
1761 struct vpif_fh *fh = priv;
1762 struct channel_obj *ch = fh->channel;
1763
1764 *prio = v4l2_prio_max(&ch->prio);
1765
1766 return 0;
1767}
1768
1769/**
1770 * vpif_s_priority() - set priority handler
1771 * @file: file ptr
1772 * @priv: file handle
1773 * @prio: ptr to v4l2_priority structure
1774 */
1775static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1776{
1777 struct vpif_fh *fh = priv;
1778 struct channel_obj *ch = fh->channel;
1779
1780 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1781}
1782
1783/**
1784 * vpif_cropcap() - cropcap handler
1785 * @file: file ptr
1786 * @priv: file handle
1787 * @crop: ptr to v4l2_cropcap structure
1788 */
1789static int vpif_cropcap(struct file *file, void *priv,
1790 struct v4l2_cropcap *crop)
1791{
1792 struct vpif_fh *fh = priv;
1793 struct channel_obj *ch = fh->channel;
1794 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1795
1796 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1797 return -EINVAL;
1798
1799 crop->bounds.left = 0;
1800 crop->bounds.top = 0;
1801 crop->bounds.height = common->height;
1802 crop->bounds.width = common->width;
1803 crop->defrect = crop->bounds;
1804 return 0;
1805}
1806
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001807/**
1808 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1809 * @file: file ptr
1810 * @priv: file handle
1811 * @preset: input preset
1812 */
1813static int vpif_enum_dv_presets(struct file *file, void *priv,
1814 struct v4l2_dv_enum_preset *preset)
1815{
1816 struct vpif_fh *fh = priv;
1817 struct channel_obj *ch = fh->channel;
1818
1819 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1820 video, enum_dv_presets, preset);
1821}
1822
1823/**
1824 * vpif_query_dv_presets() - QUERY_DV_PRESET handler
1825 * @file: file ptr
1826 * @priv: file handle
1827 * @preset: input preset
1828 */
1829static int vpif_query_dv_preset(struct file *file, void *priv,
1830 struct v4l2_dv_preset *preset)
1831{
1832 struct vpif_fh *fh = priv;
1833 struct channel_obj *ch = fh->channel;
1834
1835 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1836 video, query_dv_preset, preset);
1837}
1838/**
1839 * vpif_s_dv_presets() - S_DV_PRESETS handler
1840 * @file: file ptr
1841 * @priv: file handle
1842 * @preset: input preset
1843 */
1844static int vpif_s_dv_preset(struct file *file, void *priv,
1845 struct v4l2_dv_preset *preset)
1846{
1847 struct vpif_fh *fh = priv;
1848 struct channel_obj *ch = fh->channel;
1849 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1850 int ret = 0;
1851
1852 if (common->started) {
1853 vpif_dbg(1, debug, "streaming in progress\n");
1854 return -EBUSY;
1855 }
1856
1857 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1858 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1859 if (!fh->initialized) {
1860 vpif_dbg(1, debug, "Channel Busy\n");
1861 return -EBUSY;
1862 }
1863 }
1864
1865 ret = v4l2_prio_check(&ch->prio, fh->prio);
1866 if (ret)
1867 return ret;
1868
1869 fh->initialized = 1;
1870
1871 /* Call encoder subdevice function to set the standard */
1872 if (mutex_lock_interruptible(&common->lock))
1873 return -ERESTARTSYS;
1874
1875 ch->video.dv_preset = preset->preset;
1876 ch->video.stdid = V4L2_STD_UNKNOWN;
Mats Randgaardc027e162010-12-16 12:17:44 -03001877 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001878
1879 /* Get the information about the standard */
1880 if (vpif_update_std_info(ch)) {
1881 vpif_dbg(1, debug, "Error getting the standard info\n");
1882 ret = -EINVAL;
1883 } else {
1884 /* Configure the default format information */
1885 vpif_config_format(ch);
1886
1887 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1888 video, s_dv_preset, preset);
1889 }
1890
1891 mutex_unlock(&common->lock);
1892
1893 return ret;
1894}
1895/**
1896 * vpif_g_dv_presets() - G_DV_PRESETS handler
1897 * @file: file ptr
1898 * @priv: file handle
1899 * @preset: input preset
1900 */
1901static int vpif_g_dv_preset(struct file *file, void *priv,
1902 struct v4l2_dv_preset *preset)
1903{
1904 struct vpif_fh *fh = priv;
1905 struct channel_obj *ch = fh->channel;
1906
1907 preset->preset = ch->video.dv_preset;
1908
1909 return 0;
1910}
1911
Mats Randgaardc027e162010-12-16 12:17:44 -03001912/**
1913 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1914 * @file: file ptr
1915 * @priv: file handle
1916 * @timings: digital video timings
1917 */
1918static int vpif_s_dv_timings(struct file *file, void *priv,
1919 struct v4l2_dv_timings *timings)
1920{
1921 struct vpif_fh *fh = priv;
1922 struct channel_obj *ch = fh->channel;
1923 struct vpif_params *vpifparams = &ch->vpifparams;
1924 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1925 struct video_obj *vid_ch = &ch->video;
1926 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1927 int ret;
1928
1929 if (timings->type != V4L2_DV_BT_656_1120) {
1930 vpif_dbg(2, debug, "Timing type not defined\n");
1931 return -EINVAL;
1932 }
1933
1934 /* Configure subdevice timings, if any */
1935 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1936 video, s_dv_timings, timings);
1937 if (ret == -ENOIOCTLCMD) {
1938 vpif_dbg(2, debug, "Custom DV timings not supported by "
1939 "subdevice\n");
1940 return -EINVAL;
1941 }
1942 if (ret < 0) {
1943 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1944 return ret;
1945 }
1946
1947 if (!(timings->bt.width && timings->bt.height &&
1948 (timings->bt.hbackporch ||
1949 timings->bt.hfrontporch ||
1950 timings->bt.hsync) &&
1951 timings->bt.vfrontporch &&
1952 (timings->bt.vbackporch ||
1953 timings->bt.vsync))) {
1954 vpif_dbg(2, debug, "Timings for width, height, "
1955 "horizontal back porch, horizontal sync, "
1956 "horizontal front porch, vertical back porch, "
1957 "vertical sync and vertical back porch "
1958 "must be defined\n");
1959 return -EINVAL;
1960 }
1961
1962 *bt = timings->bt;
1963
1964 /* Configure video port timings */
1965
1966 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1967 bt->hsync - 8;
1968 std_info->sav2eav = bt->width;
1969
1970 std_info->l1 = 1;
1971 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1972
1973 if (bt->interlaced) {
1974 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1975 std_info->vsize = bt->height * 2 +
1976 bt->vfrontporch + bt->vsync + bt->vbackporch +
1977 bt->il_vfrontporch + bt->il_vsync +
1978 bt->il_vbackporch;
1979 std_info->l5 = std_info->vsize/2 -
1980 (bt->vfrontporch - 1);
1981 std_info->l7 = std_info->vsize/2 + 1;
1982 std_info->l9 = std_info->l7 + bt->il_vsync +
1983 bt->il_vbackporch + 1;
1984 std_info->l11 = std_info->vsize -
1985 (bt->il_vfrontporch - 1);
1986 } else {
1987 vpif_dbg(2, debug, "Required timing values for "
1988 "interlaced BT format missing\n");
1989 return -EINVAL;
1990 }
1991 } else {
1992 std_info->vsize = bt->height + bt->vfrontporch +
1993 bt->vsync + bt->vbackporch;
1994 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1995 }
1996 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1997 std_info->width = bt->width;
1998 std_info->height = bt->height;
1999 std_info->frm_fmt = bt->interlaced ? 0 : 1;
2000 std_info->ycmux_mode = 0;
2001 std_info->capture_format = 0;
2002 std_info->vbi_supported = 0;
2003 std_info->hd_sd = 1;
2004 std_info->stdid = 0;
2005 std_info->dv_preset = V4L2_DV_INVALID;
2006
2007 vid_ch->stdid = 0;
2008 vid_ch->dv_preset = V4L2_DV_INVALID;
2009 return 0;
2010}
2011
2012/**
2013 * vpif_g_dv_timings() - G_DV_TIMINGS handler
2014 * @file: file ptr
2015 * @priv: file handle
2016 * @timings: digital video timings
2017 */
2018static int vpif_g_dv_timings(struct file *file, void *priv,
2019 struct v4l2_dv_timings *timings)
2020{
2021 struct vpif_fh *fh = priv;
2022 struct channel_obj *ch = fh->channel;
2023 struct video_obj *vid_ch = &ch->video;
2024 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
2025
2026 timings->bt = *bt;
2027
2028 return 0;
2029}
2030
Mats Randgaard7036d6a2010-12-16 12:17:41 -03002031/*
2032 * vpif_g_chip_ident() - Identify the chip
2033 * @file: file ptr
2034 * @priv: file handle
2035 * @chip: chip identity
2036 *
2037 * Returns zero or -EINVAL if read operations fails.
2038 */
2039static int vpif_g_chip_ident(struct file *file, void *priv,
2040 struct v4l2_dbg_chip_ident *chip)
2041{
2042 chip->ident = V4L2_IDENT_NONE;
2043 chip->revision = 0;
2044 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
2045 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
2046 vpif_dbg(2, debug, "match_type is invalid.\n");
2047 return -EINVAL;
2048 }
2049
2050 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
2051 g_chip_ident, chip);
2052}
2053
2054#ifdef CONFIG_VIDEO_ADV_DEBUG
2055/*
2056 * vpif_dbg_g_register() - Read register
2057 * @file: file ptr
2058 * @priv: file handle
2059 * @reg: register to be read
2060 *
2061 * Debugging only
2062 * Returns zero or -EINVAL if read operations fails.
2063 */
2064static int vpif_dbg_g_register(struct file *file, void *priv,
2065 struct v4l2_dbg_register *reg){
2066 struct vpif_fh *fh = priv;
2067 struct channel_obj *ch = fh->channel;
2068
2069 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2070 g_register, reg);
2071}
2072
2073/*
2074 * vpif_dbg_s_register() - Write to register
2075 * @file: file ptr
2076 * @priv: file handle
2077 * @reg: register to be modified
2078 *
2079 * Debugging only
2080 * Returns zero or -EINVAL if write operations fails.
2081 */
2082static int vpif_dbg_s_register(struct file *file, void *priv,
2083 struct v4l2_dbg_register *reg){
2084 struct vpif_fh *fh = priv;
2085 struct channel_obj *ch = fh->channel;
2086
2087 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2088 s_register, reg);
2089}
2090#endif
2091
2092/*
2093 * vpif_log_status() - Status information
2094 * @file: file ptr
2095 * @priv: file handle
2096 *
2097 * Returns zero.
2098 */
2099static int vpif_log_status(struct file *filep, void *priv)
2100{
2101 /* status for sub devices */
2102 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
2103
2104 return 0;
2105}
2106
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002107/* vpif capture ioctl operations */
2108static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
2109 .vidioc_querycap = vpif_querycap,
2110 .vidioc_g_priority = vpif_g_priority,
2111 .vidioc_s_priority = vpif_s_priority,
2112 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
2113 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
2114 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
2115 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
2116 .vidioc_enum_input = vpif_enum_input,
2117 .vidioc_s_input = vpif_s_input,
2118 .vidioc_g_input = vpif_g_input,
2119 .vidioc_reqbufs = vpif_reqbufs,
2120 .vidioc_querybuf = vpif_querybuf,
2121 .vidioc_querystd = vpif_querystd,
2122 .vidioc_s_std = vpif_s_std,
2123 .vidioc_g_std = vpif_g_std,
2124 .vidioc_qbuf = vpif_qbuf,
2125 .vidioc_dqbuf = vpif_dqbuf,
2126 .vidioc_streamon = vpif_streamon,
2127 .vidioc_streamoff = vpif_streamoff,
2128 .vidioc_cropcap = vpif_cropcap,
Mats Randgaard40c8bce2010-12-16 12:17:43 -03002129 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
2130 .vidioc_s_dv_preset = vpif_s_dv_preset,
2131 .vidioc_g_dv_preset = vpif_g_dv_preset,
2132 .vidioc_query_dv_preset = vpif_query_dv_preset,
Mats Randgaardc027e162010-12-16 12:17:44 -03002133 .vidioc_s_dv_timings = vpif_s_dv_timings,
2134 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03002135 .vidioc_g_chip_ident = vpif_g_chip_ident,
2136#ifdef CONFIG_VIDEO_ADV_DEBUG
2137 .vidioc_g_register = vpif_dbg_g_register,
2138 .vidioc_s_register = vpif_dbg_s_register,
2139#endif
2140 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002141};
2142
2143/* vpif file operations */
2144static struct v4l2_file_operations vpif_fops = {
2145 .owner = THIS_MODULE,
2146 .open = vpif_open,
2147 .release = vpif_release,
2148 .ioctl = video_ioctl2,
2149 .mmap = vpif_mmap,
2150 .poll = vpif_poll
2151};
2152
2153/* vpif video template */
2154static struct video_device vpif_video_template = {
2155 .name = "vpif",
2156 .fops = &vpif_fops,
2157 .minor = -1,
2158 .ioctl_ops = &vpif_ioctl_ops,
2159};
2160
2161/**
2162 * initialize_vpif() - Initialize vpif data structures
2163 *
2164 * Allocate memory for data structures and initialize them
2165 */
2166static int initialize_vpif(void)
2167{
2168 int err = 0, i, j;
2169 int free_channel_objects_index;
2170
2171 /* Default number of buffers should be 3 */
2172 if ((ch0_numbuffers > 0) &&
2173 (ch0_numbuffers < config_params.min_numbuffers))
2174 ch0_numbuffers = config_params.min_numbuffers;
2175 if ((ch1_numbuffers > 0) &&
2176 (ch1_numbuffers < config_params.min_numbuffers))
2177 ch1_numbuffers = config_params.min_numbuffers;
2178
2179 /* Set buffer size to min buffers size if it is invalid */
2180 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2181 ch0_bufsize =
2182 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2183 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2184 ch1_bufsize =
2185 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2186
2187 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2188 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2189 if (ch0_numbuffers) {
2190 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2191 = ch0_bufsize;
2192 }
2193 if (ch1_numbuffers) {
2194 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2195 = ch1_bufsize;
2196 }
2197
2198 /* Allocate memory for six channel objects */
2199 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2200 vpif_obj.dev[i] =
2201 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2202 /* If memory allocation fails, return error */
2203 if (!vpif_obj.dev[i]) {
2204 free_channel_objects_index = i;
2205 err = -ENOMEM;
2206 goto vpif_init_free_channel_objects;
2207 }
2208 }
2209 return 0;
2210
2211vpif_init_free_channel_objects:
2212 for (j = 0; j < free_channel_objects_index; j++)
2213 kfree(vpif_obj.dev[j]);
2214 return err;
2215}
2216
2217/**
2218 * vpif_probe : This function probes the vpif capture driver
2219 * @pdev: platform device pointer
2220 *
2221 * This creates device entries by register itself to the V4L2 driver and
2222 * initializes fields of each channel objects
2223 */
2224static __init int vpif_probe(struct platform_device *pdev)
2225{
2226 struct vpif_subdev_info *subdevdata;
2227 struct vpif_capture_config *config;
2228 int i, j, k, m, q, err;
2229 struct i2c_adapter *i2c_adap;
2230 struct channel_obj *ch;
2231 struct common_obj *common;
2232 struct video_device *vfd;
2233 struct resource *res;
2234 int subdev_count;
2235
2236 vpif_dev = &pdev->dev;
2237
2238 err = initialize_vpif();
2239 if (err) {
2240 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2241 return err;
2242 }
2243
2244 k = 0;
2245 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
2246 for (i = res->start; i <= res->end; i++) {
2247 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
2248 "DM646x_Capture",
2249 (void *)(&vpif_obj.dev[k]->channel_id))) {
2250 err = -EBUSY;
2251 i--;
2252 goto vpif_int_err;
2253 }
2254 }
2255 k++;
2256 }
2257
2258 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2259 /* Get the pointer to the channel object */
2260 ch = vpif_obj.dev[i];
2261 /* Allocate memory for video device */
2262 vfd = video_device_alloc();
2263 if (NULL == vfd) {
2264 for (j = 0; j < i; j++) {
2265 ch = vpif_obj.dev[j];
2266 video_device_release(ch->video_dev);
2267 }
2268 err = -ENOMEM;
2269 goto vpif_dev_alloc_err;
2270 }
2271
2272 /* Initialize field of video device */
2273 *vfd = vpif_video_template;
2274 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2275 vfd->release = video_device_release;
2276 snprintf(vfd->name, sizeof(vfd->name),
2277 "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
2278 (VPIF_CAPTURE_VERSION_CODE >> 16) & 0xff,
2279 (VPIF_CAPTURE_VERSION_CODE >> 8) & 0xff,
2280 (VPIF_CAPTURE_VERSION_CODE) & 0xff);
2281 /* Set video_dev to the video device */
2282 ch->video_dev = vfd;
2283 }
2284
2285 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2286 ch = vpif_obj.dev[j];
2287 ch->channel_id = j;
2288 common = &(ch->common[VPIF_VIDEO_INDEX]);
2289 spin_lock_init(&common->irqlock);
2290 mutex_init(&common->lock);
2291 /* Initialize prio member of channel object */
2292 v4l2_prio_init(&ch->prio);
2293 err = video_register_device(ch->video_dev,
2294 VFL_TYPE_GRABBER, (j ? 1 : 0));
2295 if (err)
2296 goto probe_out;
2297
2298 video_set_drvdata(ch->video_dev, ch);
2299
2300 }
2301
2302 i2c_adap = i2c_get_adapter(1);
2303 config = pdev->dev.platform_data;
2304
2305 subdev_count = config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002306 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002307 GFP_KERNEL);
2308 if (vpif_obj.sd == NULL) {
2309 vpif_err("unable to allocate memory for subdevice pointers\n");
2310 err = -ENOMEM;
2311 goto probe_out;
2312 }
2313
2314 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2315 if (err) {
2316 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2317 goto probe_subdev_out;
2318 }
2319
2320 for (i = 0; i < subdev_count; i++) {
2321 subdevdata = &config->subdev_info[i];
2322 vpif_obj.sd[i] =
2323 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2324 i2c_adap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002325 &subdevdata->board_info,
2326 NULL);
2327
2328 if (!vpif_obj.sd[i]) {
2329 vpif_err("Error registering v4l2 subdevice\n");
2330 goto probe_subdev_out;
2331 }
2332 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2333 subdevdata->name);
2334
2335 if (vpif_obj.sd[i])
2336 vpif_obj.sd[i]->grp_id = 1 << i;
2337 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002338
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03002339 v4l2_info(&vpif_obj.v4l2_dev,
2340 "DM646x VPIF capture driver initialized\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002341 return 0;
2342
2343probe_subdev_out:
2344 /* free sub devices memory */
2345 kfree(vpif_obj.sd);
2346
2347 j = VPIF_CAPTURE_MAX_DEVICES;
2348probe_out:
2349 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2350 for (k = 0; k < j; k++) {
2351 /* Get the pointer to the channel object */
2352 ch = vpif_obj.dev[k];
2353 /* Unregister video device */
2354 video_unregister_device(ch->video_dev);
2355 }
2356
2357vpif_dev_alloc_err:
2358 k = VPIF_CAPTURE_MAX_DEVICES-1;
2359 res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2360 i = res->end;
2361
2362vpif_int_err:
2363 for (q = k; q >= 0; q--) {
2364 for (m = i; m >= (int)res->start; m--)
2365 free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2366
2367 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2368 if (res)
2369 i = res->end;
2370 }
2371 return err;
2372}
2373
2374/**
2375 * vpif_remove() - driver remove handler
2376 * @device: ptr to platform device structure
2377 *
2378 * The vidoe device is unregistered
2379 */
2380static int vpif_remove(struct platform_device *device)
2381{
2382 int i;
2383 struct channel_obj *ch;
2384
2385 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2386
2387 /* un-register device */
2388 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2389 /* Get the pointer to the channel object */
2390 ch = vpif_obj.dev[i];
2391 /* Unregister video device */
2392 video_unregister_device(ch->video_dev);
2393 }
2394 return 0;
2395}
2396
2397/**
2398 * vpif_suspend: vpif device suspend
2399 *
2400 * TODO: Add suspend code here
2401 */
2402static int
2403vpif_suspend(struct device *dev)
2404{
2405 return -1;
2406}
2407
2408/**
2409 * vpif_resume: vpif device suspend
2410 *
2411 * TODO: Add resume code here
2412 */
2413static int
2414vpif_resume(struct device *dev)
2415{
2416 return -1;
2417}
2418
Alexey Dobriyan47145212009-12-14 18:00:08 -08002419static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002420 .suspend = vpif_suspend,
2421 .resume = vpif_resume,
2422};
2423
Mats Randgaardffa1b392010-08-30 10:30:36 -03002424static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002425 .driver = {
2426 .name = "vpif_capture",
2427 .owner = THIS_MODULE,
2428 .pm = &vpif_dev_pm_ops,
2429 },
2430 .probe = vpif_probe,
2431 .remove = vpif_remove,
2432};
2433
2434/**
2435 * vpif_init: initialize the vpif driver
2436 *
2437 * This function registers device and driver to the kernel, requests irq
2438 * handler and allocates memory
2439 * for channel objects
2440 */
2441static __init int vpif_init(void)
2442{
2443 return platform_driver_register(&vpif_driver);
2444}
2445
2446/**
2447 * vpif_cleanup : This function clean up the vpif capture resources
2448 *
2449 * This will un-registers device and driver to the kernel, frees
2450 * requested irq handler and de-allocates memory allocated for channel
2451 * objects.
2452 */
2453static void vpif_cleanup(void)
2454{
2455 struct platform_device *pdev;
2456 struct resource *res;
2457 int irq_num;
2458 int i = 0;
2459
2460 pdev = container_of(vpif_dev, struct platform_device, dev);
2461 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2462 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2463 free_irq(irq_num,
2464 (void *)(&vpif_obj.dev[i]->channel_id));
2465 i++;
2466 }
2467
2468 platform_driver_unregister(&vpif_driver);
2469
2470 kfree(vpif_obj.sd);
2471 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2472 kfree(vpif_obj.dev[i]);
2473}
2474
2475/* Function for module initialization and cleanup */
2476module_init(vpif_init);
2477module_exit(vpif_cleanup);