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);
57 struct vpif_disp_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
59 return container_of(vb, struct vpif_disp_buffer, vb);
63 * vpif_buffer_prepare : callback function for buffer prepare
64 * @vb: ptr to vb2_buffer
66 * This is the callback function for buffer prepare when vb2_qbuf()
67 * function is called. The buffer is prepared and user space virtual address
68 * or user address is converted into physical address
70 static int vpif_buffer_prepare(struct vb2_buffer *vb)
72 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
73 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
74 struct common_obj *common;
76 common = &ch->common[VPIF_VIDEO_INDEX];
78 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
79 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
82 vbuf->field = common->fmt.fmt.pix.field;
84 if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
85 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
87 if (!ISALIGNED(addr + common->ytop_off) ||
88 !ISALIGNED(addr + common->ybtm_off) ||
89 !ISALIGNED(addr + common->ctop_off) ||
90 !ISALIGNED(addr + common->cbtm_off)) {
91 vpif_err("buffer offset not aligned to 8 bytes\n");
100 * vpif_buffer_queue_setup : Callback function for buffer setup.
103 * @nbuffers: ptr to number of buffers requested by application
104 * @nplanes:: contains number of distinct video planes needed to hold a frame
105 * @sizes[]: contains the size (in bytes) of each plane.
106 * @alloc_ctxs: ptr to allocation context
108 * This callback function is called when reqbuf() is called to adjust
109 * the buffer count and buffer size
111 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
112 const struct v4l2_format *fmt,
113 unsigned int *nbuffers, unsigned int *nplanes,
114 unsigned int sizes[], void *alloc_ctxs[])
116 struct channel_obj *ch = vb2_get_drv_priv(vq);
117 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
119 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
122 if (vq->num_buffers + *nbuffers < 3)
123 *nbuffers = 3 - vq->num_buffers;
126 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
127 alloc_ctxs[0] = common->alloc_ctx;
129 /* Calculate the offset for Y and C data in the buffer */
130 vpif_calculate_offsets(ch);
136 * vpif_buffer_queue : Callback function to add buffer to DMA queue
137 * @vb: ptr to vb2_buffer
139 * This callback fucntion queues the buffer to DMA engine
141 static void vpif_buffer_queue(struct vb2_buffer *vb)
143 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
144 struct vpif_disp_buffer *buf = to_vpif_buffer(vbuf);
145 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
146 struct common_obj *common;
149 common = &ch->common[VPIF_VIDEO_INDEX];
151 /* add the buffer to the DMA queue */
152 spin_lock_irqsave(&common->irqlock, flags);
153 list_add_tail(&buf->list, &common->dma_queue);
154 spin_unlock_irqrestore(&common->irqlock, flags);
158 * vpif_start_streaming : Starts the DMA engine for streaming
159 * @vb: ptr to vb2_buffer
160 * @count: number of buffers
162 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
164 struct vpif_display_config *vpif_config_data =
165 vpif_dev->platform_data;
166 struct channel_obj *ch = vb2_get_drv_priv(vq);
167 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
168 struct vpif_params *vpif = &ch->vpifparams;
169 struct vpif_disp_buffer *buf, *tmp;
170 unsigned long addr, flags;
173 spin_lock_irqsave(&common->irqlock, flags);
175 /* Initialize field_id */
179 if (vpif_config_data->set_clock) {
180 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
181 ycmux_mode, ch->vpifparams.std_info.hd_sd);
183 vpif_err("can't set clock\n");
188 /* set the parameters and addresses */
189 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
194 vpif_config_addr(ch, ret);
195 /* Get the next frame from the buffer queue */
196 common->next_frm = common->cur_frm =
197 list_entry(common->dma_queue.next,
198 struct vpif_disp_buffer, list);
200 list_del(&common->cur_frm->list);
201 spin_unlock_irqrestore(&common->irqlock, flags);
203 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
204 common->set_addr((addr + common->ytop_off),
205 (addr + common->ybtm_off),
206 (addr + common->ctop_off),
207 (addr + common->cbtm_off));
210 * Set interrupt for both the fields in VPIF
211 * Register enable channel in VPIF register
213 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
214 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
215 channel2_intr_assert();
216 channel2_intr_enable(1);
218 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
219 channel2_clipping_enable(1);
222 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
223 channel3_intr_assert();
224 channel3_intr_enable(1);
226 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
227 channel3_clipping_enable(1);
233 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
234 list_del(&buf->list);
235 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
237 spin_unlock_irqrestore(&common->irqlock, flags);
243 * vpif_stop_streaming : Stop the DMA engine
244 * @vq: ptr to vb2_queue
246 * This callback stops the DMA engine and any remaining buffers
247 * in the DMA queue are released.
249 static void vpif_stop_streaming(struct vb2_queue *vq)
251 struct channel_obj *ch = vb2_get_drv_priv(vq);
252 struct common_obj *common;
255 common = &ch->common[VPIF_VIDEO_INDEX];
257 /* Disable channel */
258 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
260 channel2_intr_enable(0);
262 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
264 channel3_intr_enable(0);
267 /* release all active buffers */
268 spin_lock_irqsave(&common->irqlock, flags);
269 if (common->cur_frm == common->next_frm) {
270 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
271 VB2_BUF_STATE_ERROR);
273 if (common->cur_frm != NULL)
274 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
275 VB2_BUF_STATE_ERROR);
276 if (common->next_frm != NULL)
277 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
278 VB2_BUF_STATE_ERROR);
281 while (!list_empty(&common->dma_queue)) {
282 common->next_frm = list_entry(common->dma_queue.next,
283 struct vpif_disp_buffer, list);
284 list_del(&common->next_frm->list);
285 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
286 VB2_BUF_STATE_ERROR);
288 spin_unlock_irqrestore(&common->irqlock, flags);
291 static struct vb2_ops video_qops = {
292 .queue_setup = vpif_buffer_queue_setup,
293 .wait_prepare = vb2_ops_wait_prepare,
294 .wait_finish = vb2_ops_wait_finish,
295 .buf_prepare = vpif_buffer_prepare,
296 .start_streaming = vpif_start_streaming,
297 .stop_streaming = vpif_stop_streaming,
298 .buf_queue = vpif_buffer_queue,
301 static void process_progressive_mode(struct common_obj *common)
303 unsigned long addr = 0;
305 spin_lock(&common->irqlock);
306 /* Get the next buffer from buffer queue */
307 common->next_frm = list_entry(common->dma_queue.next,
308 struct vpif_disp_buffer, list);
309 /* Remove that buffer from the buffer queue */
310 list_del(&common->next_frm->list);
311 spin_unlock(&common->irqlock);
313 /* Set top and bottom field addrs in VPIF registers */
314 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
315 common->set_addr(addr + common->ytop_off,
316 addr + common->ybtm_off,
317 addr + common->ctop_off,
318 addr + common->cbtm_off);
321 static void process_interlaced_mode(int fid, struct common_obj *common)
323 /* device field id and local field id are in sync */
324 /* If this is even field */
326 if (common->cur_frm == common->next_frm)
329 /* one frame is displayed If next frame is
330 * available, release cur_frm and move on */
331 /* Copy frame display time */
332 v4l2_get_timestamp(&common->cur_frm->vb.timestamp);
333 /* Change status of the cur_frm */
334 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
336 /* Make cur_frm pointing to next_frm */
337 common->cur_frm = common->next_frm;
339 } else if (1 == fid) { /* odd field */
340 spin_lock(&common->irqlock);
341 if (list_empty(&common->dma_queue)
342 || (common->cur_frm != common->next_frm)) {
343 spin_unlock(&common->irqlock);
346 spin_unlock(&common->irqlock);
347 /* one field is displayed configure the next
348 * frame if it is available else hold on current
350 /* Get next from the buffer queue */
351 process_progressive_mode(common);
356 * vpif_channel_isr: It changes status of the displayed buffer, takes next
357 * buffer from the queue and sets its address in VPIF registers
359 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
361 struct vpif_device *dev = &vpif_obj;
362 struct channel_obj *ch;
363 struct common_obj *common;
367 channel_id = *(int *)(dev_id);
368 if (!vpif_intr_status(channel_id + 2))
371 ch = dev->dev[channel_id];
372 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
373 common = &ch->common[i];
374 /* If streaming is started in this channel */
376 if (1 == ch->vpifparams.std_info.frm_fmt) {
377 spin_lock(&common->irqlock);
378 if (list_empty(&common->dma_queue)) {
379 spin_unlock(&common->irqlock);
382 spin_unlock(&common->irqlock);
384 /* Progressive mode */
385 if (!channel_first_int[i][channel_id]) {
386 /* Mark status of the cur_frm to
387 * done and unlock semaphore on it */
389 &common->cur_frm->vb.timestamp);
390 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
392 /* Make cur_frm pointing to next_frm */
393 common->cur_frm = common->next_frm;
396 channel_first_int[i][channel_id] = 0;
397 process_progressive_mode(common);
399 /* Interlaced mode */
400 /* If it is first interrupt, ignore it */
402 if (channel_first_int[i][channel_id]) {
403 channel_first_int[i][channel_id] = 0;
409 /* Get field id from VPIF registers */
410 fid = vpif_channel_getfid(ch->channel_id + 2);
411 /* If fid does not match with stored field id */
412 if (fid != ch->field_id) {
413 /* Make them in sync */
420 process_interlaced_mode(fid, common);
427 static int vpif_update_std_info(struct channel_obj *ch)
429 struct video_obj *vid_ch = &ch->video;
430 struct vpif_params *vpifparams = &ch->vpifparams;
431 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
432 const struct vpif_channel_config_params *config;
436 for (i = 0; i < vpif_ch_params_count; i++) {
437 config = &vpif_ch_params[i];
438 if (config->hd_sd == 0) {
439 vpif_dbg(2, debug, "SD format\n");
440 if (config->stdid & vid_ch->stdid) {
441 memcpy(std_info, config, sizeof(*config));
447 if (i == vpif_ch_params_count) {
448 vpif_dbg(1, debug, "Format not found\n");
455 static int vpif_update_resolution(struct channel_obj *ch)
457 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
458 struct video_obj *vid_ch = &ch->video;
459 struct vpif_params *vpifparams = &ch->vpifparams;
460 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
462 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
466 if (vpif_update_std_info(ch))
470 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
471 common->fmt.fmt.pix.width = std_info->width;
472 common->fmt.fmt.pix.height = std_info->height;
473 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
474 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
476 /* Set height and width paramateres */
477 common->height = std_info->height;
478 common->width = std_info->width;
479 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
482 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
484 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
486 if (ch->vpifparams.std_info.frm_fmt)
487 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
489 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
495 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
496 * in the top and bottom field
498 static void vpif_calculate_offsets(struct channel_obj *ch)
500 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
501 struct vpif_params *vpifparams = &ch->vpifparams;
502 enum v4l2_field field = common->fmt.fmt.pix.field;
503 struct video_obj *vid_ch = &ch->video;
504 unsigned int hpitch, sizeimage;
506 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
507 if (ch->vpifparams.std_info.frm_fmt)
508 vid_ch->buf_field = V4L2_FIELD_NONE;
510 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
512 vid_ch->buf_field = common->fmt.fmt.pix.field;
515 sizeimage = common->fmt.fmt.pix.sizeimage;
517 hpitch = common->fmt.fmt.pix.bytesperline;
518 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
519 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
520 common->ytop_off = 0;
521 common->ybtm_off = hpitch;
522 common->ctop_off = sizeimage / 2;
523 common->cbtm_off = sizeimage / 2 + hpitch;
524 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
525 common->ytop_off = 0;
526 common->ybtm_off = sizeimage / 4;
527 common->ctop_off = sizeimage / 2;
528 common->cbtm_off = common->ctop_off + sizeimage / 4;
529 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
530 common->ybtm_off = 0;
531 common->ytop_off = sizeimage / 4;
532 common->cbtm_off = sizeimage / 2;
533 common->ctop_off = common->cbtm_off + sizeimage / 4;
536 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
537 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
538 vpifparams->video_params.storage_mode = 1;
540 vpifparams->video_params.storage_mode = 0;
543 if (ch->vpifparams.std_info.frm_fmt == 1) {
544 vpifparams->video_params.hpitch =
545 common->fmt.fmt.pix.bytesperline;
547 if ((field == V4L2_FIELD_ANY) ||
548 (field == V4L2_FIELD_INTERLACED))
549 vpifparams->video_params.hpitch =
550 common->fmt.fmt.pix.bytesperline * 2;
552 vpifparams->video_params.hpitch =
553 common->fmt.fmt.pix.bytesperline;
556 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
559 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
561 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
563 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
564 common->set_addr = ch3_set_videobuf_addr;
567 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
569 common->set_addr = ch2_set_videobuf_addr;
573 /* functions implementing ioctls */
575 * vpif_querycap() - QUERYCAP handler
578 * @cap: ptr to v4l2_capability structure
580 static int vpif_querycap(struct file *file, void *priv,
581 struct v4l2_capability *cap)
583 struct vpif_display_config *config = vpif_dev->platform_data;
585 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
586 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
587 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
588 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
590 strlcpy(cap->card, config->card_name, sizeof(cap->card));
595 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
596 struct v4l2_fmtdesc *fmt)
601 /* Fill in the information about format */
602 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
603 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
604 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
609 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
610 struct v4l2_format *fmt)
612 struct video_device *vdev = video_devdata(file);
613 struct channel_obj *ch = video_get_drvdata(vdev);
614 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
616 /* Check the validity of the buffer type */
617 if (common->fmt.type != fmt->type)
620 if (vpif_update_resolution(ch))
626 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
627 struct v4l2_format *fmt)
629 struct video_device *vdev = video_devdata(file);
630 struct channel_obj *ch = video_get_drvdata(vdev);
631 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
632 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
635 * to supress v4l-compliance warnings silently correct
638 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
639 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
641 if (vpif_update_resolution(ch))
644 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
645 pixfmt->field = common->fmt.fmt.pix.field;
646 pixfmt->bytesperline = common->fmt.fmt.pix.width;
647 pixfmt->width = common->fmt.fmt.pix.width;
648 pixfmt->height = common->fmt.fmt.pix.height;
649 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
654 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
655 struct v4l2_format *fmt)
657 struct video_device *vdev = video_devdata(file);
658 struct channel_obj *ch = video_get_drvdata(vdev);
659 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
660 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
663 if (vb2_is_busy(&common->buffer_queue))
666 ret = vpif_try_fmt_vid_out(file, priv, fmt);
670 /* store the pix format in the channel object */
671 common->fmt.fmt.pix = *pixfmt;
673 /* store the format in the channel object */
678 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
680 struct vpif_display_config *config = vpif_dev->platform_data;
681 struct video_device *vdev = video_devdata(file);
682 struct channel_obj *ch = video_get_drvdata(vdev);
683 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
684 struct vpif_display_chan_config *chan_cfg;
685 struct v4l2_output output;
688 if (config->chan_config[ch->channel_id].outputs == NULL)
691 chan_cfg = &config->chan_config[ch->channel_id];
692 output = chan_cfg->outputs[ch->output_idx].output;
693 if (output.capabilities != V4L2_OUT_CAP_STD)
696 if (vb2_is_busy(&common->buffer_queue))
700 if (!(std_id & VPIF_V4L2_STD))
703 /* Call encoder subdevice function to set the standard */
704 ch->video.stdid = std_id;
705 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
706 /* Get the information about the standard */
707 if (vpif_update_resolution(ch))
710 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
712 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
713 s_std_output, std_id);
715 vpif_err("Failed to set output standard\n");
719 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
722 vpif_err("Failed to set standard for sub devices\n");
726 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
728 struct vpif_display_config *config = vpif_dev->platform_data;
729 struct video_device *vdev = video_devdata(file);
730 struct channel_obj *ch = video_get_drvdata(vdev);
731 struct vpif_display_chan_config *chan_cfg;
732 struct v4l2_output output;
734 if (config->chan_config[ch->channel_id].outputs == NULL)
737 chan_cfg = &config->chan_config[ch->channel_id];
738 output = chan_cfg->outputs[ch->output_idx].output;
739 if (output.capabilities != V4L2_OUT_CAP_STD)
742 *std = ch->video.stdid;
746 static int vpif_enum_output(struct file *file, void *fh,
747 struct v4l2_output *output)
750 struct vpif_display_config *config = vpif_dev->platform_data;
751 struct video_device *vdev = video_devdata(file);
752 struct channel_obj *ch = video_get_drvdata(vdev);
753 struct vpif_display_chan_config *chan_cfg;
755 chan_cfg = &config->chan_config[ch->channel_id];
756 if (output->index >= chan_cfg->output_count) {
757 vpif_dbg(1, debug, "Invalid output index\n");
761 *output = chan_cfg->outputs[output->index].output;
766 * vpif_output_to_subdev() - Maps output to sub device
767 * @vpif_cfg - global config ptr
768 * @chan_cfg - channel config ptr
769 * @index - Given output index from application
771 * lookup the sub device information for a given output index.
772 * we report all the output to application. output table also
773 * has sub device name for the each output
776 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
777 struct vpif_display_chan_config *chan_cfg, int index)
779 struct vpif_subdev_info *subdev_info;
780 const char *subdev_name;
783 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
785 if (chan_cfg->outputs == NULL)
788 subdev_name = chan_cfg->outputs[index].subdev_name;
789 if (subdev_name == NULL)
792 /* loop through the sub device list to get the sub device info */
793 for (i = 0; i < vpif_cfg->subdev_count; i++) {
794 subdev_info = &vpif_cfg->subdevinfo[i];
795 if (!strcmp(subdev_info->name, subdev_name))
802 * vpif_set_output() - Select an output
803 * @vpif_cfg - global config ptr
805 * @index - Given output index from application
807 * Select the given output.
809 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
810 struct channel_obj *ch, int index)
812 struct vpif_display_chan_config *chan_cfg =
813 &vpif_cfg->chan_config[ch->channel_id];
814 struct v4l2_subdev *sd = NULL;
815 u32 input = 0, output = 0;
819 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
821 sd = vpif_obj.sd[sd_index];
824 input = chan_cfg->outputs[index].input_route;
825 output = chan_cfg->outputs[index].output_route;
826 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
827 if (ret < 0 && ret != -ENOIOCTLCMD) {
828 vpif_err("Failed to set output\n");
833 ch->output_idx = index;
835 if (chan_cfg->outputs != NULL)
836 /* update tvnorms from the sub device output info */
837 ch->video_dev.tvnorms = chan_cfg->outputs[index].output.std;
841 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
843 struct vpif_display_config *config = vpif_dev->platform_data;
844 struct video_device *vdev = video_devdata(file);
845 struct channel_obj *ch = video_get_drvdata(vdev);
846 struct vpif_display_chan_config *chan_cfg;
847 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
849 if (vb2_is_busy(&common->buffer_queue))
852 chan_cfg = &config->chan_config[ch->channel_id];
854 if (i >= chan_cfg->output_count)
857 return vpif_set_output(config, ch, i);
860 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
862 struct video_device *vdev = video_devdata(file);
863 struct channel_obj *ch = video_get_drvdata(vdev);
871 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
874 * @timings: input timings
877 vpif_enum_dv_timings(struct file *file, void *priv,
878 struct v4l2_enum_dv_timings *timings)
880 struct vpif_display_config *config = vpif_dev->platform_data;
881 struct video_device *vdev = video_devdata(file);
882 struct channel_obj *ch = video_get_drvdata(vdev);
883 struct vpif_display_chan_config *chan_cfg;
884 struct v4l2_output output;
887 if (config->chan_config[ch->channel_id].outputs == NULL)
890 chan_cfg = &config->chan_config[ch->channel_id];
891 output = chan_cfg->outputs[ch->output_idx].output;
892 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
897 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
898 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
904 * vpif_s_dv_timings() - S_DV_TIMINGS handler
907 * @timings: digital video timings
909 static int vpif_s_dv_timings(struct file *file, void *priv,
910 struct v4l2_dv_timings *timings)
912 struct vpif_display_config *config = vpif_dev->platform_data;
913 struct video_device *vdev = video_devdata(file);
914 struct channel_obj *ch = video_get_drvdata(vdev);
915 struct vpif_params *vpifparams = &ch->vpifparams;
916 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
917 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
918 struct video_obj *vid_ch = &ch->video;
919 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
920 struct vpif_display_chan_config *chan_cfg;
921 struct v4l2_output output;
924 if (config->chan_config[ch->channel_id].outputs == NULL)
927 chan_cfg = &config->chan_config[ch->channel_id];
928 output = chan_cfg->outputs[ch->output_idx].output;
929 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
932 if (vb2_is_busy(&common->buffer_queue))
935 if (timings->type != V4L2_DV_BT_656_1120) {
936 vpif_dbg(2, debug, "Timing type not defined\n");
940 /* Configure subdevice timings, if any */
941 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
942 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
945 vpif_dbg(2, debug, "Error setting custom DV timings\n");
949 if (!(timings->bt.width && timings->bt.height &&
950 (timings->bt.hbackporch ||
951 timings->bt.hfrontporch ||
952 timings->bt.hsync) &&
953 timings->bt.vfrontporch &&
954 (timings->bt.vbackporch ||
955 timings->bt.vsync))) {
956 vpif_dbg(2, debug, "Timings for width, height, "
957 "horizontal back porch, horizontal sync, "
958 "horizontal front porch, vertical back porch, "
959 "vertical sync and vertical back porch "
960 "must be defined\n");
964 vid_ch->dv_timings = *timings;
966 /* Configure video port timings */
968 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
969 std_info->sav2eav = bt->width;
972 std_info->l3 = bt->vsync + bt->vbackporch + 1;
974 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
975 if (bt->interlaced) {
976 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
977 std_info->l5 = std_info->vsize/2 -
978 (bt->vfrontporch - 1);
979 std_info->l7 = std_info->vsize/2 + 1;
980 std_info->l9 = std_info->l7 + bt->il_vsync +
981 bt->il_vbackporch + 1;
982 std_info->l11 = std_info->vsize -
983 (bt->il_vfrontporch - 1);
985 vpif_dbg(2, debug, "Required timing values for "
986 "interlaced BT format missing\n");
990 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
992 strncpy(std_info->name, "Custom timings BT656/1120",
994 std_info->width = bt->width;
995 std_info->height = bt->height;
996 std_info->frm_fmt = bt->interlaced ? 0 : 1;
997 std_info->ycmux_mode = 0;
998 std_info->capture_format = 0;
999 std_info->vbi_supported = 0;
1000 std_info->hd_sd = 1;
1001 std_info->stdid = 0;
1008 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1010 * @priv: file handle
1011 * @timings: digital video timings
1013 static int vpif_g_dv_timings(struct file *file, void *priv,
1014 struct v4l2_dv_timings *timings)
1016 struct vpif_display_config *config = vpif_dev->platform_data;
1017 struct video_device *vdev = video_devdata(file);
1018 struct channel_obj *ch = video_get_drvdata(vdev);
1019 struct vpif_display_chan_config *chan_cfg;
1020 struct video_obj *vid_ch = &ch->video;
1021 struct v4l2_output output;
1023 if (config->chan_config[ch->channel_id].outputs == NULL)
1026 chan_cfg = &config->chan_config[ch->channel_id];
1027 output = chan_cfg->outputs[ch->output_idx].output;
1029 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1032 *timings = vid_ch->dv_timings;
1040 * vpif_log_status() - Status information
1042 * @priv: file handle
1046 static int vpif_log_status(struct file *filep, void *priv)
1048 /* status for sub devices */
1049 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1054 /* vpif display ioctl operations */
1055 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1056 .vidioc_querycap = vpif_querycap,
1057 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1058 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1059 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1060 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1062 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1063 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1064 .vidioc_querybuf = vb2_ioctl_querybuf,
1065 .vidioc_qbuf = vb2_ioctl_qbuf,
1066 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1067 .vidioc_expbuf = vb2_ioctl_expbuf,
1068 .vidioc_streamon = vb2_ioctl_streamon,
1069 .vidioc_streamoff = vb2_ioctl_streamoff,
1071 .vidioc_s_std = vpif_s_std,
1072 .vidioc_g_std = vpif_g_std,
1074 .vidioc_enum_output = vpif_enum_output,
1075 .vidioc_s_output = vpif_s_output,
1076 .vidioc_g_output = vpif_g_output,
1078 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1079 .vidioc_s_dv_timings = vpif_s_dv_timings,
1080 .vidioc_g_dv_timings = vpif_g_dv_timings,
1082 .vidioc_log_status = vpif_log_status,
1085 static const struct v4l2_file_operations vpif_fops = {
1086 .owner = THIS_MODULE,
1087 .open = v4l2_fh_open,
1088 .release = vb2_fop_release,
1089 .unlocked_ioctl = video_ioctl2,
1090 .mmap = vb2_fop_mmap,
1091 .poll = vb2_fop_poll
1094 /*Configure the channels, buffer sizei, request irq */
1095 static int initialize_vpif(void)
1097 int free_channel_objects_index;
1100 /* Allocate memory for six channel objects */
1101 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1103 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1104 /* If memory allocation fails, return error */
1105 if (!vpif_obj.dev[i]) {
1106 free_channel_objects_index = i;
1108 goto vpif_init_free_channel_objects;
1114 vpif_init_free_channel_objects:
1115 for (j = 0; j < free_channel_objects_index; j++)
1116 kfree(vpif_obj.dev[j]);
1120 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1121 struct v4l2_subdev *subdev,
1122 struct v4l2_async_subdev *asd)
1126 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1127 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1129 vpif_obj.sd[i] = subdev;
1130 vpif_obj.sd[i]->grp_id = 1 << i;
1137 static int vpif_probe_complete(void)
1139 struct common_obj *common;
1140 struct video_device *vdev;
1141 struct channel_obj *ch;
1142 struct vb2_queue *q;
1145 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1146 ch = vpif_obj.dev[j];
1147 /* Initialize field of the channel objects */
1148 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1149 common = &ch->common[k];
1150 spin_lock_init(&common->irqlock);
1151 mutex_init(&common->lock);
1152 common->set_addr = NULL;
1153 common->ytop_off = 0;
1154 common->ybtm_off = 0;
1155 common->ctop_off = 0;
1156 common->cbtm_off = 0;
1157 common->cur_frm = NULL;
1158 common->next_frm = NULL;
1159 memset(&common->fmt, 0, sizeof(common->fmt));
1161 ch->initialized = 0;
1162 if (vpif_obj.config->subdev_count)
1163 ch->sd = vpif_obj.sd[0];
1166 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1168 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1169 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1171 /* select output 0 */
1172 err = vpif_set_output(vpif_obj.config, ch, 0);
1176 /* set initial format */
1177 ch->video.stdid = V4L2_STD_525_60;
1178 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1179 vpif_update_resolution(ch);
1181 /* Initialize vb2 queue */
1182 q = &common->buffer_queue;
1183 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1184 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1186 q->ops = &video_qops;
1187 q->mem_ops = &vb2_dma_contig_memops;
1188 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1189 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1190 q->min_buffers_needed = 1;
1191 q->lock = &common->lock;
1192 err = vb2_queue_init(q);
1194 vpif_err("vpif_display: vb2_queue_init() failed\n");
1198 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1199 if (IS_ERR(common->alloc_ctx)) {
1200 vpif_err("Failed to get the context\n");
1201 err = PTR_ERR(common->alloc_ctx);
1205 INIT_LIST_HEAD(&common->dma_queue);
1207 /* register video device */
1208 vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
1209 ch, &ch->video_dev);
1211 /* Initialize the video_device structure */
1212 vdev = &ch->video_dev;
1213 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1214 vdev->release = video_device_release_empty;
1215 vdev->fops = &vpif_fops;
1216 vdev->ioctl_ops = &vpif_ioctl_ops;
1217 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1218 vdev->vfl_dir = VFL_DIR_TX;
1220 vdev->lock = &common->lock;
1221 video_set_drvdata(&ch->video_dev, ch);
1222 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1231 for (k = 0; k < j; k++) {
1232 ch = vpif_obj.dev[k];
1233 common = &ch->common[k];
1234 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1235 video_unregister_device(&ch->video_dev);
1240 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1242 return vpif_probe_complete();
1246 * vpif_probe: This function creates device entries by register itself to the
1247 * V4L2 driver and initializes fields of each channel objects
1249 static __init int vpif_probe(struct platform_device *pdev)
1251 struct vpif_subdev_info *subdevdata;
1252 struct i2c_adapter *i2c_adap;
1253 struct resource *res;
1258 vpif_dev = &pdev->dev;
1259 err = initialize_vpif();
1262 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1266 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1268 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1272 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1273 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1274 IRQF_SHARED, VPIF_DRIVER_NAME,
1275 (void *)(&vpif_obj.dev[res_idx]->
1279 vpif_err("VPIF IRQ request failed\n");
1280 goto vpif_unregister;
1285 vpif_obj.config = pdev->dev.platform_data;
1286 subdev_count = vpif_obj.config->subdev_count;
1287 subdevdata = vpif_obj.config->subdevinfo;
1288 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1290 if (vpif_obj.sd == NULL) {
1291 vpif_err("unable to allocate memory for subdevice pointers\n");
1293 goto vpif_unregister;
1296 if (!vpif_obj.config->asd_sizes) {
1297 i2c_adap = i2c_get_adapter(1);
1298 for (i = 0; i < subdev_count; i++) {
1300 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1305 if (!vpif_obj.sd[i]) {
1306 vpif_err("Error registering v4l2 subdevice\n");
1308 goto probe_subdev_out;
1312 vpif_obj.sd[i]->grp_id = 1 << i;
1314 vpif_probe_complete();
1316 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1317 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1318 vpif_obj.notifier.bound = vpif_async_bound;
1319 vpif_obj.notifier.complete = vpif_async_complete;
1320 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1321 &vpif_obj.notifier);
1323 vpif_err("Error registering async notifier\n");
1325 goto probe_subdev_out;
1334 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1340 * vpif_remove: It un-register channels from V4L2 driver
1342 static int vpif_remove(struct platform_device *device)
1344 struct common_obj *common;
1345 struct channel_obj *ch;
1348 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1351 /* un-register device */
1352 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1353 /* Get the pointer to the channel object */
1354 ch = vpif_obj.dev[i];
1355 common = &ch->common[VPIF_VIDEO_INDEX];
1356 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1357 /* Unregister video device */
1358 video_unregister_device(&ch->video_dev);
1359 kfree(vpif_obj.dev[i]);
1365 #ifdef CONFIG_PM_SLEEP
1366 static int vpif_suspend(struct device *dev)
1368 struct common_obj *common;
1369 struct channel_obj *ch;
1372 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1373 /* Get the pointer to the channel object */
1374 ch = vpif_obj.dev[i];
1375 common = &ch->common[VPIF_VIDEO_INDEX];
1377 if (!vb2_start_streaming_called(&common->buffer_queue))
1380 mutex_lock(&common->lock);
1381 /* Disable channel */
1382 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1384 channel2_intr_enable(0);
1386 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1389 channel3_intr_enable(0);
1391 mutex_unlock(&common->lock);
1397 static int vpif_resume(struct device *dev)
1400 struct common_obj *common;
1401 struct channel_obj *ch;
1404 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1405 /* Get the pointer to the channel object */
1406 ch = vpif_obj.dev[i];
1407 common = &ch->common[VPIF_VIDEO_INDEX];
1409 if (!vb2_start_streaming_called(&common->buffer_queue))
1412 mutex_lock(&common->lock);
1413 /* Enable channel */
1414 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1416 channel2_intr_enable(1);
1418 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1421 channel3_intr_enable(1);
1423 mutex_unlock(&common->lock);
1431 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1433 static __refdata struct platform_driver vpif_driver = {
1435 .name = VPIF_DRIVER_NAME,
1438 .probe = vpif_probe,
1439 .remove = vpif_remove,
1442 module_platform_driver(vpif_driver);