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