blob: cfa113cedd3edd115d82b163095186dfa3f6ec29 [file] [log] [blame]
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001/*
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
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030013#include <linux/init.h>
14#include <linux/module.h>
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -030015#include <linux/io.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030016#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>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030028#include <linux/clk.h>
29
30#include <media/v4l2-common.h>
31#include <media/v4l2-dev.h>
Paulius Zaleckas092d3922008-07-11 20:50:31 -030032#include <media/videobuf-dma-sg.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030033#include <media/soc_camera.h>
34
35#include <linux/videodev2.h>
36
Eric Miaocfbaf4d2009-01-02 12:16:02 -030037#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/camera.h>
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -030039
40#define PXA_CAM_VERSION_CODE KERNEL_VERSION(0, 0, 5)
41#define PXA_CAM_DRV_NAME "pxa27x-camera"
42
Eric Miao5ca11fa2008-12-18 11:15:50 -030043/* 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
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300138#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 */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300146#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 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300149
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
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300161#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
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300165/*
Robert Jarzmik92a83372009-03-31 03:44:21 -0300166 * YUV422P picture size should be a multiple of 16, so the heuristic aligns
167 * height, width on 4 byte boundaries to reach the 16 multiple for the size.
168 */
169#define YUV422P_X_Y_ALIGN 4
170#define YUV422P_SIZE_ALIGN YUV422P_X_Y_ALIGN * YUV422P_X_Y_ALIGN
171
172/*
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300173 * Structures
174 */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300175enum pxa_camera_active_dma {
176 DMA_Y = 0x1,
177 DMA_U = 0x2,
178 DMA_V = 0x4,
179};
180
181/* descriptor needed for the PXA DMA engine */
182struct pxa_cam_dma {
183 dma_addr_t sg_dma;
184 struct pxa_dma_desc *sg_cpu;
185 size_t sg_size;
186 int sglen;
187};
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300188
189/* buffer for one video frame */
190struct pxa_buffer {
191 /* common v4l buffer stuff -- must be first */
192 struct videobuf_buffer vb;
193
194 const struct soc_camera_data_format *fmt;
195
Mike Rapoporta5462e52008-04-22 10:36:32 -0300196 /* our descriptor lists for Y, U and V channels */
197 struct pxa_cam_dma dmas[3];
198
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300199 int inwork;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300200
201 enum pxa_camera_active_dma active_dma;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300202};
203
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300204struct pxa_camera_dev {
205 struct device *dev;
206 /* PXA27x is only supposed to handle one camera on its Quick Capture
207 * interface. If anyone ever builds hardware to enable more than
208 * one camera, they will have to modify this driver too */
209 struct soc_camera_device *icd;
210 struct clk *clk;
211
212 unsigned int irq;
213 void __iomem *base;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300214
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300215 int channels;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300216 unsigned int dma_chans[3];
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300217
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300218 struct pxacamera_platform_data *pdata;
219 struct resource *res;
220 unsigned long platform_flags;
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300221 unsigned long ciclk;
222 unsigned long mclk;
223 u32 mclk_divisor;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300224
225 struct list_head capture;
226
227 spinlock_t lock;
228
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300229 struct pxa_buffer *active;
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300230 struct pxa_dma_desc *sg_tail[3];
Robert Jarzmik3f6ac492008-08-02 07:10:04 -0300231
232 u32 save_cicr[5];
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300233};
234
235static const char *pxa_cam_driver_description = "PXA_Camera";
236
237static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
238
239/*
240 * Videobuf operations
241 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300242static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
243 unsigned int *size)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300244{
245 struct soc_camera_device *icd = vq->priv_data;
246
247 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
248
Robert Jarzmik92a83372009-03-31 03:44:21 -0300249 *size = roundup(icd->width * icd->height *
250 ((icd->current_fmt->depth + 7) >> 3), 8);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300251
252 if (0 == *count)
253 *count = 32;
254 while (*size * *count > vid_limit * 1024 * 1024)
255 (*count)--;
256
257 return 0;
258}
259
260static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
261{
262 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300263 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300264 struct pxa_camera_dev *pcdev = ici->priv;
265 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300266 int i;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300267
268 BUG_ON(in_interrupt());
269
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300270 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300271 &buf->vb, buf->vb.baddr, buf->vb.bsize);
272
273 /* This waits until this buffer is out of danger, i.e., until it is no
274 * longer in STATE_QUEUED or STATE_ACTIVE */
275 videobuf_waiton(&buf->vb, 0, 0);
276 videobuf_dma_unmap(vq, dma);
277 videobuf_dma_free(dma);
278
Mike Rapoporta5462e52008-04-22 10:36:32 -0300279 for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
280 if (buf->dmas[i].sg_cpu)
281 dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
282 buf->dmas[i].sg_cpu,
283 buf->dmas[i].sg_dma);
284 buf->dmas[i].sg_cpu = NULL;
285 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300286
287 buf->vb.state = VIDEOBUF_NEEDS_INIT;
288}
289
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300290static int calculate_dma_sglen(struct scatterlist *sglist, int sglen,
291 int sg_first_ofs, int size)
292{
293 int i, offset, dma_len, xfer_len;
294 struct scatterlist *sg;
295
296 offset = sg_first_ofs;
297 for_each_sg(sglist, sg, sglen, i) {
298 dma_len = sg_dma_len(sg);
299
300 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
301 xfer_len = roundup(min(dma_len - offset, size), 8);
302
303 size = max(0, size - xfer_len);
304 offset = 0;
305 if (size == 0)
306 break;
307 }
308
309 BUG_ON(size != 0);
310 return i + 1;
311}
312
313/**
314 * pxa_init_dma_channel - init dma descriptors
315 * @pcdev: pxa camera device
316 * @buf: pxa buffer to find pxa dma channel
317 * @dma: dma video buffer
318 * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V')
319 * @cibr: camera Receive Buffer Register
320 * @size: bytes to transfer
321 * @sg_first: first element of sg_list
322 * @sg_first_ofs: offset in first element of sg_list
323 *
324 * Prepares the pxa dma descriptors to transfer one camera channel.
325 * Beware sg_first and sg_first_ofs are both input and output parameters.
326 *
327 * Returns 0 or -ENOMEM if no coherent memory is available
328 */
Mike Rapoporta5462e52008-04-22 10:36:32 -0300329static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
330 struct pxa_buffer *buf,
331 struct videobuf_dmabuf *dma, int channel,
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300332 int cibr, int size,
333 struct scatterlist **sg_first, int *sg_first_ofs)
Mike Rapoporta5462e52008-04-22 10:36:32 -0300334{
335 struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300336 struct scatterlist *sg;
337 int i, offset, sglen;
338 int dma_len = 0, xfer_len = 0;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300339
340 if (pxa_dma->sg_cpu)
341 dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
342 pxa_dma->sg_cpu, pxa_dma->sg_dma);
343
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300344 sglen = calculate_dma_sglen(*sg_first, dma->sglen,
345 *sg_first_ofs, size);
346
Mike Rapoporta5462e52008-04-22 10:36:32 -0300347 pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
348 pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
349 &pxa_dma->sg_dma, GFP_KERNEL);
350 if (!pxa_dma->sg_cpu)
351 return -ENOMEM;
352
353 pxa_dma->sglen = sglen;
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300354 offset = *sg_first_ofs;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300355
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300356 dev_dbg(pcdev->dev, "DMA: sg_first=%p, sglen=%d, ofs=%d, dma.desc=%x\n",
357 *sg_first, sglen, *sg_first_ofs, pxa_dma->sg_dma);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300358
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300359
360 for_each_sg(*sg_first, sg, sglen, i) {
361 dma_len = sg_dma_len(sg);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300362
363 /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300364 xfer_len = roundup(min(dma_len - offset, size), 8);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300365
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300366 size = max(0, size - xfer_len);
367
368 pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
369 pxa_dma->sg_cpu[i].dtadr = sg_dma_address(sg) + offset;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300370 pxa_dma->sg_cpu[i].dcmd =
371 DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300372#ifdef DEBUG
373 if (!i)
374 pxa_dma->sg_cpu[i].dcmd |= DCMD_STARTIRQEN;
375#endif
Mike Rapoporta5462e52008-04-22 10:36:32 -0300376 pxa_dma->sg_cpu[i].ddadr =
377 pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300378
379 dev_vdbg(pcdev->dev, "DMA: desc.%08x->@phys=0x%08x, len=%d\n",
380 pxa_dma->sg_dma + i * sizeof(struct pxa_dma_desc),
381 sg_dma_address(sg) + offset, xfer_len);
382 offset = 0;
383
384 if (size == 0)
385 break;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300386 }
387
Robert Jarzmik256b0232009-03-31 03:44:21 -0300388 pxa_dma->sg_cpu[sglen].ddadr = DDADR_STOP;
389 pxa_dma->sg_cpu[sglen].dcmd = DCMD_FLOWSRC | DCMD_BURST8 | DCMD_ENDIRQEN;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300390
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300391 /*
392 * Handle 1 special case :
393 * - in 3 planes (YUV422P format), we might finish with xfer_len equal
394 * to dma_len (end on PAGE boundary). In this case, the sg element
395 * for next plane should be the next after the last used to store the
396 * last scatter gather RAM page
397 */
398 if (xfer_len >= dma_len) {
399 *sg_first_ofs = xfer_len - dma_len;
400 *sg_first = sg_next(sg);
401 } else {
402 *sg_first_ofs = xfer_len;
403 *sg_first = sg;
404 }
405
Mike Rapoporta5462e52008-04-22 10:36:32 -0300406 return 0;
407}
408
Robert Jarzmik256b0232009-03-31 03:44:21 -0300409static void pxa_videobuf_set_actdma(struct pxa_camera_dev *pcdev,
410 struct pxa_buffer *buf)
411{
412 buf->active_dma = DMA_Y;
413 if (pcdev->channels == 3)
414 buf->active_dma |= DMA_U | DMA_V;
415}
416
417/*
418 * Please check the DMA prepared buffer structure in :
419 * Documentation/video4linux/pxa_camera.txt
420 * Please check also in pxa_camera_check_link_miss() to understand why DMA chain
421 * modification while DMA chain is running will work anyway.
422 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300423static int pxa_videobuf_prepare(struct videobuf_queue *vq,
424 struct videobuf_buffer *vb, enum v4l2_field field)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300425{
426 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300427 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300428 struct pxa_camera_dev *pcdev = ici->priv;
429 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300430 int ret;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300431 int size_y, size_u = 0, size_v = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300432
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300433 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300434 vb, vb->baddr, vb->bsize);
435
436 /* Added list head initialization on alloc */
437 WARN_ON(!list_empty(&vb->queue));
438
439#ifdef DEBUG
440 /* This can be useful if you want to see if we actually fill
441 * the buffer with something */
442 memset((void *)vb->baddr, 0xaa, vb->bsize);
443#endif
444
445 BUG_ON(NULL == icd->current_fmt);
446
447 /* I think, in buf_prepare you only have to protect global data,
448 * the actual buffer is yours */
449 buf->inwork = 1;
450
451 if (buf->fmt != icd->current_fmt ||
452 vb->width != icd->width ||
453 vb->height != icd->height ||
454 vb->field != field) {
455 buf->fmt = icd->current_fmt;
456 vb->width = icd->width;
457 vb->height = icd->height;
458 vb->field = field;
459 vb->state = VIDEOBUF_NEEDS_INIT;
460 }
461
462 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
463 if (0 != vb->baddr && vb->bsize < vb->size) {
464 ret = -EINVAL;
465 goto out;
466 }
467
468 if (vb->state == VIDEOBUF_NEEDS_INIT) {
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300469 int size = vb->size;
470 int next_ofs = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300471 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300472 struct scatterlist *sg;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300473
474 ret = videobuf_iolock(vq, vb, NULL);
475 if (ret)
476 goto fail;
477
Guennadi Liakhovetski5aa21102008-04-22 10:40:23 -0300478 if (pcdev->channels == 3) {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300479 size_y = size / 2;
480 size_u = size_v = size / 4;
481 } else {
Mike Rapoporta5462e52008-04-22 10:36:32 -0300482 size_y = size;
483 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300484
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300485 sg = dma->sglist;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300486
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300487 /* init DMA for Y channel */
488 ret = pxa_init_dma_channel(pcdev, buf, dma, 0, CIBR0, size_y,
489 &sg, &next_ofs);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300490 if (ret) {
491 dev_err(pcdev->dev,
492 "DMA initialization for Y/RGB failed\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300493 goto fail;
494 }
495
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300496 /* init DMA for U channel */
497 if (size_u)
498 ret = pxa_init_dma_channel(pcdev, buf, dma, 1, CIBR1,
499 size_u, &sg, &next_ofs);
500 if (ret) {
501 dev_err(pcdev->dev,
502 "DMA initialization for U failed\n");
503 goto fail_u;
504 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300505
Robert Jarzmik37f5aef2009-03-31 03:44:21 -0300506 /* init DMA for V channel */
507 if (size_v)
508 ret = pxa_init_dma_channel(pcdev, buf, dma, 2, CIBR2,
509 size_v, &sg, &next_ofs);
510 if (ret) {
511 dev_err(pcdev->dev,
512 "DMA initialization for V failed\n");
513 goto fail_v;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300514 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300515
516 vb->state = VIDEOBUF_PREPARED;
517 }
518
519 buf->inwork = 0;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300520 pxa_videobuf_set_actdma(pcdev, buf);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300521
522 return 0;
523
Mike Rapoporta5462e52008-04-22 10:36:32 -0300524fail_v:
525 dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
526 buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
527fail_u:
528 dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
529 buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300530fail:
531 free_buffer(vq, buf);
532out:
533 buf->inwork = 0;
534 return ret;
535}
536
Robert Jarzmik256b0232009-03-31 03:44:21 -0300537/**
538 * pxa_dma_start_channels - start DMA channel for active buffer
539 * @pcdev: pxa camera device
540 *
541 * Initialize DMA channels to the beginning of the active video buffer, and
542 * start these channels.
543 */
544static void pxa_dma_start_channels(struct pxa_camera_dev *pcdev)
545{
546 int i;
547 struct pxa_buffer *active;
548
549 active = pcdev->active;
550
551 for (i = 0; i < pcdev->channels; i++) {
552 dev_dbg(pcdev->dev, "%s (channel=%d) ddadr=%08x\n", __func__,
553 i, active->dmas[i].sg_dma);
554 DDADR(pcdev->dma_chans[i]) = active->dmas[i].sg_dma;
555 DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
556 }
557}
558
559static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev)
560{
561 int i;
562
563 for (i = 0; i < pcdev->channels; i++) {
564 dev_dbg(pcdev->dev, "%s (channel=%d)\n", __func__, i);
565 DCSR(pcdev->dma_chans[i]) = 0;
566 }
567}
568
569static void pxa_dma_update_sg_tail(struct pxa_camera_dev *pcdev,
570 struct pxa_buffer *buf)
571{
572 int i;
573
574 for (i = 0; i < pcdev->channels; i++)
575 pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen;
576}
577
578static void pxa_dma_add_tail_buf(struct pxa_camera_dev *pcdev,
579 struct pxa_buffer *buf)
580{
581 int i;
582 struct pxa_dma_desc *buf_last_desc;
583
584 for (i = 0; i < pcdev->channels; i++) {
585 buf_last_desc = buf->dmas[i].sg_cpu + buf->dmas[i].sglen;
586 buf_last_desc->ddadr = DDADR_STOP;
587
588 if (!pcdev->sg_tail[i])
589 continue;
590 pcdev->sg_tail[i]->ddadr = buf->dmas[i].sg_dma;
591 }
592
593 pxa_dma_update_sg_tail(pcdev, buf);
594}
595
596/**
597 * pxa_camera_start_capture - start video capturing
598 * @pcdev: camera device
599 *
600 * Launch capturing. DMA channels should not be active yet. They should get
601 * activated at the end of frame interrupt, to capture only whole frames, and
602 * never begin the capture of a partial frame.
603 */
604static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev)
605{
606 unsigned long cicr0, cifr;
607
608 dev_dbg(pcdev->dev, "%s\n", __func__);
609 /* Reset the FIFOs */
610 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
611 __raw_writel(cifr, pcdev->base + CIFR);
612 /* Enable End-Of-Frame Interrupt */
613 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
614 cicr0 &= ~CICR0_EOFM;
615 __raw_writel(cicr0, pcdev->base + CICR0);
616}
617
618static void pxa_camera_stop_capture(struct pxa_camera_dev *pcdev)
619{
620 unsigned long cicr0;
621
622 pxa_dma_stop_channels(pcdev);
623
624 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
625 __raw_writel(cicr0, pcdev->base + CICR0);
626
Robert Jarzmik8c62e222009-03-31 03:44:22 -0300627 pcdev->active = NULL;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300628 dev_dbg(pcdev->dev, "%s\n", __func__);
629}
630
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300631static void pxa_videobuf_queue(struct videobuf_queue *vq,
632 struct videobuf_buffer *vb)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300633{
634 struct soc_camera_device *icd = vq->priv_data;
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300635 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300636 struct pxa_camera_dev *pcdev = ici->priv;
637 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300638 unsigned long flags;
639
Robert Jarzmik256b0232009-03-31 03:44:21 -0300640 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d active=%p\n", __func__,
641 vb, vb->baddr, vb->bsize, pcdev->active);
642
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300643 spin_lock_irqsave(&pcdev->lock, flags);
644
645 list_add_tail(&vb->queue, &pcdev->capture);
646
647 vb->state = VIDEOBUF_ACTIVE;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300648 pxa_dma_add_tail_buf(pcdev, buf);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300649
Robert Jarzmik256b0232009-03-31 03:44:21 -0300650 if (!pcdev->active)
651 pxa_camera_start_capture(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300652
653 spin_unlock_irqrestore(&pcdev->lock, flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300654}
655
656static void pxa_videobuf_release(struct videobuf_queue *vq,
657 struct videobuf_buffer *vb)
658{
659 struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
660#ifdef DEBUG
661 struct soc_camera_device *icd = vq->priv_data;
662
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300663 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300664 vb, vb->baddr, vb->bsize);
665
666 switch (vb->state) {
667 case VIDEOBUF_ACTIVE:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300668 dev_dbg(&icd->dev, "%s (active)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300669 break;
670 case VIDEOBUF_QUEUED:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300671 dev_dbg(&icd->dev, "%s (queued)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300672 break;
673 case VIDEOBUF_PREPARED:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300674 dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300675 break;
676 default:
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300677 dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300678 break;
679 }
680#endif
681
682 free_buffer(vq, buf);
683}
684
Mike Rapoporta5462e52008-04-22 10:36:32 -0300685static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
686 struct videobuf_buffer *vb,
687 struct pxa_buffer *buf)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300688{
Robert Jarzmik256b0232009-03-31 03:44:21 -0300689 int i;
Eric Miao5ca11fa2008-12-18 11:15:50 -0300690
Mike Rapoporta5462e52008-04-22 10:36:32 -0300691 /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
692 list_del_init(&vb->queue);
693 vb->state = VIDEOBUF_DONE;
694 do_gettimeofday(&vb->ts);
695 vb->field_count++;
696 wake_up(&vb->done);
Robert Jarzmik256b0232009-03-31 03:44:21 -0300697 dev_dbg(pcdev->dev, "%s dequeud buffer (vb=0x%p)\n", __func__, vb);
Mike Rapoporta5462e52008-04-22 10:36:32 -0300698
699 if (list_empty(&pcdev->capture)) {
Robert Jarzmik256b0232009-03-31 03:44:21 -0300700 pxa_camera_stop_capture(pcdev);
Robert Jarzmik256b0232009-03-31 03:44:21 -0300701 for (i = 0; i < pcdev->channels; i++)
702 pcdev->sg_tail[i] = NULL;
Mike Rapoporta5462e52008-04-22 10:36:32 -0300703 return;
704 }
705
706 pcdev->active = list_entry(pcdev->capture.next,
707 struct pxa_buffer, vb.queue);
708}
709
Robert Jarzmik256b0232009-03-31 03:44:21 -0300710/**
711 * pxa_camera_check_link_miss - check missed DMA linking
712 * @pcdev: camera device
713 *
714 * The DMA chaining is done with DMA running. This means a tiny temporal window
715 * remains, where a buffer is queued on the chain, while the chain is already
716 * stopped. This means the tailed buffer would never be transfered by DMA.
717 * This function restarts the capture for this corner case, where :
718 * - DADR() == DADDR_STOP
719 * - a videobuffer is queued on the pcdev->capture list
720 *
721 * Please check the "DMA hot chaining timeslice issue" in
722 * Documentation/video4linux/pxa_camera.txt
723 *
724 * Context: should only be called within the dma irq handler
725 */
726static void pxa_camera_check_link_miss(struct pxa_camera_dev *pcdev)
727{
728 int i, is_dma_stopped = 1;
729
730 for (i = 0; i < pcdev->channels; i++)
731 if (DDADR(pcdev->dma_chans[i]) != DDADR_STOP)
732 is_dma_stopped = 0;
733 dev_dbg(pcdev->dev, "%s : top queued buffer=%p, dma_stopped=%d\n",
734 __func__, pcdev->active, is_dma_stopped);
735 if (pcdev->active && is_dma_stopped)
736 pxa_camera_start_capture(pcdev);
737}
738
Mike Rapoporta5462e52008-04-22 10:36:32 -0300739static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
740 enum pxa_camera_active_dma act_dma)
741{
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300742 struct pxa_buffer *buf;
743 unsigned long flags;
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300744 u32 status, camera_status, overrun;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300745 struct videobuf_buffer *vb;
746
747 spin_lock_irqsave(&pcdev->lock, flags);
748
Mike Rapoporta5462e52008-04-22 10:36:32 -0300749 status = DCSR(channel);
Robert Jarzmik256b0232009-03-31 03:44:21 -0300750 DCSR(channel) = status;
751
752 camera_status = __raw_readl(pcdev->base + CISR);
753 overrun = CISR_IFO_0;
754 if (pcdev->channels == 3)
755 overrun |= CISR_IFO_1 | CISR_IFO_2;
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300756
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300757 if (status & DCSR_BUSERR) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300758 dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300759 goto out;
760 }
761
Robert Jarzmik256b0232009-03-31 03:44:21 -0300762 if (!(status & (DCSR_ENDINTR | DCSR_STARTINTR))) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300763 dev_err(pcdev->dev, "Unknown DMA IRQ source, "
764 "status: 0x%08x\n", status);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300765 goto out;
766 }
767
Robert Jarzmik8c62e222009-03-31 03:44:22 -0300768 /*
769 * pcdev->active should not be NULL in DMA irq handler.
770 *
771 * But there is one corner case : if capture was stopped due to an
772 * overrun of channel 1, and at that same channel 2 was completed.
773 *
774 * When handling the overrun in DMA irq for channel 1, we'll stop the
775 * capture and restart it (and thus set pcdev->active to NULL). But the
776 * DMA irq handler will already be pending for channel 2. So on entering
777 * the DMA irq handler for channel 2 there will be no active buffer, yet
778 * that is normal.
779 */
780 if (!pcdev->active)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300781 goto out;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300782
783 vb = &pcdev->active->vb;
784 buf = container_of(vb, struct pxa_buffer, vb);
785 WARN_ON(buf->inwork || list_empty(&vb->queue));
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300786
Robert Jarzmik256b0232009-03-31 03:44:21 -0300787 dev_dbg(pcdev->dev, "%s channel=%d %s%s(vb=0x%p) dma.desc=%x\n",
788 __func__, channel, status & DCSR_STARTINTR ? "SOF " : "",
789 status & DCSR_ENDINTR ? "EOF " : "", vb, DDADR(channel));
790
791 if (status & DCSR_ENDINTR) {
Robert Jarzmik8c62e222009-03-31 03:44:22 -0300792 /*
793 * It's normal if the last frame creates an overrun, as there
794 * are no more DMA descriptors to fetch from QCI fifos
795 */
796 if (camera_status & overrun &&
797 !list_is_last(pcdev->capture.next, &pcdev->capture)) {
Robert Jarzmik256b0232009-03-31 03:44:21 -0300798 dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n",
799 camera_status);
800 pxa_camera_stop_capture(pcdev);
801 pxa_camera_start_capture(pcdev);
802 goto out;
803 }
804 buf->active_dma &= ~act_dma;
805 if (!buf->active_dma) {
806 pxa_camera_wakeup(pcdev, vb, buf);
807 pxa_camera_check_link_miss(pcdev);
808 }
809 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300810
811out:
812 spin_unlock_irqrestore(&pcdev->lock, flags);
813}
814
Mike Rapoporta5462e52008-04-22 10:36:32 -0300815static void pxa_camera_dma_irq_y(int channel, void *data)
816{
817 struct pxa_camera_dev *pcdev = data;
818 pxa_camera_dma_irq(channel, pcdev, DMA_Y);
819}
820
821static void pxa_camera_dma_irq_u(int channel, void *data)
822{
823 struct pxa_camera_dev *pcdev = data;
824 pxa_camera_dma_irq(channel, pcdev, DMA_U);
825}
826
827static void pxa_camera_dma_irq_v(int channel, void *data)
828{
829 struct pxa_camera_dev *pcdev = data;
830 pxa_camera_dma_irq(channel, pcdev, DMA_V);
831}
832
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300833static struct videobuf_queue_ops pxa_videobuf_ops = {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300834 .buf_setup = pxa_videobuf_setup,
835 .buf_prepare = pxa_videobuf_prepare,
836 .buf_queue = pxa_videobuf_queue,
837 .buf_release = pxa_videobuf_release,
838};
839
Magnus Damma034d1b2008-07-11 20:59:34 -0300840static void pxa_camera_init_videobuf(struct videobuf_queue *q,
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300841 struct soc_camera_device *icd)
842{
Magnus Damma034d1b2008-07-11 20:59:34 -0300843 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
844 struct pxa_camera_dev *pcdev = ici->priv;
845
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300846 /* We must pass NULL as dev pointer, then all pci_* dma operations
847 * transform to normal dma_* ones. */
Magnus Damma034d1b2008-07-11 20:59:34 -0300848 videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
Paulius Zaleckas092d3922008-07-11 20:50:31 -0300849 V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
850 sizeof(struct pxa_buffer), icd);
851}
852
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300853static u32 mclk_get_divisor(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300854{
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300855 unsigned long mclk = pcdev->mclk;
856 u32 div;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300857 unsigned long lcdclk;
858
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300859 lcdclk = clk_get_rate(pcdev->clk);
860 pcdev->ciclk = lcdclk;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300861
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300862 /* mclk <= ciclk / 4 (27.4.2) */
863 if (mclk > lcdclk / 4) {
864 mclk = lcdclk / 4;
865 dev_warn(pcdev->dev, "Limiting master clock to %lu\n", mclk);
866 }
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300867
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300868 /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
869 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
870
871 /* If we're not supplying MCLK, leave it at 0 */
872 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
873 pcdev->mclk = lcdclk / (2 * (div + 1));
874
875 dev_dbg(pcdev->dev, "LCD clock %luHz, target freq %luHz, "
876 "divisor %u\n", lcdclk, mclk, div);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300877
878 return div;
879}
880
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300881static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
882 unsigned long pclk)
883{
884 /* We want a timeout > 1 pixel time, not ">=" */
885 u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
886
887 __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
888}
889
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300890static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300891{
892 struct pxacamera_platform_data *pdata = pcdev->pdata;
893 u32 cicr4 = 0;
894
895 dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
896 pcdev, pdata);
897
898 if (pdata && pdata->init) {
Harvey Harrison7e28adb2008-04-08 23:20:00 -0300899 dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300900 pdata->init(pcdev->dev);
901 }
902
Eric Miao5ca11fa2008-12-18 11:15:50 -0300903 /* disable all interrupts */
904 __raw_writel(0x3ff, pcdev->base + CICR0);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300905
906 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
907 cicr4 |= CICR4_PCLK_EN;
908 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
909 cicr4 |= CICR4_MCLK_EN;
910 if (pcdev->platform_flags & PXA_CAMERA_PCP)
911 cicr4 |= CICR4_PCP;
912 if (pcdev->platform_flags & PXA_CAMERA_HSP)
913 cicr4 |= CICR4_HSP;
914 if (pcdev->platform_flags & PXA_CAMERA_VSP)
915 cicr4 |= CICR4_VSP;
916
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -0300917 __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
918
919 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
920 /* Initialise the timeout under the assumption pclk = mclk */
921 recalculate_fifo_timeout(pcdev, pcdev->mclk);
922 else
923 /* "Safe default" - 13MHz */
924 recalculate_fifo_timeout(pcdev, 13000000);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300925
926 clk_enable(pcdev->clk);
927}
928
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300929static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300930{
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300931 clk_disable(pcdev->clk);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300932}
933
934static irqreturn_t pxa_camera_irq(int irq, void *data)
935{
936 struct pxa_camera_dev *pcdev = data;
Eric Miao5ca11fa2008-12-18 11:15:50 -0300937 unsigned long status, cicr0;
Robert Jarzmik256b0232009-03-31 03:44:21 -0300938 struct pxa_buffer *buf;
939 struct videobuf_buffer *vb;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300940
Eric Miao5ca11fa2008-12-18 11:15:50 -0300941 status = __raw_readl(pcdev->base + CISR);
942 dev_dbg(pcdev->dev, "Camera interrupt status 0x%lx\n", status);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300943
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300944 if (!status)
945 return IRQ_NONE;
946
Eric Miao5ca11fa2008-12-18 11:15:50 -0300947 __raw_writel(status, pcdev->base + CISR);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300948
949 if (status & CISR_EOF) {
Robert Jarzmik256b0232009-03-31 03:44:21 -0300950 pcdev->active = list_first_entry(&pcdev->capture,
951 struct pxa_buffer, vb.queue);
952 vb = &pcdev->active->vb;
953 buf = container_of(vb, struct pxa_buffer, vb);
954 pxa_videobuf_set_actdma(pcdev, buf);
955
956 pxa_dma_start_channels(pcdev);
957
Eric Miao5ca11fa2008-12-18 11:15:50 -0300958 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
959 __raw_writel(cicr0, pcdev->base + CICR0);
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -0300960 }
961
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300962 return IRQ_HANDLED;
963}
964
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300965/*
966 * The following two functions absolutely depend on the fact, that
967 * there can be only one camera on PXA quick capture interface
968 * Called with .video_lock held
969 */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300970static int pxa_camera_add_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300971{
972 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
973 struct pxa_camera_dev *pcdev = ici->priv;
974 int ret;
975
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300976 if (pcdev->icd) {
977 ret = -EBUSY;
978 goto ebusy;
979 }
980
981 dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
982 icd->devnum);
983
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300984 pxa_camera_activate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300985 ret = icd->ops->init(icd);
986
987 if (!ret)
988 pcdev->icd = icd;
989
990ebusy:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300991 return ret;
992}
993
Guennadi Liakhovetski1c3bb742008-12-18 12:28:54 -0300994/* Called with .video_lock held */
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -0300995static void pxa_camera_remove_device(struct soc_camera_device *icd)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -0300996{
997 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
998 struct pxa_camera_dev *pcdev = ici->priv;
999
1000 BUG_ON(icd != pcdev->icd);
1001
1002 dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
1003 icd->devnum);
1004
1005 /* disable capture, disable interrupts */
Eric Miao5ca11fa2008-12-18 11:15:50 -03001006 __raw_writel(0x3ff, pcdev->base + CICR0);
Mike Rapoporta5462e52008-04-22 10:36:32 -03001007
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001008 /* Stop DMA engine */
Mike Rapoporta5462e52008-04-22 10:36:32 -03001009 DCSR(pcdev->dma_chans[0]) = 0;
1010 DCSR(pcdev->dma_chans[1]) = 0;
1011 DCSR(pcdev->dma_chans[2]) = 0;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001012
1013 icd->ops->release(icd);
1014
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001015 pxa_camera_deactivate(pcdev);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001016
1017 pcdev->icd = NULL;
1018}
1019
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001020static int test_platform_param(struct pxa_camera_dev *pcdev,
1021 unsigned char buswidth, unsigned long *flags)
1022{
1023 /*
1024 * Platform specified synchronization and pixel clock polarities are
1025 * only a recommendation and are only used during probing. The PXA270
1026 * quick capture interface supports both.
1027 */
1028 *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
1029 SOCAM_MASTER : SOCAM_SLAVE) |
1030 SOCAM_HSYNC_ACTIVE_HIGH |
1031 SOCAM_HSYNC_ACTIVE_LOW |
1032 SOCAM_VSYNC_ACTIVE_HIGH |
1033 SOCAM_VSYNC_ACTIVE_LOW |
Guennadi Liakhovetski2d9329f2009-02-23 12:12:58 -03001034 SOCAM_DATA_ACTIVE_HIGH |
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001035 SOCAM_PCLK_SAMPLE_RISING |
1036 SOCAM_PCLK_SAMPLE_FALLING;
1037
1038 /* If requested data width is supported by the platform, use it */
1039 switch (buswidth) {
1040 case 10:
1041 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
1042 return -EINVAL;
1043 *flags |= SOCAM_DATAWIDTH_10;
1044 break;
1045 case 9:
1046 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
1047 return -EINVAL;
1048 *flags |= SOCAM_DATAWIDTH_9;
1049 break;
1050 case 8:
1051 if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
1052 return -EINVAL;
1053 *flags |= SOCAM_DATAWIDTH_8;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001054 break;
1055 default:
1056 return -EINVAL;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001057 }
1058
1059 return 0;
1060}
1061
1062static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001063{
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001064 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001065 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001066 unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
Eric Miao5ca11fa2008-12-18 11:15:50 -03001067 u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001068 int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001069
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001070 if (ret < 0)
1071 return ret;
1072
1073 camera_flags = icd->ops->query_bus_param(icd);
1074
1075 common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
1076 if (!common_flags)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001077 return -EINVAL;
1078
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -03001079 pcdev->channels = 1;
1080
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001081 /* Make choises, based on platform preferences */
1082 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
1083 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
1084 if (pcdev->platform_flags & PXA_CAMERA_HSP)
1085 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
1086 else
1087 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
1088 }
1089
1090 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
1091 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
1092 if (pcdev->platform_flags & PXA_CAMERA_VSP)
1093 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
1094 else
1095 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
1096 }
1097
1098 if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
1099 (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
1100 if (pcdev->platform_flags & PXA_CAMERA_PCP)
1101 common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
1102 else
1103 common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
1104 }
1105
1106 ret = icd->ops->set_bus_param(icd, common_flags);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001107 if (ret < 0)
1108 return ret;
1109
1110 /* Datawidth is now guaranteed to be equal to one of the three values.
1111 * We fix bit-per-pixel equal to data-width... */
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001112 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
1113 case SOCAM_DATAWIDTH_10:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001114 dw = 4;
1115 bpp = 0x40;
1116 break;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001117 case SOCAM_DATAWIDTH_9:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001118 dw = 3;
1119 bpp = 0x20;
1120 break;
1121 default:
1122 /* Actually it can only be 8 now,
1123 * default is just to silence compiler warnings */
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001124 case SOCAM_DATAWIDTH_8:
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001125 dw = 2;
1126 bpp = 0;
1127 }
1128
1129 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1130 cicr4 |= CICR4_PCLK_EN;
1131 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
1132 cicr4 |= CICR4_MCLK_EN;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001133 if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001134 cicr4 |= CICR4_PCP;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001135 if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001136 cicr4 |= CICR4_HSP;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001137 if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001138 cicr4 |= CICR4_VSP;
1139
Eric Miao5ca11fa2008-12-18 11:15:50 -03001140 cicr0 = __raw_readl(pcdev->base + CICR0);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001141 if (cicr0 & CICR0_ENB)
Eric Miao5ca11fa2008-12-18 11:15:50 -03001142 __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
Mike Rapoporta5462e52008-04-22 10:36:32 -03001143
1144 cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
1145
1146 switch (pixfmt) {
1147 case V4L2_PIX_FMT_YUV422P:
Guennadi Liakhovetskie7c50682008-04-22 10:37:49 -03001148 pcdev->channels = 3;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001149 cicr1 |= CICR1_YCBCR_F;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001150 /*
1151 * Normally, pxa bus wants as input UYVY format. We allow all
1152 * reorderings of the YUV422 format, as no processing is done,
1153 * and the YUV stream is just passed through without any
1154 * transformation. Note that UYVY is the only format that
1155 * should be used if pxa framebuffer Overlay2 is used.
1156 */
1157 case V4L2_PIX_FMT_UYVY:
1158 case V4L2_PIX_FMT_VYUY:
Mike Rapoporta5462e52008-04-22 10:36:32 -03001159 case V4L2_PIX_FMT_YUYV:
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001160 case V4L2_PIX_FMT_YVYU:
Mike Rapoporta5462e52008-04-22 10:36:32 -03001161 cicr1 |= CICR1_COLOR_SP_VAL(2);
1162 break;
1163 case V4L2_PIX_FMT_RGB555:
1164 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
1165 CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
1166 break;
1167 case V4L2_PIX_FMT_RGB565:
1168 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
1169 break;
1170 }
1171
Eric Miao5ca11fa2008-12-18 11:15:50 -03001172 cicr2 = 0;
1173 cicr3 = CICR3_LPF_VAL(icd->height - 1) |
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001174 CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001175 cicr4 |= pcdev->mclk_divisor;
Eric Miao5ca11fa2008-12-18 11:15:50 -03001176
1177 __raw_writel(cicr1, pcdev->base + CICR1);
1178 __raw_writel(cicr2, pcdev->base + CICR2);
1179 __raw_writel(cicr3, pcdev->base + CICR3);
1180 __raw_writel(cicr4, pcdev->base + CICR4);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001181
1182 /* CIF interrupts are not used, only DMA */
Eric Miao5ca11fa2008-12-18 11:15:50 -03001183 cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
1184 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
1185 cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
1186 __raw_writel(cicr0, pcdev->base + CICR0);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001187
1188 return 0;
1189}
1190
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001191static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
1192 unsigned char buswidth)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001193{
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001194 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001195 struct pxa_camera_dev *pcdev = ici->priv;
1196 unsigned long bus_flags, camera_flags;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001197 int ret = test_platform_param(pcdev, buswidth, &bus_flags);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001198
1199 if (ret < 0)
1200 return ret;
1201
1202 camera_flags = icd->ops->query_bus_param(icd);
1203
1204 return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
1205}
1206
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001207static const struct soc_camera_data_format pxa_camera_formats[] = {
1208 {
1209 .name = "Planar YUV422 16 bit",
1210 .depth = 16,
1211 .fourcc = V4L2_PIX_FMT_YUV422P,
1212 .colorspace = V4L2_COLORSPACE_JPEG,
1213 },
1214};
1215
1216static bool buswidth_supported(struct soc_camera_device *icd, int depth)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001217{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001218 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1219 struct pxa_camera_dev *pcdev = ici->priv;
1220
1221 switch (depth) {
1222 case 8:
1223 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8);
1224 case 9:
1225 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9);
1226 case 10:
1227 return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10);
1228 }
1229 return false;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001230}
1231
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001232static int required_buswidth(const struct soc_camera_data_format *fmt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001233{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001234 switch (fmt->fourcc) {
1235 case V4L2_PIX_FMT_UYVY:
1236 case V4L2_PIX_FMT_VYUY:
1237 case V4L2_PIX_FMT_YUYV:
1238 case V4L2_PIX_FMT_YVYU:
1239 case V4L2_PIX_FMT_RGB565:
1240 case V4L2_PIX_FMT_RGB555:
1241 return 8;
1242 default:
1243 return fmt->depth;
1244 }
1245}
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001246
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001247static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
1248 struct soc_camera_format_xlate *xlate)
1249{
1250 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1251 int formats = 0, buswidth, ret;
1252
1253 buswidth = required_buswidth(icd->formats + idx);
1254
1255 if (!buswidth_supported(icd, buswidth))
1256 return 0;
1257
1258 ret = pxa_camera_try_bus_param(icd, buswidth);
1259 if (ret < 0)
1260 return 0;
1261
1262 switch (icd->formats[idx].fourcc) {
1263 case V4L2_PIX_FMT_UYVY:
1264 formats++;
1265 if (xlate) {
1266 xlate->host_fmt = &pxa_camera_formats[0];
1267 xlate->cam_fmt = icd->formats + idx;
1268 xlate->buswidth = buswidth;
1269 xlate++;
1270 dev_dbg(&ici->dev, "Providing format %s using %s\n",
1271 pxa_camera_formats[0].name,
1272 icd->formats[idx].name);
1273 }
1274 case V4L2_PIX_FMT_VYUY:
1275 case V4L2_PIX_FMT_YUYV:
1276 case V4L2_PIX_FMT_YVYU:
1277 case V4L2_PIX_FMT_RGB565:
1278 case V4L2_PIX_FMT_RGB555:
1279 formats++;
1280 if (xlate) {
1281 xlate->host_fmt = icd->formats + idx;
1282 xlate->cam_fmt = icd->formats + idx;
1283 xlate->buswidth = buswidth;
1284 xlate++;
1285 dev_dbg(&ici->dev, "Providing format %s packed\n",
1286 icd->formats[idx].name);
1287 }
1288 break;
1289 default:
1290 /* Generic pass-through */
1291 formats++;
1292 if (xlate) {
1293 xlate->host_fmt = icd->formats + idx;
1294 xlate->cam_fmt = icd->formats + idx;
1295 xlate->buswidth = icd->formats[idx].depth;
1296 xlate++;
1297 dev_dbg(&ici->dev,
1298 "Providing format %s in pass-through mode\n",
1299 icd->formats[idx].name);
1300 }
1301 }
1302
1303 return formats;
1304}
1305
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001306static int pxa_camera_set_crop(struct soc_camera_device *icd,
1307 struct v4l2_rect *rect)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001308{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001309 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001310 struct pxa_camera_dev *pcdev = ici->priv;
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001311 struct soc_camera_sense sense = {
1312 .master_clock = pcdev->mclk,
1313 .pixel_clock_max = pcdev->ciclk / 4,
1314 };
Guennadi Liakhovetski0ad675e2009-02-23 12:11:25 -03001315 int ret;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001316
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001317 /* If PCLK is used to latch data from the sensor, check sense */
1318 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1319 icd->sense = &sense;
1320
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001321 ret = icd->ops->set_crop(icd, rect);
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001322
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001323 icd->sense = NULL;
1324
1325 if (ret < 0) {
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001326 dev_warn(&ici->dev, "Failed to crop to %ux%u@%u:%u\n",
1327 rect->width, rect->height, rect->left, rect->top);
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001328 } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
1329 if (sense.pixel_clock > sense.pixel_clock_max) {
1330 dev_err(&ici->dev,
1331 "pixel clock %lu set by the camera too high!",
1332 sense.pixel_clock);
1333 return -EIO;
1334 }
1335 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1336 }
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001337
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001338 return ret;
1339}
1340
1341static int pxa_camera_set_fmt(struct soc_camera_device *icd,
1342 struct v4l2_format *f)
1343{
1344 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1345 struct pxa_camera_dev *pcdev = ici->priv;
1346 const struct soc_camera_data_format *cam_fmt = NULL;
1347 const struct soc_camera_format_xlate *xlate = NULL;
1348 struct soc_camera_sense sense = {
1349 .master_clock = pcdev->mclk,
1350 .pixel_clock_max = pcdev->ciclk / 4,
1351 };
1352 struct v4l2_pix_format *pix = &f->fmt.pix;
1353 struct v4l2_format cam_f = *f;
1354 int ret;
1355
1356 xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
1357 if (!xlate) {
1358 dev_warn(&ici->dev, "Format %x not found\n", pix->pixelformat);
1359 return -EINVAL;
1360 }
1361
1362 cam_fmt = xlate->cam_fmt;
1363
1364 /* If PCLK is used to latch data from the sensor, check sense */
1365 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1366 icd->sense = &sense;
1367
1368 cam_f.fmt.pix.pixelformat = cam_fmt->fourcc;
1369 ret = icd->ops->set_fmt(icd, &cam_f);
1370
1371 icd->sense = NULL;
1372
1373 if (ret < 0) {
1374 dev_warn(&ici->dev, "Failed to configure for format %x\n",
1375 pix->pixelformat);
1376 } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
1377 if (sense.pixel_clock > sense.pixel_clock_max) {
1378 dev_err(&ici->dev,
1379 "pixel clock %lu set by the camera too high!",
1380 sense.pixel_clock);
1381 return -EIO;
1382 }
1383 recalculate_fifo_timeout(pcdev, sense.pixel_clock);
1384 }
1385
1386 if (!ret) {
Guennadi Liakhovetski0ad675e2009-02-23 12:11:25 -03001387 icd->buswidth = xlate->buswidth;
1388 icd->current_fmt = xlate->host_fmt;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001389 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001390
1391 return ret;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001392}
1393
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001394static int pxa_camera_try_fmt(struct soc_camera_device *icd,
1395 struct v4l2_format *f)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001396{
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001397 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1398 const struct soc_camera_format_xlate *xlate;
1399 struct v4l2_pix_format *pix = &f->fmt.pix;
1400 __u32 pixfmt = pix->pixelformat;
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -03001401 enum v4l2_field field;
Guennadi Liakhovetskibf507152008-12-18 11:53:51 -03001402 int ret;
Guennadi Liakhovetskia2c8c682008-12-01 09:44:53 -03001403
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001404 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1405 if (!xlate) {
1406 dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001407 return -EINVAL;
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001408 }
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001409
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001410 /* limit to pxa hardware capabilities */
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001411 if (pix->height < 32)
1412 pix->height = 32;
1413 if (pix->height > 2048)
1414 pix->height = 2048;
1415 if (pix->width < 48)
1416 pix->width = 48;
1417 if (pix->width > 2048)
1418 pix->width = 2048;
1419 pix->width &= ~0x01;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001420
Robert Jarzmik92a83372009-03-31 03:44:21 -03001421 /*
1422 * YUV422P planar format requires images size to be a 16 bytes
1423 * multiple. If not, zeros will be inserted between Y and U planes, and
1424 * U and V planes, and YUV422P standard would be violated.
1425 */
1426 if (xlate->host_fmt->fourcc == V4L2_PIX_FMT_YUV422P) {
1427 if (!IS_ALIGNED(pix->width * pix->height, YUV422P_SIZE_ALIGN))
1428 pix->height = ALIGN(pix->height, YUV422P_X_Y_ALIGN);
1429 if (!IS_ALIGNED(pix->width * pix->height, YUV422P_SIZE_ALIGN))
1430 pix->width = ALIGN(pix->width, YUV422P_X_Y_ALIGN);
1431 }
1432
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001433 pix->bytesperline = pix->width *
1434 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
1435 pix->sizeimage = pix->height * pix->bytesperline;
Guennadi Liakhovetski25c4d742008-12-01 09:44:59 -03001436
Guennadi Liakhovetskibf507152008-12-18 11:53:51 -03001437 /* camera has to see its format, but the user the original one */
1438 pix->pixelformat = xlate->cam_fmt->fourcc;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001439 /* limit to sensor capabilities */
Guennadi Liakhovetskibf507152008-12-18 11:53:51 -03001440 ret = icd->ops->try_fmt(icd, f);
1441 pix->pixelformat = xlate->host_fmt->fourcc;
1442
Guennadi Liakhovetski06daa1a2008-12-18 12:52:08 -03001443 field = pix->field;
1444
1445 if (field == V4L2_FIELD_ANY) {
1446 pix->field = V4L2_FIELD_NONE;
1447 } else if (field != V4L2_FIELD_NONE) {
1448 dev_err(&icd->dev, "Field type %d unsupported.\n", field);
1449 return -EINVAL;
1450 }
1451
Guennadi Liakhovetskibf507152008-12-18 11:53:51 -03001452 return ret;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001453}
1454
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001455static int pxa_camera_reqbufs(struct soc_camera_file *icf,
1456 struct v4l2_requestbuffers *p)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001457{
1458 int i;
1459
1460 /* This is for locking debugging only. I removed spinlocks and now I
1461 * check whether .prepare is ever called on a linked buffer, or whether
1462 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1463 * it hadn't triggered */
1464 for (i = 0; i < p->count; i++) {
1465 struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
1466 struct pxa_buffer, vb);
1467 buf->inwork = 0;
1468 INIT_LIST_HEAD(&buf->vb.queue);
1469 }
1470
1471 return 0;
1472}
1473
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001474static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001475{
1476 struct soc_camera_file *icf = file->private_data;
1477 struct pxa_buffer *buf;
1478
1479 buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
1480 vb.stream);
1481
1482 poll_wait(file, &buf->vb.done, pt);
1483
1484 if (buf->vb.state == VIDEOBUF_DONE ||
1485 buf->vb.state == VIDEOBUF_ERROR)
1486 return POLLIN|POLLRDNORM;
1487
1488 return 0;
1489}
1490
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001491static int pxa_camera_querycap(struct soc_camera_host *ici,
1492 struct v4l2_capability *cap)
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001493{
1494 /* cap->name is set by the firendly caller:-> */
1495 strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
1496 cap->version = PXA_CAM_VERSION_CODE;
1497 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1498
1499 return 0;
1500}
1501
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001502static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
1503{
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001504 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001505 struct pxa_camera_dev *pcdev = ici->priv;
1506 int i = 0, ret = 0;
1507
Eric Miao5ca11fa2008-12-18 11:15:50 -03001508 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
1509 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
1510 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
1511 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
1512 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001513
1514 if ((pcdev->icd) && (pcdev->icd->ops->suspend))
1515 ret = pcdev->icd->ops->suspend(pcdev->icd, state);
1516
1517 return ret;
1518}
1519
1520static int pxa_camera_resume(struct soc_camera_device *icd)
1521{
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -03001522 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001523 struct pxa_camera_dev *pcdev = ici->priv;
1524 int i = 0, ret = 0;
1525
Eric Miao87f3dd72008-09-08 15:26:43 +08001526 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1527 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1528 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001529
Eric Miao5ca11fa2008-12-18 11:15:50 -03001530 __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
1531 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
1532 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
1533 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
1534 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001535
1536 if ((pcdev->icd) && (pcdev->icd->ops->resume))
1537 ret = pcdev->icd->ops->resume(pcdev->icd);
1538
1539 /* Restart frame capture if active buffer exists */
Robert Jarzmik256b0232009-03-31 03:44:21 -03001540 if (!ret && pcdev->active)
1541 pxa_camera_start_capture(pcdev);
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001542
1543 return ret;
1544}
1545
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001546static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
1547 .owner = THIS_MODULE,
1548 .add = pxa_camera_add_device,
1549 .remove = pxa_camera_remove_device,
Robert Jarzmik3f6ac492008-08-02 07:10:04 -03001550 .suspend = pxa_camera_suspend,
1551 .resume = pxa_camera_resume,
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -03001552 .set_crop = pxa_camera_set_crop,
Robert Jarzmik2a48fc72008-12-01 09:45:35 -03001553 .get_formats = pxa_camera_get_formats,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -03001554 .set_fmt = pxa_camera_set_fmt,
1555 .try_fmt = pxa_camera_try_fmt,
Paulius Zaleckas092d3922008-07-11 20:50:31 -03001556 .init_videobuf = pxa_camera_init_videobuf,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001557 .reqbufs = pxa_camera_reqbufs,
1558 .poll = pxa_camera_poll,
1559 .querycap = pxa_camera_querycap,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001560 .set_bus_param = pxa_camera_set_bus_param,
1561};
1562
1563/* Should be allocated dynamically too, but we have only one. */
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001564static struct soc_camera_host pxa_soc_camera_host = {
1565 .drv_name = PXA_CAM_DRV_NAME,
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001566 .ops = &pxa_soc_camera_host_ops,
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001567};
1568
1569static int pxa_camera_probe(struct platform_device *pdev)
1570{
1571 struct pxa_camera_dev *pcdev;
1572 struct resource *res;
1573 void __iomem *base;
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001574 int irq;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001575 int err = 0;
1576
1577 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1578 irq = platform_get_irq(pdev, 0);
Guennadi Liakhovetski02da4652008-06-13 09:03:45 -03001579 if (!res || irq < 0) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001580 err = -ENODEV;
1581 goto exit;
1582 }
1583
1584 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1585 if (!pcdev) {
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001586 dev_err(&pdev->dev, "Could not allocate pcdev\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001587 err = -ENOMEM;
1588 goto exit;
1589 }
1590
Russell Kinge0d8b132008-11-11 17:52:32 +00001591 pcdev->clk = clk_get(&pdev->dev, NULL);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001592 if (IS_ERR(pcdev->clk)) {
1593 err = PTR_ERR(pcdev->clk);
1594 goto exit_kfree;
1595 }
1596
1597 dev_set_drvdata(&pdev->dev, pcdev);
1598 pcdev->res = res;
1599
1600 pcdev->pdata = pdev->dev.platform_data;
1601 pcdev->platform_flags = pcdev->pdata->flags;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -03001602 if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
1603 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001604 /* Platform hasn't set available data widths. This is bad.
1605 * Warn and use a default. */
1606 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
1607 "data widths, using default 10 bit\n");
1608 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
1609 }
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001610 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
1611 if (!pcdev->mclk) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001612 dev_warn(&pdev->dev,
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001613 "mclk == 0! Please, fix your platform data. "
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001614 "Using default 20MHz\n");
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001615 pcdev->mclk = 20000000;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001616 }
1617
Guennadi Liakhovetskicf34cba2008-12-18 11:38:03 -03001618 pcdev->dev = &pdev->dev;
1619 pcdev->mclk_divisor = mclk_get_divisor(pcdev);
1620
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001621 INIT_LIST_HEAD(&pcdev->capture);
1622 spin_lock_init(&pcdev->lock);
1623
1624 /*
1625 * Request the regions.
1626 */
1627 if (!request_mem_region(res->start, res->end - res->start + 1,
1628 PXA_CAM_DRV_NAME)) {
1629 err = -EBUSY;
1630 goto exit_clk;
1631 }
1632
1633 base = ioremap(res->start, res->end - res->start + 1);
1634 if (!base) {
1635 err = -ENOMEM;
1636 goto exit_release;
1637 }
1638 pcdev->irq = irq;
1639 pcdev->base = base;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001640
1641 /* request dma */
roel kluinde3e3b82008-09-18 17:50:15 -03001642 err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
1643 pxa_camera_dma_irq_y, pcdev);
1644 if (err < 0) {
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001645 dev_err(pcdev->dev, "Can't request DMA for Y\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001646 goto exit_iounmap;
1647 }
roel kluinde3e3b82008-09-18 17:50:15 -03001648 pcdev->dma_chans[0] = err;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001649 dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001650
roel kluinde3e3b82008-09-18 17:50:15 -03001651 err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
1652 pxa_camera_dma_irq_u, pcdev);
1653 if (err < 0) {
Mike Rapoporta5462e52008-04-22 10:36:32 -03001654 dev_err(pcdev->dev, "Can't request DMA for U\n");
Mike Rapoporta5462e52008-04-22 10:36:32 -03001655 goto exit_free_dma_y;
1656 }
roel kluinde3e3b82008-09-18 17:50:15 -03001657 pcdev->dma_chans[1] = err;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001658 dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
1659
roel kluinde3e3b82008-09-18 17:50:15 -03001660 err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
1661 pxa_camera_dma_irq_v, pcdev);
1662 if (err < 0) {
Mike Rapoporta5462e52008-04-22 10:36:32 -03001663 dev_err(pcdev->dev, "Can't request DMA for V\n");
Mike Rapoporta5462e52008-04-22 10:36:32 -03001664 goto exit_free_dma_u;
1665 }
roel kluinde3e3b82008-09-18 17:50:15 -03001666 pcdev->dma_chans[2] = err;
Mike Rapoporta5462e52008-04-22 10:36:32 -03001667 dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
1668
Eric Miao87f3dd72008-09-08 15:26:43 +08001669 DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
1670 DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
1671 DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001672
1673 /* request irq */
1674 err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
1675 pcdev);
1676 if (err) {
1677 dev_err(pcdev->dev, "Camera interrupt register failed \n");
1678 goto exit_free_dma;
1679 }
1680
1681 pxa_soc_camera_host.priv = pcdev;
1682 pxa_soc_camera_host.dev.parent = &pdev->dev;
1683 pxa_soc_camera_host.nr = pdev->id;
Guennadi Liakhovetskib8d99042008-04-04 13:41:25 -03001684 err = soc_camera_host_register(&pxa_soc_camera_host);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001685 if (err)
1686 goto exit_free_irq;
1687
1688 return 0;
1689
1690exit_free_irq:
1691 free_irq(pcdev->irq, pcdev);
1692exit_free_dma:
Mike Rapoporta5462e52008-04-22 10:36:32 -03001693 pxa_free_dma(pcdev->dma_chans[2]);
1694exit_free_dma_u:
1695 pxa_free_dma(pcdev->dma_chans[1]);
1696exit_free_dma_y:
1697 pxa_free_dma(pcdev->dma_chans[0]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001698exit_iounmap:
1699 iounmap(base);
1700exit_release:
1701 release_mem_region(res->start, res->end - res->start + 1);
1702exit_clk:
1703 clk_put(pcdev->clk);
1704exit_kfree:
1705 kfree(pcdev);
1706exit:
1707 return err;
1708}
1709
1710static int __devexit pxa_camera_remove(struct platform_device *pdev)
1711{
1712 struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
1713 struct resource *res;
1714
1715 clk_put(pcdev->clk);
1716
Mike Rapoporta5462e52008-04-22 10:36:32 -03001717 pxa_free_dma(pcdev->dma_chans[0]);
1718 pxa_free_dma(pcdev->dma_chans[1]);
1719 pxa_free_dma(pcdev->dma_chans[2]);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001720 free_irq(pcdev->irq, pcdev);
1721
1722 soc_camera_host_unregister(&pxa_soc_camera_host);
1723
1724 iounmap(pcdev->base);
1725
1726 res = pcdev->res;
1727 release_mem_region(res->start, res->end - res->start + 1);
1728
1729 kfree(pcdev);
1730
Guennadi Liakhovetski7102b772008-04-15 02:57:48 -03001731 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001732
1733 return 0;
1734}
1735
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001736static struct platform_driver pxa_camera_driver = {
1737 .driver = {
1738 .name = PXA_CAM_DRV_NAME,
1739 },
1740 .probe = pxa_camera_probe,
1741 .remove = __exit_p(pxa_camera_remove),
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001742};
1743
1744
1745static int __devinit pxa_camera_init(void)
1746{
1747 return platform_driver_register(&pxa_camera_driver);
1748}
1749
1750static void __exit pxa_camera_exit(void)
1751{
Paul Mundt01c1e4c2008-08-01 19:48:51 -03001752 platform_driver_unregister(&pxa_camera_driver);
Guennadi Liakhovetski3bc43842008-04-06 21:24:56 -03001753}
1754
1755module_init(pxa_camera_init);
1756module_exit(pxa_camera_exit);
1757
1758MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
1759MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1760MODULE_LICENSE("GPL");