blob: fd384d0b0c475a80b21c5e93c2389009d143258f [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
20 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030021
Lad, Prabhakar012eef72013-04-19 05:53:29 -030022#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26
Lad, Prabhakar012eef72013-04-19 05:53:29 -030027#include <media/v4l2-ioctl.h>
28
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030029#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030030#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030031
32MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
33MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030034MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030035
36#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
37#define vpif_dbg(level, debug, fmt, arg...) \
38 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
39
40static int debug = 1;
41static u32 ch0_numbuffers = 3;
42static u32 ch1_numbuffers = 3;
43static u32 ch0_bufsize = 1920 * 1080 * 2;
44static u32 ch1_bufsize = 720 * 576 * 2;
45
46module_param(debug, int, 0644);
47module_param(ch0_numbuffers, uint, S_IRUGO);
48module_param(ch1_numbuffers, uint, S_IRUGO);
49module_param(ch0_bufsize, uint, S_IRUGO);
50module_param(ch1_bufsize, uint, S_IRUGO);
51
52MODULE_PARM_DESC(debug, "Debug level 0-1");
53MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
54MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
55MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
56MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
57
58static struct vpif_config_params config_params = {
59 .min_numbuffers = 3,
60 .numbuffers[0] = 3,
61 .numbuffers[1] = 3,
62 .min_bufsize[0] = 720 * 480 * 2,
63 .min_bufsize[1] = 720 * 480 * 2,
64 .channel_bufsize[0] = 1920 * 1080 * 2,
65 .channel_bufsize[1] = 720 * 576 * 2,
66};
67
68/* global variables */
69static struct vpif_device vpif_obj = { {NULL} };
70static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030071static void vpif_calculate_offsets(struct channel_obj *ch);
72static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030073
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030074static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
75
76static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
77{
78 return container_of(vb, struct vpif_cap_buffer, vb);
79}
80
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030081/**
82 * buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030083 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030084 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030085 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030086 * function is called. The buffer is prepared and user space virtual address
87 * or user address is converted into physical address
88 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030089static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030090{
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030091 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030092 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030093 struct common_obj *common;
94 unsigned long addr;
95
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030096 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
97
98 common = &ch->common[VPIF_VIDEO_INDEX];
99
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300100 if (vb->state != VB2_BUF_STATE_ACTIVE &&
101 vb->state != VB2_BUF_STATE_PREPARED) {
102 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
103 if (vb2_plane_vaddr(vb, 0) &&
104 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
105 goto exit;
106 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300107
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300108 if (q->streaming) {
109 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
110 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
111 !IS_ALIGNED((addr + common->ctop_off), 8) ||
112 !IS_ALIGNED((addr + common->cbtm_off), 8))
113 goto exit;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300114 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300115 }
116 return 0;
117exit:
118 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
119 return -EINVAL;
120}
121
122/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300123 * vpif_buffer_queue_setup : Callback function for buffer setup.
124 * @vq: vb2_queue ptr
125 * @fmt: v4l2 format
126 * @nbuffers: ptr to number of buffers requested by application
127 * @nplanes:: contains number of distinct video planes needed to hold a frame
128 * @sizes[]: contains the size (in bytes) of each plane.
129 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300130 *
131 * This callback function is called when reqbuf() is called to adjust
132 * the buffer count and buffer size
133 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300134static int vpif_buffer_queue_setup(struct vb2_queue *vq,
135 const struct v4l2_format *fmt,
136 unsigned int *nbuffers, unsigned int *nplanes,
137 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300138{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300139 struct channel_obj *ch = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300140 struct common_obj *common;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300141 unsigned long size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300142
143 common = &ch->common[VPIF_VIDEO_INDEX];
144
145 vpif_dbg(2, debug, "vpif_buffer_setup\n");
146
147 /* If memory type is not mmap, return */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300148 if (V4L2_MEMORY_MMAP == common->memory) {
149 /* Calculate the size of the buffer */
150 size = config_params.channel_bufsize[ch->channel_id];
151 /*
152 * Checking if the buffer size exceeds the available buffer
153 * ycmux_mode = 0 means 1 channel mode HD and
154 * ycmux_mode = 1 means 2 channels mode SD
155 */
156 if (ch->vpifparams.std_info.ycmux_mode == 0) {
157 if (config_params.video_limit[ch->channel_id])
158 while (size * *nbuffers >
159 (config_params.video_limit[0]
Manjunath Hadli764af392012-04-13 04:49:34 -0300160 + config_params.video_limit[1]))
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300161 (*nbuffers)--;
162 } else {
163 if (config_params.video_limit[ch->channel_id])
164 while (size * *nbuffers >
Manjunath Hadli764af392012-04-13 04:49:34 -0300165 config_params.video_limit[ch->channel_id])
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300166 (*nbuffers)--;
167 }
168
169 } else {
170 size = common->fmt.fmt.pix.sizeimage;
Manjunath Hadli764af392012-04-13 04:49:34 -0300171 }
172
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300173 if (*nbuffers < config_params.min_numbuffers)
174 *nbuffers = config_params.min_numbuffers;
175
176 *nplanes = 1;
177 sizes[0] = size;
178 alloc_ctxs[0] = common->alloc_ctx;
179
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300180 return 0;
181}
182
183/**
184 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300185 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300186 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300187static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300188{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300189 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
190 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300191 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300192 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300193
194 common = &ch->common[VPIF_VIDEO_INDEX];
195
196 vpif_dbg(2, debug, "vpif_buffer_queue\n");
197
Hans Verkuilaec96832012-11-16 12:03:06 -0300198 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300199 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300200 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300201 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300202}
203
204/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300205 * vpif_buf_cleanup : Callback function to free buffer
206 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300207 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300208 * This function is called from the videobuf2 layer to free memory
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300209 * allocated to the buffers
210 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300211static void vpif_buf_cleanup(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300212{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300213 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
214 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300215 struct common_obj *common;
216 unsigned long flags;
217
218 common = &ch->common[VPIF_VIDEO_INDEX];
219
220 spin_lock_irqsave(&common->irqlock, flags);
221 if (vb->state == VB2_BUF_STATE_ACTIVE)
222 list_del_init(&buf->list);
223 spin_unlock_irqrestore(&common->irqlock, flags);
224
225}
226
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300227static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
228{
229 struct vpif_capture_config *vpif_config_data =
230 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300231 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300232 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
233 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300234 struct vpif_cap_buffer *buf, *tmp;
235 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300236 int ret;
237
Hans Verkuilaec96832012-11-16 12:03:06 -0300238 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300239
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300240 /* Initialize field_id and started member */
241 ch->field_id = 0;
242 common->started = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300243
244 /* Calculate the offset for Y and C data in the buffer */
245 vpif_calculate_offsets(ch);
246
247 if ((vpif->std_info.frm_fmt &&
248 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
249 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
250 (!vpif->std_info.frm_fmt &&
251 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
252 vpif_dbg(1, debug, "conflict in field format and std format\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300253 ret = -EINVAL;
254 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300255 }
256
257 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300258 if (vpif_config_data->setup_input_channel_mode) {
259 ret = vpif_config_data->
260 setup_input_channel_mode(vpif->std_info.ycmux_mode);
261 if (ret < 0) {
262 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300263 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300264 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300265 }
266
267 /* Call vpif_set_params function to set the parameters and addresses */
268 ret = vpif_set_video_params(vpif, ch->channel_id);
269
270 if (ret < 0) {
271 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300272 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300273 }
274
275 common->started = ret;
276 vpif_config_addr(ch, ret);
277
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300278 /* Get the next frame from the buffer queue */
279 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
280 struct vpif_cap_buffer, list);
281 /* Remove buffer from the buffer queue */
282 list_del(&common->cur_frm->list);
283 spin_unlock_irqrestore(&common->irqlock, flags);
284 /* Mark state of the current frame to active */
285 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
286
287 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
288
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300289 common->set_addr(addr + common->ytop_off,
290 addr + common->ybtm_off,
291 addr + common->ctop_off,
292 addr + common->cbtm_off);
293
294 /**
295 * Set interrupt for both the fields in VPIF Register enable channel in
296 * VPIF register
297 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300298 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300299 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
300 channel0_intr_assert();
301 channel0_intr_enable(1);
302 enable_channel0(1);
303 }
304 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
305 (common->started == 2)) {
306 channel1_intr_assert();
307 channel1_intr_enable(1);
308 enable_channel1(1);
309 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300310
311 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300312
313err:
314 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
315 list_del(&buf->list);
316 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
317 }
318
319 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300320}
321
322/* abort streaming and wait for last buffer */
Hans Verkuile37559b2014-04-17 02:47:21 -0300323static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300324{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300325 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300326 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300327 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300328
329 if (!vb2_is_streaming(vq))
Hans Verkuile37559b2014-04-17 02:47:21 -0300330 return;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300331
332 common = &ch->common[VPIF_VIDEO_INDEX];
333
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300334 /* Disable channel as per its device type and channel id */
335 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
336 enable_channel0(0);
337 channel0_intr_enable(0);
338 }
339 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
340 (2 == common->started)) {
341 enable_channel1(0);
342 channel1_intr_enable(0);
343 }
344 common->started = 0;
345
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300346 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300347 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300348 if (common->cur_frm == common->next_frm) {
349 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
350 } else {
351 if (common->cur_frm != NULL)
352 vb2_buffer_done(&common->cur_frm->vb,
353 VB2_BUF_STATE_ERROR);
354 if (common->next_frm != NULL)
355 vb2_buffer_done(&common->next_frm->vb,
356 VB2_BUF_STATE_ERROR);
357 }
358
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300359 while (!list_empty(&common->dma_queue)) {
360 common->next_frm = list_entry(common->dma_queue.next,
361 struct vpif_cap_buffer, list);
362 list_del(&common->next_frm->list);
363 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
364 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300365 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300366}
367
368static struct vb2_ops video_qops = {
369 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300370 .buf_prepare = vpif_buffer_prepare,
371 .start_streaming = vpif_start_streaming,
372 .stop_streaming = vpif_stop_streaming,
373 .buf_cleanup = vpif_buf_cleanup,
374 .buf_queue = vpif_buffer_queue,
375};
376
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300377/**
378 * vpif_process_buffer_complete: process a completed buffer
379 * @common: ptr to common channel object
380 *
381 * This function time stamp the buffer and mark it as DONE. It also
382 * wake up any process waiting on the QUEUE and set the next buffer
383 * as current
384 */
385static void vpif_process_buffer_complete(struct common_obj *common)
386{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300387 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300388 vb2_buffer_done(&common->cur_frm->vb,
389 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300390 /* Make curFrm pointing to nextFrm */
391 common->cur_frm = common->next_frm;
392}
393
394/**
395 * vpif_schedule_next_buffer: set next buffer address for capture
396 * @common : ptr to common channel object
397 *
398 * This function will get next buffer from the dma queue and
399 * set the buffer address in the vpif register for capture.
400 * the buffer is marked active
401 */
402static void vpif_schedule_next_buffer(struct common_obj *common)
403{
404 unsigned long addr = 0;
405
Hans Verkuilaec96832012-11-16 12:03:06 -0300406 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300407 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300408 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300409 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300410 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300411 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300412 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
413 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300414
415 /* Set top and bottom field addresses in VPIF registers */
416 common->set_addr(addr + common->ytop_off,
417 addr + common->ybtm_off,
418 addr + common->ctop_off,
419 addr + common->cbtm_off);
420}
421
422/**
423 * vpif_channel_isr : ISR handler for vpif capture
424 * @irq: irq number
425 * @dev_id: dev_id ptr
426 *
427 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300428 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300429 */
430static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
431{
432 struct vpif_device *dev = &vpif_obj;
433 struct common_obj *common;
434 struct channel_obj *ch;
435 enum v4l2_field field;
436 int channel_id = 0;
437 int fid = -1, i;
438
439 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300440 if (!vpif_intr_status(channel_id))
441 return IRQ_NONE;
442
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300443 ch = dev->dev[channel_id];
444
445 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
446
447 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
448 common = &ch->common[i];
449 /* skip If streaming is not started in this channel */
450 if (0 == common->started)
451 continue;
452
453 /* Check the field format */
454 if (1 == ch->vpifparams.std_info.frm_fmt) {
455 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300456 spin_lock(&common->irqlock);
457 if (list_empty(&common->dma_queue)) {
458 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300459 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300460 }
461 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300462
463 if (!channel_first_int[i][channel_id])
464 vpif_process_buffer_complete(common);
465
466 channel_first_int[i][channel_id] = 0;
467
468 vpif_schedule_next_buffer(common);
469
470
471 channel_first_int[i][channel_id] = 0;
472 } else {
473 /**
474 * Interlaced mode. If it is first interrupt, ignore
475 * it
476 */
477 if (channel_first_int[i][channel_id]) {
478 channel_first_int[i][channel_id] = 0;
479 continue;
480 }
481 if (0 == i) {
482 ch->field_id ^= 1;
483 /* Get field id from VPIF registers */
484 fid = vpif_channel_getfid(ch->channel_id);
485 if (fid != ch->field_id) {
486 /**
487 * If field id does not match stored
488 * field id, make them in sync
489 */
490 if (0 == fid)
491 ch->field_id = fid;
492 return IRQ_HANDLED;
493 }
494 }
495 /* device field id and local field id are in sync */
496 if (0 == fid) {
497 /* this is even field */
498 if (common->cur_frm == common->next_frm)
499 continue;
500
501 /* mark the current buffer as done */
502 vpif_process_buffer_complete(common);
503 } else if (1 == fid) {
504 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300505 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300506 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300507 (common->cur_frm != common->next_frm)) {
508 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300509 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300510 }
511 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300512
513 vpif_schedule_next_buffer(common);
514 }
515 }
516 }
517 return IRQ_HANDLED;
518}
519
520/**
521 * vpif_update_std_info() - update standard related info
522 * @ch: ptr to channel object
523 *
524 * For a given standard selected by application, update values
525 * in the device data structures
526 */
527static int vpif_update_std_info(struct channel_obj *ch)
528{
529 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
530 struct vpif_params *vpifparams = &ch->vpifparams;
531 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300532 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300533 struct video_obj *vid_ch = &ch->video;
534 int index;
535
536 vpif_dbg(2, debug, "vpif_update_std_info\n");
537
Mats Randgaardaa444402010-12-16 12:17:42 -0300538 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300539 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300540 if (config->hd_sd == 0) {
541 vpif_dbg(2, debug, "SD format\n");
542 if (config->stdid & vid_ch->stdid) {
543 memcpy(std_info, config, sizeof(*config));
544 break;
545 }
546 } else {
547 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300548 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
549 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300550 memcpy(std_info, config, sizeof(*config));
551 break;
552 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300553 }
554 }
555
556 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300557 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300558 return -EINVAL;
559
560 common->fmt.fmt.pix.width = std_info->width;
561 common->width = std_info->width;
562 common->fmt.fmt.pix.height = std_info->height;
563 common->height = std_info->height;
564 common->fmt.fmt.pix.bytesperline = std_info->width;
565 vpifparams->video_params.hpitch = std_info->width;
566 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300567
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300568 return 0;
569}
570
571/**
572 * vpif_calculate_offsets : This function calculates buffers offsets
573 * @ch : ptr to channel object
574 *
575 * This function calculates buffer offsets for Y and C in the top and
576 * bottom field
577 */
578static void vpif_calculate_offsets(struct channel_obj *ch)
579{
580 unsigned int hpitch, vpitch, sizeimage;
581 struct video_obj *vid_ch = &(ch->video);
582 struct vpif_params *vpifparams = &ch->vpifparams;
583 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
584 enum v4l2_field field = common->fmt.fmt.pix.field;
585
586 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
587
588 if (V4L2_FIELD_ANY == field) {
589 if (vpifparams->std_info.frm_fmt)
590 vid_ch->buf_field = V4L2_FIELD_NONE;
591 else
592 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
593 } else
594 vid_ch->buf_field = common->fmt.fmt.pix.field;
595
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300596 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300597
598 hpitch = common->fmt.fmt.pix.bytesperline;
599 vpitch = sizeimage / (hpitch * 2);
600
601 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
602 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
603 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
604 common->ytop_off = 0;
605 common->ybtm_off = hpitch;
606 common->ctop_off = sizeimage / 2;
607 common->cbtm_off = sizeimage / 2 + hpitch;
608 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
609 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
610 common->ytop_off = 0;
611 common->ybtm_off = sizeimage / 4;
612 common->ctop_off = sizeimage / 2;
613 common->cbtm_off = common->ctop_off + sizeimage / 4;
614 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
615 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
616 common->ybtm_off = 0;
617 common->ytop_off = sizeimage / 4;
618 common->cbtm_off = sizeimage / 2;
619 common->ctop_off = common->cbtm_off + sizeimage / 4;
620 }
621 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
622 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
623 vpifparams->video_params.storage_mode = 1;
624 else
625 vpifparams->video_params.storage_mode = 0;
626
627 if (1 == vpifparams->std_info.frm_fmt)
628 vpifparams->video_params.hpitch =
629 common->fmt.fmt.pix.bytesperline;
630 else {
631 if ((field == V4L2_FIELD_ANY)
632 || (field == V4L2_FIELD_INTERLACED))
633 vpifparams->video_params.hpitch =
634 common->fmt.fmt.pix.bytesperline * 2;
635 else
636 vpifparams->video_params.hpitch =
637 common->fmt.fmt.pix.bytesperline;
638 }
639
640 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
641}
642
643/**
644 * vpif_config_format: configure default frame format in the device
645 * ch : ptr to channel object
646 */
647static void vpif_config_format(struct channel_obj *ch)
648{
649 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
650
651 vpif_dbg(2, debug, "vpif_config_format\n");
652
653 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
654 if (config_params.numbuffers[ch->channel_id] == 0)
655 common->memory = V4L2_MEMORY_USERPTR;
656 else
657 common->memory = V4L2_MEMORY_MMAP;
658
659 common->fmt.fmt.pix.sizeimage
660 = config_params.channel_bufsize[ch->channel_id];
661
662 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
663 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
664 else
665 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
666 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
667}
668
669/**
670 * vpif_get_default_field() - Get default field type based on interface
671 * @vpif_params - ptr to vpif params
672 */
673static inline enum v4l2_field vpif_get_default_field(
674 struct vpif_interface *iface)
675{
676 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
677 V4L2_FIELD_INTERLACED;
678}
679
680/**
681 * vpif_check_format() - check given pixel format for compatibility
682 * @ch - channel ptr
683 * @pixfmt - Given pixel format
684 * @update - update the values as per hardware requirement
685 *
686 * Check the application pixel format for S_FMT and update the input
687 * values as per hardware limits for TRY_FMT. The default pixel and
688 * field format is selected based on interface type.
689 */
690static int vpif_check_format(struct channel_obj *ch,
691 struct v4l2_pix_format *pixfmt,
692 int update)
693{
694 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
695 struct vpif_params *vpif_params = &ch->vpifparams;
696 enum v4l2_field field = pixfmt->field;
697 u32 sizeimage, hpitch, vpitch;
698 int ret = -EINVAL;
699
700 vpif_dbg(2, debug, "vpif_check_format\n");
701 /**
702 * first check for the pixel format. If if_type is Raw bayer,
703 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
704 * V4L2_PIX_FMT_YUV422P is supported
705 */
706 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
707 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
708 if (!update) {
709 vpif_dbg(2, debug, "invalid pix format\n");
710 goto exit;
711 }
712 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
713 }
714 } else {
715 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
716 if (!update) {
717 vpif_dbg(2, debug, "invalid pixel format\n");
718 goto exit;
719 }
720 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
721 }
722 }
723
724 if (!(VPIF_VALID_FIELD(field))) {
725 if (!update) {
726 vpif_dbg(2, debug, "invalid field format\n");
727 goto exit;
728 }
729 /**
730 * By default use FIELD_NONE for RAW Bayer capture
731 * and FIELD_INTERLACED for other interfaces
732 */
733 field = vpif_get_default_field(&vpif_params->iface);
734 } else if (field == V4L2_FIELD_ANY)
735 /* unsupported field. Use default */
736 field = vpif_get_default_field(&vpif_params->iface);
737
738 /* validate the hpitch */
739 hpitch = pixfmt->bytesperline;
740 if (hpitch < vpif_params->std_info.width) {
741 if (!update) {
742 vpif_dbg(2, debug, "invalid hpitch\n");
743 goto exit;
744 }
745 hpitch = vpif_params->std_info.width;
746 }
747
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300748 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300749
750 vpitch = sizeimage / (hpitch * 2);
751
752 /* validate the vpitch */
753 if (vpitch < vpif_params->std_info.height) {
754 if (!update) {
755 vpif_dbg(2, debug, "Invalid vpitch\n");
756 goto exit;
757 }
758 vpitch = vpif_params->std_info.height;
759 }
760
761 /* Check for 8 byte alignment */
762 if (!ALIGN(hpitch, 8)) {
763 if (!update) {
764 vpif_dbg(2, debug, "invalid pitch alignment\n");
765 goto exit;
766 }
767 /* adjust to next 8 byte boundary */
768 hpitch = (((hpitch + 7) / 8) * 8);
769 }
770 /* if update is set, modify the bytesperline and sizeimage */
771 if (update) {
772 pixfmt->bytesperline = hpitch;
773 pixfmt->sizeimage = hpitch * vpitch * 2;
774 }
775 /**
776 * Image width and height is always based on current standard width and
777 * height
778 */
779 pixfmt->width = common->fmt.fmt.pix.width;
780 pixfmt->height = common->fmt.fmt.pix.height;
781 return 0;
782exit:
783 return ret;
784}
785
786/**
787 * vpif_config_addr() - function to configure buffer address in vpif
788 * @ch - channel ptr
789 * @muxmode - channel mux mode
790 */
791static void vpif_config_addr(struct channel_obj *ch, int muxmode)
792{
793 struct common_obj *common;
794
795 vpif_dbg(2, debug, "vpif_config_addr\n");
796
797 common = &(ch->common[VPIF_VIDEO_INDEX]);
798
799 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
800 common->set_addr = ch1_set_videobuf_addr;
801 else if (2 == muxmode)
802 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
803 else
804 common->set_addr = ch0_set_videobuf_addr;
805}
806
807/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300808 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300809 * @filep: file pointer
810 * @vma: ptr to vm_area_struct
811 */
812static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
813{
814 /* Get the channel object and file handle object */
815 struct vpif_fh *fh = filep->private_data;
816 struct channel_obj *ch = fh->channel;
817 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300818 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300819
820 vpif_dbg(2, debug, "vpif_mmap\n");
821
Hans Verkuil72246792012-07-31 03:48:31 -0300822 if (mutex_lock_interruptible(&common->lock))
823 return -ERESTARTSYS;
824 ret = vb2_mmap(&common->buffer_queue, vma);
825 mutex_unlock(&common->lock);
826 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300827}
828
829/**
830 * vpif_poll: It is used for select/poll system call
831 * @filep: file pointer
832 * @wait: poll table to wait
833 */
834static unsigned int vpif_poll(struct file *filep, poll_table * wait)
835{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300836 struct vpif_fh *fh = filep->private_data;
837 struct channel_obj *channel = fh->channel;
838 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300839 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300840
841 vpif_dbg(2, debug, "vpif_poll\n");
842
Hans Verkuil72246792012-07-31 03:48:31 -0300843 if (common->started) {
844 mutex_lock(&common->lock);
845 res = vb2_poll(&common->buffer_queue, filep, wait);
846 mutex_unlock(&common->lock);
847 }
848 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300849}
850
851/**
852 * vpif_open : vpif open handler
853 * @filep: file ptr
854 *
855 * It creates object of file handle structure and stores it in private_data
856 * member of filepointer
857 */
858static int vpif_open(struct file *filep)
859{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300860 struct video_device *vdev = video_devdata(filep);
861 struct common_obj *common;
862 struct video_obj *vid_ch;
863 struct channel_obj *ch;
864 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300865
866 vpif_dbg(2, debug, "vpif_open\n");
867
868 ch = video_get_drvdata(vdev);
869
870 vid_ch = &ch->video;
871 common = &ch->common[VPIF_VIDEO_INDEX];
872
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300873 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300874 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300875 if (NULL == fh) {
876 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300877 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300878 }
879
Hans Verkuil72246792012-07-31 03:48:31 -0300880 if (mutex_lock_interruptible(&common->lock)) {
881 kfree(fh);
882 return -ERESTARTSYS;
883 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300884 /* store pointer to fh in private_data member of filep */
885 filep->private_data = fh;
886 fh->channel = ch;
887 fh->initialized = 0;
888 /* If decoder is not initialized. initialize it */
889 if (!ch->initialized) {
890 fh->initialized = 1;
891 ch->initialized = 1;
892 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
893 }
894 /* Increment channel usrs counter */
895 ch->usrs++;
896 /* Set io_allowed member to false */
897 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
898 /* Initialize priority of this instance to default priority */
899 fh->prio = V4L2_PRIORITY_UNSET;
900 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300901 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300902 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300903}
904
905/**
906 * vpif_release : function to clean up file close
907 * @filep: file pointer
908 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300909 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300910 * handle
911 */
912static int vpif_release(struct file *filep)
913{
914 struct vpif_fh *fh = filep->private_data;
915 struct channel_obj *ch = fh->channel;
916 struct common_obj *common;
917
918 vpif_dbg(2, debug, "vpif_release\n");
919
920 common = &ch->common[VPIF_VIDEO_INDEX];
921
Hans Verkuil72246792012-07-31 03:48:31 -0300922 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300923 /* if this instance is doing IO */
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300924 if (fh->io_allowed[VPIF_VIDEO_INDEX])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300925 /* Reset io_usrs member of channel object */
926 common->io_usrs = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300927
928 /* Decrement channel usrs counter */
929 ch->usrs--;
930
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300931 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300932 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300933
934 if (fh->initialized)
935 ch->initialized = 0;
936
Hans Verkuil72246792012-07-31 03:48:31 -0300937 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300938 filep->private_data = NULL;
939 kfree(fh);
940 return 0;
941}
942
943/**
944 * vpif_reqbufs() - request buffer handler
945 * @file: file ptr
946 * @priv: file handle
947 * @reqbuf: request buffer structure ptr
948 */
949static int vpif_reqbufs(struct file *file, void *priv,
950 struct v4l2_requestbuffers *reqbuf)
951{
952 struct vpif_fh *fh = priv;
953 struct channel_obj *ch = fh->channel;
954 struct common_obj *common;
955 u8 index = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300956
957 vpif_dbg(2, debug, "vpif_reqbufs\n");
958
959 /**
960 * This file handle has not initialized the channel,
961 * It is not allowed to do settings
962 */
963 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
964 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
965 if (!fh->initialized) {
966 vpif_dbg(1, debug, "Channel Busy\n");
967 return -EBUSY;
968 }
969 }
970
Manjunath Hadli764af392012-04-13 04:49:34 -0300971 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300972 return -EINVAL;
973
974 index = VPIF_VIDEO_INDEX;
975
976 common = &ch->common[index];
977
Hans Verkuil46656af2011-01-04 06:51:35 -0300978 if (0 != common->io_usrs)
979 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300980
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300981 /* Set io allowed member of file handle to TRUE */
982 fh->io_allowed[index] = 1;
983 /* Increment io usrs member of channel object to 1 */
984 common->io_usrs = 1;
985 /* Store type of memory requested in channel object */
986 common->memory = reqbuf->memory;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300987
988 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300989 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300990}
991
992/**
993 * vpif_querybuf() - query buffer handler
994 * @file: file ptr
995 * @priv: file handle
996 * @buf: v4l2 buffer structure ptr
997 */
998static int vpif_querybuf(struct file *file, void *priv,
999 struct v4l2_buffer *buf)
1000{
1001 struct vpif_fh *fh = priv;
1002 struct channel_obj *ch = fh->channel;
1003 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1004
1005 vpif_dbg(2, debug, "vpif_querybuf\n");
1006
1007 if (common->fmt.type != buf->type)
1008 return -EINVAL;
1009
1010 if (common->memory != V4L2_MEMORY_MMAP) {
1011 vpif_dbg(1, debug, "Invalid memory\n");
1012 return -EINVAL;
1013 }
1014
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001015 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001016}
1017
1018/**
1019 * vpif_qbuf() - query buffer handler
1020 * @file: file ptr
1021 * @priv: file handle
1022 * @buf: v4l2 buffer structure ptr
1023 */
1024static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1025{
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 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001031
1032 vpif_dbg(2, debug, "vpif_qbuf\n");
1033
1034 if (common->fmt.type != tbuf.type) {
1035 vpif_err("invalid buffer type\n");
1036 return -EINVAL;
1037 }
1038
1039 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001040 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001041 return -EACCES;
1042 }
1043
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001044 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001045}
1046
1047/**
1048 * vpif_dqbuf() - query buffer handler
1049 * @file: file ptr
1050 * @priv: file handle
1051 * @buf: v4l2 buffer structure ptr
1052 */
1053static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1054{
1055 struct vpif_fh *fh = priv;
1056 struct channel_obj *ch = fh->channel;
1057 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1058
1059 vpif_dbg(2, debug, "vpif_dqbuf\n");
1060
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001061 return vb2_dqbuf(&common->buffer_queue, buf,
1062 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001063}
1064
1065/**
1066 * vpif_streamon() - streamon handler
1067 * @file: file ptr
1068 * @priv: file handle
1069 * @buftype: v4l2 buffer type
1070 */
1071static int vpif_streamon(struct file *file, void *priv,
1072 enum v4l2_buf_type buftype)
1073{
1074
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001075 struct vpif_fh *fh = priv;
1076 struct channel_obj *ch = fh->channel;
1077 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1078 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1079 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001080 int ret = 0;
1081
1082 vpif_dbg(2, debug, "vpif_streamon\n");
1083
1084 vpif = &ch->vpifparams;
1085
1086 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1087 vpif_dbg(1, debug, "buffer type not supported\n");
1088 return -EINVAL;
1089 }
1090
1091 /* If file handle is not allowed IO, return error */
1092 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1093 vpif_dbg(1, debug, "io not allowed\n");
1094 return -EACCES;
1095 }
1096
1097 /* If Streaming is already started, return error */
1098 if (common->started) {
1099 vpif_dbg(1, debug, "channel->started\n");
1100 return -EBUSY;
1101 }
1102
1103 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1104 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1105 vpif->std_info.ycmux_mode == 0) ||
1106 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1107 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1108 vpif_dbg(1, debug, "other channel is being used\n");
1109 return -EBUSY;
1110 }
1111
1112 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1113 if (ret)
1114 return ret;
1115
1116 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001117 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001118
Hans Verkuil178cce12012-09-20 09:06:30 -03001119 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001120 vpif_dbg(1, debug, "stream on failed in subdev\n");
1121 return ret;
1122 }
1123
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001124 /* Call vb2_streamon to start streaming in videobuf2 */
1125 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001126 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001127 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001128 return ret;
1129 }
1130
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001131 return ret;
1132}
1133
1134/**
1135 * vpif_streamoff() - streamoff handler
1136 * @file: file ptr
1137 * @priv: file handle
1138 * @buftype: v4l2 buffer type
1139 */
1140static int vpif_streamoff(struct file *file, void *priv,
1141 enum v4l2_buf_type buftype)
1142{
1143
1144 struct vpif_fh *fh = priv;
1145 struct channel_obj *ch = fh->channel;
1146 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1147 int ret;
1148
1149 vpif_dbg(2, debug, "vpif_streamoff\n");
1150
1151 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1152 vpif_dbg(1, debug, "buffer type not supported\n");
1153 return -EINVAL;
1154 }
1155
1156 /* If io is allowed for this file handle, return error */
1157 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1158 vpif_dbg(1, debug, "io not allowed\n");
1159 return -EACCES;
1160 }
1161
1162 /* If streaming is not started, return error */
1163 if (!common->started) {
1164 vpif_dbg(1, debug, "channel->started\n");
1165 return -EINVAL;
1166 }
1167
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001168 /* disable channel */
1169 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1170 enable_channel0(0);
1171 channel0_intr_enable(0);
1172 } else {
1173 enable_channel1(0);
1174 channel1_intr_enable(0);
1175 }
1176
1177 common->started = 0;
1178
Hans Verkuil178cce12012-09-20 09:06:30 -03001179 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001180
Hans Verkuil178cce12012-09-20 09:06:30 -03001181 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001182 vpif_dbg(1, debug, "stream off failed in subdev\n");
1183
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001184 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001185}
1186
1187/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001188 * vpif_input_to_subdev() - Maps input to sub device
1189 * @vpif_cfg - global config ptr
1190 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001191 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001192 *
1193 * lookup the sub device information for a given input index.
1194 * we report all the inputs to application. inputs table also
1195 * has sub device name for the each input
1196 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001197static int vpif_input_to_subdev(
1198 struct vpif_capture_config *vpif_cfg,
1199 struct vpif_capture_chan_config *chan_cfg,
1200 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001201{
Hans Verkuil178cce12012-09-20 09:06:30 -03001202 struct vpif_subdev_info *subdev_info;
1203 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001204 int i;
1205
Hans Verkuil178cce12012-09-20 09:06:30 -03001206 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001207
Hans Verkuil178cce12012-09-20 09:06:30 -03001208 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1209 if (subdev_name == NULL)
1210 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001211
1212 /* loop through the sub device list to get the sub device info */
1213 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1214 subdev_info = &vpif_cfg->subdev_info[i];
1215 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001216 return i;
1217 }
1218 return -1;
1219}
1220
1221/**
1222 * vpif_set_input() - Select an input
1223 * @vpif_cfg - global config ptr
1224 * @ch - channel
1225 * @_index - Given input index from application
1226 *
1227 * Select the given input.
1228 */
1229static int vpif_set_input(
1230 struct vpif_capture_config *vpif_cfg,
1231 struct channel_obj *ch,
1232 int index)
1233{
1234 struct vpif_capture_chan_config *chan_cfg =
1235 &vpif_cfg->chan_config[ch->channel_id];
1236 struct vpif_subdev_info *subdev_info = NULL;
1237 struct v4l2_subdev *sd = NULL;
1238 u32 input = 0, output = 0;
1239 int sd_index;
1240 int ret;
1241
1242 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1243 if (sd_index >= 0) {
1244 sd = vpif_obj.sd[sd_index];
1245 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001246 }
1247
Hans Verkuil178cce12012-09-20 09:06:30 -03001248 /* first setup input path from sub device to vpif */
1249 if (sd && vpif_cfg->setup_input_path) {
1250 ret = vpif_cfg->setup_input_path(ch->channel_id,
1251 subdev_info->name);
1252 if (ret < 0) {
1253 vpif_dbg(1, debug, "couldn't setup input path for the" \
1254 " sub device %s, for input index %d\n",
1255 subdev_info->name, index);
1256 return ret;
1257 }
1258 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001259
Hans Verkuil178cce12012-09-20 09:06:30 -03001260 if (sd) {
1261 input = chan_cfg->inputs[index].input_route;
1262 output = chan_cfg->inputs[index].output_route;
1263 ret = v4l2_subdev_call(sd, video, s_routing,
1264 input, output, 0);
1265 if (ret < 0 && ret != -ENOIOCTLCMD) {
1266 vpif_dbg(1, debug, "Failed to set input\n");
1267 return ret;
1268 }
1269 }
1270 ch->input_idx = index;
1271 ch->sd = sd;
1272 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001273 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001274
Hans Verkuil178cce12012-09-20 09:06:30 -03001275 /* update tvnorms from the sub device input info */
1276 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1277 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001278}
1279
1280/**
1281 * vpif_querystd() - querystd handler
1282 * @file: file ptr
1283 * @priv: file handle
1284 * @std_id: ptr to std id
1285 *
1286 * This function is called to detect standard at the selected input
1287 */
1288static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1289{
1290 struct vpif_fh *fh = priv;
1291 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001292 int ret = 0;
1293
1294 vpif_dbg(2, debug, "vpif_querystd\n");
1295
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001296 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001297 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001298
Hans Verkuil178cce12012-09-20 09:06:30 -03001299 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1300 return -ENODATA;
1301 if (ret) {
1302 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1303 return ret;
1304 }
1305
1306 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001307}
1308
1309/**
1310 * vpif_g_std() - get STD handler
1311 * @file: file ptr
1312 * @priv: file handle
1313 * @std_id: ptr to std id
1314 */
1315static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1316{
1317 struct vpif_fh *fh = priv;
1318 struct channel_obj *ch = fh->channel;
1319
1320 vpif_dbg(2, debug, "vpif_g_std\n");
1321
1322 *std = ch->video.stdid;
1323 return 0;
1324}
1325
1326/**
1327 * vpif_s_std() - set STD handler
1328 * @file: file ptr
1329 * @priv: file handle
1330 * @std_id: ptr to std id
1331 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001332static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001333{
1334 struct vpif_fh *fh = priv;
1335 struct channel_obj *ch = fh->channel;
1336 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1337 int ret = 0;
1338
1339 vpif_dbg(2, debug, "vpif_s_std\n");
1340
1341 if (common->started) {
1342 vpif_err("streaming in progress\n");
1343 return -EBUSY;
1344 }
1345
1346 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1347 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1348 if (!fh->initialized) {
1349 vpif_dbg(1, debug, "Channel Busy\n");
1350 return -EBUSY;
1351 }
1352 }
1353
Hans Verkuilffb48772010-05-01 08:03:24 -03001354 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001355 if (0 != ret)
1356 return ret;
1357
1358 fh->initialized = 1;
1359
1360 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001361 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001362 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001363
1364 /* Get the information about the standard */
1365 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001366 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001367 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001368 }
1369
1370 /* Configure the default format information */
1371 vpif_config_format(ch);
1372
1373 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001374 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001375 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001376 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001377 return ret;
1378 }
1379 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001380}
1381
1382/**
1383 * vpif_enum_input() - ENUMINPUT handler
1384 * @file: file ptr
1385 * @priv: file handle
1386 * @input: ptr to input structure
1387 */
1388static int vpif_enum_input(struct file *file, void *priv,
1389 struct v4l2_input *input)
1390{
1391
1392 struct vpif_capture_config *config = vpif_dev->platform_data;
1393 struct vpif_capture_chan_config *chan_cfg;
1394 struct vpif_fh *fh = priv;
1395 struct channel_obj *ch = fh->channel;
1396
1397 chan_cfg = &config->chan_config[ch->channel_id];
1398
1399 if (input->index >= chan_cfg->input_count) {
1400 vpif_dbg(1, debug, "Invalid input index\n");
1401 return -EINVAL;
1402 }
1403
1404 memcpy(input, &chan_cfg->inputs[input->index].input,
1405 sizeof(*input));
1406 return 0;
1407}
1408
1409/**
1410 * vpif_g_input() - Get INPUT handler
1411 * @file: file ptr
1412 * @priv: file handle
1413 * @index: ptr to input index
1414 */
1415static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1416{
1417 struct vpif_fh *fh = priv;
1418 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001419
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001420 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001421 return 0;
1422}
1423
1424/**
1425 * vpif_s_input() - Set INPUT handler
1426 * @file: file ptr
1427 * @priv: file handle
1428 * @index: input index
1429 */
1430static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1431{
1432 struct vpif_capture_config *config = vpif_dev->platform_data;
1433 struct vpif_capture_chan_config *chan_cfg;
1434 struct vpif_fh *fh = priv;
1435 struct channel_obj *ch = fh->channel;
1436 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001437 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001438
1439 chan_cfg = &config->chan_config[ch->channel_id];
1440
Hans Verkuil7aaad132012-09-20 09:06:25 -03001441 if (index >= chan_cfg->input_count)
1442 return -EINVAL;
1443
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001444 if (common->started) {
1445 vpif_err("Streaming in progress\n");
1446 return -EBUSY;
1447 }
1448
1449 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1450 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1451 if (!fh->initialized) {
1452 vpif_dbg(1, debug, "Channel Busy\n");
1453 return -EBUSY;
1454 }
1455 }
1456
Hans Verkuilffb48772010-05-01 08:03:24 -03001457 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001458 if (0 != ret)
1459 return ret;
1460
1461 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001462 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001463}
1464
1465/**
1466 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1467 * @file: file ptr
1468 * @priv: file handle
1469 * @index: input index
1470 */
1471static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1472 struct v4l2_fmtdesc *fmt)
1473{
1474 struct vpif_fh *fh = priv;
1475 struct channel_obj *ch = fh->channel;
1476
1477 if (fmt->index != 0) {
1478 vpif_dbg(1, debug, "Invalid format index\n");
1479 return -EINVAL;
1480 }
1481
1482 /* Fill in the information about format */
1483 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1484 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1485 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1486 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1487 } else {
1488 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1489 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1490 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1491 }
1492 return 0;
1493}
1494
1495/**
1496 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1497 * @file: file ptr
1498 * @priv: file handle
1499 * @fmt: ptr to v4l2 format structure
1500 */
1501static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1502 struct v4l2_format *fmt)
1503{
1504 struct vpif_fh *fh = priv;
1505 struct channel_obj *ch = fh->channel;
1506 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1507
1508 return vpif_check_format(ch, pixfmt, 1);
1509}
1510
1511
1512/**
1513 * vpif_g_fmt_vid_cap() - Set INPUT handler
1514 * @file: file ptr
1515 * @priv: file handle
1516 * @fmt: ptr to v4l2 format structure
1517 */
1518static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1519 struct v4l2_format *fmt)
1520{
1521 struct vpif_fh *fh = priv;
1522 struct channel_obj *ch = fh->channel;
1523 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1524
1525 /* Check the validity of the buffer type */
1526 if (common->fmt.type != fmt->type)
1527 return -EINVAL;
1528
1529 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001530 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001531 return 0;
1532}
1533
1534/**
1535 * vpif_s_fmt_vid_cap() - Set FMT handler
1536 * @file: file ptr
1537 * @priv: file handle
1538 * @fmt: ptr to v4l2 format structure
1539 */
1540static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1541 struct v4l2_format *fmt)
1542{
1543 struct vpif_fh *fh = priv;
1544 struct channel_obj *ch = fh->channel;
1545 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1546 struct v4l2_pix_format *pixfmt;
1547 int ret = 0;
1548
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001549 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001550
1551 /* If streaming is started, return error */
1552 if (common->started) {
1553 vpif_dbg(1, debug, "Streaming is started\n");
1554 return -EBUSY;
1555 }
1556
1557 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1558 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1559 if (!fh->initialized) {
1560 vpif_dbg(1, debug, "Channel Busy\n");
1561 return -EBUSY;
1562 }
1563 }
1564
Hans Verkuilffb48772010-05-01 08:03:24 -03001565 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001566 if (0 != ret)
1567 return ret;
1568
1569 fh->initialized = 1;
1570
1571 pixfmt = &fmt->fmt.pix;
1572 /* Check for valid field format */
1573 ret = vpif_check_format(ch, pixfmt, 0);
1574
1575 if (ret)
1576 return ret;
1577 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001578 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001579 return 0;
1580}
1581
1582/**
1583 * vpif_querycap() - QUERYCAP handler
1584 * @file: file ptr
1585 * @priv: file handle
1586 * @cap: ptr to v4l2_capability structure
1587 */
1588static int vpif_querycap(struct file *file, void *priv,
1589 struct v4l2_capability *cap)
1590{
1591 struct vpif_capture_config *config = vpif_dev->platform_data;
1592
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001593 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1594 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1595 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1596 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1597 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001598 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1599
1600 return 0;
1601}
1602
1603/**
1604 * vpif_g_priority() - get priority handler
1605 * @file: file ptr
1606 * @priv: file handle
1607 * @prio: ptr to v4l2_priority structure
1608 */
1609static int vpif_g_priority(struct file *file, void *priv,
1610 enum v4l2_priority *prio)
1611{
1612 struct vpif_fh *fh = priv;
1613 struct channel_obj *ch = fh->channel;
1614
1615 *prio = v4l2_prio_max(&ch->prio);
1616
1617 return 0;
1618}
1619
1620/**
1621 * vpif_s_priority() - set priority handler
1622 * @file: file ptr
1623 * @priv: file handle
1624 * @prio: ptr to v4l2_priority structure
1625 */
1626static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1627{
1628 struct vpif_fh *fh = priv;
1629 struct channel_obj *ch = fh->channel;
1630
1631 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1632}
1633
1634/**
1635 * vpif_cropcap() - cropcap handler
1636 * @file: file ptr
1637 * @priv: file handle
1638 * @crop: ptr to v4l2_cropcap structure
1639 */
1640static int vpif_cropcap(struct file *file, void *priv,
1641 struct v4l2_cropcap *crop)
1642{
1643 struct vpif_fh *fh = priv;
1644 struct channel_obj *ch = fh->channel;
1645 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1646
1647 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1648 return -EINVAL;
1649
1650 crop->bounds.left = 0;
1651 crop->bounds.top = 0;
1652 crop->bounds.height = common->height;
1653 crop->bounds.width = common->width;
1654 crop->defrect = crop->bounds;
1655 return 0;
1656}
1657
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001658/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001659 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001660 * @file: file ptr
1661 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001662 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001663 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001664static int
1665vpif_enum_dv_timings(struct file *file, void *priv,
1666 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001667{
1668 struct vpif_fh *fh = priv;
1669 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001670 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001671
Hans Verkuil178cce12012-09-20 09:06:30 -03001672 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001673 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001674 return -EINVAL;
1675 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001676}
1677
1678/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001679 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001680 * @file: file ptr
1681 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001682 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001683 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001684static int
1685vpif_query_dv_timings(struct file *file, void *priv,
1686 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001687{
1688 struct vpif_fh *fh = priv;
1689 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001690 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001691
Hans Verkuil178cce12012-09-20 09:06:30 -03001692 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001693 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001694 return -ENODATA;
1695 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001696}
1697
Mats Randgaardc027e162010-12-16 12:17:44 -03001698/**
1699 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1700 * @file: file ptr
1701 * @priv: file handle
1702 * @timings: digital video timings
1703 */
1704static int vpif_s_dv_timings(struct file *file, void *priv,
1705 struct v4l2_dv_timings *timings)
1706{
1707 struct vpif_fh *fh = priv;
1708 struct channel_obj *ch = fh->channel;
1709 struct vpif_params *vpifparams = &ch->vpifparams;
1710 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1711 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001712 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001713 int ret;
1714
1715 if (timings->type != V4L2_DV_BT_656_1120) {
1716 vpif_dbg(2, debug, "Timing type not defined\n");
1717 return -EINVAL;
1718 }
1719
1720 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001721 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1722 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1723 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001724 if (ret < 0) {
1725 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1726 return ret;
1727 }
1728
1729 if (!(timings->bt.width && timings->bt.height &&
1730 (timings->bt.hbackporch ||
1731 timings->bt.hfrontporch ||
1732 timings->bt.hsync) &&
1733 timings->bt.vfrontporch &&
1734 (timings->bt.vbackporch ||
1735 timings->bt.vsync))) {
1736 vpif_dbg(2, debug, "Timings for width, height, "
1737 "horizontal back porch, horizontal sync, "
1738 "horizontal front porch, vertical back porch, "
1739 "vertical sync and vertical back porch "
1740 "must be defined\n");
1741 return -EINVAL;
1742 }
1743
Hans Verkuil0598c172012-09-18 07:18:47 -03001744 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001745
1746 /* Configure video port timings */
1747
Hans Verkuile3655262013-07-29 08:41:00 -03001748 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001749 std_info->sav2eav = bt->width;
1750
1751 std_info->l1 = 1;
1752 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1753
Hans Verkuile3655262013-07-29 08:41:00 -03001754 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001755 if (bt->interlaced) {
1756 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001757 std_info->l5 = std_info->vsize/2 -
1758 (bt->vfrontporch - 1);
1759 std_info->l7 = std_info->vsize/2 + 1;
1760 std_info->l9 = std_info->l7 + bt->il_vsync +
1761 bt->il_vbackporch + 1;
1762 std_info->l11 = std_info->vsize -
1763 (bt->il_vfrontporch - 1);
1764 } else {
1765 vpif_dbg(2, debug, "Required timing values for "
1766 "interlaced BT format missing\n");
1767 return -EINVAL;
1768 }
1769 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001770 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1771 }
1772 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1773 std_info->width = bt->width;
1774 std_info->height = bt->height;
1775 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1776 std_info->ycmux_mode = 0;
1777 std_info->capture_format = 0;
1778 std_info->vbi_supported = 0;
1779 std_info->hd_sd = 1;
1780 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001781
1782 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001783 return 0;
1784}
1785
1786/**
1787 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1788 * @file: file ptr
1789 * @priv: file handle
1790 * @timings: digital video timings
1791 */
1792static int vpif_g_dv_timings(struct file *file, void *priv,
1793 struct v4l2_dv_timings *timings)
1794{
1795 struct vpif_fh *fh = priv;
1796 struct channel_obj *ch = fh->channel;
1797 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001798
Hans Verkuil0598c172012-09-18 07:18:47 -03001799 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001800
1801 return 0;
1802}
1803
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001804/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001805 * vpif_log_status() - Status information
1806 * @file: file ptr
1807 * @priv: file handle
1808 *
1809 * Returns zero.
1810 */
1811static int vpif_log_status(struct file *filep, void *priv)
1812{
1813 /* status for sub devices */
1814 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1815
1816 return 0;
1817}
1818
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001819/* vpif capture ioctl operations */
1820static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1821 .vidioc_querycap = vpif_querycap,
1822 .vidioc_g_priority = vpif_g_priority,
1823 .vidioc_s_priority = vpif_s_priority,
1824 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1825 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1826 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1827 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1828 .vidioc_enum_input = vpif_enum_input,
1829 .vidioc_s_input = vpif_s_input,
1830 .vidioc_g_input = vpif_g_input,
1831 .vidioc_reqbufs = vpif_reqbufs,
1832 .vidioc_querybuf = vpif_querybuf,
1833 .vidioc_querystd = vpif_querystd,
1834 .vidioc_s_std = vpif_s_std,
1835 .vidioc_g_std = vpif_g_std,
1836 .vidioc_qbuf = vpif_qbuf,
1837 .vidioc_dqbuf = vpif_dqbuf,
1838 .vidioc_streamon = vpif_streamon,
1839 .vidioc_streamoff = vpif_streamoff,
1840 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001841 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1842 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001843 .vidioc_s_dv_timings = vpif_s_dv_timings,
1844 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001845 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001846};
1847
1848/* vpif file operations */
1849static struct v4l2_file_operations vpif_fops = {
1850 .owner = THIS_MODULE,
1851 .open = vpif_open,
1852 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001853 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001854 .mmap = vpif_mmap,
1855 .poll = vpif_poll
1856};
1857
1858/* vpif video template */
1859static struct video_device vpif_video_template = {
1860 .name = "vpif",
1861 .fops = &vpif_fops,
1862 .minor = -1,
1863 .ioctl_ops = &vpif_ioctl_ops,
1864};
1865
1866/**
1867 * initialize_vpif() - Initialize vpif data structures
1868 *
1869 * Allocate memory for data structures and initialize them
1870 */
1871static int initialize_vpif(void)
1872{
1873 int err = 0, i, j;
1874 int free_channel_objects_index;
1875
1876 /* Default number of buffers should be 3 */
1877 if ((ch0_numbuffers > 0) &&
1878 (ch0_numbuffers < config_params.min_numbuffers))
1879 ch0_numbuffers = config_params.min_numbuffers;
1880 if ((ch1_numbuffers > 0) &&
1881 (ch1_numbuffers < config_params.min_numbuffers))
1882 ch1_numbuffers = config_params.min_numbuffers;
1883
1884 /* Set buffer size to min buffers size if it is invalid */
1885 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1886 ch0_bufsize =
1887 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1888 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1889 ch1_bufsize =
1890 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1891
1892 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1893 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1894 if (ch0_numbuffers) {
1895 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1896 = ch0_bufsize;
1897 }
1898 if (ch1_numbuffers) {
1899 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1900 = ch1_bufsize;
1901 }
1902
1903 /* Allocate memory for six channel objects */
1904 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1905 vpif_obj.dev[i] =
1906 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1907 /* If memory allocation fails, return error */
1908 if (!vpif_obj.dev[i]) {
1909 free_channel_objects_index = i;
1910 err = -ENOMEM;
1911 goto vpif_init_free_channel_objects;
1912 }
1913 }
1914 return 0;
1915
1916vpif_init_free_channel_objects:
1917 for (j = 0; j < free_channel_objects_index; j++)
1918 kfree(vpif_obj.dev[j]);
1919 return err;
1920}
1921
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001922static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1923 struct v4l2_subdev *subdev,
1924 struct v4l2_async_subdev *asd)
1925{
1926 int i;
1927
1928 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1929 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1930 subdev->name)) {
1931 vpif_obj.sd[i] = subdev;
1932 return 0;
1933 }
1934
1935 return -EINVAL;
1936}
1937
1938static int vpif_probe_complete(void)
1939{
1940 struct common_obj *common;
1941 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001942 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001943 int i, j, err, k;
1944
1945 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1946 ch = vpif_obj.dev[j];
1947 ch->channel_id = j;
1948 common = &(ch->common[VPIF_VIDEO_INDEX]);
1949 spin_lock_init(&common->irqlock);
1950 mutex_init(&common->lock);
1951 ch->video_dev->lock = &common->lock;
1952 /* Initialize prio member of channel object */
1953 v4l2_prio_init(&ch->prio);
1954 video_set_drvdata(ch->video_dev, ch);
1955
1956 /* select input 0 */
1957 err = vpif_set_input(vpif_obj.config, ch, 0);
1958 if (err)
1959 goto probe_out;
1960
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001961 /* Initialize vb2 queue */
1962 q = &common->buffer_queue;
1963 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1964 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1965 q->drv_priv = ch;
1966 q->ops = &video_qops;
1967 q->mem_ops = &vb2_dma_contig_memops;
1968 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1969 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1970 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001971 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001972
1973 err = vb2_queue_init(q);
1974 if (err) {
1975 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1976 goto probe_out;
1977 }
1978
1979 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1980 if (IS_ERR(common->alloc_ctx)) {
1981 vpif_err("Failed to get the context\n");
1982 err = PTR_ERR(common->alloc_ctx);
1983 goto probe_out;
1984 }
1985
1986 INIT_LIST_HEAD(&common->dma_queue);
1987
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001988 err = video_register_device(ch->video_dev,
1989 VFL_TYPE_GRABBER, (j ? 1 : 0));
1990 if (err)
1991 goto probe_out;
1992 }
1993
1994 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1995 return 0;
1996
1997probe_out:
1998 for (k = 0; k < j; k++) {
1999 /* Get the pointer to the channel object */
2000 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002001 common = &ch->common[k];
2002 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002003 /* Unregister video device */
2004 video_unregister_device(ch->video_dev);
2005 }
2006 kfree(vpif_obj.sd);
2007 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2008 ch = vpif_obj.dev[i];
2009 /* Note: does nothing if ch->video_dev == NULL */
2010 video_device_release(ch->video_dev);
2011 }
2012 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2013
2014 return err;
2015}
2016
2017static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2018{
2019 return vpif_probe_complete();
2020}
2021
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002022/**
2023 * vpif_probe : This function probes the vpif capture driver
2024 * @pdev: platform device pointer
2025 *
2026 * This creates device entries by register itself to the V4L2 driver and
2027 * initializes fields of each channel objects
2028 */
2029static __init int vpif_probe(struct platform_device *pdev)
2030{
2031 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002032 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002033 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002034 struct i2c_adapter *i2c_adap;
2035 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002036 struct video_device *vfd;
2037 struct resource *res;
2038 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03002039 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002040
2041 vpif_dev = &pdev->dev;
2042
2043 err = initialize_vpif();
2044 if (err) {
2045 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2046 return err;
2047 }
2048
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002049 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2050 if (err) {
2051 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2052 return err;
2053 }
2054
Hans Verkuil5be452c2012-09-20 09:06:29 -03002055 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03002056 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2057 IRQF_SHARED, "VPIF_Capture",
2058 (void *)(&vpif_obj.dev[res_idx]->
2059 channel_id));
2060 if (err) {
2061 err = -EINVAL;
2062 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002063 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002064 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002065 }
2066
2067 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2068 /* Get the pointer to the channel object */
2069 ch = vpif_obj.dev[i];
2070 /* Allocate memory for video device */
2071 vfd = video_device_alloc();
2072 if (NULL == vfd) {
2073 for (j = 0; j < i; j++) {
2074 ch = vpif_obj.dev[j];
2075 video_device_release(ch->video_dev);
2076 }
2077 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002078 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002079 }
2080
2081 /* Initialize field of video device */
2082 *vfd = vpif_video_template;
2083 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2084 vfd->release = video_device_release;
2085 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002086 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002087 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002088 /* Set video_dev to the video device */
2089 ch->video_dev = vfd;
2090 }
2091
Manjunath Hadli764af392012-04-13 04:49:34 -03002092 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2093 if (res) {
2094 size = resource_size(res);
2095 /* The resources are divided into two equal memory and when we
2096 * have HD output we can add them together
2097 */
2098 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2099 ch = vpif_obj.dev[j];
2100 ch->channel_id = j;
2101 /* only enabled if second resource exists */
2102 config_params.video_limit[ch->channel_id] = 0;
2103 if (size)
2104 config_params.video_limit[ch->channel_id] =
2105 size/2;
2106 }
2107 }
2108
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002109 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002110
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002111 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002112 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002113 GFP_KERNEL);
2114 if (vpif_obj.sd == NULL) {
2115 vpif_err("unable to allocate memory for subdevice pointers\n");
2116 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002117 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002118 }
2119
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002120 if (!vpif_obj.config->asd_sizes) {
2121 i2c_adap = i2c_get_adapter(1);
2122 for (i = 0; i < subdev_count; i++) {
2123 subdevdata = &vpif_obj.config->subdev_info[i];
2124 vpif_obj.sd[i] =
2125 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2126 i2c_adap,
2127 &subdevdata->
2128 board_info,
2129 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002130
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002131 if (!vpif_obj.sd[i]) {
2132 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03002133 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002134 goto probe_subdev_out;
2135 }
2136 v4l2_info(&vpif_obj.v4l2_dev,
2137 "registered sub device %s\n",
2138 subdevdata->name);
2139 }
2140 vpif_probe_complete();
2141 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03002142 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002143 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2144 vpif_obj.notifier.bound = vpif_async_bound;
2145 vpif_obj.notifier.complete = vpif_async_complete;
2146 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2147 &vpif_obj.notifier);
2148 if (err) {
2149 vpif_err("Error registering async notifier\n");
2150 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002151 goto probe_subdev_out;
2152 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002153 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002154
2155 return 0;
2156
Hans Verkuilb65814e2012-09-20 09:06:26 -03002157probe_subdev_out:
2158 /* free sub devices memory */
2159 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002160
Hans Verkuil5be452c2012-09-20 09:06:29 -03002161vpif_sd_error:
2162 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2163 ch = vpif_obj.dev[i];
2164 /* Note: does nothing if ch->video_dev == NULL */
2165 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002166 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002167vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002168 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002169
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002170 return err;
2171}
2172
2173/**
2174 * vpif_remove() - driver remove handler
2175 * @device: ptr to platform device structure
2176 *
2177 * The vidoe device is unregistered
2178 */
2179static int vpif_remove(struct platform_device *device)
2180{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002181 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002182 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002183 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002184
2185 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2186
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002187 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002188 /* un-register device */
2189 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2190 /* Get the pointer to the channel object */
2191 ch = vpif_obj.dev[i];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002192 common = &ch->common[i];
2193 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002194 /* Unregister video device */
2195 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002196 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002197 }
2198 return 0;
2199}
2200
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002201#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002202/**
2203 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002204 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002205static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002206{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002207
2208 struct common_obj *common;
2209 struct channel_obj *ch;
2210 int i;
2211
2212 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2213 /* Get the pointer to the channel object */
2214 ch = vpif_obj.dev[i];
2215 common = &ch->common[VPIF_VIDEO_INDEX];
2216 mutex_lock(&common->lock);
2217 if (ch->usrs && common->io_usrs) {
2218 /* Disable channel */
2219 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2220 enable_channel0(0);
2221 channel0_intr_enable(0);
2222 }
2223 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2224 common->started == 2) {
2225 enable_channel1(0);
2226 channel1_intr_enable(0);
2227 }
2228 }
2229 mutex_unlock(&common->lock);
2230 }
2231
2232 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002233}
2234
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002235/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002236 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002237 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002238static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002239{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002240 struct common_obj *common;
2241 struct channel_obj *ch;
2242 int i;
2243
2244 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2245 /* Get the pointer to the channel object */
2246 ch = vpif_obj.dev[i];
2247 common = &ch->common[VPIF_VIDEO_INDEX];
2248 mutex_lock(&common->lock);
2249 if (ch->usrs && common->io_usrs) {
2250 /* Disable channel */
2251 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2252 enable_channel0(1);
2253 channel0_intr_enable(1);
2254 }
2255 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2256 common->started == 2) {
2257 enable_channel1(1);
2258 channel1_intr_enable(1);
2259 }
2260 }
2261 mutex_unlock(&common->lock);
2262 }
2263
2264 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002265}
2266
Alexey Dobriyan47145212009-12-14 18:00:08 -08002267static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002268 .suspend = vpif_suspend,
2269 .resume = vpif_resume,
2270};
2271
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002272#define vpif_pm_ops (&vpif_dev_pm_ops)
2273#else
2274#define vpif_pm_ops NULL
2275#endif
2276
Mats Randgaardffa1b392010-08-30 10:30:36 -03002277static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002278 .driver = {
2279 .name = "vpif_capture",
2280 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002281 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002282 },
2283 .probe = vpif_probe,
2284 .remove = vpif_remove,
2285};
2286
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03002287module_platform_driver(vpif_driver);