drm/nvc0: gpuobj_new need only check validity and init the relevant engine
[linux-2.6/libata-dev.git] / drivers / media / video / omap24xxcam.c
blob378b094aff16867a35f320da2bfe230b17fbc07a
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-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
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>
38 #include <linux/slab.h>
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
43 #include "omap24xxcam.h"
45 #define OMAP24XXCAM_VERSION KERNEL_VERSION(0, 0, 0)
47 #define RESET_TIMEOUT_NS 10000
49 static void omap24xxcam_reset(struct omap24xxcam_device *cam);
50 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam);
51 static void omap24xxcam_device_unregister(struct v4l2_int_device *s);
52 static int omap24xxcam_remove(struct platform_device *pdev);
54 /* module parameters */
55 static int video_nr = -1; /* video device minor (-1 ==> auto assign) */
57 * Maximum amount of memory to use for capture buffers.
58 * Default is 4800KB, enough to double-buffer SXGA.
60 static int capture_mem = 1280 * 960 * 2 * 2;
62 static struct v4l2_int_device omap24xxcam;
66 * Clocks.
70 static void omap24xxcam_clock_put(struct omap24xxcam_device *cam)
72 if (cam->ick != NULL && !IS_ERR(cam->ick))
73 clk_put(cam->ick);
74 if (cam->fck != NULL && !IS_ERR(cam->fck))
75 clk_put(cam->fck);
77 cam->ick = cam->fck = NULL;
80 static int omap24xxcam_clock_get(struct omap24xxcam_device *cam)
82 int rval = 0;
84 cam->fck = clk_get(cam->dev, "fck");
85 if (IS_ERR(cam->fck)) {
86 dev_err(cam->dev, "can't get camera fck");
87 rval = PTR_ERR(cam->fck);
88 omap24xxcam_clock_put(cam);
89 return rval;
92 cam->ick = clk_get(cam->dev, "ick");
93 if (IS_ERR(cam->ick)) {
94 dev_err(cam->dev, "can't get camera ick");
95 rval = PTR_ERR(cam->ick);
96 omap24xxcam_clock_put(cam);
99 return rval;
102 static void omap24xxcam_clock_on(struct omap24xxcam_device *cam)
104 clk_enable(cam->fck);
105 clk_enable(cam->ick);
108 static void omap24xxcam_clock_off(struct omap24xxcam_device *cam)
110 clk_disable(cam->fck);
111 clk_disable(cam->ick);
116 * Camera core
121 * Set xclk.
123 * To disable xclk, use value zero.
125 static void omap24xxcam_core_xclk_set(const struct omap24xxcam_device *cam,
126 u32 xclk)
128 if (xclk) {
129 u32 divisor = CAM_MCLK / xclk;
131 if (divisor == 1)
132 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
133 CC_CTRL_XCLK,
134 CC_CTRL_XCLK_DIV_BYPASS);
135 else
136 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
137 CC_CTRL_XCLK, divisor);
138 } else
139 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
140 CC_CTRL_XCLK, CC_CTRL_XCLK_DIV_STABLE_LOW);
143 static void omap24xxcam_core_hwinit(const struct omap24xxcam_device *cam)
146 * Setting the camera core AUTOIDLE bit causes problems with frame
147 * synchronization, so we will clear the AUTOIDLE bit instead.
149 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_SYSCONFIG,
150 CC_SYSCONFIG_AUTOIDLE);
152 /* program the camera interface DMA packet size */
153 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL_DMA,
154 CC_CTRL_DMA_EN | (DMA_THRESHOLD / 4 - 1));
156 /* enable camera core error interrupts */
157 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQENABLE,
158 CC_IRQENABLE_FW_ERR_IRQ
159 | CC_IRQENABLE_FSC_ERR_IRQ
160 | CC_IRQENABLE_SSC_ERR_IRQ
161 | CC_IRQENABLE_FIFO_OF_IRQ);
165 * Enable the camera core.
167 * Data transfer to the camera DMA starts from next starting frame.
169 static void omap24xxcam_core_enable(const struct omap24xxcam_device *cam)
172 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
173 cam->cc_ctrl);
177 * Disable camera core.
179 * The data transfer will be stopped immediately (CC_CTRL_CC_RST). The
180 * core internal state machines will be reset. Use
181 * CC_CTRL_CC_FRAME_TRIG instead if you want to transfer the current
182 * frame completely.
184 static void omap24xxcam_core_disable(const struct omap24xxcam_device *cam)
186 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
187 CC_CTRL_CC_RST);
190 /* Interrupt service routine for camera core interrupts. */
191 static void omap24xxcam_core_isr(struct omap24xxcam_device *cam)
193 u32 cc_irqstatus;
194 const u32 cc_irqstatus_err =
195 CC_IRQSTATUS_FW_ERR_IRQ
196 | CC_IRQSTATUS_FSC_ERR_IRQ
197 | CC_IRQSTATUS_SSC_ERR_IRQ
198 | CC_IRQSTATUS_FIFO_UF_IRQ
199 | CC_IRQSTATUS_FIFO_OF_IRQ;
201 cc_irqstatus = omap24xxcam_reg_in(cam->mmio_base + CC_REG_OFFSET,
202 CC_IRQSTATUS);
203 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQSTATUS,
204 cc_irqstatus);
206 if (cc_irqstatus & cc_irqstatus_err
207 && !atomic_read(&cam->in_reset)) {
208 dev_dbg(cam->dev, "resetting camera, cc_irqstatus 0x%x\n",
209 cc_irqstatus);
210 omap24xxcam_reset(cam);
216 * videobuf_buffer handling.
218 * Memory for mmapped videobuf_buffers is not allocated
219 * conventionally, but by several kmalloc allocations and then
220 * creating the scatterlist on our own. User-space buffers are handled
221 * normally.
226 * Free the memory-mapped buffer memory allocated for a
227 * videobuf_buffer and the associated scatterlist.
229 static void omap24xxcam_vbq_free_mmap_buffer(struct videobuf_buffer *vb)
231 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
232 size_t alloc_size;
233 struct page *page;
234 int i;
236 if (dma->sglist == NULL)
237 return;
239 i = dma->sglen;
240 while (i) {
241 i--;
242 alloc_size = sg_dma_len(&dma->sglist[i]);
243 page = sg_page(&dma->sglist[i]);
244 do {
245 ClearPageReserved(page++);
246 } while (alloc_size -= PAGE_SIZE);
247 __free_pages(sg_page(&dma->sglist[i]),
248 get_order(sg_dma_len(&dma->sglist[i])));
251 kfree(dma->sglist);
252 dma->sglist = NULL;
255 /* Release all memory related to the videobuf_queue. */
256 static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue *vbq)
258 int i;
260 mutex_lock(&vbq->vb_lock);
262 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
263 if (NULL == vbq->bufs[i])
264 continue;
265 if (V4L2_MEMORY_MMAP != vbq->bufs[i]->memory)
266 continue;
267 vbq->ops->buf_release(vbq, vbq->bufs[i]);
268 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
269 kfree(vbq->bufs[i]);
270 vbq->bufs[i] = NULL;
273 mutex_unlock(&vbq->vb_lock);
275 videobuf_mmap_free(vbq);
279 * Allocate physically as contiguous as possible buffer for video
280 * frame and allocate and build DMA scatter-gather list for it.
282 static int omap24xxcam_vbq_alloc_mmap_buffer(struct videobuf_buffer *vb)
284 unsigned int order;
285 size_t alloc_size, size = vb->bsize; /* vb->bsize is page aligned */
286 struct page *page;
287 int max_pages, err = 0, i = 0;
288 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
291 * allocate maximum size scatter-gather list. Note this is
292 * overhead. We may not use as many entries as we allocate
294 max_pages = vb->bsize >> PAGE_SHIFT;
295 dma->sglist = kcalloc(max_pages, sizeof(*dma->sglist), GFP_KERNEL);
296 if (dma->sglist == NULL) {
297 err = -ENOMEM;
298 goto out;
301 while (size) {
302 order = get_order(size);
304 * do not over-allocate even if we would get larger
305 * contiguous chunk that way
307 if ((PAGE_SIZE << order) > size)
308 order--;
310 /* try to allocate as many contiguous pages as possible */
311 page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
312 /* if allocation fails, try to allocate smaller amount */
313 while (page == NULL) {
314 order--;
315 page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
316 if (page == NULL && !order) {
317 err = -ENOMEM;
318 goto out;
321 size -= (PAGE_SIZE << order);
323 /* append allocated chunk of pages into scatter-gather list */
324 sg_set_page(&dma->sglist[i], page, PAGE_SIZE << order, 0);
325 dma->sglen++;
326 i++;
328 alloc_size = (PAGE_SIZE << order);
330 /* clear pages before giving them to user space */
331 memset(page_address(page), 0, alloc_size);
333 /* mark allocated pages reserved */
334 do {
335 SetPageReserved(page++);
336 } while (alloc_size -= PAGE_SIZE);
339 * REVISIT: not fully correct to assign nr_pages == sglen but
340 * video-buf is passing nr_pages for e.g. unmap_sg calls
342 dma->nr_pages = dma->sglen;
343 dma->direction = PCI_DMA_FROMDEVICE;
345 return 0;
347 out:
348 omap24xxcam_vbq_free_mmap_buffer(vb);
349 return err;
352 static int omap24xxcam_vbq_alloc_mmap_buffers(struct videobuf_queue *vbq,
353 unsigned int count)
355 int i, err = 0;
356 struct omap24xxcam_fh *fh =
357 container_of(vbq, struct omap24xxcam_fh, vbq);
359 mutex_lock(&vbq->vb_lock);
361 for (i = 0; i < count; i++) {
362 err = omap24xxcam_vbq_alloc_mmap_buffer(vbq->bufs[i]);
363 if (err)
364 goto out;
365 dev_dbg(fh->cam->dev, "sglen is %d for buffer %d\n",
366 videobuf_to_dma(vbq->bufs[i])->sglen, i);
369 mutex_unlock(&vbq->vb_lock);
371 return 0;
372 out:
373 while (i) {
374 i--;
375 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
378 mutex_unlock(&vbq->vb_lock);
380 return err;
384 * This routine is called from interrupt context when a scatter-gather DMA
385 * transfer of a videobuf_buffer completes.
387 static void omap24xxcam_vbq_complete(struct omap24xxcam_sgdma *sgdma,
388 u32 csr, void *arg)
390 struct omap24xxcam_device *cam =
391 container_of(sgdma, struct omap24xxcam_device, sgdma);
392 struct omap24xxcam_fh *fh = cam->streaming->private_data;
393 struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
394 const u32 csr_error = CAMDMA_CSR_MISALIGNED_ERR
395 | CAMDMA_CSR_SUPERVISOR_ERR | CAMDMA_CSR_SECURE_ERR
396 | CAMDMA_CSR_TRANS_ERR | CAMDMA_CSR_DROP;
397 unsigned long flags;
399 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
400 if (--cam->sgdma_in_queue == 0)
401 omap24xxcam_core_disable(cam);
402 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
404 do_gettimeofday(&vb->ts);
405 vb->field_count = atomic_add_return(2, &fh->field_count);
406 if (csr & csr_error) {
407 vb->state = VIDEOBUF_ERROR;
408 if (!atomic_read(&fh->cam->in_reset)) {
409 dev_dbg(cam->dev, "resetting camera, csr 0x%x\n", csr);
410 omap24xxcam_reset(cam);
412 } else
413 vb->state = VIDEOBUF_DONE;
414 wake_up(&vb->done);
417 static void omap24xxcam_vbq_release(struct videobuf_queue *vbq,
418 struct videobuf_buffer *vb)
420 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
422 /* wait for buffer, especially to get out of the sgdma queue */
423 videobuf_waiton(vbq, vb, 0, 0);
424 if (vb->memory == V4L2_MEMORY_MMAP) {
425 dma_unmap_sg(vbq->dev, dma->sglist, dma->sglen,
426 dma->direction);
427 dma->direction = DMA_NONE;
428 } else {
429 videobuf_dma_unmap(vbq->dev, videobuf_to_dma(vb));
430 videobuf_dma_free(videobuf_to_dma(vb));
433 vb->state = VIDEOBUF_NEEDS_INIT;
437 * Limit the number of available kernel image capture buffers based on the
438 * number requested, the currently selected image size, and the maximum
439 * amount of memory permitted for kernel capture buffers.
441 static int omap24xxcam_vbq_setup(struct videobuf_queue *vbq, unsigned int *cnt,
442 unsigned int *size)
444 struct omap24xxcam_fh *fh = vbq->priv_data;
446 if (*cnt <= 0)
447 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
449 if (*cnt > VIDEO_MAX_FRAME)
450 *cnt = VIDEO_MAX_FRAME;
452 *size = fh->pix.sizeimage;
454 /* accessing fh->cam->capture_mem is ok, it's constant */
455 if (*size * *cnt > fh->cam->capture_mem)
456 *cnt = fh->cam->capture_mem / *size;
458 return 0;
461 static int omap24xxcam_dma_iolock(struct videobuf_queue *vbq,
462 struct videobuf_dmabuf *dma)
464 int err = 0;
466 dma->direction = PCI_DMA_FROMDEVICE;
467 if (!dma_map_sg(vbq->dev, dma->sglist, dma->sglen, dma->direction)) {
468 kfree(dma->sglist);
469 dma->sglist = NULL;
470 dma->sglen = 0;
471 err = -EIO;
474 return err;
477 static int omap24xxcam_vbq_prepare(struct videobuf_queue *vbq,
478 struct videobuf_buffer *vb,
479 enum v4l2_field field)
481 struct omap24xxcam_fh *fh = vbq->priv_data;
482 int err = 0;
485 * Accessing pix here is okay since it's constant while
486 * streaming is on (and we only get called then).
488 if (vb->baddr) {
489 /* This is a userspace buffer. */
490 if (fh->pix.sizeimage > vb->bsize) {
491 /* The buffer isn't big enough. */
492 err = -EINVAL;
493 } else
494 vb->size = fh->pix.sizeimage;
495 } else {
496 if (vb->state != VIDEOBUF_NEEDS_INIT) {
498 * We have a kernel bounce buffer that has
499 * already been allocated.
501 if (fh->pix.sizeimage > vb->size) {
503 * The image size has been changed to
504 * a larger size since this buffer was
505 * allocated, so we need to free and
506 * reallocate it.
508 omap24xxcam_vbq_release(vbq, vb);
509 vb->size = fh->pix.sizeimage;
511 } else {
512 /* We need to allocate a new kernel bounce buffer. */
513 vb->size = fh->pix.sizeimage;
517 if (err)
518 return err;
520 vb->width = fh->pix.width;
521 vb->height = fh->pix.height;
522 vb->field = field;
524 if (vb->state == VIDEOBUF_NEEDS_INIT) {
525 if (vb->memory == V4L2_MEMORY_MMAP)
527 * we have built the scatter-gather list by ourself so
528 * do the scatter-gather mapping as well
530 err = omap24xxcam_dma_iolock(vbq, videobuf_to_dma(vb));
531 else
532 err = videobuf_iolock(vbq, vb, NULL);
535 if (!err)
536 vb->state = VIDEOBUF_PREPARED;
537 else
538 omap24xxcam_vbq_release(vbq, vb);
540 return err;
543 static void omap24xxcam_vbq_queue(struct videobuf_queue *vbq,
544 struct videobuf_buffer *vb)
546 struct omap24xxcam_fh *fh = vbq->priv_data;
547 struct omap24xxcam_device *cam = fh->cam;
548 enum videobuf_state state = vb->state;
549 unsigned long flags;
550 int err;
553 * FIXME: We're marking the buffer active since we have no
554 * pretty way of marking it active exactly when the
555 * scatter-gather transfer starts.
557 vb->state = VIDEOBUF_ACTIVE;
559 err = omap24xxcam_sgdma_queue(&fh->cam->sgdma,
560 videobuf_to_dma(vb)->sglist,
561 videobuf_to_dma(vb)->sglen, vb->size,
562 omap24xxcam_vbq_complete, vb);
564 if (!err) {
565 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
566 if (++cam->sgdma_in_queue == 1
567 && !atomic_read(&cam->in_reset))
568 omap24xxcam_core_enable(cam);
569 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
570 } else {
572 * Oops. We're not supposed to get any errors here.
573 * The only way we could get an error is if we ran out
574 * of scatter-gather DMA slots, but we are supposed to
575 * have at least as many scatter-gather DMA slots as
576 * video buffers so that can't happen.
578 dev_err(cam->dev, "failed to queue a video buffer for dma!\n");
579 dev_err(cam->dev, "likely a bug in the driver!\n");
580 vb->state = state;
584 static struct videobuf_queue_ops omap24xxcam_vbq_ops = {
585 .buf_setup = omap24xxcam_vbq_setup,
586 .buf_prepare = omap24xxcam_vbq_prepare,
587 .buf_queue = omap24xxcam_vbq_queue,
588 .buf_release = omap24xxcam_vbq_release,
593 * OMAP main camera system
598 * Reset camera block to power-on state.
600 static void omap24xxcam_poweron_reset(struct omap24xxcam_device *cam)
602 int max_loop = RESET_TIMEOUT_NS;
604 /* Reset whole camera subsystem */
605 omap24xxcam_reg_out(cam->mmio_base,
606 CAM_SYSCONFIG,
607 CAM_SYSCONFIG_SOFTRESET);
609 /* Wait till it's finished */
610 while (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
611 & CAM_SYSSTATUS_RESETDONE)
612 && --max_loop) {
613 ndelay(1);
616 if (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
617 & CAM_SYSSTATUS_RESETDONE))
618 dev_err(cam->dev, "camera soft reset timeout\n");
622 * (Re)initialise the camera block.
624 static void omap24xxcam_hwinit(struct omap24xxcam_device *cam)
626 omap24xxcam_poweron_reset(cam);
628 /* set the camera subsystem autoidle bit */
629 omap24xxcam_reg_out(cam->mmio_base, CAM_SYSCONFIG,
630 CAM_SYSCONFIG_AUTOIDLE);
632 /* set the camera MMU autoidle bit */
633 omap24xxcam_reg_out(cam->mmio_base,
634 CAMMMU_REG_OFFSET + CAMMMU_SYSCONFIG,
635 CAMMMU_SYSCONFIG_AUTOIDLE);
637 omap24xxcam_core_hwinit(cam);
639 omap24xxcam_dma_hwinit(&cam->sgdma.dma);
643 * Callback for dma transfer stalling.
645 static void omap24xxcam_stalled_dma_reset(unsigned long data)
647 struct omap24xxcam_device *cam = (struct omap24xxcam_device *)data;
649 if (!atomic_read(&cam->in_reset)) {
650 dev_dbg(cam->dev, "dma stalled, resetting camera\n");
651 omap24xxcam_reset(cam);
656 * Stop capture. Mark we're doing a reset, stop DMA transfers and
657 * core. (No new scatter-gather transfers will be queued whilst
658 * in_reset is non-zero.)
660 * If omap24xxcam_capture_stop is called from several places at
661 * once, only the first call will have an effect. Similarly, the last
662 * call omap24xxcam_streaming_cont will have effect.
664 * Serialisation is ensured by using cam->core_enable_disable_lock.
666 static void omap24xxcam_capture_stop(struct omap24xxcam_device *cam)
668 unsigned long flags;
670 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
672 if (atomic_inc_return(&cam->in_reset) != 1) {
673 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
674 return;
677 omap24xxcam_core_disable(cam);
679 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
681 omap24xxcam_sgdma_sync(&cam->sgdma);
685 * Reset and continue streaming.
687 * Note: Resetting the camera FIFO via the CC_RST bit in the CC_CTRL
688 * register is supposed to be sufficient to recover from a camera
689 * interface error, but it doesn't seem to be enough. If we only do
690 * that then subsequent image captures are out of sync by either one
691 * or two times DMA_THRESHOLD bytes. Resetting and re-initializing the
692 * entire camera subsystem prevents the problem with frame
693 * synchronization.
695 static void omap24xxcam_capture_cont(struct omap24xxcam_device *cam)
697 unsigned long flags;
699 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
701 if (atomic_read(&cam->in_reset) != 1)
702 goto out;
704 omap24xxcam_hwinit(cam);
706 omap24xxcam_sensor_if_enable(cam);
708 omap24xxcam_sgdma_process(&cam->sgdma);
710 if (cam->sgdma_in_queue)
711 omap24xxcam_core_enable(cam);
713 out:
714 atomic_dec(&cam->in_reset);
715 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
718 static ssize_t
719 omap24xxcam_streaming_show(struct device *dev, struct device_attribute *attr,
720 char *buf)
722 struct omap24xxcam_device *cam = dev_get_drvdata(dev);
724 return sprintf(buf, "%s\n", cam->streaming ? "active" : "inactive");
726 static DEVICE_ATTR(streaming, S_IRUGO, omap24xxcam_streaming_show, NULL);
729 * Stop capture and restart it. I.e. reset the camera during use.
731 static void omap24xxcam_reset(struct omap24xxcam_device *cam)
733 omap24xxcam_capture_stop(cam);
734 omap24xxcam_capture_cont(cam);
738 * The main interrupt handler.
740 static irqreturn_t omap24xxcam_isr(int irq, void *arg)
742 struct omap24xxcam_device *cam = (struct omap24xxcam_device *)arg;
743 u32 irqstatus;
744 unsigned int irqhandled = 0;
746 irqstatus = omap24xxcam_reg_in(cam->mmio_base, CAM_IRQSTATUS);
748 if (irqstatus &
749 (CAM_IRQSTATUS_DMA_IRQ2 | CAM_IRQSTATUS_DMA_IRQ1
750 | CAM_IRQSTATUS_DMA_IRQ0)) {
751 omap24xxcam_dma_isr(&cam->sgdma.dma);
752 irqhandled = 1;
754 if (irqstatus & CAM_IRQSTATUS_CC_IRQ) {
755 omap24xxcam_core_isr(cam);
756 irqhandled = 1;
758 if (irqstatus & CAM_IRQSTATUS_MMU_IRQ)
759 dev_err(cam->dev, "unhandled camera MMU interrupt!\n");
761 return IRQ_RETVAL(irqhandled);
766 * Sensor handling.
771 * Enable the external sensor interface. Try to negotiate interface
772 * parameters with the sensor and start using the new ones. The calls
773 * to sensor_if_enable and sensor_if_disable need not to be balanced.
775 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam)
777 int rval;
778 struct v4l2_ifparm p;
780 rval = vidioc_int_g_ifparm(cam->sdev, &p);
781 if (rval) {
782 dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
783 return rval;
786 cam->if_type = p.if_type;
788 cam->cc_ctrl = CC_CTRL_CC_EN;
790 switch (p.if_type) {
791 case V4L2_IF_TYPE_BT656:
792 if (p.u.bt656.frame_start_on_rising_vs)
793 cam->cc_ctrl |= CC_CTRL_NOBT_SYNCHRO;
794 if (p.u.bt656.bt_sync_correct)
795 cam->cc_ctrl |= CC_CTRL_BT_CORRECT;
796 if (p.u.bt656.swap)
797 cam->cc_ctrl |= CC_CTRL_PAR_ORDERCAM;
798 if (p.u.bt656.latch_clk_inv)
799 cam->cc_ctrl |= CC_CTRL_PAR_CLK_POL;
800 if (p.u.bt656.nobt_hs_inv)
801 cam->cc_ctrl |= CC_CTRL_NOBT_HS_POL;
802 if (p.u.bt656.nobt_vs_inv)
803 cam->cc_ctrl |= CC_CTRL_NOBT_VS_POL;
805 switch (p.u.bt656.mode) {
806 case V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT:
807 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT8;
808 break;
809 case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT:
810 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT10;
811 break;
812 case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT:
813 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT12;
814 break;
815 case V4L2_IF_TYPE_BT656_MODE_BT_8BIT:
816 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT8;
817 break;
818 case V4L2_IF_TYPE_BT656_MODE_BT_10BIT:
819 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT10;
820 break;
821 default:
822 dev_err(cam->dev,
823 "bt656 interface mode %d not supported\n",
824 p.u.bt656.mode);
825 return -EINVAL;
828 * The clock rate that the sensor wants has changed.
829 * We have to adjust the xclk from OMAP 2 side to
830 * match the sensor's wish as closely as possible.
832 if (p.u.bt656.clock_curr != cam->if_u.bt656.xclk) {
833 u32 xclk = p.u.bt656.clock_curr;
834 u32 divisor;
836 if (xclk == 0)
837 return -EINVAL;
839 if (xclk > CAM_MCLK)
840 xclk = CAM_MCLK;
842 divisor = CAM_MCLK / xclk;
843 if (divisor * xclk < CAM_MCLK)
844 divisor++;
845 if (CAM_MCLK / divisor < p.u.bt656.clock_min
846 && divisor > 1)
847 divisor--;
848 if (divisor > 30)
849 divisor = 30;
851 xclk = CAM_MCLK / divisor;
853 if (xclk < p.u.bt656.clock_min
854 || xclk > p.u.bt656.clock_max)
855 return -EINVAL;
857 cam->if_u.bt656.xclk = xclk;
859 omap24xxcam_core_xclk_set(cam, cam->if_u.bt656.xclk);
860 break;
861 default:
862 /* FIXME: how about other interfaces? */
863 dev_err(cam->dev, "interface type %d not supported\n",
864 p.if_type);
865 return -EINVAL;
868 return 0;
871 static void omap24xxcam_sensor_if_disable(const struct omap24xxcam_device *cam)
873 switch (cam->if_type) {
874 case V4L2_IF_TYPE_BT656:
875 omap24xxcam_core_xclk_set(cam, 0);
876 break;
881 * Initialise the sensor hardware.
883 static int omap24xxcam_sensor_init(struct omap24xxcam_device *cam)
885 int err = 0;
886 struct v4l2_int_device *sdev = cam->sdev;
888 omap24xxcam_clock_on(cam);
889 err = omap24xxcam_sensor_if_enable(cam);
890 if (err) {
891 dev_err(cam->dev, "sensor interface could not be enabled at "
892 "initialisation, %d\n", err);
893 cam->sdev = NULL;
894 goto out;
897 /* power up sensor during sensor initialization */
898 vidioc_int_s_power(sdev, 1);
900 err = vidioc_int_dev_init(sdev);
901 if (err) {
902 dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
903 /* Sensor init failed --- it's nonexistent to us! */
904 cam->sdev = NULL;
905 goto out;
908 dev_info(cam->dev, "sensor is %s\n", sdev->name);
910 out:
911 omap24xxcam_sensor_if_disable(cam);
912 omap24xxcam_clock_off(cam);
914 vidioc_int_s_power(sdev, 0);
916 return err;
919 static void omap24xxcam_sensor_exit(struct omap24xxcam_device *cam)
921 if (cam->sdev)
922 vidioc_int_dev_exit(cam->sdev);
925 static void omap24xxcam_sensor_disable(struct omap24xxcam_device *cam)
927 omap24xxcam_sensor_if_disable(cam);
928 omap24xxcam_clock_off(cam);
929 vidioc_int_s_power(cam->sdev, 0);
933 * Power-up and configure camera sensor. It's ready for capturing now.
935 static int omap24xxcam_sensor_enable(struct omap24xxcam_device *cam)
937 int rval;
939 omap24xxcam_clock_on(cam);
941 omap24xxcam_sensor_if_enable(cam);
943 rval = vidioc_int_s_power(cam->sdev, 1);
944 if (rval)
945 goto out;
947 rval = vidioc_int_init(cam->sdev);
948 if (rval)
949 goto out;
951 return 0;
953 out:
954 omap24xxcam_sensor_disable(cam);
956 return rval;
959 static void omap24xxcam_sensor_reset_work(struct work_struct *work)
961 struct omap24xxcam_device *cam =
962 container_of(work, struct omap24xxcam_device,
963 sensor_reset_work);
965 if (atomic_read(&cam->reset_disable))
966 return;
968 omap24xxcam_capture_stop(cam);
970 if (vidioc_int_reset(cam->sdev) == 0) {
971 vidioc_int_init(cam->sdev);
972 } else {
973 /* Can't reset it by vidioc_int_reset. */
974 omap24xxcam_sensor_disable(cam);
975 omap24xxcam_sensor_enable(cam);
978 omap24xxcam_capture_cont(cam);
983 * IOCTL interface.
987 static int vidioc_querycap(struct file *file, void *fh,
988 struct v4l2_capability *cap)
990 struct omap24xxcam_fh *ofh = fh;
991 struct omap24xxcam_device *cam = ofh->cam;
993 strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
994 strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
995 cap->version = OMAP24XXCAM_VERSION;
996 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
998 return 0;
1001 static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
1002 struct v4l2_fmtdesc *f)
1004 struct omap24xxcam_fh *ofh = fh;
1005 struct omap24xxcam_device *cam = ofh->cam;
1006 int rval;
1008 rval = vidioc_int_enum_fmt_cap(cam->sdev, f);
1010 return rval;
1013 static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
1014 struct v4l2_format *f)
1016 struct omap24xxcam_fh *ofh = fh;
1017 struct omap24xxcam_device *cam = ofh->cam;
1018 int rval;
1020 mutex_lock(&cam->mutex);
1021 rval = vidioc_int_g_fmt_cap(cam->sdev, f);
1022 mutex_unlock(&cam->mutex);
1024 return rval;
1027 static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
1028 struct v4l2_format *f)
1030 struct omap24xxcam_fh *ofh = fh;
1031 struct omap24xxcam_device *cam = ofh->cam;
1032 int rval;
1034 mutex_lock(&cam->mutex);
1035 if (cam->streaming) {
1036 rval = -EBUSY;
1037 goto out;
1040 rval = vidioc_int_s_fmt_cap(cam->sdev, f);
1042 out:
1043 mutex_unlock(&cam->mutex);
1045 if (!rval) {
1046 mutex_lock(&ofh->vbq.vb_lock);
1047 ofh->pix = f->fmt.pix;
1048 mutex_unlock(&ofh->vbq.vb_lock);
1051 memset(f, 0, sizeof(*f));
1052 vidioc_g_fmt_vid_cap(file, fh, f);
1054 return rval;
1057 static int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
1058 struct v4l2_format *f)
1060 struct omap24xxcam_fh *ofh = fh;
1061 struct omap24xxcam_device *cam = ofh->cam;
1062 int rval;
1064 mutex_lock(&cam->mutex);
1065 rval = vidioc_int_try_fmt_cap(cam->sdev, f);
1066 mutex_unlock(&cam->mutex);
1068 return rval;
1071 static int vidioc_reqbufs(struct file *file, void *fh,
1072 struct v4l2_requestbuffers *b)
1074 struct omap24xxcam_fh *ofh = fh;
1075 struct omap24xxcam_device *cam = ofh->cam;
1076 int rval;
1078 mutex_lock(&cam->mutex);
1079 if (cam->streaming) {
1080 mutex_unlock(&cam->mutex);
1081 return -EBUSY;
1084 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1085 mutex_unlock(&cam->mutex);
1087 rval = videobuf_reqbufs(&ofh->vbq, b);
1090 * Either videobuf_reqbufs failed or the buffers are not
1091 * memory-mapped (which would need special attention).
1093 if (rval < 0 || b->memory != V4L2_MEMORY_MMAP)
1094 goto out;
1096 rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, rval);
1097 if (rval)
1098 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1100 out:
1101 return rval;
1104 static int vidioc_querybuf(struct file *file, void *fh,
1105 struct v4l2_buffer *b)
1107 struct omap24xxcam_fh *ofh = fh;
1109 return videobuf_querybuf(&ofh->vbq, b);
1112 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1114 struct omap24xxcam_fh *ofh = fh;
1116 return videobuf_qbuf(&ofh->vbq, b);
1119 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1121 struct omap24xxcam_fh *ofh = fh;
1122 struct omap24xxcam_device *cam = ofh->cam;
1123 struct videobuf_buffer *vb;
1124 int rval;
1126 videobuf_dqbuf_again:
1127 rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
1128 if (rval)
1129 goto out;
1131 vb = ofh->vbq.bufs[b->index];
1133 mutex_lock(&cam->mutex);
1134 /* _needs_reset returns -EIO if reset is required. */
1135 rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr);
1136 mutex_unlock(&cam->mutex);
1137 if (rval == -EIO)
1138 schedule_work(&cam->sensor_reset_work);
1139 else
1140 rval = 0;
1142 out:
1144 * This is a hack. We don't want to show -EIO to the user
1145 * space. Requeue the buffer and try again if we're not doing
1146 * this in non-blocking mode.
1148 if (rval == -EIO) {
1149 videobuf_qbuf(&ofh->vbq, b);
1150 if (!(file->f_flags & O_NONBLOCK))
1151 goto videobuf_dqbuf_again;
1153 * We don't have a videobuf_buffer now --- maybe next
1154 * time...
1156 rval = -EAGAIN;
1159 return rval;
1162 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1164 struct omap24xxcam_fh *ofh = fh;
1165 struct omap24xxcam_device *cam = ofh->cam;
1166 int rval;
1168 mutex_lock(&cam->mutex);
1169 if (cam->streaming) {
1170 rval = -EBUSY;
1171 goto out;
1174 rval = omap24xxcam_sensor_if_enable(cam);
1175 if (rval) {
1176 dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n");
1177 goto out;
1180 rval = videobuf_streamon(&ofh->vbq);
1181 if (!rval) {
1182 cam->streaming = file;
1183 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1186 out:
1187 mutex_unlock(&cam->mutex);
1189 return rval;
1192 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1194 struct omap24xxcam_fh *ofh = fh;
1195 struct omap24xxcam_device *cam = ofh->cam;
1196 struct videobuf_queue *q = &ofh->vbq;
1197 int rval;
1199 atomic_inc(&cam->reset_disable);
1201 flush_scheduled_work();
1203 rval = videobuf_streamoff(q);
1204 if (!rval) {
1205 mutex_lock(&cam->mutex);
1206 cam->streaming = NULL;
1207 mutex_unlock(&cam->mutex);
1208 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1211 atomic_dec(&cam->reset_disable);
1213 return rval;
1216 static int vidioc_enum_input(struct file *file, void *fh,
1217 struct v4l2_input *inp)
1219 if (inp->index > 0)
1220 return -EINVAL;
1222 strlcpy(inp->name, "camera", sizeof(inp->name));
1223 inp->type = V4L2_INPUT_TYPE_CAMERA;
1225 return 0;
1228 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
1230 *i = 0;
1232 return 0;
1235 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
1237 if (i > 0)
1238 return -EINVAL;
1240 return 0;
1243 static int vidioc_queryctrl(struct file *file, void *fh,
1244 struct v4l2_queryctrl *a)
1246 struct omap24xxcam_fh *ofh = fh;
1247 struct omap24xxcam_device *cam = ofh->cam;
1248 int rval;
1250 rval = vidioc_int_queryctrl(cam->sdev, a);
1252 return rval;
1255 static int vidioc_g_ctrl(struct file *file, void *fh,
1256 struct v4l2_control *a)
1258 struct omap24xxcam_fh *ofh = fh;
1259 struct omap24xxcam_device *cam = ofh->cam;
1260 int rval;
1262 mutex_lock(&cam->mutex);
1263 rval = vidioc_int_g_ctrl(cam->sdev, a);
1264 mutex_unlock(&cam->mutex);
1266 return rval;
1269 static int vidioc_s_ctrl(struct file *file, void *fh,
1270 struct v4l2_control *a)
1272 struct omap24xxcam_fh *ofh = fh;
1273 struct omap24xxcam_device *cam = ofh->cam;
1274 int rval;
1276 mutex_lock(&cam->mutex);
1277 rval = vidioc_int_s_ctrl(cam->sdev, a);
1278 mutex_unlock(&cam->mutex);
1280 return rval;
1283 static int vidioc_g_parm(struct file *file, void *fh,
1284 struct v4l2_streamparm *a) {
1285 struct omap24xxcam_fh *ofh = fh;
1286 struct omap24xxcam_device *cam = ofh->cam;
1287 int rval;
1289 mutex_lock(&cam->mutex);
1290 rval = vidioc_int_g_parm(cam->sdev, a);
1291 mutex_unlock(&cam->mutex);
1293 return rval;
1296 static int vidioc_s_parm(struct file *file, void *fh,
1297 struct v4l2_streamparm *a)
1299 struct omap24xxcam_fh *ofh = fh;
1300 struct omap24xxcam_device *cam = ofh->cam;
1301 struct v4l2_streamparm old_streamparm;
1302 int rval;
1304 mutex_lock(&cam->mutex);
1305 if (cam->streaming) {
1306 rval = -EBUSY;
1307 goto out;
1310 old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1311 rval = vidioc_int_g_parm(cam->sdev, &old_streamparm);
1312 if (rval)
1313 goto out;
1315 rval = vidioc_int_s_parm(cam->sdev, a);
1316 if (rval)
1317 goto out;
1319 rval = omap24xxcam_sensor_if_enable(cam);
1321 * Revert to old streaming parameters if enabling sensor
1322 * interface with the new ones failed.
1324 if (rval)
1325 vidioc_int_s_parm(cam->sdev, &old_streamparm);
1327 out:
1328 mutex_unlock(&cam->mutex);
1330 return rval;
1335 * File operations.
1339 static unsigned int omap24xxcam_poll(struct file *file,
1340 struct poll_table_struct *wait)
1342 struct omap24xxcam_fh *fh = file->private_data;
1343 struct omap24xxcam_device *cam = fh->cam;
1344 struct videobuf_buffer *vb;
1346 mutex_lock(&cam->mutex);
1347 if (cam->streaming != file) {
1348 mutex_unlock(&cam->mutex);
1349 return POLLERR;
1351 mutex_unlock(&cam->mutex);
1353 mutex_lock(&fh->vbq.vb_lock);
1354 if (list_empty(&fh->vbq.stream)) {
1355 mutex_unlock(&fh->vbq.vb_lock);
1356 return POLLERR;
1358 vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
1359 mutex_unlock(&fh->vbq.vb_lock);
1361 poll_wait(file, &vb->done, wait);
1363 if (vb->state == VIDEOBUF_DONE || vb->state == VIDEOBUF_ERROR)
1364 return POLLIN | POLLRDNORM;
1366 return 0;
1369 static int omap24xxcam_mmap_buffers(struct file *file,
1370 struct vm_area_struct *vma)
1372 struct omap24xxcam_fh *fh = file->private_data;
1373 struct omap24xxcam_device *cam = fh->cam;
1374 struct videobuf_queue *vbq = &fh->vbq;
1375 unsigned int first, last, size, i, j;
1376 int err = 0;
1378 mutex_lock(&cam->mutex);
1379 if (cam->streaming) {
1380 mutex_unlock(&cam->mutex);
1381 return -EBUSY;
1383 mutex_unlock(&cam->mutex);
1384 mutex_lock(&vbq->vb_lock);
1386 /* look for first buffer to map */
1387 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1388 if (NULL == vbq->bufs[first])
1389 continue;
1390 if (V4L2_MEMORY_MMAP != vbq->bufs[first]->memory)
1391 continue;
1392 if (vbq->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1393 break;
1396 /* look for last buffer to map */
1397 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1398 if (NULL == vbq->bufs[last])
1399 continue;
1400 if (V4L2_MEMORY_MMAP != vbq->bufs[last]->memory)
1401 continue;
1402 size += vbq->bufs[last]->bsize;
1403 if (size == (vma->vm_end - vma->vm_start))
1404 break;
1407 size = 0;
1408 for (i = first; i <= last && i < VIDEO_MAX_FRAME; i++) {
1409 struct videobuf_dmabuf *dma = videobuf_to_dma(vbq->bufs[i]);
1411 for (j = 0; j < dma->sglen; j++) {
1412 err = remap_pfn_range(
1413 vma, vma->vm_start + size,
1414 page_to_pfn(sg_page(&dma->sglist[j])),
1415 sg_dma_len(&dma->sglist[j]), vma->vm_page_prot);
1416 if (err)
1417 goto out;
1418 size += sg_dma_len(&dma->sglist[j]);
1422 out:
1423 mutex_unlock(&vbq->vb_lock);
1425 return err;
1428 static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma)
1430 struct omap24xxcam_fh *fh = file->private_data;
1431 int rval;
1433 /* let the video-buf mapper check arguments and set-up structures */
1434 rval = videobuf_mmap_mapper(&fh->vbq, vma);
1435 if (rval)
1436 return rval;
1438 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1440 /* do mapping to our allocated buffers */
1441 rval = omap24xxcam_mmap_buffers(file, vma);
1443 * In case of error, free vma->vm_private_data allocated by
1444 * videobuf_mmap_mapper.
1446 if (rval)
1447 kfree(vma->vm_private_data);
1449 return rval;
1452 static int omap24xxcam_open(struct file *file)
1454 struct omap24xxcam_device *cam = omap24xxcam.priv;
1455 struct omap24xxcam_fh *fh;
1456 struct v4l2_format format;
1458 if (!cam || !cam->vfd)
1459 return -ENODEV;
1461 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1462 if (fh == NULL)
1463 return -ENOMEM;
1465 mutex_lock(&cam->mutex);
1466 if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
1467 mutex_unlock(&cam->mutex);
1468 goto out_try_module_get;
1471 if (atomic_inc_return(&cam->users) == 1) {
1472 omap24xxcam_hwinit(cam);
1473 if (omap24xxcam_sensor_enable(cam)) {
1474 mutex_unlock(&cam->mutex);
1475 goto out_omap24xxcam_sensor_enable;
1478 mutex_unlock(&cam->mutex);
1480 fh->cam = cam;
1481 mutex_lock(&cam->mutex);
1482 vidioc_int_g_fmt_cap(cam->sdev, &format);
1483 mutex_unlock(&cam->mutex);
1484 /* FIXME: how about fh->pix when there are more users? */
1485 fh->pix = format.fmt.pix;
1487 file->private_data = fh;
1489 spin_lock_init(&fh->vbq_lock);
1491 videobuf_queue_sg_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL,
1492 &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1493 V4L2_FIELD_NONE,
1494 sizeof(struct videobuf_buffer), fh, NULL);
1496 return 0;
1498 out_omap24xxcam_sensor_enable:
1499 omap24xxcam_poweron_reset(cam);
1500 module_put(cam->sdev->module);
1502 out_try_module_get:
1503 kfree(fh);
1505 return -ENODEV;
1508 static int omap24xxcam_release(struct file *file)
1510 struct omap24xxcam_fh *fh = file->private_data;
1511 struct omap24xxcam_device *cam = fh->cam;
1513 atomic_inc(&cam->reset_disable);
1515 flush_scheduled_work();
1517 /* stop streaming capture */
1518 videobuf_streamoff(&fh->vbq);
1520 mutex_lock(&cam->mutex);
1521 if (cam->streaming == file) {
1522 cam->streaming = NULL;
1523 mutex_unlock(&cam->mutex);
1524 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1525 } else {
1526 mutex_unlock(&cam->mutex);
1529 atomic_dec(&cam->reset_disable);
1531 omap24xxcam_vbq_free_mmap_buffers(&fh->vbq);
1534 * Make sure the reset work we might have scheduled is not
1535 * pending! It may be run *only* if we have users. (And it may
1536 * not be scheduled anymore since streaming is already
1537 * disabled.)
1539 flush_scheduled_work();
1541 mutex_lock(&cam->mutex);
1542 if (atomic_dec_return(&cam->users) == 0) {
1543 omap24xxcam_sensor_disable(cam);
1544 omap24xxcam_poweron_reset(cam);
1546 mutex_unlock(&cam->mutex);
1548 file->private_data = NULL;
1550 module_put(cam->sdev->module);
1551 kfree(fh);
1553 return 0;
1556 static struct v4l2_file_operations omap24xxcam_fops = {
1557 .ioctl = video_ioctl2,
1558 .poll = omap24xxcam_poll,
1559 .mmap = omap24xxcam_mmap,
1560 .open = omap24xxcam_open,
1561 .release = omap24xxcam_release,
1566 * Power management.
1570 #ifdef CONFIG_PM
1571 static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state)
1573 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1575 if (atomic_read(&cam->users) == 0)
1576 return 0;
1578 if (!atomic_read(&cam->reset_disable))
1579 omap24xxcam_capture_stop(cam);
1581 omap24xxcam_sensor_disable(cam);
1582 omap24xxcam_poweron_reset(cam);
1584 return 0;
1587 static int omap24xxcam_resume(struct platform_device *pdev)
1589 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1591 if (atomic_read(&cam->users) == 0)
1592 return 0;
1594 omap24xxcam_hwinit(cam);
1595 omap24xxcam_sensor_enable(cam);
1597 if (!atomic_read(&cam->reset_disable))
1598 omap24xxcam_capture_cont(cam);
1600 return 0;
1602 #endif /* CONFIG_PM */
1604 static const struct v4l2_ioctl_ops omap24xxcam_ioctl_fops = {
1605 .vidioc_querycap = vidioc_querycap,
1606 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1607 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1608 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1609 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1610 .vidioc_reqbufs = vidioc_reqbufs,
1611 .vidioc_querybuf = vidioc_querybuf,
1612 .vidioc_qbuf = vidioc_qbuf,
1613 .vidioc_dqbuf = vidioc_dqbuf,
1614 .vidioc_streamon = vidioc_streamon,
1615 .vidioc_streamoff = vidioc_streamoff,
1616 .vidioc_enum_input = vidioc_enum_input,
1617 .vidioc_g_input = vidioc_g_input,
1618 .vidioc_s_input = vidioc_s_input,
1619 .vidioc_queryctrl = vidioc_queryctrl,
1620 .vidioc_g_ctrl = vidioc_g_ctrl,
1621 .vidioc_s_ctrl = vidioc_s_ctrl,
1622 .vidioc_g_parm = vidioc_g_parm,
1623 .vidioc_s_parm = vidioc_s_parm,
1628 * Camera device (i.e. /dev/video).
1632 static int omap24xxcam_device_register(struct v4l2_int_device *s)
1634 struct omap24xxcam_device *cam = s->u.slave->master->priv;
1635 struct video_device *vfd;
1636 int rval;
1638 /* We already have a slave. */
1639 if (cam->sdev)
1640 return -EBUSY;
1642 cam->sdev = s;
1644 if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
1645 dev_err(cam->dev, "could not register sysfs entry\n");
1646 rval = -EBUSY;
1647 goto err;
1650 /* initialize the video_device struct */
1651 vfd = cam->vfd = video_device_alloc();
1652 if (!vfd) {
1653 dev_err(cam->dev, "could not allocate video device struct\n");
1654 rval = -ENOMEM;
1655 goto err;
1657 vfd->release = video_device_release;
1659 vfd->parent = cam->dev;
1661 strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1662 vfd->fops = &omap24xxcam_fops;
1663 vfd->ioctl_ops = &omap24xxcam_ioctl_fops;
1665 omap24xxcam_hwinit(cam);
1667 rval = omap24xxcam_sensor_init(cam);
1668 if (rval)
1669 goto err;
1671 if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1672 dev_err(cam->dev, "could not register V4L device\n");
1673 rval = -EBUSY;
1674 goto err;
1677 omap24xxcam_poweron_reset(cam);
1679 dev_info(cam->dev, "registered device %s\n",
1680 video_device_node_name(vfd));
1682 return 0;
1684 err:
1685 omap24xxcam_device_unregister(s);
1687 return rval;
1690 static void omap24xxcam_device_unregister(struct v4l2_int_device *s)
1692 struct omap24xxcam_device *cam = s->u.slave->master->priv;
1694 omap24xxcam_sensor_exit(cam);
1696 if (cam->vfd) {
1697 if (!video_is_registered(cam->vfd)) {
1699 * The device was never registered, so release the
1700 * video_device struct directly.
1702 video_device_release(cam->vfd);
1703 } else {
1705 * The unregister function will release the
1706 * video_device struct as well as
1707 * unregistering it.
1709 video_unregister_device(cam->vfd);
1711 cam->vfd = NULL;
1714 device_remove_file(cam->dev, &dev_attr_streaming);
1716 cam->sdev = NULL;
1719 static struct v4l2_int_master omap24xxcam_master = {
1720 .attach = omap24xxcam_device_register,
1721 .detach = omap24xxcam_device_unregister,
1724 static struct v4l2_int_device omap24xxcam = {
1725 .module = THIS_MODULE,
1726 .name = CAM_NAME,
1727 .type = v4l2_int_type_master,
1728 .u = {
1729 .master = &omap24xxcam_master
1735 * Driver initialisation and deinitialisation.
1739 static int __devinit omap24xxcam_probe(struct platform_device *pdev)
1741 struct omap24xxcam_device *cam;
1742 struct resource *mem;
1743 int irq;
1745 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1746 if (!cam) {
1747 dev_err(&pdev->dev, "could not allocate memory\n");
1748 goto err;
1751 platform_set_drvdata(pdev, cam);
1753 cam->dev = &pdev->dev;
1756 * Impose a lower limit on the amount of memory allocated for
1757 * capture. We require at least enough memory to double-buffer
1758 * QVGA (300KB).
1760 if (capture_mem < 320 * 240 * 2 * 2)
1761 capture_mem = 320 * 240 * 2 * 2;
1762 cam->capture_mem = capture_mem;
1764 /* request the mem region for the camera registers */
1765 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1766 if (!mem) {
1767 dev_err(cam->dev, "no mem resource?\n");
1768 goto err;
1770 if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
1771 pdev->name)) {
1772 dev_err(cam->dev,
1773 "cannot reserve camera register I/O region\n");
1774 goto err;
1776 cam->mmio_base_phys = mem->start;
1777 cam->mmio_size = (mem->end - mem->start) + 1;
1779 /* map the region */
1780 cam->mmio_base = (unsigned long)
1781 ioremap_nocache(cam->mmio_base_phys, cam->mmio_size);
1782 if (!cam->mmio_base) {
1783 dev_err(cam->dev, "cannot map camera register I/O region\n");
1784 goto err;
1787 irq = platform_get_irq(pdev, 0);
1788 if (irq <= 0) {
1789 dev_err(cam->dev, "no irq for camera?\n");
1790 goto err;
1793 /* install the interrupt service routine */
1794 if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) {
1795 dev_err(cam->dev,
1796 "could not install interrupt service routine\n");
1797 goto err;
1799 cam->irq = irq;
1801 if (omap24xxcam_clock_get(cam))
1802 goto err;
1804 INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work);
1806 mutex_init(&cam->mutex);
1807 spin_lock_init(&cam->core_enable_disable_lock);
1809 omap24xxcam_sgdma_init(&cam->sgdma,
1810 cam->mmio_base + CAMDMA_REG_OFFSET,
1811 omap24xxcam_stalled_dma_reset,
1812 (unsigned long)cam);
1814 omap24xxcam.priv = cam;
1816 if (v4l2_int_device_register(&omap24xxcam))
1817 goto err;
1819 return 0;
1821 err:
1822 omap24xxcam_remove(pdev);
1823 return -ENODEV;
1826 static int omap24xxcam_remove(struct platform_device *pdev)
1828 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1830 if (!cam)
1831 return 0;
1833 if (omap24xxcam.priv != NULL)
1834 v4l2_int_device_unregister(&omap24xxcam);
1835 omap24xxcam.priv = NULL;
1837 omap24xxcam_clock_put(cam);
1839 if (cam->irq) {
1840 free_irq(cam->irq, cam);
1841 cam->irq = 0;
1844 if (cam->mmio_base) {
1845 iounmap((void *)cam->mmio_base);
1846 cam->mmio_base = 0;
1849 if (cam->mmio_base_phys) {
1850 release_mem_region(cam->mmio_base_phys, cam->mmio_size);
1851 cam->mmio_base_phys = 0;
1854 kfree(cam);
1856 return 0;
1859 static struct platform_driver omap24xxcam_driver = {
1860 .probe = omap24xxcam_probe,
1861 .remove = omap24xxcam_remove,
1862 #ifdef CONFIG_PM
1863 .suspend = omap24xxcam_suspend,
1864 .resume = omap24xxcam_resume,
1865 #endif
1866 .driver = {
1867 .name = CAM_NAME,
1868 .owner = THIS_MODULE,
1874 * Module initialisation and deinitialisation
1878 static int __init omap24xxcam_init(void)
1880 return platform_driver_register(&omap24xxcam_driver);
1883 static void __exit omap24xxcam_cleanup(void)
1885 platform_driver_unregister(&omap24xxcam_driver);
1888 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
1889 MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
1890 MODULE_LICENSE("GPL");
1891 module_param(video_nr, int, 0);
1892 MODULE_PARM_DESC(video_nr,
1893 "Minor number for video device (-1 ==> auto assign)");
1894 module_param(capture_mem, int, 0);
1895 MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture "
1896 "buffers (default 4800kiB)");
1898 module_init(omap24xxcam_init);
1899 module_exit(omap24xxcam_cleanup);