2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
6 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
12 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
23 #include <media/v4l2-ioctl.h>
26 #include "vpif_display.h"
28 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
29 MODULE_LICENSE("GPL");
30 MODULE_VERSION(VPIF_DISPLAY_VERSION);
32 #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
34 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
35 #define vpif_dbg(level, debug, fmt, arg...) \
36 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
40 module_param(debug, int, 0644);
42 MODULE_PARM_DESC(debug, "Debug level 0-1");
44 #define VPIF_DRIVER_NAME "vpif_display"
46 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
47 static int ycmux_mode;
49 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
51 static struct vpif_device vpif_obj = { {NULL} };
52 static struct device *vpif_dev;
53 static void vpif_calculate_offsets(struct channel_obj *ch);
54 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
56 static inline struct vpif_disp_buffer *to_vpif_buffer(struct vb2_buffer *vb)
58 return container_of(vb, struct vpif_disp_buffer, vb);
62 * vpif_buffer_prepare : callback function for buffer prepare
63 * @vb: ptr to vb2_buffer
65 * This is the callback function for buffer prepare when vb2_qbuf()
66 * function is called. The buffer is prepared and user space virtual address
67 * or user address is converted into physical address
69 static int vpif_buffer_prepare(struct vb2_buffer *vb)
71 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
72 struct common_obj *common;
74 common = &ch->common[VPIF_VIDEO_INDEX];
76 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
77 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
80 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
82 if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
83 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
85 if (!ISALIGNED(addr + common->ytop_off) ||
86 !ISALIGNED(addr + common->ybtm_off) ||
87 !ISALIGNED(addr + common->ctop_off) ||
88 !ISALIGNED(addr + common->cbtm_off)) {
89 vpif_err("buffer offset not aligned to 8 bytes\n");
98 * vpif_buffer_queue_setup : Callback function for buffer setup.
101 * @nbuffers: ptr to number of buffers requested by application
102 * @nplanes:: contains number of distinct video planes needed to hold a frame
103 * @sizes[]: contains the size (in bytes) of each plane.
104 * @alloc_ctxs: ptr to allocation context
106 * This callback function is called when reqbuf() is called to adjust
107 * the buffer count and buffer size
109 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
110 const struct v4l2_format *fmt,
111 unsigned int *nbuffers, unsigned int *nplanes,
112 unsigned int sizes[], void *alloc_ctxs[])
114 struct channel_obj *ch = vb2_get_drv_priv(vq);
115 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
117 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
120 if (vq->num_buffers + *nbuffers < 3)
121 *nbuffers = 3 - vq->num_buffers;
124 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
125 alloc_ctxs[0] = common->alloc_ctx;
127 /* Calculate the offset for Y and C data in the buffer */
128 vpif_calculate_offsets(ch);
134 * vpif_buffer_queue : Callback function to add buffer to DMA queue
135 * @vb: ptr to vb2_buffer
137 * This callback fucntion queues the buffer to DMA engine
139 static void vpif_buffer_queue(struct vb2_buffer *vb)
141 struct vpif_disp_buffer *buf = to_vpif_buffer(vb);
142 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
143 struct common_obj *common;
146 common = &ch->common[VPIF_VIDEO_INDEX];
148 /* add the buffer to the DMA queue */
149 spin_lock_irqsave(&common->irqlock, flags);
150 list_add_tail(&buf->list, &common->dma_queue);
151 spin_unlock_irqrestore(&common->irqlock, flags);
155 * vpif_start_streaming : Starts the DMA engine for streaming
156 * @vb: ptr to vb2_buffer
157 * @count: number of buffers
159 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
161 struct vpif_display_config *vpif_config_data =
162 vpif_dev->platform_data;
163 struct channel_obj *ch = vb2_get_drv_priv(vq);
164 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
165 struct vpif_params *vpif = &ch->vpifparams;
166 struct vpif_disp_buffer *buf, *tmp;
167 unsigned long addr, flags;
170 spin_lock_irqsave(&common->irqlock, flags);
172 /* Initialize field_id */
176 if (vpif_config_data->set_clock) {
177 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
178 ycmux_mode, ch->vpifparams.std_info.hd_sd);
180 vpif_err("can't set clock\n");
185 /* set the parameters and addresses */
186 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
191 vpif_config_addr(ch, ret);
192 /* Get the next frame from the buffer queue */
193 common->next_frm = common->cur_frm =
194 list_entry(common->dma_queue.next,
195 struct vpif_disp_buffer, list);
197 list_del(&common->cur_frm->list);
198 spin_unlock_irqrestore(&common->irqlock, flags);
199 /* Mark state of the current frame to active */
200 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
202 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
203 common->set_addr((addr + common->ytop_off),
204 (addr + common->ybtm_off),
205 (addr + common->ctop_off),
206 (addr + common->cbtm_off));
209 * Set interrupt for both the fields in VPIF
210 * Register enable channel in VPIF register
212 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
213 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
214 channel2_intr_assert();
215 channel2_intr_enable(1);
217 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
218 channel2_clipping_enable(1);
221 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
222 channel3_intr_assert();
223 channel3_intr_enable(1);
225 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
226 channel3_clipping_enable(1);
232 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
233 list_del(&buf->list);
234 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
236 spin_unlock_irqrestore(&common->irqlock, flags);
242 * vpif_stop_streaming : Stop the DMA engine
243 * @vq: ptr to vb2_queue
245 * This callback stops the DMA engine and any remaining buffers
246 * in the DMA queue are released.
248 static void vpif_stop_streaming(struct vb2_queue *vq)
250 struct channel_obj *ch = vb2_get_drv_priv(vq);
251 struct common_obj *common;
254 common = &ch->common[VPIF_VIDEO_INDEX];
256 /* Disable channel */
257 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
259 channel2_intr_enable(0);
261 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
263 channel3_intr_enable(0);
266 /* release all active buffers */
267 spin_lock_irqsave(&common->irqlock, flags);
268 if (common->cur_frm == common->next_frm) {
269 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
271 if (common->cur_frm != NULL)
272 vb2_buffer_done(&common->cur_frm->vb,
273 VB2_BUF_STATE_ERROR);
274 if (common->next_frm != NULL)
275 vb2_buffer_done(&common->next_frm->vb,
276 VB2_BUF_STATE_ERROR);
279 while (!list_empty(&common->dma_queue)) {
280 common->next_frm = list_entry(common->dma_queue.next,
281 struct vpif_disp_buffer, list);
282 list_del(&common->next_frm->list);
283 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
285 spin_unlock_irqrestore(&common->irqlock, flags);
288 static struct vb2_ops video_qops = {
289 .queue_setup = vpif_buffer_queue_setup,
290 .wait_prepare = vb2_ops_wait_prepare,
291 .wait_finish = vb2_ops_wait_finish,
292 .buf_prepare = vpif_buffer_prepare,
293 .start_streaming = vpif_start_streaming,
294 .stop_streaming = vpif_stop_streaming,
295 .buf_queue = vpif_buffer_queue,
298 static void process_progressive_mode(struct common_obj *common)
300 unsigned long addr = 0;
302 spin_lock(&common->irqlock);
303 /* Get the next buffer from buffer queue */
304 common->next_frm = list_entry(common->dma_queue.next,
305 struct vpif_disp_buffer, list);
306 /* Remove that buffer from the buffer queue */
307 list_del(&common->next_frm->list);
308 spin_unlock(&common->irqlock);
309 /* Mark status of the buffer as active */
310 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
312 /* Set top and bottom field addrs in VPIF registers */
313 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
314 common->set_addr(addr + common->ytop_off,
315 addr + common->ybtm_off,
316 addr + common->ctop_off,
317 addr + common->cbtm_off);
320 static void process_interlaced_mode(int fid, struct common_obj *common)
322 /* device field id and local field id are in sync */
323 /* If this is even field */
325 if (common->cur_frm == common->next_frm)
328 /* one frame is displayed If next frame is
329 * available, release cur_frm and move on */
330 /* Copy frame display time */
331 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
332 /* Change status of the cur_frm */
333 vb2_buffer_done(&common->cur_frm->vb,
335 /* Make cur_frm pointing to next_frm */
336 common->cur_frm = common->next_frm;
338 } else if (1 == fid) { /* odd field */
339 spin_lock(&common->irqlock);
340 if (list_empty(&common->dma_queue)
341 || (common->cur_frm != common->next_frm)) {
342 spin_unlock(&common->irqlock);
345 spin_unlock(&common->irqlock);
346 /* one field is displayed configure the next
347 * frame if it is available else hold on current
349 /* Get next from the buffer queue */
350 process_progressive_mode(common);
355 * vpif_channel_isr: It changes status of the displayed buffer, takes next
356 * buffer from the queue and sets its address in VPIF registers
358 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
360 struct vpif_device *dev = &vpif_obj;
361 struct channel_obj *ch;
362 struct common_obj *common;
363 enum v4l2_field field;
367 channel_id = *(int *)(dev_id);
368 if (!vpif_intr_status(channel_id + 2))
371 ch = dev->dev[channel_id];
372 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
373 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
374 common = &ch->common[i];
375 /* If streaming is started in this channel */
377 if (1 == ch->vpifparams.std_info.frm_fmt) {
378 spin_lock(&common->irqlock);
379 if (list_empty(&common->dma_queue)) {
380 spin_unlock(&common->irqlock);
383 spin_unlock(&common->irqlock);
385 /* Progressive mode */
386 if (!channel_first_int[i][channel_id]) {
387 /* Mark status of the cur_frm to
388 * done and unlock semaphore on it */
389 v4l2_get_timestamp(&common->cur_frm->vb.
391 vb2_buffer_done(&common->cur_frm->vb,
393 /* Make cur_frm pointing to next_frm */
394 common->cur_frm = common->next_frm;
397 channel_first_int[i][channel_id] = 0;
398 process_progressive_mode(common);
400 /* Interlaced mode */
401 /* If it is first interrupt, ignore it */
403 if (channel_first_int[i][channel_id]) {
404 channel_first_int[i][channel_id] = 0;
410 /* Get field id from VPIF registers */
411 fid = vpif_channel_getfid(ch->channel_id + 2);
412 /* If fid does not match with stored field id */
413 if (fid != ch->field_id) {
414 /* Make them in sync */
421 process_interlaced_mode(fid, common);
428 static int vpif_update_std_info(struct channel_obj *ch)
430 struct video_obj *vid_ch = &ch->video;
431 struct vpif_params *vpifparams = &ch->vpifparams;
432 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
433 const struct vpif_channel_config_params *config;
437 for (i = 0; i < vpif_ch_params_count; i++) {
438 config = &vpif_ch_params[i];
439 if (config->hd_sd == 0) {
440 vpif_dbg(2, debug, "SD format\n");
441 if (config->stdid & vid_ch->stdid) {
442 memcpy(std_info, config, sizeof(*config));
448 if (i == vpif_ch_params_count) {
449 vpif_dbg(1, debug, "Format not found\n");
456 static int vpif_update_resolution(struct channel_obj *ch)
458 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
459 struct video_obj *vid_ch = &ch->video;
460 struct vpif_params *vpifparams = &ch->vpifparams;
461 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
463 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
467 if (vpif_update_std_info(ch))
471 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
472 common->fmt.fmt.pix.width = std_info->width;
473 common->fmt.fmt.pix.height = std_info->height;
474 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
475 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
477 /* Set height and width paramateres */
478 common->height = std_info->height;
479 common->width = std_info->width;
480 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
483 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
485 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
487 if (ch->vpifparams.std_info.frm_fmt)
488 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
490 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
496 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
497 * in the top and bottom field
499 static void vpif_calculate_offsets(struct channel_obj *ch)
501 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
502 struct vpif_params *vpifparams = &ch->vpifparams;
503 enum v4l2_field field = common->fmt.fmt.pix.field;
504 struct video_obj *vid_ch = &ch->video;
505 unsigned int hpitch, vpitch, sizeimage;
507 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
508 if (ch->vpifparams.std_info.frm_fmt)
509 vid_ch->buf_field = V4L2_FIELD_NONE;
511 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
513 vid_ch->buf_field = common->fmt.fmt.pix.field;
516 sizeimage = common->fmt.fmt.pix.sizeimage;
518 hpitch = common->fmt.fmt.pix.bytesperline;
519 vpitch = sizeimage / (hpitch * 2);
520 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
521 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
522 common->ytop_off = 0;
523 common->ybtm_off = hpitch;
524 common->ctop_off = sizeimage / 2;
525 common->cbtm_off = sizeimage / 2 + hpitch;
526 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
527 common->ytop_off = 0;
528 common->ybtm_off = sizeimage / 4;
529 common->ctop_off = sizeimage / 2;
530 common->cbtm_off = common->ctop_off + sizeimage / 4;
531 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
532 common->ybtm_off = 0;
533 common->ytop_off = sizeimage / 4;
534 common->cbtm_off = sizeimage / 2;
535 common->ctop_off = common->cbtm_off + sizeimage / 4;
538 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
539 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
540 vpifparams->video_params.storage_mode = 1;
542 vpifparams->video_params.storage_mode = 0;
545 if (ch->vpifparams.std_info.frm_fmt == 1) {
546 vpifparams->video_params.hpitch =
547 common->fmt.fmt.pix.bytesperline;
549 if ((field == V4L2_FIELD_ANY) ||
550 (field == V4L2_FIELD_INTERLACED))
551 vpifparams->video_params.hpitch =
552 common->fmt.fmt.pix.bytesperline * 2;
554 vpifparams->video_params.hpitch =
555 common->fmt.fmt.pix.bytesperline;
558 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
561 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
563 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
565 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
566 common->set_addr = ch3_set_videobuf_addr;
569 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
571 common->set_addr = ch2_set_videobuf_addr;
575 /* functions implementing ioctls */
577 * vpif_querycap() - QUERYCAP handler
580 * @cap: ptr to v4l2_capability structure
582 static int vpif_querycap(struct file *file, void *priv,
583 struct v4l2_capability *cap)
585 struct vpif_display_config *config = vpif_dev->platform_data;
587 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
588 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
589 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
590 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
592 strlcpy(cap->card, config->card_name, sizeof(cap->card));
597 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
598 struct v4l2_fmtdesc *fmt)
603 /* Fill in the information about format */
604 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
605 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
606 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
611 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
612 struct v4l2_format *fmt)
614 struct video_device *vdev = video_devdata(file);
615 struct channel_obj *ch = video_get_drvdata(vdev);
616 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
618 /* Check the validity of the buffer type */
619 if (common->fmt.type != fmt->type)
622 if (vpif_update_resolution(ch))
628 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
629 struct v4l2_format *fmt)
631 struct video_device *vdev = video_devdata(file);
632 struct channel_obj *ch = video_get_drvdata(vdev);
633 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
634 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
637 * to supress v4l-compliance warnings silently correct
640 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
641 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
643 if (vpif_update_resolution(ch))
646 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
647 pixfmt->field = common->fmt.fmt.pix.field;
648 pixfmt->bytesperline = common->fmt.fmt.pix.width;
649 pixfmt->width = common->fmt.fmt.pix.width;
650 pixfmt->height = common->fmt.fmt.pix.height;
651 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
657 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
658 struct v4l2_format *fmt)
660 struct video_device *vdev = video_devdata(file);
661 struct channel_obj *ch = video_get_drvdata(vdev);
662 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
663 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
666 if (vb2_is_busy(&common->buffer_queue))
669 ret = vpif_try_fmt_vid_out(file, priv, fmt);
673 /* store the pix format in the channel object */
674 common->fmt.fmt.pix = *pixfmt;
676 /* store the format in the channel object */
681 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
683 struct vpif_display_config *config = vpif_dev->platform_data;
684 struct video_device *vdev = video_devdata(file);
685 struct channel_obj *ch = video_get_drvdata(vdev);
686 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
687 struct vpif_display_chan_config *chan_cfg;
688 struct v4l2_output output;
691 if (config->chan_config[ch->channel_id].outputs == NULL)
694 chan_cfg = &config->chan_config[ch->channel_id];
695 output = chan_cfg->outputs[ch->output_idx].output;
696 if (output.capabilities != V4L2_OUT_CAP_STD)
699 if (vb2_is_busy(&common->buffer_queue))
703 if (!(std_id & VPIF_V4L2_STD))
706 /* Call encoder subdevice function to set the standard */
707 ch->video.stdid = std_id;
708 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
709 /* Get the information about the standard */
710 if (vpif_update_resolution(ch))
713 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
715 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
716 s_std_output, std_id);
718 vpif_err("Failed to set output standard\n");
722 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
725 vpif_err("Failed to set standard for sub devices\n");
729 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
731 struct vpif_display_config *config = vpif_dev->platform_data;
732 struct video_device *vdev = video_devdata(file);
733 struct channel_obj *ch = video_get_drvdata(vdev);
734 struct vpif_display_chan_config *chan_cfg;
735 struct v4l2_output output;
737 if (config->chan_config[ch->channel_id].outputs == NULL)
740 chan_cfg = &config->chan_config[ch->channel_id];
741 output = chan_cfg->outputs[ch->output_idx].output;
742 if (output.capabilities != V4L2_OUT_CAP_STD)
745 *std = ch->video.stdid;
749 static int vpif_enum_output(struct file *file, void *fh,
750 struct v4l2_output *output)
753 struct vpif_display_config *config = vpif_dev->platform_data;
754 struct video_device *vdev = video_devdata(file);
755 struct channel_obj *ch = video_get_drvdata(vdev);
756 struct vpif_display_chan_config *chan_cfg;
758 chan_cfg = &config->chan_config[ch->channel_id];
759 if (output->index >= chan_cfg->output_count) {
760 vpif_dbg(1, debug, "Invalid output index\n");
764 *output = chan_cfg->outputs[output->index].output;
769 * vpif_output_to_subdev() - Maps output to sub device
770 * @vpif_cfg - global config ptr
771 * @chan_cfg - channel config ptr
772 * @index - Given output index from application
774 * lookup the sub device information for a given output index.
775 * we report all the output to application. output table also
776 * has sub device name for the each output
779 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
780 struct vpif_display_chan_config *chan_cfg, int index)
782 struct vpif_subdev_info *subdev_info;
783 const char *subdev_name;
786 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
788 if (chan_cfg->outputs == NULL)
791 subdev_name = chan_cfg->outputs[index].subdev_name;
792 if (subdev_name == NULL)
795 /* loop through the sub device list to get the sub device info */
796 for (i = 0; i < vpif_cfg->subdev_count; i++) {
797 subdev_info = &vpif_cfg->subdevinfo[i];
798 if (!strcmp(subdev_info->name, subdev_name))
805 * vpif_set_output() - Select an output
806 * @vpif_cfg - global config ptr
808 * @index - Given output index from application
810 * Select the given output.
812 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
813 struct channel_obj *ch, int index)
815 struct vpif_display_chan_config *chan_cfg =
816 &vpif_cfg->chan_config[ch->channel_id];
817 struct vpif_subdev_info *subdev_info = NULL;
818 struct v4l2_subdev *sd = NULL;
819 u32 input = 0, output = 0;
823 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
825 sd = vpif_obj.sd[sd_index];
826 subdev_info = &vpif_cfg->subdevinfo[sd_index];
830 input = chan_cfg->outputs[index].input_route;
831 output = chan_cfg->outputs[index].output_route;
832 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
833 if (ret < 0 && ret != -ENOIOCTLCMD) {
834 vpif_err("Failed to set output\n");
839 ch->output_idx = index;
841 if (chan_cfg->outputs != NULL)
842 /* update tvnorms from the sub device output info */
843 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
847 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
849 struct vpif_display_config *config = vpif_dev->platform_data;
850 struct video_device *vdev = video_devdata(file);
851 struct channel_obj *ch = video_get_drvdata(vdev);
852 struct vpif_display_chan_config *chan_cfg;
853 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
855 if (vb2_is_busy(&common->buffer_queue))
858 chan_cfg = &config->chan_config[ch->channel_id];
860 if (i >= chan_cfg->output_count)
863 return vpif_set_output(config, ch, i);
866 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
868 struct video_device *vdev = video_devdata(file);
869 struct channel_obj *ch = video_get_drvdata(vdev);
877 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
880 * @timings: input timings
883 vpif_enum_dv_timings(struct file *file, void *priv,
884 struct v4l2_enum_dv_timings *timings)
886 struct vpif_display_config *config = vpif_dev->platform_data;
887 struct video_device *vdev = video_devdata(file);
888 struct channel_obj *ch = video_get_drvdata(vdev);
889 struct vpif_display_chan_config *chan_cfg;
890 struct v4l2_output output;
893 if (config->chan_config[ch->channel_id].outputs == NULL)
896 chan_cfg = &config->chan_config[ch->channel_id];
897 output = chan_cfg->outputs[ch->output_idx].output;
898 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
903 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
904 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
910 * vpif_s_dv_timings() - S_DV_TIMINGS handler
913 * @timings: digital video timings
915 static int vpif_s_dv_timings(struct file *file, void *priv,
916 struct v4l2_dv_timings *timings)
918 struct vpif_display_config *config = vpif_dev->platform_data;
919 struct video_device *vdev = video_devdata(file);
920 struct channel_obj *ch = video_get_drvdata(vdev);
921 struct vpif_params *vpifparams = &ch->vpifparams;
922 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
923 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
924 struct video_obj *vid_ch = &ch->video;
925 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
926 struct vpif_display_chan_config *chan_cfg;
927 struct v4l2_output output;
930 if (config->chan_config[ch->channel_id].outputs == NULL)
933 chan_cfg = &config->chan_config[ch->channel_id];
934 output = chan_cfg->outputs[ch->output_idx].output;
935 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
938 if (vb2_is_busy(&common->buffer_queue))
941 if (timings->type != V4L2_DV_BT_656_1120) {
942 vpif_dbg(2, debug, "Timing type not defined\n");
946 /* Configure subdevice timings, if any */
947 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
948 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
951 vpif_dbg(2, debug, "Error setting custom DV timings\n");
955 if (!(timings->bt.width && timings->bt.height &&
956 (timings->bt.hbackporch ||
957 timings->bt.hfrontporch ||
958 timings->bt.hsync) &&
959 timings->bt.vfrontporch &&
960 (timings->bt.vbackporch ||
961 timings->bt.vsync))) {
962 vpif_dbg(2, debug, "Timings for width, height, "
963 "horizontal back porch, horizontal sync, "
964 "horizontal front porch, vertical back porch, "
965 "vertical sync and vertical back porch "
966 "must be defined\n");
970 vid_ch->dv_timings = *timings;
972 /* Configure video port timings */
974 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
975 std_info->sav2eav = bt->width;
978 std_info->l3 = bt->vsync + bt->vbackporch + 1;
980 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
981 if (bt->interlaced) {
982 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
983 std_info->l5 = std_info->vsize/2 -
984 (bt->vfrontporch - 1);
985 std_info->l7 = std_info->vsize/2 + 1;
986 std_info->l9 = std_info->l7 + bt->il_vsync +
987 bt->il_vbackporch + 1;
988 std_info->l11 = std_info->vsize -
989 (bt->il_vfrontporch - 1);
991 vpif_dbg(2, debug, "Required timing values for "
992 "interlaced BT format missing\n");
996 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
998 strncpy(std_info->name, "Custom timings BT656/1120",
1000 std_info->width = bt->width;
1001 std_info->height = bt->height;
1002 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1003 std_info->ycmux_mode = 0;
1004 std_info->capture_format = 0;
1005 std_info->vbi_supported = 0;
1006 std_info->hd_sd = 1;
1007 std_info->stdid = 0;
1014 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1016 * @priv: file handle
1017 * @timings: digital video timings
1019 static int vpif_g_dv_timings(struct file *file, void *priv,
1020 struct v4l2_dv_timings *timings)
1022 struct vpif_display_config *config = vpif_dev->platform_data;
1023 struct video_device *vdev = video_devdata(file);
1024 struct channel_obj *ch = video_get_drvdata(vdev);
1025 struct vpif_display_chan_config *chan_cfg;
1026 struct video_obj *vid_ch = &ch->video;
1027 struct v4l2_output output;
1029 if (config->chan_config[ch->channel_id].outputs == NULL)
1032 chan_cfg = &config->chan_config[ch->channel_id];
1033 output = chan_cfg->outputs[ch->output_idx].output;
1035 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1038 *timings = vid_ch->dv_timings;
1046 * vpif_log_status() - Status information
1048 * @priv: file handle
1052 static int vpif_log_status(struct file *filep, void *priv)
1054 /* status for sub devices */
1055 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1060 /* vpif display ioctl operations */
1061 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1062 .vidioc_querycap = vpif_querycap,
1063 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1064 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1065 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1066 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1068 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1069 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1070 .vidioc_querybuf = vb2_ioctl_querybuf,
1071 .vidioc_qbuf = vb2_ioctl_qbuf,
1072 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1073 .vidioc_expbuf = vb2_ioctl_expbuf,
1074 .vidioc_streamon = vb2_ioctl_streamon,
1075 .vidioc_streamoff = vb2_ioctl_streamoff,
1077 .vidioc_s_std = vpif_s_std,
1078 .vidioc_g_std = vpif_g_std,
1080 .vidioc_enum_output = vpif_enum_output,
1081 .vidioc_s_output = vpif_s_output,
1082 .vidioc_g_output = vpif_g_output,
1084 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1085 .vidioc_s_dv_timings = vpif_s_dv_timings,
1086 .vidioc_g_dv_timings = vpif_g_dv_timings,
1088 .vidioc_log_status = vpif_log_status,
1091 static const struct v4l2_file_operations vpif_fops = {
1092 .owner = THIS_MODULE,
1093 .open = v4l2_fh_open,
1094 .release = vb2_fop_release,
1095 .unlocked_ioctl = video_ioctl2,
1096 .mmap = vb2_fop_mmap,
1097 .poll = vb2_fop_poll
1100 /*Configure the channels, buffer sizei, request irq */
1101 static int initialize_vpif(void)
1103 int free_channel_objects_index;
1106 /* Allocate memory for six channel objects */
1107 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1109 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1110 /* If memory allocation fails, return error */
1111 if (!vpif_obj.dev[i]) {
1112 free_channel_objects_index = i;
1114 goto vpif_init_free_channel_objects;
1120 vpif_init_free_channel_objects:
1121 for (j = 0; j < free_channel_objects_index; j++)
1122 kfree(vpif_obj.dev[j]);
1126 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1127 struct v4l2_subdev *subdev,
1128 struct v4l2_async_subdev *asd)
1132 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1133 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1135 vpif_obj.sd[i] = subdev;
1136 vpif_obj.sd[i]->grp_id = 1 << i;
1143 static int vpif_probe_complete(void)
1145 struct common_obj *common;
1146 struct video_device *vdev;
1147 struct channel_obj *ch;
1148 struct vb2_queue *q;
1151 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1152 ch = vpif_obj.dev[j];
1153 /* Initialize field of the channel objects */
1154 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1155 common = &ch->common[k];
1156 spin_lock_init(&common->irqlock);
1157 mutex_init(&common->lock);
1158 common->set_addr = NULL;
1159 common->ytop_off = 0;
1160 common->ybtm_off = 0;
1161 common->ctop_off = 0;
1162 common->cbtm_off = 0;
1163 common->cur_frm = NULL;
1164 common->next_frm = NULL;
1165 memset(&common->fmt, 0, sizeof(common->fmt));
1167 ch->initialized = 0;
1168 if (vpif_obj.config->subdev_count)
1169 ch->sd = vpif_obj.sd[0];
1172 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1174 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1175 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1177 /* select output 0 */
1178 err = vpif_set_output(vpif_obj.config, ch, 0);
1182 /* set initial format */
1183 ch->video.stdid = V4L2_STD_525_60;
1184 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1185 vpif_update_resolution(ch);
1187 /* Initialize vb2 queue */
1188 q = &common->buffer_queue;
1189 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1190 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1192 q->ops = &video_qops;
1193 q->mem_ops = &vb2_dma_contig_memops;
1194 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1195 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1196 q->min_buffers_needed = 1;
1197 q->lock = &common->lock;
1198 err = vb2_queue_init(q);
1200 vpif_err("vpif_display: vb2_queue_init() failed\n");
1204 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1205 if (IS_ERR(common->alloc_ctx)) {
1206 vpif_err("Failed to get the context\n");
1207 err = PTR_ERR(common->alloc_ctx);
1211 INIT_LIST_HEAD(&common->dma_queue);
1213 /* register video device */
1214 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1215 (int)ch, (int)&ch->video_dev);
1217 /* Initialize the video_device structure */
1218 vdev = ch->video_dev;
1219 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1220 vdev->release = video_device_release;
1221 vdev->fops = &vpif_fops;
1222 vdev->ioctl_ops = &vpif_ioctl_ops;
1223 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1224 vdev->vfl_dir = VFL_DIR_TX;
1226 vdev->lock = &common->lock;
1227 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1228 video_set_drvdata(ch->video_dev, ch);
1229 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1238 for (k = 0; k < j; k++) {
1239 ch = vpif_obj.dev[k];
1240 common = &ch->common[k];
1241 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1242 video_unregister_device(ch->video_dev);
1243 video_device_release(ch->video_dev);
1244 ch->video_dev = NULL;
1249 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1251 return vpif_probe_complete();
1255 * vpif_probe: This function creates device entries by register itself to the
1256 * V4L2 driver and initializes fields of each channel objects
1258 static __init int vpif_probe(struct platform_device *pdev)
1260 struct vpif_subdev_info *subdevdata;
1261 int i, j = 0, err = 0;
1263 struct i2c_adapter *i2c_adap;
1264 struct channel_obj *ch;
1265 struct video_device *vfd;
1266 struct resource *res;
1269 vpif_dev = &pdev->dev;
1270 err = initialize_vpif();
1273 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1277 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1279 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1283 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1284 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1285 IRQF_SHARED, VPIF_DRIVER_NAME,
1286 (void *)(&vpif_obj.dev[res_idx]->
1290 vpif_err("VPIF IRQ request failed\n");
1291 goto vpif_unregister;
1296 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1297 /* Get the pointer to the channel object */
1298 ch = vpif_obj.dev[i];
1300 /* Allocate memory for video device */
1301 vfd = video_device_alloc();
1303 for (j = 0; j < i; j++) {
1304 ch = vpif_obj.dev[j];
1305 video_device_release(ch->video_dev);
1308 goto vpif_unregister;
1311 /* Set video_dev to the video device */
1312 ch->video_dev = vfd;
1315 vpif_obj.config = pdev->dev.platform_data;
1316 subdev_count = vpif_obj.config->subdev_count;
1317 subdevdata = vpif_obj.config->subdevinfo;
1318 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1320 if (vpif_obj.sd == NULL) {
1321 vpif_err("unable to allocate memory for subdevice pointers\n");
1326 if (!vpif_obj.config->asd_sizes) {
1327 i2c_adap = i2c_get_adapter(1);
1328 for (i = 0; i < subdev_count; i++) {
1330 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1335 if (!vpif_obj.sd[i]) {
1336 vpif_err("Error registering v4l2 subdevice\n");
1338 goto probe_subdev_out;
1342 vpif_obj.sd[i]->grp_id = 1 << i;
1344 vpif_probe_complete();
1346 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1347 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1348 vpif_obj.notifier.bound = vpif_async_bound;
1349 vpif_obj.notifier.complete = vpif_async_complete;
1350 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1351 &vpif_obj.notifier);
1353 vpif_err("Error registering async notifier\n");
1355 goto probe_subdev_out;
1364 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1365 ch = vpif_obj.dev[i];
1366 /* Note: does nothing if ch->video_dev == NULL */
1367 video_device_release(ch->video_dev);
1370 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1376 * vpif_remove: It un-register channels from V4L2 driver
1378 static int vpif_remove(struct platform_device *device)
1380 struct common_obj *common;
1381 struct channel_obj *ch;
1384 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1387 /* un-register device */
1388 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1389 /* Get the pointer to the channel object */
1390 ch = vpif_obj.dev[i];
1391 common = &ch->common[i];
1392 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1393 /* Unregister video device */
1394 video_unregister_device(ch->video_dev);
1396 ch->video_dev = NULL;
1397 kfree(vpif_obj.dev[i]);
1403 #ifdef CONFIG_PM_SLEEP
1404 static int vpif_suspend(struct device *dev)
1406 struct common_obj *common;
1407 struct channel_obj *ch;
1410 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1411 /* Get the pointer to the channel object */
1412 ch = vpif_obj.dev[i];
1413 common = &ch->common[VPIF_VIDEO_INDEX];
1415 if (!vb2_is_streaming(&common->buffer_queue))
1418 mutex_lock(&common->lock);
1419 /* Disable channel */
1420 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1422 channel2_intr_enable(0);
1424 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1427 channel3_intr_enable(0);
1429 mutex_unlock(&common->lock);
1435 static int vpif_resume(struct device *dev)
1438 struct common_obj *common;
1439 struct channel_obj *ch;
1442 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1443 /* Get the pointer to the channel object */
1444 ch = vpif_obj.dev[i];
1445 common = &ch->common[VPIF_VIDEO_INDEX];
1447 if (!vb2_is_streaming(&common->buffer_queue))
1450 mutex_lock(&common->lock);
1451 /* Enable channel */
1452 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1454 channel2_intr_enable(1);
1456 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1459 channel3_intr_enable(1);
1461 mutex_unlock(&common->lock);
1469 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1471 static __refdata struct platform_driver vpif_driver = {
1473 .name = VPIF_DRIVER_NAME,
1474 .owner = THIS_MODULE,
1477 .probe = vpif_probe,
1478 .remove = vpif_remove,
1481 module_platform_driver(vpif_driver);