[media] media: videobuf2: Change queue_setup argument
[deliverable/linux.git] / drivers / media / platform / davinci / vpif_display.c
CommitLineData
e7332e3a
C
1/*
2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
4 *
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
96787eb4 6 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
e7332e3a
C
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
e7332e3a 18#include <linux/interrupt.h>
012eef70 19#include <linux/module.h>
e7332e3a 20#include <linux/platform_device.h>
5a0e3ad6 21#include <linux/slab.h>
e7332e3a 22
012eef70 23#include <media/v4l2-ioctl.h>
e7332e3a 24
e7332e3a 25#include "vpif.h"
012eef70 26#include "vpif_display.h"
e7332e3a
C
27
28MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
29MODULE_LICENSE("GPL");
64dc3c1a 30MODULE_VERSION(VPIF_DISPLAY_VERSION);
e7332e3a 31
0a63172a 32#define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
e7332e3a
C
33
34#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
35#define vpif_dbg(level, debug, fmt, arg...) \
36 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
37
38static int debug = 1;
e7332e3a
C
39
40module_param(debug, int, 0644);
e7332e3a
C
41
42MODULE_PARM_DESC(debug, "Debug level 0-1");
e7332e3a 43
76c4c2be 44#define VPIF_DRIVER_NAME "vpif_display"
2f5851b5
LP
45
46/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
47static int ycmux_mode;
48
58334b00
LP
49static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
50
e7332e3a
C
51static struct vpif_device vpif_obj = { {NULL} };
52static struct device *vpif_dev;
2401dd25
LP
53static void vpif_calculate_offsets(struct channel_obj *ch);
54static void vpif_config_addr(struct channel_obj *ch, int muxmode);
e7332e3a 55
2d700715
JS
56static inline
57struct vpif_disp_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
58334b00
LP
58{
59 return container_of(vb, struct vpif_disp_buffer, vb);
60}
61
27cdf9ba
LP
62/**
63 * vpif_buffer_prepare : callback function for buffer prepare
64 * @vb: ptr to vb2_buffer
65 *
66 * This is the callback function for buffer prepare when vb2_qbuf()
67 * function is called. The buffer is prepared and user space virtual address
68 * or user address is converted into physical address
e7332e3a 69 */
2401dd25 70static int vpif_buffer_prepare(struct vb2_buffer *vb)
e7332e3a 71{
2d700715 72 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
27cdf9ba 73 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
e7332e3a 74 struct common_obj *common;
e7332e3a 75
a2b235cb 76 common = &ch->common[VPIF_VIDEO_INDEX];
27cdf9ba
LP
77
78 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
79 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
80 return -EINVAL;
81
2d700715 82 vbuf->field = common->fmt.fmt.pix.field;
27cdf9ba
LP
83
84 if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
85 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
86
87 if (!ISALIGNED(addr + common->ytop_off) ||
2401dd25
LP
88 !ISALIGNED(addr + common->ybtm_off) ||
89 !ISALIGNED(addr + common->ctop_off) ||
27cdf9ba
LP
90 !ISALIGNED(addr + common->cbtm_off)) {
91 vpif_err("buffer offset not aligned to 8 bytes\n");
92 return -EINVAL;
2401dd25 93 }
e7332e3a 94 }
e7332e3a 95
27cdf9ba 96 return 0;
e7332e3a
C
97}
98
172392a1
LP
99/**
100 * vpif_buffer_queue_setup : Callback function for buffer setup.
101 * @vq: vb2_queue ptr
102 * @fmt: v4l2 format
103 * @nbuffers: ptr to number of buffers requested by application
104 * @nplanes:: contains number of distinct video planes needed to hold a frame
105 * @sizes[]: contains the size (in bytes) of each plane.
106 * @alloc_ctxs: ptr to allocation context
107 *
108 * This callback function is called when reqbuf() is called to adjust
109 * the buffer count and buffer size
e7332e3a 110 */
2401dd25 111static int vpif_buffer_queue_setup(struct vb2_queue *vq,
33119e80 112 const void *parg,
2401dd25
LP
113 unsigned int *nbuffers, unsigned int *nplanes,
114 unsigned int sizes[], void *alloc_ctxs[])
e7332e3a 115{
33119e80 116 const struct v4l2_format *fmt = parg;
a2b235cb 117 struct channel_obj *ch = vb2_get_drv_priv(vq);
e7332e3a 118 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
fc613d44 119
172392a1
LP
120 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
121 return -EINVAL;
122
123 if (vq->num_buffers + *nbuffers < 3)
124 *nbuffers = 3 - vq->num_buffers;
e7332e3a 125
2401dd25 126 *nplanes = 1;
172392a1 127 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
2401dd25 128 alloc_ctxs[0] = common->alloc_ctx;
172392a1
LP
129
130 /* Calculate the offset for Y and C data in the buffer */
131 vpif_calculate_offsets(ch);
132
e7332e3a
C
133 return 0;
134}
135
58334b00
LP
136/**
137 * vpif_buffer_queue : Callback function to add buffer to DMA queue
138 * @vb: ptr to vb2_buffer
139 *
140 * This callback fucntion queues the buffer to DMA engine
e7332e3a 141 */
2401dd25 142static void vpif_buffer_queue(struct vb2_buffer *vb)
e7332e3a 143{
2d700715
JS
144 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
145 struct vpif_disp_buffer *buf = to_vpif_buffer(vbuf);
a2b235cb 146 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
e7332e3a 147 struct common_obj *common;
c4697d7f 148 unsigned long flags;
e7332e3a 149
2401dd25 150 common = &ch->common[VPIF_VIDEO_INDEX];
e7332e3a
C
151
152 /* add the buffer to the DMA queue */
c4697d7f 153 spin_lock_irqsave(&common->irqlock, flags);
2401dd25 154 list_add_tail(&buf->list, &common->dma_queue);
c4697d7f 155 spin_unlock_irqrestore(&common->irqlock, flags);
e7332e3a
C
156}
157
58334b00
LP
158/**
159 * vpif_start_streaming : Starts the DMA engine for streaming
160 * @vb: ptr to vb2_buffer
161 * @count: number of buffers
162 */
2401dd25
LP
163static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
164{
165 struct vpif_display_config *vpif_config_data =
166 vpif_dev->platform_data;
a2b235cb 167 struct channel_obj *ch = vb2_get_drv_priv(vq);
2401dd25
LP
168 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
169 struct vpif_params *vpif = &ch->vpifparams;
54a445a4
LP
170 struct vpif_disp_buffer *buf, *tmp;
171 unsigned long addr, flags;
2401dd25
LP
172 int ret;
173
c4697d7f 174 spin_lock_irqsave(&common->irqlock, flags);
2401dd25 175
2f5851b5 176 /* Initialize field_id */
2401dd25 177 ch->field_id = 0;
54a445a4 178
2401dd25 179 /* clock settings */
f4ad8d74
LP
180 if (vpif_config_data->set_clock) {
181 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
182 ycmux_mode, ch->vpifparams.std_info.hd_sd);
183 if (ret < 0) {
184 vpif_err("can't set clock\n");
54a445a4 185 goto err;
f4ad8d74 186 }
2401dd25
LP
187 }
188
189 /* set the parameters and addresses */
190 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
191 if (ret < 0)
54a445a4 192 goto err;
2401dd25 193
2f5851b5 194 ycmux_mode = ret;
2401dd25 195 vpif_config_addr(ch, ret);
54a445a4
LP
196 /* Get the next frame from the buffer queue */
197 common->next_frm = common->cur_frm =
198 list_entry(common->dma_queue.next,
199 struct vpif_disp_buffer, list);
200
201 list_del(&common->cur_frm->list);
202 spin_unlock_irqrestore(&common->irqlock, flags);
54a445a4 203
2d700715 204 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
2401dd25
LP
205 common->set_addr((addr + common->ytop_off),
206 (addr + common->ybtm_off),
207 (addr + common->ctop_off),
208 (addr + common->cbtm_off));
209
58334b00
LP
210 /*
211 * Set interrupt for both the fields in VPIF
212 * Register enable channel in VPIF register
213 */
9e18404a 214 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
2401dd25
LP
215 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
216 channel2_intr_assert();
217 channel2_intr_enable(1);
218 enable_channel2(1);
2bd4e58c 219 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
6964b103 220 channel2_clipping_enable(1);
2401dd25
LP
221 }
222
2f5851b5 223 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
2401dd25
LP
224 channel3_intr_assert();
225 channel3_intr_enable(1);
226 enable_channel3(1);
2bd4e58c 227 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
6964b103 228 channel3_clipping_enable(1);
2401dd25 229 }
2401dd25
LP
230
231 return 0;
54a445a4
LP
232
233err:
234 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
235 list_del(&buf->list);
2d700715 236 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
54a445a4 237 }
92491968 238 spin_unlock_irqrestore(&common->irqlock, flags);
54a445a4
LP
239
240 return ret;
2401dd25
LP
241}
242
58334b00
LP
243/**
244 * vpif_stop_streaming : Stop the DMA engine
245 * @vq: ptr to vb2_queue
246 *
247 * This callback stops the DMA engine and any remaining buffers
248 * in the DMA queue are released.
249 */
e37559b2 250static void vpif_stop_streaming(struct vb2_queue *vq)
2401dd25 251{
a2b235cb 252 struct channel_obj *ch = vb2_get_drv_priv(vq);
2401dd25 253 struct common_obj *common;
c4697d7f 254 unsigned long flags;
2401dd25 255
2401dd25
LP
256 common = &ch->common[VPIF_VIDEO_INDEX];
257
18c7adcf
LP
258 /* Disable channel */
259 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
260 enable_channel2(0);
261 channel2_intr_enable(0);
262 }
2f5851b5 263 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
18c7adcf
LP
264 enable_channel3(0);
265 channel3_intr_enable(0);
266 }
18c7adcf 267
2401dd25 268 /* release all active buffers */
c4697d7f 269 spin_lock_irqsave(&common->irqlock, flags);
18c7adcf 270 if (common->cur_frm == common->next_frm) {
2d700715
JS
271 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
272 VB2_BUF_STATE_ERROR);
18c7adcf
LP
273 } else {
274 if (common->cur_frm != NULL)
2d700715 275 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
18c7adcf
LP
276 VB2_BUF_STATE_ERROR);
277 if (common->next_frm != NULL)
2d700715 278 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
18c7adcf
LP
279 VB2_BUF_STATE_ERROR);
280 }
281
2401dd25
LP
282 while (!list_empty(&common->dma_queue)) {
283 common->next_frm = list_entry(common->dma_queue.next,
284 struct vpif_disp_buffer, list);
285 list_del(&common->next_frm->list);
2d700715
JS
286 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
287 VB2_BUF_STATE_ERROR);
2401dd25 288 }
c4697d7f 289 spin_unlock_irqrestore(&common->irqlock, flags);
2401dd25
LP
290}
291
292static struct vb2_ops video_qops = {
293 .queue_setup = vpif_buffer_queue_setup,
d10ed5c1
LP
294 .wait_prepare = vb2_ops_wait_prepare,
295 .wait_finish = vb2_ops_wait_finish,
2401dd25
LP
296 .buf_prepare = vpif_buffer_prepare,
297 .start_streaming = vpif_start_streaming,
298 .stop_streaming = vpif_stop_streaming,
2401dd25
LP
299 .buf_queue = vpif_buffer_queue,
300};
301
e7332e3a
C
302static void process_progressive_mode(struct common_obj *common)
303{
304 unsigned long addr = 0;
305
c4697d7f 306 spin_lock(&common->irqlock);
e7332e3a
C
307 /* Get the next buffer from buffer queue */
308 common->next_frm = list_entry(common->dma_queue.next,
2401dd25 309 struct vpif_disp_buffer, list);
e7332e3a 310 /* Remove that buffer from the buffer queue */
2401dd25 311 list_del(&common->next_frm->list);
c4697d7f 312 spin_unlock(&common->irqlock);
e7332e3a
C
313
314 /* Set top and bottom field addrs in VPIF registers */
2d700715 315 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
e7332e3a
C
316 common->set_addr(addr + common->ytop_off,
317 addr + common->ybtm_off,
318 addr + common->ctop_off,
319 addr + common->cbtm_off);
320}
321
322static void process_interlaced_mode(int fid, struct common_obj *common)
323{
324 /* device field id and local field id are in sync */
325 /* If this is even field */
326 if (0 == fid) {
327 if (common->cur_frm == common->next_frm)
328 return;
329
330 /* one frame is displayed If next frame is
331 * available, release cur_frm and move on */
332 /* Copy frame display time */
2d700715 333 v4l2_get_timestamp(&common->cur_frm->vb.timestamp);
e7332e3a 334 /* Change status of the cur_frm */
2d700715
JS
335 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
336 VB2_BUF_STATE_DONE);
e7332e3a
C
337 /* Make cur_frm pointing to next_frm */
338 common->cur_frm = common->next_frm;
339
340 } else if (1 == fid) { /* odd field */
c4697d7f 341 spin_lock(&common->irqlock);
e7332e3a
C
342 if (list_empty(&common->dma_queue)
343 || (common->cur_frm != common->next_frm)) {
c4697d7f 344 spin_unlock(&common->irqlock);
e7332e3a
C
345 return;
346 }
c4697d7f 347 spin_unlock(&common->irqlock);
e7332e3a
C
348 /* one field is displayed configure the next
349 * frame if it is available else hold on current
350 * frame */
351 /* Get next from the buffer queue */
352 process_progressive_mode(common);
e7332e3a
C
353 }
354}
355
356/*
357 * vpif_channel_isr: It changes status of the displayed buffer, takes next
358 * buffer from the queue and sets its address in VPIF registers
359 */
360static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
361{
362 struct vpif_device *dev = &vpif_obj;
363 struct channel_obj *ch;
364 struct common_obj *common;
e7332e3a
C
365 int fid = -1, i;
366 int channel_id = 0;
367
368 channel_id = *(int *)(dev_id);
b1fc4230
MH
369 if (!vpif_intr_status(channel_id + 2))
370 return IRQ_NONE;
371
e7332e3a 372 ch = dev->dev[channel_id];
e7332e3a
C
373 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
374 common = &ch->common[i];
375 /* If streaming is started in this channel */
e7332e3a
C
376
377 if (1 == ch->vpifparams.std_info.frm_fmt) {
c4697d7f
HV
378 spin_lock(&common->irqlock);
379 if (list_empty(&common->dma_queue)) {
380 spin_unlock(&common->irqlock);
e7332e3a 381 continue;
c4697d7f
HV
382 }
383 spin_unlock(&common->irqlock);
e7332e3a
C
384
385 /* Progressive mode */
386 if (!channel_first_int[i][channel_id]) {
387 /* Mark status of the cur_frm to
388 * done and unlock semaphore on it */
2d700715
JS
389 v4l2_get_timestamp(
390 &common->cur_frm->vb.timestamp);
391 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
392 VB2_BUF_STATE_DONE);
e7332e3a
C
393 /* Make cur_frm pointing to next_frm */
394 common->cur_frm = common->next_frm;
395 }
396
397 channel_first_int[i][channel_id] = 0;
398 process_progressive_mode(common);
399 } else {
400 /* Interlaced mode */
401 /* If it is first interrupt, ignore it */
402
403 if (channel_first_int[i][channel_id]) {
404 channel_first_int[i][channel_id] = 0;
405 continue;
406 }
407
408 if (0 == i) {
409 ch->field_id ^= 1;
410 /* Get field id from VPIF registers */
411 fid = vpif_channel_getfid(ch->channel_id + 2);
412 /* If fid does not match with stored field id */
413 if (fid != ch->field_id) {
414 /* Make them in sync */
415 if (0 == fid)
416 ch->field_id = fid;
417
418 return IRQ_HANDLED;
419 }
420 }
421 process_interlaced_mode(fid, common);
422 }
423 }
424
425 return IRQ_HANDLED;
426}
427
c027e165 428static int vpif_update_std_info(struct channel_obj *ch)
e7332e3a 429{
e7332e3a
C
430 struct video_obj *vid_ch = &ch->video;
431 struct vpif_params *vpifparams = &ch->vpifparams;
432 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
433 const struct vpif_channel_config_params *config;
434
c027e165 435 int i;
e7332e3a 436
c027e165 437 for (i = 0; i < vpif_ch_params_count; i++) {
ced9b21f 438 config = &vpif_ch_params[i];
40c8bcea
MR
439 if (config->hd_sd == 0) {
440 vpif_dbg(2, debug, "SD format\n");
441 if (config->stdid & vid_ch->stdid) {
442 memcpy(std_info, config, sizeof(*config));
443 break;
444 }
e7332e3a
C
445 }
446 }
447
c027e165
MR
448 if (i == vpif_ch_params_count) {
449 vpif_dbg(1, debug, "Format not found\n");
aa444406 450 return -EINVAL;
c027e165
MR
451 }
452
453 return 0;
454}
455
456static int vpif_update_resolution(struct channel_obj *ch)
457{
458 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
459 struct video_obj *vid_ch = &ch->video;
460 struct vpif_params *vpifparams = &ch->vpifparams;
461 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
462
0598c17b 463 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
c027e165
MR
464 return -EINVAL;
465
0598c17b 466 if (vid_ch->stdid) {
c027e165
MR
467 if (vpif_update_std_info(ch))
468 return -EINVAL;
469 }
e7332e3a 470
9cba6534 471 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
e7332e3a
C
472 common->fmt.fmt.pix.width = std_info->width;
473 common->fmt.fmt.pix.height = std_info->height;
474 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
475 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
476
477 /* Set height and width paramateres */
c027e165
MR
478 common->height = std_info->height;
479 common->width = std_info->width;
9cba6534
LP
480 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
481
482 if (vid_ch->stdid)
483 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
484 else
485 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
486
487 if (ch->vpifparams.std_info.frm_fmt)
488 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
489 else
490 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
e7332e3a
C
491
492 return 0;
493}
494
495/*
496 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
497 * in the top and bottom field
498 */
499static void vpif_calculate_offsets(struct channel_obj *ch)
500{
501 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
502 struct vpif_params *vpifparams = &ch->vpifparams;
503 enum v4l2_field field = common->fmt.fmt.pix.field;
504 struct video_obj *vid_ch = &ch->video;
a4f20e2f 505 unsigned int hpitch, sizeimage;
e7332e3a
C
506
507 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
508 if (ch->vpifparams.std_info.frm_fmt)
509 vid_ch->buf_field = V4L2_FIELD_NONE;
510 else
511 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
512 } else {
513 vid_ch->buf_field = common->fmt.fmt.pix.field;
514 }
515
2401dd25 516 sizeimage = common->fmt.fmt.pix.sizeimage;
e7332e3a
C
517
518 hpitch = common->fmt.fmt.pix.bytesperline;
e7332e3a
C
519 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
520 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
521 common->ytop_off = 0;
522 common->ybtm_off = hpitch;
523 common->ctop_off = sizeimage / 2;
524 common->cbtm_off = sizeimage / 2 + hpitch;
525 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
526 common->ytop_off = 0;
527 common->ybtm_off = sizeimage / 4;
528 common->ctop_off = sizeimage / 2;
529 common->cbtm_off = common->ctop_off + sizeimage / 4;
530 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
531 common->ybtm_off = 0;
532 common->ytop_off = sizeimage / 4;
533 common->cbtm_off = sizeimage / 2;
534 common->ctop_off = common->cbtm_off + sizeimage / 4;
535 }
536
537 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
538 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
539 vpifparams->video_params.storage_mode = 1;
540 } else {
541 vpifparams->video_params.storage_mode = 0;
542 }
543
544 if (ch->vpifparams.std_info.frm_fmt == 1) {
545 vpifparams->video_params.hpitch =
546 common->fmt.fmt.pix.bytesperline;
547 } else {
548 if ((field == V4L2_FIELD_ANY) ||
549 (field == V4L2_FIELD_INTERLACED))
550 vpifparams->video_params.hpitch =
551 common->fmt.fmt.pix.bytesperline * 2;
552 else
553 vpifparams->video_params.hpitch =
554 common->fmt.fmt.pix.bytesperline;
555 }
556
557 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
558}
559
e7332e3a
C
560static void vpif_config_addr(struct channel_obj *ch, int muxmode)
561{
562 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
563
564 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
565 common->set_addr = ch3_set_videobuf_addr;
566 } else {
567 if (2 == muxmode)
568 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
569 else
570 common->set_addr = ch2_set_videobuf_addr;
571 }
572}
573
e7332e3a 574/* functions implementing ioctls */
2c0ddd17
MR
575/**
576 * vpif_querycap() - QUERYCAP handler
577 * @file: file ptr
578 * @priv: file handle
579 * @cap: ptr to v4l2_capability structure
580 */
e7332e3a
C
581static int vpif_querycap(struct file *file, void *priv,
582 struct v4l2_capability *cap)
583{
317b2e2f 584 struct vpif_display_config *config = vpif_dev->platform_data;
e7332e3a 585
626d533f
LP
586 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
587 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
76c4c2be 588 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
626d533f
LP
589 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
590 dev_name(vpif_dev));
e7332e3a
C
591 strlcpy(cap->card, config->card_name, sizeof(cap->card));
592
593 return 0;
594}
595
596static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
597 struct v4l2_fmtdesc *fmt)
598{
9cba6534 599 if (fmt->index != 0)
e7332e3a 600 return -EINVAL;
e7332e3a
C
601
602 /* Fill in the information about format */
603 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
604 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
605 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
9cba6534 606 fmt->flags = 0;
e7332e3a
C
607 return 0;
608}
609
610static int vpif_g_fmt_vid_out(struct file *file, void *priv,
611 struct v4l2_format *fmt)
612{
fcc22af2
LP
613 struct video_device *vdev = video_devdata(file);
614 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a
C
615 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
616
617 /* Check the validity of the buffer type */
618 if (common->fmt.type != fmt->type)
619 return -EINVAL;
620
c027e165 621 if (vpif_update_resolution(ch))
9bfaae24
HV
622 return -EINVAL;
623 *fmt = common->fmt;
624 return 0;
e7332e3a
C
625}
626
9cba6534 627static int vpif_try_fmt_vid_out(struct file *file, void *priv,
e7332e3a
C
628 struct v4l2_format *fmt)
629{
fcc22af2
LP
630 struct video_device *vdev = video_devdata(file);
631 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a 632 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
9cba6534 633 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
e7332e3a 634
9cba6534
LP
635 /*
636 * to supress v4l-compliance warnings silently correct
637 * the pixelformat
638 */
639 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
640 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
e7332e3a 641
9cba6534
LP
642 if (vpif_update_resolution(ch))
643 return -EINVAL;
644
645 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
646 pixfmt->field = common->fmt.fmt.pix.field;
647 pixfmt->bytesperline = common->fmt.fmt.pix.width;
648 pixfmt->width = common->fmt.fmt.pix.width;
649 pixfmt->height = common->fmt.fmt.pix.height;
650 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
e7332e3a 651
e7332e3a
C
652 return 0;
653}
654
9cba6534 655static int vpif_s_fmt_vid_out(struct file *file, void *priv,
e7332e3a
C
656 struct v4l2_format *fmt)
657{
fcc22af2
LP
658 struct video_device *vdev = video_devdata(file);
659 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a
C
660 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
661 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
9cba6534 662 int ret;
e7332e3a 663
9cba6534
LP
664 if (vb2_is_busy(&common->buffer_queue))
665 return -EBUSY;
e7332e3a 666
9cba6534
LP
667 ret = vpif_try_fmt_vid_out(file, priv, fmt);
668 if (ret)
669 return ret;
670
671 /* store the pix format in the channel object */
672 common->fmt.fmt.pix = *pixfmt;
673
674 /* store the format in the channel object */
675 common->fmt = *fmt;
676 return 0;
e7332e3a
C
677}
678
314527ac 679static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
e7332e3a 680{
f27d0f45 681 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
682 struct video_device *vdev = video_devdata(file);
683 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a 684 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
f27d0f45
LP
685 struct vpif_display_chan_config *chan_cfg;
686 struct v4l2_output output;
687 int ret;
688
689 if (config->chan_config[ch->channel_id].outputs == NULL)
690 return -ENODATA;
691
692 chan_cfg = &config->chan_config[ch->channel_id];
693 output = chan_cfg->outputs[ch->output_idx].output;
694 if (output.capabilities != V4L2_OUT_CAP_STD)
695 return -ENODATA;
e7332e3a 696
2f5851b5
LP
697 if (vb2_is_busy(&common->buffer_queue))
698 return -EBUSY;
699
f27d0f45 700
314527ac 701 if (!(std_id & VPIF_V4L2_STD))
e7332e3a
C
702 return -EINVAL;
703
e7332e3a 704 /* Call encoder subdevice function to set the standard */
314527ac 705 ch->video.stdid = std_id;
0598c17b 706 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
e7332e3a 707 /* Get the information about the standard */
9bfaae24
HV
708 if (vpif_update_resolution(ch))
709 return -EINVAL;
e7332e3a 710
e7332e3a 711 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
e7332e3a
C
712
713 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
314527ac 714 s_std_output, std_id);
e7332e3a
C
715 if (ret < 0) {
716 vpif_err("Failed to set output standard\n");
9bfaae24 717 return ret;
e7332e3a
C
718 }
719
8774bed9 720 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
314527ac 721 s_std, std_id);
e7332e3a
C
722 if (ret < 0)
723 vpif_err("Failed to set standard for sub devices\n");
e7332e3a
C
724 return ret;
725}
726
727static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
728{
f27d0f45 729 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
730 struct video_device *vdev = video_devdata(file);
731 struct channel_obj *ch = video_get_drvdata(vdev);
f27d0f45
LP
732 struct vpif_display_chan_config *chan_cfg;
733 struct v4l2_output output;
734
735 if (config->chan_config[ch->channel_id].outputs == NULL)
736 return -ENODATA;
737
738 chan_cfg = &config->chan_config[ch->channel_id];
739 output = chan_cfg->outputs[ch->output_idx].output;
740 if (output.capabilities != V4L2_OUT_CAP_STD)
741 return -ENODATA;
e7332e3a
C
742
743 *std = ch->video.stdid;
744 return 0;
745}
746
e7332e3a
C
747static int vpif_enum_output(struct file *file, void *fh,
748 struct v4l2_output *output)
749{
750
317b2e2f 751 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
752 struct video_device *vdev = video_devdata(file);
753 struct channel_obj *ch = video_get_drvdata(vdev);
2bd4e58c 754 struct vpif_display_chan_config *chan_cfg;
e7332e3a 755
2bd4e58c
LP
756 chan_cfg = &config->chan_config[ch->channel_id];
757 if (output->index >= chan_cfg->output_count) {
e7332e3a
C
758 vpif_dbg(1, debug, "Invalid output index\n");
759 return -EINVAL;
760 }
761
2bd4e58c
LP
762 *output = chan_cfg->outputs[output->index].output;
763 return 0;
764}
765
766/**
767 * vpif_output_to_subdev() - Maps output to sub device
768 * @vpif_cfg - global config ptr
769 * @chan_cfg - channel config ptr
770 * @index - Given output index from application
771 *
772 * lookup the sub device information for a given output index.
773 * we report all the output to application. output table also
774 * has sub device name for the each output
775 */
776static int
777vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
778 struct vpif_display_chan_config *chan_cfg, int index)
779{
780 struct vpif_subdev_info *subdev_info;
781 const char *subdev_name;
782 int i;
783
784 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
785
786 if (chan_cfg->outputs == NULL)
787 return -1;
788
789 subdev_name = chan_cfg->outputs[index].subdev_name;
790 if (subdev_name == NULL)
791 return -1;
792
793 /* loop through the sub device list to get the sub device info */
794 for (i = 0; i < vpif_cfg->subdev_count; i++) {
795 subdev_info = &vpif_cfg->subdevinfo[i];
796 if (!strcmp(subdev_info->name, subdev_name))
797 return i;
798 }
799 return -1;
800}
801
802/**
803 * vpif_set_output() - Select an output
804 * @vpif_cfg - global config ptr
805 * @ch - channel
806 * @index - Given output index from application
807 *
808 * Select the given output.
809 */
810static int vpif_set_output(struct vpif_display_config *vpif_cfg,
811 struct channel_obj *ch, int index)
812{
813 struct vpif_display_chan_config *chan_cfg =
814 &vpif_cfg->chan_config[ch->channel_id];
2bd4e58c
LP
815 struct v4l2_subdev *sd = NULL;
816 u32 input = 0, output = 0;
817 int sd_index;
818 int ret;
819
820 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
a4f20e2f 821 if (sd_index >= 0)
2bd4e58c 822 sd = vpif_obj.sd[sd_index];
e7332e3a 823
2bd4e58c
LP
824 if (sd) {
825 input = chan_cfg->outputs[index].input_route;
826 output = chan_cfg->outputs[index].output_route;
827 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
828 if (ret < 0 && ret != -ENOIOCTLCMD) {
829 vpif_err("Failed to set output\n");
830 return ret;
831 }
832
833 }
834 ch->output_idx = index;
835 ch->sd = sd;
836 if (chan_cfg->outputs != NULL)
837 /* update tvnorms from the sub device output info */
e933c6bc 838 ch->video_dev.tvnorms = chan_cfg->outputs[index].output.std;
e7332e3a
C
839 return 0;
840}
841
842static int vpif_s_output(struct file *file, void *priv, unsigned int i)
843{
2bd4e58c 844 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
845 struct video_device *vdev = video_devdata(file);
846 struct channel_obj *ch = video_get_drvdata(vdev);
2bd4e58c 847 struct vpif_display_chan_config *chan_cfg;
e7332e3a 848 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
2bd4e58c 849
2f5851b5
LP
850 if (vb2_is_busy(&common->buffer_queue))
851 return -EBUSY;
852
2bd4e58c
LP
853 chan_cfg = &config->chan_config[ch->channel_id];
854
855 if (i >= chan_cfg->output_count)
856 return -EINVAL;
e7332e3a 857
2bd4e58c 858 return vpif_set_output(config, ch, i);
e7332e3a
C
859}
860
861static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
862{
fcc22af2
LP
863 struct video_device *vdev = video_devdata(file);
864 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a 865
311673ee 866 *i = ch->output_idx;
e7332e3a
C
867
868 return 0;
869}
870
40c8bcea 871/**
0598c17b 872 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
40c8bcea
MR
873 * @file: file ptr
874 * @priv: file handle
0598c17b 875 * @timings: input timings
40c8bcea 876 */
0598c17b
HV
877static int
878vpif_enum_dv_timings(struct file *file, void *priv,
879 struct v4l2_enum_dv_timings *timings)
40c8bcea 880{
1093c590 881 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
882 struct video_device *vdev = video_devdata(file);
883 struct channel_obj *ch = video_get_drvdata(vdev);
1093c590
LP
884 struct vpif_display_chan_config *chan_cfg;
885 struct v4l2_output output;
2bd4e58c 886 int ret;
40c8bcea 887
1093c590
LP
888 if (config->chan_config[ch->channel_id].outputs == NULL)
889 return -ENODATA;
890
891 chan_cfg = &config->chan_config[ch->channel_id];
892 output = chan_cfg->outputs[ch->output_idx].output;
893 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
894 return -ENODATA;
895
10d2146d
LP
896 timings->pad = 0;
897
898 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
63af4af5 899 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
2bd4e58c
LP
900 return -EINVAL;
901 return ret;
40c8bcea
MR
902}
903
c027e165
MR
904/**
905 * vpif_s_dv_timings() - S_DV_TIMINGS handler
906 * @file: file ptr
907 * @priv: file handle
908 * @timings: digital video timings
909 */
910static int vpif_s_dv_timings(struct file *file, void *priv,
911 struct v4l2_dv_timings *timings)
912{
1093c590 913 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
914 struct video_device *vdev = video_devdata(file);
915 struct channel_obj *ch = video_get_drvdata(vdev);
c027e165 916 struct vpif_params *vpifparams = &ch->vpifparams;
1093c590 917 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
c027e165
MR
918 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
919 struct video_obj *vid_ch = &ch->video;
0598c17b 920 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1093c590
LP
921 struct vpif_display_chan_config *chan_cfg;
922 struct v4l2_output output;
c027e165
MR
923 int ret;
924
1093c590
LP
925 if (config->chan_config[ch->channel_id].outputs == NULL)
926 return -ENODATA;
927
928 chan_cfg = &config->chan_config[ch->channel_id];
929 output = chan_cfg->outputs[ch->output_idx].output;
930 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
931 return -ENODATA;
932
933 if (vb2_is_busy(&common->buffer_queue))
934 return -EBUSY;
935
c027e165
MR
936 if (timings->type != V4L2_DV_BT_656_1120) {
937 vpif_dbg(2, debug, "Timing type not defined\n");
938 return -EINVAL;
939 }
940
941 /* Configure subdevice timings, if any */
882084ad 942 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
2bd4e58c
LP
943 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
944 ret = 0;
945 if (ret < 0) {
c027e165
MR
946 vpif_dbg(2, debug, "Error setting custom DV timings\n");
947 return ret;
948 }
949
950 if (!(timings->bt.width && timings->bt.height &&
951 (timings->bt.hbackporch ||
952 timings->bt.hfrontporch ||
953 timings->bt.hsync) &&
954 timings->bt.vfrontporch &&
955 (timings->bt.vbackporch ||
956 timings->bt.vsync))) {
957 vpif_dbg(2, debug, "Timings for width, height, "
958 "horizontal back porch, horizontal sync, "
959 "horizontal front porch, vertical back porch, "
960 "vertical sync and vertical back porch "
961 "must be defined\n");
962 return -EINVAL;
963 }
964
0598c17b 965 vid_ch->dv_timings = *timings;
c027e165
MR
966
967 /* Configure video port timings */
968
e3655268 969 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
c027e165
MR
970 std_info->sav2eav = bt->width;
971
972 std_info->l1 = 1;
973 std_info->l3 = bt->vsync + bt->vbackporch + 1;
974
e3655268 975 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
c027e165
MR
976 if (bt->interlaced) {
977 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
c027e165
MR
978 std_info->l5 = std_info->vsize/2 -
979 (bt->vfrontporch - 1);
980 std_info->l7 = std_info->vsize/2 + 1;
981 std_info->l9 = std_info->l7 + bt->il_vsync +
982 bt->il_vbackporch + 1;
983 std_info->l11 = std_info->vsize -
984 (bt->il_vfrontporch - 1);
985 } else {
986 vpif_dbg(2, debug, "Required timing values for "
987 "interlaced BT format missing\n");
988 return -EINVAL;
989 }
990 } else {
c027e165
MR
991 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
992 }
993 strncpy(std_info->name, "Custom timings BT656/1120",
994 VPIF_MAX_NAME);
995 std_info->width = bt->width;
996 std_info->height = bt->height;
997 std_info->frm_fmt = bt->interlaced ? 0 : 1;
998 std_info->ycmux_mode = 0;
999 std_info->capture_format = 0;
1000 std_info->vbi_supported = 0;
1001 std_info->hd_sd = 1;
1002 std_info->stdid = 0;
c027e165 1003 vid_ch->stdid = 0;
c027e165
MR
1004
1005 return 0;
1006}
1007
1008/**
1009 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1010 * @file: file ptr
1011 * @priv: file handle
1012 * @timings: digital video timings
1013 */
1014static int vpif_g_dv_timings(struct file *file, void *priv,
1015 struct v4l2_dv_timings *timings)
1016{
1093c590 1017 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
1018 struct video_device *vdev = video_devdata(file);
1019 struct channel_obj *ch = video_get_drvdata(vdev);
1093c590 1020 struct vpif_display_chan_config *chan_cfg;
c027e165 1021 struct video_obj *vid_ch = &ch->video;
1093c590
LP
1022 struct v4l2_output output;
1023
1024 if (config->chan_config[ch->channel_id].outputs == NULL)
1025 goto error;
1026
1027 chan_cfg = &config->chan_config[ch->channel_id];
1028 output = chan_cfg->outputs[ch->output_idx].output;
1029
1030 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1031 goto error;
c027e165 1032
0598c17b 1033 *timings = vid_ch->dv_timings;
c027e165
MR
1034
1035 return 0;
1093c590
LP
1036error:
1037 return -ENODATA;
c027e165 1038}
7036d6a7 1039
7036d6a7
MR
1040/*
1041 * vpif_log_status() - Status information
1042 * @file: file ptr
1043 * @priv: file handle
1044 *
1045 * Returns zero.
1046 */
1047static int vpif_log_status(struct file *filep, void *priv)
1048{
1049 /* status for sub devices */
1050 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1051
1052 return 0;
1053}
1054
e7332e3a
C
1055/* vpif display ioctl operations */
1056static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
dcce8669 1057 .vidioc_querycap = vpif_querycap,
e7332e3a 1058 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
dcce8669
LP
1059 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1060 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1061 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
b92cde17
LP
1062
1063 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1064 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1065 .vidioc_querybuf = vb2_ioctl_querybuf,
1066 .vidioc_qbuf = vb2_ioctl_qbuf,
1067 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1068 .vidioc_expbuf = vb2_ioctl_expbuf,
1069 .vidioc_streamon = vb2_ioctl_streamon,
1070 .vidioc_streamoff = vb2_ioctl_streamoff,
1071
dcce8669 1072 .vidioc_s_std = vpif_s_std,
e7332e3a 1073 .vidioc_g_std = vpif_g_std,
dcce8669 1074
e7332e3a
C
1075 .vidioc_enum_output = vpif_enum_output,
1076 .vidioc_s_output = vpif_s_output,
1077 .vidioc_g_output = vpif_g_output,
dcce8669
LP
1078
1079 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1080 .vidioc_s_dv_timings = vpif_s_dv_timings,
1081 .vidioc_g_dv_timings = vpif_g_dv_timings,
1082
7036d6a7 1083 .vidioc_log_status = vpif_log_status,
e7332e3a
C
1084};
1085
1086static const struct v4l2_file_operations vpif_fops = {
1087 .owner = THIS_MODULE,
fcc22af2
LP
1088 .open = v4l2_fh_open,
1089 .release = vb2_fop_release,
9bfaae24 1090 .unlocked_ioctl = video_ioctl2,
4be2153c
LP
1091 .mmap = vb2_fop_mmap,
1092 .poll = vb2_fop_poll
e7332e3a
C
1093};
1094
e7332e3a
C
1095/*Configure the channels, buffer sizei, request irq */
1096static int initialize_vpif(void)
1097{
1098 int free_channel_objects_index;
d5b94b99 1099 int err, i, j;
e7332e3a
C
1100
1101 /* Allocate memory for six channel objects */
1102 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1103 vpif_obj.dev[i] =
1f8766b4 1104 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
e7332e3a
C
1105 /* If memory allocation fails, return error */
1106 if (!vpif_obj.dev[i]) {
1107 free_channel_objects_index = i;
1108 err = -ENOMEM;
1109 goto vpif_init_free_channel_objects;
1110 }
1111 }
1112
e7332e3a
C
1113 return 0;
1114
1115vpif_init_free_channel_objects:
1116 for (j = 0; j < free_channel_objects_index; j++)
1117 kfree(vpif_obj.dev[j]);
1118 return err;
1119}
1120
4b8a531e
LP
1121static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1122 struct v4l2_subdev *subdev,
1123 struct v4l2_async_subdev *asd)
1124{
1125 int i;
1126
1127 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1128 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1129 subdev->name)) {
1130 vpif_obj.sd[i] = subdev;
1131 vpif_obj.sd[i]->grp_id = 1 << i;
1132 return 0;
1133 }
1134
1135 return -EINVAL;
1136}
1137
1138static int vpif_probe_complete(void)
1139{
1140 struct common_obj *common;
4be2153c 1141 struct video_device *vdev;
4b8a531e 1142 struct channel_obj *ch;
a2b235cb 1143 struct vb2_queue *q;
4b8a531e
LP
1144 int j, err, k;
1145
1146 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1147 ch = vpif_obj.dev[j];
1148 /* Initialize field of the channel objects */
4b8a531e 1149 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
4b8a531e 1150 common = &ch->common[k];
4b8a531e
LP
1151 spin_lock_init(&common->irqlock);
1152 mutex_init(&common->lock);
4b8a531e
LP
1153 common->set_addr = NULL;
1154 common->ytop_off = 0;
1155 common->ybtm_off = 0;
1156 common->ctop_off = 0;
1157 common->cbtm_off = 0;
1158 common->cur_frm = NULL;
1159 common->next_frm = NULL;
1160 memset(&common->fmt, 0, sizeof(common->fmt));
4b8a531e
LP
1161 }
1162 ch->initialized = 0;
1163 if (vpif_obj.config->subdev_count)
1164 ch->sd = vpif_obj.sd[0];
1165 ch->channel_id = j;
4b8a531e
LP
1166
1167 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1168
4b8a531e
LP
1169 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1170 V4L2_BUF_TYPE_VIDEO_OUTPUT;
4b8a531e
LP
1171
1172 /* select output 0 */
1173 err = vpif_set_output(vpif_obj.config, ch, 0);
1174 if (err)
1175 goto probe_out;
1176
9cba6534
LP
1177 /* set initial format */
1178 ch->video.stdid = V4L2_STD_525_60;
1179 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1180 vpif_update_resolution(ch);
1181
a2b235cb
LP
1182 /* Initialize vb2 queue */
1183 q = &common->buffer_queue;
1184 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1185 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1186 q->drv_priv = ch;
1187 q->ops = &video_qops;
1188 q->mem_ops = &vb2_dma_contig_memops;
1189 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1190 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1191 q->min_buffers_needed = 1;
d10ed5c1 1192 q->lock = &common->lock;
a2b235cb
LP
1193 err = vb2_queue_init(q);
1194 if (err) {
1195 vpif_err("vpif_display: vb2_queue_init() failed\n");
1196 goto probe_out;
1197 }
1198
1199 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1200 if (IS_ERR(common->alloc_ctx)) {
1201 vpif_err("Failed to get the context\n");
1202 err = PTR_ERR(common->alloc_ctx);
1203 goto probe_out;
1204 }
1205
1206 INIT_LIST_HEAD(&common->dma_queue);
1207
4b8a531e 1208 /* register video device */
212bdba3
MCC
1209 vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
1210 ch, &ch->video_dev);
4b8a531e 1211
76c4c2be 1212 /* Initialize the video_device structure */
e933c6bc 1213 vdev = &ch->video_dev;
76c4c2be 1214 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
e933c6bc 1215 vdev->release = video_device_release_empty;
76c4c2be
LP
1216 vdev->fops = &vpif_fops;
1217 vdev->ioctl_ops = &vpif_ioctl_ops;
1218 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1219 vdev->vfl_dir = VFL_DIR_TX;
4be2153c 1220 vdev->queue = q;
fcc22af2 1221 vdev->lock = &common->lock;
e933c6bc 1222 video_set_drvdata(&ch->video_dev, ch);
4be2153c
LP
1223 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1224 (j ? 3 : 2));
4b8a531e
LP
1225 if (err < 0)
1226 goto probe_out;
1227 }
1228
1229 return 0;
1230
1231probe_out:
1232 for (k = 0; k < j; k++) {
1233 ch = vpif_obj.dev[k];
a2b235cb
LP
1234 common = &ch->common[k];
1235 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
e933c6bc 1236 video_unregister_device(&ch->video_dev);
4b8a531e
LP
1237 }
1238 return err;
1239}
1240
1241static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1242{
1243 return vpif_probe_complete();
1244}
1245
e7332e3a
C
1246/*
1247 * vpif_probe: This function creates device entries by register itself to the
1248 * V4L2 driver and initializes fields of each channel objects
1249 */
1250static __init int vpif_probe(struct platform_device *pdev)
1251{
317b2e2f 1252 struct vpif_subdev_info *subdevdata;
e7332e3a 1253 struct i2c_adapter *i2c_adap;
e7332e3a
C
1254 struct resource *res;
1255 int subdev_count;
e933c6bc
LP
1256 int res_idx = 0;
1257 int i, err;
e7332e3a
C
1258
1259 vpif_dev = &pdev->dev;
317b2e2f 1260 err = initialize_vpif();
e7332e3a 1261
317b2e2f
MK
1262 if (err) {
1263 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1264 return err;
e7332e3a
C
1265 }
1266
e7332e3a
C
1267 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1268 if (err) {
1269 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1270 return err;
1271 }
1272
01b1d975 1273 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
379d2cf4 1274 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
76c4c2be 1275 IRQF_SHARED, VPIF_DRIVER_NAME,
379d2cf4
LP
1276 (void *)(&vpif_obj.dev[res_idx]->
1277 channel_id));
1278 if (err) {
1279 err = -EINVAL;
1280 vpif_err("VPIF IRQ request failed\n");
1281 goto vpif_unregister;
e7332e3a 1282 }
01b1d975 1283 res_idx++;
e7332e3a
C
1284 }
1285
4b8a531e
LP
1286 vpif_obj.config = pdev->dev.platform_data;
1287 subdev_count = vpif_obj.config->subdev_count;
1288 subdevdata = vpif_obj.config->subdevinfo;
e6067f8b
HV
1289 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1290 GFP_KERNEL);
1291 if (vpif_obj.sd == NULL) {
1292 vpif_err("unable to allocate memory for subdevice pointers\n");
1293 err = -ENOMEM;
e933c6bc 1294 goto vpif_unregister;
e6067f8b
HV
1295 }
1296
4b8a531e
LP
1297 if (!vpif_obj.config->asd_sizes) {
1298 i2c_adap = i2c_get_adapter(1);
1299 for (i = 0; i < subdev_count; i++) {
1300 vpif_obj.sd[i] =
1301 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1302 i2c_adap,
1303 &subdevdata[i].
1304 board_info,
1305 NULL);
1306 if (!vpif_obj.sd[i]) {
1307 vpif_err("Error registering v4l2 subdevice\n");
9d3e976b 1308 err = -ENODEV;
4b8a531e
LP
1309 goto probe_subdev_out;
1310 }
e7332e3a 1311
4b8a531e
LP
1312 if (vpif_obj.sd[i])
1313 vpif_obj.sd[i]->grp_id = 1 << i;
1314 }
1315 vpif_probe_complete();
1316 } else {
e8419d08 1317 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
4b8a531e
LP
1318 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1319 vpif_obj.notifier.bound = vpif_async_bound;
1320 vpif_obj.notifier.complete = vpif_async_complete;
1321 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1322 &vpif_obj.notifier);
1323 if (err) {
1324 vpif_err("Error registering async notifier\n");
1325 err = -EINVAL;
1326 goto probe_subdev_out;
e7332e3a 1327 }
e7332e3a
C
1328 }
1329
1330 return 0;
1331
e6067f8b
HV
1332probe_subdev_out:
1333 kfree(vpif_obj.sd);
9c63e01e 1334vpif_unregister:
e7332e3a 1335 v4l2_device_unregister(&vpif_obj.v4l2_dev);
e7332e3a
C
1336
1337 return err;
1338}
1339
1340/*
1341 * vpif_remove: It un-register channels from V4L2 driver
1342 */
1343static int vpif_remove(struct platform_device *device)
1344{
a2b235cb 1345 struct common_obj *common;
e7332e3a 1346 struct channel_obj *ch;
9c63e01e 1347 int i;
e7332e3a
C
1348
1349 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1350
0b361583 1351 kfree(vpif_obj.sd);
e7332e3a
C
1352 /* un-register device */
1353 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1354 /* Get the pointer to the channel object */
1355 ch = vpif_obj.dev[i];
e9b5872c 1356 common = &ch->common[VPIF_VIDEO_INDEX];
a2b235cb 1357 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
e7332e3a 1358 /* Unregister video device */
e933c6bc 1359 video_unregister_device(&ch->video_dev);
0b361583 1360 kfree(vpif_obj.dev[i]);
e7332e3a
C
1361 }
1362
1363 return 0;
1364}
1365
ba1902e3 1366#ifdef CONFIG_PM_SLEEP
e9530dac
MH
1367static int vpif_suspend(struct device *dev)
1368{
1369 struct common_obj *common;
1370 struct channel_obj *ch;
1371 int i;
1372
1373 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1374 /* Get the pointer to the channel object */
1375 ch = vpif_obj.dev[i];
1376 common = &ch->common[VPIF_VIDEO_INDEX];
ba1902e3 1377
81578924 1378 if (!vb2_start_streaming_called(&common->buffer_queue))
ba1902e3
LP
1379 continue;
1380
e9530dac 1381 mutex_lock(&common->lock);
ba1902e3
LP
1382 /* Disable channel */
1383 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1384 enable_channel2(0);
1385 channel2_intr_enable(0);
1386 }
1387 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1388 ycmux_mode == 2) {
1389 enable_channel3(0);
1390 channel3_intr_enable(0);
e9530dac
MH
1391 }
1392 mutex_unlock(&common->lock);
1393 }
1394
1395 return 0;
1396}
1397
1398static int vpif_resume(struct device *dev)
1399{
1400
1401 struct common_obj *common;
1402 struct channel_obj *ch;
1403 int i;
1404
1405 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1406 /* Get the pointer to the channel object */
1407 ch = vpif_obj.dev[i];
1408 common = &ch->common[VPIF_VIDEO_INDEX];
ba1902e3 1409
81578924 1410 if (!vb2_start_streaming_called(&common->buffer_queue))
ba1902e3
LP
1411 continue;
1412
e9530dac 1413 mutex_lock(&common->lock);
ba1902e3
LP
1414 /* Enable channel */
1415 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1416 enable_channel2(1);
1417 channel2_intr_enable(1);
1418 }
1419 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1420 ycmux_mode == 2) {
1421 enable_channel3(1);
1422 channel3_intr_enable(1);
e9530dac
MH
1423 }
1424 mutex_unlock(&common->lock);
1425 }
1426
1427 return 0;
1428}
1429
e9530dac
MH
1430#endif
1431
ba1902e3
LP
1432static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1433
ffa1b391 1434static __refdata struct platform_driver vpif_driver = {
e7332e3a 1435 .driver = {
76c4c2be 1436 .name = VPIF_DRIVER_NAME,
ba1902e3 1437 .pm = &vpif_pm_ops,
e7332e3a
C
1438 },
1439 .probe = vpif_probe,
1440 .remove = vpif_remove,
1441};
1442
b8d067b6 1443module_platform_driver(vpif_driver);
This page took 1.896452 seconds and 5 git commands to generate.