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