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/init.h>
25 #include <linux/platform_device.h>
26 #include <linux/version.h>
27 #include <linux/dma-mapping.h>
30 #include <media/v4l2-common.h>
34 #include "camera_hw_if.h"
35 #include "camera_core.h"
37 #define OMAP1CAM_VERSION KERNEL_VERSION(0, 0, 0)
39 static struct camera_device *camera_dev;
40 static void camera_core_sgdma_process(struct camera_device *cam);
42 /* Module parameters */
43 static int video_nr = -1; /* Video device minor (-1 ==> auto assign) */
45 /* Maximum amount of memory to use for capture buffers.
46 * Default is 4800KB, enough to double-buffer SXGA.
48 static int capture_mem = 1280 * 960 * 2 * 2;
50 /* Size of video overlay framebuffer. This determines the maximum image size
51 * that can be previewed. Default is 600KB, enough for sxga.
53 static int overlay_mem = 640 * 480 * 2;
55 /* Enable the external sensor interface. Try to negotiate interface
56 * parameters with the sensor and start using the new ones. The calls
57 * to sensor_if_enable and sensor_if_disable do not need to be balanced.
59 static int camera_sensor_if_enable(struct camera_device *cam)
64 rval = vidioc_int_g_ifparm(cam->sdev, &p);
66 dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
70 cam->if_type = p.if_type;
73 case V4L2_IF_TYPE_BT656:
74 cam->if_u.bt656.xclk =
75 cam->cam_hardware->set_xclk(cam->if_u.bt656.xclk,
79 /* FIXME: how about other interfaces? */
80 dev_err(cam->dev, "interface type %d not supported\n",
88 static void camera_sensor_if_disable(const struct camera_device *cam)
90 switch (cam->if_type) {
91 case V4L2_IF_TYPE_BT656:
96 /* Initialize the sensor hardware. */
97 static int camera_sensor_init(struct camera_device *cam)
100 struct v4l2_int_device *sdev = cam->sdev;
102 /* Enable the xclk output. The sensor may (and does, in the case of
103 * the OV9640) require an xclk input in order for its initialization
107 /* Choose an arbitrary xclk frequency */
108 cam->if_u.bt656.xclk = 21000000;
110 cam->if_u.bt656.xclk = cam->cam_hardware->set_xclk(cam->if_u.bt656.xclk,
113 err = camera_sensor_if_enable(cam);
115 dev_err(cam->dev, "sensor interface could not be enabled at "
116 "initialization, %d\n", err);
121 /* Power up sensor during sensor initialization */
122 vidioc_int_s_power(sdev, 1);
124 err = vidioc_int_dev_init(sdev);
126 dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
127 /* Sensor initialization failed --- it's nonexistent to us! */
132 dev_info(cam->dev, "sensor is %s\n", sdev->name);
135 camera_sensor_if_disable(cam);
137 vidioc_int_s_power(sdev, 0);
142 static void camera_sensor_exit(struct camera_device *cam)
145 vidioc_int_dev_exit(cam->sdev);
148 /* DMA completion routine for the scatter-gather DMA fragments.
149 * This function is called when a scatter DMA fragment is completed
151 static void camera_core_callback_sgdma(void *arg1, void *arg2)
153 struct camera_device *cam = (struct camera_device *)arg1;
154 int sgslot = (int)arg2;
156 struct sgdma_state *sgdma;
158 spin_lock(&cam->sg_lock);
159 sgdma = cam->sgdma + sgslot;
160 if (!sgdma->queued_sglist) {
161 spin_unlock(&cam->sg_lock);
162 dev_err(cam->dev, "SGDMA completed when none queued\n");
165 if (!--sgdma->queued_sglist) {
166 /* Queue for this sglist is empty so check whether transfer
167 * of the frame has been completed
169 if (sgdma->next_sglist == sgdma->sglen) {
170 dma_callback_t callback = sgdma->callback;
171 void *arg = sgdma->arg;
172 /* All done with this sglist */
175 spin_unlock(&cam->sg_lock);
176 (*callback) (cam, arg);
177 camera_core_sgdma_process(cam);
182 spin_unlock(&cam->sg_lock);
183 camera_core_sgdma_process(cam);
188 static void camera_core_sgdma_init(struct camera_device *cam)
192 /* Initialize the underlying camera DMA */
193 cam->cam_hardware->init_dma(cam->hardware_data);
194 spin_lock_init(&cam->sg_lock);
196 cam->free_sgdma = NUM_SG_DMA;
198 for (sg = 0; sg < NUM_SG_DMA; sg++) {
199 cam->sgdma[sg].sglen = 0;
200 cam->sgdma[sg].next_sglist = 0;
201 cam->sgdma[sg].queued_sglist = 0;
202 cam->sgdma[sg].csr = 0;
203 cam->sgdma[sg].callback = NULL;
204 cam->sgdma[sg].arg = NULL;
208 /* Process the scatter-gather DMA queue by starting queued transfers
209 * This function is called to program the DMA to start the transfer of an image.
211 static void camera_core_sgdma_process(struct camera_device *cam)
213 unsigned long irqflags;
214 int queued_sgdma, sgslot;
215 struct sgdma_state *sgdma;
216 const struct scatterlist *sglist;
218 spin_lock_irqsave(&cam->sg_lock, irqflags);
219 if (1 == cam->in_use) {
220 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
224 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
226 queued_sgdma = NUM_SG_DMA - cam->free_sgdma;
227 sgslot = (cam->next_sgdma + cam->free_sgdma) % (NUM_SG_DMA);
228 while (queued_sgdma > 0) {
229 sgdma = cam->sgdma + sgslot;
230 while (sgdma->next_sglist < sgdma->sglen) {
231 sglist = sgdma->sglist + sgdma->next_sglist;
232 if (cam->cam_hardware->
233 start_dma(sgdma, camera_core_callback_sgdma,
234 (void *)cam, (void *)sgslot,
235 cam->hardware_data)) {
236 /* DMA start failed */
240 /* DMA start successful */
241 sgdma->next_sglist++;
242 sgdma->queued_sglist++;
246 sgslot = (sgslot + 1) % (NUM_SG_DMA);
252 /* Queue a scatter-gather DMA transfer from the camera to memory.
253 * Returns zero if the transfer was successfully queued, or
254 * non-zero if all of the scatter-gather slots are already in use.
256 static int camera_core_sgdma_queue(struct camera_device *cam,
257 const struct scatterlist *sglist,
258 int sglen, dma_callback_t callback,
261 unsigned long irqflags;
262 struct sgdma_state *sgdma;
264 if ((sglen < 0) || ((sglen > 0) & !sglist))
267 spin_lock_irqsave(&cam->sg_lock, irqflags);
269 if (!cam->free_sgdma) {
270 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
274 sgdma = cam->sgdma + cam->next_sgdma;
276 sgdma->sglist = sglist;
277 sgdma->sglen = sglen;
278 sgdma->next_sglist = 0;
279 sgdma->queued_sglist = 0;
281 sgdma->callback = callback;
284 cam->next_sgdma = (cam->next_sgdma + 1) % (NUM_SG_DMA);
287 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
289 camera_core_sgdma_process(cam);
294 /* -------------------overlay routines ------------------------------
295 * Callback routine for overlay DMA completion. We just start another DMA
296 * transfer unless overlay has been turned off
298 static void camera_core_overlay_callback(void *arg1, void *arg)
300 struct camera_device *cam = (struct camera_device *)arg1;
302 unsigned long irqflags;
305 unsigned char *fb_buf =
306 phys_to_virt((unsigned long)camera_dev->fbuf.base);
308 spin_lock_irqsave(&cam->overlay_lock, irqflags);
310 if (!cam->previewing || cam->overlay_cnt == 0) {
311 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
316 sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
317 sg_dma_len(&cam->overlay_sglist) = cam->pix.sizeimage;
320 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline);
321 for (i = 0; i < cam->pix.sizeimage; i += cam->pix.bytesperline) {
322 for (index = 0; index < cam->pix.bytesperline; index++) {
323 fb_buf[j] = *(((unsigned char *)cam->overlay_base) +
327 *(((unsigned char *)cam->overlay_base) + i + index);
328 j = j - cam->fbuf.fmt.bytesperline;
331 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline) + count;
334 while (cam->overlay_cnt < 2) {
335 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
336 camera_core_overlay_callback,
343 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
347 static void camera_core_start_overlay(struct camera_device *cam)
350 unsigned long irqflags;
352 if (!cam->previewing)
355 spin_lock_irqsave(&cam->overlay_lock, irqflags);
357 sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
358 sg_dma_len(&cam->overlay_sglist) = cam->pix.sizeimage;
359 while (cam->overlay_cnt < 2) {
360 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
361 camera_core_overlay_callback,
368 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
371 /* ------------------ videobuf_queue_ops --------------------------------
372 * This routine is called from interrupt context when a scatter-gather DMA
373 * transfer of a videobuf_buffer completes.
375 static void camera_core_vbq_complete(void *arg1, void *arg)
377 struct camera_device *cam = (struct camera_device *)arg1;
378 struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
380 spin_lock(&cam->vbq_lock);
382 do_gettimeofday(&vb->ts);
383 vb->field_count = cam->field_count;
384 cam->field_count += 2;
385 vb->state = STATE_DONE;
389 spin_unlock(&cam->vbq_lock);
392 static void camera_core_vbq_release(struct videobuf_queue *q,
393 struct videobuf_buffer *vb)
395 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
396 videobuf_waiton(vb, 0, 0);
397 videobuf_dma_unmap(q, dma);
398 videobuf_dma_free(dma);
400 vb->state = STATE_NEEDS_INIT;
403 /* Limit the number of available kernel image capture buffers based on the
404 * number requested, the currently selected image size, and the maximum
405 * amount of memory permitted for kernel capture buffers.
407 static int camera_core_vbq_setup(struct videobuf_queue *q, unsigned int *cnt,
410 struct camera_fh *fh = q->priv_data;
411 struct camera_device *cam = fh->cam;
414 *cnt = VIDEO_MAX_FRAME; /* Supply a default number of buffers */
416 if (*cnt > VIDEO_MAX_FRAME)
417 *cnt = VIDEO_MAX_FRAME;
419 spin_lock(&cam->img_lock);
420 *size = cam->pix.sizeimage;
421 spin_unlock(&cam->img_lock);
423 while (*size * *cnt > capture_mem)
429 static int camera_core_vbq_prepare(struct videobuf_queue *q,
430 struct videobuf_buffer *vb,
431 enum v4l2_field field)
433 struct camera_fh *fh = q->priv_data;
434 struct camera_device *cam = fh->cam;
437 spin_lock(&cam->img_lock);
438 if (cam->pix.sizeimage > vb->bsize) {
439 spin_unlock(&cam->img_lock);
442 vb->size = cam->pix.sizeimage;
443 vb->width = cam->pix.width;
444 vb->height = cam->pix.height;
446 spin_unlock(&cam->img_lock);
448 if (vb->state == STATE_NEEDS_INIT)
449 err = videobuf_iolock(q, vb, NULL);
452 vb->state = STATE_PREPARED;
454 camera_core_vbq_release(q, vb);
459 static void camera_core_vbq_queue(struct videobuf_queue *q,
460 struct videobuf_buffer *vb)
462 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
463 struct camera_fh *fh = q->priv_data;
464 struct camera_device *cam = fh->cam;
465 enum videobuf_state state = vb->state;
468 vb->state = STATE_QUEUED;
469 err = camera_core_sgdma_queue(cam, dma->sglist, dma->sglen,
470 camera_core_vbq_complete, vb);
472 /* Oops. We're not supposed to get any errors here. The only
473 * way we could get an error is if we ran out of scatter-gather
474 * DMA slots, but we are supposed to have at least as many
475 * scatter-gather DMA slots as video buffers so that can't
478 dev_dbg(cam->dev, "Failed to queue a video buffer for SGDMA\n");
483 /* IOCTL interface. */
484 static int vidioc_querycap(struct file *file, void *fh,
485 struct v4l2_capability *cap)
487 struct camera_fh *ofh = fh;
488 struct camera_device *cam = ofh->cam;
490 strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
491 strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
492 cap->version = OMAP1CAM_VERSION;
494 V4L2_CAP_VIDEO_CAPTURE |
495 V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
500 static int vidioc_enum_fmt_cap(struct file *file, void *fh,
501 struct v4l2_fmtdesc *f)
503 struct camera_fh *ofh = fh;
504 struct camera_device *cam = ofh->cam;
506 return vidioc_int_enum_fmt_cap(cam->sdev, f);
509 static int vidioc_g_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
511 struct camera_fh *ofh = fh;
512 struct camera_device *cam = ofh->cam;
514 /* Get the current format */
515 memset(&f->fmt.pix, 0, sizeof(f->fmt.pix));
516 f->fmt.pix = cam->pix;
521 static int vidioc_s_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
523 struct camera_fh *ofh = fh;
524 struct camera_device *cam = ofh->cam;
527 vidioc_int_try_fmt_cap(cam->sdev, f);
529 cam->pix = f->fmt.pix;
531 rval = vidioc_int_s_fmt_cap(cam->sdev, f);
532 camera_sensor_if_enable(cam);
537 static int vidioc_try_fmt_cap(struct file *file, void *fh,
538 struct v4l2_format *f)
540 struct camera_fh *ofh = fh;
541 struct camera_device *cam = ofh->cam;
543 return vidioc_int_try_fmt_cap(cam->sdev, f);
546 static int vidioc_reqbufs(struct file *file, void *fh,
547 struct v4l2_requestbuffers *b)
549 struct camera_fh *ofh = fh;
551 return videobuf_reqbufs(&ofh->vbq, b);
554 static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
556 struct camera_fh *ofh = fh;
558 return videobuf_querybuf(&ofh->vbq, b);
561 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
563 struct camera_fh *ofh = fh;
565 return videobuf_qbuf(&ofh->vbq, b);
568 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
570 struct camera_fh *ofh = fh;
572 return videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
575 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
577 struct camera_fh *ofh = fh;
578 struct camera_device *cam = ofh->cam;
580 spin_lock(&cam->img_lock);
582 if (cam->streaming || cam->reading) {
583 spin_unlock(&cam->img_lock);
586 cam->streaming = ofh;
587 /* FIXME: start camera interface */
589 spin_unlock(&cam->img_lock);
591 return videobuf_streamon(&ofh->vbq);
594 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
596 struct camera_fh *ofh = fh;
597 struct camera_device *cam = ofh->cam;
600 err = videobuf_streamoff(&ofh->vbq);
604 spin_lock(&cam->img_lock);
605 if (cam->streaming == ofh)
606 cam->streaming = NULL;
607 /* FIXME: stop camera interface */
609 spin_unlock(&cam->img_lock);
613 static int vidioc_enum_input(struct file *file, void *fh,
614 struct v4l2_input *inp)
619 strlcpy(inp->name, "camera", sizeof(inp->name));
620 inp->type = V4L2_INPUT_TYPE_CAMERA;
625 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
632 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
640 static int vidioc_queryctrl(struct file *file, void *fh,
641 struct v4l2_queryctrl *a)
643 struct camera_fh *ofh = fh;
644 struct camera_device *cam = ofh->cam;
646 return vidioc_int_queryctrl(cam->sdev, a);
649 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *a)
651 struct camera_fh *ofh = fh;
652 struct camera_device *cam = ofh->cam;
654 return vidioc_int_g_ctrl(cam->sdev, a);
657 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
659 struct camera_fh *ofh = fh;
660 struct camera_device *cam = ofh->cam;
662 return vidioc_int_s_ctrl(cam->sdev, a);
665 static int vidioc_g_fbuf(struct file *file, void *fh,
666 struct v4l2_framebuffer *a)
668 struct camera_fh *ofh = fh;
669 struct camera_device *cam = ofh->cam;
671 spin_lock(&cam->img_lock);
673 spin_unlock(&cam->img_lock);
678 static int vidioc_s_fbuf(struct file *file, void *fh,
679 struct v4l2_framebuffer *a)
681 struct camera_fh *ofh = fh;
682 struct camera_device *cam = ofh->cam;
684 spin_lock(&cam->img_lock);
685 if (cam->previewing) {
686 spin_unlock(&cam->img_lock);
689 cam->fbuf.base = a->base;
690 cam->fbuf.fmt = a->fmt;
692 spin_unlock(&cam->img_lock);
696 static int vidioc_overlay(struct file *file, void *fh, unsigned int i)
698 struct camera_fh *ofh = fh;
699 struct camera_device *cam = ofh->cam;
702 /* Check whether the capture format and
703 * the display format matches
704 * return failure if they are different
706 if (cam->pix.pixelformat != cam->fbuf.fmt.pixelformat)
709 /* If the camera image size is greater
710 * than LCD size return failure
712 if ((cam->pix.width > cam->fbuf.fmt.height) ||
713 (cam->pix.height > cam->fbuf.fmt.width))
716 if (!cam->previewing && enable) {
717 cam->previewing = fh;
718 cam->overlay_cnt = 0;
719 camera_core_start_overlay(cam);
721 cam->previewing = NULL;
726 /* File operations */
727 static unsigned int camera_core_poll(struct file *file,
728 struct poll_table_struct *wait)
733 /* Callback routine for read DMA completion. We just start another DMA
734 * transfer unless overlay has been turned off
736 static void camera_core_capture_callback(void *arg1, void *arg)
738 struct camera_device *cam = (struct camera_device *)arg1;
740 unsigned long irqflags;
743 spin_lock_irqsave(&cam->capture_lock, irqflags);
746 cam->capture_started = 0;
747 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
753 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
754 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
755 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
756 camera_core_capture_callback,
759 cam->capture_completed = 1;
761 /* Wake up any process which are waiting for the
764 wake_up_interruptible(&camera_dev->new_video_frame);
765 sg_dma_address(&cam->capture_sglist) =
766 cam->capture_base_phys;
767 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
769 camera_core_sgdma_queue(cam, &cam->capture_sglist,
771 camera_core_capture_callback,
776 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
779 static ssize_t camera_core_read(struct file *file, char *data, size_t count,
782 struct camera_fh *fh = file->private_data;
783 struct camera_device *cam = fh->cam;
785 unsigned long irqflags;
787 #if 0 /* Use video_buf to do capture */
789 for (i = 0; i < 14; i++)
790 videobuf_read_one(file, &fh->vbq, data, count, ppos);
791 i = videobuf_read_one(file, &fh->vbq, data, count, ppos);
795 if (!cam->capture_base) {
796 cam->capture_base = (unsigned long)
797 dma_alloc_coherent(NULL,
800 cam->capture_base_phys,
801 GFP_KERNEL | GFP_DMA);
803 if (!cam->capture_base) {
804 dev_err(cam->dev, "cannot allocate capture buffer\n");
808 spin_lock_irqsave(&cam->capture_lock, irqflags);
810 cam->capture_started = 1;
811 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
812 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
813 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
815 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
816 camera_core_capture_callback, NULL);
818 /* Wait till DMA is completed */
820 cam->capture_completed = 0;
821 while (cam->capture_completed == 0) {
822 timeout = interruptible_sleep_on_timeout
823 (&cam->new_video_frame, timeout);
825 dev_err(cam->dev, "timeout waiting video frame\n");
826 return -EIO; /* Time out */
829 /* Copy the data to the user buffer */
830 err = copy_to_user(data, (void *)cam->capture_base, cam->pix.sizeimage);
831 return (cam->pix.sizeimage - err);
835 static int camera_core_mmap(struct file *file, struct vm_area_struct *vma)
837 struct camera_fh *fh = file->private_data;
839 return videobuf_mmap_mapper(&fh->vbq, vma);
842 static int camera_core_release(struct inode *inode, struct file *file)
844 struct camera_fh *fh = file->private_data;
845 struct camera_device *cam = fh->cam;
847 file->private_data = NULL;
850 spin_lock(&cam->img_lock);
852 if (cam->previewing == fh)
853 cam->previewing = NULL;
854 if (cam->streaming == fh)
855 cam->streaming = NULL;
856 if (cam->reading == fh)
859 spin_unlock(&cam->img_lock);
861 camera_dev->cam_hardware->finish_dma(cam->hardware_data);
863 if (cam->capture_base) {
864 dma_free_coherent(NULL, cam->pix.sizeimage,
865 (void *)cam->capture_base,
866 cam->capture_base_phys);
867 cam->capture_base = 0;
868 cam->capture_base_phys = 0;
870 if (fh->vbq.read_buf) {
871 camera_core_vbq_release(&fh->vbq, fh->vbq.read_buf);
872 kfree(fh->vbq.read_buf);
875 module_put(cam->sdev->module);
877 cam->cam_hardware->close(cam->hardware_data);
882 static int camera_core_open(struct inode *inode, struct file *file)
884 int minor = iminor(inode);
885 struct camera_device *cam = camera_dev;
886 struct camera_fh *fh;
887 struct v4l2_format format;
890 if (!cam || !cam->vfd || (cam->vfd->minor != minor))
893 /* Allocate per-filehandle data */
894 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
897 file->private_data = fh;
899 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
901 spin_lock(&cam->img_lock);
902 if (cam->active == 1) {
903 dev_err(cam->dev, "Camera device Active\n");
904 spin_unlock(&cam->img_lock);
910 if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
911 spin_unlock(&cam->img_lock);
916 vidioc_int_g_fmt_cap(cam->sdev, &format);
917 spin_unlock(&cam->img_lock);
919 videobuf_queue_pci_init(&fh->vbq, &cam->vbq_ops, NULL, &cam->vbq_lock,
920 fh->type, V4L2_FIELD_NONE,
921 sizeof(struct videobuf_buffer), fh);
923 cam->capture_completed = 0;
924 cam->capture_started = 0;
926 if (cam->cam_hardware->open(cam->hardware_data)) {
927 dev_err(cam->dev, "Camera IF configuration failed\n");
932 rval = vidioc_s_fmt_cap(file, fh, &format);
934 dev_err(cam->dev, "Camera sensor configuration failed (%d)\n",
936 cam->cam_hardware->close(cam->hardware_data);
945 module_put(cam->sdev->module);
951 static int camera_core_suspend(struct platform_device *pdev, pm_message_t state)
953 struct camera_device *cam = platform_get_drvdata(pdev);
955 spin_lock(&cam->img_lock);
957 cam->cam_hardware->close(cam->hardware_data);
959 vidioc_int_s_power(cam->sdev, 0);
960 spin_unlock(&cam->img_lock);
965 static int camera_core_resume(struct platform_device *pdev)
967 struct camera_device *cam = platform_get_drvdata(pdev);
969 spin_lock(&cam->img_lock);
970 vidioc_int_s_power(cam->sdev, 1);
972 struct v4l2_format format;
974 cam->capture_completed = 1;
975 cam->cam_hardware->open(cam->hardware_data);
977 vidioc_int_g_fmt_cap(cam->sdev, &format);
978 vidioc_int_s_fmt_cap(cam->sdev, &format);
979 camera_sensor_if_enable(cam);
981 camera_core_sgdma_process(cam);
983 spin_unlock(&cam->img_lock);
987 #endif /* CONFIG_PM */
989 static struct file_operations camera_core_fops = {
990 .owner = THIS_MODULE,
992 .read = camera_core_read,
993 .poll = camera_core_poll,
994 .ioctl = video_ioctl2,
995 .mmap = camera_core_mmap,
996 .open = camera_core_open,
997 .release = camera_core_release,
999 static ssize_t camera_streaming_show(struct device *dev,
1000 struct device_attribute *attr, char *buf)
1002 struct camera_device *cam = dev_get_drvdata(dev);
1004 return sprintf(buf, "%s\n", cam->streaming ? "active" : "inactive");
1007 static DEVICE_ATTR(streaming, S_IRUGO, camera_streaming_show, NULL);
1009 static void camera_device_unregister(struct v4l2_int_device *s)
1011 struct camera_device *cam = s->u.slave->master->priv;
1013 camera_sensor_exit(cam);
1016 static int camera_device_register(struct v4l2_int_device *s)
1018 struct camera_device *cam = s->u.slave->master->priv;
1019 struct video_device *vfd;
1022 /* We already have a slave. */
1028 if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
1029 dev_err(cam->dev, "could not register sysfs entry\n");
1034 /* Initialize the video_device struct */
1035 vfd = cam->vfd = video_device_alloc();
1037 dev_err(cam->dev, " could not allocate video device struct\n");
1042 vfd->release = video_device_release;
1044 strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1045 vfd->type = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CHROMAKEY;
1047 /* Need to register for a VID_HARDWARE_* ID in videodev.h */
1048 vfd->fops = &camera_core_fops;
1049 video_set_drvdata(vfd, cam);
1052 vfd->vidioc_querycap = vidioc_querycap;
1053 vfd->vidioc_enum_fmt_cap = vidioc_enum_fmt_cap;
1054 vfd->vidioc_g_fmt_cap = vidioc_g_fmt_cap;
1055 vfd->vidioc_s_fmt_cap = vidioc_s_fmt_cap;
1056 vfd->vidioc_try_fmt_cap = vidioc_try_fmt_cap;
1057 vfd->vidioc_reqbufs = vidioc_reqbufs;
1058 vfd->vidioc_querybuf = vidioc_querybuf;
1059 vfd->vidioc_qbuf = vidioc_qbuf;
1060 vfd->vidioc_dqbuf = vidioc_dqbuf;
1061 vfd->vidioc_streamon = vidioc_streamon;
1062 vfd->vidioc_streamoff = vidioc_streamoff;
1063 vfd->vidioc_enum_input = vidioc_enum_input;
1064 vfd->vidioc_g_input = vidioc_g_input;
1065 vfd->vidioc_s_input = vidioc_s_input;
1066 vfd->vidioc_queryctrl = vidioc_queryctrl;
1067 vfd->vidioc_g_ctrl = vidioc_g_ctrl;
1068 vfd->vidioc_s_ctrl = vidioc_s_ctrl;
1069 vfd->vidioc_g_fbuf = vidioc_g_fbuf;
1070 vfd->vidioc_s_fbuf = vidioc_s_fbuf;
1071 vfd->vidioc_overlay = vidioc_overlay;
1073 dev_info(cam->dev, "%s interface with %s sensor\n",
1074 cam->cam_hardware->name, cam->sdev->name);
1076 if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1078 "could not register Video for Linux device\n");
1083 rval = camera_sensor_init(cam);
1087 /* Disable the Camera after detection */
1088 cam->cam_hardware->disable(cam->hardware_data);
1093 camera_device_unregister(s);
1098 static struct v4l2_int_master camera_master = {
1099 .attach = camera_device_register,
1100 .detach = camera_device_unregister,
1103 static struct v4l2_int_device camera = {
1104 .module = THIS_MODULE,
1106 .type = v4l2_int_type_master,
1108 .master = &camera_master
1112 static int __init camera_core_probe(struct platform_device *pdev)
1114 struct camera_device *cam;
1117 cam = kzalloc(sizeof(struct camera_device), GFP_KERNEL);
1119 dev_err(&pdev->dev, "could not allocate memory\n");
1124 platform_set_drvdata(pdev, cam);
1126 cam->dev = &pdev->dev;
1128 /* Initialize the camera interface */
1129 cam->cam_hardware = &camera_hardware_if;
1130 cam->hardware_data = cam->cam_hardware->init();
1131 if (!cam->hardware_data) {
1132 dev_err(cam->dev, "cannot initialize interface hardware\n");
1137 /* Save the pointer to camera device in a global variable */
1140 /* Initialize the videobuf queue ops */
1141 cam->vbq_ops.buf_setup = camera_core_vbq_setup;
1142 cam->vbq_ops.buf_prepare = camera_core_vbq_prepare;
1143 cam->vbq_ops.buf_queue = camera_core_vbq_queue;
1144 cam->vbq_ops.buf_release = camera_core_vbq_release;
1146 /* Initialize the overlay interface */
1147 cam->overlay_size = overlay_mem;
1148 if (cam->overlay_size > 0) {
1149 cam->overlay_base = (unsigned long)
1150 dma_alloc_coherent(NULL,
1153 cam->overlay_base_phys,
1154 GFP_KERNEL | GFP_DMA);
1155 if (!cam->overlay_base) {
1157 "cannot allocate overlay framebuffer\n");
1162 memset((void *)cam->overlay_base, 0, cam->overlay_size);
1163 spin_lock_init(&cam->overlay_lock);
1164 spin_lock_init(&cam->capture_lock);
1166 /* Initialize the spinlock used to serialize access to the image
1169 spin_lock_init(&cam->img_lock);
1171 /* Initialize the wait queue */
1172 init_waitqueue_head(&cam->new_video_frame);
1174 /* Initialize the DMA structures */
1175 camera_core_sgdma_init(cam);
1177 platform_set_drvdata(pdev, cam);
1181 if (v4l2_int_device_register(&camera))
1187 vidioc_int_dev_exit(cam->sdev);
1188 cam->overlay_base = 0;
1192 static int camera_core_remove(struct platform_device *pdev)
1194 struct camera_device *cam = platform_get_drvdata(pdev);
1195 struct video_device *vfd;
1199 if (vfd->minor == -1) {
1200 /* The device never got registered, so release the
1201 * video_device struct directly
1203 video_device_release(vfd);
1205 /* The unregister function will release the
1206 * video_device struct as well as unregistering it.
1208 video_unregister_device(vfd);
1212 if (cam->overlay_base) {
1213 dma_free_coherent(NULL, cam->overlay_size,
1214 (void *)cam->overlay_base,
1215 cam->overlay_base_phys);
1216 cam->overlay_base = 0;
1218 cam->overlay_base_phys = 0;
1220 vidioc_int_dev_exit(cam->sdev);
1221 cam->cam_hardware->cleanup(cam->hardware_data);
1228 static struct platform_driver camera_core_driver = {
1231 .owner = THIS_MODULE,
1233 .probe = camera_core_probe,
1234 .remove = camera_core_remove,
1236 .suspend = camera_core_suspend,
1237 .resume = camera_core_resume,
1241 /* FIXME register omap16xx or omap24xx camera device in arch/arm/...
1242 * system initialization code, with its resources and mux setup, NOT here.
1243 * Then MODULE_ALIAS(CAM_NAME) so it hotplugs and coldplugs; this
1244 * "legacy" driver style is trouble.
1246 static struct platform_device *cam;
1248 static void __exit camera_core_cleanup(void)
1250 platform_driver_unregister(&camera_core_driver);
1251 platform_device_unregister(cam);
1254 static char banner[] __initdata = KERN_INFO "OMAP Camera driver initializing\n";
1256 static int __init camera_core_init(void)
1260 platform_driver_register(&camera_core_driver);
1262 cam = platform_device_register_simple(CAM_NAME, -1, NULL, 0);
1267 MODULE_AUTHOR("Texas Instruments.");
1268 MODULE_DESCRIPTION("OMAP Video for Linux camera driver");
1269 MODULE_LICENSE("GPL");
1271 module_param(video_nr, int, 0);
1272 MODULE_PARM_DESC(video_nr,
1273 "Minor number for video device (-1 ==> auto assign)");
1274 module_param(capture_mem, int, 0);
1275 MODULE_PARM_DESC(capture_mem,
1276 "Maximum amount of memory for capture buffers "
1277 "(default 4800KB)");
1279 module_init(camera_core_init);
1280 module_exit(camera_core_cleanup);