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