blob: b5ac6ce626b3c664ab7ac9b1852ac327c6663721 [file] [log] [blame]
Chaithrika U Se7332e32009-06-09 05:55:37 -03001/*
2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
4 *
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
Lad, Prabhakar96787eb2014-05-16 10:33:55 -03006 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
Chaithrika U Se7332e32009-06-09 05:55:37 -03007 *
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.
11 *
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.
16 */
17
Chaithrika U Se7332e32009-06-09 05:55:37 -030018#include <linux/interrupt.h>
Lad, Prabhakar012eef72013-04-19 05:53:29 -030019#include <linux/module.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030020#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030022
Lad, Prabhakar012eef72013-04-19 05:53:29 -030023#include <media/v4l2-ioctl.h>
Chaithrika U Se7332e32009-06-09 05:55:37 -030024
Chaithrika U Se7332e32009-06-09 05:55:37 -030025#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030026#include "vpif_display.h"
Chaithrika U Se7332e32009-06-09 05:55:37 -030027
28MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
29MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030030MODULE_VERSION(VPIF_DISPLAY_VERSION);
Chaithrika U Se7332e32009-06-09 05:55:37 -030031
Manjunath Hadli0a631722012-04-13 04:44:00 -030032#define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
Chaithrika U Se7332e32009-06-09 05:55:37 -030033
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)
37
38static int debug = 1;
Chaithrika U Se7332e32009-06-09 05:55:37 -030039
40module_param(debug, int, 0644);
Chaithrika U Se7332e32009-06-09 05:55:37 -030041
42MODULE_PARM_DESC(debug, "Debug level 0-1");
Chaithrika U Se7332e32009-06-09 05:55:37 -030043
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -030044#define VPIF_DRIVER_NAME "vpif_display"
Kevin Hilmanbff782d2016-12-16 22:47:54 -020045MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -030046
47/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
48static int ycmux_mode;
49
Lad, Prabhakar58334b02014-05-16 10:33:13 -030050static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
51
Chaithrika U Se7332e32009-06-09 05:55:37 -030052static struct vpif_device vpif_obj = { {NULL} };
53static struct device *vpif_dev;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030054static void vpif_calculate_offsets(struct channel_obj *ch);
55static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Chaithrika U Se7332e32009-06-09 05:55:37 -030056
Junghak Sung2d700712015-09-22 10:30:30 -030057static inline
58struct vpif_disp_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
Lad, Prabhakar58334b02014-05-16 10:33:13 -030059{
60 return container_of(vb, struct vpif_disp_buffer, vb);
61}
62
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030063/**
64 * vpif_buffer_prepare : callback function for buffer prepare
65 * @vb: ptr to vb2_buffer
66 *
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
Chaithrika U Se7332e32009-06-09 05:55:37 -030070 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030071static int vpif_buffer_prepare(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -030072{
Junghak Sung2d700712015-09-22 10:30:30 -030073 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030074 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -030075 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -030076
Lad, Prabhakara2b235c2014-05-16 10:33:06 -030077 common = &ch->common[VPIF_VIDEO_INDEX];
Chaithrika U Se7332e32009-06-09 05:55:37 -030078
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030079 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
80 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
81 return -EINVAL;
82
Junghak Sung2d700712015-09-22 10:30:30 -030083 vbuf->field = common->fmt.fmt.pix.field;
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030084
85 if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
86 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
87
88 if (!ISALIGNED(addr + common->ytop_off) ||
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030089 !ISALIGNED(addr + common->ybtm_off) ||
90 !ISALIGNED(addr + common->ctop_off) ||
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030091 !ISALIGNED(addr + common->cbtm_off)) {
92 vpif_err("buffer offset not aligned to 8 bytes\n");
93 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -030094 }
Chaithrika U Se7332e32009-06-09 05:55:37 -030095 }
Chaithrika U Se7332e32009-06-09 05:55:37 -030096
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030097 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -030098}
99
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300100/**
101 * vpif_buffer_queue_setup : Callback function for buffer setup.
102 * @vq: vb2_queue ptr
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300103 * @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.
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300106 * @alloc_devs: ptr to allocation context
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300107 *
108 * This callback function is called when reqbuf() is called to adjust
109 * the buffer count and buffer size
Chaithrika U Se7332e32009-06-09 05:55:37 -0300110 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300111static int vpif_buffer_queue_setup(struct vb2_queue *vq,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300112 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300113 unsigned int sizes[], struct device *alloc_devs[])
Chaithrika U Se7332e32009-06-09 05:55:37 -0300114{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300115 struct channel_obj *ch = vb2_get_drv_priv(vq);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300116 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200117 unsigned size = common->fmt.fmt.pix.sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300118
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200119 if (*nplanes) {
120 if (sizes[0] < size)
121 return -EINVAL;
122 size = sizes[0];
123 }
Manjunath Hadlifc613d42012-04-13 04:49:10 -0300124
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300125 if (vq->num_buffers + *nbuffers < 3)
126 *nbuffers = 3 - vq->num_buffers;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300127
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300128 *nplanes = 1;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200129 sizes[0] = size;
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300130
131 /* Calculate the offset for Y and C data in the buffer */
132 vpif_calculate_offsets(ch);
133
Chaithrika U Se7332e32009-06-09 05:55:37 -0300134 return 0;
135}
136
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300137/**
138 * vpif_buffer_queue : Callback function to add buffer to DMA queue
139 * @vb: ptr to vb2_buffer
140 *
141 * This callback fucntion queues the buffer to DMA engine
Chaithrika U Se7332e32009-06-09 05:55:37 -0300142 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300143static void vpif_buffer_queue(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300144{
Junghak Sung2d700712015-09-22 10:30:30 -0300145 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
146 struct vpif_disp_buffer *buf = to_vpif_buffer(vbuf);
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300147 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300148 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300149 unsigned long flags;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300150
151 common = &ch->common[VPIF_VIDEO_INDEX];
152
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300153 /* add the buffer to the DMA queue */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300154 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300155 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300156 spin_unlock_irqrestore(&common->irqlock, flags);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300157}
158
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300159/**
160 * vpif_start_streaming : Starts the DMA engine for streaming
161 * @vb: ptr to vb2_buffer
162 * @count: number of buffers
163 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300164static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
165{
166 struct vpif_display_config *vpif_config_data =
167 vpif_dev->platform_data;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300168 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300169 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
170 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300171 struct vpif_disp_buffer *buf, *tmp;
172 unsigned long addr, flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300173 int ret;
174
Hans Verkuilc4697d72012-11-16 12:03:07 -0300175 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300176
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300177 /* Initialize field_id */
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300178 ch->field_id = 0;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300179
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300180 /* clock settings */
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);
184 if (ret < 0) {
185 vpif_err("can't set clock\n");
186 goto err;
187 }
188 }
189
190 /* set the parameters and addresses */
191 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
192 if (ret < 0)
193 goto err;
194
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300195 ycmux_mode = ret;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300196 vpif_config_addr(ch, ret);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300197 /* 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);
201
202 list_del(&common->cur_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300203 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300204
Junghak Sung2d700712015-09-22 10:30:30 -0300205 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300206 common->set_addr((addr + common->ytop_off),
207 (addr + common->ybtm_off),
208 (addr + common->ctop_off),
209 (addr + common->cbtm_off));
210
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300211 /*
212 * Set interrupt for both the fields in VPIF
213 * Register enable channel in VPIF register
214 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300215 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300216 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
217 channel2_intr_assert();
218 channel2_intr_enable(1);
219 enable_channel2(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300220 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300221 channel2_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300222 }
223
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300224 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300225 channel3_intr_assert();
226 channel3_intr_enable(1);
227 enable_channel3(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300228 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300229 channel3_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300230 }
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300231
232 return 0;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300233
234err:
235 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
236 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300237 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300238 }
Dan Carpenter92491962014-06-12 04:01:45 -0300239 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300240
241 return ret;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300242}
243
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300244/**
245 * vpif_stop_streaming : Stop the DMA engine
246 * @vq: ptr to vb2_queue
247 *
248 * This callback stops the DMA engine and any remaining buffers
249 * in the DMA queue are released.
250 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300251static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300252{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300253 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300254 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300255 unsigned long flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300256
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300257 common = &ch->common[VPIF_VIDEO_INDEX];
258
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300259 /* Disable channel */
260 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
261 enable_channel2(0);
262 channel2_intr_enable(0);
263 }
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300264 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300265 enable_channel3(0);
266 channel3_intr_enable(0);
267 }
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300268
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300269 /* release all active buffers */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300270 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300271 if (common->cur_frm == common->next_frm) {
Junghak Sung2d700712015-09-22 10:30:30 -0300272 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
273 VB2_BUF_STATE_ERROR);
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300274 } else {
Markus Elfring396c88e2016-10-12 10:40:32 -0300275 if (common->cur_frm)
Junghak Sung2d700712015-09-22 10:30:30 -0300276 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300277 VB2_BUF_STATE_ERROR);
Markus Elfring396c88e2016-10-12 10:40:32 -0300278 if (common->next_frm)
Junghak Sung2d700712015-09-22 10:30:30 -0300279 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300280 VB2_BUF_STATE_ERROR);
281 }
282
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300283 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);
Junghak Sung2d700712015-09-22 10:30:30 -0300287 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
288 VB2_BUF_STATE_ERROR);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300289 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300290 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300291}
292
293static struct vb2_ops video_qops = {
294 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -0300295 .wait_prepare = vb2_ops_wait_prepare,
296 .wait_finish = vb2_ops_wait_finish,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300297 .buf_prepare = vpif_buffer_prepare,
298 .start_streaming = vpif_start_streaming,
299 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300300 .buf_queue = vpif_buffer_queue,
301};
302
Chaithrika U Se7332e32009-06-09 05:55:37 -0300303static void process_progressive_mode(struct common_obj *common)
304{
Markus Elfringea0e4372016-10-12 10:45:03 -0300305 unsigned long addr;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300306
Hans Verkuilc4697d72012-11-16 12:03:07 -0300307 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300308 /* Get the next buffer from buffer queue */
309 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300310 struct vpif_disp_buffer, list);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300311 /* Remove that buffer from the buffer queue */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300312 list_del(&common->next_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300313 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300314
315 /* Set top and bottom field addrs in VPIF registers */
Junghak Sung2d700712015-09-22 10:30:30 -0300316 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300317 common->set_addr(addr + common->ytop_off,
318 addr + common->ybtm_off,
319 addr + common->ctop_off,
320 addr + common->cbtm_off);
321}
322
323static void process_interlaced_mode(int fid, struct common_obj *common)
324{
325 /* device field id and local field id are in sync */
326 /* If this is even field */
327 if (0 == fid) {
328 if (common->cur_frm == common->next_frm)
329 return;
330
331 /* one frame is displayed If next frame is
332 * available, release cur_frm and move on */
333 /* Copy frame display time */
Junghak Sungd6dd6452015-11-03 08:16:37 -0200334 common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
Chaithrika U Se7332e32009-06-09 05:55:37 -0300335 /* Change status of the cur_frm */
Junghak Sung2d700712015-09-22 10:30:30 -0300336 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
337 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300338 /* Make cur_frm pointing to next_frm */
339 common->cur_frm = common->next_frm;
340
341 } else if (1 == fid) { /* odd field */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300342 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300343 if (list_empty(&common->dma_queue)
344 || (common->cur_frm != common->next_frm)) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300345 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300346 return;
347 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300348 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300349 /* one field is displayed configure the next
350 * frame if it is available else hold on current
351 * frame */
352 /* Get next from the buffer queue */
353 process_progressive_mode(common);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300354 }
355}
356
357/*
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
360 */
361static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
362{
363 struct vpif_device *dev = &vpif_obj;
364 struct channel_obj *ch;
365 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300366 int fid = -1, i;
Markus Elfring3ce5f662016-10-12 10:43:12 -0300367 int channel_id;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300368
369 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300370 if (!vpif_intr_status(channel_id + 2))
371 return IRQ_NONE;
372
Chaithrika U Se7332e32009-06-09 05:55:37 -0300373 ch = dev->dev[channel_id];
Chaithrika U Se7332e32009-06-09 05:55:37 -0300374 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
375 common = &ch->common[i];
376 /* If streaming is started in this channel */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300377
378 if (1 == ch->vpifparams.std_info.frm_fmt) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300379 spin_lock(&common->irqlock);
380 if (list_empty(&common->dma_queue)) {
381 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300382 continue;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300383 }
384 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300385
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 */
Junghak Sungd6dd6452015-11-03 08:16:37 -0200390 common->cur_frm->vb.vb2_buf.timestamp =
391 ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300392 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
393 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300394 /* Make cur_frm pointing to next_frm */
395 common->cur_frm = common->next_frm;
396 }
397
398 channel_first_int[i][channel_id] = 0;
399 process_progressive_mode(common);
400 } else {
401 /* Interlaced mode */
402 /* If it is first interrupt, ignore it */
403
404 if (channel_first_int[i][channel_id]) {
405 channel_first_int[i][channel_id] = 0;
406 continue;
407 }
408
409 if (0 == i) {
410 ch->field_id ^= 1;
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 */
416 if (0 == fid)
417 ch->field_id = fid;
418
419 return IRQ_HANDLED;
420 }
421 }
422 process_interlaced_mode(fid, common);
423 }
424 }
425
426 return IRQ_HANDLED;
427}
428
Mats Randgaardc027e162010-12-16 12:17:44 -0300429static int vpif_update_std_info(struct channel_obj *ch)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300430{
Chaithrika U Se7332e32009-06-09 05:55:37 -0300431 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;
435
Mats Randgaardc027e162010-12-16 12:17:44 -0300436 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300437
Mats Randgaardc027e162010-12-16 12:17:44 -0300438 for (i = 0; i < vpif_ch_params_count; i++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300439 config = &vpif_ch_params[i];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300440 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));
444 break;
445 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300446 }
447 }
448
Mats Randgaardc027e162010-12-16 12:17:44 -0300449 if (i == vpif_ch_params_count) {
450 vpif_dbg(1, debug, "Format not found\n");
Mats Randgaardaa444402010-12-16 12:17:42 -0300451 return -EINVAL;
Mats Randgaardc027e162010-12-16 12:17:44 -0300452 }
453
454 return 0;
455}
456
457static int vpif_update_resolution(struct channel_obj *ch)
458{
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;
463
Hans Verkuil0598c172012-09-18 07:18:47 -0300464 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
Mats Randgaardc027e162010-12-16 12:17:44 -0300465 return -EINVAL;
466
Hans Verkuil0598c172012-09-18 07:18:47 -0300467 if (vid_ch->stdid) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300468 if (vpif_update_std_info(ch))
469 return -EINVAL;
470 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300471
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300472 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300473 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);
477
478 /* Set height and width paramateres */
Mats Randgaardc027e162010-12-16 12:17:44 -0300479 common->height = std_info->height;
480 common->width = std_info->width;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300481 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
482
483 if (vid_ch->stdid)
484 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
485 else
486 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
487
488 if (ch->vpifparams.std_info.frm_fmt)
489 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
490 else
491 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300492
493 return 0;
494}
495
496/*
497 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
498 * in the top and bottom field
499 */
500static void vpif_calculate_offsets(struct channel_obj *ch)
501{
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;
Mauro Carvalho Chehaba4f20e22014-08-21 15:46:46 -0500506 unsigned int hpitch, sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300507
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;
511 else
512 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
513 } else {
514 vid_ch->buf_field = common->fmt.fmt.pix.field;
515 }
516
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300517 sizeimage = common->fmt.fmt.pix.sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300518
519 hpitch = common->fmt.fmt.pix.bytesperline;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300520 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;
536 }
537
538 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
539 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
540 vpifparams->video_params.storage_mode = 1;
541 } else {
542 vpifparams->video_params.storage_mode = 0;
543 }
544
545 if (ch->vpifparams.std_info.frm_fmt == 1) {
546 vpifparams->video_params.hpitch =
547 common->fmt.fmt.pix.bytesperline;
548 } else {
549 if ((field == V4L2_FIELD_ANY) ||
550 (field == V4L2_FIELD_INTERLACED))
551 vpifparams->video_params.hpitch =
552 common->fmt.fmt.pix.bytesperline * 2;
553 else
554 vpifparams->video_params.hpitch =
555 common->fmt.fmt.pix.bytesperline;
556 }
557
558 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
559}
560
Chaithrika U Se7332e32009-06-09 05:55:37 -0300561static void vpif_config_addr(struct channel_obj *ch, int muxmode)
562{
563 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
564
565 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
566 common->set_addr = ch3_set_videobuf_addr;
567 } else {
568 if (2 == muxmode)
569 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
570 else
571 common->set_addr = ch2_set_videobuf_addr;
572 }
573}
574
Chaithrika U Se7332e32009-06-09 05:55:37 -0300575/* functions implementing ioctls */
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300576/**
577 * vpif_querycap() - QUERYCAP handler
578 * @file: file ptr
579 * @priv: file handle
580 * @cap: ptr to v4l2_capability structure
581 */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300582static int vpif_querycap(struct file *file, void *priv,
583 struct v4l2_capability *cap)
584{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300585 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300586
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300587 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
588 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -0300589 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300590 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
591 dev_name(vpif_dev));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300592 strlcpy(cap->card, config->card_name, sizeof(cap->card));
593
594 return 0;
595}
596
597static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
598 struct v4l2_fmtdesc *fmt)
599{
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300600 if (fmt->index != 0)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300601 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300602
603 /* Fill in the information about format */
604 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
605 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
606 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300607 fmt->flags = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300608 return 0;
609}
610
611static int vpif_g_fmt_vid_out(struct file *file, void *priv,
612 struct v4l2_format *fmt)
613{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300614 struct video_device *vdev = video_devdata(file);
615 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300616 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
617
618 /* Check the validity of the buffer type */
619 if (common->fmt.type != fmt->type)
620 return -EINVAL;
621
Mats Randgaardc027e162010-12-16 12:17:44 -0300622 if (vpif_update_resolution(ch))
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300623 return -EINVAL;
624 *fmt = common->fmt;
625 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300626}
627
Chaithrika U Se7332e32009-06-09 05:55:37 -0300628static int vpif_try_fmt_vid_out(struct file *file, void *priv,
629 struct v4l2_format *fmt)
630{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300631 struct video_device *vdev = video_devdata(file);
632 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300633 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
634 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300635
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300636 /*
637 * to supress v4l-compliance warnings silently correct
638 * the pixelformat
639 */
640 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
641 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300642
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300643 if (vpif_update_resolution(ch))
644 return -EINVAL;
645
646 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
647 pixfmt->field = common->fmt.fmt.pix.field;
648 pixfmt->bytesperline = common->fmt.fmt.pix.width;
649 pixfmt->width = common->fmt.fmt.pix.width;
650 pixfmt->height = common->fmt.fmt.pix.height;
651 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300652
653 return 0;
654}
655
656static int vpif_s_fmt_vid_out(struct file *file, void *priv,
657 struct v4l2_format *fmt)
658{
659 struct video_device *vdev = video_devdata(file);
660 struct channel_obj *ch = video_get_drvdata(vdev);
661 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
662 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
663 int ret;
664
665 if (vb2_is_busy(&common->buffer_queue))
666 return -EBUSY;
667
668 ret = vpif_try_fmt_vid_out(file, priv, fmt);
669 if (ret)
670 return ret;
671
672 /* store the pix format in the channel object */
673 common->fmt.fmt.pix = *pixfmt;
674
675 /* store the format in the channel object */
676 common->fmt = *fmt;
677 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300678}
679
Hans Verkuil314527a2013-03-15 06:10:40 -0300680static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300681{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300682 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300683 struct video_device *vdev = video_devdata(file);
684 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300685 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300686 struct vpif_display_chan_config *chan_cfg;
687 struct v4l2_output output;
688 int ret;
689
Markus Elfring396c88e2016-10-12 10:40:32 -0300690 if (!config->chan_config[ch->channel_id].outputs)
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300691 return -ENODATA;
692
693 chan_cfg = &config->chan_config[ch->channel_id];
694 output = chan_cfg->outputs[ch->output_idx].output;
695 if (output.capabilities != V4L2_OUT_CAP_STD)
696 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300697
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300698 if (vb2_is_busy(&common->buffer_queue))
699 return -EBUSY;
700
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300701
Hans Verkuil314527a2013-03-15 06:10:40 -0300702 if (!(std_id & VPIF_V4L2_STD))
Chaithrika U Se7332e32009-06-09 05:55:37 -0300703 return -EINVAL;
704
Chaithrika U Se7332e32009-06-09 05:55:37 -0300705 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300706 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300707 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300708 /* Get the information about the standard */
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300709 if (vpif_update_resolution(ch))
710 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300711
Chaithrika U Se7332e32009-06-09 05:55:37 -0300712 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300713
714 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300715 s_std_output, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300716 if (ret < 0) {
717 vpif_err("Failed to set output standard\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300718 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300719 }
720
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300721 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300722 s_std, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300723 if (ret < 0)
724 vpif_err("Failed to set standard for sub devices\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -0300725 return ret;
726}
727
728static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
729{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300730 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300731 struct video_device *vdev = video_devdata(file);
732 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300733 struct vpif_display_chan_config *chan_cfg;
734 struct v4l2_output output;
735
Markus Elfring396c88e2016-10-12 10:40:32 -0300736 if (!config->chan_config[ch->channel_id].outputs)
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300737 return -ENODATA;
738
739 chan_cfg = &config->chan_config[ch->channel_id];
740 output = chan_cfg->outputs[ch->output_idx].output;
741 if (output.capabilities != V4L2_OUT_CAP_STD)
742 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300743
744 *std = ch->video.stdid;
745 return 0;
746}
747
Chaithrika U Se7332e32009-06-09 05:55:37 -0300748static int vpif_enum_output(struct file *file, void *fh,
749 struct v4l2_output *output)
750{
751
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300752 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300753 struct video_device *vdev = video_devdata(file);
754 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300755 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300756
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300757 chan_cfg = &config->chan_config[ch->channel_id];
758 if (output->index >= chan_cfg->output_count) {
Chaithrika U Se7332e32009-06-09 05:55:37 -0300759 vpif_dbg(1, debug, "Invalid output index\n");
760 return -EINVAL;
761 }
762
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300763 *output = chan_cfg->outputs[output->index].output;
764 return 0;
765}
Chaithrika U Se7332e32009-06-09 05:55:37 -0300766
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300767/**
768 * vpif_output_to_subdev() - Maps output to sub device
769 * @vpif_cfg - global config ptr
770 * @chan_cfg - channel config ptr
771 * @index - Given output index from application
772 *
773 * lookup the sub device information for a given output index.
774 * we report all the output to application. output table also
775 * has sub device name for the each output
776 */
777static int
778vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
779 struct vpif_display_chan_config *chan_cfg, int index)
780{
781 struct vpif_subdev_info *subdev_info;
782 const char *subdev_name;
783 int i;
784
785 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
786
Markus Elfring396c88e2016-10-12 10:40:32 -0300787 if (!chan_cfg->outputs)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300788 return -1;
789
790 subdev_name = chan_cfg->outputs[index].subdev_name;
Markus Elfring396c88e2016-10-12 10:40:32 -0300791 if (!subdev_name)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300792 return -1;
793
794 /* loop through the sub device list to get the sub device info */
795 for (i = 0; i < vpif_cfg->subdev_count; i++) {
796 subdev_info = &vpif_cfg->subdevinfo[i];
797 if (!strcmp(subdev_info->name, subdev_name))
798 return i;
799 }
800 return -1;
801}
802
803/**
804 * vpif_set_output() - Select an output
805 * @vpif_cfg - global config ptr
806 * @ch - channel
807 * @index - Given output index from application
808 *
809 * Select the given output.
810 */
811static int vpif_set_output(struct vpif_display_config *vpif_cfg,
812 struct channel_obj *ch, int index)
813{
814 struct vpif_display_chan_config *chan_cfg =
815 &vpif_cfg->chan_config[ch->channel_id];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300816 struct v4l2_subdev *sd = NULL;
817 u32 input = 0, output = 0;
818 int sd_index;
819 int ret;
820
821 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
Mauro Carvalho Chehaba4f20e22014-08-21 15:46:46 -0500822 if (sd_index >= 0)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300823 sd = vpif_obj.sd[sd_index];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300824
825 if (sd) {
826 input = chan_cfg->outputs[index].input_route;
827 output = chan_cfg->outputs[index].output_route;
828 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
829 if (ret < 0 && ret != -ENOIOCTLCMD) {
830 vpif_err("Failed to set output\n");
831 return ret;
832 }
833
834 }
835 ch->output_idx = index;
836 ch->sd = sd;
Markus Elfring396c88e2016-10-12 10:40:32 -0300837 if (chan_cfg->outputs)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300838 /* update tvnorms from the sub device output info */
Lad, Prabhakare933c6b2015-03-08 18:57:24 -0300839 ch->video_dev.tvnorms = chan_cfg->outputs[index].output.std;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300840 return 0;
841}
842
843static int vpif_s_output(struct file *file, void *priv, unsigned int i)
844{
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300845 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300846 struct video_device *vdev = video_devdata(file);
847 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300848 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300849 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300850
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300851 if (vb2_is_busy(&common->buffer_queue))
852 return -EBUSY;
853
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300854 chan_cfg = &config->chan_config[ch->channel_id];
855
856 if (i >= chan_cfg->output_count)
857 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300858
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300859 return vpif_set_output(config, ch, i);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300860}
861
862static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
863{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300864 struct video_device *vdev = video_devdata(file);
865 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300866
Hans Verkuil311673e2012-09-20 09:06:23 -0300867 *i = ch->output_idx;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300868
869 return 0;
870}
871
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300872/**
Hans Verkuil0598c172012-09-18 07:18:47 -0300873 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300874 * @file: file ptr
875 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -0300876 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300877 */
Hans Verkuil0598c172012-09-18 07:18:47 -0300878static int
879vpif_enum_dv_timings(struct file *file, void *priv,
880 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300881{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300882 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300883 struct video_device *vdev = video_devdata(file);
884 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300885 struct vpif_display_chan_config *chan_cfg;
886 struct v4l2_output output;
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300887 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300888
Markus Elfring396c88e2016-10-12 10:40:32 -0300889 if (!config->chan_config[ch->channel_id].outputs)
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300890 return -ENODATA;
891
892 chan_cfg = &config->chan_config[ch->channel_id];
893 output = chan_cfg->outputs[ch->output_idx].output;
894 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
895 return -ENODATA;
896
Laurent Pinchart10d21462014-01-31 09:04:19 -0300897 timings->pad = 0;
898
899 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjun63af4af2012-10-30 09:49:38 -0300900 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300901 return -EINVAL;
902 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300903}
904
905/**
Mats Randgaardc027e162010-12-16 12:17:44 -0300906 * vpif_s_dv_timings() - S_DV_TIMINGS handler
907 * @file: file ptr
908 * @priv: file handle
909 * @timings: digital video timings
910 */
911static int vpif_s_dv_timings(struct file *file, void *priv,
912 struct v4l2_dv_timings *timings)
913{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300914 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300915 struct video_device *vdev = video_devdata(file);
916 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -0300917 struct vpif_params *vpifparams = &ch->vpifparams;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300918 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -0300919 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
920 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -0300921 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300922 struct vpif_display_chan_config *chan_cfg;
923 struct v4l2_output output;
Mats Randgaardc027e162010-12-16 12:17:44 -0300924 int ret;
925
Markus Elfring396c88e2016-10-12 10:40:32 -0300926 if (!config->chan_config[ch->channel_id].outputs)
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300927 return -ENODATA;
928
929 chan_cfg = &config->chan_config[ch->channel_id];
930 output = chan_cfg->outputs[ch->output_idx].output;
931 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
932 return -ENODATA;
933
934 if (vb2_is_busy(&common->buffer_queue))
935 return -EBUSY;
936
Mats Randgaardc027e162010-12-16 12:17:44 -0300937 if (timings->type != V4L2_DV_BT_656_1120) {
938 vpif_dbg(2, debug, "Timing type not defined\n");
939 return -EINVAL;
940 }
941
942 /* Configure subdevice timings, if any */
Hans Verkuil882084a2012-09-20 09:06:31 -0300943 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300944 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
945 ret = 0;
946 if (ret < 0) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300947 vpif_dbg(2, debug, "Error setting custom DV timings\n");
948 return ret;
949 }
950
951 if (!(timings->bt.width && timings->bt.height &&
952 (timings->bt.hbackporch ||
953 timings->bt.hfrontporch ||
954 timings->bt.hsync) &&
955 timings->bt.vfrontporch &&
956 (timings->bt.vbackporch ||
957 timings->bt.vsync))) {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -0200958 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");
Mats Randgaardc027e162010-12-16 12:17:44 -0300959 return -EINVAL;
960 }
961
Hans Verkuil0598c172012-09-18 07:18:47 -0300962 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -0300963
964 /* Configure video port timings */
965
Hans Verkuile3655262013-07-29 08:41:00 -0300966 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -0300967 std_info->sav2eav = bt->width;
968
969 std_info->l1 = 1;
970 std_info->l3 = bt->vsync + bt->vbackporch + 1;
971
Hans Verkuile3655262013-07-29 08:41:00 -0300972 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -0300973 if (bt->interlaced) {
974 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300975 std_info->l5 = std_info->vsize/2 -
976 (bt->vfrontporch - 1);
977 std_info->l7 = std_info->vsize/2 + 1;
978 std_info->l9 = std_info->l7 + bt->il_vsync +
979 bt->il_vbackporch + 1;
980 std_info->l11 = std_info->vsize -
981 (bt->il_vfrontporch - 1);
982 } else {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -0200983 vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
Mats Randgaardc027e162010-12-16 12:17:44 -0300984 return -EINVAL;
985 }
986 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -0300987 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
988 }
989 strncpy(std_info->name, "Custom timings BT656/1120",
990 VPIF_MAX_NAME);
991 std_info->width = bt->width;
992 std_info->height = bt->height;
993 std_info->frm_fmt = bt->interlaced ? 0 : 1;
994 std_info->ycmux_mode = 0;
995 std_info->capture_format = 0;
996 std_info->vbi_supported = 0;
997 std_info->hd_sd = 1;
998 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -0300999 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001000
1001 return 0;
1002}
1003
1004/**
1005 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1006 * @file: file ptr
1007 * @priv: file handle
1008 * @timings: digital video timings
1009 */
1010static int vpif_g_dv_timings(struct file *file, void *priv,
1011 struct v4l2_dv_timings *timings)
1012{
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001013 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001014 struct video_device *vdev = video_devdata(file);
1015 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001016 struct vpif_display_chan_config *chan_cfg;
Mats Randgaardc027e162010-12-16 12:17:44 -03001017 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001018 struct v4l2_output output;
1019
Markus Elfring396c88e2016-10-12 10:40:32 -03001020 if (!config->chan_config[ch->channel_id].outputs)
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001021 goto error;
1022
1023 chan_cfg = &config->chan_config[ch->channel_id];
1024 output = chan_cfg->outputs[ch->output_idx].output;
1025
1026 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1027 goto error;
Mats Randgaardc027e162010-12-16 12:17:44 -03001028
Hans Verkuil0598c172012-09-18 07:18:47 -03001029 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001030
1031 return 0;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001032error:
1033 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001034}
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001035
1036/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001037 * vpif_log_status() - Status information
1038 * @file: file ptr
1039 * @priv: file handle
1040 *
1041 * Returns zero.
1042 */
1043static int vpif_log_status(struct file *filep, void *priv)
1044{
1045 /* status for sub devices */
1046 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1047
1048 return 0;
1049}
1050
Chaithrika U Se7332e32009-06-09 05:55:37 -03001051/* vpif display ioctl operations */
1052static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001053 .vidioc_querycap = vpif_querycap,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001054 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001055 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1056 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1057 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
Lad, Prabhakarb92cde12014-05-16 10:33:16 -03001058
1059 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1060 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1061 .vidioc_querybuf = vb2_ioctl_querybuf,
1062 .vidioc_qbuf = vb2_ioctl_qbuf,
1063 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1064 .vidioc_expbuf = vb2_ioctl_expbuf,
1065 .vidioc_streamon = vb2_ioctl_streamon,
1066 .vidioc_streamoff = vb2_ioctl_streamoff,
1067
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001068 .vidioc_s_std = vpif_s_std,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001069 .vidioc_g_std = vpif_g_std,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001070
Chaithrika U Se7332e32009-06-09 05:55:37 -03001071 .vidioc_enum_output = vpif_enum_output,
1072 .vidioc_s_output = vpif_s_output,
1073 .vidioc_g_output = vpif_g_output,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001074
1075 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1076 .vidioc_s_dv_timings = vpif_s_dv_timings,
1077 .vidioc_g_dv_timings = vpif_g_dv_timings,
1078
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001079 .vidioc_log_status = vpif_log_status,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001080};
1081
1082static const struct v4l2_file_operations vpif_fops = {
1083 .owner = THIS_MODULE,
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001084 .open = v4l2_fh_open,
1085 .release = vb2_fop_release,
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001086 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001087 .mmap = vb2_fop_mmap,
1088 .poll = vb2_fop_poll
Chaithrika U Se7332e32009-06-09 05:55:37 -03001089};
1090
Chaithrika U Se7332e32009-06-09 05:55:37 -03001091/*Configure the channels, buffer sizei, request irq */
1092static int initialize_vpif(void)
1093{
1094 int free_channel_objects_index;
Lad, Prabhakard5b94b92014-05-16 10:33:23 -03001095 int err, i, j;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001096
1097 /* Allocate memory for six channel objects */
1098 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1099 vpif_obj.dev[i] =
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001100 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001101 /* If memory allocation fails, return error */
1102 if (!vpif_obj.dev[i]) {
1103 free_channel_objects_index = i;
1104 err = -ENOMEM;
1105 goto vpif_init_free_channel_objects;
1106 }
1107 }
1108
Chaithrika U Se7332e32009-06-09 05:55:37 -03001109 return 0;
1110
1111vpif_init_free_channel_objects:
1112 for (j = 0; j < free_channel_objects_index; j++)
1113 kfree(vpif_obj.dev[j]);
1114 return err;
1115}
1116
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001117static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1118 struct v4l2_subdev *subdev,
1119 struct v4l2_async_subdev *asd)
1120{
1121 int i;
1122
1123 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1124 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1125 subdev->name)) {
1126 vpif_obj.sd[i] = subdev;
1127 vpif_obj.sd[i]->grp_id = 1 << i;
1128 return 0;
1129 }
1130
1131 return -EINVAL;
1132}
1133
1134static int vpif_probe_complete(void)
1135{
1136 struct common_obj *common;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001137 struct video_device *vdev;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001138 struct channel_obj *ch;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001139 struct vb2_queue *q;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001140 int j, err, k;
1141
1142 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1143 ch = vpif_obj.dev[j];
1144 /* Initialize field of the channel objects */
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001145 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001146 common = &ch->common[k];
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001147 spin_lock_init(&common->irqlock);
1148 mutex_init(&common->lock);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001149 common->set_addr = NULL;
1150 common->ytop_off = 0;
1151 common->ybtm_off = 0;
1152 common->ctop_off = 0;
1153 common->cbtm_off = 0;
1154 common->cur_frm = NULL;
1155 common->next_frm = NULL;
1156 memset(&common->fmt, 0, sizeof(common->fmt));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001157 }
1158 ch->initialized = 0;
1159 if (vpif_obj.config->subdev_count)
1160 ch->sd = vpif_obj.sd[0];
1161 ch->channel_id = j;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001162
1163 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1164
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001165 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1166 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001167
1168 /* select output 0 */
1169 err = vpif_set_output(vpif_obj.config, ch, 0);
1170 if (err)
1171 goto probe_out;
1172
Lad, Prabhakar9cba6532014-05-16 10:33:30 -03001173 /* set initial format */
1174 ch->video.stdid = V4L2_STD_525_60;
1175 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1176 vpif_update_resolution(ch);
1177
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001178 /* Initialize vb2 queue */
1179 q = &common->buffer_queue;
1180 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1181 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1182 q->drv_priv = ch;
1183 q->ops = &video_qops;
1184 q->mem_ops = &vb2_dma_contig_memops;
1185 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1186 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1187 q->min_buffers_needed = 1;
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -03001188 q->lock = &common->lock;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001189 q->dev = vpif_dev;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001190 err = vb2_queue_init(q);
1191 if (err) {
1192 vpif_err("vpif_display: vb2_queue_init() failed\n");
1193 goto probe_out;
1194 }
1195
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001196 INIT_LIST_HEAD(&common->dma_queue);
1197
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001198 /* register video device */
Mauro Carvalho Chehab212bdba2014-08-22 06:38:14 -05001199 vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
1200 ch, &ch->video_dev);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001201
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001202 /* Initialize the video_device structure */
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001203 vdev = &ch->video_dev;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001204 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001205 vdev->release = video_device_release_empty;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001206 vdev->fops = &vpif_fops;
1207 vdev->ioctl_ops = &vpif_ioctl_ops;
1208 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1209 vdev->vfl_dir = VFL_DIR_TX;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001210 vdev->queue = q;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001211 vdev->lock = &common->lock;
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001212 video_set_drvdata(&ch->video_dev, ch);
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001213 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1214 (j ? 3 : 2));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001215 if (err < 0)
1216 goto probe_out;
1217 }
1218
1219 return 0;
1220
1221probe_out:
1222 for (k = 0; k < j; k++) {
1223 ch = vpif_obj.dev[k];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001224 common = &ch->common[k];
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001225 video_unregister_device(&ch->video_dev);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001226 }
1227 return err;
1228}
1229
1230static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1231{
1232 return vpif_probe_complete();
1233}
1234
Chaithrika U Se7332e32009-06-09 05:55:37 -03001235/*
1236 * vpif_probe: This function creates device entries by register itself to the
1237 * V4L2 driver and initializes fields of each channel objects
1238 */
1239static __init int vpif_probe(struct platform_device *pdev)
1240{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001241 struct vpif_subdev_info *subdevdata;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001242 struct i2c_adapter *i2c_adap;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001243 struct resource *res;
1244 int subdev_count;
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001245 int res_idx = 0;
1246 int i, err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001247
Kevin Hilmanbff782d2016-12-16 22:47:54 -02001248 if (!pdev->dev.platform_data) {
1249 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1250 return -EINVAL;
1251 }
1252
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -03001253 if (!pdev->dev.platform_data) {
1254 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1255 return -EINVAL;
1256 }
1257
Chaithrika U Se7332e32009-06-09 05:55:37 -03001258 vpif_dev = &pdev->dev;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001259 err = initialize_vpif();
1260
1261 if (err) {
1262 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1263 return err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001264 }
1265
Chaithrika U Se7332e32009-06-09 05:55:37 -03001266 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1267 if (err) {
1268 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1269 return err;
1270 }
1271
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001272 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001273 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001274 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001275 (void *)(&vpif_obj.dev[res_idx]->
1276 channel_id));
1277 if (err) {
1278 err = -EINVAL;
1279 vpif_err("VPIF IRQ request failed\n");
1280 goto vpif_unregister;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001281 }
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001282 res_idx++;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001283 }
1284
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001285 vpif_obj.config = pdev->dev.platform_data;
1286 subdev_count = vpif_obj.config->subdev_count;
1287 subdevdata = vpif_obj.config->subdevinfo;
Markus Elfringd5cf4672016-10-12 10:30:44 -03001288 vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
Markus Elfring396c88e2016-10-12 10:40:32 -03001289 if (!vpif_obj.sd) {
Hans Verkuile6067f82012-09-20 09:06:27 -03001290 err = -ENOMEM;
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001291 goto vpif_unregister;
Hans Verkuile6067f82012-09-20 09:06:27 -03001292 }
1293
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001294 if (!vpif_obj.config->asd_sizes) {
Bartosz Golaszewskia16cb912017-02-16 16:08:01 -02001295 i2c_adap = i2c_get_adapter(vpif_obj.config->i2c_adapter_id);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001296 for (i = 0; i < subdev_count; i++) {
1297 vpif_obj.sd[i] =
1298 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1299 i2c_adap,
1300 &subdevdata[i].
1301 board_info,
1302 NULL);
1303 if (!vpif_obj.sd[i]) {
1304 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun9d3e9762013-08-22 22:59:44 -03001305 err = -ENODEV;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001306 goto probe_subdev_out;
1307 }
1308
1309 if (vpif_obj.sd[i])
1310 vpif_obj.sd[i]->grp_id = 1 << i;
1311 }
1312 vpif_probe_complete();
1313 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001314 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001315 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1316 vpif_obj.notifier.bound = vpif_async_bound;
1317 vpif_obj.notifier.complete = vpif_async_complete;
1318 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1319 &vpif_obj.notifier);
1320 if (err) {
1321 vpif_err("Error registering async notifier\n");
1322 err = -EINVAL;
Hans Verkuile6067f82012-09-20 09:06:27 -03001323 goto probe_subdev_out;
1324 }
Hans Verkuile6067f82012-09-20 09:06:27 -03001325 }
1326
Chaithrika U Se7332e32009-06-09 05:55:37 -03001327 return 0;
1328
Hans Verkuile6067f82012-09-20 09:06:27 -03001329probe_subdev_out:
1330 kfree(vpif_obj.sd);
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001331vpif_unregister:
Chaithrika U Se7332e32009-06-09 05:55:37 -03001332 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001333
1334 return err;
1335}
1336
1337/*
1338 * vpif_remove: It un-register channels from V4L2 driver
1339 */
1340static int vpif_remove(struct platform_device *device)
1341{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001342 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001343 struct channel_obj *ch;
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001344 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001345
1346 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1347
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001348 kfree(vpif_obj.sd);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001349 /* un-register device */
1350 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1351 /* Get the pointer to the channel object */
1352 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001353 common = &ch->common[VPIF_VIDEO_INDEX];
Chaithrika U Se7332e32009-06-09 05:55:37 -03001354 /* Unregister video device */
Lad, Prabhakare933c6b2015-03-08 18:57:24 -03001355 video_unregister_device(&ch->video_dev);
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001356 kfree(vpif_obj.dev[i]);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001357 }
1358
1359 return 0;
1360}
1361
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001362#ifdef CONFIG_PM_SLEEP
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001363static int vpif_suspend(struct device *dev)
1364{
1365 struct common_obj *common;
1366 struct channel_obj *ch;
1367 int i;
1368
1369 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1370 /* Get the pointer to the channel object */
1371 ch = vpif_obj.dev[i];
1372 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001373
Prabhakar Lad81578922014-09-06 12:26:50 -03001374 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001375 continue;
1376
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001377 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001378 /* Disable channel */
1379 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1380 enable_channel2(0);
1381 channel2_intr_enable(0);
1382 }
1383 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1384 ycmux_mode == 2) {
1385 enable_channel3(0);
1386 channel3_intr_enable(0);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001387 }
1388 mutex_unlock(&common->lock);
1389 }
1390
1391 return 0;
1392}
1393
1394static int vpif_resume(struct device *dev)
1395{
1396
1397 struct common_obj *common;
1398 struct channel_obj *ch;
1399 int i;
1400
1401 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1402 /* Get the pointer to the channel object */
1403 ch = vpif_obj.dev[i];
1404 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001405
Prabhakar Lad81578922014-09-06 12:26:50 -03001406 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001407 continue;
1408
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001409 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001410 /* Enable channel */
1411 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1412 enable_channel2(1);
1413 channel2_intr_enable(1);
1414 }
1415 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1416 ycmux_mode == 2) {
1417 enable_channel3(1);
1418 channel3_intr_enable(1);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001419 }
1420 mutex_unlock(&common->lock);
1421 }
1422
1423 return 0;
1424}
1425
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001426#endif
1427
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001428static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1429
Mats Randgaardffa1b392010-08-30 10:30:36 -03001430static __refdata struct platform_driver vpif_driver = {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001431 .driver = {
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001432 .name = VPIF_DRIVER_NAME,
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001433 .pm = &vpif_pm_ops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001434 },
1435 .probe = vpif_probe,
1436 .remove = vpif_remove,
1437};
1438
Lad, Prabhakarb8d067b2013-06-17 11:20:49 -03001439module_platform_driver(vpif_driver);