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>
29 #include <linux/sched.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 <mach/camera.h>
41 #define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
42 #define PXA_CAM_DRV_NAME "pxa27x-camera"
44 /* Camera Interface */
57 #define CICR0_DMAEN (1 << 31) /* DMA request enable */
58 #define CICR0_PAR_EN (1 << 30) /* Parity enable */
59 #define CICR0_SL_CAP_EN (1 << 29) /* Capture enable for slave mode */
60 #define CICR0_ENB (1 << 28) /* Camera interface enable */
61 #define CICR0_DIS (1 << 27) /* Camera interface disable */
62 #define CICR0_SIM (0x7 << 24) /* Sensor interface mode mask */
63 #define CICR0_TOM (1 << 9) /* Time-out mask */
64 #define CICR0_RDAVM (1 << 8) /* Receive-data-available mask */
65 #define CICR0_FEM (1 << 7) /* FIFO-empty mask */
66 #define CICR0_EOLM (1 << 6) /* End-of-line mask */
67 #define CICR0_PERRM (1 << 5) /* Parity-error mask */
68 #define CICR0_QDM (1 << 4) /* Quick-disable mask */
69 #define CICR0_CDM (1 << 3) /* Disable-done mask */
70 #define CICR0_SOFM (1 << 2) /* Start-of-frame mask */
71 #define CICR0_EOFM (1 << 1) /* End-of-frame mask */
72 #define CICR0_FOM (1 << 0) /* FIFO-overrun mask */
74 #define CICR1_TBIT (1 << 31) /* Transparency bit */
75 #define CICR1_RGBT_CONV (0x3 << 29) /* RGBT conversion mask */
76 #define CICR1_PPL (0x7ff << 15) /* Pixels per line mask */
77 #define CICR1_RGB_CONV (0x7 << 12) /* RGB conversion mask */
78 #define CICR1_RGB_F (1 << 11) /* RGB format */
79 #define CICR1_YCBCR_F (1 << 10) /* YCbCr format */
80 #define CICR1_RGB_BPP (0x7 << 7) /* RGB bis per pixel mask */
81 #define CICR1_RAW_BPP (0x3 << 5) /* Raw bis per pixel mask */
82 #define CICR1_COLOR_SP (0x3 << 3) /* Color space mask */
83 #define CICR1_DW (0x7 << 0) /* Data width mask */
85 #define CICR2_BLW (0xff << 24) /* Beginning-of-line pixel clock
87 #define CICR2_ELW (0xff << 16) /* End-of-line pixel clock
89 #define CICR2_HSW (0x3f << 10) /* Horizontal sync pulse width mask */
90 #define CICR2_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
92 #define CICR2_FSW (0x7 << 0) /* Frame stabilization
95 #define CICR3_BFW (0xff << 24) /* Beginning-of-frame line clock
97 #define CICR3_EFW (0xff << 16) /* End-of-frame line clock
99 #define CICR3_VSW (0x3f << 10) /* Vertical sync pulse width mask */
100 #define CICR3_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
102 #define CICR3_LPF (0x7ff << 0) /* Lines per frame mask */
104 #define CICR4_MCLK_DLY (0x3 << 24) /* MCLK Data Capture Delay mask */
105 #define CICR4_PCLK_EN (1 << 23) /* Pixel clock enable */
106 #define CICR4_PCP (1 << 22) /* Pixel clock polarity */
107 #define CICR4_HSP (1 << 21) /* Horizontal sync polarity */
108 #define CICR4_VSP (1 << 20) /* Vertical sync polarity */
109 #define CICR4_MCLK_EN (1 << 19) /* MCLK enable */
110 #define CICR4_FR_RATE (0x7 << 8) /* Frame rate mask */
111 #define CICR4_DIV (0xff << 0) /* Clock divisor mask */
113 #define CISR_FTO (1 << 15) /* FIFO time-out */
114 #define CISR_RDAV_2 (1 << 14) /* Channel 2 receive data available */
115 #define CISR_RDAV_1 (1 << 13) /* Channel 1 receive data available */
116 #define CISR_RDAV_0 (1 << 12) /* Channel 0 receive data available */
117 #define CISR_FEMPTY_2 (1 << 11) /* Channel 2 FIFO empty */
118 #define CISR_FEMPTY_1 (1 << 10) /* Channel 1 FIFO empty */
119 #define CISR_FEMPTY_0 (1 << 9) /* Channel 0 FIFO empty */
120 #define CISR_EOL (1 << 8) /* End of line */
121 #define CISR_PAR_ERR (1 << 7) /* Parity error */
122 #define CISR_CQD (1 << 6) /* Camera interface quick disable */
123 #define CISR_CDD (1 << 5) /* Camera interface disable done */
124 #define CISR_SOF (1 << 4) /* Start of frame */
125 #define CISR_EOF (1 << 3) /* End of frame */
126 #define CISR_IFO_2 (1 << 2) /* FIFO overrun for Channel 2 */
127 #define CISR_IFO_1 (1 << 1) /* FIFO overrun for Channel 1 */
128 #define CISR_IFO_0 (1 << 0) /* FIFO overrun for Channel 0 */
130 #define CIFR_FLVL2 (0x7f << 23) /* FIFO 2 level mask */
131 #define CIFR_FLVL1 (0x7f << 16) /* FIFO 1 level mask */
132 #define CIFR_FLVL0 (0xff << 8) /* FIFO 0 level mask */
133 #define CIFR_THL_0 (0x3 << 4) /* Threshold Level for Channel 0 FIFO */
134 #define CIFR_RESET_F (1 << 3) /* Reset input FIFOs */
135 #define CIFR_FEN2 (1 << 2) /* FIFO enable for channel 2 */
136 #define CIFR_FEN1 (1 << 1) /* FIFO enable for channel 1 */
137 #define CIFR_FEN0 (1 << 0) /* FIFO enable for channel 0 */
139 #define CICR0_SIM_MP (0 << 24)
140 #define CICR0_SIM_SP (1 << 24)
141 #define CICR0_SIM_MS (2 << 24)
142 #define CICR0_SIM_EP (3 << 24)
143 #define CICR0_SIM_ES (4 << 24)
145 #define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
146 #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
147 #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
148 #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
149 #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
151 #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
152 #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
153 #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
154 #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
155 #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
157 #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
158 #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
159 #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
160 #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
162 #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
163 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
164 CICR0_EOFM | CICR0_FOM)
169 enum pxa_camera_active_dma
{
175 /* descriptor needed for the PXA DMA engine */
178 struct pxa_dma_desc
*sg_cpu
;
183 /* buffer for one video frame */
185 /* common v4l buffer stuff -- must be first */
186 struct videobuf_buffer vb
;
188 const struct soc_camera_data_format
*fmt
;
190 /* our descriptor lists for Y, U and V channels */
191 struct pxa_cam_dma dmas
[3];
195 enum pxa_camera_active_dma active_dma
;
198 struct pxa_camera_dev
{
199 struct soc_camera_host soc_host
;
200 /* PXA27x is only supposed to handle one camera on its Quick Capture
201 * interface. If anyone ever builds hardware to enable more than
202 * one camera, they will have to modify this driver too */
203 struct soc_camera_device
*icd
;
210 unsigned int dma_chans
[3];
212 struct pxacamera_platform_data
*pdata
;
213 struct resource
*res
;
214 unsigned long platform_flags
;
219 struct list_head capture
;
223 struct pxa_buffer
*active
;
224 struct pxa_dma_desc
*sg_tail
[3];
233 static const char *pxa_cam_driver_description
= "PXA_Camera";
235 static unsigned int vid_limit
= 16; /* Video memory limit, in Mb */
238 * Videobuf operations
240 static int pxa_videobuf_setup(struct videobuf_queue
*vq
, unsigned int *count
,
243 struct soc_camera_device
*icd
= vq
->priv_data
;
245 dev_dbg(icd
->dev
.parent
, "count=%d, size=%d\n", *count
, *size
);
247 *size
= roundup(icd
->user_width
* icd
->user_height
*
248 ((icd
->current_fmt
->depth
+ 7) >> 3), 8);
252 while (*size
* *count
> vid_limit
* 1024 * 1024)
258 static void free_buffer(struct videobuf_queue
*vq
, struct pxa_buffer
*buf
)
260 struct soc_camera_device
*icd
= vq
->priv_data
;
261 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
262 struct videobuf_dmabuf
*dma
= videobuf_to_dma(&buf
->vb
);
265 BUG_ON(in_interrupt());
267 dev_dbg(icd
->dev
.parent
, "%s (vb=0x%p) 0x%08lx %d\n", __func__
,
268 &buf
->vb
, buf
->vb
.baddr
, buf
->vb
.bsize
);
270 /* This waits until this buffer is out of danger, i.e., until it is no
271 * longer in STATE_QUEUED or STATE_ACTIVE */
272 videobuf_waiton(&buf
->vb
, 0, 0);
273 videobuf_dma_unmap(vq
, dma
);
274 videobuf_dma_free(dma
);
276 for (i
= 0; i
< ARRAY_SIZE(buf
->dmas
); i
++) {
277 if (buf
->dmas
[i
].sg_cpu
)
278 dma_free_coherent(ici
->v4l2_dev
.dev
,
279 buf
->dmas
[i
].sg_size
,
281 buf
->dmas
[i
].sg_dma
);
282 buf
->dmas
[i
].sg_cpu
= NULL
;
285 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
288 static int calculate_dma_sglen(struct scatterlist
*sglist
, int sglen
,
289 int sg_first_ofs
, int size
)
291 int i
, offset
, dma_len
, xfer_len
;
292 struct scatterlist
*sg
;
294 offset
= sg_first_ofs
;
295 for_each_sg(sglist
, sg
, sglen
, i
) {
296 dma_len
= sg_dma_len(sg
);
298 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
299 xfer_len
= roundup(min(dma_len
- offset
, size
), 8);
301 size
= max(0, size
- xfer_len
);
312 * pxa_init_dma_channel - init dma descriptors
313 * @pcdev: pxa camera device
314 * @buf: pxa buffer to find pxa dma channel
315 * @dma: dma video buffer
316 * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V')
317 * @cibr: camera Receive Buffer Register
318 * @size: bytes to transfer
319 * @sg_first: first element of sg_list
320 * @sg_first_ofs: offset in first element of sg_list
322 * Prepares the pxa dma descriptors to transfer one camera channel.
323 * Beware sg_first and sg_first_ofs are both input and output parameters.
325 * Returns 0 or -ENOMEM if no coherent memory is available
327 static int pxa_init_dma_channel(struct pxa_camera_dev
*pcdev
,
328 struct pxa_buffer
*buf
,
329 struct videobuf_dmabuf
*dma
, int channel
,
331 struct scatterlist
**sg_first
, int *sg_first_ofs
)
333 struct pxa_cam_dma
*pxa_dma
= &buf
->dmas
[channel
];
334 struct device
*dev
= pcdev
->soc_host
.v4l2_dev
.dev
;
335 struct scatterlist
*sg
;
336 int i
, offset
, sglen
;
337 int dma_len
= 0, xfer_len
= 0;
340 dma_free_coherent(dev
, pxa_dma
->sg_size
,
341 pxa_dma
->sg_cpu
, pxa_dma
->sg_dma
);
343 sglen
= calculate_dma_sglen(*sg_first
, dma
->sglen
,
344 *sg_first_ofs
, size
);
346 pxa_dma
->sg_size
= (sglen
+ 1) * sizeof(struct pxa_dma_desc
);
347 pxa_dma
->sg_cpu
= dma_alloc_coherent(dev
, pxa_dma
->sg_size
,
348 &pxa_dma
->sg_dma
, GFP_KERNEL
);
349 if (!pxa_dma
->sg_cpu
)
352 pxa_dma
->sglen
= sglen
;
353 offset
= *sg_first_ofs
;
355 dev_dbg(dev
, "DMA: sg_first=%p, sglen=%d, ofs=%d, dma.desc=%x\n",
356 *sg_first
, sglen
, *sg_first_ofs
, pxa_dma
->sg_dma
);
359 for_each_sg(*sg_first
, sg
, sglen
, i
) {
360 dma_len
= sg_dma_len(sg
);
362 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
363 xfer_len
= roundup(min(dma_len
- offset
, size
), 8);
365 size
= max(0, size
- xfer_len
);
367 pxa_dma
->sg_cpu
[i
].dsadr
= pcdev
->res
->start
+ cibr
;
368 pxa_dma
->sg_cpu
[i
].dtadr
= sg_dma_address(sg
) + offset
;
369 pxa_dma
->sg_cpu
[i
].dcmd
=
370 DCMD_FLOWSRC
| DCMD_BURST8
| DCMD_INCTRGADDR
| xfer_len
;
373 pxa_dma
->sg_cpu
[i
].dcmd
|= DCMD_STARTIRQEN
;
375 pxa_dma
->sg_cpu
[i
].ddadr
=
376 pxa_dma
->sg_dma
+ (i
+ 1) * sizeof(struct pxa_dma_desc
);
378 dev_vdbg(dev
, "DMA: desc.%08x->@phys=0x%08x, len=%d\n",
379 pxa_dma
->sg_dma
+ i
* sizeof(struct pxa_dma_desc
),
380 sg_dma_address(sg
) + offset
, xfer_len
);
387 pxa_dma
->sg_cpu
[sglen
].ddadr
= DDADR_STOP
;
388 pxa_dma
->sg_cpu
[sglen
].dcmd
= DCMD_FLOWSRC
| DCMD_BURST8
| DCMD_ENDIRQEN
;
391 * Handle 1 special case :
392 * - in 3 planes (YUV422P format), we might finish with xfer_len equal
393 * to dma_len (end on PAGE boundary). In this case, the sg element
394 * for next plane should be the next after the last used to store the
395 * last scatter gather RAM page
397 if (xfer_len
>= dma_len
) {
398 *sg_first_ofs
= xfer_len
- dma_len
;
399 *sg_first
= sg_next(sg
);
401 *sg_first_ofs
= xfer_len
;
408 static void pxa_videobuf_set_actdma(struct pxa_camera_dev
*pcdev
,
409 struct pxa_buffer
*buf
)
411 buf
->active_dma
= DMA_Y
;
412 if (pcdev
->channels
== 3)
413 buf
->active_dma
|= DMA_U
| DMA_V
;
417 * Please check the DMA prepared buffer structure in :
418 * Documentation/video4linux/pxa_camera.txt
419 * Please check also in pxa_camera_check_link_miss() to understand why DMA chain
420 * modification while DMA chain is running will work anyway.
422 static int pxa_videobuf_prepare(struct videobuf_queue
*vq
,
423 struct videobuf_buffer
*vb
, enum v4l2_field field
)
425 struct soc_camera_device
*icd
= vq
->priv_data
;
426 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
427 struct pxa_camera_dev
*pcdev
= ici
->priv
;
428 struct device
*dev
= pcdev
->soc_host
.v4l2_dev
.dev
;
429 struct pxa_buffer
*buf
= container_of(vb
, struct pxa_buffer
, vb
);
431 int size_y
, size_u
= 0, size_v
= 0;
433 dev_dbg(dev
, "%s (vb=0x%p) 0x%08lx %d\n", __func__
,
434 vb
, vb
->baddr
, vb
->bsize
);
436 /* Added list head initialization on alloc */
437 WARN_ON(!list_empty(&vb
->queue
));
440 /* This can be useful if you want to see if we actually fill
441 * the buffer with something */
442 memset((void *)vb
->baddr
, 0xaa, vb
->bsize
);
445 BUG_ON(NULL
== icd
->current_fmt
);
447 /* I think, in buf_prepare you only have to protect global data,
448 * the actual buffer is yours */
451 if (buf
->fmt
!= icd
->current_fmt
||
452 vb
->width
!= icd
->user_width
||
453 vb
->height
!= icd
->user_height
||
454 vb
->field
!= field
) {
455 buf
->fmt
= icd
->current_fmt
;
456 vb
->width
= icd
->user_width
;
457 vb
->height
= icd
->user_height
;
459 vb
->state
= VIDEOBUF_NEEDS_INIT
;
462 vb
->size
= vb
->width
* vb
->height
* ((buf
->fmt
->depth
+ 7) >> 3);
463 if (0 != vb
->baddr
&& vb
->bsize
< vb
->size
) {
468 if (vb
->state
== VIDEOBUF_NEEDS_INIT
) {
471 struct videobuf_dmabuf
*dma
= videobuf_to_dma(vb
);
472 struct scatterlist
*sg
;
474 ret
= videobuf_iolock(vq
, vb
, NULL
);
478 if (pcdev
->channels
== 3) {
480 size_u
= size_v
= size
/ 4;
487 /* init DMA for Y channel */
488 ret
= pxa_init_dma_channel(pcdev
, buf
, dma
, 0, CIBR0
, size_y
,
491 dev_err(dev
, "DMA initialization for Y/RGB failed\n");
495 /* init DMA for U channel */
497 ret
= pxa_init_dma_channel(pcdev
, buf
, dma
, 1, CIBR1
,
498 size_u
, &sg
, &next_ofs
);
500 dev_err(dev
, "DMA initialization for U failed\n");
504 /* init DMA for V channel */
506 ret
= pxa_init_dma_channel(pcdev
, buf
, dma
, 2, CIBR2
,
507 size_v
, &sg
, &next_ofs
);
509 dev_err(dev
, "DMA initialization for V failed\n");
513 vb
->state
= VIDEOBUF_PREPARED
;
517 pxa_videobuf_set_actdma(pcdev
, buf
);
522 dma_free_coherent(dev
, buf
->dmas
[1].sg_size
,
523 buf
->dmas
[1].sg_cpu
, buf
->dmas
[1].sg_dma
);
525 dma_free_coherent(dev
, buf
->dmas
[0].sg_size
,
526 buf
->dmas
[0].sg_cpu
, buf
->dmas
[0].sg_dma
);
528 free_buffer(vq
, buf
);
535 * pxa_dma_start_channels - start DMA channel for active buffer
536 * @pcdev: pxa camera device
538 * Initialize DMA channels to the beginning of the active video buffer, and
539 * start these channels.
541 static void pxa_dma_start_channels(struct pxa_camera_dev
*pcdev
)
544 struct pxa_buffer
*active
;
546 active
= pcdev
->active
;
548 for (i
= 0; i
< pcdev
->channels
; i
++) {
549 dev_dbg(pcdev
->soc_host
.v4l2_dev
.dev
,
550 "%s (channel=%d) ddadr=%08x\n", __func__
,
551 i
, active
->dmas
[i
].sg_dma
);
552 DDADR(pcdev
->dma_chans
[i
]) = active
->dmas
[i
].sg_dma
;
553 DCSR(pcdev
->dma_chans
[i
]) = DCSR_RUN
;
557 static void pxa_dma_stop_channels(struct pxa_camera_dev
*pcdev
)
561 for (i
= 0; i
< pcdev
->channels
; i
++) {
562 dev_dbg(pcdev
->soc_host
.v4l2_dev
.dev
,
563 "%s (channel=%d)\n", __func__
, i
);
564 DCSR(pcdev
->dma_chans
[i
]) = 0;
568 static void pxa_dma_add_tail_buf(struct pxa_camera_dev
*pcdev
,
569 struct pxa_buffer
*buf
)
572 struct pxa_dma_desc
*buf_last_desc
;
574 for (i
= 0; i
< pcdev
->channels
; i
++) {
575 buf_last_desc
= buf
->dmas
[i
].sg_cpu
+ buf
->dmas
[i
].sglen
;
576 buf_last_desc
->ddadr
= DDADR_STOP
;
578 if (pcdev
->sg_tail
[i
])
579 /* Link the new buffer to the old tail */
580 pcdev
->sg_tail
[i
]->ddadr
= buf
->dmas
[i
].sg_dma
;
582 /* Update the channel tail */
583 pcdev
->sg_tail
[i
] = buf_last_desc
;
588 * pxa_camera_start_capture - start video capturing
589 * @pcdev: camera device
591 * Launch capturing. DMA channels should not be active yet. They should get
592 * activated at the end of frame interrupt, to capture only whole frames, and
593 * never begin the capture of a partial frame.
595 static void pxa_camera_start_capture(struct pxa_camera_dev
*pcdev
)
597 unsigned long cicr0
, cifr
;
599 dev_dbg(pcdev
->soc_host
.v4l2_dev
.dev
, "%s\n", __func__
);
600 /* Reset the FIFOs */
601 cifr
= __raw_readl(pcdev
->base
+ CIFR
) | CIFR_RESET_F
;
602 __raw_writel(cifr
, pcdev
->base
+ CIFR
);
603 /* Enable End-Of-Frame Interrupt */
604 cicr0
= __raw_readl(pcdev
->base
+ CICR0
) | CICR0_ENB
;
605 cicr0
&= ~CICR0_EOFM
;
606 __raw_writel(cicr0
, pcdev
->base
+ CICR0
);
609 static void pxa_camera_stop_capture(struct pxa_camera_dev
*pcdev
)
613 pxa_dma_stop_channels(pcdev
);
615 cicr0
= __raw_readl(pcdev
->base
+ CICR0
) & ~CICR0_ENB
;
616 __raw_writel(cicr0
, pcdev
->base
+ CICR0
);
618 pcdev
->active
= NULL
;
619 dev_dbg(pcdev
->soc_host
.v4l2_dev
.dev
, "%s\n", __func__
);
622 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
623 static void pxa_videobuf_queue(struct videobuf_queue
*vq
,
624 struct videobuf_buffer
*vb
)
626 struct soc_camera_device
*icd
= vq
->priv_data
;
627 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
628 struct pxa_camera_dev
*pcdev
= ici
->priv
;
629 struct pxa_buffer
*buf
= container_of(vb
, struct pxa_buffer
, vb
);
631 dev_dbg(icd
->dev
.parent
, "%s (vb=0x%p) 0x%08lx %d active=%p\n",
632 __func__
, vb
, vb
->baddr
, vb
->bsize
, pcdev
->active
);
634 list_add_tail(&vb
->queue
, &pcdev
->capture
);
636 vb
->state
= VIDEOBUF_ACTIVE
;
637 pxa_dma_add_tail_buf(pcdev
, buf
);
640 pxa_camera_start_capture(pcdev
);
643 static void pxa_videobuf_release(struct videobuf_queue
*vq
,
644 struct videobuf_buffer
*vb
)
646 struct pxa_buffer
*buf
= container_of(vb
, struct pxa_buffer
, vb
);
648 struct soc_camera_device
*icd
= vq
->priv_data
;
649 struct device
*dev
= icd
->dev
.parent
;
651 dev_dbg(dev
, "%s (vb=0x%p) 0x%08lx %d\n", __func__
,
652 vb
, vb
->baddr
, vb
->bsize
);
655 case VIDEOBUF_ACTIVE
:
656 dev_dbg(dev
, "%s (active)\n", __func__
);
658 case VIDEOBUF_QUEUED
:
659 dev_dbg(dev
, "%s (queued)\n", __func__
);
661 case VIDEOBUF_PREPARED
:
662 dev_dbg(dev
, "%s (prepared)\n", __func__
);
665 dev_dbg(dev
, "%s (unknown)\n", __func__
);
670 free_buffer(vq
, buf
);
673 static void pxa_camera_wakeup(struct pxa_camera_dev
*pcdev
,
674 struct videobuf_buffer
*vb
,
675 struct pxa_buffer
*buf
)
679 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
680 list_del_init(&vb
->queue
);
681 vb
->state
= VIDEOBUF_DONE
;
682 do_gettimeofday(&vb
->ts
);
685 dev_dbg(pcdev
->soc_host
.v4l2_dev
.dev
, "%s dequeud buffer (vb=0x%p)\n",
688 if (list_empty(&pcdev
->capture
)) {
689 pxa_camera_stop_capture(pcdev
);
690 for (i
= 0; i
< pcdev
->channels
; i
++)
691 pcdev
->sg_tail
[i
] = NULL
;
695 pcdev
->active
= list_entry(pcdev
->capture
.next
,
696 struct pxa_buffer
, vb
.queue
);
700 * pxa_camera_check_link_miss - check missed DMA linking
701 * @pcdev: camera device
703 * The DMA chaining is done with DMA running. This means a tiny temporal window
704 * remains, where a buffer is queued on the chain, while the chain is already
705 * stopped. This means the tailed buffer would never be transfered by DMA.
706 * This function restarts the capture for this corner case, where :
707 * - DADR() == DADDR_STOP
708 * - a videobuffer is queued on the pcdev->capture list
710 * Please check the "DMA hot chaining timeslice issue" in
711 * Documentation/video4linux/pxa_camera.txt
713 * Context: should only be called within the dma irq handler
715 static void pxa_camera_check_link_miss(struct pxa_camera_dev
*pcdev
)
717 int i
, is_dma_stopped
= 1;
719 for (i
= 0; i
< pcdev
->channels
; i
++)
720 if (DDADR(pcdev
->dma_chans
[i
]) != DDADR_STOP
)
722 dev_dbg(pcdev
->soc_host
.v4l2_dev
.dev
,
723 "%s : top queued buffer=%p, dma_stopped=%d\n",
724 __func__
, pcdev
->active
, is_dma_stopped
);
725 if (pcdev
->active
&& is_dma_stopped
)
726 pxa_camera_start_capture(pcdev
);
729 static void pxa_camera_dma_irq(int channel
, struct pxa_camera_dev
*pcdev
,
730 enum pxa_camera_active_dma act_dma
)
732 struct device
*dev
= pcdev
->soc_host
.v4l2_dev
.dev
;
733 struct pxa_buffer
*buf
;
735 u32 status
, camera_status
, overrun
;
736 struct videobuf_buffer
*vb
;
738 spin_lock_irqsave(&pcdev
->lock
, flags
);
740 status
= DCSR(channel
);
741 DCSR(channel
) = status
;
743 camera_status
= __raw_readl(pcdev
->base
+ CISR
);
744 overrun
= CISR_IFO_0
;
745 if (pcdev
->channels
== 3)
746 overrun
|= CISR_IFO_1
| CISR_IFO_2
;
748 if (status
& DCSR_BUSERR
) {
749 dev_err(dev
, "DMA Bus Error IRQ!\n");
753 if (!(status
& (DCSR_ENDINTR
| DCSR_STARTINTR
))) {
754 dev_err(dev
, "Unknown DMA IRQ source, status: 0x%08x\n",
760 * pcdev->active should not be NULL in DMA irq handler.
762 * But there is one corner case : if capture was stopped due to an
763 * overrun of channel 1, and at that same channel 2 was completed.
765 * When handling the overrun in DMA irq for channel 1, we'll stop the
766 * capture and restart it (and thus set pcdev->active to NULL). But the
767 * DMA irq handler will already be pending for channel 2. So on entering
768 * the DMA irq handler for channel 2 there will be no active buffer, yet
774 vb
= &pcdev
->active
->vb
;
775 buf
= container_of(vb
, struct pxa_buffer
, vb
);
776 WARN_ON(buf
->inwork
|| list_empty(&vb
->queue
));
778 dev_dbg(dev
, "%s channel=%d %s%s(vb=0x%p) dma.desc=%x\n",
779 __func__
, channel
, status
& DCSR_STARTINTR
? "SOF " : "",
780 status
& DCSR_ENDINTR
? "EOF " : "", vb
, DDADR(channel
));
782 if (status
& DCSR_ENDINTR
) {
784 * It's normal if the last frame creates an overrun, as there
785 * are no more DMA descriptors to fetch from QCI fifos
787 if (camera_status
& overrun
&&
788 !list_is_last(pcdev
->capture
.next
, &pcdev
->capture
)) {
789 dev_dbg(dev
, "FIFO overrun! CISR: %x\n",
791 pxa_camera_stop_capture(pcdev
);
792 pxa_camera_start_capture(pcdev
);
795 buf
->active_dma
&= ~act_dma
;
796 if (!buf
->active_dma
) {
797 pxa_camera_wakeup(pcdev
, vb
, buf
);
798 pxa_camera_check_link_miss(pcdev
);
803 spin_unlock_irqrestore(&pcdev
->lock
, flags
);
806 static void pxa_camera_dma_irq_y(int channel
, void *data
)
808 struct pxa_camera_dev
*pcdev
= data
;
809 pxa_camera_dma_irq(channel
, pcdev
, DMA_Y
);
812 static void pxa_camera_dma_irq_u(int channel
, void *data
)
814 struct pxa_camera_dev
*pcdev
= data
;
815 pxa_camera_dma_irq(channel
, pcdev
, DMA_U
);
818 static void pxa_camera_dma_irq_v(int channel
, void *data
)
820 struct pxa_camera_dev
*pcdev
= data
;
821 pxa_camera_dma_irq(channel
, pcdev
, DMA_V
);
824 static struct videobuf_queue_ops pxa_videobuf_ops
= {
825 .buf_setup
= pxa_videobuf_setup
,
826 .buf_prepare
= pxa_videobuf_prepare
,
827 .buf_queue
= pxa_videobuf_queue
,
828 .buf_release
= pxa_videobuf_release
,
831 static void pxa_camera_init_videobuf(struct videobuf_queue
*q
,
832 struct soc_camera_device
*icd
)
834 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
835 struct pxa_camera_dev
*pcdev
= ici
->priv
;
837 /* We must pass NULL as dev pointer, then all pci_* dma operations
838 * transform to normal dma_* ones. */
839 videobuf_queue_sg_init(q
, &pxa_videobuf_ops
, NULL
, &pcdev
->lock
,
840 V4L2_BUF_TYPE_VIDEO_CAPTURE
, V4L2_FIELD_NONE
,
841 sizeof(struct pxa_buffer
), icd
);
844 static u32
mclk_get_divisor(struct platform_device
*pdev
,
845 struct pxa_camera_dev
*pcdev
)
847 unsigned long mclk
= pcdev
->mclk
;
848 struct device
*dev
= &pdev
->dev
;
850 unsigned long lcdclk
;
852 lcdclk
= clk_get_rate(pcdev
->clk
);
853 pcdev
->ciclk
= lcdclk
;
855 /* mclk <= ciclk / 4 (27.4.2) */
856 if (mclk
> lcdclk
/ 4) {
858 dev_warn(dev
, "Limiting master clock to %lu\n", mclk
);
861 /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
862 div
= (lcdclk
+ 2 * mclk
- 1) / (2 * mclk
) - 1;
864 /* If we're not supplying MCLK, leave it at 0 */
865 if (pcdev
->platform_flags
& PXA_CAMERA_MCLK_EN
)
866 pcdev
->mclk
= lcdclk
/ (2 * (div
+ 1));
868 dev_dbg(dev
, "LCD clock %luHz, target freq %luHz, divisor %u\n",
874 static void recalculate_fifo_timeout(struct pxa_camera_dev
*pcdev
,
877 /* We want a timeout > 1 pixel time, not ">=" */
878 u32 ciclk_per_pixel
= pcdev
->ciclk
/ pclk
+ 1;
880 __raw_writel(ciclk_per_pixel
, pcdev
->base
+ CITOR
);
883 static void pxa_camera_activate(struct pxa_camera_dev
*pcdev
)
885 struct pxacamera_platform_data
*pdata
= pcdev
->pdata
;
886 struct device
*dev
= pcdev
->soc_host
.v4l2_dev
.dev
;
889 dev_dbg(dev
, "Registered platform device at %p data %p\n",
892 if (pdata
&& pdata
->init
) {
893 dev_dbg(dev
, "%s: Init gpios\n", __func__
);
897 /* disable all interrupts */
898 __raw_writel(0x3ff, pcdev
->base
+ CICR0
);
900 if (pcdev
->platform_flags
& PXA_CAMERA_PCLK_EN
)
901 cicr4
|= CICR4_PCLK_EN
;
902 if (pcdev
->platform_flags
& PXA_CAMERA_MCLK_EN
)
903 cicr4
|= CICR4_MCLK_EN
;
904 if (pcdev
->platform_flags
& PXA_CAMERA_PCP
)
906 if (pcdev
->platform_flags
& PXA_CAMERA_HSP
)
908 if (pcdev
->platform_flags
& PXA_CAMERA_VSP
)
911 __raw_writel(pcdev
->mclk_divisor
| cicr4
, pcdev
->base
+ CICR4
);
913 if (pcdev
->platform_flags
& PXA_CAMERA_MCLK_EN
)
914 /* Initialise the timeout under the assumption pclk = mclk */
915 recalculate_fifo_timeout(pcdev
, pcdev
->mclk
);
917 /* "Safe default" - 13MHz */
918 recalculate_fifo_timeout(pcdev
, 13000000);
920 clk_enable(pcdev
->clk
);
923 static void pxa_camera_deactivate(struct pxa_camera_dev
*pcdev
)
925 clk_disable(pcdev
->clk
);
928 static irqreturn_t
pxa_camera_irq(int irq
, void *data
)
930 struct pxa_camera_dev
*pcdev
= data
;
931 unsigned long status
, cicr0
;
932 struct pxa_buffer
*buf
;
933 struct videobuf_buffer
*vb
;
935 status
= __raw_readl(pcdev
->base
+ CISR
);
936 dev_dbg(pcdev
->soc_host
.v4l2_dev
.dev
,
937 "Camera interrupt status 0x%lx\n", status
);
942 __raw_writel(status
, pcdev
->base
+ CISR
);
944 if (status
& CISR_EOF
) {
945 pcdev
->active
= list_first_entry(&pcdev
->capture
,
946 struct pxa_buffer
, vb
.queue
);
947 vb
= &pcdev
->active
->vb
;
948 buf
= container_of(vb
, struct pxa_buffer
, vb
);
949 pxa_videobuf_set_actdma(pcdev
, buf
);
951 pxa_dma_start_channels(pcdev
);
953 cicr0
= __raw_readl(pcdev
->base
+ CICR0
) | CICR0_EOFM
;
954 __raw_writel(cicr0
, pcdev
->base
+ CICR0
);
961 * The following two functions absolutely depend on the fact, that
962 * there can be only one camera on PXA quick capture interface
963 * Called with .video_lock held
965 static int pxa_camera_add_device(struct soc_camera_device
*icd
)
967 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
968 struct pxa_camera_dev
*pcdev
= ici
->priv
;
973 pxa_camera_activate(pcdev
);
977 dev_info(icd
->dev
.parent
, "PXA Camera driver attached to camera %d\n",
983 /* Called with .video_lock held */
984 static void pxa_camera_remove_device(struct soc_camera_device
*icd
)
986 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
987 struct pxa_camera_dev
*pcdev
= ici
->priv
;
989 BUG_ON(icd
!= pcdev
->icd
);
991 dev_info(icd
->dev
.parent
, "PXA Camera driver detached from camera %d\n",
994 /* disable capture, disable interrupts */
995 __raw_writel(0x3ff, pcdev
->base
+ CICR0
);
997 /* Stop DMA engine */
998 DCSR(pcdev
->dma_chans
[0]) = 0;
999 DCSR(pcdev
->dma_chans
[1]) = 0;
1000 DCSR(pcdev
->dma_chans
[2]) = 0;
1002 pxa_camera_deactivate(pcdev
);
1007 static int test_platform_param(struct pxa_camera_dev
*pcdev
,
1008 unsigned char buswidth
, unsigned long *flags
)
1011 * Platform specified synchronization and pixel clock polarities are
1012 * only a recommendation and are only used during probing. The PXA270
1013 * quick capture interface supports both.
1015 *flags
= (pcdev
->platform_flags
& PXA_CAMERA_MASTER
?
1016 SOCAM_MASTER
: SOCAM_SLAVE
) |
1017 SOCAM_HSYNC_ACTIVE_HIGH
|
1018 SOCAM_HSYNC_ACTIVE_LOW
|
1019 SOCAM_VSYNC_ACTIVE_HIGH
|
1020 SOCAM_VSYNC_ACTIVE_LOW
|
1021 SOCAM_DATA_ACTIVE_HIGH
|
1022 SOCAM_PCLK_SAMPLE_RISING
|
1023 SOCAM_PCLK_SAMPLE_FALLING
;
1025 /* If requested data width is supported by the platform, use it */
1028 if (!(pcdev
->platform_flags
& PXA_CAMERA_DATAWIDTH_10
))
1030 *flags
|= SOCAM_DATAWIDTH_10
;
1033 if (!(pcdev
->platform_flags
& PXA_CAMERA_DATAWIDTH_9
))
1035 *flags
|= SOCAM_DATAWIDTH_9
;
1038 if (!(pcdev
->platform_flags
& PXA_CAMERA_DATAWIDTH_8
))
1040 *flags
|= SOCAM_DATAWIDTH_8
;
1049 static void pxa_camera_setup_cicr(struct soc_camera_device
*icd
,
1050 unsigned long flags
, __u32 pixfmt
)
1052 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1053 struct pxa_camera_dev
*pcdev
= ici
->priv
;
1054 unsigned long dw
, bpp
;
1055 u32 cicr0
, cicr1
, cicr2
, cicr3
, cicr4
= 0;
1057 /* Datawidth is now guaranteed to be equal to one of the three values.
1058 * We fix bit-per-pixel equal to data-width... */
1059 switch (flags
& SOCAM_DATAWIDTH_MASK
) {
1060 case SOCAM_DATAWIDTH_10
:
1064 case SOCAM_DATAWIDTH_9
:
1069 /* Actually it can only be 8 now,
1070 * default is just to silence compiler warnings */
1071 case SOCAM_DATAWIDTH_8
:
1076 if (pcdev
->platform_flags
& PXA_CAMERA_PCLK_EN
)
1077 cicr4
|= CICR4_PCLK_EN
;
1078 if (pcdev
->platform_flags
& PXA_CAMERA_MCLK_EN
)
1079 cicr4
|= CICR4_MCLK_EN
;
1080 if (flags
& SOCAM_PCLK_SAMPLE_FALLING
)
1082 if (flags
& SOCAM_HSYNC_ACTIVE_LOW
)
1084 if (flags
& SOCAM_VSYNC_ACTIVE_LOW
)
1087 cicr0
= __raw_readl(pcdev
->base
+ CICR0
);
1088 if (cicr0
& CICR0_ENB
)
1089 __raw_writel(cicr0
& ~CICR0_ENB
, pcdev
->base
+ CICR0
);
1091 cicr1
= CICR1_PPL_VAL(icd
->user_width
- 1) | bpp
| dw
;
1094 case V4L2_PIX_FMT_YUV422P
:
1095 pcdev
->channels
= 3;
1096 cicr1
|= CICR1_YCBCR_F
;
1098 * Normally, pxa bus wants as input UYVY format. We allow all
1099 * reorderings of the YUV422 format, as no processing is done,
1100 * and the YUV stream is just passed through without any
1101 * transformation. Note that UYVY is the only format that
1102 * should be used if pxa framebuffer Overlay2 is used.
1104 case V4L2_PIX_FMT_UYVY
:
1105 case V4L2_PIX_FMT_VYUY
:
1106 case V4L2_PIX_FMT_YUYV
:
1107 case V4L2_PIX_FMT_YVYU
:
1108 cicr1
|= CICR1_COLOR_SP_VAL(2);
1110 case V4L2_PIX_FMT_RGB555
:
1111 cicr1
|= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
1112 CICR1_TBIT
| CICR1_COLOR_SP_VAL(1);
1114 case V4L2_PIX_FMT_RGB565
:
1115 cicr1
|= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
1120 cicr3
= CICR3_LPF_VAL(icd
->user_height
- 1) |
1121 CICR3_BFW_VAL(min((unsigned short)255, icd
->y_skip_top
));
1122 cicr4
|= pcdev
->mclk_divisor
;
1124 __raw_writel(cicr1
, pcdev
->base
+ CICR1
);
1125 __raw_writel(cicr2
, pcdev
->base
+ CICR2
);
1126 __raw_writel(cicr3
, pcdev
->base
+ CICR3
);
1127 __raw_writel(cicr4
, pcdev
->base
+ CICR4
);
1129 /* CIF interrupts are not used, only DMA */
1130 cicr0
= (cicr0
& CICR0_ENB
) | (pcdev
->platform_flags
& PXA_CAMERA_MASTER
?
1131 CICR0_SIM_MP
: (CICR0_SL_CAP_EN
| CICR0_SIM_SP
));
1132 cicr0
|= CICR0_DMAEN
| CICR0_IRQ_MASK
;
1133 __raw_writel(cicr0
, pcdev
->base
+ CICR0
);
1136 static int pxa_camera_set_bus_param(struct soc_camera_device
*icd
, __u32 pixfmt
)
1138 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1139 struct pxa_camera_dev
*pcdev
= ici
->priv
;
1140 unsigned long bus_flags
, camera_flags
, common_flags
;
1141 int ret
= test_platform_param(pcdev
, icd
->buswidth
, &bus_flags
);
1142 struct pxa_cam
*cam
= icd
->host_priv
;
1147 camera_flags
= icd
->ops
->query_bus_param(icd
);
1149 common_flags
= soc_camera_bus_param_compatible(camera_flags
, bus_flags
);
1153 pcdev
->channels
= 1;
1155 /* Make choises, based on platform preferences */
1156 if ((common_flags
& SOCAM_HSYNC_ACTIVE_HIGH
) &&
1157 (common_flags
& SOCAM_HSYNC_ACTIVE_LOW
)) {
1158 if (pcdev
->platform_flags
& PXA_CAMERA_HSP
)
1159 common_flags
&= ~SOCAM_HSYNC_ACTIVE_HIGH
;
1161 common_flags
&= ~SOCAM_HSYNC_ACTIVE_LOW
;
1164 if ((common_flags
& SOCAM_VSYNC_ACTIVE_HIGH
) &&
1165 (common_flags
& SOCAM_VSYNC_ACTIVE_LOW
)) {
1166 if (pcdev
->platform_flags
& PXA_CAMERA_VSP
)
1167 common_flags
&= ~SOCAM_VSYNC_ACTIVE_HIGH
;
1169 common_flags
&= ~SOCAM_VSYNC_ACTIVE_LOW
;
1172 if ((common_flags
& SOCAM_PCLK_SAMPLE_RISING
) &&
1173 (common_flags
& SOCAM_PCLK_SAMPLE_FALLING
)) {
1174 if (pcdev
->platform_flags
& PXA_CAMERA_PCP
)
1175 common_flags
&= ~SOCAM_PCLK_SAMPLE_RISING
;
1177 common_flags
&= ~SOCAM_PCLK_SAMPLE_FALLING
;
1180 cam
->flags
= common_flags
;
1182 ret
= icd
->ops
->set_bus_param(icd
, common_flags
);
1186 pxa_camera_setup_cicr(icd
, common_flags
, pixfmt
);
1191 static int pxa_camera_try_bus_param(struct soc_camera_device
*icd
,
1192 unsigned char buswidth
)
1194 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1195 struct pxa_camera_dev
*pcdev
= ici
->priv
;
1196 unsigned long bus_flags
, camera_flags
;
1197 int ret
= test_platform_param(pcdev
, buswidth
, &bus_flags
);
1202 camera_flags
= icd
->ops
->query_bus_param(icd
);
1204 return soc_camera_bus_param_compatible(camera_flags
, bus_flags
) ? 0 : -EINVAL
;
1207 static const struct soc_camera_data_format pxa_camera_formats
[] = {
1209 .name
= "Planar YUV422 16 bit",
1211 .fourcc
= V4L2_PIX_FMT_YUV422P
,
1212 .colorspace
= V4L2_COLORSPACE_JPEG
,
1216 static bool buswidth_supported(struct soc_camera_device
*icd
, int depth
)
1218 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1219 struct pxa_camera_dev
*pcdev
= ici
->priv
;
1223 return !!(pcdev
->platform_flags
& PXA_CAMERA_DATAWIDTH_8
);
1225 return !!(pcdev
->platform_flags
& PXA_CAMERA_DATAWIDTH_9
);
1227 return !!(pcdev
->platform_flags
& PXA_CAMERA_DATAWIDTH_10
);
1232 static int required_buswidth(const struct soc_camera_data_format
*fmt
)
1234 switch (fmt
->fourcc
) {
1235 case V4L2_PIX_FMT_UYVY
:
1236 case V4L2_PIX_FMT_VYUY
:
1237 case V4L2_PIX_FMT_YUYV
:
1238 case V4L2_PIX_FMT_YVYU
:
1239 case V4L2_PIX_FMT_RGB565
:
1240 case V4L2_PIX_FMT_RGB555
:
1247 static int pxa_camera_get_formats(struct soc_camera_device
*icd
, int idx
,
1248 struct soc_camera_format_xlate
*xlate
)
1250 struct device
*dev
= icd
->dev
.parent
;
1251 int formats
= 0, buswidth
, ret
;
1252 struct pxa_cam
*cam
;
1254 buswidth
= required_buswidth(icd
->formats
+ idx
);
1256 if (!buswidth_supported(icd
, buswidth
))
1259 ret
= pxa_camera_try_bus_param(icd
, buswidth
);
1263 if (!icd
->host_priv
) {
1264 cam
= kzalloc(sizeof(*cam
), GFP_KERNEL
);
1268 icd
->host_priv
= cam
;
1270 cam
= icd
->host_priv
;
1273 switch (icd
->formats
[idx
].fourcc
) {
1274 case V4L2_PIX_FMT_UYVY
:
1277 xlate
->host_fmt
= &pxa_camera_formats
[0];
1278 xlate
->cam_fmt
= icd
->formats
+ idx
;
1279 xlate
->buswidth
= buswidth
;
1281 dev_dbg(dev
, "Providing format %s using %s\n",
1282 pxa_camera_formats
[0].name
,
1283 icd
->formats
[idx
].name
);
1285 case V4L2_PIX_FMT_VYUY
:
1286 case V4L2_PIX_FMT_YUYV
:
1287 case V4L2_PIX_FMT_YVYU
:
1288 case V4L2_PIX_FMT_RGB565
:
1289 case V4L2_PIX_FMT_RGB555
:
1292 xlate
->host_fmt
= icd
->formats
+ idx
;
1293 xlate
->cam_fmt
= icd
->formats
+ idx
;
1294 xlate
->buswidth
= buswidth
;
1296 dev_dbg(dev
, "Providing format %s packed\n",
1297 icd
->formats
[idx
].name
);
1301 /* Generic pass-through */
1304 xlate
->host_fmt
= icd
->formats
+ idx
;
1305 xlate
->cam_fmt
= icd
->formats
+ idx
;
1306 xlate
->buswidth
= icd
->formats
[idx
].depth
;
1309 "Providing format %s in pass-through mode\n",
1310 icd
->formats
[idx
].name
);
1317 static void pxa_camera_put_formats(struct soc_camera_device
*icd
)
1319 kfree(icd
->host_priv
);
1320 icd
->host_priv
= NULL
;
1323 static int pxa_camera_check_frame(struct v4l2_pix_format
*pix
)
1325 /* limit to pxa hardware capabilities */
1326 return pix
->height
< 32 || pix
->height
> 2048 || pix
->width
< 48 ||
1327 pix
->width
> 2048 || (pix
->width
& 0x01);
1330 static int pxa_camera_set_crop(struct soc_camera_device
*icd
,
1331 struct v4l2_crop
*a
)
1333 struct v4l2_rect
*rect
= &a
->c
;
1334 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1335 struct pxa_camera_dev
*pcdev
= ici
->priv
;
1336 struct device
*dev
= icd
->dev
.parent
;
1337 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1338 struct soc_camera_sense sense
= {
1339 .master_clock
= pcdev
->mclk
,
1340 .pixel_clock_max
= pcdev
->ciclk
/ 4,
1342 struct v4l2_format f
;
1343 struct v4l2_pix_format
*pix
= &f
.fmt
.pix
, pix_tmp
;
1344 struct pxa_cam
*cam
= icd
->host_priv
;
1347 /* If PCLK is used to latch data from the sensor, check sense */
1348 if (pcdev
->platform_flags
& PXA_CAMERA_PCLK_EN
)
1349 icd
->sense
= &sense
;
1351 ret
= v4l2_subdev_call(sd
, video
, s_crop
, a
);
1356 dev_warn(dev
, "Failed to crop to %ux%u@%u:%u\n",
1357 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
1361 f
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1363 ret
= v4l2_subdev_call(sd
, video
, g_fmt
, &f
);
1368 if (pxa_camera_check_frame(pix
)) {
1370 * Camera cropping produced a frame beyond our capabilities.
1371 * FIXME: just extract a subframe, that we can process.
1373 v4l_bound_align_image(&pix
->width
, 48, 2048, 1,
1374 &pix
->height
, 32, 2048, 0,
1375 icd
->current_fmt
->fourcc
== V4L2_PIX_FMT_YUV422P
?
1377 ret
= v4l2_subdev_call(sd
, video
, s_fmt
, &f
);
1381 if (pxa_camera_check_frame(pix
)) {
1382 dev_warn(icd
->dev
.parent
,
1383 "Inconsistent state. Use S_FMT to repair\n");
1388 if (sense
.flags
& SOCAM_SENSE_PCLK_CHANGED
) {
1389 if (sense
.pixel_clock
> sense
.pixel_clock_max
) {
1391 "pixel clock %lu set by the camera too high!",
1395 recalculate_fifo_timeout(pcdev
, sense
.pixel_clock
);
1398 icd
->user_width
= pix
->width
;
1399 icd
->user_height
= pix
->height
;
1401 pxa_camera_setup_cicr(icd
, cam
->flags
, icd
->current_fmt
->fourcc
);
1406 static int pxa_camera_set_fmt(struct soc_camera_device
*icd
,
1407 struct v4l2_format
*f
)
1409 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1410 struct pxa_camera_dev
*pcdev
= ici
->priv
;
1411 struct device
*dev
= icd
->dev
.parent
;
1412 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1413 const struct soc_camera_data_format
*cam_fmt
= NULL
;
1414 const struct soc_camera_format_xlate
*xlate
= NULL
;
1415 struct soc_camera_sense sense
= {
1416 .master_clock
= pcdev
->mclk
,
1417 .pixel_clock_max
= pcdev
->ciclk
/ 4,
1419 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1420 struct v4l2_format cam_f
= *f
;
1423 xlate
= soc_camera_xlate_by_fourcc(icd
, pix
->pixelformat
);
1425 dev_warn(dev
, "Format %x not found\n", pix
->pixelformat
);
1429 cam_fmt
= xlate
->cam_fmt
;
1431 /* If PCLK is used to latch data from the sensor, check sense */
1432 if (pcdev
->platform_flags
& PXA_CAMERA_PCLK_EN
)
1433 icd
->sense
= &sense
;
1435 cam_f
.fmt
.pix
.pixelformat
= cam_fmt
->fourcc
;
1436 ret
= v4l2_subdev_call(sd
, video
, s_fmt
, &cam_f
);
1437 cam_f
.fmt
.pix
.pixelformat
= pix
->pixelformat
;
1438 *pix
= cam_f
.fmt
.pix
;
1443 dev_warn(dev
, "Failed to configure for format %x\n",
1445 } else if (pxa_camera_check_frame(pix
)) {
1447 "Camera driver produced an unsupported frame %dx%d\n",
1448 pix
->width
, pix
->height
);
1450 } else if (sense
.flags
& SOCAM_SENSE_PCLK_CHANGED
) {
1451 if (sense
.pixel_clock
> sense
.pixel_clock_max
) {
1453 "pixel clock %lu set by the camera too high!",
1457 recalculate_fifo_timeout(pcdev
, sense
.pixel_clock
);
1461 icd
->buswidth
= xlate
->buswidth
;
1462 icd
->current_fmt
= xlate
->host_fmt
;
1468 static int pxa_camera_try_fmt(struct soc_camera_device
*icd
,
1469 struct v4l2_format
*f
)
1471 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1472 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
1473 const struct soc_camera_format_xlate
*xlate
;
1474 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1475 __u32 pixfmt
= pix
->pixelformat
;
1476 enum v4l2_field field
;
1479 xlate
= soc_camera_xlate_by_fourcc(icd
, pixfmt
);
1481 dev_warn(ici
->v4l2_dev
.dev
, "Format %x not found\n", pixfmt
);
1486 * Limit to pxa hardware capabilities. YUV422P planar format requires
1487 * images size to be a multiple of 16 bytes. If not, zeros will be
1488 * inserted between Y and U planes, and U and V planes, which violates
1489 * the YUV422P standard.
1491 v4l_bound_align_image(&pix
->width
, 48, 2048, 1,
1492 &pix
->height
, 32, 2048, 0,
1493 pixfmt
== V4L2_PIX_FMT_YUV422P
? 4 : 0);
1495 pix
->bytesperline
= pix
->width
*
1496 DIV_ROUND_UP(xlate
->host_fmt
->depth
, 8);
1497 pix
->sizeimage
= pix
->height
* pix
->bytesperline
;
1499 /* camera has to see its format, but the user the original one */
1500 pix
->pixelformat
= xlate
->cam_fmt
->fourcc
;
1501 /* limit to sensor capabilities */
1502 ret
= v4l2_subdev_call(sd
, video
, try_fmt
, f
);
1503 pix
->pixelformat
= pixfmt
;
1507 if (field
== V4L2_FIELD_ANY
) {
1508 pix
->field
= V4L2_FIELD_NONE
;
1509 } else if (field
!= V4L2_FIELD_NONE
) {
1510 dev_err(icd
->dev
.parent
, "Field type %d unsupported.\n", field
);
1517 static int pxa_camera_reqbufs(struct soc_camera_file
*icf
,
1518 struct v4l2_requestbuffers
*p
)
1522 /* This is for locking debugging only. I removed spinlocks and now I
1523 * check whether .prepare is ever called on a linked buffer, or whether
1524 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1525 * it hadn't triggered */
1526 for (i
= 0; i
< p
->count
; i
++) {
1527 struct pxa_buffer
*buf
= container_of(icf
->vb_vidq
.bufs
[i
],
1528 struct pxa_buffer
, vb
);
1530 INIT_LIST_HEAD(&buf
->vb
.queue
);
1536 static unsigned int pxa_camera_poll(struct file
*file
, poll_table
*pt
)
1538 struct soc_camera_file
*icf
= file
->private_data
;
1539 struct pxa_buffer
*buf
;
1541 buf
= list_entry(icf
->vb_vidq
.stream
.next
, struct pxa_buffer
,
1544 poll_wait(file
, &buf
->vb
.done
, pt
);
1546 if (buf
->vb
.state
== VIDEOBUF_DONE
||
1547 buf
->vb
.state
== VIDEOBUF_ERROR
)
1548 return POLLIN
|POLLRDNORM
;
1553 static int pxa_camera_querycap(struct soc_camera_host
*ici
,
1554 struct v4l2_capability
*cap
)
1556 /* cap->name is set by the firendly caller:-> */
1557 strlcpy(cap
->card
, pxa_cam_driver_description
, sizeof(cap
->card
));
1558 cap
->version
= PXA_CAM_VERSION_CODE
;
1559 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
1564 static int pxa_camera_suspend(struct soc_camera_device
*icd
, pm_message_t state
)
1566 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1567 struct pxa_camera_dev
*pcdev
= ici
->priv
;
1570 pcdev
->save_cicr
[i
++] = __raw_readl(pcdev
->base
+ CICR0
);
1571 pcdev
->save_cicr
[i
++] = __raw_readl(pcdev
->base
+ CICR1
);
1572 pcdev
->save_cicr
[i
++] = __raw_readl(pcdev
->base
+ CICR2
);
1573 pcdev
->save_cicr
[i
++] = __raw_readl(pcdev
->base
+ CICR3
);
1574 pcdev
->save_cicr
[i
++] = __raw_readl(pcdev
->base
+ CICR4
);
1576 if ((pcdev
->icd
) && (pcdev
->icd
->ops
->suspend
))
1577 ret
= pcdev
->icd
->ops
->suspend(pcdev
->icd
, state
);
1582 static int pxa_camera_resume(struct soc_camera_device
*icd
)
1584 struct soc_camera_host
*ici
= to_soc_camera_host(icd
->dev
.parent
);
1585 struct pxa_camera_dev
*pcdev
= ici
->priv
;
1588 DRCMR(68) = pcdev
->dma_chans
[0] | DRCMR_MAPVLD
;
1589 DRCMR(69) = pcdev
->dma_chans
[1] | DRCMR_MAPVLD
;
1590 DRCMR(70) = pcdev
->dma_chans
[2] | DRCMR_MAPVLD
;
1592 __raw_writel(pcdev
->save_cicr
[i
++] & ~CICR0_ENB
, pcdev
->base
+ CICR0
);
1593 __raw_writel(pcdev
->save_cicr
[i
++], pcdev
->base
+ CICR1
);
1594 __raw_writel(pcdev
->save_cicr
[i
++], pcdev
->base
+ CICR2
);
1595 __raw_writel(pcdev
->save_cicr
[i
++], pcdev
->base
+ CICR3
);
1596 __raw_writel(pcdev
->save_cicr
[i
++], pcdev
->base
+ CICR4
);
1598 if ((pcdev
->icd
) && (pcdev
->icd
->ops
->resume
))
1599 ret
= pcdev
->icd
->ops
->resume(pcdev
->icd
);
1601 /* Restart frame capture if active buffer exists */
1602 if (!ret
&& pcdev
->active
)
1603 pxa_camera_start_capture(pcdev
);
1608 static struct soc_camera_host_ops pxa_soc_camera_host_ops
= {
1609 .owner
= THIS_MODULE
,
1610 .add
= pxa_camera_add_device
,
1611 .remove
= pxa_camera_remove_device
,
1612 .suspend
= pxa_camera_suspend
,
1613 .resume
= pxa_camera_resume
,
1614 .set_crop
= pxa_camera_set_crop
,
1615 .get_formats
= pxa_camera_get_formats
,
1616 .put_formats
= pxa_camera_put_formats
,
1617 .set_fmt
= pxa_camera_set_fmt
,
1618 .try_fmt
= pxa_camera_try_fmt
,
1619 .init_videobuf
= pxa_camera_init_videobuf
,
1620 .reqbufs
= pxa_camera_reqbufs
,
1621 .poll
= pxa_camera_poll
,
1622 .querycap
= pxa_camera_querycap
,
1623 .set_bus_param
= pxa_camera_set_bus_param
,
1626 static int __devinit
pxa_camera_probe(struct platform_device
*pdev
)
1628 struct pxa_camera_dev
*pcdev
;
1629 struct resource
*res
;
1634 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1635 irq
= platform_get_irq(pdev
, 0);
1636 if (!res
|| irq
< 0) {
1641 pcdev
= kzalloc(sizeof(*pcdev
), GFP_KERNEL
);
1643 dev_err(&pdev
->dev
, "Could not allocate pcdev\n");
1648 pcdev
->clk
= clk_get(&pdev
->dev
, NULL
);
1649 if (IS_ERR(pcdev
->clk
)) {
1650 err
= PTR_ERR(pcdev
->clk
);
1656 pcdev
->pdata
= pdev
->dev
.platform_data
;
1657 pcdev
->platform_flags
= pcdev
->pdata
->flags
;
1658 if (!(pcdev
->platform_flags
& (PXA_CAMERA_DATAWIDTH_8
|
1659 PXA_CAMERA_DATAWIDTH_9
| PXA_CAMERA_DATAWIDTH_10
))) {
1660 /* Platform hasn't set available data widths. This is bad.
1661 * Warn and use a default. */
1662 dev_warn(&pdev
->dev
, "WARNING! Platform hasn't set available "
1663 "data widths, using default 10 bit\n");
1664 pcdev
->platform_flags
|= PXA_CAMERA_DATAWIDTH_10
;
1666 pcdev
->mclk
= pcdev
->pdata
->mclk_10khz
* 10000;
1668 dev_warn(&pdev
->dev
,
1669 "mclk == 0! Please, fix your platform data. "
1670 "Using default 20MHz\n");
1671 pcdev
->mclk
= 20000000;
1674 pcdev
->mclk_divisor
= mclk_get_divisor(pdev
, pcdev
);
1676 INIT_LIST_HEAD(&pcdev
->capture
);
1677 spin_lock_init(&pcdev
->lock
);
1680 * Request the regions.
1682 if (!request_mem_region(res
->start
, resource_size(res
),
1683 PXA_CAM_DRV_NAME
)) {
1688 base
= ioremap(res
->start
, resource_size(res
));
1697 err
= pxa_request_dma("CI_Y", DMA_PRIO_HIGH
,
1698 pxa_camera_dma_irq_y
, pcdev
);
1700 dev_err(&pdev
->dev
, "Can't request DMA for Y\n");
1703 pcdev
->dma_chans
[0] = err
;
1704 dev_dbg(&pdev
->dev
, "got DMA channel %d\n", pcdev
->dma_chans
[0]);
1706 err
= pxa_request_dma("CI_U", DMA_PRIO_HIGH
,
1707 pxa_camera_dma_irq_u
, pcdev
);
1709 dev_err(&pdev
->dev
, "Can't request DMA for U\n");
1710 goto exit_free_dma_y
;
1712 pcdev
->dma_chans
[1] = err
;
1713 dev_dbg(&pdev
->dev
, "got DMA channel (U) %d\n", pcdev
->dma_chans
[1]);
1715 err
= pxa_request_dma("CI_V", DMA_PRIO_HIGH
,
1716 pxa_camera_dma_irq_v
, pcdev
);
1718 dev_err(&pdev
->dev
, "Can't request DMA for V\n");
1719 goto exit_free_dma_u
;
1721 pcdev
->dma_chans
[2] = err
;
1722 dev_dbg(&pdev
->dev
, "got DMA channel (V) %d\n", pcdev
->dma_chans
[2]);
1724 DRCMR(68) = pcdev
->dma_chans
[0] | DRCMR_MAPVLD
;
1725 DRCMR(69) = pcdev
->dma_chans
[1] | DRCMR_MAPVLD
;
1726 DRCMR(70) = pcdev
->dma_chans
[2] | DRCMR_MAPVLD
;
1729 err
= request_irq(pcdev
->irq
, pxa_camera_irq
, 0, PXA_CAM_DRV_NAME
,
1732 dev_err(&pdev
->dev
, "Camera interrupt register failed \n");
1736 pcdev
->soc_host
.drv_name
= PXA_CAM_DRV_NAME
;
1737 pcdev
->soc_host
.ops
= &pxa_soc_camera_host_ops
;
1738 pcdev
->soc_host
.priv
= pcdev
;
1739 pcdev
->soc_host
.v4l2_dev
.dev
= &pdev
->dev
;
1740 pcdev
->soc_host
.nr
= pdev
->id
;
1742 err
= soc_camera_host_register(&pcdev
->soc_host
);
1749 free_irq(pcdev
->irq
, pcdev
);
1751 pxa_free_dma(pcdev
->dma_chans
[2]);
1753 pxa_free_dma(pcdev
->dma_chans
[1]);
1755 pxa_free_dma(pcdev
->dma_chans
[0]);
1759 release_mem_region(res
->start
, resource_size(res
));
1761 clk_put(pcdev
->clk
);
1768 static int __devexit
pxa_camera_remove(struct platform_device
*pdev
)
1770 struct soc_camera_host
*soc_host
= to_soc_camera_host(&pdev
->dev
);
1771 struct pxa_camera_dev
*pcdev
= container_of(soc_host
,
1772 struct pxa_camera_dev
, soc_host
);
1773 struct resource
*res
;
1775 clk_put(pcdev
->clk
);
1777 pxa_free_dma(pcdev
->dma_chans
[0]);
1778 pxa_free_dma(pcdev
->dma_chans
[1]);
1779 pxa_free_dma(pcdev
->dma_chans
[2]);
1780 free_irq(pcdev
->irq
, pcdev
);
1782 soc_camera_host_unregister(soc_host
);
1784 iounmap(pcdev
->base
);
1787 release_mem_region(res
->start
, resource_size(res
));
1791 dev_info(&pdev
->dev
, "PXA Camera driver unloaded\n");
1796 static struct platform_driver pxa_camera_driver
= {
1798 .name
= PXA_CAM_DRV_NAME
,
1800 .probe
= pxa_camera_probe
,
1801 .remove
= __devexit_p(pxa_camera_remove
),
1805 static int __init
pxa_camera_init(void)
1807 return platform_driver_register(&pxa_camera_driver
);
1810 static void __exit
pxa_camera_exit(void)
1812 platform_driver_unregister(&pxa_camera_driver
);
1815 module_init(pxa_camera_init
);
1816 module_exit(pxa_camera_exit
);
1818 MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1819 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1820 MODULE_LICENSE("GPL");
1821 MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME
);