blob: 9de0062eca592d4df713a2fe42ad9eb8f978f4c4 [file] [log] [blame]
Chaithrika U Se7332e32009-06-09 05:55:37 -03001/*
2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
4 *
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
10 *
11 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/mm.h>
23#include <linux/interrupt.h>
24#include <linux/workqueue.h>
25#include <linux/string.h>
26#include <linux/videodev2.h>
27#include <linux/wait.h>
28#include <linux/time.h>
29#include <linux/i2c.h>
30#include <linux/platform_device.h>
31#include <linux/io.h>
32#include <linux/version.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030034
35#include <asm/irq.h>
36#include <asm/page.h>
37
38#include <media/adv7343.h>
39#include <media/v4l2-device.h>
40#include <media/v4l2-ioctl.h>
Mats Randgaard7036d6a2010-12-16 12:17:41 -030041#include <media/v4l2-chip-ident.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030042
43#include <mach/dm646x.h>
44
45#include "vpif_display.h"
46#include "vpif.h"
47
48MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
49MODULE_LICENSE("GPL");
50
51#define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
52
53#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
54#define vpif_dbg(level, debug, fmt, arg...) \
55 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
56
57static int debug = 1;
58static u32 ch2_numbuffers = 3;
59static u32 ch3_numbuffers = 3;
60static u32 ch2_bufsize = 1920 * 1080 * 2;
61static u32 ch3_bufsize = 720 * 576 * 2;
62
63module_param(debug, int, 0644);
64module_param(ch2_numbuffers, uint, S_IRUGO);
65module_param(ch3_numbuffers, uint, S_IRUGO);
66module_param(ch2_bufsize, uint, S_IRUGO);
67module_param(ch3_bufsize, uint, S_IRUGO);
68
69MODULE_PARM_DESC(debug, "Debug level 0-1");
70MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
71MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
72MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
73MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
74
75static struct vpif_config_params config_params = {
76 .min_numbuffers = 3,
77 .numbuffers[0] = 3,
78 .numbuffers[1] = 3,
79 .min_bufsize[0] = 720 * 480 * 2,
80 .min_bufsize[1] = 720 * 480 * 2,
81 .channel_bufsize[0] = 1920 * 1080 * 2,
82 .channel_bufsize[1] = 720 * 576 * 2,
83};
84
85static struct vpif_device vpif_obj = { {NULL} };
86static struct device *vpif_dev;
87
Chaithrika U Se7332e32009-06-09 05:55:37 -030088/*
89 * vpif_uservirt_to_phys: This function is used to convert user
90 * space virtual address to physical address.
91 */
92static u32 vpif_uservirt_to_phys(u32 virtp)
93{
94 struct mm_struct *mm = current->mm;
95 unsigned long physp = 0;
96 struct vm_area_struct *vma;
97
98 vma = find_vma(mm, virtp);
99
100 /* For kernel direct-mapped memory, take the easy way */
101 if (virtp >= PAGE_OFFSET) {
102 physp = virt_to_phys((void *)virtp);
103 } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
104 /* this will catch, kernel-allocated, mmaped-to-usermode addr */
105 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
106 } else {
107 /* otherwise, use get_user_pages() for general userland pages */
108 int res, nr_pages = 1;
109 struct page *pages;
110 down_read(&current->mm->mmap_sem);
111
112 res = get_user_pages(current, current->mm,
113 virtp, nr_pages, 1, 0, &pages, NULL);
114 up_read(&current->mm->mmap_sem);
115
116 if (res == nr_pages) {
117 physp = __pa(page_address(&pages[0]) +
118 (virtp & ~PAGE_MASK));
119 } else {
120 vpif_err("get_user_pages failed\n");
121 return 0;
122 }
123 }
124
125 return physp;
126}
127
128/*
129 * buffer_prepare: This is the callback function called from videobuf_qbuf()
130 * function the buffer is prepared and user space virtual address is converted
131 * into physical address
132 */
133static int vpif_buffer_prepare(struct videobuf_queue *q,
134 struct videobuf_buffer *vb,
135 enum v4l2_field field)
136{
137 struct vpif_fh *fh = q->priv_data;
138 struct common_obj *common;
139 unsigned long addr;
140
141 common = &fh->channel->common[VPIF_VIDEO_INDEX];
142 if (VIDEOBUF_NEEDS_INIT == vb->state) {
143 vb->width = common->width;
144 vb->height = common->height;
145 vb->size = vb->width * vb->height;
146 vb->field = field;
147 }
148 vb->state = VIDEOBUF_PREPARED;
149
150 /* if user pointer memory mechanism is used, get the physical
151 * address of the buffer */
152 if (V4L2_MEMORY_USERPTR == common->memory) {
153 if (!vb->baddr) {
154 vpif_err("buffer_address is 0\n");
155 return -EINVAL;
156 }
157
158 vb->boff = vpif_uservirt_to_phys(vb->baddr);
159 if (!ISALIGNED(vb->boff))
160 goto buf_align_exit;
161 }
162
163 addr = vb->boff;
164 if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
165 if (!ISALIGNED(addr + common->ytop_off) ||
166 !ISALIGNED(addr + common->ybtm_off) ||
167 !ISALIGNED(addr + common->ctop_off) ||
168 !ISALIGNED(addr + common->cbtm_off))
169 goto buf_align_exit;
170 }
171 return 0;
172
173buf_align_exit:
174 vpif_err("buffer offset not aligned to 8 bytes\n");
175 return -EINVAL;
176}
177
178/*
179 * vpif_buffer_setup: This function allocates memory for the buffers
180 */
181static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
182 unsigned int *size)
183{
184 struct vpif_fh *fh = q->priv_data;
185 struct channel_obj *ch = fh->channel;
186 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
187
188 if (V4L2_MEMORY_MMAP != common->memory)
189 return 0;
190
191 *size = config_params.channel_bufsize[ch->channel_id];
192 if (*count < config_params.min_numbuffers)
193 *count = config_params.min_numbuffers;
194
195 return 0;
196}
197
198/*
199 * vpif_buffer_queue: This function adds the buffer to DMA queue
200 */
201static void vpif_buffer_queue(struct videobuf_queue *q,
202 struct videobuf_buffer *vb)
203{
204 struct vpif_fh *fh = q->priv_data;
205 struct common_obj *common;
206
207 common = &fh->channel->common[VPIF_VIDEO_INDEX];
208
209 /* add the buffer to the DMA queue */
210 list_add_tail(&vb->queue, &common->dma_queue);
211 vb->state = VIDEOBUF_QUEUED;
212}
213
214/*
215 * vpif_buffer_release: This function is called from the videobuf layer to
216 * free memory allocated to the buffers
217 */
218static void vpif_buffer_release(struct videobuf_queue *q,
219 struct videobuf_buffer *vb)
220{
221 struct vpif_fh *fh = q->priv_data;
222 struct channel_obj *ch = fh->channel;
223 struct common_obj *common;
224 unsigned int buf_size = 0;
225
226 common = &ch->common[VPIF_VIDEO_INDEX];
227
228 videobuf_dma_contig_free(q, vb);
229 vb->state = VIDEOBUF_NEEDS_INIT;
230
231 if (V4L2_MEMORY_MMAP != common->memory)
232 return;
233
234 buf_size = config_params.channel_bufsize[ch->channel_id];
235}
236
237static struct videobuf_queue_ops video_qops = {
238 .buf_setup = vpif_buffer_setup,
239 .buf_prepare = vpif_buffer_prepare,
240 .buf_queue = vpif_buffer_queue,
241 .buf_release = vpif_buffer_release,
242};
243static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
244
245static void process_progressive_mode(struct common_obj *common)
246{
247 unsigned long addr = 0;
248
249 /* Get the next buffer from buffer queue */
250 common->next_frm = list_entry(common->dma_queue.next,
251 struct videobuf_buffer, queue);
252 /* Remove that buffer from the buffer queue */
253 list_del(&common->next_frm->queue);
254 /* Mark status of the buffer as active */
255 common->next_frm->state = VIDEOBUF_ACTIVE;
256
257 /* Set top and bottom field addrs in VPIF registers */
258 addr = videobuf_to_dma_contig(common->next_frm);
259 common->set_addr(addr + common->ytop_off,
260 addr + common->ybtm_off,
261 addr + common->ctop_off,
262 addr + common->cbtm_off);
263}
264
265static void process_interlaced_mode(int fid, struct common_obj *common)
266{
267 /* device field id and local field id are in sync */
268 /* If this is even field */
269 if (0 == fid) {
270 if (common->cur_frm == common->next_frm)
271 return;
272
273 /* one frame is displayed If next frame is
274 * available, release cur_frm and move on */
275 /* Copy frame display time */
276 do_gettimeofday(&common->cur_frm->ts);
277 /* Change status of the cur_frm */
278 common->cur_frm->state = VIDEOBUF_DONE;
279 /* unlock semaphore on cur_frm */
280 wake_up_interruptible(&common->cur_frm->done);
281 /* Make cur_frm pointing to next_frm */
282 common->cur_frm = common->next_frm;
283
284 } else if (1 == fid) { /* odd field */
285 if (list_empty(&common->dma_queue)
286 || (common->cur_frm != common->next_frm)) {
287 return;
288 }
289 /* one field is displayed configure the next
290 * frame if it is available else hold on current
291 * frame */
292 /* Get next from the buffer queue */
293 process_progressive_mode(common);
294
295 }
296}
297
298/*
299 * vpif_channel_isr: It changes status of the displayed buffer, takes next
300 * buffer from the queue and sets its address in VPIF registers
301 */
302static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
303{
304 struct vpif_device *dev = &vpif_obj;
305 struct channel_obj *ch;
306 struct common_obj *common;
307 enum v4l2_field field;
308 int fid = -1, i;
309 int channel_id = 0;
310
311 channel_id = *(int *)(dev_id);
312 ch = dev->dev[channel_id];
313 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
314 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
315 common = &ch->common[i];
316 /* If streaming is started in this channel */
317 if (0 == common->started)
318 continue;
319
320 if (1 == ch->vpifparams.std_info.frm_fmt) {
321 if (list_empty(&common->dma_queue))
322 continue;
323
324 /* Progressive mode */
325 if (!channel_first_int[i][channel_id]) {
326 /* Mark status of the cur_frm to
327 * done and unlock semaphore on it */
328 do_gettimeofday(&common->cur_frm->ts);
329 common->cur_frm->state = VIDEOBUF_DONE;
330 wake_up_interruptible(&common->cur_frm->done);
331 /* Make cur_frm pointing to next_frm */
332 common->cur_frm = common->next_frm;
333 }
334
335 channel_first_int[i][channel_id] = 0;
336 process_progressive_mode(common);
337 } else {
338 /* Interlaced mode */
339 /* If it is first interrupt, ignore it */
340
341 if (channel_first_int[i][channel_id]) {
342 channel_first_int[i][channel_id] = 0;
343 continue;
344 }
345
346 if (0 == i) {
347 ch->field_id ^= 1;
348 /* Get field id from VPIF registers */
349 fid = vpif_channel_getfid(ch->channel_id + 2);
350 /* If fid does not match with stored field id */
351 if (fid != ch->field_id) {
352 /* Make them in sync */
353 if (0 == fid)
354 ch->field_id = fid;
355
356 return IRQ_HANDLED;
357 }
358 }
359 process_interlaced_mode(fid, common);
360 }
361 }
362
363 return IRQ_HANDLED;
364}
365
366static int vpif_get_std_info(struct channel_obj *ch)
367{
368 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
369 struct video_obj *vid_ch = &ch->video;
370 struct vpif_params *vpifparams = &ch->vpifparams;
371 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
372 const struct vpif_channel_config_params *config;
373
374 int index;
375
376 std_info->stdid = vid_ch->stdid;
Julia Lawall728385c2010-03-12 06:15:32 -0300377 if (!std_info->stdid)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300378 return -1;
379
Mats Randgaardaa444402010-12-16 12:17:42 -0300380 for (index = 0; index < vpif_ch_params_count; index++) {
Chaithrika U Se7332e32009-06-09 05:55:37 -0300381 config = &ch_params[index];
382 if (config->stdid & std_info->stdid) {
383 memcpy(std_info, config, sizeof(*config));
384 break;
385 }
386 }
387
Mats Randgaardaa444402010-12-16 12:17:42 -0300388 if (index == vpif_ch_params_count)
389 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300390
391 common->fmt.fmt.pix.width = std_info->width;
392 common->fmt.fmt.pix.height = std_info->height;
393 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
394 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
395
396 /* Set height and width paramateres */
397 ch->common[VPIF_VIDEO_INDEX].height = std_info->height;
398 ch->common[VPIF_VIDEO_INDEX].width = std_info->width;
399
400 return 0;
401}
402
403/*
404 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
405 * in the top and bottom field
406 */
407static void vpif_calculate_offsets(struct channel_obj *ch)
408{
409 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
410 struct vpif_params *vpifparams = &ch->vpifparams;
411 enum v4l2_field field = common->fmt.fmt.pix.field;
412 struct video_obj *vid_ch = &ch->video;
413 unsigned int hpitch, vpitch, sizeimage;
414
415 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
416 if (ch->vpifparams.std_info.frm_fmt)
417 vid_ch->buf_field = V4L2_FIELD_NONE;
418 else
419 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
420 } else {
421 vid_ch->buf_field = common->fmt.fmt.pix.field;
422 }
423
424 if (V4L2_MEMORY_USERPTR == common->memory)
425 sizeimage = common->fmt.fmt.pix.sizeimage;
426 else
427 sizeimage = config_params.channel_bufsize[ch->channel_id];
428
429 hpitch = common->fmt.fmt.pix.bytesperline;
430 vpitch = sizeimage / (hpitch * 2);
431 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
432 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
433 common->ytop_off = 0;
434 common->ybtm_off = hpitch;
435 common->ctop_off = sizeimage / 2;
436 common->cbtm_off = sizeimage / 2 + hpitch;
437 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
438 common->ytop_off = 0;
439 common->ybtm_off = sizeimage / 4;
440 common->ctop_off = sizeimage / 2;
441 common->cbtm_off = common->ctop_off + sizeimage / 4;
442 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
443 common->ybtm_off = 0;
444 common->ytop_off = sizeimage / 4;
445 common->cbtm_off = sizeimage / 2;
446 common->ctop_off = common->cbtm_off + sizeimage / 4;
447 }
448
449 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
450 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
451 vpifparams->video_params.storage_mode = 1;
452 } else {
453 vpifparams->video_params.storage_mode = 0;
454 }
455
456 if (ch->vpifparams.std_info.frm_fmt == 1) {
457 vpifparams->video_params.hpitch =
458 common->fmt.fmt.pix.bytesperline;
459 } else {
460 if ((field == V4L2_FIELD_ANY) ||
461 (field == V4L2_FIELD_INTERLACED))
462 vpifparams->video_params.hpitch =
463 common->fmt.fmt.pix.bytesperline * 2;
464 else
465 vpifparams->video_params.hpitch =
466 common->fmt.fmt.pix.bytesperline;
467 }
468
469 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
470}
471
472static void vpif_config_format(struct channel_obj *ch)
473{
474 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
475
476 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
477 if (config_params.numbuffers[ch->channel_id] == 0)
478 common->memory = V4L2_MEMORY_USERPTR;
479 else
480 common->memory = V4L2_MEMORY_MMAP;
481
482 common->fmt.fmt.pix.sizeimage =
483 config_params.channel_bufsize[ch->channel_id];
484 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
485 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
486}
487
488static int vpif_check_format(struct channel_obj *ch,
489 struct v4l2_pix_format *pixfmt)
490{
491 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
492 enum v4l2_field field = pixfmt->field;
493 u32 sizeimage, hpitch, vpitch;
494
495 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
496 goto invalid_fmt_exit;
497
498 if (!(VPIF_VALID_FIELD(field)))
499 goto invalid_fmt_exit;
500
501 if (pixfmt->bytesperline <= 0)
502 goto invalid_pitch_exit;
503
504 if (V4L2_MEMORY_USERPTR == common->memory)
505 sizeimage = pixfmt->sizeimage;
506 else
507 sizeimage = config_params.channel_bufsize[ch->channel_id];
508
509 if (vpif_get_std_info(ch)) {
510 vpif_err("Error getting the standard info\n");
511 return -EINVAL;
512 }
513
514 hpitch = pixfmt->bytesperline;
515 vpitch = sizeimage / (hpitch * 2);
516
517 /* Check for valid value of pitch */
518 if ((hpitch < ch->vpifparams.std_info.width) ||
519 (vpitch < ch->vpifparams.std_info.height))
520 goto invalid_pitch_exit;
521
522 /* Check for 8 byte alignment */
523 if (!ISALIGNED(hpitch)) {
524 vpif_err("invalid pitch alignment\n");
525 return -EINVAL;
526 }
527 pixfmt->width = common->fmt.fmt.pix.width;
528 pixfmt->height = common->fmt.fmt.pix.height;
529
530 return 0;
531
532invalid_fmt_exit:
533 vpif_err("invalid field format\n");
534 return -EINVAL;
535
536invalid_pitch_exit:
537 vpif_err("invalid pitch\n");
538 return -EINVAL;
539}
540
541static void vpif_config_addr(struct channel_obj *ch, int muxmode)
542{
543 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
544
545 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
546 common->set_addr = ch3_set_videobuf_addr;
547 } else {
548 if (2 == muxmode)
549 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
550 else
551 common->set_addr = ch2_set_videobuf_addr;
552 }
553}
554
555/*
556 * vpif_mmap: It is used to map kernel space buffers into user spaces
557 */
558static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
559{
560 struct vpif_fh *fh = filep->private_data;
561 struct common_obj *common = &fh->channel->common[VPIF_VIDEO_INDEX];
562
563 return videobuf_mmap_mapper(&common->buffer_queue, vma);
564}
565
566/*
567 * vpif_poll: It is used for select/poll system call
568 */
569static unsigned int vpif_poll(struct file *filep, poll_table *wait)
570{
571 struct vpif_fh *fh = filep->private_data;
572 struct channel_obj *ch = fh->channel;
573 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
574
575 if (common->started)
576 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
577
578 return 0;
579}
580
581/*
582 * vpif_open: It creates object of file handle structure and stores it in
583 * private_data member of filepointer
584 */
585static int vpif_open(struct file *filep)
586{
587 struct video_device *vdev = video_devdata(filep);
588 struct channel_obj *ch = NULL;
589 struct vpif_fh *fh = NULL;
590
591 ch = video_get_drvdata(vdev);
592 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300593 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300594 if (fh == NULL) {
595 vpif_err("unable to allocate memory for file handle object\n");
596 return -ENOMEM;
597 }
598
599 /* store pointer to fh in private_data member of filep */
600 filep->private_data = fh;
601 fh->channel = ch;
602 fh->initialized = 0;
603 if (!ch->initialized) {
604 fh->initialized = 1;
605 ch->initialized = 1;
606 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
607 }
608
609 /* Increment channel usrs counter */
610 atomic_inc(&ch->usrs);
611 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
612 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
613 /* Initialize priority of this instance to default priority */
614 fh->prio = V4L2_PRIORITY_UNSET;
615 v4l2_prio_open(&ch->prio, &fh->prio);
616
617 return 0;
618}
619
620/*
621 * vpif_release: This function deletes buffer queue, frees the buffers and
622 * the vpif file handle
623 */
624static int vpif_release(struct file *filep)
625{
626 struct vpif_fh *fh = filep->private_data;
627 struct channel_obj *ch = fh->channel;
628 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
629
Chaithrika U S13df4f62009-06-22 09:02:55 -0300630 if (mutex_lock_interruptible(&common->lock))
631 return -ERESTARTSYS;
632
Chaithrika U Se7332e32009-06-09 05:55:37 -0300633 /* if this instance is doing IO */
634 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
635 /* Reset io_usrs member of channel object */
636 common->io_usrs = 0;
637 /* Disable channel */
638 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
639 enable_channel2(0);
640 channel2_intr_enable(0);
641 }
642 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
643 (2 == common->started)) {
644 enable_channel3(0);
645 channel3_intr_enable(0);
646 }
647 common->started = 0;
648 /* Free buffers allocated */
649 videobuf_queue_cancel(&common->buffer_queue);
650 videobuf_mmap_free(&common->buffer_queue);
651 common->numbuffers =
652 config_params.numbuffers[ch->channel_id];
653 }
654
655 mutex_unlock(&common->lock);
656
657 /* Decrement channel usrs counter */
658 atomic_dec(&ch->usrs);
659 /* If this file handle has initialize encoder device, reset it */
660 if (fh->initialized)
661 ch->initialized = 0;
662
663 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300664 v4l2_prio_close(&ch->prio, fh->prio);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300665 filep->private_data = NULL;
666 fh->initialized = 0;
667 kfree(fh);
668
669 return 0;
670}
671
672/* functions implementing ioctls */
673
674static int vpif_querycap(struct file *file, void *priv,
675 struct v4l2_capability *cap)
676{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300677 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300678
679 cap->version = VPIF_DISPLAY_VERSION_CODE;
680 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
681 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
682 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
683 strlcpy(cap->card, config->card_name, sizeof(cap->card));
684
685 return 0;
686}
687
688static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
689 struct v4l2_fmtdesc *fmt)
690{
691 if (fmt->index != 0) {
692 vpif_err("Invalid format index\n");
693 return -EINVAL;
694 }
695
696 /* Fill in the information about format */
697 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
698 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
699 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
700
701 return 0;
702}
703
704static int vpif_g_fmt_vid_out(struct file *file, void *priv,
705 struct v4l2_format *fmt)
706{
707 struct vpif_fh *fh = priv;
708 struct channel_obj *ch = fh->channel;
709 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
710
711 /* Check the validity of the buffer type */
712 if (common->fmt.type != fmt->type)
713 return -EINVAL;
714
715 /* Fill in the information about format */
Chaithrika U S13df4f62009-06-22 09:02:55 -0300716 if (mutex_lock_interruptible(&common->lock))
717 return -ERESTARTSYS;
718
Chaithrika U Se7332e32009-06-09 05:55:37 -0300719 if (vpif_get_std_info(ch)) {
720 vpif_err("Error getting the standard info\n");
721 return -EINVAL;
722 }
723
724 *fmt = common->fmt;
725 mutex_unlock(&common->lock);
726 return 0;
727}
728
729static int vpif_s_fmt_vid_out(struct file *file, void *priv,
730 struct v4l2_format *fmt)
731{
732 struct vpif_fh *fh = priv;
733 struct v4l2_pix_format *pixfmt;
734 struct channel_obj *ch = fh->channel;
735 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
736 int ret = 0;
737
738 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
739 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
740 if (!fh->initialized) {
741 vpif_dbg(1, debug, "Channel Busy\n");
742 return -EBUSY;
743 }
744
745 /* Check for the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300746 ret = v4l2_prio_check(&ch->prio, fh->prio);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300747 if (0 != ret)
748 return ret;
749 fh->initialized = 1;
750 }
751
752 if (common->started) {
753 vpif_dbg(1, debug, "Streaming in progress\n");
754 return -EBUSY;
755 }
756
757 pixfmt = &fmt->fmt.pix;
758 /* Check for valid field format */
759 ret = vpif_check_format(ch, pixfmt);
760 if (ret)
761 return ret;
762
763 /* store the pix format in the channel object */
764 common->fmt.fmt.pix = *pixfmt;
765 /* store the format in the channel object */
Chaithrika U S13df4f62009-06-22 09:02:55 -0300766 if (mutex_lock_interruptible(&common->lock))
767 return -ERESTARTSYS;
768
Chaithrika U Se7332e32009-06-09 05:55:37 -0300769 common->fmt = *fmt;
770 mutex_unlock(&common->lock);
771
772 return 0;
773}
774
775static int vpif_try_fmt_vid_out(struct file *file, void *priv,
776 struct v4l2_format *fmt)
777{
778 struct vpif_fh *fh = priv;
779 struct channel_obj *ch = fh->channel;
780 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
781 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
782 int ret = 0;
783
784 ret = vpif_check_format(ch, pixfmt);
785 if (ret) {
786 *pixfmt = common->fmt.fmt.pix;
787 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
788 }
789
790 return ret;
791}
792
793static int vpif_reqbufs(struct file *file, void *priv,
794 struct v4l2_requestbuffers *reqbuf)
795{
796 struct vpif_fh *fh = priv;
797 struct channel_obj *ch = fh->channel;
798 struct common_obj *common;
799 enum v4l2_field field;
800 u8 index = 0;
801 int ret = 0;
802
803 /* This file handle has not initialized the channel,
804 It is not allowed to do settings */
805 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
806 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
807 if (!fh->initialized) {
808 vpif_err("Channel Busy\n");
809 return -EBUSY;
810 }
811 }
812
813 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
814 return -EINVAL;
815
816 index = VPIF_VIDEO_INDEX;
817
818 common = &ch->common[index];
Chaithrika U S13df4f62009-06-22 09:02:55 -0300819 if (mutex_lock_interruptible(&common->lock))
820 return -ERESTARTSYS;
821
Chaithrika U Se7332e32009-06-09 05:55:37 -0300822 if (common->fmt.type != reqbuf->type) {
823 ret = -EINVAL;
824 goto reqbuf_exit;
825 }
826
827 if (0 != common->io_usrs) {
828 ret = -EBUSY;
829 goto reqbuf_exit;
830 }
831
832 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
833 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
834 field = V4L2_FIELD_INTERLACED;
835 else
836 field = common->fmt.fmt.pix.field;
837 } else {
838 field = V4L2_VBI_INTERLACED;
839 }
840
841 /* Initialize videobuf queue as per the buffer type */
842 videobuf_queue_dma_contig_init(&common->buffer_queue,
843 &video_qops, NULL,
844 &common->irqlock,
845 reqbuf->type, field,
Hans Verkuile3cfd442010-09-30 09:18:52 -0300846 sizeof(struct videobuf_buffer), fh,
847 NULL);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300848
849 /* Set io allowed member of file handle to TRUE */
850 fh->io_allowed[index] = 1;
851 /* Increment io usrs member of channel object to 1 */
852 common->io_usrs = 1;
853 /* Store type of memory requested in channel object */
854 common->memory = reqbuf->memory;
855 INIT_LIST_HEAD(&common->dma_queue);
856
857 /* Allocate buffers */
858 ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
859
860reqbuf_exit:
861 mutex_unlock(&common->lock);
862 return ret;
863}
864
865static int vpif_querybuf(struct file *file, void *priv,
866 struct v4l2_buffer *tbuf)
867{
868 struct vpif_fh *fh = priv;
869 struct channel_obj *ch = fh->channel;
870 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
871
872 if (common->fmt.type != tbuf->type)
873 return -EINVAL;
874
875 return videobuf_querybuf(&common->buffer_queue, tbuf);
876}
877
878static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
879{
880
881 struct vpif_fh *fh = priv;
882 struct channel_obj *ch = fh->channel;
883 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
884 struct v4l2_buffer tbuf = *buf;
885 struct videobuf_buffer *buf1;
886 unsigned long addr = 0;
887 unsigned long flags;
888 int ret = 0;
889
890 if (common->fmt.type != tbuf.type)
891 return -EINVAL;
892
893 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
894 vpif_err("fh->io_allowed\n");
895 return -EACCES;
896 }
897
898 if (!(list_empty(&common->dma_queue)) ||
899 (common->cur_frm != common->next_frm) ||
900 !(common->started) ||
901 (common->started && (0 == ch->field_id)))
902 return videobuf_qbuf(&common->buffer_queue, buf);
903
904 /* bufferqueue is empty store buffer address in VPIF registers */
905 mutex_lock(&common->buffer_queue.vb_lock);
906 buf1 = common->buffer_queue.bufs[tbuf.index];
907 if (buf1->memory != tbuf.memory) {
908 vpif_err("invalid buffer type\n");
909 goto qbuf_exit;
910 }
911
912 if ((buf1->state == VIDEOBUF_QUEUED) ||
913 (buf1->state == VIDEOBUF_ACTIVE)) {
914 vpif_err("invalid state\n");
915 goto qbuf_exit;
916 }
917
918 switch (buf1->memory) {
919 case V4L2_MEMORY_MMAP:
920 if (buf1->baddr == 0)
921 goto qbuf_exit;
922 break;
923
924 case V4L2_MEMORY_USERPTR:
925 if (tbuf.length < buf1->bsize)
926 goto qbuf_exit;
927
928 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
Julia Lawall473d8022010-08-05 17:22:44 -0300929 && (buf1->baddr != tbuf.m.userptr)) {
Chaithrika U Se7332e32009-06-09 05:55:37 -0300930 vpif_buffer_release(&common->buffer_queue, buf1);
931 buf1->baddr = tbuf.m.userptr;
Julia Lawall473d8022010-08-05 17:22:44 -0300932 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300933 break;
934
935 default:
936 goto qbuf_exit;
937 }
938
939 local_irq_save(flags);
940 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
941 common->buffer_queue.field);
942 if (ret < 0) {
943 local_irq_restore(flags);
944 goto qbuf_exit;
945 }
946
947 buf1->state = VIDEOBUF_ACTIVE;
948 addr = buf1->boff;
949 common->next_frm = buf1;
950 if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
951 common->set_addr((addr + common->ytop_off),
952 (addr + common->ybtm_off),
953 (addr + common->ctop_off),
954 (addr + common->cbtm_off));
955 }
956
957 local_irq_restore(flags);
958 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
959 mutex_unlock(&common->buffer_queue.vb_lock);
960 return 0;
961
962qbuf_exit:
963 mutex_unlock(&common->buffer_queue.vb_lock);
964 return -EINVAL;
965}
966
967static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
968{
969 struct vpif_fh *fh = priv;
970 struct channel_obj *ch = fh->channel;
971 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
972 int ret = 0;
973
974 if (!(*std_id & DM646X_V4L2_STD))
975 return -EINVAL;
976
977 if (common->started) {
978 vpif_err("streaming in progress\n");
979 return -EBUSY;
980 }
981
982 /* Call encoder subdevice function to set the standard */
Chaithrika U S13df4f62009-06-22 09:02:55 -0300983 if (mutex_lock_interruptible(&common->lock))
984 return -ERESTARTSYS;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300985
986 ch->video.stdid = *std_id;
987 /* Get the information about the standard */
988 if (vpif_get_std_info(ch)) {
989 vpif_err("Error getting the standard info\n");
990 return -EINVAL;
991 }
992
993 if ((ch->vpifparams.std_info.width *
994 ch->vpifparams.std_info.height * 2) >
995 config_params.channel_bufsize[ch->channel_id]) {
996 vpif_err("invalid std for this size\n");
997 ret = -EINVAL;
998 goto s_std_exit;
999 }
1000
1001 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1002 /* Configure the default format information */
1003 vpif_config_format(ch);
1004
1005 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1006 s_std_output, *std_id);
1007 if (ret < 0) {
1008 vpif_err("Failed to set output standard\n");
1009 goto s_std_exit;
1010 }
1011
1012 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1013 s_std, *std_id);
1014 if (ret < 0)
1015 vpif_err("Failed to set standard for sub devices\n");
1016
1017s_std_exit:
1018 mutex_unlock(&common->lock);
1019 return ret;
1020}
1021
1022static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1023{
1024 struct vpif_fh *fh = priv;
1025 struct channel_obj *ch = fh->channel;
1026
1027 *std = ch->video.stdid;
1028 return 0;
1029}
1030
1031static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1032{
1033 struct vpif_fh *fh = priv;
1034 struct channel_obj *ch = fh->channel;
1035 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1036
1037 return videobuf_dqbuf(&common->buffer_queue, p,
1038 (file->f_flags & O_NONBLOCK));
1039}
1040
1041static int vpif_streamon(struct file *file, void *priv,
1042 enum v4l2_buf_type buftype)
1043{
1044 struct vpif_fh *fh = priv;
1045 struct channel_obj *ch = fh->channel;
1046 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1047 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1048 struct vpif_params *vpif = &ch->vpifparams;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001049 struct vpif_display_config *vpif_config_data =
Chaithrika U Se7332e32009-06-09 05:55:37 -03001050 vpif_dev->platform_data;
1051 unsigned long addr = 0;
1052 int ret = 0;
1053
1054 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1055 vpif_err("buffer type not supported\n");
1056 return -EINVAL;
1057 }
1058
1059 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1060 vpif_err("fh->io_allowed\n");
1061 return -EACCES;
1062 }
1063
1064 /* If Streaming is already started, return error */
1065 if (common->started) {
1066 vpif_err("channel->started\n");
1067 return -EBUSY;
1068 }
1069
1070 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1071 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1072 ch->vpifparams.std_info.ycmux_mode == 0)
1073 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1074 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1075 vpif_err("other channel is using\n");
1076 return -EBUSY;
1077 }
1078
1079 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1080 if (ret < 0)
1081 return ret;
1082
1083 /* Call videobuf_streamon to start streaming in videobuf */
1084 ret = videobuf_streamon(&common->buffer_queue);
1085 if (ret < 0) {
1086 vpif_err("videobuf_streamon\n");
1087 return ret;
1088 }
1089
Chaithrika U S13df4f62009-06-22 09:02:55 -03001090 if (mutex_lock_interruptible(&common->lock))
1091 return -ERESTARTSYS;
1092
Chaithrika U Se7332e32009-06-09 05:55:37 -03001093 /* If buffer queue is empty, return error */
1094 if (list_empty(&common->dma_queue)) {
1095 vpif_err("buffer queue is empty\n");
1096 ret = -EIO;
1097 goto streamon_exit;
1098 }
1099
1100 /* Get the next frame from the buffer queue */
1101 common->next_frm = common->cur_frm =
1102 list_entry(common->dma_queue.next,
1103 struct videobuf_buffer, queue);
1104
1105 list_del(&common->cur_frm->queue);
1106 /* Mark state of the current frame to active */
1107 common->cur_frm->state = VIDEOBUF_ACTIVE;
1108
1109 /* Initialize field_id and started member */
1110 ch->field_id = 0;
1111 common->started = 1;
1112 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1113 addr = common->cur_frm->boff;
1114 /* Calculate the offset for Y and C data in the buffer */
1115 vpif_calculate_offsets(ch);
1116
1117 if ((ch->vpifparams.std_info.frm_fmt &&
1118 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1119 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1120 || (!ch->vpifparams.std_info.frm_fmt
1121 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1122 vpif_err("conflict in field format and std format\n");
1123 ret = -EINVAL;
1124 goto streamon_exit;
1125 }
1126
1127 /* clock settings */
1128 ret =
1129 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1130 ch->vpifparams.std_info.hd_sd);
1131 if (ret < 0) {
1132 vpif_err("can't set clock\n");
1133 goto streamon_exit;
1134 }
1135
1136 /* set the parameters and addresses */
1137 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1138 if (ret < 0)
1139 goto streamon_exit;
1140
1141 common->started = ret;
1142 vpif_config_addr(ch, ret);
1143 common->set_addr((addr + common->ytop_off),
1144 (addr + common->ybtm_off),
1145 (addr + common->ctop_off),
1146 (addr + common->cbtm_off));
1147
1148 /* Set interrupt for both the fields in VPIF
1149 Register enable channel in VPIF register */
1150 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1151 channel2_intr_assert();
1152 channel2_intr_enable(1);
1153 enable_channel2(1);
1154 }
1155
1156 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1157 || (common->started == 2)) {
1158 channel3_intr_assert();
1159 channel3_intr_enable(1);
1160 enable_channel3(1);
1161 }
1162 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1163 }
1164
1165streamon_exit:
1166 mutex_unlock(&common->lock);
1167 return ret;
1168}
1169
1170static int vpif_streamoff(struct file *file, void *priv,
1171 enum v4l2_buf_type buftype)
1172{
1173 struct vpif_fh *fh = priv;
1174 struct channel_obj *ch = fh->channel;
1175 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1176
1177 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1178 vpif_err("buffer type not supported\n");
1179 return -EINVAL;
1180 }
1181
1182 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1183 vpif_err("fh->io_allowed\n");
1184 return -EACCES;
1185 }
1186
1187 if (!common->started) {
1188 vpif_err("channel->started\n");
1189 return -EINVAL;
1190 }
1191
Chaithrika U S13df4f62009-06-22 09:02:55 -03001192 if (mutex_lock_interruptible(&common->lock))
1193 return -ERESTARTSYS;
1194
Chaithrika U Se7332e32009-06-09 05:55:37 -03001195 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1196 /* disable channel */
1197 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1198 enable_channel2(0);
1199 channel2_intr_enable(0);
1200 }
1201 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1202 (2 == common->started)) {
1203 enable_channel3(0);
1204 channel3_intr_enable(0);
1205 }
1206 }
1207
1208 common->started = 0;
1209 mutex_unlock(&common->lock);
1210
1211 return videobuf_streamoff(&common->buffer_queue);
1212}
1213
1214static int vpif_cropcap(struct file *file, void *priv,
1215 struct v4l2_cropcap *crop)
1216{
1217 struct vpif_fh *fh = priv;
1218 struct channel_obj *ch = fh->channel;
1219 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1220 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1221 return -EINVAL;
1222
1223 crop->bounds.left = crop->bounds.top = 0;
1224 crop->defrect.left = crop->defrect.top = 0;
1225 crop->defrect.height = crop->bounds.height = common->height;
1226 crop->defrect.width = crop->bounds.width = common->width;
1227
1228 return 0;
1229}
1230
1231static int vpif_enum_output(struct file *file, void *fh,
1232 struct v4l2_output *output)
1233{
1234
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001235 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001236
1237 if (output->index >= config->output_count) {
1238 vpif_dbg(1, debug, "Invalid output index\n");
1239 return -EINVAL;
1240 }
1241
1242 strcpy(output->name, config->output[output->index]);
1243 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1244 output->std = DM646X_V4L2_STD;
1245
1246 return 0;
1247}
1248
1249static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1250{
1251 struct vpif_fh *fh = priv;
1252 struct channel_obj *ch = fh->channel;
1253 struct video_obj *vid_ch = &ch->video;
1254 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1255 int ret = 0;
1256
Chaithrika U S13df4f62009-06-22 09:02:55 -03001257 if (mutex_lock_interruptible(&common->lock))
1258 return -ERESTARTSYS;
1259
Chaithrika U Se7332e32009-06-09 05:55:37 -03001260 if (common->started) {
1261 vpif_err("Streaming in progress\n");
1262 ret = -EBUSY;
1263 goto s_output_exit;
1264 }
1265
1266 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1267 s_routing, 0, i, 0);
1268
1269 if (ret < 0)
1270 vpif_err("Failed to set output standard\n");
1271
1272 vid_ch->output_id = i;
1273
1274s_output_exit:
1275 mutex_unlock(&common->lock);
1276 return ret;
1277}
1278
1279static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1280{
1281 struct vpif_fh *fh = priv;
1282 struct channel_obj *ch = fh->channel;
1283 struct video_obj *vid_ch = &ch->video;
1284
1285 *i = vid_ch->output_id;
1286
1287 return 0;
1288}
1289
1290static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1291{
1292 struct vpif_fh *fh = priv;
1293 struct channel_obj *ch = fh->channel;
1294
1295 *p = v4l2_prio_max(&ch->prio);
1296
1297 return 0;
1298}
1299
1300static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1301{
1302 struct vpif_fh *fh = priv;
1303 struct channel_obj *ch = fh->channel;
1304
1305 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1306}
1307
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001308
1309/*
1310 * vpif_g_chip_ident() - Identify the chip
1311 * @file: file ptr
1312 * @priv: file handle
1313 * @chip: chip identity
1314 *
1315 * Returns zero or -EINVAL if read operations fails.
1316 */
1317static int vpif_g_chip_ident(struct file *file, void *priv,
1318 struct v4l2_dbg_chip_ident *chip)
1319{
1320 chip->ident = V4L2_IDENT_NONE;
1321 chip->revision = 0;
1322 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1323 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1324 vpif_dbg(2, debug, "match_type is invalid.\n");
1325 return -EINVAL;
1326 }
1327
1328 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1329 g_chip_ident, chip);
1330}
1331
1332#ifdef CONFIG_VIDEO_ADV_DEBUG
1333/*
1334 * vpif_dbg_g_register() - Read register
1335 * @file: file ptr
1336 * @priv: file handle
1337 * @reg: register to be read
1338 *
1339 * Debugging only
1340 * Returns zero or -EINVAL if read operations fails.
1341 */
1342static int vpif_dbg_g_register(struct file *file, void *priv,
1343 struct v4l2_dbg_register *reg){
1344 struct vpif_fh *fh = priv;
1345 struct channel_obj *ch = fh->channel;
1346 struct video_obj *vid_ch = &ch->video;
1347
1348 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1349 g_register, reg);
1350}
1351
1352/*
1353 * vpif_dbg_s_register() - Write to register
1354 * @file: file ptr
1355 * @priv: file handle
1356 * @reg: register to be modified
1357 *
1358 * Debugging only
1359 * Returns zero or -EINVAL if write operations fails.
1360 */
1361static int vpif_dbg_s_register(struct file *file, void *priv,
1362 struct v4l2_dbg_register *reg){
1363 struct vpif_fh *fh = priv;
1364 struct channel_obj *ch = fh->channel;
1365 struct video_obj *vid_ch = &ch->video;
1366
1367 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1368 s_register, reg);
1369}
1370#endif
1371
1372/*
1373 * vpif_log_status() - Status information
1374 * @file: file ptr
1375 * @priv: file handle
1376 *
1377 * Returns zero.
1378 */
1379static int vpif_log_status(struct file *filep, void *priv)
1380{
1381 /* status for sub devices */
1382 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1383
1384 return 0;
1385}
1386
Chaithrika U Se7332e32009-06-09 05:55:37 -03001387/* vpif display ioctl operations */
1388static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1389 .vidioc_querycap = vpif_querycap,
1390 .vidioc_g_priority = vpif_g_priority,
1391 .vidioc_s_priority = vpif_s_priority,
1392 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1393 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1394 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1395 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1396 .vidioc_reqbufs = vpif_reqbufs,
1397 .vidioc_querybuf = vpif_querybuf,
1398 .vidioc_qbuf = vpif_qbuf,
1399 .vidioc_dqbuf = vpif_dqbuf,
1400 .vidioc_streamon = vpif_streamon,
1401 .vidioc_streamoff = vpif_streamoff,
1402 .vidioc_s_std = vpif_s_std,
1403 .vidioc_g_std = vpif_g_std,
1404 .vidioc_enum_output = vpif_enum_output,
1405 .vidioc_s_output = vpif_s_output,
1406 .vidioc_g_output = vpif_g_output,
1407 .vidioc_cropcap = vpif_cropcap,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001408 .vidioc_g_chip_ident = vpif_g_chip_ident,
1409#ifdef CONFIG_VIDEO_ADV_DEBUG
1410 .vidioc_g_register = vpif_dbg_g_register,
1411 .vidioc_s_register = vpif_dbg_s_register,
1412#endif
1413 .vidioc_log_status = vpif_log_status,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001414};
1415
1416static const struct v4l2_file_operations vpif_fops = {
1417 .owner = THIS_MODULE,
1418 .open = vpif_open,
1419 .release = vpif_release,
1420 .ioctl = video_ioctl2,
1421 .mmap = vpif_mmap,
1422 .poll = vpif_poll
1423};
1424
1425static struct video_device vpif_video_template = {
1426 .name = "vpif",
1427 .fops = &vpif_fops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001428 .ioctl_ops = &vpif_ioctl_ops,
1429 .tvnorms = DM646X_V4L2_STD,
1430 .current_norm = V4L2_STD_625_50,
1431
1432};
1433
1434/*Configure the channels, buffer sizei, request irq */
1435static int initialize_vpif(void)
1436{
1437 int free_channel_objects_index;
1438 int free_buffer_channel_index;
1439 int free_buffer_index;
1440 int err = 0, i, j;
1441
1442 /* Default number of buffers should be 3 */
1443 if ((ch2_numbuffers > 0) &&
1444 (ch2_numbuffers < config_params.min_numbuffers))
1445 ch2_numbuffers = config_params.min_numbuffers;
1446 if ((ch3_numbuffers > 0) &&
1447 (ch3_numbuffers < config_params.min_numbuffers))
1448 ch3_numbuffers = config_params.min_numbuffers;
1449
1450 /* Set buffer size to min buffers size if invalid buffer size is
1451 * given */
1452 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1453 ch2_bufsize =
1454 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1455 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1456 ch3_bufsize =
1457 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1458
1459 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1460
1461 if (ch2_numbuffers) {
1462 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1463 ch2_bufsize;
1464 }
1465 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1466
1467 if (ch3_numbuffers) {
1468 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1469 ch3_bufsize;
1470 }
1471
1472 /* Allocate memory for six channel objects */
1473 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1474 vpif_obj.dev[i] =
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001475 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001476 /* If memory allocation fails, return error */
1477 if (!vpif_obj.dev[i]) {
1478 free_channel_objects_index = i;
1479 err = -ENOMEM;
1480 goto vpif_init_free_channel_objects;
1481 }
1482 }
1483
1484 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1485 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1486 free_buffer_index = config_params.numbuffers[i - 1];
1487
1488 return 0;
1489
1490vpif_init_free_channel_objects:
1491 for (j = 0; j < free_channel_objects_index; j++)
1492 kfree(vpif_obj.dev[j]);
1493 return err;
1494}
1495
1496/*
1497 * vpif_probe: This function creates device entries by register itself to the
1498 * V4L2 driver and initializes fields of each channel objects
1499 */
1500static __init int vpif_probe(struct platform_device *pdev)
1501{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001502 struct vpif_subdev_info *subdevdata;
1503 struct vpif_display_config *config;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001504 int i, j = 0, k, q, m, err = 0;
1505 struct i2c_adapter *i2c_adap;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001506 struct common_obj *common;
1507 struct channel_obj *ch;
1508 struct video_device *vfd;
1509 struct resource *res;
1510 int subdev_count;
1511
1512 vpif_dev = &pdev->dev;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001513
1514 err = initialize_vpif();
1515
1516 if (err) {
1517 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1518 return err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001519 }
1520
Chaithrika U Se7332e32009-06-09 05:55:37 -03001521 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1522 if (err) {
1523 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1524 return err;
1525 }
1526
1527 k = 0;
1528 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1529 for (i = res->start; i <= res->end; i++) {
1530 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1531 "DM646x_Display",
1532 (void *)(&vpif_obj.dev[k]->channel_id))) {
1533 err = -EBUSY;
1534 goto vpif_int_err;
1535 }
1536 }
1537 k++;
1538 }
1539
1540 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1541
1542 /* Get the pointer to the channel object */
1543 ch = vpif_obj.dev[i];
1544
1545 /* Allocate memory for video device */
1546 vfd = video_device_alloc();
1547 if (vfd == NULL) {
1548 for (j = 0; j < i; j++) {
1549 ch = vpif_obj.dev[j];
1550 video_device_release(ch->video_dev);
1551 }
1552 err = -ENOMEM;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001553 goto vpif_int_err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001554 }
1555
1556 /* Initialize field of video device */
1557 *vfd = vpif_video_template;
1558 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1559 vfd->release = video_device_release;
1560 snprintf(vfd->name, sizeof(vfd->name),
1561 "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
1562 (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
1563 (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
1564 (VPIF_DISPLAY_VERSION_CODE) & 0xff);
1565
1566 /* Set video_dev to the video device */
1567 ch->video_dev = vfd;
1568 }
1569
1570 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1571 ch = vpif_obj.dev[j];
1572 /* Initialize field of the channel objects */
1573 atomic_set(&ch->usrs, 0);
1574 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1575 ch->common[k].numbuffers = 0;
1576 common = &ch->common[k];
1577 common->io_usrs = 0;
1578 common->started = 0;
1579 spin_lock_init(&common->irqlock);
1580 mutex_init(&common->lock);
1581 common->numbuffers = 0;
1582 common->set_addr = NULL;
1583 common->ytop_off = common->ybtm_off = 0;
1584 common->ctop_off = common->cbtm_off = 0;
1585 common->cur_frm = common->next_frm = NULL;
1586 memset(&common->fmt, 0, sizeof(common->fmt));
1587 common->numbuffers = config_params.numbuffers[k];
1588
1589 }
1590 ch->initialized = 0;
1591 ch->channel_id = j;
1592 if (j < 2)
1593 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1594 config_params.numbuffers[ch->channel_id];
1595 else
1596 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1597
1598 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1599
1600 /* Initialize prio member of channel object */
1601 v4l2_prio_init(&ch->prio);
1602 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1603 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1604
1605 /* register video device */
1606 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1607 (int)ch, (int)&ch->video_dev);
1608
1609 err = video_register_device(ch->video_dev,
1610 VFL_TYPE_GRABBER, (j ? 3 : 2));
1611 if (err < 0)
1612 goto probe_out;
1613
1614 video_set_drvdata(ch->video_dev, ch);
1615 }
1616
1617 i2c_adap = i2c_get_adapter(1);
1618 config = pdev->dev.platform_data;
1619 subdev_count = config->subdev_count;
1620 subdevdata = config->subdevinfo;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001621 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001622 GFP_KERNEL);
1623 if (vpif_obj.sd == NULL) {
1624 vpif_err("unable to allocate memory for subdevice pointers\n");
1625 err = -ENOMEM;
1626 goto probe_out;
1627 }
1628
1629 for (i = 0; i < subdev_count; i++) {
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001630 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001631 i2c_adap,
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001632 &subdevdata[i].board_info,
1633 NULL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001634 if (!vpif_obj.sd[i]) {
1635 vpif_err("Error registering v4l2 subdevice\n");
1636 goto probe_subdev_out;
1637 }
1638
1639 if (vpif_obj.sd[i])
1640 vpif_obj.sd[i]->grp_id = 1 << i;
1641 }
1642
1643 return 0;
1644
1645probe_subdev_out:
1646 kfree(vpif_obj.sd);
1647probe_out:
1648 for (k = 0; k < j; k++) {
1649 ch = vpif_obj.dev[k];
1650 video_unregister_device(ch->video_dev);
1651 video_device_release(ch->video_dev);
1652 ch->video_dev = NULL;
1653 }
1654vpif_int_err:
1655 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1656 vpif_err("VPIF IRQ request failed\n");
1657 for (q = k; k >= 0; k--) {
1658 for (m = i; m >= res->start; m--)
1659 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1660 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1661 m = res->end;
1662 }
Chaithrika U Se7332e32009-06-09 05:55:37 -03001663
1664 return err;
1665}
1666
1667/*
1668 * vpif_remove: It un-register channels from V4L2 driver
1669 */
1670static int vpif_remove(struct platform_device *device)
1671{
1672 struct channel_obj *ch;
1673 int i;
1674
1675 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1676
1677 /* un-register device */
1678 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1679 /* Get the pointer to the channel object */
1680 ch = vpif_obj.dev[i];
1681 /* Unregister video device */
1682 video_unregister_device(ch->video_dev);
1683
1684 ch->video_dev = NULL;
1685 }
1686
1687 return 0;
1688}
1689
Mats Randgaardffa1b392010-08-30 10:30:36 -03001690static __refdata struct platform_driver vpif_driver = {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001691 .driver = {
1692 .name = "vpif_display",
1693 .owner = THIS_MODULE,
1694 },
1695 .probe = vpif_probe,
1696 .remove = vpif_remove,
1697};
1698
1699static __init int vpif_init(void)
1700{
1701 return platform_driver_register(&vpif_driver);
1702}
1703
1704/*
1705 * vpif_cleanup: This function un-registers device and driver to the kernel,
1706 * frees requested irq handler and de-allocates memory allocated for channel
1707 * objects.
1708 */
1709static void vpif_cleanup(void)
1710{
1711 struct platform_device *pdev;
1712 struct resource *res;
1713 int irq_num;
1714 int i = 0;
1715
1716 pdev = container_of(vpif_dev, struct platform_device, dev);
1717
1718 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1719 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1720 free_irq(irq_num,
1721 (void *)(&vpif_obj.dev[i]->channel_id));
1722 i++;
1723 }
1724
Chaithrika U Se7332e32009-06-09 05:55:37 -03001725 platform_driver_unregister(&vpif_driver);
1726 kfree(vpif_obj.sd);
1727 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1728 kfree(vpif_obj.dev[i]);
1729}
1730
1731module_init(vpif_init);
1732module_exit(vpif_cleanup);