2 * V4L2 Driver for PXA camera host
4 * Copyright (C) 2006, Sascha Hauer, Pengutronix
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/init.h>
14 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
23 #include <linux/moduleparam.h>
24 #include <linux/time.h>
25 #include <linux/version.h>
26 #include <linux/device.h>
27 #include <linux/platform_device.h>
28 #include <linux/mutex.h>
29 #include <linux/clk.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-dev.h>
33 #include <media/videobuf-dma-sg.h>
34 #include <media/soc_camera.h>
36 #include <linux/videodev2.h>
39 #include <asm/arch/pxa-regs.h>
40 #include <asm/arch/camera.h>
42 #define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
43 #define PXA_CAM_DRV_NAME "pxa27x-camera"
45 #define CICR0_SIM_MP (0 << 24)
46 #define CICR0_SIM_SP (1 << 24)
47 #define CICR0_SIM_MS (2 << 24)
48 #define CICR0_SIM_EP (3 << 24)
49 #define CICR0_SIM_ES (4 << 24)
51 #define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
52 #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
53 #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
54 #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
55 #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
57 #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
58 #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
59 #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
60 #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
61 #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
63 #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
64 #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
65 #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
66 #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
68 #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
69 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
70 CICR0_EOFM | CICR0_FOM)
72 static DEFINE_MUTEX(camera_lock);
77 enum pxa_camera_active_dma {
83 /* descriptor needed for the PXA DMA engine */
86 struct pxa_dma_desc *sg_cpu;
91 /* buffer for one video frame */
93 /* common v4l buffer stuff -- must be first */
94 struct videobuf_buffer vb;
96 const struct soc_camera_data_format *fmt;
98 /* our descriptor lists for Y, U and V channels */
99 struct pxa_cam_dma dmas[3];
103 enum pxa_camera_active_dma active_dma;
106 struct pxa_camera_dev {
108 /* PXA27x is only supposed to handle one camera on its Quick Capture
109 * interface. If anyone ever builds hardware to enable more than
110 * one camera, they will have to modify this driver too */
111 struct soc_camera_device *icd;
118 unsigned int dma_chans[3];
120 struct pxacamera_platform_data *pdata;
121 struct resource *res;
122 unsigned long platform_flags;
123 unsigned long platform_mclk_10khz;
125 struct list_head capture;
129 struct pxa_buffer *active;
130 struct pxa_dma_desc *sg_tail[3];
133 static const char *pxa_cam_driver_description = "PXA_Camera";
135 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
138 * Videobuf operations
140 static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
143 struct soc_camera_device *icd = vq->priv_data;
144 struct soc_camera_host *ici =
145 to_soc_camera_host(icd->dev.parent);
146 struct pxa_camera_dev *pcdev = ici->priv;
148 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
150 /* planar capture requires Y, U and V buffers to be page aligned */
151 if (pcdev->channels == 3) {
152 *size = PAGE_ALIGN(icd->width * icd->height); /* Y pages */
153 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* U pages */
154 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* V pages */
156 *size = icd->width * icd->height *
157 ((icd->current_fmt->depth + 7) >> 3);
162 while (*size * *count > vid_limit * 1024 * 1024)
168 static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
170 struct soc_camera_device *icd = vq->priv_data;
171 struct soc_camera_host *ici =
172 to_soc_camera_host(icd->dev.parent);
173 struct pxa_camera_dev *pcdev = ici->priv;
174 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
177 BUG_ON(in_interrupt());
179 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
180 &buf->vb, buf->vb.baddr, buf->vb.bsize);
182 /* This waits until this buffer is out of danger, i.e., until it is no
183 * longer in STATE_QUEUED or STATE_ACTIVE */
184 videobuf_waiton(&buf->vb, 0, 0);
185 videobuf_dma_unmap(vq, dma);
186 videobuf_dma_free(dma);
188 for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
189 if (buf->dmas[i].sg_cpu)
190 dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
192 buf->dmas[i].sg_dma);
193 buf->dmas[i].sg_cpu = NULL;
196 buf->vb.state = VIDEOBUF_NEEDS_INIT;
199 static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
200 struct pxa_buffer *buf,
201 struct videobuf_dmabuf *dma, int channel,
202 int sglen, int sg_start, int cibr,
205 struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
209 dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
210 pxa_dma->sg_cpu, pxa_dma->sg_dma);
212 pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
213 pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
214 &pxa_dma->sg_dma, GFP_KERNEL);
215 if (!pxa_dma->sg_cpu)
218 pxa_dma->sglen = sglen;
220 for (i = 0; i < sglen; i++) {
221 int sg_i = sg_start + i;
222 struct scatterlist *sg = dma->sglist;
223 unsigned int dma_len = sg_dma_len(&sg[sg_i]), xfer_len;
225 pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
226 pxa_dma->sg_cpu[i].dtadr = sg_dma_address(&sg[sg_i]);
228 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
229 xfer_len = (min(dma_len, size) + 7) & ~7;
231 pxa_dma->sg_cpu[i].dcmd =
232 DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
234 pxa_dma->sg_cpu[i].ddadr =
235 pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
238 pxa_dma->sg_cpu[sglen - 1].ddadr = DDADR_STOP;
239 pxa_dma->sg_cpu[sglen - 1].dcmd |= DCMD_ENDIRQEN;
244 static int pxa_videobuf_prepare(struct videobuf_queue *vq,
245 struct videobuf_buffer *vb, enum v4l2_field field)
247 struct soc_camera_device *icd = vq->priv_data;
248 struct soc_camera_host *ici =
249 to_soc_camera_host(icd->dev.parent);
250 struct pxa_camera_dev *pcdev = ici->priv;
251 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
253 int sglen_y, sglen_yu = 0, sglen_u = 0, sglen_v = 0;
254 int size_y, size_u = 0, size_v = 0;
256 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
257 vb, vb->baddr, vb->bsize);
259 /* Added list head initialization on alloc */
260 WARN_ON(!list_empty(&vb->queue));
263 /* This can be useful if you want to see if we actually fill
264 * the buffer with something */
265 memset((void *)vb->baddr, 0xaa, vb->bsize);
268 BUG_ON(NULL == icd->current_fmt);
270 /* I think, in buf_prepare you only have to protect global data,
271 * the actual buffer is yours */
274 if (buf->fmt != icd->current_fmt ||
275 vb->width != icd->width ||
276 vb->height != icd->height ||
277 vb->field != field) {
278 buf->fmt = icd->current_fmt;
279 vb->width = icd->width;
280 vb->height = icd->height;
282 vb->state = VIDEOBUF_NEEDS_INIT;
285 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
286 if (0 != vb->baddr && vb->bsize < vb->size) {
291 if (vb->state == VIDEOBUF_NEEDS_INIT) {
292 unsigned int size = vb->size;
293 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
295 ret = videobuf_iolock(vq, vb, NULL);
299 if (pcdev->channels == 3) {
300 /* FIXME the calculations should be more precise */
301 sglen_y = dma->sglen / 2;
302 sglen_u = sglen_v = dma->sglen / 4 + 1;
303 sglen_yu = sglen_y + sglen_u;
305 size_u = size_v = size / 4;
307 sglen_y = dma->sglen;
311 /* init DMA for Y channel */
312 ret = pxa_init_dma_channel(pcdev, buf, dma, 0, sglen_y,
317 "DMA initialization for Y/RGB failed\n");
321 if (pcdev->channels == 3) {
322 /* init DMA for U channel */
323 ret = pxa_init_dma_channel(pcdev, buf, dma, 1, sglen_u,
324 sglen_y, 0x30, size_u);
327 "DMA initialization for U failed\n");
331 /* init DMA for V channel */
332 ret = pxa_init_dma_channel(pcdev, buf, dma, 2, sglen_v,
333 sglen_yu, 0x38, size_v);
336 "DMA initialization for V failed\n");
341 vb->state = VIDEOBUF_PREPARED;
345 buf->active_dma = DMA_Y;
346 if (pcdev->channels == 3)
347 buf->active_dma |= DMA_U | DMA_V;
352 dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
353 buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
355 dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
356 buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
358 free_buffer(vq, buf);
364 static void pxa_videobuf_queue(struct videobuf_queue *vq,
365 struct videobuf_buffer *vb)
367 struct soc_camera_device *icd = vq->priv_data;
368 struct soc_camera_host *ici =
369 to_soc_camera_host(icd->dev.parent);
370 struct pxa_camera_dev *pcdev = ici->priv;
371 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
372 struct pxa_buffer *active;
376 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
377 vb, vb->baddr, vb->bsize);
378 spin_lock_irqsave(&pcdev->lock, flags);
380 list_add_tail(&vb->queue, &pcdev->capture);
382 vb->state = VIDEOBUF_ACTIVE;
383 active = pcdev->active;
386 CIFR |= CIFR_RESET_F;
388 for (i = 0; i < pcdev->channels; i++) {
389 DDADR(pcdev->dma_chans[i]) = buf->dmas[i].sg_dma;
390 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
391 pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen - 1;
397 struct pxa_cam_dma *buf_dma;
398 struct pxa_cam_dma *act_dma;
401 for (i = 0; i < pcdev->channels; i++) {
402 buf_dma = &buf->dmas[i];
403 act_dma = &active->dmas[i];
404 nents = buf_dma->sglen;
406 /* Stop DMA engine */
407 DCSR(pcdev->dma_chans[i]) = 0;
409 /* Add the descriptors we just initialized to
410 the currently running chain */
411 pcdev->sg_tail[i]->ddadr = buf_dma->sg_dma;
412 pcdev->sg_tail[i] = buf_dma->sg_cpu + buf_dma->sglen - 1;
414 /* Setup a dummy descriptor with the DMA engines current
417 buf_dma->sg_cpu[nents].dsadr =
418 pcdev->res->start + 0x28 + i*8; /* CIBRx */
419 buf_dma->sg_cpu[nents].dtadr =
420 DTADR(pcdev->dma_chans[i]);
421 buf_dma->sg_cpu[nents].dcmd =
422 DCMD(pcdev->dma_chans[i]);
424 if (DDADR(pcdev->dma_chans[i]) == DDADR_STOP) {
425 /* The DMA engine is on the last
426 descriptor, set the next descriptors
427 address to the descriptors we just
429 buf_dma->sg_cpu[nents].ddadr = buf_dma->sg_dma;
431 buf_dma->sg_cpu[nents].ddadr =
432 DDADR(pcdev->dma_chans[i]);
435 /* The next descriptor is the dummy descriptor */
436 DDADR(pcdev->dma_chans[i]) = buf_dma->sg_dma + nents *
437 sizeof(struct pxa_dma_desc);
439 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
443 spin_unlock_irqrestore(&pcdev->lock, flags);
446 static void pxa_videobuf_release(struct videobuf_queue *vq,
447 struct videobuf_buffer *vb)
449 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
451 struct soc_camera_device *icd = vq->priv_data;
453 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
454 vb, vb->baddr, vb->bsize);
457 case VIDEOBUF_ACTIVE:
458 dev_dbg(&icd->dev, "%s (active)\n", __func__);
460 case VIDEOBUF_QUEUED:
461 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
463 case VIDEOBUF_PREPARED:
464 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
467 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
472 free_buffer(vq, buf);
475 static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
476 struct videobuf_buffer *vb,
477 struct pxa_buffer *buf)
479 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
480 list_del_init(&vb->queue);
481 vb->state = VIDEOBUF_DONE;
482 do_gettimeofday(&vb->ts);
486 if (list_empty(&pcdev->capture)) {
487 pcdev->active = NULL;
488 DCSR(pcdev->dma_chans[0]) = 0;
489 DCSR(pcdev->dma_chans[1]) = 0;
490 DCSR(pcdev->dma_chans[2]) = 0;
495 pcdev->active = list_entry(pcdev->capture.next,
496 struct pxa_buffer, vb.queue);
499 static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
500 enum pxa_camera_active_dma act_dma)
502 struct pxa_buffer *buf;
504 u32 status, camera_status, overrun;
505 struct videobuf_buffer *vb;
507 spin_lock_irqsave(&pcdev->lock, flags);
509 status = DCSR(channel);
510 DCSR(channel) = status | DCSR_ENDINTR;
512 if (status & DCSR_BUSERR) {
513 dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
517 if (!(status & DCSR_ENDINTR)) {
518 dev_err(pcdev->dev, "Unknown DMA IRQ source, "
519 "status: 0x%08x\n", status);
523 if (!pcdev->active) {
524 dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
528 camera_status = CISR;
529 overrun = CISR_IFO_0;
530 if (pcdev->channels == 3)
531 overrun |= CISR_IFO_1 | CISR_IFO_2;
532 if (camera_status & overrun) {
533 dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n", camera_status);
534 /* Stop the Capture Interface */
538 /* Reset the FIFOs */
539 CIFR |= CIFR_RESET_F;
540 /* Enable End-Of-Frame Interrupt */
541 CICR0 &= ~CICR0_EOFM;
542 /* Restart the Capture Interface */
547 vb = &pcdev->active->vb;
548 buf = container_of(vb, struct pxa_buffer, vb);
549 WARN_ON(buf->inwork || list_empty(&vb->queue));
550 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
551 vb, vb->baddr, vb->bsize);
553 buf->active_dma &= ~act_dma;
554 if (!buf->active_dma)
555 pxa_camera_wakeup(pcdev, vb, buf);
558 spin_unlock_irqrestore(&pcdev->lock, flags);
561 static void pxa_camera_dma_irq_y(int channel, void *data)
563 struct pxa_camera_dev *pcdev = data;
564 pxa_camera_dma_irq(channel, pcdev, DMA_Y);
567 static void pxa_camera_dma_irq_u(int channel, void *data)
569 struct pxa_camera_dev *pcdev = data;
570 pxa_camera_dma_irq(channel, pcdev, DMA_U);
573 static void pxa_camera_dma_irq_v(int channel, void *data)
575 struct pxa_camera_dev *pcdev = data;
576 pxa_camera_dma_irq(channel, pcdev, DMA_V);
579 static struct videobuf_queue_ops pxa_videobuf_ops = {
580 .buf_setup = pxa_videobuf_setup,
581 .buf_prepare = pxa_videobuf_prepare,
582 .buf_queue = pxa_videobuf_queue,
583 .buf_release = pxa_videobuf_release,
586 static void pxa_camera_init_videobuf(struct videobuf_queue *q,
587 struct soc_camera_device *icd)
589 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
590 struct pxa_camera_dev *pcdev = ici->priv;
592 /* We must pass NULL as dev pointer, then all pci_* dma operations
593 * transform to normal dma_* ones. */
594 videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
595 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
596 sizeof(struct pxa_buffer), icd);
599 static int mclk_get_divisor(struct pxa_camera_dev *pcdev)
601 unsigned int mclk_10khz = pcdev->platform_mclk_10khz;
603 unsigned long lcdclk;
605 lcdclk = clk_get_rate(pcdev->clk) / 10000;
607 /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
608 * they get a nice Oops */
609 div = (lcdclk + 2 * mclk_10khz - 1) / (2 * mclk_10khz) - 1;
611 dev_dbg(pcdev->dev, "LCD clock %lukHz, target freq %dkHz, "
612 "divisor %lu\n", lcdclk * 10, mclk_10khz * 10, div);
617 static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
619 struct pxacamera_platform_data *pdata = pcdev->pdata;
622 dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
625 if (pdata && pdata->init) {
626 dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
627 pdata->init(pcdev->dev);
630 if (pdata && pdata->power) {
631 dev_dbg(pcdev->dev, "%s: Power on camera\n", __func__);
632 pdata->power(pcdev->dev, 1);
635 if (pdata && pdata->reset) {
636 dev_dbg(pcdev->dev, "%s: Releasing camera reset\n",
638 pdata->reset(pcdev->dev, 1);
641 CICR0 = 0x3FF; /* disable all interrupts */
643 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
644 cicr4 |= CICR4_PCLK_EN;
645 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
646 cicr4 |= CICR4_MCLK_EN;
647 if (pcdev->platform_flags & PXA_CAMERA_PCP)
649 if (pcdev->platform_flags & PXA_CAMERA_HSP)
651 if (pcdev->platform_flags & PXA_CAMERA_VSP)
654 CICR4 = mclk_get_divisor(pcdev) | cicr4;
656 clk_enable(pcdev->clk);
659 static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
661 struct pxacamera_platform_data *board = pcdev->pdata;
663 clk_disable(pcdev->clk);
665 if (board && board->reset) {
666 dev_dbg(pcdev->dev, "%s: Asserting camera reset\n",
668 board->reset(pcdev->dev, 0);
671 if (board && board->power) {
672 dev_dbg(pcdev->dev, "%s: Power off camera\n", __func__);
673 board->power(pcdev->dev, 0);
677 static irqreturn_t pxa_camera_irq(int irq, void *data)
679 struct pxa_camera_dev *pcdev = data;
680 unsigned int status = CISR;
682 dev_dbg(pcdev->dev, "Camera interrupt status 0x%x\n", status);
689 if (status & CISR_EOF) {
691 for (i = 0; i < pcdev->channels; i++) {
692 DDADR(pcdev->dma_chans[i]) =
693 pcdev->active->dmas[i].sg_dma;
694 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
702 /* The following two functions absolutely depend on the fact, that
703 * there can be only one camera on PXA quick capture interface */
704 static int pxa_camera_add_device(struct soc_camera_device *icd)
706 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
707 struct pxa_camera_dev *pcdev = ici->priv;
710 mutex_lock(&camera_lock);
717 dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
720 pxa_camera_activate(pcdev);
721 ret = icd->ops->init(icd);
727 mutex_unlock(&camera_lock);
732 static void pxa_camera_remove_device(struct soc_camera_device *icd)
734 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
735 struct pxa_camera_dev *pcdev = ici->priv;
737 BUG_ON(icd != pcdev->icd);
739 dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
742 /* disable capture, disable interrupts */
745 /* Stop DMA engine */
746 DCSR(pcdev->dma_chans[0]) = 0;
747 DCSR(pcdev->dma_chans[1]) = 0;
748 DCSR(pcdev->dma_chans[2]) = 0;
750 icd->ops->release(icd);
752 pxa_camera_deactivate(pcdev);
757 static int test_platform_param(struct pxa_camera_dev *pcdev,
758 unsigned char buswidth, unsigned long *flags)
761 * Platform specified synchronization and pixel clock polarities are
762 * only a recommendation and are only used during probing. The PXA270
763 * quick capture interface supports both.
765 *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
766 SOCAM_MASTER : SOCAM_SLAVE) |
767 SOCAM_HSYNC_ACTIVE_HIGH |
768 SOCAM_HSYNC_ACTIVE_LOW |
769 SOCAM_VSYNC_ACTIVE_HIGH |
770 SOCAM_VSYNC_ACTIVE_LOW |
771 SOCAM_PCLK_SAMPLE_RISING |
772 SOCAM_PCLK_SAMPLE_FALLING;
774 /* If requested data width is supported by the platform, use it */
777 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
779 *flags |= SOCAM_DATAWIDTH_10;
782 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
784 *flags |= SOCAM_DATAWIDTH_9;
787 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
789 *flags |= SOCAM_DATAWIDTH_8;
795 static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
797 struct soc_camera_host *ici =
798 to_soc_camera_host(icd->dev.parent);
799 struct pxa_camera_dev *pcdev = ici->priv;
800 unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
801 u32 cicr0, cicr1, cicr4 = 0;
802 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
807 camera_flags = icd->ops->query_bus_param(icd);
809 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
815 /* Make choises, based on platform preferences */
816 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
817 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
818 if (pcdev->platform_flags & PXA_CAMERA_HSP)
819 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
821 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
824 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
825 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
826 if (pcdev->platform_flags & PXA_CAMERA_VSP)
827 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
829 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
832 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
833 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
834 if (pcdev->platform_flags & PXA_CAMERA_PCP)
835 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
837 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
840 ret = icd->ops->set_bus_param(icd, common_flags);
844 /* Datawidth is now guaranteed to be equal to one of the three values.
845 * We fix bit-per-pixel equal to data-width... */
846 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
847 case SOCAM_DATAWIDTH_10:
852 case SOCAM_DATAWIDTH_9:
858 /* Actually it can only be 8 now,
859 * default is just to silence compiler warnings */
860 case SOCAM_DATAWIDTH_8:
866 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
867 cicr4 |= CICR4_PCLK_EN;
868 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
869 cicr4 |= CICR4_MCLK_EN;
870 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
872 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
874 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
878 if (cicr0 & CICR0_ENB)
879 CICR0 = cicr0 & ~CICR0_ENB;
881 cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
884 case V4L2_PIX_FMT_YUV422P:
886 cicr1 |= CICR1_YCBCR_F;
887 case V4L2_PIX_FMT_YUYV:
888 cicr1 |= CICR1_COLOR_SP_VAL(2);
890 case V4L2_PIX_FMT_RGB555:
891 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
892 CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
894 case V4L2_PIX_FMT_RGB565:
895 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
901 CICR3 = CICR3_LPF_VAL(icd->height - 1) |
902 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
903 CICR4 = mclk_get_divisor(pcdev) | cicr4;
905 /* CIF interrupts are not used, only DMA */
906 CICR0 = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
907 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP)) |
908 CICR0_DMAEN | CICR0_IRQ_MASK | (cicr0 & CICR0_ENB);
913 static int pxa_camera_try_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
915 struct soc_camera_host *ici =
916 to_soc_camera_host(icd->dev.parent);
917 struct pxa_camera_dev *pcdev = ici->priv;
918 unsigned long bus_flags, camera_flags;
919 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
924 camera_flags = icd->ops->query_bus_param(icd);
926 return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
929 static int pxa_camera_set_fmt_cap(struct soc_camera_device *icd,
930 __u32 pixfmt, struct v4l2_rect *rect)
932 return icd->ops->set_fmt_cap(icd, pixfmt, rect);
935 static int pxa_camera_try_fmt_cap(struct soc_camera_device *icd,
936 struct v4l2_format *f)
938 /* limit to pxa hardware capabilities */
939 if (f->fmt.pix.height < 32)
940 f->fmt.pix.height = 32;
941 if (f->fmt.pix.height > 2048)
942 f->fmt.pix.height = 2048;
943 if (f->fmt.pix.width < 48)
944 f->fmt.pix.width = 48;
945 if (f->fmt.pix.width > 2048)
946 f->fmt.pix.width = 2048;
947 f->fmt.pix.width &= ~0x01;
949 /* limit to sensor capabilities */
950 return icd->ops->try_fmt_cap(icd, f);
953 static int pxa_camera_reqbufs(struct soc_camera_file *icf,
954 struct v4l2_requestbuffers *p)
958 /* This is for locking debugging only. I removed spinlocks and now I
959 * check whether .prepare is ever called on a linked buffer, or whether
960 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
961 * it hadn't triggered */
962 for (i = 0; i < p->count; i++) {
963 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
964 struct pxa_buffer, vb);
966 INIT_LIST_HEAD(&buf->vb.queue);
972 static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
974 struct soc_camera_file *icf = file->private_data;
975 struct pxa_buffer *buf;
977 buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
980 poll_wait(file, &buf->vb.done, pt);
982 if (buf->vb.state == VIDEOBUF_DONE ||
983 buf->vb.state == VIDEOBUF_ERROR)
984 return POLLIN|POLLRDNORM;
989 static int pxa_camera_querycap(struct soc_camera_host *ici,
990 struct v4l2_capability *cap)
992 /* cap->name is set by the firendly caller:-> */
993 strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
994 cap->version = PXA_CAM_VERSION_CODE;
995 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1000 static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
1001 .owner = THIS_MODULE,
1002 .add = pxa_camera_add_device,
1003 .remove = pxa_camera_remove_device,
1004 .set_fmt_cap = pxa_camera_set_fmt_cap,
1005 .try_fmt_cap = pxa_camera_try_fmt_cap,
1006 .init_videobuf = pxa_camera_init_videobuf,
1007 .reqbufs = pxa_camera_reqbufs,
1008 .poll = pxa_camera_poll,
1009 .querycap = pxa_camera_querycap,
1010 .try_bus_param = pxa_camera_try_bus_param,
1011 .set_bus_param = pxa_camera_set_bus_param,
1014 /* Should be allocated dynamically too, but we have only one. */
1015 static struct soc_camera_host pxa_soc_camera_host = {
1016 .drv_name = PXA_CAM_DRV_NAME,
1017 .ops = &pxa_soc_camera_host_ops,
1020 static int pxa_camera_probe(struct platform_device *pdev)
1022 struct pxa_camera_dev *pcdev;
1023 struct resource *res;
1028 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1029 irq = platform_get_irq(pdev, 0);
1030 if (!res || irq < 0) {
1035 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1037 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1042 pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
1043 if (IS_ERR(pcdev->clk)) {
1044 err = PTR_ERR(pcdev->clk);
1048 dev_set_drvdata(&pdev->dev, pcdev);
1051 pcdev->pdata = pdev->dev.platform_data;
1052 pcdev->platform_flags = pcdev->pdata->flags;
1053 if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
1054 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
1055 /* Platform hasn't set available data widths. This is bad.
1056 * Warn and use a default. */
1057 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1058 "data widths, using default 10 bit\n");
1059 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
1061 pcdev->platform_mclk_10khz = pcdev->pdata->mclk_10khz;
1062 if (!pcdev->platform_mclk_10khz) {
1063 dev_warn(&pdev->dev,
1064 "mclk_10khz == 0! Please, fix your platform data. "
1065 "Using default 20MHz\n");
1066 pcdev->platform_mclk_10khz = 2000;
1069 INIT_LIST_HEAD(&pcdev->capture);
1070 spin_lock_init(&pcdev->lock);
1073 * Request the regions.
1075 if (!request_mem_region(res->start, res->end - res->start + 1,
1076 PXA_CAM_DRV_NAME)) {
1081 base = ioremap(res->start, res->end - res->start + 1);
1088 pcdev->dev = &pdev->dev;
1091 pcdev->dma_chans[0] = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
1092 pxa_camera_dma_irq_y, pcdev);
1093 if (pcdev->dma_chans[0] < 0) {
1094 dev_err(pcdev->dev, "Can't request DMA for Y\n");
1098 dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
1100 pcdev->dma_chans[1] = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
1101 pxa_camera_dma_irq_u, pcdev);
1102 if (pcdev->dma_chans[1] < 0) {
1103 dev_err(pcdev->dev, "Can't request DMA for U\n");
1105 goto exit_free_dma_y;
1107 dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
1109 pcdev->dma_chans[2] = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
1110 pxa_camera_dma_irq_v, pcdev);
1111 if (pcdev->dma_chans[0] < 0) {
1112 dev_err(pcdev->dev, "Can't request DMA for V\n");
1114 goto exit_free_dma_u;
1116 dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
1118 DRCMR68 = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1119 DRCMR69 = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1120 DRCMR70 = pcdev->dma_chans[2] | DRCMR_MAPVLD;
1123 err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
1126 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1130 pxa_soc_camera_host.priv = pcdev;
1131 pxa_soc_camera_host.dev.parent = &pdev->dev;
1132 pxa_soc_camera_host.nr = pdev->id;
1133 err = soc_camera_host_register(&pxa_soc_camera_host);
1140 free_irq(pcdev->irq, pcdev);
1142 pxa_free_dma(pcdev->dma_chans[2]);
1144 pxa_free_dma(pcdev->dma_chans[1]);
1146 pxa_free_dma(pcdev->dma_chans[0]);
1150 release_mem_region(res->start, res->end - res->start + 1);
1152 clk_put(pcdev->clk);
1159 static int __devexit pxa_camera_remove(struct platform_device *pdev)
1161 struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
1162 struct resource *res;
1164 clk_put(pcdev->clk);
1166 pxa_free_dma(pcdev->dma_chans[0]);
1167 pxa_free_dma(pcdev->dma_chans[1]);
1168 pxa_free_dma(pcdev->dma_chans[2]);
1169 free_irq(pcdev->irq, pcdev);
1171 soc_camera_host_unregister(&pxa_soc_camera_host);
1173 iounmap(pcdev->base);
1176 release_mem_region(res->start, res->end - res->start + 1);
1180 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
1185 static struct platform_driver pxa_camera_driver = {
1187 .name = PXA_CAM_DRV_NAME,
1189 .probe = pxa_camera_probe,
1190 .remove = __exit_p(pxa_camera_remove),
1194 static int __devinit pxa_camera_init(void)
1196 return platform_driver_register(&pxa_camera_driver);
1199 static void __exit pxa_camera_exit(void)
1201 return platform_driver_unregister(&pxa_camera_driver);
1204 module_init(pxa_camera_init);
1205 module_exit(pxa_camera_exit);
1207 MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1208 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1209 MODULE_LICENSE("GPL");