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 /* release all active buffers */
324 spin_lock_irqsave(&common->irqlock, flags);
325 while (!list_empty(&common->dma_queue)) {
326 common->next_frm = list_entry(common->dma_queue.next,
327 struct vpif_disp_buffer, list);
328 list_del(&common->next_frm->list);
329 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
331 spin_unlock_irqrestore(&common->irqlock, flags);
336 static struct vb2_ops video_qops = {
337 .queue_setup = vpif_buffer_queue_setup,
338 .wait_prepare = vpif_wait_prepare,
339 .wait_finish = vpif_wait_finish,
340 .buf_init = vpif_buffer_init,
341 .buf_prepare = vpif_buffer_prepare,
342 .start_streaming = vpif_start_streaming,
343 .stop_streaming = vpif_stop_streaming,
344 .buf_cleanup = vpif_buf_cleanup,
345 .buf_queue = vpif_buffer_queue,
348 static void process_progressive_mode(struct common_obj *common)
350 unsigned long addr = 0;
352 spin_lock(&common->irqlock);
353 /* Get the next buffer from buffer queue */
354 common->next_frm = list_entry(common->dma_queue.next,
355 struct vpif_disp_buffer, list);
356 /* Remove that buffer from the buffer queue */
357 list_del(&common->next_frm->list);
358 spin_unlock(&common->irqlock);
359 /* Mark status of the buffer as active */
360 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
362 /* Set top and bottom field addrs in VPIF registers */
363 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
364 common->set_addr(addr + common->ytop_off,
365 addr + common->ybtm_off,
366 addr + common->ctop_off,
367 addr + common->cbtm_off);
370 static void process_interlaced_mode(int fid, struct common_obj *common)
372 /* device field id and local field id are in sync */
373 /* If this is even field */
375 if (common->cur_frm == common->next_frm)
378 /* one frame is displayed If next frame is
379 * available, release cur_frm and move on */
380 /* Copy frame display time */
381 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
382 /* Change status of the cur_frm */
383 vb2_buffer_done(&common->cur_frm->vb,
385 /* Make cur_frm pointing to next_frm */
386 common->cur_frm = common->next_frm;
388 } else if (1 == fid) { /* odd field */
389 spin_lock(&common->irqlock);
390 if (list_empty(&common->dma_queue)
391 || (common->cur_frm != common->next_frm)) {
392 spin_unlock(&common->irqlock);
395 spin_unlock(&common->irqlock);
396 /* one field is displayed configure the next
397 * frame if it is available else hold on current
399 /* Get next from the buffer queue */
400 process_progressive_mode(common);
405 * vpif_channel_isr: It changes status of the displayed buffer, takes next
406 * buffer from the queue and sets its address in VPIF registers
408 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
410 struct vpif_device *dev = &vpif_obj;
411 struct channel_obj *ch;
412 struct common_obj *common;
413 enum v4l2_field field;
417 channel_id = *(int *)(dev_id);
418 if (!vpif_intr_status(channel_id + 2))
421 ch = dev->dev[channel_id];
422 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
423 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
424 common = &ch->common[i];
425 /* If streaming is started in this channel */
426 if (0 == common->started)
429 if (1 == ch->vpifparams.std_info.frm_fmt) {
430 spin_lock(&common->irqlock);
431 if (list_empty(&common->dma_queue)) {
432 spin_unlock(&common->irqlock);
435 spin_unlock(&common->irqlock);
437 /* Progressive mode */
438 if (!channel_first_int[i][channel_id]) {
439 /* Mark status of the cur_frm to
440 * done and unlock semaphore on it */
441 v4l2_get_timestamp(&common->cur_frm->vb.
443 vb2_buffer_done(&common->cur_frm->vb,
445 /* Make cur_frm pointing to next_frm */
446 common->cur_frm = common->next_frm;
449 channel_first_int[i][channel_id] = 0;
450 process_progressive_mode(common);
452 /* Interlaced mode */
453 /* If it is first interrupt, ignore it */
455 if (channel_first_int[i][channel_id]) {
456 channel_first_int[i][channel_id] = 0;
462 /* Get field id from VPIF registers */
463 fid = vpif_channel_getfid(ch->channel_id + 2);
464 /* If fid does not match with stored field id */
465 if (fid != ch->field_id) {
466 /* Make them in sync */
473 process_interlaced_mode(fid, common);
480 static int vpif_update_std_info(struct channel_obj *ch)
482 struct video_obj *vid_ch = &ch->video;
483 struct vpif_params *vpifparams = &ch->vpifparams;
484 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
485 const struct vpif_channel_config_params *config;
489 for (i = 0; i < vpif_ch_params_count; i++) {
490 config = &vpif_ch_params[i];
491 if (config->hd_sd == 0) {
492 vpif_dbg(2, debug, "SD format\n");
493 if (config->stdid & vid_ch->stdid) {
494 memcpy(std_info, config, sizeof(*config));
500 if (i == vpif_ch_params_count) {
501 vpif_dbg(1, debug, "Format not found\n");
508 static int vpif_update_resolution(struct channel_obj *ch)
510 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
511 struct video_obj *vid_ch = &ch->video;
512 struct vpif_params *vpifparams = &ch->vpifparams;
513 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
515 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
519 if (vpif_update_std_info(ch))
523 common->fmt.fmt.pix.width = std_info->width;
524 common->fmt.fmt.pix.height = std_info->height;
525 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
526 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
528 /* Set height and width paramateres */
529 common->height = std_info->height;
530 common->width = std_info->width;
536 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
537 * in the top and bottom field
539 static void vpif_calculate_offsets(struct channel_obj *ch)
541 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
542 struct vpif_params *vpifparams = &ch->vpifparams;
543 enum v4l2_field field = common->fmt.fmt.pix.field;
544 struct video_obj *vid_ch = &ch->video;
545 unsigned int hpitch, vpitch, sizeimage;
547 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
548 if (ch->vpifparams.std_info.frm_fmt)
549 vid_ch->buf_field = V4L2_FIELD_NONE;
551 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
553 vid_ch->buf_field = common->fmt.fmt.pix.field;
556 sizeimage = common->fmt.fmt.pix.sizeimage;
558 hpitch = common->fmt.fmt.pix.bytesperline;
559 vpitch = sizeimage / (hpitch * 2);
560 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
561 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
562 common->ytop_off = 0;
563 common->ybtm_off = hpitch;
564 common->ctop_off = sizeimage / 2;
565 common->cbtm_off = sizeimage / 2 + hpitch;
566 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
567 common->ytop_off = 0;
568 common->ybtm_off = sizeimage / 4;
569 common->ctop_off = sizeimage / 2;
570 common->cbtm_off = common->ctop_off + sizeimage / 4;
571 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
572 common->ybtm_off = 0;
573 common->ytop_off = sizeimage / 4;
574 common->cbtm_off = sizeimage / 2;
575 common->ctop_off = common->cbtm_off + sizeimage / 4;
578 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
579 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
580 vpifparams->video_params.storage_mode = 1;
582 vpifparams->video_params.storage_mode = 0;
585 if (ch->vpifparams.std_info.frm_fmt == 1) {
586 vpifparams->video_params.hpitch =
587 common->fmt.fmt.pix.bytesperline;
589 if ((field == V4L2_FIELD_ANY) ||
590 (field == V4L2_FIELD_INTERLACED))
591 vpifparams->video_params.hpitch =
592 common->fmt.fmt.pix.bytesperline * 2;
594 vpifparams->video_params.hpitch =
595 common->fmt.fmt.pix.bytesperline;
598 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
601 static void vpif_config_format(struct channel_obj *ch)
603 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
605 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
606 if (config_params.numbuffers[ch->channel_id] == 0)
607 common->memory = V4L2_MEMORY_USERPTR;
609 common->memory = V4L2_MEMORY_MMAP;
611 common->fmt.fmt.pix.sizeimage =
612 config_params.channel_bufsize[ch->channel_id];
613 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
614 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
617 static int vpif_check_format(struct channel_obj *ch,
618 struct v4l2_pix_format *pixfmt)
620 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
621 enum v4l2_field field = pixfmt->field;
622 u32 sizeimage, hpitch, vpitch;
624 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
625 goto invalid_fmt_exit;
627 if (!(VPIF_VALID_FIELD(field)))
628 goto invalid_fmt_exit;
630 if (pixfmt->bytesperline <= 0)
631 goto invalid_pitch_exit;
633 sizeimage = pixfmt->sizeimage;
635 if (vpif_update_resolution(ch))
638 hpitch = pixfmt->bytesperline;
639 vpitch = sizeimage / (hpitch * 2);
641 /* Check for valid value of pitch */
642 if ((hpitch < ch->vpifparams.std_info.width) ||
643 (vpitch < ch->vpifparams.std_info.height))
644 goto invalid_pitch_exit;
646 /* Check for 8 byte alignment */
647 if (!ISALIGNED(hpitch)) {
648 vpif_err("invalid pitch alignment\n");
651 pixfmt->width = common->fmt.fmt.pix.width;
652 pixfmt->height = common->fmt.fmt.pix.height;
657 vpif_err("invalid field format\n");
661 vpif_err("invalid pitch\n");
665 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
667 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
669 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
670 common->set_addr = ch3_set_videobuf_addr;
673 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
675 common->set_addr = ch2_set_videobuf_addr;
680 * vpif_mmap: It is used to map kernel space buffers into user spaces
682 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
684 struct vpif_fh *fh = filep->private_data;
685 struct channel_obj *ch = fh->channel;
686 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
689 vpif_dbg(2, debug, "vpif_mmap\n");
691 if (mutex_lock_interruptible(&common->lock))
693 ret = vb2_mmap(&common->buffer_queue, vma);
694 mutex_unlock(&common->lock);
699 * vpif_poll: It is used for select/poll system call
701 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
703 struct vpif_fh *fh = filep->private_data;
704 struct channel_obj *ch = fh->channel;
705 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
706 unsigned int res = 0;
708 if (common->started) {
709 mutex_lock(&common->lock);
710 res = vb2_poll(&common->buffer_queue, filep, wait);
711 mutex_unlock(&common->lock);
718 * vpif_open: It creates object of file handle structure and stores it in
719 * private_data member of filepointer
721 static int vpif_open(struct file *filep)
723 struct video_device *vdev = video_devdata(filep);
724 struct channel_obj *ch = video_get_drvdata(vdev);
725 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
728 /* Allocate memory for the file handle object */
729 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
731 vpif_err("unable to allocate memory for file handle object\n");
735 if (mutex_lock_interruptible(&common->lock)) {
739 /* store pointer to fh in private_data member of filep */
740 filep->private_data = fh;
743 if (!ch->initialized) {
746 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
749 /* Increment channel usrs counter */
750 atomic_inc(&ch->usrs);
751 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
752 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
753 /* Initialize priority of this instance to default priority */
754 fh->prio = V4L2_PRIORITY_UNSET;
755 v4l2_prio_open(&ch->prio, &fh->prio);
756 mutex_unlock(&common->lock);
762 * vpif_release: This function deletes buffer queue, frees the buffers and
763 * the vpif file handle
765 static int vpif_release(struct file *filep)
767 struct vpif_fh *fh = filep->private_data;
768 struct channel_obj *ch = fh->channel;
769 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
771 mutex_lock(&common->lock);
772 /* if this instance is doing IO */
773 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
774 /* Reset io_usrs member of channel object */
776 /* Disable channel */
777 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
779 channel2_intr_enable(0);
781 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
782 (2 == common->started)) {
784 channel3_intr_enable(0);
788 /* Free buffers allocated */
789 vb2_queue_release(&common->buffer_queue);
790 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
793 config_params.numbuffers[ch->channel_id];
796 /* Decrement channel usrs counter */
797 atomic_dec(&ch->usrs);
798 /* If this file handle has initialize encoder device, reset it */
802 /* Close the priority */
803 v4l2_prio_close(&ch->prio, fh->prio);
804 filep->private_data = NULL;
806 mutex_unlock(&common->lock);
812 /* functions implementing ioctls */
814 * vpif_querycap() - QUERYCAP handler
817 * @cap: ptr to v4l2_capability structure
819 static int vpif_querycap(struct file *file, void *priv,
820 struct v4l2_capability *cap)
822 struct vpif_display_config *config = vpif_dev->platform_data;
824 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
825 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
826 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
827 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
829 strlcpy(cap->card, config->card_name, sizeof(cap->card));
834 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
835 struct v4l2_fmtdesc *fmt)
837 if (fmt->index != 0) {
838 vpif_err("Invalid format index\n");
842 /* Fill in the information about format */
843 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
844 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
845 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
850 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
851 struct v4l2_format *fmt)
853 struct vpif_fh *fh = priv;
854 struct channel_obj *ch = fh->channel;
855 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
857 /* Check the validity of the buffer type */
858 if (common->fmt.type != fmt->type)
861 if (vpif_update_resolution(ch))
867 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
868 struct v4l2_format *fmt)
870 struct vpif_fh *fh = priv;
871 struct v4l2_pix_format *pixfmt;
872 struct channel_obj *ch = fh->channel;
873 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
876 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
877 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
878 if (!fh->initialized) {
879 vpif_dbg(1, debug, "Channel Busy\n");
883 /* Check for the priority */
884 ret = v4l2_prio_check(&ch->prio, fh->prio);
890 if (common->started) {
891 vpif_dbg(1, debug, "Streaming in progress\n");
895 pixfmt = &fmt->fmt.pix;
896 /* Check for valid field format */
897 ret = vpif_check_format(ch, pixfmt);
901 /* store the pix format in the channel object */
902 common->fmt.fmt.pix = *pixfmt;
903 /* store the format in the channel object */
908 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
909 struct v4l2_format *fmt)
911 struct vpif_fh *fh = priv;
912 struct channel_obj *ch = fh->channel;
913 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
914 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
917 ret = vpif_check_format(ch, pixfmt);
919 *pixfmt = common->fmt.fmt.pix;
920 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
926 static int vpif_reqbufs(struct file *file, void *priv,
927 struct v4l2_requestbuffers *reqbuf)
929 struct vpif_fh *fh = priv;
930 struct channel_obj *ch = fh->channel;
931 struct common_obj *common;
932 enum v4l2_field field;
937 /* This file handle has not initialized the channel,
938 It is not allowed to do settings */
939 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
940 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
941 if (!fh->initialized) {
942 vpif_err("Channel Busy\n");
947 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
950 index = VPIF_VIDEO_INDEX;
952 common = &ch->common[index];
954 if (common->fmt.type != reqbuf->type || !vpif_dev)
956 if (0 != common->io_usrs)
959 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
960 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
961 field = V4L2_FIELD_INTERLACED;
963 field = common->fmt.fmt.pix.field;
965 field = V4L2_VBI_INTERLACED;
967 /* Initialize videobuf2 queue as per the buffer type */
968 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
969 if (IS_ERR(common->alloc_ctx)) {
970 vpif_err("Failed to get the context\n");
971 return PTR_ERR(common->alloc_ctx);
973 q = &common->buffer_queue;
974 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
975 q->io_modes = VB2_MMAP | VB2_USERPTR;
977 q->ops = &video_qops;
978 q->mem_ops = &vb2_dma_contig_memops;
979 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
980 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
981 q->min_buffers_needed = 1;
983 ret = vb2_queue_init(q);
985 vpif_err("vpif_display: vb2_queue_init() failed\n");
986 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
989 /* Set io allowed member of file handle to TRUE */
990 fh->io_allowed[index] = 1;
991 /* Increment io usrs member of channel object to 1 */
993 /* Store type of memory requested in channel object */
994 common->memory = reqbuf->memory;
995 INIT_LIST_HEAD(&common->dma_queue);
996 /* Allocate buffers */
997 return vb2_reqbufs(&common->buffer_queue, reqbuf);
1000 static int vpif_querybuf(struct file *file, void *priv,
1001 struct v4l2_buffer *tbuf)
1003 struct vpif_fh *fh = priv;
1004 struct channel_obj *ch = fh->channel;
1005 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1007 if (common->fmt.type != tbuf->type)
1010 return vb2_querybuf(&common->buffer_queue, tbuf);
1013 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1015 struct vpif_fh *fh = NULL;
1016 struct channel_obj *ch = NULL;
1017 struct common_obj *common = NULL;
1027 common = &(ch->common[VPIF_VIDEO_INDEX]);
1028 if (common->fmt.type != buf->type)
1031 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1032 vpif_err("fh->io_allowed\n");
1036 return vb2_qbuf(&common->buffer_queue, buf);
1039 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1041 struct vpif_fh *fh = priv;
1042 struct channel_obj *ch = fh->channel;
1043 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1046 if (!(std_id & VPIF_V4L2_STD))
1049 if (common->started) {
1050 vpif_err("streaming in progress\n");
1054 /* Call encoder subdevice function to set the standard */
1055 ch->video.stdid = std_id;
1056 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1057 /* Get the information about the standard */
1058 if (vpif_update_resolution(ch))
1061 if ((ch->vpifparams.std_info.width *
1062 ch->vpifparams.std_info.height * 2) >
1063 config_params.channel_bufsize[ch->channel_id]) {
1064 vpif_err("invalid std for this size\n");
1068 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1069 /* Configure the default format information */
1070 vpif_config_format(ch);
1072 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1073 s_std_output, std_id);
1075 vpif_err("Failed to set output standard\n");
1079 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1082 vpif_err("Failed to set standard for sub devices\n");
1086 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1088 struct vpif_fh *fh = priv;
1089 struct channel_obj *ch = fh->channel;
1091 *std = ch->video.stdid;
1095 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1097 struct vpif_fh *fh = priv;
1098 struct channel_obj *ch = fh->channel;
1099 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1101 return vb2_dqbuf(&common->buffer_queue, p,
1102 (file->f_flags & O_NONBLOCK));
1105 static int vpif_streamon(struct file *file, void *priv,
1106 enum v4l2_buf_type buftype)
1108 struct vpif_fh *fh = priv;
1109 struct channel_obj *ch = fh->channel;
1110 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1111 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1114 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1115 vpif_err("buffer type not supported\n");
1119 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1120 vpif_err("fh->io_allowed\n");
1124 /* If Streaming is already started, return error */
1125 if (common->started) {
1126 vpif_err("channel->started\n");
1130 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1131 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1132 ch->vpifparams.std_info.ycmux_mode == 0)
1133 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1134 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1135 vpif_err("other channel is using\n");
1139 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1143 /* Call vb2_streamon to start streaming in videobuf2 */
1144 ret = vb2_streamon(&common->buffer_queue, buftype);
1146 vpif_err("vb2_streamon\n");
1153 static int vpif_streamoff(struct file *file, void *priv,
1154 enum v4l2_buf_type buftype)
1156 struct vpif_fh *fh = priv;
1157 struct channel_obj *ch = fh->channel;
1158 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1159 struct vpif_display_config *vpif_config_data =
1160 vpif_dev->platform_data;
1162 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1163 vpif_err("buffer type not supported\n");
1167 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1168 vpif_err("fh->io_allowed\n");
1172 if (!common->started) {
1173 vpif_err("channel->started\n");
1177 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1178 /* disable channel */
1179 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1180 if (vpif_config_data->
1181 chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
1182 channel2_clipping_enable(0);
1184 channel2_intr_enable(0);
1186 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1187 (2 == common->started)) {
1188 if (vpif_config_data->
1189 chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
1190 channel3_clipping_enable(0);
1192 channel3_intr_enable(0);
1196 common->started = 0;
1197 return vb2_streamoff(&common->buffer_queue, buftype);
1200 static int vpif_cropcap(struct file *file, void *priv,
1201 struct v4l2_cropcap *crop)
1203 struct vpif_fh *fh = priv;
1204 struct channel_obj *ch = fh->channel;
1205 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1206 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1209 crop->bounds.left = crop->bounds.top = 0;
1210 crop->defrect.left = crop->defrect.top = 0;
1211 crop->defrect.height = crop->bounds.height = common->height;
1212 crop->defrect.width = crop->bounds.width = common->width;
1217 static int vpif_enum_output(struct file *file, void *fh,
1218 struct v4l2_output *output)
1221 struct vpif_display_config *config = vpif_dev->platform_data;
1222 struct vpif_display_chan_config *chan_cfg;
1223 struct vpif_fh *vpif_handler = fh;
1224 struct channel_obj *ch = vpif_handler->channel;
1226 chan_cfg = &config->chan_config[ch->channel_id];
1227 if (output->index >= chan_cfg->output_count) {
1228 vpif_dbg(1, debug, "Invalid output index\n");
1232 *output = chan_cfg->outputs[output->index].output;
1237 * vpif_output_to_subdev() - Maps output to sub device
1238 * @vpif_cfg - global config ptr
1239 * @chan_cfg - channel config ptr
1240 * @index - Given output index from application
1242 * lookup the sub device information for a given output index.
1243 * we report all the output to application. output table also
1244 * has sub device name for the each output
1247 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
1248 struct vpif_display_chan_config *chan_cfg, int index)
1250 struct vpif_subdev_info *subdev_info;
1251 const char *subdev_name;
1254 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
1256 if (chan_cfg->outputs == NULL)
1259 subdev_name = chan_cfg->outputs[index].subdev_name;
1260 if (subdev_name == NULL)
1263 /* loop through the sub device list to get the sub device info */
1264 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1265 subdev_info = &vpif_cfg->subdevinfo[i];
1266 if (!strcmp(subdev_info->name, subdev_name))
1273 * vpif_set_output() - Select an output
1274 * @vpif_cfg - global config ptr
1276 * @index - Given output index from application
1278 * Select the given output.
1280 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
1281 struct channel_obj *ch, int index)
1283 struct vpif_display_chan_config *chan_cfg =
1284 &vpif_cfg->chan_config[ch->channel_id];
1285 struct vpif_subdev_info *subdev_info = NULL;
1286 struct v4l2_subdev *sd = NULL;
1287 u32 input = 0, output = 0;
1291 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
1292 if (sd_index >= 0) {
1293 sd = vpif_obj.sd[sd_index];
1294 subdev_info = &vpif_cfg->subdevinfo[sd_index];
1298 input = chan_cfg->outputs[index].input_route;
1299 output = chan_cfg->outputs[index].output_route;
1300 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1301 if (ret < 0 && ret != -ENOIOCTLCMD) {
1302 vpif_err("Failed to set output\n");
1307 ch->output_idx = index;
1309 if (chan_cfg->outputs != NULL)
1310 /* update tvnorms from the sub device output info */
1311 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
1315 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1317 struct vpif_display_config *config = vpif_dev->platform_data;
1318 struct vpif_display_chan_config *chan_cfg;
1319 struct vpif_fh *fh = priv;
1320 struct channel_obj *ch = fh->channel;
1321 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1323 chan_cfg = &config->chan_config[ch->channel_id];
1325 if (i >= chan_cfg->output_count)
1328 if (common->started) {
1329 vpif_err("Streaming in progress\n");
1333 return vpif_set_output(config, ch, i);
1336 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1338 struct vpif_fh *fh = priv;
1339 struct channel_obj *ch = fh->channel;
1341 *i = ch->output_idx;
1346 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1348 struct vpif_fh *fh = priv;
1349 struct channel_obj *ch = fh->channel;
1351 *p = v4l2_prio_max(&ch->prio);
1356 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1358 struct vpif_fh *fh = priv;
1359 struct channel_obj *ch = fh->channel;
1361 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1365 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1367 * @priv: file handle
1368 * @timings: input timings
1371 vpif_enum_dv_timings(struct file *file, void *priv,
1372 struct v4l2_enum_dv_timings *timings)
1374 struct vpif_fh *fh = priv;
1375 struct channel_obj *ch = fh->channel;
1378 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1379 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1385 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1387 * @priv: file handle
1388 * @timings: digital video timings
1390 static int vpif_s_dv_timings(struct file *file, void *priv,
1391 struct v4l2_dv_timings *timings)
1393 struct vpif_fh *fh = priv;
1394 struct channel_obj *ch = fh->channel;
1395 struct vpif_params *vpifparams = &ch->vpifparams;
1396 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1397 struct video_obj *vid_ch = &ch->video;
1398 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1401 if (timings->type != V4L2_DV_BT_656_1120) {
1402 vpif_dbg(2, debug, "Timing type not defined\n");
1406 /* Configure subdevice timings, if any */
1407 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1408 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1411 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1415 if (!(timings->bt.width && timings->bt.height &&
1416 (timings->bt.hbackporch ||
1417 timings->bt.hfrontporch ||
1418 timings->bt.hsync) &&
1419 timings->bt.vfrontporch &&
1420 (timings->bt.vbackporch ||
1421 timings->bt.vsync))) {
1422 vpif_dbg(2, debug, "Timings for width, height, "
1423 "horizontal back porch, horizontal sync, "
1424 "horizontal front porch, vertical back porch, "
1425 "vertical sync and vertical back porch "
1426 "must be defined\n");
1430 vid_ch->dv_timings = *timings;
1432 /* Configure video port timings */
1434 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1435 std_info->sav2eav = bt->width;
1438 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1440 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1441 if (bt->interlaced) {
1442 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1443 std_info->l5 = std_info->vsize/2 -
1444 (bt->vfrontporch - 1);
1445 std_info->l7 = std_info->vsize/2 + 1;
1446 std_info->l9 = std_info->l7 + bt->il_vsync +
1447 bt->il_vbackporch + 1;
1448 std_info->l11 = std_info->vsize -
1449 (bt->il_vfrontporch - 1);
1451 vpif_dbg(2, debug, "Required timing values for "
1452 "interlaced BT format missing\n");
1456 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1458 strncpy(std_info->name, "Custom timings BT656/1120",
1460 std_info->width = bt->width;
1461 std_info->height = bt->height;
1462 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1463 std_info->ycmux_mode = 0;
1464 std_info->capture_format = 0;
1465 std_info->vbi_supported = 0;
1466 std_info->hd_sd = 1;
1467 std_info->stdid = 0;
1474 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1476 * @priv: file handle
1477 * @timings: digital video timings
1479 static int vpif_g_dv_timings(struct file *file, void *priv,
1480 struct v4l2_dv_timings *timings)
1482 struct vpif_fh *fh = priv;
1483 struct channel_obj *ch = fh->channel;
1484 struct video_obj *vid_ch = &ch->video;
1486 *timings = vid_ch->dv_timings;
1492 * vpif_log_status() - Status information
1494 * @priv: file handle
1498 static int vpif_log_status(struct file *filep, void *priv)
1500 /* status for sub devices */
1501 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1506 /* vpif display ioctl operations */
1507 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1508 .vidioc_querycap = vpif_querycap,
1509 .vidioc_g_priority = vpif_g_priority,
1510 .vidioc_s_priority = vpif_s_priority,
1511 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1512 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1513 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1514 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1515 .vidioc_reqbufs = vpif_reqbufs,
1516 .vidioc_querybuf = vpif_querybuf,
1517 .vidioc_qbuf = vpif_qbuf,
1518 .vidioc_dqbuf = vpif_dqbuf,
1519 .vidioc_streamon = vpif_streamon,
1520 .vidioc_streamoff = vpif_streamoff,
1521 .vidioc_s_std = vpif_s_std,
1522 .vidioc_g_std = vpif_g_std,
1523 .vidioc_enum_output = vpif_enum_output,
1524 .vidioc_s_output = vpif_s_output,
1525 .vidioc_g_output = vpif_g_output,
1526 .vidioc_cropcap = vpif_cropcap,
1527 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1528 .vidioc_s_dv_timings = vpif_s_dv_timings,
1529 .vidioc_g_dv_timings = vpif_g_dv_timings,
1530 .vidioc_log_status = vpif_log_status,
1533 static const struct v4l2_file_operations vpif_fops = {
1534 .owner = THIS_MODULE,
1536 .release = vpif_release,
1537 .unlocked_ioctl = video_ioctl2,
1542 static struct video_device vpif_video_template = {
1545 .ioctl_ops = &vpif_ioctl_ops,
1548 /*Configure the channels, buffer sizei, request irq */
1549 static int initialize_vpif(void)
1551 int free_channel_objects_index;
1552 int free_buffer_channel_index;
1553 int free_buffer_index;
1556 /* Default number of buffers should be 3 */
1557 if ((ch2_numbuffers > 0) &&
1558 (ch2_numbuffers < config_params.min_numbuffers))
1559 ch2_numbuffers = config_params.min_numbuffers;
1560 if ((ch3_numbuffers > 0) &&
1561 (ch3_numbuffers < config_params.min_numbuffers))
1562 ch3_numbuffers = config_params.min_numbuffers;
1564 /* Set buffer size to min buffers size if invalid buffer size is
1566 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1568 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1569 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1571 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1573 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1575 if (ch2_numbuffers) {
1576 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1579 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1581 if (ch3_numbuffers) {
1582 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1586 /* Allocate memory for six channel objects */
1587 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1589 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1590 /* If memory allocation fails, return error */
1591 if (!vpif_obj.dev[i]) {
1592 free_channel_objects_index = i;
1594 goto vpif_init_free_channel_objects;
1598 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1599 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1600 free_buffer_index = config_params.numbuffers[i - 1];
1604 vpif_init_free_channel_objects:
1605 for (j = 0; j < free_channel_objects_index; j++)
1606 kfree(vpif_obj.dev[j]);
1610 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1611 struct v4l2_subdev *subdev,
1612 struct v4l2_async_subdev *asd)
1616 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1617 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1619 vpif_obj.sd[i] = subdev;
1620 vpif_obj.sd[i]->grp_id = 1 << i;
1627 static int vpif_probe_complete(void)
1629 struct common_obj *common;
1630 struct channel_obj *ch;
1633 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1634 ch = vpif_obj.dev[j];
1635 /* Initialize field of the channel objects */
1636 atomic_set(&ch->usrs, 0);
1637 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1638 ch->common[k].numbuffers = 0;
1639 common = &ch->common[k];
1640 common->io_usrs = 0;
1641 common->started = 0;
1642 spin_lock_init(&common->irqlock);
1643 mutex_init(&common->lock);
1644 common->numbuffers = 0;
1645 common->set_addr = NULL;
1646 common->ytop_off = 0;
1647 common->ybtm_off = 0;
1648 common->ctop_off = 0;
1649 common->cbtm_off = 0;
1650 common->cur_frm = NULL;
1651 common->next_frm = NULL;
1652 memset(&common->fmt, 0, sizeof(common->fmt));
1653 common->numbuffers = config_params.numbuffers[k];
1655 ch->initialized = 0;
1656 if (vpif_obj.config->subdev_count)
1657 ch->sd = vpif_obj.sd[0];
1660 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1661 config_params.numbuffers[ch->channel_id];
1663 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1665 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1667 /* Initialize prio member of channel object */
1668 v4l2_prio_init(&ch->prio);
1669 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1670 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1671 ch->video_dev->lock = &common->lock;
1672 video_set_drvdata(ch->video_dev, ch);
1674 /* select output 0 */
1675 err = vpif_set_output(vpif_obj.config, ch, 0);
1679 /* register video device */
1680 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1681 (int)ch, (int)&ch->video_dev);
1683 err = video_register_device(ch->video_dev,
1684 VFL_TYPE_GRABBER, (j ? 3 : 2));
1692 for (k = 0; k < j; k++) {
1693 ch = vpif_obj.dev[k];
1694 video_unregister_device(ch->video_dev);
1695 video_device_release(ch->video_dev);
1696 ch->video_dev = NULL;
1701 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1703 return vpif_probe_complete();
1707 * vpif_probe: This function creates device entries by register itself to the
1708 * V4L2 driver and initializes fields of each channel objects
1710 static __init int vpif_probe(struct platform_device *pdev)
1712 struct vpif_subdev_info *subdevdata;
1713 int i, j = 0, err = 0;
1715 struct i2c_adapter *i2c_adap;
1716 struct channel_obj *ch;
1717 struct video_device *vfd;
1718 struct resource *res;
1722 vpif_dev = &pdev->dev;
1723 err = initialize_vpif();
1726 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1730 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1732 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1736 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1737 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1738 IRQF_SHARED, "VPIF_Display",
1739 (void *)(&vpif_obj.dev[res_idx]->
1743 vpif_err("VPIF IRQ request failed\n");
1744 goto vpif_unregister;
1749 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1750 /* Get the pointer to the channel object */
1751 ch = vpif_obj.dev[i];
1753 /* Allocate memory for video device */
1754 vfd = video_device_alloc();
1756 for (j = 0; j < i; j++) {
1757 ch = vpif_obj.dev[j];
1758 video_device_release(ch->video_dev);
1761 goto vpif_unregister;
1764 /* Initialize field of video device */
1765 *vfd = vpif_video_template;
1766 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1767 vfd->release = video_device_release;
1768 vfd->vfl_dir = VFL_DIR_TX;
1769 snprintf(vfd->name, sizeof(vfd->name),
1770 "VPIF_Display_DRIVER_V%s",
1771 VPIF_DISPLAY_VERSION);
1773 /* Set video_dev to the video device */
1774 ch->video_dev = vfd;
1777 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1779 size = resource_size(res);
1780 /* The resources are divided into two equal memory and when
1781 * we have HD output we can add them together
1783 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1784 ch = vpif_obj.dev[j];
1787 /* only enabled if second resource exists */
1788 config_params.video_limit[ch->channel_id] = 0;
1790 config_params.video_limit[ch->channel_id] =
1794 vpif_obj.config = pdev->dev.platform_data;
1795 subdev_count = vpif_obj.config->subdev_count;
1796 subdevdata = vpif_obj.config->subdevinfo;
1797 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1799 if (vpif_obj.sd == NULL) {
1800 vpif_err("unable to allocate memory for subdevice pointers\n");
1805 if (!vpif_obj.config->asd_sizes) {
1806 i2c_adap = i2c_get_adapter(1);
1807 for (i = 0; i < subdev_count; i++) {
1809 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1814 if (!vpif_obj.sd[i]) {
1815 vpif_err("Error registering v4l2 subdevice\n");
1817 goto probe_subdev_out;
1821 vpif_obj.sd[i]->grp_id = 1 << i;
1823 vpif_probe_complete();
1825 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1826 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1827 vpif_obj.notifier.bound = vpif_async_bound;
1828 vpif_obj.notifier.complete = vpif_async_complete;
1829 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1830 &vpif_obj.notifier);
1832 vpif_err("Error registering async notifier\n");
1834 goto probe_subdev_out;
1843 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1844 ch = vpif_obj.dev[i];
1845 /* Note: does nothing if ch->video_dev == NULL */
1846 video_device_release(ch->video_dev);
1849 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1855 * vpif_remove: It un-register channels from V4L2 driver
1857 static int vpif_remove(struct platform_device *device)
1859 struct channel_obj *ch;
1862 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1865 /* un-register device */
1866 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1867 /* Get the pointer to the channel object */
1868 ch = vpif_obj.dev[i];
1869 /* Unregister video device */
1870 video_unregister_device(ch->video_dev);
1872 ch->video_dev = NULL;
1873 kfree(vpif_obj.dev[i]);
1880 static int vpif_suspend(struct device *dev)
1882 struct common_obj *common;
1883 struct channel_obj *ch;
1886 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1887 /* Get the pointer to the channel object */
1888 ch = vpif_obj.dev[i];
1889 common = &ch->common[VPIF_VIDEO_INDEX];
1890 mutex_lock(&common->lock);
1891 if (atomic_read(&ch->usrs) && common->io_usrs) {
1892 /* Disable channel */
1893 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1895 channel2_intr_enable(0);
1897 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1898 common->started == 2) {
1900 channel3_intr_enable(0);
1903 mutex_unlock(&common->lock);
1909 static int vpif_resume(struct device *dev)
1912 struct common_obj *common;
1913 struct channel_obj *ch;
1916 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1917 /* Get the pointer to the channel object */
1918 ch = vpif_obj.dev[i];
1919 common = &ch->common[VPIF_VIDEO_INDEX];
1920 mutex_lock(&common->lock);
1921 if (atomic_read(&ch->usrs) && common->io_usrs) {
1922 /* Enable channel */
1923 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1925 channel2_intr_enable(1);
1927 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1928 common->started == 2) {
1930 channel3_intr_enable(1);
1933 mutex_unlock(&common->lock);
1939 static const struct dev_pm_ops vpif_pm = {
1940 .suspend = vpif_suspend,
1941 .resume = vpif_resume,
1944 #define vpif_pm_ops (&vpif_pm)
1946 #define vpif_pm_ops NULL
1949 static __refdata struct platform_driver vpif_driver = {
1951 .name = "vpif_display",
1952 .owner = THIS_MODULE,
1955 .probe = vpif_probe,
1956 .remove = vpif_remove,
1959 module_platform_driver(vpif_driver);