Merge current mainline tree into linux-omap tree
[pandora-kernel.git] / drivers / media / video / omap24xxcam.c
1 /*
2  * drivers/media/video/omap24xxcam.c
3  *
4  * OMAP 2 camera block driver.
5  *
6  * Copyright (C) 2004 MontaVista Software, Inc.
7  * Copyright (C) 2004 Texas Instruments.
8  * Copyright (C) 2007 Nokia Corporation.
9  *
10  * Contact: Sakari Ailus <sakari.ailus@nokia.com>
11  *
12  * Based on code from Andy Lowe <source@mvista.com>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * version 2 as published by the Free Software Foundation.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26  * 02110-1301 USA
27  */
28
29 #include <linux/delay.h>
30 #include <linux/kernel.h>
31 #include <linux/interrupt.h>
32 #include <linux/videodev2.h>
33 #include <linux/pci.h>          /* needed for videobufs */
34 #include <linux/version.h>
35 #include <linux/platform_device.h>
36 #include <linux/clk.h>
37 #include <linux/io.h>
38
39 #include <media/v4l2-common.h>
40
41 #include "omap24xxcam.h"
42
43 #define OMAP24XXCAM_VERSION KERNEL_VERSION(0, 0, 0)
44
45 #define RESET_TIMEOUT_NS 10000
46
47 static void omap24xxcam_reset(struct omap24xxcam_device *cam);
48 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam);
49 static void omap24xxcam_device_unregister(struct v4l2_int_device *ctl);
50 static int omap24xxcam_remove(struct platform_device *pdev);
51
52 /* module parameters */
53 static int video_nr = -1;       /* video device minor (-1 ==> auto assign) */
54 /*
55  * Maximum amount of memory to use for capture buffers.
56  * Default is 4800KB, enough to double-buffer SXGA.
57  */
58 static int capture_mem = 1280 * 960 * 2 * 2;
59
60 static struct v4l2_int_device omap24xxcam;
61
62 /*
63  *
64  * Clocks.
65  *
66  */
67
68 static void omap24xxcam_clock_put(struct omap24xxcam_device *cam)
69 {
70         if (cam->ick != NULL && !IS_ERR(cam->ick))
71                 clk_put(cam->ick);
72         if (cam->fck != NULL && !IS_ERR(cam->fck))
73                 clk_put(cam->fck);
74
75         cam->ick = cam->fck = NULL;
76 }
77
78 static int omap24xxcam_clock_get(struct omap24xxcam_device *cam)
79 {
80         int rval = 0;
81
82         cam->fck = clk_get(cam->dev, "cam_fck");
83         if (IS_ERR(cam->fck)) {
84                 dev_err(cam->dev, "can't get cam_fck");
85                 rval = PTR_ERR(cam->fck);
86                 omap24xxcam_clock_put(cam);
87                 return rval;
88         }
89
90         cam->ick = clk_get(cam->dev, "cam_ick");
91         if (IS_ERR(cam->ick)) {
92                 dev_err(cam->dev, "can't get cam_ick");
93                 rval = PTR_ERR(cam->ick);
94                 omap24xxcam_clock_put(cam);
95         }
96
97         return rval;
98 }
99
100 static void omap24xxcam_clock_on(struct omap24xxcam_device *cam)
101 {
102         clk_enable(cam->fck);
103         clk_enable(cam->ick);
104 }
105
106 static void omap24xxcam_clock_off(struct omap24xxcam_device *cam)
107 {
108         clk_disable(cam->fck);
109         clk_disable(cam->ick);
110 }
111
112 /*
113  *
114  * Camera core
115  *
116  */
117
118 /*
119  * Set xclk.
120  *
121  * To disable xclk, use value zero.
122  */
123 static void omap24xxcam_core_xclk_set(const struct omap24xxcam_device *cam,
124                                       u32 xclk)
125 {
126         if (xclk) {
127                 u32 divisor = CAM_MCLK / xclk;
128
129                 if (divisor == 1)
130                         omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
131                                             CC_CTRL_XCLK,
132                                             CC_CTRL_XCLK_DIV_BYPASS);
133                 else
134                         omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
135                                             CC_CTRL_XCLK, divisor);
136         } else
137                 omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
138                                     CC_CTRL_XCLK, CC_CTRL_XCLK_DIV_STABLE_LOW);
139 }
140
141 static void omap24xxcam_core_hwinit(const struct omap24xxcam_device *cam)
142 {
143         /*
144          * Setting the camera core AUTOIDLE bit causes problems with frame
145          * synchronization, so we will clear the AUTOIDLE bit instead.
146          */
147         omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_SYSCONFIG,
148                             CC_SYSCONFIG_AUTOIDLE);
149
150         /* program the camera interface DMA packet size */
151         omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL_DMA,
152                             CC_CTRL_DMA_EN | (DMA_THRESHOLD / 4 - 1));
153
154         /* enable camera core error interrupts */
155         omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQENABLE,
156                             CC_IRQENABLE_FW_ERR_IRQ
157                             | CC_IRQENABLE_FSC_ERR_IRQ
158                             | CC_IRQENABLE_SSC_ERR_IRQ
159                             | CC_IRQENABLE_FIFO_OF_IRQ);
160 }
161
162 /*
163  * Enable the camera core.
164  *
165  * Data transfer to the camera DMA starts from next starting frame.
166  */
167 static void omap24xxcam_core_enable(const struct omap24xxcam_device *cam)
168 {
169
170         omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
171                             cam->cc_ctrl);
172 }
173
174 /*
175  * Disable camera core.
176  *
177  * The data transfer will be stopped immediately (CC_CTRL_CC_RST). The
178  * core internal state machines will be reset. Use
179  * CC_CTRL_CC_FRAME_TRIG instead if you want to transfer the current
180  * frame completely.
181  */
182 static void omap24xxcam_core_disable(const struct omap24xxcam_device *cam)
183 {
184         omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
185                             CC_CTRL_CC_RST);
186 }
187
188 /* Interrupt service routine for camera core interrupts. */
189 static void omap24xxcam_core_isr(struct omap24xxcam_device *cam)
190 {
191         u32 cc_irqstatus;
192         const u32 cc_irqstatus_err =
193                 CC_IRQSTATUS_FW_ERR_IRQ
194                 | CC_IRQSTATUS_FSC_ERR_IRQ
195                 | CC_IRQSTATUS_SSC_ERR_IRQ
196                 | CC_IRQSTATUS_FIFO_UF_IRQ
197                 | CC_IRQSTATUS_FIFO_OF_IRQ;
198
199         cc_irqstatus = omap24xxcam_reg_in(cam->mmio_base + CC_REG_OFFSET,
200                                           CC_IRQSTATUS);
201         omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQSTATUS,
202                             cc_irqstatus);
203
204         if (cc_irqstatus & cc_irqstatus_err
205             && !atomic_read(&cam->in_reset)) {
206                 dev_dbg(cam->dev, "resetting camera, cc_irqstatus 0x%x\n",
207                         cc_irqstatus);
208                 omap24xxcam_reset(cam);
209         }
210 }
211
212 /*
213  *
214  * videobuf_buffer handling.
215  *
216  * Memory for mmapped videobuf_buffers is not allocated
217  * conventionally, but by several kmalloc allocations and then
218  * creating the scatterlist on our own. User-space buffers are handled
219  * normally.
220  *
221  */
222
223 /*
224  * Free the memory-mapped buffer memory allocated for a
225  * videobuf_buffer and the associated scatterlist.
226  */
227 static void omap24xxcam_vbq_free_mmap_buffer(struct videobuf_buffer *vb)
228 {
229         int i;
230         size_t alloc_size;
231         struct page *page;
232
233         if (vb->dma.sglist == NULL)
234                 return;
235
236         i = vb->dma.sglen;
237         while (i) {
238                 i--;
239                 alloc_size = vb->dma.sglist[i].length;
240                 page = vb->dma.sglist[i].page;
241                 do {
242                         ClearPageReserved(page++);
243                 } while (alloc_size -= PAGE_SIZE);
244                 __free_pages(vb->dma.sglist[i].page,
245                              get_order(vb->dma.sglist[i].length));
246         }
247
248         kfree(vb->dma.sglist);
249         vb->dma.sglist = NULL;
250 }
251
252 /* Release all memory related to the videobuf_queue. */
253 static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue *vbq)
254 {
255         int i;
256
257         mutex_lock(&vbq->lock);
258
259         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
260                 if (NULL == vbq->bufs[i])
261                         continue;
262                 if (V4L2_MEMORY_MMAP != vbq->bufs[i]->memory)
263                         continue;
264                 vbq->ops->buf_release(vbq, vbq->bufs[i]);
265                 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
266                 kfree(vbq->bufs[i]);
267                 vbq->bufs[i] = NULL;
268         }
269
270         mutex_unlock(&vbq->lock);
271
272         videobuf_mmap_free(vbq);
273 }
274
275 /*
276  * Allocate physically as contiguous as possible buffer for video
277  * frame and allocate and build DMA scatter-gather list for it.
278  */
279 static int omap24xxcam_vbq_alloc_mmap_buffer(struct videobuf_buffer *vb)
280 {
281         unsigned int order;
282         size_t alloc_size, size = vb->bsize; /* vb->bsize is page aligned */
283         struct page *page;
284         int max_pages, err = 0, i = 0;
285
286         /*
287          * allocate maximum size scatter-gather list. Note this is
288          * overhead. We may not use as many entries as we allocate
289          */
290         max_pages = vb->bsize >> PAGE_SHIFT;
291         vb->dma.sglist =
292                 kcalloc(max_pages, sizeof(*vb->dma.sglist), GFP_KERNEL);
293         if (vb->dma.sglist == NULL) {
294                 err = -ENOMEM;
295                 goto out;
296         }
297
298         while (size) {
299                 order = get_order(size);
300                 /*
301                  * do not over-allocate even if we would get larger
302                  * contiguous chunk that way
303                  */
304                 if ((PAGE_SIZE << order) > size)
305                         order--;
306
307                 /* try to allocate as many contiguous pages as possible */
308                 page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
309                 /* if allocation fails, try to allocate smaller amount */
310                 while (page == NULL) {
311                         order--;
312                         page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
313                         if (page == NULL && !order) {
314                                 err = -ENOMEM;
315                                 goto out;
316                         }
317                 }
318                 size -= (PAGE_SIZE << order);
319
320                 /* append allocated chunk of pages into scatter-gather list */
321                 vb->dma.sglist[i].page = page;
322                 vb->dma.sglist[i].length = (PAGE_SIZE << order);
323                 vb->dma.sglen++;
324                 i++;
325
326                 alloc_size = (PAGE_SIZE << order);
327
328                 /* clear pages before giving them to user space */
329                 memset(page_address(page), 0, alloc_size);
330
331                 /* mark allocated pages reserved */
332                 do {
333                         SetPageReserved(page++);
334                 } while (alloc_size -= PAGE_SIZE);
335         }
336         /*
337          * REVISIT: not fully correct to assign nr_pages == sglen but
338          * video-buf is passing nr_pages for e.g. unmap_sg calls
339          */
340         vb->dma.nr_pages = vb->dma.sglen;
341         vb->dma.direction = PCI_DMA_FROMDEVICE;
342
343         return 0;
344
345 out:
346         omap24xxcam_vbq_free_mmap_buffer(vb);
347         return err;
348 }
349
350 static int omap24xxcam_vbq_alloc_mmap_buffers(struct videobuf_queue *vbq,
351                                               unsigned int count)
352 {
353         int i, err = 0;
354         struct omap24xxcam_fh *fh =
355                 container_of(vbq, struct omap24xxcam_fh, vbq);
356
357         mutex_lock(&vbq->lock);
358
359         for (i = 0; i < count; i++) {
360                 err = omap24xxcam_vbq_alloc_mmap_buffer(vbq->bufs[i]);
361                 if (err)
362                         goto out;
363                 dev_dbg(fh->cam->dev, "sglen is %d for buffer %d\n",
364                         vbq->bufs[i]->dma.sglen, i);
365         }
366
367         mutex_unlock(&vbq->lock);
368
369         return 0;
370 out:
371         while (i) {
372                 i--;
373                 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
374         }
375
376         mutex_unlock(&vbq->lock);
377
378         return err;
379 }
380
381 /*
382  * This routine is called from interrupt context when a scatter-gather DMA
383  * transfer of a videobuf_buffer completes.
384  */
385 static void omap24xxcam_vbq_complete(struct omap24xxcam_sgdma *sgdma,
386                                      u32 csr, void *arg)
387 {
388         struct omap24xxcam_device *cam =
389                 container_of(sgdma, struct omap24xxcam_device, sgdma);
390         struct omap24xxcam_fh *fh = cam->streaming->private_data;
391         struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
392         const u32 csr_error = CAMDMA_CSR_MISALIGNED_ERR
393                 | CAMDMA_CSR_SUPERVISOR_ERR | CAMDMA_CSR_SECURE_ERR
394                 | CAMDMA_CSR_TRANS_ERR | CAMDMA_CSR_DROP;
395         unsigned long flags;
396
397         spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
398         if (--cam->sgdma_in_queue == 0)
399                 omap24xxcam_core_disable(cam);
400         spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
401
402         do_gettimeofday(&vb->ts);
403         vb->field_count = atomic_add_return(2, &fh->field_count);
404         if (csr & csr_error) {
405                 vb->state = STATE_ERROR;
406                 if (!atomic_read(&fh->cam->in_reset)) {
407                         dev_dbg(cam->dev, "resetting camera, csr 0x%x\n", csr);
408                         omap24xxcam_reset(cam);
409                 }
410         } else
411                 vb->state = STATE_DONE;
412         wake_up(&vb->done);
413 }
414
415 static void omap24xxcam_vbq_release(struct videobuf_queue *vbq,
416                                     struct videobuf_buffer *vb)
417 {
418         struct videobuf_dmabuf *dma = &vb->dma;
419
420         /* wait for buffer, especially to get out of the sgdma queue */
421         videobuf_waiton(vb, 0, 0);
422         if (vb->memory == V4L2_MEMORY_MMAP) {
423                 dma_unmap_sg(vbq->dev, dma->sglist, dma->sglen,
424                              dma->direction);
425                 dma->direction = DMA_NONE;
426         } else {
427                 videobuf_dma_unmap(vbq, &vb->dma);
428                 videobuf_dma_free(&vb->dma);
429         }
430
431         vb->state = STATE_NEEDS_INIT;
432 }
433
434 /*
435  * Limit the number of available kernel image capture buffers based on the
436  * number requested, the currently selected image size, and the maximum
437  * amount of memory permitted for kernel capture buffers.
438  */
439 static int omap24xxcam_vbq_setup(struct videobuf_queue *vbq, unsigned int *cnt,
440                                  unsigned int *size)
441 {
442         struct omap24xxcam_fh *fh = vbq->priv_data;
443
444         if (*cnt <= 0)
445                 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
446
447         if (*cnt > VIDEO_MAX_FRAME)
448                 *cnt = VIDEO_MAX_FRAME;
449
450         *size = fh->pix.sizeimage;
451
452         /* accessing fh->cam->capture_mem is ok, it's constant */
453         while (*size * *cnt > fh->cam->capture_mem)
454                 (*cnt)--;
455
456         return 0;
457 }
458
459 static int omap24xxcam_dma_iolock(struct videobuf_queue *vbq,
460                                   struct videobuf_dmabuf *dma)
461 {
462         int err = 0;
463
464         dma->direction = PCI_DMA_FROMDEVICE;
465         if (!dma_map_sg(vbq->dev, dma->sglist, dma->sglen, dma->direction)) {
466                 kfree(dma->sglist);
467                 dma->sglist = NULL;
468                 dma->sglen = 0;
469                 err = -EIO;
470         }
471
472         return err;
473 }
474
475 static int omap24xxcam_vbq_prepare(struct videobuf_queue *vbq,
476                                    struct videobuf_buffer *vb,
477                                    enum v4l2_field field)
478 {
479         struct omap24xxcam_fh *fh = vbq->priv_data;
480         int err = 0;
481
482         /*
483          * Accessing pix here is okay since it's constant while
484          * streaming is on (and we only get called then).
485          */
486         if (vb->baddr) {
487                 /* This is a userspace buffer. */
488                 if (fh->pix.sizeimage > vb->bsize) {
489                         /* The buffer isn't big enough. */
490                         err = -EINVAL;
491                 } else
492                         vb->size = fh->pix.sizeimage;
493         } else {
494                 if (vb->state != STATE_NEEDS_INIT) {
495                         /*
496                          * We have a kernel bounce buffer that has
497                          * already been allocated.
498                          */
499                         if (fh->pix.sizeimage > vb->size) {
500                                 /*
501                                  * The image size has been changed to
502                                  * a larger size since this buffer was
503                                  * allocated, so we need to free and
504                                  * reallocate it.
505                                  */
506                                 omap24xxcam_vbq_release(vbq, vb);
507                                 vb->size = fh->pix.sizeimage;
508                         }
509                 } else {
510                         /* We need to allocate a new kernel bounce buffer. */
511                         vb->size = fh->pix.sizeimage;
512                 }
513         }
514
515         if (err)
516                 return err;
517
518         vb->width = fh->pix.width;
519         vb->height = fh->pix.height;
520         vb->field = field;
521
522         if (vb->state == STATE_NEEDS_INIT) {
523                 if (vb->memory == V4L2_MEMORY_MMAP)
524                         /*
525                          * we have built the scatter-gather list by ourself so
526                          * do the scatter-gather mapping as well
527                          */
528                         err = omap24xxcam_dma_iolock(vbq, &vb->dma);
529                 else
530                         err = videobuf_iolock(vbq, vb, NULL);
531         }
532
533         if (!err)
534                 vb->state = STATE_PREPARED;
535         else
536                 omap24xxcam_vbq_release(vbq, vb);
537
538         return err;
539 }
540
541 static void omap24xxcam_vbq_queue(struct videobuf_queue *vbq,
542                                   struct videobuf_buffer *vb)
543 {
544         struct omap24xxcam_fh *fh = vbq->priv_data;
545         struct omap24xxcam_device *cam = fh->cam;
546         enum videobuf_state state = vb->state;
547         unsigned long flags;
548         int err;
549
550         /*
551          * FIXME: We're marking the buffer active since we have no
552          * pretty way of marking it active exactly when the
553          * scatter-gather transfer starts.
554          */
555         vb->state = STATE_ACTIVE;
556
557         err = omap24xxcam_sgdma_queue(&fh->cam->sgdma, vb->dma.sglist,
558                                       vb->dma.sglen, vb->size,
559                                       omap24xxcam_vbq_complete, vb);
560
561         if (!err) {
562                 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
563                 if (++cam->sgdma_in_queue == 1
564                     && !atomic_read(&cam->in_reset))
565                         omap24xxcam_core_enable(cam);
566                 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
567         } else {
568                 /*
569                  * Oops. We're not supposed to get any errors here.
570                  * The only way we could get an error is if we ran out
571                  * of scatter-gather DMA slots, but we are supposed to
572                  * have at least as many scatter-gather DMA slots as
573                  * video buffers so that can't happen.
574                  */
575                 dev_err(cam->dev, "failed to queue a video buffer for dma!\n");
576                 dev_err(cam->dev, "likely a bug in the driver!\n");
577                 vb->state = state;
578         }
579 }
580
581 static struct videobuf_queue_ops omap24xxcam_vbq_ops = {
582         .buf_setup   = omap24xxcam_vbq_setup,
583         .buf_prepare = omap24xxcam_vbq_prepare,
584         .buf_queue   = omap24xxcam_vbq_queue,
585         .buf_release = omap24xxcam_vbq_release,
586 };
587
588 /*
589  *
590  * OMAP main camera system
591  *
592  */
593
594 /*
595  * Reset camera block to power-on state.
596  */
597 static void omap24xxcam_poweron_reset(const struct omap24xxcam_device *cam)
598 {
599         int max_loop = RESET_TIMEOUT_NS;
600
601         /* Reset whole camera subsystem */
602         omap24xxcam_reg_out(cam->mmio_base,
603                             CAM_SYSCONFIG,
604                             CAM_SYSCONFIG_SOFTRESET);
605
606         /* Wait till it's finished */
607         while (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
608                  & CAM_SYSSTATUS_RESETDONE)
609                && --max_loop) {
610                 ndelay(1);
611         }
612
613         if (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
614               & CAM_SYSSTATUS_RESETDONE))
615                 dev_err(cam->dev, "camera soft reset timeout\n");
616 }
617
618 /*
619  * (Re)initialise the camera block.
620  */
621 static void omap24xxcam_hwinit(const struct omap24xxcam_device *cam)
622 {
623         omap24xxcam_poweron_reset(cam);
624
625         /* set the camera subsystem autoidle bit */
626         omap24xxcam_reg_out(cam->mmio_base, CAM_SYSCONFIG,
627                             CAM_SYSCONFIG_AUTOIDLE);
628
629         /* set the camera MMU autoidle bit */
630         omap24xxcam_reg_out(cam->mmio_base,
631                             CAMMMU_REG_OFFSET + CAMMMU_SYSCONFIG,
632                             CAMMMU_SYSCONFIG_AUTOIDLE);
633
634         omap24xxcam_core_hwinit(cam);
635
636         omap24xxcam_dma_hwinit(&cam->sgdma.dma);
637 }
638
639 /*
640  * Callback for dma transfer stalling.
641  */
642 static void omap24xxcam_stalled_dma_reset(unsigned long data)
643 {
644         struct omap24xxcam_device *cam = (struct omap24xxcam_device *)data;
645
646         if (!atomic_read(&cam->in_reset)) {
647                 dev_dbg(cam->dev, "dma stalled, resetting camera\n");
648                 omap24xxcam_reset(cam);
649         }
650 }
651
652 /*
653  * Stop capture. Mark we're doing a reset, stop DMA transfers and
654  * core. (No new scatter-gather transfers will be queued whilst
655  * in_reset is non-zero.)
656  *
657  * If omap24xxcam_capture_stop is called from several places at
658  * once, only the first call will have an effect. Similarly, the last
659  * call omap24xxcam_streaming_cont will have effect.
660  *
661  * Serialisation is ensured by using cam->core_enable_disable_lock.
662  */
663 static void omap24xxcam_capture_stop(struct omap24xxcam_device *cam)
664 {
665         unsigned long flags;
666
667         spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
668
669         if (atomic_inc_return(&cam->in_reset) != 1) {
670                 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
671                 return;
672         }
673
674         omap24xxcam_core_disable(cam);
675
676         spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
677
678         omap24xxcam_sgdma_sync(&cam->sgdma);
679 }
680
681 /*
682  * Reset and continue streaming.
683  *
684  * Note: Resetting the camera FIFO via the CC_RST bit in the CC_CTRL
685  * register is supposed to be sufficient to recover from a camera
686  * interface error, but it doesn't seem to be enough. If we only do
687  * that then subsequent image captures are out of sync by either one
688  * or two times DMA_THRESHOLD bytes. Resetting and re-initializing the
689  * entire camera subsystem prevents the problem with frame
690  * synchronization.
691  */
692 static void omap24xxcam_capture_cont(struct omap24xxcam_device *cam)
693 {
694         unsigned long flags;
695
696         spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
697
698         if (atomic_read(&cam->in_reset) != 1)
699                 goto out;
700
701         omap24xxcam_hwinit(cam);
702
703         omap24xxcam_sensor_if_enable(cam);
704
705         omap24xxcam_sgdma_process(&cam->sgdma);
706
707         if (cam->sgdma_in_queue)
708                 omap24xxcam_core_enable(cam);
709
710 out:
711         atomic_dec(&cam->in_reset);
712         spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
713 }
714
715 static ssize_t
716 omap24xxcam_streaming_show(struct device *dev, struct device_attribute *attr,
717                 char *buf)
718 {
719         struct omap24xxcam_device *cam = dev_get_drvdata(dev);
720
721         return sprintf(buf, "%s\n", cam->streaming ?  "active" : "inactive");
722 }
723 static DEVICE_ATTR(streaming, S_IRUGO, omap24xxcam_streaming_show, NULL);
724
725 /*
726  * Stop capture and restart it. I.e. reset the camera during use.
727  */
728 static void omap24xxcam_reset(struct omap24xxcam_device *cam)
729 {
730         omap24xxcam_capture_stop(cam);
731         omap24xxcam_capture_cont(cam);
732 }
733
734 /*
735  * The main interrupt handler.
736  */
737 static irqreturn_t omap24xxcam_isr(int irq, void *arg)
738 {
739         struct omap24xxcam_device *cam = (struct omap24xxcam_device *)arg;
740         u32 irqstatus;
741         unsigned int irqhandled = 0;
742
743         irqstatus = omap24xxcam_reg_in(cam->mmio_base, CAM_IRQSTATUS);
744
745         if (irqstatus &
746             (CAM_IRQSTATUS_DMA_IRQ2 | CAM_IRQSTATUS_DMA_IRQ1
747              | CAM_IRQSTATUS_DMA_IRQ0)) {
748                 omap24xxcam_dma_isr(&cam->sgdma.dma);
749                 irqhandled = 1;
750         }
751         if (irqstatus & CAM_IRQSTATUS_CC_IRQ) {
752                 omap24xxcam_core_isr(cam);
753                 irqhandled = 1;
754         }
755         if (irqstatus & CAM_IRQSTATUS_MMU_IRQ)
756                 dev_err(cam->dev, "unhandled camera MMU interrupt!\n");
757
758         return IRQ_RETVAL(irqhandled);
759 }
760
761 /*
762  *
763  * Sensor handling.
764  *
765  */
766
767 /*
768  * Enable the external sensor interface. Try to negotiate interface
769  * parameters with the sensor and start using the new ones. The calls
770  * to sensor_if_enable and sensor_if_disable need not to be balanced.
771  */
772 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam)
773 {
774         int rval;
775         struct v4l2_ifparm p;
776
777         rval = vidioc_int_g_ifparm(cam->sdev, &p);
778         if (rval) {
779                 dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
780                 return rval;
781         }
782
783         cam->if_type = p.if_type;
784
785         cam->cc_ctrl = CC_CTRL_CC_EN;
786
787         switch (p.if_type) {
788         case V4L2_IF_TYPE_BT656:
789                 if (p.u.bt656.frame_start_on_rising_vs)
790                         cam->cc_ctrl |= CC_CTRL_NOBT_SYNCHRO;
791                 if (p.u.bt656.bt_sync_correct)
792                         cam->cc_ctrl |= CC_CTRL_BT_CORRECT;
793                 if (p.u.bt656.swap)
794                         cam->cc_ctrl |= CC_CTRL_PAR_ORDERCAM;
795                 if (p.u.bt656.latch_clk_inv)
796                         cam->cc_ctrl |= CC_CTRL_PAR_CLK_POL;
797                 if (p.u.bt656.nobt_hs_inv)
798                         cam->cc_ctrl |= CC_CTRL_NOBT_HS_POL;
799                 if (p.u.bt656.nobt_vs_inv)
800                         cam->cc_ctrl |= CC_CTRL_NOBT_VS_POL;
801
802                 switch (p.u.bt656.mode) {
803                 case V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT:
804                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT8;
805                         break;
806                 case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT:
807                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT10;
808                         break;
809                 case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT:
810                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT12;
811                         break;
812                 case V4L2_IF_TYPE_BT656_MODE_BT_8BIT:
813                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT8;
814                         break;
815                 case V4L2_IF_TYPE_BT656_MODE_BT_10BIT:
816                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT10;
817                         break;
818                 default:
819                         dev_err(cam->dev,
820                                 "bt656 interface mode %d not supported\n",
821                                 p.u.bt656.mode);
822                         return -EINVAL;
823                 }
824                 /*
825                  * The clock rate that the sensor wants has changed.
826                  * We have to adjust the xclk from OMAP 2 side to
827                  * match the sensor's wish as closely as possible.
828                  */
829                 if (p.u.bt656.clock_curr != cam->if_u.bt656.xclk) {
830                         u32 xclk = p.u.bt656.clock_curr;
831                         u32 divisor;
832
833                         if (xclk == 0)
834                                 return -EINVAL;
835
836                         if (xclk > CAM_MCLK)
837                                 xclk = CAM_MCLK;
838
839                         divisor = CAM_MCLK / xclk;
840                         if (divisor * xclk < CAM_MCLK)
841                                 divisor++;
842                         if (CAM_MCLK / divisor < p.u.bt656.clock_min
843                             && divisor > 1)
844                                 divisor--;
845                         if (divisor > 30)
846                                 divisor = 30;
847
848                         xclk = CAM_MCLK / divisor;
849
850                         if (xclk < p.u.bt656.clock_min
851                             || xclk > p.u.bt656.clock_max)
852                                 return -EINVAL;
853
854                         cam->if_u.bt656.xclk = xclk;
855                 }
856                 omap24xxcam_core_xclk_set(cam, cam->if_u.bt656.xclk);
857                 break;
858         default:
859                 /* FIXME: how about other interfaces? */
860                 dev_err(cam->dev, "interface type %d not supported\n",
861                         p.if_type);
862                 return -EINVAL;
863         }
864
865         return 0;
866 }
867
868 static void omap24xxcam_sensor_if_disable(const struct omap24xxcam_device *cam)
869 {
870         switch (cam->if_type) {
871         case V4L2_IF_TYPE_BT656:
872                 omap24xxcam_core_xclk_set(cam, 0);
873                 break;
874         }
875 }
876
877 /*
878  * Initialise the sensor hardware.
879  */
880 static int omap24xxcam_sensor_init(struct omap24xxcam_device *cam)
881 {
882         int err = 0;
883         struct v4l2_int_device *sdev = cam->sdev;
884
885         omap24xxcam_clock_on(cam);
886         err = omap24xxcam_sensor_if_enable(cam);
887         if (err) {
888                 dev_err(cam->dev, "sensor interface could not be enabled at "
889                         "initialisation, %d\n", err);
890                 cam->sdev = NULL;
891                 goto out;
892         }
893
894         /* power up sensor during sensor initialization */
895         vidioc_int_s_power(sdev, 1);
896
897         err = vidioc_int_dev_init(sdev);
898         if (err) {
899                 dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
900                 /* Sensor init failed --- it's nonexistent to us! */
901                 cam->sdev = NULL;
902                 goto out;
903         }
904
905         dev_info(cam->dev, "sensor is %s\n", sdev->name);
906
907 out:
908         omap24xxcam_sensor_if_disable(cam);
909         omap24xxcam_clock_off(cam);
910
911         vidioc_int_s_power(sdev, 0);
912
913         return err;
914 }
915
916 static void omap24xxcam_sensor_exit(struct omap24xxcam_device *cam)
917 {
918         if (cam->sdev)
919                 vidioc_int_dev_exit(cam->sdev);
920 }
921
922 static void omap24xxcam_sensor_disable(struct omap24xxcam_device *cam)
923 {
924         omap24xxcam_sensor_if_disable(cam);
925         omap24xxcam_clock_off(cam);
926         vidioc_int_s_power(cam->sdev, 0);
927 }
928
929 /*
930  * Power-up and configure camera sensor. It's ready for capturing now.
931  */
932 static int omap24xxcam_sensor_enable(struct omap24xxcam_device *cam)
933 {
934         int rval;
935
936         omap24xxcam_clock_on(cam);
937
938         omap24xxcam_sensor_if_enable(cam);
939
940         rval = vidioc_int_s_power(cam->sdev, 1);
941         if (rval)
942                 goto out;
943
944         rval = vidioc_int_init(cam->sdev);
945         if (rval)
946                 goto out;
947
948         return 0;
949
950 out:
951         omap24xxcam_sensor_disable(cam);
952
953         return rval;
954 }
955
956 static void omap24xxcam_sensor_reset_work(struct work_struct *work)
957 {
958         struct omap24xxcam_device *cam =
959                 container_of(work, struct omap24xxcam_device,
960                              sensor_reset_work);
961
962         if (atomic_read(&cam->reset_disable))
963                 return;
964
965         omap24xxcam_capture_stop(cam);
966
967         if (vidioc_int_reset(cam->sdev) == 0) {
968                 vidioc_int_init(cam->sdev);
969         } else {
970                 /* Can't reset it by vidioc_int_reset. */
971                 omap24xxcam_sensor_disable(cam);
972                 omap24xxcam_sensor_enable(cam);
973         }
974
975         omap24xxcam_capture_cont(cam);
976 }
977
978 /*
979  *
980  * IOCTL interface.
981  *
982  */
983
984 static int vidioc_querycap(struct file *file, void *fh,
985                            struct v4l2_capability *cap)
986 {
987         struct omap24xxcam_fh *ofh = fh;
988         struct omap24xxcam_device *cam = ofh->cam;
989
990         strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
991         strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
992         cap->version = OMAP24XXCAM_VERSION;
993         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
994
995         return 0;
996 }
997
998 static int vidioc_enum_fmt_cap(struct file *file, void *fh,
999                                struct v4l2_fmtdesc *f)
1000 {
1001         struct omap24xxcam_fh *ofh = fh;
1002         struct omap24xxcam_device *cam = ofh->cam;
1003         int rval;
1004
1005         rval = vidioc_int_enum_fmt_cap(cam->sdev, f);
1006
1007         return rval;
1008 }
1009
1010 static int vidioc_g_fmt_cap(struct file *file, void *fh,
1011                             struct v4l2_format *f)
1012 {
1013         struct omap24xxcam_fh *ofh = fh;
1014         struct omap24xxcam_device *cam = ofh->cam;
1015         int rval;
1016
1017         mutex_lock(&cam->mutex);
1018         rval = vidioc_int_g_fmt_cap(cam->sdev, f);
1019         mutex_unlock(&cam->mutex);
1020
1021         return rval;
1022 }
1023
1024 static int vidioc_s_fmt_cap(struct file *file, void *fh,
1025                             struct v4l2_format *f)
1026 {
1027         struct omap24xxcam_fh *ofh = fh;
1028         struct omap24xxcam_device *cam = ofh->cam;
1029         int rval;
1030
1031         mutex_lock(&cam->mutex);
1032         if (cam->streaming) {
1033                 rval = -EBUSY;
1034                 goto out;
1035         }
1036
1037         rval = vidioc_int_s_fmt_cap(cam->sdev, f);
1038
1039 out:
1040         mutex_unlock(&cam->mutex);
1041
1042         if (!rval) {
1043                 mutex_lock(&ofh->vbq.lock);
1044                 ofh->pix = f->fmt.pix;
1045                 mutex_unlock(&ofh->vbq.lock);
1046         }
1047
1048         memset(f, 0, sizeof(*f));
1049         vidioc_g_fmt_cap(file, fh, f);
1050
1051         return rval;
1052 }
1053
1054 static int vidioc_try_fmt_cap(struct file *file, void *fh,
1055                               struct v4l2_format *f)
1056 {
1057         struct omap24xxcam_fh *ofh = fh;
1058         struct omap24xxcam_device *cam = ofh->cam;
1059         int rval;
1060
1061         mutex_lock(&cam->mutex);
1062         rval = vidioc_int_try_fmt_cap(cam->sdev, f);
1063         mutex_unlock(&cam->mutex);
1064
1065         return rval;
1066 }
1067
1068 static int vidioc_reqbufs(struct file *file, void *fh,
1069                           struct v4l2_requestbuffers *b)
1070 {
1071         struct omap24xxcam_fh *ofh = fh;
1072         struct omap24xxcam_device *cam = ofh->cam;
1073         int rval;
1074
1075         mutex_lock(&cam->mutex);
1076         if (cam->streaming) {
1077                 mutex_unlock(&cam->mutex);
1078                 return -EBUSY;
1079         }
1080
1081         omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1082         mutex_unlock(&cam->mutex);
1083
1084         rval = videobuf_reqbufs(&ofh->vbq, b);
1085
1086         /*
1087          * Either videobuf_reqbufs failed or the buffers are not
1088          * memory-mapped (which would need special attention).
1089          */
1090         if (rval || b->memory != V4L2_MEMORY_MMAP)
1091                 goto out;
1092
1093         rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, b->count);
1094         if (rval)
1095                 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1096
1097 out:
1098         return rval;
1099 }
1100
1101 static int vidioc_querybuf(struct file *file, void *fh,
1102                            struct v4l2_buffer *b)
1103 {
1104         struct omap24xxcam_fh *ofh = fh;
1105
1106         return videobuf_querybuf(&ofh->vbq, b);
1107 }
1108
1109 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1110 {
1111         struct omap24xxcam_fh *ofh = fh;
1112
1113         return videobuf_qbuf(&ofh->vbq, b);
1114 }
1115
1116 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1117 {
1118         struct omap24xxcam_fh *ofh = fh;
1119         struct omap24xxcam_device *cam = ofh->cam;
1120         struct videobuf_buffer *vb;
1121         int rval;
1122
1123         rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
1124         if (rval)
1125                 goto out;
1126
1127         vb = ofh->vbq.bufs[b->index];
1128
1129         mutex_lock(&cam->mutex);
1130         /* _needs_reset returns -EIO if reset is required. */
1131         rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr);
1132         mutex_unlock(&cam->mutex);
1133         if (rval == -EIO)
1134                 schedule_work(&cam->sensor_reset_work);
1135         else
1136                 rval = 0;
1137
1138 out:
1139         /*
1140          * This is a hack. User space won't get the index of this
1141          * buffer and does not want to requeue it so we requeue it
1142          * here.
1143          */
1144         if (rval == -EIO)
1145                 videobuf_qbuf(&ofh->vbq, b);
1146
1147         return rval;
1148 }
1149
1150 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1151 {
1152         struct omap24xxcam_fh *ofh = fh;
1153         struct omap24xxcam_device *cam = ofh->cam;
1154         int rval;
1155
1156         mutex_lock(&cam->mutex);
1157         if (cam->streaming) {
1158                 rval = -EBUSY;
1159                 goto out;
1160         }
1161
1162         rval = omap24xxcam_sensor_if_enable(cam);
1163         if (rval) {
1164                 dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n");
1165                 goto out;
1166         }
1167
1168         rval = videobuf_streamon(&ofh->vbq);
1169         if (!rval) {
1170                 cam->streaming = file;
1171                 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1172         }
1173
1174 out:
1175         mutex_unlock(&cam->mutex);
1176
1177         return rval;
1178 }
1179
1180 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1181 {
1182         struct omap24xxcam_fh *ofh = fh;
1183         struct omap24xxcam_device *cam = ofh->cam;
1184         struct videobuf_queue *q = &ofh->vbq;
1185         int rval;
1186
1187         atomic_inc(&cam->reset_disable);
1188
1189         flush_scheduled_work();
1190
1191         rval = videobuf_streamoff(q);
1192         if (!rval) {
1193                 mutex_lock(&cam->mutex);
1194                 cam->streaming = NULL;
1195                 mutex_unlock(&cam->mutex);
1196                 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1197         }
1198
1199         atomic_dec(&cam->reset_disable);
1200
1201         return rval;
1202 }
1203
1204 static int vidioc_enum_input(struct file *file, void *fh,
1205                              struct v4l2_input *inp)
1206 {
1207         if (inp->index > 0)
1208                 return -EINVAL;
1209
1210         strlcpy(inp->name, "camera", sizeof(inp->name));
1211         inp->type = V4L2_INPUT_TYPE_CAMERA;
1212
1213         return 0;
1214 }
1215
1216 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
1217 {
1218         *i = 0;
1219
1220         return 0;
1221 }
1222
1223 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
1224 {
1225         if (i > 0)
1226                 return -EINVAL;
1227
1228         return 0;
1229 }
1230
1231 static int vidioc_queryctrl(struct file *file, void *fh,
1232                             struct v4l2_queryctrl *a)
1233 {
1234         struct omap24xxcam_fh *ofh = fh;
1235         struct omap24xxcam_device *cam = ofh->cam;
1236         int rval;
1237
1238         rval = vidioc_int_queryctrl(cam->sdev, a);
1239
1240         return rval;
1241 }
1242
1243 static int vidioc_g_ctrl(struct file *file, void *fh,
1244                          struct v4l2_control *a)
1245 {
1246         struct omap24xxcam_fh *ofh = fh;
1247         struct omap24xxcam_device *cam = ofh->cam;
1248         int rval;
1249
1250         mutex_lock(&cam->mutex);
1251         rval = vidioc_int_g_ctrl(cam->sdev, a);
1252         mutex_unlock(&cam->mutex);
1253
1254         return rval;
1255 }
1256
1257 static int vidioc_s_ctrl(struct file *file, void *fh,
1258                          struct v4l2_control *a)
1259 {
1260         struct omap24xxcam_fh *ofh = fh;
1261         struct omap24xxcam_device *cam = ofh->cam;
1262         int rval;
1263
1264         mutex_lock(&cam->mutex);
1265         rval = vidioc_int_s_ctrl(cam->sdev, a);
1266         mutex_unlock(&cam->mutex);
1267
1268         return rval;
1269 }
1270
1271 static int vidioc_g_parm(struct file *file, void *fh,
1272                          struct v4l2_streamparm *a) {
1273         struct omap24xxcam_fh *ofh = fh;
1274         struct omap24xxcam_device *cam = ofh->cam;
1275         int rval;
1276
1277         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1278                 return -EINVAL;
1279
1280         mutex_lock(&cam->mutex);
1281         rval = vidioc_int_g_parm(cam->sdev, a);
1282         mutex_unlock(&cam->mutex);
1283
1284         return rval;
1285 }
1286
1287 static int vidioc_s_parm(struct file *file, void *fh,
1288                          struct v4l2_streamparm *a)
1289 {
1290         struct omap24xxcam_fh *ofh = fh;
1291         struct omap24xxcam_device *cam = ofh->cam;
1292         struct v4l2_streamparm old_streamparm;
1293         int rval;
1294
1295         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1296                 return -EINVAL;
1297
1298         mutex_lock(&cam->mutex);
1299         if (cam->streaming) {
1300                 rval = -EBUSY;
1301                 goto out;
1302         }
1303
1304         old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1305         rval = vidioc_int_g_parm(cam->sdev, &old_streamparm);
1306         if (rval)
1307                 goto out;
1308
1309         rval = vidioc_int_s_parm(cam->sdev, a);
1310         if (rval)
1311                 goto out;
1312
1313         rval = omap24xxcam_sensor_if_enable(cam);
1314         /*
1315          * Revert to old streaming parameters if enabling sensor
1316          * interface with the new ones failed.
1317          */
1318         if (rval)
1319                 vidioc_int_s_parm(cam->sdev, &old_streamparm);
1320
1321 out:
1322         mutex_unlock(&cam->mutex);
1323
1324         return rval;
1325 }
1326
1327 /*
1328  *
1329  * File operations.
1330  *
1331  */
1332
1333 static unsigned int omap24xxcam_poll(struct file *file,
1334                                      struct poll_table_struct *wait)
1335 {
1336         struct omap24xxcam_fh *fh = file->private_data;
1337         struct omap24xxcam_device *cam = fh->cam;
1338         struct videobuf_buffer *vb;
1339
1340         mutex_lock(&cam->mutex);
1341         if (cam->streaming != file) {
1342                 mutex_unlock(&cam->mutex);
1343                 return POLLERR;
1344         }
1345         mutex_unlock(&cam->mutex);
1346
1347         mutex_lock(&fh->vbq.lock);
1348         if (list_empty(&fh->vbq.stream)) {
1349                 mutex_unlock(&fh->vbq.lock);
1350                 return POLLERR;
1351         }
1352         vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
1353         mutex_unlock(&fh->vbq.lock);
1354
1355         poll_wait(file, &vb->done, wait);
1356
1357         if (vb->state == STATE_DONE || vb->state == STATE_ERROR)
1358                 return POLLIN | POLLRDNORM;
1359
1360         return 0;
1361 }
1362
1363 static int omap24xxcam_mmap_buffers(struct file *file,
1364                                     struct vm_area_struct *vma)
1365 {
1366         struct omap24xxcam_fh *fh = file->private_data;
1367         struct omap24xxcam_device *cam = fh->cam;
1368         struct videobuf_queue *vbq = &fh->vbq;
1369         struct videobuf_buffer *vb;
1370         unsigned int first, last, size, i, j;
1371         int err = 0;
1372
1373         mutex_lock(&cam->mutex);
1374         if (cam->streaming) {
1375                 mutex_unlock(&cam->mutex);
1376                 return -EBUSY;
1377         }
1378         mutex_unlock(&cam->mutex);
1379         mutex_lock(&vbq->lock);
1380
1381         /* look for first buffer to map */
1382         for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1383                 if (NULL == vbq->bufs[first])
1384                         continue;
1385                 if (V4L2_MEMORY_MMAP != vbq->bufs[first]->memory)
1386                         continue;
1387                 if (vbq->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1388                         break;
1389         }
1390
1391         /* look for last buffer to map */
1392         for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1393                 if (NULL == vbq->bufs[last])
1394                         continue;
1395                 if (V4L2_MEMORY_MMAP != vbq->bufs[last]->memory)
1396                         continue;
1397                 size += vbq->bufs[last]->bsize;
1398                 if (size == (vma->vm_end - vma->vm_start))
1399                         break;
1400         }
1401
1402         size = 0;
1403         for (i = first; i <= last; i++) {
1404                 vb = vbq->bufs[i];
1405                 for (j = 0; j < vb->dma.sglen; j++) {
1406                         err = remap_pfn_range(
1407                                 vma, vma->vm_start + size,
1408                                 page_to_pfn(vb->dma.sglist[j].page),
1409                                 vb->dma.sglist[j].length, vma->vm_page_prot);
1410                         if (err)
1411                                 goto out;
1412                         size += vb->dma.sglist[j].length;
1413                 }
1414         }
1415
1416 out:
1417         mutex_unlock(&vbq->lock);
1418
1419         return err;
1420 }
1421
1422 static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma)
1423 {
1424         struct omap24xxcam_fh *fh = file->private_data;
1425         int rval;
1426
1427         /* let the video-buf mapper check arguments and set-up structures */
1428         rval = videobuf_mmap_mapper(&fh->vbq, vma);
1429         if (rval)
1430                 return rval;
1431
1432         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1433
1434         /* do mapping to our allocated buffers */
1435         rval = omap24xxcam_mmap_buffers(file, vma);
1436         /*
1437          * In case of error, free vma->vm_private_data allocated by
1438          * videobuf_mmap_mapper.
1439          */
1440         if (rval)
1441                 kfree(vma->vm_private_data);
1442
1443         return rval;
1444 }
1445
1446 static int omap24xxcam_open(struct inode *inode, struct file *file)
1447 {
1448         int minor = iminor(inode);
1449         struct omap24xxcam_device *cam = omap24xxcam.priv;
1450         struct omap24xxcam_fh *fh;
1451         struct v4l2_format format;
1452
1453         if (!cam || !cam->vfd || (cam->vfd->minor != minor))
1454                 return -ENODEV;
1455
1456         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1457         if (fh == NULL)
1458                 return -ENOMEM;
1459
1460         mutex_lock(&cam->mutex);
1461         if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
1462                 mutex_unlock(&cam->mutex);
1463                 goto out_try_module_get;
1464         }
1465
1466         if (atomic_inc_return(&cam->users) == 1) {
1467                 omap24xxcam_hwinit(cam);
1468                 if (omap24xxcam_sensor_enable(cam)) {
1469                         mutex_unlock(&cam->mutex);
1470                         goto out_omap24xxcam_sensor_enable;
1471                 }
1472         }
1473         mutex_unlock(&cam->mutex);
1474
1475         fh->cam = cam;
1476         mutex_lock(&cam->mutex);
1477         vidioc_int_g_fmt_cap(cam->sdev, &format);
1478         mutex_unlock(&cam->mutex);
1479         /* FIXME: how about fh->pix when there are more users? */
1480         fh->pix = format.fmt.pix;
1481
1482         file->private_data = fh;
1483
1484         spin_lock_init(&fh->vbq_lock);
1485
1486         videobuf_queue_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL,
1487                             &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1488                             V4L2_FIELD_NONE,
1489                             sizeof(struct videobuf_buffer), fh);
1490
1491         return 0;
1492
1493 out_omap24xxcam_sensor_enable:
1494         omap24xxcam_poweron_reset(cam);
1495         module_put(cam->sdev->module);
1496
1497 out_try_module_get:
1498         kfree(fh);
1499
1500         return -ENODEV;
1501 }
1502
1503 static int omap24xxcam_release(struct inode *inode, struct file *file)
1504 {
1505         struct omap24xxcam_fh *fh = file->private_data;
1506         struct omap24xxcam_device *cam = fh->cam;
1507
1508         atomic_inc(&cam->reset_disable);
1509
1510         flush_scheduled_work();
1511
1512         mutex_lock(&cam->mutex);
1513         /* stop streaming capture */
1514         if (cam->streaming == file) {
1515                 cam->streaming = NULL;
1516                 mutex_unlock(&cam->mutex);
1517                 videobuf_streamoff(&fh->vbq);
1518                 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1519         } else {
1520                 mutex_unlock(&cam->mutex);
1521         }
1522
1523         atomic_dec(&cam->reset_disable);
1524
1525         omap24xxcam_vbq_free_mmap_buffers(&fh->vbq);
1526
1527         /*
1528          * Make sure the reset work we might have scheduled is not
1529          * pending! It may be run *only* if we have users. (And it may
1530          * not be scheduled anymore since streaming is already
1531          * disabled.)
1532          */
1533         flush_scheduled_work();
1534
1535         mutex_lock(&cam->mutex);
1536         if (atomic_dec_return(&cam->users) == 0) {
1537                 omap24xxcam_sensor_disable(cam);
1538                 omap24xxcam_poweron_reset(cam);
1539         }
1540         mutex_unlock(&cam->mutex);
1541
1542         file->private_data = NULL;
1543
1544         module_put(cam->sdev->module);
1545         kfree(fh);
1546
1547         return 0;
1548 }
1549
1550 static struct file_operations omap24xxcam_fops = {
1551         .owner   = THIS_MODULE,
1552         .llseek  = no_llseek,
1553         .ioctl   = video_ioctl2,
1554         .poll    = omap24xxcam_poll,
1555         .mmap    = omap24xxcam_mmap,
1556         .open    = omap24xxcam_open,
1557         .release = omap24xxcam_release,
1558 };
1559
1560 /*
1561  *
1562  * Power management.
1563  *
1564  */
1565
1566 #ifdef CONFIG_PM
1567 static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state)
1568 {
1569         struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1570
1571         if (atomic_read(&cam->users) == 0)
1572                 return 0;
1573
1574         if (!atomic_read(&cam->reset_disable))
1575                 omap24xxcam_capture_stop(cam);
1576
1577         omap24xxcam_sensor_disable(cam);
1578         omap24xxcam_poweron_reset(cam);
1579
1580         return 0;
1581 }
1582
1583 static int omap24xxcam_resume(struct platform_device *pdev)
1584 {
1585         struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1586
1587         if (atomic_read(&cam->users) == 0)
1588                 return 0;
1589
1590         omap24xxcam_hwinit(cam);
1591         omap24xxcam_sensor_enable(cam);
1592
1593         if (!atomic_read(&cam->reset_disable))
1594                 omap24xxcam_capture_cont(cam);
1595
1596         return 0;
1597 }
1598 #endif /* CONFIG_PM */
1599
1600 /*
1601  *
1602  * Camera device (i.e. /dev/video).
1603  *
1604  */
1605
1606 static int omap24xxcam_device_register(struct v4l2_int_device *ctl,
1607                                        struct v4l2_int_device *s)
1608 {
1609         struct omap24xxcam_device *cam = ctl->priv;
1610         struct video_device *vfd;
1611         int rval;
1612
1613         /* We already have a slave. */
1614         if (cam->sdev)
1615                 return -EBUSY;
1616
1617         cam->sdev = s;
1618
1619         if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
1620                 dev_err(cam->dev, "could not register sysfs entry\n");
1621                 rval = -EBUSY;
1622                 goto err;
1623         }
1624
1625         /* initialize the video_device struct */
1626         vfd = cam->vfd = video_device_alloc();
1627         if (!vfd) {
1628                 dev_err(cam->dev, "could not allocate video device struct\n");
1629                 rval = -ENOMEM;
1630                 goto err;
1631         }
1632         vfd->release = video_device_release;
1633
1634         vfd->dev = cam->dev;
1635
1636         strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1637         vfd->type                = VID_TYPE_CAPTURE | VID_TYPE_CHROMAKEY;
1638         /* need to register for a VID_HARDWARE_* ID in videodev.h */
1639         vfd->hardware            = 0;
1640         vfd->fops                = &omap24xxcam_fops;
1641         vfd->priv                = cam;
1642         vfd->minor               = -1;
1643
1644         vfd->vidioc_querycap     = vidioc_querycap;
1645         vfd->vidioc_enum_fmt_cap = vidioc_enum_fmt_cap;
1646         vfd->vidioc_g_fmt_cap    = vidioc_g_fmt_cap;
1647         vfd->vidioc_s_fmt_cap    = vidioc_s_fmt_cap;
1648         vfd->vidioc_try_fmt_cap  = vidioc_try_fmt_cap;
1649         vfd->vidioc_reqbufs      = vidioc_reqbufs;
1650         vfd->vidioc_querybuf     = vidioc_querybuf;
1651         vfd->vidioc_qbuf         = vidioc_qbuf;
1652         vfd->vidioc_dqbuf        = vidioc_dqbuf;
1653         vfd->vidioc_streamon     = vidioc_streamon;
1654         vfd->vidioc_streamoff    = vidioc_streamoff;
1655         vfd->vidioc_enum_input   = vidioc_enum_input;
1656         vfd->vidioc_g_input      = vidioc_g_input;
1657         vfd->vidioc_s_input      = vidioc_s_input;
1658         vfd->vidioc_queryctrl    = vidioc_queryctrl;
1659         vfd->vidioc_g_ctrl       = vidioc_g_ctrl;
1660         vfd->vidioc_s_ctrl       = vidioc_s_ctrl;
1661         vfd->vidioc_g_parm       = vidioc_g_parm;
1662         vfd->vidioc_s_parm       = vidioc_s_parm;
1663
1664         omap24xxcam_hwinit(cam);
1665
1666         rval = omap24xxcam_sensor_init(cam);
1667         if (rval)
1668                 goto err;
1669
1670         if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1671                 dev_err(cam->dev, "could not register V4L device\n");
1672                 vfd->minor = -1;
1673                 rval = -EBUSY;
1674                 goto err;
1675         }
1676
1677         omap24xxcam_poweron_reset(cam);
1678
1679         dev_info(cam->dev, "registered device video%d\n", vfd->minor);
1680
1681         return 0;
1682
1683 err:
1684         omap24xxcam_device_unregister(ctl);
1685
1686         return rval;
1687 }
1688
1689 static void omap24xxcam_device_unregister(struct v4l2_int_device *ctl)
1690 {
1691         struct omap24xxcam_device *cam = ctl->priv;
1692
1693         omap24xxcam_sensor_exit(cam);
1694
1695         if (cam->vfd) {
1696                 if (cam->vfd->minor == -1) {
1697                         /*
1698                          * The device was never registered, so release the
1699                          * video_device struct directly.
1700                          */
1701                         video_device_release(cam->vfd);
1702                 } else {
1703                         /*
1704                          * The unregister function will release the
1705                          * video_device struct as well as
1706                          * unregistering it.
1707                          */
1708                         video_unregister_device(cam->vfd);
1709                 }
1710                 cam->vfd = NULL;
1711         }
1712
1713         device_remove_file(cam->dev, &dev_attr_streaming);
1714
1715         cam->sdev = NULL;
1716 }
1717
1718 static struct v4l2_int_master omap24xxcam_master = {
1719         .attach = omap24xxcam_device_register,
1720         .detach = omap24xxcam_device_unregister,
1721 };
1722
1723 static struct v4l2_int_device omap24xxcam = {
1724         .module = THIS_MODULE,
1725         .name   = CAM_NAME,
1726         .type   = v4l2_int_type_master,
1727         .u      = {
1728                 .master = &omap24xxcam_master
1729         },
1730 };
1731
1732 /*
1733  *
1734  * Driver initialisation and deinitialisation.
1735  *
1736  */
1737
1738 static int __init omap24xxcam_probe(struct platform_device *pdev)
1739 {
1740         struct omap24xxcam_device *cam;
1741         struct resource *mem;
1742         int irq;
1743
1744         cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1745         if (!cam) {
1746                 dev_err(&pdev->dev, "could not allocate memory\n");
1747                 goto err;
1748         }
1749
1750         platform_set_drvdata(pdev, cam);
1751
1752         cam->dev = &pdev->dev;
1753
1754         /*
1755          * Impose a lower limit on the amount of memory allocated for
1756          * capture. We require at least enough memory to double-buffer
1757          * QVGA (300KB).
1758          */
1759         if (capture_mem < 320 * 240 * 2 * 2)
1760                 capture_mem = 320 * 240 * 2 * 2;
1761         cam->capture_mem = capture_mem;
1762
1763         /* request the mem region for the camera registers */
1764         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1765         if (!mem) {
1766                 dev_err(cam->dev, "no mem resource?\n");
1767                 goto err;
1768         }
1769         if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
1770                                 pdev->name)) {
1771                 dev_err(cam->dev,
1772                         "cannot reserve camera register I/O region\n");
1773                 goto err;
1774         }
1775         cam->mmio_base_phys = mem->start;
1776         cam->mmio_size = (mem->end - mem->start) + 1;
1777
1778         /* map the region */
1779         cam->mmio_base = (unsigned long)
1780                 ioremap_nocache(cam->mmio_base_phys, cam->mmio_size);
1781         if (!cam->mmio_base) {
1782                 dev_err(cam->dev, "cannot map camera register I/O region\n");
1783                 goto err;
1784         }
1785
1786         irq = platform_get_irq(pdev, 0);
1787         if (irq <= 0) {
1788                 dev_err(cam->dev, "no irq for camera?\n");
1789                 goto err;
1790         }
1791
1792         /* install the interrupt service routine */
1793         if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) {
1794                 dev_err(cam->dev,
1795                         "could not install interrupt service routine\n");
1796                 goto err;
1797         }
1798         cam->irq = irq;
1799
1800         if (omap24xxcam_clock_get(cam))
1801                 goto err;
1802
1803         INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work);
1804
1805         mutex_init(&cam->mutex);
1806         spin_lock_init(&cam->core_enable_disable_lock);
1807
1808         omap24xxcam_sgdma_init(&cam->sgdma,
1809                                cam->mmio_base + CAMDMA_REG_OFFSET,
1810                                omap24xxcam_stalled_dma_reset,
1811                                (unsigned long)cam);
1812
1813         omap24xxcam.priv = cam;
1814
1815         if (v4l2_int_device_register(&omap24xxcam))
1816                 goto err;
1817
1818         return 0;
1819
1820 err:
1821         omap24xxcam_remove(pdev);
1822         return -ENODEV;
1823 }
1824
1825 static int omap24xxcam_remove(struct platform_device *pdev)
1826 {
1827         struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1828
1829         if (!cam)
1830                 return 0;
1831
1832         if (omap24xxcam.priv != NULL)
1833                 v4l2_int_device_unregister(&omap24xxcam);
1834         omap24xxcam.priv = NULL;
1835
1836         omap24xxcam_clock_put(cam);
1837
1838         if (cam->irq) {
1839                 free_irq(cam->irq, cam);
1840                 cam->irq = 0;
1841         }
1842
1843         if (cam->mmio_base) {
1844                 iounmap((void *)cam->mmio_base);
1845                 cam->mmio_base = 0;
1846         }
1847
1848         if (cam->mmio_base_phys) {
1849                 release_mem_region(cam->mmio_base_phys, cam->mmio_size);
1850                 cam->mmio_base_phys = 0;
1851         }
1852
1853         kfree(cam);
1854
1855         return 0;
1856 }
1857
1858 static struct platform_driver omap24xxcam_driver = {
1859         .probe   = omap24xxcam_probe,
1860         .remove  = omap24xxcam_remove,
1861 #ifdef CONFIG_PM
1862         .suspend = omap24xxcam_suspend,
1863         .resume  = omap24xxcam_resume,
1864 #endif
1865         .driver  = {
1866                 .name = CAM_NAME,
1867         },
1868 };
1869
1870 /*
1871  *
1872  * Module initialisation and deinitialisation
1873  *
1874  */
1875
1876 static int __init omap24xxcam_init(void)
1877 {
1878         return platform_driver_register(&omap24xxcam_driver);
1879 }
1880
1881 static void __exit omap24xxcam_cleanup(void)
1882 {
1883         platform_driver_unregister(&omap24xxcam_driver);
1884 }
1885
1886 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
1887 MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
1888 MODULE_LICENSE("GPL");
1889 module_param(video_nr, int, 0);
1890 MODULE_PARM_DESC(video_nr,
1891                  "Minor number for video device (-1 ==> auto assign)");
1892 module_param(capture_mem, int, 0);
1893 MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture "
1894                  "buffers (default 4800kiB)");
1895
1896 module_init(omap24xxcam_init);
1897 module_exit(omap24xxcam_cleanup);