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");
283 if (vpif_config_data->set_clock) {
284 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
285 ycmux_mode, ch->vpifparams.std_info.hd_sd);
287 vpif_err("can't set clock\n");
292 /* set the parameters and addresses */
293 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
297 common->started = ret;
298 vpif_config_addr(ch, ret);
299 common->set_addr((addr + common->ytop_off),
300 (addr + common->ybtm_off),
301 (addr + common->ctop_off),
302 (addr + common->cbtm_off));
304 /* Set interrupt for both the fields in VPIF
305 Register enable channel in VPIF register */
306 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
307 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
308 channel2_intr_assert();
309 channel2_intr_enable(1);
311 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
312 channel2_clipping_enable(1);
315 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
316 || (common->started == 2)) {
317 channel3_intr_assert();
318 channel3_intr_enable(1);
320 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
321 channel3_clipping_enable(1);
327 /* abort streaming and wait for last buffer */
328 static int vpif_stop_streaming(struct vb2_queue *vq)
330 struct vpif_fh *fh = vb2_get_drv_priv(vq);
331 struct channel_obj *ch = fh->channel;
332 struct common_obj *common;
334 if (!vb2_is_streaming(vq))
337 common = &ch->common[VPIF_VIDEO_INDEX];
339 /* release all active buffers */
340 while (!list_empty(&common->dma_queue)) {
341 common->next_frm = list_entry(common->dma_queue.next,
342 struct vpif_disp_buffer, list);
343 list_del(&common->next_frm->list);
344 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
350 static struct vb2_ops video_qops = {
351 .queue_setup = vpif_buffer_queue_setup,
352 .wait_prepare = vpif_wait_prepare,
353 .wait_finish = vpif_wait_finish,
354 .buf_init = vpif_buffer_init,
355 .buf_prepare = vpif_buffer_prepare,
356 .start_streaming = vpif_start_streaming,
357 .stop_streaming = vpif_stop_streaming,
358 .buf_cleanup = vpif_buf_cleanup,
359 .buf_queue = vpif_buffer_queue,
362 static void process_progressive_mode(struct common_obj *common)
364 unsigned long addr = 0;
366 /* Get the next buffer from buffer queue */
367 common->next_frm = list_entry(common->dma_queue.next,
368 struct vpif_disp_buffer, list);
369 /* Remove that buffer from the buffer queue */
370 list_del(&common->next_frm->list);
371 /* Mark status of the buffer as active */
372 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
374 /* Set top and bottom field addrs in VPIF registers */
375 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
376 common->set_addr(addr + common->ytop_off,
377 addr + common->ybtm_off,
378 addr + common->ctop_off,
379 addr + common->cbtm_off);
382 static void process_interlaced_mode(int fid, struct common_obj *common)
384 /* device field id and local field id are in sync */
385 /* If this is even field */
387 if (common->cur_frm == common->next_frm)
390 /* one frame is displayed If next frame is
391 * available, release cur_frm and move on */
392 /* Copy frame display time */
393 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
394 /* Change status of the cur_frm */
395 vb2_buffer_done(&common->cur_frm->vb,
397 /* Make cur_frm pointing to next_frm */
398 common->cur_frm = common->next_frm;
400 } else if (1 == fid) { /* odd field */
401 if (list_empty(&common->dma_queue)
402 || (common->cur_frm != common->next_frm)) {
405 /* one field is displayed configure the next
406 * frame if it is available else hold on current
408 /* Get next from the buffer queue */
409 process_progressive_mode(common);
415 * vpif_channel_isr: It changes status of the displayed buffer, takes next
416 * buffer from the queue and sets its address in VPIF registers
418 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
420 struct vpif_device *dev = &vpif_obj;
421 struct channel_obj *ch;
422 struct common_obj *common;
423 enum v4l2_field field;
427 channel_id = *(int *)(dev_id);
428 if (!vpif_intr_status(channel_id + 2))
431 ch = dev->dev[channel_id];
432 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
433 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
434 common = &ch->common[i];
435 /* If streaming is started in this channel */
436 if (0 == common->started)
439 if (1 == ch->vpifparams.std_info.frm_fmt) {
440 if (list_empty(&common->dma_queue))
443 /* Progressive mode */
444 if (!channel_first_int[i][channel_id]) {
445 /* Mark status of the cur_frm to
446 * done and unlock semaphore on it */
447 do_gettimeofday(&common->cur_frm->vb.
449 vb2_buffer_done(&common->cur_frm->vb,
451 /* Make cur_frm pointing to next_frm */
452 common->cur_frm = common->next_frm;
455 channel_first_int[i][channel_id] = 0;
456 process_progressive_mode(common);
458 /* Interlaced mode */
459 /* If it is first interrupt, ignore it */
461 if (channel_first_int[i][channel_id]) {
462 channel_first_int[i][channel_id] = 0;
468 /* Get field id from VPIF registers */
469 fid = vpif_channel_getfid(ch->channel_id + 2);
470 /* If fid does not match with stored field id */
471 if (fid != ch->field_id) {
472 /* Make them in sync */
479 process_interlaced_mode(fid, common);
486 static int vpif_update_std_info(struct channel_obj *ch)
488 struct video_obj *vid_ch = &ch->video;
489 struct vpif_params *vpifparams = &ch->vpifparams;
490 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
491 const struct vpif_channel_config_params *config;
495 for (i = 0; i < vpif_ch_params_count; i++) {
496 config = &ch_params[i];
497 if (config->hd_sd == 0) {
498 vpif_dbg(2, debug, "SD format\n");
499 if (config->stdid & vid_ch->stdid) {
500 memcpy(std_info, config, sizeof(*config));
506 if (i == vpif_ch_params_count) {
507 vpif_dbg(1, debug, "Format not found\n");
514 static int vpif_update_resolution(struct channel_obj *ch)
516 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
517 struct video_obj *vid_ch = &ch->video;
518 struct vpif_params *vpifparams = &ch->vpifparams;
519 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
521 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
525 if (vpif_update_std_info(ch))
529 common->fmt.fmt.pix.width = std_info->width;
530 common->fmt.fmt.pix.height = std_info->height;
531 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
532 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
534 /* Set height and width paramateres */
535 common->height = std_info->height;
536 common->width = std_info->width;
542 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
543 * in the top and bottom field
545 static void vpif_calculate_offsets(struct channel_obj *ch)
547 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
548 struct vpif_params *vpifparams = &ch->vpifparams;
549 enum v4l2_field field = common->fmt.fmt.pix.field;
550 struct video_obj *vid_ch = &ch->video;
551 unsigned int hpitch, vpitch, sizeimage;
553 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
554 if (ch->vpifparams.std_info.frm_fmt)
555 vid_ch->buf_field = V4L2_FIELD_NONE;
557 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
559 vid_ch->buf_field = common->fmt.fmt.pix.field;
562 sizeimage = common->fmt.fmt.pix.sizeimage;
564 hpitch = common->fmt.fmt.pix.bytesperline;
565 vpitch = sizeimage / (hpitch * 2);
566 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
567 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
568 common->ytop_off = 0;
569 common->ybtm_off = hpitch;
570 common->ctop_off = sizeimage / 2;
571 common->cbtm_off = sizeimage / 2 + hpitch;
572 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
573 common->ytop_off = 0;
574 common->ybtm_off = sizeimage / 4;
575 common->ctop_off = sizeimage / 2;
576 common->cbtm_off = common->ctop_off + sizeimage / 4;
577 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
578 common->ybtm_off = 0;
579 common->ytop_off = sizeimage / 4;
580 common->cbtm_off = sizeimage / 2;
581 common->ctop_off = common->cbtm_off + sizeimage / 4;
584 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
585 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
586 vpifparams->video_params.storage_mode = 1;
588 vpifparams->video_params.storage_mode = 0;
591 if (ch->vpifparams.std_info.frm_fmt == 1) {
592 vpifparams->video_params.hpitch =
593 common->fmt.fmt.pix.bytesperline;
595 if ((field == V4L2_FIELD_ANY) ||
596 (field == V4L2_FIELD_INTERLACED))
597 vpifparams->video_params.hpitch =
598 common->fmt.fmt.pix.bytesperline * 2;
600 vpifparams->video_params.hpitch =
601 common->fmt.fmt.pix.bytesperline;
604 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
607 static void vpif_config_format(struct channel_obj *ch)
609 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
611 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
612 if (config_params.numbuffers[ch->channel_id] == 0)
613 common->memory = V4L2_MEMORY_USERPTR;
615 common->memory = V4L2_MEMORY_MMAP;
617 common->fmt.fmt.pix.sizeimage =
618 config_params.channel_bufsize[ch->channel_id];
619 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
620 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
623 static int vpif_check_format(struct channel_obj *ch,
624 struct v4l2_pix_format *pixfmt)
626 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
627 enum v4l2_field field = pixfmt->field;
628 u32 sizeimage, hpitch, vpitch;
630 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
631 goto invalid_fmt_exit;
633 if (!(VPIF_VALID_FIELD(field)))
634 goto invalid_fmt_exit;
636 if (pixfmt->bytesperline <= 0)
637 goto invalid_pitch_exit;
639 sizeimage = pixfmt->sizeimage;
641 if (vpif_update_resolution(ch))
644 hpitch = pixfmt->bytesperline;
645 vpitch = sizeimage / (hpitch * 2);
647 /* Check for valid value of pitch */
648 if ((hpitch < ch->vpifparams.std_info.width) ||
649 (vpitch < ch->vpifparams.std_info.height))
650 goto invalid_pitch_exit;
652 /* Check for 8 byte alignment */
653 if (!ISALIGNED(hpitch)) {
654 vpif_err("invalid pitch alignment\n");
657 pixfmt->width = common->fmt.fmt.pix.width;
658 pixfmt->height = common->fmt.fmt.pix.height;
663 vpif_err("invalid field format\n");
667 vpif_err("invalid pitch\n");
671 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
673 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
675 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
676 common->set_addr = ch3_set_videobuf_addr;
679 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
681 common->set_addr = ch2_set_videobuf_addr;
686 * vpif_mmap: It is used to map kernel space buffers into user spaces
688 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
690 struct vpif_fh *fh = filep->private_data;
691 struct channel_obj *ch = fh->channel;
692 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
695 vpif_dbg(2, debug, "vpif_mmap\n");
697 if (mutex_lock_interruptible(&common->lock))
699 ret = vb2_mmap(&common->buffer_queue, vma);
700 mutex_unlock(&common->lock);
705 * vpif_poll: It is used for select/poll system call
707 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
709 struct vpif_fh *fh = filep->private_data;
710 struct channel_obj *ch = fh->channel;
711 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
712 unsigned int res = 0;
714 if (common->started) {
715 mutex_lock(&common->lock);
716 res = vb2_poll(&common->buffer_queue, filep, wait);
717 mutex_unlock(&common->lock);
724 * vpif_open: It creates object of file handle structure and stores it in
725 * private_data member of filepointer
727 static int vpif_open(struct file *filep)
729 struct video_device *vdev = video_devdata(filep);
730 struct channel_obj *ch = video_get_drvdata(vdev);
731 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
734 /* Allocate memory for the file handle object */
735 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
737 vpif_err("unable to allocate memory for file handle object\n");
741 if (mutex_lock_interruptible(&common->lock)) {
745 /* store pointer to fh in private_data member of filep */
746 filep->private_data = fh;
749 if (!ch->initialized) {
752 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
755 /* Increment channel usrs counter */
756 atomic_inc(&ch->usrs);
757 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
758 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
759 /* Initialize priority of this instance to default priority */
760 fh->prio = V4L2_PRIORITY_UNSET;
761 v4l2_prio_open(&ch->prio, &fh->prio);
762 mutex_unlock(&common->lock);
768 * vpif_release: This function deletes buffer queue, frees the buffers and
769 * the vpif file handle
771 static int vpif_release(struct file *filep)
773 struct vpif_fh *fh = filep->private_data;
774 struct channel_obj *ch = fh->channel;
775 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
777 mutex_lock(&common->lock);
778 /* if this instance is doing IO */
779 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
780 /* Reset io_usrs member of channel object */
782 /* Disable channel */
783 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
785 channel2_intr_enable(0);
787 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
788 (2 == common->started)) {
790 channel3_intr_enable(0);
794 /* Free buffers allocated */
795 vb2_queue_release(&common->buffer_queue);
796 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
799 config_params.numbuffers[ch->channel_id];
802 /* Decrement channel usrs counter */
803 atomic_dec(&ch->usrs);
804 /* If this file handle has initialize encoder device, reset it */
808 /* Close the priority */
809 v4l2_prio_close(&ch->prio, fh->prio);
810 filep->private_data = NULL;
812 mutex_unlock(&common->lock);
818 /* functions implementing ioctls */
820 * vpif_querycap() - QUERYCAP handler
823 * @cap: ptr to v4l2_capability structure
825 static int vpif_querycap(struct file *file, void *priv,
826 struct v4l2_capability *cap)
828 struct vpif_display_config *config = vpif_dev->platform_data;
830 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
831 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
832 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
833 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
835 strlcpy(cap->card, config->card_name, sizeof(cap->card));
840 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
841 struct v4l2_fmtdesc *fmt)
843 if (fmt->index != 0) {
844 vpif_err("Invalid format index\n");
848 /* Fill in the information about format */
849 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
850 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
851 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
856 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
857 struct v4l2_format *fmt)
859 struct vpif_fh *fh = priv;
860 struct channel_obj *ch = fh->channel;
861 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
863 /* Check the validity of the buffer type */
864 if (common->fmt.type != fmt->type)
867 if (vpif_update_resolution(ch))
873 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
874 struct v4l2_format *fmt)
876 struct vpif_fh *fh = priv;
877 struct v4l2_pix_format *pixfmt;
878 struct channel_obj *ch = fh->channel;
879 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
882 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
883 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
884 if (!fh->initialized) {
885 vpif_dbg(1, debug, "Channel Busy\n");
889 /* Check for the priority */
890 ret = v4l2_prio_check(&ch->prio, fh->prio);
896 if (common->started) {
897 vpif_dbg(1, debug, "Streaming in progress\n");
901 pixfmt = &fmt->fmt.pix;
902 /* Check for valid field format */
903 ret = vpif_check_format(ch, pixfmt);
907 /* store the pix format in the channel object */
908 common->fmt.fmt.pix = *pixfmt;
909 /* store the format in the channel object */
914 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
915 struct v4l2_format *fmt)
917 struct vpif_fh *fh = priv;
918 struct channel_obj *ch = fh->channel;
919 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
920 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
923 ret = vpif_check_format(ch, pixfmt);
925 *pixfmt = common->fmt.fmt.pix;
926 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
932 static int vpif_reqbufs(struct file *file, void *priv,
933 struct v4l2_requestbuffers *reqbuf)
935 struct vpif_fh *fh = priv;
936 struct channel_obj *ch = fh->channel;
937 struct common_obj *common;
938 enum v4l2_field field;
943 /* This file handle has not initialized the channel,
944 It is not allowed to do settings */
945 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
946 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
947 if (!fh->initialized) {
948 vpif_err("Channel Busy\n");
953 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
956 index = VPIF_VIDEO_INDEX;
958 common = &ch->common[index];
960 if (common->fmt.type != reqbuf->type || !vpif_dev)
962 if (0 != common->io_usrs)
965 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
966 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
967 field = V4L2_FIELD_INTERLACED;
969 field = common->fmt.fmt.pix.field;
971 field = V4L2_VBI_INTERLACED;
973 /* Initialize videobuf2 queue as per the buffer type */
974 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
975 if (!common->alloc_ctx) {
976 vpif_err("Failed to get the context\n");
979 q = &common->buffer_queue;
980 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
981 q->io_modes = VB2_MMAP | VB2_USERPTR;
983 q->ops = &video_qops;
984 q->mem_ops = &vb2_dma_contig_memops;
985 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
987 ret = vb2_queue_init(q);
989 vpif_err("vpif_display: vb2_queue_init() failed\n");
990 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
993 /* Set io allowed member of file handle to TRUE */
994 fh->io_allowed[index] = 1;
995 /* Increment io usrs member of channel object to 1 */
997 /* Store type of memory requested in channel object */
998 common->memory = reqbuf->memory;
999 INIT_LIST_HEAD(&common->dma_queue);
1000 /* Allocate buffers */
1001 return vb2_reqbufs(&common->buffer_queue, reqbuf);
1004 static int vpif_querybuf(struct file *file, void *priv,
1005 struct v4l2_buffer *tbuf)
1007 struct vpif_fh *fh = priv;
1008 struct channel_obj *ch = fh->channel;
1009 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1011 if (common->fmt.type != tbuf->type)
1014 return vb2_querybuf(&common->buffer_queue, tbuf);
1017 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1019 struct vpif_fh *fh = NULL;
1020 struct channel_obj *ch = NULL;
1021 struct common_obj *common = NULL;
1031 common = &(ch->common[VPIF_VIDEO_INDEX]);
1032 if (common->fmt.type != buf->type)
1035 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1036 vpif_err("fh->io_allowed\n");
1040 return vb2_qbuf(&common->buffer_queue, buf);
1043 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1045 struct vpif_fh *fh = priv;
1046 struct channel_obj *ch = fh->channel;
1047 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1050 if (!(*std_id & VPIF_V4L2_STD))
1053 if (common->started) {
1054 vpif_err("streaming in progress\n");
1058 /* Call encoder subdevice function to set the standard */
1059 ch->video.stdid = *std_id;
1060 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1061 /* Get the information about the standard */
1062 if (vpif_update_resolution(ch))
1065 if ((ch->vpifparams.std_info.width *
1066 ch->vpifparams.std_info.height * 2) >
1067 config_params.channel_bufsize[ch->channel_id]) {
1068 vpif_err("invalid std for this size\n");
1072 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1073 /* Configure the default format information */
1074 vpif_config_format(ch);
1076 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1077 s_std_output, *std_id);
1079 vpif_err("Failed to set output standard\n");
1083 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1086 vpif_err("Failed to set standard for sub devices\n");
1090 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1092 struct vpif_fh *fh = priv;
1093 struct channel_obj *ch = fh->channel;
1095 *std = ch->video.stdid;
1099 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1101 struct vpif_fh *fh = priv;
1102 struct channel_obj *ch = fh->channel;
1103 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1105 return vb2_dqbuf(&common->buffer_queue, p,
1106 (file->f_flags & O_NONBLOCK));
1109 static int vpif_streamon(struct file *file, void *priv,
1110 enum v4l2_buf_type buftype)
1112 struct vpif_fh *fh = priv;
1113 struct channel_obj *ch = fh->channel;
1114 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1115 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1118 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1119 vpif_err("buffer type not supported\n");
1123 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1124 vpif_err("fh->io_allowed\n");
1128 /* If Streaming is already started, return error */
1129 if (common->started) {
1130 vpif_err("channel->started\n");
1134 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1135 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1136 ch->vpifparams.std_info.ycmux_mode == 0)
1137 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1138 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1139 vpif_err("other channel is using\n");
1143 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1147 /* Call vb2_streamon to start streaming in videobuf2 */
1148 ret = vb2_streamon(&common->buffer_queue, buftype);
1150 vpif_err("vb2_streamon\n");
1157 static int vpif_streamoff(struct file *file, void *priv,
1158 enum v4l2_buf_type buftype)
1160 struct vpif_fh *fh = priv;
1161 struct channel_obj *ch = fh->channel;
1162 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1163 struct vpif_display_config *vpif_config_data =
1164 vpif_dev->platform_data;
1166 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1167 vpif_err("buffer type not supported\n");
1171 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1172 vpif_err("fh->io_allowed\n");
1176 if (!common->started) {
1177 vpif_err("channel->started\n");
1181 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1182 /* disable channel */
1183 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1184 if (vpif_config_data->
1185 chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
1186 channel2_clipping_enable(0);
1188 channel2_intr_enable(0);
1190 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1191 (2 == common->started)) {
1192 if (vpif_config_data->
1193 chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
1194 channel3_clipping_enable(0);
1196 channel3_intr_enable(0);
1200 common->started = 0;
1201 return vb2_streamoff(&common->buffer_queue, buftype);
1204 static int vpif_cropcap(struct file *file, void *priv,
1205 struct v4l2_cropcap *crop)
1207 struct vpif_fh *fh = priv;
1208 struct channel_obj *ch = fh->channel;
1209 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1210 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1213 crop->bounds.left = crop->bounds.top = 0;
1214 crop->defrect.left = crop->defrect.top = 0;
1215 crop->defrect.height = crop->bounds.height = common->height;
1216 crop->defrect.width = crop->bounds.width = common->width;
1221 static int vpif_enum_output(struct file *file, void *fh,
1222 struct v4l2_output *output)
1225 struct vpif_display_config *config = vpif_dev->platform_data;
1226 struct vpif_display_chan_config *chan_cfg;
1227 struct vpif_fh *vpif_handler = fh;
1228 struct channel_obj *ch = vpif_handler->channel;
1230 chan_cfg = &config->chan_config[ch->channel_id];
1231 if (output->index >= chan_cfg->output_count) {
1232 vpif_dbg(1, debug, "Invalid output index\n");
1236 *output = chan_cfg->outputs[output->index].output;
1241 * vpif_output_to_subdev() - Maps output to sub device
1242 * @vpif_cfg - global config ptr
1243 * @chan_cfg - channel config ptr
1244 * @index - Given output index from application
1246 * lookup the sub device information for a given output index.
1247 * we report all the output to application. output table also
1248 * has sub device name for the each output
1251 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
1252 struct vpif_display_chan_config *chan_cfg, int index)
1254 struct vpif_subdev_info *subdev_info;
1255 const char *subdev_name;
1258 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
1260 if (chan_cfg->outputs == NULL)
1263 subdev_name = chan_cfg->outputs[index].subdev_name;
1264 if (subdev_name == NULL)
1267 /* loop through the sub device list to get the sub device info */
1268 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1269 subdev_info = &vpif_cfg->subdevinfo[i];
1270 if (!strcmp(subdev_info->name, subdev_name))
1277 * vpif_set_output() - Select an output
1278 * @vpif_cfg - global config ptr
1280 * @index - Given output index from application
1282 * Select the given output.
1284 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
1285 struct channel_obj *ch, int index)
1287 struct vpif_display_chan_config *chan_cfg =
1288 &vpif_cfg->chan_config[ch->channel_id];
1289 struct vpif_subdev_info *subdev_info = NULL;
1290 struct v4l2_subdev *sd = NULL;
1291 u32 input = 0, output = 0;
1295 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
1296 if (sd_index >= 0) {
1297 sd = vpif_obj.sd[sd_index];
1298 subdev_info = &vpif_cfg->subdevinfo[sd_index];
1302 input = chan_cfg->outputs[index].input_route;
1303 output = chan_cfg->outputs[index].output_route;
1304 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1305 if (ret < 0 && ret != -ENOIOCTLCMD) {
1306 vpif_err("Failed to set output\n");
1311 ch->output_idx = index;
1313 if (chan_cfg->outputs != NULL)
1314 /* update tvnorms from the sub device output info */
1315 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
1319 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1321 struct vpif_display_config *config = vpif_dev->platform_data;
1322 struct vpif_display_chan_config *chan_cfg;
1323 struct vpif_fh *fh = priv;
1324 struct channel_obj *ch = fh->channel;
1325 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1327 chan_cfg = &config->chan_config[ch->channel_id];
1329 if (i >= chan_cfg->output_count)
1332 if (common->started) {
1333 vpif_err("Streaming in progress\n");
1337 return vpif_set_output(config, ch, i);
1340 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1342 struct vpif_fh *fh = priv;
1343 struct channel_obj *ch = fh->channel;
1345 *i = ch->output_idx;
1350 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1352 struct vpif_fh *fh = priv;
1353 struct channel_obj *ch = fh->channel;
1355 *p = v4l2_prio_max(&ch->prio);
1360 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1362 struct vpif_fh *fh = priv;
1363 struct channel_obj *ch = fh->channel;
1365 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1369 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1371 * @priv: file handle
1372 * @timings: input timings
1375 vpif_enum_dv_timings(struct file *file, void *priv,
1376 struct v4l2_enum_dv_timings *timings)
1378 struct vpif_fh *fh = priv;
1379 struct channel_obj *ch = fh->channel;
1382 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1383 if (ret == -ENOIOCTLCMD && ret == -ENODEV)
1389 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1391 * @priv: file handle
1392 * @timings: digital video timings
1394 static int vpif_s_dv_timings(struct file *file, void *priv,
1395 struct v4l2_dv_timings *timings)
1397 struct vpif_fh *fh = priv;
1398 struct channel_obj *ch = fh->channel;
1399 struct vpif_params *vpifparams = &ch->vpifparams;
1400 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1401 struct video_obj *vid_ch = &ch->video;
1402 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1405 if (timings->type != V4L2_DV_BT_656_1120) {
1406 vpif_dbg(2, debug, "Timing type not defined\n");
1410 /* Configure subdevice timings, if any */
1411 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1412 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1415 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1419 if (!(timings->bt.width && timings->bt.height &&
1420 (timings->bt.hbackporch ||
1421 timings->bt.hfrontporch ||
1422 timings->bt.hsync) &&
1423 timings->bt.vfrontporch &&
1424 (timings->bt.vbackporch ||
1425 timings->bt.vsync))) {
1426 vpif_dbg(2, debug, "Timings for width, height, "
1427 "horizontal back porch, horizontal sync, "
1428 "horizontal front porch, vertical back porch, "
1429 "vertical sync and vertical back porch "
1430 "must be defined\n");
1434 vid_ch->dv_timings = *timings;
1436 /* Configure video port timings */
1438 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1440 std_info->sav2eav = bt->width;
1443 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1445 if (bt->interlaced) {
1446 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1447 std_info->vsize = bt->height * 2 +
1448 bt->vfrontporch + bt->vsync + bt->vbackporch +
1449 bt->il_vfrontporch + bt->il_vsync +
1451 std_info->l5 = std_info->vsize/2 -
1452 (bt->vfrontporch - 1);
1453 std_info->l7 = std_info->vsize/2 + 1;
1454 std_info->l9 = std_info->l7 + bt->il_vsync +
1455 bt->il_vbackporch + 1;
1456 std_info->l11 = std_info->vsize -
1457 (bt->il_vfrontporch - 1);
1459 vpif_dbg(2, debug, "Required timing values for "
1460 "interlaced BT format missing\n");
1464 std_info->vsize = bt->height + bt->vfrontporch +
1465 bt->vsync + bt->vbackporch;
1466 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1468 strncpy(std_info->name, "Custom timings BT656/1120",
1470 std_info->width = bt->width;
1471 std_info->height = bt->height;
1472 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1473 std_info->ycmux_mode = 0;
1474 std_info->capture_format = 0;
1475 std_info->vbi_supported = 0;
1476 std_info->hd_sd = 1;
1477 std_info->stdid = 0;
1484 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1486 * @priv: file handle
1487 * @timings: digital video timings
1489 static int vpif_g_dv_timings(struct file *file, void *priv,
1490 struct v4l2_dv_timings *timings)
1492 struct vpif_fh *fh = priv;
1493 struct channel_obj *ch = fh->channel;
1494 struct video_obj *vid_ch = &ch->video;
1496 *timings = vid_ch->dv_timings;
1502 * vpif_g_chip_ident() - Identify the chip
1504 * @priv: file handle
1505 * @chip: chip identity
1507 * Returns zero or -EINVAL if read operations fails.
1509 static int vpif_g_chip_ident(struct file *file, void *priv,
1510 struct v4l2_dbg_chip_ident *chip)
1512 chip->ident = V4L2_IDENT_NONE;
1514 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1515 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1516 vpif_dbg(2, debug, "match_type is invalid.\n");
1520 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1521 g_chip_ident, chip);
1524 #ifdef CONFIG_VIDEO_ADV_DEBUG
1526 * vpif_dbg_g_register() - Read register
1528 * @priv: file handle
1529 * @reg: register to be read
1532 * Returns zero or -EINVAL if read operations fails.
1534 static int vpif_dbg_g_register(struct file *file, void *priv,
1535 struct v4l2_dbg_register *reg){
1536 struct vpif_fh *fh = priv;
1537 struct channel_obj *ch = fh->channel;
1539 return v4l2_subdev_call(ch->sd, core, g_register, reg);
1543 * vpif_dbg_s_register() - Write to register
1545 * @priv: file handle
1546 * @reg: register to be modified
1549 * Returns zero or -EINVAL if write operations fails.
1551 static int vpif_dbg_s_register(struct file *file, void *priv,
1552 struct v4l2_dbg_register *reg){
1553 struct vpif_fh *fh = priv;
1554 struct channel_obj *ch = fh->channel;
1556 return v4l2_subdev_call(ch->sd, core, s_register, reg);
1561 * vpif_log_status() - Status information
1563 * @priv: file handle
1567 static int vpif_log_status(struct file *filep, void *priv)
1569 /* status for sub devices */
1570 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1575 /* vpif display ioctl operations */
1576 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1577 .vidioc_querycap = vpif_querycap,
1578 .vidioc_g_priority = vpif_g_priority,
1579 .vidioc_s_priority = vpif_s_priority,
1580 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1581 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1582 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1583 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1584 .vidioc_reqbufs = vpif_reqbufs,
1585 .vidioc_querybuf = vpif_querybuf,
1586 .vidioc_qbuf = vpif_qbuf,
1587 .vidioc_dqbuf = vpif_dqbuf,
1588 .vidioc_streamon = vpif_streamon,
1589 .vidioc_streamoff = vpif_streamoff,
1590 .vidioc_s_std = vpif_s_std,
1591 .vidioc_g_std = vpif_g_std,
1592 .vidioc_enum_output = vpif_enum_output,
1593 .vidioc_s_output = vpif_s_output,
1594 .vidioc_g_output = vpif_g_output,
1595 .vidioc_cropcap = vpif_cropcap,
1596 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1597 .vidioc_s_dv_timings = vpif_s_dv_timings,
1598 .vidioc_g_dv_timings = vpif_g_dv_timings,
1599 .vidioc_g_chip_ident = vpif_g_chip_ident,
1600 #ifdef CONFIG_VIDEO_ADV_DEBUG
1601 .vidioc_g_register = vpif_dbg_g_register,
1602 .vidioc_s_register = vpif_dbg_s_register,
1604 .vidioc_log_status = vpif_log_status,
1607 static const struct v4l2_file_operations vpif_fops = {
1608 .owner = THIS_MODULE,
1610 .release = vpif_release,
1611 .unlocked_ioctl = video_ioctl2,
1616 static struct video_device vpif_video_template = {
1619 .ioctl_ops = &vpif_ioctl_ops,
1622 /*Configure the channels, buffer sizei, request irq */
1623 static int initialize_vpif(void)
1625 int free_channel_objects_index;
1626 int free_buffer_channel_index;
1627 int free_buffer_index;
1630 /* Default number of buffers should be 3 */
1631 if ((ch2_numbuffers > 0) &&
1632 (ch2_numbuffers < config_params.min_numbuffers))
1633 ch2_numbuffers = config_params.min_numbuffers;
1634 if ((ch3_numbuffers > 0) &&
1635 (ch3_numbuffers < config_params.min_numbuffers))
1636 ch3_numbuffers = config_params.min_numbuffers;
1638 /* Set buffer size to min buffers size if invalid buffer size is
1640 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1642 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1643 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1645 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1647 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1649 if (ch2_numbuffers) {
1650 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1653 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1655 if (ch3_numbuffers) {
1656 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1660 /* Allocate memory for six channel objects */
1661 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1663 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1664 /* If memory allocation fails, return error */
1665 if (!vpif_obj.dev[i]) {
1666 free_channel_objects_index = i;
1668 goto vpif_init_free_channel_objects;
1672 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1673 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1674 free_buffer_index = config_params.numbuffers[i - 1];
1678 vpif_init_free_channel_objects:
1679 for (j = 0; j < free_channel_objects_index; j++)
1680 kfree(vpif_obj.dev[j]);
1685 * vpif_probe: This function creates device entries by register itself to the
1686 * V4L2 driver and initializes fields of each channel objects
1688 static __init int vpif_probe(struct platform_device *pdev)
1690 struct vpif_subdev_info *subdevdata;
1691 struct vpif_display_config *config;
1692 int i, j = 0, k, err = 0;
1694 struct i2c_adapter *i2c_adap;
1695 struct common_obj *common;
1696 struct channel_obj *ch;
1697 struct video_device *vfd;
1698 struct resource *res;
1702 vpif_dev = &pdev->dev;
1703 err = initialize_vpif();
1706 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1710 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1712 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1716 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1717 for (i = res->start; i <= res->end; i++) {
1718 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
1719 "VPIF_Display", (void *)
1720 (&vpif_obj.dev[res_idx]->channel_id))) {
1722 for (j = 0; j < i; j++)
1723 free_irq(j, (void *)
1724 (&vpif_obj.dev[res_idx]->channel_id));
1731 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1732 /* Get the pointer to the channel object */
1733 ch = vpif_obj.dev[i];
1735 /* Allocate memory for video device */
1736 vfd = video_device_alloc();
1738 for (j = 0; j < i; j++) {
1739 ch = vpif_obj.dev[j];
1740 video_device_release(ch->video_dev);
1746 /* Initialize field of video device */
1747 *vfd = vpif_video_template;
1748 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1749 vfd->release = video_device_release;
1750 vfd->vfl_dir = VFL_DIR_TX;
1751 snprintf(vfd->name, sizeof(vfd->name),
1752 "VPIF_Display_DRIVER_V%s",
1753 VPIF_DISPLAY_VERSION);
1755 /* Set video_dev to the video device */
1756 ch->video_dev = vfd;
1759 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1761 size = resource_size(res);
1762 /* The resources are divided into two equal memory and when
1763 * we have HD output we can add them together
1765 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1766 ch = vpif_obj.dev[j];
1769 /* only enabled if second resource exists */
1770 config_params.video_limit[ch->channel_id] = 0;
1772 config_params.video_limit[ch->channel_id] =
1777 i2c_adap = i2c_get_adapter(1);
1778 config = pdev->dev.platform_data;
1779 subdev_count = config->subdev_count;
1780 subdevdata = config->subdevinfo;
1781 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1783 if (vpif_obj.sd == NULL) {
1784 vpif_err("unable to allocate memory for subdevice pointers\n");
1789 for (i = 0; i < subdev_count; i++) {
1790 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1792 &subdevdata[i].board_info,
1794 if (!vpif_obj.sd[i]) {
1795 vpif_err("Error registering v4l2 subdevice\n");
1796 goto probe_subdev_out;
1800 vpif_obj.sd[i]->grp_id = 1 << i;
1803 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1804 ch = vpif_obj.dev[j];
1805 /* Initialize field of the channel objects */
1806 atomic_set(&ch->usrs, 0);
1807 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1808 ch->common[k].numbuffers = 0;
1809 common = &ch->common[k];
1810 common->io_usrs = 0;
1811 common->started = 0;
1812 spin_lock_init(&common->irqlock);
1813 mutex_init(&common->lock);
1814 common->numbuffers = 0;
1815 common->set_addr = NULL;
1816 common->ytop_off = common->ybtm_off = 0;
1817 common->ctop_off = common->cbtm_off = 0;
1818 common->cur_frm = common->next_frm = NULL;
1819 memset(&common->fmt, 0, sizeof(common->fmt));
1820 common->numbuffers = config_params.numbuffers[k];
1823 ch->initialized = 0;
1825 ch->sd = vpif_obj.sd[0];
1828 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1829 config_params.numbuffers[ch->channel_id];
1831 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1833 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1835 /* Initialize prio member of channel object */
1836 v4l2_prio_init(&ch->prio);
1837 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1838 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1839 ch->video_dev->lock = &common->lock;
1840 video_set_drvdata(ch->video_dev, ch);
1842 /* select output 0 */
1843 err = vpif_set_output(config, ch, 0);
1847 /* register video device */
1848 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1849 (int)ch, (int)&ch->video_dev);
1851 err = video_register_device(ch->video_dev,
1852 VFL_TYPE_GRABBER, (j ? 3 : 2));
1857 v4l2_info(&vpif_obj.v4l2_dev,
1858 " VPIF display driver initialized\n");
1862 for (k = 0; k < j; k++) {
1863 ch = vpif_obj.dev[k];
1864 video_unregister_device(ch->video_dev);
1865 video_device_release(ch->video_dev);
1866 ch->video_dev = NULL;
1871 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1872 ch = vpif_obj.dev[i];
1873 /* Note: does nothing if ch->video_dev == NULL */
1874 video_device_release(ch->video_dev);
1877 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1878 vpif_err("VPIF IRQ request failed\n");
1879 for (i = 0; i < res_idx; i++) {
1880 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1881 for (j = res->start; j <= res->end; j++)
1882 free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
1889 * vpif_remove: It un-register channels from V4L2 driver
1891 static int vpif_remove(struct platform_device *device)
1893 struct channel_obj *ch;
1896 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1898 /* un-register device */
1899 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1900 /* Get the pointer to the channel object */
1901 ch = vpif_obj.dev[i];
1902 /* Unregister video device */
1903 video_unregister_device(ch->video_dev);
1905 ch->video_dev = NULL;
1912 static int vpif_suspend(struct device *dev)
1914 struct common_obj *common;
1915 struct channel_obj *ch;
1918 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1919 /* Get the pointer to the channel object */
1920 ch = vpif_obj.dev[i];
1921 common = &ch->common[VPIF_VIDEO_INDEX];
1922 mutex_lock(&common->lock);
1923 if (atomic_read(&ch->usrs) && common->io_usrs) {
1924 /* Disable channel */
1925 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1927 channel2_intr_enable(0);
1929 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1930 common->started == 2) {
1932 channel3_intr_enable(0);
1935 mutex_unlock(&common->lock);
1941 static int vpif_resume(struct device *dev)
1944 struct common_obj *common;
1945 struct channel_obj *ch;
1948 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1949 /* Get the pointer to the channel object */
1950 ch = vpif_obj.dev[i];
1951 common = &ch->common[VPIF_VIDEO_INDEX];
1952 mutex_lock(&common->lock);
1953 if (atomic_read(&ch->usrs) && common->io_usrs) {
1954 /* Enable channel */
1955 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1957 channel2_intr_enable(1);
1959 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1960 common->started == 2) {
1962 channel3_intr_enable(1);
1965 mutex_unlock(&common->lock);
1971 static const struct dev_pm_ops vpif_pm = {
1972 .suspend = vpif_suspend,
1973 .resume = vpif_resume,
1976 #define vpif_pm_ops (&vpif_pm)
1978 #define vpif_pm_ops NULL
1981 static __refdata struct platform_driver vpif_driver = {
1983 .name = "vpif_display",
1984 .owner = THIS_MODULE,
1987 .probe = vpif_probe,
1988 .remove = vpif_remove,
1991 static __init int vpif_init(void)
1993 return platform_driver_register(&vpif_driver);
1997 * vpif_cleanup: This function un-registers device and driver to the kernel,
1998 * frees requested irq handler and de-allocates memory allocated for channel
2001 static void vpif_cleanup(void)
2003 struct platform_device *pdev;
2004 struct resource *res;
2008 pdev = container_of(vpif_dev, struct platform_device, dev);
2010 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2011 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2013 (void *)(&vpif_obj.dev[i]->channel_id));
2017 platform_driver_unregister(&vpif_driver);
2019 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
2020 kfree(vpif_obj.dev[i]);
2023 module_init(vpif_init);
2024 module_exit(vpif_cleanup);