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