2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
5 * Copyright (C) 2009 Texas Instruments Incorporated - https://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"
45 MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);
47 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
48 static int ycmux_mode;
50 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
52 static struct vpif_device vpif_obj = { {NULL} };
53 static struct device *vpif_dev;
54 static void vpif_calculate_offsets(struct channel_obj *ch);
55 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
58 struct vpif_disp_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
60 return container_of(vb, struct vpif_disp_buffer, vb);
64 * vpif_buffer_prepare : callback function for buffer prepare
65 * @vb: ptr to vb2_buffer
67 * This is the callback function for buffer prepare when vb2_qbuf()
68 * function is called. The buffer is prepared and user space virtual address
69 * or user address is converted into physical address
71 static int vpif_buffer_prepare(struct vb2_buffer *vb)
73 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
74 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
75 struct common_obj *common;
77 common = &ch->common[VPIF_VIDEO_INDEX];
79 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
80 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
83 vbuf->field = common->fmt.fmt.pix.field;
85 if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
86 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
88 if (!ISALIGNED(addr + common->ytop_off) ||
89 !ISALIGNED(addr + common->ybtm_off) ||
90 !ISALIGNED(addr + common->ctop_off) ||
91 !ISALIGNED(addr + common->cbtm_off)) {
92 vpif_err("buffer offset not aligned to 8 bytes\n");
101 * 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_devs: 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 unsigned int *nbuffers, unsigned int *nplanes,
113 unsigned int sizes[], struct device *alloc_devs[])
115 struct channel_obj *ch = vb2_get_drv_priv(vq);
116 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
117 unsigned size = common->fmt.fmt.pix.sizeimage;
125 if (vq->num_buffers + *nbuffers < 3)
126 *nbuffers = 3 - vq->num_buffers;
131 /* Calculate the offset for Y and C data in the buffer */
132 vpif_calculate_offsets(ch);
138 * vpif_buffer_queue : Callback function to add buffer to DMA queue
139 * @vb: ptr to vb2_buffer
141 * This callback function queues the buffer to DMA engine
143 static void vpif_buffer_queue(struct vb2_buffer *vb)
145 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
146 struct vpif_disp_buffer *buf = to_vpif_buffer(vbuf);
147 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
148 struct common_obj *common;
151 common = &ch->common[VPIF_VIDEO_INDEX];
153 /* add the buffer to the DMA queue */
154 spin_lock_irqsave(&common->irqlock, flags);
155 list_add_tail(&buf->list, &common->dma_queue);
156 spin_unlock_irqrestore(&common->irqlock, flags);
160 * vpif_start_streaming : Starts the DMA engine for streaming
161 * @vq: ptr to vb2_buffer
162 * @count: number of buffers
164 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
166 struct vpif_display_config *vpif_config_data =
167 vpif_dev->platform_data;
168 struct channel_obj *ch = vb2_get_drv_priv(vq);
169 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
170 struct vpif_params *vpif = &ch->vpifparams;
171 struct vpif_disp_buffer *buf, *tmp;
172 unsigned long addr, flags;
175 spin_lock_irqsave(&common->irqlock, flags);
177 /* Initialize field_id */
181 if (vpif_config_data->set_clock) {
182 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
183 ycmux_mode, ch->vpifparams.std_info.hd_sd);
185 vpif_err("can't set clock\n");
190 /* set the parameters and addresses */
191 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
196 vpif_config_addr(ch, ret);
197 /* Get the next frame from the buffer queue */
198 common->next_frm = common->cur_frm =
199 list_entry(common->dma_queue.next,
200 struct vpif_disp_buffer, list);
202 list_del(&common->cur_frm->list);
203 spin_unlock_irqrestore(&common->irqlock, flags);
205 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
206 common->set_addr((addr + common->ytop_off),
207 (addr + common->ybtm_off),
208 (addr + common->ctop_off),
209 (addr + common->cbtm_off));
212 * Set interrupt for both the fields in VPIF
213 * Register enable channel in VPIF register
215 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
216 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
217 channel2_intr_assert();
218 channel2_intr_enable(1);
220 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
221 channel2_clipping_enable(1);
224 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
225 channel3_intr_assert();
226 channel3_intr_enable(1);
228 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
229 channel3_clipping_enable(1);
235 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
236 list_del(&buf->list);
237 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
239 spin_unlock_irqrestore(&common->irqlock, flags);
245 * vpif_stop_streaming : Stop the DMA engine
246 * @vq: ptr to vb2_queue
248 * This callback stops the DMA engine and any remaining buffers
249 * in the DMA queue are released.
251 static void vpif_stop_streaming(struct vb2_queue *vq)
253 struct channel_obj *ch = vb2_get_drv_priv(vq);
254 struct common_obj *common;
257 common = &ch->common[VPIF_VIDEO_INDEX];
259 /* Disable channel */
260 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
262 channel2_intr_enable(0);
264 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
266 channel3_intr_enable(0);
269 /* release all active buffers */
270 spin_lock_irqsave(&common->irqlock, flags);
271 if (common->cur_frm == common->next_frm) {
272 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
273 VB2_BUF_STATE_ERROR);
276 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
277 VB2_BUF_STATE_ERROR);
278 if (common->next_frm)
279 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
280 VB2_BUF_STATE_ERROR);
283 while (!list_empty(&common->dma_queue)) {
284 common->next_frm = list_entry(common->dma_queue.next,
285 struct vpif_disp_buffer, list);
286 list_del(&common->next_frm->list);
287 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
288 VB2_BUF_STATE_ERROR);
290 spin_unlock_irqrestore(&common->irqlock, flags);
293 static const struct vb2_ops video_qops = {
294 .queue_setup = vpif_buffer_queue_setup,
295 .wait_prepare = vb2_ops_wait_prepare,
296 .wait_finish = vb2_ops_wait_finish,
297 .buf_prepare = vpif_buffer_prepare,
298 .start_streaming = vpif_start_streaming,
299 .stop_streaming = vpif_stop_streaming,
300 .buf_queue = vpif_buffer_queue,
303 static void process_progressive_mode(struct common_obj *common)
307 spin_lock(&common->irqlock);
308 /* Get the next buffer from buffer queue */
309 common->next_frm = list_entry(common->dma_queue.next,
310 struct vpif_disp_buffer, list);
311 /* Remove that buffer from the buffer queue */
312 list_del(&common->next_frm->list);
313 spin_unlock(&common->irqlock);
315 /* Set top and bottom field addrs in VPIF registers */
316 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
317 common->set_addr(addr + common->ytop_off,
318 addr + common->ybtm_off,
319 addr + common->ctop_off,
320 addr + common->cbtm_off);
323 static void process_interlaced_mode(int fid, struct common_obj *common)
325 /* device field id and local field id are in sync */
326 /* If this is even field */
328 if (common->cur_frm == common->next_frm)
331 /* one frame is displayed If next frame is
332 * available, release cur_frm and move on */
333 /* Copy frame display time */
334 common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
335 /* Change status of the cur_frm */
336 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
338 /* Make cur_frm pointing to next_frm */
339 common->cur_frm = common->next_frm;
341 } else if (1 == fid) { /* odd field */
342 spin_lock(&common->irqlock);
343 if (list_empty(&common->dma_queue)
344 || (common->cur_frm != common->next_frm)) {
345 spin_unlock(&common->irqlock);
348 spin_unlock(&common->irqlock);
349 /* one field is displayed configure the next
350 * frame if it is available else hold on current
352 /* Get next from the buffer queue */
353 process_progressive_mode(common);
358 * vpif_channel_isr: It changes status of the displayed buffer, takes next
359 * buffer from the queue and sets its address in VPIF registers
361 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
363 struct vpif_device *dev = &vpif_obj;
364 struct channel_obj *ch;
365 struct common_obj *common;
369 channel_id = *(int *)(dev_id);
370 if (!vpif_intr_status(channel_id + 2))
373 ch = dev->dev[channel_id];
374 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
375 common = &ch->common[i];
376 /* If streaming is started in this channel */
378 if (1 == ch->vpifparams.std_info.frm_fmt) {
379 spin_lock(&common->irqlock);
380 if (list_empty(&common->dma_queue)) {
381 spin_unlock(&common->irqlock);
384 spin_unlock(&common->irqlock);
386 /* Progressive mode */
387 if (!channel_first_int[i][channel_id]) {
388 /* Mark status of the cur_frm to
389 * done and unlock semaphore on it */
390 common->cur_frm->vb.vb2_buf.timestamp =
392 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
394 /* Make cur_frm pointing to next_frm */
395 common->cur_frm = common->next_frm;
398 channel_first_int[i][channel_id] = 0;
399 process_progressive_mode(common);
401 /* Interlaced mode */
402 /* If it is first interrupt, ignore it */
404 if (channel_first_int[i][channel_id]) {
405 channel_first_int[i][channel_id] = 0;
411 /* Get field id from VPIF registers */
412 fid = vpif_channel_getfid(ch->channel_id + 2);
413 /* If fid does not match with stored field id */
414 if (fid != ch->field_id) {
415 /* Make them in sync */
422 process_interlaced_mode(fid, common);
429 static int vpif_update_std_info(struct channel_obj *ch)
431 struct video_obj *vid_ch = &ch->video;
432 struct vpif_params *vpifparams = &ch->vpifparams;
433 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
434 const struct vpif_channel_config_params *config;
438 for (i = 0; i < vpif_ch_params_count; i++) {
439 config = &vpif_ch_params[i];
440 if (config->hd_sd == 0) {
441 vpif_dbg(2, debug, "SD format\n");
442 if (config->stdid & vid_ch->stdid) {
443 memcpy(std_info, config, sizeof(*config));
449 if (i == vpif_ch_params_count) {
450 vpif_dbg(1, debug, "Format not found\n");
457 static int vpif_update_resolution(struct channel_obj *ch)
459 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
460 struct video_obj *vid_ch = &ch->video;
461 struct vpif_params *vpifparams = &ch->vpifparams;
462 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
464 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
468 if (vpif_update_std_info(ch))
472 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
473 common->fmt.fmt.pix.width = std_info->width;
474 common->fmt.fmt.pix.height = std_info->height;
475 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
476 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
478 /* Set height and width paramateres */
479 common->height = std_info->height;
480 common->width = std_info->width;
481 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
484 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
486 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
488 if (ch->vpifparams.std_info.frm_fmt)
489 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
491 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
497 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
498 * in the top and bottom field
500 static void vpif_calculate_offsets(struct channel_obj *ch)
502 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
503 struct vpif_params *vpifparams = &ch->vpifparams;
504 enum v4l2_field field = common->fmt.fmt.pix.field;
505 struct video_obj *vid_ch = &ch->video;
506 unsigned int hpitch, sizeimage;
508 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
509 if (ch->vpifparams.std_info.frm_fmt)
510 vid_ch->buf_field = V4L2_FIELD_NONE;
512 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
514 vid_ch->buf_field = common->fmt.fmt.pix.field;
517 sizeimage = common->fmt.fmt.pix.sizeimage;
519 hpitch = common->fmt.fmt.pix.bytesperline;
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 strscpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
588 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
590 strscpy(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->pixelformat = V4L2_PIX_FMT_YUV422P;
606 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
607 struct v4l2_format *fmt)
609 struct video_device *vdev = video_devdata(file);
610 struct channel_obj *ch = video_get_drvdata(vdev);
611 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
613 /* Check the validity of the buffer type */
614 if (common->fmt.type != fmt->type)
617 if (vpif_update_resolution(ch))
623 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
624 struct v4l2_format *fmt)
626 struct video_device *vdev = video_devdata(file);
627 struct channel_obj *ch = video_get_drvdata(vdev);
628 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
629 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
632 * to suppress v4l-compliance warnings silently correct
635 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
636 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
638 if (vpif_update_resolution(ch))
641 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
642 pixfmt->field = common->fmt.fmt.pix.field;
643 pixfmt->bytesperline = common->fmt.fmt.pix.width;
644 pixfmt->width = common->fmt.fmt.pix.width;
645 pixfmt->height = common->fmt.fmt.pix.height;
646 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
651 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
652 struct v4l2_format *fmt)
654 struct video_device *vdev = video_devdata(file);
655 struct channel_obj *ch = video_get_drvdata(vdev);
656 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
657 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
660 if (vb2_is_busy(&common->buffer_queue))
663 ret = vpif_try_fmt_vid_out(file, priv, fmt);
667 /* store the pix format in the channel object */
668 common->fmt.fmt.pix = *pixfmt;
670 /* store the format in the channel object */
675 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
677 struct vpif_display_config *config = vpif_dev->platform_data;
678 struct video_device *vdev = video_devdata(file);
679 struct channel_obj *ch = video_get_drvdata(vdev);
680 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
681 struct vpif_display_chan_config *chan_cfg;
682 struct v4l2_output output;
685 if (!config->chan_config[ch->channel_id].outputs)
688 chan_cfg = &config->chan_config[ch->channel_id];
689 output = chan_cfg->outputs[ch->output_idx].output;
690 if (output.capabilities != V4L2_OUT_CAP_STD)
693 if (vb2_is_busy(&common->buffer_queue))
697 if (!(std_id & VPIF_V4L2_STD))
700 /* Call encoder subdevice function to set the standard */
701 ch->video.stdid = std_id;
702 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
703 /* Get the information about the standard */
704 if (vpif_update_resolution(ch))
707 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
709 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
710 s_std_output, std_id);
712 vpif_err("Failed to set output standard\n");
716 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
719 vpif_err("Failed to set standard for sub devices\n");
723 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
725 struct vpif_display_config *config = vpif_dev->platform_data;
726 struct video_device *vdev = video_devdata(file);
727 struct channel_obj *ch = video_get_drvdata(vdev);
728 struct vpif_display_chan_config *chan_cfg;
729 struct v4l2_output output;
731 if (!config->chan_config[ch->channel_id].outputs)
734 chan_cfg = &config->chan_config[ch->channel_id];
735 output = chan_cfg->outputs[ch->output_idx].output;
736 if (output.capabilities != V4L2_OUT_CAP_STD)
739 *std = ch->video.stdid;
743 static int vpif_enum_output(struct file *file, void *fh,
744 struct v4l2_output *output)
747 struct vpif_display_config *config = vpif_dev->platform_data;
748 struct video_device *vdev = video_devdata(file);
749 struct channel_obj *ch = video_get_drvdata(vdev);
750 struct vpif_display_chan_config *chan_cfg;
752 chan_cfg = &config->chan_config[ch->channel_id];
753 if (output->index >= chan_cfg->output_count) {
754 vpif_dbg(1, debug, "Invalid output index\n");
758 *output = chan_cfg->outputs[output->index].output;
763 * vpif_output_to_subdev() - Maps output to sub device
764 * @vpif_cfg: global config ptr
765 * @chan_cfg: channel config ptr
766 * @index: Given output index from application
768 * lookup the sub device information for a given output index.
769 * we report all the output to application. output table also
770 * has sub device name for the each output
773 vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
774 struct vpif_display_chan_config *chan_cfg, int index)
776 struct vpif_subdev_info *subdev_info;
777 const char *subdev_name;
780 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
782 if (!chan_cfg->outputs)
785 subdev_name = chan_cfg->outputs[index].subdev_name;
789 /* loop through the sub device list to get the sub device info */
790 for (i = 0; i < vpif_cfg->subdev_count; i++) {
791 subdev_info = &vpif_cfg->subdevinfo[i];
792 if (!strcmp(subdev_info->name, subdev_name))
799 * vpif_set_output() - Select an output
800 * @vpif_cfg: global config ptr
802 * @index: Given output index from application
804 * Select the given output.
806 static int vpif_set_output(struct vpif_display_config *vpif_cfg,
807 struct channel_obj *ch, int index)
809 struct vpif_display_chan_config *chan_cfg =
810 &vpif_cfg->chan_config[ch->channel_id];
811 struct v4l2_subdev *sd = NULL;
812 u32 input = 0, output = 0;
816 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
818 sd = vpif_obj.sd[sd_index];
821 input = chan_cfg->outputs[index].input_route;
822 output = chan_cfg->outputs[index].output_route;
823 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
824 if (ret < 0 && ret != -ENOIOCTLCMD) {
825 vpif_err("Failed to set output\n");
830 ch->output_idx = index;
832 if (chan_cfg->outputs)
833 /* update tvnorms from the sub device output info */
834 ch->video_dev.tvnorms = chan_cfg->outputs[index].output.std;
838 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
840 struct vpif_display_config *config = vpif_dev->platform_data;
841 struct video_device *vdev = video_devdata(file);
842 struct channel_obj *ch = video_get_drvdata(vdev);
843 struct vpif_display_chan_config *chan_cfg;
844 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
846 if (vb2_is_busy(&common->buffer_queue))
849 chan_cfg = &config->chan_config[ch->channel_id];
851 if (i >= chan_cfg->output_count)
854 return vpif_set_output(config, ch, i);
857 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
859 struct video_device *vdev = video_devdata(file);
860 struct channel_obj *ch = video_get_drvdata(vdev);
868 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
871 * @timings: input timings
874 vpif_enum_dv_timings(struct file *file, void *priv,
875 struct v4l2_enum_dv_timings *timings)
877 struct vpif_display_config *config = vpif_dev->platform_data;
878 struct video_device *vdev = video_devdata(file);
879 struct channel_obj *ch = video_get_drvdata(vdev);
880 struct vpif_display_chan_config *chan_cfg;
881 struct v4l2_output output;
884 if (!config->chan_config[ch->channel_id].outputs)
887 chan_cfg = &config->chan_config[ch->channel_id];
888 output = chan_cfg->outputs[ch->output_idx].output;
889 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
894 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
895 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
901 * vpif_s_dv_timings() - S_DV_TIMINGS handler
904 * @timings: digital video timings
906 static int vpif_s_dv_timings(struct file *file, void *priv,
907 struct v4l2_dv_timings *timings)
909 struct vpif_display_config *config = vpif_dev->platform_data;
910 struct video_device *vdev = video_devdata(file);
911 struct channel_obj *ch = video_get_drvdata(vdev);
912 struct vpif_params *vpifparams = &ch->vpifparams;
913 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
914 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
915 struct video_obj *vid_ch = &ch->video;
916 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
917 struct vpif_display_chan_config *chan_cfg;
918 struct v4l2_output output;
921 if (!config->chan_config[ch->channel_id].outputs)
924 chan_cfg = &config->chan_config[ch->channel_id];
925 output = chan_cfg->outputs[ch->output_idx].output;
926 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
929 if (vb2_is_busy(&common->buffer_queue))
932 if (timings->type != V4L2_DV_BT_656_1120) {
933 vpif_dbg(2, debug, "Timing type not defined\n");
937 /* Configure subdevice timings, if any */
938 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
939 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
942 vpif_dbg(2, debug, "Error setting custom DV timings\n");
946 if (!(timings->bt.width && timings->bt.height &&
947 (timings->bt.hbackporch ||
948 timings->bt.hfrontporch ||
949 timings->bt.hsync) &&
950 timings->bt.vfrontporch &&
951 (timings->bt.vbackporch ||
952 timings->bt.vsync))) {
953 vpif_dbg(2, debug, "Timings for width, height, horizontal back porch, horizontal sync, horizontal front porch, vertical back porch, vertical sync and vertical back porch must be defined\n");
957 vid_ch->dv_timings = *timings;
959 /* Configure video port timings */
961 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
962 std_info->sav2eav = bt->width;
965 std_info->l3 = bt->vsync + bt->vbackporch + 1;
967 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
968 if (bt->interlaced) {
969 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
970 std_info->l5 = std_info->vsize/2 -
971 (bt->vfrontporch - 1);
972 std_info->l7 = std_info->vsize/2 + 1;
973 std_info->l9 = std_info->l7 + bt->il_vsync +
974 bt->il_vbackporch + 1;
975 std_info->l11 = std_info->vsize -
976 (bt->il_vfrontporch - 1);
978 vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
982 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
984 strscpy(std_info->name, "Custom timings BT656/1120",
985 sizeof(std_info->name));
986 std_info->width = bt->width;
987 std_info->height = bt->height;
988 std_info->frm_fmt = bt->interlaced ? 0 : 1;
989 std_info->ycmux_mode = 0;
990 std_info->capture_format = 0;
991 std_info->vbi_supported = 0;
1000 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1002 * @priv: file handle
1003 * @timings: digital video timings
1005 static int vpif_g_dv_timings(struct file *file, void *priv,
1006 struct v4l2_dv_timings *timings)
1008 struct vpif_display_config *config = vpif_dev->platform_data;
1009 struct video_device *vdev = video_devdata(file);
1010 struct channel_obj *ch = video_get_drvdata(vdev);
1011 struct vpif_display_chan_config *chan_cfg;
1012 struct video_obj *vid_ch = &ch->video;
1013 struct v4l2_output output;
1015 if (!config->chan_config[ch->channel_id].outputs)
1018 chan_cfg = &config->chan_config[ch->channel_id];
1019 output = chan_cfg->outputs[ch->output_idx].output;
1021 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1024 *timings = vid_ch->dv_timings;
1032 * vpif_log_status() - Status information
1034 * @priv: file handle
1038 static int vpif_log_status(struct file *filep, void *priv)
1040 /* status for sub devices */
1041 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1046 /* vpif display ioctl operations */
1047 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1048 .vidioc_querycap = vpif_querycap,
1049 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1050 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1051 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1052 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1054 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1055 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1056 .vidioc_querybuf = vb2_ioctl_querybuf,
1057 .vidioc_qbuf = vb2_ioctl_qbuf,
1058 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1059 .vidioc_expbuf = vb2_ioctl_expbuf,
1060 .vidioc_streamon = vb2_ioctl_streamon,
1061 .vidioc_streamoff = vb2_ioctl_streamoff,
1063 .vidioc_s_std = vpif_s_std,
1064 .vidioc_g_std = vpif_g_std,
1066 .vidioc_enum_output = vpif_enum_output,
1067 .vidioc_s_output = vpif_s_output,
1068 .vidioc_g_output = vpif_g_output,
1070 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1071 .vidioc_s_dv_timings = vpif_s_dv_timings,
1072 .vidioc_g_dv_timings = vpif_g_dv_timings,
1074 .vidioc_log_status = vpif_log_status,
1077 static const struct v4l2_file_operations vpif_fops = {
1078 .owner = THIS_MODULE,
1079 .open = v4l2_fh_open,
1080 .release = vb2_fop_release,
1081 .unlocked_ioctl = video_ioctl2,
1082 .mmap = vb2_fop_mmap,
1083 .poll = vb2_fop_poll
1086 /*Configure the channels, buffer sizei, request irq */
1087 static int initialize_vpif(void)
1089 int free_channel_objects_index;
1092 /* Allocate memory for six channel objects */
1093 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1095 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1096 /* If memory allocation fails, return error */
1097 if (!vpif_obj.dev[i]) {
1098 free_channel_objects_index = i;
1100 goto vpif_init_free_channel_objects;
1106 vpif_init_free_channel_objects:
1107 for (j = 0; j < free_channel_objects_index; j++)
1108 kfree(vpif_obj.dev[j]);
1112 static void free_vpif_objs(void)
1116 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1117 kfree(vpif_obj.dev[i]);
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;
1193 err = vb2_queue_init(q);
1195 vpif_err("vpif_display: vb2_queue_init() failed\n");
1199 INIT_LIST_HEAD(&common->dma_queue);
1201 /* register video device */
1202 vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
1203 ch, &ch->video_dev);
1205 /* Initialize the video_device structure */
1206 vdev = &ch->video_dev;
1207 strscpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1208 vdev->release = video_device_release_empty;
1209 vdev->fops = &vpif_fops;
1210 vdev->ioctl_ops = &vpif_ioctl_ops;
1211 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1212 vdev->vfl_dir = VFL_DIR_TX;
1214 vdev->lock = &common->lock;
1215 vdev->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
1216 video_set_drvdata(&ch->video_dev, ch);
1217 err = video_register_device(vdev, VFL_TYPE_VIDEO,
1226 for (k = 0; k < j; k++) {
1227 ch = vpif_obj.dev[k];
1228 video_unregister_device(&ch->video_dev);
1233 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1235 return vpif_probe_complete();
1238 static const struct v4l2_async_notifier_operations vpif_async_ops = {
1239 .bound = vpif_async_bound,
1240 .complete = vpif_async_complete,
1244 * vpif_probe: This function creates device entries by register itself to the
1245 * V4L2 driver and initializes fields of each channel objects
1247 static __init int vpif_probe(struct platform_device *pdev)
1249 struct vpif_subdev_info *subdevdata;
1250 struct i2c_adapter *i2c_adap;
1251 struct resource *res;
1256 if (!pdev->dev.platform_data) {
1257 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1261 vpif_dev = &pdev->dev;
1262 err = initialize_vpif();
1265 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1269 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1271 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1275 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1276 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1277 IRQF_SHARED, VPIF_DRIVER_NAME,
1278 (void *)(&vpif_obj.dev[res_idx]->
1282 vpif_err("VPIF IRQ request failed\n");
1283 goto vpif_unregister;
1288 vpif_obj.config = pdev->dev.platform_data;
1289 subdev_count = vpif_obj.config->subdev_count;
1290 subdevdata = vpif_obj.config->subdevinfo;
1291 vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
1294 goto vpif_unregister;
1297 v4l2_async_notifier_init(&vpif_obj.notifier);
1299 if (!vpif_obj.config->asd_sizes) {
1300 i2c_adap = i2c_get_adapter(vpif_obj.config->i2c_adapter_id);
1301 for (i = 0; i < subdev_count; i++) {
1303 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1308 if (!vpif_obj.sd[i]) {
1309 vpif_err("Error registering v4l2 subdevice\n");
1311 goto probe_subdev_out;
1315 vpif_obj.sd[i]->grp_id = 1 << i;
1317 err = vpif_probe_complete();
1319 goto probe_subdev_out;
1322 for (i = 0; i < vpif_obj.config->asd_sizes[0]; i++) {
1323 err = v4l2_async_notifier_add_subdev(
1324 &vpif_obj.notifier, vpif_obj.config->asd[i]);
1329 vpif_obj.notifier.ops = &vpif_async_ops;
1330 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1331 &vpif_obj.notifier);
1333 vpif_err("Error registering async notifier\n");
1342 v4l2_async_notifier_cleanup(&vpif_obj.notifier);
1346 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1354 * vpif_remove: It un-register channels from V4L2 driver
1356 static int vpif_remove(struct platform_device *device)
1358 struct channel_obj *ch;
1361 if (vpif_obj.config->asd_sizes) {
1362 v4l2_async_notifier_unregister(&vpif_obj.notifier);
1363 v4l2_async_notifier_cleanup(&vpif_obj.notifier);
1366 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1369 /* un-register device */
1370 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1371 /* Get the pointer to the channel object */
1372 ch = vpif_obj.dev[i];
1373 /* Unregister video device */
1374 video_unregister_device(&ch->video_dev);
1381 #ifdef CONFIG_PM_SLEEP
1382 static int vpif_suspend(struct device *dev)
1384 struct common_obj *common;
1385 struct channel_obj *ch;
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[VPIF_VIDEO_INDEX];
1393 if (!vb2_start_streaming_called(&common->buffer_queue))
1396 mutex_lock(&common->lock);
1397 /* Disable channel */
1398 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1400 channel2_intr_enable(0);
1402 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1405 channel3_intr_enable(0);
1407 mutex_unlock(&common->lock);
1413 static int vpif_resume(struct device *dev)
1416 struct common_obj *common;
1417 struct channel_obj *ch;
1420 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1421 /* Get the pointer to the channel object */
1422 ch = vpif_obj.dev[i];
1423 common = &ch->common[VPIF_VIDEO_INDEX];
1425 if (!vb2_start_streaming_called(&common->buffer_queue))
1428 mutex_lock(&common->lock);
1429 /* Enable channel */
1430 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1432 channel2_intr_enable(1);
1434 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1437 channel3_intr_enable(1);
1439 mutex_unlock(&common->lock);
1447 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1449 static __refdata struct platform_driver vpif_driver = {
1451 .name = VPIF_DRIVER_NAME,
1454 .probe = vpif_probe,
1455 .remove = vpif_remove,
1458 module_platform_driver(vpif_driver);