2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/errno.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>
32 #include <linux/version.h>
33 #include <linux/slab.h>
38 #include <media/adv7343.h>
39 #include <media/v4l2-device.h>
40 #include <media/v4l2-ioctl.h>
42 #include <mach/dm646x.h>
44 #include "vpif_display.h"
47 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
48 MODULE_LICENSE("GPL");
50 #define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
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)
57 static u32 ch2_numbuffers
= 3;
58 static u32 ch3_numbuffers
= 3;
59 static u32 ch2_bufsize
= 1920 * 1080 * 2;
60 static u32 ch3_bufsize
= 720 * 576 * 2;
62 module_param(debug
, int, 0644);
63 module_param(ch2_numbuffers
, uint
, S_IRUGO
);
64 module_param(ch3_numbuffers
, uint
, S_IRUGO
);
65 module_param(ch2_bufsize
, uint
, S_IRUGO
);
66 module_param(ch3_bufsize
, uint
, S_IRUGO
);
68 MODULE_PARM_DESC(debug
, "Debug level 0-1");
69 MODULE_PARM_DESC(ch2_numbuffers
, "Channel2 buffer count (default:3)");
70 MODULE_PARM_DESC(ch3_numbuffers
, "Channel3 buffer count (default:3)");
71 MODULE_PARM_DESC(ch2_bufsize
, "Channel2 buffer size (default:1920 x 1080 x 2)");
72 MODULE_PARM_DESC(ch3_bufsize
, "Channel3 buffer size (default:720 x 576 x 2)");
74 static struct vpif_config_params config_params
= {
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,
84 static struct vpif_device vpif_obj
= { {NULL
} };
85 static struct device
*vpif_dev
;
87 static const struct vpif_channel_config_params ch_params
[] = {
89 "NTSC", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
90 286, 525, 525, 0, 1, 0, V4L2_STD_525_60
,
93 "PAL", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
94 336, 624, 625, 0, 1, 0, V4L2_STD_625_50
,
99 * vpif_uservirt_to_phys: This function is used to convert user
100 * space virtual address to physical address.
102 static u32
vpif_uservirt_to_phys(u32 virtp
)
104 struct mm_struct
*mm
= current
->mm
;
105 unsigned long physp
= 0;
106 struct vm_area_struct
*vma
;
108 vma
= find_vma(mm
, virtp
);
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
);
117 /* otherwise, use get_user_pages() for general userland pages */
118 int res
, nr_pages
= 1;
120 down_read(¤t
->mm
->mmap_sem
);
122 res
= get_user_pages(current
, current
->mm
,
123 virtp
, nr_pages
, 1, 0, &pages
, NULL
);
124 up_read(¤t
->mm
->mmap_sem
);
126 if (res
== nr_pages
) {
127 physp
= __pa(page_address(&pages
[0]) +
128 (virtp
& ~PAGE_MASK
));
130 vpif_err("get_user_pages failed\n");
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
143 static int vpif_buffer_prepare(struct videobuf_queue
*q
,
144 struct videobuf_buffer
*vb
,
145 enum v4l2_field field
)
147 struct vpif_fh
*fh
= q
->priv_data
;
148 struct common_obj
*common
;
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
;
158 vb
->state
= VIDEOBUF_PREPARED
;
160 /* if user pointer memory mechanism is used, get the physical
161 * address of the buffer */
162 if (V4L2_MEMORY_USERPTR
== common
->memory
) {
164 vpif_err("buffer_address is 0\n");
168 vb
->boff
= vpif_uservirt_to_phys(vb
->baddr
);
169 if (!ISALIGNED(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
))
184 vpif_err("buffer offset not aligned to 8 bytes\n");
189 * vpif_buffer_setup: This function allocates memory for the buffers
191 static int vpif_buffer_setup(struct videobuf_queue
*q
, unsigned int *count
,
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
];
198 if (V4L2_MEMORY_MMAP
!= common
->memory
)
201 *size
= config_params
.channel_bufsize
[ch
->channel_id
];
202 if (*count
< config_params
.min_numbuffers
)
203 *count
= config_params
.min_numbuffers
;
209 * vpif_buffer_queue: This function adds the buffer to DMA queue
211 static void vpif_buffer_queue(struct videobuf_queue
*q
,
212 struct videobuf_buffer
*vb
)
214 struct vpif_fh
*fh
= q
->priv_data
;
215 struct common_obj
*common
;
217 common
= &fh
->channel
->common
[VPIF_VIDEO_INDEX
];
219 /* add the buffer to the DMA queue */
220 list_add_tail(&vb
->queue
, &common
->dma_queue
);
221 vb
->state
= VIDEOBUF_QUEUED
;
225 * vpif_buffer_release: This function is called from the videobuf layer to
226 * free memory allocated to the buffers
228 static void vpif_buffer_release(struct videobuf_queue
*q
,
229 struct videobuf_buffer
*vb
)
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;
236 common
= &ch
->common
[VPIF_VIDEO_INDEX
];
238 videobuf_dma_contig_free(q
, vb
);
239 vb
->state
= VIDEOBUF_NEEDS_INIT
;
241 if (V4L2_MEMORY_MMAP
!= common
->memory
)
244 buf_size
= config_params
.channel_bufsize
[ch
->channel_id
];
247 static 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
,
253 static u8 channel_first_int
[VPIF_NUMOBJECTS
][2] = { {1, 1} };
255 static void process_progressive_mode(struct common_obj
*common
)
257 unsigned long addr
= 0;
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
;
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
);
275 static void process_interlaced_mode(int fid
, struct common_obj
*common
)
277 /* device field id and local field id are in sync */
278 /* If this is even field */
280 if (common
->cur_frm
== common
->next_frm
)
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
;
294 } else if (1 == fid
) { /* odd field */
295 if (list_empty(&common
->dma_queue
)
296 || (common
->cur_frm
!= common
->next_frm
)) {
299 /* one field is displayed configure the next
300 * frame if it is available else hold on current
302 /* Get next from the buffer queue */
303 process_progressive_mode(common
);
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
312 static irqreturn_t
vpif_channel_isr(int irq
, void *dev_id
)
314 struct vpif_device
*dev
= &vpif_obj
;
315 struct channel_obj
*ch
;
316 struct common_obj
*common
;
317 enum v4l2_field field
;
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
)
330 if (1 == ch
->vpifparams
.std_info
.frm_fmt
) {
331 if (list_empty(&common
->dma_queue
))
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
;
345 channel_first_int
[i
][channel_id
] = 0;
346 process_progressive_mode(common
);
348 /* Interlaced mode */
349 /* If it is first interrupt, ignore it */
351 if (channel_first_int
[i
][channel_id
]) {
352 channel_first_int
[i
][channel_id
] = 0;
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 */
369 process_interlaced_mode(fid
, common
);
376 static int vpif_get_std_info(struct channel_obj
*ch
)
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
;
386 std_info
->stdid
= vid_ch
->stdid
;
387 if (!std_info
->stdid
)
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
));
398 if (index
== ARRAY_SIZE(ch_params
))
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
);
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
;
414 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
415 * in the top and bottom field
417 static void vpif_calculate_offsets(struct channel_obj
*ch
)
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
;
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
;
429 vid_ch
->buf_field
= V4L2_FIELD_INTERLACED
;
431 vid_ch
->buf_field
= common
->fmt
.fmt
.pix
.field
;
434 if (V4L2_MEMORY_USERPTR
== common
->memory
)
435 sizeimage
= common
->fmt
.fmt
.pix
.sizeimage
;
437 sizeimage
= config_params
.channel_bufsize
[ch
->channel_id
];
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;
459 if ((V4L2_FIELD_NONE
== vid_ch
->buf_field
) ||
460 (V4L2_FIELD_INTERLACED
== vid_ch
->buf_field
)) {
461 vpifparams
->video_params
.storage_mode
= 1;
463 vpifparams
->video_params
.storage_mode
= 0;
466 if (ch
->vpifparams
.std_info
.frm_fmt
== 1) {
467 vpifparams
->video_params
.hpitch
=
468 common
->fmt
.fmt
.pix
.bytesperline
;
470 if ((field
== V4L2_FIELD_ANY
) ||
471 (field
== V4L2_FIELD_INTERLACED
))
472 vpifparams
->video_params
.hpitch
=
473 common
->fmt
.fmt
.pix
.bytesperline
* 2;
475 vpifparams
->video_params
.hpitch
=
476 common
->fmt
.fmt
.pix
.bytesperline
;
479 ch
->vpifparams
.video_params
.stdid
= ch
->vpifparams
.std_info
.stdid
;
482 static void vpif_config_format(struct channel_obj
*ch
)
484 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
486 common
->fmt
.fmt
.pix
.field
= V4L2_FIELD_ANY
;
487 if (config_params
.numbuffers
[ch
->channel_id
] == 0)
488 common
->memory
= V4L2_MEMORY_USERPTR
;
490 common
->memory
= V4L2_MEMORY_MMAP
;
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
;
498 static int vpif_check_format(struct channel_obj
*ch
,
499 struct v4l2_pix_format
*pixfmt
)
501 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
502 enum v4l2_field field
= pixfmt
->field
;
503 u32 sizeimage
, hpitch
, vpitch
;
505 if (pixfmt
->pixelformat
!= V4L2_PIX_FMT_YUV422P
)
506 goto invalid_fmt_exit
;
508 if (!(VPIF_VALID_FIELD(field
)))
509 goto invalid_fmt_exit
;
511 if (pixfmt
->bytesperline
<= 0)
512 goto invalid_pitch_exit
;
514 if (V4L2_MEMORY_USERPTR
== common
->memory
)
515 sizeimage
= pixfmt
->sizeimage
;
517 sizeimage
= config_params
.channel_bufsize
[ch
->channel_id
];
519 if (vpif_get_std_info(ch
)) {
520 vpif_err("Error getting the standard info\n");
524 hpitch
= pixfmt
->bytesperline
;
525 vpitch
= sizeimage
/ (hpitch
* 2);
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
;
532 /* Check for 8 byte alignment */
533 if (!ISALIGNED(hpitch
)) {
534 vpif_err("invalid pitch alignment\n");
537 pixfmt
->width
= common
->fmt
.fmt
.pix
.width
;
538 pixfmt
->height
= common
->fmt
.fmt
.pix
.height
;
543 vpif_err("invalid field format\n");
547 vpif_err("invalid pitch\n");
551 static void vpif_config_addr(struct channel_obj
*ch
, int muxmode
)
553 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
555 if (VPIF_CHANNEL3_VIDEO
== ch
->channel_id
) {
556 common
->set_addr
= ch3_set_videobuf_addr
;
559 common
->set_addr
= ch2_set_videobuf_addr_yc_nmux
;
561 common
->set_addr
= ch2_set_videobuf_addr
;
566 * vpif_mmap: It is used to map kernel space buffers into user spaces
568 static int vpif_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
570 struct vpif_fh
*fh
= filep
->private_data
;
571 struct common_obj
*common
= &fh
->channel
->common
[VPIF_VIDEO_INDEX
];
573 return videobuf_mmap_mapper(&common
->buffer_queue
, vma
);
577 * vpif_poll: It is used for select/poll system call
579 static unsigned int vpif_poll(struct file
*filep
, poll_table
*wait
)
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
];
586 return videobuf_poll_stream(filep
, &common
->buffer_queue
, wait
);
592 * vpif_open: It creates object of file handle structure and stores it in
593 * private_data member of filepointer
595 static int vpif_open(struct file
*filep
)
597 struct video_device
*vdev
= video_devdata(filep
);
598 struct channel_obj
*ch
= NULL
;
599 struct vpif_fh
*fh
= NULL
;
601 ch
= video_get_drvdata(vdev
);
602 /* Allocate memory for the file handle object */
603 fh
= kzalloc(sizeof(struct vpif_fh
), GFP_KERNEL
);
605 vpif_err("unable to allocate memory for file handle object\n");
609 /* store pointer to fh in private_data member of filep */
610 filep
->private_data
= fh
;
613 if (!ch
->initialized
) {
616 memset(&ch
->vpifparams
, 0, sizeof(ch
->vpifparams
));
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
);
631 * vpif_release: This function deletes buffer queue, frees the buffers and
632 * the vpif file handle
634 static int vpif_release(struct file
*filep
)
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
];
640 if (mutex_lock_interruptible(&common
->lock
))
643 /* if this instance is doing IO */
644 if (fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
645 /* Reset io_usrs member of channel object */
647 /* Disable channel */
648 if (VPIF_CHANNEL2_VIDEO
== ch
->channel_id
) {
650 channel2_intr_enable(0);
652 if ((VPIF_CHANNEL3_VIDEO
== ch
->channel_id
) ||
653 (2 == common
->started
)) {
655 channel3_intr_enable(0);
658 /* Free buffers allocated */
659 videobuf_queue_cancel(&common
->buffer_queue
);
660 videobuf_mmap_free(&common
->buffer_queue
);
662 config_params
.numbuffers
[ch
->channel_id
];
665 mutex_unlock(&common
->lock
);
667 /* Decrement channel usrs counter */
668 atomic_dec(&ch
->usrs
);
669 /* If this file handle has initialize encoder device, reset it */
673 /* Close the priority */
674 v4l2_prio_close(&ch
->prio
, fh
->prio
);
675 filep
->private_data
= NULL
;
682 /* functions implementing ioctls */
684 static int vpif_querycap(struct file
*file
, void *priv
,
685 struct v4l2_capability
*cap
)
687 struct vpif_display_config
*config
= vpif_dev
->platform_data
;
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
));
698 static int vpif_enum_fmt_vid_out(struct file
*file
, void *priv
,
699 struct v4l2_fmtdesc
*fmt
)
701 if (fmt
->index
!= 0) {
702 vpif_err("Invalid format index\n");
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
;
714 static int vpif_g_fmt_vid_out(struct file
*file
, void *priv
,
715 struct v4l2_format
*fmt
)
717 struct vpif_fh
*fh
= priv
;
718 struct channel_obj
*ch
= fh
->channel
;
719 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
721 /* Check the validity of the buffer type */
722 if (common
->fmt
.type
!= fmt
->type
)
725 /* Fill in the information about format */
726 if (mutex_lock_interruptible(&common
->lock
))
729 if (vpif_get_std_info(ch
)) {
730 vpif_err("Error getting the standard info\n");
735 mutex_unlock(&common
->lock
);
739 static int vpif_s_fmt_vid_out(struct file
*file
, void *priv
,
740 struct v4l2_format
*fmt
)
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
];
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");
755 /* Check for the priority */
756 ret
= v4l2_prio_check(&ch
->prio
, fh
->prio
);
762 if (common
->started
) {
763 vpif_dbg(1, debug
, "Streaming in progress\n");
767 pixfmt
= &fmt
->fmt
.pix
;
768 /* Check for valid field format */
769 ret
= vpif_check_format(ch
, pixfmt
);
773 /* store the pix format in the channel object */
774 common
->fmt
.fmt
.pix
= *pixfmt
;
775 /* store the format in the channel object */
776 if (mutex_lock_interruptible(&common
->lock
))
780 mutex_unlock(&common
->lock
);
785 static int vpif_try_fmt_vid_out(struct file
*file
, void *priv
,
786 struct v4l2_format
*fmt
)
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
;
794 ret
= vpif_check_format(ch
, pixfmt
);
796 *pixfmt
= common
->fmt
.fmt
.pix
;
797 pixfmt
->sizeimage
= pixfmt
->width
* pixfmt
->height
* 2;
803 static int vpif_reqbufs(struct file
*file
, void *priv
,
804 struct v4l2_requestbuffers
*reqbuf
)
806 struct vpif_fh
*fh
= priv
;
807 struct channel_obj
*ch
= fh
->channel
;
808 struct common_obj
*common
;
809 enum v4l2_field field
;
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");
823 if (V4L2_BUF_TYPE_VIDEO_OUTPUT
!= reqbuf
->type
)
826 index
= VPIF_VIDEO_INDEX
;
828 common
= &ch
->common
[index
];
829 if (mutex_lock_interruptible(&common
->lock
))
832 if (common
->fmt
.type
!= reqbuf
->type
) {
837 if (0 != common
->io_usrs
) {
842 if (reqbuf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
843 if (common
->fmt
.fmt
.pix
.field
== V4L2_FIELD_ANY
)
844 field
= V4L2_FIELD_INTERLACED
;
846 field
= common
->fmt
.fmt
.pix
.field
;
848 field
= V4L2_VBI_INTERLACED
;
851 /* Initialize videobuf queue as per the buffer type */
852 videobuf_queue_dma_contig_init(&common
->buffer_queue
,
856 sizeof(struct videobuf_buffer
), fh
,
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 */
863 /* Store type of memory requested in channel object */
864 common
->memory
= reqbuf
->memory
;
865 INIT_LIST_HEAD(&common
->dma_queue
);
867 /* Allocate buffers */
868 ret
= videobuf_reqbufs(&common
->buffer_queue
, reqbuf
);
871 mutex_unlock(&common
->lock
);
875 static int vpif_querybuf(struct file
*file
, void *priv
,
876 struct v4l2_buffer
*tbuf
)
878 struct vpif_fh
*fh
= priv
;
879 struct channel_obj
*ch
= fh
->channel
;
880 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
882 if (common
->fmt
.type
!= tbuf
->type
)
885 return videobuf_querybuf(&common
->buffer_queue
, tbuf
);
888 static int vpif_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
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;
900 if (common
->fmt
.type
!= tbuf
.type
)
903 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
904 vpif_err("fh->io_allowed\n");
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
);
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");
922 if ((buf1
->state
== VIDEOBUF_QUEUED
) ||
923 (buf1
->state
== VIDEOBUF_ACTIVE
)) {
924 vpif_err("invalid state\n");
928 switch (buf1
->memory
) {
929 case V4L2_MEMORY_MMAP
:
930 if (buf1
->baddr
== 0)
934 case V4L2_MEMORY_USERPTR
:
935 if (tbuf
.length
< buf1
->bsize
)
938 if ((VIDEOBUF_NEEDS_INIT
!= buf1
->state
)
939 && (buf1
->baddr
!= tbuf
.m
.userptr
)) {
940 vpif_buffer_release(&common
->buffer_queue
, buf1
);
941 buf1
->baddr
= tbuf
.m
.userptr
;
949 local_irq_save(flags
);
950 ret
= vpif_buffer_prepare(&common
->buffer_queue
, buf1
,
951 common
->buffer_queue
.field
);
953 local_irq_restore(flags
);
957 buf1
->state
= VIDEOBUF_ACTIVE
;
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
));
967 local_irq_restore(flags
);
968 list_add_tail(&buf1
->stream
, &common
->buffer_queue
.stream
);
969 mutex_unlock(&common
->buffer_queue
.vb_lock
);
973 mutex_unlock(&common
->buffer_queue
.vb_lock
);
977 static int vpif_s_std(struct file
*file
, void *priv
, v4l2_std_id
*std_id
)
979 struct vpif_fh
*fh
= priv
;
980 struct channel_obj
*ch
= fh
->channel
;
981 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
984 if (!(*std_id
& DM646X_V4L2_STD
))
987 if (common
->started
) {
988 vpif_err("streaming in progress\n");
992 /* Call encoder subdevice function to set the standard */
993 if (mutex_lock_interruptible(&common
->lock
))
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");
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");
1011 common
->fmt
.fmt
.pix
.bytesperline
= common
->fmt
.fmt
.pix
.width
;
1012 /* Configure the default format information */
1013 vpif_config_format(ch
);
1015 ret
= v4l2_device_call_until_err(&vpif_obj
.v4l2_dev
, 1, video
,
1016 s_std_output
, *std_id
);
1018 vpif_err("Failed to set output standard\n");
1022 ret
= v4l2_device_call_until_err(&vpif_obj
.v4l2_dev
, 1, core
,
1025 vpif_err("Failed to set standard for sub devices\n");
1028 mutex_unlock(&common
->lock
);
1032 static int vpif_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
1034 struct vpif_fh
*fh
= priv
;
1035 struct channel_obj
*ch
= fh
->channel
;
1037 *std
= ch
->video
.stdid
;
1041 static int vpif_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1043 struct vpif_fh
*fh
= priv
;
1044 struct channel_obj
*ch
= fh
->channel
;
1045 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1047 return videobuf_dqbuf(&common
->buffer_queue
, p
,
1048 (file
->f_flags
& O_NONBLOCK
));
1051 static int vpif_streamon(struct file
*file
, void *priv
,
1052 enum v4l2_buf_type buftype
)
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
;
1059 struct vpif_display_config
*vpif_config_data
=
1060 vpif_dev
->platform_data
;
1061 unsigned long addr
= 0;
1064 if (buftype
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1065 vpif_err("buffer type not supported\n");
1069 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1070 vpif_err("fh->io_allowed\n");
1074 /* If Streaming is already started, return error */
1075 if (common
->started
) {
1076 vpif_err("channel->started\n");
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");
1089 ret
= vpif_check_format(ch
, &common
->fmt
.fmt
.pix
);
1093 /* Call videobuf_streamon to start streaming in videobuf */
1094 ret
= videobuf_streamon(&common
->buffer_queue
);
1096 vpif_err("videobuf_streamon\n");
1100 if (mutex_lock_interruptible(&common
->lock
))
1101 return -ERESTARTSYS
;
1103 /* If buffer queue is empty, return error */
1104 if (list_empty(&common
->dma_queue
)) {
1105 vpif_err("buffer queue is empty\n");
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
);
1115 list_del(&common
->cur_frm
->queue
);
1116 /* Mark state of the current frame to active */
1117 common
->cur_frm
->state
= VIDEOBUF_ACTIVE
;
1119 /* Initialize field_id and started member */
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
);
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");
1137 /* clock settings */
1139 vpif_config_data
->set_clock(ch
->vpifparams
.std_info
.ycmux_mode
,
1140 ch
->vpifparams
.std_info
.hd_sd
);
1142 vpif_err("can't set clock\n");
1146 /* set the parameters and addresses */
1147 ret
= vpif_set_video_params(vpif
, ch
->channel_id
+ 2);
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
));
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);
1166 if ((VPIF_CHANNEL3_VIDEO
== ch
->channel_id
)
1167 || (common
->started
== 2)) {
1168 channel3_intr_assert();
1169 channel3_intr_enable(1);
1172 channel_first_int
[VPIF_VIDEO_INDEX
][ch
->channel_id
] = 1;
1176 mutex_unlock(&common
->lock
);
1180 static int vpif_streamoff(struct file
*file
, void *priv
,
1181 enum v4l2_buf_type buftype
)
1183 struct vpif_fh
*fh
= priv
;
1184 struct channel_obj
*ch
= fh
->channel
;
1185 struct common_obj
*common
= &ch
->common
[VPIF_VIDEO_INDEX
];
1187 if (buftype
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1188 vpif_err("buffer type not supported\n");
1192 if (!fh
->io_allowed
[VPIF_VIDEO_INDEX
]) {
1193 vpif_err("fh->io_allowed\n");
1197 if (!common
->started
) {
1198 vpif_err("channel->started\n");
1202 if (mutex_lock_interruptible(&common
->lock
))
1203 return -ERESTARTSYS
;
1205 if (buftype
== V4L2_BUF_TYPE_VIDEO_OUTPUT
) {
1206 /* disable channel */
1207 if (VPIF_CHANNEL2_VIDEO
== ch
->channel_id
) {
1209 channel2_intr_enable(0);
1211 if ((VPIF_CHANNEL3_VIDEO
== ch
->channel_id
) ||
1212 (2 == common
->started
)) {
1214 channel3_intr_enable(0);
1218 common
->started
= 0;
1219 mutex_unlock(&common
->lock
);
1221 return videobuf_streamoff(&common
->buffer_queue
);
1224 static int vpif_cropcap(struct file
*file
, void *priv
,
1225 struct v4l2_cropcap
*crop
)
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
)
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
;
1241 static int vpif_enum_output(struct file
*file
, void *fh
,
1242 struct v4l2_output
*output
)
1245 struct vpif_display_config
*config
= vpif_dev
->platform_data
;
1247 if (output
->index
>= config
->output_count
) {
1248 vpif_dbg(1, debug
, "Invalid output index\n");
1252 strcpy(output
->name
, config
->output
[output
->index
]);
1253 output
->type
= V4L2_OUTPUT_TYPE_ANALOG
;
1254 output
->std
= DM646X_V4L2_STD
;
1259 static int vpif_s_output(struct file
*file
, void *priv
, unsigned int i
)
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
];
1267 if (mutex_lock_interruptible(&common
->lock
))
1268 return -ERESTARTSYS
;
1270 if (common
->started
) {
1271 vpif_err("Streaming in progress\n");
1276 ret
= v4l2_device_call_until_err(&vpif_obj
.v4l2_dev
, 1, video
,
1277 s_routing
, 0, i
, 0);
1280 vpif_err("Failed to set output standard\n");
1282 vid_ch
->output_id
= i
;
1285 mutex_unlock(&common
->lock
);
1289 static int vpif_g_output(struct file
*file
, void *priv
, unsigned int *i
)
1291 struct vpif_fh
*fh
= priv
;
1292 struct channel_obj
*ch
= fh
->channel
;
1293 struct video_obj
*vid_ch
= &ch
->video
;
1295 *i
= vid_ch
->output_id
;
1300 static int vpif_g_priority(struct file
*file
, void *priv
, enum v4l2_priority
*p
)
1302 struct vpif_fh
*fh
= priv
;
1303 struct channel_obj
*ch
= fh
->channel
;
1305 *p
= v4l2_prio_max(&ch
->prio
);
1310 static int vpif_s_priority(struct file
*file
, void *priv
, enum v4l2_priority p
)
1312 struct vpif_fh
*fh
= priv
;
1313 struct channel_obj
*ch
= fh
->channel
;
1315 return v4l2_prio_change(&ch
->prio
, &fh
->prio
, p
);
1318 /* vpif display ioctl operations */
1319 static 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
,
1341 static const struct v4l2_file_operations vpif_fops
= {
1342 .owner
= THIS_MODULE
,
1344 .release
= vpif_release
,
1345 .ioctl
= video_ioctl2
,
1350 static struct video_device vpif_video_template
= {
1353 .ioctl_ops
= &vpif_ioctl_ops
,
1354 .tvnorms
= DM646X_V4L2_STD
,
1355 .current_norm
= V4L2_STD_625_50
,
1359 /*Configure the channels, buffer sizei, request irq */
1360 static int initialize_vpif(void)
1362 int free_channel_objects_index
;
1363 int free_buffer_channel_index
;
1364 int free_buffer_index
;
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
;
1375 /* Set buffer size to min buffers size if invalid buffer size is
1377 if (ch2_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL2_VIDEO
])
1379 config_params
.min_bufsize
[VPIF_CHANNEL2_VIDEO
];
1380 if (ch3_bufsize
< config_params
.min_bufsize
[VPIF_CHANNEL3_VIDEO
])
1382 config_params
.min_bufsize
[VPIF_CHANNEL3_VIDEO
];
1384 config_params
.numbuffers
[VPIF_CHANNEL2_VIDEO
] = ch2_numbuffers
;
1386 if (ch2_numbuffers
) {
1387 config_params
.channel_bufsize
[VPIF_CHANNEL2_VIDEO
] =
1390 config_params
.numbuffers
[VPIF_CHANNEL3_VIDEO
] = ch3_numbuffers
;
1392 if (ch3_numbuffers
) {
1393 config_params
.channel_bufsize
[VPIF_CHANNEL3_VIDEO
] =
1397 /* Allocate memory for six channel objects */
1398 for (i
= 0; i
< VPIF_DISPLAY_MAX_DEVICES
; i
++) {
1400 kzalloc(sizeof(struct channel_obj
), GFP_KERNEL
);
1401 /* If memory allocation fails, return error */
1402 if (!vpif_obj
.dev
[i
]) {
1403 free_channel_objects_index
= i
;
1405 goto vpif_init_free_channel_objects
;
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];
1415 vpif_init_free_channel_objects
:
1416 for (j
= 0; j
< free_channel_objects_index
; j
++)
1417 kfree(vpif_obj
.dev
[j
]);
1422 * vpif_probe: This function creates device entries by register itself to the
1423 * V4L2 driver and initializes fields of each channel objects
1425 static __init
int vpif_probe(struct platform_device
*pdev
)
1427 struct vpif_subdev_info
*subdevdata
;
1428 struct vpif_display_config
*config
;
1429 int i
, j
= 0, k
, q
, m
, err
= 0;
1430 struct i2c_adapter
*i2c_adap
;
1431 struct common_obj
*common
;
1432 struct channel_obj
*ch
;
1433 struct video_device
*vfd
;
1434 struct resource
*res
;
1437 vpif_dev
= &pdev
->dev
;
1439 err
= initialize_vpif();
1442 v4l2_err(vpif_dev
->driver
, "Error initializing vpif\n");
1446 err
= v4l2_device_register(vpif_dev
, &vpif_obj
.v4l2_dev
);
1448 v4l2_err(vpif_dev
->driver
, "Error registering v4l2 device\n");
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
,
1457 (void *)(&vpif_obj
.dev
[k
]->channel_id
))) {
1465 for (i
= 0; i
< VPIF_DISPLAY_MAX_DEVICES
; i
++) {
1467 /* Get the pointer to the channel object */
1468 ch
= vpif_obj
.dev
[i
];
1470 /* Allocate memory for video device */
1471 vfd
= video_device_alloc();
1473 for (j
= 0; j
< i
; j
++) {
1474 ch
= vpif_obj
.dev
[j
];
1475 video_device_release(ch
->video_dev
);
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);
1491 /* Set video_dev to the video device */
1492 ch
->video_dev
= vfd
;
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
];
1515 ch
->initialized
= 0;
1518 ch
->common
[VPIF_VIDEO_INDEX
].numbuffers
=
1519 config_params
.numbuffers
[ch
->channel_id
];
1521 ch
->common
[VPIF_VIDEO_INDEX
].numbuffers
= 0;
1523 memset(&ch
->vpifparams
, 0, sizeof(ch
->vpifparams
));
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
;
1530 /* register video device */
1531 vpif_dbg(1, debug
, "channel=%x,channel->video_dev=%x\n",
1532 (int)ch
, (int)&ch
->video_dev
);
1534 err
= video_register_device(ch
->video_dev
,
1535 VFL_TYPE_GRABBER
, (j
? 3 : 2));
1539 video_set_drvdata(ch
->video_dev
, ch
);
1542 i2c_adap
= i2c_get_adapter(1);
1543 config
= pdev
->dev
.platform_data
;
1544 subdev_count
= config
->subdev_count
;
1545 subdevdata
= config
->subdevinfo
;
1546 vpif_obj
.sd
= kzalloc(sizeof(struct v4l2_subdev
*) * subdev_count
,
1548 if (vpif_obj
.sd
== NULL
) {
1549 vpif_err("unable to allocate memory for subdevice pointers\n");
1554 for (i
= 0; i
< subdev_count
; i
++) {
1555 vpif_obj
.sd
[i
] = v4l2_i2c_new_subdev_board(&vpif_obj
.v4l2_dev
,
1557 &subdevdata
[i
].board_info
,
1559 if (!vpif_obj
.sd
[i
]) {
1560 vpif_err("Error registering v4l2 subdevice\n");
1561 goto probe_subdev_out
;
1565 vpif_obj
.sd
[i
]->grp_id
= 1 << i
;
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
;
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);
1593 * vpif_remove: It un-register channels from V4L2 driver
1595 static int vpif_remove(struct platform_device
*device
)
1597 struct channel_obj
*ch
;
1600 v4l2_device_unregister(&vpif_obj
.v4l2_dev
);
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
);
1609 ch
->video_dev
= NULL
;
1615 static __refdata
struct platform_driver vpif_driver
= {
1617 .name
= "vpif_display",
1618 .owner
= THIS_MODULE
,
1620 .probe
= vpif_probe
,
1621 .remove
= vpif_remove
,
1624 static __init
int vpif_init(void)
1626 return platform_driver_register(&vpif_driver
);
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
1634 static void vpif_cleanup(void)
1636 struct platform_device
*pdev
;
1637 struct resource
*res
;
1641 pdev
= container_of(vpif_dev
, struct platform_device
, dev
);
1643 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
))) {
1644 for (irq_num
= res
->start
; irq_num
<= res
->end
; irq_num
++)
1646 (void *)(&vpif_obj
.dev
[i
]->channel_id
));
1650 platform_driver_unregister(&vpif_driver
);
1652 for (i
= 0; i
< VPIF_DISPLAY_MAX_DEVICES
; i
++)
1653 kfree(vpif_obj
.dev
[i
]);
1656 module_init(vpif_init
);
1657 module_exit(vpif_cleanup
);