Full support for Ginger Console
[linux-ginger.git] / drivers / media / video / omap34xxcam.c
blob7fcc1b99cd6f937bfab62ad4954a6f3810039273
1 /*
2 * omap34xxcam.c
4 * Copyright (C) 2006--2009 Nokia Corporation
5 * Copyright (C) 2007--2009 Texas Instruments
7 * Contact: Sakari Ailus <sakari.ailus@nokia.com>
8 * Tuukka Toivonen <tuukka.o.toivonen@nokia.com>
10 * Originally based on the OMAP 2 camera driver.
12 * Written by Sakari Ailus <sakari.ailus@nokia.com>
13 * Tuukka Toivonen <tuukka.o.toivonen@nokia.com>
14 * Sergio Aguirre <saaguirre@ti.com>
15 * Mohit Jalori
16 * Sameer Venkatraman
17 * Leonides Martinez
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * version 2 as published by the Free Software Foundation.
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
31 * 02110-1301 USA
35 #include <linux/videodev2.h>
36 #include <linux/version.h>
37 #include <linux/sched.h>
38 #include <linux/vmalloc.h>
39 #include <linux/mm.h>
40 #include <linux/dma-mapping.h>
42 #include <media/v4l2-common.h>
43 #include <media/v4l2-ioctl.h>
44 #include <media/v4l2-int-device.h>
45 #include <media/tvp514x-int.h>
47 #include "omap34xxcam.h"
48 #include "isp/isp.h"
50 #define OMAP34XXCAM_VERSION KERNEL_VERSION(0, 0, 0)
52 /* global variables */
53 static struct omap34xxcam_device *omap34xxcam;
57 * Sensor handling.
61 /**
62 * omap34xxcam_slave_power_set - set slave power state
63 * @vdev: per-video device data structure
64 * @power: new power state
66 static int omap34xxcam_slave_power_set(struct omap34xxcam_videodev *vdev,
67 enum v4l2_power power,
68 int mask)
70 int rval = 0, i = 0;
72 BUG_ON(!mutex_is_locked(&vdev->mutex));
74 #ifdef OMAP34XXCAM_POWEROFF_DELAY
75 vdev->power_state_wish = -1;
76 #endif
78 for (i = 0; i <= OMAP34XXCAM_SLAVE_FLASH; i++) {
79 if (vdev->slave[i] == v4l2_int_device_dummy())
80 continue;
82 if (!(mask & (1 << i))
83 || power == vdev->power_state[i])
84 continue;
86 rval = vidioc_int_s_power(vdev->slave[i], power);
88 if (rval && power != V4L2_POWER_OFF) {
89 power = V4L2_POWER_OFF;
90 goto out;
93 vdev->power_state[i] = power;
96 return 0;
98 out:
99 for (i--; i >= 0; i--) {
100 if (vdev->slave[i] == v4l2_int_device_dummy())
101 continue;
103 if (!(mask & (1 << i)))
104 continue;
106 vidioc_int_s_power(vdev->slave[i], power);
107 vdev->power_state[i] = power;
110 return rval;
113 #ifdef OMAP34XXCAM_POWEROFF_DELAY
114 static void omap34xxcam_slave_power_work(struct work_struct *work)
116 struct omap34xxcam_videodev *vdev =
117 container_of(work, struct omap34xxcam_videodev, poweroff_work);
119 mutex_lock(&vdev->mutex);
121 if (vdev->power_state_wish != -1)
122 omap34xxcam_slave_power_set(vdev, vdev->power_state_wish,
123 vdev->power_state_mask);
125 mutex_unlock(&vdev->mutex);
128 static void omap34xxcam_slave_power_timer(unsigned long ptr)
130 struct omap34xxcam_videodev *vdev = (void *)ptr;
132 schedule_work(&vdev->poweroff_work);
136 * omap34xxcam_slave_power_suggest - delayed power state change
138 * @vdev: per-video device data structure
139 * @power: new power state
141 static void omap34xxcam_slave_power_suggest(struct omap34xxcam_videodev *vdev,
142 enum v4l2_power power,
143 int mask)
145 BUG_ON(!mutex_is_locked(&vdev->mutex));
147 del_timer(&vdev->poweroff_timer);
149 vdev->power_state_wish = power;
150 vdev->power_state_mask = mask;
152 mod_timer(&vdev->poweroff_timer, jiffies + OMAP34XXCAM_POWEROFF_DELAY);
154 #else /* OMAP34XXCAM_POWEROFF_DELAY */
155 #define omap34xxcam_slave_power_suggest(a, b, c) do {} while (0)
156 #endif /* OMAP34XXCAM_POWEROFF_DELAY */
159 * omap34xxcam_update_vbq - Updates VBQ with completed input buffer
160 * @vb: ptr. to standard V4L2 video buffer structure
162 * Updates video buffer queue with completed buffer passed as
163 * input parameter. Also updates ISP H3A timestamp and field count
164 * statistics.
166 void omap34xxcam_vbq_complete(struct videobuf_buffer *vb, void *priv)
168 struct omap34xxcam_fh *fh = priv;
170 do_gettimeofday(&vb->ts);
171 vb->field_count = atomic_add_return(2, &fh->field_count);
173 wake_up(&vb->done);
177 * omap34xxcam_vbq_setup - Calcs size and num of buffs allowed in queue
178 * @vbq: ptr. to standard V4L2 video buffer queue structure
179 * @cnt: ptr to location to hold the count of buffers to be in the queue
180 * @size: ptr to location to hold the size of a frame
182 * Calculates the number of buffers of current image size that can be
183 * supported by the available capture memory.
185 static int omap34xxcam_vbq_setup(struct videobuf_queue *vbq, unsigned int *cnt,
186 unsigned int *size)
188 struct omap34xxcam_fh *fh = vbq->priv_data;
189 struct omap34xxcam_videodev *vdev = fh->vdev;
191 if (*cnt <= 0)
192 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
194 if (*cnt > VIDEO_MAX_FRAME)
195 *cnt = VIDEO_MAX_FRAME;
197 *size = vdev->pix.sizeimage;
199 while (*size * *cnt > fh->vdev->vdev_sensor_config.capture_mem)
200 (*cnt)--;
202 return isp_vbq_setup(vdev->cam->isp, vbq, cnt, size);
206 * omap34xxcam_vbq_release - Free resources for input VBQ and VB
207 * @vbq: ptr. to standard V4L2 video buffer queue structure
208 * @vb: ptr to standard V4L2 video buffer structure
210 * Unmap and free all memory associated with input VBQ and VB, also
211 * unmap the address in ISP MMU. Reset the VB state.
213 static void omap34xxcam_vbq_release(struct videobuf_queue *vbq,
214 struct videobuf_buffer *vb)
216 struct omap34xxcam_fh *fh = vbq->priv_data;
217 struct omap34xxcam_videodev *vdev = fh->vdev;
218 struct device *isp = vdev->cam->isp;
220 if (!vbq->streaming) {
221 isp_vbq_release(isp, vbq, vb);
222 videobuf_dma_unmap(vbq, videobuf_to_dma(vb));
223 videobuf_dma_free(videobuf_to_dma(vb));
224 vb->state = VIDEOBUF_NEEDS_INIT;
226 return;
232 * This function is work around for the videobuf_iolock API,
233 * for User memory allocated with ioremap (VM_IO flag) the API
234 * get_user_pages fails.
236 * To fulfill this requirement, we have completely ignored VM layer of
237 * Linux, and configuring the ISP MMU with physical address.
239 static int omap_videobuf_dma_init_user(struct videobuf_buffer *vb,
240 unsigned long physp, unsigned long asize)
242 struct videobuf_dmabuf *dma;
243 struct scatterlist *sglist;
244 unsigned long data, first, last;
245 int i = 0;
247 dma = videobuf_to_dma(vb);
248 data = vb->baddr;
250 first = (data & PAGE_MASK) >> PAGE_SHIFT;
251 last = ((data+asize-1) & PAGE_MASK) >> PAGE_SHIFT;
252 dma->offset = data & ~PAGE_MASK;
253 dma->nr_pages = last-first+1;
255 dma->direction = DMA_FROM_DEVICE;
257 BUG_ON(0 == dma->nr_pages);
259 * Allocate array of sglen + 1, to add entry of extra page
260 * for input buffer. Driver always uses 0th buffer as input buffer.
262 sglist = vmalloc(dma->nr_pages * sizeof(*sglist));
263 if (NULL == sglist)
264 return -ENOMEM;
266 sg_init_table(sglist, dma->nr_pages);
268 sglist[0].offset = 0;
269 sglist[0].length = PAGE_SIZE - dma->offset;
270 sglist[0].dma_address = (dma_addr_t)physp;
271 physp += sglist[0].length;
273 * Iterate in a loop for the number of pages
275 for (i = 1; i < dma->nr_pages; i++) {
276 sglist[i].offset = 0;
277 sglist[i].length = PAGE_SIZE;
278 sglist[i].dma_address = (dma_addr_t)physp;
279 physp += PAGE_SIZE;
281 dma->sglist = sglist;
282 dma->sglen = dma->nr_pages;
284 return 0;
289 * omap34xxcam_vbq_prepare - V4L2 video ops buf_prepare handler
290 * @vbq: ptr. to standard V4L2 video buffer queue structure
291 * @vb: ptr to standard V4L2 video buffer structure
292 * @field: standard V4L2 field enum
294 * Verifies there is sufficient locked memory for the requested
295 * buffer, or if there is not, allocates, locks and initializes
296 * it.
298 static int omap34xxcam_vbq_prepare(struct videobuf_queue *vbq,
299 struct videobuf_buffer *vb,
300 enum v4l2_field field)
302 struct omap34xxcam_fh *fh = vbq->priv_data;
303 struct omap34xxcam_videodev *vdev = fh->vdev;
304 struct device *isp = vdev->cam->isp;
306 int err = 0;
309 * Accessing pix here is okay since it's constant while
310 * streaming is on (and we only get called then).
312 if (vb->baddr) {
313 /* This is a userspace buffer. */
314 if (vdev->pix.sizeimage > vb->bsize ||
315 vb->baddr != (vb->baddr & ~0x1F))
316 /* The buffer isn't big enough. */
317 return -EINVAL;
318 } else {
319 if (vb->state != VIDEOBUF_NEEDS_INIT
320 && vdev->pix.sizeimage > vb->bsize)
322 * We have a kernel bounce buffer that has
323 * already been allocated.
325 omap34xxcam_vbq_release(vbq, vb);
328 vb->size = vdev->pix.bytesperline * vdev->pix.height;
329 vb->width = vdev->pix.width;
330 vb->height = vdev->pix.height;
331 vb->field = field;
333 if (vb->state == VIDEOBUF_NEEDS_INIT) {
334 struct videobuf_dmabuf *dma;
335 struct vm_area_struct *vma;
336 dma = videobuf_to_dma(vb);
337 vma = find_vma(current->mm, vb->baddr);
338 if ((vma) && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
339 /* This will catch ioremaped buffers to the kernel.
340 * It gives two possible scenarios -
341 * - Driver allocates buffer using either
342 * dma_alloc_coherent or get_free_pages,
343 * and maps to user space using
344 * io_remap_pfn_range/remap_pfn_range
345 * - Drivers maps memory outside from Linux using
346 * io_remap
348 unsigned long physp = 0;
349 if ((vb->baddr + vb->bsize) > vma->vm_end) {
350 dev_err(&vdev->vfd->dev,
351 "User Buffer Allocation:" \
352 "err=%lu[%u]\n",\
353 (vma->vm_end - vb->baddr),
354 vb->bsize);
355 return -ENOMEM;
357 physp = (vma->vm_pgoff << PAGE_SHIFT) +
358 (vb->baddr - vma->vm_start);
359 err = omap_videobuf_dma_init_user(vb, physp, vb->bsize);
360 } else {
361 err = videobuf_iolock(vbq, vb, NULL);
364 if (!err) {
365 /* isp_addr will be stored locally inside isp code */
366 err = isp_vbq_prepare(isp, vbq, vb, field);
370 if (!err)
371 vb->state = VIDEOBUF_PREPARED;
372 else
373 omap34xxcam_vbq_release(vbq, vb);
375 return err;
379 * omap34xxcam_vbq_queue - V4L2 video ops buf_queue handler
380 * @vbq: ptr. to standard V4L2 video buffer queue structure
381 * @vb: ptr to standard V4L2 video buffer structure
383 * Maps the video buffer to sgdma and through the isp, sets
384 * the isp buffer done callback and sets the video buffer state
385 * to active.
387 static void omap34xxcam_vbq_queue(struct videobuf_queue *vbq,
388 struct videobuf_buffer *vb)
390 struct omap34xxcam_fh *fh = vbq->priv_data;
391 struct omap34xxcam_videodev *vdev = fh->vdev;
392 struct device *isp = vdev->cam->isp;
394 isp_buf_queue(isp, vb, omap34xxcam_vbq_complete, (void *)fh);
397 static struct videobuf_queue_ops omap34xxcam_vbq_ops = {
398 .buf_setup = omap34xxcam_vbq_setup,
399 .buf_prepare = omap34xxcam_vbq_prepare,
400 .buf_queue = omap34xxcam_vbq_queue,
401 .buf_release = omap34xxcam_vbq_release,
406 * IOCTL interface.
411 * vidioc_querycap - V4L2 query capabilities IOCTL handler
412 * @file: ptr. to system file structure
413 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
414 * @cap: ptr to standard V4L2 capability structure
416 * Fill in the V4L2 capabliity structure for the camera device
418 static int vidioc_querycap(struct file *file, void *fh,
419 struct v4l2_capability *cap)
421 struct omap34xxcam_fh *ofh = fh;
422 struct omap34xxcam_videodev *vdev = ofh->vdev;
424 strlcpy(cap->driver, CAM_SHORT_NAME, sizeof(cap->driver));
425 strlcpy(cap->card, vdev->vfd->name, sizeof(cap->card));
426 cap->version = OMAP34XXCAM_VERSION;
427 if (vdev->vdev_sensor != v4l2_int_device_dummy())
428 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
430 return 0;
434 * vidioc_enum_fmt_vid_cap - V4L2 enumerate format capabilities IOCTL handler
435 * @file: ptr. to system file structure
436 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
437 * @f: ptr to standard V4L2 format description structure
439 * Fills in enumerate format capabilities information for sensor (if SOC
440 * sensor attached) or ISP (if raw sensor attached).
442 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
443 struct v4l2_fmtdesc *f)
445 struct omap34xxcam_fh *ofh = fh;
446 struct omap34xxcam_videodev *vdev = ofh->vdev;
447 int rval;
449 if (vdev->vdev_sensor == v4l2_int_device_dummy())
450 return -EINVAL;
452 if (vdev->vdev_sensor_mode)
453 rval = isp_enum_fmt_cap(f);
454 else if (vdev->vdev_sensor_config.sensor_isp)
455 rval = vidioc_int_enum_fmt_cap(vdev->vdev_sensor, f);
456 else
457 rval = isp_enum_fmt_cap(f);
459 return rval;
463 * vidioc_g_fmt_vid_cap - V4L2 get format capabilities IOCTL handler
464 * @file: ptr. to system file structure
465 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
466 * @f: ptr to standard V4L2 format structure
468 * Fills in format capabilities for sensor (if SOC sensor attached) or ISP
469 * (if raw sensor attached).
471 static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
472 struct v4l2_format *f)
474 struct omap34xxcam_fh *ofh = fh;
475 struct omap34xxcam_videodev *vdev = ofh->vdev;
476 struct device *isp = vdev->cam->isp;
478 if (vdev->vdev_sensor == v4l2_int_device_dummy())
479 return -EINVAL;
481 mutex_lock(&vdev->mutex);
482 f->fmt.pix = vdev->pix;
483 mutex_unlock(&vdev->mutex);
485 return 0;
488 static int try_pix_parm(struct omap34xxcam_videodev *vdev,
489 struct v4l2_pix_format *best_pix_in,
490 struct v4l2_pix_format *wanted_pix_out,
491 struct v4l2_fract *best_ival)
493 int fps;
494 int fmtd_index;
495 int rval;
496 struct v4l2_pix_format best_pix_out;
497 struct device *isp = vdev->cam->isp;
499 if (best_ival->numerator == 0
500 || best_ival->denominator == 0)
501 *best_ival = vdev->vdev_sensor_config.ival_default;
503 fps = best_ival->denominator / best_ival->numerator;
505 memset(best_pix_in, 0, sizeof(*best_pix_in));
507 best_ival->denominator = 0;
508 best_pix_out.height = INT_MAX >> 1;
509 best_pix_out.width = best_pix_out.height;
511 for (fmtd_index = 0; ; fmtd_index++) {
512 int size_index;
513 struct v4l2_fmtdesc fmtd;
515 fmtd.index = fmtd_index;
516 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
517 rval = vidioc_int_enum_fmt_cap(vdev->vdev_sensor, &fmtd);
518 if (rval)
519 break;
520 dev_dbg(&vdev->vfd->dev, "trying fmt %8.8x (%d)\n",
521 fmtd.pixelformat, fmtd_index);
523 * Get supported resolutions.
525 for (size_index = 0; ; size_index++) {
526 struct v4l2_frmsizeenum frms;
527 struct v4l2_pix_format pix_tmp_in, pix_tmp_out;
528 int ival_index;
530 frms.index = size_index;
531 frms.pixel_format = fmtd.pixelformat;
533 rval = vidioc_int_enum_framesizes(vdev->vdev_sensor,
534 &frms);
535 if (rval)
536 break;
538 pix_tmp_in.pixelformat = frms.pixel_format;
539 pix_tmp_in.width = frms.discrete.width;
540 pix_tmp_in.height = frms.discrete.height;
541 pix_tmp_out = *wanted_pix_out;
542 /* Don't do upscaling. */
543 if (pix_tmp_out.width > pix_tmp_in.width)
544 pix_tmp_out.width = pix_tmp_in.width;
545 if (pix_tmp_out.height > pix_tmp_in.height)
546 pix_tmp_out.height = pix_tmp_in.height;
547 rval = isp_try_fmt_cap(isp, &pix_tmp_in, &pix_tmp_out);
548 if (rval)
549 return rval;
551 dev_dbg(&vdev->vfd->dev, "this w %d\th %d\tfmt %8.8x\t"
552 "-> w %d\th %d\t fmt %8.8x"
553 "\twanted w %d\th %d\t fmt %8.8x\n",
554 pix_tmp_in.width, pix_tmp_in.height,
555 pix_tmp_in.pixelformat,
556 pix_tmp_out.width, pix_tmp_out.height,
557 pix_tmp_out.pixelformat,
558 wanted_pix_out->width, wanted_pix_out->height,
559 wanted_pix_out->pixelformat);
561 #define IS_SMALLER_OR_EQUAL(pix1, pix2) \
562 ((pix1)->width + (pix1)->height \
563 < (pix2)->width + (pix2)->height)
564 #define SIZE_DIFF(pix1, pix2) \
565 (abs((pix1)->width - (pix2)->width) \
566 + abs((pix1)->height - (pix2)->height))
569 * Don't use modes that are farther from wanted size
570 * that what we already got.
572 if (SIZE_DIFF(&pix_tmp_out, wanted_pix_out)
573 > SIZE_DIFF(&best_pix_out, wanted_pix_out)) {
574 dev_dbg(&vdev->vfd->dev, "size diff bigger: "
575 "w %d\th %d\tw %d\th %d\n",
576 pix_tmp_out.width, pix_tmp_out.height,
577 best_pix_out.width,
578 best_pix_out.height);
579 continue;
583 * There's an input mode that can provide output
584 * closer to wanted.
586 if (SIZE_DIFF(&pix_tmp_out, wanted_pix_out)
587 < SIZE_DIFF(&best_pix_out, wanted_pix_out)) {
588 /* Force renegotation of fps etc. */
589 best_ival->denominator = 0;
590 dev_dbg(&vdev->vfd->dev, "renegotiate: "
591 "w %d\th %d\tw %d\th %d\n",
592 pix_tmp_out.width, pix_tmp_out.height,
593 best_pix_out.width,
594 best_pix_out.height);
597 for (ival_index = 0; ; ival_index++) {
598 struct v4l2_frmivalenum frmi;
600 frmi.index = ival_index;
601 frmi.pixel_format = frms.pixel_format;
602 frmi.width = frms.discrete.width;
603 frmi.height = frms.discrete.height;
604 /* FIXME: try to fix standard... */
605 frmi.reserved[0] = 0xdeafbeef;
607 rval = vidioc_int_enum_frameintervals(
608 vdev->vdev_sensor, &frmi);
609 if (rval)
610 break;
612 dev_dbg(&vdev->vfd->dev, "fps %d\n",
613 frmi.discrete.denominator
614 / frmi.discrete.numerator);
616 if (best_ival->denominator == 0)
617 goto do_it_now;
619 if (best_pix_in->width == 0)
620 goto do_it_now;
623 * We aim to use maximum resolution
624 * from the sensor, provided that the
625 * fps is at least as close as on the
626 * current mode.
628 #define FPS_ABS_DIFF(fps, ival) abs(fps - (ival).denominator / (ival).numerator)
630 /* Select mode with closest fps. */
631 if (FPS_ABS_DIFF(fps, frmi.discrete)
632 < FPS_ABS_DIFF(fps, *best_ival)) {
633 dev_dbg(&vdev->vfd->dev, "closer fps: "
634 "fps %ld\t fps %ld\n",
635 FPS_ABS_DIFF(fps,
636 frmi.discrete),
637 FPS_ABS_DIFF(fps, *best_ival));
638 goto do_it_now;
642 * Select bigger resolution if it's available
643 * at same fps.
645 if (frmi.width + frmi.height
646 > best_pix_in->width + best_pix_in->height
647 && FPS_ABS_DIFF(fps, frmi.discrete)
648 <= FPS_ABS_DIFF(fps, *best_ival)) {
649 dev_dbg(&vdev->vfd->dev, "bigger res, "
650 "same fps: "
651 "w %d\th %d\tw %d\th %d\n",
652 frmi.width, frmi.height,
653 best_pix_in->width,
654 best_pix_in->height);
655 goto do_it_now;
658 dev_dbg(&vdev->vfd->dev, "falling through\n");
660 continue;
662 do_it_now:
663 *best_ival = frmi.discrete;
664 best_pix_out = pix_tmp_out;
665 best_pix_in->width = frmi.width;
666 best_pix_in->height = frmi.height;
667 best_pix_in->pixelformat = frmi.pixel_format;
669 dev_dbg(&vdev->vfd->dev,
670 "best_pix_in: w %d\th %d\tfmt %8.8x"
671 "\tival %d/%d\n",
672 best_pix_in->width,
673 best_pix_in->height,
674 best_pix_in->pixelformat,
675 best_ival->numerator,
676 best_ival->denominator);
681 if (best_ival->denominator == 0)
682 return -EINVAL;
684 *wanted_pix_out = best_pix_out;
686 dev_dbg(&vdev->vfd->dev, "w %d, h %d, fmt %8.8x -> w %d, h %d\n",
687 best_pix_in->width, best_pix_in->height,
688 best_pix_in->pixelformat,
689 best_pix_out.width, best_pix_out.height);
691 return 0;
694 static int s_pix_parm(struct omap34xxcam_videodev *vdev,
695 struct v4l2_pix_format *best_pix,
696 struct v4l2_pix_format *pix,
697 struct v4l2_fract *best_ival)
699 struct device *isp = vdev->cam->isp;
700 struct v4l2_streamparm a;
701 struct v4l2_format fmt;
702 struct v4l2_format old_fmt;
703 int rval;
705 rval = try_pix_parm(vdev, best_pix, pix, best_ival);
706 if (rval)
707 return rval;
709 rval = isp_s_fmt_cap(isp, best_pix, pix);
710 if (rval)
711 return rval;
713 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
714 fmt.fmt.pix = *best_pix;
715 vidioc_int_g_fmt_cap(vdev->vdev_sensor, &old_fmt);
716 rval = vidioc_int_s_fmt_cap(vdev->vdev_sensor, &fmt);
717 if (rval)
718 return rval;
720 a.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
721 a.parm.capture.timeperframe = *best_ival;
722 rval = vidioc_int_s_parm(vdev->vdev_sensor, &a);
724 return rval;
728 * vidioc_s_fmt_vid_cap - V4L2 set format capabilities IOCTL handler
729 * @file: ptr. to system file structure
730 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
731 * @f: ptr to standard V4L2 format structure
733 * Attempts to set input format with the sensor driver (first) and then the
734 * ISP. Returns the return code from vidioc_g_fmt_vid_cap().
736 static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
737 struct v4l2_format *f)
739 struct omap34xxcam_fh *ofh = fh;
740 struct omap34xxcam_videodev *vdev = ofh->vdev;
741 struct v4l2_pix_format pix_tmp;
742 struct v4l2_fract timeperframe;
743 int rval;
745 if (vdev->vdev_sensor == v4l2_int_device_dummy())
746 return -EINVAL;
748 mutex_lock(&vdev->mutex);
749 if (vdev->streaming) {
750 rval = -EBUSY;
751 goto out;
754 if (vdev->vdev_sensor_mode) {
755 struct v4l2_format input_fmt = *f;
756 struct v4l2_pix_format *pix = &f->fmt.pix;
757 struct device *isp = vdev->cam->isp;
759 rval = isp_try_fmt_cap(isp, pix, pix);
760 if (rval)
761 goto out;
762 /* Always negotiate with the sensor first */
763 rval = vidioc_int_s_fmt_cap(vdev->vdev_sensor, &input_fmt);
764 if (rval)
765 goto out;
766 pix->width = input_fmt.fmt.pix.width;
767 pix->height = input_fmt.fmt.pix.height;
768 pix->pixelformat = input_fmt.fmt.pix.pixelformat;
769 pix->field = input_fmt.fmt.pix.field;
770 pix->bytesperline = input_fmt.fmt.pix.bytesperline;
771 pix->colorspace = input_fmt.fmt.pix.colorspace;
772 pix->sizeimage = input_fmt.fmt.pix.sizeimage;
773 /* Negotiate with OMAP3 ISP */
774 rval = isp_s_fmt_cap(isp, pix, pix);
775 if (!rval)
776 vdev->pix = f->fmt.pix;
777 } else {
778 vdev->want_pix = f->fmt.pix;
780 timeperframe = vdev->want_timeperframe;
782 rval = s_pix_parm(vdev, &pix_tmp, &f->fmt.pix, &timeperframe);
783 if (!rval)
784 vdev->pix = f->fmt.pix;
786 out:
787 mutex_unlock(&vdev->mutex);
789 return rval;
793 * vidioc_try_fmt_vid_cap - V4L2 try format capabilities IOCTL handler
794 * @file: ptr. to system file structure
795 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
796 * @f: ptr to standard V4L2 format structure
798 * Checks if the given format is supported by the sensor driver and
799 * by the ISP.
801 static int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
802 struct v4l2_format *f)
804 struct omap34xxcam_fh *ofh = fh;
805 struct omap34xxcam_videodev *vdev = ofh->vdev;
806 struct v4l2_pix_format pix_tmp;
807 struct v4l2_fract timeperframe;
808 int rval;
810 if (vdev->vdev_sensor == v4l2_int_device_dummy())
811 return -EINVAL;
813 mutex_lock(&vdev->mutex);
815 timeperframe = vdev->want_timeperframe;
817 rval = try_pix_parm(vdev, &pix_tmp, &f->fmt.pix, &timeperframe);
819 mutex_unlock(&vdev->mutex);
821 return rval;
825 * vidioc_reqbufs - V4L2 request buffers IOCTL handler
826 * @file: ptr. to system file structure
827 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
828 * @b: ptr to standard V4L2 request buffers structure
830 * Attempts to get a buffer from the buffer queue associated with the
831 * fh through the video buffer library API.
833 static int vidioc_reqbufs(struct file *file, void *fh,
834 struct v4l2_requestbuffers *b)
836 struct omap34xxcam_fh *ofh = fh;
837 struct omap34xxcam_videodev *vdev = ofh->vdev;
838 int rval;
840 if (vdev->vdev_sensor == v4l2_int_device_dummy())
841 return -EINVAL;
843 mutex_lock(&vdev->mutex);
844 if (vdev->streaming) {
845 mutex_unlock(&vdev->mutex);
846 return -EBUSY;
849 rval = videobuf_reqbufs(&ofh->vbq, b);
851 mutex_unlock(&vdev->mutex);
854 * Either videobuf_reqbufs failed or the buffers are not
855 * memory-mapped (which would need special attention).
857 if (rval < 0 || b->memory != V4L2_MEMORY_MMAP)
858 goto out;
860 out:
861 return rval;
865 * vidioc_querybuf - V4L2 query buffer IOCTL handler
866 * @file: ptr. to system file structure
867 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
868 * @b: ptr to standard V4L2 buffer structure
870 * Attempts to fill in the v4l2_buffer structure for the buffer queue
871 * associated with the fh through the video buffer library API.
873 static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
875 struct omap34xxcam_fh *ofh = fh;
877 return videobuf_querybuf(&ofh->vbq, b);
881 * vidioc_qbuf - V4L2 queue buffer IOCTL handler
882 * @file: ptr. to system file structure
883 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
884 * @b: ptr to standard V4L2 buffer structure
886 * Attempts to queue the v4l2_buffer on the buffer queue
887 * associated with the fh through the video buffer library API.
889 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
891 struct omap34xxcam_fh *ofh = fh;
893 return videobuf_qbuf(&ofh->vbq, b);
897 * vidioc_dqbuf - V4L2 dequeue buffer IOCTL handler
898 * @file: ptr. to system file structure
899 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
900 * @b: ptr to standard V4L2 buffer structure
902 * Attempts to dequeue the v4l2_buffer from the buffer queue
903 * associated with the fh through the video buffer library API. If the
904 * buffer is a user space buffer, then this function will also requeue it,
905 * as user does not expect to do this.
907 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
909 struct omap34xxcam_fh *ofh = fh;
910 int rval;
912 videobuf_dqbuf_again:
913 rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
916 * This is a hack. We don't want to show -EIO to the user
917 * space. Requeue the buffer and try again if we're not doing
918 * this in non-blocking mode.
920 if (rval == -EIO) {
921 videobuf_qbuf(&ofh->vbq, b);
922 if (!(file->f_flags & O_NONBLOCK))
923 goto videobuf_dqbuf_again;
925 * We don't have a videobuf_buffer now --- maybe next
926 * time...
928 rval = -EAGAIN;
931 return rval;
935 * vidioc_streamon - V4L2 streamon IOCTL handler
936 * @file: ptr. to system file structure
937 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
938 * @i: V4L2 buffer type
940 * Attempts to start streaming by enabling the sensor interface and turning
941 * on video buffer streaming through the video buffer library API. Upon
942 * success the function returns 0, otherwise an error code is returned.
944 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
946 struct omap34xxcam_fh *ofh = fh;
947 struct omap34xxcam_videodev *vdev = ofh->vdev;
948 struct device *isp = vdev->cam->isp;
949 int rval;
951 if (vdev->vdev_sensor == v4l2_int_device_dummy())
952 return -EINVAL;
954 mutex_lock(&vdev->mutex);
955 if (vdev->streaming) {
956 rval = -EBUSY;
957 goto out;
960 rval = omap34xxcam_slave_power_set(vdev, V4L2_POWER_ON,
961 OMAP34XXCAM_SLAVE_POWER_SENSOR_LENS);
962 if (rval) {
963 dev_dbg(&vdev->vfd->dev,
964 "omap34xxcam_slave_power_set failed\n");
965 goto out;
968 isp_start(isp);
970 rval = videobuf_streamon(&ofh->vbq);
971 if (rval) {
972 isp_stop(isp);
973 omap34xxcam_slave_power_set(
974 vdev, V4L2_POWER_OFF,
975 OMAP34XXCAM_SLAVE_POWER_SENSOR_LENS);
976 } else
977 vdev->streaming = file;
979 out:
980 mutex_unlock(&vdev->mutex);
982 return rval;
986 * vidioc_streamoff - V4L2 streamoff IOCTL handler
987 * @file: ptr. to system file structure
988 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
989 * @i: V4L2 buffer type
991 * Attempts to stop streaming by flushing all scheduled work, waiting on
992 * any queued buffers to complete and then stopping the ISP and turning
993 * off video buffer streaming through the video buffer library API. Upon
994 * success the function returns 0, otherwise an error code is returned.
996 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
998 struct omap34xxcam_fh *ofh = fh;
999 struct omap34xxcam_videodev *vdev = ofh->vdev;
1000 struct device *isp = vdev->cam->isp;
1001 struct videobuf_queue *q = &ofh->vbq;
1002 int rval;
1004 mutex_lock(&vdev->mutex);
1006 if (vdev->streaming == file)
1007 isp_stop(isp);
1009 rval = videobuf_streamoff(q);
1010 if (!rval) {
1011 vdev->streaming = NULL;
1013 omap34xxcam_slave_power_set(vdev, V4L2_POWER_STANDBY,
1014 OMAP34XXCAM_SLAVE_POWER_SENSOR);
1015 omap34xxcam_slave_power_suggest(vdev, V4L2_POWER_STANDBY,
1016 OMAP34XXCAM_SLAVE_POWER_LENS);
1019 mutex_unlock(&vdev->mutex);
1021 return rval;
1025 * vidioc_enum_input - V4L2 enumerate input IOCTL handler
1026 * @file: ptr. to system file structure
1027 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1028 * @inp: V4L2 input type information structure
1030 * Fills in v4l2_input structure. Returns 0.
1032 static int vidioc_enum_input(struct file *file, void *fh,
1033 struct v4l2_input *inp)
1035 struct omap34xxcam_videodev *vdev = ((struct omap34xxcam_fh *)fh)->vdev;
1037 if (vdev->vdev_sensor_mode) {
1038 if (inp->index == 0) {
1039 strlcpy(inp->name, "COMPOSITE", sizeof(inp->name));
1040 inp->type = V4L2_INPUT_TYPE_CAMERA;
1041 } else if (inp->index == 1) {
1042 strlcpy(inp->name, "S-VIDEO", sizeof(inp->name));
1043 inp->type = V4L2_INPUT_TYPE_CAMERA;
1044 } else
1045 return -EINVAL;
1046 } else {
1047 if (inp->index > 0)
1048 return -EINVAL;
1049 strlcpy(inp->name, "camera", sizeof(inp->name));
1050 inp->type = V4L2_INPUT_TYPE_CAMERA;
1053 return 0;
1057 * vidioc_g_input - V4L2 get input IOCTL handler
1058 * @file: ptr. to system file structure
1059 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1060 * @i: address to hold index of input supported
1062 * Sets index to 0.
1064 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
1066 struct omap34xxcam_videodev *vdev = ((struct omap34xxcam_fh *)fh)->vdev;
1067 int rval = 0;
1069 mutex_lock(&vdev->mutex);
1070 if (vdev->vdev_sensor_mode) {
1071 if (vdev->slave_config[OMAP34XXCAM_SLAVE_SENSOR].cur_input
1072 == INPUT_CVBS_VI4A)
1073 *i = 0;
1074 else if (vdev->slave_config[OMAP34XXCAM_SLAVE_SENSOR].cur_input
1075 == INPUT_SVIDEO_VI2C_VI1C)
1076 *i = 1;
1077 } else {
1078 *i = 0;
1080 mutex_unlock(&vdev->mutex);
1082 return rval;
1086 * vidioc_s_input - V4L2 set input IOCTL handler
1087 * @file: ptr. to system file structure
1088 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1089 * @i: index of input selected
1091 * 0 is only index supported.
1093 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
1095 struct omap34xxcam_fh *ofh = fh;
1096 struct omap34xxcam_videodev *vdev = ofh->vdev;
1097 int rval = 0;
1098 struct v4l2_routing route;
1100 mutex_lock(&vdev->mutex);
1101 if (vdev->vdev_sensor_mode) {
1102 if (i == 0)
1103 route.input = INPUT_CVBS_VI4A;
1104 else
1105 route.input = INPUT_SVIDEO_VI2C_VI1C;
1107 route.output = 0;
1108 rval = vidioc_int_s_video_routing(vdev->vdev_sensor, &route);
1109 if (!rval)
1110 vdev->slave_config[OMAP34XXCAM_SLAVE_SENSOR].cur_input
1111 = route.input;
1112 } else {
1113 if (i > 0)
1114 rval = -EINVAL;
1116 mutex_unlock(&vdev->mutex);
1118 return rval;
1122 * vidioc_queryctrl - V4L2 query control IOCTL handler
1123 * @file: ptr. to system file structure
1124 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1125 * @a: standard V4L2 query control ioctl structure
1127 * If the requested control is supported, returns the control information
1128 * in the v4l2_queryctrl structure. Otherwise, returns -EINVAL if the
1129 * control is not supported. If the sensor being used is a "smart sensor",
1130 * this request is passed to the sensor driver, otherwise the ISP is
1131 * queried and if it does not support the requested control, the request
1132 * is forwarded to the "raw" sensor driver to see if it supports it.
1134 static int vidioc_queryctrl(struct file *file, void *fh,
1135 struct v4l2_queryctrl *a)
1137 struct omap34xxcam_fh *ofh = fh;
1138 struct omap34xxcam_videodev *vdev = ofh->vdev;
1139 struct v4l2_queryctrl a_tmp;
1140 int best_slave = -1;
1141 u32 best_ctrl = (u32)-1;
1142 int i;
1144 if (vdev->vdev_sensor_config.sensor_isp)
1145 return vidioc_int_queryctrl(vdev->vdev_sensor, a);
1147 /* No next flags: try slaves directly. */
1148 if (!(a->id & V4L2_CTRL_FLAG_NEXT_CTRL)) {
1149 for (i = 0; i <= OMAP34XXCAM_SLAVE_FLASH; i++) {
1150 if (!vidioc_int_queryctrl(vdev->slave[i], a))
1151 return 0;
1153 return isp_queryctrl(a);
1156 /* Find slave with smallest next control id. */
1157 for (i = 0; i <= OMAP34XXCAM_SLAVE_FLASH; i++) {
1158 a_tmp = *a;
1160 if (vidioc_int_queryctrl(vdev->slave[i], &a_tmp))
1161 continue;
1163 if (a_tmp.id < best_ctrl) {
1164 best_slave = i;
1165 best_ctrl = a_tmp.id;
1169 a_tmp = *a;
1170 if (!isp_queryctrl(&a_tmp)) {
1171 if (a_tmp.id < best_ctrl) {
1172 *a = a_tmp;
1174 return 0;
1178 if (best_slave == -1)
1179 return -EINVAL;
1181 a->id = best_ctrl;
1182 return vidioc_int_queryctrl(vdev->slave[best_slave], a);
1186 * vidioc_querymenu - V4L2 query menu IOCTL handler
1187 * @file: ptr. to system file structure
1188 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1189 * @a: standard V4L2 query menu ioctl structure
1191 * If the requested control is supported, returns the menu information
1192 * in the v4l2_querymenu structure. Otherwise, returns -EINVAL if the
1193 * control is not supported or is not a menu. If the sensor being used
1194 * is a "smart sensor", this request is passed to the sensor driver,
1195 * otherwise the ISP is queried and if it does not support the requested
1196 * menu control, the request is forwarded to the "raw" sensor driver to
1197 * see if it supports it.
1199 static int vidioc_querymenu(struct file *file, void *fh,
1200 struct v4l2_querymenu *a)
1202 struct omap34xxcam_fh *ofh = fh;
1203 struct omap34xxcam_videodev *vdev = ofh->vdev;
1204 int i;
1206 if (vdev->vdev_sensor_config.sensor_isp)
1207 return vidioc_int_querymenu(vdev->vdev_sensor, a);
1209 /* Try slaves directly. */
1210 for (i = 0; i <= OMAP34XXCAM_SLAVE_FLASH; i++) {
1211 if (!vidioc_int_querymenu(vdev->slave[i], a))
1212 return 0;
1214 return isp_querymenu(a);
1217 static int vidioc_g_ext_ctrls(struct file *file, void *fh,
1218 struct v4l2_ext_controls *a)
1220 struct omap34xxcam_fh *ofh = fh;
1221 struct omap34xxcam_videodev *vdev = ofh->vdev;
1222 struct device *isp = vdev->cam->isp;
1223 int i, ctrl_idx, rval = 0;
1225 mutex_lock(&vdev->mutex);
1227 for (ctrl_idx = 0; ctrl_idx < a->count; ctrl_idx++) {
1228 struct v4l2_control ctrl;
1230 ctrl.id = a->controls[ctrl_idx].id;
1232 if (vdev->vdev_sensor_config.sensor_isp) {
1233 rval = vidioc_int_g_ctrl(vdev->vdev_sensor, &ctrl);
1234 } else {
1235 for (i = 0; i <= OMAP34XXCAM_SLAVE_FLASH; i++) {
1236 rval = vidioc_int_g_ctrl(vdev->slave[i], &ctrl);
1237 if (!rval)
1238 break;
1242 if (rval)
1243 rval = isp_g_ctrl(isp, &ctrl);
1245 if (rval) {
1246 a->error_idx = ctrl_idx;
1247 break;
1250 a->controls[ctrl_idx].value = ctrl.value;
1253 mutex_unlock(&vdev->mutex);
1255 return rval;
1258 static int vidioc_s_ext_ctrls(struct file *file, void *fh,
1259 struct v4l2_ext_controls *a)
1261 struct omap34xxcam_fh *ofh = fh;
1262 struct omap34xxcam_videodev *vdev = ofh->vdev;
1263 struct device *isp = vdev->cam->isp;
1264 int i, ctrl_idx, rval = 0;
1266 mutex_lock(&vdev->mutex);
1268 for (ctrl_idx = 0; ctrl_idx < a->count; ctrl_idx++) {
1269 struct v4l2_control ctrl;
1271 ctrl.id = a->controls[ctrl_idx].id;
1272 ctrl.value = a->controls[ctrl_idx].value;
1274 if (vdev->vdev_sensor_config.sensor_isp) {
1275 rval = vidioc_int_s_ctrl(vdev->vdev_sensor, &ctrl);
1276 } else {
1277 for (i = 0; i <= OMAP34XXCAM_SLAVE_FLASH; i++) {
1278 rval = vidioc_int_s_ctrl(vdev->slave[i], &ctrl);
1279 if (!rval)
1280 break;
1284 if (rval)
1285 rval = isp_s_ctrl(isp, &ctrl);
1287 if (rval) {
1288 a->error_idx = ctrl_idx;
1289 break;
1292 a->controls[ctrl_idx].value = ctrl.value;
1295 mutex_unlock(&vdev->mutex);
1297 return rval;
1301 * vidioc_g_parm - V4L2 get parameters IOCTL handler
1302 * @file: ptr. to system file structure
1303 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1304 * @a: standard V4L2 stream parameters structure
1306 * If request is for video capture buffer type, handles request by
1307 * forwarding to sensor driver.
1309 static int vidioc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1311 struct omap34xxcam_fh *ofh = fh;
1312 struct omap34xxcam_videodev *vdev = ofh->vdev;
1313 int rval;
1315 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1316 return -EINVAL;
1318 mutex_lock(&vdev->mutex);
1319 rval = vidioc_int_g_parm(vdev->vdev_sensor, a);
1320 mutex_unlock(&vdev->mutex);
1322 return rval;
1326 * vidioc_s_parm - V4L2 set parameters IOCTL handler
1327 * @file: ptr. to system file structure
1328 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1329 * @a: standard V4L2 stream parameters structure
1331 * If request is for video capture buffer type, handles request by
1332 * first getting current stream parameters from sensor, then forwarding
1333 * request to set new parameters to sensor driver. It then attempts to
1334 * enable the sensor interface with the new parameters. If this fails, it
1335 * reverts back to the previous parameters.
1337 static int vidioc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1339 struct omap34xxcam_fh *ofh = fh;
1340 struct omap34xxcam_videodev *vdev = ofh->vdev;
1341 struct v4l2_pix_format pix_tmp_sensor, pix_tmp;
1342 int rval;
1344 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1345 return -EINVAL;
1347 if (vdev->vdev_sensor == v4l2_int_device_dummy())
1348 return -EINVAL;
1350 mutex_lock(&vdev->mutex);
1351 if (vdev->streaming) {
1352 rval = -EBUSY;
1353 goto out;
1356 vdev->want_timeperframe = a->parm.capture.timeperframe;
1358 pix_tmp = vdev->want_pix;
1360 rval = s_pix_parm(vdev, &pix_tmp_sensor, &pix_tmp,
1361 &a->parm.capture.timeperframe);
1363 out:
1364 mutex_unlock(&vdev->mutex);
1366 return rval;
1370 * vidioc_cropcap - V4L2 crop capture IOCTL handler
1371 * @file: ptr. to system file structure
1372 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1373 * @a: standard V4L2 crop capture structure
1375 * If using a "smart" sensor, just forwards request to the sensor driver,
1376 * otherwise fills in the v4l2_cropcap values locally.
1378 static int vidioc_cropcap(struct file *file, void *fh, struct v4l2_cropcap *a)
1380 struct omap34xxcam_fh *ofh = fh;
1381 struct omap34xxcam_videodev *vdev = ofh->vdev;
1382 struct v4l2_cropcap *cropcap = a;
1383 int rval;
1385 if (vdev->vdev_sensor == v4l2_int_device_dummy())
1386 return -EINVAL;
1388 mutex_lock(&vdev->mutex);
1390 rval = vidioc_int_cropcap(vdev->vdev_sensor, a);
1392 if (rval && !vdev->vdev_sensor_config.sensor_isp) {
1393 struct v4l2_format f;
1395 /* cropcap failed, try to do this via g_fmt_cap */
1396 rval = vidioc_int_g_fmt_cap(vdev->vdev_sensor, &f);
1397 if (!rval) {
1398 cropcap->bounds.top = 0;
1399 cropcap->bounds.left = 0;
1400 cropcap->bounds.width = f.fmt.pix.width;
1401 cropcap->bounds.height = f.fmt.pix.height;
1402 cropcap->defrect = cropcap->bounds;
1403 cropcap->pixelaspect.numerator = 1;
1404 cropcap->pixelaspect.denominator = 1;
1408 mutex_unlock(&vdev->mutex);
1410 return rval;
1414 * vidioc_g_crop - V4L2 get capture crop IOCTL handler
1415 * @file: ptr. to system file structure
1416 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1417 * @a: standard V4L2 crop structure
1419 * If using a "smart" sensor, just forwards request to the sensor driver,
1420 * otherwise calls the isp functions to fill in current crop values.
1422 static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
1424 struct omap34xxcam_fh *ofh = fh;
1425 struct omap34xxcam_videodev *vdev = ofh->vdev;
1426 struct device *isp = vdev->cam->isp;
1427 int rval = 0;
1429 if (vdev->vdev_sensor == v4l2_int_device_dummy())
1430 return -EINVAL;
1432 mutex_lock(&vdev->mutex);
1434 if (vdev->vdev_sensor_config.sensor_isp)
1435 rval = vidioc_int_g_crop(vdev->vdev_sensor, a);
1436 else
1437 rval = isp_g_crop(isp, a);
1439 mutex_unlock(&vdev->mutex);
1441 return rval;
1445 * vidioc_s_crop - V4L2 set capture crop IOCTL handler
1446 * @file: ptr. to system file structure
1447 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1448 * @a: standard V4L2 crop structure
1450 * If using a "smart" sensor, just forwards request to the sensor driver,
1451 * otherwise calls the isp functions to set the current crop values.
1453 static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *a)
1455 struct omap34xxcam_fh *ofh = fh;
1456 struct omap34xxcam_videodev *vdev = ofh->vdev;
1457 struct device *isp = vdev->cam->isp;
1458 int rval = 0;
1460 if (vdev->vdev_sensor == v4l2_int_device_dummy())
1461 return -EINVAL;
1463 mutex_lock(&vdev->mutex);
1465 if (vdev->vdev_sensor_config.sensor_isp)
1466 rval = vidioc_int_s_crop(vdev->vdev_sensor, a);
1467 else
1468 rval = isp_s_crop(isp, a);
1470 mutex_unlock(&vdev->mutex);
1472 return rval;
1475 static int vidioc_enum_framesizes(struct file *file, void *fh,
1476 struct v4l2_frmsizeenum *frms)
1478 struct omap34xxcam_fh *ofh = fh;
1479 struct omap34xxcam_videodev *vdev = ofh->vdev;
1480 u32 pixel_format;
1481 int rval;
1483 mutex_lock(&vdev->mutex);
1485 if (vdev->vdev_sensor_config.sensor_isp) {
1486 rval = vidioc_int_enum_framesizes(vdev->vdev_sensor, frms);
1487 } else {
1488 pixel_format = frms->pixel_format;
1489 frms->pixel_format = -1; /* ISP does format conversion */
1490 rval = vidioc_int_enum_framesizes(vdev->vdev_sensor, frms);
1491 frms->pixel_format = pixel_format;
1494 mutex_unlock(&vdev->mutex);
1495 return rval;
1498 static int vidioc_enum_frameintervals(struct file *file, void *fh,
1499 struct v4l2_frmivalenum *frmi)
1501 struct omap34xxcam_fh *ofh = fh;
1502 struct omap34xxcam_videodev *vdev = ofh->vdev;
1503 u32 pixel_format;
1504 int rval;
1506 mutex_lock(&vdev->mutex);
1508 if (vdev->vdev_sensor_config.sensor_isp) {
1509 rval = vidioc_int_enum_frameintervals(vdev->vdev_sensor, frmi);
1510 } else {
1511 pixel_format = frmi->pixel_format;
1512 frmi->pixel_format = -1; /* ISP does format conversion */
1513 rval = vidioc_int_enum_frameintervals(vdev->vdev_sensor, frmi);
1514 frmi->pixel_format = pixel_format;
1517 mutex_unlock(&vdev->mutex);
1518 return rval;
1522 * vidioc_querystd - V4L2 query current standard IOCTL handler
1523 * @file: ptr. to system file structure
1524 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1525 * @std: standard V4L2 v4l2_std_id enum
1527 * If using a "smart" sensor, just forwards request to the sensor driver,
1528 * otherwise returns error
1530 static int vidioc_querystd(struct file *file, void *fh, v4l2_std_id *std)
1532 struct omap34xxcam_fh *ofh = fh;
1533 struct omap34xxcam_videodev *vdev = ofh->vdev;
1534 int rval = 0;
1536 mutex_lock(&vdev->mutex);
1537 if (vdev->vdev_sensor_mode) {
1538 rval = vidioc_int_querystd(vdev->vdev_sensor, std);
1539 if (rval == 0)
1540 vdev->vfd->current_norm = *std;
1541 } else
1542 rval = -EINVAL;
1543 mutex_unlock(&vdev->mutex);
1545 return rval;
1549 * vidioc_s_std - V4L2 set standard IOCTL handler
1550 * @file: ptr. to system file structure
1551 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1552 * @std: standard V4L2 v4l2_std_id enum
1554 * If using a "smart" sensor, just forwards request to the sensor driver,
1555 * otherwise returns error
1557 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *std)
1559 struct omap34xxcam_fh *ofh = fh;
1560 struct omap34xxcam_videodev *vdev = ofh->vdev;
1561 int rval = 0;
1563 mutex_lock(&vdev->mutex);
1564 if (vdev->vdev_sensor_mode) {
1565 rval = vidioc_int_s_std(vdev->vdev_sensor, std);
1566 if (rval == 0)
1567 vdev->vfd->current_norm = *std;
1568 } else
1569 rval = -EINVAL;
1570 mutex_unlock(&vdev->mutex);
1572 return rval;
1576 * vidioc_default - private IOCTL handler
1577 * @file: ptr. to system file structure
1578 * @fh: ptr to hold address of omap34xxcam_fh struct (per-filehandle data)
1579 * @cmd: ioctl cmd value
1580 * @arg: ioctl arg value
1582 * If the sensor being used is a "smart sensor", this request is returned to
1583 * caller with -EINVAL err code. Otherwise if the control id is the private
1584 * VIDIOC_PRIVATE_ISP_AEWB_REQ to update the analog gain or exposure,
1585 * then this request is forwared directly to the sensor to incorporate the
1586 * feedback. The request is then passed on to the ISP private IOCTL handler,
1587 * isp_handle_private()
1589 static long vidioc_default(struct file *file, void *fh, int cmd, void *arg)
1591 struct omap34xxcam_fh *ofh = file->private_data;
1592 struct omap34xxcam_videodev *vdev = ofh->vdev;
1593 struct device *isp = vdev->cam->isp;
1594 int rval;
1596 if (cmd == VIDIOC_PRIVATE_OMAP34XXCAM_SENSOR_INFO) {
1597 u32 pixclk;
1598 struct v4l2_pix_format active_size, full_size;
1599 struct omap34xxcam_sensor_info *ret_sensor_info;
1601 ret_sensor_info = (struct omap34xxcam_sensor_info *)arg;
1602 mutex_lock(&vdev->mutex);
1603 rval = vidioc_int_priv_g_pixclk(vdev->vdev_sensor, &pixclk);
1604 mutex_unlock(&vdev->mutex);
1605 if (rval)
1606 goto out;
1607 mutex_lock(&vdev->mutex);
1608 rval = vidioc_int_priv_g_activesize(vdev->vdev_sensor,
1609 &active_size);
1610 mutex_unlock(&vdev->mutex);
1611 if (rval)
1612 goto out;
1613 mutex_lock(&vdev->mutex);
1614 rval = vidioc_int_priv_g_fullsize(vdev->vdev_sensor,
1615 &full_size);
1616 mutex_unlock(&vdev->mutex);
1617 if (rval)
1618 goto out;
1619 ret_sensor_info->current_xclk = pixclk;
1620 memcpy(&ret_sensor_info->active_size, &active_size,
1621 sizeof(struct v4l2_pix_format));
1622 memcpy(&ret_sensor_info->full_size, &full_size,
1623 sizeof(struct v4l2_pix_format));
1624 rval = 0;
1625 goto out;
1628 if (vdev->vdev_sensor_config.sensor_isp) {
1629 rval = -EINVAL;
1630 } else {
1631 switch (cmd) {
1632 case VIDIOC_PRIVATE_ISP_AEWB_REQ:
1634 /* Need to update sensor first */
1635 struct isph3a_aewb_data *data;
1636 struct v4l2_control vc;
1638 data = (struct isph3a_aewb_data *) arg;
1639 if (data->update & SET_EXPOSURE) {
1640 dev_dbg(&vdev->vfd->dev, "using "
1641 "VIDIOC_PRIVATE_ISP_AEWB_REQ to set "
1642 "exposure is deprecated!\n");
1643 vc.id = V4L2_CID_EXPOSURE;
1644 vc.value = data->shutter;
1645 mutex_lock(&vdev->mutex);
1646 rval = vidioc_int_s_ctrl(vdev->vdev_sensor,
1647 &vc);
1648 mutex_unlock(&vdev->mutex);
1649 if (rval)
1650 goto out;
1652 if (data->update & SET_ANALOG_GAIN) {
1653 dev_dbg(&vdev->vfd->dev, "using "
1654 "VIDIOC_PRIVATE_ISP_AEWB_REQ to set "
1655 "gain is deprecated!\n");
1656 vc.id = V4L2_CID_GAIN;
1657 vc.value = data->gain;
1658 mutex_lock(&vdev->mutex);
1659 rval = vidioc_int_s_ctrl(vdev->vdev_sensor,
1660 &vc);
1661 mutex_unlock(&vdev->mutex);
1662 if (rval)
1663 goto out;
1666 break;
1667 case VIDIOC_PRIVATE_ISP_AF_REQ: {
1668 /* Need to update lens first */
1669 struct isp_af_data *data;
1670 struct v4l2_control vc;
1672 if (!vdev->vdev_lens) {
1673 rval = -EINVAL;
1674 goto out;
1676 data = (struct isp_af_data *) arg;
1677 if (data->update & LENS_DESIRED_POSITION) {
1678 dev_dbg(&vdev->vfd->dev, "using "
1679 "VIDIOC_PRIVATE_ISP_AF_REQ to set "
1680 "lens position is deprecated!\n");
1681 vc.id = V4L2_CID_FOCUS_ABSOLUTE;
1682 vc.value = data->desired_lens_direction;
1683 mutex_lock(&vdev->mutex);
1684 rval = vidioc_int_s_ctrl(vdev->vdev_lens, &vc);
1685 mutex_unlock(&vdev->mutex);
1686 if (rval)
1687 goto out;
1690 break;
1693 mutex_lock(&vdev->mutex);
1694 rval = isp_handle_private(isp, cmd, arg);
1695 mutex_unlock(&vdev->mutex);
1697 out:
1698 return rval;
1703 * File operations.
1708 * omap34xxcam_poll - file operations poll handler
1709 * @file: ptr. to system file structure
1710 * @wait: system poll table structure
1713 static unsigned int omap34xxcam_poll(struct file *file,
1714 struct poll_table_struct *wait)
1716 struct omap34xxcam_fh *fh = file->private_data;
1717 struct omap34xxcam_videodev *vdev = fh->vdev;
1718 struct videobuf_buffer *vb;
1720 mutex_lock(&vdev->mutex);
1721 if (vdev->streaming != file) {
1722 mutex_unlock(&vdev->mutex);
1723 return POLLERR;
1725 mutex_unlock(&vdev->mutex);
1727 mutex_lock(&fh->vbq.vb_lock);
1728 if (list_empty(&fh->vbq.stream)) {
1729 mutex_unlock(&fh->vbq.vb_lock);
1730 return POLLERR;
1732 vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
1733 mutex_unlock(&fh->vbq.vb_lock);
1735 poll_wait(file, &vb->done, wait);
1737 if (vb->state == VIDEOBUF_DONE || vb->state == VIDEOBUF_ERROR)
1738 return POLLIN | POLLRDNORM;
1740 return 0;
1744 * omap34xxcam_mmap - file operations mmap handler
1745 * @file: ptr. to system file structure
1746 * @vma: system virt. mem. area structure
1748 * Maps a virtual memory area via the video buffer API
1750 static int omap34xxcam_mmap(struct file *file, struct vm_area_struct *vma)
1752 struct omap34xxcam_fh *fh = file->private_data;
1753 return videobuf_mmap_mapper(&fh->vbq, vma);
1757 * omap34xxcam_open - file operations open handler
1758 * @inode: ptr. to system inode structure
1759 * @file: ptr. to system file structure
1761 * Allocates and initializes the per-filehandle data (omap34xxcam_fh),
1762 * enables the sensor, opens/initializes the ISP interface and the
1763 * video buffer queue. Note that this function will allow multiple
1764 * file handles to be open simultaneously, however only the first
1765 * handle opened will initialize the ISP. It is the application
1766 * responsibility to only use one handle for streaming and the others
1767 * for control only.
1768 * This function returns 0 upon success and -ENODEV upon error.
1770 static int omap34xxcam_open(struct file *file)
1772 int rval = 0;
1773 struct omap34xxcam_videodev *vdev = NULL;
1774 struct omap34xxcam_device *cam = omap34xxcam;
1775 struct device *isp;
1776 struct omap34xxcam_fh *fh;
1777 struct v4l2_format sensor_format;
1778 int first_user = 0;
1779 int i;
1781 for (i = 0; i < OMAP34XXCAM_VIDEODEVS; i++) {
1782 if (cam->vdevs[i].vfd
1783 && cam->vdevs[i].vfd->minor ==
1784 iminor(file->f_dentry->d_inode)) {
1785 vdev = &cam->vdevs[i];
1786 break;
1790 if (!vdev || !vdev->vfd)
1791 return -ENODEV;
1793 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1794 if (fh == NULL)
1795 return -ENOMEM;
1797 fh->vdev = vdev;
1799 mutex_lock(&vdev->mutex);
1800 for (i = 0; i <= OMAP34XXCAM_SLAVE_FLASH; i++) {
1801 if (vdev->slave[i] != v4l2_int_device_dummy()
1802 && !try_module_get(vdev->slave[i]->module)) {
1803 mutex_unlock(&vdev->mutex);
1804 dev_err(&vdev->vfd->dev, "can't try_module_get %s\n",
1805 vdev->slave[i]->name);
1806 rval = -ENODEV;
1807 goto out_try_module_get;
1811 if (atomic_inc_return(&vdev->users) == 1) {
1812 first_user = 1;
1813 isp = isp_get();
1814 if (!isp) {
1815 rval = -EBUSY;
1816 dev_err(&vdev->vfd->dev, "can't get isp\n");
1817 goto out_isp_get;
1819 cam->isp = isp;
1820 if (omap34xxcam_slave_power_set(vdev, V4L2_POWER_ON,
1821 OMAP34XXCAM_SLAVE_POWER_ALL)) {
1822 dev_err(&vdev->vfd->dev, "can't power up slaves\n");
1823 rval = -EBUSY;
1824 goto out_slave_power_set_standby;
1826 omap34xxcam_slave_power_set(
1827 vdev, V4L2_POWER_STANDBY,
1828 OMAP34XXCAM_SLAVE_POWER_SENSOR);
1829 omap34xxcam_slave_power_suggest(
1830 vdev, V4L2_POWER_STANDBY,
1831 OMAP34XXCAM_SLAVE_POWER_LENS);
1834 if (vdev->vdev_sensor == v4l2_int_device_dummy() || !first_user)
1835 goto out_no_pix;
1837 if (vdev->vdev_sensor_config.sensor_isp) {
1838 if ((vdev->slave_config[OMAP34XXCAM_SLAVE_SENSOR].cur_input
1839 != INPUT_CVBS_VI4A) &&
1840 (vdev->slave_config[OMAP34XXCAM_SLAVE_SENSOR].
1841 cur_input != INPUT_SVIDEO_VI2C_VI1C)) {
1842 struct v4l2_routing route;
1843 int rval;
1844 route.input = INPUT_CVBS_VI4A;
1845 route.output = 0;
1846 rval = vidioc_int_s_video_routing(vdev->vdev_sensor,
1847 &route);
1848 if (rval) {
1849 route.input = INPUT_SVIDEO_VI2C_VI1C;
1850 rval = vidioc_int_s_video_routing(
1851 vdev->vdev_sensor, &route);
1853 if (!rval)
1854 vdev->slave_config[OMAP34XXCAM_SLAVE_SENSOR]
1855 .cur_input = route.input;
1857 sensor_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1860 /* Get the format the sensor is using. */
1861 rval = vidioc_int_g_fmt_cap(vdev->vdev_sensor, &sensor_format);
1862 if (rval) {
1863 dev_err(&vdev->vfd->dev,
1864 "can't get current pix from sensor!\n");
1865 goto out_vidioc_int_g_fmt_cap;
1868 if (!vdev->pix.width)
1869 vdev->pix = sensor_format.fmt.pix;
1871 if (!vdev->vdev_sensor_config.sensor_isp) {
1872 struct v4l2_pix_format pix;
1873 struct v4l2_fract timeperframe =
1874 vdev->want_timeperframe;
1876 rval = s_pix_parm(vdev, &pix, &vdev->pix, &timeperframe);
1877 if (rval) {
1878 dev_err(&vdev->vfd->dev,
1879 "isp doesn't like the sensor!\n");
1880 goto out_isp_s_fmt_cap;
1884 out_no_pix:
1885 mutex_unlock(&vdev->mutex);
1887 file->private_data = fh;
1889 spin_lock_init(&fh->vbq_lock);
1891 videobuf_queue_sg_init(&fh->vbq, &omap34xxcam_vbq_ops, NULL,
1892 &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1893 V4L2_FIELD_NONE,
1894 sizeof(struct videobuf_buffer), fh);
1896 return 0;
1898 out_isp_s_fmt_cap:
1899 out_vidioc_int_g_fmt_cap:
1900 omap34xxcam_slave_power_set(vdev, V4L2_POWER_OFF,
1901 OMAP34XXCAM_SLAVE_POWER_ALL);
1902 out_slave_power_set_standby:
1903 isp_put();
1905 out_isp_get:
1906 atomic_dec(&vdev->users);
1907 mutex_unlock(&vdev->mutex);
1909 out_try_module_get:
1910 for (i--; i >= 0; i--)
1911 if (vdev->slave[i] != v4l2_int_device_dummy())
1912 module_put(vdev->slave[i]->module);
1914 kfree(fh);
1916 return rval;
1920 * omap34xxcam_release - file operations release handler
1921 * @inode: ptr. to system inode structure
1922 * @file: ptr. to system file structure
1924 * Complement of omap34xxcam_open. This function will flush any scheduled
1925 * work, disable the sensor, close the ISP interface, stop the
1926 * video buffer queue from streaming and free the per-filehandle data
1927 * (omap34xxcam_fh). Note that because multiple open file handles
1928 * are allowed, this function will only close the ISP and disable the
1929 * sensor when the last open file handle (by count) is closed.
1930 * This function returns 0.
1932 static int omap34xxcam_release(struct file *file)
1934 struct omap34xxcam_fh *fh = file->private_data;
1935 struct omap34xxcam_videodev *vdev = fh->vdev;
1936 struct device *isp = vdev->cam->isp;
1937 int i;
1939 mutex_lock(&vdev->mutex);
1940 if (vdev->streaming == file) {
1941 isp_stop(isp);
1942 videobuf_streamoff(&fh->vbq);
1943 omap34xxcam_slave_power_set(
1944 vdev, V4L2_POWER_STANDBY,
1945 OMAP34XXCAM_SLAVE_POWER_SENSOR);
1946 omap34xxcam_slave_power_suggest(
1947 vdev, V4L2_POWER_STANDBY,
1948 OMAP34XXCAM_SLAVE_POWER_LENS);
1949 vdev->streaming = NULL;
1952 if (atomic_dec_return(&vdev->users) == 0) {
1953 omap34xxcam_slave_power_set(vdev, V4L2_POWER_OFF,
1954 OMAP34XXCAM_SLAVE_POWER_ALL);
1955 isp_put();
1957 mutex_unlock(&vdev->mutex);
1959 file->private_data = NULL;
1961 for (i = 0; i <= OMAP34XXCAM_SLAVE_FLASH; i++)
1962 if (vdev->slave[i] != v4l2_int_device_dummy())
1963 module_put(vdev->slave[i]->module);
1965 kfree(fh);
1967 return 0;
1970 static struct v4l2_file_operations omap34xxcam_fops = {
1971 .owner = THIS_MODULE,
1972 .unlocked_ioctl = video_ioctl2,
1973 .poll = omap34xxcam_poll,
1974 .mmap = omap34xxcam_mmap,
1975 .open = omap34xxcam_open,
1976 .release = omap34xxcam_release,
1979 static void omap34xxcam_vfd_name_update(struct omap34xxcam_videodev *vdev)
1981 struct video_device *vfd = vdev->vfd;
1982 int i;
1984 strlcpy(vfd->name, CAM_SHORT_NAME, sizeof(vfd->name));
1985 for (i = 0; i <= OMAP34XXCAM_SLAVE_FLASH; i++) {
1986 strlcat(vfd->name, "/", sizeof(vfd->name));
1987 if (vdev->slave[i] == v4l2_int_device_dummy())
1988 continue;
1989 strlcat(vfd->name, vdev->slave[i]->name, sizeof(vfd->name));
1991 dev_dbg(&vdev->vfd->dev, "video%d is now %s\n", vfd->num, vfd->name);
1995 * omap34xxcam_device_unregister - V4L2 detach handler
1996 * @s: ptr. to standard V4L2 device information structure
1998 * Detach sensor and unregister and release the video device.
2000 static void omap34xxcam_device_unregister(struct v4l2_int_device *s)
2002 struct omap34xxcam_videodev *vdev = s->u.slave->master->priv;
2003 struct omap34xxcam_hw_config hwc;
2005 BUG_ON(vidioc_int_g_priv(s, &hwc) < 0);
2007 mutex_lock(&vdev->mutex);
2009 if (vdev->slave[hwc.dev_type] != v4l2_int_device_dummy()) {
2010 vdev->slave[hwc.dev_type] = v4l2_int_device_dummy();
2011 vdev->slaves--;
2012 omap34xxcam_vfd_name_update(vdev);
2015 if (vdev->slaves == 0 && vdev->vfd) {
2016 if (vdev->vfd->minor == -1) {
2018 * The device was never registered, so release the
2019 * video_device struct directly.
2021 video_device_release(vdev->vfd);
2022 } else {
2024 * The unregister function will release the
2025 * video_device struct as well as
2026 * unregistering it.
2028 video_unregister_device(vdev->vfd);
2030 vdev->vfd = NULL;
2033 mutex_unlock(&vdev->mutex);
2036 static const struct v4l2_ioctl_ops omap34xxcam_ioctl_ops = {
2037 .vidioc_querycap = vidioc_querycap,
2038 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2039 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2040 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2041 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2042 .vidioc_reqbufs = vidioc_reqbufs,
2043 .vidioc_querybuf = vidioc_querybuf,
2044 .vidioc_qbuf = vidioc_qbuf,
2045 .vidioc_dqbuf = vidioc_dqbuf,
2046 .vidioc_streamon = vidioc_streamon,
2047 .vidioc_streamoff = vidioc_streamoff,
2048 .vidioc_enum_input = vidioc_enum_input,
2049 .vidioc_g_input = vidioc_g_input,
2050 .vidioc_s_input = vidioc_s_input,
2051 .vidioc_queryctrl = vidioc_queryctrl,
2052 .vidioc_querymenu = vidioc_querymenu,
2053 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
2054 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
2055 .vidioc_g_parm = vidioc_g_parm,
2056 .vidioc_s_parm = vidioc_s_parm,
2057 .vidioc_cropcap = vidioc_cropcap,
2058 .vidioc_g_crop = vidioc_g_crop,
2059 .vidioc_s_crop = vidioc_s_crop,
2060 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2061 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
2062 .vidioc_s_std = vidioc_s_std,
2063 .vidioc_querystd = vidioc_querystd,
2064 .vidioc_default = vidioc_default,
2068 * omap34xxcam_device_register - V4L2 attach handler
2069 * @s: ptr. to standard V4L2 device information structure
2071 * Allocates and initializes the V4L2 video_device structure, initializes
2072 * the sensor, and finally
2073 registers the device with V4L2 based on the
2074 * video_device structure.
2076 * Returns 0 on success, otherwise an appropriate error code on
2077 * failure.
2079 static int omap34xxcam_device_register(struct v4l2_int_device *s)
2081 struct omap34xxcam_videodev *vdev = s->u.slave->master->priv;
2082 struct omap34xxcam_hw_config hwc;
2083 struct v4l2_ifparm ifparm;
2084 struct device *isp;
2085 int rval;
2087 /* We need to check rval just once. The place is here. */
2088 if (vidioc_int_g_priv(s, &hwc))
2089 return -ENODEV;
2091 if (vdev->index != hwc.dev_index)
2092 return -ENODEV;
2094 if (hwc.dev_type < 0 || hwc.dev_type > OMAP34XXCAM_SLAVE_FLASH)
2095 return -EINVAL;
2097 if (vdev->slave[hwc.dev_type] != v4l2_int_device_dummy())
2098 return -EBUSY;
2100 mutex_lock(&vdev->mutex);
2101 if (atomic_read(&vdev->users)) {
2102 printk(KERN_ERR "%s: we're open (%d), can't register\n",
2103 __func__, atomic_read(&vdev->users));
2104 mutex_unlock(&vdev->mutex);
2105 return -EBUSY;
2108 vdev->slaves++;
2110 vdev->slave[hwc.dev_type] = s;
2111 vdev->slave_config[hwc.dev_type] = hwc;
2113 if (hwc.dev_type == OMAP34XXCAM_SLAVE_SENSOR) {
2114 isp = isp_get();
2115 if (!isp) {
2116 rval = -EBUSY;
2117 printk(KERN_ERR "%s: can't get ISP, "
2118 "sensor init failed\n", __func__);
2119 goto err;
2121 vdev->cam->isp = isp;
2123 rval = omap34xxcam_slave_power_set(vdev, V4L2_POWER_ON,
2124 1 << hwc.dev_type);
2125 if (rval)
2126 goto err_omap34xxcam_slave_power_set;
2127 if (hwc.dev_type == OMAP34XXCAM_SLAVE_SENSOR) {
2128 struct v4l2_format format;
2130 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2131 rval = vidioc_int_g_fmt_cap(vdev->vdev_sensor, &format);
2132 if (rval)
2133 rval = -EBUSY;
2135 vdev->want_pix = format.fmt.pix;
2137 omap34xxcam_slave_power_set(vdev, V4L2_POWER_OFF, 1 << hwc.dev_type);
2138 if (hwc.dev_type == OMAP34XXCAM_SLAVE_SENSOR)
2139 isp_put();
2141 if (rval)
2142 goto err;
2144 /* Are we the first slave? */
2145 if (vdev->slaves == 1) {
2146 /* initialize the video_device struct */
2147 vdev->vfd = video_device_alloc();
2148 if (!vdev->vfd) {
2149 printk(KERN_ERR "%s: could not allocate "
2150 "video device struct\n", __func__);
2151 rval = -ENOMEM;
2152 goto err;
2154 vdev->vfd->release = video_device_release;
2155 vdev->vfd->minor = -1;
2156 vdev->vfd->fops = &omap34xxcam_fops;
2157 vdev->vfd->ioctl_ops = &omap34xxcam_ioctl_ops;
2158 video_set_drvdata(vdev->vfd, vdev);
2160 if (video_register_device(vdev->vfd, VFL_TYPE_GRABBER,
2161 hwc.dev_minor) < 0) {
2162 printk(KERN_ERR "%s: could not register V4L device\n",
2163 __func__);
2164 vdev->vfd->minor = -1;
2165 rval = -EBUSY;
2166 goto err;
2169 /*Determine whether the slave connected is BT656 decoder or a sensor*/
2170 if (!vidioc_int_g_ifparm(s, &ifparm)) {
2171 if (ifparm.if_type == V4L2_IF_TYPE_BT656) {
2172 vdev->vfd->current_norm = V4L2_STD_NTSC;
2173 vdev->vfd->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL;
2174 if ((ifparm.u.bt656.mode ==
2175 V4L2_IF_TYPE_BT656_MODE_BT_8BIT) ||
2176 (ifparm.u.bt656.mode ==
2177 V4L2_IF_TYPE_BT656_MODE_BT_10BIT))
2178 vdev->slave_mode[hwc.dev_type] = 1;
2181 omap34xxcam_vfd_name_update(vdev);
2183 mutex_unlock(&vdev->mutex);
2185 return 0;
2187 err_omap34xxcam_slave_power_set:
2188 if (hwc.dev_type == OMAP34XXCAM_SLAVE_SENSOR)
2189 isp_put();
2191 err:
2192 if (s == vdev->slave[hwc.dev_type]) {
2193 vdev->slave[hwc.dev_type] = v4l2_int_device_dummy();
2194 vdev->slaves--;
2197 mutex_unlock(&vdev->mutex);
2198 omap34xxcam_device_unregister(s);
2200 return rval;
2203 static struct v4l2_int_master omap34xxcam_master = {
2204 .attach = omap34xxcam_device_register,
2205 .detach = omap34xxcam_device_unregister,
2210 * Module initialisation and deinitialisation
2214 static void omap34xxcam_exit(void)
2216 struct omap34xxcam_device *cam = omap34xxcam;
2217 int i;
2219 if (!cam)
2220 return;
2222 for (i = 0; i < OMAP34XXCAM_VIDEODEVS; i++) {
2223 if (cam->vdevs[i].cam == NULL)
2224 continue;
2226 v4l2_int_device_unregister(&cam->vdevs[i].master);
2227 cam->vdevs[i].cam = NULL;
2230 omap34xxcam = NULL;
2232 kfree(cam);
2235 static int __init omap34xxcam_init(void)
2237 struct omap34xxcam_device *cam;
2238 int i;
2240 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
2241 if (!cam) {
2242 printk(KERN_ERR "%s: could not allocate memory\n", __func__);
2243 return -ENOMEM;
2246 omap34xxcam = cam;
2248 for (i = 0; i < OMAP34XXCAM_VIDEODEVS; i++) {
2249 struct omap34xxcam_videodev *vdev = &cam->vdevs[i];
2250 struct v4l2_int_device *m = &vdev->master;
2252 m->module = THIS_MODULE;
2253 strlcpy(m->name, CAM_NAME, sizeof(m->name));
2254 m->type = v4l2_int_type_master;
2255 m->u.master = &omap34xxcam_master;
2256 m->priv = vdev;
2258 mutex_init(&vdev->mutex);
2259 vdev->index = i;
2260 vdev->cam = cam;
2261 vdev->vdev_sensor =
2262 vdev->vdev_lens =
2263 vdev->vdev_flash = v4l2_int_device_dummy();
2264 #ifdef OMAP34XXCAM_POWEROFF_DELAY
2265 setup_timer(&vdev->poweroff_timer,
2266 omap34xxcam_slave_power_timer, (unsigned long)vdev);
2267 INIT_WORK(&vdev->poweroff_work, omap34xxcam_slave_power_work);
2268 #endif /* OMAP34XXCAM_POWEROFF_DELAY */
2270 if (v4l2_int_device_register(m))
2271 goto err;
2274 return 0;
2276 err:
2277 omap34xxcam_exit();
2278 return -ENODEV;
2281 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
2282 MODULE_DESCRIPTION("OMAP34xx Video for Linux camera driver");
2283 MODULE_LICENSE("GPL");
2285 late_initcall(omap34xxcam_init);
2286 module_exit(omap34xxcam_exit);