blob: 025eb2410cde24663bd7536bab00e74d092b7ee1 [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/**
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030082 * vpif_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, Prabhakar23e76a22014-05-16 10:33:37 -0300100 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
101 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
102 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300103
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300104 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
105
106 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
107 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
108 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
109 !IS_ALIGNED((addr + common->ctop_off), 8) ||
110 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
111 vpif_dbg(1, debug, "offset is not aligned\n");
112 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300113 }
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300114
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300115 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300116}
117
118/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300119 * vpif_buffer_queue_setup : Callback function for buffer setup.
120 * @vq: vb2_queue ptr
121 * @fmt: v4l2 format
122 * @nbuffers: ptr to number of buffers requested by application
123 * @nplanes:: contains number of distinct video planes needed to hold a frame
124 * @sizes[]: contains the size (in bytes) of each plane.
125 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300126 *
127 * This callback function is called when reqbuf() is called to adjust
128 * the buffer count and buffer size
129 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300130static int vpif_buffer_queue_setup(struct vb2_queue *vq,
131 const struct v4l2_format *fmt,
132 unsigned int *nbuffers, unsigned int *nplanes,
133 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300134{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300135 struct channel_obj *ch = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300136 struct common_obj *common;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300137 unsigned long size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300138
139 common = &ch->common[VPIF_VIDEO_INDEX];
140
141 vpif_dbg(2, debug, "vpif_buffer_setup\n");
142
143 /* If memory type is not mmap, return */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300144 if (V4L2_MEMORY_MMAP == common->memory) {
145 /* Calculate the size of the buffer */
146 size = config_params.channel_bufsize[ch->channel_id];
147 /*
148 * Checking if the buffer size exceeds the available buffer
149 * ycmux_mode = 0 means 1 channel mode HD and
150 * ycmux_mode = 1 means 2 channels mode SD
151 */
152 if (ch->vpifparams.std_info.ycmux_mode == 0) {
153 if (config_params.video_limit[ch->channel_id])
154 while (size * *nbuffers >
155 (config_params.video_limit[0]
Manjunath Hadli764af392012-04-13 04:49:34 -0300156 + config_params.video_limit[1]))
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300157 (*nbuffers)--;
158 } else {
159 if (config_params.video_limit[ch->channel_id])
160 while (size * *nbuffers >
Manjunath Hadli764af392012-04-13 04:49:34 -0300161 config_params.video_limit[ch->channel_id])
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300162 (*nbuffers)--;
163 }
164
165 } else {
166 size = common->fmt.fmt.pix.sizeimage;
Manjunath Hadli764af392012-04-13 04:49:34 -0300167 }
168
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300169 if (*nbuffers < config_params.min_numbuffers)
170 *nbuffers = config_params.min_numbuffers;
171
172 *nplanes = 1;
173 sizes[0] = size;
174 alloc_ctxs[0] = common->alloc_ctx;
175
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300176 return 0;
177}
178
179/**
180 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300181 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300182 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300183static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300184{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300185 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
186 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300187 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300188 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300189
190 common = &ch->common[VPIF_VIDEO_INDEX];
191
192 vpif_dbg(2, debug, "vpif_buffer_queue\n");
193
Hans Verkuilaec96832012-11-16 12:03:06 -0300194 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300195 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300196 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300197 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300198}
199
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300200static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
201{
202 struct vpif_capture_config *vpif_config_data =
203 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300204 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300205 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
206 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300207 struct vpif_cap_buffer *buf, *tmp;
208 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300209 int ret;
210
Hans Verkuilaec96832012-11-16 12:03:06 -0300211 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300212
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300213 /* Initialize field_id and started member */
214 ch->field_id = 0;
215 common->started = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300216
217 /* Calculate the offset for Y and C data in the buffer */
218 vpif_calculate_offsets(ch);
219
220 if ((vpif->std_info.frm_fmt &&
221 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
222 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
223 (!vpif->std_info.frm_fmt &&
224 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
225 vpif_dbg(1, debug, "conflict in field format and std format\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300226 ret = -EINVAL;
227 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300228 }
229
230 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300231 if (vpif_config_data->setup_input_channel_mode) {
232 ret = vpif_config_data->
233 setup_input_channel_mode(vpif->std_info.ycmux_mode);
234 if (ret < 0) {
235 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300236 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300237 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300238 }
239
240 /* Call vpif_set_params function to set the parameters and addresses */
241 ret = vpif_set_video_params(vpif, ch->channel_id);
242
243 if (ret < 0) {
244 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300245 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300246 }
247
248 common->started = ret;
249 vpif_config_addr(ch, ret);
250
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300251 /* Get the next frame from the buffer queue */
252 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
253 struct vpif_cap_buffer, list);
254 /* Remove buffer from the buffer queue */
255 list_del(&common->cur_frm->list);
256 spin_unlock_irqrestore(&common->irqlock, flags);
257 /* Mark state of the current frame to active */
258 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
259
260 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
261
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300262 common->set_addr(addr + common->ytop_off,
263 addr + common->ybtm_off,
264 addr + common->ctop_off,
265 addr + common->cbtm_off);
266
267 /**
268 * Set interrupt for both the fields in VPIF Register enable channel in
269 * VPIF register
270 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300271 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300272 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
273 channel0_intr_assert();
274 channel0_intr_enable(1);
275 enable_channel0(1);
276 }
277 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
278 (common->started == 2)) {
279 channel1_intr_assert();
280 channel1_intr_enable(1);
281 enable_channel1(1);
282 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300283
284 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300285
286err:
287 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
288 list_del(&buf->list);
289 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
290 }
291
292 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300293}
294
295/* abort streaming and wait for last buffer */
Hans Verkuile37559b2014-04-17 02:47:21 -0300296static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300297{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300298 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300299 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300300 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300301
302 if (!vb2_is_streaming(vq))
Hans Verkuile37559b2014-04-17 02:47:21 -0300303 return;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300304
305 common = &ch->common[VPIF_VIDEO_INDEX];
306
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300307 /* Disable channel as per its device type and channel id */
308 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
309 enable_channel0(0);
310 channel0_intr_enable(0);
311 }
312 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
313 (2 == common->started)) {
314 enable_channel1(0);
315 channel1_intr_enable(0);
316 }
317 common->started = 0;
318
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300319 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300320 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300321 if (common->cur_frm == common->next_frm) {
322 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
323 } else {
324 if (common->cur_frm != NULL)
325 vb2_buffer_done(&common->cur_frm->vb,
326 VB2_BUF_STATE_ERROR);
327 if (common->next_frm != NULL)
328 vb2_buffer_done(&common->next_frm->vb,
329 VB2_BUF_STATE_ERROR);
330 }
331
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300332 while (!list_empty(&common->dma_queue)) {
333 common->next_frm = list_entry(common->dma_queue.next,
334 struct vpif_cap_buffer, list);
335 list_del(&common->next_frm->list);
336 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
337 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300338 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300339}
340
341static struct vb2_ops video_qops = {
342 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300343 .buf_prepare = vpif_buffer_prepare,
344 .start_streaming = vpif_start_streaming,
345 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300346 .buf_queue = vpif_buffer_queue,
347};
348
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300349/**
350 * vpif_process_buffer_complete: process a completed buffer
351 * @common: ptr to common channel object
352 *
353 * This function time stamp the buffer and mark it as DONE. It also
354 * wake up any process waiting on the QUEUE and set the next buffer
355 * as current
356 */
357static void vpif_process_buffer_complete(struct common_obj *common)
358{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300359 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300360 vb2_buffer_done(&common->cur_frm->vb,
361 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300362 /* Make curFrm pointing to nextFrm */
363 common->cur_frm = common->next_frm;
364}
365
366/**
367 * vpif_schedule_next_buffer: set next buffer address for capture
368 * @common : ptr to common channel object
369 *
370 * This function will get next buffer from the dma queue and
371 * set the buffer address in the vpif register for capture.
372 * the buffer is marked active
373 */
374static void vpif_schedule_next_buffer(struct common_obj *common)
375{
376 unsigned long addr = 0;
377
Hans Verkuilaec96832012-11-16 12:03:06 -0300378 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300379 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300380 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300381 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300382 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300383 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300384 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
385 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300386
387 /* Set top and bottom field addresses in VPIF registers */
388 common->set_addr(addr + common->ytop_off,
389 addr + common->ybtm_off,
390 addr + common->ctop_off,
391 addr + common->cbtm_off);
392}
393
394/**
395 * vpif_channel_isr : ISR handler for vpif capture
396 * @irq: irq number
397 * @dev_id: dev_id ptr
398 *
399 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300400 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300401 */
402static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
403{
404 struct vpif_device *dev = &vpif_obj;
405 struct common_obj *common;
406 struct channel_obj *ch;
407 enum v4l2_field field;
408 int channel_id = 0;
409 int fid = -1, i;
410
411 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300412 if (!vpif_intr_status(channel_id))
413 return IRQ_NONE;
414
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300415 ch = dev->dev[channel_id];
416
417 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
418
419 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
420 common = &ch->common[i];
421 /* skip If streaming is not started in this channel */
422 if (0 == common->started)
423 continue;
424
425 /* Check the field format */
426 if (1 == ch->vpifparams.std_info.frm_fmt) {
427 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300428 spin_lock(&common->irqlock);
429 if (list_empty(&common->dma_queue)) {
430 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300431 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300432 }
433 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300434
435 if (!channel_first_int[i][channel_id])
436 vpif_process_buffer_complete(common);
437
438 channel_first_int[i][channel_id] = 0;
439
440 vpif_schedule_next_buffer(common);
441
442
443 channel_first_int[i][channel_id] = 0;
444 } else {
445 /**
446 * Interlaced mode. If it is first interrupt, ignore
447 * it
448 */
449 if (channel_first_int[i][channel_id]) {
450 channel_first_int[i][channel_id] = 0;
451 continue;
452 }
453 if (0 == i) {
454 ch->field_id ^= 1;
455 /* Get field id from VPIF registers */
456 fid = vpif_channel_getfid(ch->channel_id);
457 if (fid != ch->field_id) {
458 /**
459 * If field id does not match stored
460 * field id, make them in sync
461 */
462 if (0 == fid)
463 ch->field_id = fid;
464 return IRQ_HANDLED;
465 }
466 }
467 /* device field id and local field id are in sync */
468 if (0 == fid) {
469 /* this is even field */
470 if (common->cur_frm == common->next_frm)
471 continue;
472
473 /* mark the current buffer as done */
474 vpif_process_buffer_complete(common);
475 } else if (1 == fid) {
476 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300477 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300478 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300479 (common->cur_frm != common->next_frm)) {
480 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300481 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300482 }
483 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300484
485 vpif_schedule_next_buffer(common);
486 }
487 }
488 }
489 return IRQ_HANDLED;
490}
491
492/**
493 * vpif_update_std_info() - update standard related info
494 * @ch: ptr to channel object
495 *
496 * For a given standard selected by application, update values
497 * in the device data structures
498 */
499static int vpif_update_std_info(struct channel_obj *ch)
500{
501 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
502 struct vpif_params *vpifparams = &ch->vpifparams;
503 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300504 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300505 struct video_obj *vid_ch = &ch->video;
506 int index;
507
508 vpif_dbg(2, debug, "vpif_update_std_info\n");
509
Mats Randgaardaa444402010-12-16 12:17:42 -0300510 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300511 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300512 if (config->hd_sd == 0) {
513 vpif_dbg(2, debug, "SD format\n");
514 if (config->stdid & vid_ch->stdid) {
515 memcpy(std_info, config, sizeof(*config));
516 break;
517 }
518 } else {
519 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300520 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
521 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300522 memcpy(std_info, config, sizeof(*config));
523 break;
524 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300525 }
526 }
527
528 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300529 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300530 return -EINVAL;
531
532 common->fmt.fmt.pix.width = std_info->width;
533 common->width = std_info->width;
534 common->fmt.fmt.pix.height = std_info->height;
535 common->height = std_info->height;
536 common->fmt.fmt.pix.bytesperline = std_info->width;
537 vpifparams->video_params.hpitch = std_info->width;
538 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300539
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300540 return 0;
541}
542
543/**
544 * vpif_calculate_offsets : This function calculates buffers offsets
545 * @ch : ptr to channel object
546 *
547 * This function calculates buffer offsets for Y and C in the top and
548 * bottom field
549 */
550static void vpif_calculate_offsets(struct channel_obj *ch)
551{
552 unsigned int hpitch, vpitch, sizeimage;
553 struct video_obj *vid_ch = &(ch->video);
554 struct vpif_params *vpifparams = &ch->vpifparams;
555 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
556 enum v4l2_field field = common->fmt.fmt.pix.field;
557
558 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
559
560 if (V4L2_FIELD_ANY == field) {
561 if (vpifparams->std_info.frm_fmt)
562 vid_ch->buf_field = V4L2_FIELD_NONE;
563 else
564 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
565 } else
566 vid_ch->buf_field = common->fmt.fmt.pix.field;
567
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300568 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300569
570 hpitch = common->fmt.fmt.pix.bytesperline;
571 vpitch = sizeimage / (hpitch * 2);
572
573 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
574 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
575 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
576 common->ytop_off = 0;
577 common->ybtm_off = hpitch;
578 common->ctop_off = sizeimage / 2;
579 common->cbtm_off = sizeimage / 2 + hpitch;
580 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
581 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
582 common->ytop_off = 0;
583 common->ybtm_off = sizeimage / 4;
584 common->ctop_off = sizeimage / 2;
585 common->cbtm_off = common->ctop_off + sizeimage / 4;
586 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
587 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
588 common->ybtm_off = 0;
589 common->ytop_off = sizeimage / 4;
590 common->cbtm_off = sizeimage / 2;
591 common->ctop_off = common->cbtm_off + sizeimage / 4;
592 }
593 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
594 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
595 vpifparams->video_params.storage_mode = 1;
596 else
597 vpifparams->video_params.storage_mode = 0;
598
599 if (1 == vpifparams->std_info.frm_fmt)
600 vpifparams->video_params.hpitch =
601 common->fmt.fmt.pix.bytesperline;
602 else {
603 if ((field == V4L2_FIELD_ANY)
604 || (field == V4L2_FIELD_INTERLACED))
605 vpifparams->video_params.hpitch =
606 common->fmt.fmt.pix.bytesperline * 2;
607 else
608 vpifparams->video_params.hpitch =
609 common->fmt.fmt.pix.bytesperline;
610 }
611
612 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
613}
614
615/**
616 * vpif_config_format: configure default frame format in the device
617 * ch : ptr to channel object
618 */
619static void vpif_config_format(struct channel_obj *ch)
620{
621 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
622
623 vpif_dbg(2, debug, "vpif_config_format\n");
624
625 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
626 if (config_params.numbuffers[ch->channel_id] == 0)
627 common->memory = V4L2_MEMORY_USERPTR;
628 else
629 common->memory = V4L2_MEMORY_MMAP;
630
631 common->fmt.fmt.pix.sizeimage
632 = config_params.channel_bufsize[ch->channel_id];
633
634 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
635 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
636 else
637 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
638 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
639}
640
641/**
642 * vpif_get_default_field() - Get default field type based on interface
643 * @vpif_params - ptr to vpif params
644 */
645static inline enum v4l2_field vpif_get_default_field(
646 struct vpif_interface *iface)
647{
648 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
649 V4L2_FIELD_INTERLACED;
650}
651
652/**
653 * vpif_check_format() - check given pixel format for compatibility
654 * @ch - channel ptr
655 * @pixfmt - Given pixel format
656 * @update - update the values as per hardware requirement
657 *
658 * Check the application pixel format for S_FMT and update the input
659 * values as per hardware limits for TRY_FMT. The default pixel and
660 * field format is selected based on interface type.
661 */
662static int vpif_check_format(struct channel_obj *ch,
663 struct v4l2_pix_format *pixfmt,
664 int update)
665{
666 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
667 struct vpif_params *vpif_params = &ch->vpifparams;
668 enum v4l2_field field = pixfmt->field;
669 u32 sizeimage, hpitch, vpitch;
670 int ret = -EINVAL;
671
672 vpif_dbg(2, debug, "vpif_check_format\n");
673 /**
674 * first check for the pixel format. If if_type is Raw bayer,
675 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
676 * V4L2_PIX_FMT_YUV422P is supported
677 */
678 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
679 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
680 if (!update) {
681 vpif_dbg(2, debug, "invalid pix format\n");
682 goto exit;
683 }
684 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
685 }
686 } else {
687 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
688 if (!update) {
689 vpif_dbg(2, debug, "invalid pixel format\n");
690 goto exit;
691 }
692 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
693 }
694 }
695
696 if (!(VPIF_VALID_FIELD(field))) {
697 if (!update) {
698 vpif_dbg(2, debug, "invalid field format\n");
699 goto exit;
700 }
701 /**
702 * By default use FIELD_NONE for RAW Bayer capture
703 * and FIELD_INTERLACED for other interfaces
704 */
705 field = vpif_get_default_field(&vpif_params->iface);
706 } else if (field == V4L2_FIELD_ANY)
707 /* unsupported field. Use default */
708 field = vpif_get_default_field(&vpif_params->iface);
709
710 /* validate the hpitch */
711 hpitch = pixfmt->bytesperline;
712 if (hpitch < vpif_params->std_info.width) {
713 if (!update) {
714 vpif_dbg(2, debug, "invalid hpitch\n");
715 goto exit;
716 }
717 hpitch = vpif_params->std_info.width;
718 }
719
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300720 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300721
722 vpitch = sizeimage / (hpitch * 2);
723
724 /* validate the vpitch */
725 if (vpitch < vpif_params->std_info.height) {
726 if (!update) {
727 vpif_dbg(2, debug, "Invalid vpitch\n");
728 goto exit;
729 }
730 vpitch = vpif_params->std_info.height;
731 }
732
733 /* Check for 8 byte alignment */
734 if (!ALIGN(hpitch, 8)) {
735 if (!update) {
736 vpif_dbg(2, debug, "invalid pitch alignment\n");
737 goto exit;
738 }
739 /* adjust to next 8 byte boundary */
740 hpitch = (((hpitch + 7) / 8) * 8);
741 }
742 /* if update is set, modify the bytesperline and sizeimage */
743 if (update) {
744 pixfmt->bytesperline = hpitch;
745 pixfmt->sizeimage = hpitch * vpitch * 2;
746 }
747 /**
748 * Image width and height is always based on current standard width and
749 * height
750 */
751 pixfmt->width = common->fmt.fmt.pix.width;
752 pixfmt->height = common->fmt.fmt.pix.height;
753 return 0;
754exit:
755 return ret;
756}
757
758/**
759 * vpif_config_addr() - function to configure buffer address in vpif
760 * @ch - channel ptr
761 * @muxmode - channel mux mode
762 */
763static void vpif_config_addr(struct channel_obj *ch, int muxmode)
764{
765 struct common_obj *common;
766
767 vpif_dbg(2, debug, "vpif_config_addr\n");
768
769 common = &(ch->common[VPIF_VIDEO_INDEX]);
770
771 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
772 common->set_addr = ch1_set_videobuf_addr;
773 else if (2 == muxmode)
774 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
775 else
776 common->set_addr = ch0_set_videobuf_addr;
777}
778
779/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300780 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300781 * @filep: file pointer
782 * @vma: ptr to vm_area_struct
783 */
784static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
785{
786 /* Get the channel object and file handle object */
787 struct vpif_fh *fh = filep->private_data;
788 struct channel_obj *ch = fh->channel;
789 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300790 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300791
792 vpif_dbg(2, debug, "vpif_mmap\n");
793
Hans Verkuil72246792012-07-31 03:48:31 -0300794 if (mutex_lock_interruptible(&common->lock))
795 return -ERESTARTSYS;
796 ret = vb2_mmap(&common->buffer_queue, vma);
797 mutex_unlock(&common->lock);
798 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300799}
800
801/**
802 * vpif_poll: It is used for select/poll system call
803 * @filep: file pointer
804 * @wait: poll table to wait
805 */
806static unsigned int vpif_poll(struct file *filep, poll_table * wait)
807{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300808 struct vpif_fh *fh = filep->private_data;
809 struct channel_obj *channel = fh->channel;
810 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300811 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300812
813 vpif_dbg(2, debug, "vpif_poll\n");
814
Hans Verkuil72246792012-07-31 03:48:31 -0300815 if (common->started) {
816 mutex_lock(&common->lock);
817 res = vb2_poll(&common->buffer_queue, filep, wait);
818 mutex_unlock(&common->lock);
819 }
820 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300821}
822
823/**
824 * vpif_open : vpif open handler
825 * @filep: file ptr
826 *
827 * It creates object of file handle structure and stores it in private_data
828 * member of filepointer
829 */
830static int vpif_open(struct file *filep)
831{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300832 struct video_device *vdev = video_devdata(filep);
833 struct common_obj *common;
834 struct video_obj *vid_ch;
835 struct channel_obj *ch;
836 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300837
838 vpif_dbg(2, debug, "vpif_open\n");
839
840 ch = video_get_drvdata(vdev);
841
842 vid_ch = &ch->video;
843 common = &ch->common[VPIF_VIDEO_INDEX];
844
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300845 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300846 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300847 if (NULL == fh) {
848 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300849 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300850 }
851
Hans Verkuil72246792012-07-31 03:48:31 -0300852 if (mutex_lock_interruptible(&common->lock)) {
853 kfree(fh);
854 return -ERESTARTSYS;
855 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300856 /* store pointer to fh in private_data member of filep */
857 filep->private_data = fh;
858 fh->channel = ch;
859 fh->initialized = 0;
860 /* If decoder is not initialized. initialize it */
861 if (!ch->initialized) {
862 fh->initialized = 1;
863 ch->initialized = 1;
864 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
865 }
866 /* Increment channel usrs counter */
867 ch->usrs++;
868 /* Set io_allowed member to false */
869 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
870 /* Initialize priority of this instance to default priority */
871 fh->prio = V4L2_PRIORITY_UNSET;
872 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300873 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300874 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300875}
876
877/**
878 * vpif_release : function to clean up file close
879 * @filep: file pointer
880 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300881 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300882 * handle
883 */
884static int vpif_release(struct file *filep)
885{
886 struct vpif_fh *fh = filep->private_data;
887 struct channel_obj *ch = fh->channel;
888 struct common_obj *common;
889
890 vpif_dbg(2, debug, "vpif_release\n");
891
892 common = &ch->common[VPIF_VIDEO_INDEX];
893
Hans Verkuil72246792012-07-31 03:48:31 -0300894 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300895 /* if this instance is doing IO */
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300896 if (fh->io_allowed[VPIF_VIDEO_INDEX])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300897 /* Reset io_usrs member of channel object */
898 common->io_usrs = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300899
900 /* Decrement channel usrs counter */
901 ch->usrs--;
902
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300903 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300904 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300905
906 if (fh->initialized)
907 ch->initialized = 0;
908
Hans Verkuil72246792012-07-31 03:48:31 -0300909 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300910 filep->private_data = NULL;
911 kfree(fh);
912 return 0;
913}
914
915/**
916 * vpif_reqbufs() - request buffer handler
917 * @file: file ptr
918 * @priv: file handle
919 * @reqbuf: request buffer structure ptr
920 */
921static int vpif_reqbufs(struct file *file, void *priv,
922 struct v4l2_requestbuffers *reqbuf)
923{
924 struct vpif_fh *fh = priv;
925 struct channel_obj *ch = fh->channel;
926 struct common_obj *common;
927 u8 index = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300928
929 vpif_dbg(2, debug, "vpif_reqbufs\n");
930
931 /**
932 * This file handle has not initialized the channel,
933 * It is not allowed to do settings
934 */
935 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
936 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
937 if (!fh->initialized) {
938 vpif_dbg(1, debug, "Channel Busy\n");
939 return -EBUSY;
940 }
941 }
942
Manjunath Hadli764af392012-04-13 04:49:34 -0300943 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300944 return -EINVAL;
945
946 index = VPIF_VIDEO_INDEX;
947
948 common = &ch->common[index];
949
Hans Verkuil46656af2011-01-04 06:51:35 -0300950 if (0 != common->io_usrs)
951 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300952
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300953 /* Set io allowed member of file handle to TRUE */
954 fh->io_allowed[index] = 1;
955 /* Increment io usrs member of channel object to 1 */
956 common->io_usrs = 1;
957 /* Store type of memory requested in channel object */
958 common->memory = reqbuf->memory;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300959
960 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300961 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300962}
963
964/**
965 * vpif_querybuf() - query buffer handler
966 * @file: file ptr
967 * @priv: file handle
968 * @buf: v4l2 buffer structure ptr
969 */
970static int vpif_querybuf(struct file *file, void *priv,
971 struct v4l2_buffer *buf)
972{
973 struct vpif_fh *fh = priv;
974 struct channel_obj *ch = fh->channel;
975 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
976
977 vpif_dbg(2, debug, "vpif_querybuf\n");
978
979 if (common->fmt.type != buf->type)
980 return -EINVAL;
981
982 if (common->memory != V4L2_MEMORY_MMAP) {
983 vpif_dbg(1, debug, "Invalid memory\n");
984 return -EINVAL;
985 }
986
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300987 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300988}
989
990/**
991 * vpif_qbuf() - query buffer handler
992 * @file: file ptr
993 * @priv: file handle
994 * @buf: v4l2 buffer structure ptr
995 */
996static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
997{
998
999 struct vpif_fh *fh = priv;
1000 struct channel_obj *ch = fh->channel;
1001 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1002 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001003
1004 vpif_dbg(2, debug, "vpif_qbuf\n");
1005
1006 if (common->fmt.type != tbuf.type) {
1007 vpif_err("invalid buffer type\n");
1008 return -EINVAL;
1009 }
1010
1011 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001012 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001013 return -EACCES;
1014 }
1015
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001016 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001017}
1018
1019/**
1020 * vpif_dqbuf() - query buffer handler
1021 * @file: file ptr
1022 * @priv: file handle
1023 * @buf: v4l2 buffer structure ptr
1024 */
1025static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1026{
1027 struct vpif_fh *fh = priv;
1028 struct channel_obj *ch = fh->channel;
1029 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1030
1031 vpif_dbg(2, debug, "vpif_dqbuf\n");
1032
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001033 return vb2_dqbuf(&common->buffer_queue, buf,
1034 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001035}
1036
1037/**
1038 * vpif_streamon() - streamon handler
1039 * @file: file ptr
1040 * @priv: file handle
1041 * @buftype: v4l2 buffer type
1042 */
1043static int vpif_streamon(struct file *file, void *priv,
1044 enum v4l2_buf_type buftype)
1045{
1046
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001047 struct vpif_fh *fh = priv;
1048 struct channel_obj *ch = fh->channel;
1049 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1050 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1051 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001052 int ret = 0;
1053
1054 vpif_dbg(2, debug, "vpif_streamon\n");
1055
1056 vpif = &ch->vpifparams;
1057
1058 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1059 vpif_dbg(1, debug, "buffer type not supported\n");
1060 return -EINVAL;
1061 }
1062
1063 /* If file handle is not allowed IO, return error */
1064 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1065 vpif_dbg(1, debug, "io not allowed\n");
1066 return -EACCES;
1067 }
1068
1069 /* If Streaming is already started, return error */
1070 if (common->started) {
1071 vpif_dbg(1, debug, "channel->started\n");
1072 return -EBUSY;
1073 }
1074
1075 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1076 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1077 vpif->std_info.ycmux_mode == 0) ||
1078 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1079 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1080 vpif_dbg(1, debug, "other channel is being used\n");
1081 return -EBUSY;
1082 }
1083
1084 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1085 if (ret)
1086 return ret;
1087
1088 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001089 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001090
Hans Verkuil178cce12012-09-20 09:06:30 -03001091 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001092 vpif_dbg(1, debug, "stream on failed in subdev\n");
1093 return ret;
1094 }
1095
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001096 /* Call vb2_streamon to start streaming in videobuf2 */
1097 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001098 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001099 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001100 return ret;
1101 }
1102
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001103 return ret;
1104}
1105
1106/**
1107 * vpif_streamoff() - streamoff handler
1108 * @file: file ptr
1109 * @priv: file handle
1110 * @buftype: v4l2 buffer type
1111 */
1112static int vpif_streamoff(struct file *file, void *priv,
1113 enum v4l2_buf_type buftype)
1114{
1115
1116 struct vpif_fh *fh = priv;
1117 struct channel_obj *ch = fh->channel;
1118 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1119 int ret;
1120
1121 vpif_dbg(2, debug, "vpif_streamoff\n");
1122
1123 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1124 vpif_dbg(1, debug, "buffer type not supported\n");
1125 return -EINVAL;
1126 }
1127
1128 /* If io is allowed for this file handle, return error */
1129 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1130 vpif_dbg(1, debug, "io not allowed\n");
1131 return -EACCES;
1132 }
1133
1134 /* If streaming is not started, return error */
1135 if (!common->started) {
1136 vpif_dbg(1, debug, "channel->started\n");
1137 return -EINVAL;
1138 }
1139
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001140 /* disable channel */
1141 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1142 enable_channel0(0);
1143 channel0_intr_enable(0);
1144 } else {
1145 enable_channel1(0);
1146 channel1_intr_enable(0);
1147 }
1148
1149 common->started = 0;
1150
Hans Verkuil178cce12012-09-20 09:06:30 -03001151 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001152
Hans Verkuil178cce12012-09-20 09:06:30 -03001153 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001154 vpif_dbg(1, debug, "stream off failed in subdev\n");
1155
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001156 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001157}
1158
1159/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001160 * vpif_input_to_subdev() - Maps input to sub device
1161 * @vpif_cfg - global config ptr
1162 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001163 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001164 *
1165 * lookup the sub device information for a given input index.
1166 * we report all the inputs to application. inputs table also
1167 * has sub device name for the each input
1168 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001169static int vpif_input_to_subdev(
1170 struct vpif_capture_config *vpif_cfg,
1171 struct vpif_capture_chan_config *chan_cfg,
1172 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001173{
Hans Verkuil178cce12012-09-20 09:06:30 -03001174 struct vpif_subdev_info *subdev_info;
1175 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001176 int i;
1177
Hans Verkuil178cce12012-09-20 09:06:30 -03001178 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001179
Hans Verkuil178cce12012-09-20 09:06:30 -03001180 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1181 if (subdev_name == NULL)
1182 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001183
1184 /* loop through the sub device list to get the sub device info */
1185 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1186 subdev_info = &vpif_cfg->subdev_info[i];
1187 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001188 return i;
1189 }
1190 return -1;
1191}
1192
1193/**
1194 * vpif_set_input() - Select an input
1195 * @vpif_cfg - global config ptr
1196 * @ch - channel
1197 * @_index - Given input index from application
1198 *
1199 * Select the given input.
1200 */
1201static int vpif_set_input(
1202 struct vpif_capture_config *vpif_cfg,
1203 struct channel_obj *ch,
1204 int index)
1205{
1206 struct vpif_capture_chan_config *chan_cfg =
1207 &vpif_cfg->chan_config[ch->channel_id];
1208 struct vpif_subdev_info *subdev_info = NULL;
1209 struct v4l2_subdev *sd = NULL;
1210 u32 input = 0, output = 0;
1211 int sd_index;
1212 int ret;
1213
1214 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1215 if (sd_index >= 0) {
1216 sd = vpif_obj.sd[sd_index];
1217 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001218 }
1219
Hans Verkuil178cce12012-09-20 09:06:30 -03001220 /* first setup input path from sub device to vpif */
1221 if (sd && vpif_cfg->setup_input_path) {
1222 ret = vpif_cfg->setup_input_path(ch->channel_id,
1223 subdev_info->name);
1224 if (ret < 0) {
1225 vpif_dbg(1, debug, "couldn't setup input path for the" \
1226 " sub device %s, for input index %d\n",
1227 subdev_info->name, index);
1228 return ret;
1229 }
1230 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001231
Hans Verkuil178cce12012-09-20 09:06:30 -03001232 if (sd) {
1233 input = chan_cfg->inputs[index].input_route;
1234 output = chan_cfg->inputs[index].output_route;
1235 ret = v4l2_subdev_call(sd, video, s_routing,
1236 input, output, 0);
1237 if (ret < 0 && ret != -ENOIOCTLCMD) {
1238 vpif_dbg(1, debug, "Failed to set input\n");
1239 return ret;
1240 }
1241 }
1242 ch->input_idx = index;
1243 ch->sd = sd;
1244 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001245 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001246
Hans Verkuil178cce12012-09-20 09:06:30 -03001247 /* update tvnorms from the sub device input info */
1248 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1249 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001250}
1251
1252/**
1253 * vpif_querystd() - querystd handler
1254 * @file: file ptr
1255 * @priv: file handle
1256 * @std_id: ptr to std id
1257 *
1258 * This function is called to detect standard at the selected input
1259 */
1260static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1261{
1262 struct vpif_fh *fh = priv;
1263 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001264 int ret = 0;
1265
1266 vpif_dbg(2, debug, "vpif_querystd\n");
1267
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001268 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001269 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001270
Hans Verkuil178cce12012-09-20 09:06:30 -03001271 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1272 return -ENODATA;
1273 if (ret) {
1274 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1275 return ret;
1276 }
1277
1278 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001279}
1280
1281/**
1282 * vpif_g_std() - get STD handler
1283 * @file: file ptr
1284 * @priv: file handle
1285 * @std_id: ptr to std id
1286 */
1287static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1288{
1289 struct vpif_fh *fh = priv;
1290 struct channel_obj *ch = fh->channel;
1291
1292 vpif_dbg(2, debug, "vpif_g_std\n");
1293
1294 *std = ch->video.stdid;
1295 return 0;
1296}
1297
1298/**
1299 * vpif_s_std() - set STD handler
1300 * @file: file ptr
1301 * @priv: file handle
1302 * @std_id: ptr to std id
1303 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001304static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001305{
1306 struct vpif_fh *fh = priv;
1307 struct channel_obj *ch = fh->channel;
1308 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1309 int ret = 0;
1310
1311 vpif_dbg(2, debug, "vpif_s_std\n");
1312
1313 if (common->started) {
1314 vpif_err("streaming in progress\n");
1315 return -EBUSY;
1316 }
1317
1318 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1319 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1320 if (!fh->initialized) {
1321 vpif_dbg(1, debug, "Channel Busy\n");
1322 return -EBUSY;
1323 }
1324 }
1325
Hans Verkuilffb48772010-05-01 08:03:24 -03001326 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001327 if (0 != ret)
1328 return ret;
1329
1330 fh->initialized = 1;
1331
1332 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001333 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001334 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001335
1336 /* Get the information about the standard */
1337 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001338 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001339 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001340 }
1341
1342 /* Configure the default format information */
1343 vpif_config_format(ch);
1344
1345 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001346 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001347 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001348 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001349 return ret;
1350 }
1351 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001352}
1353
1354/**
1355 * vpif_enum_input() - ENUMINPUT handler
1356 * @file: file ptr
1357 * @priv: file handle
1358 * @input: ptr to input structure
1359 */
1360static int vpif_enum_input(struct file *file, void *priv,
1361 struct v4l2_input *input)
1362{
1363
1364 struct vpif_capture_config *config = vpif_dev->platform_data;
1365 struct vpif_capture_chan_config *chan_cfg;
1366 struct vpif_fh *fh = priv;
1367 struct channel_obj *ch = fh->channel;
1368
1369 chan_cfg = &config->chan_config[ch->channel_id];
1370
1371 if (input->index >= chan_cfg->input_count) {
1372 vpif_dbg(1, debug, "Invalid input index\n");
1373 return -EINVAL;
1374 }
1375
1376 memcpy(input, &chan_cfg->inputs[input->index].input,
1377 sizeof(*input));
1378 return 0;
1379}
1380
1381/**
1382 * vpif_g_input() - Get INPUT handler
1383 * @file: file ptr
1384 * @priv: file handle
1385 * @index: ptr to input index
1386 */
1387static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1388{
1389 struct vpif_fh *fh = priv;
1390 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001391
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001392 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001393 return 0;
1394}
1395
1396/**
1397 * vpif_s_input() - Set INPUT handler
1398 * @file: file ptr
1399 * @priv: file handle
1400 * @index: input index
1401 */
1402static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1403{
1404 struct vpif_capture_config *config = vpif_dev->platform_data;
1405 struct vpif_capture_chan_config *chan_cfg;
1406 struct vpif_fh *fh = priv;
1407 struct channel_obj *ch = fh->channel;
1408 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001409 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001410
1411 chan_cfg = &config->chan_config[ch->channel_id];
1412
Hans Verkuil7aaad132012-09-20 09:06:25 -03001413 if (index >= chan_cfg->input_count)
1414 return -EINVAL;
1415
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001416 if (common->started) {
1417 vpif_err("Streaming in progress\n");
1418 return -EBUSY;
1419 }
1420
1421 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1422 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1423 if (!fh->initialized) {
1424 vpif_dbg(1, debug, "Channel Busy\n");
1425 return -EBUSY;
1426 }
1427 }
1428
Hans Verkuilffb48772010-05-01 08:03:24 -03001429 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001430 if (0 != ret)
1431 return ret;
1432
1433 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001434 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001435}
1436
1437/**
1438 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1439 * @file: file ptr
1440 * @priv: file handle
1441 * @index: input index
1442 */
1443static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1444 struct v4l2_fmtdesc *fmt)
1445{
1446 struct vpif_fh *fh = priv;
1447 struct channel_obj *ch = fh->channel;
1448
1449 if (fmt->index != 0) {
1450 vpif_dbg(1, debug, "Invalid format index\n");
1451 return -EINVAL;
1452 }
1453
1454 /* Fill in the information about format */
1455 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1456 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1457 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1458 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1459 } else {
1460 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1461 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1462 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1463 }
1464 return 0;
1465}
1466
1467/**
1468 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1469 * @file: file ptr
1470 * @priv: file handle
1471 * @fmt: ptr to v4l2 format structure
1472 */
1473static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1474 struct v4l2_format *fmt)
1475{
1476 struct vpif_fh *fh = priv;
1477 struct channel_obj *ch = fh->channel;
1478 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1479
1480 return vpif_check_format(ch, pixfmt, 1);
1481}
1482
1483
1484/**
1485 * vpif_g_fmt_vid_cap() - Set INPUT handler
1486 * @file: file ptr
1487 * @priv: file handle
1488 * @fmt: ptr to v4l2 format structure
1489 */
1490static int vpif_g_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 common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1496
1497 /* Check the validity of the buffer type */
1498 if (common->fmt.type != fmt->type)
1499 return -EINVAL;
1500
1501 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001502 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001503 return 0;
1504}
1505
1506/**
1507 * vpif_s_fmt_vid_cap() - Set FMT handler
1508 * @file: file ptr
1509 * @priv: file handle
1510 * @fmt: ptr to v4l2 format structure
1511 */
1512static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1513 struct v4l2_format *fmt)
1514{
1515 struct vpif_fh *fh = priv;
1516 struct channel_obj *ch = fh->channel;
1517 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1518 struct v4l2_pix_format *pixfmt;
1519 int ret = 0;
1520
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001521 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001522
1523 /* If streaming is started, return error */
1524 if (common->started) {
1525 vpif_dbg(1, debug, "Streaming is started\n");
1526 return -EBUSY;
1527 }
1528
1529 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1530 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1531 if (!fh->initialized) {
1532 vpif_dbg(1, debug, "Channel Busy\n");
1533 return -EBUSY;
1534 }
1535 }
1536
Hans Verkuilffb48772010-05-01 08:03:24 -03001537 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001538 if (0 != ret)
1539 return ret;
1540
1541 fh->initialized = 1;
1542
1543 pixfmt = &fmt->fmt.pix;
1544 /* Check for valid field format */
1545 ret = vpif_check_format(ch, pixfmt, 0);
1546
1547 if (ret)
1548 return ret;
1549 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001550 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001551 return 0;
1552}
1553
1554/**
1555 * vpif_querycap() - QUERYCAP handler
1556 * @file: file ptr
1557 * @priv: file handle
1558 * @cap: ptr to v4l2_capability structure
1559 */
1560static int vpif_querycap(struct file *file, void *priv,
1561 struct v4l2_capability *cap)
1562{
1563 struct vpif_capture_config *config = vpif_dev->platform_data;
1564
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001565 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1566 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1567 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1568 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1569 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001570 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1571
1572 return 0;
1573}
1574
1575/**
1576 * vpif_g_priority() - get priority handler
1577 * @file: file ptr
1578 * @priv: file handle
1579 * @prio: ptr to v4l2_priority structure
1580 */
1581static int vpif_g_priority(struct file *file, void *priv,
1582 enum v4l2_priority *prio)
1583{
1584 struct vpif_fh *fh = priv;
1585 struct channel_obj *ch = fh->channel;
1586
1587 *prio = v4l2_prio_max(&ch->prio);
1588
1589 return 0;
1590}
1591
1592/**
1593 * vpif_s_priority() - set priority handler
1594 * @file: file ptr
1595 * @priv: file handle
1596 * @prio: ptr to v4l2_priority structure
1597 */
1598static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1599{
1600 struct vpif_fh *fh = priv;
1601 struct channel_obj *ch = fh->channel;
1602
1603 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1604}
1605
1606/**
1607 * vpif_cropcap() - cropcap handler
1608 * @file: file ptr
1609 * @priv: file handle
1610 * @crop: ptr to v4l2_cropcap structure
1611 */
1612static int vpif_cropcap(struct file *file, void *priv,
1613 struct v4l2_cropcap *crop)
1614{
1615 struct vpif_fh *fh = priv;
1616 struct channel_obj *ch = fh->channel;
1617 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1618
1619 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1620 return -EINVAL;
1621
1622 crop->bounds.left = 0;
1623 crop->bounds.top = 0;
1624 crop->bounds.height = common->height;
1625 crop->bounds.width = common->width;
1626 crop->defrect = crop->bounds;
1627 return 0;
1628}
1629
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001630/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001631 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001632 * @file: file ptr
1633 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001634 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001635 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001636static int
1637vpif_enum_dv_timings(struct file *file, void *priv,
1638 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001639{
1640 struct vpif_fh *fh = priv;
1641 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001642 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001643
Hans Verkuil178cce12012-09-20 09:06:30 -03001644 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001645 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001646 return -EINVAL;
1647 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001648}
1649
1650/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001651 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001652 * @file: file ptr
1653 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001654 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001655 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001656static int
1657vpif_query_dv_timings(struct file *file, void *priv,
1658 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001659{
1660 struct vpif_fh *fh = priv;
1661 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001662 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001663
Hans Verkuil178cce12012-09-20 09:06:30 -03001664 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001665 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001666 return -ENODATA;
1667 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001668}
1669
Mats Randgaardc027e162010-12-16 12:17:44 -03001670/**
1671 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1672 * @file: file ptr
1673 * @priv: file handle
1674 * @timings: digital video timings
1675 */
1676static int vpif_s_dv_timings(struct file *file, void *priv,
1677 struct v4l2_dv_timings *timings)
1678{
1679 struct vpif_fh *fh = priv;
1680 struct channel_obj *ch = fh->channel;
1681 struct vpif_params *vpifparams = &ch->vpifparams;
1682 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1683 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001684 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001685 int ret;
1686
1687 if (timings->type != V4L2_DV_BT_656_1120) {
1688 vpif_dbg(2, debug, "Timing type not defined\n");
1689 return -EINVAL;
1690 }
1691
1692 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001693 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1694 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1695 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001696 if (ret < 0) {
1697 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1698 return ret;
1699 }
1700
1701 if (!(timings->bt.width && timings->bt.height &&
1702 (timings->bt.hbackporch ||
1703 timings->bt.hfrontporch ||
1704 timings->bt.hsync) &&
1705 timings->bt.vfrontporch &&
1706 (timings->bt.vbackporch ||
1707 timings->bt.vsync))) {
1708 vpif_dbg(2, debug, "Timings for width, height, "
1709 "horizontal back porch, horizontal sync, "
1710 "horizontal front porch, vertical back porch, "
1711 "vertical sync and vertical back porch "
1712 "must be defined\n");
1713 return -EINVAL;
1714 }
1715
Hans Verkuil0598c172012-09-18 07:18:47 -03001716 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001717
1718 /* Configure video port timings */
1719
Hans Verkuile3655262013-07-29 08:41:00 -03001720 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001721 std_info->sav2eav = bt->width;
1722
1723 std_info->l1 = 1;
1724 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1725
Hans Verkuile3655262013-07-29 08:41:00 -03001726 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001727 if (bt->interlaced) {
1728 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001729 std_info->l5 = std_info->vsize/2 -
1730 (bt->vfrontporch - 1);
1731 std_info->l7 = std_info->vsize/2 + 1;
1732 std_info->l9 = std_info->l7 + bt->il_vsync +
1733 bt->il_vbackporch + 1;
1734 std_info->l11 = std_info->vsize -
1735 (bt->il_vfrontporch - 1);
1736 } else {
1737 vpif_dbg(2, debug, "Required timing values for "
1738 "interlaced BT format missing\n");
1739 return -EINVAL;
1740 }
1741 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001742 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1743 }
1744 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1745 std_info->width = bt->width;
1746 std_info->height = bt->height;
1747 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1748 std_info->ycmux_mode = 0;
1749 std_info->capture_format = 0;
1750 std_info->vbi_supported = 0;
1751 std_info->hd_sd = 1;
1752 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001753
1754 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001755 return 0;
1756}
1757
1758/**
1759 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1760 * @file: file ptr
1761 * @priv: file handle
1762 * @timings: digital video timings
1763 */
1764static int vpif_g_dv_timings(struct file *file, void *priv,
1765 struct v4l2_dv_timings *timings)
1766{
1767 struct vpif_fh *fh = priv;
1768 struct channel_obj *ch = fh->channel;
1769 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001770
Hans Verkuil0598c172012-09-18 07:18:47 -03001771 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001772
1773 return 0;
1774}
1775
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001776/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001777 * vpif_log_status() - Status information
1778 * @file: file ptr
1779 * @priv: file handle
1780 *
1781 * Returns zero.
1782 */
1783static int vpif_log_status(struct file *filep, void *priv)
1784{
1785 /* status for sub devices */
1786 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1787
1788 return 0;
1789}
1790
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001791/* vpif capture ioctl operations */
1792static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1793 .vidioc_querycap = vpif_querycap,
1794 .vidioc_g_priority = vpif_g_priority,
1795 .vidioc_s_priority = vpif_s_priority,
1796 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1797 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1798 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1799 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1800 .vidioc_enum_input = vpif_enum_input,
1801 .vidioc_s_input = vpif_s_input,
1802 .vidioc_g_input = vpif_g_input,
1803 .vidioc_reqbufs = vpif_reqbufs,
1804 .vidioc_querybuf = vpif_querybuf,
1805 .vidioc_querystd = vpif_querystd,
1806 .vidioc_s_std = vpif_s_std,
1807 .vidioc_g_std = vpif_g_std,
1808 .vidioc_qbuf = vpif_qbuf,
1809 .vidioc_dqbuf = vpif_dqbuf,
1810 .vidioc_streamon = vpif_streamon,
1811 .vidioc_streamoff = vpif_streamoff,
1812 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001813 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1814 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001815 .vidioc_s_dv_timings = vpif_s_dv_timings,
1816 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001817 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001818};
1819
1820/* vpif file operations */
1821static struct v4l2_file_operations vpif_fops = {
1822 .owner = THIS_MODULE,
1823 .open = vpif_open,
1824 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001825 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001826 .mmap = vpif_mmap,
1827 .poll = vpif_poll
1828};
1829
1830/* vpif video template */
1831static struct video_device vpif_video_template = {
1832 .name = "vpif",
1833 .fops = &vpif_fops,
1834 .minor = -1,
1835 .ioctl_ops = &vpif_ioctl_ops,
1836};
1837
1838/**
1839 * initialize_vpif() - Initialize vpif data structures
1840 *
1841 * Allocate memory for data structures and initialize them
1842 */
1843static int initialize_vpif(void)
1844{
1845 int err = 0, i, j;
1846 int free_channel_objects_index;
1847
1848 /* Default number of buffers should be 3 */
1849 if ((ch0_numbuffers > 0) &&
1850 (ch0_numbuffers < config_params.min_numbuffers))
1851 ch0_numbuffers = config_params.min_numbuffers;
1852 if ((ch1_numbuffers > 0) &&
1853 (ch1_numbuffers < config_params.min_numbuffers))
1854 ch1_numbuffers = config_params.min_numbuffers;
1855
1856 /* Set buffer size to min buffers size if it is invalid */
1857 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1858 ch0_bufsize =
1859 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1860 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1861 ch1_bufsize =
1862 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1863
1864 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1865 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1866 if (ch0_numbuffers) {
1867 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1868 = ch0_bufsize;
1869 }
1870 if (ch1_numbuffers) {
1871 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1872 = ch1_bufsize;
1873 }
1874
1875 /* Allocate memory for six channel objects */
1876 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1877 vpif_obj.dev[i] =
1878 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1879 /* If memory allocation fails, return error */
1880 if (!vpif_obj.dev[i]) {
1881 free_channel_objects_index = i;
1882 err = -ENOMEM;
1883 goto vpif_init_free_channel_objects;
1884 }
1885 }
1886 return 0;
1887
1888vpif_init_free_channel_objects:
1889 for (j = 0; j < free_channel_objects_index; j++)
1890 kfree(vpif_obj.dev[j]);
1891 return err;
1892}
1893
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001894static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1895 struct v4l2_subdev *subdev,
1896 struct v4l2_async_subdev *asd)
1897{
1898 int i;
1899
1900 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1901 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1902 subdev->name)) {
1903 vpif_obj.sd[i] = subdev;
1904 return 0;
1905 }
1906
1907 return -EINVAL;
1908}
1909
1910static int vpif_probe_complete(void)
1911{
1912 struct common_obj *common;
1913 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001914 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001915 int i, j, err, k;
1916
1917 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1918 ch = vpif_obj.dev[j];
1919 ch->channel_id = j;
1920 common = &(ch->common[VPIF_VIDEO_INDEX]);
1921 spin_lock_init(&common->irqlock);
1922 mutex_init(&common->lock);
1923 ch->video_dev->lock = &common->lock;
1924 /* Initialize prio member of channel object */
1925 v4l2_prio_init(&ch->prio);
1926 video_set_drvdata(ch->video_dev, ch);
1927
1928 /* select input 0 */
1929 err = vpif_set_input(vpif_obj.config, ch, 0);
1930 if (err)
1931 goto probe_out;
1932
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001933 /* Initialize vb2 queue */
1934 q = &common->buffer_queue;
1935 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1936 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1937 q->drv_priv = ch;
1938 q->ops = &video_qops;
1939 q->mem_ops = &vb2_dma_contig_memops;
1940 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1941 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1942 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001943 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001944
1945 err = vb2_queue_init(q);
1946 if (err) {
1947 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1948 goto probe_out;
1949 }
1950
1951 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1952 if (IS_ERR(common->alloc_ctx)) {
1953 vpif_err("Failed to get the context\n");
1954 err = PTR_ERR(common->alloc_ctx);
1955 goto probe_out;
1956 }
1957
1958 INIT_LIST_HEAD(&common->dma_queue);
1959
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001960 err = video_register_device(ch->video_dev,
1961 VFL_TYPE_GRABBER, (j ? 1 : 0));
1962 if (err)
1963 goto probe_out;
1964 }
1965
1966 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1967 return 0;
1968
1969probe_out:
1970 for (k = 0; k < j; k++) {
1971 /* Get the pointer to the channel object */
1972 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001973 common = &ch->common[k];
1974 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001975 /* Unregister video device */
1976 video_unregister_device(ch->video_dev);
1977 }
1978 kfree(vpif_obj.sd);
1979 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1980 ch = vpif_obj.dev[i];
1981 /* Note: does nothing if ch->video_dev == NULL */
1982 video_device_release(ch->video_dev);
1983 }
1984 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1985
1986 return err;
1987}
1988
1989static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1990{
1991 return vpif_probe_complete();
1992}
1993
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001994/**
1995 * vpif_probe : This function probes the vpif capture driver
1996 * @pdev: platform device pointer
1997 *
1998 * This creates device entries by register itself to the V4L2 driver and
1999 * initializes fields of each channel objects
2000 */
2001static __init int vpif_probe(struct platform_device *pdev)
2002{
2003 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002004 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002005 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002006 struct i2c_adapter *i2c_adap;
2007 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002008 struct video_device *vfd;
2009 struct resource *res;
2010 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03002011 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002012
2013 vpif_dev = &pdev->dev;
2014
2015 err = initialize_vpif();
2016 if (err) {
2017 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2018 return err;
2019 }
2020
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002021 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2022 if (err) {
2023 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2024 return err;
2025 }
2026
Hans Verkuil5be452c2012-09-20 09:06:29 -03002027 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03002028 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2029 IRQF_SHARED, "VPIF_Capture",
2030 (void *)(&vpif_obj.dev[res_idx]->
2031 channel_id));
2032 if (err) {
2033 err = -EINVAL;
2034 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002035 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002036 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002037 }
2038
2039 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2040 /* Get the pointer to the channel object */
2041 ch = vpif_obj.dev[i];
2042 /* Allocate memory for video device */
2043 vfd = video_device_alloc();
2044 if (NULL == vfd) {
2045 for (j = 0; j < i; j++) {
2046 ch = vpif_obj.dev[j];
2047 video_device_release(ch->video_dev);
2048 }
2049 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002050 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002051 }
2052
2053 /* Initialize field of video device */
2054 *vfd = vpif_video_template;
2055 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2056 vfd->release = video_device_release;
2057 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002058 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002059 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002060 /* Set video_dev to the video device */
2061 ch->video_dev = vfd;
2062 }
2063
Manjunath Hadli764af392012-04-13 04:49:34 -03002064 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2065 if (res) {
2066 size = resource_size(res);
2067 /* The resources are divided into two equal memory and when we
2068 * have HD output we can add them together
2069 */
2070 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2071 ch = vpif_obj.dev[j];
2072 ch->channel_id = j;
2073 /* only enabled if second resource exists */
2074 config_params.video_limit[ch->channel_id] = 0;
2075 if (size)
2076 config_params.video_limit[ch->channel_id] =
2077 size/2;
2078 }
2079 }
2080
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002081 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002082
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002083 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002084 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002085 GFP_KERNEL);
2086 if (vpif_obj.sd == NULL) {
2087 vpif_err("unable to allocate memory for subdevice pointers\n");
2088 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002089 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002090 }
2091
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002092 if (!vpif_obj.config->asd_sizes) {
2093 i2c_adap = i2c_get_adapter(1);
2094 for (i = 0; i < subdev_count; i++) {
2095 subdevdata = &vpif_obj.config->subdev_info[i];
2096 vpif_obj.sd[i] =
2097 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2098 i2c_adap,
2099 &subdevdata->
2100 board_info,
2101 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002102
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002103 if (!vpif_obj.sd[i]) {
2104 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03002105 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002106 goto probe_subdev_out;
2107 }
2108 v4l2_info(&vpif_obj.v4l2_dev,
2109 "registered sub device %s\n",
2110 subdevdata->name);
2111 }
2112 vpif_probe_complete();
2113 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03002114 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002115 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2116 vpif_obj.notifier.bound = vpif_async_bound;
2117 vpif_obj.notifier.complete = vpif_async_complete;
2118 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2119 &vpif_obj.notifier);
2120 if (err) {
2121 vpif_err("Error registering async notifier\n");
2122 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002123 goto probe_subdev_out;
2124 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002125 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002126
2127 return 0;
2128
Hans Verkuilb65814e2012-09-20 09:06:26 -03002129probe_subdev_out:
2130 /* free sub devices memory */
2131 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002132
Hans Verkuil5be452c2012-09-20 09:06:29 -03002133vpif_sd_error:
2134 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2135 ch = vpif_obj.dev[i];
2136 /* Note: does nothing if ch->video_dev == NULL */
2137 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002138 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002139vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002140 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002141
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002142 return err;
2143}
2144
2145/**
2146 * vpif_remove() - driver remove handler
2147 * @device: ptr to platform device structure
2148 *
2149 * The vidoe device is unregistered
2150 */
2151static int vpif_remove(struct platform_device *device)
2152{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002153 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002154 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002155 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002156
2157 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2158
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002159 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002160 /* un-register device */
2161 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2162 /* Get the pointer to the channel object */
2163 ch = vpif_obj.dev[i];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002164 common = &ch->common[i];
2165 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002166 /* Unregister video device */
2167 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002168 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002169 }
2170 return 0;
2171}
2172
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002173#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002174/**
2175 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002176 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002177static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002178{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002179
2180 struct common_obj *common;
2181 struct channel_obj *ch;
2182 int i;
2183
2184 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2185 /* Get the pointer to the channel object */
2186 ch = vpif_obj.dev[i];
2187 common = &ch->common[VPIF_VIDEO_INDEX];
2188 mutex_lock(&common->lock);
2189 if (ch->usrs && common->io_usrs) {
2190 /* Disable channel */
2191 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2192 enable_channel0(0);
2193 channel0_intr_enable(0);
2194 }
2195 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2196 common->started == 2) {
2197 enable_channel1(0);
2198 channel1_intr_enable(0);
2199 }
2200 }
2201 mutex_unlock(&common->lock);
2202 }
2203
2204 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002205}
2206
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002207/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002208 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002209 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002210static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002211{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002212 struct common_obj *common;
2213 struct channel_obj *ch;
2214 int i;
2215
2216 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2217 /* Get the pointer to the channel object */
2218 ch = vpif_obj.dev[i];
2219 common = &ch->common[VPIF_VIDEO_INDEX];
2220 mutex_lock(&common->lock);
2221 if (ch->usrs && common->io_usrs) {
2222 /* Disable channel */
2223 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2224 enable_channel0(1);
2225 channel0_intr_enable(1);
2226 }
2227 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2228 common->started == 2) {
2229 enable_channel1(1);
2230 channel1_intr_enable(1);
2231 }
2232 }
2233 mutex_unlock(&common->lock);
2234 }
2235
2236 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002237}
2238
Alexey Dobriyan47145212009-12-14 18:00:08 -08002239static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002240 .suspend = vpif_suspend,
2241 .resume = vpif_resume,
2242};
2243
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002244#define vpif_pm_ops (&vpif_dev_pm_ops)
2245#else
2246#define vpif_pm_ops NULL
2247#endif
2248
Mats Randgaardffa1b392010-08-30 10:30:36 -03002249static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002250 .driver = {
2251 .name = "vpif_capture",
2252 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002253 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002254 },
2255 .probe = vpif_probe,
2256 .remove = vpif_remove,
2257};
2258
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03002259module_platform_driver(vpif_driver);