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/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
22 #include <media/v4l2-ioctl.h>
25 #include "vpif_display.h"
27 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
28 MODULE_LICENSE("GPL");
29 MODULE_VERSION(VPIF_DISPLAY_VERSION);
31 #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
33 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
34 #define vpif_dbg(level, debug, fmt, arg...) \
35 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
38 static u32 ch2_numbuffers = 3;
39 static u32 ch3_numbuffers = 3;
40 static u32 ch2_bufsize = 1920 * 1080 * 2;
41 static u32 ch3_bufsize = 720 * 576 * 2;
43 module_param(debug, int, 0644);
44 module_param(ch2_numbuffers, uint, S_IRUGO);
45 module_param(ch3_numbuffers, uint, S_IRUGO);
46 module_param(ch2_bufsize, uint, S_IRUGO);
47 module_param(ch3_bufsize, uint, S_IRUGO);
49 MODULE_PARM_DESC(debug, "Debug level 0-1");
50 MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
51 MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
52 MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
53 MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
55 static struct vpif_config_params config_params = {
59 .min_bufsize[0] = 720 * 480 * 2,
60 .min_bufsize[1] = 720 * 480 * 2,
61 .channel_bufsize[0] = 1920 * 1080 * 2,
62 .channel_bufsize[1] = 720 * 576 * 2,
65 static struct vpif_device vpif_obj = { {NULL} };
66 static struct device *vpif_dev;
67 static void vpif_calculate_offsets(struct channel_obj *ch);
68 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
71 * buffer_prepare: This is the callback function called from vb2_qbuf()
72 * function the buffer is prepared and user space virtual address is converted
73 * into physical address
75 static int vpif_buffer_prepare(struct vb2_buffer *vb)
77 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
78 struct vb2_queue *q = vb->vb2_queue;
79 struct common_obj *common;
82 common = &fh->channel->common[VPIF_VIDEO_INDEX];
83 if (vb->state != VB2_BUF_STATE_ACTIVE &&
84 vb->state != VB2_BUF_STATE_PREPARED) {
85 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
86 if (vb2_plane_vaddr(vb, 0) &&
87 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
90 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
92 (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
93 if (!ISALIGNED(addr + common->ytop_off) ||
94 !ISALIGNED(addr + common->ybtm_off) ||
95 !ISALIGNED(addr + common->ctop_off) ||
96 !ISALIGNED(addr + common->cbtm_off))
103 vpif_err("buffer offset not aligned to 8 bytes\n");
108 * vpif_buffer_queue_setup: This function allocates memory for the buffers
110 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
111 const struct v4l2_format *fmt,
112 unsigned int *nbuffers, unsigned int *nplanes,
113 unsigned int sizes[], void *alloc_ctxs[])
115 struct vpif_fh *fh = vb2_get_drv_priv(vq);
116 struct channel_obj *ch = fh->channel;
117 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
120 if (V4L2_MEMORY_MMAP == common->memory) {
121 size = config_params.channel_bufsize[ch->channel_id];
123 * Checking if the buffer size exceeds the available buffer
124 * ycmux_mode = 0 means 1 channel mode HD and
125 * ycmux_mode = 1 means 2 channels mode SD
127 if (ch->vpifparams.std_info.ycmux_mode == 0) {
128 if (config_params.video_limit[ch->channel_id])
129 while (size * *nbuffers >
130 (config_params.video_limit[0]
131 + config_params.video_limit[1]))
134 if (config_params.video_limit[ch->channel_id])
135 while (size * *nbuffers >
136 config_params.video_limit[ch->channel_id])
140 size = common->fmt.fmt.pix.sizeimage;
143 if (*nbuffers < config_params.min_numbuffers)
144 *nbuffers = config_params.min_numbuffers;
148 alloc_ctxs[0] = common->alloc_ctx;
153 * vpif_buffer_queue: This function adds the buffer to DMA queue
155 static void vpif_buffer_queue(struct vb2_buffer *vb)
157 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
158 struct vpif_disp_buffer *buf = container_of(vb,
159 struct vpif_disp_buffer, vb);
160 struct channel_obj *ch = fh->channel;
161 struct common_obj *common;
164 common = &ch->common[VPIF_VIDEO_INDEX];
166 /* add the buffer to the DMA queue */
167 spin_lock_irqsave(&common->irqlock, flags);
168 list_add_tail(&buf->list, &common->dma_queue);
169 spin_unlock_irqrestore(&common->irqlock, flags);
173 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
174 * free memory allocated to the buffers
176 static void vpif_buf_cleanup(struct vb2_buffer *vb)
178 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
179 struct vpif_disp_buffer *buf = container_of(vb,
180 struct vpif_disp_buffer, vb);
181 struct channel_obj *ch = fh->channel;
182 struct common_obj *common;
185 common = &ch->common[VPIF_VIDEO_INDEX];
187 spin_lock_irqsave(&common->irqlock, flags);
188 if (vb->state == VB2_BUF_STATE_ACTIVE)
189 list_del_init(&buf->list);
190 spin_unlock_irqrestore(&common->irqlock, flags);
193 static void vpif_wait_prepare(struct vb2_queue *vq)
195 struct vpif_fh *fh = vb2_get_drv_priv(vq);
196 struct channel_obj *ch = fh->channel;
197 struct common_obj *common;
199 common = &ch->common[VPIF_VIDEO_INDEX];
200 mutex_unlock(&common->lock);
203 static void vpif_wait_finish(struct vb2_queue *vq)
205 struct vpif_fh *fh = vb2_get_drv_priv(vq);
206 struct channel_obj *ch = fh->channel;
207 struct common_obj *common;
209 common = &ch->common[VPIF_VIDEO_INDEX];
210 mutex_lock(&common->lock);
213 static int vpif_buffer_init(struct vb2_buffer *vb)
215 struct vpif_disp_buffer *buf = container_of(vb,
216 struct vpif_disp_buffer, vb);
218 INIT_LIST_HEAD(&buf->list);
223 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
225 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
227 struct vpif_display_config *vpif_config_data =
228 vpif_dev->platform_data;
229 struct vpif_fh *fh = vb2_get_drv_priv(vq);
230 struct channel_obj *ch = fh->channel;
231 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
232 struct vpif_params *vpif = &ch->vpifparams;
233 unsigned long addr = 0;
237 spin_lock_irqsave(&common->irqlock, flags);
239 /* Get the next frame from the buffer queue */
240 common->next_frm = common->cur_frm =
241 list_entry(common->dma_queue.next,
242 struct vpif_disp_buffer, list);
244 list_del(&common->cur_frm->list);
245 spin_unlock_irqrestore(&common->irqlock, flags);
246 /* Mark state of the current frame to active */
247 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
249 /* Initialize field_id and started member */
252 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
253 /* Calculate the offset for Y and C data in the buffer */
254 vpif_calculate_offsets(ch);
256 if ((ch->vpifparams.std_info.frm_fmt &&
257 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
258 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
259 || (!ch->vpifparams.std_info.frm_fmt
260 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
261 vpif_err("conflict in field format and std format\n");
266 if (vpif_config_data->set_clock) {
267 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
268 ycmux_mode, ch->vpifparams.std_info.hd_sd);
270 vpif_err("can't set clock\n");
275 /* set the parameters and addresses */
276 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
280 common->started = ret;
281 vpif_config_addr(ch, ret);
282 common->set_addr((addr + common->ytop_off),
283 (addr + common->ybtm_off),
284 (addr + common->ctop_off),
285 (addr + common->cbtm_off));
287 /* Set interrupt for both the fields in VPIF
288 Register enable channel in VPIF register */
289 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
290 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
291 channel2_intr_assert();
292 channel2_intr_enable(1);
294 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
295 channel2_clipping_enable(1);
298 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
299 || (common->started == 2)) {
300 channel3_intr_assert();
301 channel3_intr_enable(1);
303 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
304 channel3_clipping_enable(1);
310 /* abort streaming and wait for last buffer */
311 static int vpif_stop_streaming(struct vb2_queue *vq)
313 struct vpif_fh *fh = vb2_get_drv_priv(vq);
314 struct channel_obj *ch = fh->channel;
315 struct common_obj *common;
318 if (!vb2_is_streaming(vq))
321 common = &ch->common[VPIF_VIDEO_INDEX];
323 /* Disable channel */
324 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
326 channel2_intr_enable(0);
328 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
329 (2 == common->started)) {
331 channel3_intr_enable(0);
335 /* release all active buffers */
336 spin_lock_irqsave(&common->irqlock, flags);
337 if (common->cur_frm == common->next_frm) {
338 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
340 if (common->cur_frm != NULL)
341 vb2_buffer_done(&common->cur_frm->vb,
342 VB2_BUF_STATE_ERROR);
343 if (common->next_frm != NULL)
344 vb2_buffer_done(&common->next_frm->vb,
345 VB2_BUF_STATE_ERROR);
348 while (!list_empty(&common->dma_queue)) {
349 common->next_frm = list_entry(common->dma_queue.next,
350 struct vpif_disp_buffer, list);
351 list_del(&common->next_frm->list);
352 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
354 spin_unlock_irqrestore(&common->irqlock, flags);
359 static struct vb2_ops video_qops = {
360 .queue_setup = vpif_buffer_queue_setup,
361 .wait_prepare = vpif_wait_prepare,
362 .wait_finish = vpif_wait_finish,
363 .buf_init = vpif_buffer_init,
364 .buf_prepare = vpif_buffer_prepare,
365 .start_streaming = vpif_start_streaming,
366 .stop_streaming = vpif_stop_streaming,
367 .buf_cleanup = vpif_buf_cleanup,
368 .buf_queue = vpif_buffer_queue,
371 static void process_progressive_mode(struct common_obj *common)
373 unsigned long addr = 0;
375 spin_lock(&common->irqlock);
376 /* Get the next buffer from buffer queue */
377 common->next_frm = list_entry(common->dma_queue.next,
378 struct vpif_disp_buffer, list);
379 /* Remove that buffer from the buffer queue */
380 list_del(&common->next_frm->list);
381 spin_unlock(&common->irqlock);
382 /* Mark status of the buffer as active */
383 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
385 /* Set top and bottom field addrs in VPIF registers */
386 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
387 common->set_addr(addr + common->ytop_off,
388 addr + common->ybtm_off,
389 addr + common->ctop_off,
390 addr + common->cbtm_off);
393 static void process_interlaced_mode(int fid, struct common_obj *common)
395 /* device field id and local field id are in sync */
396 /* If this is even field */
398 if (common->cur_frm == common->next_frm)
401 /* one frame is displayed If next frame is
402 * available, release cur_frm and move on */
403 /* Copy frame display time */
404 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
405 /* Change status of the cur_frm */
406 vb2_buffer_done(&common->cur_frm->vb,
408 /* Make cur_frm pointing to next_frm */
409 common->cur_frm = common->next_frm;
411 } else if (1 == fid) { /* odd field */
412 spin_lock(&common->irqlock);
413 if (list_empty(&common->dma_queue)
414 || (common->cur_frm != common->next_frm)) {
415 spin_unlock(&common->irqlock);
418 spin_unlock(&common->irqlock);
419 /* one field is displayed configure the next
420 * frame if it is available else hold on current
422 /* Get next from the buffer queue */
423 process_progressive_mode(common);
428 * vpif_channel_isr: It changes status of the displayed buffer, takes next
429 * buffer from the queue and sets its address in VPIF registers
431 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
433 struct vpif_device *dev = &vpif_obj;
434 struct channel_obj *ch;
435 struct common_obj *common;
436 enum v4l2_field field;
440 channel_id = *(int *)(dev_id);
441 if (!vpif_intr_status(channel_id + 2))
444 ch = dev->dev[channel_id];
445 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
446 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
447 common = &ch->common[i];
448 /* If streaming is started in this channel */
449 if (0 == common->started)
452 if (1 == ch->vpifparams.std_info.frm_fmt) {
453 spin_lock(&common->irqlock);
454 if (list_empty(&common->dma_queue)) {
455 spin_unlock(&common->irqlock);
458 spin_unlock(&common->irqlock);
460 /* Progressive mode */
461 if (!channel_first_int[i][channel_id]) {
462 /* Mark status of the cur_frm to
463 * done and unlock semaphore on it */
464 v4l2_get_timestamp(&common->cur_frm->vb.
466 vb2_buffer_done(&common->cur_frm->vb,
468 /* Make cur_frm pointing to next_frm */
469 common->cur_frm = common->next_frm;
472 channel_first_int[i][channel_id] = 0;
473 process_progressive_mode(common);
475 /* Interlaced mode */
476 /* If it is first interrupt, ignore it */
478 if (channel_first_int[i][channel_id]) {
479 channel_first_int[i][channel_id] = 0;
485 /* Get field id from VPIF registers */
486 fid = vpif_channel_getfid(ch->channel_id + 2);
487 /* If fid does not match with stored field id */
488 if (fid != ch->field_id) {
489 /* Make them in sync */
496 process_interlaced_mode(fid, common);
503 static int vpif_update_std_info(struct channel_obj *ch)
505 struct video_obj *vid_ch = &ch->video;
506 struct vpif_params *vpifparams = &ch->vpifparams;
507 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
508 const struct vpif_channel_config_params *config;
512 for (i = 0; i < vpif_ch_params_count; i++) {
513 config = &vpif_ch_params[i];
514 if (config->hd_sd == 0) {
515 vpif_dbg(2, debug, "SD format\n");
516 if (config->stdid & vid_ch->stdid) {
517 memcpy(std_info, config, sizeof(*config));
523 if (i == vpif_ch_params_count) {
524 vpif_dbg(1, debug, "Format not found\n");
531 static int vpif_update_resolution(struct channel_obj *ch)
533 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
534 struct video_obj *vid_ch = &ch->video;
535 struct vpif_params *vpifparams = &ch->vpifparams;
536 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
538 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
542 if (vpif_update_std_info(ch))
546 common->fmt.fmt.pix.width = std_info->width;
547 common->fmt.fmt.pix.height = std_info->height;
548 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
549 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
551 /* Set height and width paramateres */
552 common->height = std_info->height;
553 common->width = std_info->width;
559 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
560 * in the top and bottom field
562 static void vpif_calculate_offsets(struct channel_obj *ch)
564 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
565 struct vpif_params *vpifparams = &ch->vpifparams;
566 enum v4l2_field field = common->fmt.fmt.pix.field;
567 struct video_obj *vid_ch = &ch->video;
568 unsigned int hpitch, vpitch, sizeimage;
570 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
571 if (ch->vpifparams.std_info.frm_fmt)
572 vid_ch->buf_field = V4L2_FIELD_NONE;
574 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
576 vid_ch->buf_field = common->fmt.fmt.pix.field;
579 sizeimage = common->fmt.fmt.pix.sizeimage;
581 hpitch = common->fmt.fmt.pix.bytesperline;
582 vpitch = sizeimage / (hpitch * 2);
583 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
584 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
585 common->ytop_off = 0;
586 common->ybtm_off = hpitch;
587 common->ctop_off = sizeimage / 2;
588 common->cbtm_off = sizeimage / 2 + hpitch;
589 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
590 common->ytop_off = 0;
591 common->ybtm_off = sizeimage / 4;
592 common->ctop_off = sizeimage / 2;
593 common->cbtm_off = common->ctop_off + sizeimage / 4;
594 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
595 common->ybtm_off = 0;
596 common->ytop_off = sizeimage / 4;
597 common->cbtm_off = sizeimage / 2;
598 common->ctop_off = common->cbtm_off + sizeimage / 4;
601 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
602 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
603 vpifparams->video_params.storage_mode = 1;
605 vpifparams->video_params.storage_mode = 0;
608 if (ch->vpifparams.std_info.frm_fmt == 1) {
609 vpifparams->video_params.hpitch =
610 common->fmt.fmt.pix.bytesperline;
612 if ((field == V4L2_FIELD_ANY) ||
613 (field == V4L2_FIELD_INTERLACED))
614 vpifparams->video_params.hpitch =
615 common->fmt.fmt.pix.bytesperline * 2;
617 vpifparams->video_params.hpitch =
618 common->fmt.fmt.pix.bytesperline;
621 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
624 static void vpif_config_format(struct channel_obj *ch)
626 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
628 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
629 if (config_params.numbuffers[ch->channel_id] == 0)
630 common->memory = V4L2_MEMORY_USERPTR;
632 common->memory = V4L2_MEMORY_MMAP;
634 common->fmt.fmt.pix.sizeimage =
635 config_params.channel_bufsize[ch->channel_id];
636 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
637 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
640 static int vpif_check_format(struct channel_obj *ch,
641 struct v4l2_pix_format *pixfmt)
643 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
644 enum v4l2_field field = pixfmt->field;
645 u32 sizeimage, hpitch, vpitch;
647 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
648 goto invalid_fmt_exit;
650 if (!(VPIF_VALID_FIELD(field)))
651 goto invalid_fmt_exit;
653 if (pixfmt->bytesperline <= 0)
654 goto invalid_pitch_exit;
656 sizeimage = pixfmt->sizeimage;
658 if (vpif_update_resolution(ch))
661 hpitch = pixfmt->bytesperline;
662 vpitch = sizeimage / (hpitch * 2);
664 /* Check for valid value of pitch */
665 if ((hpitch < ch->vpifparams.std_info.width) ||
666 (vpitch < ch->vpifparams.std_info.height))
667 goto invalid_pitch_exit;
669 /* Check for 8 byte alignment */
670 if (!ISALIGNED(hpitch)) {
671 vpif_err("invalid pitch alignment\n");
674 pixfmt->width = common->fmt.fmt.pix.width;
675 pixfmt->height = common->fmt.fmt.pix.height;
680 vpif_err("invalid field format\n");
684 vpif_err("invalid pitch\n");
688 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
690 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
692 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
693 common->set_addr = ch3_set_videobuf_addr;
696 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
698 common->set_addr = ch2_set_videobuf_addr;
703 * vpif_mmap: It is used to map kernel space buffers into user spaces
705 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
707 struct vpif_fh *fh = filep->private_data;
708 struct channel_obj *ch = fh->channel;
709 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
712 vpif_dbg(2, debug, "vpif_mmap\n");
714 if (mutex_lock_interruptible(&common->lock))
716 ret = vb2_mmap(&common->buffer_queue, vma);
717 mutex_unlock(&common->lock);
722 * vpif_poll: It is used for select/poll system call
724 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
726 struct vpif_fh *fh = filep->private_data;
727 struct channel_obj *ch = fh->channel;
728 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
729 unsigned int res = 0;
731 if (common->started) {
732 mutex_lock(&common->lock);
733 res = vb2_poll(&common->buffer_queue, filep, wait);
734 mutex_unlock(&common->lock);
741 * vpif_open: It creates object of file handle structure and stores it in
742 * private_data member of filepointer
744 static int vpif_open(struct file *filep)
746 struct video_device *vdev = video_devdata(filep);
747 struct channel_obj *ch = video_get_drvdata(vdev);
748 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
751 /* Allocate memory for the file handle object */
752 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
754 vpif_err("unable to allocate memory for file handle object\n");
758 if (mutex_lock_interruptible(&common->lock)) {
762 /* store pointer to fh in private_data member of filep */
763 filep->private_data = fh;
766 if (!ch->initialized) {
769 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
772 /* Increment channel usrs counter */
773 atomic_inc(&ch->usrs);
774 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
775 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
776 /* Initialize priority of this instance to default priority */
777 fh->prio = V4L2_PRIORITY_UNSET;
778 v4l2_prio_open(&ch->prio, &fh->prio);
779 mutex_unlock(&common->lock);
785 * vpif_release: This function deletes buffer queue, frees the buffers and
786 * the vpif file handle
788 static int vpif_release(struct file *filep)
790 struct vpif_fh *fh = filep->private_data;
791 struct channel_obj *ch = fh->channel;
792 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
794 mutex_lock(&common->lock);
795 /* if this instance is doing IO */
796 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
797 /* Reset io_usrs member of channel object */
799 /* Free buffers allocated */
800 vb2_queue_release(&common->buffer_queue);
801 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
804 config_params.numbuffers[ch->channel_id];
807 /* Decrement channel usrs counter */
808 atomic_dec(&ch->usrs);
809 /* If this file handle has initialize encoder device, reset it */
813 /* Close the priority */
814 v4l2_prio_close(&ch->prio, fh->prio);
815 filep->private_data = NULL;
817 mutex_unlock(&common->lock);
823 /* functions implementing ioctls */
825 * vpif_querycap() - QUERYCAP handler
828 * @cap: ptr to v4l2_capability structure
830 static int vpif_querycap(struct file *file, void *priv,
831 struct v4l2_capability *cap)
833 struct vpif_display_config *config = vpif_dev->platform_data;
835 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
836 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
837 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
838 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
840 strlcpy(cap->card, config->card_name, sizeof(cap->card));
845 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
846 struct v4l2_fmtdesc *fmt)
848 if (fmt->index != 0) {
849 vpif_err("Invalid format index\n");
853 /* Fill in the information about format */
854 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
855 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
856 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
861 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
862 struct v4l2_format *fmt)
864 struct vpif_fh *fh = priv;
865 struct channel_obj *ch = fh->channel;
866 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
868 /* Check the validity of the buffer type */
869 if (common->fmt.type != fmt->type)
872 if (vpif_update_resolution(ch))
878 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
879 struct v4l2_format *fmt)
881 struct vpif_fh *fh = priv;
882 struct v4l2_pix_format *pixfmt;
883 struct channel_obj *ch = fh->channel;
884 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
887 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
888 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
889 if (!fh->initialized) {
890 vpif_dbg(1, debug, "Channel Busy\n");
894 /* Check for the priority */
895 ret = v4l2_prio_check(&ch->prio, fh->prio);
901 if (common->started) {
902 vpif_dbg(1, debug, "Streaming in progress\n");
906 pixfmt = &fmt->fmt.pix;
907 /* Check for valid field format */
908 ret = vpif_check_format(ch, pixfmt);
912 /* store the pix format in the channel object */
913 common->fmt.fmt.pix = *pixfmt;
914 /* store the format in the channel object */
919 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
920 struct v4l2_format *fmt)
922 struct vpif_fh *fh = priv;
923 struct channel_obj *ch = fh->channel;
924 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
925 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
928 ret = vpif_check_format(ch, pixfmt);
930 *pixfmt = common->fmt.fmt.pix;
931 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
937 static int vpif_reqbufs(struct file *file, void *priv,
938 struct v4l2_requestbuffers *reqbuf)
940 struct vpif_fh *fh = priv;
941 struct channel_obj *ch = fh->channel;
942 struct common_obj *common;
943 enum v4l2_field field;
948 /* This file handle has not initialized the channel,
949 It is not allowed to do settings */
950 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
951 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
952 if (!fh->initialized) {
953 vpif_err("Channel Busy\n");
958 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
961 index = VPIF_VIDEO_INDEX;
963 common = &ch->common[index];
965 if (common->fmt.type != reqbuf->type || !vpif_dev)
967 if (0 != common->io_usrs)
970 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
971 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
972 field = V4L2_FIELD_INTERLACED;
974 field = common->fmt.fmt.pix.field;
976 field = V4L2_VBI_INTERLACED;
978 /* Initialize videobuf2 queue as per the buffer type */
979 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
980 if (IS_ERR(common->alloc_ctx)) {
981 vpif_err("Failed to get the context\n");
982 return PTR_ERR(common->alloc_ctx);
984 q = &common->buffer_queue;
985 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
986 q->io_modes = VB2_MMAP | VB2_USERPTR;
988 q->ops = &video_qops;
989 q->mem_ops = &vb2_dma_contig_memops;
990 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
991 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
992 q->min_buffers_needed = 1;
994 ret = vb2_queue_init(q);
996 vpif_err("vpif_display: vb2_queue_init() failed\n");
997 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1000 /* Set io allowed member of file handle to TRUE */
1001 fh->io_allowed[index] = 1;
1002 /* Increment io usrs member of channel object to 1 */
1003 common->io_usrs = 1;
1004 /* Store type of memory requested in channel object */
1005 common->memory = reqbuf->memory;
1006 INIT_LIST_HEAD(&common->dma_queue);
1007 /* Allocate buffers */
1008 return vb2_reqbufs(&common->buffer_queue, reqbuf);
1011 static int vpif_querybuf(struct file *file, void *priv,
1012 struct v4l2_buffer *tbuf)
1014 struct vpif_fh *fh = priv;
1015 struct channel_obj *ch = fh->channel;
1016 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1018 if (common->fmt.type != tbuf->type)
1021 return vb2_querybuf(&common->buffer_queue, tbuf);
1024 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1026 struct vpif_fh *fh = NULL;
1027 struct channel_obj *ch = NULL;
1028 struct common_obj *common = NULL;
1038 common = &(ch->common[VPIF_VIDEO_INDEX]);
1039 if (common->fmt.type != buf->type)
1042 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1043 vpif_err("fh->io_allowed\n");
1047 return vb2_qbuf(&common->buffer_queue, buf);
1050 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1052 struct vpif_fh *fh = priv;
1053 struct channel_obj *ch = fh->channel;
1054 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1057 if (!(std_id & VPIF_V4L2_STD))
1060 if (common->started) {
1061 vpif_err("streaming in progress\n");
1065 /* Call encoder subdevice function to set the standard */
1066 ch->video.stdid = std_id;
1067 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1068 /* Get the information about the standard */
1069 if (vpif_update_resolution(ch))
1072 if ((ch->vpifparams.std_info.width *
1073 ch->vpifparams.std_info.height * 2) >
1074 config_params.channel_bufsize[ch->channel_id]) {
1075 vpif_err("invalid std for this size\n");
1079 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1080 /* Configure the default format information */
1081 vpif_config_format(ch);
1083 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1084 s_std_output, std_id);
1086 vpif_err("Failed to set output standard\n");
1090 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1093 vpif_err("Failed to set standard for sub devices\n");
1097 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1099 struct vpif_fh *fh = priv;
1100 struct channel_obj *ch = fh->channel;
1102 *std = ch->video.stdid;
1106 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1108 struct vpif_fh *fh = priv;
1109 struct channel_obj *ch = fh->channel;
1110 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1112 return vb2_dqbuf(&common->buffer_queue, p,
1113 (file->f_flags & O_NONBLOCK));
1116 static int vpif_streamon(struct file *file, void *priv,
1117 enum v4l2_buf_type buftype)
1119 struct vpif_fh *fh = priv;
1120 struct channel_obj *ch = fh->channel;
1121 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1122 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1125 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1126 vpif_err("buffer type not supported\n");
1130 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1131 vpif_err("fh->io_allowed\n");
1135 /* If Streaming is already started, return error */
1136 if (common->started) {
1137 vpif_err("channel->started\n");
1141 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1142 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1143 ch->vpifparams.std_info.ycmux_mode == 0)
1144 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1145 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1146 vpif_err("other channel is using\n");
1150 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1154 /* Call vb2_streamon to start streaming in videobuf2 */
1155 ret = vb2_streamon(&common->buffer_queue, buftype);
1157 vpif_err("vb2_streamon\n");
1164 static int vpif_streamoff(struct file *file, void *priv,
1165 enum v4l2_buf_type buftype)
1167 struct vpif_fh *fh = priv;
1168 struct channel_obj *ch = fh->channel;
1169 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1170 struct vpif_display_config *vpif_config_data =
1171 vpif_dev->platform_data;
1173 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1174 vpif_err("buffer type not supported\n");
1178 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1179 vpif_err("fh->io_allowed\n");
1183 if (!common->started) {
1184 vpif_err("channel->started\n");
1188 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1189 /* disable channel */
1190 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1191 if (vpif_config_data->
1192 chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
1193 channel2_clipping_enable(0);
1195 channel2_intr_enable(0);
1197 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1198 (2 == common->started)) {
1199 if (vpif_config_data->
1200 chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
1201 channel3_clipping_enable(0);
1203 channel3_intr_enable(0);
1207 common->started = 0;
1208 return vb2_streamoff(&common->buffer_queue, buftype);
1211 static int vpif_cropcap(struct file *file, void *priv,
1212 struct v4l2_cropcap *crop)
1214 struct vpif_fh *fh = priv;
1215 struct channel_obj *ch = fh->channel;
1216 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1217 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1220 crop->bounds.left = crop->bounds.top = 0;
1221 crop->defrect.left = crop->defrect.top = 0;
1222 crop->defrect.height = crop->bounds.height = common->height;
1223 crop->defrect.width = crop->bounds.width = common->width;
1228 static int vpif_enum_output(struct file *file, void *fh,
1229 struct v4l2_output *output)
1232 struct vpif_display_config *config = vpif_dev->platform_data;
1233 struct vpif_display_chan_config *chan_cfg;
1234 struct vpif_fh *vpif_handler = fh;
1235 struct channel_obj *ch = vpif_handler->channel;
1237 chan_cfg = &config->chan_config[ch->channel_id];
1238 if (output->index >= chan_cfg->output_count) {
1239 vpif_dbg(1, debug, "Invalid output index\n");
1243 *output = chan_cfg->outputs[output->index].output;
1248 * vpif_output_to_subdev() - Maps output to sub device
1249 * @vpif_cfg - global config ptr
1250 * @chan_cfg - channel config ptr
1251 * @index - Given output index from application
1253 * lookup the sub device information for a given output index.
1254 * we report all the output to application. output table also
1255 * has sub device name for the each output
1258 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
1259 struct vpif_display_chan_config *chan_cfg, int index)
1261 struct vpif_subdev_info *subdev_info;
1262 const char *subdev_name;
1265 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
1267 if (chan_cfg->outputs == NULL)
1270 subdev_name = chan_cfg->outputs[index].subdev_name;
1271 if (subdev_name == NULL)
1274 /* loop through the sub device list to get the sub device info */
1275 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1276 subdev_info = &vpif_cfg->subdevinfo[i];
1277 if (!strcmp(subdev_info->name, subdev_name))
1284 * vpif_set_output() - Select an output
1285 * @vpif_cfg - global config ptr
1287 * @index - Given output index from application
1289 * Select the given output.
1291 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
1292 struct channel_obj *ch, int index)
1294 struct vpif_display_chan_config *chan_cfg =
1295 &vpif_cfg->chan_config[ch->channel_id];
1296 struct vpif_subdev_info *subdev_info = NULL;
1297 struct v4l2_subdev *sd = NULL;
1298 u32 input = 0, output = 0;
1302 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
1303 if (sd_index >= 0) {
1304 sd = vpif_obj.sd[sd_index];
1305 subdev_info = &vpif_cfg->subdevinfo[sd_index];
1309 input = chan_cfg->outputs[index].input_route;
1310 output = chan_cfg->outputs[index].output_route;
1311 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1312 if (ret < 0 && ret != -ENOIOCTLCMD) {
1313 vpif_err("Failed to set output\n");
1318 ch->output_idx = index;
1320 if (chan_cfg->outputs != NULL)
1321 /* update tvnorms from the sub device output info */
1322 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
1326 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1328 struct vpif_display_config *config = vpif_dev->platform_data;
1329 struct vpif_display_chan_config *chan_cfg;
1330 struct vpif_fh *fh = priv;
1331 struct channel_obj *ch = fh->channel;
1332 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1334 chan_cfg = &config->chan_config[ch->channel_id];
1336 if (i >= chan_cfg->output_count)
1339 if (common->started) {
1340 vpif_err("Streaming in progress\n");
1344 return vpif_set_output(config, ch, i);
1347 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1349 struct vpif_fh *fh = priv;
1350 struct channel_obj *ch = fh->channel;
1352 *i = ch->output_idx;
1357 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1359 struct vpif_fh *fh = priv;
1360 struct channel_obj *ch = fh->channel;
1362 *p = v4l2_prio_max(&ch->prio);
1367 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1369 struct vpif_fh *fh = priv;
1370 struct channel_obj *ch = fh->channel;
1372 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1376 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1378 * @priv: file handle
1379 * @timings: input timings
1382 vpif_enum_dv_timings(struct file *file, void *priv,
1383 struct v4l2_enum_dv_timings *timings)
1385 struct vpif_fh *fh = priv;
1386 struct channel_obj *ch = fh->channel;
1389 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1390 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1396 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1398 * @priv: file handle
1399 * @timings: digital video timings
1401 static int vpif_s_dv_timings(struct file *file, void *priv,
1402 struct v4l2_dv_timings *timings)
1404 struct vpif_fh *fh = priv;
1405 struct channel_obj *ch = fh->channel;
1406 struct vpif_params *vpifparams = &ch->vpifparams;
1407 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1408 struct video_obj *vid_ch = &ch->video;
1409 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1412 if (timings->type != V4L2_DV_BT_656_1120) {
1413 vpif_dbg(2, debug, "Timing type not defined\n");
1417 /* Configure subdevice timings, if any */
1418 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1419 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1422 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1426 if (!(timings->bt.width && timings->bt.height &&
1427 (timings->bt.hbackporch ||
1428 timings->bt.hfrontporch ||
1429 timings->bt.hsync) &&
1430 timings->bt.vfrontporch &&
1431 (timings->bt.vbackporch ||
1432 timings->bt.vsync))) {
1433 vpif_dbg(2, debug, "Timings for width, height, "
1434 "horizontal back porch, horizontal sync, "
1435 "horizontal front porch, vertical back porch, "
1436 "vertical sync and vertical back porch "
1437 "must be defined\n");
1441 vid_ch->dv_timings = *timings;
1443 /* Configure video port timings */
1445 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1446 std_info->sav2eav = bt->width;
1449 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1451 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1452 if (bt->interlaced) {
1453 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1454 std_info->l5 = std_info->vsize/2 -
1455 (bt->vfrontporch - 1);
1456 std_info->l7 = std_info->vsize/2 + 1;
1457 std_info->l9 = std_info->l7 + bt->il_vsync +
1458 bt->il_vbackporch + 1;
1459 std_info->l11 = std_info->vsize -
1460 (bt->il_vfrontporch - 1);
1462 vpif_dbg(2, debug, "Required timing values for "
1463 "interlaced BT format missing\n");
1467 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1469 strncpy(std_info->name, "Custom timings BT656/1120",
1471 std_info->width = bt->width;
1472 std_info->height = bt->height;
1473 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1474 std_info->ycmux_mode = 0;
1475 std_info->capture_format = 0;
1476 std_info->vbi_supported = 0;
1477 std_info->hd_sd = 1;
1478 std_info->stdid = 0;
1485 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1487 * @priv: file handle
1488 * @timings: digital video timings
1490 static int vpif_g_dv_timings(struct file *file, void *priv,
1491 struct v4l2_dv_timings *timings)
1493 struct vpif_fh *fh = priv;
1494 struct channel_obj *ch = fh->channel;
1495 struct video_obj *vid_ch = &ch->video;
1497 *timings = vid_ch->dv_timings;
1503 * vpif_log_status() - Status information
1505 * @priv: file handle
1509 static int vpif_log_status(struct file *filep, void *priv)
1511 /* status for sub devices */
1512 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1517 /* vpif display ioctl operations */
1518 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1519 .vidioc_querycap = vpif_querycap,
1520 .vidioc_g_priority = vpif_g_priority,
1521 .vidioc_s_priority = vpif_s_priority,
1522 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1523 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1524 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1525 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1526 .vidioc_reqbufs = vpif_reqbufs,
1527 .vidioc_querybuf = vpif_querybuf,
1528 .vidioc_qbuf = vpif_qbuf,
1529 .vidioc_dqbuf = vpif_dqbuf,
1530 .vidioc_streamon = vpif_streamon,
1531 .vidioc_streamoff = vpif_streamoff,
1532 .vidioc_s_std = vpif_s_std,
1533 .vidioc_g_std = vpif_g_std,
1534 .vidioc_enum_output = vpif_enum_output,
1535 .vidioc_s_output = vpif_s_output,
1536 .vidioc_g_output = vpif_g_output,
1537 .vidioc_cropcap = vpif_cropcap,
1538 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1539 .vidioc_s_dv_timings = vpif_s_dv_timings,
1540 .vidioc_g_dv_timings = vpif_g_dv_timings,
1541 .vidioc_log_status = vpif_log_status,
1544 static const struct v4l2_file_operations vpif_fops = {
1545 .owner = THIS_MODULE,
1547 .release = vpif_release,
1548 .unlocked_ioctl = video_ioctl2,
1553 static struct video_device vpif_video_template = {
1556 .ioctl_ops = &vpif_ioctl_ops,
1559 /*Configure the channels, buffer sizei, request irq */
1560 static int initialize_vpif(void)
1562 int free_channel_objects_index;
1563 int free_buffer_channel_index;
1564 int free_buffer_index;
1567 /* Default number of buffers should be 3 */
1568 if ((ch2_numbuffers > 0) &&
1569 (ch2_numbuffers < config_params.min_numbuffers))
1570 ch2_numbuffers = config_params.min_numbuffers;
1571 if ((ch3_numbuffers > 0) &&
1572 (ch3_numbuffers < config_params.min_numbuffers))
1573 ch3_numbuffers = config_params.min_numbuffers;
1575 /* Set buffer size to min buffers size if invalid buffer size is
1577 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1579 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1580 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1582 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1584 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1586 if (ch2_numbuffers) {
1587 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1590 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1592 if (ch3_numbuffers) {
1593 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1597 /* Allocate memory for six channel objects */
1598 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1600 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1601 /* If memory allocation fails, return error */
1602 if (!vpif_obj.dev[i]) {
1603 free_channel_objects_index = i;
1605 goto vpif_init_free_channel_objects;
1609 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1610 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1611 free_buffer_index = config_params.numbuffers[i - 1];
1615 vpif_init_free_channel_objects:
1616 for (j = 0; j < free_channel_objects_index; j++)
1617 kfree(vpif_obj.dev[j]);
1621 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1622 struct v4l2_subdev *subdev,
1623 struct v4l2_async_subdev *asd)
1627 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1628 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1630 vpif_obj.sd[i] = subdev;
1631 vpif_obj.sd[i]->grp_id = 1 << i;
1638 static int vpif_probe_complete(void)
1640 struct common_obj *common;
1641 struct channel_obj *ch;
1644 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1645 ch = vpif_obj.dev[j];
1646 /* Initialize field of the channel objects */
1647 atomic_set(&ch->usrs, 0);
1648 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1649 ch->common[k].numbuffers = 0;
1650 common = &ch->common[k];
1651 common->io_usrs = 0;
1652 common->started = 0;
1653 spin_lock_init(&common->irqlock);
1654 mutex_init(&common->lock);
1655 common->numbuffers = 0;
1656 common->set_addr = NULL;
1657 common->ytop_off = 0;
1658 common->ybtm_off = 0;
1659 common->ctop_off = 0;
1660 common->cbtm_off = 0;
1661 common->cur_frm = NULL;
1662 common->next_frm = NULL;
1663 memset(&common->fmt, 0, sizeof(common->fmt));
1664 common->numbuffers = config_params.numbuffers[k];
1666 ch->initialized = 0;
1667 if (vpif_obj.config->subdev_count)
1668 ch->sd = vpif_obj.sd[0];
1671 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1672 config_params.numbuffers[ch->channel_id];
1674 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1676 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1678 /* Initialize prio member of channel object */
1679 v4l2_prio_init(&ch->prio);
1680 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1681 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1682 ch->video_dev->lock = &common->lock;
1683 video_set_drvdata(ch->video_dev, ch);
1685 /* select output 0 */
1686 err = vpif_set_output(vpif_obj.config, ch, 0);
1690 /* register video device */
1691 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1692 (int)ch, (int)&ch->video_dev);
1694 err = video_register_device(ch->video_dev,
1695 VFL_TYPE_GRABBER, (j ? 3 : 2));
1703 for (k = 0; k < j; k++) {
1704 ch = vpif_obj.dev[k];
1705 video_unregister_device(ch->video_dev);
1706 video_device_release(ch->video_dev);
1707 ch->video_dev = NULL;
1712 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1714 return vpif_probe_complete();
1718 * vpif_probe: This function creates device entries by register itself to the
1719 * V4L2 driver and initializes fields of each channel objects
1721 static __init int vpif_probe(struct platform_device *pdev)
1723 struct vpif_subdev_info *subdevdata;
1724 int i, j = 0, err = 0;
1726 struct i2c_adapter *i2c_adap;
1727 struct channel_obj *ch;
1728 struct video_device *vfd;
1729 struct resource *res;
1733 vpif_dev = &pdev->dev;
1734 err = initialize_vpif();
1737 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1741 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1743 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1747 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1748 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1749 IRQF_SHARED, "VPIF_Display",
1750 (void *)(&vpif_obj.dev[res_idx]->
1754 vpif_err("VPIF IRQ request failed\n");
1755 goto vpif_unregister;
1760 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1761 /* Get the pointer to the channel object */
1762 ch = vpif_obj.dev[i];
1764 /* Allocate memory for video device */
1765 vfd = video_device_alloc();
1767 for (j = 0; j < i; j++) {
1768 ch = vpif_obj.dev[j];
1769 video_device_release(ch->video_dev);
1772 goto vpif_unregister;
1775 /* Initialize field of video device */
1776 *vfd = vpif_video_template;
1777 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1778 vfd->release = video_device_release;
1779 vfd->vfl_dir = VFL_DIR_TX;
1780 snprintf(vfd->name, sizeof(vfd->name),
1781 "VPIF_Display_DRIVER_V%s",
1782 VPIF_DISPLAY_VERSION);
1784 /* Set video_dev to the video device */
1785 ch->video_dev = vfd;
1788 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1790 size = resource_size(res);
1791 /* The resources are divided into two equal memory and when
1792 * we have HD output we can add them together
1794 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1795 ch = vpif_obj.dev[j];
1798 /* only enabled if second resource exists */
1799 config_params.video_limit[ch->channel_id] = 0;
1801 config_params.video_limit[ch->channel_id] =
1805 vpif_obj.config = pdev->dev.platform_data;
1806 subdev_count = vpif_obj.config->subdev_count;
1807 subdevdata = vpif_obj.config->subdevinfo;
1808 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1810 if (vpif_obj.sd == NULL) {
1811 vpif_err("unable to allocate memory for subdevice pointers\n");
1816 if (!vpif_obj.config->asd_sizes) {
1817 i2c_adap = i2c_get_adapter(1);
1818 for (i = 0; i < subdev_count; i++) {
1820 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1825 if (!vpif_obj.sd[i]) {
1826 vpif_err("Error registering v4l2 subdevice\n");
1828 goto probe_subdev_out;
1832 vpif_obj.sd[i]->grp_id = 1 << i;
1834 vpif_probe_complete();
1836 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1837 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1838 vpif_obj.notifier.bound = vpif_async_bound;
1839 vpif_obj.notifier.complete = vpif_async_complete;
1840 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1841 &vpif_obj.notifier);
1843 vpif_err("Error registering async notifier\n");
1845 goto probe_subdev_out;
1854 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1855 ch = vpif_obj.dev[i];
1856 /* Note: does nothing if ch->video_dev == NULL */
1857 video_device_release(ch->video_dev);
1860 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1866 * vpif_remove: It un-register channels from V4L2 driver
1868 static int vpif_remove(struct platform_device *device)
1870 struct channel_obj *ch;
1873 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1876 /* un-register device */
1877 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1878 /* Get the pointer to the channel object */
1879 ch = vpif_obj.dev[i];
1880 /* Unregister video device */
1881 video_unregister_device(ch->video_dev);
1883 ch->video_dev = NULL;
1884 kfree(vpif_obj.dev[i]);
1891 static int vpif_suspend(struct device *dev)
1893 struct common_obj *common;
1894 struct channel_obj *ch;
1897 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1898 /* Get the pointer to the channel object */
1899 ch = vpif_obj.dev[i];
1900 common = &ch->common[VPIF_VIDEO_INDEX];
1901 mutex_lock(&common->lock);
1902 if (atomic_read(&ch->usrs) && common->io_usrs) {
1903 /* Disable channel */
1904 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1906 channel2_intr_enable(0);
1908 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1909 common->started == 2) {
1911 channel3_intr_enable(0);
1914 mutex_unlock(&common->lock);
1920 static int vpif_resume(struct device *dev)
1923 struct common_obj *common;
1924 struct channel_obj *ch;
1927 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1928 /* Get the pointer to the channel object */
1929 ch = vpif_obj.dev[i];
1930 common = &ch->common[VPIF_VIDEO_INDEX];
1931 mutex_lock(&common->lock);
1932 if (atomic_read(&ch->usrs) && common->io_usrs) {
1933 /* Enable channel */
1934 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1936 channel2_intr_enable(1);
1938 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1939 common->started == 2) {
1941 channel3_intr_enable(1);
1944 mutex_unlock(&common->lock);
1950 static const struct dev_pm_ops vpif_pm = {
1951 .suspend = vpif_suspend,
1952 .resume = vpif_resume,
1955 #define vpif_pm_ops (&vpif_pm)
1957 #define vpif_pm_ops NULL
1960 static __refdata struct platform_driver vpif_driver = {
1962 .name = "vpif_display",
1963 .owner = THIS_MODULE,
1966 .probe = vpif_probe,
1967 .remove = vpif_remove,
1970 module_platform_driver(vpif_driver);