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/platform_device.h>
36 #include <linux/delay.h>
37 #include <linux/interrupt.h>
38 #include <linux/videodev.h>
39 #include <linux/pci.h>
40 #include <linux/version.h>
41 #include <asm/semaphore.h>
42 #include <asm/processor.h>
43 #include <linux/dma-mapping.h>
47 #include <asm/byteorder.h>
50 #include "sensor_if.h"
51 #include "camera_hw_if.h"
52 #include "camera_core.h"
54 struct camera_device *camera_dev;
55 extern struct camera_sensor camera_sensor_if;
56 extern struct camera_hardware camera_hardware_if;
58 static void camera_core_sgdma_process(struct camera_device *cam);
60 /* module parameters */
61 static int video_nr = -1; /* video device minor (-1 ==> auto assign) */
63 /* Maximum amount of memory to use for capture buffers.
64 * Default is 4800KB, enough to double-buffer SXGA.
66 static int capture_mem = 1280*960*2*2;
68 /*Size of video overlay framebuffer. This determines the maximum image size
69 *that can be previewed. Default is 600KB, enough for sxga.
71 static int overlay_mem = 640*480*2;
74 /* DMA completion routine for the scatter-gather DMA fragments. */
75 /* This function is called when a scatter DMA fragment is completed */
77 camera_core_callback_sgdma(void *arg1, void *arg2)
79 struct camera_device *cam = (struct camera_device *)arg1;
80 int sgslot = (int)arg2;
82 struct sgdma_state *sgdma;
84 spin_lock(&cam->sg_lock);
85 sgdma = cam->sgdma + sgslot;
86 if (!sgdma->queued_sglist)
88 spin_unlock(&cam->sg_lock);
89 printk(KERN_ERR CAM_NAME ": SGDMA completed when none queued\n");
92 if (!--sgdma->queued_sglist) {
93 /* queue for this sglist is empty so check whether transfer
94 ** of the frame has been completed */
95 if (sgdma->next_sglist == sgdma->sglen) {
96 dma_callback_t callback = sgdma->callback;
97 void *arg = sgdma->arg;
98 /* all done with this sglist */
101 spin_unlock(&cam->sg_lock);
102 (*callback)(cam, arg);
103 camera_core_sgdma_process(cam);
108 spin_unlock(&cam->sg_lock);
109 camera_core_sgdma_process(cam);
115 camera_core_sgdma_init(struct camera_device *cam)
119 /* Initialize the underlying camera DMA */
120 cam->cam_hardware->init_dma(cam->hardware_data);
121 spin_lock_init(&cam->sg_lock);
123 cam->free_sgdma = NUM_SG_DMA;
125 for (sg = 0; sg < NUM_SG_DMA; sg++) {
126 cam->sgdma[sg].sglen = 0;
127 cam->sgdma[sg].next_sglist = 0;
128 cam->sgdma[sg].queued_sglist = 0;
129 cam->sgdma[sg].csr = 0;
130 cam->sgdma[sg].callback = NULL;
131 cam->sgdma[sg].arg = NULL;
136 * Process the scatter-gather DMA queue by starting queued transfers
137 * This function is called to program the dma to start the transfer of an image.
140 camera_core_sgdma_process(struct camera_device *cam)
142 unsigned long irqflags;
143 int queued_sgdma, sgslot;
144 struct sgdma_state *sgdma;
145 const struct scatterlist *sglist;
147 spin_lock_irqsave(&cam->sg_lock, irqflags);
148 if (1 == cam->in_use) {
149 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
153 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
155 queued_sgdma = NUM_SG_DMA - cam->free_sgdma;
156 sgslot = (cam->next_sgdma + cam->free_sgdma) % (NUM_SG_DMA);
157 while (queued_sgdma > 0) {
158 sgdma = cam->sgdma + sgslot;
159 while (sgdma->next_sglist < sgdma->sglen) {
160 sglist = sgdma->sglist + sgdma->next_sglist;
161 if (cam->cam_hardware->start_dma(sgdma, camera_core_callback_sgdma,
162 (void *)cam, (void *)sgslot, cam->hardware_data)) {
163 /* dma start failed */
168 /* dma start successful */
169 sgdma->next_sglist ++;
170 sgdma->queued_sglist ++;
174 sgslot = (sgslot + 1) % (NUM_SG_DMA);
180 /* Queue a scatter-gather DMA transfer from the camera to memory.
181 * Returns zero if the transfer was successfully queued, or
182 * non-zero if all of the scatter-gather slots are already in use.
185 camera_core_sgdma_queue(struct camera_device *cam,
186 const struct scatterlist *sglist, int sglen, dma_callback_t callback,
189 unsigned long irqflags;
190 struct sgdma_state *sgdma;
192 if ((sglen < 0) || ((sglen > 0) & !sglist))
195 spin_lock_irqsave(&cam->sg_lock, irqflags);
197 if (!cam->free_sgdma) {
198 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
202 sgdma = cam->sgdma + cam->next_sgdma;
204 sgdma->sglist = sglist;
205 sgdma->sglen = sglen;
206 sgdma->next_sglist = 0;
207 sgdma->queued_sglist = 0;
209 sgdma->callback = callback;
212 cam->next_sgdma = (cam->next_sgdma + 1) % (NUM_SG_DMA);
215 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
217 camera_core_sgdma_process(cam);
223 /* -------------------overlay routines ------------------------------*/
224 /* callback routine for overlay DMA completion. We just start another DMA
225 * transfer unless overlay has been turned off
229 camera_core_overlay_callback(void *arg1, void *arg)
231 struct camera_device *cam = (struct camera_device *)arg1;
233 unsigned long irqflags;
236 unsigned char *fb_buf = phys_to_virt((unsigned long)camera_dev->fbuf.base);
238 spin_lock_irqsave(&cam->overlay_lock, irqflags);
240 if (!cam->previewing || cam->overlay_cnt == 0) {
241 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
246 sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
247 sg_dma_len(&cam->overlay_sglist) = cam->pix.sizeimage;
250 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline);
251 for (i = 0 ; i < cam->pix.sizeimage; i += cam->pix.bytesperline) {
252 for (index = 0; index < cam->pix.bytesperline; index++) {
253 fb_buf[j] = *(((unsigned char *) cam->overlay_base) +
256 fb_buf[j + 1] = *(((unsigned char *) cam->overlay_base) + i + index);
257 j = j - cam->fbuf.fmt.bytesperline;
260 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline) + count;
263 while (cam->overlay_cnt < 2) {
264 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
265 camera_core_overlay_callback, NULL);
271 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
277 camera_core_start_overlay(struct camera_device *cam)
280 unsigned long irqflags;
282 if (!cam->previewing)
285 spin_lock_irqsave(&cam->overlay_lock, irqflags);
287 sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
288 sg_dma_len(&cam->overlay_sglist)= cam->pix.sizeimage;
289 while (cam->overlay_cnt < 2) {
290 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
291 camera_core_overlay_callback, NULL);
297 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
300 /* ------------------ videobuf_queue_ops ---------------------------------------- */
302 /* This routine is called from interrupt context when a scatter-gather DMA
303 * transfer of a videobuf_buffer completes.
306 camera_core_vbq_complete(void *arg1, void *arg)
308 struct camera_device *cam = (struct camera_device *)arg1;
309 struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
311 spin_lock(&cam->vbq_lock);
313 do_gettimeofday(&vb->ts);
314 vb->field_count = cam->field_count;
315 cam->field_count += 2;
316 vb->state = STATE_DONE;
320 spin_unlock(&cam->vbq_lock);
324 camera_core_vbq_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
326 videobuf_waiton(vb, 0, 0);
327 videobuf_dma_pci_unmap(NULL, &vb->dma);
328 videobuf_dma_free(&vb->dma);
330 vb->state = STATE_NEEDS_INIT;
333 /* Limit the number of available kernel image capture buffers based on the
334 * number requested, the currently selected image size, and the maximum
335 * amount of memory permitted for kernel capture buffers.
338 camera_core_vbq_setup(struct videobuf_queue *q, unsigned int *cnt, unsigned int *size)
340 struct camera_device *cam = q->priv_data;
343 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
345 if (*cnt > VIDEO_MAX_FRAME)
346 *cnt = VIDEO_MAX_FRAME;
348 spin_lock(&cam->img_lock);
349 *size = cam->pix.sizeimage;
350 spin_unlock(&cam->img_lock);
352 while (*size * *cnt > capture_mem)
359 camera_core_vbq_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
360 enum v4l2_field field)
362 struct camera_device *cam = q->priv_data;
365 spin_lock(&cam->img_lock);
366 if (cam->pix.sizeimage > vb->bsize) {
367 spin_unlock(&cam->img_lock);
370 vb->size = cam->pix.sizeimage;
371 vb->width = cam->pix.width;
372 vb->height = cam->pix.height;
374 spin_unlock(&cam->img_lock);
376 if (vb->state == STATE_NEEDS_INIT)
377 err = videobuf_iolock(NULL, vb, NULL);
380 vb->state = STATE_PREPARED;
382 camera_core_vbq_release (q, vb);
388 camera_core_vbq_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
390 struct camera_device *cam = q->priv_data;
391 enum videobuf_state state = vb->state;
394 vb->state = STATE_QUEUED;
395 err = camera_core_sgdma_queue(cam, vb->dma.sglist, vb->dma.sglen,
396 camera_core_vbq_complete, vb);
398 /* Oops. We're not supposed to get any errors here. The only
399 * way we could get an error is if we ran out of scatter-gather
400 * DMA slots, but we are supposed to have at least as many
401 * scatter-gather DMA slots as video buffers so that can't
404 printk(KERN_DEBUG CAM_NAME
405 ": Failed to queue a video buffer for SGDMA\n");
410 /* ------------------ videobuf_queue_ops ---------------------------------------- */
413 camera_core_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
416 struct camera_fh *fh = file->private_data;
417 struct camera_device *cam = fh->cam;
421 case VIDIOC_ENUMINPUT:
423 /* default handler assumes 1 video input (the camera) */
424 struct v4l2_input *input = (struct v4l2_input *)arg;
425 int index = input->index;
427 memset(input, 0, sizeof(*input));
428 input->index = index;
433 strlcpy(input->name, "camera", sizeof(input->name));
434 input->type = V4L2_INPUT_TYPE_CAMERA;
441 unsigned int *input = arg;
449 unsigned int *input = arg;
457 case VIDIOC_ENUM_FMT:
459 struct v4l2_fmtdesc *fmt = arg;
460 return cam->cam_sensor->enum_pixformat(fmt, cam->sensor_data);
465 struct v4l2_format *fmt = arg;
466 return cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
472 struct v4l2_format *fmt = arg;
474 /* get the current format */
475 memset(&fmt->fmt.pix, 0, sizeof (fmt->fmt.pix));
476 fmt->fmt.pix = cam->pix;
483 struct v4l2_format *fmt = arg;
484 unsigned int temp_sizeimage = 0;
486 temp_sizeimage = cam->pix.sizeimage;
487 cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
488 cam->pix = fmt->fmt.pix;
490 cam->xclk = cam->cam_sensor->calc_xclk(&cam->pix,
491 &cam->nominal_timeperframe, cam->sensor_data);
492 cam->cparm.timeperframe = cam->nominal_timeperframe;
493 cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
494 return cam->cam_sensor->configure(&cam->pix, cam->xclk,
495 &cam->cparm.timeperframe, cam->sensor_data);
498 case VIDIOC_QUERYCTRL:
500 struct v4l2_queryctrl *qc = arg;
501 return cam->cam_sensor->query_control(qc, cam->sensor_data);
506 struct v4l2_control *vc = arg;
507 return cam->cam_sensor->get_control(vc, cam->sensor_data);
512 struct v4l2_control *vc = arg;
513 return cam->cam_sensor->set_control(vc, cam->sensor_data);
516 case VIDIOC_QUERYCAP:
518 struct v4l2_capability *cap =
519 (struct v4l2_capability *) arg;
521 memset(cap, 0, sizeof(*cap));
522 strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
523 strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
524 cap->bus_info[0] = '\0';
525 cap->version = KERNEL_VERSION(0, 0, 0);
527 V4L2_CAP_VIDEO_CAPTURE |
528 V4L2_CAP_VIDEO_OVERLAY |
534 case VIDIOC_G_FBUF: /* Get the frame buffer parameters */
536 struct v4l2_framebuffer *fbuf =
537 (struct v4l2_framebuffer *) arg;
539 spin_lock(&cam->img_lock);
541 spin_unlock(&cam->img_lock);
545 case VIDIOC_S_FBUF: /* set the frame buffer parameters */
547 struct v4l2_framebuffer *fbuf =
548 (struct v4l2_framebuffer *) arg;
550 spin_lock(&cam->img_lock);
551 if (cam->previewing) {
552 spin_unlock(&cam->img_lock);
555 cam->fbuf.base = fbuf->base;
556 cam->fbuf.fmt = fbuf->fmt;
558 spin_unlock(&cam->img_lock);
564 int enable = *((int *) arg);
567 * check whether the capture format and
568 ** the display format matches
569 * return failure if they are different
571 if (cam->pix.pixelformat != cam->fbuf.fmt.pixelformat)
576 /* If the camera image size is greater
577 ** than LCD size return failure */
578 if ((cam->pix.width > cam->fbuf.fmt.height) ||
579 (cam->pix.height > cam->fbuf.fmt.width))
584 if (!cam->previewing && enable)
586 cam->previewing = fh;
587 cam->overlay_cnt = 0;
588 camera_core_start_overlay(cam);
592 cam->previewing = NULL;
599 return videobuf_reqbufs(&fh->vbq, arg);
601 case VIDIOC_QUERYBUF:
602 return videobuf_querybuf(&fh->vbq, arg);
605 return videobuf_qbuf(&fh->vbq, arg);
608 return videobuf_dqbuf(&fh->vbq, arg,
609 file->f_flags & O_NONBLOCK);
611 case VIDIOC_STREAMON:
613 spin_lock(&cam->img_lock);
615 if (cam->streaming || cam->reading) {
616 spin_unlock(&cam->img_lock);
621 /* FIXME: start camera interface */
624 spin_unlock(&cam->img_lock);
626 return videobuf_streamon(&fh->vbq);
628 case VIDIOC_STREAMOFF:
630 err = videobuf_streamoff(&fh->vbq);
634 spin_lock(&cam->img_lock);
635 if (cam->streaming == fh) {
636 cam->streaming = NULL;
637 /* FIXME: stop camera interface */
639 spin_unlock(&cam->img_lock);
645 case VIDIOC_QUERYSTD:
647 /* Digital cameras don't have an analog video standard,
648 * so we don't need to implement these ioctls.
654 case VIDIOC_G_AUDOUT:
655 case VIDIOC_S_AUDOUT:
657 /* we don't have any audio inputs or outputs */
661 case VIDIOC_G_JPEGCOMP:
662 case VIDIOC_S_JPEGCOMP:
664 /* JPEG compression is not supported */
670 case VIDIOC_G_MODULATOR:
671 case VIDIOC_S_MODULATOR:
672 case VIDIOC_G_FREQUENCY:
673 case VIDIOC_S_FREQUENCY:
675 /* we don't have a tuner or modulator */
679 case VIDIOC_ENUMOUTPUT:
680 case VIDIOC_G_OUTPUT:
681 case VIDIOC_S_OUTPUT:
683 /* we don't have any video outputs */
689 /* unrecognized ioctl */
701 int camera_core_poll(struct file *file, struct poll_table_struct *wait)
706 /* ------------------------------------------------------------ */
707 /* callback routine for read DMA completion. We just start another DMA
708 * transfer unless overlay has been turned off
711 camera_core_capture_callback(void *arg1, void *arg)
713 struct camera_device *cam = (struct camera_device *)arg1;
715 unsigned long irqflags;
718 spin_lock_irqsave(&cam->capture_lock, irqflags);
722 cam->capture_started = 0;
723 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
729 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
730 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
731 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
732 camera_core_capture_callback, NULL);
734 cam->capture_completed = 1;
737 /* Wake up any process which are waiting for the
738 ** DMA to complete */
739 wake_up_interruptible(&camera_dev->new_video_frame);
740 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
741 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
742 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
743 camera_core_capture_callback, NULL);
747 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
752 camera_core_read(struct file *file, char *data, size_t count, loff_t *ppos)
754 struct camera_fh *fh = file->private_data;
755 struct camera_device *cam = fh->cam;
757 unsigned long irqflags;
759 #if 0 /* use video_buf to do capture */
761 for (i = 0; i < 14; i++)
762 videobuf_read_one(file, &fh->vbq, data, count, ppos);
763 i = videobuf_read_one(file, &fh->vbq, data, count, ppos);
767 if (!cam->capture_base) {
768 cam->capture_base = (unsigned long)dma_alloc_coherent(NULL,
770 (dma_addr_t *) &cam->capture_base_phys,
771 GFP_KERNEL | GFP_DMA);
773 if (!cam->capture_base) {
774 printk(KERN_ERR CAM_NAME
775 ": cannot allocate capture buffer\n");
779 spin_lock_irqsave(&cam->capture_lock, irqflags);
781 cam->capture_started = 1;
782 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
783 sg_dma_len(&cam->capture_sglist)= cam->pix.sizeimage;
784 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
786 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
787 camera_core_capture_callback, NULL);
789 /* Wait till DMA is completed */
791 cam->capture_completed = 0;
792 while (cam->capture_completed == 0) {
793 timeout = interruptible_sleep_on_timeout
794 (&cam->new_video_frame, timeout);
796 printk(KERN_ERR CAM_NAME ": timeout waiting video frame\n");
797 return -EIO; /* time out */
800 /* copy the data to the user buffer */
801 err = copy_to_user(data, (void *)cam->capture_base, cam->pix.sizeimage);
802 return (cam->pix.sizeimage - err);
807 camera_core_mmap(struct file *file, struct vm_area_struct *vma)
809 struct camera_fh *fh = file->private_data;
811 return videobuf_mmap_mapper(&fh->vbq, vma);
815 camera_core_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
819 return video_usercopy(inode, file, cmd, arg, camera_core_do_ioctl);
823 camera_core_release(struct inode *inode, struct file *file)
825 struct camera_fh *fh = file->private_data;
826 struct camera_device *cam = fh->cam;
828 file->private_data = NULL;
831 spin_lock(&cam->img_lock);
832 if (cam->previewing == fh) {
833 cam->previewing = NULL;
835 if (cam->streaming == fh) {
836 cam->streaming = NULL;
838 if (cam->reading == fh) {
841 spin_unlock(&cam->img_lock);
843 camera_dev->cam_hardware->finish_dma(cam->hardware_data);
845 if (cam->capture_base) {
846 dma_free_coherent(NULL, cam->pix.sizeimage,
847 (void *)cam->capture_base,
848 cam->capture_base_phys);
849 cam->capture_base = 0;
850 cam->capture_base_phys = 0;
852 if (fh->vbq.read_buf) {
853 camera_core_vbq_release(&fh->vbq, fh->vbq.read_buf);
854 kfree(fh->vbq.read_buf);
857 cam->cam_hardware->close(cam->hardware_data);
863 camera_core_open(struct inode *inode, struct file *file)
865 int minor = iminor(inode);
866 struct camera_device *cam = camera_dev;
867 struct camera_fh *fh;
869 if (!cam || !cam->vfd || (cam->vfd->minor != minor))
872 /* allocate per-filehandle data */
873 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
876 file->private_data = fh;
878 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
880 spin_lock(&cam->img_lock);
881 if (cam->active == 1) {
882 printk (KERN_ERR CAM_NAME ": Camera device Active\n");
883 spin_unlock(&cam->img_lock);
887 spin_unlock(&cam->img_lock);
889 videobuf_queue_init(&fh->vbq, &cam->vbq_ops, NULL, &cam->vbq_lock,
890 fh->type, V4L2_FIELD_NONE, sizeof(struct videobuf_buffer), fh);
892 cam->capture_completed = 0;
893 cam->capture_started = 0;
895 if (cam->cam_hardware->open(cam->hardware_data))
897 printk (KERN_ERR CAM_NAME ": Camera IF configuration failed\n");
902 cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
903 /* program the sensor for the capture format and rate */
904 if (cam->cam_sensor->configure(&cam->pix, cam->xclk,
905 &cam->cparm.timeperframe, cam->sensor_data))
907 printk (KERN_ERR CAM_NAME ": Camera sensor configuration failed\n");
908 cam->cam_hardware->close(cam->hardware_data);
917 static int camera_core_suspend(struct device *dev, pm_message_t state)
919 struct camera_device *cam = dev_get_drvdata(dev);
922 spin_lock(&cam->img_lock);
924 cam->cam_hardware->close(cam->hardware_data);
926 cam->cam_sensor->power_off(cam->sensor_data);
927 spin_unlock(&cam->img_lock);
931 static int camera_core_resume(struct device *dev)
933 struct camera_device *cam = dev_get_drvdata(dev);
936 spin_lock(&cam->img_lock);
937 cam->cam_sensor->power_on(cam->sensor_data);
939 cam->capture_completed = 1;
940 cam->cam_hardware->open(cam->hardware_data);
941 cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
943 cam->cam_sensor->configure(&cam->pix, cam->xclk,
944 &cam->cparm.timeperframe,
946 camera_core_sgdma_process(cam);
948 spin_unlock(&cam->img_lock);
952 #endif /* CONFIG_PM */
954 static struct file_operations camera_core_fops =
956 .owner = THIS_MODULE,
958 .read = camera_core_read,
959 .poll = camera_core_poll,
960 .ioctl = camera_core_ioctl,
961 .mmap = camera_core_mmap,
962 .open = camera_core_open,
963 .release = camera_core_release,
966 static struct device_driver camera_core_driver = {
968 .bus = &platform_bus_type,
972 .suspend = camera_core_suspend,
973 .resume = camera_core_resume,
978 static struct platform_device camera_core_device = {
987 camera_core_cleanup(void)
989 struct camera_device *cam = camera_dev;
990 struct video_device *vfd;
997 if (vfd->minor == -1) {
998 /* The device never got registered, so release the
999 ** video_device struct directly
1001 video_device_release(vfd);
1003 /* The unregister function will release the video_device
1004 ** struct as well as unregistering it.
1006 video_unregister_device(vfd);
1007 driver_unregister(&camera_core_driver);
1008 platform_device_unregister(&camera_core_device);
1012 if (cam->overlay_base) {
1013 dma_free_coherent(NULL, cam->overlay_size,
1014 (void *)cam->overlay_base,
1015 cam->overlay_base_phys);
1016 cam->overlay_base = 0;
1018 cam->overlay_base_phys = 0;
1020 cam->cam_sensor->cleanup(cam->sensor_data);
1021 cam->cam_hardware->cleanup(cam->hardware_data);
1030 camera_core_init(void)
1032 struct camera_device *cam;
1033 struct video_device *vfd;
1035 cam = kzalloc(sizeof(struct camera_device), GFP_KERNEL);
1037 printk(KERN_ERR CAM_NAME ": could not allocate memory\n");
1041 /* Save the pointer to camera device in a global variable */
1044 /* initialize the video_device struct */
1045 vfd = cam->vfd = video_device_alloc();
1047 printk(KERN_ERR CAM_NAME
1048 ": could not allocate video device struct\n");
1052 vfd->release = video_device_release;
1054 strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1055 vfd->type = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CHROMAKEY;
1057 /* need to register for a VID_HARDWARE_* ID in videodev.h */
1059 vfd->fops = &camera_core_fops;
1060 video_set_drvdata(vfd, cam);
1063 /* initialize the videobuf queue ops */
1064 cam->vbq_ops.buf_setup = camera_core_vbq_setup;
1065 cam->vbq_ops.buf_prepare = camera_core_vbq_prepare;
1066 cam->vbq_ops.buf_queue = camera_core_vbq_queue;
1067 cam->vbq_ops.buf_release = camera_core_vbq_release;
1069 /* initilize the overlay interface */
1070 cam->overlay_size = overlay_mem;
1071 if (cam->overlay_size > 0)
1073 cam->overlay_base = (unsigned long) dma_alloc_coherent(NULL,
1075 (dma_addr_t *) &cam->overlay_base_phys,
1076 GFP_KERNEL | GFP_DMA);
1077 if (!cam->overlay_base) {
1078 printk(KERN_ERR CAM_NAME
1079 ": cannot allocate overlay framebuffer\n");
1083 memset((void*)cam->overlay_base, 0, cam->overlay_size);
1084 spin_lock_init(&cam->overlay_lock);
1085 spin_lock_init(&cam->capture_lock);
1087 /*Initialise the pointer to the sensor interface and camera interface */
1088 cam->cam_sensor = &camera_sensor_if;
1089 cam->cam_hardware = &camera_hardware_if;
1091 /* initialize the camera interface */
1092 cam->hardware_data = cam->cam_hardware->init();
1093 if (!cam->hardware_data) {
1094 printk(KERN_ERR CAM_NAME ": cannot initialize interface hardware\n");
1098 /* initialize the spinlock used to serialize access to the image
1101 spin_lock_init(&cam->img_lock);
1103 /* initialize the streaming capture parameters */
1104 cam->cparm.capability = V4L2_CAP_TIMEPERFRAME;
1105 cam->cparm.readbuffers = 1;
1107 /* Enable the xclk output. The sensor may (and does, in the case of
1108 * the OV9640) require an xclk input in order for its initialization
1111 cam->xclk = 21000000; /* choose an arbitrary xclk frequency */
1112 cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
1114 /* initialize the sensor and define a default capture format cam->pix */
1115 cam->sensor_data = cam->cam_sensor->init(&cam->pix);
1116 if (!cam->sensor_data) {
1117 cam->cam_hardware->disable(cam->hardware_data);
1118 printk(KERN_ERR CAM_NAME ": cannot initialize sensor\n");
1122 printk(KERN_INFO CAM_NAME ": %s interface with %s sensor\n",
1123 cam->cam_hardware->name, cam->cam_sensor->name);
1125 /* select an arbitrary default capture frame rate of 15fps */
1126 cam->nominal_timeperframe.numerator = 1;
1127 cam->nominal_timeperframe.denominator = 15;
1129 /* calculate xclk based on the default capture format and default
1132 cam->xclk = cam->cam_sensor->calc_xclk(&cam->pix,
1133 &cam->nominal_timeperframe, cam->sensor_data);
1134 cam->cparm.timeperframe = cam->nominal_timeperframe;
1136 /* initialise the wait queue */
1137 init_waitqueue_head(&cam->new_video_frame);
1139 /* Initialise the DMA structures */
1140 camera_core_sgdma_init(cam);
1142 /* Disable the Camera after detection */
1143 cam->cam_hardware->disable(cam->hardware_data);
1145 dev_set_drvdata(&camera_core_device.dev, (void *)cam);
1146 if (platform_device_register(&camera_core_device) < 0) {
1147 printk(KERN_ERR CAM_NAME
1148 ": could not register platform_device\n");
1152 if (driver_register(&camera_core_driver) < 0) {
1153 printk(KERN_ERR CAM_NAME
1154 ": could not register driver\n");
1155 platform_device_unregister(&camera_core_device);
1158 if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1159 printk(KERN_ERR CAM_NAME
1160 ": could not register Video for Linux device\n");
1161 platform_device_unregister(&camera_core_device);
1162 driver_unregister(&camera_core_driver);
1166 printk(KERN_INFO CAM_NAME
1167 ": registered device video%d [v4l2]\n", vfd->minor);
1171 camera_core_cleanup();
1175 MODULE_AUTHOR("Texas Instruments.");
1176 MODULE_DESCRIPTION("OMAP Video for Linux camera driver");
1177 MODULE_LICENSE("GPL");
1178 module_param(video_nr, int, 0);
1179 MODULE_PARM_DESC(video_nr,
1180 "Minor number for video device (-1 ==> auto assign)");
1181 module_param(capture_mem, int, 0);
1182 MODULE_PARM_DESC(capture_mem,
1183 "Maximum amount of memory for capture buffers (default 4800KB)");
1185 module_init(camera_core_init);
1186 module_exit(camera_core_cleanup);