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
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/platform_device.h>
27 #include <linux/version.h>
28 #include <linux/dma-mapping.h>
31 #include <media/v4l2-common.h>
35 #include "sensor_if.h"
36 #include "camera_hw_if.h"
37 #include "camera_core.h"
40 static struct camera_device *camera_dev;
41 static void camera_core_sgdma_process(struct camera_device *cam);
43 /* module parameters */
44 static int video_nr = -1; /* video device minor (-1 ==> auto assign) */
46 /* Maximum amount of memory to use for capture buffers.
47 * Default is 4800KB, enough to double-buffer SXGA.
49 static int capture_mem = 1280*960*2*2;
51 /*Size of video overlay framebuffer. This determines the maximum image size
52 *that can be previewed. Default is 600KB, enough for sxga.
54 static int overlay_mem = 640*480*2;
57 /* DMA completion routine for the scatter-gather DMA fragments. */
58 /* This function is called when a scatter DMA fragment is completed */
60 camera_core_callback_sgdma(void *arg1, void *arg2)
62 struct camera_device *cam = (struct camera_device *)arg1;
63 int sgslot = (int)arg2;
65 struct sgdma_state *sgdma;
67 spin_lock(&cam->sg_lock);
68 sgdma = cam->sgdma + sgslot;
69 if (!sgdma->queued_sglist)
71 spin_unlock(&cam->sg_lock);
72 printk(KERN_ERR CAM_NAME ": SGDMA completed when none queued\n");
75 if (!--sgdma->queued_sglist) {
76 /* queue for this sglist is empty so check whether transfer
77 ** of the frame has been completed */
78 if (sgdma->next_sglist == sgdma->sglen) {
79 dma_callback_t callback = sgdma->callback;
80 void *arg = sgdma->arg;
81 /* all done with this sglist */
84 spin_unlock(&cam->sg_lock);
85 (*callback)(cam, arg);
86 camera_core_sgdma_process(cam);
91 spin_unlock(&cam->sg_lock);
92 camera_core_sgdma_process(cam);
98 camera_core_sgdma_init(struct camera_device *cam)
102 /* Initialize the underlying camera DMA */
103 cam->cam_hardware->init_dma(cam->hardware_data);
104 spin_lock_init(&cam->sg_lock);
106 cam->free_sgdma = NUM_SG_DMA;
108 for (sg = 0; sg < NUM_SG_DMA; sg++) {
109 cam->sgdma[sg].sglen = 0;
110 cam->sgdma[sg].next_sglist = 0;
111 cam->sgdma[sg].queued_sglist = 0;
112 cam->sgdma[sg].csr = 0;
113 cam->sgdma[sg].callback = NULL;
114 cam->sgdma[sg].arg = NULL;
119 * Process the scatter-gather DMA queue by starting queued transfers
120 * This function is called to program the dma to start the transfer of an image.
123 camera_core_sgdma_process(struct camera_device *cam)
125 unsigned long irqflags;
126 int queued_sgdma, sgslot;
127 struct sgdma_state *sgdma;
128 const struct scatterlist *sglist;
130 spin_lock_irqsave(&cam->sg_lock, irqflags);
131 if (1 == cam->in_use) {
132 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
136 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
138 queued_sgdma = NUM_SG_DMA - cam->free_sgdma;
139 sgslot = (cam->next_sgdma + cam->free_sgdma) % (NUM_SG_DMA);
140 while (queued_sgdma > 0) {
141 sgdma = cam->sgdma + sgslot;
142 while (sgdma->next_sglist < sgdma->sglen) {
143 sglist = sgdma->sglist + sgdma->next_sglist;
144 if (cam->cam_hardware->start_dma(sgdma, camera_core_callback_sgdma,
145 (void *)cam, (void *)sgslot, cam->hardware_data)) {
146 /* dma start failed */
151 /* dma start successful */
152 sgdma->next_sglist ++;
153 sgdma->queued_sglist ++;
157 sgslot = (sgslot + 1) % (NUM_SG_DMA);
163 /* Queue a scatter-gather DMA transfer from the camera to memory.
164 * Returns zero if the transfer was successfully queued, or
165 * non-zero if all of the scatter-gather slots are already in use.
168 camera_core_sgdma_queue(struct camera_device *cam,
169 const struct scatterlist *sglist, int sglen, dma_callback_t callback,
172 unsigned long irqflags;
173 struct sgdma_state *sgdma;
175 if ((sglen < 0) || ((sglen > 0) & !sglist))
178 spin_lock_irqsave(&cam->sg_lock, irqflags);
180 if (!cam->free_sgdma) {
181 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
185 sgdma = cam->sgdma + cam->next_sgdma;
187 sgdma->sglist = sglist;
188 sgdma->sglen = sglen;
189 sgdma->next_sglist = 0;
190 sgdma->queued_sglist = 0;
192 sgdma->callback = callback;
195 cam->next_sgdma = (cam->next_sgdma + 1) % (NUM_SG_DMA);
198 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
200 camera_core_sgdma_process(cam);
206 /* -------------------overlay routines ------------------------------*/
207 /* callback routine for overlay DMA completion. We just start another DMA
208 * transfer unless overlay has been turned off
212 camera_core_overlay_callback(void *arg1, void *arg)
214 struct camera_device *cam = (struct camera_device *)arg1;
216 unsigned long irqflags;
219 unsigned char *fb_buf = phys_to_virt((unsigned long)camera_dev->fbuf.base);
221 spin_lock_irqsave(&cam->overlay_lock, irqflags);
223 if (!cam->previewing || cam->overlay_cnt == 0) {
224 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
229 sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
230 sg_dma_len(&cam->overlay_sglist) = cam->pix.sizeimage;
233 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline);
234 for (i = 0 ; i < cam->pix.sizeimage; i += cam->pix.bytesperline) {
235 for (index = 0; index < cam->pix.bytesperline; index++) {
236 fb_buf[j] = *(((unsigned char *) cam->overlay_base) +
239 fb_buf[j + 1] = *(((unsigned char *) cam->overlay_base) + i + index);
240 j = j - cam->fbuf.fmt.bytesperline;
243 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline) + count;
246 while (cam->overlay_cnt < 2) {
247 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
248 camera_core_overlay_callback, NULL);
254 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
260 camera_core_start_overlay(struct camera_device *cam)
263 unsigned long irqflags;
265 if (!cam->previewing)
268 spin_lock_irqsave(&cam->overlay_lock, irqflags);
270 sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
271 sg_dma_len(&cam->overlay_sglist)= cam->pix.sizeimage;
272 while (cam->overlay_cnt < 2) {
273 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
274 camera_core_overlay_callback, NULL);
280 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
283 /* ------------------ videobuf_queue_ops ---------------------------------------- */
285 /* This routine is called from interrupt context when a scatter-gather DMA
286 * transfer of a videobuf_buffer completes.
289 camera_core_vbq_complete(void *arg1, void *arg)
291 struct camera_device *cam = (struct camera_device *)arg1;
292 struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
294 spin_lock(&cam->vbq_lock);
296 do_gettimeofday(&vb->ts);
297 vb->field_count = cam->field_count;
298 cam->field_count += 2;
299 vb->state = STATE_DONE;
303 spin_unlock(&cam->vbq_lock);
307 camera_core_vbq_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
309 videobuf_waiton(vb, 0, 0);
310 videobuf_dma_unmap(q, &vb->dma);
311 videobuf_dma_free(&vb->dma);
313 vb->state = STATE_NEEDS_INIT;
316 /* Limit the number of available kernel image capture buffers based on the
317 * number requested, the currently selected image size, and the maximum
318 * amount of memory permitted for kernel capture buffers.
321 camera_core_vbq_setup(struct videobuf_queue *q, unsigned int *cnt, unsigned int *size)
323 struct camera_device *cam = q->priv_data;
326 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
328 if (*cnt > VIDEO_MAX_FRAME)
329 *cnt = VIDEO_MAX_FRAME;
331 spin_lock(&cam->img_lock);
332 *size = cam->pix.sizeimage;
333 spin_unlock(&cam->img_lock);
335 while (*size * *cnt > capture_mem)
342 camera_core_vbq_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
343 enum v4l2_field field)
345 struct camera_device *cam = q->priv_data;
348 spin_lock(&cam->img_lock);
349 if (cam->pix.sizeimage > vb->bsize) {
350 spin_unlock(&cam->img_lock);
353 vb->size = cam->pix.sizeimage;
354 vb->width = cam->pix.width;
355 vb->height = cam->pix.height;
357 spin_unlock(&cam->img_lock);
359 if (vb->state == STATE_NEEDS_INIT)
360 err = videobuf_iolock(q, vb, NULL);
363 vb->state = STATE_PREPARED;
365 camera_core_vbq_release (q, vb);
371 camera_core_vbq_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
373 struct camera_device *cam = q->priv_data;
374 enum videobuf_state state = vb->state;
377 vb->state = STATE_QUEUED;
378 err = camera_core_sgdma_queue(cam, vb->dma.sglist, vb->dma.sglen,
379 camera_core_vbq_complete, vb);
381 /* Oops. We're not supposed to get any errors here. The only
382 * way we could get an error is if we ran out of scatter-gather
383 * DMA slots, but we are supposed to have at least as many
384 * scatter-gather DMA slots as video buffers so that can't
387 printk(KERN_DEBUG CAM_NAME
388 ": Failed to queue a video buffer for SGDMA\n");
393 /* ------------------ videobuf_queue_ops ---------------------------------------- */
396 camera_core_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
399 struct camera_fh *fh = file->private_data;
400 struct camera_device *cam = fh->cam;
404 case VIDIOC_ENUMINPUT:
406 /* default handler assumes 1 video input (the camera) */
407 struct v4l2_input *input = (struct v4l2_input *)arg;
408 int index = input->index;
410 memset(input, 0, sizeof(*input));
411 input->index = index;
416 strlcpy(input->name, "camera", sizeof(input->name));
417 input->type = V4L2_INPUT_TYPE_CAMERA;
424 unsigned int *input = arg;
432 unsigned int *input = arg;
440 case VIDIOC_ENUM_FMT:
442 struct v4l2_fmtdesc *fmt = arg;
443 return cam->cam_sensor->enum_pixformat(fmt, cam->sensor_data);
448 struct v4l2_format *fmt = arg;
449 return cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
455 struct v4l2_format *fmt = arg;
457 /* get the current format */
458 memset(&fmt->fmt.pix, 0, sizeof (fmt->fmt.pix));
459 fmt->fmt.pix = cam->pix;
466 struct v4l2_format *fmt = arg;
467 unsigned int temp_sizeimage = 0;
469 temp_sizeimage = cam->pix.sizeimage;
470 cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
471 cam->pix = fmt->fmt.pix;
473 cam->xclk = cam->cam_sensor->calc_xclk(&cam->pix,
474 &cam->nominal_timeperframe, cam->sensor_data);
475 cam->cparm.timeperframe = cam->nominal_timeperframe;
476 cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
477 return cam->cam_sensor->configure(&cam->pix, cam->xclk,
478 &cam->cparm.timeperframe, cam->sensor_data);
481 case VIDIOC_QUERYCTRL:
483 struct v4l2_queryctrl *qc = arg;
484 return cam->cam_sensor->query_control(qc, cam->sensor_data);
489 struct v4l2_control *vc = arg;
490 return cam->cam_sensor->get_control(vc, cam->sensor_data);
495 struct v4l2_control *vc = arg;
496 return cam->cam_sensor->set_control(vc, cam->sensor_data);
499 case VIDIOC_QUERYCAP:
501 struct v4l2_capability *cap =
502 (struct v4l2_capability *) arg;
504 memset(cap, 0, sizeof(*cap));
505 strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
506 strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
507 cap->bus_info[0] = '\0';
508 cap->version = KERNEL_VERSION(0, 0, 0);
510 V4L2_CAP_VIDEO_CAPTURE |
511 V4L2_CAP_VIDEO_OVERLAY |
517 case VIDIOC_G_FBUF: /* Get the frame buffer parameters */
519 struct v4l2_framebuffer *fbuf =
520 (struct v4l2_framebuffer *) arg;
522 spin_lock(&cam->img_lock);
524 spin_unlock(&cam->img_lock);
528 case VIDIOC_S_FBUF: /* set the frame buffer parameters */
530 struct v4l2_framebuffer *fbuf =
531 (struct v4l2_framebuffer *) arg;
533 spin_lock(&cam->img_lock);
534 if (cam->previewing) {
535 spin_unlock(&cam->img_lock);
538 cam->fbuf.base = fbuf->base;
539 cam->fbuf.fmt = fbuf->fmt;
541 spin_unlock(&cam->img_lock);
547 int enable = *((int *) arg);
550 * check whether the capture format and
551 ** the display format matches
552 * return failure if they are different
554 if (cam->pix.pixelformat != cam->fbuf.fmt.pixelformat)
559 /* If the camera image size is greater
560 ** than LCD size return failure */
561 if ((cam->pix.width > cam->fbuf.fmt.height) ||
562 (cam->pix.height > cam->fbuf.fmt.width))
567 if (!cam->previewing && enable)
569 cam->previewing = fh;
570 cam->overlay_cnt = 0;
571 camera_core_start_overlay(cam);
575 cam->previewing = NULL;
582 return videobuf_reqbufs(&fh->vbq, arg);
584 case VIDIOC_QUERYBUF:
585 return videobuf_querybuf(&fh->vbq, arg);
588 return videobuf_qbuf(&fh->vbq, arg);
591 return videobuf_dqbuf(&fh->vbq, arg,
592 file->f_flags & O_NONBLOCK);
594 case VIDIOC_STREAMON:
596 spin_lock(&cam->img_lock);
598 if (cam->streaming || cam->reading) {
599 spin_unlock(&cam->img_lock);
604 /* FIXME: start camera interface */
607 spin_unlock(&cam->img_lock);
609 return videobuf_streamon(&fh->vbq);
611 case VIDIOC_STREAMOFF:
613 err = videobuf_streamoff(&fh->vbq);
617 spin_lock(&cam->img_lock);
618 if (cam->streaming == fh) {
619 cam->streaming = NULL;
620 /* FIXME: stop camera interface */
622 spin_unlock(&cam->img_lock);
628 case VIDIOC_QUERYSTD:
630 /* Digital cameras don't have an analog video standard,
631 * so we don't need to implement these ioctls.
637 case VIDIOC_G_AUDOUT:
638 case VIDIOC_S_AUDOUT:
640 /* we don't have any audio inputs or outputs */
644 case VIDIOC_G_JPEGCOMP:
645 case VIDIOC_S_JPEGCOMP:
647 /* JPEG compression is not supported */
653 case VIDIOC_G_MODULATOR:
654 case VIDIOC_S_MODULATOR:
655 case VIDIOC_G_FREQUENCY:
656 case VIDIOC_S_FREQUENCY:
658 /* we don't have a tuner or modulator */
662 case VIDIOC_ENUMOUTPUT:
663 case VIDIOC_G_OUTPUT:
664 case VIDIOC_S_OUTPUT:
666 /* we don't have any video outputs */
672 /* unrecognized ioctl */
684 int camera_core_poll(struct file *file, struct poll_table_struct *wait)
689 /* ------------------------------------------------------------ */
690 /* callback routine for read DMA completion. We just start another DMA
691 * transfer unless overlay has been turned off
694 camera_core_capture_callback(void *arg1, void *arg)
696 struct camera_device *cam = (struct camera_device *)arg1;
698 unsigned long irqflags;
701 spin_lock_irqsave(&cam->capture_lock, irqflags);
705 cam->capture_started = 0;
706 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
712 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
713 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
714 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
715 camera_core_capture_callback, NULL);
717 cam->capture_completed = 1;
720 /* Wake up any process which are waiting for the
721 ** DMA to complete */
722 wake_up_interruptible(&camera_dev->new_video_frame);
723 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
724 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
725 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
726 camera_core_capture_callback, NULL);
730 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
735 camera_core_read(struct file *file, char *data, size_t count, loff_t *ppos)
737 struct camera_fh *fh = file->private_data;
738 struct camera_device *cam = fh->cam;
740 unsigned long irqflags;
742 #if 0 /* use video_buf to do capture */
744 for (i = 0; i < 14; i++)
745 videobuf_read_one(file, &fh->vbq, data, count, ppos);
746 i = videobuf_read_one(file, &fh->vbq, data, count, ppos);
750 if (!cam->capture_base) {
751 cam->capture_base = (unsigned long)dma_alloc_coherent(NULL,
753 (dma_addr_t *) &cam->capture_base_phys,
754 GFP_KERNEL | GFP_DMA);
756 if (!cam->capture_base) {
757 printk(KERN_ERR CAM_NAME
758 ": cannot allocate capture buffer\n");
762 spin_lock_irqsave(&cam->capture_lock, irqflags);
764 cam->capture_started = 1;
765 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
766 sg_dma_len(&cam->capture_sglist)= cam->pix.sizeimage;
767 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
769 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
770 camera_core_capture_callback, NULL);
772 /* Wait till DMA is completed */
774 cam->capture_completed = 0;
775 while (cam->capture_completed == 0) {
776 timeout = interruptible_sleep_on_timeout
777 (&cam->new_video_frame, timeout);
779 printk(KERN_ERR CAM_NAME ": timeout waiting video frame\n");
780 return -EIO; /* time out */
783 /* copy the data to the user buffer */
784 err = copy_to_user(data, (void *)cam->capture_base, cam->pix.sizeimage);
785 return (cam->pix.sizeimage - err);
790 camera_core_mmap(struct file *file, struct vm_area_struct *vma)
792 struct camera_fh *fh = file->private_data;
794 return videobuf_mmap_mapper(&fh->vbq, vma);
798 camera_core_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
802 return video_usercopy(inode, file, cmd, arg, camera_core_do_ioctl);
806 camera_core_release(struct inode *inode, struct file *file)
808 struct camera_fh *fh = file->private_data;
809 struct camera_device *cam = fh->cam;
811 file->private_data = NULL;
814 spin_lock(&cam->img_lock);
815 if (cam->previewing == fh) {
816 cam->previewing = NULL;
818 if (cam->streaming == fh) {
819 cam->streaming = NULL;
821 if (cam->reading == fh) {
824 spin_unlock(&cam->img_lock);
826 camera_dev->cam_hardware->finish_dma(cam->hardware_data);
828 if (cam->capture_base) {
829 dma_free_coherent(NULL, cam->pix.sizeimage,
830 (void *)cam->capture_base,
831 cam->capture_base_phys);
832 cam->capture_base = 0;
833 cam->capture_base_phys = 0;
835 if (fh->vbq.read_buf) {
836 camera_core_vbq_release(&fh->vbq, fh->vbq.read_buf);
837 kfree(fh->vbq.read_buf);
840 cam->cam_hardware->close(cam->hardware_data);
846 camera_core_open(struct inode *inode, struct file *file)
848 int minor = iminor(inode);
849 struct camera_device *cam = camera_dev;
850 struct camera_fh *fh;
852 if (!cam || !cam->vfd || (cam->vfd->minor != minor))
855 /* allocate per-filehandle data */
856 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
859 file->private_data = fh;
861 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
863 spin_lock(&cam->img_lock);
864 if (cam->active == 1) {
865 printk (KERN_ERR CAM_NAME ": Camera device Active\n");
866 spin_unlock(&cam->img_lock);
870 spin_unlock(&cam->img_lock);
872 videobuf_queue_init(&fh->vbq, &cam->vbq_ops, NULL, &cam->vbq_lock,
873 fh->type, V4L2_FIELD_NONE, sizeof(struct videobuf_buffer), fh);
875 cam->capture_completed = 0;
876 cam->capture_started = 0;
878 if (cam->cam_hardware->open(cam->hardware_data))
880 printk (KERN_ERR CAM_NAME ": Camera IF configuration failed\n");
885 cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
886 /* program the sensor for the capture format and rate */
887 if (cam->cam_sensor->configure(&cam->pix, cam->xclk,
888 &cam->cparm.timeperframe, cam->sensor_data))
890 printk (KERN_ERR CAM_NAME ": Camera sensor configuration failed\n");
891 cam->cam_hardware->close(cam->hardware_data);
900 static int camera_core_suspend(struct platform_device *pdev, pm_message_t state)
902 struct camera_device *cam = platform_get_drvdata(pdev);
905 spin_lock(&cam->img_lock);
907 cam->cam_hardware->close(cam->hardware_data);
909 cam->cam_sensor->power_off(cam->sensor_data);
910 spin_unlock(&cam->img_lock);
915 static int camera_core_resume(struct platform_device *pdev)
917 struct camera_device *cam = platform_get_drvdata(pdev);
920 spin_lock(&cam->img_lock);
921 cam->cam_sensor->power_on(cam->sensor_data);
923 cam->capture_completed = 1;
924 cam->cam_hardware->open(cam->hardware_data);
925 cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
927 cam->cam_sensor->configure(&cam->pix, cam->xclk,
928 &cam->cparm.timeperframe,
930 camera_core_sgdma_process(cam);
932 spin_unlock(&cam->img_lock);
936 #endif /* CONFIG_PM */
938 static struct file_operations camera_core_fops =
940 .owner = THIS_MODULE,
942 .read = camera_core_read,
943 .poll = camera_core_poll,
944 .ioctl = camera_core_ioctl,
945 .mmap = camera_core_mmap,
946 .open = camera_core_open,
947 .release = camera_core_release,
950 static int __init camera_core_probe(struct platform_device *pdev)
952 struct camera_device *cam;
953 struct video_device *vfd;
956 cam = kzalloc(sizeof(struct camera_device), GFP_KERNEL);
958 printk(KERN_ERR CAM_NAME ": could not allocate memory\n");
963 /* Save the pointer to camera device in a global variable */
966 /* initialize the video_device struct */
967 vfd = cam->vfd = video_device_alloc();
969 printk(KERN_ERR CAM_NAME
970 ": could not allocate video device struct\n");
975 vfd->release = video_device_release;
977 strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
978 vfd->type = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CHROMAKEY;
980 /* need to register for a VID_HARDWARE_* ID in videodev.h */
982 vfd->fops = &camera_core_fops;
983 video_set_drvdata(vfd, cam);
986 /* initialize the videobuf queue ops */
987 cam->vbq_ops.buf_setup = camera_core_vbq_setup;
988 cam->vbq_ops.buf_prepare = camera_core_vbq_prepare;
989 cam->vbq_ops.buf_queue = camera_core_vbq_queue;
990 cam->vbq_ops.buf_release = camera_core_vbq_release;
992 /* initilize the overlay interface */
993 cam->overlay_size = overlay_mem;
994 if (cam->overlay_size > 0)
996 cam->overlay_base = (unsigned long) dma_alloc_coherent(NULL,
998 (dma_addr_t *) &cam->overlay_base_phys,
999 GFP_KERNEL | GFP_DMA);
1000 if (!cam->overlay_base) {
1001 printk(KERN_ERR CAM_NAME
1002 ": cannot allocate overlay framebuffer\n");
1007 memset((void*)cam->overlay_base, 0, cam->overlay_size);
1008 spin_lock_init(&cam->overlay_lock);
1009 spin_lock_init(&cam->capture_lock);
1011 /*Initialise the pointer to the sensor interface and camera interface */
1012 cam->cam_sensor = &camera_sensor_if;
1013 cam->cam_hardware = &camera_hardware_if;
1015 /* initialize the camera interface */
1016 cam->hardware_data = cam->cam_hardware->init();
1017 if (!cam->hardware_data) {
1018 printk(KERN_ERR CAM_NAME ": cannot initialize interface hardware\n");
1023 /* initialize the spinlock used to serialize access to the image
1026 spin_lock_init(&cam->img_lock);
1028 /* initialize the streaming capture parameters */
1029 cam->cparm.capability = V4L2_CAP_TIMEPERFRAME;
1030 cam->cparm.readbuffers = 1;
1032 /* Enable the xclk output. The sensor may (and does, in the case of
1033 * the OV9640) require an xclk input in order for its initialization
1036 cam->xclk = 21000000; /* choose an arbitrary xclk frequency */
1037 cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
1039 /* initialize the sensor and define a default capture format cam->pix */
1040 cam->sensor_data = cam->cam_sensor->init(&cam->pix);
1041 if (!cam->sensor_data) {
1042 cam->cam_hardware->disable(cam->hardware_data);
1043 printk(KERN_ERR CAM_NAME ": cannot initialize sensor\n");
1048 printk(KERN_INFO CAM_NAME ": %s interface with %s sensor\n",
1049 cam->cam_hardware->name, cam->cam_sensor->name);
1051 /* select an arbitrary default capture frame rate of 15fps */
1052 cam->nominal_timeperframe.numerator = 1;
1053 cam->nominal_timeperframe.denominator = 15;
1055 /* calculate xclk based on the default capture format and default
1058 cam->xclk = cam->cam_sensor->calc_xclk(&cam->pix,
1059 &cam->nominal_timeperframe, cam->sensor_data);
1060 cam->cparm.timeperframe = cam->nominal_timeperframe;
1062 /* initialise the wait queue */
1063 init_waitqueue_head(&cam->new_video_frame);
1065 /* Initialise the DMA structures */
1066 camera_core_sgdma_init(cam);
1068 /* Disable the Camera after detection */
1069 cam->cam_hardware->disable(cam->hardware_data);
1071 platform_set_drvdata(pdev, cam);
1073 if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1074 printk(KERN_ERR CAM_NAME
1075 ": could not register Video for Linux device\n");
1080 printk(KERN_INFO CAM_NAME
1081 ": registered device video%d [v4l2]\n", vfd->minor);
1086 cam->cam_sensor->cleanup(cam->sensor_data);
1088 cam->cam_hardware->cleanup(cam->hardware_data);
1090 dma_free_coherent(NULL, cam->overlay_size,
1091 (void *)cam->overlay_base,
1092 cam->overlay_base_phys);
1093 cam->overlay_base = 0;
1095 video_device_release(vfd);
1103 static int camera_core_remove(struct platform_device *pdev)
1105 struct camera_device *cam = platform_get_drvdata(pdev);
1106 struct video_device *vfd;
1110 if (vfd->minor == -1) {
1111 /* The device never got registered, so release the
1112 ** video_device struct directly
1114 video_device_release(vfd);
1116 /* The unregister function will release the video_device
1117 ** struct as well as unregistering it.
1119 video_unregister_device(vfd);
1123 if (cam->overlay_base) {
1124 dma_free_coherent(NULL, cam->overlay_size,
1125 (void *)cam->overlay_base,
1126 cam->overlay_base_phys);
1127 cam->overlay_base = 0;
1129 cam->overlay_base_phys = 0;
1131 cam->cam_sensor->cleanup(cam->sensor_data);
1132 cam->cam_hardware->cleanup(cam->hardware_data);
1139 static struct platform_driver camera_core_driver = {
1142 .owner = THIS_MODULE,
1144 .probe = camera_core_probe,
1145 .remove = camera_core_remove,
1147 .suspend = camera_core_suspend,
1148 .resume = camera_core_resume,
1152 /* FIXME register omap16xx or omap24xx camera device in arch/arm/...
1153 * system init code, with its resources and mux setup, NOT here.
1154 * Then MODULE_ALIAS(CAM_NAME) so it hotplugs and coldplugs; this
1155 * "legacy" driver style is trouble.
1157 static struct platform_device *cam;
1160 camera_core_cleanup(void)
1162 platform_driver_unregister(&camera_core_driver);
1163 platform_device_unregister(cam);
1166 static char banner[] __initdata = KERN_INFO "OMAP Camera driver initialzing\n";
1169 camera_core_init(void)
1173 platform_driver_register(&camera_core_driver);
1175 cam = platform_device_register_simple(CAM_NAME, -1, NULL, 0);
1180 MODULE_AUTHOR("Texas Instruments.");
1181 MODULE_DESCRIPTION("OMAP Video for Linux camera driver");
1182 MODULE_LICENSE("GPL");
1184 module_param(video_nr, int, 0);
1185 MODULE_PARM_DESC(video_nr,
1186 "Minor number for video device (-1 ==> auto assign)");
1187 module_param(capture_mem, int, 0);
1188 MODULE_PARM_DESC(capture_mem,
1189 "Maximum amount of memory for capture buffers (default 4800KB)");
1191 module_init(camera_core_init);
1192 module_exit(camera_core_cleanup);