[media] vpif_display: use a v4l2_subdev pointer to call a subdev
[linux-2.6/btrfs-unstable.git] / drivers / media / platform / davinci / vpif_display.c
blob6229e4844bed58fe6e38e77a3e0de3a239ef7736
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 if (vpif_config_data->set_clock) {
284 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
285 ycmux_mode, ch->vpifparams.std_info.hd_sd);
286 if (ret < 0) {
287 vpif_err("can't set clock\n");
288 return ret;
292 /* set the parameters and addresses */
293 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
294 if (ret < 0)
295 return ret;
297 common->started = ret;
298 vpif_config_addr(ch, ret);
299 common->set_addr((addr + common->ytop_off),
300 (addr + common->ybtm_off),
301 (addr + common->ctop_off),
302 (addr + common->cbtm_off));
304 /* Set interrupt for both the fields in VPIF
305 Register enable channel in VPIF register */
306 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
307 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
308 channel2_intr_assert();
309 channel2_intr_enable(1);
310 enable_channel2(1);
311 if (vpif_config_data->ch2_clip_en)
312 channel2_clipping_enable(1);
315 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
316 || (common->started == 2)) {
317 channel3_intr_assert();
318 channel3_intr_enable(1);
319 enable_channel3(1);
320 if (vpif_config_data->ch3_clip_en)
321 channel3_clipping_enable(1);
324 return 0;
327 /* abort streaming and wait for last buffer */
328 static int vpif_stop_streaming(struct vb2_queue *vq)
330 struct vpif_fh *fh = vb2_get_drv_priv(vq);
331 struct channel_obj *ch = fh->channel;
332 struct common_obj *common;
334 if (!vb2_is_streaming(vq))
335 return 0;
337 common = &ch->common[VPIF_VIDEO_INDEX];
339 /* release all active buffers */
340 while (!list_empty(&common->dma_queue)) {
341 common->next_frm = list_entry(common->dma_queue.next,
342 struct vpif_disp_buffer, list);
343 list_del(&common->next_frm->list);
344 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
347 return 0;
350 static struct vb2_ops video_qops = {
351 .queue_setup = vpif_buffer_queue_setup,
352 .wait_prepare = vpif_wait_prepare,
353 .wait_finish = vpif_wait_finish,
354 .buf_init = vpif_buffer_init,
355 .buf_prepare = vpif_buffer_prepare,
356 .start_streaming = vpif_start_streaming,
357 .stop_streaming = vpif_stop_streaming,
358 .buf_cleanup = vpif_buf_cleanup,
359 .buf_queue = vpif_buffer_queue,
362 static void process_progressive_mode(struct common_obj *common)
364 unsigned long addr = 0;
366 /* Get the next buffer from buffer queue */
367 common->next_frm = list_entry(common->dma_queue.next,
368 struct vpif_disp_buffer, list);
369 /* Remove that buffer from the buffer queue */
370 list_del(&common->next_frm->list);
371 /* Mark status of the buffer as active */
372 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
374 /* Set top and bottom field addrs in VPIF registers */
375 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
376 common->set_addr(addr + common->ytop_off,
377 addr + common->ybtm_off,
378 addr + common->ctop_off,
379 addr + common->cbtm_off);
382 static void process_interlaced_mode(int fid, struct common_obj *common)
384 /* device field id and local field id are in sync */
385 /* If this is even field */
386 if (0 == fid) {
387 if (common->cur_frm == common->next_frm)
388 return;
390 /* one frame is displayed If next frame is
391 * available, release cur_frm and move on */
392 /* Copy frame display time */
393 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
394 /* Change status of the cur_frm */
395 vb2_buffer_done(&common->cur_frm->vb,
396 VB2_BUF_STATE_DONE);
397 /* Make cur_frm pointing to next_frm */
398 common->cur_frm = common->next_frm;
400 } else if (1 == fid) { /* odd field */
401 if (list_empty(&common->dma_queue)
402 || (common->cur_frm != common->next_frm)) {
403 return;
405 /* one field is displayed configure the next
406 * frame if it is available else hold on current
407 * frame */
408 /* Get next from the buffer queue */
409 process_progressive_mode(common);
415 * vpif_channel_isr: It changes status of the displayed buffer, takes next
416 * buffer from the queue and sets its address in VPIF registers
418 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
420 struct vpif_device *dev = &vpif_obj;
421 struct channel_obj *ch;
422 struct common_obj *common;
423 enum v4l2_field field;
424 int fid = -1, i;
425 int channel_id = 0;
427 channel_id = *(int *)(dev_id);
428 if (!vpif_intr_status(channel_id + 2))
429 return IRQ_NONE;
431 ch = dev->dev[channel_id];
432 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
433 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
434 common = &ch->common[i];
435 /* If streaming is started in this channel */
436 if (0 == common->started)
437 continue;
439 if (1 == ch->vpifparams.std_info.frm_fmt) {
440 if (list_empty(&common->dma_queue))
441 continue;
443 /* Progressive mode */
444 if (!channel_first_int[i][channel_id]) {
445 /* Mark status of the cur_frm to
446 * done and unlock semaphore on it */
447 do_gettimeofday(&common->cur_frm->vb.
448 v4l2_buf.timestamp);
449 vb2_buffer_done(&common->cur_frm->vb,
450 VB2_BUF_STATE_DONE);
451 /* Make cur_frm pointing to next_frm */
452 common->cur_frm = common->next_frm;
455 channel_first_int[i][channel_id] = 0;
456 process_progressive_mode(common);
457 } else {
458 /* Interlaced mode */
459 /* If it is first interrupt, ignore it */
461 if (channel_first_int[i][channel_id]) {
462 channel_first_int[i][channel_id] = 0;
463 continue;
466 if (0 == i) {
467 ch->field_id ^= 1;
468 /* Get field id from VPIF registers */
469 fid = vpif_channel_getfid(ch->channel_id + 2);
470 /* If fid does not match with stored field id */
471 if (fid != ch->field_id) {
472 /* Make them in sync */
473 if (0 == fid)
474 ch->field_id = fid;
476 return IRQ_HANDLED;
479 process_interlaced_mode(fid, common);
483 return IRQ_HANDLED;
486 static int vpif_update_std_info(struct channel_obj *ch)
488 struct video_obj *vid_ch = &ch->video;
489 struct vpif_params *vpifparams = &ch->vpifparams;
490 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
491 const struct vpif_channel_config_params *config;
493 int i;
495 for (i = 0; i < vpif_ch_params_count; i++) {
496 config = &ch_params[i];
497 if (config->hd_sd == 0) {
498 vpif_dbg(2, debug, "SD format\n");
499 if (config->stdid & vid_ch->stdid) {
500 memcpy(std_info, config, sizeof(*config));
501 break;
506 if (i == vpif_ch_params_count) {
507 vpif_dbg(1, debug, "Format not found\n");
508 return -EINVAL;
511 return 0;
514 static int vpif_update_resolution(struct channel_obj *ch)
516 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
517 struct video_obj *vid_ch = &ch->video;
518 struct vpif_params *vpifparams = &ch->vpifparams;
519 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
521 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
522 return -EINVAL;
524 if (vid_ch->stdid) {
525 if (vpif_update_std_info(ch))
526 return -EINVAL;
529 common->fmt.fmt.pix.width = std_info->width;
530 common->fmt.fmt.pix.height = std_info->height;
531 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
532 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
534 /* Set height and width paramateres */
535 common->height = std_info->height;
536 common->width = std_info->width;
538 return 0;
542 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
543 * in the top and bottom field
545 static void vpif_calculate_offsets(struct channel_obj *ch)
547 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
548 struct vpif_params *vpifparams = &ch->vpifparams;
549 enum v4l2_field field = common->fmt.fmt.pix.field;
550 struct video_obj *vid_ch = &ch->video;
551 unsigned int hpitch, vpitch, sizeimage;
553 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
554 if (ch->vpifparams.std_info.frm_fmt)
555 vid_ch->buf_field = V4L2_FIELD_NONE;
556 else
557 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
558 } else {
559 vid_ch->buf_field = common->fmt.fmt.pix.field;
562 sizeimage = common->fmt.fmt.pix.sizeimage;
564 hpitch = common->fmt.fmt.pix.bytesperline;
565 vpitch = sizeimage / (hpitch * 2);
566 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
567 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
568 common->ytop_off = 0;
569 common->ybtm_off = hpitch;
570 common->ctop_off = sizeimage / 2;
571 common->cbtm_off = sizeimage / 2 + hpitch;
572 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
573 common->ytop_off = 0;
574 common->ybtm_off = sizeimage / 4;
575 common->ctop_off = sizeimage / 2;
576 common->cbtm_off = common->ctop_off + sizeimage / 4;
577 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
578 common->ybtm_off = 0;
579 common->ytop_off = sizeimage / 4;
580 common->cbtm_off = sizeimage / 2;
581 common->ctop_off = common->cbtm_off + sizeimage / 4;
584 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
585 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
586 vpifparams->video_params.storage_mode = 1;
587 } else {
588 vpifparams->video_params.storage_mode = 0;
591 if (ch->vpifparams.std_info.frm_fmt == 1) {
592 vpifparams->video_params.hpitch =
593 common->fmt.fmt.pix.bytesperline;
594 } else {
595 if ((field == V4L2_FIELD_ANY) ||
596 (field == V4L2_FIELD_INTERLACED))
597 vpifparams->video_params.hpitch =
598 common->fmt.fmt.pix.bytesperline * 2;
599 else
600 vpifparams->video_params.hpitch =
601 common->fmt.fmt.pix.bytesperline;
604 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
607 static void vpif_config_format(struct channel_obj *ch)
609 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
611 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
612 if (config_params.numbuffers[ch->channel_id] == 0)
613 common->memory = V4L2_MEMORY_USERPTR;
614 else
615 common->memory = V4L2_MEMORY_MMAP;
617 common->fmt.fmt.pix.sizeimage =
618 config_params.channel_bufsize[ch->channel_id];
619 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
620 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
623 static int vpif_check_format(struct channel_obj *ch,
624 struct v4l2_pix_format *pixfmt)
626 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
627 enum v4l2_field field = pixfmt->field;
628 u32 sizeimage, hpitch, vpitch;
630 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
631 goto invalid_fmt_exit;
633 if (!(VPIF_VALID_FIELD(field)))
634 goto invalid_fmt_exit;
636 if (pixfmt->bytesperline <= 0)
637 goto invalid_pitch_exit;
639 sizeimage = pixfmt->sizeimage;
641 if (vpif_update_resolution(ch))
642 return -EINVAL;
644 hpitch = pixfmt->bytesperline;
645 vpitch = sizeimage / (hpitch * 2);
647 /* Check for valid value of pitch */
648 if ((hpitch < ch->vpifparams.std_info.width) ||
649 (vpitch < ch->vpifparams.std_info.height))
650 goto invalid_pitch_exit;
652 /* Check for 8 byte alignment */
653 if (!ISALIGNED(hpitch)) {
654 vpif_err("invalid pitch alignment\n");
655 return -EINVAL;
657 pixfmt->width = common->fmt.fmt.pix.width;
658 pixfmt->height = common->fmt.fmt.pix.height;
660 return 0;
662 invalid_fmt_exit:
663 vpif_err("invalid field format\n");
664 return -EINVAL;
666 invalid_pitch_exit:
667 vpif_err("invalid pitch\n");
668 return -EINVAL;
671 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
673 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
675 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
676 common->set_addr = ch3_set_videobuf_addr;
677 } else {
678 if (2 == muxmode)
679 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
680 else
681 common->set_addr = ch2_set_videobuf_addr;
686 * vpif_mmap: It is used to map kernel space buffers into user spaces
688 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
690 struct vpif_fh *fh = filep->private_data;
691 struct channel_obj *ch = fh->channel;
692 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
693 int ret;
695 vpif_dbg(2, debug, "vpif_mmap\n");
697 if (mutex_lock_interruptible(&common->lock))
698 return -ERESTARTSYS;
699 ret = vb2_mmap(&common->buffer_queue, vma);
700 mutex_unlock(&common->lock);
701 return ret;
705 * vpif_poll: It is used for select/poll system call
707 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
709 struct vpif_fh *fh = filep->private_data;
710 struct channel_obj *ch = fh->channel;
711 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
712 unsigned int res = 0;
714 if (common->started) {
715 mutex_lock(&common->lock);
716 res = vb2_poll(&common->buffer_queue, filep, wait);
717 mutex_unlock(&common->lock);
720 return res;
724 * vpif_open: It creates object of file handle structure and stores it in
725 * private_data member of filepointer
727 static int vpif_open(struct file *filep)
729 struct video_device *vdev = video_devdata(filep);
730 struct channel_obj *ch = video_get_drvdata(vdev);
731 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
732 struct vpif_fh *fh;
734 /* Allocate memory for the file handle object */
735 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
736 if (fh == NULL) {
737 vpif_err("unable to allocate memory for file handle object\n");
738 return -ENOMEM;
741 if (mutex_lock_interruptible(&common->lock)) {
742 kfree(fh);
743 return -ERESTARTSYS;
745 /* store pointer to fh in private_data member of filep */
746 filep->private_data = fh;
747 fh->channel = ch;
748 fh->initialized = 0;
749 if (!ch->initialized) {
750 fh->initialized = 1;
751 ch->initialized = 1;
752 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
755 /* Increment channel usrs counter */
756 atomic_inc(&ch->usrs);
757 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
758 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
759 /* Initialize priority of this instance to default priority */
760 fh->prio = V4L2_PRIORITY_UNSET;
761 v4l2_prio_open(&ch->prio, &fh->prio);
762 mutex_unlock(&common->lock);
764 return 0;
768 * vpif_release: This function deletes buffer queue, frees the buffers and
769 * the vpif file handle
771 static int vpif_release(struct file *filep)
773 struct vpif_fh *fh = filep->private_data;
774 struct channel_obj *ch = fh->channel;
775 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
777 mutex_lock(&common->lock);
778 /* if this instance is doing IO */
779 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
780 /* Reset io_usrs member of channel object */
781 common->io_usrs = 0;
782 /* Disable channel */
783 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
784 enable_channel2(0);
785 channel2_intr_enable(0);
787 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
788 (2 == common->started)) {
789 enable_channel3(0);
790 channel3_intr_enable(0);
792 common->started = 0;
794 /* Free buffers allocated */
795 vb2_queue_release(&common->buffer_queue);
796 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
798 common->numbuffers =
799 config_params.numbuffers[ch->channel_id];
802 /* Decrement channel usrs counter */
803 atomic_dec(&ch->usrs);
804 /* If this file handle has initialize encoder device, reset it */
805 if (fh->initialized)
806 ch->initialized = 0;
808 /* Close the priority */
809 v4l2_prio_close(&ch->prio, fh->prio);
810 filep->private_data = NULL;
811 fh->initialized = 0;
812 mutex_unlock(&common->lock);
813 kfree(fh);
815 return 0;
818 /* functions implementing ioctls */
820 * vpif_querycap() - QUERYCAP handler
821 * @file: file ptr
822 * @priv: file handle
823 * @cap: ptr to v4l2_capability structure
825 static int vpif_querycap(struct file *file, void *priv,
826 struct v4l2_capability *cap)
828 struct vpif_display_config *config = vpif_dev->platform_data;
830 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
831 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
832 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
833 strlcpy(cap->card, config->card_name, sizeof(cap->card));
835 return 0;
838 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
839 struct v4l2_fmtdesc *fmt)
841 if (fmt->index != 0) {
842 vpif_err("Invalid format index\n");
843 return -EINVAL;
846 /* Fill in the information about format */
847 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
848 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
849 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
851 return 0;
854 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
855 struct v4l2_format *fmt)
857 struct vpif_fh *fh = priv;
858 struct channel_obj *ch = fh->channel;
859 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
861 /* Check the validity of the buffer type */
862 if (common->fmt.type != fmt->type)
863 return -EINVAL;
865 if (vpif_update_resolution(ch))
866 return -EINVAL;
867 *fmt = common->fmt;
868 return 0;
871 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
872 struct v4l2_format *fmt)
874 struct vpif_fh *fh = priv;
875 struct v4l2_pix_format *pixfmt;
876 struct channel_obj *ch = fh->channel;
877 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
878 int ret = 0;
880 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
881 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
882 if (!fh->initialized) {
883 vpif_dbg(1, debug, "Channel Busy\n");
884 return -EBUSY;
887 /* Check for the priority */
888 ret = v4l2_prio_check(&ch->prio, fh->prio);
889 if (0 != ret)
890 return ret;
891 fh->initialized = 1;
894 if (common->started) {
895 vpif_dbg(1, debug, "Streaming in progress\n");
896 return -EBUSY;
899 pixfmt = &fmt->fmt.pix;
900 /* Check for valid field format */
901 ret = vpif_check_format(ch, pixfmt);
902 if (ret)
903 return ret;
905 /* store the pix format in the channel object */
906 common->fmt.fmt.pix = *pixfmt;
907 /* store the format in the channel object */
908 common->fmt = *fmt;
909 return 0;
912 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
913 struct v4l2_format *fmt)
915 struct vpif_fh *fh = priv;
916 struct channel_obj *ch = fh->channel;
917 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
918 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
919 int ret = 0;
921 ret = vpif_check_format(ch, pixfmt);
922 if (ret) {
923 *pixfmt = common->fmt.fmt.pix;
924 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
927 return ret;
930 static int vpif_reqbufs(struct file *file, void *priv,
931 struct v4l2_requestbuffers *reqbuf)
933 struct vpif_fh *fh = priv;
934 struct channel_obj *ch = fh->channel;
935 struct common_obj *common;
936 enum v4l2_field field;
937 struct vb2_queue *q;
938 u8 index = 0;
940 /* This file handle has not initialized the channel,
941 It is not allowed to do settings */
942 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
943 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
944 if (!fh->initialized) {
945 vpif_err("Channel Busy\n");
946 return -EBUSY;
950 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
951 return -EINVAL;
953 index = VPIF_VIDEO_INDEX;
955 common = &ch->common[index];
957 if (common->fmt.type != reqbuf->type || !vpif_dev)
958 return -EINVAL;
959 if (0 != common->io_usrs)
960 return -EBUSY;
962 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
963 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
964 field = V4L2_FIELD_INTERLACED;
965 else
966 field = common->fmt.fmt.pix.field;
967 } else {
968 field = V4L2_VBI_INTERLACED;
970 /* Initialize videobuf2 queue as per the buffer type */
971 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
972 if (!common->alloc_ctx) {
973 vpif_err("Failed to get the context\n");
974 return -EINVAL;
976 q = &common->buffer_queue;
977 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
978 q->io_modes = VB2_MMAP | VB2_USERPTR;
979 q->drv_priv = fh;
980 q->ops = &video_qops;
981 q->mem_ops = &vb2_dma_contig_memops;
982 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
984 vb2_queue_init(q);
986 /* Set io allowed member of file handle to TRUE */
987 fh->io_allowed[index] = 1;
988 /* Increment io usrs member of channel object to 1 */
989 common->io_usrs = 1;
990 /* Store type of memory requested in channel object */
991 common->memory = reqbuf->memory;
992 INIT_LIST_HEAD(&common->dma_queue);
993 /* Allocate buffers */
994 return vb2_reqbufs(&common->buffer_queue, reqbuf);
997 static int vpif_querybuf(struct file *file, void *priv,
998 struct v4l2_buffer *tbuf)
1000 struct vpif_fh *fh = priv;
1001 struct channel_obj *ch = fh->channel;
1002 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1004 if (common->fmt.type != tbuf->type)
1005 return -EINVAL;
1007 return vb2_querybuf(&common->buffer_queue, tbuf);
1010 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1012 struct vpif_fh *fh = NULL;
1013 struct channel_obj *ch = NULL;
1014 struct common_obj *common = NULL;
1016 if (!buf || !priv)
1017 return -EINVAL;
1019 fh = priv;
1020 ch = fh->channel;
1021 if (!ch)
1022 return -EINVAL;
1024 common = &(ch->common[VPIF_VIDEO_INDEX]);
1025 if (common->fmt.type != buf->type)
1026 return -EINVAL;
1028 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1029 vpif_err("fh->io_allowed\n");
1030 return -EACCES;
1033 return vb2_qbuf(&common->buffer_queue, buf);
1036 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1038 struct vpif_fh *fh = priv;
1039 struct channel_obj *ch = fh->channel;
1040 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1041 int ret = 0;
1043 if (!(*std_id & VPIF_V4L2_STD))
1044 return -EINVAL;
1046 if (common->started) {
1047 vpif_err("streaming in progress\n");
1048 return -EBUSY;
1051 /* Call encoder subdevice function to set the standard */
1052 ch->video.stdid = *std_id;
1053 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1054 /* Get the information about the standard */
1055 if (vpif_update_resolution(ch))
1056 return -EINVAL;
1058 if ((ch->vpifparams.std_info.width *
1059 ch->vpifparams.std_info.height * 2) >
1060 config_params.channel_bufsize[ch->channel_id]) {
1061 vpif_err("invalid std for this size\n");
1062 return -EINVAL;
1065 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1066 /* Configure the default format information */
1067 vpif_config_format(ch);
1069 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1070 s_std_output, *std_id);
1071 if (ret < 0) {
1072 vpif_err("Failed to set output standard\n");
1073 return ret;
1076 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1077 s_std, *std_id);
1078 if (ret < 0)
1079 vpif_err("Failed to set standard for sub devices\n");
1080 return ret;
1083 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1085 struct vpif_fh *fh = priv;
1086 struct channel_obj *ch = fh->channel;
1088 *std = ch->video.stdid;
1089 return 0;
1092 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1094 struct vpif_fh *fh = priv;
1095 struct channel_obj *ch = fh->channel;
1096 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1098 return vb2_dqbuf(&common->buffer_queue, p,
1099 (file->f_flags & O_NONBLOCK));
1102 static int vpif_streamon(struct file *file, void *priv,
1103 enum v4l2_buf_type buftype)
1105 struct vpif_fh *fh = priv;
1106 struct channel_obj *ch = fh->channel;
1107 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1108 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1109 int ret = 0;
1111 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1112 vpif_err("buffer type not supported\n");
1113 return -EINVAL;
1116 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1117 vpif_err("fh->io_allowed\n");
1118 return -EACCES;
1121 /* If Streaming is already started, return error */
1122 if (common->started) {
1123 vpif_err("channel->started\n");
1124 return -EBUSY;
1127 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1128 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1129 ch->vpifparams.std_info.ycmux_mode == 0)
1130 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1131 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1132 vpif_err("other channel is using\n");
1133 return -EBUSY;
1136 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1137 if (ret < 0)
1138 return ret;
1140 /* Call vb2_streamon to start streaming in videobuf2 */
1141 ret = vb2_streamon(&common->buffer_queue, buftype);
1142 if (ret < 0) {
1143 vpif_err("vb2_streamon\n");
1144 return ret;
1147 return ret;
1150 static int vpif_streamoff(struct file *file, void *priv,
1151 enum v4l2_buf_type buftype)
1153 struct vpif_fh *fh = priv;
1154 struct channel_obj *ch = fh->channel;
1155 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1156 struct vpif_display_config *vpif_config_data =
1157 vpif_dev->platform_data;
1159 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1160 vpif_err("buffer type not supported\n");
1161 return -EINVAL;
1164 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1165 vpif_err("fh->io_allowed\n");
1166 return -EACCES;
1169 if (!common->started) {
1170 vpif_err("channel->started\n");
1171 return -EINVAL;
1174 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1175 /* disable channel */
1176 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1177 if (vpif_config_data->ch2_clip_en)
1178 channel2_clipping_enable(0);
1179 enable_channel2(0);
1180 channel2_intr_enable(0);
1182 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1183 (2 == common->started)) {
1184 if (vpif_config_data->ch3_clip_en)
1185 channel3_clipping_enable(0);
1186 enable_channel3(0);
1187 channel3_intr_enable(0);
1191 common->started = 0;
1192 return vb2_streamoff(&common->buffer_queue, buftype);
1195 static int vpif_cropcap(struct file *file, void *priv,
1196 struct v4l2_cropcap *crop)
1198 struct vpif_fh *fh = priv;
1199 struct channel_obj *ch = fh->channel;
1200 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1201 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1202 return -EINVAL;
1204 crop->bounds.left = crop->bounds.top = 0;
1205 crop->defrect.left = crop->defrect.top = 0;
1206 crop->defrect.height = crop->bounds.height = common->height;
1207 crop->defrect.width = crop->bounds.width = common->width;
1209 return 0;
1212 static int vpif_enum_output(struct file *file, void *fh,
1213 struct v4l2_output *output)
1216 struct vpif_display_config *config = vpif_dev->platform_data;
1218 if (output->index >= config->output_count) {
1219 vpif_dbg(1, debug, "Invalid output index\n");
1220 return -EINVAL;
1223 strcpy(output->name, config->output[output->index]);
1224 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1225 output->std = VPIF_V4L2_STD;
1227 return 0;
1230 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1232 struct vpif_fh *fh = priv;
1233 struct channel_obj *ch = fh->channel;
1234 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1235 int ret = 0;
1237 if (common->started) {
1238 vpif_err("Streaming in progress\n");
1239 return -EBUSY;
1242 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1243 s_routing, 0, i, 0);
1245 if (ret < 0)
1246 vpif_err("Failed to set output standard\n");
1248 ch->output_idx = i;
1249 if (vpif_obj.sd[i])
1250 ch->sd = vpif_obj.sd[i];
1251 return ret;
1254 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1256 struct vpif_fh *fh = priv;
1257 struct channel_obj *ch = fh->channel;
1259 *i = ch->output_idx;
1261 return 0;
1264 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1266 struct vpif_fh *fh = priv;
1267 struct channel_obj *ch = fh->channel;
1269 *p = v4l2_prio_max(&ch->prio);
1271 return 0;
1274 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1276 struct vpif_fh *fh = priv;
1277 struct channel_obj *ch = fh->channel;
1279 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1283 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1284 * @file: file ptr
1285 * @priv: file handle
1286 * @timings: input timings
1288 static int
1289 vpif_enum_dv_timings(struct file *file, void *priv,
1290 struct v4l2_enum_dv_timings *timings)
1292 struct vpif_fh *fh = priv;
1293 struct channel_obj *ch = fh->channel;
1295 return v4l2_subdev_call(vpif_obj.sd[ch->output_idx],
1296 video, enum_dv_timings, timings);
1300 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1301 * @file: file ptr
1302 * @priv: file handle
1303 * @timings: digital video timings
1305 static int vpif_s_dv_timings(struct file *file, void *priv,
1306 struct v4l2_dv_timings *timings)
1308 struct vpif_fh *fh = priv;
1309 struct channel_obj *ch = fh->channel;
1310 struct vpif_params *vpifparams = &ch->vpifparams;
1311 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1312 struct video_obj *vid_ch = &ch->video;
1313 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1314 int ret;
1316 if (timings->type != V4L2_DV_BT_656_1120) {
1317 vpif_dbg(2, debug, "Timing type not defined\n");
1318 return -EINVAL;
1321 /* Configure subdevice timings, if any */
1322 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1323 if (ret == -ENOIOCTLCMD) {
1324 vpif_dbg(2, debug, "Custom DV timings not supported by "
1325 "subdevice\n");
1326 return -ENODATA;
1328 if (ret < 0 && ret != -ENODEV) {
1329 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1330 return ret;
1333 if (!(timings->bt.width && timings->bt.height &&
1334 (timings->bt.hbackporch ||
1335 timings->bt.hfrontporch ||
1336 timings->bt.hsync) &&
1337 timings->bt.vfrontporch &&
1338 (timings->bt.vbackporch ||
1339 timings->bt.vsync))) {
1340 vpif_dbg(2, debug, "Timings for width, height, "
1341 "horizontal back porch, horizontal sync, "
1342 "horizontal front porch, vertical back porch, "
1343 "vertical sync and vertical back porch "
1344 "must be defined\n");
1345 return -EINVAL;
1348 vid_ch->dv_timings = *timings;
1350 /* Configure video port timings */
1352 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1353 bt->hsync - 8;
1354 std_info->sav2eav = bt->width;
1356 std_info->l1 = 1;
1357 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1359 if (bt->interlaced) {
1360 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1361 std_info->vsize = bt->height * 2 +
1362 bt->vfrontporch + bt->vsync + bt->vbackporch +
1363 bt->il_vfrontporch + bt->il_vsync +
1364 bt->il_vbackporch;
1365 std_info->l5 = std_info->vsize/2 -
1366 (bt->vfrontporch - 1);
1367 std_info->l7 = std_info->vsize/2 + 1;
1368 std_info->l9 = std_info->l7 + bt->il_vsync +
1369 bt->il_vbackporch + 1;
1370 std_info->l11 = std_info->vsize -
1371 (bt->il_vfrontporch - 1);
1372 } else {
1373 vpif_dbg(2, debug, "Required timing values for "
1374 "interlaced BT format missing\n");
1375 return -EINVAL;
1377 } else {
1378 std_info->vsize = bt->height + bt->vfrontporch +
1379 bt->vsync + bt->vbackporch;
1380 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1382 strncpy(std_info->name, "Custom timings BT656/1120",
1383 VPIF_MAX_NAME);
1384 std_info->width = bt->width;
1385 std_info->height = bt->height;
1386 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1387 std_info->ycmux_mode = 0;
1388 std_info->capture_format = 0;
1389 std_info->vbi_supported = 0;
1390 std_info->hd_sd = 1;
1391 std_info->stdid = 0;
1392 vid_ch->stdid = 0;
1394 return 0;
1398 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1399 * @file: file ptr
1400 * @priv: file handle
1401 * @timings: digital video timings
1403 static int vpif_g_dv_timings(struct file *file, void *priv,
1404 struct v4l2_dv_timings *timings)
1406 struct vpif_fh *fh = priv;
1407 struct channel_obj *ch = fh->channel;
1408 struct video_obj *vid_ch = &ch->video;
1410 *timings = vid_ch->dv_timings;
1412 return 0;
1416 * vpif_g_chip_ident() - Identify the chip
1417 * @file: file ptr
1418 * @priv: file handle
1419 * @chip: chip identity
1421 * Returns zero or -EINVAL if read operations fails.
1423 static int vpif_g_chip_ident(struct file *file, void *priv,
1424 struct v4l2_dbg_chip_ident *chip)
1426 chip->ident = V4L2_IDENT_NONE;
1427 chip->revision = 0;
1428 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1429 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1430 vpif_dbg(2, debug, "match_type is invalid.\n");
1431 return -EINVAL;
1434 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1435 g_chip_ident, chip);
1438 #ifdef CONFIG_VIDEO_ADV_DEBUG
1440 * vpif_dbg_g_register() - Read register
1441 * @file: file ptr
1442 * @priv: file handle
1443 * @reg: register to be read
1445 * Debugging only
1446 * Returns zero or -EINVAL if read operations fails.
1448 static int vpif_dbg_g_register(struct file *file, void *priv,
1449 struct v4l2_dbg_register *reg){
1450 struct vpif_fh *fh = priv;
1451 struct channel_obj *ch = fh->channel;
1453 return v4l2_subdev_call(ch->sd, core, g_register, reg);
1457 * vpif_dbg_s_register() - Write to register
1458 * @file: file ptr
1459 * @priv: file handle
1460 * @reg: register to be modified
1462 * Debugging only
1463 * Returns zero or -EINVAL if write operations fails.
1465 static int vpif_dbg_s_register(struct file *file, void *priv,
1466 struct v4l2_dbg_register *reg){
1467 struct vpif_fh *fh = priv;
1468 struct channel_obj *ch = fh->channel;
1470 return v4l2_subdev_call(ch->sd, core, s_register, reg);
1472 #endif
1475 * vpif_log_status() - Status information
1476 * @file: file ptr
1477 * @priv: file handle
1479 * Returns zero.
1481 static int vpif_log_status(struct file *filep, void *priv)
1483 /* status for sub devices */
1484 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1486 return 0;
1489 /* vpif display ioctl operations */
1490 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1491 .vidioc_querycap = vpif_querycap,
1492 .vidioc_g_priority = vpif_g_priority,
1493 .vidioc_s_priority = vpif_s_priority,
1494 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1495 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1496 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1497 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1498 .vidioc_reqbufs = vpif_reqbufs,
1499 .vidioc_querybuf = vpif_querybuf,
1500 .vidioc_qbuf = vpif_qbuf,
1501 .vidioc_dqbuf = vpif_dqbuf,
1502 .vidioc_streamon = vpif_streamon,
1503 .vidioc_streamoff = vpif_streamoff,
1504 .vidioc_s_std = vpif_s_std,
1505 .vidioc_g_std = vpif_g_std,
1506 .vidioc_enum_output = vpif_enum_output,
1507 .vidioc_s_output = vpif_s_output,
1508 .vidioc_g_output = vpif_g_output,
1509 .vidioc_cropcap = vpif_cropcap,
1510 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1511 .vidioc_s_dv_timings = vpif_s_dv_timings,
1512 .vidioc_g_dv_timings = vpif_g_dv_timings,
1513 .vidioc_g_chip_ident = vpif_g_chip_ident,
1514 #ifdef CONFIG_VIDEO_ADV_DEBUG
1515 .vidioc_g_register = vpif_dbg_g_register,
1516 .vidioc_s_register = vpif_dbg_s_register,
1517 #endif
1518 .vidioc_log_status = vpif_log_status,
1521 static const struct v4l2_file_operations vpif_fops = {
1522 .owner = THIS_MODULE,
1523 .open = vpif_open,
1524 .release = vpif_release,
1525 .unlocked_ioctl = video_ioctl2,
1526 .mmap = vpif_mmap,
1527 .poll = vpif_poll
1530 static struct video_device vpif_video_template = {
1531 .name = "vpif",
1532 .fops = &vpif_fops,
1533 .ioctl_ops = &vpif_ioctl_ops,
1534 .tvnorms = VPIF_V4L2_STD,
1535 .current_norm = V4L2_STD_625_50,
1539 /*Configure the channels, buffer sizei, request irq */
1540 static int initialize_vpif(void)
1542 int free_channel_objects_index;
1543 int free_buffer_channel_index;
1544 int free_buffer_index;
1545 int err = 0, i, j;
1547 /* Default number of buffers should be 3 */
1548 if ((ch2_numbuffers > 0) &&
1549 (ch2_numbuffers < config_params.min_numbuffers))
1550 ch2_numbuffers = config_params.min_numbuffers;
1551 if ((ch3_numbuffers > 0) &&
1552 (ch3_numbuffers < config_params.min_numbuffers))
1553 ch3_numbuffers = config_params.min_numbuffers;
1555 /* Set buffer size to min buffers size if invalid buffer size is
1556 * given */
1557 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1558 ch2_bufsize =
1559 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1560 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1561 ch3_bufsize =
1562 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1564 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1566 if (ch2_numbuffers) {
1567 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1568 ch2_bufsize;
1570 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1572 if (ch3_numbuffers) {
1573 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1574 ch3_bufsize;
1577 /* Allocate memory for six channel objects */
1578 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1579 vpif_obj.dev[i] =
1580 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1581 /* If memory allocation fails, return error */
1582 if (!vpif_obj.dev[i]) {
1583 free_channel_objects_index = i;
1584 err = -ENOMEM;
1585 goto vpif_init_free_channel_objects;
1589 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1590 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1591 free_buffer_index = config_params.numbuffers[i - 1];
1593 return 0;
1595 vpif_init_free_channel_objects:
1596 for (j = 0; j < free_channel_objects_index; j++)
1597 kfree(vpif_obj.dev[j]);
1598 return err;
1602 * vpif_probe: This function creates device entries by register itself to the
1603 * V4L2 driver and initializes fields of each channel objects
1605 static __init int vpif_probe(struct platform_device *pdev)
1607 struct vpif_subdev_info *subdevdata;
1608 struct vpif_display_config *config;
1609 int i, j = 0, k, err = 0;
1610 int res_idx = 0;
1611 struct i2c_adapter *i2c_adap;
1612 struct common_obj *common;
1613 struct channel_obj *ch;
1614 struct video_device *vfd;
1615 struct resource *res;
1616 int subdev_count;
1617 size_t size;
1619 vpif_dev = &pdev->dev;
1620 err = initialize_vpif();
1622 if (err) {
1623 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1624 return err;
1627 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1628 if (err) {
1629 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1630 return err;
1633 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1634 for (i = res->start; i <= res->end; i++) {
1635 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
1636 "VPIF_Display", (void *)
1637 (&vpif_obj.dev[res_idx]->channel_id))) {
1638 err = -EBUSY;
1639 for (j = 0; j < i; j++)
1640 free_irq(j, (void *)
1641 (&vpif_obj.dev[res_idx]->channel_id));
1642 goto vpif_int_err;
1645 res_idx++;
1648 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1649 /* Get the pointer to the channel object */
1650 ch = vpif_obj.dev[i];
1652 /* Allocate memory for video device */
1653 vfd = video_device_alloc();
1654 if (vfd == NULL) {
1655 for (j = 0; j < i; j++) {
1656 ch = vpif_obj.dev[j];
1657 video_device_release(ch->video_dev);
1659 err = -ENOMEM;
1660 goto vpif_int_err;
1663 /* Initialize field of video device */
1664 *vfd = vpif_video_template;
1665 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1666 vfd->release = video_device_release;
1667 vfd->vfl_dir = VFL_DIR_TX;
1668 snprintf(vfd->name, sizeof(vfd->name),
1669 "VPIF_Display_DRIVER_V%s",
1670 VPIF_DISPLAY_VERSION);
1672 /* Set video_dev to the video device */
1673 ch->video_dev = vfd;
1676 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1677 if (res) {
1678 size = resource_size(res);
1679 /* The resources are divided into two equal memory and when
1680 * we have HD output we can add them together
1682 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1683 ch = vpif_obj.dev[j];
1684 ch->channel_id = j;
1686 /* only enabled if second resource exists */
1687 config_params.video_limit[ch->channel_id] = 0;
1688 if (size)
1689 config_params.video_limit[ch->channel_id] =
1690 size/2;
1694 i2c_adap = i2c_get_adapter(1);
1695 config = pdev->dev.platform_data;
1696 subdev_count = config->subdev_count;
1697 subdevdata = config->subdevinfo;
1698 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1699 GFP_KERNEL);
1700 if (vpif_obj.sd == NULL) {
1701 vpif_err("unable to allocate memory for subdevice pointers\n");
1702 err = -ENOMEM;
1703 goto vpif_sd_error;
1706 for (i = 0; i < subdev_count; i++) {
1707 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1708 i2c_adap,
1709 &subdevdata[i].board_info,
1710 NULL);
1711 if (!vpif_obj.sd[i]) {
1712 vpif_err("Error registering v4l2 subdevice\n");
1713 goto probe_subdev_out;
1716 if (vpif_obj.sd[i])
1717 vpif_obj.sd[i]->grp_id = 1 << i;
1720 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1721 ch = vpif_obj.dev[j];
1722 /* Initialize field of the channel objects */
1723 atomic_set(&ch->usrs, 0);
1724 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1725 ch->common[k].numbuffers = 0;
1726 common = &ch->common[k];
1727 common->io_usrs = 0;
1728 common->started = 0;
1729 spin_lock_init(&common->irqlock);
1730 mutex_init(&common->lock);
1731 common->numbuffers = 0;
1732 common->set_addr = NULL;
1733 common->ytop_off = common->ybtm_off = 0;
1734 common->ctop_off = common->cbtm_off = 0;
1735 common->cur_frm = common->next_frm = NULL;
1736 memset(&common->fmt, 0, sizeof(common->fmt));
1737 common->numbuffers = config_params.numbuffers[k];
1740 ch->initialized = 0;
1741 if (subdev_count)
1742 ch->sd = vpif_obj.sd[0];
1743 ch->channel_id = j;
1744 if (j < 2)
1745 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1746 config_params.numbuffers[ch->channel_id];
1747 else
1748 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1750 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1752 /* Initialize prio member of channel object */
1753 v4l2_prio_init(&ch->prio);
1754 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1755 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1756 ch->video_dev->lock = &common->lock;
1757 video_set_drvdata(ch->video_dev, ch);
1759 /* register video device */
1760 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1761 (int)ch, (int)&ch->video_dev);
1763 err = video_register_device(ch->video_dev,
1764 VFL_TYPE_GRABBER, (j ? 3 : 2));
1765 if (err < 0)
1766 goto probe_out;
1769 v4l2_info(&vpif_obj.v4l2_dev,
1770 " VPIF display driver initialized\n");
1771 return 0;
1773 probe_out:
1774 for (k = 0; k < j; k++) {
1775 ch = vpif_obj.dev[k];
1776 video_unregister_device(ch->video_dev);
1777 video_device_release(ch->video_dev);
1778 ch->video_dev = NULL;
1780 probe_subdev_out:
1781 kfree(vpif_obj.sd);
1782 vpif_sd_error:
1783 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1784 ch = vpif_obj.dev[i];
1785 /* Note: does nothing if ch->video_dev == NULL */
1786 video_device_release(ch->video_dev);
1788 vpif_int_err:
1789 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1790 vpif_err("VPIF IRQ request failed\n");
1791 for (i = 0; i < res_idx; i++) {
1792 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1793 for (j = res->start; j <= res->end; j++)
1794 free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
1797 return err;
1801 * vpif_remove: It un-register channels from V4L2 driver
1803 static int vpif_remove(struct platform_device *device)
1805 struct channel_obj *ch;
1806 int i;
1808 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1810 /* un-register device */
1811 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1812 /* Get the pointer to the channel object */
1813 ch = vpif_obj.dev[i];
1814 /* Unregister video device */
1815 video_unregister_device(ch->video_dev);
1817 ch->video_dev = NULL;
1820 return 0;
1823 #ifdef CONFIG_PM
1824 static int vpif_suspend(struct device *dev)
1826 struct common_obj *common;
1827 struct channel_obj *ch;
1828 int i;
1830 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1831 /* Get the pointer to the channel object */
1832 ch = vpif_obj.dev[i];
1833 common = &ch->common[VPIF_VIDEO_INDEX];
1834 mutex_lock(&common->lock);
1835 if (atomic_read(&ch->usrs) && common->io_usrs) {
1836 /* Disable channel */
1837 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1838 enable_channel2(0);
1839 channel2_intr_enable(0);
1841 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1842 common->started == 2) {
1843 enable_channel3(0);
1844 channel3_intr_enable(0);
1847 mutex_unlock(&common->lock);
1850 return 0;
1853 static int vpif_resume(struct device *dev)
1856 struct common_obj *common;
1857 struct channel_obj *ch;
1858 int i;
1860 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1861 /* Get the pointer to the channel object */
1862 ch = vpif_obj.dev[i];
1863 common = &ch->common[VPIF_VIDEO_INDEX];
1864 mutex_lock(&common->lock);
1865 if (atomic_read(&ch->usrs) && common->io_usrs) {
1866 /* Enable channel */
1867 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1868 enable_channel2(1);
1869 channel2_intr_enable(1);
1871 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1872 common->started == 2) {
1873 enable_channel3(1);
1874 channel3_intr_enable(1);
1877 mutex_unlock(&common->lock);
1880 return 0;
1883 static const struct dev_pm_ops vpif_pm = {
1884 .suspend = vpif_suspend,
1885 .resume = vpif_resume,
1888 #define vpif_pm_ops (&vpif_pm)
1889 #else
1890 #define vpif_pm_ops NULL
1891 #endif
1893 static __refdata struct platform_driver vpif_driver = {
1894 .driver = {
1895 .name = "vpif_display",
1896 .owner = THIS_MODULE,
1897 .pm = vpif_pm_ops,
1899 .probe = vpif_probe,
1900 .remove = vpif_remove,
1903 static __init int vpif_init(void)
1905 return platform_driver_register(&vpif_driver);
1909 * vpif_cleanup: This function un-registers device and driver to the kernel,
1910 * frees requested irq handler and de-allocates memory allocated for channel
1911 * objects.
1913 static void vpif_cleanup(void)
1915 struct platform_device *pdev;
1916 struct resource *res;
1917 int irq_num;
1918 int i = 0;
1920 pdev = container_of(vpif_dev, struct platform_device, dev);
1922 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1923 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1924 free_irq(irq_num,
1925 (void *)(&vpif_obj.dev[i]->channel_id));
1926 i++;
1929 platform_driver_unregister(&vpif_driver);
1930 kfree(vpif_obj.sd);
1931 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1932 kfree(vpif_obj.dev[i]);
1935 module_init(vpif_init);
1936 module_exit(vpif_cleanup);