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