2 * drivers/media/video/omap/camera_core.c
4 * Copyright (C) 2004 Texas Instruments, Inc.
6 * Video-for-Linux (Version 2) camera capture driver for
7 * the OMAP H2 and H3 camera controller.
9 * Adapted from omap24xx driver written by Andy Lowe (source@mvista.com)
10 * Copyright (C) 2003-2004 MontaVista Software, Inc.
12 * This package is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
16 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * 27/03/05 Vladimir Barinov - Added support for power management
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
29 #include <linux/vmalloc.h>
30 #include <linux/slab.h>
31 #include <linux/proc_fs.h>
32 #include <linux/ctype.h>
33 #include <linux/pagemap.h>
35 #include <linux/device.h>
36 #include <linux/delay.h>
37 #include <linux/interrupt.h>
38 #include <linux/videodev.h>
39 #include <linux/pci.h>
40 #include <asm/semaphore.h>
41 #include <asm/processor.h>
42 #include <linux/dma-mapping.h>
46 #include <asm/byteorder.h>
49 #include "sensor_if.h"
50 #include "camera_hw_if.h"
51 #include "camera_core.h"
53 struct camera_device *camera_dev;
54 extern struct camera_sensor camera_sensor_if;
55 extern struct camera_hardware camera_hardware_if;
57 static void camera_core_sgdma_process(struct camera_device *cam);
59 /* module parameters */
60 static int video_nr = -1; /* video device minor (-1 ==> auto assign) */
62 /* Maximum amount of memory to use for capture buffers.
63 * Default is 4800KB, enough to double-buffer SXGA.
65 static int capture_mem = 1280*960*2*2;
67 /*Size of video overlay framebuffer. This determines the maximum image size
68 *that can be previewed. Default is 600KB, enough for sxga.
70 static int overlay_mem = 640*480*2;
73 /* DMA completion routine for the scatter-gather DMA fragments. */
74 /* This function is called when a scatter DMA fragment is completed */
76 camera_core_callback_sgdma(void *arg1, void *arg2)
78 struct camera_device *cam = (struct camera_device *)arg1;
79 int sgslot = (int)arg2;
81 struct sgdma_state *sgdma;
83 spin_lock(&cam->sg_lock);
84 sgdma = cam->sgdma + sgslot;
85 if (!sgdma->queued_sglist)
87 spin_unlock(&cam->sg_lock);
88 printk(KERN_ERR CAM_NAME ": SGDMA completed when none queued\n");
91 if (!--sgdma->queued_sglist) {
92 /* queue for this sglist is empty so check whether transfer
93 ** of the frame has been completed */
94 if (sgdma->next_sglist == sgdma->sglen) {
95 dma_callback_t callback = sgdma->callback;
96 void *arg = sgdma->arg;
97 /* all done with this sglist */
100 spin_unlock(&cam->sg_lock);
101 (*callback)(cam, arg);
102 camera_core_sgdma_process(cam);
107 spin_unlock(&cam->sg_lock);
108 camera_core_sgdma_process(cam);
114 camera_core_sgdma_init(struct camera_device *cam)
118 /* Initialize the underlying camera DMA */
119 cam->cam_hardware->init_dma(cam->hardware_data);
120 spin_lock_init(&cam->sg_lock);
122 cam->free_sgdma = NUM_SG_DMA;
124 for (sg = 0; sg < NUM_SG_DMA; sg++) {
125 cam->sgdma[sg].sglen = 0;
126 cam->sgdma[sg].next_sglist = 0;
127 cam->sgdma[sg].queued_sglist = 0;
128 cam->sgdma[sg].csr = 0;
129 cam->sgdma[sg].callback = NULL;
130 cam->sgdma[sg].arg = NULL;
135 * Process the scatter-gather DMA queue by starting queued transfers
136 * This function is called to program the dma to start the transfer of an image.
139 camera_core_sgdma_process(struct camera_device *cam)
141 unsigned long irqflags;
142 int queued_sgdma, sgslot;
143 struct sgdma_state *sgdma;
144 const struct scatterlist *sglist;
146 spin_lock_irqsave(&cam->sg_lock, irqflags);
147 if (1 == cam->in_use) {
148 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
152 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
154 queued_sgdma = NUM_SG_DMA - cam->free_sgdma;
155 sgslot = (cam->next_sgdma + cam->free_sgdma) % (NUM_SG_DMA);
156 while (queued_sgdma > 0) {
157 sgdma = cam->sgdma + sgslot;
158 while (sgdma->next_sglist < sgdma->sglen) {
159 sglist = sgdma->sglist + sgdma->next_sglist;
160 if (cam->cam_hardware->start_dma(sgdma, camera_core_callback_sgdma,
161 (void *)cam, (void *)sgslot, cam->hardware_data)) {
162 /* dma start failed */
167 /* dma start successful */
168 sgdma->next_sglist ++;
169 sgdma->queued_sglist ++;
173 sgslot = (sgslot + 1) % (NUM_SG_DMA);
179 /* Queue a scatter-gather DMA transfer from the camera to memory.
180 * Returns zero if the transfer was successfully queued, or
181 * non-zero if all of the scatter-gather slots are already in use.
184 camera_core_sgdma_queue(struct camera_device *cam,
185 const struct scatterlist *sglist, int sglen, dma_callback_t callback,
188 unsigned long irqflags;
189 struct sgdma_state *sgdma;
191 if ((sglen < 0) || ((sglen > 0) & !sglist))
194 spin_lock_irqsave(&cam->sg_lock, irqflags);
196 if (!cam->free_sgdma) {
197 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
201 sgdma = cam->sgdma + cam->next_sgdma;
203 sgdma->sglist = sglist;
204 sgdma->sglen = sglen;
205 sgdma->next_sglist = 0;
206 sgdma->queued_sglist = 0;
208 sgdma->callback = callback;
211 cam->next_sgdma = (cam->next_sgdma + 1) % (NUM_SG_DMA);
214 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
216 camera_core_sgdma_process(cam);
222 /* -------------------overlay routines ------------------------------*/
223 /* callback routine for overlay DMA completion. We just start another DMA
224 * transfer unless overlay has been turned off
228 camera_core_overlay_callback(void *arg1, void *arg)
230 struct camera_device *cam = (struct camera_device *)arg1;
232 unsigned long irqflags;
235 unsigned char *fb_buf = phys_to_virt((unsigned long)camera_dev->fbuf.base);
237 spin_lock_irqsave(&cam->overlay_lock, irqflags);
239 if (!cam->previewing || cam->overlay_cnt == 0) {
240 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
245 sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
246 sg_dma_len(&cam->overlay_sglist) = cam->pix.sizeimage;
249 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline);
250 for (i = 0 ; i < cam->pix.sizeimage; i += cam->pix.bytesperline) {
251 for (index = 0; index < cam->pix.bytesperline; index++) {
252 fb_buf[j] = *(((unsigned char *) cam->overlay_base) +
255 fb_buf[j + 1] = *(((unsigned char *) cam->overlay_base) + i + index);
256 j = j - cam->fbuf.fmt.bytesperline;
259 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline) + count;
262 while (cam->overlay_cnt < 2) {
263 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
264 camera_core_overlay_callback, NULL);
270 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
276 camera_core_start_overlay(struct camera_device *cam)
279 unsigned long irqflags;
281 if (!cam->previewing)
284 spin_lock_irqsave(&cam->overlay_lock, irqflags);
286 sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
287 sg_dma_len(&cam->overlay_sglist)= cam->pix.sizeimage;
288 while (cam->overlay_cnt < 2) {
289 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
290 camera_core_overlay_callback, NULL);
296 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
299 /* ------------------ videobuf_queue_ops ---------------------------------------- */
301 /* This routine is called from interrupt context when a scatter-gather DMA
302 * transfer of a videobuf_buffer completes.
305 camera_core_vbq_complete(void *arg1, void *arg)
307 struct camera_device *cam = (struct camera_device *)arg1;
308 struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
310 spin_lock(&cam->vbq_lock);
312 do_gettimeofday(&vb->ts);
313 vb->field_count = cam->field_count;
314 cam->field_count += 2;
315 vb->state = STATE_DONE;
319 spin_unlock(&cam->vbq_lock);
323 camera_core_vbq_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
325 videobuf_waiton(vb, 0, 0);
326 videobuf_dma_pci_unmap(NULL, &vb->dma);
327 videobuf_dma_free(&vb->dma);
329 vb->state = STATE_NEEDS_INIT;
332 /* Limit the number of available kernel image capture buffers based on the
333 * number requested, the currently selected image size, and the maximum
334 * amount of memory permitted for kernel capture buffers.
337 camera_core_vbq_setup(struct videobuf_queue *q, unsigned int *cnt, unsigned int *size)
339 struct camera_device *cam = q->priv_data;
342 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
344 if (*cnt > VIDEO_MAX_FRAME)
345 *cnt = VIDEO_MAX_FRAME;
347 spin_lock(&cam->img_lock);
348 *size = cam->pix.sizeimage;
349 spin_unlock(&cam->img_lock);
351 while (*size * *cnt > capture_mem)
358 camera_core_vbq_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
359 enum v4l2_field field)
361 struct camera_device *cam = q->priv_data;
364 spin_lock(&cam->img_lock);
365 if (cam->pix.sizeimage > vb->bsize) {
366 spin_unlock(&cam->img_lock);
369 vb->size = cam->pix.sizeimage;
370 vb->width = cam->pix.width;
371 vb->height = cam->pix.height;
373 spin_unlock(&cam->img_lock);
375 if (vb->state == STATE_NEEDS_INIT)
376 err = videobuf_iolock(NULL, vb, NULL);
379 vb->state = STATE_PREPARED;
381 camera_core_vbq_release (q, vb);
387 camera_core_vbq_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
389 struct camera_device *cam = q->priv_data;
390 enum videobuf_state state = vb->state;
393 vb->state = STATE_QUEUED;
394 err = camera_core_sgdma_queue(cam, vb->dma.sglist, vb->dma.sglen,
395 camera_core_vbq_complete, vb);
397 /* Oops. We're not supposed to get any errors here. The only
398 * way we could get an error is if we ran out of scatter-gather
399 * DMA slots, but we are supposed to have at least as many
400 * scatter-gather DMA slots as video buffers so that can't
403 printk(KERN_DEBUG CAM_NAME
404 ": Failed to queue a video buffer for SGDMA\n");
409 /* ------------------ videobuf_queue_ops ---------------------------------------- */
412 camera_core_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
415 struct camera_fh *fh = file->private_data;
416 struct camera_device *cam = fh->cam;
420 case VIDIOC_ENUMINPUT:
422 /* default handler assumes 1 video input (the camera) */
423 struct v4l2_input *input = (struct v4l2_input *)arg;
424 int index = input->index;
426 memset(input, 0, sizeof(*input));
427 input->index = index;
432 strlcpy(input->name, "camera", sizeof(input->name));
433 input->type = V4L2_INPUT_TYPE_CAMERA;
440 unsigned int *input = arg;
448 unsigned int *input = arg;
456 case VIDIOC_ENUM_FMT:
458 struct v4l2_fmtdesc *fmt = arg;
459 return cam->cam_sensor->enum_pixformat(fmt, cam->sensor_data);
464 struct v4l2_format *fmt = arg;
465 return cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
471 struct v4l2_format *fmt = arg;
473 /* get the current format */
474 memset(&fmt->fmt.pix, 0, sizeof (fmt->fmt.pix));
475 fmt->fmt.pix = cam->pix;
482 struct v4l2_format *fmt = arg;
483 unsigned int temp_sizeimage = 0;
485 temp_sizeimage = cam->pix.sizeimage;
486 cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
487 cam->pix = fmt->fmt.pix;
489 cam->xclk = cam->cam_sensor->calc_xclk(&cam->pix,
490 &cam->nominal_timeperframe, cam->sensor_data);
491 cam->cparm.timeperframe = cam->nominal_timeperframe;
492 cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
493 return cam->cam_sensor->configure(&cam->pix, cam->xclk,
494 &cam->cparm.timeperframe, cam->sensor_data);
497 case VIDIOC_QUERYCTRL:
499 struct v4l2_queryctrl *qc = arg;
500 return cam->cam_sensor->query_control(qc, cam->sensor_data);
505 struct v4l2_control *vc = arg;
506 return cam->cam_sensor->get_control(vc, cam->sensor_data);
511 struct v4l2_control *vc = arg;
512 return cam->cam_sensor->set_control(vc, cam->sensor_data);
515 case VIDIOC_QUERYCAP:
517 struct v4l2_capability *cap =
518 (struct v4l2_capability *) arg;
520 memset(cap, 0, sizeof(*cap));
521 strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
522 strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
523 cap->bus_info[0] = '\0';
524 cap->version = KERNEL_VERSION(0, 0, 0);
526 V4L2_CAP_VIDEO_CAPTURE |
527 V4L2_CAP_VIDEO_OVERLAY |
533 case VIDIOC_G_FBUF: /* Get the frame buffer parameters */
535 struct v4l2_framebuffer *fbuf =
536 (struct v4l2_framebuffer *) arg;
538 spin_lock(&cam->img_lock);
540 spin_unlock(&cam->img_lock);
544 case VIDIOC_S_FBUF: /* set the frame buffer parameters */
546 struct v4l2_framebuffer *fbuf =
547 (struct v4l2_framebuffer *) arg;
549 spin_lock(&cam->img_lock);
550 if (cam->previewing) {
551 spin_unlock(&cam->img_lock);
554 cam->fbuf.base = fbuf->base;
555 cam->fbuf.fmt = fbuf->fmt;
557 spin_unlock(&cam->img_lock);
563 int enable = *((int *) arg);
566 * check whether the capture format and
567 ** the display format matches
568 * return failure if they are different
570 if (cam->pix.pixelformat != cam->fbuf.fmt.pixelformat)
575 /* If the camera image size is greater
576 ** than LCD size return failure */
577 if ((cam->pix.width > cam->fbuf.fmt.height) ||
578 (cam->pix.height > cam->fbuf.fmt.width))
583 if (!cam->previewing && enable)
585 cam->previewing = fh;
586 cam->overlay_cnt = 0;
587 camera_core_start_overlay(cam);
591 cam->previewing = NULL;
598 return videobuf_reqbufs(&fh->vbq, arg);
600 case VIDIOC_QUERYBUF:
601 return videobuf_querybuf(&fh->vbq, arg);
604 return videobuf_qbuf(&fh->vbq, arg);
607 return videobuf_dqbuf(&fh->vbq, arg,
608 file->f_flags & O_NONBLOCK);
610 case VIDIOC_STREAMON:
612 spin_lock(&cam->img_lock);
614 if (cam->streaming || cam->reading) {
615 spin_unlock(&cam->img_lock);
620 /* FIXME: start camera interface */
623 spin_unlock(&cam->img_lock);
625 return videobuf_streamon(&fh->vbq);
627 case VIDIOC_STREAMOFF:
629 err = videobuf_streamoff(&fh->vbq);
633 spin_lock(&cam->img_lock);
634 if (cam->streaming == fh) {
635 cam->streaming = NULL;
636 /* FIXME: stop camera interface */
638 spin_unlock(&cam->img_lock);
644 case VIDIOC_QUERYSTD:
646 /* Digital cameras don't have an analog video standard,
647 * so we don't need to implement these ioctls.
653 case VIDIOC_G_AUDOUT:
654 case VIDIOC_S_AUDOUT:
656 /* we don't have any audio inputs or outputs */
660 case VIDIOC_G_JPEGCOMP:
661 case VIDIOC_S_JPEGCOMP:
663 /* JPEG compression is not supported */
669 case VIDIOC_G_MODULATOR:
670 case VIDIOC_S_MODULATOR:
671 case VIDIOC_G_FREQUENCY:
672 case VIDIOC_S_FREQUENCY:
674 /* we don't have a tuner or modulator */
678 case VIDIOC_ENUMOUTPUT:
679 case VIDIOC_G_OUTPUT:
680 case VIDIOC_S_OUTPUT:
682 /* we don't have any video outputs */
688 /* unrecognized ioctl */
700 int camera_core_poll(struct file *file, struct poll_table_struct *wait)
705 /* ------------------------------------------------------------ */
706 /* callback routine for read DMA completion. We just start another DMA
707 * transfer unless overlay has been turned off
710 camera_core_capture_callback(void *arg1, void *arg)
712 struct camera_device *cam = (struct camera_device *)arg1;
714 unsigned long irqflags;
717 spin_lock_irqsave(&cam->capture_lock, irqflags);
721 cam->capture_started = 0;
722 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
728 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
729 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
730 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
731 camera_core_capture_callback, NULL);
733 cam->capture_completed = 1;
736 /* Wake up any process which are waiting for the
737 ** DMA to complete */
738 wake_up_interruptible(&camera_dev->new_video_frame);
739 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
740 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
741 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
742 camera_core_capture_callback, NULL);
746 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
751 camera_core_read(struct file *file, char *data, size_t count, loff_t *ppos)
753 struct camera_fh *fh = file->private_data;
754 struct camera_device *cam = fh->cam;
756 unsigned long irqflags;
758 #if 0 /* use video_buf to do capture */
760 for (i = 0; i < 14; i++)
761 videobuf_read_one(file, &fh->vbq, data, count, ppos);
762 i = videobuf_read_one(file, &fh->vbq, data, count, ppos);
766 if (!cam->capture_base) {
767 cam->capture_base = (unsigned long)dma_alloc_coherent(NULL,
769 (dma_addr_t *) &cam->capture_base_phys,
770 GFP_KERNEL | GFP_DMA);
772 if (!cam->capture_base) {
773 printk(KERN_ERR CAM_NAME
774 ": cannot allocate capture buffer\n");
778 spin_lock_irqsave(&cam->capture_lock, irqflags);
780 cam->capture_started = 1;
781 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
782 sg_dma_len(&cam->capture_sglist)= cam->pix.sizeimage;
783 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
785 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
786 camera_core_capture_callback, NULL);
788 /* Wait till DMA is completed */
790 cam->capture_completed = 0;
791 while (cam->capture_completed == 0) {
792 timeout = interruptible_sleep_on_timeout
793 (&cam->new_video_frame, timeout);
795 printk(KERN_ERR CAM_NAME ": timeout waiting video frame\n");
796 return -EIO; /* time out */
799 /* copy the data to the user buffer */
800 err = copy_to_user(data, (void *)cam->capture_base, cam->pix.sizeimage);
801 return (cam->pix.sizeimage - err);
806 camera_core_mmap(struct file *file, struct vm_area_struct *vma)
808 struct camera_fh *fh = file->private_data;
810 return videobuf_mmap_mapper(&fh->vbq, vma);
814 camera_core_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
818 return video_usercopy(inode, file, cmd, arg, camera_core_do_ioctl);
822 camera_core_release(struct inode *inode, struct file *file)
824 struct camera_fh *fh = file->private_data;
825 struct camera_device *cam = fh->cam;
827 file->private_data = NULL;
830 spin_lock(&cam->img_lock);
831 if (cam->previewing == fh) {
832 cam->previewing = NULL;
834 if (cam->streaming == fh) {
835 cam->streaming = NULL;
837 if (cam->reading == fh) {
840 spin_unlock(&cam->img_lock);
842 camera_dev->cam_hardware->finish_dma(cam->hardware_data);
844 if (cam->capture_base) {
845 dma_free_coherent(NULL, cam->pix.sizeimage,
846 (void *)cam->capture_base,
847 cam->capture_base_phys);
848 cam->capture_base = 0;
849 cam->capture_base_phys = 0;
851 if (fh->vbq.read_buf) {
852 camera_core_vbq_release(&fh->vbq, fh->vbq.read_buf);
853 kfree(fh->vbq.read_buf);
856 cam->cam_hardware->close(cam->hardware_data);
862 camera_core_open(struct inode *inode, struct file *file)
864 int minor = iminor(inode);
865 struct camera_device *cam = camera_dev;
866 struct camera_fh *fh;
868 if (!cam || !cam->vfd || (cam->vfd->minor != minor))
871 /* allocate per-filehandle data */
872 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
875 file->private_data = fh;
877 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
879 spin_lock(&cam->img_lock);
880 if (cam->active == 1) {
881 printk (KERN_ERR CAM_NAME ": Camera device Active\n");
882 spin_unlock(&cam->img_lock);
886 spin_unlock(&cam->img_lock);
888 videobuf_queue_init(&fh->vbq, &cam->vbq_ops, NULL, &cam->vbq_lock,
889 fh->type, V4L2_FIELD_NONE, sizeof(struct videobuf_buffer), fh);
891 cam->capture_completed = 0;
892 cam->capture_started = 0;
894 if (cam->cam_hardware->open(cam->hardware_data))
896 printk (KERN_ERR CAM_NAME ": Camera IF configuration failed\n");
901 cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
902 /* program the sensor for the capture format and rate */
903 if (cam->cam_sensor->configure(&cam->pix, cam->xclk,
904 &cam->cparm.timeperframe, cam->sensor_data))
906 printk (KERN_ERR CAM_NAME ": Camera sensor configuration failed\n");
907 cam->cam_hardware->close(cam->hardware_data);
916 static int camera_core_suspend(struct device *dev, u32 state, u32 level)
918 struct camera_device *cam = dev_get_drvdata(dev);
921 spin_lock(&cam->img_lock);
923 case SUSPEND_POWER_DOWN:
925 cam->cam_hardware->close(cam->hardware_data);
927 cam->cam_sensor->power_off(cam->sensor_data);
931 spin_unlock(&cam->img_lock);
935 static int camera_core_resume(struct device *dev, u32 level)
937 struct camera_device *cam = dev_get_drvdata(dev);
940 spin_lock(&cam->img_lock);
942 case RESUME_POWER_ON:
943 cam->cam_sensor->power_on(cam->sensor_data);
945 cam->capture_completed = 1;
946 cam->cam_hardware->open(cam->hardware_data);
947 cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
949 cam->cam_sensor->configure(&cam->pix, cam->xclk,
950 &cam->cparm.timeperframe, cam->sensor_data);
952 camera_core_sgdma_process(cam);
957 spin_unlock(&cam->img_lock);
960 #endif /* CONFIG_PM */
962 static struct file_operations camera_core_fops =
964 .owner = THIS_MODULE,
966 .read = camera_core_read,
967 .poll = camera_core_poll,
968 .ioctl = camera_core_ioctl,
969 .mmap = camera_core_mmap,
970 .open = camera_core_open,
971 .release = camera_core_release,
974 static struct device_driver camera_core_driver = {
976 .bus = &platform_bus_type,
980 .suspend = camera_core_suspend,
981 .resume = camera_core_resume,
986 static struct platform_device camera_core_device = {
995 camera_core_cleanup(void)
997 struct camera_device *cam = camera_dev;
998 struct video_device *vfd;
1005 if (vfd->minor == -1) {
1006 /* The device never got registered, so release the
1007 ** video_device struct directly
1009 video_device_release(vfd);
1011 /* The unregister function will release the video_device
1012 ** struct as well as unregistering it.
1014 video_unregister_device(vfd);
1015 driver_unregister(&camera_core_driver);
1016 platform_device_unregister(&camera_core_device);
1020 if (cam->overlay_base) {
1021 dma_free_coherent(NULL, cam->overlay_size,
1022 (void *)cam->overlay_base,
1023 cam->overlay_base_phys);
1024 cam->overlay_base = 0;
1026 cam->overlay_base_phys = 0;
1028 cam->cam_sensor->cleanup(cam->sensor_data);
1029 cam->cam_hardware->cleanup(cam->hardware_data);
1038 camera_core_init(void)
1040 struct camera_device *cam;
1041 struct video_device *vfd;
1043 cam = kmalloc(sizeof(struct camera_device), GFP_KERNEL);
1045 printk(KERN_ERR CAM_NAME ": could not allocate memory\n");
1048 memset(cam, 0, sizeof(struct camera_device));
1050 /* Save the pointer to camera device in a global variable */
1053 /* initialize the video_device struct */
1054 vfd = cam->vfd = video_device_alloc();
1056 printk(KERN_ERR CAM_NAME
1057 ": could not allocate video device struct\n");
1061 vfd->release = video_device_release;
1063 strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1064 vfd->type = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CHROMAKEY;
1066 /* need to register for a VID_HARDWARE_* ID in videodev.h */
1068 vfd->fops = &camera_core_fops;
1069 video_set_drvdata(vfd, cam);
1072 /* initialize the videobuf queue ops */
1073 cam->vbq_ops.buf_setup = camera_core_vbq_setup;
1074 cam->vbq_ops.buf_prepare = camera_core_vbq_prepare;
1075 cam->vbq_ops.buf_queue = camera_core_vbq_queue;
1076 cam->vbq_ops.buf_release = camera_core_vbq_release;
1078 /* initilize the overlay interface */
1079 cam->overlay_size = overlay_mem;
1080 if (cam->overlay_size > 0)
1082 cam->overlay_base = (unsigned long) dma_alloc_coherent(NULL,
1084 (dma_addr_t *) &cam->overlay_base_phys,
1085 GFP_KERNEL | GFP_DMA);
1086 if (!cam->overlay_base) {
1087 printk(KERN_ERR CAM_NAME
1088 ": cannot allocate overlay framebuffer\n");
1092 memset((void*)cam->overlay_base, 0, cam->overlay_size);
1093 spin_lock_init(&cam->overlay_lock);
1094 spin_lock_init(&cam->capture_lock);
1096 /*Initialise the pointer to the sensor interface and camera interface */
1097 cam->cam_sensor = &camera_sensor_if;
1098 cam->cam_hardware = &camera_hardware_if;
1100 /* initialize the camera interface */
1101 cam->hardware_data = cam->cam_hardware->init();
1102 if (!cam->hardware_data) {
1103 printk(KERN_ERR CAM_NAME ": cannot initialize interface hardware\n");
1107 /* initialize the spinlock used to serialize access to the image
1110 spin_lock_init(&cam->img_lock);
1112 /* initialize the streaming capture parameters */
1113 cam->cparm.capability = V4L2_CAP_TIMEPERFRAME;
1114 cam->cparm.readbuffers = 1;
1116 /* Enable the xclk output. The sensor may (and does, in the case of
1117 * the OV9640) require an xclk input in order for its initialization
1120 cam->xclk = 21000000; /* choose an arbitrary xclk frequency */
1121 cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
1123 /* initialize the sensor and define a default capture format cam->pix */
1124 cam->sensor_data = cam->cam_sensor->init(&cam->pix);
1125 if (!cam->sensor_data) {
1126 cam->cam_hardware->disable(cam->hardware_data);
1127 printk(KERN_ERR CAM_NAME ": cannot initialize sensor\n");
1131 printk(KERN_INFO CAM_NAME ": %s interface with %s sensor\n",
1132 cam->cam_hardware->name, cam->cam_sensor->name);
1134 /* select an arbitrary default capture frame rate of 15fps */
1135 cam->nominal_timeperframe.numerator = 1;
1136 cam->nominal_timeperframe.denominator = 15;
1138 /* calculate xclk based on the default capture format and default
1141 cam->xclk = cam->cam_sensor->calc_xclk(&cam->pix,
1142 &cam->nominal_timeperframe, cam->sensor_data);
1143 cam->cparm.timeperframe = cam->nominal_timeperframe;
1145 /* initialise the wait queue */
1146 init_waitqueue_head(&cam->new_video_frame);
1148 /* Initialise the DMA structures */
1149 camera_core_sgdma_init(cam);
1151 /* Disable the Camera after detection */
1152 cam->cam_hardware->disable(cam->hardware_data);
1154 dev_set_drvdata(&camera_core_device.dev, (void *)cam);
1155 if (platform_device_register(&camera_core_device) < 0) {
1156 printk(KERN_ERR CAM_NAME
1157 ": could not register platform_device\n");
1161 if (driver_register(&camera_core_driver) < 0) {
1162 printk(KERN_ERR CAM_NAME
1163 ": could not register driver\n");
1164 platform_device_unregister(&camera_core_device);
1167 if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1168 printk(KERN_ERR CAM_NAME
1169 ": could not register Video for Linux device\n");
1170 platform_device_unregister(&camera_core_device);
1171 driver_unregister(&camera_core_driver);
1175 printk(KERN_INFO CAM_NAME
1176 ": registered device video%d [v4l2]\n", vfd->minor);
1180 camera_core_cleanup();
1184 MODULE_AUTHOR("Texas Instruments.");
1185 MODULE_DESCRIPTION("OMAP Video for Linux camera driver");
1186 MODULE_LICENSE("GPL");
1187 module_param(video_nr, int, 0);
1188 MODULE_PARM_DESC(video_nr,
1189 "Minor number for video device (-1 ==> auto assign)");
1190 module_param(capture_mem, int, 0);
1191 MODULE_PARM_DESC(capture_mem,
1192 "Maximum amount of memory for capture buffers (default 4800KB)");
1194 module_init(camera_core_init);
1195 module_exit(camera_core_cleanup);