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