blob: b035c88dd0a643424b4ef1e0ad70896c8f671fa2 [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
227static void vpif_wait_prepare(struct vb2_queue *vq)
228{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300229 struct channel_obj *ch = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300230 struct common_obj *common;
231
232 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300233 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300234}
235
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300236static void vpif_wait_finish(struct vb2_queue *vq)
237{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300238 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300239 struct common_obj *common;
240
241 common = &ch->common[VPIF_VIDEO_INDEX];
242 mutex_lock(&common->lock);
243}
244
245static int vpif_buffer_init(struct vb2_buffer *vb)
246{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300247 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300248
249 INIT_LIST_HEAD(&buf->list);
250
251 return 0;
252}
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300253
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300254static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
255{
256 struct vpif_capture_config *vpif_config_data =
257 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300258 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300259 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
260 struct vpif_params *vpif = &ch->vpifparams;
261 unsigned long addr = 0;
Hans Verkuilaec96832012-11-16 12:03:06 -0300262 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300263 int ret;
264
Hans Verkuilaec96832012-11-16 12:03:06 -0300265 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300266
267 /* Get the next frame from the buffer queue */
268 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
269 struct vpif_cap_buffer, list);
270 /* Remove buffer from the buffer queue */
271 list_del(&common->cur_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300272 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300273 /* Mark state of the current frame to active */
274 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
275 /* Initialize field_id and started member */
276 ch->field_id = 0;
277 common->started = 1;
278 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
279
280 /* Calculate the offset for Y and C data in the buffer */
281 vpif_calculate_offsets(ch);
282
283 if ((vpif->std_info.frm_fmt &&
284 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
285 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
286 (!vpif->std_info.frm_fmt &&
287 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
288 vpif_dbg(1, debug, "conflict in field format and std format\n");
289 return -EINVAL;
290 }
291
292 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300293 if (vpif_config_data->setup_input_channel_mode) {
294 ret = vpif_config_data->
295 setup_input_channel_mode(vpif->std_info.ycmux_mode);
296 if (ret < 0) {
297 vpif_dbg(1, debug, "can't set vpif channel mode\n");
298 return ret;
299 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300300 }
301
302 /* Call vpif_set_params function to set the parameters and addresses */
303 ret = vpif_set_video_params(vpif, ch->channel_id);
304
305 if (ret < 0) {
306 vpif_dbg(1, debug, "can't set video params\n");
307 return ret;
308 }
309
310 common->started = ret;
311 vpif_config_addr(ch, ret);
312
313 common->set_addr(addr + common->ytop_off,
314 addr + common->ybtm_off,
315 addr + common->ctop_off,
316 addr + common->cbtm_off);
317
318 /**
319 * Set interrupt for both the fields in VPIF Register enable channel in
320 * VPIF register
321 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300322 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300323 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
324 channel0_intr_assert();
325 channel0_intr_enable(1);
326 enable_channel0(1);
327 }
328 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
329 (common->started == 2)) {
330 channel1_intr_assert();
331 channel1_intr_enable(1);
332 enable_channel1(1);
333 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300334
335 return 0;
336}
337
338/* abort streaming and wait for last buffer */
Hans Verkuile37559b2014-04-17 02:47:21 -0300339static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300340{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300341 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300342 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300343 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300344
345 if (!vb2_is_streaming(vq))
Hans Verkuile37559b2014-04-17 02:47:21 -0300346 return;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300347
348 common = &ch->common[VPIF_VIDEO_INDEX];
349
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300350 /* Disable channel as per its device type and channel id */
351 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
352 enable_channel0(0);
353 channel0_intr_enable(0);
354 }
355 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
356 (2 == common->started)) {
357 enable_channel1(0);
358 channel1_intr_enable(0);
359 }
360 common->started = 0;
361
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300362 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300363 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300364 if (common->cur_frm == common->next_frm) {
365 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
366 } else {
367 if (common->cur_frm != NULL)
368 vb2_buffer_done(&common->cur_frm->vb,
369 VB2_BUF_STATE_ERROR);
370 if (common->next_frm != NULL)
371 vb2_buffer_done(&common->next_frm->vb,
372 VB2_BUF_STATE_ERROR);
373 }
374
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300375 while (!list_empty(&common->dma_queue)) {
376 common->next_frm = list_entry(common->dma_queue.next,
377 struct vpif_cap_buffer, list);
378 list_del(&common->next_frm->list);
379 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
380 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300381 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300382}
383
384static struct vb2_ops video_qops = {
385 .queue_setup = vpif_buffer_queue_setup,
386 .wait_prepare = vpif_wait_prepare,
387 .wait_finish = vpif_wait_finish,
388 .buf_init = vpif_buffer_init,
389 .buf_prepare = vpif_buffer_prepare,
390 .start_streaming = vpif_start_streaming,
391 .stop_streaming = vpif_stop_streaming,
392 .buf_cleanup = vpif_buf_cleanup,
393 .buf_queue = vpif_buffer_queue,
394};
395
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300396/**
397 * vpif_process_buffer_complete: process a completed buffer
398 * @common: ptr to common channel object
399 *
400 * This function time stamp the buffer and mark it as DONE. It also
401 * wake up any process waiting on the QUEUE and set the next buffer
402 * as current
403 */
404static void vpif_process_buffer_complete(struct common_obj *common)
405{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300406 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300407 vb2_buffer_done(&common->cur_frm->vb,
408 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300409 /* Make curFrm pointing to nextFrm */
410 common->cur_frm = common->next_frm;
411}
412
413/**
414 * vpif_schedule_next_buffer: set next buffer address for capture
415 * @common : ptr to common channel object
416 *
417 * This function will get next buffer from the dma queue and
418 * set the buffer address in the vpif register for capture.
419 * the buffer is marked active
420 */
421static void vpif_schedule_next_buffer(struct common_obj *common)
422{
423 unsigned long addr = 0;
424
Hans Verkuilaec96832012-11-16 12:03:06 -0300425 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300426 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300427 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300428 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300429 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300430 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300431 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
432 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300433
434 /* Set top and bottom field addresses in VPIF registers */
435 common->set_addr(addr + common->ytop_off,
436 addr + common->ybtm_off,
437 addr + common->ctop_off,
438 addr + common->cbtm_off);
439}
440
441/**
442 * vpif_channel_isr : ISR handler for vpif capture
443 * @irq: irq number
444 * @dev_id: dev_id ptr
445 *
446 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300447 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300448 */
449static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
450{
451 struct vpif_device *dev = &vpif_obj;
452 struct common_obj *common;
453 struct channel_obj *ch;
454 enum v4l2_field field;
455 int channel_id = 0;
456 int fid = -1, i;
457
458 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300459 if (!vpif_intr_status(channel_id))
460 return IRQ_NONE;
461
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300462 ch = dev->dev[channel_id];
463
464 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
465
466 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
467 common = &ch->common[i];
468 /* skip If streaming is not started in this channel */
469 if (0 == common->started)
470 continue;
471
472 /* Check the field format */
473 if (1 == ch->vpifparams.std_info.frm_fmt) {
474 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300475 spin_lock(&common->irqlock);
476 if (list_empty(&common->dma_queue)) {
477 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300478 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300479 }
480 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300481
482 if (!channel_first_int[i][channel_id])
483 vpif_process_buffer_complete(common);
484
485 channel_first_int[i][channel_id] = 0;
486
487 vpif_schedule_next_buffer(common);
488
489
490 channel_first_int[i][channel_id] = 0;
491 } else {
492 /**
493 * Interlaced mode. If it is first interrupt, ignore
494 * it
495 */
496 if (channel_first_int[i][channel_id]) {
497 channel_first_int[i][channel_id] = 0;
498 continue;
499 }
500 if (0 == i) {
501 ch->field_id ^= 1;
502 /* Get field id from VPIF registers */
503 fid = vpif_channel_getfid(ch->channel_id);
504 if (fid != ch->field_id) {
505 /**
506 * If field id does not match stored
507 * field id, make them in sync
508 */
509 if (0 == fid)
510 ch->field_id = fid;
511 return IRQ_HANDLED;
512 }
513 }
514 /* device field id and local field id are in sync */
515 if (0 == fid) {
516 /* this is even field */
517 if (common->cur_frm == common->next_frm)
518 continue;
519
520 /* mark the current buffer as done */
521 vpif_process_buffer_complete(common);
522 } else if (1 == fid) {
523 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300524 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300525 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300526 (common->cur_frm != common->next_frm)) {
527 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300528 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300529 }
530 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300531
532 vpif_schedule_next_buffer(common);
533 }
534 }
535 }
536 return IRQ_HANDLED;
537}
538
539/**
540 * vpif_update_std_info() - update standard related info
541 * @ch: ptr to channel object
542 *
543 * For a given standard selected by application, update values
544 * in the device data structures
545 */
546static int vpif_update_std_info(struct channel_obj *ch)
547{
548 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
549 struct vpif_params *vpifparams = &ch->vpifparams;
550 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300551 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300552 struct video_obj *vid_ch = &ch->video;
553 int index;
554
555 vpif_dbg(2, debug, "vpif_update_std_info\n");
556
Mats Randgaardaa444402010-12-16 12:17:42 -0300557 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300558 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300559 if (config->hd_sd == 0) {
560 vpif_dbg(2, debug, "SD format\n");
561 if (config->stdid & vid_ch->stdid) {
562 memcpy(std_info, config, sizeof(*config));
563 break;
564 }
565 } else {
566 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300567 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
568 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300569 memcpy(std_info, config, sizeof(*config));
570 break;
571 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300572 }
573 }
574
575 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300576 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300577 return -EINVAL;
578
579 common->fmt.fmt.pix.width = std_info->width;
580 common->width = std_info->width;
581 common->fmt.fmt.pix.height = std_info->height;
582 common->height = std_info->height;
583 common->fmt.fmt.pix.bytesperline = std_info->width;
584 vpifparams->video_params.hpitch = std_info->width;
585 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300586
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300587 return 0;
588}
589
590/**
591 * vpif_calculate_offsets : This function calculates buffers offsets
592 * @ch : ptr to channel object
593 *
594 * This function calculates buffer offsets for Y and C in the top and
595 * bottom field
596 */
597static void vpif_calculate_offsets(struct channel_obj *ch)
598{
599 unsigned int hpitch, vpitch, sizeimage;
600 struct video_obj *vid_ch = &(ch->video);
601 struct vpif_params *vpifparams = &ch->vpifparams;
602 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
603 enum v4l2_field field = common->fmt.fmt.pix.field;
604
605 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
606
607 if (V4L2_FIELD_ANY == field) {
608 if (vpifparams->std_info.frm_fmt)
609 vid_ch->buf_field = V4L2_FIELD_NONE;
610 else
611 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
612 } else
613 vid_ch->buf_field = common->fmt.fmt.pix.field;
614
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300615 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300616
617 hpitch = common->fmt.fmt.pix.bytesperline;
618 vpitch = sizeimage / (hpitch * 2);
619
620 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
621 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
622 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
623 common->ytop_off = 0;
624 common->ybtm_off = hpitch;
625 common->ctop_off = sizeimage / 2;
626 common->cbtm_off = sizeimage / 2 + hpitch;
627 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
628 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
629 common->ytop_off = 0;
630 common->ybtm_off = sizeimage / 4;
631 common->ctop_off = sizeimage / 2;
632 common->cbtm_off = common->ctop_off + sizeimage / 4;
633 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
634 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
635 common->ybtm_off = 0;
636 common->ytop_off = sizeimage / 4;
637 common->cbtm_off = sizeimage / 2;
638 common->ctop_off = common->cbtm_off + sizeimage / 4;
639 }
640 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
641 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
642 vpifparams->video_params.storage_mode = 1;
643 else
644 vpifparams->video_params.storage_mode = 0;
645
646 if (1 == vpifparams->std_info.frm_fmt)
647 vpifparams->video_params.hpitch =
648 common->fmt.fmt.pix.bytesperline;
649 else {
650 if ((field == V4L2_FIELD_ANY)
651 || (field == V4L2_FIELD_INTERLACED))
652 vpifparams->video_params.hpitch =
653 common->fmt.fmt.pix.bytesperline * 2;
654 else
655 vpifparams->video_params.hpitch =
656 common->fmt.fmt.pix.bytesperline;
657 }
658
659 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
660}
661
662/**
663 * vpif_config_format: configure default frame format in the device
664 * ch : ptr to channel object
665 */
666static void vpif_config_format(struct channel_obj *ch)
667{
668 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
669
670 vpif_dbg(2, debug, "vpif_config_format\n");
671
672 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
673 if (config_params.numbuffers[ch->channel_id] == 0)
674 common->memory = V4L2_MEMORY_USERPTR;
675 else
676 common->memory = V4L2_MEMORY_MMAP;
677
678 common->fmt.fmt.pix.sizeimage
679 = config_params.channel_bufsize[ch->channel_id];
680
681 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
682 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
683 else
684 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
685 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
686}
687
688/**
689 * vpif_get_default_field() - Get default field type based on interface
690 * @vpif_params - ptr to vpif params
691 */
692static inline enum v4l2_field vpif_get_default_field(
693 struct vpif_interface *iface)
694{
695 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
696 V4L2_FIELD_INTERLACED;
697}
698
699/**
700 * vpif_check_format() - check given pixel format for compatibility
701 * @ch - channel ptr
702 * @pixfmt - Given pixel format
703 * @update - update the values as per hardware requirement
704 *
705 * Check the application pixel format for S_FMT and update the input
706 * values as per hardware limits for TRY_FMT. The default pixel and
707 * field format is selected based on interface type.
708 */
709static int vpif_check_format(struct channel_obj *ch,
710 struct v4l2_pix_format *pixfmt,
711 int update)
712{
713 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
714 struct vpif_params *vpif_params = &ch->vpifparams;
715 enum v4l2_field field = pixfmt->field;
716 u32 sizeimage, hpitch, vpitch;
717 int ret = -EINVAL;
718
719 vpif_dbg(2, debug, "vpif_check_format\n");
720 /**
721 * first check for the pixel format. If if_type is Raw bayer,
722 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
723 * V4L2_PIX_FMT_YUV422P is supported
724 */
725 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
726 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
727 if (!update) {
728 vpif_dbg(2, debug, "invalid pix format\n");
729 goto exit;
730 }
731 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
732 }
733 } else {
734 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
735 if (!update) {
736 vpif_dbg(2, debug, "invalid pixel format\n");
737 goto exit;
738 }
739 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
740 }
741 }
742
743 if (!(VPIF_VALID_FIELD(field))) {
744 if (!update) {
745 vpif_dbg(2, debug, "invalid field format\n");
746 goto exit;
747 }
748 /**
749 * By default use FIELD_NONE for RAW Bayer capture
750 * and FIELD_INTERLACED for other interfaces
751 */
752 field = vpif_get_default_field(&vpif_params->iface);
753 } else if (field == V4L2_FIELD_ANY)
754 /* unsupported field. Use default */
755 field = vpif_get_default_field(&vpif_params->iface);
756
757 /* validate the hpitch */
758 hpitch = pixfmt->bytesperline;
759 if (hpitch < vpif_params->std_info.width) {
760 if (!update) {
761 vpif_dbg(2, debug, "invalid hpitch\n");
762 goto exit;
763 }
764 hpitch = vpif_params->std_info.width;
765 }
766
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300767 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300768
769 vpitch = sizeimage / (hpitch * 2);
770
771 /* validate the vpitch */
772 if (vpitch < vpif_params->std_info.height) {
773 if (!update) {
774 vpif_dbg(2, debug, "Invalid vpitch\n");
775 goto exit;
776 }
777 vpitch = vpif_params->std_info.height;
778 }
779
780 /* Check for 8 byte alignment */
781 if (!ALIGN(hpitch, 8)) {
782 if (!update) {
783 vpif_dbg(2, debug, "invalid pitch alignment\n");
784 goto exit;
785 }
786 /* adjust to next 8 byte boundary */
787 hpitch = (((hpitch + 7) / 8) * 8);
788 }
789 /* if update is set, modify the bytesperline and sizeimage */
790 if (update) {
791 pixfmt->bytesperline = hpitch;
792 pixfmt->sizeimage = hpitch * vpitch * 2;
793 }
794 /**
795 * Image width and height is always based on current standard width and
796 * height
797 */
798 pixfmt->width = common->fmt.fmt.pix.width;
799 pixfmt->height = common->fmt.fmt.pix.height;
800 return 0;
801exit:
802 return ret;
803}
804
805/**
806 * vpif_config_addr() - function to configure buffer address in vpif
807 * @ch - channel ptr
808 * @muxmode - channel mux mode
809 */
810static void vpif_config_addr(struct channel_obj *ch, int muxmode)
811{
812 struct common_obj *common;
813
814 vpif_dbg(2, debug, "vpif_config_addr\n");
815
816 common = &(ch->common[VPIF_VIDEO_INDEX]);
817
818 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
819 common->set_addr = ch1_set_videobuf_addr;
820 else if (2 == muxmode)
821 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
822 else
823 common->set_addr = ch0_set_videobuf_addr;
824}
825
826/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300827 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300828 * @filep: file pointer
829 * @vma: ptr to vm_area_struct
830 */
831static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
832{
833 /* Get the channel object and file handle object */
834 struct vpif_fh *fh = filep->private_data;
835 struct channel_obj *ch = fh->channel;
836 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300837 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300838
839 vpif_dbg(2, debug, "vpif_mmap\n");
840
Hans Verkuil72246792012-07-31 03:48:31 -0300841 if (mutex_lock_interruptible(&common->lock))
842 return -ERESTARTSYS;
843 ret = vb2_mmap(&common->buffer_queue, vma);
844 mutex_unlock(&common->lock);
845 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300846}
847
848/**
849 * vpif_poll: It is used for select/poll system call
850 * @filep: file pointer
851 * @wait: poll table to wait
852 */
853static unsigned int vpif_poll(struct file *filep, poll_table * wait)
854{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300855 struct vpif_fh *fh = filep->private_data;
856 struct channel_obj *channel = fh->channel;
857 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300858 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300859
860 vpif_dbg(2, debug, "vpif_poll\n");
861
Hans Verkuil72246792012-07-31 03:48:31 -0300862 if (common->started) {
863 mutex_lock(&common->lock);
864 res = vb2_poll(&common->buffer_queue, filep, wait);
865 mutex_unlock(&common->lock);
866 }
867 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300868}
869
870/**
871 * vpif_open : vpif open handler
872 * @filep: file ptr
873 *
874 * It creates object of file handle structure and stores it in private_data
875 * member of filepointer
876 */
877static int vpif_open(struct file *filep)
878{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300879 struct video_device *vdev = video_devdata(filep);
880 struct common_obj *common;
881 struct video_obj *vid_ch;
882 struct channel_obj *ch;
883 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300884
885 vpif_dbg(2, debug, "vpif_open\n");
886
887 ch = video_get_drvdata(vdev);
888
889 vid_ch = &ch->video;
890 common = &ch->common[VPIF_VIDEO_INDEX];
891
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300892 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300893 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300894 if (NULL == fh) {
895 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300896 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300897 }
898
Hans Verkuil72246792012-07-31 03:48:31 -0300899 if (mutex_lock_interruptible(&common->lock)) {
900 kfree(fh);
901 return -ERESTARTSYS;
902 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300903 /* store pointer to fh in private_data member of filep */
904 filep->private_data = fh;
905 fh->channel = ch;
906 fh->initialized = 0;
907 /* If decoder is not initialized. initialize it */
908 if (!ch->initialized) {
909 fh->initialized = 1;
910 ch->initialized = 1;
911 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
912 }
913 /* Increment channel usrs counter */
914 ch->usrs++;
915 /* Set io_allowed member to false */
916 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
917 /* Initialize priority of this instance to default priority */
918 fh->prio = V4L2_PRIORITY_UNSET;
919 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300920 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300921 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300922}
923
924/**
925 * vpif_release : function to clean up file close
926 * @filep: file pointer
927 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300928 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300929 * handle
930 */
931static int vpif_release(struct file *filep)
932{
933 struct vpif_fh *fh = filep->private_data;
934 struct channel_obj *ch = fh->channel;
935 struct common_obj *common;
936
937 vpif_dbg(2, debug, "vpif_release\n");
938
939 common = &ch->common[VPIF_VIDEO_INDEX];
940
Hans Verkuil72246792012-07-31 03:48:31 -0300941 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300942 /* if this instance is doing IO */
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300943 if (fh->io_allowed[VPIF_VIDEO_INDEX])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300944 /* Reset io_usrs member of channel object */
945 common->io_usrs = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300946
947 /* Decrement channel usrs counter */
948 ch->usrs--;
949
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300950 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300951 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300952
953 if (fh->initialized)
954 ch->initialized = 0;
955
Hans Verkuil72246792012-07-31 03:48:31 -0300956 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300957 filep->private_data = NULL;
958 kfree(fh);
959 return 0;
960}
961
962/**
963 * vpif_reqbufs() - request buffer handler
964 * @file: file ptr
965 * @priv: file handle
966 * @reqbuf: request buffer structure ptr
967 */
968static int vpif_reqbufs(struct file *file, void *priv,
969 struct v4l2_requestbuffers *reqbuf)
970{
971 struct vpif_fh *fh = priv;
972 struct channel_obj *ch = fh->channel;
973 struct common_obj *common;
974 u8 index = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300975
976 vpif_dbg(2, debug, "vpif_reqbufs\n");
977
978 /**
979 * This file handle has not initialized the channel,
980 * It is not allowed to do settings
981 */
982 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
983 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
984 if (!fh->initialized) {
985 vpif_dbg(1, debug, "Channel Busy\n");
986 return -EBUSY;
987 }
988 }
989
Manjunath Hadli764af392012-04-13 04:49:34 -0300990 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300991 return -EINVAL;
992
993 index = VPIF_VIDEO_INDEX;
994
995 common = &ch->common[index];
996
Hans Verkuil46656af2011-01-04 06:51:35 -0300997 if (0 != common->io_usrs)
998 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300999
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001000 /* Set io allowed member of file handle to TRUE */
1001 fh->io_allowed[index] = 1;
1002 /* Increment io usrs member of channel object to 1 */
1003 common->io_usrs = 1;
1004 /* Store type of memory requested in channel object */
1005 common->memory = reqbuf->memory;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001006
1007 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001008 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001009}
1010
1011/**
1012 * vpif_querybuf() - query buffer handler
1013 * @file: file ptr
1014 * @priv: file handle
1015 * @buf: v4l2 buffer structure ptr
1016 */
1017static int vpif_querybuf(struct file *file, void *priv,
1018 struct v4l2_buffer *buf)
1019{
1020 struct vpif_fh *fh = priv;
1021 struct channel_obj *ch = fh->channel;
1022 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1023
1024 vpif_dbg(2, debug, "vpif_querybuf\n");
1025
1026 if (common->fmt.type != buf->type)
1027 return -EINVAL;
1028
1029 if (common->memory != V4L2_MEMORY_MMAP) {
1030 vpif_dbg(1, debug, "Invalid memory\n");
1031 return -EINVAL;
1032 }
1033
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001034 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001035}
1036
1037/**
1038 * vpif_qbuf() - query buffer handler
1039 * @file: file ptr
1040 * @priv: file handle
1041 * @buf: v4l2 buffer structure ptr
1042 */
1043static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1044{
1045
1046 struct vpif_fh *fh = priv;
1047 struct channel_obj *ch = fh->channel;
1048 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1049 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001050
1051 vpif_dbg(2, debug, "vpif_qbuf\n");
1052
1053 if (common->fmt.type != tbuf.type) {
1054 vpif_err("invalid buffer type\n");
1055 return -EINVAL;
1056 }
1057
1058 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001059 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001060 return -EACCES;
1061 }
1062
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001063 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001064}
1065
1066/**
1067 * vpif_dqbuf() - query buffer handler
1068 * @file: file ptr
1069 * @priv: file handle
1070 * @buf: v4l2 buffer structure ptr
1071 */
1072static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1073{
1074 struct vpif_fh *fh = priv;
1075 struct channel_obj *ch = fh->channel;
1076 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1077
1078 vpif_dbg(2, debug, "vpif_dqbuf\n");
1079
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001080 return vb2_dqbuf(&common->buffer_queue, buf,
1081 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001082}
1083
1084/**
1085 * vpif_streamon() - streamon handler
1086 * @file: file ptr
1087 * @priv: file handle
1088 * @buftype: v4l2 buffer type
1089 */
1090static int vpif_streamon(struct file *file, void *priv,
1091 enum v4l2_buf_type buftype)
1092{
1093
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001094 struct vpif_fh *fh = priv;
1095 struct channel_obj *ch = fh->channel;
1096 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1097 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1098 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001099 int ret = 0;
1100
1101 vpif_dbg(2, debug, "vpif_streamon\n");
1102
1103 vpif = &ch->vpifparams;
1104
1105 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1106 vpif_dbg(1, debug, "buffer type not supported\n");
1107 return -EINVAL;
1108 }
1109
1110 /* If file handle is not allowed IO, return error */
1111 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1112 vpif_dbg(1, debug, "io not allowed\n");
1113 return -EACCES;
1114 }
1115
1116 /* If Streaming is already started, return error */
1117 if (common->started) {
1118 vpif_dbg(1, debug, "channel->started\n");
1119 return -EBUSY;
1120 }
1121
1122 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1123 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1124 vpif->std_info.ycmux_mode == 0) ||
1125 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1126 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1127 vpif_dbg(1, debug, "other channel is being used\n");
1128 return -EBUSY;
1129 }
1130
1131 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1132 if (ret)
1133 return ret;
1134
1135 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001136 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001137
Hans Verkuil178cce12012-09-20 09:06:30 -03001138 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001139 vpif_dbg(1, debug, "stream on failed in subdev\n");
1140 return ret;
1141 }
1142
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001143 /* Call vb2_streamon to start streaming in videobuf2 */
1144 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001145 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001146 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001147 return ret;
1148 }
1149
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001150 return ret;
1151}
1152
1153/**
1154 * vpif_streamoff() - streamoff handler
1155 * @file: file ptr
1156 * @priv: file handle
1157 * @buftype: v4l2 buffer type
1158 */
1159static int vpif_streamoff(struct file *file, void *priv,
1160 enum v4l2_buf_type buftype)
1161{
1162
1163 struct vpif_fh *fh = priv;
1164 struct channel_obj *ch = fh->channel;
1165 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1166 int ret;
1167
1168 vpif_dbg(2, debug, "vpif_streamoff\n");
1169
1170 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1171 vpif_dbg(1, debug, "buffer type not supported\n");
1172 return -EINVAL;
1173 }
1174
1175 /* If io is allowed for this file handle, return error */
1176 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1177 vpif_dbg(1, debug, "io not allowed\n");
1178 return -EACCES;
1179 }
1180
1181 /* If streaming is not started, return error */
1182 if (!common->started) {
1183 vpif_dbg(1, debug, "channel->started\n");
1184 return -EINVAL;
1185 }
1186
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001187 /* disable channel */
1188 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1189 enable_channel0(0);
1190 channel0_intr_enable(0);
1191 } else {
1192 enable_channel1(0);
1193 channel1_intr_enable(0);
1194 }
1195
1196 common->started = 0;
1197
Hans Verkuil178cce12012-09-20 09:06:30 -03001198 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001199
Hans Verkuil178cce12012-09-20 09:06:30 -03001200 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001201 vpif_dbg(1, debug, "stream off failed in subdev\n");
1202
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001203 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001204}
1205
1206/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001207 * vpif_input_to_subdev() - Maps input to sub device
1208 * @vpif_cfg - global config ptr
1209 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001210 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001211 *
1212 * lookup the sub device information for a given input index.
1213 * we report all the inputs to application. inputs table also
1214 * has sub device name for the each input
1215 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001216static int vpif_input_to_subdev(
1217 struct vpif_capture_config *vpif_cfg,
1218 struct vpif_capture_chan_config *chan_cfg,
1219 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001220{
Hans Verkuil178cce12012-09-20 09:06:30 -03001221 struct vpif_subdev_info *subdev_info;
1222 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001223 int i;
1224
Hans Verkuil178cce12012-09-20 09:06:30 -03001225 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001226
Hans Verkuil178cce12012-09-20 09:06:30 -03001227 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1228 if (subdev_name == NULL)
1229 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001230
1231 /* loop through the sub device list to get the sub device info */
1232 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1233 subdev_info = &vpif_cfg->subdev_info[i];
1234 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001235 return i;
1236 }
1237 return -1;
1238}
1239
1240/**
1241 * vpif_set_input() - Select an input
1242 * @vpif_cfg - global config ptr
1243 * @ch - channel
1244 * @_index - Given input index from application
1245 *
1246 * Select the given input.
1247 */
1248static int vpif_set_input(
1249 struct vpif_capture_config *vpif_cfg,
1250 struct channel_obj *ch,
1251 int index)
1252{
1253 struct vpif_capture_chan_config *chan_cfg =
1254 &vpif_cfg->chan_config[ch->channel_id];
1255 struct vpif_subdev_info *subdev_info = NULL;
1256 struct v4l2_subdev *sd = NULL;
1257 u32 input = 0, output = 0;
1258 int sd_index;
1259 int ret;
1260
1261 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1262 if (sd_index >= 0) {
1263 sd = vpif_obj.sd[sd_index];
1264 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001265 }
1266
Hans Verkuil178cce12012-09-20 09:06:30 -03001267 /* first setup input path from sub device to vpif */
1268 if (sd && vpif_cfg->setup_input_path) {
1269 ret = vpif_cfg->setup_input_path(ch->channel_id,
1270 subdev_info->name);
1271 if (ret < 0) {
1272 vpif_dbg(1, debug, "couldn't setup input path for the" \
1273 " sub device %s, for input index %d\n",
1274 subdev_info->name, index);
1275 return ret;
1276 }
1277 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001278
Hans Verkuil178cce12012-09-20 09:06:30 -03001279 if (sd) {
1280 input = chan_cfg->inputs[index].input_route;
1281 output = chan_cfg->inputs[index].output_route;
1282 ret = v4l2_subdev_call(sd, video, s_routing,
1283 input, output, 0);
1284 if (ret < 0 && ret != -ENOIOCTLCMD) {
1285 vpif_dbg(1, debug, "Failed to set input\n");
1286 return ret;
1287 }
1288 }
1289 ch->input_idx = index;
1290 ch->sd = sd;
1291 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001292 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001293
Hans Verkuil178cce12012-09-20 09:06:30 -03001294 /* update tvnorms from the sub device input info */
1295 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1296 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001297}
1298
1299/**
1300 * vpif_querystd() - querystd handler
1301 * @file: file ptr
1302 * @priv: file handle
1303 * @std_id: ptr to std id
1304 *
1305 * This function is called to detect standard at the selected input
1306 */
1307static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1308{
1309 struct vpif_fh *fh = priv;
1310 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001311 int ret = 0;
1312
1313 vpif_dbg(2, debug, "vpif_querystd\n");
1314
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001315 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001316 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001317
Hans Verkuil178cce12012-09-20 09:06:30 -03001318 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1319 return -ENODATA;
1320 if (ret) {
1321 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1322 return ret;
1323 }
1324
1325 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001326}
1327
1328/**
1329 * vpif_g_std() - get STD handler
1330 * @file: file ptr
1331 * @priv: file handle
1332 * @std_id: ptr to std id
1333 */
1334static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1335{
1336 struct vpif_fh *fh = priv;
1337 struct channel_obj *ch = fh->channel;
1338
1339 vpif_dbg(2, debug, "vpif_g_std\n");
1340
1341 *std = ch->video.stdid;
1342 return 0;
1343}
1344
1345/**
1346 * vpif_s_std() - set STD handler
1347 * @file: file ptr
1348 * @priv: file handle
1349 * @std_id: ptr to std id
1350 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001351static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001352{
1353 struct vpif_fh *fh = priv;
1354 struct channel_obj *ch = fh->channel;
1355 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1356 int ret = 0;
1357
1358 vpif_dbg(2, debug, "vpif_s_std\n");
1359
1360 if (common->started) {
1361 vpif_err("streaming in progress\n");
1362 return -EBUSY;
1363 }
1364
1365 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1366 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1367 if (!fh->initialized) {
1368 vpif_dbg(1, debug, "Channel Busy\n");
1369 return -EBUSY;
1370 }
1371 }
1372
Hans Verkuilffb48772010-05-01 08:03:24 -03001373 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001374 if (0 != ret)
1375 return ret;
1376
1377 fh->initialized = 1;
1378
1379 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001380 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001381 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001382
1383 /* Get the information about the standard */
1384 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001385 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001386 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001387 }
1388
1389 /* Configure the default format information */
1390 vpif_config_format(ch);
1391
1392 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001393 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001394 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001395 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001396 return ret;
1397 }
1398 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001399}
1400
1401/**
1402 * vpif_enum_input() - ENUMINPUT handler
1403 * @file: file ptr
1404 * @priv: file handle
1405 * @input: ptr to input structure
1406 */
1407static int vpif_enum_input(struct file *file, void *priv,
1408 struct v4l2_input *input)
1409{
1410
1411 struct vpif_capture_config *config = vpif_dev->platform_data;
1412 struct vpif_capture_chan_config *chan_cfg;
1413 struct vpif_fh *fh = priv;
1414 struct channel_obj *ch = fh->channel;
1415
1416 chan_cfg = &config->chan_config[ch->channel_id];
1417
1418 if (input->index >= chan_cfg->input_count) {
1419 vpif_dbg(1, debug, "Invalid input index\n");
1420 return -EINVAL;
1421 }
1422
1423 memcpy(input, &chan_cfg->inputs[input->index].input,
1424 sizeof(*input));
1425 return 0;
1426}
1427
1428/**
1429 * vpif_g_input() - Get INPUT handler
1430 * @file: file ptr
1431 * @priv: file handle
1432 * @index: ptr to input index
1433 */
1434static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1435{
1436 struct vpif_fh *fh = priv;
1437 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001438
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001439 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001440 return 0;
1441}
1442
1443/**
1444 * vpif_s_input() - Set INPUT handler
1445 * @file: file ptr
1446 * @priv: file handle
1447 * @index: input index
1448 */
1449static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1450{
1451 struct vpif_capture_config *config = vpif_dev->platform_data;
1452 struct vpif_capture_chan_config *chan_cfg;
1453 struct vpif_fh *fh = priv;
1454 struct channel_obj *ch = fh->channel;
1455 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001456 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001457
1458 chan_cfg = &config->chan_config[ch->channel_id];
1459
Hans Verkuil7aaad132012-09-20 09:06:25 -03001460 if (index >= chan_cfg->input_count)
1461 return -EINVAL;
1462
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001463 if (common->started) {
1464 vpif_err("Streaming in progress\n");
1465 return -EBUSY;
1466 }
1467
1468 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1469 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1470 if (!fh->initialized) {
1471 vpif_dbg(1, debug, "Channel Busy\n");
1472 return -EBUSY;
1473 }
1474 }
1475
Hans Verkuilffb48772010-05-01 08:03:24 -03001476 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001477 if (0 != ret)
1478 return ret;
1479
1480 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001481 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001482}
1483
1484/**
1485 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1486 * @file: file ptr
1487 * @priv: file handle
1488 * @index: input index
1489 */
1490static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1491 struct v4l2_fmtdesc *fmt)
1492{
1493 struct vpif_fh *fh = priv;
1494 struct channel_obj *ch = fh->channel;
1495
1496 if (fmt->index != 0) {
1497 vpif_dbg(1, debug, "Invalid format index\n");
1498 return -EINVAL;
1499 }
1500
1501 /* Fill in the information about format */
1502 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1503 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1504 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1505 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1506 } else {
1507 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1508 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1509 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1510 }
1511 return 0;
1512}
1513
1514/**
1515 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1516 * @file: file ptr
1517 * @priv: file handle
1518 * @fmt: ptr to v4l2 format structure
1519 */
1520static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1521 struct v4l2_format *fmt)
1522{
1523 struct vpif_fh *fh = priv;
1524 struct channel_obj *ch = fh->channel;
1525 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1526
1527 return vpif_check_format(ch, pixfmt, 1);
1528}
1529
1530
1531/**
1532 * vpif_g_fmt_vid_cap() - Set INPUT handler
1533 * @file: file ptr
1534 * @priv: file handle
1535 * @fmt: ptr to v4l2 format structure
1536 */
1537static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1538 struct v4l2_format *fmt)
1539{
1540 struct vpif_fh *fh = priv;
1541 struct channel_obj *ch = fh->channel;
1542 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1543
1544 /* Check the validity of the buffer type */
1545 if (common->fmt.type != fmt->type)
1546 return -EINVAL;
1547
1548 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001549 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001550 return 0;
1551}
1552
1553/**
1554 * vpif_s_fmt_vid_cap() - Set FMT handler
1555 * @file: file ptr
1556 * @priv: file handle
1557 * @fmt: ptr to v4l2 format structure
1558 */
1559static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1560 struct v4l2_format *fmt)
1561{
1562 struct vpif_fh *fh = priv;
1563 struct channel_obj *ch = fh->channel;
1564 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1565 struct v4l2_pix_format *pixfmt;
1566 int ret = 0;
1567
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001568 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001569
1570 /* If streaming is started, return error */
1571 if (common->started) {
1572 vpif_dbg(1, debug, "Streaming is started\n");
1573 return -EBUSY;
1574 }
1575
1576 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1577 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1578 if (!fh->initialized) {
1579 vpif_dbg(1, debug, "Channel Busy\n");
1580 return -EBUSY;
1581 }
1582 }
1583
Hans Verkuilffb48772010-05-01 08:03:24 -03001584 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001585 if (0 != ret)
1586 return ret;
1587
1588 fh->initialized = 1;
1589
1590 pixfmt = &fmt->fmt.pix;
1591 /* Check for valid field format */
1592 ret = vpif_check_format(ch, pixfmt, 0);
1593
1594 if (ret)
1595 return ret;
1596 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001597 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001598 return 0;
1599}
1600
1601/**
1602 * vpif_querycap() - QUERYCAP handler
1603 * @file: file ptr
1604 * @priv: file handle
1605 * @cap: ptr to v4l2_capability structure
1606 */
1607static int vpif_querycap(struct file *file, void *priv,
1608 struct v4l2_capability *cap)
1609{
1610 struct vpif_capture_config *config = vpif_dev->platform_data;
1611
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001612 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1613 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1614 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1615 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1616 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001617 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1618
1619 return 0;
1620}
1621
1622/**
1623 * vpif_g_priority() - get priority handler
1624 * @file: file ptr
1625 * @priv: file handle
1626 * @prio: ptr to v4l2_priority structure
1627 */
1628static int vpif_g_priority(struct file *file, void *priv,
1629 enum v4l2_priority *prio)
1630{
1631 struct vpif_fh *fh = priv;
1632 struct channel_obj *ch = fh->channel;
1633
1634 *prio = v4l2_prio_max(&ch->prio);
1635
1636 return 0;
1637}
1638
1639/**
1640 * vpif_s_priority() - set priority handler
1641 * @file: file ptr
1642 * @priv: file handle
1643 * @prio: ptr to v4l2_priority structure
1644 */
1645static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1646{
1647 struct vpif_fh *fh = priv;
1648 struct channel_obj *ch = fh->channel;
1649
1650 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1651}
1652
1653/**
1654 * vpif_cropcap() - cropcap handler
1655 * @file: file ptr
1656 * @priv: file handle
1657 * @crop: ptr to v4l2_cropcap structure
1658 */
1659static int vpif_cropcap(struct file *file, void *priv,
1660 struct v4l2_cropcap *crop)
1661{
1662 struct vpif_fh *fh = priv;
1663 struct channel_obj *ch = fh->channel;
1664 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1665
1666 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1667 return -EINVAL;
1668
1669 crop->bounds.left = 0;
1670 crop->bounds.top = 0;
1671 crop->bounds.height = common->height;
1672 crop->bounds.width = common->width;
1673 crop->defrect = crop->bounds;
1674 return 0;
1675}
1676
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001677/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001678 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001679 * @file: file ptr
1680 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001681 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001682 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001683static int
1684vpif_enum_dv_timings(struct file *file, void *priv,
1685 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001686{
1687 struct vpif_fh *fh = priv;
1688 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001689 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001690
Hans Verkuil178cce12012-09-20 09:06:30 -03001691 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001692 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001693 return -EINVAL;
1694 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001695}
1696
1697/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001698 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001699 * @file: file ptr
1700 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001701 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001702 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001703static int
1704vpif_query_dv_timings(struct file *file, void *priv,
1705 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001706{
1707 struct vpif_fh *fh = priv;
1708 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001709 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001710
Hans Verkuil178cce12012-09-20 09:06:30 -03001711 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001712 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001713 return -ENODATA;
1714 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001715}
1716
Mats Randgaardc027e162010-12-16 12:17:44 -03001717/**
1718 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1719 * @file: file ptr
1720 * @priv: file handle
1721 * @timings: digital video timings
1722 */
1723static int vpif_s_dv_timings(struct file *file, void *priv,
1724 struct v4l2_dv_timings *timings)
1725{
1726 struct vpif_fh *fh = priv;
1727 struct channel_obj *ch = fh->channel;
1728 struct vpif_params *vpifparams = &ch->vpifparams;
1729 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1730 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001731 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001732 int ret;
1733
1734 if (timings->type != V4L2_DV_BT_656_1120) {
1735 vpif_dbg(2, debug, "Timing type not defined\n");
1736 return -EINVAL;
1737 }
1738
1739 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001740 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1741 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1742 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001743 if (ret < 0) {
1744 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1745 return ret;
1746 }
1747
1748 if (!(timings->bt.width && timings->bt.height &&
1749 (timings->bt.hbackporch ||
1750 timings->bt.hfrontporch ||
1751 timings->bt.hsync) &&
1752 timings->bt.vfrontporch &&
1753 (timings->bt.vbackporch ||
1754 timings->bt.vsync))) {
1755 vpif_dbg(2, debug, "Timings for width, height, "
1756 "horizontal back porch, horizontal sync, "
1757 "horizontal front porch, vertical back porch, "
1758 "vertical sync and vertical back porch "
1759 "must be defined\n");
1760 return -EINVAL;
1761 }
1762
Hans Verkuil0598c172012-09-18 07:18:47 -03001763 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001764
1765 /* Configure video port timings */
1766
Hans Verkuile3655262013-07-29 08:41:00 -03001767 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001768 std_info->sav2eav = bt->width;
1769
1770 std_info->l1 = 1;
1771 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1772
Hans Verkuile3655262013-07-29 08:41:00 -03001773 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001774 if (bt->interlaced) {
1775 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001776 std_info->l5 = std_info->vsize/2 -
1777 (bt->vfrontporch - 1);
1778 std_info->l7 = std_info->vsize/2 + 1;
1779 std_info->l9 = std_info->l7 + bt->il_vsync +
1780 bt->il_vbackporch + 1;
1781 std_info->l11 = std_info->vsize -
1782 (bt->il_vfrontporch - 1);
1783 } else {
1784 vpif_dbg(2, debug, "Required timing values for "
1785 "interlaced BT format missing\n");
1786 return -EINVAL;
1787 }
1788 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001789 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1790 }
1791 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1792 std_info->width = bt->width;
1793 std_info->height = bt->height;
1794 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1795 std_info->ycmux_mode = 0;
1796 std_info->capture_format = 0;
1797 std_info->vbi_supported = 0;
1798 std_info->hd_sd = 1;
1799 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001800
1801 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001802 return 0;
1803}
1804
1805/**
1806 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1807 * @file: file ptr
1808 * @priv: file handle
1809 * @timings: digital video timings
1810 */
1811static int vpif_g_dv_timings(struct file *file, void *priv,
1812 struct v4l2_dv_timings *timings)
1813{
1814 struct vpif_fh *fh = priv;
1815 struct channel_obj *ch = fh->channel;
1816 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001817
Hans Verkuil0598c172012-09-18 07:18:47 -03001818 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001819
1820 return 0;
1821}
1822
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001823/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001824 * vpif_log_status() - Status information
1825 * @file: file ptr
1826 * @priv: file handle
1827 *
1828 * Returns zero.
1829 */
1830static int vpif_log_status(struct file *filep, void *priv)
1831{
1832 /* status for sub devices */
1833 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1834
1835 return 0;
1836}
1837
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001838/* vpif capture ioctl operations */
1839static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1840 .vidioc_querycap = vpif_querycap,
1841 .vidioc_g_priority = vpif_g_priority,
1842 .vidioc_s_priority = vpif_s_priority,
1843 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1844 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1845 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1846 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1847 .vidioc_enum_input = vpif_enum_input,
1848 .vidioc_s_input = vpif_s_input,
1849 .vidioc_g_input = vpif_g_input,
1850 .vidioc_reqbufs = vpif_reqbufs,
1851 .vidioc_querybuf = vpif_querybuf,
1852 .vidioc_querystd = vpif_querystd,
1853 .vidioc_s_std = vpif_s_std,
1854 .vidioc_g_std = vpif_g_std,
1855 .vidioc_qbuf = vpif_qbuf,
1856 .vidioc_dqbuf = vpif_dqbuf,
1857 .vidioc_streamon = vpif_streamon,
1858 .vidioc_streamoff = vpif_streamoff,
1859 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001860 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1861 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001862 .vidioc_s_dv_timings = vpif_s_dv_timings,
1863 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001864 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001865};
1866
1867/* vpif file operations */
1868static struct v4l2_file_operations vpif_fops = {
1869 .owner = THIS_MODULE,
1870 .open = vpif_open,
1871 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001872 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001873 .mmap = vpif_mmap,
1874 .poll = vpif_poll
1875};
1876
1877/* vpif video template */
1878static struct video_device vpif_video_template = {
1879 .name = "vpif",
1880 .fops = &vpif_fops,
1881 .minor = -1,
1882 .ioctl_ops = &vpif_ioctl_ops,
1883};
1884
1885/**
1886 * initialize_vpif() - Initialize vpif data structures
1887 *
1888 * Allocate memory for data structures and initialize them
1889 */
1890static int initialize_vpif(void)
1891{
1892 int err = 0, i, j;
1893 int free_channel_objects_index;
1894
1895 /* Default number of buffers should be 3 */
1896 if ((ch0_numbuffers > 0) &&
1897 (ch0_numbuffers < config_params.min_numbuffers))
1898 ch0_numbuffers = config_params.min_numbuffers;
1899 if ((ch1_numbuffers > 0) &&
1900 (ch1_numbuffers < config_params.min_numbuffers))
1901 ch1_numbuffers = config_params.min_numbuffers;
1902
1903 /* Set buffer size to min buffers size if it is invalid */
1904 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1905 ch0_bufsize =
1906 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1907 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1908 ch1_bufsize =
1909 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1910
1911 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1912 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1913 if (ch0_numbuffers) {
1914 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1915 = ch0_bufsize;
1916 }
1917 if (ch1_numbuffers) {
1918 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1919 = ch1_bufsize;
1920 }
1921
1922 /* Allocate memory for six channel objects */
1923 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1924 vpif_obj.dev[i] =
1925 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1926 /* If memory allocation fails, return error */
1927 if (!vpif_obj.dev[i]) {
1928 free_channel_objects_index = i;
1929 err = -ENOMEM;
1930 goto vpif_init_free_channel_objects;
1931 }
1932 }
1933 return 0;
1934
1935vpif_init_free_channel_objects:
1936 for (j = 0; j < free_channel_objects_index; j++)
1937 kfree(vpif_obj.dev[j]);
1938 return err;
1939}
1940
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001941static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1942 struct v4l2_subdev *subdev,
1943 struct v4l2_async_subdev *asd)
1944{
1945 int i;
1946
1947 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1948 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1949 subdev->name)) {
1950 vpif_obj.sd[i] = subdev;
1951 return 0;
1952 }
1953
1954 return -EINVAL;
1955}
1956
1957static int vpif_probe_complete(void)
1958{
1959 struct common_obj *common;
1960 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001961 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001962 int i, j, err, k;
1963
1964 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1965 ch = vpif_obj.dev[j];
1966 ch->channel_id = j;
1967 common = &(ch->common[VPIF_VIDEO_INDEX]);
1968 spin_lock_init(&common->irqlock);
1969 mutex_init(&common->lock);
1970 ch->video_dev->lock = &common->lock;
1971 /* Initialize prio member of channel object */
1972 v4l2_prio_init(&ch->prio);
1973 video_set_drvdata(ch->video_dev, ch);
1974
1975 /* select input 0 */
1976 err = vpif_set_input(vpif_obj.config, ch, 0);
1977 if (err)
1978 goto probe_out;
1979
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001980 /* Initialize vb2 queue */
1981 q = &common->buffer_queue;
1982 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1983 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1984 q->drv_priv = ch;
1985 q->ops = &video_qops;
1986 q->mem_ops = &vb2_dma_contig_memops;
1987 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1988 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1989 q->min_buffers_needed = 1;
1990
1991 err = vb2_queue_init(q);
1992 if (err) {
1993 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1994 goto probe_out;
1995 }
1996
1997 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1998 if (IS_ERR(common->alloc_ctx)) {
1999 vpif_err("Failed to get the context\n");
2000 err = PTR_ERR(common->alloc_ctx);
2001 goto probe_out;
2002 }
2003
2004 INIT_LIST_HEAD(&common->dma_queue);
2005
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002006 err = video_register_device(ch->video_dev,
2007 VFL_TYPE_GRABBER, (j ? 1 : 0));
2008 if (err)
2009 goto probe_out;
2010 }
2011
2012 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2013 return 0;
2014
2015probe_out:
2016 for (k = 0; k < j; k++) {
2017 /* Get the pointer to the channel object */
2018 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002019 common = &ch->common[k];
2020 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002021 /* Unregister video device */
2022 video_unregister_device(ch->video_dev);
2023 }
2024 kfree(vpif_obj.sd);
2025 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2026 ch = vpif_obj.dev[i];
2027 /* Note: does nothing if ch->video_dev == NULL */
2028 video_device_release(ch->video_dev);
2029 }
2030 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2031
2032 return err;
2033}
2034
2035static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2036{
2037 return vpif_probe_complete();
2038}
2039
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002040/**
2041 * vpif_probe : This function probes the vpif capture driver
2042 * @pdev: platform device pointer
2043 *
2044 * This creates device entries by register itself to the V4L2 driver and
2045 * initializes fields of each channel objects
2046 */
2047static __init int vpif_probe(struct platform_device *pdev)
2048{
2049 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002050 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002051 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002052 struct i2c_adapter *i2c_adap;
2053 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002054 struct video_device *vfd;
2055 struct resource *res;
2056 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03002057 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002058
2059 vpif_dev = &pdev->dev;
2060
2061 err = initialize_vpif();
2062 if (err) {
2063 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2064 return err;
2065 }
2066
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002067 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2068 if (err) {
2069 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2070 return err;
2071 }
2072
Hans Verkuil5be452c2012-09-20 09:06:29 -03002073 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03002074 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2075 IRQF_SHARED, "VPIF_Capture",
2076 (void *)(&vpif_obj.dev[res_idx]->
2077 channel_id));
2078 if (err) {
2079 err = -EINVAL;
2080 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002081 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002082 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002083 }
2084
2085 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2086 /* Get the pointer to the channel object */
2087 ch = vpif_obj.dev[i];
2088 /* Allocate memory for video device */
2089 vfd = video_device_alloc();
2090 if (NULL == vfd) {
2091 for (j = 0; j < i; j++) {
2092 ch = vpif_obj.dev[j];
2093 video_device_release(ch->video_dev);
2094 }
2095 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002096 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002097 }
2098
2099 /* Initialize field of video device */
2100 *vfd = vpif_video_template;
2101 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2102 vfd->release = video_device_release;
2103 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002104 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002105 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002106 /* Set video_dev to the video device */
2107 ch->video_dev = vfd;
2108 }
2109
Manjunath Hadli764af392012-04-13 04:49:34 -03002110 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2111 if (res) {
2112 size = resource_size(res);
2113 /* The resources are divided into two equal memory and when we
2114 * have HD output we can add them together
2115 */
2116 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2117 ch = vpif_obj.dev[j];
2118 ch->channel_id = j;
2119 /* only enabled if second resource exists */
2120 config_params.video_limit[ch->channel_id] = 0;
2121 if (size)
2122 config_params.video_limit[ch->channel_id] =
2123 size/2;
2124 }
2125 }
2126
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002127 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002128
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002129 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002130 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002131 GFP_KERNEL);
2132 if (vpif_obj.sd == NULL) {
2133 vpif_err("unable to allocate memory for subdevice pointers\n");
2134 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002135 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002136 }
2137
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002138 if (!vpif_obj.config->asd_sizes) {
2139 i2c_adap = i2c_get_adapter(1);
2140 for (i = 0; i < subdev_count; i++) {
2141 subdevdata = &vpif_obj.config->subdev_info[i];
2142 vpif_obj.sd[i] =
2143 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2144 i2c_adap,
2145 &subdevdata->
2146 board_info,
2147 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002148
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002149 if (!vpif_obj.sd[i]) {
2150 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03002151 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002152 goto probe_subdev_out;
2153 }
2154 v4l2_info(&vpif_obj.v4l2_dev,
2155 "registered sub device %s\n",
2156 subdevdata->name);
2157 }
2158 vpif_probe_complete();
2159 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03002160 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002161 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2162 vpif_obj.notifier.bound = vpif_async_bound;
2163 vpif_obj.notifier.complete = vpif_async_complete;
2164 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2165 &vpif_obj.notifier);
2166 if (err) {
2167 vpif_err("Error registering async notifier\n");
2168 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002169 goto probe_subdev_out;
2170 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002171 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002172
2173 return 0;
2174
Hans Verkuilb65814e2012-09-20 09:06:26 -03002175probe_subdev_out:
2176 /* free sub devices memory */
2177 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002178
Hans Verkuil5be452c2012-09-20 09:06:29 -03002179vpif_sd_error:
2180 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2181 ch = vpif_obj.dev[i];
2182 /* Note: does nothing if ch->video_dev == NULL */
2183 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002184 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002185vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002186 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002187
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002188 return err;
2189}
2190
2191/**
2192 * vpif_remove() - driver remove handler
2193 * @device: ptr to platform device structure
2194 *
2195 * The vidoe device is unregistered
2196 */
2197static int vpif_remove(struct platform_device *device)
2198{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002199 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002200 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002201 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002202
2203 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2204
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002205 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002206 /* un-register device */
2207 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2208 /* Get the pointer to the channel object */
2209 ch = vpif_obj.dev[i];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002210 common = &ch->common[i];
2211 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002212 /* Unregister video device */
2213 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002214 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002215 }
2216 return 0;
2217}
2218
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002219#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002220/**
2221 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002222 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002223static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002224{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002225
2226 struct common_obj *common;
2227 struct channel_obj *ch;
2228 int i;
2229
2230 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2231 /* Get the pointer to the channel object */
2232 ch = vpif_obj.dev[i];
2233 common = &ch->common[VPIF_VIDEO_INDEX];
2234 mutex_lock(&common->lock);
2235 if (ch->usrs && common->io_usrs) {
2236 /* Disable channel */
2237 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2238 enable_channel0(0);
2239 channel0_intr_enable(0);
2240 }
2241 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2242 common->started == 2) {
2243 enable_channel1(0);
2244 channel1_intr_enable(0);
2245 }
2246 }
2247 mutex_unlock(&common->lock);
2248 }
2249
2250 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002251}
2252
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002253/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002254 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002255 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002256static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002257{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002258 struct common_obj *common;
2259 struct channel_obj *ch;
2260 int i;
2261
2262 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2263 /* Get the pointer to the channel object */
2264 ch = vpif_obj.dev[i];
2265 common = &ch->common[VPIF_VIDEO_INDEX];
2266 mutex_lock(&common->lock);
2267 if (ch->usrs && common->io_usrs) {
2268 /* Disable channel */
2269 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2270 enable_channel0(1);
2271 channel0_intr_enable(1);
2272 }
2273 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2274 common->started == 2) {
2275 enable_channel1(1);
2276 channel1_intr_enable(1);
2277 }
2278 }
2279 mutex_unlock(&common->lock);
2280 }
2281
2282 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002283}
2284
Alexey Dobriyan47145212009-12-14 18:00:08 -08002285static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002286 .suspend = vpif_suspend,
2287 .resume = vpif_resume,
2288};
2289
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002290#define vpif_pm_ops (&vpif_dev_pm_ops)
2291#else
2292#define vpif_pm_ops NULL
2293#endif
2294
Mats Randgaardffa1b392010-08-30 10:30:36 -03002295static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002296 .driver = {
2297 .name = "vpif_capture",
2298 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002299 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002300 },
2301 .probe = vpif_probe,
2302 .remove = vpif_remove,
2303};
2304
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03002305module_platform_driver(vpif_driver);