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
29 #include <linux/delay.h>
30 #include <linux/kernel.h>
31 #include <linux/interrupt.h>
32 #include <linux/videodev2.h>
33 #include <linux/pci.h> /* needed for videobufs */
34 #include <linux/version.h>
35 #include <linux/platform_device.h>
36 #include <linux/clk.h>
39 #include <media/v4l2-common.h>
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;
68 static void omap24xxcam_clock_put(struct omap24xxcam_device *cam)
70 if (cam->ick != NULL && !IS_ERR(cam->ick))
72 if (cam->fck != NULL && !IS_ERR(cam->fck))
75 cam->ick = cam->fck = NULL;
78 static int omap24xxcam_clock_get(struct omap24xxcam_device *cam)
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);
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);
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);
121 * To disable xclk, use value zero.
123 static void omap24xxcam_core_xclk_set(const struct omap24xxcam_device *cam,
127 u32 divisor = CAM_MCLK / xclk;
130 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
132 CC_CTRL_XCLK_DIV_BYPASS);
134 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
135 CC_CTRL_XCLK, divisor);
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,
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
182 static void omap24xxcam_core_disable(const struct omap24xxcam_device *cam)
184 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
188 /* Interrupt service routine for camera core interrupts. */
189 static void omap24xxcam_core_isr(struct omap24xxcam_device *cam)
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,
201 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, 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",
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
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);
234 if (dma->sglist == NULL)
240 alloc_size = sg_dma_len(&dma->sglist[i]);
241 page = sg_page(&dma->sglist[i]);
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])));
253 /* Release all memory related to the videobuf_queue. */
254 static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue *vbq)
258 mutex_lock(&vbq->lock);
260 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
261 if (NULL == vbq->bufs[i])
263 if (V4L2_MEMORY_MMAP != vbq->bufs[i]->memory)
265 vbq->ops->buf_release(vbq, vbq->bufs[i]);
266 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
271 mutex_unlock(&vbq->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)
283 size_t alloc_size, size = vb->bsize; /* vb->bsize is page aligned */
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) {
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)
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) {
313 page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
314 if (page == NULL && !order) {
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);
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 */
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;
346 omap24xxcam_vbq_free_mmap_buffer(vb);
350 static int omap24xxcam_vbq_alloc_mmap_buffers(struct videobuf_queue *vbq,
354 struct omap24xxcam_fh *fh =
355 container_of(vbq, struct omap24xxcam_fh, vbq);
357 mutex_lock(&vbq->lock);
359 for (i = 0; i < count; i++) {
360 err = omap24xxcam_vbq_alloc_mmap_buffer(vbq->bufs[i]);
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->lock);
373 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
376 mutex_unlock(&vbq->lock);
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,
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;
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 = STATE_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);
411 vb->state = STATE_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,
425 dma->direction = DMA_NONE;
427 videobuf_dma_unmap(vbq, videobuf_to_dma(vb));
428 videobuf_dma_free(videobuf_to_dma(vb));
431 vb->state = STATE_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,
442 struct omap24xxcam_fh *fh = vbq->priv_data;
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)
459 static int omap24xxcam_dma_iolock(struct videobuf_queue *vbq,
460 struct videobuf_dmabuf *dma)
464 dma->direction = PCI_DMA_FROMDEVICE;
465 if (!dma_map_sg(vbq->dev, dma->sglist, dma->sglen, dma->direction)) {
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;
483 * Accessing pix here is okay since it's constant while
484 * streaming is on (and we only get called then).
487 /* This is a userspace buffer. */
488 if (fh->pix.sizeimage > vb->bsize) {
489 /* The buffer isn't big enough. */
492 vb->size = fh->pix.sizeimage;
494 if (vb->state != STATE_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
506 omap24xxcam_vbq_release(vbq, vb);
507 vb->size = fh->pix.sizeimage;
510 /* We need to allocate a new kernel bounce buffer. */
511 vb->size = fh->pix.sizeimage;
518 vb->width = fh->pix.width;
519 vb->height = fh->pix.height;
522 if (vb->state == STATE_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));
530 err = videobuf_iolock(vbq, vb, NULL);
534 vb->state = STATE_PREPARED;
536 omap24xxcam_vbq_release(vbq, vb);
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;
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 = STATE_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);
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);
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");
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,
605 CAM_SYSCONFIG_SOFTRESET);
607 /* Wait till it's finished */
608 while (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
609 & CAM_SYSSTATUS_RESETDONE)
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)
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);
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
693 static void omap24xxcam_capture_cont(struct omap24xxcam_device *cam)
697 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
699 if (atomic_read(&cam->in_reset) != 1)
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);
712 atomic_dec(&cam->in_reset);
713 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
717 omap24xxcam_streaming_show(struct device *dev, struct device_attribute *attr,
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;
742 unsigned int irqhandled = 0;
744 irqstatus = omap24xxcam_reg_in(cam->mmio_base, CAM_IRQSTATUS);
747 (CAM_IRQSTATUS_DMA_IRQ2 | CAM_IRQSTATUS_DMA_IRQ1
748 | CAM_IRQSTATUS_DMA_IRQ0)) {
749 omap24xxcam_dma_isr(&cam->sgdma.dma);
752 if (irqstatus & CAM_IRQSTATUS_CC_IRQ) {
753 omap24xxcam_core_isr(cam);
756 if (irqstatus & CAM_IRQSTATUS_MMU_IRQ)
757 dev_err(cam->dev, "unhandled camera MMU interrupt!\n");
759 return IRQ_RETVAL(irqhandled);
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)
776 struct v4l2_ifparm p;
778 rval = vidioc_int_g_ifparm(cam->sdev, &p);
780 dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
784 cam->if_type = p.if_type;
786 cam->cc_ctrl = CC_CTRL_CC_EN;
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;
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;
807 case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT:
808 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT10;
810 case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT:
811 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT12;
813 case V4L2_IF_TYPE_BT656_MODE_BT_8BIT:
814 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT8;
816 case V4L2_IF_TYPE_BT656_MODE_BT_10BIT:
817 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT10;
821 "bt656 interface mode %d not supported\n",
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;
840 divisor = CAM_MCLK / xclk;
841 if (divisor * xclk < CAM_MCLK)
843 if (CAM_MCLK / divisor < p.u.bt656.clock_min
849 xclk = CAM_MCLK / divisor;
851 if (xclk < p.u.bt656.clock_min
852 || xclk > p.u.bt656.clock_max)
855 cam->if_u.bt656.xclk = xclk;
857 omap24xxcam_core_xclk_set(cam, cam->if_u.bt656.xclk);
860 /* FIXME: how about other interfaces? */
861 dev_err(cam->dev, "interface type %d not supported\n",
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);
879 * Initialise the sensor hardware.
881 static int omap24xxcam_sensor_init(struct omap24xxcam_device *cam)
884 struct v4l2_int_device *sdev = cam->sdev;
886 omap24xxcam_clock_on(cam);
887 err = omap24xxcam_sensor_if_enable(cam);
889 dev_err(cam->dev, "sensor interface could not be enabled at "
890 "initialisation, %d\n", err);
895 /* power up sensor during sensor initialization */
896 vidioc_int_s_power(sdev, 1);
898 err = vidioc_int_dev_init(sdev);
900 dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
901 /* Sensor init failed --- it's nonexistent to us! */
906 dev_info(cam->dev, "sensor is %s\n", sdev->name);
909 omap24xxcam_sensor_if_disable(cam);
910 omap24xxcam_clock_off(cam);
912 vidioc_int_s_power(sdev, 0);
917 static void omap24xxcam_sensor_exit(struct omap24xxcam_device *cam)
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)
937 omap24xxcam_clock_on(cam);
939 omap24xxcam_sensor_if_enable(cam);
941 rval = vidioc_int_s_power(cam->sdev, 1);
945 rval = vidioc_int_init(cam->sdev);
952 omap24xxcam_sensor_disable(cam);
957 static void omap24xxcam_sensor_reset_work(struct work_struct *work)
959 struct omap24xxcam_device *cam =
960 container_of(work, struct omap24xxcam_device,
963 if (atomic_read(&cam->reset_disable))
966 omap24xxcam_capture_stop(cam);
968 if (vidioc_int_reset(cam->sdev) == 0) {
969 vidioc_int_init(cam->sdev);
971 /* Can't reset it by vidioc_int_reset. */
972 omap24xxcam_sensor_disable(cam);
973 omap24xxcam_sensor_enable(cam);
976 omap24xxcam_capture_cont(cam);
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;
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;
1006 rval = vidioc_int_enum_fmt_cap(cam->sdev, f);
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;
1018 mutex_lock(&cam->mutex);
1019 rval = vidioc_int_g_fmt_cap(cam->sdev, f);
1020 mutex_unlock(&cam->mutex);
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;
1032 mutex_lock(&cam->mutex);
1033 if (cam->streaming) {
1038 rval = vidioc_int_s_fmt_cap(cam->sdev, f);
1041 mutex_unlock(&cam->mutex);
1044 mutex_lock(&ofh->vbq.lock);
1045 ofh->pix = f->fmt.pix;
1046 mutex_unlock(&ofh->vbq.lock);
1049 memset(f, 0, sizeof(*f));
1050 vidioc_g_fmt_cap(file, fh, f);
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;
1062 mutex_lock(&cam->mutex);
1063 rval = vidioc_int_try_fmt_cap(cam->sdev, f);
1064 mutex_unlock(&cam->mutex);
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;
1076 mutex_lock(&cam->mutex);
1077 if (cam->streaming) {
1078 mutex_unlock(&cam->mutex);
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)
1094 rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, rval);
1096 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
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;
1124 rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
1128 vb = ofh->vbq.bufs[b->index];
1130 mutex_lock(&cam->mutex);
1131 /* _needs_reset returns -EIO if reset is required. */
1132 rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr);
1133 mutex_unlock(&cam->mutex);
1135 schedule_work(&cam->sensor_reset_work);
1141 * This is a hack. User space won't get the index of this
1142 * buffer and does not want to requeue it so we requeue it
1146 videobuf_qbuf(&ofh->vbq, b);
1151 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1153 struct omap24xxcam_fh *ofh = fh;
1154 struct omap24xxcam_device *cam = ofh->cam;
1157 mutex_lock(&cam->mutex);
1158 if (cam->streaming) {
1163 rval = omap24xxcam_sensor_if_enable(cam);
1165 dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n");
1169 rval = videobuf_streamon(&ofh->vbq);
1171 cam->streaming = file;
1172 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1176 mutex_unlock(&cam->mutex);
1181 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1183 struct omap24xxcam_fh *ofh = fh;
1184 struct omap24xxcam_device *cam = ofh->cam;
1185 struct videobuf_queue *q = &ofh->vbq;
1188 atomic_inc(&cam->reset_disable);
1190 flush_scheduled_work();
1192 rval = videobuf_streamoff(q);
1194 mutex_lock(&cam->mutex);
1195 cam->streaming = NULL;
1196 mutex_unlock(&cam->mutex);
1197 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1200 atomic_dec(&cam->reset_disable);
1205 static int vidioc_enum_input(struct file *file, void *fh,
1206 struct v4l2_input *inp)
1211 strlcpy(inp->name, "camera", sizeof(inp->name));
1212 inp->type = V4L2_INPUT_TYPE_CAMERA;
1217 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
1224 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
1232 static int vidioc_queryctrl(struct file *file, void *fh,
1233 struct v4l2_queryctrl *a)
1235 struct omap24xxcam_fh *ofh = fh;
1236 struct omap24xxcam_device *cam = ofh->cam;
1239 rval = vidioc_int_queryctrl(cam->sdev, a);
1244 static int vidioc_g_ctrl(struct file *file, void *fh,
1245 struct v4l2_control *a)
1247 struct omap24xxcam_fh *ofh = fh;
1248 struct omap24xxcam_device *cam = ofh->cam;
1251 mutex_lock(&cam->mutex);
1252 rval = vidioc_int_g_ctrl(cam->sdev, a);
1253 mutex_unlock(&cam->mutex);
1258 static int vidioc_s_ctrl(struct file *file, void *fh,
1259 struct v4l2_control *a)
1261 struct omap24xxcam_fh *ofh = fh;
1262 struct omap24xxcam_device *cam = ofh->cam;
1265 mutex_lock(&cam->mutex);
1266 rval = vidioc_int_s_ctrl(cam->sdev, a);
1267 mutex_unlock(&cam->mutex);
1272 static int vidioc_g_parm(struct file *file, void *fh,
1273 struct v4l2_streamparm *a) {
1274 struct omap24xxcam_fh *ofh = fh;
1275 struct omap24xxcam_device *cam = ofh->cam;
1278 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1281 mutex_lock(&cam->mutex);
1282 rval = vidioc_int_g_parm(cam->sdev, a);
1283 mutex_unlock(&cam->mutex);
1288 static int vidioc_s_parm(struct file *file, void *fh,
1289 struct v4l2_streamparm *a)
1291 struct omap24xxcam_fh *ofh = fh;
1292 struct omap24xxcam_device *cam = ofh->cam;
1293 struct v4l2_streamparm old_streamparm;
1296 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1299 mutex_lock(&cam->mutex);
1300 if (cam->streaming) {
1305 old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1306 rval = vidioc_int_g_parm(cam->sdev, &old_streamparm);
1310 rval = vidioc_int_s_parm(cam->sdev, a);
1314 rval = omap24xxcam_sensor_if_enable(cam);
1316 * Revert to old streaming parameters if enabling sensor
1317 * interface with the new ones failed.
1320 vidioc_int_s_parm(cam->sdev, &old_streamparm);
1323 mutex_unlock(&cam->mutex);
1334 static unsigned int omap24xxcam_poll(struct file *file,
1335 struct poll_table_struct *wait)
1337 struct omap24xxcam_fh *fh = file->private_data;
1338 struct omap24xxcam_device *cam = fh->cam;
1339 struct videobuf_buffer *vb;
1341 mutex_lock(&cam->mutex);
1342 if (cam->streaming != file) {
1343 mutex_unlock(&cam->mutex);
1346 mutex_unlock(&cam->mutex);
1348 mutex_lock(&fh->vbq.lock);
1349 if (list_empty(&fh->vbq.stream)) {
1350 mutex_unlock(&fh->vbq.lock);
1353 vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
1354 mutex_unlock(&fh->vbq.lock);
1356 poll_wait(file, &vb->done, wait);
1358 if (vb->state == STATE_DONE || vb->state == STATE_ERROR)
1359 return POLLIN | POLLRDNORM;
1364 static int omap24xxcam_mmap_buffers(struct file *file,
1365 struct vm_area_struct *vma)
1367 struct omap24xxcam_fh *fh = file->private_data;
1368 struct omap24xxcam_device *cam = fh->cam;
1369 struct videobuf_queue *vbq = &fh->vbq;
1370 unsigned int first, last, size, i, j;
1373 mutex_lock(&cam->mutex);
1374 if (cam->streaming) {
1375 mutex_unlock(&cam->mutex);
1378 mutex_unlock(&cam->mutex);
1379 mutex_lock(&vbq->lock);
1381 /* look for first buffer to map */
1382 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1383 if (NULL == vbq->bufs[first])
1385 if (V4L2_MEMORY_MMAP != vbq->bufs[first]->memory)
1387 if (vbq->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1391 /* look for last buffer to map */
1392 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1393 if (NULL == vbq->bufs[last])
1395 if (V4L2_MEMORY_MMAP != vbq->bufs[last]->memory)
1397 size += vbq->bufs[last]->bsize;
1398 if (size == (vma->vm_end - vma->vm_start))
1403 for (i = first; i <= last; i++) {
1404 struct videobuf_dmabuf *dma = videobuf_to_dma(vbq->bufs[i]);
1406 for (j = 0; j < dma->sglen; j++) {
1407 err = remap_pfn_range(
1408 vma, vma->vm_start + size,
1409 page_to_pfn(sg_page(&dma->sglist[j])),
1410 sg_dma_len(&dma->sglist[j]), vma->vm_page_prot);
1413 size += sg_dma_len(&dma->sglist[j]);
1418 mutex_unlock(&vbq->lock);
1423 static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma)
1425 struct omap24xxcam_fh *fh = file->private_data;
1428 /* let the video-buf mapper check arguments and set-up structures */
1429 rval = videobuf_mmap_mapper(&fh->vbq, vma);
1433 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1435 /* do mapping to our allocated buffers */
1436 rval = omap24xxcam_mmap_buffers(file, vma);
1438 * In case of error, free vma->vm_private_data allocated by
1439 * videobuf_mmap_mapper.
1442 kfree(vma->vm_private_data);
1447 static int omap24xxcam_open(struct inode *inode, struct file *file)
1449 int minor = iminor(inode);
1450 struct omap24xxcam_device *cam = omap24xxcam.priv;
1451 struct omap24xxcam_fh *fh;
1452 struct v4l2_format format;
1454 if (!cam || !cam->vfd || (cam->vfd->minor != minor))
1457 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1461 mutex_lock(&cam->mutex);
1462 if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
1463 mutex_unlock(&cam->mutex);
1464 goto out_try_module_get;
1467 if (atomic_inc_return(&cam->users) == 1) {
1468 omap24xxcam_hwinit(cam);
1469 if (omap24xxcam_sensor_enable(cam)) {
1470 mutex_unlock(&cam->mutex);
1471 goto out_omap24xxcam_sensor_enable;
1474 mutex_unlock(&cam->mutex);
1477 mutex_lock(&cam->mutex);
1478 vidioc_int_g_fmt_cap(cam->sdev, &format);
1479 mutex_unlock(&cam->mutex);
1480 /* FIXME: how about fh->pix when there are more users? */
1481 fh->pix = format.fmt.pix;
1483 file->private_data = fh;
1485 spin_lock_init(&fh->vbq_lock);
1487 videobuf_queue_pci_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL,
1488 &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1490 sizeof(struct videobuf_buffer), fh);
1494 out_omap24xxcam_sensor_enable:
1495 omap24xxcam_poweron_reset(cam);
1496 module_put(cam->sdev->module);
1504 static int omap24xxcam_release(struct inode *inode, struct file *file)
1506 struct omap24xxcam_fh *fh = file->private_data;
1507 struct omap24xxcam_device *cam = fh->cam;
1509 atomic_inc(&cam->reset_disable);
1511 flush_scheduled_work();
1513 mutex_lock(&cam->mutex);
1514 /* stop streaming capture */
1515 if (cam->streaming == file) {
1516 cam->streaming = NULL;
1517 mutex_unlock(&cam->mutex);
1518 videobuf_streamoff(&fh->vbq);
1519 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1521 mutex_unlock(&cam->mutex);
1524 atomic_dec(&cam->reset_disable);
1526 omap24xxcam_vbq_free_mmap_buffers(&fh->vbq);
1529 * Make sure the reset work we might have scheduled is not
1530 * pending! It may be run *only* if we have users. (And it may
1531 * not be scheduled anymore since streaming is already
1534 flush_scheduled_work();
1536 mutex_lock(&cam->mutex);
1537 if (atomic_dec_return(&cam->users) == 0) {
1538 omap24xxcam_sensor_disable(cam);
1539 omap24xxcam_poweron_reset(cam);
1541 mutex_unlock(&cam->mutex);
1543 file->private_data = NULL;
1545 module_put(cam->sdev->module);
1551 static struct file_operations omap24xxcam_fops = {
1552 .owner = THIS_MODULE,
1553 .llseek = no_llseek,
1554 .ioctl = video_ioctl2,
1555 .poll = omap24xxcam_poll,
1556 .mmap = omap24xxcam_mmap,
1557 .open = omap24xxcam_open,
1558 .release = omap24xxcam_release,
1568 static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state)
1570 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1572 if (atomic_read(&cam->users) == 0)
1575 if (!atomic_read(&cam->reset_disable))
1576 omap24xxcam_capture_stop(cam);
1578 omap24xxcam_sensor_disable(cam);
1579 omap24xxcam_poweron_reset(cam);
1584 static int omap24xxcam_resume(struct platform_device *pdev)
1586 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1588 if (atomic_read(&cam->users) == 0)
1591 omap24xxcam_hwinit(cam);
1592 omap24xxcam_sensor_enable(cam);
1594 if (!atomic_read(&cam->reset_disable))
1595 omap24xxcam_capture_cont(cam);
1599 #endif /* CONFIG_PM */
1603 * Camera device (i.e. /dev/video).
1607 static int omap24xxcam_device_register(struct v4l2_int_device *s)
1609 struct omap24xxcam_device *cam = s->u.slave->master->priv;
1610 struct video_device *vfd;
1613 /* We already have a slave. */
1619 if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
1620 dev_err(cam->dev, "could not register sysfs entry\n");
1625 /* initialize the video_device struct */
1626 vfd = cam->vfd = video_device_alloc();
1628 dev_err(cam->dev, "could not allocate video device struct\n");
1632 vfd->release = video_device_release;
1634 vfd->dev = cam->dev;
1636 strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1637 vfd->type = VID_TYPE_CAPTURE | VID_TYPE_CHROMAKEY;
1638 vfd->fops = &omap24xxcam_fops;
1642 vfd->vidioc_querycap = vidioc_querycap;
1643 vfd->vidioc_enum_fmt_cap = vidioc_enum_fmt_cap;
1644 vfd->vidioc_g_fmt_cap = vidioc_g_fmt_cap;
1645 vfd->vidioc_s_fmt_cap = vidioc_s_fmt_cap;
1646 vfd->vidioc_try_fmt_cap = vidioc_try_fmt_cap;
1647 vfd->vidioc_reqbufs = vidioc_reqbufs;
1648 vfd->vidioc_querybuf = vidioc_querybuf;
1649 vfd->vidioc_qbuf = vidioc_qbuf;
1650 vfd->vidioc_dqbuf = vidioc_dqbuf;
1651 vfd->vidioc_streamon = vidioc_streamon;
1652 vfd->vidioc_streamoff = vidioc_streamoff;
1653 vfd->vidioc_enum_input = vidioc_enum_input;
1654 vfd->vidioc_g_input = vidioc_g_input;
1655 vfd->vidioc_s_input = vidioc_s_input;
1656 vfd->vidioc_queryctrl = vidioc_queryctrl;
1657 vfd->vidioc_g_ctrl = vidioc_g_ctrl;
1658 vfd->vidioc_s_ctrl = vidioc_s_ctrl;
1659 vfd->vidioc_g_parm = vidioc_g_parm;
1660 vfd->vidioc_s_parm = vidioc_s_parm;
1662 omap24xxcam_hwinit(cam);
1664 rval = omap24xxcam_sensor_init(cam);
1668 if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1669 dev_err(cam->dev, "could not register V4L device\n");
1675 omap24xxcam_poweron_reset(cam);
1677 dev_info(cam->dev, "registered device video%d\n", vfd->minor);
1682 omap24xxcam_device_unregister(s);
1687 static void omap24xxcam_device_unregister(struct v4l2_int_device *s)
1689 struct omap24xxcam_device *cam = s->u.slave->master->priv;
1691 omap24xxcam_sensor_exit(cam);
1694 if (cam->vfd->minor == -1) {
1696 * The device was never registered, so release the
1697 * video_device struct directly.
1699 video_device_release(cam->vfd);
1702 * The unregister function will release the
1703 * video_device struct as well as
1706 video_unregister_device(cam->vfd);
1711 device_remove_file(cam->dev, &dev_attr_streaming);
1716 static struct v4l2_int_master omap24xxcam_master = {
1717 .attach = omap24xxcam_device_register,
1718 .detach = omap24xxcam_device_unregister,
1721 static struct v4l2_int_device omap24xxcam = {
1722 .module = THIS_MODULE,
1724 .type = v4l2_int_type_master,
1726 .master = &omap24xxcam_master
1732 * Driver initialisation and deinitialisation.
1736 static int __init omap24xxcam_probe(struct platform_device *pdev)
1738 struct omap24xxcam_device *cam;
1739 struct resource *mem;
1742 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1744 dev_err(&pdev->dev, "could not allocate memory\n");
1748 platform_set_drvdata(pdev, cam);
1750 cam->dev = &pdev->dev;
1753 * Impose a lower limit on the amount of memory allocated for
1754 * capture. We require at least enough memory to double-buffer
1757 if (capture_mem < 320 * 240 * 2 * 2)
1758 capture_mem = 320 * 240 * 2 * 2;
1759 cam->capture_mem = capture_mem;
1761 /* request the mem region for the camera registers */
1762 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1764 dev_err(cam->dev, "no mem resource?\n");
1767 if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
1770 "cannot reserve camera register I/O region\n");
1773 cam->mmio_base_phys = mem->start;
1774 cam->mmio_size = (mem->end - mem->start) + 1;
1776 /* map the region */
1777 cam->mmio_base = (unsigned long)
1778 ioremap_nocache(cam->mmio_base_phys, cam->mmio_size);
1779 if (!cam->mmio_base) {
1780 dev_err(cam->dev, "cannot map camera register I/O region\n");
1784 irq = platform_get_irq(pdev, 0);
1786 dev_err(cam->dev, "no irq for camera?\n");
1790 /* install the interrupt service routine */
1791 if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) {
1793 "could not install interrupt service routine\n");
1798 if (omap24xxcam_clock_get(cam))
1801 INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work);
1803 mutex_init(&cam->mutex);
1804 spin_lock_init(&cam->core_enable_disable_lock);
1806 omap24xxcam_sgdma_init(&cam->sgdma,
1807 cam->mmio_base + CAMDMA_REG_OFFSET,
1808 omap24xxcam_stalled_dma_reset,
1809 (unsigned long)cam);
1811 omap24xxcam.priv = cam;
1813 if (v4l2_int_device_register(&omap24xxcam))
1819 omap24xxcam_remove(pdev);
1823 static int omap24xxcam_remove(struct platform_device *pdev)
1825 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1830 if (omap24xxcam.priv != NULL)
1831 v4l2_int_device_unregister(&omap24xxcam);
1832 omap24xxcam.priv = NULL;
1834 omap24xxcam_clock_put(cam);
1837 free_irq(cam->irq, cam);
1841 if (cam->mmio_base) {
1842 iounmap((void *)cam->mmio_base);
1846 if (cam->mmio_base_phys) {
1847 release_mem_region(cam->mmio_base_phys, cam->mmio_size);
1848 cam->mmio_base_phys = 0;
1856 static struct platform_driver omap24xxcam_driver = {
1857 .probe = omap24xxcam_probe,
1858 .remove = omap24xxcam_remove,
1860 .suspend = omap24xxcam_suspend,
1861 .resume = omap24xxcam_resume,
1870 * Module initialisation and deinitialisation
1874 static int __init omap24xxcam_init(void)
1876 return platform_driver_register(&omap24xxcam_driver);
1879 static void __exit omap24xxcam_cleanup(void)
1881 platform_driver_unregister(&omap24xxcam_driver);
1884 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
1885 MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
1886 MODULE_LICENSE("GPL");
1887 module_param(video_nr, int, 0);
1888 MODULE_PARM_DESC(video_nr,
1889 "Minor number for video device (-1 ==> auto assign)");
1890 module_param(capture_mem, int, 0);
1891 MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture "
1892 "buffers (default 4800kiB)");
1894 module_init(omap24xxcam_init);
1895 module_exit(omap24xxcam_cleanup);