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