V4L/DVB (10074): soc-camera: add camera sense data
[deliverable/linux.git] / drivers / media / video / pxa_camera.c
CommitLineData
3bc43840
GL
1/*
2 * V4L2 Driver for PXA camera host
3 *
4 * Copyright (C) 2006, Sascha Hauer, Pengutronix
5 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
6 *
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.
11 */
12
3bc43840
GL
13#include <linux/init.h>
14#include <linux/module.h>
7102b773 15#include <linux/io.h>
3bc43840
GL
16#include <linux/delay.h>
17#include <linux/dma-mapping.h>
18#include <linux/errno.h>
19#include <linux/fs.h>
20#include <linux/interrupt.h>
21#include <linux/kernel.h>
22#include <linux/mm.h>
23#include <linux/moduleparam.h>
24#include <linux/time.h>
25#include <linux/version.h>
26#include <linux/device.h>
27#include <linux/platform_device.h>
28#include <linux/mutex.h>
29#include <linux/clk.h>
30
31#include <media/v4l2-common.h>
32#include <media/v4l2-dev.h>
092d3921 33#include <media/videobuf-dma-sg.h>
3bc43840
GL
34#include <media/soc_camera.h>
35
36#include <linux/videodev2.h>
37
38#include <asm/dma.h>
a09e64fb
RK
39#include <mach/pxa-regs.h>
40#include <mach/camera.h>
3bc43840
GL
41
42#define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
43#define PXA_CAM_DRV_NAME "pxa27x-camera"
44
5ca11fa3
EM
45/* Camera Interface */
46#define CICR0 0x0000
47#define CICR1 0x0004
48#define CICR2 0x0008
49#define CICR3 0x000C
50#define CICR4 0x0010
51#define CISR 0x0014
52#define CIFR 0x0018
53#define CITOR 0x001C
54#define CIBR0 0x0028
55#define CIBR1 0x0030
56#define CIBR2 0x0038
57
58#define CICR0_DMAEN (1 << 31) /* DMA request enable */
59#define CICR0_PAR_EN (1 << 30) /* Parity enable */
60#define CICR0_SL_CAP_EN (1 << 29) /* Capture enable for slave mode */
61#define CICR0_ENB (1 << 28) /* Camera interface enable */
62#define CICR0_DIS (1 << 27) /* Camera interface disable */
63#define CICR0_SIM (0x7 << 24) /* Sensor interface mode mask */
64#define CICR0_TOM (1 << 9) /* Time-out mask */
65#define CICR0_RDAVM (1 << 8) /* Receive-data-available mask */
66#define CICR0_FEM (1 << 7) /* FIFO-empty mask */
67#define CICR0_EOLM (1 << 6) /* End-of-line mask */
68#define CICR0_PERRM (1 << 5) /* Parity-error mask */
69#define CICR0_QDM (1 << 4) /* Quick-disable mask */
70#define CICR0_CDM (1 << 3) /* Disable-done mask */
71#define CICR0_SOFM (1 << 2) /* Start-of-frame mask */
72#define CICR0_EOFM (1 << 1) /* End-of-frame mask */
73#define CICR0_FOM (1 << 0) /* FIFO-overrun mask */
74
75#define CICR1_TBIT (1 << 31) /* Transparency bit */
76#define CICR1_RGBT_CONV (0x3 << 29) /* RGBT conversion mask */
77#define CICR1_PPL (0x7ff << 15) /* Pixels per line mask */
78#define CICR1_RGB_CONV (0x7 << 12) /* RGB conversion mask */
79#define CICR1_RGB_F (1 << 11) /* RGB format */
80#define CICR1_YCBCR_F (1 << 10) /* YCbCr format */
81#define CICR1_RGB_BPP (0x7 << 7) /* RGB bis per pixel mask */
82#define CICR1_RAW_BPP (0x3 << 5) /* Raw bis per pixel mask */
83#define CICR1_COLOR_SP (0x3 << 3) /* Color space mask */
84#define CICR1_DW (0x7 << 0) /* Data width mask */
85
86#define CICR2_BLW (0xff << 24) /* Beginning-of-line pixel clock
87 wait count mask */
88#define CICR2_ELW (0xff << 16) /* End-of-line pixel clock
89 wait count mask */
90#define CICR2_HSW (0x3f << 10) /* Horizontal sync pulse width mask */
91#define CICR2_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
92 wait count mask */
93#define CICR2_FSW (0x7 << 0) /* Frame stabilization
94 wait count mask */
95
96#define CICR3_BFW (0xff << 24) /* Beginning-of-frame line clock
97 wait count mask */
98#define CICR3_EFW (0xff << 16) /* End-of-frame line clock
99 wait count mask */
100#define CICR3_VSW (0x3f << 10) /* Vertical sync pulse width mask */
101#define CICR3_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
102 wait count mask */
103#define CICR3_LPF (0x7ff << 0) /* Lines per frame mask */
104
105#define CICR4_MCLK_DLY (0x3 << 24) /* MCLK Data Capture Delay mask */
106#define CICR4_PCLK_EN (1 << 23) /* Pixel clock enable */
107#define CICR4_PCP (1 << 22) /* Pixel clock polarity */
108#define CICR4_HSP (1 << 21) /* Horizontal sync polarity */
109#define CICR4_VSP (1 << 20) /* Vertical sync polarity */
110#define CICR4_MCLK_EN (1 << 19) /* MCLK enable */
111#define CICR4_FR_RATE (0x7 << 8) /* Frame rate mask */
112#define CICR4_DIV (0xff << 0) /* Clock divisor mask */
113
114#define CISR_FTO (1 << 15) /* FIFO time-out */
115#define CISR_RDAV_2 (1 << 14) /* Channel 2 receive data available */
116#define CISR_RDAV_1 (1 << 13) /* Channel 1 receive data available */
117#define CISR_RDAV_0 (1 << 12) /* Channel 0 receive data available */
118#define CISR_FEMPTY_2 (1 << 11) /* Channel 2 FIFO empty */
119#define CISR_FEMPTY_1 (1 << 10) /* Channel 1 FIFO empty */
120#define CISR_FEMPTY_0 (1 << 9) /* Channel 0 FIFO empty */
121#define CISR_EOL (1 << 8) /* End of line */
122#define CISR_PAR_ERR (1 << 7) /* Parity error */
123#define CISR_CQD (1 << 6) /* Camera interface quick disable */
124#define CISR_CDD (1 << 5) /* Camera interface disable done */
125#define CISR_SOF (1 << 4) /* Start of frame */
126#define CISR_EOF (1 << 3) /* End of frame */
127#define CISR_IFO_2 (1 << 2) /* FIFO overrun for Channel 2 */
128#define CISR_IFO_1 (1 << 1) /* FIFO overrun for Channel 1 */
129#define CISR_IFO_0 (1 << 0) /* FIFO overrun for Channel 0 */
130
131#define CIFR_FLVL2 (0x7f << 23) /* FIFO 2 level mask */
132#define CIFR_FLVL1 (0x7f << 16) /* FIFO 1 level mask */
133#define CIFR_FLVL0 (0xff << 8) /* FIFO 0 level mask */
134#define CIFR_THL_0 (0x3 << 4) /* Threshold Level for Channel 0 FIFO */
135#define CIFR_RESET_F (1 << 3) /* Reset input FIFOs */
136#define CIFR_FEN2 (1 << 2) /* FIFO enable for channel 2 */
137#define CIFR_FEN1 (1 << 1) /* FIFO enable for channel 1 */
138#define CIFR_FEN0 (1 << 0) /* FIFO enable for channel 0 */
139
7102b773
GL
140#define CICR0_SIM_MP (0 << 24)
141#define CICR0_SIM_SP (1 << 24)
142#define CICR0_SIM_MS (2 << 24)
143#define CICR0_SIM_EP (3 << 24)
144#define CICR0_SIM_ES (4 << 24)
145
146#define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
147#define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
a5462e5b
MR
148#define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
149#define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
150#define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
7102b773
GL
151
152#define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
153#define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
154#define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
155#define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
156#define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
157
158#define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
159#define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
160#define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
161#define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
162
3bc43840
GL
163#define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
164 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
165 CICR0_EOFM | CICR0_FOM)
166
167static DEFINE_MUTEX(camera_lock);
168
169/*
170 * Structures
171 */
a5462e5b
MR
172enum pxa_camera_active_dma {
173 DMA_Y = 0x1,
174 DMA_U = 0x2,
175 DMA_V = 0x4,
176};
177
178/* descriptor needed for the PXA DMA engine */
179struct pxa_cam_dma {
180 dma_addr_t sg_dma;
181 struct pxa_dma_desc *sg_cpu;
182 size_t sg_size;
183 int sglen;
184};
3bc43840
GL
185
186/* buffer for one video frame */
187struct pxa_buffer {
188 /* common v4l buffer stuff -- must be first */
189 struct videobuf_buffer vb;
190
191 const struct soc_camera_data_format *fmt;
192
a5462e5b
MR
193 /* our descriptor lists for Y, U and V channels */
194 struct pxa_cam_dma dmas[3];
195
3bc43840 196 int inwork;
a5462e5b
MR
197
198 enum pxa_camera_active_dma active_dma;
3bc43840
GL
199};
200
3bc43840
GL
201struct pxa_camera_dev {
202 struct device *dev;
203 /* PXA27x is only supposed to handle one camera on its Quick Capture
204 * interface. If anyone ever builds hardware to enable more than
205 * one camera, they will have to modify this driver too */
206 struct soc_camera_device *icd;
207 struct clk *clk;
208
209 unsigned int irq;
210 void __iomem *base;
a5462e5b 211
e7c50688 212 int channels;
a5462e5b 213 unsigned int dma_chans[3];
3bc43840 214
3bc43840
GL
215 struct pxacamera_platform_data *pdata;
216 struct resource *res;
217 unsigned long platform_flags;
218 unsigned long platform_mclk_10khz;
219
220 struct list_head capture;
221
222 spinlock_t lock;
223
3bc43840 224 struct pxa_buffer *active;
5aa2110f 225 struct pxa_dma_desc *sg_tail[3];
3f6ac497
RJ
226
227 u32 save_cicr[5];
3bc43840
GL
228};
229
230static const char *pxa_cam_driver_description = "PXA_Camera";
231
232static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
233
234/*
235 * Videobuf operations
236 */
7102b773
GL
237static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
238 unsigned int *size)
3bc43840
GL
239{
240 struct soc_camera_device *icd = vq->priv_data;
5aa2110f
GL
241 struct soc_camera_host *ici =
242 to_soc_camera_host(icd->dev.parent);
243 struct pxa_camera_dev *pcdev = ici->priv;
3bc43840
GL
244
245 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
246
a5462e5b 247 /* planar capture requires Y, U and V buffers to be page aligned */
5aa2110f 248 if (pcdev->channels == 3) {
a5462e5b
MR
249 *size = PAGE_ALIGN(icd->width * icd->height); /* Y pages */
250 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* U pages */
251 *size += PAGE_ALIGN(icd->width * icd->height / 2); /* V pages */
252 } else {
253 *size = icd->width * icd->height *
254 ((icd->current_fmt->depth + 7) >> 3);
255 }
3bc43840
GL
256
257 if (0 == *count)
258 *count = 32;
259 while (*size * *count > vid_limit * 1024 * 1024)
260 (*count)--;
261
262 return 0;
263}
264
265static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
266{
267 struct soc_camera_device *icd = vq->priv_data;
268 struct soc_camera_host *ici =
269 to_soc_camera_host(icd->dev.parent);
270 struct pxa_camera_dev *pcdev = ici->priv;
271 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
a5462e5b 272 int i;
3bc43840
GL
273
274 BUG_ON(in_interrupt());
275
7e28adb2 276 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
3bc43840
GL
277 &buf->vb, buf->vb.baddr, buf->vb.bsize);
278
279 /* This waits until this buffer is out of danger, i.e., until it is no
280 * longer in STATE_QUEUED or STATE_ACTIVE */
281 videobuf_waiton(&buf->vb, 0, 0);
282 videobuf_dma_unmap(vq, dma);
283 videobuf_dma_free(dma);
284
a5462e5b
MR
285 for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
286 if (buf->dmas[i].sg_cpu)
287 dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
288 buf->dmas[i].sg_cpu,
289 buf->dmas[i].sg_dma);
290 buf->dmas[i].sg_cpu = NULL;
291 }
3bc43840
GL
292
293 buf->vb.state = VIDEOBUF_NEEDS_INIT;
294}
295
a5462e5b
MR
296static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
297 struct pxa_buffer *buf,
298 struct videobuf_dmabuf *dma, int channel,
299 int sglen, int sg_start, int cibr,
300 unsigned int size)
301{
302 struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
303 int i;
304
305 if (pxa_dma->sg_cpu)
306 dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
307 pxa_dma->sg_cpu, pxa_dma->sg_dma);
308
309 pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
310 pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
311 &pxa_dma->sg_dma, GFP_KERNEL);
312 if (!pxa_dma->sg_cpu)
313 return -ENOMEM;
314
315 pxa_dma->sglen = sglen;
316
317 for (i = 0; i < sglen; i++) {
318 int sg_i = sg_start + i;
319 struct scatterlist *sg = dma->sglist;
320 unsigned int dma_len = sg_dma_len(&sg[sg_i]), xfer_len;
321
322 pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
323 pxa_dma->sg_cpu[i].dtadr = sg_dma_address(&sg[sg_i]);
324
325 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
326 xfer_len = (min(dma_len, size) + 7) & ~7;
327
328 pxa_dma->sg_cpu[i].dcmd =
329 DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
330 size -= dma_len;
331 pxa_dma->sg_cpu[i].ddadr =
332 pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
333 }
334
335 pxa_dma->sg_cpu[sglen - 1].ddadr = DDADR_STOP;
336 pxa_dma->sg_cpu[sglen - 1].dcmd |= DCMD_ENDIRQEN;
337
338 return 0;
339}
340
7102b773
GL
341static int pxa_videobuf_prepare(struct videobuf_queue *vq,
342 struct videobuf_buffer *vb, enum v4l2_field field)
3bc43840
GL
343{
344 struct soc_camera_device *icd = vq->priv_data;
345 struct soc_camera_host *ici =
346 to_soc_camera_host(icd->dev.parent);
347 struct pxa_camera_dev *pcdev = ici->priv;
348 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
a5462e5b
MR
349 int ret;
350 int sglen_y, sglen_yu = 0, sglen_u = 0, sglen_v = 0;
351 int size_y, size_u = 0, size_v = 0;
3bc43840 352
7e28adb2 353 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
3bc43840
GL
354 vb, vb->baddr, vb->bsize);
355
356 /* Added list head initialization on alloc */
357 WARN_ON(!list_empty(&vb->queue));
358
359#ifdef DEBUG
360 /* This can be useful if you want to see if we actually fill
361 * the buffer with something */
362 memset((void *)vb->baddr, 0xaa, vb->bsize);
363#endif
364
365 BUG_ON(NULL == icd->current_fmt);
366
367 /* I think, in buf_prepare you only have to protect global data,
368 * the actual buffer is yours */
369 buf->inwork = 1;
370
371 if (buf->fmt != icd->current_fmt ||
372 vb->width != icd->width ||
373 vb->height != icd->height ||
374 vb->field != field) {
375 buf->fmt = icd->current_fmt;
376 vb->width = icd->width;
377 vb->height = icd->height;
378 vb->field = field;
379 vb->state = VIDEOBUF_NEEDS_INIT;
380 }
381
382 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
383 if (0 != vb->baddr && vb->bsize < vb->size) {
384 ret = -EINVAL;
385 goto out;
386 }
387
388 if (vb->state == VIDEOBUF_NEEDS_INIT) {
389 unsigned int size = vb->size;
390 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
391
392 ret = videobuf_iolock(vq, vb, NULL);
393 if (ret)
394 goto fail;
395
5aa2110f 396 if (pcdev->channels == 3) {
a5462e5b
MR
397 /* FIXME the calculations should be more precise */
398 sglen_y = dma->sglen / 2;
399 sglen_u = sglen_v = dma->sglen / 4 + 1;
400 sglen_yu = sglen_y + sglen_u;
401 size_y = size / 2;
402 size_u = size_v = size / 4;
403 } else {
404 sglen_y = dma->sglen;
405 size_y = size;
406 }
407
408 /* init DMA for Y channel */
409 ret = pxa_init_dma_channel(pcdev, buf, dma, 0, sglen_y,
410 0, 0x28, size_y);
3bc43840 411
a5462e5b
MR
412 if (ret) {
413 dev_err(pcdev->dev,
414 "DMA initialization for Y/RGB failed\n");
3bc43840
GL
415 goto fail;
416 }
417
5aa2110f 418 if (pcdev->channels == 3) {
a5462e5b
MR
419 /* init DMA for U channel */
420 ret = pxa_init_dma_channel(pcdev, buf, dma, 1, sglen_u,
421 sglen_y, 0x30, size_u);
422 if (ret) {
423 dev_err(pcdev->dev,
424 "DMA initialization for U failed\n");
425 goto fail_u;
426 }
427
428 /* init DMA for V channel */
429 ret = pxa_init_dma_channel(pcdev, buf, dma, 2, sglen_v,
430 sglen_yu, 0x38, size_v);
431 if (ret) {
432 dev_err(pcdev->dev,
433 "DMA initialization for V failed\n");
434 goto fail_v;
435 }
3bc43840 436 }
3bc43840
GL
437
438 vb->state = VIDEOBUF_PREPARED;
439 }
440
441 buf->inwork = 0;
a5462e5b 442 buf->active_dma = DMA_Y;
5aa2110f 443 if (pcdev->channels == 3)
a5462e5b 444 buf->active_dma |= DMA_U | DMA_V;
3bc43840
GL
445
446 return 0;
447
a5462e5b
MR
448fail_v:
449 dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
450 buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
451fail_u:
452 dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
453 buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
3bc43840
GL
454fail:
455 free_buffer(vq, buf);
456out:
457 buf->inwork = 0;
458 return ret;
459}
460
7102b773
GL
461static void pxa_videobuf_queue(struct videobuf_queue *vq,
462 struct videobuf_buffer *vb)
3bc43840
GL
463{
464 struct soc_camera_device *icd = vq->priv_data;
465 struct soc_camera_host *ici =
466 to_soc_camera_host(icd->dev.parent);
467 struct pxa_camera_dev *pcdev = ici->priv;
468 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
7102b773 469 struct pxa_buffer *active;
3bc43840 470 unsigned long flags;
5aa2110f 471 int i;
3bc43840 472
7e28adb2 473 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
3bc43840
GL
474 vb, vb->baddr, vb->bsize);
475 spin_lock_irqsave(&pcdev->lock, flags);
476
477 list_add_tail(&vb->queue, &pcdev->capture);
478
479 vb->state = VIDEOBUF_ACTIVE;
7102b773 480 active = pcdev->active;
3bc43840 481
7102b773 482 if (!active) {
5ca11fa3
EM
483 unsigned long cifr, cicr0;
484
485 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
486 __raw_writel(cifr, pcdev->base + CIFR);
a5462e5b 487
5aa2110f
GL
488 for (i = 0; i < pcdev->channels; i++) {
489 DDADR(pcdev->dma_chans[i]) = buf->dmas[i].sg_dma;
490 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
491 pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen - 1;
a5462e5b
MR
492 }
493
3bc43840 494 pcdev->active = buf;
5ca11fa3
EM
495
496 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
497 __raw_writel(cicr0, pcdev->base + CICR0);
3bc43840 498 } else {
a5462e5b
MR
499 struct pxa_cam_dma *buf_dma;
500 struct pxa_cam_dma *act_dma;
a5462e5b 501 int nents;
a5462e5b 502
e7c50688 503 for (i = 0; i < pcdev->channels; i++) {
a5462e5b
MR
504 buf_dma = &buf->dmas[i];
505 act_dma = &active->dmas[i];
506 nents = buf_dma->sglen;
507
508 /* Stop DMA engine */
509 DCSR(pcdev->dma_chans[i]) = 0;
510
511 /* Add the descriptors we just initialized to
512 the currently running chain */
5aa2110f
GL
513 pcdev->sg_tail[i]->ddadr = buf_dma->sg_dma;
514 pcdev->sg_tail[i] = buf_dma->sg_cpu + buf_dma->sglen - 1;
a5462e5b
MR
515
516 /* Setup a dummy descriptor with the DMA engines current
517 * state
3bc43840 518 */
a5462e5b
MR
519 buf_dma->sg_cpu[nents].dsadr =
520 pcdev->res->start + 0x28 + i*8; /* CIBRx */
521 buf_dma->sg_cpu[nents].dtadr =
522 DTADR(pcdev->dma_chans[i]);
523 buf_dma->sg_cpu[nents].dcmd =
524 DCMD(pcdev->dma_chans[i]);
525
526 if (DDADR(pcdev->dma_chans[i]) == DDADR_STOP) {
527 /* The DMA engine is on the last
528 descriptor, set the next descriptors
529 address to the descriptors we just
530 initialized */
531 buf_dma->sg_cpu[nents].ddadr = buf_dma->sg_dma;
532 } else {
533 buf_dma->sg_cpu[nents].ddadr =
534 DDADR(pcdev->dma_chans[i]);
535 }
536
537 /* The next descriptor is the dummy descriptor */
538 DDADR(pcdev->dma_chans[i]) = buf_dma->sg_dma + nents *
539 sizeof(struct pxa_dma_desc);
540
541 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
3bc43840 542 }
3bc43840
GL
543 }
544
545 spin_unlock_irqrestore(&pcdev->lock, flags);
3bc43840
GL
546}
547
548static void pxa_videobuf_release(struct videobuf_queue *vq,
549 struct videobuf_buffer *vb)
550{
551 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
552#ifdef DEBUG
553 struct soc_camera_device *icd = vq->priv_data;
554
7e28adb2 555 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
3bc43840
GL
556 vb, vb->baddr, vb->bsize);
557
558 switch (vb->state) {
559 case VIDEOBUF_ACTIVE:
7e28adb2 560 dev_dbg(&icd->dev, "%s (active)\n", __func__);
3bc43840
GL
561 break;
562 case VIDEOBUF_QUEUED:
7e28adb2 563 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
3bc43840
GL
564 break;
565 case VIDEOBUF_PREPARED:
7e28adb2 566 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
3bc43840
GL
567 break;
568 default:
7e28adb2 569 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
3bc43840
GL
570 break;
571 }
572#endif
573
574 free_buffer(vq, buf);
575}
576
a5462e5b
MR
577static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
578 struct videobuf_buffer *vb,
579 struct pxa_buffer *buf)
580{
5ca11fa3
EM
581 unsigned long cicr0;
582
a5462e5b
MR
583 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
584 list_del_init(&vb->queue);
585 vb->state = VIDEOBUF_DONE;
586 do_gettimeofday(&vb->ts);
587 vb->field_count++;
588 wake_up(&vb->done);
589
590 if (list_empty(&pcdev->capture)) {
591 pcdev->active = NULL;
592 DCSR(pcdev->dma_chans[0]) = 0;
593 DCSR(pcdev->dma_chans[1]) = 0;
594 DCSR(pcdev->dma_chans[2]) = 0;
5ca11fa3
EM
595
596 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
597 __raw_writel(cicr0, pcdev->base + CICR0);
a5462e5b
MR
598 return;
599 }
600
601 pcdev->active = list_entry(pcdev->capture.next,
602 struct pxa_buffer, vb.queue);
603}
604
605static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
606 enum pxa_camera_active_dma act_dma)
3bc43840 607{
3bc43840
GL
608 struct pxa_buffer *buf;
609 unsigned long flags;
e7c50688 610 u32 status, camera_status, overrun;
3bc43840 611 struct videobuf_buffer *vb;
5ca11fa3 612 unsigned long cifr, cicr0;
3bc43840
GL
613
614 spin_lock_irqsave(&pcdev->lock, flags);
615
a5462e5b
MR
616 status = DCSR(channel);
617 DCSR(channel) = status | DCSR_ENDINTR;
7102b773 618
3bc43840 619 if (status & DCSR_BUSERR) {
7102b773 620 dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
3bc43840
GL
621 goto out;
622 }
623
624 if (!(status & DCSR_ENDINTR)) {
7102b773
GL
625 dev_err(pcdev->dev, "Unknown DMA IRQ source, "
626 "status: 0x%08x\n", status);
3bc43840
GL
627 goto out;
628 }
629
3bc43840 630 if (!pcdev->active) {
7102b773 631 dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
3bc43840
GL
632 goto out;
633 }
634
5ca11fa3 635 camera_status = __raw_readl(pcdev->base + CISR);
e7c50688
GL
636 overrun = CISR_IFO_0;
637 if (pcdev->channels == 3)
638 overrun |= CISR_IFO_1 | CISR_IFO_2;
639 if (camera_status & overrun) {
640 dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n", camera_status);
641 /* Stop the Capture Interface */
5ca11fa3
EM
642 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
643 __raw_writel(cicr0, pcdev->base + CICR0);
644
e7c50688
GL
645 /* Stop DMA */
646 DCSR(channel) = 0;
647 /* Reset the FIFOs */
5ca11fa3
EM
648 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
649 __raw_writel(cifr, pcdev->base + CIFR);
e7c50688 650 /* Enable End-Of-Frame Interrupt */
5ca11fa3
EM
651 cicr0 &= ~CICR0_EOFM;
652 __raw_writel(cicr0, pcdev->base + CICR0);
e7c50688 653 /* Restart the Capture Interface */
5ca11fa3 654 __raw_writel(cicr0 | CICR0_ENB, pcdev->base + CICR0);
e7c50688
GL
655 goto out;
656 }
657
3bc43840
GL
658 vb = &pcdev->active->vb;
659 buf = container_of(vb, struct pxa_buffer, vb);
660 WARN_ON(buf->inwork || list_empty(&vb->queue));
7e28adb2 661 dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
3bc43840
GL
662 vb, vb->baddr, vb->bsize);
663
a5462e5b
MR
664 buf->active_dma &= ~act_dma;
665 if (!buf->active_dma)
666 pxa_camera_wakeup(pcdev, vb, buf);
3bc43840
GL
667
668out:
669 spin_unlock_irqrestore(&pcdev->lock, flags);
670}
671
a5462e5b
MR
672static void pxa_camera_dma_irq_y(int channel, void *data)
673{
674 struct pxa_camera_dev *pcdev = data;
675 pxa_camera_dma_irq(channel, pcdev, DMA_Y);
676}
677
678static void pxa_camera_dma_irq_u(int channel, void *data)
679{
680 struct pxa_camera_dev *pcdev = data;
681 pxa_camera_dma_irq(channel, pcdev, DMA_U);
682}
683
684static void pxa_camera_dma_irq_v(int channel, void *data)
685{
686 struct pxa_camera_dev *pcdev = data;
687 pxa_camera_dma_irq(channel, pcdev, DMA_V);
688}
689
7102b773 690static struct videobuf_queue_ops pxa_videobuf_ops = {
3bc43840
GL
691 .buf_setup = pxa_videobuf_setup,
692 .buf_prepare = pxa_videobuf_prepare,
693 .buf_queue = pxa_videobuf_queue,
694 .buf_release = pxa_videobuf_release,
695};
696
a034d1b7 697static void pxa_camera_init_videobuf(struct videobuf_queue *q,
092d3921
PZ
698 struct soc_camera_device *icd)
699{
a034d1b7
MD
700 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
701 struct pxa_camera_dev *pcdev = ici->priv;
702
092d3921
PZ
703 /* We must pass NULL as dev pointer, then all pci_* dma operations
704 * transform to normal dma_* ones. */
a034d1b7 705 videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
092d3921
PZ
706 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
707 sizeof(struct pxa_buffer), icd);
708}
709
3bc43840
GL
710static int mclk_get_divisor(struct pxa_camera_dev *pcdev)
711{
712 unsigned int mclk_10khz = pcdev->platform_mclk_10khz;
713 unsigned long div;
714 unsigned long lcdclk;
715
716 lcdclk = clk_get_rate(pcdev->clk) / 10000;
717
718 /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
719 * they get a nice Oops */
720 div = (lcdclk + 2 * mclk_10khz - 1) / (2 * mclk_10khz) - 1;
721
722 dev_dbg(pcdev->dev, "LCD clock %lukHz, target freq %dkHz, "
723 "divisor %lu\n", lcdclk * 10, mclk_10khz * 10, div);
724
725 return div;
726}
727
7102b773 728static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
3bc43840
GL
729{
730 struct pxacamera_platform_data *pdata = pcdev->pdata;
731 u32 cicr4 = 0;
732
733 dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
734 pcdev, pdata);
735
736 if (pdata && pdata->init) {
7e28adb2 737 dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
3bc43840
GL
738 pdata->init(pcdev->dev);
739 }
740
5ca11fa3
EM
741 /* disable all interrupts */
742 __raw_writel(0x3ff, pcdev->base + CICR0);
3bc43840
GL
743
744 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
745 cicr4 |= CICR4_PCLK_EN;
746 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
747 cicr4 |= CICR4_MCLK_EN;
748 if (pcdev->platform_flags & PXA_CAMERA_PCP)
749 cicr4 |= CICR4_PCP;
750 if (pcdev->platform_flags & PXA_CAMERA_HSP)
751 cicr4 |= CICR4_HSP;
752 if (pcdev->platform_flags & PXA_CAMERA_VSP)
753 cicr4 |= CICR4_VSP;
754
5ca11fa3
EM
755 cicr4 |= mclk_get_divisor(pcdev);
756 __raw_writel(cicr4, pcdev->base + CICR4);
3bc43840
GL
757
758 clk_enable(pcdev->clk);
759}
760
7102b773 761static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
3bc43840 762{
3bc43840 763 clk_disable(pcdev->clk);
3bc43840
GL
764}
765
766static irqreturn_t pxa_camera_irq(int irq, void *data)
767{
768 struct pxa_camera_dev *pcdev = data;
5ca11fa3 769 unsigned long status, cicr0;
3bc43840 770
5ca11fa3
EM
771 status = __raw_readl(pcdev->base + CISR);
772 dev_dbg(pcdev->dev, "Camera interrupt status 0x%lx\n", status);
3bc43840 773
e7c50688
GL
774 if (!status)
775 return IRQ_NONE;
776
5ca11fa3 777 __raw_writel(status, pcdev->base + CISR);
e7c50688
GL
778
779 if (status & CISR_EOF) {
780 int i;
781 for (i = 0; i < pcdev->channels; i++) {
782 DDADR(pcdev->dma_chans[i]) =
783 pcdev->active->dmas[i].sg_dma;
784 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
785 }
5ca11fa3
EM
786 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
787 __raw_writel(cicr0, pcdev->base + CICR0);
e7c50688
GL
788 }
789
3bc43840
GL
790 return IRQ_HANDLED;
791}
792
793/* The following two functions absolutely depend on the fact, that
794 * there can be only one camera on PXA quick capture interface */
7102b773 795static int pxa_camera_add_device(struct soc_camera_device *icd)
3bc43840
GL
796{
797 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
798 struct pxa_camera_dev *pcdev = ici->priv;
799 int ret;
800
801 mutex_lock(&camera_lock);
802
803 if (pcdev->icd) {
804 ret = -EBUSY;
805 goto ebusy;
806 }
807
808 dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
809 icd->devnum);
810
7102b773 811 pxa_camera_activate(pcdev);
3bc43840
GL
812 ret = icd->ops->init(icd);
813
814 if (!ret)
815 pcdev->icd = icd;
816
817ebusy:
818 mutex_unlock(&camera_lock);
819
820 return ret;
821}
822
7102b773 823static void pxa_camera_remove_device(struct soc_camera_device *icd)
3bc43840
GL
824{
825 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
826 struct pxa_camera_dev *pcdev = ici->priv;
827
828 BUG_ON(icd != pcdev->icd);
829
830 dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
831 icd->devnum);
832
833 /* disable capture, disable interrupts */
5ca11fa3 834 __raw_writel(0x3ff, pcdev->base + CICR0);
a5462e5b 835
3bc43840 836 /* Stop DMA engine */
a5462e5b
MR
837 DCSR(pcdev->dma_chans[0]) = 0;
838 DCSR(pcdev->dma_chans[1]) = 0;
839 DCSR(pcdev->dma_chans[2]) = 0;
3bc43840
GL
840
841 icd->ops->release(icd);
842
7102b773 843 pxa_camera_deactivate(pcdev);
3bc43840
GL
844
845 pcdev->icd = NULL;
846}
847
ad5f2e85
GL
848static int test_platform_param(struct pxa_camera_dev *pcdev,
849 unsigned char buswidth, unsigned long *flags)
3bc43840 850{
ad5f2e85
GL
851 /*
852 * Platform specified synchronization and pixel clock polarities are
853 * only a recommendation and are only used during probing. The PXA270
854 * quick capture interface supports both.
855 */
856 *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
857 SOCAM_MASTER : SOCAM_SLAVE) |
858 SOCAM_HSYNC_ACTIVE_HIGH |
859 SOCAM_HSYNC_ACTIVE_LOW |
860 SOCAM_VSYNC_ACTIVE_HIGH |
861 SOCAM_VSYNC_ACTIVE_LOW |
862 SOCAM_PCLK_SAMPLE_RISING |
863 SOCAM_PCLK_SAMPLE_FALLING;
3bc43840
GL
864
865 /* If requested data width is supported by the platform, use it */
ad5f2e85 866 switch (buswidth) {
3bc43840 867 case 10:
ad5f2e85
GL
868 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
869 return -EINVAL;
870 *flags |= SOCAM_DATAWIDTH_10;
3bc43840
GL
871 break;
872 case 9:
ad5f2e85
GL
873 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
874 return -EINVAL;
875 *flags |= SOCAM_DATAWIDTH_9;
3bc43840
GL
876 break;
877 case 8:
ad5f2e85
GL
878 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
879 return -EINVAL;
880 *flags |= SOCAM_DATAWIDTH_8;
2a48fc73
RJ
881 break;
882 default:
883 return -EINVAL;
3bc43840 884 }
ad5f2e85
GL
885
886 return 0;
887}
888
889static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
890{
891 struct soc_camera_host *ici =
892 to_soc_camera_host(icd->dev.parent);
893 struct pxa_camera_dev *pcdev = ici->priv;
894 unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
5ca11fa3 895 u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0;
ad5f2e85
GL
896 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
897
898 if (ret < 0)
899 return ret;
900
901 camera_flags = icd->ops->query_bus_param(icd);
902
903 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
904 if (!common_flags)
3bc43840
GL
905 return -EINVAL;
906
e7c50688
GL
907 pcdev->channels = 1;
908
ad5f2e85
GL
909 /* Make choises, based on platform preferences */
910 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
911 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
912 if (pcdev->platform_flags & PXA_CAMERA_HSP)
913 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
914 else
915 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
916 }
917
918 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
919 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
920 if (pcdev->platform_flags & PXA_CAMERA_VSP)
921 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
922 else
923 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
924 }
925
926 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
927 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
928 if (pcdev->platform_flags & PXA_CAMERA_PCP)
929 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
930 else
931 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
932 }
933
934 ret = icd->ops->set_bus_param(icd, common_flags);
3bc43840
GL
935 if (ret < 0)
936 return ret;
937
938 /* Datawidth is now guaranteed to be equal to one of the three values.
939 * We fix bit-per-pixel equal to data-width... */
ad5f2e85
GL
940 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
941 case SOCAM_DATAWIDTH_10:
3bc43840
GL
942 dw = 4;
943 bpp = 0x40;
944 break;
ad5f2e85 945 case SOCAM_DATAWIDTH_9:
3bc43840
GL
946 dw = 3;
947 bpp = 0x20;
948 break;
949 default:
950 /* Actually it can only be 8 now,
951 * default is just to silence compiler warnings */
ad5f2e85 952 case SOCAM_DATAWIDTH_8:
3bc43840
GL
953 dw = 2;
954 bpp = 0;
955 }
956
957 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
958 cicr4 |= CICR4_PCLK_EN;
959 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
960 cicr4 |= CICR4_MCLK_EN;
ad5f2e85 961 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
3bc43840 962 cicr4 |= CICR4_PCP;
ad5f2e85 963 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
3bc43840 964 cicr4 |= CICR4_HSP;
ad5f2e85 965 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
3bc43840
GL
966 cicr4 |= CICR4_VSP;
967
5ca11fa3 968 cicr0 = __raw_readl(pcdev->base + CICR0);
3bc43840 969 if (cicr0 & CICR0_ENB)
5ca11fa3 970 __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
a5462e5b
MR
971
972 cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
973
974 switch (pixfmt) {
975 case V4L2_PIX_FMT_YUV422P:
e7c50688 976 pcdev->channels = 3;
a5462e5b 977 cicr1 |= CICR1_YCBCR_F;
2a48fc73
RJ
978 /*
979 * Normally, pxa bus wants as input UYVY format. We allow all
980 * reorderings of the YUV422 format, as no processing is done,
981 * and the YUV stream is just passed through without any
982 * transformation. Note that UYVY is the only format that
983 * should be used if pxa framebuffer Overlay2 is used.
984 */
985 case V4L2_PIX_FMT_UYVY:
986 case V4L2_PIX_FMT_VYUY:
a5462e5b 987 case V4L2_PIX_FMT_YUYV:
2a48fc73 988 case V4L2_PIX_FMT_YVYU:
a5462e5b
MR
989 cicr1 |= CICR1_COLOR_SP_VAL(2);
990 break;
991 case V4L2_PIX_FMT_RGB555:
992 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
993 CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
994 break;
995 case V4L2_PIX_FMT_RGB565:
996 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
997 break;
998 }
999
5ca11fa3
EM
1000 cicr2 = 0;
1001 cicr3 = CICR3_LPF_VAL(icd->height - 1) |
3bc43840 1002 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
5ca11fa3
EM
1003 cicr4 |= mclk_get_divisor(pcdev);
1004
1005 __raw_writel(cicr1, pcdev->base + CICR1);
1006 __raw_writel(cicr2, pcdev->base + CICR2);
1007 __raw_writel(cicr3, pcdev->base + CICR3);
1008 __raw_writel(cicr4, pcdev->base + CICR4);
3bc43840
GL
1009
1010 /* CIF interrupts are not used, only DMA */
5ca11fa3
EM
1011 cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
1012 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
1013 cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
1014 __raw_writel(cicr0, pcdev->base + CICR0);
3bc43840
GL
1015
1016 return 0;
1017}
1018
2a48fc73
RJ
1019static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
1020 unsigned char buswidth)
ad5f2e85
GL
1021{
1022 struct soc_camera_host *ici =
1023 to_soc_camera_host(icd->dev.parent);
1024 struct pxa_camera_dev *pcdev = ici->priv;
1025 unsigned long bus_flags, camera_flags;
2a48fc73 1026 int ret = test_platform_param(pcdev, buswidth, &bus_flags);
ad5f2e85
GL
1027
1028 if (ret < 0)
1029 return ret;
1030
1031 camera_flags = icd->ops->query_bus_param(icd);
1032
1033 return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
1034}
1035
2a48fc73
RJ
1036static const struct soc_camera_data_format pxa_camera_formats[] = {
1037 {
1038 .name = "Planar YUV422 16 bit",
1039 .depth = 16,
1040 .fourcc = V4L2_PIX_FMT_YUV422P,
1041 .colorspace = V4L2_COLORSPACE_JPEG,
1042 },
1043};
1044
1045static bool buswidth_supported(struct soc_camera_device *icd, int depth)
1046{
1047 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1048 struct pxa_camera_dev *pcdev = ici->priv;
1049
1050 switch (depth) {
1051 case 8:
1052 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8);
1053 case 9:
1054 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9);
1055 case 10:
1056 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10);
1057 }
1058 return false;
1059}
1060
1061static int required_buswidth(const struct soc_camera_data_format *fmt)
1062{
1063 switch (fmt->fourcc) {
1064 case V4L2_PIX_FMT_UYVY:
1065 case V4L2_PIX_FMT_VYUY:
1066 case V4L2_PIX_FMT_YUYV:
1067 case V4L2_PIX_FMT_YVYU:
1068 case V4L2_PIX_FMT_RGB565:
1069 case V4L2_PIX_FMT_RGB555:
1070 return 8;
1071 default:
1072 return fmt->depth;
1073 }
1074}
1075
1076static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
1077 struct soc_camera_format_xlate *xlate)
1078{
1079 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1080 int formats = 0, buswidth, ret;
1081
1082 buswidth = required_buswidth(icd->formats + idx);
1083
1084 if (!buswidth_supported(icd, buswidth))
1085 return 0;
1086
1087 ret = pxa_camera_try_bus_param(icd, buswidth);
1088 if (ret < 0)
1089 return 0;
1090
1091 switch (icd->formats[idx].fourcc) {
1092 case V4L2_PIX_FMT_UYVY:
1093 formats++;
1094 if (xlate) {
1095 xlate->host_fmt = &pxa_camera_formats[0];
1096 xlate->cam_fmt = icd->formats + idx;
1097 xlate->buswidth = buswidth;
1098 xlate++;
1099 dev_dbg(&ici->dev, "Providing format %s using %s\n",
1100 pxa_camera_formats[0].name,
1101 icd->formats[idx].name);
1102 }
1103 case V4L2_PIX_FMT_VYUY:
1104 case V4L2_PIX_FMT_YUYV:
1105 case V4L2_PIX_FMT_YVYU:
1106 case V4L2_PIX_FMT_RGB565:
1107 case V4L2_PIX_FMT_RGB555:
1108 formats++;
1109 if (xlate) {
1110 xlate->host_fmt = icd->formats + idx;
1111 xlate->cam_fmt = icd->formats + idx;
1112 xlate->buswidth = buswidth;
1113 xlate++;
1114 dev_dbg(&ici->dev, "Providing format %s packed\n",
1115 icd->formats[idx].name);
1116 }
1117 break;
1118 default:
1119 /* Generic pass-through */
1120 formats++;
1121 if (xlate) {
1122 xlate->host_fmt = icd->formats + idx;
1123 xlate->cam_fmt = icd->formats + idx;
1124 xlate->buswidth = icd->formats[idx].depth;
1125 xlate++;
1126 dev_dbg(&ici->dev,
1127 "Providing format %s in pass-through mode\n",
1128 icd->formats[idx].name);
1129 }
1130 }
1131
1132 return formats;
1133}
1134
d8fac217
GL
1135static int pxa_camera_set_fmt(struct soc_camera_device *icd,
1136 __u32 pixfmt, struct v4l2_rect *rect)
ad5f2e85 1137{
2a48fc73
RJ
1138 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1139 const struct soc_camera_data_format *host_fmt, *cam_fmt = NULL;
1140 const struct soc_camera_format_xlate *xlate;
1141 int ret, buswidth;
25c4d74e 1142
2a48fc73
RJ
1143 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1144 if (!xlate) {
1145 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
1146 return -EINVAL;
25c4d74e
GL
1147 }
1148
2a48fc73
RJ
1149 buswidth = xlate->buswidth;
1150 host_fmt = xlate->host_fmt;
1151 cam_fmt = xlate->cam_fmt;
1152
1153 switch (pixfmt) {
1154 case 0: /* Only geometry change */
1155 ret = icd->ops->set_fmt(icd, pixfmt, rect);
1156 break;
1157 default:
1158 ret = icd->ops->set_fmt(icd, cam_fmt->fourcc, rect);
1159 }
1160
1161 if (ret < 0)
1162 dev_warn(&ici->dev, "Failed to configure for format %x\n",
1163 pixfmt);
1164
1165 if (pixfmt && !ret) {
1166 icd->buswidth = buswidth;
1167 icd->current_fmt = host_fmt;
1168 }
25c4d74e
GL
1169
1170 return ret;
ad5f2e85
GL
1171}
1172
d8fac217
GL
1173static int pxa_camera_try_fmt(struct soc_camera_device *icd,
1174 struct v4l2_format *f)
3bc43840 1175{
2a48fc73
RJ
1176 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1177 const struct soc_camera_format_xlate *xlate;
1178 struct v4l2_pix_format *pix = &f->fmt.pix;
1179 __u32 pixfmt = pix->pixelformat;
a2c8c68c 1180
2a48fc73
RJ
1181 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1182 if (!xlate) {
1183 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
25c4d74e 1184 return -EINVAL;
2a48fc73 1185 }
25c4d74e 1186
3bc43840 1187 /* limit to pxa hardware capabilities */
2a48fc73
RJ
1188 if (pix->height < 32)
1189 pix->height = 32;
1190 if (pix->height > 2048)
1191 pix->height = 2048;
1192 if (pix->width < 48)
1193 pix->width = 48;
1194 if (pix->width > 2048)
1195 pix->width = 2048;
1196 pix->width &= ~0x01;
1197
1198 pix->bytesperline = pix->width *
1199 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1200 pix->sizeimage = pix->height * pix->bytesperline;
25c4d74e 1201
ad5f2e85 1202 /* limit to sensor capabilities */
d8fac217 1203 return icd->ops->try_fmt(icd, f);
3bc43840
GL
1204}
1205
7102b773
GL
1206static int pxa_camera_reqbufs(struct soc_camera_file *icf,
1207 struct v4l2_requestbuffers *p)
3bc43840
GL
1208{
1209 int i;
1210
1211 /* This is for locking debugging only. I removed spinlocks and now I
1212 * check whether .prepare is ever called on a linked buffer, or whether
1213 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1214 * it hadn't triggered */
1215 for (i = 0; i < p->count; i++) {
1216 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1217 struct pxa_buffer, vb);
1218 buf->inwork = 0;
1219 INIT_LIST_HEAD(&buf->vb.queue);
1220 }
1221
1222 return 0;
1223}
1224
7102b773 1225static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
3bc43840
GL
1226{
1227 struct soc_camera_file *icf = file->private_data;
1228 struct pxa_buffer *buf;
1229
1230 buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
1231 vb.stream);
1232
1233 poll_wait(file, &buf->vb.done, pt);
1234
1235 if (buf->vb.state == VIDEOBUF_DONE ||
1236 buf->vb.state == VIDEOBUF_ERROR)
1237 return POLLIN|POLLRDNORM;
1238
1239 return 0;
1240}
1241
7102b773
GL
1242static int pxa_camera_querycap(struct soc_camera_host *ici,
1243 struct v4l2_capability *cap)
3bc43840
GL
1244{
1245 /* cap->name is set by the firendly caller:-> */
1246 strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
1247 cap->version = PXA_CAM_VERSION_CODE;
1248 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1249
1250 return 0;
1251}
1252
3f6ac497
RJ
1253static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1254{
1255 struct soc_camera_host *ici =
1256 to_soc_camera_host(icd->dev.parent);
1257 struct pxa_camera_dev *pcdev = ici->priv;
1258 int i = 0, ret = 0;
1259
5ca11fa3
EM
1260 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
1261 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
1262 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
1263 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
1264 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
3f6ac497
RJ
1265
1266 if ((pcdev->icd) && (pcdev->icd->ops->suspend))
1267 ret = pcdev->icd->ops->suspend(pcdev->icd, state);
1268
1269 return ret;
1270}
1271
1272static int pxa_camera_resume(struct soc_camera_device *icd)
1273{
1274 struct soc_camera_host *ici =
1275 to_soc_camera_host(icd->dev.parent);
1276 struct pxa_camera_dev *pcdev = ici->priv;
1277 int i = 0, ret = 0;
1278
87f3dd77
EM
1279 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1280 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1281 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
3f6ac497 1282
5ca11fa3
EM
1283 __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
1284 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
1285 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
1286 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
1287 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
3f6ac497
RJ
1288
1289 if ((pcdev->icd) && (pcdev->icd->ops->resume))
1290 ret = pcdev->icd->ops->resume(pcdev->icd);
1291
1292 /* Restart frame capture if active buffer exists */
1293 if (!ret && pcdev->active) {
5ca11fa3
EM
1294 unsigned long cifr, cicr0;
1295
3f6ac497 1296 /* Reset the FIFOs */
5ca11fa3
EM
1297 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
1298 __raw_writel(cifr, pcdev->base + CIFR);
1299
1300 cicr0 = __raw_readl(pcdev->base + CICR0);
1301 cicr0 &= ~CICR0_EOFM; /* Enable End-Of-Frame Interrupt */
1302 cicr0 |= CICR0_ENB; /* Restart the Capture Interface */
1303 __raw_writel(cicr0, pcdev->base + CICR0);
3f6ac497
RJ
1304 }
1305
1306 return ret;
1307}
1308
b8d9904c
GL
1309static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
1310 .owner = THIS_MODULE,
1311 .add = pxa_camera_add_device,
1312 .remove = pxa_camera_remove_device,
3f6ac497
RJ
1313 .suspend = pxa_camera_suspend,
1314 .resume = pxa_camera_resume,
2a48fc73 1315 .get_formats = pxa_camera_get_formats,
d8fac217
GL
1316 .set_fmt = pxa_camera_set_fmt,
1317 .try_fmt = pxa_camera_try_fmt,
092d3921 1318 .init_videobuf = pxa_camera_init_videobuf,
b8d9904c
GL
1319 .reqbufs = pxa_camera_reqbufs,
1320 .poll = pxa_camera_poll,
1321 .querycap = pxa_camera_querycap,
b8d9904c
GL
1322 .set_bus_param = pxa_camera_set_bus_param,
1323};
1324
1325/* Should be allocated dynamically too, but we have only one. */
3bc43840
GL
1326static struct soc_camera_host pxa_soc_camera_host = {
1327 .drv_name = PXA_CAM_DRV_NAME,
b8d9904c 1328 .ops = &pxa_soc_camera_host_ops,
3bc43840
GL
1329};
1330
1331static int pxa_camera_probe(struct platform_device *pdev)
1332{
1333 struct pxa_camera_dev *pcdev;
1334 struct resource *res;
1335 void __iomem *base;
02da4659 1336 int irq;
3bc43840
GL
1337 int err = 0;
1338
1339 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1340 irq = platform_get_irq(pdev, 0);
02da4659 1341 if (!res || irq < 0) {
3bc43840
GL
1342 err = -ENODEV;
1343 goto exit;
1344 }
1345
1346 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1347 if (!pcdev) {
7102b773 1348 dev_err(&pdev->dev, "Could not allocate pcdev\n");
3bc43840
GL
1349 err = -ENOMEM;
1350 goto exit;
1351 }
1352
1353 pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
1354 if (IS_ERR(pcdev->clk)) {
1355 err = PTR_ERR(pcdev->clk);
1356 goto exit_kfree;
1357 }
1358
1359 dev_set_drvdata(&pdev->dev, pcdev);
1360 pcdev->res = res;
1361
1362 pcdev->pdata = pdev->dev.platform_data;
1363 pcdev->platform_flags = pcdev->pdata->flags;
ad5f2e85
GL
1364 if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
1365 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
3bc43840
GL
1366 /* Platform hasn't set available data widths. This is bad.
1367 * Warn and use a default. */
1368 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1369 "data widths, using default 10 bit\n");
1370 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
1371 }
1372 pcdev->platform_mclk_10khz = pcdev->pdata->mclk_10khz;
1373 if (!pcdev->platform_mclk_10khz) {
1374 dev_warn(&pdev->dev,
1375 "mclk_10khz == 0! Please, fix your platform data. "
1376 "Using default 20MHz\n");
1377 pcdev->platform_mclk_10khz = 2000;
1378 }
1379
1380 INIT_LIST_HEAD(&pcdev->capture);
1381 spin_lock_init(&pcdev->lock);
1382
1383 /*
1384 * Request the regions.
1385 */
1386 if (!request_mem_region(res->start, res->end - res->start + 1,
1387 PXA_CAM_DRV_NAME)) {
1388 err = -EBUSY;
1389 goto exit_clk;
1390 }
1391
1392 base = ioremap(res->start, res->end - res->start + 1);
1393 if (!base) {
1394 err = -ENOMEM;
1395 goto exit_release;
1396 }
1397 pcdev->irq = irq;
1398 pcdev->base = base;
1399 pcdev->dev = &pdev->dev;
1400
1401 /* request dma */
de3e3b82 1402 err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
1403 pxa_camera_dma_irq_y, pcdev);
1404 if (err < 0) {
3bc43840 1405 dev_err(pcdev->dev, "Can't request DMA for Y\n");
3bc43840
GL
1406 goto exit_iounmap;
1407 }
de3e3b82 1408 pcdev->dma_chans[0] = err;
a5462e5b
MR
1409 dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
1410
de3e3b82 1411 err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
1412 pxa_camera_dma_irq_u, pcdev);
1413 if (err < 0) {
a5462e5b 1414 dev_err(pcdev->dev, "Can't request DMA for U\n");
a5462e5b
MR
1415 goto exit_free_dma_y;
1416 }
de3e3b82 1417 pcdev->dma_chans[1] = err;
a5462e5b
MR
1418 dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
1419
de3e3b82 1420 err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
1421 pxa_camera_dma_irq_v, pcdev);
1422 if (err < 0) {
a5462e5b 1423 dev_err(pcdev->dev, "Can't request DMA for V\n");
a5462e5b
MR
1424 goto exit_free_dma_u;
1425 }
de3e3b82 1426 pcdev->dma_chans[2] = err;
a5462e5b 1427 dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
3bc43840 1428
87f3dd77
EM
1429 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1430 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1431 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
3bc43840
GL
1432
1433 /* request irq */
1434 err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
1435 pcdev);
1436 if (err) {
1437 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1438 goto exit_free_dma;
1439 }
1440
1441 pxa_soc_camera_host.priv = pcdev;
1442 pxa_soc_camera_host.dev.parent = &pdev->dev;
1443 pxa_soc_camera_host.nr = pdev->id;
b8d9904c 1444 err = soc_camera_host_register(&pxa_soc_camera_host);
3bc43840
GL
1445 if (err)
1446 goto exit_free_irq;
1447
1448 return 0;
1449
1450exit_free_irq:
1451 free_irq(pcdev->irq, pcdev);
1452exit_free_dma:
a5462e5b
MR
1453 pxa_free_dma(pcdev->dma_chans[2]);
1454exit_free_dma_u:
1455 pxa_free_dma(pcdev->dma_chans[1]);
1456exit_free_dma_y:
1457 pxa_free_dma(pcdev->dma_chans[0]);
3bc43840
GL
1458exit_iounmap:
1459 iounmap(base);
1460exit_release:
1461 release_mem_region(res->start, res->end - res->start + 1);
1462exit_clk:
1463 clk_put(pcdev->clk);
1464exit_kfree:
1465 kfree(pcdev);
1466exit:
1467 return err;
1468}
1469
1470static int __devexit pxa_camera_remove(struct platform_device *pdev)
1471{
1472 struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
1473 struct resource *res;
1474
1475 clk_put(pcdev->clk);
1476
a5462e5b
MR
1477 pxa_free_dma(pcdev->dma_chans[0]);
1478 pxa_free_dma(pcdev->dma_chans[1]);
1479 pxa_free_dma(pcdev->dma_chans[2]);
3bc43840
GL
1480 free_irq(pcdev->irq, pcdev);
1481
1482 soc_camera_host_unregister(&pxa_soc_camera_host);
1483
1484 iounmap(pcdev->base);
1485
1486 res = pcdev->res;
1487 release_mem_region(res->start, res->end - res->start + 1);
1488
1489 kfree(pcdev);
1490
7102b773 1491 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
3bc43840 1492
3bc43840
GL
1493 return 0;
1494}
1495
3bc43840
GL
1496static struct platform_driver pxa_camera_driver = {
1497 .driver = {
1498 .name = PXA_CAM_DRV_NAME,
1499 },
1500 .probe = pxa_camera_probe,
1501 .remove = __exit_p(pxa_camera_remove),
3bc43840
GL
1502};
1503
1504
1505static int __devinit pxa_camera_init(void)
1506{
1507 return platform_driver_register(&pxa_camera_driver);
1508}
1509
1510static void __exit pxa_camera_exit(void)
1511{
01c1e4ca 1512 platform_driver_unregister(&pxa_camera_driver);
3bc43840
GL
1513}
1514
1515module_init(pxa_camera_init);
1516module_exit(pxa_camera_exit);
1517
1518MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1519MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1520MODULE_LICENSE("GPL");
This page took 0.206357 seconds and 5 git commands to generate.