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