V4L/DVB (12540): v4l: simplify v4l2_i2c_new_subdev and friends
[deliverable/linux.git] / drivers / media / video / davinci / vpif_display.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>
32 #include <linux/version.h>
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>
40
41 #include <mach/dm646x.h>
42
43 #include "vpif_display.h"
44 #include "vpif.h"
45
46 MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
47 MODULE_LICENSE("GPL");
48
49 #define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
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
55 static int debug = 1;
56 static u32 ch2_numbuffers = 3;
57 static u32 ch3_numbuffers = 3;
58 static u32 ch2_bufsize = 1920 * 1080 * 2;
59 static u32 ch3_bufsize = 720 * 576 * 2;
60
61 module_param(debug, int, 0644);
62 module_param(ch2_numbuffers, uint, S_IRUGO);
63 module_param(ch3_numbuffers, uint, S_IRUGO);
64 module_param(ch2_bufsize, uint, S_IRUGO);
65 module_param(ch3_bufsize, uint, S_IRUGO);
66
67 MODULE_PARM_DESC(debug, "Debug level 0-1");
68 MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
69 MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
70 MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
71 MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
72
73 static 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
83 static struct vpif_device vpif_obj = { {NULL} };
84 static struct device *vpif_dev;
85
86 static const struct vpif_channel_config_params ch_params[] = {
87 {
88 "NTSC", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
89 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
90 },
91 {
92 "PAL", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
93 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
94 },
95 };
96
97 /*
98 * vpif_uservirt_to_phys: This function is used to convert user
99 * space virtual address to physical address.
100 */
101 static u32 vpif_uservirt_to_phys(u32 virtp)
102 {
103 struct mm_struct *mm = current->mm;
104 unsigned long physp = 0;
105 struct vm_area_struct *vma;
106
107 vma = find_vma(mm, virtp);
108
109 /* For kernel direct-mapped memory, take the easy way */
110 if (virtp >= PAGE_OFFSET) {
111 physp = virt_to_phys((void *)virtp);
112 } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
113 /* this will catch, kernel-allocated, mmaped-to-usermode addr */
114 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
115 } else {
116 /* otherwise, use get_user_pages() for general userland pages */
117 int res, nr_pages = 1;
118 struct page *pages;
119 down_read(&current->mm->mmap_sem);
120
121 res = get_user_pages(current, current->mm,
122 virtp, nr_pages, 1, 0, &pages, NULL);
123 up_read(&current->mm->mmap_sem);
124
125 if (res == nr_pages) {
126 physp = __pa(page_address(&pages[0]) +
127 (virtp & ~PAGE_MASK));
128 } else {
129 vpif_err("get_user_pages failed\n");
130 return 0;
131 }
132 }
133
134 return physp;
135 }
136
137 /*
138 * buffer_prepare: This is the callback function called from videobuf_qbuf()
139 * function the buffer is prepared and user space virtual address is converted
140 * into physical address
141 */
142 static int vpif_buffer_prepare(struct videobuf_queue *q,
143 struct videobuf_buffer *vb,
144 enum v4l2_field field)
145 {
146 struct vpif_fh *fh = q->priv_data;
147 struct common_obj *common;
148 unsigned long addr;
149
150 common = &fh->channel->common[VPIF_VIDEO_INDEX];
151 if (VIDEOBUF_NEEDS_INIT == vb->state) {
152 vb->width = common->width;
153 vb->height = common->height;
154 vb->size = vb->width * vb->height;
155 vb->field = field;
156 }
157 vb->state = VIDEOBUF_PREPARED;
158
159 /* if user pointer memory mechanism is used, get the physical
160 * address of the buffer */
161 if (V4L2_MEMORY_USERPTR == common->memory) {
162 if (!vb->baddr) {
163 vpif_err("buffer_address is 0\n");
164 return -EINVAL;
165 }
166
167 vb->boff = vpif_uservirt_to_phys(vb->baddr);
168 if (!ISALIGNED(vb->boff))
169 goto buf_align_exit;
170 }
171
172 addr = vb->boff;
173 if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
174 if (!ISALIGNED(addr + common->ytop_off) ||
175 !ISALIGNED(addr + common->ybtm_off) ||
176 !ISALIGNED(addr + common->ctop_off) ||
177 !ISALIGNED(addr + common->cbtm_off))
178 goto buf_align_exit;
179 }
180 return 0;
181
182 buf_align_exit:
183 vpif_err("buffer offset not aligned to 8 bytes\n");
184 return -EINVAL;
185 }
186
187 /*
188 * vpif_buffer_setup: This function allocates memory for the buffers
189 */
190 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
191 unsigned int *size)
192 {
193 struct vpif_fh *fh = q->priv_data;
194 struct channel_obj *ch = fh->channel;
195 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
196
197 if (V4L2_MEMORY_MMAP != common->memory)
198 return 0;
199
200 *size = config_params.channel_bufsize[ch->channel_id];
201 if (*count < config_params.min_numbuffers)
202 *count = config_params.min_numbuffers;
203
204 return 0;
205 }
206
207 /*
208 * vpif_buffer_queue: This function adds the buffer to DMA queue
209 */
210 static void vpif_buffer_queue(struct videobuf_queue *q,
211 struct videobuf_buffer *vb)
212 {
213 struct vpif_fh *fh = q->priv_data;
214 struct common_obj *common;
215
216 common = &fh->channel->common[VPIF_VIDEO_INDEX];
217
218 /* add the buffer to the DMA queue */
219 list_add_tail(&vb->queue, &common->dma_queue);
220 vb->state = VIDEOBUF_QUEUED;
221 }
222
223 /*
224 * vpif_buffer_release: This function is called from the videobuf layer to
225 * free memory allocated to the buffers
226 */
227 static void vpif_buffer_release(struct videobuf_queue *q,
228 struct videobuf_buffer *vb)
229 {
230 struct vpif_fh *fh = q->priv_data;
231 struct channel_obj *ch = fh->channel;
232 struct common_obj *common;
233 unsigned int buf_size = 0;
234
235 common = &ch->common[VPIF_VIDEO_INDEX];
236
237 videobuf_dma_contig_free(q, vb);
238 vb->state = VIDEOBUF_NEEDS_INIT;
239
240 if (V4L2_MEMORY_MMAP != common->memory)
241 return;
242
243 buf_size = config_params.channel_bufsize[ch->channel_id];
244 }
245
246 static struct videobuf_queue_ops video_qops = {
247 .buf_setup = vpif_buffer_setup,
248 .buf_prepare = vpif_buffer_prepare,
249 .buf_queue = vpif_buffer_queue,
250 .buf_release = vpif_buffer_release,
251 };
252 static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
253
254 static void process_progressive_mode(struct common_obj *common)
255 {
256 unsigned long addr = 0;
257
258 /* Get the next buffer from buffer queue */
259 common->next_frm = list_entry(common->dma_queue.next,
260 struct videobuf_buffer, queue);
261 /* Remove that buffer from the buffer queue */
262 list_del(&common->next_frm->queue);
263 /* Mark status of the buffer as active */
264 common->next_frm->state = VIDEOBUF_ACTIVE;
265
266 /* Set top and bottom field addrs in VPIF registers */
267 addr = videobuf_to_dma_contig(common->next_frm);
268 common->set_addr(addr + common->ytop_off,
269 addr + common->ybtm_off,
270 addr + common->ctop_off,
271 addr + common->cbtm_off);
272 }
273
274 static void process_interlaced_mode(int fid, struct common_obj *common)
275 {
276 /* device field id and local field id are in sync */
277 /* If this is even field */
278 if (0 == fid) {
279 if (common->cur_frm == common->next_frm)
280 return;
281
282 /* one frame is displayed If next frame is
283 * available, release cur_frm and move on */
284 /* Copy frame display time */
285 do_gettimeofday(&common->cur_frm->ts);
286 /* Change status of the cur_frm */
287 common->cur_frm->state = VIDEOBUF_DONE;
288 /* unlock semaphore on cur_frm */
289 wake_up_interruptible(&common->cur_frm->done);
290 /* Make cur_frm pointing to next_frm */
291 common->cur_frm = common->next_frm;
292
293 } else if (1 == fid) { /* odd field */
294 if (list_empty(&common->dma_queue)
295 || (common->cur_frm != common->next_frm)) {
296 return;
297 }
298 /* one field is displayed configure the next
299 * frame if it is available else hold on current
300 * frame */
301 /* Get next from the buffer queue */
302 process_progressive_mode(common);
303
304 }
305 }
306
307 /*
308 * vpif_channel_isr: It changes status of the displayed buffer, takes next
309 * buffer from the queue and sets its address in VPIF registers
310 */
311 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
312 {
313 struct vpif_device *dev = &vpif_obj;
314 struct channel_obj *ch;
315 struct common_obj *common;
316 enum v4l2_field field;
317 int fid = -1, i;
318 int channel_id = 0;
319
320 channel_id = *(int *)(dev_id);
321 ch = dev->dev[channel_id];
322 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
323 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
324 common = &ch->common[i];
325 /* If streaming is started in this channel */
326 if (0 == common->started)
327 continue;
328
329 if (1 == ch->vpifparams.std_info.frm_fmt) {
330 if (list_empty(&common->dma_queue))
331 continue;
332
333 /* Progressive mode */
334 if (!channel_first_int[i][channel_id]) {
335 /* Mark status of the cur_frm to
336 * done and unlock semaphore on it */
337 do_gettimeofday(&common->cur_frm->ts);
338 common->cur_frm->state = VIDEOBUF_DONE;
339 wake_up_interruptible(&common->cur_frm->done);
340 /* Make cur_frm pointing to next_frm */
341 common->cur_frm = common->next_frm;
342 }
343
344 channel_first_int[i][channel_id] = 0;
345 process_progressive_mode(common);
346 } else {
347 /* Interlaced mode */
348 /* If it is first interrupt, ignore it */
349
350 if (channel_first_int[i][channel_id]) {
351 channel_first_int[i][channel_id] = 0;
352 continue;
353 }
354
355 if (0 == i) {
356 ch->field_id ^= 1;
357 /* Get field id from VPIF registers */
358 fid = vpif_channel_getfid(ch->channel_id + 2);
359 /* If fid does not match with stored field id */
360 if (fid != ch->field_id) {
361 /* Make them in sync */
362 if (0 == fid)
363 ch->field_id = fid;
364
365 return IRQ_HANDLED;
366 }
367 }
368 process_interlaced_mode(fid, common);
369 }
370 }
371
372 return IRQ_HANDLED;
373 }
374
375 static int vpif_get_std_info(struct channel_obj *ch)
376 {
377 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
378 struct video_obj *vid_ch = &ch->video;
379 struct vpif_params *vpifparams = &ch->vpifparams;
380 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
381 const struct vpif_channel_config_params *config;
382
383 int index;
384
385 std_info->stdid = vid_ch->stdid;
386 if (!std_info)
387 return -1;
388
389 for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
390 config = &ch_params[index];
391 if (config->stdid & std_info->stdid) {
392 memcpy(std_info, config, sizeof(*config));
393 break;
394 }
395 }
396
397 if (index == ARRAY_SIZE(ch_params))
398 return -1;
399
400 common->fmt.fmt.pix.width = std_info->width;
401 common->fmt.fmt.pix.height = std_info->height;
402 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
403 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
404
405 /* Set height and width paramateres */
406 ch->common[VPIF_VIDEO_INDEX].height = std_info->height;
407 ch->common[VPIF_VIDEO_INDEX].width = std_info->width;
408
409 return 0;
410 }
411
412 /*
413 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
414 * in the top and bottom field
415 */
416 static void vpif_calculate_offsets(struct channel_obj *ch)
417 {
418 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
419 struct vpif_params *vpifparams = &ch->vpifparams;
420 enum v4l2_field field = common->fmt.fmt.pix.field;
421 struct video_obj *vid_ch = &ch->video;
422 unsigned int hpitch, vpitch, sizeimage;
423
424 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
425 if (ch->vpifparams.std_info.frm_fmt)
426 vid_ch->buf_field = V4L2_FIELD_NONE;
427 else
428 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
429 } else {
430 vid_ch->buf_field = common->fmt.fmt.pix.field;
431 }
432
433 if (V4L2_MEMORY_USERPTR == common->memory)
434 sizeimage = common->fmt.fmt.pix.sizeimage;
435 else
436 sizeimage = config_params.channel_bufsize[ch->channel_id];
437
438 hpitch = common->fmt.fmt.pix.bytesperline;
439 vpitch = sizeimage / (hpitch * 2);
440 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
441 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
442 common->ytop_off = 0;
443 common->ybtm_off = hpitch;
444 common->ctop_off = sizeimage / 2;
445 common->cbtm_off = sizeimage / 2 + hpitch;
446 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
447 common->ytop_off = 0;
448 common->ybtm_off = sizeimage / 4;
449 common->ctop_off = sizeimage / 2;
450 common->cbtm_off = common->ctop_off + sizeimage / 4;
451 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
452 common->ybtm_off = 0;
453 common->ytop_off = sizeimage / 4;
454 common->cbtm_off = sizeimage / 2;
455 common->ctop_off = common->cbtm_off + sizeimage / 4;
456 }
457
458 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
459 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
460 vpifparams->video_params.storage_mode = 1;
461 } else {
462 vpifparams->video_params.storage_mode = 0;
463 }
464
465 if (ch->vpifparams.std_info.frm_fmt == 1) {
466 vpifparams->video_params.hpitch =
467 common->fmt.fmt.pix.bytesperline;
468 } else {
469 if ((field == V4L2_FIELD_ANY) ||
470 (field == V4L2_FIELD_INTERLACED))
471 vpifparams->video_params.hpitch =
472 common->fmt.fmt.pix.bytesperline * 2;
473 else
474 vpifparams->video_params.hpitch =
475 common->fmt.fmt.pix.bytesperline;
476 }
477
478 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
479 }
480
481 static void vpif_config_format(struct channel_obj *ch)
482 {
483 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
484
485 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
486 if (config_params.numbuffers[ch->channel_id] == 0)
487 common->memory = V4L2_MEMORY_USERPTR;
488 else
489 common->memory = V4L2_MEMORY_MMAP;
490
491 common->fmt.fmt.pix.sizeimage =
492 config_params.channel_bufsize[ch->channel_id];
493 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
494 common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
495 }
496
497 static int vpif_check_format(struct channel_obj *ch,
498 struct v4l2_pix_format *pixfmt)
499 {
500 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
501 enum v4l2_field field = pixfmt->field;
502 u32 sizeimage, hpitch, vpitch;
503
504 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
505 goto invalid_fmt_exit;
506
507 if (!(VPIF_VALID_FIELD(field)))
508 goto invalid_fmt_exit;
509
510 if (pixfmt->bytesperline <= 0)
511 goto invalid_pitch_exit;
512
513 if (V4L2_MEMORY_USERPTR == common->memory)
514 sizeimage = pixfmt->sizeimage;
515 else
516 sizeimage = config_params.channel_bufsize[ch->channel_id];
517
518 if (vpif_get_std_info(ch)) {
519 vpif_err("Error getting the standard info\n");
520 return -EINVAL;
521 }
522
523 hpitch = pixfmt->bytesperline;
524 vpitch = sizeimage / (hpitch * 2);
525
526 /* Check for valid value of pitch */
527 if ((hpitch < ch->vpifparams.std_info.width) ||
528 (vpitch < ch->vpifparams.std_info.height))
529 goto invalid_pitch_exit;
530
531 /* Check for 8 byte alignment */
532 if (!ISALIGNED(hpitch)) {
533 vpif_err("invalid pitch alignment\n");
534 return -EINVAL;
535 }
536 pixfmt->width = common->fmt.fmt.pix.width;
537 pixfmt->height = common->fmt.fmt.pix.height;
538
539 return 0;
540
541 invalid_fmt_exit:
542 vpif_err("invalid field format\n");
543 return -EINVAL;
544
545 invalid_pitch_exit:
546 vpif_err("invalid pitch\n");
547 return -EINVAL;
548 }
549
550 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
551 {
552 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
553
554 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
555 common->set_addr = ch3_set_videobuf_addr;
556 } else {
557 if (2 == muxmode)
558 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
559 else
560 common->set_addr = ch2_set_videobuf_addr;
561 }
562 }
563
564 /*
565 * vpif_mmap: It is used to map kernel space buffers into user spaces
566 */
567 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
568 {
569 struct vpif_fh *fh = filep->private_data;
570 struct common_obj *common = &fh->channel->common[VPIF_VIDEO_INDEX];
571
572 return videobuf_mmap_mapper(&common->buffer_queue, vma);
573 }
574
575 /*
576 * vpif_poll: It is used for select/poll system call
577 */
578 static unsigned int vpif_poll(struct file *filep, poll_table *wait)
579 {
580 struct vpif_fh *fh = filep->private_data;
581 struct channel_obj *ch = fh->channel;
582 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
583
584 if (common->started)
585 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
586
587 return 0;
588 }
589
590 /*
591 * vpif_open: It creates object of file handle structure and stores it in
592 * private_data member of filepointer
593 */
594 static int vpif_open(struct file *filep)
595 {
596 struct video_device *vdev = video_devdata(filep);
597 struct channel_obj *ch = NULL;
598 struct vpif_fh *fh = NULL;
599
600 ch = video_get_drvdata(vdev);
601 /* Allocate memory for the file handle object */
602 fh = kmalloc(sizeof(struct vpif_fh), GFP_KERNEL);
603 if (fh == NULL) {
604 vpif_err("unable to allocate memory for file handle object\n");
605 return -ENOMEM;
606 }
607
608 /* store pointer to fh in private_data member of filep */
609 filep->private_data = fh;
610 fh->channel = ch;
611 fh->initialized = 0;
612 if (!ch->initialized) {
613 fh->initialized = 1;
614 ch->initialized = 1;
615 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
616 }
617
618 /* Increment channel usrs counter */
619 atomic_inc(&ch->usrs);
620 /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
621 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
622 /* Initialize priority of this instance to default priority */
623 fh->prio = V4L2_PRIORITY_UNSET;
624 v4l2_prio_open(&ch->prio, &fh->prio);
625
626 return 0;
627 }
628
629 /*
630 * vpif_release: This function deletes buffer queue, frees the buffers and
631 * the vpif file handle
632 */
633 static int vpif_release(struct file *filep)
634 {
635 struct vpif_fh *fh = filep->private_data;
636 struct channel_obj *ch = fh->channel;
637 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
638
639 if (mutex_lock_interruptible(&common->lock))
640 return -ERESTARTSYS;
641
642 /* if this instance is doing IO */
643 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
644 /* Reset io_usrs member of channel object */
645 common->io_usrs = 0;
646 /* Disable channel */
647 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
648 enable_channel2(0);
649 channel2_intr_enable(0);
650 }
651 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
652 (2 == common->started)) {
653 enable_channel3(0);
654 channel3_intr_enable(0);
655 }
656 common->started = 0;
657 /* Free buffers allocated */
658 videobuf_queue_cancel(&common->buffer_queue);
659 videobuf_mmap_free(&common->buffer_queue);
660 common->numbuffers =
661 config_params.numbuffers[ch->channel_id];
662 }
663
664 mutex_unlock(&common->lock);
665
666 /* Decrement channel usrs counter */
667 atomic_dec(&ch->usrs);
668 /* If this file handle has initialize encoder device, reset it */
669 if (fh->initialized)
670 ch->initialized = 0;
671
672 /* Close the priority */
673 v4l2_prio_close(&ch->prio, &fh->prio);
674 filep->private_data = NULL;
675 fh->initialized = 0;
676 kfree(fh);
677
678 return 0;
679 }
680
681 /* functions implementing ioctls */
682
683 static int vpif_querycap(struct file *file, void *priv,
684 struct v4l2_capability *cap)
685 {
686 struct vpif_config *config = vpif_dev->platform_data;
687
688 cap->version = VPIF_DISPLAY_VERSION_CODE;
689 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
690 strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
691 strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
692 strlcpy(cap->card, config->card_name, sizeof(cap->card));
693
694 return 0;
695 }
696
697 static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
698 struct v4l2_fmtdesc *fmt)
699 {
700 if (fmt->index != 0) {
701 vpif_err("Invalid format index\n");
702 return -EINVAL;
703 }
704
705 /* Fill in the information about format */
706 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
707 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
708 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
709
710 return 0;
711 }
712
713 static int vpif_g_fmt_vid_out(struct file *file, void *priv,
714 struct v4l2_format *fmt)
715 {
716 struct vpif_fh *fh = priv;
717 struct channel_obj *ch = fh->channel;
718 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
719
720 /* Check the validity of the buffer type */
721 if (common->fmt.type != fmt->type)
722 return -EINVAL;
723
724 /* Fill in the information about format */
725 if (mutex_lock_interruptible(&common->lock))
726 return -ERESTARTSYS;
727
728 if (vpif_get_std_info(ch)) {
729 vpif_err("Error getting the standard info\n");
730 return -EINVAL;
731 }
732
733 *fmt = common->fmt;
734 mutex_unlock(&common->lock);
735 return 0;
736 }
737
738 static int vpif_s_fmt_vid_out(struct file *file, void *priv,
739 struct v4l2_format *fmt)
740 {
741 struct vpif_fh *fh = priv;
742 struct v4l2_pix_format *pixfmt;
743 struct channel_obj *ch = fh->channel;
744 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
745 int ret = 0;
746
747 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
748 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
749 if (!fh->initialized) {
750 vpif_dbg(1, debug, "Channel Busy\n");
751 return -EBUSY;
752 }
753
754 /* Check for the priority */
755 ret = v4l2_prio_check(&ch->prio, &fh->prio);
756 if (0 != ret)
757 return ret;
758 fh->initialized = 1;
759 }
760
761 if (common->started) {
762 vpif_dbg(1, debug, "Streaming in progress\n");
763 return -EBUSY;
764 }
765
766 pixfmt = &fmt->fmt.pix;
767 /* Check for valid field format */
768 ret = vpif_check_format(ch, pixfmt);
769 if (ret)
770 return ret;
771
772 /* store the pix format in the channel object */
773 common->fmt.fmt.pix = *pixfmt;
774 /* store the format in the channel object */
775 if (mutex_lock_interruptible(&common->lock))
776 return -ERESTARTSYS;
777
778 common->fmt = *fmt;
779 mutex_unlock(&common->lock);
780
781 return 0;
782 }
783
784 static int vpif_try_fmt_vid_out(struct file *file, void *priv,
785 struct v4l2_format *fmt)
786 {
787 struct vpif_fh *fh = priv;
788 struct channel_obj *ch = fh->channel;
789 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
790 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
791 int ret = 0;
792
793 ret = vpif_check_format(ch, pixfmt);
794 if (ret) {
795 *pixfmt = common->fmt.fmt.pix;
796 pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
797 }
798
799 return ret;
800 }
801
802 static int vpif_reqbufs(struct file *file, void *priv,
803 struct v4l2_requestbuffers *reqbuf)
804 {
805 struct vpif_fh *fh = priv;
806 struct channel_obj *ch = fh->channel;
807 struct common_obj *common;
808 enum v4l2_field field;
809 u8 index = 0;
810 int ret = 0;
811
812 /* This file handle has not initialized the channel,
813 It is not allowed to do settings */
814 if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
815 || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
816 if (!fh->initialized) {
817 vpif_err("Channel Busy\n");
818 return -EBUSY;
819 }
820 }
821
822 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
823 return -EINVAL;
824
825 index = VPIF_VIDEO_INDEX;
826
827 common = &ch->common[index];
828 if (mutex_lock_interruptible(&common->lock))
829 return -ERESTARTSYS;
830
831 if (common->fmt.type != reqbuf->type) {
832 ret = -EINVAL;
833 goto reqbuf_exit;
834 }
835
836 if (0 != common->io_usrs) {
837 ret = -EBUSY;
838 goto reqbuf_exit;
839 }
840
841 if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
842 if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
843 field = V4L2_FIELD_INTERLACED;
844 else
845 field = common->fmt.fmt.pix.field;
846 } else {
847 field = V4L2_VBI_INTERLACED;
848 }
849
850 /* Initialize videobuf queue as per the buffer type */
851 videobuf_queue_dma_contig_init(&common->buffer_queue,
852 &video_qops, NULL,
853 &common->irqlock,
854 reqbuf->type, field,
855 sizeof(struct videobuf_buffer), fh);
856
857 /* Set io allowed member of file handle to TRUE */
858 fh->io_allowed[index] = 1;
859 /* Increment io usrs member of channel object to 1 */
860 common->io_usrs = 1;
861 /* Store type of memory requested in channel object */
862 common->memory = reqbuf->memory;
863 INIT_LIST_HEAD(&common->dma_queue);
864
865 /* Allocate buffers */
866 ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
867
868 reqbuf_exit:
869 mutex_unlock(&common->lock);
870 return ret;
871 }
872
873 static int vpif_querybuf(struct file *file, void *priv,
874 struct v4l2_buffer *tbuf)
875 {
876 struct vpif_fh *fh = priv;
877 struct channel_obj *ch = fh->channel;
878 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
879
880 if (common->fmt.type != tbuf->type)
881 return -EINVAL;
882
883 return videobuf_querybuf(&common->buffer_queue, tbuf);
884 }
885
886 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
887 {
888
889 struct vpif_fh *fh = priv;
890 struct channel_obj *ch = fh->channel;
891 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
892 struct v4l2_buffer tbuf = *buf;
893 struct videobuf_buffer *buf1;
894 unsigned long addr = 0;
895 unsigned long flags;
896 int ret = 0;
897
898 if (common->fmt.type != tbuf.type)
899 return -EINVAL;
900
901 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
902 vpif_err("fh->io_allowed\n");
903 return -EACCES;
904 }
905
906 if (!(list_empty(&common->dma_queue)) ||
907 (common->cur_frm != common->next_frm) ||
908 !(common->started) ||
909 (common->started && (0 == ch->field_id)))
910 return videobuf_qbuf(&common->buffer_queue, buf);
911
912 /* bufferqueue is empty store buffer address in VPIF registers */
913 mutex_lock(&common->buffer_queue.vb_lock);
914 buf1 = common->buffer_queue.bufs[tbuf.index];
915 if (buf1->memory != tbuf.memory) {
916 vpif_err("invalid buffer type\n");
917 goto qbuf_exit;
918 }
919
920 if ((buf1->state == VIDEOBUF_QUEUED) ||
921 (buf1->state == VIDEOBUF_ACTIVE)) {
922 vpif_err("invalid state\n");
923 goto qbuf_exit;
924 }
925
926 switch (buf1->memory) {
927 case V4L2_MEMORY_MMAP:
928 if (buf1->baddr == 0)
929 goto qbuf_exit;
930 break;
931
932 case V4L2_MEMORY_USERPTR:
933 if (tbuf.length < buf1->bsize)
934 goto qbuf_exit;
935
936 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
937 && (buf1->baddr != tbuf.m.userptr))
938 vpif_buffer_release(&common->buffer_queue, buf1);
939 buf1->baddr = tbuf.m.userptr;
940 break;
941
942 default:
943 goto qbuf_exit;
944 }
945
946 local_irq_save(flags);
947 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
948 common->buffer_queue.field);
949 if (ret < 0) {
950 local_irq_restore(flags);
951 goto qbuf_exit;
952 }
953
954 buf1->state = VIDEOBUF_ACTIVE;
955 addr = buf1->boff;
956 common->next_frm = buf1;
957 if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
958 common->set_addr((addr + common->ytop_off),
959 (addr + common->ybtm_off),
960 (addr + common->ctop_off),
961 (addr + common->cbtm_off));
962 }
963
964 local_irq_restore(flags);
965 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
966 mutex_unlock(&common->buffer_queue.vb_lock);
967 return 0;
968
969 qbuf_exit:
970 mutex_unlock(&common->buffer_queue.vb_lock);
971 return -EINVAL;
972 }
973
974 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
975 {
976 struct vpif_fh *fh = priv;
977 struct channel_obj *ch = fh->channel;
978 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
979 int ret = 0;
980
981 if (!(*std_id & DM646X_V4L2_STD))
982 return -EINVAL;
983
984 if (common->started) {
985 vpif_err("streaming in progress\n");
986 return -EBUSY;
987 }
988
989 /* Call encoder subdevice function to set the standard */
990 if (mutex_lock_interruptible(&common->lock))
991 return -ERESTARTSYS;
992
993 ch->video.stdid = *std_id;
994 /* Get the information about the standard */
995 if (vpif_get_std_info(ch)) {
996 vpif_err("Error getting the standard info\n");
997 return -EINVAL;
998 }
999
1000 if ((ch->vpifparams.std_info.width *
1001 ch->vpifparams.std_info.height * 2) >
1002 config_params.channel_bufsize[ch->channel_id]) {
1003 vpif_err("invalid std for this size\n");
1004 ret = -EINVAL;
1005 goto s_std_exit;
1006 }
1007
1008 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1009 /* Configure the default format information */
1010 vpif_config_format(ch);
1011
1012 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1013 s_std_output, *std_id);
1014 if (ret < 0) {
1015 vpif_err("Failed to set output standard\n");
1016 goto s_std_exit;
1017 }
1018
1019 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1020 s_std, *std_id);
1021 if (ret < 0)
1022 vpif_err("Failed to set standard for sub devices\n");
1023
1024 s_std_exit:
1025 mutex_unlock(&common->lock);
1026 return ret;
1027 }
1028
1029 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1030 {
1031 struct vpif_fh *fh = priv;
1032 struct channel_obj *ch = fh->channel;
1033
1034 *std = ch->video.stdid;
1035 return 0;
1036 }
1037
1038 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1039 {
1040 struct vpif_fh *fh = priv;
1041 struct channel_obj *ch = fh->channel;
1042 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1043
1044 return videobuf_dqbuf(&common->buffer_queue, p,
1045 (file->f_flags & O_NONBLOCK));
1046 }
1047
1048 static int vpif_streamon(struct file *file, void *priv,
1049 enum v4l2_buf_type buftype)
1050 {
1051 struct vpif_fh *fh = priv;
1052 struct channel_obj *ch = fh->channel;
1053 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1054 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1055 struct vpif_params *vpif = &ch->vpifparams;
1056 struct vpif_config *vpif_config_data =
1057 vpif_dev->platform_data;
1058 unsigned long addr = 0;
1059 int ret = 0;
1060
1061 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1062 vpif_err("buffer type not supported\n");
1063 return -EINVAL;
1064 }
1065
1066 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1067 vpif_err("fh->io_allowed\n");
1068 return -EACCES;
1069 }
1070
1071 /* If Streaming is already started, return error */
1072 if (common->started) {
1073 vpif_err("channel->started\n");
1074 return -EBUSY;
1075 }
1076
1077 if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1078 && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1079 ch->vpifparams.std_info.ycmux_mode == 0)
1080 || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1081 && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1082 vpif_err("other channel is using\n");
1083 return -EBUSY;
1084 }
1085
1086 ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1087 if (ret < 0)
1088 return ret;
1089
1090 /* Call videobuf_streamon to start streaming in videobuf */
1091 ret = videobuf_streamon(&common->buffer_queue);
1092 if (ret < 0) {
1093 vpif_err("videobuf_streamon\n");
1094 return ret;
1095 }
1096
1097 if (mutex_lock_interruptible(&common->lock))
1098 return -ERESTARTSYS;
1099
1100 /* If buffer queue is empty, return error */
1101 if (list_empty(&common->dma_queue)) {
1102 vpif_err("buffer queue is empty\n");
1103 ret = -EIO;
1104 goto streamon_exit;
1105 }
1106
1107 /* Get the next frame from the buffer queue */
1108 common->next_frm = common->cur_frm =
1109 list_entry(common->dma_queue.next,
1110 struct videobuf_buffer, queue);
1111
1112 list_del(&common->cur_frm->queue);
1113 /* Mark state of the current frame to active */
1114 common->cur_frm->state = VIDEOBUF_ACTIVE;
1115
1116 /* Initialize field_id and started member */
1117 ch->field_id = 0;
1118 common->started = 1;
1119 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1120 addr = common->cur_frm->boff;
1121 /* Calculate the offset for Y and C data in the buffer */
1122 vpif_calculate_offsets(ch);
1123
1124 if ((ch->vpifparams.std_info.frm_fmt &&
1125 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1126 && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1127 || (!ch->vpifparams.std_info.frm_fmt
1128 && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1129 vpif_err("conflict in field format and std format\n");
1130 ret = -EINVAL;
1131 goto streamon_exit;
1132 }
1133
1134 /* clock settings */
1135 ret =
1136 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1137 ch->vpifparams.std_info.hd_sd);
1138 if (ret < 0) {
1139 vpif_err("can't set clock\n");
1140 goto streamon_exit;
1141 }
1142
1143 /* set the parameters and addresses */
1144 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1145 if (ret < 0)
1146 goto streamon_exit;
1147
1148 common->started = ret;
1149 vpif_config_addr(ch, ret);
1150 common->set_addr((addr + common->ytop_off),
1151 (addr + common->ybtm_off),
1152 (addr + common->ctop_off),
1153 (addr + common->cbtm_off));
1154
1155 /* Set interrupt for both the fields in VPIF
1156 Register enable channel in VPIF register */
1157 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1158 channel2_intr_assert();
1159 channel2_intr_enable(1);
1160 enable_channel2(1);
1161 }
1162
1163 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1164 || (common->started == 2)) {
1165 channel3_intr_assert();
1166 channel3_intr_enable(1);
1167 enable_channel3(1);
1168 }
1169 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1170 }
1171
1172 streamon_exit:
1173 mutex_unlock(&common->lock);
1174 return ret;
1175 }
1176
1177 static int vpif_streamoff(struct file *file, void *priv,
1178 enum v4l2_buf_type buftype)
1179 {
1180 struct vpif_fh *fh = priv;
1181 struct channel_obj *ch = fh->channel;
1182 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1183
1184 if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1185 vpif_err("buffer type not supported\n");
1186 return -EINVAL;
1187 }
1188
1189 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1190 vpif_err("fh->io_allowed\n");
1191 return -EACCES;
1192 }
1193
1194 if (!common->started) {
1195 vpif_err("channel->started\n");
1196 return -EINVAL;
1197 }
1198
1199 if (mutex_lock_interruptible(&common->lock))
1200 return -ERESTARTSYS;
1201
1202 if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1203 /* disable channel */
1204 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1205 enable_channel2(0);
1206 channel2_intr_enable(0);
1207 }
1208 if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1209 (2 == common->started)) {
1210 enable_channel3(0);
1211 channel3_intr_enable(0);
1212 }
1213 }
1214
1215 common->started = 0;
1216 mutex_unlock(&common->lock);
1217
1218 return videobuf_streamoff(&common->buffer_queue);
1219 }
1220
1221 static int vpif_cropcap(struct file *file, void *priv,
1222 struct v4l2_cropcap *crop)
1223 {
1224 struct vpif_fh *fh = priv;
1225 struct channel_obj *ch = fh->channel;
1226 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1227 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1228 return -EINVAL;
1229
1230 crop->bounds.left = crop->bounds.top = 0;
1231 crop->defrect.left = crop->defrect.top = 0;
1232 crop->defrect.height = crop->bounds.height = common->height;
1233 crop->defrect.width = crop->bounds.width = common->width;
1234
1235 return 0;
1236 }
1237
1238 static int vpif_enum_output(struct file *file, void *fh,
1239 struct v4l2_output *output)
1240 {
1241
1242 struct vpif_config *config = vpif_dev->platform_data;
1243
1244 if (output->index >= config->output_count) {
1245 vpif_dbg(1, debug, "Invalid output index\n");
1246 return -EINVAL;
1247 }
1248
1249 strcpy(output->name, config->output[output->index]);
1250 output->type = V4L2_OUTPUT_TYPE_ANALOG;
1251 output->std = DM646X_V4L2_STD;
1252
1253 return 0;
1254 }
1255
1256 static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1257 {
1258 struct vpif_fh *fh = priv;
1259 struct channel_obj *ch = fh->channel;
1260 struct video_obj *vid_ch = &ch->video;
1261 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1262 int ret = 0;
1263
1264 if (mutex_lock_interruptible(&common->lock))
1265 return -ERESTARTSYS;
1266
1267 if (common->started) {
1268 vpif_err("Streaming in progress\n");
1269 ret = -EBUSY;
1270 goto s_output_exit;
1271 }
1272
1273 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1274 s_routing, 0, i, 0);
1275
1276 if (ret < 0)
1277 vpif_err("Failed to set output standard\n");
1278
1279 vid_ch->output_id = i;
1280
1281 s_output_exit:
1282 mutex_unlock(&common->lock);
1283 return ret;
1284 }
1285
1286 static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1287 {
1288 struct vpif_fh *fh = priv;
1289 struct channel_obj *ch = fh->channel;
1290 struct video_obj *vid_ch = &ch->video;
1291
1292 *i = vid_ch->output_id;
1293
1294 return 0;
1295 }
1296
1297 static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1298 {
1299 struct vpif_fh *fh = priv;
1300 struct channel_obj *ch = fh->channel;
1301
1302 *p = v4l2_prio_max(&ch->prio);
1303
1304 return 0;
1305 }
1306
1307 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1308 {
1309 struct vpif_fh *fh = priv;
1310 struct channel_obj *ch = fh->channel;
1311
1312 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1313 }
1314
1315 /* vpif display ioctl operations */
1316 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1317 .vidioc_querycap = vpif_querycap,
1318 .vidioc_g_priority = vpif_g_priority,
1319 .vidioc_s_priority = vpif_s_priority,
1320 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
1321 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1322 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1323 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
1324 .vidioc_reqbufs = vpif_reqbufs,
1325 .vidioc_querybuf = vpif_querybuf,
1326 .vidioc_qbuf = vpif_qbuf,
1327 .vidioc_dqbuf = vpif_dqbuf,
1328 .vidioc_streamon = vpif_streamon,
1329 .vidioc_streamoff = vpif_streamoff,
1330 .vidioc_s_std = vpif_s_std,
1331 .vidioc_g_std = vpif_g_std,
1332 .vidioc_enum_output = vpif_enum_output,
1333 .vidioc_s_output = vpif_s_output,
1334 .vidioc_g_output = vpif_g_output,
1335 .vidioc_cropcap = vpif_cropcap,
1336 };
1337
1338 static const struct v4l2_file_operations vpif_fops = {
1339 .owner = THIS_MODULE,
1340 .open = vpif_open,
1341 .release = vpif_release,
1342 .ioctl = video_ioctl2,
1343 .mmap = vpif_mmap,
1344 .poll = vpif_poll
1345 };
1346
1347 static struct video_device vpif_video_template = {
1348 .name = "vpif",
1349 .fops = &vpif_fops,
1350 .minor = -1,
1351 .ioctl_ops = &vpif_ioctl_ops,
1352 .tvnorms = DM646X_V4L2_STD,
1353 .current_norm = V4L2_STD_625_50,
1354
1355 };
1356
1357 /*Configure the channels, buffer sizei, request irq */
1358 static int initialize_vpif(void)
1359 {
1360 int free_channel_objects_index;
1361 int free_buffer_channel_index;
1362 int free_buffer_index;
1363 int err = 0, i, j;
1364
1365 /* Default number of buffers should be 3 */
1366 if ((ch2_numbuffers > 0) &&
1367 (ch2_numbuffers < config_params.min_numbuffers))
1368 ch2_numbuffers = config_params.min_numbuffers;
1369 if ((ch3_numbuffers > 0) &&
1370 (ch3_numbuffers < config_params.min_numbuffers))
1371 ch3_numbuffers = config_params.min_numbuffers;
1372
1373 /* Set buffer size to min buffers size if invalid buffer size is
1374 * given */
1375 if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1376 ch2_bufsize =
1377 config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1378 if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1379 ch3_bufsize =
1380 config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1381
1382 config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1383
1384 if (ch2_numbuffers) {
1385 config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1386 ch2_bufsize;
1387 }
1388 config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1389
1390 if (ch3_numbuffers) {
1391 config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1392 ch3_bufsize;
1393 }
1394
1395 /* Allocate memory for six channel objects */
1396 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1397 vpif_obj.dev[i] =
1398 kmalloc(sizeof(struct channel_obj), GFP_KERNEL);
1399 /* If memory allocation fails, return error */
1400 if (!vpif_obj.dev[i]) {
1401 free_channel_objects_index = i;
1402 err = -ENOMEM;
1403 goto vpif_init_free_channel_objects;
1404 }
1405 }
1406
1407 free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1408 free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1409 free_buffer_index = config_params.numbuffers[i - 1];
1410
1411 return 0;
1412
1413 vpif_init_free_channel_objects:
1414 for (j = 0; j < free_channel_objects_index; j++)
1415 kfree(vpif_obj.dev[j]);
1416 return err;
1417 }
1418
1419 /*
1420 * vpif_probe: This function creates device entries by register itself to the
1421 * V4L2 driver and initializes fields of each channel objects
1422 */
1423 static __init int vpif_probe(struct platform_device *pdev)
1424 {
1425 const struct vpif_subdev_info *subdevdata;
1426 int i, j = 0, k, q, m, err = 0;
1427 struct i2c_adapter *i2c_adap;
1428 struct vpif_config *config;
1429 struct common_obj *common;
1430 struct channel_obj *ch;
1431 struct video_device *vfd;
1432 struct resource *res;
1433 int subdev_count;
1434
1435 vpif_dev = &pdev->dev;
1436 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1437 if (!res) {
1438 v4l2_err(vpif_dev->driver,
1439 "Error getting platform resource\n");
1440 return -ENOENT;
1441 }
1442
1443 if (!request_mem_region(res->start, res->end - res->start + 1,
1444 vpif_dev->driver->name)) {
1445 v4l2_err(vpif_dev->driver, "VPIF: failed request_mem_region\n");
1446 return -ENXIO;
1447 }
1448
1449 vpif_base = ioremap_nocache(res->start, res->end - res->start + 1);
1450 if (!vpif_base) {
1451 v4l2_err(vpif_dev->driver, "Unable to ioremap VPIF reg\n");
1452 err = -ENXIO;
1453 goto resource_exit;
1454 }
1455
1456 vpif_base_addr_init(vpif_base);
1457
1458 initialize_vpif();
1459
1460 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1461 if (err) {
1462 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1463 return err;
1464 }
1465
1466 k = 0;
1467 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1468 for (i = res->start; i <= res->end; i++) {
1469 if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1470 "DM646x_Display",
1471 (void *)(&vpif_obj.dev[k]->channel_id))) {
1472 err = -EBUSY;
1473 goto vpif_int_err;
1474 }
1475 }
1476 k++;
1477 }
1478
1479 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1480
1481 /* Get the pointer to the channel object */
1482 ch = vpif_obj.dev[i];
1483
1484 /* Allocate memory for video device */
1485 vfd = video_device_alloc();
1486 if (vfd == NULL) {
1487 for (j = 0; j < i; j++) {
1488 ch = vpif_obj.dev[j];
1489 video_device_release(ch->video_dev);
1490 }
1491 err = -ENOMEM;
1492 goto video_dev_alloc_exit;
1493 }
1494
1495 /* Initialize field of video device */
1496 *vfd = vpif_video_template;
1497 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1498 vfd->release = video_device_release;
1499 snprintf(vfd->name, sizeof(vfd->name),
1500 "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
1501 (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
1502 (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
1503 (VPIF_DISPLAY_VERSION_CODE) & 0xff);
1504
1505 /* Set video_dev to the video device */
1506 ch->video_dev = vfd;
1507 }
1508
1509 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1510 ch = vpif_obj.dev[j];
1511 /* Initialize field of the channel objects */
1512 atomic_set(&ch->usrs, 0);
1513 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1514 ch->common[k].numbuffers = 0;
1515 common = &ch->common[k];
1516 common->io_usrs = 0;
1517 common->started = 0;
1518 spin_lock_init(&common->irqlock);
1519 mutex_init(&common->lock);
1520 common->numbuffers = 0;
1521 common->set_addr = NULL;
1522 common->ytop_off = common->ybtm_off = 0;
1523 common->ctop_off = common->cbtm_off = 0;
1524 common->cur_frm = common->next_frm = NULL;
1525 memset(&common->fmt, 0, sizeof(common->fmt));
1526 common->numbuffers = config_params.numbuffers[k];
1527
1528 }
1529 ch->initialized = 0;
1530 ch->channel_id = j;
1531 if (j < 2)
1532 ch->common[VPIF_VIDEO_INDEX].numbuffers =
1533 config_params.numbuffers[ch->channel_id];
1534 else
1535 ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1536
1537 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1538
1539 /* Initialize prio member of channel object */
1540 v4l2_prio_init(&ch->prio);
1541 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1542 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1543
1544 /* register video device */
1545 vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1546 (int)ch, (int)&ch->video_dev);
1547
1548 err = video_register_device(ch->video_dev,
1549 VFL_TYPE_GRABBER, (j ? 3 : 2));
1550 if (err < 0)
1551 goto probe_out;
1552
1553 video_set_drvdata(ch->video_dev, ch);
1554 }
1555
1556 i2c_adap = i2c_get_adapter(1);
1557 config = pdev->dev.platform_data;
1558 subdev_count = config->subdev_count;
1559 subdevdata = config->subdevinfo;
1560 vpif_obj.sd = kmalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1561 GFP_KERNEL);
1562 if (vpif_obj.sd == NULL) {
1563 vpif_err("unable to allocate memory for subdevice pointers\n");
1564 err = -ENOMEM;
1565 goto probe_out;
1566 }
1567
1568 for (i = 0; i < subdev_count; i++) {
1569 vpif_obj.sd[i] = v4l2_i2c_new_subdev(&vpif_obj.v4l2_dev,
1570 i2c_adap, subdevdata[i].name,
1571 subdevdata[i].name,
1572 0, I2C_ADDRS(subdevdata[i].addr));
1573 if (!vpif_obj.sd[i]) {
1574 vpif_err("Error registering v4l2 subdevice\n");
1575 goto probe_subdev_out;
1576 }
1577
1578 if (vpif_obj.sd[i])
1579 vpif_obj.sd[i]->grp_id = 1 << i;
1580 }
1581
1582 return 0;
1583
1584 probe_subdev_out:
1585 kfree(vpif_obj.sd);
1586 probe_out:
1587 for (k = 0; k < j; k++) {
1588 ch = vpif_obj.dev[k];
1589 video_unregister_device(ch->video_dev);
1590 video_device_release(ch->video_dev);
1591 ch->video_dev = NULL;
1592 }
1593 vpif_int_err:
1594 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1595 vpif_err("VPIF IRQ request failed\n");
1596 for (q = k; k >= 0; k--) {
1597 for (m = i; m >= res->start; m--)
1598 free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1599 res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1600 m = res->end;
1601 }
1602 video_dev_alloc_exit:
1603 iounmap(vpif_base);
1604 resource_exit:
1605 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1606 release_mem_region(res->start, res->end - res->start + 1);
1607
1608 return err;
1609 }
1610
1611 /*
1612 * vpif_remove: It un-register channels from V4L2 driver
1613 */
1614 static int vpif_remove(struct platform_device *device)
1615 {
1616 struct channel_obj *ch;
1617 int i;
1618
1619 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1620
1621 /* un-register device */
1622 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1623 /* Get the pointer to the channel object */
1624 ch = vpif_obj.dev[i];
1625 /* Unregister video device */
1626 video_unregister_device(ch->video_dev);
1627
1628 ch->video_dev = NULL;
1629 }
1630
1631 return 0;
1632 }
1633
1634 static struct platform_driver vpif_driver = {
1635 .driver = {
1636 .name = "vpif_display",
1637 .owner = THIS_MODULE,
1638 },
1639 .probe = vpif_probe,
1640 .remove = vpif_remove,
1641 };
1642
1643 static __init int vpif_init(void)
1644 {
1645 return platform_driver_register(&vpif_driver);
1646 }
1647
1648 /*
1649 * vpif_cleanup: This function un-registers device and driver to the kernel,
1650 * frees requested irq handler and de-allocates memory allocated for channel
1651 * objects.
1652 */
1653 static void vpif_cleanup(void)
1654 {
1655 struct platform_device *pdev;
1656 struct resource *res;
1657 int irq_num;
1658 int i = 0;
1659
1660 pdev = container_of(vpif_dev, struct platform_device, dev);
1661
1662 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1663 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1664 free_irq(irq_num,
1665 (void *)(&vpif_obj.dev[i]->channel_id));
1666 i++;
1667 }
1668
1669 iounmap(vpif_base);
1670 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1671 release_mem_region(res->start, res->end - res->start + 1);
1672 platform_driver_unregister(&vpif_driver);
1673 kfree(vpif_obj.sd);
1674 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1675 kfree(vpif_obj.dev[i]);
1676 }
1677
1678 module_init(vpif_init);
1679 module_exit(vpif_cleanup);
This page took 0.124059 seconds and 5 git commands to generate.