PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / media / video / omap24xxcam.c
blobdfd34796d0493345101b385e065c3e978d842a01
1 /*
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 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
26 * 02110-1301 USA
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>
37 #include <linux/io.h>
39 #include <media/v4l2-common.h>
41 #include "omap24xxcam.h"
43 #define OMAP24XXCAM_VERSION KERNEL_VERSION(0, 0, 0)
45 #define RESET_TIMEOUT_NS 10000
47 static void omap24xxcam_reset(struct omap24xxcam_device *cam);
48 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam);
49 static void omap24xxcam_device_unregister(struct v4l2_int_device *s);
50 static int omap24xxcam_remove(struct platform_device *pdev);
52 /* module parameters */
53 static int video_nr = -1; /* video device minor (-1 ==> auto assign) */
55 * Maximum amount of memory to use for capture buffers.
56 * Default is 4800KB, enough to double-buffer SXGA.
58 static int capture_mem = 1280 * 960 * 2 * 2;
60 static struct v4l2_int_device omap24xxcam;
64 * Clocks.
68 static void omap24xxcam_clock_put(struct omap24xxcam_device *cam)
70 if (cam->ick != NULL && !IS_ERR(cam->ick))
71 clk_put(cam->ick);
72 if (cam->fck != NULL && !IS_ERR(cam->fck))
73 clk_put(cam->fck);
75 cam->ick = cam->fck = NULL;
78 static int omap24xxcam_clock_get(struct omap24xxcam_device *cam)
80 int rval = 0;
82 cam->fck = clk_get(cam->dev, "cam_fck");
83 if (IS_ERR(cam->fck)) {
84 dev_err(cam->dev, "can't get cam_fck");
85 rval = PTR_ERR(cam->fck);
86 omap24xxcam_clock_put(cam);
87 return rval;
90 cam->ick = clk_get(cam->dev, "cam_ick");
91 if (IS_ERR(cam->ick)) {
92 dev_err(cam->dev, "can't get cam_ick");
93 rval = PTR_ERR(cam->ick);
94 omap24xxcam_clock_put(cam);
97 return rval;
100 static void omap24xxcam_clock_on(struct omap24xxcam_device *cam)
102 clk_enable(cam->fck);
103 clk_enable(cam->ick);
106 static void omap24xxcam_clock_off(struct omap24xxcam_device *cam)
108 clk_disable(cam->fck);
109 clk_disable(cam->ick);
114 * Camera core
119 * Set xclk.
121 * To disable xclk, use value zero.
123 static void omap24xxcam_core_xclk_set(const struct omap24xxcam_device *cam,
124 u32 xclk)
126 if (xclk) {
127 u32 divisor = CAM_MCLK / xclk;
129 if (divisor == 1)
130 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
131 CC_CTRL_XCLK,
132 CC_CTRL_XCLK_DIV_BYPASS);
133 else
134 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
135 CC_CTRL_XCLK, divisor);
136 } else
137 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
138 CC_CTRL_XCLK, CC_CTRL_XCLK_DIV_STABLE_LOW);
141 static void omap24xxcam_core_hwinit(const struct omap24xxcam_device *cam)
144 * Setting the camera core AUTOIDLE bit causes problems with frame
145 * synchronization, so we will clear the AUTOIDLE bit instead.
147 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_SYSCONFIG,
148 CC_SYSCONFIG_AUTOIDLE);
150 /* program the camera interface DMA packet size */
151 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL_DMA,
152 CC_CTRL_DMA_EN | (DMA_THRESHOLD / 4 - 1));
154 /* enable camera core error interrupts */
155 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQENABLE,
156 CC_IRQENABLE_FW_ERR_IRQ
157 | CC_IRQENABLE_FSC_ERR_IRQ
158 | CC_IRQENABLE_SSC_ERR_IRQ
159 | CC_IRQENABLE_FIFO_OF_IRQ);
163 * Enable the camera core.
165 * Data transfer to the camera DMA starts from next starting frame.
167 static void omap24xxcam_core_enable(const struct omap24xxcam_device *cam)
170 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
171 cam->cc_ctrl);
175 * Disable camera core.
177 * The data transfer will be stopped immediately (CC_CTRL_CC_RST). The
178 * core internal state machines will be reset. Use
179 * CC_CTRL_CC_FRAME_TRIG instead if you want to transfer the current
180 * frame completely.
182 static void omap24xxcam_core_disable(const struct omap24xxcam_device *cam)
184 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
185 CC_CTRL_CC_RST);
188 /* Interrupt service routine for camera core interrupts. */
189 static void omap24xxcam_core_isr(struct omap24xxcam_device *cam)
191 u32 cc_irqstatus;
192 const u32 cc_irqstatus_err =
193 CC_IRQSTATUS_FW_ERR_IRQ
194 | CC_IRQSTATUS_FSC_ERR_IRQ
195 | CC_IRQSTATUS_SSC_ERR_IRQ
196 | CC_IRQSTATUS_FIFO_UF_IRQ
197 | CC_IRQSTATUS_FIFO_OF_IRQ;
199 cc_irqstatus = omap24xxcam_reg_in(cam->mmio_base + CC_REG_OFFSET,
200 CC_IRQSTATUS);
201 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQSTATUS,
202 cc_irqstatus);
204 if (cc_irqstatus & cc_irqstatus_err
205 && !atomic_read(&cam->in_reset)) {
206 dev_dbg(cam->dev, "resetting camera, cc_irqstatus 0x%x\n",
207 cc_irqstatus);
208 omap24xxcam_reset(cam);
214 * videobuf_buffer handling.
216 * Memory for mmapped videobuf_buffers is not allocated
217 * conventionally, but by several kmalloc allocations and then
218 * creating the scatterlist on our own. User-space buffers are handled
219 * normally.
224 * Free the memory-mapped buffer memory allocated for a
225 * videobuf_buffer and the associated scatterlist.
227 static void omap24xxcam_vbq_free_mmap_buffer(struct videobuf_buffer *vb)
229 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
230 size_t alloc_size;
231 struct page *page;
232 int i;
234 if (dma->sglist == NULL)
235 return;
237 i = dma->sglen;
238 while (i) {
239 i--;
240 alloc_size = sg_dma_len(&dma->sglist[i]);
241 page = sg_page(&dma->sglist[i]);
242 do {
243 ClearPageReserved(page++);
244 } while (alloc_size -= PAGE_SIZE);
245 __free_pages(sg_page(&dma->sglist[i]),
246 get_order(sg_dma_len(&dma->sglist[i])));
249 kfree(dma->sglist);
250 dma->sglist = NULL;
253 /* Release all memory related to the videobuf_queue. */
254 static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue *vbq)
256 int i;
258 mutex_lock(&vbq->vb_lock);
260 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
261 if (NULL == vbq->bufs[i])
262 continue;
263 if (V4L2_MEMORY_MMAP != vbq->bufs[i]->memory)
264 continue;
265 vbq->ops->buf_release(vbq, vbq->bufs[i]);
266 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
267 kfree(vbq->bufs[i]);
268 vbq->bufs[i] = NULL;
271 mutex_unlock(&vbq->vb_lock);
273 videobuf_mmap_free(vbq);
277 * Allocate physically as contiguous as possible buffer for video
278 * frame and allocate and build DMA scatter-gather list for it.
280 static int omap24xxcam_vbq_alloc_mmap_buffer(struct videobuf_buffer *vb)
282 unsigned int order;
283 size_t alloc_size, size = vb->bsize; /* vb->bsize is page aligned */
284 struct page *page;
285 int max_pages, err = 0, i = 0;
286 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
289 * allocate maximum size scatter-gather list. Note this is
290 * overhead. We may not use as many entries as we allocate
292 max_pages = vb->bsize >> PAGE_SHIFT;
293 dma->sglist = kcalloc(max_pages, sizeof(*dma->sglist), GFP_KERNEL);
294 if (dma->sglist == NULL) {
295 err = -ENOMEM;
296 goto out;
299 while (size) {
300 order = get_order(size);
302 * do not over-allocate even if we would get larger
303 * contiguous chunk that way
305 if ((PAGE_SIZE << order) > size)
306 order--;
308 /* try to allocate as many contiguous pages as possible */
309 page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
310 /* if allocation fails, try to allocate smaller amount */
311 while (page == NULL) {
312 order--;
313 page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
314 if (page == NULL && !order) {
315 err = -ENOMEM;
316 goto out;
319 size -= (PAGE_SIZE << order);
321 /* append allocated chunk of pages into scatter-gather list */
322 sg_set_page(&dma->sglist[i], page, PAGE_SIZE << order, 0);
323 dma->sglen++;
324 i++;
326 alloc_size = (PAGE_SIZE << order);
328 /* clear pages before giving them to user space */
329 memset(page_address(page), 0, alloc_size);
331 /* mark allocated pages reserved */
332 do {
333 SetPageReserved(page++);
334 } while (alloc_size -= PAGE_SIZE);
337 * REVISIT: not fully correct to assign nr_pages == sglen but
338 * video-buf is passing nr_pages for e.g. unmap_sg calls
340 dma->nr_pages = dma->sglen;
341 dma->direction = PCI_DMA_FROMDEVICE;
343 return 0;
345 out:
346 omap24xxcam_vbq_free_mmap_buffer(vb);
347 return err;
350 static int omap24xxcam_vbq_alloc_mmap_buffers(struct videobuf_queue *vbq,
351 unsigned int count)
353 int i, err = 0;
354 struct omap24xxcam_fh *fh =
355 container_of(vbq, struct omap24xxcam_fh, vbq);
357 mutex_lock(&vbq->vb_lock);
359 for (i = 0; i < count; i++) {
360 err = omap24xxcam_vbq_alloc_mmap_buffer(vbq->bufs[i]);
361 if (err)
362 goto out;
363 dev_dbg(fh->cam->dev, "sglen is %d for buffer %d\n",
364 videobuf_to_dma(vbq->bufs[i])->sglen, i);
367 mutex_unlock(&vbq->vb_lock);
369 return 0;
370 out:
371 while (i) {
372 i--;
373 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
376 mutex_unlock(&vbq->vb_lock);
378 return err;
382 * This routine is called from interrupt context when a scatter-gather DMA
383 * transfer of a videobuf_buffer completes.
385 static void omap24xxcam_vbq_complete(struct omap24xxcam_sgdma *sgdma,
386 u32 csr, void *arg)
388 struct omap24xxcam_device *cam =
389 container_of(sgdma, struct omap24xxcam_device, sgdma);
390 struct omap24xxcam_fh *fh = cam->streaming->private_data;
391 struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
392 const u32 csr_error = CAMDMA_CSR_MISALIGNED_ERR
393 | CAMDMA_CSR_SUPERVISOR_ERR | CAMDMA_CSR_SECURE_ERR
394 | CAMDMA_CSR_TRANS_ERR | CAMDMA_CSR_DROP;
395 unsigned long flags;
397 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
398 if (--cam->sgdma_in_queue == 0)
399 omap24xxcam_core_disable(cam);
400 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
402 do_gettimeofday(&vb->ts);
403 vb->field_count = atomic_add_return(2, &fh->field_count);
404 if (csr & csr_error) {
405 vb->state = VIDEOBUF_ERROR;
406 if (!atomic_read(&fh->cam->in_reset)) {
407 dev_dbg(cam->dev, "resetting camera, csr 0x%x\n", csr);
408 omap24xxcam_reset(cam);
410 } else
411 vb->state = VIDEOBUF_DONE;
412 wake_up(&vb->done);
415 static void omap24xxcam_vbq_release(struct videobuf_queue *vbq,
416 struct videobuf_buffer *vb)
418 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
420 /* wait for buffer, especially to get out of the sgdma queue */
421 videobuf_waiton(vb, 0, 0);
422 if (vb->memory == V4L2_MEMORY_MMAP) {
423 dma_unmap_sg(vbq->dev, dma->sglist, dma->sglen,
424 dma->direction);
425 dma->direction = DMA_NONE;
426 } else {
427 videobuf_dma_unmap(vbq, videobuf_to_dma(vb));
428 videobuf_dma_free(videobuf_to_dma(vb));
431 vb->state = VIDEOBUF_NEEDS_INIT;
435 * Limit the number of available kernel image capture buffers based on the
436 * number requested, the currently selected image size, and the maximum
437 * amount of memory permitted for kernel capture buffers.
439 static int omap24xxcam_vbq_setup(struct videobuf_queue *vbq, unsigned int *cnt,
440 unsigned int *size)
442 struct omap24xxcam_fh *fh = vbq->priv_data;
444 if (*cnt <= 0)
445 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
447 if (*cnt > VIDEO_MAX_FRAME)
448 *cnt = VIDEO_MAX_FRAME;
450 *size = fh->pix.sizeimage;
452 /* accessing fh->cam->capture_mem is ok, it's constant */
453 while (*size * *cnt > fh->cam->capture_mem)
454 (*cnt)--;
456 return 0;
459 static int omap24xxcam_dma_iolock(struct videobuf_queue *vbq,
460 struct videobuf_dmabuf *dma)
462 int err = 0;
464 dma->direction = PCI_DMA_FROMDEVICE;
465 if (!dma_map_sg(vbq->dev, dma->sglist, dma->sglen, dma->direction)) {
466 kfree(dma->sglist);
467 dma->sglist = NULL;
468 dma->sglen = 0;
469 err = -EIO;
472 return err;
475 static int omap24xxcam_vbq_prepare(struct videobuf_queue *vbq,
476 struct videobuf_buffer *vb,
477 enum v4l2_field field)
479 struct omap24xxcam_fh *fh = vbq->priv_data;
480 int err = 0;
483 * Accessing pix here is okay since it's constant while
484 * streaming is on (and we only get called then).
486 if (vb->baddr) {
487 /* This is a userspace buffer. */
488 if (fh->pix.sizeimage > vb->bsize) {
489 /* The buffer isn't big enough. */
490 err = -EINVAL;
491 } else
492 vb->size = fh->pix.sizeimage;
493 } else {
494 if (vb->state != VIDEOBUF_NEEDS_INIT) {
496 * We have a kernel bounce buffer that has
497 * already been allocated.
499 if (fh->pix.sizeimage > vb->size) {
501 * The image size has been changed to
502 * a larger size since this buffer was
503 * allocated, so we need to free and
504 * reallocate it.
506 omap24xxcam_vbq_release(vbq, vb);
507 vb->size = fh->pix.sizeimage;
509 } else {
510 /* We need to allocate a new kernel bounce buffer. */
511 vb->size = fh->pix.sizeimage;
515 if (err)
516 return err;
518 vb->width = fh->pix.width;
519 vb->height = fh->pix.height;
520 vb->field = field;
522 if (vb->state == VIDEOBUF_NEEDS_INIT) {
523 if (vb->memory == V4L2_MEMORY_MMAP)
525 * we have built the scatter-gather list by ourself so
526 * do the scatter-gather mapping as well
528 err = omap24xxcam_dma_iolock(vbq, videobuf_to_dma(vb));
529 else
530 err = videobuf_iolock(vbq, vb, NULL);
533 if (!err)
534 vb->state = VIDEOBUF_PREPARED;
535 else
536 omap24xxcam_vbq_release(vbq, vb);
538 return err;
541 static void omap24xxcam_vbq_queue(struct videobuf_queue *vbq,
542 struct videobuf_buffer *vb)
544 struct omap24xxcam_fh *fh = vbq->priv_data;
545 struct omap24xxcam_device *cam = fh->cam;
546 enum videobuf_state state = vb->state;
547 unsigned long flags;
548 int err;
551 * FIXME: We're marking the buffer active since we have no
552 * pretty way of marking it active exactly when the
553 * scatter-gather transfer starts.
555 vb->state = VIDEOBUF_ACTIVE;
557 err = omap24xxcam_sgdma_queue(&fh->cam->sgdma,
558 videobuf_to_dma(vb)->sglist,
559 videobuf_to_dma(vb)->sglen, vb->size,
560 omap24xxcam_vbq_complete, vb);
562 if (!err) {
563 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
564 if (++cam->sgdma_in_queue == 1
565 && !atomic_read(&cam->in_reset))
566 omap24xxcam_core_enable(cam);
567 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
568 } else {
570 * Oops. We're not supposed to get any errors here.
571 * The only way we could get an error is if we ran out
572 * of scatter-gather DMA slots, but we are supposed to
573 * have at least as many scatter-gather DMA slots as
574 * video buffers so that can't happen.
576 dev_err(cam->dev, "failed to queue a video buffer for dma!\n");
577 dev_err(cam->dev, "likely a bug in the driver!\n");
578 vb->state = state;
582 static struct videobuf_queue_ops omap24xxcam_vbq_ops = {
583 .buf_setup = omap24xxcam_vbq_setup,
584 .buf_prepare = omap24xxcam_vbq_prepare,
585 .buf_queue = omap24xxcam_vbq_queue,
586 .buf_release = omap24xxcam_vbq_release,
591 * OMAP main camera system
596 * Reset camera block to power-on state.
598 static void omap24xxcam_poweron_reset(const struct omap24xxcam_device *cam)
600 int max_loop = RESET_TIMEOUT_NS;
602 /* Reset whole camera subsystem */
603 omap24xxcam_reg_out(cam->mmio_base,
604 CAM_SYSCONFIG,
605 CAM_SYSCONFIG_SOFTRESET);
607 /* Wait till it's finished */
608 while (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
609 & CAM_SYSSTATUS_RESETDONE)
610 && --max_loop) {
611 ndelay(1);
614 if (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
615 & CAM_SYSSTATUS_RESETDONE))
616 dev_err(cam->dev, "camera soft reset timeout\n");
620 * (Re)initialise the camera block.
622 static void omap24xxcam_hwinit(const struct omap24xxcam_device *cam)
624 omap24xxcam_poweron_reset(cam);
626 /* set the camera subsystem autoidle bit */
627 omap24xxcam_reg_out(cam->mmio_base, CAM_SYSCONFIG,
628 CAM_SYSCONFIG_AUTOIDLE);
630 /* set the camera MMU autoidle bit */
631 omap24xxcam_reg_out(cam->mmio_base,
632 CAMMMU_REG_OFFSET + CAMMMU_SYSCONFIG,
633 CAMMMU_SYSCONFIG_AUTOIDLE);
635 omap24xxcam_core_hwinit(cam);
637 omap24xxcam_dma_hwinit(&cam->sgdma.dma);
641 * Callback for dma transfer stalling.
643 static void omap24xxcam_stalled_dma_reset(unsigned long data)
645 struct omap24xxcam_device *cam = (struct omap24xxcam_device *)data;
647 if (!atomic_read(&cam->in_reset)) {
648 dev_dbg(cam->dev, "dma stalled, resetting camera\n");
649 omap24xxcam_reset(cam);
654 * Stop capture. Mark we're doing a reset, stop DMA transfers and
655 * core. (No new scatter-gather transfers will be queued whilst
656 * in_reset is non-zero.)
658 * If omap24xxcam_capture_stop is called from several places at
659 * once, only the first call will have an effect. Similarly, the last
660 * call omap24xxcam_streaming_cont will have effect.
662 * Serialisation is ensured by using cam->core_enable_disable_lock.
664 static void omap24xxcam_capture_stop(struct omap24xxcam_device *cam)
666 unsigned long flags;
668 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
670 if (atomic_inc_return(&cam->in_reset) != 1) {
671 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
672 return;
675 omap24xxcam_core_disable(cam);
677 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
679 omap24xxcam_sgdma_sync(&cam->sgdma);
683 * Reset and continue streaming.
685 * Note: Resetting the camera FIFO via the CC_RST bit in the CC_CTRL
686 * register is supposed to be sufficient to recover from a camera
687 * interface error, but it doesn't seem to be enough. If we only do
688 * that then subsequent image captures are out of sync by either one
689 * or two times DMA_THRESHOLD bytes. Resetting and re-initializing the
690 * entire camera subsystem prevents the problem with frame
691 * synchronization.
693 static void omap24xxcam_capture_cont(struct omap24xxcam_device *cam)
695 unsigned long flags;
697 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
699 if (atomic_read(&cam->in_reset) != 1)
700 goto out;
702 omap24xxcam_hwinit(cam);
704 omap24xxcam_sensor_if_enable(cam);
706 omap24xxcam_sgdma_process(&cam->sgdma);
708 if (cam->sgdma_in_queue)
709 omap24xxcam_core_enable(cam);
711 out:
712 atomic_dec(&cam->in_reset);
713 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
716 static ssize_t
717 omap24xxcam_streaming_show(struct device *dev, struct device_attribute *attr,
718 char *buf)
720 struct omap24xxcam_device *cam = dev_get_drvdata(dev);
722 return sprintf(buf, "%s\n", cam->streaming ? "active" : "inactive");
724 static DEVICE_ATTR(streaming, S_IRUGO, omap24xxcam_streaming_show, NULL);
727 * Stop capture and restart it. I.e. reset the camera during use.
729 static void omap24xxcam_reset(struct omap24xxcam_device *cam)
731 omap24xxcam_capture_stop(cam);
732 omap24xxcam_capture_cont(cam);
736 * The main interrupt handler.
738 static irqreturn_t omap24xxcam_isr(int irq, void *arg)
740 struct omap24xxcam_device *cam = (struct omap24xxcam_device *)arg;
741 u32 irqstatus;
742 unsigned int irqhandled = 0;
744 irqstatus = omap24xxcam_reg_in(cam->mmio_base, CAM_IRQSTATUS);
746 if (irqstatus &
747 (CAM_IRQSTATUS_DMA_IRQ2 | CAM_IRQSTATUS_DMA_IRQ1
748 | CAM_IRQSTATUS_DMA_IRQ0)) {
749 omap24xxcam_dma_isr(&cam->sgdma.dma);
750 irqhandled = 1;
752 if (irqstatus & CAM_IRQSTATUS_CC_IRQ) {
753 omap24xxcam_core_isr(cam);
754 irqhandled = 1;
756 if (irqstatus & CAM_IRQSTATUS_MMU_IRQ)
757 dev_err(cam->dev, "unhandled camera MMU interrupt!\n");
759 return IRQ_RETVAL(irqhandled);
764 * Sensor handling.
769 * Enable the external sensor interface. Try to negotiate interface
770 * parameters with the sensor and start using the new ones. The calls
771 * to sensor_if_enable and sensor_if_disable need not to be balanced.
773 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam)
775 int rval;
776 struct v4l2_ifparm p;
778 rval = vidioc_int_g_ifparm(cam->sdev, &p);
779 if (rval) {
780 dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
781 return rval;
784 cam->if_type = p.if_type;
786 cam->cc_ctrl = CC_CTRL_CC_EN;
788 switch (p.if_type) {
789 case V4L2_IF_TYPE_BT656:
790 if (p.u.bt656.frame_start_on_rising_vs)
791 cam->cc_ctrl |= CC_CTRL_NOBT_SYNCHRO;
792 if (p.u.bt656.bt_sync_correct)
793 cam->cc_ctrl |= CC_CTRL_BT_CORRECT;
794 if (p.u.bt656.swap)
795 cam->cc_ctrl |= CC_CTRL_PAR_ORDERCAM;
796 if (p.u.bt656.latch_clk_inv)
797 cam->cc_ctrl |= CC_CTRL_PAR_CLK_POL;
798 if (p.u.bt656.nobt_hs_inv)
799 cam->cc_ctrl |= CC_CTRL_NOBT_HS_POL;
800 if (p.u.bt656.nobt_vs_inv)
801 cam->cc_ctrl |= CC_CTRL_NOBT_VS_POL;
803 switch (p.u.bt656.mode) {
804 case V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT:
805 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT8;
806 break;
807 case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT:
808 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT10;
809 break;
810 case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT:
811 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT12;
812 break;
813 case V4L2_IF_TYPE_BT656_MODE_BT_8BIT:
814 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT8;
815 break;
816 case V4L2_IF_TYPE_BT656_MODE_BT_10BIT:
817 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT10;
818 break;
819 default:
820 dev_err(cam->dev,
821 "bt656 interface mode %d not supported\n",
822 p.u.bt656.mode);
823 return -EINVAL;
826 * The clock rate that the sensor wants has changed.
827 * We have to adjust the xclk from OMAP 2 side to
828 * match the sensor's wish as closely as possible.
830 if (p.u.bt656.clock_curr != cam->if_u.bt656.xclk) {
831 u32 xclk = p.u.bt656.clock_curr;
832 u32 divisor;
834 if (xclk == 0)
835 return -EINVAL;
837 if (xclk > CAM_MCLK)
838 xclk = CAM_MCLK;
840 divisor = CAM_MCLK / xclk;
841 if (divisor * xclk < CAM_MCLK)
842 divisor++;
843 if (CAM_MCLK / divisor < p.u.bt656.clock_min
844 && divisor > 1)
845 divisor--;
846 if (divisor > 30)
847 divisor = 30;
849 xclk = CAM_MCLK / divisor;
851 if (xclk < p.u.bt656.clock_min
852 || xclk > p.u.bt656.clock_max)
853 return -EINVAL;
855 cam->if_u.bt656.xclk = xclk;
857 omap24xxcam_core_xclk_set(cam, cam->if_u.bt656.xclk);
858 break;
859 default:
860 /* FIXME: how about other interfaces? */
861 dev_err(cam->dev, "interface type %d not supported\n",
862 p.if_type);
863 return -EINVAL;
866 return 0;
869 static void omap24xxcam_sensor_if_disable(const struct omap24xxcam_device *cam)
871 switch (cam->if_type) {
872 case V4L2_IF_TYPE_BT656:
873 omap24xxcam_core_xclk_set(cam, 0);
874 break;
879 * Initialise the sensor hardware.
881 static int omap24xxcam_sensor_init(struct omap24xxcam_device *cam)
883 int err = 0;
884 struct v4l2_int_device *sdev = cam->sdev;
886 omap24xxcam_clock_on(cam);
887 err = omap24xxcam_sensor_if_enable(cam);
888 if (err) {
889 dev_err(cam->dev, "sensor interface could not be enabled at "
890 "initialisation, %d\n", err);
891 cam->sdev = NULL;
892 goto out;
895 /* power up sensor during sensor initialization */
896 vidioc_int_s_power(sdev, 1);
898 err = vidioc_int_dev_init(sdev);
899 if (err) {
900 dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
901 /* Sensor init failed --- it's nonexistent to us! */
902 cam->sdev = NULL;
903 goto out;
906 dev_info(cam->dev, "sensor is %s\n", sdev->name);
908 out:
909 omap24xxcam_sensor_if_disable(cam);
910 omap24xxcam_clock_off(cam);
912 vidioc_int_s_power(sdev, 0);
914 return err;
917 static void omap24xxcam_sensor_exit(struct omap24xxcam_device *cam)
919 if (cam->sdev)
920 vidioc_int_dev_exit(cam->sdev);
923 static void omap24xxcam_sensor_disable(struct omap24xxcam_device *cam)
925 omap24xxcam_sensor_if_disable(cam);
926 omap24xxcam_clock_off(cam);
927 vidioc_int_s_power(cam->sdev, 0);
931 * Power-up and configure camera sensor. It's ready for capturing now.
933 static int omap24xxcam_sensor_enable(struct omap24xxcam_device *cam)
935 int rval;
937 omap24xxcam_clock_on(cam);
939 omap24xxcam_sensor_if_enable(cam);
941 rval = vidioc_int_s_power(cam->sdev, 1);
942 if (rval)
943 goto out;
945 rval = vidioc_int_init(cam->sdev);
946 if (rval)
947 goto out;
949 return 0;
951 out:
952 omap24xxcam_sensor_disable(cam);
954 return rval;
957 static void omap24xxcam_sensor_reset_work(struct work_struct *work)
959 struct omap24xxcam_device *cam =
960 container_of(work, struct omap24xxcam_device,
961 sensor_reset_work);
963 if (atomic_read(&cam->reset_disable))
964 return;
966 omap24xxcam_capture_stop(cam);
968 if (vidioc_int_reset(cam->sdev) == 0) {
969 vidioc_int_init(cam->sdev);
970 } else {
971 /* Can't reset it by vidioc_int_reset. */
972 omap24xxcam_sensor_disable(cam);
973 omap24xxcam_sensor_enable(cam);
976 omap24xxcam_capture_cont(cam);
981 * IOCTL interface.
985 static int vidioc_querycap(struct file *file, void *fh,
986 struct v4l2_capability *cap)
988 struct omap24xxcam_fh *ofh = fh;
989 struct omap24xxcam_device *cam = ofh->cam;
991 strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
992 strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
993 cap->version = OMAP24XXCAM_VERSION;
994 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
996 return 0;
999 static int vidioc_enum_fmt_cap(struct file *file, void *fh,
1000 struct v4l2_fmtdesc *f)
1002 struct omap24xxcam_fh *ofh = fh;
1003 struct omap24xxcam_device *cam = ofh->cam;
1004 int rval;
1006 rval = vidioc_int_enum_fmt_cap(cam->sdev, f);
1008 return rval;
1011 static int vidioc_g_fmt_cap(struct file *file, void *fh,
1012 struct v4l2_format *f)
1014 struct omap24xxcam_fh *ofh = fh;
1015 struct omap24xxcam_device *cam = ofh->cam;
1016 int rval;
1018 mutex_lock(&cam->mutex);
1019 rval = vidioc_int_g_fmt_cap(cam->sdev, f);
1020 mutex_unlock(&cam->mutex);
1022 return rval;
1025 static int vidioc_s_fmt_cap(struct file *file, void *fh,
1026 struct v4l2_format *f)
1028 struct omap24xxcam_fh *ofh = fh;
1029 struct omap24xxcam_device *cam = ofh->cam;
1030 int rval;
1032 mutex_lock(&cam->mutex);
1033 if (cam->streaming) {
1034 rval = -EBUSY;
1035 goto out;
1038 rval = vidioc_int_s_fmt_cap(cam->sdev, f);
1040 out:
1041 mutex_unlock(&cam->mutex);
1043 if (!rval) {
1044 mutex_lock(&ofh->vbq.vb_lock);
1045 ofh->pix = f->fmt.pix;
1046 mutex_unlock(&ofh->vbq.vb_lock);
1049 memset(f, 0, sizeof(*f));
1050 vidioc_g_fmt_cap(file, fh, f);
1052 return rval;
1055 static int vidioc_try_fmt_cap(struct file *file, void *fh,
1056 struct v4l2_format *f)
1058 struct omap24xxcam_fh *ofh = fh;
1059 struct omap24xxcam_device *cam = ofh->cam;
1060 int rval;
1062 mutex_lock(&cam->mutex);
1063 rval = vidioc_int_try_fmt_cap(cam->sdev, f);
1064 mutex_unlock(&cam->mutex);
1066 return rval;
1069 static int vidioc_reqbufs(struct file *file, void *fh,
1070 struct v4l2_requestbuffers *b)
1072 struct omap24xxcam_fh *ofh = fh;
1073 struct omap24xxcam_device *cam = ofh->cam;
1074 int rval;
1076 mutex_lock(&cam->mutex);
1077 if (cam->streaming) {
1078 mutex_unlock(&cam->mutex);
1079 return -EBUSY;
1082 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1083 mutex_unlock(&cam->mutex);
1085 rval = videobuf_reqbufs(&ofh->vbq, b);
1088 * Either videobuf_reqbufs failed or the buffers are not
1089 * memory-mapped (which would need special attention).
1091 if (rval < 0 || b->memory != V4L2_MEMORY_MMAP)
1092 goto out;
1094 rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, rval);
1095 if (rval)
1096 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1098 out:
1099 return rval;
1102 static int vidioc_querybuf(struct file *file, void *fh,
1103 struct v4l2_buffer *b)
1105 struct omap24xxcam_fh *ofh = fh;
1107 return videobuf_querybuf(&ofh->vbq, b);
1110 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1112 struct omap24xxcam_fh *ofh = fh;
1114 return videobuf_qbuf(&ofh->vbq, b);
1117 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1119 struct omap24xxcam_fh *ofh = fh;
1120 struct omap24xxcam_device *cam = ofh->cam;
1121 struct videobuf_buffer *vb;
1122 int rval;
1124 videobuf_dqbuf_again:
1125 rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
1126 if (rval)
1127 goto out;
1129 vb = ofh->vbq.bufs[b->index];
1131 mutex_lock(&cam->mutex);
1132 /* _needs_reset returns -EIO if reset is required. */
1133 rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr);
1134 mutex_unlock(&cam->mutex);
1135 if (rval == -EIO)
1136 schedule_work(&cam->sensor_reset_work);
1137 else
1138 rval = 0;
1140 out:
1142 * This is a hack. We don't want to show -EIO to the user
1143 * space. Requeue the buffer and try again if we're not doing
1144 * this in non-blocking mode.
1146 if (rval == -EIO) {
1147 videobuf_qbuf(&ofh->vbq, b);
1148 if (!(file->f_flags & O_NONBLOCK))
1149 goto videobuf_dqbuf_again;
1151 * We don't have a videobuf_buffer now --- maybe next
1152 * time...
1154 rval = -EAGAIN;
1157 return rval;
1160 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1162 struct omap24xxcam_fh *ofh = fh;
1163 struct omap24xxcam_device *cam = ofh->cam;
1164 int rval;
1166 mutex_lock(&cam->mutex);
1167 if (cam->streaming) {
1168 rval = -EBUSY;
1169 goto out;
1172 rval = omap24xxcam_sensor_if_enable(cam);
1173 if (rval) {
1174 dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n");
1175 goto out;
1178 rval = videobuf_streamon(&ofh->vbq);
1179 if (!rval) {
1180 cam->streaming = file;
1181 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1184 out:
1185 mutex_unlock(&cam->mutex);
1187 return rval;
1190 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1192 struct omap24xxcam_fh *ofh = fh;
1193 struct omap24xxcam_device *cam = ofh->cam;
1194 struct videobuf_queue *q = &ofh->vbq;
1195 int rval;
1197 atomic_inc(&cam->reset_disable);
1199 flush_scheduled_work();
1201 rval = videobuf_streamoff(q);
1202 if (!rval) {
1203 mutex_lock(&cam->mutex);
1204 cam->streaming = NULL;
1205 mutex_unlock(&cam->mutex);
1206 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1209 atomic_dec(&cam->reset_disable);
1211 return rval;
1214 static int vidioc_enum_input(struct file *file, void *fh,
1215 struct v4l2_input *inp)
1217 if (inp->index > 0)
1218 return -EINVAL;
1220 strlcpy(inp->name, "camera", sizeof(inp->name));
1221 inp->type = V4L2_INPUT_TYPE_CAMERA;
1223 return 0;
1226 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
1228 *i = 0;
1230 return 0;
1233 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
1235 if (i > 0)
1236 return -EINVAL;
1238 return 0;
1241 static int vidioc_queryctrl(struct file *file, void *fh,
1242 struct v4l2_queryctrl *a)
1244 struct omap24xxcam_fh *ofh = fh;
1245 struct omap24xxcam_device *cam = ofh->cam;
1246 int rval;
1248 rval = vidioc_int_queryctrl(cam->sdev, a);
1250 return rval;
1253 static int vidioc_g_ctrl(struct file *file, void *fh,
1254 struct v4l2_control *a)
1256 struct omap24xxcam_fh *ofh = fh;
1257 struct omap24xxcam_device *cam = ofh->cam;
1258 int rval;
1260 mutex_lock(&cam->mutex);
1261 rval = vidioc_int_g_ctrl(cam->sdev, a);
1262 mutex_unlock(&cam->mutex);
1264 return rval;
1267 static int vidioc_s_ctrl(struct file *file, void *fh,
1268 struct v4l2_control *a)
1270 struct omap24xxcam_fh *ofh = fh;
1271 struct omap24xxcam_device *cam = ofh->cam;
1272 int rval;
1274 mutex_lock(&cam->mutex);
1275 rval = vidioc_int_s_ctrl(cam->sdev, a);
1276 mutex_unlock(&cam->mutex);
1278 return rval;
1281 static int vidioc_g_parm(struct file *file, void *fh,
1282 struct v4l2_streamparm *a) {
1283 struct omap24xxcam_fh *ofh = fh;
1284 struct omap24xxcam_device *cam = ofh->cam;
1285 int rval;
1287 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1288 return -EINVAL;
1290 mutex_lock(&cam->mutex);
1291 rval = vidioc_int_g_parm(cam->sdev, a);
1292 mutex_unlock(&cam->mutex);
1294 return rval;
1297 static int vidioc_s_parm(struct file *file, void *fh,
1298 struct v4l2_streamparm *a)
1300 struct omap24xxcam_fh *ofh = fh;
1301 struct omap24xxcam_device *cam = ofh->cam;
1302 struct v4l2_streamparm old_streamparm;
1303 int rval;
1305 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1306 return -EINVAL;
1308 mutex_lock(&cam->mutex);
1309 if (cam->streaming) {
1310 rval = -EBUSY;
1311 goto out;
1314 old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1315 rval = vidioc_int_g_parm(cam->sdev, &old_streamparm);
1316 if (rval)
1317 goto out;
1319 rval = vidioc_int_s_parm(cam->sdev, a);
1320 if (rval)
1321 goto out;
1323 rval = omap24xxcam_sensor_if_enable(cam);
1325 * Revert to old streaming parameters if enabling sensor
1326 * interface with the new ones failed.
1328 if (rval)
1329 vidioc_int_s_parm(cam->sdev, &old_streamparm);
1331 out:
1332 mutex_unlock(&cam->mutex);
1334 return rval;
1339 * File operations.
1343 static unsigned int omap24xxcam_poll(struct file *file,
1344 struct poll_table_struct *wait)
1346 struct omap24xxcam_fh *fh = file->private_data;
1347 struct omap24xxcam_device *cam = fh->cam;
1348 struct videobuf_buffer *vb;
1350 mutex_lock(&cam->mutex);
1351 if (cam->streaming != file) {
1352 mutex_unlock(&cam->mutex);
1353 return POLLERR;
1355 mutex_unlock(&cam->mutex);
1357 mutex_lock(&fh->vbq.vb_lock);
1358 if (list_empty(&fh->vbq.stream)) {
1359 mutex_unlock(&fh->vbq.vb_lock);
1360 return POLLERR;
1362 vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
1363 mutex_unlock(&fh->vbq.vb_lock);
1365 poll_wait(file, &vb->done, wait);
1367 if (vb->state == VIDEOBUF_DONE || vb->state == VIDEOBUF_ERROR)
1368 return POLLIN | POLLRDNORM;
1370 return 0;
1373 static int omap24xxcam_mmap_buffers(struct file *file,
1374 struct vm_area_struct *vma)
1376 struct omap24xxcam_fh *fh = file->private_data;
1377 struct omap24xxcam_device *cam = fh->cam;
1378 struct videobuf_queue *vbq = &fh->vbq;
1379 unsigned int first, last, size, i, j;
1380 int err = 0;
1382 mutex_lock(&cam->mutex);
1383 if (cam->streaming) {
1384 mutex_unlock(&cam->mutex);
1385 return -EBUSY;
1387 mutex_unlock(&cam->mutex);
1388 mutex_lock(&vbq->vb_lock);
1390 /* look for first buffer to map */
1391 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1392 if (NULL == vbq->bufs[first])
1393 continue;
1394 if (V4L2_MEMORY_MMAP != vbq->bufs[first]->memory)
1395 continue;
1396 if (vbq->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1397 break;
1400 /* look for last buffer to map */
1401 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1402 if (NULL == vbq->bufs[last])
1403 continue;
1404 if (V4L2_MEMORY_MMAP != vbq->bufs[last]->memory)
1405 continue;
1406 size += vbq->bufs[last]->bsize;
1407 if (size == (vma->vm_end - vma->vm_start))
1408 break;
1411 size = 0;
1412 for (i = first; i <= last; i++) {
1413 struct videobuf_dmabuf *dma = videobuf_to_dma(vbq->bufs[i]);
1415 for (j = 0; j < dma->sglen; j++) {
1416 err = remap_pfn_range(
1417 vma, vma->vm_start + size,
1418 page_to_pfn(sg_page(&dma->sglist[j])),
1419 sg_dma_len(&dma->sglist[j]), vma->vm_page_prot);
1420 if (err)
1421 goto out;
1422 size += sg_dma_len(&dma->sglist[j]);
1426 out:
1427 mutex_unlock(&vbq->vb_lock);
1429 return err;
1432 static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma)
1434 struct omap24xxcam_fh *fh = file->private_data;
1435 int rval;
1437 /* let the video-buf mapper check arguments and set-up structures */
1438 rval = videobuf_mmap_mapper(&fh->vbq, vma);
1439 if (rval)
1440 return rval;
1442 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1444 /* do mapping to our allocated buffers */
1445 rval = omap24xxcam_mmap_buffers(file, vma);
1447 * In case of error, free vma->vm_private_data allocated by
1448 * videobuf_mmap_mapper.
1450 if (rval)
1451 kfree(vma->vm_private_data);
1453 return rval;
1456 static int omap24xxcam_open(struct inode *inode, struct file *file)
1458 int minor = iminor(inode);
1459 struct omap24xxcam_device *cam = omap24xxcam.priv;
1460 struct omap24xxcam_fh *fh;
1461 struct v4l2_format format;
1463 if (!cam || !cam->vfd || (cam->vfd->minor != minor))
1464 return -ENODEV;
1466 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1467 if (fh == NULL)
1468 return -ENOMEM;
1470 mutex_lock(&cam->mutex);
1471 if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
1472 mutex_unlock(&cam->mutex);
1473 goto out_try_module_get;
1476 if (atomic_inc_return(&cam->users) == 1) {
1477 omap24xxcam_hwinit(cam);
1478 if (omap24xxcam_sensor_enable(cam)) {
1479 mutex_unlock(&cam->mutex);
1480 goto out_omap24xxcam_sensor_enable;
1483 mutex_unlock(&cam->mutex);
1485 fh->cam = cam;
1486 mutex_lock(&cam->mutex);
1487 vidioc_int_g_fmt_cap(cam->sdev, &format);
1488 mutex_unlock(&cam->mutex);
1489 /* FIXME: how about fh->pix when there are more users? */
1490 fh->pix = format.fmt.pix;
1492 file->private_data = fh;
1494 spin_lock_init(&fh->vbq_lock);
1496 videobuf_queue_sg_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL,
1497 &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1498 V4L2_FIELD_NONE,
1499 sizeof(struct videobuf_buffer), fh);
1501 return 0;
1503 out_omap24xxcam_sensor_enable:
1504 omap24xxcam_poweron_reset(cam);
1505 module_put(cam->sdev->module);
1507 out_try_module_get:
1508 kfree(fh);
1510 return -ENODEV;
1513 static int omap24xxcam_release(struct inode *inode, struct file *file)
1515 struct omap24xxcam_fh *fh = file->private_data;
1516 struct omap24xxcam_device *cam = fh->cam;
1518 atomic_inc(&cam->reset_disable);
1520 flush_scheduled_work();
1522 /* stop streaming capture */
1523 videobuf_streamoff(&fh->vbq);
1525 mutex_lock(&cam->mutex);
1526 if (cam->streaming == file) {
1527 cam->streaming = NULL;
1528 mutex_unlock(&cam->mutex);
1529 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1530 } else {
1531 mutex_unlock(&cam->mutex);
1534 atomic_dec(&cam->reset_disable);
1536 omap24xxcam_vbq_free_mmap_buffers(&fh->vbq);
1539 * Make sure the reset work we might have scheduled is not
1540 * pending! It may be run *only* if we have users. (And it may
1541 * not be scheduled anymore since streaming is already
1542 * disabled.)
1544 flush_scheduled_work();
1546 mutex_lock(&cam->mutex);
1547 if (atomic_dec_return(&cam->users) == 0) {
1548 omap24xxcam_sensor_disable(cam);
1549 omap24xxcam_poweron_reset(cam);
1551 mutex_unlock(&cam->mutex);
1553 file->private_data = NULL;
1555 module_put(cam->sdev->module);
1556 kfree(fh);
1558 return 0;
1561 static struct file_operations omap24xxcam_fops = {
1562 .owner = THIS_MODULE,
1563 .llseek = no_llseek,
1564 .ioctl = video_ioctl2,
1565 .poll = omap24xxcam_poll,
1566 .mmap = omap24xxcam_mmap,
1567 .open = omap24xxcam_open,
1568 .release = omap24xxcam_release,
1573 * Power management.
1577 #ifdef CONFIG_PM
1578 static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state)
1580 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1582 if (atomic_read(&cam->users) == 0)
1583 return 0;
1585 if (!atomic_read(&cam->reset_disable))
1586 omap24xxcam_capture_stop(cam);
1588 omap24xxcam_sensor_disable(cam);
1589 omap24xxcam_poweron_reset(cam);
1591 return 0;
1594 static int omap24xxcam_resume(struct platform_device *pdev)
1596 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1598 if (atomic_read(&cam->users) == 0)
1599 return 0;
1601 omap24xxcam_hwinit(cam);
1602 omap24xxcam_sensor_enable(cam);
1604 if (!atomic_read(&cam->reset_disable))
1605 omap24xxcam_capture_cont(cam);
1607 return 0;
1609 #endif /* CONFIG_PM */
1613 * Camera device (i.e. /dev/video).
1617 static int omap24xxcam_device_register(struct v4l2_int_device *s)
1619 struct omap24xxcam_device *cam = s->u.slave->master->priv;
1620 struct video_device *vfd;
1621 int rval;
1623 /* We already have a slave. */
1624 if (cam->sdev)
1625 return -EBUSY;
1627 cam->sdev = s;
1629 if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
1630 dev_err(cam->dev, "could not register sysfs entry\n");
1631 rval = -EBUSY;
1632 goto err;
1635 /* initialize the video_device struct */
1636 vfd = cam->vfd = video_device_alloc();
1637 if (!vfd) {
1638 dev_err(cam->dev, "could not allocate video device struct\n");
1639 rval = -ENOMEM;
1640 goto err;
1642 vfd->release = video_device_release;
1644 vfd->dev = cam->dev;
1646 strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1647 vfd->type = VID_TYPE_CAPTURE | VID_TYPE_CHROMAKEY;
1648 vfd->fops = &omap24xxcam_fops;
1649 vfd->priv = cam;
1650 vfd->minor = -1;
1652 vfd->vidioc_querycap = vidioc_querycap;
1653 vfd->vidioc_enum_fmt_cap = vidioc_enum_fmt_cap;
1654 vfd->vidioc_g_fmt_cap = vidioc_g_fmt_cap;
1655 vfd->vidioc_s_fmt_cap = vidioc_s_fmt_cap;
1656 vfd->vidioc_try_fmt_cap = vidioc_try_fmt_cap;
1657 vfd->vidioc_reqbufs = vidioc_reqbufs;
1658 vfd->vidioc_querybuf = vidioc_querybuf;
1659 vfd->vidioc_qbuf = vidioc_qbuf;
1660 vfd->vidioc_dqbuf = vidioc_dqbuf;
1661 vfd->vidioc_streamon = vidioc_streamon;
1662 vfd->vidioc_streamoff = vidioc_streamoff;
1663 vfd->vidioc_enum_input = vidioc_enum_input;
1664 vfd->vidioc_g_input = vidioc_g_input;
1665 vfd->vidioc_s_input = vidioc_s_input;
1666 vfd->vidioc_queryctrl = vidioc_queryctrl;
1667 vfd->vidioc_g_ctrl = vidioc_g_ctrl;
1668 vfd->vidioc_s_ctrl = vidioc_s_ctrl;
1669 vfd->vidioc_g_parm = vidioc_g_parm;
1670 vfd->vidioc_s_parm = vidioc_s_parm;
1672 omap24xxcam_hwinit(cam);
1674 rval = omap24xxcam_sensor_init(cam);
1675 if (rval)
1676 goto err;
1678 if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1679 dev_err(cam->dev, "could not register V4L device\n");
1680 vfd->minor = -1;
1681 rval = -EBUSY;
1682 goto err;
1685 omap24xxcam_poweron_reset(cam);
1687 dev_info(cam->dev, "registered device video%d\n", vfd->minor);
1689 return 0;
1691 err:
1692 omap24xxcam_device_unregister(s);
1694 return rval;
1697 static void omap24xxcam_device_unregister(struct v4l2_int_device *s)
1699 struct omap24xxcam_device *cam = s->u.slave->master->priv;
1701 omap24xxcam_sensor_exit(cam);
1703 if (cam->vfd) {
1704 if (cam->vfd->minor == -1) {
1706 * The device was never registered, so release the
1707 * video_device struct directly.
1709 video_device_release(cam->vfd);
1710 } else {
1712 * The unregister function will release the
1713 * video_device struct as well as
1714 * unregistering it.
1716 video_unregister_device(cam->vfd);
1718 cam->vfd = NULL;
1721 device_remove_file(cam->dev, &dev_attr_streaming);
1723 cam->sdev = NULL;
1726 static struct v4l2_int_master omap24xxcam_master = {
1727 .attach = omap24xxcam_device_register,
1728 .detach = omap24xxcam_device_unregister,
1731 static struct v4l2_int_device omap24xxcam = {
1732 .module = THIS_MODULE,
1733 .name = CAM_NAME,
1734 .type = v4l2_int_type_master,
1735 .u = {
1736 .master = &omap24xxcam_master
1742 * Driver initialisation and deinitialisation.
1746 static int __init omap24xxcam_probe(struct platform_device *pdev)
1748 struct omap24xxcam_device *cam;
1749 struct resource *mem;
1750 int irq;
1752 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1753 if (!cam) {
1754 dev_err(&pdev->dev, "could not allocate memory\n");
1755 goto err;
1758 platform_set_drvdata(pdev, cam);
1760 cam->dev = &pdev->dev;
1763 * Impose a lower limit on the amount of memory allocated for
1764 * capture. We require at least enough memory to double-buffer
1765 * QVGA (300KB).
1767 if (capture_mem < 320 * 240 * 2 * 2)
1768 capture_mem = 320 * 240 * 2 * 2;
1769 cam->capture_mem = capture_mem;
1771 /* request the mem region for the camera registers */
1772 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1773 if (!mem) {
1774 dev_err(cam->dev, "no mem resource?\n");
1775 goto err;
1777 if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
1778 pdev->name)) {
1779 dev_err(cam->dev,
1780 "cannot reserve camera register I/O region\n");
1781 goto err;
1783 cam->mmio_base_phys = mem->start;
1784 cam->mmio_size = (mem->end - mem->start) + 1;
1786 /* map the region */
1787 cam->mmio_base = (unsigned long)
1788 ioremap_nocache(cam->mmio_base_phys, cam->mmio_size);
1789 if (!cam->mmio_base) {
1790 dev_err(cam->dev, "cannot map camera register I/O region\n");
1791 goto err;
1794 irq = platform_get_irq(pdev, 0);
1795 if (irq <= 0) {
1796 dev_err(cam->dev, "no irq for camera?\n");
1797 goto err;
1800 /* install the interrupt service routine */
1801 if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) {
1802 dev_err(cam->dev,
1803 "could not install interrupt service routine\n");
1804 goto err;
1806 cam->irq = irq;
1808 if (omap24xxcam_clock_get(cam))
1809 goto err;
1811 INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work);
1813 mutex_init(&cam->mutex);
1814 spin_lock_init(&cam->core_enable_disable_lock);
1816 omap24xxcam_sgdma_init(&cam->sgdma,
1817 cam->mmio_base + CAMDMA_REG_OFFSET,
1818 omap24xxcam_stalled_dma_reset,
1819 (unsigned long)cam);
1821 omap24xxcam.priv = cam;
1823 if (v4l2_int_device_register(&omap24xxcam))
1824 goto err;
1826 return 0;
1828 err:
1829 omap24xxcam_remove(pdev);
1830 return -ENODEV;
1833 static int omap24xxcam_remove(struct platform_device *pdev)
1835 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1837 if (!cam)
1838 return 0;
1840 if (omap24xxcam.priv != NULL)
1841 v4l2_int_device_unregister(&omap24xxcam);
1842 omap24xxcam.priv = NULL;
1844 omap24xxcam_clock_put(cam);
1846 if (cam->irq) {
1847 free_irq(cam->irq, cam);
1848 cam->irq = 0;
1851 if (cam->mmio_base) {
1852 iounmap((void *)cam->mmio_base);
1853 cam->mmio_base = 0;
1856 if (cam->mmio_base_phys) {
1857 release_mem_region(cam->mmio_base_phys, cam->mmio_size);
1858 cam->mmio_base_phys = 0;
1861 kfree(cam);
1863 return 0;
1866 static struct platform_driver omap24xxcam_driver = {
1867 .probe = omap24xxcam_probe,
1868 .remove = omap24xxcam_remove,
1869 #ifdef CONFIG_PM
1870 .suspend = omap24xxcam_suspend,
1871 .resume = omap24xxcam_resume,
1872 #endif
1873 .driver = {
1874 .name = CAM_NAME,
1880 * Module initialisation and deinitialisation
1884 static int __init omap24xxcam_init(void)
1886 return platform_driver_register(&omap24xxcam_driver);
1889 static void __exit omap24xxcam_cleanup(void)
1891 platform_driver_unregister(&omap24xxcam_driver);
1894 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
1895 MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
1896 MODULE_LICENSE("GPL");
1897 module_param(video_nr, int, 0);
1898 MODULE_PARM_DESC(video_nr,
1899 "Minor number for video device (-1 ==> auto assign)");
1900 module_param(capture_mem, int, 0);
1901 MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture "
1902 "buffers (default 4800kiB)");
1904 module_init(omap24xxcam_init);
1905 module_exit(omap24xxcam_cleanup);