initial commit with v3.6.7
[linux-3.6.7-moxart.git] / drivers / media / video / davinci / vpif_display.c
blobe129c98921ad493b0b8ea90562d2878325218fc5
1 /*
2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
11 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/fs.h>
22 #include <linux/mm.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/string.h>
26 #include <linux/videodev2.h>
27 #include <linux/wait.h>
28 #include <linux/time.h>
29 #include <linux/i2c.h>
30 #include <linux/platform_device.h>
31 #include <linux/io.h>
32 #include <linux/slab.h>
34 #include <asm/irq.h>
35 #include <asm/page.h>
37 #include <media/adv7343.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-chip-ident.h>
42 #include "vpif_display.h"
43 #include "vpif.h"
45 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(VPIF_DISPLAY_VERSION);
49 #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
51 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
52 #define vpif_dbg(level, debug, fmt, arg...) \
53 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
55 static int debug = 1;
56 static u32 ch2_numbuffers = 3;
57 static u32 ch3_numbuffers = 3;
58 static u32 ch2_bufsize = 1920 * 1080 * 2;
59 static u32 ch3_bufsize = 720 * 576 * 2;
61 module_param(debug, int, 0644);
62 module_param(ch2_numbuffers, uint, S_IRUGO);
63 module_param(ch3_numbuffers, uint, S_IRUGO);
64 module_param(ch2_bufsize, uint, S_IRUGO);
65 module_param(ch3_bufsize, uint, S_IRUGO);
67 MODULE_PARM_DESC(debug, "Debug level 0-1");
68 MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
69 MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
70 MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
71 MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
73 static struct vpif_config_params config_params = {
74 .min_numbuffers = 3,
75 .numbuffers[0] = 3,
76 .numbuffers[1] = 3,
77 .min_bufsize[0] = 720 * 480 * 2,
78 .min_bufsize[1] = 720 * 480 * 2,
79 .channel_bufsize[0] = 1920 * 1080 * 2,
80 .channel_bufsize[1] = 720 * 576 * 2,
83 static struct vpif_device vpif_obj = { {NULL} };
84 static struct device *vpif_dev;
85 static void vpif_calculate_offsets(struct channel_obj *ch);
86 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
89 * buffer_prepare: This is the callback function called from vb2_qbuf()
90 * function the buffer is prepared and user space virtual address is converted
91 * into physical address
93 static int vpif_buffer_prepare(struct vb2_buffer *vb)
95 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
96 struct vb2_queue *q = vb->vb2_queue;
97 struct common_obj *common;
98 unsigned long addr;
100 common = &fh->channel->common[VPIF_VIDEO_INDEX];
101 if (vb->state != VB2_BUF_STATE_ACTIVE &&
102 vb->state != VB2_BUF_STATE_PREPARED) {
103 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
104 if (vb2_plane_vaddr(vb, 0) &&
105 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
106 goto buf_align_exit;
108 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
109 if (q->streaming &&
110 (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
111 if (!ISALIGNED(addr + common->ytop_off) ||
112 !ISALIGNED(addr + common->ybtm_off) ||
113 !ISALIGNED(addr + common->ctop_off) ||
114 !ISALIGNED(addr + common->cbtm_off))
115 goto buf_align_exit;
118 return 0;
120 buf_align_exit:
121 vpif_err("buffer offset not aligned to 8 bytes\n");
122 return -EINVAL;
126 * vpif_buffer_queue_setup: This function allocates memory for the buffers
128 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
129 const struct v4l2_format *fmt,
130 unsigned int *nbuffers, unsigned int *nplanes,
131 unsigned int sizes[], void *alloc_ctxs[])
133 struct vpif_fh *fh = vb2_get_drv_priv(vq);
134 struct channel_obj *ch = fh->channel;
135 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
136 unsigned long size;
138 if (V4L2_MEMORY_MMAP == common->memory) {
139 size = config_params.channel_bufsize[ch->channel_id];
141 * Checking if the buffer size exceeds the available buffer
142 * ycmux_mode = 0 means 1 channel mode HD and
143 * ycmux_mode = 1 means 2 channels mode SD
145 if (ch->vpifparams.std_info.ycmux_mode == 0) {
146 if (config_params.video_limit[ch->channel_id])
147 while (size * *nbuffers >
148 (config_params.video_limit[0]
149 + config_params.video_limit[1]))
150 (*nbuffers)--;
151 } else {
152 if (config_params.video_limit[ch->channel_id])
153 while (size * *nbuffers >
154 config_params.video_limit[ch->channel_id])
155 (*nbuffers)--;
157 } else {
158 size = common->fmt.fmt.pix.sizeimage;
161 if (*nbuffers < config_params.min_numbuffers)
162 *nbuffers = config_params.min_numbuffers;
164 *nplanes = 1;
165 sizes[0] = size;
166 alloc_ctxs[0] = common->alloc_ctx;
167 return 0;
171 * vpif_buffer_queue: This function adds the buffer to DMA queue
173 static void vpif_buffer_queue(struct vb2_buffer *vb)
175 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
176 struct vpif_disp_buffer *buf = container_of(vb,
177 struct vpif_disp_buffer, vb);
178 struct channel_obj *ch = fh->channel;
179 struct common_obj *common;
181 common = &ch->common[VPIF_VIDEO_INDEX];
183 /* add the buffer to the DMA queue */
184 list_add_tail(&buf->list, &common->dma_queue);
188 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
189 * free memory allocated to the buffers
191 static void vpif_buf_cleanup(struct vb2_buffer *vb)
193 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
194 struct vpif_disp_buffer *buf = container_of(vb,
195 struct vpif_disp_buffer, vb);
196 struct channel_obj *ch = fh->channel;
197 struct common_obj *common;
198 unsigned long flags;
200 common = &ch->common[VPIF_VIDEO_INDEX];
202 spin_lock_irqsave(&common->irqlock, flags);
203 if (vb->state == VB2_BUF_STATE_ACTIVE)
204 list_del_init(&buf->list);
205 spin_unlock_irqrestore(&common->irqlock, flags);
208 static void vpif_wait_prepare(struct vb2_queue *vq)
210 struct vpif_fh *fh = vb2_get_drv_priv(vq);
211 struct channel_obj *ch = fh->channel;
212 struct common_obj *common;
214 common = &ch->common[VPIF_VIDEO_INDEX];
215 mutex_unlock(&common->lock);
218 static void vpif_wait_finish(struct vb2_queue *vq)
220 struct vpif_fh *fh = vb2_get_drv_priv(vq);
221 struct channel_obj *ch = fh->channel;
222 struct common_obj *common;
224 common = &ch->common[VPIF_VIDEO_INDEX];
225 mutex_lock(&common->lock);
228 static int vpif_buffer_init(struct vb2_buffer *vb)
230 struct vpif_disp_buffer *buf = container_of(vb,
231 struct vpif_disp_buffer, vb);
233 INIT_LIST_HEAD(&buf->list);
235 return 0;
238 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
240 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
242 struct vpif_display_config *vpif_config_data =
243 vpif_dev->platform_data;
244 struct vpif_fh *fh = vb2_get_drv_priv(vq);
245 struct channel_obj *ch = fh->channel;
246 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
247 struct vpif_params *vpif = &ch->vpifparams;
248 unsigned long addr = 0;
249 int ret;
251 /* If buffer queue is empty, return error */
252 if (list_empty(&common->dma_queue)) {
253 vpif_err("buffer queue is empty\n");
254 return -EIO;
257 /* Get the next frame from the buffer queue */
258 common->next_frm = common->cur_frm =
259 list_entry(common->dma_queue.next,
260 struct vpif_disp_buffer, list);
262 list_del(&common->cur_frm->list);
263 /* Mark state of the current frame to active */
264 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
266 /* Initialize field_id and started member */
267 ch->field_id = 0;
268 common->started = 1;
269 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
270 /* Calculate the offset for Y and C data in the buffer */
271 vpif_calculate_offsets(ch);
273 if ((ch->vpifparams.std_info.frm_fmt &&
274 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
275 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
276 || (!ch->vpifparams.std_info.frm_fmt
277 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
278 vpif_err("conflict in field format and std format\n");
279 return -EINVAL;
282 /* clock settings */
283 ret =
284 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
285 ch->vpifparams.std_info.hd_sd);
286 if (ret < 0) {
287 vpif_err("can't set clock\n");
288 return ret;
291 /* set the parameters and addresses */
292 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
293 if (ret < 0)
294 return ret;
296 common->started = ret;
297 vpif_config_addr(ch, ret);
298 common->set_addr((addr + common->ytop_off),
299 (addr + common->ybtm_off),
300 (addr + common->ctop_off),
301 (addr + common->cbtm_off));
303 /* Set interrupt for both the fields in VPIF
304 Register enable channel in VPIF register */
305 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
306 channel2_intr_assert();
307 channel2_intr_enable(1);
308 enable_channel2(1);
309 if (vpif_config_data->ch2_clip_en)
310 channel2_clipping_enable(1);
313 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
314 || (common->started == 2)) {
315 channel3_intr_assert();
316 channel3_intr_enable(1);
317 enable_channel3(1);
318 if (vpif_config_data->ch3_clip_en)
319 channel3_clipping_enable(1);
321 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
323 return 0;
326 /* abort streaming and wait for last buffer */
327 static int vpif_stop_streaming(struct vb2_queue *vq)
329 struct vpif_fh *fh = vb2_get_drv_priv(vq);
330 struct channel_obj *ch = fh->channel;
331 struct common_obj *common;
333 if (!vb2_is_streaming(vq))
334 return 0;
336 common = &ch->common[VPIF_VIDEO_INDEX];
338 /* release all active buffers */
339 while (!list_empty(&common->dma_queue)) {
340 common->next_frm = list_entry(common->dma_queue.next,
341 struct vpif_disp_buffer, list);
342 list_del(&common->next_frm->list);
343 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
346 return 0;
349 static struct vb2_ops video_qops = {
350 .queue_setup = vpif_buffer_queue_setup,
351 .wait_prepare = vpif_wait_prepare,
352 .wait_finish = vpif_wait_finish,
353 .buf_init = vpif_buffer_init,
354 .buf_prepare = vpif_buffer_prepare,
355 .start_streaming = vpif_start_streaming,
356 .stop_streaming = vpif_stop_streaming,
357 .buf_cleanup = vpif_buf_cleanup,
358 .buf_queue = vpif_buffer_queue,
361 static void process_progressive_mode(struct common_obj *common)
363 unsigned long addr = 0;
365 /* Get the next buffer from buffer queue */
366 common->next_frm = list_entry(common->dma_queue.next,
367 struct vpif_disp_buffer, list);
368 /* Remove that buffer from the buffer queue */
369 list_del(&common->next_frm->list);
370 /* Mark status of the buffer as active */
371 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
373 /* Set top and bottom field addrs in VPIF registers */
374 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
375 common->set_addr(addr + common->ytop_off,
376 addr + common->ybtm_off,
377 addr + common->ctop_off,
378 addr + common->cbtm_off);
381 static void process_interlaced_mode(int fid, struct common_obj *common)
383 /* device field id and local field id are in sync */
384 /* If this is even field */
385 if (0 == fid) {
386 if (common->cur_frm == common->next_frm)
387 return;
389 /* one frame is displayed If next frame is
390 * available, release cur_frm and move on */
391 /* Copy frame display time */
392 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
393 /* Change status of the cur_frm */
394 vb2_buffer_done(&common->cur_frm->vb,
395 VB2_BUF_STATE_DONE);
396 /* Make cur_frm pointing to next_frm */
397 common->cur_frm = common->next_frm;
399 } else if (1 == fid) { /* odd field */
400 if (list_empty(&common->dma_queue)
401 || (common->cur_frm != common->next_frm)) {
402 return;
404 /* one field is displayed configure the next
405 * frame if it is available else hold on current
406 * frame */
407 /* Get next from the buffer queue */
408 process_progressive_mode(common);
414 * vpif_channel_isr: It changes status of the displayed buffer, takes next
415 * buffer from the queue and sets its address in VPIF registers
417 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
419 struct vpif_device *dev = &vpif_obj;
420 struct channel_obj *ch;
421 struct common_obj *common;
422 enum v4l2_field field;
423 int fid = -1, i;
424 int channel_id = 0;
426 channel_id = *(int *)(dev_id);
427 if (!vpif_intr_status(channel_id + 2))
428 return IRQ_NONE;
430 ch = dev->dev[channel_id];
431 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
432 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
433 common = &ch->common[i];
434 /* If streaming is started in this channel */
435 if (0 == common->started)
436 continue;
438 if (1 == ch->vpifparams.std_info.frm_fmt) {
439 if (list_empty(&common->dma_queue))
440 continue;
442 /* Progressive mode */
443 if (!channel_first_int[i][channel_id]) {
444 /* Mark status of the cur_frm to
445 * done and unlock semaphore on it */
446 do_gettimeofday(&common->cur_frm->vb.
447 v4l2_buf.timestamp);
448 vb2_buffer_done(&common->cur_frm->vb,
449 VB2_BUF_STATE_DONE);
450 /* Make cur_frm pointing to next_frm */
451 common->cur_frm = common->next_frm;
454 channel_first_int[i][channel_id] = 0;
455 process_progressive_mode(common);
456 } else {
457 /* Interlaced mode */
458 /* If it is first interrupt, ignore it */
460 if (channel_first_int[i][channel_id]) {
461 channel_first_int[i][channel_id] = 0;
462 continue;
465 if (0 == i) {
466 ch->field_id ^= 1;
467 /* Get field id from VPIF registers */
468 fid = vpif_channel_getfid(ch->channel_id + 2);
469 /* If fid does not match with stored field id */
470 if (fid != ch->field_id) {
471 /* Make them in sync */
472 if (0 == fid)
473 ch->field_id = fid;
475 return IRQ_HANDLED;
478 process_interlaced_mode(fid, common);
482 return IRQ_HANDLED;
485 static int vpif_update_std_info(struct channel_obj *ch)
487 struct video_obj *vid_ch = &ch->video;
488 struct vpif_params *vpifparams = &ch->vpifparams;
489 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
490 const struct vpif_channel_config_params *config;
492 int i;
494 for (i = 0; i < vpif_ch_params_count; i++) {
495 config = &ch_params[i];
496 if (config->hd_sd == 0) {
497 vpif_dbg(2, debug, "SD format\n");
498 if (config->stdid & vid_ch->stdid) {
499 memcpy(std_info, config, sizeof(*config));
500 break;
502 } else {
503 vpif_dbg(2, debug, "HD format\n");
504 if (config->dv_preset == vid_ch->dv_preset) {
505 memcpy(std_info, config, sizeof(*config));
506 break;
511 if (i == vpif_ch_params_count) {
512 vpif_dbg(1, debug, "Format not found\n");
513 return -EINVAL;
516 return 0;
519 static int vpif_update_resolution(struct channel_obj *ch)
521 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
522 struct video_obj *vid_ch = &ch->video;
523 struct vpif_params *vpifparams = &ch->vpifparams;
524 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
526 if (!vid_ch->stdid && !vid_ch->dv_preset && !vid_ch->bt_timings.height)
527 return -EINVAL;
529 if (vid_ch->stdid || vid_ch->dv_preset) {
530 if (vpif_update_std_info(ch))
531 return -EINVAL;
534 common->fmt.fmt.pix.width = std_info->width;
535 common->fmt.fmt.pix.height = std_info->height;
536 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
537 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
539 /* Set height and width paramateres */
540 common->height = std_info->height;
541 common->width = std_info->width;
543 return 0;
547 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
548 * in the top and bottom field
550 static void vpif_calculate_offsets(struct channel_obj *ch)
552 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
553 struct vpif_params *vpifparams = &ch->vpifparams;
554 enum v4l2_field field = common->fmt.fmt.pix.field;
555 struct video_obj *vid_ch = &ch->video;
556 unsigned int hpitch, vpitch, sizeimage;
558 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
559 if (ch->vpifparams.std_info.frm_fmt)
560 vid_ch->buf_field = V4L2_FIELD_NONE;
561 else
562 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
563 } else {
564 vid_ch->buf_field = common->fmt.fmt.pix.field;
567 sizeimage = common->fmt.fmt.pix.sizeimage;
569 hpitch = common->fmt.fmt.pix.bytesperline;
570 vpitch = sizeimage / (hpitch * 2);
571 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
572 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
573 common->ytop_off = 0;
574 common->ybtm_off = hpitch;
575 common->ctop_off = sizeimage / 2;
576 common->cbtm_off = sizeimage / 2 + hpitch;
577 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
578 common->ytop_off = 0;
579 common->ybtm_off = sizeimage / 4;
580 common->ctop_off = sizeimage / 2;
581 common->cbtm_off = common->ctop_off + sizeimage / 4;
582 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
583 common->ybtm_off = 0;
584 common->ytop_off = sizeimage / 4;
585 common->cbtm_off = sizeimage / 2;
586 common->ctop_off = common->cbtm_off + sizeimage / 4;
589 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
590 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
591 vpifparams->video_params.storage_mode = 1;
592 } else {
593 vpifparams->video_params.storage_mode = 0;
596 if (ch->vpifparams.std_info.frm_fmt == 1) {
597 vpifparams->video_params.hpitch =
598 common->fmt.fmt.pix.bytesperline;
599 } else {
600 if ((field == V4L2_FIELD_ANY) ||
601 (field == V4L2_FIELD_INTERLACED))
602 vpifparams->video_params.hpitch =
603 common->fmt.fmt.pix.bytesperline * 2;
604 else
605 vpifparams->video_params.hpitch =
606 common->fmt.fmt.pix.bytesperline;
609 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
612 static void vpif_config_format(struct channel_obj *ch)
614 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
616 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
617 if (config_params.numbuffers[ch->channel_id] == 0)
618 common->memory = V4L2_MEMORY_USERPTR;
619 else
620 common->memory = V4L2_MEMORY_MMAP;
622 common->fmt.fmt.pix.sizeimage =
623 config_params.channel_bufsize[ch->channel_id];
624 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
625 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
628 static int vpif_check_format(struct channel_obj *ch,
629 struct v4l2_pix_format *pixfmt)
631 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
632 enum v4l2_field field = pixfmt->field;
633 u32 sizeimage, hpitch, vpitch;
635 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
636 goto invalid_fmt_exit;
638 if (!(VPIF_VALID_FIELD(field)))
639 goto invalid_fmt_exit;
641 if (pixfmt->bytesperline <= 0)
642 goto invalid_pitch_exit;
644 sizeimage = pixfmt->sizeimage;
646 if (vpif_update_resolution(ch))
647 return -EINVAL;
649 hpitch = pixfmt->bytesperline;
650 vpitch = sizeimage / (hpitch * 2);
652 /* Check for valid value of pitch */
653 if ((hpitch < ch->vpifparams.std_info.width) ||
654 (vpitch < ch->vpifparams.std_info.height))
655 goto invalid_pitch_exit;
657 /* Check for 8 byte alignment */
658 if (!ISALIGNED(hpitch)) {
659 vpif_err("invalid pitch alignment\n");
660 return -EINVAL;
662 pixfmt->width = common->fmt.fmt.pix.width;
663 pixfmt->height = common->fmt.fmt.pix.height;
665 return 0;
667 invalid_fmt_exit:
668 vpif_err("invalid field format\n");
669 return -EINVAL;
671 invalid_pitch_exit:
672 vpif_err("invalid pitch\n");
673 return -EINVAL;
676 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
678 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
680 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
681 common->set_addr = ch3_set_videobuf_addr;
682 } else {
683 if (2 == muxmode)
684 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
685 else
686 common->set_addr = ch2_set_videobuf_addr;
691 * vpif_mmap: It is used to map kernel space buffers into user spaces
693 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
695 struct vpif_fh *fh = filep->private_data;
696 struct channel_obj *ch = fh->channel;
697 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
699 vpif_dbg(2, debug, "vpif_mmap\n");
701 return vb2_mmap(&common->buffer_queue, vma);
705 * vpif_poll: It is used for select/poll system call
707 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
709 struct vpif_fh *fh = filep->private_data;
710 struct channel_obj *ch = fh->channel;
711 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
713 if (common->started)
714 return vb2_poll(&common->buffer_queue, filep, wait);
716 return 0;
720 * vpif_open: It creates object of file handle structure and stores it in
721 * private_data member of filepointer
723 static int vpif_open(struct file *filep)
725 struct video_device *vdev = video_devdata(filep);
726 struct channel_obj *ch = NULL;
727 struct vpif_fh *fh = NULL;
729 ch = video_get_drvdata(vdev);
730 /* Allocate memory for the file handle object */
731 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
732 if (fh == NULL) {
733 vpif_err("unable to allocate memory for file handle object\n");
734 return -ENOMEM;
737 /* store pointer to fh in private_data member of filep */
738 filep->private_data = fh;
739 fh->channel = ch;
740 fh->initialized = 0;
741 if (!ch->initialized) {
742 fh->initialized = 1;
743 ch->initialized = 1;
744 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
747 /* Increment channel usrs counter */
748 atomic_inc(&ch->usrs);
749 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
750 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
751 /* Initialize priority of this instance to default priority */
752 fh->prio = V4L2_PRIORITY_UNSET;
753 v4l2_prio_open(&ch->prio, &fh->prio);
755 return 0;
759 * vpif_release: This function deletes buffer queue, frees the buffers and
760 * the vpif file handle
762 static int vpif_release(struct file *filep)
764 struct vpif_fh *fh = filep->private_data;
765 struct channel_obj *ch = fh->channel;
766 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
768 /* if this instance is doing IO */
769 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
770 /* Reset io_usrs member of channel object */
771 common->io_usrs = 0;
772 /* Disable channel */
773 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
774 enable_channel2(0);
775 channel2_intr_enable(0);
777 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
778 (2 == common->started)) {
779 enable_channel3(0);
780 channel3_intr_enable(0);
782 common->started = 0;
784 /* Free buffers allocated */
785 vb2_queue_release(&common->buffer_queue);
786 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
788 common->numbuffers =
789 config_params.numbuffers[ch->channel_id];
792 /* Decrement channel usrs counter */
793 atomic_dec(&ch->usrs);
794 /* If this file handle has initialize encoder device, reset it */
795 if (fh->initialized)
796 ch->initialized = 0;
798 /* Close the priority */
799 v4l2_prio_close(&ch->prio, fh->prio);
800 filep->private_data = NULL;
801 fh->initialized = 0;
802 kfree(fh);
804 return 0;
807 /* functions implementing ioctls */
809 * vpif_querycap() - QUERYCAP handler
810 * @file: file ptr
811 * @priv: file handle
812 * @cap: ptr to v4l2_capability structure
814 static int vpif_querycap(struct file *file, void *priv,
815 struct v4l2_capability *cap)
817 struct vpif_display_config *config = vpif_dev->platform_data;
819 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
820 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
821 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
822 strlcpy(cap->card, config->card_name, sizeof(cap->card));
824 return 0;
827 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
828 struct v4l2_fmtdesc *fmt)
830 if (fmt->index != 0) {
831 vpif_err("Invalid format index\n");
832 return -EINVAL;
835 /* Fill in the information about format */
836 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
837 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
838 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
840 return 0;
843 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
844 struct v4l2_format *fmt)
846 struct vpif_fh *fh = priv;
847 struct channel_obj *ch = fh->channel;
848 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
850 /* Check the validity of the buffer type */
851 if (common->fmt.type != fmt->type)
852 return -EINVAL;
854 if (vpif_update_resolution(ch))
855 return -EINVAL;
856 *fmt = common->fmt;
857 return 0;
860 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
861 struct v4l2_format *fmt)
863 struct vpif_fh *fh = priv;
864 struct v4l2_pix_format *pixfmt;
865 struct channel_obj *ch = fh->channel;
866 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
867 int ret = 0;
869 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
870 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
871 if (!fh->initialized) {
872 vpif_dbg(1, debug, "Channel Busy\n");
873 return -EBUSY;
876 /* Check for the priority */
877 ret = v4l2_prio_check(&ch->prio, fh->prio);
878 if (0 != ret)
879 return ret;
880 fh->initialized = 1;
883 if (common->started) {
884 vpif_dbg(1, debug, "Streaming in progress\n");
885 return -EBUSY;
888 pixfmt = &fmt->fmt.pix;
889 /* Check for valid field format */
890 ret = vpif_check_format(ch, pixfmt);
891 if (ret)
892 return ret;
894 /* store the pix format in the channel object */
895 common->fmt.fmt.pix = *pixfmt;
896 /* store the format in the channel object */
897 common->fmt = *fmt;
898 return 0;
901 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
902 struct v4l2_format *fmt)
904 struct vpif_fh *fh = priv;
905 struct channel_obj *ch = fh->channel;
906 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
907 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
908 int ret = 0;
910 ret = vpif_check_format(ch, pixfmt);
911 if (ret) {
912 *pixfmt = common->fmt.fmt.pix;
913 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
916 return ret;
919 static int vpif_reqbufs(struct file *file, void *priv,
920 struct v4l2_requestbuffers *reqbuf)
922 struct vpif_fh *fh = priv;
923 struct channel_obj *ch = fh->channel;
924 struct common_obj *common;
925 enum v4l2_field field;
926 struct vb2_queue *q;
927 u8 index = 0;
929 /* This file handle has not initialized the channel,
930 It is not allowed to do settings */
931 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
932 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
933 if (!fh->initialized) {
934 vpif_err("Channel Busy\n");
935 return -EBUSY;
939 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
940 return -EINVAL;
942 index = VPIF_VIDEO_INDEX;
944 common = &ch->common[index];
946 if (common->fmt.type != reqbuf->type || !vpif_dev)
947 return -EINVAL;
948 if (0 != common->io_usrs)
949 return -EBUSY;
951 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
952 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
953 field = V4L2_FIELD_INTERLACED;
954 else
955 field = common->fmt.fmt.pix.field;
956 } else {
957 field = V4L2_VBI_INTERLACED;
959 /* Initialize videobuf2 queue as per the buffer type */
960 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
961 if (!common->alloc_ctx) {
962 vpif_err("Failed to get the context\n");
963 return -EINVAL;
965 q = &common->buffer_queue;
966 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
967 q->io_modes = VB2_MMAP | VB2_USERPTR;
968 q->drv_priv = fh;
969 q->ops = &video_qops;
970 q->mem_ops = &vb2_dma_contig_memops;
971 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
973 vb2_queue_init(q);
975 /* Set io allowed member of file handle to TRUE */
976 fh->io_allowed[index] = 1;
977 /* Increment io usrs member of channel object to 1 */
978 common->io_usrs = 1;
979 /* Store type of memory requested in channel object */
980 common->memory = reqbuf->memory;
981 INIT_LIST_HEAD(&common->dma_queue);
982 /* Allocate buffers */
983 return vb2_reqbufs(&common->buffer_queue, reqbuf);
986 static int vpif_querybuf(struct file *file, void *priv,
987 struct v4l2_buffer *tbuf)
989 struct vpif_fh *fh = priv;
990 struct channel_obj *ch = fh->channel;
991 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
993 if (common->fmt.type != tbuf->type)
994 return -EINVAL;
996 return vb2_querybuf(&common->buffer_queue, tbuf);
999 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1001 struct vpif_fh *fh = NULL;
1002 struct channel_obj *ch = NULL;
1003 struct common_obj *common = NULL;
1005 if (!buf || !priv)
1006 return -EINVAL;
1008 fh = priv;
1009 ch = fh->channel;
1010 if (!ch)
1011 return -EINVAL;
1013 common = &(ch->common[VPIF_VIDEO_INDEX]);
1014 if (common->fmt.type != buf->type)
1015 return -EINVAL;
1017 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1018 vpif_err("fh->io_allowed\n");
1019 return -EACCES;
1022 return vb2_qbuf(&common->buffer_queue, buf);
1025 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1027 struct vpif_fh *fh = priv;
1028 struct channel_obj *ch = fh->channel;
1029 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1030 int ret = 0;
1032 if (!(*std_id & VPIF_V4L2_STD))
1033 return -EINVAL;
1035 if (common->started) {
1036 vpif_err("streaming in progress\n");
1037 return -EBUSY;
1040 /* Call encoder subdevice function to set the standard */
1041 ch->video.stdid = *std_id;
1042 ch->video.dv_preset = V4L2_DV_INVALID;
1043 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1045 /* Get the information about the standard */
1046 if (vpif_update_resolution(ch))
1047 return -EINVAL;
1049 if ((ch->vpifparams.std_info.width *
1050 ch->vpifparams.std_info.height * 2) >
1051 config_params.channel_bufsize[ch->channel_id]) {
1052 vpif_err("invalid std for this size\n");
1053 return -EINVAL;
1056 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1057 /* Configure the default format information */
1058 vpif_config_format(ch);
1060 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1061 s_std_output, *std_id);
1062 if (ret < 0) {
1063 vpif_err("Failed to set output standard\n");
1064 return ret;
1067 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1068 s_std, *std_id);
1069 if (ret < 0)
1070 vpif_err("Failed to set standard for sub devices\n");
1071 return ret;
1074 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1076 struct vpif_fh *fh = priv;
1077 struct channel_obj *ch = fh->channel;
1079 *std = ch->video.stdid;
1080 return 0;
1083 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1085 struct vpif_fh *fh = priv;
1086 struct channel_obj *ch = fh->channel;
1087 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1089 return vb2_dqbuf(&common->buffer_queue, p,
1090 (file->f_flags & O_NONBLOCK));
1093 static int vpif_streamon(struct file *file, void *priv,
1094 enum v4l2_buf_type buftype)
1096 struct vpif_fh *fh = priv;
1097 struct channel_obj *ch = fh->channel;
1098 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1099 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1100 int ret = 0;
1102 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1103 vpif_err("buffer type not supported\n");
1104 return -EINVAL;
1107 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1108 vpif_err("fh->io_allowed\n");
1109 return -EACCES;
1112 /* If Streaming is already started, return error */
1113 if (common->started) {
1114 vpif_err("channel->started\n");
1115 return -EBUSY;
1118 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1119 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1120 ch->vpifparams.std_info.ycmux_mode == 0)
1121 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1122 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1123 vpif_err("other channel is using\n");
1124 return -EBUSY;
1127 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1128 if (ret < 0)
1129 return ret;
1131 /* Call vb2_streamon to start streaming in videobuf2 */
1132 ret = vb2_streamon(&common->buffer_queue, buftype);
1133 if (ret < 0) {
1134 vpif_err("vb2_streamon\n");
1135 return ret;
1138 return ret;
1141 static int vpif_streamoff(struct file *file, void *priv,
1142 enum v4l2_buf_type buftype)
1144 struct vpif_fh *fh = priv;
1145 struct channel_obj *ch = fh->channel;
1146 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1147 struct vpif_display_config *vpif_config_data =
1148 vpif_dev->platform_data;
1150 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1151 vpif_err("buffer type not supported\n");
1152 return -EINVAL;
1155 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1156 vpif_err("fh->io_allowed\n");
1157 return -EACCES;
1160 if (!common->started) {
1161 vpif_err("channel->started\n");
1162 return -EINVAL;
1165 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1166 /* disable channel */
1167 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1168 if (vpif_config_data->ch2_clip_en)
1169 channel2_clipping_enable(0);
1170 enable_channel2(0);
1171 channel2_intr_enable(0);
1173 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1174 (2 == common->started)) {
1175 if (vpif_config_data->ch3_clip_en)
1176 channel3_clipping_enable(0);
1177 enable_channel3(0);
1178 channel3_intr_enable(0);
1182 common->started = 0;
1183 return vb2_streamoff(&common->buffer_queue, buftype);
1186 static int vpif_cropcap(struct file *file, void *priv,
1187 struct v4l2_cropcap *crop)
1189 struct vpif_fh *fh = priv;
1190 struct channel_obj *ch = fh->channel;
1191 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1192 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1193 return -EINVAL;
1195 crop->bounds.left = crop->bounds.top = 0;
1196 crop->defrect.left = crop->defrect.top = 0;
1197 crop->defrect.height = crop->bounds.height = common->height;
1198 crop->defrect.width = crop->bounds.width = common->width;
1200 return 0;
1203 static int vpif_enum_output(struct file *file, void *fh,
1204 struct v4l2_output *output)
1207 struct vpif_display_config *config = vpif_dev->platform_data;
1209 if (output->index >= config->output_count) {
1210 vpif_dbg(1, debug, "Invalid output index\n");
1211 return -EINVAL;
1214 strcpy(output->name, config->output[output->index]);
1215 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1216 output->std = VPIF_V4L2_STD;
1218 return 0;
1221 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1223 struct vpif_fh *fh = priv;
1224 struct channel_obj *ch = fh->channel;
1225 struct video_obj *vid_ch = &ch->video;
1226 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1227 int ret = 0;
1229 if (common->started) {
1230 vpif_err("Streaming in progress\n");
1231 return -EBUSY;
1234 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1235 s_routing, 0, i, 0);
1237 if (ret < 0)
1238 vpif_err("Failed to set output standard\n");
1240 vid_ch->output_id = i;
1241 return ret;
1244 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1246 struct vpif_fh *fh = priv;
1247 struct channel_obj *ch = fh->channel;
1248 struct video_obj *vid_ch = &ch->video;
1250 *i = vid_ch->output_id;
1252 return 0;
1255 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1257 struct vpif_fh *fh = priv;
1258 struct channel_obj *ch = fh->channel;
1260 *p = v4l2_prio_max(&ch->prio);
1262 return 0;
1265 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1267 struct vpif_fh *fh = priv;
1268 struct channel_obj *ch = fh->channel;
1270 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1274 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1275 * @file: file ptr
1276 * @priv: file handle
1277 * @preset: input preset
1279 static int vpif_enum_dv_presets(struct file *file, void *priv,
1280 struct v4l2_dv_enum_preset *preset)
1282 struct vpif_fh *fh = priv;
1283 struct channel_obj *ch = fh->channel;
1284 struct video_obj *vid_ch = &ch->video;
1286 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1287 video, enum_dv_presets, preset);
1291 * vpif_s_dv_presets() - S_DV_PRESETS handler
1292 * @file: file ptr
1293 * @priv: file handle
1294 * @preset: input preset
1296 static int vpif_s_dv_preset(struct file *file, void *priv,
1297 struct v4l2_dv_preset *preset)
1299 struct vpif_fh *fh = priv;
1300 struct channel_obj *ch = fh->channel;
1301 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1302 struct video_obj *vid_ch = &ch->video;
1303 int ret = 0;
1305 if (common->started) {
1306 vpif_dbg(1, debug, "streaming in progress\n");
1307 return -EBUSY;
1310 ret = v4l2_prio_check(&ch->prio, fh->prio);
1311 if (ret != 0)
1312 return ret;
1314 fh->initialized = 1;
1316 /* Call encoder subdevice function to set the standard */
1317 if (mutex_lock_interruptible(&common->lock))
1318 return -ERESTARTSYS;
1320 ch->video.dv_preset = preset->preset;
1321 ch->video.stdid = V4L2_STD_UNKNOWN;
1322 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1324 /* Get the information about the standard */
1325 if (vpif_update_resolution(ch)) {
1326 ret = -EINVAL;
1327 } else {
1328 /* Configure the default format information */
1329 vpif_config_format(ch);
1331 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1332 video, s_dv_preset, preset);
1335 mutex_unlock(&common->lock);
1337 return ret;
1340 * vpif_g_dv_presets() - G_DV_PRESETS handler
1341 * @file: file ptr
1342 * @priv: file handle
1343 * @preset: input preset
1345 static int vpif_g_dv_preset(struct file *file, void *priv,
1346 struct v4l2_dv_preset *preset)
1348 struct vpif_fh *fh = priv;
1349 struct channel_obj *ch = fh->channel;
1351 preset->preset = ch->video.dv_preset;
1353 return 0;
1356 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1357 * @file: file ptr
1358 * @priv: file handle
1359 * @timings: digital video timings
1361 static int vpif_s_dv_timings(struct file *file, void *priv,
1362 struct v4l2_dv_timings *timings)
1364 struct vpif_fh *fh = priv;
1365 struct channel_obj *ch = fh->channel;
1366 struct vpif_params *vpifparams = &ch->vpifparams;
1367 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1368 struct video_obj *vid_ch = &ch->video;
1369 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1370 int ret;
1372 if (timings->type != V4L2_DV_BT_656_1120) {
1373 vpif_dbg(2, debug, "Timing type not defined\n");
1374 return -EINVAL;
1377 /* Configure subdevice timings, if any */
1378 ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
1379 video, s_dv_timings, timings);
1380 if (ret == -ENOIOCTLCMD) {
1381 vpif_dbg(2, debug, "Custom DV timings not supported by "
1382 "subdevice\n");
1383 return -EINVAL;
1385 if (ret < 0) {
1386 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1387 return ret;
1390 if (!(timings->bt.width && timings->bt.height &&
1391 (timings->bt.hbackporch ||
1392 timings->bt.hfrontporch ||
1393 timings->bt.hsync) &&
1394 timings->bt.vfrontporch &&
1395 (timings->bt.vbackporch ||
1396 timings->bt.vsync))) {
1397 vpif_dbg(2, debug, "Timings for width, height, "
1398 "horizontal back porch, horizontal sync, "
1399 "horizontal front porch, vertical back porch, "
1400 "vertical sync and vertical back porch "
1401 "must be defined\n");
1402 return -EINVAL;
1405 *bt = timings->bt;
1407 /* Configure video port timings */
1409 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1410 bt->hsync - 8;
1411 std_info->sav2eav = bt->width;
1413 std_info->l1 = 1;
1414 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1416 if (bt->interlaced) {
1417 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1418 std_info->vsize = bt->height * 2 +
1419 bt->vfrontporch + bt->vsync + bt->vbackporch +
1420 bt->il_vfrontporch + bt->il_vsync +
1421 bt->il_vbackporch;
1422 std_info->l5 = std_info->vsize/2 -
1423 (bt->vfrontporch - 1);
1424 std_info->l7 = std_info->vsize/2 + 1;
1425 std_info->l9 = std_info->l7 + bt->il_vsync +
1426 bt->il_vbackporch + 1;
1427 std_info->l11 = std_info->vsize -
1428 (bt->il_vfrontporch - 1);
1429 } else {
1430 vpif_dbg(2, debug, "Required timing values for "
1431 "interlaced BT format missing\n");
1432 return -EINVAL;
1434 } else {
1435 std_info->vsize = bt->height + bt->vfrontporch +
1436 bt->vsync + bt->vbackporch;
1437 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1439 strncpy(std_info->name, "Custom timings BT656/1120",
1440 VPIF_MAX_NAME);
1441 std_info->width = bt->width;
1442 std_info->height = bt->height;
1443 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1444 std_info->ycmux_mode = 0;
1445 std_info->capture_format = 0;
1446 std_info->vbi_supported = 0;
1447 std_info->hd_sd = 1;
1448 std_info->stdid = 0;
1449 std_info->dv_preset = V4L2_DV_INVALID;
1451 vid_ch->stdid = 0;
1452 vid_ch->dv_preset = V4L2_DV_INVALID;
1454 return 0;
1458 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1459 * @file: file ptr
1460 * @priv: file handle
1461 * @timings: digital video timings
1463 static int vpif_g_dv_timings(struct file *file, void *priv,
1464 struct v4l2_dv_timings *timings)
1466 struct vpif_fh *fh = priv;
1467 struct channel_obj *ch = fh->channel;
1468 struct video_obj *vid_ch = &ch->video;
1469 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1471 timings->bt = *bt;
1473 return 0;
1477 * vpif_g_chip_ident() - Identify the chip
1478 * @file: file ptr
1479 * @priv: file handle
1480 * @chip: chip identity
1482 * Returns zero or -EINVAL if read operations fails.
1484 static int vpif_g_chip_ident(struct file *file, void *priv,
1485 struct v4l2_dbg_chip_ident *chip)
1487 chip->ident = V4L2_IDENT_NONE;
1488 chip->revision = 0;
1489 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1490 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1491 vpif_dbg(2, debug, "match_type is invalid.\n");
1492 return -EINVAL;
1495 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1496 g_chip_ident, chip);
1499 #ifdef CONFIG_VIDEO_ADV_DEBUG
1501 * vpif_dbg_g_register() - Read register
1502 * @file: file ptr
1503 * @priv: file handle
1504 * @reg: register to be read
1506 * Debugging only
1507 * Returns zero or -EINVAL if read operations fails.
1509 static int vpif_dbg_g_register(struct file *file, void *priv,
1510 struct v4l2_dbg_register *reg){
1511 struct vpif_fh *fh = priv;
1512 struct channel_obj *ch = fh->channel;
1513 struct video_obj *vid_ch = &ch->video;
1515 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1516 g_register, reg);
1520 * vpif_dbg_s_register() - Write to register
1521 * @file: file ptr
1522 * @priv: file handle
1523 * @reg: register to be modified
1525 * Debugging only
1526 * Returns zero or -EINVAL if write operations fails.
1528 static int vpif_dbg_s_register(struct file *file, void *priv,
1529 struct v4l2_dbg_register *reg){
1530 struct vpif_fh *fh = priv;
1531 struct channel_obj *ch = fh->channel;
1532 struct video_obj *vid_ch = &ch->video;
1534 return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
1535 s_register, reg);
1537 #endif
1540 * vpif_log_status() - Status information
1541 * @file: file ptr
1542 * @priv: file handle
1544 * Returns zero.
1546 static int vpif_log_status(struct file *filep, void *priv)
1548 /* status for sub devices */
1549 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1551 return 0;
1554 /* vpif display ioctl operations */
1555 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1556 .vidioc_querycap = vpif_querycap,
1557 .vidioc_g_priority = vpif_g_priority,
1558 .vidioc_s_priority = vpif_s_priority,
1559 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1560 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1561 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1562 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1563 .vidioc_reqbufs = vpif_reqbufs,
1564 .vidioc_querybuf = vpif_querybuf,
1565 .vidioc_qbuf = vpif_qbuf,
1566 .vidioc_dqbuf = vpif_dqbuf,
1567 .vidioc_streamon = vpif_streamon,
1568 .vidioc_streamoff = vpif_streamoff,
1569 .vidioc_s_std = vpif_s_std,
1570 .vidioc_g_std = vpif_g_std,
1571 .vidioc_enum_output = vpif_enum_output,
1572 .vidioc_s_output = vpif_s_output,
1573 .vidioc_g_output = vpif_g_output,
1574 .vidioc_cropcap = vpif_cropcap,
1575 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
1576 .vidioc_s_dv_preset = vpif_s_dv_preset,
1577 .vidioc_g_dv_preset = vpif_g_dv_preset,
1578 .vidioc_s_dv_timings = vpif_s_dv_timings,
1579 .vidioc_g_dv_timings = vpif_g_dv_timings,
1580 .vidioc_g_chip_ident = vpif_g_chip_ident,
1581 #ifdef CONFIG_VIDEO_ADV_DEBUG
1582 .vidioc_g_register = vpif_dbg_g_register,
1583 .vidioc_s_register = vpif_dbg_s_register,
1584 #endif
1585 .vidioc_log_status = vpif_log_status,
1588 static const struct v4l2_file_operations vpif_fops = {
1589 .owner = THIS_MODULE,
1590 .open = vpif_open,
1591 .release = vpif_release,
1592 .unlocked_ioctl = video_ioctl2,
1593 .mmap = vpif_mmap,
1594 .poll = vpif_poll
1597 static struct video_device vpif_video_template = {
1598 .name = "vpif",
1599 .fops = &vpif_fops,
1600 .ioctl_ops = &vpif_ioctl_ops,
1601 .tvnorms = VPIF_V4L2_STD,
1602 .current_norm = V4L2_STD_625_50,
1606 /*Configure the channels, buffer sizei, request irq */
1607 static int initialize_vpif(void)
1609 int free_channel_objects_index;
1610 int free_buffer_channel_index;
1611 int free_buffer_index;
1612 int err = 0, i, j;
1614 /* Default number of buffers should be 3 */
1615 if ((ch2_numbuffers > 0) &&
1616 (ch2_numbuffers < config_params.min_numbuffers))
1617 ch2_numbuffers = config_params.min_numbuffers;
1618 if ((ch3_numbuffers > 0) &&
1619 (ch3_numbuffers < config_params.min_numbuffers))
1620 ch3_numbuffers = config_params.min_numbuffers;
1622 /* Set buffer size to min buffers size if invalid buffer size is
1623 * given */
1624 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1625 ch2_bufsize =
1626 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1627 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1628 ch3_bufsize =
1629 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1631 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1633 if (ch2_numbuffers) {
1634 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1635 ch2_bufsize;
1637 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1639 if (ch3_numbuffers) {
1640 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1641 ch3_bufsize;
1644 /* Allocate memory for six channel objects */
1645 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1646 vpif_obj.dev[i] =
1647 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1648 /* If memory allocation fails, return error */
1649 if (!vpif_obj.dev[i]) {
1650 free_channel_objects_index = i;
1651 err = -ENOMEM;
1652 goto vpif_init_free_channel_objects;
1656 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1657 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1658 free_buffer_index = config_params.numbuffers[i - 1];
1660 return 0;
1662 vpif_init_free_channel_objects:
1663 for (j = 0; j < free_channel_objects_index; j++)
1664 kfree(vpif_obj.dev[j]);
1665 return err;
1669 * vpif_probe: This function creates device entries by register itself to the
1670 * V4L2 driver and initializes fields of each channel objects
1672 static __init int vpif_probe(struct platform_device *pdev)
1674 struct vpif_subdev_info *subdevdata;
1675 struct vpif_display_config *config;
1676 int i, j = 0, k, q, m, err = 0;
1677 struct i2c_adapter *i2c_adap;
1678 struct common_obj *common;
1679 struct channel_obj *ch;
1680 struct video_device *vfd;
1681 struct resource *res;
1682 int subdev_count;
1683 size_t size;
1685 vpif_dev = &pdev->dev;
1686 err = initialize_vpif();
1688 if (err) {
1689 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1690 return err;
1693 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1694 if (err) {
1695 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1696 return err;
1699 k = 0;
1700 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1701 for (i = res->start; i <= res->end; i++) {
1702 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
1703 "VPIF_Display",
1704 (void *)(&vpif_obj.dev[k]->channel_id))) {
1705 err = -EBUSY;
1706 goto vpif_int_err;
1709 k++;
1712 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1714 /* Get the pointer to the channel object */
1715 ch = vpif_obj.dev[i];
1717 /* Allocate memory for video device */
1718 vfd = video_device_alloc();
1719 if (vfd == NULL) {
1720 for (j = 0; j < i; j++) {
1721 ch = vpif_obj.dev[j];
1722 video_device_release(ch->video_dev);
1724 err = -ENOMEM;
1725 goto vpif_int_err;
1728 /* Initialize field of video device */
1729 *vfd = vpif_video_template;
1730 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1731 vfd->release = video_device_release;
1732 snprintf(vfd->name, sizeof(vfd->name),
1733 "VPIF_Display_DRIVER_V%s",
1734 VPIF_DISPLAY_VERSION);
1736 /* Set video_dev to the video device */
1737 ch->video_dev = vfd;
1740 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1741 if (res) {
1742 size = resource_size(res);
1743 /* The resources are divided into two equal memory and when
1744 * we have HD output we can add them together
1746 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1747 ch = vpif_obj.dev[j];
1748 ch->channel_id = j;
1750 /* only enabled if second resource exists */
1751 config_params.video_limit[ch->channel_id] = 0;
1752 if (size)
1753 config_params.video_limit[ch->channel_id] =
1754 size/2;
1758 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1759 ch = vpif_obj.dev[j];
1760 /* Initialize field of the channel objects */
1761 atomic_set(&ch->usrs, 0);
1762 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1763 ch->common[k].numbuffers = 0;
1764 common = &ch->common[k];
1765 common->io_usrs = 0;
1766 common->started = 0;
1767 spin_lock_init(&common->irqlock);
1768 mutex_init(&common->lock);
1769 common->numbuffers = 0;
1770 common->set_addr = NULL;
1771 common->ytop_off = common->ybtm_off = 0;
1772 common->ctop_off = common->cbtm_off = 0;
1773 common->cur_frm = common->next_frm = NULL;
1774 memset(&common->fmt, 0, sizeof(common->fmt));
1775 common->numbuffers = config_params.numbuffers[k];
1778 ch->initialized = 0;
1779 ch->channel_id = j;
1780 if (j < 2)
1781 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1782 config_params.numbuffers[ch->channel_id];
1783 else
1784 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1786 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1788 /* Initialize prio member of channel object */
1789 v4l2_prio_init(&ch->prio);
1790 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1791 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1792 /* Locking in file operations other than ioctl should be done
1793 by the driver, not the V4L2 core.
1794 This driver needs auditing so that this flag can be removed. */
1795 set_bit(V4L2_FL_LOCK_ALL_FOPS, &ch->video_dev->flags);
1796 ch->video_dev->lock = &common->lock;
1798 /* register video device */
1799 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1800 (int)ch, (int)&ch->video_dev);
1802 err = video_register_device(ch->video_dev,
1803 VFL_TYPE_GRABBER, (j ? 3 : 2));
1804 if (err < 0)
1805 goto probe_out;
1807 video_set_drvdata(ch->video_dev, ch);
1810 i2c_adap = i2c_get_adapter(1);
1811 config = pdev->dev.platform_data;
1812 subdev_count = config->subdev_count;
1813 subdevdata = config->subdevinfo;
1814 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1815 GFP_KERNEL);
1816 if (vpif_obj.sd == NULL) {
1817 vpif_err("unable to allocate memory for subdevice pointers\n");
1818 err = -ENOMEM;
1819 goto probe_out;
1822 for (i = 0; i < subdev_count; i++) {
1823 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1824 i2c_adap,
1825 &subdevdata[i].board_info,
1826 NULL);
1827 if (!vpif_obj.sd[i]) {
1828 vpif_err("Error registering v4l2 subdevice\n");
1829 goto probe_subdev_out;
1832 if (vpif_obj.sd[i])
1833 vpif_obj.sd[i]->grp_id = 1 << i;
1836 v4l2_info(&vpif_obj.v4l2_dev,
1837 " VPIF display driver initialized\n");
1838 return 0;
1840 probe_subdev_out:
1841 kfree(vpif_obj.sd);
1842 probe_out:
1843 for (k = 0; k < j; k++) {
1844 ch = vpif_obj.dev[k];
1845 video_unregister_device(ch->video_dev);
1846 video_device_release(ch->video_dev);
1847 ch->video_dev = NULL;
1849 vpif_int_err:
1850 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1851 vpif_err("VPIF IRQ request failed\n");
1852 for (q = k; k >= 0; k--) {
1853 for (m = i; m >= res->start; m--)
1854 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1855 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1856 m = res->end;
1859 return err;
1863 * vpif_remove: It un-register channels from V4L2 driver
1865 static int vpif_remove(struct platform_device *device)
1867 struct channel_obj *ch;
1868 int i;
1870 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1872 /* un-register device */
1873 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1874 /* Get the pointer to the channel object */
1875 ch = vpif_obj.dev[i];
1876 /* Unregister video device */
1877 video_unregister_device(ch->video_dev);
1879 ch->video_dev = NULL;
1882 return 0;
1885 #ifdef CONFIG_PM
1886 static int vpif_suspend(struct device *dev)
1888 struct common_obj *common;
1889 struct channel_obj *ch;
1890 int i;
1892 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1893 /* Get the pointer to the channel object */
1894 ch = vpif_obj.dev[i];
1895 common = &ch->common[VPIF_VIDEO_INDEX];
1896 mutex_lock(&common->lock);
1897 if (atomic_read(&ch->usrs) && common->io_usrs) {
1898 /* Disable channel */
1899 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1900 enable_channel2(0);
1901 channel2_intr_enable(0);
1903 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1904 common->started == 2) {
1905 enable_channel3(0);
1906 channel3_intr_enable(0);
1909 mutex_unlock(&common->lock);
1912 return 0;
1915 static int vpif_resume(struct device *dev)
1918 struct common_obj *common;
1919 struct channel_obj *ch;
1920 int i;
1922 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1923 /* Get the pointer to the channel object */
1924 ch = vpif_obj.dev[i];
1925 common = &ch->common[VPIF_VIDEO_INDEX];
1926 mutex_lock(&common->lock);
1927 if (atomic_read(&ch->usrs) && common->io_usrs) {
1928 /* Enable channel */
1929 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1930 enable_channel2(1);
1931 channel2_intr_enable(1);
1933 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1934 common->started == 2) {
1935 enable_channel3(1);
1936 channel3_intr_enable(1);
1939 mutex_unlock(&common->lock);
1942 return 0;
1945 static const struct dev_pm_ops vpif_pm = {
1946 .suspend = vpif_suspend,
1947 .resume = vpif_resume,
1950 #define vpif_pm_ops (&vpif_pm)
1951 #else
1952 #define vpif_pm_ops NULL
1953 #endif
1955 static __refdata struct platform_driver vpif_driver = {
1956 .driver = {
1957 .name = "vpif_display",
1958 .owner = THIS_MODULE,
1959 .pm = vpif_pm_ops,
1961 .probe = vpif_probe,
1962 .remove = vpif_remove,
1965 static __init int vpif_init(void)
1967 return platform_driver_register(&vpif_driver);
1971 * vpif_cleanup: This function un-registers device and driver to the kernel,
1972 * frees requested irq handler and de-allocates memory allocated for channel
1973 * objects.
1975 static void vpif_cleanup(void)
1977 struct platform_device *pdev;
1978 struct resource *res;
1979 int irq_num;
1980 int i = 0;
1982 pdev = container_of(vpif_dev, struct platform_device, dev);
1984 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1985 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1986 free_irq(irq_num,
1987 (void *)(&vpif_obj.dev[i]->channel_id));
1988 i++;
1991 platform_driver_unregister(&vpif_driver);
1992 kfree(vpif_obj.sd);
1993 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1994 kfree(vpif_obj.dev[i]);
1997 module_init(vpif_init);
1998 module_exit(vpif_cleanup);