]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/omap/camera_core.c
f2662647ee88d46b09278bf0c5e46384a2b8a7fc
[linux-2.6-omap-h63xx.git] / drivers / media / video / omap / camera_core.c
1 /*
2  * drivers/media/video/omap/camera_core.c
3  *
4  * Copyright (C) 2004 Texas Instruments, Inc. 
5  *
6  * Video-for-Linux (Version 2) camera capture driver for
7  * the OMAP H2 and H3 camera controller.
8  *
9  * Adapted from omap24xx driver written by Andy Lowe (source@mvista.com)
10  * Copyright (C) 2003-2004 MontaVista Software, Inc.
11  * 
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. 
15  * 
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. 
19  *
20  * History:
21  *   27/03/05   Vladimir Barinov - Added support for power management
22  */
23  
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/init.h>
27 #include <linux/fs.h>
28 #include <linux/vmalloc.h>
29 #include <linux/slab.h>
30 #include <linux/proc_fs.h>
31 #include <linux/ctype.h>
32 #include <linux/pagemap.h>
33 #include <linux/mm.h>
34 #include <linux/platform_device.h>
35 #include <linux/delay.h>
36 #include <linux/interrupt.h>
37 #include <linux/videodev.h>
38 #include <linux/pci.h>
39 #include <linux/version.h>
40 #include <asm/semaphore.h>
41 #include <asm/processor.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/fb.h>
44
45 #include <asm/io.h>
46 #include <asm/byteorder.h>
47 #include <asm/irq.h>
48
49 #include "sensor_if.h"
50 #include "camera_hw_if.h"
51 #include "camera_core.h"
52  
53 struct camera_device *camera_dev;
54 extern struct omap_camera_sensor camera_sensor_if;
55 extern struct camera_hardware camera_hardware_if;
56  
57 static void camera_core_sgdma_process(struct camera_device *cam);
58
59 /* module parameters */
60 static int video_nr = -1;       /* video device minor (-1 ==> auto assign) */
61
62 /* Maximum amount of memory to use for capture buffers.
63  * Default is 4800KB, enough to double-buffer SXGA.
64  */
65 static int capture_mem = 1280*960*2*2;
66
67 /*Size of video overlay framebuffer. This determines the maximum image size
68  *that can be previewed. Default is 600KB, enough for sxga.
69  */
70 static int overlay_mem = 640*480*2;
71
72  
73 /* DMA completion routine for the scatter-gather DMA fragments. */
74 /* This function is called when a scatter DMA fragment is completed */
75 static void
76 camera_core_callback_sgdma(void *arg1, void *arg2)
77 {
78         struct camera_device *cam = (struct camera_device *)arg1;
79         int sgslot = (int)arg2;
80
81         struct sgdma_state *sgdma;
82
83         spin_lock(&cam->sg_lock);
84         sgdma = cam->sgdma + sgslot;
85         if (!sgdma->queued_sglist)
86         {
87                 spin_unlock(&cam->sg_lock);
88                 printk(KERN_ERR CAM_NAME ": SGDMA completed when none queued\n");
89                 return;
90         }
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 */
98                         cam->free_sgdma++;
99                         if (callback) {
100                                 spin_unlock(&cam->sg_lock);
101                                 (*callback)(cam, arg);
102                                 camera_core_sgdma_process(cam);
103                                 return;
104                         }
105                 }
106         }
107         spin_unlock(&cam->sg_lock);
108         camera_core_sgdma_process(cam);
109
110         return;
111 }
112
113 static void
114 camera_core_sgdma_init(struct camera_device *cam)
115 {
116         int sg;
117
118         /* Initialize the underlying camera DMA */
119         cam->cam_hardware->init_dma(cam->hardware_data);
120         spin_lock_init(&cam->sg_lock);
121         
122         cam->free_sgdma = NUM_SG_DMA;
123         cam->next_sgdma = 0;
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;
131         }
132 }
133
134 /*
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.
137  */
138 static void
139 camera_core_sgdma_process(struct camera_device *cam)
140 {
141         unsigned long irqflags;
142         int queued_sgdma, sgslot;
143         struct sgdma_state *sgdma;
144         const struct scatterlist *sglist;
145         
146         spin_lock_irqsave(&cam->sg_lock, irqflags);
147         if (1 == cam->in_use) {
148                 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
149                 return;
150         }
151         cam->in_use = 1;
152         spin_unlock_irqrestore(&cam->sg_lock, irqflags);
153
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 */
163                                         cam->in_use = 0;
164                                         return;
165                         }
166                         else {
167                                 /* dma start successful */
168                                 sgdma->next_sglist ++;
169                                 sgdma->queued_sglist ++;
170                         }
171                 }
172                 queued_sgdma-- ;
173                 sgslot = (sgslot + 1) % (NUM_SG_DMA);
174         }
175
176         cam->in_use = 0;
177 }
178
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.
182  */
183 static int
184 camera_core_sgdma_queue(struct camera_device *cam,
185         const struct scatterlist *sglist, int sglen, dma_callback_t callback,
186         void *arg)
187 {
188         unsigned long irqflags;
189         struct sgdma_state *sgdma;
190
191         if ((sglen < 0) || ((sglen > 0) & !sglist))
192                 return -EINVAL;
193
194         spin_lock_irqsave(&cam->sg_lock, irqflags);
195
196         if (!cam->free_sgdma) {
197                 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
198                 return -EBUSY;
199         }
200
201         sgdma = cam->sgdma + cam->next_sgdma;
202
203         sgdma->sglist = sglist;
204         sgdma->sglen = sglen;
205         sgdma->next_sglist = 0;
206         sgdma->queued_sglist = 0;
207         sgdma->csr = 0;
208         sgdma->callback = callback;
209         sgdma->arg = arg;
210
211         cam->next_sgdma = (cam->next_sgdma + 1) % (NUM_SG_DMA); 
212         cam->free_sgdma--;
213
214         spin_unlock_irqrestore(&cam->sg_lock, irqflags);
215
216         camera_core_sgdma_process(cam);
217
218         return 0;
219 }
220
221
222 /* -------------------overlay routines ------------------------------*/
223 /* callback routine for overlay DMA completion. We just start another DMA
224  * transfer unless overlay has been turned off
225  */
226
227 static void
228 camera_core_overlay_callback(void *arg1, void *arg)
229 {
230         struct camera_device *cam = (struct camera_device *)arg1;
231         int err;
232         unsigned long irqflags;
233         int i, j;
234         int count, index;
235         unsigned char *fb_buf = phys_to_virt((unsigned long)camera_dev->fbuf.base);
236
237         spin_lock_irqsave(&cam->overlay_lock, irqflags);
238
239         if (!cam->previewing || cam->overlay_cnt == 0) {
240                 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
241                 return;
242         }
243
244         --cam->overlay_cnt;
245         sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
246         sg_dma_len(&cam->overlay_sglist) = cam->pix.sizeimage;
247
248         count = 0;
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) +
253                                                                  i + index);
254                         index++;
255                         fb_buf[j + 1] = *(((unsigned char *) cam->overlay_base) + i + index);
256                         j = j - cam->fbuf.fmt.bytesperline;
257                 }
258                 count += 2;
259                 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline) + count;
260         }
261
262         while (cam->overlay_cnt < 2) {
263                 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
264                         camera_core_overlay_callback, NULL);
265                 if (err)
266                         break;
267                 ++cam->overlay_cnt;
268         }
269
270         spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
271
272 }
273
274  
275 static void
276 camera_core_start_overlay(struct camera_device *cam)
277 {
278         int err;
279         unsigned long irqflags;
280
281         if (!cam->previewing) 
282                 return;
283
284         spin_lock_irqsave(&cam->overlay_lock, irqflags);
285
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);
291                 if (err)
292                         break;
293                 ++cam->overlay_cnt;
294         }
295
296         spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
297 }
298
299 /* ------------------ videobuf_queue_ops ---------------------------------------- */
300
301 /* This routine is called from interrupt context when a scatter-gather DMA
302  * transfer of a videobuf_buffer completes.
303  */
304 static void
305 camera_core_vbq_complete(void *arg1, void *arg)
306 {
307         struct camera_device *cam = (struct camera_device *)arg1;
308         struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
309
310         spin_lock(&cam->vbq_lock);
311
312         do_gettimeofday(&vb->ts);
313         vb->field_count = cam->field_count;
314         cam->field_count += 2;
315         vb->state = STATE_DONE;
316
317         wake_up(&vb->done);
318         
319         spin_unlock(&cam->vbq_lock);
320 }
321
322 static void
323 camera_core_vbq_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
324 {
325         videobuf_waiton(vb, 0, 0);
326         videobuf_dma_unmap(q, &vb->dma);
327         videobuf_dma_free(&vb->dma);
328
329         vb->state = STATE_NEEDS_INIT;
330 }
331
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.
335  */
336 static int
337 camera_core_vbq_setup(struct videobuf_queue *q, unsigned int *cnt, unsigned int *size)
338 {
339         struct camera_device *cam = q->priv_data;
340
341         if (*cnt <= 0)
342                 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
343
344         if (*cnt > VIDEO_MAX_FRAME)
345                 *cnt = VIDEO_MAX_FRAME;
346
347         spin_lock(&cam->img_lock);
348         *size = cam->pix.sizeimage;
349         spin_unlock(&cam->img_lock);
350
351         while (*size * *cnt > capture_mem)
352                 (*cnt)--;
353
354         return 0;
355 }
356
357 static int
358 camera_core_vbq_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
359         enum v4l2_field field)
360 {
361         struct camera_device *cam = q->priv_data;
362         int err = 0;
363
364         spin_lock(&cam->img_lock);
365         if (cam->pix.sizeimage > vb->bsize) {
366                 spin_unlock(&cam->img_lock);
367                 return -EINVAL;
368         }
369         vb->size = cam->pix.sizeimage; 
370         vb->width = cam->pix.width;
371         vb->height = cam->pix.height;
372         vb->field = field;
373         spin_unlock(&cam->img_lock);
374
375         if (vb->state == STATE_NEEDS_INIT)
376                 err = videobuf_iolock(q, vb, NULL);
377
378         if (!err)
379                 vb->state = STATE_PREPARED;
380         else
381                 camera_core_vbq_release (q, vb);
382
383         return err;
384 }
385
386 static void
387 camera_core_vbq_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
388 {
389         struct camera_device *cam = q->priv_data;
390         enum videobuf_state state = vb->state;
391         int err;
392
393         vb->state = STATE_QUEUED;
394         err = camera_core_sgdma_queue(cam, vb->dma.sglist, vb->dma.sglen,
395                 camera_core_vbq_complete, vb);
396         if (err) {
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
401                 * happen.
402                 */
403                 printk(KERN_DEBUG CAM_NAME
404                         ": Failed to queue a video buffer for SGDMA\n");
405                 vb->state = state;
406         }
407 }
408
409 /* ------------------ videobuf_queue_ops ---------------------------------------- */
410
411 static int
412 camera_core_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, 
413                      void *arg)
414 {
415         struct camera_fh *fh  = file->private_data;
416         struct camera_device *cam = fh->cam;
417         int err;
418
419         switch (cmd) {
420                 case VIDIOC_ENUMINPUT:
421                 {
422                         /* default handler assumes 1 video input (the camera) */
423                         struct v4l2_input *input = (struct v4l2_input *)arg;
424                         int index = input->index;
425
426                         memset(input, 0, sizeof(*input));
427                         input->index = index;
428
429                         if (index > 0)
430                                 return -EINVAL;
431
432                         strlcpy(input->name, "camera", sizeof(input->name));
433                         input->type = V4L2_INPUT_TYPE_CAMERA;
434
435                         return 0;
436                 }
437
438                 case VIDIOC_G_INPUT:
439                 {
440                         unsigned int *input = arg;
441                         *input = 0;
442
443                         return 0;
444                 }
445
446                 case VIDIOC_S_INPUT:
447                 {
448                         unsigned int *input = arg;
449
450                         if (*input > 0)
451                                 return -EINVAL;
452
453                         return 0;
454                 }
455
456                 case VIDIOC_ENUM_FMT:
457                 {
458                         struct v4l2_fmtdesc *fmt = arg;
459                         return cam->cam_sensor->enum_pixformat(fmt, cam->sensor_data);
460                 }       
461
462                 case VIDIOC_TRY_FMT:
463                 {
464                         struct v4l2_format *fmt = arg;
465                         return cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
466
467                 }
468
469                 case VIDIOC_G_FMT:
470                 {
471                         struct v4l2_format *fmt = arg;
472
473                         /* get the current format */
474                         memset(&fmt->fmt.pix, 0, sizeof (fmt->fmt.pix));
475                         fmt->fmt.pix = cam->pix;
476                         
477                         return 0;
478                 }
479
480                 case VIDIOC_S_FMT:
481                 {
482                         struct v4l2_format *fmt = arg;
483                         unsigned int temp_sizeimage = 0;
484
485                         temp_sizeimage = cam->pix.sizeimage;
486                         cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
487                         cam->pix = fmt->fmt.pix;
488
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);
495                 }
496
497                 case VIDIOC_QUERYCTRL:
498                 {
499                         struct v4l2_queryctrl *qc = arg;
500                         return cam->cam_sensor->query_control(qc, cam->sensor_data);
501                 }
502
503                 case VIDIOC_G_CTRL:
504                 {
505                         struct v4l2_control *vc = arg;
506                         return cam->cam_sensor->get_control(vc, cam->sensor_data);
507                 }
508
509                 case VIDIOC_S_CTRL:
510                 {
511                         struct v4l2_control *vc = arg;
512                         return cam->cam_sensor->set_control(vc, cam->sensor_data);
513                 }
514                 
515                 case VIDIOC_QUERYCAP:
516                 {
517                         struct v4l2_capability *cap = 
518                                 (struct v4l2_capability *) arg;
519
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);
525                         cap->capabilities =
526                                 V4L2_CAP_VIDEO_CAPTURE |
527                                 V4L2_CAP_VIDEO_OVERLAY |
528                                 V4L2_CAP_READWRITE | 
529                                 V4L2_CAP_STREAMING;
530                         return 0;
531                 }
532
533                 case VIDIOC_G_FBUF: /* Get the frame buffer parameters */
534                 {
535                         struct v4l2_framebuffer *fbuf =
536                                 (struct v4l2_framebuffer *) arg;
537
538                         spin_lock(&cam->img_lock);
539                         *fbuf = cam->fbuf;
540                         spin_unlock(&cam->img_lock);
541                         return 0;
542                 }
543
544                 case VIDIOC_S_FBUF: /* set the frame buffer parameters */
545                 {
546                         struct v4l2_framebuffer *fbuf =
547                                 (struct v4l2_framebuffer *) arg;
548
549                         spin_lock(&cam->img_lock);
550                         if (cam->previewing) {
551                                 spin_unlock(&cam->img_lock);
552                                 return -EBUSY;
553                         }
554                         cam->fbuf.base = fbuf->base;
555                         cam->fbuf.fmt = fbuf->fmt;      
556                         
557                         spin_unlock(&cam->img_lock);
558                         return 0;
559                 }
560
561                 case VIDIOC_OVERLAY:
562                 {
563                         int enable = *((int *) arg);
564
565                         /* 
566                          * check whether the capture format and 
567                          ** the display format matches 
568                          * return failure if they are different
569                          */
570                         if (cam->pix.pixelformat != cam->fbuf.fmt.pixelformat)
571                         {
572                                 return -EINVAL;
573                         }
574
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))
579                         {
580                                 return -EINVAL;
581                         }
582                         
583                         if (!cam->previewing && enable)
584                         {
585                                 cam->previewing = fh;
586                                 cam->overlay_cnt = 0;
587                                 camera_core_start_overlay(cam);
588                         }
589                         else if (!enable)
590                         {
591                                 cam->previewing = NULL;
592                         }
593         
594                         return 0;
595                 }
596
597                 case VIDIOC_REQBUFS:
598                         return videobuf_reqbufs(&fh->vbq, arg);
599
600                 case VIDIOC_QUERYBUF:
601                         return videobuf_querybuf(&fh->vbq, arg);
602
603                 case VIDIOC_QBUF:
604                         return videobuf_qbuf(&fh->vbq, arg);
605
606                 case VIDIOC_DQBUF:
607                         return videobuf_dqbuf(&fh->vbq, arg,
608            file->f_flags & O_NONBLOCK);
609
610                 case VIDIOC_STREAMON:
611                 {
612                         spin_lock(&cam->img_lock);
613
614                         if (cam->streaming || cam->reading) {
615                                 spin_unlock(&cam->img_lock);
616                                 return -EBUSY;
617                         }
618                         else {
619                                 cam->streaming = fh;
620                                 /* FIXME: start camera interface */
621                         }
622
623                         spin_unlock(&cam->img_lock);
624
625                         return videobuf_streamon(&fh->vbq);
626                 }
627                 case VIDIOC_STREAMOFF:
628                 {
629                         err = videobuf_streamoff(&fh->vbq);
630                         if (err < 0)
631                                 return err;
632
633                         spin_lock(&cam->img_lock);
634                         if (cam->streaming == fh) {
635                                 cam->streaming = NULL;
636                                 /* FIXME: stop camera interface */
637                         }
638                         spin_unlock(&cam->img_lock);
639                         return 0;
640                 }
641                 case VIDIOC_ENUMSTD:
642                 case VIDIOC_G_STD:
643                 case VIDIOC_S_STD:
644                 case VIDIOC_QUERYSTD:
645                 {
646                         /* Digital cameras don't have an analog video standard, 
647                          * so we don't need to implement these ioctls.
648                          */
649                          return -EINVAL;
650                 }
651                 case VIDIOC_G_AUDIO:
652                 case VIDIOC_S_AUDIO:
653                 case VIDIOC_G_AUDOUT:
654                 case VIDIOC_S_AUDOUT:
655                 {
656                         /* we don't have any audio inputs or outputs */
657                         return -EINVAL;
658                 }
659
660                 case VIDIOC_G_JPEGCOMP:
661                 case VIDIOC_S_JPEGCOMP:
662                 {
663                         /* JPEG compression is not supported */
664                         return -EINVAL;
665                 }
666
667                 case VIDIOC_G_TUNER:
668                 case VIDIOC_S_TUNER:
669                 case VIDIOC_G_MODULATOR:
670                 case VIDIOC_S_MODULATOR:
671                 case VIDIOC_G_FREQUENCY:
672                 case VIDIOC_S_FREQUENCY:
673                 {
674                         /* we don't have a tuner or modulator */
675                         return -EINVAL;
676                 }
677
678                 case VIDIOC_ENUMOUTPUT:
679                 case VIDIOC_G_OUTPUT:
680                 case VIDIOC_S_OUTPUT:
681                 {
682                         /* we don't have any video outputs */
683                         return -EINVAL;
684                 }
685
686                 default:
687                 {
688                         /* unrecognized ioctl */
689                         return -ENOIOCTLCMD;
690                 }
691         }
692         return 0;
693 }
694
695 /*
696  *  file operations
697  */
698
699 static unsigned
700 int camera_core_poll(struct file *file, struct poll_table_struct *wait)
701 {
702         return -EINVAL;
703 }
704
705 /* ------------------------------------------------------------ */
706 /* callback routine for read DMA completion. We just start another DMA
707  * transfer unless overlay has been turned off
708  */
709 static void
710 camera_core_capture_callback(void *arg1, void *arg)
711 {
712         struct camera_device *cam = (struct camera_device *)arg1;
713         int err;
714         unsigned long irqflags;
715         static int done = 0;
716
717         spin_lock_irqsave(&cam->capture_lock, irqflags);
718         if (!cam->reading)
719         {
720                 done = 0;
721                 cam->capture_started = 0;
722                 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
723                 return;
724         }
725
726         if (done < 14) {
727                 ++done;
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);            
732         } else {
733                 cam->capture_completed = 1;
734                 if (cam->reading)
735                 {
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);
743                 }
744         }
745
746         spin_unlock_irqrestore(&cam->capture_lock, irqflags);
747 }
748
749  
750 static ssize_t
751 camera_core_read(struct file *file, char *data, size_t count, loff_t *ppos)
752 {
753         struct camera_fh *fh = file->private_data;
754         struct camera_device *cam = fh->cam;
755         int err;
756         unsigned long irqflags;
757         long timeout;
758 #if 0   /* use video_buf to do capture */
759         int i;
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);
763         return i;
764 #endif
765         
766         if (!cam->capture_base) {
767                 cam->capture_base = (unsigned long)dma_alloc_coherent(NULL,
768                                 cam->pix.sizeimage,
769                                 (dma_addr_t *) &cam->capture_base_phys,
770                                 GFP_KERNEL | GFP_DMA);
771         }
772         if (!cam->capture_base) {
773                 printk(KERN_ERR CAM_NAME
774                         ": cannot allocate capture buffer\n");
775                 return 0;
776         }
777
778         spin_lock_irqsave(&cam->capture_lock, irqflags);
779         cam->reading = fh;
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);
784
785         err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
786                         camera_core_capture_callback, NULL);
787
788         /* Wait till DMA is completed */
789         timeout = HZ * 10;
790         cam->capture_completed = 0;
791         while (cam->capture_completed == 0) {
792                 timeout = interruptible_sleep_on_timeout 
793                                 (&cam->new_video_frame, timeout);
794                 if (timeout == 0) {
795                         printk(KERN_ERR CAM_NAME ": timeout waiting video frame\n");    
796                         return -EIO; /* time out */
797                 }
798         }
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);
802         
803 }
804
805 static int
806 camera_core_mmap(struct file *file, struct vm_area_struct *vma)
807 {
808         struct camera_fh *fh = file->private_data;
809
810         return videobuf_mmap_mapper(&fh->vbq, vma);
811 }
812
813 static int
814 camera_core_ioctl(struct inode *inode, struct file *file, unsigned int cmd, 
815                   unsigned long arg)
816 {
817
818         return video_usercopy(inode, file, cmd, arg, camera_core_do_ioctl);
819 }
820
821 static int
822 camera_core_release(struct inode *inode, struct file *file)
823 {
824         struct camera_fh *fh = file->private_data;
825         struct camera_device *cam = fh->cam;
826         
827         file->private_data = NULL;
828         kfree(fh);
829
830         spin_lock(&cam->img_lock);
831         if (cam->previewing == fh) {
832                 cam->previewing = NULL;
833         }
834         if (cam->streaming == fh) {
835                 cam->streaming = NULL;
836         }
837         if (cam->reading == fh) {
838                 cam->reading = NULL;
839         }
840         spin_unlock(&cam->img_lock);
841
842         camera_dev->cam_hardware->finish_dma(cam->hardware_data);
843
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;
850         }
851         if (fh->vbq.read_buf) {
852                 camera_core_vbq_release(&fh->vbq, fh->vbq.read_buf);
853                 kfree(fh->vbq.read_buf);
854         }
855
856         cam->cam_hardware->close(cam->hardware_data);
857         cam->active = 0;
858         return 0;
859 }
860
861 static int
862 camera_core_open(struct inode *inode, struct file *file)
863 {
864         int minor = iminor(inode);
865         struct camera_device *cam = camera_dev;
866         struct camera_fh *fh;
867
868         if (!cam || !cam->vfd || (cam->vfd->minor != minor))
869                 return -ENODEV;
870
871         /* allocate per-filehandle data */
872         fh = kmalloc(sizeof(*fh), GFP_KERNEL);
873         if (NULL == fh)
874                 return -ENOMEM;
875         file->private_data = fh;
876         fh->cam = cam;
877         fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
878
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);
883                 return -EPERM;
884         }
885         cam->active = 1;
886         spin_unlock(&cam->img_lock);
887
888         videobuf_queue_init(&fh->vbq, &cam->vbq_ops, NULL, &cam->vbq_lock,
889                 fh->type, V4L2_FIELD_NONE, sizeof(struct videobuf_buffer), fh);
890
891         cam->capture_completed = 0;
892         cam->capture_started = 0;
893
894         if (cam->cam_hardware->open(cam->hardware_data))
895         {
896                 printk (KERN_ERR CAM_NAME ": Camera IF configuration failed\n");
897                 cam->active = 0;
898                 return -ENODEV;
899         }
900         
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))
905         {
906                 printk (KERN_ERR CAM_NAME ": Camera sensor configuration failed\n");
907                 cam->cam_hardware->close(cam->hardware_data);
908                 cam->active = 0;
909                 return -ENODEV;
910         }
911
912         return 0;
913 }
914
915 #ifdef CONFIG_PM
916 static int camera_core_suspend(struct platform_device *pdev, pm_message_t state)
917 {
918         struct camera_device *cam = platform_get_drvdata(pdev);
919         int ret = 0;
920
921         spin_lock(&cam->img_lock);
922         if (cam->active) {
923                 cam->cam_hardware->close(cam->hardware_data);
924         }
925         cam->cam_sensor->power_off(cam->sensor_data);
926         spin_unlock(&cam->img_lock);
927
928         return ret;
929 }
930
931 static int camera_core_resume(struct platform_device *pdev)
932 {
933         struct camera_device *cam = platform_get_drvdata(pdev);
934         int ret = 0;
935
936         spin_lock(&cam->img_lock);
937         cam->cam_sensor->power_on(cam->sensor_data);
938         if (cam->active) {
939                 cam->capture_completed = 1;
940                 cam->cam_hardware->open(cam->hardware_data);
941                 cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
942
943                 cam->cam_sensor->configure(&cam->pix, cam->xclk,
944                                            &cam->cparm.timeperframe,
945                                            cam->sensor_data);
946                 camera_core_sgdma_process(cam);
947         }
948         spin_unlock(&cam->img_lock);
949         
950         return ret;
951 }
952 #endif  /* CONFIG_PM */
953
954 static struct file_operations camera_core_fops = 
955 {
956         .owner                  = THIS_MODULE,
957         .llseek                 = no_llseek,
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,
964 };
965
966 static int __init camera_core_probe(struct platform_device *pdev)
967 {
968         struct camera_device *cam;
969         struct video_device *vfd;
970         int     status;
971
972         cam = kzalloc(sizeof(struct camera_device), GFP_KERNEL);
973         if (!cam) {
974                 printk(KERN_ERR CAM_NAME ": could not allocate memory\n");
975                 status = -ENOMEM;
976                 goto err0;
977         }
978
979         /* Save the pointer to camera device in a global variable */
980         camera_dev = cam;
981         
982         /* initialize the video_device struct */
983         vfd = cam->vfd = video_device_alloc();
984         if (!vfd) {
985                 printk(KERN_ERR CAM_NAME 
986                         ": could not allocate video device struct\n");
987                 status = -ENOMEM;
988                 goto err1;
989         }
990         
991         vfd->release = video_device_release;
992
993         strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
994         vfd->type = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CHROMAKEY;
995         
996         /* need to register for a VID_HARDWARE_* ID in videodev.h */
997         vfd->hardware = 0;
998         vfd->fops = &camera_core_fops;
999         video_set_drvdata(vfd, cam);
1000         vfd->minor = -1;
1001
1002         /* initialize the videobuf queue ops */
1003         cam->vbq_ops.buf_setup = camera_core_vbq_setup;
1004         cam->vbq_ops.buf_prepare = camera_core_vbq_prepare;
1005         cam->vbq_ops.buf_queue = camera_core_vbq_queue;
1006         cam->vbq_ops.buf_release = camera_core_vbq_release;
1007
1008         /* initilize the overlay interface */
1009         cam->overlay_size = overlay_mem;
1010         if (cam->overlay_size > 0)
1011         {
1012                 cam->overlay_base = (unsigned long) dma_alloc_coherent(NULL,
1013                                         cam->overlay_size,
1014                                         (dma_addr_t *) &cam->overlay_base_phys,
1015                                         GFP_KERNEL | GFP_DMA);
1016                 if (!cam->overlay_base) {
1017                         printk(KERN_ERR CAM_NAME
1018                                 ": cannot allocate overlay framebuffer\n");
1019                         status = -ENOMEM;
1020                         goto err2;
1021                 }
1022         }
1023         memset((void*)cam->overlay_base, 0, cam->overlay_size);
1024         spin_lock_init(&cam->overlay_lock);
1025         spin_lock_init(&cam->capture_lock);
1026
1027         /*Initialise the pointer to the sensor interface and camera interface */
1028         cam->cam_sensor = &camera_sensor_if;
1029         cam->cam_hardware = &camera_hardware_if;
1030
1031         /* initialize the camera interface */
1032         cam->hardware_data = cam->cam_hardware->init();
1033         if (!cam->hardware_data) {
1034                 printk(KERN_ERR CAM_NAME ": cannot initialize interface hardware\n");
1035                 status = -ENODEV;
1036                 goto err3;
1037         }
1038          
1039         /* initialize the spinlock used to serialize access to the image 
1040          * parameters
1041          */
1042         spin_lock_init(&cam->img_lock);
1043
1044         /* initialize the streaming capture parameters */
1045         cam->cparm.capability = V4L2_CAP_TIMEPERFRAME;
1046         cam->cparm.readbuffers = 1;
1047
1048         /* Enable the xclk output.  The sensor may (and does, in the case of 
1049          * the OV9640) require an xclk input in order for its initialization 
1050          * routine to work.
1051          */
1052         cam->xclk = 21000000;   /* choose an arbitrary xclk frequency */
1053         cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
1054
1055         /* initialize the sensor and define a default capture format cam->pix */
1056         cam->sensor_data = cam->cam_sensor->init(&cam->pix);
1057         if (!cam->sensor_data) {
1058                 cam->cam_hardware->disable(cam->hardware_data);
1059                 printk(KERN_ERR CAM_NAME ": cannot initialize sensor\n");
1060                 status = -ENODEV;
1061                 goto err4;
1062         }
1063
1064         printk(KERN_INFO CAM_NAME ": %s interface with %s sensor\n",
1065                 cam->cam_hardware->name, cam->cam_sensor->name);
1066
1067         /* select an arbitrary default capture frame rate of 15fps */
1068         cam->nominal_timeperframe.numerator = 1;
1069         cam->nominal_timeperframe.denominator = 15;
1070
1071         /* calculate xclk based on the default capture format and default 
1072          * frame rate
1073          */
1074         cam->xclk = cam->cam_sensor->calc_xclk(&cam->pix,
1075                 &cam->nominal_timeperframe, cam->sensor_data);
1076         cam->cparm.timeperframe = cam->nominal_timeperframe;
1077
1078         /* initialise the wait queue */
1079         init_waitqueue_head(&cam->new_video_frame);
1080
1081         /* Initialise the DMA structures */
1082         camera_core_sgdma_init(cam);
1083
1084         /* Disable the Camera after detection */
1085         cam->cam_hardware->disable(cam->hardware_data);
1086         
1087         platform_set_drvdata(pdev, cam);
1088         
1089         if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1090                 printk(KERN_ERR CAM_NAME 
1091                         ": could not register Video for Linux device\n");
1092                 status = -ENODEV;
1093                 goto err5;
1094         }
1095
1096         printk(KERN_INFO CAM_NAME 
1097                ": registered device video%d [v4l2]\n", vfd->minor);
1098
1099         return 0;
1100
1101  err5:
1102         cam->cam_sensor->cleanup(cam->sensor_data);
1103  err4:
1104         cam->cam_hardware->cleanup(cam->hardware_data);
1105  err3:
1106         dma_free_coherent(NULL, cam->overlay_size,
1107                                 (void *)cam->overlay_base, 
1108                                 cam->overlay_base_phys);
1109         cam->overlay_base = 0;
1110  err2:
1111         video_device_release(vfd);
1112  err1:
1113         kfree(cam);
1114         camera_dev = NULL;
1115  err0:
1116         return status;
1117 }
1118
1119 static int camera_core_remove(struct platform_device *pdev)
1120 {
1121         struct camera_device *cam = platform_get_drvdata(pdev);
1122         struct video_device *vfd;
1123
1124         vfd = cam->vfd;
1125         if (vfd) {
1126                 if (vfd->minor == -1) {
1127                         /* The device never got registered, so release the 
1128                         ** video_device struct directly
1129                         */
1130                         video_device_release(vfd);
1131                 } else {
1132                         /* The unregister function will release the video_device
1133                         ** struct as well as unregistering it.
1134                         */
1135                         video_unregister_device(vfd);
1136                 }
1137                 cam->vfd = NULL;
1138         }
1139         if (cam->overlay_base) {
1140                 dma_free_coherent(NULL, cam->overlay_size,
1141                                         (void *)cam->overlay_base, 
1142                                         cam->overlay_base_phys);
1143                 cam->overlay_base = 0;
1144         }       
1145         cam->overlay_base_phys = 0;
1146
1147         cam->cam_sensor->cleanup(cam->sensor_data);
1148         cam->cam_hardware->cleanup(cam->hardware_data);
1149         kfree(cam);
1150         camera_dev = NULL;
1151
1152         return 0;
1153 }
1154
1155 static struct platform_driver camera_core_driver = {
1156         .driver = {
1157                 .name           = CAM_NAME,
1158                 .owner          = THIS_MODULE,
1159         },
1160         .probe                  = camera_core_probe,
1161         .remove                 = camera_core_remove,
1162 #ifdef CONFIG_PM
1163         .suspend                = camera_core_suspend,
1164         .resume                 = camera_core_resume,
1165 #endif
1166 };
1167
1168 static struct platform_device camera_core_device = {
1169         .name   = CAM_NAME,
1170         .dev    = {
1171                         .release        = NULL,
1172                   },
1173         .id     = 0,
1174 };
1175
1176 void __exit
1177 camera_core_cleanup(void)
1178 {
1179         platform_driver_unregister(&camera_core_driver);
1180         platform_device_unregister(&camera_core_device);
1181
1182         return;
1183 }
1184
1185 static char banner[] __initdata = KERN_INFO "OMAP Camera driver initialzing\n";
1186
1187 int __init 
1188 camera_core_init(void)
1189 {
1190
1191         printk(banner);
1192         platform_device_register(&camera_core_device);
1193         platform_driver_register(&camera_core_driver);
1194
1195         return 0;
1196 }
1197
1198 MODULE_AUTHOR("Texas Instruments.");
1199 MODULE_DESCRIPTION("OMAP Video for Linux camera driver");
1200 MODULE_LICENSE("GPL");
1201 module_param(video_nr, int, 0);
1202 MODULE_PARM_DESC(video_nr, 
1203                 "Minor number for video device (-1 ==> auto assign)");
1204 module_param(capture_mem, int, 0);
1205 MODULE_PARM_DESC(capture_mem,
1206         "Maximum amount of memory for capture buffers (default 4800KB)");
1207
1208 module_init(camera_core_init);
1209 module_exit(camera_core_cleanup);
1210