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);
200 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
201 common->set_addr((addr + common->ytop_off),
202 (addr + common->ybtm_off),
203 (addr + common->ctop_off),
204 (addr + common->cbtm_off));
207 * Set interrupt for both the fields in VPIF
208 * Register enable channel in VPIF register
210 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
211 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
212 channel2_intr_assert();
213 channel2_intr_enable(1);
215 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
216 channel2_clipping_enable(1);
219 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
220 channel3_intr_assert();
221 channel3_intr_enable(1);
223 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
224 channel3_clipping_enable(1);
230 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
231 list_del(&buf->list);
232 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
234 spin_unlock_irqrestore(&common->irqlock, flags);
240 * vpif_stop_streaming : Stop the DMA engine
241 * @vq: ptr to vb2_queue
243 * This callback stops the DMA engine and any remaining buffers
244 * in the DMA queue are released.
246 static void vpif_stop_streaming(struct vb2_queue *vq)
248 struct channel_obj *ch = vb2_get_drv_priv(vq);
249 struct common_obj *common;
252 common = &ch->common[VPIF_VIDEO_INDEX];
254 /* Disable channel */
255 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
257 channel2_intr_enable(0);
259 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
261 channel3_intr_enable(0);
264 /* release all active buffers */
265 spin_lock_irqsave(&common->irqlock, flags);
266 if (common->cur_frm == common->next_frm) {
267 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
269 if (common->cur_frm != NULL)
270 vb2_buffer_done(&common->cur_frm->vb,
271 VB2_BUF_STATE_ERROR);
272 if (common->next_frm != NULL)
273 vb2_buffer_done(&common->next_frm->vb,
274 VB2_BUF_STATE_ERROR);
277 while (!list_empty(&common->dma_queue)) {
278 common->next_frm = list_entry(common->dma_queue.next,
279 struct vpif_disp_buffer, list);
280 list_del(&common->next_frm->list);
281 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
283 spin_unlock_irqrestore(&common->irqlock, flags);
286 static struct vb2_ops video_qops = {
287 .queue_setup = vpif_buffer_queue_setup,
288 .wait_prepare = vb2_ops_wait_prepare,
289 .wait_finish = vb2_ops_wait_finish,
290 .buf_prepare = vpif_buffer_prepare,
291 .start_streaming = vpif_start_streaming,
292 .stop_streaming = vpif_stop_streaming,
293 .buf_queue = vpif_buffer_queue,
296 static void process_progressive_mode(struct common_obj *common)
298 unsigned long addr = 0;
300 spin_lock(&common->irqlock);
301 /* Get the next buffer from buffer queue */
302 common->next_frm = list_entry(common->dma_queue.next,
303 struct vpif_disp_buffer, list);
304 /* Remove that buffer from the buffer queue */
305 list_del(&common->next_frm->list);
306 spin_unlock(&common->irqlock);
308 /* Set top and bottom field addrs in VPIF registers */
309 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
310 common->set_addr(addr + common->ytop_off,
311 addr + common->ybtm_off,
312 addr + common->ctop_off,
313 addr + common->cbtm_off);
316 static void process_interlaced_mode(int fid, struct common_obj *common)
318 /* device field id and local field id are in sync */
319 /* If this is even field */
321 if (common->cur_frm == common->next_frm)
324 /* one frame is displayed If next frame is
325 * available, release cur_frm and move on */
326 /* Copy frame display time */
327 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
328 /* Change status of the cur_frm */
329 vb2_buffer_done(&common->cur_frm->vb,
331 /* Make cur_frm pointing to next_frm */
332 common->cur_frm = common->next_frm;
334 } else if (1 == fid) { /* odd field */
335 spin_lock(&common->irqlock);
336 if (list_empty(&common->dma_queue)
337 || (common->cur_frm != common->next_frm)) {
338 spin_unlock(&common->irqlock);
341 spin_unlock(&common->irqlock);
342 /* one field is displayed configure the next
343 * frame if it is available else hold on current
345 /* Get next from the buffer queue */
346 process_progressive_mode(common);
351 * vpif_channel_isr: It changes status of the displayed buffer, takes next
352 * buffer from the queue and sets its address in VPIF registers
354 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
356 struct vpif_device *dev = &vpif_obj;
357 struct channel_obj *ch;
358 struct common_obj *common;
362 channel_id = *(int *)(dev_id);
363 if (!vpif_intr_status(channel_id + 2))
366 ch = dev->dev[channel_id];
367 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
368 common = &ch->common[i];
369 /* If streaming is started in this channel */
371 if (1 == ch->vpifparams.std_info.frm_fmt) {
372 spin_lock(&common->irqlock);
373 if (list_empty(&common->dma_queue)) {
374 spin_unlock(&common->irqlock);
377 spin_unlock(&common->irqlock);
379 /* Progressive mode */
380 if (!channel_first_int[i][channel_id]) {
381 /* Mark status of the cur_frm to
382 * done and unlock semaphore on it */
383 v4l2_get_timestamp(&common->cur_frm->vb.
385 vb2_buffer_done(&common->cur_frm->vb,
387 /* Make cur_frm pointing to next_frm */
388 common->cur_frm = common->next_frm;
391 channel_first_int[i][channel_id] = 0;
392 process_progressive_mode(common);
394 /* Interlaced mode */
395 /* If it is first interrupt, ignore it */
397 if (channel_first_int[i][channel_id]) {
398 channel_first_int[i][channel_id] = 0;
404 /* Get field id from VPIF registers */
405 fid = vpif_channel_getfid(ch->channel_id + 2);
406 /* If fid does not match with stored field id */
407 if (fid != ch->field_id) {
408 /* Make them in sync */
415 process_interlaced_mode(fid, common);
422 static int vpif_update_std_info(struct channel_obj *ch)
424 struct video_obj *vid_ch = &ch->video;
425 struct vpif_params *vpifparams = &ch->vpifparams;
426 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
427 const struct vpif_channel_config_params *config;
431 for (i = 0; i < vpif_ch_params_count; i++) {
432 config = &vpif_ch_params[i];
433 if (config->hd_sd == 0) {
434 vpif_dbg(2, debug, "SD format\n");
435 if (config->stdid & vid_ch->stdid) {
436 memcpy(std_info, config, sizeof(*config));
442 if (i == vpif_ch_params_count) {
443 vpif_dbg(1, debug, "Format not found\n");
450 static int vpif_update_resolution(struct channel_obj *ch)
452 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
453 struct video_obj *vid_ch = &ch->video;
454 struct vpif_params *vpifparams = &ch->vpifparams;
455 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
457 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
461 if (vpif_update_std_info(ch))
465 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
466 common->fmt.fmt.pix.width = std_info->width;
467 common->fmt.fmt.pix.height = std_info->height;
468 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
469 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
471 /* Set height and width paramateres */
472 common->height = std_info->height;
473 common->width = std_info->width;
474 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
477 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
479 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
481 if (ch->vpifparams.std_info.frm_fmt)
482 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
484 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
490 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
491 * in the top and bottom field
493 static void vpif_calculate_offsets(struct channel_obj *ch)
495 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
496 struct vpif_params *vpifparams = &ch->vpifparams;
497 enum v4l2_field field = common->fmt.fmt.pix.field;
498 struct video_obj *vid_ch = &ch->video;
499 unsigned int hpitch, sizeimage;
501 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
502 if (ch->vpifparams.std_info.frm_fmt)
503 vid_ch->buf_field = V4L2_FIELD_NONE;
505 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
507 vid_ch->buf_field = common->fmt.fmt.pix.field;
510 sizeimage = common->fmt.fmt.pix.sizeimage;
512 hpitch = common->fmt.fmt.pix.bytesperline;
513 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
514 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
515 common->ytop_off = 0;
516 common->ybtm_off = hpitch;
517 common->ctop_off = sizeimage / 2;
518 common->cbtm_off = sizeimage / 2 + hpitch;
519 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
520 common->ytop_off = 0;
521 common->ybtm_off = sizeimage / 4;
522 common->ctop_off = sizeimage / 2;
523 common->cbtm_off = common->ctop_off + sizeimage / 4;
524 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
525 common->ybtm_off = 0;
526 common->ytop_off = sizeimage / 4;
527 common->cbtm_off = sizeimage / 2;
528 common->ctop_off = common->cbtm_off + sizeimage / 4;
531 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
532 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
533 vpifparams->video_params.storage_mode = 1;
535 vpifparams->video_params.storage_mode = 0;
538 if (ch->vpifparams.std_info.frm_fmt == 1) {
539 vpifparams->video_params.hpitch =
540 common->fmt.fmt.pix.bytesperline;
542 if ((field == V4L2_FIELD_ANY) ||
543 (field == V4L2_FIELD_INTERLACED))
544 vpifparams->video_params.hpitch =
545 common->fmt.fmt.pix.bytesperline * 2;
547 vpifparams->video_params.hpitch =
548 common->fmt.fmt.pix.bytesperline;
551 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
554 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
556 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
558 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
559 common->set_addr = ch3_set_videobuf_addr;
562 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
564 common->set_addr = ch2_set_videobuf_addr;
568 /* functions implementing ioctls */
570 * vpif_querycap() - QUERYCAP handler
573 * @cap: ptr to v4l2_capability structure
575 static int vpif_querycap(struct file *file, void *priv,
576 struct v4l2_capability *cap)
578 struct vpif_display_config *config = vpif_dev->platform_data;
580 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
581 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
582 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
583 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
585 strlcpy(cap->card, config->card_name, sizeof(cap->card));
590 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
591 struct v4l2_fmtdesc *fmt)
596 /* Fill in the information about format */
597 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
598 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
599 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
604 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
605 struct v4l2_format *fmt)
607 struct video_device *vdev = video_devdata(file);
608 struct channel_obj *ch = video_get_drvdata(vdev);
609 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
611 /* Check the validity of the buffer type */
612 if (common->fmt.type != fmt->type)
615 if (vpif_update_resolution(ch))
621 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
622 struct v4l2_format *fmt)
624 struct video_device *vdev = video_devdata(file);
625 struct channel_obj *ch = video_get_drvdata(vdev);
626 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
627 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
630 * to supress v4l-compliance warnings silently correct
633 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
634 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
636 if (vpif_update_resolution(ch))
639 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
640 pixfmt->field = common->fmt.fmt.pix.field;
641 pixfmt->bytesperline = common->fmt.fmt.pix.width;
642 pixfmt->width = common->fmt.fmt.pix.width;
643 pixfmt->height = common->fmt.fmt.pix.height;
644 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
649 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
650 struct v4l2_format *fmt)
652 struct video_device *vdev = video_devdata(file);
653 struct channel_obj *ch = video_get_drvdata(vdev);
654 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
655 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
658 if (vb2_is_busy(&common->buffer_queue))
661 ret = vpif_try_fmt_vid_out(file, priv, fmt);
665 /* store the pix format in the channel object */
666 common->fmt.fmt.pix = *pixfmt;
668 /* store the format in the channel object */
673 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
675 struct vpif_display_config *config = vpif_dev->platform_data;
676 struct video_device *vdev = video_devdata(file);
677 struct channel_obj *ch = video_get_drvdata(vdev);
678 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
679 struct vpif_display_chan_config *chan_cfg;
680 struct v4l2_output output;
683 if (config->chan_config[ch->channel_id].outputs == NULL)
686 chan_cfg = &config->chan_config[ch->channel_id];
687 output = chan_cfg->outputs[ch->output_idx].output;
688 if (output.capabilities != V4L2_OUT_CAP_STD)
691 if (vb2_is_busy(&common->buffer_queue))
695 if (!(std_id & VPIF_V4L2_STD))
698 /* Call encoder subdevice function to set the standard */
699 ch->video.stdid = std_id;
700 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
701 /* Get the information about the standard */
702 if (vpif_update_resolution(ch))
705 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
707 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
708 s_std_output, std_id);
710 vpif_err("Failed to set output standard\n");
714 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
717 vpif_err("Failed to set standard for sub devices\n");
721 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
723 struct vpif_display_config *config = vpif_dev->platform_data;
724 struct video_device *vdev = video_devdata(file);
725 struct channel_obj *ch = video_get_drvdata(vdev);
726 struct vpif_display_chan_config *chan_cfg;
727 struct v4l2_output output;
729 if (config->chan_config[ch->channel_id].outputs == NULL)
732 chan_cfg = &config->chan_config[ch->channel_id];
733 output = chan_cfg->outputs[ch->output_idx].output;
734 if (output.capabilities != V4L2_OUT_CAP_STD)
737 *std = ch->video.stdid;
741 static int vpif_enum_output(struct file *file, void *fh,
742 struct v4l2_output *output)
745 struct vpif_display_config *config = vpif_dev->platform_data;
746 struct video_device *vdev = video_devdata(file);
747 struct channel_obj *ch = video_get_drvdata(vdev);
748 struct vpif_display_chan_config *chan_cfg;
750 chan_cfg = &config->chan_config[ch->channel_id];
751 if (output->index >= chan_cfg->output_count) {
752 vpif_dbg(1, debug, "Invalid output index\n");
756 *output = chan_cfg->outputs[output->index].output;
761 * vpif_output_to_subdev() - Maps output to sub device
762 * @vpif_cfg - global config ptr
763 * @chan_cfg - channel config ptr
764 * @index - Given output index from application
766 * lookup the sub device information for a given output index.
767 * we report all the output to application. output table also
768 * has sub device name for the each output
771 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
772 struct vpif_display_chan_config *chan_cfg, int index)
774 struct vpif_subdev_info *subdev_info;
775 const char *subdev_name;
778 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
780 if (chan_cfg->outputs == NULL)
783 subdev_name = chan_cfg->outputs[index].subdev_name;
784 if (subdev_name == NULL)
787 /* loop through the sub device list to get the sub device info */
788 for (i = 0; i < vpif_cfg->subdev_count; i++) {
789 subdev_info = &vpif_cfg->subdevinfo[i];
790 if (!strcmp(subdev_info->name, subdev_name))
797 * vpif_set_output() - Select an output
798 * @vpif_cfg - global config ptr
800 * @index - Given output index from application
802 * Select the given output.
804 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
805 struct channel_obj *ch, int index)
807 struct vpif_display_chan_config *chan_cfg =
808 &vpif_cfg->chan_config[ch->channel_id];
809 struct v4l2_subdev *sd = NULL;
810 u32 input = 0, output = 0;
814 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
816 sd = vpif_obj.sd[sd_index];
819 input = chan_cfg->outputs[index].input_route;
820 output = chan_cfg->outputs[index].output_route;
821 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
822 if (ret < 0 && ret != -ENOIOCTLCMD) {
823 vpif_err("Failed to set output\n");
828 ch->output_idx = index;
830 if (chan_cfg->outputs != NULL)
831 /* update tvnorms from the sub device output info */
832 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
836 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
838 struct vpif_display_config *config = vpif_dev->platform_data;
839 struct video_device *vdev = video_devdata(file);
840 struct channel_obj *ch = video_get_drvdata(vdev);
841 struct vpif_display_chan_config *chan_cfg;
842 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
844 if (vb2_is_busy(&common->buffer_queue))
847 chan_cfg = &config->chan_config[ch->channel_id];
849 if (i >= chan_cfg->output_count)
852 return vpif_set_output(config, ch, i);
855 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
857 struct video_device *vdev = video_devdata(file);
858 struct channel_obj *ch = video_get_drvdata(vdev);
866 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
869 * @timings: input timings
872 vpif_enum_dv_timings(struct file *file, void *priv,
873 struct v4l2_enum_dv_timings *timings)
875 struct vpif_display_config *config = vpif_dev->platform_data;
876 struct video_device *vdev = video_devdata(file);
877 struct channel_obj *ch = video_get_drvdata(vdev);
878 struct vpif_display_chan_config *chan_cfg;
879 struct v4l2_output output;
882 if (config->chan_config[ch->channel_id].outputs == NULL)
885 chan_cfg = &config->chan_config[ch->channel_id];
886 output = chan_cfg->outputs[ch->output_idx].output;
887 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
892 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
893 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
899 * vpif_s_dv_timings() - S_DV_TIMINGS handler
902 * @timings: digital video timings
904 static int vpif_s_dv_timings(struct file *file, void *priv,
905 struct v4l2_dv_timings *timings)
907 struct vpif_display_config *config = vpif_dev->platform_data;
908 struct video_device *vdev = video_devdata(file);
909 struct channel_obj *ch = video_get_drvdata(vdev);
910 struct vpif_params *vpifparams = &ch->vpifparams;
911 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
912 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
913 struct video_obj *vid_ch = &ch->video;
914 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
915 struct vpif_display_chan_config *chan_cfg;
916 struct v4l2_output output;
919 if (config->chan_config[ch->channel_id].outputs == NULL)
922 chan_cfg = &config->chan_config[ch->channel_id];
923 output = chan_cfg->outputs[ch->output_idx].output;
924 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
927 if (vb2_is_busy(&common->buffer_queue))
930 if (timings->type != V4L2_DV_BT_656_1120) {
931 vpif_dbg(2, debug, "Timing type not defined\n");
935 /* Configure subdevice timings, if any */
936 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
937 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
940 vpif_dbg(2, debug, "Error setting custom DV timings\n");
944 if (!(timings->bt.width && timings->bt.height &&
945 (timings->bt.hbackporch ||
946 timings->bt.hfrontporch ||
947 timings->bt.hsync) &&
948 timings->bt.vfrontporch &&
949 (timings->bt.vbackporch ||
950 timings->bt.vsync))) {
951 vpif_dbg(2, debug, "Timings for width, height, "
952 "horizontal back porch, horizontal sync, "
953 "horizontal front porch, vertical back porch, "
954 "vertical sync and vertical back porch "
955 "must be defined\n");
959 vid_ch->dv_timings = *timings;
961 /* Configure video port timings */
963 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
964 std_info->sav2eav = bt->width;
967 std_info->l3 = bt->vsync + bt->vbackporch + 1;
969 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
970 if (bt->interlaced) {
971 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
972 std_info->l5 = std_info->vsize/2 -
973 (bt->vfrontporch - 1);
974 std_info->l7 = std_info->vsize/2 + 1;
975 std_info->l9 = std_info->l7 + bt->il_vsync +
976 bt->il_vbackporch + 1;
977 std_info->l11 = std_info->vsize -
978 (bt->il_vfrontporch - 1);
980 vpif_dbg(2, debug, "Required timing values for "
981 "interlaced BT format missing\n");
985 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
987 strncpy(std_info->name, "Custom timings BT656/1120",
989 std_info->width = bt->width;
990 std_info->height = bt->height;
991 std_info->frm_fmt = bt->interlaced ? 0 : 1;
992 std_info->ycmux_mode = 0;
993 std_info->capture_format = 0;
994 std_info->vbi_supported = 0;
1003 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1005 * @priv: file handle
1006 * @timings: digital video timings
1008 static int vpif_g_dv_timings(struct file *file, void *priv,
1009 struct v4l2_dv_timings *timings)
1011 struct vpif_display_config *config = vpif_dev->platform_data;
1012 struct video_device *vdev = video_devdata(file);
1013 struct channel_obj *ch = video_get_drvdata(vdev);
1014 struct vpif_display_chan_config *chan_cfg;
1015 struct video_obj *vid_ch = &ch->video;
1016 struct v4l2_output output;
1018 if (config->chan_config[ch->channel_id].outputs == NULL)
1021 chan_cfg = &config->chan_config[ch->channel_id];
1022 output = chan_cfg->outputs[ch->output_idx].output;
1024 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1027 *timings = vid_ch->dv_timings;
1035 * vpif_log_status() - Status information
1037 * @priv: file handle
1041 static int vpif_log_status(struct file *filep, void *priv)
1043 /* status for sub devices */
1044 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1049 /* vpif display ioctl operations */
1050 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1051 .vidioc_querycap = vpif_querycap,
1052 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1053 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1054 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1055 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1057 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1058 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1059 .vidioc_querybuf = vb2_ioctl_querybuf,
1060 .vidioc_qbuf = vb2_ioctl_qbuf,
1061 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1062 .vidioc_expbuf = vb2_ioctl_expbuf,
1063 .vidioc_streamon = vb2_ioctl_streamon,
1064 .vidioc_streamoff = vb2_ioctl_streamoff,
1066 .vidioc_s_std = vpif_s_std,
1067 .vidioc_g_std = vpif_g_std,
1069 .vidioc_enum_output = vpif_enum_output,
1070 .vidioc_s_output = vpif_s_output,
1071 .vidioc_g_output = vpif_g_output,
1073 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1074 .vidioc_s_dv_timings = vpif_s_dv_timings,
1075 .vidioc_g_dv_timings = vpif_g_dv_timings,
1077 .vidioc_log_status = vpif_log_status,
1080 static const struct v4l2_file_operations vpif_fops = {
1081 .owner = THIS_MODULE,
1082 .open = v4l2_fh_open,
1083 .release = vb2_fop_release,
1084 .unlocked_ioctl = video_ioctl2,
1085 .mmap = vb2_fop_mmap,
1086 .poll = vb2_fop_poll
1089 /*Configure the channels, buffer sizei, request irq */
1090 static int initialize_vpif(void)
1092 int free_channel_objects_index;
1095 /* Allocate memory for six channel objects */
1096 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1098 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1099 /* If memory allocation fails, return error */
1100 if (!vpif_obj.dev[i]) {
1101 free_channel_objects_index = i;
1103 goto vpif_init_free_channel_objects;
1109 vpif_init_free_channel_objects:
1110 for (j = 0; j < free_channel_objects_index; j++)
1111 kfree(vpif_obj.dev[j]);
1115 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1116 struct v4l2_subdev *subdev,
1117 struct v4l2_async_subdev *asd)
1121 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1122 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1124 vpif_obj.sd[i] = subdev;
1125 vpif_obj.sd[i]->grp_id = 1 << i;
1132 static int vpif_probe_complete(void)
1134 struct common_obj *common;
1135 struct video_device *vdev;
1136 struct channel_obj *ch;
1137 struct vb2_queue *q;
1140 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1141 ch = vpif_obj.dev[j];
1142 /* Initialize field of the channel objects */
1143 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1144 common = &ch->common[k];
1145 spin_lock_init(&common->irqlock);
1146 mutex_init(&common->lock);
1147 common->set_addr = NULL;
1148 common->ytop_off = 0;
1149 common->ybtm_off = 0;
1150 common->ctop_off = 0;
1151 common->cbtm_off = 0;
1152 common->cur_frm = NULL;
1153 common->next_frm = NULL;
1154 memset(&common->fmt, 0, sizeof(common->fmt));
1156 ch->initialized = 0;
1157 if (vpif_obj.config->subdev_count)
1158 ch->sd = vpif_obj.sd[0];
1161 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1163 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1164 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1166 /* select output 0 */
1167 err = vpif_set_output(vpif_obj.config, ch, 0);
1171 /* set initial format */
1172 ch->video.stdid = V4L2_STD_525_60;
1173 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1174 vpif_update_resolution(ch);
1176 /* Initialize vb2 queue */
1177 q = &common->buffer_queue;
1178 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1179 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1181 q->ops = &video_qops;
1182 q->mem_ops = &vb2_dma_contig_memops;
1183 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1184 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1185 q->min_buffers_needed = 1;
1186 q->lock = &common->lock;
1187 err = vb2_queue_init(q);
1189 vpif_err("vpif_display: vb2_queue_init() failed\n");
1193 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1194 if (IS_ERR(common->alloc_ctx)) {
1195 vpif_err("Failed to get the context\n");
1196 err = PTR_ERR(common->alloc_ctx);
1200 INIT_LIST_HEAD(&common->dma_queue);
1202 /* register video device */
1203 vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
1204 ch, &ch->video_dev);
1206 /* Initialize the video_device structure */
1207 vdev = ch->video_dev;
1208 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1209 vdev->release = video_device_release;
1210 vdev->fops = &vpif_fops;
1211 vdev->ioctl_ops = &vpif_ioctl_ops;
1212 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1213 vdev->vfl_dir = VFL_DIR_TX;
1215 vdev->lock = &common->lock;
1216 video_set_drvdata(ch->video_dev, ch);
1217 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1226 for (k = 0; k < j; k++) {
1227 ch = vpif_obj.dev[k];
1228 common = &ch->common[k];
1229 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1230 video_unregister_device(ch->video_dev);
1231 video_device_release(ch->video_dev);
1232 ch->video_dev = NULL;
1237 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1239 return vpif_probe_complete();
1243 * vpif_probe: This function creates device entries by register itself to the
1244 * V4L2 driver and initializes fields of each channel objects
1246 static __init int vpif_probe(struct platform_device *pdev)
1248 struct vpif_subdev_info *subdevdata;
1249 int i, j = 0, err = 0;
1251 struct i2c_adapter *i2c_adap;
1252 struct channel_obj *ch;
1253 struct video_device *vfd;
1254 struct resource *res;
1257 vpif_dev = &pdev->dev;
1258 err = initialize_vpif();
1261 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1265 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1267 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1271 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1272 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1273 IRQF_SHARED, VPIF_DRIVER_NAME,
1274 (void *)(&vpif_obj.dev[res_idx]->
1278 vpif_err("VPIF IRQ request failed\n");
1279 goto vpif_unregister;
1284 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1285 /* Get the pointer to the channel object */
1286 ch = vpif_obj.dev[i];
1288 /* Allocate memory for video device */
1289 vfd = video_device_alloc();
1291 for (j = 0; j < i; j++) {
1292 ch = vpif_obj.dev[j];
1293 video_device_release(ch->video_dev);
1296 goto vpif_unregister;
1299 /* Set video_dev to the video device */
1300 ch->video_dev = vfd;
1303 vpif_obj.config = pdev->dev.platform_data;
1304 subdev_count = vpif_obj.config->subdev_count;
1305 subdevdata = vpif_obj.config->subdevinfo;
1306 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1308 if (vpif_obj.sd == NULL) {
1309 vpif_err("unable to allocate memory for subdevice pointers\n");
1314 if (!vpif_obj.config->asd_sizes) {
1315 i2c_adap = i2c_get_adapter(1);
1316 for (i = 0; i < subdev_count; i++) {
1318 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1323 if (!vpif_obj.sd[i]) {
1324 vpif_err("Error registering v4l2 subdevice\n");
1326 goto probe_subdev_out;
1330 vpif_obj.sd[i]->grp_id = 1 << i;
1332 vpif_probe_complete();
1334 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1335 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1336 vpif_obj.notifier.bound = vpif_async_bound;
1337 vpif_obj.notifier.complete = vpif_async_complete;
1338 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1339 &vpif_obj.notifier);
1341 vpif_err("Error registering async notifier\n");
1343 goto probe_subdev_out;
1352 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1353 ch = vpif_obj.dev[i];
1354 /* Note: does nothing if ch->video_dev == NULL */
1355 video_device_release(ch->video_dev);
1358 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1364 * vpif_remove: It un-register channels from V4L2 driver
1366 static int vpif_remove(struct platform_device *device)
1368 struct common_obj *common;
1369 struct channel_obj *ch;
1372 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1375 /* un-register device */
1376 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1377 /* Get the pointer to the channel object */
1378 ch = vpif_obj.dev[i];
1379 common = &ch->common[VPIF_VIDEO_INDEX];
1380 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1381 /* Unregister video device */
1382 video_unregister_device(ch->video_dev);
1384 ch->video_dev = NULL;
1385 kfree(vpif_obj.dev[i]);
1391 #ifdef CONFIG_PM_SLEEP
1392 static int vpif_suspend(struct device *dev)
1394 struct common_obj *common;
1395 struct channel_obj *ch;
1398 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1399 /* Get the pointer to the channel object */
1400 ch = vpif_obj.dev[i];
1401 common = &ch->common[VPIF_VIDEO_INDEX];
1403 if (!vb2_start_streaming_called(&common->buffer_queue))
1406 mutex_lock(&common->lock);
1407 /* Disable channel */
1408 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1410 channel2_intr_enable(0);
1412 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1415 channel3_intr_enable(0);
1417 mutex_unlock(&common->lock);
1423 static int vpif_resume(struct device *dev)
1426 struct common_obj *common;
1427 struct channel_obj *ch;
1430 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1431 /* Get the pointer to the channel object */
1432 ch = vpif_obj.dev[i];
1433 common = &ch->common[VPIF_VIDEO_INDEX];
1435 if (!vb2_start_streaming_called(&common->buffer_queue))
1438 mutex_lock(&common->lock);
1439 /* Enable channel */
1440 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1442 channel2_intr_enable(1);
1444 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1447 channel3_intr_enable(1);
1449 mutex_unlock(&common->lock);
1457 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1459 static __refdata struct platform_driver vpif_driver = {
1461 .name = VPIF_DRIVER_NAME,
1462 .owner = THIS_MODULE,
1465 .probe = vpif_probe,
1466 .remove = vpif_remove,
1469 module_platform_driver(vpif_driver);