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