]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/platform/davinci/vpif_capture.c
[media] media: videobuf2: Restructure vb2_buffer
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / platform / davinci / vpif_capture.c
CommitLineData
6ffefff5
MK
1/*
2 * Copyright (C) 2009 Texas Instruments Inc
96787eb4 3 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
6ffefff5
MK
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * TODO : add support for VBI & HBI data service
20 * add static buffer allocation
21 */
012eef70 22
6ffefff5 23#include <linux/module.h>
6ffefff5 24#include <linux/interrupt.h>
6ffefff5 25#include <linux/platform_device.h>
5a0e3ad6 26#include <linux/slab.h>
012eef70 27
012eef70 28#include <media/v4l2-ioctl.h>
6ffefff5 29
6ffefff5 30#include "vpif.h"
012eef70 31#include "vpif_capture.h"
6ffefff5
MK
32
33MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34MODULE_LICENSE("GPL");
64dc3c1a 35MODULE_VERSION(VPIF_CAPTURE_VERSION);
6ffefff5
MK
36
37#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
38#define vpif_dbg(level, debug, fmt, arg...) \
39 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
40
41static int debug = 1;
6ffefff5
MK
42
43module_param(debug, int, 0644);
6ffefff5
MK
44
45MODULE_PARM_DESC(debug, "Debug level 0-1");
6ffefff5 46
e75ea0c1
LP
47#define VPIF_DRIVER_NAME "vpif_capture"
48
6ffefff5
MK
49/* global variables */
50static struct vpif_device vpif_obj = { {NULL} };
51static struct device *vpif_dev;
60aa38d8
LP
52static void vpif_calculate_offsets(struct channel_obj *ch);
53static void vpif_config_addr(struct channel_obj *ch, int muxmode);
6ffefff5 54
fa09accc
LP
55static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
56
c66238f3
LP
57/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
58static int ycmux_mode;
59
2d700715
JS
60static inline
61struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
fa09accc
LP
62{
63 return container_of(vb, struct vpif_cap_buffer, vb);
64}
65
6ffefff5 66/**
23e76a2a 67 * vpif_buffer_prepare : callback function for buffer prepare
60aa38d8 68 * @vb: ptr to vb2_buffer
6ffefff5 69 *
60aa38d8 70 * This is the callback function for buffer prepare when vb2_qbuf()
6ffefff5
MK
71 * function is called. The buffer is prepared and user space virtual address
72 * or user address is converted into physical address
73 */
60aa38d8 74static int vpif_buffer_prepare(struct vb2_buffer *vb)
6ffefff5 75{
2d700715 76 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
60aa38d8 77 struct vb2_queue *q = vb->vb2_queue;
fa09accc 78 struct channel_obj *ch = vb2_get_drv_priv(q);
6ffefff5
MK
79 struct common_obj *common;
80 unsigned long addr;
81
6ffefff5
MK
82 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
83
84 common = &ch->common[VPIF_VIDEO_INDEX];
85
23e76a2a
LP
86 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
87 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
88 return -EINVAL;
89
2d700715 90 vbuf->field = common->fmt.fmt.pix.field;
23e76a2a
LP
91
92 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
93 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
94 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
95 !IS_ALIGNED((addr + common->ctop_off), 8) ||
96 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
97 vpif_dbg(1, debug, "offset is not aligned\n");
98 return -EINVAL;
6ffefff5 99 }
23e76a2a 100
6ffefff5 101 return 0;
6ffefff5
MK
102}
103
104/**
60aa38d8
LP
105 * vpif_buffer_queue_setup : Callback function for buffer setup.
106 * @vq: vb2_queue ptr
107 * @fmt: v4l2 format
108 * @nbuffers: ptr to number of buffers requested by application
109 * @nplanes:: contains number of distinct video planes needed to hold a frame
110 * @sizes[]: contains the size (in bytes) of each plane.
111 * @alloc_ctxs: ptr to allocation context
6ffefff5
MK
112 *
113 * This callback function is called when reqbuf() is called to adjust
114 * the buffer count and buffer size
115 */
60aa38d8
LP
116static int vpif_buffer_queue_setup(struct vb2_queue *vq,
117 const struct v4l2_format *fmt,
118 unsigned int *nbuffers, unsigned int *nplanes,
119 unsigned int sizes[], void *alloc_ctxs[])
6ffefff5 120{
fa09accc 121 struct channel_obj *ch = vb2_get_drv_priv(vq);
6ffefff5
MK
122 struct common_obj *common;
123
124 common = &ch->common[VPIF_VIDEO_INDEX];
125
126 vpif_dbg(2, debug, "vpif_buffer_setup\n");
127
837939de
LP
128 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
129 return -EINVAL;
764af39a 130
837939de
LP
131 if (vq->num_buffers + *nbuffers < 3)
132 *nbuffers = 3 - vq->num_buffers;
60aa38d8
LP
133
134 *nplanes = 1;
837939de 135 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
60aa38d8
LP
136 alloc_ctxs[0] = common->alloc_ctx;
137
837939de
LP
138 /* Calculate the offset for Y and C data in the buffer */
139 vpif_calculate_offsets(ch);
140
6ffefff5
MK
141 return 0;
142}
143
144/**
145 * vpif_buffer_queue : Callback function to add buffer to DMA queue
60aa38d8 146 * @vb: ptr to vb2_buffer
6ffefff5 147 */
60aa38d8 148static void vpif_buffer_queue(struct vb2_buffer *vb)
6ffefff5 149{
2d700715 150 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
fa09accc 151 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
2d700715 152 struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
6ffefff5 153 struct common_obj *common;
aec96832 154 unsigned long flags;
6ffefff5
MK
155
156 common = &ch->common[VPIF_VIDEO_INDEX];
157
158 vpif_dbg(2, debug, "vpif_buffer_queue\n");
159
aec96832 160 spin_lock_irqsave(&common->irqlock, flags);
6ffefff5 161 /* add the buffer to the DMA queue */
60aa38d8 162 list_add_tail(&buf->list, &common->dma_queue);
aec96832 163 spin_unlock_irqrestore(&common->irqlock, flags);
6ffefff5
MK
164}
165
41b9f24e
LP
166/**
167 * vpif_start_streaming : Starts the DMA engine for streaming
168 * @vb: ptr to vb2_buffer
169 * @count: number of buffers
170 */
60aa38d8
LP
171static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
172{
173 struct vpif_capture_config *vpif_config_data =
174 vpif_dev->platform_data;
fa09accc 175 struct channel_obj *ch = vb2_get_drv_priv(vq);
60aa38d8
LP
176 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
177 struct vpif_params *vpif = &ch->vpifparams;
bebd8d12
LP
178 struct vpif_cap_buffer *buf, *tmp;
179 unsigned long addr, flags;
60aa38d8
LP
180 int ret;
181
aec96832 182 spin_lock_irqsave(&common->irqlock, flags);
60aa38d8 183
c66238f3 184 /* Initialize field_id */
60aa38d8 185 ch->field_id = 0;
60aa38d8 186
60aa38d8 187 /* configure 1 or 2 channel mode */
f4ad8d74
LP
188 if (vpif_config_data->setup_input_channel_mode) {
189 ret = vpif_config_data->
190 setup_input_channel_mode(vpif->std_info.ycmux_mode);
191 if (ret < 0) {
192 vpif_dbg(1, debug, "can't set vpif channel mode\n");
bebd8d12 193 goto err;
f4ad8d74 194 }
60aa38d8
LP
195 }
196
d9a3b13c
LP
197 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
198 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
199 vpif_dbg(1, debug, "stream on failed in subdev\n");
200 goto err;
201 }
202
60aa38d8
LP
203 /* Call vpif_set_params function to set the parameters and addresses */
204 ret = vpif_set_video_params(vpif, ch->channel_id);
60aa38d8
LP
205 if (ret < 0) {
206 vpif_dbg(1, debug, "can't set video params\n");
bebd8d12 207 goto err;
60aa38d8
LP
208 }
209
c66238f3 210 ycmux_mode = ret;
60aa38d8
LP
211 vpif_config_addr(ch, ret);
212
bebd8d12
LP
213 /* Get the next frame from the buffer queue */
214 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
215 struct vpif_cap_buffer, list);
216 /* Remove buffer from the buffer queue */
217 list_del(&common->cur_frm->list);
218 spin_unlock_irqrestore(&common->irqlock, flags);
bebd8d12 219
2d700715 220 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
bebd8d12 221
60aa38d8
LP
222 common->set_addr(addr + common->ytop_off,
223 addr + common->ybtm_off,
224 addr + common->ctop_off,
225 addr + common->cbtm_off);
226
227 /**
228 * Set interrupt for both the fields in VPIF Register enable channel in
229 * VPIF register
230 */
9e18404a 231 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
41b9f24e 232 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
60aa38d8
LP
233 channel0_intr_assert();
234 channel0_intr_enable(1);
235 enable_channel0(1);
236 }
41b9f24e 237 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
c66238f3 238 ycmux_mode == 2) {
60aa38d8
LP
239 channel1_intr_assert();
240 channel1_intr_enable(1);
241 enable_channel1(1);
242 }
60aa38d8
LP
243
244 return 0;
bebd8d12
LP
245
246err:
247 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
248 list_del(&buf->list);
2d700715 249 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
bebd8d12 250 }
92491968 251 spin_unlock_irqrestore(&common->irqlock, flags);
bebd8d12
LP
252
253 return ret;
60aa38d8
LP
254}
255
41b9f24e
LP
256/**
257 * vpif_stop_streaming : Stop the DMA engine
258 * @vq: ptr to vb2_queue
259 *
260 * This callback stops the DMA engine and any remaining buffers
261 * in the DMA queue are released.
262 */
e37559b2 263static void vpif_stop_streaming(struct vb2_queue *vq)
60aa38d8 264{
fa09accc 265 struct channel_obj *ch = vb2_get_drv_priv(vq);
60aa38d8 266 struct common_obj *common;
aec96832 267 unsigned long flags;
d9a3b13c 268 int ret;
60aa38d8 269
60aa38d8
LP
270 common = &ch->common[VPIF_VIDEO_INDEX];
271
e6ba3db1
LP
272 /* Disable channel as per its device type and channel id */
273 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
274 enable_channel0(0);
275 channel0_intr_enable(0);
276 }
41b9f24e 277 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
c66238f3 278 ycmux_mode == 2) {
e6ba3db1
LP
279 enable_channel1(0);
280 channel1_intr_enable(0);
281 }
c66238f3
LP
282
283 ycmux_mode = 0;
e6ba3db1 284
d9a3b13c
LP
285 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
286 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
287 vpif_dbg(1, debug, "stream off failed in subdev\n");
288
60aa38d8 289 /* release all active buffers */
aec96832 290 spin_lock_irqsave(&common->irqlock, flags);
e6ba3db1 291 if (common->cur_frm == common->next_frm) {
2d700715
JS
292 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
293 VB2_BUF_STATE_ERROR);
e6ba3db1
LP
294 } else {
295 if (common->cur_frm != NULL)
2d700715 296 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
e6ba3db1
LP
297 VB2_BUF_STATE_ERROR);
298 if (common->next_frm != NULL)
2d700715 299 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
e6ba3db1
LP
300 VB2_BUF_STATE_ERROR);
301 }
302
60aa38d8
LP
303 while (!list_empty(&common->dma_queue)) {
304 common->next_frm = list_entry(common->dma_queue.next,
305 struct vpif_cap_buffer, list);
306 list_del(&common->next_frm->list);
2d700715
JS
307 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
308 VB2_BUF_STATE_ERROR);
60aa38d8 309 }
aec96832 310 spin_unlock_irqrestore(&common->irqlock, flags);
60aa38d8
LP
311}
312
313static struct vb2_ops video_qops = {
314 .queue_setup = vpif_buffer_queue_setup,
60aa38d8
LP
315 .buf_prepare = vpif_buffer_prepare,
316 .start_streaming = vpif_start_streaming,
317 .stop_streaming = vpif_stop_streaming,
60aa38d8 318 .buf_queue = vpif_buffer_queue,
b41a97a2
PL
319 .wait_prepare = vb2_ops_wait_prepare,
320 .wait_finish = vb2_ops_wait_finish,
60aa38d8
LP
321};
322
6ffefff5
MK
323/**
324 * vpif_process_buffer_complete: process a completed buffer
325 * @common: ptr to common channel object
326 *
327 * This function time stamp the buffer and mark it as DONE. It also
328 * wake up any process waiting on the QUEUE and set the next buffer
329 * as current
330 */
331static void vpif_process_buffer_complete(struct common_obj *common)
332{
2d700715
JS
333 v4l2_get_timestamp(&common->cur_frm->vb.timestamp);
334 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
6ffefff5
MK
335 /* Make curFrm pointing to nextFrm */
336 common->cur_frm = common->next_frm;
337}
338
339/**
340 * vpif_schedule_next_buffer: set next buffer address for capture
341 * @common : ptr to common channel object
342 *
343 * This function will get next buffer from the dma queue and
344 * set the buffer address in the vpif register for capture.
345 * the buffer is marked active
346 */
347static void vpif_schedule_next_buffer(struct common_obj *common)
348{
349 unsigned long addr = 0;
350
aec96832 351 spin_lock(&common->irqlock);
6ffefff5 352 common->next_frm = list_entry(common->dma_queue.next,
60aa38d8 353 struct vpif_cap_buffer, list);
6ffefff5 354 /* Remove that buffer from the buffer queue */
60aa38d8 355 list_del(&common->next_frm->list);
aec96832 356 spin_unlock(&common->irqlock);
2d700715 357 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
6ffefff5
MK
358
359 /* Set top and bottom field addresses in VPIF registers */
360 common->set_addr(addr + common->ytop_off,
361 addr + common->ybtm_off,
362 addr + common->ctop_off,
363 addr + common->cbtm_off);
364}
365
366/**
367 * vpif_channel_isr : ISR handler for vpif capture
368 * @irq: irq number
369 * @dev_id: dev_id ptr
370 *
371 * It changes status of the captured buffer, takes next buffer from the queue
2c0ddd17 372 * and sets its address in VPIF registers
6ffefff5
MK
373 */
374static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
375{
376 struct vpif_device *dev = &vpif_obj;
377 struct common_obj *common;
378 struct channel_obj *ch;
6ffefff5
MK
379 int channel_id = 0;
380 int fid = -1, i;
381
382 channel_id = *(int *)(dev_id);
b1fc4230
MH
383 if (!vpif_intr_status(channel_id))
384 return IRQ_NONE;
385
6ffefff5
MK
386 ch = dev->dev[channel_id];
387
6ffefff5
MK
388 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
389 common = &ch->common[i];
390 /* skip If streaming is not started in this channel */
6ffefff5
MK
391 /* Check the field format */
392 if (1 == ch->vpifparams.std_info.frm_fmt) {
393 /* Progressive mode */
aec96832
HV
394 spin_lock(&common->irqlock);
395 if (list_empty(&common->dma_queue)) {
396 spin_unlock(&common->irqlock);
6ffefff5 397 continue;
aec96832
HV
398 }
399 spin_unlock(&common->irqlock);
6ffefff5
MK
400
401 if (!channel_first_int[i][channel_id])
402 vpif_process_buffer_complete(common);
403
404 channel_first_int[i][channel_id] = 0;
405
406 vpif_schedule_next_buffer(common);
407
408
409 channel_first_int[i][channel_id] = 0;
410 } else {
411 /**
412 * Interlaced mode. If it is first interrupt, ignore
413 * it
414 */
415 if (channel_first_int[i][channel_id]) {
416 channel_first_int[i][channel_id] = 0;
417 continue;
418 }
419 if (0 == i) {
420 ch->field_id ^= 1;
421 /* Get field id from VPIF registers */
422 fid = vpif_channel_getfid(ch->channel_id);
423 if (fid != ch->field_id) {
424 /**
425 * If field id does not match stored
426 * field id, make them in sync
427 */
428 if (0 == fid)
429 ch->field_id = fid;
430 return IRQ_HANDLED;
431 }
432 }
433 /* device field id and local field id are in sync */
434 if (0 == fid) {
435 /* this is even field */
436 if (common->cur_frm == common->next_frm)
437 continue;
438
439 /* mark the current buffer as done */
440 vpif_process_buffer_complete(common);
441 } else if (1 == fid) {
442 /* odd field */
aec96832 443 spin_lock(&common->irqlock);
6ffefff5 444 if (list_empty(&common->dma_queue) ||
aec96832
HV
445 (common->cur_frm != common->next_frm)) {
446 spin_unlock(&common->irqlock);
6ffefff5 447 continue;
aec96832
HV
448 }
449 spin_unlock(&common->irqlock);
6ffefff5
MK
450
451 vpif_schedule_next_buffer(common);
452 }
453 }
454 }
455 return IRQ_HANDLED;
456}
457
458/**
459 * vpif_update_std_info() - update standard related info
460 * @ch: ptr to channel object
461 *
462 * For a given standard selected by application, update values
463 * in the device data structures
464 */
465static int vpif_update_std_info(struct channel_obj *ch)
466{
467 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
468 struct vpif_params *vpifparams = &ch->vpifparams;
469 const struct vpif_channel_config_params *config;
2c0ddd17 470 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
6ffefff5
MK
471 struct video_obj *vid_ch = &ch->video;
472 int index;
473
474 vpif_dbg(2, debug, "vpif_update_std_info\n");
475
aa444406 476 for (index = 0; index < vpif_ch_params_count; index++) {
ced9b21f 477 config = &vpif_ch_params[index];
40c8bcea
MR
478 if (config->hd_sd == 0) {
479 vpif_dbg(2, debug, "SD format\n");
480 if (config->stdid & vid_ch->stdid) {
481 memcpy(std_info, config, sizeof(*config));
482 break;
483 }
484 } else {
485 vpif_dbg(2, debug, "HD format\n");
0598c17b
HV
486 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
487 sizeof(vid_ch->dv_timings))) {
40c8bcea
MR
488 memcpy(std_info, config, sizeof(*config));
489 break;
490 }
6ffefff5
MK
491 }
492 }
493
494 /* standard not found */
aa444406 495 if (index == vpif_ch_params_count)
6ffefff5
MK
496 return -EINVAL;
497
498 common->fmt.fmt.pix.width = std_info->width;
499 common->width = std_info->width;
500 common->fmt.fmt.pix.height = std_info->height;
501 common->height = std_info->height;
a4965c59 502 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
6ffefff5
MK
503 common->fmt.fmt.pix.bytesperline = std_info->width;
504 vpifparams->video_params.hpitch = std_info->width;
505 vpifparams->video_params.storage_mode = std_info->frm_fmt;
2c0ddd17 506
a4965c59
LP
507 if (vid_ch->stdid)
508 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
509 else
510 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
511
512 if (ch->vpifparams.std_info.frm_fmt)
513 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
514 else
515 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
516
517 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
518 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
519 else
520 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
521
522 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
523
6ffefff5
MK
524 return 0;
525}
526
527/**
528 * vpif_calculate_offsets : This function calculates buffers offsets
529 * @ch : ptr to channel object
530 *
531 * This function calculates buffer offsets for Y and C in the top and
532 * bottom field
533 */
534static void vpif_calculate_offsets(struct channel_obj *ch)
535{
24ab6338 536 unsigned int hpitch, sizeimage;
6ffefff5
MK
537 struct video_obj *vid_ch = &(ch->video);
538 struct vpif_params *vpifparams = &ch->vpifparams;
539 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
540 enum v4l2_field field = common->fmt.fmt.pix.field;
541
542 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
543
544 if (V4L2_FIELD_ANY == field) {
545 if (vpifparams->std_info.frm_fmt)
546 vid_ch->buf_field = V4L2_FIELD_NONE;
547 else
548 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
549 } else
550 vid_ch->buf_field = common->fmt.fmt.pix.field;
551
60aa38d8 552 sizeimage = common->fmt.fmt.pix.sizeimage;
6ffefff5
MK
553
554 hpitch = common->fmt.fmt.pix.bytesperline;
6ffefff5
MK
555
556 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
557 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
558 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
559 common->ytop_off = 0;
560 common->ybtm_off = hpitch;
561 common->ctop_off = sizeimage / 2;
562 common->cbtm_off = sizeimage / 2 + hpitch;
563 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
564 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
565 common->ytop_off = 0;
566 common->ybtm_off = sizeimage / 4;
567 common->ctop_off = sizeimage / 2;
568 common->cbtm_off = common->ctop_off + sizeimage / 4;
569 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
570 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
571 common->ybtm_off = 0;
572 common->ytop_off = sizeimage / 4;
573 common->cbtm_off = sizeimage / 2;
574 common->ctop_off = common->cbtm_off + sizeimage / 4;
575 }
576 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
577 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
578 vpifparams->video_params.storage_mode = 1;
579 else
580 vpifparams->video_params.storage_mode = 0;
581
582 if (1 == vpifparams->std_info.frm_fmt)
583 vpifparams->video_params.hpitch =
584 common->fmt.fmt.pix.bytesperline;
585 else {
586 if ((field == V4L2_FIELD_ANY)
587 || (field == V4L2_FIELD_INTERLACED))
588 vpifparams->video_params.hpitch =
589 common->fmt.fmt.pix.bytesperline * 2;
590 else
591 vpifparams->video_params.hpitch =
592 common->fmt.fmt.pix.bytesperline;
593 }
594
595 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
596}
597
6ffefff5
MK
598/**
599 * vpif_get_default_field() - Get default field type based on interface
600 * @vpif_params - ptr to vpif params
601 */
602static inline enum v4l2_field vpif_get_default_field(
603 struct vpif_interface *iface)
604{
605 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
606 V4L2_FIELD_INTERLACED;
607}
608
6ffefff5
MK
609/**
610 * vpif_config_addr() - function to configure buffer address in vpif
611 * @ch - channel ptr
612 * @muxmode - channel mux mode
613 */
614static void vpif_config_addr(struct channel_obj *ch, int muxmode)
615{
616 struct common_obj *common;
617
618 vpif_dbg(2, debug, "vpif_config_addr\n");
619
620 common = &(ch->common[VPIF_VIDEO_INDEX]);
621
622 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
623 common->set_addr = ch1_set_videobuf_addr;
624 else if (2 == muxmode)
625 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
626 else
627 common->set_addr = ch0_set_videobuf_addr;
628}
629
6ffefff5 630/**
178cce12
HV
631 * vpif_input_to_subdev() - Maps input to sub device
632 * @vpif_cfg - global config ptr
633 * @chan_cfg - channel config ptr
6ffefff5 634 * @input_index - Given input index from application
6ffefff5
MK
635 *
636 * lookup the sub device information for a given input index.
637 * we report all the inputs to application. inputs table also
638 * has sub device name for the each input
639 */
178cce12
HV
640static int vpif_input_to_subdev(
641 struct vpif_capture_config *vpif_cfg,
642 struct vpif_capture_chan_config *chan_cfg,
643 int input_index)
6ffefff5 644{
178cce12
HV
645 struct vpif_subdev_info *subdev_info;
646 const char *subdev_name;
6ffefff5
MK
647 int i;
648
178cce12 649 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
6ffefff5 650
178cce12
HV
651 subdev_name = chan_cfg->inputs[input_index].subdev_name;
652 if (subdev_name == NULL)
653 return -1;
6ffefff5
MK
654
655 /* loop through the sub device list to get the sub device info */
656 for (i = 0; i < vpif_cfg->subdev_count; i++) {
657 subdev_info = &vpif_cfg->subdev_info[i];
658 if (!strcmp(subdev_info->name, subdev_name))
178cce12 659 return i;
6ffefff5 660 }
178cce12
HV
661 return -1;
662}
6ffefff5 663
178cce12
HV
664/**
665 * vpif_set_input() - Select an input
666 * @vpif_cfg - global config ptr
667 * @ch - channel
668 * @_index - Given input index from application
669 *
670 * Select the given input.
671 */
672static int vpif_set_input(
673 struct vpif_capture_config *vpif_cfg,
674 struct channel_obj *ch,
675 int index)
676{
677 struct vpif_capture_chan_config *chan_cfg =
678 &vpif_cfg->chan_config[ch->channel_id];
679 struct vpif_subdev_info *subdev_info = NULL;
680 struct v4l2_subdev *sd = NULL;
681 u32 input = 0, output = 0;
682 int sd_index;
683 int ret;
6ffefff5 684
178cce12
HV
685 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
686 if (sd_index >= 0) {
687 sd = vpif_obj.sd[sd_index];
688 subdev_info = &vpif_cfg->subdev_info[sd_index];
689 }
6ffefff5 690
178cce12
HV
691 /* first setup input path from sub device to vpif */
692 if (sd && vpif_cfg->setup_input_path) {
693 ret = vpif_cfg->setup_input_path(ch->channel_id,
694 subdev_info->name);
695 if (ret < 0) {
696 vpif_dbg(1, debug, "couldn't setup input path for the" \
697 " sub device %s, for input index %d\n",
698 subdev_info->name, index);
699 return ret;
700 }
701 }
702
703 if (sd) {
704 input = chan_cfg->inputs[index].input_route;
705 output = chan_cfg->inputs[index].output_route;
706 ret = v4l2_subdev_call(sd, video, s_routing,
707 input, output, 0);
708 if (ret < 0 && ret != -ENOIOCTLCMD) {
709 vpif_dbg(1, debug, "Failed to set input\n");
710 return ret;
711 }
712 }
713 ch->input_idx = index;
714 ch->sd = sd;
715 /* copy interface parameters to vpif */
0d4f35f3 716 ch->vpifparams.iface = chan_cfg->vpif_if;
178cce12
HV
717
718 /* update tvnorms from the sub device input info */
8eef6669 719 ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
178cce12 720 return 0;
6ffefff5
MK
721}
722
723/**
724 * vpif_querystd() - querystd handler
725 * @file: file ptr
726 * @priv: file handle
727 * @std_id: ptr to std id
728 *
729 * This function is called to detect standard at the selected input
730 */
731static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
732{
7ff51197
LP
733 struct video_device *vdev = video_devdata(file);
734 struct channel_obj *ch = video_get_drvdata(vdev);
6ffefff5
MK
735 int ret = 0;
736
737 vpif_dbg(2, debug, "vpif_querystd\n");
738
6ffefff5 739 /* Call querystd function of decoder device */
178cce12 740 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
6ffefff5 741
178cce12
HV
742 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
743 return -ENODATA;
744 if (ret) {
745 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
746 return ret;
747 }
748
749 return 0;
6ffefff5
MK
750}
751
752/**
753 * vpif_g_std() - get STD handler
754 * @file: file ptr
755 * @priv: file handle
756 * @std_id: ptr to std id
757 */
758static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
759{
d557b7d5 760 struct vpif_capture_config *config = vpif_dev->platform_data;
7ff51197
LP
761 struct video_device *vdev = video_devdata(file);
762 struct channel_obj *ch = video_get_drvdata(vdev);
d557b7d5
LP
763 struct vpif_capture_chan_config *chan_cfg;
764 struct v4l2_input input;
6ffefff5
MK
765
766 vpif_dbg(2, debug, "vpif_g_std\n");
767
d557b7d5
LP
768 if (config->chan_config[ch->channel_id].inputs == NULL)
769 return -ENODATA;
770
771 chan_cfg = &config->chan_config[ch->channel_id];
772 input = chan_cfg->inputs[ch->input_idx].input;
773 if (input.capabilities != V4L2_IN_CAP_STD)
774 return -ENODATA;
775
6ffefff5
MK
776 *std = ch->video.stdid;
777 return 0;
778}
779
780/**
781 * vpif_s_std() - set STD handler
782 * @file: file ptr
783 * @priv: file handle
784 * @std_id: ptr to std id
785 */
314527ac 786static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
6ffefff5 787{
d557b7d5 788 struct vpif_capture_config *config = vpif_dev->platform_data;
7ff51197
LP
789 struct video_device *vdev = video_devdata(file);
790 struct channel_obj *ch = video_get_drvdata(vdev);
6ffefff5 791 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
d557b7d5
LP
792 struct vpif_capture_chan_config *chan_cfg;
793 struct v4l2_input input;
794 int ret;
6ffefff5
MK
795
796 vpif_dbg(2, debug, "vpif_s_std\n");
797
d557b7d5
LP
798 if (config->chan_config[ch->channel_id].inputs == NULL)
799 return -ENODATA;
800
801 chan_cfg = &config->chan_config[ch->channel_id];
802 input = chan_cfg->inputs[ch->input_idx].input;
803 if (input.capabilities != V4L2_IN_CAP_STD)
804 return -ENODATA;
805
c66238f3 806 if (vb2_is_busy(&common->buffer_queue))
6ffefff5 807 return -EBUSY;
6ffefff5 808
6ffefff5 809 /* Call encoder subdevice function to set the standard */
314527ac 810 ch->video.stdid = std_id;
0598c17b 811 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
6ffefff5
MK
812
813 /* Get the information about the standard */
814 if (vpif_update_std_info(ch)) {
6ffefff5 815 vpif_err("Error getting the standard info\n");
46656afa 816 return -EINVAL;
6ffefff5
MK
817 }
818
6ffefff5 819 /* set standard in the sub device */
8774bed9 820 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
178cce12 821 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
6ffefff5 822 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
178cce12
HV
823 return ret;
824 }
825 return 0;
6ffefff5
MK
826}
827
828/**
829 * vpif_enum_input() - ENUMINPUT handler
830 * @file: file ptr
831 * @priv: file handle
832 * @input: ptr to input structure
833 */
834static int vpif_enum_input(struct file *file, void *priv,
835 struct v4l2_input *input)
836{
837
838 struct vpif_capture_config *config = vpif_dev->platform_data;
7ff51197
LP
839 struct video_device *vdev = video_devdata(file);
840 struct channel_obj *ch = video_get_drvdata(vdev);
6ffefff5 841 struct vpif_capture_chan_config *chan_cfg;
6ffefff5
MK
842
843 chan_cfg = &config->chan_config[ch->channel_id];
844
a4965c59 845 if (input->index >= chan_cfg->input_count)
6ffefff5 846 return -EINVAL;
6ffefff5
MK
847
848 memcpy(input, &chan_cfg->inputs[input->index].input,
849 sizeof(*input));
850 return 0;
851}
852
853/**
854 * vpif_g_input() - Get INPUT handler
855 * @file: file ptr
856 * @priv: file handle
857 * @index: ptr to input index
858 */
859static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
860{
7ff51197
LP
861 struct video_device *vdev = video_devdata(file);
862 struct channel_obj *ch = video_get_drvdata(vdev);
6ffefff5 863
6f47c6c6 864 *index = ch->input_idx;
6ffefff5
MK
865 return 0;
866}
867
868/**
869 * vpif_s_input() - Set INPUT handler
870 * @file: file ptr
871 * @priv: file handle
872 * @index: input index
873 */
874static int vpif_s_input(struct file *file, void *priv, unsigned int index)
875{
876 struct vpif_capture_config *config = vpif_dev->platform_data;
7ff51197
LP
877 struct video_device *vdev = video_devdata(file);
878 struct channel_obj *ch = video_get_drvdata(vdev);
6ffefff5 879 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
7ff51197 880 struct vpif_capture_chan_config *chan_cfg;
6ffefff5
MK
881
882 chan_cfg = &config->chan_config[ch->channel_id];
883
7aaad131
HV
884 if (index >= chan_cfg->input_count)
885 return -EINVAL;
886
c66238f3 887 if (vb2_is_busy(&common->buffer_queue))
6ffefff5 888 return -EBUSY;
6ffefff5 889
178cce12 890 return vpif_set_input(config, ch, index);
6ffefff5
MK
891}
892
893/**
894 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
895 * @file: file ptr
896 * @priv: file handle
897 * @index: input index
898 */
899static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
900 struct v4l2_fmtdesc *fmt)
901{
7ff51197
LP
902 struct video_device *vdev = video_devdata(file);
903 struct channel_obj *ch = video_get_drvdata(vdev);
6ffefff5
MK
904
905 if (fmt->index != 0) {
906 vpif_dbg(1, debug, "Invalid format index\n");
907 return -EINVAL;
908 }
909
910 /* Fill in the information about format */
911 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
912 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
913 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
914 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
915 } else {
916 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
917 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
918 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
919 }
920 return 0;
921}
922
923/**
924 * vpif_try_fmt_vid_cap() - TRY_FMT handler
925 * @file: file ptr
926 * @priv: file handle
927 * @fmt: ptr to v4l2 format structure
928 */
929static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
930 struct v4l2_format *fmt)
931{
7ff51197
LP
932 struct video_device *vdev = video_devdata(file);
933 struct channel_obj *ch = video_get_drvdata(vdev);
6ffefff5 934 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
a4965c59
LP
935 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
936 struct vpif_params *vpif_params = &ch->vpifparams;
937
938 /*
939 * to supress v4l-compliance warnings silently correct
940 * the pixelformat
941 */
942 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
943 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8)
944 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
945 } else {
946 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
947 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
948 }
949
950 common->fmt.fmt.pix.pixelformat = pixfmt->pixelformat;
6ffefff5 951
a4965c59
LP
952 vpif_update_std_info(ch);
953
954 pixfmt->field = common->fmt.fmt.pix.field;
955 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
956 pixfmt->bytesperline = common->fmt.fmt.pix.width;
957 pixfmt->width = common->fmt.fmt.pix.width;
958 pixfmt->height = common->fmt.fmt.pix.height;
959 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
960 pixfmt->priv = 0;
961
962 return 0;
6ffefff5
MK
963}
964
965
966/**
967 * vpif_g_fmt_vid_cap() - Set INPUT handler
968 * @file: file ptr
969 * @priv: file handle
970 * @fmt: ptr to v4l2 format structure
971 */
972static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
973 struct v4l2_format *fmt)
974{
7ff51197
LP
975 struct video_device *vdev = video_devdata(file);
976 struct channel_obj *ch = video_get_drvdata(vdev);
6ffefff5
MK
977 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
978
979 /* Check the validity of the buffer type */
980 if (common->fmt.type != fmt->type)
981 return -EINVAL;
982
983 /* Fill in the information about format */
6ffefff5 984 *fmt = common->fmt;
6ffefff5
MK
985 return 0;
986}
987
988/**
989 * vpif_s_fmt_vid_cap() - Set FMT handler
990 * @file: file ptr
991 * @priv: file handle
992 * @fmt: ptr to v4l2 format structure
993 */
994static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
995 struct v4l2_format *fmt)
996{
7ff51197
LP
997 struct video_device *vdev = video_devdata(file);
998 struct channel_obj *ch = video_get_drvdata(vdev);
6ffefff5 999 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
a4965c59 1000 int ret;
6ffefff5 1001
2c0ddd17 1002 vpif_dbg(2, debug, "%s\n", __func__);
6ffefff5 1003
c66238f3 1004 if (vb2_is_busy(&common->buffer_queue))
6ffefff5 1005 return -EBUSY;
6ffefff5 1006
a4965c59 1007 ret = vpif_try_fmt_vid_cap(file, priv, fmt);
6ffefff5
MK
1008 if (ret)
1009 return ret;
a4965c59 1010
6ffefff5 1011 /* store the format in the channel object */
6ffefff5 1012 common->fmt = *fmt;
6ffefff5
MK
1013 return 0;
1014}
1015
1016/**
1017 * vpif_querycap() - QUERYCAP handler
1018 * @file: file ptr
1019 * @priv: file handle
1020 * @cap: ptr to v4l2_capability structure
1021 */
1022static int vpif_querycap(struct file *file, void *priv,
1023 struct v4l2_capability *cap)
1024{
1025 struct vpif_capture_config *config = vpif_dev->platform_data;
1026
626d533f
LP
1027 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1028 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
e75ea0c1 1029 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
626d533f
LP
1030 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1031 dev_name(vpif_dev));
6ffefff5
MK
1032 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1033
1034 return 0;
1035}
1036
40c8bcea 1037/**
0598c17b 1038 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
40c8bcea
MR
1039 * @file: file ptr
1040 * @priv: file handle
0598c17b 1041 * @timings: input timings
40c8bcea 1042 */
0598c17b
HV
1043static int
1044vpif_enum_dv_timings(struct file *file, void *priv,
1045 struct v4l2_enum_dv_timings *timings)
40c8bcea 1046{
1e8852af 1047 struct vpif_capture_config *config = vpif_dev->platform_data;
7ff51197
LP
1048 struct video_device *vdev = video_devdata(file);
1049 struct channel_obj *ch = video_get_drvdata(vdev);
1e8852af
LP
1050 struct vpif_capture_chan_config *chan_cfg;
1051 struct v4l2_input input;
178cce12 1052 int ret;
40c8bcea 1053
1e8852af
LP
1054 if (config->chan_config[ch->channel_id].inputs == NULL)
1055 return -ENODATA;
1056
1057 chan_cfg = &config->chan_config[ch->channel_id];
1058 input = chan_cfg->inputs[ch->input_idx].input;
1059 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1060 return -ENODATA;
1061
10d2146d
LP
1062 timings->pad = 0;
1063
1064 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
e070f1b4 1065 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
178cce12 1066 return -EINVAL;
1e8852af 1067
178cce12 1068 return ret;
40c8bcea
MR
1069}
1070
1071/**
0598c17b 1072 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
40c8bcea
MR
1073 * @file: file ptr
1074 * @priv: file handle
0598c17b 1075 * @timings: input timings
40c8bcea 1076 */
0598c17b
HV
1077static int
1078vpif_query_dv_timings(struct file *file, void *priv,
1079 struct v4l2_dv_timings *timings)
40c8bcea 1080{
1e8852af 1081 struct vpif_capture_config *config = vpif_dev->platform_data;
7ff51197
LP
1082 struct video_device *vdev = video_devdata(file);
1083 struct channel_obj *ch = video_get_drvdata(vdev);
1e8852af
LP
1084 struct vpif_capture_chan_config *chan_cfg;
1085 struct v4l2_input input;
178cce12 1086 int ret;
40c8bcea 1087
1e8852af
LP
1088 if (config->chan_config[ch->channel_id].inputs == NULL)
1089 return -ENODATA;
1090
1091 chan_cfg = &config->chan_config[ch->channel_id];
1092 input = chan_cfg->inputs[ch->input_idx].input;
1093 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1094 return -ENODATA;
1095
178cce12 1096 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
e070f1b4 1097 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
178cce12 1098 return -ENODATA;
1e8852af 1099
178cce12 1100 return ret;
40c8bcea
MR
1101}
1102
c027e165
MR
1103/**
1104 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1105 * @file: file ptr
1106 * @priv: file handle
1107 * @timings: digital video timings
1108 */
1109static int vpif_s_dv_timings(struct file *file, void *priv,
1110 struct v4l2_dv_timings *timings)
1111{
1e8852af 1112 struct vpif_capture_config *config = vpif_dev->platform_data;
7ff51197
LP
1113 struct video_device *vdev = video_devdata(file);
1114 struct channel_obj *ch = video_get_drvdata(vdev);
c027e165
MR
1115 struct vpif_params *vpifparams = &ch->vpifparams;
1116 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1e8852af 1117 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
c027e165 1118 struct video_obj *vid_ch = &ch->video;
0598c17b 1119 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1e8852af
LP
1120 struct vpif_capture_chan_config *chan_cfg;
1121 struct v4l2_input input;
c027e165
MR
1122 int ret;
1123
1e8852af
LP
1124 if (config->chan_config[ch->channel_id].inputs == NULL)
1125 return -ENODATA;
1126
1127 chan_cfg = &config->chan_config[ch->channel_id];
1128 input = chan_cfg->inputs[ch->input_idx].input;
1129 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1130 return -ENODATA;
1131
c027e165
MR
1132 if (timings->type != V4L2_DV_BT_656_1120) {
1133 vpif_dbg(2, debug, "Timing type not defined\n");
1134 return -EINVAL;
1135 }
1136
1e8852af
LP
1137 if (vb2_is_busy(&common->buffer_queue))
1138 return -EBUSY;
1139
c027e165 1140 /* Configure subdevice timings, if any */
178cce12
HV
1141 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1142 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1143 ret = 0;
c027e165
MR
1144 if (ret < 0) {
1145 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1146 return ret;
1147 }
1148
1149 if (!(timings->bt.width && timings->bt.height &&
1150 (timings->bt.hbackporch ||
1151 timings->bt.hfrontporch ||
1152 timings->bt.hsync) &&
1153 timings->bt.vfrontporch &&
1154 (timings->bt.vbackporch ||
1155 timings->bt.vsync))) {
1156 vpif_dbg(2, debug, "Timings for width, height, "
1157 "horizontal back porch, horizontal sync, "
1158 "horizontal front porch, vertical back porch, "
1159 "vertical sync and vertical back porch "
1160 "must be defined\n");
1161 return -EINVAL;
1162 }
1163
0598c17b 1164 vid_ch->dv_timings = *timings;
c027e165
MR
1165
1166 /* Configure video port timings */
1167
e3655268 1168 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
c027e165
MR
1169 std_info->sav2eav = bt->width;
1170
1171 std_info->l1 = 1;
1172 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1173
e3655268 1174 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
c027e165
MR
1175 if (bt->interlaced) {
1176 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
c027e165
MR
1177 std_info->l5 = std_info->vsize/2 -
1178 (bt->vfrontporch - 1);
1179 std_info->l7 = std_info->vsize/2 + 1;
1180 std_info->l9 = std_info->l7 + bt->il_vsync +
1181 bt->il_vbackporch + 1;
1182 std_info->l11 = std_info->vsize -
1183 (bt->il_vfrontporch - 1);
1184 } else {
1185 vpif_dbg(2, debug, "Required timing values for "
1186 "interlaced BT format missing\n");
1187 return -EINVAL;
1188 }
1189 } else {
c027e165
MR
1190 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1191 }
1192 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1193 std_info->width = bt->width;
1194 std_info->height = bt->height;
1195 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1196 std_info->ycmux_mode = 0;
1197 std_info->capture_format = 0;
1198 std_info->vbi_supported = 0;
1199 std_info->hd_sd = 1;
1200 std_info->stdid = 0;
c027e165
MR
1201
1202 vid_ch->stdid = 0;
c027e165
MR
1203 return 0;
1204}
1205
1206/**
1207 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1208 * @file: file ptr
1209 * @priv: file handle
1210 * @timings: digital video timings
1211 */
1212static int vpif_g_dv_timings(struct file *file, void *priv,
1213 struct v4l2_dv_timings *timings)
1214{
1e8852af 1215 struct vpif_capture_config *config = vpif_dev->platform_data;
7ff51197
LP
1216 struct video_device *vdev = video_devdata(file);
1217 struct channel_obj *ch = video_get_drvdata(vdev);
c027e165 1218 struct video_obj *vid_ch = &ch->video;
1e8852af
LP
1219 struct vpif_capture_chan_config *chan_cfg;
1220 struct v4l2_input input;
1221
1222 if (config->chan_config[ch->channel_id].inputs == NULL)
1223 return -ENODATA;
1224
1225 chan_cfg = &config->chan_config[ch->channel_id];
1226 input = chan_cfg->inputs[ch->input_idx].input;
1227 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1228 return -ENODATA;
c027e165 1229
0598c17b 1230 *timings = vid_ch->dv_timings;
c027e165
MR
1231
1232 return 0;
1233}
1234
7036d6a7
MR
1235/*
1236 * vpif_log_status() - Status information
1237 * @file: file ptr
1238 * @priv: file handle
1239 *
1240 * Returns zero.
1241 */
1242static int vpif_log_status(struct file *filep, void *priv)
1243{
1244 /* status for sub devices */
1245 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1246
1247 return 0;
1248}
1249
6ffefff5
MK
1250/* vpif capture ioctl operations */
1251static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
7b4657fa 1252 .vidioc_querycap = vpif_querycap,
6ffefff5 1253 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
7b4657fa 1254 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
6ffefff5
MK
1255 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1256 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
7b4657fa 1257
6ffefff5
MK
1258 .vidioc_enum_input = vpif_enum_input,
1259 .vidioc_s_input = vpif_s_input,
1260 .vidioc_g_input = vpif_g_input,
d9a3b13c
LP
1261
1262 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1263 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1264 .vidioc_querybuf = vb2_ioctl_querybuf,
1265 .vidioc_qbuf = vb2_ioctl_qbuf,
1266 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1267 .vidioc_expbuf = vb2_ioctl_expbuf,
1268 .vidioc_streamon = vb2_ioctl_streamon,
1269 .vidioc_streamoff = vb2_ioctl_streamoff,
1270
6ffefff5 1271 .vidioc_querystd = vpif_querystd,
7b4657fa 1272 .vidioc_s_std = vpif_s_std,
6ffefff5 1273 .vidioc_g_std = vpif_g_std,
d9a3b13c 1274
7b4657fa
LP
1275 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1276 .vidioc_query_dv_timings = vpif_query_dv_timings,
1277 .vidioc_s_dv_timings = vpif_s_dv_timings,
1278 .vidioc_g_dv_timings = vpif_g_dv_timings,
1279
7036d6a7 1280 .vidioc_log_status = vpif_log_status,
6ffefff5
MK
1281};
1282
1283/* vpif file operations */
1284static struct v4l2_file_operations vpif_fops = {
1285 .owner = THIS_MODULE,
7ff51197
LP
1286 .open = v4l2_fh_open,
1287 .release = vb2_fop_release,
46656afa 1288 .unlocked_ioctl = video_ioctl2,
d26d26b5
LP
1289 .mmap = vb2_fop_mmap,
1290 .poll = vb2_fop_poll
6ffefff5
MK
1291};
1292
6ffefff5
MK
1293/**
1294 * initialize_vpif() - Initialize vpif data structures
1295 *
1296 * Allocate memory for data structures and initialize them
1297 */
1298static int initialize_vpif(void)
1299{
4015bef6 1300 int err, i, j;
6ffefff5
MK
1301 int free_channel_objects_index;
1302
6ffefff5
MK
1303 /* Allocate memory for six channel objects */
1304 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1305 vpif_obj.dev[i] =
1306 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1307 /* If memory allocation fails, return error */
1308 if (!vpif_obj.dev[i]) {
1309 free_channel_objects_index = i;
1310 err = -ENOMEM;
1311 goto vpif_init_free_channel_objects;
1312 }
1313 }
1314 return 0;
1315
1316vpif_init_free_channel_objects:
1317 for (j = 0; j < free_channel_objects_index; j++)
1318 kfree(vpif_obj.dev[j]);
1319 return err;
1320}
1321
873229e4
LP
1322static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1323 struct v4l2_subdev *subdev,
1324 struct v4l2_async_subdev *asd)
1325{
1326 int i;
1327
1328 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1329 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1330 subdev->name)) {
1331 vpif_obj.sd[i] = subdev;
1332 return 0;
1333 }
1334
1335 return -EINVAL;
1336}
1337
1338static int vpif_probe_complete(void)
1339{
1340 struct common_obj *common;
d26d26b5 1341 struct video_device *vdev;
873229e4 1342 struct channel_obj *ch;
fa09accc 1343 struct vb2_queue *q;
8eef6669 1344 int j, err, k;
873229e4
LP
1345
1346 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1347 ch = vpif_obj.dev[j];
1348 ch->channel_id = j;
1349 common = &(ch->common[VPIF_VIDEO_INDEX]);
1350 spin_lock_init(&common->irqlock);
1351 mutex_init(&common->lock);
873229e4
LP
1352
1353 /* select input 0 */
1354 err = vpif_set_input(vpif_obj.config, ch, 0);
1355 if (err)
1356 goto probe_out;
1357
a4965c59
LP
1358 /* set initial format */
1359 ch->video.stdid = V4L2_STD_525_60;
1360 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1361 vpif_update_std_info(ch);
1362
fa09accc
LP
1363 /* Initialize vb2 queue */
1364 q = &common->buffer_queue;
1365 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1366 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1367 q->drv_priv = ch;
1368 q->ops = &video_qops;
1369 q->mem_ops = &vb2_dma_contig_memops;
1370 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1371 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1372 q->min_buffers_needed = 1;
999ba69b 1373 q->lock = &common->lock;
fa09accc
LP
1374
1375 err = vb2_queue_init(q);
1376 if (err) {
1377 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1378 goto probe_out;
1379 }
1380
1381 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1382 if (IS_ERR(common->alloc_ctx)) {
1383 vpif_err("Failed to get the context\n");
1384 err = PTR_ERR(common->alloc_ctx);
1385 goto probe_out;
1386 }
1387
1388 INIT_LIST_HEAD(&common->dma_queue);
1389
e75ea0c1 1390 /* Initialize the video_device structure */
8eef6669 1391 vdev = &ch->video_dev;
e75ea0c1 1392 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
8eef6669 1393 vdev->release = video_device_release_empty;
e75ea0c1
LP
1394 vdev->fops = &vpif_fops;
1395 vdev->ioctl_ops = &vpif_ioctl_ops;
1396 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1397 vdev->vfl_dir = VFL_DIR_RX;
d26d26b5 1398 vdev->queue = q;
7ff51197 1399 vdev->lock = &common->lock;
8eef6669 1400 video_set_drvdata(&ch->video_dev, ch);
d26d26b5 1401 err = video_register_device(vdev,
873229e4
LP
1402 VFL_TYPE_GRABBER, (j ? 1 : 0));
1403 if (err)
1404 goto probe_out;
1405 }
1406
1407 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1408 return 0;
1409
1410probe_out:
1411 for (k = 0; k < j; k++) {
1412 /* Get the pointer to the channel object */
1413 ch = vpif_obj.dev[k];
fa09accc
LP
1414 common = &ch->common[k];
1415 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
873229e4 1416 /* Unregister video device */
8eef6669 1417 video_unregister_device(&ch->video_dev);
873229e4
LP
1418 }
1419 kfree(vpif_obj.sd);
873229e4
LP
1420 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1421
1422 return err;
1423}
1424
1425static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1426{
1427 return vpif_probe_complete();
1428}
1429
6ffefff5
MK
1430/**
1431 * vpif_probe : This function probes the vpif capture driver
1432 * @pdev: platform device pointer
1433 *
1434 * This creates device entries by register itself to the V4L2 driver and
1435 * initializes fields of each channel objects
1436 */
1437static __init int vpif_probe(struct platform_device *pdev)
1438{
1439 struct vpif_subdev_info *subdevdata;
6ffefff5 1440 struct i2c_adapter *i2c_adap;
6ffefff5
MK
1441 struct resource *res;
1442 int subdev_count;
8eef6669
LP
1443 int res_idx = 0;
1444 int i, err;
6ffefff5
MK
1445
1446 vpif_dev = &pdev->dev;
1447
1448 err = initialize_vpif();
1449 if (err) {
1450 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1451 return err;
1452 }
1453
d2f7a1a2
HV
1454 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1455 if (err) {
1456 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1457 return err;
1458 }
1459
5be452c3 1460 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
a76a0b33 1461 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
e75ea0c1 1462 IRQF_SHARED, VPIF_DRIVER_NAME,
a76a0b33
LP
1463 (void *)(&vpif_obj.dev[res_idx]->
1464 channel_id));
1465 if (err) {
1466 err = -EINVAL;
1467 goto vpif_unregister;
6ffefff5 1468 }
5be452c3 1469 res_idx++;
6ffefff5
MK
1470 }
1471
873229e4 1472 vpif_obj.config = pdev->dev.platform_data;
6ffefff5 1473
873229e4 1474 subdev_count = vpif_obj.config->subdev_count;
1f8766b4 1475 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
6ffefff5
MK
1476 GFP_KERNEL);
1477 if (vpif_obj.sd == NULL) {
1478 vpif_err("unable to allocate memory for subdevice pointers\n");
1479 err = -ENOMEM;
8eef6669 1480 goto vpif_unregister;
6ffefff5
MK
1481 }
1482
873229e4
LP
1483 if (!vpif_obj.config->asd_sizes) {
1484 i2c_adap = i2c_get_adapter(1);
1485 for (i = 0; i < subdev_count; i++) {
1486 subdevdata = &vpif_obj.config->subdev_info[i];
1487 vpif_obj.sd[i] =
1488 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1489 i2c_adap,
1490 &subdevdata->
1491 board_info,
1492 NULL);
1493
1494 if (!vpif_obj.sd[i]) {
1495 vpif_err("Error registering v4l2 subdevice\n");
2fcd9dc3 1496 err = -ENODEV;
873229e4
LP
1497 goto probe_subdev_out;
1498 }
1499 v4l2_info(&vpif_obj.v4l2_dev,
1500 "registered sub device %s\n",
1501 subdevdata->name);
1502 }
1503 vpif_probe_complete();
1504 } else {
e8419d08 1505 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
873229e4
LP
1506 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1507 vpif_obj.notifier.bound = vpif_async_bound;
1508 vpif_obj.notifier.complete = vpif_async_complete;
1509 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1510 &vpif_obj.notifier);
1511 if (err) {
1512 vpif_err("Error registering async notifier\n");
1513 err = -EINVAL;
6ffefff5
MK
1514 goto probe_subdev_out;
1515 }
6ffefff5 1516 }
6ffefff5
MK
1517
1518 return 0;
1519
b65814e5
HV
1520probe_subdev_out:
1521 /* free sub devices memory */
1522 kfree(vpif_obj.sd);
b2de4f25 1523vpif_unregister:
d2f7a1a2 1524 v4l2_device_unregister(&vpif_obj.v4l2_dev);
b2de4f25 1525
6ffefff5
MK
1526 return err;
1527}
1528
1529/**
1530 * vpif_remove() - driver remove handler
1531 * @device: ptr to platform device structure
1532 *
1533 * The vidoe device is unregistered
1534 */
1535static int vpif_remove(struct platform_device *device)
1536{
fa09accc 1537 struct common_obj *common;
6ffefff5 1538 struct channel_obj *ch;
b2de4f25 1539 int i;
6ffefff5
MK
1540
1541 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1542
410ca605 1543 kfree(vpif_obj.sd);
6ffefff5
MK
1544 /* un-register device */
1545 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1546 /* Get the pointer to the channel object */
1547 ch = vpif_obj.dev[i];
e9b5872c 1548 common = &ch->common[VPIF_VIDEO_INDEX];
fa09accc 1549 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
6ffefff5 1550 /* Unregister video device */
8eef6669 1551 video_unregister_device(&ch->video_dev);
410ca605 1552 kfree(vpif_obj.dev[i]);
6ffefff5
MK
1553 }
1554 return 0;
1555}
1556
b7047713 1557#ifdef CONFIG_PM_SLEEP
6ffefff5
MK
1558/**
1559 * vpif_suspend: vpif device suspend
6ffefff5 1560 */
3d5946dc 1561static int vpif_suspend(struct device *dev)
6ffefff5 1562{
3d5946dc
MH
1563
1564 struct common_obj *common;
1565 struct channel_obj *ch;
1566 int i;
1567
1568 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1569 /* Get the pointer to the channel object */
1570 ch = vpif_obj.dev[i];
1571 common = &ch->common[VPIF_VIDEO_INDEX];
b7047713 1572
c54d4a0b 1573 if (!vb2_start_streaming_called(&common->buffer_queue))
b7047713
LP
1574 continue;
1575
3d5946dc 1576 mutex_lock(&common->lock);
b7047713
LP
1577 /* Disable channel */
1578 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1579 enable_channel0(0);
1580 channel0_intr_enable(0);
1581 }
1582 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1583 ycmux_mode == 2) {
1584 enable_channel1(0);
1585 channel1_intr_enable(0);
3d5946dc
MH
1586 }
1587 mutex_unlock(&common->lock);
1588 }
1589
1590 return 0;
6ffefff5
MK
1591}
1592
3d5946dc 1593/*
6ffefff5 1594 * vpif_resume: vpif device suspend
6ffefff5 1595 */
3d5946dc 1596static int vpif_resume(struct device *dev)
6ffefff5 1597{
3d5946dc
MH
1598 struct common_obj *common;
1599 struct channel_obj *ch;
1600 int i;
1601
1602 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1603 /* Get the pointer to the channel object */
1604 ch = vpif_obj.dev[i];
1605 common = &ch->common[VPIF_VIDEO_INDEX];
b7047713 1606
c54d4a0b 1607 if (!vb2_start_streaming_called(&common->buffer_queue))
b7047713
LP
1608 continue;
1609
3d5946dc 1610 mutex_lock(&common->lock);
b7047713
LP
1611 /* Enable channel */
1612 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1613 enable_channel0(1);
1614 channel0_intr_enable(1);
1615 }
1616 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1617 ycmux_mode == 2) {
1618 enable_channel1(1);
1619 channel1_intr_enable(1);
3d5946dc
MH
1620 }
1621 mutex_unlock(&common->lock);
1622 }
1623
1624 return 0;
6ffefff5 1625}
3d5946dc
MH
1626#endif
1627
b7047713
LP
1628static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1629
ffa1b391 1630static __refdata struct platform_driver vpif_driver = {
6ffefff5 1631 .driver = {
e75ea0c1 1632 .name = VPIF_DRIVER_NAME,
b7047713 1633 .pm = &vpif_pm_ops,
6ffefff5
MK
1634 },
1635 .probe = vpif_probe,
1636 .remove = vpif_remove,
1637};
1638
fe424b2f 1639module_platform_driver(vpif_driver);