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