]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/omap/camera_core.c
REMOVE OMAP LEGACY CODE: Delete all old omap specific sound drivers
[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/init.h>
25 #include <linux/platform_device.h>
26 #include <linux/version.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/fb.h>
29
30 #include <media/v4l2-common.h>
31
32 #include <asm/io.h>
33
34 #include "camera_hw_if.h"
35 #include "camera_core.h"
36
37 #define OMAP1CAM_VERSION KERNEL_VERSION(0, 0, 0)
38
39 static struct camera_device *camera_dev;
40 static void camera_core_sgdma_process(struct camera_device *cam);
41
42 /* Module parameters */
43 static int video_nr = -1;       /* Video device minor (-1 ==> auto assign) */
44
45 /* Maximum amount of memory to use for capture buffers.
46  * Default is 4800KB, enough to double-buffer SXGA.
47  */
48 static int capture_mem = 1280 * 960 * 2 * 2;
49
50 /* Size of video overlay framebuffer. This determines the maximum image size
51  * that can be previewed. Default is 600KB, enough for sxga.
52  */
53 static int overlay_mem = 640 * 480 * 2;
54
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.
58  */
59 static int camera_sensor_if_enable(struct camera_device *cam)
60 {
61         int rval;
62         struct v4l2_ifparm p;
63
64         rval = vidioc_int_g_ifparm(cam->sdev, &p);
65         if (rval) {
66                 dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
67                 return rval;
68         }
69
70         cam->if_type = p.if_type;
71
72         switch (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,
76                                                 cam->hardware_data);
77                 break;
78         default:
79                 /* FIXME: how about other interfaces? */
80                 dev_err(cam->dev, "interface type %d not supported\n",
81                         p.if_type);
82                 return -EINVAL;
83         }
84
85         return 0;
86 }
87
88 static void camera_sensor_if_disable(const struct camera_device *cam)
89 {
90         switch (cam->if_type) {
91         case V4L2_IF_TYPE_BT656:
92                 break;
93         }
94 }
95
96 /* Initialize the sensor hardware. */
97 static int camera_sensor_init(struct camera_device *cam)
98 {
99         int err = 0;
100         struct v4l2_int_device *sdev = cam->sdev;
101
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
104          * routine to work.
105          */
106
107         /* Choose an arbitrary xclk frequency */
108         cam->if_u.bt656.xclk = 21000000;
109
110         cam->if_u.bt656.xclk = cam->cam_hardware->set_xclk(cam->if_u.bt656.xclk,
111                                                            cam->hardware_data);
112
113         err = camera_sensor_if_enable(cam);
114         if (err) {
115                 dev_err(cam->dev, "sensor interface could not be enabled at "
116                         "initialization, %d\n", err);
117                 cam->sdev = NULL;
118                 goto out;
119         }
120
121         /* Power up sensor during sensor initialization */
122         vidioc_int_s_power(sdev, 1);
123
124         err = vidioc_int_dev_init(sdev);
125         if (err) {
126                 dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
127                 /* Sensor initialization failed --- it's nonexistent to us! */
128                 cam->sdev = NULL;
129                 goto out;
130         }
131
132         dev_info(cam->dev, "sensor is %s\n", sdev->name);
133
134 out:
135         camera_sensor_if_disable(cam);
136
137         vidioc_int_s_power(sdev, 0);
138
139         return err;
140 }
141
142 static void camera_sensor_exit(struct camera_device *cam)
143 {
144         if (cam->sdev)
145                 vidioc_int_dev_exit(cam->sdev);
146 }
147
148 /* DMA completion routine for the scatter-gather DMA fragments.
149  * This function is called when a scatter DMA fragment is completed
150  */
151 static void camera_core_callback_sgdma(void *arg1, void *arg2)
152 {
153         struct camera_device *cam = (struct camera_device *)arg1;
154         int sgslot = (int)arg2;
155
156         struct sgdma_state *sgdma;
157
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");
163                 return;
164         }
165         if (!--sgdma->queued_sglist) {
166                 /* Queue for this sglist is empty so check whether transfer
167                  * of the frame has been completed
168                  */
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 */
173                         cam->free_sgdma++;
174                         if (callback) {
175                                 spin_unlock(&cam->sg_lock);
176                                 (*callback) (cam, arg);
177                                 camera_core_sgdma_process(cam);
178                                 return;
179                         }
180                 }
181         }
182         spin_unlock(&cam->sg_lock);
183         camera_core_sgdma_process(cam);
184
185         return;
186 }
187
188 static void camera_core_sgdma_init(struct camera_device *cam)
189 {
190         int sg;
191
192         /* Initialize the underlying camera DMA */
193         cam->cam_hardware->init_dma(cam->hardware_data);
194         spin_lock_init(&cam->sg_lock);
195
196         cam->free_sgdma = NUM_SG_DMA;
197         cam->next_sgdma = 0;
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;
205         }
206 }
207
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.
210  */
211 static void camera_core_sgdma_process(struct camera_device *cam)
212 {
213         unsigned long irqflags;
214         int queued_sgdma, sgslot;
215         struct sgdma_state *sgdma;
216         const struct scatterlist *sglist;
217
218         spin_lock_irqsave(&cam->sg_lock, irqflags);
219         if (1 == cam->in_use) {
220                 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
221                 return;
222         }
223         cam->in_use = 1;
224         spin_unlock_irqrestore(&cam->sg_lock, irqflags);
225
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 */
237                                 cam->in_use = 0;
238                                 return;
239                         } else {
240                                 /* DMA start successful */
241                                 sgdma->next_sglist++;
242                                 sgdma->queued_sglist++;
243                         }
244                 }
245                 queued_sgdma--;
246                 sgslot = (sgslot + 1) % (NUM_SG_DMA);
247         }
248
249         cam->in_use = 0;
250 }
251
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.
255  */
256 static int camera_core_sgdma_queue(struct camera_device *cam,
257                                    const struct scatterlist *sglist,
258                                    int sglen, dma_callback_t callback,
259                                    void *arg)
260 {
261         unsigned long irqflags;
262         struct sgdma_state *sgdma;
263
264         if ((sglen < 0) || ((sglen > 0) & !sglist))
265                 return -EINVAL;
266
267         spin_lock_irqsave(&cam->sg_lock, irqflags);
268
269         if (!cam->free_sgdma) {
270                 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
271                 return -EBUSY;
272         }
273
274         sgdma = cam->sgdma + cam->next_sgdma;
275
276         sgdma->sglist = sglist;
277         sgdma->sglen = sglen;
278         sgdma->next_sglist = 0;
279         sgdma->queued_sglist = 0;
280         sgdma->csr = 0;
281         sgdma->callback = callback;
282         sgdma->arg = arg;
283
284         cam->next_sgdma = (cam->next_sgdma + 1) % (NUM_SG_DMA);
285         cam->free_sgdma--;
286
287         spin_unlock_irqrestore(&cam->sg_lock, irqflags);
288
289         camera_core_sgdma_process(cam);
290
291         return 0;
292 }
293
294 /* -------------------overlay routines ------------------------------
295  * Callback routine for overlay DMA completion. We just start another DMA
296  * transfer unless overlay has been turned off
297  */
298 static void camera_core_overlay_callback(void *arg1, void *arg)
299 {
300         struct camera_device *cam = (struct camera_device *)arg1;
301         int err;
302         unsigned long irqflags;
303         int i, j;
304         int count, index;
305         unsigned char *fb_buf =
306             phys_to_virt((unsigned long)camera_dev->fbuf.base);
307
308         spin_lock_irqsave(&cam->overlay_lock, irqflags);
309
310         if (!cam->previewing || cam->overlay_cnt == 0) {
311                 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
312                 return;
313         }
314
315         --cam->overlay_cnt;
316         sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
317         sg_dma_len(&cam->overlay_sglist) = cam->pix.sizeimage;
318
319         count = 0;
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) +
324                                       i + index);
325                         index++;
326                         fb_buf[j + 1] =
327                             *(((unsigned char *)cam->overlay_base) + i + index);
328                         j = j - cam->fbuf.fmt.bytesperline;
329                 }
330                 count += 2;
331                 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline) + count;
332         }
333
334         while (cam->overlay_cnt < 2) {
335                 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
336                                               camera_core_overlay_callback,
337                                               NULL);
338                 if (err)
339                         break;
340                 ++cam->overlay_cnt;
341         }
342
343         spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
344
345 }
346
347 static void camera_core_start_overlay(struct camera_device *cam)
348 {
349         int err;
350         unsigned long irqflags;
351
352         if (!cam->previewing)
353                 return;
354
355         spin_lock_irqsave(&cam->overlay_lock, irqflags);
356
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,
362                                               NULL);
363                 if (err)
364                         break;
365                 ++cam->overlay_cnt;
366         }
367
368         spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
369 }
370
371 /* ------------------ videobuf_queue_ops --------------------------------
372  * This routine is called from interrupt context when a scatter-gather DMA
373  * transfer of a videobuf_buffer completes.
374  */
375 static void camera_core_vbq_complete(void *arg1, void *arg)
376 {
377         struct camera_device *cam = (struct camera_device *)arg1;
378         struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
379
380         spin_lock(&cam->vbq_lock);
381
382         do_gettimeofday(&vb->ts);
383         vb->field_count = cam->field_count;
384         cam->field_count += 2;
385         vb->state = STATE_DONE;
386
387         wake_up(&vb->done);
388
389         spin_unlock(&cam->vbq_lock);
390 }
391
392 static void camera_core_vbq_release(struct videobuf_queue *q,
393                                     struct videobuf_buffer *vb)
394 {
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);
399
400         vb->state = STATE_NEEDS_INIT;
401 }
402
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.
406  */
407 static int camera_core_vbq_setup(struct videobuf_queue *q, unsigned int *cnt,
408                                  unsigned int *size)
409 {
410         struct camera_fh *fh = q->priv_data;
411         struct camera_device *cam = fh->cam;
412
413         if (*cnt <= 0)
414                 *cnt = VIDEO_MAX_FRAME; /* Supply a default number of buffers */
415
416         if (*cnt > VIDEO_MAX_FRAME)
417                 *cnt = VIDEO_MAX_FRAME;
418
419         spin_lock(&cam->img_lock);
420         *size = cam->pix.sizeimage;
421         spin_unlock(&cam->img_lock);
422
423         while (*size * *cnt > capture_mem)
424                 (*cnt)--;
425
426         return 0;
427 }
428
429 static int camera_core_vbq_prepare(struct videobuf_queue *q,
430                                    struct videobuf_buffer *vb,
431                                    enum v4l2_field field)
432 {
433         struct camera_fh *fh = q->priv_data;
434         struct camera_device *cam = fh->cam;
435         int err = 0;
436
437         spin_lock(&cam->img_lock);
438         if (cam->pix.sizeimage > vb->bsize) {
439                 spin_unlock(&cam->img_lock);
440                 return -EINVAL;
441         }
442         vb->size = cam->pix.sizeimage;
443         vb->width = cam->pix.width;
444         vb->height = cam->pix.height;
445         vb->field = field;
446         spin_unlock(&cam->img_lock);
447
448         if (vb->state == STATE_NEEDS_INIT)
449                 err = videobuf_iolock(q, vb, NULL);
450
451         if (!err)
452                 vb->state = STATE_PREPARED;
453         else
454                 camera_core_vbq_release(q, vb);
455
456         return err;
457 }
458
459 static void camera_core_vbq_queue(struct videobuf_queue *q,
460                                   struct videobuf_buffer *vb)
461 {
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;
466         int err;
467
468         vb->state = STATE_QUEUED;
469         err = camera_core_sgdma_queue(cam, dma->sglist, dma->sglen,
470                                       camera_core_vbq_complete, vb);
471         if (err) {
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
476                  * happen.
477                  */
478                 dev_dbg(cam->dev, "Failed to queue a video buffer for SGDMA\n");
479                 vb->state = state;
480         }
481 }
482
483 /* IOCTL interface. */
484 static int vidioc_querycap(struct file *file, void *fh,
485                            struct v4l2_capability *cap)
486 {
487         struct camera_fh *ofh = fh;
488         struct camera_device *cam = ofh->cam;
489
490         strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
491         strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
492         cap->version = OMAP1CAM_VERSION;
493         cap->capabilities =
494             V4L2_CAP_VIDEO_CAPTURE |
495             V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
496
497         return 0;
498 }
499
500 static int vidioc_enum_fmt_cap(struct file *file, void *fh,
501                                struct v4l2_fmtdesc *f)
502 {
503         struct camera_fh *ofh = fh;
504         struct camera_device *cam = ofh->cam;
505
506         return vidioc_int_enum_fmt_cap(cam->sdev, f);
507 }
508
509 static int vidioc_g_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
510 {
511         struct camera_fh *ofh = fh;
512         struct camera_device *cam = ofh->cam;
513
514         /* Get the current format */
515         memset(&f->fmt.pix, 0, sizeof(f->fmt.pix));
516         f->fmt.pix = cam->pix;
517
518         return 0;
519 }
520
521 static int vidioc_s_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
522 {
523         struct camera_fh *ofh = fh;
524         struct camera_device *cam = ofh->cam;
525         int rval = 0;
526
527         vidioc_int_try_fmt_cap(cam->sdev, f);
528
529         cam->pix = f->fmt.pix;
530
531         rval = vidioc_int_s_fmt_cap(cam->sdev, f);
532         camera_sensor_if_enable(cam);
533
534         return rval;
535 }
536
537 static int vidioc_try_fmt_cap(struct file *file, void *fh,
538                               struct v4l2_format *f)
539 {
540         struct camera_fh *ofh = fh;
541         struct camera_device *cam = ofh->cam;
542
543         return vidioc_int_try_fmt_cap(cam->sdev, f);
544 }
545
546 static int vidioc_reqbufs(struct file *file, void *fh,
547                           struct v4l2_requestbuffers *b)
548 {
549         struct camera_fh *ofh = fh;
550
551         return videobuf_reqbufs(&ofh->vbq, b);
552 }
553
554 static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
555 {
556         struct camera_fh *ofh = fh;
557
558         return videobuf_querybuf(&ofh->vbq, b);
559 }
560
561 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
562 {
563         struct camera_fh *ofh = fh;
564
565         return videobuf_qbuf(&ofh->vbq, b);
566 }
567
568 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
569 {
570         struct camera_fh *ofh = fh;
571
572         return videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
573 }
574
575 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
576 {
577         struct camera_fh *ofh = fh;
578         struct camera_device *cam = ofh->cam;
579
580         spin_lock(&cam->img_lock);
581
582         if (cam->streaming || cam->reading) {
583                 spin_unlock(&cam->img_lock);
584                 return -EBUSY;
585         } else
586                 cam->streaming = ofh;
587         /* FIXME: start camera interface */
588
589         spin_unlock(&cam->img_lock);
590
591         return videobuf_streamon(&ofh->vbq);
592 }
593
594 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
595 {
596         struct camera_fh *ofh = fh;
597         struct camera_device *cam = ofh->cam;
598         int err;
599
600         err = videobuf_streamoff(&ofh->vbq);
601         if (err < 0)
602                 return err;
603
604         spin_lock(&cam->img_lock);
605         if (cam->streaming == ofh)
606                 cam->streaming = NULL;
607         /* FIXME: stop camera interface */
608
609         spin_unlock(&cam->img_lock);
610         return 0;
611 }
612
613 static int vidioc_enum_input(struct file *file, void *fh,
614                              struct v4l2_input *inp)
615 {
616         if (inp->index > 0)
617                 return -EINVAL;
618
619         strlcpy(inp->name, "camera", sizeof(inp->name));
620         inp->type = V4L2_INPUT_TYPE_CAMERA;
621
622         return 0;
623 }
624
625 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
626 {
627         *i = 0;
628
629         return 0;
630 }
631
632 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
633 {
634         if (i > 0)
635                 return -EINVAL;
636
637         return 0;
638 }
639
640 static int vidioc_queryctrl(struct file *file, void *fh,
641                             struct v4l2_queryctrl *a)
642 {
643         struct camera_fh *ofh = fh;
644         struct camera_device *cam = ofh->cam;
645
646         return vidioc_int_queryctrl(cam->sdev, a);
647 }
648
649 static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *a)
650 {
651         struct camera_fh *ofh = fh;
652         struct camera_device *cam = ofh->cam;
653
654         return vidioc_int_g_ctrl(cam->sdev, a);
655 }
656
657 static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
658 {
659         struct camera_fh *ofh = fh;
660         struct camera_device *cam = ofh->cam;
661
662         return vidioc_int_s_ctrl(cam->sdev, a);
663 }
664
665 static int vidioc_g_fbuf(struct file *file, void *fh,
666                          struct v4l2_framebuffer *a)
667 {
668         struct camera_fh *ofh = fh;
669         struct camera_device *cam = ofh->cam;
670
671         spin_lock(&cam->img_lock);
672         *a = cam->fbuf;
673         spin_unlock(&cam->img_lock);
674
675         return 0;
676 }
677
678 static int vidioc_s_fbuf(struct file *file, void *fh,
679                          struct v4l2_framebuffer *a)
680 {
681         struct camera_fh *ofh = fh;
682         struct camera_device *cam = ofh->cam;
683
684         spin_lock(&cam->img_lock);
685         if (cam->previewing) {
686                 spin_unlock(&cam->img_lock);
687                 return -EBUSY;
688         }
689         cam->fbuf.base = a->base;
690         cam->fbuf.fmt = a->fmt;
691
692         spin_unlock(&cam->img_lock);
693         return 0;
694 }
695
696 static int vidioc_overlay(struct file *file, void *fh, unsigned int i)
697 {
698         struct camera_fh *ofh = fh;
699         struct camera_device *cam = ofh->cam;
700         int enable = i;
701
702         /* Check whether the capture format and
703          * the display format matches
704          * return failure if they are different
705          */
706         if (cam->pix.pixelformat != cam->fbuf.fmt.pixelformat)
707                 return -EINVAL;
708
709         /* If the camera image size is greater
710          * than LCD size return failure
711          */
712         if ((cam->pix.width > cam->fbuf.fmt.height) ||
713             (cam->pix.height > cam->fbuf.fmt.width))
714                 return -EINVAL;
715
716         if (!cam->previewing && enable) {
717                 cam->previewing = fh;
718                 cam->overlay_cnt = 0;
719                 camera_core_start_overlay(cam);
720         } else if (!enable)
721                 cam->previewing = NULL;
722
723         return 0;
724 }
725
726 /* File operations */
727 static unsigned int camera_core_poll(struct file *file,
728                                      struct poll_table_struct *wait)
729 {
730         return -EINVAL;
731 }
732
733 /* Callback routine for read DMA completion. We just start another DMA
734  * transfer unless overlay has been turned off
735  */
736 static void camera_core_capture_callback(void *arg1, void *arg)
737 {
738         struct camera_device *cam = (struct camera_device *)arg1;
739         int err;
740         unsigned long irqflags;
741         static int done = 0;
742
743         spin_lock_irqsave(&cam->capture_lock, irqflags);
744         if (!cam->reading) {
745                 done = 0;
746                 cam->capture_started = 0;
747                 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
748                 return;
749         }
750
751         if (done < 14) {
752                 ++done;
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,
757                                               NULL);
758         } else {
759                 cam->capture_completed = 1;
760                 if (cam->reading) {
761                         /* Wake up any process which are waiting for the
762                          * DMA to complete
763                          */
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;
768                         err =
769                            camera_core_sgdma_queue(cam, &cam->capture_sglist,
770                                                    1,
771                                                    camera_core_capture_callback,
772                                                    NULL);
773                 }
774         }
775
776         spin_unlock_irqrestore(&cam->capture_lock, irqflags);
777 }
778
779 static ssize_t camera_core_read(struct file *file, char *data, size_t count,
780                                 loff_t *ppos)
781 {
782         struct camera_fh *fh = file->private_data;
783         struct camera_device *cam = fh->cam;
784         int err;
785         unsigned long irqflags;
786         long timeout;
787 #if 0                           /* Use video_buf to do capture */
788         int i;
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);
792         return i;
793 #endif
794
795         if (!cam->capture_base) {
796                 cam->capture_base = (unsigned long)
797                     dma_alloc_coherent(NULL,
798                                        cam->pix.sizeimage,
799                                        (dma_addr_t *) &
800                                        cam->capture_base_phys,
801                                        GFP_KERNEL | GFP_DMA);
802         }
803         if (!cam->capture_base) {
804                 dev_err(cam->dev, "cannot allocate capture buffer\n");
805                 return 0;
806         }
807
808         spin_lock_irqsave(&cam->capture_lock, irqflags);
809         cam->reading = fh;
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);
814
815         err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
816                                       camera_core_capture_callback, NULL);
817
818         /* Wait till DMA is completed */
819         timeout = HZ * 10;
820         cam->capture_completed = 0;
821         while (cam->capture_completed == 0) {
822                 timeout = interruptible_sleep_on_timeout
823                     (&cam->new_video_frame, timeout);
824                 if (timeout == 0) {
825                         dev_err(cam->dev, "timeout waiting video frame\n");
826                         return -EIO;    /* Time out */
827                 }
828         }
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);
832
833 }
834
835 static int camera_core_mmap(struct file *file, struct vm_area_struct *vma)
836 {
837         struct camera_fh *fh = file->private_data;
838
839         return videobuf_mmap_mapper(&fh->vbq, vma);
840 }
841
842 static int camera_core_release(struct inode *inode, struct file *file)
843 {
844         struct camera_fh *fh = file->private_data;
845         struct camera_device *cam = fh->cam;
846
847         file->private_data = NULL;
848         kfree(fh);
849
850         spin_lock(&cam->img_lock);
851
852         if (cam->previewing == fh)
853                 cam->previewing = NULL;
854         if (cam->streaming == fh)
855                 cam->streaming = NULL;
856         if (cam->reading == fh)
857                 cam->reading = NULL;
858
859         spin_unlock(&cam->img_lock);
860
861         camera_dev->cam_hardware->finish_dma(cam->hardware_data);
862
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;
869         }
870         if (fh->vbq.read_buf) {
871                 camera_core_vbq_release(&fh->vbq, fh->vbq.read_buf);
872                 kfree(fh->vbq.read_buf);
873         }
874
875         module_put(cam->sdev->module);
876
877         cam->cam_hardware->close(cam->hardware_data);
878         cam->active = 0;
879         return 0;
880 }
881
882 static int camera_core_open(struct inode *inode, struct file *file)
883 {
884         int minor = iminor(inode);
885         struct camera_device *cam = camera_dev;
886         struct camera_fh *fh;
887         struct v4l2_format format;
888         int rval;
889
890         if (!cam || !cam->vfd || (cam->vfd->minor != minor))
891                 return -ENODEV;
892
893         /* Allocate per-filehandle data */
894         fh = kmalloc(sizeof(*fh), GFP_KERNEL);
895         if (NULL == fh)
896                 return -ENOMEM;
897         file->private_data = fh;
898         fh->cam = cam;
899         fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
900
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);
905                 rval = -EPERM;
906                 goto err;
907         }
908         cam->active = 1;
909
910         if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
911                 spin_unlock(&cam->img_lock);
912                 rval = -ENODEV;
913                 goto err;
914         }
915
916         vidioc_int_g_fmt_cap(cam->sdev, &format);
917         spin_unlock(&cam->img_lock);
918
919         videobuf_queue_sg_init(&fh->vbq, &cam->vbq_ops, NULL, &cam->vbq_lock,
920                             fh->type, V4L2_FIELD_NONE,
921                             sizeof(struct videobuf_buffer), fh);
922
923         cam->capture_completed = 0;
924         cam->capture_started = 0;
925
926         if (cam->cam_hardware->open(cam->hardware_data)) {
927                 dev_err(cam->dev, "Camera IF configuration failed\n");
928                 cam->active = 0;
929                 rval = -ENODEV;
930                 goto err;
931         }
932         rval = vidioc_s_fmt_cap(file, fh, &format);
933         if (rval) {
934                 dev_err(cam->dev, "Camera sensor configuration failed (%d)\n",
935                         rval);
936                 cam->cam_hardware->close(cam->hardware_data);
937                 cam->active = 0;
938                 rval = -ENODEV;
939                 goto err;
940         }
941
942         return 0;
943
944 err:
945         module_put(cam->sdev->module);
946         kfree(fh);
947         return rval;
948 }
949
950 #ifdef CONFIG_PM
951 static int camera_core_suspend(struct platform_device *pdev, pm_message_t state)
952 {
953         struct camera_device *cam = platform_get_drvdata(pdev);
954
955         spin_lock(&cam->img_lock);
956         if (cam->active)
957                 cam->cam_hardware->close(cam->hardware_data);
958
959         vidioc_int_s_power(cam->sdev, 0);
960         spin_unlock(&cam->img_lock);
961
962         return 0;
963 }
964
965 static int camera_core_resume(struct platform_device *pdev)
966 {
967         struct camera_device *cam = platform_get_drvdata(pdev);
968
969         spin_lock(&cam->img_lock);
970         vidioc_int_s_power(cam->sdev, 1);
971         if (cam->active) {
972                 struct v4l2_format format;
973
974                 cam->capture_completed = 1;
975                 cam->cam_hardware->open(cam->hardware_data);
976
977                 vidioc_int_g_fmt_cap(cam->sdev, &format);
978                 vidioc_int_s_fmt_cap(cam->sdev, &format);
979                 camera_sensor_if_enable(cam);
980
981                 camera_core_sgdma_process(cam);
982         }
983         spin_unlock(&cam->img_lock);
984
985         return 0;
986 }
987 #endif /* CONFIG_PM */
988
989 static struct file_operations camera_core_fops = {
990         .owner          = THIS_MODULE,
991         .llseek         = no_llseek,
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,
998 };
999 static ssize_t camera_streaming_show(struct device *dev,
1000                                      struct device_attribute *attr, char *buf)
1001 {
1002         struct camera_device *cam = dev_get_drvdata(dev);
1003
1004         return sprintf(buf, "%s\n", cam->streaming ? "active" : "inactive");
1005 }
1006
1007 static DEVICE_ATTR(streaming, S_IRUGO, camera_streaming_show, NULL);
1008
1009 static void camera_device_unregister(struct v4l2_int_device *s)
1010 {
1011         struct camera_device *cam = s->u.slave->master->priv;
1012
1013         camera_sensor_exit(cam);
1014 }
1015
1016 static int camera_device_register(struct v4l2_int_device *s)
1017 {
1018         struct camera_device *cam = s->u.slave->master->priv;
1019         struct video_device *vfd;
1020         int rval;
1021
1022         /* We already have a slave. */
1023         if (cam->sdev)
1024                 return -EBUSY;
1025
1026         cam->sdev = s;
1027
1028         if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
1029                 dev_err(cam->dev, "could not register sysfs entry\n");
1030                 rval = -EBUSY;
1031                 goto err;
1032         }
1033
1034         /* Initialize the video_device struct */
1035         vfd = cam->vfd = video_device_alloc();
1036         if (!vfd) {
1037                 dev_err(cam->dev, " could not allocate video device struct\n");
1038                 rval = -ENOMEM;
1039                 goto err;
1040         }
1041
1042         vfd->release = video_device_release;
1043
1044         strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1045         vfd->type = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CHROMAKEY;
1046
1047         /* Need to register for a VID_HARDWARE_* ID in videodev.h */
1048         vfd->fops = &camera_core_fops;
1049         video_set_drvdata(vfd, cam);
1050         vfd->minor = -1;
1051
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;
1072
1073         dev_info(cam->dev, "%s interface with %s sensor\n",
1074                  cam->cam_hardware->name, cam->sdev->name);
1075
1076         if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1077                 dev_err(cam->dev,
1078                         "could not register Video for Linux device\n");
1079                 rval = -ENODEV;
1080                 goto err;
1081         }
1082
1083         rval = camera_sensor_init(cam);
1084         if (rval)
1085                 goto err;
1086
1087         /* Disable the Camera after detection */
1088         cam->cam_hardware->disable(cam->hardware_data);
1089
1090         return 0;
1091
1092 err:
1093         camera_device_unregister(s);
1094
1095         return rval;
1096 }
1097
1098 static struct v4l2_int_master camera_master = {
1099         .attach = camera_device_register,
1100         .detach = camera_device_unregister,
1101 };
1102
1103 static struct v4l2_int_device camera = {
1104         .module = THIS_MODULE,
1105         .name   = CAM_NAME,
1106         .type   = v4l2_int_type_master,
1107         .u      = {
1108                 .master = &camera_master
1109         },
1110 };
1111
1112 static int __init camera_core_probe(struct platform_device *pdev)
1113 {
1114         struct camera_device *cam;
1115         int status = 0;
1116
1117         cam = kzalloc(sizeof(struct camera_device), GFP_KERNEL);
1118         if (!cam) {
1119                 dev_err(&pdev->dev, "could not allocate memory\n");
1120                 status = -ENOMEM;
1121                 goto err;
1122         }
1123
1124         platform_set_drvdata(pdev, cam);
1125
1126         cam->dev = &pdev->dev;
1127
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");
1133                 status = -ENODEV;
1134                 goto err;
1135         }
1136
1137         /* Save the pointer to camera device in a global variable */
1138         camera_dev = cam;
1139
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;
1145
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,
1151                                        cam->overlay_size,
1152                                        (dma_addr_t *) &
1153                                        cam->overlay_base_phys,
1154                                        GFP_KERNEL | GFP_DMA);
1155                 if (!cam->overlay_base) {
1156                         dev_err(cam->dev,
1157                                 "cannot allocate overlay framebuffer\n");
1158                         status = -ENOMEM;
1159                         goto err;
1160                 }
1161         }
1162         memset((void *)cam->overlay_base, 0, cam->overlay_size);
1163         spin_lock_init(&cam->overlay_lock);
1164         spin_lock_init(&cam->capture_lock);
1165
1166         /* Initialize the spinlock used to serialize access to the image
1167          * parameters
1168          */
1169         spin_lock_init(&cam->img_lock);
1170
1171         /* Initialize the wait queue */
1172         init_waitqueue_head(&cam->new_video_frame);
1173
1174         /* Initialize the DMA structures */
1175         camera_core_sgdma_init(cam);
1176
1177         platform_set_drvdata(pdev, cam);
1178
1179         camera.priv = cam;
1180
1181         if (v4l2_int_device_register(&camera))
1182                 goto err;
1183
1184         return 0;
1185
1186 err:
1187         vidioc_int_dev_exit(cam->sdev);
1188         cam->overlay_base = 0;
1189         return status;
1190 }
1191
1192 static int camera_core_remove(struct platform_device *pdev)
1193 {
1194         struct camera_device *cam = platform_get_drvdata(pdev);
1195         struct video_device *vfd;
1196
1197         vfd = cam->vfd;
1198         if (vfd) {
1199                 if (vfd->minor == -1) {
1200                         /* The device never got registered, so release the
1201                          * video_device struct directly
1202                          */
1203                         video_device_release(vfd);
1204                 } else {
1205                         /* The unregister function will release the
1206                          * video_device struct as well as unregistering it.
1207                          */
1208                         video_unregister_device(vfd);
1209                 }
1210                 cam->vfd = NULL;
1211         }
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;
1217         }
1218         cam->overlay_base_phys = 0;
1219
1220         vidioc_int_dev_exit(cam->sdev);
1221         cam->cam_hardware->cleanup(cam->hardware_data);
1222         kfree(cam);
1223         camera_dev = NULL;
1224
1225         return 0;
1226 }
1227
1228 static struct platform_driver camera_core_driver = {
1229         .driver = {
1230                 .name   = CAM_NAME,
1231                 .owner  = THIS_MODULE,
1232         },
1233         .probe          = camera_core_probe,
1234         .remove         = camera_core_remove,
1235 #ifdef CONFIG_PM
1236         .suspend        = camera_core_suspend,
1237         .resume         = camera_core_resume,
1238 #endif
1239 };
1240
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.
1245  */
1246 static struct platform_device *cam;
1247
1248 static void __exit camera_core_cleanup(void)
1249 {
1250         platform_driver_unregister(&camera_core_driver);
1251         platform_device_unregister(cam);
1252 }
1253
1254 static char banner[] __initdata = KERN_INFO "OMAP Camera driver initializing\n";
1255
1256 static int __init camera_core_init(void)
1257 {
1258
1259         printk(banner);
1260         platform_driver_register(&camera_core_driver);
1261
1262         cam = platform_device_register_simple(CAM_NAME, -1, NULL, 0);
1263
1264         return 0;
1265 }
1266
1267 MODULE_AUTHOR("Texas Instruments.");
1268 MODULE_DESCRIPTION("OMAP Video for Linux camera driver");
1269 MODULE_LICENSE("GPL");
1270
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)");
1278
1279 module_init(camera_core_init);
1280 module_exit(camera_core_cleanup);