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;
182 common = &ch->common[VPIF_VIDEO_INDEX];
184 /* add the buffer to the DMA queue */
185 spin_lock_irqsave(&common->irqlock, flags);
186 list_add_tail(&buf->list, &common->dma_queue);
187 spin_unlock_irqrestore(&common->irqlock, flags);
191 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
192 * free memory allocated to the buffers
194 static void vpif_buf_cleanup(struct vb2_buffer *vb)
196 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
197 struct vpif_disp_buffer *buf = container_of(vb,
198 struct vpif_disp_buffer, vb);
199 struct channel_obj *ch = fh->channel;
200 struct common_obj *common;
203 common = &ch->common[VPIF_VIDEO_INDEX];
205 spin_lock_irqsave(&common->irqlock, flags);
206 if (vb->state == VB2_BUF_STATE_ACTIVE)
207 list_del_init(&buf->list);
208 spin_unlock_irqrestore(&common->irqlock, flags);
211 static void vpif_wait_prepare(struct vb2_queue *vq)
213 struct vpif_fh *fh = vb2_get_drv_priv(vq);
214 struct channel_obj *ch = fh->channel;
215 struct common_obj *common;
217 common = &ch->common[VPIF_VIDEO_INDEX];
218 mutex_unlock(&common->lock);
221 static void vpif_wait_finish(struct vb2_queue *vq)
223 struct vpif_fh *fh = vb2_get_drv_priv(vq);
224 struct channel_obj *ch = fh->channel;
225 struct common_obj *common;
227 common = &ch->common[VPIF_VIDEO_INDEX];
228 mutex_lock(&common->lock);
231 static int vpif_buffer_init(struct vb2_buffer *vb)
233 struct vpif_disp_buffer *buf = container_of(vb,
234 struct vpif_disp_buffer, vb);
236 INIT_LIST_HEAD(&buf->list);
241 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
243 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
245 struct vpif_display_config *vpif_config_data =
246 vpif_dev->platform_data;
247 struct vpif_fh *fh = vb2_get_drv_priv(vq);
248 struct channel_obj *ch = fh->channel;
249 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
250 struct vpif_params *vpif = &ch->vpifparams;
251 unsigned long addr = 0;
255 /* If buffer queue is empty, return error */
256 spin_lock_irqsave(&common->irqlock, flags);
257 if (list_empty(&common->dma_queue)) {
258 spin_unlock_irqrestore(&common->irqlock, flags);
259 vpif_err("buffer queue is empty\n");
263 /* Get the next frame from the buffer queue */
264 common->next_frm = common->cur_frm =
265 list_entry(common->dma_queue.next,
266 struct vpif_disp_buffer, list);
268 list_del(&common->cur_frm->list);
269 spin_unlock_irqrestore(&common->irqlock, flags);
270 /* Mark state of the current frame to active */
271 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
273 /* Initialize field_id and started member */
276 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
277 /* Calculate the offset for Y and C data in the buffer */
278 vpif_calculate_offsets(ch);
280 if ((ch->vpifparams.std_info.frm_fmt &&
281 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
282 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
283 || (!ch->vpifparams.std_info.frm_fmt
284 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
285 vpif_err("conflict in field format and std format\n");
290 if (vpif_config_data->set_clock) {
291 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
292 ycmux_mode, ch->vpifparams.std_info.hd_sd);
294 vpif_err("can't set clock\n");
299 /* set the parameters and addresses */
300 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
304 common->started = ret;
305 vpif_config_addr(ch, ret);
306 common->set_addr((addr + common->ytop_off),
307 (addr + common->ybtm_off),
308 (addr + common->ctop_off),
309 (addr + common->cbtm_off));
311 /* Set interrupt for both the fields in VPIF
312 Register enable channel in VPIF register */
313 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
314 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
315 channel2_intr_assert();
316 channel2_intr_enable(1);
318 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
319 channel2_clipping_enable(1);
322 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
323 || (common->started == 2)) {
324 channel3_intr_assert();
325 channel3_intr_enable(1);
327 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
328 channel3_clipping_enable(1);
334 /* abort streaming and wait for last buffer */
335 static int vpif_stop_streaming(struct vb2_queue *vq)
337 struct vpif_fh *fh = vb2_get_drv_priv(vq);
338 struct channel_obj *ch = fh->channel;
339 struct common_obj *common;
342 if (!vb2_is_streaming(vq))
345 common = &ch->common[VPIF_VIDEO_INDEX];
347 /* release all active buffers */
348 spin_lock_irqsave(&common->irqlock, flags);
349 while (!list_empty(&common->dma_queue)) {
350 common->next_frm = list_entry(common->dma_queue.next,
351 struct vpif_disp_buffer, list);
352 list_del(&common->next_frm->list);
353 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
355 spin_unlock_irqrestore(&common->irqlock, flags);
360 static struct vb2_ops video_qops = {
361 .queue_setup = vpif_buffer_queue_setup,
362 .wait_prepare = vpif_wait_prepare,
363 .wait_finish = vpif_wait_finish,
364 .buf_init = vpif_buffer_init,
365 .buf_prepare = vpif_buffer_prepare,
366 .start_streaming = vpif_start_streaming,
367 .stop_streaming = vpif_stop_streaming,
368 .buf_cleanup = vpif_buf_cleanup,
369 .buf_queue = vpif_buffer_queue,
372 static void process_progressive_mode(struct common_obj *common)
374 unsigned long addr = 0;
376 spin_lock(&common->irqlock);
377 /* Get the next buffer from buffer queue */
378 common->next_frm = list_entry(common->dma_queue.next,
379 struct vpif_disp_buffer, list);
380 /* Remove that buffer from the buffer queue */
381 list_del(&common->next_frm->list);
382 spin_unlock(&common->irqlock);
383 /* Mark status of the buffer as active */
384 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
386 /* Set top and bottom field addrs in VPIF registers */
387 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
388 common->set_addr(addr + common->ytop_off,
389 addr + common->ybtm_off,
390 addr + common->ctop_off,
391 addr + common->cbtm_off);
394 static void process_interlaced_mode(int fid, struct common_obj *common)
396 /* device field id and local field id are in sync */
397 /* If this is even field */
399 if (common->cur_frm == common->next_frm)
402 /* one frame is displayed If next frame is
403 * available, release cur_frm and move on */
404 /* Copy frame display time */
405 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
406 /* Change status of the cur_frm */
407 vb2_buffer_done(&common->cur_frm->vb,
409 /* Make cur_frm pointing to next_frm */
410 common->cur_frm = common->next_frm;
412 } else if (1 == fid) { /* odd field */
413 spin_lock(&common->irqlock);
414 if (list_empty(&common->dma_queue)
415 || (common->cur_frm != common->next_frm)) {
416 spin_unlock(&common->irqlock);
419 spin_unlock(&common->irqlock);
420 /* one field is displayed configure the next
421 * frame if it is available else hold on current
423 /* Get next from the buffer queue */
424 process_progressive_mode(common);
429 * vpif_channel_isr: It changes status of the displayed buffer, takes next
430 * buffer from the queue and sets its address in VPIF registers
432 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
434 struct vpif_device *dev = &vpif_obj;
435 struct channel_obj *ch;
436 struct common_obj *common;
437 enum v4l2_field field;
441 channel_id = *(int *)(dev_id);
442 if (!vpif_intr_status(channel_id + 2))
445 ch = dev->dev[channel_id];
446 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
447 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
448 common = &ch->common[i];
449 /* If streaming is started in this channel */
450 if (0 == common->started)
453 if (1 == ch->vpifparams.std_info.frm_fmt) {
454 spin_lock(&common->irqlock);
455 if (list_empty(&common->dma_queue)) {
456 spin_unlock(&common->irqlock);
459 spin_unlock(&common->irqlock);
461 /* Progressive mode */
462 if (!channel_first_int[i][channel_id]) {
463 /* Mark status of the cur_frm to
464 * done and unlock semaphore on it */
465 v4l2_get_timestamp(&common->cur_frm->vb.
467 vb2_buffer_done(&common->cur_frm->vb,
469 /* Make cur_frm pointing to next_frm */
470 common->cur_frm = common->next_frm;
473 channel_first_int[i][channel_id] = 0;
474 process_progressive_mode(common);
476 /* Interlaced mode */
477 /* If it is first interrupt, ignore it */
479 if (channel_first_int[i][channel_id]) {
480 channel_first_int[i][channel_id] = 0;
486 /* Get field id from VPIF registers */
487 fid = vpif_channel_getfid(ch->channel_id + 2);
488 /* If fid does not match with stored field id */
489 if (fid != ch->field_id) {
490 /* Make them in sync */
497 process_interlaced_mode(fid, common);
504 static int vpif_update_std_info(struct channel_obj *ch)
506 struct video_obj *vid_ch = &ch->video;
507 struct vpif_params *vpifparams = &ch->vpifparams;
508 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
509 const struct vpif_channel_config_params *config;
513 for (i = 0; i < vpif_ch_params_count; i++) {
514 config = &vpif_ch_params[i];
515 if (config->hd_sd == 0) {
516 vpif_dbg(2, debug, "SD format\n");
517 if (config->stdid & vid_ch->stdid) {
518 memcpy(std_info, config, sizeof(*config));
524 if (i == vpif_ch_params_count) {
525 vpif_dbg(1, debug, "Format not found\n");
532 static int vpif_update_resolution(struct channel_obj *ch)
534 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
535 struct video_obj *vid_ch = &ch->video;
536 struct vpif_params *vpifparams = &ch->vpifparams;
537 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
539 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
543 if (vpif_update_std_info(ch))
547 common->fmt.fmt.pix.width = std_info->width;
548 common->fmt.fmt.pix.height = std_info->height;
549 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
550 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
552 /* Set height and width paramateres */
553 common->height = std_info->height;
554 common->width = std_info->width;
560 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
561 * in the top and bottom field
563 static void vpif_calculate_offsets(struct channel_obj *ch)
565 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
566 struct vpif_params *vpifparams = &ch->vpifparams;
567 enum v4l2_field field = common->fmt.fmt.pix.field;
568 struct video_obj *vid_ch = &ch->video;
569 unsigned int hpitch, vpitch, sizeimage;
571 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
572 if (ch->vpifparams.std_info.frm_fmt)
573 vid_ch->buf_field = V4L2_FIELD_NONE;
575 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
577 vid_ch->buf_field = common->fmt.fmt.pix.field;
580 sizeimage = common->fmt.fmt.pix.sizeimage;
582 hpitch = common->fmt.fmt.pix.bytesperline;
583 vpitch = sizeimage / (hpitch * 2);
584 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
585 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
586 common->ytop_off = 0;
587 common->ybtm_off = hpitch;
588 common->ctop_off = sizeimage / 2;
589 common->cbtm_off = sizeimage / 2 + hpitch;
590 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
591 common->ytop_off = 0;
592 common->ybtm_off = sizeimage / 4;
593 common->ctop_off = sizeimage / 2;
594 common->cbtm_off = common->ctop_off + sizeimage / 4;
595 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
596 common->ybtm_off = 0;
597 common->ytop_off = sizeimage / 4;
598 common->cbtm_off = sizeimage / 2;
599 common->ctop_off = common->cbtm_off + sizeimage / 4;
602 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
603 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
604 vpifparams->video_params.storage_mode = 1;
606 vpifparams->video_params.storage_mode = 0;
609 if (ch->vpifparams.std_info.frm_fmt == 1) {
610 vpifparams->video_params.hpitch =
611 common->fmt.fmt.pix.bytesperline;
613 if ((field == V4L2_FIELD_ANY) ||
614 (field == V4L2_FIELD_INTERLACED))
615 vpifparams->video_params.hpitch =
616 common->fmt.fmt.pix.bytesperline * 2;
618 vpifparams->video_params.hpitch =
619 common->fmt.fmt.pix.bytesperline;
622 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
625 static void vpif_config_format(struct channel_obj *ch)
627 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
629 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
630 if (config_params.numbuffers[ch->channel_id] == 0)
631 common->memory = V4L2_MEMORY_USERPTR;
633 common->memory = V4L2_MEMORY_MMAP;
635 common->fmt.fmt.pix.sizeimage =
636 config_params.channel_bufsize[ch->channel_id];
637 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
638 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
641 static int vpif_check_format(struct channel_obj *ch,
642 struct v4l2_pix_format *pixfmt)
644 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
645 enum v4l2_field field = pixfmt->field;
646 u32 sizeimage, hpitch, vpitch;
648 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
649 goto invalid_fmt_exit;
651 if (!(VPIF_VALID_FIELD(field)))
652 goto invalid_fmt_exit;
654 if (pixfmt->bytesperline <= 0)
655 goto invalid_pitch_exit;
657 sizeimage = pixfmt->sizeimage;
659 if (vpif_update_resolution(ch))
662 hpitch = pixfmt->bytesperline;
663 vpitch = sizeimage / (hpitch * 2);
665 /* Check for valid value of pitch */
666 if ((hpitch < ch->vpifparams.std_info.width) ||
667 (vpitch < ch->vpifparams.std_info.height))
668 goto invalid_pitch_exit;
670 /* Check for 8 byte alignment */
671 if (!ISALIGNED(hpitch)) {
672 vpif_err("invalid pitch alignment\n");
675 pixfmt->width = common->fmt.fmt.pix.width;
676 pixfmt->height = common->fmt.fmt.pix.height;
681 vpif_err("invalid field format\n");
685 vpif_err("invalid pitch\n");
689 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
691 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
693 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
694 common->set_addr = ch3_set_videobuf_addr;
697 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
699 common->set_addr = ch2_set_videobuf_addr;
704 * vpif_mmap: It is used to map kernel space buffers into user spaces
706 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
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]);
713 vpif_dbg(2, debug, "vpif_mmap\n");
715 if (mutex_lock_interruptible(&common->lock))
717 ret = vb2_mmap(&common->buffer_queue, vma);
718 mutex_unlock(&common->lock);
723 * vpif_poll: It is used for select/poll system call
725 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
727 struct vpif_fh *fh = filep->private_data;
728 struct channel_obj *ch = fh->channel;
729 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
730 unsigned int res = 0;
732 if (common->started) {
733 mutex_lock(&common->lock);
734 res = vb2_poll(&common->buffer_queue, filep, wait);
735 mutex_unlock(&common->lock);
742 * vpif_open: It creates object of file handle structure and stores it in
743 * private_data member of filepointer
745 static int vpif_open(struct file *filep)
747 struct video_device *vdev = video_devdata(filep);
748 struct channel_obj *ch = video_get_drvdata(vdev);
749 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
752 /* Allocate memory for the file handle object */
753 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
755 vpif_err("unable to allocate memory for file handle object\n");
759 if (mutex_lock_interruptible(&common->lock)) {
763 /* store pointer to fh in private_data member of filep */
764 filep->private_data = fh;
767 if (!ch->initialized) {
770 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
773 /* Increment channel usrs counter */
774 atomic_inc(&ch->usrs);
775 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
776 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
777 /* Initialize priority of this instance to default priority */
778 fh->prio = V4L2_PRIORITY_UNSET;
779 v4l2_prio_open(&ch->prio, &fh->prio);
780 mutex_unlock(&common->lock);
786 * vpif_release: This function deletes buffer queue, frees the buffers and
787 * the vpif file handle
789 static int vpif_release(struct file *filep)
791 struct vpif_fh *fh = filep->private_data;
792 struct channel_obj *ch = fh->channel;
793 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
795 mutex_lock(&common->lock);
796 /* if this instance is doing IO */
797 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
798 /* Reset io_usrs member of channel object */
800 /* Disable channel */
801 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
803 channel2_intr_enable(0);
805 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
806 (2 == common->started)) {
808 channel3_intr_enable(0);
812 /* Free buffers allocated */
813 vb2_queue_release(&common->buffer_queue);
814 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
817 config_params.numbuffers[ch->channel_id];
820 /* Decrement channel usrs counter */
821 atomic_dec(&ch->usrs);
822 /* If this file handle has initialize encoder device, reset it */
826 /* Close the priority */
827 v4l2_prio_close(&ch->prio, fh->prio);
828 filep->private_data = NULL;
830 mutex_unlock(&common->lock);
836 /* functions implementing ioctls */
838 * vpif_querycap() - QUERYCAP handler
841 * @cap: ptr to v4l2_capability structure
843 static int vpif_querycap(struct file *file, void *priv,
844 struct v4l2_capability *cap)
846 struct vpif_display_config *config = vpif_dev->platform_data;
848 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
849 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
850 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
851 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
853 strlcpy(cap->card, config->card_name, sizeof(cap->card));
858 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
859 struct v4l2_fmtdesc *fmt)
861 if (fmt->index != 0) {
862 vpif_err("Invalid format index\n");
866 /* Fill in the information about format */
867 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
868 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
869 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
874 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
875 struct v4l2_format *fmt)
877 struct vpif_fh *fh = priv;
878 struct channel_obj *ch = fh->channel;
879 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
881 /* Check the validity of the buffer type */
882 if (common->fmt.type != fmt->type)
885 if (vpif_update_resolution(ch))
891 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
892 struct v4l2_format *fmt)
894 struct vpif_fh *fh = priv;
895 struct v4l2_pix_format *pixfmt;
896 struct channel_obj *ch = fh->channel;
897 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
900 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
901 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
902 if (!fh->initialized) {
903 vpif_dbg(1, debug, "Channel Busy\n");
907 /* Check for the priority */
908 ret = v4l2_prio_check(&ch->prio, fh->prio);
914 if (common->started) {
915 vpif_dbg(1, debug, "Streaming in progress\n");
919 pixfmt = &fmt->fmt.pix;
920 /* Check for valid field format */
921 ret = vpif_check_format(ch, pixfmt);
925 /* store the pix format in the channel object */
926 common->fmt.fmt.pix = *pixfmt;
927 /* store the format in the channel object */
932 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
933 struct v4l2_format *fmt)
935 struct vpif_fh *fh = priv;
936 struct channel_obj *ch = fh->channel;
937 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
938 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
941 ret = vpif_check_format(ch, pixfmt);
943 *pixfmt = common->fmt.fmt.pix;
944 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
950 static int vpif_reqbufs(struct file *file, void *priv,
951 struct v4l2_requestbuffers *reqbuf)
953 struct vpif_fh *fh = priv;
954 struct channel_obj *ch = fh->channel;
955 struct common_obj *common;
956 enum v4l2_field field;
961 /* This file handle has not initialized the channel,
962 It is not allowed to do settings */
963 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
964 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
965 if (!fh->initialized) {
966 vpif_err("Channel Busy\n");
971 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
974 index = VPIF_VIDEO_INDEX;
976 common = &ch->common[index];
978 if (common->fmt.type != reqbuf->type || !vpif_dev)
980 if (0 != common->io_usrs)
983 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
984 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
985 field = V4L2_FIELD_INTERLACED;
987 field = common->fmt.fmt.pix.field;
989 field = V4L2_VBI_INTERLACED;
991 /* Initialize videobuf2 queue as per the buffer type */
992 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
993 if (IS_ERR(common->alloc_ctx)) {
994 vpif_err("Failed to get the context\n");
995 return PTR_ERR(common->alloc_ctx);
997 q = &common->buffer_queue;
998 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
999 q->io_modes = VB2_MMAP | VB2_USERPTR;
1001 q->ops = &video_qops;
1002 q->mem_ops = &vb2_dma_contig_memops;
1003 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1004 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1006 ret = vb2_queue_init(q);
1008 vpif_err("vpif_display: vb2_queue_init() failed\n");
1009 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1012 /* Set io allowed member of file handle to TRUE */
1013 fh->io_allowed[index] = 1;
1014 /* Increment io usrs member of channel object to 1 */
1015 common->io_usrs = 1;
1016 /* Store type of memory requested in channel object */
1017 common->memory = reqbuf->memory;
1018 INIT_LIST_HEAD(&common->dma_queue);
1019 /* Allocate buffers */
1020 return vb2_reqbufs(&common->buffer_queue, reqbuf);
1023 static int vpif_querybuf(struct file *file, void *priv,
1024 struct v4l2_buffer *tbuf)
1026 struct vpif_fh *fh = priv;
1027 struct channel_obj *ch = fh->channel;
1028 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1030 if (common->fmt.type != tbuf->type)
1033 return vb2_querybuf(&common->buffer_queue, tbuf);
1036 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1038 struct vpif_fh *fh = NULL;
1039 struct channel_obj *ch = NULL;
1040 struct common_obj *common = NULL;
1050 common = &(ch->common[VPIF_VIDEO_INDEX]);
1051 if (common->fmt.type != buf->type)
1054 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1055 vpif_err("fh->io_allowed\n");
1059 return vb2_qbuf(&common->buffer_queue, buf);
1062 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1064 struct vpif_fh *fh = priv;
1065 struct channel_obj *ch = fh->channel;
1066 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1069 if (!(std_id & VPIF_V4L2_STD))
1072 if (common->started) {
1073 vpif_err("streaming in progress\n");
1077 /* Call encoder subdevice function to set the standard */
1078 ch->video.stdid = std_id;
1079 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1080 /* Get the information about the standard */
1081 if (vpif_update_resolution(ch))
1084 if ((ch->vpifparams.std_info.width *
1085 ch->vpifparams.std_info.height * 2) >
1086 config_params.channel_bufsize[ch->channel_id]) {
1087 vpif_err("invalid std for this size\n");
1091 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1092 /* Configure the default format information */
1093 vpif_config_format(ch);
1095 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1096 s_std_output, std_id);
1098 vpif_err("Failed to set output standard\n");
1102 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1105 vpif_err("Failed to set standard for sub devices\n");
1109 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1111 struct vpif_fh *fh = priv;
1112 struct channel_obj *ch = fh->channel;
1114 *std = ch->video.stdid;
1118 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1120 struct vpif_fh *fh = priv;
1121 struct channel_obj *ch = fh->channel;
1122 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1124 return vb2_dqbuf(&common->buffer_queue, p,
1125 (file->f_flags & O_NONBLOCK));
1128 static int vpif_streamon(struct file *file, void *priv,
1129 enum v4l2_buf_type buftype)
1131 struct vpif_fh *fh = priv;
1132 struct channel_obj *ch = fh->channel;
1133 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1134 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1137 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1138 vpif_err("buffer type not supported\n");
1142 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1143 vpif_err("fh->io_allowed\n");
1147 /* If Streaming is already started, return error */
1148 if (common->started) {
1149 vpif_err("channel->started\n");
1153 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1154 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1155 ch->vpifparams.std_info.ycmux_mode == 0)
1156 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1157 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1158 vpif_err("other channel is using\n");
1162 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1166 /* Call vb2_streamon to start streaming in videobuf2 */
1167 ret = vb2_streamon(&common->buffer_queue, buftype);
1169 vpif_err("vb2_streamon\n");
1176 static int vpif_streamoff(struct file *file, void *priv,
1177 enum v4l2_buf_type buftype)
1179 struct vpif_fh *fh = priv;
1180 struct channel_obj *ch = fh->channel;
1181 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1182 struct vpif_display_config *vpif_config_data =
1183 vpif_dev->platform_data;
1185 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1186 vpif_err("buffer type not supported\n");
1190 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1191 vpif_err("fh->io_allowed\n");
1195 if (!common->started) {
1196 vpif_err("channel->started\n");
1200 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1201 /* disable channel */
1202 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1203 if (vpif_config_data->
1204 chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
1205 channel2_clipping_enable(0);
1207 channel2_intr_enable(0);
1209 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1210 (2 == common->started)) {
1211 if (vpif_config_data->
1212 chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
1213 channel3_clipping_enable(0);
1215 channel3_intr_enable(0);
1219 common->started = 0;
1220 return vb2_streamoff(&common->buffer_queue, buftype);
1223 static int vpif_cropcap(struct file *file, void *priv,
1224 struct v4l2_cropcap *crop)
1226 struct vpif_fh *fh = priv;
1227 struct channel_obj *ch = fh->channel;
1228 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1229 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1232 crop->bounds.left = crop->bounds.top = 0;
1233 crop->defrect.left = crop->defrect.top = 0;
1234 crop->defrect.height = crop->bounds.height = common->height;
1235 crop->defrect.width = crop->bounds.width = common->width;
1240 static int vpif_enum_output(struct file *file, void *fh,
1241 struct v4l2_output *output)
1244 struct vpif_display_config *config = vpif_dev->platform_data;
1245 struct vpif_display_chan_config *chan_cfg;
1246 struct vpif_fh *vpif_handler = fh;
1247 struct channel_obj *ch = vpif_handler->channel;
1249 chan_cfg = &config->chan_config[ch->channel_id];
1250 if (output->index >= chan_cfg->output_count) {
1251 vpif_dbg(1, debug, "Invalid output index\n");
1255 *output = chan_cfg->outputs[output->index].output;
1260 * vpif_output_to_subdev() - Maps output to sub device
1261 * @vpif_cfg - global config ptr
1262 * @chan_cfg - channel config ptr
1263 * @index - Given output index from application
1265 * lookup the sub device information for a given output index.
1266 * we report all the output to application. output table also
1267 * has sub device name for the each output
1270 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
1271 struct vpif_display_chan_config *chan_cfg, int index)
1273 struct vpif_subdev_info *subdev_info;
1274 const char *subdev_name;
1277 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
1279 if (chan_cfg->outputs == NULL)
1282 subdev_name = chan_cfg->outputs[index].subdev_name;
1283 if (subdev_name == NULL)
1286 /* loop through the sub device list to get the sub device info */
1287 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1288 subdev_info = &vpif_cfg->subdevinfo[i];
1289 if (!strcmp(subdev_info->name, subdev_name))
1296 * vpif_set_output() - Select an output
1297 * @vpif_cfg - global config ptr
1299 * @index - Given output index from application
1301 * Select the given output.
1303 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
1304 struct channel_obj *ch, int index)
1306 struct vpif_display_chan_config *chan_cfg =
1307 &vpif_cfg->chan_config[ch->channel_id];
1308 struct vpif_subdev_info *subdev_info = NULL;
1309 struct v4l2_subdev *sd = NULL;
1310 u32 input = 0, output = 0;
1314 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
1315 if (sd_index >= 0) {
1316 sd = vpif_obj.sd[sd_index];
1317 subdev_info = &vpif_cfg->subdevinfo[sd_index];
1321 input = chan_cfg->outputs[index].input_route;
1322 output = chan_cfg->outputs[index].output_route;
1323 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1324 if (ret < 0 && ret != -ENOIOCTLCMD) {
1325 vpif_err("Failed to set output\n");
1330 ch->output_idx = index;
1332 if (chan_cfg->outputs != NULL)
1333 /* update tvnorms from the sub device output info */
1334 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
1338 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1340 struct vpif_display_config *config = vpif_dev->platform_data;
1341 struct vpif_display_chan_config *chan_cfg;
1342 struct vpif_fh *fh = priv;
1343 struct channel_obj *ch = fh->channel;
1344 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1346 chan_cfg = &config->chan_config[ch->channel_id];
1348 if (i >= chan_cfg->output_count)
1351 if (common->started) {
1352 vpif_err("Streaming in progress\n");
1356 return vpif_set_output(config, ch, i);
1359 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1361 struct vpif_fh *fh = priv;
1362 struct channel_obj *ch = fh->channel;
1364 *i = ch->output_idx;
1369 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1371 struct vpif_fh *fh = priv;
1372 struct channel_obj *ch = fh->channel;
1374 *p = v4l2_prio_max(&ch->prio);
1379 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1381 struct vpif_fh *fh = priv;
1382 struct channel_obj *ch = fh->channel;
1384 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1388 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1390 * @priv: file handle
1391 * @timings: input timings
1394 vpif_enum_dv_timings(struct file *file, void *priv,
1395 struct v4l2_enum_dv_timings *timings)
1397 struct vpif_fh *fh = priv;
1398 struct channel_obj *ch = fh->channel;
1401 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1402 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1408 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1410 * @priv: file handle
1411 * @timings: digital video timings
1413 static int vpif_s_dv_timings(struct file *file, void *priv,
1414 struct v4l2_dv_timings *timings)
1416 struct vpif_fh *fh = priv;
1417 struct channel_obj *ch = fh->channel;
1418 struct vpif_params *vpifparams = &ch->vpifparams;
1419 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1420 struct video_obj *vid_ch = &ch->video;
1421 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1424 if (timings->type != V4L2_DV_BT_656_1120) {
1425 vpif_dbg(2, debug, "Timing type not defined\n");
1429 /* Configure subdevice timings, if any */
1430 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1431 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1434 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1438 if (!(timings->bt.width && timings->bt.height &&
1439 (timings->bt.hbackporch ||
1440 timings->bt.hfrontporch ||
1441 timings->bt.hsync) &&
1442 timings->bt.vfrontporch &&
1443 (timings->bt.vbackporch ||
1444 timings->bt.vsync))) {
1445 vpif_dbg(2, debug, "Timings for width, height, "
1446 "horizontal back porch, horizontal sync, "
1447 "horizontal front porch, vertical back porch, "
1448 "vertical sync and vertical back porch "
1449 "must be defined\n");
1453 vid_ch->dv_timings = *timings;
1455 /* Configure video port timings */
1457 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1459 std_info->sav2eav = bt->width;
1462 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1464 if (bt->interlaced) {
1465 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1466 std_info->vsize = bt->height * 2 +
1467 bt->vfrontporch + bt->vsync + bt->vbackporch +
1468 bt->il_vfrontporch + bt->il_vsync +
1470 std_info->l5 = std_info->vsize/2 -
1471 (bt->vfrontporch - 1);
1472 std_info->l7 = std_info->vsize/2 + 1;
1473 std_info->l9 = std_info->l7 + bt->il_vsync +
1474 bt->il_vbackporch + 1;
1475 std_info->l11 = std_info->vsize -
1476 (bt->il_vfrontporch - 1);
1478 vpif_dbg(2, debug, "Required timing values for "
1479 "interlaced BT format missing\n");
1483 std_info->vsize = bt->height + bt->vfrontporch +
1484 bt->vsync + bt->vbackporch;
1485 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1487 strncpy(std_info->name, "Custom timings BT656/1120",
1489 std_info->width = bt->width;
1490 std_info->height = bt->height;
1491 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1492 std_info->ycmux_mode = 0;
1493 std_info->capture_format = 0;
1494 std_info->vbi_supported = 0;
1495 std_info->hd_sd = 1;
1496 std_info->stdid = 0;
1503 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1505 * @priv: file handle
1506 * @timings: digital video timings
1508 static int vpif_g_dv_timings(struct file *file, void *priv,
1509 struct v4l2_dv_timings *timings)
1511 struct vpif_fh *fh = priv;
1512 struct channel_obj *ch = fh->channel;
1513 struct video_obj *vid_ch = &ch->video;
1515 *timings = vid_ch->dv_timings;
1521 * vpif_g_chip_ident() - Identify the chip
1523 * @priv: file handle
1524 * @chip: chip identity
1526 * Returns zero or -EINVAL if read operations fails.
1528 static int vpif_g_chip_ident(struct file *file, void *priv,
1529 struct v4l2_dbg_chip_ident *chip)
1531 chip->ident = V4L2_IDENT_NONE;
1533 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1534 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1535 vpif_dbg(2, debug, "match_type is invalid.\n");
1539 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1540 g_chip_ident, chip);
1543 #ifdef CONFIG_VIDEO_ADV_DEBUG
1545 * vpif_dbg_g_register() - Read register
1547 * @priv: file handle
1548 * @reg: register to be read
1551 * Returns zero or -EINVAL if read operations fails.
1553 static int vpif_dbg_g_register(struct file *file, void *priv,
1554 struct v4l2_dbg_register *reg){
1555 struct vpif_fh *fh = priv;
1556 struct channel_obj *ch = fh->channel;
1558 return v4l2_subdev_call(ch->sd, core, g_register, reg);
1562 * vpif_dbg_s_register() - Write to register
1564 * @priv: file handle
1565 * @reg: register to be modified
1568 * Returns zero or -EINVAL if write operations fails.
1570 static int vpif_dbg_s_register(struct file *file, void *priv,
1571 const struct v4l2_dbg_register *reg)
1573 struct vpif_fh *fh = priv;
1574 struct channel_obj *ch = fh->channel;
1576 return v4l2_subdev_call(ch->sd, core, s_register, reg);
1581 * vpif_log_status() - Status information
1583 * @priv: file handle
1587 static int vpif_log_status(struct file *filep, void *priv)
1589 /* status for sub devices */
1590 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1595 /* vpif display ioctl operations */
1596 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1597 .vidioc_querycap = vpif_querycap,
1598 .vidioc_g_priority = vpif_g_priority,
1599 .vidioc_s_priority = vpif_s_priority,
1600 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1601 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1602 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1603 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1604 .vidioc_reqbufs = vpif_reqbufs,
1605 .vidioc_querybuf = vpif_querybuf,
1606 .vidioc_qbuf = vpif_qbuf,
1607 .vidioc_dqbuf = vpif_dqbuf,
1608 .vidioc_streamon = vpif_streamon,
1609 .vidioc_streamoff = vpif_streamoff,
1610 .vidioc_s_std = vpif_s_std,
1611 .vidioc_g_std = vpif_g_std,
1612 .vidioc_enum_output = vpif_enum_output,
1613 .vidioc_s_output = vpif_s_output,
1614 .vidioc_g_output = vpif_g_output,
1615 .vidioc_cropcap = vpif_cropcap,
1616 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1617 .vidioc_s_dv_timings = vpif_s_dv_timings,
1618 .vidioc_g_dv_timings = vpif_g_dv_timings,
1619 .vidioc_g_chip_ident = vpif_g_chip_ident,
1620 #ifdef CONFIG_VIDEO_ADV_DEBUG
1621 .vidioc_g_register = vpif_dbg_g_register,
1622 .vidioc_s_register = vpif_dbg_s_register,
1624 .vidioc_log_status = vpif_log_status,
1627 static const struct v4l2_file_operations vpif_fops = {
1628 .owner = THIS_MODULE,
1630 .release = vpif_release,
1631 .unlocked_ioctl = video_ioctl2,
1636 static struct video_device vpif_video_template = {
1639 .ioctl_ops = &vpif_ioctl_ops,
1642 /*Configure the channels, buffer sizei, request irq */
1643 static int initialize_vpif(void)
1645 int free_channel_objects_index;
1646 int free_buffer_channel_index;
1647 int free_buffer_index;
1650 /* Default number of buffers should be 3 */
1651 if ((ch2_numbuffers > 0) &&
1652 (ch2_numbuffers < config_params.min_numbuffers))
1653 ch2_numbuffers = config_params.min_numbuffers;
1654 if ((ch3_numbuffers > 0) &&
1655 (ch3_numbuffers < config_params.min_numbuffers))
1656 ch3_numbuffers = config_params.min_numbuffers;
1658 /* Set buffer size to min buffers size if invalid buffer size is
1660 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1662 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1663 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1665 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1667 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1669 if (ch2_numbuffers) {
1670 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1673 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1675 if (ch3_numbuffers) {
1676 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1680 /* Allocate memory for six channel objects */
1681 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1683 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1684 /* If memory allocation fails, return error */
1685 if (!vpif_obj.dev[i]) {
1686 free_channel_objects_index = i;
1688 goto vpif_init_free_channel_objects;
1692 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1693 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1694 free_buffer_index = config_params.numbuffers[i - 1];
1698 vpif_init_free_channel_objects:
1699 for (j = 0; j < free_channel_objects_index; j++)
1700 kfree(vpif_obj.dev[j]);
1705 * vpif_probe: This function creates device entries by register itself to the
1706 * V4L2 driver and initializes fields of each channel objects
1708 static __init int vpif_probe(struct platform_device *pdev)
1710 struct vpif_subdev_info *subdevdata;
1711 struct vpif_display_config *config;
1712 int i, j = 0, k, err = 0;
1714 struct i2c_adapter *i2c_adap;
1715 struct common_obj *common;
1716 struct channel_obj *ch;
1717 struct video_device *vfd;
1718 struct resource *res;
1722 vpif_dev = &pdev->dev;
1723 err = initialize_vpif();
1726 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1730 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1732 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1736 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1737 for (i = res->start; i <= res->end; i++) {
1738 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
1739 "VPIF_Display", (void *)
1740 (&vpif_obj.dev[res_idx]->channel_id))) {
1742 for (j = 0; j < i; j++)
1743 free_irq(j, (void *)
1744 (&vpif_obj.dev[res_idx]->channel_id));
1751 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1752 /* Get the pointer to the channel object */
1753 ch = vpif_obj.dev[i];
1755 /* Allocate memory for video device */
1756 vfd = video_device_alloc();
1758 for (j = 0; j < i; j++) {
1759 ch = vpif_obj.dev[j];
1760 video_device_release(ch->video_dev);
1766 /* Initialize field of video device */
1767 *vfd = vpif_video_template;
1768 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1769 vfd->release = video_device_release;
1770 vfd->vfl_dir = VFL_DIR_TX;
1771 snprintf(vfd->name, sizeof(vfd->name),
1772 "VPIF_Display_DRIVER_V%s",
1773 VPIF_DISPLAY_VERSION);
1775 /* Set video_dev to the video device */
1776 ch->video_dev = vfd;
1779 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1781 size = resource_size(res);
1782 /* The resources are divided into two equal memory and when
1783 * we have HD output we can add them together
1785 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1786 ch = vpif_obj.dev[j];
1789 /* only enabled if second resource exists */
1790 config_params.video_limit[ch->channel_id] = 0;
1792 config_params.video_limit[ch->channel_id] =
1797 i2c_adap = i2c_get_adapter(1);
1798 config = pdev->dev.platform_data;
1799 subdev_count = config->subdev_count;
1800 subdevdata = config->subdevinfo;
1801 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1803 if (vpif_obj.sd == NULL) {
1804 vpif_err("unable to allocate memory for subdevice pointers\n");
1809 for (i = 0; i < subdev_count; i++) {
1810 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1812 &subdevdata[i].board_info,
1814 if (!vpif_obj.sd[i]) {
1815 vpif_err("Error registering v4l2 subdevice\n");
1816 goto probe_subdev_out;
1820 vpif_obj.sd[i]->grp_id = 1 << i;
1823 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1824 ch = vpif_obj.dev[j];
1825 /* Initialize field of the channel objects */
1826 atomic_set(&ch->usrs, 0);
1827 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1828 ch->common[k].numbuffers = 0;
1829 common = &ch->common[k];
1830 common->io_usrs = 0;
1831 common->started = 0;
1832 spin_lock_init(&common->irqlock);
1833 mutex_init(&common->lock);
1834 common->numbuffers = 0;
1835 common->set_addr = NULL;
1836 common->ytop_off = common->ybtm_off = 0;
1837 common->ctop_off = common->cbtm_off = 0;
1838 common->cur_frm = common->next_frm = NULL;
1839 memset(&common->fmt, 0, sizeof(common->fmt));
1840 common->numbuffers = config_params.numbuffers[k];
1843 ch->initialized = 0;
1845 ch->sd = vpif_obj.sd[0];
1848 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1849 config_params.numbuffers[ch->channel_id];
1851 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1853 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1855 /* Initialize prio member of channel object */
1856 v4l2_prio_init(&ch->prio);
1857 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1858 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1859 ch->video_dev->lock = &common->lock;
1860 video_set_drvdata(ch->video_dev, ch);
1862 /* select output 0 */
1863 err = vpif_set_output(config, ch, 0);
1867 /* register video device */
1868 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1869 (int)ch, (int)&ch->video_dev);
1871 err = video_register_device(ch->video_dev,
1872 VFL_TYPE_GRABBER, (j ? 3 : 2));
1877 v4l2_info(&vpif_obj.v4l2_dev,
1878 " VPIF display driver initialized\n");
1882 for (k = 0; k < j; k++) {
1883 ch = vpif_obj.dev[k];
1884 video_unregister_device(ch->video_dev);
1885 video_device_release(ch->video_dev);
1886 ch->video_dev = NULL;
1891 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1892 ch = vpif_obj.dev[i];
1893 /* Note: does nothing if ch->video_dev == NULL */
1894 video_device_release(ch->video_dev);
1897 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1898 vpif_err("VPIF IRQ request failed\n");
1899 for (i = 0; i < res_idx; i++) {
1900 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1901 for (j = res->start; j <= res->end; j++)
1902 free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
1909 * vpif_remove: It un-register channels from V4L2 driver
1911 static int vpif_remove(struct platform_device *device)
1913 struct channel_obj *ch;
1916 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1918 /* un-register device */
1919 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1920 /* Get the pointer to the channel object */
1921 ch = vpif_obj.dev[i];
1922 /* Unregister video device */
1923 video_unregister_device(ch->video_dev);
1925 ch->video_dev = NULL;
1932 static int vpif_suspend(struct device *dev)
1934 struct common_obj *common;
1935 struct channel_obj *ch;
1938 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1939 /* Get the pointer to the channel object */
1940 ch = vpif_obj.dev[i];
1941 common = &ch->common[VPIF_VIDEO_INDEX];
1942 mutex_lock(&common->lock);
1943 if (atomic_read(&ch->usrs) && common->io_usrs) {
1944 /* Disable channel */
1945 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1947 channel2_intr_enable(0);
1949 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1950 common->started == 2) {
1952 channel3_intr_enable(0);
1955 mutex_unlock(&common->lock);
1961 static int vpif_resume(struct device *dev)
1964 struct common_obj *common;
1965 struct channel_obj *ch;
1968 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1969 /* Get the pointer to the channel object */
1970 ch = vpif_obj.dev[i];
1971 common = &ch->common[VPIF_VIDEO_INDEX];
1972 mutex_lock(&common->lock);
1973 if (atomic_read(&ch->usrs) && common->io_usrs) {
1974 /* Enable channel */
1975 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1977 channel2_intr_enable(1);
1979 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1980 common->started == 2) {
1982 channel3_intr_enable(1);
1985 mutex_unlock(&common->lock);
1991 static const struct dev_pm_ops vpif_pm = {
1992 .suspend = vpif_suspend,
1993 .resume = vpif_resume,
1996 #define vpif_pm_ops (&vpif_pm)
1998 #define vpif_pm_ops NULL
2001 static __refdata struct platform_driver vpif_driver = {
2003 .name = "vpif_display",
2004 .owner = THIS_MODULE,
2007 .probe = vpif_probe,
2008 .remove = vpif_remove,
2011 static __init int vpif_init(void)
2013 return platform_driver_register(&vpif_driver);
2017 * vpif_cleanup: This function un-registers device and driver to the kernel,
2018 * frees requested irq handler and de-allocates memory allocated for channel
2021 static void vpif_cleanup(void)
2023 struct platform_device *pdev;
2024 struct resource *res;
2028 pdev = container_of(vpif_dev, struct platform_device, dev);
2030 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2031 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2033 (void *)(&vpif_obj.dev[i]->channel_id));
2037 platform_driver_unregister(&vpif_driver);
2039 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
2040 kfree(vpif_obj.dev[i]);
2043 module_init(vpif_init);
2044 module_exit(vpif_cleanup);