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 *s);
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         struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
230         size_t alloc_size;
231         struct page *page;
232         int i;
233
234         if (dma->sglist == NULL)
235                 return;
236
237         i = dma->sglen;
238         while (i) {
239                 i--;
240                 alloc_size = sg_dma_len(&dma->sglist[i]);
241                 page = sg_page(&dma->sglist[i]);
242                 do {
243                         ClearPageReserved(page++);
244                 } while (alloc_size -= PAGE_SIZE);
245                 __free_pages(sg_page(&dma->sglist[i]),
246                              get_order(sg_dma_len(&dma->sglist[i])));
247         }
248
249         kfree(dma->sglist);
250         dma->sglist = NULL;
251 }
252
253 /* Release all memory related to the videobuf_queue. */
254 static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue *vbq)
255 {
256         int i;
257
258         mutex_lock(&vbq->vb_lock);
259
260         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
261                 if (NULL == vbq->bufs[i])
262                         continue;
263                 if (V4L2_MEMORY_MMAP != vbq->bufs[i]->memory)
264                         continue;
265                 vbq->ops->buf_release(vbq, vbq->bufs[i]);
266                 omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
267                 kfree(vbq->bufs[i]);
268                 vbq->bufs[i] = NULL;
269         }
270
271         mutex_unlock(&vbq->vb_lock);
272
273         videobuf_mmap_free(vbq);
274 }
275
276 /*
277  * Allocate physically as contiguous as possible buffer for video
278  * frame and allocate and build DMA scatter-gather list for it.
279  */
280 static int omap24xxcam_vbq_alloc_mmap_buffer(struct videobuf_buffer *vb)
281 {
282         unsigned int order;
283         size_t alloc_size, size = vb->bsize; /* vb->bsize is page aligned */
284         struct page *page;
285         int max_pages, err = 0, i = 0;
286         struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
287
288         /*
289          * allocate maximum size scatter-gather list. Note this is
290          * overhead. We may not use as many entries as we allocate
291          */
292         max_pages = vb->bsize >> PAGE_SHIFT;
293         dma->sglist = kcalloc(max_pages, sizeof(*dma->sglist), GFP_KERNEL);
294         if (dma->sglist == NULL) {
295                 err = -ENOMEM;
296                 goto out;
297         }
298
299         while (size) {
300                 order = get_order(size);
301                 /*
302                  * do not over-allocate even if we would get larger
303                  * contiguous chunk that way
304                  */
305                 if ((PAGE_SIZE << order) > size)
306                         order--;
307
308                 /* try to allocate as many contiguous pages as possible */
309                 page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
310                 /* if allocation fails, try to allocate smaller amount */
311                 while (page == NULL) {
312                         order--;
313                         page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
314                         if (page == NULL && !order) {
315                                 err = -ENOMEM;
316                                 goto out;
317                         }
318                 }
319                 size -= (PAGE_SIZE << order);
320
321                 /* append allocated chunk of pages into scatter-gather list */
322                 sg_set_page(&dma->sglist[i], page, PAGE_SIZE << order, 0);
323                 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         dma->nr_pages = dma->sglen;
341         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->vb_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                         videobuf_to_dma(vbq->bufs[i])->sglen, i);
365         }
366
367         mutex_unlock(&vbq->vb_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->vb_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 = VIDEOBUF_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 = VIDEOBUF_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 = videobuf_to_dma(vb);
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, videobuf_to_dma(vb));
428                 videobuf_dma_free(videobuf_to_dma(vb));
429         }
430
431         vb->state = VIDEOBUF_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 != VIDEOBUF_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 == VIDEOBUF_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, videobuf_to_dma(vb));
529                 else
530                         err = videobuf_iolock(vbq, vb, NULL);
531         }
532
533         if (!err)
534                 vb->state = VIDEOBUF_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 = VIDEOBUF_ACTIVE;
556
557         err = omap24xxcam_sgdma_queue(&fh->cam->sgdma,
558                                       videobuf_to_dma(vb)->sglist,
559                                       videobuf_to_dma(vb)->sglen, vb->size,
560                                       omap24xxcam_vbq_complete, vb);
561
562         if (!err) {
563                 spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
564                 if (++cam->sgdma_in_queue == 1
565                     && !atomic_read(&cam->in_reset))
566                         omap24xxcam_core_enable(cam);
567                 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
568         } else {
569                 /*
570                  * Oops. We're not supposed to get any errors here.
571                  * The only way we could get an error is if we ran out
572                  * of scatter-gather DMA slots, but we are supposed to
573                  * have at least as many scatter-gather DMA slots as
574                  * video buffers so that can't happen.
575                  */
576                 dev_err(cam->dev, "failed to queue a video buffer for dma!\n");
577                 dev_err(cam->dev, "likely a bug in the driver!\n");
578                 vb->state = state;
579         }
580 }
581
582 static struct videobuf_queue_ops omap24xxcam_vbq_ops = {
583         .buf_setup   = omap24xxcam_vbq_setup,
584         .buf_prepare = omap24xxcam_vbq_prepare,
585         .buf_queue   = omap24xxcam_vbq_queue,
586         .buf_release = omap24xxcam_vbq_release,
587 };
588
589 /*
590  *
591  * OMAP main camera system
592  *
593  */
594
595 /*
596  * Reset camera block to power-on state.
597  */
598 static void omap24xxcam_poweron_reset(const struct omap24xxcam_device *cam)
599 {
600         int max_loop = RESET_TIMEOUT_NS;
601
602         /* Reset whole camera subsystem */
603         omap24xxcam_reg_out(cam->mmio_base,
604                             CAM_SYSCONFIG,
605                             CAM_SYSCONFIG_SOFTRESET);
606
607         /* Wait till it's finished */
608         while (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
609                  & CAM_SYSSTATUS_RESETDONE)
610                && --max_loop) {
611                 ndelay(1);
612         }
613
614         if (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
615               & CAM_SYSSTATUS_RESETDONE))
616                 dev_err(cam->dev, "camera soft reset timeout\n");
617 }
618
619 /*
620  * (Re)initialise the camera block.
621  */
622 static void omap24xxcam_hwinit(const struct omap24xxcam_device *cam)
623 {
624         omap24xxcam_poweron_reset(cam);
625
626         /* set the camera subsystem autoidle bit */
627         omap24xxcam_reg_out(cam->mmio_base, CAM_SYSCONFIG,
628                             CAM_SYSCONFIG_AUTOIDLE);
629
630         /* set the camera MMU autoidle bit */
631         omap24xxcam_reg_out(cam->mmio_base,
632                             CAMMMU_REG_OFFSET + CAMMMU_SYSCONFIG,
633                             CAMMMU_SYSCONFIG_AUTOIDLE);
634
635         omap24xxcam_core_hwinit(cam);
636
637         omap24xxcam_dma_hwinit(&cam->sgdma.dma);
638 }
639
640 /*
641  * Callback for dma transfer stalling.
642  */
643 static void omap24xxcam_stalled_dma_reset(unsigned long data)
644 {
645         struct omap24xxcam_device *cam = (struct omap24xxcam_device *)data;
646
647         if (!atomic_read(&cam->in_reset)) {
648                 dev_dbg(cam->dev, "dma stalled, resetting camera\n");
649                 omap24xxcam_reset(cam);
650         }
651 }
652
653 /*
654  * Stop capture. Mark we're doing a reset, stop DMA transfers and
655  * core. (No new scatter-gather transfers will be queued whilst
656  * in_reset is non-zero.)
657  *
658  * If omap24xxcam_capture_stop is called from several places at
659  * once, only the first call will have an effect. Similarly, the last
660  * call omap24xxcam_streaming_cont will have effect.
661  *
662  * Serialisation is ensured by using cam->core_enable_disable_lock.
663  */
664 static void omap24xxcam_capture_stop(struct omap24xxcam_device *cam)
665 {
666         unsigned long flags;
667
668         spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
669
670         if (atomic_inc_return(&cam->in_reset) != 1) {
671                 spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
672                 return;
673         }
674
675         omap24xxcam_core_disable(cam);
676
677         spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
678
679         omap24xxcam_sgdma_sync(&cam->sgdma);
680 }
681
682 /*
683  * Reset and continue streaming.
684  *
685  * Note: Resetting the camera FIFO via the CC_RST bit in the CC_CTRL
686  * register is supposed to be sufficient to recover from a camera
687  * interface error, but it doesn't seem to be enough. If we only do
688  * that then subsequent image captures are out of sync by either one
689  * or two times DMA_THRESHOLD bytes. Resetting and re-initializing the
690  * entire camera subsystem prevents the problem with frame
691  * synchronization.
692  */
693 static void omap24xxcam_capture_cont(struct omap24xxcam_device *cam)
694 {
695         unsigned long flags;
696
697         spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
698
699         if (atomic_read(&cam->in_reset) != 1)
700                 goto out;
701
702         omap24xxcam_hwinit(cam);
703
704         omap24xxcam_sensor_if_enable(cam);
705
706         omap24xxcam_sgdma_process(&cam->sgdma);
707
708         if (cam->sgdma_in_queue)
709                 omap24xxcam_core_enable(cam);
710
711 out:
712         atomic_dec(&cam->in_reset);
713         spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
714 }
715
716 static ssize_t
717 omap24xxcam_streaming_show(struct device *dev, struct device_attribute *attr,
718                 char *buf)
719 {
720         struct omap24xxcam_device *cam = dev_get_drvdata(dev);
721
722         return sprintf(buf, "%s\n", cam->streaming ?  "active" : "inactive");
723 }
724 static DEVICE_ATTR(streaming, S_IRUGO, omap24xxcam_streaming_show, NULL);
725
726 /*
727  * Stop capture and restart it. I.e. reset the camera during use.
728  */
729 static void omap24xxcam_reset(struct omap24xxcam_device *cam)
730 {
731         omap24xxcam_capture_stop(cam);
732         omap24xxcam_capture_cont(cam);
733 }
734
735 /*
736  * The main interrupt handler.
737  */
738 static irqreturn_t omap24xxcam_isr(int irq, void *arg)
739 {
740         struct omap24xxcam_device *cam = (struct omap24xxcam_device *)arg;
741         u32 irqstatus;
742         unsigned int irqhandled = 0;
743
744         irqstatus = omap24xxcam_reg_in(cam->mmio_base, CAM_IRQSTATUS);
745
746         if (irqstatus &
747             (CAM_IRQSTATUS_DMA_IRQ2 | CAM_IRQSTATUS_DMA_IRQ1
748              | CAM_IRQSTATUS_DMA_IRQ0)) {
749                 omap24xxcam_dma_isr(&cam->sgdma.dma);
750                 irqhandled = 1;
751         }
752         if (irqstatus & CAM_IRQSTATUS_CC_IRQ) {
753                 omap24xxcam_core_isr(cam);
754                 irqhandled = 1;
755         }
756         if (irqstatus & CAM_IRQSTATUS_MMU_IRQ)
757                 dev_err(cam->dev, "unhandled camera MMU interrupt!\n");
758
759         return IRQ_RETVAL(irqhandled);
760 }
761
762 /*
763  *
764  * Sensor handling.
765  *
766  */
767
768 /*
769  * Enable the external sensor interface. Try to negotiate interface
770  * parameters with the sensor and start using the new ones. The calls
771  * to sensor_if_enable and sensor_if_disable need not to be balanced.
772  */
773 static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam)
774 {
775         int rval;
776         struct v4l2_ifparm p;
777
778         rval = vidioc_int_g_ifparm(cam->sdev, &p);
779         if (rval) {
780                 dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
781                 return rval;
782         }
783
784         cam->if_type = p.if_type;
785
786         cam->cc_ctrl = CC_CTRL_CC_EN;
787
788         switch (p.if_type) {
789         case V4L2_IF_TYPE_BT656:
790                 if (p.u.bt656.frame_start_on_rising_vs)
791                         cam->cc_ctrl |= CC_CTRL_NOBT_SYNCHRO;
792                 if (p.u.bt656.bt_sync_correct)
793                         cam->cc_ctrl |= CC_CTRL_BT_CORRECT;
794                 if (p.u.bt656.swap)
795                         cam->cc_ctrl |= CC_CTRL_PAR_ORDERCAM;
796                 if (p.u.bt656.latch_clk_inv)
797                         cam->cc_ctrl |= CC_CTRL_PAR_CLK_POL;
798                 if (p.u.bt656.nobt_hs_inv)
799                         cam->cc_ctrl |= CC_CTRL_NOBT_HS_POL;
800                 if (p.u.bt656.nobt_vs_inv)
801                         cam->cc_ctrl |= CC_CTRL_NOBT_VS_POL;
802
803                 switch (p.u.bt656.mode) {
804                 case V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT:
805                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT8;
806                         break;
807                 case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT:
808                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT10;
809                         break;
810                 case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT:
811                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT12;
812                         break;
813                 case V4L2_IF_TYPE_BT656_MODE_BT_8BIT:
814                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT8;
815                         break;
816                 case V4L2_IF_TYPE_BT656_MODE_BT_10BIT:
817                         cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT10;
818                         break;
819                 default:
820                         dev_err(cam->dev,
821                                 "bt656 interface mode %d not supported\n",
822                                 p.u.bt656.mode);
823                         return -EINVAL;
824                 }
825                 /*
826                  * The clock rate that the sensor wants has changed.
827                  * We have to adjust the xclk from OMAP 2 side to
828                  * match the sensor's wish as closely as possible.
829                  */
830                 if (p.u.bt656.clock_curr != cam->if_u.bt656.xclk) {
831                         u32 xclk = p.u.bt656.clock_curr;
832                         u32 divisor;
833
834                         if (xclk == 0)
835                                 return -EINVAL;
836
837                         if (xclk > CAM_MCLK)
838                                 xclk = CAM_MCLK;
839
840                         divisor = CAM_MCLK / xclk;
841                         if (divisor * xclk < CAM_MCLK)
842                                 divisor++;
843                         if (CAM_MCLK / divisor < p.u.bt656.clock_min
844                             && divisor > 1)
845                                 divisor--;
846                         if (divisor > 30)
847                                 divisor = 30;
848
849                         xclk = CAM_MCLK / divisor;
850
851                         if (xclk < p.u.bt656.clock_min
852                             || xclk > p.u.bt656.clock_max)
853                                 return -EINVAL;
854
855                         cam->if_u.bt656.xclk = xclk;
856                 }
857                 omap24xxcam_core_xclk_set(cam, cam->if_u.bt656.xclk);
858                 break;
859         default:
860                 /* FIXME: how about other interfaces? */
861                 dev_err(cam->dev, "interface type %d not supported\n",
862                         p.if_type);
863                 return -EINVAL;
864         }
865
866         return 0;
867 }
868
869 static void omap24xxcam_sensor_if_disable(const struct omap24xxcam_device *cam)
870 {
871         switch (cam->if_type) {
872         case V4L2_IF_TYPE_BT656:
873                 omap24xxcam_core_xclk_set(cam, 0);
874                 break;
875         }
876 }
877
878 /*
879  * Initialise the sensor hardware.
880  */
881 static int omap24xxcam_sensor_init(struct omap24xxcam_device *cam)
882 {
883         int err = 0;
884         struct v4l2_int_device *sdev = cam->sdev;
885
886         omap24xxcam_clock_on(cam);
887         err = omap24xxcam_sensor_if_enable(cam);
888         if (err) {
889                 dev_err(cam->dev, "sensor interface could not be enabled at "
890                         "initialisation, %d\n", err);
891                 cam->sdev = NULL;
892                 goto out;
893         }
894
895         /* power up sensor during sensor initialization */
896         vidioc_int_s_power(sdev, 1);
897
898         err = vidioc_int_dev_init(sdev);
899         if (err) {
900                 dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
901                 /* Sensor init failed --- it's nonexistent to us! */
902                 cam->sdev = NULL;
903                 goto out;
904         }
905
906         dev_info(cam->dev, "sensor is %s\n", sdev->name);
907
908 out:
909         omap24xxcam_sensor_if_disable(cam);
910         omap24xxcam_clock_off(cam);
911
912         vidioc_int_s_power(sdev, 0);
913
914         return err;
915 }
916
917 static void omap24xxcam_sensor_exit(struct omap24xxcam_device *cam)
918 {
919         if (cam->sdev)
920                 vidioc_int_dev_exit(cam->sdev);
921 }
922
923 static void omap24xxcam_sensor_disable(struct omap24xxcam_device *cam)
924 {
925         omap24xxcam_sensor_if_disable(cam);
926         omap24xxcam_clock_off(cam);
927         vidioc_int_s_power(cam->sdev, 0);
928 }
929
930 /*
931  * Power-up and configure camera sensor. It's ready for capturing now.
932  */
933 static int omap24xxcam_sensor_enable(struct omap24xxcam_device *cam)
934 {
935         int rval;
936
937         omap24xxcam_clock_on(cam);
938
939         omap24xxcam_sensor_if_enable(cam);
940
941         rval = vidioc_int_s_power(cam->sdev, 1);
942         if (rval)
943                 goto out;
944
945         rval = vidioc_int_init(cam->sdev);
946         if (rval)
947                 goto out;
948
949         return 0;
950
951 out:
952         omap24xxcam_sensor_disable(cam);
953
954         return rval;
955 }
956
957 static void omap24xxcam_sensor_reset_work(struct work_struct *work)
958 {
959         struct omap24xxcam_device *cam =
960                 container_of(work, struct omap24xxcam_device,
961                              sensor_reset_work);
962
963         if (atomic_read(&cam->reset_disable))
964                 return;
965
966         omap24xxcam_capture_stop(cam);
967
968         if (vidioc_int_reset(cam->sdev) == 0) {
969                 vidioc_int_init(cam->sdev);
970         } else {
971                 /* Can't reset it by vidioc_int_reset. */
972                 omap24xxcam_sensor_disable(cam);
973                 omap24xxcam_sensor_enable(cam);
974         }
975
976         omap24xxcam_capture_cont(cam);
977 }
978
979 /*
980  *
981  * IOCTL interface.
982  *
983  */
984
985 static int vidioc_querycap(struct file *file, void *fh,
986                            struct v4l2_capability *cap)
987 {
988         struct omap24xxcam_fh *ofh = fh;
989         struct omap24xxcam_device *cam = ofh->cam;
990
991         strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
992         strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
993         cap->version = OMAP24XXCAM_VERSION;
994         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
995
996         return 0;
997 }
998
999 static int vidioc_enum_fmt_cap(struct file *file, void *fh,
1000                                struct v4l2_fmtdesc *f)
1001 {
1002         struct omap24xxcam_fh *ofh = fh;
1003         struct omap24xxcam_device *cam = ofh->cam;
1004         int rval;
1005
1006         rval = vidioc_int_enum_fmt_cap(cam->sdev, f);
1007
1008         return rval;
1009 }
1010
1011 static int vidioc_g_fmt_cap(struct file *file, void *fh,
1012                             struct v4l2_format *f)
1013 {
1014         struct omap24xxcam_fh *ofh = fh;
1015         struct omap24xxcam_device *cam = ofh->cam;
1016         int rval;
1017
1018         mutex_lock(&cam->mutex);
1019         rval = vidioc_int_g_fmt_cap(cam->sdev, f);
1020         mutex_unlock(&cam->mutex);
1021
1022         return rval;
1023 }
1024
1025 static int vidioc_s_fmt_cap(struct file *file, void *fh,
1026                             struct v4l2_format *f)
1027 {
1028         struct omap24xxcam_fh *ofh = fh;
1029         struct omap24xxcam_device *cam = ofh->cam;
1030         int rval;
1031
1032         mutex_lock(&cam->mutex);
1033         if (cam->streaming) {
1034                 rval = -EBUSY;
1035                 goto out;
1036         }
1037
1038         rval = vidioc_int_s_fmt_cap(cam->sdev, f);
1039
1040 out:
1041         mutex_unlock(&cam->mutex);
1042
1043         if (!rval) {
1044                 mutex_lock(&ofh->vbq.vb_lock);
1045                 ofh->pix = f->fmt.pix;
1046                 mutex_unlock(&ofh->vbq.vb_lock);
1047         }
1048
1049         memset(f, 0, sizeof(*f));
1050         vidioc_g_fmt_cap(file, fh, f);
1051
1052         return rval;
1053 }
1054
1055 static int vidioc_try_fmt_cap(struct file *file, void *fh,
1056                               struct v4l2_format *f)
1057 {
1058         struct omap24xxcam_fh *ofh = fh;
1059         struct omap24xxcam_device *cam = ofh->cam;
1060         int rval;
1061
1062         mutex_lock(&cam->mutex);
1063         rval = vidioc_int_try_fmt_cap(cam->sdev, f);
1064         mutex_unlock(&cam->mutex);
1065
1066         return rval;
1067 }
1068
1069 static int vidioc_reqbufs(struct file *file, void *fh,
1070                           struct v4l2_requestbuffers *b)
1071 {
1072         struct omap24xxcam_fh *ofh = fh;
1073         struct omap24xxcam_device *cam = ofh->cam;
1074         int rval;
1075
1076         mutex_lock(&cam->mutex);
1077         if (cam->streaming) {
1078                 mutex_unlock(&cam->mutex);
1079                 return -EBUSY;
1080         }
1081
1082         omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1083         mutex_unlock(&cam->mutex);
1084
1085         rval = videobuf_reqbufs(&ofh->vbq, b);
1086
1087         /*
1088          * Either videobuf_reqbufs failed or the buffers are not
1089          * memory-mapped (which would need special attention).
1090          */
1091         if (rval < 0 || b->memory != V4L2_MEMORY_MMAP)
1092                 goto out;
1093
1094         rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, rval);
1095         if (rval)
1096                 omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
1097
1098 out:
1099         return rval;
1100 }
1101
1102 static int vidioc_querybuf(struct file *file, void *fh,
1103                            struct v4l2_buffer *b)
1104 {
1105         struct omap24xxcam_fh *ofh = fh;
1106
1107         return videobuf_querybuf(&ofh->vbq, b);
1108 }
1109
1110 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1111 {
1112         struct omap24xxcam_fh *ofh = fh;
1113
1114         return videobuf_qbuf(&ofh->vbq, b);
1115 }
1116
1117 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1118 {
1119         struct omap24xxcam_fh *ofh = fh;
1120         struct omap24xxcam_device *cam = ofh->cam;
1121         struct videobuf_buffer *vb;
1122         int rval;
1123
1124 videobuf_dqbuf_again:
1125         rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
1126         if (rval)
1127                 goto out;
1128
1129         vb = ofh->vbq.bufs[b->index];
1130
1131         mutex_lock(&cam->mutex);
1132         /* _needs_reset returns -EIO if reset is required. */
1133         rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr);
1134         mutex_unlock(&cam->mutex);
1135         if (rval == -EIO)
1136                 schedule_work(&cam->sensor_reset_work);
1137         else
1138                 rval = 0;
1139
1140 out:
1141         /*
1142          * This is a hack. We don't want to show -EIO to the user
1143          * space. Requeue the buffer and try again if we're not doing
1144          * this in non-blocking mode.
1145          */
1146         if (rval == -EIO) {
1147                 videobuf_qbuf(&ofh->vbq, b);
1148                 if (!(file->f_flags & O_NONBLOCK))
1149                         goto videobuf_dqbuf_again;
1150                 /*
1151                  * We don't have a videobuf_buffer now --- maybe next
1152                  * time...
1153                  */
1154                 rval = -EAGAIN;
1155         }
1156
1157         return rval;
1158 }
1159
1160 static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1161 {
1162         struct omap24xxcam_fh *ofh = fh;
1163         struct omap24xxcam_device *cam = ofh->cam;
1164         int rval;
1165
1166         mutex_lock(&cam->mutex);
1167         if (cam->streaming) {
1168                 rval = -EBUSY;
1169                 goto out;
1170         }
1171
1172         rval = omap24xxcam_sensor_if_enable(cam);
1173         if (rval) {
1174                 dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n");
1175                 goto out;
1176         }
1177
1178         rval = videobuf_streamon(&ofh->vbq);
1179         if (!rval) {
1180                 cam->streaming = file;
1181                 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1182         }
1183
1184 out:
1185         mutex_unlock(&cam->mutex);
1186
1187         return rval;
1188 }
1189
1190 static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1191 {
1192         struct omap24xxcam_fh *ofh = fh;
1193         struct omap24xxcam_device *cam = ofh->cam;
1194         struct videobuf_queue *q = &ofh->vbq;
1195         int rval;
1196
1197         atomic_inc(&cam->reset_disable);
1198
1199         flush_scheduled_work();
1200
1201         rval = videobuf_streamoff(q);
1202         if (!rval) {
1203                 mutex_lock(&cam->mutex);
1204                 cam->streaming = NULL;
1205                 mutex_unlock(&cam->mutex);
1206                 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1207         }
1208
1209         atomic_dec(&cam->reset_disable);
1210
1211         return rval;
1212 }
1213
1214 static int vidioc_enum_input(struct file *file, void *fh,
1215                              struct v4l2_input *inp)
1216 {
1217         if (inp->index > 0)
1218                 return -EINVAL;
1219
1220         strlcpy(inp->name, "camera", sizeof(inp->name));
1221         inp->type = V4L2_INPUT_TYPE_CAMERA;
1222
1223         return 0;
1224 }
1225
1226 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
1227 {
1228         *i = 0;
1229
1230         return 0;
1231 }
1232
1233 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
1234 {
1235         if (i > 0)
1236                 return -EINVAL;
1237
1238         return 0;
1239 }
1240
1241 static int vidioc_queryctrl(struct file *file, void *fh,
1242                             struct v4l2_queryctrl *a)
1243 {
1244         struct omap24xxcam_fh *ofh = fh;
1245         struct omap24xxcam_device *cam = ofh->cam;
1246         int rval;
1247
1248         rval = vidioc_int_queryctrl(cam->sdev, a);
1249
1250         return rval;
1251 }
1252
1253 static int vidioc_g_ctrl(struct file *file, void *fh,
1254                          struct v4l2_control *a)
1255 {
1256         struct omap24xxcam_fh *ofh = fh;
1257         struct omap24xxcam_device *cam = ofh->cam;
1258         int rval;
1259
1260         mutex_lock(&cam->mutex);
1261         rval = vidioc_int_g_ctrl(cam->sdev, a);
1262         mutex_unlock(&cam->mutex);
1263
1264         return rval;
1265 }
1266
1267 static int vidioc_s_ctrl(struct file *file, void *fh,
1268                          struct v4l2_control *a)
1269 {
1270         struct omap24xxcam_fh *ofh = fh;
1271         struct omap24xxcam_device *cam = ofh->cam;
1272         int rval;
1273
1274         mutex_lock(&cam->mutex);
1275         rval = vidioc_int_s_ctrl(cam->sdev, a);
1276         mutex_unlock(&cam->mutex);
1277
1278         return rval;
1279 }
1280
1281 static int vidioc_g_parm(struct file *file, void *fh,
1282                          struct v4l2_streamparm *a) {
1283         struct omap24xxcam_fh *ofh = fh;
1284         struct omap24xxcam_device *cam = ofh->cam;
1285         int rval;
1286
1287         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1288                 return -EINVAL;
1289
1290         mutex_lock(&cam->mutex);
1291         rval = vidioc_int_g_parm(cam->sdev, a);
1292         mutex_unlock(&cam->mutex);
1293
1294         return rval;
1295 }
1296
1297 static int vidioc_s_parm(struct file *file, void *fh,
1298                          struct v4l2_streamparm *a)
1299 {
1300         struct omap24xxcam_fh *ofh = fh;
1301         struct omap24xxcam_device *cam = ofh->cam;
1302         struct v4l2_streamparm old_streamparm;
1303         int rval;
1304
1305         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1306                 return -EINVAL;
1307
1308         mutex_lock(&cam->mutex);
1309         if (cam->streaming) {
1310                 rval = -EBUSY;
1311                 goto out;
1312         }
1313
1314         old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1315         rval = vidioc_int_g_parm(cam->sdev, &old_streamparm);
1316         if (rval)
1317                 goto out;
1318
1319         rval = vidioc_int_s_parm(cam->sdev, a);
1320         if (rval)
1321                 goto out;
1322
1323         rval = omap24xxcam_sensor_if_enable(cam);
1324         /*
1325          * Revert to old streaming parameters if enabling sensor
1326          * interface with the new ones failed.
1327          */
1328         if (rval)
1329                 vidioc_int_s_parm(cam->sdev, &old_streamparm);
1330
1331 out:
1332         mutex_unlock(&cam->mutex);
1333
1334         return rval;
1335 }
1336
1337 /*
1338  *
1339  * File operations.
1340  *
1341  */
1342
1343 static unsigned int omap24xxcam_poll(struct file *file,
1344                                      struct poll_table_struct *wait)
1345 {
1346         struct omap24xxcam_fh *fh = file->private_data;
1347         struct omap24xxcam_device *cam = fh->cam;
1348         struct videobuf_buffer *vb;
1349
1350         mutex_lock(&cam->mutex);
1351         if (cam->streaming != file) {
1352                 mutex_unlock(&cam->mutex);
1353                 return POLLERR;
1354         }
1355         mutex_unlock(&cam->mutex);
1356
1357         mutex_lock(&fh->vbq.vb_lock);
1358         if (list_empty(&fh->vbq.stream)) {
1359                 mutex_unlock(&fh->vbq.vb_lock);
1360                 return POLLERR;
1361         }
1362         vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
1363         mutex_unlock(&fh->vbq.vb_lock);
1364
1365         poll_wait(file, &vb->done, wait);
1366
1367         if (vb->state == VIDEOBUF_DONE || vb->state == VIDEOBUF_ERROR)
1368                 return POLLIN | POLLRDNORM;
1369
1370         return 0;
1371 }
1372
1373 static int omap24xxcam_mmap_buffers(struct file *file,
1374                                     struct vm_area_struct *vma)
1375 {
1376         struct omap24xxcam_fh *fh = file->private_data;
1377         struct omap24xxcam_device *cam = fh->cam;
1378         struct videobuf_queue *vbq = &fh->vbq;
1379         unsigned int first, last, size, i, j;
1380         int err = 0;
1381
1382         mutex_lock(&cam->mutex);
1383         if (cam->streaming) {
1384                 mutex_unlock(&cam->mutex);
1385                 return -EBUSY;
1386         }
1387         mutex_unlock(&cam->mutex);
1388         mutex_lock(&vbq->vb_lock);
1389
1390         /* look for first buffer to map */
1391         for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1392                 if (NULL == vbq->bufs[first])
1393                         continue;
1394                 if (V4L2_MEMORY_MMAP != vbq->bufs[first]->memory)
1395                         continue;
1396                 if (vbq->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1397                         break;
1398         }
1399
1400         /* look for last buffer to map */
1401         for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1402                 if (NULL == vbq->bufs[last])
1403                         continue;
1404                 if (V4L2_MEMORY_MMAP != vbq->bufs[last]->memory)
1405                         continue;
1406                 size += vbq->bufs[last]->bsize;
1407                 if (size == (vma->vm_end - vma->vm_start))
1408                         break;
1409         }
1410
1411         size = 0;
1412         for (i = first; i <= last; i++) {
1413                 struct videobuf_dmabuf *dma = videobuf_to_dma(vbq->bufs[i]);
1414
1415                 for (j = 0; j < dma->sglen; j++) {
1416                         err = remap_pfn_range(
1417                                 vma, vma->vm_start + size,
1418                                 page_to_pfn(sg_page(&dma->sglist[j])),
1419                                 sg_dma_len(&dma->sglist[j]), vma->vm_page_prot);
1420                         if (err)
1421                                 goto out;
1422                         size += sg_dma_len(&dma->sglist[j]);
1423                 }
1424         }
1425
1426 out:
1427         mutex_unlock(&vbq->vb_lock);
1428
1429         return err;
1430 }
1431
1432 static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma)
1433 {
1434         struct omap24xxcam_fh *fh = file->private_data;
1435         int rval;
1436
1437         /* let the video-buf mapper check arguments and set-up structures */
1438         rval = videobuf_mmap_mapper(&fh->vbq, vma);
1439         if (rval)
1440                 return rval;
1441
1442         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1443
1444         /* do mapping to our allocated buffers */
1445         rval = omap24xxcam_mmap_buffers(file, vma);
1446         /*
1447          * In case of error, free vma->vm_private_data allocated by
1448          * videobuf_mmap_mapper.
1449          */
1450         if (rval)
1451                 kfree(vma->vm_private_data);
1452
1453         return rval;
1454 }
1455
1456 static int omap24xxcam_open(struct inode *inode, struct file *file)
1457 {
1458         int minor = iminor(inode);
1459         struct omap24xxcam_device *cam = omap24xxcam.priv;
1460         struct omap24xxcam_fh *fh;
1461         struct v4l2_format format;
1462
1463         if (!cam || !cam->vfd || (cam->vfd->minor != minor))
1464                 return -ENODEV;
1465
1466         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1467         if (fh == NULL)
1468                 return -ENOMEM;
1469
1470         mutex_lock(&cam->mutex);
1471         if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
1472                 mutex_unlock(&cam->mutex);
1473                 goto out_try_module_get;
1474         }
1475
1476         if (atomic_inc_return(&cam->users) == 1) {
1477                 omap24xxcam_hwinit(cam);
1478                 if (omap24xxcam_sensor_enable(cam)) {
1479                         mutex_unlock(&cam->mutex);
1480                         goto out_omap24xxcam_sensor_enable;
1481                 }
1482         }
1483         mutex_unlock(&cam->mutex);
1484
1485         fh->cam = cam;
1486         mutex_lock(&cam->mutex);
1487         vidioc_int_g_fmt_cap(cam->sdev, &format);
1488         mutex_unlock(&cam->mutex);
1489         /* FIXME: how about fh->pix when there are more users? */
1490         fh->pix = format.fmt.pix;
1491
1492         file->private_data = fh;
1493
1494         spin_lock_init(&fh->vbq_lock);
1495
1496         videobuf_queue_sg_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL,
1497                                 &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1498                                 V4L2_FIELD_NONE,
1499                                 sizeof(struct videobuf_buffer), fh);
1500
1501         return 0;
1502
1503 out_omap24xxcam_sensor_enable:
1504         omap24xxcam_poweron_reset(cam);
1505         module_put(cam->sdev->module);
1506
1507 out_try_module_get:
1508         kfree(fh);
1509
1510         return -ENODEV;
1511 }
1512
1513 static int omap24xxcam_release(struct inode *inode, struct file *file)
1514 {
1515         struct omap24xxcam_fh *fh = file->private_data;
1516         struct omap24xxcam_device *cam = fh->cam;
1517
1518         atomic_inc(&cam->reset_disable);
1519
1520         flush_scheduled_work();
1521
1522         /* stop streaming capture */
1523         videobuf_streamoff(&fh->vbq);
1524
1525         mutex_lock(&cam->mutex);
1526         if (cam->streaming == file) {
1527                 cam->streaming = NULL;
1528                 mutex_unlock(&cam->mutex);
1529                 sysfs_notify(&cam->dev->kobj, NULL, "streaming");
1530         } else {
1531                 mutex_unlock(&cam->mutex);
1532         }
1533
1534         atomic_dec(&cam->reset_disable);
1535
1536         omap24xxcam_vbq_free_mmap_buffers(&fh->vbq);
1537
1538         /*
1539          * Make sure the reset work we might have scheduled is not
1540          * pending! It may be run *only* if we have users. (And it may
1541          * not be scheduled anymore since streaming is already
1542          * disabled.)
1543          */
1544         flush_scheduled_work();
1545
1546         mutex_lock(&cam->mutex);
1547         if (atomic_dec_return(&cam->users) == 0) {
1548                 omap24xxcam_sensor_disable(cam);
1549                 omap24xxcam_poweron_reset(cam);
1550         }
1551         mutex_unlock(&cam->mutex);
1552
1553         file->private_data = NULL;
1554
1555         module_put(cam->sdev->module);
1556         kfree(fh);
1557
1558         return 0;
1559 }
1560
1561 static struct file_operations omap24xxcam_fops = {
1562         .owner   = THIS_MODULE,
1563         .llseek  = no_llseek,
1564         .ioctl   = video_ioctl2,
1565         .poll    = omap24xxcam_poll,
1566         .mmap    = omap24xxcam_mmap,
1567         .open    = omap24xxcam_open,
1568         .release = omap24xxcam_release,
1569 };
1570
1571 /*
1572  *
1573  * Power management.
1574  *
1575  */
1576
1577 #ifdef CONFIG_PM
1578 static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state)
1579 {
1580         struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1581
1582         if (atomic_read(&cam->users) == 0)
1583                 return 0;
1584
1585         if (!atomic_read(&cam->reset_disable))
1586                 omap24xxcam_capture_stop(cam);
1587
1588         omap24xxcam_sensor_disable(cam);
1589         omap24xxcam_poweron_reset(cam);
1590
1591         return 0;
1592 }
1593
1594 static int omap24xxcam_resume(struct platform_device *pdev)
1595 {
1596         struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1597
1598         if (atomic_read(&cam->users) == 0)
1599                 return 0;
1600
1601         omap24xxcam_hwinit(cam);
1602         omap24xxcam_sensor_enable(cam);
1603
1604         if (!atomic_read(&cam->reset_disable))
1605                 omap24xxcam_capture_cont(cam);
1606
1607         return 0;
1608 }
1609 #endif /* CONFIG_PM */
1610
1611 /*
1612  *
1613  * Camera device (i.e. /dev/video).
1614  *
1615  */
1616
1617 static int omap24xxcam_device_register(struct v4l2_int_device *s)
1618 {
1619         struct omap24xxcam_device *cam = s->u.slave->master->priv;
1620         struct video_device *vfd;
1621         int rval;
1622
1623         /* We already have a slave. */
1624         if (cam->sdev)
1625                 return -EBUSY;
1626
1627         cam->sdev = s;
1628
1629         if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
1630                 dev_err(cam->dev, "could not register sysfs entry\n");
1631                 rval = -EBUSY;
1632                 goto err;
1633         }
1634
1635         /* initialize the video_device struct */
1636         vfd = cam->vfd = video_device_alloc();
1637         if (!vfd) {
1638                 dev_err(cam->dev, "could not allocate video device struct\n");
1639                 rval = -ENOMEM;
1640                 goto err;
1641         }
1642         vfd->release = video_device_release;
1643
1644         vfd->dev = cam->dev;
1645
1646         strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1647         vfd->type                = VID_TYPE_CAPTURE | VID_TYPE_CHROMAKEY;
1648         vfd->fops                = &omap24xxcam_fops;
1649         vfd->priv                = cam;
1650         vfd->minor               = -1;
1651
1652         vfd->vidioc_querycap     = vidioc_querycap;
1653         vfd->vidioc_enum_fmt_cap = vidioc_enum_fmt_cap;
1654         vfd->vidioc_g_fmt_cap    = vidioc_g_fmt_cap;
1655         vfd->vidioc_s_fmt_cap    = vidioc_s_fmt_cap;
1656         vfd->vidioc_try_fmt_cap  = vidioc_try_fmt_cap;
1657         vfd->vidioc_reqbufs      = vidioc_reqbufs;
1658         vfd->vidioc_querybuf     = vidioc_querybuf;
1659         vfd->vidioc_qbuf         = vidioc_qbuf;
1660         vfd->vidioc_dqbuf        = vidioc_dqbuf;
1661         vfd->vidioc_streamon     = vidioc_streamon;
1662         vfd->vidioc_streamoff    = vidioc_streamoff;
1663         vfd->vidioc_enum_input   = vidioc_enum_input;
1664         vfd->vidioc_g_input      = vidioc_g_input;
1665         vfd->vidioc_s_input      = vidioc_s_input;
1666         vfd->vidioc_queryctrl    = vidioc_queryctrl;
1667         vfd->vidioc_g_ctrl       = vidioc_g_ctrl;
1668         vfd->vidioc_s_ctrl       = vidioc_s_ctrl;
1669         vfd->vidioc_g_parm       = vidioc_g_parm;
1670         vfd->vidioc_s_parm       = vidioc_s_parm;
1671
1672         omap24xxcam_hwinit(cam);
1673
1674         rval = omap24xxcam_sensor_init(cam);
1675         if (rval)
1676                 goto err;
1677
1678         if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1679                 dev_err(cam->dev, "could not register V4L device\n");
1680                 vfd->minor = -1;
1681                 rval = -EBUSY;
1682                 goto err;
1683         }
1684
1685         omap24xxcam_poweron_reset(cam);
1686
1687         dev_info(cam->dev, "registered device video%d\n", vfd->minor);
1688
1689         return 0;
1690
1691 err:
1692         omap24xxcam_device_unregister(s);
1693
1694         return rval;
1695 }
1696
1697 static void omap24xxcam_device_unregister(struct v4l2_int_device *s)
1698 {
1699         struct omap24xxcam_device *cam = s->u.slave->master->priv;
1700
1701         omap24xxcam_sensor_exit(cam);
1702
1703         if (cam->vfd) {
1704                 if (cam->vfd->minor == -1) {
1705                         /*
1706                          * The device was never registered, so release the
1707                          * video_device struct directly.
1708                          */
1709                         video_device_release(cam->vfd);
1710                 } else {
1711                         /*
1712                          * The unregister function will release the
1713                          * video_device struct as well as
1714                          * unregistering it.
1715                          */
1716                         video_unregister_device(cam->vfd);
1717                 }
1718                 cam->vfd = NULL;
1719         }
1720
1721         device_remove_file(cam->dev, &dev_attr_streaming);
1722
1723         cam->sdev = NULL;
1724 }
1725
1726 static struct v4l2_int_master omap24xxcam_master = {
1727         .attach = omap24xxcam_device_register,
1728         .detach = omap24xxcam_device_unregister,
1729 };
1730
1731 static struct v4l2_int_device omap24xxcam = {
1732         .module = THIS_MODULE,
1733         .name   = CAM_NAME,
1734         .type   = v4l2_int_type_master,
1735         .u      = {
1736                 .master = &omap24xxcam_master
1737         },
1738 };
1739
1740 /*
1741  *
1742  * Driver initialisation and deinitialisation.
1743  *
1744  */
1745
1746 static int __init omap24xxcam_probe(struct platform_device *pdev)
1747 {
1748         struct omap24xxcam_device *cam;
1749         struct resource *mem;
1750         int irq;
1751
1752         cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1753         if (!cam) {
1754                 dev_err(&pdev->dev, "could not allocate memory\n");
1755                 goto err;
1756         }
1757
1758         platform_set_drvdata(pdev, cam);
1759
1760         cam->dev = &pdev->dev;
1761
1762         /*
1763          * Impose a lower limit on the amount of memory allocated for
1764          * capture. We require at least enough memory to double-buffer
1765          * QVGA (300KB).
1766          */
1767         if (capture_mem < 320 * 240 * 2 * 2)
1768                 capture_mem = 320 * 240 * 2 * 2;
1769         cam->capture_mem = capture_mem;
1770
1771         /* request the mem region for the camera registers */
1772         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1773         if (!mem) {
1774                 dev_err(cam->dev, "no mem resource?\n");
1775                 goto err;
1776         }
1777         if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
1778                                 pdev->name)) {
1779                 dev_err(cam->dev,
1780                         "cannot reserve camera register I/O region\n");
1781                 goto err;
1782         }
1783         cam->mmio_base_phys = mem->start;
1784         cam->mmio_size = (mem->end - mem->start) + 1;
1785
1786         /* map the region */
1787         cam->mmio_base = (unsigned long)
1788                 ioremap_nocache(cam->mmio_base_phys, cam->mmio_size);
1789         if (!cam->mmio_base) {
1790                 dev_err(cam->dev, "cannot map camera register I/O region\n");
1791                 goto err;
1792         }
1793
1794         irq = platform_get_irq(pdev, 0);
1795         if (irq <= 0) {
1796                 dev_err(cam->dev, "no irq for camera?\n");
1797                 goto err;
1798         }
1799
1800         /* install the interrupt service routine */
1801         if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) {
1802                 dev_err(cam->dev,
1803                         "could not install interrupt service routine\n");
1804                 goto err;
1805         }
1806         cam->irq = irq;
1807
1808         if (omap24xxcam_clock_get(cam))
1809                 goto err;
1810
1811         INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work);
1812
1813         mutex_init(&cam->mutex);
1814         spin_lock_init(&cam->core_enable_disable_lock);
1815
1816         omap24xxcam_sgdma_init(&cam->sgdma,
1817                                cam->mmio_base + CAMDMA_REG_OFFSET,
1818                                omap24xxcam_stalled_dma_reset,
1819                                (unsigned long)cam);
1820
1821         omap24xxcam.priv = cam;
1822
1823         if (v4l2_int_device_register(&omap24xxcam))
1824                 goto err;
1825
1826         return 0;
1827
1828 err:
1829         omap24xxcam_remove(pdev);
1830         return -ENODEV;
1831 }
1832
1833 static int omap24xxcam_remove(struct platform_device *pdev)
1834 {
1835         struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
1836
1837         if (!cam)
1838                 return 0;
1839
1840         if (omap24xxcam.priv != NULL)
1841                 v4l2_int_device_unregister(&omap24xxcam);
1842         omap24xxcam.priv = NULL;
1843
1844         omap24xxcam_clock_put(cam);
1845
1846         if (cam->irq) {
1847                 free_irq(cam->irq, cam);
1848                 cam->irq = 0;
1849         }
1850
1851         if (cam->mmio_base) {
1852                 iounmap((void *)cam->mmio_base);
1853                 cam->mmio_base = 0;
1854         }
1855
1856         if (cam->mmio_base_phys) {
1857                 release_mem_region(cam->mmio_base_phys, cam->mmio_size);
1858                 cam->mmio_base_phys = 0;
1859         }
1860
1861         kfree(cam);
1862
1863         return 0;
1864 }
1865
1866 static struct platform_driver omap24xxcam_driver = {
1867         .probe   = omap24xxcam_probe,
1868         .remove  = omap24xxcam_remove,
1869 #ifdef CONFIG_PM
1870         .suspend = omap24xxcam_suspend,
1871         .resume  = omap24xxcam_resume,
1872 #endif
1873         .driver  = {
1874                 .name = CAM_NAME,
1875         },
1876 };
1877
1878 /*
1879  *
1880  * Module initialisation and deinitialisation
1881  *
1882  */
1883
1884 static int __init omap24xxcam_init(void)
1885 {
1886         return platform_driver_register(&omap24xxcam_driver);
1887 }
1888
1889 static void __exit omap24xxcam_cleanup(void)
1890 {
1891         platform_driver_unregister(&omap24xxcam_driver);
1892 }
1893
1894 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
1895 MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
1896 MODULE_LICENSE("GPL");
1897 module_param(video_nr, int, 0);
1898 MODULE_PARM_DESC(video_nr,
1899                  "Minor number for video device (-1 ==> auto assign)");
1900 module_param(capture_mem, int, 0);
1901 MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture "
1902                  "buffers (default 4800kiB)");
1903
1904 module_init(omap24xxcam_init);
1905 module_exit(omap24xxcam_cleanup);