blob: 42f1cd60780b5d90611f0a02a0d752d291e46698 [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;
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
1694 vpif_dbg(2, debug, "VIDIOC_S_FMT\n");
1695
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;
1877
1878 /* Get the information about the standard */
1879 if (vpif_update_std_info(ch)) {
1880 vpif_dbg(1, debug, "Error getting the standard info\n");
1881 ret = -EINVAL;
1882 } else {
1883 /* Configure the default format information */
1884 vpif_config_format(ch);
1885
1886 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1887 video, s_dv_preset, preset);
1888 }
1889
1890 mutex_unlock(&common->lock);
1891
1892 return ret;
1893}
1894/**
1895 * vpif_g_dv_presets() - G_DV_PRESETS handler
1896 * @file: file ptr
1897 * @priv: file handle
1898 * @preset: input preset
1899 */
1900static int vpif_g_dv_preset(struct file *file, void *priv,
1901 struct v4l2_dv_preset *preset)
1902{
1903 struct vpif_fh *fh = priv;
1904 struct channel_obj *ch = fh->channel;
1905
1906 preset->preset = ch->video.dv_preset;
1907
1908 return 0;
1909}
1910
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001911/*
1912 * vpif_g_chip_ident() - Identify the chip
1913 * @file: file ptr
1914 * @priv: file handle
1915 * @chip: chip identity
1916 *
1917 * Returns zero or -EINVAL if read operations fails.
1918 */
1919static int vpif_g_chip_ident(struct file *file, void *priv,
1920 struct v4l2_dbg_chip_ident *chip)
1921{
1922 chip->ident = V4L2_IDENT_NONE;
1923 chip->revision = 0;
1924 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1925 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1926 vpif_dbg(2, debug, "match_type is invalid.\n");
1927 return -EINVAL;
1928 }
1929
1930 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1931 g_chip_ident, chip);
1932}
1933
1934#ifdef CONFIG_VIDEO_ADV_DEBUG
1935/*
1936 * vpif_dbg_g_register() - Read register
1937 * @file: file ptr
1938 * @priv: file handle
1939 * @reg: register to be read
1940 *
1941 * Debugging only
1942 * Returns zero or -EINVAL if read operations fails.
1943 */
1944static int vpif_dbg_g_register(struct file *file, void *priv,
1945 struct v4l2_dbg_register *reg){
1946 struct vpif_fh *fh = priv;
1947 struct channel_obj *ch = fh->channel;
1948
1949 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1950 g_register, reg);
1951}
1952
1953/*
1954 * vpif_dbg_s_register() - Write to register
1955 * @file: file ptr
1956 * @priv: file handle
1957 * @reg: register to be modified
1958 *
1959 * Debugging only
1960 * Returns zero or -EINVAL if write operations fails.
1961 */
1962static int vpif_dbg_s_register(struct file *file, void *priv,
1963 struct v4l2_dbg_register *reg){
1964 struct vpif_fh *fh = priv;
1965 struct channel_obj *ch = fh->channel;
1966
1967 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1968 s_register, reg);
1969}
1970#endif
1971
1972/*
1973 * vpif_log_status() - Status information
1974 * @file: file ptr
1975 * @priv: file handle
1976 *
1977 * Returns zero.
1978 */
1979static int vpif_log_status(struct file *filep, void *priv)
1980{
1981 /* status for sub devices */
1982 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1983
1984 return 0;
1985}
1986
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001987/* vpif capture ioctl operations */
1988static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1989 .vidioc_querycap = vpif_querycap,
1990 .vidioc_g_priority = vpif_g_priority,
1991 .vidioc_s_priority = vpif_s_priority,
1992 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1993 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1994 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1995 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1996 .vidioc_enum_input = vpif_enum_input,
1997 .vidioc_s_input = vpif_s_input,
1998 .vidioc_g_input = vpif_g_input,
1999 .vidioc_reqbufs = vpif_reqbufs,
2000 .vidioc_querybuf = vpif_querybuf,
2001 .vidioc_querystd = vpif_querystd,
2002 .vidioc_s_std = vpif_s_std,
2003 .vidioc_g_std = vpif_g_std,
2004 .vidioc_qbuf = vpif_qbuf,
2005 .vidioc_dqbuf = vpif_dqbuf,
2006 .vidioc_streamon = vpif_streamon,
2007 .vidioc_streamoff = vpif_streamoff,
2008 .vidioc_cropcap = vpif_cropcap,
Mats Randgaard40c8bce2010-12-16 12:17:43 -03002009 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
2010 .vidioc_s_dv_preset = vpif_s_dv_preset,
2011 .vidioc_g_dv_preset = vpif_g_dv_preset,
2012 .vidioc_query_dv_preset = vpif_query_dv_preset,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03002013 .vidioc_g_chip_ident = vpif_g_chip_ident,
2014#ifdef CONFIG_VIDEO_ADV_DEBUG
2015 .vidioc_g_register = vpif_dbg_g_register,
2016 .vidioc_s_register = vpif_dbg_s_register,
2017#endif
2018 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002019};
2020
2021/* vpif file operations */
2022static struct v4l2_file_operations vpif_fops = {
2023 .owner = THIS_MODULE,
2024 .open = vpif_open,
2025 .release = vpif_release,
2026 .ioctl = video_ioctl2,
2027 .mmap = vpif_mmap,
2028 .poll = vpif_poll
2029};
2030
2031/* vpif video template */
2032static struct video_device vpif_video_template = {
2033 .name = "vpif",
2034 .fops = &vpif_fops,
2035 .minor = -1,
2036 .ioctl_ops = &vpif_ioctl_ops,
2037};
2038
2039/**
2040 * initialize_vpif() - Initialize vpif data structures
2041 *
2042 * Allocate memory for data structures and initialize them
2043 */
2044static int initialize_vpif(void)
2045{
2046 int err = 0, i, j;
2047 int free_channel_objects_index;
2048
2049 /* Default number of buffers should be 3 */
2050 if ((ch0_numbuffers > 0) &&
2051 (ch0_numbuffers < config_params.min_numbuffers))
2052 ch0_numbuffers = config_params.min_numbuffers;
2053 if ((ch1_numbuffers > 0) &&
2054 (ch1_numbuffers < config_params.min_numbuffers))
2055 ch1_numbuffers = config_params.min_numbuffers;
2056
2057 /* Set buffer size to min buffers size if it is invalid */
2058 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2059 ch0_bufsize =
2060 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2061 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2062 ch1_bufsize =
2063 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2064
2065 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2066 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2067 if (ch0_numbuffers) {
2068 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2069 = ch0_bufsize;
2070 }
2071 if (ch1_numbuffers) {
2072 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2073 = ch1_bufsize;
2074 }
2075
2076 /* Allocate memory for six channel objects */
2077 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2078 vpif_obj.dev[i] =
2079 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2080 /* If memory allocation fails, return error */
2081 if (!vpif_obj.dev[i]) {
2082 free_channel_objects_index = i;
2083 err = -ENOMEM;
2084 goto vpif_init_free_channel_objects;
2085 }
2086 }
2087 return 0;
2088
2089vpif_init_free_channel_objects:
2090 for (j = 0; j < free_channel_objects_index; j++)
2091 kfree(vpif_obj.dev[j]);
2092 return err;
2093}
2094
2095/**
2096 * vpif_probe : This function probes the vpif capture driver
2097 * @pdev: platform device pointer
2098 *
2099 * This creates device entries by register itself to the V4L2 driver and
2100 * initializes fields of each channel objects
2101 */
2102static __init int vpif_probe(struct platform_device *pdev)
2103{
2104 struct vpif_subdev_info *subdevdata;
2105 struct vpif_capture_config *config;
2106 int i, j, k, m, q, err;
2107 struct i2c_adapter *i2c_adap;
2108 struct channel_obj *ch;
2109 struct common_obj *common;
2110 struct video_device *vfd;
2111 struct resource *res;
2112 int subdev_count;
2113
2114 vpif_dev = &pdev->dev;
2115
2116 err = initialize_vpif();
2117 if (err) {
2118 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2119 return err;
2120 }
2121
2122 k = 0;
2123 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
2124 for (i = res->start; i <= res->end; i++) {
2125 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
2126 "DM646x_Capture",
2127 (void *)(&vpif_obj.dev[k]->channel_id))) {
2128 err = -EBUSY;
2129 i--;
2130 goto vpif_int_err;
2131 }
2132 }
2133 k++;
2134 }
2135
2136 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2137 /* Get the pointer to the channel object */
2138 ch = vpif_obj.dev[i];
2139 /* Allocate memory for video device */
2140 vfd = video_device_alloc();
2141 if (NULL == vfd) {
2142 for (j = 0; j < i; j++) {
2143 ch = vpif_obj.dev[j];
2144 video_device_release(ch->video_dev);
2145 }
2146 err = -ENOMEM;
2147 goto vpif_dev_alloc_err;
2148 }
2149
2150 /* Initialize field of video device */
2151 *vfd = vpif_video_template;
2152 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2153 vfd->release = video_device_release;
2154 snprintf(vfd->name, sizeof(vfd->name),
2155 "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
2156 (VPIF_CAPTURE_VERSION_CODE >> 16) & 0xff,
2157 (VPIF_CAPTURE_VERSION_CODE >> 8) & 0xff,
2158 (VPIF_CAPTURE_VERSION_CODE) & 0xff);
2159 /* Set video_dev to the video device */
2160 ch->video_dev = vfd;
2161 }
2162
2163 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2164 ch = vpif_obj.dev[j];
2165 ch->channel_id = j;
2166 common = &(ch->common[VPIF_VIDEO_INDEX]);
2167 spin_lock_init(&common->irqlock);
2168 mutex_init(&common->lock);
2169 /* Initialize prio member of channel object */
2170 v4l2_prio_init(&ch->prio);
2171 err = video_register_device(ch->video_dev,
2172 VFL_TYPE_GRABBER, (j ? 1 : 0));
2173 if (err)
2174 goto probe_out;
2175
2176 video_set_drvdata(ch->video_dev, ch);
2177
2178 }
2179
2180 i2c_adap = i2c_get_adapter(1);
2181 config = pdev->dev.platform_data;
2182
2183 subdev_count = config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002184 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002185 GFP_KERNEL);
2186 if (vpif_obj.sd == NULL) {
2187 vpif_err("unable to allocate memory for subdevice pointers\n");
2188 err = -ENOMEM;
2189 goto probe_out;
2190 }
2191
2192 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2193 if (err) {
2194 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2195 goto probe_subdev_out;
2196 }
2197
2198 for (i = 0; i < subdev_count; i++) {
2199 subdevdata = &config->subdev_info[i];
2200 vpif_obj.sd[i] =
2201 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2202 i2c_adap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002203 &subdevdata->board_info,
2204 NULL);
2205
2206 if (!vpif_obj.sd[i]) {
2207 vpif_err("Error registering v4l2 subdevice\n");
2208 goto probe_subdev_out;
2209 }
2210 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2211 subdevdata->name);
2212
2213 if (vpif_obj.sd[i])
2214 vpif_obj.sd[i]->grp_id = 1 << i;
2215 }
2216 v4l2_info(&vpif_obj.v4l2_dev, "DM646x VPIF Capture driver"
2217 " initialized\n");
2218
2219 return 0;
2220
2221probe_subdev_out:
2222 /* free sub devices memory */
2223 kfree(vpif_obj.sd);
2224
2225 j = VPIF_CAPTURE_MAX_DEVICES;
2226probe_out:
2227 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2228 for (k = 0; k < j; k++) {
2229 /* Get the pointer to the channel object */
2230 ch = vpif_obj.dev[k];
2231 /* Unregister video device */
2232 video_unregister_device(ch->video_dev);
2233 }
2234
2235vpif_dev_alloc_err:
2236 k = VPIF_CAPTURE_MAX_DEVICES-1;
2237 res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2238 i = res->end;
2239
2240vpif_int_err:
2241 for (q = k; q >= 0; q--) {
2242 for (m = i; m >= (int)res->start; m--)
2243 free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2244
2245 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2246 if (res)
2247 i = res->end;
2248 }
2249 return err;
2250}
2251
2252/**
2253 * vpif_remove() - driver remove handler
2254 * @device: ptr to platform device structure
2255 *
2256 * The vidoe device is unregistered
2257 */
2258static int vpif_remove(struct platform_device *device)
2259{
2260 int i;
2261 struct channel_obj *ch;
2262
2263 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2264
2265 /* un-register device */
2266 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2267 /* Get the pointer to the channel object */
2268 ch = vpif_obj.dev[i];
2269 /* Unregister video device */
2270 video_unregister_device(ch->video_dev);
2271 }
2272 return 0;
2273}
2274
2275/**
2276 * vpif_suspend: vpif device suspend
2277 *
2278 * TODO: Add suspend code here
2279 */
2280static int
2281vpif_suspend(struct device *dev)
2282{
2283 return -1;
2284}
2285
2286/**
2287 * vpif_resume: vpif device suspend
2288 *
2289 * TODO: Add resume code here
2290 */
2291static int
2292vpif_resume(struct device *dev)
2293{
2294 return -1;
2295}
2296
Alexey Dobriyan47145212009-12-14 18:00:08 -08002297static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002298 .suspend = vpif_suspend,
2299 .resume = vpif_resume,
2300};
2301
Mats Randgaardffa1b392010-08-30 10:30:36 -03002302static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002303 .driver = {
2304 .name = "vpif_capture",
2305 .owner = THIS_MODULE,
2306 .pm = &vpif_dev_pm_ops,
2307 },
2308 .probe = vpif_probe,
2309 .remove = vpif_remove,
2310};
2311
2312/**
2313 * vpif_init: initialize the vpif driver
2314 *
2315 * This function registers device and driver to the kernel, requests irq
2316 * handler and allocates memory
2317 * for channel objects
2318 */
2319static __init int vpif_init(void)
2320{
2321 return platform_driver_register(&vpif_driver);
2322}
2323
2324/**
2325 * vpif_cleanup : This function clean up the vpif capture resources
2326 *
2327 * This will un-registers device and driver to the kernel, frees
2328 * requested irq handler and de-allocates memory allocated for channel
2329 * objects.
2330 */
2331static void vpif_cleanup(void)
2332{
2333 struct platform_device *pdev;
2334 struct resource *res;
2335 int irq_num;
2336 int i = 0;
2337
2338 pdev = container_of(vpif_dev, struct platform_device, dev);
2339 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2340 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2341 free_irq(irq_num,
2342 (void *)(&vpif_obj.dev[i]->channel_id));
2343 i++;
2344 }
2345
2346 platform_driver_unregister(&vpif_driver);
2347
2348 kfree(vpif_obj.sd);
2349 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2350 kfree(vpif_obj.dev[i]);
2351}
2352
2353/* Function for module initialization and cleanup */
2354module_init(vpif_init);
2355module_exit(vpif_cleanup);