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/slab.h>
37 #include <media/adv7343.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-chip-ident.h>
42 #include "vpif_display.h"
45 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(VPIF_DISPLAY_VERSION);
49 #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_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)
56 static u32 ch2_numbuffers = 3;
57 static u32 ch3_numbuffers = 3;
58 static u32 ch2_bufsize = 1920 * 1080 * 2;
59 static u32 ch3_bufsize = 720 * 576 * 2;
61 module_param(debug, int, 0644);
62 module_param(ch2_numbuffers, uint, S_IRUGO);
63 module_param(ch3_numbuffers, uint, S_IRUGO);
64 module_param(ch2_bufsize, uint, S_IRUGO);
65 module_param(ch3_bufsize, uint, S_IRUGO);
67 MODULE_PARM_DESC(debug, "Debug level 0-1");
68 MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
69 MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
70 MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
71 MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
73 static struct vpif_config_params config_params = {
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,
83 static struct vpif_device vpif_obj = { {NULL} };
84 static struct device *vpif_dev;
85 static void vpif_calculate_offsets(struct channel_obj *ch);
86 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
89 * buffer_prepare: This is the callback function called from vb2_qbuf()
90 * function the buffer is prepared and user space virtual address is converted
91 * into physical address
93 static int vpif_buffer_prepare(struct vb2_buffer *vb)
95 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
96 struct vb2_queue *q = vb->vb2_queue;
97 struct common_obj *common;
100 common = &fh->channel->common[VPIF_VIDEO_INDEX];
101 if (vb->state != VB2_BUF_STATE_ACTIVE &&
102 vb->state != VB2_BUF_STATE_PREPARED) {
103 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
104 if (vb2_plane_vaddr(vb, 0) &&
105 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
108 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
110 (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
111 if (!ISALIGNED(addr + common->ytop_off) ||
112 !ISALIGNED(addr + common->ybtm_off) ||
113 !ISALIGNED(addr + common->ctop_off) ||
114 !ISALIGNED(addr + common->cbtm_off))
121 vpif_err("buffer offset not aligned to 8 bytes\n");
126 * vpif_buffer_queue_setup: This function allocates memory for the buffers
128 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
129 const struct v4l2_format *fmt,
130 unsigned int *nbuffers, unsigned int *nplanes,
131 unsigned int sizes[], void *alloc_ctxs[])
133 struct vpif_fh *fh = vb2_get_drv_priv(vq);
134 struct channel_obj *ch = fh->channel;
135 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
138 if (V4L2_MEMORY_MMAP == common->memory) {
139 size = config_params.channel_bufsize[ch->channel_id];
141 * Checking if the buffer size exceeds the available buffer
142 * ycmux_mode = 0 means 1 channel mode HD and
143 * ycmux_mode = 1 means 2 channels mode SD
145 if (ch->vpifparams.std_info.ycmux_mode == 0) {
146 if (config_params.video_limit[ch->channel_id])
147 while (size * *nbuffers >
148 (config_params.video_limit[0]
149 + config_params.video_limit[1]))
152 if (config_params.video_limit[ch->channel_id])
153 while (size * *nbuffers >
154 config_params.video_limit[ch->channel_id])
158 size = common->fmt.fmt.pix.sizeimage;
161 if (*nbuffers < config_params.min_numbuffers)
162 *nbuffers = config_params.min_numbuffers;
166 alloc_ctxs[0] = common->alloc_ctx;
171 * vpif_buffer_queue: This function adds the buffer to DMA queue
173 static void vpif_buffer_queue(struct vb2_buffer *vb)
175 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
176 struct vpif_disp_buffer *buf = container_of(vb,
177 struct vpif_disp_buffer, vb);
178 struct channel_obj *ch = fh->channel;
179 struct common_obj *common;
181 common = &ch->common[VPIF_VIDEO_INDEX];
183 /* add the buffer to the DMA queue */
184 list_add_tail(&buf->list, &common->dma_queue);
188 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
189 * free memory allocated to the buffers
191 static void vpif_buf_cleanup(struct vb2_buffer *vb)
193 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
194 struct vpif_disp_buffer *buf = container_of(vb,
195 struct vpif_disp_buffer, vb);
196 struct channel_obj *ch = fh->channel;
197 struct common_obj *common;
200 common = &ch->common[VPIF_VIDEO_INDEX];
202 spin_lock_irqsave(&common->irqlock, flags);
203 if (vb->state == VB2_BUF_STATE_ACTIVE)
204 list_del_init(&buf->list);
205 spin_unlock_irqrestore(&common->irqlock, flags);
208 static void vpif_wait_prepare(struct vb2_queue *vq)
210 struct vpif_fh *fh = vb2_get_drv_priv(vq);
211 struct channel_obj *ch = fh->channel;
212 struct common_obj *common;
214 common = &ch->common[VPIF_VIDEO_INDEX];
215 mutex_unlock(&common->lock);
218 static void vpif_wait_finish(struct vb2_queue *vq)
220 struct vpif_fh *fh = vb2_get_drv_priv(vq);
221 struct channel_obj *ch = fh->channel;
222 struct common_obj *common;
224 common = &ch->common[VPIF_VIDEO_INDEX];
225 mutex_lock(&common->lock);
228 static int vpif_buffer_init(struct vb2_buffer *vb)
230 struct vpif_disp_buffer *buf = container_of(vb,
231 struct vpif_disp_buffer, vb);
233 INIT_LIST_HEAD(&buf->list);
238 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
240 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
242 struct vpif_display_config *vpif_config_data =
243 vpif_dev->platform_data;
244 struct vpif_fh *fh = vb2_get_drv_priv(vq);
245 struct channel_obj *ch = fh->channel;
246 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
247 struct vpif_params *vpif = &ch->vpifparams;
248 unsigned long addr = 0;
251 /* If buffer queue is empty, return error */
252 if (list_empty(&common->dma_queue)) {
253 vpif_err("buffer queue is empty\n");
257 /* Get the next frame from the buffer queue */
258 common->next_frm = common->cur_frm =
259 list_entry(common->dma_queue.next,
260 struct vpif_disp_buffer, list);
262 list_del(&common->cur_frm->list);
263 /* Mark state of the current frame to active */
264 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
266 /* Initialize field_id and started member */
269 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
270 /* Calculate the offset for Y and C data in the buffer */
271 vpif_calculate_offsets(ch);
273 if ((ch->vpifparams.std_info.frm_fmt &&
274 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
275 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
276 || (!ch->vpifparams.std_info.frm_fmt
277 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
278 vpif_err("conflict in field format and std format\n");
284 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
285 ch->vpifparams.std_info.hd_sd);
287 vpif_err("can't set clock\n");
291 /* set the parameters and addresses */
292 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
296 common->started = ret;
297 vpif_config_addr(ch, ret);
298 common->set_addr((addr + common->ytop_off),
299 (addr + common->ybtm_off),
300 (addr + common->ctop_off),
301 (addr + common->cbtm_off));
303 /* Set interrupt for both the fields in VPIF
304 Register enable channel in VPIF register */
305 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
306 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
307 channel2_intr_assert();
308 channel2_intr_enable(1);
310 if (vpif_config_data->ch2_clip_en)
311 channel2_clipping_enable(1);
314 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
315 || (common->started == 2)) {
316 channel3_intr_assert();
317 channel3_intr_enable(1);
319 if (vpif_config_data->ch3_clip_en)
320 channel3_clipping_enable(1);
326 /* abort streaming and wait for last buffer */
327 static int vpif_stop_streaming(struct vb2_queue *vq)
329 struct vpif_fh *fh = vb2_get_drv_priv(vq);
330 struct channel_obj *ch = fh->channel;
331 struct common_obj *common;
333 if (!vb2_is_streaming(vq))
336 common = &ch->common[VPIF_VIDEO_INDEX];
338 /* release all active buffers */
339 while (!list_empty(&common->dma_queue)) {
340 common->next_frm = list_entry(common->dma_queue.next,
341 struct vpif_disp_buffer, list);
342 list_del(&common->next_frm->list);
343 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
349 static struct vb2_ops video_qops = {
350 .queue_setup = vpif_buffer_queue_setup,
351 .wait_prepare = vpif_wait_prepare,
352 .wait_finish = vpif_wait_finish,
353 .buf_init = vpif_buffer_init,
354 .buf_prepare = vpif_buffer_prepare,
355 .start_streaming = vpif_start_streaming,
356 .stop_streaming = vpif_stop_streaming,
357 .buf_cleanup = vpif_buf_cleanup,
358 .buf_queue = vpif_buffer_queue,
361 static void process_progressive_mode(struct common_obj *common)
363 unsigned long addr = 0;
365 /* Get the next buffer from buffer queue */
366 common->next_frm = list_entry(common->dma_queue.next,
367 struct vpif_disp_buffer, list);
368 /* Remove that buffer from the buffer queue */
369 list_del(&common->next_frm->list);
370 /* Mark status of the buffer as active */
371 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
373 /* Set top and bottom field addrs in VPIF registers */
374 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
375 common->set_addr(addr + common->ytop_off,
376 addr + common->ybtm_off,
377 addr + common->ctop_off,
378 addr + common->cbtm_off);
381 static void process_interlaced_mode(int fid, struct common_obj *common)
383 /* device field id and local field id are in sync */
384 /* If this is even field */
386 if (common->cur_frm == common->next_frm)
389 /* one frame is displayed If next frame is
390 * available, release cur_frm and move on */
391 /* Copy frame display time */
392 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
393 /* Change status of the cur_frm */
394 vb2_buffer_done(&common->cur_frm->vb,
396 /* Make cur_frm pointing to next_frm */
397 common->cur_frm = common->next_frm;
399 } else if (1 == fid) { /* odd field */
400 if (list_empty(&common->dma_queue)
401 || (common->cur_frm != common->next_frm)) {
404 /* one field is displayed configure the next
405 * frame if it is available else hold on current
407 /* Get next from the buffer queue */
408 process_progressive_mode(common);
414 * vpif_channel_isr: It changes status of the displayed buffer, takes next
415 * buffer from the queue and sets its address in VPIF registers
417 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
419 struct vpif_device *dev = &vpif_obj;
420 struct channel_obj *ch;
421 struct common_obj *common;
422 enum v4l2_field field;
426 channel_id = *(int *)(dev_id);
427 if (!vpif_intr_status(channel_id + 2))
430 ch = dev->dev[channel_id];
431 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
432 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
433 common = &ch->common[i];
434 /* If streaming is started in this channel */
435 if (0 == common->started)
438 if (1 == ch->vpifparams.std_info.frm_fmt) {
439 if (list_empty(&common->dma_queue))
442 /* Progressive mode */
443 if (!channel_first_int[i][channel_id]) {
444 /* Mark status of the cur_frm to
445 * done and unlock semaphore on it */
446 do_gettimeofday(&common->cur_frm->vb.
448 vb2_buffer_done(&common->cur_frm->vb,
450 /* Make cur_frm pointing to next_frm */
451 common->cur_frm = common->next_frm;
454 channel_first_int[i][channel_id] = 0;
455 process_progressive_mode(common);
457 /* Interlaced mode */
458 /* If it is first interrupt, ignore it */
460 if (channel_first_int[i][channel_id]) {
461 channel_first_int[i][channel_id] = 0;
467 /* Get field id from VPIF registers */
468 fid = vpif_channel_getfid(ch->channel_id + 2);
469 /* If fid does not match with stored field id */
470 if (fid != ch->field_id) {
471 /* Make them in sync */
478 process_interlaced_mode(fid, common);
485 static int vpif_update_std_info(struct channel_obj *ch)
487 struct video_obj *vid_ch = &ch->video;
488 struct vpif_params *vpifparams = &ch->vpifparams;
489 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
490 const struct vpif_channel_config_params *config;
494 for (i = 0; i < vpif_ch_params_count; i++) {
495 config = &ch_params[i];
496 if (config->hd_sd == 0) {
497 vpif_dbg(2, debug, "SD format\n");
498 if (config->stdid & vid_ch->stdid) {
499 memcpy(std_info, config, sizeof(*config));
505 if (i == vpif_ch_params_count) {
506 vpif_dbg(1, debug, "Format not found\n");
513 static int vpif_update_resolution(struct channel_obj *ch)
515 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
516 struct video_obj *vid_ch = &ch->video;
517 struct vpif_params *vpifparams = &ch->vpifparams;
518 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
520 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
524 if (vpif_update_std_info(ch))
528 common->fmt.fmt.pix.width = std_info->width;
529 common->fmt.fmt.pix.height = std_info->height;
530 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
531 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
533 /* Set height and width paramateres */
534 common->height = std_info->height;
535 common->width = std_info->width;
541 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
542 * in the top and bottom field
544 static void vpif_calculate_offsets(struct channel_obj *ch)
546 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
547 struct vpif_params *vpifparams = &ch->vpifparams;
548 enum v4l2_field field = common->fmt.fmt.pix.field;
549 struct video_obj *vid_ch = &ch->video;
550 unsigned int hpitch, vpitch, sizeimage;
552 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
553 if (ch->vpifparams.std_info.frm_fmt)
554 vid_ch->buf_field = V4L2_FIELD_NONE;
556 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
558 vid_ch->buf_field = common->fmt.fmt.pix.field;
561 sizeimage = common->fmt.fmt.pix.sizeimage;
563 hpitch = common->fmt.fmt.pix.bytesperline;
564 vpitch = sizeimage / (hpitch * 2);
565 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
566 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
567 common->ytop_off = 0;
568 common->ybtm_off = hpitch;
569 common->ctop_off = sizeimage / 2;
570 common->cbtm_off = sizeimage / 2 + hpitch;
571 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
572 common->ytop_off = 0;
573 common->ybtm_off = sizeimage / 4;
574 common->ctop_off = sizeimage / 2;
575 common->cbtm_off = common->ctop_off + sizeimage / 4;
576 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
577 common->ybtm_off = 0;
578 common->ytop_off = sizeimage / 4;
579 common->cbtm_off = sizeimage / 2;
580 common->ctop_off = common->cbtm_off + sizeimage / 4;
583 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
584 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
585 vpifparams->video_params.storage_mode = 1;
587 vpifparams->video_params.storage_mode = 0;
590 if (ch->vpifparams.std_info.frm_fmt == 1) {
591 vpifparams->video_params.hpitch =
592 common->fmt.fmt.pix.bytesperline;
594 if ((field == V4L2_FIELD_ANY) ||
595 (field == V4L2_FIELD_INTERLACED))
596 vpifparams->video_params.hpitch =
597 common->fmt.fmt.pix.bytesperline * 2;
599 vpifparams->video_params.hpitch =
600 common->fmt.fmt.pix.bytesperline;
603 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
606 static void vpif_config_format(struct channel_obj *ch)
608 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
610 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
611 if (config_params.numbuffers[ch->channel_id] == 0)
612 common->memory = V4L2_MEMORY_USERPTR;
614 common->memory = V4L2_MEMORY_MMAP;
616 common->fmt.fmt.pix.sizeimage =
617 config_params.channel_bufsize[ch->channel_id];
618 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
619 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
622 static int vpif_check_format(struct channel_obj *ch,
623 struct v4l2_pix_format *pixfmt)
625 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
626 enum v4l2_field field = pixfmt->field;
627 u32 sizeimage, hpitch, vpitch;
629 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
630 goto invalid_fmt_exit;
632 if (!(VPIF_VALID_FIELD(field)))
633 goto invalid_fmt_exit;
635 if (pixfmt->bytesperline <= 0)
636 goto invalid_pitch_exit;
638 sizeimage = pixfmt->sizeimage;
640 if (vpif_update_resolution(ch))
643 hpitch = pixfmt->bytesperline;
644 vpitch = sizeimage / (hpitch * 2);
646 /* Check for valid value of pitch */
647 if ((hpitch < ch->vpifparams.std_info.width) ||
648 (vpitch < ch->vpifparams.std_info.height))
649 goto invalid_pitch_exit;
651 /* Check for 8 byte alignment */
652 if (!ISALIGNED(hpitch)) {
653 vpif_err("invalid pitch alignment\n");
656 pixfmt->width = common->fmt.fmt.pix.width;
657 pixfmt->height = common->fmt.fmt.pix.height;
662 vpif_err("invalid field format\n");
666 vpif_err("invalid pitch\n");
670 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
672 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
674 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
675 common->set_addr = ch3_set_videobuf_addr;
678 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
680 common->set_addr = ch2_set_videobuf_addr;
685 * vpif_mmap: It is used to map kernel space buffers into user spaces
687 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
689 struct vpif_fh *fh = filep->private_data;
690 struct channel_obj *ch = fh->channel;
691 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
694 vpif_dbg(2, debug, "vpif_mmap\n");
696 if (mutex_lock_interruptible(&common->lock))
698 ret = vb2_mmap(&common->buffer_queue, vma);
699 mutex_unlock(&common->lock);
704 * vpif_poll: It is used for select/poll system call
706 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
708 struct vpif_fh *fh = filep->private_data;
709 struct channel_obj *ch = fh->channel;
710 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
711 unsigned int res = 0;
713 if (common->started) {
714 mutex_lock(&common->lock);
715 res = vb2_poll(&common->buffer_queue, filep, wait);
716 mutex_unlock(&common->lock);
723 * vpif_open: It creates object of file handle structure and stores it in
724 * private_data member of filepointer
726 static int vpif_open(struct file *filep)
728 struct video_device *vdev = video_devdata(filep);
729 struct channel_obj *ch = video_get_drvdata(vdev);
730 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
733 /* Allocate memory for the file handle object */
734 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
736 vpif_err("unable to allocate memory for file handle object\n");
740 if (mutex_lock_interruptible(&common->lock)) {
744 /* store pointer to fh in private_data member of filep */
745 filep->private_data = fh;
748 if (!ch->initialized) {
751 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
754 /* Increment channel usrs counter */
755 atomic_inc(&ch->usrs);
756 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
757 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
758 /* Initialize priority of this instance to default priority */
759 fh->prio = V4L2_PRIORITY_UNSET;
760 v4l2_prio_open(&ch->prio, &fh->prio);
761 mutex_unlock(&common->lock);
767 * vpif_release: This function deletes buffer queue, frees the buffers and
768 * the vpif file handle
770 static int vpif_release(struct file *filep)
772 struct vpif_fh *fh = filep->private_data;
773 struct channel_obj *ch = fh->channel;
774 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
776 mutex_lock(&common->lock);
777 /* if this instance is doing IO */
778 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
779 /* Reset io_usrs member of channel object */
781 /* Disable channel */
782 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
784 channel2_intr_enable(0);
786 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
787 (2 == common->started)) {
789 channel3_intr_enable(0);
793 /* Free buffers allocated */
794 vb2_queue_release(&common->buffer_queue);
795 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
798 config_params.numbuffers[ch->channel_id];
801 /* Decrement channel usrs counter */
802 atomic_dec(&ch->usrs);
803 /* If this file handle has initialize encoder device, reset it */
807 /* Close the priority */
808 v4l2_prio_close(&ch->prio, fh->prio);
809 filep->private_data = NULL;
811 mutex_unlock(&common->lock);
817 /* functions implementing ioctls */
819 * vpif_querycap() - QUERYCAP handler
822 * @cap: ptr to v4l2_capability structure
824 static int vpif_querycap(struct file *file, void *priv,
825 struct v4l2_capability *cap)
827 struct vpif_display_config *config = vpif_dev->platform_data;
829 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
830 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
831 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
832 strlcpy(cap->card, config->card_name, sizeof(cap->card));
837 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
838 struct v4l2_fmtdesc *fmt)
840 if (fmt->index != 0) {
841 vpif_err("Invalid format index\n");
845 /* Fill in the information about format */
846 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
847 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
848 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
853 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
854 struct v4l2_format *fmt)
856 struct vpif_fh *fh = priv;
857 struct channel_obj *ch = fh->channel;
858 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
860 /* Check the validity of the buffer type */
861 if (common->fmt.type != fmt->type)
864 if (vpif_update_resolution(ch))
870 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
871 struct v4l2_format *fmt)
873 struct vpif_fh *fh = priv;
874 struct v4l2_pix_format *pixfmt;
875 struct channel_obj *ch = fh->channel;
876 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
879 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
880 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
881 if (!fh->initialized) {
882 vpif_dbg(1, debug, "Channel Busy\n");
886 /* Check for the priority */
887 ret = v4l2_prio_check(&ch->prio, fh->prio);
893 if (common->started) {
894 vpif_dbg(1, debug, "Streaming in progress\n");
898 pixfmt = &fmt->fmt.pix;
899 /* Check for valid field format */
900 ret = vpif_check_format(ch, pixfmt);
904 /* store the pix format in the channel object */
905 common->fmt.fmt.pix = *pixfmt;
906 /* store the format in the channel object */
911 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
912 struct v4l2_format *fmt)
914 struct vpif_fh *fh = priv;
915 struct channel_obj *ch = fh->channel;
916 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
917 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
920 ret = vpif_check_format(ch, pixfmt);
922 *pixfmt = common->fmt.fmt.pix;
923 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
929 static int vpif_reqbufs(struct file *file, void *priv,
930 struct v4l2_requestbuffers *reqbuf)
932 struct vpif_fh *fh = priv;
933 struct channel_obj *ch = fh->channel;
934 struct common_obj *common;
935 enum v4l2_field field;
939 /* This file handle has not initialized the channel,
940 It is not allowed to do settings */
941 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
942 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
943 if (!fh->initialized) {
944 vpif_err("Channel Busy\n");
949 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
952 index = VPIF_VIDEO_INDEX;
954 common = &ch->common[index];
956 if (common->fmt.type != reqbuf->type || !vpif_dev)
958 if (0 != common->io_usrs)
961 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
962 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
963 field = V4L2_FIELD_INTERLACED;
965 field = common->fmt.fmt.pix.field;
967 field = V4L2_VBI_INTERLACED;
969 /* Initialize videobuf2 queue as per the buffer type */
970 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
971 if (!common->alloc_ctx) {
972 vpif_err("Failed to get the context\n");
975 q = &common->buffer_queue;
976 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
977 q->io_modes = VB2_MMAP | VB2_USERPTR;
979 q->ops = &video_qops;
980 q->mem_ops = &vb2_dma_contig_memops;
981 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
985 /* Set io allowed member of file handle to TRUE */
986 fh->io_allowed[index] = 1;
987 /* Increment io usrs member of channel object to 1 */
989 /* Store type of memory requested in channel object */
990 common->memory = reqbuf->memory;
991 INIT_LIST_HEAD(&common->dma_queue);
992 /* Allocate buffers */
993 return vb2_reqbufs(&common->buffer_queue, reqbuf);
996 static int vpif_querybuf(struct file *file, void *priv,
997 struct v4l2_buffer *tbuf)
999 struct vpif_fh *fh = priv;
1000 struct channel_obj *ch = fh->channel;
1001 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1003 if (common->fmt.type != tbuf->type)
1006 return vb2_querybuf(&common->buffer_queue, tbuf);
1009 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1011 struct vpif_fh *fh = NULL;
1012 struct channel_obj *ch = NULL;
1013 struct common_obj *common = NULL;
1023 common = &(ch->common[VPIF_VIDEO_INDEX]);
1024 if (common->fmt.type != buf->type)
1027 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1028 vpif_err("fh->io_allowed\n");
1032 return vb2_qbuf(&common->buffer_queue, buf);
1035 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1037 struct vpif_fh *fh = priv;
1038 struct channel_obj *ch = fh->channel;
1039 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1042 if (!(*std_id & VPIF_V4L2_STD))
1045 if (common->started) {
1046 vpif_err("streaming in progress\n");
1050 /* Call encoder subdevice function to set the standard */
1051 ch->video.stdid = *std_id;
1052 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1053 /* Get the information about the standard */
1054 if (vpif_update_resolution(ch))
1057 if ((ch->vpifparams.std_info.width *
1058 ch->vpifparams.std_info.height * 2) >
1059 config_params.channel_bufsize[ch->channel_id]) {
1060 vpif_err("invalid std for this size\n");
1064 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1065 /* Configure the default format information */
1066 vpif_config_format(ch);
1068 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1069 s_std_output, *std_id);
1071 vpif_err("Failed to set output standard\n");
1075 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1078 vpif_err("Failed to set standard for sub devices\n");
1082 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1084 struct vpif_fh *fh = priv;
1085 struct channel_obj *ch = fh->channel;
1087 *std = ch->video.stdid;
1091 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1093 struct vpif_fh *fh = priv;
1094 struct channel_obj *ch = fh->channel;
1095 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1097 return vb2_dqbuf(&common->buffer_queue, p,
1098 (file->f_flags & O_NONBLOCK));
1101 static int vpif_streamon(struct file *file, void *priv,
1102 enum v4l2_buf_type buftype)
1104 struct vpif_fh *fh = priv;
1105 struct channel_obj *ch = fh->channel;
1106 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1107 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1110 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1111 vpif_err("buffer type not supported\n");
1115 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1116 vpif_err("fh->io_allowed\n");
1120 /* If Streaming is already started, return error */
1121 if (common->started) {
1122 vpif_err("channel->started\n");
1126 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1127 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1128 ch->vpifparams.std_info.ycmux_mode == 0)
1129 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1130 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1131 vpif_err("other channel is using\n");
1135 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1139 /* Call vb2_streamon to start streaming in videobuf2 */
1140 ret = vb2_streamon(&common->buffer_queue, buftype);
1142 vpif_err("vb2_streamon\n");
1149 static int vpif_streamoff(struct file *file, void *priv,
1150 enum v4l2_buf_type buftype)
1152 struct vpif_fh *fh = priv;
1153 struct channel_obj *ch = fh->channel;
1154 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1155 struct vpif_display_config *vpif_config_data =
1156 vpif_dev->platform_data;
1158 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1159 vpif_err("buffer type not supported\n");
1163 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1164 vpif_err("fh->io_allowed\n");
1168 if (!common->started) {
1169 vpif_err("channel->started\n");
1173 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1174 /* disable channel */
1175 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1176 if (vpif_config_data->ch2_clip_en)
1177 channel2_clipping_enable(0);
1179 channel2_intr_enable(0);
1181 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1182 (2 == common->started)) {
1183 if (vpif_config_data->ch3_clip_en)
1184 channel3_clipping_enable(0);
1186 channel3_intr_enable(0);
1190 common->started = 0;
1191 return vb2_streamoff(&common->buffer_queue, buftype);
1194 static int vpif_cropcap(struct file *file, void *priv,
1195 struct v4l2_cropcap *crop)
1197 struct vpif_fh *fh = priv;
1198 struct channel_obj *ch = fh->channel;
1199 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1200 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1203 crop->bounds.left = crop->bounds.top = 0;
1204 crop->defrect.left = crop->defrect.top = 0;
1205 crop->defrect.height = crop->bounds.height = common->height;
1206 crop->defrect.width = crop->bounds.width = common->width;
1211 static int vpif_enum_output(struct file *file, void *fh,
1212 struct v4l2_output *output)
1215 struct vpif_display_config *config = vpif_dev->platform_data;
1217 if (output->index >= config->output_count) {
1218 vpif_dbg(1, debug, "Invalid output index\n");
1222 strcpy(output->name, config->output[output->index]);
1223 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1224 output->std = VPIF_V4L2_STD;
1229 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1231 struct vpif_fh *fh = priv;
1232 struct channel_obj *ch = fh->channel;
1233 struct video_obj *vid_ch = &ch->video;
1234 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1237 if (common->started) {
1238 vpif_err("Streaming in progress\n");
1242 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1243 s_routing, 0, i, 0);
1246 vpif_err("Failed to set output standard\n");
1248 vid_ch->output_id = i;
1252 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1254 struct vpif_fh *fh = priv;
1255 struct channel_obj *ch = fh->channel;
1256 struct video_obj *vid_ch = &ch->video;
1258 *i = vid_ch->output_id;
1263 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1265 struct vpif_fh *fh = priv;
1266 struct channel_obj *ch = fh->channel;
1268 *p = v4l2_prio_max(&ch->prio);
1273 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1275 struct vpif_fh *fh = priv;
1276 struct channel_obj *ch = fh->channel;
1278 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1282 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1284 * @priv: file handle
1285 * @timings: input timings
1288 vpif_enum_dv_timings(struct file *file, void *priv,
1289 struct v4l2_enum_dv_timings *timings)
1291 struct vpif_fh *fh = priv;
1292 struct channel_obj *ch = fh->channel;
1293 struct video_obj *vid_ch = &ch->video;
1295 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1296 video, enum_dv_timings, timings);
1300 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1302 * @priv: file handle
1303 * @timings: digital video timings
1305 static int vpif_s_dv_timings(struct file *file, void *priv,
1306 struct v4l2_dv_timings *timings)
1308 struct vpif_fh *fh = priv;
1309 struct channel_obj *ch = fh->channel;
1310 struct vpif_params *vpifparams = &ch->vpifparams;
1311 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1312 struct video_obj *vid_ch = &ch->video;
1313 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1316 if (timings->type != V4L2_DV_BT_656_1120) {
1317 vpif_dbg(2, debug, "Timing type not defined\n");
1321 /* Configure subdevice timings, if any */
1322 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1323 video, s_dv_timings, timings);
1324 if (ret == -ENOIOCTLCMD) {
1325 vpif_dbg(2, debug, "Custom DV timings not supported by "
1330 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1334 if (!(timings->bt.width && timings->bt.height &&
1335 (timings->bt.hbackporch ||
1336 timings->bt.hfrontporch ||
1337 timings->bt.hsync) &&
1338 timings->bt.vfrontporch &&
1339 (timings->bt.vbackporch ||
1340 timings->bt.vsync))) {
1341 vpif_dbg(2, debug, "Timings for width, height, "
1342 "horizontal back porch, horizontal sync, "
1343 "horizontal front porch, vertical back porch, "
1344 "vertical sync and vertical back porch "
1345 "must be defined\n");
1349 vid_ch->dv_timings = *timings;
1351 /* Configure video port timings */
1353 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1355 std_info->sav2eav = bt->width;
1358 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1360 if (bt->interlaced) {
1361 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1362 std_info->vsize = bt->height * 2 +
1363 bt->vfrontporch + bt->vsync + bt->vbackporch +
1364 bt->il_vfrontporch + bt->il_vsync +
1366 std_info->l5 = std_info->vsize/2 -
1367 (bt->vfrontporch - 1);
1368 std_info->l7 = std_info->vsize/2 + 1;
1369 std_info->l9 = std_info->l7 + bt->il_vsync +
1370 bt->il_vbackporch + 1;
1371 std_info->l11 = std_info->vsize -
1372 (bt->il_vfrontporch - 1);
1374 vpif_dbg(2, debug, "Required timing values for "
1375 "interlaced BT format missing\n");
1379 std_info->vsize = bt->height + bt->vfrontporch +
1380 bt->vsync + bt->vbackporch;
1381 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1383 strncpy(std_info->name, "Custom timings BT656/1120",
1385 std_info->width = bt->width;
1386 std_info->height = bt->height;
1387 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1388 std_info->ycmux_mode = 0;
1389 std_info->capture_format = 0;
1390 std_info->vbi_supported = 0;
1391 std_info->hd_sd = 1;
1392 std_info->stdid = 0;
1399 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1401 * @priv: file handle
1402 * @timings: digital video timings
1404 static int vpif_g_dv_timings(struct file *file, void *priv,
1405 struct v4l2_dv_timings *timings)
1407 struct vpif_fh *fh = priv;
1408 struct channel_obj *ch = fh->channel;
1409 struct video_obj *vid_ch = &ch->video;
1411 *timings = vid_ch->dv_timings;
1417 * vpif_g_chip_ident() - Identify the chip
1419 * @priv: file handle
1420 * @chip: chip identity
1422 * Returns zero or -EINVAL if read operations fails.
1424 static int vpif_g_chip_ident(struct file *file, void *priv,
1425 struct v4l2_dbg_chip_ident *chip)
1427 chip->ident = V4L2_IDENT_NONE;
1429 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1430 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1431 vpif_dbg(2, debug, "match_type is invalid.\n");
1435 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1436 g_chip_ident, chip);
1439 #ifdef CONFIG_VIDEO_ADV_DEBUG
1441 * vpif_dbg_g_register() - Read register
1443 * @priv: file handle
1444 * @reg: register to be read
1447 * Returns zero or -EINVAL if read operations fails.
1449 static int vpif_dbg_g_register(struct file *file, void *priv,
1450 struct v4l2_dbg_register *reg){
1451 struct vpif_fh *fh = priv;
1452 struct channel_obj *ch = fh->channel;
1453 struct video_obj *vid_ch = &ch->video;
1455 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1460 * vpif_dbg_s_register() - Write to register
1462 * @priv: file handle
1463 * @reg: register to be modified
1466 * Returns zero or -EINVAL if write operations fails.
1468 static int vpif_dbg_s_register(struct file *file, void *priv,
1469 struct v4l2_dbg_register *reg){
1470 struct vpif_fh *fh = priv;
1471 struct channel_obj *ch = fh->channel;
1472 struct video_obj *vid_ch = &ch->video;
1474 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1480 * vpif_log_status() - Status information
1482 * @priv: file handle
1486 static int vpif_log_status(struct file *filep, void *priv)
1488 /* status for sub devices */
1489 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1494 /* vpif display ioctl operations */
1495 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1496 .vidioc_querycap = vpif_querycap,
1497 .vidioc_g_priority = vpif_g_priority,
1498 .vidioc_s_priority = vpif_s_priority,
1499 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1500 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1501 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1502 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1503 .vidioc_reqbufs = vpif_reqbufs,
1504 .vidioc_querybuf = vpif_querybuf,
1505 .vidioc_qbuf = vpif_qbuf,
1506 .vidioc_dqbuf = vpif_dqbuf,
1507 .vidioc_streamon = vpif_streamon,
1508 .vidioc_streamoff = vpif_streamoff,
1509 .vidioc_s_std = vpif_s_std,
1510 .vidioc_g_std = vpif_g_std,
1511 .vidioc_enum_output = vpif_enum_output,
1512 .vidioc_s_output = vpif_s_output,
1513 .vidioc_g_output = vpif_g_output,
1514 .vidioc_cropcap = vpif_cropcap,
1515 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1516 .vidioc_s_dv_timings = vpif_s_dv_timings,
1517 .vidioc_g_dv_timings = vpif_g_dv_timings,
1518 .vidioc_g_chip_ident = vpif_g_chip_ident,
1519 #ifdef CONFIG_VIDEO_ADV_DEBUG
1520 .vidioc_g_register = vpif_dbg_g_register,
1521 .vidioc_s_register = vpif_dbg_s_register,
1523 .vidioc_log_status = vpif_log_status,
1526 static const struct v4l2_file_operations vpif_fops = {
1527 .owner = THIS_MODULE,
1529 .release = vpif_release,
1530 .unlocked_ioctl = video_ioctl2,
1535 static struct video_device vpif_video_template = {
1538 .ioctl_ops = &vpif_ioctl_ops,
1539 .tvnorms = VPIF_V4L2_STD,
1540 .current_norm = V4L2_STD_625_50,
1544 /*Configure the channels, buffer sizei, request irq */
1545 static int initialize_vpif(void)
1547 int free_channel_objects_index;
1548 int free_buffer_channel_index;
1549 int free_buffer_index;
1552 /* Default number of buffers should be 3 */
1553 if ((ch2_numbuffers > 0) &&
1554 (ch2_numbuffers < config_params.min_numbuffers))
1555 ch2_numbuffers = config_params.min_numbuffers;
1556 if ((ch3_numbuffers > 0) &&
1557 (ch3_numbuffers < config_params.min_numbuffers))
1558 ch3_numbuffers = config_params.min_numbuffers;
1560 /* Set buffer size to min buffers size if invalid buffer size is
1562 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1564 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1565 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1567 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1569 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1571 if (ch2_numbuffers) {
1572 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1575 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1577 if (ch3_numbuffers) {
1578 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1582 /* Allocate memory for six channel objects */
1583 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1585 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1586 /* If memory allocation fails, return error */
1587 if (!vpif_obj.dev[i]) {
1588 free_channel_objects_index = i;
1590 goto vpif_init_free_channel_objects;
1594 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1595 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1596 free_buffer_index = config_params.numbuffers[i - 1];
1600 vpif_init_free_channel_objects:
1601 for (j = 0; j < free_channel_objects_index; j++)
1602 kfree(vpif_obj.dev[j]);
1607 * vpif_probe: This function creates device entries by register itself to the
1608 * V4L2 driver and initializes fields of each channel objects
1610 static __init int vpif_probe(struct platform_device *pdev)
1612 struct vpif_subdev_info *subdevdata;
1613 struct vpif_display_config *config;
1614 int i, j = 0, k, q, m, err = 0;
1615 struct i2c_adapter *i2c_adap;
1616 struct common_obj *common;
1617 struct channel_obj *ch;
1618 struct video_device *vfd;
1619 struct resource *res;
1623 vpif_dev = &pdev->dev;
1624 err = initialize_vpif();
1627 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1631 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1633 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1638 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1639 for (i = res->start; i <= res->end; i++) {
1640 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
1642 (void *)(&vpif_obj.dev[k]->channel_id))) {
1650 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1652 /* Get the pointer to the channel object */
1653 ch = vpif_obj.dev[i];
1655 /* Allocate memory for video device */
1656 vfd = video_device_alloc();
1658 for (j = 0; j < i; j++) {
1659 ch = vpif_obj.dev[j];
1660 video_device_release(ch->video_dev);
1666 /* Initialize field of video device */
1667 *vfd = vpif_video_template;
1668 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1669 vfd->release = video_device_release;
1670 vfd->vfl_dir = VFL_DIR_TX;
1671 snprintf(vfd->name, sizeof(vfd->name),
1672 "VPIF_Display_DRIVER_V%s",
1673 VPIF_DISPLAY_VERSION);
1675 /* Set video_dev to the video device */
1676 ch->video_dev = vfd;
1679 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1681 size = resource_size(res);
1682 /* The resources are divided into two equal memory and when
1683 * we have HD output we can add them together
1685 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1686 ch = vpif_obj.dev[j];
1689 /* only enabled if second resource exists */
1690 config_params.video_limit[ch->channel_id] = 0;
1692 config_params.video_limit[ch->channel_id] =
1697 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1698 ch = vpif_obj.dev[j];
1699 /* Initialize field of the channel objects */
1700 atomic_set(&ch->usrs, 0);
1701 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1702 ch->common[k].numbuffers = 0;
1703 common = &ch->common[k];
1704 common->io_usrs = 0;
1705 common->started = 0;
1706 spin_lock_init(&common->irqlock);
1707 mutex_init(&common->lock);
1708 common->numbuffers = 0;
1709 common->set_addr = NULL;
1710 common->ytop_off = common->ybtm_off = 0;
1711 common->ctop_off = common->cbtm_off = 0;
1712 common->cur_frm = common->next_frm = NULL;
1713 memset(&common->fmt, 0, sizeof(common->fmt));
1714 common->numbuffers = config_params.numbuffers[k];
1717 ch->initialized = 0;
1720 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1721 config_params.numbuffers[ch->channel_id];
1723 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1725 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1727 /* Initialize prio member of channel object */
1728 v4l2_prio_init(&ch->prio);
1729 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1730 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1731 ch->video_dev->lock = &common->lock;
1733 /* register video device */
1734 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1735 (int)ch, (int)&ch->video_dev);
1737 err = video_register_device(ch->video_dev,
1738 VFL_TYPE_GRABBER, (j ? 3 : 2));
1742 video_set_drvdata(ch->video_dev, ch);
1745 i2c_adap = i2c_get_adapter(1);
1746 config = pdev->dev.platform_data;
1747 subdev_count = config->subdev_count;
1748 subdevdata = config->subdevinfo;
1749 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1751 if (vpif_obj.sd == NULL) {
1752 vpif_err("unable to allocate memory for subdevice pointers\n");
1757 for (i = 0; i < subdev_count; i++) {
1758 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1760 &subdevdata[i].board_info,
1762 if (!vpif_obj.sd[i]) {
1763 vpif_err("Error registering v4l2 subdevice\n");
1764 goto probe_subdev_out;
1768 vpif_obj.sd[i]->grp_id = 1 << i;
1771 v4l2_info(&vpif_obj.v4l2_dev,
1772 " VPIF display driver initialized\n");
1778 for (k = 0; k < j; k++) {
1779 ch = vpif_obj.dev[k];
1780 video_unregister_device(ch->video_dev);
1781 video_device_release(ch->video_dev);
1782 ch->video_dev = NULL;
1785 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1786 vpif_err("VPIF IRQ request failed\n");
1787 for (q = k; k >= 0; k--) {
1788 for (m = i; m >= res->start; m--)
1789 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1790 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1798 * vpif_remove: It un-register channels from V4L2 driver
1800 static int vpif_remove(struct platform_device *device)
1802 struct channel_obj *ch;
1805 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1807 /* un-register device */
1808 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1809 /* Get the pointer to the channel object */
1810 ch = vpif_obj.dev[i];
1811 /* Unregister video device */
1812 video_unregister_device(ch->video_dev);
1814 ch->video_dev = NULL;
1821 static int vpif_suspend(struct device *dev)
1823 struct common_obj *common;
1824 struct channel_obj *ch;
1827 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1828 /* Get the pointer to the channel object */
1829 ch = vpif_obj.dev[i];
1830 common = &ch->common[VPIF_VIDEO_INDEX];
1831 mutex_lock(&common->lock);
1832 if (atomic_read(&ch->usrs) && common->io_usrs) {
1833 /* Disable channel */
1834 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1836 channel2_intr_enable(0);
1838 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1839 common->started == 2) {
1841 channel3_intr_enable(0);
1844 mutex_unlock(&common->lock);
1850 static int vpif_resume(struct device *dev)
1853 struct common_obj *common;
1854 struct channel_obj *ch;
1857 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1858 /* Get the pointer to the channel object */
1859 ch = vpif_obj.dev[i];
1860 common = &ch->common[VPIF_VIDEO_INDEX];
1861 mutex_lock(&common->lock);
1862 if (atomic_read(&ch->usrs) && common->io_usrs) {
1863 /* Enable channel */
1864 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1866 channel2_intr_enable(1);
1868 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1869 common->started == 2) {
1871 channel3_intr_enable(1);
1874 mutex_unlock(&common->lock);
1880 static const struct dev_pm_ops vpif_pm = {
1881 .suspend = vpif_suspend,
1882 .resume = vpif_resume,
1885 #define vpif_pm_ops (&vpif_pm)
1887 #define vpif_pm_ops NULL
1890 static __refdata struct platform_driver vpif_driver = {
1892 .name = "vpif_display",
1893 .owner = THIS_MODULE,
1896 .probe = vpif_probe,
1897 .remove = vpif_remove,
1900 static __init int vpif_init(void)
1902 return platform_driver_register(&vpif_driver);
1906 * vpif_cleanup: This function un-registers device and driver to the kernel,
1907 * frees requested irq handler and de-allocates memory allocated for channel
1910 static void vpif_cleanup(void)
1912 struct platform_device *pdev;
1913 struct resource *res;
1917 pdev = container_of(vpif_dev, struct platform_device, dev);
1919 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1920 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1922 (void *)(&vpif_obj.dev[i]->channel_id));
1926 platform_driver_unregister(&vpif_driver);
1928 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1929 kfree(vpif_obj.dev[i]);
1932 module_init(vpif_init);
1933 module_exit(vpif_cleanup);