blob: aaa9ec06630f668f2579532882d2a2ff61f339d5 [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"
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -030045
46/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
47static int ycmux_mode;
48
Lad, Prabhakar58334b02014-05-16 10:33:13 -030049static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
50
Chaithrika U Se7332e32009-06-09 05:55:37 -030051static struct vpif_device vpif_obj = { {NULL} };
52static struct device *vpif_dev;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030053static void vpif_calculate_offsets(struct channel_obj *ch);
54static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Chaithrika U Se7332e32009-06-09 05:55:37 -030055
Lad, Prabhakar58334b02014-05-16 10:33:13 -030056static inline struct vpif_disp_buffer *to_vpif_buffer(struct vb2_buffer *vb)
57{
58 return container_of(vb, struct vpif_disp_buffer, vb);
59}
60
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030061/**
62 * vpif_buffer_prepare : callback function for buffer prepare
63 * @vb: ptr to vb2_buffer
64 *
65 * This is the callback function for buffer prepare when vb2_qbuf()
66 * function is called. The buffer is prepared and user space virtual address
67 * or user address is converted into physical address
Chaithrika U Se7332e32009-06-09 05:55:37 -030068 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030069static int vpif_buffer_prepare(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -030070{
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030071 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -030072 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -030073
Lad, Prabhakara2b235c2014-05-16 10:33:06 -030074 common = &ch->common[VPIF_VIDEO_INDEX];
Chaithrika U Se7332e32009-06-09 05:55:37 -030075
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030076 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
77 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
78 return -EINVAL;
79
80 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
81
82 if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
83 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
84
85 if (!ISALIGNED(addr + common->ytop_off) ||
Lad, Prabhakar2401dd22012-06-28 09:28:36 -030086 !ISALIGNED(addr + common->ybtm_off) ||
87 !ISALIGNED(addr + common->ctop_off) ||
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030088 !ISALIGNED(addr + common->cbtm_off)) {
89 vpif_err("buffer offset not aligned to 8 bytes\n");
90 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -030091 }
Chaithrika U Se7332e32009-06-09 05:55:37 -030092 }
Chaithrika U Se7332e32009-06-09 05:55:37 -030093
Lad, Prabhakar27cdf9b2014-05-16 10:33:11 -030094 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -030095}
96
Lad, Prabhakar172392a2014-05-16 10:33:12 -030097/**
98 * vpif_buffer_queue_setup : Callback function for buffer setup.
99 * @vq: vb2_queue ptr
100 * @fmt: v4l2 format
101 * @nbuffers: ptr to number of buffers requested by application
102 * @nplanes:: contains number of distinct video planes needed to hold a frame
103 * @sizes[]: contains the size (in bytes) of each plane.
104 * @alloc_ctxs: ptr to allocation context
105 *
106 * This callback function is called when reqbuf() is called to adjust
107 * the buffer count and buffer size
Chaithrika U Se7332e32009-06-09 05:55:37 -0300108 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300109static int vpif_buffer_queue_setup(struct vb2_queue *vq,
110 const struct v4l2_format *fmt,
111 unsigned int *nbuffers, unsigned int *nplanes,
112 unsigned int sizes[], void *alloc_ctxs[])
Chaithrika U Se7332e32009-06-09 05:55:37 -0300113{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300114 struct channel_obj *ch = vb2_get_drv_priv(vq);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300115 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
116
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300117 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
118 return -EINVAL;
Manjunath Hadlifc613d42012-04-13 04:49:10 -0300119
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300120 if (vq->num_buffers + *nbuffers < 3)
121 *nbuffers = 3 - vq->num_buffers;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300122
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300123 *nplanes = 1;
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300124 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300125 alloc_ctxs[0] = common->alloc_ctx;
Lad, Prabhakar172392a2014-05-16 10:33:12 -0300126
127 /* Calculate the offset for Y and C data in the buffer */
128 vpif_calculate_offsets(ch);
129
Chaithrika U Se7332e32009-06-09 05:55:37 -0300130 return 0;
131}
132
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300133/**
134 * vpif_buffer_queue : Callback function to add buffer to DMA queue
135 * @vb: ptr to vb2_buffer
136 *
137 * This callback fucntion queues the buffer to DMA engine
Chaithrika U Se7332e32009-06-09 05:55:37 -0300138 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300139static void vpif_buffer_queue(struct vb2_buffer *vb)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300140{
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300141 struct vpif_disp_buffer *buf = to_vpif_buffer(vb);
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300142 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300143 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300144 unsigned long flags;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300145
146 common = &ch->common[VPIF_VIDEO_INDEX];
147
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300148 /* add the buffer to the DMA queue */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300149 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300150 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300151 spin_unlock_irqrestore(&common->irqlock, flags);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300152}
153
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300154/**
155 * vpif_start_streaming : Starts the DMA engine for streaming
156 * @vb: ptr to vb2_buffer
157 * @count: number of buffers
158 */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300159static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
160{
161 struct vpif_display_config *vpif_config_data =
162 vpif_dev->platform_data;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300163 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300164 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
165 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300166 struct vpif_disp_buffer *buf, *tmp;
167 unsigned long addr, flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300168 int ret;
169
Hans Verkuilc4697d72012-11-16 12:03:07 -0300170 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300171
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300172 /* Initialize field_id */
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300173 ch->field_id = 0;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300174
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300175 /* clock settings */
176 if (vpif_config_data->set_clock) {
177 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
178 ycmux_mode, ch->vpifparams.std_info.hd_sd);
179 if (ret < 0) {
180 vpif_err("can't set clock\n");
181 goto err;
182 }
183 }
184
185 /* set the parameters and addresses */
186 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
187 if (ret < 0)
188 goto err;
189
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300190 ycmux_mode = ret;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300191 vpif_config_addr(ch, ret);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300192 /* Get the next frame from the buffer queue */
193 common->next_frm = common->cur_frm =
194 list_entry(common->dma_queue.next,
195 struct vpif_disp_buffer, list);
196
197 list_del(&common->cur_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300198 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300199 /* Mark state of the current frame to active */
200 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
201
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300202 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300203 common->set_addr((addr + common->ytop_off),
204 (addr + common->ybtm_off),
205 (addr + common->ctop_off),
206 (addr + common->cbtm_off));
207
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300208 /*
209 * Set interrupt for both the fields in VPIF
210 * Register enable channel in VPIF register
211 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300212 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300213 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
214 channel2_intr_assert();
215 channel2_intr_enable(1);
216 enable_channel2(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300217 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300218 channel2_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300219 }
220
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300221 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300222 channel3_intr_assert();
223 channel3_intr_enable(1);
224 enable_channel3(1);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300225 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
Manjunath Hadli6964b102012-06-29 03:20:12 -0300226 channel3_clipping_enable(1);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300227 }
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300228
229 return 0;
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300230
231err:
232 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
233 list_del(&buf->list);
234 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
235 }
Dan Carpenter92491962014-06-12 04:01:45 -0300236 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar54a445a42014-05-16 10:33:09 -0300237
238 return ret;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300239}
240
Lad, Prabhakar58334b02014-05-16 10:33:13 -0300241/**
242 * vpif_stop_streaming : Stop the DMA engine
243 * @vq: ptr to vb2_queue
244 *
245 * This callback stops the DMA engine and any remaining buffers
246 * in the DMA queue are released.
247 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300248static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300249{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -0300250 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300251 struct common_obj *common;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300252 unsigned long flags;
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300253
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300254 common = &ch->common[VPIF_VIDEO_INDEX];
255
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300256 /* Disable channel */
257 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
258 enable_channel2(0);
259 channel2_intr_enable(0);
260 }
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300261 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300262 enable_channel3(0);
263 channel3_intr_enable(0);
264 }
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300265
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300266 /* release all active buffers */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300267 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar18c7adc2014-03-22 08:03:08 -0300268 if (common->cur_frm == common->next_frm) {
269 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
270 } else {
271 if (common->cur_frm != NULL)
272 vb2_buffer_done(&common->cur_frm->vb,
273 VB2_BUF_STATE_ERROR);
274 if (common->next_frm != NULL)
275 vb2_buffer_done(&common->next_frm->vb,
276 VB2_BUF_STATE_ERROR);
277 }
278
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300279 while (!list_empty(&common->dma_queue)) {
280 common->next_frm = list_entry(common->dma_queue.next,
281 struct vpif_disp_buffer, list);
282 list_del(&common->next_frm->list);
283 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
284 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300285 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300286}
287
288static struct vb2_ops video_qops = {
289 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -0300290 .wait_prepare = vb2_ops_wait_prepare,
291 .wait_finish = vb2_ops_wait_finish,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300292 .buf_prepare = vpif_buffer_prepare,
293 .start_streaming = vpif_start_streaming,
294 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300295 .buf_queue = vpif_buffer_queue,
296};
297
Chaithrika U Se7332e32009-06-09 05:55:37 -0300298static void process_progressive_mode(struct common_obj *common)
299{
300 unsigned long addr = 0;
301
Hans Verkuilc4697d72012-11-16 12:03:07 -0300302 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300303 /* Get the next buffer from buffer queue */
304 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300305 struct vpif_disp_buffer, list);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300306 /* Remove that buffer from the buffer queue */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300307 list_del(&common->next_frm->list);
Hans Verkuilc4697d72012-11-16 12:03:07 -0300308 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300309 /* Mark status of the buffer as active */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300310 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300311
312 /* Set top and bottom field addrs in VPIF registers */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300313 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300314 common->set_addr(addr + common->ytop_off,
315 addr + common->ybtm_off,
316 addr + common->ctop_off,
317 addr + common->cbtm_off);
318}
319
320static void process_interlaced_mode(int fid, struct common_obj *common)
321{
322 /* device field id and local field id are in sync */
323 /* If this is even field */
324 if (0 == fid) {
325 if (common->cur_frm == common->next_frm)
326 return;
327
328 /* one frame is displayed If next frame is
329 * available, release cur_frm and move on */
330 /* Copy frame display time */
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300331 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300332 /* Change status of the cur_frm */
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300333 vb2_buffer_done(&common->cur_frm->vb,
334 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300335 /* Make cur_frm pointing to next_frm */
336 common->cur_frm = common->next_frm;
337
338 } else if (1 == fid) { /* odd field */
Hans Verkuilc4697d72012-11-16 12:03:07 -0300339 spin_lock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300340 if (list_empty(&common->dma_queue)
341 || (common->cur_frm != common->next_frm)) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300342 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300343 return;
344 }
Hans Verkuilc4697d72012-11-16 12:03:07 -0300345 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300346 /* one field is displayed configure the next
347 * frame if it is available else hold on current
348 * frame */
349 /* Get next from the buffer queue */
350 process_progressive_mode(common);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300351 }
352}
353
354/*
355 * vpif_channel_isr: It changes status of the displayed buffer, takes next
356 * buffer from the queue and sets its address in VPIF registers
357 */
358static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
359{
360 struct vpif_device *dev = &vpif_obj;
361 struct channel_obj *ch;
362 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300363 int fid = -1, i;
364 int channel_id = 0;
365
366 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300367 if (!vpif_intr_status(channel_id + 2))
368 return IRQ_NONE;
369
Chaithrika U Se7332e32009-06-09 05:55:37 -0300370 ch = dev->dev[channel_id];
Chaithrika U Se7332e32009-06-09 05:55:37 -0300371 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
372 common = &ch->common[i];
373 /* If streaming is started in this channel */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300374
375 if (1 == ch->vpifparams.std_info.frm_fmt) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300376 spin_lock(&common->irqlock);
377 if (list_empty(&common->dma_queue)) {
378 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300379 continue;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300380 }
381 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300382
383 /* Progressive mode */
384 if (!channel_first_int[i][channel_id]) {
385 /* Mark status of the cur_frm to
386 * done and unlock semaphore on it */
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300387 v4l2_get_timestamp(&common->cur_frm->vb.
388 v4l2_buf.timestamp);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300389 vb2_buffer_done(&common->cur_frm->vb,
390 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300391 /* Make cur_frm pointing to next_frm */
392 common->cur_frm = common->next_frm;
393 }
394
395 channel_first_int[i][channel_id] = 0;
396 process_progressive_mode(common);
397 } else {
398 /* Interlaced mode */
399 /* If it is first interrupt, ignore it */
400
401 if (channel_first_int[i][channel_id]) {
402 channel_first_int[i][channel_id] = 0;
403 continue;
404 }
405
406 if (0 == i) {
407 ch->field_id ^= 1;
408 /* Get field id from VPIF registers */
409 fid = vpif_channel_getfid(ch->channel_id + 2);
410 /* If fid does not match with stored field id */
411 if (fid != ch->field_id) {
412 /* Make them in sync */
413 if (0 == fid)
414 ch->field_id = fid;
415
416 return IRQ_HANDLED;
417 }
418 }
419 process_interlaced_mode(fid, common);
420 }
421 }
422
423 return IRQ_HANDLED;
424}
425
Mats Randgaardc027e162010-12-16 12:17:44 -0300426static int vpif_update_std_info(struct channel_obj *ch)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300427{
Chaithrika U Se7332e32009-06-09 05:55:37 -0300428 struct video_obj *vid_ch = &ch->video;
429 struct vpif_params *vpifparams = &ch->vpifparams;
430 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
431 const struct vpif_channel_config_params *config;
432
Mats Randgaardc027e162010-12-16 12:17:44 -0300433 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300434
Mats Randgaardc027e162010-12-16 12:17:44 -0300435 for (i = 0; i < vpif_ch_params_count; i++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300436 config = &vpif_ch_params[i];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300437 if (config->hd_sd == 0) {
438 vpif_dbg(2, debug, "SD format\n");
439 if (config->stdid & vid_ch->stdid) {
440 memcpy(std_info, config, sizeof(*config));
441 break;
442 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300443 }
444 }
445
Mats Randgaardc027e162010-12-16 12:17:44 -0300446 if (i == vpif_ch_params_count) {
447 vpif_dbg(1, debug, "Format not found\n");
Mats Randgaardaa444402010-12-16 12:17:42 -0300448 return -EINVAL;
Mats Randgaardc027e162010-12-16 12:17:44 -0300449 }
450
451 return 0;
452}
453
454static int vpif_update_resolution(struct channel_obj *ch)
455{
456 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
457 struct video_obj *vid_ch = &ch->video;
458 struct vpif_params *vpifparams = &ch->vpifparams;
459 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
460
Hans Verkuil0598c172012-09-18 07:18:47 -0300461 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
Mats Randgaardc027e162010-12-16 12:17:44 -0300462 return -EINVAL;
463
Hans Verkuil0598c172012-09-18 07:18:47 -0300464 if (vid_ch->stdid) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300465 if (vpif_update_std_info(ch))
466 return -EINVAL;
467 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300468
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300469 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300470 common->fmt.fmt.pix.width = std_info->width;
471 common->fmt.fmt.pix.height = std_info->height;
472 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
473 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
474
475 /* Set height and width paramateres */
Mats Randgaardc027e162010-12-16 12:17:44 -0300476 common->height = std_info->height;
477 common->width = std_info->width;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300478 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
479
480 if (vid_ch->stdid)
481 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
482 else
483 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
484
485 if (ch->vpifparams.std_info.frm_fmt)
486 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
487 else
488 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300489
490 return 0;
491}
492
493/*
494 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
495 * in the top and bottom field
496 */
497static void vpif_calculate_offsets(struct channel_obj *ch)
498{
499 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
500 struct vpif_params *vpifparams = &ch->vpifparams;
501 enum v4l2_field field = common->fmt.fmt.pix.field;
502 struct video_obj *vid_ch = &ch->video;
Mauro Carvalho Chehaba4f20e22014-08-21 15:46:46 -0500503 unsigned int hpitch, sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300504
505 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
506 if (ch->vpifparams.std_info.frm_fmt)
507 vid_ch->buf_field = V4L2_FIELD_NONE;
508 else
509 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
510 } else {
511 vid_ch->buf_field = common->fmt.fmt.pix.field;
512 }
513
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300514 sizeimage = common->fmt.fmt.pix.sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300515
516 hpitch = common->fmt.fmt.pix.bytesperline;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300517 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
518 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
519 common->ytop_off = 0;
520 common->ybtm_off = hpitch;
521 common->ctop_off = sizeimage / 2;
522 common->cbtm_off = sizeimage / 2 + hpitch;
523 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
524 common->ytop_off = 0;
525 common->ybtm_off = sizeimage / 4;
526 common->ctop_off = sizeimage / 2;
527 common->cbtm_off = common->ctop_off + sizeimage / 4;
528 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
529 common->ybtm_off = 0;
530 common->ytop_off = sizeimage / 4;
531 common->cbtm_off = sizeimage / 2;
532 common->ctop_off = common->cbtm_off + sizeimage / 4;
533 }
534
535 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
536 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
537 vpifparams->video_params.storage_mode = 1;
538 } else {
539 vpifparams->video_params.storage_mode = 0;
540 }
541
542 if (ch->vpifparams.std_info.frm_fmt == 1) {
543 vpifparams->video_params.hpitch =
544 common->fmt.fmt.pix.bytesperline;
545 } else {
546 if ((field == V4L2_FIELD_ANY) ||
547 (field == V4L2_FIELD_INTERLACED))
548 vpifparams->video_params.hpitch =
549 common->fmt.fmt.pix.bytesperline * 2;
550 else
551 vpifparams->video_params.hpitch =
552 common->fmt.fmt.pix.bytesperline;
553 }
554
555 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
556}
557
Chaithrika U Se7332e32009-06-09 05:55:37 -0300558static void vpif_config_addr(struct channel_obj *ch, int muxmode)
559{
560 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
561
562 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
563 common->set_addr = ch3_set_videobuf_addr;
564 } else {
565 if (2 == muxmode)
566 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
567 else
568 common->set_addr = ch2_set_videobuf_addr;
569 }
570}
571
Chaithrika U Se7332e32009-06-09 05:55:37 -0300572/* functions implementing ioctls */
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300573/**
574 * vpif_querycap() - QUERYCAP handler
575 * @file: file ptr
576 * @priv: file handle
577 * @cap: ptr to v4l2_capability structure
578 */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300579static int vpif_querycap(struct file *file, void *priv,
580 struct v4l2_capability *cap)
581{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300582 struct vpif_display_config *config = vpif_dev->platform_data;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300583
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300584 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
585 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -0300586 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -0300587 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
588 dev_name(vpif_dev));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300589 strlcpy(cap->card, config->card_name, sizeof(cap->card));
590
591 return 0;
592}
593
594static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
595 struct v4l2_fmtdesc *fmt)
596{
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300597 if (fmt->index != 0)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300598 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300599
600 /* Fill in the information about format */
601 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
602 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
603 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300604 fmt->flags = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300605 return 0;
606}
607
608static int vpif_g_fmt_vid_out(struct file *file, void *priv,
609 struct v4l2_format *fmt)
610{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300611 struct video_device *vdev = video_devdata(file);
612 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300613 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
614
615 /* Check the validity of the buffer type */
616 if (common->fmt.type != fmt->type)
617 return -EINVAL;
618
Mats Randgaardc027e162010-12-16 12:17:44 -0300619 if (vpif_update_resolution(ch))
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300620 return -EINVAL;
621 *fmt = common->fmt;
622 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300623}
624
Chaithrika U Se7332e32009-06-09 05:55:37 -0300625static int vpif_try_fmt_vid_out(struct file *file, void *priv,
626 struct v4l2_format *fmt)
627{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300628 struct video_device *vdev = video_devdata(file);
629 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300630 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
631 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300632
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300633 /*
634 * to supress v4l-compliance warnings silently correct
635 * the pixelformat
636 */
637 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
638 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300639
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300640 if (vpif_update_resolution(ch))
641 return -EINVAL;
642
643 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
644 pixfmt->field = common->fmt.fmt.pix.field;
645 pixfmt->bytesperline = common->fmt.fmt.pix.width;
646 pixfmt->width = common->fmt.fmt.pix.width;
647 pixfmt->height = common->fmt.fmt.pix.height;
648 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300649
650 return 0;
651}
652
653static int vpif_s_fmt_vid_out(struct file *file, void *priv,
654 struct v4l2_format *fmt)
655{
656 struct video_device *vdev = video_devdata(file);
657 struct channel_obj *ch = video_get_drvdata(vdev);
658 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
659 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
660 int ret;
661
662 if (vb2_is_busy(&common->buffer_queue))
663 return -EBUSY;
664
665 ret = vpif_try_fmt_vid_out(file, priv, fmt);
666 if (ret)
667 return ret;
668
669 /* store the pix format in the channel object */
670 common->fmt.fmt.pix = *pixfmt;
671
672 /* store the format in the channel object */
673 common->fmt = *fmt;
674 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300675}
676
Hans Verkuil314527a2013-03-15 06:10:40 -0300677static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300678{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300679 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300680 struct video_device *vdev = video_devdata(file);
681 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300682 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300683 struct vpif_display_chan_config *chan_cfg;
684 struct v4l2_output output;
685 int ret;
686
687 if (config->chan_config[ch->channel_id].outputs == NULL)
688 return -ENODATA;
689
690 chan_cfg = &config->chan_config[ch->channel_id];
691 output = chan_cfg->outputs[ch->output_idx].output;
692 if (output.capabilities != V4L2_OUT_CAP_STD)
693 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300694
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300695 if (vb2_is_busy(&common->buffer_queue))
696 return -EBUSY;
697
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300698
Hans Verkuil314527a2013-03-15 06:10:40 -0300699 if (!(std_id & VPIF_V4L2_STD))
Chaithrika U Se7332e32009-06-09 05:55:37 -0300700 return -EINVAL;
701
Chaithrika U Se7332e32009-06-09 05:55:37 -0300702 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300703 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300704 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300705 /* Get the information about the standard */
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300706 if (vpif_update_resolution(ch))
707 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300708
Chaithrika U Se7332e32009-06-09 05:55:37 -0300709 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300710
711 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300712 s_std_output, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300713 if (ret < 0) {
714 vpif_err("Failed to set output standard\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300715 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300716 }
717
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300718 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300719 s_std, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300720 if (ret < 0)
721 vpif_err("Failed to set standard for sub devices\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -0300722 return ret;
723}
724
725static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
726{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300727 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300728 struct video_device *vdev = video_devdata(file);
729 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300730 struct vpif_display_chan_config *chan_cfg;
731 struct v4l2_output output;
732
733 if (config->chan_config[ch->channel_id].outputs == NULL)
734 return -ENODATA;
735
736 chan_cfg = &config->chan_config[ch->channel_id];
737 output = chan_cfg->outputs[ch->output_idx].output;
738 if (output.capabilities != V4L2_OUT_CAP_STD)
739 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300740
741 *std = ch->video.stdid;
742 return 0;
743}
744
Chaithrika U Se7332e32009-06-09 05:55:37 -0300745static int vpif_enum_output(struct file *file, void *fh,
746 struct v4l2_output *output)
747{
748
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300749 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300750 struct video_device *vdev = video_devdata(file);
751 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300752 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300753
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300754 chan_cfg = &config->chan_config[ch->channel_id];
755 if (output->index >= chan_cfg->output_count) {
Chaithrika U Se7332e32009-06-09 05:55:37 -0300756 vpif_dbg(1, debug, "Invalid output index\n");
757 return -EINVAL;
758 }
759
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300760 *output = chan_cfg->outputs[output->index].output;
761 return 0;
762}
Chaithrika U Se7332e32009-06-09 05:55:37 -0300763
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300764/**
765 * vpif_output_to_subdev() - Maps output to sub device
766 * @vpif_cfg - global config ptr
767 * @chan_cfg - channel config ptr
768 * @index - Given output index from application
769 *
770 * lookup the sub device information for a given output index.
771 * we report all the output to application. output table also
772 * has sub device name for the each output
773 */
774static int
775vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
776 struct vpif_display_chan_config *chan_cfg, int index)
777{
778 struct vpif_subdev_info *subdev_info;
779 const char *subdev_name;
780 int i;
781
782 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
783
784 if (chan_cfg->outputs == NULL)
785 return -1;
786
787 subdev_name = chan_cfg->outputs[index].subdev_name;
788 if (subdev_name == NULL)
789 return -1;
790
791 /* loop through the sub device list to get the sub device info */
792 for (i = 0; i < vpif_cfg->subdev_count; i++) {
793 subdev_info = &vpif_cfg->subdevinfo[i];
794 if (!strcmp(subdev_info->name, subdev_name))
795 return i;
796 }
797 return -1;
798}
799
800/**
801 * vpif_set_output() - Select an output
802 * @vpif_cfg - global config ptr
803 * @ch - channel
804 * @index - Given output index from application
805 *
806 * Select the given output.
807 */
808static int vpif_set_output(struct vpif_display_config *vpif_cfg,
809 struct channel_obj *ch, int index)
810{
811 struct vpif_display_chan_config *chan_cfg =
812 &vpif_cfg->chan_config[ch->channel_id];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300813 struct v4l2_subdev *sd = NULL;
814 u32 input = 0, output = 0;
815 int sd_index;
816 int ret;
817
818 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
Mauro Carvalho Chehaba4f20e22014-08-21 15:46:46 -0500819 if (sd_index >= 0)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300820 sd = vpif_obj.sd[sd_index];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300821
822 if (sd) {
823 input = chan_cfg->outputs[index].input_route;
824 output = chan_cfg->outputs[index].output_route;
825 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
826 if (ret < 0 && ret != -ENOIOCTLCMD) {
827 vpif_err("Failed to set output\n");
828 return ret;
829 }
830
831 }
832 ch->output_idx = index;
833 ch->sd = sd;
834 if (chan_cfg->outputs != NULL)
835 /* update tvnorms from the sub device output info */
836 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300837 return 0;
838}
839
840static int vpif_s_output(struct file *file, void *priv, unsigned int i)
841{
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300842 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300843 struct video_device *vdev = video_devdata(file);
844 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300845 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300846 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300847
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300848 if (vb2_is_busy(&common->buffer_queue))
849 return -EBUSY;
850
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300851 chan_cfg = &config->chan_config[ch->channel_id];
852
853 if (i >= chan_cfg->output_count)
854 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300855
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300856 return vpif_set_output(config, ch, i);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300857}
858
859static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
860{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300861 struct video_device *vdev = video_devdata(file);
862 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300863
Hans Verkuil311673e2012-09-20 09:06:23 -0300864 *i = ch->output_idx;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300865
866 return 0;
867}
868
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300869/**
Hans Verkuil0598c172012-09-18 07:18:47 -0300870 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300871 * @file: file ptr
872 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -0300873 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300874 */
Hans Verkuil0598c172012-09-18 07:18:47 -0300875static int
876vpif_enum_dv_timings(struct file *file, void *priv,
877 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300878{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300879 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300880 struct video_device *vdev = video_devdata(file);
881 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300882 struct vpif_display_chan_config *chan_cfg;
883 struct v4l2_output output;
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300884 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300885
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300886 if (config->chan_config[ch->channel_id].outputs == NULL)
887 return -ENODATA;
888
889 chan_cfg = &config->chan_config[ch->channel_id];
890 output = chan_cfg->outputs[ch->output_idx].output;
891 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
892 return -ENODATA;
893
Laurent Pinchart10d21462014-01-31 09:04:19 -0300894 timings->pad = 0;
895
896 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjun63af4af2012-10-30 09:49:38 -0300897 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300898 return -EINVAL;
899 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300900}
901
902/**
Mats Randgaardc027e162010-12-16 12:17:44 -0300903 * vpif_s_dv_timings() - S_DV_TIMINGS handler
904 * @file: file ptr
905 * @priv: file handle
906 * @timings: digital video timings
907 */
908static int vpif_s_dv_timings(struct file *file, void *priv,
909 struct v4l2_dv_timings *timings)
910{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300911 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300912 struct video_device *vdev = video_devdata(file);
913 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -0300914 struct vpif_params *vpifparams = &ch->vpifparams;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300915 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -0300916 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
917 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -0300918 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300919 struct vpif_display_chan_config *chan_cfg;
920 struct v4l2_output output;
Mats Randgaardc027e162010-12-16 12:17:44 -0300921 int ret;
922
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300923 if (config->chan_config[ch->channel_id].outputs == NULL)
924 return -ENODATA;
925
926 chan_cfg = &config->chan_config[ch->channel_id];
927 output = chan_cfg->outputs[ch->output_idx].output;
928 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
929 return -ENODATA;
930
931 if (vb2_is_busy(&common->buffer_queue))
932 return -EBUSY;
933
Mats Randgaardc027e162010-12-16 12:17:44 -0300934 if (timings->type != V4L2_DV_BT_656_1120) {
935 vpif_dbg(2, debug, "Timing type not defined\n");
936 return -EINVAL;
937 }
938
939 /* Configure subdevice timings, if any */
Hans Verkuil882084a2012-09-20 09:06:31 -0300940 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300941 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
942 ret = 0;
943 if (ret < 0) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300944 vpif_dbg(2, debug, "Error setting custom DV timings\n");
945 return ret;
946 }
947
948 if (!(timings->bt.width && timings->bt.height &&
949 (timings->bt.hbackporch ||
950 timings->bt.hfrontporch ||
951 timings->bt.hsync) &&
952 timings->bt.vfrontporch &&
953 (timings->bt.vbackporch ||
954 timings->bt.vsync))) {
955 vpif_dbg(2, debug, "Timings for width, height, "
956 "horizontal back porch, horizontal sync, "
957 "horizontal front porch, vertical back porch, "
958 "vertical sync and vertical back porch "
959 "must be defined\n");
960 return -EINVAL;
961 }
962
Hans Verkuil0598c172012-09-18 07:18:47 -0300963 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -0300964
965 /* Configure video port timings */
966
Hans Verkuile3655262013-07-29 08:41:00 -0300967 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -0300968 std_info->sav2eav = bt->width;
969
970 std_info->l1 = 1;
971 std_info->l3 = bt->vsync + bt->vbackporch + 1;
972
Hans Verkuile3655262013-07-29 08:41:00 -0300973 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -0300974 if (bt->interlaced) {
975 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300976 std_info->l5 = std_info->vsize/2 -
977 (bt->vfrontporch - 1);
978 std_info->l7 = std_info->vsize/2 + 1;
979 std_info->l9 = std_info->l7 + bt->il_vsync +
980 bt->il_vbackporch + 1;
981 std_info->l11 = std_info->vsize -
982 (bt->il_vfrontporch - 1);
983 } else {
984 vpif_dbg(2, debug, "Required timing values for "
985 "interlaced BT format missing\n");
986 return -EINVAL;
987 }
988 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -0300989 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
990 }
991 strncpy(std_info->name, "Custom timings BT656/1120",
992 VPIF_MAX_NAME);
993 std_info->width = bt->width;
994 std_info->height = bt->height;
995 std_info->frm_fmt = bt->interlaced ? 0 : 1;
996 std_info->ycmux_mode = 0;
997 std_info->capture_format = 0;
998 std_info->vbi_supported = 0;
999 std_info->hd_sd = 1;
1000 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001001 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001002
1003 return 0;
1004}
1005
1006/**
1007 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1008 * @file: file ptr
1009 * @priv: file handle
1010 * @timings: digital video timings
1011 */
1012static int vpif_g_dv_timings(struct file *file, void *priv,
1013 struct v4l2_dv_timings *timings)
1014{
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001015 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001016 struct video_device *vdev = video_devdata(file);
1017 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001018 struct vpif_display_chan_config *chan_cfg;
Mats Randgaardc027e162010-12-16 12:17:44 -03001019 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001020 struct v4l2_output output;
1021
1022 if (config->chan_config[ch->channel_id].outputs == NULL)
1023 goto error;
1024
1025 chan_cfg = &config->chan_config[ch->channel_id];
1026 output = chan_cfg->outputs[ch->output_idx].output;
1027
1028 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1029 goto error;
Mats Randgaardc027e162010-12-16 12:17:44 -03001030
Hans Verkuil0598c172012-09-18 07:18:47 -03001031 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001032
1033 return 0;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001034error:
1035 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001036}
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001037
1038/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001039 * vpif_log_status() - Status information
1040 * @file: file ptr
1041 * @priv: file handle
1042 *
1043 * Returns zero.
1044 */
1045static int vpif_log_status(struct file *filep, void *priv)
1046{
1047 /* status for sub devices */
1048 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1049
1050 return 0;
1051}
1052
Chaithrika U Se7332e32009-06-09 05:55:37 -03001053/* vpif display ioctl operations */
1054static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001055 .vidioc_querycap = vpif_querycap,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001056 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001057 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1058 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1059 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
Lad, Prabhakarb92cde12014-05-16 10:33:16 -03001060
1061 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1062 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1063 .vidioc_querybuf = vb2_ioctl_querybuf,
1064 .vidioc_qbuf = vb2_ioctl_qbuf,
1065 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1066 .vidioc_expbuf = vb2_ioctl_expbuf,
1067 .vidioc_streamon = vb2_ioctl_streamon,
1068 .vidioc_streamoff = vb2_ioctl_streamoff,
1069
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001070 .vidioc_s_std = vpif_s_std,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001071 .vidioc_g_std = vpif_g_std,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001072
Chaithrika U Se7332e32009-06-09 05:55:37 -03001073 .vidioc_enum_output = vpif_enum_output,
1074 .vidioc_s_output = vpif_s_output,
1075 .vidioc_g_output = vpif_g_output,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001076
1077 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1078 .vidioc_s_dv_timings = vpif_s_dv_timings,
1079 .vidioc_g_dv_timings = vpif_g_dv_timings,
1080
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001081 .vidioc_log_status = vpif_log_status,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001082};
1083
1084static const struct v4l2_file_operations vpif_fops = {
1085 .owner = THIS_MODULE,
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001086 .open = v4l2_fh_open,
1087 .release = vb2_fop_release,
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001088 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001089 .mmap = vb2_fop_mmap,
1090 .poll = vb2_fop_poll
Chaithrika U Se7332e32009-06-09 05:55:37 -03001091};
1092
Chaithrika U Se7332e32009-06-09 05:55:37 -03001093/*Configure the channels, buffer sizei, request irq */
1094static int initialize_vpif(void)
1095{
1096 int free_channel_objects_index;
Lad, Prabhakard5b94b92014-05-16 10:33:23 -03001097 int err, i, j;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001098
1099 /* Allocate memory for six channel objects */
1100 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1101 vpif_obj.dev[i] =
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001102 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001103 /* If memory allocation fails, return error */
1104 if (!vpif_obj.dev[i]) {
1105 free_channel_objects_index = i;
1106 err = -ENOMEM;
1107 goto vpif_init_free_channel_objects;
1108 }
1109 }
1110
Chaithrika U Se7332e32009-06-09 05:55:37 -03001111 return 0;
1112
1113vpif_init_free_channel_objects:
1114 for (j = 0; j < free_channel_objects_index; j++)
1115 kfree(vpif_obj.dev[j]);
1116 return err;
1117}
1118
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001119static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1120 struct v4l2_subdev *subdev,
1121 struct v4l2_async_subdev *asd)
1122{
1123 int i;
1124
1125 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1126 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1127 subdev->name)) {
1128 vpif_obj.sd[i] = subdev;
1129 vpif_obj.sd[i]->grp_id = 1 << i;
1130 return 0;
1131 }
1132
1133 return -EINVAL;
1134}
1135
1136static int vpif_probe_complete(void)
1137{
1138 struct common_obj *common;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001139 struct video_device *vdev;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001140 struct channel_obj *ch;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001141 struct vb2_queue *q;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001142 int j, err, k;
1143
1144 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1145 ch = vpif_obj.dev[j];
1146 /* Initialize field of the channel objects */
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001147 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001148 common = &ch->common[k];
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001149 spin_lock_init(&common->irqlock);
1150 mutex_init(&common->lock);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001151 common->set_addr = NULL;
1152 common->ytop_off = 0;
1153 common->ybtm_off = 0;
1154 common->ctop_off = 0;
1155 common->cbtm_off = 0;
1156 common->cur_frm = NULL;
1157 common->next_frm = NULL;
1158 memset(&common->fmt, 0, sizeof(common->fmt));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001159 }
1160 ch->initialized = 0;
1161 if (vpif_obj.config->subdev_count)
1162 ch->sd = vpif_obj.sd[0];
1163 ch->channel_id = j;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001164
1165 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1166
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001167 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1168 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001169
1170 /* select output 0 */
1171 err = vpif_set_output(vpif_obj.config, ch, 0);
1172 if (err)
1173 goto probe_out;
1174
Lad, Prabhakar9cba6532014-05-16 10:33:30 -03001175 /* set initial format */
1176 ch->video.stdid = V4L2_STD_525_60;
1177 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1178 vpif_update_resolution(ch);
1179
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001180 /* Initialize vb2 queue */
1181 q = &common->buffer_queue;
1182 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1183 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1184 q->drv_priv = ch;
1185 q->ops = &video_qops;
1186 q->mem_ops = &vb2_dma_contig_memops;
1187 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1188 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1189 q->min_buffers_needed = 1;
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -03001190 q->lock = &common->lock;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001191 err = vb2_queue_init(q);
1192 if (err) {
1193 vpif_err("vpif_display: vb2_queue_init() failed\n");
1194 goto probe_out;
1195 }
1196
1197 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1198 if (IS_ERR(common->alloc_ctx)) {
1199 vpif_err("Failed to get the context\n");
1200 err = PTR_ERR(common->alloc_ctx);
1201 goto probe_out;
1202 }
1203
1204 INIT_LIST_HEAD(&common->dma_queue);
1205
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001206 /* register video device */
1207 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1208 (int)ch, (int)&ch->video_dev);
1209
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001210 /* Initialize the video_device structure */
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001211 vdev = ch->video_dev;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001212 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1213 vdev->release = video_device_release;
1214 vdev->fops = &vpif_fops;
1215 vdev->ioctl_ops = &vpif_ioctl_ops;
1216 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1217 vdev->vfl_dir = VFL_DIR_TX;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001218 vdev->queue = q;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001219 vdev->lock = &common->lock;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001220 video_set_drvdata(ch->video_dev, ch);
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001221 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1222 (j ? 3 : 2));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001223 if (err < 0)
1224 goto probe_out;
1225 }
1226
1227 return 0;
1228
1229probe_out:
1230 for (k = 0; k < j; k++) {
1231 ch = vpif_obj.dev[k];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001232 common = &ch->common[k];
1233 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001234 video_unregister_device(ch->video_dev);
1235 video_device_release(ch->video_dev);
1236 ch->video_dev = NULL;
1237 }
1238 return err;
1239}
1240
1241static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1242{
1243 return vpif_probe_complete();
1244}
1245
Chaithrika U Se7332e32009-06-09 05:55:37 -03001246/*
1247 * vpif_probe: This function creates device entries by register itself to the
1248 * V4L2 driver and initializes fields of each channel objects
1249 */
1250static __init int vpif_probe(struct platform_device *pdev)
1251{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001252 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001253 int i, j = 0, err = 0;
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001254 int res_idx = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001255 struct i2c_adapter *i2c_adap;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001256 struct channel_obj *ch;
1257 struct video_device *vfd;
1258 struct resource *res;
1259 int subdev_count;
1260
1261 vpif_dev = &pdev->dev;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001262 err = initialize_vpif();
1263
1264 if (err) {
1265 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1266 return err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001267 }
1268
Chaithrika U Se7332e32009-06-09 05:55:37 -03001269 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1270 if (err) {
1271 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1272 return err;
1273 }
1274
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001275 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001276 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001277 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001278 (void *)(&vpif_obj.dev[res_idx]->
1279 channel_id));
1280 if (err) {
1281 err = -EINVAL;
1282 vpif_err("VPIF IRQ request failed\n");
1283 goto vpif_unregister;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001284 }
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001285 res_idx++;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001286 }
1287
1288 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001289 /* Get the pointer to the channel object */
1290 ch = vpif_obj.dev[i];
1291
1292 /* Allocate memory for video device */
1293 vfd = video_device_alloc();
1294 if (vfd == NULL) {
1295 for (j = 0; j < i; j++) {
1296 ch = vpif_obj.dev[j];
1297 video_device_release(ch->video_dev);
1298 }
1299 err = -ENOMEM;
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001300 goto vpif_unregister;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001301 }
1302
Chaithrika U Se7332e32009-06-09 05:55:37 -03001303 /* Set video_dev to the video device */
1304 ch->video_dev = vfd;
1305 }
1306
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001307 vpif_obj.config = pdev->dev.platform_data;
1308 subdev_count = vpif_obj.config->subdev_count;
1309 subdevdata = vpif_obj.config->subdevinfo;
Hans Verkuile6067f82012-09-20 09:06:27 -03001310 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1311 GFP_KERNEL);
1312 if (vpif_obj.sd == NULL) {
1313 vpif_err("unable to allocate memory for subdevice pointers\n");
1314 err = -ENOMEM;
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001315 goto vpif_sd_error;
Hans Verkuile6067f82012-09-20 09:06:27 -03001316 }
1317
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001318 if (!vpif_obj.config->asd_sizes) {
1319 i2c_adap = i2c_get_adapter(1);
1320 for (i = 0; i < subdev_count; i++) {
1321 vpif_obj.sd[i] =
1322 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1323 i2c_adap,
1324 &subdevdata[i].
1325 board_info,
1326 NULL);
1327 if (!vpif_obj.sd[i]) {
1328 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun9d3e9762013-08-22 22:59:44 -03001329 err = -ENODEV;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001330 goto probe_subdev_out;
1331 }
1332
1333 if (vpif_obj.sd[i])
1334 vpif_obj.sd[i]->grp_id = 1 << i;
1335 }
1336 vpif_probe_complete();
1337 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001338 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001339 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1340 vpif_obj.notifier.bound = vpif_async_bound;
1341 vpif_obj.notifier.complete = vpif_async_complete;
1342 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1343 &vpif_obj.notifier);
1344 if (err) {
1345 vpif_err("Error registering async notifier\n");
1346 err = -EINVAL;
Hans Verkuile6067f82012-09-20 09:06:27 -03001347 goto probe_subdev_out;
1348 }
Hans Verkuile6067f82012-09-20 09:06:27 -03001349 }
1350
Chaithrika U Se7332e32009-06-09 05:55:37 -03001351 return 0;
1352
Hans Verkuile6067f82012-09-20 09:06:27 -03001353probe_subdev_out:
1354 kfree(vpif_obj.sd);
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001355vpif_sd_error:
1356 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1357 ch = vpif_obj.dev[i];
1358 /* Note: does nothing if ch->video_dev == NULL */
1359 video_device_release(ch->video_dev);
1360 }
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001361vpif_unregister:
Chaithrika U Se7332e32009-06-09 05:55:37 -03001362 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001363
1364 return err;
1365}
1366
1367/*
1368 * vpif_remove: It un-register channels from V4L2 driver
1369 */
1370static int vpif_remove(struct platform_device *device)
1371{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001372 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001373 struct channel_obj *ch;
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001374 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001375
1376 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1377
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001378 kfree(vpif_obj.sd);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001379 /* un-register device */
1380 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1381 /* Get the pointer to the channel object */
1382 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001383 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001384 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001385 /* Unregister video device */
1386 video_unregister_device(ch->video_dev);
1387
1388 ch->video_dev = NULL;
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001389 kfree(vpif_obj.dev[i]);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001390 }
1391
1392 return 0;
1393}
1394
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001395#ifdef CONFIG_PM_SLEEP
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001396static int vpif_suspend(struct device *dev)
1397{
1398 struct common_obj *common;
1399 struct channel_obj *ch;
1400 int i;
1401
1402 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1403 /* Get the pointer to the channel object */
1404 ch = vpif_obj.dev[i];
1405 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001406
1407 if (!vb2_is_streaming(&common->buffer_queue))
1408 continue;
1409
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001410 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001411 /* Disable channel */
1412 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1413 enable_channel2(0);
1414 channel2_intr_enable(0);
1415 }
1416 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1417 ycmux_mode == 2) {
1418 enable_channel3(0);
1419 channel3_intr_enable(0);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001420 }
1421 mutex_unlock(&common->lock);
1422 }
1423
1424 return 0;
1425}
1426
1427static int vpif_resume(struct device *dev)
1428{
1429
1430 struct common_obj *common;
1431 struct channel_obj *ch;
1432 int i;
1433
1434 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1435 /* Get the pointer to the channel object */
1436 ch = vpif_obj.dev[i];
1437 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001438
1439 if (!vb2_is_streaming(&common->buffer_queue))
1440 continue;
1441
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001442 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001443 /* Enable channel */
1444 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1445 enable_channel2(1);
1446 channel2_intr_enable(1);
1447 }
1448 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1449 ycmux_mode == 2) {
1450 enable_channel3(1);
1451 channel3_intr_enable(1);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001452 }
1453 mutex_unlock(&common->lock);
1454 }
1455
1456 return 0;
1457}
1458
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001459#endif
1460
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001461static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1462
Mats Randgaardffa1b392010-08-30 10:30:36 -03001463static __refdata struct platform_driver vpif_driver = {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001464 .driver = {
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001465 .name = VPIF_DRIVER_NAME,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001466 .owner = THIS_MODULE,
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001467 .pm = &vpif_pm_ops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001468 },
1469 .probe = vpif_probe,
1470 .remove = vpif_remove,
1471};
1472
Lad, Prabhakarb8d067b2013-06-17 11:20:49 -03001473module_platform_driver(vpif_driver);