blob: 8572efe949c8210e87210e80cce3236636102185 [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
20 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030021
Lad, Prabhakar012eef72013-04-19 05:53:29 -030022#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26
Lad, Prabhakar012eef72013-04-19 05:53:29 -030027#include <media/v4l2-ioctl.h>
28
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030029#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030030#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030031
32MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
33MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030034MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030035
36#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
37#define vpif_dbg(level, debug, fmt, arg...) \
38 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
39
40static int debug = 1;
41static u32 ch0_numbuffers = 3;
42static u32 ch1_numbuffers = 3;
43static u32 ch0_bufsize = 1920 * 1080 * 2;
44static u32 ch1_bufsize = 720 * 576 * 2;
45
46module_param(debug, int, 0644);
47module_param(ch0_numbuffers, uint, S_IRUGO);
48module_param(ch1_numbuffers, uint, S_IRUGO);
49module_param(ch0_bufsize, uint, S_IRUGO);
50module_param(ch1_bufsize, uint, S_IRUGO);
51
52MODULE_PARM_DESC(debug, "Debug level 0-1");
53MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
54MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
55MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
56MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
57
58static struct vpif_config_params config_params = {
59 .min_numbuffers = 3,
60 .numbuffers[0] = 3,
61 .numbuffers[1] = 3,
62 .min_bufsize[0] = 720 * 480 * 2,
63 .min_bufsize[1] = 720 * 480 * 2,
64 .channel_bufsize[0] = 1920 * 1080 * 2,
65 .channel_bufsize[1] = 720 * 576 * 2,
66};
67
68/* global variables */
69static struct vpif_device vpif_obj = { {NULL} };
70static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030071static void vpif_calculate_offsets(struct channel_obj *ch);
72static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030073
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030074static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
75
76static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
77{
78 return container_of(vb, struct vpif_cap_buffer, vb);
79}
80
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030081/**
82 * buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030083 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030084 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030085 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030086 * function is called. The buffer is prepared and user space virtual address
87 * or user address is converted into physical address
88 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030089static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030090{
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030091 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030092 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030093 struct common_obj *common;
94 unsigned long addr;
95
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030096 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
97
98 common = &ch->common[VPIF_VIDEO_INDEX];
99
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300100 if (vb->state != VB2_BUF_STATE_ACTIVE &&
101 vb->state != VB2_BUF_STATE_PREPARED) {
102 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
103 if (vb2_plane_vaddr(vb, 0) &&
104 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
105 goto exit;
106 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300107
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300108 if (q->streaming) {
109 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
110 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
111 !IS_ALIGNED((addr + common->ctop_off), 8) ||
112 !IS_ALIGNED((addr + common->cbtm_off), 8))
113 goto exit;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300114 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300115 }
116 return 0;
117exit:
118 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
119 return -EINVAL;
120}
121
122/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300123 * vpif_buffer_queue_setup : Callback function for buffer setup.
124 * @vq: vb2_queue ptr
125 * @fmt: v4l2 format
126 * @nbuffers: ptr to number of buffers requested by application
127 * @nplanes:: contains number of distinct video planes needed to hold a frame
128 * @sizes[]: contains the size (in bytes) of each plane.
129 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300130 *
131 * This callback function is called when reqbuf() is called to adjust
132 * the buffer count and buffer size
133 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300134static int vpif_buffer_queue_setup(struct vb2_queue *vq,
135 const struct v4l2_format *fmt,
136 unsigned int *nbuffers, unsigned int *nplanes,
137 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300138{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300139 struct channel_obj *ch = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300140 struct common_obj *common;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300141 unsigned long size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300142
143 common = &ch->common[VPIF_VIDEO_INDEX];
144
145 vpif_dbg(2, debug, "vpif_buffer_setup\n");
146
147 /* If memory type is not mmap, return */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300148 if (V4L2_MEMORY_MMAP == common->memory) {
149 /* Calculate the size of the buffer */
150 size = config_params.channel_bufsize[ch->channel_id];
151 /*
152 * Checking if the buffer size exceeds the available buffer
153 * ycmux_mode = 0 means 1 channel mode HD and
154 * ycmux_mode = 1 means 2 channels mode SD
155 */
156 if (ch->vpifparams.std_info.ycmux_mode == 0) {
157 if (config_params.video_limit[ch->channel_id])
158 while (size * *nbuffers >
159 (config_params.video_limit[0]
Manjunath Hadli764af392012-04-13 04:49:34 -0300160 + config_params.video_limit[1]))
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300161 (*nbuffers)--;
162 } else {
163 if (config_params.video_limit[ch->channel_id])
164 while (size * *nbuffers >
Manjunath Hadli764af392012-04-13 04:49:34 -0300165 config_params.video_limit[ch->channel_id])
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300166 (*nbuffers)--;
167 }
168
169 } else {
170 size = common->fmt.fmt.pix.sizeimage;
Manjunath Hadli764af392012-04-13 04:49:34 -0300171 }
172
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300173 if (*nbuffers < config_params.min_numbuffers)
174 *nbuffers = config_params.min_numbuffers;
175
176 *nplanes = 1;
177 sizes[0] = size;
178 alloc_ctxs[0] = common->alloc_ctx;
179
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300180 return 0;
181}
182
183/**
184 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300185 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300186 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300187static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300188{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300189 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
190 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300191 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300192 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300193
194 common = &ch->common[VPIF_VIDEO_INDEX];
195
196 vpif_dbg(2, debug, "vpif_buffer_queue\n");
197
Hans Verkuilaec96832012-11-16 12:03:06 -0300198 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300199 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300200 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300201 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300202}
203
204/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300205 * vpif_buf_cleanup : Callback function to free buffer
206 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300207 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300208 * This function is called from the videobuf2 layer to free memory
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300209 * allocated to the buffers
210 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300211static void vpif_buf_cleanup(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300212{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300213 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
214 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300215 struct common_obj *common;
216 unsigned long flags;
217
218 common = &ch->common[VPIF_VIDEO_INDEX];
219
220 spin_lock_irqsave(&common->irqlock, flags);
221 if (vb->state == VB2_BUF_STATE_ACTIVE)
222 list_del_init(&buf->list);
223 spin_unlock_irqrestore(&common->irqlock, flags);
224
225}
226
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300227static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
228{
229 struct vpif_capture_config *vpif_config_data =
230 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300231 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300232 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
233 struct vpif_params *vpif = &ch->vpifparams;
234 unsigned long addr = 0;
Hans Verkuilaec96832012-11-16 12:03:06 -0300235 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300236 int ret;
237
Hans Verkuilaec96832012-11-16 12:03:06 -0300238 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300239
240 /* Get the next frame from the buffer queue */
241 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
242 struct vpif_cap_buffer, list);
243 /* Remove buffer from the buffer queue */
244 list_del(&common->cur_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300245 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300246 /* Mark state of the current frame to active */
247 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
248 /* Initialize field_id and started member */
249 ch->field_id = 0;
250 common->started = 1;
251 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
252
253 /* Calculate the offset for Y and C data in the buffer */
254 vpif_calculate_offsets(ch);
255
256 if ((vpif->std_info.frm_fmt &&
257 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
258 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
259 (!vpif->std_info.frm_fmt &&
260 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
261 vpif_dbg(1, debug, "conflict in field format and std format\n");
262 return -EINVAL;
263 }
264
265 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300266 if (vpif_config_data->setup_input_channel_mode) {
267 ret = vpif_config_data->
268 setup_input_channel_mode(vpif->std_info.ycmux_mode);
269 if (ret < 0) {
270 vpif_dbg(1, debug, "can't set vpif channel mode\n");
271 return ret;
272 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300273 }
274
275 /* Call vpif_set_params function to set the parameters and addresses */
276 ret = vpif_set_video_params(vpif, ch->channel_id);
277
278 if (ret < 0) {
279 vpif_dbg(1, debug, "can't set video params\n");
280 return ret;
281 }
282
283 common->started = ret;
284 vpif_config_addr(ch, ret);
285
286 common->set_addr(addr + common->ytop_off,
287 addr + common->ybtm_off,
288 addr + common->ctop_off,
289 addr + common->cbtm_off);
290
291 /**
292 * Set interrupt for both the fields in VPIF Register enable channel in
293 * VPIF register
294 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300295 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300296 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
297 channel0_intr_assert();
298 channel0_intr_enable(1);
299 enable_channel0(1);
300 }
301 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
302 (common->started == 2)) {
303 channel1_intr_assert();
304 channel1_intr_enable(1);
305 enable_channel1(1);
306 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300307
308 return 0;
309}
310
311/* abort streaming and wait for last buffer */
Hans Verkuile37559b2014-04-17 02:47:21 -0300312static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300313{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300314 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300315 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300316 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300317
318 if (!vb2_is_streaming(vq))
Hans Verkuile37559b2014-04-17 02:47:21 -0300319 return;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300320
321 common = &ch->common[VPIF_VIDEO_INDEX];
322
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300323 /* Disable channel as per its device type and channel id */
324 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
325 enable_channel0(0);
326 channel0_intr_enable(0);
327 }
328 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
329 (2 == common->started)) {
330 enable_channel1(0);
331 channel1_intr_enable(0);
332 }
333 common->started = 0;
334
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300335 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300336 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300337 if (common->cur_frm == common->next_frm) {
338 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
339 } else {
340 if (common->cur_frm != NULL)
341 vb2_buffer_done(&common->cur_frm->vb,
342 VB2_BUF_STATE_ERROR);
343 if (common->next_frm != NULL)
344 vb2_buffer_done(&common->next_frm->vb,
345 VB2_BUF_STATE_ERROR);
346 }
347
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300348 while (!list_empty(&common->dma_queue)) {
349 common->next_frm = list_entry(common->dma_queue.next,
350 struct vpif_cap_buffer, list);
351 list_del(&common->next_frm->list);
352 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
353 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300354 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300355}
356
357static struct vb2_ops video_qops = {
358 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300359 .buf_prepare = vpif_buffer_prepare,
360 .start_streaming = vpif_start_streaming,
361 .stop_streaming = vpif_stop_streaming,
362 .buf_cleanup = vpif_buf_cleanup,
363 .buf_queue = vpif_buffer_queue,
364};
365
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300366/**
367 * vpif_process_buffer_complete: process a completed buffer
368 * @common: ptr to common channel object
369 *
370 * This function time stamp the buffer and mark it as DONE. It also
371 * wake up any process waiting on the QUEUE and set the next buffer
372 * as current
373 */
374static void vpif_process_buffer_complete(struct common_obj *common)
375{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300376 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300377 vb2_buffer_done(&common->cur_frm->vb,
378 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300379 /* Make curFrm pointing to nextFrm */
380 common->cur_frm = common->next_frm;
381}
382
383/**
384 * vpif_schedule_next_buffer: set next buffer address for capture
385 * @common : ptr to common channel object
386 *
387 * This function will get next buffer from the dma queue and
388 * set the buffer address in the vpif register for capture.
389 * the buffer is marked active
390 */
391static void vpif_schedule_next_buffer(struct common_obj *common)
392{
393 unsigned long addr = 0;
394
Hans Verkuilaec96832012-11-16 12:03:06 -0300395 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300396 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300397 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300398 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300399 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300400 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300401 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
402 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300403
404 /* Set top and bottom field addresses in VPIF registers */
405 common->set_addr(addr + common->ytop_off,
406 addr + common->ybtm_off,
407 addr + common->ctop_off,
408 addr + common->cbtm_off);
409}
410
411/**
412 * vpif_channel_isr : ISR handler for vpif capture
413 * @irq: irq number
414 * @dev_id: dev_id ptr
415 *
416 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300417 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300418 */
419static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
420{
421 struct vpif_device *dev = &vpif_obj;
422 struct common_obj *common;
423 struct channel_obj *ch;
424 enum v4l2_field field;
425 int channel_id = 0;
426 int fid = -1, i;
427
428 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300429 if (!vpif_intr_status(channel_id))
430 return IRQ_NONE;
431
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300432 ch = dev->dev[channel_id];
433
434 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
435
436 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
437 common = &ch->common[i];
438 /* skip If streaming is not started in this channel */
439 if (0 == common->started)
440 continue;
441
442 /* Check the field format */
443 if (1 == ch->vpifparams.std_info.frm_fmt) {
444 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300445 spin_lock(&common->irqlock);
446 if (list_empty(&common->dma_queue)) {
447 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300448 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300449 }
450 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300451
452 if (!channel_first_int[i][channel_id])
453 vpif_process_buffer_complete(common);
454
455 channel_first_int[i][channel_id] = 0;
456
457 vpif_schedule_next_buffer(common);
458
459
460 channel_first_int[i][channel_id] = 0;
461 } else {
462 /**
463 * Interlaced mode. If it is first interrupt, ignore
464 * it
465 */
466 if (channel_first_int[i][channel_id]) {
467 channel_first_int[i][channel_id] = 0;
468 continue;
469 }
470 if (0 == i) {
471 ch->field_id ^= 1;
472 /* Get field id from VPIF registers */
473 fid = vpif_channel_getfid(ch->channel_id);
474 if (fid != ch->field_id) {
475 /**
476 * If field id does not match stored
477 * field id, make them in sync
478 */
479 if (0 == fid)
480 ch->field_id = fid;
481 return IRQ_HANDLED;
482 }
483 }
484 /* device field id and local field id are in sync */
485 if (0 == fid) {
486 /* this is even field */
487 if (common->cur_frm == common->next_frm)
488 continue;
489
490 /* mark the current buffer as done */
491 vpif_process_buffer_complete(common);
492 } else if (1 == fid) {
493 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300494 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300495 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300496 (common->cur_frm != common->next_frm)) {
497 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300498 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300499 }
500 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300501
502 vpif_schedule_next_buffer(common);
503 }
504 }
505 }
506 return IRQ_HANDLED;
507}
508
509/**
510 * vpif_update_std_info() - update standard related info
511 * @ch: ptr to channel object
512 *
513 * For a given standard selected by application, update values
514 * in the device data structures
515 */
516static int vpif_update_std_info(struct channel_obj *ch)
517{
518 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
519 struct vpif_params *vpifparams = &ch->vpifparams;
520 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300521 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300522 struct video_obj *vid_ch = &ch->video;
523 int index;
524
525 vpif_dbg(2, debug, "vpif_update_std_info\n");
526
Mats Randgaardaa444402010-12-16 12:17:42 -0300527 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300528 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300529 if (config->hd_sd == 0) {
530 vpif_dbg(2, debug, "SD format\n");
531 if (config->stdid & vid_ch->stdid) {
532 memcpy(std_info, config, sizeof(*config));
533 break;
534 }
535 } else {
536 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300537 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
538 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300539 memcpy(std_info, config, sizeof(*config));
540 break;
541 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300542 }
543 }
544
545 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300546 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300547 return -EINVAL;
548
549 common->fmt.fmt.pix.width = std_info->width;
550 common->width = std_info->width;
551 common->fmt.fmt.pix.height = std_info->height;
552 common->height = std_info->height;
553 common->fmt.fmt.pix.bytesperline = std_info->width;
554 vpifparams->video_params.hpitch = std_info->width;
555 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300556
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300557 return 0;
558}
559
560/**
561 * vpif_calculate_offsets : This function calculates buffers offsets
562 * @ch : ptr to channel object
563 *
564 * This function calculates buffer offsets for Y and C in the top and
565 * bottom field
566 */
567static void vpif_calculate_offsets(struct channel_obj *ch)
568{
569 unsigned int hpitch, vpitch, sizeimage;
570 struct video_obj *vid_ch = &(ch->video);
571 struct vpif_params *vpifparams = &ch->vpifparams;
572 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
573 enum v4l2_field field = common->fmt.fmt.pix.field;
574
575 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
576
577 if (V4L2_FIELD_ANY == field) {
578 if (vpifparams->std_info.frm_fmt)
579 vid_ch->buf_field = V4L2_FIELD_NONE;
580 else
581 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
582 } else
583 vid_ch->buf_field = common->fmt.fmt.pix.field;
584
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300585 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300586
587 hpitch = common->fmt.fmt.pix.bytesperline;
588 vpitch = sizeimage / (hpitch * 2);
589
590 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
591 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
592 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
593 common->ytop_off = 0;
594 common->ybtm_off = hpitch;
595 common->ctop_off = sizeimage / 2;
596 common->cbtm_off = sizeimage / 2 + hpitch;
597 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
598 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
599 common->ytop_off = 0;
600 common->ybtm_off = sizeimage / 4;
601 common->ctop_off = sizeimage / 2;
602 common->cbtm_off = common->ctop_off + sizeimage / 4;
603 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
604 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
605 common->ybtm_off = 0;
606 common->ytop_off = sizeimage / 4;
607 common->cbtm_off = sizeimage / 2;
608 common->ctop_off = common->cbtm_off + sizeimage / 4;
609 }
610 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
611 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
612 vpifparams->video_params.storage_mode = 1;
613 else
614 vpifparams->video_params.storage_mode = 0;
615
616 if (1 == vpifparams->std_info.frm_fmt)
617 vpifparams->video_params.hpitch =
618 common->fmt.fmt.pix.bytesperline;
619 else {
620 if ((field == V4L2_FIELD_ANY)
621 || (field == V4L2_FIELD_INTERLACED))
622 vpifparams->video_params.hpitch =
623 common->fmt.fmt.pix.bytesperline * 2;
624 else
625 vpifparams->video_params.hpitch =
626 common->fmt.fmt.pix.bytesperline;
627 }
628
629 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
630}
631
632/**
633 * vpif_config_format: configure default frame format in the device
634 * ch : ptr to channel object
635 */
636static void vpif_config_format(struct channel_obj *ch)
637{
638 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
639
640 vpif_dbg(2, debug, "vpif_config_format\n");
641
642 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
643 if (config_params.numbuffers[ch->channel_id] == 0)
644 common->memory = V4L2_MEMORY_USERPTR;
645 else
646 common->memory = V4L2_MEMORY_MMAP;
647
648 common->fmt.fmt.pix.sizeimage
649 = config_params.channel_bufsize[ch->channel_id];
650
651 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
652 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
653 else
654 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
655 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
656}
657
658/**
659 * vpif_get_default_field() - Get default field type based on interface
660 * @vpif_params - ptr to vpif params
661 */
662static inline enum v4l2_field vpif_get_default_field(
663 struct vpif_interface *iface)
664{
665 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
666 V4L2_FIELD_INTERLACED;
667}
668
669/**
670 * vpif_check_format() - check given pixel format for compatibility
671 * @ch - channel ptr
672 * @pixfmt - Given pixel format
673 * @update - update the values as per hardware requirement
674 *
675 * Check the application pixel format for S_FMT and update the input
676 * values as per hardware limits for TRY_FMT. The default pixel and
677 * field format is selected based on interface type.
678 */
679static int vpif_check_format(struct channel_obj *ch,
680 struct v4l2_pix_format *pixfmt,
681 int update)
682{
683 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
684 struct vpif_params *vpif_params = &ch->vpifparams;
685 enum v4l2_field field = pixfmt->field;
686 u32 sizeimage, hpitch, vpitch;
687 int ret = -EINVAL;
688
689 vpif_dbg(2, debug, "vpif_check_format\n");
690 /**
691 * first check for the pixel format. If if_type is Raw bayer,
692 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
693 * V4L2_PIX_FMT_YUV422P is supported
694 */
695 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
696 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
697 if (!update) {
698 vpif_dbg(2, debug, "invalid pix format\n");
699 goto exit;
700 }
701 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
702 }
703 } else {
704 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
705 if (!update) {
706 vpif_dbg(2, debug, "invalid pixel format\n");
707 goto exit;
708 }
709 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
710 }
711 }
712
713 if (!(VPIF_VALID_FIELD(field))) {
714 if (!update) {
715 vpif_dbg(2, debug, "invalid field format\n");
716 goto exit;
717 }
718 /**
719 * By default use FIELD_NONE for RAW Bayer capture
720 * and FIELD_INTERLACED for other interfaces
721 */
722 field = vpif_get_default_field(&vpif_params->iface);
723 } else if (field == V4L2_FIELD_ANY)
724 /* unsupported field. Use default */
725 field = vpif_get_default_field(&vpif_params->iface);
726
727 /* validate the hpitch */
728 hpitch = pixfmt->bytesperline;
729 if (hpitch < vpif_params->std_info.width) {
730 if (!update) {
731 vpif_dbg(2, debug, "invalid hpitch\n");
732 goto exit;
733 }
734 hpitch = vpif_params->std_info.width;
735 }
736
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300737 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300738
739 vpitch = sizeimage / (hpitch * 2);
740
741 /* validate the vpitch */
742 if (vpitch < vpif_params->std_info.height) {
743 if (!update) {
744 vpif_dbg(2, debug, "Invalid vpitch\n");
745 goto exit;
746 }
747 vpitch = vpif_params->std_info.height;
748 }
749
750 /* Check for 8 byte alignment */
751 if (!ALIGN(hpitch, 8)) {
752 if (!update) {
753 vpif_dbg(2, debug, "invalid pitch alignment\n");
754 goto exit;
755 }
756 /* adjust to next 8 byte boundary */
757 hpitch = (((hpitch + 7) / 8) * 8);
758 }
759 /* if update is set, modify the bytesperline and sizeimage */
760 if (update) {
761 pixfmt->bytesperline = hpitch;
762 pixfmt->sizeimage = hpitch * vpitch * 2;
763 }
764 /**
765 * Image width and height is always based on current standard width and
766 * height
767 */
768 pixfmt->width = common->fmt.fmt.pix.width;
769 pixfmt->height = common->fmt.fmt.pix.height;
770 return 0;
771exit:
772 return ret;
773}
774
775/**
776 * vpif_config_addr() - function to configure buffer address in vpif
777 * @ch - channel ptr
778 * @muxmode - channel mux mode
779 */
780static void vpif_config_addr(struct channel_obj *ch, int muxmode)
781{
782 struct common_obj *common;
783
784 vpif_dbg(2, debug, "vpif_config_addr\n");
785
786 common = &(ch->common[VPIF_VIDEO_INDEX]);
787
788 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
789 common->set_addr = ch1_set_videobuf_addr;
790 else if (2 == muxmode)
791 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
792 else
793 common->set_addr = ch0_set_videobuf_addr;
794}
795
796/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300797 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300798 * @filep: file pointer
799 * @vma: ptr to vm_area_struct
800 */
801static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
802{
803 /* Get the channel object and file handle object */
804 struct vpif_fh *fh = filep->private_data;
805 struct channel_obj *ch = fh->channel;
806 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300807 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300808
809 vpif_dbg(2, debug, "vpif_mmap\n");
810
Hans Verkuil72246792012-07-31 03:48:31 -0300811 if (mutex_lock_interruptible(&common->lock))
812 return -ERESTARTSYS;
813 ret = vb2_mmap(&common->buffer_queue, vma);
814 mutex_unlock(&common->lock);
815 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300816}
817
818/**
819 * vpif_poll: It is used for select/poll system call
820 * @filep: file pointer
821 * @wait: poll table to wait
822 */
823static unsigned int vpif_poll(struct file *filep, poll_table * wait)
824{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300825 struct vpif_fh *fh = filep->private_data;
826 struct channel_obj *channel = fh->channel;
827 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300828 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300829
830 vpif_dbg(2, debug, "vpif_poll\n");
831
Hans Verkuil72246792012-07-31 03:48:31 -0300832 if (common->started) {
833 mutex_lock(&common->lock);
834 res = vb2_poll(&common->buffer_queue, filep, wait);
835 mutex_unlock(&common->lock);
836 }
837 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300838}
839
840/**
841 * vpif_open : vpif open handler
842 * @filep: file ptr
843 *
844 * It creates object of file handle structure and stores it in private_data
845 * member of filepointer
846 */
847static int vpif_open(struct file *filep)
848{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300849 struct video_device *vdev = video_devdata(filep);
850 struct common_obj *common;
851 struct video_obj *vid_ch;
852 struct channel_obj *ch;
853 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300854
855 vpif_dbg(2, debug, "vpif_open\n");
856
857 ch = video_get_drvdata(vdev);
858
859 vid_ch = &ch->video;
860 common = &ch->common[VPIF_VIDEO_INDEX];
861
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300862 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300863 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300864 if (NULL == fh) {
865 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300866 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300867 }
868
Hans Verkuil72246792012-07-31 03:48:31 -0300869 if (mutex_lock_interruptible(&common->lock)) {
870 kfree(fh);
871 return -ERESTARTSYS;
872 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300873 /* store pointer to fh in private_data member of filep */
874 filep->private_data = fh;
875 fh->channel = ch;
876 fh->initialized = 0;
877 /* If decoder is not initialized. initialize it */
878 if (!ch->initialized) {
879 fh->initialized = 1;
880 ch->initialized = 1;
881 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
882 }
883 /* Increment channel usrs counter */
884 ch->usrs++;
885 /* Set io_allowed member to false */
886 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
887 /* Initialize priority of this instance to default priority */
888 fh->prio = V4L2_PRIORITY_UNSET;
889 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300890 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300891 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300892}
893
894/**
895 * vpif_release : function to clean up file close
896 * @filep: file pointer
897 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300898 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300899 * handle
900 */
901static int vpif_release(struct file *filep)
902{
903 struct vpif_fh *fh = filep->private_data;
904 struct channel_obj *ch = fh->channel;
905 struct common_obj *common;
906
907 vpif_dbg(2, debug, "vpif_release\n");
908
909 common = &ch->common[VPIF_VIDEO_INDEX];
910
Hans Verkuil72246792012-07-31 03:48:31 -0300911 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300912 /* if this instance is doing IO */
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300913 if (fh->io_allowed[VPIF_VIDEO_INDEX])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300914 /* Reset io_usrs member of channel object */
915 common->io_usrs = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300916
917 /* Decrement channel usrs counter */
918 ch->usrs--;
919
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300920 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300921 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300922
923 if (fh->initialized)
924 ch->initialized = 0;
925
Hans Verkuil72246792012-07-31 03:48:31 -0300926 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300927 filep->private_data = NULL;
928 kfree(fh);
929 return 0;
930}
931
932/**
933 * vpif_reqbufs() - request buffer handler
934 * @file: file ptr
935 * @priv: file handle
936 * @reqbuf: request buffer structure ptr
937 */
938static int vpif_reqbufs(struct file *file, void *priv,
939 struct v4l2_requestbuffers *reqbuf)
940{
941 struct vpif_fh *fh = priv;
942 struct channel_obj *ch = fh->channel;
943 struct common_obj *common;
944 u8 index = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300945
946 vpif_dbg(2, debug, "vpif_reqbufs\n");
947
948 /**
949 * This file handle has not initialized the channel,
950 * It is not allowed to do settings
951 */
952 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
953 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
954 if (!fh->initialized) {
955 vpif_dbg(1, debug, "Channel Busy\n");
956 return -EBUSY;
957 }
958 }
959
Manjunath Hadli764af392012-04-13 04:49:34 -0300960 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300961 return -EINVAL;
962
963 index = VPIF_VIDEO_INDEX;
964
965 common = &ch->common[index];
966
Hans Verkuil46656af2011-01-04 06:51:35 -0300967 if (0 != common->io_usrs)
968 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300969
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300970 /* Set io allowed member of file handle to TRUE */
971 fh->io_allowed[index] = 1;
972 /* Increment io usrs member of channel object to 1 */
973 common->io_usrs = 1;
974 /* Store type of memory requested in channel object */
975 common->memory = reqbuf->memory;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300976
977 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300978 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300979}
980
981/**
982 * vpif_querybuf() - query buffer handler
983 * @file: file ptr
984 * @priv: file handle
985 * @buf: v4l2 buffer structure ptr
986 */
987static int vpif_querybuf(struct file *file, void *priv,
988 struct v4l2_buffer *buf)
989{
990 struct vpif_fh *fh = priv;
991 struct channel_obj *ch = fh->channel;
992 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
993
994 vpif_dbg(2, debug, "vpif_querybuf\n");
995
996 if (common->fmt.type != buf->type)
997 return -EINVAL;
998
999 if (common->memory != V4L2_MEMORY_MMAP) {
1000 vpif_dbg(1, debug, "Invalid memory\n");
1001 return -EINVAL;
1002 }
1003
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001004 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001005}
1006
1007/**
1008 * vpif_qbuf() - query buffer handler
1009 * @file: file ptr
1010 * @priv: file handle
1011 * @buf: v4l2 buffer structure ptr
1012 */
1013static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1014{
1015
1016 struct vpif_fh *fh = priv;
1017 struct channel_obj *ch = fh->channel;
1018 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1019 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001020
1021 vpif_dbg(2, debug, "vpif_qbuf\n");
1022
1023 if (common->fmt.type != tbuf.type) {
1024 vpif_err("invalid buffer type\n");
1025 return -EINVAL;
1026 }
1027
1028 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001029 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001030 return -EACCES;
1031 }
1032
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001033 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001034}
1035
1036/**
1037 * vpif_dqbuf() - query buffer handler
1038 * @file: file ptr
1039 * @priv: file handle
1040 * @buf: v4l2 buffer structure ptr
1041 */
1042static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1043{
1044 struct vpif_fh *fh = priv;
1045 struct channel_obj *ch = fh->channel;
1046 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1047
1048 vpif_dbg(2, debug, "vpif_dqbuf\n");
1049
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001050 return vb2_dqbuf(&common->buffer_queue, buf,
1051 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001052}
1053
1054/**
1055 * vpif_streamon() - streamon handler
1056 * @file: file ptr
1057 * @priv: file handle
1058 * @buftype: v4l2 buffer type
1059 */
1060static int vpif_streamon(struct file *file, void *priv,
1061 enum v4l2_buf_type buftype)
1062{
1063
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001064 struct vpif_fh *fh = priv;
1065 struct channel_obj *ch = fh->channel;
1066 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1067 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1068 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001069 int ret = 0;
1070
1071 vpif_dbg(2, debug, "vpif_streamon\n");
1072
1073 vpif = &ch->vpifparams;
1074
1075 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1076 vpif_dbg(1, debug, "buffer type not supported\n");
1077 return -EINVAL;
1078 }
1079
1080 /* If file handle is not allowed IO, return error */
1081 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1082 vpif_dbg(1, debug, "io not allowed\n");
1083 return -EACCES;
1084 }
1085
1086 /* If Streaming is already started, return error */
1087 if (common->started) {
1088 vpif_dbg(1, debug, "channel->started\n");
1089 return -EBUSY;
1090 }
1091
1092 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1093 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1094 vpif->std_info.ycmux_mode == 0) ||
1095 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1096 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1097 vpif_dbg(1, debug, "other channel is being used\n");
1098 return -EBUSY;
1099 }
1100
1101 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1102 if (ret)
1103 return ret;
1104
1105 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001106 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001107
Hans Verkuil178cce12012-09-20 09:06:30 -03001108 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001109 vpif_dbg(1, debug, "stream on failed in subdev\n");
1110 return ret;
1111 }
1112
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001113 /* Call vb2_streamon to start streaming in videobuf2 */
1114 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001115 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001116 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001117 return ret;
1118 }
1119
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001120 return ret;
1121}
1122
1123/**
1124 * vpif_streamoff() - streamoff handler
1125 * @file: file ptr
1126 * @priv: file handle
1127 * @buftype: v4l2 buffer type
1128 */
1129static int vpif_streamoff(struct file *file, void *priv,
1130 enum v4l2_buf_type buftype)
1131{
1132
1133 struct vpif_fh *fh = priv;
1134 struct channel_obj *ch = fh->channel;
1135 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1136 int ret;
1137
1138 vpif_dbg(2, debug, "vpif_streamoff\n");
1139
1140 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1141 vpif_dbg(1, debug, "buffer type not supported\n");
1142 return -EINVAL;
1143 }
1144
1145 /* If io is allowed for this file handle, return error */
1146 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1147 vpif_dbg(1, debug, "io not allowed\n");
1148 return -EACCES;
1149 }
1150
1151 /* If streaming is not started, return error */
1152 if (!common->started) {
1153 vpif_dbg(1, debug, "channel->started\n");
1154 return -EINVAL;
1155 }
1156
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001157 /* disable channel */
1158 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1159 enable_channel0(0);
1160 channel0_intr_enable(0);
1161 } else {
1162 enable_channel1(0);
1163 channel1_intr_enable(0);
1164 }
1165
1166 common->started = 0;
1167
Hans Verkuil178cce12012-09-20 09:06:30 -03001168 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001169
Hans Verkuil178cce12012-09-20 09:06:30 -03001170 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001171 vpif_dbg(1, debug, "stream off failed in subdev\n");
1172
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001173 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001174}
1175
1176/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001177 * vpif_input_to_subdev() - Maps input to sub device
1178 * @vpif_cfg - global config ptr
1179 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001180 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001181 *
1182 * lookup the sub device information for a given input index.
1183 * we report all the inputs to application. inputs table also
1184 * has sub device name for the each input
1185 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001186static int vpif_input_to_subdev(
1187 struct vpif_capture_config *vpif_cfg,
1188 struct vpif_capture_chan_config *chan_cfg,
1189 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001190{
Hans Verkuil178cce12012-09-20 09:06:30 -03001191 struct vpif_subdev_info *subdev_info;
1192 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001193 int i;
1194
Hans Verkuil178cce12012-09-20 09:06:30 -03001195 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001196
Hans Verkuil178cce12012-09-20 09:06:30 -03001197 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1198 if (subdev_name == NULL)
1199 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001200
1201 /* loop through the sub device list to get the sub device info */
1202 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1203 subdev_info = &vpif_cfg->subdev_info[i];
1204 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001205 return i;
1206 }
1207 return -1;
1208}
1209
1210/**
1211 * vpif_set_input() - Select an input
1212 * @vpif_cfg - global config ptr
1213 * @ch - channel
1214 * @_index - Given input index from application
1215 *
1216 * Select the given input.
1217 */
1218static int vpif_set_input(
1219 struct vpif_capture_config *vpif_cfg,
1220 struct channel_obj *ch,
1221 int index)
1222{
1223 struct vpif_capture_chan_config *chan_cfg =
1224 &vpif_cfg->chan_config[ch->channel_id];
1225 struct vpif_subdev_info *subdev_info = NULL;
1226 struct v4l2_subdev *sd = NULL;
1227 u32 input = 0, output = 0;
1228 int sd_index;
1229 int ret;
1230
1231 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1232 if (sd_index >= 0) {
1233 sd = vpif_obj.sd[sd_index];
1234 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001235 }
1236
Hans Verkuil178cce12012-09-20 09:06:30 -03001237 /* first setup input path from sub device to vpif */
1238 if (sd && vpif_cfg->setup_input_path) {
1239 ret = vpif_cfg->setup_input_path(ch->channel_id,
1240 subdev_info->name);
1241 if (ret < 0) {
1242 vpif_dbg(1, debug, "couldn't setup input path for the" \
1243 " sub device %s, for input index %d\n",
1244 subdev_info->name, index);
1245 return ret;
1246 }
1247 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001248
Hans Verkuil178cce12012-09-20 09:06:30 -03001249 if (sd) {
1250 input = chan_cfg->inputs[index].input_route;
1251 output = chan_cfg->inputs[index].output_route;
1252 ret = v4l2_subdev_call(sd, video, s_routing,
1253 input, output, 0);
1254 if (ret < 0 && ret != -ENOIOCTLCMD) {
1255 vpif_dbg(1, debug, "Failed to set input\n");
1256 return ret;
1257 }
1258 }
1259 ch->input_idx = index;
1260 ch->sd = sd;
1261 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001262 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001263
Hans Verkuil178cce12012-09-20 09:06:30 -03001264 /* update tvnorms from the sub device input info */
1265 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1266 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001267}
1268
1269/**
1270 * vpif_querystd() - querystd handler
1271 * @file: file ptr
1272 * @priv: file handle
1273 * @std_id: ptr to std id
1274 *
1275 * This function is called to detect standard at the selected input
1276 */
1277static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1278{
1279 struct vpif_fh *fh = priv;
1280 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001281 int ret = 0;
1282
1283 vpif_dbg(2, debug, "vpif_querystd\n");
1284
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001285 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001286 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001287
Hans Verkuil178cce12012-09-20 09:06:30 -03001288 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1289 return -ENODATA;
1290 if (ret) {
1291 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1292 return ret;
1293 }
1294
1295 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001296}
1297
1298/**
1299 * vpif_g_std() - get STD handler
1300 * @file: file ptr
1301 * @priv: file handle
1302 * @std_id: ptr to std id
1303 */
1304static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1305{
1306 struct vpif_fh *fh = priv;
1307 struct channel_obj *ch = fh->channel;
1308
1309 vpif_dbg(2, debug, "vpif_g_std\n");
1310
1311 *std = ch->video.stdid;
1312 return 0;
1313}
1314
1315/**
1316 * vpif_s_std() - set STD handler
1317 * @file: file ptr
1318 * @priv: file handle
1319 * @std_id: ptr to std id
1320 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001321static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001322{
1323 struct vpif_fh *fh = priv;
1324 struct channel_obj *ch = fh->channel;
1325 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1326 int ret = 0;
1327
1328 vpif_dbg(2, debug, "vpif_s_std\n");
1329
1330 if (common->started) {
1331 vpif_err("streaming in progress\n");
1332 return -EBUSY;
1333 }
1334
1335 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1336 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1337 if (!fh->initialized) {
1338 vpif_dbg(1, debug, "Channel Busy\n");
1339 return -EBUSY;
1340 }
1341 }
1342
Hans Verkuilffb48772010-05-01 08:03:24 -03001343 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001344 if (0 != ret)
1345 return ret;
1346
1347 fh->initialized = 1;
1348
1349 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001350 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001351 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001352
1353 /* Get the information about the standard */
1354 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001355 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001356 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001357 }
1358
1359 /* Configure the default format information */
1360 vpif_config_format(ch);
1361
1362 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001363 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001364 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001365 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001366 return ret;
1367 }
1368 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001369}
1370
1371/**
1372 * vpif_enum_input() - ENUMINPUT handler
1373 * @file: file ptr
1374 * @priv: file handle
1375 * @input: ptr to input structure
1376 */
1377static int vpif_enum_input(struct file *file, void *priv,
1378 struct v4l2_input *input)
1379{
1380
1381 struct vpif_capture_config *config = vpif_dev->platform_data;
1382 struct vpif_capture_chan_config *chan_cfg;
1383 struct vpif_fh *fh = priv;
1384 struct channel_obj *ch = fh->channel;
1385
1386 chan_cfg = &config->chan_config[ch->channel_id];
1387
1388 if (input->index >= chan_cfg->input_count) {
1389 vpif_dbg(1, debug, "Invalid input index\n");
1390 return -EINVAL;
1391 }
1392
1393 memcpy(input, &chan_cfg->inputs[input->index].input,
1394 sizeof(*input));
1395 return 0;
1396}
1397
1398/**
1399 * vpif_g_input() - Get INPUT handler
1400 * @file: file ptr
1401 * @priv: file handle
1402 * @index: ptr to input index
1403 */
1404static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1405{
1406 struct vpif_fh *fh = priv;
1407 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001408
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001409 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001410 return 0;
1411}
1412
1413/**
1414 * vpif_s_input() - Set INPUT handler
1415 * @file: file ptr
1416 * @priv: file handle
1417 * @index: input index
1418 */
1419static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1420{
1421 struct vpif_capture_config *config = vpif_dev->platform_data;
1422 struct vpif_capture_chan_config *chan_cfg;
1423 struct vpif_fh *fh = priv;
1424 struct channel_obj *ch = fh->channel;
1425 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001426 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001427
1428 chan_cfg = &config->chan_config[ch->channel_id];
1429
Hans Verkuil7aaad132012-09-20 09:06:25 -03001430 if (index >= chan_cfg->input_count)
1431 return -EINVAL;
1432
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001433 if (common->started) {
1434 vpif_err("Streaming in progress\n");
1435 return -EBUSY;
1436 }
1437
1438 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1439 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1440 if (!fh->initialized) {
1441 vpif_dbg(1, debug, "Channel Busy\n");
1442 return -EBUSY;
1443 }
1444 }
1445
Hans Verkuilffb48772010-05-01 08:03:24 -03001446 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001447 if (0 != ret)
1448 return ret;
1449
1450 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001451 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001452}
1453
1454/**
1455 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1456 * @file: file ptr
1457 * @priv: file handle
1458 * @index: input index
1459 */
1460static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1461 struct v4l2_fmtdesc *fmt)
1462{
1463 struct vpif_fh *fh = priv;
1464 struct channel_obj *ch = fh->channel;
1465
1466 if (fmt->index != 0) {
1467 vpif_dbg(1, debug, "Invalid format index\n");
1468 return -EINVAL;
1469 }
1470
1471 /* Fill in the information about format */
1472 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1473 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1474 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1475 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1476 } else {
1477 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1478 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1479 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1480 }
1481 return 0;
1482}
1483
1484/**
1485 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1486 * @file: file ptr
1487 * @priv: file handle
1488 * @fmt: ptr to v4l2 format structure
1489 */
1490static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1491 struct v4l2_format *fmt)
1492{
1493 struct vpif_fh *fh = priv;
1494 struct channel_obj *ch = fh->channel;
1495 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1496
1497 return vpif_check_format(ch, pixfmt, 1);
1498}
1499
1500
1501/**
1502 * vpif_g_fmt_vid_cap() - Set INPUT handler
1503 * @file: file ptr
1504 * @priv: file handle
1505 * @fmt: ptr to v4l2 format structure
1506 */
1507static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1508 struct v4l2_format *fmt)
1509{
1510 struct vpif_fh *fh = priv;
1511 struct channel_obj *ch = fh->channel;
1512 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1513
1514 /* Check the validity of the buffer type */
1515 if (common->fmt.type != fmt->type)
1516 return -EINVAL;
1517
1518 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001519 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001520 return 0;
1521}
1522
1523/**
1524 * vpif_s_fmt_vid_cap() - Set FMT handler
1525 * @file: file ptr
1526 * @priv: file handle
1527 * @fmt: ptr to v4l2 format structure
1528 */
1529static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1530 struct v4l2_format *fmt)
1531{
1532 struct vpif_fh *fh = priv;
1533 struct channel_obj *ch = fh->channel;
1534 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1535 struct v4l2_pix_format *pixfmt;
1536 int ret = 0;
1537
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001538 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001539
1540 /* If streaming is started, return error */
1541 if (common->started) {
1542 vpif_dbg(1, debug, "Streaming is started\n");
1543 return -EBUSY;
1544 }
1545
1546 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1547 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1548 if (!fh->initialized) {
1549 vpif_dbg(1, debug, "Channel Busy\n");
1550 return -EBUSY;
1551 }
1552 }
1553
Hans Verkuilffb48772010-05-01 08:03:24 -03001554 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001555 if (0 != ret)
1556 return ret;
1557
1558 fh->initialized = 1;
1559
1560 pixfmt = &fmt->fmt.pix;
1561 /* Check for valid field format */
1562 ret = vpif_check_format(ch, pixfmt, 0);
1563
1564 if (ret)
1565 return ret;
1566 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001567 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001568 return 0;
1569}
1570
1571/**
1572 * vpif_querycap() - QUERYCAP handler
1573 * @file: file ptr
1574 * @priv: file handle
1575 * @cap: ptr to v4l2_capability structure
1576 */
1577static int vpif_querycap(struct file *file, void *priv,
1578 struct v4l2_capability *cap)
1579{
1580 struct vpif_capture_config *config = vpif_dev->platform_data;
1581
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001582 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1583 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1584 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1585 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1586 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001587 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1588
1589 return 0;
1590}
1591
1592/**
1593 * vpif_g_priority() - get priority handler
1594 * @file: file ptr
1595 * @priv: file handle
1596 * @prio: ptr to v4l2_priority structure
1597 */
1598static int vpif_g_priority(struct file *file, void *priv,
1599 enum v4l2_priority *prio)
1600{
1601 struct vpif_fh *fh = priv;
1602 struct channel_obj *ch = fh->channel;
1603
1604 *prio = v4l2_prio_max(&ch->prio);
1605
1606 return 0;
1607}
1608
1609/**
1610 * vpif_s_priority() - set priority handler
1611 * @file: file ptr
1612 * @priv: file handle
1613 * @prio: ptr to v4l2_priority structure
1614 */
1615static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1616{
1617 struct vpif_fh *fh = priv;
1618 struct channel_obj *ch = fh->channel;
1619
1620 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1621}
1622
1623/**
1624 * vpif_cropcap() - cropcap handler
1625 * @file: file ptr
1626 * @priv: file handle
1627 * @crop: ptr to v4l2_cropcap structure
1628 */
1629static int vpif_cropcap(struct file *file, void *priv,
1630 struct v4l2_cropcap *crop)
1631{
1632 struct vpif_fh *fh = priv;
1633 struct channel_obj *ch = fh->channel;
1634 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1635
1636 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1637 return -EINVAL;
1638
1639 crop->bounds.left = 0;
1640 crop->bounds.top = 0;
1641 crop->bounds.height = common->height;
1642 crop->bounds.width = common->width;
1643 crop->defrect = crop->bounds;
1644 return 0;
1645}
1646
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001647/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001648 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001649 * @file: file ptr
1650 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001651 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001652 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001653static int
1654vpif_enum_dv_timings(struct file *file, void *priv,
1655 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001656{
1657 struct vpif_fh *fh = priv;
1658 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001659 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001660
Hans Verkuil178cce12012-09-20 09:06:30 -03001661 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001662 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001663 return -EINVAL;
1664 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001665}
1666
1667/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001668 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001669 * @file: file ptr
1670 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001671 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001672 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001673static int
1674vpif_query_dv_timings(struct file *file, void *priv,
1675 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001676{
1677 struct vpif_fh *fh = priv;
1678 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001679 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001680
Hans Verkuil178cce12012-09-20 09:06:30 -03001681 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001682 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001683 return -ENODATA;
1684 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001685}
1686
Mats Randgaardc027e162010-12-16 12:17:44 -03001687/**
1688 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1689 * @file: file ptr
1690 * @priv: file handle
1691 * @timings: digital video timings
1692 */
1693static int vpif_s_dv_timings(struct file *file, void *priv,
1694 struct v4l2_dv_timings *timings)
1695{
1696 struct vpif_fh *fh = priv;
1697 struct channel_obj *ch = fh->channel;
1698 struct vpif_params *vpifparams = &ch->vpifparams;
1699 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1700 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001701 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001702 int ret;
1703
1704 if (timings->type != V4L2_DV_BT_656_1120) {
1705 vpif_dbg(2, debug, "Timing type not defined\n");
1706 return -EINVAL;
1707 }
1708
1709 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001710 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1711 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1712 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001713 if (ret < 0) {
1714 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1715 return ret;
1716 }
1717
1718 if (!(timings->bt.width && timings->bt.height &&
1719 (timings->bt.hbackporch ||
1720 timings->bt.hfrontporch ||
1721 timings->bt.hsync) &&
1722 timings->bt.vfrontporch &&
1723 (timings->bt.vbackporch ||
1724 timings->bt.vsync))) {
1725 vpif_dbg(2, debug, "Timings for width, height, "
1726 "horizontal back porch, horizontal sync, "
1727 "horizontal front porch, vertical back porch, "
1728 "vertical sync and vertical back porch "
1729 "must be defined\n");
1730 return -EINVAL;
1731 }
1732
Hans Verkuil0598c172012-09-18 07:18:47 -03001733 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001734
1735 /* Configure video port timings */
1736
Hans Verkuile3655262013-07-29 08:41:00 -03001737 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001738 std_info->sav2eav = bt->width;
1739
1740 std_info->l1 = 1;
1741 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1742
Hans Verkuile3655262013-07-29 08:41:00 -03001743 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001744 if (bt->interlaced) {
1745 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001746 std_info->l5 = std_info->vsize/2 -
1747 (bt->vfrontporch - 1);
1748 std_info->l7 = std_info->vsize/2 + 1;
1749 std_info->l9 = std_info->l7 + bt->il_vsync +
1750 bt->il_vbackporch + 1;
1751 std_info->l11 = std_info->vsize -
1752 (bt->il_vfrontporch - 1);
1753 } else {
1754 vpif_dbg(2, debug, "Required timing values for "
1755 "interlaced BT format missing\n");
1756 return -EINVAL;
1757 }
1758 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001759 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1760 }
1761 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1762 std_info->width = bt->width;
1763 std_info->height = bt->height;
1764 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1765 std_info->ycmux_mode = 0;
1766 std_info->capture_format = 0;
1767 std_info->vbi_supported = 0;
1768 std_info->hd_sd = 1;
1769 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001770
1771 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001772 return 0;
1773}
1774
1775/**
1776 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1777 * @file: file ptr
1778 * @priv: file handle
1779 * @timings: digital video timings
1780 */
1781static int vpif_g_dv_timings(struct file *file, void *priv,
1782 struct v4l2_dv_timings *timings)
1783{
1784 struct vpif_fh *fh = priv;
1785 struct channel_obj *ch = fh->channel;
1786 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001787
Hans Verkuil0598c172012-09-18 07:18:47 -03001788 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001789
1790 return 0;
1791}
1792
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001793/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001794 * vpif_log_status() - Status information
1795 * @file: file ptr
1796 * @priv: file handle
1797 *
1798 * Returns zero.
1799 */
1800static int vpif_log_status(struct file *filep, void *priv)
1801{
1802 /* status for sub devices */
1803 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1804
1805 return 0;
1806}
1807
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001808/* vpif capture ioctl operations */
1809static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1810 .vidioc_querycap = vpif_querycap,
1811 .vidioc_g_priority = vpif_g_priority,
1812 .vidioc_s_priority = vpif_s_priority,
1813 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1814 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1815 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1816 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1817 .vidioc_enum_input = vpif_enum_input,
1818 .vidioc_s_input = vpif_s_input,
1819 .vidioc_g_input = vpif_g_input,
1820 .vidioc_reqbufs = vpif_reqbufs,
1821 .vidioc_querybuf = vpif_querybuf,
1822 .vidioc_querystd = vpif_querystd,
1823 .vidioc_s_std = vpif_s_std,
1824 .vidioc_g_std = vpif_g_std,
1825 .vidioc_qbuf = vpif_qbuf,
1826 .vidioc_dqbuf = vpif_dqbuf,
1827 .vidioc_streamon = vpif_streamon,
1828 .vidioc_streamoff = vpif_streamoff,
1829 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001830 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1831 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001832 .vidioc_s_dv_timings = vpif_s_dv_timings,
1833 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001834 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001835};
1836
1837/* vpif file operations */
1838static struct v4l2_file_operations vpif_fops = {
1839 .owner = THIS_MODULE,
1840 .open = vpif_open,
1841 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001842 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001843 .mmap = vpif_mmap,
1844 .poll = vpif_poll
1845};
1846
1847/* vpif video template */
1848static struct video_device vpif_video_template = {
1849 .name = "vpif",
1850 .fops = &vpif_fops,
1851 .minor = -1,
1852 .ioctl_ops = &vpif_ioctl_ops,
1853};
1854
1855/**
1856 * initialize_vpif() - Initialize vpif data structures
1857 *
1858 * Allocate memory for data structures and initialize them
1859 */
1860static int initialize_vpif(void)
1861{
1862 int err = 0, i, j;
1863 int free_channel_objects_index;
1864
1865 /* Default number of buffers should be 3 */
1866 if ((ch0_numbuffers > 0) &&
1867 (ch0_numbuffers < config_params.min_numbuffers))
1868 ch0_numbuffers = config_params.min_numbuffers;
1869 if ((ch1_numbuffers > 0) &&
1870 (ch1_numbuffers < config_params.min_numbuffers))
1871 ch1_numbuffers = config_params.min_numbuffers;
1872
1873 /* Set buffer size to min buffers size if it is invalid */
1874 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1875 ch0_bufsize =
1876 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1877 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1878 ch1_bufsize =
1879 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1880
1881 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1882 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1883 if (ch0_numbuffers) {
1884 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1885 = ch0_bufsize;
1886 }
1887 if (ch1_numbuffers) {
1888 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1889 = ch1_bufsize;
1890 }
1891
1892 /* Allocate memory for six channel objects */
1893 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1894 vpif_obj.dev[i] =
1895 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1896 /* If memory allocation fails, return error */
1897 if (!vpif_obj.dev[i]) {
1898 free_channel_objects_index = i;
1899 err = -ENOMEM;
1900 goto vpif_init_free_channel_objects;
1901 }
1902 }
1903 return 0;
1904
1905vpif_init_free_channel_objects:
1906 for (j = 0; j < free_channel_objects_index; j++)
1907 kfree(vpif_obj.dev[j]);
1908 return err;
1909}
1910
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001911static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1912 struct v4l2_subdev *subdev,
1913 struct v4l2_async_subdev *asd)
1914{
1915 int i;
1916
1917 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1918 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1919 subdev->name)) {
1920 vpif_obj.sd[i] = subdev;
1921 return 0;
1922 }
1923
1924 return -EINVAL;
1925}
1926
1927static int vpif_probe_complete(void)
1928{
1929 struct common_obj *common;
1930 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001931 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001932 int i, j, err, k;
1933
1934 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1935 ch = vpif_obj.dev[j];
1936 ch->channel_id = j;
1937 common = &(ch->common[VPIF_VIDEO_INDEX]);
1938 spin_lock_init(&common->irqlock);
1939 mutex_init(&common->lock);
1940 ch->video_dev->lock = &common->lock;
1941 /* Initialize prio member of channel object */
1942 v4l2_prio_init(&ch->prio);
1943 video_set_drvdata(ch->video_dev, ch);
1944
1945 /* select input 0 */
1946 err = vpif_set_input(vpif_obj.config, ch, 0);
1947 if (err)
1948 goto probe_out;
1949
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001950 /* Initialize vb2 queue */
1951 q = &common->buffer_queue;
1952 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1953 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1954 q->drv_priv = ch;
1955 q->ops = &video_qops;
1956 q->mem_ops = &vb2_dma_contig_memops;
1957 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1958 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1959 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001960 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001961
1962 err = vb2_queue_init(q);
1963 if (err) {
1964 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1965 goto probe_out;
1966 }
1967
1968 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1969 if (IS_ERR(common->alloc_ctx)) {
1970 vpif_err("Failed to get the context\n");
1971 err = PTR_ERR(common->alloc_ctx);
1972 goto probe_out;
1973 }
1974
1975 INIT_LIST_HEAD(&common->dma_queue);
1976
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001977 err = video_register_device(ch->video_dev,
1978 VFL_TYPE_GRABBER, (j ? 1 : 0));
1979 if (err)
1980 goto probe_out;
1981 }
1982
1983 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1984 return 0;
1985
1986probe_out:
1987 for (k = 0; k < j; k++) {
1988 /* Get the pointer to the channel object */
1989 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001990 common = &ch->common[k];
1991 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001992 /* Unregister video device */
1993 video_unregister_device(ch->video_dev);
1994 }
1995 kfree(vpif_obj.sd);
1996 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1997 ch = vpif_obj.dev[i];
1998 /* Note: does nothing if ch->video_dev == NULL */
1999 video_device_release(ch->video_dev);
2000 }
2001 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2002
2003 return err;
2004}
2005
2006static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2007{
2008 return vpif_probe_complete();
2009}
2010
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002011/**
2012 * vpif_probe : This function probes the vpif capture driver
2013 * @pdev: platform device pointer
2014 *
2015 * This creates device entries by register itself to the V4L2 driver and
2016 * initializes fields of each channel objects
2017 */
2018static __init int vpif_probe(struct platform_device *pdev)
2019{
2020 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002021 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002022 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002023 struct i2c_adapter *i2c_adap;
2024 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002025 struct video_device *vfd;
2026 struct resource *res;
2027 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03002028 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002029
2030 vpif_dev = &pdev->dev;
2031
2032 err = initialize_vpif();
2033 if (err) {
2034 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2035 return err;
2036 }
2037
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002038 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2039 if (err) {
2040 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2041 return err;
2042 }
2043
Hans Verkuil5be452c2012-09-20 09:06:29 -03002044 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03002045 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2046 IRQF_SHARED, "VPIF_Capture",
2047 (void *)(&vpif_obj.dev[res_idx]->
2048 channel_id));
2049 if (err) {
2050 err = -EINVAL;
2051 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002052 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002053 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002054 }
2055
2056 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2057 /* Get the pointer to the channel object */
2058 ch = vpif_obj.dev[i];
2059 /* Allocate memory for video device */
2060 vfd = video_device_alloc();
2061 if (NULL == vfd) {
2062 for (j = 0; j < i; j++) {
2063 ch = vpif_obj.dev[j];
2064 video_device_release(ch->video_dev);
2065 }
2066 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002067 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002068 }
2069
2070 /* Initialize field of video device */
2071 *vfd = vpif_video_template;
2072 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2073 vfd->release = video_device_release;
2074 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002075 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002076 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002077 /* Set video_dev to the video device */
2078 ch->video_dev = vfd;
2079 }
2080
Manjunath Hadli764af392012-04-13 04:49:34 -03002081 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2082 if (res) {
2083 size = resource_size(res);
2084 /* The resources are divided into two equal memory and when we
2085 * have HD output we can add them together
2086 */
2087 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2088 ch = vpif_obj.dev[j];
2089 ch->channel_id = j;
2090 /* only enabled if second resource exists */
2091 config_params.video_limit[ch->channel_id] = 0;
2092 if (size)
2093 config_params.video_limit[ch->channel_id] =
2094 size/2;
2095 }
2096 }
2097
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002098 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002099
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002100 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002101 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002102 GFP_KERNEL);
2103 if (vpif_obj.sd == NULL) {
2104 vpif_err("unable to allocate memory for subdevice pointers\n");
2105 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002106 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002107 }
2108
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002109 if (!vpif_obj.config->asd_sizes) {
2110 i2c_adap = i2c_get_adapter(1);
2111 for (i = 0; i < subdev_count; i++) {
2112 subdevdata = &vpif_obj.config->subdev_info[i];
2113 vpif_obj.sd[i] =
2114 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2115 i2c_adap,
2116 &subdevdata->
2117 board_info,
2118 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002119
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002120 if (!vpif_obj.sd[i]) {
2121 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03002122 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002123 goto probe_subdev_out;
2124 }
2125 v4l2_info(&vpif_obj.v4l2_dev,
2126 "registered sub device %s\n",
2127 subdevdata->name);
2128 }
2129 vpif_probe_complete();
2130 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03002131 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002132 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2133 vpif_obj.notifier.bound = vpif_async_bound;
2134 vpif_obj.notifier.complete = vpif_async_complete;
2135 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2136 &vpif_obj.notifier);
2137 if (err) {
2138 vpif_err("Error registering async notifier\n");
2139 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002140 goto probe_subdev_out;
2141 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002142 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002143
2144 return 0;
2145
Hans Verkuilb65814e2012-09-20 09:06:26 -03002146probe_subdev_out:
2147 /* free sub devices memory */
2148 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002149
Hans Verkuil5be452c2012-09-20 09:06:29 -03002150vpif_sd_error:
2151 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2152 ch = vpif_obj.dev[i];
2153 /* Note: does nothing if ch->video_dev == NULL */
2154 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002155 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002156vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002157 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002158
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002159 return err;
2160}
2161
2162/**
2163 * vpif_remove() - driver remove handler
2164 * @device: ptr to platform device structure
2165 *
2166 * The vidoe device is unregistered
2167 */
2168static int vpif_remove(struct platform_device *device)
2169{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002170 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002171 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002172 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002173
2174 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2175
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002176 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002177 /* un-register device */
2178 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2179 /* Get the pointer to the channel object */
2180 ch = vpif_obj.dev[i];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002181 common = &ch->common[i];
2182 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002183 /* Unregister video device */
2184 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002185 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002186 }
2187 return 0;
2188}
2189
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002190#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002191/**
2192 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002193 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002194static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002195{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002196
2197 struct common_obj *common;
2198 struct channel_obj *ch;
2199 int i;
2200
2201 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2202 /* Get the pointer to the channel object */
2203 ch = vpif_obj.dev[i];
2204 common = &ch->common[VPIF_VIDEO_INDEX];
2205 mutex_lock(&common->lock);
2206 if (ch->usrs && common->io_usrs) {
2207 /* Disable channel */
2208 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2209 enable_channel0(0);
2210 channel0_intr_enable(0);
2211 }
2212 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2213 common->started == 2) {
2214 enable_channel1(0);
2215 channel1_intr_enable(0);
2216 }
2217 }
2218 mutex_unlock(&common->lock);
2219 }
2220
2221 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002222}
2223
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002224/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002225 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002226 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002227static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002228{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002229 struct common_obj *common;
2230 struct channel_obj *ch;
2231 int i;
2232
2233 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2234 /* Get the pointer to the channel object */
2235 ch = vpif_obj.dev[i];
2236 common = &ch->common[VPIF_VIDEO_INDEX];
2237 mutex_lock(&common->lock);
2238 if (ch->usrs && common->io_usrs) {
2239 /* Disable channel */
2240 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2241 enable_channel0(1);
2242 channel0_intr_enable(1);
2243 }
2244 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2245 common->started == 2) {
2246 enable_channel1(1);
2247 channel1_intr_enable(1);
2248 }
2249 }
2250 mutex_unlock(&common->lock);
2251 }
2252
2253 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002254}
2255
Alexey Dobriyan47145212009-12-14 18:00:08 -08002256static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002257 .suspend = vpif_suspend,
2258 .resume = vpif_resume,
2259};
2260
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002261#define vpif_pm_ops (&vpif_dev_pm_ops)
2262#else
2263#define vpif_pm_ops NULL
2264#endif
2265
Mats Randgaardffa1b392010-08-30 10:30:36 -03002266static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002267 .driver = {
2268 .name = "vpif_capture",
2269 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002270 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002271 },
2272 .probe = vpif_probe,
2273 .remove = vpif_remove,
2274};
2275
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03002276module_platform_driver(vpif_driver);