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/clk.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-dev.h>
32 #include <media/videobuf-dma-sg.h>
33 #include <media/soc_camera.h>
35 #include <linux/videodev2.h>
38 #include <mach/camera.h>
40 #define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
41 #define PXA_CAM_DRV_NAME "pxa27x-camera"
43 /* Camera Interface */
56 #define CICR0_DMAEN (1 << 31) /* DMA request enable */
57 #define CICR0_PAR_EN (1 << 30) /* Parity enable */
58 #define CICR0_SL_CAP_EN (1 << 29) /* Capture enable for slave mode */
59 #define CICR0_ENB (1 << 28) /* Camera interface enable */
60 #define CICR0_DIS (1 << 27) /* Camera interface disable */
61 #define CICR0_SIM (0x7 << 24) /* Sensor interface mode mask */
62 #define CICR0_TOM (1 << 9) /* Time-out mask */
63 #define CICR0_RDAVM (1 << 8) /* Receive-data-available mask */
64 #define CICR0_FEM (1 << 7) /* FIFO-empty mask */
65 #define CICR0_EOLM (1 << 6) /* End-of-line mask */
66 #define CICR0_PERRM (1 << 5) /* Parity-error mask */
67 #define CICR0_QDM (1 << 4) /* Quick-disable mask */
68 #define CICR0_CDM (1 << 3) /* Disable-done mask */
69 #define CICR0_SOFM (1 << 2) /* Start-of-frame mask */
70 #define CICR0_EOFM (1 << 1) /* End-of-frame mask */
71 #define CICR0_FOM (1 << 0) /* FIFO-overrun mask */
73 #define CICR1_TBIT (1 << 31) /* Transparency bit */
74 #define CICR1_RGBT_CONV (0x3 << 29) /* RGBT conversion mask */
75 #define CICR1_PPL (0x7ff << 15) /* Pixels per line mask */
76 #define CICR1_RGB_CONV (0x7 << 12) /* RGB conversion mask */
77 #define CICR1_RGB_F (1 << 11) /* RGB format */
78 #define CICR1_YCBCR_F (1 << 10) /* YCbCr format */
79 #define CICR1_RGB_BPP (0x7 << 7) /* RGB bis per pixel mask */
80 #define CICR1_RAW_BPP (0x3 << 5) /* Raw bis per pixel mask */
81 #define CICR1_COLOR_SP (0x3 << 3) /* Color space mask */
82 #define CICR1_DW (0x7 << 0) /* Data width mask */
84 #define CICR2_BLW (0xff << 24) /* Beginning-of-line pixel clock
86 #define CICR2_ELW (0xff << 16) /* End-of-line pixel clock
88 #define CICR2_HSW (0x3f << 10) /* Horizontal sync pulse width mask */
89 #define CICR2_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
91 #define CICR2_FSW (0x7 << 0) /* Frame stabilization
94 #define CICR3_BFW (0xff << 24) /* Beginning-of-frame line clock
96 #define CICR3_EFW (0xff << 16) /* End-of-frame line clock
98 #define CICR3_VSW (0x3f << 10) /* Vertical sync pulse width mask */
99 #define CICR3_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
101 #define CICR3_LPF (0x7ff << 0) /* Lines per frame mask */
103 #define CICR4_MCLK_DLY (0x3 << 24) /* MCLK Data Capture Delay mask */
104 #define CICR4_PCLK_EN (1 << 23) /* Pixel clock enable */
105 #define CICR4_PCP (1 << 22) /* Pixel clock polarity */
106 #define CICR4_HSP (1 << 21) /* Horizontal sync polarity */
107 #define CICR4_VSP (1 << 20) /* Vertical sync polarity */
108 #define CICR4_MCLK_EN (1 << 19) /* MCLK enable */
109 #define CICR4_FR_RATE (0x7 << 8) /* Frame rate mask */
110 #define CICR4_DIV (0xff << 0) /* Clock divisor mask */
112 #define CISR_FTO (1 << 15) /* FIFO time-out */
113 #define CISR_RDAV_2 (1 << 14) /* Channel 2 receive data available */
114 #define CISR_RDAV_1 (1 << 13) /* Channel 1 receive data available */
115 #define CISR_RDAV_0 (1 << 12) /* Channel 0 receive data available */
116 #define CISR_FEMPTY_2 (1 << 11) /* Channel 2 FIFO empty */
117 #define CISR_FEMPTY_1 (1 << 10) /* Channel 1 FIFO empty */
118 #define CISR_FEMPTY_0 (1 << 9) /* Channel 0 FIFO empty */
119 #define CISR_EOL (1 << 8) /* End of line */
120 #define CISR_PAR_ERR (1 << 7) /* Parity error */
121 #define CISR_CQD (1 << 6) /* Camera interface quick disable */
122 #define CISR_CDD (1 << 5) /* Camera interface disable done */
123 #define CISR_SOF (1 << 4) /* Start of frame */
124 #define CISR_EOF (1 << 3) /* End of frame */
125 #define CISR_IFO_2 (1 << 2) /* FIFO overrun for Channel 2 */
126 #define CISR_IFO_1 (1 << 1) /* FIFO overrun for Channel 1 */
127 #define CISR_IFO_0 (1 << 0) /* FIFO overrun for Channel 0 */
129 #define CIFR_FLVL2 (0x7f << 23) /* FIFO 2 level mask */
130 #define CIFR_FLVL1 (0x7f << 16) /* FIFO 1 level mask */
131 #define CIFR_FLVL0 (0xff << 8) /* FIFO 0 level mask */
132 #define CIFR_THL_0 (0x3 << 4) /* Threshold Level for Channel 0 FIFO */
133 #define CIFR_RESET_F (1 << 3) /* Reset input FIFOs */
134 #define CIFR_FEN2 (1 << 2) /* FIFO enable for channel 2 */
135 #define CIFR_FEN1 (1 << 1) /* FIFO enable for channel 1 */
136 #define CIFR_FEN0 (1 << 0) /* FIFO enable for channel 0 */
138 #define CICR0_SIM_MP (0 << 24)
139 #define CICR0_SIM_SP (1 << 24)
140 #define CICR0_SIM_MS (2 << 24)
141 #define CICR0_SIM_EP (3 << 24)
142 #define CICR0_SIM_ES (4 << 24)
144 #define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
145 #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
146 #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
147 #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
148 #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
150 #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
151 #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
152 #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
153 #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
154 #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
156 #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
157 #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
158 #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
159 #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
161 #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
162 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
163 CICR0_EOFM | CICR0_FOM)
168 enum pxa_camera_active_dma {
174 /* descriptor needed for the PXA DMA engine */
177 struct pxa_dma_desc *sg_cpu;
182 /* buffer for one video frame */
184 /* common v4l buffer stuff -- must be first */
185 struct videobuf_buffer vb;
187 const struct soc_camera_data_format *fmt;
189 /* our descriptor lists for Y, U and V channels */
190 struct pxa_cam_dma dmas[3];
194 enum pxa_camera_active_dma active_dma;
197 struct pxa_camera_dev {
199 /* PXA27x is only supposed to handle one camera on its Quick Capture
200 * interface. If anyone ever builds hardware to enable more than
201 * one camera, they will have to modify this driver too */
202 struct soc_camera_device *icd;
209 unsigned int dma_chans[3];
211 struct pxacamera_platform_data *pdata;
212 struct resource *res;
213 unsigned long platform_flags;
218 struct list_head capture;
222 struct pxa_buffer *active;
223 struct pxa_dma_desc *sg_tail[3];
228 static const char *pxa_cam_driver_description = "PXA_Camera";
230 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
233 * Videobuf operations
235 static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
238 struct soc_camera_device *icd = vq->priv_data;
239 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
240 struct pxa_camera_dev *pcdev = ici->priv;
242 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
244 /* planar capture requires Y, U and V buffers to be page aligned */
245 if (pcdev->channels == 3) {
246 *size = PAGE_ALIGN(icd->width * icd->height); /* Y pages */
247 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* U pages */
248 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* V pages */
250 *size = icd->width * icd->height *
251 ((icd->current_fmt->depth + 7) >> 3);
256 while (*size * *count > vid_limit * 1024 * 1024)
262 static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
264 struct soc_camera_device *icd = vq->priv_data;
265 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
266 struct pxa_camera_dev *pcdev = ici->priv;
267 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
270 BUG_ON(in_interrupt());
272 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
273 &buf->vb, buf->vb.baddr, buf->vb.bsize);
275 /* This waits until this buffer is out of danger, i.e., until it is no
276 * longer in STATE_QUEUED or STATE_ACTIVE */
277 videobuf_waiton(&buf->vb, 0, 0);
278 videobuf_dma_unmap(vq, dma);
279 videobuf_dma_free(dma);
281 for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
282 if (buf->dmas[i].sg_cpu)
283 dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
285 buf->dmas[i].sg_dma);
286 buf->dmas[i].sg_cpu = NULL;
289 buf->vb.state = VIDEOBUF_NEEDS_INIT;
292 static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
293 struct pxa_buffer *buf,
294 struct videobuf_dmabuf *dma, int channel,
295 int sglen, int sg_start, int cibr,
298 struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
302 dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
303 pxa_dma->sg_cpu, pxa_dma->sg_dma);
305 pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
306 pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
307 &pxa_dma->sg_dma, GFP_KERNEL);
308 if (!pxa_dma->sg_cpu)
311 pxa_dma->sglen = sglen;
313 for (i = 0; i < sglen; i++) {
314 int sg_i = sg_start + i;
315 struct scatterlist *sg = dma->sglist;
316 unsigned int dma_len = sg_dma_len(&sg[sg_i]), xfer_len;
318 pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
319 pxa_dma->sg_cpu[i].dtadr = sg_dma_address(&sg[sg_i]);
321 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
322 xfer_len = (min(dma_len, size) + 7) & ~7;
324 pxa_dma->sg_cpu[i].dcmd =
325 DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
327 pxa_dma->sg_cpu[i].ddadr =
328 pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
331 pxa_dma->sg_cpu[sglen - 1].ddadr = DDADR_STOP;
332 pxa_dma->sg_cpu[sglen - 1].dcmd |= DCMD_ENDIRQEN;
337 static int pxa_videobuf_prepare(struct videobuf_queue *vq,
338 struct videobuf_buffer *vb, enum v4l2_field field)
340 struct soc_camera_device *icd = vq->priv_data;
341 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
342 struct pxa_camera_dev *pcdev = ici->priv;
343 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
345 int sglen_y, sglen_yu = 0, sglen_u = 0, sglen_v = 0;
346 int size_y, size_u = 0, size_v = 0;
348 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
349 vb, vb->baddr, vb->bsize);
351 /* Added list head initialization on alloc */
352 WARN_ON(!list_empty(&vb->queue));
355 /* This can be useful if you want to see if we actually fill
356 * the buffer with something */
357 memset((void *)vb->baddr, 0xaa, vb->bsize);
360 BUG_ON(NULL == icd->current_fmt);
362 /* I think, in buf_prepare you only have to protect global data,
363 * the actual buffer is yours */
366 if (buf->fmt != icd->current_fmt ||
367 vb->width != icd->width ||
368 vb->height != icd->height ||
369 vb->field != field) {
370 buf->fmt = icd->current_fmt;
371 vb->width = icd->width;
372 vb->height = icd->height;
374 vb->state = VIDEOBUF_NEEDS_INIT;
377 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
378 if (0 != vb->baddr && vb->bsize < vb->size) {
383 if (vb->state == VIDEOBUF_NEEDS_INIT) {
384 unsigned int size = vb->size;
385 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
387 ret = videobuf_iolock(vq, vb, NULL);
391 if (pcdev->channels == 3) {
392 /* FIXME the calculations should be more precise */
393 sglen_y = dma->sglen / 2;
394 sglen_u = sglen_v = dma->sglen / 4 + 1;
395 sglen_yu = sglen_y + sglen_u;
397 size_u = size_v = size / 4;
399 sglen_y = dma->sglen;
403 /* init DMA for Y channel */
404 ret = pxa_init_dma_channel(pcdev, buf, dma, 0, sglen_y,
409 "DMA initialization for Y/RGB failed\n");
413 if (pcdev->channels == 3) {
414 /* init DMA for U channel */
415 ret = pxa_init_dma_channel(pcdev, buf, dma, 1, sglen_u,
416 sglen_y, 0x30, size_u);
419 "DMA initialization for U failed\n");
423 /* init DMA for V channel */
424 ret = pxa_init_dma_channel(pcdev, buf, dma, 2, sglen_v,
425 sglen_yu, 0x38, size_v);
428 "DMA initialization for V failed\n");
433 vb->state = VIDEOBUF_PREPARED;
437 buf->active_dma = DMA_Y;
438 if (pcdev->channels == 3)
439 buf->active_dma |= DMA_U | DMA_V;
444 dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
445 buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
447 dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
448 buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
450 free_buffer(vq, buf);
456 static void pxa_videobuf_queue(struct videobuf_queue *vq,
457 struct videobuf_buffer *vb)
459 struct soc_camera_device *icd = vq->priv_data;
460 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
461 struct pxa_camera_dev *pcdev = ici->priv;
462 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
463 struct pxa_buffer *active;
467 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
468 vb, vb->baddr, vb->bsize);
469 spin_lock_irqsave(&pcdev->lock, flags);
471 list_add_tail(&vb->queue, &pcdev->capture);
473 vb->state = VIDEOBUF_ACTIVE;
474 active = pcdev->active;
477 unsigned long cifr, cicr0;
479 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
480 __raw_writel(cifr, pcdev->base + CIFR);
482 for (i = 0; i < pcdev->channels; i++) {
483 DDADR(pcdev->dma_chans[i]) = buf->dmas[i].sg_dma;
484 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
485 pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen - 1;
490 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
491 __raw_writel(cicr0, pcdev->base + CICR0);
493 struct pxa_cam_dma *buf_dma;
494 struct pxa_cam_dma *act_dma;
497 for (i = 0; i < pcdev->channels; i++) {
498 buf_dma = &buf->dmas[i];
499 act_dma = &active->dmas[i];
500 nents = buf_dma->sglen;
502 /* Stop DMA engine */
503 DCSR(pcdev->dma_chans[i]) = 0;
505 /* Add the descriptors we just initialized to
506 the currently running chain */
507 pcdev->sg_tail[i]->ddadr = buf_dma->sg_dma;
508 pcdev->sg_tail[i] = buf_dma->sg_cpu + buf_dma->sglen - 1;
510 /* Setup a dummy descriptor with the DMA engines current
513 buf_dma->sg_cpu[nents].dsadr =
514 pcdev->res->start + 0x28 + i*8; /* CIBRx */
515 buf_dma->sg_cpu[nents].dtadr =
516 DTADR(pcdev->dma_chans[i]);
517 buf_dma->sg_cpu[nents].dcmd =
518 DCMD(pcdev->dma_chans[i]);
520 if (DDADR(pcdev->dma_chans[i]) == DDADR_STOP) {
521 /* The DMA engine is on the last
522 descriptor, set the next descriptors
523 address to the descriptors we just
525 buf_dma->sg_cpu[nents].ddadr = buf_dma->sg_dma;
527 buf_dma->sg_cpu[nents].ddadr =
528 DDADR(pcdev->dma_chans[i]);
531 /* The next descriptor is the dummy descriptor */
532 DDADR(pcdev->dma_chans[i]) = buf_dma->sg_dma + nents *
533 sizeof(struct pxa_dma_desc);
535 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
539 spin_unlock_irqrestore(&pcdev->lock, flags);
542 static void pxa_videobuf_release(struct videobuf_queue *vq,
543 struct videobuf_buffer *vb)
545 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
547 struct soc_camera_device *icd = vq->priv_data;
549 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
550 vb, vb->baddr, vb->bsize);
553 case VIDEOBUF_ACTIVE:
554 dev_dbg(&icd->dev, "%s (active)\n", __func__);
556 case VIDEOBUF_QUEUED:
557 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
559 case VIDEOBUF_PREPARED:
560 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
563 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
568 free_buffer(vq, buf);
571 static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
572 struct videobuf_buffer *vb,
573 struct pxa_buffer *buf)
577 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
578 list_del_init(&vb->queue);
579 vb->state = VIDEOBUF_DONE;
580 do_gettimeofday(&vb->ts);
584 if (list_empty(&pcdev->capture)) {
585 pcdev->active = NULL;
586 DCSR(pcdev->dma_chans[0]) = 0;
587 DCSR(pcdev->dma_chans[1]) = 0;
588 DCSR(pcdev->dma_chans[2]) = 0;
590 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
591 __raw_writel(cicr0, pcdev->base + CICR0);
595 pcdev->active = list_entry(pcdev->capture.next,
596 struct pxa_buffer, vb.queue);
599 static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
600 enum pxa_camera_active_dma act_dma)
602 struct pxa_buffer *buf;
604 u32 status, camera_status, overrun;
605 struct videobuf_buffer *vb;
606 unsigned long cifr, cicr0;
608 spin_lock_irqsave(&pcdev->lock, flags);
610 status = DCSR(channel);
611 DCSR(channel) = status | DCSR_ENDINTR;
613 if (status & DCSR_BUSERR) {
614 dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
618 if (!(status & DCSR_ENDINTR)) {
619 dev_err(pcdev->dev, "Unknown DMA IRQ source, "
620 "status: 0x%08x\n", status);
624 if (!pcdev->active) {
625 dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
629 camera_status = __raw_readl(pcdev->base + CISR);
630 overrun = CISR_IFO_0;
631 if (pcdev->channels == 3)
632 overrun |= CISR_IFO_1 | CISR_IFO_2;
633 if (camera_status & overrun) {
634 dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n", camera_status);
635 /* Stop the Capture Interface */
636 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
637 __raw_writel(cicr0, pcdev->base + CICR0);
641 /* Reset the FIFOs */
642 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
643 __raw_writel(cifr, pcdev->base + CIFR);
644 /* Enable End-Of-Frame Interrupt */
645 cicr0 &= ~CICR0_EOFM;
646 __raw_writel(cicr0, pcdev->base + CICR0);
647 /* Restart the Capture Interface */
648 __raw_writel(cicr0 | CICR0_ENB, pcdev->base + CICR0);
652 vb = &pcdev->active->vb;
653 buf = container_of(vb, struct pxa_buffer, vb);
654 WARN_ON(buf->inwork || list_empty(&vb->queue));
655 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
656 vb, vb->baddr, vb->bsize);
658 buf->active_dma &= ~act_dma;
659 if (!buf->active_dma)
660 pxa_camera_wakeup(pcdev, vb, buf);
663 spin_unlock_irqrestore(&pcdev->lock, flags);
666 static void pxa_camera_dma_irq_y(int channel, void *data)
668 struct pxa_camera_dev *pcdev = data;
669 pxa_camera_dma_irq(channel, pcdev, DMA_Y);
672 static void pxa_camera_dma_irq_u(int channel, void *data)
674 struct pxa_camera_dev *pcdev = data;
675 pxa_camera_dma_irq(channel, pcdev, DMA_U);
678 static void pxa_camera_dma_irq_v(int channel, void *data)
680 struct pxa_camera_dev *pcdev = data;
681 pxa_camera_dma_irq(channel, pcdev, DMA_V);
684 static struct videobuf_queue_ops pxa_videobuf_ops = {
685 .buf_setup = pxa_videobuf_setup,
686 .buf_prepare = pxa_videobuf_prepare,
687 .buf_queue = pxa_videobuf_queue,
688 .buf_release = pxa_videobuf_release,
691 static void pxa_camera_init_videobuf(struct videobuf_queue *q,
692 struct soc_camera_device *icd)
694 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
695 struct pxa_camera_dev *pcdev = ici->priv;
697 /* We must pass NULL as dev pointer, then all pci_* dma operations
698 * transform to normal dma_* ones. */
699 videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
700 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
701 sizeof(struct pxa_buffer), icd);
704 static u32 mclk_get_divisor(struct pxa_camera_dev *pcdev)
706 unsigned long mclk = pcdev->mclk;
708 unsigned long lcdclk;
710 lcdclk = clk_get_rate(pcdev->clk);
711 pcdev->ciclk = lcdclk;
713 /* mclk <= ciclk / 4 (27.4.2) */
714 if (mclk > lcdclk / 4) {
716 dev_warn(pcdev->dev, "Limiting master clock to %lu\n", mclk);
719 /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
720 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
722 /* If we're not supplying MCLK, leave it at 0 */
723 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
724 pcdev->mclk = lcdclk / (2 * (div + 1));
726 dev_dbg(pcdev->dev, "LCD clock %luHz, target freq %luHz, "
727 "divisor %u\n", lcdclk, mclk, div);
732 static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
735 /* We want a timeout > 1 pixel time, not ">=" */
736 u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
738 __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
741 static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
743 struct pxacamera_platform_data *pdata = pcdev->pdata;
746 dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
749 if (pdata && pdata->init) {
750 dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
751 pdata->init(pcdev->dev);
754 /* disable all interrupts */
755 __raw_writel(0x3ff, pcdev->base + CICR0);
757 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
758 cicr4 |= CICR4_PCLK_EN;
759 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
760 cicr4 |= CICR4_MCLK_EN;
761 if (pcdev->platform_flags & PXA_CAMERA_PCP)
763 if (pcdev->platform_flags & PXA_CAMERA_HSP)
765 if (pcdev->platform_flags & PXA_CAMERA_VSP)
768 __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
770 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
771 /* Initialise the timeout under the assumption pclk = mclk */
772 recalculate_fifo_timeout(pcdev, pcdev->mclk);
774 /* "Safe default" - 13MHz */
775 recalculate_fifo_timeout(pcdev, 13000000);
777 clk_enable(pcdev->clk);
780 static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
782 clk_disable(pcdev->clk);
785 static irqreturn_t pxa_camera_irq(int irq, void *data)
787 struct pxa_camera_dev *pcdev = data;
788 unsigned long status, cicr0;
790 status = __raw_readl(pcdev->base + CISR);
791 dev_dbg(pcdev->dev, "Camera interrupt status 0x%lx\n", status);
796 __raw_writel(status, pcdev->base + CISR);
798 if (status & CISR_EOF) {
800 for (i = 0; i < pcdev->channels; i++) {
801 DDADR(pcdev->dma_chans[i]) =
802 pcdev->active->dmas[i].sg_dma;
803 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
805 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
806 __raw_writel(cicr0, pcdev->base + CICR0);
813 * The following two functions absolutely depend on the fact, that
814 * there can be only one camera on PXA quick capture interface
815 * Called with .video_lock held
817 static int pxa_camera_add_device(struct soc_camera_device *icd)
819 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
820 struct pxa_camera_dev *pcdev = ici->priv;
828 dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
831 pxa_camera_activate(pcdev);
832 ret = icd->ops->init(icd);
841 /* Called with .video_lock held */
842 static void pxa_camera_remove_device(struct soc_camera_device *icd)
844 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
845 struct pxa_camera_dev *pcdev = ici->priv;
847 BUG_ON(icd != pcdev->icd);
849 dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
852 /* disable capture, disable interrupts */
853 __raw_writel(0x3ff, pcdev->base + CICR0);
855 /* Stop DMA engine */
856 DCSR(pcdev->dma_chans[0]) = 0;
857 DCSR(pcdev->dma_chans[1]) = 0;
858 DCSR(pcdev->dma_chans[2]) = 0;
860 icd->ops->release(icd);
862 pxa_camera_deactivate(pcdev);
867 static int test_platform_param(struct pxa_camera_dev *pcdev,
868 unsigned char buswidth, unsigned long *flags)
871 * Platform specified synchronization and pixel clock polarities are
872 * only a recommendation and are only used during probing. The PXA270
873 * quick capture interface supports both.
875 *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
876 SOCAM_MASTER : SOCAM_SLAVE) |
877 SOCAM_HSYNC_ACTIVE_HIGH |
878 SOCAM_HSYNC_ACTIVE_LOW |
879 SOCAM_VSYNC_ACTIVE_HIGH |
880 SOCAM_VSYNC_ACTIVE_LOW |
881 SOCAM_DATA_ACTIVE_HIGH |
882 SOCAM_PCLK_SAMPLE_RISING |
883 SOCAM_PCLK_SAMPLE_FALLING;
885 /* If requested data width is supported by the platform, use it */
888 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
890 *flags |= SOCAM_DATAWIDTH_10;
893 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
895 *flags |= SOCAM_DATAWIDTH_9;
898 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
900 *flags |= SOCAM_DATAWIDTH_8;
909 static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
911 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
912 struct pxa_camera_dev *pcdev = ici->priv;
913 unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
914 u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0;
915 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
920 camera_flags = icd->ops->query_bus_param(icd);
922 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
928 /* Make choises, based on platform preferences */
929 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
930 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
931 if (pcdev->platform_flags & PXA_CAMERA_HSP)
932 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
934 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
937 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
938 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
939 if (pcdev->platform_flags & PXA_CAMERA_VSP)
940 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
942 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
945 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
946 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
947 if (pcdev->platform_flags & PXA_CAMERA_PCP)
948 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
950 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
953 ret = icd->ops->set_bus_param(icd, common_flags);
957 /* Datawidth is now guaranteed to be equal to one of the three values.
958 * We fix bit-per-pixel equal to data-width... */
959 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
960 case SOCAM_DATAWIDTH_10:
964 case SOCAM_DATAWIDTH_9:
969 /* Actually it can only be 8 now,
970 * default is just to silence compiler warnings */
971 case SOCAM_DATAWIDTH_8:
976 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
977 cicr4 |= CICR4_PCLK_EN;
978 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
979 cicr4 |= CICR4_MCLK_EN;
980 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
982 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
984 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
987 cicr0 = __raw_readl(pcdev->base + CICR0);
988 if (cicr0 & CICR0_ENB)
989 __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
991 cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
994 case V4L2_PIX_FMT_YUV422P:
996 cicr1 |= CICR1_YCBCR_F;
998 * Normally, pxa bus wants as input UYVY format. We allow all
999 * reorderings of the YUV422 format, as no processing is done,
1000 * and the YUV stream is just passed through without any
1001 * transformation. Note that UYVY is the only format that
1002 * should be used if pxa framebuffer Overlay2 is used.
1004 case V4L2_PIX_FMT_UYVY:
1005 case V4L2_PIX_FMT_VYUY:
1006 case V4L2_PIX_FMT_YUYV:
1007 case V4L2_PIX_FMT_YVYU:
1008 cicr1 |= CICR1_COLOR_SP_VAL(2);
1010 case V4L2_PIX_FMT_RGB555:
1011 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
1012 CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
1014 case V4L2_PIX_FMT_RGB565:
1015 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
1020 cicr3 = CICR3_LPF_VAL(icd->height - 1) |
1021 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
1022 cicr4 |= pcdev->mclk_divisor;
1024 __raw_writel(cicr1, pcdev->base + CICR1);
1025 __raw_writel(cicr2, pcdev->base + CICR2);
1026 __raw_writel(cicr3, pcdev->base + CICR3);
1027 __raw_writel(cicr4, pcdev->base + CICR4);
1029 /* CIF interrupts are not used, only DMA */
1030 cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
1031 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
1032 cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
1033 __raw_writel(cicr0, pcdev->base + CICR0);
1038 static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
1039 unsigned char buswidth)
1041 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1042 struct pxa_camera_dev *pcdev = ici->priv;
1043 unsigned long bus_flags, camera_flags;
1044 int ret = test_platform_param(pcdev, buswidth, &bus_flags);
1049 camera_flags = icd->ops->query_bus_param(icd);
1051 return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
1054 static const struct soc_camera_data_format pxa_camera_formats[] = {
1056 .name = "Planar YUV422 16 bit",
1058 .fourcc = V4L2_PIX_FMT_YUV422P,
1059 .colorspace = V4L2_COLORSPACE_JPEG,
1063 static bool buswidth_supported(struct soc_camera_device *icd, int depth)
1065 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1066 struct pxa_camera_dev *pcdev = ici->priv;
1070 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8);
1072 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9);
1074 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10);
1079 static int required_buswidth(const struct soc_camera_data_format *fmt)
1081 switch (fmt->fourcc) {
1082 case V4L2_PIX_FMT_UYVY:
1083 case V4L2_PIX_FMT_VYUY:
1084 case V4L2_PIX_FMT_YUYV:
1085 case V4L2_PIX_FMT_YVYU:
1086 case V4L2_PIX_FMT_RGB565:
1087 case V4L2_PIX_FMT_RGB555:
1094 static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
1095 struct soc_camera_format_xlate *xlate)
1097 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1098 int formats = 0, buswidth, ret;
1100 buswidth = required_buswidth(icd->formats + idx);
1102 if (!buswidth_supported(icd, buswidth))
1105 ret = pxa_camera_try_bus_param(icd, buswidth);
1109 switch (icd->formats[idx].fourcc) {
1110 case V4L2_PIX_FMT_UYVY:
1113 xlate->host_fmt = &pxa_camera_formats[0];
1114 xlate->cam_fmt = icd->formats + idx;
1115 xlate->buswidth = buswidth;
1117 dev_dbg(&ici->dev, "Providing format %s using %s\n",
1118 pxa_camera_formats[0].name,
1119 icd->formats[idx].name);
1121 case V4L2_PIX_FMT_VYUY:
1122 case V4L2_PIX_FMT_YUYV:
1123 case V4L2_PIX_FMT_YVYU:
1124 case V4L2_PIX_FMT_RGB565:
1125 case V4L2_PIX_FMT_RGB555:
1128 xlate->host_fmt = icd->formats + idx;
1129 xlate->cam_fmt = icd->formats + idx;
1130 xlate->buswidth = buswidth;
1132 dev_dbg(&ici->dev, "Providing format %s packed\n",
1133 icd->formats[idx].name);
1137 /* Generic pass-through */
1140 xlate->host_fmt = icd->formats + idx;
1141 xlate->cam_fmt = icd->formats + idx;
1142 xlate->buswidth = icd->formats[idx].depth;
1145 "Providing format %s in pass-through mode\n",
1146 icd->formats[idx].name);
1153 static int pxa_camera_set_crop(struct soc_camera_device *icd,
1154 struct v4l2_rect *rect)
1156 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1157 struct pxa_camera_dev *pcdev = ici->priv;
1158 struct soc_camera_sense sense = {
1159 .master_clock = pcdev->mclk,
1160 .pixel_clock_max = pcdev->ciclk / 4,
1164 /* If PCLK is used to latch data from the sensor, check sense */
1165 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1166 icd->sense = &sense;
1168 ret = icd->ops->set_crop(icd, rect);
1173 dev_warn(&ici->dev, "Failed to crop to %ux%u@%u:%u\n",
1174 rect->width, rect->height, rect->left, rect->top);
1175 } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
1176 if (sense.pixel_clock > sense.pixel_clock_max) {
1178 "pixel clock %lu set by the camera too high!",
1182 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1188 static int pxa_camera_set_fmt(struct soc_camera_device *icd,
1189 struct v4l2_format *f)
1191 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1192 struct pxa_camera_dev *pcdev = ici->priv;
1193 const struct soc_camera_data_format *cam_fmt = NULL;
1194 const struct soc_camera_format_xlate *xlate = NULL;
1195 struct soc_camera_sense sense = {
1196 .master_clock = pcdev->mclk,
1197 .pixel_clock_max = pcdev->ciclk / 4,
1199 struct v4l2_pix_format *pix = &f->fmt.pix;
1200 struct v4l2_format cam_f = *f;
1203 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1205 dev_warn(&ici->dev, "Format %x not found\n", pix->pixelformat);
1209 cam_fmt = xlate->cam_fmt;
1211 /* If PCLK is used to latch data from the sensor, check sense */
1212 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1213 icd->sense = &sense;
1215 cam_f.fmt.pix.pixelformat = cam_fmt->fourcc;
1216 ret = icd->ops->set_fmt(icd, &cam_f);
1221 dev_warn(&ici->dev, "Failed to configure for format %x\n",
1223 } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
1224 if (sense.pixel_clock > sense.pixel_clock_max) {
1226 "pixel clock %lu set by the camera too high!",
1230 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1234 icd->buswidth = xlate->buswidth;
1235 icd->current_fmt = xlate->host_fmt;
1241 static int pxa_camera_try_fmt(struct soc_camera_device *icd,
1242 struct v4l2_format *f)
1244 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1245 const struct soc_camera_format_xlate *xlate;
1246 struct v4l2_pix_format *pix = &f->fmt.pix;
1247 __u32 pixfmt = pix->pixelformat;
1248 enum v4l2_field field;
1251 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1253 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
1257 /* limit to pxa hardware capabilities */
1258 if (pix->height < 32)
1260 if (pix->height > 2048)
1262 if (pix->width < 48)
1264 if (pix->width > 2048)
1266 pix->width &= ~0x01;
1268 pix->bytesperline = pix->width *
1269 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1270 pix->sizeimage = pix->height * pix->bytesperline;
1272 /* camera has to see its format, but the user the original one */
1273 pix->pixelformat = xlate->cam_fmt->fourcc;
1274 /* limit to sensor capabilities */
1275 ret = icd->ops->try_fmt(icd, f);
1276 pix->pixelformat = xlate->host_fmt->fourcc;
1280 if (field == V4L2_FIELD_ANY) {
1281 pix->field = V4L2_FIELD_NONE;
1282 } else if (field != V4L2_FIELD_NONE) {
1283 dev_err(&icd->dev, "Field type %d unsupported.\n", field);
1290 static int pxa_camera_reqbufs(struct soc_camera_file *icf,
1291 struct v4l2_requestbuffers *p)
1295 /* This is for locking debugging only. I removed spinlocks and now I
1296 * check whether .prepare is ever called on a linked buffer, or whether
1297 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1298 * it hadn't triggered */
1299 for (i = 0; i < p->count; i++) {
1300 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1301 struct pxa_buffer, vb);
1303 INIT_LIST_HEAD(&buf->vb.queue);
1309 static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
1311 struct soc_camera_file *icf = file->private_data;
1312 struct pxa_buffer *buf;
1314 buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
1317 poll_wait(file, &buf->vb.done, pt);
1319 if (buf->vb.state == VIDEOBUF_DONE ||
1320 buf->vb.state == VIDEOBUF_ERROR)
1321 return POLLIN|POLLRDNORM;
1326 static int pxa_camera_querycap(struct soc_camera_host *ici,
1327 struct v4l2_capability *cap)
1329 /* cap->name is set by the firendly caller:-> */
1330 strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
1331 cap->version = PXA_CAM_VERSION_CODE;
1332 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1337 static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1339 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1340 struct pxa_camera_dev *pcdev = ici->priv;
1343 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
1344 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
1345 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
1346 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
1347 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
1349 if ((pcdev->icd) && (pcdev->icd->ops->suspend))
1350 ret = pcdev->icd->ops->suspend(pcdev->icd, state);
1355 static int pxa_camera_resume(struct soc_camera_device *icd)
1357 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1358 struct pxa_camera_dev *pcdev = ici->priv;
1361 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1362 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1363 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
1365 __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
1366 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
1367 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
1368 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
1369 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
1371 if ((pcdev->icd) && (pcdev->icd->ops->resume))
1372 ret = pcdev->icd->ops->resume(pcdev->icd);
1374 /* Restart frame capture if active buffer exists */
1375 if (!ret && pcdev->active) {
1376 unsigned long cifr, cicr0;
1378 /* Reset the FIFOs */
1379 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
1380 __raw_writel(cifr, pcdev->base + CIFR);
1382 cicr0 = __raw_readl(pcdev->base + CICR0);
1383 cicr0 &= ~CICR0_EOFM; /* Enable End-Of-Frame Interrupt */
1384 cicr0 |= CICR0_ENB; /* Restart the Capture Interface */
1385 __raw_writel(cicr0, pcdev->base + CICR0);
1391 static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
1392 .owner = THIS_MODULE,
1393 .add = pxa_camera_add_device,
1394 .remove = pxa_camera_remove_device,
1395 .suspend = pxa_camera_suspend,
1396 .resume = pxa_camera_resume,
1397 .set_crop = pxa_camera_set_crop,
1398 .get_formats = pxa_camera_get_formats,
1399 .set_fmt = pxa_camera_set_fmt,
1400 .try_fmt = pxa_camera_try_fmt,
1401 .init_videobuf = pxa_camera_init_videobuf,
1402 .reqbufs = pxa_camera_reqbufs,
1403 .poll = pxa_camera_poll,
1404 .querycap = pxa_camera_querycap,
1405 .set_bus_param = pxa_camera_set_bus_param,
1408 /* Should be allocated dynamically too, but we have only one. */
1409 static struct soc_camera_host pxa_soc_camera_host = {
1410 .drv_name = PXA_CAM_DRV_NAME,
1411 .ops = &pxa_soc_camera_host_ops,
1414 static int pxa_camera_probe(struct platform_device *pdev)
1416 struct pxa_camera_dev *pcdev;
1417 struct resource *res;
1422 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1423 irq = platform_get_irq(pdev, 0);
1424 if (!res || irq < 0) {
1429 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1431 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1436 pcdev->clk = clk_get(&pdev->dev, NULL);
1437 if (IS_ERR(pcdev->clk)) {
1438 err = PTR_ERR(pcdev->clk);
1442 dev_set_drvdata(&pdev->dev, pcdev);
1445 pcdev->pdata = pdev->dev.platform_data;
1446 pcdev->platform_flags = pcdev->pdata->flags;
1447 if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
1448 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
1449 /* Platform hasn't set available data widths. This is bad.
1450 * Warn and use a default. */
1451 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1452 "data widths, using default 10 bit\n");
1453 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
1455 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
1457 dev_warn(&pdev->dev,
1458 "mclk == 0! Please, fix your platform data. "
1459 "Using default 20MHz\n");
1460 pcdev->mclk = 20000000;
1463 pcdev->dev = &pdev->dev;
1464 pcdev->mclk_divisor = mclk_get_divisor(pcdev);
1466 INIT_LIST_HEAD(&pcdev->capture);
1467 spin_lock_init(&pcdev->lock);
1470 * Request the regions.
1472 if (!request_mem_region(res->start, res->end - res->start + 1,
1473 PXA_CAM_DRV_NAME)) {
1478 base = ioremap(res->start, res->end - res->start + 1);
1487 err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
1488 pxa_camera_dma_irq_y, pcdev);
1490 dev_err(pcdev->dev, "Can't request DMA for Y\n");
1493 pcdev->dma_chans[0] = err;
1494 dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
1496 err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
1497 pxa_camera_dma_irq_u, pcdev);
1499 dev_err(pcdev->dev, "Can't request DMA for U\n");
1500 goto exit_free_dma_y;
1502 pcdev->dma_chans[1] = err;
1503 dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
1505 err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
1506 pxa_camera_dma_irq_v, pcdev);
1508 dev_err(pcdev->dev, "Can't request DMA for V\n");
1509 goto exit_free_dma_u;
1511 pcdev->dma_chans[2] = err;
1512 dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
1514 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1515 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1516 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
1519 err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
1522 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1526 pxa_soc_camera_host.priv = pcdev;
1527 pxa_soc_camera_host.dev.parent = &pdev->dev;
1528 pxa_soc_camera_host.nr = pdev->id;
1529 err = soc_camera_host_register(&pxa_soc_camera_host);
1536 free_irq(pcdev->irq, pcdev);
1538 pxa_free_dma(pcdev->dma_chans[2]);
1540 pxa_free_dma(pcdev->dma_chans[1]);
1542 pxa_free_dma(pcdev->dma_chans[0]);
1546 release_mem_region(res->start, res->end - res->start + 1);
1548 clk_put(pcdev->clk);
1555 static int __devexit pxa_camera_remove(struct platform_device *pdev)
1557 struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
1558 struct resource *res;
1560 clk_put(pcdev->clk);
1562 pxa_free_dma(pcdev->dma_chans[0]);
1563 pxa_free_dma(pcdev->dma_chans[1]);
1564 pxa_free_dma(pcdev->dma_chans[2]);
1565 free_irq(pcdev->irq, pcdev);
1567 soc_camera_host_unregister(&pxa_soc_camera_host);
1569 iounmap(pcdev->base);
1572 release_mem_region(res->start, res->end - res->start + 1);
1576 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
1581 static struct platform_driver pxa_camera_driver = {
1583 .name = PXA_CAM_DRV_NAME,
1585 .probe = pxa_camera_probe,
1586 .remove = __exit_p(pxa_camera_remove),
1590 static int __devinit pxa_camera_init(void)
1592 return platform_driver_register(&pxa_camera_driver);
1595 static void __exit pxa_camera_exit(void)
1597 platform_driver_unregister(&pxa_camera_driver);
1600 module_init(pxa_camera_init);
1601 module_exit(pxa_camera_exit);
1603 MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1604 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1605 MODULE_LICENSE("GPL");