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 *ctl);
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)
233 if (vb->dma.sglist == NULL)
239 alloc_size = vb->dma.sglist[i].length;
240 page = vb->dma.sglist[i].page;
242 ClearPageReserved(page++);
243 } while (alloc_size -= PAGE_SIZE);
244 __free_pages(vb->dma.sglist[i].page,
245 get_order(vb->dma.sglist[i].length));
248 kfree(vb->dma.sglist);
249 vb->dma.sglist = NULL;
252 /* Release all memory related to the videobuf_queue. */
253 static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue *vbq)
257 mutex_lock(&vbq->lock);
259 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
260 if (NULL == vbq->bufs[i])
262 if (V4L2_MEMORY_MMAP != vbq->bufs[i]->memory)
264 vbq->ops->buf_release(vbq, vbq->bufs[i]);
265 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
270 mutex_unlock(&vbq->lock);
272 videobuf_mmap_free(vbq);
276 * Allocate physically as contiguous as possible buffer for video
277 * frame and allocate and build DMA scatter-gather list for it.
279 static int omap24xxcam_vbq_alloc_mmap_buffer(struct videobuf_buffer *vb)
282 size_t alloc_size, size = vb->bsize; /* vb->bsize is page aligned */
284 int max_pages, err = 0, i = 0;
287 * allocate maximum size scatter-gather list. Note this is
288 * overhead. We may not use as many entries as we allocate
290 max_pages = vb->bsize >> PAGE_SHIFT;
292 kcalloc(max_pages, sizeof(*vb->dma.sglist), GFP_KERNEL);
293 if (vb->dma.sglist == NULL) {
299 order = get_order(size);
301 * do not over-allocate even if we would get larger
302 * contiguous chunk that way
304 if ((PAGE_SIZE << order) > size)
307 /* try to allocate as many contiguous pages as possible */
308 page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
309 /* if allocation fails, try to allocate smaller amount */
310 while (page == NULL) {
312 page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
313 if (page == NULL && !order) {
318 size -= (PAGE_SIZE << order);
320 /* append allocated chunk of pages into scatter-gather list */
321 vb->dma.sglist[i].page = page;
322 vb->dma.sglist[i].length = (PAGE_SIZE << order);
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 vb->dma.nr_pages = vb->dma.sglen;
341 vb->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 vbq->bufs[i]->dma.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 = &vb->dma;
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, &vb->dma);
428 videobuf_dma_free(&vb->dma);
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, &vb->dma);
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, vb->dma.sglist,
558 vb->dma.sglen, vb->size,
559 omap24xxcam_vbq_complete, vb);
562 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
563 if (++cam->sgdma_in_queue == 1
564 && !atomic_read(&cam->in_reset))
565 omap24xxcam_core_enable(cam);
566 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
569 * Oops. We're not supposed to get any errors here.
570 * The only way we could get an error is if we ran out
571 * of scatter-gather DMA slots, but we are supposed to
572 * have at least as many scatter-gather DMA slots as
573 * video buffers so that can't happen.
575 dev_err(cam->dev, "failed to queue a video buffer for dma!\n");
576 dev_err(cam->dev, "likely a bug in the driver!\n");
581 static struct videobuf_queue_ops omap24xxcam_vbq_ops = {
582 .buf_setup = omap24xxcam_vbq_setup,
583 .buf_prepare = omap24xxcam_vbq_prepare,
584 .buf_queue = omap24xxcam_vbq_queue,
585 .buf_release = omap24xxcam_vbq_release,
590 * OMAP main camera system
595 * Reset camera block to power-on state.
597 static void omap24xxcam_poweron_reset(const struct omap24xxcam_device *cam)
599 int max_loop = RESET_TIMEOUT_NS;
601 /* Reset whole camera subsystem */
602 omap24xxcam_reg_out(cam->mmio_base,
604 CAM_SYSCONFIG_SOFTRESET);
606 /* Wait till it's finished */
607 while (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
608 & CAM_SYSSTATUS_RESETDONE)
613 if (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
614 & CAM_SYSSTATUS_RESETDONE))
615 dev_err(cam->dev, "camera soft reset timeout\n");
619 * (Re)initialise the camera block.
621 static void omap24xxcam_hwinit(const struct omap24xxcam_device *cam)
623 omap24xxcam_poweron_reset(cam);
625 /* set the camera subsystem autoidle bit */
626 omap24xxcam_reg_out(cam->mmio_base, CAM_SYSCONFIG,
627 CAM_SYSCONFIG_AUTOIDLE);
629 /* set the camera MMU autoidle bit */
630 omap24xxcam_reg_out(cam->mmio_base,
631 CAMMMU_REG_OFFSET + CAMMMU_SYSCONFIG,
632 CAMMMU_SYSCONFIG_AUTOIDLE);
634 omap24xxcam_core_hwinit(cam);
636 omap24xxcam_dma_hwinit(&cam->sgdma.dma);
640 * Callback for dma transfer stalling.
642 static void omap24xxcam_stalled_dma_reset(unsigned long data)
644 struct omap24xxcam_device *cam = (struct omap24xxcam_device *)data;
646 if (!atomic_read(&cam->in_reset)) {
647 dev_dbg(cam->dev, "dma stalled, resetting camera\n");
648 omap24xxcam_reset(cam);
653 * Stop capture. Mark we're doing a reset, stop DMA transfers and
654 * core. (No new scatter-gather transfers will be queued whilst
655 * in_reset is non-zero.)
657 * If omap24xxcam_capture_stop is called from several places at
658 * once, only the first call will have an effect. Similarly, the last
659 * call omap24xxcam_streaming_cont will have effect.
661 * Serialisation is ensured by using cam->core_enable_disable_lock.
663 static void omap24xxcam_capture_stop(struct omap24xxcam_device *cam)
667 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
669 if (atomic_inc_return(&cam->in_reset) != 1) {
670 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
674 omap24xxcam_core_disable(cam);
676 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
678 omap24xxcam_sgdma_sync(&cam->sgdma);
682 * Reset and continue streaming.
684 * Note: Resetting the camera FIFO via the CC_RST bit in the CC_CTRL
685 * register is supposed to be sufficient to recover from a camera
686 * interface error, but it doesn't seem to be enough. If we only do
687 * that then subsequent image captures are out of sync by either one
688 * or two times DMA_THRESHOLD bytes. Resetting and re-initializing the
689 * entire camera subsystem prevents the problem with frame
692 static void omap24xxcam_capture_cont(struct omap24xxcam_device *cam)
696 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
698 if (atomic_read(&cam->in_reset) != 1)
701 omap24xxcam_hwinit(cam);
703 omap24xxcam_sensor_if_enable(cam);
705 omap24xxcam_sgdma_process(&cam->sgdma);
707 if (cam->sgdma_in_queue)
708 omap24xxcam_core_enable(cam);
711 atomic_dec(&cam->in_reset);
712 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
716 omap24xxcam_streaming_show(struct device *dev, struct device_attribute *attr,
719 struct omap24xxcam_device *cam = dev_get_drvdata(dev);
721 return sprintf(buf, "%s\n", cam->streaming ? "active" : "inactive");
723 static DEVICE_ATTR(streaming, S_IRUGO, omap24xxcam_streaming_show, NULL);
726 * Stop capture and restart it. I.e. reset the camera during use.
728 static void omap24xxcam_reset(struct omap24xxcam_device *cam)
730 omap24xxcam_capture_stop(cam);
731 omap24xxcam_capture_cont(cam);
735 * The main interrupt handler.
737 static irqreturn_t omap24xxcam_isr(int irq, void *arg)
739 struct omap24xxcam_device *cam = (struct omap24xxcam_device *)arg;
741 unsigned int irqhandled = 0;
743 irqstatus = omap24xxcam_reg_in(cam->mmio_base, CAM_IRQSTATUS);
746 (CAM_IRQSTATUS_DMA_IRQ2 | CAM_IRQSTATUS_DMA_IRQ1
747 | CAM_IRQSTATUS_DMA_IRQ0)) {
748 omap24xxcam_dma_isr(&cam->sgdma.dma);
751 if (irqstatus & CAM_IRQSTATUS_CC_IRQ) {
752 omap24xxcam_core_isr(cam);
755 if (irqstatus & CAM_IRQSTATUS_MMU_IRQ)
756 dev_err(cam->dev, "unhandled camera MMU interrupt!\n");
758 return IRQ_RETVAL(irqhandled);
768 * Enable the external sensor interface. Try to negotiate interface
769 * parameters with the sensor and start using the new ones. The calls
770 * to sensor_if_enable and sensor_if_disable need not to be balanced.
772 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam)
775 struct v4l2_ifparm p;
777 rval = vidioc_int_g_ifparm(cam->sdev, &p);
779 dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
783 cam->if_type = p.if_type;
785 cam->cc_ctrl = CC_CTRL_CC_EN;
788 case V4L2_IF_TYPE_BT656:
789 if (p.u.bt656.frame_start_on_rising_vs)
790 cam->cc_ctrl |= CC_CTRL_NOBT_SYNCHRO;
791 if (p.u.bt656.bt_sync_correct)
792 cam->cc_ctrl |= CC_CTRL_BT_CORRECT;
794 cam->cc_ctrl |= CC_CTRL_PAR_ORDERCAM;
795 if (p.u.bt656.latch_clk_inv)
796 cam->cc_ctrl |= CC_CTRL_PAR_CLK_POL;
797 if (p.u.bt656.nobt_hs_inv)
798 cam->cc_ctrl |= CC_CTRL_NOBT_HS_POL;
799 if (p.u.bt656.nobt_vs_inv)
800 cam->cc_ctrl |= CC_CTRL_NOBT_VS_POL;
802 switch (p.u.bt656.mode) {
803 case V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT:
804 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT8;
806 case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT:
807 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT10;
809 case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT:
810 cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT12;
812 case V4L2_IF_TYPE_BT656_MODE_BT_8BIT:
813 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT8;
815 case V4L2_IF_TYPE_BT656_MODE_BT_10BIT:
816 cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT10;
820 "bt656 interface mode %d not supported\n",
825 * The clock rate that the sensor wants has changed.
826 * We have to adjust the xclk from OMAP 2 side to
827 * match the sensor's wish as closely as possible.
829 if (p.u.bt656.clock_curr != cam->if_u.bt656.xclk) {
830 u32 xclk = p.u.bt656.clock_curr;
839 divisor = CAM_MCLK / xclk;
840 if (divisor * xclk < CAM_MCLK)
842 if (CAM_MCLK / divisor < p.u.bt656.clock_min
848 xclk = CAM_MCLK / divisor;
850 if (xclk < p.u.bt656.clock_min
851 || xclk > p.u.bt656.clock_max)
854 cam->if_u.bt656.xclk = xclk;
856 omap24xxcam_core_xclk_set(cam, cam->if_u.bt656.xclk);
859 /* FIXME: how about other interfaces? */
860 dev_err(cam->dev, "interface type %d not supported\n",
868 static void omap24xxcam_sensor_if_disable(const struct omap24xxcam_device *cam)
870 switch (cam->if_type) {
871 case V4L2_IF_TYPE_BT656:
872 omap24xxcam_core_xclk_set(cam, 0);
878 * Initialise the sensor hardware.
880 static int omap24xxcam_sensor_init(struct omap24xxcam_device *cam)
883 struct v4l2_int_device *sdev = cam->sdev;
885 omap24xxcam_clock_on(cam);
886 err = omap24xxcam_sensor_if_enable(cam);
888 dev_err(cam->dev, "sensor interface could not be enabled at "
889 "initialisation, %d\n", err);
894 /* power up sensor during sensor initialization */
895 vidioc_int_s_power(sdev, 1);
897 err = vidioc_int_dev_init(sdev);
899 dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
900 /* Sensor init failed --- it's nonexistent to us! */
905 dev_info(cam->dev, "sensor is %s\n", sdev->name);
908 omap24xxcam_sensor_if_disable(cam);
909 omap24xxcam_clock_off(cam);
911 vidioc_int_s_power(sdev, 0);
916 static void omap24xxcam_sensor_exit(struct omap24xxcam_device *cam)
919 vidioc_int_dev_exit(cam->sdev);
922 static void omap24xxcam_sensor_disable(struct omap24xxcam_device *cam)
924 omap24xxcam_sensor_if_disable(cam);
925 omap24xxcam_clock_off(cam);
926 vidioc_int_s_power(cam->sdev, 0);
930 * Power-up and configure camera sensor. It's ready for capturing now.
932 static int omap24xxcam_sensor_enable(struct omap24xxcam_device *cam)
936 omap24xxcam_clock_on(cam);
938 omap24xxcam_sensor_if_enable(cam);
940 rval = vidioc_int_s_power(cam->sdev, 1);
944 rval = vidioc_int_init(cam->sdev);
951 omap24xxcam_sensor_disable(cam);
956 static void omap24xxcam_sensor_reset_work(struct work_struct *work)
958 struct omap24xxcam_device *cam =
959 container_of(work, struct omap24xxcam_device,
962 if (atomic_read(&cam->reset_disable))
965 omap24xxcam_capture_stop(cam);
967 if (vidioc_int_reset(cam->sdev) == 0) {
968 vidioc_int_init(cam->sdev);
970 /* Can't reset it by vidioc_int_reset. */
971 omap24xxcam_sensor_disable(cam);
972 omap24xxcam_sensor_enable(cam);
975 omap24xxcam_capture_cont(cam);
984 static int vidioc_querycap(struct file *file, void *fh,
985 struct v4l2_capability *cap)
987 struct omap24xxcam_fh *ofh = fh;
988 struct omap24xxcam_device *cam = ofh->cam;
990 strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
991 strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
992 cap->version = OMAP24XXCAM_VERSION;
993 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
998 static int vidioc_enum_fmt_cap(struct file *file, void *fh,
999 struct v4l2_fmtdesc *f)
1001 struct omap24xxcam_fh *ofh = fh;
1002 struct omap24xxcam_device *cam = ofh->cam;
1005 rval = vidioc_int_enum_fmt_cap(cam->sdev, f);
1010 static int vidioc_g_fmt_cap(struct file *file, void *fh,
1011 struct v4l2_format *f)
1013 struct omap24xxcam_fh *ofh = fh;
1014 struct omap24xxcam_device *cam = ofh->cam;
1017 mutex_lock(&cam->mutex);
1018 rval = vidioc_int_g_fmt_cap(cam->sdev, f);
1019 mutex_unlock(&cam->mutex);
1024 static int vidioc_s_fmt_cap(struct file *file, void *fh,
1025 struct v4l2_format *f)
1027 struct omap24xxcam_fh *ofh = fh;
1028 struct omap24xxcam_device *cam = ofh->cam;
1031 mutex_lock(&cam->mutex);
1032 if (cam->streaming) {
1037 rval = vidioc_int_s_fmt_cap(cam->sdev, f);
1040 mutex_unlock(&cam->mutex);
1043 mutex_lock(&ofh->vbq.lock);
1044 ofh->pix = f->fmt.pix;
1045 mutex_unlock(&ofh->vbq.lock);
1048 memset(f, 0, sizeof(*f));
1049 vidioc_g_fmt_cap(file, fh, f);
1054 static int vidioc_try_fmt_cap(struct file *file, void *fh,
1055 struct v4l2_format *f)
1057 struct omap24xxcam_fh *ofh = fh;
1058 struct omap24xxcam_device *cam = ofh->cam;
1061 mutex_lock(&cam->mutex);
1062 rval = vidioc_int_try_fmt_cap(cam->sdev, f);
1063 mutex_unlock(&cam->mutex);
1068 static int vidioc_reqbufs(struct file *file, void *fh,
1069 struct v4l2_requestbuffers *b)
1071 struct omap24xxcam_fh *ofh = fh;
1072 struct omap24xxcam_device *cam = ofh->cam;
1075 mutex_lock(&cam->mutex);
1076 if (cam->streaming) {
1077 mutex_unlock(&cam->mutex);
1081 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1082 mutex_unlock(&cam->mutex);
1084 rval = videobuf_reqbufs(&ofh->vbq, b);
1087 * Either videobuf_reqbufs failed or the buffers are not
1088 * memory-mapped (which would need special attention).
1090 if (rval || b->memory != V4L2_MEMORY_MMAP)
1093 rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, b->count);
1095 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1101 static int vidioc_querybuf(struct file *file, void *fh,
1102 struct v4l2_buffer *b)
1104 struct omap24xxcam_fh *ofh = fh;
1106 return videobuf_querybuf(&ofh->vbq, b);
1109 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1111 struct omap24xxcam_fh *ofh = fh;
1113 return videobuf_qbuf(&ofh->vbq, b);
1116 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1118 struct omap24xxcam_fh *ofh = fh;
1119 struct omap24xxcam_device *cam = ofh->cam;
1120 struct videobuf_buffer *vb;
1123 rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
1127 vb = ofh->vbq.bufs[b->index];
1129 mutex_lock(&cam->mutex);
1130 /* _needs_reset returns -EIO if reset is required. */
1131 rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr);
1132 mutex_unlock(&cam->mutex);
1134 schedule_work(&cam->sensor_reset_work);
1140 * This is a hack. User space won't get the index of this
1141 * buffer and does not want to requeue it so we requeue it
1145 videobuf_qbuf(&ofh->vbq, b);
1150 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1152 struct omap24xxcam_fh *ofh = fh;
1153 struct omap24xxcam_device *cam = ofh->cam;
1156 mutex_lock(&cam->mutex);
1157 if (cam->streaming) {
1162 rval = omap24xxcam_sensor_if_enable(cam);
1164 dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n");
1168 rval = videobuf_streamon(&ofh->vbq);
1170 cam->streaming = file;
1171 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1175 mutex_unlock(&cam->mutex);
1180 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1182 struct omap24xxcam_fh *ofh = fh;
1183 struct omap24xxcam_device *cam = ofh->cam;
1184 struct videobuf_queue *q = &ofh->vbq;
1187 atomic_inc(&cam->reset_disable);
1189 flush_scheduled_work();
1191 rval = videobuf_streamoff(q);
1193 mutex_lock(&cam->mutex);
1194 cam->streaming = NULL;
1195 mutex_unlock(&cam->mutex);
1196 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1199 atomic_dec(&cam->reset_disable);
1204 static int vidioc_enum_input(struct file *file, void *fh,
1205 struct v4l2_input *inp)
1210 strlcpy(inp->name, "camera", sizeof(inp->name));
1211 inp->type = V4L2_INPUT_TYPE_CAMERA;
1216 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
1223 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
1231 static int vidioc_queryctrl(struct file *file, void *fh,
1232 struct v4l2_queryctrl *a)
1234 struct omap24xxcam_fh *ofh = fh;
1235 struct omap24xxcam_device *cam = ofh->cam;
1238 rval = vidioc_int_queryctrl(cam->sdev, a);
1243 static int vidioc_g_ctrl(struct file *file, void *fh,
1244 struct v4l2_control *a)
1246 struct omap24xxcam_fh *ofh = fh;
1247 struct omap24xxcam_device *cam = ofh->cam;
1250 mutex_lock(&cam->mutex);
1251 rval = vidioc_int_g_ctrl(cam->sdev, a);
1252 mutex_unlock(&cam->mutex);
1257 static int vidioc_s_ctrl(struct file *file, void *fh,
1258 struct v4l2_control *a)
1260 struct omap24xxcam_fh *ofh = fh;
1261 struct omap24xxcam_device *cam = ofh->cam;
1264 mutex_lock(&cam->mutex);
1265 rval = vidioc_int_s_ctrl(cam->sdev, a);
1266 mutex_unlock(&cam->mutex);
1271 static int vidioc_g_parm(struct file *file, void *fh,
1272 struct v4l2_streamparm *a) {
1273 struct omap24xxcam_fh *ofh = fh;
1274 struct omap24xxcam_device *cam = ofh->cam;
1277 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1280 mutex_lock(&cam->mutex);
1281 rval = vidioc_int_g_parm(cam->sdev, a);
1282 mutex_unlock(&cam->mutex);
1287 static int vidioc_s_parm(struct file *file, void *fh,
1288 struct v4l2_streamparm *a)
1290 struct omap24xxcam_fh *ofh = fh;
1291 struct omap24xxcam_device *cam = ofh->cam;
1292 struct v4l2_streamparm old_streamparm;
1295 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1298 mutex_lock(&cam->mutex);
1299 if (cam->streaming) {
1304 old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1305 rval = vidioc_int_g_parm(cam->sdev, &old_streamparm);
1309 rval = vidioc_int_s_parm(cam->sdev, a);
1313 rval = omap24xxcam_sensor_if_enable(cam);
1315 * Revert to old streaming parameters if enabling sensor
1316 * interface with the new ones failed.
1319 vidioc_int_s_parm(cam->sdev, &old_streamparm);
1322 mutex_unlock(&cam->mutex);
1333 static unsigned int omap24xxcam_poll(struct file *file,
1334 struct poll_table_struct *wait)
1336 struct omap24xxcam_fh *fh = file->private_data;
1337 struct omap24xxcam_device *cam = fh->cam;
1338 struct videobuf_buffer *vb;
1340 mutex_lock(&cam->mutex);
1341 if (cam->streaming != file) {
1342 mutex_unlock(&cam->mutex);
1345 mutex_unlock(&cam->mutex);
1347 mutex_lock(&fh->vbq.lock);
1348 if (list_empty(&fh->vbq.stream)) {
1349 mutex_unlock(&fh->vbq.lock);
1352 vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
1353 mutex_unlock(&fh->vbq.lock);
1355 poll_wait(file, &vb->done, wait);
1357 if (vb->state == STATE_DONE || vb->state == STATE_ERROR)
1358 return POLLIN | POLLRDNORM;
1363 static int omap24xxcam_mmap_buffers(struct file *file,
1364 struct vm_area_struct *vma)
1366 struct omap24xxcam_fh *fh = file->private_data;
1367 struct omap24xxcam_device *cam = fh->cam;
1368 struct videobuf_queue *vbq = &fh->vbq;
1369 struct videobuf_buffer *vb;
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++) {
1405 for (j = 0; j < vb->dma.sglen; j++) {
1406 err = remap_pfn_range(
1407 vma, vma->vm_start + size,
1408 page_to_pfn(vb->dma.sglist[j].page),
1409 vb->dma.sglist[j].length, vma->vm_page_prot);
1412 size += vb->dma.sglist[j].length;
1417 mutex_unlock(&vbq->lock);
1422 static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma)
1424 struct omap24xxcam_fh *fh = file->private_data;
1427 /* let the video-buf mapper check arguments and set-up structures */
1428 rval = videobuf_mmap_mapper(&fh->vbq, vma);
1432 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1434 /* do mapping to our allocated buffers */
1435 rval = omap24xxcam_mmap_buffers(file, vma);
1437 * In case of error, free vma->vm_private_data allocated by
1438 * videobuf_mmap_mapper.
1441 kfree(vma->vm_private_data);
1446 static int omap24xxcam_open(struct inode *inode, struct file *file)
1448 int minor = iminor(inode);
1449 struct omap24xxcam_device *cam = omap24xxcam.priv;
1450 struct omap24xxcam_fh *fh;
1451 struct v4l2_format format;
1453 if (!cam || !cam->vfd || (cam->vfd->minor != minor))
1456 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1460 mutex_lock(&cam->mutex);
1461 if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
1462 mutex_unlock(&cam->mutex);
1463 goto out_try_module_get;
1466 if (atomic_inc_return(&cam->users) == 1) {
1467 omap24xxcam_hwinit(cam);
1468 if (omap24xxcam_sensor_enable(cam)) {
1469 mutex_unlock(&cam->mutex);
1470 goto out_omap24xxcam_sensor_enable;
1473 mutex_unlock(&cam->mutex);
1476 mutex_lock(&cam->mutex);
1477 vidioc_int_g_fmt_cap(cam->sdev, &format);
1478 mutex_unlock(&cam->mutex);
1479 /* FIXME: how about fh->pix when there are more users? */
1480 fh->pix = format.fmt.pix;
1482 file->private_data = fh;
1484 spin_lock_init(&fh->vbq_lock);
1486 videobuf_queue_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL,
1487 &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1489 sizeof(struct videobuf_buffer), fh);
1493 out_omap24xxcam_sensor_enable:
1494 omap24xxcam_poweron_reset(cam);
1495 module_put(cam->sdev->module);
1503 static int omap24xxcam_release(struct inode *inode, struct file *file)
1505 struct omap24xxcam_fh *fh = file->private_data;
1506 struct omap24xxcam_device *cam = fh->cam;
1508 atomic_inc(&cam->reset_disable);
1510 flush_scheduled_work();
1512 mutex_lock(&cam->mutex);
1513 /* stop streaming capture */
1514 if (cam->streaming == file) {
1515 cam->streaming = NULL;
1516 mutex_unlock(&cam->mutex);
1517 videobuf_streamoff(&fh->vbq);
1518 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1520 mutex_unlock(&cam->mutex);
1523 atomic_dec(&cam->reset_disable);
1525 omap24xxcam_vbq_free_mmap_buffers(&fh->vbq);
1528 * Make sure the reset work we might have scheduled is not
1529 * pending! It may be run *only* if we have users. (And it may
1530 * not be scheduled anymore since streaming is already
1533 flush_scheduled_work();
1535 mutex_lock(&cam->mutex);
1536 if (atomic_dec_return(&cam->users) == 0) {
1537 omap24xxcam_sensor_disable(cam);
1538 omap24xxcam_poweron_reset(cam);
1540 mutex_unlock(&cam->mutex);
1542 file->private_data = NULL;
1544 module_put(cam->sdev->module);
1550 static struct file_operations omap24xxcam_fops = {
1551 .owner = THIS_MODULE,
1552 .llseek = no_llseek,
1553 .ioctl = video_ioctl2,
1554 .poll = omap24xxcam_poll,
1555 .mmap = omap24xxcam_mmap,
1556 .open = omap24xxcam_open,
1557 .release = omap24xxcam_release,
1567 static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state)
1569 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1571 if (atomic_read(&cam->users) == 0)
1574 if (!atomic_read(&cam->reset_disable))
1575 omap24xxcam_capture_stop(cam);
1577 omap24xxcam_sensor_disable(cam);
1578 omap24xxcam_poweron_reset(cam);
1583 static int omap24xxcam_resume(struct platform_device *pdev)
1585 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1587 if (atomic_read(&cam->users) == 0)
1590 omap24xxcam_hwinit(cam);
1591 omap24xxcam_sensor_enable(cam);
1593 if (!atomic_read(&cam->reset_disable))
1594 omap24xxcam_capture_cont(cam);
1598 #endif /* CONFIG_PM */
1602 * Camera device (i.e. /dev/video).
1606 static int omap24xxcam_device_register(struct v4l2_int_device *ctl,
1607 struct v4l2_int_device *s)
1609 struct omap24xxcam_device *cam = ctl->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 /* need to register for a VID_HARDWARE_* ID in videodev.h */
1640 vfd->fops = &omap24xxcam_fops;
1644 vfd->vidioc_querycap = vidioc_querycap;
1645 vfd->vidioc_enum_fmt_cap = vidioc_enum_fmt_cap;
1646 vfd->vidioc_g_fmt_cap = vidioc_g_fmt_cap;
1647 vfd->vidioc_s_fmt_cap = vidioc_s_fmt_cap;
1648 vfd->vidioc_try_fmt_cap = vidioc_try_fmt_cap;
1649 vfd->vidioc_reqbufs = vidioc_reqbufs;
1650 vfd->vidioc_querybuf = vidioc_querybuf;
1651 vfd->vidioc_qbuf = vidioc_qbuf;
1652 vfd->vidioc_dqbuf = vidioc_dqbuf;
1653 vfd->vidioc_streamon = vidioc_streamon;
1654 vfd->vidioc_streamoff = vidioc_streamoff;
1655 vfd->vidioc_enum_input = vidioc_enum_input;
1656 vfd->vidioc_g_input = vidioc_g_input;
1657 vfd->vidioc_s_input = vidioc_s_input;
1658 vfd->vidioc_queryctrl = vidioc_queryctrl;
1659 vfd->vidioc_g_ctrl = vidioc_g_ctrl;
1660 vfd->vidioc_s_ctrl = vidioc_s_ctrl;
1661 vfd->vidioc_g_parm = vidioc_g_parm;
1662 vfd->vidioc_s_parm = vidioc_s_parm;
1664 omap24xxcam_hwinit(cam);
1666 rval = omap24xxcam_sensor_init(cam);
1670 if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1671 dev_err(cam->dev, "could not register V4L device\n");
1677 omap24xxcam_poweron_reset(cam);
1679 dev_info(cam->dev, "registered device video%d\n", vfd->minor);
1684 omap24xxcam_device_unregister(ctl);
1689 static void omap24xxcam_device_unregister(struct v4l2_int_device *ctl)
1691 struct omap24xxcam_device *cam = ctl->priv;
1693 omap24xxcam_sensor_exit(cam);
1696 if (cam->vfd->minor == -1) {
1698 * The device was never registered, so release the
1699 * video_device struct directly.
1701 video_device_release(cam->vfd);
1704 * The unregister function will release the
1705 * video_device struct as well as
1708 video_unregister_device(cam->vfd);
1713 device_remove_file(cam->dev, &dev_attr_streaming);
1718 static struct v4l2_int_master omap24xxcam_master = {
1719 .attach = omap24xxcam_device_register,
1720 .detach = omap24xxcam_device_unregister,
1723 static struct v4l2_int_device omap24xxcam = {
1724 .module = THIS_MODULE,
1726 .type = v4l2_int_type_master,
1728 .master = &omap24xxcam_master
1734 * Driver initialisation and deinitialisation.
1738 static int __init omap24xxcam_probe(struct platform_device *pdev)
1740 struct omap24xxcam_device *cam;
1741 struct resource *mem;
1744 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1746 dev_err(&pdev->dev, "could not allocate memory\n");
1750 platform_set_drvdata(pdev, cam);
1752 cam->dev = &pdev->dev;
1755 * Impose a lower limit on the amount of memory allocated for
1756 * capture. We require at least enough memory to double-buffer
1759 if (capture_mem < 320 * 240 * 2 * 2)
1760 capture_mem = 320 * 240 * 2 * 2;
1761 cam->capture_mem = capture_mem;
1763 /* request the mem region for the camera registers */
1764 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1766 dev_err(cam->dev, "no mem resource?\n");
1769 if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
1772 "cannot reserve camera register I/O region\n");
1775 cam->mmio_base_phys = mem->start;
1776 cam->mmio_size = (mem->end - mem->start) + 1;
1778 /* map the region */
1779 cam->mmio_base = (unsigned long)
1780 ioremap_nocache(cam->mmio_base_phys, cam->mmio_size);
1781 if (!cam->mmio_base) {
1782 dev_err(cam->dev, "cannot map camera register I/O region\n");
1786 irq = platform_get_irq(pdev, 0);
1788 dev_err(cam->dev, "no irq for camera?\n");
1792 /* install the interrupt service routine */
1793 if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) {
1795 "could not install interrupt service routine\n");
1800 if (omap24xxcam_clock_get(cam))
1803 INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work);
1805 mutex_init(&cam->mutex);
1806 spin_lock_init(&cam->core_enable_disable_lock);
1808 omap24xxcam_sgdma_init(&cam->sgdma,
1809 cam->mmio_base + CAMDMA_REG_OFFSET,
1810 omap24xxcam_stalled_dma_reset,
1811 (unsigned long)cam);
1813 omap24xxcam.priv = cam;
1815 if (v4l2_int_device_register(&omap24xxcam))
1821 omap24xxcam_remove(pdev);
1825 static int omap24xxcam_remove(struct platform_device *pdev)
1827 struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1832 if (omap24xxcam.priv != NULL)
1833 v4l2_int_device_unregister(&omap24xxcam);
1834 omap24xxcam.priv = NULL;
1836 omap24xxcam_clock_put(cam);
1839 free_irq(cam->irq, cam);
1843 if (cam->mmio_base) {
1844 iounmap((void *)cam->mmio_base);
1848 if (cam->mmio_base_phys) {
1849 release_mem_region(cam->mmio_base_phys, cam->mmio_size);
1850 cam->mmio_base_phys = 0;
1858 static struct platform_driver omap24xxcam_driver = {
1859 .probe = omap24xxcam_probe,
1860 .remove = omap24xxcam_remove,
1862 .suspend = omap24xxcam_suspend,
1863 .resume = omap24xxcam_resume,
1872 * Module initialisation and deinitialisation
1876 static int __init omap24xxcam_init(void)
1878 return platform_driver_register(&omap24xxcam_driver);
1881 static void __exit omap24xxcam_cleanup(void)
1883 platform_driver_unregister(&omap24xxcam_driver);
1886 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
1887 MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
1888 MODULE_LICENSE("GPL");
1889 module_param(video_nr, int, 0);
1890 MODULE_PARM_DESC(video_nr,
1891 "Minor number for video device (-1 ==> auto assign)");
1892 module_param(capture_mem, int, 0);
1893 MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture "
1894 "buffers (default 4800kiB)");
1896 module_init(omap24xxcam_init);
1897 module_exit(omap24xxcam_cleanup);