]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/media/platform/davinci/vpif_display.c
[media] media: davinci: vpif_display: move the freeing of irq and global variables...
[thirdparty/kernel/stable.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/
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
10 *
11 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
e7332e3a 17#include <linux/interrupt.h>
012eef70 18#include <linux/module.h>
e7332e3a 19#include <linux/platform_device.h>
5a0e3ad6 20#include <linux/slab.h>
e7332e3a 21
012eef70 22#include <media/v4l2-ioctl.h>
e7332e3a 23
e7332e3a 24#include "vpif.h"
012eef70 25#include "vpif_display.h"
e7332e3a
C
26
27MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
28MODULE_LICENSE("GPL");
64dc3c1a 29MODULE_VERSION(VPIF_DISPLAY_VERSION);
e7332e3a 30
0a63172a 31#define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
e7332e3a
C
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;
38static u32 ch2_numbuffers = 3;
39static u32 ch3_numbuffers = 3;
40static u32 ch2_bufsize = 1920 * 1080 * 2;
41static u32 ch3_bufsize = 720 * 576 * 2;
42
43module_param(debug, int, 0644);
44module_param(ch2_numbuffers, uint, S_IRUGO);
45module_param(ch3_numbuffers, uint, S_IRUGO);
46module_param(ch2_bufsize, uint, S_IRUGO);
47module_param(ch3_bufsize, uint, S_IRUGO);
48
49MODULE_PARM_DESC(debug, "Debug level 0-1");
50MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
51MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
52MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
53MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
54
55static struct vpif_config_params config_params = {
56 .min_numbuffers = 3,
57 .numbuffers[0] = 3,
58 .numbuffers[1] = 3,
59 .min_bufsize[0] = 720 * 480 * 2,
60 .min_bufsize[1] = 720 * 480 * 2,
61 .channel_bufsize[0] = 1920 * 1080 * 2,
62 .channel_bufsize[1] = 720 * 576 * 2,
63};
64
65static struct vpif_device vpif_obj = { {NULL} };
66static struct device *vpif_dev;
2401dd25
LP
67static void vpif_calculate_offsets(struct channel_obj *ch);
68static void vpif_config_addr(struct channel_obj *ch, int muxmode);
e7332e3a 69
e7332e3a 70/*
2401dd25 71 * buffer_prepare: This is the callback function called from vb2_qbuf()
e7332e3a
C
72 * function the buffer is prepared and user space virtual address is converted
73 * into physical address
74 */
2401dd25 75static int vpif_buffer_prepare(struct vb2_buffer *vb)
e7332e3a 76{
2401dd25
LP
77 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
78 struct vb2_queue *q = vb->vb2_queue;
e7332e3a
C
79 struct common_obj *common;
80 unsigned long addr;
81
82 common = &fh->channel->common[VPIF_VIDEO_INDEX];
2401dd25
LP
83 if (vb->state != VB2_BUF_STATE_ACTIVE &&
84 vb->state != VB2_BUF_STATE_PREPARED) {
85 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
86 if (vb2_plane_vaddr(vb, 0) &&
87 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
e7332e3a 88 goto buf_align_exit;
e7332e3a 89
2401dd25
LP
90 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
91 if (q->streaming &&
92 (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
93 if (!ISALIGNED(addr + common->ytop_off) ||
94 !ISALIGNED(addr + common->ybtm_off) ||
95 !ISALIGNED(addr + common->ctop_off) ||
96 !ISALIGNED(addr + common->cbtm_off))
97 goto buf_align_exit;
98 }
e7332e3a
C
99 }
100 return 0;
101
102buf_align_exit:
103 vpif_err("buffer offset not aligned to 8 bytes\n");
104 return -EINVAL;
105}
106
107/*
2401dd25 108 * vpif_buffer_queue_setup: This function allocates memory for the buffers
e7332e3a 109 */
2401dd25
LP
110static int vpif_buffer_queue_setup(struct vb2_queue *vq,
111 const struct v4l2_format *fmt,
112 unsigned int *nbuffers, unsigned int *nplanes,
113 unsigned int sizes[], void *alloc_ctxs[])
e7332e3a 114{
2401dd25 115 struct vpif_fh *fh = vb2_get_drv_priv(vq);
e7332e3a
C
116 struct channel_obj *ch = fh->channel;
117 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
2401dd25
LP
118 unsigned long size;
119
120 if (V4L2_MEMORY_MMAP == common->memory) {
121 size = config_params.channel_bufsize[ch->channel_id];
122 /*
123 * Checking if the buffer size exceeds the available buffer
124 * ycmux_mode = 0 means 1 channel mode HD and
125 * ycmux_mode = 1 means 2 channels mode SD
126 */
127 if (ch->vpifparams.std_info.ycmux_mode == 0) {
128 if (config_params.video_limit[ch->channel_id])
129 while (size * *nbuffers >
130 (config_params.video_limit[0]
131 + config_params.video_limit[1]))
132 (*nbuffers)--;
133 } else {
134 if (config_params.video_limit[ch->channel_id])
135 while (size * *nbuffers >
fc613d44 136 config_params.video_limit[ch->channel_id])
2401dd25
LP
137 (*nbuffers)--;
138 }
139 } else {
140 size = common->fmt.fmt.pix.sizeimage;
fc613d44
MH
141 }
142
2401dd25
LP
143 if (*nbuffers < config_params.min_numbuffers)
144 *nbuffers = config_params.min_numbuffers;
e7332e3a 145
2401dd25
LP
146 *nplanes = 1;
147 sizes[0] = size;
148 alloc_ctxs[0] = common->alloc_ctx;
e7332e3a
C
149 return 0;
150}
151
152/*
153 * vpif_buffer_queue: This function adds the buffer to DMA queue
154 */
2401dd25 155static void vpif_buffer_queue(struct vb2_buffer *vb)
e7332e3a 156{
2401dd25
LP
157 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
158 struct vpif_disp_buffer *buf = container_of(vb,
159 struct vpif_disp_buffer, vb);
160 struct channel_obj *ch = fh->channel;
e7332e3a 161 struct common_obj *common;
c4697d7f 162 unsigned long flags;
e7332e3a 163
2401dd25 164 common = &ch->common[VPIF_VIDEO_INDEX];
e7332e3a
C
165
166 /* add the buffer to the DMA queue */
c4697d7f 167 spin_lock_irqsave(&common->irqlock, flags);
2401dd25 168 list_add_tail(&buf->list, &common->dma_queue);
c4697d7f 169 spin_unlock_irqrestore(&common->irqlock, flags);
e7332e3a
C
170}
171
172/*
2401dd25 173 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
e7332e3a
C
174 * free memory allocated to the buffers
175 */
2401dd25 176static void vpif_buf_cleanup(struct vb2_buffer *vb)
e7332e3a 177{
2401dd25
LP
178 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
179 struct vpif_disp_buffer *buf = container_of(vb,
180 struct vpif_disp_buffer, vb);
e7332e3a
C
181 struct channel_obj *ch = fh->channel;
182 struct common_obj *common;
2401dd25 183 unsigned long flags;
e7332e3a
C
184
185 common = &ch->common[VPIF_VIDEO_INDEX];
186
2401dd25
LP
187 spin_lock_irqsave(&common->irqlock, flags);
188 if (vb->state == VB2_BUF_STATE_ACTIVE)
189 list_del_init(&buf->list);
190 spin_unlock_irqrestore(&common->irqlock, flags);
191}
192
193static void vpif_wait_prepare(struct vb2_queue *vq)
194{
195 struct vpif_fh *fh = vb2_get_drv_priv(vq);
196 struct channel_obj *ch = fh->channel;
197 struct common_obj *common;
198
199 common = &ch->common[VPIF_VIDEO_INDEX];
200 mutex_unlock(&common->lock);
201}
e7332e3a 202
2401dd25
LP
203static void vpif_wait_finish(struct vb2_queue *vq)
204{
205 struct vpif_fh *fh = vb2_get_drv_priv(vq);
206 struct channel_obj *ch = fh->channel;
207 struct common_obj *common;
e7332e3a 208
2401dd25
LP
209 common = &ch->common[VPIF_VIDEO_INDEX];
210 mutex_lock(&common->lock);
211}
212
213static int vpif_buffer_init(struct vb2_buffer *vb)
214{
215 struct vpif_disp_buffer *buf = container_of(vb,
216 struct vpif_disp_buffer, vb);
217
218 INIT_LIST_HEAD(&buf->list);
219
220 return 0;
e7332e3a
C
221}
222
e7332e3a
C
223static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
224
2401dd25
LP
225static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
226{
227 struct vpif_display_config *vpif_config_data =
228 vpif_dev->platform_data;
229 struct vpif_fh *fh = vb2_get_drv_priv(vq);
230 struct channel_obj *ch = fh->channel;
231 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
232 struct vpif_params *vpif = &ch->vpifparams;
233 unsigned long addr = 0;
c4697d7f 234 unsigned long flags;
2401dd25
LP
235 int ret;
236
237 /* If buffer queue is empty, return error */
c4697d7f 238 spin_lock_irqsave(&common->irqlock, flags);
2401dd25 239 if (list_empty(&common->dma_queue)) {
c4697d7f 240 spin_unlock_irqrestore(&common->irqlock, flags);
2401dd25
LP
241 vpif_err("buffer queue is empty\n");
242 return -EIO;
243 }
244
245 /* Get the next frame from the buffer queue */
246 common->next_frm = common->cur_frm =
247 list_entry(common->dma_queue.next,
248 struct vpif_disp_buffer, list);
249
250 list_del(&common->cur_frm->list);
c4697d7f 251 spin_unlock_irqrestore(&common->irqlock, flags);
2401dd25
LP
252 /* Mark state of the current frame to active */
253 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
254
255 /* Initialize field_id and started member */
256 ch->field_id = 0;
257 common->started = 1;
258 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
259 /* Calculate the offset for Y and C data in the buffer */
260 vpif_calculate_offsets(ch);
261
262 if ((ch->vpifparams.std_info.frm_fmt &&
263 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
264 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
265 || (!ch->vpifparams.std_info.frm_fmt
266 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
267 vpif_err("conflict in field format and std format\n");
268 return -EINVAL;
269 }
270
271 /* clock settings */
f4ad8d74
LP
272 if (vpif_config_data->set_clock) {
273 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
274 ycmux_mode, ch->vpifparams.std_info.hd_sd);
275 if (ret < 0) {
276 vpif_err("can't set clock\n");
277 return ret;
278 }
2401dd25
LP
279 }
280
281 /* set the parameters and addresses */
282 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
283 if (ret < 0)
284 return ret;
285
286 common->started = ret;
287 vpif_config_addr(ch, ret);
288 common->set_addr((addr + common->ytop_off),
289 (addr + common->ybtm_off),
290 (addr + common->ctop_off),
291 (addr + common->cbtm_off));
292
293 /* Set interrupt for both the fields in VPIF
294 Register enable channel in VPIF register */
9e18404a 295 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
2401dd25
LP
296 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
297 channel2_intr_assert();
298 channel2_intr_enable(1);
299 enable_channel2(1);
2bd4e58c 300 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
6964b103 301 channel2_clipping_enable(1);
2401dd25
LP
302 }
303
304 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
305 || (common->started == 2)) {
306 channel3_intr_assert();
307 channel3_intr_enable(1);
308 enable_channel3(1);
2bd4e58c 309 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
6964b103 310 channel3_clipping_enable(1);
2401dd25 311 }
2401dd25
LP
312
313 return 0;
314}
315
316/* abort streaming and wait for last buffer */
317static int vpif_stop_streaming(struct vb2_queue *vq)
318{
319 struct vpif_fh *fh = vb2_get_drv_priv(vq);
320 struct channel_obj *ch = fh->channel;
321 struct common_obj *common;
c4697d7f 322 unsigned long flags;
2401dd25
LP
323
324 if (!vb2_is_streaming(vq))
325 return 0;
326
327 common = &ch->common[VPIF_VIDEO_INDEX];
328
329 /* release all active buffers */
c4697d7f 330 spin_lock_irqsave(&common->irqlock, flags);
2401dd25
LP
331 while (!list_empty(&common->dma_queue)) {
332 common->next_frm = list_entry(common->dma_queue.next,
333 struct vpif_disp_buffer, list);
334 list_del(&common->next_frm->list);
335 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
336 }
c4697d7f 337 spin_unlock_irqrestore(&common->irqlock, flags);
2401dd25
LP
338
339 return 0;
340}
341
342static struct vb2_ops video_qops = {
343 .queue_setup = vpif_buffer_queue_setup,
344 .wait_prepare = vpif_wait_prepare,
345 .wait_finish = vpif_wait_finish,
346 .buf_init = vpif_buffer_init,
347 .buf_prepare = vpif_buffer_prepare,
348 .start_streaming = vpif_start_streaming,
349 .stop_streaming = vpif_stop_streaming,
350 .buf_cleanup = vpif_buf_cleanup,
351 .buf_queue = vpif_buffer_queue,
352};
353
e7332e3a
C
354static void process_progressive_mode(struct common_obj *common)
355{
356 unsigned long addr = 0;
357
c4697d7f 358 spin_lock(&common->irqlock);
e7332e3a
C
359 /* Get the next buffer from buffer queue */
360 common->next_frm = list_entry(common->dma_queue.next,
2401dd25 361 struct vpif_disp_buffer, list);
e7332e3a 362 /* Remove that buffer from the buffer queue */
2401dd25 363 list_del(&common->next_frm->list);
c4697d7f 364 spin_unlock(&common->irqlock);
e7332e3a 365 /* Mark status of the buffer as active */
2401dd25 366 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
e7332e3a
C
367
368 /* Set top and bottom field addrs in VPIF registers */
2401dd25 369 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
e7332e3a
C
370 common->set_addr(addr + common->ytop_off,
371 addr + common->ybtm_off,
372 addr + common->ctop_off,
373 addr + common->cbtm_off);
374}
375
376static void process_interlaced_mode(int fid, struct common_obj *common)
377{
378 /* device field id and local field id are in sync */
379 /* If this is even field */
380 if (0 == fid) {
381 if (common->cur_frm == common->next_frm)
382 return;
383
384 /* one frame is displayed If next frame is
385 * available, release cur_frm and move on */
386 /* Copy frame display time */
8e6057b5 387 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
e7332e3a 388 /* Change status of the cur_frm */
2401dd25
LP
389 vb2_buffer_done(&common->cur_frm->vb,
390 VB2_BUF_STATE_DONE);
e7332e3a
C
391 /* Make cur_frm pointing to next_frm */
392 common->cur_frm = common->next_frm;
393
394 } else if (1 == fid) { /* odd field */
c4697d7f 395 spin_lock(&common->irqlock);
e7332e3a
C
396 if (list_empty(&common->dma_queue)
397 || (common->cur_frm != common->next_frm)) {
c4697d7f 398 spin_unlock(&common->irqlock);
e7332e3a
C
399 return;
400 }
c4697d7f 401 spin_unlock(&common->irqlock);
e7332e3a
C
402 /* one field is displayed configure the next
403 * frame if it is available else hold on current
404 * frame */
405 /* Get next from the buffer queue */
406 process_progressive_mode(common);
e7332e3a
C
407 }
408}
409
410/*
411 * vpif_channel_isr: It changes status of the displayed buffer, takes next
412 * buffer from the queue and sets its address in VPIF registers
413 */
414static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
415{
416 struct vpif_device *dev = &vpif_obj;
417 struct channel_obj *ch;
418 struct common_obj *common;
419 enum v4l2_field field;
420 int fid = -1, i;
421 int channel_id = 0;
422
423 channel_id = *(int *)(dev_id);
b1fc4230
MH
424 if (!vpif_intr_status(channel_id + 2))
425 return IRQ_NONE;
426
e7332e3a
C
427 ch = dev->dev[channel_id];
428 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
429 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
430 common = &ch->common[i];
431 /* If streaming is started in this channel */
432 if (0 == common->started)
433 continue;
434
435 if (1 == ch->vpifparams.std_info.frm_fmt) {
c4697d7f
HV
436 spin_lock(&common->irqlock);
437 if (list_empty(&common->dma_queue)) {
438 spin_unlock(&common->irqlock);
e7332e3a 439 continue;
c4697d7f
HV
440 }
441 spin_unlock(&common->irqlock);
e7332e3a
C
442
443 /* Progressive mode */
444 if (!channel_first_int[i][channel_id]) {
445 /* Mark status of the cur_frm to
446 * done and unlock semaphore on it */
8e6057b5
SA
447 v4l2_get_timestamp(&common->cur_frm->vb.
448 v4l2_buf.timestamp);
2401dd25
LP
449 vb2_buffer_done(&common->cur_frm->vb,
450 VB2_BUF_STATE_DONE);
e7332e3a
C
451 /* Make cur_frm pointing to next_frm */
452 common->cur_frm = common->next_frm;
453 }
454
455 channel_first_int[i][channel_id] = 0;
456 process_progressive_mode(common);
457 } else {
458 /* Interlaced mode */
459 /* If it is first interrupt, ignore it */
460
461 if (channel_first_int[i][channel_id]) {
462 channel_first_int[i][channel_id] = 0;
463 continue;
464 }
465
466 if (0 == i) {
467 ch->field_id ^= 1;
468 /* Get field id from VPIF registers */
469 fid = vpif_channel_getfid(ch->channel_id + 2);
470 /* If fid does not match with stored field id */
471 if (fid != ch->field_id) {
472 /* Make them in sync */
473 if (0 == fid)
474 ch->field_id = fid;
475
476 return IRQ_HANDLED;
477 }
478 }
479 process_interlaced_mode(fid, common);
480 }
481 }
482
483 return IRQ_HANDLED;
484}
485
c027e165 486static int vpif_update_std_info(struct channel_obj *ch)
e7332e3a 487{
e7332e3a
C
488 struct video_obj *vid_ch = &ch->video;
489 struct vpif_params *vpifparams = &ch->vpifparams;
490 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
491 const struct vpif_channel_config_params *config;
492
c027e165 493 int i;
e7332e3a 494
c027e165 495 for (i = 0; i < vpif_ch_params_count; i++) {
ced9b21f 496 config = &vpif_ch_params[i];
40c8bcea
MR
497 if (config->hd_sd == 0) {
498 vpif_dbg(2, debug, "SD format\n");
499 if (config->stdid & vid_ch->stdid) {
500 memcpy(std_info, config, sizeof(*config));
501 break;
502 }
e7332e3a
C
503 }
504 }
505
c027e165
MR
506 if (i == vpif_ch_params_count) {
507 vpif_dbg(1, debug, "Format not found\n");
aa444406 508 return -EINVAL;
c027e165
MR
509 }
510
511 return 0;
512}
513
514static int vpif_update_resolution(struct channel_obj *ch)
515{
516 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
517 struct video_obj *vid_ch = &ch->video;
518 struct vpif_params *vpifparams = &ch->vpifparams;
519 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
520
0598c17b 521 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
c027e165
MR
522 return -EINVAL;
523
0598c17b 524 if (vid_ch->stdid) {
c027e165
MR
525 if (vpif_update_std_info(ch))
526 return -EINVAL;
527 }
e7332e3a
C
528
529 common->fmt.fmt.pix.width = std_info->width;
530 common->fmt.fmt.pix.height = std_info->height;
531 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
532 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
533
534 /* Set height and width paramateres */
c027e165
MR
535 common->height = std_info->height;
536 common->width = std_info->width;
e7332e3a
C
537
538 return 0;
539}
540
541/*
542 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
543 * in the top and bottom field
544 */
545static void vpif_calculate_offsets(struct channel_obj *ch)
546{
547 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
548 struct vpif_params *vpifparams = &ch->vpifparams;
549 enum v4l2_field field = common->fmt.fmt.pix.field;
550 struct video_obj *vid_ch = &ch->video;
551 unsigned int hpitch, vpitch, sizeimage;
552
553 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
554 if (ch->vpifparams.std_info.frm_fmt)
555 vid_ch->buf_field = V4L2_FIELD_NONE;
556 else
557 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
558 } else {
559 vid_ch->buf_field = common->fmt.fmt.pix.field;
560 }
561
2401dd25 562 sizeimage = common->fmt.fmt.pix.sizeimage;
e7332e3a
C
563
564 hpitch = common->fmt.fmt.pix.bytesperline;
565 vpitch = sizeimage / (hpitch * 2);
566 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
567 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
568 common->ytop_off = 0;
569 common->ybtm_off = hpitch;
570 common->ctop_off = sizeimage / 2;
571 common->cbtm_off = sizeimage / 2 + hpitch;
572 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
573 common->ytop_off = 0;
574 common->ybtm_off = sizeimage / 4;
575 common->ctop_off = sizeimage / 2;
576 common->cbtm_off = common->ctop_off + sizeimage / 4;
577 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
578 common->ybtm_off = 0;
579 common->ytop_off = sizeimage / 4;
580 common->cbtm_off = sizeimage / 2;
581 common->ctop_off = common->cbtm_off + sizeimage / 4;
582 }
583
584 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
585 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
586 vpifparams->video_params.storage_mode = 1;
587 } else {
588 vpifparams->video_params.storage_mode = 0;
589 }
590
591 if (ch->vpifparams.std_info.frm_fmt == 1) {
592 vpifparams->video_params.hpitch =
593 common->fmt.fmt.pix.bytesperline;
594 } else {
595 if ((field == V4L2_FIELD_ANY) ||
596 (field == V4L2_FIELD_INTERLACED))
597 vpifparams->video_params.hpitch =
598 common->fmt.fmt.pix.bytesperline * 2;
599 else
600 vpifparams->video_params.hpitch =
601 common->fmt.fmt.pix.bytesperline;
602 }
603
604 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
605}
606
607static void vpif_config_format(struct channel_obj *ch)
608{
609 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
610
611 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
612 if (config_params.numbuffers[ch->channel_id] == 0)
613 common->memory = V4L2_MEMORY_USERPTR;
614 else
615 common->memory = V4L2_MEMORY_MMAP;
616
617 common->fmt.fmt.pix.sizeimage =
618 config_params.channel_bufsize[ch->channel_id];
619 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
620 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
621}
622
623static int vpif_check_format(struct channel_obj *ch,
624 struct v4l2_pix_format *pixfmt)
625{
626 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
627 enum v4l2_field field = pixfmt->field;
628 u32 sizeimage, hpitch, vpitch;
629
630 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
631 goto invalid_fmt_exit;
632
633 if (!(VPIF_VALID_FIELD(field)))
634 goto invalid_fmt_exit;
635
636 if (pixfmt->bytesperline <= 0)
637 goto invalid_pitch_exit;
638
2401dd25 639 sizeimage = pixfmt->sizeimage;
e7332e3a 640
c027e165 641 if (vpif_update_resolution(ch))
e7332e3a 642 return -EINVAL;
e7332e3a
C
643
644 hpitch = pixfmt->bytesperline;
645 vpitch = sizeimage / (hpitch * 2);
646
647 /* Check for valid value of pitch */
648 if ((hpitch < ch->vpifparams.std_info.width) ||
649 (vpitch < ch->vpifparams.std_info.height))
650 goto invalid_pitch_exit;
651
652 /* Check for 8 byte alignment */
653 if (!ISALIGNED(hpitch)) {
654 vpif_err("invalid pitch alignment\n");
655 return -EINVAL;
656 }
657 pixfmt->width = common->fmt.fmt.pix.width;
658 pixfmt->height = common->fmt.fmt.pix.height;
659
660 return 0;
661
662invalid_fmt_exit:
663 vpif_err("invalid field format\n");
664 return -EINVAL;
665
666invalid_pitch_exit:
667 vpif_err("invalid pitch\n");
668 return -EINVAL;
669}
670
671static void vpif_config_addr(struct channel_obj *ch, int muxmode)
672{
673 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
674
675 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
676 common->set_addr = ch3_set_videobuf_addr;
677 } else {
678 if (2 == muxmode)
679 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
680 else
681 common->set_addr = ch2_set_videobuf_addr;
682 }
683}
684
685/*
686 * vpif_mmap: It is used to map kernel space buffers into user spaces
687 */
688static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
689{
690 struct vpif_fh *fh = filep->private_data;
2c0ddd17
MR
691 struct channel_obj *ch = fh->channel;
692 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
0b7286d9 693 int ret;
2c0ddd17
MR
694
695 vpif_dbg(2, debug, "vpif_mmap\n");
e7332e3a 696
0b7286d9
HV
697 if (mutex_lock_interruptible(&common->lock))
698 return -ERESTARTSYS;
699 ret = vb2_mmap(&common->buffer_queue, vma);
700 mutex_unlock(&common->lock);
701 return ret;
e7332e3a
C
702}
703
704/*
705 * vpif_poll: It is used for select/poll system call
706 */
707static unsigned int vpif_poll(struct file *filep, poll_table *wait)
708{
709 struct vpif_fh *fh = filep->private_data;
710 struct channel_obj *ch = fh->channel;
711 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
0b7286d9 712 unsigned int res = 0;
e7332e3a 713
0b7286d9
HV
714 if (common->started) {
715 mutex_lock(&common->lock);
716 res = vb2_poll(&common->buffer_queue, filep, wait);
717 mutex_unlock(&common->lock);
718 }
e7332e3a 719
0b7286d9 720 return res;
e7332e3a
C
721}
722
723/*
724 * vpif_open: It creates object of file handle structure and stores it in
725 * private_data member of filepointer
726 */
727static int vpif_open(struct file *filep)
728{
729 struct video_device *vdev = video_devdata(filep);
0b7286d9
HV
730 struct channel_obj *ch = video_get_drvdata(vdev);
731 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
732 struct vpif_fh *fh;
e7332e3a 733
e7332e3a 734 /* Allocate memory for the file handle object */
1f8766b4 735 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
e7332e3a
C
736 if (fh == NULL) {
737 vpif_err("unable to allocate memory for file handle object\n");
738 return -ENOMEM;
739 }
740
0b7286d9
HV
741 if (mutex_lock_interruptible(&common->lock)) {
742 kfree(fh);
743 return -ERESTARTSYS;
744 }
e7332e3a
C
745 /* store pointer to fh in private_data member of filep */
746 filep->private_data = fh;
747 fh->channel = ch;
748 fh->initialized = 0;
749 if (!ch->initialized) {
750 fh->initialized = 1;
751 ch->initialized = 1;
752 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
753 }
754
755 /* Increment channel usrs counter */
756 atomic_inc(&ch->usrs);
757 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
758 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
759 /* Initialize priority of this instance to default priority */
760 fh->prio = V4L2_PRIORITY_UNSET;
761 v4l2_prio_open(&ch->prio, &fh->prio);
0b7286d9 762 mutex_unlock(&common->lock);
e7332e3a
C
763
764 return 0;
765}
766
767/*
768 * vpif_release: This function deletes buffer queue, frees the buffers and
769 * the vpif file handle
770 */
771static int vpif_release(struct file *filep)
772{
773 struct vpif_fh *fh = filep->private_data;
774 struct channel_obj *ch = fh->channel;
775 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
776
0b7286d9 777 mutex_lock(&common->lock);
e7332e3a
C
778 /* if this instance is doing IO */
779 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
780 /* Reset io_usrs member of channel object */
781 common->io_usrs = 0;
782 /* Disable channel */
783 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
784 enable_channel2(0);
785 channel2_intr_enable(0);
786 }
787 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
788 (2 == common->started)) {
789 enable_channel3(0);
790 channel3_intr_enable(0);
791 }
792 common->started = 0;
2401dd25 793
e7332e3a 794 /* Free buffers allocated */
2401dd25
LP
795 vb2_queue_release(&common->buffer_queue);
796 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
797
e7332e3a
C
798 common->numbuffers =
799 config_params.numbuffers[ch->channel_id];
800 }
801
e7332e3a
C
802 /* Decrement channel usrs counter */
803 atomic_dec(&ch->usrs);
804 /* If this file handle has initialize encoder device, reset it */
805 if (fh->initialized)
806 ch->initialized = 0;
807
808 /* Close the priority */
ffb4877b 809 v4l2_prio_close(&ch->prio, fh->prio);
e7332e3a
C
810 filep->private_data = NULL;
811 fh->initialized = 0;
0b7286d9 812 mutex_unlock(&common->lock);
e7332e3a
C
813 kfree(fh);
814
815 return 0;
816}
817
818/* functions implementing ioctls */
2c0ddd17
MR
819/**
820 * vpif_querycap() - QUERYCAP handler
821 * @file: file ptr
822 * @priv: file handle
823 * @cap: ptr to v4l2_capability structure
824 */
e7332e3a
C
825static int vpif_querycap(struct file *file, void *priv,
826 struct v4l2_capability *cap)
827{
317b2e2f 828 struct vpif_display_config *config = vpif_dev->platform_data;
e7332e3a 829
626d533f
LP
830 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
831 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
832 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
833 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
834 dev_name(vpif_dev));
e7332e3a
C
835 strlcpy(cap->card, config->card_name, sizeof(cap->card));
836
837 return 0;
838}
839
840static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
841 struct v4l2_fmtdesc *fmt)
842{
843 if (fmt->index != 0) {
844 vpif_err("Invalid format index\n");
845 return -EINVAL;
846 }
847
848 /* Fill in the information about format */
849 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
850 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
851 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
852
853 return 0;
854}
855
856static int vpif_g_fmt_vid_out(struct file *file, void *priv,
857 struct v4l2_format *fmt)
858{
859 struct vpif_fh *fh = priv;
860 struct channel_obj *ch = fh->channel;
861 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
862
863 /* Check the validity of the buffer type */
864 if (common->fmt.type != fmt->type)
865 return -EINVAL;
866
c027e165 867 if (vpif_update_resolution(ch))
9bfaae24
HV
868 return -EINVAL;
869 *fmt = common->fmt;
870 return 0;
e7332e3a
C
871}
872
873static int vpif_s_fmt_vid_out(struct file *file, void *priv,
874 struct v4l2_format *fmt)
875{
876 struct vpif_fh *fh = priv;
877 struct v4l2_pix_format *pixfmt;
878 struct channel_obj *ch = fh->channel;
879 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
880 int ret = 0;
881
882 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
883 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
884 if (!fh->initialized) {
885 vpif_dbg(1, debug, "Channel Busy\n");
886 return -EBUSY;
887 }
888
889 /* Check for the priority */
ffb4877b 890 ret = v4l2_prio_check(&ch->prio, fh->prio);
e7332e3a
C
891 if (0 != ret)
892 return ret;
893 fh->initialized = 1;
894 }
895
896 if (common->started) {
897 vpif_dbg(1, debug, "Streaming in progress\n");
898 return -EBUSY;
899 }
900
901 pixfmt = &fmt->fmt.pix;
902 /* Check for valid field format */
903 ret = vpif_check_format(ch, pixfmt);
904 if (ret)
905 return ret;
906
907 /* store the pix format in the channel object */
908 common->fmt.fmt.pix = *pixfmt;
909 /* store the format in the channel object */
e7332e3a 910 common->fmt = *fmt;
e7332e3a
C
911 return 0;
912}
913
914static int vpif_try_fmt_vid_out(struct file *file, void *priv,
915 struct v4l2_format *fmt)
916{
917 struct vpif_fh *fh = priv;
918 struct channel_obj *ch = fh->channel;
919 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
920 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
921 int ret = 0;
922
923 ret = vpif_check_format(ch, pixfmt);
924 if (ret) {
925 *pixfmt = common->fmt.fmt.pix;
926 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
927 }
928
929 return ret;
930}
931
932static int vpif_reqbufs(struct file *file, void *priv,
933 struct v4l2_requestbuffers *reqbuf)
934{
935 struct vpif_fh *fh = priv;
936 struct channel_obj *ch = fh->channel;
937 struct common_obj *common;
938 enum v4l2_field field;
2401dd25 939 struct vb2_queue *q;
e7332e3a 940 u8 index = 0;
b4a711e7 941 int ret;
e7332e3a
C
942
943 /* This file handle has not initialized the channel,
944 It is not allowed to do settings */
945 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
946 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
947 if (!fh->initialized) {
948 vpif_err("Channel Busy\n");
949 return -EBUSY;
950 }
951 }
952
953 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
954 return -EINVAL;
955
956 index = VPIF_VIDEO_INDEX;
957
958 common = &ch->common[index];
13df4f6a 959
fc613d44 960 if (common->fmt.type != reqbuf->type || !vpif_dev)
9bfaae24 961 return -EINVAL;
9bfaae24
HV
962 if (0 != common->io_usrs)
963 return -EBUSY;
e7332e3a
C
964
965 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
966 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
967 field = V4L2_FIELD_INTERLACED;
968 else
969 field = common->fmt.fmt.pix.field;
970 } else {
971 field = V4L2_VBI_INTERLACED;
972 }
2401dd25
LP
973 /* Initialize videobuf2 queue as per the buffer type */
974 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
94b76a88 975 if (IS_ERR(common->alloc_ctx)) {
2401dd25 976 vpif_err("Failed to get the context\n");
94b76a88 977 return PTR_ERR(common->alloc_ctx);
2401dd25
LP
978 }
979 q = &common->buffer_queue;
980 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
981 q->io_modes = VB2_MMAP | VB2_USERPTR;
982 q->drv_priv = fh;
983 q->ops = &video_qops;
984 q->mem_ops = &vb2_dma_contig_memops;
985 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
6aa69f99 986 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
e7332e3a 987
b4a711e7
LP
988 ret = vb2_queue_init(q);
989 if (ret) {
990 vpif_err("vpif_display: vb2_queue_init() failed\n");
991 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
992 return ret;
993 }
e7332e3a
C
994 /* Set io allowed member of file handle to TRUE */
995 fh->io_allowed[index] = 1;
996 /* Increment io usrs member of channel object to 1 */
997 common->io_usrs = 1;
998 /* Store type of memory requested in channel object */
999 common->memory = reqbuf->memory;
1000 INIT_LIST_HEAD(&common->dma_queue);
e7332e3a 1001 /* Allocate buffers */
2401dd25 1002 return vb2_reqbufs(&common->buffer_queue, reqbuf);
e7332e3a
C
1003}
1004
1005static int vpif_querybuf(struct file *file, void *priv,
1006 struct v4l2_buffer *tbuf)
1007{
1008 struct vpif_fh *fh = priv;
1009 struct channel_obj *ch = fh->channel;
1010 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1011
1012 if (common->fmt.type != tbuf->type)
1013 return -EINVAL;
1014
2401dd25 1015 return vb2_querybuf(&common->buffer_queue, tbuf);
e7332e3a
C
1016}
1017
1018static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1019{
2401dd25
LP
1020 struct vpif_fh *fh = NULL;
1021 struct channel_obj *ch = NULL;
1022 struct common_obj *common = NULL;
e7332e3a 1023
2401dd25
LP
1024 if (!buf || !priv)
1025 return -EINVAL;
e7332e3a 1026
2401dd25
LP
1027 fh = priv;
1028 ch = fh->channel;
1029 if (!ch)
1030 return -EINVAL;
1031
1032 common = &(ch->common[VPIF_VIDEO_INDEX]);
1033 if (common->fmt.type != buf->type)
e7332e3a
C
1034 return -EINVAL;
1035
1036 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1037 vpif_err("fh->io_allowed\n");
1038 return -EACCES;
1039 }
1040
2401dd25 1041 return vb2_qbuf(&common->buffer_queue, buf);
e7332e3a
C
1042}
1043
314527ac 1044static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
e7332e3a
C
1045{
1046 struct vpif_fh *fh = priv;
1047 struct channel_obj *ch = fh->channel;
1048 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1049 int ret = 0;
1050
314527ac 1051 if (!(std_id & VPIF_V4L2_STD))
e7332e3a
C
1052 return -EINVAL;
1053
1054 if (common->started) {
1055 vpif_err("streaming in progress\n");
1056 return -EBUSY;
1057 }
1058
1059 /* Call encoder subdevice function to set the standard */
314527ac 1060 ch->video.stdid = std_id;
0598c17b 1061 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
e7332e3a 1062 /* Get the information about the standard */
9bfaae24
HV
1063 if (vpif_update_resolution(ch))
1064 return -EINVAL;
e7332e3a
C
1065
1066 if ((ch->vpifparams.std_info.width *
1067 ch->vpifparams.std_info.height * 2) >
1068 config_params.channel_bufsize[ch->channel_id]) {
1069 vpif_err("invalid std for this size\n");
9bfaae24 1070 return -EINVAL;
e7332e3a
C
1071 }
1072
1073 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1074 /* Configure the default format information */
1075 vpif_config_format(ch);
1076
1077 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
314527ac 1078 s_std_output, std_id);
e7332e3a
C
1079 if (ret < 0) {
1080 vpif_err("Failed to set output standard\n");
9bfaae24 1081 return ret;
e7332e3a
C
1082 }
1083
1084 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
314527ac 1085 s_std, std_id);
e7332e3a
C
1086 if (ret < 0)
1087 vpif_err("Failed to set standard for sub devices\n");
e7332e3a
C
1088 return ret;
1089}
1090
1091static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1092{
1093 struct vpif_fh *fh = priv;
1094 struct channel_obj *ch = fh->channel;
1095
1096 *std = ch->video.stdid;
1097 return 0;
1098}
1099
1100static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1101{
1102 struct vpif_fh *fh = priv;
1103 struct channel_obj *ch = fh->channel;
1104 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1105
2401dd25 1106 return vb2_dqbuf(&common->buffer_queue, p,
e7332e3a
C
1107 (file->f_flags & O_NONBLOCK));
1108}
1109
1110static int vpif_streamon(struct file *file, void *priv,
1111 enum v4l2_buf_type buftype)
1112{
1113 struct vpif_fh *fh = priv;
1114 struct channel_obj *ch = fh->channel;
1115 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1116 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
e7332e3a
C
1117 int ret = 0;
1118
1119 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1120 vpif_err("buffer type not supported\n");
1121 return -EINVAL;
1122 }
1123
1124 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1125 vpif_err("fh->io_allowed\n");
1126 return -EACCES;
1127 }
1128
1129 /* If Streaming is already started, return error */
1130 if (common->started) {
1131 vpif_err("channel->started\n");
1132 return -EBUSY;
1133 }
1134
1135 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1136 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1137 ch->vpifparams.std_info.ycmux_mode == 0)
1138 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1139 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1140 vpif_err("other channel is using\n");
1141 return -EBUSY;
1142 }
1143
1144 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1145 if (ret < 0)
1146 return ret;
1147
2401dd25
LP
1148 /* Call vb2_streamon to start streaming in videobuf2 */
1149 ret = vb2_streamon(&common->buffer_queue, buftype);
e7332e3a 1150 if (ret < 0) {
2401dd25 1151 vpif_err("vb2_streamon\n");
e7332e3a
C
1152 return ret;
1153 }
1154
e7332e3a
C
1155 return ret;
1156}
1157
1158static int vpif_streamoff(struct file *file, void *priv,
1159 enum v4l2_buf_type buftype)
1160{
1161 struct vpif_fh *fh = priv;
1162 struct channel_obj *ch = fh->channel;
1163 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
6964b103
MH
1164 struct vpif_display_config *vpif_config_data =
1165 vpif_dev->platform_data;
e7332e3a
C
1166
1167 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1168 vpif_err("buffer type not supported\n");
1169 return -EINVAL;
1170 }
1171
1172 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1173 vpif_err("fh->io_allowed\n");
1174 return -EACCES;
1175 }
1176
1177 if (!common->started) {
1178 vpif_err("channel->started\n");
1179 return -EINVAL;
1180 }
1181
e7332e3a
C
1182 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1183 /* disable channel */
1184 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
2bd4e58c
LP
1185 if (vpif_config_data->
1186 chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
6964b103 1187 channel2_clipping_enable(0);
e7332e3a
C
1188 enable_channel2(0);
1189 channel2_intr_enable(0);
1190 }
1191 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1192 (2 == common->started)) {
2bd4e58c
LP
1193 if (vpif_config_data->
1194 chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
6964b103 1195 channel3_clipping_enable(0);
e7332e3a
C
1196 enable_channel3(0);
1197 channel3_intr_enable(0);
1198 }
1199 }
1200
1201 common->started = 0;
2401dd25 1202 return vb2_streamoff(&common->buffer_queue, buftype);
e7332e3a
C
1203}
1204
1205static int vpif_cropcap(struct file *file, void *priv,
1206 struct v4l2_cropcap *crop)
1207{
1208 struct vpif_fh *fh = priv;
1209 struct channel_obj *ch = fh->channel;
1210 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1211 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1212 return -EINVAL;
1213
1214 crop->bounds.left = crop->bounds.top = 0;
1215 crop->defrect.left = crop->defrect.top = 0;
1216 crop->defrect.height = crop->bounds.height = common->height;
1217 crop->defrect.width = crop->bounds.width = common->width;
1218
1219 return 0;
1220}
1221
1222static int vpif_enum_output(struct file *file, void *fh,
1223 struct v4l2_output *output)
1224{
1225
317b2e2f 1226 struct vpif_display_config *config = vpif_dev->platform_data;
2bd4e58c
LP
1227 struct vpif_display_chan_config *chan_cfg;
1228 struct vpif_fh *vpif_handler = fh;
1229 struct channel_obj *ch = vpif_handler->channel;
e7332e3a 1230
2bd4e58c
LP
1231 chan_cfg = &config->chan_config[ch->channel_id];
1232 if (output->index >= chan_cfg->output_count) {
e7332e3a
C
1233 vpif_dbg(1, debug, "Invalid output index\n");
1234 return -EINVAL;
1235 }
1236
2bd4e58c
LP
1237 *output = chan_cfg->outputs[output->index].output;
1238 return 0;
1239}
1240
1241/**
1242 * vpif_output_to_subdev() - Maps output to sub device
1243 * @vpif_cfg - global config ptr
1244 * @chan_cfg - channel config ptr
1245 * @index - Given output index from application
1246 *
1247 * lookup the sub device information for a given output index.
1248 * we report all the output to application. output table also
1249 * has sub device name for the each output
1250 */
1251static int
1252vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
1253 struct vpif_display_chan_config *chan_cfg, int index)
1254{
1255 struct vpif_subdev_info *subdev_info;
1256 const char *subdev_name;
1257 int i;
1258
1259 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
1260
1261 if (chan_cfg->outputs == NULL)
1262 return -1;
1263
1264 subdev_name = chan_cfg->outputs[index].subdev_name;
1265 if (subdev_name == NULL)
1266 return -1;
1267
1268 /* loop through the sub device list to get the sub device info */
1269 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1270 subdev_info = &vpif_cfg->subdevinfo[i];
1271 if (!strcmp(subdev_info->name, subdev_name))
1272 return i;
1273 }
1274 return -1;
1275}
1276
1277/**
1278 * vpif_set_output() - Select an output
1279 * @vpif_cfg - global config ptr
1280 * @ch - channel
1281 * @index - Given output index from application
1282 *
1283 * Select the given output.
1284 */
1285static int vpif_set_output(struct vpif_display_config *vpif_cfg,
1286 struct channel_obj *ch, int index)
1287{
1288 struct vpif_display_chan_config *chan_cfg =
1289 &vpif_cfg->chan_config[ch->channel_id];
1290 struct vpif_subdev_info *subdev_info = NULL;
1291 struct v4l2_subdev *sd = NULL;
1292 u32 input = 0, output = 0;
1293 int sd_index;
1294 int ret;
1295
1296 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
1297 if (sd_index >= 0) {
1298 sd = vpif_obj.sd[sd_index];
1299 subdev_info = &vpif_cfg->subdevinfo[sd_index];
1300 }
e7332e3a 1301
2bd4e58c
LP
1302 if (sd) {
1303 input = chan_cfg->outputs[index].input_route;
1304 output = chan_cfg->outputs[index].output_route;
1305 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1306 if (ret < 0 && ret != -ENOIOCTLCMD) {
1307 vpif_err("Failed to set output\n");
1308 return ret;
1309 }
1310
1311 }
1312 ch->output_idx = index;
1313 ch->sd = sd;
1314 if (chan_cfg->outputs != NULL)
1315 /* update tvnorms from the sub device output info */
1316 ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
e7332e3a
C
1317 return 0;
1318}
1319
1320static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1321{
2bd4e58c
LP
1322 struct vpif_display_config *config = vpif_dev->platform_data;
1323 struct vpif_display_chan_config *chan_cfg;
e7332e3a
C
1324 struct vpif_fh *fh = priv;
1325 struct channel_obj *ch = fh->channel;
e7332e3a 1326 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
2bd4e58c
LP
1327
1328 chan_cfg = &config->chan_config[ch->channel_id];
1329
1330 if (i >= chan_cfg->output_count)
1331 return -EINVAL;
e7332e3a 1332
e7332e3a
C
1333 if (common->started) {
1334 vpif_err("Streaming in progress\n");
9bfaae24 1335 return -EBUSY;
e7332e3a
C
1336 }
1337
2bd4e58c 1338 return vpif_set_output(config, ch, i);
e7332e3a
C
1339}
1340
1341static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1342{
1343 struct vpif_fh *fh = priv;
1344 struct channel_obj *ch = fh->channel;
e7332e3a 1345
311673ee 1346 *i = ch->output_idx;
e7332e3a
C
1347
1348 return 0;
1349}
1350
1351static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1352{
1353 struct vpif_fh *fh = priv;
1354 struct channel_obj *ch = fh->channel;
1355
1356 *p = v4l2_prio_max(&ch->prio);
1357
1358 return 0;
1359}
1360
1361static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1362{
1363 struct vpif_fh *fh = priv;
1364 struct channel_obj *ch = fh->channel;
1365
1366 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1367}
1368
40c8bcea 1369/**
0598c17b 1370 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
40c8bcea
MR
1371 * @file: file ptr
1372 * @priv: file handle
0598c17b 1373 * @timings: input timings
40c8bcea 1374 */
0598c17b
HV
1375static int
1376vpif_enum_dv_timings(struct file *file, void *priv,
1377 struct v4l2_enum_dv_timings *timings)
40c8bcea
MR
1378{
1379 struct vpif_fh *fh = priv;
1380 struct channel_obj *ch = fh->channel;
2bd4e58c 1381 int ret;
40c8bcea 1382
2bd4e58c 1383 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
63af4af5 1384 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
2bd4e58c
LP
1385 return -EINVAL;
1386 return ret;
40c8bcea
MR
1387}
1388
c027e165
MR
1389/**
1390 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1391 * @file: file ptr
1392 * @priv: file handle
1393 * @timings: digital video timings
1394 */
1395static int vpif_s_dv_timings(struct file *file, void *priv,
1396 struct v4l2_dv_timings *timings)
1397{
1398 struct vpif_fh *fh = priv;
1399 struct channel_obj *ch = fh->channel;
1400 struct vpif_params *vpifparams = &ch->vpifparams;
1401 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1402 struct video_obj *vid_ch = &ch->video;
0598c17b 1403 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
c027e165
MR
1404 int ret;
1405
1406 if (timings->type != V4L2_DV_BT_656_1120) {
1407 vpif_dbg(2, debug, "Timing type not defined\n");
1408 return -EINVAL;
1409 }
1410
1411 /* Configure subdevice timings, if any */
882084ad 1412 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
2bd4e58c
LP
1413 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1414 ret = 0;
1415 if (ret < 0) {
c027e165
MR
1416 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1417 return ret;
1418 }
1419
1420 if (!(timings->bt.width && timings->bt.height &&
1421 (timings->bt.hbackporch ||
1422 timings->bt.hfrontporch ||
1423 timings->bt.hsync) &&
1424 timings->bt.vfrontporch &&
1425 (timings->bt.vbackporch ||
1426 timings->bt.vsync))) {
1427 vpif_dbg(2, debug, "Timings for width, height, "
1428 "horizontal back porch, horizontal sync, "
1429 "horizontal front porch, vertical back porch, "
1430 "vertical sync and vertical back porch "
1431 "must be defined\n");
1432 return -EINVAL;
1433 }
1434
0598c17b 1435 vid_ch->dv_timings = *timings;
c027e165
MR
1436
1437 /* Configure video port timings */
1438
1439 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1440 bt->hsync - 8;
1441 std_info->sav2eav = bt->width;
1442
1443 std_info->l1 = 1;
1444 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1445
1446 if (bt->interlaced) {
1447 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1448 std_info->vsize = bt->height * 2 +
1449 bt->vfrontporch + bt->vsync + bt->vbackporch +
1450 bt->il_vfrontporch + bt->il_vsync +
1451 bt->il_vbackporch;
1452 std_info->l5 = std_info->vsize/2 -
1453 (bt->vfrontporch - 1);
1454 std_info->l7 = std_info->vsize/2 + 1;
1455 std_info->l9 = std_info->l7 + bt->il_vsync +
1456 bt->il_vbackporch + 1;
1457 std_info->l11 = std_info->vsize -
1458 (bt->il_vfrontporch - 1);
1459 } else {
1460 vpif_dbg(2, debug, "Required timing values for "
1461 "interlaced BT format missing\n");
1462 return -EINVAL;
1463 }
1464 } else {
1465 std_info->vsize = bt->height + bt->vfrontporch +
1466 bt->vsync + bt->vbackporch;
1467 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1468 }
1469 strncpy(std_info->name, "Custom timings BT656/1120",
1470 VPIF_MAX_NAME);
1471 std_info->width = bt->width;
1472 std_info->height = bt->height;
1473 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1474 std_info->ycmux_mode = 0;
1475 std_info->capture_format = 0;
1476 std_info->vbi_supported = 0;
1477 std_info->hd_sd = 1;
1478 std_info->stdid = 0;
c027e165 1479 vid_ch->stdid = 0;
c027e165
MR
1480
1481 return 0;
1482}
1483
1484/**
1485 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1486 * @file: file ptr
1487 * @priv: file handle
1488 * @timings: digital video timings
1489 */
1490static int vpif_g_dv_timings(struct file *file, void *priv,
1491 struct v4l2_dv_timings *timings)
1492{
1493 struct vpif_fh *fh = priv;
1494 struct channel_obj *ch = fh->channel;
1495 struct video_obj *vid_ch = &ch->video;
c027e165 1496
0598c17b 1497 *timings = vid_ch->dv_timings;
c027e165
MR
1498
1499 return 0;
1500}
7036d6a7 1501
7036d6a7
MR
1502/*
1503 * vpif_log_status() - Status information
1504 * @file: file ptr
1505 * @priv: file handle
1506 *
1507 * Returns zero.
1508 */
1509static int vpif_log_status(struct file *filep, void *priv)
1510{
1511 /* status for sub devices */
1512 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1513
1514 return 0;
1515}
1516
e7332e3a
C
1517/* vpif display ioctl operations */
1518static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1519 .vidioc_querycap = vpif_querycap,
1520 .vidioc_g_priority = vpif_g_priority,
1521 .vidioc_s_priority = vpif_s_priority,
1522 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1523 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1524 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1525 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1526 .vidioc_reqbufs = vpif_reqbufs,
1527 .vidioc_querybuf = vpif_querybuf,
1528 .vidioc_qbuf = vpif_qbuf,
1529 .vidioc_dqbuf = vpif_dqbuf,
1530 .vidioc_streamon = vpif_streamon,
1531 .vidioc_streamoff = vpif_streamoff,
1532 .vidioc_s_std = vpif_s_std,
1533 .vidioc_g_std = vpif_g_std,
1534 .vidioc_enum_output = vpif_enum_output,
1535 .vidioc_s_output = vpif_s_output,
1536 .vidioc_g_output = vpif_g_output,
1537 .vidioc_cropcap = vpif_cropcap,
0598c17b 1538 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
c027e165
MR
1539 .vidioc_s_dv_timings = vpif_s_dv_timings,
1540 .vidioc_g_dv_timings = vpif_g_dv_timings,
7036d6a7 1541 .vidioc_log_status = vpif_log_status,
e7332e3a
C
1542};
1543
1544static const struct v4l2_file_operations vpif_fops = {
1545 .owner = THIS_MODULE,
1546 .open = vpif_open,
1547 .release = vpif_release,
9bfaae24 1548 .unlocked_ioctl = video_ioctl2,
e7332e3a
C
1549 .mmap = vpif_mmap,
1550 .poll = vpif_poll
1551};
1552
1553static struct video_device vpif_video_template = {
1554 .name = "vpif",
1555 .fops = &vpif_fops,
e7332e3a 1556 .ioctl_ops = &vpif_ioctl_ops,
e7332e3a
C
1557};
1558
1559/*Configure the channels, buffer sizei, request irq */
1560static int initialize_vpif(void)
1561{
1562 int free_channel_objects_index;
1563 int free_buffer_channel_index;
1564 int free_buffer_index;
1565 int err = 0, i, j;
1566
1567 /* Default number of buffers should be 3 */
1568 if ((ch2_numbuffers > 0) &&
1569 (ch2_numbuffers < config_params.min_numbuffers))
1570 ch2_numbuffers = config_params.min_numbuffers;
1571 if ((ch3_numbuffers > 0) &&
1572 (ch3_numbuffers < config_params.min_numbuffers))
1573 ch3_numbuffers = config_params.min_numbuffers;
1574
1575 /* Set buffer size to min buffers size if invalid buffer size is
1576 * given */
1577 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1578 ch2_bufsize =
1579 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1580 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1581 ch3_bufsize =
1582 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1583
1584 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1585
1586 if (ch2_numbuffers) {
1587 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1588 ch2_bufsize;
1589 }
1590 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1591
1592 if (ch3_numbuffers) {
1593 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1594 ch3_bufsize;
1595 }
1596
1597 /* Allocate memory for six channel objects */
1598 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1599 vpif_obj.dev[i] =
1f8766b4 1600 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
e7332e3a
C
1601 /* If memory allocation fails, return error */
1602 if (!vpif_obj.dev[i]) {
1603 free_channel_objects_index = i;
1604 err = -ENOMEM;
1605 goto vpif_init_free_channel_objects;
1606 }
1607 }
1608
1609 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1610 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1611 free_buffer_index = config_params.numbuffers[i - 1];
1612
1613 return 0;
1614
1615vpif_init_free_channel_objects:
1616 for (j = 0; j < free_channel_objects_index; j++)
1617 kfree(vpif_obj.dev[j]);
1618 return err;
1619}
1620
1621/*
1622 * vpif_probe: This function creates device entries by register itself to the
1623 * V4L2 driver and initializes fields of each channel objects
1624 */
1625static __init int vpif_probe(struct platform_device *pdev)
1626{
317b2e2f
MK
1627 struct vpif_subdev_info *subdevdata;
1628 struct vpif_display_config *config;
01b1d975
HV
1629 int i, j = 0, k, err = 0;
1630 int res_idx = 0;
e7332e3a 1631 struct i2c_adapter *i2c_adap;
e7332e3a
C
1632 struct common_obj *common;
1633 struct channel_obj *ch;
1634 struct video_device *vfd;
1635 struct resource *res;
1636 int subdev_count;
fc613d44 1637 size_t size;
e7332e3a
C
1638
1639 vpif_dev = &pdev->dev;
317b2e2f 1640 err = initialize_vpif();
e7332e3a 1641
317b2e2f
MK
1642 if (err) {
1643 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1644 return err;
e7332e3a
C
1645 }
1646
e7332e3a
C
1647 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1648 if (err) {
1649 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1650 return err;
1651 }
1652
01b1d975 1653 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
e7332e3a 1654 for (i = res->start; i <= res->end; i++) {
0316b89a 1655 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
01b1d975
HV
1656 "VPIF_Display", (void *)
1657 (&vpif_obj.dev[res_idx]->channel_id))) {
e7332e3a 1658 err = -EBUSY;
01b1d975
HV
1659 for (j = 0; j < i; j++)
1660 free_irq(j, (void *)
1661 (&vpif_obj.dev[res_idx]->channel_id));
c8f089c9 1662 vpif_err("VPIF IRQ request failed\n");
e7332e3a
C
1663 goto vpif_int_err;
1664 }
1665 }
01b1d975 1666 res_idx++;
e7332e3a
C
1667 }
1668
1669 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
e7332e3a
C
1670 /* Get the pointer to the channel object */
1671 ch = vpif_obj.dev[i];
1672
1673 /* Allocate memory for video device */
1674 vfd = video_device_alloc();
1675 if (vfd == NULL) {
1676 for (j = 0; j < i; j++) {
1677 ch = vpif_obj.dev[j];
1678 video_device_release(ch->video_dev);
1679 }
1680 err = -ENOMEM;
317b2e2f 1681 goto vpif_int_err;
e7332e3a
C
1682 }
1683
1684 /* Initialize field of video device */
1685 *vfd = vpif_video_template;
1686 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1687 vfd->release = video_device_release;
954f340f 1688 vfd->vfl_dir = VFL_DIR_TX;
e7332e3a 1689 snprintf(vfd->name, sizeof(vfd->name),
0a63172a 1690 "VPIF_Display_DRIVER_V%s",
64dc3c1a 1691 VPIF_DISPLAY_VERSION);
e7332e3a
C
1692
1693 /* Set video_dev to the video device */
1694 ch->video_dev = vfd;
1695 }
1696
fc613d44
MH
1697 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1698 if (res) {
1699 size = resource_size(res);
1700 /* The resources are divided into two equal memory and when
1701 * we have HD output we can add them together
1702 */
1703 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1704 ch = vpif_obj.dev[j];
1705 ch->channel_id = j;
1706
1707 /* only enabled if second resource exists */
1708 config_params.video_limit[ch->channel_id] = 0;
1709 if (size)
1710 config_params.video_limit[ch->channel_id] =
1711 size/2;
1712 }
1713 }
1714
e6067f8b
HV
1715 i2c_adap = i2c_get_adapter(1);
1716 config = pdev->dev.platform_data;
1717 subdev_count = config->subdev_count;
1718 subdevdata = config->subdevinfo;
1719 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1720 GFP_KERNEL);
1721 if (vpif_obj.sd == NULL) {
1722 vpif_err("unable to allocate memory for subdevice pointers\n");
1723 err = -ENOMEM;
01b1d975 1724 goto vpif_sd_error;
e6067f8b
HV
1725 }
1726
1727 for (i = 0; i < subdev_count; i++) {
1728 vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1729 i2c_adap,
1730 &subdevdata[i].board_info,
1731 NULL);
1732 if (!vpif_obj.sd[i]) {
1733 vpif_err("Error registering v4l2 subdevice\n");
b2d2cf10 1734 err = -ENODEV;
e6067f8b
HV
1735 goto probe_subdev_out;
1736 }
1737
1738 if (vpif_obj.sd[i])
1739 vpif_obj.sd[i]->grp_id = 1 << i;
1740 }
1741
e7332e3a
C
1742 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1743 ch = vpif_obj.dev[j];
1744 /* Initialize field of the channel objects */
1745 atomic_set(&ch->usrs, 0);
1746 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1747 ch->common[k].numbuffers = 0;
1748 common = &ch->common[k];
1749 common->io_usrs = 0;
1750 common->started = 0;
1751 spin_lock_init(&common->irqlock);
1752 mutex_init(&common->lock);
1753 common->numbuffers = 0;
1754 common->set_addr = NULL;
1755 common->ytop_off = common->ybtm_off = 0;
1756 common->ctop_off = common->cbtm_off = 0;
1757 common->cur_frm = common->next_frm = NULL;
1758 memset(&common->fmt, 0, sizeof(common->fmt));
1759 common->numbuffers = config_params.numbuffers[k];
1760
1761 }
1762 ch->initialized = 0;
882084ad
HV
1763 if (subdev_count)
1764 ch->sd = vpif_obj.sd[0];
e7332e3a
C
1765 ch->channel_id = j;
1766 if (j < 2)
1767 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1768 config_params.numbuffers[ch->channel_id];
1769 else
1770 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1771
1772 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1773
1774 /* Initialize prio member of channel object */
1775 v4l2_prio_init(&ch->prio);
1776 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1777 V4L2_BUF_TYPE_VIDEO_OUTPUT;
9bfaae24 1778 ch->video_dev->lock = &common->lock;
e6067f8b 1779 video_set_drvdata(ch->video_dev, ch);
e7332e3a 1780
2bd4e58c
LP
1781 /* select output 0 */
1782 err = vpif_set_output(config, ch, 0);
1783 if (err)
1784 goto probe_out;
1785
e7332e3a
C
1786 /* register video device */
1787 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1788 (int)ch, (int)&ch->video_dev);
1789
1790 err = video_register_device(ch->video_dev,
1791 VFL_TYPE_GRABBER, (j ? 3 : 2));
1792 if (err < 0)
1793 goto probe_out;
e7332e3a
C
1794 }
1795
2c0ddd17 1796 v4l2_info(&vpif_obj.v4l2_dev,
0a63172a 1797 " VPIF display driver initialized\n");
e7332e3a
C
1798 return 0;
1799
e7332e3a
C
1800probe_out:
1801 for (k = 0; k < j; k++) {
1802 ch = vpif_obj.dev[k];
1803 video_unregister_device(ch->video_dev);
1804 video_device_release(ch->video_dev);
1805 ch->video_dev = NULL;
1806 }
e6067f8b
HV
1807probe_subdev_out:
1808 kfree(vpif_obj.sd);
01b1d975
HV
1809vpif_sd_error:
1810 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1811 ch = vpif_obj.dev[i];
1812 /* Note: does nothing if ch->video_dev == NULL */
1813 video_device_release(ch->video_dev);
1814 }
e7332e3a
C
1815vpif_int_err:
1816 v4l2_device_unregister(&vpif_obj.v4l2_dev);
01b1d975
HV
1817 for (i = 0; i < res_idx; i++) {
1818 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1819 for (j = res->start; j <= res->end; j++)
1820 free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
e7332e3a 1821 }
e7332e3a
C
1822
1823 return err;
1824}
1825
1826/*
1827 * vpif_remove: It un-register channels from V4L2 driver
1828 */
1829static int vpif_remove(struct platform_device *device)
1830{
1831 struct channel_obj *ch;
0b361583
LP
1832 struct resource *res;
1833 int irq_num;
1834 int i = 0;
1835
1836 while ((res = platform_get_resource(device, IORESOURCE_IRQ, i))) {
1837 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1838 free_irq(irq_num,
1839 (void *)(&vpif_obj.dev[i]->channel_id));
1840 i++;
1841 }
e7332e3a
C
1842
1843 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1844
0b361583 1845 kfree(vpif_obj.sd);
e7332e3a
C
1846 /* un-register device */
1847 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1848 /* Get the pointer to the channel object */
1849 ch = vpif_obj.dev[i];
1850 /* Unregister video device */
1851 video_unregister_device(ch->video_dev);
1852
1853 ch->video_dev = NULL;
0b361583 1854 kfree(vpif_obj.dev[i]);
e7332e3a
C
1855 }
1856
1857 return 0;
1858}
1859
e9530dac
MH
1860#ifdef CONFIG_PM
1861static int vpif_suspend(struct device *dev)
1862{
1863 struct common_obj *common;
1864 struct channel_obj *ch;
1865 int i;
1866
1867 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1868 /* Get the pointer to the channel object */
1869 ch = vpif_obj.dev[i];
1870 common = &ch->common[VPIF_VIDEO_INDEX];
1871 mutex_lock(&common->lock);
1872 if (atomic_read(&ch->usrs) && common->io_usrs) {
1873 /* Disable channel */
1874 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1875 enable_channel2(0);
1876 channel2_intr_enable(0);
1877 }
1878 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1879 common->started == 2) {
1880 enable_channel3(0);
1881 channel3_intr_enable(0);
1882 }
1883 }
1884 mutex_unlock(&common->lock);
1885 }
1886
1887 return 0;
1888}
1889
1890static int vpif_resume(struct device *dev)
1891{
1892
1893 struct common_obj *common;
1894 struct channel_obj *ch;
1895 int i;
1896
1897 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1898 /* Get the pointer to the channel object */
1899 ch = vpif_obj.dev[i];
1900 common = &ch->common[VPIF_VIDEO_INDEX];
1901 mutex_lock(&common->lock);
1902 if (atomic_read(&ch->usrs) && common->io_usrs) {
1903 /* Enable channel */
1904 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1905 enable_channel2(1);
1906 channel2_intr_enable(1);
1907 }
1908 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1909 common->started == 2) {
1910 enable_channel3(1);
1911 channel3_intr_enable(1);
1912 }
1913 }
1914 mutex_unlock(&common->lock);
1915 }
1916
1917 return 0;
1918}
1919
1920static const struct dev_pm_ops vpif_pm = {
1921 .suspend = vpif_suspend,
1922 .resume = vpif_resume,
1923};
1924
1925#define vpif_pm_ops (&vpif_pm)
1926#else
1927#define vpif_pm_ops NULL
1928#endif
1929
ffa1b391 1930static __refdata struct platform_driver vpif_driver = {
e7332e3a
C
1931 .driver = {
1932 .name = "vpif_display",
1933 .owner = THIS_MODULE,
e9530dac 1934 .pm = vpif_pm_ops,
e7332e3a
C
1935 },
1936 .probe = vpif_probe,
1937 .remove = vpif_remove,
1938};
1939
1940static __init int vpif_init(void)
1941{
1942 return platform_driver_register(&vpif_driver);
1943}
1944
1945/*
1946 * vpif_cleanup: This function un-registers device and driver to the kernel,
1947 * frees requested irq handler and de-allocates memory allocated for channel
1948 * objects.
1949 */
1950static void vpif_cleanup(void)
1951{
e7332e3a 1952 platform_driver_unregister(&vpif_driver);
e7332e3a
C
1953}
1954
1955module_init(vpif_init);
1956module_exit(vpif_cleanup);