blob: b431b58f39e3648d7d33ae6e049f1896ccd3fedc [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;
363 enum v4l2_field field;
364 int fid = -1, i;
365 int channel_id = 0;
366
367 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300368 if (!vpif_intr_status(channel_id + 2))
369 return IRQ_NONE;
370
Chaithrika U Se7332e32009-06-09 05:55:37 -0300371 ch = dev->dev[channel_id];
372 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
373 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
374 common = &ch->common[i];
375 /* If streaming is started in this channel */
Chaithrika U Se7332e32009-06-09 05:55:37 -0300376
377 if (1 == ch->vpifparams.std_info.frm_fmt) {
Hans Verkuilc4697d72012-11-16 12:03:07 -0300378 spin_lock(&common->irqlock);
379 if (list_empty(&common->dma_queue)) {
380 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300381 continue;
Hans Verkuilc4697d72012-11-16 12:03:07 -0300382 }
383 spin_unlock(&common->irqlock);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300384
385 /* Progressive mode */
386 if (!channel_first_int[i][channel_id]) {
387 /* Mark status of the cur_frm to
388 * done and unlock semaphore on it */
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300389 v4l2_get_timestamp(&common->cur_frm->vb.
390 v4l2_buf.timestamp);
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300391 vb2_buffer_done(&common->cur_frm->vb,
392 VB2_BUF_STATE_DONE);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300393 /* Make cur_frm pointing to next_frm */
394 common->cur_frm = common->next_frm;
395 }
396
397 channel_first_int[i][channel_id] = 0;
398 process_progressive_mode(common);
399 } else {
400 /* Interlaced mode */
401 /* If it is first interrupt, ignore it */
402
403 if (channel_first_int[i][channel_id]) {
404 channel_first_int[i][channel_id] = 0;
405 continue;
406 }
407
408 if (0 == i) {
409 ch->field_id ^= 1;
410 /* Get field id from VPIF registers */
411 fid = vpif_channel_getfid(ch->channel_id + 2);
412 /* If fid does not match with stored field id */
413 if (fid != ch->field_id) {
414 /* Make them in sync */
415 if (0 == fid)
416 ch->field_id = fid;
417
418 return IRQ_HANDLED;
419 }
420 }
421 process_interlaced_mode(fid, common);
422 }
423 }
424
425 return IRQ_HANDLED;
426}
427
Mats Randgaardc027e162010-12-16 12:17:44 -0300428static int vpif_update_std_info(struct channel_obj *ch)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300429{
Chaithrika U Se7332e32009-06-09 05:55:37 -0300430 struct video_obj *vid_ch = &ch->video;
431 struct vpif_params *vpifparams = &ch->vpifparams;
432 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
433 const struct vpif_channel_config_params *config;
434
Mats Randgaardc027e162010-12-16 12:17:44 -0300435 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300436
Mats Randgaardc027e162010-12-16 12:17:44 -0300437 for (i = 0; i < vpif_ch_params_count; i++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300438 config = &vpif_ch_params[i];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300439 if (config->hd_sd == 0) {
440 vpif_dbg(2, debug, "SD format\n");
441 if (config->stdid & vid_ch->stdid) {
442 memcpy(std_info, config, sizeof(*config));
443 break;
444 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300445 }
446 }
447
Mats Randgaardc027e162010-12-16 12:17:44 -0300448 if (i == vpif_ch_params_count) {
449 vpif_dbg(1, debug, "Format not found\n");
Mats Randgaardaa444402010-12-16 12:17:42 -0300450 return -EINVAL;
Mats Randgaardc027e162010-12-16 12:17:44 -0300451 }
452
453 return 0;
454}
455
456static int vpif_update_resolution(struct channel_obj *ch)
457{
458 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
459 struct video_obj *vid_ch = &ch->video;
460 struct vpif_params *vpifparams = &ch->vpifparams;
461 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
462
Hans Verkuil0598c172012-09-18 07:18:47 -0300463 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
Mats Randgaardc027e162010-12-16 12:17:44 -0300464 return -EINVAL;
465
Hans Verkuil0598c172012-09-18 07:18:47 -0300466 if (vid_ch->stdid) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300467 if (vpif_update_std_info(ch))
468 return -EINVAL;
469 }
Chaithrika U Se7332e32009-06-09 05:55:37 -0300470
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300471 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300472 common->fmt.fmt.pix.width = std_info->width;
473 common->fmt.fmt.pix.height = std_info->height;
474 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
475 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
476
477 /* Set height and width paramateres */
Mats Randgaardc027e162010-12-16 12:17:44 -0300478 common->height = std_info->height;
479 common->width = std_info->width;
Lad, Prabhakar9cba6532014-05-16 10:33:30 -0300480 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
481
482 if (vid_ch->stdid)
483 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
484 else
485 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
486
487 if (ch->vpifparams.std_info.frm_fmt)
488 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
489 else
490 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300491
492 return 0;
493}
494
495/*
496 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
497 * in the top and bottom field
498 */
499static void vpif_calculate_offsets(struct channel_obj *ch)
500{
501 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
502 struct vpif_params *vpifparams = &ch->vpifparams;
503 enum v4l2_field field = common->fmt.fmt.pix.field;
504 struct video_obj *vid_ch = &ch->video;
505 unsigned int hpitch, vpitch, sizeimage;
506
507 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
508 if (ch->vpifparams.std_info.frm_fmt)
509 vid_ch->buf_field = V4L2_FIELD_NONE;
510 else
511 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
512 } else {
513 vid_ch->buf_field = common->fmt.fmt.pix.field;
514 }
515
Lad, Prabhakar2401dd22012-06-28 09:28:36 -0300516 sizeimage = common->fmt.fmt.pix.sizeimage;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300517
518 hpitch = common->fmt.fmt.pix.bytesperline;
519 vpitch = sizeimage / (hpitch * 2);
520 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
521 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
522 common->ytop_off = 0;
523 common->ybtm_off = hpitch;
524 common->ctop_off = sizeimage / 2;
525 common->cbtm_off = sizeimage / 2 + hpitch;
526 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
527 common->ytop_off = 0;
528 common->ybtm_off = sizeimage / 4;
529 common->ctop_off = sizeimage / 2;
530 common->cbtm_off = common->ctop_off + sizeimage / 4;
531 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
532 common->ybtm_off = 0;
533 common->ytop_off = sizeimage / 4;
534 common->cbtm_off = sizeimage / 2;
535 common->ctop_off = common->cbtm_off + sizeimage / 4;
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;
652 pixfmt->priv = 0;
653
654 return 0;
655}
656
657static int vpif_s_fmt_vid_out(struct file *file, void *priv,
658 struct v4l2_format *fmt)
659{
660 struct video_device *vdev = video_devdata(file);
661 struct channel_obj *ch = video_get_drvdata(vdev);
662 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
663 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
664 int ret;
665
666 if (vb2_is_busy(&common->buffer_queue))
667 return -EBUSY;
668
669 ret = vpif_try_fmt_vid_out(file, priv, fmt);
670 if (ret)
671 return ret;
672
673 /* store the pix format in the channel object */
674 common->fmt.fmt.pix = *pixfmt;
675
676 /* store the format in the channel object */
677 common->fmt = *fmt;
678 return 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300679}
680
Hans Verkuil314527a2013-03-15 06:10:40 -0300681static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Chaithrika U Se7332e32009-06-09 05:55:37 -0300682{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300683 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300684 struct video_device *vdev = video_devdata(file);
685 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300686 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300687 struct vpif_display_chan_config *chan_cfg;
688 struct v4l2_output output;
689 int ret;
690
691 if (config->chan_config[ch->channel_id].outputs == NULL)
692 return -ENODATA;
693
694 chan_cfg = &config->chan_config[ch->channel_id];
695 output = chan_cfg->outputs[ch->output_idx].output;
696 if (output.capabilities != V4L2_OUT_CAP_STD)
697 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300698
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300699 if (vb2_is_busy(&common->buffer_queue))
700 return -EBUSY;
701
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300702
Hans Verkuil314527a2013-03-15 06:10:40 -0300703 if (!(std_id & VPIF_V4L2_STD))
Chaithrika U Se7332e32009-06-09 05:55:37 -0300704 return -EINVAL;
705
Chaithrika U Se7332e32009-06-09 05:55:37 -0300706 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300707 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300708 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Chaithrika U Se7332e32009-06-09 05:55:37 -0300709 /* Get the information about the standard */
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300710 if (vpif_update_resolution(ch))
711 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300712
Chaithrika U Se7332e32009-06-09 05:55:37 -0300713 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300714
715 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300716 s_std_output, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300717 if (ret < 0) {
718 vpif_err("Failed to set output standard\n");
Hans Verkuil9bfaae22011-01-05 13:35:45 -0300719 return ret;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300720 }
721
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300722 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300723 s_std, std_id);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300724 if (ret < 0)
725 vpif_err("Failed to set standard for sub devices\n");
Chaithrika U Se7332e32009-06-09 05:55:37 -0300726 return ret;
727}
728
729static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
730{
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300731 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300732 struct video_device *vdev = video_devdata(file);
733 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakarf27d0f42014-05-16 10:33:28 -0300734 struct vpif_display_chan_config *chan_cfg;
735 struct v4l2_output output;
736
737 if (config->chan_config[ch->channel_id].outputs == NULL)
738 return -ENODATA;
739
740 chan_cfg = &config->chan_config[ch->channel_id];
741 output = chan_cfg->outputs[ch->output_idx].output;
742 if (output.capabilities != V4L2_OUT_CAP_STD)
743 return -ENODATA;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300744
745 *std = ch->video.stdid;
746 return 0;
747}
748
Chaithrika U Se7332e32009-06-09 05:55:37 -0300749static int vpif_enum_output(struct file *file, void *fh,
750 struct v4l2_output *output)
751{
752
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -0300753 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300754 struct video_device *vdev = video_devdata(file);
755 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300756 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300757
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300758 chan_cfg = &config->chan_config[ch->channel_id];
759 if (output->index >= chan_cfg->output_count) {
Chaithrika U Se7332e32009-06-09 05:55:37 -0300760 vpif_dbg(1, debug, "Invalid output index\n");
761 return -EINVAL;
762 }
763
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300764 *output = chan_cfg->outputs[output->index].output;
765 return 0;
766}
Chaithrika U Se7332e32009-06-09 05:55:37 -0300767
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300768/**
769 * vpif_output_to_subdev() - Maps output to sub device
770 * @vpif_cfg - global config ptr
771 * @chan_cfg - channel config ptr
772 * @index - Given output index from application
773 *
774 * lookup the sub device information for a given output index.
775 * we report all the output to application. output table also
776 * has sub device name for the each output
777 */
778static int
779vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
780 struct vpif_display_chan_config *chan_cfg, int index)
781{
782 struct vpif_subdev_info *subdev_info;
783 const char *subdev_name;
784 int i;
785
786 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
787
788 if (chan_cfg->outputs == NULL)
789 return -1;
790
791 subdev_name = chan_cfg->outputs[index].subdev_name;
792 if (subdev_name == NULL)
793 return -1;
794
795 /* loop through the sub device list to get the sub device info */
796 for (i = 0; i < vpif_cfg->subdev_count; i++) {
797 subdev_info = &vpif_cfg->subdevinfo[i];
798 if (!strcmp(subdev_info->name, subdev_name))
799 return i;
800 }
801 return -1;
802}
803
804/**
805 * vpif_set_output() - Select an output
806 * @vpif_cfg - global config ptr
807 * @ch - channel
808 * @index - Given output index from application
809 *
810 * Select the given output.
811 */
812static int vpif_set_output(struct vpif_display_config *vpif_cfg,
813 struct channel_obj *ch, int index)
814{
815 struct vpif_display_chan_config *chan_cfg =
816 &vpif_cfg->chan_config[ch->channel_id];
817 struct vpif_subdev_info *subdev_info = NULL;
818 struct v4l2_subdev *sd = NULL;
819 u32 input = 0, output = 0;
820 int sd_index;
821 int ret;
822
823 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
824 if (sd_index >= 0) {
825 sd = vpif_obj.sd[sd_index];
826 subdev_info = &vpif_cfg->subdevinfo[sd_index];
827 }
828
829 if (sd) {
830 input = chan_cfg->outputs[index].input_route;
831 output = chan_cfg->outputs[index].output_route;
832 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
833 if (ret < 0 && ret != -ENOIOCTLCMD) {
834 vpif_err("Failed to set output\n");
835 return ret;
836 }
837
838 }
839 ch->output_idx = index;
840 ch->sd = sd;
841 if (chan_cfg->outputs != NULL)
842 /* update tvnorms from the sub device output info */
843 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300844 return 0;
845}
846
847static int vpif_s_output(struct file *file, void *priv, unsigned int i)
848{
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300849 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300850 struct video_device *vdev = video_devdata(file);
851 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300852 struct vpif_display_chan_config *chan_cfg;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300853 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300854
Lad, Prabhakar2f5851b2014-05-16 10:33:21 -0300855 if (vb2_is_busy(&common->buffer_queue))
856 return -EBUSY;
857
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300858 chan_cfg = &config->chan_config[ch->channel_id];
859
860 if (i >= chan_cfg->output_count)
861 return -EINVAL;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300862
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300863 return vpif_set_output(config, ch, i);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300864}
865
866static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
867{
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300868 struct video_device *vdev = video_devdata(file);
869 struct channel_obj *ch = video_get_drvdata(vdev);
Chaithrika U Se7332e32009-06-09 05:55:37 -0300870
Hans Verkuil311673e2012-09-20 09:06:23 -0300871 *i = ch->output_idx;
Chaithrika U Se7332e32009-06-09 05:55:37 -0300872
873 return 0;
874}
875
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300876/**
Hans Verkuil0598c172012-09-18 07:18:47 -0300877 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300878 * @file: file ptr
879 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -0300880 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300881 */
Hans Verkuil0598c172012-09-18 07:18:47 -0300882static int
883vpif_enum_dv_timings(struct file *file, void *priv,
884 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300885{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300886 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300887 struct video_device *vdev = video_devdata(file);
888 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300889 struct vpif_display_chan_config *chan_cfg;
890 struct v4l2_output output;
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300891 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300892
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300893 if (config->chan_config[ch->channel_id].outputs == NULL)
894 return -ENODATA;
895
896 chan_cfg = &config->chan_config[ch->channel_id];
897 output = chan_cfg->outputs[ch->output_idx].output;
898 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
899 return -ENODATA;
900
Laurent Pinchart10d21462014-01-31 09:04:19 -0300901 timings->pad = 0;
902
903 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjun63af4af2012-10-30 09:49:38 -0300904 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300905 return -EINVAL;
906 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300907}
908
909/**
Mats Randgaardc027e162010-12-16 12:17:44 -0300910 * vpif_s_dv_timings() - S_DV_TIMINGS handler
911 * @file: file ptr
912 * @priv: file handle
913 * @timings: digital video timings
914 */
915static int vpif_s_dv_timings(struct file *file, void *priv,
916 struct v4l2_dv_timings *timings)
917{
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300918 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -0300919 struct video_device *vdev = video_devdata(file);
920 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -0300921 struct vpif_params *vpifparams = &ch->vpifparams;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300922 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -0300923 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
924 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -0300925 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300926 struct vpif_display_chan_config *chan_cfg;
927 struct v4l2_output output;
Mats Randgaardc027e162010-12-16 12:17:44 -0300928 int ret;
929
Lad, Prabhakar1093c592014-05-16 10:33:27 -0300930 if (config->chan_config[ch->channel_id].outputs == NULL)
931 return -ENODATA;
932
933 chan_cfg = &config->chan_config[ch->channel_id];
934 output = chan_cfg->outputs[ch->output_idx].output;
935 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
936 return -ENODATA;
937
938 if (vb2_is_busy(&common->buffer_queue))
939 return -EBUSY;
940
Mats Randgaardc027e162010-12-16 12:17:44 -0300941 if (timings->type != V4L2_DV_BT_656_1120) {
942 vpif_dbg(2, debug, "Timing type not defined\n");
943 return -EINVAL;
944 }
945
946 /* Configure subdevice timings, if any */
Hans Verkuil882084a2012-09-20 09:06:31 -0300947 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
Lad, Prabhakar2bd4e582012-09-25 08:11:49 -0300948 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
949 ret = 0;
950 if (ret < 0) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300951 vpif_dbg(2, debug, "Error setting custom DV timings\n");
952 return ret;
953 }
954
955 if (!(timings->bt.width && timings->bt.height &&
956 (timings->bt.hbackporch ||
957 timings->bt.hfrontporch ||
958 timings->bt.hsync) &&
959 timings->bt.vfrontporch &&
960 (timings->bt.vbackporch ||
961 timings->bt.vsync))) {
962 vpif_dbg(2, debug, "Timings for width, height, "
963 "horizontal back porch, horizontal sync, "
964 "horizontal front porch, vertical back porch, "
965 "vertical sync and vertical back porch "
966 "must be defined\n");
967 return -EINVAL;
968 }
969
Hans Verkuil0598c172012-09-18 07:18:47 -0300970 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -0300971
972 /* Configure video port timings */
973
Hans Verkuile3655262013-07-29 08:41:00 -0300974 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -0300975 std_info->sav2eav = bt->width;
976
977 std_info->l1 = 1;
978 std_info->l3 = bt->vsync + bt->vbackporch + 1;
979
Hans Verkuile3655262013-07-29 08:41:00 -0300980 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -0300981 if (bt->interlaced) {
982 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -0300983 std_info->l5 = std_info->vsize/2 -
984 (bt->vfrontporch - 1);
985 std_info->l7 = std_info->vsize/2 + 1;
986 std_info->l9 = std_info->l7 + bt->il_vsync +
987 bt->il_vbackporch + 1;
988 std_info->l11 = std_info->vsize -
989 (bt->il_vfrontporch - 1);
990 } else {
991 vpif_dbg(2, debug, "Required timing values for "
992 "interlaced BT format missing\n");
993 return -EINVAL;
994 }
995 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -0300996 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
997 }
998 strncpy(std_info->name, "Custom timings BT656/1120",
999 VPIF_MAX_NAME);
1000 std_info->width = bt->width;
1001 std_info->height = bt->height;
1002 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1003 std_info->ycmux_mode = 0;
1004 std_info->capture_format = 0;
1005 std_info->vbi_supported = 0;
1006 std_info->hd_sd = 1;
1007 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001008 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001009
1010 return 0;
1011}
1012
1013/**
1014 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1015 * @file: file ptr
1016 * @priv: file handle
1017 * @timings: digital video timings
1018 */
1019static int vpif_g_dv_timings(struct file *file, void *priv,
1020 struct v4l2_dv_timings *timings)
1021{
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001022 struct vpif_display_config *config = vpif_dev->platform_data;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001023 struct video_device *vdev = video_devdata(file);
1024 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001025 struct vpif_display_chan_config *chan_cfg;
Mats Randgaardc027e162010-12-16 12:17:44 -03001026 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001027 struct v4l2_output output;
1028
1029 if (config->chan_config[ch->channel_id].outputs == NULL)
1030 goto error;
1031
1032 chan_cfg = &config->chan_config[ch->channel_id];
1033 output = chan_cfg->outputs[ch->output_idx].output;
1034
1035 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1036 goto error;
Mats Randgaardc027e162010-12-16 12:17:44 -03001037
Hans Verkuil0598c172012-09-18 07:18:47 -03001038 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001039
1040 return 0;
Lad, Prabhakar1093c592014-05-16 10:33:27 -03001041error:
1042 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001043}
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001044
1045/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001046 * vpif_log_status() - Status information
1047 * @file: file ptr
1048 * @priv: file handle
1049 *
1050 * Returns zero.
1051 */
1052static int vpif_log_status(struct file *filep, void *priv)
1053{
1054 /* status for sub devices */
1055 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1056
1057 return 0;
1058}
1059
Chaithrika U Se7332e32009-06-09 05:55:37 -03001060/* vpif display ioctl operations */
1061static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001062 .vidioc_querycap = vpif_querycap,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001063 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001064 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1065 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1066 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
Lad, Prabhakarb92cde12014-05-16 10:33:16 -03001067
1068 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1069 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1070 .vidioc_querybuf = vb2_ioctl_querybuf,
1071 .vidioc_qbuf = vb2_ioctl_qbuf,
1072 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1073 .vidioc_expbuf = vb2_ioctl_expbuf,
1074 .vidioc_streamon = vb2_ioctl_streamon,
1075 .vidioc_streamoff = vb2_ioctl_streamoff,
1076
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001077 .vidioc_s_std = vpif_s_std,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001078 .vidioc_g_std = vpif_g_std,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001079
Chaithrika U Se7332e32009-06-09 05:55:37 -03001080 .vidioc_enum_output = vpif_enum_output,
1081 .vidioc_s_output = vpif_s_output,
1082 .vidioc_g_output = vpif_g_output,
Lad, Prabhakardcce8662014-05-16 10:33:25 -03001083
1084 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1085 .vidioc_s_dv_timings = vpif_s_dv_timings,
1086 .vidioc_g_dv_timings = vpif_g_dv_timings,
1087
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001088 .vidioc_log_status = vpif_log_status,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001089};
1090
1091static const struct v4l2_file_operations vpif_fops = {
1092 .owner = THIS_MODULE,
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001093 .open = v4l2_fh_open,
1094 .release = vb2_fop_release,
Hans Verkuil9bfaae22011-01-05 13:35:45 -03001095 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001096 .mmap = vb2_fop_mmap,
1097 .poll = vb2_fop_poll
Chaithrika U Se7332e32009-06-09 05:55:37 -03001098};
1099
Chaithrika U Se7332e32009-06-09 05:55:37 -03001100/*Configure the channels, buffer sizei, request irq */
1101static int initialize_vpif(void)
1102{
1103 int free_channel_objects_index;
Lad, Prabhakard5b94b92014-05-16 10:33:23 -03001104 int err, i, j;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001105
1106 /* Allocate memory for six channel objects */
1107 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1108 vpif_obj.dev[i] =
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001109 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001110 /* If memory allocation fails, return error */
1111 if (!vpif_obj.dev[i]) {
1112 free_channel_objects_index = i;
1113 err = -ENOMEM;
1114 goto vpif_init_free_channel_objects;
1115 }
1116 }
1117
Chaithrika U Se7332e32009-06-09 05:55:37 -03001118 return 0;
1119
1120vpif_init_free_channel_objects:
1121 for (j = 0; j < free_channel_objects_index; j++)
1122 kfree(vpif_obj.dev[j]);
1123 return err;
1124}
1125
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001126static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1127 struct v4l2_subdev *subdev,
1128 struct v4l2_async_subdev *asd)
1129{
1130 int i;
1131
1132 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1133 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1134 subdev->name)) {
1135 vpif_obj.sd[i] = subdev;
1136 vpif_obj.sd[i]->grp_id = 1 << i;
1137 return 0;
1138 }
1139
1140 return -EINVAL;
1141}
1142
1143static int vpif_probe_complete(void)
1144{
1145 struct common_obj *common;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001146 struct video_device *vdev;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001147 struct channel_obj *ch;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001148 struct vb2_queue *q;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001149 int j, err, k;
1150
1151 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1152 ch = vpif_obj.dev[j];
1153 /* Initialize field of the channel objects */
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001154 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001155 common = &ch->common[k];
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001156 spin_lock_init(&common->irqlock);
1157 mutex_init(&common->lock);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001158 common->set_addr = NULL;
1159 common->ytop_off = 0;
1160 common->ybtm_off = 0;
1161 common->ctop_off = 0;
1162 common->cbtm_off = 0;
1163 common->cur_frm = NULL;
1164 common->next_frm = NULL;
1165 memset(&common->fmt, 0, sizeof(common->fmt));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001166 }
1167 ch->initialized = 0;
1168 if (vpif_obj.config->subdev_count)
1169 ch->sd = vpif_obj.sd[0];
1170 ch->channel_id = j;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001171
1172 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1173
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001174 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1175 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001176
1177 /* select output 0 */
1178 err = vpif_set_output(vpif_obj.config, ch, 0);
1179 if (err)
1180 goto probe_out;
1181
Lad, Prabhakar9cba6532014-05-16 10:33:30 -03001182 /* set initial format */
1183 ch->video.stdid = V4L2_STD_525_60;
1184 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1185 vpif_update_resolution(ch);
1186
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001187 /* Initialize vb2 queue */
1188 q = &common->buffer_queue;
1189 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1190 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1191 q->drv_priv = ch;
1192 q->ops = &video_qops;
1193 q->mem_ops = &vb2_dma_contig_memops;
1194 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1195 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1196 q->min_buffers_needed = 1;
Lad, Prabhakard10ed5c2014-05-16 10:33:08 -03001197 q->lock = &common->lock;
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001198 err = vb2_queue_init(q);
1199 if (err) {
1200 vpif_err("vpif_display: vb2_queue_init() failed\n");
1201 goto probe_out;
1202 }
1203
1204 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1205 if (IS_ERR(common->alloc_ctx)) {
1206 vpif_err("Failed to get the context\n");
1207 err = PTR_ERR(common->alloc_ctx);
1208 goto probe_out;
1209 }
1210
1211 INIT_LIST_HEAD(&common->dma_queue);
1212
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001213 /* register video device */
1214 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1215 (int)ch, (int)&ch->video_dev);
1216
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001217 /* Initialize the video_device structure */
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001218 vdev = ch->video_dev;
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001219 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1220 vdev->release = video_device_release;
1221 vdev->fops = &vpif_fops;
1222 vdev->ioctl_ops = &vpif_ioctl_ops;
1223 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1224 vdev->vfl_dir = VFL_DIR_TX;
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001225 vdev->queue = q;
Lad, Prabhakarfcc22af2014-05-16 10:33:15 -03001226 vdev->lock = &common->lock;
1227 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1228 video_set_drvdata(ch->video_dev, ch);
Lad, Prabhakar4be21532014-05-16 10:33:14 -03001229 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1230 (j ? 3 : 2));
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001231 if (err < 0)
1232 goto probe_out;
1233 }
1234
1235 return 0;
1236
1237probe_out:
1238 for (k = 0; k < j; k++) {
1239 ch = vpif_obj.dev[k];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001240 common = &ch->common[k];
1241 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001242 video_unregister_device(ch->video_dev);
1243 video_device_release(ch->video_dev);
1244 ch->video_dev = NULL;
1245 }
1246 return err;
1247}
1248
1249static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1250{
1251 return vpif_probe_complete();
1252}
1253
Chaithrika U Se7332e32009-06-09 05:55:37 -03001254/*
1255 * vpif_probe: This function creates device entries by register itself to the
1256 * V4L2 driver and initializes fields of each channel objects
1257 */
1258static __init int vpif_probe(struct platform_device *pdev)
1259{
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001260 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001261 int i, j = 0, err = 0;
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001262 int res_idx = 0;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001263 struct i2c_adapter *i2c_adap;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001264 struct channel_obj *ch;
1265 struct video_device *vfd;
1266 struct resource *res;
1267 int subdev_count;
1268
1269 vpif_dev = &pdev->dev;
Muralidharan Karicheri317b2e22009-09-16 14:30:53 -03001270 err = initialize_vpif();
1271
1272 if (err) {
1273 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1274 return err;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001275 }
1276
Chaithrika U Se7332e32009-06-09 05:55:37 -03001277 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1278 if (err) {
1279 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1280 return err;
1281 }
1282
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001283 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001284 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001285 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakar379d2cf2013-06-17 11:20:51 -03001286 (void *)(&vpif_obj.dev[res_idx]->
1287 channel_id));
1288 if (err) {
1289 err = -EINVAL;
1290 vpif_err("VPIF IRQ request failed\n");
1291 goto vpif_unregister;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001292 }
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001293 res_idx++;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001294 }
1295
1296 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001297 /* Get the pointer to the channel object */
1298 ch = vpif_obj.dev[i];
1299
1300 /* Allocate memory for video device */
1301 vfd = video_device_alloc();
1302 if (vfd == NULL) {
1303 for (j = 0; j < i; j++) {
1304 ch = vpif_obj.dev[j];
1305 video_device_release(ch->video_dev);
1306 }
1307 err = -ENOMEM;
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001308 goto vpif_unregister;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001309 }
1310
Chaithrika U Se7332e32009-06-09 05:55:37 -03001311 /* Set video_dev to the video device */
1312 ch->video_dev = vfd;
1313 }
1314
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001315 vpif_obj.config = pdev->dev.platform_data;
1316 subdev_count = vpif_obj.config->subdev_count;
1317 subdevdata = vpif_obj.config->subdevinfo;
Hans Verkuile6067f82012-09-20 09:06:27 -03001318 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1319 GFP_KERNEL);
1320 if (vpif_obj.sd == NULL) {
1321 vpif_err("unable to allocate memory for subdevice pointers\n");
1322 err = -ENOMEM;
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001323 goto vpif_sd_error;
Hans Verkuile6067f82012-09-20 09:06:27 -03001324 }
1325
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001326 if (!vpif_obj.config->asd_sizes) {
1327 i2c_adap = i2c_get_adapter(1);
1328 for (i = 0; i < subdev_count; i++) {
1329 vpif_obj.sd[i] =
1330 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1331 i2c_adap,
1332 &subdevdata[i].
1333 board_info,
1334 NULL);
1335 if (!vpif_obj.sd[i]) {
1336 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun9d3e9762013-08-22 22:59:44 -03001337 err = -ENODEV;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001338 goto probe_subdev_out;
1339 }
1340
1341 if (vpif_obj.sd[i])
1342 vpif_obj.sd[i]->grp_id = 1 << i;
1343 }
1344 vpif_probe_complete();
1345 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001346 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar4b8a5312013-06-25 11:17:35 -03001347 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1348 vpif_obj.notifier.bound = vpif_async_bound;
1349 vpif_obj.notifier.complete = vpif_async_complete;
1350 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1351 &vpif_obj.notifier);
1352 if (err) {
1353 vpif_err("Error registering async notifier\n");
1354 err = -EINVAL;
Hans Verkuile6067f82012-09-20 09:06:27 -03001355 goto probe_subdev_out;
1356 }
Hans Verkuile6067f82012-09-20 09:06:27 -03001357 }
1358
Chaithrika U Se7332e32009-06-09 05:55:37 -03001359 return 0;
1360
Hans Verkuile6067f82012-09-20 09:06:27 -03001361probe_subdev_out:
1362 kfree(vpif_obj.sd);
Hans Verkuil01b1d9752012-09-20 09:06:28 -03001363vpif_sd_error:
1364 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1365 ch = vpif_obj.dev[i];
1366 /* Note: does nothing if ch->video_dev == NULL */
1367 video_device_release(ch->video_dev);
1368 }
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001369vpif_unregister:
Chaithrika U Se7332e32009-06-09 05:55:37 -03001370 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001371
1372 return err;
1373}
1374
1375/*
1376 * vpif_remove: It un-register channels from V4L2 driver
1377 */
1378static int vpif_remove(struct platform_device *device)
1379{
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001380 struct common_obj *common;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001381 struct channel_obj *ch;
Lad, Prabhakar9c63e012013-06-17 11:20:50 -03001382 int i;
Chaithrika U Se7332e32009-06-09 05:55:37 -03001383
1384 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1385
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001386 kfree(vpif_obj.sd);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001387 /* un-register device */
1388 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1389 /* Get the pointer to the channel object */
1390 ch = vpif_obj.dev[i];
Lad, Prabhakara2b235c2014-05-16 10:33:06 -03001391 common = &ch->common[i];
1392 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001393 /* Unregister video device */
1394 video_unregister_device(ch->video_dev);
1395
1396 ch->video_dev = NULL;
Lad, Prabhakar0b361582013-06-17 11:20:48 -03001397 kfree(vpif_obj.dev[i]);
Chaithrika U Se7332e32009-06-09 05:55:37 -03001398 }
1399
1400 return 0;
1401}
1402
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001403#ifdef CONFIG_PM_SLEEP
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001404static int vpif_suspend(struct device *dev)
1405{
1406 struct common_obj *common;
1407 struct channel_obj *ch;
1408 int i;
1409
1410 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1411 /* Get the pointer to the channel object */
1412 ch = vpif_obj.dev[i];
1413 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001414
1415 if (!vb2_is_streaming(&common->buffer_queue))
1416 continue;
1417
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001418 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001419 /* Disable channel */
1420 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1421 enable_channel2(0);
1422 channel2_intr_enable(0);
1423 }
1424 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1425 ycmux_mode == 2) {
1426 enable_channel3(0);
1427 channel3_intr_enable(0);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001428 }
1429 mutex_unlock(&common->lock);
1430 }
1431
1432 return 0;
1433}
1434
1435static int vpif_resume(struct device *dev)
1436{
1437
1438 struct common_obj *common;
1439 struct channel_obj *ch;
1440 int i;
1441
1442 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1443 /* Get the pointer to the channel object */
1444 ch = vpif_obj.dev[i];
1445 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001446
1447 if (!vb2_is_streaming(&common->buffer_queue))
1448 continue;
1449
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001450 mutex_lock(&common->lock);
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001451 /* Enable channel */
1452 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1453 enable_channel2(1);
1454 channel2_intr_enable(1);
1455 }
1456 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1457 ycmux_mode == 2) {
1458 enable_channel3(1);
1459 channel3_intr_enable(1);
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001460 }
1461 mutex_unlock(&common->lock);
1462 }
1463
1464 return 0;
1465}
1466
Manjunath Hadlie9530da2012-04-13 04:50:35 -03001467#endif
1468
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001469static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1470
Mats Randgaardffa1b392010-08-30 10:30:36 -03001471static __refdata struct platform_driver vpif_driver = {
Chaithrika U Se7332e32009-06-09 05:55:37 -03001472 .driver = {
Lad, Prabhakar76c4c2b2014-05-16 10:33:22 -03001473 .name = VPIF_DRIVER_NAME,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001474 .owner = THIS_MODULE,
Lad, Prabhakarba1902e2014-05-16 10:33:26 -03001475 .pm = &vpif_pm_ops,
Chaithrika U Se7332e32009-06-09 05:55:37 -03001476 },
1477 .probe = vpif_probe,
1478 .remove = vpif_remove,
1479};
1480
Lad, Prabhakarb8d067b2013-06-17 11:20:49 -03001481module_platform_driver(vpif_driver);