2 * drivers/media/video/omap24xxcam.c
4 * OMAP 2 camera block driver.
6 * Copyright (C) 2004 MontaVista Software, Inc.
7 * Copyright (C) 2004 Texas Instruments.
8 * Copyright (C) 2007-2008 Nokia Corporation.
10 * Contact: Sakari Ailus <sakari.ailus@nokia.com>
12 * Based on code from Andy Lowe <source@mvista.com>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * version 2 as published by the Free Software Foundation.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29 #include <linux/delay.h>
30 #include <linux/kernel.h>
31 #include <linux/interrupt.h>
32 #include <linux/videodev2.h>
33 #include <linux/pci.h> /* needed for videobufs */
34 #include <linux/version.h>
35 #include <linux/platform_device.h>
36 #include <linux/clk.h>
39 #include <media/v4l2-common.h>
40 #include <media/v4l2-ioctl.h>
42 #include "omap24xxcam.h"
44 #define OMAP24XXCAM_VERSION KERNEL_VERSION(0, 0, 0)
46 #define RESET_TIMEOUT_NS 10000
48 static void omap24xxcam_reset(struct omap24xxcam_device
*cam
);
49 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device
*cam
);
50 static void omap24xxcam_device_unregister(struct v4l2_int_device
*s
);
51 static int omap24xxcam_remove(struct platform_device
*pdev
);
53 /* module parameters */
54 static int video_nr
= -1; /* video device minor (-1 ==> auto assign) */
56 * Maximum amount of memory to use for capture buffers.
57 * Default is 4800KB, enough to double-buffer SXGA.
59 static int capture_mem
= 1280 * 960 * 2 * 2;
61 static struct v4l2_int_device omap24xxcam
;
69 static void omap24xxcam_clock_put(struct omap24xxcam_device
*cam
)
71 if (cam
->ick
!= NULL
&& !IS_ERR(cam
->ick
))
73 if (cam
->fck
!= NULL
&& !IS_ERR(cam
->fck
))
76 cam
->ick
= cam
->fck
= NULL
;
79 static int omap24xxcam_clock_get(struct omap24xxcam_device
*cam
)
83 cam
->fck
= clk_get(cam
->dev
, "fck");
84 if (IS_ERR(cam
->fck
)) {
85 dev_err(cam
->dev
, "can't get camera fck");
86 rval
= PTR_ERR(cam
->fck
);
87 omap24xxcam_clock_put(cam
);
91 cam
->ick
= clk_get(cam
->dev
, "ick");
92 if (IS_ERR(cam
->ick
)) {
93 dev_err(cam
->dev
, "can't get camera ick");
94 rval
= PTR_ERR(cam
->ick
);
95 omap24xxcam_clock_put(cam
);
101 static void omap24xxcam_clock_on(struct omap24xxcam_device
*cam
)
103 clk_enable(cam
->fck
);
104 clk_enable(cam
->ick
);
107 static void omap24xxcam_clock_off(struct omap24xxcam_device
*cam
)
109 clk_disable(cam
->fck
);
110 clk_disable(cam
->ick
);
122 * To disable xclk, use value zero.
124 static void omap24xxcam_core_xclk_set(const struct omap24xxcam_device
*cam
,
128 u32 divisor
= CAM_MCLK
/ xclk
;
131 omap24xxcam_reg_out(cam
->mmio_base
+ CC_REG_OFFSET
,
133 CC_CTRL_XCLK_DIV_BYPASS
);
135 omap24xxcam_reg_out(cam
->mmio_base
+ CC_REG_OFFSET
,
136 CC_CTRL_XCLK
, divisor
);
138 omap24xxcam_reg_out(cam
->mmio_base
+ CC_REG_OFFSET
,
139 CC_CTRL_XCLK
, CC_CTRL_XCLK_DIV_STABLE_LOW
);
142 static void omap24xxcam_core_hwinit(const struct omap24xxcam_device
*cam
)
145 * Setting the camera core AUTOIDLE bit causes problems with frame
146 * synchronization, so we will clear the AUTOIDLE bit instead.
148 omap24xxcam_reg_out(cam
->mmio_base
+ CC_REG_OFFSET
, CC_SYSCONFIG
,
149 CC_SYSCONFIG_AUTOIDLE
);
151 /* program the camera interface DMA packet size */
152 omap24xxcam_reg_out(cam
->mmio_base
+ CC_REG_OFFSET
, CC_CTRL_DMA
,
153 CC_CTRL_DMA_EN
| (DMA_THRESHOLD
/ 4 - 1));
155 /* enable camera core error interrupts */
156 omap24xxcam_reg_out(cam
->mmio_base
+ CC_REG_OFFSET
, CC_IRQENABLE
,
157 CC_IRQENABLE_FW_ERR_IRQ
158 | CC_IRQENABLE_FSC_ERR_IRQ
159 | CC_IRQENABLE_SSC_ERR_IRQ
160 | CC_IRQENABLE_FIFO_OF_IRQ
);
164 * Enable the camera core.
166 * Data transfer to the camera DMA starts from next starting frame.
168 static void omap24xxcam_core_enable(const struct omap24xxcam_device
*cam
)
171 omap24xxcam_reg_out(cam
->mmio_base
+ CC_REG_OFFSET
, CC_CTRL
,
176 * Disable camera core.
178 * The data transfer will be stopped immediately (CC_CTRL_CC_RST). The
179 * core internal state machines will be reset. Use
180 * CC_CTRL_CC_FRAME_TRIG instead if you want to transfer the current
183 static void omap24xxcam_core_disable(const struct omap24xxcam_device
*cam
)
185 omap24xxcam_reg_out(cam
->mmio_base
+ CC_REG_OFFSET
, CC_CTRL
,
189 /* Interrupt service routine for camera core interrupts. */
190 static void omap24xxcam_core_isr(struct omap24xxcam_device
*cam
)
193 const u32 cc_irqstatus_err
=
194 CC_IRQSTATUS_FW_ERR_IRQ
195 | CC_IRQSTATUS_FSC_ERR_IRQ
196 | CC_IRQSTATUS_SSC_ERR_IRQ
197 | CC_IRQSTATUS_FIFO_UF_IRQ
198 | CC_IRQSTATUS_FIFO_OF_IRQ
;
200 cc_irqstatus
= omap24xxcam_reg_in(cam
->mmio_base
+ CC_REG_OFFSET
,
202 omap24xxcam_reg_out(cam
->mmio_base
+ CC_REG_OFFSET
, CC_IRQSTATUS
,
205 if (cc_irqstatus
& cc_irqstatus_err
206 && !atomic_read(&cam
->in_reset
)) {
207 dev_dbg(cam
->dev
, "resetting camera, cc_irqstatus 0x%x\n",
209 omap24xxcam_reset(cam
);
215 * videobuf_buffer handling.
217 * Memory for mmapped videobuf_buffers is not allocated
218 * conventionally, but by several kmalloc allocations and then
219 * creating the scatterlist on our own. User-space buffers are handled
225 * Free the memory-mapped buffer memory allocated for a
226 * videobuf_buffer and the associated scatterlist.
228 static void omap24xxcam_vbq_free_mmap_buffer(struct videobuf_buffer
*vb
)
230 struct videobuf_dmabuf
*dma
= videobuf_to_dma(vb
);
235 if (dma
->sglist
== NULL
)
241 alloc_size
= sg_dma_len(&dma
->sglist
[i
]);
242 page
= sg_page(&dma
->sglist
[i
]);
244 ClearPageReserved(page
++);
245 } while (alloc_size
-= PAGE_SIZE
);
246 __free_pages(sg_page(&dma
->sglist
[i
]),
247 get_order(sg_dma_len(&dma
->sglist
[i
])));
254 /* Release all memory related to the videobuf_queue. */
255 static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue
*vbq
)
259 mutex_lock(&vbq
->vb_lock
);
261 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
262 if (NULL
== vbq
->bufs
[i
])
264 if (V4L2_MEMORY_MMAP
!= vbq
->bufs
[i
]->memory
)
266 vbq
->ops
->buf_release(vbq
, vbq
->bufs
[i
]);
267 omap24xxcam_vbq_free_mmap_buffer(vbq
->bufs
[i
]);
272 mutex_unlock(&vbq
->vb_lock
);
274 videobuf_mmap_free(vbq
);
278 * Allocate physically as contiguous as possible buffer for video
279 * frame and allocate and build DMA scatter-gather list for it.
281 static int omap24xxcam_vbq_alloc_mmap_buffer(struct videobuf_buffer
*vb
)
284 size_t alloc_size
, size
= vb
->bsize
; /* vb->bsize is page aligned */
286 int max_pages
, err
= 0, i
= 0;
287 struct videobuf_dmabuf
*dma
= videobuf_to_dma(vb
);
290 * allocate maximum size scatter-gather list. Note this is
291 * overhead. We may not use as many entries as we allocate
293 max_pages
= vb
->bsize
>> PAGE_SHIFT
;
294 dma
->sglist
= kcalloc(max_pages
, sizeof(*dma
->sglist
), GFP_KERNEL
);
295 if (dma
->sglist
== NULL
) {
301 order
= get_order(size
);
303 * do not over-allocate even if we would get larger
304 * contiguous chunk that way
306 if ((PAGE_SIZE
<< order
) > size
)
309 /* try to allocate as many contiguous pages as possible */
310 page
= alloc_pages(GFP_KERNEL
| GFP_DMA
, order
);
311 /* if allocation fails, try to allocate smaller amount */
312 while (page
== NULL
) {
314 page
= alloc_pages(GFP_KERNEL
| GFP_DMA
, order
);
315 if (page
== NULL
&& !order
) {
320 size
-= (PAGE_SIZE
<< order
);
322 /* append allocated chunk of pages into scatter-gather list */
323 sg_set_page(&dma
->sglist
[i
], page
, PAGE_SIZE
<< order
, 0);
327 alloc_size
= (PAGE_SIZE
<< order
);
329 /* clear pages before giving them to user space */
330 memset(page_address(page
), 0, alloc_size
);
332 /* mark allocated pages reserved */
334 SetPageReserved(page
++);
335 } while (alloc_size
-= PAGE_SIZE
);
338 * REVISIT: not fully correct to assign nr_pages == sglen but
339 * video-buf is passing nr_pages for e.g. unmap_sg calls
341 dma
->nr_pages
= dma
->sglen
;
342 dma
->direction
= PCI_DMA_FROMDEVICE
;
347 omap24xxcam_vbq_free_mmap_buffer(vb
);
351 static int omap24xxcam_vbq_alloc_mmap_buffers(struct videobuf_queue
*vbq
,
355 struct omap24xxcam_fh
*fh
=
356 container_of(vbq
, struct omap24xxcam_fh
, vbq
);
358 mutex_lock(&vbq
->vb_lock
);
360 for (i
= 0; i
< count
; i
++) {
361 err
= omap24xxcam_vbq_alloc_mmap_buffer(vbq
->bufs
[i
]);
364 dev_dbg(fh
->cam
->dev
, "sglen is %d for buffer %d\n",
365 videobuf_to_dma(vbq
->bufs
[i
])->sglen
, i
);
368 mutex_unlock(&vbq
->vb_lock
);
374 omap24xxcam_vbq_free_mmap_buffer(vbq
->bufs
[i
]);
377 mutex_unlock(&vbq
->vb_lock
);
383 * This routine is called from interrupt context when a scatter-gather DMA
384 * transfer of a videobuf_buffer completes.
386 static void omap24xxcam_vbq_complete(struct omap24xxcam_sgdma
*sgdma
,
389 struct omap24xxcam_device
*cam
=
390 container_of(sgdma
, struct omap24xxcam_device
, sgdma
);
391 struct omap24xxcam_fh
*fh
= cam
->streaming
->private_data
;
392 struct videobuf_buffer
*vb
= (struct videobuf_buffer
*)arg
;
393 const u32 csr_error
= CAMDMA_CSR_MISALIGNED_ERR
394 | CAMDMA_CSR_SUPERVISOR_ERR
| CAMDMA_CSR_SECURE_ERR
395 | CAMDMA_CSR_TRANS_ERR
| CAMDMA_CSR_DROP
;
398 spin_lock_irqsave(&cam
->core_enable_disable_lock
, flags
);
399 if (--cam
->sgdma_in_queue
== 0)
400 omap24xxcam_core_disable(cam
);
401 spin_unlock_irqrestore(&cam
->core_enable_disable_lock
, flags
);
403 do_gettimeofday(&vb
->ts
);
404 vb
->field_count
= atomic_add_return(2, &fh
->field_count
);
405 if (csr
& csr_error
) {
406 vb
->state
= VIDEOBUF_ERROR
;
407 if (!atomic_read(&fh
->cam
->in_reset
)) {
408 dev_dbg(cam
->dev
, "resetting camera, csr 0x%x\n", csr
);
409 omap24xxcam_reset(cam
);
412 vb
->state
= VIDEOBUF_DONE
;
416 static void omap24xxcam_vbq_release(struct videobuf_queue
*vbq
,
417 struct videobuf_buffer
*vb
)
419 struct videobuf_dmabuf
*dma
= videobuf_to_dma(vb
);
421 /* wait for buffer, especially to get out of the sgdma queue */
422 videobuf_waiton(vb
, 0, 0);
423 if (vb
->memory
== V4L2_MEMORY_MMAP
) {
424 dma_unmap_sg(vbq
->dev
, dma
->sglist
, dma
->sglen
,
426 dma
->direction
= DMA_NONE
;
428 videobuf_dma_unmap(vbq
, videobuf_to_dma(vb
));
429 videobuf_dma_free(videobuf_to_dma(vb
));
432 vb
->state
= VIDEOBUF_NEEDS_INIT
;
436 * Limit the number of available kernel image capture buffers based on the
437 * number requested, the currently selected image size, and the maximum
438 * amount of memory permitted for kernel capture buffers.
440 static int omap24xxcam_vbq_setup(struct videobuf_queue
*vbq
, unsigned int *cnt
,
443 struct omap24xxcam_fh
*fh
= vbq
->priv_data
;
446 *cnt
= VIDEO_MAX_FRAME
; /* supply a default number of buffers */
448 if (*cnt
> VIDEO_MAX_FRAME
)
449 *cnt
= VIDEO_MAX_FRAME
;
451 *size
= fh
->pix
.sizeimage
;
453 /* accessing fh->cam->capture_mem is ok, it's constant */
454 while (*size
* *cnt
> fh
->cam
->capture_mem
)
460 static int omap24xxcam_dma_iolock(struct videobuf_queue
*vbq
,
461 struct videobuf_dmabuf
*dma
)
465 dma
->direction
= PCI_DMA_FROMDEVICE
;
466 if (!dma_map_sg(vbq
->dev
, dma
->sglist
, dma
->sglen
, dma
->direction
)) {
476 static int omap24xxcam_vbq_prepare(struct videobuf_queue
*vbq
,
477 struct videobuf_buffer
*vb
,
478 enum v4l2_field field
)
480 struct omap24xxcam_fh
*fh
= vbq
->priv_data
;
484 * Accessing pix here is okay since it's constant while
485 * streaming is on (and we only get called then).
488 /* This is a userspace buffer. */
489 if (fh
->pix
.sizeimage
> vb
->bsize
) {
490 /* The buffer isn't big enough. */
493 vb
->size
= fh
->pix
.sizeimage
;
495 if (vb
->state
!= VIDEOBUF_NEEDS_INIT
) {
497 * We have a kernel bounce buffer that has
498 * already been allocated.
500 if (fh
->pix
.sizeimage
> vb
->size
) {
502 * The image size has been changed to
503 * a larger size since this buffer was
504 * allocated, so we need to free and
507 omap24xxcam_vbq_release(vbq
, vb
);
508 vb
->size
= fh
->pix
.sizeimage
;
511 /* We need to allocate a new kernel bounce buffer. */
512 vb
->size
= fh
->pix
.sizeimage
;
519 vb
->width
= fh
->pix
.width
;
520 vb
->height
= fh
->pix
.height
;
523 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
524 if (vb
->memory
== V4L2_MEMORY_MMAP
)
526 * we have built the scatter-gather list by ourself so
527 * do the scatter-gather mapping as well
529 err
= omap24xxcam_dma_iolock(vbq
, videobuf_to_dma(vb
));
531 err
= videobuf_iolock(vbq
, vb
, NULL
);
535 vb
->state
= VIDEOBUF_PREPARED
;
537 omap24xxcam_vbq_release(vbq
, vb
);
542 static void omap24xxcam_vbq_queue(struct videobuf_queue
*vbq
,
543 struct videobuf_buffer
*vb
)
545 struct omap24xxcam_fh
*fh
= vbq
->priv_data
;
546 struct omap24xxcam_device
*cam
= fh
->cam
;
547 enum videobuf_state state
= vb
->state
;
552 * FIXME: We're marking the buffer active since we have no
553 * pretty way of marking it active exactly when the
554 * scatter-gather transfer starts.
556 vb
->state
= VIDEOBUF_ACTIVE
;
558 err
= omap24xxcam_sgdma_queue(&fh
->cam
->sgdma
,
559 videobuf_to_dma(vb
)->sglist
,
560 videobuf_to_dma(vb
)->sglen
, vb
->size
,
561 omap24xxcam_vbq_complete
, vb
);
564 spin_lock_irqsave(&cam
->core_enable_disable_lock
, flags
);
565 if (++cam
->sgdma_in_queue
== 1
566 && !atomic_read(&cam
->in_reset
))
567 omap24xxcam_core_enable(cam
);
568 spin_unlock_irqrestore(&cam
->core_enable_disable_lock
, flags
);
571 * Oops. We're not supposed to get any errors here.
572 * The only way we could get an error is if we ran out
573 * of scatter-gather DMA slots, but we are supposed to
574 * have at least as many scatter-gather DMA slots as
575 * video buffers so that can't happen.
577 dev_err(cam
->dev
, "failed to queue a video buffer for dma!\n");
578 dev_err(cam
->dev
, "likely a bug in the driver!\n");
583 static struct videobuf_queue_ops omap24xxcam_vbq_ops
= {
584 .buf_setup
= omap24xxcam_vbq_setup
,
585 .buf_prepare
= omap24xxcam_vbq_prepare
,
586 .buf_queue
= omap24xxcam_vbq_queue
,
587 .buf_release
= omap24xxcam_vbq_release
,
592 * OMAP main camera system
597 * Reset camera block to power-on state.
599 static void omap24xxcam_poweron_reset(struct omap24xxcam_device
*cam
)
601 int max_loop
= RESET_TIMEOUT_NS
;
603 /* Reset whole camera subsystem */
604 omap24xxcam_reg_out(cam
->mmio_base
,
606 CAM_SYSCONFIG_SOFTRESET
);
608 /* Wait till it's finished */
609 while (!(omap24xxcam_reg_in(cam
->mmio_base
, CAM_SYSSTATUS
)
610 & CAM_SYSSTATUS_RESETDONE
)
615 if (!(omap24xxcam_reg_in(cam
->mmio_base
, CAM_SYSSTATUS
)
616 & CAM_SYSSTATUS_RESETDONE
))
617 dev_err(cam
->dev
, "camera soft reset timeout\n");
621 * (Re)initialise the camera block.
623 static void omap24xxcam_hwinit(struct omap24xxcam_device
*cam
)
625 omap24xxcam_poweron_reset(cam
);
627 /* set the camera subsystem autoidle bit */
628 omap24xxcam_reg_out(cam
->mmio_base
, CAM_SYSCONFIG
,
629 CAM_SYSCONFIG_AUTOIDLE
);
631 /* set the camera MMU autoidle bit */
632 omap24xxcam_reg_out(cam
->mmio_base
,
633 CAMMMU_REG_OFFSET
+ CAMMMU_SYSCONFIG
,
634 CAMMMU_SYSCONFIG_AUTOIDLE
);
636 omap24xxcam_core_hwinit(cam
);
638 omap24xxcam_dma_hwinit(&cam
->sgdma
.dma
);
642 * Callback for dma transfer stalling.
644 static void omap24xxcam_stalled_dma_reset(unsigned long data
)
646 struct omap24xxcam_device
*cam
= (struct omap24xxcam_device
*)data
;
648 if (!atomic_read(&cam
->in_reset
)) {
649 dev_dbg(cam
->dev
, "dma stalled, resetting camera\n");
650 omap24xxcam_reset(cam
);
655 * Stop capture. Mark we're doing a reset, stop DMA transfers and
656 * core. (No new scatter-gather transfers will be queued whilst
657 * in_reset is non-zero.)
659 * If omap24xxcam_capture_stop is called from several places at
660 * once, only the first call will have an effect. Similarly, the last
661 * call omap24xxcam_streaming_cont will have effect.
663 * Serialisation is ensured by using cam->core_enable_disable_lock.
665 static void omap24xxcam_capture_stop(struct omap24xxcam_device
*cam
)
669 spin_lock_irqsave(&cam
->core_enable_disable_lock
, flags
);
671 if (atomic_inc_return(&cam
->in_reset
) != 1) {
672 spin_unlock_irqrestore(&cam
->core_enable_disable_lock
, flags
);
676 omap24xxcam_core_disable(cam
);
678 spin_unlock_irqrestore(&cam
->core_enable_disable_lock
, flags
);
680 omap24xxcam_sgdma_sync(&cam
->sgdma
);
684 * Reset and continue streaming.
686 * Note: Resetting the camera FIFO via the CC_RST bit in the CC_CTRL
687 * register is supposed to be sufficient to recover from a camera
688 * interface error, but it doesn't seem to be enough. If we only do
689 * that then subsequent image captures are out of sync by either one
690 * or two times DMA_THRESHOLD bytes. Resetting and re-initializing the
691 * entire camera subsystem prevents the problem with frame
694 static void omap24xxcam_capture_cont(struct omap24xxcam_device
*cam
)
698 spin_lock_irqsave(&cam
->core_enable_disable_lock
, flags
);
700 if (atomic_read(&cam
->in_reset
) != 1)
703 omap24xxcam_hwinit(cam
);
705 omap24xxcam_sensor_if_enable(cam
);
707 omap24xxcam_sgdma_process(&cam
->sgdma
);
709 if (cam
->sgdma_in_queue
)
710 omap24xxcam_core_enable(cam
);
713 atomic_dec(&cam
->in_reset
);
714 spin_unlock_irqrestore(&cam
->core_enable_disable_lock
, flags
);
718 omap24xxcam_streaming_show(struct device
*dev
, struct device_attribute
*attr
,
721 struct omap24xxcam_device
*cam
= dev_get_drvdata(dev
);
723 return sprintf(buf
, "%s\n", cam
->streaming
? "active" : "inactive");
725 static DEVICE_ATTR(streaming
, S_IRUGO
, omap24xxcam_streaming_show
, NULL
);
728 * Stop capture and restart it. I.e. reset the camera during use.
730 static void omap24xxcam_reset(struct omap24xxcam_device
*cam
)
732 omap24xxcam_capture_stop(cam
);
733 omap24xxcam_capture_cont(cam
);
737 * The main interrupt handler.
739 static irqreturn_t
omap24xxcam_isr(int irq
, void *arg
)
741 struct omap24xxcam_device
*cam
= (struct omap24xxcam_device
*)arg
;
743 unsigned int irqhandled
= 0;
745 irqstatus
= omap24xxcam_reg_in(cam
->mmio_base
, CAM_IRQSTATUS
);
748 (CAM_IRQSTATUS_DMA_IRQ2
| CAM_IRQSTATUS_DMA_IRQ1
749 | CAM_IRQSTATUS_DMA_IRQ0
)) {
750 omap24xxcam_dma_isr(&cam
->sgdma
.dma
);
753 if (irqstatus
& CAM_IRQSTATUS_CC_IRQ
) {
754 omap24xxcam_core_isr(cam
);
757 if (irqstatus
& CAM_IRQSTATUS_MMU_IRQ
)
758 dev_err(cam
->dev
, "unhandled camera MMU interrupt!\n");
760 return IRQ_RETVAL(irqhandled
);
770 * Enable the external sensor interface. Try to negotiate interface
771 * parameters with the sensor and start using the new ones. The calls
772 * to sensor_if_enable and sensor_if_disable need not to be balanced.
774 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device
*cam
)
777 struct v4l2_ifparm p
;
779 rval
= vidioc_int_g_ifparm(cam
->sdev
, &p
);
781 dev_err(cam
->dev
, "vidioc_int_g_ifparm failed with %d\n", rval
);
785 cam
->if_type
= p
.if_type
;
787 cam
->cc_ctrl
= CC_CTRL_CC_EN
;
790 case V4L2_IF_TYPE_BT656
:
791 if (p
.u
.bt656
.frame_start_on_rising_vs
)
792 cam
->cc_ctrl
|= CC_CTRL_NOBT_SYNCHRO
;
793 if (p
.u
.bt656
.bt_sync_correct
)
794 cam
->cc_ctrl
|= CC_CTRL_BT_CORRECT
;
796 cam
->cc_ctrl
|= CC_CTRL_PAR_ORDERCAM
;
797 if (p
.u
.bt656
.latch_clk_inv
)
798 cam
->cc_ctrl
|= CC_CTRL_PAR_CLK_POL
;
799 if (p
.u
.bt656
.nobt_hs_inv
)
800 cam
->cc_ctrl
|= CC_CTRL_NOBT_HS_POL
;
801 if (p
.u
.bt656
.nobt_vs_inv
)
802 cam
->cc_ctrl
|= CC_CTRL_NOBT_VS_POL
;
804 switch (p
.u
.bt656
.mode
) {
805 case V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT
:
806 cam
->cc_ctrl
|= CC_CTRL_PAR_MODE_NOBT8
;
808 case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT
:
809 cam
->cc_ctrl
|= CC_CTRL_PAR_MODE_NOBT10
;
811 case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT
:
812 cam
->cc_ctrl
|= CC_CTRL_PAR_MODE_NOBT12
;
814 case V4L2_IF_TYPE_BT656_MODE_BT_8BIT
:
815 cam
->cc_ctrl
|= CC_CTRL_PAR_MODE_BT8
;
817 case V4L2_IF_TYPE_BT656_MODE_BT_10BIT
:
818 cam
->cc_ctrl
|= CC_CTRL_PAR_MODE_BT10
;
822 "bt656 interface mode %d not supported\n",
827 * The clock rate that the sensor wants has changed.
828 * We have to adjust the xclk from OMAP 2 side to
829 * match the sensor's wish as closely as possible.
831 if (p
.u
.bt656
.clock_curr
!= cam
->if_u
.bt656
.xclk
) {
832 u32 xclk
= p
.u
.bt656
.clock_curr
;
841 divisor
= CAM_MCLK
/ xclk
;
842 if (divisor
* xclk
< CAM_MCLK
)
844 if (CAM_MCLK
/ divisor
< p
.u
.bt656
.clock_min
850 xclk
= CAM_MCLK
/ divisor
;
852 if (xclk
< p
.u
.bt656
.clock_min
853 || xclk
> p
.u
.bt656
.clock_max
)
856 cam
->if_u
.bt656
.xclk
= xclk
;
858 omap24xxcam_core_xclk_set(cam
, cam
->if_u
.bt656
.xclk
);
861 /* FIXME: how about other interfaces? */
862 dev_err(cam
->dev
, "interface type %d not supported\n",
870 static void omap24xxcam_sensor_if_disable(const struct omap24xxcam_device
*cam
)
872 switch (cam
->if_type
) {
873 case V4L2_IF_TYPE_BT656
:
874 omap24xxcam_core_xclk_set(cam
, 0);
880 * Initialise the sensor hardware.
882 static int omap24xxcam_sensor_init(struct omap24xxcam_device
*cam
)
885 struct v4l2_int_device
*sdev
= cam
->sdev
;
887 omap24xxcam_clock_on(cam
);
888 err
= omap24xxcam_sensor_if_enable(cam
);
890 dev_err(cam
->dev
, "sensor interface could not be enabled at "
891 "initialisation, %d\n", err
);
896 /* power up sensor during sensor initialization */
897 vidioc_int_s_power(sdev
, 1);
899 err
= vidioc_int_dev_init(sdev
);
901 dev_err(cam
->dev
, "cannot initialize sensor, error %d\n", err
);
902 /* Sensor init failed --- it's nonexistent to us! */
907 dev_info(cam
->dev
, "sensor is %s\n", sdev
->name
);
910 omap24xxcam_sensor_if_disable(cam
);
911 omap24xxcam_clock_off(cam
);
913 vidioc_int_s_power(sdev
, 0);
918 static void omap24xxcam_sensor_exit(struct omap24xxcam_device
*cam
)
921 vidioc_int_dev_exit(cam
->sdev
);
924 static void omap24xxcam_sensor_disable(struct omap24xxcam_device
*cam
)
926 omap24xxcam_sensor_if_disable(cam
);
927 omap24xxcam_clock_off(cam
);
928 vidioc_int_s_power(cam
->sdev
, 0);
932 * Power-up and configure camera sensor. It's ready for capturing now.
934 static int omap24xxcam_sensor_enable(struct omap24xxcam_device
*cam
)
938 omap24xxcam_clock_on(cam
);
940 omap24xxcam_sensor_if_enable(cam
);
942 rval
= vidioc_int_s_power(cam
->sdev
, 1);
946 rval
= vidioc_int_init(cam
->sdev
);
953 omap24xxcam_sensor_disable(cam
);
958 static void omap24xxcam_sensor_reset_work(struct work_struct
*work
)
960 struct omap24xxcam_device
*cam
=
961 container_of(work
, struct omap24xxcam_device
,
964 if (atomic_read(&cam
->reset_disable
))
967 omap24xxcam_capture_stop(cam
);
969 if (vidioc_int_reset(cam
->sdev
) == 0) {
970 vidioc_int_init(cam
->sdev
);
972 /* Can't reset it by vidioc_int_reset. */
973 omap24xxcam_sensor_disable(cam
);
974 omap24xxcam_sensor_enable(cam
);
977 omap24xxcam_capture_cont(cam
);
986 static int vidioc_querycap(struct file
*file
, void *fh
,
987 struct v4l2_capability
*cap
)
989 struct omap24xxcam_fh
*ofh
= fh
;
990 struct omap24xxcam_device
*cam
= ofh
->cam
;
992 strlcpy(cap
->driver
, CAM_NAME
, sizeof(cap
->driver
));
993 strlcpy(cap
->card
, cam
->vfd
->name
, sizeof(cap
->card
));
994 cap
->version
= OMAP24XXCAM_VERSION
;
995 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1000 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *fh
,
1001 struct v4l2_fmtdesc
*f
)
1003 struct omap24xxcam_fh
*ofh
= fh
;
1004 struct omap24xxcam_device
*cam
= ofh
->cam
;
1007 rval
= vidioc_int_enum_fmt_cap(cam
->sdev
, f
);
1012 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *fh
,
1013 struct v4l2_format
*f
)
1015 struct omap24xxcam_fh
*ofh
= fh
;
1016 struct omap24xxcam_device
*cam
= ofh
->cam
;
1019 mutex_lock(&cam
->mutex
);
1020 rval
= vidioc_int_g_fmt_cap(cam
->sdev
, f
);
1021 mutex_unlock(&cam
->mutex
);
1026 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *fh
,
1027 struct v4l2_format
*f
)
1029 struct omap24xxcam_fh
*ofh
= fh
;
1030 struct omap24xxcam_device
*cam
= ofh
->cam
;
1033 mutex_lock(&cam
->mutex
);
1034 if (cam
->streaming
) {
1039 rval
= vidioc_int_s_fmt_cap(cam
->sdev
, f
);
1042 mutex_unlock(&cam
->mutex
);
1045 mutex_lock(&ofh
->vbq
.vb_lock
);
1046 ofh
->pix
= f
->fmt
.pix
;
1047 mutex_unlock(&ofh
->vbq
.vb_lock
);
1050 memset(f
, 0, sizeof(*f
));
1051 vidioc_g_fmt_vid_cap(file
, fh
, f
);
1056 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *fh
,
1057 struct v4l2_format
*f
)
1059 struct omap24xxcam_fh
*ofh
= fh
;
1060 struct omap24xxcam_device
*cam
= ofh
->cam
;
1063 mutex_lock(&cam
->mutex
);
1064 rval
= vidioc_int_try_fmt_cap(cam
->sdev
, f
);
1065 mutex_unlock(&cam
->mutex
);
1070 static int vidioc_reqbufs(struct file
*file
, void *fh
,
1071 struct v4l2_requestbuffers
*b
)
1073 struct omap24xxcam_fh
*ofh
= fh
;
1074 struct omap24xxcam_device
*cam
= ofh
->cam
;
1077 mutex_lock(&cam
->mutex
);
1078 if (cam
->streaming
) {
1079 mutex_unlock(&cam
->mutex
);
1083 omap24xxcam_vbq_free_mmap_buffers(&ofh
->vbq
);
1084 mutex_unlock(&cam
->mutex
);
1086 rval
= videobuf_reqbufs(&ofh
->vbq
, b
);
1089 * Either videobuf_reqbufs failed or the buffers are not
1090 * memory-mapped (which would need special attention).
1092 if (rval
< 0 || b
->memory
!= V4L2_MEMORY_MMAP
)
1095 rval
= omap24xxcam_vbq_alloc_mmap_buffers(&ofh
->vbq
, rval
);
1097 omap24xxcam_vbq_free_mmap_buffers(&ofh
->vbq
);
1103 static int vidioc_querybuf(struct file
*file
, void *fh
,
1104 struct v4l2_buffer
*b
)
1106 struct omap24xxcam_fh
*ofh
= fh
;
1108 return videobuf_querybuf(&ofh
->vbq
, b
);
1111 static int vidioc_qbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*b
)
1113 struct omap24xxcam_fh
*ofh
= fh
;
1115 return videobuf_qbuf(&ofh
->vbq
, b
);
1118 static int vidioc_dqbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*b
)
1120 struct omap24xxcam_fh
*ofh
= fh
;
1121 struct omap24xxcam_device
*cam
= ofh
->cam
;
1122 struct videobuf_buffer
*vb
;
1125 videobuf_dqbuf_again
:
1126 rval
= videobuf_dqbuf(&ofh
->vbq
, b
, file
->f_flags
& O_NONBLOCK
);
1130 vb
= ofh
->vbq
.bufs
[b
->index
];
1132 mutex_lock(&cam
->mutex
);
1133 /* _needs_reset returns -EIO if reset is required. */
1134 rval
= vidioc_int_g_needs_reset(cam
->sdev
, (void *)vb
->baddr
);
1135 mutex_unlock(&cam
->mutex
);
1137 schedule_work(&cam
->sensor_reset_work
);
1143 * This is a hack. We don't want to show -EIO to the user
1144 * space. Requeue the buffer and try again if we're not doing
1145 * this in non-blocking mode.
1148 videobuf_qbuf(&ofh
->vbq
, b
);
1149 if (!(file
->f_flags
& O_NONBLOCK
))
1150 goto videobuf_dqbuf_again
;
1152 * We don't have a videobuf_buffer now --- maybe next
1161 static int vidioc_streamon(struct file
*file
, void *fh
, enum v4l2_buf_type i
)
1163 struct omap24xxcam_fh
*ofh
= fh
;
1164 struct omap24xxcam_device
*cam
= ofh
->cam
;
1167 mutex_lock(&cam
->mutex
);
1168 if (cam
->streaming
) {
1173 rval
= omap24xxcam_sensor_if_enable(cam
);
1175 dev_dbg(cam
->dev
, "vidioc_int_g_ifparm failed\n");
1179 rval
= videobuf_streamon(&ofh
->vbq
);
1181 cam
->streaming
= file
;
1182 sysfs_notify(&cam
->dev
->kobj
, NULL
, "streaming");
1186 mutex_unlock(&cam
->mutex
);
1191 static int vidioc_streamoff(struct file
*file
, void *fh
, enum v4l2_buf_type i
)
1193 struct omap24xxcam_fh
*ofh
= fh
;
1194 struct omap24xxcam_device
*cam
= ofh
->cam
;
1195 struct videobuf_queue
*q
= &ofh
->vbq
;
1198 atomic_inc(&cam
->reset_disable
);
1200 flush_scheduled_work();
1202 rval
= videobuf_streamoff(q
);
1204 mutex_lock(&cam
->mutex
);
1205 cam
->streaming
= NULL
;
1206 mutex_unlock(&cam
->mutex
);
1207 sysfs_notify(&cam
->dev
->kobj
, NULL
, "streaming");
1210 atomic_dec(&cam
->reset_disable
);
1215 static int vidioc_enum_input(struct file
*file
, void *fh
,
1216 struct v4l2_input
*inp
)
1221 strlcpy(inp
->name
, "camera", sizeof(inp
->name
));
1222 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1227 static int vidioc_g_input(struct file
*file
, void *fh
, unsigned int *i
)
1234 static int vidioc_s_input(struct file
*file
, void *fh
, unsigned int i
)
1242 static int vidioc_queryctrl(struct file
*file
, void *fh
,
1243 struct v4l2_queryctrl
*a
)
1245 struct omap24xxcam_fh
*ofh
= fh
;
1246 struct omap24xxcam_device
*cam
= ofh
->cam
;
1249 rval
= vidioc_int_queryctrl(cam
->sdev
, a
);
1254 static int vidioc_g_ctrl(struct file
*file
, void *fh
,
1255 struct v4l2_control
*a
)
1257 struct omap24xxcam_fh
*ofh
= fh
;
1258 struct omap24xxcam_device
*cam
= ofh
->cam
;
1261 mutex_lock(&cam
->mutex
);
1262 rval
= vidioc_int_g_ctrl(cam
->sdev
, a
);
1263 mutex_unlock(&cam
->mutex
);
1268 static int vidioc_s_ctrl(struct file
*file
, void *fh
,
1269 struct v4l2_control
*a
)
1271 struct omap24xxcam_fh
*ofh
= fh
;
1272 struct omap24xxcam_device
*cam
= ofh
->cam
;
1275 mutex_lock(&cam
->mutex
);
1276 rval
= vidioc_int_s_ctrl(cam
->sdev
, a
);
1277 mutex_unlock(&cam
->mutex
);
1282 static int vidioc_g_parm(struct file
*file
, void *fh
,
1283 struct v4l2_streamparm
*a
) {
1284 struct omap24xxcam_fh
*ofh
= fh
;
1285 struct omap24xxcam_device
*cam
= ofh
->cam
;
1288 mutex_lock(&cam
->mutex
);
1289 rval
= vidioc_int_g_parm(cam
->sdev
, a
);
1290 mutex_unlock(&cam
->mutex
);
1295 static int vidioc_s_parm(struct file
*file
, void *fh
,
1296 struct v4l2_streamparm
*a
)
1298 struct omap24xxcam_fh
*ofh
= fh
;
1299 struct omap24xxcam_device
*cam
= ofh
->cam
;
1300 struct v4l2_streamparm old_streamparm
;
1303 mutex_lock(&cam
->mutex
);
1304 if (cam
->streaming
) {
1309 old_streamparm
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1310 rval
= vidioc_int_g_parm(cam
->sdev
, &old_streamparm
);
1314 rval
= vidioc_int_s_parm(cam
->sdev
, a
);
1318 rval
= omap24xxcam_sensor_if_enable(cam
);
1320 * Revert to old streaming parameters if enabling sensor
1321 * interface with the new ones failed.
1324 vidioc_int_s_parm(cam
->sdev
, &old_streamparm
);
1327 mutex_unlock(&cam
->mutex
);
1338 static unsigned int omap24xxcam_poll(struct file
*file
,
1339 struct poll_table_struct
*wait
)
1341 struct omap24xxcam_fh
*fh
= file
->private_data
;
1342 struct omap24xxcam_device
*cam
= fh
->cam
;
1343 struct videobuf_buffer
*vb
;
1345 mutex_lock(&cam
->mutex
);
1346 if (cam
->streaming
!= file
) {
1347 mutex_unlock(&cam
->mutex
);
1350 mutex_unlock(&cam
->mutex
);
1352 mutex_lock(&fh
->vbq
.vb_lock
);
1353 if (list_empty(&fh
->vbq
.stream
)) {
1354 mutex_unlock(&fh
->vbq
.vb_lock
);
1357 vb
= list_entry(fh
->vbq
.stream
.next
, struct videobuf_buffer
, stream
);
1358 mutex_unlock(&fh
->vbq
.vb_lock
);
1360 poll_wait(file
, &vb
->done
, wait
);
1362 if (vb
->state
== VIDEOBUF_DONE
|| vb
->state
== VIDEOBUF_ERROR
)
1363 return POLLIN
| POLLRDNORM
;
1368 static int omap24xxcam_mmap_buffers(struct file
*file
,
1369 struct vm_area_struct
*vma
)
1371 struct omap24xxcam_fh
*fh
= file
->private_data
;
1372 struct omap24xxcam_device
*cam
= fh
->cam
;
1373 struct videobuf_queue
*vbq
= &fh
->vbq
;
1374 unsigned int first
, last
, size
, i
, j
;
1377 mutex_lock(&cam
->mutex
);
1378 if (cam
->streaming
) {
1379 mutex_unlock(&cam
->mutex
);
1382 mutex_unlock(&cam
->mutex
);
1383 mutex_lock(&vbq
->vb_lock
);
1385 /* look for first buffer to map */
1386 for (first
= 0; first
< VIDEO_MAX_FRAME
; first
++) {
1387 if (NULL
== vbq
->bufs
[first
])
1389 if (V4L2_MEMORY_MMAP
!= vbq
->bufs
[first
]->memory
)
1391 if (vbq
->bufs
[first
]->boff
== (vma
->vm_pgoff
<< PAGE_SHIFT
))
1395 /* look for last buffer to map */
1396 for (size
= 0, last
= first
; last
< VIDEO_MAX_FRAME
; last
++) {
1397 if (NULL
== vbq
->bufs
[last
])
1399 if (V4L2_MEMORY_MMAP
!= vbq
->bufs
[last
]->memory
)
1401 size
+= vbq
->bufs
[last
]->bsize
;
1402 if (size
== (vma
->vm_end
- vma
->vm_start
))
1407 for (i
= first
; i
<= last
; i
++) {
1408 struct videobuf_dmabuf
*dma
= videobuf_to_dma(vbq
->bufs
[i
]);
1410 for (j
= 0; j
< dma
->sglen
; j
++) {
1411 err
= remap_pfn_range(
1412 vma
, vma
->vm_start
+ size
,
1413 page_to_pfn(sg_page(&dma
->sglist
[j
])),
1414 sg_dma_len(&dma
->sglist
[j
]), vma
->vm_page_prot
);
1417 size
+= sg_dma_len(&dma
->sglist
[j
]);
1422 mutex_unlock(&vbq
->vb_lock
);
1427 static int omap24xxcam_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1429 struct omap24xxcam_fh
*fh
= file
->private_data
;
1432 /* let the video-buf mapper check arguments and set-up structures */
1433 rval
= videobuf_mmap_mapper(&fh
->vbq
, vma
);
1437 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1439 /* do mapping to our allocated buffers */
1440 rval
= omap24xxcam_mmap_buffers(file
, vma
);
1442 * In case of error, free vma->vm_private_data allocated by
1443 * videobuf_mmap_mapper.
1446 kfree(vma
->vm_private_data
);
1451 static int omap24xxcam_open(struct file
*file
)
1453 struct omap24xxcam_device
*cam
= omap24xxcam
.priv
;
1454 struct omap24xxcam_fh
*fh
;
1455 struct v4l2_format format
;
1457 if (!cam
|| !cam
->vfd
)
1460 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1464 mutex_lock(&cam
->mutex
);
1465 if (cam
->sdev
== NULL
|| !try_module_get(cam
->sdev
->module
)) {
1466 mutex_unlock(&cam
->mutex
);
1467 goto out_try_module_get
;
1470 if (atomic_inc_return(&cam
->users
) == 1) {
1471 omap24xxcam_hwinit(cam
);
1472 if (omap24xxcam_sensor_enable(cam
)) {
1473 mutex_unlock(&cam
->mutex
);
1474 goto out_omap24xxcam_sensor_enable
;
1477 mutex_unlock(&cam
->mutex
);
1480 mutex_lock(&cam
->mutex
);
1481 vidioc_int_g_fmt_cap(cam
->sdev
, &format
);
1482 mutex_unlock(&cam
->mutex
);
1483 /* FIXME: how about fh->pix when there are more users? */
1484 fh
->pix
= format
.fmt
.pix
;
1486 file
->private_data
= fh
;
1488 spin_lock_init(&fh
->vbq_lock
);
1490 videobuf_queue_sg_init(&fh
->vbq
, &omap24xxcam_vbq_ops
, NULL
,
1491 &fh
->vbq_lock
, V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1493 sizeof(struct videobuf_buffer
), fh
);
1497 out_omap24xxcam_sensor_enable
:
1498 omap24xxcam_poweron_reset(cam
);
1499 module_put(cam
->sdev
->module
);
1507 static int omap24xxcam_release(struct file
*file
)
1509 struct omap24xxcam_fh
*fh
= file
->private_data
;
1510 struct omap24xxcam_device
*cam
= fh
->cam
;
1512 atomic_inc(&cam
->reset_disable
);
1514 flush_scheduled_work();
1516 /* stop streaming capture */
1517 videobuf_streamoff(&fh
->vbq
);
1519 mutex_lock(&cam
->mutex
);
1520 if (cam
->streaming
== file
) {
1521 cam
->streaming
= NULL
;
1522 mutex_unlock(&cam
->mutex
);
1523 sysfs_notify(&cam
->dev
->kobj
, NULL
, "streaming");
1525 mutex_unlock(&cam
->mutex
);
1528 atomic_dec(&cam
->reset_disable
);
1530 omap24xxcam_vbq_free_mmap_buffers(&fh
->vbq
);
1533 * Make sure the reset work we might have scheduled is not
1534 * pending! It may be run *only* if we have users. (And it may
1535 * not be scheduled anymore since streaming is already
1538 flush_scheduled_work();
1540 mutex_lock(&cam
->mutex
);
1541 if (atomic_dec_return(&cam
->users
) == 0) {
1542 omap24xxcam_sensor_disable(cam
);
1543 omap24xxcam_poweron_reset(cam
);
1545 mutex_unlock(&cam
->mutex
);
1547 file
->private_data
= NULL
;
1549 module_put(cam
->sdev
->module
);
1555 static struct v4l2_file_operations omap24xxcam_fops
= {
1556 .ioctl
= video_ioctl2
,
1557 .poll
= omap24xxcam_poll
,
1558 .mmap
= omap24xxcam_mmap
,
1559 .open
= omap24xxcam_open
,
1560 .release
= omap24xxcam_release
,
1570 static int omap24xxcam_suspend(struct platform_device
*pdev
, pm_message_t state
)
1572 struct omap24xxcam_device
*cam
= platform_get_drvdata(pdev
);
1574 if (atomic_read(&cam
->users
) == 0)
1577 if (!atomic_read(&cam
->reset_disable
))
1578 omap24xxcam_capture_stop(cam
);
1580 omap24xxcam_sensor_disable(cam
);
1581 omap24xxcam_poweron_reset(cam
);
1586 static int omap24xxcam_resume(struct platform_device
*pdev
)
1588 struct omap24xxcam_device
*cam
= platform_get_drvdata(pdev
);
1590 if (atomic_read(&cam
->users
) == 0)
1593 omap24xxcam_hwinit(cam
);
1594 omap24xxcam_sensor_enable(cam
);
1596 if (!atomic_read(&cam
->reset_disable
))
1597 omap24xxcam_capture_cont(cam
);
1601 #endif /* CONFIG_PM */
1603 static const struct v4l2_ioctl_ops omap24xxcam_ioctl_fops
= {
1604 .vidioc_querycap
= vidioc_querycap
,
1605 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1606 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1607 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1608 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1609 .vidioc_reqbufs
= vidioc_reqbufs
,
1610 .vidioc_querybuf
= vidioc_querybuf
,
1611 .vidioc_qbuf
= vidioc_qbuf
,
1612 .vidioc_dqbuf
= vidioc_dqbuf
,
1613 .vidioc_streamon
= vidioc_streamon
,
1614 .vidioc_streamoff
= vidioc_streamoff
,
1615 .vidioc_enum_input
= vidioc_enum_input
,
1616 .vidioc_g_input
= vidioc_g_input
,
1617 .vidioc_s_input
= vidioc_s_input
,
1618 .vidioc_queryctrl
= vidioc_queryctrl
,
1619 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1620 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1621 .vidioc_g_parm
= vidioc_g_parm
,
1622 .vidioc_s_parm
= vidioc_s_parm
,
1627 * Camera device (i.e. /dev/video).
1631 static int omap24xxcam_device_register(struct v4l2_int_device
*s
)
1633 struct omap24xxcam_device
*cam
= s
->u
.slave
->master
->priv
;
1634 struct video_device
*vfd
;
1637 /* We already have a slave. */
1643 if (device_create_file(cam
->dev
, &dev_attr_streaming
) != 0) {
1644 dev_err(cam
->dev
, "could not register sysfs entry\n");
1649 /* initialize the video_device struct */
1650 vfd
= cam
->vfd
= video_device_alloc();
1652 dev_err(cam
->dev
, "could not allocate video device struct\n");
1656 vfd
->release
= video_device_release
;
1658 vfd
->parent
= cam
->dev
;
1660 strlcpy(vfd
->name
, CAM_NAME
, sizeof(vfd
->name
));
1661 vfd
->fops
= &omap24xxcam_fops
;
1662 vfd
->ioctl_ops
= &omap24xxcam_ioctl_fops
;
1664 omap24xxcam_hwinit(cam
);
1666 rval
= omap24xxcam_sensor_init(cam
);
1670 if (video_register_device(vfd
, VFL_TYPE_GRABBER
, video_nr
) < 0) {
1671 dev_err(cam
->dev
, "could not register V4L device\n");
1676 omap24xxcam_poweron_reset(cam
);
1678 dev_info(cam
->dev
, "registered device %s\n",
1679 video_device_node_name(vfd
));
1684 omap24xxcam_device_unregister(s
);
1689 static void omap24xxcam_device_unregister(struct v4l2_int_device
*s
)
1691 struct omap24xxcam_device
*cam
= s
->u
.slave
->master
->priv
;
1693 omap24xxcam_sensor_exit(cam
);
1696 if (!video_is_registered(cam
->vfd
)) {
1698 * The device was never registered, so release the
1699 * video_device struct directly.
1701 video_device_release(cam
->vfd
);
1704 * The unregister function will release the
1705 * video_device struct as well as
1708 video_unregister_device(cam
->vfd
);
1713 device_remove_file(cam
->dev
, &dev_attr_streaming
);
1718 static struct v4l2_int_master omap24xxcam_master
= {
1719 .attach
= omap24xxcam_device_register
,
1720 .detach
= omap24xxcam_device_unregister
,
1723 static struct v4l2_int_device omap24xxcam
= {
1724 .module
= THIS_MODULE
,
1726 .type
= v4l2_int_type_master
,
1728 .master
= &omap24xxcam_master
1734 * Driver initialisation and deinitialisation.
1738 static int __devinit
omap24xxcam_probe(struct platform_device
*pdev
)
1740 struct omap24xxcam_device
*cam
;
1741 struct resource
*mem
;
1744 cam
= kzalloc(sizeof(*cam
), GFP_KERNEL
);
1746 dev_err(&pdev
->dev
, "could not allocate memory\n");
1750 platform_set_drvdata(pdev
, cam
);
1752 cam
->dev
= &pdev
->dev
;
1755 * Impose a lower limit on the amount of memory allocated for
1756 * capture. We require at least enough memory to double-buffer
1759 if (capture_mem
< 320 * 240 * 2 * 2)
1760 capture_mem
= 320 * 240 * 2 * 2;
1761 cam
->capture_mem
= capture_mem
;
1763 /* request the mem region for the camera registers */
1764 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1766 dev_err(cam
->dev
, "no mem resource?\n");
1769 if (!request_mem_region(mem
->start
, (mem
->end
- mem
->start
) + 1,
1772 "cannot reserve camera register I/O region\n");
1775 cam
->mmio_base_phys
= mem
->start
;
1776 cam
->mmio_size
= (mem
->end
- mem
->start
) + 1;
1778 /* map the region */
1779 cam
->mmio_base
= (unsigned long)
1780 ioremap_nocache(cam
->mmio_base_phys
, cam
->mmio_size
);
1781 if (!cam
->mmio_base
) {
1782 dev_err(cam
->dev
, "cannot map camera register I/O region\n");
1786 irq
= platform_get_irq(pdev
, 0);
1788 dev_err(cam
->dev
, "no irq for camera?\n");
1792 /* install the interrupt service routine */
1793 if (request_irq(irq
, omap24xxcam_isr
, 0, CAM_NAME
, cam
)) {
1795 "could not install interrupt service routine\n");
1800 if (omap24xxcam_clock_get(cam
))
1803 INIT_WORK(&cam
->sensor_reset_work
, omap24xxcam_sensor_reset_work
);
1805 mutex_init(&cam
->mutex
);
1806 spin_lock_init(&cam
->core_enable_disable_lock
);
1808 omap24xxcam_sgdma_init(&cam
->sgdma
,
1809 cam
->mmio_base
+ CAMDMA_REG_OFFSET
,
1810 omap24xxcam_stalled_dma_reset
,
1811 (unsigned long)cam
);
1813 omap24xxcam
.priv
= cam
;
1815 if (v4l2_int_device_register(&omap24xxcam
))
1821 omap24xxcam_remove(pdev
);
1825 static int omap24xxcam_remove(struct platform_device
*pdev
)
1827 struct omap24xxcam_device
*cam
= platform_get_drvdata(pdev
);
1832 if (omap24xxcam
.priv
!= NULL
)
1833 v4l2_int_device_unregister(&omap24xxcam
);
1834 omap24xxcam
.priv
= NULL
;
1836 omap24xxcam_clock_put(cam
);
1839 free_irq(cam
->irq
, cam
);
1843 if (cam
->mmio_base
) {
1844 iounmap((void *)cam
->mmio_base
);
1848 if (cam
->mmio_base_phys
) {
1849 release_mem_region(cam
->mmio_base_phys
, cam
->mmio_size
);
1850 cam
->mmio_base_phys
= 0;
1858 static struct platform_driver omap24xxcam_driver
= {
1859 .probe
= omap24xxcam_probe
,
1860 .remove
= omap24xxcam_remove
,
1862 .suspend
= omap24xxcam_suspend
,
1863 .resume
= omap24xxcam_resume
,
1867 .owner
= THIS_MODULE
,
1873 * Module initialisation and deinitialisation
1877 static int __init
omap24xxcam_init(void)
1879 return platform_driver_register(&omap24xxcam_driver
);
1882 static void __exit
omap24xxcam_cleanup(void)
1884 platform_driver_unregister(&omap24xxcam_driver
);
1887 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
1888 MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
1889 MODULE_LICENSE("GPL");
1890 module_param(video_nr
, int, 0);
1891 MODULE_PARM_DESC(video_nr
,
1892 "Minor number for video device (-1 ==> auto assign)");
1893 module_param(capture_mem
, int, 0);
1894 MODULE_PARM_DESC(capture_mem
, "Maximum amount of memory for capture "
1895 "buffers (default 4800kiB)");
1897 module_init(omap24xxcam_init
);
1898 module_exit(omap24xxcam_cleanup
);